diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000000..d6e9cfceb0f --- /dev/null +++ b/.clang-format @@ -0,0 +1,92 @@ +# This file is an example configuration for clang-format 5.0. +# +# Note that this style definition should only be understood as a hint +# for writing new code. The rules are still work-in-progress and does +# not yet exactly match the style we have in the existing code. + +# C Language specifics +Language: Cpp + +# Use tabs whenever we need to fill whitespace that spans at least from one tab +# stop to the next one. +# +# These settings are mirrored in .editorconfig. Keep them in sync. +UseTab: ForIndentation +TabWidth: 8 +IndentWidth: 8 +ContinuationIndentWidth: 8 +ColumnLimit: 80 + +AlignAfterOpenBracket: AlwaysBreak +AlignEscapedNewlines: Left +AlignTrailingComments: false + +# Allow putting parameters onto the next line +AllowAllArgumentsOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false + +# Don't allow short braced statements to be on a single line +# if (a) not if (a) return; +# return; +AllowShortBlocksOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortLoopsOnASingleLine: false +AllowShortLambdasOnASingleLine: None + +# Pack as many parameters or arguments onto the same line as possible +# int myFunction(int aaaaaaaaaaaa, int bbbbbbbb, +# int cccc); +BinPackArguments: true +BinPackParameters: false + +BreakBeforeBraces: Linux +BreakBeforeBinaryOperators: None +BreakBeforeTernaryOperators: false +BreakStringLiterals: false + +# The number of spaces before trailing line comments (// - comments). +# This does not affect trailing block comments (/* - comments). +SpacesBeforeTrailingComments: 1 + +# Don't insert spaces in casts +# x = (int32) y; not x = ( int32 ) y; +SpacesInCStyleCastParentheses: false + +# Don't insert spaces inside container literals +# var arr = [1, 2, 3]; not var arr = [ 1, 2, 3 ]; +SpacesInContainerLiterals: false + +# Don't insert spaces after '(' or before ')' +# f(arg); not f( arg ); +SpacesInParentheses: false + +# Don't insert spaces after '[' or before ']' +# int a[5]; not int a[ 5 ]; +SpacesInSquareBrackets: false + +# Insert a space after '{' and before '}' in struct initializers +Cpp11BracedListStyle: false + +# A list of macros that should be interpreted as foreach loops instead of as +# function calls. +ForEachMacros: + - 'git_array_foreach' + - 'git_vector_foreach' + +# The maximum number of consecutive empty lines to keep. +MaxEmptyLinesToKeep: 1 + +# No empty line at the start of a block. +KeepEmptyLinesAtTheStartOfBlocks: false + +# Penalties +# This decides what order things should be done if a line is too long +PenaltyBreakAssignment: 10 +PenaltyBreakBeforeFirstCallParameter: 30 +PenaltyBreakComment: 10 +PenaltyBreakFirstLessLess: 0 +PenaltyBreakString: 10 +PenaltyExcessCharacter: 100 +PenaltyReturnTypeOnItsOwnLine: 60 + +SortIncludes: false diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000000..bc6344b93df --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,3 @@ +{ + "postCreateCommand": "bash .devcontainer/setup.sh" +} diff --git a/.devcontainer/setup.sh b/.devcontainer/setup.sh new file mode 100755 index 00000000000..c328bf3b98b --- /dev/null +++ b/.devcontainer/setup.sh @@ -0,0 +1,9 @@ +#!/bin/sh +set -e + +sudo apt-get update +sudo apt-get -y --no-install-recommends install cmake + +mkdir build +cd build +cmake .. \ No newline at end of file diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000000..2230fd86002 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,21 @@ +; Check http://editorconfig.org/ for more informations +root = true + +[*] +indent_style = tab +tab_width = 8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.yml] +indent_style = space +indent_size = 2 + +[*.md] +indent_style = space +indent_size = 4 +trim_trailing_whitespace = false + +[*.py] +indent_style = space +indent_size = 4 diff --git a/.gitattributes b/.gitattributes index f90540b55b1..3788dc98358 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,4 @@ -*.c eol=lf -*.h eol=lf +* text=auto +ci/**/*.sh text eol=lf +script/**/*.sh text eol=lf +tests/resources/** linguist-vendored diff --git a/.github/ISSUE_TEMPLATE b/.github/ISSUE_TEMPLATE new file mode 100644 index 00000000000..717f8b93490 --- /dev/null +++ b/.github/ISSUE_TEMPLATE @@ -0,0 +1,19 @@ +You are opening a _bug report_ against the libgit2 project: we use +GitHub Issues for tracking bug reports and feature requests. If you +have a question about an API or usage, please ask on StackOverflow: +http://stackoverflow.com/questions/tagged/libgit2. If you want to +have high-level discussions about the libgit2 project itself, visit +https://github.com/libgit2/discussions. + +Otherwise, to report a bug, please fill out the reproduction steps +(below) and delete these introductory paragraphs. Thanks! + +### Reproduction steps + +### Expected behavior + +### Actual behavior + +### Version of libgit2 (release number or SHA1) + +### Operating system(s) tested diff --git a/.github/actions/download-or-build-container/action.yml b/.github/actions/download-or-build-container/action.yml new file mode 100644 index 00000000000..9c83a9836c3 --- /dev/null +++ b/.github/actions/download-or-build-container/action.yml @@ -0,0 +1,109 @@ +# Run a build step in a container or directly on the Actions runner +name: Download or Build Container +description: Download a container from the package registry, or build it if it's not found + +inputs: + container: + description: Container name + type: string + required: true + dockerfile: + description: Dockerfile + type: string + base: + description: Container base + type: string + registry: + description: Docker registry to read and publish to + type: string + default: ghcr.io + config-path: + description: Path to Dockerfiles + type: string + github_token: + description: GitHub Token + type: string + +runs: + using: 'composite' + steps: + - name: Download container + run: | + IMAGE_NAME="${{ inputs.container }}" + DOCKERFILE_PATH="${{ inputs.dockerfile }}" + DOCKER_REGISTRY="${{ inputs.registry }}" + DOCKERFILE_ROOT="${{ inputs.config-path }}" + + if [ "${DOCKERFILE_PATH}" = "" ]; then + DOCKERFILE_PATH="${DOCKERFILE_ROOT}/${IMAGE_NAME}" + else + DOCKERFILE_PATH="${DOCKERFILE_ROOT}/${DOCKERFILE_PATH}" + fi + + GIT_WORKTREE=$(cd "${GITHUB_ACTION_PATH}" && git rev-parse --show-toplevel) + echo "::: git worktree is ${GIT_WORKTREE}" + cd "${GIT_WORKTREE}" + + DOCKER_CONTAINER="${GITHUB_REPOSITORY}/${IMAGE_NAME}" + DOCKER_REGISTRY_CONTAINER="${DOCKER_REGISTRY}/${DOCKER_CONTAINER}" + + echo "dockerfile=${DOCKERFILE_PATH}" >> $GITHUB_ENV + echo "docker-container=${DOCKER_CONTAINER}" >> $GITHUB_ENV + echo "docker-registry-container=${DOCKER_REGISTRY_CONTAINER}" >> $GITHUB_ENV + + # Identify the last git commit that touched the Dockerfiles + # Use this as a hash to identify the resulting docker containers + echo "::: dockerfile path is ${DOCKERFILE_PATH}" + + DOCKER_SHA=$(git log -1 --pretty=format:"%h" -- "${DOCKERFILE_PATH}") + echo "docker-sha=${DOCKER_SHA}" >> $GITHUB_ENV + + echo "::: docker sha is ${DOCKER_SHA}" + + DOCKER_REGISTRY_CONTAINER_SHA="${DOCKER_REGISTRY_CONTAINER}:${DOCKER_SHA}" + + echo "docker-registry-container-sha=${DOCKER_REGISTRY_CONTAINER_SHA}" >> $GITHUB_ENV + echo "docker-registry-container-latest=${DOCKER_REGISTRY_CONTAINER}:latest" >> $GITHUB_ENV + + echo "::: logging in to ${DOCKER_REGISTRY} as ${GITHUB_ACTOR}" + + exists="true" + docker login https://${DOCKER_REGISTRY} -u ${GITHUB_ACTOR} -p ${GITHUB_TOKEN} || exists="false" + + echo "::: pulling ${DOCKER_REGISTRY_CONTAINER_SHA}" + + if [ "${exists}" != "false" ]; then + docker pull ${DOCKER_REGISTRY_CONTAINER_SHA} || exists="false" + fi + + if [ "${exists}" = "true" ]; then + echo "::: docker container exists in registry" + echo "docker-container-exists=true" >> $GITHUB_ENV + else + echo "::: docker container does not exist in registry" + echo "docker-container-exists=false" >> $GITHUB_ENV + fi + shell: bash + env: + GITHUB_TOKEN: ${{ inputs.github_token }} + - name: Create container + run: | + if [ "${{ inputs.base }}" != "" ]; then + BASE_ARG="--build-arg BASE=${{ inputs.base }}" + fi + + GIT_WORKTREE=$(cd "${GITHUB_ACTION_PATH}" && git rev-parse --show-toplevel) + echo "::: git worktree is ${GIT_WORKTREE}" + cd "${GIT_WORKTREE}" + + docker build -t ${{ env.docker-registry-container-sha }} --build-arg UID=$(id -u) --build-arg GID=$(id -g) ${BASE_ARG} -f ${{ env.dockerfile }} . + docker tag ${{ env.docker-registry-container-sha }} ${{ env.docker-registry-container-latest }} + shell: bash + working-directory: source/${{ inputs.config-path }} + if: env.docker-container-exists != 'true' + - name: Publish container + run: | + docker push ${{ env.docker-registry-container-sha }} + docker push ${{ env.docker-registry-container-latest }} + shell: bash + if: env.docker-container-exists != 'true' && github.event_name != 'pull_request' diff --git a/.github/actions/run-build/action.yml b/.github/actions/run-build/action.yml new file mode 100644 index 00000000000..9afcfb11e72 --- /dev/null +++ b/.github/actions/run-build/action.yml @@ -0,0 +1,51 @@ +# Run a build step in a container or directly on the Actions runner +name: Run Build Step +description: Run a build step in a container or directly on the Actions runner + +inputs: + command: + description: Command to run + type: string + required: true + container: + description: Optional container to run in + type: string + container-version: + description: Version of the container to run + type: string + shell: + description: Shell to use + type: string + required: true + default: 'bash' + +runs: + using: 'composite' + steps: + - run: | + if [ -n "${{ inputs.container }}" ]; then + docker run \ + --rm \ + --user "$(id -u):$(id -g)" \ + -v "$(pwd)/source:/home/libgit2/source" \ + -v "$(pwd)/build:/home/libgit2/build" \ + -w /home/libgit2 \ + -e ASAN_SYMBOLIZER_PATH \ + -e CC \ + -e CFLAGS \ + -e CMAKE_GENERATOR \ + -e CMAKE_OPTIONS \ + -e GITTEST_NEGOTIATE_PASSWORD \ + -e GITTEST_FLAKY_STAT \ + -e PKG_CONFIG_PATH \ + -e SKIP_NEGOTIATE_TESTS \ + -e SKIP_SSH_TESTS \ + -e SKIP_PUSHOPTIONS_TESTS \ + -e TSAN_OPTIONS \ + -e UBSAN_OPTIONS \ + ${{ inputs.container-version }} \ + /bin/bash -c "${{ inputs.command }}" + else + ${{ inputs.command }} + fi + shell: ${{ inputs.shell != '' && inputs.shell || 'bash' }} diff --git a/.github/release.yml b/.github/release.yml new file mode 100644 index 00000000000..ac05fc6de70 --- /dev/null +++ b/.github/release.yml @@ -0,0 +1,38 @@ +changelog: + categories: + - title: New features + labels: + - feature + - title: Performance improvements + labels: + - performance + - title: Bug fixes + labels: + - bug + - title: Security fixes + labels: + - security + - title: Code cleanups + labels: + - cleanup + - title: Benchmarks + labels: + - benchmarks + - title: Build and CI improvements + labels: + - build + - title: Documentation improvements + labels: + - documentation + - title: Platform compatibility fixes + labels: + - compatibility + - title: Git compatibility fixes + labels: + - git compatibility + - title: Dependency updates + labels: + - dependency + - title: Other changes + labels: + - '*' diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml new file mode 100644 index 00000000000..103f4bcd07c --- /dev/null +++ b/.github/workflows/benchmark.yml @@ -0,0 +1,201 @@ +# Benchmark libgit2 against the git reference implementation. +name: Benchmark + +on: + workflow_dispatch: + inputs: + suite: + description: Benchmark suite to run + debug: + type: boolean + description: Debugging output + deploy: + type: boolean + description: Deploy the benchmark site + schedule: + - cron: '15 4 * * *' + +permissions: + contents: read + +jobs: + # Run our benchmarks. We build a matrix with the various build + # targets and their details. Unlike our CI builds, we run these + # directly on the VM instead of in containers since we do not + # need the breadth of platform diversity. + build: + # Only run scheduled workflows on the main repository; prevents people + # from using build minutes on their forks. + if: github.repository == 'libgit2/libgit2' + + strategy: + matrix: + platform: + - name: "Linux (clang, OpenSSL)" + id: linux + os: ubuntu-latest + setup-script: ubuntu + env: + CC: clang + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=builtin -DDEPRECATE_HARD=ON -DUSE_GSSAPI=ON -DBUILD_TESTS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_CLI=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo + CMAKE_BUILD_OPTIONS: --config RelWithDebInfo + - name: "macOS" + id: macos + os: macos-latest + setup-script: osx + env: + CC: clang + CMAKE_OPTIONS: -DREGEX_BACKEND=regcomp_l -DDEPRECATE_HARD=ON -DUSE_GSSAPI=ON -DBUILD_TESTS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_CLI=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo + CMAKE_BUILD_OPTIONS: --config RelWithDebInfo + PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig + - name: "Windows (amd64, Visual Studio)" + id: windows + os: windows-2022 + setup-script: win32 + env: + ARCH: amd64 + CMAKE_GENERATOR: Visual Studio 17 2022 + CMAKE_OPTIONS: -A x64 -DDEPRECATE_HARD=ON -DBUILD_TESTS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_CLI=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo + CMAKE_BUILD_OPTIONS: --config RelWithDebInfo + fail-fast: false + name: "Benchmark ${{ matrix.platform.name }}" + env: ${{ matrix.platform.env }} + runs-on: ${{ matrix.platform.os }} + steps: + - name: Check out repository + uses: actions/checkout@v4 + with: + path: source + fetch-depth: 0 + - name: Set up benchmark environment + run: source/ci/setup-${{ matrix.platform.setup-script }}-benchmark.sh + shell: bash + if: matrix.platform.setup-script != '' + - name: Clone resource repositories + run: | + # TODO: + # we need a superior way to package the benchmark resources; lfs + # is too expensive + # git lfs install + # git clone https://github.com/libgit2/benchmark-resources resources + + git clone --bare https://github.com/git/git resources/git + + # TODO: + # avoid linux temporarily; the linux blame benchmarks are simply + # too slow to use + # git clone --bare https://github.com/torvalds/linux resources/linux + - name: Build + run: | + mkdir build && cd build + ../source/ci/build.sh + shell: bash + - name: Benchmark + run: | + # TODO: + # avoid benchmark resource path currently + #export BENCHMARK_RESOURCES_PATH="$(pwd)/resources" + export BENCHMARK_GIT_PATH="$(pwd)/resources/git" + # avoid linux temporarily; the linux blame benchmarks are simply + # too slow to use + # export BENCHMARK_LINUX_PATH="$(pwd)/resources/linux" + + if [[ "$(uname -s)" == MINGW* ]]; then + GIT2_CLI="$(cygpath -w $(pwd))\\build\\RelWithDebInfo\\git2" + else + GIT2_CLI="$(pwd)/build/git2" + fi + + if [ "${{ github.event.inputs.suite }}" != "" ]; then + SUITE_FLAG="--suite ${{ github.event.inputs.suite }}" + fi + + if [ "${{ github.event.inputs.debug }}" = "true" ]; then + DEBUG_FLAG="--debug" + fi + + mkdir benchmark && cd benchmark + ../source/tests/benchmarks/benchmark.sh \ + ${SUITE_FLAG} ${DEBUG_FLAG} \ + --admin \ + --baseline-cli "git" --cli "${GIT2_CLI}" --name libgit2 \ + --json benchmarks.json --flamegraph --zip benchmarks.zip + shell: bash + - name: Upload results + uses: actions/upload-artifact@v4 + with: + name: benchmark-${{ matrix.platform.id }} + path: benchmark + if: always() + + # Publish the results + publish: + name: Publish results + needs: [ build ] + if: always() && github.repository == 'libgit2/libgit2' + runs-on: ubuntu-latest + steps: + - name: Check out benchmark repository + uses: actions/checkout@v4 + with: + repository: libgit2/benchmarks + path: site + fetch-depth: 0 + ssh-key: ${{ secrets.BENCHMARKS_PUBLISH_KEY }} + - name: Download test results + uses: actions/download-artifact@v4 + - name: Generate API + run: | + # Move today's benchmark run into the right place + for platform in linux macos windows; do + TIMESTAMP=$(jq .time.start < "benchmark-${platform}/benchmarks.json") + TIMESTAMP_LEN=$(echo -n ${TIMESTAMP} | wc -c | xargs) + DENOMINATOR=1 + if [ "${TIMESTAMP_LEN}" = "19" ]; then + DENOMINATOR="1000000000" + elif [ "${TIMESTAMP_LEN}" = "13" ]; then + DENOMINATOR="1000" + else + echo "unknown timestamp" + exit 1 + fi + + if [[ "$(uname -s)" == "Darwin" ]]; then + DATE=$(date -R -r $(("${TIMESTAMP}/${DENOMINATOR}")) +"%Y-%m-%d") + else + DATE=$(date -d @$(("${TIMESTAMP}/${DENOMINATOR}")) +"%Y-%m-%d") + fi + + # move the complete results in + mkdir -p "site/public/api/runs/${DATE}" + cp "benchmark-${platform}/benchmarks.json" "site/public/api/runs/${DATE}/${platform}.json" + + # unzip the individual results + PLATFORM_TEMP=$(mktemp -d) + unzip "benchmark-${platform}/benchmarks.zip" -d "${PLATFORM_TEMP}" + + mkdir -p "site/public/api/runs/${DATE}/${platform}" + find "${PLATFORM_TEMP}" -name \*\.svg -exec cp {} "site/public/api/runs/${DATE}/${platform}" \; + done + + (cd site && node scripts/aggregate.js) + shell: bash + + # in debug mode, don't deploy the site; only create a zip file and + # upload it for debugging + - name: Upload site + uses: actions/upload-artifact@v4 + with: + name: site + path: site + if: github.event_name == 'workflow_dispatch' + - name: Publish API + run: | + git config user.name 'Benchmark Site Generation' && + git config user.email 'libgit2@users.noreply.github.com' && + git add . && + git commit --allow-empty -m"benchmark update ${DATE}" && + git push origin main + shell: bash + working-directory: site + if: github.event_name == 'schedule' || github.event.inputs.deploy == 'true' diff --git a/.github/workflows/build-containers.yml b/.github/workflows/build-containers.yml new file mode 100644 index 00000000000..b52571c1811 --- /dev/null +++ b/.github/workflows/build-containers.yml @@ -0,0 +1,74 @@ +# Generate the containers that we use for builds. +name: Build Containers + +on: + workflow_call: + +env: + docker-registry: ghcr.io + docker-config-path: source/ci/docker + +jobs: + # Build the docker container images that we will use for our Linux + # builds. This will identify the last commit to the repository that + # updated the docker images, and try to download the image tagged with + # that sha. If it does not exist, we'll do a docker build and push + # the image up to GitHub Packages for the actual CI/CD runs. We tag + # with both the sha and "latest" so that the subsequent runs need not + # know the sha. Only do this on CI builds (when the event is a "push") + # because PR builds from forks lack permission to write packages. + containers: + strategy: + matrix: + container: + - name: xenial + - name: bionic + - name: focal + - name: noble + - name: docurium + - name: bionic-x86 + dockerfile: bionic + base: multiarch/ubuntu-core:x86-bionic + qemu: true + - name: bionic-arm32 + dockerfile: bionic + base: multiarch/ubuntu-core:armhf-bionic + qemu: true + - name: bionic-arm64 + dockerfile: bionic + base: multiarch/ubuntu-core:arm64-bionic + qemu: true + - name: centos7 + - name: centos8 + - name: fedora + runs-on: ubuntu-latest + name: "Create container: ${{ matrix.container.name }}" + steps: + - name: Check out repository + uses: actions/checkout@v4 + with: + path: source + fetch-depth: 0 + if: github.event_name != 'pull_request' + - name: Setup QEMU + run: docker run --rm --privileged multiarch/qemu-user-static:register --reset + if: matrix.container.qemu == true + - name: Download existing container + run: | + "${{ github.workspace }}/source/ci/getcontainer.sh" "${{ matrix.container.name }}" "${{ matrix.container.dockerfile }}" + env: + DOCKER_REGISTRY: ${{ env.docker-registry }} + GITHUB_TOKEN: ${{ secrets.github_token }} + working-directory: ${{ env.docker-config-path }} + if: github.event_name != 'pull_request' + - name: Build and publish image + run: | + if [ "${{ matrix.container.base }}" != "" ]; then + BASE_ARG="--build-arg BASE=${{ matrix.container.base }}" + fi + docker build -t ${{ env.docker-registry-container-sha }} --build-arg UID=$(id -u) --build-arg GID=$(id -g) ${BASE_ARG} -f ${{ env.dockerfile }} . + docker tag ${{ env.docker-registry-container-sha }} ${{ env.docker-registry-container-latest }} + docker push ${{ env.docker-registry-container-sha }} + docker push ${{ env.docker-registry-container-latest }} + working-directory: ${{ env.docker-config-path }} + if: github.event_name != 'pull_request' && env.docker-container-exists != 'true' diff --git a/.github/workflows/documentation.yml b/.github/workflows/documentation.yml new file mode 100644 index 00000000000..d82887d2741 --- /dev/null +++ b/.github/workflows/documentation.yml @@ -0,0 +1,76 @@ +# Update the www.libgit2.org reference documentation +name: Generate Documentation + +on: + push: + branches: [ main ] + release: + workflow_dispatch: + inputs: + force: + description: 'Force rebuild' + type: boolean + required: true + +concurrency: + group: documentation + +permissions: + contents: read + +jobs: + documentation: + name: "Generate documentation" + runs-on: "ubuntu-latest" + steps: + - name: Check out source repository + uses: actions/checkout@v4 + with: + path: source + fetch-depth: 0 + - name: Check out documentation repository + uses: actions/checkout@v4 + with: + repository: libgit2/www.libgit2.org + path: www + fetch-depth: 0 + ssh-key: ${{ secrets.DOCS_PUBLISH_KEY }} + - name: Prepare branches + run: | + for a in main $(git branch -r --list 'origin/maint/*' | sed -e "s/^ origin\///"); do + if [ "$(git rev-parse --abbrev-ref HEAD)" != "${a}" ]; then + git branch --track "$a" "origin/$a" + fi + done + working-directory: source + - name: Generate documentation + run: | + args="" + + if [ "${{ inputs.force }}" = "true" ]; then + args="--force" + fi + + npm install + ./generate --verbose $args ../.. ../../../www/docs + working-directory: source/script/api-docs + - name: Examine changes + run: | + if [ -n "$(git diff --name-only)" ]; then + echo "changes=true" >> $GITHUB_OUTPUT + else + echo "changes=false" >> $GITHUB_OUTPUT + fi + id: check + working-directory: www + - name: Publish documentation + run: | + DATE=$(date +"%Y-%m-%d") + + git config user.name 'Documentation Site Generator' + git config user.email 'libgit2@users.noreply.github.com' + git add . + git commit -m"Documentation update ${DATE}" + git push origin main + if: steps.check.outputs.changes == 'true' + working-directory: www diff --git a/.github/workflows/experimental.yml b/.github/workflows/experimental.yml new file mode 100644 index 00000000000..cce959f1553 --- /dev/null +++ b/.github/workflows/experimental.yml @@ -0,0 +1,120 @@ +# Validation builds for experimental features; these shouldn't be +# required for pull request approval. +name: Experimental Features + +on: + push: + branches: [ main, maint/* ] + pull_request: + branches: [ main, maint/* ] + workflow_dispatch: + +env: + docker-registry: ghcr.io + docker-config-path: ci/docker + +permissions: + contents: write + packages: write + +jobs: + # Run our CI/CD builds. We build a matrix with the various build targets + # and their details. Then we build either in a docker container (Linux) + # or on the actual hosts (macOS, Windows). + build: + strategy: + matrix: + platform: + # All builds: experimental SHA256 support + - name: "Linux (SHA256, Xenial, Clang, OpenSSL)" + id: linux-sha256 + os: ubuntu-latest + container: + name: xenial + env: + CC: clang + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON -DEXPERIMENTAL_SHA256=ON + - name: "macOS (SHA256)" + id: macos-sha256 + os: macos-14 + setup-script: osx + env: + CC: clang + CMAKE_OPTIONS: -DREGEX_BACKEND=regcomp_l -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=leaks -DUSE_GSSAPI=ON -DEXPERIMENTAL_SHA256=ON + CMAKE_GENERATOR: Ninja + PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + - name: "Windows (SHA256, amd64, Visual Studio)" + id: windows-sha256 + os: windows-2022 + env: + ARCH: amd64 + CMAKE_GENERATOR: Visual Studio 17 2022 + CMAKE_OPTIONS: -A x64 -DDEBUG_LEAK_CHECKER=win32 -DDEPRECATE_HARD=ON -DEXPERIMENTAL_SHA256=ON + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + # TODO: this is a temporary removal + SKIP_GITDAEMON_TESTS: true + fail-fast: false + env: ${{ matrix.platform.env }} + runs-on: ${{ matrix.platform.os }} + name: "Build: ${{ matrix.platform.name }}" + steps: + - name: Check out repository + uses: actions/checkout@v4 + with: + path: source + fetch-depth: 0 + - name: Set up build environment + run: source/ci/setup-${{ matrix.platform.setup-script }}-build.sh + shell: bash + if: matrix.platform.setup-script != '' + - name: Setup QEMU + run: docker run --rm --privileged multiarch/qemu-user-static:register --reset + if: matrix.platform.container.qemu == true + - name: Set up container + uses: ./source/.github/actions/download-or-build-container + with: + registry: ${{ env.docker-registry }} + config-path: ${{ env.docker-config-path }} + container: ${{ matrix.platform.container.name }} + github_token: ${{ secrets.github_token }} + dockerfile: ${{ matrix.platform.container.dockerfile }} + if: matrix.platform.container.name != '' + - name: Prepare build + run: mkdir build + - name: Build + uses: ./source/.github/actions/run-build + with: + command: cd ${BUILD_WORKSPACE:-.}/build && ../source/ci/build.sh + container: ${{ matrix.platform.container.name }} + container-version: ${{ env.docker-registry-container-sha }} + shell: ${{ matrix.platform.shell }} + - name: Test + uses: ./source/.github/actions/run-build + with: + command: cd ${BUILD_WORKSPACE:-.}/build && ../source/ci/test.sh + container: ${{ matrix.platform.container.name }} + container-version: ${{ env.docker-registry-container-sha }} + shell: ${{ matrix.platform.shell }} + - name: Upload test results + uses: actions/upload-artifact@v4 + if: success() || failure() + with: + name: test-results-${{ matrix.platform.id }} + path: build/results_*.xml + + test_results: + name: Test results + needs: [ build ] + if: always() + runs-on: ubuntu-latest + steps: + - name: Download test results + uses: actions/download-artifact@v4 + - name: Generate test summary + uses: test-summary/action@v2 + with: + paths: 'test-results-*/*.xml' diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000000..4ca339806a3 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,258 @@ +# Continuous integration and pull request validation builds for the +# main and maintenance branches. +name: CI Build + +on: + push: + branches: [ main, maint/* ] + pull_request: + branches: [ main, maint/* ] + workflow_dispatch: + +env: + docker-registry: ghcr.io + docker-config-path: ci/docker + +permissions: + contents: write + packages: write + +jobs: + # Run our CI/CD builds. We build a matrix with the various build targets + # and their details. Then we build either in a docker container (Linux) + # or on the actual hosts (macOS, Windows). + build: + strategy: + matrix: + platform: + # All builds: core platforms + - name: "Linux (Noble, GCC, OpenSSL, libssh2)" + id: noble-gcc-openssl + os: ubuntu-latest + container: + name: noble + env: + CC: gcc + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=builtin -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=libssh2 -DDEBUG_STRICT_ALLOC=ON -DDEBUG_STRICT_OPEN=ON + - name: "Linux (Noble, Clang, mbedTLS, OpenSSH)" + id: noble-clang-mbedtls + os: ubuntu-latest + container: + name: noble + env: + CC: clang + CMAKE_OPTIONS: -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=exec -DUSE_HTTP_PARSER=http-parser + CMAKE_GENERATOR: Ninja + - name: "Linux (Xenial, GCC, OpenSSL, OpenSSH)" + id: xenial-gcc-openssl + os: ubuntu-latest + container: + name: xenial + env: + CC: gcc + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=builtin -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=exec -DDEBUG_STRICT_ALLOC=ON -DDEBUG_STRICT_OPEN=ON + - name: "Linux (Xenial, Clang, mbedTLS, libssh2)" + id: xenial-gcc-mbedtls + os: ubuntu-latest + container: + name: xenial + env: + CC: clang + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=libssh2 + - name: "macOS" + id: macos + os: macos-14 + setup-script: osx + env: + CC: clang + CMAKE_OPTIONS: -DREGEX_BACKEND=regcomp_l -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=leaks -DUSE_GSSAPI=ON + CMAKE_GENERATOR: Ninja + PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + - name: "Windows (amd64, Visual Studio, Schannel)" + id: windows-amd64-vs + os: windows-2022 + setup-script: win32 + env: + ARCH: amd64 + CMAKE_GENERATOR: Visual Studio 17 2022 + CMAKE_OPTIONS: -A x64 -DDEBUG_LEAK_CHECKER=win32 -DDEPRECATE_HARD=ON -DUSE_HTTPS=Schannel -DUSE_SSH=ON -DCMAKE_PREFIX_PATH=D:\Temp\libssh2 + BUILD_PATH: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x86)\CMake\bin;D:\Temp\libssh2\bin + BUILD_TEMP: D:\Temp + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + - name: "Windows (x86, Visual Studio, WinHTTP)" + id: windows-x86-vs + os: windows-2022 + setup-script: win32 + env: + ARCH: x86 + CMAKE_GENERATOR: Visual Studio 17 2022 + CMAKE_OPTIONS: -A Win32 -DDEBUG_LEAK_CHECKER=win32 -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS -DUSE_BUNDLED_ZLIB=ON -DUSE_SSH=ON -DCMAKE_PREFIX_PATH=D:\Temp\libssh2 + BUILD_PATH: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x86)\CMake\bin;D:\Temp\libssh2\bin + BUILD_TEMP: D:\Temp + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + - name: "Windows (amd64, mingw, WinHTTP)" + id: windows-amd64-mingw + os: windows-2022 + setup-script: mingw + env: + ARCH: amd64 + CMAKE_GENERATOR: MinGW Makefiles + CMAKE_OPTIONS: -DDEPRECATE_HARD=ON + BUILD_TEMP: D:\Temp + BUILD_PATH: D:\Temp\mingw64\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x86)\CMake\bin + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + - name: "Windows (x86, mingw, Schannel)" + id: windows-x86-mingw + os: windows-2022 + setup-script: mingw + env: + ARCH: x86 + CMAKE_GENERATOR: MinGW Makefiles + CMAKE_OPTIONS: -DDEPRECATE_HARD=ON -DUSE_HTTPS=Schannel + BUILD_TEMP: D:\Temp + BUILD_PATH: D:\Temp\mingw32\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x86)\CMake\bin + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + + # All builds: sanitizers + - name: "Sanitizer (Memory)" + id: sanitizer-memory + os: ubuntu-latest + setup-script: sanitizer + container: + name: noble + env: + CC: clang + CFLAGS: -fsanitize=memory -fsanitize-memory-track-origins=2 -fsanitize-blacklist=/home/libgit2/source/script/sanitizers.supp -fno-optimize-sibling-calls -fno-omit-frame-pointer + CMAKE_OPTIONS: -DCMAKE_C_EXTENSIONS=ON -DCMAKE_PREFIX_PATH=/usr/local/msan -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON -DUSE_SSH=ON + CMAKE_GENERATOR: Ninja + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + ASAN_SYMBOLIZER_PATH: /usr/bin/llvm-symbolizer-10 + UBSAN_OPTIONS: print_stacktrace=1 + - name: "Sanitizer (Address)" + id: sanitizer-address + os: ubuntu-latest + setup-script: sanitizer + container: + name: noble + env: + CC: clang + CFLAGS: -fsanitize=address -ggdb -fsanitize-blacklist=/home/libgit2/source/script/sanitizers.supp -fno-optimize-sibling-calls -fno-omit-frame-pointer + CMAKE_OPTIONS: -DCMAKE_PREFIX_PATH=/usr/local -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON -DUSE_SSH=ON + CMAKE_GENERATOR: Ninja + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + ASAN_SYMBOLIZER_PATH: /usr/bin/llvm-symbolizer-10 + UBSAN_OPTIONS: print_stacktrace=1 + - name: "Sanitizer (UndefinedBehavior)" + id: sanitizer-ub + os: ubuntu-latest + setup-script: sanitizer + container: + name: noble + env: + CC: clang + CFLAGS: -fsanitize=undefined,nullability -fno-sanitize-recover=undefined,nullability -fsanitize-blacklist=/home/libgit2/source/script/sanitizers.supp -fno-optimize-sibling-calls -fno-omit-frame-pointer + CMAKE_OPTIONS: -DCMAKE_PREFIX_PATH=/usr/local -DUSE_HTTPS=OpenSSL -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON -DUSE_SSH=ON + CMAKE_GENERATOR: Ninja + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + ASAN_SYMBOLIZER_PATH: /usr/bin/llvm-symbolizer-10 + UBSAN_OPTIONS: print_stacktrace=1 + - name: "Sanitizer (Thread)" + id: sanitizer-thread + os: ubuntu-latest + setup-script: sanitizer + container: + name: noble + env: + CC: clang + CFLAGS: -fsanitize=thread -fno-optimize-sibling-calls -fno-omit-frame-pointer + CMAKE_OPTIONS: -DCMAKE_PREFIX_PATH=/usr/local -DUSE_HTTPS=OpenSSL -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON -DUSE_SSH=ON + CMAKE_GENERATOR: Ninja + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + ASAN_SYMBOLIZER_PATH: /usr/bin/llvm-symbolizer-10 + UBSAN_OPTIONS: print_stacktrace=1 + TSAN_OPTIONS: suppressions=/home/libgit2/source/script/thread-sanitizer.supp second_deadlock_stack=1 + fail-fast: false + env: ${{ matrix.platform.env }} + runs-on: ${{ matrix.platform.os }} + name: "Build: ${{ matrix.platform.name }}" + steps: + - name: Check out repository + uses: actions/checkout@v4 + with: + path: source + fetch-depth: 0 + - name: Set up build environment + run: source/ci/setup-${{ matrix.platform.setup-script }}-build.sh + shell: bash + if: matrix.platform.setup-script != '' + - name: Setup QEMU + run: docker run --rm --privileged multiarch/qemu-user-static:register --reset + if: matrix.platform.container.qemu == true + - name: Set up container + uses: ./source/.github/actions/download-or-build-container + with: + registry: ${{ env.docker-registry }} + config-path: ${{ env.docker-config-path }} + container: ${{ matrix.platform.container.name }} + github_token: ${{ secrets.github_token }} + dockerfile: ${{ matrix.platform.container.dockerfile }} + if: matrix.platform.container.name != '' + - name: Prepare build + run: mkdir build + - name: Build + uses: ./source/.github/actions/run-build + with: + command: cd ${BUILD_WORKSPACE:-.}/build && ../source/ci/build.sh + container: ${{ matrix.platform.container.name }} + container-version: ${{ env.docker-registry-container-sha }} + shell: ${{ matrix.platform.shell }} + - name: Test + uses: ./source/.github/actions/run-build + with: + command: cd ${BUILD_WORKSPACE:-.}/build && ../source/ci/test.sh + container: ${{ matrix.platform.container.name }} + container-version: ${{ env.docker-registry-container-sha }} + shell: ${{ matrix.platform.shell }} + - name: Upload test results + uses: actions/upload-artifact@v4 + if: success() || failure() + with: + name: test-results-${{ matrix.platform.id }} + path: build/results_*.xml + + documentation: + name: Validate documentation + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v4 + - name: Validate documentation + run: | + (cd script/api-docs && npm install) + script/api-docs/api-generator.js --validate-only --strict --deprecate-hard . + + test_results: + name: Test results + needs: [ build ] + if: always() + runs-on: ubuntu-latest + steps: + - name: Download test results + uses: actions/download-artifact@v4 + - name: Generate test summary + uses: test-summary/action@v2 + with: + paths: 'test-results-*/*.xml' diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml new file mode 100644 index 00000000000..c3dc6a53970 --- /dev/null +++ b/.github/workflows/nightly.yml @@ -0,0 +1,507 @@ +# Nightly build for the main branch across multiple targets. +name: Nightly Build + +on: + workflow_dispatch: + schedule: + - cron: '15 1 * * *' + +env: + docker-registry: ghcr.io + docker-config-path: ci/docker + +permissions: + contents: read + packages: write + +jobs: + # Run our nightly builds. We build a matrix with the various build + # targets and their details. Then we build either in a docker container + # (Linux) or on the actual hosts (macOS, Windows). + build: + # Only run scheduled workflows on the main repository; prevents people + # from using build minutes on their forks. + if: github.repository == 'libgit2/libgit2' + + strategy: + matrix: + platform: + # All builds: core platforms + - name: "Linux (Noble, GCC, OpenSSL, libssh2)" + id: noble-gcc-openssl + os: ubuntu-latest + container: + name: noble + env: + CC: gcc + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=builtin -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=libssh2 -DDEBUG_STRICT_ALLOC=ON -DDEBUG_STRICT_OPEN=ON + - name: "Linux (Noble, Clang, mbedTLS, OpenSSH)" + id: noble-clang-mbedtls + os: ubuntu-latest + container: + name: noble + env: + CC: clang + CMAKE_OPTIONS: -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=exec -DUSE_HTTP_PARSER=http-parser + CMAKE_GENERATOR: Ninja + - name: "Linux (Xenial, GCC, OpenSSL, OpenSSH)" + id: xenial-gcc-openssl + os: ubuntu-latest + container: + name: xenial + env: + CC: gcc + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=builtin -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=exec -DDEBUG_STRICT_ALLOC=ON -DDEBUG_STRICT_OPEN=ON + - name: "Linux (Xenial, Clang, mbedTLS, libssh2)" + id: xenial-gcc-mbedtls + os: ubuntu-latest + container: + name: xenial + env: + CC: clang + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=libssh2 + - name: "macOS" + id: macos + os: macos-14 + setup-script: osx + env: + CC: clang + CMAKE_OPTIONS: -DREGEX_BACKEND=regcomp_l -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=leaks -DUSE_GSSAPI=ON + CMAKE_GENERATOR: Ninja + PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + - name: "iOS" + id: ios + os: macos-14 + setup-script: ios + env: + CC: clang + CMAKE_OPTIONS: -DBUILD_TESTS=OFF -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=leaks -DUSE_GSSAPI=ON -DCMAKE_TOOLCHAIN_FILE=../ios.toolchain.cmake -DCMAKE_SYSTEM_NAME=iOS -DPLATFORM=OS64 + CMAKE_GENERATOR: Ninja + PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig + SKIP_TESTS: true # Cannot exec iOS app on macOS + - name: "Windows (amd64, Visual Studio, Schannel)" + id: windows-amd64-vs + os: windows-2022 + setup-script: win32 + env: + ARCH: amd64 + CMAKE_GENERATOR: Visual Studio 17 2022 + CMAKE_OPTIONS: -A x64 -DDEBUG_LEAK_CHECKER=win32 -DDEPRECATE_HARD=ON -DUSE_HTTPS=Schannel -DUSE_SSH=ON -DCMAKE_PREFIX_PATH=D:\Temp\libssh2 + BUILD_PATH: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x86)\CMake\bin;D:\Temp\libssh2\bin + BUILD_TEMP: D:\Temp + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + - name: "Windows (x86, Visual Studio, WinHTTP)" + id: windows-x86-vs + os: windows-2022 + setup-script: win32 + env: + ARCH: x86 + CMAKE_GENERATOR: Visual Studio 17 2022 + CMAKE_OPTIONS: -A Win32 -DDEBUG_LEAK_CHECKER=win32 -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS -DUSE_BUNDLED_ZLIB=ON -DUSE_SSH=ON -DCMAKE_PREFIX_PATH=D:\Temp\libssh2 + BUILD_PATH: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x86)\CMake\bin;D:\Temp\libssh2\bin + BUILD_TEMP: D:\Temp + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + - name: "Windows (amd64, mingw, WinHTTP)" + id: windows-amd64-mingw + os: windows-2022 + setup-script: mingw + env: + ARCH: amd64 + CMAKE_GENERATOR: MinGW Makefiles + CMAKE_OPTIONS: -DDEPRECATE_HARD=ON + BUILD_TEMP: D:\Temp + BUILD_PATH: D:\Temp\mingw64\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x86)\CMake\bin + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + - name: "Windows (x86, mingw, Schannel)" + id: windows-x86-mingw + os: windows-2022 + setup-script: mingw + env: + ARCH: x86 + CMAKE_GENERATOR: MinGW Makefiles + CMAKE_OPTIONS: -DDEPRECATE_HARD=ON -DUSE_HTTPS=Schannel + BUILD_TEMP: D:\Temp + BUILD_PATH: D:\Temp\mingw32\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x86)\CMake\bin + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + + # All builds: sanitizers + - name: "Sanitizer (Memory)" + id: memorysanitizer + os: ubuntu-latest + setup-script: sanitizer + container: + name: noble + env: + CC: clang + CFLAGS: -fsanitize=memory -fsanitize-memory-track-origins=2 -fsanitize-blacklist=/home/libgit2/source/script/sanitizers.supp -fno-optimize-sibling-calls -fno-omit-frame-pointer + CMAKE_OPTIONS: -DCMAKE_C_EXTENSIONS=ON -DCMAKE_PREFIX_PATH=/usr/local/msan -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON -DUSE_SSH=ON + CMAKE_GENERATOR: Ninja + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + ASAN_SYMBOLIZER_PATH: /usr/bin/llvm-symbolizer-10 + UBSAN_OPTIONS: print_stacktrace=1 + - name: "Sanitizer (UndefinedBehavior)" + id: ubsanitizer + os: ubuntu-latest + setup-script: sanitizer + container: + name: noble + env: + CC: clang + CFLAGS: -fsanitize=undefined,nullability -fno-sanitize-recover=undefined,nullability -fsanitize-blacklist=/home/libgit2/source/script/sanitizers.supp -fno-optimize-sibling-calls -fno-omit-frame-pointer + CMAKE_OPTIONS: -DCMAKE_PREFIX_PATH=/usr/local -DUSE_HTTPS=OpenSSL -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON -DUSE_SSH=ON + CMAKE_GENERATOR: Ninja + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + ASAN_SYMBOLIZER_PATH: /usr/bin/llvm-symbolizer-10 + UBSAN_OPTIONS: print_stacktrace=1 + - name: "Sanitizer (Thread)" + id: threadsanitizer + os: ubuntu-latest + setup-script: sanitizer + container: + name: noble + env: + CC: clang + CFLAGS: -fsanitize=thread -fno-optimize-sibling-calls -fno-omit-frame-pointer + CMAKE_OPTIONS: -DCMAKE_PREFIX_PATH=/usr/local -DUSE_HTTPS=OpenSSL -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON -DUSE_SSH=ON + CMAKE_GENERATOR: Ninja + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + ASAN_SYMBOLIZER_PATH: /usr/bin/llvm-symbolizer-10 + UBSAN_OPTIONS: print_stacktrace=1 + TSAN_OPTIONS: suppressions=/home/libgit2/source/script/thread-sanitizer.supp second_deadlock_stack=1 + + # Nightly builds: extended platforms + - name: "Linux (CentOS 7, OpenSSL)" + id: centos7-openssl + os: ubuntu-latest + container: + name: centos7 + env: + CMAKE_OPTIONS: -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON + PKG_CONFIG_PATH: /usr/local/lib/pkgconfig + SKIP_NEGOTIATE_TESTS: true + SKIP_PUSHOPTIONS_TESTS: true + - name: "Linux (CentOS 7, dynamically-loaded OpenSSL)" + id: centos7-dynamicopenssl + os: ubuntu-latest + container: + name: centos7 + env: + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL-Dynamic -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON + PKG_CONFIG_PATH: /usr/local/lib/pkgconfig + SKIP_NEGOTIATE_TESTS: true + SKIP_PUSHOPTIONS_TESTS: true + - name: "Linux (CentOS 8, OpenSSL)" + id: centos8-openssl + os: ubuntu-latest + container: + name: centos8 + env: + CMAKE_OPTIONS: -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON + PKG_CONFIG_PATH: /usr/local/lib/pkgconfig + SKIP_NEGOTIATE_TESTS: true + SKIP_SSH_TESTS: true + - name: "Linux (CentOS 8, dynamically-loaded OpenSSL)" + id: centos8-dynamicopenssl + os: ubuntu-latest + container: + name: centos8 + env: + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL-Dynamic -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON + PKG_CONFIG_PATH: /usr/local/lib/pkgconfig + SKIP_NEGOTIATE_TESTS: true + SKIP_SSH_TESTS: true + ARCH: x86 + - name: "Linux (Fedora, llhttp)" + id: fedora + os: ubuntu-latest + container: + name: fedora + env: + CC: gcc + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=pcre2 -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=libssh2 -DUSE_HTTP_PARSER=llhttp + - name: "Linux (Bionic, GCC, dynamically-loaded OpenSSL)" + id: bionic-gcc-dynamicopenssl + container: + name: bionic + dockerfile: bionic + env: + CC: gcc + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL-Dynamic -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON + RUN_INVASIVE_TESTS: true + SKIP_PUSHOPTIONS_TESTS: true + os: ubuntu-latest + - name: "Linux (x86, Bionic, Clang, OpenSSL)" + id: bionic-x86-clang-openssl + container: + name: bionic-x86 + dockerfile: bionic + qemu: true + env: + CC: clang + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON + RUN_INVASIVE_TESTS: true + SKIP_PUSHOPTIONS_TESTS: true + os: ubuntu-latest + - name: "Linux (x86, Bionic, GCC, OpenSSL)" + id: bionic-x86-gcc-openssl + container: + name: bionic-x86 + dockerfile: bionic + env: + CC: gcc + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON + RUN_INVASIVE_TESTS: true + SKIP_PUSHOPTIONS_TESTS: true + os: ubuntu-latest + - name: "Linux (arm32, Bionic, GCC, OpenSSL)" + id: bionic-arm32-gcc-openssl + container: + name: bionic-arm32 + dockerfile: bionic + qemu: true + env: + CC: gcc + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_GSSAPI=ON -DUSE_SSH=ON + RUN_INVASIVE_TESTS: true + SKIP_PROXY_TESTS: true + SKIP_PUSHOPTIONS_TESTS: true + GITTEST_FLAKY_STAT: true + os: ubuntu-latest + - name: "Linux (arm64, Bionic, GCC, OpenSSL)" + id: bionic-arm64-gcc-openssl + container: + name: bionic-arm64 + dockerfile: bionic + qemu: true + env: + CC: gcc + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_GSSAPI=ON -DUSE_SSH=ON + RUN_INVASIVE_TESTS: true + SKIP_PROXY_TESTS: true + SKIP_PUSHOPTIONS_TESTS: true + os: ubuntu-latest + + # Nightly builds: ensure we fallback when missing core functionality + - name: "Linux (no threads)" + id: xenial-nothreads + os: ubuntu-latest + container: + name: xenial + env: + CC: gcc + CMAKE_OPTIONS: -DTHREADSAFE=OFF -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON + CMAKE_GENERATOR: Ninja + SKIP_PUSHOPTIONS_TESTS: true + - name: "Linux (no mmap)" + id: noble-nommap + os: ubuntu-latest + container: + name: noble + env: + CC: gcc + CFLAGS: -DNO_MMAP + CMAKE_OPTIONS: -DCMAKE_PREFIX_PATH=/usr/local + CMAKE_GENERATOR: Ninja + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + - name: "Windows (no mmap)" + id: windows-nommap + os: windows-2022 + env: + ARCH: amd64 + CMAKE_GENERATOR: Visual Studio 17 2022 + CFLAGS: -DNO_MMAP + CMAKE_OPTIONS: -A x64 -DDEPRECATE_HARD=ON + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + + # Nightly builds: extended SSL support + - name: "Linux (dynamically-loaded OpenSSL)" + id: xenial-dynamicopenssl + os: ubuntu-latest + container: + name: xenial + env: + CC: clang + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL-Dynamic -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON + CMAKE_GENERATOR: Ninja + + # All builds: experimental SHA256 support + - name: "Linux (SHA256, Xenial, Clang, OpenSSL)" + id: linux-sha256 + container: + name: xenial + env: + CC: clang + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON + os: ubuntu-latest + - name: "macOS (SHA256)" + id: macos-sha256 + os: macos-14 + setup-script: osx + env: + CC: clang + CMAKE_OPTIONS: -DREGEX_BACKEND=regcomp_l -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=leaks -DUSE_GSSAPI=ON -DEXPERIMENTAL_SHA256=ON + PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + - name: "Windows (SHA256, amd64, Visual Studio)" + id: windows-sha256 + os: windows-2022 + env: + ARCH: amd64 + CMAKE_GENERATOR: Visual Studio 17 2022 + CMAKE_OPTIONS: -A x64 -DDEBUG_LEAK_CHECKER=win32 -DDEPRECATE_HARD=ON -DEXPERIMENTAL_SHA256=ON + SKIP_SSH_TESTS: true + SKIP_NEGOTIATE_TESTS: true + # TODO: this is a temporary removal + SKIP_GITDAEMON_TESTS: true + - name: "Linux (SHA256, Xenial, Clang, OpenSSL-FIPS)" + id: linux-sha256-fips + container: + name: xenial + env: + CC: clang + CMAKE_GENERATOR: Ninja + CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DDEBUG_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON -DUSE_SHA1=OpenSSL-FIPS -DUSE_SHA256=OpenSSL-FIPS + os: ubuntu-latest + fail-fast: false + env: ${{ matrix.platform.env }} + runs-on: ${{ matrix.platform.os }} + name: "Build ${{ matrix.platform.name }}" + steps: + - name: Check out repository + uses: actions/checkout@v4 + with: + path: source + fetch-depth: 0 + - name: Set up build environment + run: source/ci/setup-${{ matrix.platform.setup-script }}-build.sh + shell: bash + if: matrix.platform.setup-script != '' + - name: Setup QEMU + run: docker run --rm --privileged multiarch/qemu-user-static:register --reset + if: matrix.platform.container.qemu == true + - name: Set up container + uses: ./source/.github/actions/download-or-build-container + with: + registry: ${{ env.docker-registry }} + config-path: ${{ env.docker-config-path }} + container: ${{ matrix.platform.container.name }} + github_token: ${{ secrets.github_token }} + dockerfile: ${{ matrix.platform.container.dockerfile }} + if: matrix.platform.container.name != '' + - name: Prepare build + run: mkdir build + - name: Build + uses: ./source/.github/actions/run-build + with: + command: cd ${BUILD_WORKSPACE:-.}/build && ../source/ci/build.sh + container: ${{ matrix.platform.container.name }} + container-version: ${{ env.docker-registry-container-sha }} + shell: ${{ matrix.platform.shell }} + - name: Test + uses: ./source/.github/actions/run-build + with: + command: cd ${BUILD_WORKSPACE:-.}/build && ../source/ci/test.sh + container: ${{ matrix.platform.container.name }} + container-version: ${{ env.docker-registry-container-sha }} + shell: ${{ matrix.platform.shell }} + - name: Upload test results + uses: actions/upload-artifact@v4 + if: success() || failure() + with: + name: test-results-${{ matrix.platform.id }} + path: build/results_*.xml + + test_results: + name: Test results + needs: [ build ] + if: ${{ always() && github.repository == 'libgit2/libgit2' }} + runs-on: ubuntu-latest + steps: + - name: Download test results + uses: actions/download-artifact@v4 + - name: Generate test summary + uses: test-summary/action@v2 + with: + paths: 'test-results-*/*.xml' + + coverity: + # Only run scheduled workflows on the main repository; prevents people + # from using build minutes on their forks. + if: github.repository == 'libgit2/libgit2' + + name: Coverity + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v4 + with: + path: source + fetch-depth: 0 + - name: Set up container + uses: ./source/.github/actions/download-or-build-container + with: + registry: ${{ env.docker-registry }} + config-path: ${{ env.docker-config-path }} + container: xenial + github_token: ${{ secrets.github_token }} + if: matrix.platform.container.name != '' + - name: Run Coverity + run: source/ci/coverity.sh + env: + COVERITY_TOKEN: ${{ secrets.coverity_token }} + + codeql: + # Only run scheduled workflows on the main repository; prevents people + # from using build minutes on their forks. + if: github.repository == 'libgit2/libgit2' + + permissions: + actions: read + contents: read + security-events: write + + name: CodeQL + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v3 + with: + languages: 'cpp' + + - name: Build + run: | + mkdir build + cd build + cmake .. -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON + cmake --build . + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v2 diff --git a/.gitignore b/.gitignore index 949baec980e..1b482f038af 100644 --- a/.gitignore +++ b/.gitignore @@ -1,32 +1,8 @@ -/tests-clar/clar.suite -/tests-clar/clar.suite.rule -/tests-clar/.clarcache -/apidocs -/trash-*.exe -/libgit2.pc -/config.mak -*.o -*.a -*.exe -*.gcda -*.gcno -*.gcov -.lock-wafbuild -.waf* -build/ -build-amiga/ -tests/tmp/ -msvc/Debug/ -msvc/Release/ -*.sln -*.suo -*.vc*proj* -*.sdf -*.opensdf -*.aps -CMake* -*.cmake +/build/ .DS_Store *~ -tags -mkmf.log +.*.swp +/tags +CMakeSettings.json +.vs +.idea diff --git a/.mailmap b/.mailmap index 582dae0b99b..0b16a7e1f1a 100644 --- a/.mailmap +++ b/.mailmap @@ -1,3 +1,22 @@ -Vicent Martí Vicent Marti +Vicent Martí Vicent Marti Vicent Martí Vicent Martí Michael Schubert schu +Ben Straub Ben Straub +Ben Straub Ben Straub +Carlos Martín Nieto +Carlos Martín Nieto +nulltoken +Scott J. Goldman +Martin Woodward +Peter Drahoš +Adam Roben +Adam Roben +Xavier L. +Xavier L. +Sascha Cunz +Authmillenon +Authmillenon +Edward Thomson +Edward Thomson +J. David Ibáñez +Russell Belfer diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 490f1462adc..00000000000 --- a/.travis.yml +++ /dev/null @@ -1,54 +0,0 @@ -# Travis-CI Build for libgit2 -# see travis-ci.org for details - -# As CMake is not officially supported we use erlang VMs -language: c - -compiler: - - gcc - - clang - -# Settings to try -env: - - OPTIONS="-DTHREADSAFE=ON -DCMAKE_BUILD_TYPE=Release" - - OPTIONS="-DBUILD_CLAR=ON -DBUILD_EXAMPLES=ON" - -matrix: - include: - - compiler: i586-mingw32msvc-gcc - env: OPTIONS="-DBUILD_CLAR=OFF -DWIN32=ON -DMINGW=ON" - -# Make sure CMake is installed -install: - - sudo apt-get update >/dev/null - - sudo apt-get -q install cmake valgrind - -# Run the Build script -script: - - mkdir _build - - cd _build - - cmake .. -DCMAKE_INSTALL_PREFIX=../_install $OPTIONS - - cmake --build . --target install - - ctest -V . - -# Run Tests -after_success: - - valgrind --leak-check=full --show-reachable=yes --suppressions=../libgit2_clar.supp ./libgit2_clar -ionline - -# Only watch the development branch -branches: - only: - - development - -# Notify development list when needed -notifications: - irc: - channels: - - irc.freenode.net#libgit2 - on_success: change - on_failure: always - campfire: - on_success: always - on_failure: always - rooms: - - secure: "sH0dpPWMirbEe7AvLddZ2yOp8rzHalGmv0bYL/LIhVw3JDI589HCYckeLMSB\n3e/FeXw4bn0EqXWEXijVa4ijbilVY6d8oprdqMdWHEodng4KvY5vID3iZSGT\nxylhahO1XHmRynKQLOAvxlc93IlpVW38vQfby8giIY1nkpspb2w=" diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000000..62d4ec949b1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/libgit2_tests", + "args": [], + "stopAtEntry": false, + "cwd": "${fileDirname}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000000..5dabb895af6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "sarif-viewer.connectToGithubCodeScanning": "off" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 00000000000..24b4d745b31 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,27 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "Build", + "type": "shell", + "command": "cd build && cmake --build . --parallel", + "group": "build", + "presentation": { + "reveal": "always", + "panel": "new" + } + }, + { + "label": "Run Tests", + "type": "shell", + "command": "build/libgit2_tests -v", + "group": "test", + "presentation": { + "reveal": "always", + "panel": "new" + } + } + ] + } diff --git a/AUTHORS b/AUTHORS index 587da249d4b..f6164ae4f63 100644 --- a/AUTHORS +++ b/AUTHORS @@ -6,6 +6,7 @@ Alexei Sholik Andreas Ericsson Anton "antong" Gyllenberg Ankur Sethi +Arthur Schreiber Ben Noordhuis Ben Straub Benjamin C Meyer @@ -21,10 +22,13 @@ Dmitry Kakurin Dmitry Kovega Emeric Fermas Emmanuel Rodriguez +Eric Myhre +Erik Aigner Florian Forster Holger Weiss Ingmar Vanhassel J. David Ibáñez +Jacques Germishuys Jakob Pfender Jason Penny Jason R. McNeil @@ -47,6 +51,7 @@ Microsoft Corporation Olivier Ramonat Peter Drahoš Pierre Habouzit +Pierre-Olivier Latour Przemyslaw Pawelczyk Ramsay Jones Robert G. Jakabosky @@ -54,6 +59,7 @@ Romain Geissler Romain Muller Russell Belfer Sakari Jokinen +Sam Altier Samuel Charles "Sam" Day Sarath Lakshman Sascha Cunz @@ -65,8 +71,11 @@ Shawn O. Pearce Shuhei Tanuma Steve Frécinaux Sven Strickroth +Talya "kivikakk" Connor Tim Branyen Tim Clem Tim Harder +Torsten Bögershausen Trent Mick +Venus Xeon-Blonde Vicent Marti diff --git a/CMakeLists.txt b/CMakeLists.txt index e9972fd4147..335901d1fb0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,305 +1,144 @@ -# CMake build script for the libgit2 project +# libgit2: the cross-platform, linkable library implementation of git. +# See `README.md` for build instructions. # -# Building (out of source build): -# > mkdir build && cd build -# > cmake .. [-DSETTINGS=VALUE] -# > cmake --build . -# -# Testing: -# > ctest -V -# -# Install: -# > cmake --build . --target install - -PROJECT(libgit2 C) -CMAKE_MINIMUM_REQUIRED(VERSION 2.6) - -# Build options -# -OPTION( BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON ) -OPTION( THREADSAFE "Build libgit2 as threadsafe" OFF ) -OPTION( BUILD_CLAR "Build Tests using the Clar suite" ON ) -OPTION( BUILD_EXAMPLES "Build library usage example apps" OFF ) -OPTION( TAGS "Generate tags" OFF ) -OPTION( PROFILE "Generate profiling information" OFF ) -IF(MSVC) - # This option is only availalbe when building with MSVC. By default, - # libgit2 is build using the stdcall calling convention, as that's what - # the CLR expects by default and how the Windows API is built. - # - # If you are writing a C or C++ program and want to link to libgit2, you - # have to either: - # - Add /Gz to the compiler options of _your_ program / library. - # - Turn this off by invoking CMake with the "-DSTDCALL=Off" argument. - # - OPTION( STDCALL "Build libgit2 with the __stdcall convention" ON ) -ENDIF() - -# Installation paths -# -SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.") -SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.") -SET(INCLUDE_INSTALL_DIR include CACHE PATH "Where to install headers to.") - -FUNCTION(TARGET_OS_LIBRARIES target) - IF(WIN32) - TARGET_LINK_LIBRARIES(${target} ws2_32) - ELSEIF(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") - TARGET_LINK_LIBRARIES(${target} socket nsl) - ENDIF () - IF(THREADSAFE) - TARGET_LINK_LIBRARIES(${target} ${CMAKE_THREAD_LIBS_INIT}) - ENDIF() -ENDFUNCTION() - -# For the MSVC IDE, this function splits up the source files like windows -# explorer does. This is esp. useful with the libgit2_clar project, were -# usually 2 or more files share the same name. Sadly, this file grouping -# is a per-directory option in cmake and not per-target, resulting in -# empty virtual folders "tests-clar" for the git2.dll -FUNCTION(MSVC_SPLIT_SOURCES target) - IF(MSVC_IDE) - GET_TARGET_PROPERTY(sources ${target} SOURCES) - FOREACH(source ${sources}) - IF(source MATCHES ".*/") - STRING(REPLACE ${CMAKE_CURRENT_SOURCE_DIR}/ "" rel ${source}) - IF(rel) - STRING(REGEX REPLACE "/([^/]*)$" "" rel ${rel}) - IF(rel) - STRING(REPLACE "/" "\\\\" rel ${rel}) - SOURCE_GROUP(${rel} FILES ${source}) - ENDIF() - ENDIF() - ENDIF() - ENDFOREACH() - ENDIF() -ENDFUNCTION() - -FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$") - -STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}") -STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_MINOR "${GIT2_HEADER}") -STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_REV "${GIT2_HEADER}") -SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${LIBGIT2_VERSION_REV}") - -# Find required dependencies -INCLUDE_DIRECTORIES(src include) - -IF (WIN32 AND NOT MINGW) - ADD_DEFINITIONS(-DGIT_WINHTTP) -ELSE () - IF (NOT AMIGA) - FIND_PACKAGE(OpenSSL) - ENDIF () - FILE(GLOB SRC_HTTP deps/http-parser/*.c) - INCLUDE_DIRECTORIES(deps/http-parser) -ENDIF() - -# Specify sha1 implementation -IF (WIN32 AND NOT MINGW AND NOT SHA1_TYPE STREQUAL "builtin") - ADD_DEFINITIONS(-DWIN32_SHA1) - FILE(GLOB SRC_SHA1 src/hash/hash_win32.c) -ELSEIF (OPENSSL_FOUND AND NOT SHA1_TYPE STREQUAL "builtin") - ADD_DEFINITIONS(-DOPENSSL_SHA1) -ELSE() - FILE(GLOB SRC_SHA1 src/hash/hash_generic.c) -ENDIF() - -# Include POSIX regex when it is required -IF(WIN32 OR AMIGA) - INCLUDE_DIRECTORIES(deps/regex) - SET(SRC_REGEX deps/regex/regex.c) -ENDIF() - -# Optional external dependency: zlib -IF(NOT ZLIB_LIBRARY) - # It's optional, but FIND_PACKAGE gives a warning that looks more like an - # error. - FIND_PACKAGE(ZLIB QUIET) -ENDIF() -IF (ZLIB_FOUND) - INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS}) - LINK_LIBRARIES(${ZLIB_LIBRARIES}) -ELSE() - MESSAGE( "zlib was not found; using bundled 3rd-party sources." ) - INCLUDE_DIRECTORIES(deps/zlib) - ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP) - FILE(GLOB SRC_ZLIB deps/zlib/*.c) -ENDIF() - -# Platform specific compilation flags -IF (MSVC) +# This top-level CMakeLists.txt sets up configuration options and +# determines which subprojects to build. - STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") +cmake_minimum_required(VERSION 3.5.1) - SET(CMAKE_C_FLAGS "/MP /nologo /Zi ${CMAKE_C_FLAGS}") - IF (STDCALL) - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz") - ENDIF () - SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd /RTC1 /RTCs /RTCu") - SET(CMAKE_C_FLAGS_RELEASE "/MT /O2") - SET(WIN_RC "src/win32/git2.rc") +project(libgit2 VERSION "1.9.0" LANGUAGES C) - # Precompiled headers +# Add find modules to the path +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake") -ELSE () - SET(CMAKE_C_FLAGS "-D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes ${CMAKE_C_FLAGS}") - IF (MINGW) # MinGW always does PIC and complains if we tell it to - STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}") - ELSEIF (BUILD_SHARED_LIBS) - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fPIC") - ENDIF () - IF (APPLE) # Apple deprecated OpenSSL - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations") - ENDIF () - IF (PROFILE) - SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}") - SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}") - ENDIF () -ENDIF() - -IF( NOT CMAKE_CONFIGURATION_TYPES ) - # Build Debug by default - IF (NOT CMAKE_BUILD_TYPE) - SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE) - ENDIF () -ELSE() - # Using a multi-configuration generator eg MSVC or Xcode - # that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE -ENDIF() - -IF (OPENSSL_FOUND) - ADD_DEFINITIONS(-DGIT_SSL) - INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR}) - SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES}) -ENDIF() - -IF (THREADSAFE) - IF (NOT WIN32) - find_package(Threads REQUIRED) - ENDIF() - - ADD_DEFINITIONS(-DGIT_THREADS) -ENDIF() - -ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64) - -# Collect sourcefiles -FILE(GLOB SRC_H include/git2/*.h) - -# On Windows use specific platform sources -IF (WIN32 AND NOT CYGWIN) - ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501) - FILE(GLOB SRC_OS src/win32/*.c) -ELSEIF (AMIGA) - ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R) - FILE(GLOB SRC_OS src/amiga/*.c) -ELSE() - FILE(GLOB SRC_OS src/unix/*.c) -ENDIF() -FILE(GLOB SRC_GIT2 src/*.c src/transports/*.c src/xdiff/*.c) - -# Compile and link libgit2 -ADD_LIBRARY(git2 ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC}) -TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES}) -TARGET_OS_LIBRARIES(git2) - -MSVC_SPLIT_SOURCES(git2) - -SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING}) -SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR}) -CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY) - -IF (MSVC_IDE) - # Precompiled headers - SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") - SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h") -ENDIF () - -# Install -INSTALL(TARGETS git2 - RUNTIME DESTINATION ${BIN_INSTALL_DIR} - LIBRARY DESTINATION ${LIB_INSTALL_DIR} - ARCHIVE DESTINATION ${LIB_INSTALL_DIR} -) -INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig ) -INSTALL(DIRECTORY include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} ) -INSTALL(FILES include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} ) - -# Tests -IF (BUILD_CLAR) - FIND_PACKAGE(PythonInterp REQUIRED) +# +# Build options +# - SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/") - SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar") - SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.") - ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\") - ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\") +# Experimental features +option(EXPERIMENTAL_SHA256 "Enable experimental SHA256 support (for R&D/testing)" OFF) + +# Optional subsystems +option(BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON) +option(BUILD_TESTS "Build Tests using the Clar suite" ON) +option(BUILD_CLI "Build the command-line interface" ON) +option(BUILD_EXAMPLES "Build library usage example apps" OFF) +option(BUILD_FUZZERS "Build the fuzz targets" OFF) + +# Feature enablement and backend selection + set(USE_THREADS "" CACHE STRING "Use threads for parallel processing when possible. One of ON, OFF, or a specific provider: pthreads or win32. (Defaults to ON.)") + set(USE_SSH "" CACHE STRING "Enables SSH support and optionally selects provider. One of ON, OFF, or a specific provider: libssh2 or exec. (Defaults to OFF.)") + set(USE_HTTPS "" CACHE STRING "Enable HTTPS support and optionally selects the provider. One of ON, OFF, or a specific provider: OpenSSL, OpenSSL-FIPS, OpenSSL-Dynamic, mbedTLS, SecureTransport, Schannel, or WinHTTP. (Defaults to ON.)") + set(USE_SHA1 "" CACHE STRING "Selects SHA1 provider. One of builtin, HTTPS, or a specific provider. (Defaults to builtin.)") + set(USE_SHA256 "" CACHE STRING "Selects SHA256 provider. One of Builtin, HTTPS, or a specific provider. (Defaults to HTTPS.)") + set(USE_HTTP_PARSER "" CACHE STRING "Selects HTTP Parser support: http-parser, llhttp, or builtin. (Defaults to builtin.)") + set(USE_AUTH_NTLM "" CACHE STRING "Enables NTLM authentication support. One of Builtin or win32.") + set(USE_AUTH_NEGOTIATE "" CACHE STRING "Enable Negotiate (SPNEGO) authentication support. One of GSSAPI or win32.") +# set(USE_XDIFF "" CACHE STRING "Specifies the xdiff implementation; either system or builtin.") + set(USE_REGEX "" CACHE STRING "Selects regex provider. One of regcomp_l, pcre2, pcre, regcomp, or builtin.") + set(USE_COMPRESSION "" CACHE STRING "Selects compression backend. Either builtin or zlib.") + set(USE_NSEC "" CACHE STRING "Enable nanosecond precision timestamps. One of ON, OFF, or a specific provider: mtimespec, mtim, mtime, or win32. (Defaults to ON).") + +if(APPLE) + # Currently only available on macOS for `precomposeUnicode` support + set(USE_I18N "" CACHE STRING "Enables internationalization support.") +endif() + +# Debugging options + set(DEBUG_LEAK_CHECKER "" CACHE STRING "Configure for leak checking test runs. One of valgrind, leaks, or win32. Either valgrind or leaks.") +option(USE_STANDALONE_FUZZERS "Enable standalone fuzzers (compatible with gcc)" OFF) +option(DEBUG_POOL "Enable debug pool allocator" OFF) +option(DEBUG_STRICT_ALLOC "Enable strict allocator behavior" OFF) +option(DEBUG_STRICT_OPEN "Enable path validation in open" OFF) + +# Output options +option(SONAME "Set the (SO)VERSION of the target" ON) + set(LIBGIT2_FILENAME "git2" CACHE STRING "Name of the produced binary") +option(DEPRECATE_HARD "Do not include deprecated functions in the library" OFF) + +# Compilation options +# Default to c99 on Android Studio for compatibility; c90 everywhere else +if("${CMAKE_SYSTEM_NAME}" STREQUAL "Android") + set(CMAKE_C_STANDARD "99" CACHE STRING "The C standard to compile against") +else() + set(CMAKE_C_STANDARD "90" CACHE STRING "The C standard to compile against") +endif() +option(CMAKE_C_EXTENSIONS "Whether compiler extensions are supported" OFF) +option(ENABLE_WERROR "Enable compilation with -Werror" OFF) + +if(UNIX) + option(ENABLE_REPRODUCIBLE_BUILDS "Enable reproducible builds" OFF) +endif() + +if(MSVC) + # This option must match the settings used in your program, in particular if you + # are linking statically + option(STATIC_CRT "Link the static CRT libraries" ON) +endif() + +if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE) +endif() + + +# Modules + +include(FeatureSummary) +include(CheckLibraryExists) +include(CheckFunctionExists) +include(CheckSymbolExists) +include(CheckStructHasMember) +include(CheckPrototypeDefinitionSafe) +include(AddCFlagIfSupported) +include(FindPkgLibraries) +include(FindThreads) +include(FindStatNsec) +include(Findfutimens) +include(GNUInstallDirs) +include(IdeSplitSources) +include(EnableWarnings) +include(DefaultCFlags) +include(ExperimentalFeatures) - INCLUDE_DIRECTORIES(${CLAR_PATH}) - FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c) - SET(SRC_CLAR "${CLAR_PATH}/main.c" "${CLAR_PATH}/clar_libgit2.c" "${CLAR_PATH}/clar.c") - ADD_CUSTOM_COMMAND( - OUTPUT ${CLAR_PATH}/clar.suite - COMMAND ${PYTHON_EXECUTABLE} generate.py -xonline . - DEPENDS ${SRC_TEST} - WORKING_DIRECTORY ${CLAR_PATH} - ) +# +# Subdirectories +# - SET_SOURCE_FILES_PROPERTIES( - ${CLAR_PATH}/clar.c - PROPERTIES OBJECT_DEPENDS ${CLAR_PATH}/clar.suite) +add_subdirectory(src) - ADD_EXECUTABLE(libgit2_clar ${SRC_GIT2} ${SRC_OS} ${SRC_CLAR} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1}) +if(BUILD_TESTS) + enable_testing() + add_subdirectory(tests) +endif() - TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES}) - TARGET_OS_LIBRARIES(libgit2_clar) - MSVC_SPLIT_SOURCES(libgit2_clar) +if(BUILD_EXAMPLES) + add_subdirectory(examples) +endif() - IF (MSVC_IDE) - # Precompiled headers - SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") - ENDIF () +if(BUILD_FUZZERS) + if((BUILD_TESTS OR BUILD_EXAMPLES) AND NOT USE_STANDALONE_FUZZERS) + message(FATAL_ERROR "Cannot build the fuzzer and the tests or examples together") + endif() + add_subdirectory(fuzzers) +endif() - ENABLE_TESTING() - ADD_TEST(libgit2_clar libgit2_clar -ionline) -ENDIF () -IF (TAGS) - FIND_PROGRAM(CTAGS ctags) - IF (NOT CTAGS) - message(FATAL_ERROR "Could not find ctags command") - ENDIF () +# Export for people who use us as a dependency - FILE(GLOB_RECURSE SRC_ALL *.[ch]) +if(NOT "${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}") + set(LIBGIT2_DEPENDENCY_OBJECTS ${LIBGIT2_DEPENDENCY_OBJECTS} PARENT_SCOPE) + set(LIBGIT2_SYSTEM_LIBS ${LIBGIT2_SYSTEM_LIBS} PARENT_SCOPE) +endif() - ADD_CUSTOM_COMMAND( - OUTPUT tags - COMMAND ${CTAGS} -a ${SRC_ALL} - DEPENDS ${SRC_ALL} - ) - ADD_CUSTOM_TARGET( - do_tags ALL - DEPENDS tags - ) -ENDIF () -IF (BUILD_EXAMPLES) - FILE(GLOB_RECURSE EXAMPLE_SRC examples/network/*.c) - ADD_EXECUTABLE(cgit2 ${EXAMPLE_SRC}) - IF(WIN32) - TARGET_LINK_LIBRARIES(cgit2 git2) - ELSE() - TARGET_LINK_LIBRARIES(cgit2 git2 pthread) - ENDIF() +# Summary - ADD_EXECUTABLE(git-diff examples/diff.c) - TARGET_LINK_LIBRARIES(git-diff git2) +feature_summary(WHAT ENABLED_FEATURES DESCRIPTION "Enabled features:") +feature_summary(WHAT DISABLED_FEATURES DESCRIPTION "Disabled features:") - ADD_EXECUTABLE(git-general examples/general.c) - TARGET_LINK_LIBRARIES(git-general git2) +# warn for not using sha1dc - ADD_EXECUTABLE(git-showindex examples/showindex.c) - TARGET_LINK_LIBRARIES(git-showindex git2) -ENDIF () +foreach(WARNING ${WARNINGS}) + message(WARNING ${WARNING}) +endforeach() diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 85617948180..00000000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,47 +0,0 @@ -# Welcome to libgit2! - -We're making it easy to do interesting things with git, and we'd love to have -your help. - -## Discussion & Chat - -We hang out in the #libgit2 channel on irc.freenode.net. - -## Reporting Bugs - -First, know which version of libgit2 your problem is in. Compile and test -against the `development` branch to avoid re-reporting an issue that's already -been fixed. - -It's *incredibly* helpful to be able to reproduce the problem. Please include -a bit of code and/or a zipped repository (if possible). Note that some of the -developers are employees of GitHub, so if your repository is private, find us -on IRC and we'll figure out a way to help you. - -## Pull Requests - -Life will be a lot easier for you if you create a named branch for your -contribution, rather than just using your fork's `development`. - -It's helpful if you include a nice description of your change with your PR; if -someone has to read the whole diff to figure out why you're contributing in the -first place, you're less likely to get feedback and have your change merged in. - -## Porting Code From Other Open-Source Projects - -The most common case here is porting code from core Git. Git is a GPL project, -which means that in order to port code to this project, we need the explicit -permission of the author. Check the -[`git.git-authors`](https://github.com/libgit2/libgit2/blob/development/git.git-authors) -file for authors who have already consented; feel free to add someone if you've -obtained their consent. - -Other licenses have other requirements; check the license of the library you're -porting code *from* to see what you need to do. - -## Styleguide - -We like to keep the source code consistent and easy to read. Maintaining this -takes some discipline, but it's been more than worth it. Take a look at the -[conventions file](https://github.com/libgit2/libgit2/blob/development/CONVENTIONS.md). - diff --git a/CONVENTIONS.md b/CONVENTIONS.md deleted file mode 100644 index 1e909a3e83f..00000000000 --- a/CONVENTIONS.md +++ /dev/null @@ -1,169 +0,0 @@ -# Libgit2 Conventions - -We like to keep the source consistent and readable. Herein are some guidelines -that should help with that. - - -## Naming Things - -All types and functions start with `git_`, and all #define macros start with `GIT_`. - -Functions with a single output parameter should name that parameter `out`. -Multiple outputs should be named `foo_out`, `bar_out`, etc. - -Parameters of type `git_oid` should be named `id`, or `foo_id`. Calls that -return an OID should be named `git_foo_id`. - -Where there is a callback passed in, the `void *` that is passed into it should -be named "payload". - -## Typedef - -Wherever possible, use `typedef`. If a structure is just a collection of -function pointers, the pointer types don't need to be separately typedef'd, but -loose function pointer types should be. - -## Exports - -All exported functions must be declared as: - -```C -GIT_EXTERN(result_type) git_modulename_functionname(arg_list); -``` - -## Internals - -Functions whose modulename is followed by two underscores, -for example `git_odb__read_packed`, are semi-private functions. -They are primarily intended for use within the library itself, -and may disappear or change their signature in a future release. - -## Parameters - -Out parameters come first. - -Whenever possible, pass argument pointers as `const`. Some structures (such -as `git_repository` and `git_index`) have internal structure that prevents -this. - -Callbacks should always take a `void *` payload as their last parameter. -Callback pointers are grouped with their payloads, and come last when passed as -arguments: - -```C -int foo(git_repository *repo, git_foo_cb callback, void *payload); -``` - - -## Memory Ownership - -Some APIs allocate memory which the caller is responsible for freeing; others -return a pointer into a buffer that's owned by some other object. Make this -explicit in the documentation. - - -## Return codes - -Return an `int` when a public API can fail in multiple ways. These may be -transformed into exception types in some bindings, so returning a semantically -appropriate error code is important. Check -[`errors.h`](https://github.com/libgit2/libgit2/blob/development/include/git2/errors.h) -for the return codes already defined. - -Use `giterr_set` to provide extended error information to callers. - -If an error is not to be propagated, use `giterr_clear` to prevent callers from -getting the wrong error message later on. - - -## Opaque Structs - -Most types should be opaque, e.g.: - -```C -typedef struct git_odb git_odb; -``` - -...with allocation functions returning an "instance" created within -the library, and not within the application. This allows the type -to grow (or shrink) in size without rebuilding client code. - -To preserve ABI compatibility, include an `int version` field in all opaque -structures, and initialize to the latest version in the construction call. -Increment the "latest" version whenever the structure changes, and try to only -append to the end of the structure. - -## Option Structures - -If a function's parameter count is too high, it may be desirable to package up -the options in a structure. Make them transparent, include a version field, -and provide an initializer constant or constructor. Using these structures -should be this easy: - -```C -git_foo_options opts = GIT_FOO_OPTIONS_INIT; -opts.baz = BAZ_OPTION_ONE; -git_foo(&opts); -``` - -## Enumerations - -Typedef all enumerated types. If each option stands alone, use the enum type -for passing them as parameters; if they are flags, pass them as `unsigned int`. - -## Code Layout - -Try to keep lines less than 80 characters long. Use common sense to wrap most -code lines; public function declarations should use this convention: - -```C -GIT_EXTERN(int) git_foo_id( - git_oid **out, - int a, - int b); -``` - -Indentation is done with tabs; set your editor's tab width to 3 for best effect. - - -## Documentation - -All comments should conform to Doxygen "javadoc" style conventions for -formatting the public API documentation. Try to document every parameter, and -keep the comments up to date if you change the parameter list. - - -## Public Header Template - -Use this template when creating a new public header. - -```C -#ifndef INCLUDE_git_${filename}_h__ -#define INCLUDE_git_${filename}_h__ - -#include "git/common.h" - -/** - * @file git/${filename}.h - * @brief Git some description - * @defgroup git_${filename} some description routines - * @ingroup Git - * @{ - */ -GIT_BEGIN_DECL - -/* ... definitions ... */ - -/** @} */ -GIT_END_DECL -#endif -``` - -## Inlined functions - -All inlined functions must be declared as: - -```C -GIT_INLINE(result_type) git_modulename_functionname(arg_list); -``` - diff --git a/COPYING b/COPYING index d1ca4d401e3..f53de296a9c 100644 --- a/COPYING +++ b/COPYING @@ -365,7 +365,7 @@ Public License instead of this License. The bundled ZLib code is licensed under the ZLib license: -Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler + (C) 1995-2022 Jean-loup Gailly and Mark Adler This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -388,43 +388,76 @@ Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler ---------------------------------------------------------------------- -The priority queue implementation is based on code licensed under the -Apache 2.0 license: +The Clar framework is licensed under the ISC license: - Copyright 2010 Volkan Yazıcı - Copyright 2006-2010 The Apache Software Foundation +Copyright (c) 2011-2015 Vicent Marti -The full text of the Apache 2.0 license is available at: +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. - http://www.apache.org/licenses/LICENSE-2.0 +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ---------------------------------------------------------------------- -The Clay framework is licensed under the MIT license: +The bundled PCRE implementation (deps/pcre/) is licensed under the BSD +license. -Copyright (C) 2011 by Vicent Marti +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. + * Neither the name of the University of Cambridge nor the name of Google + Inc. nor the names of their contributors may be used to endorse or + promote products derived from this software without specific prior + written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. ---------------------------------------------------------------------- -The regex library (deps/regex/) is licensed under the GNU LGPL +The bundled winhttp definition files (deps/winhttp/) are licensed under +the GNU LGPL (available at the end of this file). + +Copyright (C) 2007 Francois Gouget + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library 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 +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + +---------------------------------------------------------------------- GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 @@ -928,3 +961,331 @@ necessary. Here is a sample; alter the names: Ty Coon, President of Vice That's all there is to it! + +---------------------------------------------------------------------- + +The bundled SHA1 collision detection code is licensed under the MIT license: + +MIT License + +Copyright (c) 2017: + Marc Stevens + Cryptology Group + Centrum Wiskunde & Informatica + P.O. Box 94079, 1090 GB Amsterdam, Netherlands + marc@marc-stevens.nl + + Dan Shumow + Microsoft Research + danshu@microsoft.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +---------------------------------------------------------------------- + +The bundled wildmatch code was originally written by Rich $alz and is +available in the public domain. + +---------------------------------------------------------------------- + +Portions of the OpenSSL headers are included under the OpenSSL license: + +Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. + +Licensed under the Apache License 2.0 (the "License"). You may not use +this file except in compliance with the License. You can obtain a copy +in the file LICENSE in the source distribution or at +https://www.openssl.org/source/license.html + +---------------------------------------------------------------------- + +The xoroshiro256** implementation is licensed in the public domain: + +Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . + +---------------------------------------------------------------------- + +The built-in SHA256 support (src/hash/rfc6234) is taken from RFC 6234 +under the following license: + +Copyright (c) 2011 IETF Trust and the persons identified as +authors of the code. All rights reserved. + +Redistribution and use in source and binary forms, with or +without modification, are permitted provided that the following +conditions are met: + +- Redistributions of source code must retain the above + copyright notice, this list of conditions and + the following disclaimer. + +- Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + +- Neither the name of Internet Society, IETF or IETF Trust, nor + the names of specific contributors, may be used to endorse or + promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +---------------------------------------------------------------------- + +The built-in git_fs_path_basename_r() function is based on the +Android implementation, BSD licensed: + +Copyright (C) 2008 The Android Open Source Project +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +* Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +---------------------------------------------------------------------- + +The bundled ntlmclient code is licensed under the MIT license: + +Copyright (c) Edward Thomson. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + +---------------------------------------------------------------------- + +Portions of this software derived from Team Explorer Everywhere: + +Copyright (c) Microsoft Corporation + +All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +--------------------------------------------------------------------------- + +Portions of this software derived from the LLVM Compiler Infrastructure: + +Copyright (c) 2003-2016 University of Illinois at Urbana-Champaign. +All rights reserved. + +Developed by: + + LLVM Team + + University of Illinois at Urbana-Champaign + + http://llvm.org + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimers. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimers in the + documentation and/or other materials provided with the distribution. + + * Neither the names of the LLVM Team, University of Illinois at + Urbana-Champaign, nor the names of its contributors may be used to + endorse or promote products derived from this Software without specific + prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE +SOFTWARE. + +--------------------------------------------------------------------------- + +Portions of this software derived from Unicode, Inc: + +Copyright 2001-2004 Unicode, Inc. + +Disclaimer + +This source code is provided as is by Unicode, Inc. No claims are +made as to fitness for any particular purpose. No warranties of any +kind are expressed or implied. The recipient agrees to determine +applicability of information provided. If this file has been +purchased on magnetic or optical media from Unicode, Inc., the +sole remedy for any claim will be exchange of defective media +within 90 days of receipt. + +Limitations on Rights to Redistribute This Code + +Unicode, Inc. hereby grants the right to freely use the information +supplied in this file in the creation of products supporting the +Unicode Standard, and to make copies of this file in any form +for internal or external distribution as long as this notice +remains attached. + +--------------------------------------------------------------------------- + +Portions of this software derived from sheredom/utf8.h: + +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to + +--------------------------------------------------------------------------- + +Portions of this software derived from RFC 1320: + +Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved. + +License to copy and use this software is granted provided that it +is identified as the "RSA Data Security, Inc. MD4 Message-Digest +Algorithm" in all material mentioning or referencing this software +or this function. + +License is also granted to make and use derivative works provided +that such works are identified as "derived from the RSA Data +Security, Inc. MD4 Message-Digest Algorithm" in all material +mentioning or referencing the derived work. + +RSA Data Security, Inc. makes no representations concerning either +the merchantability of this software or the suitability of this +software for any particular purpose. It is provided "as is" +without express or implied warranty of any kind. + +These notices must be retained in any copies of any part of this +documentation and/or software. + +---------------------------------------------------------------------- + +The bundled llhttp dependency is licensed under the MIT license: + +Copyright Fedor Indutny, 2018. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to permit +persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/FUNDING.json b/FUNDING.json new file mode 100644 index 00000000000..eb2f6310368 --- /dev/null +++ b/FUNDING.json @@ -0,0 +1,7 @@ +{ + "drips": { + "ethereum": { + "ownedBy": "0x939121dD13f796C69d0Ac4185787285518081f8D" + } + } +} diff --git a/Makefile.embed b/Makefile.embed deleted file mode 100644 index 76b4d3cda2d..00000000000 --- a/Makefile.embed +++ /dev/null @@ -1,42 +0,0 @@ -PLATFORM=$(shell uname -o) - -rm=rm -f -AR=ar cq -RANLIB=ranlib -LIBNAME=libgit2.a -ifeq ($(PLATFORM),Msys) - CC=gcc -else - CC=cc -endif - -INCLUDES= -I. -Isrc -Iinclude -Ideps/http-parser -Ideps/zlib - -DEFINES= $(INCLUDES) -DNO_VIZ -DSTDC -DNO_GZIP -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(EXTRA_DEFINES) -CFLAGS= -g $(DEFINES) -Wall -Wextra -O2 $(EXTRA_CFLAGS) - -SRCS = $(wildcard src/*.c) $(wildcard src/transports/*.c) $(wildcard src/xdiff/*.c) $(wildcard deps/http-parser/*.c) $(wildcard deps/zlib/*.c) src/hash/hash_generic.c - -ifeq ($(PLATFORM),Msys) - SRCS += $(wildcard src/win32/*.c) $(wildcard src/compat/*.c) deps/regex/regex.c - INCLUDES += -Ideps/regex - DEFINES += -DWIN32 -D_WIN32_WINNT=0x0501 -else - SRCS += $(wildcard src/unix/*.c) - CFLAGS += -fPIC -endif - -OBJS = $(patsubst %.c,%.o,$(SRCS)) - -%.c.o: - $(CC) $(CFLAGS) -c $*.c - -all: $(LIBNAME) - -$(LIBNAME): $(OBJS) - $(rm) $@ - $(AR) $@ $(OBJS) - $(RANLIB) $@ - -clean: - $(rm) $(OBJS) $(LIBNAME) diff --git a/README.md b/README.md index 91e0ce4bb2e..08eda848569 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,149 @@ libgit2 - the Git linkable library -====================== - -[![Build Status](https://secure.travis-ci.org/libgit2/libgit2.png?branch=development)](http://travis-ci.org/libgit2/libgit2) - -libgit2 is a portable, pure C implementation of the Git core methods provided as a -re-entrant linkable library with a solid API, allowing you to write native -speed custom Git applications in any language with bindings. +================================== +[![OpenSSF Best Practices](https://www.bestpractices.dev/projects/9609/badge)](https://www.bestpractices.dev/projects/9609) + +| Build Status | | +| ------------ | - | +| **main** branch builds | [![CI Build](https://github.com/libgit2/libgit2/actions/workflows/main.yml/badge.svg?branch=main&event=push)](https://github.com/libgit2/libgit2/actions/workflows/main.yml?query=event%3Apush+branch%3Amain) [![Experimental Features](https://github.com/libgit2/libgit2/actions/workflows/experimental.yml/badge.svg?branch=main)](https://github.com/libgit2/libgit2/actions/workflows/experimental.yml?query=event%3Apush+branch%3Amain) | +| **v1.9 branch** builds | [![CI Build](https://github.com/libgit2/libgit2/actions/workflows/main.yml/badge.svg?branch=maint%2Fv1.9&event=push)](https://github.com/libgit2/libgit2/actions/workflows/main.yml?query=event%3Apush+branch%3Amaint%2Fv1.9) [![Experimental Features](https://github.com/libgit2/libgit2/actions/workflows/experimental.yml/badge.svg?branch=maint%2Fv1.9)](https://github.com/libgit2/libgit2/actions/workflows/experimental.yml?query=event%3Apush+branch%3Amaint%2Fv1.9) | +| **v1.8 branch** builds | [![CI Build](https://github.com/libgit2/libgit2/actions/workflows/main.yml/badge.svg?branch=maint%2Fv1.8&event=push)](https://github.com/libgit2/libgit2/actions/workflows/main.yml?query=event%3Apush+branch%3Amaint%2Fv1.8) [![Experimental Features](https://github.com/libgit2/libgit2/actions/workflows/experimental.yml/badge.svg?branch=maint%2Fv1.8)](https://github.com/libgit2/libgit2/actions/workflows/experimental.yml?query=event%3Apush+branch%3Amaint%2Fv1.8) | +| **Nightly** builds | [![Nightly Build](https://github.com/libgit2/libgit2/actions/workflows/nightly.yml/badge.svg?branch=main&event=schedule)](https://github.com/libgit2/libgit2/actions/workflows/nightly.yml) [![Coverity Scan Status](https://scan.coverity.com/projects/639/badge.svg)](https://scan.coverity.com/projects/639) | + +`libgit2` is a portable, pure C implementation of the Git core methods +provided as a linkable library with a solid API, allowing to build Git +functionality into your application. + +`libgit2` is used in a variety of places, from GUI clients to hosting +providers ("forges") and countless utilities and applications in +between. Because it's written in C, it can be made available to any +other programming language through "bindings", so you can use it in +[Ruby](https://github.com/libgit2/rugged), +[.NET](https://github.com/libgit2/libgit2sharp), +[Python](http://www.pygit2.org/), +[Node.js](http://nodegit.org), +[Rust](https://github.com/rust-lang/git2-rs), and more. + +`libgit2` is licensed under a **very permissive license** (GPLv2 with +a special Linking Exception). This means that you can link against +the library with any kind of software without making that software +fall under the GPL. Changes to libgit2 would still be covered under +its GPL license. + +Table of Contents +================= + +* [Using libgit2](#using-libgit2) +* [Quick Start](#quick-start) +* [Getting Help](#getting-help) +* [What It Can Do](#what-it-can-do) +* [Optional dependencies](#optional-dependencies) +* [Initialization](#initialization) +* [Threading](#threading) +* [Conventions](#conventions) +* [Building libgit2 - Using CMake](#building-libgit2---using-cmake) + * [Building](#building) + * [Installation](#installation) + * [Advanced Usage](#advanced-usage) + * [Compiler and linker options](#compiler-and-linker-options) + * [macOS](#macos) + * [iOS](#ios) + * [Android](#android) + * [MinGW](#mingw) +* [Language Bindings](#language-bindings) +* [How Can I Contribute?](#how-can-i-contribute) +* [License](#license) + +Using libgit2 +============= + +Most of these instructions assume that you're writing an application +in C and want to use libgit2 directly. If you're _not_ using C, +and you're writing in a different language or platform like .NET, +Node.js, or Ruby, then there is probably a +"[language binding](#language-bindings)" that you can use to take care +of the messy tasks of calling into native code. + +But if you _do_ want to use libgit2 directly - because you're building +an application in C - then you may be able use an existing binary. +There are packages for the +[vcpkg](https://github.com/Microsoft/vcpkg) and +[conan](https://conan.io/center/recipes/libgit2) +package managers. And libgit2 is available in +[Homebrew](https://formulae.brew.sh/formula/libgit2) and most Linux +distributions. + +However, these versions _may_ be outdated and we recommend using the +latest version if possible. Thankfully libgit2 is not hard to compile. + +Quick Start +=========== + +**Prerequisites** for building libgit2: + +1. [CMake](https://cmake.org/), and is recommended to be installed into + your `PATH`. +2. [Python](https://www.python.org) is used by our test framework, and + should be installed into your `PATH`. +3. C compiler: libgit2 is C90 and should compile on most compilers. + * Windows: Visual Studio is recommended + * Mac: Xcode is recommended + * Unix: gcc or clang is recommended. + +**Build** + +1. Create a build directory beneath the libgit2 source directory, + and change into it: `mkdir build && cd build` +2. Create the cmake build environment: `cmake ..` +3. Build libgit2: `cmake --build .` + +Trouble with these steps? Read our [troubleshooting guide](docs/troubleshooting.md). +More detailed build guidance is available below. + +Getting Help +============ + +**Chat with us** + +- via IRC: join [#libgit2](https://web.libera.chat/#libgit2) on + [libera](https://libera.chat). +- via Slack: visit [slack.libgit2.org](http://slack.libgit2.org/) + to sign up, then join us in `#libgit2` + +**Getting Help** + +If you have questions about the library, please be sure to check out the +[API documentation](https://libgit2.org/libgit2/). If you still have +questions, reach out to us on Slack or post a question on +[StackOverflow](http://stackoverflow.com/questions/tagged/libgit2) +(with the `libgit2` tag). + +**Reporting Bugs** + +Please open a [GitHub Issue](https://github.com/libgit2/libgit2/issues) +and include as much information as possible. If possible, provide +sample code that illustrates the problem you're seeing. If you're +seeing a bug only on a specific repository, please provide a link to +it if possible. + +We ask that you not open a GitHub Issue for help, only for bug reports. + +**Reporting Security Issues** + +Please have a look at SECURITY.md. -libgit2 is licensed under a **very permissive license** (GPLv2 with a special Linking Exception). -This basically means that you can link it (unmodified) with any kind of software without having to -release its source code. +What It Can Do +============== -* Mailing list: ~~~~ - The libgit2 mailing list has - traditionally been hosted in Librelist, but Librelist is and has always - been a shitshow. We encourage you to [open an issue](https://github.com/libgit2/libgit2/issues) - on GitHub instead for any questions regarding the library. - * Archives: -* Website: -* API documentation: -* Usage guide: +libgit2 provides you with the ability to manage Git repositories in the +programming language of your choice. It's used in production to power many +applications including GitHub.com, Plastic SCM and Azure DevOps. -What It Can Do -================================== +It does not aim to replace the git tool or its user-facing commands. Some +APIs resemble the plumbing commands as those align closely with the +concepts of the Git system, but most commands a user would type are out +of scope for this library to implement directly. -libgit2 is already very usable. +The library provides: * SHA conversions, formatting and shortening * abstracted ODB backend system @@ -39,15 +158,71 @@ libgit2 is already very usable. * descriptive and detailed error messages * ...and more (over 175 different API calls) +As libgit2 is purely a consumer of the Git system, we have to +adjust to changes made upstream. This has two major consequences: + +* Some changes may require us to change provided interfaces. While + we try to implement functions in a generic way so that no future + changes are required, we cannot promise a completely stable API. +* As we have to keep up with changes in behavior made upstream, we + may lag behind in some areas. We usually to document these + incompatibilities in our issue tracker with the label "git change". + +Optional dependencies +===================== + +While the library provides git functionality with very few +dependencies, some recommended dependencies are used for performance +or complete functionality. + +- Hash generation: Git uses SHA1DC (collision detecting SHA1) for + its default hash generation. SHA256 support is experimental, and + optimized support is provided by system libraries on macOS and + Windows, or by the HTTPS library on Unix systems when available. +- Threading: is provided by the system libraries on Windows, and + pthreads on Unix systems. +- HTTPS: is provided by the system libraries on macOS and Windows, + or by OpenSSL or mbedTLS on other Unix systems. +- SSH: is provided by [libssh2](https://libssh2.org/) or by invoking + [OpenSSH](https://www.openssh.com). +- Unicode: is provided by the system libraries on Windows and macOS. + +Initialization +=============== + +The library needs to keep track of some global state. Call + + git_libgit2_init(); + +before calling any other libgit2 functions. You can call this function many times. A matching number of calls to + + git_libgit2_shutdown(); + +will free the resources. Note that if you have worker threads, you should +call `git_libgit2_shutdown` *after* those threads have exited. If you +require assistance coordinating this, simply have the worker threads call +`git_libgit2_init` at startup and `git_libgit2_shutdown` at shutdown. + +Threading +========= + +See [threading](docs/threading.md) for information + +Conventions +=========== + +See [conventions](docs/conventions.md) for an overview of the external +and internal API/coding conventions we use. + Building libgit2 - Using CMake ============================== -libgit2 builds cleanly on most platforms without any external dependencies. -Under Unix-like systems, like Linux, \*BSD and Mac OS X, libgit2 expects `pthreads` to be available; -they should be installed by default on all systems. Under Windows, libgit2 uses the native Windows API -for threading. +Building +-------- -The libgit2 library is built using CMake 2.6+ () on all platforms. +`libgit2` builds cleanly on most platforms without any external +dependencies as a requirement. `libgit2` is built using +[CMake]() (version 2.8 or newer) on all platforms. On most systems you can build the library using the following commands @@ -55,54 +230,255 @@ On most systems you can build the library using the following commands $ cmake .. $ cmake --build . +To include the examples in the build, use `cmake -DBUILD_EXAMPLES=ON ..` +instead of `cmake ..`. The built executable for the examples can then +be found in `build/examples`, relative to the toplevel directory. + Alternatively you can point the CMake GUI tool to the CMakeLists.txt file and generate platform specific build project or IDE workspace. +If you're not familiar with CMake, [a more detailed explanation](https://preshing.com/20170511/how-to-build-a-cmake-based-project/) may be helpful. + +Advanced Options +---------------- + +You can specify a number of options to `cmake` that will change the +way `libgit2` is built. To use this, specify `-Doption=value` during +the initial `cmake` configuration. For example, to enable SHA256 +compatibility: + + $ mkdir build && cd build + $ cmake -DEXPERIMENTAL_SHA256=ON .. + $ cmake --build . + +libgit2 options: + +* `EXPERIMENTAL_SHA256=ON`: turns on SHA256 compatibility; note that + this is an API-incompatible change, hence why it is labeled + "experimental" + +Build options: + +* `BUILD_EXAMPLES=ON`: builds the suite of example code +* `BUILD_FUZZERS=ON`: builds the fuzzing suite +* `ENABLE_WERROR=ON`: build with `-Werror` or the equivalent, which turns + compiler warnings into errors in the libgit2 codebase (but not its + dependencies) + +Dependency options: + +* `USE_SSH=type`: enables SSH support and optionally selects the provider; + `type` can be set to `libssh2` or `exec` (which will execute an external + OpenSSH command). `ON` implies `libssh2`; defaults to `OFF`. +* `USE_HTTPS=type`: enables HTTPS support and optionally selects the + provider; `type` can be set to `OpenSSL`, `OpenSSL-Dynamic` (to not + link against OpenSSL, but load it dynamically), `SecureTransport`, + `Schannel` or `WinHTTP`; the default is `SecureTransport` on macOS, + `WinHTTP` on Windows, and whichever of `OpenSSL` or `mbedTLS` is + detected on other platforms. Defaults to `ON`. +* `USE_SHA1=type`: selects the SHA1 mechanism to use; `type` can be set + to `CollisionDetection`, `HTTPS` to use the system or HTTPS provider, + or one of `OpenSSL`, `OpenSSL-Dynamic`, `OpenSSL-FIPS` (to use FIPS + compliant routines in OpenSSL), `CommonCrypto`, or `Schannel`. + Defaults to `CollisionDetection`. This option is retained for + backward compatibility and should not be changed. +* `USE_SHA256=type`: selects the SHA256 mechanism to use; `type` can be + set to `HTTPS` to use the system or HTTPS driver, `builtin`, or one of + `OpenSSL`, `OpenSSL-Dynamic`, `OpenSSL-FIPS` (to use FIPS compliant + routines in OpenSSL), `CommonCrypto`, or `Schannel`. Defaults to `HTTPS`. +* `USE_GSSAPI=`: enables GSSAPI for SPNEGO authentication on + Unix. Defaults to `OFF`. +* `USE_HTTP_PARSER=type`: selects the HTTP Parser; either `http-parser` + for an external + [`http-parser`](https://github.com/nodejs/http-parser) dependency, + `llhttp` for an external [`llhttp`](https://github.com/nodejs/llhttp) + dependency, or `builtin`. Defaults to `builtin`. +* `REGEX_BACKEND=type`: selects the regular expression backend to use; + one of `regcomp_l`, `pcre2`, `pcre`, `regcomp`, or `builtin`. The + default is to use `regcomp_l` where available, PCRE if found, otherwise, + to use the builtin. +* `USE_BUNDLED_ZLIB=type`: selects the bundled zlib; either `ON` or `OFF`. + Defaults to using the system zlib if available, falling back to the + bundled zlib. + +Locating Dependencies +--------------------- + +The `libgit2` project uses `cmake` since it helps with cross-platform +projects, especially those with many dependencies. If your dependencies +are in non-standard places, you may want to use the `_ROOT_DIR` options +to specify their location. For example, to specify an OpenSSL location: + + $ cmake -DOPENSSL_ROOT_DIR=/tmp/openssl-3.3.2 .. + +Since these options are general to CMake, their +[documentation](https://cmake.org/documentation/) may be helpful. If +you have questions about dependencies, please [contact us](#getting-help). + +Running Tests +------------- + +Once built, you can run the tests from the `build` directory with the command + + $ ctest -V + +Alternatively you can run the test suite directly using, + + $ ./libgit2_tests + +Invoking the test suite directly is useful because it allows you to execute +individual tests, or groups of tests using the `-s` flag. For example, to +run the index tests: + + $ ./libgit2_tests -sindex + +To run a single test named `index::racy::diff`, which corresponds to +the test function +[`test_index_racy__diff`](https://github.com/libgit2/libgit2/blob/main/tests/libgit2/index/racy.c#L22): + + $ ./libgit2_tests -sindex::racy::diff + +The test suite will print a `.` for every passing test, and an `F` for any +failing test. An `S` indicates that a test was skipped because it is not +applicable to your platform or is particularly expensive. + +**Note:** There should be _no_ failing tests when you build an unmodified +source tree from a [release](https://github.com/libgit2/libgit2/releases), +or from the [main branch](https://github.com/libgit2/libgit2/tree/main). +Please contact us or +[open an issue](https://github.com/libgit2/libgit2/issues) +if you see test failures. + +Installation +------------ + To install the library you can specify the install prefix by setting: $ cmake .. -DCMAKE_INSTALL_PREFIX=/install/prefix $ cmake --build . --target install -For more advanced use or questions about CMake please read . +Advanced Usage +-------------- + +For more advanced use or questions about CMake please read the +[CMake FAQ](https://cmake.org/Wiki/CMake_FAQ). The following CMake variables are declared: -- `BIN_INSTALL_DIR`: Where to install binaries to. -- `LIB_INSTALL_DIR`: Where to install libraries to. -- `INCLUDE_INSTALL_DIR`: Where to install headers to. +- `CMAKE_INSTALL_BINDIR`: Where to install binaries to. +- `CMAKE_INSTALL_LIBDIR`: Where to install libraries to. +- `CMAKE_INSTALL_INCLUDEDIR`: Where to install headers to. - `BUILD_SHARED_LIBS`: Build libgit2 as a Shared Library (defaults to ON) -- `BUILD_CLAR`: Build [Clar](https://github.com/vmg/clar)-based test suite (defaults to ON) -- `THREADSAFE`: Build libgit2 with threading support (defaults to OFF) -- `STDCALL`: Build libgit2 as `stdcall`. Turn off for `cdecl` (Windows; defaults to ON) +- `BUILD_TESTS`: Build the unit and integration test suites (defaults to ON) +- `USE_THREADS`: Build libgit2 with threading support (defaults to ON) + +To list all build options and their current value, you can do the +following: + + # Create and set up a build directory + $ mkdir build && cd build + $ cmake .. + + # List all build options and their values + $ cmake -L Compiler and linker options --------------------------- -CMake lets you specify a few variables to control the behavior of the -compiler and linker. These flags are rarely used but can be useful for -64-bit to 32-bit cross-compilation. +There are several options that control the behavior of the compiler and +linker. These flags may be useful for cross-compilation or specialized +setups. - `CMAKE_C_FLAGS`: Set your own compiler flags +- `CMAKE_C_STANDARD`: the C standard to compile against; defaults to `C90` +- `CMAKE_C_EXTENSIONS`: whether compiler extensions are supported; defaults to `OFF` - `CMAKE_FIND_ROOT_PATH`: Override the search path for libraries - `ZLIB_LIBRARY`, `OPENSSL_SSL_LIBRARY` AND `OPENSSL_CRYPTO_LIBRARY`: Tell CMake where to find those specific libraries +- `LINK_WITH_STATIC_LIBRARIES`: Link only with static versions of +system libraries -MacOS X +macOS ------- -If you want to build a universal binary for Mac OS X, CMake sets it -all up for you if you use `-DCMAKE_OSX_ARCHITECTURES="i386;x86_64"` -when configuring. +If you'd like to work with Xcode, you can generate an Xcode project with "-G Xcode". -Windows + # Create and set up a build directory + $ mkdir build && cd build + $ cmake -G Xcode .. + +> [!TIP] +> Universal binary support: +> +> If you want to build a universal binary for macOS 11.0+, CMake sets it +> all up for you if you use `-DCMAKE_OSX_ARCHITECTURES="x86_64;arm64"` +> when configuring. +> +> [Deprecated] If you want to build a universal binary for Mac OS X +> (10.4.4 ~ 10.6), CMake sets it all up for you if you use +> `-DCMAKE_OSX_ARCHITECTURES="i386;x86_64"` when configuring. + +iOS ------- -You need to run the CMake commands from the Visual Studio command -prompt, not the regular or Windows SDK one. Select the right generator -for your version with the `-G "Visual Studio X" option. +1. Get an iOS cmake toolchain File: + +You can use a pre-existing toolchain file like [ios-cmake](https://github.com/leetal/ios-cmake) or write your own. + +2. Specify the toolchain and system Name: + +- The CMAKE_TOOLCHAIN_FILE variable points to the toolchain file for iOS. +- The CMAKE_SYSTEM_NAME should be set to iOS. + +3. Example Command: + +Assuming you're using the ios-cmake toolchain, the command might look like this: + +``` +cmake -G Xcode -DCMAKE_TOOLCHAIN_FILE=path/to/ios.toolchain.cmake -DCMAKE_SYSTEM_NAME=iOS -DPLATFORM=OS64 .. +``` + +4. Build the Project: -See [the wiki] -(https://github.com/libgit2/libgit2/wiki/Building-libgit2-on-Windows) -for more detailed instructions. +After generating the project, open the .xcodeproj file in Xcode, select your iOS device or simulator as the target, and build your project. + +Android +------- + +Extract toolchain from NDK using, `make-standalone-toolchain.sh` script. +Optionally, crosscompile and install OpenSSL inside of it. Then create CMake +toolchain file that configures paths to your crosscompiler (substitute `{PATH}` +with full path to the toolchain): + + SET(CMAKE_SYSTEM_NAME Linux) + SET(CMAKE_SYSTEM_VERSION Android) + + SET(CMAKE_C_COMPILER {PATH}/bin/arm-linux-androideabi-gcc) + SET(CMAKE_CXX_COMPILER {PATH}/bin/arm-linux-androideabi-g++) + SET(CMAKE_FIND_ROOT_PATH {PATH}/sysroot/) + + SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) + SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) + SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) + +Add `-DCMAKE_TOOLCHAIN_FILE={pathToToolchainFile}` to cmake command +when configuring. + +MinGW +----- + +If you want to build the library in MinGW environment with SSH support +enabled, you may need to pass +`-DCMAKE_LIBRARY_PATH="${MINGW_PREFIX}/${MINGW_CHOST}/lib/"` flag +to CMake when configuring. This is because CMake cannot find the +Win32 libraries in MinGW folders by default and you might see an +error message stating that CMake could not resolve `ws2_32` library +during configuration. + +Another option would be to install `msys2-w32api-runtime` package before +configuring. This package installs the Win32 libraries into `/usr/lib` +folder which is by default recognized as the library path by CMake. +Please note though that this package is meant for MSYS subsystem which +is different from MinGW. Language Bindings ================================== @@ -110,43 +486,64 @@ Language Bindings Here are the bindings to libgit2 that are currently available: * C++ - * libqgit2, Qt bindings + * libqgit2, Qt bindings * Chicken Scheme * chicken-git * D - * dlibgit + * dlibgit * Delphi * GitForDelphi + * libgit2-delphi * Erlang - * Geef + * Geef * Go - * go-git + * git2go * GObject - * libgit2-glib + * libgit2-glib +* Guile + * Guile-Git * Haskell - * hgit2 + * hgit2 +* Java + * Jagged + * Git24j +* Javascript / WebAssembly ( browser and nodejs ) + * WASM-git +* Julia + * LibGit2.jl * Lua * luagit2 * .NET - * libgit2net, low level bindings * libgit2sharp * Node.js - * node-gitteh - * nodegit + * nodegit * Objective-C * objective-git * OCaml - * libgit2-ocaml + * ocaml-libgit2 * Parrot Virtual Machine * parrot-libgit2 * Perl - * git-xs-pm + * Git-Raw +* Pharo Smalltalk + * libgit2-pharo-bindings * PHP - * php-git + * php-git2 * Python * pygit2 +* R + * gert + * git2r * Ruby * Rugged +* Rust + * git2-rs +* Swift + * SwiftGit2 + * SwiftGitX + * swift-libgit2 +* Tcl + * lg2 * Vala * libgit2.vapi @@ -156,22 +553,23 @@ we can add it to the list. How Can I Contribute? ================================== -Fork libgit2/libgit2 on GitHub, add your improvement, push it to a branch -in your fork named for the topic, send a pull request. If you change the -API or make other large changes, make a note of it in docs/rel-notes/ in a -file named after the next release. - -You can also file bugs or feature requests under the libgit2 project on -GitHub, or join us on the mailing list by sending an email to: - -libgit2@librelist.com +We welcome new contributors! We have a number of issues marked as +["up for grabs"](https://github.com/libgit2/libgit2/issues?q=is%3Aissue+is%3Aopen+label%3A%22up+for+grabs%22) +and +["easy fix"](https://github.com/libgit2/libgit2/issues?utf8=✓&q=is%3Aissue+is%3Aopen+label%3A%22easy+fix%22) +that are good places to jump in and get started. There's much more detailed +information in our list of [outstanding projects](docs/projects.md). +Please be sure to check the [contribution guidelines](docs/contributing.md) +to understand our workflow, and the libgit2 +[coding conventions](docs/conventions.md). License ================================== -libgit2 is under GPL2 **with linking exemption**. This means you -can link to the library with any program, commercial, open source or -other. However, you cannot modify libgit2 and distribute it without -supplying the source. -See the COPYING file for the full license text. +`libgit2` is under GPL2 **with linking exception**. This means you can +link to and use the library from any program, proprietary or open source; +paid or gratis. However, if you modify libgit2 itself, you must distribute +the source to your modified version of libgit2. + +See the [COPYING file](COPYING) for the full license text. diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 00000000000..914e660b26d --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,14 @@ +# Security Policy + +## Supported Versions + +This project will always provide security fixes for the latest two released +versions. E.g. if the latest version is v0.28.x, then we will provide security +fixes for both v0.28.x and v0.27.y, but no earlier versions. + +## Reporting a Vulnerability + +In case you think to have found a security issue with libgit2, please do not +open a public issue. Instead, you can report the issue to the private mailing +list [security@libgit2.com](mailto:security@libgit2.com). We will acknowledge +receipt of your message in at most three days and try to clarify further steps. diff --git a/api.docurium b/api.docurium index 9e17817dbec..bf733273b8c 100644 --- a/api.docurium +++ b/api.docurium @@ -1,7 +1,7 @@ { "name": "libgit2", "github": "libgit2/libgit2", - "input": "include/git2", + "input": "include", "prefix": "git_", "output": "docs", "branch": "gh-pages", diff --git a/ci/build.sh b/ci/build.sh new file mode 100755 index 00000000000..a9b66f6613f --- /dev/null +++ b/ci/build.sh @@ -0,0 +1,95 @@ +#!/usr/bin/env bash +# +# Environment variables: +# +# SOURCE_DIR: Set to the directory of the libgit2 source (optional) +# If not set, it will be derived relative to this script. + +set -e + +SOURCE_DIR=${SOURCE_DIR:-$( cd "$( dirname "${BASH_SOURCE[0]}" )" && dirname $( pwd ) )} +BUILD_DIR=$(pwd) +BUILD_PATH=${BUILD_PATH:=$PATH} +CMAKE=$(which cmake) +CMAKE_GENERATOR=${CMAKE_GENERATOR:-Unix Makefiles} + +indent() { sed "s/^/ /"; } + +cygfullpath() { + result=$(echo "${1}" | tr \; \\n | while read -r element; do + if [ "${last}" != "" ]; then echo -n ":"; fi + echo -n $(cygpath "${element}") + last="${element}" + done) + if [ "${result}" = "" ]; then exit 1; fi + echo "${result}" +} + +if [[ "$(uname -s)" == MINGW* ]]; then + BUILD_PATH=$(cygfullpath "${BUILD_PATH}") +fi + + +echo "Source directory: ${SOURCE_DIR}" +echo "Build directory: ${BUILD_DIR}" +echo "" + +echo "Platform:" +uname -s | indent + +if [ "$(uname -s)" = "Darwin" ]; then + echo "macOS version:" + sw_vers | indent +fi + +if [ -f "/etc/debian_version" ]; then + echo "Debian version:" + (source /etc/lsb-release && echo "${DISTRIB_DESCRIPTION}") | indent +fi + +CORES=$(getconf _NPROCESSORS_ONLN || true) +echo "Number of cores: ${CORES:-(Unknown)}" + +echo "Kernel version:" +uname -a 2>&1 | indent + +echo "CMake version:" +env PATH="${BUILD_PATH}" "${CMAKE}" --version | head -1 2>&1 | indent + +if test -n "${CC}"; then + echo "Compiler version:" + "${CC}" --version 2>&1 | indent +fi +echo "Environment:" +echo "PATH=${BUILD_PATH}" | indent + +if test -n "${CC}"; then + echo "CC=${CC}" | indent +fi +if test -n "${CFLAGS}"; then + echo "CFLAGS=${CFLAGS}" | indent +fi +echo "" + +echo "##############################################################################" +echo "## Configuring build environment" +echo "##############################################################################" + +echo "${CMAKE}" -DENABLE_WERROR=ON -DBUILD_EXAMPLES=ON -DBUILD_FUZZERS=ON -DUSE_STANDALONE_FUZZERS=ON -G \"${CMAKE_GENERATOR}\" ${CMAKE_OPTIONS} -S \"${SOURCE_DIR}\" +env PATH="${BUILD_PATH}" "${CMAKE}" -DENABLE_WERROR=ON -DBUILD_EXAMPLES=ON -DBUILD_FUZZERS=ON -DUSE_STANDALONE_FUZZERS=ON -G "${CMAKE_GENERATOR}" ${CMAKE_OPTIONS} -S "${SOURCE_DIR}" + +echo "" +echo "##############################################################################" +echo "## Building libgit2" +echo "##############################################################################" + +# Determine parallelism; newer cmake supports `--build --parallel` but +# we cannot yet rely on that. +if [ "${CMAKE_GENERATOR}" = "Unix Makefiles" -a "${CORES}" != "" -a "${CMAKE_BUILD_OPTIONS}" = "" ]; then + BUILDER=(make -j ${CORES}) +else + BUILDER=("${CMAKE}" --build . ${CMAKE_BUILD_OPTIONS}) +fi + +echo "${BUILDER[@]}" +env PATH="${BUILD_PATH}" "${BUILDER[@]}" diff --git a/ci/coverity.sh b/ci/coverity.sh new file mode 100755 index 00000000000..c68b6f8ccb6 --- /dev/null +++ b/ci/coverity.sh @@ -0,0 +1,62 @@ +#!/bin/bash -e + +if test -z "$COVERITY_TOKEN" +then + echo "Need to set a coverity token" + exit 1 +fi + +case $(uname -m) in + i?86) + BITS=32;; + amd64|x86_64) + BITS=64;; + *) + echo "Unsupported arch '$(uname -m)'" + exit 1;; +esac + +SCAN_TOOL=https://scan.coverity.com/download/cxx/linux${BITS} +SOURCE_DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")"/..) +BUILD_DIR=${SOURCE_DIR}/coverity-build +TOOL_DIR=${BUILD_DIR}/coverity-tools + +# Install coverity tools +if ! test -d "$TOOL_DIR" +then + mkdir -p "$TOOL_DIR" + curl --silent --show-error --location --data "project=libgit2&token=$COVERITY_TOKEN" "$SCAN_TOOL" | + tar -xzC "$TOOL_DIR" + ln -s "$(find "$TOOL_DIR" -type d -name 'cov-analysis*')" "$TOOL_DIR"/cov-analysis +fi + +cp "${SOURCE_DIR}/script/user_nodefs.h" "$TOOL_DIR"/cov-analysis/config/ + +# Build libgit2 with Coverity +mkdir -p "$BUILD_DIR" +cd "$BUILD_DIR" +cmake "$SOURCE_DIR" +COVERITY_UNSUPPORTED=1 \ + "$TOOL_DIR/cov-analysis/bin/cov-build" --dir cov-int \ + cmake --build . + +# Upload results +tar -czf libgit2.tgz cov-int +REVISION=$(cd ${SOURCE_DIR} && git rev-parse --short HEAD) +HTML="$(curl \ + --silent --show-error \ + --write-out "\n%{http_code}" \ + --form token="$COVERITY_TOKEN" \ + --form email=libgit2@gmail.com \ + --form file=@libgit2.tgz \ + --form version="$REVISION" \ + --form description="libgit2 build" \ + https://scan.coverity.com/builds?project=libgit2)" + +# Status code is the last line +STATUS_CODE="$(echo "$HTML" | tail -n1)" +if test "${STATUS_CODE}" != 200 && test "${STATUS_CODE}" != 201 +then + echo "Received error code ${STATUS_CODE} from Coverity" + exit 1 +fi diff --git a/ci/docker/bionic b/ci/docker/bionic new file mode 100644 index 00000000000..f42c6d2aa0b --- /dev/null +++ b/ci/docker/bionic @@ -0,0 +1,57 @@ +ARG BASE=ubuntu:bionic + +FROM ${BASE} AS apt +RUN apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + clang \ + cmake \ + curl \ + gcc \ + git \ + krb5-user \ + libcurl4-openssl-dev \ + libkrb5-dev \ + libpcre3-dev \ + libssl-dev \ + libz-dev \ + ninja-build \ + openjdk-8-jre-headless \ + openssh-server \ + openssl \ + pkgconf \ + python \ + sudo \ + valgrind \ + && \ + rm -rf /var/lib/apt/lists/* + +FROM apt AS mbedtls +RUN cd /tmp && \ + curl --location --silent --show-error https://github.com/Mbed-TLS/mbedtls/archive/refs/tags/mbedtls-2.16.2.tar.gz | \ + tar -xz && \ + cd mbedtls-mbedtls-2.16.2 && \ + scripts/config.pl set MBEDTLS_MD4_C 1 && \ + CFLAGS=-fPIC cmake -G Ninja -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF -DUSE_SHARED_MBEDTLS_LIBRARY=OFF -DUSE_STATIC_MBEDTLS_LIBRARY=ON . && \ + ninja install && \ + cd .. && \ + rm -rf mbedtls-mbedtls-2.16.2 + +FROM mbedtls AS libssh2 +RUN cd /tmp && \ + curl --location --silent --show-error https://www.libssh2.org/download/libssh2-1.11.0.tar.gz | tar -xz && \ + cd libssh2-1.11.0 && \ + CFLAGS=-fPIC cmake -G Ninja -DBUILD_SHARED_LIBS=ON . && \ + ninja install && \ + cd .. && \ + rm -rf libssh2-1.11.0 + +FROM libssh2 AS adduser +ARG UID="" +ARG GID="" +RUN if [ "${UID}" != "" ]; then USER_ARG="--uid ${UID}"; fi && \ + if [ "${GID}" != "" ]; then GROUP_ARG="--gid ${GID}"; fi && \ + groupadd ${GROUP_ARG} libgit2 && \ + useradd ${USER_ARG} --gid libgit2 --shell /bin/bash --create-home libgit2 + +FROM adduser AS configure +RUN mkdir /var/run/sshd diff --git a/ci/docker/centos7 b/ci/docker/centos7 new file mode 100644 index 00000000000..45c299d10bb --- /dev/null +++ b/ci/docker/centos7 @@ -0,0 +1,60 @@ +ARG BASE=centos:7 + +FROM ${BASE} AS yum +RUN yum install -y \ + which \ + bzip2 \ + git \ + libarchive \ + gcc \ + gcc-c++ \ + make \ + openssl-devel \ + openssh-server \ + git-daemon \ + java-1.8.0-openjdk-headless \ + sudo \ + python + +FROM yum AS libssh2 +RUN cd /tmp && \ + curl --location --silent --show-error https://www.libssh2.org/download/libssh2-1.11.0.tar.gz | tar -xz && \ + cd libssh2-1.11.0 && \ + ./configure && \ + make && \ + make install && \ + cd .. && \ + rm -rf libssh-1.11.0 + +FROM libssh2 AS valgrind +RUN cd /tmp && \ + curl --insecure --location --silent --show-error https://sourceware.org/pub/valgrind/valgrind-3.15.0.tar.bz2 | \ + tar -xj && \ + cd valgrind-3.15.0 && \ + ./configure && \ + make MAKEFLAGS="-j -l$(grep -c ^processor /proc/cpuinfo)" && \ + make install && \ + cd .. && \ + rm -rf valgrind-3.15.0 + +FROM valgrind AS cmake +RUN cd /tmp && \ + curl -L https://github.com/Kitware/CMake/releases/download/v3.21.1/cmake-3.21.1.tar.gz | tar -xz && \ + cd cmake-3.21.1 && \ + ./configure && \ + make && \ + make install && \ + cd .. && \ + rm -rf cmake-3.21.1 + +FROM cmake AS adduser +ARG UID="" +ARG GID="" +RUN if [ "${UID}" != "" ]; then USER_ARG="--uid ${UID}"; fi && \ + if [ "${GID}" != "" ]; then GROUP_ARG="--gid ${GID}"; fi && \ + groupadd ${GROUP_ARG} libgit2 && \ + useradd ${USER_ARG} --gid libgit2 --shell /bin/bash --create-home libgit2 + +FROM adduser AS configure +ENV PKG_CONFIG_PATH /usr/local/lib/pkgconfig +RUN mkdir /var/run/sshd diff --git a/ci/docker/centos8 b/ci/docker/centos8 new file mode 100644 index 00000000000..c2ac5f07af3 --- /dev/null +++ b/ci/docker/centos8 @@ -0,0 +1,58 @@ +ARG BASE=centos:8 + +FROM ${BASE} AS stream +RUN dnf -y --disablerepo '*' --enablerepo=extras swap centos-linux-repos centos-stream-repos && \ + dnf -y distro-sync + +FROM stream AS yum +RUN yum install -y \ + which \ + bzip2 \ + git \ + libarchive \ + cmake \ + gcc \ + make \ + openssl-devel \ + openssh-server \ + git-daemon \ + java-1.8.0-openjdk-headless \ + sudo \ + python39 \ + krb5-workstation \ + krb5-libs + +FROM yum AS libssh2 +RUN cd /tmp && \ + curl --location --silent --show-error https://www.libssh2.org/download/libssh2-1.11.0.tar.gz | tar -xz && \ + cd libssh2-1.11.0 && \ + ./configure && \ + make && \ + make install && \ + cd .. && \ + rm -rf libssh2-1.11.0 + +FROM libssh2 AS valgrind +RUN cd /tmp && \ + curl --insecure --location --silent --show-error https://sourceware.org/pub/valgrind/valgrind-3.15.0.tar.bz2 | \ + tar -xj && \ + cd valgrind-3.15.0 && \ + ./configure && \ + make MAKEFLAGS="-j -l$(grep -c ^processor /proc/cpuinfo)" && \ + make install && \ + cd .. && \ + rm -rf valgrind-3.15.0 + +FROM valgrind AS adduser +ARG UID="" +ARG GID="" +RUN if [ "${UID}" != "" ]; then USER_ARG="--uid ${UID}"; fi && \ + if [ "${GID}" != "" ]; then GROUP_ARG="--gid ${GID}"; fi && \ + groupadd ${GROUP_ARG} libgit2 && \ + useradd ${USER_ARG} --gid libgit2 --shell /bin/bash --create-home libgit2 + +FROM adduser AS configure +ENV PKG_CONFIG_PATH /usr/local/lib/pkgconfig +RUN mkdir /var/run/sshd +RUN echo "/usr/local/lib" > /etc/ld.so.conf.d/local && \ + ldconfig diff --git a/ci/docker/docurium b/ci/docker/docurium new file mode 100644 index 00000000000..1957bbb3bf3 --- /dev/null +++ b/ci/docker/docurium @@ -0,0 +1,5 @@ +ARG BASE=ubuntu:bionic + +FROM ${BASE} +RUN apt update && apt install -y cmake pkg-config ruby ruby-dev llvm libclang-dev libssl-dev python-pygments +RUN gem install docurium diff --git a/ci/docker/fedora b/ci/docker/fedora new file mode 100644 index 00000000000..236594ff021 --- /dev/null +++ b/ci/docker/fedora @@ -0,0 +1,52 @@ +ARG BASE=fedora:rawhide + +FROM ${BASE} AS stream +RUN dnf -y distro-sync + +FROM stream AS yum +RUN yum install -y \ + which \ + bzip2 \ + git \ + libarchive \ + cmake \ + gcc \ + make \ + openssl-devel \ + openssh-server \ + git-daemon \ + java-21-openjdk-headless \ + sudo \ + python3 \ + valgrind \ + krb5-workstation \ + krb5-libs \ + krb5-devel \ + pcre2-devel \ + zlib-devel \ + ninja-build \ + llhttp-devel + +FROM yum AS libssh2 +RUN cd /tmp && \ + curl --location --silent --show-error https://www.libssh2.org/download/libssh2-1.11.0.tar.gz | tar -xz && \ + cd libssh2-1.11.0 && \ + ./configure && \ + make && \ + make install && \ + cd .. && \ + rm -rf libssh2-1.11.0 + +FROM libssh2 AS adduser +ARG UID="" +ARG GID="" +RUN if [ "${UID}" != "" ]; then USER_ARG="--uid ${UID}"; fi && \ + if [ "${GID}" != "" ]; then GROUP_ARG="--gid ${GID}"; fi && \ + groupadd ${GROUP_ARG} libgit2 && \ + useradd ${USER_ARG} --gid libgit2 --shell /bin/bash --create-home libgit2 + +FROM adduser AS configure +ENV PKG_CONFIG_PATH=/usr/local/lib/pkgconfig +RUN mkdir -p /var/run/sshd +RUN echo "/usr/local/lib" > /etc/ld.so.conf.d/local && \ + ldconfig diff --git a/ci/docker/focal b/ci/docker/focal new file mode 100644 index 00000000000..62f5b6301ae --- /dev/null +++ b/ci/docker/focal @@ -0,0 +1,85 @@ +ARG BASE=ubuntu:focal + +FROM ${BASE} AS apt +RUN apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + bzip2 \ + clang-10 \ + cmake \ + curl \ + gcc-10 \ + git \ + krb5-user \ + libcurl4-gnutls-dev \ + libgcrypt20-dev \ + libkrb5-dev \ + libpcre3-dev \ + libssl-dev \ + libz-dev \ + llvm-10 \ + make \ + ninja-build \ + openjdk-8-jre-headless \ + openssh-server \ + openssl \ + pkgconf \ + python \ + sudo \ + valgrind \ + && \ + rm -rf /var/lib/apt/lists/* && \ + mkdir /usr/local/msan + +FROM apt AS mbedtls +RUN cd /tmp && \ + curl --location --silent --show-error https://github.com/Mbed-TLS/mbedtls/archive/refs/tags/mbedtls-2.16.2.tar.gz | \ + tar -xz && \ + cd mbedtls-mbedtls-2.16.2 && \ + scripts/config.pl unset MBEDTLS_AESNI_C && \ + scripts/config.pl set MBEDTLS_MD4_C 1 && \ + mkdir build build-msan && \ + cd build && \ + CC=clang-10 CFLAGS="-fPIC" cmake -G Ninja -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF -DUSE_SHARED_MBEDTLS_LIBRARY=ON -DUSE_STATIC_MBEDTLS_LIBRARY=OFF -DCMAKE_BUILD_TYPE=Debug -DCMAKE_PREFIX_PATH=/usr/local -DCMAKE_INSTALL_PREFIX=/usr/local .. && \ + ninja install && \ + cd ../build-msan && \ + CC=clang-10 CFLAGS="-fPIC" cmake -G Ninja -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF -DUSE_SHARED_MBEDTLS_LIBRARY=ON -DUSE_STATIC_MBEDTLS_LIBRARY=OFF -DCMAKE_BUILD_TYPE=MemSanDbg -DCMAKE_INSTALL_PREFIX=/usr/local/msan .. && \ + ninja install && \ + cd .. && \ + rm -rf mbedtls-mbedtls-2.16.2 + +FROM mbedtls AS libssh2 +RUN cd /tmp && \ + curl --location --silent --show-error https://www.libssh2.org/download/libssh2-1.9.0.tar.gz | tar -xz && \ + cd libssh2-1.9.0 && \ + mkdir build build-msan && \ + cd build && \ + CC=clang-10 CFLAGS="-fPIC" cmake -G Ninja -DBUILD_SHARED_LIBS=ON -DCMAKE_PREFIX_PATH=/usr/local -DCMAKE_INSTALL_PREFIX=/usr/local .. && \ + ninja install && \ + cd ../build-msan && \ + CC=clang-10 CFLAGS="-fPIC -fsanitize=memory -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer" LDFLAGS="-fsanitize=memory" cmake -G Ninja -DBUILD_SHARED_LIBS=ON -DCRYPTO_BACKEND=mbedTLS -DCMAKE_PREFIX_PATH=/usr/local/msan -DCMAKE_INSTALL_PREFIX=/usr/local/msan .. && \ + ninja install && \ + cd .. && \ + rm -rf libssh2-1.9.0 + +FROM libssh2 AS valgrind +RUN cd /tmp && \ + curl --insecure --location --silent --show-error https://sourceware.org/pub/valgrind/valgrind-3.15.0.tar.bz2 | \ + tar -xj && \ + cd valgrind-3.15.0 && \ + CC=clang-10 ./configure && \ + make MAKEFLAGS="-j -l$(grep -c ^processor /proc/cpuinfo)" && \ + make install && \ + cd .. && \ + rm -rf valgrind-3.15.0 + +FROM valgrind AS adduser +ARG UID="" +ARG GID="" +RUN if [ "${UID}" != "" ]; then USER_ARG="--uid ${UID}"; fi && \ + if [ "${GID}" != "" ]; then GROUP_ARG="--gid ${GID}"; fi && \ + groupadd ${GROUP_ARG} libgit2 && \ + useradd ${USER_ARG} --gid libgit2 --shell /bin/bash --create-home libgit2 + + +FROM adduser AS configure +RUN mkdir /var/run/sshd diff --git a/ci/docker/noble b/ci/docker/noble new file mode 100644 index 00000000000..e7330277379 --- /dev/null +++ b/ci/docker/noble @@ -0,0 +1,90 @@ +ARG BASE=ubuntu:noble + +FROM ${BASE} AS apt +RUN apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + bzip2 \ + clang \ + clang-18 \ + cmake \ + curl \ + gcc \ + git \ + krb5-user \ + libclang-rt-18-dev \ + libcurl4-gnutls-dev \ + libgcrypt20-dev \ + libhttp-parser-dev \ + libkrb5-dev \ + libpcre3-dev \ + libssl-dev \ + libz-dev \ + llvm-18 \ + make \ + ninja-build \ + openjdk-8-jre-headless \ + openssh-server \ + openssl \ + pkgconf \ + python3 \ + sudo \ + valgrind \ + && \ + rm -rf /var/lib/apt/lists/* && \ + mkdir /usr/local/msan + +FROM apt AS mbedtls +RUN cd /tmp && \ + curl --location --silent --show-error https://github.com/Mbed-TLS/mbedtls/archive/refs/tags/mbedtls-2.28.6.tar.gz | \ + tar -xz && \ + cd mbedtls-mbedtls-2.28.6 && \ + scripts/config.pl unset MBEDTLS_AESNI_C && \ + scripts/config.pl set MBEDTLS_MD4_C 1 && \ + mkdir build build-msan && \ + cd build && \ + CC=clang-18 CFLAGS="-fPIC" cmake -G Ninja -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF -DUSE_SHARED_MBEDTLS_LIBRARY=ON -DUSE_STATIC_MBEDTLS_LIBRARY=OFF -DCMAKE_BUILD_TYPE=Debug -DCMAKE_PREFIX_PATH=/usr/local -DCMAKE_INSTALL_PREFIX=/usr/local .. && \ + ninja install && \ + cd ../build-msan && \ + CC=clang-18 CFLAGS="-fPIC" cmake -G Ninja -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF -DUSE_SHARED_MBEDTLS_LIBRARY=ON -DUSE_STATIC_MBEDTLS_LIBRARY=OFF -DCMAKE_BUILD_TYPE=MemSanDbg -DCMAKE_INSTALL_PREFIX=/usr/local/msan .. && \ + ninja install && \ + cd .. && \ + rm -rf mbedtls-mbedtls-2.28.6 + +FROM mbedtls AS libssh2 +RUN cd /tmp && \ + curl --location --silent --show-error https://www.libssh2.org/download/libssh2-1.11.0.tar.gz | tar -xz && \ + cd libssh2-1.11.0 && \ + mkdir build build-msan && \ + cd build && \ + CC=clang-18 CFLAGS="-fPIC" cmake -G Ninja -DBUILD_SHARED_LIBS=ON -DCMAKE_PREFIX_PATH=/usr/local -DCMAKE_INSTALL_PREFIX=/usr/local .. && \ + ninja install && \ + cd ../build-msan && \ + CC=clang-18 CFLAGS="-fPIC -fsanitize=memory -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer" LDFLAGS="-fsanitize=memory" cmake -G Ninja -DBUILD_SHARED_LIBS=ON -DCRYPTO_BACKEND=mbedTLS -DCMAKE_PREFIX_PATH=/usr/local/msan -DCMAKE_INSTALL_PREFIX=/usr/local/msan .. && \ + ninja install && \ + cd .. && \ + rm -rf libssh2-1.11.0 + +FROM libssh2 AS valgrind +RUN cd /tmp && \ + curl --insecure --location --silent --show-error https://sourceware.org/pub/valgrind/valgrind-3.23.0.tar.bz2 | \ + tar -xj && \ + cd valgrind-3.23.0 && \ + CC=clang-18 ./configure && \ + make MAKEFLAGS="-j -l$(grep -c ^processor /proc/cpuinfo)" && \ + make install && \ + cd .. && \ + rm -rf valgrind-3.23.0 + +FROM valgrind AS adduser +ARG UID="" +ARG GID="" +RUN if [ "${UID}" != "" ]; then USER_ARG="--uid ${UID}"; fi && \ + if [ "${GID}" != "" ]; then GROUP_ARG="--gid ${GID}"; fi && \ + groupadd ${GROUP_ARG} libgit2 && \ + useradd ${USER_ARG} --gid libgit2 --shell /bin/bash --create-home libgit2 + +FROM adduser AS ldconfig +RUN ldconfig + +FROM ldconfig AS configure +RUN mkdir /var/run/sshd diff --git a/ci/docker/xenial b/ci/docker/xenial new file mode 100644 index 00000000000..c84db419ab9 --- /dev/null +++ b/ci/docker/xenial @@ -0,0 +1,84 @@ +ARG BASE=ubuntu:xenial + +FROM ${BASE} AS apt +RUN apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + bzip2 \ + clang \ + cmake \ + curl \ + gettext \ + gcc \ + krb5-user \ + libcurl4-gnutls-dev \ + libexpat1-dev \ + libgcrypt20-dev \ + libintl-perl \ + libkrb5-dev \ + libpcre3-dev \ + libssl-dev \ + libz-dev \ + make \ + ninja-build \ + openjdk-8-jre-headless \ + openssh-server \ + openssl \ + pkgconf \ + python \ + sudo \ + valgrind \ + && \ + rm -rf /var/lib/apt/lists/* + +FROM apt AS git +RUN cd /tmp && \ + curl --location --silent --show-error https://github.com/git/git/archive/refs/tags/v2.39.1.tar.gz | \ + tar -xz && \ + cd git-2.39.1 && \ + make && \ + make prefix=/usr install && \ + cd .. && \ + rm -rf git-2.39.1 + +FROM git AS mbedtls +RUN cd /tmp && \ + curl --location --silent --show-error https://github.com/Mbed-TLS/mbedtls/archive/refs/tags/mbedtls-2.16.2.tar.gz | \ + tar -xz && \ + cd mbedtls-mbedtls-2.16.2 && \ + scripts/config.pl set MBEDTLS_MD4_C 1 && \ + CFLAGS=-fPIC cmake -G Ninja -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF -DUSE_SHARED_MBEDTLS_LIBRARY=OFF -DUSE_STATIC_MBEDTLS_LIBRARY=ON . && \ + ninja install && \ + cd .. && \ + rm -rf mbedtls-mbedtls-2.16.2 + +FROM mbedtls AS libssh2 +RUN cd /tmp && \ + curl --location --silent --show-error https://www.libssh2.org/download/libssh2-1.11.0.tar.gz | tar -xz && \ + cd libssh2-1.11.0 && \ + CFLAGS=-fPIC cmake -G Ninja -DBUILD_SHARED_LIBS=ON . && \ + ninja install && \ + cd .. && \ + rm -rf libssh2-1.11.0 + +FROM libssh2 AS valgrind +RUN cd /tmp && \ + curl --insecure --location --silent --show-error https://sourceware.org/pub/valgrind/valgrind-3.15.0.tar.bz2 | \ + tar -xj && \ + cd valgrind-3.15.0 && \ + ./configure && \ + make && \ + make install && \ + cd .. && \ + rm -rf valgrind-3.15.0 + +FROM valgrind AS adduser +ARG UID="" +ARG GID="" +RUN if [ "${UID}" != "" ]; then USER_ARG="--uid ${UID}"; fi && \ + if [ "${GID}" != "" ]; then GROUP_ARG="--gid ${GID}"; fi && \ + groupadd ${GROUP_ARG} libgit2 && \ + useradd ${USER_ARG} --gid libgit2 --shell /bin/bash --create-home libgit2 + + +FROM adduser AS configure +RUN mkdir /var/run/sshd diff --git a/ci/setup-ios-build.sh b/ci/setup-ios-build.sh new file mode 100755 index 00000000000..623b135cf24 --- /dev/null +++ b/ci/setup-ios-build.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +set -ex + +brew update +brew install ninja + +sudo mkdir /usr/local/lib || true +sudo chmod 0755 /usr/local/lib +sudo ln -s /Applications/Xcode.app/Contents/Developer/usr/lib/libLeaksAtExit.dylib /usr/local/lib + +curl -s -L https://raw.githubusercontent.com/leetal/ios-cmake/master/ios.toolchain.cmake -o ios.toolchain.cmake diff --git a/ci/setup-mingw-build.sh b/ci/setup-mingw-build.sh new file mode 100755 index 00000000000..6c444f584e2 --- /dev/null +++ b/ci/setup-mingw-build.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +set -ex + +echo "##############################################################################" +echo "## Downloading mingw" +echo "##############################################################################" + +BUILD_TEMP=${BUILD_TEMP:=$TEMP} +BUILD_TEMP=$(cygpath $BUILD_TEMP) + +case "$ARCH" in + amd64) + MINGW_URI="https://github.com/libgit2/ci-dependencies/releases/download/2023-01-23/mingw-x86_64-8.1.0-release-win32-sjlj-rt_v6-rev0.zip";; + x86) + MINGW_URI="https://github.com/libgit2/ci-dependencies/releases/download/2023-01-23/mingw-i686-8.1.0-release-win32-sjlj-rt_v6-rev0.zip";; +esac + +if [ -z "$MINGW_URI" ]; then + echo "No URL" + exit 1 +fi + +mkdir -p "$BUILD_TEMP" + +curl -s -L "$MINGW_URI" -o "$BUILD_TEMP"/mingw-"$ARCH".zip +unzip -q "$BUILD_TEMP"/mingw-"$ARCH".zip -d "$BUILD_TEMP" diff --git a/ci/setup-osx-benchmark.sh b/ci/setup-osx-benchmark.sh new file mode 100755 index 00000000000..80d87682b3e --- /dev/null +++ b/ci/setup-osx-benchmark.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +set -ex + +brew update +brew install hyperfine diff --git a/ci/setup-osx-build.sh b/ci/setup-osx-build.sh new file mode 100755 index 00000000000..5598902546d --- /dev/null +++ b/ci/setup-osx-build.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +set -ex + +brew update +brew install ninja + +sudo mkdir /usr/local/lib || true +sudo chmod 0755 /usr/local/lib +sudo ln -s /Applications/Xcode.app/Contents/Developer/usr/lib/libLeaksAtExit.dylib /usr/local/lib diff --git a/ci/setup-sanitizer-build.sh b/ci/setup-sanitizer-build.sh new file mode 100755 index 00000000000..e4591f85bec --- /dev/null +++ b/ci/setup-sanitizer-build.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +set -ex + +# Linux updated its ASLR randomization in a way that is incompatible with +# TSAN. See https://github.com/google/sanitizers/issues/1716 +sudo sysctl vm.mmap_rnd_bits=28 diff --git a/ci/setup-ubuntu-benchmark.sh b/ci/setup-ubuntu-benchmark.sh new file mode 100755 index 00000000000..8250c6c2db0 --- /dev/null +++ b/ci/setup-ubuntu-benchmark.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +set -ex + +sudo apt-get update +sudo apt-get install -y --no-install-recommends \ + cargo \ + cmake \ + gcc \ + git \ + krb5-user \ + libkrb5-dev \ + libssl-dev \ + libz-dev \ + make \ + ninja-build \ + pkgconf + +wget https://github.com/sharkdp/hyperfine/releases/download/v1.12.0/hyperfine_1.12.0_amd64.deb +sudo dpkg -i hyperfine_1.12.0_amd64.deb + +echo -n "Setting performance events availability to: " +echo -1 | sudo tee /proc/sys/kernel/perf_event_paranoid diff --git a/ci/setup-win32-benchmark.sh b/ci/setup-win32-benchmark.sh new file mode 100755 index 00000000000..0eac2f666fc --- /dev/null +++ b/ci/setup-win32-benchmark.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +set -ex + +choco install hyperfine zip + +CHOCO_PATH=$(mktemp -d) +curl -L https://github.com/ethomson/PurgeStandbyList/releases/download/v1.0/purgestandbylist.1.0.0.nupkg -o "${CHOCO_PATH}/purgestandbylist.1.0.0.nupkg" +choco install purgestandbylist -s $(cygpath -w "${CHOCO_PATH}") diff --git a/ci/setup-win32-build.sh b/ci/setup-win32-build.sh new file mode 100755 index 00000000000..a8b81e5ef6e --- /dev/null +++ b/ci/setup-win32-build.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +set -ex + +echo "##############################################################################" +echo "## Downloading libssh2" +echo "##############################################################################" + +BUILD_TEMP=${BUILD_TEMP:=$TEMP} +BUILD_TEMP=$(cygpath $BUILD_TEMP) + +case "$ARCH" in + amd64) + LIBSSH2_URI="https://github.com/libgit2/ci-dependencies/releases/download/2023-02-01/libssh2-20230201-amd64.zip";; + x86) + LIBSSH2_URI="https://github.com/libgit2/ci-dependencies/releases/download/2023-02-01-v2/libssh2-20230201-x86.zip";; +esac + +if [ -z "$LIBSSH2_URI" ]; then + echo "No URL" + exit 1 +fi + +mkdir -p "$BUILD_TEMP" + +curl -s -L "$LIBSSH2_URI" -o "$BUILD_TEMP"/libssh2-"$ARCH".zip +unzip -q "$BUILD_TEMP"/libssh2-"$ARCH".zip -d "$BUILD_TEMP" diff --git a/ci/test.sh b/ci/test.sh new file mode 100755 index 00000000000..98093c6ec5c --- /dev/null +++ b/ci/test.sh @@ -0,0 +1,492 @@ +#!/usr/bin/env bash + +set -e + +if [ -n "$SKIP_TESTS" ]; then + if [ -z "$SKIP_OFFLINE_TESTS" ]; then SKIP_OFFLINE_TESTS=1; fi + if [ -z "$SKIP_ONLINE_TESTS" ]; then SKIP_ONLINE_TESTS=1; fi + if [ -z "$SKIP_GITDAEMON_TESTS" ]; then SKIP_GITDAEMON_TESTS=1; fi + if [ -z "$SKIP_PROXY_TESTS" ]; then SKIP_PROXY_TESTS=1; fi + if [ -z "$SKIP_NTLM_TESTS" ]; then SKIP_NTLM_TESTS=1; fi + if [ -z "$SKIP_NEGOTIATE_TESTS" ]; then SKIP_NEGOTIATE_TESTS=1; fi + if [ -z "$SKIP_SSH_TESTS" ]; then SKIP_SSH_TESTS=1; fi + if [ -z "$SKIP_FUZZERS" ]; then SKIP_FUZZERS=1; fi +fi + +# Windows doesn't run the NTLM tests properly (yet) +if [[ "$(uname -s)" == MINGW* ]]; then + SKIP_NTLM_TESTS=1 +fi + +# older versions of git don't support push options +if [ -z "$SKIP_PUSHOPTIONS_TESTS" ]; then + export GITTEST_PUSH_OPTIONS=true +fi + +SOURCE_DIR=${SOURCE_DIR:-$( cd "$( dirname "${BASH_SOURCE[0]}" )" && dirname $( pwd ) )} +BUILD_DIR=$(pwd) +BUILD_PATH=${BUILD_PATH:=$PATH} +CTEST=$(which ctest) +TMPDIR=${TMPDIR:-/tmp} +USER=${USER:-$(whoami)} + +GITTEST_SSH_KEYTYPE=${GITTEST_SSH_KEYTYPE:="ecdsa"} + +HOME=`mktemp -d ${TMPDIR}/home.XXXXXXXX` +export CLAR_HOMEDIR=${HOME} + +SUCCESS=1 +CONTINUE_ON_FAILURE=0 + +should_run() { + eval "skip=\${SKIP_${1}}" + [ -z "$skip" \ + -o "$skip" == "no" -o "$skip" == "NO" \ + -o "$skip" == "n" -o "$skip" == "N" \ + -o "$skip" == "false" -o "$skip" == "FALSE" \ + -o "$skip" == "f" -o "$skip" == "F" \ + -o "$skip" == "0" ] +} + +cleanup() { + echo "Cleaning up..." + + if [ ! -z "$GIT_STANDARD_PID" ]; then + echo "Stopping git daemon (standard)..." + kill $GIT_STANDARD_PID + fi + + if [ ! -z "$GIT_NAMESPACE_PID" ]; then + echo "Stopping git daemon (namespace)..." + kill $GIT_NAMESPACE_PID + fi + + if [ ! -z "$GIT_SHA256_PID" ]; then + echo "Stopping git daemon (sha256)..." + kill $GIT_SHA256_PID + fi + + if [ ! -z "$PROXY_BASIC_PID" ]; then + echo "Stopping proxy (Basic)..." + kill $PROXY_BASIC_PID + fi + + if [ ! -z "$PROXY_NTLM_PID" ]; then + echo "Stopping proxy (NTLM)..." + kill $PROXY_NTLM_PID + fi + + if [ ! -z "$HTTP_PID" ]; then + echo "Stopping HTTP server..." + kill $HTTP_PID + fi + + if [ ! -z "$SSHD_DIR" -a -f "${SSHD_DIR}/pid" ]; then + echo "Stopping SSH server..." + kill $(cat "${SSHD_DIR}/pid") + fi + + echo "Done." +} + +run_test() { + if [[ "$GITTEST_FLAKY_RETRY" > 0 ]]; then + ATTEMPTS_REMAIN=$GITTEST_FLAKY_RETRY + else + ATTEMPTS_REMAIN=1 + fi + + FAILED=0 + while [[ "$ATTEMPTS_REMAIN" > 0 ]]; do + if [ "$FAILED" -eq 1 ]; then + echo "" + echo "Re-running flaky ${1} tests..." + echo "" + + sleep 2 + fi + + RETURN_CODE=0 + + ( + export PATH="${BUILD_PATH}" + export CLAR_SUMMARY="${BUILD_DIR}/results_${1}.xml" + "${CTEST}" -V -R "^${1}$" + ) || RETURN_CODE=$? && true + + if [ "$RETURN_CODE" -eq 0 ]; then + FAILED=0 + break + fi + + echo "Test exited with code: $RETURN_CODE" + ATTEMPTS_REMAIN="$(($ATTEMPTS_REMAIN-1))" + FAILED=1 + done + + if [ "$FAILED" -ne 0 ]; then + if [ "$CONTINUE_ON_FAILURE" -ne 1 ]; then + exit 1 + fi + + SUCCESS=0 + fi +} + +indent() { sed "s/^/ /"; } + +cygfullpath() { + result=$(echo "${1}" | tr \; \\n | while read -r element; do + if [ "${last}" != "" ]; then echo -n ":"; fi + echo -n $(cygpath "${element}") + last="${element}" + done) + if [ "${result}" = "" ]; then exit 1; fi + echo "${result}" +} + +if [[ "$(uname -s)" == MINGW* ]]; then + BUILD_PATH=$(cygfullpath "$BUILD_PATH") +fi + + +# Configure the test environment; run them early so that we're certain +# that they're started by the time we need them. + +echo "CTest version:" +env PATH="${BUILD_PATH}" "${CTEST}" --version | head -1 2>&1 | indent + +echo "" + +echo "##############################################################################" +echo "## Configuring test environment" +echo "##############################################################################" + +echo "" + +if should_run "GITDAEMON_TESTS"; then + echo "Starting git daemon (standard)..." + GIT_STANDARD_DIR=`mktemp -d ${TMPDIR}/git_standard.XXXXXXXX` + cp -R "${SOURCE_DIR}/tests/resources/pushoptions.git" "${GIT_STANDARD_DIR}/test.git" + git daemon --listen=localhost --export-all --enable=receive-pack --base-path="${GIT_STANDARD_DIR}" "${GIT_STANDARD_DIR}" 2>/dev/null & + + GIT_STANDARD_PID=$! + + echo "Starting git daemon (namespace)..." + GIT_NAMESPACE_DIR=`mktemp -d ${TMPDIR}/git_namespace.XXXXXXXX` + cp -R "${SOURCE_DIR}/tests/resources/namespace.git" "${GIT_NAMESPACE_DIR}/namespace.git" + GIT_NAMESPACE="name1" git daemon --listen=localhost --port=9419 --export-all --enable=receive-pack --base-path="${GIT_NAMESPACE_DIR}" "${GIT_NAMESPACE_DIR}" & + GIT_NAMESPACE_PID=$! + + echo "Starting git daemon (sha256)..." + GIT_SHA256_DIR=`mktemp -d ${TMPDIR}/git_sha256.XXXXXXXX` + cp -R "${SOURCE_DIR}/tests/resources/testrepo_256.git" "${GIT_SHA256_DIR}/testrepo_256.git" + git daemon --listen=localhost --port=9420 --export-all --enable=receive-pack --base-path="${GIT_SHA256_DIR}" "${GIT_SHA256_DIR}" & + GIT_SHA256_PID=$! +fi + +if should_run "PROXY_TESTS"; then + curl --location --silent --show-error https://github.com/ethomson/poxyproxy/releases/download/v0.7.0/poxyproxy-0.7.0.jar >poxyproxy.jar + + echo "Starting HTTP proxy (Basic)..." + java -jar poxyproxy.jar --address 127.0.0.1 --port 8080 --credentials foo:bar --auth-type basic --quiet & + PROXY_BASIC_PID=$! + + echo "Starting HTTP proxy (NTLM)..." + java -jar poxyproxy.jar --address 127.0.0.1 --port 8090 --credentials foo:bar --auth-type ntlm --quiet & + PROXY_NTLM_PID=$! +fi + +if should_run "NTLM_TESTS" || should_run "ONLINE_TESTS"; then + curl --location --silent --show-error https://github.com/ethomson/poxygit/releases/download/v0.6.0/poxygit-0.6.0.jar >poxygit.jar + + echo "Starting HTTP server..." + HTTP_DIR=`mktemp -d ${TMPDIR}/http.XXXXXXXX` + cp -R "${SOURCE_DIR}/tests/resources/pushoptions.git" "${HTTP_DIR}/test.git" + + java -jar poxygit.jar --address 127.0.0.1 --port 9000 --credentials foo:baz --quiet "${HTTP_DIR}" & + HTTP_PID=$! +fi + +if should_run "SSH_TESTS"; then + echo "Starting SSH server..." + SSHD_DIR=`mktemp -d ${TMPDIR}/sshd.XXXXXXXX` + cp -R "${SOURCE_DIR}/tests/resources/pushoptions.git" "${SSHD_DIR}/test.git" + + cat >"${SSHD_DIR}/sshd_config" <<-EOF + Port 2222 + ListenAddress 0.0.0.0 + Protocol 2 + HostKey ${SSHD_DIR}/id_${GITTEST_SSH_KEYTYPE} + PidFile ${SSHD_DIR}/pid + AuthorizedKeysFile ${HOME}/.ssh/authorized_keys + LogLevel DEBUG + RSAAuthentication yes + PasswordAuthentication yes + PubkeyAuthentication yes + ChallengeResponseAuthentication no + StrictModes no + HostCertificate ${SSHD_DIR}/id_${GITTEST_SSH_KEYTYPE}.pub + HostKey ${SSHD_DIR}/id_${GITTEST_SSH_KEYTYPE} + # Required here as sshd will simply close connection otherwise + UsePAM no + EOF + ssh-keygen -t "${GITTEST_SSH_KEYTYPE}" -f "${SSHD_DIR}/id_${GITTEST_SSH_KEYTYPE}" -N "" -q + /usr/sbin/sshd -f "${SSHD_DIR}/sshd_config" -E "${SSHD_DIR}/log" + + # Set up keys + mkdir "${HOME}/.ssh" + ssh-keygen -t "${GITTEST_SSH_KEYTYPE}" -f "${HOME}/.ssh/id_${GITTEST_SSH_KEYTYPE}" -N "" -q + cat "${HOME}/.ssh/id_${GITTEST_SSH_KEYTYPE}.pub" >>"${HOME}/.ssh/authorized_keys" + while read algorithm key comment; do + echo "[localhost]:2222 $algorithm $key" >>"${HOME}/.ssh/known_hosts" + done <"${SSHD_DIR}/id_${GITTEST_SSH_KEYTYPE}.pub" + + # Append the github.com keys for the tests that don't override checks. + # We ask for ssh-rsa to test that the selection based off of known_hosts + # is working. + ssh-keyscan -t ssh-rsa github.com >>"${HOME}/.ssh/known_hosts" + + # Get the fingerprint for localhost and remove the colons so we can + # parse it as a hex number. Older versions have a different output + # format. + if [[ $(ssh -V 2>&1) == OpenSSH_6* ]]; then + SSH_FINGERPRINT=$(ssh-keygen -F '[localhost]:2222' -f "${HOME}/.ssh/known_hosts" -l | tail -n 1 | cut -d ' ' -f 2 | tr -d ':') + else + SSH_FINGERPRINT=$(ssh-keygen -E md5 -F '[localhost]:2222' -f "${HOME}/.ssh/known_hosts" -l | tail -n 1 | cut -d ' ' -f 3 | cut -d : -f2- | tr -d :) + fi +fi + +# Run the tests that do not require network connectivity. + +if should_run "OFFLINE_TESTS"; then + echo "" + echo "##############################################################################" + echo "## Running core tests" + echo "##############################################################################" + + echo "" + echo "Running libgit2 integration (offline) tests" + echo "" + run_test offline + + echo "" + echo "Running utility tests" + echo "" + run_test util +fi + +if [ -n "$RUN_INVASIVE_TESTS" ]; then + echo "" + echo "Running invasive tests" + echo "" + + export GITTEST_INVASIVE_FS_SIZE=1 + export GITTEST_INVASIVE_MEMORY=1 + export GITTEST_INVASIVE_SPEED=1 + run_test invasive + unset GITTEST_INVASIVE_FS_SIZE + unset GITTEST_INVASIVE_MEMORY + unset GITTEST_INVASIVE_SPEED +fi + +# the various network tests can fail due to network connectivity problems; +# allow them to retry up to 5 times +export GITTEST_FLAKY_RETRY=5 + +if should_run "ONLINE_TESTS"; then + # Run the online tests. The "online" test suite only includes the + # default online tests that do not require additional configuration. + # The "proxy" and "ssh" test suites require further setup. + + echo "" + echo "##############################################################################" + echo "## Running networking (online) tests" + echo "##############################################################################" + + export GITTEST_REMOTE_REDIRECT_INITIAL="http://localhost:9000/initial-redirect/libgit2/TestGitRepository" + export GITTEST_REMOTE_REDIRECT_SUBSEQUENT="http://localhost:9000/subsequent-redirect/libgit2/TestGitRepository" + export GITTEST_REMOTE_SPEED_SLOW="http://localhost:9000/speed-9600/test.git" + export GITTEST_REMOTE_SPEED_TIMESOUT="http://localhost:9000/speed-0.5/test.git" + run_test online + unset GITTEST_REMOTE_REDIRECT_INITIAL + unset GITTEST_REMOTE_REDIRECT_SUBSEQUENT + unset GITTEST_REMOTE_SPEED_SLOW + unset GITTEST_REMOTE_SPEED_TIMESOUT + + # Run the online tests that immutably change global state separately + # to avoid polluting the test environment. + echo "" + echo "Running custom certificate (online_customcert) tests" + echo "" + + run_test online_customcert +fi + +if should_run "GITDAEMON_TESTS"; then + echo "" + echo "Running gitdaemon (standard) tests" + echo "" + + export GITTEST_REMOTE_URL="git://localhost/test.git" + run_test gitdaemon + unset GITTEST_REMOTE_URL + + echo "" + echo "Running gitdaemon (namespace) tests" + echo "" + + export GITTEST_REMOTE_URL="git://localhost:9419/namespace.git" + export GITTEST_REMOTE_BRANCH="four" + run_test gitdaemon_namespace + unset GITTEST_REMOTE_URL + unset GITTEST_REMOTE_BRANCH + + echo "" + echo "Running gitdaemon (sha256) tests" + echo "" + + export GITTEST_REMOTE_URL="git://localhost:9420/testrepo_256.git" + run_test gitdaemon_sha256 + unset GITTEST_REMOTE_URL +fi + +if should_run "PROXY_TESTS"; then + echo "" + echo "Running proxy tests (Basic authentication)" + echo "" + + export GITTEST_REMOTE_PROXY_HOST="localhost:8080" + export GITTEST_REMOTE_PROXY_USER="foo" + export GITTEST_REMOTE_PROXY_PASS="bar" + run_test proxy + unset GITTEST_REMOTE_PROXY_HOST + unset GITTEST_REMOTE_PROXY_USER + unset GITTEST_REMOTE_PROXY_PASS + + echo "" + echo "Running proxy tests (NTLM authentication)" + echo "" + + export GITTEST_REMOTE_PROXY_HOST="localhost:8090" + export GITTEST_REMOTE_PROXY_USER="foo" + export GITTEST_REMOTE_PROXY_PASS="bar" + run_test proxy + unset GITTEST_REMOTE_PROXY_HOST + unset GITTEST_REMOTE_PROXY_USER + unset GITTEST_REMOTE_PROXY_PASS +fi + +if should_run "NTLM_TESTS"; then + echo "" + echo "Running NTLM tests (IIS emulation)" + echo "" + + export GITTEST_REMOTE_URL="http://localhost:9000/ntlm/test.git" + export GITTEST_REMOTE_USER="foo" + export GITTEST_REMOTE_PASS="baz" + run_test auth_clone_and_push + unset GITTEST_REMOTE_URL + unset GITTEST_REMOTE_USER + unset GITTEST_REMOTE_PASS + + echo "" + echo "Running NTLM tests (Apache emulation)" + echo "" + + export GITTEST_REMOTE_URL="http://localhost:9000/broken-ntlm/test.git" + export GITTEST_REMOTE_USER="foo" + export GITTEST_REMOTE_PASS="baz" + run_test auth_clone_and_push + unset GITTEST_REMOTE_URL + unset GITTEST_REMOTE_USER + unset GITTEST_REMOTE_PASS +fi + +if should_run "NEGOTIATE_TESTS" && -n "$GITTEST_NEGOTIATE_PASSWORD" ; then + echo "" + echo "Running SPNEGO tests" + echo "" + + if [ "$(uname -s)" = "Darwin" ]; then + KINIT_FLAGS="--password-file=STDIN" + fi + + echo $GITTEST_NEGOTIATE_PASSWORD | kinit $KINIT_FLAGS test@LIBGIT2.ORG + klist -5f + + export GITTEST_REMOTE_URL="https://test.libgit2.org/kerberos/empty.git" + export GITTEST_REMOTE_DEFAULT="true" + run_test auth_clone + unset GITTEST_REMOTE_URL + unset GITTEST_REMOTE_DEFAULT + + echo "" + echo "Running SPNEGO tests (expect/continue)" + echo "" + + export GITTEST_REMOTE_URL="https://test.libgit2.org/kerberos/empty.git" + export GITTEST_REMOTE_DEFAULT="true" + export GITTEST_REMOTE_EXPECTCONTINUE="true" + run_test auth_clone + unset GITTEST_REMOTE_URL + unset GITTEST_REMOTE_DEFAULT + unset GITTEST_REMOTE_EXPECTCONTINUE + + kdestroy -A +fi + +if should_run "SSH_TESTS"; then + export GITTEST_REMOTE_USER=$USER + export GITTEST_REMOTE_SSH_KEY="${HOME}/.ssh/id_${GITTEST_SSH_KEYTYPE}" + export GITTEST_REMOTE_SSH_PUBKEY="${HOME}/.ssh/id_${GITTEST_SSH_KEYTYPE}.pub" + export GITTEST_REMOTE_SSH_PASSPHRASE="" + export GITTEST_REMOTE_SSH_FINGERPRINT="${SSH_FINGERPRINT}" + + export GITTEST_SSH_CMD="ssh -i ${HOME}/.ssh/id_${GITTEST_SSH_KEYTYPE} -o UserKnownHostsFile=${HOME}/.ssh/known_hosts" + + echo "" + echo "Running ssh tests" + echo "" + + export GITTEST_REMOTE_URL="ssh://localhost:2222/$SSHD_DIR/test.git" + run_test ssh + unset GITTEST_REMOTE_URL + + echo "" + echo "Running ssh tests (scp-style paths)" + echo "" + + export GITTEST_REMOTE_URL="[localhost:2222]:$SSHD_DIR/test.git" + run_test ssh + unset GITTEST_REMOTE_URL + + unset GITTEST_SSH_CMD + + unset GITTEST_REMOTE_USER + unset GITTEST_REMOTE_SSH_KEY + unset GITTEST_REMOTE_SSH_PUBKEY + unset GITTEST_REMOTE_SSH_PASSPHRASE + unset GITTEST_REMOTE_SSH_FINGERPRINT +fi + +unset GITTEST_FLAKY_RETRY + +if should_run "FUZZERS"; then + echo "" + echo "##############################################################################" + echo "## Running fuzzers" + echo "##############################################################################" + + env PATH="${BUILD_PATH}" "${CTEST}" -V -R 'fuzzer' +fi + +cleanup + +if [ "$SUCCESS" -ne 1 ]; then + echo "Some tests failed." + exit 1 +fi + +echo "Success." +exit 0 diff --git a/cmake/AddCFlagIfSupported.cmake b/cmake/AddCFlagIfSupported.cmake new file mode 100644 index 00000000000..685f26a00fc --- /dev/null +++ b/cmake/AddCFlagIfSupported.cmake @@ -0,0 +1,30 @@ +# - Append compiler flag to CMAKE_C_FLAGS if compiler supports it +# ADD_C_FLAG_IF_SUPPORTED() +# - the compiler flag to test +# This internally calls the CHECK_C_COMPILER_FLAG macro. + +include(CheckCCompilerFlag) + +macro(ADD_C_FLAG _FLAG) + string(TOUPPER ${_FLAG} UPCASE) + string(REGEX REPLACE "[-=]" "_" UPCASE_PRETTY ${UPCASE}) + string(REGEX REPLACE "^_+" "" UPCASE_PRETTY ${UPCASE_PRETTY}) + check_c_compiler_flag(${_FLAG} IS_${UPCASE_PRETTY}_SUPPORTED) + + if(IS_${UPCASE_PRETTY}_SUPPORTED) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_FLAG}") + else() + message(FATAL_ERROR "Required flag ${_FLAG} is not supported") + endif() +endmacro() + +macro(ADD_C_FLAG_IF_SUPPORTED _FLAG) + string(TOUPPER ${_FLAG} UPCASE) + string(REGEX REPLACE "[-=]" "_" UPCASE_PRETTY ${UPCASE}) + string(REGEX REPLACE "^_+" "" UPCASE_PRETTY ${UPCASE_PRETTY}) + check_c_compiler_flag(${_FLAG} IS_${UPCASE_PRETTY}_SUPPORTED) + + if(IS_${UPCASE_PRETTY}_SUPPORTED) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_FLAG}") + endif() +endmacro() diff --git a/cmake/AddClarTest.cmake b/cmake/AddClarTest.cmake new file mode 100644 index 00000000000..26e9273306f --- /dev/null +++ b/cmake/AddClarTest.cmake @@ -0,0 +1,9 @@ +function(ADD_CLAR_TEST project name) + if(NOT DEBUG_LEAK_CHECKER STREQUAL "OFF" AND + NOT DEBUG_LEAK_CHECKER STREQUAL "" AND + NOT DEBUG_LEAK_CHECKER STREQUAL "win32") + add_test(${name} "${PROJECT_SOURCE_DIR}/script/${DEBUG_LEAK_CHECKER}.sh" "${PROJECT_BINARY_DIR}/${project}" ${ARGN}) + else() + add_test(${name} "${PROJECT_BINARY_DIR}/${project}" ${ARGN}) + endif() +endfunction(ADD_CLAR_TEST) diff --git a/cmake/CheckPrototypeDefinitionSafe.cmake b/cmake/CheckPrototypeDefinitionSafe.cmake new file mode 100644 index 00000000000..f82603d3d72 --- /dev/null +++ b/cmake/CheckPrototypeDefinitionSafe.cmake @@ -0,0 +1,16 @@ +include(CheckPrototypeDefinition) + +function(check_prototype_definition_safe function prototype return header variable) + # temporarily save CMAKE_C_FLAGS and disable warnings about unused + # unused functions and parameters, otherwise they will always fail + # if ENABLE_WERROR is on + set(SAVED_CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") + + disable_warnings(unused-function) + disable_warnings(unused-parameter) + + check_prototype_definition("${function}" "${prototype}" "${return}" "${header}" "${variable}") + + # restore CMAKE_C_FLAGS + set(CMAKE_C_FLAGS "${SAVED_CMAKE_C_FLAGS}") +endfunction() diff --git a/cmake/DefaultCFlags.cmake b/cmake/DefaultCFlags.cmake new file mode 100644 index 00000000000..5abe0c8c247 --- /dev/null +++ b/cmake/DefaultCFlags.cmake @@ -0,0 +1,157 @@ +# Platform specific compilation flags +if(MSVC) + add_definitions(-D_SCL_SECURE_NO_WARNINGS) + add_definitions(-D_CRT_SECURE_NO_DEPRECATE) + add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE) + + string(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") + + # /GF - String pooling + # /MP - Parallel build + set(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}") + + # /Gd - explicitly set cdecl calling convention + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd") + + # Remove warnings about operands that use different enum types. + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd5287") + + if(NOT (MSVC_VERSION LESS 1900)) + # /guard:cf - Enable Control Flow Guard + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf") + endif() + + if(STATIC_CRT) + set(CRT_FLAG_DEBUG "/MTd") + set(CRT_FLAG_RELEASE "/MT") + else() + set(CRT_FLAG_DEBUG "/MDd") + set(CRT_FLAG_RELEASE "/MD") + endif() + + if(DEBUG_LEAK_CHECKER STREQUAL "win32") + set(GIT_DEBUG_LEAKCHECK_WIN32 1) + set(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}") + set(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} Dbghelp.lib") + endif() + + # /Zi - Create debugging information + # /Od - Disable optimization + # /D_DEBUG - #define _DEBUG + # /MTd - Statically link the multithreaded debug version of the CRT + # /MDd - Dynamically link the multithreaded debug version of the CRT + # /RTC1 - Run time checks + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Zi /Od /D_DEBUG /RTC1 ${CRT_FLAG_DEBUG}") + + # /DNDEBUG - Disables asserts + # /MT - Statically link the multithreaded release version of the CRT + # /MD - Dynamically link the multithreaded release version of the CRT + # /O2 - Optimize for speed + # /Oy - Enable frame pointer omission (FPO) (otherwise CMake will automatically turn it off) + # /GL - Link time code generation (whole program optimization) + # /Gy - Function-level linking + set(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}") + + # /Oy- - Disable frame pointer omission (FPO) + set(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /O2 /Oy- /GL /Gy ${CRT_FLAG_RELEASE}") + + # /O1 - Optimize for size + set(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}") + + # /IGNORE:4221 - Ignore empty compilation units + set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221") + + # /DYNAMICBASE - Address space load randomization (ASLR) + # /NXCOMPAT - Data execution prevention (DEP) + # /LARGEADDRESSAWARE - >2GB user address space on x86 + # /VERSION - Embed version information in PE header + set(CMAKE_EXE_LINKER_FLAGS "/DYNAMICBASE /NXCOMPAT /LARGEADDRESSAWARE /VERSION:${libgit2_VERSION_MAJOR}.${libgit2_VERSION_MINOR}") + + if(NOT (MSVC_VERSION LESS 1900)) + # /GUARD:CF - Enable Control Flow Guard + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF") + endif() + + # /DEBUG - Create a PDB + # /LTCG - Link time code generation (whole program optimization) + # /OPT:REF /OPT:ICF - Fold out duplicate code at link step + # /INCREMENTAL:NO - Required to use /LTCG + # /DEBUGTYPE:cv,fixup - Additional data embedded in the PDB (requires /INCREMENTAL:NO, so not on for Debug) + set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG") + set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO") + set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/DEBUG /RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:cv,fixup") + set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO") + + # Same linker settings for DLL as EXE + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") + set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}") + set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}") + set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}") + set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}") +else() + if(ENABLE_REPRODUCIBLE_BUILDS) + set(CMAKE_C_ARCHIVE_CREATE " Dqc ") + set(CMAKE_C_ARCHIVE_APPEND " Dq ") + set(CMAKE_C_ARCHIVE_FINISH " -D ") + endif() + + if(NOT BUILD_SHARED_LIBS AND LINK_WITH_STATIC_LIBRARIES) + set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") + endif() + + set(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}") + + enable_warnings(all) + enable_warnings(extra) + + if(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") + set(CMAKE_C_FLAGS "-D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}") + endif() + + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0") + + if(MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to + string(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}") + elseif(BUILD_SHARED_LIBS) + add_c_flag_IF_SUPPORTED(-fvisibility=hidden) + + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") + endif() + + if(MINGW) + # MinGW >= 3.14 uses the C99-style stdio functions + # automatically, but forks like mingw-w64 still want + # us to define this in order to use them + add_definitions(-D__USE_MINGW_ANSI_STDIO=1) + endif() + + enable_warnings(documentation) + disable_warnings(documentation-deprecated-sync) + disable_warnings(missing-field-initializers) + enable_warnings(missing-declarations) + enable_warnings(strict-aliasing) + enable_warnings(strict-prototypes) + enable_warnings(declaration-after-statement) + enable_warnings(shift-count-overflow) + enable_warnings(unused-const-variable) + enable_warnings(unused-function) + enable_warnings(int-conversion) + enable_warnings(c11-extensions) + enable_warnings(c99-c11-compat) + + # MinGW uses gcc, which expects POSIX formatting for printf, but + # uses the Windows C library, which uses its own format specifiers. + # Disable format specifier warnings. + if(MINGW) + disable_warnings(format) + disable_warnings(format-security) + else() + enable_warnings(format) + enable_warnings(format-security) + endif() +endif() + +# Ensure that MinGW provides the correct header files. +if(WIN32 AND NOT CYGWIN) + add_definitions(-DWIN32 -D_WIN32_WINNT=0x0600) +endif() diff --git a/cmake/EnableWarnings.cmake b/cmake/EnableWarnings.cmake new file mode 100644 index 00000000000..0700b521b68 --- /dev/null +++ b/cmake/EnableWarnings.cmake @@ -0,0 +1,15 @@ +macro(ENABLE_WARNINGS flag) + add_c_flag_if_supported(-W${flag}) +endmacro() + +macro(DISABLE_WARNINGS flag) + add_c_flag_if_supported(-Wno-${flag}) +endmacro() + +if(ENABLE_WERROR) + if(MSVC) + add_compile_options(-WX) + else() + add_c_flag_if_supported(-Werror) + endif() +endif() diff --git a/cmake/ExperimentalFeatures.cmake b/cmake/ExperimentalFeatures.cmake new file mode 100644 index 00000000000..7eff40bdbc2 --- /dev/null +++ b/cmake/ExperimentalFeatures.cmake @@ -0,0 +1,23 @@ +# Experimental feature support for libgit2 - developers can opt in to +# experimental functionality, like sha256 support. When experimental +# functionality is enabled, we set both a cmake flag *and* a compile +# definition. The cmake flag is used to generate `experimental.h`, +# which will be installed by a `make install`. But the compile definition +# is used by the libgit2 sources to detect the functionality at library +# build time. This allows us to have an in-tree `experimental.h` with +# *no* experiments enabled. This lets us support users who build without +# cmake and cannot generate the `experimental.h` file. + +if(EXPERIMENTAL_SHA256) + add_feature_info("SHA256 API" ON "experimental SHA256 APIs") + + set(EXPERIMENTAL 1) + set(GIT_EXPERIMENTAL_SHA256 1) + add_definitions(-DGIT_EXPERIMENTAL_SHA256=1) +else() + add_feature_info("SHA256 API" OFF "experimental SHA256 APIs") +endif() + +if(EXPERIMENTAL) + set(LIBGIT2_FILENAME "${LIBGIT2_FILENAME}-experimental") +endif() diff --git a/cmake/FindCoreFoundation.cmake b/cmake/FindCoreFoundation.cmake new file mode 100644 index 00000000000..b419ec9abd9 --- /dev/null +++ b/cmake/FindCoreFoundation.cmake @@ -0,0 +1,26 @@ +# Find CoreFoundation.framework +# This will define : +# +# COREFOUNDATION_FOUND +# COREFOUNDATION_LIBRARIES +# COREFOUNDATION_LDFLAGS +# + +find_path(COREFOUNDATION_INCLUDE_DIR NAMES CoreFoundation.h) +find_library(COREFOUNDATION_LIBRARIES NAMES CoreFoundation) +if(COREFOUNDATION_INCLUDE_DIR AND COREFOUNDATION_LIBRARIES) + if(NOT CoreFoundation_FIND_QUIETLY) + message(STATUS "Found CoreFoundation ${COREFOUNDATION_LIBRARIES}") + endif() + set(COREFOUNDATION_FOUND TRUE) + set(COREFOUNDATION_LDFLAGS "-framework CoreFoundation") +endif() + +if(CoreFoundation_FIND_REQUIRED AND NOT COREFOUNDATION_FOUND) + message(FATAL_ERROR "CoreFoundation not found") +endif() + +mark_as_advanced( + COREFOUNDATION_INCLUDE_DIR + COREFOUNDATION_LIBRARIES +) diff --git a/cmake/FindGSSAPI.cmake b/cmake/FindGSSAPI.cmake new file mode 100644 index 00000000000..a11d72a67d5 --- /dev/null +++ b/cmake/FindGSSAPI.cmake @@ -0,0 +1,208 @@ +# - Try to find GSSAPI +# Once done this will define +# +# KRB5_CONFIG - Path to krb5-config +# GSSAPI_ROOT_DIR - Set this variable to the root installation of GSSAPI +# +# Read-Only variables: +# GSSAPI_FLAVOR_MIT - set to TRUE if MIT Kerberos has been found +# GSSAPI_FLAVOR_HEIMDAL - set to TRUE if Heimdal Kerberos has been found +# GSSAPI_FOUND - system has GSSAPI +# GSSAPI_INCLUDE_DIR - the GSSAPI include directory +# GSSAPI_LIBRARIES - Link these to use GSSAPI +# GSSAPI_DEFINITIONS - Compiler switches required for using GSSAPI +# +#============================================================================= +# Copyright (c) 2013 Andreas Schneider +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# + +find_path(GSSAPI_ROOT_DIR + NAMES include/gssapi.h include/gssapi/gssapi.h + HINTS ${_GSSAPI_ROOT_HINTS} + PATHS ${_GSSAPI_ROOT_PATHS}) +mark_as_advanced(GSSAPI_ROOT_DIR) + +if(UNIX) + find_program(KRB5_CONFIG + NAMES krb5-config + PATHS ${GSSAPI_ROOT_DIR}/bin /opt/local/bin) + mark_as_advanced(KRB5_CONFIG) + + if(KRB5_CONFIG) + # Check if we have MIT KRB5 + execute_process( + COMMAND ${KRB5_CONFIG} --vendor + RESULT_VARIABLE _GSSAPI_VENDOR_RESULT + OUTPUT_VARIABLE _GSSAPI_VENDOR_STRING) + + if(_GSSAPI_VENDOR_STRING MATCHES ".*Massachusetts.*") + set(GSSAPI_FLAVOR_MIT TRUE) + else() + execute_process( + COMMAND ${KRB5_CONFIG} --libs gssapi + RESULT_VARIABLE _GSSAPI_LIBS_RESULT + OUTPUT_VARIABLE _GSSAPI_LIBS_STRING) + + if(_GSSAPI_LIBS_STRING MATCHES ".*roken.*") + set(GSSAPI_FLAVOR_HEIMDAL TRUE) + endif() + endif() + + # Get the include dir + execute_process( + COMMAND ${KRB5_CONFIG} --cflags gssapi + RESULT_VARIABLE _GSSAPI_INCLUDE_RESULT + OUTPUT_VARIABLE _GSSAPI_INCLUDE_STRING) + string(REGEX REPLACE "(\r?\n)+$" "" _GSSAPI_INCLUDE_STRING "${_GSSAPI_INCLUDE_STRING}") + string(REGEX REPLACE " *-I" "" _GSSAPI_INCLUDEDIR "${_GSSAPI_INCLUDE_STRING}") + endif() + + if(NOT GSSAPI_FLAVOR_MIT AND NOT GSSAPI_FLAVOR_HEIMDAL) + # Check for HEIMDAL + find_package(PkgConfig) + if(PKG_CONFIG_FOUND) + pkg_check_modules(_GSSAPI heimdal-gssapi) + endif() + + if(_GSSAPI_FOUND) + set(GSSAPI_FLAVOR_HEIMDAL TRUE) + else() + find_path(_GSSAPI_ROKEN + NAMES roken.h + PATHS ${GSSAPI_ROOT_DIR}/include ${_GSSAPI_INCLUDEDIR}) + if(_GSSAPI_ROKEN) + set(GSSAPI_FLAVOR_HEIMDAL TRUE) + endif() + endif() + endif() +endif() + +find_path(GSSAPI_INCLUDE_DIR + NAMES gssapi.h gssapi/gssapi.h + PATHS ${GSSAPI_ROOT_DIR}/include ${_GSSAPI_INCLUDEDIR}) + +if(GSSAPI_FLAVOR_MIT) + find_library(GSSAPI_LIBRARY + NAMES gssapi_krb5 + PATHS ${GSSAPI_ROOT_DIR}/lib ${_GSSAPI_LIBDIR}) + + find_library(KRB5_LIBRARY + NAMES krb5 + PATHS ${GSSAPI_ROOT_DIR}/lib ${_GSSAPI_LIBDIR}) + + find_library(K5CRYPTO_LIBRARY + NAMES k5crypto + PATHS ${GSSAPI_ROOT_DIR}/lib ${_GSSAPI_LIBDIR}) + + find_library(COM_ERR_LIBRARY + NAMES com_err + PATHS ${GSSAPI_ROOT_DIR}/lib ${_GSSAPI_LIBDIR}) + + if(GSSAPI_LIBRARY) + set(GSSAPI_LIBRARIES ${GSSAPI_LIBRARIES} ${GSSAPI_LIBRARY}) + endif() + + if(KRB5_LIBRARY) + set(GSSAPI_LIBRARIES ${GSSAPI_LIBRARIES} ${KRB5_LIBRARY}) + endif() + + if(K5CRYPTO_LIBRARY) + set(GSSAPI_LIBRARIES ${GSSAPI_LIBRARIES} ${K5CRYPTO_LIBRARY}) + endif() + + if(COM_ERR_LIBRARY) + set(GSSAPI_LIBRARIES ${GSSAPI_LIBRARIES} ${COM_ERR_LIBRARY}) + endif() +endif() + +if(GSSAPI_FLAVOR_HEIMDAL) + find_library(GSSAPI_LIBRARY + NAMES gssapi + PATHS ${GSSAPI_ROOT_DIR}/lib ${_GSSAPI_LIBDIR}) + + find_library(KRB5_LIBRARY + NAMES krb5 + PATHS ${GSSAPI_ROOT_DIR}/lib ${_GSSAPI_LIBDIR}) + + find_library(HCRYPTO_LIBRARY + NAMES hcrypto + PATHS ${GSSAPI_ROOT_DIR}/lib ${_GSSAPI_LIBDIR}) + + find_library(COM_ERR_LIBRARY + NAMES com_err + PATHS ${GSSAPI_ROOT_DIR}/lib ${_GSSAPI_LIBDIR}) + + find_library(HEIMNTLM_LIBRARY + NAMES heimntlm + PATHS ${GSSAPI_ROOT_DIR}/lib ${_GSSAPI_LIBDIR}) + + find_library(HX509_LIBRARY + NAMES hx509 + PATHS ${GSSAPI_ROOT_DIR}/lib ${_GSSAPI_LIBDIR}) + + find_library(ASN1_LIBRARY + NAMES asn1 + PATHS ${GSSAPI_ROOT_DIR}/lib ${_GSSAPI_LIBDIR}) + + find_library(WIND_LIBRARY + NAMES wind + PATHS ${GSSAPI_ROOT_DIR}/lib ${_GSSAPI_LIBDIR}) + + find_library(ROKEN_LIBRARY + NAMES roken + PATHS ${GSSAPI_ROOT_DIR}/lib ${_GSSAPI_LIBDIR}) + + if(GSSAPI_LIBRARY) + set(GSSAPI_LIBRARIES ${GSSAPI_LIBRARIES} ${GSSAPI_LIBRARY}) + endif() + + if(KRB5_LIBRARY) + set(GSSAPI_LIBRARIES ${GSSAPI_LIBRARIES} ${KRB5_LIBRARY}) + endif() + + if(HCRYPTO_LIBRARY) + set(GSSAPI_LIBRARIES ${GSSAPI_LIBRARIES} ${HCRYPTO_LIBRARY}) + endif() + + if(COM_ERR_LIBRARY) + set(GSSAPI_LIBRARIES ${GSSAPI_LIBRARIES} ${COM_ERR_LIBRARY}) + endif() + + if(HEIMNTLM_LIBRARY) + set(GSSAPI_LIBRARIES ${GSSAPI_LIBRARIES} ${HEIMNTLM_LIBRARY}) + endif() + + if(HX509_LIBRARY) + set(GSSAPI_LIBRARIES ${GSSAPI_LIBRARIES} ${HX509_LIBRARY}) + endif() + + if(ASN1_LIBRARY) + set(GSSAPI_LIBRARIES ${GSSAPI_LIBRARIES} ${ASN1_LIBRARY}) + endif() + + if(WIND_LIBRARY) + set(GSSAPI_LIBRARIES ${GSSAPI_LIBRARIES} ${WIND_LIBRARY}) + endif() + + if(ROKEN_LIBRARY) + set(GSSAPI_LIBRARIES ${GSSAPI_LIBRARIES} ${WIND_LIBRARY}) + endif() +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(GSSAPI DEFAULT_MSG GSSAPI_LIBRARIES GSSAPI_INCLUDE_DIR) + +if(GSSAPI_INCLUDE_DIRS AND GSSAPI_LIBRARIES) + set(GSSAPI_FOUND TRUE) +endif(GSSAPI_INCLUDE_DIRS AND GSSAPI_LIBRARIES) + +# show the GSSAPI_INCLUDE_DIRS and GSSAPI_LIBRARIES variables only in the advanced view +mark_as_advanced(GSSAPI_INCLUDE_DIRS GSSAPI_LIBRARIES) diff --git a/cmake/FindGSSFramework.cmake b/cmake/FindGSSFramework.cmake new file mode 100644 index 00000000000..1b0c936d7cc --- /dev/null +++ b/cmake/FindGSSFramework.cmake @@ -0,0 +1,28 @@ +# Find GSS.framework +# This will define : +# +# GSSFRAMEWORK_FOUND +# GSSFRAMEWORK_INCLUDE_DIR +# GSSFRAMEWORK_LIBRARIES +# GSSFRAMEWORK_LDFLAGS +# + +find_path(GSSFRAMEWORK_INCLUDE_DIR NAMES GSS.h) +find_library(GSSFRAMEWORK_LIBRARIES NAMES GSS) +if(GSSFRAMEWORK_INCLUDE_DIR AND GSSFRAMEWORK_LIBRARIES) + if(NOT CoreFoundation_FIND_QUIETLY) + message(STATUS "Found GSS.framework ${GSSFRAMEWORK_LIBRARIES}") + endif() + set(GSSFRAMEWORK_FOUND TRUE) + set(GSSFRAMEWORK_LDFLAGS "-framework GSS") +endif() + +if(GSS_FIND_REQUIRED AND NOT GSSFRAMEWORK_FOUND) + message(FATAL_ERROR "CoreFoundation not found") +endif() + +mark_as_advanced( + GSSFRAMEWORK_INCLUDE_DIR + GSSFRAMEWORK_LIBRARIES + GSSFRAMEWORK_LDFLAGS +) diff --git a/cmake/FindHTTP_Parser.cmake b/cmake/FindHTTP_Parser.cmake new file mode 100644 index 00000000000..3350190e054 --- /dev/null +++ b/cmake/FindHTTP_Parser.cmake @@ -0,0 +1,39 @@ +# - Try to find http-parser +# +# Defines the following variables: +# +# HTTP_PARSER_FOUND - system has http-parser +# HTTP_PARSER_INCLUDE_DIR - the http-parser include directory +# HTTP_PARSER_LIBRARIES - Link these to use http-parser +# HTTP_PARSER_VERSION_MAJOR - major version +# HTTP_PARSER_VERSION_MINOR - minor version +# HTTP_PARSER_VERSION_STRING - the version of http-parser found + +# Find the header and library +find_path(HTTP_PARSER_INCLUDE_DIR NAMES http_parser.h) +find_library(HTTP_PARSER_LIBRARY NAMES http_parser libhttp_parser) + +# Found the header, read version +if(HTTP_PARSER_INCLUDE_DIR AND EXISTS "${HTTP_PARSER_INCLUDE_DIR}/http_parser.h") + file(READ "${HTTP_PARSER_INCLUDE_DIR}/http_parser.h" HTTP_PARSER_H) + if(HTTP_PARSER_H) + string(REGEX REPLACE ".*#define[\t ]+HTTP_PARSER_VERSION_MAJOR[\t ]+([0-9]+).*" "\\1" HTTP_PARSER_VERSION_MAJOR "${HTTP_PARSER_H}") + string(REGEX REPLACE ".*#define[\t ]+HTTP_PARSER_VERSION_MINOR[\t ]+([0-9]+).*" "\\1" HTTP_PARSER_VERSION_MINOR "${HTTP_PARSER_H}") + set(HTTP_PARSER_VERSION_STRING "${HTTP_PARSER_VERSION_MAJOR}.${HTTP_PARSER_VERSION_MINOR}") + endif() + unset(HTTP_PARSER_H) +endif() + +# Handle the QUIETLY and REQUIRED arguments and set HTTP_PARSER_FOUND +# to TRUE if all listed variables are TRUE +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(HTTP_Parser REQUIRED_VARS HTTP_PARSER_INCLUDE_DIR HTTP_PARSER_LIBRARY) + +# Hide advanced variables +mark_as_advanced(HTTP_PARSER_INCLUDE_DIR HTTP_PARSER_LIBRARY) + +# Set standard variables +if(HTTP_PARSER_FOUND) + set(HTTP_PARSER_LIBRARIES ${HTTP_PARSER_LIBRARY}) + set(HTTP_PARSER_INCLUDE_DIRS ${HTTP_PARSER_INCLUDE_DIR}) +endif() diff --git a/cmake/FindIntlIconv.cmake b/cmake/FindIntlIconv.cmake new file mode 100644 index 00000000000..07959ca1a19 --- /dev/null +++ b/cmake/FindIntlIconv.cmake @@ -0,0 +1,51 @@ +# - Try to find Iconv +# Once done this will define +# +# ICONV_FOUND - system has Iconv +# ICONV_INCLUDE_DIR - the Iconv include directory +# ICONV_LIBRARIES - Link these to use Iconv +# + +if(ICONV_INCLUDE_DIR AND ICONV_LIBRARIES) + # Already in cache, be silent + set(ICONV_FIND_QUIETLY TRUE) +endif() + +find_path(ICONV_INCLUDE_DIR iconv.h) +check_function_exists(iconv_open libc_has_iconv) +find_library(iconv_lib NAMES iconv libiconv libiconv-2 c) + +# workaround the iOS issue where iconv is provided by libc +# We set it to false to force it add -liconv to the linker flags +if(CMAKE_SYSTEM_NAME MATCHES "iOS") + set(libc_has_iconv FALSE) +endif() + +if(ICONV_INCLUDE_DIR AND libc_has_iconv) + set(ICONV_FOUND TRUE) + set(ICONV_LIBRARIES "") + if(NOT ICONV_FIND_QUIETLY) + message(STATUS "Found Iconv: provided by libc") + endif(NOT ICONV_FIND_QUIETLY) +elseif(ICONV_INCLUDE_DIR AND iconv_lib) + set(ICONV_FOUND TRUE) + # split iconv into -L and -l linker options, so we can + # set them for pkg-config + get_filename_component(iconv_path ${iconv_lib} PATH) + get_filename_component(iconv_name ${iconv_lib} NAME_WE) + string(REGEX REPLACE "^lib" "" iconv_name ${iconv_name}) + set(ICONV_LIBRARIES "-L${iconv_path} -l${iconv_name}") + + if(NOT ICONV_FIND_QUIETLY) + message(STATUS "Found Iconv: ${ICONV_LIBRARIES}") + endif() +else() + if(Iconv_FIND_REQUIRED) + message(FATAL_ERROR "Could not find Iconv") + endif(Iconv_FIND_REQUIRED) +endif() + +mark_as_advanced( + ICONV_INCLUDE_DIR + ICONV_LIBRARIES +) diff --git a/cmake/FindLLHTTP.cmake b/cmake/FindLLHTTP.cmake new file mode 100644 index 00000000000..a87d335d048 --- /dev/null +++ b/cmake/FindLLHTTP.cmake @@ -0,0 +1,39 @@ +# - Try to find llhttp +# +# Defines the following variables: +# +# LLHTTP_FOUND - system has llhttp +# LLHTTP_INCLUDE_DIR - the llhttp include directory +# LLHTTP_LIBRARIES - Link these to use llhttp +# LLHTTP_VERSION_MAJOR - major version +# LLHTTP_VERSION_MINOR - minor version +# LLHTTP_VERSION_STRING - the version of llhttp found + +# Find the header and library +find_path(LLHTTP_INCLUDE_DIR NAMES llhttp.h) +find_library(LLHTTP_LIBRARY NAMES llhttp libllhttp) + +# Found the header, read version +if(LLHTTP_INCLUDE_DIR AND EXISTS "${LLHTTP_INCLUDE_DIR}/llhttp.h") + file(READ "${LLHTTP_INCLUDE_DIR}/llhttp.h" LLHTTP_H) + if(LLHTTP_H) + string(REGEX REPLACE ".*#define[\t ]+LLHTTP_VERSION_MAJOR[\t ]+([0-9]+).*" "\\1" LLHTTP_VERSION_MAJOR "${LLHTTP_H}") + string(REGEX REPLACE ".*#define[\t ]+LLHTTP_VERSION_MINOR[\t ]+([0-9]+).*" "\\1" LLHTTP_VERSION_MINOR "${LLHTTP_H}") + set(LLHTTP_VERSION_STRING "${LLHTTP_VERSION_MAJOR}.${LLHTTP_VERSION_MINOR}") + endif() + unset(LLHTTP_H) +endif() + +# Handle the QUIETLY and REQUIRED arguments and set LLHTTP_FOUND +# to TRUE if all listed variables are TRUE +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(LLHTTP REQUIRED_VARS LLHTTP_INCLUDE_DIR LLHTTP_LIBRARY) + +# Hide advanced variables +mark_as_advanced(LLHTTP_INCLUDE_DIR LLHTTP_LIBRARY) + +# Set standard variables +if(LLHTTP_FOUND) + set(LLHTTP_LIBRARIES ${LLHTTP_LIBRARY}) + set(LLHTTP_INCLUDE_DIRS ${LLHTTP_INCLUDE_DIR}) +endif() diff --git a/cmake/FindLibSSH2.cmake b/cmake/FindLibSSH2.cmake new file mode 100644 index 00000000000..c571997c434 --- /dev/null +++ b/cmake/FindLibSSH2.cmake @@ -0,0 +1,13 @@ +# LIBSSH2_FOUND - system has the libssh2 library +# LIBSSH2_INCLUDE_DIR - the libssh2 include directory +# LIBSSH2_LIBRARY - the libssh2 library name + +find_path(LIBSSH2_INCLUDE_DIR libssh2.h) + +find_library(LIBSSH2_LIBRARY NAMES ssh2 libssh2) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(LibSSH2 + REQUIRED_VARS LIBSSH2_LIBRARY LIBSSH2_INCLUDE_DIR) + +mark_as_advanced(LIBSSH2_INCLUDE_DIR LIBSSH2_LIBRARY) diff --git a/cmake/FindPCRE.cmake b/cmake/FindPCRE.cmake new file mode 100644 index 00000000000..02e7edce114 --- /dev/null +++ b/cmake/FindPCRE.cmake @@ -0,0 +1,37 @@ +# Copyright (C) 2007-2009 LuaDist. +# Created by Peter Kapec +# Redistribution and use of this file is allowed according to the terms of the MIT license. +# For details see the COPYRIGHT file distributed with LuaDist. +# Note: +# Searching headers and libraries is very simple and is NOT as powerful as scripts +# distributed with CMake, because LuaDist defines directories to search for. +# Everyone is encouraged to contact the author with improvements. Maybe this file +# becomes part of CMake distribution sometimes. + +# - Find pcre +# Find the native PCRE headers and libraries. +# +# PCRE_INCLUDE_DIRS - where to find pcre.h, etc. +# PCRE_LIBRARIES - List of libraries when using pcre. +# PCRE_FOUND - True if pcre found. + +# Look for the header file. +find_path(PCRE_INCLUDE_DIR NAMES pcre.h) + +# Look for the library. +find_library(PCRE_LIBRARY NAMES pcre) + +# Handle the QUIETLY and REQUIRED arguments and set PCRE_FOUND to TRUE if all listed variables are TRUE. +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(PCRE DEFAULT_MSG PCRE_LIBRARY PCRE_INCLUDE_DIR) + +# Copy the results to the output variables. +if(PCRE_FOUND) + set(PCRE_LIBRARIES ${PCRE_LIBRARY}) + set(PCRE_INCLUDE_DIRS ${PCRE_INCLUDE_DIR}) +else(PCRE_FOUND) + set(PCRE_LIBRARIES) + set(PCRE_INCLUDE_DIRS) +endif() + +mark_as_advanced(PCRE_INCLUDE_DIRS PCRE_LIBRARIES) diff --git a/cmake/FindPCRE2.cmake b/cmake/FindPCRE2.cmake new file mode 100644 index 00000000000..41c165b6587 --- /dev/null +++ b/cmake/FindPCRE2.cmake @@ -0,0 +1,37 @@ +# Copyright (C) 2007-2009 LuaDist. +# Created by Peter Kapec +# Redistribution and use of this file is allowed according to the terms of the MIT license. +# For details see the COPYRIGHT file distributed with LuaDist. +# Note: +# Searching headers and libraries is very simple and is NOT as powerful as scripts +# distributed with CMake, because LuaDist defines directories to search for. +# Everyone is encouraged to contact the author with improvements. Maybe this file +# becomes part of CMake distribution sometimes. + +# - Find pcre +# Find the native PCRE2 headers and libraries. +# +# PCRE2_INCLUDE_DIRS - where to find pcre.h, etc. +# PCRE2_LIBRARIES - List of libraries when using pcre. +# PCRE2_FOUND - True if pcre found. + +# Look for the header file. +find_path(PCRE2_INCLUDE_DIR NAMES pcre2.h) + +# Look for the library. +find_library(PCRE2_LIBRARY NAMES pcre2-8) + +# Handle the QUIETLY and REQUIRED arguments and set PCRE2_FOUND to TRUE if all listed variables are TRUE. +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(PCRE2 DEFAULT_MSG PCRE2_LIBRARY PCRE2_INCLUDE_DIR) + +# Copy the results to the output variables. +if(PCRE2_FOUND) + set(PCRE2_LIBRARIES ${PCRE2_LIBRARY}) + set(PCRE2_INCLUDE_DIRS ${PCRE2_INCLUDE_DIR}) +else(PCRE2_FOUND) + set(PCRE2_LIBRARIES) + set(PCRE2_INCLUDE_DIRS) +endif() + +mark_as_advanced(PCRE2_INCLUDE_DIRS PCRE2_LIBRARIES) diff --git a/cmake/FindPkgLibraries.cmake b/cmake/FindPkgLibraries.cmake new file mode 100644 index 00000000000..220bb2ce21a --- /dev/null +++ b/cmake/FindPkgLibraries.cmake @@ -0,0 +1,28 @@ +include(FindPkgConfig) + +# This function will find and set up a pkg-config based module. +# If a pc-file was found, it will resolve library paths to +# absolute paths. Furthermore, the function will automatically +# fall back to use static libraries in case no dynamic libraries +# were found. +function(FIND_PKGLIBRARIES prefix package) + pkg_check_modules(${prefix} ${package}) + if(NOT ${prefix}_FOUND) + return() + endif() + + foreach(LIBRARY ${${prefix}_LIBRARIES}) + find_library(${LIBRARY}_RESOLVED ${LIBRARY} PATHS ${${prefix}_LIBRARY_DIRS}) + if(${${LIBRARY}_RESOLVED} STREQUAL "${LIBRARY}_RESOLVED-NOTFOUND") + message(FATAL_ERROR "could not resolve ${LIBRARY}") + endif() + list(APPEND RESOLVED_LIBRARIES ${${LIBRARY}_RESOLVED}) + endforeach() + + set(${prefix}_FOUND 1 PARENT_SCOPE) + set(${prefix}_LIBRARIES ${RESOLVED_LIBRARIES} PARENT_SCOPE) + set(${prefix}_INCLUDE_DIRS ${${prefix}_INCLUDE_DIRS} PARENT_SCOPE) + set(${prefix}_LDFLAGS ${${prefix}_LDFLAGS} PARENT_SCOPE) + + message(STATUS " Resolved libraries: ${RESOLVED_LIBRARIES}") +endfunction() diff --git a/cmake/FindSecurity.cmake b/cmake/FindSecurity.cmake new file mode 100644 index 00000000000..14a2e2dd7bb --- /dev/null +++ b/cmake/FindSecurity.cmake @@ -0,0 +1,28 @@ +# Find Security.framework +# This will define : +# +# SECURITY_FOUND +# SECURITY_LIBRARIES +# SECURITY_LDFLAGS +# SECURITY_HAS_SSLCREATECONTEXT +# + +find_path(SECURITY_INCLUDE_DIR NAMES Security/Security.h) +find_library(SECURITY_LIBRARIES NAMES Security) +if(SECURITY_INCLUDE_DIR AND SECURITY_LIBRARIES) + if(NOT Security_FIND_QUIETLY) + message(STATUS "Found Security ${SECURITY_LIBRARIES}") + endif() + set(SECURITY_FOUND TRUE) + set(SECURITY_LDFLAGS "-framework Security") + check_library_exists("${SECURITY_LIBRARIES}" SSLCreateContext "Security/SecureTransport.h" SECURITY_HAS_SSLCREATECONTEXT) +endif() + +if(Security_FIND_REQUIRED AND NOT SECURITY_FOUND) + message(FATAL_ERROR "Security not found") +endif() + +mark_as_advanced( + SECURITY_INCLUDE_DIR + SECURITY_LIBRARIES +) diff --git a/cmake/FindStatNsec.cmake b/cmake/FindStatNsec.cmake new file mode 100644 index 00000000000..368cfedc1cc --- /dev/null +++ b/cmake/FindStatNsec.cmake @@ -0,0 +1,6 @@ +check_struct_has_member("struct stat" st_mtim "sys/types.h;sys/stat.h" + HAVE_STRUCT_STAT_ST_MTIM LANGUAGE C) +check_struct_has_member("struct stat" st_mtimespec "sys/types.h;sys/stat.h" + HAVE_STRUCT_STAT_ST_MTIMESPEC LANGUAGE C) +check_struct_has_member("struct stat" st_mtime_nsec sys/stat.h + HAVE_STRUCT_STAT_MTIME_NSEC LANGUAGE C) diff --git a/cmake/Findfutimens.cmake b/cmake/Findfutimens.cmake new file mode 100644 index 00000000000..3449c9d54e9 --- /dev/null +++ b/cmake/Findfutimens.cmake @@ -0,0 +1,14 @@ +include(EnableWarnings) + +if(APPLE) + # We cannot simply CHECK_FUNCTION_EXISTS on macOS because + # MACOSX_DEPLOYMENT_TARGET may be set to a version in the past + # that doesn't have futimens. Instead we need to enable warnings + # as errors, then check for the symbol existing in `sys/stat.h`, + # then reset warnings as errors. + enable_warnings(error) + check_symbol_exists(futimens sys/stat.h HAVE_FUTIMENS) + disable_warnings(error) +else() + check_function_exists(futimens HAVE_FUTIMENS) +endif() diff --git a/cmake/FindmbedTLS.cmake b/cmake/FindmbedTLS.cmake new file mode 100644 index 00000000000..a4a5487c296 --- /dev/null +++ b/cmake/FindmbedTLS.cmake @@ -0,0 +1,86 @@ +# - Try to find mbedTLS +# Once done this will define +# +# Read-Only variables +# MBEDTLS_FOUND - system has mbedTLS +# MBEDTLS_INCLUDE_DIR - the mbedTLS include directory +# MBEDTLS_LIBRARY_DIR - the mbedTLS library directory +# MBEDTLS_LIBRARIES - Link these to use mbedTLS +# MBEDTLS_LIBRARY - path to mbedTLS library +# MBEDX509_LIBRARY - path to mbedTLS X.509 library +# MBEDCRYPTO_LIBRARY - path to mbedTLS Crypto library +# +# Hint +# MBEDTLS_ROOT_DIR can be pointed to a local mbedTLS installation. + +set(_MBEDTLS_ROOT_HINTS + ${MBEDTLS_ROOT_DIR} + ENV MBEDTLS_ROOT_DIR) + +set(_MBEDTLS_ROOT_HINTS_AND_PATHS + HINTS ${_MBEDTLS_ROOT_HINTS} + PATHS ${_MBEDTLS_ROOT_PATHS}) + +find_path(MBEDTLS_INCLUDE_DIR + NAMES mbedtls/version.h + ${_MBEDTLS_ROOT_HINTS_AND_PATHS} + PATH_SUFFIXES include) + +if(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARIES) + # Already in cache, be silent + set(MBEDTLS_FIND_QUIETLY TRUE) +endif() + +find_library(MBEDTLS_LIBRARY + NAMES mbedtls libmbedtls + ${_MBEDTLS_ROOT_HINTS_AND_PATHS} + PATH_SUFFIXES library) +find_library(MBEDX509_LIBRARY + NAMES mbedx509 libmbedx509 + ${_MBEDTLS_ROOT_HINTS_AND_PATHS} + PATH_SUFFIXES library) +find_library(MBEDCRYPTO_LIBRARY + NAMES mbedcrypto libmbedcrypto + ${_MBEDTLS_ROOT_HINTS_AND_PATHS} + PATH_SUFFIXES library) + +if(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARY AND MBEDX509_LIBRARY AND MBEDCRYPTO_LIBRARY) + set(MBEDTLS_FOUND TRUE) +endif() + +if(MBEDTLS_FOUND) + # split mbedTLS into -L and -l linker options, so we can set them for pkg-config + get_filename_component(MBEDTLS_LIBRARY_DIR ${MBEDTLS_LIBRARY} PATH) + get_filename_component(MBEDTLS_LIBRARY_FILE ${MBEDTLS_LIBRARY} NAME_WE) + get_filename_component(MBEDX509_LIBRARY_FILE ${MBEDX509_LIBRARY} NAME_WE) + get_filename_component(MBEDCRYPTO_LIBRARY_FILE ${MBEDCRYPTO_LIBRARY} NAME_WE) + string(REGEX REPLACE "^lib" "" MBEDTLS_LIBRARY_FILE ${MBEDTLS_LIBRARY_FILE}) + string(REGEX REPLACE "^lib" "" MBEDX509_LIBRARY_FILE ${MBEDX509_LIBRARY_FILE}) + string(REGEX REPLACE "^lib" "" MBEDCRYPTO_LIBRARY_FILE ${MBEDCRYPTO_LIBRARY_FILE}) + set(MBEDTLS_LIBRARIES "-L${MBEDTLS_LIBRARY_DIR} -l${MBEDTLS_LIBRARY_FILE} -l${MBEDX509_LIBRARY_FILE} -l${MBEDCRYPTO_LIBRARY_FILE}") + + if(NOT MBEDTLS_FIND_QUIETLY) + message(STATUS "Found mbedTLS:") + file(READ ${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h MBEDTLSCONTENT) + string(REGEX MATCH "MBEDTLS_VERSION_STRING +\"[0-9|.]+\"" MBEDTLSMATCH ${MBEDTLSCONTENT}) + if(MBEDTLSMATCH) + string(REGEX REPLACE "MBEDTLS_VERSION_STRING +\"([0-9|.]+)\"" "\\1" MBEDTLS_VERSION ${MBEDTLSMATCH}) + message(STATUS " version ${MBEDTLS_VERSION}") + endif() + message(STATUS " TLS: ${MBEDTLS_LIBRARY}") + message(STATUS " X509: ${MBEDX509_LIBRARY}") + message(STATUS " Crypto: ${MBEDCRYPTO_LIBRARY}") + endif() +else(MBEDTLS_FOUND) + if(MBEDTLS_FIND_REQUIRED) + message(FATAL_ERROR "Could not find mbedTLS") + endif() +endif() + +mark_as_advanced( + MBEDTLS_INCLUDE_DIR + MBEDTLS_LIBRARY_DIR + MBEDTLS_LIBRARIES + MBEDTLS_LIBRARY + MBEDX509_LIBRARY + MBEDCRYPTO_LIBRARY) diff --git a/cmake/IdeSplitSources.cmake b/cmake/IdeSplitSources.cmake new file mode 100644 index 00000000000..4f928ac081f --- /dev/null +++ b/cmake/IdeSplitSources.cmake @@ -0,0 +1,22 @@ +# This function splits the sources files up into their appropriate +# subdirectories. This is especially useful for IDEs like Xcode and +# Visual Studio, so that you can navigate into the libgit2_tests project, +# and see the folders within the tests folder (instead of just seeing all +# source and tests in a single folder.) +function(IDE_SPLIT_SOURCES target) + if(MSVC_IDE OR CMAKE_GENERATOR STREQUAL Xcode) + get_target_property(sources ${target} SOURCES) + foreach(source ${sources}) + if(source MATCHES ".*/") + string(REPLACE ${PROJECT_SOURCE_DIR}/ "" rel ${source}) + if(rel) + string(REGEX REPLACE "/([^/]*)$" "" rel ${rel}) + if(rel) + string(REPLACE "/" "\\\\" rel ${rel}) + source_group(${rel} FILES ${source}) + endif() + endif() + endif() + endforeach() + endif() +endfunction() diff --git a/cmake/PkgBuildConfig.cmake b/cmake/PkgBuildConfig.cmake new file mode 100644 index 00000000000..c8939e63a2e --- /dev/null +++ b/cmake/PkgBuildConfig.cmake @@ -0,0 +1,77 @@ +# pkg-config file generation +# + +function(pkg_build_config) + set(options) + set(oneValueArgs NAME DESCRIPTION VERSION FILENAME LIBS_SELF) + set(multiValueArgs LIBS PRIVATE_LIBS REQUIRES CFLAGS) + + cmake_parse_arguments(PKGCONFIG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if (NOT DEFINED PKGCONFIG_FILENAME AND DEFINED PKGCONFIG_NAME) + set(PKGCONFIG_FILENAME ${PKGCONFIG_NAME}) + endif() + if (NOT DEFINED PKGCONFIG_FILENAME) + message(FATAL_ERROR "Missing FILENAME argument") + endif() + set(PKGCONFIG_FILE "${PROJECT_BINARY_DIR}/${PKGCONFIG_FILENAME}.pc") + + if (NOT DEFINED PKGCONFIG_DESCRIPTION) + message(FATAL_ERROR "Missing DESCRIPTION argument") + endif() + + if (NOT DEFINED PKGCONFIG_VERSION) + message(FATAL_ERROR "Missing VERSION argument") + endif() + + # Write .pc "header" + file(WRITE "${PKGCONFIG_FILE}" + "prefix=\"${CMAKE_INSTALL_PREFIX}\"\n" + "libdir=\"${CMAKE_INSTALL_FULL_LIBDIR}\"\n" + "includedir=\"${CMAKE_INSTALL_FULL_INCLUDEDIR}\"\n" + "\n" + "Name: ${PKGCONFIG_NAME}\n" + "Description: ${PKGCONFIG_DESCRIPTION}\n" + "Version: ${PKGCONFIG_VERSION}\n" + ) + + # Prepare Libs + if(NOT DEFINED PKGCONFIG_LIBS_SELF) + set(PKGCONFIG_LIBS_SELF "${PKGCONFIG_FILE}") + endif() + + if(NOT DEFINED PKGCONFIG_LIBS) + set(PKGCONFIG_LIBS "-l${PKGCONFIG_LIBS_SELF}") + else() + list(INSERT PKGCONFIG_LIBS 0 "-l${PKGCONFIG_LIBS_SELF}") + endif() + + list(REMOVE_DUPLICATES PKGCONFIG_LIBS) + string(REPLACE ";" " " PKGCONFIG_LIBS "${PKGCONFIG_LIBS}") + file(APPEND "${PKGCONFIG_FILE}" "Libs: -L\${libdir} ${PKGCONFIG_LIBS}\n") + + # Prepare Libs.private + if(DEFINED PKGCONFIG_PRIVATE_LIBS) + list(REMOVE_DUPLICATES PKGCONFIG_PRIVATE_LIBS) + string(REPLACE ";" " " PKGCONFIG_PRIVATE_LIBS "${PKGCONFIG_PRIVATE_LIBS}") + file(APPEND "${PKGCONFIG_FILE}" "Libs.private: ${PKGCONFIG_PRIVATE_LIBS}\n") + endif() + + # Prepare Requires.private + if(DEFINED PKGCONFIG_REQUIRES) + list(REMOVE_DUPLICATES PKGCONFIG_REQUIRES) + string(REPLACE ";" " " PKGCONFIG_REQUIRES "${PKGCONFIG_REQUIRES}") + file(APPEND "${PKGCONFIG_FILE}" "Requires.private: ${PKGCONFIG_REQUIRES}\n") + endif() + + # Prepare Cflags + if(DEFINED PKGCONFIG_CFLAGS) + string(REPLACE ";" " " PKGCONFIG_CFLAGS "${PKGCONFIG_CFLAGS}") + else() + set(PKGCONFIG_CFLAGS "") + endif() + file(APPEND "${PKGCONFIG_FILE}" "Cflags: -I\${includedir} ${PKGCONFIG_CFLAGS}\n") + + # Install .pc file + install(FILES "${PKGCONFIG_FILE}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") +endfunction() diff --git a/cmake/SanitizeInput.cmake b/cmake/SanitizeInput.cmake new file mode 100644 index 00000000000..8398d888986 --- /dev/null +++ b/cmake/SanitizeInput.cmake @@ -0,0 +1,22 @@ +function(SanitizeInput VAR) + string(TOLOWER "${${VAR}}" VALUE) + if(VALUE STREQUAL "on") + set(${VAR} "ON" PARENT_SCOPE) + elseif(VALUE STREQUAL "yes") + set(${VAR} "ON" PARENT_SCOPE) + elseif(VALUE STREQUAL "true") + set(${VAR} "ON" PARENT_SCOPE) + elseif(VALUE STREQUAL "1") + set(${VAR} "ON" PARENT_SCOPE) + elseif(VALUE STREQUAL "off") + set(${VAR} "OFF" PARENT_SCOPE) + elseif(VALUE STREQUAL "no") + set(${VAR} "OFF" PARENT_SCOPE) + elseif(VALUE STREQUAL "false") + set(${VAR} "OFF" PARENT_SCOPE) + elseif(VALUE STREQUAL "0") + set(${VAR} "OFF" PARENT_SCOPE) + else() + set(${VAR} "${VALUE}" PARENT_SCOPE) + endif() +endfunction() diff --git a/cmake/SelectAuthNTLM.cmake b/cmake/SelectAuthNTLM.cmake new file mode 100644 index 00000000000..ed48c047867 --- /dev/null +++ b/cmake/SelectAuthNTLM.cmake @@ -0,0 +1,46 @@ +include(SanitizeInput) + +if(USE_AUTH_NTLM STREQUAL "" AND NOT USE_NTLMCLIENT STREQUAL "") + sanitizeinput(USE_NTLMCLIENT) + set(USE_AUTH_NTLM "${USE_NTLMCLIENT}") +endif() + +sanitizeinput(USE_AUTH_NTLM) + +if(USE_AUTH_NTLM STREQUAL "") + set(USE_AUTH_NTLM ON) +endif() + +if(USE_AUTH_NTLM STREQUAL ON AND UNIX) + set(USE_AUTH_NTLM "builtin") +elseif(USE_AUTH_NTLM STREQUAL ON AND WIN32) + set(USE_AUTH_NTLM "sspi") +elseif(USE_AUTH_NTLM STREQUAL ON) + message(FATAL_ERROR "ntlm support was requested but no backend is available") +endif() + +if(USE_AUTH_NTLM STREQUAL "builtin") + if(NOT UNIX) + message(FATAL_ERROR "ntlm support requested via builtin provider, but builtin ntlmclient only supports posix platforms") + endif() + + add_subdirectory("${PROJECT_SOURCE_DIR}/deps/ntlmclient" "${PROJECT_BINARY_DIR}/deps/ntlmclient") + list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${PROJECT_SOURCE_DIR}/deps/ntlmclient") + list(APPEND LIBGIT2_DEPENDENCY_OBJECTS "$") + + set(GIT_AUTH_NTLM 1) + set(GIT_AUTH_NTLM_BUILTIN 1) + add_feature_info("NTLM authentication" ON "using bundled ntlmclient") +elseif(USE_AUTH_NTLM STREQUAL "sspi") + if(NOT WIN32) + message(FATAL_ERROR "SSPI is only available on Win32") + endif() + + set(GIT_AUTH_NTLM 1) + set(GIT_AUTH_NTLM_SSPI 1) + add_feature_info("NTLM authentication" ON "using Win32 SSPI") +elseif(USE_AUTH_NTLM STREQUAL OFF OR USE_AUTH_NTLM STREQUAL "") + add_feature_info("NTLM authentication" OFF "NTLM support is disabled") +else() + message(FATAL_ERROR "unknown ntlm option: ${USE_AUTH_NTLM}") +endif() diff --git a/cmake/SelectAuthNegotiate.cmake b/cmake/SelectAuthNegotiate.cmake new file mode 100644 index 00000000000..98e925af26f --- /dev/null +++ b/cmake/SelectAuthNegotiate.cmake @@ -0,0 +1,61 @@ +include(SanitizeInput) + +find_package(GSSAPI) + +if(CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "iOS") + include(FindGSSFramework) +endif() + +if(USE_AUTH_NEGOTIATE STREQUAL "" AND NOT USE_GSSAPI STREQUAL "") + sanitizeinput(USE_GSSAPI) + set(USE_AUTH_NEGOTIATE "${USE_GSSAPI}") +endif() + +sanitizeinput(USE_AUTH_NEGOTIATE) + +if((USE_AUTH_NEGOTIATE STREQUAL ON OR USE_AUTH_NEGOTIATE STREQUAL "") AND GSSFRAMEWORK_FOUND) + set(USE_AUTH_NEGOTIATE "gssframework") +elseif((USE_AUTH_NEGOTIATE STREQUAL ON OR USE_AUTH_NEGOTIATE STREQUAL "") AND GSSAPI_FOUND) + set(USE_AUTH_NEGOTIATE "gssapi") +elseif((USE_AUTH_NEGOTIATE STREQUAL ON OR USE_AUTH_NEGOTIATE STREQUAL "") AND WIN32) + set(USE_AUTH_NEGOTIATE "sspi") +elseif(USE_AUTH_NEGOTIATE STREQUAL "") + set(USE_AUTH_NEGOTIATE OFF) +elseif(USE_AUTH_NEGOTIATE STREQUAL ON) + message(FATAL_ERROR "negotiate support was requested but no backend is available") +endif() + +if(USE_AUTH_NEGOTIATE STREQUAL "gssframework") + if(NOT GSSFRAMEWORK_FOUND) + message(FATAL_ERROR "GSS.framework could not be found") + endif() + + list(APPEND LIBGIT2_SYSTEM_LIBS ${GSSFRAMEWORK_LIBRARIES}) + + set(GIT_AUTH_NEGOTIATE 1) + set(GIT_AUTH_NEGOTIATE_GSSFRAMEWORK 1) + add_feature_info("Negotiate authentication" ON "using GSS.framework") +elseif(USE_AUTH_NEGOTIATE STREQUAL "gssapi") + if(NOT GSSAPI_FOUND) + message(FATAL_ERROR "GSSAPI could not be found") + endif() + + list(APPEND LIBGIT2_SYSTEM_LIBS ${GSSAPI_LIBRARIES}) + + set(GIT_AUTH_NEGOTIATE 1) + set(GIT_AUTH_NEGOTIATE_GSSAPI 1) + add_feature_info("Negotiate authentication" ON "using GSSAPI") +elseif(USE_AUTH_NEGOTIATE STREQUAL "sspi") + if(NOT WIN32) + message(FATAL_ERROR "SSPI is only available on Win32") + endif() + + set(GIT_AUTH_NEGOTIATE 1) + set(GIT_AUTH_NEGOTIATE_SSPI 1) + add_feature_info("Negotiate authentication" ON "using Win32 SSPI") +elseif(USE_AUTH_NEGOTIATE STREQUAL OFF) + set(GIT_AUTH_NEGOTIATE 0) + add_feature_info("Negotiate authentication" OFF "SPNEGO support is disabled") +else() + message(FATAL_ERROR "unknown negotiate option: ${USE_AUTH_NEGOTIATE}") +endif() diff --git a/cmake/SelectCompression.cmake b/cmake/SelectCompression.cmake new file mode 100644 index 00000000000..d0a4b566476 --- /dev/null +++ b/cmake/SelectCompression.cmake @@ -0,0 +1,55 @@ +include(SanitizeInput) + +# Fall back to the previous cmake configuration, "USE_BUNDLED_ZLIB" +if(NOT USE_COMPRESSION AND USE_BUNDLED_ZLIB) + SanitizeInput(USE_BUNDLED_ZLIB) + + if(USE_BUNDLED_ZLIB STREQUAL ON) + set(USE_COMPRESSION "builtin") + elseif(USE_BUNDLED_ZLIB STREQUAL OFF) + set(USE_COMPRESSION "zlib") + else() + message(FATAL_ERROR "unknown setting to USE_BUNDLED_ZLIB: ${USE_BUNDLED_ZLIB}") + endif() +endif() + +SanitizeInput(USE_COMPRESSION) + +if(NOT USE_COMPRESSION) + find_package(ZLIB) + + if(ZLIB_FOUND) + set(GIT_COMPRESSION_ZLIB 1) + else() + message(STATUS "zlib was not found; using bundled 3rd-party sources." ) + set(GIT_COMPRESSION_BUILTIN 1) + endif() +elseif(USE_COMPRESSION STREQUAL "zlib") + find_package(ZLIB) + + if(NOT ZLIB_FOUND) + message(FATAL_ERROR "system zlib was requested but not found") + endif() + + set(GIT_COMPRESSION_ZLIB 1) +elseif(USE_COMPRESSION STREQUAL "builtin") + set(GIT_COMPRESSION_BUILTIN 1) +endif() + +if(GIT_COMPRESSION_ZLIB) + list(APPEND LIBGIT2_SYSTEM_INCLUDES ${ZLIB_INCLUDE_DIRS}) + list(APPEND LIBGIT2_SYSTEM_LIBS ${ZLIB_LIBRARIES}) + if(APPLE OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD") + list(APPEND LIBGIT2_PC_LIBS "-lz") + else() + list(APPEND LIBGIT2_PC_REQUIRES "zlib") + endif() + add_feature_info("Compression" ON "using system zlib") +elseif(GIT_COMPRESSION_BUILTIN) + add_subdirectory("${PROJECT_SOURCE_DIR}/deps/zlib" "${PROJECT_BINARY_DIR}/deps/zlib") + list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${PROJECT_SOURCE_DIR}/deps/zlib") + list(APPEND LIBGIT2_DEPENDENCY_OBJECTS $) + add_feature_info("Compression" ON "using bundled zlib") +else() + message(FATAL_ERROR "unknown compression backend") +endif() diff --git a/cmake/SelectHTTPParser.cmake b/cmake/SelectHTTPParser.cmake new file mode 100644 index 00000000000..00138c20da1 --- /dev/null +++ b/cmake/SelectHTTPParser.cmake @@ -0,0 +1,40 @@ +include(SanitizeInput) + +sanitizeinput(USE_HTTP_PARSER) + +# Optional external dependency: http-parser +if(USE_HTTP_PARSER STREQUAL "http-parser" OR + USE_HTTP_PARSER STREQUAL "httpparser" OR + USE_HTTP_PARSER STREQUAL "system") + find_package(HTTP_Parser) + + if(HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2) + list(APPEND LIBGIT2_SYSTEM_INCLUDES ${HTTP_PARSER_INCLUDE_DIRS}) + list(APPEND LIBGIT2_SYSTEM_LIBS ${HTTP_PARSER_LIBRARIES}) + list(APPEND LIBGIT2_PC_LIBS "-lhttp_parser") + set(GIT_HTTPPARSER_HTTPPARSER 1) + add_feature_info("HTTP Parser" ON "using system http-parser") + else() + message(FATAL_ERROR "http-parser support was requested but not found") + endif() +elseif(USE_HTTP_PARSER STREQUAL "llhttp") + find_package(LLHTTP) + + if(LLHTTP_FOUND AND LLHTTP_VERSION_MAJOR EQUAL 9) + list(APPEND LIBGIT2_SYSTEM_INCLUDES ${LLHTTP_INCLUDE_DIRS}) + list(APPEND LIBGIT2_SYSTEM_LIBS ${LLHTTP_LIBRARIES}) + list(APPEND LIBGIT2_PC_LIBS "-lllhttp") + set(GIT_HTTPPARSER_LLHTTP 1) + add_feature_info("HTTP Parser" ON "using system llhttp") + else() + message(FATAL_ERROR "llhttp support was requested but not found") + endif() +elseif(USE_HTTP_PARSER STREQUAL "" OR USE_HTTP_PARSER STREQUAL "builtin") + add_subdirectory("${PROJECT_SOURCE_DIR}/deps/llhttp" "${PROJECT_BINARY_DIR}/deps/llhttp") + list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${PROJECT_SOURCE_DIR}/deps/llhttp") + list(APPEND LIBGIT2_DEPENDENCY_OBJECTS "$") + set(GIT_HTTPPARSER_BUILTIN 1) + add_feature_info("HTTP Parser" ON "using bundled parser") +else() + message(FATAL_ERROR "unknown http-parser: ${USE_HTTP_PARSER}") +endif() diff --git a/cmake/SelectHTTPSBackend.cmake b/cmake/SelectHTTPSBackend.cmake new file mode 100644 index 00000000000..a264945be45 --- /dev/null +++ b/cmake/SelectHTTPSBackend.cmake @@ -0,0 +1,167 @@ +include(SanitizeInput) + +# We try to find any packages our backends might use +find_package(OpenSSL) +find_package(mbedTLS) + +if(CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "iOS") + find_package(Security) + find_package(CoreFoundation) +endif() + +if(USE_HTTPS STREQUAL "") + set(USE_HTTPS ON) +endif() + +sanitizeinput(USE_HTTPS) + +if(USE_HTTPS) + # Auto-select TLS backend + if(USE_HTTPS STREQUAL ON) + if(SECURITY_FOUND) + if(SECURITY_HAS_SSLCREATECONTEXT) + set(USE_HTTPS "securetransport") + else() + message(STATUS "Security framework is too old, falling back to OpenSSL") + set(USE_HTTPS "OpenSSL") + endif() + elseif(WIN32) + set(USE_HTTPS "winhttp") + elseif(OPENSSL_FOUND) + set(USE_HTTPS "openssl") + elseif(MBEDTLS_FOUND) + set(USE_HTTPS "mbedtls") + else() + message(FATAL_ERROR "Unable to autodetect a usable HTTPS backend." + "Please pass the backend name explicitly (-DUSE_HTTPS=backend)") + endif() + endif() + + # Check that we can find what's required for the selected backend + if(USE_HTTPS STREQUAL "securetransport") + if(NOT COREFOUNDATION_FOUND) + message(FATAL_ERROR "Cannot use SecureTransport backend, CoreFoundation.framework not found") + endif() + if(NOT SECURITY_FOUND) + message(FATAL_ERROR "Cannot use SecureTransport backend, Security.framework not found") + endif() + if(NOT SECURITY_HAS_SSLCREATECONTEXT) + message(FATAL_ERROR "Cannot use SecureTransport backend, SSLCreateContext not supported") + endif() + + list(APPEND LIBGIT2_SYSTEM_INCLUDES ${SECURITY_INCLUDE_DIR}) + list(APPEND LIBGIT2_SYSTEM_LIBS ${COREFOUNDATION_LDFLAGS} ${SECURITY_LDFLAGS}) + list(APPEND LIBGIT2_PC_LIBS ${COREFOUNDATION_LDFLAGS} ${SECURITY_LDFLAGS}) + + set(GIT_HTTPS_SECURETRANSPORT 1) + add_feature_info("HTTPS" ON "using SecureTransport") + elseif(USE_HTTPS STREQUAL "openssl") + if(NOT OPENSSL_FOUND) + message(FATAL_ERROR "Asked for OpenSSL TLS backend, but it wasn't found") + endif() + + list(APPEND LIBGIT2_SYSTEM_INCLUDES ${OPENSSL_INCLUDE_DIR}) + list(APPEND LIBGIT2_SYSTEM_LIBS ${OPENSSL_LIBRARIES}) + + # Static OpenSSL (lib crypto.a) requires libdl, include it explicitly + if(LINK_WITH_STATIC_LIBRARIES STREQUAL ON) + list(APPEND LIBGIT2_SYSTEM_LIBS ${CMAKE_DL_LIBS}) + endif() + + list(APPEND LIBGIT2_PC_LIBS ${OPENSSL_LDFLAGS}) + list(APPEND LIBGIT2_PC_REQUIRES "openssl") + + set(GIT_HTTPS_OPENSSL 1) + add_feature_info("HTTPS" ON "using OpenSSL") + elseif(USE_HTTPS STREQUAL "mbedtls") + if(NOT MBEDTLS_FOUND) + message(FATAL_ERROR "Asked for mbedTLS backend, but it wasn't found") + endif() + + if(NOT CERT_LOCATION) + message(STATUS "Auto-detecting default certificates location") + if(EXISTS "/usr/local/opt/openssl/bin/openssl") + # Check for an Homebrew installation + set(OPENSSL_CMD "/usr/local/opt/openssl/bin/openssl") + else() + set(OPENSSL_CMD "openssl") + endif() + execute_process(COMMAND ${OPENSSL_CMD} version -d OUTPUT_VARIABLE OPENSSL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE) + if(OPENSSL_DIR) + string(REGEX REPLACE "^OPENSSLDIR: \"(.*)\"$" "\\1/" OPENSSL_DIR ${OPENSSL_DIR}) + + set(OPENSSL_CA_LOCATIONS + "ca-bundle.pem" # OpenSUSE Leap 42.1 + "cert.pem" # Ubuntu 14.04, FreeBSD + "certs/ca-certificates.crt" # Ubuntu 16.04 + "certs/ca.pem" # Debian 7 + ) + foreach(SUFFIX IN LISTS OPENSSL_CA_LOCATIONS) + set(LOC "${OPENSSL_DIR}${SUFFIX}") + if(NOT CERT_LOCATION AND EXISTS "${OPENSSL_DIR}${SUFFIX}") + set(CERT_LOCATION ${LOC}) + endif() + endforeach() + else() + message(FATAL_ERROR "Unable to find OpenSSL executable. Please provide default certificate location via CERT_LOCATION") + endif() + endif() + + if(CERT_LOCATION) + if(NOT EXISTS ${CERT_LOCATION}) + message(FATAL_ERROR "cannot use CERT_LOCATION=${CERT_LOCATION} as it doesn't exist") + endif() + add_definitions(-DGIT_DEFAULT_CERT_LOCATION="${CERT_LOCATION}") + endif() + + list(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR}) + list(APPEND LIBGIT2_SYSTEM_LIBS ${MBEDTLS_LIBRARIES}) + # mbedTLS has no pkgconfig file, hence we can't require it + # https://github.com/ARMmbed/mbedtls/issues/228 + # For now, pass its link flags as our own + list(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES}) + + set(GIT_HTTPS_MBEDTLS 1) + + if(CERT_LOCATION) + add_feature_info("HTTPS" ON "using mbedTLS (certificate location: ${CERT_LOCATION})") + else() + add_feature_info("HTTPS" ON "using mbedTLS") + endif() + elseif(USE_HTTPS STREQUAL "schannel") + list(APPEND LIBGIT2_SYSTEM_LIBS "rpcrt4" "crypt32" "ole32") + list(APPEND LIBGIT2_PC_LIBS "-lrpcrt4" "-lcrypt32" "-lole32") + + set(GIT_HTTPS_SCHANNEL 1) + add_feature_info("HTTPS" ON "using Schannel") + elseif(USE_HTTPS STREQUAL "winhttp") + # Since MinGW does not come with headers or an import library for winhttp, + # we have to include a private header and generate our own import library + if(MINGW) + add_subdirectory("${PROJECT_SOURCE_DIR}/deps/winhttp" "${PROJECT_BINARY_DIR}/deps/winhttp") + list(APPEND LIBGIT2_SYSTEM_LIBS winhttp) + list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${PROJECT_SOURCE_DIR}/deps/winhttp") + else() + list(APPEND LIBGIT2_SYSTEM_LIBS "winhttp") + list(APPEND LIBGIT2_PC_LIBS "-lwinhttp") + endif() + + list(APPEND LIBGIT2_SYSTEM_LIBS "rpcrt4" "crypt32" "ole32") + list(APPEND LIBGIT2_PC_LIBS "-lrpcrt4" "-lcrypt32" "-lole32") + + set(GIT_HTTPS_WINHTTP 1) + add_feature_info("HTTPS" ON "using WinHTTP") + elseif(USE_HTTPS STREQUAL "openssl-dynamic") + list(APPEND LIBGIT2_SYSTEM_LIBS dl) + + set(GIT_HTTPS_OPENSSL_DYNAMIC 1) + add_feature_info("HTTPS" ON "using OpenSSL-Dynamic") + else() + message(FATAL_ERROR "unknown HTTPS backend: ${USE_HTTPS}") + endif() + + set(GIT_HTTPS 1) +else() + set(GIT_HTTPS 0) + add_feature_info("HTTPS" NO "HTTPS support is disabled") +endif() diff --git a/cmake/SelectHashes.cmake b/cmake/SelectHashes.cmake new file mode 100644 index 00000000000..cf229666a4a --- /dev/null +++ b/cmake/SelectHashes.cmake @@ -0,0 +1,127 @@ +# Select a hash backend + +include(SanitizeInput) + +sanitizeinput(USE_HTTPS) +sanitizeinput(USE_SHA1) +sanitizeinput(USE_SHA256) + +# sha1 + +if(USE_SHA1 STREQUAL "" OR + USE_SHA1 STREQUAL ON OR + USE_SHA1 STREQUAL "collisiondetection") + SET(USE_SHA1 "builtin") +elseif(USE_SHA1 STREQUAL "https") + if(USE_HTTPS STREQUAL "securetransport") + set(USE_SHA1 "commoncrypto") + elseif(USE_HTTPS STREQUAL "schannel") + set(USE_SHA1 "win32") + elseif(USE_HTTPS STREQUAL "winhttp") + set(USE_SHA1 "win32") + elseif(USE_HTTPS) + set(USE_SHA1 ${USE_HTTPS}) + else() + message(FATAL_ERROR "asked for HTTPS SHA1 backend but HTTPS is not enabled") + endif() +endif() + +if(USE_SHA1 STREQUAL "builtin") + set(GIT_SHA1_BUILTIN 1) + add_feature_info(SHA1 ON "using bundled collision detection implementation") +elseif(USE_SHA1 STREQUAL "openssl") + set(GIT_SHA1_OPENSSL 1) + add_feature_info(SHA1 ON "using OpenSSL") +elseif(USE_SHA1 STREQUAL "openssl-fips") + set(GIT_SHA1_OPENSSL_FIPS 1) + add_feature_info(SHA1 ON "using OpenSSL-FIPS") +elseif(USE_SHA1 STREQUAL "openssl-dynamic") + list(APPEND LIBGIT2_SYSTEM_LIBS dl) + set(GIT_SHA1_OPENSSL_DYNAMIC 1) + add_feature_info(SHA1 ON "using OpenSSL-Dynamic") +elseif(USE_SHA1 STREQUAL "commoncrypto") + set(GIT_SHA1_COMMON_CRYPTO 1) + add_feature_info(SHA1 ON "using CommonCrypto") +elseif(USE_SHA1 STREQUAL "mbedtls") + set(GIT_SHA1_MBEDTLS 1) + add_feature_info(SHA1 ON "using mbedTLS") +elseif(USE_SHA1 STREQUAL "win32") + set(GIT_SHA1_WIN32 1) + add_feature_info(SHA1 ON "using Win32 APIs") +else() + message(FATAL_ERROR "asked for unknown SHA1 backend: ${USE_SHA1}") +endif() + +# sha256 + +if(USE_SHA256 STREQUAL "" OR USE_SHA256 STREQUAL ON) + if(USE_HTTPS) + SET(USE_SHA256 "https") + else() + SET(USE_SHA256 "builtin") + endif() +endif() + +if(USE_SHA256 STREQUAL "https") + if(USE_HTTPS STREQUAL "securetransport") + set(USE_SHA256 "commoncrypto") + elseif(USE_HTTPS STREQUAL "schannel") + set(USE_SHA256 "win32") + elseif(USE_HTTPS STREQUAL "winhttp") + set(USE_SHA256 "win32") + elseif(USE_HTTPS) + set(USE_SHA256 ${USE_HTTPS}) + endif() +endif() + +if(USE_SHA256 STREQUAL "builtin") + set(GIT_SHA256_BUILTIN 1) + add_feature_info(SHA256 ON "using bundled implementation") +elseif(USE_SHA256 STREQUAL "openssl") + set(GIT_SHA256_OPENSSL 1) + add_feature_info(SHA256 ON "using OpenSSL") +elseif(USE_SHA256 STREQUAL "openssl-fips") + set(GIT_SHA256_OPENSSL_FIPS 1) + add_feature_info(SHA256 ON "using OpenSSL-FIPS") +elseif(USE_SHA256 STREQUAL "openssl-dynamic") + list(APPEND LIBGIT2_SYSTEM_LIBS dl) + set(GIT_SHA256_OPENSSL_DYNAMIC 1) + add_feature_info(SHA256 ON "using OpenSSL-Dynamic") +elseif(USE_SHA256 STREQUAL "commoncrypto") + set(GIT_SHA256_COMMON_CRYPTO 1) + add_feature_info(SHA256 ON "using CommonCrypto") +elseif(USE_SHA256 STREQUAL "mbedtls") + set(GIT_SHA256_MBEDTLS 1) + add_feature_info(SHA256 ON "using mbedTLS") +elseif(USE_SHA256 STREQUAL "win32") + set(GIT_SHA256_WIN32 1) + add_feature_info(SHA256 ON "using Win32 APIs") +else() + message(FATAL_ERROR "asked for unknown SHA256 backend: ${USE_SHA256}") +endif() + +# add library requirements +if(USE_SHA1 STREQUAL "openssl" OR USE_SHA256 STREQUAL "openssl" OR + USE_SHA1 STREQUAL "openssl-fips" OR USE_SHA256 STREQUAL "openssl-fips") + if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") + list(APPEND LIBGIT2_PC_LIBS "-lssl") + else() + list(APPEND LIBGIT2_PC_REQUIRES "openssl") + endif() +endif() + +if(USE_SHA1 STREQUAL "mbedtls" OR USE_SHA256 STREQUAL "mbedtls") + list(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR}) + list(APPEND LIBGIT2_SYSTEM_LIBS ${MBEDTLS_LIBRARIES}) + # mbedTLS has no pkgconfig file, hence we can't require it + # https://github.com/ARMmbed/mbedtls/issues/228 + # For now, pass its link flags as our own + list(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES}) +endif() + +# warn for users who do not use sha1dc + +if(NOT "${USE_SHA1}" STREQUAL "builtin") + list(APPEND WARNINGS "SHA1 support is set to ${USE_SHA1} which is not recommended - git's hash algorithm is sha1dc, it is *not* SHA1. Using SHA1 may leave you and your users susceptible to SHAttered-style attacks.") + set(WARNINGS ${WARNINGS} PARENT_SCOPE) +endif() diff --git a/cmake/SelectI18n.cmake b/cmake/SelectI18n.cmake new file mode 100644 index 00000000000..f95e1244135 --- /dev/null +++ b/cmake/SelectI18n.cmake @@ -0,0 +1,40 @@ +include(SanitizeInput) + +find_package(IntlIconv) + +if(USE_I18N STREQUAL "" AND NOT USE_ICONV STREQUAL "") + sanitizeinput(USE_ICONV) + set(USE_I18N "${USE_ICONV}") +endif() + +if(USE_I18N STREQUAL "") + set(USE_I18N ON) +endif() + +sanitizeinput(USE_I18N) + +if(USE_I18N) + if(USE_I18N STREQUAL ON) + if(ICONV_FOUND) + set(USE_I18N "iconv") + else() + message(FATAL_ERROR "Unable to detect internationalization support") + endif() + endif() + + if(USE_I18N STREQUAL "iconv") + else() + message(FATAL_ERROR "unknown internationalization backend: ${USE_I18N}") + endif() + + list(APPEND LIBGIT2_SYSTEM_INCLUDES ${ICONV_INCLUDE_DIR}) + list(APPEND LIBGIT2_SYSTEM_LIBS ${ICONV_LIBRARIES}) + list(APPEND LIBGIT2_PC_LIBS ${ICONV_LIBRARIES}) + + set(GIT_I18N 1) + set(GIT_I18N_ICONV 1) + add_feature_info("Internationalization" ON "using ${USE_I18N}") +else() + set(GIT_I18N 0) + add_feature_info("Internationalization" OFF "internationalization support is disabled") +endif() diff --git a/cmake/SelectNsec.cmake b/cmake/SelectNsec.cmake new file mode 100644 index 00000000000..59606a47f81 --- /dev/null +++ b/cmake/SelectNsec.cmake @@ -0,0 +1,58 @@ +include(SanitizeInput) +include(FeatureSummary) + +sanitizeinput(USE_NSEC) + +if((USE_NSEC STREQUAL ON OR USE_NSEC STREQUAL "") AND HAVE_STRUCT_STAT_ST_MTIM) + set(USE_NSEC "mtim") +elseif((USE_NSEC STREQUAL ON OR USE_NSEC STREQUAL "") AND HAVE_STRUCT_STAT_ST_MTIMESPEC) + set(USE_NSEC "mtimespec") +elseif((USE_NSEC STREQUAL ON OR USE_NSEC STREQUAL "") AND HAVE_STRUCT_STAT_MTIME_NSEC) + set(USE_NSEC "mtime_nsec") +elseif((USE_NSEC STREQUAL ON OR USE_NSEC STREQUAL "") AND WIN32) + set(USE_NSEC "win32") +elseif(USE_NSEC STREQUAL "") + message(WARNING "nanosecond timestamp precision was not detected") + set(USE_NSEC OFF) +elseif(USE_NSEC STREQUAL ON) + message(FATAL_ERROR "nanosecond support was requested but no platform support is available") +endif() + +if(USE_NSEC STREQUAL "mtim") + if(NOT HAVE_STRUCT_STAT_ST_MTIM) + message(FATAL_ERROR "stat mtim could not be found") + endif() + + set(GIT_NSEC 1) + set(GIT_NSEC_MTIM 1) + add_feature_info("Nanosecond support" ON "using mtim") +elseif(USE_NSEC STREQUAL "mtimespec") + if(NOT HAVE_STRUCT_STAT_ST_MTIMESPEC) + message(FATAL_ERROR "mtimespec could not be found") + endif() + + set(GIT_NSEC 1) + set(GIT_NSEC_MTIMESPEC 1) + add_feature_info("Nanosecond support" ON "using mtimespec") +elseif(USE_NSEC STREQUAL "mtime_nsec") + if(NOT HAVE_STRUCT_STAT_MTIME_NSEC) + message(FATAL_ERROR "mtime_nsec could not be found") + endif() + + set(GIT_NSEC 1) + set(GIT_NSEC_MTIME_NSEC 1) + add_feature_info("Nanosecond support" ON "using mtime_nsec") +elseif(USE_NSEC STREQUAL "win32") + if(NOT WIN32) + message(FATAL_ERROR "Win32 API support is not available on this platform") + endif() + + set(GIT_NSEC 1) + set(GIT_NSEC_WIN32 1) + add_feature_info("Nanosecond support" ON "using Win32 APIs") +elseif(USE_NSEC STREQUAL OFF) + set(GIT_NSEC 0) + add_feature_info("Nanosecond support" OFF "Nanosecond timestamp resolution is disabled") +else() + message(FATAL_ERROR "unknown nanosecond option: ${USE_NSEC}") +endif() diff --git a/cmake/SelectRegex.cmake b/cmake/SelectRegex.cmake new file mode 100644 index 00000000000..d1c09ae44c2 --- /dev/null +++ b/cmake/SelectRegex.cmake @@ -0,0 +1,64 @@ +# Specify regular expression implementation +find_package(PCRE) + +set(OPTION_NAME "USE_REGEX") + +# Fall back to the previous cmake configuration, "USE_BUNDLED_ZLIB" +if(NOT USE_REGEX AND REGEX_BACKEND) + set(USE_REGEX "${REGEX_BACKEND}") + set(OPTION_NAME "REGEX_BACKEND") +endif() + +if(NOT USE_REGEX) + check_symbol_exists(regcomp_l "regex.h;xlocale.h" HAVE_REGCOMP_L) + + if(HAVE_REGCOMP_L) + # 'regcomp_l' has been explicitly marked unavailable on iOS_SDK + if(CMAKE_SYSTEM_NAME MATCHES "iOS") + set(USE_REGEX "regcomp") + else() + set(USE_REGEX "regcomp_l") + endif() + elseif(PCRE_FOUND) + set(USE_REGEX "pcre") + else() + set(USE_REGEX "builtin") + endif() +endif() + +if(USE_REGEX STREQUAL "regcomp_l") + add_feature_info("Regular expressions" ON "using system regcomp_l") + set(GIT_REGEX_REGCOMP_L 1) +elseif(USE_REGEX STREQUAL "pcre2") + find_package(PCRE2) + + if(NOT PCRE2_FOUND) + MESSAGE(FATAL_ERROR "PCRE2 support was requested but not found") + endif() + + add_feature_info("Regular expressions" ON "using system PCRE2") + set(GIT_REGEX_PCRE2 1) + + list(APPEND LIBGIT2_SYSTEM_INCLUDES ${PCRE2_INCLUDE_DIRS}) + list(APPEND LIBGIT2_SYSTEM_LIBS ${PCRE2_LIBRARIES}) + list(APPEND LIBGIT2_PC_REQUIRES "libpcre2-8") +elseif(USE_REGEX STREQUAL "pcre") + add_feature_info("Regular expressions" ON "using system PCRE") + set(GIT_REGEX_PCRE 1) + + list(APPEND LIBGIT2_SYSTEM_INCLUDES ${PCRE_INCLUDE_DIRS}) + list(APPEND LIBGIT2_SYSTEM_LIBS ${PCRE_LIBRARIES}) + list(APPEND LIBGIT2_PC_REQUIRES "libpcre") +elseif(USE_REGEX STREQUAL "regcomp") + add_feature_info("Regular expressions" ON "using system regcomp") + set(GIT_REGEX_REGCOMP 1) +elseif(USE_REGEX STREQUAL "builtin") + add_feature_info("Regular expressions" ON "using bundled implementation") + set(GIT_REGEX_BUILTIN 1) + + add_subdirectory("${PROJECT_SOURCE_DIR}/deps/pcre" "${PROJECT_BINARY_DIR}/deps/pcre") + list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${PROJECT_SOURCE_DIR}/deps/pcre") + list(APPEND LIBGIT2_DEPENDENCY_OBJECTS $) +else() + message(FATAL_ERROR "unknown setting to ${OPTION_NAME}: ${USE_REGEX}") +endif() diff --git a/cmake/SelectSSH.cmake b/cmake/SelectSSH.cmake new file mode 100644 index 00000000000..e655c3eccb4 --- /dev/null +++ b/cmake/SelectSSH.cmake @@ -0,0 +1,37 @@ +if(USE_SSH STREQUAL "exec") + set(GIT_SSH 1) + set(GIT_SSH_EXEC 1) + + add_feature_info(SSH ON "using OpenSSH exec support") +elseif(USE_SSH STREQUAL ON OR USE_SSH STREQUAL "libssh2") + find_pkglibraries(LIBSSH2 libssh2) + + if(NOT LIBSSH2_FOUND) + find_package(LibSSH2) + set(LIBSSH2_INCLUDE_DIRS ${LIBSSH2_INCLUDE_DIR}) + get_filename_component(LIBSSH2_LIBRARY_DIRS "${LIBSSH2_LIBRARY}" DIRECTORY) + set(LIBSSH2_LIBRARIES ${LIBSSH2_LIBRARY}) + set(LIBSSH2_LDFLAGS "-lssh2") + endif() + + if(NOT LIBSSH2_FOUND) + message(FATAL_ERROR "LIBSSH2 not found. Set CMAKE_PREFIX_PATH if it is installed outside of the default search path.") + endif() + + list(APPEND LIBGIT2_SYSTEM_INCLUDES ${LIBSSH2_INCLUDE_DIRS}) + list(APPEND LIBGIT2_SYSTEM_LIBS ${LIBSSH2_LIBRARIES}) + list(APPEND LIBGIT2_PC_LIBS ${LIBSSH2_LDFLAGS}) + + check_library_exists("${LIBSSH2_LIBRARIES}" libssh2_userauth_publickey_frommemory "${LIBSSH2_LIBRARY_DIRS}" HAVE_LIBSSH2_MEMORY_CREDENTIALS) + if(HAVE_LIBSSH2_MEMORY_CREDENTIALS) + set(GIT_SSH_LIBSSH2_MEMORY_CREDENTIALS 1) + endif() + + set(GIT_SSH 1) + set(GIT_SSH_LIBSSH2 1) + add_feature_info(SSH ON "using libssh2") +elseif(USE_SSH STREQUAL OFF OR USE_SSH STREQUAL "") + add_feature_info(SSH OFF "SSH transport support") +else() + message(FATAL_ERROR "unknown SSH option: ${USE_SSH}") +endif() diff --git a/cmake/SelectThreads.cmake b/cmake/SelectThreads.cmake new file mode 100644 index 00000000000..1a6ddfa9670 --- /dev/null +++ b/cmake/SelectThreads.cmake @@ -0,0 +1,41 @@ +include(SanitizeInput) + +sanitizeinput(USE_THREADS) + +if(NOT WIN32) + find_package(Threads) +endif() + +if((USE_THREADS STREQUAL ON OR USE_THREADS STREQUAL "") AND THREADS_FOUND) + set(USE_THREADS "pthreads") +elseif((USE_THREADS STREQUAL ON OR USE_THREADS STREQUAL "") AND WIN32) + set(USE_THREADS "win32") +elseif(USE_THREADS STREQUAL "") + set(USE_THREADS OFF) +endif() + +if(USE_THREADS STREQUAL "pthreads") + if(NOT THREADS_FOUND) + message(FATAL_ERROR "pthreads were requested but not found") + endif() + + list(APPEND LIBGIT2_SYSTEM_LIBS ${CMAKE_THREAD_LIBS_INIT}) + list(APPEND LIBGIT2_PC_LIBS ${CMAKE_THREAD_LIBS_INIT}) + + set(GIT_THREADS 1) + set(GIT_THREADS_PTHREADS 1) + add_feature_info("Threads" ON "using pthreads") +elseif(USE_THREADS STREQUAL "win32") + if(NOT WIN32) + message(FATAL_ERROR "Win32 API support is not available on this platform") + endif() + + set(GIT_THREADS 1) + set(GIT_THREADS_WIN32 1) + add_feature_info("Threads" ON "using Win32 APIs") +elseif(USE_THREADS STREQUAL OFF) + set(GIT_THREADS 0) + add_feature_info("Threads" OFF "threads support is disabled") +else() + message(FATAL_ERROR "unknown threads option: ${USE_THREADS}") +endif() diff --git a/cmake/SelectXdiff.cmake b/cmake/SelectXdiff.cmake new file mode 100644 index 00000000000..91d72ad34b6 --- /dev/null +++ b/cmake/SelectXdiff.cmake @@ -0,0 +1,16 @@ +# Optional external dependency: xdiff + +include(SanitizeInput) + +sanitizeinput(USE_XDIFF) + +if(USE_XDIFF STREQUAL "system") + message(FATAL_ERROR "external/system xdiff is not yet supported") +elseif(USE_XDIFF STREQUAL "builtin" OR USE_XDIFF STREQUAL "") + add_subdirectory("${PROJECT_SOURCE_DIR}/deps/xdiff" "${PROJECT_BINARY_DIR}/deps/xdiff") + list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${PROJECT_SOURCE_DIR}/deps/xdiff") + list(APPEND LIBGIT2_DEPENDENCY_OBJECTS "$") + add_feature_info("Xdiff" ON "using bundled provider") +else() + message(FATAL_ERROR "asked for unknown Xdiff backend: ${USE_XDIFF}") +endif() diff --git a/deps/http-parser/LICENSE-MIT b/deps/http-parser/LICENSE-MIT deleted file mode 100644 index 58010b38894..00000000000 --- a/deps/http-parser/LICENSE-MIT +++ /dev/null @@ -1,23 +0,0 @@ -http_parser.c is based on src/http/ngx_http_parse.c from NGINX copyright -Igor Sysoev. - -Additional changes are licensed under the same terms as NGINX and -copyright Joyent, Inc. and other Node contributors. All rights reserved. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to -deal in the Software without restriction, including without limitation the -rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -sell copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -IN THE SOFTWARE. diff --git a/deps/http-parser/http_parser.c b/deps/http-parser/http_parser.c deleted file mode 100644 index 20353025472..00000000000 --- a/deps/http-parser/http_parser.c +++ /dev/null @@ -1,2174 +0,0 @@ -/* Based on src/http/ngx_http_parse.c from NGINX copyright Igor Sysoev - * - * Additional changes are licensed under the same terms as NGINX and - * copyright Joyent, Inc. and other Node contributors. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ -#include "http_parser.h" -#include -#include -#include -#include -#include -#include - -#ifndef ULLONG_MAX -# define ULLONG_MAX ((uint64_t) -1) /* 2^64-1 */ -#endif - -#ifndef MIN -# define MIN(a,b) ((a) < (b) ? (a) : (b)) -#endif - -#ifndef ARRAY_SIZE -# define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) -#endif - -#ifndef BIT_AT -# define BIT_AT(a, i) \ - (!!((unsigned int) (a)[(unsigned int) (i) >> 3] & \ - (1 << ((unsigned int) (i) & 7)))) -#endif - -#ifndef ELEM_AT -# define ELEM_AT(a, i, v) ((unsigned int) (i) < ARRAY_SIZE(a) ? (a)[(i)] : (v)) -#endif - -#define SET_ERRNO(e) \ -do { \ - parser->http_errno = (e); \ -} while(0) - - -/* Run the notify callback FOR, returning ER if it fails */ -#define CALLBACK_NOTIFY_(FOR, ER) \ -do { \ - assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \ - \ - if (settings->on_##FOR) { \ - if (0 != settings->on_##FOR(parser)) { \ - SET_ERRNO(HPE_CB_##FOR); \ - } \ - \ - /* We either errored above or got paused; get out */ \ - if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { \ - return (ER); \ - } \ - } \ -} while (0) - -/* Run the notify callback FOR and consume the current byte */ -#define CALLBACK_NOTIFY(FOR) CALLBACK_NOTIFY_(FOR, p - data + 1) - -/* Run the notify callback FOR and don't consume the current byte */ -#define CALLBACK_NOTIFY_NOADVANCE(FOR) CALLBACK_NOTIFY_(FOR, p - data) - -/* Run data callback FOR with LEN bytes, returning ER if it fails */ -#define CALLBACK_DATA_(FOR, LEN, ER) \ -do { \ - assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \ - \ - if (FOR##_mark) { \ - if (settings->on_##FOR) { \ - if (0 != settings->on_##FOR(parser, FOR##_mark, (LEN))) { \ - SET_ERRNO(HPE_CB_##FOR); \ - } \ - \ - /* We either errored above or got paused; get out */ \ - if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { \ - return (ER); \ - } \ - } \ - FOR##_mark = NULL; \ - } \ -} while (0) - -/* Run the data callback FOR and consume the current byte */ -#define CALLBACK_DATA(FOR) \ - CALLBACK_DATA_(FOR, p - FOR##_mark, p - data + 1) - -/* Run the data callback FOR and don't consume the current byte */ -#define CALLBACK_DATA_NOADVANCE(FOR) \ - CALLBACK_DATA_(FOR, p - FOR##_mark, p - data) - -/* Set the mark FOR; non-destructive if mark is already set */ -#define MARK(FOR) \ -do { \ - if (!FOR##_mark) { \ - FOR##_mark = p; \ - } \ -} while (0) - - -#define PROXY_CONNECTION "proxy-connection" -#define CONNECTION "connection" -#define CONTENT_LENGTH "content-length" -#define TRANSFER_ENCODING "transfer-encoding" -#define UPGRADE "upgrade" -#define CHUNKED "chunked" -#define KEEP_ALIVE "keep-alive" -#define CLOSE "close" - - -static const char *method_strings[] = - { -#define XX(num, name, string) #string, - HTTP_METHOD_MAP(XX) -#undef XX - }; - - -/* Tokens as defined by rfc 2616. Also lowercases them. - * token = 1* - * separators = "(" | ")" | "<" | ">" | "@" - * | "," | ";" | ":" | "\" | <"> - * | "/" | "[" | "]" | "?" | "=" - * | "{" | "}" | SP | HT - */ -static const char tokens[256] = { -/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */ - 0, 0, 0, 0, 0, 0, 0, 0, -/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */ - 0, 0, 0, 0, 0, 0, 0, 0, -/* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */ - 0, 0, 0, 0, 0, 0, 0, 0, -/* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */ - 0, 0, 0, 0, 0, 0, 0, 0, -/* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */ - 0, '!', 0, '#', '$', '%', '&', '\'', -/* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */ - 0, 0, '*', '+', 0, '-', '.', 0, -/* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */ - '0', '1', '2', '3', '4', '5', '6', '7', -/* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */ - '8', '9', 0, 0, 0, 0, 0, 0, -/* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */ - 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', -/* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */ - 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', -/* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */ - 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', -/* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */ - 'x', 'y', 'z', 0, 0, 0, '^', '_', -/* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */ - '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', -/* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */ - 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', -/* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */ - 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', -/* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */ - 'x', 'y', 'z', 0, '|', 0, '~', 0 }; - - -static const int8_t unhex[256] = - {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 - ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 - ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 - , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1 - ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 - ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 - ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 - ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 - }; - - -#if HTTP_PARSER_STRICT -# define T(v) 0 -#else -# define T(v) v -#endif - - -static const uint8_t normal_url_char[32] = { -/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */ - 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, -/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */ - 0 | T(2) | 0 | 0 | T(16) | 0 | 0 | 0, -/* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */ - 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, -/* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */ - 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, -/* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */ - 0 | 2 | 4 | 0 | 16 | 32 | 64 | 128, -/* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */ - 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, -/* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */ - 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, -/* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */ - 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, -/* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */ - 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, -/* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */ - 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, -/* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */ - 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, -/* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */ - 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, -/* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */ - 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, -/* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */ - 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, -/* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */ - 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, -/* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */ - 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, }; - -#undef T - -enum state - { s_dead = 1 /* important that this is > 0 */ - - , s_start_req_or_res - , s_res_or_resp_H - , s_start_res - , s_res_H - , s_res_HT - , s_res_HTT - , s_res_HTTP - , s_res_first_http_major - , s_res_http_major - , s_res_first_http_minor - , s_res_http_minor - , s_res_first_status_code - , s_res_status_code - , s_res_status - , s_res_line_almost_done - - , s_start_req - - , s_req_method - , s_req_spaces_before_url - , s_req_schema - , s_req_schema_slash - , s_req_schema_slash_slash - , s_req_server_start - , s_req_server - , s_req_server_with_at - , s_req_path - , s_req_query_string_start - , s_req_query_string - , s_req_fragment_start - , s_req_fragment - , s_req_http_start - , s_req_http_H - , s_req_http_HT - , s_req_http_HTT - , s_req_http_HTTP - , s_req_first_http_major - , s_req_http_major - , s_req_first_http_minor - , s_req_http_minor - , s_req_line_almost_done - - , s_header_field_start - , s_header_field - , s_header_value_start - , s_header_value - , s_header_value_lws - - , s_header_almost_done - - , s_chunk_size_start - , s_chunk_size - , s_chunk_parameters - , s_chunk_size_almost_done - - , s_headers_almost_done - , s_headers_done - - /* Important: 's_headers_done' must be the last 'header' state. All - * states beyond this must be 'body' states. It is used for overflow - * checking. See the PARSING_HEADER() macro. - */ - - , s_chunk_data - , s_chunk_data_almost_done - , s_chunk_data_done - - , s_body_identity - , s_body_identity_eof - - , s_message_done - }; - - -#define PARSING_HEADER(state) (state <= s_headers_done) - - -enum header_states - { h_general = 0 - , h_C - , h_CO - , h_CON - - , h_matching_connection - , h_matching_proxy_connection - , h_matching_content_length - , h_matching_transfer_encoding - , h_matching_upgrade - - , h_connection - , h_content_length - , h_transfer_encoding - , h_upgrade - - , h_matching_transfer_encoding_chunked - , h_matching_connection_keep_alive - , h_matching_connection_close - - , h_transfer_encoding_chunked - , h_connection_keep_alive - , h_connection_close - }; - -enum http_host_state - { - s_http_host_dead = 1 - , s_http_userinfo_start - , s_http_userinfo - , s_http_host_start - , s_http_host_v6_start - , s_http_host - , s_http_host_v6 - , s_http_host_v6_end - , s_http_host_port_start - , s_http_host_port -}; - -/* Macros for character classes; depends on strict-mode */ -#define CR '\r' -#define LF '\n' -#define LOWER(c) (unsigned char)(c | 0x20) -#define IS_ALPHA(c) (LOWER(c) >= 'a' && LOWER(c) <= 'z') -#define IS_NUM(c) ((c) >= '0' && (c) <= '9') -#define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_NUM(c)) -#define IS_HEX(c) (IS_NUM(c) || (LOWER(c) >= 'a' && LOWER(c) <= 'f')) -#define IS_MARK(c) ((c) == '-' || (c) == '_' || (c) == '.' || \ - (c) == '!' || (c) == '~' || (c) == '*' || (c) == '\'' || (c) == '(' || \ - (c) == ')') -#define IS_USERINFO_CHAR(c) (IS_ALPHANUM(c) || IS_MARK(c) || (c) == '%' || \ - (c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \ - (c) == '$' || (c) == ',') - -#if HTTP_PARSER_STRICT -#define TOKEN(c) (tokens[(unsigned char)c]) -#define IS_URL_CHAR(c) (BIT_AT(normal_url_char, (unsigned char)c)) -#define IS_HOST_CHAR(c) (IS_ALPHANUM(c) || (c) == '.' || (c) == '-') -#else -#define TOKEN(c) ((c == ' ') ? ' ' : tokens[(unsigned char)c]) -#define IS_URL_CHAR(c) \ - (BIT_AT(normal_url_char, (unsigned char)c) || ((c) & 0x80)) -#define IS_HOST_CHAR(c) \ - (IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_') -#endif - - -#define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res) - - -#if HTTP_PARSER_STRICT -# define STRICT_CHECK(cond) \ -do { \ - if (cond) { \ - SET_ERRNO(HPE_STRICT); \ - goto error; \ - } \ -} while (0) -# define NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : s_dead) -#else -# define STRICT_CHECK(cond) -# define NEW_MESSAGE() start_state -#endif - - -/* Map errno values to strings for human-readable output */ -#define HTTP_STRERROR_GEN(n, s) { "HPE_" #n, s }, -static struct { - const char *name; - const char *description; -} http_strerror_tab[] = { - HTTP_ERRNO_MAP(HTTP_STRERROR_GEN) -}; -#undef HTTP_STRERROR_GEN - -int http_message_needs_eof(const http_parser *parser); - -/* Our URL parser. - * - * This is designed to be shared by http_parser_execute() for URL validation, - * hence it has a state transition + byte-for-byte interface. In addition, it - * is meant to be embedded in http_parser_parse_url(), which does the dirty - * work of turning state transitions URL components for its API. - * - * This function should only be invoked with non-space characters. It is - * assumed that the caller cares about (and can detect) the transition between - * URL and non-URL states by looking for these. - */ -static enum state -parse_url_char(enum state s, const char ch) -{ - if (ch == ' ' || ch == '\r' || ch == '\n') { - return s_dead; - } - -#if HTTP_PARSER_STRICT - if (ch == '\t' || ch == '\f') { - return s_dead; - } -#endif - - switch (s) { - case s_req_spaces_before_url: - /* Proxied requests are followed by scheme of an absolute URI (alpha). - * All methods except CONNECT are followed by '/' or '*'. - */ - - if (ch == '/' || ch == '*') { - return s_req_path; - } - - if (IS_ALPHA(ch)) { - return s_req_schema; - } - - break; - - case s_req_schema: - if (IS_ALPHA(ch)) { - return s; - } - - if (ch == ':') { - return s_req_schema_slash; - } - - break; - - case s_req_schema_slash: - if (ch == '/') { - return s_req_schema_slash_slash; - } - - break; - - case s_req_schema_slash_slash: - if (ch == '/') { - return s_req_server_start; - } - - break; - - case s_req_server_with_at: - if (ch == '@') { - return s_dead; - } - - /* FALLTHROUGH */ - case s_req_server_start: - case s_req_server: - if (ch == '/') { - return s_req_path; - } - - if (ch == '?') { - return s_req_query_string_start; - } - - if (ch == '@') { - return s_req_server_with_at; - } - - if (IS_USERINFO_CHAR(ch) || ch == '[' || ch == ']') { - return s_req_server; - } - - break; - - case s_req_path: - if (IS_URL_CHAR(ch)) { - return s; - } - - switch (ch) { - case '?': - return s_req_query_string_start; - - case '#': - return s_req_fragment_start; - } - - break; - - case s_req_query_string_start: - case s_req_query_string: - if (IS_URL_CHAR(ch)) { - return s_req_query_string; - } - - switch (ch) { - case '?': - /* allow extra '?' in query string */ - return s_req_query_string; - - case '#': - return s_req_fragment_start; - } - - break; - - case s_req_fragment_start: - if (IS_URL_CHAR(ch)) { - return s_req_fragment; - } - - switch (ch) { - case '?': - return s_req_fragment; - - case '#': - return s; - } - - break; - - case s_req_fragment: - if (IS_URL_CHAR(ch)) { - return s; - } - - switch (ch) { - case '?': - case '#': - return s; - } - - break; - - default: - break; - } - - /* We should never fall out of the switch above unless there's an error */ - return s_dead; -} - -size_t http_parser_execute (http_parser *parser, - const http_parser_settings *settings, - const char *data, - size_t len) -{ - char c, ch; - int8_t unhex_val; - const char *p = data; - const char *header_field_mark = 0; - const char *header_value_mark = 0; - const char *url_mark = 0; - const char *body_mark = 0; - - /* We're in an error state. Don't bother doing anything. */ - if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { - return 0; - } - - if (len == 0) { - switch (parser->state) { - case s_body_identity_eof: - /* Use of CALLBACK_NOTIFY() here would erroneously return 1 byte read if - * we got paused. - */ - CALLBACK_NOTIFY_NOADVANCE(message_complete); - return 0; - - case s_dead: - case s_start_req_or_res: - case s_start_res: - case s_start_req: - return 0; - - default: - SET_ERRNO(HPE_INVALID_EOF_STATE); - return 1; - } - } - - - if (parser->state == s_header_field) - header_field_mark = data; - if (parser->state == s_header_value) - header_value_mark = data; - switch (parser->state) { - case s_req_path: - case s_req_schema: - case s_req_schema_slash: - case s_req_schema_slash_slash: - case s_req_server_start: - case s_req_server: - case s_req_server_with_at: - case s_req_query_string_start: - case s_req_query_string: - case s_req_fragment_start: - case s_req_fragment: - url_mark = data; - break; - } - - for (p=data; p != data + len; p++) { - ch = *p; - - if (PARSING_HEADER(parser->state)) { - ++parser->nread; - /* Buffer overflow attack */ - if (parser->nread > HTTP_MAX_HEADER_SIZE) { - SET_ERRNO(HPE_HEADER_OVERFLOW); - goto error; - } - } - - reexecute_byte: - switch (parser->state) { - - case s_dead: - /* this state is used after a 'Connection: close' message - * the parser will error out if it reads another message - */ - if (ch == CR || ch == LF) - break; - - SET_ERRNO(HPE_CLOSED_CONNECTION); - goto error; - - case s_start_req_or_res: - { - if (ch == CR || ch == LF) - break; - parser->flags = 0; - parser->content_length = ULLONG_MAX; - - if (ch == 'H') { - parser->state = s_res_or_resp_H; - - CALLBACK_NOTIFY(message_begin); - } else { - parser->type = HTTP_REQUEST; - parser->state = s_start_req; - goto reexecute_byte; - } - - break; - } - - case s_res_or_resp_H: - if (ch == 'T') { - parser->type = HTTP_RESPONSE; - parser->state = s_res_HT; - } else { - if (ch != 'E') { - SET_ERRNO(HPE_INVALID_CONSTANT); - goto error; - } - - parser->type = HTTP_REQUEST; - parser->method = HTTP_HEAD; - parser->index = 2; - parser->state = s_req_method; - } - break; - - case s_start_res: - { - parser->flags = 0; - parser->content_length = ULLONG_MAX; - - switch (ch) { - case 'H': - parser->state = s_res_H; - break; - - case CR: - case LF: - break; - - default: - SET_ERRNO(HPE_INVALID_CONSTANT); - goto error; - } - - CALLBACK_NOTIFY(message_begin); - break; - } - - case s_res_H: - STRICT_CHECK(ch != 'T'); - parser->state = s_res_HT; - break; - - case s_res_HT: - STRICT_CHECK(ch != 'T'); - parser->state = s_res_HTT; - break; - - case s_res_HTT: - STRICT_CHECK(ch != 'P'); - parser->state = s_res_HTTP; - break; - - case s_res_HTTP: - STRICT_CHECK(ch != '/'); - parser->state = s_res_first_http_major; - break; - - case s_res_first_http_major: - if (ch < '0' || ch > '9') { - SET_ERRNO(HPE_INVALID_VERSION); - goto error; - } - - parser->http_major = ch - '0'; - parser->state = s_res_http_major; - break; - - /* major HTTP version or dot */ - case s_res_http_major: - { - if (ch == '.') { - parser->state = s_res_first_http_minor; - break; - } - - if (!IS_NUM(ch)) { - SET_ERRNO(HPE_INVALID_VERSION); - goto error; - } - - parser->http_major *= 10; - parser->http_major += ch - '0'; - - if (parser->http_major > 999) { - SET_ERRNO(HPE_INVALID_VERSION); - goto error; - } - - break; - } - - /* first digit of minor HTTP version */ - case s_res_first_http_minor: - if (!IS_NUM(ch)) { - SET_ERRNO(HPE_INVALID_VERSION); - goto error; - } - - parser->http_minor = ch - '0'; - parser->state = s_res_http_minor; - break; - - /* minor HTTP version or end of request line */ - case s_res_http_minor: - { - if (ch == ' ') { - parser->state = s_res_first_status_code; - break; - } - - if (!IS_NUM(ch)) { - SET_ERRNO(HPE_INVALID_VERSION); - goto error; - } - - parser->http_minor *= 10; - parser->http_minor += ch - '0'; - - if (parser->http_minor > 999) { - SET_ERRNO(HPE_INVALID_VERSION); - goto error; - } - - break; - } - - case s_res_first_status_code: - { - if (!IS_NUM(ch)) { - if (ch == ' ') { - break; - } - - SET_ERRNO(HPE_INVALID_STATUS); - goto error; - } - parser->status_code = ch - '0'; - parser->state = s_res_status_code; - break; - } - - case s_res_status_code: - { - if (!IS_NUM(ch)) { - switch (ch) { - case ' ': - parser->state = s_res_status; - break; - case CR: - parser->state = s_res_line_almost_done; - break; - case LF: - parser->state = s_header_field_start; - break; - default: - SET_ERRNO(HPE_INVALID_STATUS); - goto error; - } - break; - } - - parser->status_code *= 10; - parser->status_code += ch - '0'; - - if (parser->status_code > 999) { - SET_ERRNO(HPE_INVALID_STATUS); - goto error; - } - - break; - } - - case s_res_status: - /* the human readable status. e.g. "NOT FOUND" - * we are not humans so just ignore this */ - if (ch == CR) { - parser->state = s_res_line_almost_done; - break; - } - - if (ch == LF) { - parser->state = s_header_field_start; - break; - } - break; - - case s_res_line_almost_done: - STRICT_CHECK(ch != LF); - parser->state = s_header_field_start; - break; - - case s_start_req: - { - if (ch == CR || ch == LF) - break; - parser->flags = 0; - parser->content_length = ULLONG_MAX; - - if (!IS_ALPHA(ch)) { - SET_ERRNO(HPE_INVALID_METHOD); - goto error; - } - - parser->method = (enum http_method) 0; - parser->index = 1; - switch (ch) { - case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break; - case 'D': parser->method = HTTP_DELETE; break; - case 'G': parser->method = HTTP_GET; break; - case 'H': parser->method = HTTP_HEAD; break; - case 'L': parser->method = HTTP_LOCK; break; - case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH */ break; - case 'N': parser->method = HTTP_NOTIFY; break; - case 'O': parser->method = HTTP_OPTIONS; break; - case 'P': parser->method = HTTP_POST; - /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */ - break; - case 'R': parser->method = HTTP_REPORT; break; - case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break; - case 'T': parser->method = HTTP_TRACE; break; - case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE */ break; - default: - SET_ERRNO(HPE_INVALID_METHOD); - goto error; - } - parser->state = s_req_method; - - CALLBACK_NOTIFY(message_begin); - - break; - } - - case s_req_method: - { - const char *matcher; - if (ch == '\0') { - SET_ERRNO(HPE_INVALID_METHOD); - goto error; - } - - matcher = method_strings[parser->method]; - if (ch == ' ' && matcher[parser->index] == '\0') { - parser->state = s_req_spaces_before_url; - } else if (ch == matcher[parser->index]) { - ; /* nada */ - } else if (parser->method == HTTP_CONNECT) { - if (parser->index == 1 && ch == 'H') { - parser->method = HTTP_CHECKOUT; - } else if (parser->index == 2 && ch == 'P') { - parser->method = HTTP_COPY; - } else { - goto error; - } - } else if (parser->method == HTTP_MKCOL) { - if (parser->index == 1 && ch == 'O') { - parser->method = HTTP_MOVE; - } else if (parser->index == 1 && ch == 'E') { - parser->method = HTTP_MERGE; - } else if (parser->index == 1 && ch == '-') { - parser->method = HTTP_MSEARCH; - } else if (parser->index == 2 && ch == 'A') { - parser->method = HTTP_MKACTIVITY; - } else { - goto error; - } - } else if (parser->method == HTTP_SUBSCRIBE) { - if (parser->index == 1 && ch == 'E') { - parser->method = HTTP_SEARCH; - } else { - goto error; - } - } else if (parser->index == 1 && parser->method == HTTP_POST) { - if (ch == 'R') { - parser->method = HTTP_PROPFIND; /* or HTTP_PROPPATCH */ - } else if (ch == 'U') { - parser->method = HTTP_PUT; /* or HTTP_PURGE */ - } else if (ch == 'A') { - parser->method = HTTP_PATCH; - } else { - goto error; - } - } else if (parser->index == 2) { - if (parser->method == HTTP_PUT) { - if (ch == 'R') parser->method = HTTP_PURGE; - } else if (parser->method == HTTP_UNLOCK) { - if (ch == 'S') parser->method = HTTP_UNSUBSCRIBE; - } - } else if (parser->index == 4 && parser->method == HTTP_PROPFIND && ch == 'P') { - parser->method = HTTP_PROPPATCH; - } else { - SET_ERRNO(HPE_INVALID_METHOD); - goto error; - } - - ++parser->index; - break; - } - - case s_req_spaces_before_url: - { - if (ch == ' ') break; - - MARK(url); - if (parser->method == HTTP_CONNECT) { - parser->state = s_req_server_start; - } - - parser->state = parse_url_char((enum state)parser->state, ch); - if (parser->state == s_dead) { - SET_ERRNO(HPE_INVALID_URL); - goto error; - } - - break; - } - - case s_req_schema: - case s_req_schema_slash: - case s_req_schema_slash_slash: - case s_req_server_start: - { - switch (ch) { - /* No whitespace allowed here */ - case ' ': - case CR: - case LF: - SET_ERRNO(HPE_INVALID_URL); - goto error; - default: - parser->state = parse_url_char((enum state)parser->state, ch); - if (parser->state == s_dead) { - SET_ERRNO(HPE_INVALID_URL); - goto error; - } - } - - break; - } - - case s_req_server: - case s_req_server_with_at: - case s_req_path: - case s_req_query_string_start: - case s_req_query_string: - case s_req_fragment_start: - case s_req_fragment: - { - switch (ch) { - case ' ': - parser->state = s_req_http_start; - CALLBACK_DATA(url); - break; - case CR: - case LF: - parser->http_major = 0; - parser->http_minor = 9; - parser->state = (ch == CR) ? - s_req_line_almost_done : - s_header_field_start; - CALLBACK_DATA(url); - break; - default: - parser->state = parse_url_char((enum state)parser->state, ch); - if (parser->state == s_dead) { - SET_ERRNO(HPE_INVALID_URL); - goto error; - } - } - break; - } - - case s_req_http_start: - switch (ch) { - case 'H': - parser->state = s_req_http_H; - break; - case ' ': - break; - default: - SET_ERRNO(HPE_INVALID_CONSTANT); - goto error; - } - break; - - case s_req_http_H: - STRICT_CHECK(ch != 'T'); - parser->state = s_req_http_HT; - break; - - case s_req_http_HT: - STRICT_CHECK(ch != 'T'); - parser->state = s_req_http_HTT; - break; - - case s_req_http_HTT: - STRICT_CHECK(ch != 'P'); - parser->state = s_req_http_HTTP; - break; - - case s_req_http_HTTP: - STRICT_CHECK(ch != '/'); - parser->state = s_req_first_http_major; - break; - - /* first digit of major HTTP version */ - case s_req_first_http_major: - if (ch < '1' || ch > '9') { - SET_ERRNO(HPE_INVALID_VERSION); - goto error; - } - - parser->http_major = ch - '0'; - parser->state = s_req_http_major; - break; - - /* major HTTP version or dot */ - case s_req_http_major: - { - if (ch == '.') { - parser->state = s_req_first_http_minor; - break; - } - - if (!IS_NUM(ch)) { - SET_ERRNO(HPE_INVALID_VERSION); - goto error; - } - - parser->http_major *= 10; - parser->http_major += ch - '0'; - - if (parser->http_major > 999) { - SET_ERRNO(HPE_INVALID_VERSION); - goto error; - } - - break; - } - - /* first digit of minor HTTP version */ - case s_req_first_http_minor: - if (!IS_NUM(ch)) { - SET_ERRNO(HPE_INVALID_VERSION); - goto error; - } - - parser->http_minor = ch - '0'; - parser->state = s_req_http_minor; - break; - - /* minor HTTP version or end of request line */ - case s_req_http_minor: - { - if (ch == CR) { - parser->state = s_req_line_almost_done; - break; - } - - if (ch == LF) { - parser->state = s_header_field_start; - break; - } - - /* XXX allow spaces after digit? */ - - if (!IS_NUM(ch)) { - SET_ERRNO(HPE_INVALID_VERSION); - goto error; - } - - parser->http_minor *= 10; - parser->http_minor += ch - '0'; - - if (parser->http_minor > 999) { - SET_ERRNO(HPE_INVALID_VERSION); - goto error; - } - - break; - } - - /* end of request line */ - case s_req_line_almost_done: - { - if (ch != LF) { - SET_ERRNO(HPE_LF_EXPECTED); - goto error; - } - - parser->state = s_header_field_start; - break; - } - - case s_header_field_start: - { - if (ch == CR) { - parser->state = s_headers_almost_done; - break; - } - - if (ch == LF) { - /* they might be just sending \n instead of \r\n so this would be - * the second \n to denote the end of headers*/ - parser->state = s_headers_almost_done; - goto reexecute_byte; - } - - c = TOKEN(ch); - - if (!c) { - SET_ERRNO(HPE_INVALID_HEADER_TOKEN); - goto error; - } - - MARK(header_field); - - parser->index = 0; - parser->state = s_header_field; - - switch (c) { - case 'c': - parser->header_state = h_C; - break; - - case 'p': - parser->header_state = h_matching_proxy_connection; - break; - - case 't': - parser->header_state = h_matching_transfer_encoding; - break; - - case 'u': - parser->header_state = h_matching_upgrade; - break; - - default: - parser->header_state = h_general; - break; - } - break; - } - - case s_header_field: - { - c = TOKEN(ch); - - if (c) { - switch (parser->header_state) { - case h_general: - break; - - case h_C: - parser->index++; - parser->header_state = (c == 'o' ? h_CO : h_general); - break; - - case h_CO: - parser->index++; - parser->header_state = (c == 'n' ? h_CON : h_general); - break; - - case h_CON: - parser->index++; - switch (c) { - case 'n': - parser->header_state = h_matching_connection; - break; - case 't': - parser->header_state = h_matching_content_length; - break; - default: - parser->header_state = h_general; - break; - } - break; - - /* connection */ - - case h_matching_connection: - parser->index++; - if (parser->index > sizeof(CONNECTION)-1 - || c != CONNECTION[parser->index]) { - parser->header_state = h_general; - } else if (parser->index == sizeof(CONNECTION)-2) { - parser->header_state = h_connection; - } - break; - - /* proxy-connection */ - - case h_matching_proxy_connection: - parser->index++; - if (parser->index > sizeof(PROXY_CONNECTION)-1 - || c != PROXY_CONNECTION[parser->index]) { - parser->header_state = h_general; - } else if (parser->index == sizeof(PROXY_CONNECTION)-2) { - parser->header_state = h_connection; - } - break; - - /* content-length */ - - case h_matching_content_length: - parser->index++; - if (parser->index > sizeof(CONTENT_LENGTH)-1 - || c != CONTENT_LENGTH[parser->index]) { - parser->header_state = h_general; - } else if (parser->index == sizeof(CONTENT_LENGTH)-2) { - parser->header_state = h_content_length; - } - break; - - /* transfer-encoding */ - - case h_matching_transfer_encoding: - parser->index++; - if (parser->index > sizeof(TRANSFER_ENCODING)-1 - || c != TRANSFER_ENCODING[parser->index]) { - parser->header_state = h_general; - } else if (parser->index == sizeof(TRANSFER_ENCODING)-2) { - parser->header_state = h_transfer_encoding; - } - break; - - /* upgrade */ - - case h_matching_upgrade: - parser->index++; - if (parser->index > sizeof(UPGRADE)-1 - || c != UPGRADE[parser->index]) { - parser->header_state = h_general; - } else if (parser->index == sizeof(UPGRADE)-2) { - parser->header_state = h_upgrade; - } - break; - - case h_connection: - case h_content_length: - case h_transfer_encoding: - case h_upgrade: - if (ch != ' ') parser->header_state = h_general; - break; - - default: - assert(0 && "Unknown header_state"); - break; - } - break; - } - - if (ch == ':') { - parser->state = s_header_value_start; - CALLBACK_DATA(header_field); - break; - } - - if (ch == CR) { - parser->state = s_header_almost_done; - CALLBACK_DATA(header_field); - break; - } - - if (ch == LF) { - parser->state = s_header_field_start; - CALLBACK_DATA(header_field); - break; - } - - SET_ERRNO(HPE_INVALID_HEADER_TOKEN); - goto error; - } - - case s_header_value_start: - { - if (ch == ' ' || ch == '\t') break; - - MARK(header_value); - - parser->state = s_header_value; - parser->index = 0; - - if (ch == CR) { - parser->header_state = h_general; - parser->state = s_header_almost_done; - CALLBACK_DATA(header_value); - break; - } - - if (ch == LF) { - parser->state = s_header_field_start; - CALLBACK_DATA(header_value); - break; - } - - c = LOWER(ch); - - switch (parser->header_state) { - case h_upgrade: - parser->flags |= F_UPGRADE; - parser->header_state = h_general; - break; - - case h_transfer_encoding: - /* looking for 'Transfer-Encoding: chunked' */ - if ('c' == c) { - parser->header_state = h_matching_transfer_encoding_chunked; - } else { - parser->header_state = h_general; - } - break; - - case h_content_length: - if (!IS_NUM(ch)) { - SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); - goto error; - } - - parser->content_length = ch - '0'; - break; - - case h_connection: - /* looking for 'Connection: keep-alive' */ - if (c == 'k') { - parser->header_state = h_matching_connection_keep_alive; - /* looking for 'Connection: close' */ - } else if (c == 'c') { - parser->header_state = h_matching_connection_close; - } else { - parser->header_state = h_general; - } - break; - - default: - parser->header_state = h_general; - break; - } - break; - } - - case s_header_value: - { - - if (ch == CR) { - parser->state = s_header_almost_done; - CALLBACK_DATA(header_value); - break; - } - - if (ch == LF) { - parser->state = s_header_almost_done; - CALLBACK_DATA_NOADVANCE(header_value); - goto reexecute_byte; - } - - c = LOWER(ch); - - switch (parser->header_state) { - case h_general: - break; - - case h_connection: - case h_transfer_encoding: - assert(0 && "Shouldn't get here."); - break; - - case h_content_length: - { - uint64_t t; - - if (ch == ' ') break; - - if (!IS_NUM(ch)) { - SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); - goto error; - } - - t = parser->content_length; - t *= 10; - t += ch - '0'; - - /* Overflow? */ - if (t < parser->content_length || t == ULLONG_MAX) { - SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); - goto error; - } - - parser->content_length = t; - break; - } - - /* Transfer-Encoding: chunked */ - case h_matching_transfer_encoding_chunked: - parser->index++; - if (parser->index > sizeof(CHUNKED)-1 - || c != CHUNKED[parser->index]) { - parser->header_state = h_general; - } else if (parser->index == sizeof(CHUNKED)-2) { - parser->header_state = h_transfer_encoding_chunked; - } - break; - - /* looking for 'Connection: keep-alive' */ - case h_matching_connection_keep_alive: - parser->index++; - if (parser->index > sizeof(KEEP_ALIVE)-1 - || c != KEEP_ALIVE[parser->index]) { - parser->header_state = h_general; - } else if (parser->index == sizeof(KEEP_ALIVE)-2) { - parser->header_state = h_connection_keep_alive; - } - break; - - /* looking for 'Connection: close' */ - case h_matching_connection_close: - parser->index++; - if (parser->index > sizeof(CLOSE)-1 || c != CLOSE[parser->index]) { - parser->header_state = h_general; - } else if (parser->index == sizeof(CLOSE)-2) { - parser->header_state = h_connection_close; - } - break; - - case h_transfer_encoding_chunked: - case h_connection_keep_alive: - case h_connection_close: - if (ch != ' ') parser->header_state = h_general; - break; - - default: - parser->state = s_header_value; - parser->header_state = h_general; - break; - } - break; - } - - case s_header_almost_done: - { - STRICT_CHECK(ch != LF); - - parser->state = s_header_value_lws; - - switch (parser->header_state) { - case h_connection_keep_alive: - parser->flags |= F_CONNECTION_KEEP_ALIVE; - break; - case h_connection_close: - parser->flags |= F_CONNECTION_CLOSE; - break; - case h_transfer_encoding_chunked: - parser->flags |= F_CHUNKED; - break; - default: - break; - } - - break; - } - - case s_header_value_lws: - { - if (ch == ' ' || ch == '\t') - parser->state = s_header_value_start; - else - { - parser->state = s_header_field_start; - goto reexecute_byte; - } - break; - } - - case s_headers_almost_done: - { - STRICT_CHECK(ch != LF); - - if (parser->flags & F_TRAILING) { - /* End of a chunked request */ - parser->state = NEW_MESSAGE(); - CALLBACK_NOTIFY(message_complete); - break; - } - - parser->state = s_headers_done; - - /* Set this here so that on_headers_complete() callbacks can see it */ - parser->upgrade = - (parser->flags & F_UPGRADE || parser->method == HTTP_CONNECT); - - /* Here we call the headers_complete callback. This is somewhat - * different than other callbacks because if the user returns 1, we - * will interpret that as saying that this message has no body. This - * is needed for the annoying case of recieving a response to a HEAD - * request. - * - * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so - * we have to simulate it by handling a change in errno below. - */ - if (settings->on_headers_complete) { - switch (settings->on_headers_complete(parser)) { - case 0: - break; - - case 1: - parser->flags |= F_SKIPBODY; - break; - - default: - SET_ERRNO(HPE_CB_headers_complete); - return p - data; /* Error */ - } - } - - if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { - return p - data; - } - - goto reexecute_byte; - } - - case s_headers_done: - { - STRICT_CHECK(ch != LF); - - parser->nread = 0; - - /* Exit, the rest of the connect is in a different protocol. */ - if (parser->upgrade) { - parser->state = NEW_MESSAGE(); - CALLBACK_NOTIFY(message_complete); - return (p - data) + 1; - } - - if (parser->flags & F_SKIPBODY) { - parser->state = NEW_MESSAGE(); - CALLBACK_NOTIFY(message_complete); - } else if (parser->flags & F_CHUNKED) { - /* chunked encoding - ignore Content-Length header */ - parser->state = s_chunk_size_start; - } else { - if (parser->content_length == 0) { - /* Content-Length header given but zero: Content-Length: 0\r\n */ - parser->state = NEW_MESSAGE(); - CALLBACK_NOTIFY(message_complete); - } else if (parser->content_length != ULLONG_MAX) { - /* Content-Length header given and non-zero */ - parser->state = s_body_identity; - } else { - if (parser->type == HTTP_REQUEST || - !http_message_needs_eof(parser)) { - /* Assume content-length 0 - read the next */ - parser->state = NEW_MESSAGE(); - CALLBACK_NOTIFY(message_complete); - } else { - /* Read body until EOF */ - parser->state = s_body_identity_eof; - } - } - } - - break; - } - - case s_body_identity: - { - uint64_t to_read = MIN(parser->content_length, - (uint64_t) ((data + len) - p)); - - assert(parser->content_length != 0 - && parser->content_length != ULLONG_MAX); - - /* The difference between advancing content_length and p is because - * the latter will automaticaly advance on the next loop iteration. - * Further, if content_length ends up at 0, we want to see the last - * byte again for our message complete callback. - */ - MARK(body); - parser->content_length -= to_read; - p += to_read - 1; - - if (parser->content_length == 0) { - parser->state = s_message_done; - - /* Mimic CALLBACK_DATA_NOADVANCE() but with one extra byte. - * - * The alternative to doing this is to wait for the next byte to - * trigger the data callback, just as in every other case. The - * problem with this is that this makes it difficult for the test - * harness to distinguish between complete-on-EOF and - * complete-on-length. It's not clear that this distinction is - * important for applications, but let's keep it for now. - */ - CALLBACK_DATA_(body, p - body_mark + 1, p - data); - goto reexecute_byte; - } - - break; - } - - /* read until EOF */ - case s_body_identity_eof: - MARK(body); - p = data + len - 1; - - break; - - case s_message_done: - parser->state = NEW_MESSAGE(); - CALLBACK_NOTIFY(message_complete); - break; - - case s_chunk_size_start: - { - assert(parser->nread == 1); - assert(parser->flags & F_CHUNKED); - - unhex_val = unhex[(unsigned char)ch]; - if (unhex_val == -1) { - SET_ERRNO(HPE_INVALID_CHUNK_SIZE); - goto error; - } - - parser->content_length = unhex_val; - parser->state = s_chunk_size; - break; - } - - case s_chunk_size: - { - uint64_t t; - - assert(parser->flags & F_CHUNKED); - - if (ch == CR) { - parser->state = s_chunk_size_almost_done; - break; - } - - unhex_val = unhex[(unsigned char)ch]; - - if (unhex_val == -1) { - if (ch == ';' || ch == ' ') { - parser->state = s_chunk_parameters; - break; - } - - SET_ERRNO(HPE_INVALID_CHUNK_SIZE); - goto error; - } - - t = parser->content_length; - t *= 16; - t += unhex_val; - - /* Overflow? */ - if (t < parser->content_length || t == ULLONG_MAX) { - SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); - goto error; - } - - parser->content_length = t; - break; - } - - case s_chunk_parameters: - { - assert(parser->flags & F_CHUNKED); - /* just ignore this. TODO check for overflow */ - if (ch == CR) { - parser->state = s_chunk_size_almost_done; - break; - } - break; - } - - case s_chunk_size_almost_done: - { - assert(parser->flags & F_CHUNKED); - STRICT_CHECK(ch != LF); - - parser->nread = 0; - - if (parser->content_length == 0) { - parser->flags |= F_TRAILING; - parser->state = s_header_field_start; - } else { - parser->state = s_chunk_data; - } - break; - } - - case s_chunk_data: - { - uint64_t to_read = MIN(parser->content_length, - (uint64_t) ((data + len) - p)); - - assert(parser->flags & F_CHUNKED); - assert(parser->content_length != 0 - && parser->content_length != ULLONG_MAX); - - /* See the explanation in s_body_identity for why the content - * length and data pointers are managed this way. - */ - MARK(body); - parser->content_length -= to_read; - p += to_read - 1; - - if (parser->content_length == 0) { - parser->state = s_chunk_data_almost_done; - } - - break; - } - - case s_chunk_data_almost_done: - assert(parser->flags & F_CHUNKED); - assert(parser->content_length == 0); - STRICT_CHECK(ch != CR); - parser->state = s_chunk_data_done; - CALLBACK_DATA(body); - break; - - case s_chunk_data_done: - assert(parser->flags & F_CHUNKED); - STRICT_CHECK(ch != LF); - parser->nread = 0; - parser->state = s_chunk_size_start; - break; - - default: - assert(0 && "unhandled state"); - SET_ERRNO(HPE_INVALID_INTERNAL_STATE); - goto error; - } - } - - /* Run callbacks for any marks that we have leftover after we ran our of - * bytes. There should be at most one of these set, so it's OK to invoke - * them in series (unset marks will not result in callbacks). - * - * We use the NOADVANCE() variety of callbacks here because 'p' has already - * overflowed 'data' and this allows us to correct for the off-by-one that - * we'd otherwise have (since CALLBACK_DATA() is meant to be run with a 'p' - * value that's in-bounds). - */ - - assert(((header_field_mark ? 1 : 0) + - (header_value_mark ? 1 : 0) + - (url_mark ? 1 : 0) + - (body_mark ? 1 : 0)) <= 1); - - CALLBACK_DATA_NOADVANCE(header_field); - CALLBACK_DATA_NOADVANCE(header_value); - CALLBACK_DATA_NOADVANCE(url); - CALLBACK_DATA_NOADVANCE(body); - - return len; - -error: - if (HTTP_PARSER_ERRNO(parser) == HPE_OK) { - SET_ERRNO(HPE_UNKNOWN); - } - - return (p - data); -} - - -/* Does the parser need to see an EOF to find the end of the message? */ -int -http_message_needs_eof (const http_parser *parser) -{ - if (parser->type == HTTP_REQUEST) { - return 0; - } - - /* See RFC 2616 section 4.4 */ - if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */ - parser->status_code == 204 || /* No Content */ - parser->status_code == 304 || /* Not Modified */ - parser->flags & F_SKIPBODY) { /* response to a HEAD request */ - return 0; - } - - if ((parser->flags & F_CHUNKED) || parser->content_length != ULLONG_MAX) { - return 0; - } - - return 1; -} - - -int -http_should_keep_alive (const http_parser *parser) -{ - if (parser->http_major > 0 && parser->http_minor > 0) { - /* HTTP/1.1 */ - if (parser->flags & F_CONNECTION_CLOSE) { - return 0; - } - } else { - /* HTTP/1.0 or earlier */ - if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) { - return 0; - } - } - - return !http_message_needs_eof(parser); -} - - -const char * -http_method_str (enum http_method m) -{ - return ELEM_AT(method_strings, m, ""); -} - - -void -http_parser_init (http_parser *parser, enum http_parser_type t) -{ - void *data = parser->data; /* preserve application data */ - memset(parser, 0, sizeof(*parser)); - parser->data = data; - parser->type = t; - parser->state = (t == HTTP_REQUEST ? s_start_req : (t == HTTP_RESPONSE ? s_start_res : s_start_req_or_res)); - parser->http_errno = HPE_OK; -} - -const char * -http_errno_name(enum http_errno err) { - assert(err < (sizeof(http_strerror_tab)/sizeof(http_strerror_tab[0]))); - return http_strerror_tab[err].name; -} - -const char * -http_errno_description(enum http_errno err) { - assert(err < (sizeof(http_strerror_tab)/sizeof(http_strerror_tab[0]))); - return http_strerror_tab[err].description; -} - -static enum http_host_state -http_parse_host_char(enum http_host_state s, const char ch) { - switch(s) { - case s_http_userinfo: - case s_http_userinfo_start: - if (ch == '@') { - return s_http_host_start; - } - - if (IS_USERINFO_CHAR(ch)) { - return s_http_userinfo; - } - break; - - case s_http_host_start: - if (ch == '[') { - return s_http_host_v6_start; - } - - if (IS_HOST_CHAR(ch)) { - return s_http_host; - } - - break; - - case s_http_host: - if (IS_HOST_CHAR(ch)) { - return s_http_host; - } - - /* FALLTHROUGH */ - case s_http_host_v6_end: - if (ch == ':') { - return s_http_host_port_start; - } - - break; - - case s_http_host_v6: - if (ch == ']') { - return s_http_host_v6_end; - } - - /* FALLTHROUGH */ - case s_http_host_v6_start: - if (IS_HEX(ch) || ch == ':') { - return s_http_host_v6; - } - - break; - - case s_http_host_port: - case s_http_host_port_start: - if (IS_NUM(ch)) { - return s_http_host_port; - } - - break; - - default: - break; - } - return s_http_host_dead; -} - -static int -http_parse_host(const char * buf, struct http_parser_url *u, int found_at) { - enum http_host_state s; - - const char *p; - size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len; - - u->field_data[UF_HOST].len = 0; - - s = found_at ? s_http_userinfo_start : s_http_host_start; - - for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) { - enum http_host_state new_s = http_parse_host_char(s, *p); - - if (new_s == s_http_host_dead) { - return 1; - } - - switch(new_s) { - case s_http_host: - if (s != s_http_host) { - u->field_data[UF_HOST].off = p - buf; - } - u->field_data[UF_HOST].len++; - break; - - case s_http_host_v6: - if (s != s_http_host_v6) { - u->field_data[UF_HOST].off = p - buf; - } - u->field_data[UF_HOST].len++; - break; - - case s_http_host_port: - if (s != s_http_host_port) { - u->field_data[UF_PORT].off = p - buf; - u->field_data[UF_PORT].len = 0; - u->field_set |= (1 << UF_PORT); - } - u->field_data[UF_PORT].len++; - break; - - case s_http_userinfo: - if (s != s_http_userinfo) { - u->field_data[UF_USERINFO].off = p - buf ; - u->field_data[UF_USERINFO].len = 0; - u->field_set |= (1 << UF_USERINFO); - } - u->field_data[UF_USERINFO].len++; - break; - - default: - break; - } - s = new_s; - } - - /* Make sure we don't end somewhere unexpected */ - switch (s) { - case s_http_host_start: - case s_http_host_v6_start: - case s_http_host_v6: - case s_http_host_port_start: - case s_http_userinfo: - case s_http_userinfo_start: - return 1; - default: - break; - } - - return 0; -} - -int -http_parser_parse_url(const char *buf, size_t buflen, int is_connect, - struct http_parser_url *u) -{ - enum state s; - const char *p; - enum http_parser_url_fields uf, old_uf; - int found_at = 0; - - u->port = u->field_set = 0; - s = is_connect ? s_req_server_start : s_req_spaces_before_url; - uf = old_uf = UF_MAX; - - for (p = buf; p < buf + buflen; p++) { - s = parse_url_char(s, *p); - - /* Figure out the next field that we're operating on */ - switch (s) { - case s_dead: - return 1; - - /* Skip delimeters */ - case s_req_schema_slash: - case s_req_schema_slash_slash: - case s_req_server_start: - case s_req_query_string_start: - case s_req_fragment_start: - continue; - - case s_req_schema: - uf = UF_SCHEMA; - break; - - case s_req_server_with_at: - found_at = 1; - - /* FALLTROUGH */ - case s_req_server: - uf = UF_HOST; - break; - - case s_req_path: - uf = UF_PATH; - break; - - case s_req_query_string: - uf = UF_QUERY; - break; - - case s_req_fragment: - uf = UF_FRAGMENT; - break; - - default: - assert(!"Unexpected state"); - return 1; - } - - /* Nothing's changed; soldier on */ - if (uf == old_uf) { - u->field_data[uf].len++; - continue; - } - - u->field_data[uf].off = p - buf; - u->field_data[uf].len = 1; - - u->field_set |= (1 << uf); - old_uf = uf; - } - - /* host must be present if there is a schema */ - /* parsing http:///toto will fail */ - if ((u->field_set & ((1 << UF_SCHEMA) | (1 << UF_HOST))) != 0) { - if (http_parse_host(buf, u, found_at) != 0) { - return 1; - } - } - - /* CONNECT requests can only contain "hostname:port" */ - if (is_connect && u->field_set != ((1 << UF_HOST)|(1 << UF_PORT))) { - return 1; - } - - if (u->field_set & (1 << UF_PORT)) { - /* Don't bother with endp; we've already validated the string */ - unsigned long v = strtoul(buf + u->field_data[UF_PORT].off, NULL, 10); - - /* Ports have a max value of 2^16 */ - if (v > 0xffff) { - return 1; - } - - u->port = (uint16_t) v; - } - - return 0; -} - -void -http_parser_pause(http_parser *parser, int paused) { - /* Users should only be pausing/unpausing a parser that is not in an error - * state. In non-debug builds, there's not much that we can do about this - * other than ignore it. - */ - if (HTTP_PARSER_ERRNO(parser) == HPE_OK || - HTTP_PARSER_ERRNO(parser) == HPE_PAUSED) { - SET_ERRNO((paused) ? HPE_PAUSED : HPE_OK); - } else { - assert(0 && "Attempting to pause parser in error state"); - } -} - -int -http_body_is_final(const struct http_parser *parser) { - return parser->state == s_message_done; -} diff --git a/deps/http-parser/http_parser.h b/deps/http-parser/http_parser.h deleted file mode 100644 index 4f20396c64e..00000000000 --- a/deps/http-parser/http_parser.h +++ /dev/null @@ -1,303 +0,0 @@ -/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ -#ifndef http_parser_h -#define http_parser_h -#ifdef __cplusplus -extern "C" { -#endif - -#define HTTP_PARSER_VERSION_MAJOR 2 -#define HTTP_PARSER_VERSION_MINOR 0 - -#include -#if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600) -#include -typedef __int8 int8_t; -typedef unsigned __int8 uint8_t; -typedef __int16 int16_t; -typedef unsigned __int16 uint16_t; -typedef __int32 int32_t; -typedef unsigned __int32 uint32_t; -typedef __int64 int64_t; -typedef unsigned __int64 uint64_t; -typedef SIZE_T size_t; -typedef SSIZE_T ssize_t; -#else -#include -#endif - -/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run - * faster - */ -#ifndef HTTP_PARSER_STRICT -# define HTTP_PARSER_STRICT 1 -#endif - -/* Maximium header size allowed */ -#define HTTP_MAX_HEADER_SIZE (80*1024) - - -typedef struct http_parser http_parser; -typedef struct http_parser_settings http_parser_settings; - - -/* Callbacks should return non-zero to indicate an error. The parser will - * then halt execution. - * - * The one exception is on_headers_complete. In a HTTP_RESPONSE parser - * returning '1' from on_headers_complete will tell the parser that it - * should not expect a body. This is used when receiving a response to a - * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding: - * chunked' headers that indicate the presence of a body. - * - * http_data_cb does not return data chunks. It will be call arbitrarally - * many times for each string. E.G. you might get 10 callbacks for "on_url" - * each providing just a few characters more data. - */ -typedef int (*http_data_cb) (http_parser*, const char *at, size_t length); -typedef int (*http_cb) (http_parser*); - - -/* Request Methods */ -#define HTTP_METHOD_MAP(XX) \ - XX(0, DELETE, DELETE) \ - XX(1, GET, GET) \ - XX(2, HEAD, HEAD) \ - XX(3, POST, POST) \ - XX(4, PUT, PUT) \ - /* pathological */ \ - XX(5, CONNECT, CONNECT) \ - XX(6, OPTIONS, OPTIONS) \ - XX(7, TRACE, TRACE) \ - /* webdav */ \ - XX(8, COPY, COPY) \ - XX(9, LOCK, LOCK) \ - XX(10, MKCOL, MKCOL) \ - XX(11, MOVE, MOVE) \ - XX(12, PROPFIND, PROPFIND) \ - XX(13, PROPPATCH, PROPPATCH) \ - XX(14, SEARCH, SEARCH) \ - XX(15, UNLOCK, UNLOCK) \ - /* subversion */ \ - XX(16, REPORT, REPORT) \ - XX(17, MKACTIVITY, MKACTIVITY) \ - XX(18, CHECKOUT, CHECKOUT) \ - XX(19, MERGE, MERGE) \ - /* upnp */ \ - XX(20, MSEARCH, M-SEARCH) \ - XX(21, NOTIFY, NOTIFY) \ - XX(22, SUBSCRIBE, SUBSCRIBE) \ - XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \ - /* RFC-5789 */ \ - XX(24, PATCH, PATCH) \ - XX(25, PURGE, PURGE) \ - -enum http_method - { -#define XX(num, name, string) HTTP_##name = num, - HTTP_METHOD_MAP(XX) -#undef XX - }; - - -enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH }; - - -/* Flag values for http_parser.flags field */ -enum flags - { F_CHUNKED = 1 << 0 - , F_CONNECTION_KEEP_ALIVE = 1 << 1 - , F_CONNECTION_CLOSE = 1 << 2 - , F_TRAILING = 1 << 3 - , F_UPGRADE = 1 << 4 - , F_SKIPBODY = 1 << 5 - }; - - -/* Map for errno-related constants - * - * The provided argument should be a macro that takes 2 arguments. - */ -#define HTTP_ERRNO_MAP(XX) \ - /* No error */ \ - XX(OK, "success") \ - \ - /* Callback-related errors */ \ - XX(CB_message_begin, "the on_message_begin callback failed") \ - XX(CB_url, "the on_url callback failed") \ - XX(CB_header_field, "the on_header_field callback failed") \ - XX(CB_header_value, "the on_header_value callback failed") \ - XX(CB_headers_complete, "the on_headers_complete callback failed") \ - XX(CB_body, "the on_body callback failed") \ - XX(CB_message_complete, "the on_message_complete callback failed") \ - \ - /* Parsing-related errors */ \ - XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \ - XX(HEADER_OVERFLOW, \ - "too many header bytes seen; overflow detected") \ - XX(CLOSED_CONNECTION, \ - "data received after completed connection: close message") \ - XX(INVALID_VERSION, "invalid HTTP version") \ - XX(INVALID_STATUS, "invalid HTTP status code") \ - XX(INVALID_METHOD, "invalid HTTP method") \ - XX(INVALID_URL, "invalid URL") \ - XX(INVALID_HOST, "invalid host") \ - XX(INVALID_PORT, "invalid port") \ - XX(INVALID_PATH, "invalid path") \ - XX(INVALID_QUERY_STRING, "invalid query string") \ - XX(INVALID_FRAGMENT, "invalid fragment") \ - XX(LF_EXPECTED, "LF character expected") \ - XX(INVALID_HEADER_TOKEN, "invalid character in header") \ - XX(INVALID_CONTENT_LENGTH, \ - "invalid character in content-length header") \ - XX(INVALID_CHUNK_SIZE, \ - "invalid character in chunk size header") \ - XX(INVALID_CONSTANT, "invalid constant string") \ - XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\ - XX(STRICT, "strict mode assertion failed") \ - XX(PAUSED, "parser is paused") \ - XX(UNKNOWN, "an unknown error occurred") - - -/* Define HPE_* values for each errno value above */ -#define HTTP_ERRNO_GEN(n, s) HPE_##n, -enum http_errno { - HTTP_ERRNO_MAP(HTTP_ERRNO_GEN) -}; -#undef HTTP_ERRNO_GEN - - -/* Get an http_errno value from an http_parser */ -#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno) - - -struct http_parser { - /** PRIVATE **/ - unsigned char type : 2; /* enum http_parser_type */ - unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */ - unsigned char state; /* enum state from http_parser.c */ - unsigned char header_state; /* enum header_state from http_parser.c */ - unsigned char index; /* index into current matcher */ - - uint32_t nread; /* # bytes read in various scenarios */ - uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */ - - /** READ-ONLY **/ - unsigned short http_major; - unsigned short http_minor; - unsigned short status_code; /* responses only */ - unsigned char method; /* requests only */ - unsigned char http_errno : 7; - - /* 1 = Upgrade header was present and the parser has exited because of that. - * 0 = No upgrade header present. - * Should be checked when http_parser_execute() returns in addition to - * error checking. - */ - unsigned char upgrade : 1; - - /** PUBLIC **/ - void *data; /* A pointer to get hook to the "connection" or "socket" object */ -}; - - -struct http_parser_settings { - http_cb on_message_begin; - http_data_cb on_url; - http_data_cb on_header_field; - http_data_cb on_header_value; - http_cb on_headers_complete; - http_data_cb on_body; - http_cb on_message_complete; -}; - - -enum http_parser_url_fields - { UF_SCHEMA = 0 - , UF_HOST = 1 - , UF_PORT = 2 - , UF_PATH = 3 - , UF_QUERY = 4 - , UF_FRAGMENT = 5 - , UF_USERINFO = 6 - , UF_MAX = 7 - }; - - -/* Result structure for http_parser_parse_url(). - * - * Callers should index into field_data[] with UF_* values iff field_set - * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and - * because we probably have padding left over), we convert any port to - * a uint16_t. - */ -struct http_parser_url { - uint16_t field_set; /* Bitmask of (1 << UF_*) values */ - uint16_t port; /* Converted UF_PORT string */ - - struct { - uint16_t off; /* Offset into buffer in which field starts */ - uint16_t len; /* Length of run in buffer */ - } field_data[UF_MAX]; -}; - - -void http_parser_init(http_parser *parser, enum http_parser_type type); - - -size_t http_parser_execute(http_parser *parser, - const http_parser_settings *settings, - const char *data, - size_t len); - - -/* If http_should_keep_alive() in the on_headers_complete or - * on_message_complete callback returns 0, then this should be - * the last message on the connection. - * If you are the server, respond with the "Connection: close" header. - * If you are the client, close the connection. - */ -int http_should_keep_alive(const http_parser *parser); - -/* Returns a string version of the HTTP method. */ -const char *http_method_str(enum http_method m); - -/* Return a string name of the given error */ -const char *http_errno_name(enum http_errno err); - -/* Return a string description of the given error */ -const char *http_errno_description(enum http_errno err); - -/* Parse a URL; return nonzero on failure */ -int http_parser_parse_url(const char *buf, size_t buflen, - int is_connect, - struct http_parser_url *u); - -/* Pause or un-pause the parser; a nonzero value pauses */ -void http_parser_pause(http_parser *parser, int paused); - -/* Checks if this is the final chunk of the body. */ -int http_body_is_final(const http_parser *parser); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/deps/llhttp/CMakeLists.txt b/deps/llhttp/CMakeLists.txt new file mode 100644 index 00000000000..6965335ab27 --- /dev/null +++ b/deps/llhttp/CMakeLists.txt @@ -0,0 +1,8 @@ +file(GLOB SRC_LLHTTP "*.c" "*.h") +list(SORT SRC_LLHTTP) + +add_library(llhttp OBJECT ${SRC_LLHTTP}) + +if(NOT MSVC) + set_source_files_properties(api.c http.c llhttp.c PROPERTIES COMPILE_FLAGS "-Wno-unused-parameter -Wno-missing-declarations") +endif() diff --git a/deps/llhttp/LICENSE-MIT b/deps/llhttp/LICENSE-MIT new file mode 100644 index 00000000000..6c1512dd6bc --- /dev/null +++ b/deps/llhttp/LICENSE-MIT @@ -0,0 +1,22 @@ +This software is licensed under the MIT License. + +Copyright Fedor Indutny, 2018. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to permit +persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/deps/llhttp/api.c b/deps/llhttp/api.c new file mode 100644 index 00000000000..eddb478315a --- /dev/null +++ b/deps/llhttp/api.c @@ -0,0 +1,510 @@ +#include +#include +#include + +#include "llhttp.h" + +#define CALLBACK_MAYBE(PARSER, NAME) \ + do { \ + const llhttp_settings_t* settings; \ + settings = (const llhttp_settings_t*) (PARSER)->settings; \ + if (settings == NULL || settings->NAME == NULL) { \ + err = 0; \ + break; \ + } \ + err = settings->NAME((PARSER)); \ + } while (0) + +#define SPAN_CALLBACK_MAYBE(PARSER, NAME, START, LEN) \ + do { \ + const llhttp_settings_t* settings; \ + settings = (const llhttp_settings_t*) (PARSER)->settings; \ + if (settings == NULL || settings->NAME == NULL) { \ + err = 0; \ + break; \ + } \ + err = settings->NAME((PARSER), (START), (LEN)); \ + if (err == -1) { \ + err = HPE_USER; \ + llhttp_set_error_reason((PARSER), "Span callback error in " #NAME); \ + } \ + } while (0) + +void llhttp_init(llhttp_t* parser, llhttp_type_t type, + const llhttp_settings_t* settings) { + llhttp__internal_init(parser); + + parser->type = type; + parser->settings = (void*) settings; +} + + +#if defined(__wasm__) + +extern int wasm_on_message_begin(llhttp_t * p); +extern int wasm_on_url(llhttp_t* p, const char* at, size_t length); +extern int wasm_on_status(llhttp_t* p, const char* at, size_t length); +extern int wasm_on_header_field(llhttp_t* p, const char* at, size_t length); +extern int wasm_on_header_value(llhttp_t* p, const char* at, size_t length); +extern int wasm_on_headers_complete(llhttp_t * p, int status_code, + uint8_t upgrade, int should_keep_alive); +extern int wasm_on_body(llhttp_t* p, const char* at, size_t length); +extern int wasm_on_message_complete(llhttp_t * p); + +static int wasm_on_headers_complete_wrap(llhttp_t* p) { + return wasm_on_headers_complete(p, p->status_code, p->upgrade, + llhttp_should_keep_alive(p)); +} + +const llhttp_settings_t wasm_settings = { + wasm_on_message_begin, + wasm_on_url, + wasm_on_status, + NULL, + NULL, + wasm_on_header_field, + wasm_on_header_value, + NULL, + NULL, + wasm_on_headers_complete_wrap, + wasm_on_body, + wasm_on_message_complete, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, +}; + + +llhttp_t* llhttp_alloc(llhttp_type_t type) { + llhttp_t* parser = malloc(sizeof(llhttp_t)); + llhttp_init(parser, type, &wasm_settings); + return parser; +} + +void llhttp_free(llhttp_t* parser) { + free(parser); +} + +#endif /* defined(__wasm__) */ + +/* Some getters required to get stuff from the parser */ + +uint8_t llhttp_get_type(llhttp_t* parser) { + return parser->type; +} + +uint8_t llhttp_get_http_major(llhttp_t* parser) { + return parser->http_major; +} + +uint8_t llhttp_get_http_minor(llhttp_t* parser) { + return parser->http_minor; +} + +uint8_t llhttp_get_method(llhttp_t* parser) { + return parser->method; +} + +int llhttp_get_status_code(llhttp_t* parser) { + return parser->status_code; +} + +uint8_t llhttp_get_upgrade(llhttp_t* parser) { + return parser->upgrade; +} + + +void llhttp_reset(llhttp_t* parser) { + llhttp_type_t type = parser->type; + const llhttp_settings_t* settings = parser->settings; + void* data = parser->data; + uint16_t lenient_flags = parser->lenient_flags; + + llhttp__internal_init(parser); + + parser->type = type; + parser->settings = (void*) settings; + parser->data = data; + parser->lenient_flags = lenient_flags; +} + + +llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len) { + return llhttp__internal_execute(parser, data, data + len); +} + + +void llhttp_settings_init(llhttp_settings_t* settings) { + memset(settings, 0, sizeof(*settings)); +} + + +llhttp_errno_t llhttp_finish(llhttp_t* parser) { + int err; + + /* We're in an error state. Don't bother doing anything. */ + if (parser->error != 0) { + return 0; + } + + switch (parser->finish) { + case HTTP_FINISH_SAFE_WITH_CB: + CALLBACK_MAYBE(parser, on_message_complete); + if (err != HPE_OK) return err; + + /* FALLTHROUGH */ + case HTTP_FINISH_SAFE: + return HPE_OK; + case HTTP_FINISH_UNSAFE: + parser->reason = "Invalid EOF state"; + return HPE_INVALID_EOF_STATE; + default: + abort(); + } +} + + +void llhttp_pause(llhttp_t* parser) { + if (parser->error != HPE_OK) { + return; + } + + parser->error = HPE_PAUSED; + parser->reason = "Paused"; +} + + +void llhttp_resume(llhttp_t* parser) { + if (parser->error != HPE_PAUSED) { + return; + } + + parser->error = 0; +} + + +void llhttp_resume_after_upgrade(llhttp_t* parser) { + if (parser->error != HPE_PAUSED_UPGRADE) { + return; + } + + parser->error = 0; +} + + +llhttp_errno_t llhttp_get_errno(const llhttp_t* parser) { + return parser->error; +} + + +const char* llhttp_get_error_reason(const llhttp_t* parser) { + return parser->reason; +} + + +void llhttp_set_error_reason(llhttp_t* parser, const char* reason) { + parser->reason = reason; +} + + +const char* llhttp_get_error_pos(const llhttp_t* parser) { + return parser->error_pos; +} + + +const char* llhttp_errno_name(llhttp_errno_t err) { +#define HTTP_ERRNO_GEN(CODE, NAME, _) case HPE_##NAME: return "HPE_" #NAME; + switch (err) { + HTTP_ERRNO_MAP(HTTP_ERRNO_GEN) + default: abort(); + } +#undef HTTP_ERRNO_GEN +} + + +const char* llhttp_method_name(llhttp_method_t method) { +#define HTTP_METHOD_GEN(NUM, NAME, STRING) case HTTP_##NAME: return #STRING; + switch (method) { + HTTP_ALL_METHOD_MAP(HTTP_METHOD_GEN) + default: abort(); + } +#undef HTTP_METHOD_GEN +} + +const char* llhttp_status_name(llhttp_status_t status) { +#define HTTP_STATUS_GEN(NUM, NAME, STRING) case HTTP_STATUS_##NAME: return #STRING; + switch (status) { + HTTP_STATUS_MAP(HTTP_STATUS_GEN) + default: abort(); + } +#undef HTTP_STATUS_GEN +} + + +void llhttp_set_lenient_headers(llhttp_t* parser, int enabled) { + if (enabled) { + parser->lenient_flags |= LENIENT_HEADERS; + } else { + parser->lenient_flags &= ~LENIENT_HEADERS; + } +} + + +void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled) { + if (enabled) { + parser->lenient_flags |= LENIENT_CHUNKED_LENGTH; + } else { + parser->lenient_flags &= ~LENIENT_CHUNKED_LENGTH; + } +} + + +void llhttp_set_lenient_keep_alive(llhttp_t* parser, int enabled) { + if (enabled) { + parser->lenient_flags |= LENIENT_KEEP_ALIVE; + } else { + parser->lenient_flags &= ~LENIENT_KEEP_ALIVE; + } +} + +void llhttp_set_lenient_transfer_encoding(llhttp_t* parser, int enabled) { + if (enabled) { + parser->lenient_flags |= LENIENT_TRANSFER_ENCODING; + } else { + parser->lenient_flags &= ~LENIENT_TRANSFER_ENCODING; + } +} + +void llhttp_set_lenient_version(llhttp_t* parser, int enabled) { + if (enabled) { + parser->lenient_flags |= LENIENT_VERSION; + } else { + parser->lenient_flags &= ~LENIENT_VERSION; + } +} + +void llhttp_set_lenient_data_after_close(llhttp_t* parser, int enabled) { + if (enabled) { + parser->lenient_flags |= LENIENT_DATA_AFTER_CLOSE; + } else { + parser->lenient_flags &= ~LENIENT_DATA_AFTER_CLOSE; + } +} + +void llhttp_set_lenient_optional_lf_after_cr(llhttp_t* parser, int enabled) { + if (enabled) { + parser->lenient_flags |= LENIENT_OPTIONAL_LF_AFTER_CR; + } else { + parser->lenient_flags &= ~LENIENT_OPTIONAL_LF_AFTER_CR; + } +} + +void llhttp_set_lenient_optional_crlf_after_chunk(llhttp_t* parser, int enabled) { + if (enabled) { + parser->lenient_flags |= LENIENT_OPTIONAL_CRLF_AFTER_CHUNK; + } else { + parser->lenient_flags &= ~LENIENT_OPTIONAL_CRLF_AFTER_CHUNK; + } +} + +void llhttp_set_lenient_optional_cr_before_lf(llhttp_t* parser, int enabled) { + if (enabled) { + parser->lenient_flags |= LENIENT_OPTIONAL_CR_BEFORE_LF; + } else { + parser->lenient_flags &= ~LENIENT_OPTIONAL_CR_BEFORE_LF; + } +} + +void llhttp_set_lenient_spaces_after_chunk_size(llhttp_t* parser, int enabled) { + if (enabled) { + parser->lenient_flags |= LENIENT_SPACES_AFTER_CHUNK_SIZE; + } else { + parser->lenient_flags &= ~LENIENT_SPACES_AFTER_CHUNK_SIZE; + } +} + +/* Callbacks */ + + +int llhttp__on_message_begin(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_message_begin); + return err; +} + + +int llhttp__on_url(llhttp_t* s, const char* p, const char* endp) { + int err; + SPAN_CALLBACK_MAYBE(s, on_url, p, endp - p); + return err; +} + + +int llhttp__on_url_complete(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_url_complete); + return err; +} + + +int llhttp__on_status(llhttp_t* s, const char* p, const char* endp) { + int err; + SPAN_CALLBACK_MAYBE(s, on_status, p, endp - p); + return err; +} + + +int llhttp__on_status_complete(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_status_complete); + return err; +} + + +int llhttp__on_method(llhttp_t* s, const char* p, const char* endp) { + int err; + SPAN_CALLBACK_MAYBE(s, on_method, p, endp - p); + return err; +} + + +int llhttp__on_method_complete(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_method_complete); + return err; +} + + +int llhttp__on_version(llhttp_t* s, const char* p, const char* endp) { + int err; + SPAN_CALLBACK_MAYBE(s, on_version, p, endp - p); + return err; +} + + +int llhttp__on_version_complete(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_version_complete); + return err; +} + + +int llhttp__on_header_field(llhttp_t* s, const char* p, const char* endp) { + int err; + SPAN_CALLBACK_MAYBE(s, on_header_field, p, endp - p); + return err; +} + + +int llhttp__on_header_field_complete(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_header_field_complete); + return err; +} + + +int llhttp__on_header_value(llhttp_t* s, const char* p, const char* endp) { + int err; + SPAN_CALLBACK_MAYBE(s, on_header_value, p, endp - p); + return err; +} + + +int llhttp__on_header_value_complete(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_header_value_complete); + return err; +} + + +int llhttp__on_headers_complete(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_headers_complete); + return err; +} + + +int llhttp__on_message_complete(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_message_complete); + return err; +} + + +int llhttp__on_body(llhttp_t* s, const char* p, const char* endp) { + int err; + SPAN_CALLBACK_MAYBE(s, on_body, p, endp - p); + return err; +} + + +int llhttp__on_chunk_header(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_chunk_header); + return err; +} + + +int llhttp__on_chunk_extension_name(llhttp_t* s, const char* p, const char* endp) { + int err; + SPAN_CALLBACK_MAYBE(s, on_chunk_extension_name, p, endp - p); + return err; +} + + +int llhttp__on_chunk_extension_name_complete(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_chunk_extension_name_complete); + return err; +} + + +int llhttp__on_chunk_extension_value(llhttp_t* s, const char* p, const char* endp) { + int err; + SPAN_CALLBACK_MAYBE(s, on_chunk_extension_value, p, endp - p); + return err; +} + + +int llhttp__on_chunk_extension_value_complete(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_chunk_extension_value_complete); + return err; +} + + +int llhttp__on_chunk_complete(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_chunk_complete); + return err; +} + + +int llhttp__on_reset(llhttp_t* s, const char* p, const char* endp) { + int err; + CALLBACK_MAYBE(s, on_reset); + return err; +} + + +/* Private */ + + +void llhttp__debug(llhttp_t* s, const char* p, const char* endp, + const char* msg) { + if (p == endp) { + fprintf(stderr, "p=%p type=%d flags=%02x next=null debug=%s\n", s, s->type, + s->flags, msg); + } else { + fprintf(stderr, "p=%p type=%d flags=%02x next=%02x debug=%s\n", s, + s->type, s->flags, *p, msg); + } +} diff --git a/deps/llhttp/http.c b/deps/llhttp/http.c new file mode 100644 index 00000000000..1ab91a55796 --- /dev/null +++ b/deps/llhttp/http.c @@ -0,0 +1,170 @@ +#include +#ifndef LLHTTP__TEST +# include "llhttp.h" +#else +# define llhttp_t llparse_t +#endif /* */ + +int llhttp_message_needs_eof(const llhttp_t* parser); +int llhttp_should_keep_alive(const llhttp_t* parser); + +int llhttp__before_headers_complete(llhttp_t* parser, const char* p, + const char* endp) { + /* Set this here so that on_headers_complete() callbacks can see it */ + if ((parser->flags & F_UPGRADE) && + (parser->flags & F_CONNECTION_UPGRADE)) { + /* For responses, "Upgrade: foo" and "Connection: upgrade" are + * mandatory only when it is a 101 Switching Protocols response, + * otherwise it is purely informational, to announce support. + */ + parser->upgrade = + (parser->type == HTTP_REQUEST || parser->status_code == 101); + } else { + parser->upgrade = (parser->method == HTTP_CONNECT); + } + return 0; +} + + +/* Return values: + * 0 - No body, `restart`, message_complete + * 1 - CONNECT request, `restart`, message_complete, and pause + * 2 - chunk_size_start + * 3 - body_identity + * 4 - body_identity_eof + * 5 - invalid transfer-encoding for request + */ +int llhttp__after_headers_complete(llhttp_t* parser, const char* p, + const char* endp) { + int hasBody; + + hasBody = parser->flags & F_CHUNKED || parser->content_length > 0; + if ( + (parser->upgrade && (parser->method == HTTP_CONNECT || + (parser->flags & F_SKIPBODY) || !hasBody)) || + /* See RFC 2616 section 4.4 - 1xx e.g. Continue */ + (parser->type == HTTP_RESPONSE && parser->status_code == 101) + ) { + /* Exit, the rest of the message is in a different protocol. */ + return 1; + } + + if (parser->type == HTTP_RESPONSE && parser->status_code == 100) { + /* No body, restart as the message is complete */ + return 0; + } + + /* See RFC 2616 section 4.4 */ + if ( + parser->flags & F_SKIPBODY || /* response to a HEAD request */ + ( + parser->type == HTTP_RESPONSE && ( + parser->status_code == 102 || /* Processing */ + parser->status_code == 103 || /* Early Hints */ + parser->status_code == 204 || /* No Content */ + parser->status_code == 304 /* Not Modified */ + ) + ) + ) { + return 0; + } else if (parser->flags & F_CHUNKED) { + /* chunked encoding - ignore Content-Length header, prepare for a chunk */ + return 2; + } else if (parser->flags & F_TRANSFER_ENCODING) { + if (parser->type == HTTP_REQUEST && + (parser->lenient_flags & LENIENT_CHUNKED_LENGTH) == 0 && + (parser->lenient_flags & LENIENT_TRANSFER_ENCODING) == 0) { + /* RFC 7230 3.3.3 */ + + /* If a Transfer-Encoding header field + * is present in a request and the chunked transfer coding is not + * the final encoding, the message body length cannot be determined + * reliably; the server MUST respond with the 400 (Bad Request) + * status code and then close the connection. + */ + return 5; + } else { + /* RFC 7230 3.3.3 */ + + /* If a Transfer-Encoding header field is present in a response and + * the chunked transfer coding is not the final encoding, the + * message body length is determined by reading the connection until + * it is closed by the server. + */ + return 4; + } + } else { + if (!(parser->flags & F_CONTENT_LENGTH)) { + if (!llhttp_message_needs_eof(parser)) { + /* Assume content-length 0 - read the next */ + return 0; + } else { + /* Read body until EOF */ + return 4; + } + } else if (parser->content_length == 0) { + /* Content-Length header given but zero: Content-Length: 0\r\n */ + return 0; + } else { + /* Content-Length header given and non-zero */ + return 3; + } + } +} + + +int llhttp__after_message_complete(llhttp_t* parser, const char* p, + const char* endp) { + int should_keep_alive; + + should_keep_alive = llhttp_should_keep_alive(parser); + parser->finish = HTTP_FINISH_SAFE; + parser->flags = 0; + + /* NOTE: this is ignored in loose parsing mode */ + return should_keep_alive; +} + + +int llhttp_message_needs_eof(const llhttp_t* parser) { + if (parser->type == HTTP_REQUEST) { + return 0; + } + + /* See RFC 2616 section 4.4 */ + if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */ + parser->status_code == 204 || /* No Content */ + parser->status_code == 304 || /* Not Modified */ + (parser->flags & F_SKIPBODY)) { /* response to a HEAD request */ + return 0; + } + + /* RFC 7230 3.3.3, see `llhttp__after_headers_complete` */ + if ((parser->flags & F_TRANSFER_ENCODING) && + (parser->flags & F_CHUNKED) == 0) { + return 1; + } + + if (parser->flags & (F_CHUNKED | F_CONTENT_LENGTH)) { + return 0; + } + + return 1; +} + + +int llhttp_should_keep_alive(const llhttp_t* parser) { + if (parser->http_major > 0 && parser->http_minor > 0) { + /* HTTP/1.1 */ + if (parser->flags & F_CONNECTION_CLOSE) { + return 0; + } + } else { + /* HTTP/1.0 or earlier */ + if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) { + return 0; + } + } + + return !llhttp_message_needs_eof(parser); +} diff --git a/deps/llhttp/llhttp.c b/deps/llhttp/llhttp.c new file mode 100644 index 00000000000..3ef3b817f3d --- /dev/null +++ b/deps/llhttp/llhttp.c @@ -0,0 +1,10168 @@ +#include +#include +#include + +#ifdef __SSE4_2__ + #ifdef _MSC_VER + #include + #else /* !_MSC_VER */ + #include + #endif /* _MSC_VER */ +#endif /* __SSE4_2__ */ + +#ifdef _MSC_VER + #define ALIGN(n) _declspec(align(n)) +#else /* !_MSC_VER */ + #define ALIGN(n) __attribute__((aligned(n))) +#endif /* _MSC_VER */ + +#include "llhttp.h" + +typedef int (*llhttp__internal__span_cb)( + llhttp__internal_t*, const char*, const char*); + +static const unsigned char llparse_blob0[] = { + 'o', 'n' +}; +static const unsigned char llparse_blob1[] = { + 'e', 'c', 't', 'i', 'o', 'n' +}; +static const unsigned char llparse_blob2[] = { + 'l', 'o', 's', 'e' +}; +static const unsigned char llparse_blob3[] = { + 'e', 'e', 'p', '-', 'a', 'l', 'i', 'v', 'e' +}; +static const unsigned char llparse_blob4[] = { + 'p', 'g', 'r', 'a', 'd', 'e' +}; +static const unsigned char llparse_blob5[] = { + 'c', 'h', 'u', 'n', 'k', 'e', 'd' +}; +#ifdef __SSE4_2__ +static const unsigned char ALIGN(16) llparse_blob6[] = { + 0x9, 0x9, ' ', '~', 0x80, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0 +}; +#endif /* __SSE4_2__ */ +#ifdef __SSE4_2__ +static const unsigned char ALIGN(16) llparse_blob7[] = { + '!', '!', '#', '\'', '*', '+', '-', '.', '0', '9', 'A', + 'Z', '^', 'z', '|', '|' +}; +#endif /* __SSE4_2__ */ +#ifdef __SSE4_2__ +static const unsigned char ALIGN(16) llparse_blob8[] = { + '~', '~', 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0 +}; +#endif /* __SSE4_2__ */ +static const unsigned char llparse_blob9[] = { + 'e', 'n', 't', '-', 'l', 'e', 'n', 'g', 't', 'h' +}; +static const unsigned char llparse_blob10[] = { + 'r', 'o', 'x', 'y', '-', 'c', 'o', 'n', 'n', 'e', 'c', + 't', 'i', 'o', 'n' +}; +static const unsigned char llparse_blob11[] = { + 'r', 'a', 'n', 's', 'f', 'e', 'r', '-', 'e', 'n', 'c', + 'o', 'd', 'i', 'n', 'g' +}; +static const unsigned char llparse_blob12[] = { + 'p', 'g', 'r', 'a', 'd', 'e' +}; +static const unsigned char llparse_blob13[] = { + 'T', 'T', 'P', '/' +}; +static const unsigned char llparse_blob14[] = { + 0xd, 0xa, 0xd, 0xa, 'S', 'M', 0xd, 0xa, 0xd, 0xa +}; +static const unsigned char llparse_blob15[] = { + 'C', 'E', '/' +}; +static const unsigned char llparse_blob16[] = { + 'T', 'S', 'P', '/' +}; +static const unsigned char llparse_blob17[] = { + 'N', 'O', 'U', 'N', 'C', 'E' +}; +static const unsigned char llparse_blob18[] = { + 'I', 'N', 'D' +}; +static const unsigned char llparse_blob19[] = { + 'E', 'C', 'K', 'O', 'U', 'T' +}; +static const unsigned char llparse_blob20[] = { + 'N', 'E', 'C', 'T' +}; +static const unsigned char llparse_blob21[] = { + 'E', 'T', 'E' +}; +static const unsigned char llparse_blob22[] = { + 'C', 'R', 'I', 'B', 'E' +}; +static const unsigned char llparse_blob23[] = { + 'L', 'U', 'S', 'H' +}; +static const unsigned char llparse_blob24[] = { + 'E', 'T' +}; +static const unsigned char llparse_blob25[] = { + 'P', 'A', 'R', 'A', 'M', 'E', 'T', 'E', 'R' +}; +static const unsigned char llparse_blob26[] = { + 'E', 'A', 'D' +}; +static const unsigned char llparse_blob27[] = { + 'N', 'K' +}; +static const unsigned char llparse_blob28[] = { + 'C', 'K' +}; +static const unsigned char llparse_blob29[] = { + 'S', 'E', 'A', 'R', 'C', 'H' +}; +static const unsigned char llparse_blob30[] = { + 'R', 'G', 'E' +}; +static const unsigned char llparse_blob31[] = { + 'C', 'T', 'I', 'V', 'I', 'T', 'Y' +}; +static const unsigned char llparse_blob32[] = { + 'L', 'E', 'N', 'D', 'A', 'R' +}; +static const unsigned char llparse_blob33[] = { + 'V', 'E' +}; +static const unsigned char llparse_blob34[] = { + 'O', 'T', 'I', 'F', 'Y' +}; +static const unsigned char llparse_blob35[] = { + 'P', 'T', 'I', 'O', 'N', 'S' +}; +static const unsigned char llparse_blob36[] = { + 'C', 'H' +}; +static const unsigned char llparse_blob37[] = { + 'S', 'E' +}; +static const unsigned char llparse_blob38[] = { + 'A', 'Y' +}; +static const unsigned char llparse_blob39[] = { + 'S', 'T' +}; +static const unsigned char llparse_blob40[] = { + 'I', 'N', 'D' +}; +static const unsigned char llparse_blob41[] = { + 'A', 'T', 'C', 'H' +}; +static const unsigned char llparse_blob42[] = { + 'G', 'E' +}; +static const unsigned char llparse_blob43[] = { + 'U', 'E', 'R', 'Y' +}; +static const unsigned char llparse_blob44[] = { + 'I', 'N', 'D' +}; +static const unsigned char llparse_blob45[] = { + 'O', 'R', 'D' +}; +static const unsigned char llparse_blob46[] = { + 'I', 'R', 'E', 'C', 'T' +}; +static const unsigned char llparse_blob47[] = { + 'O', 'R', 'T' +}; +static const unsigned char llparse_blob48[] = { + 'R', 'C', 'H' +}; +static const unsigned char llparse_blob49[] = { + 'P', 'A', 'R', 'A', 'M', 'E', 'T', 'E', 'R' +}; +static const unsigned char llparse_blob50[] = { + 'U', 'R', 'C', 'E' +}; +static const unsigned char llparse_blob51[] = { + 'B', 'S', 'C', 'R', 'I', 'B', 'E' +}; +static const unsigned char llparse_blob52[] = { + 'A', 'R', 'D', 'O', 'W', 'N' +}; +static const unsigned char llparse_blob53[] = { + 'A', 'C', 'E' +}; +static const unsigned char llparse_blob54[] = { + 'I', 'N', 'D' +}; +static const unsigned char llparse_blob55[] = { + 'N', 'K' +}; +static const unsigned char llparse_blob56[] = { + 'C', 'K' +}; +static const unsigned char llparse_blob57[] = { + 'U', 'B', 'S', 'C', 'R', 'I', 'B', 'E' +}; +static const unsigned char llparse_blob58[] = { + 'H', 'T', 'T', 'P', '/' +}; +static const unsigned char llparse_blob59[] = { + 'A', 'D' +}; +static const unsigned char llparse_blob60[] = { + 'T', 'P', '/' +}; + +enum llparse_match_status_e { + kMatchComplete, + kMatchPause, + kMatchMismatch +}; +typedef enum llparse_match_status_e llparse_match_status_t; + +struct llparse_match_s { + llparse_match_status_t status; + const unsigned char* current; +}; +typedef struct llparse_match_s llparse_match_t; + +static llparse_match_t llparse__match_sequence_to_lower( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp, + const unsigned char* seq, uint32_t seq_len) { + uint32_t index; + llparse_match_t res; + + index = s->_index; + for (; p != endp; p++) { + unsigned char current; + + current = ((*p) >= 'A' && (*p) <= 'Z' ? (*p | 0x20) : (*p)); + if (current == seq[index]) { + if (++index == seq_len) { + res.status = kMatchComplete; + goto reset; + } + } else { + res.status = kMatchMismatch; + goto reset; + } + } + s->_index = index; + res.status = kMatchPause; + res.current = p; + return res; +reset: + s->_index = 0; + res.current = p; + return res; +} + +static llparse_match_t llparse__match_sequence_to_lower_unsafe( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp, + const unsigned char* seq, uint32_t seq_len) { + uint32_t index; + llparse_match_t res; + + index = s->_index; + for (; p != endp; p++) { + unsigned char current; + + current = ((*p) | 0x20); + if (current == seq[index]) { + if (++index == seq_len) { + res.status = kMatchComplete; + goto reset; + } + } else { + res.status = kMatchMismatch; + goto reset; + } + } + s->_index = index; + res.status = kMatchPause; + res.current = p; + return res; +reset: + s->_index = 0; + res.current = p; + return res; +} + +static llparse_match_t llparse__match_sequence_id( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp, + const unsigned char* seq, uint32_t seq_len) { + uint32_t index; + llparse_match_t res; + + index = s->_index; + for (; p != endp; p++) { + unsigned char current; + + current = *p; + if (current == seq[index]) { + if (++index == seq_len) { + res.status = kMatchComplete; + goto reset; + } + } else { + res.status = kMatchMismatch; + goto reset; + } + } + s->_index = index; + res.status = kMatchPause; + res.current = p; + return res; +reset: + s->_index = 0; + res.current = p; + return res; +} + +enum llparse_state_e { + s_error, + s_n_llhttp__internal__n_closed, + s_n_llhttp__internal__n_invoke_llhttp__after_message_complete, + s_n_llhttp__internal__n_pause_1, + s_n_llhttp__internal__n_invoke_is_equal_upgrade, + s_n_llhttp__internal__n_invoke_llhttp__on_message_complete_2, + s_n_llhttp__internal__n_chunk_data_almost_done_1, + s_n_llhttp__internal__n_chunk_data_almost_done, + s_n_llhttp__internal__n_consume_content_length, + s_n_llhttp__internal__n_span_start_llhttp__on_body, + s_n_llhttp__internal__n_invoke_is_equal_content_length, + s_n_llhttp__internal__n_chunk_size_almost_done, + s_n_llhttp__internal__n_invoke_test_lenient_flags_9, + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete, + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete_1, + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete_2, + s_n_llhttp__internal__n_invoke_test_lenient_flags_10, + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete, + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_1, + s_n_llhttp__internal__n_chunk_extension_quoted_value_done, + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_2, + s_n_llhttp__internal__n_error_30, + s_n_llhttp__internal__n_chunk_extension_quoted_value_quoted_pair, + s_n_llhttp__internal__n_error_31, + s_n_llhttp__internal__n_chunk_extension_quoted_value, + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_3, + s_n_llhttp__internal__n_error_33, + s_n_llhttp__internal__n_chunk_extension_value, + s_n_llhttp__internal__n_span_start_llhttp__on_chunk_extension_value, + s_n_llhttp__internal__n_error_34, + s_n_llhttp__internal__n_chunk_extension_name, + s_n_llhttp__internal__n_span_start_llhttp__on_chunk_extension_name, + s_n_llhttp__internal__n_chunk_extensions, + s_n_llhttp__internal__n_chunk_size_otherwise, + s_n_llhttp__internal__n_chunk_size, + s_n_llhttp__internal__n_chunk_size_digit, + s_n_llhttp__internal__n_invoke_update_content_length_1, + s_n_llhttp__internal__n_consume_content_length_1, + s_n_llhttp__internal__n_span_start_llhttp__on_body_1, + s_n_llhttp__internal__n_eof, + s_n_llhttp__internal__n_span_start_llhttp__on_body_2, + s_n_llhttp__internal__n_invoke_llhttp__after_headers_complete, + s_n_llhttp__internal__n_error_5, + s_n_llhttp__internal__n_headers_almost_done, + s_n_llhttp__internal__n_header_field_colon_discard_ws, + s_n_llhttp__internal__n_invoke_llhttp__on_header_value_complete, + s_n_llhttp__internal__n_span_start_llhttp__on_header_value, + s_n_llhttp__internal__n_header_value_discard_lws, + s_n_llhttp__internal__n_header_value_discard_ws_almost_done, + s_n_llhttp__internal__n_header_value_lws, + s_n_llhttp__internal__n_header_value_almost_done, + s_n_llhttp__internal__n_invoke_test_lenient_flags_17, + s_n_llhttp__internal__n_header_value_lenient, + s_n_llhttp__internal__n_error_54, + s_n_llhttp__internal__n_header_value_otherwise, + s_n_llhttp__internal__n_header_value_connection_token, + s_n_llhttp__internal__n_header_value_connection_ws, + s_n_llhttp__internal__n_header_value_connection_1, + s_n_llhttp__internal__n_header_value_connection_2, + s_n_llhttp__internal__n_header_value_connection_3, + s_n_llhttp__internal__n_header_value_connection, + s_n_llhttp__internal__n_error_56, + s_n_llhttp__internal__n_error_57, + s_n_llhttp__internal__n_header_value_content_length_ws, + s_n_llhttp__internal__n_header_value_content_length, + s_n_llhttp__internal__n_error_59, + s_n_llhttp__internal__n_error_58, + s_n_llhttp__internal__n_header_value_te_token_ows, + s_n_llhttp__internal__n_header_value, + s_n_llhttp__internal__n_header_value_te_token, + s_n_llhttp__internal__n_header_value_te_chunked_last, + s_n_llhttp__internal__n_header_value_te_chunked, + s_n_llhttp__internal__n_span_start_llhttp__on_header_value_1, + s_n_llhttp__internal__n_header_value_discard_ws, + s_n_llhttp__internal__n_invoke_load_header_state, + s_n_llhttp__internal__n_invoke_llhttp__on_header_field_complete, + s_n_llhttp__internal__n_header_field_general_otherwise, + s_n_llhttp__internal__n_header_field_general, + s_n_llhttp__internal__n_header_field_colon, + s_n_llhttp__internal__n_header_field_3, + s_n_llhttp__internal__n_header_field_4, + s_n_llhttp__internal__n_header_field_2, + s_n_llhttp__internal__n_header_field_1, + s_n_llhttp__internal__n_header_field_5, + s_n_llhttp__internal__n_header_field_6, + s_n_llhttp__internal__n_header_field_7, + s_n_llhttp__internal__n_header_field, + s_n_llhttp__internal__n_span_start_llhttp__on_header_field, + s_n_llhttp__internal__n_header_field_start, + s_n_llhttp__internal__n_headers_start, + s_n_llhttp__internal__n_url_to_http_09, + s_n_llhttp__internal__n_url_skip_to_http09, + s_n_llhttp__internal__n_url_skip_lf_to_http09_1, + s_n_llhttp__internal__n_url_skip_lf_to_http09, + s_n_llhttp__internal__n_req_pri_upgrade, + s_n_llhttp__internal__n_req_http_complete_crlf, + s_n_llhttp__internal__n_req_http_complete, + s_n_llhttp__internal__n_invoke_load_method_1, + s_n_llhttp__internal__n_invoke_llhttp__on_version_complete, + s_n_llhttp__internal__n_error_66, + s_n_llhttp__internal__n_error_73, + s_n_llhttp__internal__n_req_http_minor, + s_n_llhttp__internal__n_error_74, + s_n_llhttp__internal__n_req_http_dot, + s_n_llhttp__internal__n_error_75, + s_n_llhttp__internal__n_req_http_major, + s_n_llhttp__internal__n_span_start_llhttp__on_version, + s_n_llhttp__internal__n_req_http_start_1, + s_n_llhttp__internal__n_req_http_start_2, + s_n_llhttp__internal__n_req_http_start_3, + s_n_llhttp__internal__n_req_http_start, + s_n_llhttp__internal__n_url_to_http, + s_n_llhttp__internal__n_url_skip_to_http, + s_n_llhttp__internal__n_url_fragment, + s_n_llhttp__internal__n_span_end_stub_query_3, + s_n_llhttp__internal__n_url_query, + s_n_llhttp__internal__n_url_query_or_fragment, + s_n_llhttp__internal__n_url_path, + s_n_llhttp__internal__n_span_start_stub_path_2, + s_n_llhttp__internal__n_span_start_stub_path, + s_n_llhttp__internal__n_span_start_stub_path_1, + s_n_llhttp__internal__n_url_server_with_at, + s_n_llhttp__internal__n_url_server, + s_n_llhttp__internal__n_url_schema_delim_1, + s_n_llhttp__internal__n_url_schema_delim, + s_n_llhttp__internal__n_span_end_stub_schema, + s_n_llhttp__internal__n_url_schema, + s_n_llhttp__internal__n_url_start, + s_n_llhttp__internal__n_span_start_llhttp__on_url_1, + s_n_llhttp__internal__n_url_entry_normal, + s_n_llhttp__internal__n_span_start_llhttp__on_url, + s_n_llhttp__internal__n_url_entry_connect, + s_n_llhttp__internal__n_req_spaces_before_url, + s_n_llhttp__internal__n_req_first_space_before_url, + s_n_llhttp__internal__n_invoke_llhttp__on_method_complete_1, + s_n_llhttp__internal__n_after_start_req_2, + s_n_llhttp__internal__n_after_start_req_3, + s_n_llhttp__internal__n_after_start_req_1, + s_n_llhttp__internal__n_after_start_req_4, + s_n_llhttp__internal__n_after_start_req_6, + s_n_llhttp__internal__n_after_start_req_8, + s_n_llhttp__internal__n_after_start_req_9, + s_n_llhttp__internal__n_after_start_req_7, + s_n_llhttp__internal__n_after_start_req_5, + s_n_llhttp__internal__n_after_start_req_12, + s_n_llhttp__internal__n_after_start_req_13, + s_n_llhttp__internal__n_after_start_req_11, + s_n_llhttp__internal__n_after_start_req_10, + s_n_llhttp__internal__n_after_start_req_14, + s_n_llhttp__internal__n_after_start_req_17, + s_n_llhttp__internal__n_after_start_req_16, + s_n_llhttp__internal__n_after_start_req_15, + s_n_llhttp__internal__n_after_start_req_18, + s_n_llhttp__internal__n_after_start_req_20, + s_n_llhttp__internal__n_after_start_req_21, + s_n_llhttp__internal__n_after_start_req_19, + s_n_llhttp__internal__n_after_start_req_23, + s_n_llhttp__internal__n_after_start_req_24, + s_n_llhttp__internal__n_after_start_req_26, + s_n_llhttp__internal__n_after_start_req_28, + s_n_llhttp__internal__n_after_start_req_29, + s_n_llhttp__internal__n_after_start_req_27, + s_n_llhttp__internal__n_after_start_req_25, + s_n_llhttp__internal__n_after_start_req_30, + s_n_llhttp__internal__n_after_start_req_22, + s_n_llhttp__internal__n_after_start_req_31, + s_n_llhttp__internal__n_after_start_req_32, + s_n_llhttp__internal__n_after_start_req_35, + s_n_llhttp__internal__n_after_start_req_36, + s_n_llhttp__internal__n_after_start_req_34, + s_n_llhttp__internal__n_after_start_req_37, + s_n_llhttp__internal__n_after_start_req_38, + s_n_llhttp__internal__n_after_start_req_42, + s_n_llhttp__internal__n_after_start_req_43, + s_n_llhttp__internal__n_after_start_req_41, + s_n_llhttp__internal__n_after_start_req_40, + s_n_llhttp__internal__n_after_start_req_39, + s_n_llhttp__internal__n_after_start_req_45, + s_n_llhttp__internal__n_after_start_req_44, + s_n_llhttp__internal__n_after_start_req_33, + s_n_llhttp__internal__n_after_start_req_46, + s_n_llhttp__internal__n_after_start_req_49, + s_n_llhttp__internal__n_after_start_req_50, + s_n_llhttp__internal__n_after_start_req_51, + s_n_llhttp__internal__n_after_start_req_52, + s_n_llhttp__internal__n_after_start_req_48, + s_n_llhttp__internal__n_after_start_req_47, + s_n_llhttp__internal__n_after_start_req_55, + s_n_llhttp__internal__n_after_start_req_57, + s_n_llhttp__internal__n_after_start_req_58, + s_n_llhttp__internal__n_after_start_req_56, + s_n_llhttp__internal__n_after_start_req_54, + s_n_llhttp__internal__n_after_start_req_59, + s_n_llhttp__internal__n_after_start_req_60, + s_n_llhttp__internal__n_after_start_req_53, + s_n_llhttp__internal__n_after_start_req_62, + s_n_llhttp__internal__n_after_start_req_63, + s_n_llhttp__internal__n_after_start_req_61, + s_n_llhttp__internal__n_after_start_req_66, + s_n_llhttp__internal__n_after_start_req_68, + s_n_llhttp__internal__n_after_start_req_69, + s_n_llhttp__internal__n_after_start_req_67, + s_n_llhttp__internal__n_after_start_req_70, + s_n_llhttp__internal__n_after_start_req_65, + s_n_llhttp__internal__n_after_start_req_64, + s_n_llhttp__internal__n_after_start_req, + s_n_llhttp__internal__n_span_start_llhttp__on_method_1, + s_n_llhttp__internal__n_res_line_almost_done, + s_n_llhttp__internal__n_invoke_test_lenient_flags_30, + s_n_llhttp__internal__n_res_status, + s_n_llhttp__internal__n_span_start_llhttp__on_status, + s_n_llhttp__internal__n_res_status_code_otherwise, + s_n_llhttp__internal__n_res_status_code_digit_3, + s_n_llhttp__internal__n_res_status_code_digit_2, + s_n_llhttp__internal__n_res_status_code_digit_1, + s_n_llhttp__internal__n_res_after_version, + s_n_llhttp__internal__n_invoke_llhttp__on_version_complete_1, + s_n_llhttp__internal__n_error_89, + s_n_llhttp__internal__n_error_103, + s_n_llhttp__internal__n_res_http_minor, + s_n_llhttp__internal__n_error_104, + s_n_llhttp__internal__n_res_http_dot, + s_n_llhttp__internal__n_error_105, + s_n_llhttp__internal__n_res_http_major, + s_n_llhttp__internal__n_span_start_llhttp__on_version_1, + s_n_llhttp__internal__n_start_res, + s_n_llhttp__internal__n_invoke_llhttp__on_method_complete, + s_n_llhttp__internal__n_req_or_res_method_2, + s_n_llhttp__internal__n_invoke_update_type_1, + s_n_llhttp__internal__n_req_or_res_method_3, + s_n_llhttp__internal__n_req_or_res_method_1, + s_n_llhttp__internal__n_req_or_res_method, + s_n_llhttp__internal__n_span_start_llhttp__on_method, + s_n_llhttp__internal__n_start_req_or_res, + s_n_llhttp__internal__n_invoke_load_type, + s_n_llhttp__internal__n_invoke_update_finish, + s_n_llhttp__internal__n_start, +}; +typedef enum llparse_state_e llparse_state_t; + +int llhttp__on_method( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__on_url( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__on_version( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__on_header_field( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__on_header_value( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__on_body( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__on_chunk_extension_name( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__on_chunk_extension_value( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__on_status( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_load_initial_message_completed( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return state->initial_message_completed; +} + +int llhttp__on_reset( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_update_finish( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->finish = 2; + return 0; +} + +int llhttp__on_message_begin( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_load_type( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return state->type; +} + +int llhttp__internal__c_store_method( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp, + int match) { + state->method = match; + return 0; +} + +int llhttp__on_method_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_is_equal_method( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return state->method == 5; +} + +int llhttp__internal__c_update_http_major( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->http_major = 0; + return 0; +} + +int llhttp__internal__c_update_http_minor( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->http_minor = 9; + return 0; +} + +int llhttp__on_url_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_test_lenient_flags( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->lenient_flags & 1) == 1; +} + +int llhttp__internal__c_test_lenient_flags_1( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->lenient_flags & 256) == 256; +} + +int llhttp__internal__c_test_flags( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->flags & 128) == 128; +} + +int llhttp__on_chunk_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__on_message_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_is_equal_upgrade( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return state->upgrade == 1; +} + +int llhttp__after_message_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_update_content_length( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->content_length = 0; + return 0; +} + +int llhttp__internal__c_update_initial_message_completed( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->initial_message_completed = 1; + return 0; +} + +int llhttp__internal__c_update_finish_1( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->finish = 0; + return 0; +} + +int llhttp__internal__c_test_lenient_flags_2( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->lenient_flags & 4) == 4; +} + +int llhttp__internal__c_test_lenient_flags_3( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->lenient_flags & 32) == 32; +} + +int llhttp__before_headers_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__on_headers_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__after_headers_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_mul_add_content_length( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp, + int match) { + /* Multiplication overflow */ + if (state->content_length > 0xffffffffffffffffULL / 16) { + return 1; + } + + state->content_length *= 16; + + /* Addition overflow */ + if (match >= 0) { + if (state->content_length > 0xffffffffffffffffULL - match) { + return 1; + } + } else { + if (state->content_length < 0ULL - match) { + return 1; + } + } + state->content_length += match; + return 0; +} + +int llhttp__internal__c_test_lenient_flags_4( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->lenient_flags & 512) == 512; +} + +int llhttp__on_chunk_header( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_is_equal_content_length( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return state->content_length == 0; +} + +int llhttp__internal__c_test_lenient_flags_7( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->lenient_flags & 128) == 128; +} + +int llhttp__internal__c_or_flags( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->flags |= 128; + return 0; +} + +int llhttp__internal__c_test_lenient_flags_8( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->lenient_flags & 64) == 64; +} + +int llhttp__on_chunk_extension_name_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__on_chunk_extension_value_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_update_finish_3( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->finish = 1; + return 0; +} + +int llhttp__internal__c_or_flags_1( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->flags |= 64; + return 0; +} + +int llhttp__internal__c_update_upgrade( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->upgrade = 1; + return 0; +} + +int llhttp__internal__c_store_header_state( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp, + int match) { + state->header_state = match; + return 0; +} + +int llhttp__on_header_field_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_load_header_state( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return state->header_state; +} + +int llhttp__internal__c_test_flags_4( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->flags & 512) == 512; +} + +int llhttp__internal__c_test_lenient_flags_22( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->lenient_flags & 2) == 2; +} + +int llhttp__internal__c_or_flags_5( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->flags |= 1; + return 0; +} + +int llhttp__internal__c_update_header_state( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->header_state = 1; + return 0; +} + +int llhttp__on_header_value_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_or_flags_6( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->flags |= 2; + return 0; +} + +int llhttp__internal__c_or_flags_7( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->flags |= 4; + return 0; +} + +int llhttp__internal__c_or_flags_8( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->flags |= 8; + return 0; +} + +int llhttp__internal__c_update_header_state_3( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->header_state = 6; + return 0; +} + +int llhttp__internal__c_update_header_state_1( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->header_state = 0; + return 0; +} + +int llhttp__internal__c_update_header_state_6( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->header_state = 5; + return 0; +} + +int llhttp__internal__c_update_header_state_7( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->header_state = 7; + return 0; +} + +int llhttp__internal__c_test_flags_2( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->flags & 32) == 32; +} + +int llhttp__internal__c_mul_add_content_length_1( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp, + int match) { + /* Multiplication overflow */ + if (state->content_length > 0xffffffffffffffffULL / 10) { + return 1; + } + + state->content_length *= 10; + + /* Addition overflow */ + if (match >= 0) { + if (state->content_length > 0xffffffffffffffffULL - match) { + return 1; + } + } else { + if (state->content_length < 0ULL - match) { + return 1; + } + } + state->content_length += match; + return 0; +} + +int llhttp__internal__c_or_flags_17( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->flags |= 32; + return 0; +} + +int llhttp__internal__c_test_flags_3( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->flags & 8) == 8; +} + +int llhttp__internal__c_test_lenient_flags_20( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->lenient_flags & 8) == 8; +} + +int llhttp__internal__c_or_flags_18( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->flags |= 512; + return 0; +} + +int llhttp__internal__c_and_flags( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->flags &= -9; + return 0; +} + +int llhttp__internal__c_update_header_state_8( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->header_state = 8; + return 0; +} + +int llhttp__internal__c_or_flags_20( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->flags |= 16; + return 0; +} + +int llhttp__internal__c_load_method( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return state->method; +} + +int llhttp__internal__c_store_http_major( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp, + int match) { + state->http_major = match; + return 0; +} + +int llhttp__internal__c_store_http_minor( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp, + int match) { + state->http_minor = match; + return 0; +} + +int llhttp__internal__c_test_lenient_flags_24( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return (state->lenient_flags & 16) == 16; +} + +int llhttp__on_version_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_load_http_major( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return state->http_major; +} + +int llhttp__internal__c_load_http_minor( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + return state->http_minor; +} + +int llhttp__internal__c_update_status_code( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->status_code = 0; + return 0; +} + +int llhttp__internal__c_mul_add_status_code( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp, + int match) { + /* Multiplication overflow */ + if (state->status_code > 0xffff / 10) { + return 1; + } + + state->status_code *= 10; + + /* Addition overflow */ + if (match >= 0) { + if (state->status_code > 0xffff - match) { + return 1; + } + } else { + if (state->status_code < 0 - match) { + return 1; + } + } + state->status_code += match; + return 0; +} + +int llhttp__on_status_complete( + llhttp__internal_t* s, const unsigned char* p, + const unsigned char* endp); + +int llhttp__internal__c_update_type( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->type = 1; + return 0; +} + +int llhttp__internal__c_update_type_1( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + state->type = 2; + return 0; +} + +int llhttp__internal_init(llhttp__internal_t* state) { + memset(state, 0, sizeof(*state)); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_start; + return 0; +} + +static llparse_state_t llhttp__internal__run( + llhttp__internal_t* state, + const unsigned char* p, + const unsigned char* endp) { + int match; + switch ((llparse_state_t) (intptr_t) state->_current) { + case s_n_llhttp__internal__n_closed: + s_n_llhttp__internal__n_closed: { + if (p == endp) { + return s_n_llhttp__internal__n_closed; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_closed; + } + case 13: { + p++; + goto s_n_llhttp__internal__n_closed; + } + default: { + p++; + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_3; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__after_message_complete: + s_n_llhttp__internal__n_invoke_llhttp__after_message_complete: { + switch (llhttp__after_message_complete(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_update_content_length; + default: + goto s_n_llhttp__internal__n_invoke_update_finish_1; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_pause_1: + s_n_llhttp__internal__n_pause_1: { + state->error = 0x16; + state->reason = "Pause on CONNECT/Upgrade"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__after_message_complete; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_is_equal_upgrade: + s_n_llhttp__internal__n_invoke_is_equal_upgrade: { + switch (llhttp__internal__c_is_equal_upgrade(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_llhttp__after_message_complete; + default: + goto s_n_llhttp__internal__n_pause_1; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_message_complete_2: + s_n_llhttp__internal__n_invoke_llhttp__on_message_complete_2: { + switch (llhttp__on_message_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_is_equal_upgrade; + case 21: + goto s_n_llhttp__internal__n_pause_13; + default: + goto s_n_llhttp__internal__n_error_38; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_chunk_data_almost_done_1: + s_n_llhttp__internal__n_chunk_data_almost_done_1: { + if (p == endp) { + return s_n_llhttp__internal__n_chunk_data_almost_done_1; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_complete; + } + default: { + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_7; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_chunk_data_almost_done: + s_n_llhttp__internal__n_chunk_data_almost_done: { + if (p == endp) { + return s_n_llhttp__internal__n_chunk_data_almost_done; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_6; + } + case 13: { + p++; + goto s_n_llhttp__internal__n_chunk_data_almost_done_1; + } + default: { + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_7; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_consume_content_length: + s_n_llhttp__internal__n_consume_content_length: { + size_t avail; + uint64_t need; + + avail = endp - p; + need = state->content_length; + if (avail >= need) { + p += need; + state->content_length = 0; + goto s_n_llhttp__internal__n_span_end_llhttp__on_body; + } + + state->content_length -= avail; + return s_n_llhttp__internal__n_consume_content_length; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_body: + s_n_llhttp__internal__n_span_start_llhttp__on_body: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_body; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_body; + goto s_n_llhttp__internal__n_consume_content_length; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_is_equal_content_length: + s_n_llhttp__internal__n_invoke_is_equal_content_length: { + switch (llhttp__internal__c_is_equal_content_length(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_span_start_llhttp__on_body; + default: + goto s_n_llhttp__internal__n_invoke_or_flags; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_chunk_size_almost_done: + s_n_llhttp__internal__n_chunk_size_almost_done: { + if (p == endp) { + return s_n_llhttp__internal__n_chunk_size_almost_done; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_header; + } + default: { + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_8; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_test_lenient_flags_9: + s_n_llhttp__internal__n_invoke_test_lenient_flags_9: { + switch (llhttp__internal__c_test_lenient_flags_1(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_chunk_size_almost_done; + default: + goto s_n_llhttp__internal__n_error_20; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete: + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete: { + switch (llhttp__on_chunk_extension_name_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_9; + case 21: + goto s_n_llhttp__internal__n_pause_5; + default: + goto s_n_llhttp__internal__n_error_19; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete_1: + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete_1: { + switch (llhttp__on_chunk_extension_name_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_chunk_size_almost_done; + case 21: + goto s_n_llhttp__internal__n_pause_6; + default: + goto s_n_llhttp__internal__n_error_21; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete_2: + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete_2: { + switch (llhttp__on_chunk_extension_name_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_chunk_extensions; + case 21: + goto s_n_llhttp__internal__n_pause_7; + default: + goto s_n_llhttp__internal__n_error_22; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_test_lenient_flags_10: + s_n_llhttp__internal__n_invoke_test_lenient_flags_10: { + switch (llhttp__internal__c_test_lenient_flags_1(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_chunk_size_almost_done; + default: + goto s_n_llhttp__internal__n_error_25; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete: + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete: { + switch (llhttp__on_chunk_extension_value_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_10; + case 21: + goto s_n_llhttp__internal__n_pause_8; + default: + goto s_n_llhttp__internal__n_error_24; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_1: + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_1: { + switch (llhttp__on_chunk_extension_value_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_chunk_size_almost_done; + case 21: + goto s_n_llhttp__internal__n_pause_9; + default: + goto s_n_llhttp__internal__n_error_26; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_chunk_extension_quoted_value_done: + s_n_llhttp__internal__n_chunk_extension_quoted_value_done: { + if (p == endp) { + return s_n_llhttp__internal__n_chunk_extension_quoted_value_done; + } + switch (*p) { + case 10: { + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_11; + } + case 13: { + p++; + goto s_n_llhttp__internal__n_chunk_size_almost_done; + } + case ';': { + p++; + goto s_n_llhttp__internal__n_chunk_extensions; + } + default: { + goto s_n_llhttp__internal__n_error_29; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_2: + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_2: { + switch (llhttp__on_chunk_extension_value_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_chunk_extension_quoted_value_done; + case 21: + goto s_n_llhttp__internal__n_pause_10; + default: + goto s_n_llhttp__internal__n_error_27; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_30: + s_n_llhttp__internal__n_error_30: { + state->error = 0x2; + state->reason = "Invalid quoted-pair in chunk extensions quoted value"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_chunk_extension_quoted_value_quoted_pair: + s_n_llhttp__internal__n_chunk_extension_quoted_value_quoted_pair: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + }; + if (p == endp) { + return s_n_llhttp__internal__n_chunk_extension_quoted_value_quoted_pair; + } + switch (lookup_table[(uint8_t) *p]) { + case 1: { + p++; + goto s_n_llhttp__internal__n_chunk_extension_quoted_value; + } + default: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value_3; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_31: + s_n_llhttp__internal__n_error_31: { + state->error = 0x2; + state->reason = "Invalid character in chunk extensions quoted value"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_chunk_extension_quoted_value: + s_n_llhttp__internal__n_chunk_extension_quoted_value: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + }; + if (p == endp) { + return s_n_llhttp__internal__n_chunk_extension_quoted_value; + } + switch (lookup_table[(uint8_t) *p]) { + case 1: { + p++; + goto s_n_llhttp__internal__n_chunk_extension_quoted_value; + } + case 2: { + p++; + goto s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value_2; + } + case 3: { + p++; + goto s_n_llhttp__internal__n_chunk_extension_quoted_value_quoted_pair; + } + default: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value_4; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_3: + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_3: { + switch (llhttp__on_chunk_extension_value_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_chunk_extensions; + case 21: + goto s_n_llhttp__internal__n_pause_11; + default: + goto s_n_llhttp__internal__n_error_32; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_33: + s_n_llhttp__internal__n_error_33: { + state->error = 0x2; + state->reason = "Invalid character in chunk extensions value"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_chunk_extension_value: + s_n_llhttp__internal__n_chunk_extension_value: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 3, 4, 3, 3, 3, 3, 3, 0, 0, 3, 3, 0, 3, 3, 0, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 5, 0, 0, 0, 0, + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 3, 0, 3, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + if (p == endp) { + return s_n_llhttp__internal__n_chunk_extension_value; + } + switch (lookup_table[(uint8_t) *p]) { + case 1: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value; + } + case 2: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value_1; + } + case 3: { + p++; + goto s_n_llhttp__internal__n_chunk_extension_value; + } + case 4: { + p++; + goto s_n_llhttp__internal__n_chunk_extension_quoted_value; + } + case 5: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value_5; + } + default: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value_6; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_chunk_extension_value: + s_n_llhttp__internal__n_span_start_llhttp__on_chunk_extension_value: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_chunk_extension_value; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_chunk_extension_value; + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete_3; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_34: + s_n_llhttp__internal__n_error_34: { + state->error = 0x2; + state->reason = "Invalid character in chunk extensions name"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_chunk_extension_name: + s_n_llhttp__internal__n_chunk_extension_name: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 3, 0, 3, 3, 3, 3, 3, 0, 0, 3, 3, 0, 3, 3, 0, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 4, 0, 5, 0, 0, + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 3, 0, 3, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + if (p == endp) { + return s_n_llhttp__internal__n_chunk_extension_name; + } + switch (lookup_table[(uint8_t) *p]) { + case 1: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_name; + } + case 2: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_name_1; + } + case 3: { + p++; + goto s_n_llhttp__internal__n_chunk_extension_name; + } + case 4: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_name_2; + } + case 5: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_name_3; + } + default: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_name_4; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_chunk_extension_name: + s_n_llhttp__internal__n_span_start_llhttp__on_chunk_extension_name: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_chunk_extension_name; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_chunk_extension_name; + goto s_n_llhttp__internal__n_chunk_extension_name; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_chunk_extensions: + s_n_llhttp__internal__n_chunk_extensions: { + if (p == endp) { + return s_n_llhttp__internal__n_chunk_extensions; + } + switch (*p) { + case 13: { + p++; + goto s_n_llhttp__internal__n_error_17; + } + case ' ': { + p++; + goto s_n_llhttp__internal__n_error_18; + } + default: { + goto s_n_llhttp__internal__n_span_start_llhttp__on_chunk_extension_name; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_chunk_size_otherwise: + s_n_llhttp__internal__n_chunk_size_otherwise: { + if (p == endp) { + return s_n_llhttp__internal__n_chunk_size_otherwise; + } + switch (*p) { + case 9: { + p++; + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_4; + } + case 10: { + p++; + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_5; + } + case 13: { + p++; + goto s_n_llhttp__internal__n_chunk_size_almost_done; + } + case ' ': { + p++; + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_4; + } + case ';': { + p++; + goto s_n_llhttp__internal__n_chunk_extensions; + } + default: { + goto s_n_llhttp__internal__n_error_35; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_chunk_size: + s_n_llhttp__internal__n_chunk_size: { + if (p == endp) { + return s_n_llhttp__internal__n_chunk_size; + } + switch (*p) { + case '0': { + p++; + match = 0; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '1': { + p++; + match = 1; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '2': { + p++; + match = 2; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '3': { + p++; + match = 3; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '4': { + p++; + match = 4; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '5': { + p++; + match = 5; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '6': { + p++; + match = 6; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '7': { + p++; + match = 7; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '8': { + p++; + match = 8; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '9': { + p++; + match = 9; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'A': { + p++; + match = 10; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'B': { + p++; + match = 11; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'C': { + p++; + match = 12; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'D': { + p++; + match = 13; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'E': { + p++; + match = 14; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'F': { + p++; + match = 15; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'a': { + p++; + match = 10; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'b': { + p++; + match = 11; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'c': { + p++; + match = 12; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'd': { + p++; + match = 13; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'e': { + p++; + match = 14; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'f': { + p++; + match = 15; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + default: { + goto s_n_llhttp__internal__n_chunk_size_otherwise; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_chunk_size_digit: + s_n_llhttp__internal__n_chunk_size_digit: { + if (p == endp) { + return s_n_llhttp__internal__n_chunk_size_digit; + } + switch (*p) { + case '0': { + p++; + match = 0; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '1': { + p++; + match = 1; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '2': { + p++; + match = 2; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '3': { + p++; + match = 3; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '4': { + p++; + match = 4; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '5': { + p++; + match = 5; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '6': { + p++; + match = 6; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '7': { + p++; + match = 7; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '8': { + p++; + match = 8; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case '9': { + p++; + match = 9; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'A': { + p++; + match = 10; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'B': { + p++; + match = 11; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'C': { + p++; + match = 12; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'D': { + p++; + match = 13; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'E': { + p++; + match = 14; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'F': { + p++; + match = 15; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'a': { + p++; + match = 10; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'b': { + p++; + match = 11; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'c': { + p++; + match = 12; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'd': { + p++; + match = 13; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'e': { + p++; + match = 14; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + case 'f': { + p++; + match = 15; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length; + } + default: { + goto s_n_llhttp__internal__n_error_37; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_update_content_length_1: + s_n_llhttp__internal__n_invoke_update_content_length_1: { + switch (llhttp__internal__c_update_content_length(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_chunk_size_digit; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_consume_content_length_1: + s_n_llhttp__internal__n_consume_content_length_1: { + size_t avail; + uint64_t need; + + avail = endp - p; + need = state->content_length; + if (avail >= need) { + p += need; + state->content_length = 0; + goto s_n_llhttp__internal__n_span_end_llhttp__on_body_1; + } + + state->content_length -= avail; + return s_n_llhttp__internal__n_consume_content_length_1; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_body_1: + s_n_llhttp__internal__n_span_start_llhttp__on_body_1: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_body_1; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_body; + goto s_n_llhttp__internal__n_consume_content_length_1; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_eof: + s_n_llhttp__internal__n_eof: { + if (p == endp) { + return s_n_llhttp__internal__n_eof; + } + p++; + goto s_n_llhttp__internal__n_eof; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_body_2: + s_n_llhttp__internal__n_span_start_llhttp__on_body_2: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_body_2; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_body; + goto s_n_llhttp__internal__n_eof; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__after_headers_complete: + s_n_llhttp__internal__n_invoke_llhttp__after_headers_complete: { + switch (llhttp__after_headers_complete(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_llhttp__on_message_complete_1; + case 2: + goto s_n_llhttp__internal__n_invoke_update_content_length_1; + case 3: + goto s_n_llhttp__internal__n_span_start_llhttp__on_body_1; + case 4: + goto s_n_llhttp__internal__n_invoke_update_finish_3; + case 5: + goto s_n_llhttp__internal__n_error_39; + default: + goto s_n_llhttp__internal__n_invoke_llhttp__on_message_complete; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_5: + s_n_llhttp__internal__n_error_5: { + state->error = 0xa; + state->reason = "Invalid header field char"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_headers_almost_done: + s_n_llhttp__internal__n_headers_almost_done: { + if (p == endp) { + return s_n_llhttp__internal__n_headers_almost_done; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_invoke_test_flags_1; + } + default: { + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_12; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_field_colon_discard_ws: + s_n_llhttp__internal__n_header_field_colon_discard_ws: { + if (p == endp) { + return s_n_llhttp__internal__n_header_field_colon_discard_ws; + } + switch (*p) { + case ' ': { + p++; + goto s_n_llhttp__internal__n_header_field_colon_discard_ws; + } + default: { + goto s_n_llhttp__internal__n_header_field_colon; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_header_value_complete: + s_n_llhttp__internal__n_invoke_llhttp__on_header_value_complete: { + switch (llhttp__on_header_value_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_header_field_start; + case 21: + goto s_n_llhttp__internal__n_pause_18; + default: + goto s_n_llhttp__internal__n_error_48; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_header_value: + s_n_llhttp__internal__n_span_start_llhttp__on_header_value: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_header_value; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_header_value; + goto s_n_llhttp__internal__n_span_end_llhttp__on_header_value; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_discard_lws: + s_n_llhttp__internal__n_header_value_discard_lws: { + if (p == endp) { + return s_n_llhttp__internal__n_header_value_discard_lws; + } + switch (*p) { + case 9: { + p++; + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_15; + } + case ' ': { + p++; + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_15; + } + default: { + goto s_n_llhttp__internal__n_invoke_load_header_state_1; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_discard_ws_almost_done: + s_n_llhttp__internal__n_header_value_discard_ws_almost_done: { + if (p == endp) { + return s_n_llhttp__internal__n_header_value_discard_ws_almost_done; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_header_value_discard_lws; + } + default: { + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_16; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_lws: + s_n_llhttp__internal__n_header_value_lws: { + if (p == endp) { + return s_n_llhttp__internal__n_header_value_lws; + } + switch (*p) { + case 9: { + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_18; + } + case ' ': { + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_18; + } + default: { + goto s_n_llhttp__internal__n_invoke_load_header_state_5; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_almost_done: + s_n_llhttp__internal__n_header_value_almost_done: { + if (p == endp) { + return s_n_llhttp__internal__n_header_value_almost_done; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_header_value_lws; + } + default: { + goto s_n_llhttp__internal__n_error_53; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_test_lenient_flags_17: + s_n_llhttp__internal__n_invoke_test_lenient_flags_17: { + switch (llhttp__internal__c_test_lenient_flags_1(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_header_value_almost_done; + default: + goto s_n_llhttp__internal__n_error_51; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_lenient: + s_n_llhttp__internal__n_header_value_lenient: { + if (p == endp) { + return s_n_llhttp__internal__n_header_value_lenient; + } + switch (*p) { + case 10: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_header_value_4; + } + case 13: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_header_value_5; + } + default: { + p++; + goto s_n_llhttp__internal__n_header_value_lenient; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_54: + s_n_llhttp__internal__n_error_54: { + state->error = 0xa; + state->reason = "Invalid header value char"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_otherwise: + s_n_llhttp__internal__n_header_value_otherwise: { + if (p == endp) { + return s_n_llhttp__internal__n_header_value_otherwise; + } + switch (*p) { + case 10: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_header_value_1; + } + case 13: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_header_value_2; + } + default: { + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_19; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_connection_token: + s_n_llhttp__internal__n_header_value_connection_token: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + }; + if (p == endp) { + return s_n_llhttp__internal__n_header_value_connection_token; + } + switch (lookup_table[(uint8_t) *p]) { + case 1: { + p++; + goto s_n_llhttp__internal__n_header_value_connection_token; + } + case 2: { + p++; + goto s_n_llhttp__internal__n_header_value_connection; + } + default: { + goto s_n_llhttp__internal__n_header_value_otherwise; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_connection_ws: + s_n_llhttp__internal__n_header_value_connection_ws: { + if (p == endp) { + return s_n_llhttp__internal__n_header_value_connection_ws; + } + switch (*p) { + case 10: { + goto s_n_llhttp__internal__n_header_value_otherwise; + } + case 13: { + goto s_n_llhttp__internal__n_header_value_otherwise; + } + case ' ': { + p++; + goto s_n_llhttp__internal__n_header_value_connection_ws; + } + case ',': { + p++; + goto s_n_llhttp__internal__n_invoke_load_header_state_6; + } + default: { + goto s_n_llhttp__internal__n_invoke_update_header_state_5; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_connection_1: + s_n_llhttp__internal__n_header_value_connection_1: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_header_value_connection_1; + } + match_seq = llparse__match_sequence_to_lower(state, p, endp, llparse_blob2, 4); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + goto s_n_llhttp__internal__n_invoke_update_header_state_3; + } + case kMatchPause: { + return s_n_llhttp__internal__n_header_value_connection_1; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_header_value_connection_token; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_connection_2: + s_n_llhttp__internal__n_header_value_connection_2: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_header_value_connection_2; + } + match_seq = llparse__match_sequence_to_lower(state, p, endp, llparse_blob3, 9); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + goto s_n_llhttp__internal__n_invoke_update_header_state_6; + } + case kMatchPause: { + return s_n_llhttp__internal__n_header_value_connection_2; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_header_value_connection_token; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_connection_3: + s_n_llhttp__internal__n_header_value_connection_3: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_header_value_connection_3; + } + match_seq = llparse__match_sequence_to_lower(state, p, endp, llparse_blob4, 6); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + goto s_n_llhttp__internal__n_invoke_update_header_state_7; + } + case kMatchPause: { + return s_n_llhttp__internal__n_header_value_connection_3; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_header_value_connection_token; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_connection: + s_n_llhttp__internal__n_header_value_connection: { + if (p == endp) { + return s_n_llhttp__internal__n_header_value_connection; + } + switch (((*p) >= 'A' && (*p) <= 'Z' ? (*p | 0x20) : (*p))) { + case 9: { + p++; + goto s_n_llhttp__internal__n_header_value_connection; + } + case ' ': { + p++; + goto s_n_llhttp__internal__n_header_value_connection; + } + case 'c': { + p++; + goto s_n_llhttp__internal__n_header_value_connection_1; + } + case 'k': { + p++; + goto s_n_llhttp__internal__n_header_value_connection_2; + } + case 'u': { + p++; + goto s_n_llhttp__internal__n_header_value_connection_3; + } + default: { + goto s_n_llhttp__internal__n_header_value_connection_token; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_56: + s_n_llhttp__internal__n_error_56: { + state->error = 0xb; + state->reason = "Content-Length overflow"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_57: + s_n_llhttp__internal__n_error_57: { + state->error = 0xb; + state->reason = "Invalid character in Content-Length"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_content_length_ws: + s_n_llhttp__internal__n_header_value_content_length_ws: { + if (p == endp) { + return s_n_llhttp__internal__n_header_value_content_length_ws; + } + switch (*p) { + case 10: { + goto s_n_llhttp__internal__n_invoke_or_flags_17; + } + case 13: { + goto s_n_llhttp__internal__n_invoke_or_flags_17; + } + case ' ': { + p++; + goto s_n_llhttp__internal__n_header_value_content_length_ws; + } + default: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_header_value_7; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_content_length: + s_n_llhttp__internal__n_header_value_content_length: { + if (p == endp) { + return s_n_llhttp__internal__n_header_value_content_length; + } + switch (*p) { + case '0': { + p++; + match = 0; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length_1; + } + case '1': { + p++; + match = 1; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length_1; + } + case '2': { + p++; + match = 2; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length_1; + } + case '3': { + p++; + match = 3; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length_1; + } + case '4': { + p++; + match = 4; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length_1; + } + case '5': { + p++; + match = 5; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length_1; + } + case '6': { + p++; + match = 6; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length_1; + } + case '7': { + p++; + match = 7; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length_1; + } + case '8': { + p++; + match = 8; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length_1; + } + case '9': { + p++; + match = 9; + goto s_n_llhttp__internal__n_invoke_mul_add_content_length_1; + } + default: { + goto s_n_llhttp__internal__n_header_value_content_length_ws; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_59: + s_n_llhttp__internal__n_error_59: { + state->error = 0xf; + state->reason = "Invalid `Transfer-Encoding` header value"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_58: + s_n_llhttp__internal__n_error_58: { + state->error = 0xf; + state->reason = "Invalid `Transfer-Encoding` header value"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_te_token_ows: + s_n_llhttp__internal__n_header_value_te_token_ows: { + if (p == endp) { + return s_n_llhttp__internal__n_header_value_te_token_ows; + } + switch (*p) { + case 9: { + p++; + goto s_n_llhttp__internal__n_header_value_te_token_ows; + } + case ' ': { + p++; + goto s_n_llhttp__internal__n_header_value_te_token_ows; + } + default: { + goto s_n_llhttp__internal__n_header_value_te_chunked; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value: + s_n_llhttp__internal__n_header_value: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + }; + if (p == endp) { + return s_n_llhttp__internal__n_header_value; + } + #ifdef __SSE4_2__ + if (endp - p >= 16) { + __m128i ranges; + __m128i input; + int avail; + int match_len; + + /* Load input */ + input = _mm_loadu_si128((__m128i const*) p); + ranges = _mm_loadu_si128((__m128i const*) llparse_blob6); + + /* Find first character that does not match `ranges` */ + match_len = _mm_cmpestri(ranges, 6, + input, 16, + _SIDD_UBYTE_OPS | _SIDD_CMP_RANGES | + _SIDD_NEGATIVE_POLARITY); + + if (match_len != 0) { + p += match_len; + goto s_n_llhttp__internal__n_header_value; + } + goto s_n_llhttp__internal__n_header_value_otherwise; + } + #endif /* __SSE4_2__ */ + switch (lookup_table[(uint8_t) *p]) { + case 1: { + p++; + goto s_n_llhttp__internal__n_header_value; + } + default: { + goto s_n_llhttp__internal__n_header_value_otherwise; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_te_token: + s_n_llhttp__internal__n_header_value_te_token: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + }; + if (p == endp) { + return s_n_llhttp__internal__n_header_value_te_token; + } + switch (lookup_table[(uint8_t) *p]) { + case 1: { + p++; + goto s_n_llhttp__internal__n_header_value_te_token; + } + case 2: { + p++; + goto s_n_llhttp__internal__n_header_value_te_token_ows; + } + default: { + goto s_n_llhttp__internal__n_invoke_update_header_state_9; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_te_chunked_last: + s_n_llhttp__internal__n_header_value_te_chunked_last: { + if (p == endp) { + return s_n_llhttp__internal__n_header_value_te_chunked_last; + } + switch (*p) { + case 10: { + goto s_n_llhttp__internal__n_invoke_update_header_state_8; + } + case 13: { + goto s_n_llhttp__internal__n_invoke_update_header_state_8; + } + case ' ': { + p++; + goto s_n_llhttp__internal__n_header_value_te_chunked_last; + } + case ',': { + goto s_n_llhttp__internal__n_invoke_load_type_1; + } + default: { + goto s_n_llhttp__internal__n_header_value_te_token; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_te_chunked: + s_n_llhttp__internal__n_header_value_te_chunked: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_header_value_te_chunked; + } + match_seq = llparse__match_sequence_to_lower_unsafe(state, p, endp, llparse_blob5, 7); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + goto s_n_llhttp__internal__n_header_value_te_chunked_last; + } + case kMatchPause: { + return s_n_llhttp__internal__n_header_value_te_chunked; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_header_value_te_token; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_header_value_1: + s_n_llhttp__internal__n_span_start_llhttp__on_header_value_1: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_header_value_1; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_header_value; + goto s_n_llhttp__internal__n_invoke_load_header_state_3; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_value_discard_ws: + s_n_llhttp__internal__n_header_value_discard_ws: { + if (p == endp) { + return s_n_llhttp__internal__n_header_value_discard_ws; + } + switch (*p) { + case 9: { + p++; + goto s_n_llhttp__internal__n_header_value_discard_ws; + } + case 10: { + p++; + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_14; + } + case 13: { + p++; + goto s_n_llhttp__internal__n_header_value_discard_ws_almost_done; + } + case ' ': { + p++; + goto s_n_llhttp__internal__n_header_value_discard_ws; + } + default: { + goto s_n_llhttp__internal__n_span_start_llhttp__on_header_value_1; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_load_header_state: + s_n_llhttp__internal__n_invoke_load_header_state: { + switch (llhttp__internal__c_load_header_state(state, p, endp)) { + case 2: + goto s_n_llhttp__internal__n_invoke_test_flags_4; + case 3: + goto s_n_llhttp__internal__n_invoke_test_flags_5; + default: + goto s_n_llhttp__internal__n_header_value_discard_ws; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_header_field_complete: + s_n_llhttp__internal__n_invoke_llhttp__on_header_field_complete: { + switch (llhttp__on_header_field_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_load_header_state; + case 21: + goto s_n_llhttp__internal__n_pause_19; + default: + goto s_n_llhttp__internal__n_error_45; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_field_general_otherwise: + s_n_llhttp__internal__n_header_field_general_otherwise: { + if (p == endp) { + return s_n_llhttp__internal__n_header_field_general_otherwise; + } + switch (*p) { + case ':': { + goto s_n_llhttp__internal__n_span_end_llhttp__on_header_field_2; + } + default: { + goto s_n_llhttp__internal__n_error_62; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_field_general: + s_n_llhttp__internal__n_header_field_general: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + if (p == endp) { + return s_n_llhttp__internal__n_header_field_general; + } + #ifdef __SSE4_2__ + if (endp - p >= 16) { + __m128i ranges; + __m128i input; + int avail; + int match_len; + + /* Load input */ + input = _mm_loadu_si128((__m128i const*) p); + ranges = _mm_loadu_si128((__m128i const*) llparse_blob7); + + /* Find first character that does not match `ranges` */ + match_len = _mm_cmpestri(ranges, 16, + input, 16, + _SIDD_UBYTE_OPS | _SIDD_CMP_RANGES | + _SIDD_NEGATIVE_POLARITY); + + if (match_len != 0) { + p += match_len; + goto s_n_llhttp__internal__n_header_field_general; + } + ranges = _mm_loadu_si128((__m128i const*) llparse_blob8); + + /* Find first character that does not match `ranges` */ + match_len = _mm_cmpestri(ranges, 2, + input, 16, + _SIDD_UBYTE_OPS | _SIDD_CMP_RANGES | + _SIDD_NEGATIVE_POLARITY); + + if (match_len != 0) { + p += match_len; + goto s_n_llhttp__internal__n_header_field_general; + } + goto s_n_llhttp__internal__n_header_field_general_otherwise; + } + #endif /* __SSE4_2__ */ + switch (lookup_table[(uint8_t) *p]) { + case 1: { + p++; + goto s_n_llhttp__internal__n_header_field_general; + } + default: { + goto s_n_llhttp__internal__n_header_field_general_otherwise; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_field_colon: + s_n_llhttp__internal__n_header_field_colon: { + if (p == endp) { + return s_n_llhttp__internal__n_header_field_colon; + } + switch (*p) { + case ' ': { + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_13; + } + case ':': { + goto s_n_llhttp__internal__n_span_end_llhttp__on_header_field_1; + } + default: { + goto s_n_llhttp__internal__n_invoke_update_header_state_10; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_field_3: + s_n_llhttp__internal__n_header_field_3: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_header_field_3; + } + match_seq = llparse__match_sequence_to_lower(state, p, endp, llparse_blob1, 6); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 1; + goto s_n_llhttp__internal__n_invoke_store_header_state; + } + case kMatchPause: { + return s_n_llhttp__internal__n_header_field_3; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_invoke_update_header_state_11; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_field_4: + s_n_llhttp__internal__n_header_field_4: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_header_field_4; + } + match_seq = llparse__match_sequence_to_lower(state, p, endp, llparse_blob9, 10); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 2; + goto s_n_llhttp__internal__n_invoke_store_header_state; + } + case kMatchPause: { + return s_n_llhttp__internal__n_header_field_4; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_invoke_update_header_state_11; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_field_2: + s_n_llhttp__internal__n_header_field_2: { + if (p == endp) { + return s_n_llhttp__internal__n_header_field_2; + } + switch (((*p) >= 'A' && (*p) <= 'Z' ? (*p | 0x20) : (*p))) { + case 'n': { + p++; + goto s_n_llhttp__internal__n_header_field_3; + } + case 't': { + p++; + goto s_n_llhttp__internal__n_header_field_4; + } + default: { + goto s_n_llhttp__internal__n_invoke_update_header_state_11; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_field_1: + s_n_llhttp__internal__n_header_field_1: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_header_field_1; + } + match_seq = llparse__match_sequence_to_lower(state, p, endp, llparse_blob0, 2); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + goto s_n_llhttp__internal__n_header_field_2; + } + case kMatchPause: { + return s_n_llhttp__internal__n_header_field_1; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_invoke_update_header_state_11; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_field_5: + s_n_llhttp__internal__n_header_field_5: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_header_field_5; + } + match_seq = llparse__match_sequence_to_lower(state, p, endp, llparse_blob10, 15); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 1; + goto s_n_llhttp__internal__n_invoke_store_header_state; + } + case kMatchPause: { + return s_n_llhttp__internal__n_header_field_5; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_invoke_update_header_state_11; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_field_6: + s_n_llhttp__internal__n_header_field_6: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_header_field_6; + } + match_seq = llparse__match_sequence_to_lower(state, p, endp, llparse_blob11, 16); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 3; + goto s_n_llhttp__internal__n_invoke_store_header_state; + } + case kMatchPause: { + return s_n_llhttp__internal__n_header_field_6; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_invoke_update_header_state_11; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_field_7: + s_n_llhttp__internal__n_header_field_7: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_header_field_7; + } + match_seq = llparse__match_sequence_to_lower(state, p, endp, llparse_blob12, 6); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 4; + goto s_n_llhttp__internal__n_invoke_store_header_state; + } + case kMatchPause: { + return s_n_llhttp__internal__n_header_field_7; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_invoke_update_header_state_11; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_field: + s_n_llhttp__internal__n_header_field: { + if (p == endp) { + return s_n_llhttp__internal__n_header_field; + } + switch (((*p) >= 'A' && (*p) <= 'Z' ? (*p | 0x20) : (*p))) { + case 'c': { + p++; + goto s_n_llhttp__internal__n_header_field_1; + } + case 'p': { + p++; + goto s_n_llhttp__internal__n_header_field_5; + } + case 't': { + p++; + goto s_n_llhttp__internal__n_header_field_6; + } + case 'u': { + p++; + goto s_n_llhttp__internal__n_header_field_7; + } + default: { + goto s_n_llhttp__internal__n_invoke_update_header_state_11; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_header_field: + s_n_llhttp__internal__n_span_start_llhttp__on_header_field: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_header_field; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_header_field; + goto s_n_llhttp__internal__n_header_field; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_header_field_start: + s_n_llhttp__internal__n_header_field_start: { + if (p == endp) { + return s_n_llhttp__internal__n_header_field_start; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_1; + } + case 13: { + p++; + goto s_n_llhttp__internal__n_headers_almost_done; + } + case ':': { + goto s_n_llhttp__internal__n_error_44; + } + default: { + goto s_n_llhttp__internal__n_span_start_llhttp__on_header_field; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_headers_start: + s_n_llhttp__internal__n_headers_start: { + if (p == endp) { + return s_n_llhttp__internal__n_headers_start; + } + switch (*p) { + case ' ': { + p++; + goto s_n_llhttp__internal__n_invoke_test_lenient_flags; + } + default: { + goto s_n_llhttp__internal__n_header_field_start; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_to_http_09: + s_n_llhttp__internal__n_url_to_http_09: { + if (p == endp) { + return s_n_llhttp__internal__n_url_to_http_09; + } + switch (*p) { + case 9: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 12: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + default: { + goto s_n_llhttp__internal__n_invoke_update_http_major; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_skip_to_http09: + s_n_llhttp__internal__n_url_skip_to_http09: { + if (p == endp) { + return s_n_llhttp__internal__n_url_skip_to_http09; + } + switch (*p) { + case 9: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 12: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + default: { + p++; + goto s_n_llhttp__internal__n_url_to_http_09; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_skip_lf_to_http09_1: + s_n_llhttp__internal__n_url_skip_lf_to_http09_1: { + if (p == endp) { + return s_n_llhttp__internal__n_url_skip_lf_to_http09_1; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_url_to_http_09; + } + default: { + goto s_n_llhttp__internal__n_error_63; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_skip_lf_to_http09: + s_n_llhttp__internal__n_url_skip_lf_to_http09: { + if (p == endp) { + return s_n_llhttp__internal__n_url_skip_lf_to_http09; + } + switch (*p) { + case 9: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 12: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 13: { + p++; + goto s_n_llhttp__internal__n_url_skip_lf_to_http09_1; + } + default: { + goto s_n_llhttp__internal__n_error_63; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_pri_upgrade: + s_n_llhttp__internal__n_req_pri_upgrade: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_req_pri_upgrade; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob14, 10); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + goto s_n_llhttp__internal__n_error_71; + } + case kMatchPause: { + return s_n_llhttp__internal__n_req_pri_upgrade; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_72; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_http_complete_crlf: + s_n_llhttp__internal__n_req_http_complete_crlf: { + if (p == endp) { + return s_n_llhttp__internal__n_req_http_complete_crlf; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_headers_start; + } + default: { + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_26; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_http_complete: + s_n_llhttp__internal__n_req_http_complete: { + if (p == endp) { + return s_n_llhttp__internal__n_req_http_complete; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_25; + } + case 13: { + p++; + goto s_n_llhttp__internal__n_req_http_complete_crlf; + } + default: { + goto s_n_llhttp__internal__n_error_70; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_load_method_1: + s_n_llhttp__internal__n_invoke_load_method_1: { + switch (llhttp__internal__c_load_method(state, p, endp)) { + case 34: + goto s_n_llhttp__internal__n_req_pri_upgrade; + default: + goto s_n_llhttp__internal__n_req_http_complete; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_version_complete: + s_n_llhttp__internal__n_invoke_llhttp__on_version_complete: { + switch (llhttp__on_version_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_load_method_1; + case 21: + goto s_n_llhttp__internal__n_pause_21; + default: + goto s_n_llhttp__internal__n_error_67; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_66: + s_n_llhttp__internal__n_error_66: { + state->error = 0x9; + state->reason = "Invalid HTTP version"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_73: + s_n_llhttp__internal__n_error_73: { + state->error = 0x9; + state->reason = "Invalid minor version"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_http_minor: + s_n_llhttp__internal__n_req_http_minor: { + if (p == endp) { + return s_n_llhttp__internal__n_req_http_minor; + } + switch (*p) { + case '0': { + p++; + match = 0; + goto s_n_llhttp__internal__n_invoke_store_http_minor; + } + case '1': { + p++; + match = 1; + goto s_n_llhttp__internal__n_invoke_store_http_minor; + } + case '2': { + p++; + match = 2; + goto s_n_llhttp__internal__n_invoke_store_http_minor; + } + case '3': { + p++; + match = 3; + goto s_n_llhttp__internal__n_invoke_store_http_minor; + } + case '4': { + p++; + match = 4; + goto s_n_llhttp__internal__n_invoke_store_http_minor; + } + case '5': { + p++; + match = 5; + goto s_n_llhttp__internal__n_invoke_store_http_minor; + } + case '6': { + p++; + match = 6; + goto s_n_llhttp__internal__n_invoke_store_http_minor; + } + case '7': { + p++; + match = 7; + goto s_n_llhttp__internal__n_invoke_store_http_minor; + } + case '8': { + p++; + match = 8; + goto s_n_llhttp__internal__n_invoke_store_http_minor; + } + case '9': { + p++; + match = 9; + goto s_n_llhttp__internal__n_invoke_store_http_minor; + } + default: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_2; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_74: + s_n_llhttp__internal__n_error_74: { + state->error = 0x9; + state->reason = "Expected dot"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_http_dot: + s_n_llhttp__internal__n_req_http_dot: { + if (p == endp) { + return s_n_llhttp__internal__n_req_http_dot; + } + switch (*p) { + case '.': { + p++; + goto s_n_llhttp__internal__n_req_http_minor; + } + default: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_3; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_75: + s_n_llhttp__internal__n_error_75: { + state->error = 0x9; + state->reason = "Invalid major version"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_http_major: + s_n_llhttp__internal__n_req_http_major: { + if (p == endp) { + return s_n_llhttp__internal__n_req_http_major; + } + switch (*p) { + case '0': { + p++; + match = 0; + goto s_n_llhttp__internal__n_invoke_store_http_major; + } + case '1': { + p++; + match = 1; + goto s_n_llhttp__internal__n_invoke_store_http_major; + } + case '2': { + p++; + match = 2; + goto s_n_llhttp__internal__n_invoke_store_http_major; + } + case '3': { + p++; + match = 3; + goto s_n_llhttp__internal__n_invoke_store_http_major; + } + case '4': { + p++; + match = 4; + goto s_n_llhttp__internal__n_invoke_store_http_major; + } + case '5': { + p++; + match = 5; + goto s_n_llhttp__internal__n_invoke_store_http_major; + } + case '6': { + p++; + match = 6; + goto s_n_llhttp__internal__n_invoke_store_http_major; + } + case '7': { + p++; + match = 7; + goto s_n_llhttp__internal__n_invoke_store_http_major; + } + case '8': { + p++; + match = 8; + goto s_n_llhttp__internal__n_invoke_store_http_major; + } + case '9': { + p++; + match = 9; + goto s_n_llhttp__internal__n_invoke_store_http_major; + } + default: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_4; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_version: + s_n_llhttp__internal__n_span_start_llhttp__on_version: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_version; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_version; + goto s_n_llhttp__internal__n_req_http_major; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_http_start_1: + s_n_llhttp__internal__n_req_http_start_1: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_req_http_start_1; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob13, 4); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + goto s_n_llhttp__internal__n_invoke_load_method; + } + case kMatchPause: { + return s_n_llhttp__internal__n_req_http_start_1; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_78; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_http_start_2: + s_n_llhttp__internal__n_req_http_start_2: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_req_http_start_2; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob15, 3); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + goto s_n_llhttp__internal__n_invoke_load_method_2; + } + case kMatchPause: { + return s_n_llhttp__internal__n_req_http_start_2; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_78; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_http_start_3: + s_n_llhttp__internal__n_req_http_start_3: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_req_http_start_3; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob16, 4); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + goto s_n_llhttp__internal__n_invoke_load_method_3; + } + case kMatchPause: { + return s_n_llhttp__internal__n_req_http_start_3; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_78; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_http_start: + s_n_llhttp__internal__n_req_http_start: { + if (p == endp) { + return s_n_llhttp__internal__n_req_http_start; + } + switch (*p) { + case ' ': { + p++; + goto s_n_llhttp__internal__n_req_http_start; + } + case 'H': { + p++; + goto s_n_llhttp__internal__n_req_http_start_1; + } + case 'I': { + p++; + goto s_n_llhttp__internal__n_req_http_start_2; + } + case 'R': { + p++; + goto s_n_llhttp__internal__n_req_http_start_3; + } + default: { + goto s_n_llhttp__internal__n_error_78; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_to_http: + s_n_llhttp__internal__n_url_to_http: { + if (p == endp) { + return s_n_llhttp__internal__n_url_to_http; + } + switch (*p) { + case 9: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 12: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + default: { + goto s_n_llhttp__internal__n_invoke_llhttp__on_url_complete_1; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_skip_to_http: + s_n_llhttp__internal__n_url_skip_to_http: { + if (p == endp) { + return s_n_llhttp__internal__n_url_skip_to_http; + } + switch (*p) { + case 9: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 12: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + default: { + p++; + goto s_n_llhttp__internal__n_url_to_http; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_fragment: + s_n_llhttp__internal__n_url_fragment: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 1, 3, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + if (p == endp) { + return s_n_llhttp__internal__n_url_fragment; + } + switch (lookup_table[(uint8_t) *p]) { + case 1: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 2: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_6; + } + case 3: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_7; + } + case 4: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_8; + } + case 5: { + p++; + goto s_n_llhttp__internal__n_url_fragment; + } + default: { + goto s_n_llhttp__internal__n_error_79; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_end_stub_query_3: + s_n_llhttp__internal__n_span_end_stub_query_3: { + if (p == endp) { + return s_n_llhttp__internal__n_span_end_stub_query_3; + } + p++; + goto s_n_llhttp__internal__n_url_fragment; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_query: + s_n_llhttp__internal__n_url_query: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 1, 3, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 4, 5, 5, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + if (p == endp) { + return s_n_llhttp__internal__n_url_query; + } + switch (lookup_table[(uint8_t) *p]) { + case 1: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 2: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_9; + } + case 3: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_10; + } + case 4: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_11; + } + case 5: { + p++; + goto s_n_llhttp__internal__n_url_query; + } + case 6: { + goto s_n_llhttp__internal__n_span_end_stub_query_3; + } + default: { + goto s_n_llhttp__internal__n_error_80; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_query_or_fragment: + s_n_llhttp__internal__n_url_query_or_fragment: { + if (p == endp) { + return s_n_llhttp__internal__n_url_query_or_fragment; + } + switch (*p) { + case 9: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 10: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_3; + } + case 12: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 13: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_4; + } + case ' ': { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_5; + } + case '#': { + p++; + goto s_n_llhttp__internal__n_url_fragment; + } + case '?': { + p++; + goto s_n_llhttp__internal__n_url_query; + } + default: { + goto s_n_llhttp__internal__n_error_81; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_path: + s_n_llhttp__internal__n_url_path: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + if (p == endp) { + return s_n_llhttp__internal__n_url_path; + } + switch (lookup_table[(uint8_t) *p]) { + case 1: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 2: { + p++; + goto s_n_llhttp__internal__n_url_path; + } + default: { + goto s_n_llhttp__internal__n_url_query_or_fragment; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_stub_path_2: + s_n_llhttp__internal__n_span_start_stub_path_2: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_stub_path_2; + } + p++; + goto s_n_llhttp__internal__n_url_path; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_stub_path: + s_n_llhttp__internal__n_span_start_stub_path: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_stub_path; + } + p++; + goto s_n_llhttp__internal__n_url_path; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_stub_path_1: + s_n_llhttp__internal__n_span_start_stub_path_1: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_stub_path_1; + } + p++; + goto s_n_llhttp__internal__n_url_path; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_server_with_at: + s_n_llhttp__internal__n_url_server_with_at: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 1, 3, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 4, 5, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 0, 7, + 8, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 0, 5, + 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 5, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + if (p == endp) { + return s_n_llhttp__internal__n_url_server_with_at; + } + switch (lookup_table[(uint8_t) *p]) { + case 1: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 2: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_12; + } + case 3: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_13; + } + case 4: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_14; + } + case 5: { + p++; + goto s_n_llhttp__internal__n_url_server; + } + case 6: { + goto s_n_llhttp__internal__n_span_start_stub_path_1; + } + case 7: { + p++; + goto s_n_llhttp__internal__n_url_query; + } + case 8: { + p++; + goto s_n_llhttp__internal__n_error_82; + } + default: { + goto s_n_llhttp__internal__n_error_83; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_server: + s_n_llhttp__internal__n_url_server: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 1, 3, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 4, 5, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 0, 7, + 8, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 0, 5, + 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 5, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + if (p == endp) { + return s_n_llhttp__internal__n_url_server; + } + switch (lookup_table[(uint8_t) *p]) { + case 1: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 2: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url; + } + case 3: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_1; + } + case 4: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_url_2; + } + case 5: { + p++; + goto s_n_llhttp__internal__n_url_server; + } + case 6: { + goto s_n_llhttp__internal__n_span_start_stub_path; + } + case 7: { + p++; + goto s_n_llhttp__internal__n_url_query; + } + case 8: { + p++; + goto s_n_llhttp__internal__n_url_server_with_at; + } + default: { + goto s_n_llhttp__internal__n_error_84; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_schema_delim_1: + s_n_llhttp__internal__n_url_schema_delim_1: { + if (p == endp) { + return s_n_llhttp__internal__n_url_schema_delim_1; + } + switch (*p) { + case '/': { + p++; + goto s_n_llhttp__internal__n_url_server; + } + default: { + goto s_n_llhttp__internal__n_error_85; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_schema_delim: + s_n_llhttp__internal__n_url_schema_delim: { + if (p == endp) { + return s_n_llhttp__internal__n_url_schema_delim; + } + switch (*p) { + case 9: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 10: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 12: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 13: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case ' ': { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case '/': { + p++; + goto s_n_llhttp__internal__n_url_schema_delim_1; + } + default: { + goto s_n_llhttp__internal__n_error_85; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_end_stub_schema: + s_n_llhttp__internal__n_span_end_stub_schema: { + if (p == endp) { + return s_n_llhttp__internal__n_span_end_stub_schema; + } + p++; + goto s_n_llhttp__internal__n_url_schema_delim; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_schema: + s_n_llhttp__internal__n_url_schema: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + if (p == endp) { + return s_n_llhttp__internal__n_url_schema; + } + switch (lookup_table[(uint8_t) *p]) { + case 1: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 2: { + goto s_n_llhttp__internal__n_span_end_stub_schema; + } + case 3: { + p++; + goto s_n_llhttp__internal__n_url_schema; + } + default: { + goto s_n_llhttp__internal__n_error_86; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_start: + s_n_llhttp__internal__n_url_start: { + static uint8_t lookup_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + if (p == endp) { + return s_n_llhttp__internal__n_url_start; + } + switch (lookup_table[(uint8_t) *p]) { + case 1: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 2: { + goto s_n_llhttp__internal__n_span_start_stub_path_2; + } + case 3: { + goto s_n_llhttp__internal__n_url_schema; + } + default: { + goto s_n_llhttp__internal__n_error_87; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_url_1: + s_n_llhttp__internal__n_span_start_llhttp__on_url_1: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_url_1; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_url; + goto s_n_llhttp__internal__n_url_start; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_entry_normal: + s_n_llhttp__internal__n_url_entry_normal: { + if (p == endp) { + return s_n_llhttp__internal__n_url_entry_normal; + } + switch (*p) { + case 9: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 12: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + default: { + goto s_n_llhttp__internal__n_span_start_llhttp__on_url_1; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_url: + s_n_llhttp__internal__n_span_start_llhttp__on_url: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_url; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_url; + goto s_n_llhttp__internal__n_url_server; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_url_entry_connect: + s_n_llhttp__internal__n_url_entry_connect: { + if (p == endp) { + return s_n_llhttp__internal__n_url_entry_connect; + } + switch (*p) { + case 9: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + case 12: { + p++; + goto s_n_llhttp__internal__n_error_2; + } + default: { + goto s_n_llhttp__internal__n_span_start_llhttp__on_url; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_spaces_before_url: + s_n_llhttp__internal__n_req_spaces_before_url: { + if (p == endp) { + return s_n_llhttp__internal__n_req_spaces_before_url; + } + switch (*p) { + case ' ': { + p++; + goto s_n_llhttp__internal__n_req_spaces_before_url; + } + default: { + goto s_n_llhttp__internal__n_invoke_is_equal_method; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_first_space_before_url: + s_n_llhttp__internal__n_req_first_space_before_url: { + if (p == endp) { + return s_n_llhttp__internal__n_req_first_space_before_url; + } + switch (*p) { + case ' ': { + p++; + goto s_n_llhttp__internal__n_req_spaces_before_url; + } + default: { + goto s_n_llhttp__internal__n_error_88; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_method_complete_1: + s_n_llhttp__internal__n_invoke_llhttp__on_method_complete_1: { + switch (llhttp__on_method_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_req_first_space_before_url; + case 21: + goto s_n_llhttp__internal__n_pause_26; + default: + goto s_n_llhttp__internal__n_error_107; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_2: + s_n_llhttp__internal__n_after_start_req_2: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_2; + } + switch (*p) { + case 'L': { + p++; + match = 19; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_3: + s_n_llhttp__internal__n_after_start_req_3: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_3; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob17, 6); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 36; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_3; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_1: + s_n_llhttp__internal__n_after_start_req_1: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_1; + } + switch (*p) { + case 'C': { + p++; + goto s_n_llhttp__internal__n_after_start_req_2; + } + case 'N': { + p++; + goto s_n_llhttp__internal__n_after_start_req_3; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_4: + s_n_llhttp__internal__n_after_start_req_4: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_4; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob18, 3); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 16; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_4; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_6: + s_n_llhttp__internal__n_after_start_req_6: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_6; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob19, 6); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 22; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_6; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_8: + s_n_llhttp__internal__n_after_start_req_8: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_8; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob20, 4); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 5; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_8; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_9: + s_n_llhttp__internal__n_after_start_req_9: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_9; + } + switch (*p) { + case 'Y': { + p++; + match = 8; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_7: + s_n_llhttp__internal__n_after_start_req_7: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_7; + } + switch (*p) { + case 'N': { + p++; + goto s_n_llhttp__internal__n_after_start_req_8; + } + case 'P': { + p++; + goto s_n_llhttp__internal__n_after_start_req_9; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_5: + s_n_llhttp__internal__n_after_start_req_5: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_5; + } + switch (*p) { + case 'H': { + p++; + goto s_n_llhttp__internal__n_after_start_req_6; + } + case 'O': { + p++; + goto s_n_llhttp__internal__n_after_start_req_7; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_12: + s_n_llhttp__internal__n_after_start_req_12: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_12; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob21, 3); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 0; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_12; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_13: + s_n_llhttp__internal__n_after_start_req_13: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_13; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob22, 5); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 35; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_13; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_11: + s_n_llhttp__internal__n_after_start_req_11: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_11; + } + switch (*p) { + case 'L': { + p++; + goto s_n_llhttp__internal__n_after_start_req_12; + } + case 'S': { + p++; + goto s_n_llhttp__internal__n_after_start_req_13; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_10: + s_n_llhttp__internal__n_after_start_req_10: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_10; + } + switch (*p) { + case 'E': { + p++; + goto s_n_llhttp__internal__n_after_start_req_11; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_14: + s_n_llhttp__internal__n_after_start_req_14: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_14; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob23, 4); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 45; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_14; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_17: + s_n_llhttp__internal__n_after_start_req_17: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_17; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob25, 9); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 41; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_17; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_16: + s_n_llhttp__internal__n_after_start_req_16: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_16; + } + switch (*p) { + case '_': { + p++; + goto s_n_llhttp__internal__n_after_start_req_17; + } + default: { + match = 1; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_15: + s_n_llhttp__internal__n_after_start_req_15: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_15; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob24, 2); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + goto s_n_llhttp__internal__n_after_start_req_16; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_15; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_18: + s_n_llhttp__internal__n_after_start_req_18: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_18; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob26, 3); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 2; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_18; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_20: + s_n_llhttp__internal__n_after_start_req_20: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_20; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob27, 2); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 31; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_20; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_21: + s_n_llhttp__internal__n_after_start_req_21: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_21; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob28, 2); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 9; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_21; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_19: + s_n_llhttp__internal__n_after_start_req_19: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_19; + } + switch (*p) { + case 'I': { + p++; + goto s_n_llhttp__internal__n_after_start_req_20; + } + case 'O': { + p++; + goto s_n_llhttp__internal__n_after_start_req_21; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_23: + s_n_llhttp__internal__n_after_start_req_23: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_23; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob29, 6); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 24; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_23; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_24: + s_n_llhttp__internal__n_after_start_req_24: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_24; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob30, 3); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 23; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_24; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_26: + s_n_llhttp__internal__n_after_start_req_26: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_26; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob31, 7); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 21; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_26; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_28: + s_n_llhttp__internal__n_after_start_req_28: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_28; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob32, 6); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 30; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_28; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_29: + s_n_llhttp__internal__n_after_start_req_29: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_29; + } + switch (*p) { + case 'L': { + p++; + match = 10; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_27: + s_n_llhttp__internal__n_after_start_req_27: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_27; + } + switch (*p) { + case 'A': { + p++; + goto s_n_llhttp__internal__n_after_start_req_28; + } + case 'O': { + p++; + goto s_n_llhttp__internal__n_after_start_req_29; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_25: + s_n_llhttp__internal__n_after_start_req_25: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_25; + } + switch (*p) { + case 'A': { + p++; + goto s_n_llhttp__internal__n_after_start_req_26; + } + case 'C': { + p++; + goto s_n_llhttp__internal__n_after_start_req_27; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_30: + s_n_llhttp__internal__n_after_start_req_30: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_30; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob33, 2); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 11; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_30; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_22: + s_n_llhttp__internal__n_after_start_req_22: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_22; + } + switch (*p) { + case '-': { + p++; + goto s_n_llhttp__internal__n_after_start_req_23; + } + case 'E': { + p++; + goto s_n_llhttp__internal__n_after_start_req_24; + } + case 'K': { + p++; + goto s_n_llhttp__internal__n_after_start_req_25; + } + case 'O': { + p++; + goto s_n_llhttp__internal__n_after_start_req_30; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_31: + s_n_llhttp__internal__n_after_start_req_31: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_31; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob34, 5); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 25; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_31; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_32: + s_n_llhttp__internal__n_after_start_req_32: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_32; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob35, 6); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 6; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_32; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_35: + s_n_llhttp__internal__n_after_start_req_35: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_35; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob36, 2); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 28; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_35; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_36: + s_n_llhttp__internal__n_after_start_req_36: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_36; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob37, 2); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 39; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_36; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_34: + s_n_llhttp__internal__n_after_start_req_34: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_34; + } + switch (*p) { + case 'T': { + p++; + goto s_n_llhttp__internal__n_after_start_req_35; + } + case 'U': { + p++; + goto s_n_llhttp__internal__n_after_start_req_36; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_37: + s_n_llhttp__internal__n_after_start_req_37: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_37; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob38, 2); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 38; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_37; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_38: + s_n_llhttp__internal__n_after_start_req_38: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_38; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob39, 2); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 3; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_38; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_42: + s_n_llhttp__internal__n_after_start_req_42: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_42; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob40, 3); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 12; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_42; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_43: + s_n_llhttp__internal__n_after_start_req_43: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_43; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob41, 4); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 13; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_43; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_41: + s_n_llhttp__internal__n_after_start_req_41: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_41; + } + switch (*p) { + case 'F': { + p++; + goto s_n_llhttp__internal__n_after_start_req_42; + } + case 'P': { + p++; + goto s_n_llhttp__internal__n_after_start_req_43; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_40: + s_n_llhttp__internal__n_after_start_req_40: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_40; + } + switch (*p) { + case 'P': { + p++; + goto s_n_llhttp__internal__n_after_start_req_41; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_39: + s_n_llhttp__internal__n_after_start_req_39: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_39; + } + switch (*p) { + case 'I': { + p++; + match = 34; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case 'O': { + p++; + goto s_n_llhttp__internal__n_after_start_req_40; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_45: + s_n_llhttp__internal__n_after_start_req_45: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_45; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob42, 2); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 29; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_45; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_44: + s_n_llhttp__internal__n_after_start_req_44: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_44; + } + switch (*p) { + case 'R': { + p++; + goto s_n_llhttp__internal__n_after_start_req_45; + } + case 'T': { + p++; + match = 4; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_33: + s_n_llhttp__internal__n_after_start_req_33: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_33; + } + switch (*p) { + case 'A': { + p++; + goto s_n_llhttp__internal__n_after_start_req_34; + } + case 'L': { + p++; + goto s_n_llhttp__internal__n_after_start_req_37; + } + case 'O': { + p++; + goto s_n_llhttp__internal__n_after_start_req_38; + } + case 'R': { + p++; + goto s_n_llhttp__internal__n_after_start_req_39; + } + case 'U': { + p++; + goto s_n_llhttp__internal__n_after_start_req_44; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_46: + s_n_llhttp__internal__n_after_start_req_46: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_46; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob43, 4); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 46; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_46; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_49: + s_n_llhttp__internal__n_after_start_req_49: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_49; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob44, 3); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 17; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_49; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_50: + s_n_llhttp__internal__n_after_start_req_50: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_50; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob45, 3); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 44; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_50; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_51: + s_n_llhttp__internal__n_after_start_req_51: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_51; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob46, 5); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 43; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_51; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_52: + s_n_llhttp__internal__n_after_start_req_52: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_52; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob47, 3); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 20; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_52; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_48: + s_n_llhttp__internal__n_after_start_req_48: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_48; + } + switch (*p) { + case 'B': { + p++; + goto s_n_llhttp__internal__n_after_start_req_49; + } + case 'C': { + p++; + goto s_n_llhttp__internal__n_after_start_req_50; + } + case 'D': { + p++; + goto s_n_llhttp__internal__n_after_start_req_51; + } + case 'P': { + p++; + goto s_n_llhttp__internal__n_after_start_req_52; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_47: + s_n_llhttp__internal__n_after_start_req_47: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_47; + } + switch (*p) { + case 'E': { + p++; + goto s_n_llhttp__internal__n_after_start_req_48; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_55: + s_n_llhttp__internal__n_after_start_req_55: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_55; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob48, 3); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 14; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_55; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_57: + s_n_llhttp__internal__n_after_start_req_57: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_57; + } + switch (*p) { + case 'P': { + p++; + match = 37; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_58: + s_n_llhttp__internal__n_after_start_req_58: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_58; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob49, 9); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 42; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_58; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_56: + s_n_llhttp__internal__n_after_start_req_56: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_56; + } + switch (*p) { + case 'U': { + p++; + goto s_n_llhttp__internal__n_after_start_req_57; + } + case '_': { + p++; + goto s_n_llhttp__internal__n_after_start_req_58; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_54: + s_n_llhttp__internal__n_after_start_req_54: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_54; + } + switch (*p) { + case 'A': { + p++; + goto s_n_llhttp__internal__n_after_start_req_55; + } + case 'T': { + p++; + goto s_n_llhttp__internal__n_after_start_req_56; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_59: + s_n_llhttp__internal__n_after_start_req_59: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_59; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob50, 4); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 33; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_59; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_60: + s_n_llhttp__internal__n_after_start_req_60: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_60; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob51, 7); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 26; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_60; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_53: + s_n_llhttp__internal__n_after_start_req_53: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_53; + } + switch (*p) { + case 'E': { + p++; + goto s_n_llhttp__internal__n_after_start_req_54; + } + case 'O': { + p++; + goto s_n_llhttp__internal__n_after_start_req_59; + } + case 'U': { + p++; + goto s_n_llhttp__internal__n_after_start_req_60; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_62: + s_n_llhttp__internal__n_after_start_req_62: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_62; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob52, 6); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 40; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_62; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_63: + s_n_llhttp__internal__n_after_start_req_63: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_63; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob53, 3); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 7; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_63; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_61: + s_n_llhttp__internal__n_after_start_req_61: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_61; + } + switch (*p) { + case 'E': { + p++; + goto s_n_llhttp__internal__n_after_start_req_62; + } + case 'R': { + p++; + goto s_n_llhttp__internal__n_after_start_req_63; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_66: + s_n_llhttp__internal__n_after_start_req_66: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_66; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob54, 3); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 18; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_66; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_68: + s_n_llhttp__internal__n_after_start_req_68: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_68; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob55, 2); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 32; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_68; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_69: + s_n_llhttp__internal__n_after_start_req_69: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_69; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob56, 2); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 15; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_69; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_67: + s_n_llhttp__internal__n_after_start_req_67: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_67; + } + switch (*p) { + case 'I': { + p++; + goto s_n_llhttp__internal__n_after_start_req_68; + } + case 'O': { + p++; + goto s_n_llhttp__internal__n_after_start_req_69; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_70: + s_n_llhttp__internal__n_after_start_req_70: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_70; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob57, 8); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 27; + goto s_n_llhttp__internal__n_invoke_store_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_after_start_req_70; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_65: + s_n_llhttp__internal__n_after_start_req_65: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_65; + } + switch (*p) { + case 'B': { + p++; + goto s_n_llhttp__internal__n_after_start_req_66; + } + case 'L': { + p++; + goto s_n_llhttp__internal__n_after_start_req_67; + } + case 'S': { + p++; + goto s_n_llhttp__internal__n_after_start_req_70; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req_64: + s_n_llhttp__internal__n_after_start_req_64: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req_64; + } + switch (*p) { + case 'N': { + p++; + goto s_n_llhttp__internal__n_after_start_req_65; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_after_start_req: + s_n_llhttp__internal__n_after_start_req: { + if (p == endp) { + return s_n_llhttp__internal__n_after_start_req; + } + switch (*p) { + case 'A': { + p++; + goto s_n_llhttp__internal__n_after_start_req_1; + } + case 'B': { + p++; + goto s_n_llhttp__internal__n_after_start_req_4; + } + case 'C': { + p++; + goto s_n_llhttp__internal__n_after_start_req_5; + } + case 'D': { + p++; + goto s_n_llhttp__internal__n_after_start_req_10; + } + case 'F': { + p++; + goto s_n_llhttp__internal__n_after_start_req_14; + } + case 'G': { + p++; + goto s_n_llhttp__internal__n_after_start_req_15; + } + case 'H': { + p++; + goto s_n_llhttp__internal__n_after_start_req_18; + } + case 'L': { + p++; + goto s_n_llhttp__internal__n_after_start_req_19; + } + case 'M': { + p++; + goto s_n_llhttp__internal__n_after_start_req_22; + } + case 'N': { + p++; + goto s_n_llhttp__internal__n_after_start_req_31; + } + case 'O': { + p++; + goto s_n_llhttp__internal__n_after_start_req_32; + } + case 'P': { + p++; + goto s_n_llhttp__internal__n_after_start_req_33; + } + case 'Q': { + p++; + goto s_n_llhttp__internal__n_after_start_req_46; + } + case 'R': { + p++; + goto s_n_llhttp__internal__n_after_start_req_47; + } + case 'S': { + p++; + goto s_n_llhttp__internal__n_after_start_req_53; + } + case 'T': { + p++; + goto s_n_llhttp__internal__n_after_start_req_61; + } + case 'U': { + p++; + goto s_n_llhttp__internal__n_after_start_req_64; + } + default: { + goto s_n_llhttp__internal__n_error_108; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_method_1: + s_n_llhttp__internal__n_span_start_llhttp__on_method_1: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_method_1; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_method; + goto s_n_llhttp__internal__n_after_start_req; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_res_line_almost_done: + s_n_llhttp__internal__n_res_line_almost_done: { + if (p == endp) { + return s_n_llhttp__internal__n_res_line_almost_done; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_invoke_llhttp__on_status_complete; + } + case 13: { + p++; + goto s_n_llhttp__internal__n_invoke_llhttp__on_status_complete; + } + default: { + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_29; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_test_lenient_flags_30: + s_n_llhttp__internal__n_invoke_test_lenient_flags_30: { + switch (llhttp__internal__c_test_lenient_flags_1(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_llhttp__on_status_complete; + default: + goto s_n_llhttp__internal__n_error_94; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_res_status: + s_n_llhttp__internal__n_res_status: { + if (p == endp) { + return s_n_llhttp__internal__n_res_status; + } + switch (*p) { + case 10: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_status; + } + case 13: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_status_1; + } + default: { + p++; + goto s_n_llhttp__internal__n_res_status; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_status: + s_n_llhttp__internal__n_span_start_llhttp__on_status: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_status; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_status; + goto s_n_llhttp__internal__n_res_status; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_res_status_code_otherwise: + s_n_llhttp__internal__n_res_status_code_otherwise: { + if (p == endp) { + return s_n_llhttp__internal__n_res_status_code_otherwise; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_28; + } + case 13: { + p++; + goto s_n_llhttp__internal__n_res_line_almost_done; + } + case ' ': { + p++; + goto s_n_llhttp__internal__n_span_start_llhttp__on_status; + } + default: { + goto s_n_llhttp__internal__n_error_95; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_res_status_code_digit_3: + s_n_llhttp__internal__n_res_status_code_digit_3: { + if (p == endp) { + return s_n_llhttp__internal__n_res_status_code_digit_3; + } + switch (*p) { + case '0': { + p++; + match = 0; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_2; + } + case '1': { + p++; + match = 1; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_2; + } + case '2': { + p++; + match = 2; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_2; + } + case '3': { + p++; + match = 3; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_2; + } + case '4': { + p++; + match = 4; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_2; + } + case '5': { + p++; + match = 5; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_2; + } + case '6': { + p++; + match = 6; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_2; + } + case '7': { + p++; + match = 7; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_2; + } + case '8': { + p++; + match = 8; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_2; + } + case '9': { + p++; + match = 9; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_2; + } + default: { + goto s_n_llhttp__internal__n_error_97; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_res_status_code_digit_2: + s_n_llhttp__internal__n_res_status_code_digit_2: { + if (p == endp) { + return s_n_llhttp__internal__n_res_status_code_digit_2; + } + switch (*p) { + case '0': { + p++; + match = 0; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_1; + } + case '1': { + p++; + match = 1; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_1; + } + case '2': { + p++; + match = 2; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_1; + } + case '3': { + p++; + match = 3; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_1; + } + case '4': { + p++; + match = 4; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_1; + } + case '5': { + p++; + match = 5; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_1; + } + case '6': { + p++; + match = 6; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_1; + } + case '7': { + p++; + match = 7; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_1; + } + case '8': { + p++; + match = 8; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_1; + } + case '9': { + p++; + match = 9; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code_1; + } + default: { + goto s_n_llhttp__internal__n_error_99; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_res_status_code_digit_1: + s_n_llhttp__internal__n_res_status_code_digit_1: { + if (p == endp) { + return s_n_llhttp__internal__n_res_status_code_digit_1; + } + switch (*p) { + case '0': { + p++; + match = 0; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code; + } + case '1': { + p++; + match = 1; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code; + } + case '2': { + p++; + match = 2; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code; + } + case '3': { + p++; + match = 3; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code; + } + case '4': { + p++; + match = 4; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code; + } + case '5': { + p++; + match = 5; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code; + } + case '6': { + p++; + match = 6; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code; + } + case '7': { + p++; + match = 7; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code; + } + case '8': { + p++; + match = 8; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code; + } + case '9': { + p++; + match = 9; + goto s_n_llhttp__internal__n_invoke_mul_add_status_code; + } + default: { + goto s_n_llhttp__internal__n_error_101; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_res_after_version: + s_n_llhttp__internal__n_res_after_version: { + if (p == endp) { + return s_n_llhttp__internal__n_res_after_version; + } + switch (*p) { + case ' ': { + p++; + goto s_n_llhttp__internal__n_invoke_update_status_code; + } + default: { + goto s_n_llhttp__internal__n_error_102; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_version_complete_1: + s_n_llhttp__internal__n_invoke_llhttp__on_version_complete_1: { + switch (llhttp__on_version_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_res_after_version; + case 21: + goto s_n_llhttp__internal__n_pause_25; + default: + goto s_n_llhttp__internal__n_error_90; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_89: + s_n_llhttp__internal__n_error_89: { + state->error = 0x9; + state->reason = "Invalid HTTP version"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_103: + s_n_llhttp__internal__n_error_103: { + state->error = 0x9; + state->reason = "Invalid minor version"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_res_http_minor: + s_n_llhttp__internal__n_res_http_minor: { + if (p == endp) { + return s_n_llhttp__internal__n_res_http_minor; + } + switch (*p) { + case '0': { + p++; + match = 0; + goto s_n_llhttp__internal__n_invoke_store_http_minor_1; + } + case '1': { + p++; + match = 1; + goto s_n_llhttp__internal__n_invoke_store_http_minor_1; + } + case '2': { + p++; + match = 2; + goto s_n_llhttp__internal__n_invoke_store_http_minor_1; + } + case '3': { + p++; + match = 3; + goto s_n_llhttp__internal__n_invoke_store_http_minor_1; + } + case '4': { + p++; + match = 4; + goto s_n_llhttp__internal__n_invoke_store_http_minor_1; + } + case '5': { + p++; + match = 5; + goto s_n_llhttp__internal__n_invoke_store_http_minor_1; + } + case '6': { + p++; + match = 6; + goto s_n_llhttp__internal__n_invoke_store_http_minor_1; + } + case '7': { + p++; + match = 7; + goto s_n_llhttp__internal__n_invoke_store_http_minor_1; + } + case '8': { + p++; + match = 8; + goto s_n_llhttp__internal__n_invoke_store_http_minor_1; + } + case '9': { + p++; + match = 9; + goto s_n_llhttp__internal__n_invoke_store_http_minor_1; + } + default: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_7; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_104: + s_n_llhttp__internal__n_error_104: { + state->error = 0x9; + state->reason = "Expected dot"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_res_http_dot: + s_n_llhttp__internal__n_res_http_dot: { + if (p == endp) { + return s_n_llhttp__internal__n_res_http_dot; + } + switch (*p) { + case '.': { + p++; + goto s_n_llhttp__internal__n_res_http_minor; + } + default: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_8; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_error_105: + s_n_llhttp__internal__n_error_105: { + state->error = 0x9; + state->reason = "Invalid major version"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_res_http_major: + s_n_llhttp__internal__n_res_http_major: { + if (p == endp) { + return s_n_llhttp__internal__n_res_http_major; + } + switch (*p) { + case '0': { + p++; + match = 0; + goto s_n_llhttp__internal__n_invoke_store_http_major_1; + } + case '1': { + p++; + match = 1; + goto s_n_llhttp__internal__n_invoke_store_http_major_1; + } + case '2': { + p++; + match = 2; + goto s_n_llhttp__internal__n_invoke_store_http_major_1; + } + case '3': { + p++; + match = 3; + goto s_n_llhttp__internal__n_invoke_store_http_major_1; + } + case '4': { + p++; + match = 4; + goto s_n_llhttp__internal__n_invoke_store_http_major_1; + } + case '5': { + p++; + match = 5; + goto s_n_llhttp__internal__n_invoke_store_http_major_1; + } + case '6': { + p++; + match = 6; + goto s_n_llhttp__internal__n_invoke_store_http_major_1; + } + case '7': { + p++; + match = 7; + goto s_n_llhttp__internal__n_invoke_store_http_major_1; + } + case '8': { + p++; + match = 8; + goto s_n_llhttp__internal__n_invoke_store_http_major_1; + } + case '9': { + p++; + match = 9; + goto s_n_llhttp__internal__n_invoke_store_http_major_1; + } + default: { + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_9; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_version_1: + s_n_llhttp__internal__n_span_start_llhttp__on_version_1: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_version_1; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_version; + goto s_n_llhttp__internal__n_res_http_major; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_start_res: + s_n_llhttp__internal__n_start_res: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_start_res; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob58, 5); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + goto s_n_llhttp__internal__n_span_start_llhttp__on_version_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_start_res; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_109; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_llhttp__on_method_complete: + s_n_llhttp__internal__n_invoke_llhttp__on_method_complete: { + switch (llhttp__on_method_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_req_first_space_before_url; + case 21: + goto s_n_llhttp__internal__n_pause_23; + default: + goto s_n_llhttp__internal__n_error_1; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_or_res_method_2: + s_n_llhttp__internal__n_req_or_res_method_2: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_req_or_res_method_2; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob59, 2); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + match = 2; + goto s_n_llhttp__internal__n_invoke_store_method; + } + case kMatchPause: { + return s_n_llhttp__internal__n_req_or_res_method_2; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_106; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_update_type_1: + s_n_llhttp__internal__n_invoke_update_type_1: { + switch (llhttp__internal__c_update_type_1(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version_1; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_or_res_method_3: + s_n_llhttp__internal__n_req_or_res_method_3: { + llparse_match_t match_seq; + + if (p == endp) { + return s_n_llhttp__internal__n_req_or_res_method_3; + } + match_seq = llparse__match_sequence_id(state, p, endp, llparse_blob60, 3); + p = match_seq.current; + switch (match_seq.status) { + case kMatchComplete: { + p++; + goto s_n_llhttp__internal__n_span_end_llhttp__on_method_1; + } + case kMatchPause: { + return s_n_llhttp__internal__n_req_or_res_method_3; + } + case kMatchMismatch: { + goto s_n_llhttp__internal__n_error_106; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_or_res_method_1: + s_n_llhttp__internal__n_req_or_res_method_1: { + if (p == endp) { + return s_n_llhttp__internal__n_req_or_res_method_1; + } + switch (*p) { + case 'E': { + p++; + goto s_n_llhttp__internal__n_req_or_res_method_2; + } + case 'T': { + p++; + goto s_n_llhttp__internal__n_req_or_res_method_3; + } + default: { + goto s_n_llhttp__internal__n_error_106; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_req_or_res_method: + s_n_llhttp__internal__n_req_or_res_method: { + if (p == endp) { + return s_n_llhttp__internal__n_req_or_res_method; + } + switch (*p) { + case 'H': { + p++; + goto s_n_llhttp__internal__n_req_or_res_method_1; + } + default: { + goto s_n_llhttp__internal__n_error_106; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_span_start_llhttp__on_method: + s_n_llhttp__internal__n_span_start_llhttp__on_method: { + if (p == endp) { + return s_n_llhttp__internal__n_span_start_llhttp__on_method; + } + state->_span_pos0 = (void*) p; + state->_span_cb0 = llhttp__on_method; + goto s_n_llhttp__internal__n_req_or_res_method; + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_start_req_or_res: + s_n_llhttp__internal__n_start_req_or_res: { + if (p == endp) { + return s_n_llhttp__internal__n_start_req_or_res; + } + switch (*p) { + case 'H': { + goto s_n_llhttp__internal__n_span_start_llhttp__on_method; + } + default: { + goto s_n_llhttp__internal__n_invoke_update_type_2; + } + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_load_type: + s_n_llhttp__internal__n_invoke_load_type: { + switch (llhttp__internal__c_load_type(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_span_start_llhttp__on_method_1; + case 2: + goto s_n_llhttp__internal__n_start_res; + default: + goto s_n_llhttp__internal__n_start_req_or_res; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_invoke_update_finish: + s_n_llhttp__internal__n_invoke_update_finish: { + switch (llhttp__internal__c_update_finish(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_llhttp__on_message_begin; + } + /* UNREACHABLE */; + abort(); + } + case s_n_llhttp__internal__n_start: + s_n_llhttp__internal__n_start: { + if (p == endp) { + return s_n_llhttp__internal__n_start; + } + switch (*p) { + case 10: { + p++; + goto s_n_llhttp__internal__n_start; + } + case 13: { + p++; + goto s_n_llhttp__internal__n_start; + } + default: { + goto s_n_llhttp__internal__n_invoke_load_initial_message_completed; + } + } + /* UNREACHABLE */; + abort(); + } + default: + /* UNREACHABLE */ + abort(); + } + s_n_llhttp__internal__n_error_2: { + state->error = 0x7; + state->reason = "Invalid characters in url"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_finish_2: { + switch (llhttp__internal__c_update_finish_1(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_start; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_initial_message_completed: { + switch (llhttp__internal__c_update_initial_message_completed(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_update_finish_2; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_content_length: { + switch (llhttp__internal__c_update_content_length(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_update_initial_message_completed; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_8: { + state->error = 0x5; + state->reason = "Data after `Connection: close`"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_3: { + switch (llhttp__internal__c_test_lenient_flags_3(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_closed; + default: + goto s_n_llhttp__internal__n_error_8; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_2: { + switch (llhttp__internal__c_test_lenient_flags_2(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_update_initial_message_completed; + default: + goto s_n_llhttp__internal__n_closed; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_finish_1: { + switch (llhttp__internal__c_update_finish_1(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_2; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_13: { + state->error = 0x15; + state->reason = "on_message_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_is_equal_upgrade; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_38: { + state->error = 0x12; + state->reason = "`on_message_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_15: { + state->error = 0x15; + state->reason = "on_chunk_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_message_complete_2; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_40: { + state->error = 0x14; + state->reason = "`on_chunk_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_complete_1: { + switch (llhttp__on_chunk_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_llhttp__on_message_complete_2; + case 21: + goto s_n_llhttp__internal__n_pause_15; + default: + goto s_n_llhttp__internal__n_error_40; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_2: { + state->error = 0x15; + state->reason = "on_message_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_pause_1; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_9: { + state->error = 0x12; + state->reason = "`on_message_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_message_complete_1: { + switch (llhttp__on_message_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_pause_1; + case 21: + goto s_n_llhttp__internal__n_pause_2; + default: + goto s_n_llhttp__internal__n_error_9; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_36: { + state->error = 0xc; + state->reason = "Chunk size overflow"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_10: { + state->error = 0xc; + state->reason = "Invalid character in chunk size"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_4: { + switch (llhttp__internal__c_test_lenient_flags_4(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_chunk_size_otherwise; + default: + goto s_n_llhttp__internal__n_error_10; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_3: { + state->error = 0x15; + state->reason = "on_chunk_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_update_content_length_1; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_14: { + state->error = 0x14; + state->reason = "`on_chunk_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_complete: { + switch (llhttp__on_chunk_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_update_content_length_1; + case 21: + goto s_n_llhttp__internal__n_pause_3; + default: + goto s_n_llhttp__internal__n_error_14; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_13: { + state->error = 0x19; + state->reason = "Missing expected CR after chunk data"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_6: { + switch (llhttp__internal__c_test_lenient_flags_1(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_complete; + default: + goto s_n_llhttp__internal__n_error_13; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_15: { + state->error = 0x2; + state->reason = "Expected LF after chunk data"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_7: { + switch (llhttp__internal__c_test_lenient_flags_7(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_complete; + default: + goto s_n_llhttp__internal__n_error_15; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_body: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_body(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_chunk_data_almost_done; + return s_error; + } + goto s_n_llhttp__internal__n_chunk_data_almost_done; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags: { + switch (llhttp__internal__c_or_flags(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_header_field_start; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_4: { + state->error = 0x15; + state->reason = "on_chunk_header pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_is_equal_content_length; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_12: { + state->error = 0x13; + state->reason = "`on_chunk_header` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_header: { + switch (llhttp__on_chunk_header(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_is_equal_content_length; + case 21: + goto s_n_llhttp__internal__n_pause_4; + default: + goto s_n_llhttp__internal__n_error_12; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_16: { + state->error = 0x2; + state->reason = "Expected LF after chunk size"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_8: { + switch (llhttp__internal__c_test_lenient_flags_8(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_header; + default: + goto s_n_llhttp__internal__n_error_16; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_11: { + state->error = 0x19; + state->reason = "Missing expected CR after chunk size"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_5: { + switch (llhttp__internal__c_test_lenient_flags_1(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_chunk_size_almost_done; + default: + goto s_n_llhttp__internal__n_error_11; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_17: { + state->error = 0x2; + state->reason = "Invalid character in chunk extensions"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_18: { + state->error = 0x2; + state->reason = "Invalid character in chunk extensions"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_20: { + state->error = 0x19; + state->reason = "Missing expected CR after chunk extension name"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_5: { + state->error = 0x15; + state->reason = "on_chunk_extension_name pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_test_lenient_flags_9; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_19: { + state->error = 0x22; + state->reason = "`on_chunk_extension_name` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_name: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_chunk_extension_name(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete; + return s_error; + } + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_6: { + state->error = 0x15; + state->reason = "on_chunk_extension_name pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_chunk_size_almost_done; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_21: { + state->error = 0x22; + state->reason = "`on_chunk_extension_name` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_name_1: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_chunk_extension_name(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete_1; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete_1; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_7: { + state->error = 0x15; + state->reason = "on_chunk_extension_name pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_chunk_extensions; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_22: { + state->error = 0x22; + state->reason = "`on_chunk_extension_name` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_name_2: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_chunk_extension_name(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete_2; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete_2; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_25: { + state->error = 0x19; + state->reason = "Missing expected CR after chunk extension value"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_8: { + state->error = 0x15; + state->reason = "on_chunk_extension_value pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_test_lenient_flags_10; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_24: { + state->error = 0x23; + state->reason = "`on_chunk_extension_value` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_chunk_extension_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete; + return s_error; + } + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_9: { + state->error = 0x15; + state->reason = "on_chunk_extension_value pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_chunk_size_almost_done; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_26: { + state->error = 0x23; + state->reason = "`on_chunk_extension_value` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value_1: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_chunk_extension_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_1; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_1; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_28: { + state->error = 0x19; + state->reason = "Missing expected CR after chunk extension value"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_11: { + switch (llhttp__internal__c_test_lenient_flags_1(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_chunk_size_almost_done; + default: + goto s_n_llhttp__internal__n_error_28; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_29: { + state->error = 0x2; + state->reason = "Invalid character in chunk extensions quote value"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_10: { + state->error = 0x15; + state->reason = "on_chunk_extension_value pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_chunk_extension_quoted_value_done; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_27: { + state->error = 0x23; + state->reason = "`on_chunk_extension_value` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value_2: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_chunk_extension_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_2; + return s_error; + } + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_2; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value_3: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_chunk_extension_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_30; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_error_30; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value_4: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_chunk_extension_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_31; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_error_31; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_11: { + state->error = 0x15; + state->reason = "on_chunk_extension_value pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_chunk_extensions; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_32: { + state->error = 0x23; + state->reason = "`on_chunk_extension_value` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value_5: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_chunk_extension_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_3; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_value_complete_3; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_value_6: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_chunk_extension_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_33; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_error_33; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_12: { + state->error = 0x15; + state->reason = "on_chunk_extension_name pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_chunk_extension_value; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_23: { + state->error = 0x22; + state->reason = "`on_chunk_extension_name` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_extension_name_complete_3: { + switch (llhttp__on_chunk_extension_name_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_chunk_extension_value; + case 21: + goto s_n_llhttp__internal__n_pause_12; + default: + goto s_n_llhttp__internal__n_error_23; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_name_3: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_chunk_extension_name(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_span_start_llhttp__on_chunk_extension_value; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_span_start_llhttp__on_chunk_extension_value; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_chunk_extension_name_4: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_chunk_extension_name(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_34; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_error_34; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_35: { + state->error = 0xc; + state->reason = "Invalid character in chunk size"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_mul_add_content_length: { + switch (llhttp__internal__c_mul_add_content_length(state, p, endp, match)) { + case 1: + goto s_n_llhttp__internal__n_error_36; + default: + goto s_n_llhttp__internal__n_chunk_size; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_37: { + state->error = 0xc; + state->reason = "Invalid character in chunk size"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_body_1: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_body(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_message_complete_2; + return s_error; + } + goto s_n_llhttp__internal__n_invoke_llhttp__on_message_complete_2; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_finish_3: { + switch (llhttp__internal__c_update_finish_3(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_span_start_llhttp__on_body_2; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_39: { + state->error = 0xf; + state->reason = "Request has invalid `Transfer-Encoding`"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause: { + state->error = 0x15; + state->reason = "on_message_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__after_message_complete; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_7: { + state->error = 0x12; + state->reason = "`on_message_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_message_complete: { + switch (llhttp__on_message_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_llhttp__after_message_complete; + case 21: + goto s_n_llhttp__internal__n_pause; + default: + goto s_n_llhttp__internal__n_error_7; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_1: { + switch (llhttp__internal__c_or_flags_1(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_llhttp__after_headers_complete; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_2: { + switch (llhttp__internal__c_or_flags_1(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_llhttp__after_headers_complete; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_upgrade: { + switch (llhttp__internal__c_update_upgrade(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_or_flags_2; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_14: { + state->error = 0x15; + state->reason = "Paused by on_headers_complete"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__after_headers_complete; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_6: { + state->error = 0x11; + state->reason = "User callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_headers_complete: { + switch (llhttp__on_headers_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_llhttp__after_headers_complete; + case 1: + goto s_n_llhttp__internal__n_invoke_or_flags_1; + case 2: + goto s_n_llhttp__internal__n_invoke_update_upgrade; + case 21: + goto s_n_llhttp__internal__n_pause_14; + default: + goto s_n_llhttp__internal__n_error_6; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__before_headers_complete: { + switch (llhttp__before_headers_complete(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_llhttp__on_headers_complete; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_flags: { + switch (llhttp__internal__c_test_flags(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_complete_1; + default: + goto s_n_llhttp__internal__n_invoke_llhttp__before_headers_complete; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_1: { + switch (llhttp__internal__c_test_lenient_flags_1(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_test_flags; + default: + goto s_n_llhttp__internal__n_error_5; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_17: { + state->error = 0x15; + state->reason = "on_chunk_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_message_complete_2; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_42: { + state->error = 0x14; + state->reason = "`on_chunk_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_chunk_complete_2: { + switch (llhttp__on_chunk_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_llhttp__on_message_complete_2; + case 21: + goto s_n_llhttp__internal__n_pause_17; + default: + goto s_n_llhttp__internal__n_error_42; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_3: { + switch (llhttp__internal__c_or_flags_1(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_llhttp__after_headers_complete; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_4: { + switch (llhttp__internal__c_or_flags_1(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_llhttp__after_headers_complete; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_upgrade_1: { + switch (llhttp__internal__c_update_upgrade(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_or_flags_4; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_16: { + state->error = 0x15; + state->reason = "Paused by on_headers_complete"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__after_headers_complete; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_41: { + state->error = 0x11; + state->reason = "User callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_headers_complete_1: { + switch (llhttp__on_headers_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_llhttp__after_headers_complete; + case 1: + goto s_n_llhttp__internal__n_invoke_or_flags_3; + case 2: + goto s_n_llhttp__internal__n_invoke_update_upgrade_1; + case 21: + goto s_n_llhttp__internal__n_pause_16; + default: + goto s_n_llhttp__internal__n_error_41; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__before_headers_complete_1: { + switch (llhttp__before_headers_complete(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_llhttp__on_headers_complete_1; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_flags_1: { + switch (llhttp__internal__c_test_flags(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_llhttp__on_chunk_complete_2; + default: + goto s_n_llhttp__internal__n_invoke_llhttp__before_headers_complete_1; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_43: { + state->error = 0x2; + state->reason = "Expected LF after headers"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_12: { + switch (llhttp__internal__c_test_lenient_flags_8(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_test_flags_1; + default: + goto s_n_llhttp__internal__n_error_43; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_44: { + state->error = 0xa; + state->reason = "Invalid header token"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_header_field: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_header_field(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_5; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_error_5; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_13: { + switch (llhttp__internal__c_test_lenient_flags(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_header_field_colon_discard_ws; + default: + goto s_n_llhttp__internal__n_span_end_llhttp__on_header_field; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_60: { + state->error = 0xb; + state->reason = "Content-Length can't be present with Transfer-Encoding"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_47: { + state->error = 0xa; + state->reason = "Invalid header value char"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_15: { + switch (llhttp__internal__c_test_lenient_flags(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_header_value_discard_ws; + default: + goto s_n_llhttp__internal__n_error_47; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_49: { + state->error = 0xb; + state->reason = "Empty Content-Length"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_18: { + state->error = 0x15; + state->reason = "on_header_value_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_header_field_start; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_48: { + state->error = 0x1d; + state->reason = "`on_header_value_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_header_value: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_header_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_header_value_complete; + return s_error; + } + goto s_n_llhttp__internal__n_invoke_llhttp__on_header_value_complete; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_header_state: { + switch (llhttp__internal__c_update_header_state(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_span_start_llhttp__on_header_value; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_5: { + switch (llhttp__internal__c_or_flags_5(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_update_header_state; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_6: { + switch (llhttp__internal__c_or_flags_6(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_update_header_state; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_7: { + switch (llhttp__internal__c_or_flags_7(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_update_header_state; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_8: { + switch (llhttp__internal__c_or_flags_8(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_span_start_llhttp__on_header_value; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_header_state_2: { + switch (llhttp__internal__c_load_header_state(state, p, endp)) { + case 5: + goto s_n_llhttp__internal__n_invoke_or_flags_5; + case 6: + goto s_n_llhttp__internal__n_invoke_or_flags_6; + case 7: + goto s_n_llhttp__internal__n_invoke_or_flags_7; + case 8: + goto s_n_llhttp__internal__n_invoke_or_flags_8; + default: + goto s_n_llhttp__internal__n_span_start_llhttp__on_header_value; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_header_state_1: { + switch (llhttp__internal__c_load_header_state(state, p, endp)) { + case 2: + goto s_n_llhttp__internal__n_error_49; + default: + goto s_n_llhttp__internal__n_invoke_load_header_state_2; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_46: { + state->error = 0xa; + state->reason = "Invalid header value char"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_14: { + switch (llhttp__internal__c_test_lenient_flags_1(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_header_value_discard_lws; + default: + goto s_n_llhttp__internal__n_error_46; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_50: { + state->error = 0x2; + state->reason = "Expected LF after CR"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_16: { + switch (llhttp__internal__c_test_lenient_flags(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_header_value_discard_lws; + default: + goto s_n_llhttp__internal__n_error_50; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_header_state_1: { + switch (llhttp__internal__c_update_header_state_1(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_span_start_llhttp__on_header_value_1; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_header_state_4: { + switch (llhttp__internal__c_load_header_state(state, p, endp)) { + case 8: + goto s_n_llhttp__internal__n_invoke_update_header_state_1; + default: + goto s_n_llhttp__internal__n_span_start_llhttp__on_header_value_1; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_52: { + state->error = 0xa; + state->reason = "Unexpected whitespace after header value"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_18: { + switch (llhttp__internal__c_test_lenient_flags(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_load_header_state_4; + default: + goto s_n_llhttp__internal__n_error_52; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_header_state_2: { + switch (llhttp__internal__c_update_header_state(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_llhttp__on_header_value_complete; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_9: { + switch (llhttp__internal__c_or_flags_5(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_update_header_state_2; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_10: { + switch (llhttp__internal__c_or_flags_6(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_update_header_state_2; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_11: { + switch (llhttp__internal__c_or_flags_7(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_update_header_state_2; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_12: { + switch (llhttp__internal__c_or_flags_8(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_llhttp__on_header_value_complete; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_header_state_5: { + switch (llhttp__internal__c_load_header_state(state, p, endp)) { + case 5: + goto s_n_llhttp__internal__n_invoke_or_flags_9; + case 6: + goto s_n_llhttp__internal__n_invoke_or_flags_10; + case 7: + goto s_n_llhttp__internal__n_invoke_or_flags_11; + case 8: + goto s_n_llhttp__internal__n_invoke_or_flags_12; + default: + goto s_n_llhttp__internal__n_invoke_llhttp__on_header_value_complete; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_53: { + state->error = 0x3; + state->reason = "Missing expected LF after header value"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_51: { + state->error = 0x19; + state->reason = "Missing expected CR after header value"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_header_value_1: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_header_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_test_lenient_flags_17; + return s_error; + } + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_17; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_header_value_2: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_header_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_header_value_almost_done; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_header_value_almost_done; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_header_value_4: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_header_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_header_value_almost_done; + return s_error; + } + goto s_n_llhttp__internal__n_header_value_almost_done; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_header_value_5: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_header_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_header_value_almost_done; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_header_value_almost_done; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_header_value_3: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_header_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_54; + return s_error; + } + goto s_n_llhttp__internal__n_error_54; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_19: { + switch (llhttp__internal__c_test_lenient_flags(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_header_value_lenient; + default: + goto s_n_llhttp__internal__n_span_end_llhttp__on_header_value_3; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_header_state_4: { + switch (llhttp__internal__c_update_header_state(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_header_value_connection; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_13: { + switch (llhttp__internal__c_or_flags_5(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_update_header_state_4; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_14: { + switch (llhttp__internal__c_or_flags_6(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_update_header_state_4; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_15: { + switch (llhttp__internal__c_or_flags_7(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_update_header_state_4; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_16: { + switch (llhttp__internal__c_or_flags_8(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_header_value_connection; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_header_state_6: { + switch (llhttp__internal__c_load_header_state(state, p, endp)) { + case 5: + goto s_n_llhttp__internal__n_invoke_or_flags_13; + case 6: + goto s_n_llhttp__internal__n_invoke_or_flags_14; + case 7: + goto s_n_llhttp__internal__n_invoke_or_flags_15; + case 8: + goto s_n_llhttp__internal__n_invoke_or_flags_16; + default: + goto s_n_llhttp__internal__n_header_value_connection; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_header_state_5: { + switch (llhttp__internal__c_update_header_state_1(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_header_value_connection_token; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_header_state_3: { + switch (llhttp__internal__c_update_header_state_3(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_header_value_connection_ws; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_header_state_6: { + switch (llhttp__internal__c_update_header_state_6(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_header_value_connection_ws; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_header_state_7: { + switch (llhttp__internal__c_update_header_state_7(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_header_value_connection_ws; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_header_value_6: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_header_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_56; + return s_error; + } + goto s_n_llhttp__internal__n_error_56; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_mul_add_content_length_1: { + switch (llhttp__internal__c_mul_add_content_length_1(state, p, endp, match)) { + case 1: + goto s_n_llhttp__internal__n_span_end_llhttp__on_header_value_6; + default: + goto s_n_llhttp__internal__n_header_value_content_length; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_17: { + switch (llhttp__internal__c_or_flags_17(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_header_value_otherwise; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_header_value_7: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_header_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_57; + return s_error; + } + goto s_n_llhttp__internal__n_error_57; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_55: { + state->error = 0x4; + state->reason = "Duplicate Content-Length"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_flags_2: { + switch (llhttp__internal__c_test_flags_2(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_header_value_content_length; + default: + goto s_n_llhttp__internal__n_error_55; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_header_value_9: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_header_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_59; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_error_59; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_header_state_8: { + switch (llhttp__internal__c_update_header_state_8(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_header_value_otherwise; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_header_value_8: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_header_value(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_58; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_error_58; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_20: { + switch (llhttp__internal__c_test_lenient_flags_20(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_span_end_llhttp__on_header_value_8; + default: + goto s_n_llhttp__internal__n_header_value_te_chunked; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_type_1: { + switch (llhttp__internal__c_load_type(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_20; + default: + goto s_n_llhttp__internal__n_header_value_te_chunked; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_header_state_9: { + switch (llhttp__internal__c_update_header_state_1(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_header_value; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_and_flags: { + switch (llhttp__internal__c_and_flags(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_header_value_te_chunked; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_19: { + switch (llhttp__internal__c_or_flags_18(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_and_flags; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_21: { + switch (llhttp__internal__c_test_lenient_flags_20(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_span_end_llhttp__on_header_value_9; + default: + goto s_n_llhttp__internal__n_invoke_or_flags_19; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_type_2: { + switch (llhttp__internal__c_load_type(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_21; + default: + goto s_n_llhttp__internal__n_invoke_or_flags_19; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_18: { + switch (llhttp__internal__c_or_flags_18(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_and_flags; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_flags_3: { + switch (llhttp__internal__c_test_flags_3(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_load_type_2; + default: + goto s_n_llhttp__internal__n_invoke_or_flags_18; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_or_flags_20: { + switch (llhttp__internal__c_or_flags_20(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_update_header_state_9; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_header_state_3: { + switch (llhttp__internal__c_load_header_state(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_header_value_connection; + case 2: + goto s_n_llhttp__internal__n_invoke_test_flags_2; + case 3: + goto s_n_llhttp__internal__n_invoke_test_flags_3; + case 4: + goto s_n_llhttp__internal__n_invoke_or_flags_20; + default: + goto s_n_llhttp__internal__n_header_value; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_22: { + switch (llhttp__internal__c_test_lenient_flags_22(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_error_60; + default: + goto s_n_llhttp__internal__n_header_value_discard_ws; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_flags_4: { + switch (llhttp__internal__c_test_flags_4(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_22; + default: + goto s_n_llhttp__internal__n_header_value_discard_ws; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_61: { + state->error = 0xf; + state->reason = "Transfer-Encoding can't be present with Content-Length"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_23: { + switch (llhttp__internal__c_test_lenient_flags_22(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_error_61; + default: + goto s_n_llhttp__internal__n_header_value_discard_ws; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_flags_5: { + switch (llhttp__internal__c_test_flags_2(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_23; + default: + goto s_n_llhttp__internal__n_header_value_discard_ws; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_19: { + state->error = 0x15; + state->reason = "on_header_field_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_load_header_state; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_45: { + state->error = 0x1c; + state->reason = "`on_header_field_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_header_field_1: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_header_field(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_header_field_complete; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_invoke_llhttp__on_header_field_complete; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_header_field_2: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_header_field(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_header_field_complete; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_invoke_llhttp__on_header_field_complete; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_62: { + state->error = 0xa; + state->reason = "Invalid header token"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_header_state_10: { + switch (llhttp__internal__c_update_header_state_1(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_header_field_general; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_store_header_state: { + switch (llhttp__internal__c_store_header_state(state, p, endp, match)) { + default: + goto s_n_llhttp__internal__n_header_field_colon; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_header_state_11: { + switch (llhttp__internal__c_update_header_state_1(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_header_field_general; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_4: { + state->error = 0x1e; + state->reason = "Unexpected space after start line"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags: { + switch (llhttp__internal__c_test_lenient_flags(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_header_field_start; + default: + goto s_n_llhttp__internal__n_error_4; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_20: { + state->error = 0x15; + state->reason = "on_url_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_headers_start; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_3: { + state->error = 0x1a; + state->reason = "`on_url_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_url_complete: { + switch (llhttp__on_url_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_headers_start; + case 21: + goto s_n_llhttp__internal__n_pause_20; + default: + goto s_n_llhttp__internal__n_error_3; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_http_minor: { + switch (llhttp__internal__c_update_http_minor(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_llhttp__on_url_complete; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_http_major: { + switch (llhttp__internal__c_update_http_major(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_invoke_update_http_minor; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_3: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_to_http09; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_to_http09; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_63: { + state->error = 0x7; + state->reason = "Expected CRLF"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_4: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_lf_to_http09; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_lf_to_http09; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_71: { + state->error = 0x17; + state->reason = "Pause on PRI/Upgrade"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_72: { + state->error = 0x9; + state->reason = "Expected HTTP/2 Connection Preface"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_69: { + state->error = 0x2; + state->reason = "Expected CRLF after version"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_26: { + switch (llhttp__internal__c_test_lenient_flags_8(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_headers_start; + default: + goto s_n_llhttp__internal__n_error_69; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_68: { + state->error = 0x9; + state->reason = "Expected CRLF after version"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_25: { + switch (llhttp__internal__c_test_lenient_flags_1(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_req_http_complete_crlf; + default: + goto s_n_llhttp__internal__n_error_68; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_70: { + state->error = 0x9; + state->reason = "Expected CRLF after version"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_21: { + state->error = 0x15; + state->reason = "on_version_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_load_method_1; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_67: { + state->error = 0x21; + state->reason = "`on_version_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_version_1: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_version(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_version_complete; + return s_error; + } + goto s_n_llhttp__internal__n_invoke_llhttp__on_version_complete; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_version: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_version(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_66; + return s_error; + } + goto s_n_llhttp__internal__n_error_66; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_http_minor: { + switch (llhttp__internal__c_load_http_minor(state, p, endp)) { + case 9: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_1; + default: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_http_minor_1: { + switch (llhttp__internal__c_load_http_minor(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_1; + case 1: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_1; + default: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_http_minor_2: { + switch (llhttp__internal__c_load_http_minor(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_1; + default: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_http_major: { + switch (llhttp__internal__c_load_http_major(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_load_http_minor; + case 1: + goto s_n_llhttp__internal__n_invoke_load_http_minor_1; + case 2: + goto s_n_llhttp__internal__n_invoke_load_http_minor_2; + default: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_24: { + switch (llhttp__internal__c_test_lenient_flags_24(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_1; + default: + goto s_n_llhttp__internal__n_invoke_load_http_major; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_store_http_minor: { + switch (llhttp__internal__c_store_http_minor(state, p, endp, match)) { + default: + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_24; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_version_2: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_version(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_73; + return s_error; + } + goto s_n_llhttp__internal__n_error_73; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_version_3: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_version(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_74; + return s_error; + } + goto s_n_llhttp__internal__n_error_74; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_store_http_major: { + switch (llhttp__internal__c_store_http_major(state, p, endp, match)) { + default: + goto s_n_llhttp__internal__n_req_http_dot; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_version_4: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_version(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_75; + return s_error; + } + goto s_n_llhttp__internal__n_error_75; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_65: { + state->error = 0x8; + state->reason = "Invalid method for HTTP/x.x request"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_method: { + switch (llhttp__internal__c_load_method(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 1: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 2: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 3: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 4: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 5: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 6: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 7: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 8: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 9: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 10: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 11: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 12: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 13: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 14: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 15: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 16: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 17: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 18: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 19: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 20: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 21: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 22: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 23: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 24: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 25: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 26: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 27: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 28: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 29: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 30: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 31: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 32: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 33: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 34: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 46: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + default: + goto s_n_llhttp__internal__n_error_65; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_78: { + state->error = 0x8; + state->reason = "Expected HTTP/"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_76: { + state->error = 0x8; + state->reason = "Expected SOURCE method for ICE/x.x request"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_method_2: { + switch (llhttp__internal__c_load_method(state, p, endp)) { + case 33: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + default: + goto s_n_llhttp__internal__n_error_76; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_77: { + state->error = 0x8; + state->reason = "Invalid method for RTSP/x.x request"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_method_3: { + switch (llhttp__internal__c_load_method(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 3: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 6: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 35: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 36: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 37: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 38: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 39: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 40: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 41: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 42: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 43: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 44: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + case 45: + goto s_n_llhttp__internal__n_span_start_llhttp__on_version; + default: + goto s_n_llhttp__internal__n_error_77; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_22: { + state->error = 0x15; + state->reason = "on_url_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_req_http_start; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_64: { + state->error = 0x1a; + state->reason = "`on_url_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_url_complete_1: { + switch (llhttp__on_url_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_req_http_start; + case 21: + goto s_n_llhttp__internal__n_pause_22; + default: + goto s_n_llhttp__internal__n_error_64; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_5: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_to_http; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_to_http; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_6: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_to_http09; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_to_http09; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_7: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_lf_to_http09; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_lf_to_http09; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_8: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_to_http; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_to_http; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_79: { + state->error = 0x7; + state->reason = "Invalid char in url fragment start"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_9: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_to_http09; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_to_http09; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_10: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_lf_to_http09; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_lf_to_http09; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_11: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_to_http; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_to_http; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_80: { + state->error = 0x7; + state->reason = "Invalid char in url query"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_81: { + state->error = 0x7; + state->reason = "Invalid char in url path"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_to_http09; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_to_http09; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_1: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_lf_to_http09; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_lf_to_http09; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_2: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_to_http; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_to_http; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_12: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_to_http09; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_to_http09; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_13: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_lf_to_http09; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_lf_to_http09; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_url_14: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_url(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_url_skip_to_http; + return s_error; + } + goto s_n_llhttp__internal__n_url_skip_to_http; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_82: { + state->error = 0x7; + state->reason = "Double @ in url"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_83: { + state->error = 0x7; + state->reason = "Unexpected char in url server"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_84: { + state->error = 0x7; + state->reason = "Unexpected char in url server"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_85: { + state->error = 0x7; + state->reason = "Unexpected char in url schema"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_86: { + state->error = 0x7; + state->reason = "Unexpected char in url schema"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_87: { + state->error = 0x7; + state->reason = "Unexpected start char in url"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_is_equal_method: { + switch (llhttp__internal__c_is_equal_method(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_url_entry_normal; + default: + goto s_n_llhttp__internal__n_url_entry_connect; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_88: { + state->error = 0x6; + state->reason = "Expected space after method"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_26: { + state->error = 0x15; + state->reason = "on_method_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_req_first_space_before_url; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_107: { + state->error = 0x20; + state->reason = "`on_method_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_method_2: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_method(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_method_complete_1; + return s_error; + } + goto s_n_llhttp__internal__n_invoke_llhttp__on_method_complete_1; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_store_method_1: { + switch (llhttp__internal__c_store_method(state, p, endp, match)) { + default: + goto s_n_llhttp__internal__n_span_end_llhttp__on_method_2; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_108: { + state->error = 0x6; + state->reason = "Invalid method encountered"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_100: { + state->error = 0xd; + state->reason = "Invalid status code"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_98: { + state->error = 0xd; + state->reason = "Invalid status code"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_96: { + state->error = 0xd; + state->reason = "Invalid status code"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_24: { + state->error = 0x15; + state->reason = "on_status_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_headers_start; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_92: { + state->error = 0x1b; + state->reason = "`on_status_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_status_complete: { + switch (llhttp__on_status_complete(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_headers_start; + case 21: + goto s_n_llhttp__internal__n_pause_24; + default: + goto s_n_llhttp__internal__n_error_92; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_91: { + state->error = 0xd; + state->reason = "Invalid response status"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_28: { + switch (llhttp__internal__c_test_lenient_flags_1(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_llhttp__on_status_complete; + default: + goto s_n_llhttp__internal__n_error_91; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_93: { + state->error = 0x2; + state->reason = "Expected LF after CR"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_29: { + switch (llhttp__internal__c_test_lenient_flags_8(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_llhttp__on_status_complete; + default: + goto s_n_llhttp__internal__n_error_93; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_94: { + state->error = 0x19; + state->reason = "Missing expected CR after response line"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_status: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_status(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_test_lenient_flags_30; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_30; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_status_1: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_status(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) (p + 1); + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_res_line_almost_done; + return s_error; + } + p++; + goto s_n_llhttp__internal__n_res_line_almost_done; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_95: { + state->error = 0xd; + state->reason = "Invalid response status"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_mul_add_status_code_2: { + switch (llhttp__internal__c_mul_add_status_code(state, p, endp, match)) { + case 1: + goto s_n_llhttp__internal__n_error_96; + default: + goto s_n_llhttp__internal__n_res_status_code_otherwise; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_97: { + state->error = 0xd; + state->reason = "Invalid status code"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_mul_add_status_code_1: { + switch (llhttp__internal__c_mul_add_status_code(state, p, endp, match)) { + case 1: + goto s_n_llhttp__internal__n_error_98; + default: + goto s_n_llhttp__internal__n_res_status_code_digit_3; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_99: { + state->error = 0xd; + state->reason = "Invalid status code"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_mul_add_status_code: { + switch (llhttp__internal__c_mul_add_status_code(state, p, endp, match)) { + case 1: + goto s_n_llhttp__internal__n_error_100; + default: + goto s_n_llhttp__internal__n_res_status_code_digit_2; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_101: { + state->error = 0xd; + state->reason = "Invalid status code"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_status_code: { + switch (llhttp__internal__c_update_status_code(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_res_status_code_digit_1; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_102: { + state->error = 0x9; + state->reason = "Expected space after version"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_25: { + state->error = 0x15; + state->reason = "on_version_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_res_after_version; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_90: { + state->error = 0x21; + state->reason = "`on_version_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_version_6: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_version(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_version_complete_1; + return s_error; + } + goto s_n_llhttp__internal__n_invoke_llhttp__on_version_complete_1; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_version_5: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_version(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_89; + return s_error; + } + goto s_n_llhttp__internal__n_error_89; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_http_minor_3: { + switch (llhttp__internal__c_load_http_minor(state, p, endp)) { + case 9: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_6; + default: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_5; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_http_minor_4: { + switch (llhttp__internal__c_load_http_minor(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_6; + case 1: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_6; + default: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_5; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_http_minor_5: { + switch (llhttp__internal__c_load_http_minor(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_6; + default: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_5; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_http_major_1: { + switch (llhttp__internal__c_load_http_major(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_load_http_minor_3; + case 1: + goto s_n_llhttp__internal__n_invoke_load_http_minor_4; + case 2: + goto s_n_llhttp__internal__n_invoke_load_http_minor_5; + default: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_5; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_test_lenient_flags_27: { + switch (llhttp__internal__c_test_lenient_flags_24(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_span_end_llhttp__on_version_6; + default: + goto s_n_llhttp__internal__n_invoke_load_http_major_1; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_store_http_minor_1: { + switch (llhttp__internal__c_store_http_minor(state, p, endp, match)) { + default: + goto s_n_llhttp__internal__n_invoke_test_lenient_flags_27; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_version_7: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_version(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_103; + return s_error; + } + goto s_n_llhttp__internal__n_error_103; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_version_8: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_version(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_104; + return s_error; + } + goto s_n_llhttp__internal__n_error_104; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_store_http_major_1: { + switch (llhttp__internal__c_store_http_major(state, p, endp, match)) { + default: + goto s_n_llhttp__internal__n_res_http_dot; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_version_9: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_version(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_error_105; + return s_error; + } + goto s_n_llhttp__internal__n_error_105; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_109: { + state->error = 0x8; + state->reason = "Expected HTTP/"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_23: { + state->error = 0x15; + state->reason = "on_method_complete pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_req_first_space_before_url; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_1: { + state->error = 0x20; + state->reason = "`on_method_complete` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_method: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_method(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_llhttp__on_method_complete; + return s_error; + } + goto s_n_llhttp__internal__n_invoke_llhttp__on_method_complete; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_type: { + switch (llhttp__internal__c_update_type(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_span_end_llhttp__on_method; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_store_method: { + switch (llhttp__internal__c_store_method(state, p, endp, match)) { + default: + goto s_n_llhttp__internal__n_invoke_update_type; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_106: { + state->error = 0x8; + state->reason = "Invalid word encountered"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_span_end_llhttp__on_method_1: { + const unsigned char* start; + int err; + + start = state->_span_pos0; + state->_span_pos0 = NULL; + err = llhttp__on_method(state, start, p); + if (err != 0) { + state->error = err; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_update_type_1; + return s_error; + } + goto s_n_llhttp__internal__n_invoke_update_type_1; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_update_type_2: { + switch (llhttp__internal__c_update_type(state, p, endp)) { + default: + goto s_n_llhttp__internal__n_span_start_llhttp__on_method_1; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_27: { + state->error = 0x15; + state->reason = "on_message_begin pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_load_type; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error: { + state->error = 0x10; + state->reason = "`on_message_begin` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_message_begin: { + switch (llhttp__on_message_begin(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_load_type; + case 21: + goto s_n_llhttp__internal__n_pause_27; + default: + goto s_n_llhttp__internal__n_error; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_pause_28: { + state->error = 0x15; + state->reason = "on_reset pause"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_n_llhttp__internal__n_invoke_update_finish; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_error_110: { + state->error = 0x1f; + state->reason = "`on_reset` callback error"; + state->error_pos = (const char*) p; + state->_current = (void*) (intptr_t) s_error; + return s_error; + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_llhttp__on_reset: { + switch (llhttp__on_reset(state, p, endp)) { + case 0: + goto s_n_llhttp__internal__n_invoke_update_finish; + case 21: + goto s_n_llhttp__internal__n_pause_28; + default: + goto s_n_llhttp__internal__n_error_110; + } + /* UNREACHABLE */; + abort(); + } + s_n_llhttp__internal__n_invoke_load_initial_message_completed: { + switch (llhttp__internal__c_load_initial_message_completed(state, p, endp)) { + case 1: + goto s_n_llhttp__internal__n_invoke_llhttp__on_reset; + default: + goto s_n_llhttp__internal__n_invoke_update_finish; + } + /* UNREACHABLE */; + abort(); + } +} + +int llhttp__internal_execute(llhttp__internal_t* state, const char* p, const char* endp) { + llparse_state_t next; + + /* check lingering errors */ + if (state->error != 0) { + return state->error; + } + + /* restart spans */ + if (state->_span_pos0 != NULL) { + state->_span_pos0 = (void*) p; + } + + next = llhttp__internal__run(state, (const unsigned char*) p, (const unsigned char*) endp); + if (next == s_error) { + return state->error; + } + state->_current = (void*) (intptr_t) next; + + /* execute spans */ + if (state->_span_pos0 != NULL) { + int error; + + error = ((llhttp__internal__span_cb) state->_span_cb0)(state, state->_span_pos0, (const char*) endp); + if (error != 0) { + state->error = error; + state->error_pos = endp; + return error; + } + } + + return 0; +} \ No newline at end of file diff --git a/deps/llhttp/llhttp.h b/deps/llhttp/llhttp.h new file mode 100644 index 00000000000..26f01c32cc7 --- /dev/null +++ b/deps/llhttp/llhttp.h @@ -0,0 +1,897 @@ + +#ifndef INCLUDE_LLHTTP_H_ +#define INCLUDE_LLHTTP_H_ + +#define LLHTTP_VERSION_MAJOR 9 +#define LLHTTP_VERSION_MINOR 2 +#define LLHTTP_VERSION_PATCH 1 + +#ifndef INCLUDE_LLHTTP_ITSELF_H_ +#define INCLUDE_LLHTTP_ITSELF_H_ +#ifdef __cplusplus +extern "C" { +#endif + +#include + +typedef struct llhttp__internal_s llhttp__internal_t; +struct llhttp__internal_s { + int32_t _index; + void* _span_pos0; + void* _span_cb0; + int32_t error; + const char* reason; + const char* error_pos; + void* data; + void* _current; + uint64_t content_length; + uint8_t type; + uint8_t method; + uint8_t http_major; + uint8_t http_minor; + uint8_t header_state; + uint16_t lenient_flags; + uint8_t upgrade; + uint8_t finish; + uint16_t flags; + uint16_t status_code; + uint8_t initial_message_completed; + void* settings; +}; + +int llhttp__internal_init(llhttp__internal_t* s); +int llhttp__internal_execute(llhttp__internal_t* s, const char* p, const char* endp); + +#ifdef __cplusplus +} /* extern "C" */ +#endif +#endif /* INCLUDE_LLHTTP_ITSELF_H_ */ + + +#ifndef LLLLHTTP_C_HEADERS_ +#define LLLLHTTP_C_HEADERS_ +#ifdef __cplusplus +extern "C" { +#endif + +enum llhttp_errno { + HPE_OK = 0, + HPE_INTERNAL = 1, + HPE_STRICT = 2, + HPE_CR_EXPECTED = 25, + HPE_LF_EXPECTED = 3, + HPE_UNEXPECTED_CONTENT_LENGTH = 4, + HPE_UNEXPECTED_SPACE = 30, + HPE_CLOSED_CONNECTION = 5, + HPE_INVALID_METHOD = 6, + HPE_INVALID_URL = 7, + HPE_INVALID_CONSTANT = 8, + HPE_INVALID_VERSION = 9, + HPE_INVALID_HEADER_TOKEN = 10, + HPE_INVALID_CONTENT_LENGTH = 11, + HPE_INVALID_CHUNK_SIZE = 12, + HPE_INVALID_STATUS = 13, + HPE_INVALID_EOF_STATE = 14, + HPE_INVALID_TRANSFER_ENCODING = 15, + HPE_CB_MESSAGE_BEGIN = 16, + HPE_CB_HEADERS_COMPLETE = 17, + HPE_CB_MESSAGE_COMPLETE = 18, + HPE_CB_CHUNK_HEADER = 19, + HPE_CB_CHUNK_COMPLETE = 20, + HPE_PAUSED = 21, + HPE_PAUSED_UPGRADE = 22, + HPE_PAUSED_H2_UPGRADE = 23, + HPE_USER = 24, + HPE_CB_URL_COMPLETE = 26, + HPE_CB_STATUS_COMPLETE = 27, + HPE_CB_METHOD_COMPLETE = 32, + HPE_CB_VERSION_COMPLETE = 33, + HPE_CB_HEADER_FIELD_COMPLETE = 28, + HPE_CB_HEADER_VALUE_COMPLETE = 29, + HPE_CB_CHUNK_EXTENSION_NAME_COMPLETE = 34, + HPE_CB_CHUNK_EXTENSION_VALUE_COMPLETE = 35, + HPE_CB_RESET = 31 +}; +typedef enum llhttp_errno llhttp_errno_t; + +enum llhttp_flags { + F_CONNECTION_KEEP_ALIVE = 0x1, + F_CONNECTION_CLOSE = 0x2, + F_CONNECTION_UPGRADE = 0x4, + F_CHUNKED = 0x8, + F_UPGRADE = 0x10, + F_CONTENT_LENGTH = 0x20, + F_SKIPBODY = 0x40, + F_TRAILING = 0x80, + F_TRANSFER_ENCODING = 0x200 +}; +typedef enum llhttp_flags llhttp_flags_t; + +enum llhttp_lenient_flags { + LENIENT_HEADERS = 0x1, + LENIENT_CHUNKED_LENGTH = 0x2, + LENIENT_KEEP_ALIVE = 0x4, + LENIENT_TRANSFER_ENCODING = 0x8, + LENIENT_VERSION = 0x10, + LENIENT_DATA_AFTER_CLOSE = 0x20, + LENIENT_OPTIONAL_LF_AFTER_CR = 0x40, + LENIENT_OPTIONAL_CRLF_AFTER_CHUNK = 0x80, + LENIENT_OPTIONAL_CR_BEFORE_LF = 0x100, + LENIENT_SPACES_AFTER_CHUNK_SIZE = 0x200 +}; +typedef enum llhttp_lenient_flags llhttp_lenient_flags_t; + +enum llhttp_type { + HTTP_BOTH = 0, + HTTP_REQUEST = 1, + HTTP_RESPONSE = 2 +}; +typedef enum llhttp_type llhttp_type_t; + +enum llhttp_finish { + HTTP_FINISH_SAFE = 0, + HTTP_FINISH_SAFE_WITH_CB = 1, + HTTP_FINISH_UNSAFE = 2 +}; +typedef enum llhttp_finish llhttp_finish_t; + +enum llhttp_method { + HTTP_DELETE = 0, + HTTP_GET = 1, + HTTP_HEAD = 2, + HTTP_POST = 3, + HTTP_PUT = 4, + HTTP_CONNECT = 5, + HTTP_OPTIONS = 6, + HTTP_TRACE = 7, + HTTP_COPY = 8, + HTTP_LOCK = 9, + HTTP_MKCOL = 10, + HTTP_MOVE = 11, + HTTP_PROPFIND = 12, + HTTP_PROPPATCH = 13, + HTTP_SEARCH = 14, + HTTP_UNLOCK = 15, + HTTP_BIND = 16, + HTTP_REBIND = 17, + HTTP_UNBIND = 18, + HTTP_ACL = 19, + HTTP_REPORT = 20, + HTTP_MKACTIVITY = 21, + HTTP_CHECKOUT = 22, + HTTP_MERGE = 23, + HTTP_MSEARCH = 24, + HTTP_NOTIFY = 25, + HTTP_SUBSCRIBE = 26, + HTTP_UNSUBSCRIBE = 27, + HTTP_PATCH = 28, + HTTP_PURGE = 29, + HTTP_MKCALENDAR = 30, + HTTP_LINK = 31, + HTTP_UNLINK = 32, + HTTP_SOURCE = 33, + HTTP_PRI = 34, + HTTP_DESCRIBE = 35, + HTTP_ANNOUNCE = 36, + HTTP_SETUP = 37, + HTTP_PLAY = 38, + HTTP_PAUSE = 39, + HTTP_TEARDOWN = 40, + HTTP_GET_PARAMETER = 41, + HTTP_SET_PARAMETER = 42, + HTTP_REDIRECT = 43, + HTTP_RECORD = 44, + HTTP_FLUSH = 45, + HTTP_QUERY = 46 +}; +typedef enum llhttp_method llhttp_method_t; + +enum llhttp_status { + HTTP_STATUS_CONTINUE = 100, + HTTP_STATUS_SWITCHING_PROTOCOLS = 101, + HTTP_STATUS_PROCESSING = 102, + HTTP_STATUS_EARLY_HINTS = 103, + HTTP_STATUS_RESPONSE_IS_STALE = 110, + HTTP_STATUS_REVALIDATION_FAILED = 111, + HTTP_STATUS_DISCONNECTED_OPERATION = 112, + HTTP_STATUS_HEURISTIC_EXPIRATION = 113, + HTTP_STATUS_MISCELLANEOUS_WARNING = 199, + HTTP_STATUS_OK = 200, + HTTP_STATUS_CREATED = 201, + HTTP_STATUS_ACCEPTED = 202, + HTTP_STATUS_NON_AUTHORITATIVE_INFORMATION = 203, + HTTP_STATUS_NO_CONTENT = 204, + HTTP_STATUS_RESET_CONTENT = 205, + HTTP_STATUS_PARTIAL_CONTENT = 206, + HTTP_STATUS_MULTI_STATUS = 207, + HTTP_STATUS_ALREADY_REPORTED = 208, + HTTP_STATUS_TRANSFORMATION_APPLIED = 214, + HTTP_STATUS_IM_USED = 226, + HTTP_STATUS_MISCELLANEOUS_PERSISTENT_WARNING = 299, + HTTP_STATUS_MULTIPLE_CHOICES = 300, + HTTP_STATUS_MOVED_PERMANENTLY = 301, + HTTP_STATUS_FOUND = 302, + HTTP_STATUS_SEE_OTHER = 303, + HTTP_STATUS_NOT_MODIFIED = 304, + HTTP_STATUS_USE_PROXY = 305, + HTTP_STATUS_SWITCH_PROXY = 306, + HTTP_STATUS_TEMPORARY_REDIRECT = 307, + HTTP_STATUS_PERMANENT_REDIRECT = 308, + HTTP_STATUS_BAD_REQUEST = 400, + HTTP_STATUS_UNAUTHORIZED = 401, + HTTP_STATUS_PAYMENT_REQUIRED = 402, + HTTP_STATUS_FORBIDDEN = 403, + HTTP_STATUS_NOT_FOUND = 404, + HTTP_STATUS_METHOD_NOT_ALLOWED = 405, + HTTP_STATUS_NOT_ACCEPTABLE = 406, + HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED = 407, + HTTP_STATUS_REQUEST_TIMEOUT = 408, + HTTP_STATUS_CONFLICT = 409, + HTTP_STATUS_GONE = 410, + HTTP_STATUS_LENGTH_REQUIRED = 411, + HTTP_STATUS_PRECONDITION_FAILED = 412, + HTTP_STATUS_PAYLOAD_TOO_LARGE = 413, + HTTP_STATUS_URI_TOO_LONG = 414, + HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE = 415, + HTTP_STATUS_RANGE_NOT_SATISFIABLE = 416, + HTTP_STATUS_EXPECTATION_FAILED = 417, + HTTP_STATUS_IM_A_TEAPOT = 418, + HTTP_STATUS_PAGE_EXPIRED = 419, + HTTP_STATUS_ENHANCE_YOUR_CALM = 420, + HTTP_STATUS_MISDIRECTED_REQUEST = 421, + HTTP_STATUS_UNPROCESSABLE_ENTITY = 422, + HTTP_STATUS_LOCKED = 423, + HTTP_STATUS_FAILED_DEPENDENCY = 424, + HTTP_STATUS_TOO_EARLY = 425, + HTTP_STATUS_UPGRADE_REQUIRED = 426, + HTTP_STATUS_PRECONDITION_REQUIRED = 428, + HTTP_STATUS_TOO_MANY_REQUESTS = 429, + HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE_UNOFFICIAL = 430, + HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE = 431, + HTTP_STATUS_LOGIN_TIMEOUT = 440, + HTTP_STATUS_NO_RESPONSE = 444, + HTTP_STATUS_RETRY_WITH = 449, + HTTP_STATUS_BLOCKED_BY_PARENTAL_CONTROL = 450, + HTTP_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS = 451, + HTTP_STATUS_CLIENT_CLOSED_LOAD_BALANCED_REQUEST = 460, + HTTP_STATUS_INVALID_X_FORWARDED_FOR = 463, + HTTP_STATUS_REQUEST_HEADER_TOO_LARGE = 494, + HTTP_STATUS_SSL_CERTIFICATE_ERROR = 495, + HTTP_STATUS_SSL_CERTIFICATE_REQUIRED = 496, + HTTP_STATUS_HTTP_REQUEST_SENT_TO_HTTPS_PORT = 497, + HTTP_STATUS_INVALID_TOKEN = 498, + HTTP_STATUS_CLIENT_CLOSED_REQUEST = 499, + HTTP_STATUS_INTERNAL_SERVER_ERROR = 500, + HTTP_STATUS_NOT_IMPLEMENTED = 501, + HTTP_STATUS_BAD_GATEWAY = 502, + HTTP_STATUS_SERVICE_UNAVAILABLE = 503, + HTTP_STATUS_GATEWAY_TIMEOUT = 504, + HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED = 505, + HTTP_STATUS_VARIANT_ALSO_NEGOTIATES = 506, + HTTP_STATUS_INSUFFICIENT_STORAGE = 507, + HTTP_STATUS_LOOP_DETECTED = 508, + HTTP_STATUS_BANDWIDTH_LIMIT_EXCEEDED = 509, + HTTP_STATUS_NOT_EXTENDED = 510, + HTTP_STATUS_NETWORK_AUTHENTICATION_REQUIRED = 511, + HTTP_STATUS_WEB_SERVER_UNKNOWN_ERROR = 520, + HTTP_STATUS_WEB_SERVER_IS_DOWN = 521, + HTTP_STATUS_CONNECTION_TIMEOUT = 522, + HTTP_STATUS_ORIGIN_IS_UNREACHABLE = 523, + HTTP_STATUS_TIMEOUT_OCCURED = 524, + HTTP_STATUS_SSL_HANDSHAKE_FAILED = 525, + HTTP_STATUS_INVALID_SSL_CERTIFICATE = 526, + HTTP_STATUS_RAILGUN_ERROR = 527, + HTTP_STATUS_SITE_IS_OVERLOADED = 529, + HTTP_STATUS_SITE_IS_FROZEN = 530, + HTTP_STATUS_IDENTITY_PROVIDER_AUTHENTICATION_ERROR = 561, + HTTP_STATUS_NETWORK_READ_TIMEOUT = 598, + HTTP_STATUS_NETWORK_CONNECT_TIMEOUT = 599 +}; +typedef enum llhttp_status llhttp_status_t; + +#define HTTP_ERRNO_MAP(XX) \ + XX(0, OK, OK) \ + XX(1, INTERNAL, INTERNAL) \ + XX(2, STRICT, STRICT) \ + XX(25, CR_EXPECTED, CR_EXPECTED) \ + XX(3, LF_EXPECTED, LF_EXPECTED) \ + XX(4, UNEXPECTED_CONTENT_LENGTH, UNEXPECTED_CONTENT_LENGTH) \ + XX(30, UNEXPECTED_SPACE, UNEXPECTED_SPACE) \ + XX(5, CLOSED_CONNECTION, CLOSED_CONNECTION) \ + XX(6, INVALID_METHOD, INVALID_METHOD) \ + XX(7, INVALID_URL, INVALID_URL) \ + XX(8, INVALID_CONSTANT, INVALID_CONSTANT) \ + XX(9, INVALID_VERSION, INVALID_VERSION) \ + XX(10, INVALID_HEADER_TOKEN, INVALID_HEADER_TOKEN) \ + XX(11, INVALID_CONTENT_LENGTH, INVALID_CONTENT_LENGTH) \ + XX(12, INVALID_CHUNK_SIZE, INVALID_CHUNK_SIZE) \ + XX(13, INVALID_STATUS, INVALID_STATUS) \ + XX(14, INVALID_EOF_STATE, INVALID_EOF_STATE) \ + XX(15, INVALID_TRANSFER_ENCODING, INVALID_TRANSFER_ENCODING) \ + XX(16, CB_MESSAGE_BEGIN, CB_MESSAGE_BEGIN) \ + XX(17, CB_HEADERS_COMPLETE, CB_HEADERS_COMPLETE) \ + XX(18, CB_MESSAGE_COMPLETE, CB_MESSAGE_COMPLETE) \ + XX(19, CB_CHUNK_HEADER, CB_CHUNK_HEADER) \ + XX(20, CB_CHUNK_COMPLETE, CB_CHUNK_COMPLETE) \ + XX(21, PAUSED, PAUSED) \ + XX(22, PAUSED_UPGRADE, PAUSED_UPGRADE) \ + XX(23, PAUSED_H2_UPGRADE, PAUSED_H2_UPGRADE) \ + XX(24, USER, USER) \ + XX(26, CB_URL_COMPLETE, CB_URL_COMPLETE) \ + XX(27, CB_STATUS_COMPLETE, CB_STATUS_COMPLETE) \ + XX(32, CB_METHOD_COMPLETE, CB_METHOD_COMPLETE) \ + XX(33, CB_VERSION_COMPLETE, CB_VERSION_COMPLETE) \ + XX(28, CB_HEADER_FIELD_COMPLETE, CB_HEADER_FIELD_COMPLETE) \ + XX(29, CB_HEADER_VALUE_COMPLETE, CB_HEADER_VALUE_COMPLETE) \ + XX(34, CB_CHUNK_EXTENSION_NAME_COMPLETE, CB_CHUNK_EXTENSION_NAME_COMPLETE) \ + XX(35, CB_CHUNK_EXTENSION_VALUE_COMPLETE, CB_CHUNK_EXTENSION_VALUE_COMPLETE) \ + XX(31, CB_RESET, CB_RESET) \ + + +#define HTTP_METHOD_MAP(XX) \ + XX(0, DELETE, DELETE) \ + XX(1, GET, GET) \ + XX(2, HEAD, HEAD) \ + XX(3, POST, POST) \ + XX(4, PUT, PUT) \ + XX(5, CONNECT, CONNECT) \ + XX(6, OPTIONS, OPTIONS) \ + XX(7, TRACE, TRACE) \ + XX(8, COPY, COPY) \ + XX(9, LOCK, LOCK) \ + XX(10, MKCOL, MKCOL) \ + XX(11, MOVE, MOVE) \ + XX(12, PROPFIND, PROPFIND) \ + XX(13, PROPPATCH, PROPPATCH) \ + XX(14, SEARCH, SEARCH) \ + XX(15, UNLOCK, UNLOCK) \ + XX(16, BIND, BIND) \ + XX(17, REBIND, REBIND) \ + XX(18, UNBIND, UNBIND) \ + XX(19, ACL, ACL) \ + XX(20, REPORT, REPORT) \ + XX(21, MKACTIVITY, MKACTIVITY) \ + XX(22, CHECKOUT, CHECKOUT) \ + XX(23, MERGE, MERGE) \ + XX(24, MSEARCH, M-SEARCH) \ + XX(25, NOTIFY, NOTIFY) \ + XX(26, SUBSCRIBE, SUBSCRIBE) \ + XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \ + XX(28, PATCH, PATCH) \ + XX(29, PURGE, PURGE) \ + XX(30, MKCALENDAR, MKCALENDAR) \ + XX(31, LINK, LINK) \ + XX(32, UNLINK, UNLINK) \ + XX(33, SOURCE, SOURCE) \ + XX(46, QUERY, QUERY) \ + + +#define RTSP_METHOD_MAP(XX) \ + XX(1, GET, GET) \ + XX(3, POST, POST) \ + XX(6, OPTIONS, OPTIONS) \ + XX(35, DESCRIBE, DESCRIBE) \ + XX(36, ANNOUNCE, ANNOUNCE) \ + XX(37, SETUP, SETUP) \ + XX(38, PLAY, PLAY) \ + XX(39, PAUSE, PAUSE) \ + XX(40, TEARDOWN, TEARDOWN) \ + XX(41, GET_PARAMETER, GET_PARAMETER) \ + XX(42, SET_PARAMETER, SET_PARAMETER) \ + XX(43, REDIRECT, REDIRECT) \ + XX(44, RECORD, RECORD) \ + XX(45, FLUSH, FLUSH) \ + + +#define HTTP_ALL_METHOD_MAP(XX) \ + XX(0, DELETE, DELETE) \ + XX(1, GET, GET) \ + XX(2, HEAD, HEAD) \ + XX(3, POST, POST) \ + XX(4, PUT, PUT) \ + XX(5, CONNECT, CONNECT) \ + XX(6, OPTIONS, OPTIONS) \ + XX(7, TRACE, TRACE) \ + XX(8, COPY, COPY) \ + XX(9, LOCK, LOCK) \ + XX(10, MKCOL, MKCOL) \ + XX(11, MOVE, MOVE) \ + XX(12, PROPFIND, PROPFIND) \ + XX(13, PROPPATCH, PROPPATCH) \ + XX(14, SEARCH, SEARCH) \ + XX(15, UNLOCK, UNLOCK) \ + XX(16, BIND, BIND) \ + XX(17, REBIND, REBIND) \ + XX(18, UNBIND, UNBIND) \ + XX(19, ACL, ACL) \ + XX(20, REPORT, REPORT) \ + XX(21, MKACTIVITY, MKACTIVITY) \ + XX(22, CHECKOUT, CHECKOUT) \ + XX(23, MERGE, MERGE) \ + XX(24, MSEARCH, M-SEARCH) \ + XX(25, NOTIFY, NOTIFY) \ + XX(26, SUBSCRIBE, SUBSCRIBE) \ + XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \ + XX(28, PATCH, PATCH) \ + XX(29, PURGE, PURGE) \ + XX(30, MKCALENDAR, MKCALENDAR) \ + XX(31, LINK, LINK) \ + XX(32, UNLINK, UNLINK) \ + XX(33, SOURCE, SOURCE) \ + XX(34, PRI, PRI) \ + XX(35, DESCRIBE, DESCRIBE) \ + XX(36, ANNOUNCE, ANNOUNCE) \ + XX(37, SETUP, SETUP) \ + XX(38, PLAY, PLAY) \ + XX(39, PAUSE, PAUSE) \ + XX(40, TEARDOWN, TEARDOWN) \ + XX(41, GET_PARAMETER, GET_PARAMETER) \ + XX(42, SET_PARAMETER, SET_PARAMETER) \ + XX(43, REDIRECT, REDIRECT) \ + XX(44, RECORD, RECORD) \ + XX(45, FLUSH, FLUSH) \ + XX(46, QUERY, QUERY) \ + + +#define HTTP_STATUS_MAP(XX) \ + XX(100, CONTINUE, CONTINUE) \ + XX(101, SWITCHING_PROTOCOLS, SWITCHING_PROTOCOLS) \ + XX(102, PROCESSING, PROCESSING) \ + XX(103, EARLY_HINTS, EARLY_HINTS) \ + XX(110, RESPONSE_IS_STALE, RESPONSE_IS_STALE) \ + XX(111, REVALIDATION_FAILED, REVALIDATION_FAILED) \ + XX(112, DISCONNECTED_OPERATION, DISCONNECTED_OPERATION) \ + XX(113, HEURISTIC_EXPIRATION, HEURISTIC_EXPIRATION) \ + XX(199, MISCELLANEOUS_WARNING, MISCELLANEOUS_WARNING) \ + XX(200, OK, OK) \ + XX(201, CREATED, CREATED) \ + XX(202, ACCEPTED, ACCEPTED) \ + XX(203, NON_AUTHORITATIVE_INFORMATION, NON_AUTHORITATIVE_INFORMATION) \ + XX(204, NO_CONTENT, NO_CONTENT) \ + XX(205, RESET_CONTENT, RESET_CONTENT) \ + XX(206, PARTIAL_CONTENT, PARTIAL_CONTENT) \ + XX(207, MULTI_STATUS, MULTI_STATUS) \ + XX(208, ALREADY_REPORTED, ALREADY_REPORTED) \ + XX(214, TRANSFORMATION_APPLIED, TRANSFORMATION_APPLIED) \ + XX(226, IM_USED, IM_USED) \ + XX(299, MISCELLANEOUS_PERSISTENT_WARNING, MISCELLANEOUS_PERSISTENT_WARNING) \ + XX(300, MULTIPLE_CHOICES, MULTIPLE_CHOICES) \ + XX(301, MOVED_PERMANENTLY, MOVED_PERMANENTLY) \ + XX(302, FOUND, FOUND) \ + XX(303, SEE_OTHER, SEE_OTHER) \ + XX(304, NOT_MODIFIED, NOT_MODIFIED) \ + XX(305, USE_PROXY, USE_PROXY) \ + XX(306, SWITCH_PROXY, SWITCH_PROXY) \ + XX(307, TEMPORARY_REDIRECT, TEMPORARY_REDIRECT) \ + XX(308, PERMANENT_REDIRECT, PERMANENT_REDIRECT) \ + XX(400, BAD_REQUEST, BAD_REQUEST) \ + XX(401, UNAUTHORIZED, UNAUTHORIZED) \ + XX(402, PAYMENT_REQUIRED, PAYMENT_REQUIRED) \ + XX(403, FORBIDDEN, FORBIDDEN) \ + XX(404, NOT_FOUND, NOT_FOUND) \ + XX(405, METHOD_NOT_ALLOWED, METHOD_NOT_ALLOWED) \ + XX(406, NOT_ACCEPTABLE, NOT_ACCEPTABLE) \ + XX(407, PROXY_AUTHENTICATION_REQUIRED, PROXY_AUTHENTICATION_REQUIRED) \ + XX(408, REQUEST_TIMEOUT, REQUEST_TIMEOUT) \ + XX(409, CONFLICT, CONFLICT) \ + XX(410, GONE, GONE) \ + XX(411, LENGTH_REQUIRED, LENGTH_REQUIRED) \ + XX(412, PRECONDITION_FAILED, PRECONDITION_FAILED) \ + XX(413, PAYLOAD_TOO_LARGE, PAYLOAD_TOO_LARGE) \ + XX(414, URI_TOO_LONG, URI_TOO_LONG) \ + XX(415, UNSUPPORTED_MEDIA_TYPE, UNSUPPORTED_MEDIA_TYPE) \ + XX(416, RANGE_NOT_SATISFIABLE, RANGE_NOT_SATISFIABLE) \ + XX(417, EXPECTATION_FAILED, EXPECTATION_FAILED) \ + XX(418, IM_A_TEAPOT, IM_A_TEAPOT) \ + XX(419, PAGE_EXPIRED, PAGE_EXPIRED) \ + XX(420, ENHANCE_YOUR_CALM, ENHANCE_YOUR_CALM) \ + XX(421, MISDIRECTED_REQUEST, MISDIRECTED_REQUEST) \ + XX(422, UNPROCESSABLE_ENTITY, UNPROCESSABLE_ENTITY) \ + XX(423, LOCKED, LOCKED) \ + XX(424, FAILED_DEPENDENCY, FAILED_DEPENDENCY) \ + XX(425, TOO_EARLY, TOO_EARLY) \ + XX(426, UPGRADE_REQUIRED, UPGRADE_REQUIRED) \ + XX(428, PRECONDITION_REQUIRED, PRECONDITION_REQUIRED) \ + XX(429, TOO_MANY_REQUESTS, TOO_MANY_REQUESTS) \ + XX(430, REQUEST_HEADER_FIELDS_TOO_LARGE_UNOFFICIAL, REQUEST_HEADER_FIELDS_TOO_LARGE_UNOFFICIAL) \ + XX(431, REQUEST_HEADER_FIELDS_TOO_LARGE, REQUEST_HEADER_FIELDS_TOO_LARGE) \ + XX(440, LOGIN_TIMEOUT, LOGIN_TIMEOUT) \ + XX(444, NO_RESPONSE, NO_RESPONSE) \ + XX(449, RETRY_WITH, RETRY_WITH) \ + XX(450, BLOCKED_BY_PARENTAL_CONTROL, BLOCKED_BY_PARENTAL_CONTROL) \ + XX(451, UNAVAILABLE_FOR_LEGAL_REASONS, UNAVAILABLE_FOR_LEGAL_REASONS) \ + XX(460, CLIENT_CLOSED_LOAD_BALANCED_REQUEST, CLIENT_CLOSED_LOAD_BALANCED_REQUEST) \ + XX(463, INVALID_X_FORWARDED_FOR, INVALID_X_FORWARDED_FOR) \ + XX(494, REQUEST_HEADER_TOO_LARGE, REQUEST_HEADER_TOO_LARGE) \ + XX(495, SSL_CERTIFICATE_ERROR, SSL_CERTIFICATE_ERROR) \ + XX(496, SSL_CERTIFICATE_REQUIRED, SSL_CERTIFICATE_REQUIRED) \ + XX(497, HTTP_REQUEST_SENT_TO_HTTPS_PORT, HTTP_REQUEST_SENT_TO_HTTPS_PORT) \ + XX(498, INVALID_TOKEN, INVALID_TOKEN) \ + XX(499, CLIENT_CLOSED_REQUEST, CLIENT_CLOSED_REQUEST) \ + XX(500, INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR) \ + XX(501, NOT_IMPLEMENTED, NOT_IMPLEMENTED) \ + XX(502, BAD_GATEWAY, BAD_GATEWAY) \ + XX(503, SERVICE_UNAVAILABLE, SERVICE_UNAVAILABLE) \ + XX(504, GATEWAY_TIMEOUT, GATEWAY_TIMEOUT) \ + XX(505, HTTP_VERSION_NOT_SUPPORTED, HTTP_VERSION_NOT_SUPPORTED) \ + XX(506, VARIANT_ALSO_NEGOTIATES, VARIANT_ALSO_NEGOTIATES) \ + XX(507, INSUFFICIENT_STORAGE, INSUFFICIENT_STORAGE) \ + XX(508, LOOP_DETECTED, LOOP_DETECTED) \ + XX(509, BANDWIDTH_LIMIT_EXCEEDED, BANDWIDTH_LIMIT_EXCEEDED) \ + XX(510, NOT_EXTENDED, NOT_EXTENDED) \ + XX(511, NETWORK_AUTHENTICATION_REQUIRED, NETWORK_AUTHENTICATION_REQUIRED) \ + XX(520, WEB_SERVER_UNKNOWN_ERROR, WEB_SERVER_UNKNOWN_ERROR) \ + XX(521, WEB_SERVER_IS_DOWN, WEB_SERVER_IS_DOWN) \ + XX(522, CONNECTION_TIMEOUT, CONNECTION_TIMEOUT) \ + XX(523, ORIGIN_IS_UNREACHABLE, ORIGIN_IS_UNREACHABLE) \ + XX(524, TIMEOUT_OCCURED, TIMEOUT_OCCURED) \ + XX(525, SSL_HANDSHAKE_FAILED, SSL_HANDSHAKE_FAILED) \ + XX(526, INVALID_SSL_CERTIFICATE, INVALID_SSL_CERTIFICATE) \ + XX(527, RAILGUN_ERROR, RAILGUN_ERROR) \ + XX(529, SITE_IS_OVERLOADED, SITE_IS_OVERLOADED) \ + XX(530, SITE_IS_FROZEN, SITE_IS_FROZEN) \ + XX(561, IDENTITY_PROVIDER_AUTHENTICATION_ERROR, IDENTITY_PROVIDER_AUTHENTICATION_ERROR) \ + XX(598, NETWORK_READ_TIMEOUT, NETWORK_READ_TIMEOUT) \ + XX(599, NETWORK_CONNECT_TIMEOUT, NETWORK_CONNECT_TIMEOUT) \ + + +#ifdef __cplusplus +} /* extern "C" */ +#endif +#endif /* LLLLHTTP_C_HEADERS_ */ + + +#ifndef INCLUDE_LLHTTP_API_H_ +#define INCLUDE_LLHTTP_API_H_ +#ifdef __cplusplus +extern "C" { +#endif +#include + +#define LLHTTP_EXPORT + +typedef llhttp__internal_t llhttp_t; +typedef struct llhttp_settings_s llhttp_settings_t; + +typedef int (*llhttp_data_cb)(llhttp_t*, const char *at, size_t length); +typedef int (*llhttp_cb)(llhttp_t*); + +struct llhttp_settings_s { + /* Possible return values 0, -1, `HPE_PAUSED` */ + llhttp_cb on_message_begin; + + /* Possible return values 0, -1, HPE_USER */ + llhttp_data_cb on_url; + llhttp_data_cb on_status; + llhttp_data_cb on_method; + llhttp_data_cb on_version; + llhttp_data_cb on_header_field; + llhttp_data_cb on_header_value; + llhttp_data_cb on_chunk_extension_name; + llhttp_data_cb on_chunk_extension_value; + + /* Possible return values: + * 0 - Proceed normally + * 1 - Assume that request/response has no body, and proceed to parsing the + * next message + * 2 - Assume absence of body (as above) and make `llhttp_execute()` return + * `HPE_PAUSED_UPGRADE` + * -1 - Error + * `HPE_PAUSED` + */ + llhttp_cb on_headers_complete; + + /* Possible return values 0, -1, HPE_USER */ + llhttp_data_cb on_body; + + /* Possible return values 0, -1, `HPE_PAUSED` */ + llhttp_cb on_message_complete; + llhttp_cb on_url_complete; + llhttp_cb on_status_complete; + llhttp_cb on_method_complete; + llhttp_cb on_version_complete; + llhttp_cb on_header_field_complete; + llhttp_cb on_header_value_complete; + llhttp_cb on_chunk_extension_name_complete; + llhttp_cb on_chunk_extension_value_complete; + + /* When on_chunk_header is called, the current chunk length is stored + * in parser->content_length. + * Possible return values 0, -1, `HPE_PAUSED` + */ + llhttp_cb on_chunk_header; + llhttp_cb on_chunk_complete; + llhttp_cb on_reset; +}; + +/* Initialize the parser with specific type and user settings. + * + * NOTE: lifetime of `settings` has to be at least the same as the lifetime of + * the `parser` here. In practice, `settings` has to be either a static + * variable or be allocated with `malloc`, `new`, etc. + */ +LLHTTP_EXPORT +void llhttp_init(llhttp_t* parser, llhttp_type_t type, + const llhttp_settings_t* settings); + +LLHTTP_EXPORT +llhttp_t* llhttp_alloc(llhttp_type_t type); + +LLHTTP_EXPORT +void llhttp_free(llhttp_t* parser); + +LLHTTP_EXPORT +uint8_t llhttp_get_type(llhttp_t* parser); + +LLHTTP_EXPORT +uint8_t llhttp_get_http_major(llhttp_t* parser); + +LLHTTP_EXPORT +uint8_t llhttp_get_http_minor(llhttp_t* parser); + +LLHTTP_EXPORT +uint8_t llhttp_get_method(llhttp_t* parser); + +LLHTTP_EXPORT +int llhttp_get_status_code(llhttp_t* parser); + +LLHTTP_EXPORT +uint8_t llhttp_get_upgrade(llhttp_t* parser); + +/* Reset an already initialized parser back to the start state, preserving the + * existing parser type, callback settings, user data, and lenient flags. + */ +LLHTTP_EXPORT +void llhttp_reset(llhttp_t* parser); + +/* Initialize the settings object */ +LLHTTP_EXPORT +void llhttp_settings_init(llhttp_settings_t* settings); + +/* Parse full or partial request/response, invoking user callbacks along the + * way. + * + * If any of `llhttp_data_cb` returns errno not equal to `HPE_OK` - the parsing + * interrupts, and such errno is returned from `llhttp_execute()`. If + * `HPE_PAUSED` was used as a errno, the execution can be resumed with + * `llhttp_resume()` call. + * + * In a special case of CONNECT/Upgrade request/response `HPE_PAUSED_UPGRADE` + * is returned after fully parsing the request/response. If the user wishes to + * continue parsing, they need to invoke `llhttp_resume_after_upgrade()`. + * + * NOTE: if this function ever returns a non-pause type error, it will continue + * to return the same error upon each successive call up until `llhttp_init()` + * is called. + */ +LLHTTP_EXPORT +llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len); + +/* This method should be called when the other side has no further bytes to + * send (e.g. shutdown of readable side of the TCP connection.) + * + * Requests without `Content-Length` and other messages might require treating + * all incoming bytes as the part of the body, up to the last byte of the + * connection. This method will invoke `on_message_complete()` callback if the + * request was terminated safely. Otherwise a error code would be returned. + */ +LLHTTP_EXPORT +llhttp_errno_t llhttp_finish(llhttp_t* parser); + +/* Returns `1` if the incoming message is parsed until the last byte, and has + * to be completed by calling `llhttp_finish()` on EOF + */ +LLHTTP_EXPORT +int llhttp_message_needs_eof(const llhttp_t* parser); + +/* Returns `1` if there might be any other messages following the last that was + * successfully parsed. + */ +LLHTTP_EXPORT +int llhttp_should_keep_alive(const llhttp_t* parser); + +/* Make further calls of `llhttp_execute()` return `HPE_PAUSED` and set + * appropriate error reason. + * + * Important: do not call this from user callbacks! User callbacks must return + * `HPE_PAUSED` if pausing is required. + */ +LLHTTP_EXPORT +void llhttp_pause(llhttp_t* parser); + +/* Might be called to resume the execution after the pause in user's callback. + * See `llhttp_execute()` above for details. + * + * Call this only if `llhttp_execute()` returns `HPE_PAUSED`. + */ +LLHTTP_EXPORT +void llhttp_resume(llhttp_t* parser); + +/* Might be called to resume the execution after the pause in user's callback. + * See `llhttp_execute()` above for details. + * + * Call this only if `llhttp_execute()` returns `HPE_PAUSED_UPGRADE` + */ +LLHTTP_EXPORT +void llhttp_resume_after_upgrade(llhttp_t* parser); + +/* Returns the latest return error */ +LLHTTP_EXPORT +llhttp_errno_t llhttp_get_errno(const llhttp_t* parser); + +/* Returns the verbal explanation of the latest returned error. + * + * Note: User callback should set error reason when returning the error. See + * `llhttp_set_error_reason()` for details. + */ +LLHTTP_EXPORT +const char* llhttp_get_error_reason(const llhttp_t* parser); + +/* Assign verbal description to the returned error. Must be called in user + * callbacks right before returning the errno. + * + * Note: `HPE_USER` error code might be useful in user callbacks. + */ +LLHTTP_EXPORT +void llhttp_set_error_reason(llhttp_t* parser, const char* reason); + +/* Returns the pointer to the last parsed byte before the returned error. The + * pointer is relative to the `data` argument of `llhttp_execute()`. + * + * Note: this method might be useful for counting the number of parsed bytes. + */ +LLHTTP_EXPORT +const char* llhttp_get_error_pos(const llhttp_t* parser); + +/* Returns textual name of error code */ +LLHTTP_EXPORT +const char* llhttp_errno_name(llhttp_errno_t err); + +/* Returns textual name of HTTP method */ +LLHTTP_EXPORT +const char* llhttp_method_name(llhttp_method_t method); + +/* Returns textual name of HTTP status */ +LLHTTP_EXPORT +const char* llhttp_status_name(llhttp_status_t status); + +/* Enables/disables lenient header value parsing (disabled by default). + * + * Lenient parsing disables header value token checks, extending llhttp's + * protocol support to highly non-compliant clients/server. No + * `HPE_INVALID_HEADER_TOKEN` will be raised for incorrect header values when + * lenient parsing is "on". + * + * **Enabling this flag can pose a security issue since you will be exposed to + * request smuggling attacks. USE WITH CAUTION!** + */ +LLHTTP_EXPORT +void llhttp_set_lenient_headers(llhttp_t* parser, int enabled); + + +/* Enables/disables lenient handling of conflicting `Transfer-Encoding` and + * `Content-Length` headers (disabled by default). + * + * Normally `llhttp` would error when `Transfer-Encoding` is present in + * conjunction with `Content-Length`. This error is important to prevent HTTP + * request smuggling, but may be less desirable for small number of cases + * involving legacy servers. + * + * **Enabling this flag can pose a security issue since you will be exposed to + * request smuggling attacks. USE WITH CAUTION!** + */ +LLHTTP_EXPORT +void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled); + + +/* Enables/disables lenient handling of `Connection: close` and HTTP/1.0 + * requests responses. + * + * Normally `llhttp` would error on (in strict mode) or discard (in loose mode) + * the HTTP request/response after the request/response with `Connection: close` + * and `Content-Length`. This is important to prevent cache poisoning attacks, + * but might interact badly with outdated and insecure clients. With this flag + * the extra request/response will be parsed normally. + * + * **Enabling this flag can pose a security issue since you will be exposed to + * poisoning attacks. USE WITH CAUTION!** + */ +LLHTTP_EXPORT +void llhttp_set_lenient_keep_alive(llhttp_t* parser, int enabled); + +/* Enables/disables lenient handling of `Transfer-Encoding` header. + * + * Normally `llhttp` would error when a `Transfer-Encoding` has `chunked` value + * and another value after it (either in a single header or in multiple + * headers whose value are internally joined using `, `). + * This is mandated by the spec to reliably determine request body size and thus + * avoid request smuggling. + * With this flag the extra value will be parsed normally. + * + * **Enabling this flag can pose a security issue since you will be exposed to + * request smuggling attacks. USE WITH CAUTION!** + */ +LLHTTP_EXPORT +void llhttp_set_lenient_transfer_encoding(llhttp_t* parser, int enabled); + +/* Enables/disables lenient handling of HTTP version. + * + * Normally `llhttp` would error when the HTTP version in the request or status line + * is not `0.9`, `1.0`, `1.1` or `2.0`. + * With this flag the invalid value will be parsed normally. + * + * **Enabling this flag can pose a security issue since you will allow unsupported + * HTTP versions. USE WITH CAUTION!** + */ +LLHTTP_EXPORT +void llhttp_set_lenient_version(llhttp_t* parser, int enabled); + +/* Enables/disables lenient handling of additional data received after a message ends + * and keep-alive is disabled. + * + * Normally `llhttp` would error when additional unexpected data is received if the message + * contains the `Connection` header with `close` value. + * With this flag the extra data will discarded without throwing an error. + * + * **Enabling this flag can pose a security issue since you will be exposed to + * poisoning attacks. USE WITH CAUTION!** + */ +LLHTTP_EXPORT +void llhttp_set_lenient_data_after_close(llhttp_t* parser, int enabled); + +/* Enables/disables lenient handling of incomplete CRLF sequences. + * + * Normally `llhttp` would error when a CR is not followed by LF when terminating the + * request line, the status line, the headers or a chunk header. + * With this flag only a CR is required to terminate such sections. + * + * **Enabling this flag can pose a security issue since you will be exposed to + * request smuggling attacks. USE WITH CAUTION!** + */ +LLHTTP_EXPORT +void llhttp_set_lenient_optional_lf_after_cr(llhttp_t* parser, int enabled); + +/* + * Enables/disables lenient handling of line separators. + * + * Normally `llhttp` would error when a LF is not preceded by CR when terminating the + * request line, the status line, the headers, a chunk header or a chunk data. + * With this flag only a LF is required to terminate such sections. + * + * **Enabling this flag can pose a security issue since you will be exposed to + * request smuggling attacks. USE WITH CAUTION!** + */ +LLHTTP_EXPORT +void llhttp_set_lenient_optional_cr_before_lf(llhttp_t* parser, int enabled); + +/* Enables/disables lenient handling of chunks not separated via CRLF. + * + * Normally `llhttp` would error when after a chunk data a CRLF is missing before + * starting a new chunk. + * With this flag the new chunk can start immediately after the previous one. + * + * **Enabling this flag can pose a security issue since you will be exposed to + * request smuggling attacks. USE WITH CAUTION!** + */ +LLHTTP_EXPORT +void llhttp_set_lenient_optional_crlf_after_chunk(llhttp_t* parser, int enabled); + +/* Enables/disables lenient handling of spaces after chunk size. + * + * Normally `llhttp` would error when after a chunk size is followed by one or more + * spaces are present instead of a CRLF or `;`. + * With this flag this check is disabled. + * + * **Enabling this flag can pose a security issue since you will be exposed to + * request smuggling attacks. USE WITH CAUTION!** + */ +LLHTTP_EXPORT +void llhttp_set_lenient_spaces_after_chunk_size(llhttp_t* parser, int enabled); + +#ifdef __cplusplus +} /* extern "C" */ +#endif +#endif /* INCLUDE_LLHTTP_API_H_ */ + + +#endif /* INCLUDE_LLHTTP_H_ */ diff --git a/deps/ntlmclient/CMakeLists.txt b/deps/ntlmclient/CMakeLists.txt new file mode 100644 index 00000000000..a3e8c2aff03 --- /dev/null +++ b/deps/ntlmclient/CMakeLists.txt @@ -0,0 +1,39 @@ +file(GLOB SRC_NTLMCLIENT "ntlm.c" "ntlm.h" "util.c" "util.h") +list(SORT SRC_NTLMCLIENT) + +add_definitions(-DNTLM_STATIC=1) + +disable_warnings(implicit-fallthrough) + +if(USE_ICONV) + add_definitions(-DUNICODE_ICONV=1) + file(GLOB SRC_NTLMCLIENT_UNICODE "unicode_iconv.c" "unicode_iconv.h") +else() + add_definitions(-DUNICODE_BUILTIN=1) + file(GLOB SRC_NTLMCLIENT_UNICODE "unicode_builtin.c" "unicode_builtin.h") +endif() + +if(USE_HTTPS STREQUAL "securetransport") + add_definitions(-DCRYPT_COMMONCRYPTO) + set(SRC_NTLMCLIENT_CRYPTO "crypt_commoncrypto.c" "crypt_commoncrypto.h") + # CC_MD4 has been deprecated in macOS 10.15. + set_source_files_properties("crypt_commoncrypto.c" COMPILE_FLAGS "-Wno-deprecated") +elseif(USE_HTTPS STREQUAL "openssl") + add_definitions(-DCRYPT_OPENSSL) + add_definitions(-DOPENSSL_API_COMPAT=0x10100000L) + include_directories(${OPENSSL_INCLUDE_DIR}) + set(SRC_NTLMCLIENT_CRYPTO "crypt_openssl.c" "crypt_openssl.h") +elseif(USE_HTTPS STREQUAL "openssl-dynamic") + add_definitions(-DCRYPT_OPENSSL) + add_definitions(-DCRYPT_OPENSSL_DYNAMIC) + add_definitions(-DOPENSSL_API_COMPAT=0x10100000L) + set(SRC_NTLMCLIENT_CRYPTO "crypt_openssl.c" "crypt_openssl.h") +elseif(USE_HTTPS STREQUAL "mbedtls") + add_definitions(-DCRYPT_MBEDTLS) + include_directories(${MBEDTLS_INCLUDE_DIR}) + set(SRC_NTLMCLIENT_CRYPTO "crypt_mbedtls.c" "crypt_mbedtls.h" "crypt_builtin_md4.c") +else() + message(FATAL_ERROR "Unable to use libgit2's HTTPS backend (${USE_HTTPS}) for NTLM crypto") +endif() + +add_library(ntlmclient OBJECT ${SRC_NTLMCLIENT} ${SRC_NTLMCLIENT_UNICODE} ${SRC_NTLMCLIENT_CRYPTO}) diff --git a/deps/ntlmclient/compat.h b/deps/ntlmclient/compat.h new file mode 100644 index 00000000000..befc7cc4fb3 --- /dev/null +++ b/deps/ntlmclient/compat.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#ifndef PRIVATE_COMPAT_H__ +#define PRIVATE_COMPAT_H__ + +#if defined (_MSC_VER) + typedef unsigned char bool; +# ifndef true +# define true 1 +# endif +# ifndef false +# define false 0 +# endif +#else +# include +#endif + +#ifndef MIN +# define MIN(x, y) (((x) < (y)) ? (x) : (y)) +#endif + +#endif /* PRIVATE_COMPAT_H__ */ diff --git a/deps/ntlmclient/crypt.h b/deps/ntlmclient/crypt.h new file mode 100644 index 00000000000..4ad543ef78e --- /dev/null +++ b/deps/ntlmclient/crypt.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#ifndef PRIVATE_CRYPT_COMMON_H__ +#define PRIVATE_CRYPT_COMMON_H__ + +#include "ntlmclient.h" +#include "ntlm.h" + +#if defined(CRYPT_OPENSSL) +# include "crypt_openssl.h" +#elif defined(CRYPT_MBEDTLS) +# include "crypt_mbedtls.h" +#elif defined(CRYPT_COMMONCRYPTO) +# include "crypt_commoncrypto.h" +#else +# error "no crypto support" +#endif + +#define CRYPT_DES_BLOCKSIZE 8 +#define CRYPT_MD4_DIGESTSIZE 16 +#define CRYPT_MD5_DIGESTSIZE 16 + +typedef unsigned char ntlm_des_block[CRYPT_DES_BLOCKSIZE]; + +typedef struct ntlm_crypt_ctx ntlm_crypt_ctx; + +extern bool ntlm_crypt_init(ntlm_client *ntlm); + +extern bool ntlm_random_bytes( + unsigned char *out, + ntlm_client *ntlm, + size_t len); + +extern bool ntlm_des_encrypt( + ntlm_des_block *out, + ntlm_client *ntlm, + ntlm_des_block *plaintext, + ntlm_des_block *key); + +extern bool ntlm_md4_digest( + unsigned char out[CRYPT_MD4_DIGESTSIZE], + ntlm_client *ntlm, + const unsigned char *in, + size_t in_len); + +extern bool ntlm_hmac_md5_init( + ntlm_client *ntlm, + const unsigned char *key, + size_t key_len); + +extern bool ntlm_hmac_md5_update( + ntlm_client *ntlm, + const unsigned char *data, + size_t data_len); + +extern bool ntlm_hmac_md5_final( + unsigned char *out, + size_t *out_len, + ntlm_client *ntlm); + +extern void ntlm_crypt_shutdown(ntlm_client *ntlm); + +#endif /* PRIVATE_CRYPT_COMMON_H__ */ diff --git a/deps/ntlmclient/crypt_builtin_md4.c b/deps/ntlmclient/crypt_builtin_md4.c new file mode 100644 index 00000000000..de9a85cafaa --- /dev/null +++ b/deps/ntlmclient/crypt_builtin_md4.c @@ -0,0 +1,311 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#include +#include + +#include "ntlm.h" +#include "crypt.h" + +/* + * Below is the MD4 code from RFC 1320, with minor modifications + * to make it compile on a modern compiler. It is included since + * many system crypto libraries lack MD4, sensibly. + */ + +/* MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm + */ + +/* Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved. + + License to copy and use this software is granted provided that it + is identified as the "RSA Data Security, Inc. MD4 Message-Digest + Algorithm" in all material mentioning or referencing this software + or this function. + + License is also granted to make and use derivative works provided + that such works are identified as "derived from the RSA Data + Security, Inc. MD4 Message-Digest Algorithm" in all material + mentioning or referencing the derived work. + + RSA Data Security, Inc. makes no representations concerning either + the merchantability of this software or the suitability of this + software for any particular purpose. It is provided "as is" + without express or implied warranty of any kind. + + These notices must be retained in any copies of any part of this + documentation and/or software. + */ + +/* POINTER defines a generic pointer type */ +typedef unsigned char *POINTER; + +/* UINT2 defines a two byte word */ +typedef uint16_t UINT2; + +/* UINT4 defines a four byte word */ +typedef uint32_t UINT4; + +#define MD4_memcpy memcpy +#define MD4_memset memset + +/* MD4 context. */ +typedef struct { + UINT4 state[4]; /* state (ABCD) */ + UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ + unsigned char buffer[64]; /* input buffer */ +} MD4_CTX; + +/* Constants for MD4Transform routine. + */ +#define S11 3 +#define S12 7 +#define S13 11 +#define S14 19 +#define S21 3 +#define S22 5 +#define S23 9 +#define S24 13 +#define S31 3 +#define S32 9 +#define S33 11 +#define S34 15 + +static void MD4Transform(UINT4 [4], const unsigned char [64]); +static void Encode (unsigned char *, UINT4 *, unsigned int); +static void Decode (UINT4 *, const unsigned char *, unsigned int); + +static unsigned char PADDING[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +/* F, G and H are basic MD4 functions. + */ +#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) +#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) + +/* ROTATE_LEFT rotates x left n bits. + */ +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +/* FF, GG and HH are transformations for rounds 1, 2 and 3 */ +/* Rotation is separate from addition to prevent recomputation */ +#define FF(a, b, c, d, x, s) { \ + (a) += F ((b), (c), (d)) + (x); \ + (a) = ROTATE_LEFT ((a), (s)); \ + } +#define GG(a, b, c, d, x, s) { \ + (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \ + (a) = ROTATE_LEFT ((a), (s)); \ + } +#define HH(a, b, c, d, x, s) { \ + (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \ + (a) = ROTATE_LEFT ((a), (s)); \ + } + +/* MD4 initialization. Begins an MD4 operation, writing a new context. + */ +static void MD4Init (MD4_CTX *context) +{ + context->count[0] = context->count[1] = 0; + + /* Load magic initialization constants. + */ + context->state[0] = 0x67452301; + context->state[1] = 0xefcdab89; + context->state[2] = 0x98badcfe; + context->state[3] = 0x10325476; +} + +/* MD4 block update operation. Continues an MD4 message-digest + operation, processing another message block, and updating the + context. + */ +static void MD4Update (MD4_CTX *context, const unsigned char *input, unsigned int inputLen) +{ + unsigned int i, index, partLen; + + /* Compute number of bytes mod 64 */ + index = (unsigned int)((context->count[0] >> 3) & 0x3F); + /* Update number of bits */ + if ((context->count[0] += ((UINT4)inputLen << 3)) + < ((UINT4)inputLen << 3)) + context->count[1]++; + context->count[1] += ((UINT4)inputLen >> 29); + + partLen = 64 - index; + + /* Transform as many times as possible. + */ + if (inputLen >= partLen) { + MD4_memcpy + ((POINTER)&context->buffer[index], (POINTER)input, partLen); + MD4Transform (context->state, context->buffer); + + for (i = partLen; i + 63 < inputLen; i += 64) + MD4Transform (context->state, &input[i]); + + index = 0; + } + else + i = 0; + + /* Buffer remaining input */ + MD4_memcpy + ((POINTER)&context->buffer[index], (POINTER)&input[i], + inputLen-i); +} + +/* MD4 finalization. Ends an MD4 message-digest operation, writing the + the message digest and zeroizing the context. + */ +static void MD4Final (unsigned char digest[16], MD4_CTX *context) +{ + unsigned char bits[8]; + unsigned int index, padLen; + + /* Save number of bits */ + Encode (bits, context->count, 8); + + /* Pad out to 56 mod 64. + */ + index = (unsigned int)((context->count[0] >> 3) & 0x3f); + padLen = (index < 56) ? (56 - index) : (120 - index); + MD4Update (context, PADDING, padLen); + + /* Append length (before padding) */ + MD4Update (context, bits, 8); + /* Store state in digest */ + Encode (digest, context->state, 16); + + /* Zeroize sensitive information. + */ + MD4_memset ((POINTER)context, 0, sizeof (*context)); +} + +/* MD4 basic transformation. Transforms state based on block. + */ +static void MD4Transform (UINT4 state[4], const unsigned char block[64]) +{ + UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; + + Decode (x, block, 64); + + /* Round 1 */ + FF (a, b, c, d, x[ 0], S11); /* 1 */ + FF (d, a, b, c, x[ 1], S12); /* 2 */ + FF (c, d, a, b, x[ 2], S13); /* 3 */ + FF (b, c, d, a, x[ 3], S14); /* 4 */ + FF (a, b, c, d, x[ 4], S11); /* 5 */ + FF (d, a, b, c, x[ 5], S12); /* 6 */ + FF (c, d, a, b, x[ 6], S13); /* 7 */ + FF (b, c, d, a, x[ 7], S14); /* 8 */ + FF (a, b, c, d, x[ 8], S11); /* 9 */ + FF (d, a, b, c, x[ 9], S12); /* 10 */ + FF (c, d, a, b, x[10], S13); /* 11 */ + FF (b, c, d, a, x[11], S14); /* 12 */ + FF (a, b, c, d, x[12], S11); /* 13 */ + FF (d, a, b, c, x[13], S12); /* 14 */ + FF (c, d, a, b, x[14], S13); /* 15 */ + FF (b, c, d, a, x[15], S14); /* 16 */ + + /* Round 2 */ + GG (a, b, c, d, x[ 0], S21); /* 17 */ + GG (d, a, b, c, x[ 4], S22); /* 18 */ + GG (c, d, a, b, x[ 8], S23); /* 19 */ + GG (b, c, d, a, x[12], S24); /* 20 */ + GG (a, b, c, d, x[ 1], S21); /* 21 */ + GG (d, a, b, c, x[ 5], S22); /* 22 */ + GG (c, d, a, b, x[ 9], S23); /* 23 */ + GG (b, c, d, a, x[13], S24); /* 24 */ + GG (a, b, c, d, x[ 2], S21); /* 25 */ + GG (d, a, b, c, x[ 6], S22); /* 26 */ + GG (c, d, a, b, x[10], S23); /* 27 */ + GG (b, c, d, a, x[14], S24); /* 28 */ + GG (a, b, c, d, x[ 3], S21); /* 29 */ + GG (d, a, b, c, x[ 7], S22); /* 30 */ + GG (c, d, a, b, x[11], S23); /* 31 */ + GG (b, c, d, a, x[15], S24); /* 32 */ + + /* Round 3 */ + HH (a, b, c, d, x[ 0], S31); /* 33 */ + HH (d, a, b, c, x[ 8], S32); /* 34 */ + HH (c, d, a, b, x[ 4], S33); /* 35 */ + HH (b, c, d, a, x[12], S34); /* 36 */ + HH (a, b, c, d, x[ 2], S31); /* 37 */ + HH (d, a, b, c, x[10], S32); /* 38 */ + HH (c, d, a, b, x[ 6], S33); /* 39 */ + HH (b, c, d, a, x[14], S34); /* 40 */ + HH (a, b, c, d, x[ 1], S31); /* 41 */ + HH (d, a, b, c, x[ 9], S32); /* 42 */ + HH (c, d, a, b, x[ 5], S33); /* 43 */ + HH (b, c, d, a, x[13], S34); /* 44 */ + HH (a, b, c, d, x[ 3], S31); /* 45 */ + HH (d, a, b, c, x[11], S32); /* 46 */ + HH (c, d, a, b, x[ 7], S33); /* 47 */ + HH (b, c, d, a, x[15], S34); /* 48 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + + /* Zeroize sensitive information. + */ + MD4_memset ((POINTER)x, 0, sizeof (x)); +} + +/* Encodes input (UINT4) into output (unsigned char). Assumes len is + a multiple of 4. + */ +static void Encode (unsigned char *output, UINT4 *input, unsigned int len) +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) { + output[j] = (unsigned char)(input[i] & 0xff); + output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); + output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); + output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); + } +} + +/* Decodes input (unsigned char) into output (UINT4). Assumes len is + a multiple of 4. + */ +static void Decode (UINT4 *output, const unsigned char *input, unsigned int len) +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) + output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | + (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); +} + +bool ntlm_md4_digest( + unsigned char out[CRYPT_MD4_DIGESTSIZE], + ntlm_client *ntlm, + const unsigned char *in, + size_t in_len) +{ + MD4_CTX ctx; + + NTLM_UNUSED(ntlm); + + if (in_len > UINT_MAX) + return false; + + MD4Init(&ctx); + MD4Update(&ctx, in, (unsigned int)in_len); + MD4Final (out, &ctx); + + return true; +} diff --git a/deps/ntlmclient/crypt_commoncrypto.c b/deps/ntlmclient/crypt_commoncrypto.c new file mode 100644 index 00000000000..3c20469f58d --- /dev/null +++ b/deps/ntlmclient/crypt_commoncrypto.c @@ -0,0 +1,121 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#include +#include +#include +#include +#include +#include + +#include + +#include "ntlm.h" +#include "crypt.h" + +bool ntlm_crypt_init(ntlm_client *ntlm) +{ + memset(&ntlm->crypt_ctx, 0, sizeof(ntlm_crypt_ctx)); + return true; +} + +bool ntlm_random_bytes( + unsigned char *out, + ntlm_client *ntlm, + size_t len) +{ + int fd, ret; + size_t total = 0; + + if ((fd = open("/dev/urandom", O_RDONLY)) < 0) { + ntlm_client_set_errmsg(ntlm, strerror(errno)); + return false; + } + + while (total < len) { + if ((ret = read(fd, out, (len - total))) < 0) { + ntlm_client_set_errmsg(ntlm, strerror(errno)); + return false; + } else if (ret == 0) { + ntlm_client_set_errmsg(ntlm, "unexpected eof on random device"); + return false; + } + + total += ret; + } + + close(fd); + return true; +} + +bool ntlm_des_encrypt( + ntlm_des_block *out, + ntlm_client *ntlm, + ntlm_des_block *plaintext, + ntlm_des_block *key) +{ + CCCryptorStatus result; + size_t written; + + NTLM_UNUSED(ntlm); + + result = CCCrypt(kCCEncrypt, + kCCAlgorithmDES, kCCOptionECBMode, + key, sizeof(ntlm_des_block), NULL, + plaintext, sizeof(ntlm_des_block), + out, sizeof(ntlm_des_block), &written); + + return (result == kCCSuccess) ? true : false; +} + +bool ntlm_md4_digest( + unsigned char out[CRYPT_MD4_DIGESTSIZE], + ntlm_client *ntlm, + const unsigned char *in, + size_t in_len) +{ + NTLM_UNUSED(ntlm); + return !!CC_MD4(in, in_len, out); +} + +bool ntlm_hmac_md5_init( + ntlm_client *ntlm, + const unsigned char *key, + size_t key_len) +{ + CCHmacInit(&ntlm->crypt_ctx.hmac, kCCHmacAlgMD5, key, key_len); + return true; +} + +bool ntlm_hmac_md5_update( + ntlm_client *ntlm, + const unsigned char *data, + size_t data_len) +{ + CCHmacUpdate(&ntlm->crypt_ctx.hmac, data, data_len); + return true; +} + +bool ntlm_hmac_md5_final( + unsigned char *out, + size_t *out_len, + ntlm_client *ntlm) +{ + if (*out_len < CRYPT_MD5_DIGESTSIZE) + return false; + + CCHmacFinal(&ntlm->crypt_ctx.hmac, out); + + *out_len = CRYPT_MD5_DIGESTSIZE; + return true; +} + +void ntlm_crypt_shutdown(ntlm_client *ntlm) +{ + NTLM_UNUSED(ntlm); +} diff --git a/deps/ntlmclient/crypt_commoncrypto.h b/deps/ntlmclient/crypt_commoncrypto.h new file mode 100644 index 00000000000..e4df91d2951 --- /dev/null +++ b/deps/ntlmclient/crypt_commoncrypto.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#ifndef PRIVATE_CRYPT_COMMONCRYPTO_H__ +#define PRIVATE_CRYPT_COMMONCRYPTO_H__ + +#include + +struct ntlm_crypt_ctx { + CCHmacContext hmac; +}; + +#endif /* PRIVATE_CRYPT_COMMONCRYPTO_H__ */ diff --git a/deps/ntlmclient/crypt_mbedtls.c b/deps/ntlmclient/crypt_mbedtls.c new file mode 100644 index 00000000000..4bbb878015d --- /dev/null +++ b/deps/ntlmclient/crypt_mbedtls.c @@ -0,0 +1,126 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#include +#include + +#include "mbedtls/ctr_drbg.h" +#include "mbedtls/des.h" +#include "mbedtls/entropy.h" + +#include "ntlm.h" +#include "crypt.h" + +bool ntlm_crypt_init(ntlm_client *ntlm) +{ + const mbedtls_md_info_t *info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5); + + mbedtls_md_init(&ntlm->crypt_ctx.hmac); + + if (mbedtls_md_setup(&ntlm->crypt_ctx.hmac, info, 1) != 0) { + ntlm_client_set_errmsg(ntlm, "could not setup mbedtls digest"); + return false; + } + + return true; +} + + +bool ntlm_random_bytes( + unsigned char *out, + ntlm_client *ntlm, + size_t len) +{ + mbedtls_ctr_drbg_context ctr_drbg; + mbedtls_entropy_context entropy; + bool ret = true; + + const unsigned char personalization[] = { + 0xec, 0xb5, 0xd1, 0x0b, 0x8f, 0x15, 0x1f, 0xc2, + 0xe4, 0x8e, 0xec, 0x36, 0xf7, 0x0a, 0x45, 0x9a, + 0x1f, 0xe1, 0x35, 0x58, 0xb1, 0xcb, 0xfd, 0x8a, + 0x57, 0x5c, 0x75, 0x7d, 0x2f, 0xc9, 0x70, 0xac + }; + + mbedtls_ctr_drbg_init(&ctr_drbg); + mbedtls_entropy_init(&entropy); + + if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, + &entropy, personalization, sizeof(personalization)) || + mbedtls_ctr_drbg_random(&ctr_drbg, out, len)) { + ntlm_client_set_errmsg(ntlm, "random generation failed"); + ret = false; + } + + mbedtls_entropy_free(&entropy); + mbedtls_ctr_drbg_free(&ctr_drbg); + + return ret; +} + +bool ntlm_des_encrypt( + ntlm_des_block *out, + ntlm_client *ntlm, + ntlm_des_block *plaintext, + ntlm_des_block *key) +{ + mbedtls_des_context ctx; + bool success = false; + + mbedtls_des_init(&ctx); + + if (mbedtls_des_setkey_enc(&ctx, *key) || + mbedtls_des_crypt_ecb(&ctx, *plaintext, *out)) { + ntlm_client_set_errmsg(ntlm, "DES encryption failed"); + goto done; + } + + success = true; + +done: + mbedtls_des_free(&ctx); + return success; +} + +bool ntlm_hmac_md5_init( + ntlm_client *ntlm, + const unsigned char *key, + size_t key_len) +{ + if (ntlm->crypt_ctx.hmac_initialized) { + if (mbedtls_md_hmac_reset(&ntlm->crypt_ctx.hmac)) + return false; + } + + ntlm->crypt_ctx.hmac_initialized = !mbedtls_md_hmac_starts(&ntlm->crypt_ctx.hmac, key, key_len); + return ntlm->crypt_ctx.hmac_initialized; +} + +bool ntlm_hmac_md5_update( + ntlm_client *ntlm, + const unsigned char *in, + size_t in_len) +{ + return !mbedtls_md_hmac_update(&ntlm->crypt_ctx.hmac, in, in_len); +} + +bool ntlm_hmac_md5_final( + unsigned char *out, + size_t *out_len, + ntlm_client *ntlm) +{ + if (*out_len < CRYPT_MD5_DIGESTSIZE) + return false; + + return !mbedtls_md_hmac_finish(&ntlm->crypt_ctx.hmac, out); +} + +void ntlm_crypt_shutdown(ntlm_client *ntlm) +{ + mbedtls_md_free(&ntlm->crypt_ctx.hmac); +} diff --git a/deps/ntlmclient/crypt_mbedtls.h b/deps/ntlmclient/crypt_mbedtls.h new file mode 100644 index 00000000000..2fc85035d9f --- /dev/null +++ b/deps/ntlmclient/crypt_mbedtls.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#ifndef PRIVATE_CRYPT_MBEDTLS_H__ +#define PRIVATE_CRYPT_MBEDTLS_H__ + +#include "mbedtls/md.h" + +struct ntlm_crypt_ctx { + mbedtls_md_context_t hmac; + unsigned int hmac_initialized : 1; +}; + +#endif /* PRIVATE_CRYPT_MBEDTLS_H__ */ diff --git a/deps/ntlmclient/crypt_openssl.c b/deps/ntlmclient/crypt_openssl.c new file mode 100644 index 00000000000..3bec2725999 --- /dev/null +++ b/deps/ntlmclient/crypt_openssl.c @@ -0,0 +1,259 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#include +#include + +#ifdef CRYPT_OPENSSL_DYNAMIC +# include +#else +# include +# include +# include +# include +# include +#endif + +#include "ntlm.h" +#include "compat.h" +#include "util.h" +#include "crypt.h" + +#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(CRYPT_OPENSSL_DYNAMIC) + +NTLM_INLINE(HMAC_CTX *) HMAC_CTX_new(void) +{ + return calloc(1, sizeof(HMAC_CTX)); +} + +NTLM_INLINE(int) HMAC_CTX_reset(HMAC_CTX *ctx) +{ + ntlm_memzero(ctx, sizeof(HMAC_CTX)); + return 1; +} + +NTLM_INLINE(void) HMAC_CTX_free(HMAC_CTX *ctx) +{ + free(ctx); +} + +#endif + +#if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)) || \ + (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x03050000fL) || \ + defined(CRYPT_OPENSSL_DYNAMIC) + +NTLM_INLINE(void) HMAC_CTX_cleanup(HMAC_CTX *ctx) +{ + NTLM_UNUSED(ctx); +} + +#endif + + +#ifdef CRYPT_OPENSSL_DYNAMIC + +static bool ntlm_crypt_init_functions(ntlm_client *ntlm) +{ + void *handle; + + if ((handle = dlopen("libssl.so.1.1", RTLD_NOW)) == NULL && + (handle = dlopen("libssl.1.1.dylib", RTLD_NOW)) == NULL && + (handle = dlopen("libssl.so.1.0.0", RTLD_NOW)) == NULL && + (handle = dlopen("libssl.1.0.0.dylib", RTLD_NOW)) == NULL && + (handle = dlopen("libssl.so.10", RTLD_NOW)) == NULL) { + ntlm_client_set_errmsg(ntlm, "could not open libssl"); + return false; + } + + ntlm->crypt_ctx.des_set_key_fn = dlsym(handle, "DES_set_key"); + ntlm->crypt_ctx.des_ecb_encrypt_fn = dlsym(handle, "DES_ecb_encrypt"); + ntlm->crypt_ctx.err_get_error_fn = dlsym(handle, "ERR_get_error"); + ntlm->crypt_ctx.err_lib_error_string_fn = dlsym(handle, "ERR_lib_error_string"); + ntlm->crypt_ctx.evp_md5_fn = dlsym(handle, "EVP_md5"); + ntlm->crypt_ctx.hmac_ctx_new_fn = dlsym(handle, "HMAC_CTX_new"); + ntlm->crypt_ctx.hmac_ctx_free_fn = dlsym(handle, "HMAC_CTX_free"); + ntlm->crypt_ctx.hmac_ctx_reset_fn = dlsym(handle, "HMAC_CTX_reset"); + ntlm->crypt_ctx.hmac_init_ex_fn = dlsym(handle, "HMAC_Init_ex"); + ntlm->crypt_ctx.hmac_update_fn = dlsym(handle, "HMAC_Update"); + ntlm->crypt_ctx.hmac_final_fn = dlsym(handle, "HMAC_Final"); + ntlm->crypt_ctx.md4_fn = dlsym(handle, "MD4"); + ntlm->crypt_ctx.rand_bytes_fn = dlsym(handle, "RAND_bytes"); + + if (!ntlm->crypt_ctx.des_set_key_fn || + !ntlm->crypt_ctx.des_ecb_encrypt_fn || + !ntlm->crypt_ctx.err_get_error_fn || + !ntlm->crypt_ctx.err_lib_error_string_fn || + !ntlm->crypt_ctx.evp_md5_fn || + !ntlm->crypt_ctx.hmac_init_ex_fn || + !ntlm->crypt_ctx.hmac_update_fn || + !ntlm->crypt_ctx.hmac_final_fn || + !ntlm->crypt_ctx.md4_fn || + !ntlm->crypt_ctx.rand_bytes_fn) { + ntlm_client_set_errmsg(ntlm, "could not load libssl functions"); + dlclose(handle); + return false; + } + + /* Toggle legacy HMAC context functions */ + if (ntlm->crypt_ctx.hmac_ctx_new_fn && + ntlm->crypt_ctx.hmac_ctx_free_fn && + ntlm->crypt_ctx.hmac_ctx_reset_fn) { + ntlm->crypt_ctx.hmac_ctx_cleanup_fn = HMAC_CTX_cleanup; + } else { + ntlm->crypt_ctx.hmac_ctx_cleanup_fn = dlsym(handle, "HMAC_CTX_cleanup"); + + if (!ntlm->crypt_ctx.hmac_ctx_cleanup_fn) { + ntlm_client_set_errmsg(ntlm, "could not load legacy libssl functions"); + dlclose(handle); + return false; + } + + ntlm->crypt_ctx.hmac_ctx_new_fn = HMAC_CTX_new; + ntlm->crypt_ctx.hmac_ctx_free_fn = HMAC_CTX_free; + ntlm->crypt_ctx.hmac_ctx_reset_fn = HMAC_CTX_reset; + } + + ntlm->crypt_ctx.openssl_handle = handle; + return true; +} + +#else /* CRYPT_OPENSSL_DYNAMIC */ + +static bool ntlm_crypt_init_functions(ntlm_client *ntlm) +{ + ntlm->crypt_ctx.des_set_key_fn = DES_set_key; + ntlm->crypt_ctx.des_ecb_encrypt_fn = DES_ecb_encrypt; + ntlm->crypt_ctx.err_get_error_fn = ERR_get_error; + ntlm->crypt_ctx.err_lib_error_string_fn = ERR_lib_error_string; + ntlm->crypt_ctx.evp_md5_fn = EVP_md5; + ntlm->crypt_ctx.hmac_ctx_new_fn = HMAC_CTX_new; + ntlm->crypt_ctx.hmac_ctx_free_fn = HMAC_CTX_free; + ntlm->crypt_ctx.hmac_ctx_reset_fn = HMAC_CTX_reset; + ntlm->crypt_ctx.hmac_ctx_cleanup_fn = HMAC_CTX_cleanup; + ntlm->crypt_ctx.hmac_init_ex_fn = HMAC_Init_ex; + ntlm->crypt_ctx.hmac_update_fn = HMAC_Update; + ntlm->crypt_ctx.hmac_final_fn = HMAC_Final; + ntlm->crypt_ctx.md4_fn = MD4; + ntlm->crypt_ctx.rand_bytes_fn = RAND_bytes; + + return true; +} + +#endif /* CRYPT_OPENSSL_DYNAMIC */ + +bool ntlm_crypt_init(ntlm_client *ntlm) +{ + if (!ntlm_crypt_init_functions(ntlm)) + return false; + + ntlm->crypt_ctx.hmac = ntlm->crypt_ctx.hmac_ctx_new_fn(); + + if (ntlm->crypt_ctx.hmac == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + return false; + } + + return true; +} + +bool ntlm_random_bytes( + unsigned char *out, + ntlm_client *ntlm, + size_t len) +{ + int rc = ntlm->crypt_ctx.rand_bytes_fn(out, len); + + if (rc != 1) { + ntlm_client_set_errmsg(ntlm, ntlm->crypt_ctx.err_lib_error_string_fn(ntlm->crypt_ctx.err_get_error_fn())); + return false; + } + + return true; +} + +bool ntlm_des_encrypt( + ntlm_des_block *out, + ntlm_client *ntlm, + ntlm_des_block *plaintext, + ntlm_des_block *key) +{ + DES_key_schedule keysched; + + NTLM_UNUSED(ntlm); + + memset(out, 0, sizeof(ntlm_des_block)); + + ntlm->crypt_ctx.des_set_key_fn(key, &keysched); + ntlm->crypt_ctx.des_ecb_encrypt_fn(plaintext, out, &keysched, DES_ENCRYPT); + + return true; +} + +bool ntlm_md4_digest( + unsigned char out[CRYPT_MD4_DIGESTSIZE], + ntlm_client *ntlm, + const unsigned char *in, + size_t in_len) +{ + ntlm->crypt_ctx.md4_fn(in, in_len, out); + return true; +} + +bool ntlm_hmac_md5_init( + ntlm_client *ntlm, + const unsigned char *key, + size_t key_len) +{ + const EVP_MD *md5 = ntlm->crypt_ctx.evp_md5_fn(); + + ntlm->crypt_ctx.hmac_ctx_cleanup_fn(ntlm->crypt_ctx.hmac); + + return ntlm->crypt_ctx.hmac_ctx_reset_fn(ntlm->crypt_ctx.hmac) && + ntlm->crypt_ctx.hmac_init_ex_fn(ntlm->crypt_ctx.hmac, key, key_len, md5, NULL); +} + +bool ntlm_hmac_md5_update( + ntlm_client *ntlm, + const unsigned char *in, + size_t in_len) +{ + return ntlm->crypt_ctx.hmac_update_fn(ntlm->crypt_ctx.hmac, in, in_len); +} + +bool ntlm_hmac_md5_final( + unsigned char *out, + size_t *out_len, + ntlm_client *ntlm) +{ + unsigned int len; + + if (*out_len < CRYPT_MD5_DIGESTSIZE) + return false; + + if (!ntlm->crypt_ctx.hmac_final_fn(ntlm->crypt_ctx.hmac, out, &len)) + return false; + + *out_len = len; + return true; +} + +void ntlm_crypt_shutdown(ntlm_client *ntlm) +{ + if (ntlm->crypt_ctx.hmac) { + ntlm->crypt_ctx.hmac_ctx_cleanup_fn(ntlm->crypt_ctx.hmac); + ntlm->crypt_ctx.hmac_ctx_free_fn(ntlm->crypt_ctx.hmac); + } + +#ifdef CRYPT_OPENSSL_DYNAMIC + if (ntlm->crypt_ctx.openssl_handle) + dlclose(ntlm->crypt_ctx.openssl_handle); +#endif + + memset(&ntlm->crypt_ctx, 0, sizeof(ntlm_crypt_ctx)); +} diff --git a/deps/ntlmclient/crypt_openssl.h b/deps/ntlmclient/crypt_openssl.h new file mode 100644 index 00000000000..8654027dbff --- /dev/null +++ b/deps/ntlmclient/crypt_openssl.h @@ -0,0 +1,90 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#ifndef PRIVATE_CRYPT_OPENSSL_H__ +#define PRIVATE_CRYPT_OPENSSL_H__ + +#ifndef CRYPT_OPENSSL_DYNAMIC +# include +# include +#endif + +/* OpenSSL 1.1.0 uses opaque structs, we'll reuse these. */ +#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L +# define HMAC_CTX struct hmac_ctx_st +#endif + +#ifdef CRYPT_OPENSSL_DYNAMIC +typedef unsigned char DES_cblock[8]; +typedef unsigned char const_DES_cblock[8]; + +typedef unsigned long DES_LONG; + +typedef struct DES_ks { + union { + DES_cblock cblock; + DES_LONG deslong[2]; + } ks[16]; +} DES_key_schedule; + +#define DES_ENCRYPT 1 + +typedef void EVP_MD; +typedef void ENGINE; +typedef void EVP_PKEY_CTX; + +#define HMAC_MAX_MD_CBLOCK 128 + +typedef struct env_md_ctx_st EVP_MD_CTX; +struct env_md_ctx_st { + const EVP_MD *digest; + ENGINE *engine; + unsigned long flags; + void *md_data; + EVP_PKEY_CTX *pctx; + int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count); +}; + +typedef struct hmac_ctx_st { + const EVP_MD *md; + EVP_MD_CTX md_ctx; + EVP_MD_CTX i_ctx; + EVP_MD_CTX o_ctx; + unsigned int key_length; + unsigned char key[HMAC_MAX_MD_CBLOCK]; +} HMAC_CTX; +#endif + +struct ntlm_crypt_ctx { + HMAC_CTX *hmac; + + void *openssl_handle; + + void (*des_ecb_encrypt_fn)(const_DES_cblock *input, DES_cblock *output, DES_key_schedule *ks, int enc); + int (*des_set_key_fn)(const_DES_cblock *key, DES_key_schedule *schedule); + + unsigned long (*err_get_error_fn)(void); + const char *(*err_lib_error_string_fn)(unsigned long e); + + const EVP_MD *(*evp_md5_fn)(void); + + HMAC_CTX *(*hmac_ctx_new_fn)(void); + int (*hmac_ctx_reset_fn)(HMAC_CTX *ctx); + void (*hmac_ctx_free_fn)(HMAC_CTX *ctx); + void (*hmac_ctx_cleanup_fn)(HMAC_CTX *ctx); + + int (*hmac_init_ex_fn)(HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md, ENGINE *impl); + int (*hmac_update_fn)(HMAC_CTX *ctx, const unsigned char *data, size_t len); + int (*hmac_final_fn)(HMAC_CTX *ctx, unsigned char *md, unsigned int *len); + + unsigned char *(*md4_fn)(const unsigned char *d, size_t n, unsigned char *md); + + int (*rand_bytes_fn)(unsigned char *buf, int num); +}; + +#endif /* PRIVATE_CRYPT_OPENSSL_H__ */ diff --git a/deps/ntlmclient/ntlm.c b/deps/ntlmclient/ntlm.c new file mode 100644 index 00000000000..28dff670e16 --- /dev/null +++ b/deps/ntlmclient/ntlm.c @@ -0,0 +1,1451 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ntlm.h" +#include "unicode.h" +#include "utf8.h" +#include "crypt.h" +#include "compat.h" +#include "util.h" + +#define NTLM_ASSERT_ARG(expr) do { \ + if (!(expr)) \ + return NTLM_CLIENT_ERROR_INVALID_INPUT; \ + } while(0) + +#define NTLM_ASSERT(ntlm, expr) do { \ + if (!(expr)) { \ + ntlm_client_set_errmsg(ntlm, "internal error: " #expr); \ + return -1; \ + } \ + } while(0) + +unsigned char ntlm_client_signature[] = NTLM_SIGNATURE; + +static bool supports_unicode(ntlm_client *ntlm) +{ + return (ntlm->flags & NTLM_CLIENT_DISABLE_UNICODE) ? + false : true; +} + +NTLM_INLINE(bool) increment_size(size_t *out, size_t incr) +{ + if (SIZE_MAX - *out < incr) { + *out = (size_t)-1; + return false; + } + + *out = *out + incr; + return true; +} + +ntlm_client *ntlm_client_init(ntlm_client_flags flags) +{ + ntlm_client *ntlm = NULL; + + if ((ntlm = calloc(1, sizeof(ntlm_client))) == NULL) + return NULL; + + ntlm->flags = flags; + + return ntlm; +} + +#define ENSURE_INITIALIZED(ntlm) \ + do { \ + if (!(ntlm)->unicode_initialized) \ + (ntlm)->unicode_initialized = ntlm_unicode_init((ntlm)); \ + if (!(ntlm)->crypt_initialized) \ + (ntlm)->crypt_initialized = ntlm_crypt_init((ntlm)); \ + if (!(ntlm)->unicode_initialized || \ + !(ntlm)->crypt_initialized) \ + return -1; \ + } while(0) + +void ntlm_client_set_errmsg(ntlm_client *ntlm, const char *errmsg) +{ + ntlm->state = NTLM_STATE_ERROR; + ntlm->errmsg = errmsg; +} + +const char *ntlm_client_errmsg(ntlm_client *ntlm) +{ + if (!ntlm) + return "internal error"; + + return ntlm->errmsg ? ntlm->errmsg : "no error"; +} + +int ntlm_client_set_version( + ntlm_client *ntlm, + uint8_t major, + uint8_t minor, + uint16_t build) +{ + NTLM_ASSERT_ARG(ntlm); + + ntlm->host_version.major = major; + ntlm->host_version.minor = minor; + ntlm->host_version.build = build; + ntlm->host_version.reserved = 0x0f000000; + + ntlm->flags |= NTLM_ENABLE_HOSTVERSION; + + return 0; +} + +#define reset(ptr) do { free(ptr); ptr = NULL; } while(0) + +static void free_hostname(ntlm_client *ntlm) +{ + reset(ntlm->hostname); + reset(ntlm->hostdomain); + reset(ntlm->hostname_utf16); + ntlm->hostname_utf16_len = 0; +} + +int ntlm_client_set_hostname( + ntlm_client *ntlm, + const char *hostname, + const char *domain) +{ + NTLM_ASSERT_ARG(ntlm); + ENSURE_INITIALIZED(ntlm); + + free_hostname(ntlm); + + if (hostname && (ntlm->hostname = strdup(hostname)) == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + return -1; + } + + if (domain && (ntlm->hostdomain = strdup(domain)) == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + return -1; + } + + if (hostname && supports_unicode(ntlm) && !ntlm_unicode_utf8_to_16( + &ntlm->hostname_utf16, + &ntlm->hostname_utf16_len, + ntlm, + hostname, + strlen(hostname))) + return -1; + + return 0; +} + +static void free_credentials(ntlm_client *ntlm) +{ + if (ntlm->password) + ntlm_memzero(ntlm->password, strlen(ntlm->password)); + + if (ntlm->password_utf16) + ntlm_memzero(ntlm->password_utf16, ntlm->password_utf16_len); + + reset(ntlm->username); + reset(ntlm->username_upper); + reset(ntlm->userdomain); + reset(ntlm->password); + + reset(ntlm->username_utf16); + reset(ntlm->username_upper_utf16); + reset(ntlm->userdomain_utf16); + reset(ntlm->password_utf16); + + ntlm->username_utf16_len = 0; + ntlm->username_upper_utf16_len = 0; + ntlm->userdomain_utf16_len = 0; + ntlm->password_utf16_len = 0; +} + +int ntlm_client_set_credentials( + ntlm_client *ntlm, + const char *username, + const char *domain, + const char *password) +{ + NTLM_ASSERT_ARG(ntlm); + ENSURE_INITIALIZED(ntlm); + + free_credentials(ntlm); + + if ((username && (ntlm->username = strdup(username)) == NULL) || + (domain && (ntlm->userdomain = strdup(domain)) == NULL) || + (password && (ntlm->password = strdup(password)) == NULL)) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + return -1; + } + + if (username && supports_unicode(ntlm)) { + if ((ntlm->username_upper = strdup(username)) == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + return -1; + } + utf8upr(ntlm->username_upper); + + if (!ntlm_unicode_utf8_to_16( + &ntlm->username_utf16, + &ntlm->username_utf16_len, + ntlm, + ntlm->username, + strlen(ntlm->username))) + return -1; + + if (!ntlm_unicode_utf8_to_16( + &ntlm->username_upper_utf16, + &ntlm->username_upper_utf16_len, + ntlm, + ntlm->username_upper, + strlen(ntlm->username_upper))) + return -1; + } + + if (domain && supports_unicode(ntlm) && !ntlm_unicode_utf8_to_16( + &ntlm->userdomain_utf16, + &ntlm->userdomain_utf16_len, + ntlm, + ntlm->userdomain, + strlen(ntlm->userdomain))) + return -1; + + return 0; +} + +int ntlm_client_set_target(ntlm_client *ntlm, const char *target) +{ + NTLM_ASSERT_ARG(ntlm); + ENSURE_INITIALIZED(ntlm); + + free(ntlm->target); + free(ntlm->target_utf16); + + ntlm->target = NULL; + ntlm->target_utf16 = NULL; + + if (target) { + if ((ntlm->target = strdup(target)) == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + return -1; + } + + if (supports_unicode(ntlm) && !ntlm_unicode_utf8_to_16( + &ntlm->target_utf16, + &ntlm->target_utf16_len, + ntlm, + ntlm->target, + strlen(ntlm->target))) + return -1; + } + + return 0; +} + +int ntlm_client_set_nonce(ntlm_client *ntlm, uint64_t nonce) +{ + NTLM_ASSERT_ARG(ntlm); + + ntlm->nonce = nonce; + return 0; +} + +int ntlm_client_set_timestamp(ntlm_client *ntlm, uint64_t timestamp) +{ + NTLM_ASSERT_ARG(ntlm); + + ntlm->timestamp = timestamp; + return 0; +} + +NTLM_INLINE(bool) write_buf( + ntlm_client *ntlm, + ntlm_buf *out, + const unsigned char *buf, + size_t len) +{ + if (!len) + return true; + + if (out->len - out->pos < len) { + ntlm_client_set_errmsg(ntlm, "out of buffer space"); + return false; + } + + memcpy(&out->buf[out->pos], buf, len); + out->pos += len; + return true; +} + +NTLM_INLINE(bool) write_byte( + ntlm_client *ntlm, + ntlm_buf *out, + uint8_t value) +{ + if (out->len - out->pos < 1) { + ntlm_client_set_errmsg(ntlm, "out of buffer space"); + return false; + } + + out->buf[out->pos++] = value; + return true; +} + +NTLM_INLINE(bool) write_int16( + ntlm_client *ntlm, + ntlm_buf *out, + uint16_t value) +{ + if (out->len - out->pos < 2) { + ntlm_client_set_errmsg(ntlm, "out of buffer space"); + return false; + } + + out->buf[out->pos++] = (value & 0x000000ff); + out->buf[out->pos++] = (value & 0x0000ff00) >> 8; + return true; +} + +NTLM_INLINE(bool) write_int32( + ntlm_client *ntlm, + ntlm_buf *out, + uint32_t value) +{ + if (out->len - out->pos < 2) { + ntlm_client_set_errmsg(ntlm, "out of buffer space"); + return false; + } + + out->buf[out->pos++] = (value & 0x000000ff); + out->buf[out->pos++] = (value & 0x0000ff00) >> 8; + out->buf[out->pos++] = (value & 0x00ff0000) >> 16; + out->buf[out->pos++] = (value & 0xff000000) >> 24; + return true; +} + +NTLM_INLINE(bool) write_version( + ntlm_client *ntlm, + ntlm_buf *out, + ntlm_version *version) +{ + return write_byte(ntlm, out, version->major) && + write_byte(ntlm, out, version->minor) && + write_int16(ntlm, out, version->build) && + write_int32(ntlm, out, version->reserved); +} + +NTLM_INLINE(bool) write_bufinfo( + ntlm_client *ntlm, + ntlm_buf *out, + size_t len, + size_t offset) +{ + if (len > UINT16_MAX) { + ntlm_client_set_errmsg(ntlm, "invalid string, too long"); + return false; + } + + if (offset > UINT32_MAX) { + ntlm_client_set_errmsg(ntlm, "invalid string, invalid offset"); + return false; + } + + return write_int16(ntlm, out, (uint16_t)len) && + write_int16(ntlm, out, (uint16_t)len) && + write_int32(ntlm, out, (uint32_t)offset); +} + +NTLM_INLINE(bool) read_buf( + unsigned char *out, + ntlm_client *ntlm, + ntlm_buf *message, + size_t len) +{ + if (message->len - message->pos < len) { + ntlm_client_set_errmsg(ntlm, "truncated message"); + return false; + } + + memcpy(out, &message->buf[message->pos], len); + message->pos += len; + + return true; +} + +NTLM_INLINE(bool) read_byte( + uint8_t *out, + ntlm_client *ntlm, + ntlm_buf *message) +{ + if (message->len - message->pos < 1) { + ntlm_client_set_errmsg(ntlm, "truncated message"); + return false; + } + + *out = message->buf[message->pos++]; + return true; +} + +NTLM_INLINE(bool) read_int16( + uint16_t *out, + ntlm_client *ntlm, + ntlm_buf *message) +{ + if (message->len - message->pos < 2) { + ntlm_client_set_errmsg(ntlm, "truncated message"); + return false; + } + + *out = + ((message->buf[message->pos] & 0xff)) | + ((message->buf[message->pos+1] & 0xff) << 8); + + message->pos += 2; + return true; +} + +NTLM_INLINE(bool) read_int32( + uint32_t *out, + ntlm_client *ntlm, + ntlm_buf *message) +{ + if (message->len - message->pos < 4) { + ntlm_client_set_errmsg(ntlm, "truncated message"); + return false; + } + + *out = + ((message->buf[message->pos] & 0xff)) | + ((message->buf[message->pos+1] & 0xff) << 8) | + ((message->buf[message->pos+2] & 0xff) << 16) | + ((message->buf[message->pos+3] & 0xff) << 24); + + message->pos += 4; + return true; +} + +NTLM_INLINE(bool) read_int64( + uint64_t *out, + ntlm_client *ntlm, + ntlm_buf *message) +{ + if (message->len - message->pos < 8) { + ntlm_client_set_errmsg(ntlm, "truncated message"); + return false; + } + + *out = + ((uint64_t)(message->buf[message->pos] & 0xff)) | + ((uint64_t)(message->buf[message->pos+1] & 0xff) << 8) | + ((uint64_t)(message->buf[message->pos+2] & 0xff) << 16) | + ((uint64_t)(message->buf[message->pos+3] & 0xff) << 24) | + ((uint64_t)(message->buf[message->pos+4] & 0xff) << 32) | + ((uint64_t)(message->buf[message->pos+5] & 0xff) << 40) | + ((uint64_t)(message->buf[message->pos+6] & 0xff) << 48) | + ((uint64_t)(message->buf[message->pos+7] & 0xff) << 56); + + message->pos += 8; + return true; +} + +NTLM_INLINE(bool) read_version( + ntlm_version *out, + ntlm_client *ntlm, + ntlm_buf *message) +{ + return read_byte(&out->major, ntlm, message) && + read_byte(&out->minor, ntlm, message) && + read_int16(&out->build, ntlm, message) && + read_int32(&out->reserved, ntlm, message); +} + +NTLM_INLINE(bool) read_bufinfo( + uint16_t *out_len, + uint32_t *out_offset, + ntlm_client *ntlm, + ntlm_buf *message) +{ + uint16_t allocated; + + return read_int16(out_len, ntlm, message) && + read_int16(&allocated, ntlm, message) && + read_int32(out_offset, ntlm, message); +} + +NTLM_INLINE(bool) read_string_unicode( + char **out, + ntlm_client *ntlm, + ntlm_buf *message, + uint8_t string_len) +{ + size_t out_len; + int ret = ntlm_unicode_utf16_to_8(out, + &out_len, + ntlm, + (char *)&message->buf[message->pos], + string_len); + + message->pos += string_len; + + return ret; +} + +NTLM_INLINE(bool) read_string_ascii( + char **out, + ntlm_client *ntlm, + ntlm_buf *message, + uint8_t string_len) +{ + char *str; + + if ((str = malloc(string_len + 1)) == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + return false; + } + + memcpy(str, &message->buf[message->pos], string_len); + str[string_len] = '\0'; + + message->pos += string_len; + + *out = str; + return true; +} + +NTLM_INLINE(bool) read_string( + char **out, + ntlm_client *ntlm, + ntlm_buf *message, + uint8_t string_len, + bool unicode) +{ + if (unicode) + return read_string_unicode(out, ntlm, message, string_len); + else + return read_string_ascii(out, ntlm, message, string_len); +} + +NTLM_INLINE(bool) read_target_info( + char **server_out, + char **domain_out, + char **server_dns_out, + char **domain_dns_out, + ntlm_client *ntlm, + ntlm_buf *message, + bool unicode) +{ + uint16_t block_type, block_len; + bool done = false; + + *server_out = NULL; + *domain_out = NULL; + *server_dns_out = NULL; + *domain_dns_out = NULL; + + while (!done && (message->len - message->pos) >= 4) { + if (!read_int16(&block_type, ntlm, message) || + !read_int16(&block_len, ntlm, message)) { + ntlm_client_set_errmsg(ntlm, "truncated target info block"); + return false; + } + + if (!block_type && block_len) { + ntlm_client_set_errmsg(ntlm, "invalid target info block"); + return -1; + } + + switch (block_type) { + case NTLM_TARGET_INFO_DOMAIN: + if (!read_string(domain_out, ntlm, message, block_len, unicode)) + return -1; + break; + case NTLM_TARGET_INFO_SERVER: + if (!read_string(server_out, ntlm, message, block_len, unicode)) + return -1; + break; + case NTLM_TARGET_INFO_DOMAIN_DNS: + if (!read_string(domain_dns_out, ntlm, message, block_len, unicode)) + return -1; + break; + case NTLM_TARGET_INFO_SERVER_DNS: + if (!read_string(server_dns_out, ntlm, message, block_len, unicode)) + return -1; + break; + case NTLM_TARGET_INFO_END: + done = true; + break; + default: + ntlm_client_set_errmsg(ntlm, "unknown target info block type"); + return -1; + } + } + + if (message->len != message->pos) { + ntlm_client_set_errmsg(ntlm, + "invalid extra data in target info section"); + return false; + } + + return true; +} + +int ntlm_client_negotiate( + const unsigned char **out, + size_t *out_len, + ntlm_client *ntlm) +{ + size_t hostname_len, domain_len; + size_t domain_offset = 0; + size_t hostname_offset = 0; + uint32_t flags = 0; + + NTLM_ASSERT_ARG(out); + NTLM_ASSERT_ARG(out_len); + NTLM_ASSERT_ARG(ntlm); + + *out = NULL; + *out_len = 0; + + if (ntlm->state != NTLM_STATE_NEGOTIATE) { + ntlm_client_set_errmsg(ntlm, "ntlm handle in invalid state"); + return -1; + } + + flags |= NTLM_NEGOTIATE_OEM; + + if (supports_unicode(ntlm)) + flags |= NTLM_NEGOTIATE_UNICODE; + + if (!(ntlm->flags & NTLM_CLIENT_DISABLE_NTLM2) || + (ntlm->flags & NTLM_CLIENT_ENABLE_NTLM)) + flags |= NTLM_NEGOTIATE_NTLM; + + if (!(ntlm->flags & NTLM_CLIENT_DISABLE_REQUEST_TARGET)) + flags |= NTLM_NEGOTIATE_REQUEST_TARGET; + + hostname_len = ntlm->hostname ? strlen(ntlm->hostname) : 0; + domain_len = ntlm->hostdomain ? strlen(ntlm->hostdomain) : 0; + + /* Minimum header size */ + ntlm->negotiate.len = 16; + + /* Include space for security buffer descriptors */ + if (domain_len) + increment_size(&ntlm->negotiate.len, 8); + + if (hostname_len) + increment_size(&ntlm->negotiate.len, 8); + + if (ntlm->flags & NTLM_ENABLE_HOSTVERSION) + increment_size(&ntlm->negotiate.len, 8); + + /* Location of security buffers */ + if (hostname_len) { + flags |= NTLM_NEGOTIATE_WORKSTATION_SUPPLIED; + hostname_offset = ntlm->negotiate.len; + increment_size(&ntlm->negotiate.len, hostname_len); + } + + if (domain_len) { + flags |= NTLM_NEGOTIATE_DOMAIN_SUPPLIED; + domain_offset = ntlm->negotiate.len; + increment_size(&ntlm->negotiate.len, domain_len); + } + + if (ntlm->negotiate.len == (size_t)-1) { + ntlm_client_set_errmsg(ntlm, "message too large"); + return -1; + } + + if ((ntlm->negotiate.buf = calloc(1, ntlm->negotiate.len)) == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + return -1; + } + + if (!write_buf(ntlm, &ntlm->negotiate, + ntlm_client_signature, sizeof(ntlm_client_signature)) || + !write_int32(ntlm, &ntlm->negotiate, 1) || + !write_int32(ntlm, &ntlm->negotiate, flags)) + return -1; + + /* Domain information */ + if (domain_len > 0 && + !write_bufinfo(ntlm, &ntlm->negotiate, domain_len, domain_offset)) + return -1; + + /* Workstation information */ + if (hostname_len > 0 && + !write_bufinfo(ntlm, &ntlm->negotiate, hostname_len, hostname_offset)) + return -1; + + /* Version number */ + if (!!(ntlm->flags & NTLM_ENABLE_HOSTVERSION) && + !write_version(ntlm, &ntlm->negotiate, &ntlm->host_version)) + return -1; + + if (hostname_len > 0) { + NTLM_ASSERT(ntlm, hostname_offset == ntlm->negotiate.pos); + + if (!write_buf(ntlm, &ntlm->negotiate, + (const unsigned char *)ntlm->hostname, hostname_len)) + return -1; + } + + if (domain_len > 0) { + NTLM_ASSERT(ntlm, domain_offset == ntlm->negotiate.pos); + + if (!write_buf(ntlm, &ntlm->negotiate, + (const unsigned char *)ntlm->hostdomain, domain_len)) + return -1; + } + + NTLM_ASSERT(ntlm, ntlm->negotiate.pos == ntlm->negotiate.len); + + ntlm->state = NTLM_STATE_CHALLENGE; + + *out = ntlm->negotiate.buf; + *out_len = ntlm->negotiate.len; + + return 0; +} + +int ntlm_client_set_challenge( + ntlm_client *ntlm, + const unsigned char *challenge_msg, + size_t challenge_msg_len) +{ + unsigned char signature[8]; + ntlm_buf challenge; + uint32_t type_indicator, header_end; + uint16_t name_len, info_len = 0; + uint32_t name_offset, info_offset = 0; + bool unicode, has_target_info = false; + + NTLM_ASSERT_ARG(ntlm); + NTLM_ASSERT_ARG(challenge_msg || !challenge_msg_len); + + ENSURE_INITIALIZED(ntlm); + + if (ntlm->state != NTLM_STATE_NEGOTIATE && + ntlm->state != NTLM_STATE_CHALLENGE) { + ntlm_client_set_errmsg(ntlm, "ntlm handle in invalid state"); + return -1; + } + + challenge.buf = (unsigned char *)challenge_msg; + challenge.len = challenge_msg_len; + challenge.pos = 0; + + if (!read_buf(signature, ntlm, &challenge, 8) || + !read_int32(&type_indicator, ntlm, &challenge) || + !read_bufinfo(&name_len, &name_offset, ntlm, &challenge) || + !read_int32(&ntlm->challenge.flags, ntlm, &challenge) || + !read_int64(&ntlm->challenge.nonce, ntlm, &challenge)) + return -1; + + if (memcmp(signature, + ntlm_client_signature, sizeof(ntlm_client_signature)) != 0) { + ntlm_client_set_errmsg(ntlm, "invalid message signature"); + return -1; + } + + if (type_indicator != 2) { + ntlm_client_set_errmsg(ntlm, "invalid message indicator"); + return -1; + } + + /* + * If there's additional space before the data section, that's the + * target information description section. + */ + header_end = challenge.len; + + if (name_offset && name_offset < header_end) + header_end = name_offset; + + if ((header_end - challenge.pos) >= 16) { + has_target_info = true; + } + + if (!has_target_info && + (ntlm->challenge.flags & NTLM_NEGOTIATE_TARGET_INFO)) { + ntlm_client_set_errmsg(ntlm, + "truncated message; expected target info"); + return -1; + } + + /* + * If there's a target info section then advanced over the reserved + * space and read the target information. + */ + if (has_target_info) { + uint64_t reserved; + + if (!read_int64(&reserved, ntlm, &challenge)) { + ntlm_client_set_errmsg(ntlm, + "truncated message; expected reserved space"); + return -1; + } + + if (reserved != 0) { + ntlm_client_set_errmsg(ntlm, + "invalid message; expected reserved space to be empty"); + return -1; + } + + if (!read_bufinfo(&info_len, &info_offset, ntlm, &challenge)) { + ntlm_client_set_errmsg(ntlm, + "truncated message; expected target info"); + return -1; + } + } + + unicode = !!(ntlm->challenge.flags & NTLM_NEGOTIATE_UNICODE); + + /* + * If there's still additional space before the data section, + * that's the server's version information. + */ + if (info_offset && info_offset < header_end) + header_end = info_offset; + + if (ntlm->challenge.flags & NTLM_NEGOTIATE_VERSION) { + if ((header_end - challenge.pos) != sizeof(ntlm_version) || + !read_version(&ntlm->challenge.target_version, + ntlm, &challenge)) { + ntlm_client_set_errmsg(ntlm, + "truncated message; expected version"); + return -1; + } + } + + /* validate data section */ + if ((name_offset && name_offset < challenge.pos) || + challenge.len < name_len || + (challenge.len - name_len) < name_offset) { + ntlm_client_set_errmsg(ntlm, + "invalid message; invalid target name buffer"); + return -1; + } + if ((info_offset && info_offset < challenge.pos) || + challenge.len < info_len || + (challenge.len - info_len) < info_offset) { + ntlm_client_set_errmsg(ntlm, + "invalid message; invalid target info buffer"); + return -1; + } + + /* advance to the data section */ + if (name_len && name_offset) { + challenge.pos = name_offset; + + if (!read_string(&ntlm->challenge.target, + ntlm, &challenge, name_len, unicode)) { + ntlm_client_set_errmsg(ntlm, + "truncated message; truncated target name"); + return -1; + } + } + + if (info_len && info_offset) { + ntlm_buf info_buf; + + challenge.pos = info_offset; + + /* create a copy of the target info; we need the literal data */ + if ((ntlm->challenge.target_info = malloc(info_len)) == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + return -1; + } + + if (!read_buf(ntlm->challenge.target_info, + ntlm, &challenge, info_len)) { + ntlm_client_set_errmsg(ntlm, + "truncated message; truncated target info"); + return -1; + } + + info_buf.buf = ntlm->challenge.target_info; + info_buf.pos = 0; + info_buf.len = info_len; + + /* then set up the target info and parse it */ + if (!read_target_info(&ntlm->challenge.target_server, + &ntlm->challenge.target_domain, + &ntlm->challenge.target_server_dns, + &ntlm->challenge.target_domain_dns, + ntlm, &info_buf, unicode)) + return -1; + + ntlm->challenge.target_info_len = info_len; + } + + ntlm->state = NTLM_STATE_RESPONSE; + + return 0; +} + +uint64_t ntlm_client_challenge_nonce(ntlm_client *ntlm) +{ + return ntlm->challenge.nonce; +} + +const char *ntlm_client_target(ntlm_client *ntlm) +{ + return ntlm->challenge.target; +} + +const char *ntlm_client_target_server(ntlm_client *ntlm) +{ + return ntlm->challenge.target_server; +} + +const char *ntlm_client_target_domain(ntlm_client *ntlm) +{ + return ntlm->challenge.target_domain; +} + +const char *ntlm_client_target_server_dns(ntlm_client *ntlm) +{ + return ntlm->challenge.target_server_dns; +} + +const char *ntlm_client_target_domain_dns(ntlm_client *ntlm) +{ + return ntlm->challenge.target_domain_dns; +} + +#define EVEN_PARITY(a) \ + (!!((a) & INT64_C(0x01)) ^ !!((a) & INT64_C(0x02)) ^ \ + !!((a) & INT64_C(0x04)) ^ !!((a) & INT64_C(0x08)) ^ \ + !!((a) & INT64_C(0x10)) ^ !!((a) & INT64_C(0x20)) ^ \ + !!((a) & INT64_C(0x40)) ^ !!((a) & INT64_C(0x80))) + +static void generate_odd_parity(ntlm_des_block *block) +{ + size_t i; + + for (i = 0; i < sizeof(ntlm_des_block); i++) + (*block)[i] |= (1 ^ EVEN_PARITY((*block)[i])); +} + +static void des_key_from_password( + ntlm_des_block *out, + const unsigned char *plaintext, + size_t plaintext_len) +{ + size_t i; + + plaintext_len = MIN(plaintext_len, 7); + + memset(*out, 0, sizeof(ntlm_des_block)); + + for (i = 0; i < plaintext_len; i++) { + size_t j = (7 - i); + uint8_t mask = (0xff >> j); + + (*out)[i] |= ((plaintext[i] & (0xff - mask)) >> i); + (*out)[i+1] |= ((plaintext[i] & mask) << j); + } + + generate_odd_parity(out); +} + +NTLM_INLINE(bool) generate_lm_hash( + ntlm_des_block out[2], + ntlm_client *ntlm, + const char *password) +{ + /* LM encrypts this known plaintext using the password as a key */ + ntlm_des_block plaintext = NTLM_LM_PLAINTEXT; + ntlm_des_block keystr1, keystr2; + size_t keystr1_len, keystr2_len; + ntlm_des_block key1, key2; + size_t password_len, i; + + /* Copy the first 14 characters of the password, uppercased */ + memset(&keystr1, 0, sizeof(keystr1)); + memset(&keystr2, 0, sizeof(keystr2)); + + password_len = password ? strlen(password) : 0; + + /* Split the password into two 7 byte chunks */ + keystr1_len = MIN(7, password_len); + keystr2_len = (password_len > 7) ? MIN(14, password_len) - 7 : 0; + + for (i = 0; i < keystr1_len; i++) + keystr1[i] = (unsigned char)toupper((unsigned char)password[i]); + for (i = 0; i < keystr2_len; i++) + keystr2[i] = (unsigned char)toupper((unsigned char)password[i+7]); + + /* DES encrypt the LM constant using the password as the key */ + des_key_from_password(&key1, keystr1, keystr1_len); + des_key_from_password(&key2, keystr2, keystr2_len); + + return ntlm_des_encrypt(&out[0], ntlm, &plaintext, &key1) && + ntlm_des_encrypt(&out[1], ntlm, &plaintext, &key2); +} + +static void des_keys_from_lm_hash(ntlm_des_block out[3], ntlm_des_block lm_hash[2]) +{ + ntlm_des_block split[3]; + + memcpy(&split[0][0], &lm_hash[0][0], 7); + + memcpy(&split[1][0], &lm_hash[0][7], 1); + memcpy(&split[1][1], &lm_hash[1][0], 6); + + memcpy(&split[2][0], &lm_hash[1][6], 2); + + des_key_from_password(&out[0], split[0], 7); + des_key_from_password(&out[1], split[1], 7); + des_key_from_password(&out[2], split[2], 2); +} + +static bool generate_lm_response(ntlm_client *ntlm) +{ + ntlm_des_block lm_hash[2], key[3], lm_response[3]; + ntlm_des_block *challenge = (ntlm_des_block *)&ntlm->challenge.nonce; + + /* Generate the LM hash from the password */ + if (!generate_lm_hash(lm_hash, ntlm, ntlm->password)) + return false; + + /* Convert that LM hash to three DES keys */ + des_keys_from_lm_hash(key, lm_hash); + + /* Finally, encrypt the challenge with each of these keys */ + if (!ntlm_des_encrypt(&lm_response[0], ntlm, challenge, &key[0]) || + !ntlm_des_encrypt(&lm_response[1], ntlm, challenge, &key[1]) || + !ntlm_des_encrypt(&lm_response[2], ntlm, challenge, &key[2])) + return false; + + memcpy(&ntlm->lm_response[0], lm_response[0], 8); + memcpy(&ntlm->lm_response[8], lm_response[1], 8); + memcpy(&ntlm->lm_response[16], lm_response[2], 8); + + ntlm->lm_response_len = sizeof(ntlm->lm_response); + + return true; +} + +static bool generate_ntlm_hash( + unsigned char out[NTLM_NTLM_HASH_LEN], ntlm_client *ntlm) +{ + /* Generate the LM hash from the (Unicode) password */ + if (ntlm->password && !ntlm_unicode_utf8_to_16( + &ntlm->password_utf16, + &ntlm->password_utf16_len, + ntlm, + ntlm->password, + strlen(ntlm->password))) + return false; + + return ntlm_md4_digest(out, + ntlm, + (const unsigned char *)ntlm->password_utf16, + ntlm->password_utf16_len); +} + +static bool generate_ntlm_response(ntlm_client *ntlm) +{ + unsigned char ntlm_hash[NTLM_NTLM_HASH_LEN] = {0}; + ntlm_des_block key[3], ntlm_response[3]; + ntlm_des_block *challenge = + (ntlm_des_block *)&ntlm->challenge.nonce; + + if (!generate_ntlm_hash(ntlm_hash, ntlm)) + return false; + + /* Convert that LM hash to three DES keys */ + des_key_from_password(&key[0], &ntlm_hash[0], 7); + des_key_from_password(&key[1], &ntlm_hash[7], 7); + des_key_from_password(&key[2], &ntlm_hash[14], 2); + + /* Finally, encrypt the challenge with each of these keys */ + if (!ntlm_des_encrypt(&ntlm_response[0], ntlm, challenge, &key[0]) || + !ntlm_des_encrypt(&ntlm_response[1], ntlm, challenge, &key[1]) || + !ntlm_des_encrypt(&ntlm_response[2], ntlm, challenge, &key[2])) + return false; + + memcpy(&ntlm->ntlm_response[0], ntlm_response[0], 8); + memcpy(&ntlm->ntlm_response[8], ntlm_response[1], 8); + memcpy(&ntlm->ntlm_response[16], ntlm_response[2], 8); + + ntlm->ntlm_response_len = sizeof(ntlm->ntlm_response); + return true; +} + +static bool generate_ntlm2_hash( + unsigned char out[NTLM_NTLM2_HASH_LEN], ntlm_client *ntlm) +{ + unsigned char ntlm_hash[NTLM_NTLM_HASH_LEN] = {0}; + const unsigned char *username = NULL, *target = NULL; + size_t username_len = 0, target_len = 0, out_len = NTLM_NTLM2_HASH_LEN; + + if (!generate_ntlm_hash(ntlm_hash, ntlm)) + return false; + + if (ntlm->username_upper_utf16) { + username = (const unsigned char *)ntlm->username_upper_utf16; + username_len = ntlm->username_upper_utf16_len; + } + + if (ntlm->target_utf16) { + target = (const unsigned char *)ntlm->target_utf16; + target_len = ntlm->target_utf16_len; + } + + if (!ntlm_hmac_md5_init(ntlm, ntlm_hash, sizeof(ntlm_hash)) || + !ntlm_hmac_md5_update(ntlm, username, username_len) || + !ntlm_hmac_md5_update(ntlm, target, target_len) || + !ntlm_hmac_md5_final(out, &out_len, ntlm)) { + ntlm_client_set_errmsg(ntlm, "failed to create HMAC-MD5"); + return false; + } + + NTLM_ASSERT(ntlm, out_len == NTLM_NTLM2_HASH_LEN); + return true; +} + +static bool generate_ntlm2_challengehash( + unsigned char out[16], + ntlm_client *ntlm, + unsigned char ntlm2_hash[NTLM_NTLM2_HASH_LEN], + const unsigned char *blob, + size_t blob_len) +{ + size_t out_len = 16; + + if (!ntlm_hmac_md5_init(ntlm, ntlm2_hash, NTLM_NTLM2_HASH_LEN) || + !ntlm_hmac_md5_update(ntlm, (const unsigned char *)&ntlm->challenge.nonce, 8) || + !ntlm_hmac_md5_update(ntlm, blob, blob_len) || + !ntlm_hmac_md5_final(out, &out_len, ntlm)) { + ntlm_client_set_errmsg(ntlm, "failed to create HMAC-MD5"); + return false; + } + + NTLM_ASSERT(ntlm, out_len == 16); + return true; +} + +static bool generate_lm2_response(ntlm_client *ntlm, + unsigned char ntlm2_hash[NTLM_NTLM2_HASH_LEN]) +{ + unsigned char lm2_challengehash[16] = {0}; + size_t lm2_len = 16; + uint64_t local_nonce; + + local_nonce = ntlm_htonll(ntlm->nonce); + + if (!ntlm_hmac_md5_init(ntlm, ntlm2_hash, NTLM_NTLM2_HASH_LEN) || + !ntlm_hmac_md5_update(ntlm, (const unsigned char *)&ntlm->challenge.nonce, 8) || + !ntlm_hmac_md5_update(ntlm, (const unsigned char *)&local_nonce, 8) || + !ntlm_hmac_md5_final(lm2_challengehash, &lm2_len, ntlm)) { + ntlm_client_set_errmsg(ntlm, "failed to create HMAC-MD5"); + return false; + } + + NTLM_ASSERT(ntlm, lm2_len == 16); + + memcpy(&ntlm->lm_response[0], lm2_challengehash, 16); + memcpy(&ntlm->lm_response[16], &local_nonce, 8); + + ntlm->lm_response_len = 24; + return true; +} + +static bool generate_timestamp(ntlm_client *ntlm) +{ + if (!ntlm->timestamp) + ntlm->timestamp = (time(NULL) + 11644473600) * 10000000; + + return true; +} + +static bool generate_nonce(ntlm_client *ntlm) +{ + unsigned char buf[8]; + + if (ntlm->nonce) + return true; + + if (!ntlm_random_bytes(buf, ntlm, 8)) + return false; + + memcpy(&ntlm->nonce, buf, sizeof(uint64_t)); + return true; +} + +static bool generate_ntlm2_response(ntlm_client *ntlm) +{ + size_t blob_len, ntlm2_response_len; + uint32_t signature; + uint64_t timestamp, nonce; + unsigned char ntlm2_hash[NTLM_NTLM2_HASH_LEN]; + unsigned char challengehash[16] = {0}; + unsigned char *blob; + + if (!generate_timestamp(ntlm) || + !generate_nonce(ntlm) || + !generate_ntlm2_hash(ntlm2_hash, ntlm)) + return false; + + blob_len = ntlm->challenge.target_info_len + 32; + ntlm2_response_len = blob_len + 16; + + if ((ntlm->ntlm2_response = malloc(ntlm2_response_len)) == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + return false; + } + + /* position the blob in the response; we'll use it then return it */ + blob = ntlm->ntlm2_response + 16; + + /* the blob's integer values are in network byte order */ + signature = htonl(0x01010000); + timestamp = ntlm_htonll(ntlm->timestamp); + nonce = ntlm_htonll(ntlm->nonce); + + /* construct the blob */ + memcpy(&blob[0], &signature, 4); + memset(&blob[4], 0, 4); + memcpy(&blob[8], ×tamp, 8); + memcpy(&blob[16], &nonce, 8); + memset(&blob[24], 0, 4); + memcpy(&blob[28], ntlm->challenge.target_info, ntlm->challenge.target_info_len); + memset(&blob[28 + ntlm->challenge.target_info_len], 0, 4); + + if (!generate_ntlm2_challengehash(challengehash, ntlm, ntlm2_hash, blob, blob_len)) + return false; + + memcpy(ntlm->ntlm2_response, challengehash, 16); + ntlm->ntlm2_response_len = ntlm2_response_len; + + if (!generate_lm2_response(ntlm, ntlm2_hash)) + return false; + + return true; +} + +int ntlm_client_response( + const unsigned char **out, + size_t *out_len, + ntlm_client *ntlm) +{ + unsigned char *domain, *username, *hostname, *ntlm_rep, *session; + size_t lm_rep_len, lm_rep_offset, ntlm_rep_len, ntlm_rep_offset, + domain_len, domain_offset, username_len, username_offset, + hostname_len, hostname_offset, session_len, session_offset; + uint32_t flags = 0; + bool unicode; + + NTLM_ASSERT_ARG(out); + NTLM_ASSERT_ARG(out_len); + NTLM_ASSERT_ARG(ntlm); + + ENSURE_INITIALIZED(ntlm); + + *out = NULL; + *out_len = 0; + + if (ntlm->state != NTLM_STATE_RESPONSE) { + ntlm_client_set_errmsg(ntlm, "ntlm handle in invalid state"); + return -1; + } + + /* + * Minimum message size is 64 bytes: + * 8 byte signature, + * 4 byte message indicator, + * 6x8 byte security buffers + * 4 byte flags + */ + ntlm->response.len = 64; + + unicode = supports_unicode(ntlm) && + (ntlm->challenge.flags & NTLM_NEGOTIATE_UNICODE); + + if (unicode) + flags |= NTLM_NEGOTIATE_UNICODE; + else + flags |= NTLM_NEGOTIATE_OEM; + + if (unicode) { + domain = (unsigned char *)ntlm->userdomain_utf16; + domain_len = ntlm->userdomain_utf16_len; + + username = (unsigned char *)ntlm->username_utf16; + username_len = ntlm->username_utf16_len; + + hostname = (unsigned char *)ntlm->hostname_utf16; + hostname_len = ntlm->hostname_utf16_len; + } else { + domain = (unsigned char *)ntlm->userdomain; + domain_len = ntlm->userdomain ? strlen(ntlm->userdomain) : 0; + + username = (unsigned char *)ntlm->username; + username_len = ntlm->username ? strlen(ntlm->username) : 0; + + hostname = (unsigned char *)ntlm->hostname; + hostname_len = ntlm->hostname ? strlen(ntlm->hostname) : 0; + } + + /* Negotiate our requested authentication type with the server's */ + if (!(ntlm->flags & NTLM_CLIENT_DISABLE_NTLM2) && + (ntlm->challenge.flags & NTLM_NEGOTIATE_NTLM)) { + flags |= NTLM_NEGOTIATE_NTLM; + + if (!generate_ntlm2_response(ntlm)) + return -1; + } else if ((ntlm->flags & NTLM_CLIENT_ENABLE_NTLM) && + (ntlm->challenge.flags & NTLM_NEGOTIATE_NTLM)) { + flags |= NTLM_NEGOTIATE_NTLM; + + if (!generate_ntlm_response(ntlm) || + !generate_lm_response(ntlm)) + return -1; + } else if (ntlm->flags & NTLM_CLIENT_ENABLE_LM) { + if (!generate_lm_response(ntlm)) + return -1; + } else { + ntlm_client_set_errmsg(ntlm, + "no encryption options could be negotiated"); + return -1; + } + + domain_offset = ntlm->response.len; + increment_size(&ntlm->response.len, domain_len); + + username_offset = ntlm->response.len; + increment_size(&ntlm->response.len, username_len); + + hostname_offset = ntlm->response.len; + increment_size(&ntlm->response.len, hostname_len); + + lm_rep_len = ntlm->lm_response_len; + lm_rep_offset = ntlm->response.len; + increment_size(&ntlm->response.len, lm_rep_len); + + ntlm_rep = ntlm->ntlm2_response_len ? + ntlm->ntlm2_response : ntlm->ntlm_response; + ntlm_rep_len = ntlm->ntlm2_response_len ? + ntlm->ntlm2_response_len : ntlm->ntlm_response_len; + ntlm_rep_offset = ntlm->response.len; + increment_size(&ntlm->response.len, ntlm_rep_len); + + session = NULL; + session_len = 0; + session_offset = ntlm->response.len; + increment_size(&ntlm->response.len, session_len); + + if (ntlm->response.len == (size_t)-1) { + ntlm_client_set_errmsg(ntlm, "message too large"); + return -1; + } + + if ((ntlm->response.buf = calloc(1, ntlm->response.len)) == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + return -1; + } + + if (!write_buf(ntlm, &ntlm->response, + ntlm_client_signature, sizeof(ntlm_client_signature)) || + !write_int32(ntlm, &ntlm->response, 3) || + !write_bufinfo(ntlm, &ntlm->response, lm_rep_len, lm_rep_offset) || + !write_bufinfo(ntlm, &ntlm->response, ntlm_rep_len, ntlm_rep_offset) || + !write_bufinfo(ntlm, &ntlm->response, domain_len, domain_offset) || + !write_bufinfo(ntlm, &ntlm->response, username_len, username_offset) || + !write_bufinfo(ntlm, &ntlm->response, hostname_len, hostname_offset) || + !write_bufinfo(ntlm, &ntlm->response, session_len, session_offset) || + !write_int32(ntlm, &ntlm->response, flags) || + !write_buf(ntlm, &ntlm->response, domain, domain_len) || + !write_buf(ntlm, &ntlm->response, username, username_len) || + !write_buf(ntlm, &ntlm->response, hostname, hostname_len) || + !write_buf(ntlm, &ntlm->response, ntlm->lm_response, lm_rep_len) || + !write_buf(ntlm, &ntlm->response, ntlm_rep, ntlm_rep_len) || + !write_buf(ntlm, &ntlm->response, session, session_len)) + return -1; + + NTLM_ASSERT(ntlm, ntlm->response.pos == ntlm->response.len); + + ntlm->state = NTLM_STATE_COMPLETE; + + *out = ntlm->response.buf; + *out_len = ntlm->response.len; + + return 0; +} + +void ntlm_client_reset(ntlm_client *ntlm) +{ + if (!ntlm) + return; + + ntlm->state = NTLM_STATE_NEGOTIATE; + + free_hostname(ntlm); + + memset(&ntlm->host_version, 0, sizeof(ntlm_version)); + + reset(ntlm->target); + reset(ntlm->target_utf16); + ntlm->target_utf16_len = 0; + + free_credentials(ntlm); + + ntlm->nonce = 0; + ntlm->timestamp = 0; + + memset(ntlm->lm_response, 0, NTLM_LM_RESPONSE_LEN); + ntlm->lm_response_len = 0; + + memset(ntlm->ntlm_response, 0, NTLM_NTLM_RESPONSE_LEN); + ntlm->ntlm_response_len = 0; + + reset(ntlm->ntlm2_response); + ntlm->ntlm2_response_len = 0; + + reset(ntlm->negotiate.buf); + ntlm->negotiate.pos = 0; + ntlm->negotiate.len = 0; + + reset(ntlm->response.buf); + ntlm->response.pos = 0; + ntlm->response.len = 0; + + free(ntlm->challenge.target_info); + free(ntlm->challenge.target); + free(ntlm->challenge.target_domain); + free(ntlm->challenge.target_domain_dns); + free(ntlm->challenge.target_server); + free(ntlm->challenge.target_server_dns); + memset(&ntlm->challenge, 0, sizeof(ntlm_challenge)); +} + +void ntlm_client_free(ntlm_client *ntlm) +{ + if (!ntlm) + return; + + ntlm_crypt_shutdown(ntlm); + ntlm_unicode_shutdown(ntlm); + + ntlm_client_reset(ntlm); + + free(ntlm); +} diff --git a/deps/ntlmclient/ntlm.h b/deps/ntlmclient/ntlm.h new file mode 100644 index 00000000000..4abfd7a8528 --- /dev/null +++ b/deps/ntlmclient/ntlm.h @@ -0,0 +1,178 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#ifndef PRIVATE_NTLM_H__ +#define PRIVATE_NTLM_H__ + +#include "ntlmclient.h" +#include "unicode.h" +#include "crypt.h" +#include "compat.h" + +#define NTLM_UNUSED(x) ((void)(x)) + +#define NTLM_LM_RESPONSE_LEN 24 +#define NTLM_NTLM_RESPONSE_LEN 24 +#define NTLM_NTLM_HASH_LEN 16 +#define NTLM_NTLM2_HASH_LEN 16 + +#define NTLM_SIGNATURE { 'N', 'T', 'L', 'M', 'S', 'S', 'P', 0x00 } + +#define NTLM_LM_PLAINTEXT { 0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 } + +typedef enum { + NTLM_STATE_NEGOTIATE = 0, + NTLM_STATE_CHALLENGE = 1, + NTLM_STATE_RESPONSE = 2, + NTLM_STATE_ERROR = 3, + NTLM_STATE_COMPLETE = 4 +} ntlm_state; + +typedef struct { + unsigned char *buf; + size_t pos; + size_t len; +} ntlm_buf; + +typedef struct { + uint8_t major; + uint8_t minor; + uint16_t build; + uint32_t reserved; +} ntlm_version; + +typedef struct { + uint32_t flags; + uint64_t nonce; + ntlm_version target_version; + + /* The unparsed target information from the server */ + unsigned char *target_info; + size_t target_info_len; + + /* The target information parsed into usable strings */ + char *target; + char *target_server; + char *target_domain; + char *target_server_dns; + char *target_domain_dns; +} ntlm_challenge; + +struct ntlm_client { + ntlm_client_flags flags; + + ntlm_state state; + + /* subsystem contexts */ + ntlm_crypt_ctx crypt_ctx; + ntlm_unicode_ctx unicode_ctx; + int crypt_initialized : 1, + unicode_initialized : 1; + + /* error message as set by the library */ + const char *errmsg; + + char *hostname; + char *hostdomain; + ntlm_version host_version; + + char *target; + + char *username; + char *username_upper; + char *userdomain; + char *password; + + /* strings as converted to utf16 */ + char *hostname_utf16; + char *target_utf16; + char *username_utf16; + char *username_upper_utf16; + char *userdomain_utf16; + char *password_utf16; + + size_t hostname_utf16_len; + size_t username_utf16_len; + size_t username_upper_utf16_len; + size_t userdomain_utf16_len; + size_t password_utf16_len; + size_t target_utf16_len; + + /* timestamp and nonce; only for debugging */ + uint64_t nonce; + uint64_t timestamp; + + unsigned char lm_response[NTLM_LM_RESPONSE_LEN]; + size_t lm_response_len; + + unsigned char ntlm_response[NTLM_NTLM_RESPONSE_LEN]; + size_t ntlm_response_len; + + unsigned char *ntlm2_response; + size_t ntlm2_response_len; + + ntlm_buf negotiate; + ntlm_challenge challenge; + ntlm_buf response; +}; + +typedef enum { + NTLM_ENABLE_HOSTVERSION = (1 << 31) +} ntlm_client_internal_flags; + +typedef enum { + NTLM_TARGET_INFO_END = 0, + NTLM_TARGET_INFO_SERVER = 1, + NTLM_TARGET_INFO_DOMAIN = 2, + NTLM_TARGET_INFO_SERVER_DNS = 3, + NTLM_TARGET_INFO_DOMAIN_DNS = 4 +} ntlm_target_info_type_t; + +typedef enum { + /* Unicode strings are supported in security buffers */ + NTLM_NEGOTIATE_UNICODE = 0x00000001, + + /* OEM (ANSI) strings are supported in security buffers */ + NTLM_NEGOTIATE_OEM = 0x00000002, + + /* Request the target realm from the server */ + NTLM_NEGOTIATE_REQUEST_TARGET = 0x00000004, + + /* NTLM authentication is supported */ + NTLM_NEGOTIATE_NTLM = 0x00000200, + + /* Negotiate domain name */ + NTLM_NEGOTIATE_DOMAIN_SUPPLIED = 0x00001000, + + /* Negotiate workstation (client) name */ + NTLM_NEGOTIATE_WORKSTATION_SUPPLIED = 0x00002000, + + /* Indicates that a local context is available */ + NTLM_NEGOTIATE_LOCAL_CALL = 0x00004000, + + /* Request a dummy signature */ + NTLM_NEGOTIATE_ALWAYS_SIGN = 0x00008000, + + /* Target (server) is a domain */ + NTLM_NEGOTIATE_TYPE_DOMAIN = 0x00010000, + + /* NTLM2 signing and sealing is supported */ + NTLM_NEGOTIATE_NTLM2_SIGN_AND_SEAL = 0x00080000, + + /* A target information block is included */ + NTLM_NEGOTIATE_TARGET_INFO = 0x00800000, + + /* Version information should be provided */ + NTLM_NEGOTIATE_VERSION = 0x01000000 +} ntlm_negotiate_t; + +extern int ntlm_client_set_nonce(ntlm_client *ntlm, uint64_t nonce); +extern int ntlm_client_set_timestamp(ntlm_client *ntlm, uint64_t timestamp); +extern void ntlm_client_set_errmsg(ntlm_client *ntlm, const char *errmsg); + +#endif /* PRIVATE_NTLM_H__ */ diff --git a/deps/ntlmclient/ntlmclient.h b/deps/ntlmclient/ntlmclient.h new file mode 100644 index 00000000000..958b27e6246 --- /dev/null +++ b/deps/ntlmclient/ntlmclient.h @@ -0,0 +1,333 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ +#ifndef INCLUDE_NTLMCLIENT_H__ +#define INCLUDE_NTLMCLIENT_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define NTLM_CLIENT_VERSION "0.9.0" +#define NTLM_CLIENT_VERSION_MAJOR 0 +#define NTLM_CLIENT_VERSION_MINOR 9 +#define NTLM_CLIENT_VERSION_TEENY 0 + +typedef struct ntlm_client ntlm_client; + +typedef enum { + /** + * An error occurred; more details are available by querying + * `ntlm_client_errmsg`. + */ + NTLM_CLIENT_ERROR = -1, + + /** + * The input provided to the function is missing or invalid. + */ + NTLM_CLIENT_ERROR_INVALID_INPUT = -2 +} ntlm_error_code; + +/* + * Flags for initializing the `ntlm_client` context. A combination of + * these flags can be provided to `ntlm_client_init`. + */ +typedef enum { + /** Default settings for the `ntlm_client`. */ + NTLM_CLIENT_DEFAULTS = 0, + + /** + * Disable Unicode negotiation. By default, strings are converted + * into UTF-16 when supplied to the remote host, but if this flag + * is specified, localizable strings (like username and password) + * will only be sent to the server as they were provided to the + * library. Since the NTLM protocol does not deliver the locale + * information, these will be interpreted by the remote host in + * whatever locale is configured, and likely be corrupted unless + * you limit yourself to ASCII. + * + * You are discouraged from setting this flag. + */ + NTLM_CLIENT_DISABLE_UNICODE = (1 << 0), + + /* + * Enable LM ("Lan Manager") authentication support. By default, + * LM authentication is disabled, since most remote servers have + * disabled support for it, and because it is both trivially + * brute-forced _and_ subject to rainbow table lookups. If this + * flag is enabled, LM is still not used unless NTLM2 support is + * also disabled. + * + * You are discouraged from setting this flag. + */ + NTLM_CLIENT_ENABLE_LM = (1 << 1), + + /* + * Enable NTLM ("Lan Manager") authentication support. By default, + * NTLM authentication is disabled, since most remote servers have + * disabled support for it, due to its weakness. If this flag is + * enabled, NTLM is still not used unless NTLM2 support is also + * disabled. + * + * You are discouraged from setting this flag. + */ + NTLM_CLIENT_ENABLE_NTLM = (1 << 2), + + /* + * Disable NTLM2 authentication support. By default, _only_ NTLM2 + * support is enabled, since most remote servers will only support + * it due to its (relative) lack of weakness. If this flag is + * set, either NTLM or LM (or both) must be explicitly enabled or + * there will be no mechanisms available to use. + * + * You are discouraged from setting this flag. + */ + NTLM_CLIENT_DISABLE_NTLM2 = (1 << 3), + + /* + * Request the target's name. By default, you are expected to + * provide the name of the target you are authenticating to (eg, + * the remote hostname). If set, the remote host will provide + * its idea of its hostname in the challenge message. You may + * then set the authentication target based on it. + */ + NTLM_CLIENT_DISABLE_REQUEST_TARGET = (1 << 4) +} ntlm_client_flags; + + +/** Declare a public function exported for application use. */ +#if __GNUC__ >= 4 && !defined(NTLM_STATIC) +# define NTLM_EXTERN(type) extern \ + __attribute__((visibility("default"))) \ + type +#elif defined(_MSC_VER) && !defined(NTLM_STATIC) +# define NTLM_EXTERN(type) __declspec(dllexport) type +#else +# define NTLM_EXTERN(type) extern type +#endif + +/** + * Initializes an `ntlm_client` context, which can begin sending + * and receiving NTLM authentication messages. + * + * @param flags the `ntlm_client_flag_t`s to use for negotiation. + * @return the `ntlm_client` context, or `NULL` if out-of-memory. + */ +NTLM_EXTERN(ntlm_client *) ntlm_client_init(ntlm_client_flags flags); + +/** + * Gets the error message for the most recent error that occurred. If + * a function returns an error, more details can be retrieved with this + * function. The string returned is a constant string; it should not + * be freed. + * + * @return a constant string containing the error message. + */ +NTLM_EXTERN(const char *) ntlm_client_errmsg(ntlm_client *ntlm); + +/** + * Sets the local hostname and domain. These strings should be in + * ASCII. They will be provided to the remote host during the + * negotiation phase. + * + * @param ntlm the `ntlm_client` context to configure + * @param hostname the hostname of the local machine + * @param domain the domain of the local machine + * @return 0 on success, non-zero on failure + */ +NTLM_EXTERN(int) ntlm_client_set_hostname( + ntlm_client *ntlm, + const char *hostname, + const char *domain); + +/** + * Sets the local operating system version. These numbers are expected + * to correspond to Windows operating system versions; for example + * major version 6, minor version 2, build 9200 would correspond to + * Windows 8 (aka "NT 6.2"). + * + * It is not likely that you need to set the local version. + * + * @param ntlm the `ntlm_client` context to configure + * @param major the major version number of the local operating system + * @param minor the minor version number of the local operating system + * @param build the build number of the local operating system + * @return 0 on success, non-zero on failure + */ +NTLM_EXTERN(int) ntlm_client_set_version( + ntlm_client *ntlm, + uint8_t major, + uint8_t minor, + uint16_t build); + +/** + * Sets the username and password to authenticate with to the remote + * host. Username and password may be specified in UTF-8 but the + * domain should be in ASCII. These will not be sent to the remote host + * but will instead be used to compute the LM, NTLM or NTLM2 responses, + * which will be provided to the remote host during the response phase. + * + * @param ntlm the `ntlm_client` context to configure + * @param username the username to authenticate with + * @param domain the domain of the user authenticating + * @param password the password to authenticate with + * @return 0 on success, non-zero on failure + */ +NTLM_EXTERN(int) ntlm_client_set_credentials( + ntlm_client *ntlm, + const char *username, + const char *domain, + const char *password); + +/** + * Sets the authentication target, your idea of the remote host's + * name. The target should be provided as ASCII. It will be + * provided to the remote host during the response phase. + * + * @param ntlm the `ntlm_client` context to configure + * @param target the name of the authentication target + * @return 0 on success, non-zero on failure + */ +NTLM_EXTERN(int) ntlm_client_set_target( + ntlm_client *ntlm, + const char *target); + +/** + * Gets the remote host's nonce, as it was provided in the challenge + * message. This is an opaque 8 byte value that is used to compute + * the LM, NTLM and NTLM2 responses. + * + * @param ntlm the `ntlm_client` context to query + * @return the challenge from the remote host + */ +NTLM_EXTERN(uint64_t) ntlm_client_challenge_nonce( + ntlm_client *ntlm); + +/** + * Gets the remote hosts's target name, which can be used as the + * authentication target. This will be given as it was provided + * in the challenge message. + * + * @param ntlm the `ntlm_client` context to query + * @return the remote host's target name + */ +NTLM_EXTERN(const char *) ntlm_client_target(ntlm_client *ntlm); + +/** + * Gets the remote hosts's name, which is generally its short name. + * This will be given as it was provided in the challenge message. + * + * @param ntlm the `ntlm_client` context to query + * @return the remote host's server name + */ +NTLM_EXTERN(const char *) ntlm_client_target_server(ntlm_client *ntlm); + +/** + * Gets the remote hosts's domain, which is generally the short or + * NT-style domain name. This will be given as it was provided in + * the challenge message. + * + * @param ntlm the `ntlm_client` context to query + * @return the remote host's domain + */ +NTLM_EXTERN(const char *) ntlm_client_target_domain(ntlm_client *ntlm); + +/** + * Gets the remote hosts's DNS name, which is generally the long-style + * Active Directory or fully-qualified hostname. This will be given + * as it was provided in the challenge message. + * + * @param ntlm the `ntlm_client` context to query + * @return the remote host's DNS name + */ +NTLM_EXTERN(const char *) ntlm_client_target_server_dns(ntlm_client *ntlm); + +/** + * Gets the remote hosts's DNS domain, which is generally the long-style + * Active Directory or fully-qualified domain name. This will be given + * as it was provided in the challenge message. + * + * @param ntlm the `ntlm_client` context to query + * @return the remote host's DNS domain + */ +NTLM_EXTERN(const char *) ntlm_client_target_domain_dns(ntlm_client *ntlm); + +/** + * Computes a negotiation message (aka a "Type 1" message) to begin + * NTLM authentication with the server. The local hostname should be + * set before calling this function (if necessary). This message + * should be delivered to the server to indicate a willingness to begin + * NTLM authentication. This buffer should not be freed by the caller. + * + * @param out a pointer to the negotiation message + * @param out_len a pointer to the length of the negotiation message + * @param ntlm the `ntlm_client` context + * @return 0 on success, non-zero on failure + */ +NTLM_EXTERN(int) ntlm_client_negotiate( + const unsigned char **out, + size_t *out_len, + ntlm_client *ntlm); + +/** + * Parses a challenge message (aka a "Type 2" message) from the server. + * This must be called in order to calculate the response to the + * authentication. + * + * @param ntlm the `ntlm_client` context + * @param message the challenge message from the server + * @param message_len the length of the challenge message + * @return 0 on success, non-zero on failure + */ +NTLM_EXTERN(int) ntlm_client_set_challenge( + ntlm_client *ntlm, + const unsigned char *message, + size_t message_len); + +/** + * Computes a response message (aka a "Type 3" message) to complete + * NTLM authentication with the server. The credentials should be + * set before calling this function. This message should be delivered + * to the server to complete authentication. This buffer should not + * be freed by the caller. + * + * @param out a pointer to the response message + * @param out_len a pointer to the length of the response message + * @param ntlm the `ntlm_client` context + * @return 0 on success, non-zero on failure + */ +NTLM_EXTERN(int) ntlm_client_response( + const unsigned char **out, + size_t *out_len, + ntlm_client *ntlm); + +/** + * Resets an `ntlm_client` context completely, so that authentication + * may be retried. You must set _all_ parameters again, including the + * target, username, password, etc. Once these values are configured + * again, the negotiation can begin. + * + * @param ntlm the `ntlm_client` context to reset + */ +NTLM_EXTERN(void) ntlm_client_reset(ntlm_client *ntlm); + +/** + * Frees an `ntlm_client` context. This should be done to free memory + * belonging to the context. The context cannot be reused. + * + * @param ntlm the `ntlm_client` context to free + */ +NTLM_EXTERN(void) ntlm_client_free(ntlm_client *ntlm); + +#ifdef __cplusplus +} +#endif + +#endif /* INCLUDE_NTLMCLIENT_H__ */ diff --git a/deps/ntlmclient/unicode.h b/deps/ntlmclient/unicode.h new file mode 100644 index 00000000000..b7c63f2ed46 --- /dev/null +++ b/deps/ntlmclient/unicode.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#ifndef PRIVATE_UNICODE_H__ +#define PRIVATE_UNICODE_H__ + +#include "compat.h" + +#ifdef UNICODE_ICONV +# include "unicode_iconv.h" +#elif UNICODE_BUILTIN +# include "unicode_builtin.h" +#endif + +#define NTLM_UNICODE_MAX_LEN 2048 + +typedef struct ntlm_unicode_ctx ntlm_unicode_ctx; + +extern bool ntlm_unicode_init(ntlm_client *ntlm); + +bool ntlm_unicode_utf8_to_16( + char **converted, + size_t *converted_len, + ntlm_client *ntlm, + const char *string, + size_t string_len); + +bool ntlm_unicode_utf16_to_8( + char **converted, + size_t *converted_len, + ntlm_client *ntlm, + const char *string, + size_t string_len); + +extern void ntlm_unicode_shutdown(ntlm_client *ntlm); + +#endif /* PRIVATE_UNICODE_H__ */ diff --git a/deps/ntlmclient/unicode_builtin.c b/deps/ntlmclient/unicode_builtin.c new file mode 100644 index 00000000000..cb98f70b3db --- /dev/null +++ b/deps/ntlmclient/unicode_builtin.c @@ -0,0 +1,435 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#include +#include + +#include "ntlm.h" +#include "unicode.h" +#include "compat.h" +#include "util.h" + +typedef unsigned int UTF32; /* at least 32 bits */ +typedef unsigned short UTF16; /* at least 16 bits */ +typedef unsigned char UTF8; /* typically 8 bits */ + +/* Some fundamental constants */ +#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD +#define UNI_MAX_BMP (UTF32)0x0000FFFF +#define UNI_MAX_UTF16 (UTF32)0x0010FFFF +#define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF +#define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF + +#define UNI_MAX_UTF8_BYTES_PER_CODE_POINT 4 + +typedef enum { + conversionOK, /* conversion successful */ + sourceExhausted, /* partial character in source, but hit end */ + targetExhausted, /* insuff. room in target for conversion */ + sourceIllegal /* source sequence is illegal/malformed */ +} ConversionResult; + +typedef enum { + strictConversion = 0, + lenientConversion +} ConversionFlags; + + +static const int halfShift = 10; /* used for shifting by 10 bits */ + +static const UTF32 halfBase = 0x0010000UL; +static const UTF32 halfMask = 0x3FFUL; + +#define UNI_SUR_HIGH_START (UTF32)0xD800 +#define UNI_SUR_HIGH_END (UTF32)0xDBFF +#define UNI_SUR_LOW_START (UTF32)0xDC00 +#define UNI_SUR_LOW_END (UTF32)0xDFFF +#define false 0 +#define true 1 + +/* --------------------------------------------------------------------- */ + +/* + * Index into the table below with the first byte of a UTF-8 sequence to + * get the number of trailing bytes that are supposed to follow it. + * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is + * left as-is for anyone who may want to do such conversion, which was + * allowed in earlier algorithms. + */ +static const char trailingBytesForUTF8[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5 +}; + +/* + * Magic values subtracted from a buffer value during UTF8 conversion. + * This table contains as many values as there might be trailing bytes + * in a UTF-8 sequence. + */ +static const UTF32 offsetsFromUTF8[6] = { + 0x00000000UL, 0x00003080UL, 0x000E2080UL, + 0x03C82080UL, 0xFA082080UL, 0x82082080UL }; + +/* + * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed + * into the first byte, depending on how many bytes follow. There are + * as many entries in this table as there are UTF-8 sequence types. + * (I.e., one byte sequence, two byte... etc.). Remember that sequencs + * for *legal* UTF-8 will be 4 or fewer bytes total. + */ +static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; + +/* --------------------------------------------------------------------- */ + +/* The interface converts a whole buffer to avoid function-call overhead. + * Constants have been gathered. Loops & conditionals have been removed as + * much as possible for efficiency, in favor of drop-through switches. + * (See "Note A" at the bottom of the file for equivalent code.) + * If your compiler supports it, the "isLegalUTF8" call can be turned + * into an inline function. + */ + +static ConversionResult ConvertUTF16toUTF8 ( + const UTF16** sourceStart, const UTF16* sourceEnd, + UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) { + ConversionResult result = conversionOK; + const UTF16* source = *sourceStart; + UTF8* target = *targetStart; + while (source < sourceEnd) { + UTF32 ch; + unsigned short bytesToWrite = 0; + const UTF32 byteMask = 0xBF; + const UTF32 byteMark = 0x80; + const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */ + ch = *source++; + /* If we have a surrogate pair, convert to UTF32 first. */ + if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) { + /* If the 16 bits following the high surrogate are in the source buffer... */ + if (source < sourceEnd) { + UTF32 ch2 = *source; + /* If it's a low surrogate, convert to UTF32. */ + if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) { + ch = ((ch - UNI_SUR_HIGH_START) << halfShift) + + (ch2 - UNI_SUR_LOW_START) + halfBase; + ++source; + } else if (flags == strictConversion) { /* it's an unpaired high surrogate */ + --source; /* return to the illegal value itself */ + result = sourceIllegal; + break; + } + } else { /* We don't have the 16 bits following the high surrogate. */ + --source; /* return to the high surrogate */ + result = sourceExhausted; + break; + } + } else if (flags == strictConversion) { + /* UTF-16 surrogate values are illegal in UTF-32 */ + if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) { + --source; /* return to the illegal value itself */ + result = sourceIllegal; + break; + } + } + /* Figure out how many bytes the result will require */ + if (ch < (UTF32)0x80) { bytesToWrite = 1; + } else if (ch < (UTF32)0x800) { bytesToWrite = 2; + } else if (ch < (UTF32)0x10000) { bytesToWrite = 3; + } else if (ch < (UTF32)0x110000) { bytesToWrite = 4; + } else { bytesToWrite = 3; + ch = UNI_REPLACEMENT_CHAR; + } + + target += bytesToWrite; + if (target > targetEnd) { + source = oldSource; /* Back up source pointer! */ + target -= bytesToWrite; result = targetExhausted; break; + } + switch (bytesToWrite) { /* note: everything falls through. */ + case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 1: *--target = (UTF8)(ch | firstByteMark[bytesToWrite]); + } + target += bytesToWrite; + } + *sourceStart = source; + *targetStart = target; + return result; +} + +/* --------------------------------------------------------------------- */ + +/* + * Utility routine to tell whether a sequence of bytes is legal UTF-8. + * This must be called with the length pre-determined by the first byte. + * If not calling this from ConvertUTF8to*, then the length can be set by: + * length = trailingBytesForUTF8[*source]+1; + * and the sequence is illegal right away if there aren't that many bytes + * available. + * If presented with a length > 4, this returns false. The Unicode + * definition of UTF-8 goes up to 4-byte sequences. + */ + +NTLM_INLINE(bool) isLegalUTF8(const UTF8 *source, int length) { + UTF8 a; + const UTF8 *srcptr = source+length; + switch (length) { + default: return false; + /* Everything else falls through when "true"... */ + case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false; + case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false; + case 2: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false; + + switch (*source) { + /* no fall-through in this inner switch */ + case 0xE0: if (a < 0xA0) return false; break; + case 0xED: if (a > 0x9F) return false; break; + case 0xF0: if (a < 0x90) return false; break; + case 0xF4: if (a > 0x8F) return false; break; + default: if (a < 0x80) return false; + } + + case 1: if (*source >= 0x80 && *source < 0xC2) return false; + } + if (*source > 0xF4) return false; + return true; +} + +static ConversionResult ConvertUTF8toUTF16 ( + const UTF8** sourceStart, const UTF8* sourceEnd, + UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) { + ConversionResult result = conversionOK; + const UTF8* source = *sourceStart; + UTF16* target = *targetStart; + while (source < sourceEnd) { + UTF32 ch = 0; + unsigned short extraBytesToRead = trailingBytesForUTF8[*source]; + if (extraBytesToRead >= sourceEnd - source) { + result = sourceExhausted; break; + } + /* Do this check whether lenient or strict */ + if (!isLegalUTF8(source, extraBytesToRead+1)) { + result = sourceIllegal; + break; + } + /* + * The cases all fall through. See "Note A" below. + */ + switch (extraBytesToRead) { + case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */ + case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */ + case 3: ch += *source++; ch <<= 6; + case 2: ch += *source++; ch <<= 6; + case 1: ch += *source++; ch <<= 6; + case 0: ch += *source++; + } + ch -= offsetsFromUTF8[extraBytesToRead]; + + if (target >= targetEnd) { + source -= (extraBytesToRead+1); /* Back up source pointer! */ + result = targetExhausted; break; + } + if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */ + /* UTF-16 surrogate values are illegal in UTF-32 */ + if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { + if (flags == strictConversion) { + source -= (extraBytesToRead+1); /* return to the illegal value itself */ + result = sourceIllegal; + break; + } else { + *target++ = UNI_REPLACEMENT_CHAR; + } + } else { + *target++ = (UTF16)ch; /* normal case */ + } + } else if (ch > UNI_MAX_UTF16) { + if (flags == strictConversion) { + result = sourceIllegal; + source -= (extraBytesToRead+1); /* return to the start */ + break; /* Bail out; shouldn't continue */ + } else { + *target++ = UNI_REPLACEMENT_CHAR; + } + } else { + /* target is a character in range 0xFFFF - 0x10FFFF. */ + if (target + 1 >= targetEnd) { + source -= (extraBytesToRead+1); /* Back up source pointer! */ + result = targetExhausted; break; + } + ch -= halfBase; + *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START); + *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START); + } + } + *sourceStart = source; + *targetStart = target; + return result; +} + + +bool ntlm_unicode_init(ntlm_client *ntlm) +{ + NTLM_UNUSED(ntlm); + return true; +} + +typedef enum { + unicode_builtin_utf8_to_16, + unicode_builtin_utf16_to_8 +} unicode_builtin_encoding_direction; + +NTLM_INLINE(bool) unicode_builtin_encoding_convert( + char **converted, + size_t *converted_len, + ntlm_client *ntlm, + const char *string, + size_t string_len, + unicode_builtin_encoding_direction direction) +{ + const char *in_start, *in_end; + char *out, *out_start, *out_end, *new_out; + size_t out_size, out_len; + bool success = false; + ConversionResult result; + + *converted = NULL; + *converted_len = 0; + + in_start = string; + in_end = in_start + string_len; + + /* + * When translating UTF8 to UTF16, these strings are only used + * internally, and we obey the given length, so we can simply + * use a buffer that is 2x the size. Add an extra byte to NUL + * terminate the results (two bytes for UTF16). + */ + if (direction == unicode_builtin_utf8_to_16) + out_size = (string_len * 2 + 2); + else + out_size = (string_len / 2 + 1); + + /* Round to the nearest multiple of 8 */ + out_size = (out_size + 7) & ~7; + + if ((out = malloc(out_size)) == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + return false; + } + + out_start = out; + out_end = out_start + out_size; + + /* Make room for NUL termination */ + if (direction == unicode_builtin_utf16_to_8) + out_end--; + + while (true) { + if (direction == unicode_builtin_utf8_to_16) + result = ConvertUTF8toUTF16( + (const UTF8 **)&in_start, (UTF8 *)in_end, + (UTF16 **)&out_start, (UTF16 *)out_end, strictConversion); + else + result = ConvertUTF16toUTF8( + (const UTF16 **)&in_start, (UTF16 *)in_end, + (UTF8 **)&out_start, (UTF8 *)out_end, lenientConversion); + + switch (result) { + case conversionOK: + success = true; + goto done; + case sourceExhausted: + ntlm_client_set_errmsg(ntlm, + "invalid unicode string; trailing data remains"); + goto done; + case targetExhausted: + break; + case sourceIllegal: + ntlm_client_set_errmsg(ntlm, + "invalid unicode string; trailing data remains"); + goto done; + default: + ntlm_client_set_errmsg(ntlm, + "unknown unicode conversion failure"); + goto done; + } + + /* Grow buffer size by 1.5 (rounded up to a multiple of 8) */ + out_size = ((((out_size << 1) - (out_size >> 1)) + 7) & ~7); + + if (out_size > NTLM_UNICODE_MAX_LEN) { + ntlm_client_set_errmsg(ntlm, "unicode conversion too large"); + goto done; + } + + out_len = out_start - out; + + if ((new_out = realloc(out, out_size)) == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + goto done; + } + + out = new_out; + out_start = new_out + out_len; + out_end = out + out_size; + + /* Make room for NUL termination */ + out_end -= (direction == unicode_builtin_utf8_to_16) ? 2 : 1; + } + +done: + if (!success) { + free(out); + return false; + } + + out_len = (out_start - out); + + /* NUL terminate */ + out[out_len] = '\0'; + + if (direction == unicode_builtin_utf8_to_16) + out[out_len+1] = '\0'; + + *converted = out; + *converted_len = out_len; + return true; +} + +bool ntlm_unicode_utf8_to_16( + char **converted, + size_t *converted_len, + ntlm_client *client, + const char *string, + size_t string_len) +{ + return unicode_builtin_encoding_convert(converted, converted_len, + client, string, string_len, unicode_builtin_utf8_to_16); +} + +bool ntlm_unicode_utf16_to_8( + char **converted, + size_t *converted_len, + ntlm_client *client, + const char *string, + size_t string_len) +{ + return unicode_builtin_encoding_convert(converted, converted_len, + client, string, string_len, unicode_builtin_utf16_to_8); +} + +void ntlm_unicode_shutdown(ntlm_client *ntlm) +{ + NTLM_UNUSED(ntlm); +} diff --git a/deps/ntlmclient/unicode_builtin.h b/deps/ntlmclient/unicode_builtin.h new file mode 100644 index 00000000000..eabec40bfdc --- /dev/null +++ b/deps/ntlmclient/unicode_builtin.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#ifndef PRIVATE_UNICODE_BUILTIN_H__ +#define PRIVATE_UNICODE_BUILTIN_H__ + +#include +#include + +#include "ntlmclient.h" + +struct ntlm_unicode_ctx { +}; + +#endif /* PRIVATE_UNICODE_BUILTIN_H__ */ diff --git a/deps/ntlmclient/unicode_iconv.c b/deps/ntlmclient/unicode_iconv.c new file mode 100644 index 00000000000..ac53638bf86 --- /dev/null +++ b/deps/ntlmclient/unicode_iconv.c @@ -0,0 +1,178 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#include +#include +#include +#include + +#include "ntlmclient.h" +#include "unicode.h" +#include "ntlm.h" +#include "util.h" +#include "compat.h" + +typedef enum { + unicode_iconv_utf8_to_16, + unicode_iconv_utf16_to_8 +} unicode_iconv_encoding_direction; + +bool ntlm_unicode_init(ntlm_client *ntlm) +{ + ntlm->unicode_ctx.utf8_to_16 = iconv_open("UTF-16LE", "UTF-8"); + ntlm->unicode_ctx.utf16_to_8 = iconv_open("UTF-8", "UTF-16LE"); + + if (ntlm->unicode_ctx.utf8_to_16 == (iconv_t)-1 || + ntlm->unicode_ctx.utf16_to_8 == (iconv_t)-1) { + if (errno == EINVAL) + ntlm_client_set_errmsg(ntlm, + "iconv does not support UTF8 <-> UTF16 conversion"); + else + ntlm_client_set_errmsg(ntlm, strerror(errno)); + + return false; + } + + return true; +} + +NTLM_INLINE(bool) unicode_iconv_encoding_convert( + char **converted, + size_t *converted_len, + ntlm_client *ntlm, + const char *string, + size_t string_len, + unicode_iconv_encoding_direction direction) +{ + char *in_start, *out_start, *out, *new_out; + size_t in_start_len, out_start_len, out_size, nul_size, ret, written = 0; + iconv_t converter; + + *converted = NULL; + *converted_len = 0; + + /* + * When translating UTF8 to UTF16, these strings are only used + * internally, and we obey the given length, so we can simply + * use a buffer that is 2x the size. When translating from UTF16 + * to UTF8, we may need to return to callers, so we need to NUL + * terminate and expect an extra byte for UTF8, two for UTF16. + */ + if (direction == unicode_iconv_utf8_to_16) { + converter = ntlm->unicode_ctx.utf8_to_16; + out_size = (string_len * 2) + 2; + nul_size = 2; + } else { + converter = ntlm->unicode_ctx.utf16_to_8; + out_size = (string_len / 2) + 1; + nul_size = 1; + } + + /* Round to the nearest multiple of 8 */ + out_size = (out_size + 7) & ~7; + + if ((out = malloc(out_size)) == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + return false; + } + + in_start = (char *)string; + in_start_len = string_len; + + while (true) { + out_start = out + written; + out_start_len = (out_size - nul_size) - written; + + ret = iconv(converter, &in_start, &in_start_len, &out_start, &out_start_len); + written = (out_size - nul_size) - out_start_len; + + if (ret == 0) + break; + + if (ret == (size_t)-1 && errno != E2BIG) { + ntlm_client_set_errmsg(ntlm, strerror(errno)); + goto on_error; + } + + /* Grow buffer size by 1.5 (rounded up to a multiple of 8) */ + out_size = ((((out_size << 1) - (out_size >> 1)) + 7) & ~7); + + if (out_size > NTLM_UNICODE_MAX_LEN) { + ntlm_client_set_errmsg(ntlm, "unicode conversion too large"); + goto on_error; + } + + if ((new_out = realloc(out, out_size)) == NULL) { + ntlm_client_set_errmsg(ntlm, "out of memory"); + goto on_error; + } + + out = new_out; + } + + if (in_start_len != 0) { + ntlm_client_set_errmsg(ntlm, + "invalid unicode string; trailing data remains"); + goto on_error; + } + + /* NUL terminate */ + out[written] = '\0'; + + if (direction == unicode_iconv_utf8_to_16) + out[written + 1] = '\0'; + + *converted = out; + + if (converted_len) + *converted_len = written; + + return true; + +on_error: + free(out); + return false; +} + +bool ntlm_unicode_utf8_to_16( + char **converted, + size_t *converted_len, + ntlm_client *ntlm, + const char *string, + size_t string_len) +{ + return unicode_iconv_encoding_convert( + converted, converted_len, ntlm, string, string_len, + unicode_iconv_utf8_to_16); +} + +bool ntlm_unicode_utf16_to_8( + char **converted, + size_t *converted_len, + ntlm_client *ntlm, + const char *string, + size_t string_len) +{ + return unicode_iconv_encoding_convert( + converted, converted_len, ntlm, string, string_len, + unicode_iconv_utf16_to_8); +} + +void ntlm_unicode_shutdown(ntlm_client *ntlm) +{ + if (ntlm->unicode_ctx.utf16_to_8 != (iconv_t)0 && + ntlm->unicode_ctx.utf16_to_8 != (iconv_t)-1) + iconv_close(ntlm->unicode_ctx.utf16_to_8); + + if (ntlm->unicode_ctx.utf8_to_16 != (iconv_t)0 && + ntlm->unicode_ctx.utf8_to_16 != (iconv_t)-1) + iconv_close(ntlm->unicode_ctx.utf8_to_16); + + ntlm->unicode_ctx.utf8_to_16 = (iconv_t)-1; + ntlm->unicode_ctx.utf16_to_8 = (iconv_t)-1; +} diff --git a/deps/ntlmclient/unicode_iconv.h b/deps/ntlmclient/unicode_iconv.h new file mode 100644 index 00000000000..87a96a67d87 --- /dev/null +++ b/deps/ntlmclient/unicode_iconv.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#ifndef PRIVATE_UNICODE_ICONV_H__ +#define PRIVATE_UNICODE_ICONV_H__ + +#include +#include + +#include "ntlmclient.h" + +struct ntlm_unicode_ctx { + iconv_t utf8_to_16; + iconv_t utf16_to_8; +}; + +#endif /* PRIVATE_UNICODE_ICONV_H__ */ diff --git a/deps/ntlmclient/utf8.h b/deps/ntlmclient/utf8.h new file mode 100644 index 00000000000..495e259db30 --- /dev/null +++ b/deps/ntlmclient/utf8.h @@ -0,0 +1,1712 @@ +/* The latest version of this library is available on GitHub; + * https://github.com/sheredom/utf8.h */ + +/* This is free and unencumbered software released into the public domain. + * + * Anyone is free to copy, modify, publish, use, compile, sell, or + * distribute this software, either in source code form or as a compiled + * binary, for any purpose, commercial or non-commercial, and by any + * means. + * + * In jurisdictions that recognize copyright laws, the author or authors + * of this software dedicate any and all copyright interest in the + * software to the public domain. We make this dedication for the benefit + * of the public at large and to the detriment of our heirs and + * successors. We intend this dedication to be an overt act of + * relinquishment in perpetuity of all present and future rights to this + * software under copyright law. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * For more information, please refer to */ + +#ifndef SHEREDOM_UTF8_H_INCLUDED +#define SHEREDOM_UTF8_H_INCLUDED + +#if defined(_MSC_VER) +#pragma warning(push) + +/* disable warning: no function prototype given: converting '()' to '(void)' */ +#pragma warning(disable : 4255) + +/* disable warning: '__cplusplus' is not defined as a preprocessor macro, + * replacing with '0' for '#if/#elif' */ +#pragma warning(disable : 4668) + +/* disable warning: bytes padding added after construct */ +#pragma warning(disable : 4820) +#endif + +#if defined(__cplusplus) +#if defined(_MSC_VER) +#define utf8_cplusplus _MSVC_LANG +#else +#define utf8_cplusplus __cplusplus +#endif +#endif + +#include +#include + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif + +#if defined(_MSC_VER) && (_MSC_VER < 1920) +typedef __int32 utf8_int32_t; +#else +#include +typedef int32_t utf8_int32_t; +#endif + +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wold-style-cast" +#pragma clang diagnostic ignored "-Wcast-qual" + +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + +#ifdef utf8_cplusplus +extern "C" { +#endif + +#if defined(__TINYC__) +#define UTF8_ATTRIBUTE(a) __attribute((a)) +#else +#define UTF8_ATTRIBUTE(a) __attribute__((a)) +#endif + +#if defined(_MSC_VER) +#define utf8_nonnull +#define utf8_pure +#define utf8_restrict __restrict +#define utf8_weak __inline +#elif defined(__clang__) || defined(__GNUC__) +#define utf8_nonnull UTF8_ATTRIBUTE(nonnull) +#define utf8_pure UTF8_ATTRIBUTE(pure) +#define utf8_restrict __restrict__ +#define utf8_weak UTF8_ATTRIBUTE(weak) +#elif defined(__TINYC__) +#define utf8_nonnull UTF8_ATTRIBUTE(nonnull) +#define utf8_pure UTF8_ATTRIBUTE(pure) +#define utf8_restrict +#define utf8_weak UTF8_ATTRIBUTE(weak) +#else +#error Non clang, non gcc, non MSVC, non tcc compiler found! +#endif + +#ifdef utf8_cplusplus +#define utf8_null NULL +#else +#define utf8_null 0 +#endif + +#if defined(utf8_cplusplus) && utf8_cplusplus >= 201402L && (!defined(_MSC_VER) || (defined(_MSC_VER) && _MSC_VER >= 1910)) +#define utf8_constexpr14 constexpr +#define utf8_constexpr14_impl constexpr +#else +/* constexpr and weak are incompatible. so only enable one of them */ +#define utf8_constexpr14 utf8_weak +#define utf8_constexpr14_impl +#endif + +#if defined(utf8_cplusplus) && utf8_cplusplus >= 202002L +using utf8_int8_t = char8_t; /* Introduced in C++20 */ +#else +typedef char utf8_int8_t; +#endif + +/* Return less than 0, 0, greater than 0 if src1 < src2, src1 == src2, src1 > + * src2 respectively, case insensitive. */ +utf8_constexpr14 utf8_nonnull utf8_pure int +utf8casecmp(const utf8_int8_t *src1, const utf8_int8_t *src2); + +/* Append the utf8 string src onto the utf8 string dst. */ +utf8_nonnull utf8_weak utf8_int8_t * +utf8cat(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src); + +/* Find the first match of the utf8 codepoint chr in the utf8 string src. */ +utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * +utf8chr(const utf8_int8_t *src, utf8_int32_t chr); + +/* Return less than 0, 0, greater than 0 if src1 < src2, + * src1 == src2, src1 > src2 respectively. */ +utf8_constexpr14 utf8_nonnull utf8_pure int utf8cmp(const utf8_int8_t *src1, + const utf8_int8_t *src2); + +/* Copy the utf8 string src onto the memory allocated in dst. */ +utf8_nonnull utf8_weak utf8_int8_t * +utf8cpy(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src); + +/* Number of utf8 codepoints in the utf8 string src that consists entirely + * of utf8 codepoints not from the utf8 string reject. */ +utf8_constexpr14 utf8_nonnull utf8_pure size_t +utf8cspn(const utf8_int8_t *src, const utf8_int8_t *reject); + +/* Duplicate the utf8 string src by getting its size, malloc'ing a new buffer + * copying over the data, and returning that. Or 0 if malloc failed. */ +utf8_weak utf8_int8_t *utf8dup(const utf8_int8_t *src); + +/* Number of utf8 codepoints in the utf8 string str, + * excluding the null terminating byte. */ +utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8len(const utf8_int8_t *str); + +/* Similar to utf8len, except that only at most n bytes of src are looked. */ +utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8nlen(const utf8_int8_t *str, + size_t n); + +/* Return less than 0, 0, greater than 0 if src1 < src2, src1 == src2, src1 > + * src2 respectively, case insensitive. Checking at most n bytes of each utf8 + * string. */ +utf8_constexpr14 utf8_nonnull utf8_pure int +utf8ncasecmp(const utf8_int8_t *src1, const utf8_int8_t *src2, size_t n); + +/* Append the utf8 string src onto the utf8 string dst, + * writing at most n+1 bytes. Can produce an invalid utf8 + * string if n falls partway through a utf8 codepoint. */ +utf8_nonnull utf8_weak utf8_int8_t * +utf8ncat(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src, + size_t n); + +/* Return less than 0, 0, greater than 0 if src1 < src2, + * src1 == src2, src1 > src2 respectively. Checking at most n + * bytes of each utf8 string. */ +utf8_constexpr14 utf8_nonnull utf8_pure int +utf8ncmp(const utf8_int8_t *src1, const utf8_int8_t *src2, size_t n); + +/* Copy the utf8 string src onto the memory allocated in dst. + * Copies at most n bytes. If n falls partway through a utf8 + * codepoint, or if dst doesn't have enough room for a null + * terminator, the final string will be cut short to preserve + * utf8 validity. */ + +utf8_nonnull utf8_weak utf8_int8_t * +utf8ncpy(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src, + size_t n); + +/* Similar to utf8dup, except that at most n bytes of src are copied. If src is + * longer than n, only n bytes are copied and a null byte is added. + * + * Returns a new string if successful, 0 otherwise */ +utf8_weak utf8_int8_t *utf8ndup(const utf8_int8_t *src, size_t n); + +/* Locates the first occurrence in the utf8 string str of any byte in the + * utf8 string accept, or 0 if no match was found. */ +utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * +utf8pbrk(const utf8_int8_t *str, const utf8_int8_t *accept); + +/* Find the last match of the utf8 codepoint chr in the utf8 string src. */ +utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * +utf8rchr(const utf8_int8_t *src, int chr); + +/* Number of bytes in the utf8 string str, + * including the null terminating byte. */ +utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8size(const utf8_int8_t *str); + +/* Similar to utf8size, except that the null terminating byte is excluded. */ +utf8_constexpr14 utf8_nonnull utf8_pure size_t +utf8size_lazy(const utf8_int8_t *str); + +/* Similar to utf8size, except that only at most n bytes of src are looked and + * the null terminating byte is excluded. */ +utf8_constexpr14 utf8_nonnull utf8_pure size_t +utf8nsize_lazy(const utf8_int8_t *str, size_t n); + +/* Number of utf8 codepoints in the utf8 string src that consists entirely + * of utf8 codepoints from the utf8 string accept. */ +utf8_constexpr14 utf8_nonnull utf8_pure size_t +utf8spn(const utf8_int8_t *src, const utf8_int8_t *accept); + +/* The position of the utf8 string needle in the utf8 string haystack. */ +utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * +utf8str(const utf8_int8_t *haystack, const utf8_int8_t *needle); + +/* The position of the utf8 string needle in the utf8 string haystack, case + * insensitive. */ +utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * +utf8casestr(const utf8_int8_t *haystack, const utf8_int8_t *needle); + +/* Return 0 on success, or the position of the invalid + * utf8 codepoint on failure. */ +utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * +utf8valid(const utf8_int8_t *str); + +/* Similar to utf8valid, except that only at most n bytes of src are looked. */ +utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * +utf8nvalid(const utf8_int8_t *str, size_t n); + +/* Given a null-terminated string, makes the string valid by replacing invalid + * codepoints with a 1-byte replacement. Returns 0 on success. */ +utf8_nonnull utf8_weak int utf8makevalid(utf8_int8_t *str, + const utf8_int32_t replacement); + +/* Sets out_codepoint to the current utf8 codepoint in str, and returns the + * address of the next utf8 codepoint after the current one in str. */ +utf8_constexpr14 utf8_nonnull utf8_int8_t * +utf8codepoint(const utf8_int8_t *utf8_restrict str, + utf8_int32_t *utf8_restrict out_codepoint); + +/* Calculates the size of the next utf8 codepoint in str. */ +utf8_constexpr14 utf8_nonnull size_t +utf8codepointcalcsize(const utf8_int8_t *str); + +/* Returns the size of the given codepoint in bytes. */ +utf8_constexpr14 size_t utf8codepointsize(utf8_int32_t chr); + +/* Write a codepoint to the given string, and return the address to the next + * place after the written codepoint. Pass how many bytes left in the buffer to + * n. If there is not enough space for the codepoint, this function returns + * null. */ +utf8_nonnull utf8_weak utf8_int8_t * +utf8catcodepoint(utf8_int8_t *str, utf8_int32_t chr, size_t n); + +/* Returns 1 if the given character is lowercase, or 0 if it is not. */ +utf8_constexpr14 int utf8islower(utf8_int32_t chr); + +/* Returns 1 if the given character is uppercase, or 0 if it is not. */ +utf8_constexpr14 int utf8isupper(utf8_int32_t chr); + +/* Transform the given string into all lowercase codepoints. */ +utf8_nonnull utf8_weak void utf8lwr(utf8_int8_t *utf8_restrict str); + +/* Transform the given string into all uppercase codepoints. */ +utf8_nonnull utf8_weak void utf8upr(utf8_int8_t *utf8_restrict str); + +/* Make a codepoint lower case if possible. */ +utf8_constexpr14 utf8_int32_t utf8lwrcodepoint(utf8_int32_t cp); + +/* Make a codepoint upper case if possible. */ +utf8_constexpr14 utf8_int32_t utf8uprcodepoint(utf8_int32_t cp); + +/* Sets out_codepoint to the current utf8 codepoint in str, and returns the + * address of the previous utf8 codepoint before the current one in str. */ +utf8_constexpr14 utf8_nonnull utf8_int8_t * +utf8rcodepoint(const utf8_int8_t *utf8_restrict str, + utf8_int32_t *utf8_restrict out_codepoint); + +/* Duplicate the utf8 string src by getting its size, calling alloc_func_ptr to + * copy over data to a new buffer, and returning that. Or 0 if alloc_func_ptr + * returned null. */ +utf8_weak utf8_int8_t *utf8dup_ex(const utf8_int8_t *src, + utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *, + size_t), + utf8_int8_t *user_data); + +/* Similar to utf8dup, except that at most n bytes of src are copied. If src is + * longer than n, only n bytes are copied and a null byte is added. + * + * Returns a new string if successful, 0 otherwise. */ +utf8_weak utf8_int8_t *utf8ndup_ex(const utf8_int8_t *src, size_t n, + utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *, + size_t), + utf8_int8_t *user_data); + +#undef utf8_weak +#undef utf8_pure +#undef utf8_nonnull + +utf8_constexpr14_impl int utf8casecmp(const utf8_int8_t *src1, + const utf8_int8_t *src2) { + utf8_int32_t src1_lwr_cp = 0, src2_lwr_cp = 0, src1_upr_cp = 0, + src2_upr_cp = 0, src1_orig_cp = 0, src2_orig_cp = 0; + + for (;;) { + src1 = utf8codepoint(src1, &src1_orig_cp); + src2 = utf8codepoint(src2, &src2_orig_cp); + + /* lower the srcs if required */ + src1_lwr_cp = utf8lwrcodepoint(src1_orig_cp); + src2_lwr_cp = utf8lwrcodepoint(src2_orig_cp); + + /* lower the srcs if required */ + src1_upr_cp = utf8uprcodepoint(src1_orig_cp); + src2_upr_cp = utf8uprcodepoint(src2_orig_cp); + + /* check if the lowered codepoints match */ + if ((0 == src1_orig_cp) && (0 == src2_orig_cp)) { + return 0; + } else if ((src1_lwr_cp == src2_lwr_cp) || (src1_upr_cp == src2_upr_cp)) { + continue; + } + + /* if they don't match, then we return the difference between the characters + */ + return src1_lwr_cp - src2_lwr_cp; + } +} + +utf8_int8_t *utf8cat(utf8_int8_t *utf8_restrict dst, + const utf8_int8_t *utf8_restrict src) { + utf8_int8_t *d = dst; + /* find the null terminating byte in dst */ + while ('\0' != *d) { + d++; + } + + /* overwriting the null terminating byte in dst, append src byte-by-byte */ + while ('\0' != *src) { + *d++ = *src++; + } + + /* write out a new null terminating byte into dst */ + *d = '\0'; + + return dst; +} + +utf8_constexpr14_impl utf8_int8_t *utf8chr(const utf8_int8_t *src, + utf8_int32_t chr) { + utf8_int8_t c[5] = {'\0', '\0', '\0', '\0', '\0'}; + + if (0 == chr) { + /* being asked to return position of null terminating byte, so + * just run s to the end, and return! */ + while ('\0' != *src) { + src++; + } + return (utf8_int8_t *)src; + } else if (0 == ((utf8_int32_t)0xffffff80 & chr)) { + /* 1-byte/7-bit ascii + * (0b0xxxxxxx) */ + c[0] = (utf8_int8_t)chr; + } else if (0 == ((utf8_int32_t)0xfffff800 & chr)) { + /* 2-byte/11-bit utf8 code point + * (0b110xxxxx 0b10xxxxxx) */ + c[0] = (utf8_int8_t)(0xc0 | (utf8_int8_t)(chr >> 6)); + c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); + } else if (0 == ((utf8_int32_t)0xffff0000 & chr)) { + /* 3-byte/16-bit utf8 code point + * (0b1110xxxx 0b10xxxxxx 0b10xxxxxx) */ + c[0] = (utf8_int8_t)(0xe0 | (utf8_int8_t)(chr >> 12)); + c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f)); + c[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); + } else { /* if (0 == ((int)0xffe00000 & chr)) { */ + /* 4-byte/21-bit utf8 code point + * (0b11110xxx 0b10xxxxxx 0b10xxxxxx 0b10xxxxxx) */ + c[0] = (utf8_int8_t)(0xf0 | (utf8_int8_t)(chr >> 18)); + c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 12) & 0x3f)); + c[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f)); + c[3] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); + } + + /* we've made c into a 2 utf8 codepoint string, one for the chr we are + * seeking, another for the null terminating byte. Now use utf8str to + * search */ + return utf8str(src, c); +} + +utf8_constexpr14_impl int utf8cmp(const utf8_int8_t *src1, + const utf8_int8_t *src2) { + while (('\0' != *src1) || ('\0' != *src2)) { + if (*src1 < *src2) { + return -1; + } else if (*src1 > *src2) { + return 1; + } + + src1++; + src2++; + } + + /* both utf8 strings matched */ + return 0; +} + +utf8_constexpr14_impl int utf8coll(const utf8_int8_t *src1, + const utf8_int8_t *src2); + +utf8_int8_t *utf8cpy(utf8_int8_t *utf8_restrict dst, + const utf8_int8_t *utf8_restrict src) { + utf8_int8_t *d = dst; + + /* overwriting anything previously in dst, write byte-by-byte + * from src */ + while ('\0' != *src) { + *d++ = *src++; + } + + /* append null terminating byte */ + *d = '\0'; + + return dst; +} + +utf8_constexpr14_impl size_t utf8cspn(const utf8_int8_t *src, + const utf8_int8_t *reject) { + size_t chars = 0; + + while ('\0' != *src) { + const utf8_int8_t *r = reject; + size_t offset = 0; + + while ('\0' != *r) { + /* checking that if *r is the start of a utf8 codepoint + * (it is not 0b10xxxxxx) and we have successfully matched + * a previous character (0 < offset) - we found a match */ + if ((0x80 != (0xc0 & *r)) && (0 < offset)) { + return chars; + } else { + if (*r == src[offset]) { + /* part of a utf8 codepoint matched, so move our checking + * onwards to the next byte */ + offset++; + r++; + } else { + /* r could be in the middle of an unmatching utf8 code point, + * so we need to march it on to the next character beginning, */ + + do { + r++; + } while (0x80 == (0xc0 & *r)); + + /* reset offset too as we found a mismatch */ + offset = 0; + } + } + } + + /* found a match at the end of *r, so didn't get a chance to test it */ + if (0 < offset) { + return chars; + } + + /* the current utf8 codepoint in src did not match reject, but src + * could have been partway through a utf8 codepoint, so we need to + * march it onto the next utf8 codepoint starting byte */ + do { + src++; + } while ((0x80 == (0xc0 & *src))); + chars++; + } + + return chars; +} + +utf8_int8_t *utf8dup(const utf8_int8_t *src) { + return utf8dup_ex(src, utf8_null, utf8_null); +} + +utf8_int8_t *utf8dup_ex(const utf8_int8_t *src, + utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *, size_t), + utf8_int8_t *user_data) { + utf8_int8_t *n = utf8_null; + + /* figure out how many bytes (including the terminator) we need to copy first + */ + size_t bytes = utf8size(src); + + if (alloc_func_ptr) { + n = alloc_func_ptr(user_data, bytes); + } else { +#if !defined(UTF8_NO_STD_MALLOC) + n = (utf8_int8_t *)malloc(bytes); +#else + return utf8_null; +#endif + } + + if (utf8_null == n) { + /* out of memory so we bail */ + return utf8_null; + } else { + bytes = 0; + + /* copy src byte-by-byte into our new utf8 string */ + while ('\0' != src[bytes]) { + n[bytes] = src[bytes]; + bytes++; + } + + /* append null terminating byte */ + n[bytes] = '\0'; + return n; + } +} + +utf8_constexpr14_impl utf8_int8_t *utf8fry(const utf8_int8_t *str); + +utf8_constexpr14_impl size_t utf8len(const utf8_int8_t *str) { + return utf8nlen(str, SIZE_MAX); +} + +utf8_constexpr14_impl size_t utf8nlen(const utf8_int8_t *str, size_t n) { + const utf8_int8_t *t = str; + size_t length = 0; + + while ((size_t)(str - t) < n && '\0' != *str) { + if (0xf0 == (0xf8 & *str)) { + /* 4-byte utf8 code point (began with 0b11110xxx) */ + str += 4; + } else if (0xe0 == (0xf0 & *str)) { + /* 3-byte utf8 code point (began with 0b1110xxxx) */ + str += 3; + } else if (0xc0 == (0xe0 & *str)) { + /* 2-byte utf8 code point (began with 0b110xxxxx) */ + str += 2; + } else { /* if (0x00 == (0x80 & *s)) { */ + /* 1-byte ascii (began with 0b0xxxxxxx) */ + str += 1; + } + + /* no matter the bytes we marched s forward by, it was + * only 1 utf8 codepoint */ + length++; + } + + if ((size_t)(str - t) > n) { + length--; + } + return length; +} + +utf8_constexpr14_impl int utf8ncasecmp(const utf8_int8_t *src1, + const utf8_int8_t *src2, size_t n) { + utf8_int32_t src1_lwr_cp = 0, src2_lwr_cp = 0, src1_upr_cp = 0, + src2_upr_cp = 0, src1_orig_cp = 0, src2_orig_cp = 0; + + do { + const utf8_int8_t *const s1 = src1; + const utf8_int8_t *const s2 = src2; + + /* first check that we have enough bytes left in n to contain an entire + * codepoint */ + if (0 == n) { + return 0; + } + + if ((1 == n) && ((0xc0 == (0xe0 & *s1)) || (0xc0 == (0xe0 & *s2)))) { + const utf8_int32_t c1 = (0xe0 & *s1); + const utf8_int32_t c2 = (0xe0 & *s2); + + if (c1 != c2) { + return c1 - c2; + } else { + return 0; + } + } + + if ((2 >= n) && ((0xe0 == (0xf0 & *s1)) || (0xe0 == (0xf0 & *s2)))) { + const utf8_int32_t c1 = (0xf0 & *s1); + const utf8_int32_t c2 = (0xf0 & *s2); + + if (c1 != c2) { + return c1 - c2; + } else { + return 0; + } + } + + if ((3 >= n) && ((0xf0 == (0xf8 & *s1)) || (0xf0 == (0xf8 & *s2)))) { + const utf8_int32_t c1 = (0xf8 & *s1); + const utf8_int32_t c2 = (0xf8 & *s2); + + if (c1 != c2) { + return c1 - c2; + } else { + return 0; + } + } + + src1 = utf8codepoint(src1, &src1_orig_cp); + src2 = utf8codepoint(src2, &src2_orig_cp); + n -= utf8codepointsize(src1_orig_cp); + + src1_lwr_cp = utf8lwrcodepoint(src1_orig_cp); + src2_lwr_cp = utf8lwrcodepoint(src2_orig_cp); + + src1_upr_cp = utf8uprcodepoint(src1_orig_cp); + src2_upr_cp = utf8uprcodepoint(src2_orig_cp); + + /* check if the lowered codepoints match */ + if ((0 == src1_orig_cp) && (0 == src2_orig_cp)) { + return 0; + } else if ((src1_lwr_cp == src2_lwr_cp) || (src1_upr_cp == src2_upr_cp)) { + continue; + } + + /* if they don't match, then we return the difference between the characters + */ + return src1_lwr_cp - src2_lwr_cp; + } while (0 < n); + + /* both utf8 strings matched */ + return 0; +} + +utf8_int8_t *utf8ncat(utf8_int8_t *utf8_restrict dst, + const utf8_int8_t *utf8_restrict src, size_t n) { + utf8_int8_t *d = dst; + + /* find the null terminating byte in dst */ + while ('\0' != *d) { + d++; + } + + /* overwriting the null terminating byte in dst, append src byte-by-byte + * stopping if we run out of space */ + while (('\0' != *src) && (0 != n--)) { + *d++ = *src++; + } + + /* write out a new null terminating byte into dst */ + *d = '\0'; + + return dst; +} + +utf8_constexpr14_impl int utf8ncmp(const utf8_int8_t *src1, + const utf8_int8_t *src2, size_t n) { + while ((0 != n--) && (('\0' != *src1) || ('\0' != *src2))) { + if (*src1 < *src2) { + return -1; + } else if (*src1 > *src2) { + return 1; + } + + src1++; + src2++; + } + + /* both utf8 strings matched */ + return 0; +} + +utf8_int8_t *utf8ncpy(utf8_int8_t *utf8_restrict dst, + const utf8_int8_t *utf8_restrict src, size_t n) { + utf8_int8_t *d = dst; + size_t index = 0, check_index = 0; + + if (n == 0) { + return dst; + } + + /* overwriting anything previously in dst, write byte-by-byte + * from src */ + for (index = 0; index < n; index++) { + d[index] = src[index]; + if ('\0' == src[index]) { + break; + } + } + + for (check_index = index - 1; + check_index > 0 && 0x80 == (0xc0 & d[check_index]); check_index--) { + /* just moving the index */ + } + + if (check_index < index && + ((index - check_index) < utf8codepointcalcsize(&d[check_index]) || + (index - check_index) == n)) { + index = check_index; + } + + /* append null terminating byte */ + for (; index < n; index++) { + d[index] = 0; + } + + return dst; +} + +utf8_int8_t *utf8ndup(const utf8_int8_t *src, size_t n) { + return utf8ndup_ex(src, n, utf8_null, utf8_null); +} + +utf8_int8_t *utf8ndup_ex(const utf8_int8_t *src, size_t n, + utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *, size_t), + utf8_int8_t *user_data) { + utf8_int8_t *c = utf8_null; + size_t bytes = 0; + + /* Find the end of the string or stop when n is reached */ + while ('\0' != src[bytes] && bytes < n) { + bytes++; + } + + /* In case bytes is actually less than n, we need to set it + * to be used later in the copy byte by byte. */ + n = bytes; + + if (alloc_func_ptr) { + c = alloc_func_ptr(user_data, bytes + 1); + } else { +#if !defined(UTF8_NO_STD_MALLOC) + c = (utf8_int8_t *)malloc(bytes + 1); +#else + c = utf8_null; +#endif + } + + if (utf8_null == c) { + /* out of memory so we bail */ + return utf8_null; + } + + bytes = 0; + + /* copy src byte-by-byte into our new utf8 string */ + while ('\0' != src[bytes] && bytes < n) { + c[bytes] = src[bytes]; + bytes++; + } + + /* append null terminating byte */ + c[bytes] = '\0'; + return c; +} + +utf8_constexpr14_impl utf8_int8_t *utf8rchr(const utf8_int8_t *src, int chr) { + + utf8_int8_t *match = utf8_null; + utf8_int8_t c[5] = {'\0', '\0', '\0', '\0', '\0'}; + + if (0 == chr) { + /* being asked to return position of null terminating byte, so + * just run s to the end, and return! */ + while ('\0' != *src) { + src++; + } + return (utf8_int8_t *)src; + } else if (0 == ((int)0xffffff80 & chr)) { + /* 1-byte/7-bit ascii + * (0b0xxxxxxx) */ + c[0] = (utf8_int8_t)chr; + } else if (0 == ((int)0xfffff800 & chr)) { + /* 2-byte/11-bit utf8 code point + * (0b110xxxxx 0b10xxxxxx) */ + c[0] = (utf8_int8_t)(0xc0 | (utf8_int8_t)(chr >> 6)); + c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); + } else if (0 == ((int)0xffff0000 & chr)) { + /* 3-byte/16-bit utf8 code point + * (0b1110xxxx 0b10xxxxxx 0b10xxxxxx) */ + c[0] = (utf8_int8_t)(0xe0 | (utf8_int8_t)(chr >> 12)); + c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f)); + c[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); + } else { /* if (0 == ((int)0xffe00000 & chr)) { */ + /* 4-byte/21-bit utf8 code point + * (0b11110xxx 0b10xxxxxx 0b10xxxxxx 0b10xxxxxx) */ + c[0] = (utf8_int8_t)(0xf0 | (utf8_int8_t)(chr >> 18)); + c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 12) & 0x3f)); + c[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f)); + c[3] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); + } + + /* we've created a 2 utf8 codepoint string in c that is + * the utf8 character asked for by chr, and a null + * terminating byte */ + + while ('\0' != *src) { + size_t offset = 0; + + while ((src[offset] == c[offset]) && ('\0' != src[offset])) { + offset++; + } + + if ('\0' == c[offset]) { + /* we found a matching utf8 code point */ + match = (utf8_int8_t *)src; + src += offset; + + if ('\0' == *src) { + break; + } + } else { + src += offset; + + /* need to march s along to next utf8 codepoint start + * (the next byte that doesn't match 0b10xxxxxx) */ + if ('\0' != *src) { + do { + src++; + } while (0x80 == (0xc0 & *src)); + } + } + } + + /* return the last match we found (or 0 if no match was found) */ + return match; +} + +utf8_constexpr14_impl utf8_int8_t *utf8pbrk(const utf8_int8_t *str, + const utf8_int8_t *accept) { + while ('\0' != *str) { + const utf8_int8_t *a = accept; + size_t offset = 0; + + while ('\0' != *a) { + /* checking that if *a is the start of a utf8 codepoint + * (it is not 0b10xxxxxx) and we have successfully matched + * a previous character (0 < offset) - we found a match */ + if ((0x80 != (0xc0 & *a)) && (0 < offset)) { + return (utf8_int8_t *)str; + } else { + if (*a == str[offset]) { + /* part of a utf8 codepoint matched, so move our checking + * onwards to the next byte */ + offset++; + a++; + } else { + /* r could be in the middle of an unmatching utf8 code point, + * so we need to march it on to the next character beginning, */ + + do { + a++; + } while (0x80 == (0xc0 & *a)); + + /* reset offset too as we found a mismatch */ + offset = 0; + } + } + } + + /* we found a match on the last utf8 codepoint */ + if (0 < offset) { + return (utf8_int8_t *)str; + } + + /* the current utf8 codepoint in src did not match accept, but src + * could have been partway through a utf8 codepoint, so we need to + * march it onto the next utf8 codepoint starting byte */ + do { + str++; + } while ((0x80 == (0xc0 & *str))); + } + + return utf8_null; +} + +utf8_constexpr14_impl size_t utf8size(const utf8_int8_t *str) { + return utf8size_lazy(str) + 1; +} + +utf8_constexpr14_impl size_t utf8size_lazy(const utf8_int8_t *str) { + return utf8nsize_lazy(str, SIZE_MAX); +} + +utf8_constexpr14_impl size_t utf8nsize_lazy(const utf8_int8_t *str, size_t n) { + size_t size = 0; + while (size < n && '\0' != str[size]) { + size++; + } + return size; +} + +utf8_constexpr14_impl size_t utf8spn(const utf8_int8_t *src, + const utf8_int8_t *accept) { + size_t chars = 0; + + while ('\0' != *src) { + const utf8_int8_t *a = accept; + size_t offset = 0; + + while ('\0' != *a) { + /* checking that if *r is the start of a utf8 codepoint + * (it is not 0b10xxxxxx) and we have successfully matched + * a previous character (0 < offset) - we found a match */ + if ((0x80 != (0xc0 & *a)) && (0 < offset)) { + /* found a match, so increment the number of utf8 codepoints + * that have matched and stop checking whether any other utf8 + * codepoints in a match */ + chars++; + src += offset; + offset = 0; + break; + } else { + if (*a == src[offset]) { + offset++; + a++; + } else { + /* a could be in the middle of an unmatching utf8 codepoint, + * so we need to march it on to the next character beginning, */ + do { + a++; + } while (0x80 == (0xc0 & *a)); + + /* reset offset too as we found a mismatch */ + offset = 0; + } + } + } + + /* found a match at the end of *a, so didn't get a chance to test it */ + if (0 < offset) { + chars++; + src += offset; + continue; + } + + /* if a got to its terminating null byte, then we didn't find a match. + * Return the current number of matched utf8 codepoints */ + if ('\0' == *a) { + return chars; + } + } + + return chars; +} + +utf8_constexpr14_impl utf8_int8_t *utf8str(const utf8_int8_t *haystack, + const utf8_int8_t *needle) { + utf8_int32_t throwaway_codepoint = 0; + + /* if needle has no utf8 codepoints before the null terminating + * byte then return haystack */ + if ('\0' == *needle) { + return (utf8_int8_t *)haystack; + } + + while ('\0' != *haystack) { + const utf8_int8_t *maybeMatch = haystack; + const utf8_int8_t *n = needle; + + while (*haystack == *n && (*haystack != '\0' && *n != '\0')) { + n++; + haystack++; + } + + if ('\0' == *n) { + /* we found the whole utf8 string for needle in haystack at + * maybeMatch, so return it */ + return (utf8_int8_t *)maybeMatch; + } else { + /* h could be in the middle of an unmatching utf8 codepoint, + * so we need to march it on to the next character beginning + * starting from the current character */ + haystack = utf8codepoint(maybeMatch, &throwaway_codepoint); + } + } + + /* no match */ + return utf8_null; +} + +utf8_constexpr14_impl utf8_int8_t *utf8casestr(const utf8_int8_t *haystack, + const utf8_int8_t *needle) { + /* if needle has no utf8 codepoints before the null terminating + * byte then return haystack */ + if ('\0' == *needle) { + return (utf8_int8_t *)haystack; + } + + for (;;) { + const utf8_int8_t *maybeMatch = haystack; + const utf8_int8_t *n = needle; + utf8_int32_t h_cp = 0, n_cp = 0; + + /* Get the next code point and track it */ + const utf8_int8_t *nextH = haystack = utf8codepoint(haystack, &h_cp); + n = utf8codepoint(n, &n_cp); + + while ((0 != h_cp) && (0 != n_cp)) { + h_cp = utf8lwrcodepoint(h_cp); + n_cp = utf8lwrcodepoint(n_cp); + + /* if we find a mismatch, bail out! */ + if (h_cp != n_cp) { + break; + } + + haystack = utf8codepoint(haystack, &h_cp); + n = utf8codepoint(n, &n_cp); + } + + if (0 == n_cp) { + /* we found the whole utf8 string for needle in haystack at + * maybeMatch, so return it */ + return (utf8_int8_t *)maybeMatch; + } + + if (0 == h_cp) { + /* no match */ + return utf8_null; + } + + /* Roll back to the next code point in the haystack to test */ + haystack = nextH; + } +} + +utf8_constexpr14_impl utf8_int8_t *utf8valid(const utf8_int8_t *str) { + return utf8nvalid(str, SIZE_MAX); +} + +utf8_constexpr14_impl utf8_int8_t *utf8nvalid(const utf8_int8_t *str, + size_t n) { + const utf8_int8_t *t = str; + size_t consumed = 0; + + while ((void)(consumed = (size_t)(str - t)), consumed < n && '\0' != *str) { + const size_t remaining = n - consumed; + + if (0xf0 == (0xf8 & *str)) { + /* ensure that there's 4 bytes or more remaining */ + if (remaining < 4) { + return (utf8_int8_t *)str; + } + + /* ensure each of the 3 following bytes in this 4-byte + * utf8 codepoint began with 0b10xxxxxx */ + if ((0x80 != (0xc0 & str[1])) || (0x80 != (0xc0 & str[2])) || + (0x80 != (0xc0 & str[3]))) { + return (utf8_int8_t *)str; + } + + /* ensure that our utf8 codepoint ended after 4 bytes */ + if ((remaining != 4) && (0x80 == (0xc0 & str[4]))) { + return (utf8_int8_t *)str; + } + + /* ensure that the top 5 bits of this 4-byte utf8 + * codepoint were not 0, as then we could have used + * one of the smaller encodings */ + if ((0 == (0x07 & str[0])) && (0 == (0x30 & str[1]))) { + return (utf8_int8_t *)str; + } + + /* 4-byte utf8 code point (began with 0b11110xxx) */ + str += 4; + } else if (0xe0 == (0xf0 & *str)) { + /* ensure that there's 3 bytes or more remaining */ + if (remaining < 3) { + return (utf8_int8_t *)str; + } + + /* ensure each of the 2 following bytes in this 3-byte + * utf8 codepoint began with 0b10xxxxxx */ + if ((0x80 != (0xc0 & str[1])) || (0x80 != (0xc0 & str[2]))) { + return (utf8_int8_t *)str; + } + + /* ensure that our utf8 codepoint ended after 3 bytes */ + if ((remaining != 3) && (0x80 == (0xc0 & str[3]))) { + return (utf8_int8_t *)str; + } + + /* ensure that the top 5 bits of this 3-byte utf8 + * codepoint were not 0, as then we could have used + * one of the smaller encodings */ + if ((0 == (0x0f & str[0])) && (0 == (0x20 & str[1]))) { + return (utf8_int8_t *)str; + } + + /* 3-byte utf8 code point (began with 0b1110xxxx) */ + str += 3; + } else if (0xc0 == (0xe0 & *str)) { + /* ensure that there's 2 bytes or more remaining */ + if (remaining < 2) { + return (utf8_int8_t *)str; + } + + /* ensure the 1 following byte in this 2-byte + * utf8 codepoint began with 0b10xxxxxx */ + if (0x80 != (0xc0 & str[1])) { + return (utf8_int8_t *)str; + } + + /* ensure that our utf8 codepoint ended after 2 bytes */ + if ((remaining != 2) && (0x80 == (0xc0 & str[2]))) { + return (utf8_int8_t *)str; + } + + /* ensure that the top 4 bits of this 2-byte utf8 + * codepoint were not 0, as then we could have used + * one of the smaller encodings */ + if (0 == (0x1e & str[0])) { + return (utf8_int8_t *)str; + } + + /* 2-byte utf8 code point (began with 0b110xxxxx) */ + str += 2; + } else if (0x00 == (0x80 & *str)) { + /* 1-byte ascii (began with 0b0xxxxxxx) */ + str += 1; + } else { + /* we have an invalid 0b1xxxxxxx utf8 code point entry */ + return (utf8_int8_t *)str; + } + } + + return utf8_null; +} + +int utf8makevalid(utf8_int8_t *str, const utf8_int32_t replacement) { + utf8_int8_t *read = str; + utf8_int8_t *write = read; + const utf8_int8_t r = (utf8_int8_t)replacement; + utf8_int32_t codepoint = 0; + + if (replacement > 0x7f) { + return -1; + } + + while ('\0' != *read) { + if (0xf0 == (0xf8 & *read)) { + /* ensure each of the 3 following bytes in this 4-byte + * utf8 codepoint began with 0b10xxxxxx */ + if ((0x80 != (0xc0 & read[1])) || (0x80 != (0xc0 & read[2])) || + (0x80 != (0xc0 & read[3]))) { + *write++ = r; + read++; + continue; + } + + /* 4-byte utf8 code point (began with 0b11110xxx) */ + read = utf8codepoint(read, &codepoint); + write = utf8catcodepoint(write, codepoint, 4); + } else if (0xe0 == (0xf0 & *read)) { + /* ensure each of the 2 following bytes in this 3-byte + * utf8 codepoint began with 0b10xxxxxx */ + if ((0x80 != (0xc0 & read[1])) || (0x80 != (0xc0 & read[2]))) { + *write++ = r; + read++; + continue; + } + + /* 3-byte utf8 code point (began with 0b1110xxxx) */ + read = utf8codepoint(read, &codepoint); + write = utf8catcodepoint(write, codepoint, 3); + } else if (0xc0 == (0xe0 & *read)) { + /* ensure the 1 following byte in this 2-byte + * utf8 codepoint began with 0b10xxxxxx */ + if (0x80 != (0xc0 & read[1])) { + *write++ = r; + read++; + continue; + } + + /* 2-byte utf8 code point (began with 0b110xxxxx) */ + read = utf8codepoint(read, &codepoint); + write = utf8catcodepoint(write, codepoint, 2); + } else if (0x00 == (0x80 & *read)) { + /* 1-byte ascii (began with 0b0xxxxxxx) */ + read = utf8codepoint(read, &codepoint); + write = utf8catcodepoint(write, codepoint, 1); + } else { + /* if we got here then we've got a dangling continuation (0b10xxxxxx) */ + *write++ = r; + read++; + continue; + } + } + + *write = '\0'; + + return 0; +} + +utf8_constexpr14_impl utf8_int8_t * +utf8codepoint(const utf8_int8_t *utf8_restrict str, + utf8_int32_t *utf8_restrict out_codepoint) { + if (0xf0 == (0xf8 & str[0])) { + /* 4 byte utf8 codepoint */ + *out_codepoint = ((0x07 & str[0]) << 18) | ((0x3f & str[1]) << 12) | + ((0x3f & str[2]) << 6) | (0x3f & str[3]); + str += 4; + } else if (0xe0 == (0xf0 & str[0])) { + /* 3 byte utf8 codepoint */ + *out_codepoint = + ((0x0f & str[0]) << 12) | ((0x3f & str[1]) << 6) | (0x3f & str[2]); + str += 3; + } else if (0xc0 == (0xe0 & str[0])) { + /* 2 byte utf8 codepoint */ + *out_codepoint = ((0x1f & str[0]) << 6) | (0x3f & str[1]); + str += 2; + } else { + /* 1 byte utf8 codepoint otherwise */ + *out_codepoint = str[0]; + str += 1; + } + + return (utf8_int8_t *)str; +} + +utf8_constexpr14_impl size_t utf8codepointcalcsize(const utf8_int8_t *str) { + if (0xf0 == (0xf8 & str[0])) { + /* 4 byte utf8 codepoint */ + return 4; + } else if (0xe0 == (0xf0 & str[0])) { + /* 3 byte utf8 codepoint */ + return 3; + } else if (0xc0 == (0xe0 & str[0])) { + /* 2 byte utf8 codepoint */ + return 2; + } + + /* 1 byte utf8 codepoint otherwise */ + return 1; +} + +utf8_constexpr14_impl size_t utf8codepointsize(utf8_int32_t chr) { + if (0 == ((utf8_int32_t)0xffffff80 & chr)) { + return 1; + } else if (0 == ((utf8_int32_t)0xfffff800 & chr)) { + return 2; + } else if (0 == ((utf8_int32_t)0xffff0000 & chr)) { + return 3; + } else { /* if (0 == ((int)0xffe00000 & chr)) { */ + return 4; + } +} + +utf8_int8_t *utf8catcodepoint(utf8_int8_t *str, utf8_int32_t chr, size_t n) { + if (0 == ((utf8_int32_t)0xffffff80 & chr)) { + /* 1-byte/7-bit ascii + * (0b0xxxxxxx) */ + if (n < 1) { + return utf8_null; + } + str[0] = (utf8_int8_t)chr; + str += 1; + } else if (0 == ((utf8_int32_t)0xfffff800 & chr)) { + /* 2-byte/11-bit utf8 code point + * (0b110xxxxx 0b10xxxxxx) */ + if (n < 2) { + return utf8_null; + } + str[0] = (utf8_int8_t)(0xc0 | (utf8_int8_t)((chr >> 6) & 0x1f)); + str[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); + str += 2; + } else if (0 == ((utf8_int32_t)0xffff0000 & chr)) { + /* 3-byte/16-bit utf8 code point + * (0b1110xxxx 0b10xxxxxx 0b10xxxxxx) */ + if (n < 3) { + return utf8_null; + } + str[0] = (utf8_int8_t)(0xe0 | (utf8_int8_t)((chr >> 12) & 0x0f)); + str[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f)); + str[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); + str += 3; + } else { /* if (0 == ((int)0xffe00000 & chr)) { */ + /* 4-byte/21-bit utf8 code point + * (0b11110xxx 0b10xxxxxx 0b10xxxxxx 0b10xxxxxx) */ + if (n < 4) { + return utf8_null; + } + str[0] = (utf8_int8_t)(0xf0 | (utf8_int8_t)((chr >> 18) & 0x07)); + str[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 12) & 0x3f)); + str[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f)); + str[3] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); + str += 4; + } + + return str; +} + +utf8_constexpr14_impl int utf8islower(utf8_int32_t chr) { + return chr != utf8uprcodepoint(chr); +} + +utf8_constexpr14_impl int utf8isupper(utf8_int32_t chr) { + return chr != utf8lwrcodepoint(chr); +} + +void utf8lwr(utf8_int8_t *utf8_restrict str) { + utf8_int32_t cp = 0; + utf8_int8_t *pn = utf8codepoint(str, &cp); + + while (cp != 0) { + const utf8_int32_t lwr_cp = utf8lwrcodepoint(cp); + const size_t size = utf8codepointsize(lwr_cp); + + if (lwr_cp != cp) { + utf8catcodepoint(str, lwr_cp, size); + } + + str = pn; + pn = utf8codepoint(str, &cp); + } +} + +void utf8upr(utf8_int8_t *utf8_restrict str) { + utf8_int32_t cp = 0; + utf8_int8_t *pn = utf8codepoint(str, &cp); + + while (cp != 0) { + const utf8_int32_t lwr_cp = utf8uprcodepoint(cp); + const size_t size = utf8codepointsize(lwr_cp); + + if (lwr_cp != cp) { + utf8catcodepoint(str, lwr_cp, size); + } + + str = pn; + pn = utf8codepoint(str, &cp); + } +} + +utf8_constexpr14_impl utf8_int32_t utf8lwrcodepoint(utf8_int32_t cp) { + if (((0x0041 <= cp) && (0x005a >= cp)) || + ((0x00c0 <= cp) && (0x00d6 >= cp)) || + ((0x00d8 <= cp) && (0x00de >= cp)) || + ((0x0391 <= cp) && (0x03a1 >= cp)) || + ((0x03a3 <= cp) && (0x03ab >= cp)) || + ((0x0410 <= cp) && (0x042f >= cp))) { + cp += 32; + } else if ((0x0400 <= cp) && (0x040f >= cp)) { + cp += 80; + } else if (((0x0100 <= cp) && (0x012f >= cp)) || + ((0x0132 <= cp) && (0x0137 >= cp)) || + ((0x014a <= cp) && (0x0177 >= cp)) || + ((0x0182 <= cp) && (0x0185 >= cp)) || + ((0x01a0 <= cp) && (0x01a5 >= cp)) || + ((0x01de <= cp) && (0x01ef >= cp)) || + ((0x01f8 <= cp) && (0x021f >= cp)) || + ((0x0222 <= cp) && (0x0233 >= cp)) || + ((0x0246 <= cp) && (0x024f >= cp)) || + ((0x03d8 <= cp) && (0x03ef >= cp)) || + ((0x0460 <= cp) && (0x0481 >= cp)) || + ((0x048a <= cp) && (0x04ff >= cp))) { + cp |= 0x1; + } else if (((0x0139 <= cp) && (0x0148 >= cp)) || + ((0x0179 <= cp) && (0x017e >= cp)) || + ((0x01af <= cp) && (0x01b0 >= cp)) || + ((0x01b3 <= cp) && (0x01b6 >= cp)) || + ((0x01cd <= cp) && (0x01dc >= cp))) { + cp += 1; + cp &= ~0x1; + } else { + switch (cp) { + default: + break; + case 0x0178: + cp = 0x00ff; + break; + case 0x0243: + cp = 0x0180; + break; + case 0x018e: + cp = 0x01dd; + break; + case 0x023d: + cp = 0x019a; + break; + case 0x0220: + cp = 0x019e; + break; + case 0x01b7: + cp = 0x0292; + break; + case 0x01c4: + cp = 0x01c6; + break; + case 0x01c7: + cp = 0x01c9; + break; + case 0x01ca: + cp = 0x01cc; + break; + case 0x01f1: + cp = 0x01f3; + break; + case 0x01f7: + cp = 0x01bf; + break; + case 0x0187: + cp = 0x0188; + break; + case 0x018b: + cp = 0x018c; + break; + case 0x0191: + cp = 0x0192; + break; + case 0x0198: + cp = 0x0199; + break; + case 0x01a7: + cp = 0x01a8; + break; + case 0x01ac: + cp = 0x01ad; + break; + case 0x01b8: + cp = 0x01b9; + break; + case 0x01bc: + cp = 0x01bd; + break; + case 0x01f4: + cp = 0x01f5; + break; + case 0x023b: + cp = 0x023c; + break; + case 0x0241: + cp = 0x0242; + break; + case 0x03fd: + cp = 0x037b; + break; + case 0x03fe: + cp = 0x037c; + break; + case 0x03ff: + cp = 0x037d; + break; + case 0x037f: + cp = 0x03f3; + break; + case 0x0386: + cp = 0x03ac; + break; + case 0x0388: + cp = 0x03ad; + break; + case 0x0389: + cp = 0x03ae; + break; + case 0x038a: + cp = 0x03af; + break; + case 0x038c: + cp = 0x03cc; + break; + case 0x038e: + cp = 0x03cd; + break; + case 0x038f: + cp = 0x03ce; + break; + case 0x0370: + cp = 0x0371; + break; + case 0x0372: + cp = 0x0373; + break; + case 0x0376: + cp = 0x0377; + break; + case 0x03f4: + cp = 0x03b8; + break; + case 0x03cf: + cp = 0x03d7; + break; + case 0x03f9: + cp = 0x03f2; + break; + case 0x03f7: + cp = 0x03f8; + break; + case 0x03fa: + cp = 0x03fb; + break; + } + } + + return cp; +} + +utf8_constexpr14_impl utf8_int32_t utf8uprcodepoint(utf8_int32_t cp) { + if (((0x0061 <= cp) && (0x007a >= cp)) || + ((0x00e0 <= cp) && (0x00f6 >= cp)) || + ((0x00f8 <= cp) && (0x00fe >= cp)) || + ((0x03b1 <= cp) && (0x03c1 >= cp)) || + ((0x03c3 <= cp) && (0x03cb >= cp)) || + ((0x0430 <= cp) && (0x044f >= cp))) { + cp -= 32; + } else if ((0x0450 <= cp) && (0x045f >= cp)) { + cp -= 80; + } else if (((0x0100 <= cp) && (0x012f >= cp)) || + ((0x0132 <= cp) && (0x0137 >= cp)) || + ((0x014a <= cp) && (0x0177 >= cp)) || + ((0x0182 <= cp) && (0x0185 >= cp)) || + ((0x01a0 <= cp) && (0x01a5 >= cp)) || + ((0x01de <= cp) && (0x01ef >= cp)) || + ((0x01f8 <= cp) && (0x021f >= cp)) || + ((0x0222 <= cp) && (0x0233 >= cp)) || + ((0x0246 <= cp) && (0x024f >= cp)) || + ((0x03d8 <= cp) && (0x03ef >= cp)) || + ((0x0460 <= cp) && (0x0481 >= cp)) || + ((0x048a <= cp) && (0x04ff >= cp))) { + cp &= ~0x1; + } else if (((0x0139 <= cp) && (0x0148 >= cp)) || + ((0x0179 <= cp) && (0x017e >= cp)) || + ((0x01af <= cp) && (0x01b0 >= cp)) || + ((0x01b3 <= cp) && (0x01b6 >= cp)) || + ((0x01cd <= cp) && (0x01dc >= cp))) { + cp -= 1; + cp |= 0x1; + } else { + switch (cp) { + default: + break; + case 0x00ff: + cp = 0x0178; + break; + case 0x0180: + cp = 0x0243; + break; + case 0x01dd: + cp = 0x018e; + break; + case 0x019a: + cp = 0x023d; + break; + case 0x019e: + cp = 0x0220; + break; + case 0x0292: + cp = 0x01b7; + break; + case 0x01c6: + cp = 0x01c4; + break; + case 0x01c9: + cp = 0x01c7; + break; + case 0x01cc: + cp = 0x01ca; + break; + case 0x01f3: + cp = 0x01f1; + break; + case 0x01bf: + cp = 0x01f7; + break; + case 0x0188: + cp = 0x0187; + break; + case 0x018c: + cp = 0x018b; + break; + case 0x0192: + cp = 0x0191; + break; + case 0x0199: + cp = 0x0198; + break; + case 0x01a8: + cp = 0x01a7; + break; + case 0x01ad: + cp = 0x01ac; + break; + case 0x01b9: + cp = 0x01b8; + break; + case 0x01bd: + cp = 0x01bc; + break; + case 0x01f5: + cp = 0x01f4; + break; + case 0x023c: + cp = 0x023b; + break; + case 0x0242: + cp = 0x0241; + break; + case 0x037b: + cp = 0x03fd; + break; + case 0x037c: + cp = 0x03fe; + break; + case 0x037d: + cp = 0x03ff; + break; + case 0x03f3: + cp = 0x037f; + break; + case 0x03ac: + cp = 0x0386; + break; + case 0x03ad: + cp = 0x0388; + break; + case 0x03ae: + cp = 0x0389; + break; + case 0x03af: + cp = 0x038a; + break; + case 0x03cc: + cp = 0x038c; + break; + case 0x03cd: + cp = 0x038e; + break; + case 0x03ce: + cp = 0x038f; + break; + case 0x0371: + cp = 0x0370; + break; + case 0x0373: + cp = 0x0372; + break; + case 0x0377: + cp = 0x0376; + break; + case 0x03d1: + cp = 0x0398; + break; + case 0x03d7: + cp = 0x03cf; + break; + case 0x03f2: + cp = 0x03f9; + break; + case 0x03f8: + cp = 0x03f7; + break; + case 0x03fb: + cp = 0x03fa; + break; + } + } + + return cp; +} + +utf8_constexpr14_impl utf8_int8_t * +utf8rcodepoint(const utf8_int8_t *utf8_restrict str, + utf8_int32_t *utf8_restrict out_codepoint) { + const utf8_int8_t *s = (const utf8_int8_t *)str; + + if (0xf0 == (0xf8 & s[0])) { + /* 4 byte utf8 codepoint */ + *out_codepoint = ((0x07 & s[0]) << 18) | ((0x3f & s[1]) << 12) | + ((0x3f & s[2]) << 6) | (0x3f & s[3]); + } else if (0xe0 == (0xf0 & s[0])) { + /* 3 byte utf8 codepoint */ + *out_codepoint = + ((0x0f & s[0]) << 12) | ((0x3f & s[1]) << 6) | (0x3f & s[2]); + } else if (0xc0 == (0xe0 & s[0])) { + /* 2 byte utf8 codepoint */ + *out_codepoint = ((0x1f & s[0]) << 6) | (0x3f & s[1]); + } else { + /* 1 byte utf8 codepoint otherwise */ + *out_codepoint = s[0]; + } + + do { + s--; + } while ((0 != (0x80 & s[0])) && (0x80 == (0xc0 & s[0]))); + + return (utf8_int8_t *)s; +} + +#undef utf8_restrict +#undef utf8_constexpr14 +#undef utf8_null + +#ifdef utf8_cplusplus +} /* extern "C" */ +#endif + +#if defined(__clang__) +#pragma clang diagnostic pop +#endif + +#endif /* SHEREDOM_UTF8_H_INCLUDED */ diff --git a/deps/ntlmclient/util.c b/deps/ntlmclient/util.c new file mode 100644 index 00000000000..07d10f6c609 --- /dev/null +++ b/deps/ntlmclient/util.c @@ -0,0 +1,35 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#include +#include +#include + +#include "compat.h" +#include "util.h" + +void ntlm_memzero(void *data, size_t size) +{ + volatile uint8_t *scan = (volatile uint8_t *)data; + + while (size--) + *scan++ = 0x0; +} + +uint64_t ntlm_htonll(uint64_t value) +{ + static union { + uint32_t i; + char c[8]; + } test = { 0x01020304 }; + + if (test.c[0] == 0x01) + return value; + else + return ((uint64_t)htonl(value) << 32) | htonl((uint64_t)value >> 32); +} diff --git a/deps/ntlmclient/util.h b/deps/ntlmclient/util.h new file mode 100644 index 00000000000..48e0169932f --- /dev/null +++ b/deps/ntlmclient/util.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) Edward Thomson. All rights reserved. + * + * This file is part of ntlmclient, distributed under the MIT license. + * For full terms and copyright information, and for third-party + * copyright information, see the included LICENSE.txt file. + */ + +#ifndef PRIVATE_UTIL_H__ +#define PRIVATE_UTIL_H__ + +#include +#include + +#if defined(_MSC_VER) +# define NTLM_INLINE(type) static __inline type +#elif defined(__GNUC__) +# define NTLM_INLINE(type) static __inline__ type +#else +# define NTLM_INLINE(type) static type +#endif + +extern void ntlm_memzero(void *data, size_t size); +extern uint64_t ntlm_htonll(uint64_t value); + +#endif /* PRIVATE_UTIL_H__ */ diff --git a/deps/pcre/CMakeLists.txt b/deps/pcre/CMakeLists.txt new file mode 100644 index 00000000000..53b5cee86d5 --- /dev/null +++ b/deps/pcre/CMakeLists.txt @@ -0,0 +1,141 @@ +include(CheckIncludeFile) +include(CheckFunctionExists) +include(CheckTypeSize) + +check_include_file(dirent.h HAVE_DIRENT_H) +check_include_file(stdint.h HAVE_STDINT_H) +check_include_file(inttypes.h HAVE_INTTYPES_H) +check_include_file(sys/stat.h HAVE_SYS_STAT_H) +check_include_file(sys/types.h HAVE_SYS_TYPES_H) +check_include_file(unistd.h HAVE_UNISTD_H) +check_include_file(windows.h HAVE_WINDOWS_H) + +check_function_exists(bcopy HAVE_BCOPY) +check_function_exists(memmove HAVE_MEMMOVE) +check_function_exists(strerror HAVE_STRERROR) +check_function_exists(strtoll HAVE_STRTOLL) +check_function_exists(strtoq HAVE_STRTOQ) +check_function_exists(_strtoi64 HAVE__STRTOI64) + +check_type_size("long long" LONG_LONG) +check_type_size("unsigned long long" UNSIGNED_LONG_LONG) + +disable_warnings(unused-function) +disable_warnings(implicit-fallthrough) +disable_warnings(unused-but-set-variable) + +# User-configurable options + +set(SUPPORT_PCRE8 1) +set(PCRE_LINK_SIZE "2") +set(PCRE_PARENS_NEST_LIMIT "250") +set(PCRE_MATCH_LIMIT "10000000") +set(PCRE_MATCH_LIMIT_RECURSION "MATCH_LIMIT") +set(PCRE_NEWLINE "LF") +set(NO_RECURSE 1) +set(PCRE_POSIX_MALLOC_THRESHOLD "10") +set(BSR_ANYCRLF 0) + +if(MINGW) + option(NON_STANDARD_LIB_PREFIX + "ON=Shared libraries built in mingw will be named pcre.dll, etc., instead of libpcre.dll, etc." + OFF) + + option(NON_STANDARD_LIB_SUFFIX + "ON=Shared libraries built in mingw will be named libpcre-0.dll, etc., instead of libpcre.dll, etc." + OFF) +endif(MINGW) + +# Prepare build configuration + +set(pcre_have_long_long 0) +set(pcre_have_ulong_long 0) + +if(HAVE_LONG_LONG) + set(pcre_have_long_long 1) +endif(HAVE_LONG_LONG) + +if(HAVE_UNSIGNED_LONG_LONG) + set(pcre_have_ulong_long 1) +endif(HAVE_UNSIGNED_LONG_LONG) + +set(NEWLINE "") + +if(PCRE_NEWLINE STREQUAL "LF") + set(NEWLINE "10") +endif(PCRE_NEWLINE STREQUAL "LF") +if(PCRE_NEWLINE STREQUAL "CR") + set(NEWLINE "13") +endif(PCRE_NEWLINE STREQUAL "CR") +if(PCRE_NEWLINE STREQUAL "CRLF") + set(NEWLINE "3338") +endif(PCRE_NEWLINE STREQUAL "CRLF") +if(PCRE_NEWLINE STREQUAL "ANY") + set(NEWLINE "-1") +endif(PCRE_NEWLINE STREQUAL "ANY") +if(PCRE_NEWLINE STREQUAL "ANYCRLF") + set(NEWLINE "-2") +endif(PCRE_NEWLINE STREQUAL "ANYCRLF") + +if(NEWLINE STREQUAL "") + message(FATAL_ERROR "The PCRE_NEWLINE variable must be set to one of the following values: \"LF\", \"CR\", \"CRLF\", \"ANY\", \"ANYCRLF\".") +endif(NEWLINE STREQUAL "") + +# Output files +configure_file(config.h.in + ${PROJECT_BINARY_DIR}/src/pcre/config.h + @ONLY) + +# Source code + +set(PCRE_HEADERS ${PROJECT_BINARY_DIR}/src/pcre/config.h) + +set(PCRE_SOURCES + pcre_byte_order.c + pcre_chartables.c + pcre_compile.c + pcre_config.c + pcre_dfa_exec.c + pcre_exec.c + pcre_fullinfo.c + pcre_get.c + pcre_globals.c + pcre_jit_compile.c + pcre_maketables.c + pcre_newline.c + pcre_ord2utf8.c + pcre_refcount.c + pcre_string_utils.c + pcre_study.c + pcre_tables.c + pcre_ucd.c + pcre_valid_utf8.c + pcre_version.c + pcre_xclass.c +) + +set(PCREPOSIX_HEADERS pcreposix.h) + +set(PCREPOSIX_SOURCES pcreposix.c) + +# Fix static compilation with MSVC: https://bugs.exim.org/show_bug.cgi?id=1681 +# This code was taken from the CMake wiki, not from WebM. + +# Build setup + +add_definitions(-DHAVE_CONFIG_H) + +if(MSVC) + add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS) +endif(MSVC) + +set(CMAKE_INCLUDE_CURRENT_DIR 1) + +set(targets) + +# Libraries +# pcre +include_directories(${PROJECT_BINARY_DIR}/src/pcre) +add_library(pcre OBJECT ${PCRE_HEADERS} ${PCRE_SOURCES} ${PCREPOSIX_SOURCES}) + +# end CMakeLists.txt diff --git a/deps/pcre/COPYING b/deps/pcre/COPYING new file mode 100644 index 00000000000..58eed01b61d --- /dev/null +++ b/deps/pcre/COPYING @@ -0,0 +1,5 @@ +PCRE LICENCE + +Please see the file LICENCE in the PCRE distribution for licensing details. + +End diff --git a/deps/pcre/LICENCE b/deps/pcre/LICENCE new file mode 100644 index 00000000000..803b4119e50 --- /dev/null +++ b/deps/pcre/LICENCE @@ -0,0 +1,93 @@ +PCRE LICENCE +------------ + +PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + +Release 8 of PCRE is distributed under the terms of the "BSD" licence, as +specified below. The documentation for PCRE, supplied in the "doc" +directory, is distributed under the same terms as the software itself. The data +in the testdata directory is not copyrighted and is in the public domain. + +The basic library functions are written in C and are freestanding. Also +included in the distribution is a set of C++ wrapper functions, and a +just-in-time compiler that can be used to optimize pattern matching. These +are both optional features that can be omitted when the library is built. + + +THE BASIC LIBRARY FUNCTIONS +--------------------------- + +Written by: Philip Hazel +Email local part: Philip.Hazel +Email domain: gmail.com + +University of Cambridge Computing Service, +Cambridge, England. + +Copyright (c) 1997-2021 University of Cambridge +All rights reserved. + + +PCRE JUST-IN-TIME COMPILATION SUPPORT +------------------------------------- + +Written by: Zoltan Herczeg +Email local part: hzmester +Email domain: freemail.hu + +Copyright(c) 2010-2021 Zoltan Herczeg +All rights reserved. + + +STACK-LESS JUST-IN-TIME COMPILER +-------------------------------- + +Written by: Zoltan Herczeg +Email local part: hzmester +Email domain: freemail.hu + +Copyright(c) 2009-2021 Zoltan Herczeg +All rights reserved. + + +THE C++ WRAPPER FUNCTIONS +------------------------- + +Contributed by: Google Inc. + +Copyright (c) 2007-2012, Google Inc. +All rights reserved. + + +THE "BSD" LICENCE +----------------- + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the name of Google + Inc. nor the names of their contributors may be used to endorse or + promote products derived from this software without specific prior + written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +End diff --git a/deps/pcre/cmake/COPYING-CMAKE-SCRIPTS b/deps/pcre/cmake/COPYING-CMAKE-SCRIPTS new file mode 100644 index 00000000000..4b417765f3a --- /dev/null +++ b/deps/pcre/cmake/COPYING-CMAKE-SCRIPTS @@ -0,0 +1,22 @@ +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/deps/pcre/cmake/FindEditline.cmake b/deps/pcre/cmake/FindEditline.cmake new file mode 100644 index 00000000000..2d0b7cc543c --- /dev/null +++ b/deps/pcre/cmake/FindEditline.cmake @@ -0,0 +1,17 @@ +# Modified from FindReadline.cmake (PH Feb 2012) + +if(EDITLINE_INCLUDE_DIR AND EDITLINE_LIBRARY AND NCURSES_LIBRARY) + set(EDITLINE_FOUND TRUE) +else(EDITLINE_INCLUDE_DIR AND EDITLINE_LIBRARY AND NCURSES_LIBRARY) + FIND_PATH(EDITLINE_INCLUDE_DIR readline.h + /usr/include/editline + /usr/include/edit/readline + /usr/include/readline + ) + + FIND_LIBRARY(EDITLINE_LIBRARY NAMES edit) + include(FindPackageHandleStandardArgs) + FIND_PACKAGE_HANDLE_STANDARD_ARGS(Editline DEFAULT_MSG EDITLINE_INCLUDE_DIR EDITLINE_LIBRARY ) + + MARK_AS_ADVANCED(EDITLINE_INCLUDE_DIR EDITLINE_LIBRARY) +endif(EDITLINE_INCLUDE_DIR AND EDITLINE_LIBRARY AND NCURSES_LIBRARY) diff --git a/deps/pcre/cmake/FindPackageHandleStandardArgs.cmake b/deps/pcre/cmake/FindPackageHandleStandardArgs.cmake new file mode 100644 index 00000000000..151d8125031 --- /dev/null +++ b/deps/pcre/cmake/FindPackageHandleStandardArgs.cmake @@ -0,0 +1,58 @@ +# FIND_PACKAGE_HANDLE_STANDARD_ARGS(NAME (DEFAULT_MSG|"Custom failure message") VAR1 ... ) +# This macro is intended to be used in FindXXX.cmake modules files. +# It handles the REQUIRED and QUIET argument to FIND_PACKAGE() and +# it also sets the _FOUND variable. +# The package is found if all variables listed are TRUE. +# Example: +# +# FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibXml2 DEFAULT_MSG LIBXML2_LIBRARIES LIBXML2_INCLUDE_DIR) +# +# LibXml2 is considered to be found, if both LIBXML2_LIBRARIES and +# LIBXML2_INCLUDE_DIR are valid. Then also LIBXML2_FOUND is set to TRUE. +# If it is not found and REQUIRED was used, it fails with FATAL_ERROR, +# independent whether QUIET was used or not. +# If it is found, the location is reported using the VAR1 argument, so +# here a message "Found LibXml2: /usr/lib/libxml2.so" will be printed out. +# If the second argument is DEFAULT_MSG, the message in the failure case will +# be "Could NOT find LibXml2", if you don't like this message you can specify +# your own custom failure message there. + +MACRO(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FAIL_MSG _VAR1 ) + + IF("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG") + IF (${_NAME}_FIND_REQUIRED) + SET(_FAIL_MESSAGE "Could not find REQUIRED package ${_NAME}") + ELSE (${_NAME}_FIND_REQUIRED) + SET(_FAIL_MESSAGE "Could not find OPTIONAL package ${_NAME}") + ENDIF (${_NAME}_FIND_REQUIRED) + ELSE("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG") + SET(_FAIL_MESSAGE "${_FAIL_MSG}") + ENDIF("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG") + + STRING(TOUPPER ${_NAME} _NAME_UPPER) + + SET(${_NAME_UPPER}_FOUND TRUE) + IF(NOT ${_VAR1}) + SET(${_NAME_UPPER}_FOUND FALSE) + ENDIF(NOT ${_VAR1}) + + FOREACH(_CURRENT_VAR ${ARGN}) + IF(NOT ${_CURRENT_VAR}) + SET(${_NAME_UPPER}_FOUND FALSE) + ENDIF(NOT ${_CURRENT_VAR}) + ENDFOREACH(_CURRENT_VAR) + + IF (${_NAME_UPPER}_FOUND) + IF (NOT ${_NAME}_FIND_QUIETLY) + MESSAGE(STATUS "Found ${_NAME}: ${${_VAR1}}") + ENDIF (NOT ${_NAME}_FIND_QUIETLY) + ELSE (${_NAME_UPPER}_FOUND) + IF (${_NAME}_FIND_REQUIRED) + MESSAGE(FATAL_ERROR "${_FAIL_MESSAGE}") + ELSE (${_NAME}_FIND_REQUIRED) + IF (NOT ${_NAME}_FIND_QUIETLY) + MESSAGE(STATUS "${_FAIL_MESSAGE}") + ENDIF (NOT ${_NAME}_FIND_QUIETLY) + ENDIF (${_NAME}_FIND_REQUIRED) + ENDIF (${_NAME_UPPER}_FOUND) +ENDMACRO(FIND_PACKAGE_HANDLE_STANDARD_ARGS) diff --git a/deps/pcre/cmake/FindReadline.cmake b/deps/pcre/cmake/FindReadline.cmake new file mode 100644 index 00000000000..1d4cc558431 --- /dev/null +++ b/deps/pcre/cmake/FindReadline.cmake @@ -0,0 +1,29 @@ +# from http://websvn.kde.org/trunk/KDE/kdeedu/cmake/modules/FindReadline.cmake +# http://websvn.kde.org/trunk/KDE/kdeedu/cmake/modules/COPYING-CMAKE-SCRIPTS +# --> BSD licensed +# +# GNU Readline library finder +if(READLINE_INCLUDE_DIR AND READLINE_LIBRARY AND NCURSES_LIBRARY) + set(READLINE_FOUND TRUE) +else(READLINE_INCLUDE_DIR AND READLINE_LIBRARY AND NCURSES_LIBRARY) + FIND_PATH(READLINE_INCLUDE_DIR readline/readline.h + /usr/include/readline + ) + +# 2008-04-22 The next clause used to read like this: +# +# FIND_LIBRARY(READLINE_LIBRARY NAMES readline) +# FIND_LIBRARY(NCURSES_LIBRARY NAMES ncurses ) +# include(FindPackageHandleStandardArgs) +# FIND_PACKAGE_HANDLE_STANDARD_ARGS(Readline DEFAULT_MSG NCURSES_LIBRARY READLINE_INCLUDE_DIR READLINE_LIBRARY ) +# +# I was advised to modify it such that it will find an ncurses library if +# required, but not if one was explicitly given, that is, it allows the +# default to be overridden. PH + + FIND_LIBRARY(READLINE_LIBRARY NAMES readline) + include(FindPackageHandleStandardArgs) + FIND_PACKAGE_HANDLE_STANDARD_ARGS(Readline DEFAULT_MSG READLINE_INCLUDE_DIR READLINE_LIBRARY ) + + MARK_AS_ADVANCED(READLINE_INCLUDE_DIR READLINE_LIBRARY) +endif(READLINE_INCLUDE_DIR AND READLINE_LIBRARY AND NCURSES_LIBRARY) diff --git a/deps/pcre/config.h.in b/deps/pcre/config.h.in new file mode 100644 index 00000000000..77d901165d7 --- /dev/null +++ b/deps/pcre/config.h.in @@ -0,0 +1,57 @@ +/* config.h for CMake builds */ + +#cmakedefine HAVE_DIRENT_H 1 +#cmakedefine HAVE_SYS_STAT_H 1 +#cmakedefine HAVE_SYS_TYPES_H 1 +#cmakedefine HAVE_UNISTD_H 1 +#cmakedefine HAVE_WINDOWS_H 1 +#cmakedefine HAVE_STDINT_H 1 +#cmakedefine HAVE_INTTYPES_H 1 + +#cmakedefine HAVE_TYPE_TRAITS_H 1 +#cmakedefine HAVE_BITS_TYPE_TRAITS_H 1 + +#cmakedefine HAVE_BCOPY 1 +#cmakedefine HAVE_MEMMOVE 1 +#cmakedefine HAVE_STRERROR 1 +#cmakedefine HAVE_STRTOLL 1 +#cmakedefine HAVE_STRTOQ 1 +#cmakedefine HAVE__STRTOI64 1 + +#cmakedefine PCRE_STATIC 1 + +#cmakedefine SUPPORT_PCRE8 1 +#cmakedefine SUPPORT_PCRE16 1 +#cmakedefine SUPPORT_PCRE32 1 +#cmakedefine SUPPORT_JIT 1 +#cmakedefine SUPPORT_PCREGREP_JIT 1 +#cmakedefine SUPPORT_UTF 1 +#cmakedefine SUPPORT_UCP 1 +#cmakedefine EBCDIC 1 +#cmakedefine EBCDIC_NL25 1 +#cmakedefine BSR_ANYCRLF 1 +#cmakedefine NO_RECURSE 1 + +#cmakedefine HAVE_LONG_LONG 1 +#cmakedefine HAVE_UNSIGNED_LONG_LONG 1 + +#cmakedefine SUPPORT_LIBBZ2 1 +#cmakedefine SUPPORT_LIBZ 1 +#cmakedefine SUPPORT_LIBEDIT 1 +#cmakedefine SUPPORT_LIBREADLINE 1 + +#cmakedefine SUPPORT_VALGRIND 1 +#cmakedefine SUPPORT_GCOV 1 + +#define NEWLINE @NEWLINE@ +#define POSIX_MALLOC_THRESHOLD @PCRE_POSIX_MALLOC_THRESHOLD@ +#define LINK_SIZE @PCRE_LINK_SIZE@ +#define PARENS_NEST_LIMIT @PCRE_PARENS_NEST_LIMIT@ +#define MATCH_LIMIT @PCRE_MATCH_LIMIT@ +#define MATCH_LIMIT_RECURSION @PCRE_MATCH_LIMIT_RECURSION@ +#define PCREGREP_BUFSIZE @PCREGREP_BUFSIZE@ + +#define MAX_NAME_SIZE 32 +#define MAX_NAME_COUNT 10000 + +/* end config.h for CMake builds */ diff --git a/deps/pcre/pcre.h b/deps/pcre/pcre.h new file mode 100644 index 00000000000..821b50e88a9 --- /dev/null +++ b/deps/pcre/pcre.h @@ -0,0 +1,641 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* This is the public header file for the PCRE library, to be #included by +applications that call the PCRE functions. + + Copyright (c) 1997-2014 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + +#ifndef _PCRE_H +#define _PCRE_H + +/* The current PCRE version information. */ + +#define PCRE_MAJOR 8 +#define PCRE_MINOR 45 +#define PCRE_PRERELEASE +#define PCRE_DATE 2021-06-15 + +#define PCRE_EXP_DECL extern + +/* Have to include stdlib.h in order to ensure that size_t is defined; +it is needed here for malloc. */ + +#include + +/* Allow for C++ users */ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Public options. Some are compile-time only, some are run-time only, and some +are both. Most of the compile-time options are saved with the compiled regex so +that they can be inspected during studying (and therefore JIT compiling). Note +that pcre_study() has its own set of options. Originally, all the options +defined here used distinct bits. However, almost all the bits in a 32-bit word +are now used, so in order to conserve them, option bits that were previously +only recognized at matching time (i.e. by pcre_exec() or pcre_dfa_exec()) may +also be used for compile-time options that affect only compiling and are not +relevant for studying or JIT compiling. + +Some options for pcre_compile() change its behaviour but do not affect the +behaviour of the execution functions. Other options are passed through to the +execution functions and affect their behaviour, with or without affecting the +behaviour of pcre_compile(). + +Options that can be passed to pcre_compile() are tagged Cx below, with these +variants: + +C1 Affects compile only +C2 Does not affect compile; affects exec, dfa_exec +C3 Affects compile, exec, dfa_exec +C4 Affects compile, exec, dfa_exec, study +C5 Affects compile, exec, study + +Options that can be set for pcre_exec() and/or pcre_dfa_exec() are flagged with +E and D, respectively. They take precedence over C3, C4, and C5 settings passed +from pcre_compile(). Those that are compatible with JIT execution are flagged +with J. */ + +#define PCRE_CASELESS 0x00000001 /* C1 */ +#define PCRE_MULTILINE 0x00000002 /* C1 */ +#define PCRE_DOTALL 0x00000004 /* C1 */ +#define PCRE_EXTENDED 0x00000008 /* C1 */ +#define PCRE_ANCHORED 0x00000010 /* C4 E D */ +#define PCRE_DOLLAR_ENDONLY 0x00000020 /* C2 */ +#define PCRE_EXTRA 0x00000040 /* C1 */ +#define PCRE_NOTBOL 0x00000080 /* E D J */ +#define PCRE_NOTEOL 0x00000100 /* E D J */ +#define PCRE_UNGREEDY 0x00000200 /* C1 */ +#define PCRE_NOTEMPTY 0x00000400 /* E D J */ +#define PCRE_UTF8 0x00000800 /* C4 ) */ +#define PCRE_UTF16 0x00000800 /* C4 ) Synonyms */ +#define PCRE_UTF32 0x00000800 /* C4 ) */ +#define PCRE_NO_AUTO_CAPTURE 0x00001000 /* C1 */ +#define PCRE_NO_UTF8_CHECK 0x00002000 /* C1 E D J ) */ +#define PCRE_NO_UTF16_CHECK 0x00002000 /* C1 E D J ) Synonyms */ +#define PCRE_NO_UTF32_CHECK 0x00002000 /* C1 E D J ) */ +#define PCRE_AUTO_CALLOUT 0x00004000 /* C1 */ +#define PCRE_PARTIAL_SOFT 0x00008000 /* E D J ) Synonyms */ +#define PCRE_PARTIAL 0x00008000 /* E D J ) */ + +/* This pair use the same bit. */ +#define PCRE_NEVER_UTF 0x00010000 /* C1 ) Overlaid */ +#define PCRE_DFA_SHORTEST 0x00010000 /* D ) Overlaid */ + +/* This pair use the same bit. */ +#define PCRE_NO_AUTO_POSSESS 0x00020000 /* C1 ) Overlaid */ +#define PCRE_DFA_RESTART 0x00020000 /* D ) Overlaid */ + +#define PCRE_FIRSTLINE 0x00040000 /* C3 */ +#define PCRE_DUPNAMES 0x00080000 /* C1 */ +#define PCRE_NEWLINE_CR 0x00100000 /* C3 E D */ +#define PCRE_NEWLINE_LF 0x00200000 /* C3 E D */ +#define PCRE_NEWLINE_CRLF 0x00300000 /* C3 E D */ +#define PCRE_NEWLINE_ANY 0x00400000 /* C3 E D */ +#define PCRE_NEWLINE_ANYCRLF 0x00500000 /* C3 E D */ +#define PCRE_BSR_ANYCRLF 0x00800000 /* C3 E D */ +#define PCRE_BSR_UNICODE 0x01000000 /* C3 E D */ +#define PCRE_JAVASCRIPT_COMPAT 0x02000000 /* C5 */ +#define PCRE_NO_START_OPTIMIZE 0x04000000 /* C2 E D ) Synonyms */ +#define PCRE_NO_START_OPTIMISE 0x04000000 /* C2 E D ) */ +#define PCRE_PARTIAL_HARD 0x08000000 /* E D J */ +#define PCRE_NOTEMPTY_ATSTART 0x10000000 /* E D J */ +#define PCRE_UCP 0x20000000 /* C3 */ + +/* Exec-time and get/set-time error codes */ + +#define PCRE_ERROR_NOMATCH (-1) +#define PCRE_ERROR_NULL (-2) +#define PCRE_ERROR_BADOPTION (-3) +#define PCRE_ERROR_BADMAGIC (-4) +#define PCRE_ERROR_UNKNOWN_OPCODE (-5) +#define PCRE_ERROR_UNKNOWN_NODE (-5) /* For backward compatibility */ +#define PCRE_ERROR_NOMEMORY (-6) +#define PCRE_ERROR_NOSUBSTRING (-7) +#define PCRE_ERROR_MATCHLIMIT (-8) +#define PCRE_ERROR_CALLOUT (-9) /* Never used by PCRE itself */ +#define PCRE_ERROR_BADUTF8 (-10) /* Same for 8/16/32 */ +#define PCRE_ERROR_BADUTF16 (-10) /* Same for 8/16/32 */ +#define PCRE_ERROR_BADUTF32 (-10) /* Same for 8/16/32 */ +#define PCRE_ERROR_BADUTF8_OFFSET (-11) /* Same for 8/16 */ +#define PCRE_ERROR_BADUTF16_OFFSET (-11) /* Same for 8/16 */ +#define PCRE_ERROR_PARTIAL (-12) +#define PCRE_ERROR_BADPARTIAL (-13) +#define PCRE_ERROR_INTERNAL (-14) +#define PCRE_ERROR_BADCOUNT (-15) +#define PCRE_ERROR_DFA_UITEM (-16) +#define PCRE_ERROR_DFA_UCOND (-17) +#define PCRE_ERROR_DFA_UMLIMIT (-18) +#define PCRE_ERROR_DFA_WSSIZE (-19) +#define PCRE_ERROR_DFA_RECURSE (-20) +#define PCRE_ERROR_RECURSIONLIMIT (-21) +#define PCRE_ERROR_NULLWSLIMIT (-22) /* No longer actually used */ +#define PCRE_ERROR_BADNEWLINE (-23) +#define PCRE_ERROR_BADOFFSET (-24) +#define PCRE_ERROR_SHORTUTF8 (-25) +#define PCRE_ERROR_SHORTUTF16 (-25) /* Same for 8/16 */ +#define PCRE_ERROR_RECURSELOOP (-26) +#define PCRE_ERROR_JIT_STACKLIMIT (-27) +#define PCRE_ERROR_BADMODE (-28) +#define PCRE_ERROR_BADENDIANNESS (-29) +#define PCRE_ERROR_DFA_BADRESTART (-30) +#define PCRE_ERROR_JIT_BADOPTION (-31) +#define PCRE_ERROR_BADLENGTH (-32) +#define PCRE_ERROR_UNSET (-33) + +/* Specific error codes for UTF-8 validity checks */ + +#define PCRE_UTF8_ERR0 0 +#define PCRE_UTF8_ERR1 1 +#define PCRE_UTF8_ERR2 2 +#define PCRE_UTF8_ERR3 3 +#define PCRE_UTF8_ERR4 4 +#define PCRE_UTF8_ERR5 5 +#define PCRE_UTF8_ERR6 6 +#define PCRE_UTF8_ERR7 7 +#define PCRE_UTF8_ERR8 8 +#define PCRE_UTF8_ERR9 9 +#define PCRE_UTF8_ERR10 10 +#define PCRE_UTF8_ERR11 11 +#define PCRE_UTF8_ERR12 12 +#define PCRE_UTF8_ERR13 13 +#define PCRE_UTF8_ERR14 14 +#define PCRE_UTF8_ERR15 15 +#define PCRE_UTF8_ERR16 16 +#define PCRE_UTF8_ERR17 17 +#define PCRE_UTF8_ERR18 18 +#define PCRE_UTF8_ERR19 19 +#define PCRE_UTF8_ERR20 20 +#define PCRE_UTF8_ERR21 21 +#define PCRE_UTF8_ERR22 22 /* Unused (was non-character) */ + +/* Specific error codes for UTF-16 validity checks */ + +#define PCRE_UTF16_ERR0 0 +#define PCRE_UTF16_ERR1 1 +#define PCRE_UTF16_ERR2 2 +#define PCRE_UTF16_ERR3 3 +#define PCRE_UTF16_ERR4 4 /* Unused (was non-character) */ + +/* Specific error codes for UTF-32 validity checks */ + +#define PCRE_UTF32_ERR0 0 +#define PCRE_UTF32_ERR1 1 +#define PCRE_UTF32_ERR2 2 /* Unused (was non-character) */ +#define PCRE_UTF32_ERR3 3 + +/* Request types for pcre_fullinfo() */ + +#define PCRE_INFO_OPTIONS 0 +#define PCRE_INFO_SIZE 1 +#define PCRE_INFO_CAPTURECOUNT 2 +#define PCRE_INFO_BACKREFMAX 3 +#define PCRE_INFO_FIRSTBYTE 4 +#define PCRE_INFO_FIRSTCHAR 4 /* For backwards compatibility */ +#define PCRE_INFO_FIRSTTABLE 5 +#define PCRE_INFO_LASTLITERAL 6 +#define PCRE_INFO_NAMEENTRYSIZE 7 +#define PCRE_INFO_NAMECOUNT 8 +#define PCRE_INFO_NAMETABLE 9 +#define PCRE_INFO_STUDYSIZE 10 +#define PCRE_INFO_DEFAULT_TABLES 11 +#define PCRE_INFO_OKPARTIAL 12 +#define PCRE_INFO_JCHANGED 13 +#define PCRE_INFO_HASCRORLF 14 +#define PCRE_INFO_MINLENGTH 15 +#define PCRE_INFO_JIT 16 +#define PCRE_INFO_JITSIZE 17 +#define PCRE_INFO_MAXLOOKBEHIND 18 +#define PCRE_INFO_FIRSTCHARACTER 19 +#define PCRE_INFO_FIRSTCHARACTERFLAGS 20 +#define PCRE_INFO_REQUIREDCHAR 21 +#define PCRE_INFO_REQUIREDCHARFLAGS 22 +#define PCRE_INFO_MATCHLIMIT 23 +#define PCRE_INFO_RECURSIONLIMIT 24 +#define PCRE_INFO_MATCH_EMPTY 25 + +/* Request types for pcre_config(). Do not re-arrange, in order to remain +compatible. */ + +#define PCRE_CONFIG_UTF8 0 +#define PCRE_CONFIG_NEWLINE 1 +#define PCRE_CONFIG_LINK_SIZE 2 +#define PCRE_CONFIG_POSIX_MALLOC_THRESHOLD 3 +#define PCRE_CONFIG_MATCH_LIMIT 4 +#define PCRE_CONFIG_STACKRECURSE 5 +#define PCRE_CONFIG_UNICODE_PROPERTIES 6 +#define PCRE_CONFIG_MATCH_LIMIT_RECURSION 7 +#define PCRE_CONFIG_BSR 8 +#define PCRE_CONFIG_JIT 9 +#define PCRE_CONFIG_UTF16 10 +#define PCRE_CONFIG_JITTARGET 11 +#define PCRE_CONFIG_UTF32 12 +#define PCRE_CONFIG_PARENS_LIMIT 13 + +/* Request types for pcre_study(). Do not re-arrange, in order to remain +compatible. */ + +#define PCRE_STUDY_JIT_COMPILE 0x0001 +#define PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE 0x0002 +#define PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE 0x0004 +#define PCRE_STUDY_EXTRA_NEEDED 0x0008 + +/* Bit flags for the pcre[16|32]_extra structure. Do not re-arrange or redefine +these bits, just add new ones on the end, in order to remain compatible. */ + +#define PCRE_EXTRA_STUDY_DATA 0x0001 +#define PCRE_EXTRA_MATCH_LIMIT 0x0002 +#define PCRE_EXTRA_CALLOUT_DATA 0x0004 +#define PCRE_EXTRA_TABLES 0x0008 +#define PCRE_EXTRA_MATCH_LIMIT_RECURSION 0x0010 +#define PCRE_EXTRA_MARK 0x0020 +#define PCRE_EXTRA_EXECUTABLE_JIT 0x0040 + +/* Types */ + +struct real_pcre8_or_16; /* declaration; the definition is private */ +typedef struct real_pcre8_or_16 pcre; + +struct real_pcre8_or_16; /* declaration; the definition is private */ +typedef struct real_pcre8_or_16 pcre16; + +struct real_pcre32; /* declaration; the definition is private */ +typedef struct real_pcre32 pcre32; + +struct real_pcre_jit_stack; /* declaration; the definition is private */ +typedef struct real_pcre_jit_stack pcre_jit_stack; + +struct real_pcre16_jit_stack; /* declaration; the definition is private */ +typedef struct real_pcre16_jit_stack pcre16_jit_stack; + +struct real_pcre32_jit_stack; /* declaration; the definition is private */ +typedef struct real_pcre32_jit_stack pcre32_jit_stack; + +/* If PCRE is compiled with 16 bit character support, PCRE_UCHAR16 must contain +a 16 bit wide signed data type. Otherwise it can be a dummy data type since +pcre16 functions are not implemented. There is a check for this in pcre_internal.h. */ +#ifndef PCRE_UCHAR16 +#define PCRE_UCHAR16 unsigned short +#endif + +#ifndef PCRE_SPTR16 +#define PCRE_SPTR16 const PCRE_UCHAR16 * +#endif + +/* If PCRE is compiled with 32 bit character support, PCRE_UCHAR32 must contain +a 32 bit wide signed data type. Otherwise it can be a dummy data type since +pcre32 functions are not implemented. There is a check for this in pcre_internal.h. */ +#ifndef PCRE_UCHAR32 +#define PCRE_UCHAR32 unsigned int +#endif + +#ifndef PCRE_SPTR32 +#define PCRE_SPTR32 const PCRE_UCHAR32 * +#endif + +/* When PCRE is compiled as a C++ library, the subject pointer type can be +replaced with a custom type. For conventional use, the public interface is a +const char *. */ + +#ifndef PCRE_SPTR +#define PCRE_SPTR const char * +#endif + +/* The structure for passing additional data to pcre_exec(). This is defined in +such as way as to be extensible. Always add new fields at the end, in order to +remain compatible. */ + +typedef struct pcre_extra { + unsigned long int flags; /* Bits for which fields are set */ + void *study_data; /* Opaque data from pcre_study() */ + unsigned long int match_limit; /* Maximum number of calls to match() */ + void *callout_data; /* Data passed back in callouts */ + const unsigned char *tables; /* Pointer to character tables */ + unsigned long int match_limit_recursion; /* Max recursive calls to match() */ + unsigned char **mark; /* For passing back a mark pointer */ + void *executable_jit; /* Contains a pointer to a compiled jit code */ +} pcre_extra; + +/* Same structure as above, but with 16 bit char pointers. */ + +typedef struct pcre16_extra { + unsigned long int flags; /* Bits for which fields are set */ + void *study_data; /* Opaque data from pcre_study() */ + unsigned long int match_limit; /* Maximum number of calls to match() */ + void *callout_data; /* Data passed back in callouts */ + const unsigned char *tables; /* Pointer to character tables */ + unsigned long int match_limit_recursion; /* Max recursive calls to match() */ + PCRE_UCHAR16 **mark; /* For passing back a mark pointer */ + void *executable_jit; /* Contains a pointer to a compiled jit code */ +} pcre16_extra; + +/* Same structure as above, but with 32 bit char pointers. */ + +typedef struct pcre32_extra { + unsigned long int flags; /* Bits for which fields are set */ + void *study_data; /* Opaque data from pcre_study() */ + unsigned long int match_limit; /* Maximum number of calls to match() */ + void *callout_data; /* Data passed back in callouts */ + const unsigned char *tables; /* Pointer to character tables */ + unsigned long int match_limit_recursion; /* Max recursive calls to match() */ + PCRE_UCHAR32 **mark; /* For passing back a mark pointer */ + void *executable_jit; /* Contains a pointer to a compiled jit code */ +} pcre32_extra; + +/* The structure for passing out data via the pcre_callout_function. We use a +structure so that new fields can be added on the end in future versions, +without changing the API of the function, thereby allowing old clients to work +without modification. */ + +typedef struct pcre_callout_block { + int version; /* Identifies version of block */ + /* ------------------------ Version 0 ------------------------------- */ + int callout_number; /* Number compiled into pattern */ + int *offset_vector; /* The offset vector */ + PCRE_SPTR subject; /* The subject being matched */ + int subject_length; /* The length of the subject */ + int start_match; /* Offset to start of this match attempt */ + int current_position; /* Where we currently are in the subject */ + int capture_top; /* Max current capture */ + int capture_last; /* Most recently closed capture */ + void *callout_data; /* Data passed in with the call */ + /* ------------------- Added for Version 1 -------------------------- */ + int pattern_position; /* Offset to next item in the pattern */ + int next_item_length; /* Length of next item in the pattern */ + /* ------------------- Added for Version 2 -------------------------- */ + const unsigned char *mark; /* Pointer to current mark or NULL */ + /* ------------------------------------------------------------------ */ +} pcre_callout_block; + +/* Same structure as above, but with 16 bit char pointers. */ + +typedef struct pcre16_callout_block { + int version; /* Identifies version of block */ + /* ------------------------ Version 0 ------------------------------- */ + int callout_number; /* Number compiled into pattern */ + int *offset_vector; /* The offset vector */ + PCRE_SPTR16 subject; /* The subject being matched */ + int subject_length; /* The length of the subject */ + int start_match; /* Offset to start of this match attempt */ + int current_position; /* Where we currently are in the subject */ + int capture_top; /* Max current capture */ + int capture_last; /* Most recently closed capture */ + void *callout_data; /* Data passed in with the call */ + /* ------------------- Added for Version 1 -------------------------- */ + int pattern_position; /* Offset to next item in the pattern */ + int next_item_length; /* Length of next item in the pattern */ + /* ------------------- Added for Version 2 -------------------------- */ + const PCRE_UCHAR16 *mark; /* Pointer to current mark or NULL */ + /* ------------------------------------------------------------------ */ +} pcre16_callout_block; + +/* Same structure as above, but with 32 bit char pointers. */ + +typedef struct pcre32_callout_block { + int version; /* Identifies version of block */ + /* ------------------------ Version 0 ------------------------------- */ + int callout_number; /* Number compiled into pattern */ + int *offset_vector; /* The offset vector */ + PCRE_SPTR32 subject; /* The subject being matched */ + int subject_length; /* The length of the subject */ + int start_match; /* Offset to start of this match attempt */ + int current_position; /* Where we currently are in the subject */ + int capture_top; /* Max current capture */ + int capture_last; /* Most recently closed capture */ + void *callout_data; /* Data passed in with the call */ + /* ------------------- Added for Version 1 -------------------------- */ + int pattern_position; /* Offset to next item in the pattern */ + int next_item_length; /* Length of next item in the pattern */ + /* ------------------- Added for Version 2 -------------------------- */ + const PCRE_UCHAR32 *mark; /* Pointer to current mark or NULL */ + /* ------------------------------------------------------------------ */ +} pcre32_callout_block; + +/* Indirection for store get and free functions. These can be set to +alternative malloc/free functions if required. Special ones are used in the +non-recursive case for "frames". There is also an optional callout function +that is triggered by the (?) regex item. For Virtual Pascal, these definitions +have to take another form. */ + +#ifndef VPCOMPAT +PCRE_EXP_DECL void *(*pcre_malloc)(size_t); +PCRE_EXP_DECL void (*pcre_free)(void *); +PCRE_EXP_DECL void *(*pcre_stack_malloc)(size_t); +PCRE_EXP_DECL void (*pcre_stack_free)(void *); +PCRE_EXP_DECL int (*pcre_callout)(pcre_callout_block *); +PCRE_EXP_DECL int (*pcre_stack_guard)(void); + +PCRE_EXP_DECL void *(*pcre16_malloc)(size_t); +PCRE_EXP_DECL void (*pcre16_free)(void *); +PCRE_EXP_DECL void *(*pcre16_stack_malloc)(size_t); +PCRE_EXP_DECL void (*pcre16_stack_free)(void *); +PCRE_EXP_DECL int (*pcre16_callout)(pcre16_callout_block *); +PCRE_EXP_DECL int (*pcre16_stack_guard)(void); + +PCRE_EXP_DECL void *(*pcre32_malloc)(size_t); +PCRE_EXP_DECL void (*pcre32_free)(void *); +PCRE_EXP_DECL void *(*pcre32_stack_malloc)(size_t); +PCRE_EXP_DECL void (*pcre32_stack_free)(void *); +PCRE_EXP_DECL int (*pcre32_callout)(pcre32_callout_block *); +PCRE_EXP_DECL int (*pcre32_stack_guard)(void); +#else /* VPCOMPAT */ +PCRE_EXP_DECL void *pcre_malloc(size_t); +PCRE_EXP_DECL void pcre_free(void *); +PCRE_EXP_DECL void *pcre_stack_malloc(size_t); +PCRE_EXP_DECL void pcre_stack_free(void *); +PCRE_EXP_DECL int pcre_callout(pcre_callout_block *); +PCRE_EXP_DECL int pcre_stack_guard(void); + +PCRE_EXP_DECL void *pcre16_malloc(size_t); +PCRE_EXP_DECL void pcre16_free(void *); +PCRE_EXP_DECL void *pcre16_stack_malloc(size_t); +PCRE_EXP_DECL void pcre16_stack_free(void *); +PCRE_EXP_DECL int pcre16_callout(pcre16_callout_block *); +PCRE_EXP_DECL int pcre16_stack_guard(void); + +PCRE_EXP_DECL void *pcre32_malloc(size_t); +PCRE_EXP_DECL void pcre32_free(void *); +PCRE_EXP_DECL void *pcre32_stack_malloc(size_t); +PCRE_EXP_DECL void pcre32_stack_free(void *); +PCRE_EXP_DECL int pcre32_callout(pcre32_callout_block *); +PCRE_EXP_DECL int pcre32_stack_guard(void); +#endif /* VPCOMPAT */ + +/* User defined callback which provides a stack just before the match starts. */ + +typedef pcre_jit_stack *(*pcre_jit_callback)(void *); +typedef pcre16_jit_stack *(*pcre16_jit_callback)(void *); +typedef pcre32_jit_stack *(*pcre32_jit_callback)(void *); + +/* Exported PCRE functions */ + +PCRE_EXP_DECL pcre *pcre_compile(const char *, int, const char **, int *, + const unsigned char *); +PCRE_EXP_DECL pcre16 *pcre16_compile(PCRE_SPTR16, int, const char **, int *, + const unsigned char *); +PCRE_EXP_DECL pcre32 *pcre32_compile(PCRE_SPTR32, int, const char **, int *, + const unsigned char *); +PCRE_EXP_DECL pcre *pcre_compile2(const char *, int, int *, const char **, + int *, const unsigned char *); +PCRE_EXP_DECL pcre16 *pcre16_compile2(PCRE_SPTR16, int, int *, const char **, + int *, const unsigned char *); +PCRE_EXP_DECL pcre32 *pcre32_compile2(PCRE_SPTR32, int, int *, const char **, + int *, const unsigned char *); +PCRE_EXP_DECL int pcre_config(int, void *); +PCRE_EXP_DECL int pcre16_config(int, void *); +PCRE_EXP_DECL int pcre32_config(int, void *); +PCRE_EXP_DECL int pcre_copy_named_substring(const pcre *, const char *, + int *, int, const char *, char *, int); +PCRE_EXP_DECL int pcre16_copy_named_substring(const pcre16 *, PCRE_SPTR16, + int *, int, PCRE_SPTR16, PCRE_UCHAR16 *, int); +PCRE_EXP_DECL int pcre32_copy_named_substring(const pcre32 *, PCRE_SPTR32, + int *, int, PCRE_SPTR32, PCRE_UCHAR32 *, int); +PCRE_EXP_DECL int pcre_copy_substring(const char *, int *, int, int, + char *, int); +PCRE_EXP_DECL int pcre16_copy_substring(PCRE_SPTR16, int *, int, int, + PCRE_UCHAR16 *, int); +PCRE_EXP_DECL int pcre32_copy_substring(PCRE_SPTR32, int *, int, int, + PCRE_UCHAR32 *, int); +PCRE_EXP_DECL int pcre_dfa_exec(const pcre *, const pcre_extra *, + const char *, int, int, int, int *, int , int *, int); +PCRE_EXP_DECL int pcre16_dfa_exec(const pcre16 *, const pcre16_extra *, + PCRE_SPTR16, int, int, int, int *, int , int *, int); +PCRE_EXP_DECL int pcre32_dfa_exec(const pcre32 *, const pcre32_extra *, + PCRE_SPTR32, int, int, int, int *, int , int *, int); +PCRE_EXP_DECL int pcre_exec(const pcre *, const pcre_extra *, PCRE_SPTR, + int, int, int, int *, int); +PCRE_EXP_DECL int pcre16_exec(const pcre16 *, const pcre16_extra *, + PCRE_SPTR16, int, int, int, int *, int); +PCRE_EXP_DECL int pcre32_exec(const pcre32 *, const pcre32_extra *, + PCRE_SPTR32, int, int, int, int *, int); +PCRE_EXP_DECL int pcre_jit_exec(const pcre *, const pcre_extra *, + PCRE_SPTR, int, int, int, int *, int, + pcre_jit_stack *); +PCRE_EXP_DECL int pcre16_jit_exec(const pcre16 *, const pcre16_extra *, + PCRE_SPTR16, int, int, int, int *, int, + pcre16_jit_stack *); +PCRE_EXP_DECL int pcre32_jit_exec(const pcre32 *, const pcre32_extra *, + PCRE_SPTR32, int, int, int, int *, int, + pcre32_jit_stack *); +PCRE_EXP_DECL void pcre_free_substring(const char *); +PCRE_EXP_DECL void pcre16_free_substring(PCRE_SPTR16); +PCRE_EXP_DECL void pcre32_free_substring(PCRE_SPTR32); +PCRE_EXP_DECL void pcre_free_substring_list(const char **); +PCRE_EXP_DECL void pcre16_free_substring_list(PCRE_SPTR16 *); +PCRE_EXP_DECL void pcre32_free_substring_list(PCRE_SPTR32 *); +PCRE_EXP_DECL int pcre_fullinfo(const pcre *, const pcre_extra *, int, + void *); +PCRE_EXP_DECL int pcre16_fullinfo(const pcre16 *, const pcre16_extra *, int, + void *); +PCRE_EXP_DECL int pcre32_fullinfo(const pcre32 *, const pcre32_extra *, int, + void *); +PCRE_EXP_DECL int pcre_get_named_substring(const pcre *, const char *, + int *, int, const char *, const char **); +PCRE_EXP_DECL int pcre16_get_named_substring(const pcre16 *, PCRE_SPTR16, + int *, int, PCRE_SPTR16, PCRE_SPTR16 *); +PCRE_EXP_DECL int pcre32_get_named_substring(const pcre32 *, PCRE_SPTR32, + int *, int, PCRE_SPTR32, PCRE_SPTR32 *); +PCRE_EXP_DECL int pcre_get_stringnumber(const pcre *, const char *); +PCRE_EXP_DECL int pcre16_get_stringnumber(const pcre16 *, PCRE_SPTR16); +PCRE_EXP_DECL int pcre32_get_stringnumber(const pcre32 *, PCRE_SPTR32); +PCRE_EXP_DECL int pcre_get_stringtable_entries(const pcre *, const char *, + char **, char **); +PCRE_EXP_DECL int pcre16_get_stringtable_entries(const pcre16 *, PCRE_SPTR16, + PCRE_UCHAR16 **, PCRE_UCHAR16 **); +PCRE_EXP_DECL int pcre32_get_stringtable_entries(const pcre32 *, PCRE_SPTR32, + PCRE_UCHAR32 **, PCRE_UCHAR32 **); +PCRE_EXP_DECL int pcre_get_substring(const char *, int *, int, int, + const char **); +PCRE_EXP_DECL int pcre16_get_substring(PCRE_SPTR16, int *, int, int, + PCRE_SPTR16 *); +PCRE_EXP_DECL int pcre32_get_substring(PCRE_SPTR32, int *, int, int, + PCRE_SPTR32 *); +PCRE_EXP_DECL int pcre_get_substring_list(const char *, int *, int, + const char ***); +PCRE_EXP_DECL int pcre16_get_substring_list(PCRE_SPTR16, int *, int, + PCRE_SPTR16 **); +PCRE_EXP_DECL int pcre32_get_substring_list(PCRE_SPTR32, int *, int, + PCRE_SPTR32 **); +PCRE_EXP_DECL const unsigned char *pcre_maketables(void); +PCRE_EXP_DECL const unsigned char *pcre16_maketables(void); +PCRE_EXP_DECL const unsigned char *pcre32_maketables(void); +PCRE_EXP_DECL int pcre_refcount(pcre *, int); +PCRE_EXP_DECL int pcre16_refcount(pcre16 *, int); +PCRE_EXP_DECL int pcre32_refcount(pcre32 *, int); +PCRE_EXP_DECL pcre_extra *pcre_study(const pcre *, int, const char **); +PCRE_EXP_DECL pcre16_extra *pcre16_study(const pcre16 *, int, const char **); +PCRE_EXP_DECL pcre32_extra *pcre32_study(const pcre32 *, int, const char **); +PCRE_EXP_DECL void pcre_free_study(pcre_extra *); +PCRE_EXP_DECL void pcre16_free_study(pcre16_extra *); +PCRE_EXP_DECL void pcre32_free_study(pcre32_extra *); +PCRE_EXP_DECL const char *pcre_version(void); +PCRE_EXP_DECL const char *pcre16_version(void); +PCRE_EXP_DECL const char *pcre32_version(void); + +/* Utility functions for byte order swaps. */ +PCRE_EXP_DECL int pcre_pattern_to_host_byte_order(pcre *, pcre_extra *, + const unsigned char *); +PCRE_EXP_DECL int pcre16_pattern_to_host_byte_order(pcre16 *, pcre16_extra *, + const unsigned char *); +PCRE_EXP_DECL int pcre32_pattern_to_host_byte_order(pcre32 *, pcre32_extra *, + const unsigned char *); +PCRE_EXP_DECL int pcre16_utf16_to_host_byte_order(PCRE_UCHAR16 *, + PCRE_SPTR16, int, int *, int); +PCRE_EXP_DECL int pcre32_utf32_to_host_byte_order(PCRE_UCHAR32 *, + PCRE_SPTR32, int, int *, int); + +/* JIT compiler related functions. */ + +PCRE_EXP_DECL pcre_jit_stack *pcre_jit_stack_alloc(int, int); +PCRE_EXP_DECL pcre16_jit_stack *pcre16_jit_stack_alloc(int, int); +PCRE_EXP_DECL pcre32_jit_stack *pcre32_jit_stack_alloc(int, int); +PCRE_EXP_DECL void pcre_jit_stack_free(pcre_jit_stack *); +PCRE_EXP_DECL void pcre16_jit_stack_free(pcre16_jit_stack *); +PCRE_EXP_DECL void pcre32_jit_stack_free(pcre32_jit_stack *); +PCRE_EXP_DECL void pcre_assign_jit_stack(pcre_extra *, + pcre_jit_callback, void *); +PCRE_EXP_DECL void pcre16_assign_jit_stack(pcre16_extra *, + pcre16_jit_callback, void *); +PCRE_EXP_DECL void pcre32_assign_jit_stack(pcre32_extra *, + pcre32_jit_callback, void *); +PCRE_EXP_DECL void pcre_jit_free_unused_memory(void); +PCRE_EXP_DECL void pcre16_jit_free_unused_memory(void); +PCRE_EXP_DECL void pcre32_jit_free_unused_memory(void); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* End of pcre.h */ diff --git a/deps/pcre/pcre_byte_order.c b/deps/pcre/pcre_byte_order.c new file mode 100644 index 00000000000..cf5f12b04ea --- /dev/null +++ b/deps/pcre/pcre_byte_order.c @@ -0,0 +1,319 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2014 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains an internal function that tests a compiled pattern to +see if it was compiled with the opposite endianness. If so, it uses an +auxiliary local function to flip the appropriate bytes. */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pcre_internal.h" + + +/************************************************* +* Swap byte functions * +*************************************************/ + +/* The following functions swap the bytes of a pcre_uint16 +and pcre_uint32 value. + +Arguments: + value any number + +Returns: the byte swapped value +*/ + +static pcre_uint32 +swap_uint32(pcre_uint32 value) +{ +return ((value & 0x000000ff) << 24) | + ((value & 0x0000ff00) << 8) | + ((value & 0x00ff0000) >> 8) | + (value >> 24); +} + +static pcre_uint16 +swap_uint16(pcre_uint16 value) +{ +return (value >> 8) | (value << 8); +} + + +/************************************************* +* Test for a byte-flipped compiled regex * +*************************************************/ + +/* This function swaps the bytes of a compiled pattern usually +loaded form the disk. It also sets the tables pointer, which +is likely an invalid pointer after reload. + +Arguments: + argument_re points to the compiled expression + extra_data points to extra data or is NULL + tables points to the character tables or NULL + +Returns: 0 if the swap is successful, negative on error +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DECL int pcre_pattern_to_host_byte_order(pcre *argument_re, + pcre_extra *extra_data, const unsigned char *tables) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DECL int pcre16_pattern_to_host_byte_order(pcre16 *argument_re, + pcre16_extra *extra_data, const unsigned char *tables) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DECL int pcre32_pattern_to_host_byte_order(pcre32 *argument_re, + pcre32_extra *extra_data, const unsigned char *tables) +#endif +{ +REAL_PCRE *re = (REAL_PCRE *)argument_re; +pcre_study_data *study; +#ifndef COMPILE_PCRE8 +pcre_uchar *ptr; +int length; +#if defined SUPPORT_UTF && defined COMPILE_PCRE16 +BOOL utf; +BOOL utf16_char; +#endif /* SUPPORT_UTF && COMPILE_PCRE16 */ +#endif /* !COMPILE_PCRE8 */ + +if (re == NULL) return PCRE_ERROR_NULL; +if (re->magic_number == MAGIC_NUMBER) + { + if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE; + re->tables = tables; + return 0; + } + +if (re->magic_number != REVERSED_MAGIC_NUMBER) return PCRE_ERROR_BADMAGIC; +if ((swap_uint32(re->flags) & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE; + +re->magic_number = MAGIC_NUMBER; +re->size = swap_uint32(re->size); +re->options = swap_uint32(re->options); +re->flags = swap_uint32(re->flags); +re->limit_match = swap_uint32(re->limit_match); +re->limit_recursion = swap_uint32(re->limit_recursion); + +#if defined COMPILE_PCRE8 || defined COMPILE_PCRE16 +re->first_char = swap_uint16(re->first_char); +re->req_char = swap_uint16(re->req_char); +#elif defined COMPILE_PCRE32 +re->first_char = swap_uint32(re->first_char); +re->req_char = swap_uint32(re->req_char); +#endif + +re->max_lookbehind = swap_uint16(re->max_lookbehind); +re->top_bracket = swap_uint16(re->top_bracket); +re->top_backref = swap_uint16(re->top_backref); +re->name_table_offset = swap_uint16(re->name_table_offset); +re->name_entry_size = swap_uint16(re->name_entry_size); +re->name_count = swap_uint16(re->name_count); +re->ref_count = swap_uint16(re->ref_count); +re->tables = tables; + +if (extra_data != NULL && (extra_data->flags & PCRE_EXTRA_STUDY_DATA) != 0) + { + study = (pcre_study_data *)extra_data->study_data; + study->size = swap_uint32(study->size); + study->flags = swap_uint32(study->flags); + study->minlength = swap_uint32(study->minlength); + } + +#ifndef COMPILE_PCRE8 +ptr = (pcre_uchar *)re + re->name_table_offset; +length = re->name_count * re->name_entry_size; +#if defined SUPPORT_UTF && defined COMPILE_PCRE16 +utf = (re->options & PCRE_UTF16) != 0; +utf16_char = FALSE; +#endif /* SUPPORT_UTF && COMPILE_PCRE16 */ + +while(TRUE) + { + /* Swap previous characters. */ + while (length-- > 0) + { +#if defined COMPILE_PCRE16 + *ptr = swap_uint16(*ptr); +#elif defined COMPILE_PCRE32 + *ptr = swap_uint32(*ptr); +#endif + ptr++; + } +#if defined SUPPORT_UTF && defined COMPILE_PCRE16 + if (utf16_char) + { + if (HAS_EXTRALEN(ptr[-1])) + { + /* We know that there is only one extra character in UTF-16. */ + *ptr = swap_uint16(*ptr); + ptr++; + } + } + utf16_char = FALSE; +#endif /* SUPPORT_UTF */ + + /* Get next opcode. */ + length = 0; +#if defined COMPILE_PCRE16 + *ptr = swap_uint16(*ptr); +#elif defined COMPILE_PCRE32 + *ptr = swap_uint32(*ptr); +#endif + switch (*ptr) + { + case OP_END: + return 0; + +#if defined SUPPORT_UTF && defined COMPILE_PCRE16 + case OP_CHAR: + case OP_CHARI: + case OP_NOT: + case OP_NOTI: + case OP_STAR: + case OP_MINSTAR: + case OP_PLUS: + case OP_MINPLUS: + case OP_QUERY: + case OP_MINQUERY: + case OP_UPTO: + case OP_MINUPTO: + case OP_EXACT: + case OP_POSSTAR: + case OP_POSPLUS: + case OP_POSQUERY: + case OP_POSUPTO: + case OP_STARI: + case OP_MINSTARI: + case OP_PLUSI: + case OP_MINPLUSI: + case OP_QUERYI: + case OP_MINQUERYI: + case OP_UPTOI: + case OP_MINUPTOI: + case OP_EXACTI: + case OP_POSSTARI: + case OP_POSPLUSI: + case OP_POSQUERYI: + case OP_POSUPTOI: + case OP_NOTSTAR: + case OP_NOTMINSTAR: + case OP_NOTPLUS: + case OP_NOTMINPLUS: + case OP_NOTQUERY: + case OP_NOTMINQUERY: + case OP_NOTUPTO: + case OP_NOTMINUPTO: + case OP_NOTEXACT: + case OP_NOTPOSSTAR: + case OP_NOTPOSPLUS: + case OP_NOTPOSQUERY: + case OP_NOTPOSUPTO: + case OP_NOTSTARI: + case OP_NOTMINSTARI: + case OP_NOTPLUSI: + case OP_NOTMINPLUSI: + case OP_NOTQUERYI: + case OP_NOTMINQUERYI: + case OP_NOTUPTOI: + case OP_NOTMINUPTOI: + case OP_NOTEXACTI: + case OP_NOTPOSSTARI: + case OP_NOTPOSPLUSI: + case OP_NOTPOSQUERYI: + case OP_NOTPOSUPTOI: + if (utf) utf16_char = TRUE; +#endif + /* Fall through. */ + + default: + length = PRIV(OP_lengths)[*ptr] - 1; + break; + + case OP_CLASS: + case OP_NCLASS: + /* Skip the character bit map. */ + ptr += 32/sizeof(pcre_uchar); + length = 0; + break; + + case OP_XCLASS: + /* Reverse the size of the XCLASS instance. */ + ptr++; +#if defined COMPILE_PCRE16 + *ptr = swap_uint16(*ptr); +#elif defined COMPILE_PCRE32 + *ptr = swap_uint32(*ptr); +#endif +#ifndef COMPILE_PCRE32 + if (LINK_SIZE > 1) + { + /* LINK_SIZE can be 1 or 2 in 16 bit mode. */ + ptr++; + *ptr = swap_uint16(*ptr); + } +#endif + ptr++; + length = (GET(ptr, -LINK_SIZE)) - (1 + LINK_SIZE + 1); +#if defined COMPILE_PCRE16 + *ptr = swap_uint16(*ptr); +#elif defined COMPILE_PCRE32 + *ptr = swap_uint32(*ptr); +#endif + if ((*ptr & XCL_MAP) != 0) + { + /* Skip the character bit map. */ + ptr += 32/sizeof(pcre_uchar); + length -= 32/sizeof(pcre_uchar); + } + break; + } + ptr++; + } +/* Control should never reach here in 16/32 bit mode. */ +#else /* In 8-bit mode, the pattern does not need to be processed. */ +return 0; +#endif /* !COMPILE_PCRE8 */ +} + +/* End of pcre_byte_order.c */ diff --git a/deps/pcre/pcre_chartables.c b/deps/pcre/pcre_chartables.c new file mode 100644 index 00000000000..1e20ec29d05 --- /dev/null +++ b/deps/pcre/pcre_chartables.c @@ -0,0 +1,198 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* This file contains character tables that are used when no external tables +are passed to PCRE by the application that calls it. The tables are used only +for characters whose code values are less than 256. + +This is a default version of the tables that assumes ASCII encoding. A program +called dftables (which is distributed with PCRE) can be used to build +alternative versions of this file. This is necessary if you are running in an +EBCDIC environment, or if you want to default to a different encoding, for +example ISO-8859-1. When dftables is run, it creates these tables in the +current locale. If PCRE is configured with --enable-rebuild-chartables, this +happens automatically. + +The following #includes are present because without them gcc 4.x may remove the +array definition from the final binary if PCRE is built into a static library +and dead code stripping is activated. This leads to link errors. Pulling in the +header ensures that the array gets flagged as "someone outside this compilation +unit might reference this" and so it will always be supplied to the linker. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pcre_internal.h" + +const pcre_uint8 PRIV(default_tables)[] = { + +/* This table is a lower casing table. */ + + 0, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, + 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, + 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 58, 59, 60, 61, 62, 63, + 64, 97, 98, 99,100,101,102,103, + 104,105,106,107,108,109,110,111, + 112,113,114,115,116,117,118,119, + 120,121,122, 91, 92, 93, 94, 95, + 96, 97, 98, 99,100,101,102,103, + 104,105,106,107,108,109,110,111, + 112,113,114,115,116,117,118,119, + 120,121,122,123,124,125,126,127, + 128,129,130,131,132,133,134,135, + 136,137,138,139,140,141,142,143, + 144,145,146,147,148,149,150,151, + 152,153,154,155,156,157,158,159, + 160,161,162,163,164,165,166,167, + 168,169,170,171,172,173,174,175, + 176,177,178,179,180,181,182,183, + 184,185,186,187,188,189,190,191, + 192,193,194,195,196,197,198,199, + 200,201,202,203,204,205,206,207, + 208,209,210,211,212,213,214,215, + 216,217,218,219,220,221,222,223, + 224,225,226,227,228,229,230,231, + 232,233,234,235,236,237,238,239, + 240,241,242,243,244,245,246,247, + 248,249,250,251,252,253,254,255, + +/* This table is a case flipping table. */ + + 0, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, + 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, + 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 58, 59, 60, 61, 62, 63, + 64, 97, 98, 99,100,101,102,103, + 104,105,106,107,108,109,110,111, + 112,113,114,115,116,117,118,119, + 120,121,122, 91, 92, 93, 94, 95, + 96, 65, 66, 67, 68, 69, 70, 71, + 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, + 88, 89, 90,123,124,125,126,127, + 128,129,130,131,132,133,134,135, + 136,137,138,139,140,141,142,143, + 144,145,146,147,148,149,150,151, + 152,153,154,155,156,157,158,159, + 160,161,162,163,164,165,166,167, + 168,169,170,171,172,173,174,175, + 176,177,178,179,180,181,182,183, + 184,185,186,187,188,189,190,191, + 192,193,194,195,196,197,198,199, + 200,201,202,203,204,205,206,207, + 208,209,210,211,212,213,214,215, + 216,217,218,219,220,221,222,223, + 224,225,226,227,228,229,230,231, + 232,233,234,235,236,237,238,239, + 240,241,242,243,244,245,246,247, + 248,249,250,251,252,253,254,255, + +/* This table contains bit maps for various character classes. Each map is 32 +bytes long and the bits run from the least significant end of each byte. The +classes that have their own maps are: space, xdigit, digit, upper, lower, word, +graph, print, punct, and cntrl. Other classes are built from combinations. */ + + 0x00,0x3e,0x00,0x00,0x01,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + + 0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x03, + 0x7e,0x00,0x00,0x00,0x7e,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + + 0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x03, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0xfe,0xff,0xff,0x07,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0xfe,0xff,0xff,0x07, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + + 0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x03, + 0xfe,0xff,0xff,0x87,0xfe,0xff,0xff,0x07, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + + 0x00,0x00,0x00,0x00,0xfe,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + + 0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + + 0x00,0x00,0x00,0x00,0xfe,0xff,0x00,0xfc, + 0x01,0x00,0x00,0xf8,0x01,0x00,0x00,0x78, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + + 0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + +/* This table identifies various classes of character by individual bits: + 0x01 white space character + 0x02 letter + 0x04 decimal digit + 0x08 hexadecimal digit + 0x10 alphanumeric or '_' + 0x80 regular expression metacharacter or binary zero +*/ + + 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */ + 0x00,0x01,0x01,0x01,0x01,0x01,0x00,0x00, /* 8- 15 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */ + 0x01,0x00,0x00,0x00,0x80,0x00,0x00,0x00, /* - ' */ + 0x80,0x80,0x80,0x80,0x00,0x00,0x80,0x00, /* ( - / */ + 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */ + 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x80, /* 8 - ? */ + 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* @ - G */ + 0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* H - O */ + 0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* P - W */ + 0x12,0x12,0x12,0x80,0x80,0x00,0x80,0x10, /* X - _ */ + 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* ` - g */ + 0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* h - o */ + 0x12,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* p - w */ + 0x12,0x12,0x12,0x80,0x80,0x00,0x00,0x00, /* x -127 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */ + +/* End of pcre_chartables.c */ diff --git a/deps/pcre/pcre_compile.c b/deps/pcre/pcre_compile.c new file mode 100644 index 00000000000..43f852f4627 --- /dev/null +++ b/deps/pcre/pcre_compile.c @@ -0,0 +1,9815 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2021 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains the external function pcre_compile(), along with +supporting internal functions that are not used by other modules. */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#define NLBLOCK cd /* Block containing newline information */ +#define PSSTART start_pattern /* Field containing pattern start */ +#define PSEND end_pattern /* Field containing pattern end */ + +#include "pcre_internal.h" + + +/* When PCRE_DEBUG is defined, we need the pcre(16|32)_printint() function, which +is also used by pcretest. PCRE_DEBUG is not defined when building a production +library. We do not need to select pcre16_printint.c specially, because the +COMPILE_PCREx macro will already be appropriately set. */ + +#ifdef PCRE_DEBUG +/* pcre_printint.c should not include any headers */ +#define PCRE_INCLUDED +#include "pcre_printint.c" +#undef PCRE_INCLUDED +#endif + + +/* Macro for setting individual bits in class bitmaps. */ + +#define SETBIT(a,b) a[(b)/8] |= (1U << ((b)&7)) + +/* Maximum length value to check against when making sure that the integer that +holds the compiled pattern length does not overflow. We make it a bit less than +INT_MAX to allow for adding in group terminating bytes, so that we don't have +to check them every time. */ + +#define OFLOW_MAX (INT_MAX - 20) + +/* Definitions to allow mutual recursion */ + +static int + add_list_to_class(pcre_uint8 *, pcre_uchar **, int, compile_data *, + const pcre_uint32 *, unsigned int); + +static BOOL + compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int, + pcre_uint32 *, pcre_int32 *, pcre_uint32 *, pcre_int32 *, branch_chain *, + compile_data *, int *); + + + +/************************************************* +* Code parameters and static tables * +*************************************************/ + +/* This value specifies the size of stack workspace that is used during the +first pre-compile phase that determines how much memory is required. The regex +is partly compiled into this space, but the compiled parts are discarded as +soon as they can be, so that hopefully there will never be an overrun. The code +does, however, check for an overrun. The largest amount I've seen used is 218, +so this number is very generous. + +The same workspace is used during the second, actual compile phase for +remembering forward references to groups so that they can be filled in at the +end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE +is 4 there is plenty of room for most patterns. However, the memory can get +filled up by repetitions of forward references, for example patterns like +/(?1){0,1999}(b)/, and one user did hit the limit. The code has been changed so +that the workspace is expanded using malloc() in this situation. The value +below is therefore a minimum, and we put a maximum on it for safety. The +minimum is now also defined in terms of LINK_SIZE so that the use of malloc() +kicks in at the same number of forward references in all cases. */ + +#define COMPILE_WORK_SIZE (2048*LINK_SIZE) +#define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE) + +/* This value determines the size of the initial vector that is used for +remembering named groups during the pre-compile. It is allocated on the stack, +but if it is too small, it is expanded using malloc(), in a similar way to the +workspace. The value is the number of slots in the list. */ + +#define NAMED_GROUP_LIST_SIZE 20 + +/* The overrun tests check for a slightly smaller size so that they detect the +overrun before it actually does run off the end of the data block. */ + +#define WORK_SIZE_SAFETY_MARGIN (100) + +/* Private flags added to firstchar and reqchar. */ + +#define REQ_CASELESS (1U << 0) /* Indicates caselessness */ +#define REQ_VARY (1U << 1) /* Reqchar followed non-literal item */ +/* Negative values for the firstchar and reqchar flags */ +#define REQ_UNSET (-2) +#define REQ_NONE (-1) + +/* Repeated character flags. */ + +#define UTF_LENGTH 0x10000000l /* The char contains its length. */ + +/* Table for handling escaped characters in the range '0'-'z'. Positive returns +are simple data values; negative values are for special things like \d and so +on. Zero means further processing is needed (for things like \x), or the escape +is invalid. */ + +#ifndef EBCDIC + +/* This is the "normal" table for ASCII systems or for EBCDIC systems running +in UTF-8 mode. */ + +static const short int escapes[] = { + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + CHAR_COLON, CHAR_SEMICOLON, + CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN, + CHAR_GREATER_THAN_SIGN, CHAR_QUESTION_MARK, + CHAR_COMMERCIAL_AT, -ESC_A, + -ESC_B, -ESC_C, + -ESC_D, -ESC_E, + 0, -ESC_G, + -ESC_H, 0, + 0, -ESC_K, + 0, 0, + -ESC_N, 0, + -ESC_P, -ESC_Q, + -ESC_R, -ESC_S, + 0, 0, + -ESC_V, -ESC_W, + -ESC_X, 0, + -ESC_Z, CHAR_LEFT_SQUARE_BRACKET, + CHAR_BACKSLASH, CHAR_RIGHT_SQUARE_BRACKET, + CHAR_CIRCUMFLEX_ACCENT, CHAR_UNDERSCORE, + CHAR_GRAVE_ACCENT, ESC_a, + -ESC_b, 0, + -ESC_d, ESC_e, + ESC_f, 0, + -ESC_h, 0, + 0, -ESC_k, + 0, 0, + ESC_n, 0, + -ESC_p, 0, + ESC_r, -ESC_s, + ESC_tee, 0, + -ESC_v, -ESC_w, + 0, 0, + -ESC_z +}; + +#else + +/* This is the "abnormal" table for EBCDIC systems without UTF-8 support. */ + +static const short int escapes[] = { +/* 48 */ 0, 0, 0, '.', '<', '(', '+', '|', +/* 50 */ '&', 0, 0, 0, 0, 0, 0, 0, +/* 58 */ 0, 0, '!', '$', '*', ')', ';', '~', +/* 60 */ '-', '/', 0, 0, 0, 0, 0, 0, +/* 68 */ 0, 0, '|', ',', '%', '_', '>', '?', +/* 70 */ 0, 0, 0, 0, 0, 0, 0, 0, +/* 78 */ 0, '`', ':', '#', '@', '\'', '=', '"', +/* 80 */ 0, ESC_a, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0, +/* 88 */-ESC_h, 0, 0, '{', 0, 0, 0, 0, +/* 90 */ 0, 0, -ESC_k, 0, 0, ESC_n, 0, -ESC_p, +/* 98 */ 0, ESC_r, 0, '}', 0, 0, 0, 0, +/* A0 */ 0, '~', -ESC_s, ESC_tee, 0,-ESC_v, -ESC_w, 0, +/* A8 */ 0,-ESC_z, 0, 0, 0, '[', 0, 0, +/* B0 */ 0, 0, 0, 0, 0, 0, 0, 0, +/* B8 */ 0, 0, 0, 0, 0, ']', '=', '-', +/* C0 */ '{',-ESC_A, -ESC_B, -ESC_C, -ESC_D,-ESC_E, 0, -ESC_G, +/* C8 */-ESC_H, 0, 0, 0, 0, 0, 0, 0, +/* D0 */ '}', 0, -ESC_K, 0, 0,-ESC_N, 0, -ESC_P, +/* D8 */-ESC_Q,-ESC_R, 0, 0, 0, 0, 0, 0, +/* E0 */ '\\', 0, -ESC_S, 0, 0,-ESC_V, -ESC_W, -ESC_X, +/* E8 */ 0,-ESC_Z, 0, 0, 0, 0, 0, 0, +/* F0 */ 0, 0, 0, 0, 0, 0, 0, 0, +/* F8 */ 0, 0, 0, 0, 0, 0, 0, 0 +}; + +/* We also need a table of characters that may follow \c in an EBCDIC +environment for characters 0-31. */ + +static unsigned char ebcdic_escape_c[] = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"; + +#endif + + +/* Table of special "verbs" like (*PRUNE). This is a short table, so it is +searched linearly. Put all the names into a single string, in order to reduce +the number of relocations when a shared library is dynamically linked. The +string is built from string macros so that it works in UTF-8 mode on EBCDIC +platforms. */ + +typedef struct verbitem { + int len; /* Length of verb name */ + int op; /* Op when no arg, or -1 if arg mandatory */ + int op_arg; /* Op when arg present, or -1 if not allowed */ +} verbitem; + +static const char verbnames[] = + "\0" /* Empty name is a shorthand for MARK */ + STRING_MARK0 + STRING_ACCEPT0 + STRING_COMMIT0 + STRING_F0 + STRING_FAIL0 + STRING_PRUNE0 + STRING_SKIP0 + STRING_THEN; + +static const verbitem verbs[] = { + { 0, -1, OP_MARK }, + { 4, -1, OP_MARK }, + { 6, OP_ACCEPT, -1 }, + { 6, OP_COMMIT, -1 }, + { 1, OP_FAIL, -1 }, + { 4, OP_FAIL, -1 }, + { 5, OP_PRUNE, OP_PRUNE_ARG }, + { 4, OP_SKIP, OP_SKIP_ARG }, + { 4, OP_THEN, OP_THEN_ARG } +}; + +static const int verbcount = sizeof(verbs)/sizeof(verbitem); + + +/* Substitutes for [[:<:]] and [[:>:]], which mean start and end of word in +another regex library. */ + +static const pcre_uchar sub_start_of_word[] = { + CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK, + CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w, CHAR_RIGHT_PARENTHESIS, '\0' }; + +static const pcre_uchar sub_end_of_word[] = { + CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK, + CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w, + CHAR_RIGHT_PARENTHESIS, '\0' }; + + +/* Tables of names of POSIX character classes and their lengths. The names are +now all in a single string, to reduce the number of relocations when a shared +library is dynamically loaded. The list of lengths is terminated by a zero +length entry. The first three must be alpha, lower, upper, as this is assumed +for handling case independence. The indices for graph, print, and punct are +needed, so identify them. */ + +static const char posix_names[] = + STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0 + STRING_ascii0 STRING_blank0 STRING_cntrl0 STRING_digit0 + STRING_graph0 STRING_print0 STRING_punct0 STRING_space0 + STRING_word0 STRING_xdigit; + +static const pcre_uint8 posix_name_lengths[] = { + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 }; + +#define PC_GRAPH 8 +#define PC_PRINT 9 +#define PC_PUNCT 10 + + +/* Table of class bit maps for each POSIX class. Each class is formed from a +base map, with an optional addition or removal of another map. Then, for some +classes, there is some additional tweaking: for [:blank:] the vertical space +characters are removed, and for [:alpha:] and [:alnum:] the underscore +character is removed. The triples in the table consist of the base map offset, +second map offset or -1 if no second map, and a non-negative value for map +addition or a negative value for map subtraction (if there are two maps). The +absolute value of the third field has these meanings: 0 => no tweaking, 1 => +remove vertical space characters, 2 => remove underscore. */ + +static const int posix_class_maps[] = { + cbit_word, cbit_digit, -2, /* alpha */ + cbit_lower, -1, 0, /* lower */ + cbit_upper, -1, 0, /* upper */ + cbit_word, -1, 2, /* alnum - word without underscore */ + cbit_print, cbit_cntrl, 0, /* ascii */ + cbit_space, -1, 1, /* blank - a GNU extension */ + cbit_cntrl, -1, 0, /* cntrl */ + cbit_digit, -1, 0, /* digit */ + cbit_graph, -1, 0, /* graph */ + cbit_print, -1, 0, /* print */ + cbit_punct, -1, 0, /* punct */ + cbit_space, -1, 0, /* space */ + cbit_word, -1, 0, /* word - a Perl extension */ + cbit_xdigit,-1, 0 /* xdigit */ +}; + +/* Table of substitutes for \d etc when PCRE_UCP is set. They are replaced by +Unicode property escapes. */ + +#ifdef SUPPORT_UCP +static const pcre_uchar string_PNd[] = { + CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET, + CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_pNd[] = { + CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET, + CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_PXsp[] = { + CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET, + CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_pXsp[] = { + CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET, + CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_PXwd[] = { + CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET, + CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_pXwd[] = { + CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET, + CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' }; + +static const pcre_uchar *substitutes[] = { + string_PNd, /* \D */ + string_pNd, /* \d */ + string_PXsp, /* \S */ /* Xsp is Perl space, but from 8.34, Perl */ + string_pXsp, /* \s */ /* space and POSIX space are the same. */ + string_PXwd, /* \W */ + string_pXwd /* \w */ +}; + +/* The POSIX class substitutes must be in the order of the POSIX class names, +defined above, and there are both positive and negative cases. NULL means no +general substitute of a Unicode property escape (\p or \P). However, for some +POSIX classes (e.g. graph, print, punct) a special property code is compiled +directly. */ + +static const pcre_uchar string_pL[] = { + CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET, + CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_pLl[] = { + CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET, + CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_pLu[] = { + CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET, + CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_pXan[] = { + CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET, + CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_h[] = { + CHAR_BACKSLASH, CHAR_h, '\0' }; +static const pcre_uchar string_pXps[] = { + CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET, + CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_PL[] = { + CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET, + CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_PLl[] = { + CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET, + CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_PLu[] = { + CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET, + CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_PXan[] = { + CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET, + CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' }; +static const pcre_uchar string_H[] = { + CHAR_BACKSLASH, CHAR_H, '\0' }; +static const pcre_uchar string_PXps[] = { + CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET, + CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' }; + +static const pcre_uchar *posix_substitutes[] = { + string_pL, /* alpha */ + string_pLl, /* lower */ + string_pLu, /* upper */ + string_pXan, /* alnum */ + NULL, /* ascii */ + string_h, /* blank */ + NULL, /* cntrl */ + string_pNd, /* digit */ + NULL, /* graph */ + NULL, /* print */ + NULL, /* punct */ + string_pXps, /* space */ /* Xps is POSIX space, but from 8.34 */ + string_pXwd, /* word */ /* Perl and POSIX space are the same */ + NULL, /* xdigit */ + /* Negated cases */ + string_PL, /* ^alpha */ + string_PLl, /* ^lower */ + string_PLu, /* ^upper */ + string_PXan, /* ^alnum */ + NULL, /* ^ascii */ + string_H, /* ^blank */ + NULL, /* ^cntrl */ + string_PNd, /* ^digit */ + NULL, /* ^graph */ + NULL, /* ^print */ + NULL, /* ^punct */ + string_PXps, /* ^space */ /* Xps is POSIX space, but from 8.34 */ + string_PXwd, /* ^word */ /* Perl and POSIX space are the same */ + NULL /* ^xdigit */ +}; +#define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *)) +#endif + +#define STRING(a) # a +#define XSTRING(s) STRING(s) + +/* The texts of compile-time error messages. These are "char *" because they +are passed to the outside world. Do not ever re-use any error number, because +they are documented. Always add a new error instead. Messages marked DEAD below +are no longer used. This used to be a table of strings, but in order to reduce +the number of relocations needed when a shared library is loaded dynamically, +it is now one long string. We cannot use a table of offsets, because the +lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we +simply count through to the one we want - this isn't a performance issue +because these strings are used only when there is a compilation error. + +Each substring ends with \0 to insert a null character. This includes the final +substring, so that the whole string ends with \0\0, which can be detected when +counting through. */ + +static const char error_texts[] = + "no error\0" + "\\ at end of pattern\0" + "\\c at end of pattern\0" + "unrecognized character follows \\\0" + "numbers out of order in {} quantifier\0" + /* 5 */ + "number too big in {} quantifier\0" + "missing terminating ] for character class\0" + "invalid escape sequence in character class\0" + "range out of order in character class\0" + "nothing to repeat\0" + /* 10 */ + "internal error: invalid forward reference offset\0" + "internal error: unexpected repeat\0" + "unrecognized character after (? or (?-\0" + "POSIX named classes are supported only within a class\0" + "missing )\0" + /* 15 */ + "reference to non-existent subpattern\0" + "erroffset passed as NULL\0" + "unknown option bit(s) set\0" + "missing ) after comment\0" + "parentheses nested too deeply\0" /** DEAD **/ + /* 20 */ + "regular expression is too large\0" + "failed to get memory\0" + "unmatched parentheses\0" + "internal error: code overflow\0" + "unrecognized character after (?<\0" + /* 25 */ + "lookbehind assertion is not fixed length\0" + "malformed number or name after (?(\0" + "conditional group contains more than two branches\0" + "assertion expected after (?( or (?(?C)\0" + "(?R or (?[+-]digits must be followed by )\0" + /* 30 */ + "unknown POSIX class name\0" + "POSIX collating elements are not supported\0" + "this version of PCRE is compiled without UTF support\0" + "spare error\0" /** DEAD **/ + "character value in \\x{} or \\o{} is too large\0" + /* 35 */ + "invalid condition (?(0)\0" + "\\C not allowed in lookbehind assertion\0" + "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0" + "number after (?C is > 255\0" + "closing ) for (?C expected\0" + /* 40 */ + "recursive call could loop indefinitely\0" + "unrecognized character after (?P\0" + "syntax error in subpattern name (missing terminator)\0" + "two named subpatterns have the same name\0" + "invalid UTF-8 string\0" + /* 45 */ + "support for \\P, \\p, and \\X has not been compiled\0" + "malformed \\P or \\p sequence\0" + "unknown property name after \\P or \\p\0" + "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)\0" + "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0" + /* 50 */ + "repeated subpattern is too long\0" /** DEAD **/ + "octal value is greater than \\377 in 8-bit non-UTF-8 mode\0" + "internal error: overran compiling workspace\0" + "internal error: previously-checked referenced subpattern not found\0" + "DEFINE group contains more than one branch\0" + /* 55 */ + "repeating a DEFINE group is not allowed\0" /** DEAD **/ + "inconsistent NEWLINE options\0" + "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0" + "a numbered reference must not be zero\0" + "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0" + /* 60 */ + "(*VERB) not recognized or malformed\0" + "number is too big\0" + "subpattern name expected\0" + "digit expected after (?+\0" + "] is an invalid data character in JavaScript compatibility mode\0" + /* 65 */ + "different names for subpatterns of the same number are not allowed\0" + "(*MARK) must have an argument\0" + "this version of PCRE is not compiled with Unicode property support\0" +#ifndef EBCDIC + "\\c must be followed by an ASCII character\0" +#else + "\\c must be followed by a letter or one of [\\]^_?\0" +#endif + "\\k is not followed by a braced, angle-bracketed, or quoted name\0" + /* 70 */ + "internal error: unknown opcode in find_fixedlength()\0" + "\\N is not supported in a class\0" + "too many forward references\0" + "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0" + "invalid UTF-16 string\0" + /* 75 */ + "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0" + "character value in \\u.... sequence is too large\0" + "invalid UTF-32 string\0" + "setting UTF is disabled by the application\0" + "non-hex character in \\x{} (closing brace missing?)\0" + /* 80 */ + "non-octal character in \\o{} (closing brace missing?)\0" + "missing opening brace after \\o\0" + "parentheses are too deeply nested\0" + "invalid range in character class\0" + "group name must start with a non-digit\0" + /* 85 */ + "parentheses are too deeply nested (stack check)\0" + "digits missing in \\x{} or \\o{}\0" + "regular expression is too complicated\0" + ; + +/* Table to identify digits and hex digits. This is used when compiling +patterns. Note that the tables in chartables are dependent on the locale, and +may mark arbitrary characters as digits - but the PCRE compiling code expects +to handle only 0-9, a-z, and A-Z as digits when compiling. That is why we have +a private table here. It costs 256 bytes, but it is a lot faster than doing +character value tests (at least in some simple cases I timed), and in some +applications one wants PCRE to compile efficiently as well as match +efficiently. + +For convenience, we use the same bit definitions as in chartables: + + 0x04 decimal digit + 0x08 hexadecimal digit + +Then we can use ctype_digit and ctype_xdigit in the code. */ + +/* Using a simple comparison for decimal numbers rather than a memory read +is much faster, and the resulting code is simpler (the compiler turns it +into a subtraction and unsigned comparison). */ + +#define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9) + +#ifndef EBCDIC + +/* This is the "normal" case, for ASCII systems, and EBCDIC systems running in +UTF-8 mode. */ + +static const pcre_uint8 digitab[] = + { + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - ' */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ( - / */ + 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 */ + 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, /* 8 - ? */ + 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* @ - G */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H - O */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* P - W */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* X - _ */ + 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* ` - g */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h - o */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* p - w */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* x -127 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */ + +#else + +/* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */ + +static const pcre_uint8 digitab[] = + { + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 0 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 10 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 32- 39 20 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 30 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 40 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 72- | */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 50 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 88- 95 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 60 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104- ? */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 70 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */ + 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* 128- g 80 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144- p 90 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160- x A0 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 B0 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */ + 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* { - G C0 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* } - P D0 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* \ - X E0 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */ + 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 F0 */ + 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */ + +static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */ + 0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 0- 7 */ + 0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /* 8- 15 */ + 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 16- 23 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */ + 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 32- 39 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */ + 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 */ + 0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80, /* 72- | */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 */ + 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, /* 88- 95 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 */ + 0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x80, /* 104- ? */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */ + 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* 128- g */ + 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */ + 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* 144- p */ + 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */ + 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* 160- x */ + 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */ + 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 */ + 0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00, /* 184-191 */ + 0x80,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* { - G */ + 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */ + 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* } - P */ + 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */ + 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* \ - X */ + 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */ + 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */ + 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */ +#endif + + +/* This table is used to check whether auto-possessification is possible +between adjacent character-type opcodes. The left-hand (repeated) opcode is +used to select the row, and the right-hand opcode is use to select the column. +A value of 1 means that auto-possessification is OK. For example, the second +value in the first row means that \D+\d can be turned into \D++\d. + +The Unicode property types (\P and \p) have to be present to fill out the table +because of what their opcode values are, but the table values should always be +zero because property types are handled separately in the code. The last four +columns apply to items that cannot be repeated, so there is no need to have +rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is +*not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */ + +#define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1) +#define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1) + +static const pcre_uint8 autoposstab[APTROWS][APTCOLS] = { +/* \D \d \S \s \W \w . .+ \C \P \p \R \H \h \V \v \X \Z \z $ $M */ + { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \D */ + { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \d */ + { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \S */ + { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \s */ + { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \W */ + { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \w */ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* . */ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* .+ */ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \C */ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* \P */ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* \p */ + { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, /* \R */ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, /* \H */ + { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 }, /* \h */ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 }, /* \V */ + { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 }, /* \v */ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 } /* \X */ +}; + + +/* This table is used to check whether auto-possessification is possible +between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The +left-hand (repeated) opcode is used to select the row, and the right-hand +opcode is used to select the column. The values are as follows: + + 0 Always return FALSE (never auto-possessify) + 1 Character groups are distinct (possessify if both are OP_PROP) + 2 Check character categories in the same group (general or particular) + 3 TRUE if the two opcodes are not the same (PROP vs NOTPROP) + + 4 Check left general category vs right particular category + 5 Check right general category vs left particular category + + 6 Left alphanum vs right general category + 7 Left space vs right general category + 8 Left word vs right general category + + 9 Right alphanum vs left general category + 10 Right space vs left general category + 11 Right word vs left general category + + 12 Left alphanum vs right particular category + 13 Left space vs right particular category + 14 Left word vs right particular category + + 15 Right alphanum vs left particular category + 16 Right space vs left particular category + 17 Right word vs left particular category +*/ + +static const pcre_uint8 propposstab[PT_TABSIZE][PT_TABSIZE] = { +/* ANY LAMP GC PC SC ALNUM SPACE PXSPACE WORD CLIST UCNC */ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* PT_ANY */ + { 0, 3, 0, 0, 0, 3, 1, 1, 0, 0, 0 }, /* PT_LAMP */ + { 0, 0, 2, 4, 0, 9, 10, 10, 11, 0, 0 }, /* PT_GC */ + { 0, 0, 5, 2, 0, 15, 16, 16, 17, 0, 0 }, /* PT_PC */ + { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 }, /* PT_SC */ + { 0, 3, 6, 12, 0, 3, 1, 1, 0, 0, 0 }, /* PT_ALNUM */ + { 0, 1, 7, 13, 0, 1, 3, 3, 1, 0, 0 }, /* PT_SPACE */ + { 0, 1, 7, 13, 0, 1, 3, 3, 1, 0, 0 }, /* PT_PXSPACE */ + { 0, 0, 8, 14, 0, 0, 1, 1, 3, 0, 0 }, /* PT_WORD */ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* PT_CLIST */ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 } /* PT_UCNC */ +}; + +/* This table is used to check whether auto-possessification is possible +between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one +specifies a general category and the other specifies a particular category. The +row is selected by the general category and the column by the particular +category. The value is 1 if the particular category is not part of the general +category. */ + +static const pcre_uint8 catposstab[7][30] = { +/* Cc Cf Cn Co Cs Ll Lm Lo Lt Lu Mc Me Mn Nd Nl No Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So Zl Zp Zs */ + { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* C */ + { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* L */ + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* M */ + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* N */ + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 }, /* P */ + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 }, /* S */ + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 } /* Z */ +}; + +/* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against +a general or particular category. The properties in each row are those +that apply to the character set in question. Duplication means that a little +unnecessary work is done when checking, but this keeps things much simpler +because they can all use the same code. For more details see the comment where +this table is used. + +Note: SPACE and PXSPACE used to be different because Perl excluded VT from +"space", but from Perl 5.18 it's included, so both categories are treated the +same here. */ + +static const pcre_uint8 posspropstab[3][4] = { + { ucp_L, ucp_N, ucp_N, ucp_Nl }, /* ALNUM, 3rd and 4th values redundant */ + { ucp_Z, ucp_Z, ucp_C, ucp_Cc }, /* SPACE and PXSPACE, 2nd value redundant */ + { ucp_L, ucp_N, ucp_P, ucp_Po } /* WORD */ +}; + +/* This table is used when converting repeating opcodes into possessified +versions as a result of an explicit possessive quantifier such as ++. A zero +value means there is no possessified version - in those cases the item in +question must be wrapped in ONCE brackets. The table is truncated at OP_CALLOUT +because all relevant opcodes are less than that. */ + +static const pcre_uint8 opcode_possessify[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 15 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16 - 31 */ + + 0, /* NOTI */ + OP_POSSTAR, 0, /* STAR, MINSTAR */ + OP_POSPLUS, 0, /* PLUS, MINPLUS */ + OP_POSQUERY, 0, /* QUERY, MINQUERY */ + OP_POSUPTO, 0, /* UPTO, MINUPTO */ + 0, /* EXACT */ + 0, 0, 0, 0, /* POS{STAR,PLUS,QUERY,UPTO} */ + + OP_POSSTARI, 0, /* STARI, MINSTARI */ + OP_POSPLUSI, 0, /* PLUSI, MINPLUSI */ + OP_POSQUERYI, 0, /* QUERYI, MINQUERYI */ + OP_POSUPTOI, 0, /* UPTOI, MINUPTOI */ + 0, /* EXACTI */ + 0, 0, 0, 0, /* POS{STARI,PLUSI,QUERYI,UPTOI} */ + + OP_NOTPOSSTAR, 0, /* NOTSTAR, NOTMINSTAR */ + OP_NOTPOSPLUS, 0, /* NOTPLUS, NOTMINPLUS */ + OP_NOTPOSQUERY, 0, /* NOTQUERY, NOTMINQUERY */ + OP_NOTPOSUPTO, 0, /* NOTUPTO, NOTMINUPTO */ + 0, /* NOTEXACT */ + 0, 0, 0, 0, /* NOTPOS{STAR,PLUS,QUERY,UPTO} */ + + OP_NOTPOSSTARI, 0, /* NOTSTARI, NOTMINSTARI */ + OP_NOTPOSPLUSI, 0, /* NOTPLUSI, NOTMINPLUSI */ + OP_NOTPOSQUERYI, 0, /* NOTQUERYI, NOTMINQUERYI */ + OP_NOTPOSUPTOI, 0, /* NOTUPTOI, NOTMINUPTOI */ + 0, /* NOTEXACTI */ + 0, 0, 0, 0, /* NOTPOS{STARI,PLUSI,QUERYI,UPTOI} */ + + OP_TYPEPOSSTAR, 0, /* TYPESTAR, TYPEMINSTAR */ + OP_TYPEPOSPLUS, 0, /* TYPEPLUS, TYPEMINPLUS */ + OP_TYPEPOSQUERY, 0, /* TYPEQUERY, TYPEMINQUERY */ + OP_TYPEPOSUPTO, 0, /* TYPEUPTO, TYPEMINUPTO */ + 0, /* TYPEEXACT */ + 0, 0, 0, 0, /* TYPEPOS{STAR,PLUS,QUERY,UPTO} */ + + OP_CRPOSSTAR, 0, /* CRSTAR, CRMINSTAR */ + OP_CRPOSPLUS, 0, /* CRPLUS, CRMINPLUS */ + OP_CRPOSQUERY, 0, /* CRQUERY, CRMINQUERY */ + OP_CRPOSRANGE, 0, /* CRRANGE, CRMINRANGE */ + 0, 0, 0, 0, /* CRPOS{STAR,PLUS,QUERY,RANGE} */ + + 0, 0, 0, /* CLASS, NCLASS, XCLASS */ + 0, 0, /* REF, REFI */ + 0, 0, /* DNREF, DNREFI */ + 0, 0 /* RECURSE, CALLOUT */ +}; + + + +/************************************************* +* Find an error text * +*************************************************/ + +/* The error texts are now all in one long string, to save on relocations. As +some of the text is of unknown length, we can't use a table of offsets. +Instead, just count through the strings. This is not a performance issue +because it happens only when there has been a compilation error. + +Argument: the error number +Returns: pointer to the error string +*/ + +static const char * +find_error_text(int n) +{ +const char *s = error_texts; +for (; n > 0; n--) + { + while (*s++ != CHAR_NULL) {}; + if (*s == CHAR_NULL) return "Error text not found (please report)"; + } +return s; +} + + + +/************************************************* +* Expand the workspace * +*************************************************/ + +/* This function is called during the second compiling phase, if the number of +forward references fills the existing workspace, which is originally a block on +the stack. A larger block is obtained from malloc() unless the ultimate limit +has been reached or the increase will be rather small. + +Argument: pointer to the compile data block +Returns: 0 if all went well, else an error number +*/ + +static int +expand_workspace(compile_data *cd) +{ +pcre_uchar *newspace; +int newsize = cd->workspace_size * 2; + +if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX; +if (cd->workspace_size >= COMPILE_WORK_SIZE_MAX || + newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN) + return ERR72; + +newspace = (PUBL(malloc))(IN_UCHARS(newsize)); +if (newspace == NULL) return ERR21; +memcpy(newspace, cd->start_workspace, cd->workspace_size * sizeof(pcre_uchar)); +cd->hwm = (pcre_uchar *)newspace + (cd->hwm - cd->start_workspace); +if (cd->workspace_size > COMPILE_WORK_SIZE) + (PUBL(free))((void *)cd->start_workspace); +cd->start_workspace = newspace; +cd->workspace_size = newsize; +return 0; +} + + + +/************************************************* +* Check for counted repeat * +*************************************************/ + +/* This function is called when a '{' is encountered in a place where it might +start a quantifier. It looks ahead to see if it really is a quantifier or not. +It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd} +where the ddds are digits. + +Arguments: + p pointer to the first char after '{' + +Returns: TRUE or FALSE +*/ + +static BOOL +is_counted_repeat(const pcre_uchar *p) +{ +if (!IS_DIGIT(*p)) return FALSE; +p++; +while (IS_DIGIT(*p)) p++; +if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE; + +if (*p++ != CHAR_COMMA) return FALSE; +if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE; + +if (!IS_DIGIT(*p)) return FALSE; +p++; +while (IS_DIGIT(*p)) p++; + +return (*p == CHAR_RIGHT_CURLY_BRACKET); +} + + + +/************************************************* +* Handle escapes * +*************************************************/ + +/* This function is called when a \ has been encountered. It either returns a +positive value for a simple escape such as \n, or 0 for a data character which +will be placed in chptr. A backreference to group n is returned as negative n. +When UTF-8 is enabled, a positive value greater than 255 may be returned in +chptr. On entry, ptr is pointing at the \. On exit, it is on the final +character of the escape sequence. + +Arguments: + ptrptr points to the pattern position pointer + chptr points to a returned data character + errorcodeptr points to the errorcode variable + bracount number of previous extracting brackets + options the options bits + isclass TRUE if inside a character class + +Returns: zero => a data character + positive => a special escape sequence + negative => a back reference + on error, errorcodeptr is set +*/ + +static int +check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr, + int bracount, int options, BOOL isclass) +{ +/* PCRE_UTF16 has the same value as PCRE_UTF8. */ +BOOL utf = (options & PCRE_UTF8) != 0; +const pcre_uchar *ptr = *ptrptr + 1; +pcre_uint32 c; +int escape = 0; +int i; + +GETCHARINCTEST(c, ptr); /* Get character value, increment pointer */ +ptr--; /* Set pointer back to the last byte */ + +/* If backslash is at the end of the pattern, it's an error. */ + +if (c == CHAR_NULL) *errorcodeptr = ERR1; + +/* Non-alphanumerics are literals. For digits or letters, do an initial lookup +in a table. A non-zero result is something that can be returned immediately. +Otherwise further processing may be required. */ + +#ifndef EBCDIC /* ASCII/UTF-8 coding */ +/* Not alphanumeric */ +else if (c < CHAR_0 || c > CHAR_z) {} +else if ((i = escapes[c - CHAR_0]) != 0) + { if (i > 0) c = (pcre_uint32)i; else escape = -i; } + +#else /* EBCDIC coding */ +/* Not alphanumeric */ +else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {} +else if ((i = escapes[c - 0x48]) != 0) { if (i > 0) c = (pcre_uint32)i; else escape = -i; } +#endif + +/* Escapes that need further processing, or are illegal. */ + +else + { + const pcre_uchar *oldptr; + BOOL braced, negated, overflow; + int s; + + switch (c) + { + /* A number of Perl escapes are not handled by PCRE. We give an explicit + error. */ + + case CHAR_l: + case CHAR_L: + *errorcodeptr = ERR37; + break; + + case CHAR_u: + if ((options & PCRE_JAVASCRIPT_COMPAT) != 0) + { + /* In JavaScript, \u must be followed by four hexadecimal numbers. + Otherwise it is a lowercase u letter. */ + if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0 + && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0 + && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0 + && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0) + { + c = 0; + for (i = 0; i < 4; ++i) + { + register pcre_uint32 cc = *(++ptr); +#ifndef EBCDIC /* ASCII/UTF-8 coding */ + if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */ + c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10)); +#else /* EBCDIC coding */ + if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */ + c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10)); +#endif + } + +#if defined COMPILE_PCRE8 + if (c > (utf ? 0x10ffffU : 0xffU)) +#elif defined COMPILE_PCRE16 + if (c > (utf ? 0x10ffffU : 0xffffU)) +#elif defined COMPILE_PCRE32 + if (utf && c > 0x10ffffU) +#endif + { + *errorcodeptr = ERR76; + } + else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73; + } + } + else + *errorcodeptr = ERR37; + break; + + case CHAR_U: + /* In JavaScript, \U is an uppercase U letter. */ + if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) *errorcodeptr = ERR37; + break; + + /* In a character class, \g is just a literal "g". Outside a character + class, \g must be followed by one of a number of specific things: + + (1) A number, either plain or braced. If positive, it is an absolute + backreference. If negative, it is a relative backreference. This is a Perl + 5.10 feature. + + (2) Perl 5.10 also supports \g{name} as a reference to a named group. This + is part of Perl's movement towards a unified syntax for back references. As + this is synonymous with \k{name}, we fudge it up by pretending it really + was \k. + + (3) For Oniguruma compatibility we also support \g followed by a name or a + number either in angle brackets or in single quotes. However, these are + (possibly recursive) subroutine calls, _not_ backreferences. Just return + the ESC_g code (cf \k). */ + + case CHAR_g: + if (isclass) break; + if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE) + { + escape = ESC_g; + break; + } + + /* Handle the Perl-compatible cases */ + + if (ptr[1] == CHAR_LEFT_CURLY_BRACKET) + { + const pcre_uchar *p; + for (p = ptr+2; *p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET; p++) + if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break; + if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET) + { + escape = ESC_k; + break; + } + braced = TRUE; + ptr++; + } + else braced = FALSE; + + if (ptr[1] == CHAR_MINUS) + { + negated = TRUE; + ptr++; + } + else negated = FALSE; + + /* The integer range is limited by the machine's int representation. */ + s = 0; + overflow = FALSE; + while (IS_DIGIT(ptr[1])) + { + if (s > INT_MAX / 10 - 1) /* Integer overflow */ + { + overflow = TRUE; + break; + } + s = s * 10 + (int)(*(++ptr) - CHAR_0); + } + if (overflow) /* Integer overflow */ + { + while (IS_DIGIT(ptr[1])) + ptr++; + *errorcodeptr = ERR61; + break; + } + + if (braced && *(++ptr) != CHAR_RIGHT_CURLY_BRACKET) + { + *errorcodeptr = ERR57; + break; + } + + if (s == 0) + { + *errorcodeptr = ERR58; + break; + } + + if (negated) + { + if (s > bracount) + { + *errorcodeptr = ERR15; + break; + } + s = bracount - (s - 1); + } + + escape = -s; + break; + + /* The handling of escape sequences consisting of a string of digits + starting with one that is not zero is not straightforward. Perl has changed + over the years. Nowadays \g{} for backreferences and \o{} for octal are + recommended to avoid the ambiguities in the old syntax. + + Outside a character class, the digits are read as a decimal number. If the + number is less than 8 (used to be 10), or if there are that many previous + extracting left brackets, then it is a back reference. Otherwise, up to + three octal digits are read to form an escaped byte. Thus \123 is likely to + be octal 123 (cf \0123, which is octal 012 followed by the literal 3). If + the octal value is greater than 377, the least significant 8 bits are + taken. \8 and \9 are treated as the literal characters 8 and 9. + + Inside a character class, \ followed by a digit is always either a literal + 8 or 9 or an octal number. */ + + case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5: + case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9: + + if (!isclass) + { + oldptr = ptr; + /* The integer range is limited by the machine's int representation. */ + s = (int)(c -CHAR_0); + overflow = FALSE; + while (IS_DIGIT(ptr[1])) + { + if (s > INT_MAX / 10 - 1) /* Integer overflow */ + { + overflow = TRUE; + break; + } + s = s * 10 + (int)(*(++ptr) - CHAR_0); + } + if (overflow) /* Integer overflow */ + { + while (IS_DIGIT(ptr[1])) + ptr++; + *errorcodeptr = ERR61; + break; + } + if (s < 8 || s <= bracount) /* Check for back reference */ + { + escape = -s; + break; + } + ptr = oldptr; /* Put the pointer back and fall through */ + } + + /* Handle a digit following \ when the number is not a back reference. If + the first digit is 8 or 9, Perl used to generate a binary zero byte and + then treat the digit as a following literal. At least by Perl 5.18 this + changed so as not to insert the binary zero. */ + + if ((c = *ptr) >= CHAR_8) break; + + /* Fall through with a digit less than 8 */ + + /* \0 always starts an octal number, but we may drop through to here with a + larger first octal digit. The original code used just to take the least + significant 8 bits of octal numbers (I think this is what early Perls used + to do). Nowadays we allow for larger numbers in UTF-8 mode and 16-bit mode, + but no more than 3 octal digits. */ + + case CHAR_0: + c -= CHAR_0; + while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7) + c = c * 8 + *(++ptr) - CHAR_0; +#ifdef COMPILE_PCRE8 + if (!utf && c > 0xff) *errorcodeptr = ERR51; +#endif + break; + + /* \o is a relatively new Perl feature, supporting a more general way of + specifying character codes in octal. The only supported form is \o{ddd}. */ + + case CHAR_o: + if (ptr[1] != CHAR_LEFT_CURLY_BRACKET) *errorcodeptr = ERR81; else + if (ptr[2] == CHAR_RIGHT_CURLY_BRACKET) *errorcodeptr = ERR86; else + { + ptr += 2; + c = 0; + overflow = FALSE; + while (*ptr >= CHAR_0 && *ptr <= CHAR_7) + { + register pcre_uint32 cc = *ptr++; + if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */ +#ifdef COMPILE_PCRE32 + if (c >= 0x20000000l) { overflow = TRUE; break; } +#endif + c = (c << 3) + cc - CHAR_0 ; +#if defined COMPILE_PCRE8 + if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; } +#elif defined COMPILE_PCRE16 + if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; } +#elif defined COMPILE_PCRE32 + if (utf && c > 0x10ffffU) { overflow = TRUE; break; } +#endif + } + if (overflow) + { + while (*ptr >= CHAR_0 && *ptr <= CHAR_7) ptr++; + *errorcodeptr = ERR34; + } + else if (*ptr == CHAR_RIGHT_CURLY_BRACKET) + { + if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73; + } + else *errorcodeptr = ERR80; + } + break; + + /* \x is complicated. In JavaScript, \x must be followed by two hexadecimal + numbers. Otherwise it is a lowercase x letter. */ + + case CHAR_x: + if ((options & PCRE_JAVASCRIPT_COMPAT) != 0) + { + if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0 + && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0) + { + c = 0; + for (i = 0; i < 2; ++i) + { + register pcre_uint32 cc = *(++ptr); +#ifndef EBCDIC /* ASCII/UTF-8 coding */ + if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */ + c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10)); +#else /* EBCDIC coding */ + if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */ + c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10)); +#endif + } + } + } /* End JavaScript handling */ + + /* Handle \x in Perl's style. \x{ddd} is a character number which can be + greater than 0xff in utf or non-8bit mode, but only if the ddd are hex + digits. If not, { used to be treated as a data character. However, Perl + seems to read hex digits up to the first non-such, and ignore the rest, so + that, for example \x{zz} matches a binary zero. This seems crazy, so PCRE + now gives an error. */ + + else + { + if (ptr[1] == CHAR_LEFT_CURLY_BRACKET) + { + ptr += 2; + if (*ptr == CHAR_RIGHT_CURLY_BRACKET) + { + *errorcodeptr = ERR86; + break; + } + c = 0; + overflow = FALSE; + while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0) + { + register pcre_uint32 cc = *ptr++; + if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */ + +#ifdef COMPILE_PCRE32 + if (c >= 0x10000000l) { overflow = TRUE; break; } +#endif + +#ifndef EBCDIC /* ASCII/UTF-8 coding */ + if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */ + c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10)); +#else /* EBCDIC coding */ + if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */ + c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10)); +#endif + +#if defined COMPILE_PCRE8 + if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; } +#elif defined COMPILE_PCRE16 + if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; } +#elif defined COMPILE_PCRE32 + if (utf && c > 0x10ffffU) { overflow = TRUE; break; } +#endif + } + + if (overflow) + { + while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0) ptr++; + *errorcodeptr = ERR34; + } + + else if (*ptr == CHAR_RIGHT_CURLY_BRACKET) + { + if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73; + } + + /* If the sequence of hex digits does not end with '}', give an error. + We used just to recognize this construct and fall through to the normal + \x handling, but nowadays Perl gives an error, which seems much more + sensible, so we do too. */ + + else *errorcodeptr = ERR79; + } /* End of \x{} processing */ + + /* Read a single-byte hex-defined char (up to two hex digits after \x) */ + + else + { + c = 0; + while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0) + { + pcre_uint32 cc; /* Some compilers don't like */ + cc = *(++ptr); /* ++ in initializers */ +#ifndef EBCDIC /* ASCII/UTF-8 coding */ + if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */ + c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10)); +#else /* EBCDIC coding */ + if (cc <= CHAR_z) cc += 64; /* Convert to upper case */ + c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10)); +#endif + } + } /* End of \xdd handling */ + } /* End of Perl-style \x handling */ + break; + + /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped. + An error is given if the byte following \c is not an ASCII character. This + coding is ASCII-specific, but then the whole concept of \cx is + ASCII-specific. (However, an EBCDIC equivalent has now been added.) */ + + case CHAR_c: + c = *(++ptr); + if (c == CHAR_NULL) + { + *errorcodeptr = ERR2; + break; + } +#ifndef EBCDIC /* ASCII/UTF-8 coding */ + if (c > 127) /* Excludes all non-ASCII in either mode */ + { + *errorcodeptr = ERR68; + break; + } + if (c >= CHAR_a && c <= CHAR_z) c -= 32; + c ^= 0x40; +#else /* EBCDIC coding */ + if (c >= CHAR_a && c <= CHAR_z) c += 64; + if (c == CHAR_QUESTION_MARK) + c = ('\\' == 188 && '`' == 74)? 0x5f : 0xff; + else + { + for (i = 0; i < 32; i++) + { + if (c == ebcdic_escape_c[i]) break; + } + if (i < 32) c = i; else *errorcodeptr = ERR68; + } +#endif + break; + + /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any + other alphanumeric following \ is an error if PCRE_EXTRA was set; + otherwise, for Perl compatibility, it is a literal. This code looks a bit + odd, but there used to be some cases other than the default, and there may + be again in future, so I haven't "optimized" it. */ + + default: + if ((options & PCRE_EXTRA) != 0) switch(c) + { + default: + *errorcodeptr = ERR3; + break; + } + break; + } + } + +/* Perl supports \N{name} for character names, as well as plain \N for "not +newline". PCRE does not support \N{name}. However, it does support +quantification such as \N{2,3}. */ + +if (escape == ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET && + !is_counted_repeat(ptr+2)) + *errorcodeptr = ERR37; + +/* If PCRE_UCP is set, we change the values for \d etc. */ + +if ((options & PCRE_UCP) != 0 && escape >= ESC_D && escape <= ESC_w) + escape += (ESC_DU - ESC_D); + +/* Set the pointer to the final character before returning. */ + +*ptrptr = ptr; +*chptr = c; +return escape; +} + + + +#ifdef SUPPORT_UCP +/************************************************* +* Handle \P and \p * +*************************************************/ + +/* This function is called after \P or \p has been encountered, provided that +PCRE is compiled with support for Unicode properties. On entry, ptrptr is +pointing at the P or p. On exit, it is pointing at the final character of the +escape sequence. + +Argument: + ptrptr points to the pattern position pointer + negptr points to a boolean that is set TRUE for negation else FALSE + ptypeptr points to an unsigned int that is set to the type value + pdataptr points to an unsigned int that is set to the detailed property value + errorcodeptr points to the error code variable + +Returns: TRUE if the type value was found, or FALSE for an invalid type +*/ + +static BOOL +get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr, + unsigned int *pdataptr, int *errorcodeptr) +{ +pcre_uchar c; +int i, bot, top; +const pcre_uchar *ptr = *ptrptr; +pcre_uchar name[32]; + +c = *(++ptr); +if (c == CHAR_NULL) goto ERROR_RETURN; + +*negptr = FALSE; + +/* \P or \p can be followed by a name in {}, optionally preceded by ^ for +negation. */ + +if (c == CHAR_LEFT_CURLY_BRACKET) + { + if (ptr[1] == CHAR_CIRCUMFLEX_ACCENT) + { + *negptr = TRUE; + ptr++; + } + for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++) + { + c = *(++ptr); + if (c == CHAR_NULL) goto ERROR_RETURN; + if (c == CHAR_RIGHT_CURLY_BRACKET) break; + name[i] = c; + } + if (c != CHAR_RIGHT_CURLY_BRACKET) goto ERROR_RETURN; + name[i] = 0; + } + +/* Otherwise there is just one following character */ + +else + { + name[0] = c; + name[1] = 0; + } + +*ptrptr = ptr; + +/* Search for a recognized property name using binary chop */ + +bot = 0; +top = PRIV(utt_size); + +while (bot < top) + { + int r; + i = (bot + top) >> 1; + r = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset); + if (r == 0) + { + *ptypeptr = PRIV(utt)[i].type; + *pdataptr = PRIV(utt)[i].value; + return TRUE; + } + if (r > 0) bot = i + 1; else top = i; + } + +*errorcodeptr = ERR47; +*ptrptr = ptr; +return FALSE; + +ERROR_RETURN: +*errorcodeptr = ERR46; +*ptrptr = ptr; +return FALSE; +} +#endif + + + +/************************************************* +* Read repeat counts * +*************************************************/ + +/* Read an item of the form {n,m} and return the values. This is called only +after is_counted_repeat() has confirmed that a repeat-count quantifier exists, +so the syntax is guaranteed to be correct, but we need to check the values. + +Arguments: + p pointer to first char after '{' + minp pointer to int for min + maxp pointer to int for max + returned as -1 if no max + errorcodeptr points to error code variable + +Returns: pointer to '}' on success; + current ptr on error, with errorcodeptr set non-zero +*/ + +static const pcre_uchar * +read_repeat_counts(const pcre_uchar *p, int *minp, int *maxp, int *errorcodeptr) +{ +int min = 0; +int max = -1; + +while (IS_DIGIT(*p)) + { + min = min * 10 + (int)(*p++ - CHAR_0); + if (min > 65535) + { + *errorcodeptr = ERR5; + return p; + } + } + +if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else + { + if (*(++p) != CHAR_RIGHT_CURLY_BRACKET) + { + max = 0; + while(IS_DIGIT(*p)) + { + max = max * 10 + (int)(*p++ - CHAR_0); + if (max > 65535) + { + *errorcodeptr = ERR5; + return p; + } + } + if (max < min) + { + *errorcodeptr = ERR4; + return p; + } + } + } + +*minp = min; +*maxp = max; +return p; +} + + + +/************************************************* +* Find first significant op code * +*************************************************/ + +/* This is called by several functions that scan a compiled expression looking +for a fixed first character, or an anchoring op code etc. It skips over things +that do not influence this. For some calls, it makes sense to skip negative +forward and all backward assertions, and also the \b assertion; for others it +does not. + +Arguments: + code pointer to the start of the group + skipassert TRUE if certain assertions are to be skipped + +Returns: pointer to the first significant opcode +*/ + +static const pcre_uchar* +first_significant_code(const pcre_uchar *code, BOOL skipassert) +{ +for (;;) + { + switch ((int)*code) + { + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + if (!skipassert) return code; + do code += GET(code, 1); while (*code == OP_ALT); + code += PRIV(OP_lengths)[*code]; + break; + + case OP_WORD_BOUNDARY: + case OP_NOT_WORD_BOUNDARY: + if (!skipassert) return code; + /* Fall through */ + + case OP_CALLOUT: + case OP_CREF: + case OP_DNCREF: + case OP_RREF: + case OP_DNRREF: + case OP_DEF: + code += PRIV(OP_lengths)[*code]; + break; + + default: + return code; + } + } +/* Control never reaches here */ +} + + + +/************************************************* +* Find the fixed length of a branch * +*************************************************/ + +/* Scan a branch and compute the fixed length of subject that will match it, +if the length is fixed. This is needed for dealing with backward assertions. +In UTF8 mode, the result is in characters rather than bytes. The branch is +temporarily terminated with OP_END when this function is called. + +This function is called when a backward assertion is encountered, so that if it +fails, the error message can point to the correct place in the pattern. +However, we cannot do this when the assertion contains subroutine calls, +because they can be forward references. We solve this by remembering this case +and doing the check at the end; a flag specifies which mode we are running in. + +Arguments: + code points to the start of the pattern (the bracket) + utf TRUE in UTF-8 / UTF-16 / UTF-32 mode + atend TRUE if called when the pattern is complete + cd the "compile data" structure + recurses chain of recurse_check to catch mutual recursion + +Returns: the fixed length, + or -1 if there is no fixed length, + or -2 if \C was encountered (in UTF-8 mode only) + or -3 if an OP_RECURSE item was encountered and atend is FALSE + or -4 if an unknown opcode was encountered (internal error) +*/ + +static int +find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd, + recurse_check *recurses) +{ +int length = -1; +recurse_check this_recurse; +register int branchlength = 0; +register pcre_uchar *cc = code + 1 + LINK_SIZE; + +/* Scan along the opcodes for this branch. If we get to the end of the +branch, check the length against that of the other branches. */ + +for (;;) + { + int d; + pcre_uchar *ce, *cs; + register pcre_uchar op = *cc; + + switch (op) + { + /* We only need to continue for OP_CBRA (normal capturing bracket) and + OP_BRA (normal non-capturing bracket) because the other variants of these + opcodes are all concerned with unlimited repeated groups, which of course + are not of fixed length. */ + + case OP_CBRA: + case OP_BRA: + case OP_ONCE: + case OP_ONCE_NC: + case OP_COND: + d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf, atend, cd, + recurses); + if (d < 0) return d; + branchlength += d; + do cc += GET(cc, 1); while (*cc == OP_ALT); + cc += 1 + LINK_SIZE; + break; + + /* Reached end of a branch; if it's a ket it is the end of a nested call. + If it's ALT it is an alternation in a nested call. An ACCEPT is effectively + an ALT. If it is END it's the end of the outer call. All can be handled by + the same code. Note that we must not include the OP_KETRxxx opcodes here, + because they all imply an unlimited repeat. */ + + case OP_ALT: + case OP_KET: + case OP_END: + case OP_ACCEPT: + case OP_ASSERT_ACCEPT: + if (length < 0) length = branchlength; + else if (length != branchlength) return -1; + if (*cc != OP_ALT) return length; + cc += 1 + LINK_SIZE; + branchlength = 0; + break; + + /* A true recursion implies not fixed length, but a subroutine call may + be OK. If the subroutine is a forward reference, we can't deal with + it until the end of the pattern, so return -3. */ + + case OP_RECURSE: + if (!atend) return -3; + cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1); /* Start subpattern */ + do ce += GET(ce, 1); while (*ce == OP_ALT); /* End subpattern */ + if (cc > cs && cc < ce) return -1; /* Recursion */ + else /* Check for mutual recursion */ + { + recurse_check *r = recurses; + for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break; + if (r != NULL) return -1; /* Mutual recursion */ + } + this_recurse.prev = recurses; + this_recurse.group = cs; + d = find_fixedlength(cs + IMM2_SIZE, utf, atend, cd, &this_recurse); + if (d < 0) return d; + branchlength += d; + cc += 1 + LINK_SIZE; + break; + + /* Skip over assertive subpatterns */ + + case OP_ASSERT: + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + do cc += GET(cc, 1); while (*cc == OP_ALT); + cc += 1 + LINK_SIZE; + break; + + /* Skip over things that don't match chars */ + + case OP_MARK: + case OP_PRUNE_ARG: + case OP_SKIP_ARG: + case OP_THEN_ARG: + cc += cc[1] + PRIV(OP_lengths)[*cc]; + break; + + case OP_CALLOUT: + case OP_CIRC: + case OP_CIRCM: + case OP_CLOSE: + case OP_COMMIT: + case OP_CREF: + case OP_DEF: + case OP_DNCREF: + case OP_DNRREF: + case OP_DOLL: + case OP_DOLLM: + case OP_EOD: + case OP_EODN: + case OP_FAIL: + case OP_NOT_WORD_BOUNDARY: + case OP_PRUNE: + case OP_REVERSE: + case OP_RREF: + case OP_SET_SOM: + case OP_SKIP: + case OP_SOD: + case OP_SOM: + case OP_THEN: + case OP_WORD_BOUNDARY: + cc += PRIV(OP_lengths)[*cc]; + break; + + /* Handle literal characters */ + + case OP_CHAR: + case OP_CHARI: + case OP_NOT: + case OP_NOTI: + branchlength++; + cc += 2; +#ifdef SUPPORT_UTF + if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); +#endif + break; + + /* Handle exact repetitions. The count is already in characters, but we + need to skip over a multibyte character in UTF8 mode. */ + + case OP_EXACT: + case OP_EXACTI: + case OP_NOTEXACT: + case OP_NOTEXACTI: + branchlength += (int)GET2(cc,1); + cc += 2 + IMM2_SIZE; +#ifdef SUPPORT_UTF + if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); +#endif + break; + + case OP_TYPEEXACT: + branchlength += GET2(cc,1); + if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP) + cc += 2; + cc += 1 + IMM2_SIZE + 1; + break; + + /* Handle single-char matchers */ + + case OP_PROP: + case OP_NOTPROP: + cc += 2; + /* Fall through */ + + case OP_HSPACE: + case OP_VSPACE: + case OP_NOT_HSPACE: + case OP_NOT_VSPACE: + case OP_NOT_DIGIT: + case OP_DIGIT: + case OP_NOT_WHITESPACE: + case OP_WHITESPACE: + case OP_NOT_WORDCHAR: + case OP_WORDCHAR: + case OP_ANY: + case OP_ALLANY: + branchlength++; + cc++; + break; + + /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode; + otherwise \C is coded as OP_ALLANY. */ + + case OP_ANYBYTE: + return -2; + + /* Check a class for variable quantification */ + + case OP_CLASS: + case OP_NCLASS: +#if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + case OP_XCLASS: + /* The original code caused an unsigned overflow in 64 bit systems, + so now we use a conditional statement. */ + if (op == OP_XCLASS) + cc += GET(cc, 1); + else + cc += PRIV(OP_lengths)[OP_CLASS]; +#else + cc += PRIV(OP_lengths)[OP_CLASS]; +#endif + + switch (*cc) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRPLUS: + case OP_CRMINPLUS: + case OP_CRQUERY: + case OP_CRMINQUERY: + case OP_CRPOSSTAR: + case OP_CRPOSPLUS: + case OP_CRPOSQUERY: + return -1; + + case OP_CRRANGE: + case OP_CRMINRANGE: + case OP_CRPOSRANGE: + if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1; + branchlength += (int)GET2(cc,1); + cc += 1 + 2 * IMM2_SIZE; + break; + + default: + branchlength++; + } + break; + + /* Anything else is variable length */ + + case OP_ANYNL: + case OP_BRAMINZERO: + case OP_BRAPOS: + case OP_BRAPOSZERO: + case OP_BRAZERO: + case OP_CBRAPOS: + case OP_EXTUNI: + case OP_KETRMAX: + case OP_KETRMIN: + case OP_KETRPOS: + case OP_MINPLUS: + case OP_MINPLUSI: + case OP_MINQUERY: + case OP_MINQUERYI: + case OP_MINSTAR: + case OP_MINSTARI: + case OP_MINUPTO: + case OP_MINUPTOI: + case OP_NOTMINPLUS: + case OP_NOTMINPLUSI: + case OP_NOTMINQUERY: + case OP_NOTMINQUERYI: + case OP_NOTMINSTAR: + case OP_NOTMINSTARI: + case OP_NOTMINUPTO: + case OP_NOTMINUPTOI: + case OP_NOTPLUS: + case OP_NOTPLUSI: + case OP_NOTPOSPLUS: + case OP_NOTPOSPLUSI: + case OP_NOTPOSQUERY: + case OP_NOTPOSQUERYI: + case OP_NOTPOSSTAR: + case OP_NOTPOSSTARI: + case OP_NOTPOSUPTO: + case OP_NOTPOSUPTOI: + case OP_NOTQUERY: + case OP_NOTQUERYI: + case OP_NOTSTAR: + case OP_NOTSTARI: + case OP_NOTUPTO: + case OP_NOTUPTOI: + case OP_PLUS: + case OP_PLUSI: + case OP_POSPLUS: + case OP_POSPLUSI: + case OP_POSQUERY: + case OP_POSQUERYI: + case OP_POSSTAR: + case OP_POSSTARI: + case OP_POSUPTO: + case OP_POSUPTOI: + case OP_QUERY: + case OP_QUERYI: + case OP_REF: + case OP_REFI: + case OP_DNREF: + case OP_DNREFI: + case OP_SBRA: + case OP_SBRAPOS: + case OP_SCBRA: + case OP_SCBRAPOS: + case OP_SCOND: + case OP_SKIPZERO: + case OP_STAR: + case OP_STARI: + case OP_TYPEMINPLUS: + case OP_TYPEMINQUERY: + case OP_TYPEMINSTAR: + case OP_TYPEMINUPTO: + case OP_TYPEPLUS: + case OP_TYPEPOSPLUS: + case OP_TYPEPOSQUERY: + case OP_TYPEPOSSTAR: + case OP_TYPEPOSUPTO: + case OP_TYPEQUERY: + case OP_TYPESTAR: + case OP_TYPEUPTO: + case OP_UPTO: + case OP_UPTOI: + return -1; + + /* Catch unrecognized opcodes so that when new ones are added they + are not forgotten, as has happened in the past. */ + + default: + return -4; + } + } +/* Control never gets here */ +} + + + +/************************************************* +* Scan compiled regex for specific bracket * +*************************************************/ + +/* This little function scans through a compiled pattern until it finds a +capturing bracket with the given number, or, if the number is negative, an +instance of OP_REVERSE for a lookbehind. The function is global in the C sense +so that it can be called from pcre_study() when finding the minimum matching +length. + +Arguments: + code points to start of expression + utf TRUE in UTF-8 / UTF-16 / UTF-32 mode + number the required bracket number or negative to find a lookbehind + +Returns: pointer to the opcode for the bracket, or NULL if not found +*/ + +const pcre_uchar * +PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number) +{ +for (;;) + { + register pcre_uchar c = *code; + + if (c == OP_END) return NULL; + + /* XCLASS is used for classes that cannot be represented just by a bit + map. This includes negated single high-valued characters. The length in + the table is zero; the actual length is stored in the compiled code. */ + + if (c == OP_XCLASS) code += GET(code, 1); + + /* Handle recursion */ + + else if (c == OP_REVERSE) + { + if (number < 0) return (pcre_uchar *)code; + code += PRIV(OP_lengths)[c]; + } + + /* Handle capturing bracket */ + + else if (c == OP_CBRA || c == OP_SCBRA || + c == OP_CBRAPOS || c == OP_SCBRAPOS) + { + int n = (int)GET2(code, 1+LINK_SIZE); + if (n == number) return (pcre_uchar *)code; + code += PRIV(OP_lengths)[c]; + } + + /* Otherwise, we can get the item's length from the table, except that for + repeated character types, we have to test for \p and \P, which have an extra + two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we + must add in its length. */ + + else + { + switch(c) + { + case OP_TYPESTAR: + case OP_TYPEMINSTAR: + case OP_TYPEPLUS: + case OP_TYPEMINPLUS: + case OP_TYPEQUERY: + case OP_TYPEMINQUERY: + case OP_TYPEPOSSTAR: + case OP_TYPEPOSPLUS: + case OP_TYPEPOSQUERY: + if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2; + break; + + case OP_TYPEUPTO: + case OP_TYPEMINUPTO: + case OP_TYPEEXACT: + case OP_TYPEPOSUPTO: + if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP) + code += 2; + break; + + case OP_MARK: + case OP_PRUNE_ARG: + case OP_SKIP_ARG: + case OP_THEN_ARG: + code += code[1]; + break; + } + + /* Add in the fixed length from the table */ + + code += PRIV(OP_lengths)[c]; + + /* In UTF-8 mode, opcodes that are followed by a character may be followed by + a multi-byte character. The length in the table is a minimum, so we have to + arrange to skip the extra bytes. */ + +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf) switch(c) + { + case OP_CHAR: + case OP_CHARI: + case OP_NOT: + case OP_NOTI: + case OP_EXACT: + case OP_EXACTI: + case OP_NOTEXACT: + case OP_NOTEXACTI: + case OP_UPTO: + case OP_UPTOI: + case OP_NOTUPTO: + case OP_NOTUPTOI: + case OP_MINUPTO: + case OP_MINUPTOI: + case OP_NOTMINUPTO: + case OP_NOTMINUPTOI: + case OP_POSUPTO: + case OP_POSUPTOI: + case OP_NOTPOSUPTO: + case OP_NOTPOSUPTOI: + case OP_STAR: + case OP_STARI: + case OP_NOTSTAR: + case OP_NOTSTARI: + case OP_MINSTAR: + case OP_MINSTARI: + case OP_NOTMINSTAR: + case OP_NOTMINSTARI: + case OP_POSSTAR: + case OP_POSSTARI: + case OP_NOTPOSSTAR: + case OP_NOTPOSSTARI: + case OP_PLUS: + case OP_PLUSI: + case OP_NOTPLUS: + case OP_NOTPLUSI: + case OP_MINPLUS: + case OP_MINPLUSI: + case OP_NOTMINPLUS: + case OP_NOTMINPLUSI: + case OP_POSPLUS: + case OP_POSPLUSI: + case OP_NOTPOSPLUS: + case OP_NOTPOSPLUSI: + case OP_QUERY: + case OP_QUERYI: + case OP_NOTQUERY: + case OP_NOTQUERYI: + case OP_MINQUERY: + case OP_MINQUERYI: + case OP_NOTMINQUERY: + case OP_NOTMINQUERYI: + case OP_POSQUERY: + case OP_POSQUERYI: + case OP_NOTPOSQUERY: + case OP_NOTPOSQUERYI: + if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]); + break; + } +#else + (void)(utf); /* Keep compiler happy by referencing function argument */ +#endif + } + } +} + + + +/************************************************* +* Scan compiled regex for recursion reference * +*************************************************/ + +/* This little function scans through a compiled pattern until it finds an +instance of OP_RECURSE. + +Arguments: + code points to start of expression + utf TRUE in UTF-8 / UTF-16 / UTF-32 mode + +Returns: pointer to the opcode for OP_RECURSE, or NULL if not found +*/ + +static const pcre_uchar * +find_recurse(const pcre_uchar *code, BOOL utf) +{ +for (;;) + { + register pcre_uchar c = *code; + if (c == OP_END) return NULL; + if (c == OP_RECURSE) return code; + + /* XCLASS is used for classes that cannot be represented just by a bit + map. This includes negated single high-valued characters. The length in + the table is zero; the actual length is stored in the compiled code. */ + + if (c == OP_XCLASS) code += GET(code, 1); + + /* Otherwise, we can get the item's length from the table, except that for + repeated character types, we have to test for \p and \P, which have an extra + two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we + must add in its length. */ + + else + { + switch(c) + { + case OP_TYPESTAR: + case OP_TYPEMINSTAR: + case OP_TYPEPLUS: + case OP_TYPEMINPLUS: + case OP_TYPEQUERY: + case OP_TYPEMINQUERY: + case OP_TYPEPOSSTAR: + case OP_TYPEPOSPLUS: + case OP_TYPEPOSQUERY: + if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2; + break; + + case OP_TYPEPOSUPTO: + case OP_TYPEUPTO: + case OP_TYPEMINUPTO: + case OP_TYPEEXACT: + if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP) + code += 2; + break; + + case OP_MARK: + case OP_PRUNE_ARG: + case OP_SKIP_ARG: + case OP_THEN_ARG: + code += code[1]; + break; + } + + /* Add in the fixed length from the table */ + + code += PRIV(OP_lengths)[c]; + + /* In UTF-8 mode, opcodes that are followed by a character may be followed + by a multi-byte character. The length in the table is a minimum, so we have + to arrange to skip the extra bytes. */ + +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf) switch(c) + { + case OP_CHAR: + case OP_CHARI: + case OP_NOT: + case OP_NOTI: + case OP_EXACT: + case OP_EXACTI: + case OP_NOTEXACT: + case OP_NOTEXACTI: + case OP_UPTO: + case OP_UPTOI: + case OP_NOTUPTO: + case OP_NOTUPTOI: + case OP_MINUPTO: + case OP_MINUPTOI: + case OP_NOTMINUPTO: + case OP_NOTMINUPTOI: + case OP_POSUPTO: + case OP_POSUPTOI: + case OP_NOTPOSUPTO: + case OP_NOTPOSUPTOI: + case OP_STAR: + case OP_STARI: + case OP_NOTSTAR: + case OP_NOTSTARI: + case OP_MINSTAR: + case OP_MINSTARI: + case OP_NOTMINSTAR: + case OP_NOTMINSTARI: + case OP_POSSTAR: + case OP_POSSTARI: + case OP_NOTPOSSTAR: + case OP_NOTPOSSTARI: + case OP_PLUS: + case OP_PLUSI: + case OP_NOTPLUS: + case OP_NOTPLUSI: + case OP_MINPLUS: + case OP_MINPLUSI: + case OP_NOTMINPLUS: + case OP_NOTMINPLUSI: + case OP_POSPLUS: + case OP_POSPLUSI: + case OP_NOTPOSPLUS: + case OP_NOTPOSPLUSI: + case OP_QUERY: + case OP_QUERYI: + case OP_NOTQUERY: + case OP_NOTQUERYI: + case OP_MINQUERY: + case OP_MINQUERYI: + case OP_NOTMINQUERY: + case OP_NOTMINQUERYI: + case OP_POSQUERY: + case OP_POSQUERYI: + case OP_NOTPOSQUERY: + case OP_NOTPOSQUERYI: + if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]); + break; + } +#else + (void)(utf); /* Keep compiler happy by referencing function argument */ +#endif + } + } +} + + + +/************************************************* +* Scan compiled branch for non-emptiness * +*************************************************/ + +/* This function scans through a branch of a compiled pattern to see whether it +can match the empty string or not. It is called from could_be_empty() +below and from compile_branch() when checking for an unlimited repeat of a +group that can match nothing. Note that first_significant_code() skips over +backward and negative forward assertions when its final argument is TRUE. If we +hit an unclosed bracket, we return "empty" - this means we've struck an inner +bracket whose current branch will already have been scanned. + +Arguments: + code points to start of search + endcode points to where to stop + utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode + cd contains pointers to tables etc. + recurses chain of recurse_check to catch mutual recursion + +Returns: TRUE if what is matched could be empty +*/ + +static BOOL +could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode, + BOOL utf, compile_data *cd, recurse_check *recurses) +{ +register pcre_uchar c; +recurse_check this_recurse; + +for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE); + code < endcode; + code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE)) + { + const pcre_uchar *ccode; + + c = *code; + + /* Skip over forward assertions; the other assertions are skipped by + first_significant_code() with a TRUE final argument. */ + + if (c == OP_ASSERT) + { + do code += GET(code, 1); while (*code == OP_ALT); + c = *code; + continue; + } + + /* For a recursion/subroutine call, if its end has been reached, which + implies a backward reference subroutine call, we can scan it. If it's a + forward reference subroutine call, we can't. To detect forward reference + we have to scan up the list that is kept in the workspace. This function is + called only when doing the real compile, not during the pre-compile that + measures the size of the compiled pattern. */ + + if (c == OP_RECURSE) + { + const pcre_uchar *scode = cd->start_code + GET(code, 1); + const pcre_uchar *endgroup = scode; + BOOL empty_branch; + + /* Test for forward reference or uncompleted reference. This is disabled + when called to scan a completed pattern by setting cd->start_workspace to + NULL. */ + + if (cd->start_workspace != NULL) + { + const pcre_uchar *tcode; + for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE) + if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE; + if (GET(scode, 1) == 0) return TRUE; /* Unclosed */ + } + + /* If the reference is to a completed group, we need to detect whether this + is a recursive call, as otherwise there will be an infinite loop. If it is + a recursion, just skip over it. Simple recursions are easily detected. For + mutual recursions we keep a chain on the stack. */ + + do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT); + if (code >= scode && code <= endgroup) continue; /* Simple recursion */ + else + { + recurse_check *r = recurses; + for (r = recurses; r != NULL; r = r->prev) + if (r->group == scode) break; + if (r != NULL) continue; /* Mutual recursion */ + } + + /* Completed reference; scan the referenced group, remembering it on the + stack chain to detect mutual recursions. */ + + empty_branch = FALSE; + this_recurse.prev = recurses; + this_recurse.group = scode; + + do + { + if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse)) + { + empty_branch = TRUE; + break; + } + scode += GET(scode, 1); + } + while (*scode == OP_ALT); + + if (!empty_branch) return FALSE; /* All branches are non-empty */ + continue; + } + + /* Groups with zero repeats can of course be empty; skip them. */ + + if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO || + c == OP_BRAPOSZERO) + { + code += PRIV(OP_lengths)[c]; + do code += GET(code, 1); while (*code == OP_ALT); + c = *code; + continue; + } + + /* A nested group that is already marked as "could be empty" can just be + skipped. */ + + if (c == OP_SBRA || c == OP_SBRAPOS || + c == OP_SCBRA || c == OP_SCBRAPOS) + { + do code += GET(code, 1); while (*code == OP_ALT); + c = *code; + continue; + } + + /* For other groups, scan the branches. */ + + if (c == OP_BRA || c == OP_BRAPOS || + c == OP_CBRA || c == OP_CBRAPOS || + c == OP_ONCE || c == OP_ONCE_NC || + c == OP_COND || c == OP_SCOND) + { + BOOL empty_branch; + if (GET(code, 1) == 0) return TRUE; /* Hit unclosed bracket */ + + /* If a conditional group has only one branch, there is a second, implied, + empty branch, so just skip over the conditional, because it could be empty. + Otherwise, scan the individual branches of the group. */ + + if (c == OP_COND && code[GET(code, 1)] != OP_ALT) + code += GET(code, 1); + else + { + empty_branch = FALSE; + do + { + if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, + recurses)) empty_branch = TRUE; + code += GET(code, 1); + } + while (*code == OP_ALT); + if (!empty_branch) return FALSE; /* All branches are non-empty */ + } + + c = *code; + continue; + } + + /* Handle the other opcodes */ + + switch (c) + { + /* Check for quantifiers after a class. XCLASS is used for classes that + cannot be represented just by a bit map. This includes negated single + high-valued characters. The length in PRIV(OP_lengths)[] is zero; the + actual length is stored in the compiled code, so we must update "code" + here. */ + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: + ccode = code += GET(code, 1); + goto CHECK_CLASS_REPEAT; +#endif + + case OP_CLASS: + case OP_NCLASS: + ccode = code + PRIV(OP_lengths)[OP_CLASS]; + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + CHECK_CLASS_REPEAT: +#endif + + switch (*ccode) + { + case OP_CRSTAR: /* These could be empty; continue */ + case OP_CRMINSTAR: + case OP_CRQUERY: + case OP_CRMINQUERY: + case OP_CRPOSSTAR: + case OP_CRPOSQUERY: + break; + + default: /* Non-repeat => class must match */ + case OP_CRPLUS: /* These repeats aren't empty */ + case OP_CRMINPLUS: + case OP_CRPOSPLUS: + return FALSE; + + case OP_CRRANGE: + case OP_CRMINRANGE: + case OP_CRPOSRANGE: + if (GET2(ccode, 1) > 0) return FALSE; /* Minimum > 0 */ + break; + } + break; + + /* Opcodes that must match a character */ + + case OP_ANY: + case OP_ALLANY: + case OP_ANYBYTE: + + case OP_PROP: + case OP_NOTPROP: + case OP_ANYNL: + + case OP_NOT_HSPACE: + case OP_HSPACE: + case OP_NOT_VSPACE: + case OP_VSPACE: + case OP_EXTUNI: + + case OP_NOT_DIGIT: + case OP_DIGIT: + case OP_NOT_WHITESPACE: + case OP_WHITESPACE: + case OP_NOT_WORDCHAR: + case OP_WORDCHAR: + + case OP_CHAR: + case OP_CHARI: + case OP_NOT: + case OP_NOTI: + + case OP_PLUS: + case OP_PLUSI: + case OP_MINPLUS: + case OP_MINPLUSI: + + case OP_NOTPLUS: + case OP_NOTPLUSI: + case OP_NOTMINPLUS: + case OP_NOTMINPLUSI: + + case OP_POSPLUS: + case OP_POSPLUSI: + case OP_NOTPOSPLUS: + case OP_NOTPOSPLUSI: + + case OP_EXACT: + case OP_EXACTI: + case OP_NOTEXACT: + case OP_NOTEXACTI: + + case OP_TYPEPLUS: + case OP_TYPEMINPLUS: + case OP_TYPEPOSPLUS: + case OP_TYPEEXACT: + + return FALSE; + + /* These are going to continue, as they may be empty, but we have to + fudge the length for the \p and \P cases. */ + + case OP_TYPESTAR: + case OP_TYPEMINSTAR: + case OP_TYPEPOSSTAR: + case OP_TYPEQUERY: + case OP_TYPEMINQUERY: + case OP_TYPEPOSQUERY: + if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2; + break; + + /* Same for these */ + + case OP_TYPEUPTO: + case OP_TYPEMINUPTO: + case OP_TYPEPOSUPTO: + if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP) + code += 2; + break; + + /* End of branch */ + + case OP_KET: + case OP_KETRMAX: + case OP_KETRMIN: + case OP_KETRPOS: + case OP_ALT: + return TRUE; + + /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO, + MINUPTO, and POSUPTO and their caseless and negative versions may be + followed by a multibyte character. */ + +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + case OP_STAR: + case OP_STARI: + case OP_NOTSTAR: + case OP_NOTSTARI: + + case OP_MINSTAR: + case OP_MINSTARI: + case OP_NOTMINSTAR: + case OP_NOTMINSTARI: + + case OP_POSSTAR: + case OP_POSSTARI: + case OP_NOTPOSSTAR: + case OP_NOTPOSSTARI: + + case OP_QUERY: + case OP_QUERYI: + case OP_NOTQUERY: + case OP_NOTQUERYI: + + case OP_MINQUERY: + case OP_MINQUERYI: + case OP_NOTMINQUERY: + case OP_NOTMINQUERYI: + + case OP_POSQUERY: + case OP_POSQUERYI: + case OP_NOTPOSQUERY: + case OP_NOTPOSQUERYI: + + if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]); + break; + + case OP_UPTO: + case OP_UPTOI: + case OP_NOTUPTO: + case OP_NOTUPTOI: + + case OP_MINUPTO: + case OP_MINUPTOI: + case OP_NOTMINUPTO: + case OP_NOTMINUPTOI: + + case OP_POSUPTO: + case OP_POSUPTOI: + case OP_NOTPOSUPTO: + case OP_NOTPOSUPTOI: + + if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]); + break; +#endif + + /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument + string. */ + + case OP_MARK: + case OP_PRUNE_ARG: + case OP_SKIP_ARG: + case OP_THEN_ARG: + code += code[1]; + break; + + /* None of the remaining opcodes are required to match a character. */ + + default: + break; + } + } + +return TRUE; +} + + + +/************************************************* +* Scan compiled regex for non-emptiness * +*************************************************/ + +/* This function is called to check for left recursive calls. We want to check +the current branch of the current pattern to see if it could match the empty +string. If it could, we must look outwards for branches at other levels, +stopping when we pass beyond the bracket which is the subject of the recursion. +This function is called only during the real compile, not during the +pre-compile. + +Arguments: + code points to start of the recursion + endcode points to where to stop (current RECURSE item) + bcptr points to the chain of current (unclosed) branch starts + utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode + cd pointers to tables etc + +Returns: TRUE if what is matched could be empty +*/ + +static BOOL +could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode, + branch_chain *bcptr, BOOL utf, compile_data *cd) +{ +while (bcptr != NULL && bcptr->current_branch >= code) + { + if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL)) + return FALSE; + bcptr = bcptr->outer; + } +return TRUE; +} + + + +/************************************************* +* Base opcode of repeated opcodes * +*************************************************/ + +/* Returns the base opcode for repeated single character type opcodes. If the +opcode is not a repeated character type, it returns with the original value. + +Arguments: c opcode +Returns: base opcode for the type +*/ + +static pcre_uchar +get_repeat_base(pcre_uchar c) +{ +return (c > OP_TYPEPOSUPTO)? c : + (c >= OP_TYPESTAR)? OP_TYPESTAR : + (c >= OP_NOTSTARI)? OP_NOTSTARI : + (c >= OP_NOTSTAR)? OP_NOTSTAR : + (c >= OP_STARI)? OP_STARI : + OP_STAR; +} + + + +#ifdef SUPPORT_UCP +/************************************************* +* Check a character and a property * +*************************************************/ + +/* This function is called by check_auto_possessive() when a property item +is adjacent to a fixed character. + +Arguments: + c the character + ptype the property type + pdata the data for the type + negated TRUE if it's a negated property (\P or \p{^) + +Returns: TRUE if auto-possessifying is OK +*/ + +static BOOL +check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata, + BOOL negated) +{ +const pcre_uint32 *p; +const ucd_record *prop = GET_UCD(c); + +switch(ptype) + { + case PT_LAMP: + return (prop->chartype == ucp_Lu || + prop->chartype == ucp_Ll || + prop->chartype == ucp_Lt) == negated; + + case PT_GC: + return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated; + + case PT_PC: + return (pdata == prop->chartype) == negated; + + case PT_SC: + return (pdata == prop->script) == negated; + + /* These are specials */ + + case PT_ALNUM: + return (PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated; + + /* Perl space used to exclude VT, but from Perl 5.18 it is included, which + means that Perl space and POSIX space are now identical. PCRE was changed + at release 8.34. */ + + case PT_SPACE: /* Perl space */ + case PT_PXSPACE: /* POSIX space */ + switch(c) + { + HSPACE_CASES: + VSPACE_CASES: + return negated; + + default: + return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated; + } + break; /* Control never reaches here */ + + case PT_WORD: + return (PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N || + c == CHAR_UNDERSCORE) == negated; + + case PT_CLIST: + p = PRIV(ucd_caseless_sets) + prop->caseset; + for (;;) + { + if (c < *p) return !negated; + if (c == *p++) return negated; + } + break; /* Control never reaches here */ + } + +return FALSE; +} +#endif /* SUPPORT_UCP */ + + + +/************************************************* +* Fill the character property list * +*************************************************/ + +/* Checks whether the code points to an opcode that can take part in auto- +possessification, and if so, fills a list with its properties. + +Arguments: + code points to start of expression + utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode + fcc points to case-flipping table + list points to output list + list[0] will be filled with the opcode + list[1] will be non-zero if this opcode + can match an empty character string + list[2..7] depends on the opcode + +Returns: points to the start of the next opcode if *code is accepted + NULL if *code is not accepted +*/ + +static const pcre_uchar * +get_chr_property_list(const pcre_uchar *code, BOOL utf, + const pcre_uint8 *fcc, pcre_uint32 *list) +{ +pcre_uchar c = *code; +pcre_uchar base; +const pcre_uchar *end; +pcre_uint32 chr; + +#ifdef SUPPORT_UCP +pcre_uint32 *clist_dest; +const pcre_uint32 *clist_src; +#else +((void)utf); /* Suppress "unused parameter" compiler warning */ +#endif + +list[0] = c; +list[1] = FALSE; +code++; + +if (c >= OP_STAR && c <= OP_TYPEPOSUPTO) + { + base = get_repeat_base(c); + c -= (base - OP_STAR); + + if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO) + code += IMM2_SIZE; + + list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS); + + switch(base) + { + case OP_STAR: + list[0] = OP_CHAR; + break; + + case OP_STARI: + list[0] = OP_CHARI; + break; + + case OP_NOTSTAR: + list[0] = OP_NOT; + break; + + case OP_NOTSTARI: + list[0] = OP_NOTI; + break; + + case OP_TYPESTAR: + list[0] = *code; + code++; + break; + } + c = list[0]; + } + +switch(c) + { + case OP_NOT_DIGIT: + case OP_DIGIT: + case OP_NOT_WHITESPACE: + case OP_WHITESPACE: + case OP_NOT_WORDCHAR: + case OP_WORDCHAR: + case OP_ANY: + case OP_ALLANY: + case OP_ANYNL: + case OP_NOT_HSPACE: + case OP_HSPACE: + case OP_NOT_VSPACE: + case OP_VSPACE: + case OP_EXTUNI: + case OP_EODN: + case OP_EOD: + case OP_DOLL: + case OP_DOLLM: + return code; + + case OP_CHAR: + case OP_NOT: + GETCHARINCTEST(chr, code); + list[2] = chr; + list[3] = NOTACHAR; + return code; + + case OP_CHARI: + case OP_NOTI: + list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT; + GETCHARINCTEST(chr, code); + list[2] = chr; + +#ifdef SUPPORT_UCP + if (chr < 128 || (chr < 256 && !utf)) + list[3] = fcc[chr]; + else + list[3] = UCD_OTHERCASE(chr); +#elif defined SUPPORT_UTF || !defined COMPILE_PCRE8 + list[3] = (chr < 256) ? fcc[chr] : chr; +#else + list[3] = fcc[chr]; +#endif + + /* The othercase might be the same value. */ + + if (chr == list[3]) + list[3] = NOTACHAR; + else + list[4] = NOTACHAR; + return code; + +#ifdef SUPPORT_UCP + case OP_PROP: + case OP_NOTPROP: + if (code[0] != PT_CLIST) + { + list[2] = code[0]; + list[3] = code[1]; + return code + 2; + } + + /* Convert only if we have enough space. */ + + clist_src = PRIV(ucd_caseless_sets) + code[1]; + clist_dest = list + 2; + code += 2; + + do { + if (clist_dest >= list + 8) + { + /* Early return if there is not enough space. This should never + happen, since all clists are shorter than 5 character now. */ + list[2] = code[0]; + list[3] = code[1]; + return code; + } + *clist_dest++ = *clist_src; + } + while(*clist_src++ != NOTACHAR); + + /* All characters are stored. The terminating NOTACHAR + is copied form the clist itself. */ + + list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT; + return code; +#endif + + case OP_NCLASS: + case OP_CLASS: +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: + if (c == OP_XCLASS) + end = code + GET(code, 0) - 1; + else +#endif + end = code + 32 / sizeof(pcre_uchar); + + switch(*end) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRQUERY: + case OP_CRMINQUERY: + case OP_CRPOSSTAR: + case OP_CRPOSQUERY: + list[1] = TRUE; + end++; + break; + + case OP_CRPLUS: + case OP_CRMINPLUS: + case OP_CRPOSPLUS: + end++; + break; + + case OP_CRRANGE: + case OP_CRMINRANGE: + case OP_CRPOSRANGE: + list[1] = (GET2(end, 1) == 0); + end += 1 + 2 * IMM2_SIZE; + break; + } + list[2] = (pcre_uint32)(end - code); + return end; + } +return NULL; /* Opcode not accepted */ +} + + + +/************************************************* +* Scan further character sets for match * +*************************************************/ + +/* Checks whether the base and the current opcode have a common character, in +which case the base cannot be possessified. + +Arguments: + code points to the byte code + utf TRUE in UTF-8 / UTF-16 / UTF-32 mode + cd static compile data + base_list the data list of the base opcode + +Returns: TRUE if the auto-possessification is possible +*/ + +static BOOL +compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd, + const pcre_uint32 *base_list, const pcre_uchar *base_end, int *rec_limit) +{ +pcre_uchar c; +pcre_uint32 list[8]; +const pcre_uint32 *chr_ptr; +const pcre_uint32 *ochr_ptr; +const pcre_uint32 *list_ptr; +const pcre_uchar *next_code; +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 +const pcre_uchar *xclass_flags; +#endif +const pcre_uint8 *class_bitset; +const pcre_uint8 *set1, *set2, *set_end; +pcre_uint32 chr; +BOOL accepted, invert_bits; +BOOL entered_a_group = FALSE; + +if (*rec_limit == 0) return FALSE; +--(*rec_limit); + +/* Note: the base_list[1] contains whether the current opcode has greedy +(represented by a non-zero value) quantifier. This is a different from +other character type lists, which stores here that the character iterator +matches to an empty string (also represented by a non-zero value). */ + +for(;;) + { + /* All operations move the code pointer forward. + Therefore infinite recursions are not possible. */ + + c = *code; + + /* Skip over callouts */ + + if (c == OP_CALLOUT) + { + code += PRIV(OP_lengths)[c]; + continue; + } + + if (c == OP_ALT) + { + do code += GET(code, 1); while (*code == OP_ALT); + c = *code; + } + + switch(c) + { + case OP_END: + case OP_KETRPOS: + /* TRUE only in greedy case. The non-greedy case could be replaced by + an OP_EXACT, but it is probably not worth it. (And note that OP_EXACT + uses more memory, which we cannot get at this stage.) */ + + return base_list[1] != 0; + + case OP_KET: + /* If the bracket is capturing, and referenced by an OP_RECURSE, or + it is an atomic sub-pattern (assert, once, etc.) the non-greedy case + cannot be converted to a possessive form. */ + + if (base_list[1] == 0) return FALSE; + + switch(*(code - GET(code, 1))) + { + case OP_ASSERT: + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + case OP_ONCE: + case OP_ONCE_NC: + /* Atomic sub-patterns and assertions can always auto-possessify their + last iterator. However, if the group was entered as a result of checking + a previous iterator, this is not possible. */ + + return !entered_a_group; + } + + code += PRIV(OP_lengths)[c]; + continue; + + case OP_ONCE: + case OP_ONCE_NC: + case OP_BRA: + case OP_CBRA: + next_code = code + GET(code, 1); + code += PRIV(OP_lengths)[c]; + + while (*next_code == OP_ALT) + { + if (!compare_opcodes(code, utf, cd, base_list, base_end, rec_limit)) + return FALSE; + code = next_code + 1 + LINK_SIZE; + next_code += GET(next_code, 1); + } + + entered_a_group = TRUE; + continue; + + case OP_BRAZERO: + case OP_BRAMINZERO: + + next_code = code + 1; + if (*next_code != OP_BRA && *next_code != OP_CBRA + && *next_code != OP_ONCE && *next_code != OP_ONCE_NC) return FALSE; + + do next_code += GET(next_code, 1); while (*next_code == OP_ALT); + + /* The bracket content will be checked by the + OP_BRA/OP_CBRA case above. */ + next_code += 1 + LINK_SIZE; + if (!compare_opcodes(next_code, utf, cd, base_list, base_end, rec_limit)) + return FALSE; + + code += PRIV(OP_lengths)[c]; + continue; + + default: + break; + } + + /* Check for a supported opcode, and load its properties. */ + + code = get_chr_property_list(code, utf, cd->fcc, list); + if (code == NULL) return FALSE; /* Unsupported */ + + /* If either opcode is a small character list, set pointers for comparing + characters from that list with another list, or with a property. */ + + if (base_list[0] == OP_CHAR) + { + chr_ptr = base_list + 2; + list_ptr = list; + } + else if (list[0] == OP_CHAR) + { + chr_ptr = list + 2; + list_ptr = base_list; + } + + /* Character bitsets can also be compared to certain opcodes. */ + + else if (base_list[0] == OP_CLASS || list[0] == OP_CLASS +#ifdef COMPILE_PCRE8 + /* In 8 bit, non-UTF mode, OP_CLASS and OP_NCLASS are the same. */ + || (!utf && (base_list[0] == OP_NCLASS || list[0] == OP_NCLASS)) +#endif + ) + { +#ifdef COMPILE_PCRE8 + if (base_list[0] == OP_CLASS || (!utf && base_list[0] == OP_NCLASS)) +#else + if (base_list[0] == OP_CLASS) +#endif + { + set1 = (pcre_uint8 *)(base_end - base_list[2]); + list_ptr = list; + } + else + { + set1 = (pcre_uint8 *)(code - list[2]); + list_ptr = base_list; + } + + invert_bits = FALSE; + switch(list_ptr[0]) + { + case OP_CLASS: + case OP_NCLASS: + set2 = (pcre_uint8 *) + ((list_ptr == list ? code : base_end) - list_ptr[2]); + break; + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: + xclass_flags = (list_ptr == list ? code : base_end) - list_ptr[2] + LINK_SIZE; + if ((*xclass_flags & XCL_HASPROP) != 0) return FALSE; + if ((*xclass_flags & XCL_MAP) == 0) + { + /* No bits are set for characters < 256. */ + if (list[1] == 0) return (*xclass_flags & XCL_NOT) == 0; + /* Might be an empty repeat. */ + continue; + } + set2 = (pcre_uint8 *)(xclass_flags + 1); + break; +#endif + + case OP_NOT_DIGIT: + invert_bits = TRUE; + /* Fall through */ + case OP_DIGIT: + set2 = (pcre_uint8 *)(cd->cbits + cbit_digit); + break; + + case OP_NOT_WHITESPACE: + invert_bits = TRUE; + /* Fall through */ + case OP_WHITESPACE: + set2 = (pcre_uint8 *)(cd->cbits + cbit_space); + break; + + case OP_NOT_WORDCHAR: + invert_bits = TRUE; + /* Fall through */ + case OP_WORDCHAR: + set2 = (pcre_uint8 *)(cd->cbits + cbit_word); + break; + + default: + return FALSE; + } + + /* Because the sets are unaligned, we need + to perform byte comparison here. */ + set_end = set1 + 32; + if (invert_bits) + { + do + { + if ((*set1++ & ~(*set2++)) != 0) return FALSE; + } + while (set1 < set_end); + } + else + { + do + { + if ((*set1++ & *set2++) != 0) return FALSE; + } + while (set1 < set_end); + } + + if (list[1] == 0) return TRUE; + /* Might be an empty repeat. */ + continue; + } + + /* Some property combinations also acceptable. Unicode property opcodes are + processed specially; the rest can be handled with a lookup table. */ + + else + { + pcre_uint32 leftop, rightop; + + leftop = base_list[0]; + rightop = list[0]; + +#ifdef SUPPORT_UCP + accepted = FALSE; /* Always set in non-unicode case. */ + if (leftop == OP_PROP || leftop == OP_NOTPROP) + { + if (rightop == OP_EOD) + accepted = TRUE; + else if (rightop == OP_PROP || rightop == OP_NOTPROP) + { + int n; + const pcre_uint8 *p; + BOOL same = leftop == rightop; + BOOL lisprop = leftop == OP_PROP; + BOOL risprop = rightop == OP_PROP; + BOOL bothprop = lisprop && risprop; + + /* There's a table that specifies how each combination is to be + processed: + 0 Always return FALSE (never auto-possessify) + 1 Character groups are distinct (possessify if both are OP_PROP) + 2 Check character categories in the same group (general or particular) + 3 Return TRUE if the two opcodes are not the same + ... see comments below + */ + + n = propposstab[base_list[2]][list[2]]; + switch(n) + { + case 0: break; + case 1: accepted = bothprop; break; + case 2: accepted = (base_list[3] == list[3]) != same; break; + case 3: accepted = !same; break; + + case 4: /* Left general category, right particular category */ + accepted = risprop && catposstab[base_list[3]][list[3]] == same; + break; + + case 5: /* Right general category, left particular category */ + accepted = lisprop && catposstab[list[3]][base_list[3]] == same; + break; + + /* This code is logically tricky. Think hard before fiddling with it. + The posspropstab table has four entries per row. Each row relates to + one of PCRE's special properties such as ALNUM or SPACE or WORD. + Only WORD actually needs all four entries, but using repeats for the + others means they can all use the same code below. + + The first two entries in each row are Unicode general categories, and + apply always, because all the characters they include are part of the + PCRE character set. The third and fourth entries are a general and a + particular category, respectively, that include one or more relevant + characters. One or the other is used, depending on whether the check + is for a general or a particular category. However, in both cases the + category contains more characters than the specials that are defined + for the property being tested against. Therefore, it cannot be used + in a NOTPROP case. + + Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po. + Underscore is covered by ucp_P or ucp_Po. */ + + case 6: /* Left alphanum vs right general category */ + case 7: /* Left space vs right general category */ + case 8: /* Left word vs right general category */ + p = posspropstab[n-6]; + accepted = risprop && lisprop == + (list[3] != p[0] && + list[3] != p[1] && + (list[3] != p[2] || !lisprop)); + break; + + case 9: /* Right alphanum vs left general category */ + case 10: /* Right space vs left general category */ + case 11: /* Right word vs left general category */ + p = posspropstab[n-9]; + accepted = lisprop && risprop == + (base_list[3] != p[0] && + base_list[3] != p[1] && + (base_list[3] != p[2] || !risprop)); + break; + + case 12: /* Left alphanum vs right particular category */ + case 13: /* Left space vs right particular category */ + case 14: /* Left word vs right particular category */ + p = posspropstab[n-12]; + accepted = risprop && lisprop == + (catposstab[p[0]][list[3]] && + catposstab[p[1]][list[3]] && + (list[3] != p[3] || !lisprop)); + break; + + case 15: /* Right alphanum vs left particular category */ + case 16: /* Right space vs left particular category */ + case 17: /* Right word vs left particular category */ + p = posspropstab[n-15]; + accepted = lisprop && risprop == + (catposstab[p[0]][base_list[3]] && + catposstab[p[1]][base_list[3]] && + (base_list[3] != p[3] || !risprop)); + break; + } + } + } + + else +#endif /* SUPPORT_UCP */ + + accepted = leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP && + rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP && + autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP]; + + if (!accepted) return FALSE; + + if (list[1] == 0) return TRUE; + /* Might be an empty repeat. */ + continue; + } + + /* Control reaches here only if one of the items is a small character list. + All characters are checked against the other side. */ + + do + { + chr = *chr_ptr; + + switch(list_ptr[0]) + { + case OP_CHAR: + ochr_ptr = list_ptr + 2; + do + { + if (chr == *ochr_ptr) return FALSE; + ochr_ptr++; + } + while(*ochr_ptr != NOTACHAR); + break; + + case OP_NOT: + ochr_ptr = list_ptr + 2; + do + { + if (chr == *ochr_ptr) + break; + ochr_ptr++; + } + while(*ochr_ptr != NOTACHAR); + if (*ochr_ptr == NOTACHAR) return FALSE; /* Not found */ + break; + + /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* + set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */ + + case OP_DIGIT: + if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE; + break; + + case OP_NOT_DIGIT: + if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE; + break; + + case OP_WHITESPACE: + if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE; + break; + + case OP_NOT_WHITESPACE: + if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE; + break; + + case OP_WORDCHAR: + if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE; + break; + + case OP_NOT_WORDCHAR: + if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE; + break; + + case OP_HSPACE: + switch(chr) + { + HSPACE_CASES: return FALSE; + default: break; + } + break; + + case OP_NOT_HSPACE: + switch(chr) + { + HSPACE_CASES: break; + default: return FALSE; + } + break; + + case OP_ANYNL: + case OP_VSPACE: + switch(chr) + { + VSPACE_CASES: return FALSE; + default: break; + } + break; + + case OP_NOT_VSPACE: + switch(chr) + { + VSPACE_CASES: break; + default: return FALSE; + } + break; + + case OP_DOLL: + case OP_EODN: + switch (chr) + { + case CHAR_CR: + case CHAR_LF: + case CHAR_VT: + case CHAR_FF: + case CHAR_NEL: +#ifndef EBCDIC + case 0x2028: + case 0x2029: +#endif /* Not EBCDIC */ + return FALSE; + } + break; + + case OP_EOD: /* Can always possessify before \z */ + break; + +#ifdef SUPPORT_UCP + case OP_PROP: + case OP_NOTPROP: + if (!check_char_prop(chr, list_ptr[2], list_ptr[3], + list_ptr[0] == OP_NOTPROP)) + return FALSE; + break; +#endif + + case OP_NCLASS: + if (chr > 255) return FALSE; + /* Fall through */ + + case OP_CLASS: + if (chr > 255) break; + class_bitset = (pcre_uint8 *) + ((list_ptr == list ? code : base_end) - list_ptr[2]); + if ((class_bitset[chr >> 3] & (1U << (chr & 7))) != 0) return FALSE; + break; + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: + if (PRIV(xclass)(chr, (list_ptr == list ? code : base_end) - + list_ptr[2] + LINK_SIZE, utf)) return FALSE; + break; +#endif + + default: + return FALSE; + } + + chr_ptr++; + } + while(*chr_ptr != NOTACHAR); + + /* At least one character must be matched from this opcode. */ + + if (list[1] == 0) return TRUE; + } + +/* Control never reaches here. There used to be a fail-save return FALSE; here, +but some compilers complain about an unreachable statement. */ + +} + + + +/************************************************* +* Scan compiled regex for auto-possession * +*************************************************/ + +/* Replaces single character iterations with their possessive alternatives +if appropriate. This function modifies the compiled opcode! + +Arguments: + code points to start of the byte code + utf TRUE in UTF-8 / UTF-16 / UTF-32 mode + cd static compile data + +Returns: nothing +*/ + +static void +auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd) +{ +register pcre_uchar c; +const pcre_uchar *end; +pcre_uchar *repeat_opcode; +pcre_uint32 list[8]; +int rec_limit; + +for (;;) + { + c = *code; + + /* When a pattern with bad UTF-8 encoding is compiled with NO_UTF_CHECK, + it may compile without complaining, but may get into a loop here if the code + pointer points to a bad value. This is, of course a documentated possibility, + when NO_UTF_CHECK is set, so it isn't a bug, but we can detect this case and + just give up on this optimization. */ + + if (c >= OP_TABLE_LENGTH) return; + + if (c >= OP_STAR && c <= OP_TYPEPOSUPTO) + { + c -= get_repeat_base(c) - OP_STAR; + end = (c <= OP_MINUPTO) ? + get_chr_property_list(code, utf, cd->fcc, list) : NULL; + list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO; + + rec_limit = 1000; + if (end != NULL && compare_opcodes(end, utf, cd, list, end, &rec_limit)) + { + switch(c) + { + case OP_STAR: + *code += OP_POSSTAR - OP_STAR; + break; + + case OP_MINSTAR: + *code += OP_POSSTAR - OP_MINSTAR; + break; + + case OP_PLUS: + *code += OP_POSPLUS - OP_PLUS; + break; + + case OP_MINPLUS: + *code += OP_POSPLUS - OP_MINPLUS; + break; + + case OP_QUERY: + *code += OP_POSQUERY - OP_QUERY; + break; + + case OP_MINQUERY: + *code += OP_POSQUERY - OP_MINQUERY; + break; + + case OP_UPTO: + *code += OP_POSUPTO - OP_UPTO; + break; + + case OP_MINUPTO: + *code += OP_POSUPTO - OP_MINUPTO; + break; + } + } + c = *code; + } + else if (c == OP_CLASS || c == OP_NCLASS || c == OP_XCLASS) + { +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + if (c == OP_XCLASS) + repeat_opcode = code + GET(code, 1); + else +#endif + repeat_opcode = code + 1 + (32 / sizeof(pcre_uchar)); + + c = *repeat_opcode; + if (c >= OP_CRSTAR && c <= OP_CRMINRANGE) + { + /* end must not be NULL. */ + end = get_chr_property_list(code, utf, cd->fcc, list); + + list[1] = (c & 1) == 0; + + rec_limit = 1000; + if (compare_opcodes(end, utf, cd, list, end, &rec_limit)) + { + switch (c) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + *repeat_opcode = OP_CRPOSSTAR; + break; + + case OP_CRPLUS: + case OP_CRMINPLUS: + *repeat_opcode = OP_CRPOSPLUS; + break; + + case OP_CRQUERY: + case OP_CRMINQUERY: + *repeat_opcode = OP_CRPOSQUERY; + break; + + case OP_CRRANGE: + case OP_CRMINRANGE: + *repeat_opcode = OP_CRPOSRANGE; + break; + } + } + } + c = *code; + } + + switch(c) + { + case OP_END: + return; + + case OP_TYPESTAR: + case OP_TYPEMINSTAR: + case OP_TYPEPLUS: + case OP_TYPEMINPLUS: + case OP_TYPEQUERY: + case OP_TYPEMINQUERY: + case OP_TYPEPOSSTAR: + case OP_TYPEPOSPLUS: + case OP_TYPEPOSQUERY: + if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2; + break; + + case OP_TYPEUPTO: + case OP_TYPEMINUPTO: + case OP_TYPEEXACT: + case OP_TYPEPOSUPTO: + if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP) + code += 2; + break; + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: + code += GET(code, 1); + break; +#endif + + case OP_MARK: + case OP_PRUNE_ARG: + case OP_SKIP_ARG: + case OP_THEN_ARG: + code += code[1]; + break; + } + + /* Add in the fixed length from the table */ + + code += PRIV(OP_lengths)[c]; + + /* In UTF-8 mode, opcodes that are followed by a character may be followed by + a multi-byte character. The length in the table is a minimum, so we have to + arrange to skip the extra bytes. */ + +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf) switch(c) + { + case OP_CHAR: + case OP_CHARI: + case OP_NOT: + case OP_NOTI: + case OP_STAR: + case OP_MINSTAR: + case OP_PLUS: + case OP_MINPLUS: + case OP_QUERY: + case OP_MINQUERY: + case OP_UPTO: + case OP_MINUPTO: + case OP_EXACT: + case OP_POSSTAR: + case OP_POSPLUS: + case OP_POSQUERY: + case OP_POSUPTO: + case OP_STARI: + case OP_MINSTARI: + case OP_PLUSI: + case OP_MINPLUSI: + case OP_QUERYI: + case OP_MINQUERYI: + case OP_UPTOI: + case OP_MINUPTOI: + case OP_EXACTI: + case OP_POSSTARI: + case OP_POSPLUSI: + case OP_POSQUERYI: + case OP_POSUPTOI: + case OP_NOTSTAR: + case OP_NOTMINSTAR: + case OP_NOTPLUS: + case OP_NOTMINPLUS: + case OP_NOTQUERY: + case OP_NOTMINQUERY: + case OP_NOTUPTO: + case OP_NOTMINUPTO: + case OP_NOTEXACT: + case OP_NOTPOSSTAR: + case OP_NOTPOSPLUS: + case OP_NOTPOSQUERY: + case OP_NOTPOSUPTO: + case OP_NOTSTARI: + case OP_NOTMINSTARI: + case OP_NOTPLUSI: + case OP_NOTMINPLUSI: + case OP_NOTQUERYI: + case OP_NOTMINQUERYI: + case OP_NOTUPTOI: + case OP_NOTMINUPTOI: + case OP_NOTEXACTI: + case OP_NOTPOSSTARI: + case OP_NOTPOSPLUSI: + case OP_NOTPOSQUERYI: + case OP_NOTPOSUPTOI: + if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]); + break; + } +#else + (void)(utf); /* Keep compiler happy by referencing function argument */ +#endif + } +} + + + +/************************************************* +* Check for POSIX class syntax * +*************************************************/ + +/* This function is called when the sequence "[:" or "[." or "[=" is +encountered in a character class. It checks whether this is followed by a +sequence of characters terminated by a matching ":]" or ".]" or "=]". If we +reach an unescaped ']' without the special preceding character, return FALSE. + +Originally, this function only recognized a sequence of letters between the +terminators, but it seems that Perl recognizes any sequence of characters, +though of course unknown POSIX names are subsequently rejected. Perl gives an +"Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE +didn't consider this to be a POSIX class. Likewise for [:1234:]. + +The problem in trying to be exactly like Perl is in the handling of escapes. We +have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX +class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code +below handles the special cases \\ and \], but does not try to do any other +escape processing. This makes it different from Perl for cases such as +[:l\ower:] where Perl recognizes it as the POSIX class "lower" but PCRE does +not recognize "l\ower". This is a lesser evil than not diagnosing bad classes +when Perl does, I think. + +A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not. +It seems that the appearance of a nested POSIX class supersedes an apparent +external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or +a digit. + +In Perl, unescaped square brackets may also appear as part of class names. For +example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for +[:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not +seem right at all. PCRE does not allow closing square brackets in POSIX class +names. + +Arguments: + ptr pointer to the initial [ + endptr where to return the end pointer + +Returns: TRUE or FALSE +*/ + +static BOOL +check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr) +{ +pcre_uchar terminator; /* Don't combine these lines; the Solaris cc */ +terminator = *(++ptr); /* compiler warns about "non-constant" initializer. */ +for (++ptr; *ptr != CHAR_NULL; ptr++) + { + if (*ptr == CHAR_BACKSLASH && + (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET || + ptr[1] == CHAR_BACKSLASH)) + ptr++; + else if ((*ptr == CHAR_LEFT_SQUARE_BRACKET && ptr[1] == terminator) || + *ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE; + else if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) + { + *endptr = ptr; + return TRUE; + } + } +return FALSE; +} + + + + +/************************************************* +* Check POSIX class name * +*************************************************/ + +/* This function is called to check the name given in a POSIX-style class entry +such as [:alnum:]. + +Arguments: + ptr points to the first letter + len the length of the name + +Returns: a value representing the name, or -1 if unknown +*/ + +static int +check_posix_name(const pcre_uchar *ptr, int len) +{ +const char *pn = posix_names; +register int yield = 0; +while (posix_name_lengths[yield] != 0) + { + if (len == posix_name_lengths[yield] && + STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield; + pn += posix_name_lengths[yield] + 1; + yield++; + } +return -1; +} + + +/************************************************* +* Adjust OP_RECURSE items in repeated group * +*************************************************/ + +/* OP_RECURSE items contain an offset from the start of the regex to the group +that is referenced. This means that groups can be replicated for fixed +repetition simply by copying (because the recursion is allowed to refer to +earlier groups that are outside the current group). However, when a group is +optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is +inserted before it, after it has been compiled. This means that any OP_RECURSE +items within it that refer to the group itself or any contained groups have to +have their offsets adjusted. That one of the jobs of this function. Before it +is called, the partially compiled regex must be temporarily terminated with +OP_END. + +This function has been extended to cope with forward references for recursions +and subroutine calls. It must check the list of such references for the +group we are dealing with. If it finds that one of the recursions in the +current group is on this list, it does not adjust the value in the reference +(which is a group number). After the group has been scanned, all the offsets in +the forward reference list for the group are adjusted. + +Arguments: + group points to the start of the group + adjust the amount by which the group is to be moved + utf TRUE in UTF-8 / UTF-16 / UTF-32 mode + cd contains pointers to tables etc. + save_hwm_offset the hwm forward reference offset at the start of the group + +Returns: nothing +*/ + +static void +adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd, + size_t save_hwm_offset) +{ +int offset; +pcre_uchar *hc; +pcre_uchar *ptr = group; + +while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL) + { + for (hc = (pcre_uchar *)cd->start_workspace + save_hwm_offset; hc < cd->hwm; + hc += LINK_SIZE) + { + offset = (int)GET(hc, 0); + if (cd->start_code + offset == ptr + 1) break; + } + + /* If we have not found this recursion on the forward reference list, adjust + the recursion's offset if it's after the start of this group. */ + + if (hc >= cd->hwm) + { + offset = (int)GET(ptr, 1); + if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust); + } + + ptr += 1 + LINK_SIZE; + } + +/* Now adjust all forward reference offsets for the group. */ + +for (hc = (pcre_uchar *)cd->start_workspace + save_hwm_offset; hc < cd->hwm; + hc += LINK_SIZE) + { + offset = (int)GET(hc, 0); + PUT(hc, 0, offset + adjust); + } +} + + + +/************************************************* +* Insert an automatic callout point * +*************************************************/ + +/* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert +callout points before each pattern item. + +Arguments: + code current code pointer + ptr current pattern pointer + cd pointers to tables etc + +Returns: new code pointer +*/ + +static pcre_uchar * +auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd) +{ +*code++ = OP_CALLOUT; +*code++ = 255; +PUT(code, 0, (int)(ptr - cd->start_pattern)); /* Pattern offset */ +PUT(code, LINK_SIZE, 0); /* Default length */ +return code + 2 * LINK_SIZE; +} + + + +/************************************************* +* Complete a callout item * +*************************************************/ + +/* A callout item contains the length of the next item in the pattern, which +we can't fill in till after we have reached the relevant point. This is used +for both automatic and manual callouts. + +Arguments: + previous_callout points to previous callout item + ptr current pattern pointer + cd pointers to tables etc + +Returns: nothing +*/ + +static void +complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd) +{ +int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2)); +PUT(previous_callout, 2 + LINK_SIZE, length); +} + + + +#ifdef SUPPORT_UCP +/************************************************* +* Get othercase range * +*************************************************/ + +/* This function is passed the start and end of a class range, in UTF-8 mode +with UCP support. It searches up the characters, looking for ranges of +characters in the "other" case. Each call returns the next one, updating the +start address. A character with multiple other cases is returned on its own +with a special return value. + +Arguments: + cptr points to starting character value; updated + d end value + ocptr where to put start of othercase range + odptr where to put end of othercase range + +Yield: -1 when no more + 0 when a range is returned + >0 the CASESET offset for char with multiple other cases + in this case, ocptr contains the original +*/ + +static int +get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr, + pcre_uint32 *odptr) +{ +pcre_uint32 c, othercase, next; +unsigned int co; + +/* Find the first character that has an other case. If it has multiple other +cases, return its case offset value. */ + +for (c = *cptr; c <= d; c++) + { + if ((co = UCD_CASESET(c)) != 0) + { + *ocptr = c++; /* Character that has the set */ + *cptr = c; /* Rest of input range */ + return (int)co; + } + if ((othercase = UCD_OTHERCASE(c)) != c) break; + } + +if (c > d) return -1; /* Reached end of range */ + +/* Found a character that has a single other case. Search for the end of the +range, which is either the end of the input range, or a character that has zero +or more than one other cases. */ + +*ocptr = othercase; +next = othercase + 1; + +for (++c; c <= d; c++) + { + if ((co = UCD_CASESET(c)) != 0 || UCD_OTHERCASE(c) != next) break; + next++; + } + +*odptr = next - 1; /* End of othercase range */ +*cptr = c; /* Rest of input range */ +return 0; +} +#endif /* SUPPORT_UCP */ + + + +/************************************************* +* Add a character or range to a class * +*************************************************/ + +/* This function packages up the logic of adding a character or range of +characters to a class. The character values in the arguments will be within the +valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is +mutually recursive with the function immediately below. + +Arguments: + classbits the bit map for characters < 256 + uchardptr points to the pointer for extra data + options the options word + cd contains pointers to tables etc. + start start of range character + end end of range character + +Returns: the number of < 256 characters added + the pointer to extra data is updated +*/ + +static int +add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options, + compile_data *cd, pcre_uint32 start, pcre_uint32 end) +{ +pcre_uint32 c; +pcre_uint32 classbits_end = (end <= 0xff ? end : 0xff); +int n8 = 0; + +((void)uchardptr); +((void)propposstab); +((void)catposstab); +((void)posspropstab); + +/* If caseless matching is required, scan the range and process alternate +cases. In Unicode, there are 8-bit characters that have alternate cases that +are greater than 255 and vice-versa. Sometimes we can just extend the original +range. */ + +if ((options & PCRE_CASELESS) != 0) + { +#ifdef SUPPORT_UCP + if ((options & PCRE_UTF8) != 0) + { + int rc; + pcre_uint32 oc, od; + + options &= ~PCRE_CASELESS; /* Remove for recursive calls */ + c = start; + + while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0) + { + /* Handle a single character that has more than one other case. */ + + if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd, + PRIV(ucd_caseless_sets) + rc, oc); + + /* Do nothing if the other case range is within the original range. */ + + else if (oc >= start && od <= end) continue; + + /* Extend the original range if there is overlap, noting that if oc < c, we + can't have od > end because a subrange is always shorter than the basic + range. Otherwise, use a recursive call to add the additional range. */ + + else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */ + else if (od > end && oc <= end + 1) + { + end = od; /* Extend upwards */ + if (end > classbits_end) classbits_end = (end <= 0xff ? end : 0xff); + } + else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od); + } + } + else +#endif /* SUPPORT_UCP */ + + /* Not UTF-mode, or no UCP */ + + for (c = start; c <= classbits_end; c++) + { + SETBIT(classbits, cd->fcc[c]); + n8++; + } + } + +/* Now handle the original range. Adjust the final value according to the bit +length - this means that the same lists of (e.g.) horizontal spaces can be used +in all cases. */ + +#if defined COMPILE_PCRE8 +#ifdef SUPPORT_UTF + if ((options & PCRE_UTF8) == 0) +#endif + if (end > 0xff) end = 0xff; + +#elif defined COMPILE_PCRE16 +#ifdef SUPPORT_UTF + if ((options & PCRE_UTF16) == 0) +#endif + if (end > 0xffff) end = 0xffff; + +#endif /* COMPILE_PCRE[8|16] */ + +/* Use the bitmap for characters < 256. Otherwise use extra data.*/ + +for (c = start; c <= classbits_end; c++) + { + /* Regardless of start, c will always be <= 255. */ + SETBIT(classbits, c); + n8++; + } + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 +if (start <= 0xff) start = 0xff + 1; + +if (end >= start) + { + pcre_uchar *uchardata = *uchardptr; +#ifdef SUPPORT_UTF + if ((options & PCRE_UTF8) != 0) /* All UTFs use the same flag bit */ + { + if (start < end) + { + *uchardata++ = XCL_RANGE; + uchardata += PRIV(ord2utf)(start, uchardata); + uchardata += PRIV(ord2utf)(end, uchardata); + } + else if (start == end) + { + *uchardata++ = XCL_SINGLE; + uchardata += PRIV(ord2utf)(start, uchardata); + } + } + else +#endif /* SUPPORT_UTF */ + + /* Without UTF support, character values are constrained by the bit length, + and can only be > 256 for 16-bit and 32-bit libraries. */ + +#ifdef COMPILE_PCRE8 + {} +#else + if (start < end) + { + *uchardata++ = XCL_RANGE; + *uchardata++ = start; + *uchardata++ = end; + } + else if (start == end) + { + *uchardata++ = XCL_SINGLE; + *uchardata++ = start; + } +#endif + + *uchardptr = uchardata; /* Updata extra data pointer */ + } +#endif /* SUPPORT_UTF || !COMPILE_PCRE8 */ + +return n8; /* Number of 8-bit characters */ +} + + + + +/************************************************* +* Add a list of characters to a class * +*************************************************/ + +/* This function is used for adding a list of case-equivalent characters to a +class, and also for adding a list of horizontal or vertical whitespace. If the +list is in order (which it should be), ranges of characters are detected and +handled appropriately. This function is mutually recursive with the function +above. + +Arguments: + classbits the bit map for characters < 256 + uchardptr points to the pointer for extra data + options the options word + cd contains pointers to tables etc. + p points to row of 32-bit values, terminated by NOTACHAR + except character to omit; this is used when adding lists of + case-equivalent characters to avoid including the one we + already know about + +Returns: the number of < 256 characters added + the pointer to extra data is updated +*/ + +static int +add_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options, + compile_data *cd, const pcre_uint32 *p, unsigned int except) +{ +int n8 = 0; +while (p[0] < NOTACHAR) + { + int n = 0; + if (p[0] != except) + { + while(p[n+1] == p[0] + n + 1) n++; + n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]); + } + p += n + 1; + } +return n8; +} + + + +/************************************************* +* Add characters not in a list to a class * +*************************************************/ + +/* This function is used for adding the complement of a list of horizontal or +vertical whitespace to a class. The list must be in order. + +Arguments: + classbits the bit map for characters < 256 + uchardptr points to the pointer for extra data + options the options word + cd contains pointers to tables etc. + p points to row of 32-bit values, terminated by NOTACHAR + +Returns: the number of < 256 characters added + the pointer to extra data is updated +*/ + +static int +add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, + int options, compile_data *cd, const pcre_uint32 *p) +{ +BOOL utf = (options & PCRE_UTF8) != 0; +int n8 = 0; +if (p[0] > 0) + n8 += add_to_class(classbits, uchardptr, options, cd, 0, p[0] - 1); +while (p[0] < NOTACHAR) + { + while (p[1] == p[0] + 1) p++; + n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1, + (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1); + p++; + } +return n8; +} + + + +/************************************************* +* Compile one branch * +*************************************************/ + +/* Scan the pattern, compiling it into the a vector. If the options are +changed during the branch, the pointer is used to change the external options +bits. This function is used during the pre-compile phase when we are trying +to find out the amount of memory needed, as well as during the real compile +phase. The value of lengthptr distinguishes the two phases. + +Arguments: + optionsptr pointer to the option bits + codeptr points to the pointer to the current code point + ptrptr points to the current pattern pointer + errorcodeptr points to error code variable + firstcharptr place to put the first required character + firstcharflagsptr place to put the first character flags, or a negative number + reqcharptr place to put the last required character + reqcharflagsptr place to put the last required character flags, or a negative number + bcptr points to current branch chain + cond_depth conditional nesting depth + cd contains pointers to tables etc. + lengthptr NULL during the real compile phase + points to length accumulator during pre-compile phase + +Returns: TRUE on success + FALSE, with *errorcodeptr set non-zero on error +*/ + +static BOOL +compile_branch(int *optionsptr, pcre_uchar **codeptr, + const pcre_uchar **ptrptr, int *errorcodeptr, + pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr, + pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr, + branch_chain *bcptr, int cond_depth, + compile_data *cd, int *lengthptr) +{ +int repeat_type, op_type; +int repeat_min = 0, repeat_max = 0; /* To please picky compilers */ +int bravalue = 0; +int greedy_default, greedy_non_default; +pcre_uint32 firstchar, reqchar; +pcre_int32 firstcharflags, reqcharflags; +pcre_uint32 zeroreqchar, zerofirstchar; +pcre_int32 zeroreqcharflags, zerofirstcharflags; +pcre_int32 req_caseopt, reqvary, tempreqvary; +int options = *optionsptr; /* May change dynamically */ +int after_manual_callout = 0; +int length_prevgroup = 0; +register pcre_uint32 c; +int escape; +register pcre_uchar *code = *codeptr; +pcre_uchar *last_code = code; +pcre_uchar *orig_code = code; +pcre_uchar *tempcode; +BOOL inescq = FALSE; +BOOL groupsetfirstchar = FALSE; +const pcre_uchar *ptr = *ptrptr; +const pcre_uchar *tempptr; +const pcre_uchar *nestptr = NULL; +pcre_uchar *previous = NULL; +pcre_uchar *previous_callout = NULL; +size_t item_hwm_offset = 0; +pcre_uint8 classbits[32]; + +/* We can fish out the UTF-8 setting once and for all into a BOOL, but we +must not do this for other options (e.g. PCRE_EXTENDED) because they may change +dynamically as we process the pattern. */ + +#ifdef SUPPORT_UTF +/* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */ +BOOL utf = (options & PCRE_UTF8) != 0; +#ifndef COMPILE_PCRE32 +pcre_uchar utf_chars[6]; +#endif +#else +BOOL utf = FALSE; +#endif + +/* Helper variables for OP_XCLASS opcode (for characters > 255). We define +class_uchardata always so that it can be passed to add_to_class() always, +though it will not be used in non-UTF 8-bit cases. This avoids having to supply +alternative calls for the different cases. */ + +pcre_uchar *class_uchardata; +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 +BOOL xclass; +pcre_uchar *class_uchardata_base; +#endif + +#ifdef PCRE_DEBUG +if (lengthptr != NULL) DPRINTF((">> start branch\n")); +#endif + +/* Set up the default and non-default settings for greediness */ + +greedy_default = ((options & PCRE_UNGREEDY) != 0); +greedy_non_default = greedy_default ^ 1; + +/* Initialize no first byte, no required byte. REQ_UNSET means "no char +matching encountered yet". It gets changed to REQ_NONE if we hit something that +matches a non-fixed char first char; reqchar just remains unset if we never +find one. + +When we hit a repeat whose minimum is zero, we may have to adjust these values +to take the zero repeat into account. This is implemented by setting them to +zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual +item types that can be repeated set these backoff variables appropriately. */ + +firstchar = reqchar = zerofirstchar = zeroreqchar = 0; +firstcharflags = reqcharflags = zerofirstcharflags = zeroreqcharflags = REQ_UNSET; + +/* The variable req_caseopt contains either the REQ_CASELESS value +or zero, according to the current setting of the caseless flag. The +REQ_CASELESS leaves the lower 28 bit empty. It is added into the +firstchar or reqchar variables to record the case status of the +value. This is used only for ASCII characters. */ + +req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS:0; + +/* Switch on next character until the end of the branch */ + +for (;; ptr++) + { + BOOL negate_class; + BOOL should_flip_negation; + BOOL possessive_quantifier; + BOOL is_quantifier; + BOOL is_recurse; + BOOL reset_bracount; + int class_has_8bitchar; + int class_one_char; +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + BOOL xclass_has_prop; +#endif + int newoptions; + int recno; + int refsign; + int skipbytes; + pcre_uint32 subreqchar, subfirstchar; + pcre_int32 subreqcharflags, subfirstcharflags; + int terminator; + unsigned int mclength; + unsigned int tempbracount; + pcre_uint32 ec; + pcre_uchar mcbuffer[8]; + + /* Come here to restart the loop without advancing the pointer. */ + + REDO_LOOP: + + /* Get next character in the pattern */ + + c = *ptr; + + /* If we are at the end of a nested substitution, revert to the outer level + string. Nesting only happens one level deep. */ + + if (c == CHAR_NULL && nestptr != NULL) + { + ptr = nestptr; + nestptr = NULL; + c = *ptr; + } + + /* If we are in the pre-compile phase, accumulate the length used for the + previous cycle of this loop. */ + + if (lengthptr != NULL) + { +#ifdef PCRE_DEBUG + if (code > cd->hwm) cd->hwm = code; /* High water info */ +#endif + if (code > cd->start_workspace + cd->workspace_size - + WORK_SIZE_SAFETY_MARGIN) /* Check for overrun */ + { + *errorcodeptr = (code >= cd->start_workspace + cd->workspace_size)? + ERR52 : ERR87; + goto FAILED; + } + + /* There is at least one situation where code goes backwards: this is the + case of a zero quantifier after a class (e.g. [ab]{0}). At compile time, + the class is simply eliminated. However, it is created first, so we have to + allow memory for it. Therefore, don't ever reduce the length at this point. + */ + + if (code < last_code) code = last_code; + + /* Paranoid check for integer overflow */ + + if (OFLOW_MAX - *lengthptr < code - last_code) + { + *errorcodeptr = ERR20; + goto FAILED; + } + + *lengthptr += (int)(code - last_code); + DPRINTF(("length=%d added %d c=%c (0x%x)\n", *lengthptr, + (int)(code - last_code), c, c)); + + /* If "previous" is set and it is not at the start of the work space, move + it back to there, in order to avoid filling up the work space. Otherwise, + if "previous" is NULL, reset the current code pointer to the start. */ + + if (previous != NULL) + { + if (previous > orig_code) + { + memmove(orig_code, previous, IN_UCHARS(code - previous)); + code -= previous - orig_code; + previous = orig_code; + } + } + else code = orig_code; + + /* Remember where this code item starts so we can pick up the length + next time round. */ + + last_code = code; + } + + /* In the real compile phase, just check the workspace used by the forward + reference list. */ + + else if (cd->hwm > cd->start_workspace + cd->workspace_size) + { + *errorcodeptr = ERR52; + goto FAILED; + } + + /* If in \Q...\E, check for the end; if not, we have a literal. Otherwise an + isolated \E is ignored. */ + + if (c != CHAR_NULL) + { + if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E) + { + inescq = FALSE; + ptr++; + continue; + } + else if (inescq) + { + if (previous_callout != NULL) + { + if (lengthptr == NULL) /* Don't attempt in pre-compile phase */ + complete_callout(previous_callout, ptr, cd); + previous_callout = NULL; + } + if ((options & PCRE_AUTO_CALLOUT) != 0) + { + previous_callout = code; + code = auto_callout(code, ptr, cd); + } + goto NORMAL_CHAR; + } + + /* Check for the start of a \Q...\E sequence. We must do this here rather + than later in case it is immediately followed by \E, which turns it into a + "do nothing" sequence. */ + + if (c == CHAR_BACKSLASH && ptr[1] == CHAR_Q) + { + inescq = TRUE; + ptr++; + continue; + } + } + + /* In extended mode, skip white space and comments. */ + + if ((options & PCRE_EXTENDED) != 0) + { + const pcre_uchar *wscptr = ptr; + while (MAX_255(c) && (cd->ctypes[c] & ctype_space) != 0) c = *(++ptr); + if (c == CHAR_NUMBER_SIGN) + { + ptr++; + while (*ptr != CHAR_NULL) + { + if (IS_NEWLINE(ptr)) /* For non-fixed-length newline cases, */ + { /* IS_NEWLINE sets cd->nllen. */ + ptr += cd->nllen; + break; + } + ptr++; +#ifdef SUPPORT_UTF + if (utf) FORWARDCHAR(ptr); +#endif + } + } + + /* If we skipped any characters, restart the loop. Otherwise, we didn't see + a comment. */ + + if (ptr > wscptr) goto REDO_LOOP; + } + + /* Skip over (?# comments. We need to do this here because we want to know if + the next thing is a quantifier, and these comments may come between an item + and its quantifier. */ + + if (c == CHAR_LEFT_PARENTHESIS && ptr[1] == CHAR_QUESTION_MARK && + ptr[2] == CHAR_NUMBER_SIGN) + { + ptr += 3; + while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++; + if (*ptr == CHAR_NULL) + { + *errorcodeptr = ERR18; + goto FAILED; + } + continue; + } + + /* See if the next thing is a quantifier. */ + + is_quantifier = + c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK || + (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1)); + + /* Fill in length of a previous callout, except when the next thing is a + quantifier or when processing a property substitution string in UCP mode. */ + + if (!is_quantifier && previous_callout != NULL && nestptr == NULL && + after_manual_callout-- <= 0) + { + if (lengthptr == NULL) /* Don't attempt in pre-compile phase */ + complete_callout(previous_callout, ptr, cd); + previous_callout = NULL; + } + + /* Create auto callout, except for quantifiers, or while processing property + strings that are substituted for \w etc in UCP mode. */ + + if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier && nestptr == NULL) + { + previous_callout = code; + code = auto_callout(code, ptr, cd); + } + + /* Process the next pattern item. */ + + switch(c) + { + /* ===================================================================*/ + case CHAR_NULL: /* The branch terminates at string end */ + case CHAR_VERTICAL_LINE: /* or | or ) */ + case CHAR_RIGHT_PARENTHESIS: + *firstcharptr = firstchar; + *firstcharflagsptr = firstcharflags; + *reqcharptr = reqchar; + *reqcharflagsptr = reqcharflags; + *codeptr = code; + *ptrptr = ptr; + if (lengthptr != NULL) + { + if (OFLOW_MAX - *lengthptr < code - last_code) + { + *errorcodeptr = ERR20; + goto FAILED; + } + *lengthptr += (int)(code - last_code); /* To include callout length */ + DPRINTF((">> end branch\n")); + } + return TRUE; + + + /* ===================================================================*/ + /* Handle single-character metacharacters. In multiline mode, ^ disables + the setting of any following char as a first character. */ + + case CHAR_CIRCUMFLEX_ACCENT: + previous = NULL; + if ((options & PCRE_MULTILINE) != 0) + { + if (firstcharflags == REQ_UNSET) + zerofirstcharflags = firstcharflags = REQ_NONE; + *code++ = OP_CIRCM; + } + else *code++ = OP_CIRC; + break; + + case CHAR_DOLLAR_SIGN: + previous = NULL; + *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL; + break; + + /* There can never be a first char if '.' is first, whatever happens about + repeats. The value of reqchar doesn't change either. */ + + case CHAR_DOT: + if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE; + zerofirstchar = firstchar; + zerofirstcharflags = firstcharflags; + zeroreqchar = reqchar; + zeroreqcharflags = reqcharflags; + previous = code; + item_hwm_offset = cd->hwm - cd->start_workspace; + *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY; + break; + + + /* ===================================================================*/ + /* Character classes. If the included characters are all < 256, we build a + 32-byte bitmap of the permitted characters, except in the special case + where there is only one such character. For negated classes, we build the + map as usual, then invert it at the end. However, we use a different opcode + so that data characters > 255 can be handled correctly. + + If the class contains characters outside the 0-255 range, a different + opcode is compiled. It may optionally have a bit map for characters < 256, + but those above are are explicitly listed afterwards. A flag byte tells + whether the bitmap is present, and whether this is a negated class or not. + + In JavaScript compatibility mode, an isolated ']' causes an error. In + default (Perl) mode, it is treated as a data character. */ + + case CHAR_RIGHT_SQUARE_BRACKET: + if ((cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0) + { + *errorcodeptr = ERR64; + goto FAILED; + } + goto NORMAL_CHAR; + + /* In another (POSIX) regex library, the ugly syntax [[:<:]] and [[:>:]] is + used for "start of word" and "end of word". As these are otherwise illegal + sequences, we don't break anything by recognizing them. They are replaced + by \b(?=\w) and \b(?<=\w) respectively. Sequences like [a[:<:]] are + erroneous and are handled by the normal code below. */ + + case CHAR_LEFT_SQUARE_BRACKET: + if (STRNCMP_UC_C8(ptr+1, STRING_WEIRD_STARTWORD, 6) == 0) + { + nestptr = ptr + 7; + ptr = sub_start_of_word; + goto REDO_LOOP; + } + + if (STRNCMP_UC_C8(ptr+1, STRING_WEIRD_ENDWORD, 6) == 0) + { + nestptr = ptr + 7; + ptr = sub_end_of_word; + goto REDO_LOOP; + } + + /* Handle a real character class. */ + + previous = code; + item_hwm_offset = cd->hwm - cd->start_workspace; + + /* PCRE supports POSIX class stuff inside a class. Perl gives an error if + they are encountered at the top level, so we'll do that too. */ + + if ((ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT || + ptr[1] == CHAR_EQUALS_SIGN) && + check_posix_syntax(ptr, &tempptr)) + { + *errorcodeptr = (ptr[1] == CHAR_COLON)? ERR13 : ERR31; + goto FAILED; + } + + /* If the first character is '^', set the negation flag and skip it. Also, + if the first few characters (either before or after ^) are \Q\E or \E we + skip them too. This makes for compatibility with Perl. */ + + negate_class = FALSE; + for (;;) + { + c = *(++ptr); + if (c == CHAR_BACKSLASH) + { + if (ptr[1] == CHAR_E) + ptr++; + else if (STRNCMP_UC_C8(ptr + 1, STR_Q STR_BACKSLASH STR_E, 3) == 0) + ptr += 3; + else + break; + } + else if (!negate_class && c == CHAR_CIRCUMFLEX_ACCENT) + negate_class = TRUE; + else break; + } + + /* Empty classes are allowed in JavaScript compatibility mode. Otherwise, + an initial ']' is taken as a data character -- the code below handles + that. In JS mode, [] must always fail, so generate OP_FAIL, whereas + [^] must match any character, so generate OP_ALLANY. */ + + if (c == CHAR_RIGHT_SQUARE_BRACKET && + (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0) + { + *code++ = negate_class? OP_ALLANY : OP_FAIL; + if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE; + zerofirstchar = firstchar; + zerofirstcharflags = firstcharflags; + break; + } + + /* If a class contains a negative special such as \S, we need to flip the + negation flag at the end, so that support for characters > 255 works + correctly (they are all included in the class). */ + + should_flip_negation = FALSE; + + /* Extended class (xclass) will be used when characters > 255 + might match. */ + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + xclass = FALSE; + class_uchardata = code + LINK_SIZE + 2; /* For XCLASS items */ + class_uchardata_base = class_uchardata; /* Save the start */ +#endif + + /* For optimization purposes, we track some properties of the class: + class_has_8bitchar will be non-zero if the class contains at least one < + 256 character; class_one_char will be 1 if the class contains just one + character; xclass_has_prop will be TRUE if unicode property checks + are present in the class. */ + + class_has_8bitchar = 0; + class_one_char = 0; +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + xclass_has_prop = FALSE; +#endif + + /* Initialize the 32-char bit map to all zeros. We build the map in a + temporary bit of memory, in case the class contains fewer than two + 8-bit characters because in that case the compiled code doesn't use the bit + map. */ + + memset(classbits, 0, 32 * sizeof(pcre_uint8)); + + /* Process characters until ] is reached. By writing this as a "do" it + means that an initial ] is taken as a data character. At the start of the + loop, c contains the first byte of the character. */ + + if (c != CHAR_NULL) do + { + const pcre_uchar *oldptr; + +#ifdef SUPPORT_UTF + if (utf && HAS_EXTRALEN(c)) + { /* Braces are required because the */ + GETCHARLEN(c, ptr, ptr); /* macro generates multiple statements */ + } +#endif + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + /* In the pre-compile phase, accumulate the length of any extra + data and reset the pointer. This is so that very large classes that + contain a zillion > 255 characters no longer overwrite the work space + (which is on the stack). We have to remember that there was XCLASS data, + however. */ + + if (class_uchardata > class_uchardata_base) xclass = TRUE; + + if (lengthptr != NULL && class_uchardata > class_uchardata_base) + { + *lengthptr += (int)(class_uchardata - class_uchardata_base); + class_uchardata = class_uchardata_base; + } +#endif + + /* Inside \Q...\E everything is literal except \E */ + + if (inescq) + { + if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E) /* If we are at \E */ + { + inescq = FALSE; /* Reset literal state */ + ptr++; /* Skip the 'E' */ + continue; /* Carry on with next */ + } + goto CHECK_RANGE; /* Could be range if \E follows */ + } + + /* Handle POSIX class names. Perl allows a negation extension of the + form [:^name:]. A square bracket that doesn't match the syntax is + treated as a literal. We also recognize the POSIX constructions + [.ch.] and [=ch=] ("collating elements") and fault them, as Perl + 5.6 and 5.8 do. */ + + if (c == CHAR_LEFT_SQUARE_BRACKET && + (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT || + ptr[1] == CHAR_EQUALS_SIGN) && check_posix_syntax(ptr, &tempptr)) + { + BOOL local_negate = FALSE; + int posix_class, taboffset, tabopt; + register const pcre_uint8 *cbits = cd->cbits; + pcre_uint8 pbits[32]; + + if (ptr[1] != CHAR_COLON) + { + *errorcodeptr = ERR31; + goto FAILED; + } + + ptr += 2; + if (*ptr == CHAR_CIRCUMFLEX_ACCENT) + { + local_negate = TRUE; + should_flip_negation = TRUE; /* Note negative special */ + ptr++; + } + + posix_class = check_posix_name(ptr, (int)(tempptr - ptr)); + if (posix_class < 0) + { + *errorcodeptr = ERR30; + goto FAILED; + } + + /* If matching is caseless, upper and lower are converted to + alpha. This relies on the fact that the class table starts with + alpha, lower, upper as the first 3 entries. */ + + if ((options & PCRE_CASELESS) != 0 && posix_class <= 2) + posix_class = 0; + + /* When PCRE_UCP is set, some of the POSIX classes are converted to + different escape sequences that use Unicode properties \p or \P. Others + that are not available via \p or \P generate XCL_PROP/XCL_NOTPROP + directly. */ + +#ifdef SUPPORT_UCP + if ((options & PCRE_UCP) != 0) + { + unsigned int ptype = 0; + int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0); + + /* The posix_substitutes table specifies which POSIX classes can be + converted to \p or \P items. */ + + if (posix_substitutes[pc] != NULL) + { + nestptr = tempptr + 1; + ptr = posix_substitutes[pc] - 1; + continue; + } + + /* There are three other classes that generate special property calls + that are recognized only in an XCLASS. */ + + else switch(posix_class) + { + case PC_GRAPH: + ptype = PT_PXGRAPH; + /* Fall through */ + case PC_PRINT: + if (ptype == 0) ptype = PT_PXPRINT; + /* Fall through */ + case PC_PUNCT: + if (ptype == 0) ptype = PT_PXPUNCT; + *class_uchardata++ = local_negate? XCL_NOTPROP : XCL_PROP; + *class_uchardata++ = ptype; + *class_uchardata++ = 0; + xclass_has_prop = TRUE; + ptr = tempptr + 1; + continue; + + /* For the other POSIX classes (ascii, cntrl, xdigit) we are going + to fall through to the non-UCP case and build a bit map for + characters with code points less than 256. If we are in a negated + POSIX class, characters with code points greater than 255 must + either all match or all not match. In the special case where we + have not yet generated any xclass data, and this is the final item + in the overall class, we need do nothing: later on, the opcode + OP_NCLASS will be used to indicate that characters greater than 255 + are acceptable. If we have already seen an xclass item or one may + follow (we have to assume that it might if this is not the end of + the class), explicitly list all wide codepoints, which will then + either not match or match, depending on whether the class is or is + not negated. */ + + default: + if (local_negate && + (xclass || tempptr[2] != CHAR_RIGHT_SQUARE_BRACKET)) + { + *class_uchardata++ = XCL_RANGE; + class_uchardata += PRIV(ord2utf)(0x100, class_uchardata); + class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata); + } + break; + } + } +#endif + /* In the non-UCP case, or when UCP makes no difference, we build the + bit map for the POSIX class in a chunk of local store because we may be + adding and subtracting from it, and we don't want to subtract bits that + may be in the main map already. At the end we or the result into the + bit map that is being built. */ + + posix_class *= 3; + + /* Copy in the first table (always present) */ + + memcpy(pbits, cbits + posix_class_maps[posix_class], + 32 * sizeof(pcre_uint8)); + + /* If there is a second table, add or remove it as required. */ + + taboffset = posix_class_maps[posix_class + 1]; + tabopt = posix_class_maps[posix_class + 2]; + + if (taboffset >= 0) + { + if (tabopt >= 0) + for (c = 0; c < 32; c++) pbits[c] |= cbits[c + taboffset]; + else + for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset]; + } + + /* Now see if we need to remove any special characters. An option + value of 1 removes vertical space and 2 removes underscore. */ + + if (tabopt < 0) tabopt = -tabopt; + if (tabopt == 1) pbits[1] &= ~0x3c; + else if (tabopt == 2) pbits[11] &= 0x7f; + + /* Add the POSIX table or its complement into the main table that is + being built and we are done. */ + + if (local_negate) + for (c = 0; c < 32; c++) classbits[c] |= ~pbits[c]; + else + for (c = 0; c < 32; c++) classbits[c] |= pbits[c]; + + ptr = tempptr + 1; + /* Every class contains at least one < 256 character. */ + class_has_8bitchar = 1; + /* Every class contains at least two characters. */ + class_one_char = 2; + continue; /* End of POSIX syntax handling */ + } + + /* Backslash may introduce a single character, or it may introduce one + of the specials, which just set a flag. The sequence \b is a special + case. Inside a class (and only there) it is treated as backspace. We + assume that other escapes have more than one character in them, so + speculatively set both class_has_8bitchar and class_one_char bigger + than one. Unrecognized escapes fall through and are either treated + as literal characters (by default), or are faulted if + PCRE_EXTRA is set. */ + + if (c == CHAR_BACKSLASH) + { + escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options, + TRUE); + if (*errorcodeptr != 0) goto FAILED; + if (escape == 0) c = ec; + else if (escape == ESC_b) c = CHAR_BS; /* \b is backspace in a class */ + else if (escape == ESC_N) /* \N is not supported in a class */ + { + *errorcodeptr = ERR71; + goto FAILED; + } + else if (escape == ESC_Q) /* Handle start of quoted string */ + { + if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E) + { + ptr += 2; /* avoid empty string */ + } + else inescq = TRUE; + continue; + } + else if (escape == ESC_E) continue; /* Ignore orphan \E */ + + else + { + register const pcre_uint8 *cbits = cd->cbits; + /* Every class contains at least two < 256 characters. */ + class_has_8bitchar++; + /* Every class contains at least two characters. */ + class_one_char += 2; + + switch (escape) + { +#ifdef SUPPORT_UCP + case ESC_du: /* These are the values given for \d etc */ + case ESC_DU: /* when PCRE_UCP is set. We replace the */ + case ESC_wu: /* escape sequence with an appropriate \p */ + case ESC_WU: /* or \P to test Unicode properties instead */ + case ESC_su: /* of the default ASCII testing. */ + case ESC_SU: + nestptr = ptr; + ptr = substitutes[escape - ESC_DU] - 1; /* Just before substitute */ + class_has_8bitchar--; /* Undo! */ + continue; +#endif + case ESC_d: + for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit]; + continue; + + case ESC_D: + should_flip_negation = TRUE; + for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_digit]; + continue; + + case ESC_w: + for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_word]; + continue; + + case ESC_W: + should_flip_negation = TRUE; + for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word]; + continue; + + /* Perl 5.004 onwards omitted VT from \s, but restored it at Perl + 5.18. Before PCRE 8.34, we had to preserve the VT bit if it was + previously set by something earlier in the character class. + Luckily, the value of CHAR_VT is 0x0b in both ASCII and EBCDIC, so + we could just adjust the appropriate bit. From PCRE 8.34 we no + longer treat \s and \S specially. */ + + case ESC_s: + for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space]; + continue; + + case ESC_S: + should_flip_negation = TRUE; + for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space]; + continue; + + /* The rest apply in both UCP and non-UCP cases. */ + + case ESC_h: + (void)add_list_to_class(classbits, &class_uchardata, options, cd, + PRIV(hspace_list), NOTACHAR); + continue; + + case ESC_H: + (void)add_not_list_to_class(classbits, &class_uchardata, options, + cd, PRIV(hspace_list)); + continue; + + case ESC_v: + (void)add_list_to_class(classbits, &class_uchardata, options, cd, + PRIV(vspace_list), NOTACHAR); + continue; + + case ESC_V: + (void)add_not_list_to_class(classbits, &class_uchardata, options, + cd, PRIV(vspace_list)); + continue; + + case ESC_p: + case ESC_P: +#ifdef SUPPORT_UCP + { + BOOL negated; + unsigned int ptype = 0, pdata = 0; + if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr)) + goto FAILED; + *class_uchardata++ = ((escape == ESC_p) != negated)? + XCL_PROP : XCL_NOTPROP; + *class_uchardata++ = ptype; + *class_uchardata++ = pdata; + xclass_has_prop = TRUE; + class_has_8bitchar--; /* Undo! */ + continue; + } +#else + *errorcodeptr = ERR45; + goto FAILED; +#endif + /* Unrecognized escapes are faulted if PCRE is running in its + strict mode. By default, for compatibility with Perl, they are + treated as literals. */ + + default: + if ((options & PCRE_EXTRA) != 0) + { + *errorcodeptr = ERR7; + goto FAILED; + } + class_has_8bitchar--; /* Undo the speculative increase. */ + class_one_char -= 2; /* Undo the speculative increase. */ + c = *ptr; /* Get the final character and fall through */ + break; + } + } + + /* Fall through if the escape just defined a single character (c >= 0). + This may be greater than 256. */ + + escape = 0; + + } /* End of backslash handling */ + + /* A character may be followed by '-' to form a range. However, Perl does + not permit ']' to be the end of the range. A '-' character at the end is + treated as a literal. Perl ignores orphaned \E sequences entirely. The + code for handling \Q and \E is messy. */ + + CHECK_RANGE: + while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E) + { + inescq = FALSE; + ptr += 2; + } + oldptr = ptr; + + /* Remember if \r or \n were explicitly used */ + + if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF; + + /* Check for range */ + + if (!inescq && ptr[1] == CHAR_MINUS) + { + pcre_uint32 d; + ptr += 2; + while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2; + + /* If we hit \Q (not followed by \E) at this point, go into escaped + mode. */ + + while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_Q) + { + ptr += 2; + if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) + { ptr += 2; continue; } + inescq = TRUE; + break; + } + + /* Minus (hyphen) at the end of a class is treated as a literal, so put + back the pointer and jump to handle the character that preceded it. */ + + if (*ptr == CHAR_NULL || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET)) + { + ptr = oldptr; + goto CLASS_SINGLE_CHARACTER; + } + + /* Otherwise, we have a potential range; pick up the next character */ + +#ifdef SUPPORT_UTF + if (utf) + { /* Braces are required because the */ + GETCHARLEN(d, ptr, ptr); /* macro generates multiple statements */ + } + else +#endif + d = *ptr; /* Not UTF-8 mode */ + + /* The second part of a range can be a single-character escape + sequence, but not any of the other escapes. Perl treats a hyphen as a + literal in such circumstances. However, in Perl's warning mode, a + warning is given, so PCRE now faults it as it is almost certainly a + mistake on the user's part. */ + + if (!inescq) + { + if (d == CHAR_BACKSLASH) + { + int descape; + descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE); + if (*errorcodeptr != 0) goto FAILED; + + /* 0 means a character was put into d; \b is backspace; any other + special causes an error. */ + + if (descape != 0) + { + if (descape == ESC_b) d = CHAR_BS; else + { + *errorcodeptr = ERR83; + goto FAILED; + } + } + } + + /* A hyphen followed by a POSIX class is treated in the same way. */ + + else if (d == CHAR_LEFT_SQUARE_BRACKET && + (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT || + ptr[1] == CHAR_EQUALS_SIGN) && + check_posix_syntax(ptr, &tempptr)) + { + *errorcodeptr = ERR83; + goto FAILED; + } + } + + /* Check that the two values are in the correct order. Optimize + one-character ranges. */ + + if (d < c) + { + *errorcodeptr = ERR8; + goto FAILED; + } + if (d == c) goto CLASS_SINGLE_CHARACTER; /* A few lines below */ + + /* We have found a character range, so single character optimizations + cannot be done anymore. Any value greater than 1 indicates that there + is more than one character. */ + + class_one_char = 2; + + /* Remember an explicit \r or \n, and add the range to the class. */ + + if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF; + + class_has_8bitchar += + add_to_class(classbits, &class_uchardata, options, cd, c, d); + + continue; /* Go get the next char in the class */ + } + + /* Handle a single character - we can get here for a normal non-escape + char, or after \ that introduces a single character or for an apparent + range that isn't. Only the value 1 matters for class_one_char, so don't + increase it if it is already 2 or more ... just in case there's a class + with a zillion characters in it. */ + + CLASS_SINGLE_CHARACTER: + if (class_one_char < 2) class_one_char++; + + /* If xclass_has_prop is false and class_one_char is 1, we have the first + single character in the class, and there have been no prior ranges, or + XCLASS items generated by escapes. If this is the final character in the + class, we can optimize by turning the item into a 1-character OP_CHAR[I] + if it's positive, or OP_NOT[I] if it's negative. In the positive case, it + can cause firstchar to be set. Otherwise, there can be no first char if + this item is first, whatever repeat count may follow. In the case of + reqchar, save the previous value for reinstating. */ + + if (!inescq && +#ifdef SUPPORT_UCP + !xclass_has_prop && +#endif + class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) + { + ptr++; + zeroreqchar = reqchar; + zeroreqcharflags = reqcharflags; + + if (negate_class) + { +#ifdef SUPPORT_UCP + int d; +#endif + if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE; + zerofirstchar = firstchar; + zerofirstcharflags = firstcharflags; + + /* For caseless UTF-8 mode when UCP support is available, check + whether this character has more than one other case. If so, generate + a special OP_NOTPROP item instead of OP_NOTI. */ + +#ifdef SUPPORT_UCP + if (utf && (options & PCRE_CASELESS) != 0 && + (d = UCD_CASESET(c)) != 0) + { + *code++ = OP_NOTPROP; + *code++ = PT_CLIST; + *code++ = d; + } + else +#endif + /* Char has only one other case, or UCP not available */ + + { + *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT; +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR) + code += PRIV(ord2utf)(c, code); + else +#endif + *code++ = c; + } + + /* We are finished with this character class */ + + goto END_CLASS; + } + + /* For a single, positive character, get the value into mcbuffer, and + then we can handle this with the normal one-character code. */ + +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR) + mclength = PRIV(ord2utf)(c, mcbuffer); + else +#endif + { + mcbuffer[0] = c; + mclength = 1; + } + goto ONE_CHAR; + } /* End of 1-char optimization */ + + /* There is more than one character in the class, or an XCLASS item + has been generated. Add this character to the class. */ + + class_has_8bitchar += + add_to_class(classbits, &class_uchardata, options, cd, c, c); + } + + /* Loop until ']' reached. This "while" is the end of the "do" far above. + If we are at the end of an internal nested string, revert to the outer + string. */ + + while (((c = *(++ptr)) != CHAR_NULL || + (nestptr != NULL && + (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != CHAR_NULL)) && + (c != CHAR_RIGHT_SQUARE_BRACKET || inescq)); + + /* Check for missing terminating ']' */ + + if (c == CHAR_NULL) + { + *errorcodeptr = ERR6; + goto FAILED; + } + + /* We will need an XCLASS if data has been placed in class_uchardata. In + the second phase this is a sufficient test. However, in the pre-compile + phase, class_uchardata gets emptied to prevent workspace overflow, so it + only if the very last character in the class needs XCLASS will it contain + anything at this point. For this reason, xclass gets set TRUE above when + uchar_classdata is emptied, and that's why this code is the way it is here + instead of just doing a test on class_uchardata below. */ + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + if (class_uchardata > class_uchardata_base) xclass = TRUE; +#endif + + /* If this is the first thing in the branch, there can be no first char + setting, whatever the repeat count. Any reqchar setting must remain + unchanged after any kind of repeat. */ + + if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE; + zerofirstchar = firstchar; + zerofirstcharflags = firstcharflags; + zeroreqchar = reqchar; + zeroreqcharflags = reqcharflags; + + /* If there are characters with values > 255, we have to compile an + extended class, with its own opcode, unless there was a negated special + such as \S in the class, and PCRE_UCP is not set, because in that case all + characters > 255 are in the class, so any that were explicitly given as + well can be ignored. If (when there are explicit characters > 255 that must + be listed) there are no characters < 256, we can omit the bitmap in the + actual compiled code. */ + +#ifdef SUPPORT_UTF + if (xclass && (xclass_has_prop || !should_flip_negation || + (options & PCRE_UCP) != 0)) +#elif !defined COMPILE_PCRE8 + if (xclass && (xclass_has_prop || !should_flip_negation)) +#endif +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + { + /* For non-UCP wide characters, in a non-negative class containing \S or + similar (should_flip_negation is set), all characters greater than 255 + must be in the class. */ + + if ( +#if defined COMPILE_PCRE8 + utf && +#endif + should_flip_negation && !negate_class && (options & PCRE_UCP) == 0) + { + *class_uchardata++ = XCL_RANGE; + if (utf) /* Will always be utf in the 8-bit library */ + { + class_uchardata += PRIV(ord2utf)(0x100, class_uchardata); + class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata); + } + else /* Can only happen for the 16-bit & 32-bit libraries */ + { +#if defined COMPILE_PCRE16 + *class_uchardata++ = 0x100; + *class_uchardata++ = 0xffffu; +#elif defined COMPILE_PCRE32 + *class_uchardata++ = 0x100; + *class_uchardata++ = 0xffffffffu; +#endif + } + } + + *class_uchardata++ = XCL_END; /* Marks the end of extra data */ + *code++ = OP_XCLASS; + code += LINK_SIZE; + *code = negate_class? XCL_NOT:0; + if (xclass_has_prop) *code |= XCL_HASPROP; + + /* If the map is required, move up the extra data to make room for it; + otherwise just move the code pointer to the end of the extra data. */ + + if (class_has_8bitchar > 0) + { + *code++ |= XCL_MAP; + memmove(code + (32 / sizeof(pcre_uchar)), code, + IN_UCHARS(class_uchardata - code)); + if (negate_class && !xclass_has_prop) + for (c = 0; c < 32; c++) classbits[c] = ~classbits[c]; + memcpy(code, classbits, 32); + code = class_uchardata + (32 / sizeof(pcre_uchar)); + } + else code = class_uchardata; + + /* Now fill in the complete length of the item */ + + PUT(previous, 1, (int)(code - previous)); + break; /* End of class handling */ + } + + /* Even though any XCLASS list is now discarded, we must allow for + its memory. */ + + if (lengthptr != NULL) + *lengthptr += (int)(class_uchardata - class_uchardata_base); +#endif + + /* If there are no characters > 255, or they are all to be included or + excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the + whole class was negated and whether there were negative specials such as \S + (non-UCP) in the class. Then copy the 32-byte map into the code vector, + negating it if necessary. */ + + *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS; + if (lengthptr == NULL) /* Save time in the pre-compile phase */ + { + if (negate_class) + for (c = 0; c < 32; c++) classbits[c] = ~classbits[c]; + memcpy(code, classbits, 32); + } + code += 32 / sizeof(pcre_uchar); + + END_CLASS: + break; + + + /* ===================================================================*/ + /* Various kinds of repeat; '{' is not necessarily a quantifier, but this + has been tested above. */ + + case CHAR_LEFT_CURLY_BRACKET: + if (!is_quantifier) goto NORMAL_CHAR; + ptr = read_repeat_counts(ptr+1, &repeat_min, &repeat_max, errorcodeptr); + if (*errorcodeptr != 0) goto FAILED; + goto REPEAT; + + case CHAR_ASTERISK: + repeat_min = 0; + repeat_max = -1; + goto REPEAT; + + case CHAR_PLUS: + repeat_min = 1; + repeat_max = -1; + goto REPEAT; + + case CHAR_QUESTION_MARK: + repeat_min = 0; + repeat_max = 1; + + REPEAT: + if (previous == NULL) + { + *errorcodeptr = ERR9; + goto FAILED; + } + + if (repeat_min == 0) + { + firstchar = zerofirstchar; /* Adjust for zero repeat */ + firstcharflags = zerofirstcharflags; + reqchar = zeroreqchar; /* Ditto */ + reqcharflags = zeroreqcharflags; + } + + /* Remember whether this is a variable length repeat */ + + reqvary = (repeat_min == repeat_max)? 0 : REQ_VARY; + + op_type = 0; /* Default single-char op codes */ + possessive_quantifier = FALSE; /* Default not possessive quantifier */ + + /* Save start of previous item, in case we have to move it up in order to + insert something before it. */ + + tempcode = previous; + + /* Before checking for a possessive quantifier, we must skip over + whitespace and comments in extended mode because Perl allows white space at + this point. */ + + if ((options & PCRE_EXTENDED) != 0) + { + const pcre_uchar *p = ptr + 1; + for (;;) + { + while (MAX_255(*p) && (cd->ctypes[*p] & ctype_space) != 0) p++; + if (*p != CHAR_NUMBER_SIGN) break; + p++; + while (*p != CHAR_NULL) + { + if (IS_NEWLINE(p)) /* For non-fixed-length newline cases, */ + { /* IS_NEWLINE sets cd->nllen. */ + p += cd->nllen; + break; + } + p++; +#ifdef SUPPORT_UTF + if (utf) FORWARDCHAR(p); +#endif + } /* Loop for comment characters */ + } /* Loop for multiple comments */ + ptr = p - 1; /* Character before the next significant one. */ + } + + /* We also need to skip over (?# comments, which are not dependent on + extended mode. */ + + if (ptr[1] == CHAR_LEFT_PARENTHESIS && ptr[2] == CHAR_QUESTION_MARK && + ptr[3] == CHAR_NUMBER_SIGN) + { + ptr += 4; + while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++; + if (*ptr == CHAR_NULL) + { + *errorcodeptr = ERR18; + goto FAILED; + } + } + + /* If the next character is '+', we have a possessive quantifier. This + implies greediness, whatever the setting of the PCRE_UNGREEDY option. + If the next character is '?' this is a minimizing repeat, by default, + but if PCRE_UNGREEDY is set, it works the other way round. We change the + repeat type to the non-default. */ + + if (ptr[1] == CHAR_PLUS) + { + repeat_type = 0; /* Force greedy */ + possessive_quantifier = TRUE; + ptr++; + } + else if (ptr[1] == CHAR_QUESTION_MARK) + { + repeat_type = greedy_non_default; + ptr++; + } + else repeat_type = greedy_default; + + /* If previous was a recursion call, wrap it in atomic brackets so that + previous becomes the atomic group. All recursions were so wrapped in the + past, but it no longer happens for non-repeated recursions. In fact, the + repeated ones could be re-implemented independently so as not to need this, + but for the moment we rely on the code for repeating groups. */ + + if (*previous == OP_RECURSE) + { + memmove(previous + 1 + LINK_SIZE, previous, IN_UCHARS(1 + LINK_SIZE)); + *previous = OP_ONCE; + PUT(previous, 1, 2 + 2*LINK_SIZE); + previous[2 + 2*LINK_SIZE] = OP_KET; + PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE); + code += 2 + 2 * LINK_SIZE; + length_prevgroup = 3 + 3*LINK_SIZE; + + /* When actually compiling, we need to check whether this was a forward + reference, and if so, adjust the offset. */ + + if (lengthptr == NULL && cd->hwm >= cd->start_workspace + LINK_SIZE) + { + int offset = GET(cd->hwm, -LINK_SIZE); + if (offset == previous + 1 - cd->start_code) + PUT(cd->hwm, -LINK_SIZE, offset + 1 + LINK_SIZE); + } + } + + /* Now handle repetition for the different types of item. */ + + /* If previous was a character or negated character match, abolish the item + and generate a repeat item instead. If a char item has a minimum of more + than one, ensure that it is set in reqchar - it might not be if a sequence + such as x{3} is the first thing in a branch because the x will have gone + into firstchar instead. */ + + if (*previous == OP_CHAR || *previous == OP_CHARI + || *previous == OP_NOT || *previous == OP_NOTI) + { + switch (*previous) + { + default: /* Make compiler happy. */ + case OP_CHAR: op_type = OP_STAR - OP_STAR; break; + case OP_CHARI: op_type = OP_STARI - OP_STAR; break; + case OP_NOT: op_type = OP_NOTSTAR - OP_STAR; break; + case OP_NOTI: op_type = OP_NOTSTARI - OP_STAR; break; + } + + /* Deal with UTF characters that take up more than one character. It's + easier to write this out separately than try to macrify it. Use c to + hold the length of the character in bytes, plus UTF_LENGTH to flag that + it's a length rather than a small character. */ + +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf && NOT_FIRSTCHAR(code[-1])) + { + pcre_uchar *lastchar = code - 1; + BACKCHAR(lastchar); + c = (int)(code - lastchar); /* Length of UTF-8 character */ + memcpy(utf_chars, lastchar, IN_UCHARS(c)); /* Save the char */ + c |= UTF_LENGTH; /* Flag c as a length */ + } + else +#endif /* SUPPORT_UTF */ + + /* Handle the case of a single charater - either with no UTF support, or + with UTF disabled, or for a single character UTF character. */ + { + c = code[-1]; + if (*previous <= OP_CHARI && repeat_min > 1) + { + reqchar = c; + reqcharflags = req_caseopt | cd->req_varyopt; + } + } + + goto OUTPUT_SINGLE_REPEAT; /* Code shared with single character types */ + } + + /* If previous was a character type match (\d or similar), abolish it and + create a suitable repeat item. The code is shared with single-character + repeats by setting op_type to add a suitable offset into repeat_type. Note + the the Unicode property types will be present only when SUPPORT_UCP is + defined, but we don't wrap the little bits of code here because it just + makes it horribly messy. */ + + else if (*previous < OP_EODN) + { + pcre_uchar *oldcode; + int prop_type, prop_value; + op_type = OP_TYPESTAR - OP_STAR; /* Use type opcodes */ + c = *previous; + + OUTPUT_SINGLE_REPEAT: + if (*previous == OP_PROP || *previous == OP_NOTPROP) + { + prop_type = previous[1]; + prop_value = previous[2]; + } + else prop_type = prop_value = -1; + + oldcode = code; + code = previous; /* Usually overwrite previous item */ + + /* If the maximum is zero then the minimum must also be zero; Perl allows + this case, so we do too - by simply omitting the item altogether. */ + + if (repeat_max == 0) goto END_REPEAT; + + /* Combine the op_type with the repeat_type */ + + repeat_type += op_type; + + /* A minimum of zero is handled either as the special case * or ?, or as + an UPTO, with the maximum given. */ + + if (repeat_min == 0) + { + if (repeat_max == -1) *code++ = OP_STAR + repeat_type; + else if (repeat_max == 1) *code++ = OP_QUERY + repeat_type; + else + { + *code++ = OP_UPTO + repeat_type; + PUT2INC(code, 0, repeat_max); + } + } + + /* A repeat minimum of 1 is optimized into some special cases. If the + maximum is unlimited, we use OP_PLUS. Otherwise, the original item is + left in place and, if the maximum is greater than 1, we use OP_UPTO with + one less than the maximum. */ + + else if (repeat_min == 1) + { + if (repeat_max == -1) + *code++ = OP_PLUS + repeat_type; + else + { + code = oldcode; /* leave previous item in place */ + if (repeat_max == 1) goto END_REPEAT; + *code++ = OP_UPTO + repeat_type; + PUT2INC(code, 0, repeat_max - 1); + } + } + + /* The case {n,n} is just an EXACT, while the general case {n,m} is + handled as an EXACT followed by an UPTO. */ + + else + { + *code++ = OP_EXACT + op_type; /* NB EXACT doesn't have repeat_type */ + PUT2INC(code, 0, repeat_min); + + /* If the maximum is unlimited, insert an OP_STAR. Before doing so, + we have to insert the character for the previous code. For a repeated + Unicode property match, there are two extra bytes that define the + required property. In UTF-8 mode, long characters have their length in + c, with the UTF_LENGTH bit as a flag. */ + + if (repeat_max < 0) + { +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf && (c & UTF_LENGTH) != 0) + { + memcpy(code, utf_chars, IN_UCHARS(c & 7)); + code += c & 7; + } + else +#endif + { + *code++ = c; + if (prop_type >= 0) + { + *code++ = prop_type; + *code++ = prop_value; + } + } + *code++ = OP_STAR + repeat_type; + } + + /* Else insert an UPTO if the max is greater than the min, again + preceded by the character, for the previously inserted code. If the + UPTO is just for 1 instance, we can use QUERY instead. */ + + else if (repeat_max != repeat_min) + { +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf && (c & UTF_LENGTH) != 0) + { + memcpy(code, utf_chars, IN_UCHARS(c & 7)); + code += c & 7; + } + else +#endif + *code++ = c; + if (prop_type >= 0) + { + *code++ = prop_type; + *code++ = prop_value; + } + repeat_max -= repeat_min; + + if (repeat_max == 1) + { + *code++ = OP_QUERY + repeat_type; + } + else + { + *code++ = OP_UPTO + repeat_type; + PUT2INC(code, 0, repeat_max); + } + } + } + + /* The character or character type itself comes last in all cases. */ + +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf && (c & UTF_LENGTH) != 0) + { + memcpy(code, utf_chars, IN_UCHARS(c & 7)); + code += c & 7; + } + else +#endif + *code++ = c; + + /* For a repeated Unicode property match, there are two extra bytes that + define the required property. */ + +#ifdef SUPPORT_UCP + if (prop_type >= 0) + { + *code++ = prop_type; + *code++ = prop_value; + } +#endif + } + + /* If previous was a character class or a back reference, we put the repeat + stuff after it, but just skip the item if the repeat was {0,0}. */ + + else if (*previous == OP_CLASS || *previous == OP_NCLASS || +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + *previous == OP_XCLASS || +#endif + *previous == OP_REF || *previous == OP_REFI || + *previous == OP_DNREF || *previous == OP_DNREFI) + { + if (repeat_max == 0) + { + code = previous; + goto END_REPEAT; + } + + if (repeat_min == 0 && repeat_max == -1) + *code++ = OP_CRSTAR + repeat_type; + else if (repeat_min == 1 && repeat_max == -1) + *code++ = OP_CRPLUS + repeat_type; + else if (repeat_min == 0 && repeat_max == 1) + *code++ = OP_CRQUERY + repeat_type; + else + { + *code++ = OP_CRRANGE + repeat_type; + PUT2INC(code, 0, repeat_min); + if (repeat_max == -1) repeat_max = 0; /* 2-byte encoding for max */ + PUT2INC(code, 0, repeat_max); + } + } + + /* If previous was a bracket group, we may have to replicate it in certain + cases. Note that at this point we can encounter only the "basic" bracket + opcodes such as BRA and CBRA, as this is the place where they get converted + into the more special varieties such as BRAPOS and SBRA. A test for >= + OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK, + ASSERTBACK_NOT, ONCE, ONCE_NC, BRA, BRAPOS, CBRA, CBRAPOS, and COND. + Originally, PCRE did not allow repetition of assertions, but now it does, + for Perl compatibility. */ + + else if (*previous >= OP_ASSERT && *previous <= OP_COND) + { + register int i; + int len = (int)(code - previous); + size_t base_hwm_offset = item_hwm_offset; + pcre_uchar *bralink = NULL; + pcre_uchar *brazeroptr = NULL; + + /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so + we just ignore the repeat. */ + + if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF) + goto END_REPEAT; + + /* There is no sense in actually repeating assertions. The only potential + use of repetition is in cases when the assertion is optional. Therefore, + if the minimum is greater than zero, just ignore the repeat. If the + maximum is not zero or one, set it to 1. */ + + if (*previous < OP_ONCE) /* Assertion */ + { + if (repeat_min > 0) goto END_REPEAT; + if (repeat_max < 0 || repeat_max > 1) repeat_max = 1; + } + + /* The case of a zero minimum is special because of the need to stick + OP_BRAZERO in front of it, and because the group appears once in the + data, whereas in other cases it appears the minimum number of times. For + this reason, it is simplest to treat this case separately, as otherwise + the code gets far too messy. There are several special subcases when the + minimum is zero. */ + + if (repeat_min == 0) + { + /* If the maximum is also zero, we used to just omit the group from the + output altogether, like this: + + ** if (repeat_max == 0) + ** { + ** code = previous; + ** goto END_REPEAT; + ** } + + However, that fails when a group or a subgroup within it is referenced + as a subroutine from elsewhere in the pattern, so now we stick in + OP_SKIPZERO in front of it so that it is skipped on execution. As we + don't have a list of which groups are referenced, we cannot do this + selectively. + + If the maximum is 1 or unlimited, we just have to stick in the BRAZERO + and do no more at this point. However, we do need to adjust any + OP_RECURSE calls inside the group that refer to the group itself or any + internal or forward referenced group, because the offset is from the + start of the whole regex. Temporarily terminate the pattern while doing + this. */ + + if (repeat_max <= 1) /* Covers 0, 1, and unlimited */ + { + *code = OP_END; + adjust_recurse(previous, 1, utf, cd, item_hwm_offset); + memmove(previous + 1, previous, IN_UCHARS(len)); + code++; + if (repeat_max == 0) + { + *previous++ = OP_SKIPZERO; + goto END_REPEAT; + } + brazeroptr = previous; /* Save for possessive optimizing */ + *previous++ = OP_BRAZERO + repeat_type; + } + + /* If the maximum is greater than 1 and limited, we have to replicate + in a nested fashion, sticking OP_BRAZERO before each set of brackets. + The first one has to be handled carefully because it's the original + copy, which has to be moved up. The remainder can be handled by code + that is common with the non-zero minimum case below. We have to + adjust the value or repeat_max, since one less copy is required. Once + again, we may have to adjust any OP_RECURSE calls inside the group. */ + + else + { + int offset; + *code = OP_END; + adjust_recurse(previous, 2 + LINK_SIZE, utf, cd, item_hwm_offset); + memmove(previous + 2 + LINK_SIZE, previous, IN_UCHARS(len)); + code += 2 + LINK_SIZE; + *previous++ = OP_BRAZERO + repeat_type; + *previous++ = OP_BRA; + + /* We chain together the bracket offset fields that have to be + filled in later when the ends of the brackets are reached. */ + + offset = (bralink == NULL)? 0 : (int)(previous - bralink); + bralink = previous; + PUTINC(previous, 0, offset); + } + + repeat_max--; + } + + /* If the minimum is greater than zero, replicate the group as many + times as necessary, and adjust the maximum to the number of subsequent + copies that we need. If we set a first char from the group, and didn't + set a required char, copy the latter from the former. If there are any + forward reference subroutine calls in the group, there will be entries on + the workspace list; replicate these with an appropriate increment. */ + + else + { + if (repeat_min > 1) + { + /* In the pre-compile phase, we don't actually do the replication. We + just adjust the length as if we had. Do some paranoid checks for + potential integer overflow. The INT64_OR_DOUBLE type is a 64-bit + integer type when available, otherwise double. */ + + if (lengthptr != NULL) + { + int delta = (repeat_min - 1)*length_prevgroup; + if ((INT64_OR_DOUBLE)(repeat_min - 1)* + (INT64_OR_DOUBLE)length_prevgroup > + (INT64_OR_DOUBLE)INT_MAX || + OFLOW_MAX - *lengthptr < delta) + { + *errorcodeptr = ERR20; + goto FAILED; + } + *lengthptr += delta; + } + + /* This is compiling for real. If there is a set first byte for + the group, and we have not yet set a "required byte", set it. Make + sure there is enough workspace for copying forward references before + doing the copy. */ + + else + { + if (groupsetfirstchar && reqcharflags < 0) + { + reqchar = firstchar; + reqcharflags = firstcharflags; + } + + for (i = 1; i < repeat_min; i++) + { + pcre_uchar *hc; + size_t this_hwm_offset = cd->hwm - cd->start_workspace; + memcpy(code, previous, IN_UCHARS(len)); + + while (cd->hwm > cd->start_workspace + cd->workspace_size - + WORK_SIZE_SAFETY_MARGIN - + (this_hwm_offset - base_hwm_offset)) + { + *errorcodeptr = expand_workspace(cd); + if (*errorcodeptr != 0) goto FAILED; + } + + for (hc = (pcre_uchar *)cd->start_workspace + base_hwm_offset; + hc < (pcre_uchar *)cd->start_workspace + this_hwm_offset; + hc += LINK_SIZE) + { + PUT(cd->hwm, 0, GET(hc, 0) + len); + cd->hwm += LINK_SIZE; + } + base_hwm_offset = this_hwm_offset; + code += len; + } + } + } + + if (repeat_max > 0) repeat_max -= repeat_min; + } + + /* This code is common to both the zero and non-zero minimum cases. If + the maximum is limited, it replicates the group in a nested fashion, + remembering the bracket starts on a stack. In the case of a zero minimum, + the first one was set up above. In all cases the repeat_max now specifies + the number of additional copies needed. Again, we must remember to + replicate entries on the forward reference list. */ + + if (repeat_max >= 0) + { + /* In the pre-compile phase, we don't actually do the replication. We + just adjust the length as if we had. For each repetition we must add 1 + to the length for BRAZERO and for all but the last repetition we must + add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some + paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type is + a 64-bit integer type when available, otherwise double. */ + + if (lengthptr != NULL && repeat_max > 0) + { + int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) - + 2 - 2*LINK_SIZE; /* Last one doesn't nest */ + if ((INT64_OR_DOUBLE)repeat_max * + (INT64_OR_DOUBLE)(length_prevgroup + 1 + 2 + 2*LINK_SIZE) + > (INT64_OR_DOUBLE)INT_MAX || + OFLOW_MAX - *lengthptr < delta) + { + *errorcodeptr = ERR20; + goto FAILED; + } + *lengthptr += delta; + } + + /* This is compiling for real */ + + else for (i = repeat_max - 1; i >= 0; i--) + { + pcre_uchar *hc; + size_t this_hwm_offset = cd->hwm - cd->start_workspace; + + *code++ = OP_BRAZERO + repeat_type; + + /* All but the final copy start a new nesting, maintaining the + chain of brackets outstanding. */ + + if (i != 0) + { + int offset; + *code++ = OP_BRA; + offset = (bralink == NULL)? 0 : (int)(code - bralink); + bralink = code; + PUTINC(code, 0, offset); + } + + memcpy(code, previous, IN_UCHARS(len)); + + /* Ensure there is enough workspace for forward references before + copying them. */ + + while (cd->hwm > cd->start_workspace + cd->workspace_size - + WORK_SIZE_SAFETY_MARGIN - + (this_hwm_offset - base_hwm_offset)) + { + *errorcodeptr = expand_workspace(cd); + if (*errorcodeptr != 0) goto FAILED; + } + + for (hc = (pcre_uchar *)cd->start_workspace + base_hwm_offset; + hc < (pcre_uchar *)cd->start_workspace + this_hwm_offset; + hc += LINK_SIZE) + { + PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1)); + cd->hwm += LINK_SIZE; + } + base_hwm_offset = this_hwm_offset; + code += len; + } + + /* Now chain through the pending brackets, and fill in their length + fields (which are holding the chain links pro tem). */ + + while (bralink != NULL) + { + int oldlinkoffset; + int offset = (int)(code - bralink + 1); + pcre_uchar *bra = code - offset; + oldlinkoffset = GET(bra, 1); + bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset; + *code++ = OP_KET; + PUTINC(code, 0, offset); + PUT(bra, 1, offset); + } + } + + /* If the maximum is unlimited, set a repeater in the final copy. For + ONCE brackets, that's all we need to do. However, possessively repeated + ONCE brackets can be converted into non-capturing brackets, as the + behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to + deal with possessive ONCEs specially. + + Otherwise, when we are doing the actual compile phase, check to see + whether this group is one that could match an empty string. If so, + convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so + that runtime checking can be done. [This check is also applied to ONCE + groups at runtime, but in a different way.] + + Then, if the quantifier was possessive and the bracket is not a + conditional, we convert the BRA code to the POS form, and the KET code to + KETRPOS. (It turns out to be convenient at runtime to detect this kind of + subpattern at both the start and at the end.) The use of special opcodes + makes it possible to reduce greatly the stack usage in pcre_exec(). If + the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO. + + Then, if the minimum number of matches is 1 or 0, cancel the possessive + flag so that the default action below, of wrapping everything inside + atomic brackets, does not happen. When the minimum is greater than 1, + there will be earlier copies of the group, and so we still have to wrap + the whole thing. */ + + else + { + pcre_uchar *ketcode = code - 1 - LINK_SIZE; + pcre_uchar *bracode = ketcode - GET(ketcode, 1); + + /* Convert possessive ONCE brackets to non-capturing */ + + if ((*bracode == OP_ONCE || *bracode == OP_ONCE_NC) && + possessive_quantifier) *bracode = OP_BRA; + + /* For non-possessive ONCE brackets, all we need to do is to + set the KET. */ + + if (*bracode == OP_ONCE || *bracode == OP_ONCE_NC) + *ketcode = OP_KETRMAX + repeat_type; + + /* Handle non-ONCE brackets and possessive ONCEs (which have been + converted to non-capturing above). */ + + else + { + /* In the compile phase, check for empty string matching. */ + + if (lengthptr == NULL) + { + pcre_uchar *scode = bracode; + do + { + if (could_be_empty_branch(scode, ketcode, utf, cd, NULL)) + { + *bracode += OP_SBRA - OP_BRA; + break; + } + scode += GET(scode, 1); + } + while (*scode == OP_ALT); + } + + /* A conditional group with only one branch has an implicit empty + alternative branch. */ + + if (*bracode == OP_COND && bracode[GET(bracode,1)] != OP_ALT) + *bracode = OP_SCOND; + + /* Handle possessive quantifiers. */ + + if (possessive_quantifier) + { + /* For COND brackets, we wrap the whole thing in a possessively + repeated non-capturing bracket, because we have not invented POS + versions of the COND opcodes. Because we are moving code along, we + must ensure that any pending recursive references are updated. */ + + if (*bracode == OP_COND || *bracode == OP_SCOND) + { + int nlen = (int)(code - bracode); + *code = OP_END; + adjust_recurse(bracode, 1 + LINK_SIZE, utf, cd, item_hwm_offset); + memmove(bracode + 1 + LINK_SIZE, bracode, IN_UCHARS(nlen)); + code += 1 + LINK_SIZE; + nlen += 1 + LINK_SIZE; + *bracode = (*bracode == OP_COND)? OP_BRAPOS : OP_SBRAPOS; + *code++ = OP_KETRPOS; + PUTINC(code, 0, nlen); + PUT(bracode, 1, nlen); + } + + /* For non-COND brackets, we modify the BRA code and use KETRPOS. */ + + else + { + *bracode += 1; /* Switch to xxxPOS opcodes */ + *ketcode = OP_KETRPOS; + } + + /* If the minimum is zero, mark it as possessive, then unset the + possessive flag when the minimum is 0 or 1. */ + + if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO; + if (repeat_min < 2) possessive_quantifier = FALSE; + } + + /* Non-possessive quantifier */ + + else *ketcode = OP_KETRMAX + repeat_type; + } + } + } + + /* If previous is OP_FAIL, it was generated by an empty class [] in + JavaScript mode. The other ways in which OP_FAIL can be generated, that is + by (*FAIL) or (?!) set previous to NULL, which gives a "nothing to repeat" + error above. We can just ignore the repeat in JS case. */ + + else if (*previous == OP_FAIL) goto END_REPEAT; + + /* Else there's some kind of shambles */ + + else + { + *errorcodeptr = ERR11; + goto FAILED; + } + + /* If the character following a repeat is '+', possessive_quantifier is + TRUE. For some opcodes, there are special alternative opcodes for this + case. For anything else, we wrap the entire repeated item inside OP_ONCE + brackets. Logically, the '+' notation is just syntactic sugar, taken from + Sun's Java package, but the special opcodes can optimize it. + + Some (but not all) possessively repeated subpatterns have already been + completely handled in the code just above. For them, possessive_quantifier + is always FALSE at this stage. Note that the repeated item starts at + tempcode, not at previous, which might be the first part of a string whose + (former) last char we repeated. */ + + if (possessive_quantifier) + { + int len; + + /* Possessifying an EXACT quantifier has no effect, so we can ignore it. + However, QUERY, STAR, or UPTO may follow (for quantifiers such as {5,6}, + {5,}, or {5,10}). We skip over an EXACT item; if the length of what + remains is greater than zero, there's a further opcode that can be + handled. If not, do nothing, leaving the EXACT alone. */ + + switch(*tempcode) + { + case OP_TYPEEXACT: + tempcode += PRIV(OP_lengths)[*tempcode] + + ((tempcode[1 + IMM2_SIZE] == OP_PROP + || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0); + break; + + /* CHAR opcodes are used for exacts whose count is 1. */ + + case OP_CHAR: + case OP_CHARI: + case OP_NOT: + case OP_NOTI: + case OP_EXACT: + case OP_EXACTI: + case OP_NOTEXACT: + case OP_NOTEXACTI: + tempcode += PRIV(OP_lengths)[*tempcode]; +#ifdef SUPPORT_UTF + if (utf && HAS_EXTRALEN(tempcode[-1])) + tempcode += GET_EXTRALEN(tempcode[-1]); +#endif + break; + + /* For the class opcodes, the repeat operator appears at the end; + adjust tempcode to point to it. */ + + case OP_CLASS: + case OP_NCLASS: + tempcode += 1 + 32/sizeof(pcre_uchar); + break; + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: + tempcode += GET(tempcode, 1); + break; +#endif + } + + /* If tempcode is equal to code (which points to the end of the repeated + item), it means we have skipped an EXACT item but there is no following + QUERY, STAR, or UPTO; the value of len will be 0, and we do nothing. In + all other cases, tempcode will be pointing to the repeat opcode, and will + be less than code, so the value of len will be greater than 0. */ + + len = (int)(code - tempcode); + if (len > 0) + { + unsigned int repcode = *tempcode; + + /* There is a table for possessifying opcodes, all of which are less + than OP_CALLOUT. A zero entry means there is no possessified version. + */ + + if (repcode < OP_CALLOUT && opcode_possessify[repcode] > 0) + *tempcode = opcode_possessify[repcode]; + + /* For opcode without a special possessified version, wrap the item in + ONCE brackets. Because we are moving code along, we must ensure that any + pending recursive references are updated. */ + + else + { + *code = OP_END; + adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, item_hwm_offset); + memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len)); + code += 1 + LINK_SIZE; + len += 1 + LINK_SIZE; + tempcode[0] = OP_ONCE; + *code++ = OP_KET; + PUTINC(code, 0, len); + PUT(tempcode, 1, len); + } + } + +#ifdef NEVER + if (len > 0) switch (*tempcode) + { + case OP_STAR: *tempcode = OP_POSSTAR; break; + case OP_PLUS: *tempcode = OP_POSPLUS; break; + case OP_QUERY: *tempcode = OP_POSQUERY; break; + case OP_UPTO: *tempcode = OP_POSUPTO; break; + + case OP_STARI: *tempcode = OP_POSSTARI; break; + case OP_PLUSI: *tempcode = OP_POSPLUSI; break; + case OP_QUERYI: *tempcode = OP_POSQUERYI; break; + case OP_UPTOI: *tempcode = OP_POSUPTOI; break; + + case OP_NOTSTAR: *tempcode = OP_NOTPOSSTAR; break; + case OP_NOTPLUS: *tempcode = OP_NOTPOSPLUS; break; + case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break; + case OP_NOTUPTO: *tempcode = OP_NOTPOSUPTO; break; + + case OP_NOTSTARI: *tempcode = OP_NOTPOSSTARI; break; + case OP_NOTPLUSI: *tempcode = OP_NOTPOSPLUSI; break; + case OP_NOTQUERYI: *tempcode = OP_NOTPOSQUERYI; break; + case OP_NOTUPTOI: *tempcode = OP_NOTPOSUPTOI; break; + + case OP_TYPESTAR: *tempcode = OP_TYPEPOSSTAR; break; + case OP_TYPEPLUS: *tempcode = OP_TYPEPOSPLUS; break; + case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break; + case OP_TYPEUPTO: *tempcode = OP_TYPEPOSUPTO; break; + + case OP_CRSTAR: *tempcode = OP_CRPOSSTAR; break; + case OP_CRPLUS: *tempcode = OP_CRPOSPLUS; break; + case OP_CRQUERY: *tempcode = OP_CRPOSQUERY; break; + case OP_CRRANGE: *tempcode = OP_CRPOSRANGE; break; + + /* Because we are moving code along, we must ensure that any + pending recursive references are updated. */ + + default: + *code = OP_END; + adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, item_hwm_offset); + memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len)); + code += 1 + LINK_SIZE; + len += 1 + LINK_SIZE; + tempcode[0] = OP_ONCE; + *code++ = OP_KET; + PUTINC(code, 0, len); + PUT(tempcode, 1, len); + break; + } +#endif + } + + /* In all case we no longer have a previous item. We also set the + "follows varying string" flag for subsequently encountered reqchars if + it isn't already set and we have just passed a varying length item. */ + + END_REPEAT: + previous = NULL; + cd->req_varyopt |= reqvary; + break; + + + /* ===================================================================*/ + /* Start of nested parenthesized sub-expression, or comment or lookahead or + lookbehind or option setting or condition or all the other extended + parenthesis forms. */ + + case CHAR_LEFT_PARENTHESIS: + ptr++; + + /* Now deal with various "verbs" that can be introduced by '*'. */ + + if (ptr[0] == CHAR_ASTERISK && (ptr[1] == ':' + || (MAX_255(ptr[1]) && ((cd->ctypes[ptr[1]] & ctype_letter) != 0)))) + { + int i, namelen; + int arglen = 0; + const char *vn = verbnames; + const pcre_uchar *name = ptr + 1; + const pcre_uchar *arg = NULL; + previous = NULL; + ptr++; + while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_letter) != 0) ptr++; + namelen = (int)(ptr - name); + + /* It appears that Perl allows any characters whatsoever, other than + a closing parenthesis, to appear in arguments, so we no longer insist on + letters, digits, and underscores. */ + + if (*ptr == CHAR_COLON) + { + arg = ++ptr; + while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++; + arglen = (int)(ptr - arg); + if ((unsigned int)arglen > MAX_MARK) + { + *errorcodeptr = ERR75; + goto FAILED; + } + } + + if (*ptr != CHAR_RIGHT_PARENTHESIS) + { + *errorcodeptr = ERR60; + goto FAILED; + } + + /* Scan the table of verb names */ + + for (i = 0; i < verbcount; i++) + { + if (namelen == verbs[i].len && + STRNCMP_UC_C8(name, vn, namelen) == 0) + { + int setverb; + + /* Check for open captures before ACCEPT and convert it to + ASSERT_ACCEPT if in an assertion. */ + + if (verbs[i].op == OP_ACCEPT) + { + open_capitem *oc; + if (arglen != 0) + { + *errorcodeptr = ERR59; + goto FAILED; + } + cd->had_accept = TRUE; + for (oc = cd->open_caps; oc != NULL; oc = oc->next) + { + if (lengthptr != NULL) + { +#ifdef COMPILE_PCRE8 + *lengthptr += 1 + IMM2_SIZE; +#elif defined COMPILE_PCRE16 + *lengthptr += 2 + IMM2_SIZE; +#elif defined COMPILE_PCRE32 + *lengthptr += 4 + IMM2_SIZE; +#endif + } + else + { + *code++ = OP_CLOSE; + PUT2INC(code, 0, oc->number); + } + } + setverb = *code++ = + (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT; + + /* Do not set firstchar after *ACCEPT */ + if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE; + } + + /* Handle other cases with/without an argument */ + + else if (arglen == 0) + { + if (verbs[i].op < 0) /* Argument is mandatory */ + { + *errorcodeptr = ERR66; + goto FAILED; + } + setverb = *code++ = verbs[i].op; + } + + else + { + if (verbs[i].op_arg < 0) /* Argument is forbidden */ + { + *errorcodeptr = ERR59; + goto FAILED; + } + setverb = *code++ = verbs[i].op_arg; + if (lengthptr != NULL) /* In pass 1 just add in the length */ + { /* to avoid potential workspace */ + *lengthptr += arglen; /* overflow. */ + *code++ = 0; + } + else + { + *code++ = arglen; + memcpy(code, arg, IN_UCHARS(arglen)); + code += arglen; + } + *code++ = 0; + } + + switch (setverb) + { + case OP_THEN: + case OP_THEN_ARG: + cd->external_flags |= PCRE_HASTHEN; + break; + + case OP_PRUNE: + case OP_PRUNE_ARG: + case OP_SKIP: + case OP_SKIP_ARG: + cd->had_pruneorskip = TRUE; + break; + } + + break; /* Found verb, exit loop */ + } + + vn += verbs[i].len + 1; + } + + if (i < verbcount) continue; /* Successfully handled a verb */ + *errorcodeptr = ERR60; /* Verb not recognized */ + goto FAILED; + } + + /* Initialize for "real" parentheses */ + + newoptions = options; + skipbytes = 0; + bravalue = OP_CBRA; + item_hwm_offset = cd->hwm - cd->start_workspace; + reset_bracount = FALSE; + + /* Deal with the extended parentheses; all are introduced by '?', and the + appearance of any of them means that this is not a capturing group. */ + + if (*ptr == CHAR_QUESTION_MARK) + { + int i, set, unset, namelen; + int *optset; + const pcre_uchar *name; + pcre_uchar *slot; + + switch (*(++ptr)) + { + /* ------------------------------------------------------------ */ + case CHAR_VERTICAL_LINE: /* Reset capture count for each branch */ + reset_bracount = TRUE; + cd->dupgroups = TRUE; /* Record (?| encountered */ + /* Fall through */ + + /* ------------------------------------------------------------ */ + case CHAR_COLON: /* Non-capturing bracket */ + bravalue = OP_BRA; + ptr++; + break; + + + /* ------------------------------------------------------------ */ + case CHAR_LEFT_PARENTHESIS: + bravalue = OP_COND; /* Conditional group */ + tempptr = ptr; + + /* A condition can be an assertion, a number (referring to a numbered + group's having been set), a name (referring to a named group), or 'R', + referring to recursion. R and R&name are also permitted for + recursion tests. + + There are ways of testing a named group: (?(name)) is used by Python; + Perl 5.10 onwards uses (?() or (?('name')). + + There is one unfortunate ambiguity, caused by history. 'R' can be the + recursive thing or the name 'R' (and similarly for 'R' followed by + digits). We look for a name first; if not found, we try the other case. + + For compatibility with auto-callouts, we allow a callout to be + specified before a condition that is an assertion. First, check for the + syntax of a callout; if found, adjust the temporary pointer that is + used to check for an assertion condition. That's all that is needed! */ + + if (ptr[1] == CHAR_QUESTION_MARK && ptr[2] == CHAR_C) + { + for (i = 3;; i++) if (!IS_DIGIT(ptr[i])) break; + if (ptr[i] == CHAR_RIGHT_PARENTHESIS) + tempptr += i + 1; + + /* tempptr should now be pointing to the opening parenthesis of the + assertion condition. */ + + if (*tempptr != CHAR_LEFT_PARENTHESIS) + { + *errorcodeptr = ERR28; + goto FAILED; + } + } + + /* For conditions that are assertions, check the syntax, and then exit + the switch. This will take control down to where bracketed groups, + including assertions, are processed. */ + + if (tempptr[1] == CHAR_QUESTION_MARK && + (tempptr[2] == CHAR_EQUALS_SIGN || + tempptr[2] == CHAR_EXCLAMATION_MARK || + (tempptr[2] == CHAR_LESS_THAN_SIGN && + (tempptr[3] == CHAR_EQUALS_SIGN || + tempptr[3] == CHAR_EXCLAMATION_MARK)))) + { + cd->iscondassert = TRUE; + break; + } + + /* Other conditions use OP_CREF/OP_DNCREF/OP_RREF/OP_DNRREF, and all + need to skip at least 1+IMM2_SIZE bytes at the start of the group. */ + + code[1+LINK_SIZE] = OP_CREF; + skipbytes = 1+IMM2_SIZE; + refsign = -1; /* => not a number */ + namelen = -1; /* => not a name; must set to avoid warning */ + name = NULL; /* Always set to avoid warning */ + recno = 0; /* Always set to avoid warning */ + + /* Check for a test for recursion in a named group. */ + + ptr++; + if (*ptr == CHAR_R && ptr[1] == CHAR_AMPERSAND) + { + terminator = -1; + ptr += 2; + code[1+LINK_SIZE] = OP_RREF; /* Change the type of test */ + } + + /* Check for a test for a named group's having been set, using the Perl + syntax (?() or (?('name'), and also allow for the original PCRE + syntax of (?(name) or for (?(+n), (?(-n), and just (?(n). */ + + else if (*ptr == CHAR_LESS_THAN_SIGN) + { + terminator = CHAR_GREATER_THAN_SIGN; + ptr++; + } + else if (*ptr == CHAR_APOSTROPHE) + { + terminator = CHAR_APOSTROPHE; + ptr++; + } + else + { + terminator = CHAR_NULL; + if (*ptr == CHAR_MINUS || *ptr == CHAR_PLUS) refsign = *ptr++; + else if (IS_DIGIT(*ptr)) refsign = 0; + } + + /* Handle a number */ + + if (refsign >= 0) + { + while (IS_DIGIT(*ptr)) + { + if (recno > INT_MAX / 10 - 1) /* Integer overflow */ + { + while (IS_DIGIT(*ptr)) ptr++; + *errorcodeptr = ERR61; + goto FAILED; + } + recno = recno * 10 + (int)(*ptr - CHAR_0); + ptr++; + } + } + + /* Otherwise we expect to read a name; anything else is an error. When + a name is one of a number of duplicates, a different opcode is used and + it needs more memory. Unfortunately we cannot tell whether a name is a + duplicate in the first pass, so we have to allow for more memory. */ + + else + { + if (IS_DIGIT(*ptr)) + { + *errorcodeptr = ERR84; + goto FAILED; + } + if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_word) == 0) + { + *errorcodeptr = ERR28; /* Assertion expected */ + goto FAILED; + } + name = ptr++; + while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) + { + ptr++; + } + namelen = (int)(ptr - name); + if (lengthptr != NULL) skipbytes += IMM2_SIZE; + } + + /* Check the terminator */ + + if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) || + *ptr++ != CHAR_RIGHT_PARENTHESIS) + { + ptr--; /* Error offset */ + *errorcodeptr = ERR26; /* Malformed number or name */ + goto FAILED; + } + + /* Do no further checking in the pre-compile phase. */ + + if (lengthptr != NULL) break; + + /* In the real compile we do the work of looking for the actual + reference. If refsign is not negative, it means we have a number in + recno. */ + + if (refsign >= 0) + { + if (recno <= 0) + { + *errorcodeptr = ERR35; + goto FAILED; + } + if (refsign != 0) recno = (refsign == CHAR_MINUS)? + cd->bracount - recno + 1 : recno + cd->bracount; + if (recno <= 0 || recno > cd->final_bracount) + { + *errorcodeptr = ERR15; + goto FAILED; + } + PUT2(code, 2+LINK_SIZE, recno); + if (recno > cd->top_backref) cd->top_backref = recno; + break; + } + + /* Otherwise look for the name. */ + + slot = cd->name_table; + for (i = 0; i < cd->names_found; i++) + { + if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0 && + slot[IMM2_SIZE+namelen] == 0) break; + slot += cd->name_entry_size; + } + + /* Found the named subpattern. If the name is duplicated, add one to + the opcode to change CREF/RREF into DNCREF/DNRREF and insert + appropriate data values. Otherwise, just insert the unique subpattern + number. */ + + if (i < cd->names_found) + { + int offset = i++; + int count = 1; + recno = GET2(slot, 0); /* Number from first found */ + if (recno > cd->top_backref) cd->top_backref = recno; + for (; i < cd->names_found; i++) + { + slot += cd->name_entry_size; + if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) != 0 || + (slot+IMM2_SIZE)[namelen] != 0) break; + count++; + } + + if (count > 1) + { + PUT2(code, 2+LINK_SIZE, offset); + PUT2(code, 2+LINK_SIZE+IMM2_SIZE, count); + skipbytes += IMM2_SIZE; + code[1+LINK_SIZE]++; + } + else /* Not a duplicated name */ + { + PUT2(code, 2+LINK_SIZE, recno); + } + } + + /* If terminator == CHAR_NULL it means that the name followed directly + after the opening parenthesis [e.g. (?(abc)...] and in this case there + are some further alternatives to try. For the cases where terminator != + CHAR_NULL [things like (?(... or (?('name')... or (?(R&name)... ] + we have now checked all the possibilities, so give an error. */ + + else if (terminator != CHAR_NULL) + { + *errorcodeptr = ERR15; + goto FAILED; + } + + /* Check for (?(R) for recursion. Allow digits after R to specify a + specific group number. */ + + else if (*name == CHAR_R) + { + recno = 0; + for (i = 1; i < namelen; i++) + { + if (!IS_DIGIT(name[i])) + { + *errorcodeptr = ERR15; + goto FAILED; + } + if (recno > INT_MAX / 10 - 1) /* Integer overflow */ + { + *errorcodeptr = ERR61; + goto FAILED; + } + recno = recno * 10 + name[i] - CHAR_0; + } + if (recno == 0) recno = RREF_ANY; + code[1+LINK_SIZE] = OP_RREF; /* Change test type */ + PUT2(code, 2+LINK_SIZE, recno); + } + + /* Similarly, check for the (?(DEFINE) "condition", which is always + false. */ + + else if (namelen == 6 && STRNCMP_UC_C8(name, STRING_DEFINE, 6) == 0) + { + code[1+LINK_SIZE] = OP_DEF; + skipbytes = 1; + } + + /* Reference to an unidentified subpattern. */ + + else + { + *errorcodeptr = ERR15; + goto FAILED; + } + break; + + + /* ------------------------------------------------------------ */ + case CHAR_EQUALS_SIGN: /* Positive lookahead */ + bravalue = OP_ASSERT; + cd->assert_depth += 1; + ptr++; + break; + + /* Optimize (?!) to (*FAIL) unless it is quantified - which is a weird + thing to do, but Perl allows all assertions to be quantified, and when + they contain capturing parentheses there may be a potential use for + this feature. Not that that applies to a quantified (?!) but we allow + it for uniformity. */ + + /* ------------------------------------------------------------ */ + case CHAR_EXCLAMATION_MARK: /* Negative lookahead */ + ptr++; + if (*ptr == CHAR_RIGHT_PARENTHESIS && ptr[1] != CHAR_ASTERISK && + ptr[1] != CHAR_PLUS && ptr[1] != CHAR_QUESTION_MARK && + (ptr[1] != CHAR_LEFT_CURLY_BRACKET || !is_counted_repeat(ptr+2))) + { + *code++ = OP_FAIL; + previous = NULL; + continue; + } + bravalue = OP_ASSERT_NOT; + cd->assert_depth += 1; + break; + + + /* ------------------------------------------------------------ */ + case CHAR_LESS_THAN_SIGN: /* Lookbehind or named define */ + switch (ptr[1]) + { + case CHAR_EQUALS_SIGN: /* Positive lookbehind */ + bravalue = OP_ASSERTBACK; + cd->assert_depth += 1; + ptr += 2; + break; + + case CHAR_EXCLAMATION_MARK: /* Negative lookbehind */ + bravalue = OP_ASSERTBACK_NOT; + cd->assert_depth += 1; + ptr += 2; + break; + + default: /* Could be name define, else bad */ + if (MAX_255(ptr[1]) && (cd->ctypes[ptr[1]] & ctype_word) != 0) + goto DEFINE_NAME; + ptr++; /* Correct offset for error */ + *errorcodeptr = ERR24; + goto FAILED; + } + break; + + + /* ------------------------------------------------------------ */ + case CHAR_GREATER_THAN_SIGN: /* One-time brackets */ + bravalue = OP_ONCE; + ptr++; + break; + + + /* ------------------------------------------------------------ */ + case CHAR_C: /* Callout - may be followed by digits; */ + previous_callout = code; /* Save for later completion */ + after_manual_callout = 1; /* Skip one item before completing */ + *code++ = OP_CALLOUT; + { + int n = 0; + ptr++; + while(IS_DIGIT(*ptr)) + { + n = n * 10 + *ptr++ - CHAR_0; + if (n > 255) + { + *errorcodeptr = ERR38; + goto FAILED; + } + } + if (*ptr != CHAR_RIGHT_PARENTHESIS) + { + *errorcodeptr = ERR39; + goto FAILED; + } + *code++ = n; + PUT(code, 0, (int)(ptr - cd->start_pattern + 1)); /* Pattern offset */ + PUT(code, LINK_SIZE, 0); /* Default length */ + code += 2 * LINK_SIZE; + } + previous = NULL; + continue; + + + /* ------------------------------------------------------------ */ + case CHAR_P: /* Python-style named subpattern handling */ + if (*(++ptr) == CHAR_EQUALS_SIGN || + *ptr == CHAR_GREATER_THAN_SIGN) /* Reference or recursion */ + { + is_recurse = *ptr == CHAR_GREATER_THAN_SIGN; + terminator = CHAR_RIGHT_PARENTHESIS; + goto NAMED_REF_OR_RECURSE; + } + else if (*ptr != CHAR_LESS_THAN_SIGN) /* Test for Python-style defn */ + { + *errorcodeptr = ERR41; + goto FAILED; + } + /* Fall through to handle (?P< as (?< is handled */ + + + /* ------------------------------------------------------------ */ + DEFINE_NAME: /* Come here from (?< handling */ + case CHAR_APOSTROPHE: + terminator = (*ptr == CHAR_LESS_THAN_SIGN)? + CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE; + name = ++ptr; + if (IS_DIGIT(*ptr)) + { + *errorcodeptr = ERR84; /* Group name must start with non-digit */ + goto FAILED; + } + while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++; + namelen = (int)(ptr - name); + + /* In the pre-compile phase, do a syntax check, remember the longest + name, and then remember the group in a vector, expanding it if + necessary. Duplicates for the same number are skipped; other duplicates + are checked for validity. In the actual compile, there is nothing to + do. */ + + if (lengthptr != NULL) + { + named_group *ng; + pcre_uint32 number = cd->bracount + 1; + + if (*ptr != (pcre_uchar)terminator) + { + *errorcodeptr = ERR42; + goto FAILED; + } + + if (cd->names_found >= MAX_NAME_COUNT) + { + *errorcodeptr = ERR49; + goto FAILED; + } + + if (namelen + IMM2_SIZE + 1 > cd->name_entry_size) + { + cd->name_entry_size = namelen + IMM2_SIZE + 1; + if (namelen > MAX_NAME_SIZE) + { + *errorcodeptr = ERR48; + goto FAILED; + } + } + + /* Scan the list to check for duplicates. For duplicate names, if the + number is the same, break the loop, which causes the name to be + discarded; otherwise, if DUPNAMES is not set, give an error. + If it is set, allow the name with a different number, but continue + scanning in case this is a duplicate with the same number. For + non-duplicate names, give an error if the number is duplicated. */ + + ng = cd->named_groups; + for (i = 0; i < cd->names_found; i++, ng++) + { + if (namelen == ng->length && + STRNCMP_UC_UC(name, ng->name, namelen) == 0) + { + if (ng->number == number) break; + if ((options & PCRE_DUPNAMES) == 0) + { + *errorcodeptr = ERR43; + goto FAILED; + } + cd->dupnames = TRUE; /* Duplicate names exist */ + } + else if (ng->number == number) + { + *errorcodeptr = ERR65; + goto FAILED; + } + } + + if (i >= cd->names_found) /* Not a duplicate with same number */ + { + /* Increase the list size if necessary */ + + if (cd->names_found >= cd->named_group_list_size) + { + int newsize = cd->named_group_list_size * 2; + named_group *newspace = (PUBL(malloc)) + (newsize * sizeof(named_group)); + + if (newspace == NULL) + { + *errorcodeptr = ERR21; + goto FAILED; + } + + memcpy(newspace, cd->named_groups, + cd->named_group_list_size * sizeof(named_group)); + if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE) + (PUBL(free))((void *)cd->named_groups); + cd->named_groups = newspace; + cd->named_group_list_size = newsize; + } + + cd->named_groups[cd->names_found].name = name; + cd->named_groups[cd->names_found].length = namelen; + cd->named_groups[cd->names_found].number = number; + cd->names_found++; + } + } + + ptr++; /* Move past > or ' in both passes. */ + goto NUMBERED_GROUP; + + + /* ------------------------------------------------------------ */ + case CHAR_AMPERSAND: /* Perl recursion/subroutine syntax */ + terminator = CHAR_RIGHT_PARENTHESIS; + is_recurse = TRUE; + /* Fall through */ + + /* We come here from the Python syntax above that handles both + references (?P=name) and recursion (?P>name), as well as falling + through from the Perl recursion syntax (?&name). We also come here from + the Perl \k or \k'name' back reference syntax and the \k{name} + .NET syntax, and the Oniguruma \g<...> and \g'...' subroutine syntax. */ + + NAMED_REF_OR_RECURSE: + name = ++ptr; + if (IS_DIGIT(*ptr)) + { + *errorcodeptr = ERR84; /* Group name must start with non-digit */ + goto FAILED; + } + while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++; + namelen = (int)(ptr - name); + + /* In the pre-compile phase, do a syntax check. We used to just set + a dummy reference number, because it was not used in the first pass. + However, with the change of recursive back references to be atomic, + we have to look for the number so that this state can be identified, as + otherwise the incorrect length is computed. If it's not a backwards + reference, the dummy number will do. */ + + if (lengthptr != NULL) + { + named_group *ng; + recno = 0; + + if (namelen == 0) + { + *errorcodeptr = ERR62; + goto FAILED; + } + if (*ptr != (pcre_uchar)terminator) + { + *errorcodeptr = ERR42; + goto FAILED; + } + if (namelen > MAX_NAME_SIZE) + { + *errorcodeptr = ERR48; + goto FAILED; + } + + /* Count named back references. */ + + if (!is_recurse) cd->namedrefcount++; + + /* We have to allow for a named reference to a duplicated name (this + cannot be determined until the second pass). This needs an extra + 16-bit data item. */ + + *lengthptr += IMM2_SIZE; + + /* If this is a forward reference and we are within a (?|...) group, + the reference may end up as the number of a group which we are + currently inside, that is, it could be a recursive reference. In the + real compile this will be picked up and the reference wrapped with + OP_ONCE to make it atomic, so we must space in case this occurs. */ + + /* In fact, this can happen for a non-forward reference because + another group with the same number might be created later. This + issue is fixed "properly" in PCRE2. As PCRE1 is now in maintenance + only mode, we finesse the bug by allowing more memory always. */ + + *lengthptr += 4 + 4*LINK_SIZE; + + /* It is even worse than that. The current reference may be to an + existing named group with a different number (so apparently not + recursive) but which later on is also attached to a group with the + current number. This can only happen if $(| has been previous + encountered. In that case, we allow yet more memory, just in case. + (Again, this is fixed "properly" in PCRE2. */ + + if (cd->dupgroups) *lengthptr += 4 + 4*LINK_SIZE; + + /* Otherwise, check for recursion here. The name table does not exist + in the first pass; instead we must scan the list of names encountered + so far in order to get the number. If the name is not found, leave + the value of recno as 0 for a forward reference. */ + + /* This patch (removing "else") fixes a problem when a reference is + to multiple identically named nested groups from within the nest. + Once again, it is not the "proper" fix, and it results in an + over-allocation of memory. */ + + /* else */ + { + ng = cd->named_groups; + for (i = 0; i < cd->names_found; i++, ng++) + { + if (namelen == ng->length && + STRNCMP_UC_UC(name, ng->name, namelen) == 0) + { + open_capitem *oc; + recno = ng->number; + if (is_recurse) break; + for (oc = cd->open_caps; oc != NULL; oc = oc->next) + { + if (oc->number == recno) + { + oc->flag = TRUE; + break; + } + } + } + } + } + } + + /* In the real compile, search the name table. We check the name + first, and then check that we have reached the end of the name in the + table. That way, if the name is longer than any in the table, the + comparison will fail without reading beyond the table entry. */ + + else + { + slot = cd->name_table; + for (i = 0; i < cd->names_found; i++) + { + if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0 && + slot[IMM2_SIZE+namelen] == 0) + break; + slot += cd->name_entry_size; + } + + if (i < cd->names_found) + { + recno = GET2(slot, 0); + } + else + { + *errorcodeptr = ERR15; + goto FAILED; + } + } + + /* In both phases, for recursions, we can now go to the code than + handles numerical recursion. */ + + if (is_recurse) goto HANDLE_RECURSION; + + /* In the second pass we must see if the name is duplicated. If so, we + generate a different opcode. */ + + if (lengthptr == NULL && cd->dupnames) + { + int count = 1; + unsigned int index = i; + pcre_uchar *cslot = slot + cd->name_entry_size; + + for (i++; i < cd->names_found; i++) + { + if (STRCMP_UC_UC(slot + IMM2_SIZE, cslot + IMM2_SIZE) != 0) break; + count++; + cslot += cd->name_entry_size; + } + + if (count > 1) + { + if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE; + previous = code; + item_hwm_offset = cd->hwm - cd->start_workspace; + *code++ = ((options & PCRE_CASELESS) != 0)? OP_DNREFI : OP_DNREF; + PUT2INC(code, 0, index); + PUT2INC(code, 0, count); + + /* Process each potentially referenced group. */ + + for (; slot < cslot; slot += cd->name_entry_size) + { + open_capitem *oc; + recno = GET2(slot, 0); + cd->backref_map |= (recno < 32)? (1U << recno) : 1; + if (recno > cd->top_backref) cd->top_backref = recno; + + /* Check to see if this back reference is recursive, that it, it + is inside the group that it references. A flag is set so that the + group can be made atomic. */ + + for (oc = cd->open_caps; oc != NULL; oc = oc->next) + { + if (oc->number == recno) + { + oc->flag = TRUE; + break; + } + } + } + + continue; /* End of back ref handling */ + } + } + + /* First pass, or a non-duplicated name. */ + + goto HANDLE_REFERENCE; + + + /* ------------------------------------------------------------ */ + case CHAR_R: /* Recursion, same as (?0) */ + recno = 0; + if (*(++ptr) != CHAR_RIGHT_PARENTHESIS) + { + *errorcodeptr = ERR29; + goto FAILED; + } + goto HANDLE_RECURSION; + + + /* ------------------------------------------------------------ */ + case CHAR_MINUS: case CHAR_PLUS: /* Recursion or subroutine */ + case CHAR_0: case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: + case CHAR_5: case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9: + { + const pcre_uchar *called; + terminator = CHAR_RIGHT_PARENTHESIS; + + /* Come here from the \g<...> and \g'...' code (Oniguruma + compatibility). However, the syntax has been checked to ensure that + the ... are a (signed) number, so that neither ERR63 nor ERR29 will + be called on this path, nor with the jump to OTHER_CHAR_AFTER_QUERY + ever be taken. */ + + HANDLE_NUMERICAL_RECURSION: + + if ((refsign = *ptr) == CHAR_PLUS) + { + ptr++; + if (!IS_DIGIT(*ptr)) + { + *errorcodeptr = ERR63; + goto FAILED; + } + } + else if (refsign == CHAR_MINUS) + { + if (!IS_DIGIT(ptr[1])) + goto OTHER_CHAR_AFTER_QUERY; + ptr++; + } + + recno = 0; + while(IS_DIGIT(*ptr)) + { + if (recno > INT_MAX / 10 - 1) /* Integer overflow */ + { + while (IS_DIGIT(*ptr)) ptr++; + *errorcodeptr = ERR61; + goto FAILED; + } + recno = recno * 10 + *ptr++ - CHAR_0; + } + + if (*ptr != (pcre_uchar)terminator) + { + *errorcodeptr = ERR29; + goto FAILED; + } + + if (refsign == CHAR_MINUS) + { + if (recno == 0) + { + *errorcodeptr = ERR58; + goto FAILED; + } + recno = cd->bracount - recno + 1; + if (recno <= 0) + { + *errorcodeptr = ERR15; + goto FAILED; + } + } + else if (refsign == CHAR_PLUS) + { + if (recno == 0) + { + *errorcodeptr = ERR58; + goto FAILED; + } + recno += cd->bracount; + } + + /* Come here from code above that handles a named recursion */ + + HANDLE_RECURSION: + + previous = code; + item_hwm_offset = cd->hwm - cd->start_workspace; + called = cd->start_code; + + /* When we are actually compiling, find the bracket that is being + referenced. Temporarily end the regex in case it doesn't exist before + this point. If we end up with a forward reference, first check that + the bracket does occur later so we can give the error (and position) + now. Then remember this forward reference in the workspace so it can + be filled in at the end. */ + + if (lengthptr == NULL) + { + *code = OP_END; + if (recno != 0) + called = PRIV(find_bracket)(cd->start_code, utf, recno); + + /* Forward reference */ + + if (called == NULL) + { + if (recno > cd->final_bracount) + { + *errorcodeptr = ERR15; + goto FAILED; + } + + /* Fudge the value of "called" so that when it is inserted as an + offset below, what it actually inserted is the reference number + of the group. Then remember the forward reference. */ + + called = cd->start_code + recno; + if (cd->hwm >= cd->start_workspace + cd->workspace_size - + WORK_SIZE_SAFETY_MARGIN) + { + *errorcodeptr = expand_workspace(cd); + if (*errorcodeptr != 0) goto FAILED; + } + PUTINC(cd->hwm, 0, (int)(code + 1 - cd->start_code)); + } + + /* If not a forward reference, and the subpattern is still open, + this is a recursive call. We check to see if this is a left + recursion that could loop for ever, and diagnose that case. We + must not, however, do this check if we are in a conditional + subpattern because the condition might be testing for recursion in + a pattern such as /(?(R)a+|(?R)b)/, which is perfectly valid. + Forever loops are also detected at runtime, so those that occur in + conditional subpatterns will be picked up then. */ + + else if (GET(called, 1) == 0 && cond_depth <= 0 && + could_be_empty(called, code, bcptr, utf, cd)) + { + *errorcodeptr = ERR40; + goto FAILED; + } + } + + /* Insert the recursion/subroutine item. It does not have a set first + character (relevant if it is repeated, because it will then be + wrapped with ONCE brackets). */ + + *code = OP_RECURSE; + PUT(code, 1, (int)(called - cd->start_code)); + code += 1 + LINK_SIZE; + groupsetfirstchar = FALSE; + } + + /* Can't determine a first byte now */ + + if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE; + zerofirstchar = firstchar; + zerofirstcharflags = firstcharflags; + continue; + + + /* ------------------------------------------------------------ */ + default: /* Other characters: check option setting */ + OTHER_CHAR_AFTER_QUERY: + set = unset = 0; + optset = &set; + + while (*ptr != CHAR_RIGHT_PARENTHESIS && *ptr != CHAR_COLON) + { + switch (*ptr++) + { + case CHAR_MINUS: optset = &unset; break; + + case CHAR_J: /* Record that it changed in the external options */ + *optset |= PCRE_DUPNAMES; + cd->external_flags |= PCRE_JCHANGED; + break; + + case CHAR_i: *optset |= PCRE_CASELESS; break; + case CHAR_m: *optset |= PCRE_MULTILINE; break; + case CHAR_s: *optset |= PCRE_DOTALL; break; + case CHAR_x: *optset |= PCRE_EXTENDED; break; + case CHAR_U: *optset |= PCRE_UNGREEDY; break; + case CHAR_X: *optset |= PCRE_EXTRA; break; + + default: *errorcodeptr = ERR12; + ptr--; /* Correct the offset */ + goto FAILED; + } + } + + /* Set up the changed option bits, but don't change anything yet. */ + + newoptions = (options | set) & (~unset); + + /* If the options ended with ')' this is not the start of a nested + group with option changes, so the options change at this level. + If we are not at the pattern start, reset the greedy defaults and the + case value for firstchar and reqchar. */ + + if (*ptr == CHAR_RIGHT_PARENTHESIS) + { + greedy_default = ((newoptions & PCRE_UNGREEDY) != 0); + greedy_non_default = greedy_default ^ 1; + req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS:0; + + /* Change options at this level, and pass them back for use + in subsequent branches. */ + + *optionsptr = options = newoptions; + previous = NULL; /* This item can't be repeated */ + continue; /* It is complete */ + } + + /* If the options ended with ':' we are heading into a nested group + with possible change of options. Such groups are non-capturing and are + not assertions of any kind. All we need to do is skip over the ':'; + the newoptions value is handled below. */ + + bravalue = OP_BRA; + ptr++; + } /* End of switch for character following (? */ + } /* End of (? handling */ + + /* Opening parenthesis not followed by '*' or '?'. If PCRE_NO_AUTO_CAPTURE + is set, all unadorned brackets become non-capturing and behave like (?:...) + brackets. */ + + else if ((options & PCRE_NO_AUTO_CAPTURE) != 0) + { + bravalue = OP_BRA; + } + + /* Else we have a capturing group. */ + + else + { + NUMBERED_GROUP: + cd->bracount += 1; + PUT2(code, 1+LINK_SIZE, cd->bracount); + skipbytes = IMM2_SIZE; + } + + /* Process nested bracketed regex. First check for parentheses nested too + deeply. */ + + if ((cd->parens_depth += 1) > PARENS_NEST_LIMIT) + { + *errorcodeptr = ERR82; + goto FAILED; + } + + /* All assertions used not to be repeatable, but this was changed for Perl + compatibility. All kinds can now be repeated except for assertions that are + conditions (Perl also forbids these to be repeated). We copy code into a + non-register variable (tempcode) in order to be able to pass its address + because some compilers complain otherwise. At the start of a conditional + group whose condition is an assertion, cd->iscondassert is set. We unset it + here so as to allow assertions later in the group to be quantified. */ + + if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT && + cd->iscondassert) + { + previous = NULL; + cd->iscondassert = FALSE; + } + else + { + previous = code; + item_hwm_offset = cd->hwm - cd->start_workspace; + } + + *code = bravalue; + tempcode = code; + tempreqvary = cd->req_varyopt; /* Save value before bracket */ + tempbracount = cd->bracount; /* Save value before bracket */ + length_prevgroup = 0; /* Initialize for pre-compile phase */ + + if (!compile_regex( + newoptions, /* The complete new option state */ + &tempcode, /* Where to put code (updated) */ + &ptr, /* Input pointer (updated) */ + errorcodeptr, /* Where to put an error message */ + (bravalue == OP_ASSERTBACK || + bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */ + reset_bracount, /* True if (?| group */ + skipbytes, /* Skip over bracket number */ + cond_depth + + ((bravalue == OP_COND)?1:0), /* Depth of condition subpatterns */ + &subfirstchar, /* For possible first char */ + &subfirstcharflags, + &subreqchar, /* For possible last char */ + &subreqcharflags, + bcptr, /* Current branch chain */ + cd, /* Tables block */ + (lengthptr == NULL)? NULL : /* Actual compile phase */ + &length_prevgroup /* Pre-compile phase */ + )) + goto FAILED; + + cd->parens_depth -= 1; + + /* If this was an atomic group and there are no capturing groups within it, + generate OP_ONCE_NC instead of OP_ONCE. */ + + if (bravalue == OP_ONCE && cd->bracount <= tempbracount) + *code = OP_ONCE_NC; + + if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT) + cd->assert_depth -= 1; + + /* At the end of compiling, code is still pointing to the start of the + group, while tempcode has been updated to point past the end of the group. + The pattern pointer (ptr) is on the bracket. + + If this is a conditional bracket, check that there are no more than + two branches in the group, or just one if it's a DEFINE group. We do this + in the real compile phase, not in the pre-pass, where the whole group may + not be available. */ + + if (bravalue == OP_COND && lengthptr == NULL) + { + pcre_uchar *tc = code; + int condcount = 0; + + do { + condcount++; + tc += GET(tc,1); + } + while (*tc != OP_KET); + + /* A DEFINE group is never obeyed inline (the "condition" is always + false). It must have only one branch. */ + + if (code[LINK_SIZE+1] == OP_DEF) + { + if (condcount > 1) + { + *errorcodeptr = ERR54; + goto FAILED; + } + bravalue = OP_DEF; /* Just a flag to suppress char handling below */ + } + + /* A "normal" conditional group. If there is just one branch, we must not + make use of its firstchar or reqchar, because this is equivalent to an + empty second branch. */ + + else + { + if (condcount > 2) + { + *errorcodeptr = ERR27; + goto FAILED; + } + if (condcount == 1) subfirstcharflags = subreqcharflags = REQ_NONE; + } + } + + /* Error if hit end of pattern */ + + if (*ptr != CHAR_RIGHT_PARENTHESIS) + { + *errorcodeptr = ERR14; + goto FAILED; + } + + /* In the pre-compile phase, update the length by the length of the group, + less the brackets at either end. Then reduce the compiled code to just a + set of non-capturing brackets so that it doesn't use much memory if it is + duplicated by a quantifier.*/ + + if (lengthptr != NULL) + { + if (OFLOW_MAX - *lengthptr < length_prevgroup - 2 - 2*LINK_SIZE) + { + *errorcodeptr = ERR20; + goto FAILED; + } + *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE; + code++; /* This already contains bravalue */ + PUTINC(code, 0, 1 + LINK_SIZE); + *code++ = OP_KET; + PUTINC(code, 0, 1 + LINK_SIZE); + break; /* No need to waste time with special character handling */ + } + + /* Otherwise update the main code pointer to the end of the group. */ + + code = tempcode; + + /* For a DEFINE group, required and first character settings are not + relevant. */ + + if (bravalue == OP_DEF) break; + + /* Handle updating of the required and first characters for other types of + group. Update for normal brackets of all kinds, and conditions with two + branches (see code above). If the bracket is followed by a quantifier with + zero repeat, we have to back off. Hence the definition of zeroreqchar and + zerofirstchar outside the main loop so that they can be accessed for the + back off. */ + + zeroreqchar = reqchar; + zeroreqcharflags = reqcharflags; + zerofirstchar = firstchar; + zerofirstcharflags = firstcharflags; + groupsetfirstchar = FALSE; + + if (bravalue >= OP_ONCE) + { + /* If we have not yet set a firstchar in this branch, take it from the + subpattern, remembering that it was set here so that a repeat of more + than one can replicate it as reqchar if necessary. If the subpattern has + no firstchar, set "none" for the whole branch. In both cases, a zero + repeat forces firstchar to "none". */ + + if (firstcharflags == REQ_UNSET) + { + if (subfirstcharflags >= 0) + { + firstchar = subfirstchar; + firstcharflags = subfirstcharflags; + groupsetfirstchar = TRUE; + } + else firstcharflags = REQ_NONE; + zerofirstcharflags = REQ_NONE; + } + + /* If firstchar was previously set, convert the subpattern's firstchar + into reqchar if there wasn't one, using the vary flag that was in + existence beforehand. */ + + else if (subfirstcharflags >= 0 && subreqcharflags < 0) + { + subreqchar = subfirstchar; + subreqcharflags = subfirstcharflags | tempreqvary; + } + + /* If the subpattern set a required byte (or set a first byte that isn't + really the first byte - see above), set it. */ + + if (subreqcharflags >= 0) + { + reqchar = subreqchar; + reqcharflags = subreqcharflags; + } + } + + /* For a forward assertion, we take the reqchar, if set, provided that the + group has also set a first char. This can be helpful if the pattern that + follows the assertion doesn't set a different char. For example, it's + useful for /(?=abcde).+/. We can't set firstchar for an assertion, however + because it leads to incorrect effect for patterns such as /(?=a)a.+/ when + the "real" "a" would then become a reqchar instead of a firstchar. This is + overcome by a scan at the end if there's no firstchar, looking for an + asserted first char. */ + + else if (bravalue == OP_ASSERT && subreqcharflags >= 0 && + subfirstcharflags >= 0) + { + reqchar = subreqchar; + reqcharflags = subreqcharflags; + } + break; /* End of processing '(' */ + + + /* ===================================================================*/ + /* Handle metasequences introduced by \. For ones like \d, the ESC_ values + are arranged to be the negation of the corresponding OP_values in the + default case when PCRE_UCP is not set. For the back references, the values + are negative the reference number. Only back references and those types + that consume a character may be repeated. We can test for values between + ESC_b and ESC_Z for the latter; this may have to change if any new ones are + ever created. */ + + case CHAR_BACKSLASH: + tempptr = ptr; + escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options, FALSE); + if (*errorcodeptr != 0) goto FAILED; + + if (escape == 0) /* The escape coded a single character */ + c = ec; + else + { + /* For metasequences that actually match a character, we disable the + setting of a first character if it hasn't already been set. */ + + if (firstcharflags == REQ_UNSET && escape > ESC_b && escape < ESC_Z) + firstcharflags = REQ_NONE; + + /* Set values to reset to if this is followed by a zero repeat. */ + + zerofirstchar = firstchar; + zerofirstcharflags = firstcharflags; + zeroreqchar = reqchar; + zeroreqcharflags = reqcharflags; + + /* \g or \g'name' is a subroutine call by name and \g or \g'n' + is a subroutine call by number (Oniguruma syntax). In fact, the value + ESC_g is returned only for these cases. So we don't need to check for < + or ' if the value is ESC_g. For the Perl syntax \g{n} the value is + -n, and for the Perl syntax \g{name} the result is ESC_k (as + that is a synonym for a named back reference). */ + + if (escape == ESC_g) + { + const pcre_uchar *p; + pcre_uint32 cf; + + item_hwm_offset = cd->hwm - cd->start_workspace; /* Normally this is set when '(' is read */ + terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)? + CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE; + + /* These two statements stop the compiler for warning about possibly + unset variables caused by the jump to HANDLE_NUMERICAL_RECURSION. In + fact, because we do the check for a number below, the paths that + would actually be in error are never taken. */ + + skipbytes = 0; + reset_bracount = FALSE; + + /* If it's not a signed or unsigned number, treat it as a name. */ + + cf = ptr[1]; + if (cf != CHAR_PLUS && cf != CHAR_MINUS && !IS_DIGIT(cf)) + { + is_recurse = TRUE; + goto NAMED_REF_OR_RECURSE; + } + + /* Signed or unsigned number (cf = ptr[1]) is known to be plus or minus + or a digit. */ + + p = ptr + 2; + while (IS_DIGIT(*p)) p++; + if (*p != (pcre_uchar)terminator) + { + *errorcodeptr = ERR57; + goto FAILED; + } + ptr++; + goto HANDLE_NUMERICAL_RECURSION; + } + + /* \k or \k'name' is a back reference by name (Perl syntax). + We also support \k{name} (.NET syntax). */ + + if (escape == ESC_k) + { + if ((ptr[1] != CHAR_LESS_THAN_SIGN && + ptr[1] != CHAR_APOSTROPHE && ptr[1] != CHAR_LEFT_CURLY_BRACKET)) + { + *errorcodeptr = ERR69; + goto FAILED; + } + is_recurse = FALSE; + terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)? + CHAR_GREATER_THAN_SIGN : (*ptr == CHAR_APOSTROPHE)? + CHAR_APOSTROPHE : CHAR_RIGHT_CURLY_BRACKET; + goto NAMED_REF_OR_RECURSE; + } + + /* Back references are handled specially; must disable firstchar if + not set to cope with cases like (?=(\w+))\1: which would otherwise set + ':' later. */ + + if (escape < 0) + { + open_capitem *oc; + recno = -escape; + + /* Come here from named backref handling when the reference is to a + single group (i.e. not to a duplicated name. */ + + HANDLE_REFERENCE: + if (firstcharflags == REQ_UNSET) zerofirstcharflags = firstcharflags = REQ_NONE; + previous = code; + item_hwm_offset = cd->hwm - cd->start_workspace; + *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF; + PUT2INC(code, 0, recno); + cd->backref_map |= (recno < 32)? (1U << recno) : 1; + if (recno > cd->top_backref) cd->top_backref = recno; + + /* Check to see if this back reference is recursive, that it, it + is inside the group that it references. A flag is set so that the + group can be made atomic. */ + + for (oc = cd->open_caps; oc != NULL; oc = oc->next) + { + if (oc->number == recno) + { + oc->flag = TRUE; + break; + } + } + } + + /* So are Unicode property matches, if supported. */ + +#ifdef SUPPORT_UCP + else if (escape == ESC_P || escape == ESC_p) + { + BOOL negated; + unsigned int ptype = 0, pdata = 0; + if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr)) + goto FAILED; + previous = code; + item_hwm_offset = cd->hwm - cd->start_workspace; + *code++ = ((escape == ESC_p) != negated)? OP_PROP : OP_NOTPROP; + *code++ = ptype; + *code++ = pdata; + } +#else + + /* If Unicode properties are not supported, \X, \P, and \p are not + allowed. */ + + else if (escape == ESC_X || escape == ESC_P || escape == ESC_p) + { + *errorcodeptr = ERR45; + goto FAILED; + } +#endif + + /* For the rest (including \X when Unicode properties are supported), we + can obtain the OP value by negating the escape value in the default + situation when PCRE_UCP is not set. When it *is* set, we substitute + Unicode property tests. Note that \b and \B do a one-character + lookbehind, and \A also behaves as if it does. */ + + else + { + if ((escape == ESC_b || escape == ESC_B || escape == ESC_A) && + cd->max_lookbehind == 0) + cd->max_lookbehind = 1; +#ifdef SUPPORT_UCP + if (escape >= ESC_DU && escape <= ESC_wu) + { + nestptr = ptr + 1; /* Where to resume */ + ptr = substitutes[escape - ESC_DU] - 1; /* Just before substitute */ + } + else +#endif + /* In non-UTF-8 mode, we turn \C into OP_ALLANY instead of OP_ANYBYTE + so that it works in DFA mode and in lookbehinds. */ + + { + previous = (escape > ESC_b && escape < ESC_Z)? code : NULL; + item_hwm_offset = cd->hwm - cd->start_workspace; + *code++ = (!utf && escape == ESC_C)? OP_ALLANY : escape; + } + } + continue; + } + + /* We have a data character whose value is in c. In UTF-8 mode it may have + a value > 127. We set its representation in the length/buffer, and then + handle it as a data character. */ + +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR) + mclength = PRIV(ord2utf)(c, mcbuffer); + else +#endif + + { + mcbuffer[0] = c; + mclength = 1; + } + goto ONE_CHAR; + + + /* ===================================================================*/ + /* Handle a literal character. It is guaranteed not to be whitespace or # + when the extended flag is set. If we are in a UTF mode, it may be a + multi-unit literal character. */ + + default: + NORMAL_CHAR: + mclength = 1; + mcbuffer[0] = c; + +#ifdef SUPPORT_UTF + if (utf && HAS_EXTRALEN(c)) + ACROSSCHAR(TRUE, ptr[1], mcbuffer[mclength++] = *(++ptr)); +#endif + + /* At this point we have the character's bytes in mcbuffer, and the length + in mclength. When not in UTF-8 mode, the length is always 1. */ + + ONE_CHAR: + previous = code; + item_hwm_offset = cd->hwm - cd->start_workspace; + + /* For caseless UTF-8 mode when UCP support is available, check whether + this character has more than one other case. If so, generate a special + OP_PROP item instead of OP_CHARI. */ + +#ifdef SUPPORT_UCP + if (utf && (options & PCRE_CASELESS) != 0) + { + GETCHAR(c, mcbuffer); + if ((c = UCD_CASESET(c)) != 0) + { + *code++ = OP_PROP; + *code++ = PT_CLIST; + *code++ = c; + if (firstcharflags == REQ_UNSET) + firstcharflags = zerofirstcharflags = REQ_NONE; + break; + } + } +#endif + + /* Caseful matches, or not one of the multicase characters. */ + + *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARI : OP_CHAR; + for (c = 0; c < mclength; c++) *code++ = mcbuffer[c]; + + /* Remember if \r or \n were seen */ + + if (mcbuffer[0] == CHAR_CR || mcbuffer[0] == CHAR_NL) + cd->external_flags |= PCRE_HASCRORLF; + + /* Set the first and required bytes appropriately. If no previous first + byte, set it from this character, but revert to none on a zero repeat. + Otherwise, leave the firstchar value alone, and don't change it on a zero + repeat. */ + + if (firstcharflags == REQ_UNSET) + { + zerofirstcharflags = REQ_NONE; + zeroreqchar = reqchar; + zeroreqcharflags = reqcharflags; + + /* If the character is more than one byte long, we can set firstchar + only if it is not to be matched caselessly. */ + + if (mclength == 1 || req_caseopt == 0) + { + firstchar = mcbuffer[0]; + firstcharflags = req_caseopt; + + if (mclength != 1) + { + reqchar = code[-1]; + reqcharflags = cd->req_varyopt; + } + } + else firstcharflags = reqcharflags = REQ_NONE; + } + + /* firstchar was previously set; we can set reqchar only if the length is + 1 or the matching is caseful. */ + + else + { + zerofirstchar = firstchar; + zerofirstcharflags = firstcharflags; + zeroreqchar = reqchar; + zeroreqcharflags = reqcharflags; + if (mclength == 1 || req_caseopt == 0) + { + reqchar = code[-1]; + reqcharflags = req_caseopt | cd->req_varyopt; + } + } + + break; /* End of literal character handling */ + } + } /* end of big loop */ + + +/* Control never reaches here by falling through, only by a goto for all the +error states. Pass back the position in the pattern so that it can be displayed +to the user for diagnosing the error. */ + +FAILED: +*ptrptr = ptr; +return FALSE; +} + + + +/************************************************* +* Compile sequence of alternatives * +*************************************************/ + +/* On entry, ptr is pointing past the bracket character, but on return it +points to the closing bracket, or vertical bar, or end of string. The code +variable is pointing at the byte into which the BRA operator has been stored. +This function is used during the pre-compile phase when we are trying to find +out the amount of memory needed, as well as during the real compile phase. The +value of lengthptr distinguishes the two phases. + +Arguments: + options option bits, including any changes for this subpattern + codeptr -> the address of the current code pointer + ptrptr -> the address of the current pattern pointer + errorcodeptr -> pointer to error code variable + lookbehind TRUE if this is a lookbehind assertion + reset_bracount TRUE to reset the count for each branch + skipbytes skip this many bytes at start (for brackets and OP_COND) + cond_depth depth of nesting for conditional subpatterns + firstcharptr place to put the first required character + firstcharflagsptr place to put the first character flags, or a negative number + reqcharptr place to put the last required character + reqcharflagsptr place to put the last required character flags, or a negative number + bcptr pointer to the chain of currently open branches + cd points to the data block with tables pointers etc. + lengthptr NULL during the real compile phase + points to length accumulator during pre-compile phase + +Returns: TRUE on success +*/ + +static BOOL +compile_regex(int options, pcre_uchar **codeptr, const pcre_uchar **ptrptr, + int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes, + int cond_depth, + pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr, + pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr, + branch_chain *bcptr, compile_data *cd, int *lengthptr) +{ +const pcre_uchar *ptr = *ptrptr; +pcre_uchar *code = *codeptr; +pcre_uchar *last_branch = code; +pcre_uchar *start_bracket = code; +pcre_uchar *reverse_count = NULL; +open_capitem capitem; +int capnumber = 0; +pcre_uint32 firstchar, reqchar; +pcre_int32 firstcharflags, reqcharflags; +pcre_uint32 branchfirstchar, branchreqchar; +pcre_int32 branchfirstcharflags, branchreqcharflags; +int length; +unsigned int orig_bracount; +unsigned int max_bracount; +branch_chain bc; +size_t save_hwm_offset; + +/* If set, call the external function that checks for stack availability. */ + +if (PUBL(stack_guard) != NULL && PUBL(stack_guard)()) + { + *errorcodeptr= ERR85; + return FALSE; + } + +/* Miscellaneous initialization */ + +bc.outer = bcptr; +bc.current_branch = code; + +firstchar = reqchar = 0; +firstcharflags = reqcharflags = REQ_UNSET; + +save_hwm_offset = cd->hwm - cd->start_workspace; + +/* Accumulate the length for use in the pre-compile phase. Start with the +length of the BRA and KET and any extra bytes that are required at the +beginning. We accumulate in a local variable to save frequent testing of +lenthptr for NULL. We cannot do this by looking at the value of code at the +start and end of each alternative, because compiled items are discarded during +the pre-compile phase so that the work space is not exceeded. */ + +length = 2 + 2*LINK_SIZE + skipbytes; + +/* WARNING: If the above line is changed for any reason, you must also change +the code that abstracts option settings at the start of the pattern and makes +them global. It tests the value of length for (2 + 2*LINK_SIZE) in the +pre-compile phase to find out whether anything has yet been compiled or not. */ + +/* If this is a capturing subpattern, add to the chain of open capturing items +so that we can detect them if (*ACCEPT) is encountered. This is also used to +detect groups that contain recursive back references to themselves. Note that +only OP_CBRA need be tested here; changing this opcode to one of its variants, +e.g. OP_SCBRAPOS, happens later, after the group has been compiled. */ + +if (*code == OP_CBRA) + { + capnumber = GET2(code, 1 + LINK_SIZE); + capitem.number = capnumber; + capitem.next = cd->open_caps; + capitem.flag = FALSE; + cd->open_caps = &capitem; + } + +/* Offset is set zero to mark that this bracket is still open */ + +PUT(code, 1, 0); +code += 1 + LINK_SIZE + skipbytes; + +/* Loop for each alternative branch */ + +orig_bracount = max_bracount = cd->bracount; +for (;;) + { + /* For a (?| group, reset the capturing bracket count so that each branch + uses the same numbers. */ + + if (reset_bracount) cd->bracount = orig_bracount; + + /* Set up dummy OP_REVERSE if lookbehind assertion */ + + if (lookbehind) + { + *code++ = OP_REVERSE; + reverse_count = code; + PUTINC(code, 0, 0); + length += 1 + LINK_SIZE; + } + + /* Now compile the branch; in the pre-compile phase its length gets added + into the length. */ + + if (!compile_branch(&options, &code, &ptr, errorcodeptr, &branchfirstchar, + &branchfirstcharflags, &branchreqchar, &branchreqcharflags, &bc, + cond_depth, cd, (lengthptr == NULL)? NULL : &length)) + { + *ptrptr = ptr; + return FALSE; + } + + /* Keep the highest bracket count in case (?| was used and some branch + has fewer than the rest. */ + + if (cd->bracount > max_bracount) max_bracount = cd->bracount; + + /* In the real compile phase, there is some post-processing to be done. */ + + if (lengthptr == NULL) + { + /* If this is the first branch, the firstchar and reqchar values for the + branch become the values for the regex. */ + + if (*last_branch != OP_ALT) + { + firstchar = branchfirstchar; + firstcharflags = branchfirstcharflags; + reqchar = branchreqchar; + reqcharflags = branchreqcharflags; + } + + /* If this is not the first branch, the first char and reqchar have to + match the values from all the previous branches, except that if the + previous value for reqchar didn't have REQ_VARY set, it can still match, + and we set REQ_VARY for the regex. */ + + else + { + /* If we previously had a firstchar, but it doesn't match the new branch, + we have to abandon the firstchar for the regex, but if there was + previously no reqchar, it takes on the value of the old firstchar. */ + + if (firstcharflags >= 0 && + (firstcharflags != branchfirstcharflags || firstchar != branchfirstchar)) + { + if (reqcharflags < 0) + { + reqchar = firstchar; + reqcharflags = firstcharflags; + } + firstcharflags = REQ_NONE; + } + + /* If we (now or from before) have no firstchar, a firstchar from the + branch becomes a reqchar if there isn't a branch reqchar. */ + + if (firstcharflags < 0 && branchfirstcharflags >= 0 && branchreqcharflags < 0) + { + branchreqchar = branchfirstchar; + branchreqcharflags = branchfirstcharflags; + } + + /* Now ensure that the reqchars match */ + + if (((reqcharflags & ~REQ_VARY) != (branchreqcharflags & ~REQ_VARY)) || + reqchar != branchreqchar) + reqcharflags = REQ_NONE; + else + { + reqchar = branchreqchar; + reqcharflags |= branchreqcharflags; /* To "or" REQ_VARY */ + } + } + + /* If lookbehind, check that this branch matches a fixed-length string, and + put the length into the OP_REVERSE item. Temporarily mark the end of the + branch with OP_END. If the branch contains OP_RECURSE, the result is -3 + because there may be forward references that we can't check here. Set a + flag to cause another lookbehind check at the end. Why not do it all at the + end? Because common, erroneous checks are picked up here and the offset of + the problem can be shown. */ + + if (lookbehind) + { + int fixed_length; + *code = OP_END; + fixed_length = find_fixedlength(last_branch, (options & PCRE_UTF8) != 0, + FALSE, cd, NULL); + DPRINTF(("fixed length = %d\n", fixed_length)); + if (fixed_length == -3) + { + cd->check_lookbehind = TRUE; + } + else if (fixed_length < 0) + { + *errorcodeptr = (fixed_length == -2)? ERR36 : + (fixed_length == -4)? ERR70: ERR25; + *ptrptr = ptr; + return FALSE; + } + else + { + if (fixed_length > cd->max_lookbehind) + cd->max_lookbehind = fixed_length; + PUT(reverse_count, 0, fixed_length); + } + } + } + + /* Reached end of expression, either ')' or end of pattern. In the real + compile phase, go back through the alternative branches and reverse the chain + of offsets, with the field in the BRA item now becoming an offset to the + first alternative. If there are no alternatives, it points to the end of the + group. The length in the terminating ket is always the length of the whole + bracketed item. Return leaving the pointer at the terminating char. */ + + if (*ptr != CHAR_VERTICAL_LINE) + { + if (lengthptr == NULL) + { + int branch_length = (int)(code - last_branch); + do + { + int prev_length = GET(last_branch, 1); + PUT(last_branch, 1, branch_length); + branch_length = prev_length; + last_branch -= branch_length; + } + while (branch_length > 0); + } + + /* Fill in the ket */ + + *code = OP_KET; + PUT(code, 1, (int)(code - start_bracket)); + code += 1 + LINK_SIZE; + + /* If it was a capturing subpattern, check to see if it contained any + recursive back references. If so, we must wrap it in atomic brackets. + Because we are moving code along, we must ensure that any pending recursive + references are updated. In any event, remove the block from the chain. */ + + if (capnumber > 0) + { + if (cd->open_caps->flag) + { + *code = OP_END; + adjust_recurse(start_bracket, 1 + LINK_SIZE, + (options & PCRE_UTF8) != 0, cd, save_hwm_offset); + memmove(start_bracket + 1 + LINK_SIZE, start_bracket, + IN_UCHARS(code - start_bracket)); + *start_bracket = OP_ONCE; + code += 1 + LINK_SIZE; + PUT(start_bracket, 1, (int)(code - start_bracket)); + *code = OP_KET; + PUT(code, 1, (int)(code - start_bracket)); + code += 1 + LINK_SIZE; + length += 2 + 2*LINK_SIZE; + } + cd->open_caps = cd->open_caps->next; + } + + /* Retain the highest bracket number, in case resetting was used. */ + + cd->bracount = max_bracount; + + /* Set values to pass back */ + + *codeptr = code; + *ptrptr = ptr; + *firstcharptr = firstchar; + *firstcharflagsptr = firstcharflags; + *reqcharptr = reqchar; + *reqcharflagsptr = reqcharflags; + if (lengthptr != NULL) + { + if (OFLOW_MAX - *lengthptr < length) + { + *errorcodeptr = ERR20; + return FALSE; + } + *lengthptr += length; + } + return TRUE; + } + + /* Another branch follows. In the pre-compile phase, we can move the code + pointer back to where it was for the start of the first branch. (That is, + pretend that each branch is the only one.) + + In the real compile phase, insert an ALT node. Its length field points back + to the previous branch while the bracket remains open. At the end the chain + is reversed. It's done like this so that the start of the bracket has a + zero offset until it is closed, making it possible to detect recursion. */ + + if (lengthptr != NULL) + { + code = *codeptr + 1 + LINK_SIZE + skipbytes; + length += 1 + LINK_SIZE; + } + else + { + *code = OP_ALT; + PUT(code, 1, (int)(code - last_branch)); + bc.current_branch = last_branch = code; + code += 1 + LINK_SIZE; + } + + ptr++; + } +/* Control never reaches here */ +} + + + + +/************************************************* +* Check for anchored expression * +*************************************************/ + +/* Try to find out if this is an anchored regular expression. Consider each +alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket +all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then +it's anchored. However, if this is a multiline pattern, then only OP_SOD will +be found, because ^ generates OP_CIRCM in that mode. + +We can also consider a regex to be anchored if OP_SOM starts all its branches. +This is the code for \G, which means "match at start of match position, taking +into account the match offset". + +A branch is also implicitly anchored if it starts with .* and DOTALL is set, +because that will try the rest of the pattern at all possible matching points, +so there is no point trying again.... er .... + +.... except when the .* appears inside capturing parentheses, and there is a +subsequent back reference to those parentheses. We haven't enough information +to catch that case precisely. + +At first, the best we could do was to detect when .* was in capturing brackets +and the highest back reference was greater than or equal to that level. +However, by keeping a bitmap of the first 31 back references, we can catch some +of the more common cases more precisely. + +... A second exception is when the .* appears inside an atomic group, because +this prevents the number of characters it matches from being adjusted. + +Arguments: + code points to start of expression (the bracket) + bracket_map a bitmap of which brackets we are inside while testing; this + handles up to substring 31; after that we just have to take + the less precise approach + cd points to the compile data block + atomcount atomic group level + +Returns: TRUE or FALSE +*/ + +static BOOL +is_anchored(register const pcre_uchar *code, unsigned int bracket_map, + compile_data *cd, int atomcount) +{ +do { + const pcre_uchar *scode = first_significant_code( + code + PRIV(OP_lengths)[*code], FALSE); + register int op = *scode; + + /* Non-capturing brackets */ + + if (op == OP_BRA || op == OP_BRAPOS || + op == OP_SBRA || op == OP_SBRAPOS) + { + if (!is_anchored(scode, bracket_map, cd, atomcount)) return FALSE; + } + + /* Capturing brackets */ + + else if (op == OP_CBRA || op == OP_CBRAPOS || + op == OP_SCBRA || op == OP_SCBRAPOS) + { + int n = GET2(scode, 1+LINK_SIZE); + int new_map = bracket_map | ((n < 32)? (1U << n) : 1); + if (!is_anchored(scode, new_map, cd, atomcount)) return FALSE; + } + + /* Positive forward assertion */ + + else if (op == OP_ASSERT) + { + if (!is_anchored(scode, bracket_map, cd, atomcount)) return FALSE; + } + + /* Condition; not anchored if no second branch */ + + else if (op == OP_COND) + { + if (scode[GET(scode,1)] != OP_ALT) return FALSE; + if (!is_anchored(scode, bracket_map, cd, atomcount)) return FALSE; + } + + /* Atomic groups */ + + else if (op == OP_ONCE || op == OP_ONCE_NC) + { + if (!is_anchored(scode, bracket_map, cd, atomcount + 1)) + return FALSE; + } + + /* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and + it isn't in brackets that are or may be referenced or inside an atomic + group. */ + + else if ((op == OP_TYPESTAR || op == OP_TYPEMINSTAR || + op == OP_TYPEPOSSTAR)) + { + if (scode[1] != OP_ALLANY || (bracket_map & cd->backref_map) != 0 || + atomcount > 0 || cd->had_pruneorskip) + return FALSE; + } + + /* Check for explicit anchoring */ + + else if (op != OP_SOD && op != OP_SOM && op != OP_CIRC) return FALSE; + + code += GET(code, 1); + } +while (*code == OP_ALT); /* Loop for each alternative */ +return TRUE; +} + + + +/************************************************* +* Check for starting with ^ or .* * +*************************************************/ + +/* This is called to find out if every branch starts with ^ or .* so that +"first char" processing can be done to speed things up in multiline +matching and for non-DOTALL patterns that start with .* (which must start at +the beginning or after \n). As in the case of is_anchored() (see above), we +have to take account of back references to capturing brackets that contain .* +because in that case we can't make the assumption. Also, the appearance of .* +inside atomic brackets or in an assertion, or in a pattern that contains *PRUNE +or *SKIP does not count, because once again the assumption no longer holds. + +Arguments: + code points to start of expression (the bracket) + bracket_map a bitmap of which brackets we are inside while testing; this + handles up to substring 31; after that we just have to take + the less precise approach + cd points to the compile data + atomcount atomic group level + inassert TRUE if in an assertion + +Returns: TRUE or FALSE +*/ + +static BOOL +is_startline(const pcre_uchar *code, unsigned int bracket_map, + compile_data *cd, int atomcount, BOOL inassert) +{ +do { + const pcre_uchar *scode = first_significant_code( + code + PRIV(OP_lengths)[*code], FALSE); + register int op = *scode; + + /* If we are at the start of a conditional assertion group, *both* the + conditional assertion *and* what follows the condition must satisfy the test + for start of line. Other kinds of condition fail. Note that there may be an + auto-callout at the start of a condition. */ + + if (op == OP_COND) + { + scode += 1 + LINK_SIZE; + if (*scode == OP_CALLOUT) scode += PRIV(OP_lengths)[OP_CALLOUT]; + switch (*scode) + { + case OP_CREF: + case OP_DNCREF: + case OP_RREF: + case OP_DNRREF: + case OP_DEF: + case OP_FAIL: + return FALSE; + + default: /* Assertion */ + if (!is_startline(scode, bracket_map, cd, atomcount, TRUE)) return FALSE; + do scode += GET(scode, 1); while (*scode == OP_ALT); + scode += 1 + LINK_SIZE; + break; + } + scode = first_significant_code(scode, FALSE); + op = *scode; + } + + /* Non-capturing brackets */ + + if (op == OP_BRA || op == OP_BRAPOS || + op == OP_SBRA || op == OP_SBRAPOS) + { + if (!is_startline(scode, bracket_map, cd, atomcount, inassert)) return FALSE; + } + + /* Capturing brackets */ + + else if (op == OP_CBRA || op == OP_CBRAPOS || + op == OP_SCBRA || op == OP_SCBRAPOS) + { + int n = GET2(scode, 1+LINK_SIZE); + int new_map = bracket_map | ((n < 32)? (1U << n) : 1); + if (!is_startline(scode, new_map, cd, atomcount, inassert)) return FALSE; + } + + /* Positive forward assertions */ + + else if (op == OP_ASSERT) + { + if (!is_startline(scode, bracket_map, cd, atomcount, TRUE)) return FALSE; + } + + /* Atomic brackets */ + + else if (op == OP_ONCE || op == OP_ONCE_NC) + { + if (!is_startline(scode, bracket_map, cd, atomcount + 1, inassert)) return FALSE; + } + + /* .* means "start at start or after \n" if it isn't in atomic brackets or + brackets that may be referenced or an assertion, as long as the pattern does + not contain *PRUNE or *SKIP, because these break the feature. Consider, for + example, /.*?a(*PRUNE)b/ with the subject "aab", which matches "ab", i.e. + not at the start of a line. */ + + else if (op == OP_TYPESTAR || op == OP_TYPEMINSTAR || op == OP_TYPEPOSSTAR) + { + if (scode[1] != OP_ANY || (bracket_map & cd->backref_map) != 0 || + atomcount > 0 || cd->had_pruneorskip || inassert) + return FALSE; + } + + /* Check for explicit circumflex; anything else gives a FALSE result. Note + in particular that this includes atomic brackets OP_ONCE and OP_ONCE_NC + because the number of characters matched by .* cannot be adjusted inside + them. */ + + else if (op != OP_CIRC && op != OP_CIRCM) return FALSE; + + /* Move on to the next alternative */ + + code += GET(code, 1); + } +while (*code == OP_ALT); /* Loop for each alternative */ +return TRUE; +} + + + +/************************************************* +* Check for asserted fixed first char * +*************************************************/ + +/* During compilation, the "first char" settings from forward assertions are +discarded, because they can cause conflicts with actual literals that follow. +However, if we end up without a first char setting for an unanchored pattern, +it is worth scanning the regex to see if there is an initial asserted first +char. If all branches start with the same asserted char, or with a +non-conditional bracket all of whose alternatives start with the same asserted +char (recurse ad lib), then we return that char, with the flags set to zero or +REQ_CASELESS; otherwise return zero with REQ_NONE in the flags. + +Arguments: + code points to start of expression (the bracket) + flags points to the first char flags, or to REQ_NONE + inassert TRUE if in an assertion + +Returns: the fixed first char, or 0 with REQ_NONE in flags +*/ + +static pcre_uint32 +find_firstassertedchar(const pcre_uchar *code, pcre_int32 *flags, + BOOL inassert) +{ +register pcre_uint32 c = 0; +int cflags = REQ_NONE; + +*flags = REQ_NONE; +do { + pcre_uint32 d; + int dflags; + int xl = (*code == OP_CBRA || *code == OP_SCBRA || + *code == OP_CBRAPOS || *code == OP_SCBRAPOS)? IMM2_SIZE:0; + const pcre_uchar *scode = first_significant_code(code + 1+LINK_SIZE + xl, + TRUE); + register pcre_uchar op = *scode; + + switch(op) + { + default: + return 0; + + case OP_BRA: + case OP_BRAPOS: + case OP_CBRA: + case OP_SCBRA: + case OP_CBRAPOS: + case OP_SCBRAPOS: + case OP_ASSERT: + case OP_ONCE: + case OP_ONCE_NC: + d = find_firstassertedchar(scode, &dflags, op == OP_ASSERT); + if (dflags < 0) + return 0; + if (cflags < 0) { c = d; cflags = dflags; } else if (c != d || cflags != dflags) return 0; + break; + + case OP_EXACT: + scode += IMM2_SIZE; + /* Fall through */ + + case OP_CHAR: + case OP_PLUS: + case OP_MINPLUS: + case OP_POSPLUS: + if (!inassert) return 0; + if (cflags < 0) { c = scode[1]; cflags = 0; } + else if (c != scode[1]) return 0; + break; + + case OP_EXACTI: + scode += IMM2_SIZE; + /* Fall through */ + + case OP_CHARI: + case OP_PLUSI: + case OP_MINPLUSI: + case OP_POSPLUSI: + if (!inassert) return 0; + if (cflags < 0) { c = scode[1]; cflags = REQ_CASELESS; } + else if (c != scode[1]) return 0; + break; + } + + code += GET(code, 1); + } +while (*code == OP_ALT); + +*flags = cflags; +return c; +} + + + +/************************************************* +* Add an entry to the name/number table * +*************************************************/ + +/* This function is called between compiling passes to add an entry to the +name/number table, maintaining alphabetical order. Checking for permitted +and forbidden duplicates has already been done. + +Arguments: + cd the compile data block + name the name to add + length the length of the name + groupno the group number + +Returns: nothing +*/ + +static void +add_name(compile_data *cd, const pcre_uchar *name, int length, + unsigned int groupno) +{ +int i; +pcre_uchar *slot = cd->name_table; + +for (i = 0; i < cd->names_found; i++) + { + int crc = memcmp(name, slot+IMM2_SIZE, IN_UCHARS(length)); + if (crc == 0 && slot[IMM2_SIZE+length] != 0) + crc = -1; /* Current name is a substring */ + + /* Make space in the table and break the loop for an earlier name. For a + duplicate or later name, carry on. We do this for duplicates so that in the + simple case (when ?(| is not used) they are in order of their numbers. In all + cases they are in the order in which they appear in the pattern. */ + + if (crc < 0) + { + memmove(slot + cd->name_entry_size, slot, + IN_UCHARS((cd->names_found - i) * cd->name_entry_size)); + break; + } + + /* Continue the loop for a later or duplicate name */ + + slot += cd->name_entry_size; + } + +PUT2(slot, 0, groupno); +memcpy(slot + IMM2_SIZE, name, IN_UCHARS(length)); +slot[IMM2_SIZE + length] = 0; +cd->names_found++; +} + + + +/************************************************* +* Compile a Regular Expression * +*************************************************/ + +/* This function takes a string and returns a pointer to a block of store +holding a compiled version of the expression. The original API for this +function had no error code return variable; it is retained for backwards +compatibility. The new function is given a new name. + +Arguments: + pattern the regular expression + options various option bits + errorcodeptr pointer to error code variable (pcre_compile2() only) + can be NULL if you don't want a code value + errorptr pointer to pointer to error text + erroroffset ptr offset in pattern where error was detected + tables pointer to character tables or NULL + +Returns: pointer to compiled data block, or NULL on error, + with errorptr and erroroffset set +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION +pcre_compile(const char *pattern, int options, const char **errorptr, + int *erroroffset, const unsigned char *tables) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN pcre16 * PCRE_CALL_CONVENTION +pcre16_compile(PCRE_SPTR16 pattern, int options, const char **errorptr, + int *erroroffset, const unsigned char *tables) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN pcre32 * PCRE_CALL_CONVENTION +pcre32_compile(PCRE_SPTR32 pattern, int options, const char **errorptr, + int *erroroffset, const unsigned char *tables) +#endif +{ +#if defined COMPILE_PCRE8 +return pcre_compile2(pattern, options, NULL, errorptr, erroroffset, tables); +#elif defined COMPILE_PCRE16 +return pcre16_compile2(pattern, options, NULL, errorptr, erroroffset, tables); +#elif defined COMPILE_PCRE32 +return pcre32_compile2(pattern, options, NULL, errorptr, erroroffset, tables); +#endif +} + + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION +pcre_compile2(const char *pattern, int options, int *errorcodeptr, + const char **errorptr, int *erroroffset, const unsigned char *tables) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN pcre16 * PCRE_CALL_CONVENTION +pcre16_compile2(PCRE_SPTR16 pattern, int options, int *errorcodeptr, + const char **errorptr, int *erroroffset, const unsigned char *tables) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN pcre32 * PCRE_CALL_CONVENTION +pcre32_compile2(PCRE_SPTR32 pattern, int options, int *errorcodeptr, + const char **errorptr, int *erroroffset, const unsigned char *tables) +#endif +{ +REAL_PCRE *re; +int length = 1; /* For final END opcode */ +pcre_int32 firstcharflags, reqcharflags; +pcre_uint32 firstchar, reqchar; +pcre_uint32 limit_match = PCRE_UINT32_MAX; +pcre_uint32 limit_recursion = PCRE_UINT32_MAX; +int newline; +int errorcode = 0; +int skipatstart = 0; +BOOL utf; +BOOL never_utf = FALSE; +size_t size; +pcre_uchar *code; +const pcre_uchar *codestart; +const pcre_uchar *ptr; +compile_data compile_block; +compile_data *cd = &compile_block; + +/* This space is used for "compiling" into during the first phase, when we are +computing the amount of memory that is needed. Compiled items are thrown away +as soon as possible, so that a fairly large buffer should be sufficient for +this purpose. The same space is used in the second phase for remembering where +to fill in forward references to subpatterns. That may overflow, in which case +new memory is obtained from malloc(). */ + +pcre_uchar cworkspace[COMPILE_WORK_SIZE]; + +/* This vector is used for remembering name groups during the pre-compile. In a +similar way to cworkspace, it can be expanded using malloc() if necessary. */ + +named_group named_groups[NAMED_GROUP_LIST_SIZE]; +cd->named_groups = named_groups; +cd->named_group_list_size = NAMED_GROUP_LIST_SIZE; + +/* Set this early so that early errors get offset 0. */ + +ptr = (const pcre_uchar *)pattern; + +/* We can't pass back an error message if errorptr is NULL; I guess the best we +can do is just return NULL, but we can set a code value if there is a code +pointer. */ + +if (errorptr == NULL) + { + if (errorcodeptr != NULL) *errorcodeptr = 99; + return NULL; + } + +*errorptr = NULL; +if (errorcodeptr != NULL) *errorcodeptr = ERR0; + +/* However, we can give a message for this error */ + +if (erroroffset == NULL) + { + errorcode = ERR16; + goto PCRE_EARLY_ERROR_RETURN2; + } + +*erroroffset = 0; + +/* Set up pointers to the individual character tables */ + +if (tables == NULL) tables = PRIV(default_tables); +cd->lcc = tables + lcc_offset; +cd->fcc = tables + fcc_offset; +cd->cbits = tables + cbits_offset; +cd->ctypes = tables + ctypes_offset; + +/* Check that all undefined public option bits are zero */ + +if ((options & ~PUBLIC_COMPILE_OPTIONS) != 0) + { + errorcode = ERR17; + goto PCRE_EARLY_ERROR_RETURN; + } + +/* If PCRE_NEVER_UTF is set, remember it. */ + +if ((options & PCRE_NEVER_UTF) != 0) never_utf = TRUE; + +/* Check for global one-time settings at the start of the pattern, and remember +the offset for later. */ + +cd->external_flags = 0; /* Initialize here for LIMIT_MATCH/RECURSION */ + +while (ptr[skipatstart] == CHAR_LEFT_PARENTHESIS && + ptr[skipatstart+1] == CHAR_ASTERISK) + { + int newnl = 0; + int newbsr = 0; + +/* For completeness and backward compatibility, (*UTFn) is supported in the +relevant libraries, but (*UTF) is generic and always supported. Note that +PCRE_UTF8 == PCRE_UTF16 == PCRE_UTF32. */ + +#ifdef COMPILE_PCRE8 + if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF8_RIGHTPAR, 5) == 0) + { skipatstart += 7; options |= PCRE_UTF8; continue; } +#endif +#ifdef COMPILE_PCRE16 + if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF16_RIGHTPAR, 6) == 0) + { skipatstart += 8; options |= PCRE_UTF16; continue; } +#endif +#ifdef COMPILE_PCRE32 + if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF32_RIGHTPAR, 6) == 0) + { skipatstart += 8; options |= PCRE_UTF32; continue; } +#endif + + else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF_RIGHTPAR, 4) == 0) + { skipatstart += 6; options |= PCRE_UTF8; continue; } + else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UCP_RIGHTPAR, 4) == 0) + { skipatstart += 6; options |= PCRE_UCP; continue; } + else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_NO_AUTO_POSSESS_RIGHTPAR, 16) == 0) + { skipatstart += 18; options |= PCRE_NO_AUTO_POSSESS; continue; } + else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_NO_START_OPT_RIGHTPAR, 13) == 0) + { skipatstart += 15; options |= PCRE_NO_START_OPTIMIZE; continue; } + + else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LIMIT_MATCH_EQ, 12) == 0) + { + pcre_uint32 c = 0; + int p = skipatstart + 14; + while (isdigit(ptr[p])) + { + if (c > PCRE_UINT32_MAX / 10 - 1) break; /* Integer overflow */ + c = c*10 + ptr[p++] - CHAR_0; + } + if (ptr[p++] != CHAR_RIGHT_PARENTHESIS) break; + if (c < limit_match) + { + limit_match = c; + cd->external_flags |= PCRE_MLSET; + } + skipatstart = p; + continue; + } + + else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LIMIT_RECURSION_EQ, 16) == 0) + { + pcre_uint32 c = 0; + int p = skipatstart + 18; + while (isdigit(ptr[p])) + { + if (c > PCRE_UINT32_MAX / 10 - 1) break; /* Integer overflow check */ + c = c*10 + ptr[p++] - CHAR_0; + } + if (ptr[p++] != CHAR_RIGHT_PARENTHESIS) break; + if (c < limit_recursion) + { + limit_recursion = c; + cd->external_flags |= PCRE_RLSET; + } + skipatstart = p; + continue; + } + + if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_CR_RIGHTPAR, 3) == 0) + { skipatstart += 5; newnl = PCRE_NEWLINE_CR; } + else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LF_RIGHTPAR, 3) == 0) + { skipatstart += 5; newnl = PCRE_NEWLINE_LF; } + else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_CRLF_RIGHTPAR, 5) == 0) + { skipatstart += 7; newnl = PCRE_NEWLINE_CR + PCRE_NEWLINE_LF; } + else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_ANY_RIGHTPAR, 4) == 0) + { skipatstart += 6; newnl = PCRE_NEWLINE_ANY; } + else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_ANYCRLF_RIGHTPAR, 8) == 0) + { skipatstart += 10; newnl = PCRE_NEWLINE_ANYCRLF; } + + else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_BSR_ANYCRLF_RIGHTPAR, 12) == 0) + { skipatstart += 14; newbsr = PCRE_BSR_ANYCRLF; } + else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_BSR_UNICODE_RIGHTPAR, 12) == 0) + { skipatstart += 14; newbsr = PCRE_BSR_UNICODE; } + + if (newnl != 0) + options = (options & ~PCRE_NEWLINE_BITS) | newnl; + else if (newbsr != 0) + options = (options & ~(PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) | newbsr; + else break; + } + +/* PCRE_UTF(16|32) have the same value as PCRE_UTF8. */ +utf = (options & PCRE_UTF8) != 0; +if (utf && never_utf) + { + errorcode = ERR78; + goto PCRE_EARLY_ERROR_RETURN2; + } + +/* Can't support UTF unless PCRE has been compiled to include the code. The +return of an error code from PRIV(valid_utf)() is a new feature, introduced in +release 8.13. It is passed back from pcre_[dfa_]exec(), but at the moment is +not used here. */ + +#ifdef SUPPORT_UTF +if (utf && (options & PCRE_NO_UTF8_CHECK) == 0 && + (errorcode = PRIV(valid_utf)((PCRE_PUCHAR)pattern, -1, erroroffset)) != 0) + { +#if defined COMPILE_PCRE8 + errorcode = ERR44; +#elif defined COMPILE_PCRE16 + errorcode = ERR74; +#elif defined COMPILE_PCRE32 + errorcode = ERR77; +#endif + goto PCRE_EARLY_ERROR_RETURN2; + } +#else +if (utf) + { + errorcode = ERR32; + goto PCRE_EARLY_ERROR_RETURN; + } +#endif + +/* Can't support UCP unless PCRE has been compiled to include the code. */ + +#ifndef SUPPORT_UCP +if ((options & PCRE_UCP) != 0) + { + errorcode = ERR67; + goto PCRE_EARLY_ERROR_RETURN; + } +#endif + +/* Check validity of \R options. */ + +if ((options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) == + (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) + { + errorcode = ERR56; + goto PCRE_EARLY_ERROR_RETURN; + } + +/* Handle different types of newline. The three bits give seven cases. The +current code allows for fixed one- or two-byte sequences, plus "any" and +"anycrlf". */ + +switch (options & PCRE_NEWLINE_BITS) + { + case 0: newline = NEWLINE; break; /* Build-time default */ + case PCRE_NEWLINE_CR: newline = CHAR_CR; break; + case PCRE_NEWLINE_LF: newline = CHAR_NL; break; + case PCRE_NEWLINE_CR+ + PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break; + case PCRE_NEWLINE_ANY: newline = -1; break; + case PCRE_NEWLINE_ANYCRLF: newline = -2; break; + default: errorcode = ERR56; goto PCRE_EARLY_ERROR_RETURN; + } + +if (newline == -2) + { + cd->nltype = NLTYPE_ANYCRLF; + } +else if (newline < 0) + { + cd->nltype = NLTYPE_ANY; + } +else + { + cd->nltype = NLTYPE_FIXED; + if (newline > 255) + { + cd->nllen = 2; + cd->nl[0] = (newline >> 8) & 255; + cd->nl[1] = newline & 255; + } + else + { + cd->nllen = 1; + cd->nl[0] = newline; + } + } + +/* Maximum back reference and backref bitmap. The bitmap records up to 31 back +references to help in deciding whether (.*) can be treated as anchored or not. +*/ + +cd->top_backref = 0; +cd->backref_map = 0; + +/* Reflect pattern for debugging output */ + +DPRINTF(("------------------------------------------------------------------\n")); +#ifdef PCRE_DEBUG +print_puchar(stdout, (PCRE_PUCHAR)pattern); +#endif +DPRINTF(("\n")); + +/* Pretend to compile the pattern while actually just accumulating the length +of memory required. This behaviour is triggered by passing a non-NULL final +argument to compile_regex(). We pass a block of workspace (cworkspace) for it +to compile parts of the pattern into; the compiled code is discarded when it is +no longer needed, so hopefully this workspace will never overflow, though there +is a test for its doing so. */ + +cd->bracount = cd->final_bracount = 0; +cd->names_found = 0; +cd->name_entry_size = 0; +cd->name_table = NULL; +cd->dupnames = FALSE; +cd->dupgroups = FALSE; +cd->namedrefcount = 0; +cd->start_code = cworkspace; +cd->hwm = cworkspace; +cd->iscondassert = FALSE; +cd->start_workspace = cworkspace; +cd->workspace_size = COMPILE_WORK_SIZE; +cd->start_pattern = (const pcre_uchar *)pattern; +cd->end_pattern = (const pcre_uchar *)(pattern + STRLEN_UC((const pcre_uchar *)pattern)); +cd->req_varyopt = 0; +cd->parens_depth = 0; +cd->assert_depth = 0; +cd->max_lookbehind = 0; +cd->external_options = options; +cd->open_caps = NULL; + +/* Now do the pre-compile. On error, errorcode will be set non-zero, so we +don't need to look at the result of the function here. The initial options have +been put into the cd block so that they can be changed if an option setting is +found within the regex right at the beginning. Bringing initial option settings +outside can help speed up starting point checks. */ + +ptr += skipatstart; +code = cworkspace; +*code = OP_BRA; + +(void)compile_regex(cd->external_options, &code, &ptr, &errorcode, FALSE, + FALSE, 0, 0, &firstchar, &firstcharflags, &reqchar, &reqcharflags, NULL, + cd, &length); +if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN; + +DPRINTF(("end pre-compile: length=%d workspace=%d\n", length, + (int)(cd->hwm - cworkspace))); + +if (length > MAX_PATTERN_SIZE) + { + errorcode = ERR20; + goto PCRE_EARLY_ERROR_RETURN; + } + +/* Compute the size of the data block for storing the compiled pattern. Integer +overflow should no longer be possible because nowadays we limit the maximum +value of cd->names_found and cd->name_entry_size. */ + +size = sizeof(REAL_PCRE) + + (length + cd->names_found * cd->name_entry_size) * sizeof(pcre_uchar); + +/* Get the memory. */ + +re = (REAL_PCRE *)(PUBL(malloc))(size); +if (re == NULL) + { + errorcode = ERR21; + goto PCRE_EARLY_ERROR_RETURN; + } + +/* Put in the magic number, and save the sizes, initial options, internal +flags, and character table pointer. NULL is used for the default character +tables. The nullpad field is at the end; it's there to help in the case when a +regex compiled on a system with 4-byte pointers is run on another with 8-byte +pointers. */ + +re->magic_number = MAGIC_NUMBER; +re->size = (int)size; +re->options = cd->external_options; +re->flags = cd->external_flags; +re->limit_match = limit_match; +re->limit_recursion = limit_recursion; +re->first_char = 0; +re->req_char = 0; +re->name_table_offset = sizeof(REAL_PCRE) / sizeof(pcre_uchar); +re->name_entry_size = cd->name_entry_size; +re->name_count = cd->names_found; +re->ref_count = 0; +re->tables = (tables == PRIV(default_tables))? NULL : tables; +re->nullpad = NULL; +#ifdef COMPILE_PCRE32 +re->dummy = 0; +#else +re->dummy1 = re->dummy2 = re->dummy3 = 0; +#endif + +/* The starting points of the name/number translation table and of the code are +passed around in the compile data block. The start/end pattern and initial +options are already set from the pre-compile phase, as is the name_entry_size +field. Reset the bracket count and the names_found field. Also reset the hwm +field; this time it's used for remembering forward references to subpatterns. +*/ + +cd->final_bracount = cd->bracount; /* Save for checking forward references */ +cd->parens_depth = 0; +cd->assert_depth = 0; +cd->bracount = 0; +cd->max_lookbehind = 0; +cd->name_table = (pcre_uchar *)re + re->name_table_offset; +codestart = cd->name_table + re->name_entry_size * re->name_count; +cd->start_code = codestart; +cd->hwm = (pcre_uchar *)(cd->start_workspace); +cd->iscondassert = FALSE; +cd->req_varyopt = 0; +cd->had_accept = FALSE; +cd->had_pruneorskip = FALSE; +cd->check_lookbehind = FALSE; +cd->open_caps = NULL; + +/* If any named groups were found, create the name/number table from the list +created in the first pass. */ + +if (cd->names_found > 0) + { + int i = cd->names_found; + named_group *ng = cd->named_groups; + cd->names_found = 0; + for (; i > 0; i--, ng++) + add_name(cd, ng->name, ng->length, ng->number); + if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE) + (PUBL(free))((void *)cd->named_groups); + cd->named_group_list_size = 0; /* So we don't free it twice */ + } + +/* Set up a starting, non-extracting bracket, then compile the expression. On +error, errorcode will be set non-zero, so we don't need to look at the result +of the function here. */ + +ptr = (const pcre_uchar *)pattern + skipatstart; +code = (pcre_uchar *)codestart; +*code = OP_BRA; +(void)compile_regex(re->options, &code, &ptr, &errorcode, FALSE, FALSE, 0, 0, + &firstchar, &firstcharflags, &reqchar, &reqcharflags, NULL, cd, NULL); +re->top_bracket = cd->bracount; +re->top_backref = cd->top_backref; +re->max_lookbehind = cd->max_lookbehind; +re->flags = cd->external_flags | PCRE_MODE; + +if (cd->had_accept) + { + reqchar = 0; /* Must disable after (*ACCEPT) */ + reqcharflags = REQ_NONE; + } + +/* If not reached end of pattern on success, there's an excess bracket. */ + +if (errorcode == 0 && *ptr != CHAR_NULL) errorcode = ERR22; + +/* Fill in the terminating state and check for disastrous overflow, but +if debugging, leave the test till after things are printed out. */ + +*code++ = OP_END; + +#ifndef PCRE_DEBUG +if (code - codestart > length) errorcode = ERR23; +#endif + +#ifdef SUPPORT_VALGRIND +/* If the estimated length exceeds the really used length, mark the extra +allocated memory as unaddressable, so that any out-of-bound reads can be +detected. */ +VALGRIND_MAKE_MEM_NOACCESS(code, (length - (code - codestart)) * sizeof(pcre_uchar)); +#endif + +/* Fill in any forward references that are required. There may be repeated +references; optimize for them, as searching a large regex takes time. */ + +if (cd->hwm > cd->start_workspace) + { + int prev_recno = -1; + const pcre_uchar *groupptr = NULL; + while (errorcode == 0 && cd->hwm > cd->start_workspace) + { + int offset, recno; + cd->hwm -= LINK_SIZE; + offset = GET(cd->hwm, 0); + + /* Check that the hwm handling hasn't gone wrong. This whole area is + rewritten in PCRE2 because there are some obscure cases. */ + + if (offset == 0 || codestart[offset-1] != OP_RECURSE) + { + errorcode = ERR10; + break; + } + + recno = GET(codestart, offset); + if (recno != prev_recno) + { + groupptr = PRIV(find_bracket)(codestart, utf, recno); + prev_recno = recno; + } + if (groupptr == NULL) errorcode = ERR53; + else PUT(((pcre_uchar *)codestart), offset, (int)(groupptr - codestart)); + } + } + +/* If the workspace had to be expanded, free the new memory. Set the pointer to +NULL to indicate that forward references have been filled in. */ + +if (cd->workspace_size > COMPILE_WORK_SIZE) + (PUBL(free))((void *)cd->start_workspace); +cd->start_workspace = NULL; + +/* Give an error if there's back reference to a non-existent capturing +subpattern. */ + +if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15; + +/* Unless disabled, check whether any single character iterators can be +auto-possessified. The function overwrites the appropriate opcode values, so +the type of the pointer must be cast. NOTE: the intermediate variable "temp" is +used in this code because at least one compiler gives a warning about loss of +"const" attribute if the cast (pcre_uchar *)codestart is used directly in the +function call. */ + +if (errorcode == 0 && (options & PCRE_NO_AUTO_POSSESS) == 0) + { + pcre_uchar *temp = (pcre_uchar *)codestart; + auto_possessify(temp, utf, cd); + } + +/* If there were any lookbehind assertions that contained OP_RECURSE +(recursions or subroutine calls), a flag is set for them to be checked here, +because they may contain forward references. Actual recursions cannot be fixed +length, but subroutine calls can. It is done like this so that those without +OP_RECURSE that are not fixed length get a diagnosic with a useful offset. The +exceptional ones forgo this. We scan the pattern to check that they are fixed +length, and set their lengths. */ + +if (errorcode == 0 && cd->check_lookbehind) + { + pcre_uchar *cc = (pcre_uchar *)codestart; + + /* Loop, searching for OP_REVERSE items, and process those that do not have + their length set. (Actually, it will also re-process any that have a length + of zero, but that is a pathological case, and it does no harm.) When we find + one, we temporarily terminate the branch it is in while we scan it. */ + + for (cc = (pcre_uchar *)PRIV(find_bracket)(codestart, utf, -1); + cc != NULL; + cc = (pcre_uchar *)PRIV(find_bracket)(cc, utf, -1)) + { + if (GET(cc, 1) == 0) + { + int fixed_length; + pcre_uchar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE); + int end_op = *be; + *be = OP_END; + fixed_length = find_fixedlength(cc, (re->options & PCRE_UTF8) != 0, TRUE, + cd, NULL); + *be = end_op; + DPRINTF(("fixed length = %d\n", fixed_length)); + if (fixed_length < 0) + { + errorcode = (fixed_length == -2)? ERR36 : + (fixed_length == -4)? ERR70 : ERR25; + break; + } + if (fixed_length > cd->max_lookbehind) cd->max_lookbehind = fixed_length; + PUT(cc, 1, fixed_length); + } + cc += 1 + LINK_SIZE; + } + } + +/* Failed to compile, or error while post-processing */ + +if (errorcode != 0) + { + (PUBL(free))(re); + PCRE_EARLY_ERROR_RETURN: + if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE) + (PUBL(free))((void *)cd->named_groups); + *erroroffset = (int)(ptr - (const pcre_uchar *)pattern); + PCRE_EARLY_ERROR_RETURN2: + *errorptr = find_error_text(errorcode); + if (errorcodeptr != NULL) *errorcodeptr = errorcode; + return NULL; + } + +/* If the anchored option was not passed, set the flag if we can determine that +the pattern is anchored by virtue of ^ characters or \A or anything else, such +as starting with non-atomic .* when DOTALL is set and there are no occurrences +of *PRUNE or *SKIP. + +Otherwise, if we know what the first byte has to be, save it, because that +speeds up unanchored matches no end. If not, see if we can set the +PCRE_STARTLINE flag. This is helpful for multiline matches when all branches +start with ^. and also when all branches start with non-atomic .* for +non-DOTALL matches when *PRUNE and SKIP are not present. */ + +if ((re->options & PCRE_ANCHORED) == 0) + { + if (is_anchored(codestart, 0, cd, 0)) re->options |= PCRE_ANCHORED; + else + { + if (firstcharflags < 0) + firstchar = find_firstassertedchar(codestart, &firstcharflags, FALSE); + if (firstcharflags >= 0) /* Remove caseless flag for non-caseable chars */ + { +#if defined COMPILE_PCRE8 + re->first_char = firstchar & 0xff; +#elif defined COMPILE_PCRE16 + re->first_char = firstchar & 0xffff; +#elif defined COMPILE_PCRE32 + re->first_char = firstchar; +#endif + if ((firstcharflags & REQ_CASELESS) != 0) + { +#if defined SUPPORT_UCP && !(defined COMPILE_PCRE8) + /* We ignore non-ASCII first chars in 8 bit mode. */ + if (utf) + { + if (re->first_char < 128) + { + if (cd->fcc[re->first_char] != re->first_char) + re->flags |= PCRE_FCH_CASELESS; + } + else if (UCD_OTHERCASE(re->first_char) != re->first_char) + re->flags |= PCRE_FCH_CASELESS; + } + else +#endif + if (MAX_255(re->first_char) + && cd->fcc[re->first_char] != re->first_char) + re->flags |= PCRE_FCH_CASELESS; + } + + re->flags |= PCRE_FIRSTSET; + } + + else if (is_startline(codestart, 0, cd, 0, FALSE)) re->flags |= PCRE_STARTLINE; + } + } + +/* For an anchored pattern, we use the "required byte" only if it follows a +variable length item in the regex. Remove the caseless flag for non-caseable +bytes. */ + +if (reqcharflags >= 0 && + ((re->options & PCRE_ANCHORED) == 0 || (reqcharflags & REQ_VARY) != 0)) + { +#if defined COMPILE_PCRE8 + re->req_char = reqchar & 0xff; +#elif defined COMPILE_PCRE16 + re->req_char = reqchar & 0xffff; +#elif defined COMPILE_PCRE32 + re->req_char = reqchar; +#endif + if ((reqcharflags & REQ_CASELESS) != 0) + { +#if defined SUPPORT_UCP && !(defined COMPILE_PCRE8) + /* We ignore non-ASCII first chars in 8 bit mode. */ + if (utf) + { + if (re->req_char < 128) + { + if (cd->fcc[re->req_char] != re->req_char) + re->flags |= PCRE_RCH_CASELESS; + } + else if (UCD_OTHERCASE(re->req_char) != re->req_char) + re->flags |= PCRE_RCH_CASELESS; + } + else +#endif + if (MAX_255(re->req_char) && cd->fcc[re->req_char] != re->req_char) + re->flags |= PCRE_RCH_CASELESS; + } + + re->flags |= PCRE_REQCHSET; + } + +/* Print out the compiled data if debugging is enabled. This is never the +case when building a production library. */ + +#ifdef PCRE_DEBUG +printf("Length = %d top_bracket = %d top_backref = %d\n", + length, re->top_bracket, re->top_backref); + +printf("Options=%08x\n", re->options); + +if ((re->flags & PCRE_FIRSTSET) != 0) + { + pcre_uchar ch = re->first_char; + const char *caseless = + ((re->flags & PCRE_FCH_CASELESS) == 0)? "" : " (caseless)"; + if (PRINTABLE(ch)) printf("First char = %c%s\n", ch, caseless); + else printf("First char = \\x%02x%s\n", ch, caseless); + } + +if ((re->flags & PCRE_REQCHSET) != 0) + { + pcre_uchar ch = re->req_char; + const char *caseless = + ((re->flags & PCRE_RCH_CASELESS) == 0)? "" : " (caseless)"; + if (PRINTABLE(ch)) printf("Req char = %c%s\n", ch, caseless); + else printf("Req char = \\x%02x%s\n", ch, caseless); + } + +#if defined COMPILE_PCRE8 +pcre_printint((pcre *)re, stdout, TRUE); +#elif defined COMPILE_PCRE16 +pcre16_printint((pcre *)re, stdout, TRUE); +#elif defined COMPILE_PCRE32 +pcre32_printint((pcre *)re, stdout, TRUE); +#endif + +/* This check is done here in the debugging case so that the code that +was compiled can be seen. */ + +if (code - codestart > length) + { + (PUBL(free))(re); + *errorptr = find_error_text(ERR23); + *erroroffset = ptr - (pcre_uchar *)pattern; + if (errorcodeptr != NULL) *errorcodeptr = ERR23; + return NULL; + } +#endif /* PCRE_DEBUG */ + +/* Check for a pattern than can match an empty string, so that this information +can be provided to applications. */ + +do + { + if (could_be_empty_branch(codestart, code, utf, cd, NULL)) + { + re->flags |= PCRE_MATCH_EMPTY; + break; + } + codestart += GET(codestart, 1); + } +while (*codestart == OP_ALT); + +#if defined COMPILE_PCRE8 +return (pcre *)re; +#elif defined COMPILE_PCRE16 +return (pcre16 *)re; +#elif defined COMPILE_PCRE32 +return (pcre32 *)re; +#endif +} + +/* End of pcre_compile.c */ diff --git a/deps/pcre/pcre_config.c b/deps/pcre/pcre_config.c new file mode 100644 index 00000000000..1cbdd9c960c --- /dev/null +++ b/deps/pcre/pcre_config.c @@ -0,0 +1,190 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2012 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains the external function pcre_config(). */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +/* Keep the original link size. */ +static int real_link_size = LINK_SIZE; + +#include "pcre_internal.h" + + +/************************************************* +* Return info about what features are configured * +*************************************************/ + +/* This function has an extensible interface so that additional items can be +added compatibly. + +Arguments: + what what information is required + where where to put the information + +Returns: 0 if data returned, negative on error +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_config(int what, void *where) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre16_config(int what, void *where) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre32_config(int what, void *where) +#endif +{ +switch (what) + { + case PCRE_CONFIG_UTF8: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + *((int *)where) = 0; + return PCRE_ERROR_BADOPTION; +#else +#if defined SUPPORT_UTF + *((int *)where) = 1; +#else + *((int *)where) = 0; +#endif + break; +#endif + + case PCRE_CONFIG_UTF16: +#if defined COMPILE_PCRE8 || defined COMPILE_PCRE32 + *((int *)where) = 0; + return PCRE_ERROR_BADOPTION; +#else +#if defined SUPPORT_UTF + *((int *)where) = 1; +#else + *((int *)where) = 0; +#endif + break; +#endif + + case PCRE_CONFIG_UTF32: +#if defined COMPILE_PCRE8 || defined COMPILE_PCRE16 + *((int *)where) = 0; + return PCRE_ERROR_BADOPTION; +#else +#if defined SUPPORT_UTF + *((int *)where) = 1; +#else + *((int *)where) = 0; +#endif + break; +#endif + + case PCRE_CONFIG_UNICODE_PROPERTIES: +#ifdef SUPPORT_UCP + *((int *)where) = 1; +#else + *((int *)where) = 0; +#endif + break; + + case PCRE_CONFIG_JIT: +#ifdef SUPPORT_JIT + *((int *)where) = 1; +#else + *((int *)where) = 0; +#endif + break; + + case PCRE_CONFIG_JITTARGET: +#ifdef SUPPORT_JIT + *((const char **)where) = PRIV(jit_get_target)(); +#else + *((const char **)where) = NULL; +#endif + break; + + case PCRE_CONFIG_NEWLINE: + *((int *)where) = NEWLINE; + break; + + case PCRE_CONFIG_BSR: +#ifdef BSR_ANYCRLF + *((int *)where) = 1; +#else + *((int *)where) = 0; +#endif + break; + + case PCRE_CONFIG_LINK_SIZE: + *((int *)where) = real_link_size; + break; + + case PCRE_CONFIG_POSIX_MALLOC_THRESHOLD: + *((int *)where) = POSIX_MALLOC_THRESHOLD; + break; + + case PCRE_CONFIG_PARENS_LIMIT: + *((unsigned long int *)where) = PARENS_NEST_LIMIT; + break; + + case PCRE_CONFIG_MATCH_LIMIT: + *((unsigned long int *)where) = MATCH_LIMIT; + break; + + case PCRE_CONFIG_MATCH_LIMIT_RECURSION: + *((unsigned long int *)where) = MATCH_LIMIT_RECURSION; + break; + + case PCRE_CONFIG_STACKRECURSE: +#ifdef NO_RECURSE + *((int *)where) = 0; +#else + *((int *)where) = 1; +#endif + break; + + default: return PCRE_ERROR_BADOPTION; + } + +return 0; +} + +/* End of pcre_config.c */ diff --git a/deps/pcre/pcre_dfa_exec.c b/deps/pcre/pcre_dfa_exec.c new file mode 100644 index 00000000000..f333381d088 --- /dev/null +++ b/deps/pcre/pcre_dfa_exec.c @@ -0,0 +1,3676 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language (but see +below for why this module is different). + + Written by Philip Hazel + Copyright (c) 1997-2017 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + +/* This module contains the external function pcre_dfa_exec(), which is an +alternative matching function that uses a sort of DFA algorithm (not a true +FSM). This is NOT Perl-compatible, but it has advantages in certain +applications. */ + + +/* NOTE ABOUT PERFORMANCE: A user of this function sent some code that improved +the performance of his patterns greatly. I could not use it as it stood, as it +was not thread safe, and made assumptions about pattern sizes. Also, it caused +test 7 to loop, and test 9 to crash with a segfault. + +The issue is the check for duplicate states, which is done by a simple linear +search up the state list. (Grep for "duplicate" below to find the code.) For +many patterns, there will never be many states active at one time, so a simple +linear search is fine. In patterns that have many active states, it might be a +bottleneck. The suggested code used an indexing scheme to remember which states +had previously been used for each character, and avoided the linear search when +it knew there was no chance of a duplicate. This was implemented when adding +states to the state lists. + +I wrote some thread-safe, not-limited code to try something similar at the time +of checking for duplicates (instead of when adding states), using index vectors +on the stack. It did give a 13% improvement with one specially constructed +pattern for certain subject strings, but on other strings and on many of the +simpler patterns in the test suite it did worse. The major problem, I think, +was the extra time to initialize the index. This had to be done for each call +of internal_dfa_exec(). (The supplied patch used a static vector, initialized +only once - I suspect this was the cause of the problems with the tests.) + +Overall, I concluded that the gains in some cases did not outweigh the losses +in others, so I abandoned this code. */ + + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#define NLBLOCK md /* Block containing newline information */ +#define PSSTART start_subject /* Field containing processed string start */ +#define PSEND end_subject /* Field containing processed string end */ + +#include "pcre_internal.h" + + +/* For use to indent debugging output */ + +#define SP " " + + +/************************************************* +* Code parameters and static tables * +*************************************************/ + +/* These are offsets that are used to turn the OP_TYPESTAR and friends opcodes +into others, under special conditions. A gap of 20 between the blocks should be +enough. The resulting opcodes don't have to be less than 256 because they are +never stored, so we push them well clear of the normal opcodes. */ + +#define OP_PROP_EXTRA 300 +#define OP_EXTUNI_EXTRA 320 +#define OP_ANYNL_EXTRA 340 +#define OP_HSPACE_EXTRA 360 +#define OP_VSPACE_EXTRA 380 + + +/* This table identifies those opcodes that are followed immediately by a +character that is to be tested in some way. This makes it possible to +centralize the loading of these characters. In the case of Type * etc, the +"character" is the opcode for \D, \d, \S, \s, \W, or \w, which will always be a +small value. Non-zero values in the table are the offsets from the opcode where +the character is to be found. ***NOTE*** If the start of this table is +modified, the three tables that follow must also be modified. */ + +static const pcre_uint8 coptable[] = { + 0, /* End */ + 0, 0, 0, 0, 0, /* \A, \G, \K, \B, \b */ + 0, 0, 0, 0, 0, 0, /* \D, \d, \S, \s, \W, \w */ + 0, 0, 0, /* Any, AllAny, Anybyte */ + 0, 0, /* \P, \p */ + 0, 0, 0, 0, 0, /* \R, \H, \h, \V, \v */ + 0, /* \X */ + 0, 0, 0, 0, 0, 0, /* \Z, \z, $, $M, ^, ^M */ + 1, /* Char */ + 1, /* Chari */ + 1, /* not */ + 1, /* noti */ + /* Positive single-char repeats */ + 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */ + 1+IMM2_SIZE, 1+IMM2_SIZE, /* upto, minupto */ + 1+IMM2_SIZE, /* exact */ + 1, 1, 1, 1+IMM2_SIZE, /* *+, ++, ?+, upto+ */ + 1, 1, 1, 1, 1, 1, /* *I, *?I, +I, +?I, ?I, ??I */ + 1+IMM2_SIZE, 1+IMM2_SIZE, /* upto I, minupto I */ + 1+IMM2_SIZE, /* exact I */ + 1, 1, 1, 1+IMM2_SIZE, /* *+I, ++I, ?+I, upto+I */ + /* Negative single-char repeats - only for chars < 256 */ + 1, 1, 1, 1, 1, 1, /* NOT *, *?, +, +?, ?, ?? */ + 1+IMM2_SIZE, 1+IMM2_SIZE, /* NOT upto, minupto */ + 1+IMM2_SIZE, /* NOT exact */ + 1, 1, 1, 1+IMM2_SIZE, /* NOT *+, ++, ?+, upto+ */ + 1, 1, 1, 1, 1, 1, /* NOT *I, *?I, +I, +?I, ?I, ??I */ + 1+IMM2_SIZE, 1+IMM2_SIZE, /* NOT upto I, minupto I */ + 1+IMM2_SIZE, /* NOT exact I */ + 1, 1, 1, 1+IMM2_SIZE, /* NOT *+I, ++I, ?+I, upto+I */ + /* Positive type repeats */ + 1, 1, 1, 1, 1, 1, /* Type *, *?, +, +?, ?, ?? */ + 1+IMM2_SIZE, 1+IMM2_SIZE, /* Type upto, minupto */ + 1+IMM2_SIZE, /* Type exact */ + 1, 1, 1, 1+IMM2_SIZE, /* Type *+, ++, ?+, upto+ */ + /* Character class & ref repeats */ + 0, 0, 0, 0, 0, 0, /* *, *?, +, +?, ?, ?? */ + 0, 0, /* CRRANGE, CRMINRANGE */ + 0, 0, 0, 0, /* Possessive *+, ++, ?+, CRPOSRANGE */ + 0, /* CLASS */ + 0, /* NCLASS */ + 0, /* XCLASS - variable length */ + 0, /* REF */ + 0, /* REFI */ + 0, /* DNREF */ + 0, /* DNREFI */ + 0, /* RECURSE */ + 0, /* CALLOUT */ + 0, /* Alt */ + 0, /* Ket */ + 0, /* KetRmax */ + 0, /* KetRmin */ + 0, /* KetRpos */ + 0, /* Reverse */ + 0, /* Assert */ + 0, /* Assert not */ + 0, /* Assert behind */ + 0, /* Assert behind not */ + 0, 0, /* ONCE, ONCE_NC */ + 0, 0, 0, 0, 0, /* BRA, BRAPOS, CBRA, CBRAPOS, COND */ + 0, 0, 0, 0, 0, /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND */ + 0, 0, /* CREF, DNCREF */ + 0, 0, /* RREF, DNRREF */ + 0, /* DEF */ + 0, 0, 0, /* BRAZERO, BRAMINZERO, BRAPOSZERO */ + 0, 0, 0, /* MARK, PRUNE, PRUNE_ARG */ + 0, 0, 0, 0, /* SKIP, SKIP_ARG, THEN, THEN_ARG */ + 0, 0, 0, 0, /* COMMIT, FAIL, ACCEPT, ASSERT_ACCEPT */ + 0, 0 /* CLOSE, SKIPZERO */ +}; + +/* This table identifies those opcodes that inspect a character. It is used to +remember the fact that a character could have been inspected when the end of +the subject is reached. ***NOTE*** If the start of this table is modified, the +two tables that follow must also be modified. */ + +static const pcre_uint8 poptable[] = { + 0, /* End */ + 0, 0, 0, 1, 1, /* \A, \G, \K, \B, \b */ + 1, 1, 1, 1, 1, 1, /* \D, \d, \S, \s, \W, \w */ + 1, 1, 1, /* Any, AllAny, Anybyte */ + 1, 1, /* \P, \p */ + 1, 1, 1, 1, 1, /* \R, \H, \h, \V, \v */ + 1, /* \X */ + 0, 0, 0, 0, 0, 0, /* \Z, \z, $, $M, ^, ^M */ + 1, /* Char */ + 1, /* Chari */ + 1, /* not */ + 1, /* noti */ + /* Positive single-char repeats */ + 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */ + 1, 1, 1, /* upto, minupto, exact */ + 1, 1, 1, 1, /* *+, ++, ?+, upto+ */ + 1, 1, 1, 1, 1, 1, /* *I, *?I, +I, +?I, ?I, ??I */ + 1, 1, 1, /* upto I, minupto I, exact I */ + 1, 1, 1, 1, /* *+I, ++I, ?+I, upto+I */ + /* Negative single-char repeats - only for chars < 256 */ + 1, 1, 1, 1, 1, 1, /* NOT *, *?, +, +?, ?, ?? */ + 1, 1, 1, /* NOT upto, minupto, exact */ + 1, 1, 1, 1, /* NOT *+, ++, ?+, upto+ */ + 1, 1, 1, 1, 1, 1, /* NOT *I, *?I, +I, +?I, ?I, ??I */ + 1, 1, 1, /* NOT upto I, minupto I, exact I */ + 1, 1, 1, 1, /* NOT *+I, ++I, ?+I, upto+I */ + /* Positive type repeats */ + 1, 1, 1, 1, 1, 1, /* Type *, *?, +, +?, ?, ?? */ + 1, 1, 1, /* Type upto, minupto, exact */ + 1, 1, 1, 1, /* Type *+, ++, ?+, upto+ */ + /* Character class & ref repeats */ + 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */ + 1, 1, /* CRRANGE, CRMINRANGE */ + 1, 1, 1, 1, /* Possessive *+, ++, ?+, CRPOSRANGE */ + 1, /* CLASS */ + 1, /* NCLASS */ + 1, /* XCLASS - variable length */ + 0, /* REF */ + 0, /* REFI */ + 0, /* DNREF */ + 0, /* DNREFI */ + 0, /* RECURSE */ + 0, /* CALLOUT */ + 0, /* Alt */ + 0, /* Ket */ + 0, /* KetRmax */ + 0, /* KetRmin */ + 0, /* KetRpos */ + 0, /* Reverse */ + 0, /* Assert */ + 0, /* Assert not */ + 0, /* Assert behind */ + 0, /* Assert behind not */ + 0, 0, /* ONCE, ONCE_NC */ + 0, 0, 0, 0, 0, /* BRA, BRAPOS, CBRA, CBRAPOS, COND */ + 0, 0, 0, 0, 0, /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND */ + 0, 0, /* CREF, DNCREF */ + 0, 0, /* RREF, DNRREF */ + 0, /* DEF */ + 0, 0, 0, /* BRAZERO, BRAMINZERO, BRAPOSZERO */ + 0, 0, 0, /* MARK, PRUNE, PRUNE_ARG */ + 0, 0, 0, 0, /* SKIP, SKIP_ARG, THEN, THEN_ARG */ + 0, 0, 0, 0, /* COMMIT, FAIL, ACCEPT, ASSERT_ACCEPT */ + 0, 0 /* CLOSE, SKIPZERO */ +}; + +/* These 2 tables allow for compact code for testing for \D, \d, \S, \s, \W, +and \w */ + +static const pcre_uint8 toptable1[] = { + 0, 0, 0, 0, 0, 0, + ctype_digit, ctype_digit, + ctype_space, ctype_space, + ctype_word, ctype_word, + 0, 0 /* OP_ANY, OP_ALLANY */ +}; + +static const pcre_uint8 toptable2[] = { + 0, 0, 0, 0, 0, 0, + ctype_digit, 0, + ctype_space, 0, + ctype_word, 0, + 1, 1 /* OP_ANY, OP_ALLANY */ +}; + + +/* Structure for holding data about a particular state, which is in effect the +current data for an active path through the match tree. It must consist +entirely of ints because the working vector we are passed, and which we put +these structures in, is a vector of ints. */ + +typedef struct stateblock { + int offset; /* Offset to opcode */ + int count; /* Count for repeats */ + int data; /* Some use extra data */ +} stateblock; + +#define INTS_PER_STATEBLOCK (int)(sizeof(stateblock)/sizeof(int)) + + +#ifdef PCRE_DEBUG +/************************************************* +* Print character string * +*************************************************/ + +/* Character string printing function for debugging. + +Arguments: + p points to string + length number of bytes + f where to print + +Returns: nothing +*/ + +static void +pchars(const pcre_uchar *p, int length, FILE *f) +{ +pcre_uint32 c; +while (length-- > 0) + { + if (isprint(c = *(p++))) + fprintf(f, "%c", c); + else + fprintf(f, "\\x{%02x}", c); + } +} +#endif + + + +/************************************************* +* Execute a Regular Expression - DFA engine * +*************************************************/ + +/* This internal function applies a compiled pattern to a subject string, +starting at a given point, using a DFA engine. This function is called from the +external one, possibly multiple times if the pattern is not anchored. The +function calls itself recursively for some kinds of subpattern. + +Arguments: + md the match_data block with fixed information + this_start_code the opening bracket of this subexpression's code + current_subject where we currently are in the subject string + start_offset start offset in the subject string + offsets vector to contain the matching string offsets + offsetcount size of same + workspace vector of workspace + wscount size of same + rlevel function call recursion level + +Returns: > 0 => number of match offset pairs placed in offsets + = 0 => offsets overflowed; longest matches are present + -1 => failed to match + < -1 => some kind of unexpected problem + +The following macros are used for adding states to the two state vectors (one +for the current character, one for the following character). */ + +#define ADD_ACTIVE(x,y) \ + if (active_count++ < wscount) \ + { \ + next_active_state->offset = (x); \ + next_active_state->count = (y); \ + next_active_state++; \ + DPRINTF(("%.*sADD_ACTIVE(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \ + } \ + else return PCRE_ERROR_DFA_WSSIZE + +#define ADD_ACTIVE_DATA(x,y,z) \ + if (active_count++ < wscount) \ + { \ + next_active_state->offset = (x); \ + next_active_state->count = (y); \ + next_active_state->data = (z); \ + next_active_state++; \ + DPRINTF(("%.*sADD_ACTIVE_DATA(%d,%d,%d)\n", rlevel*2-2, SP, (x), (y), (z))); \ + } \ + else return PCRE_ERROR_DFA_WSSIZE + +#define ADD_NEW(x,y) \ + if (new_count++ < wscount) \ + { \ + next_new_state->offset = (x); \ + next_new_state->count = (y); \ + next_new_state++; \ + DPRINTF(("%.*sADD_NEW(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \ + } \ + else return PCRE_ERROR_DFA_WSSIZE + +#define ADD_NEW_DATA(x,y,z) \ + if (new_count++ < wscount) \ + { \ + next_new_state->offset = (x); \ + next_new_state->count = (y); \ + next_new_state->data = (z); \ + next_new_state++; \ + DPRINTF(("%.*sADD_NEW_DATA(%d,%d,%d) line %d\n", rlevel*2-2, SP, \ + (x), (y), (z), __LINE__)); \ + } \ + else return PCRE_ERROR_DFA_WSSIZE + +/* And now, here is the code */ + +static int +internal_dfa_exec( + dfa_match_data *md, + const pcre_uchar *this_start_code, + const pcre_uchar *current_subject, + int start_offset, + int *offsets, + int offsetcount, + int *workspace, + int wscount, + int rlevel) +{ +stateblock *active_states, *new_states, *temp_states; +stateblock *next_active_state, *next_new_state; + +const pcre_uint8 *ctypes, *lcc, *fcc; +const pcre_uchar *ptr; +const pcre_uchar *end_code, *first_op; + +dfa_recursion_info new_recursive; + +int active_count, new_count, match_count; + +/* Some fields in the md block are frequently referenced, so we load them into +independent variables in the hope that this will perform better. */ + +const pcre_uchar *start_subject = md->start_subject; +const pcre_uchar *end_subject = md->end_subject; +const pcre_uchar *start_code = md->start_code; + +#ifdef SUPPORT_UTF +BOOL utf = (md->poptions & PCRE_UTF8) != 0; +#else +BOOL utf = FALSE; +#endif + +BOOL reset_could_continue = FALSE; + +rlevel++; +offsetcount &= (-2); + +wscount -= 2; +wscount = (wscount - (wscount % (INTS_PER_STATEBLOCK * 2))) / + (2 * INTS_PER_STATEBLOCK); + +DPRINTF(("\n%.*s---------------------\n" + "%.*sCall to internal_dfa_exec f=%d\n", + rlevel*2-2, SP, rlevel*2-2, SP, rlevel)); + +ctypes = md->tables + ctypes_offset; +lcc = md->tables + lcc_offset; +fcc = md->tables + fcc_offset; + +match_count = PCRE_ERROR_NOMATCH; /* A negative number */ + +active_states = (stateblock *)(workspace + 2); +next_new_state = new_states = active_states + wscount; +new_count = 0; + +first_op = this_start_code + 1 + LINK_SIZE + + ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA || + *this_start_code == OP_CBRAPOS || *this_start_code == OP_SCBRAPOS) + ? IMM2_SIZE:0); + +/* The first thing in any (sub) pattern is a bracket of some sort. Push all +the alternative states onto the list, and find out where the end is. This +makes is possible to use this function recursively, when we want to stop at a +matching internal ket rather than at the end. + +If the first opcode in the first alternative is OP_REVERSE, we are dealing with +a backward assertion. In that case, we have to find out the maximum amount to +move back, and set up each alternative appropriately. */ + +if (*first_op == OP_REVERSE) + { + int max_back = 0; + int gone_back; + + end_code = this_start_code; + do + { + int back = GET(end_code, 2+LINK_SIZE); + if (back > max_back) max_back = back; + end_code += GET(end_code, 1); + } + while (*end_code == OP_ALT); + + /* If we can't go back the amount required for the longest lookbehind + pattern, go back as far as we can; some alternatives may still be viable. */ + +#ifdef SUPPORT_UTF + /* In character mode we have to step back character by character */ + + if (utf) + { + for (gone_back = 0; gone_back < max_back; gone_back++) + { + if (current_subject <= start_subject) break; + current_subject--; + ACROSSCHAR(current_subject > start_subject, *current_subject, current_subject--); + } + } + else +#endif + + /* In byte-mode we can do this quickly. */ + + { + gone_back = (current_subject - max_back < start_subject)? + (int)(current_subject - start_subject) : max_back; + current_subject -= gone_back; + } + + /* Save the earliest consulted character */ + + if (current_subject < md->start_used_ptr) + md->start_used_ptr = current_subject; + + /* Now we can process the individual branches. */ + + end_code = this_start_code; + do + { + int back = GET(end_code, 2+LINK_SIZE); + if (back <= gone_back) + { + int bstate = (int)(end_code - start_code + 2 + 2*LINK_SIZE); + ADD_NEW_DATA(-bstate, 0, gone_back - back); + } + end_code += GET(end_code, 1); + } + while (*end_code == OP_ALT); + } + +/* This is the code for a "normal" subpattern (not a backward assertion). The +start of a whole pattern is always one of these. If we are at the top level, +we may be asked to restart matching from the same point that we reached for a +previous partial match. We still have to scan through the top-level branches to +find the end state. */ + +else + { + end_code = this_start_code; + + /* Restarting */ + + if (rlevel == 1 && (md->moptions & PCRE_DFA_RESTART) != 0) + { + do { end_code += GET(end_code, 1); } while (*end_code == OP_ALT); + new_count = workspace[1]; + if (!workspace[0]) + memcpy(new_states, active_states, new_count * sizeof(stateblock)); + } + + /* Not restarting */ + + else + { + int length = 1 + LINK_SIZE + + ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA || + *this_start_code == OP_CBRAPOS || *this_start_code == OP_SCBRAPOS) + ? IMM2_SIZE:0); + do + { + ADD_NEW((int)(end_code - start_code + length), 0); + end_code += GET(end_code, 1); + length = 1 + LINK_SIZE; + } + while (*end_code == OP_ALT); + } + } + +workspace[0] = 0; /* Bit indicating which vector is current */ + +DPRINTF(("%.*sEnd state = %d\n", rlevel*2-2, SP, (int)(end_code - start_code))); + +/* Loop for scanning the subject */ + +ptr = current_subject; +for (;;) + { + int i, j; + int clen, dlen; + pcre_uint32 c, d; + int forced_fail = 0; + BOOL partial_newline = FALSE; + BOOL could_continue = reset_could_continue; + reset_could_continue = FALSE; + + /* Make the new state list into the active state list and empty the + new state list. */ + + temp_states = active_states; + active_states = new_states; + new_states = temp_states; + active_count = new_count; + new_count = 0; + + workspace[0] ^= 1; /* Remember for the restarting feature */ + workspace[1] = active_count; + +#ifdef PCRE_DEBUG + printf("%.*sNext character: rest of subject = \"", rlevel*2-2, SP); + pchars(ptr, STRLEN_UC(ptr), stdout); + printf("\"\n"); + + printf("%.*sActive states: ", rlevel*2-2, SP); + for (i = 0; i < active_count; i++) + printf("%d/%d ", active_states[i].offset, active_states[i].count); + printf("\n"); +#endif + + /* Set the pointers for adding new states */ + + next_active_state = active_states + active_count; + next_new_state = new_states; + + /* Load the current character from the subject outside the loop, as many + different states may want to look at it, and we assume that at least one + will. */ + + if (ptr < end_subject) + { + clen = 1; /* Number of data items in the character */ +#ifdef SUPPORT_UTF + GETCHARLENTEST(c, ptr, clen); +#else + c = *ptr; +#endif /* SUPPORT_UTF */ + } + else + { + clen = 0; /* This indicates the end of the subject */ + c = NOTACHAR; /* This value should never actually be used */ + } + + /* Scan up the active states and act on each one. The result of an action + may be to add more states to the currently active list (e.g. on hitting a + parenthesis) or it may be to put states on the new list, for considering + when we move the character pointer on. */ + + for (i = 0; i < active_count; i++) + { + stateblock *current_state = active_states + i; + BOOL caseless = FALSE; + const pcre_uchar *code; + int state_offset = current_state->offset; + int codevalue, rrc; + int count; + +#ifdef PCRE_DEBUG + printf ("%.*sProcessing state %d c=", rlevel*2-2, SP, state_offset); + if (clen == 0) printf("EOL\n"); + else if (c > 32 && c < 127) printf("'%c'\n", c); + else printf("0x%02x\n", c); +#endif + + /* A negative offset is a special case meaning "hold off going to this + (negated) state until the number of characters in the data field have + been skipped". If the could_continue flag was passed over from a previous + state, arrange for it to passed on. */ + + if (state_offset < 0) + { + if (current_state->data > 0) + { + DPRINTF(("%.*sSkipping this character\n", rlevel*2-2, SP)); + ADD_NEW_DATA(state_offset, current_state->count, + current_state->data - 1); + if (could_continue) reset_could_continue = TRUE; + continue; + } + else + { + current_state->offset = state_offset = -state_offset; + } + } + + /* Check for a duplicate state with the same count, and skip if found. + See the note at the head of this module about the possibility of improving + performance here. */ + + for (j = 0; j < i; j++) + { + if (active_states[j].offset == state_offset && + active_states[j].count == current_state->count) + { + DPRINTF(("%.*sDuplicate state: skipped\n", rlevel*2-2, SP)); + goto NEXT_ACTIVE_STATE; + } + } + + /* The state offset is the offset to the opcode */ + + code = start_code + state_offset; + codevalue = *code; + + /* If this opcode inspects a character, but we are at the end of the + subject, remember the fact for use when testing for a partial match. */ + + if (clen == 0 && poptable[codevalue] != 0) + could_continue = TRUE; + + /* If this opcode is followed by an inline character, load it. It is + tempting to test for the presence of a subject character here, but that + is wrong, because sometimes zero repetitions of the subject are + permitted. + + We also use this mechanism for opcodes such as OP_TYPEPLUS that take an + argument that is not a data character - but is always one byte long because + the values are small. We have to take special action to deal with \P, \p, + \H, \h, \V, \v and \X in this case. To keep the other cases fast, convert + these ones to new opcodes. */ + + if (coptable[codevalue] > 0) + { + dlen = 1; +#ifdef SUPPORT_UTF + if (utf) { GETCHARLEN(d, (code + coptable[codevalue]), dlen); } else +#endif /* SUPPORT_UTF */ + d = code[coptable[codevalue]]; + if (codevalue >= OP_TYPESTAR) + { + switch(d) + { + case OP_ANYBYTE: return PCRE_ERROR_DFA_UITEM; + case OP_NOTPROP: + case OP_PROP: codevalue += OP_PROP_EXTRA; break; + case OP_ANYNL: codevalue += OP_ANYNL_EXTRA; break; + case OP_EXTUNI: codevalue += OP_EXTUNI_EXTRA; break; + case OP_NOT_HSPACE: + case OP_HSPACE: codevalue += OP_HSPACE_EXTRA; break; + case OP_NOT_VSPACE: + case OP_VSPACE: codevalue += OP_VSPACE_EXTRA; break; + default: break; + } + } + } + else + { + dlen = 0; /* Not strictly necessary, but compilers moan */ + d = NOTACHAR; /* if these variables are not set. */ + } + + + /* Now process the individual opcodes */ + + switch (codevalue) + { +/* ========================================================================== */ + /* These cases are never obeyed. This is a fudge that causes a compile- + time error if the vectors coptable or poptable, which are indexed by + opcode, are not the correct length. It seems to be the only way to do + such a check at compile time, as the sizeof() operator does not work + in the C preprocessor. */ + + case OP_TABLE_LENGTH: + case OP_TABLE_LENGTH + + ((sizeof(coptable) == OP_TABLE_LENGTH) && + (sizeof(poptable) == OP_TABLE_LENGTH)): + break; + +/* ========================================================================== */ + /* Reached a closing bracket. If not at the end of the pattern, carry + on with the next opcode. For repeating opcodes, also add the repeat + state. Note that KETRPOS will always be encountered at the end of the + subpattern, because the possessive subpattern repeats are always handled + using recursive calls. Thus, it never adds any new states. + + At the end of the (sub)pattern, unless we have an empty string and + PCRE_NOTEMPTY is set, or PCRE_NOTEMPTY_ATSTART is set and we are at the + start of the subject, save the match data, shifting up all previous + matches so we always have the longest first. */ + + case OP_KET: + case OP_KETRMIN: + case OP_KETRMAX: + case OP_KETRPOS: + if (code != end_code) + { + ADD_ACTIVE(state_offset + 1 + LINK_SIZE, 0); + if (codevalue != OP_KET) + { + ADD_ACTIVE(state_offset - GET(code, 1), 0); + } + } + else + { + if (ptr > current_subject || + ((md->moptions & PCRE_NOTEMPTY) == 0 && + ((md->moptions & PCRE_NOTEMPTY_ATSTART) == 0 || + current_subject > start_subject + md->start_offset))) + { + if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0; + else if (match_count > 0 && ++match_count * 2 > offsetcount) + match_count = 0; + count = ((match_count == 0)? offsetcount : match_count * 2) - 2; + if (count > 0) memmove(offsets + 2, offsets, count * sizeof(int)); + if (offsetcount >= 2) + { + offsets[0] = (int)(current_subject - start_subject); + offsets[1] = (int)(ptr - start_subject); + DPRINTF(("%.*sSet matched string = \"%.*s\"\n", rlevel*2-2, SP, + offsets[1] - offsets[0], (char *)current_subject)); + } + if ((md->moptions & PCRE_DFA_SHORTEST) != 0) + { + DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n" + "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel, + match_count, rlevel*2-2, SP)); + return match_count; + } + } + } + break; + +/* ========================================================================== */ + /* These opcodes add to the current list of states without looking + at the current character. */ + + /*-----------------------------------------------------------------*/ + case OP_ALT: + do { code += GET(code, 1); } while (*code == OP_ALT); + ADD_ACTIVE((int)(code - start_code), 0); + break; + + /*-----------------------------------------------------------------*/ + case OP_BRA: + case OP_SBRA: + do + { + ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0); + code += GET(code, 1); + } + while (*code == OP_ALT); + break; + + /*-----------------------------------------------------------------*/ + case OP_CBRA: + case OP_SCBRA: + ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE + IMM2_SIZE), 0); + code += GET(code, 1); + while (*code == OP_ALT) + { + ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0); + code += GET(code, 1); + } + break; + + /*-----------------------------------------------------------------*/ + case OP_BRAZERO: + case OP_BRAMINZERO: + ADD_ACTIVE(state_offset + 1, 0); + code += 1 + GET(code, 2); + while (*code == OP_ALT) code += GET(code, 1); + ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0); + break; + + /*-----------------------------------------------------------------*/ + case OP_SKIPZERO: + code += 1 + GET(code, 2); + while (*code == OP_ALT) code += GET(code, 1); + ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0); + break; + + /*-----------------------------------------------------------------*/ + case OP_CIRC: + if (ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0) + { ADD_ACTIVE(state_offset + 1, 0); } + break; + + /*-----------------------------------------------------------------*/ + case OP_CIRCM: + if ((ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0) || + (ptr != end_subject && WAS_NEWLINE(ptr))) + { ADD_ACTIVE(state_offset + 1, 0); } + break; + + /*-----------------------------------------------------------------*/ + case OP_EOD: + if (ptr >= end_subject) + { + if ((md->moptions & PCRE_PARTIAL_HARD) != 0) + could_continue = TRUE; + else { ADD_ACTIVE(state_offset + 1, 0); } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_SOD: + if (ptr == start_subject) { ADD_ACTIVE(state_offset + 1, 0); } + break; + + /*-----------------------------------------------------------------*/ + case OP_SOM: + if (ptr == start_subject + start_offset) { ADD_ACTIVE(state_offset + 1, 0); } + break; + + +/* ========================================================================== */ + /* These opcodes inspect the next subject character, and sometimes + the previous one as well, but do not have an argument. The variable + clen contains the length of the current character and is zero if we are + at the end of the subject. */ + + /*-----------------------------------------------------------------*/ + case OP_ANY: + if (clen > 0 && !IS_NEWLINE(ptr)) + { + if (ptr + 1 >= md->end_subject && + (md->moptions & (PCRE_PARTIAL_HARD)) != 0 && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + c == NLBLOCK->nl[0]) + { + could_continue = partial_newline = TRUE; + } + else + { + ADD_NEW(state_offset + 1, 0); + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_ALLANY: + if (clen > 0) + { ADD_NEW(state_offset + 1, 0); } + break; + + /*-----------------------------------------------------------------*/ + case OP_EODN: + if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0) + could_continue = TRUE; + else if (clen == 0 || (IS_NEWLINE(ptr) && ptr == end_subject - md->nllen)) + { ADD_ACTIVE(state_offset + 1, 0); } + break; + + /*-----------------------------------------------------------------*/ + case OP_DOLL: + if ((md->moptions & PCRE_NOTEOL) == 0) + { + if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0) + could_continue = TRUE; + else if (clen == 0 || + ((md->poptions & PCRE_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr) && + (ptr == end_subject - md->nllen) + )) + { ADD_ACTIVE(state_offset + 1, 0); } + else if (ptr + 1 >= md->end_subject && + (md->moptions & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT)) != 0 && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + c == NLBLOCK->nl[0]) + { + if ((md->moptions & PCRE_PARTIAL_HARD) != 0) + { + reset_could_continue = TRUE; + ADD_NEW_DATA(-(state_offset + 1), 0, 1); + } + else could_continue = partial_newline = TRUE; + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_DOLLM: + if ((md->moptions & PCRE_NOTEOL) == 0) + { + if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0) + could_continue = TRUE; + else if (clen == 0 || + ((md->poptions & PCRE_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr))) + { ADD_ACTIVE(state_offset + 1, 0); } + else if (ptr + 1 >= md->end_subject && + (md->moptions & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT)) != 0 && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + c == NLBLOCK->nl[0]) + { + if ((md->moptions & PCRE_PARTIAL_HARD) != 0) + { + reset_could_continue = TRUE; + ADD_NEW_DATA(-(state_offset + 1), 0, 1); + } + else could_continue = partial_newline = TRUE; + } + } + else if (IS_NEWLINE(ptr)) + { ADD_ACTIVE(state_offset + 1, 0); } + break; + + /*-----------------------------------------------------------------*/ + + case OP_DIGIT: + case OP_WHITESPACE: + case OP_WORDCHAR: + if (clen > 0 && c < 256 && + ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0) + { ADD_NEW(state_offset + 1, 0); } + break; + + /*-----------------------------------------------------------------*/ + case OP_NOT_DIGIT: + case OP_NOT_WHITESPACE: + case OP_NOT_WORDCHAR: + if (clen > 0 && (c >= 256 || + ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0)) + { ADD_NEW(state_offset + 1, 0); } + break; + + /*-----------------------------------------------------------------*/ + case OP_WORD_BOUNDARY: + case OP_NOT_WORD_BOUNDARY: + { + int left_word, right_word; + + if (ptr > start_subject) + { + const pcre_uchar *temp = ptr - 1; + if (temp < md->start_used_ptr) md->start_used_ptr = temp; +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf) { BACKCHAR(temp); } +#endif + GETCHARTEST(d, temp); +#ifdef SUPPORT_UCP + if ((md->poptions & PCRE_UCP) != 0) + { + if (d == '_') left_word = TRUE; else + { + int cat = UCD_CATEGORY(d); + left_word = (cat == ucp_L || cat == ucp_N); + } + } + else +#endif + left_word = d < 256 && (ctypes[d] & ctype_word) != 0; + } + else left_word = FALSE; + + if (clen > 0) + { +#ifdef SUPPORT_UCP + if ((md->poptions & PCRE_UCP) != 0) + { + if (c == '_') right_word = TRUE; else + { + int cat = UCD_CATEGORY(c); + right_word = (cat == ucp_L || cat == ucp_N); + } + } + else +#endif + right_word = c < 256 && (ctypes[c] & ctype_word) != 0; + } + else right_word = FALSE; + + if ((left_word == right_word) == (codevalue == OP_NOT_WORD_BOUNDARY)) + { ADD_ACTIVE(state_offset + 1, 0); } + } + break; + + + /*-----------------------------------------------------------------*/ + /* Check the next character by Unicode property. We will get here only + if the support is in the binary; otherwise a compile-time error occurs. + */ + +#ifdef SUPPORT_UCP + case OP_PROP: + case OP_NOTPROP: + if (clen > 0) + { + BOOL OK; + const pcre_uint32 *cp; + const ucd_record * prop = GET_UCD(c); + switch(code[1]) + { + case PT_ANY: + OK = TRUE; + break; + + case PT_LAMP: + OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll || + prop->chartype == ucp_Lt; + break; + + case PT_GC: + OK = PRIV(ucp_gentype)[prop->chartype] == code[2]; + break; + + case PT_PC: + OK = prop->chartype == code[2]; + break; + + case PT_SC: + OK = prop->script == code[2]; + break; + + /* These are specials for combination cases. */ + + case PT_ALNUM: + OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N; + break; + + /* Perl space used to exclude VT, but from Perl 5.18 it is included, + which means that Perl space and POSIX space are now identical. PCRE + was changed at release 8.34. */ + + case PT_SPACE: /* Perl space */ + case PT_PXSPACE: /* POSIX space */ + switch(c) + { + HSPACE_CASES: + VSPACE_CASES: + OK = TRUE; + break; + + default: + OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z; + break; + } + break; + + case PT_WORD: + OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N || + c == CHAR_UNDERSCORE; + break; + + case PT_CLIST: + cp = PRIV(ucd_caseless_sets) + code[2]; + for (;;) + { + if (c < *cp) { OK = FALSE; break; } + if (c == *cp++) { OK = TRUE; break; } + } + break; + + case PT_UCNC: + OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT || + c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) || + c >= 0xe000; + break; + + /* Should never occur, but keep compilers from grumbling. */ + + default: + OK = codevalue != OP_PROP; + break; + } + + if (OK == (codevalue == OP_PROP)) { ADD_NEW(state_offset + 3, 0); } + } + break; +#endif + + + +/* ========================================================================== */ + /* These opcodes likewise inspect the subject character, but have an + argument that is not a data character. It is one of these opcodes: + OP_ANY, OP_ALLANY, OP_DIGIT, OP_NOT_DIGIT, OP_WHITESPACE, OP_NOT_SPACE, + OP_WORDCHAR, OP_NOT_WORDCHAR. The value is loaded into d. */ + + case OP_TYPEPLUS: + case OP_TYPEMINPLUS: + case OP_TYPEPOSPLUS: + count = current_state->count; /* Already matched */ + if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); } + if (clen > 0) + { + if (d == OP_ANY && ptr + 1 >= md->end_subject && + (md->moptions & (PCRE_PARTIAL_HARD)) != 0 && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + c == NLBLOCK->nl[0]) + { + could_continue = partial_newline = TRUE; + } + else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || + (c < 256 && + (d != OP_ANY || !IS_NEWLINE(ptr)) && + ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) + { + if (count > 0 && codevalue == OP_TYPEPOSPLUS) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + count++; + ADD_NEW(state_offset, count); + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_TYPEQUERY: + case OP_TYPEMINQUERY: + case OP_TYPEPOSQUERY: + ADD_ACTIVE(state_offset + 2, 0); + if (clen > 0) + { + if (d == OP_ANY && ptr + 1 >= md->end_subject && + (md->moptions & (PCRE_PARTIAL_HARD)) != 0 && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + c == NLBLOCK->nl[0]) + { + could_continue = partial_newline = TRUE; + } + else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || + (c < 256 && + (d != OP_ANY || !IS_NEWLINE(ptr)) && + ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) + { + if (codevalue == OP_TYPEPOSQUERY) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + ADD_NEW(state_offset + 2, 0); + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_TYPESTAR: + case OP_TYPEMINSTAR: + case OP_TYPEPOSSTAR: + ADD_ACTIVE(state_offset + 2, 0); + if (clen > 0) + { + if (d == OP_ANY && ptr + 1 >= md->end_subject && + (md->moptions & (PCRE_PARTIAL_HARD)) != 0 && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + c == NLBLOCK->nl[0]) + { + could_continue = partial_newline = TRUE; + } + else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || + (c < 256 && + (d != OP_ANY || !IS_NEWLINE(ptr)) && + ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) + { + if (codevalue == OP_TYPEPOSSTAR) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + ADD_NEW(state_offset, 0); + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_TYPEEXACT: + count = current_state->count; /* Number already matched */ + if (clen > 0) + { + if (d == OP_ANY && ptr + 1 >= md->end_subject && + (md->moptions & (PCRE_PARTIAL_HARD)) != 0 && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + c == NLBLOCK->nl[0]) + { + could_continue = partial_newline = TRUE; + } + else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || + (c < 256 && + (d != OP_ANY || !IS_NEWLINE(ptr)) && + ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) + { + if (++count >= (int)GET2(code, 1)) + { ADD_NEW(state_offset + 1 + IMM2_SIZE + 1, 0); } + else + { ADD_NEW(state_offset, count); } + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_TYPEUPTO: + case OP_TYPEMINUPTO: + case OP_TYPEPOSUPTO: + ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); + count = current_state->count; /* Number already matched */ + if (clen > 0) + { + if (d == OP_ANY && ptr + 1 >= md->end_subject && + (md->moptions & (PCRE_PARTIAL_HARD)) != 0 && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + c == NLBLOCK->nl[0]) + { + could_continue = partial_newline = TRUE; + } + else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) || + (c < 256 && + (d != OP_ANY || !IS_NEWLINE(ptr)) && + ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0)) + { + if (codevalue == OP_TYPEPOSUPTO) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + if (++count >= (int)GET2(code, 1)) + { ADD_NEW(state_offset + 2 + IMM2_SIZE, 0); } + else + { ADD_NEW(state_offset, count); } + } + } + break; + +/* ========================================================================== */ + /* These are virtual opcodes that are used when something like + OP_TYPEPLUS has OP_PROP, OP_NOTPROP, OP_ANYNL, or OP_EXTUNI as its + argument. It keeps the code above fast for the other cases. The argument + is in the d variable. */ + +#ifdef SUPPORT_UCP + case OP_PROP_EXTRA + OP_TYPEPLUS: + case OP_PROP_EXTRA + OP_TYPEMINPLUS: + case OP_PROP_EXTRA + OP_TYPEPOSPLUS: + count = current_state->count; /* Already matched */ + if (count > 0) { ADD_ACTIVE(state_offset + 4, 0); } + if (clen > 0) + { + BOOL OK; + const pcre_uint32 *cp; + const ucd_record * prop = GET_UCD(c); + switch(code[2]) + { + case PT_ANY: + OK = TRUE; + break; + + case PT_LAMP: + OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll || + prop->chartype == ucp_Lt; + break; + + case PT_GC: + OK = PRIV(ucp_gentype)[prop->chartype] == code[3]; + break; + + case PT_PC: + OK = prop->chartype == code[3]; + break; + + case PT_SC: + OK = prop->script == code[3]; + break; + + /* These are specials for combination cases. */ + + case PT_ALNUM: + OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N; + break; + + /* Perl space used to exclude VT, but from Perl 5.18 it is included, + which means that Perl space and POSIX space are now identical. PCRE + was changed at release 8.34. */ + + case PT_SPACE: /* Perl space */ + case PT_PXSPACE: /* POSIX space */ + switch(c) + { + HSPACE_CASES: + VSPACE_CASES: + OK = TRUE; + break; + + default: + OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z; + break; + } + break; + + case PT_WORD: + OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N || + c == CHAR_UNDERSCORE; + break; + + case PT_CLIST: + cp = PRIV(ucd_caseless_sets) + code[3]; + for (;;) + { + if (c < *cp) { OK = FALSE; break; } + if (c == *cp++) { OK = TRUE; break; } + } + break; + + case PT_UCNC: + OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT || + c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) || + c >= 0xe000; + break; + + /* Should never occur, but keep compilers from grumbling. */ + + default: + OK = codevalue != OP_PROP; + break; + } + + if (OK == (d == OP_PROP)) + { + if (count > 0 && codevalue == OP_PROP_EXTRA + OP_TYPEPOSPLUS) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + count++; + ADD_NEW(state_offset, count); + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_EXTUNI_EXTRA + OP_TYPEPLUS: + case OP_EXTUNI_EXTRA + OP_TYPEMINPLUS: + case OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS: + count = current_state->count; /* Already matched */ + if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); } + if (clen > 0) + { + int lgb, rgb; + const pcre_uchar *nptr = ptr + clen; + int ncount = 0; + if (count > 0 && codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + lgb = UCD_GRAPHBREAK(c); + while (nptr < end_subject) + { + dlen = 1; + if (!utf) d = *nptr; else { GETCHARLEN(d, nptr, dlen); } + rgb = UCD_GRAPHBREAK(d); + if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break; + ncount++; + lgb = rgb; + nptr += dlen; + } + count++; + ADD_NEW_DATA(-state_offset, count, ncount); + } + break; +#endif + + /*-----------------------------------------------------------------*/ + case OP_ANYNL_EXTRA + OP_TYPEPLUS: + case OP_ANYNL_EXTRA + OP_TYPEMINPLUS: + case OP_ANYNL_EXTRA + OP_TYPEPOSPLUS: + count = current_state->count; /* Already matched */ + if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); } + if (clen > 0) + { + int ncount = 0; + switch (c) + { + case CHAR_VT: + case CHAR_FF: + case CHAR_NEL: +#ifndef EBCDIC + case 0x2028: + case 0x2029: +#endif /* Not EBCDIC */ + if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break; + goto ANYNL01; + + case CHAR_CR: + if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1; + /* Fall through */ + + ANYNL01: + case CHAR_LF: + if (count > 0 && codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSPLUS) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + count++; + ADD_NEW_DATA(-state_offset, count, ncount); + break; + + default: + break; + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_VSPACE_EXTRA + OP_TYPEPLUS: + case OP_VSPACE_EXTRA + OP_TYPEMINPLUS: + case OP_VSPACE_EXTRA + OP_TYPEPOSPLUS: + count = current_state->count; /* Already matched */ + if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); } + if (clen > 0) + { + BOOL OK; + switch (c) + { + VSPACE_CASES: + OK = TRUE; + break; + + default: + OK = FALSE; + break; + } + + if (OK == (d == OP_VSPACE)) + { + if (count > 0 && codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSPLUS) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + count++; + ADD_NEW_DATA(-state_offset, count, 0); + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_HSPACE_EXTRA + OP_TYPEPLUS: + case OP_HSPACE_EXTRA + OP_TYPEMINPLUS: + case OP_HSPACE_EXTRA + OP_TYPEPOSPLUS: + count = current_state->count; /* Already matched */ + if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); } + if (clen > 0) + { + BOOL OK; + switch (c) + { + HSPACE_CASES: + OK = TRUE; + break; + + default: + OK = FALSE; + break; + } + + if (OK == (d == OP_HSPACE)) + { + if (count > 0 && codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSPLUS) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + count++; + ADD_NEW_DATA(-state_offset, count, 0); + } + } + break; + + /*-----------------------------------------------------------------*/ +#ifdef SUPPORT_UCP + case OP_PROP_EXTRA + OP_TYPEQUERY: + case OP_PROP_EXTRA + OP_TYPEMINQUERY: + case OP_PROP_EXTRA + OP_TYPEPOSQUERY: + count = 4; + goto QS1; + + case OP_PROP_EXTRA + OP_TYPESTAR: + case OP_PROP_EXTRA + OP_TYPEMINSTAR: + case OP_PROP_EXTRA + OP_TYPEPOSSTAR: + count = 0; + + QS1: + + ADD_ACTIVE(state_offset + 4, 0); + if (clen > 0) + { + BOOL OK; + const pcre_uint32 *cp; + const ucd_record * prop = GET_UCD(c); + switch(code[2]) + { + case PT_ANY: + OK = TRUE; + break; + + case PT_LAMP: + OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll || + prop->chartype == ucp_Lt; + break; + + case PT_GC: + OK = PRIV(ucp_gentype)[prop->chartype] == code[3]; + break; + + case PT_PC: + OK = prop->chartype == code[3]; + break; + + case PT_SC: + OK = prop->script == code[3]; + break; + + /* These are specials for combination cases. */ + + case PT_ALNUM: + OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N; + break; + + /* Perl space used to exclude VT, but from Perl 5.18 it is included, + which means that Perl space and POSIX space are now identical. PCRE + was changed at release 8.34. */ + + case PT_SPACE: /* Perl space */ + case PT_PXSPACE: /* POSIX space */ + switch(c) + { + HSPACE_CASES: + VSPACE_CASES: + OK = TRUE; + break; + + default: + OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z; + break; + } + break; + + case PT_WORD: + OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N || + c == CHAR_UNDERSCORE; + break; + + case PT_CLIST: + cp = PRIV(ucd_caseless_sets) + code[3]; + for (;;) + { + if (c < *cp) { OK = FALSE; break; } + if (c == *cp++) { OK = TRUE; break; } + } + break; + + case PT_UCNC: + OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT || + c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) || + c >= 0xe000; + break; + + /* Should never occur, but keep compilers from grumbling. */ + + default: + OK = codevalue != OP_PROP; + break; + } + + if (OK == (d == OP_PROP)) + { + if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSSTAR || + codevalue == OP_PROP_EXTRA + OP_TYPEPOSQUERY) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + ADD_NEW(state_offset + count, 0); + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_EXTUNI_EXTRA + OP_TYPEQUERY: + case OP_EXTUNI_EXTRA + OP_TYPEMINQUERY: + case OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY: + count = 2; + goto QS2; + + case OP_EXTUNI_EXTRA + OP_TYPESTAR: + case OP_EXTUNI_EXTRA + OP_TYPEMINSTAR: + case OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR: + count = 0; + + QS2: + + ADD_ACTIVE(state_offset + 2, 0); + if (clen > 0) + { + int lgb, rgb; + const pcre_uchar *nptr = ptr + clen; + int ncount = 0; + if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR || + codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + lgb = UCD_GRAPHBREAK(c); + while (nptr < end_subject) + { + dlen = 1; + if (!utf) d = *nptr; else { GETCHARLEN(d, nptr, dlen); } + rgb = UCD_GRAPHBREAK(d); + if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break; + ncount++; + lgb = rgb; + nptr += dlen; + } + ADD_NEW_DATA(-(state_offset + count), 0, ncount); + } + break; +#endif + + /*-----------------------------------------------------------------*/ + case OP_ANYNL_EXTRA + OP_TYPEQUERY: + case OP_ANYNL_EXTRA + OP_TYPEMINQUERY: + case OP_ANYNL_EXTRA + OP_TYPEPOSQUERY: + count = 2; + goto QS3; + + case OP_ANYNL_EXTRA + OP_TYPESTAR: + case OP_ANYNL_EXTRA + OP_TYPEMINSTAR: + case OP_ANYNL_EXTRA + OP_TYPEPOSSTAR: + count = 0; + + QS3: + ADD_ACTIVE(state_offset + 2, 0); + if (clen > 0) + { + int ncount = 0; + switch (c) + { + case CHAR_VT: + case CHAR_FF: + case CHAR_NEL: +#ifndef EBCDIC + case 0x2028: + case 0x2029: +#endif /* Not EBCDIC */ + if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break; + goto ANYNL02; + + case CHAR_CR: + if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1; + /* Fall through */ + + ANYNL02: + case CHAR_LF: + if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSSTAR || + codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSQUERY) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + ADD_NEW_DATA(-(state_offset + (int)count), 0, ncount); + break; + + default: + break; + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_VSPACE_EXTRA + OP_TYPEQUERY: + case OP_VSPACE_EXTRA + OP_TYPEMINQUERY: + case OP_VSPACE_EXTRA + OP_TYPEPOSQUERY: + count = 2; + goto QS4; + + case OP_VSPACE_EXTRA + OP_TYPESTAR: + case OP_VSPACE_EXTRA + OP_TYPEMINSTAR: + case OP_VSPACE_EXTRA + OP_TYPEPOSSTAR: + count = 0; + + QS4: + ADD_ACTIVE(state_offset + 2, 0); + if (clen > 0) + { + BOOL OK; + switch (c) + { + VSPACE_CASES: + OK = TRUE; + break; + + default: + OK = FALSE; + break; + } + if (OK == (d == OP_VSPACE)) + { + if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSSTAR || + codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSQUERY) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + ADD_NEW_DATA(-(state_offset + (int)count), 0, 0); + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_HSPACE_EXTRA + OP_TYPEQUERY: + case OP_HSPACE_EXTRA + OP_TYPEMINQUERY: + case OP_HSPACE_EXTRA + OP_TYPEPOSQUERY: + count = 2; + goto QS5; + + case OP_HSPACE_EXTRA + OP_TYPESTAR: + case OP_HSPACE_EXTRA + OP_TYPEMINSTAR: + case OP_HSPACE_EXTRA + OP_TYPEPOSSTAR: + count = 0; + + QS5: + ADD_ACTIVE(state_offset + 2, 0); + if (clen > 0) + { + BOOL OK; + switch (c) + { + HSPACE_CASES: + OK = TRUE; + break; + + default: + OK = FALSE; + break; + } + + if (OK == (d == OP_HSPACE)) + { + if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSSTAR || + codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSQUERY) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + ADD_NEW_DATA(-(state_offset + (int)count), 0, 0); + } + } + break; + + /*-----------------------------------------------------------------*/ +#ifdef SUPPORT_UCP + case OP_PROP_EXTRA + OP_TYPEEXACT: + case OP_PROP_EXTRA + OP_TYPEUPTO: + case OP_PROP_EXTRA + OP_TYPEMINUPTO: + case OP_PROP_EXTRA + OP_TYPEPOSUPTO: + if (codevalue != OP_PROP_EXTRA + OP_TYPEEXACT) + { ADD_ACTIVE(state_offset + 1 + IMM2_SIZE + 3, 0); } + count = current_state->count; /* Number already matched */ + if (clen > 0) + { + BOOL OK; + const pcre_uint32 *cp; + const ucd_record * prop = GET_UCD(c); + switch(code[1 + IMM2_SIZE + 1]) + { + case PT_ANY: + OK = TRUE; + break; + + case PT_LAMP: + OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll || + prop->chartype == ucp_Lt; + break; + + case PT_GC: + OK = PRIV(ucp_gentype)[prop->chartype] == code[1 + IMM2_SIZE + 2]; + break; + + case PT_PC: + OK = prop->chartype == code[1 + IMM2_SIZE + 2]; + break; + + case PT_SC: + OK = prop->script == code[1 + IMM2_SIZE + 2]; + break; + + /* These are specials for combination cases. */ + + case PT_ALNUM: + OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N; + break; + + /* Perl space used to exclude VT, but from Perl 5.18 it is included, + which means that Perl space and POSIX space are now identical. PCRE + was changed at release 8.34. */ + + case PT_SPACE: /* Perl space */ + case PT_PXSPACE: /* POSIX space */ + switch(c) + { + HSPACE_CASES: + VSPACE_CASES: + OK = TRUE; + break; + + default: + OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z; + break; + } + break; + + case PT_WORD: + OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N || + c == CHAR_UNDERSCORE; + break; + + case PT_CLIST: + cp = PRIV(ucd_caseless_sets) + code[1 + IMM2_SIZE + 2]; + for (;;) + { + if (c < *cp) { OK = FALSE; break; } + if (c == *cp++) { OK = TRUE; break; } + } + break; + + case PT_UCNC: + OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT || + c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) || + c >= 0xe000; + break; + + /* Should never occur, but keep compilers from grumbling. */ + + default: + OK = codevalue != OP_PROP; + break; + } + + if (OK == (d == OP_PROP)) + { + if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSUPTO) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + if (++count >= (int)GET2(code, 1)) + { ADD_NEW(state_offset + 1 + IMM2_SIZE + 3, 0); } + else + { ADD_NEW(state_offset, count); } + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_EXTUNI_EXTRA + OP_TYPEEXACT: + case OP_EXTUNI_EXTRA + OP_TYPEUPTO: + case OP_EXTUNI_EXTRA + OP_TYPEMINUPTO: + case OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO: + if (codevalue != OP_EXTUNI_EXTRA + OP_TYPEEXACT) + { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); } + count = current_state->count; /* Number already matched */ + if (clen > 0) + { + int lgb, rgb; + const pcre_uchar *nptr = ptr + clen; + int ncount = 0; + if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + lgb = UCD_GRAPHBREAK(c); + while (nptr < end_subject) + { + dlen = 1; + if (!utf) d = *nptr; else { GETCHARLEN(d, nptr, dlen); } + rgb = UCD_GRAPHBREAK(d); + if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break; + ncount++; + lgb = rgb; + nptr += dlen; + } + if (nptr >= end_subject && (md->moptions & PCRE_PARTIAL_HARD) != 0) + reset_could_continue = TRUE; + if (++count >= (int)GET2(code, 1)) + { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, ncount); } + else + { ADD_NEW_DATA(-state_offset, count, ncount); } + } + break; +#endif + + /*-----------------------------------------------------------------*/ + case OP_ANYNL_EXTRA + OP_TYPEEXACT: + case OP_ANYNL_EXTRA + OP_TYPEUPTO: + case OP_ANYNL_EXTRA + OP_TYPEMINUPTO: + case OP_ANYNL_EXTRA + OP_TYPEPOSUPTO: + if (codevalue != OP_ANYNL_EXTRA + OP_TYPEEXACT) + { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); } + count = current_state->count; /* Number already matched */ + if (clen > 0) + { + int ncount = 0; + switch (c) + { + case CHAR_VT: + case CHAR_FF: + case CHAR_NEL: +#ifndef EBCDIC + case 0x2028: + case 0x2029: +#endif /* Not EBCDIC */ + if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break; + goto ANYNL03; + + case CHAR_CR: + if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1; + /* Fall through */ + + ANYNL03: + case CHAR_LF: + if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSUPTO) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + if (++count >= (int)GET2(code, 1)) + { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, ncount); } + else + { ADD_NEW_DATA(-state_offset, count, ncount); } + break; + + default: + break; + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_VSPACE_EXTRA + OP_TYPEEXACT: + case OP_VSPACE_EXTRA + OP_TYPEUPTO: + case OP_VSPACE_EXTRA + OP_TYPEMINUPTO: + case OP_VSPACE_EXTRA + OP_TYPEPOSUPTO: + if (codevalue != OP_VSPACE_EXTRA + OP_TYPEEXACT) + { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); } + count = current_state->count; /* Number already matched */ + if (clen > 0) + { + BOOL OK; + switch (c) + { + VSPACE_CASES: + OK = TRUE; + break; + + default: + OK = FALSE; + } + + if (OK == (d == OP_VSPACE)) + { + if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSUPTO) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + if (++count >= (int)GET2(code, 1)) + { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, 0); } + else + { ADD_NEW_DATA(-state_offset, count, 0); } + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_HSPACE_EXTRA + OP_TYPEEXACT: + case OP_HSPACE_EXTRA + OP_TYPEUPTO: + case OP_HSPACE_EXTRA + OP_TYPEMINUPTO: + case OP_HSPACE_EXTRA + OP_TYPEPOSUPTO: + if (codevalue != OP_HSPACE_EXTRA + OP_TYPEEXACT) + { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); } + count = current_state->count; /* Number already matched */ + if (clen > 0) + { + BOOL OK; + switch (c) + { + HSPACE_CASES: + OK = TRUE; + break; + + default: + OK = FALSE; + break; + } + + if (OK == (d == OP_HSPACE)) + { + if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSUPTO) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + if (++count >= (int)GET2(code, 1)) + { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, 0); } + else + { ADD_NEW_DATA(-state_offset, count, 0); } + } + } + break; + +/* ========================================================================== */ + /* These opcodes are followed by a character that is usually compared + to the current subject character; it is loaded into d. We still get + here even if there is no subject character, because in some cases zero + repetitions are permitted. */ + + /*-----------------------------------------------------------------*/ + case OP_CHAR: + if (clen > 0 && c == d) { ADD_NEW(state_offset + dlen + 1, 0); } + break; + + /*-----------------------------------------------------------------*/ + case OP_CHARI: + if (clen == 0) break; + +#ifdef SUPPORT_UTF + if (utf) + { + if (c == d) { ADD_NEW(state_offset + dlen + 1, 0); } else + { + unsigned int othercase; + if (c < 128) + othercase = fcc[c]; + else + /* If we have Unicode property support, we can use it to test the + other case of the character. */ +#ifdef SUPPORT_UCP + othercase = UCD_OTHERCASE(c); +#else + othercase = NOTACHAR; +#endif + + if (d == othercase) { ADD_NEW(state_offset + dlen + 1, 0); } + } + } + else +#endif /* SUPPORT_UTF */ + /* Not UTF mode */ + { + if (TABLE_GET(c, lcc, c) == TABLE_GET(d, lcc, d)) + { ADD_NEW(state_offset + 2, 0); } + } + break; + + +#ifdef SUPPORT_UCP + /*-----------------------------------------------------------------*/ + /* This is a tricky one because it can match more than one character. + Find out how many characters to skip, and then set up a negative state + to wait for them to pass before continuing. */ + + case OP_EXTUNI: + if (clen > 0) + { + int lgb, rgb; + const pcre_uchar *nptr = ptr + clen; + int ncount = 0; + lgb = UCD_GRAPHBREAK(c); + while (nptr < end_subject) + { + dlen = 1; + if (!utf) d = *nptr; else { GETCHARLEN(d, nptr, dlen); } + rgb = UCD_GRAPHBREAK(d); + if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break; + ncount++; + lgb = rgb; + nptr += dlen; + } + if (nptr >= end_subject && (md->moptions & PCRE_PARTIAL_HARD) != 0) + reset_could_continue = TRUE; + ADD_NEW_DATA(-(state_offset + 1), 0, ncount); + } + break; +#endif + + /*-----------------------------------------------------------------*/ + /* This is a tricky like EXTUNI because it too can match more than one + character (when CR is followed by LF). In this case, set up a negative + state to wait for one character to pass before continuing. */ + + case OP_ANYNL: + if (clen > 0) switch(c) + { + case CHAR_VT: + case CHAR_FF: + case CHAR_NEL: +#ifndef EBCDIC + case 0x2028: + case 0x2029: +#endif /* Not EBCDIC */ + if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break; + + case CHAR_LF: + ADD_NEW(state_offset + 1, 0); + break; + + case CHAR_CR: + if (ptr + 1 >= end_subject) + { + ADD_NEW(state_offset + 1, 0); + if ((md->moptions & PCRE_PARTIAL_HARD) != 0) + reset_could_continue = TRUE; + } + else if (UCHAR21TEST(ptr + 1) == CHAR_LF) + { + ADD_NEW_DATA(-(state_offset + 1), 0, 1); + } + else + { + ADD_NEW(state_offset + 1, 0); + } + break; + } + break; + + /*-----------------------------------------------------------------*/ + case OP_NOT_VSPACE: + if (clen > 0) switch(c) + { + VSPACE_CASES: + break; + + default: + ADD_NEW(state_offset + 1, 0); + break; + } + break; + + /*-----------------------------------------------------------------*/ + case OP_VSPACE: + if (clen > 0) switch(c) + { + VSPACE_CASES: + ADD_NEW(state_offset + 1, 0); + break; + + default: + break; + } + break; + + /*-----------------------------------------------------------------*/ + case OP_NOT_HSPACE: + if (clen > 0) switch(c) + { + HSPACE_CASES: + break; + + default: + ADD_NEW(state_offset + 1, 0); + break; + } + break; + + /*-----------------------------------------------------------------*/ + case OP_HSPACE: + if (clen > 0) switch(c) + { + HSPACE_CASES: + ADD_NEW(state_offset + 1, 0); + break; + + default: + break; + } + break; + + /*-----------------------------------------------------------------*/ + /* Match a negated single character casefully. */ + + case OP_NOT: + if (clen > 0 && c != d) { ADD_NEW(state_offset + dlen + 1, 0); } + break; + + /*-----------------------------------------------------------------*/ + /* Match a negated single character caselessly. */ + + case OP_NOTI: + if (clen > 0) + { + pcre_uint32 otherd; +#ifdef SUPPORT_UTF + if (utf && d >= 128) + { +#ifdef SUPPORT_UCP + otherd = UCD_OTHERCASE(d); +#else + otherd = d; +#endif /* SUPPORT_UCP */ + } + else +#endif /* SUPPORT_UTF */ + otherd = TABLE_GET(d, fcc, d); + if (c != d && c != otherd) + { ADD_NEW(state_offset + dlen + 1, 0); } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_PLUSI: + case OP_MINPLUSI: + case OP_POSPLUSI: + case OP_NOTPLUSI: + case OP_NOTMINPLUSI: + case OP_NOTPOSPLUSI: + caseless = TRUE; + codevalue -= OP_STARI - OP_STAR; + + /* Fall through */ + case OP_PLUS: + case OP_MINPLUS: + case OP_POSPLUS: + case OP_NOTPLUS: + case OP_NOTMINPLUS: + case OP_NOTPOSPLUS: + count = current_state->count; /* Already matched */ + if (count > 0) { ADD_ACTIVE(state_offset + dlen + 1, 0); } + if (clen > 0) + { + pcre_uint32 otherd = NOTACHAR; + if (caseless) + { +#ifdef SUPPORT_UTF + if (utf && d >= 128) + { +#ifdef SUPPORT_UCP + otherd = UCD_OTHERCASE(d); +#endif /* SUPPORT_UCP */ + } + else +#endif /* SUPPORT_UTF */ + otherd = TABLE_GET(d, fcc, d); + } + if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR)) + { + if (count > 0 && + (codevalue == OP_POSPLUS || codevalue == OP_NOTPOSPLUS)) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + count++; + ADD_NEW(state_offset, count); + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_QUERYI: + case OP_MINQUERYI: + case OP_POSQUERYI: + case OP_NOTQUERYI: + case OP_NOTMINQUERYI: + case OP_NOTPOSQUERYI: + caseless = TRUE; + codevalue -= OP_STARI - OP_STAR; + /* Fall through */ + case OP_QUERY: + case OP_MINQUERY: + case OP_POSQUERY: + case OP_NOTQUERY: + case OP_NOTMINQUERY: + case OP_NOTPOSQUERY: + ADD_ACTIVE(state_offset + dlen + 1, 0); + if (clen > 0) + { + pcre_uint32 otherd = NOTACHAR; + if (caseless) + { +#ifdef SUPPORT_UTF + if (utf && d >= 128) + { +#ifdef SUPPORT_UCP + otherd = UCD_OTHERCASE(d); +#endif /* SUPPORT_UCP */ + } + else +#endif /* SUPPORT_UTF */ + otherd = TABLE_GET(d, fcc, d); + } + if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR)) + { + if (codevalue == OP_POSQUERY || codevalue == OP_NOTPOSQUERY) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + ADD_NEW(state_offset + dlen + 1, 0); + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_STARI: + case OP_MINSTARI: + case OP_POSSTARI: + case OP_NOTSTARI: + case OP_NOTMINSTARI: + case OP_NOTPOSSTARI: + caseless = TRUE; + codevalue -= OP_STARI - OP_STAR; + /* Fall through */ + case OP_STAR: + case OP_MINSTAR: + case OP_POSSTAR: + case OP_NOTSTAR: + case OP_NOTMINSTAR: + case OP_NOTPOSSTAR: + ADD_ACTIVE(state_offset + dlen + 1, 0); + if (clen > 0) + { + pcre_uint32 otherd = NOTACHAR; + if (caseless) + { +#ifdef SUPPORT_UTF + if (utf && d >= 128) + { +#ifdef SUPPORT_UCP + otherd = UCD_OTHERCASE(d); +#endif /* SUPPORT_UCP */ + } + else +#endif /* SUPPORT_UTF */ + otherd = TABLE_GET(d, fcc, d); + } + if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR)) + { + if (codevalue == OP_POSSTAR || codevalue == OP_NOTPOSSTAR) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + ADD_NEW(state_offset, 0); + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_EXACTI: + case OP_NOTEXACTI: + caseless = TRUE; + codevalue -= OP_STARI - OP_STAR; + /* Fall through */ + case OP_EXACT: + case OP_NOTEXACT: + count = current_state->count; /* Number already matched */ + if (clen > 0) + { + pcre_uint32 otherd = NOTACHAR; + if (caseless) + { +#ifdef SUPPORT_UTF + if (utf && d >= 128) + { +#ifdef SUPPORT_UCP + otherd = UCD_OTHERCASE(d); +#endif /* SUPPORT_UCP */ + } + else +#endif /* SUPPORT_UTF */ + otherd = TABLE_GET(d, fcc, d); + } + if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR)) + { + if (++count >= (int)GET2(code, 1)) + { ADD_NEW(state_offset + dlen + 1 + IMM2_SIZE, 0); } + else + { ADD_NEW(state_offset, count); } + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_UPTOI: + case OP_MINUPTOI: + case OP_POSUPTOI: + case OP_NOTUPTOI: + case OP_NOTMINUPTOI: + case OP_NOTPOSUPTOI: + caseless = TRUE; + codevalue -= OP_STARI - OP_STAR; + /* Fall through */ + case OP_UPTO: + case OP_MINUPTO: + case OP_POSUPTO: + case OP_NOTUPTO: + case OP_NOTMINUPTO: + case OP_NOTPOSUPTO: + ADD_ACTIVE(state_offset + dlen + 1 + IMM2_SIZE, 0); + count = current_state->count; /* Number already matched */ + if (clen > 0) + { + pcre_uint32 otherd = NOTACHAR; + if (caseless) + { +#ifdef SUPPORT_UTF + if (utf && d >= 128) + { +#ifdef SUPPORT_UCP + otherd = UCD_OTHERCASE(d); +#endif /* SUPPORT_UCP */ + } + else +#endif /* SUPPORT_UTF */ + otherd = TABLE_GET(d, fcc, d); + } + if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR)) + { + if (codevalue == OP_POSUPTO || codevalue == OP_NOTPOSUPTO) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + if (++count >= (int)GET2(code, 1)) + { ADD_NEW(state_offset + dlen + 1 + IMM2_SIZE, 0); } + else + { ADD_NEW(state_offset, count); } + } + } + break; + + +/* ========================================================================== */ + /* These are the class-handling opcodes */ + + case OP_CLASS: + case OP_NCLASS: + case OP_XCLASS: + { + BOOL isinclass = FALSE; + int next_state_offset; + const pcre_uchar *ecode; + + /* For a simple class, there is always just a 32-byte table, and we + can set isinclass from it. */ + + if (codevalue != OP_XCLASS) + { + ecode = code + 1 + (32 / sizeof(pcre_uchar)); + if (clen > 0) + { + isinclass = (c > 255)? (codevalue == OP_NCLASS) : + ((((pcre_uint8 *)(code + 1))[c/8] & (1 << (c&7))) != 0); + } + } + + /* An extended class may have a table or a list of single characters, + ranges, or both, and it may be positive or negative. There's a + function that sorts all this out. */ + + else + { + ecode = code + GET(code, 1); + if (clen > 0) isinclass = PRIV(xclass)(c, code + 1 + LINK_SIZE, utf); + } + + /* At this point, isinclass is set for all kinds of class, and ecode + points to the byte after the end of the class. If there is a + quantifier, this is where it will be. */ + + next_state_offset = (int)(ecode - start_code); + + switch (*ecode) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRPOSSTAR: + ADD_ACTIVE(next_state_offset + 1, 0); + if (isinclass) + { + if (*ecode == OP_CRPOSSTAR) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + ADD_NEW(state_offset, 0); + } + break; + + case OP_CRPLUS: + case OP_CRMINPLUS: + case OP_CRPOSPLUS: + count = current_state->count; /* Already matched */ + if (count > 0) { ADD_ACTIVE(next_state_offset + 1, 0); } + if (isinclass) + { + if (count > 0 && *ecode == OP_CRPOSPLUS) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + count++; + ADD_NEW(state_offset, count); + } + break; + + case OP_CRQUERY: + case OP_CRMINQUERY: + case OP_CRPOSQUERY: + ADD_ACTIVE(next_state_offset + 1, 0); + if (isinclass) + { + if (*ecode == OP_CRPOSQUERY) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + ADD_NEW(next_state_offset + 1, 0); + } + break; + + case OP_CRRANGE: + case OP_CRMINRANGE: + case OP_CRPOSRANGE: + count = current_state->count; /* Already matched */ + if (count >= (int)GET2(ecode, 1)) + { ADD_ACTIVE(next_state_offset + 1 + 2 * IMM2_SIZE, 0); } + if (isinclass) + { + int max = (int)GET2(ecode, 1 + IMM2_SIZE); + if (*ecode == OP_CRPOSRANGE && count >= (int)GET2(ecode, 1)) + { + active_count--; /* Remove non-match possibility */ + next_active_state--; + } + if (++count >= max && max != 0) /* Max 0 => no limit */ + { ADD_NEW(next_state_offset + 1 + 2 * IMM2_SIZE, 0); } + else + { ADD_NEW(state_offset, count); } + } + break; + + default: + if (isinclass) { ADD_NEW(next_state_offset, 0); } + break; + } + } + break; + +/* ========================================================================== */ + /* These are the opcodes for fancy brackets of various kinds. We have + to use recursion in order to handle them. The "always failing" assertion + (?!) is optimised to OP_FAIL when compiling, so we have to support that, + though the other "backtracking verbs" are not supported. */ + + case OP_FAIL: + forced_fail++; /* Count FAILs for multiple states */ + break; + + case OP_ASSERT: + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + { + int rc; + int local_offsets[2]; + int local_workspace[1000]; + const pcre_uchar *endasscode = code + GET(code, 1); + + while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1); + + rc = internal_dfa_exec( + md, /* static match data */ + code, /* this subexpression's code */ + ptr, /* where we currently are */ + (int)(ptr - start_subject), /* start offset */ + local_offsets, /* offset vector */ + sizeof(local_offsets)/sizeof(int), /* size of same */ + local_workspace, /* workspace vector */ + sizeof(local_workspace)/sizeof(int), /* size of same */ + rlevel); /* function recursion level */ + + if (rc == PCRE_ERROR_DFA_UITEM) return rc; + if ((rc >= 0) == (codevalue == OP_ASSERT || codevalue == OP_ASSERTBACK)) + { ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_COND: + case OP_SCOND: + { + int local_offsets[1000]; + int local_workspace[1000]; + int codelink = GET(code, 1); + int condcode; + + /* Because of the way auto-callout works during compile, a callout item + is inserted between OP_COND and an assertion condition. This does not + happen for the other conditions. */ + + if (code[LINK_SIZE+1] == OP_CALLOUT) + { + rrc = 0; + if (PUBL(callout) != NULL) + { + PUBL(callout_block) cb; + cb.version = 1; /* Version 1 of the callout block */ + cb.callout_number = code[LINK_SIZE+2]; + cb.offset_vector = offsets; +#if defined COMPILE_PCRE8 + cb.subject = (PCRE_SPTR)start_subject; +#elif defined COMPILE_PCRE16 + cb.subject = (PCRE_SPTR16)start_subject; +#elif defined COMPILE_PCRE32 + cb.subject = (PCRE_SPTR32)start_subject; +#endif + cb.subject_length = (int)(end_subject - start_subject); + cb.start_match = (int)(current_subject - start_subject); + cb.current_position = (int)(ptr - start_subject); + cb.pattern_position = GET(code, LINK_SIZE + 3); + cb.next_item_length = GET(code, 3 + 2*LINK_SIZE); + cb.capture_top = 1; + cb.capture_last = -1; + cb.callout_data = md->callout_data; + cb.mark = NULL; /* No (*MARK) support */ + if ((rrc = (*PUBL(callout))(&cb)) < 0) return rrc; /* Abandon */ + } + if (rrc > 0) break; /* Fail this thread */ + code += PRIV(OP_lengths)[OP_CALLOUT]; /* Skip callout data */ + } + + condcode = code[LINK_SIZE+1]; + + /* Back reference conditions and duplicate named recursion conditions + are not supported */ + + if (condcode == OP_CREF || condcode == OP_DNCREF || + condcode == OP_DNRREF) + return PCRE_ERROR_DFA_UCOND; + + /* The DEFINE condition is always false, and the assertion (?!) is + converted to OP_FAIL. */ + + if (condcode == OP_DEF || condcode == OP_FAIL) + { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); } + + /* The only supported version of OP_RREF is for the value RREF_ANY, + which means "test if in any recursion". We can't test for specifically + recursed groups. */ + + else if (condcode == OP_RREF) + { + int value = GET2(code, LINK_SIZE + 2); + if (value != RREF_ANY) return PCRE_ERROR_DFA_UCOND; + if (md->recursive != NULL) + { ADD_ACTIVE(state_offset + LINK_SIZE + 2 + IMM2_SIZE, 0); } + else { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); } + } + + /* Otherwise, the condition is an assertion */ + + else + { + int rc; + const pcre_uchar *asscode = code + LINK_SIZE + 1; + const pcre_uchar *endasscode = asscode + GET(asscode, 1); + + while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1); + + rc = internal_dfa_exec( + md, /* fixed match data */ + asscode, /* this subexpression's code */ + ptr, /* where we currently are */ + (int)(ptr - start_subject), /* start offset */ + local_offsets, /* offset vector */ + sizeof(local_offsets)/sizeof(int), /* size of same */ + local_workspace, /* workspace vector */ + sizeof(local_workspace)/sizeof(int), /* size of same */ + rlevel); /* function recursion level */ + + if (rc == PCRE_ERROR_DFA_UITEM) return rc; + if ((rc >= 0) == + (condcode == OP_ASSERT || condcode == OP_ASSERTBACK)) + { ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); } + else + { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); } + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_RECURSE: + { + dfa_recursion_info *ri; + int local_offsets[1000]; + int local_workspace[1000]; + const pcre_uchar *callpat = start_code + GET(code, 1); + int recno = (callpat == md->start_code)? 0 : + GET2(callpat, 1 + LINK_SIZE); + int rc; + + DPRINTF(("%.*sStarting regex recursion\n", rlevel*2-2, SP)); + + /* Check for repeating a recursion without advancing the subject + pointer. This should catch convoluted mutual recursions. (Some simple + cases are caught at compile time.) */ + + for (ri = md->recursive; ri != NULL; ri = ri->prevrec) + if (recno == ri->group_num && ptr == ri->subject_position) + return PCRE_ERROR_RECURSELOOP; + + /* Remember this recursion and where we started it so as to + catch infinite loops. */ + + new_recursive.group_num = recno; + new_recursive.subject_position = ptr; + new_recursive.prevrec = md->recursive; + md->recursive = &new_recursive; + + rc = internal_dfa_exec( + md, /* fixed match data */ + callpat, /* this subexpression's code */ + ptr, /* where we currently are */ + (int)(ptr - start_subject), /* start offset */ + local_offsets, /* offset vector */ + sizeof(local_offsets)/sizeof(int), /* size of same */ + local_workspace, /* workspace vector */ + sizeof(local_workspace)/sizeof(int), /* size of same */ + rlevel); /* function recursion level */ + + md->recursive = new_recursive.prevrec; /* Done this recursion */ + + DPRINTF(("%.*sReturn from regex recursion: rc=%d\n", rlevel*2-2, SP, + rc)); + + /* Ran out of internal offsets */ + + if (rc == 0) return PCRE_ERROR_DFA_RECURSE; + + /* For each successful matched substring, set up the next state with a + count of characters to skip before trying it. Note that the count is in + characters, not bytes. */ + + if (rc > 0) + { + for (rc = rc*2 - 2; rc >= 0; rc -= 2) + { + int charcount = local_offsets[rc+1] - local_offsets[rc]; +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf) + { + const pcre_uchar *p = start_subject + local_offsets[rc]; + const pcre_uchar *pp = start_subject + local_offsets[rc+1]; + while (p < pp) if (NOT_FIRSTCHAR(*p++)) charcount--; + } +#endif + if (charcount > 0) + { + ADD_NEW_DATA(-(state_offset + LINK_SIZE + 1), 0, (charcount - 1)); + } + else + { + ADD_ACTIVE(state_offset + LINK_SIZE + 1, 0); + } + } + } + else if (rc != PCRE_ERROR_NOMATCH) return rc; + } + break; + + /*-----------------------------------------------------------------*/ + case OP_BRAPOS: + case OP_SBRAPOS: + case OP_CBRAPOS: + case OP_SCBRAPOS: + case OP_BRAPOSZERO: + { + int charcount, matched_count; + const pcre_uchar *local_ptr = ptr; + BOOL allow_zero; + + if (codevalue == OP_BRAPOSZERO) + { + allow_zero = TRUE; + codevalue = *(++code); /* Codevalue will be one of above BRAs */ + } + else allow_zero = FALSE; + + /* Loop to match the subpattern as many times as possible as if it were + a complete pattern. */ + + for (matched_count = 0;; matched_count++) + { + int local_offsets[2]; + int local_workspace[1000]; + + int rc = internal_dfa_exec( + md, /* fixed match data */ + code, /* this subexpression's code */ + local_ptr, /* where we currently are */ + (int)(ptr - start_subject), /* start offset */ + local_offsets, /* offset vector */ + sizeof(local_offsets)/sizeof(int), /* size of same */ + local_workspace, /* workspace vector */ + sizeof(local_workspace)/sizeof(int), /* size of same */ + rlevel); /* function recursion level */ + + /* Failed to match */ + + if (rc < 0) + { + if (rc != PCRE_ERROR_NOMATCH) return rc; + break; + } + + /* Matched: break the loop if zero characters matched. */ + + charcount = local_offsets[1] - local_offsets[0]; + if (charcount == 0) break; + local_ptr += charcount; /* Advance temporary position ptr */ + } + + /* At this point we have matched the subpattern matched_count + times, and local_ptr is pointing to the character after the end of the + last match. */ + + if (matched_count > 0 || allow_zero) + { + const pcre_uchar *end_subpattern = code; + int next_state_offset; + + do { end_subpattern += GET(end_subpattern, 1); } + while (*end_subpattern == OP_ALT); + next_state_offset = + (int)(end_subpattern - start_code + LINK_SIZE + 1); + + /* Optimization: if there are no more active states, and there + are no new states yet set up, then skip over the subject string + right here, to save looping. Otherwise, set up the new state to swing + into action when the end of the matched substring is reached. */ + + if (i + 1 >= active_count && new_count == 0) + { + ptr = local_ptr; + clen = 0; + ADD_NEW(next_state_offset, 0); + } + else + { + const pcre_uchar *p = ptr; + const pcre_uchar *pp = local_ptr; + charcount = (int)(pp - p); +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf) while (p < pp) if (NOT_FIRSTCHAR(*p++)) charcount--; +#endif + ADD_NEW_DATA(-next_state_offset, 0, (charcount - 1)); + } + } + } + break; + + /*-----------------------------------------------------------------*/ + case OP_ONCE: + case OP_ONCE_NC: + { + int local_offsets[2]; + int local_workspace[1000]; + + int rc = internal_dfa_exec( + md, /* fixed match data */ + code, /* this subexpression's code */ + ptr, /* where we currently are */ + (int)(ptr - start_subject), /* start offset */ + local_offsets, /* offset vector */ + sizeof(local_offsets)/sizeof(int), /* size of same */ + local_workspace, /* workspace vector */ + sizeof(local_workspace)/sizeof(int), /* size of same */ + rlevel); /* function recursion level */ + + if (rc >= 0) + { + const pcre_uchar *end_subpattern = code; + int charcount = local_offsets[1] - local_offsets[0]; + int next_state_offset, repeat_state_offset; + + do { end_subpattern += GET(end_subpattern, 1); } + while (*end_subpattern == OP_ALT); + next_state_offset = + (int)(end_subpattern - start_code + LINK_SIZE + 1); + + /* If the end of this subpattern is KETRMAX or KETRMIN, we must + arrange for the repeat state also to be added to the relevant list. + Calculate the offset, or set -1 for no repeat. */ + + repeat_state_offset = (*end_subpattern == OP_KETRMAX || + *end_subpattern == OP_KETRMIN)? + (int)(end_subpattern - start_code - GET(end_subpattern, 1)) : -1; + + /* If we have matched an empty string, add the next state at the + current character pointer. This is important so that the duplicate + checking kicks in, which is what breaks infinite loops that match an + empty string. */ + + if (charcount == 0) + { + ADD_ACTIVE(next_state_offset, 0); + } + + /* Optimization: if there are no more active states, and there + are no new states yet set up, then skip over the subject string + right here, to save looping. Otherwise, set up the new state to swing + into action when the end of the matched substring is reached. */ + + else if (i + 1 >= active_count && new_count == 0) + { + ptr += charcount; + clen = 0; + ADD_NEW(next_state_offset, 0); + + /* If we are adding a repeat state at the new character position, + we must fudge things so that it is the only current state. + Otherwise, it might be a duplicate of one we processed before, and + that would cause it to be skipped. */ + + if (repeat_state_offset >= 0) + { + next_active_state = active_states; + active_count = 0; + i = -1; + ADD_ACTIVE(repeat_state_offset, 0); + } + } + else + { +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (utf) + { + const pcre_uchar *p = start_subject + local_offsets[0]; + const pcre_uchar *pp = start_subject + local_offsets[1]; + while (p < pp) if (NOT_FIRSTCHAR(*p++)) charcount--; + } +#endif + ADD_NEW_DATA(-next_state_offset, 0, (charcount - 1)); + if (repeat_state_offset >= 0) + { ADD_NEW_DATA(-repeat_state_offset, 0, (charcount - 1)); } + } + } + else if (rc != PCRE_ERROR_NOMATCH) return rc; + } + break; + + +/* ========================================================================== */ + /* Handle callouts */ + + case OP_CALLOUT: + rrc = 0; + if (PUBL(callout) != NULL) + { + PUBL(callout_block) cb; + cb.version = 1; /* Version 1 of the callout block */ + cb.callout_number = code[1]; + cb.offset_vector = offsets; +#if defined COMPILE_PCRE8 + cb.subject = (PCRE_SPTR)start_subject; +#elif defined COMPILE_PCRE16 + cb.subject = (PCRE_SPTR16)start_subject; +#elif defined COMPILE_PCRE32 + cb.subject = (PCRE_SPTR32)start_subject; +#endif + cb.subject_length = (int)(end_subject - start_subject); + cb.start_match = (int)(current_subject - start_subject); + cb.current_position = (int)(ptr - start_subject); + cb.pattern_position = GET(code, 2); + cb.next_item_length = GET(code, 2 + LINK_SIZE); + cb.capture_top = 1; + cb.capture_last = -1; + cb.callout_data = md->callout_data; + cb.mark = NULL; /* No (*MARK) support */ + if ((rrc = (*PUBL(callout))(&cb)) < 0) return rrc; /* Abandon */ + } + if (rrc == 0) + { ADD_ACTIVE(state_offset + PRIV(OP_lengths)[OP_CALLOUT], 0); } + break; + + +/* ========================================================================== */ + default: /* Unsupported opcode */ + return PCRE_ERROR_DFA_UITEM; + } + + NEXT_ACTIVE_STATE: continue; + + } /* End of loop scanning active states */ + + /* We have finished the processing at the current subject character. If no + new states have been set for the next character, we have found all the + matches that we are going to find. If we are at the top level and partial + matching has been requested, check for appropriate conditions. + + The "forced_ fail" variable counts the number of (*F) encountered for the + character. If it is equal to the original active_count (saved in + workspace[1]) it means that (*F) was found on every active state. In this + case we don't want to give a partial match. + + The "could_continue" variable is true if a state could have continued but + for the fact that the end of the subject was reached. */ + + if (new_count <= 0) + { + if (rlevel == 1 && /* Top level, and */ + could_continue && /* Some could go on, and */ + forced_fail != workspace[1] && /* Not all forced fail & */ + ( /* either... */ + (md->moptions & PCRE_PARTIAL_HARD) != 0 /* Hard partial */ + || /* or... */ + ((md->moptions & PCRE_PARTIAL_SOFT) != 0 && /* Soft partial and */ + match_count < 0) /* no matches */ + ) && /* And... */ + ( + partial_newline || /* Either partial NL */ + ( /* or ... */ + ptr >= end_subject && /* End of subject and */ + ptr > md->start_used_ptr) /* Inspected non-empty string */ + ) + ) + match_count = PCRE_ERROR_PARTIAL; + DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n" + "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel, match_count, + rlevel*2-2, SP)); + break; /* In effect, "return", but see the comment below */ + } + + /* One or more states are active for the next character. */ + + ptr += clen; /* Advance to next subject character */ + } /* Loop to move along the subject string */ + +/* Control gets here from "break" a few lines above. We do it this way because +if we use "return" above, we have compiler trouble. Some compilers warn if +there's nothing here because they think the function doesn't return a value. On +the other hand, if we put a dummy statement here, some more clever compilers +complain that it can't be reached. Sigh. */ + +return match_count; +} + + + + +/************************************************* +* Execute a Regular Expression - DFA engine * +*************************************************/ + +/* This external function applies a compiled re to a subject string using a DFA +engine. This function calls the internal function multiple times if the pattern +is not anchored. + +Arguments: + argument_re points to the compiled expression + extra_data points to extra data or is NULL + subject points to the subject string + length length of subject string (may contain binary zeros) + start_offset where to start in the subject string + options option bits + offsets vector of match offsets + offsetcount size of same + workspace workspace vector + wscount size of same + +Returns: > 0 => number of match offset pairs placed in offsets + = 0 => offsets overflowed; longest matches are present + -1 => failed to match + < -1 => some kind of unexpected problem +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_dfa_exec(const pcre *argument_re, const pcre_extra *extra_data, + const char *subject, int length, int start_offset, int options, int *offsets, + int offsetcount, int *workspace, int wscount) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre16_dfa_exec(const pcre16 *argument_re, const pcre16_extra *extra_data, + PCRE_SPTR16 subject, int length, int start_offset, int options, int *offsets, + int offsetcount, int *workspace, int wscount) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre32_dfa_exec(const pcre32 *argument_re, const pcre32_extra *extra_data, + PCRE_SPTR32 subject, int length, int start_offset, int options, int *offsets, + int offsetcount, int *workspace, int wscount) +#endif +{ +REAL_PCRE *re = (REAL_PCRE *)argument_re; +dfa_match_data match_block; +dfa_match_data *md = &match_block; +BOOL utf, anchored, startline, firstline; +const pcre_uchar *current_subject, *end_subject; +const pcre_study_data *study = NULL; + +const pcre_uchar *req_char_ptr; +const pcre_uint8 *start_bits = NULL; +BOOL has_first_char = FALSE; +BOOL has_req_char = FALSE; +pcre_uchar first_char = 0; +pcre_uchar first_char2 = 0; +pcre_uchar req_char = 0; +pcre_uchar req_char2 = 0; +int newline; + +/* Plausibility checks */ + +if ((options & ~PUBLIC_DFA_EXEC_OPTIONS) != 0) return PCRE_ERROR_BADOPTION; +if (re == NULL || subject == NULL || workspace == NULL || + (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL; +if (offsetcount < 0) return PCRE_ERROR_BADCOUNT; +if (wscount < 20) return PCRE_ERROR_DFA_WSSIZE; +if (length < 0) return PCRE_ERROR_BADLENGTH; +if (start_offset < 0 || start_offset > length) return PCRE_ERROR_BADOFFSET; + +/* Check that the first field in the block is the magic number. If it is not, +return with PCRE_ERROR_BADMAGIC. However, if the magic number is equal to +REVERSED_MAGIC_NUMBER we return with PCRE_ERROR_BADENDIANNESS, which +means that the pattern is likely compiled with different endianness. */ + +if (re->magic_number != MAGIC_NUMBER) + return re->magic_number == REVERSED_MAGIC_NUMBER? + PCRE_ERROR_BADENDIANNESS:PCRE_ERROR_BADMAGIC; +if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE; + +/* If restarting after a partial match, do some sanity checks on the contents +of the workspace. */ + +if ((options & PCRE_DFA_RESTART) != 0) + { + if ((workspace[0] & (-2)) != 0 || workspace[1] < 1 || + workspace[1] > (wscount - 2)/INTS_PER_STATEBLOCK) + return PCRE_ERROR_DFA_BADRESTART; + } + +/* Set up study, callout, and table data */ + +md->tables = re->tables; +md->callout_data = NULL; + +if (extra_data != NULL) + { + unsigned long int flags = extra_data->flags; + if ((flags & PCRE_EXTRA_STUDY_DATA) != 0) + study = (const pcre_study_data *)extra_data->study_data; + if ((flags & PCRE_EXTRA_MATCH_LIMIT) != 0) return PCRE_ERROR_DFA_UMLIMIT; + if ((flags & PCRE_EXTRA_MATCH_LIMIT_RECURSION) != 0) + return PCRE_ERROR_DFA_UMLIMIT; + if ((flags & PCRE_EXTRA_CALLOUT_DATA) != 0) + md->callout_data = extra_data->callout_data; + if ((flags & PCRE_EXTRA_TABLES) != 0) + md->tables = extra_data->tables; + } + +/* Set some local values */ + +current_subject = (const pcre_uchar *)subject + start_offset; +end_subject = (const pcre_uchar *)subject + length; +req_char_ptr = current_subject - 1; + +#ifdef SUPPORT_UTF +/* PCRE_UTF(16|32) have the same value as PCRE_UTF8. */ +utf = (re->options & PCRE_UTF8) != 0; +#else +utf = FALSE; +#endif + +anchored = (options & (PCRE_ANCHORED|PCRE_DFA_RESTART)) != 0 || + (re->options & PCRE_ANCHORED) != 0; + +/* The remaining fixed data for passing around. */ + +md->start_code = (const pcre_uchar *)argument_re + + re->name_table_offset + re->name_count * re->name_entry_size; +md->start_subject = (const pcre_uchar *)subject; +md->end_subject = end_subject; +md->start_offset = start_offset; +md->moptions = options; +md->poptions = re->options; + +/* If the BSR option is not set at match time, copy what was set +at compile time. */ + +if ((md->moptions & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) == 0) + { + if ((re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) != 0) + md->moptions |= re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE); +#ifdef BSR_ANYCRLF + else md->moptions |= PCRE_BSR_ANYCRLF; +#endif + } + +/* Handle different types of newline. The three bits give eight cases. If +nothing is set at run time, whatever was used at compile time applies. */ + +switch ((((options & PCRE_NEWLINE_BITS) == 0)? re->options : (pcre_uint32)options) & + PCRE_NEWLINE_BITS) + { + case 0: newline = NEWLINE; break; /* Compile-time default */ + case PCRE_NEWLINE_CR: newline = CHAR_CR; break; + case PCRE_NEWLINE_LF: newline = CHAR_NL; break; + case PCRE_NEWLINE_CR+ + PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break; + case PCRE_NEWLINE_ANY: newline = -1; break; + case PCRE_NEWLINE_ANYCRLF: newline = -2; break; + default: return PCRE_ERROR_BADNEWLINE; + } + +if (newline == -2) + { + md->nltype = NLTYPE_ANYCRLF; + } +else if (newline < 0) + { + md->nltype = NLTYPE_ANY; + } +else + { + md->nltype = NLTYPE_FIXED; + if (newline > 255) + { + md->nllen = 2; + md->nl[0] = (newline >> 8) & 255; + md->nl[1] = newline & 255; + } + else + { + md->nllen = 1; + md->nl[0] = newline; + } + } + +/* Check a UTF-8 string if required. Unfortunately there's no way of passing +back the character offset. */ + +#ifdef SUPPORT_UTF +if (utf && (options & PCRE_NO_UTF8_CHECK) == 0) + { + int erroroffset; + int errorcode = PRIV(valid_utf)((pcre_uchar *)subject, length, &erroroffset); + if (errorcode != 0) + { + if (offsetcount >= 2) + { + offsets[0] = erroroffset; + offsets[1] = errorcode; + } +#if defined COMPILE_PCRE8 + return (errorcode <= PCRE_UTF8_ERR5 && (options & PCRE_PARTIAL_HARD) != 0) ? + PCRE_ERROR_SHORTUTF8 : PCRE_ERROR_BADUTF8; +#elif defined COMPILE_PCRE16 + return (errorcode <= PCRE_UTF16_ERR1 && (options & PCRE_PARTIAL_HARD) != 0) ? + PCRE_ERROR_SHORTUTF16 : PCRE_ERROR_BADUTF16; +#elif defined COMPILE_PCRE32 + return PCRE_ERROR_BADUTF32; +#endif + } +#if defined COMPILE_PCRE8 || defined COMPILE_PCRE16 + if (start_offset > 0 && start_offset < length && + NOT_FIRSTCHAR(((PCRE_PUCHAR)subject)[start_offset])) + return PCRE_ERROR_BADUTF8_OFFSET; +#endif + } +#endif + +/* If the exec call supplied NULL for tables, use the inbuilt ones. This +is a feature that makes it possible to save compiled regex and re-use them +in other programs later. */ + +if (md->tables == NULL) md->tables = PRIV(default_tables); + +/* The "must be at the start of a line" flags are used in a loop when finding +where to start. */ + +startline = (re->flags & PCRE_STARTLINE) != 0; +firstline = (re->options & PCRE_FIRSTLINE) != 0; + +/* Set up the first character to match, if available. The first_byte value is +never set for an anchored regular expression, but the anchoring may be forced +at run time, so we have to test for anchoring. The first char may be unset for +an unanchored pattern, of course. If there's no first char and the pattern was +studied, there may be a bitmap of possible first characters. */ + +if (!anchored) + { + if ((re->flags & PCRE_FIRSTSET) != 0) + { + has_first_char = TRUE; + first_char = first_char2 = (pcre_uchar)(re->first_char); + if ((re->flags & PCRE_FCH_CASELESS) != 0) + { + first_char2 = TABLE_GET(first_char, md->tables + fcc_offset, first_char); +#if defined SUPPORT_UCP && !(defined COMPILE_PCRE8) + if (utf && first_char > 127) + first_char2 = UCD_OTHERCASE(first_char); +#endif + } + } + else + { + if (!startline && study != NULL && + (study->flags & PCRE_STUDY_MAPPED) != 0) + start_bits = study->start_bits; + } + } + +/* For anchored or unanchored matches, there may be a "last known required +character" set. */ + +if ((re->flags & PCRE_REQCHSET) != 0) + { + has_req_char = TRUE; + req_char = req_char2 = (pcre_uchar)(re->req_char); + if ((re->flags & PCRE_RCH_CASELESS) != 0) + { + req_char2 = TABLE_GET(req_char, md->tables + fcc_offset, req_char); +#if defined SUPPORT_UCP && !(defined COMPILE_PCRE8) + if (utf && req_char > 127) + req_char2 = UCD_OTHERCASE(req_char); +#endif + } + } + +/* Call the main matching function, looping for a non-anchored regex after a +failed match. If not restarting, perform certain optimizations at the start of +a match. */ + +for (;;) + { + int rc; + + if ((options & PCRE_DFA_RESTART) == 0) + { + const pcre_uchar *save_end_subject = end_subject; + + /* If firstline is TRUE, the start of the match is constrained to the first + line of a multiline string. Implement this by temporarily adjusting + end_subject so that we stop scanning at a newline. If the match fails at + the newline, later code breaks this loop. */ + + if (firstline) + { + PCRE_PUCHAR t = current_subject; +#ifdef SUPPORT_UTF + if (utf) + { + while (t < md->end_subject && !IS_NEWLINE(t)) + { + t++; + ACROSSCHAR(t < end_subject, *t, t++); + } + } + else +#endif + while (t < md->end_subject && !IS_NEWLINE(t)) t++; + end_subject = t; + } + + /* There are some optimizations that avoid running the match if a known + starting point is not found. However, there is an option that disables + these, for testing and for ensuring that all callouts do actually occur. + The option can be set in the regex by (*NO_START_OPT) or passed in + match-time options. */ + + if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0) + { + /* Advance to a known first pcre_uchar (i.e. data item) */ + + if (has_first_char) + { + if (first_char != first_char2) + { + pcre_uchar csc; + while (current_subject < end_subject && + (csc = UCHAR21TEST(current_subject)) != first_char && csc != first_char2) + current_subject++; + } + else + while (current_subject < end_subject && + UCHAR21TEST(current_subject) != first_char) + current_subject++; + } + + /* Or to just after a linebreak for a multiline match if possible */ + + else if (startline) + { + if (current_subject > md->start_subject + start_offset) + { +#ifdef SUPPORT_UTF + if (utf) + { + while (current_subject < end_subject && + !WAS_NEWLINE(current_subject)) + { + current_subject++; + ACROSSCHAR(current_subject < end_subject, *current_subject, + current_subject++); + } + } + else +#endif + while (current_subject < end_subject && !WAS_NEWLINE(current_subject)) + current_subject++; + + /* If we have just passed a CR and the newline option is ANY or + ANYCRLF, and we are now at a LF, advance the match position by one + more character. */ + + if (UCHAR21TEST(current_subject - 1) == CHAR_CR && + (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF) && + current_subject < end_subject && + UCHAR21TEST(current_subject) == CHAR_NL) + current_subject++; + } + } + + /* Advance to a non-unique first pcre_uchar after study */ + + else if (start_bits != NULL) + { + while (current_subject < end_subject) + { + register pcre_uint32 c = UCHAR21TEST(current_subject); +#ifndef COMPILE_PCRE8 + if (c > 255) c = 255; +#endif + if ((start_bits[c/8] & (1 << (c&7))) != 0) break; + current_subject++; + } + } + } + + /* Restore fudged end_subject */ + + end_subject = save_end_subject; + + /* The following two optimizations are disabled for partial matching or if + disabling is explicitly requested (and of course, by the test above, this + code is not obeyed when restarting after a partial match). */ + + if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0 && + (options & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT)) == 0) + { + /* If the pattern was studied, a minimum subject length may be set. This + is a lower bound; no actual string of that length may actually match the + pattern. Although the value is, strictly, in characters, we treat it as + in pcre_uchar units to avoid spending too much time in this optimization. + */ + + if (study != NULL && (study->flags & PCRE_STUDY_MINLEN) != 0 && + (pcre_uint32)(end_subject - current_subject) < study->minlength) + return PCRE_ERROR_NOMATCH; + + /* If req_char is set, we know that that pcre_uchar must appear in the + subject for the match to succeed. If the first pcre_uchar is set, + req_char must be later in the subject; otherwise the test starts at the + match point. This optimization can save a huge amount of work in patterns + with nested unlimited repeats that aren't going to match. Writing + separate code for cased/caseless versions makes it go faster, as does + using an autoincrement and backing off on a match. + + HOWEVER: when the subject string is very, very long, searching to its end + can take a long time, and give bad performance on quite ordinary + patterns. This showed up when somebody was matching /^C/ on a 32-megabyte + string... so we don't do this when the string is sufficiently long. */ + + if (has_req_char && end_subject - current_subject < REQ_BYTE_MAX) + { + register PCRE_PUCHAR p = current_subject + (has_first_char? 1:0); + + /* We don't need to repeat the search if we haven't yet reached the + place we found it at last time. */ + + if (p > req_char_ptr) + { + if (req_char != req_char2) + { + while (p < end_subject) + { + register pcre_uint32 pp = UCHAR21INCTEST(p); + if (pp == req_char || pp == req_char2) { p--; break; } + } + } + else + { + while (p < end_subject) + { + if (UCHAR21INCTEST(p) == req_char) { p--; break; } + } + } + + /* If we can't find the required pcre_uchar, break the matching loop, + which will cause a return or PCRE_ERROR_NOMATCH. */ + + if (p >= end_subject) break; + + /* If we have found the required pcre_uchar, save the point where we + found it, so that we don't search again next time round the loop if + the start hasn't passed this point yet. */ + + req_char_ptr = p; + } + } + } + } /* End of optimizations that are done when not restarting */ + + /* OK, now we can do the business */ + + md->start_used_ptr = current_subject; + md->recursive = NULL; + + rc = internal_dfa_exec( + md, /* fixed match data */ + md->start_code, /* this subexpression's code */ + current_subject, /* where we currently are */ + start_offset, /* start offset in subject */ + offsets, /* offset vector */ + offsetcount, /* size of same */ + workspace, /* workspace vector */ + wscount, /* size of same */ + 0); /* function recurse level */ + + /* Anything other than "no match" means we are done, always; otherwise, carry + on only if not anchored. */ + + if (rc != PCRE_ERROR_NOMATCH || anchored) + { + if (rc == PCRE_ERROR_PARTIAL && offsetcount >= 2) + { + offsets[0] = (int)(md->start_used_ptr - (PCRE_PUCHAR)subject); + offsets[1] = (int)(end_subject - (PCRE_PUCHAR)subject); + if (offsetcount > 2) + offsets[2] = (int)(current_subject - (PCRE_PUCHAR)subject); + } + return rc; + } + + /* Advance to the next subject character unless we are at the end of a line + and firstline is set. */ + + if (firstline && IS_NEWLINE(current_subject)) break; + current_subject++; +#ifdef SUPPORT_UTF + if (utf) + { + ACROSSCHAR(current_subject < end_subject, *current_subject, + current_subject++); + } +#endif + if (current_subject > end_subject) break; + + /* If we have just passed a CR and we are now at a LF, and the pattern does + not contain any explicit matches for \r or \n, and the newline option is CRLF + or ANY or ANYCRLF, advance the match position by one more character. */ + + if (UCHAR21TEST(current_subject - 1) == CHAR_CR && + current_subject < end_subject && + UCHAR21TEST(current_subject) == CHAR_NL && + (re->flags & PCRE_HASCRORLF) == 0 && + (md->nltype == NLTYPE_ANY || + md->nltype == NLTYPE_ANYCRLF || + md->nllen == 2)) + current_subject++; + + } /* "Bumpalong" loop */ + +return PCRE_ERROR_NOMATCH; +} + +/* End of pcre_dfa_exec.c */ diff --git a/deps/pcre/pcre_exec.c b/deps/pcre/pcre_exec.c new file mode 100644 index 00000000000..5b96954fcd9 --- /dev/null +++ b/deps/pcre/pcre_exec.c @@ -0,0 +1,7173 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2021 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + +/* This module contains pcre_exec(), the externally visible function that does +pattern matching using an NFA algorithm, trying to mimic Perl as closely as +possible. There are also some static supporting functions. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#define NLBLOCK md /* Block containing newline information */ +#define PSSTART start_subject /* Field containing processed string start */ +#define PSEND end_subject /* Field containing processed string end */ + +#include "pcre_internal.h" + +/* Undefine some potentially clashing cpp symbols */ + +#undef min +#undef max + +/* The md->capture_last field uses the lower 16 bits for the last captured +substring (which can never be greater than 65535) and a bit in the top half +to mean "capture vector overflowed". This odd way of doing things was +implemented when it was realized that preserving and restoring the overflow bit +whenever the last capture number was saved/restored made for a neater +interface, and doing it this way saved on (a) another variable, which would +have increased the stack frame size (a big NO-NO in PCRE) and (b) another +separate set of save/restore instructions. The following defines are used in +implementing this. */ + +#define CAPLMASK 0x0000ffff /* The bits used for last_capture */ +#define OVFLMASK 0xffff0000 /* The bits used for the overflow flag */ +#define OVFLBIT 0x00010000 /* The bit that is set for overflow */ + +/* Values for setting in md->match_function_type to indicate two special types +of call to match(). We do it this way to save on using another stack variable, +as stack usage is to be discouraged. */ + +#define MATCH_CONDASSERT 1 /* Called to check a condition assertion */ +#define MATCH_CBEGROUP 2 /* Could-be-empty unlimited repeat group */ + +/* Non-error returns from the match() function. Error returns are externally +defined PCRE_ERROR_xxx codes, which are all negative. */ + +#define MATCH_MATCH 1 +#define MATCH_NOMATCH 0 + +/* Special internal returns from the match() function. Make them sufficiently +negative to avoid the external error codes. */ + +#define MATCH_ACCEPT (-999) +#define MATCH_KETRPOS (-998) +#define MATCH_ONCE (-997) +/* The next 5 must be kept together and in sequence so that a test that checks +for any one of them can use a range. */ +#define MATCH_COMMIT (-996) +#define MATCH_PRUNE (-995) +#define MATCH_SKIP (-994) +#define MATCH_SKIP_ARG (-993) +#define MATCH_THEN (-992) +#define MATCH_BACKTRACK_MAX MATCH_THEN +#define MATCH_BACKTRACK_MIN MATCH_COMMIT + +/* Maximum number of ints of offset to save on the stack for recursive calls. +If the offset vector is bigger, malloc is used. This should be a multiple of 3, +because the offset vector is always a multiple of 3 long. */ + +#define REC_STACK_SAVE_MAX 30 + +/* Min and max values for the common repeats; for the maxima, 0 => infinity */ + +static const char rep_min[] = { 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, }; +static const char rep_max[] = { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, }; + +#ifdef PCRE_DEBUG +/************************************************* +* Debugging function to print chars * +*************************************************/ + +/* Print a sequence of chars in printable format, stopping at the end of the +subject if the requested. + +Arguments: + p points to characters + length number to print + is_subject TRUE if printing from within md->start_subject + md pointer to matching data block, if is_subject is TRUE + +Returns: nothing +*/ + +static void +pchars(const pcre_uchar *p, int length, BOOL is_subject, match_data *md) +{ +pcre_uint32 c; +BOOL utf = md->utf; +if (is_subject && length > md->end_subject - p) length = md->end_subject - p; +while (length-- > 0) + if (isprint(c = UCHAR21INCTEST(p))) printf("%c", (char)c); else printf("\\x{%02x}", c); +} +#endif + + + +/************************************************* +* Match a back-reference * +*************************************************/ + +/* Normally, if a back reference hasn't been set, the length that is passed is +negative, so the match always fails. However, in JavaScript compatibility mode, +the length passed is zero. Note that in caseless UTF-8 mode, the number of +subject bytes matched may be different to the number of reference bytes. + +Arguments: + offset index into the offset vector + eptr pointer into the subject + length length of reference to be matched (number of bytes) + md points to match data block + caseless TRUE if caseless + +Returns: >= 0 the number of subject bytes matched + -1 no match + -2 partial match; always given if at end subject +*/ + +static int +match_ref(int offset, register PCRE_PUCHAR eptr, int length, match_data *md, + BOOL caseless) +{ +PCRE_PUCHAR eptr_start = eptr; +register PCRE_PUCHAR p = md->start_subject + md->offset_vector[offset]; +#if defined SUPPORT_UTF && defined SUPPORT_UCP +BOOL utf = md->utf; +#endif + +#ifdef PCRE_DEBUG +if (eptr >= md->end_subject) + printf("matching subject "); +else + { + printf("matching subject "); + pchars(eptr, length, TRUE, md); + } +printf(" against backref "); +pchars(p, length, FALSE, md); +printf("\n"); +#endif + +/* Always fail if reference not set (and not JavaScript compatible - in that +case the length is passed as zero). */ + +if (length < 0) return -1; + +/* Separate the caseless case for speed. In UTF-8 mode we can only do this +properly if Unicode properties are supported. Otherwise, we can check only +ASCII characters. */ + +if (caseless) + { +#if defined SUPPORT_UTF && defined SUPPORT_UCP + if (utf) + { + /* Match characters up to the end of the reference. NOTE: the number of + data units matched may differ, because in UTF-8 there are some characters + whose upper and lower case versions code have different numbers of bytes. + For example, U+023A (2 bytes in UTF-8) is the upper case version of U+2C65 + (3 bytes in UTF-8); a sequence of 3 of the former uses 6 bytes, as does a + sequence of two of the latter. It is important, therefore, to check the + length along the reference, not along the subject (earlier code did this + wrong). */ + + PCRE_PUCHAR endptr = p + length; + while (p < endptr) + { + pcre_uint32 c, d; + const ucd_record *ur; + if (eptr >= md->end_subject) return -2; /* Partial match */ + GETCHARINC(c, eptr); + GETCHARINC(d, p); + ur = GET_UCD(d); + if (c != d && c != d + ur->other_case) + { + const pcre_uint32 *pp = PRIV(ucd_caseless_sets) + ur->caseset; + for (;;) + { + if (c < *pp) return -1; + if (c == *pp++) break; + } + } + } + } + else +#endif + + /* The same code works when not in UTF-8 mode and in UTF-8 mode when there + is no UCP support. */ + { + while (length-- > 0) + { + pcre_uint32 cc, cp; + if (eptr >= md->end_subject) return -2; /* Partial match */ + cc = UCHAR21TEST(eptr); + cp = UCHAR21TEST(p); + if (TABLE_GET(cp, md->lcc, cp) != TABLE_GET(cc, md->lcc, cc)) return -1; + p++; + eptr++; + } + } + } + +/* In the caseful case, we can just compare the bytes, whether or not we +are in UTF-8 mode. */ + +else + { + while (length-- > 0) + { + if (eptr >= md->end_subject) return -2; /* Partial match */ + if (UCHAR21INCTEST(p) != UCHAR21INCTEST(eptr)) return -1; + } + } + +return (int)(eptr - eptr_start); +} + + + +/*************************************************************************** +**************************************************************************** + RECURSION IN THE match() FUNCTION + +The match() function is highly recursive, though not every recursive call +increases the recursive depth. Nevertheless, some regular expressions can cause +it to recurse to a great depth. I was writing for Unix, so I just let it call +itself recursively. This uses the stack for saving everything that has to be +saved for a recursive call. On Unix, the stack can be large, and this works +fine. + +It turns out that on some non-Unix-like systems there are problems with +programs that use a lot of stack. (This despite the fact that every last chip +has oodles of memory these days, and techniques for extending the stack have +been known for decades.) So.... + +There is a fudge, triggered by defining NO_RECURSE, which avoids recursive +calls by keeping local variables that need to be preserved in blocks of memory +obtained from malloc() instead instead of on the stack. Macros are used to +achieve this so that the actual code doesn't look very different to what it +always used to. + +The original heap-recursive code used longjmp(). However, it seems that this +can be very slow on some operating systems. Following a suggestion from Stan +Switzer, the use of longjmp() has been abolished, at the cost of having to +provide a unique number for each call to RMATCH. There is no way of generating +a sequence of numbers at compile time in C. I have given them names, to make +them stand out more clearly. + +Crude tests on x86 Linux show a small speedup of around 5-8%. However, on +FreeBSD, avoiding longjmp() more than halves the time taken to run the standard +tests. Furthermore, not using longjmp() means that local dynamic variables +don't have indeterminate values; this has meant that the frame size can be +reduced because the result can be "passed back" by straight setting of the +variable instead of being passed in the frame. +**************************************************************************** +***************************************************************************/ + +/* Numbers for RMATCH calls. When this list is changed, the code at HEAP_RETURN +below must be updated in sync. */ + +enum { RM1=1, RM2, RM3, RM4, RM5, RM6, RM7, RM8, RM9, RM10, + RM11, RM12, RM13, RM14, RM15, RM16, RM17, RM18, RM19, RM20, + RM21, RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30, + RM31, RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40, + RM41, RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50, + RM51, RM52, RM53, RM54, RM55, RM56, RM57, RM58, RM59, RM60, + RM61, RM62, RM63, RM64, RM65, RM66, RM67 }; + +/* These versions of the macros use the stack, as normal. There are debugging +versions and production versions. Note that the "rw" argument of RMATCH isn't +actually used in this definition. */ + +#ifndef NO_RECURSE +#define REGISTER register + +#ifdef PCRE_DEBUG +#define RMATCH(ra,rb,rc,rd,re,rw) \ + { \ + printf("match() called in line %d\n", __LINE__); \ + rrc = match(ra,rb,mstart,rc,rd,re,rdepth+1); \ + printf("to line %d\n", __LINE__); \ + } +#define RRETURN(ra) \ + { \ + printf("match() returned %d from line %d\n", ra, __LINE__); \ + return ra; \ + } +#else +#define RMATCH(ra,rb,rc,rd,re,rw) \ + rrc = match(ra,rb,mstart,rc,rd,re,rdepth+1) +#define RRETURN(ra) return ra +#endif + +#else + + +/* These versions of the macros manage a private stack on the heap. Note that +the "rd" argument of RMATCH isn't actually used in this definition. It's the md +argument of match(), which never changes. */ + +#define REGISTER + +#define RMATCH(ra,rb,rc,rd,re,rw)\ + {\ + heapframe *newframe = frame->Xnextframe;\ + if (newframe == NULL)\ + {\ + newframe = (heapframe *)(PUBL(stack_malloc))(sizeof(heapframe));\ + if (newframe == NULL) RRETURN(PCRE_ERROR_NOMEMORY);\ + newframe->Xnextframe = NULL;\ + frame->Xnextframe = newframe;\ + }\ + frame->Xwhere = rw;\ + newframe->Xeptr = ra;\ + newframe->Xecode = rb;\ + newframe->Xmstart = mstart;\ + newframe->Xoffset_top = rc;\ + newframe->Xeptrb = re;\ + newframe->Xrdepth = frame->Xrdepth + 1;\ + newframe->Xprevframe = frame;\ + frame = newframe;\ + DPRINTF(("restarting from line %d\n", __LINE__));\ + goto HEAP_RECURSE;\ + L_##rw:\ + DPRINTF(("jumped back to line %d\n", __LINE__));\ + } + +#define RRETURN(ra)\ + {\ + heapframe *oldframe = frame;\ + frame = oldframe->Xprevframe;\ + if (frame != NULL)\ + {\ + rrc = ra;\ + goto HEAP_RETURN;\ + }\ + return ra;\ + } + + +/* Structure for remembering the local variables in a private frame */ + +typedef struct heapframe { + struct heapframe *Xprevframe; + struct heapframe *Xnextframe; + + /* Function arguments that may change */ + + PCRE_PUCHAR Xeptr; + const pcre_uchar *Xecode; + PCRE_PUCHAR Xmstart; + int Xoffset_top; + eptrblock *Xeptrb; + unsigned int Xrdepth; + + /* Function local variables */ + + PCRE_PUCHAR Xcallpat; +#ifdef SUPPORT_UTF + PCRE_PUCHAR Xcharptr; +#endif + PCRE_PUCHAR Xdata; + PCRE_PUCHAR Xnext; + PCRE_PUCHAR Xpp; + PCRE_PUCHAR Xprev; + PCRE_PUCHAR Xsaved_eptr; + + recursion_info Xnew_recursive; + + BOOL Xcur_is_word; + BOOL Xcondition; + BOOL Xprev_is_word; + +#ifdef SUPPORT_UCP + int Xprop_type; + unsigned int Xprop_value; + int Xprop_fail_result; + int Xoclength; + pcre_uchar Xocchars[6]; +#endif + + int Xcodelink; + int Xctype; + unsigned int Xfc; + int Xfi; + int Xlength; + int Xmax; + int Xmin; + unsigned int Xnumber; + int Xoffset; + unsigned int Xop; + pcre_int32 Xsave_capture_last; + int Xsave_offset1, Xsave_offset2, Xsave_offset3; + int Xstacksave[REC_STACK_SAVE_MAX]; + + eptrblock Xnewptrb; + + /* Where to jump back to */ + + int Xwhere; + +} heapframe; + +#endif + + +/*************************************************************************** +***************************************************************************/ + + + +/************************************************* +* Match from current position * +*************************************************/ + +/* This function is called recursively in many circumstances. Whenever it +returns a negative (error) response, the outer incarnation must also return the +same response. */ + +/* These macros pack up tests that are used for partial matching, and which +appear several times in the code. We set the "hit end" flag if the pointer is +at the end of the subject and also past the start of the subject (i.e. +something has been matched). For hard partial matching, we then return +immediately. The second one is used when we already know we are past the end of +the subject. */ + +#define CHECK_PARTIAL()\ + if (md->partial != 0 && eptr >= md->end_subject && \ + eptr > md->start_used_ptr) \ + { \ + md->hitend = TRUE; \ + if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); \ + } + +#define SCHECK_PARTIAL()\ + if (md->partial != 0 && eptr > md->start_used_ptr) \ + { \ + md->hitend = TRUE; \ + if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); \ + } + + +/* Performance note: It might be tempting to extract commonly used fields from +the md structure (e.g. utf, end_subject) into individual variables to improve +performance. Tests using gcc on a SPARC disproved this; in the first case, it +made performance worse. + +Arguments: + eptr pointer to current character in subject + ecode pointer to current position in compiled code + mstart pointer to the current match start position (can be modified + by encountering \K) + offset_top current top pointer + md pointer to "static" info for the match + eptrb pointer to chain of blocks containing eptr at start of + brackets - for testing for empty matches + rdepth the recursion depth + +Returns: MATCH_MATCH if matched ) these values are >= 0 + MATCH_NOMATCH if failed to match ) + a negative MATCH_xxx value for PRUNE, SKIP, etc + a negative PCRE_ERROR_xxx value if aborted by an error condition + (e.g. stopped by repeated call or recursion limit) +*/ + +static int +match(REGISTER PCRE_PUCHAR eptr, REGISTER const pcre_uchar *ecode, + PCRE_PUCHAR mstart, int offset_top, match_data *md, eptrblock *eptrb, + unsigned int rdepth) +{ +/* These variables do not need to be preserved over recursion in this function, +so they can be ordinary variables in all cases. Mark some of them with +"register" because they are used a lot in loops. */ + +register int rrc; /* Returns from recursive calls */ +register int i; /* Used for loops not involving calls to RMATCH() */ +register pcre_uint32 c; /* Character values not kept over RMATCH() calls */ +register BOOL utf; /* Local copy of UTF flag for speed */ + +BOOL minimize, possessive; /* Quantifier options */ +BOOL caseless; +int condcode; + +/* When recursion is not being used, all "local" variables that have to be +preserved over calls to RMATCH() are part of a "frame". We set up the top-level +frame on the stack here; subsequent instantiations are obtained from the heap +whenever RMATCH() does a "recursion". See the macro definitions above. Putting +the top-level on the stack rather than malloc-ing them all gives a performance +boost in many cases where there is not much "recursion". */ + +#ifdef NO_RECURSE +heapframe *frame = (heapframe *)md->match_frames_base; + +/* Copy in the original argument variables */ + +frame->Xeptr = eptr; +frame->Xecode = ecode; +frame->Xmstart = mstart; +frame->Xoffset_top = offset_top; +frame->Xeptrb = eptrb; +frame->Xrdepth = rdepth; + +/* This is where control jumps back to to effect "recursion" */ + +HEAP_RECURSE: + +/* Macros make the argument variables come from the current frame */ + +#define eptr frame->Xeptr +#define ecode frame->Xecode +#define mstart frame->Xmstart +#define offset_top frame->Xoffset_top +#define eptrb frame->Xeptrb +#define rdepth frame->Xrdepth + +/* Ditto for the local variables */ + +#ifdef SUPPORT_UTF +#define charptr frame->Xcharptr +#endif +#define callpat frame->Xcallpat +#define codelink frame->Xcodelink +#define data frame->Xdata +#define next frame->Xnext +#define pp frame->Xpp +#define prev frame->Xprev +#define saved_eptr frame->Xsaved_eptr + +#define new_recursive frame->Xnew_recursive + +#define cur_is_word frame->Xcur_is_word +#define condition frame->Xcondition +#define prev_is_word frame->Xprev_is_word + +#ifdef SUPPORT_UCP +#define prop_type frame->Xprop_type +#define prop_value frame->Xprop_value +#define prop_fail_result frame->Xprop_fail_result +#define oclength frame->Xoclength +#define occhars frame->Xocchars +#endif + +#define ctype frame->Xctype +#define fc frame->Xfc +#define fi frame->Xfi +#define length frame->Xlength +#define max frame->Xmax +#define min frame->Xmin +#define number frame->Xnumber +#define offset frame->Xoffset +#define op frame->Xop +#define save_capture_last frame->Xsave_capture_last +#define save_offset1 frame->Xsave_offset1 +#define save_offset2 frame->Xsave_offset2 +#define save_offset3 frame->Xsave_offset3 +#define stacksave frame->Xstacksave + +#define newptrb frame->Xnewptrb + +/* When recursion is being used, local variables are allocated on the stack and +get preserved during recursion in the normal way. In this environment, fi and +i, and fc and c, can be the same variables. */ + +#else /* NO_RECURSE not defined */ +#define fi i +#define fc c + +/* Many of the following variables are used only in small blocks of the code. +My normal style of coding would have declared them within each of those blocks. +However, in order to accommodate the version of this code that uses an external +"stack" implemented on the heap, it is easier to declare them all here, so the +declarations can be cut out in a block. The only declarations within blocks +below are for variables that do not have to be preserved over a recursive call +to RMATCH(). */ + +#ifdef SUPPORT_UTF +const pcre_uchar *charptr; +#endif +const pcre_uchar *callpat; +const pcre_uchar *data; +const pcre_uchar *next; +PCRE_PUCHAR pp; +const pcre_uchar *prev; +PCRE_PUCHAR saved_eptr; + +recursion_info new_recursive; + +BOOL cur_is_word; +BOOL condition; +BOOL prev_is_word; + +#ifdef SUPPORT_UCP +int prop_type; +unsigned int prop_value; +int prop_fail_result; +int oclength; +pcre_uchar occhars[6]; +#endif + +int codelink; +int ctype; +int length; +int max; +int min; +unsigned int number; +int offset; +unsigned int op; +pcre_int32 save_capture_last; +int save_offset1, save_offset2, save_offset3; +int stacksave[REC_STACK_SAVE_MAX]; + +eptrblock newptrb; + +/* There is a special fudge for calling match() in a way that causes it to +measure the size of its basic stack frame when the stack is being used for +recursion. The second argument (ecode) being NULL triggers this behaviour. It +cannot normally ever be NULL. The return is the negated value of the frame +size. */ + +if (ecode == NULL) + { + if (rdepth == 0) + return match((PCRE_PUCHAR)&rdepth, NULL, NULL, 0, NULL, NULL, 1); + else + { + int len = (int)((char *)&rdepth - (char *)eptr); + return (len > 0)? -len : len; + } + } +#endif /* NO_RECURSE */ + +/* To save space on the stack and in the heap frame, I have doubled up on some +of the local variables that are used only in localised parts of the code, but +still need to be preserved over recursive calls of match(). These macros define +the alternative names that are used. */ + +#define allow_zero cur_is_word +#define cbegroup condition +#define code_offset codelink +#define condassert condition +#define matched_once prev_is_word +#define foc number +#define save_mark data + +/* These statements are here to stop the compiler complaining about unitialized +variables. */ + +#ifdef SUPPORT_UCP +prop_value = 0; +prop_fail_result = 0; +#endif + + +/* This label is used for tail recursion, which is used in a few cases even +when NO_RECURSE is not defined, in order to reduce the amount of stack that is +used. Thanks to Ian Taylor for noticing this possibility and sending the +original patch. */ + +TAIL_RECURSE: + +/* OK, now we can get on with the real code of the function. Recursive calls +are specified by the macro RMATCH and RRETURN is used to return. When +NO_RECURSE is *not* defined, these just turn into a recursive call to match() +and a "return", respectively (possibly with some debugging if PCRE_DEBUG is +defined). However, RMATCH isn't like a function call because it's quite a +complicated macro. It has to be used in one particular way. This shouldn't, +however, impact performance when true recursion is being used. */ + +#ifdef SUPPORT_UTF +utf = md->utf; /* Local copy of the flag */ +#else +utf = FALSE; +#endif + +/* First check that we haven't called match() too many times, or that we +haven't exceeded the recursive call limit. */ + +if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT); +if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT); + +/* At the start of a group with an unlimited repeat that may match an empty +string, the variable md->match_function_type is set to MATCH_CBEGROUP. It is +done this way to save having to use another function argument, which would take +up space on the stack. See also MATCH_CONDASSERT below. + +When MATCH_CBEGROUP is set, add the current subject pointer to the chain of +such remembered pointers, to be checked when we hit the closing ket, in order +to break infinite loops that match no characters. When match() is called in +other circumstances, don't add to the chain. The MATCH_CBEGROUP feature must +NOT be used with tail recursion, because the memory block that is used is on +the stack, so a new one may be required for each match(). */ + +if (md->match_function_type == MATCH_CBEGROUP) + { + newptrb.epb_saved_eptr = eptr; + newptrb.epb_prev = eptrb; + eptrb = &newptrb; + md->match_function_type = 0; + } + +/* Now start processing the opcodes. */ + +for (;;) + { + minimize = possessive = FALSE; + op = *ecode; + + switch(op) + { + case OP_MARK: + md->nomatch_mark = ecode + 2; + md->mark = NULL; /* In case previously set by assertion */ + RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md, + eptrb, RM55); + if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT || rrc == MATCH_KETRPOS) && + md->mark == NULL) md->mark = ecode + 2; + + /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an + argument, and we must check whether that argument matches this MARK's + argument. It is passed back in md->start_match_ptr (an overloading of that + variable). If it does match, we reset that variable to the current subject + position and return MATCH_SKIP. Otherwise, pass back the return code + unaltered. */ + + else if (rrc == MATCH_SKIP_ARG && + STRCMP_UC_UC_TEST(ecode + 2, md->start_match_ptr) == 0) + { + md->start_match_ptr = eptr; + RRETURN(MATCH_SKIP); + } + RRETURN(rrc); + + case OP_FAIL: + RRETURN(MATCH_NOMATCH); + + case OP_COMMIT: + RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md, + eptrb, RM52); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + RRETURN(MATCH_COMMIT); + + case OP_PRUNE: + RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md, + eptrb, RM51); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + RRETURN(MATCH_PRUNE); + + case OP_PRUNE_ARG: + md->nomatch_mark = ecode + 2; + md->mark = NULL; /* In case previously set by assertion */ + RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md, + eptrb, RM56); + if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) && + md->mark == NULL) md->mark = ecode + 2; + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + RRETURN(MATCH_PRUNE); + + case OP_SKIP: + RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md, + eptrb, RM53); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + md->start_match_ptr = eptr; /* Pass back current position */ + RRETURN(MATCH_SKIP); + + /* Note that, for Perl compatibility, SKIP with an argument does NOT set + nomatch_mark. When a pattern match ends with a SKIP_ARG for which there was + not a matching mark, we have to re-run the match, ignoring the SKIP_ARG + that failed and any that precede it (either they also failed, or were not + triggered). To do this, we maintain a count of executed SKIP_ARGs. If a + SKIP_ARG gets to top level, the match is re-run with md->ignore_skip_arg + set to the count of the one that failed. */ + + case OP_SKIP_ARG: + md->skip_arg_count++; + if (md->skip_arg_count <= md->ignore_skip_arg) + { + ecode += PRIV(OP_lengths)[*ecode] + ecode[1]; + break; + } + RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md, + eptrb, RM57); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + + /* Pass back the current skip name by overloading md->start_match_ptr and + returning the special MATCH_SKIP_ARG return code. This will either be + caught by a matching MARK, or get to the top, where it causes a rematch + with md->ignore_skip_arg set to the value of md->skip_arg_count. */ + + md->start_match_ptr = ecode + 2; + RRETURN(MATCH_SKIP_ARG); + + /* For THEN (and THEN_ARG) we pass back the address of the opcode, so that + the branch in which it occurs can be determined. Overload the start of + match pointer to do this. */ + + case OP_THEN: + RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md, + eptrb, RM54); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + md->start_match_ptr = ecode; + RRETURN(MATCH_THEN); + + case OP_THEN_ARG: + md->nomatch_mark = ecode + 2; + md->mark = NULL; /* In case previously set by assertion */ + RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, + md, eptrb, RM58); + if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) && + md->mark == NULL) md->mark = ecode + 2; + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + md->start_match_ptr = ecode; + RRETURN(MATCH_THEN); + + /* Handle an atomic group that does not contain any capturing parentheses. + This can be handled like an assertion. Prior to 8.13, all atomic groups + were handled this way. In 8.13, the code was changed as below for ONCE, so + that backups pass through the group and thereby reset captured values. + However, this uses a lot more stack, so in 8.20, atomic groups that do not + contain any captures generate OP_ONCE_NC, which can be handled in the old, + less stack intensive way. + + Check the alternative branches in turn - the matching won't pass the KET + for this kind of subpattern. If any one branch matches, we carry on as at + the end of a normal bracket, leaving the subject pointer, but resetting + the start-of-match value in case it was changed by \K. */ + + case OP_ONCE_NC: + prev = ecode; + saved_eptr = eptr; + save_mark = md->mark; + do + { + RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM64); + if (rrc == MATCH_MATCH) /* Note: _not_ MATCH_ACCEPT */ + { + mstart = md->start_match_ptr; + break; + } + if (rrc == MATCH_THEN) + { + next = ecode + GET(ecode,1); + if (md->start_match_ptr < next && + (*ecode == OP_ALT || *next == OP_ALT)) + rrc = MATCH_NOMATCH; + } + + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + ecode += GET(ecode,1); + md->mark = save_mark; + } + while (*ecode == OP_ALT); + + /* If hit the end of the group (which could be repeated), fail */ + + if (*ecode != OP_ONCE_NC && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH); + + /* Continue as from after the group, updating the offsets high water + mark, since extracts may have been taken. */ + + do ecode += GET(ecode, 1); while (*ecode == OP_ALT); + + offset_top = md->end_offset_top; + eptr = md->end_match_ptr; + + /* For a non-repeating ket, just continue at this level. This also + happens for a repeating ket if no characters were matched in the group. + This is the forcible breaking of infinite loops as implemented in Perl + 5.005. */ + + if (*ecode == OP_KET || eptr == saved_eptr) + { + ecode += 1+LINK_SIZE; + break; + } + + /* The repeating kets try the rest of the pattern or restart from the + preceding bracket, in the appropriate order. The second "call" of match() + uses tail recursion, to avoid using another stack frame. */ + + if (*ecode == OP_KETRMIN) + { + RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM65); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + ecode = prev; + goto TAIL_RECURSE; + } + else /* OP_KETRMAX */ + { + RMATCH(eptr, prev, offset_top, md, eptrb, RM66); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + ecode += 1 + LINK_SIZE; + goto TAIL_RECURSE; + } + /* Control never gets here */ + + /* Handle a capturing bracket, other than those that are possessive with an + unlimited repeat. If there is space in the offset vector, save the current + subject position in the working slot at the top of the vector. We mustn't + change the current values of the data slot, because they may be set from a + previous iteration of this group, and be referred to by a reference inside + the group. A failure to match might occur after the group has succeeded, + if something later on doesn't match. For this reason, we need to restore + the working value and also the values of the final offsets, in case they + were set by a previous iteration of the same bracket. + + If there isn't enough space in the offset vector, treat this as if it were + a non-capturing bracket. Don't worry about setting the flag for the error + case here; that is handled in the code for KET. */ + + case OP_CBRA: + case OP_SCBRA: + number = GET2(ecode, 1+LINK_SIZE); + offset = number << 1; + +#ifdef PCRE_DEBUG + printf("start bracket %d\n", number); + printf("subject="); + pchars(eptr, 16, TRUE, md); + printf("\n"); +#endif + + if (offset < md->offset_max) + { + save_offset1 = md->offset_vector[offset]; + save_offset2 = md->offset_vector[offset+1]; + save_offset3 = md->offset_vector[md->offset_end - number]; + save_capture_last = md->capture_last; + save_mark = md->mark; + + DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3)); + md->offset_vector[md->offset_end - number] = + (int)(eptr - md->start_subject); + + for (;;) + { + if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP; + RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md, + eptrb, RM1); + if (rrc == MATCH_ONCE) break; /* Backing up through an atomic group */ + + /* If we backed up to a THEN, check whether it is within the current + branch by comparing the address of the THEN that is passed back with + the end of the branch. If it is within the current branch, and the + branch is one of two or more alternatives (it either starts or ends + with OP_ALT), we have reached the limit of THEN's action, so convert + the return code to NOMATCH, which will cause normal backtracking to + happen from now on. Otherwise, THEN is passed back to an outer + alternative. This implements Perl's treatment of parenthesized groups, + where a group not containing | does not affect the current alternative, + that is, (X) is NOT the same as (X|(*F)). */ + + if (rrc == MATCH_THEN) + { + next = ecode + GET(ecode,1); + if (md->start_match_ptr < next && + (*ecode == OP_ALT || *next == OP_ALT)) + rrc = MATCH_NOMATCH; + } + + /* Anything other than NOMATCH is passed back. */ + + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + md->capture_last = save_capture_last; + ecode += GET(ecode, 1); + md->mark = save_mark; + if (*ecode != OP_ALT) break; + } + + DPRINTF(("bracket %d failed\n", number)); + md->offset_vector[offset] = save_offset1; + md->offset_vector[offset+1] = save_offset2; + md->offset_vector[md->offset_end - number] = save_offset3; + + /* At this point, rrc will be one of MATCH_ONCE or MATCH_NOMATCH. */ + + RRETURN(rrc); + } + + /* FALL THROUGH ... Insufficient room for saving captured contents. Treat + as a non-capturing bracket. */ + + /* VVVVVVVVVVVVVVVVVVVVVVVVV */ + /* VVVVVVVVVVVVVVVVVVVVVVVVV */ + + DPRINTF(("insufficient capture room: treat as non-capturing\n")); + + /* VVVVVVVVVVVVVVVVVVVVVVVVV */ + /* VVVVVVVVVVVVVVVVVVVVVVVVV */ + + /* Non-capturing or atomic group, except for possessive with unlimited + repeat and ONCE group with no captures. Loop for all the alternatives. + + When we get to the final alternative within the brackets, we used to return + the result of a recursive call to match() whatever happened so it was + possible to reduce stack usage by turning this into a tail recursion, + except in the case of a possibly empty group. However, now that there is + the possiblity of (*THEN) occurring in the final alternative, this + optimization is no longer always possible. + + We can optimize if we know there are no (*THEN)s in the pattern; at present + this is the best that can be done. + + MATCH_ONCE is returned when the end of an atomic group is successfully + reached, but subsequent matching fails. It passes back up the tree (causing + captured values to be reset) until the original atomic group level is + reached. This is tested by comparing md->once_target with the start of the + group. At this point, the return is converted into MATCH_NOMATCH so that + previous backup points can be taken. */ + + case OP_ONCE: + case OP_BRA: + case OP_SBRA: + DPRINTF(("start non-capturing bracket\n")); + + for (;;) + { + if (op >= OP_SBRA || op == OP_ONCE) + md->match_function_type = MATCH_CBEGROUP; + + /* If this is not a possibly empty group, and there are no (*THEN)s in + the pattern, and this is the final alternative, optimize as described + above. */ + + else if (!md->hasthen && ecode[GET(ecode, 1)] != OP_ALT) + { + ecode += PRIV(OP_lengths)[*ecode]; + goto TAIL_RECURSE; + } + + /* In all other cases, we have to make another call to match(). */ + + save_mark = md->mark; + save_capture_last = md->capture_last; + RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md, eptrb, + RM2); + + /* See comment in the code for capturing groups above about handling + THEN. */ + + if (rrc == MATCH_THEN) + { + next = ecode + GET(ecode,1); + if (md->start_match_ptr < next && + (*ecode == OP_ALT || *next == OP_ALT)) + rrc = MATCH_NOMATCH; + } + + if (rrc != MATCH_NOMATCH) + { + if (rrc == MATCH_ONCE) + { + const pcre_uchar *scode = ecode; + if (*scode != OP_ONCE) /* If not at start, find it */ + { + while (*scode == OP_ALT) scode += GET(scode, 1); + scode -= GET(scode, 1); + } + if (md->once_target == scode) rrc = MATCH_NOMATCH; + } + RRETURN(rrc); + } + ecode += GET(ecode, 1); + md->mark = save_mark; + if (*ecode != OP_ALT) break; + md->capture_last = save_capture_last; + } + + RRETURN(MATCH_NOMATCH); + + /* Handle possessive capturing brackets with an unlimited repeat. We come + here from BRAZERO with allow_zero set TRUE. The offset_vector values are + handled similarly to the normal case above. However, the matching is + different. The end of these brackets will always be OP_KETRPOS, which + returns MATCH_KETRPOS without going further in the pattern. By this means + we can handle the group by iteration rather than recursion, thereby + reducing the amount of stack needed. */ + + case OP_CBRAPOS: + case OP_SCBRAPOS: + allow_zero = FALSE; + + POSSESSIVE_CAPTURE: + number = GET2(ecode, 1+LINK_SIZE); + offset = number << 1; + +#ifdef PCRE_DEBUG + printf("start possessive bracket %d\n", number); + printf("subject="); + pchars(eptr, 16, TRUE, md); + printf("\n"); +#endif + + if (offset >= md->offset_max) goto POSSESSIVE_NON_CAPTURE; + + matched_once = FALSE; + code_offset = (int)(ecode - md->start_code); + + save_offset1 = md->offset_vector[offset]; + save_offset2 = md->offset_vector[offset+1]; + save_offset3 = md->offset_vector[md->offset_end - number]; + save_capture_last = md->capture_last; + + DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3)); + + /* Each time round the loop, save the current subject position for use + when the group matches. For MATCH_MATCH, the group has matched, so we + restart it with a new subject starting position, remembering that we had + at least one match. For MATCH_NOMATCH, carry on with the alternatives, as + usual. If we haven't matched any alternatives in any iteration, check to + see if a previous iteration matched. If so, the group has matched; + continue from afterwards. Otherwise it has failed; restore the previous + capture values before returning NOMATCH. */ + + for (;;) + { + md->offset_vector[md->offset_end - number] = + (int)(eptr - md->start_subject); + if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP; + RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md, + eptrb, RM63); + if (rrc == MATCH_KETRPOS) + { + offset_top = md->end_offset_top; + ecode = md->start_code + code_offset; + save_capture_last = md->capture_last; + matched_once = TRUE; + mstart = md->start_match_ptr; /* In case \K changed it */ + if (eptr == md->end_match_ptr) /* Matched an empty string */ + { + do ecode += GET(ecode, 1); while (*ecode == OP_ALT); + break; + } + eptr = md->end_match_ptr; + continue; + } + + /* See comment in the code for capturing groups above about handling + THEN. */ + + if (rrc == MATCH_THEN) + { + next = ecode + GET(ecode,1); + if (md->start_match_ptr < next && + (*ecode == OP_ALT || *next == OP_ALT)) + rrc = MATCH_NOMATCH; + } + + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + md->capture_last = save_capture_last; + ecode += GET(ecode, 1); + if (*ecode != OP_ALT) break; + } + + if (!matched_once) + { + md->offset_vector[offset] = save_offset1; + md->offset_vector[offset+1] = save_offset2; + md->offset_vector[md->offset_end - number] = save_offset3; + } + + if (allow_zero || matched_once) + { + ecode += 1 + LINK_SIZE; + break; + } + + RRETURN(MATCH_NOMATCH); + + /* Non-capturing possessive bracket with unlimited repeat. We come here + from BRAZERO with allow_zero = TRUE. The code is similar to the above, + without the capturing complication. It is written out separately for speed + and cleanliness. */ + + case OP_BRAPOS: + case OP_SBRAPOS: + allow_zero = FALSE; + + POSSESSIVE_NON_CAPTURE: + matched_once = FALSE; + code_offset = (int)(ecode - md->start_code); + save_capture_last = md->capture_last; + + for (;;) + { + if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP; + RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md, + eptrb, RM48); + if (rrc == MATCH_KETRPOS) + { + offset_top = md->end_offset_top; + ecode = md->start_code + code_offset; + matched_once = TRUE; + mstart = md->start_match_ptr; /* In case \K reset it */ + if (eptr == md->end_match_ptr) /* Matched an empty string */ + { + do ecode += GET(ecode, 1); while (*ecode == OP_ALT); + break; + } + eptr = md->end_match_ptr; + continue; + } + + /* See comment in the code for capturing groups above about handling + THEN. */ + + if (rrc == MATCH_THEN) + { + next = ecode + GET(ecode,1); + if (md->start_match_ptr < next && + (*ecode == OP_ALT || *next == OP_ALT)) + rrc = MATCH_NOMATCH; + } + + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + ecode += GET(ecode, 1); + if (*ecode != OP_ALT) break; + md->capture_last = save_capture_last; + } + + if (matched_once || allow_zero) + { + ecode += 1 + LINK_SIZE; + break; + } + RRETURN(MATCH_NOMATCH); + + /* Control never reaches here. */ + + /* Conditional group: compilation checked that there are no more than two + branches. If the condition is false, skipping the first branch takes us + past the end of the item if there is only one branch, but that's exactly + what we want. */ + + case OP_COND: + case OP_SCOND: + + /* The variable codelink will be added to ecode when the condition is + false, to get to the second branch. Setting it to the offset to the ALT + or KET, then incrementing ecode achieves this effect. We now have ecode + pointing to the condition or callout. */ + + codelink = GET(ecode, 1); /* Offset to the second branch */ + ecode += 1 + LINK_SIZE; /* From this opcode */ + + /* Because of the way auto-callout works during compile, a callout item is + inserted between OP_COND and an assertion condition. */ + + if (*ecode == OP_CALLOUT) + { + if (PUBL(callout) != NULL) + { + PUBL(callout_block) cb; + cb.version = 2; /* Version 1 of the callout block */ + cb.callout_number = ecode[1]; + cb.offset_vector = md->offset_vector; +#if defined COMPILE_PCRE8 + cb.subject = (PCRE_SPTR)md->start_subject; +#elif defined COMPILE_PCRE16 + cb.subject = (PCRE_SPTR16)md->start_subject; +#elif defined COMPILE_PCRE32 + cb.subject = (PCRE_SPTR32)md->start_subject; +#endif + cb.subject_length = (int)(md->end_subject - md->start_subject); + cb.start_match = (int)(mstart - md->start_subject); + cb.current_position = (int)(eptr - md->start_subject); + cb.pattern_position = GET(ecode, 2); + cb.next_item_length = GET(ecode, 2 + LINK_SIZE); + cb.capture_top = offset_top/2; + cb.capture_last = md->capture_last & CAPLMASK; + /* Internal change requires this for API compatibility. */ + if (cb.capture_last == 0) cb.capture_last = -1; + cb.callout_data = md->callout_data; + cb.mark = md->nomatch_mark; + if ((rrc = (*PUBL(callout))(&cb)) > 0) RRETURN(MATCH_NOMATCH); + if (rrc < 0) RRETURN(rrc); + } + + /* Advance ecode past the callout, so it now points to the condition. We + must adjust codelink so that the value of ecode+codelink is unchanged. */ + + ecode += PRIV(OP_lengths)[OP_CALLOUT]; + codelink -= PRIV(OP_lengths)[OP_CALLOUT]; + } + + /* Test the various possible conditions */ + + condition = FALSE; + switch(condcode = *ecode) + { + case OP_RREF: /* Numbered group recursion test */ + if (md->recursive != NULL) /* Not recursing => FALSE */ + { + unsigned int recno = GET2(ecode, 1); /* Recursion group number*/ + condition = (recno == RREF_ANY || recno == md->recursive->group_num); + } + break; + + case OP_DNRREF: /* Duplicate named group recursion test */ + if (md->recursive != NULL) + { + int count = GET2(ecode, 1 + IMM2_SIZE); + pcre_uchar *slot = md->name_table + GET2(ecode, 1) * md->name_entry_size; + while (count-- > 0) + { + unsigned int recno = GET2(slot, 0); + condition = recno == md->recursive->group_num; + if (condition) break; + slot += md->name_entry_size; + } + } + break; + + case OP_CREF: /* Numbered group used test */ + offset = GET2(ecode, 1) << 1; /* Doubled ref number */ + condition = offset < offset_top && md->offset_vector[offset] >= 0; + break; + + case OP_DNCREF: /* Duplicate named group used test */ + { + int count = GET2(ecode, 1 + IMM2_SIZE); + pcre_uchar *slot = md->name_table + GET2(ecode, 1) * md->name_entry_size; + while (count-- > 0) + { + offset = GET2(slot, 0) << 1; + condition = offset < offset_top && md->offset_vector[offset] >= 0; + if (condition) break; + slot += md->name_entry_size; + } + } + break; + + case OP_DEF: /* DEFINE - always false */ + case OP_FAIL: /* From optimized (?!) condition */ + break; + + /* The condition is an assertion. Call match() to evaluate it - setting + md->match_function_type to MATCH_CONDASSERT causes it to stop at the end + of an assertion. */ + + default: + md->match_function_type = MATCH_CONDASSERT; + RMATCH(eptr, ecode, offset_top, md, NULL, RM3); + if (rrc == MATCH_MATCH) + { + if (md->end_offset_top > offset_top) + offset_top = md->end_offset_top; /* Captures may have happened */ + condition = TRUE; + + /* Advance ecode past the assertion to the start of the first branch, + but adjust it so that the general choosing code below works. If the + assertion has a quantifier that allows zero repeats we must skip over + the BRAZERO. This is a lunatic thing to do, but somebody did! */ + + if (*ecode == OP_BRAZERO) ecode++; + ecode += GET(ecode, 1); + while (*ecode == OP_ALT) ecode += GET(ecode, 1); + ecode += 1 + LINK_SIZE - PRIV(OP_lengths)[condcode]; + } + + /* PCRE doesn't allow the effect of (*THEN) to escape beyond an + assertion; it is therefore treated as NOMATCH. Any other return is an + error. */ + + else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) + { + RRETURN(rrc); /* Need braces because of following else */ + } + break; + } + + /* Choose branch according to the condition */ + + ecode += condition? PRIV(OP_lengths)[condcode] : codelink; + + /* We are now at the branch that is to be obeyed. As there is only one, we + can use tail recursion to avoid using another stack frame, except when + there is unlimited repeat of a possibly empty group. In the latter case, a + recursive call to match() is always required, unless the second alternative + doesn't exist, in which case we can just plough on. Note that, for + compatibility with Perl, the | in a conditional group is NOT treated as + creating two alternatives. If a THEN is encountered in the branch, it + propagates out to the enclosing alternative (unless nested in a deeper set + of alternatives, of course). */ + + if (condition || ecode[-(1+LINK_SIZE)] == OP_ALT) + { + if (op != OP_SCOND) + { + goto TAIL_RECURSE; + } + + md->match_function_type = MATCH_CBEGROUP; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM49); + RRETURN(rrc); + } + + /* Condition false & no alternative; continue after the group. */ + + else + { + } + break; + + + /* Before OP_ACCEPT there may be any number of OP_CLOSE opcodes, + to close any currently open capturing brackets. */ + + case OP_CLOSE: + number = GET2(ecode, 1); /* Must be less than 65536 */ + offset = number << 1; + +#ifdef PCRE_DEBUG + printf("end bracket %d at *ACCEPT", number); + printf("\n"); +#endif + + md->capture_last = (md->capture_last & OVFLMASK) | number; + if (offset >= md->offset_max) md->capture_last |= OVFLBIT; else + { + md->offset_vector[offset] = + md->offset_vector[md->offset_end - number]; + md->offset_vector[offset+1] = (int)(eptr - md->start_subject); + + /* If this group is at or above the current highwater mark, ensure that + any groups between the current high water mark and this group are marked + unset and then update the high water mark. */ + + if (offset >= offset_top) + { + register int *iptr = md->offset_vector + offset_top; + register int *iend = md->offset_vector + offset; + while (iptr < iend) *iptr++ = -1; + offset_top = offset + 2; + } + } + ecode += 1 + IMM2_SIZE; + break; + + + /* End of the pattern, either real or forced. */ + + case OP_END: + case OP_ACCEPT: + case OP_ASSERT_ACCEPT: + + /* If we have matched an empty string, fail if not in an assertion and not + in a recursion if either PCRE_NOTEMPTY is set, or if PCRE_NOTEMPTY_ATSTART + is set and we have matched at the start of the subject. In both cases, + backtracking will then try other alternatives, if any. */ + + if (eptr == mstart && op != OP_ASSERT_ACCEPT && + md->recursive == NULL && + (md->notempty || + (md->notempty_atstart && + mstart == md->start_subject + md->start_offset))) + RRETURN(MATCH_NOMATCH); + + /* Otherwise, we have a match. */ + + md->end_match_ptr = eptr; /* Record where we ended */ + md->end_offset_top = offset_top; /* and how many extracts were taken */ + md->start_match_ptr = mstart; /* and the start (\K can modify) */ + + /* For some reason, the macros don't work properly if an expression is + given as the argument to RRETURN when the heap is in use. */ + + rrc = (op == OP_END)? MATCH_MATCH : MATCH_ACCEPT; + RRETURN(rrc); + + /* Assertion brackets. Check the alternative branches in turn - the + matching won't pass the KET for an assertion. If any one branch matches, + the assertion is true. Lookbehind assertions have an OP_REVERSE item at the + start of each branch to move the current point backwards, so the code at + this level is identical to the lookahead case. When the assertion is part + of a condition, we want to return immediately afterwards. The caller of + this incarnation of the match() function will have set MATCH_CONDASSERT in + md->match_function type, and one of these opcodes will be the first opcode + that is processed. We use a local variable that is preserved over calls to + match() to remember this case. */ + + case OP_ASSERT: + case OP_ASSERTBACK: + save_mark = md->mark; + if (md->match_function_type == MATCH_CONDASSERT) + { + condassert = TRUE; + md->match_function_type = 0; + } + else condassert = FALSE; + + /* Loop for each branch */ + + do + { + RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM4); + + /* A match means that the assertion is true; break out of the loop + that matches its alternatives. */ + + if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) + { + mstart = md->start_match_ptr; /* In case \K reset it */ + break; + } + + /* If not matched, restore the previous mark setting. */ + + md->mark = save_mark; + + /* See comment in the code for capturing groups above about handling + THEN. */ + + if (rrc == MATCH_THEN) + { + next = ecode + GET(ecode,1); + if (md->start_match_ptr < next && + (*ecode == OP_ALT || *next == OP_ALT)) + rrc = MATCH_NOMATCH; + } + + /* Anything other than NOMATCH causes the entire assertion to fail, + passing back the return code. This includes COMMIT, SKIP, PRUNE and an + uncaptured THEN, which means they take their normal effect. This + consistent approach does not always have exactly the same effect as in + Perl. */ + + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + ecode += GET(ecode, 1); + } + while (*ecode == OP_ALT); /* Continue for next alternative */ + + /* If we have tried all the alternative branches, the assertion has + failed. If not, we broke out after a match. */ + + if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH); + + /* If checking an assertion for a condition, return MATCH_MATCH. */ + + if (condassert) RRETURN(MATCH_MATCH); + + /* Continue from after a successful assertion, updating the offsets high + water mark, since extracts may have been taken during the assertion. */ + + do ecode += GET(ecode,1); while (*ecode == OP_ALT); + ecode += 1 + LINK_SIZE; + offset_top = md->end_offset_top; + continue; + + /* Negative assertion: all branches must fail to match for the assertion to + succeed. */ + + case OP_ASSERT_NOT: + case OP_ASSERTBACK_NOT: + save_mark = md->mark; + if (md->match_function_type == MATCH_CONDASSERT) + { + condassert = TRUE; + md->match_function_type = 0; + } + else condassert = FALSE; + + /* Loop for each alternative branch. */ + + do + { + RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM5); + md->mark = save_mark; /* Always restore the mark setting */ + + switch(rrc) + { + case MATCH_MATCH: /* A successful match means */ + case MATCH_ACCEPT: /* the assertion has failed. */ + RRETURN(MATCH_NOMATCH); + + case MATCH_NOMATCH: /* Carry on with next branch */ + break; + + /* See comment in the code for capturing groups above about handling + THEN. */ + + case MATCH_THEN: + next = ecode + GET(ecode,1); + if (md->start_match_ptr < next && + (*ecode == OP_ALT || *next == OP_ALT)) + { + rrc = MATCH_NOMATCH; + break; + } + /* Otherwise fall through. */ + + /* COMMIT, SKIP, PRUNE, and an uncaptured THEN cause the whole + assertion to fail to match, without considering any more alternatives. + Failing to match means the assertion is true. This is a consistent + approach, but does not always have the same effect as in Perl. */ + + case MATCH_COMMIT: + case MATCH_SKIP: + case MATCH_SKIP_ARG: + case MATCH_PRUNE: + do ecode += GET(ecode,1); while (*ecode == OP_ALT); + goto NEG_ASSERT_TRUE; /* Break out of alternation loop */ + + /* Anything else is an error */ + + default: + RRETURN(rrc); + } + + /* Continue with next branch */ + + ecode += GET(ecode,1); + } + while (*ecode == OP_ALT); + + /* All branches in the assertion failed to match. */ + + NEG_ASSERT_TRUE: + if (condassert) RRETURN(MATCH_MATCH); /* Condition assertion */ + ecode += 1 + LINK_SIZE; /* Continue with current branch */ + continue; + + /* Move the subject pointer back. This occurs only at the start of + each branch of a lookbehind assertion. If we are too close to the start to + move back, this match function fails. When working with UTF-8 we move + back a number of characters, not bytes. */ + + case OP_REVERSE: +#ifdef SUPPORT_UTF + if (utf) + { + i = GET(ecode, 1); + while (i-- > 0) + { + eptr--; + if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH); + BACKCHAR(eptr); + } + } + else +#endif + + /* No UTF-8 support, or not in UTF-8 mode: count is byte count */ + + { + eptr -= GET(ecode, 1); + if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH); + } + + /* Save the earliest consulted character, then skip to next op code */ + + if (eptr < md->start_used_ptr) md->start_used_ptr = eptr; + ecode += 1 + LINK_SIZE; + break; + + /* The callout item calls an external function, if one is provided, passing + details of the match so far. This is mainly for debugging, though the + function is able to force a failure. */ + + case OP_CALLOUT: + if (PUBL(callout) != NULL) + { + PUBL(callout_block) cb; + cb.version = 2; /* Version 1 of the callout block */ + cb.callout_number = ecode[1]; + cb.offset_vector = md->offset_vector; +#if defined COMPILE_PCRE8 + cb.subject = (PCRE_SPTR)md->start_subject; +#elif defined COMPILE_PCRE16 + cb.subject = (PCRE_SPTR16)md->start_subject; +#elif defined COMPILE_PCRE32 + cb.subject = (PCRE_SPTR32)md->start_subject; +#endif + cb.subject_length = (int)(md->end_subject - md->start_subject); + cb.start_match = (int)(mstart - md->start_subject); + cb.current_position = (int)(eptr - md->start_subject); + cb.pattern_position = GET(ecode, 2); + cb.next_item_length = GET(ecode, 2 + LINK_SIZE); + cb.capture_top = offset_top/2; + cb.capture_last = md->capture_last & CAPLMASK; + /* Internal change requires this for API compatibility. */ + if (cb.capture_last == 0) cb.capture_last = -1; + cb.callout_data = md->callout_data; + cb.mark = md->nomatch_mark; + if ((rrc = (*PUBL(callout))(&cb)) > 0) RRETURN(MATCH_NOMATCH); + if (rrc < 0) RRETURN(rrc); + } + ecode += 2 + 2*LINK_SIZE; + break; + + /* Recursion either matches the current regex, or some subexpression. The + offset data is the offset to the starting bracket from the start of the + whole pattern. (This is so that it works from duplicated subpatterns.) + + The state of the capturing groups is preserved over recursion, and + re-instated afterwards. We don't know how many are started and not yet + finished (offset_top records the completed total) so we just have to save + all the potential data. There may be up to 65535 such values, which is too + large to put on the stack, but using malloc for small numbers seems + expensive. As a compromise, the stack is used when there are no more than + REC_STACK_SAVE_MAX values to store; otherwise malloc is used. + + There are also other values that have to be saved. We use a chained + sequence of blocks that actually live on the stack. Thanks to Robin Houston + for the original version of this logic. It has, however, been hacked around + a lot, so he is not to blame for the current way it works. */ + + case OP_RECURSE: + { + recursion_info *ri; + unsigned int recno; + + callpat = md->start_code + GET(ecode, 1); + recno = (callpat == md->start_code)? 0 : + GET2(callpat, 1 + LINK_SIZE); + + /* Check for repeating a recursion without advancing the subject pointer. + This should catch convoluted mutual recursions. (Some simple cases are + caught at compile time.) */ + + for (ri = md->recursive; ri != NULL; ri = ri->prevrec) + if (recno == ri->group_num && eptr == ri->subject_position) + RRETURN(PCRE_ERROR_RECURSELOOP); + + /* Add to "recursing stack" */ + + new_recursive.group_num = recno; + new_recursive.saved_capture_last = md->capture_last; + new_recursive.subject_position = eptr; + new_recursive.prevrec = md->recursive; + md->recursive = &new_recursive; + + /* Where to continue from afterwards */ + + ecode += 1 + LINK_SIZE; + + /* Now save the offset data */ + + new_recursive.saved_max = md->offset_end; + if (new_recursive.saved_max <= REC_STACK_SAVE_MAX) + new_recursive.offset_save = stacksave; + else + { + new_recursive.offset_save = + (int *)(PUBL(malloc))(new_recursive.saved_max * sizeof(int)); + if (new_recursive.offset_save == NULL) RRETURN(PCRE_ERROR_NOMEMORY); + } + memcpy(new_recursive.offset_save, md->offset_vector, + new_recursive.saved_max * sizeof(int)); + + /* OK, now we can do the recursion. After processing each alternative, + restore the offset data and the last captured value. If there were nested + recursions, md->recursive might be changed, so reset it before looping. + */ + + DPRINTF(("Recursing into group %d\n", new_recursive.group_num)); + cbegroup = (*callpat >= OP_SBRA); + do + { + if (cbegroup) md->match_function_type = MATCH_CBEGROUP; + RMATCH(eptr, callpat + PRIV(OP_lengths)[*callpat], offset_top, + md, eptrb, RM6); + memcpy(md->offset_vector, new_recursive.offset_save, + new_recursive.saved_max * sizeof(int)); + md->capture_last = new_recursive.saved_capture_last; + md->recursive = new_recursive.prevrec; + if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) + { + DPRINTF(("Recursion matched\n")); + if (new_recursive.offset_save != stacksave) + (PUBL(free))(new_recursive.offset_save); + + /* Set where we got to in the subject, and reset the start in case + it was changed by \K. This *is* propagated back out of a recursion, + for Perl compatibility. */ + + eptr = md->end_match_ptr; + mstart = md->start_match_ptr; + goto RECURSION_MATCHED; /* Exit loop; end processing */ + } + + /* PCRE does not allow THEN, SKIP, PRUNE or COMMIT to escape beyond a + recursion; they cause a NOMATCH for the entire recursion. These codes + are defined in a range that can be tested for. */ + + if (rrc >= MATCH_BACKTRACK_MIN && rrc <= MATCH_BACKTRACK_MAX) + { + if (new_recursive.offset_save != stacksave) + (PUBL(free))(new_recursive.offset_save); + RRETURN(MATCH_NOMATCH); + } + + /* Any return code other than NOMATCH is an error. */ + + if (rrc != MATCH_NOMATCH) + { + DPRINTF(("Recursion gave error %d\n", rrc)); + if (new_recursive.offset_save != stacksave) + (PUBL(free))(new_recursive.offset_save); + RRETURN(rrc); + } + + md->recursive = &new_recursive; + callpat += GET(callpat, 1); + } + while (*callpat == OP_ALT); + + DPRINTF(("Recursion didn't match\n")); + md->recursive = new_recursive.prevrec; + if (new_recursive.offset_save != stacksave) + (PUBL(free))(new_recursive.offset_save); + RRETURN(MATCH_NOMATCH); + } + + RECURSION_MATCHED: + break; + + /* An alternation is the end of a branch; scan along to find the end of the + bracketed group and go to there. */ + + case OP_ALT: + do ecode += GET(ecode,1); while (*ecode == OP_ALT); + break; + + /* BRAZERO, BRAMINZERO and SKIPZERO occur just before a bracket group, + indicating that it may occur zero times. It may repeat infinitely, or not + at all - i.e. it could be ()* or ()? or even (){0} in the pattern. Brackets + with fixed upper repeat limits are compiled as a number of copies, with the + optional ones preceded by BRAZERO or BRAMINZERO. */ + + case OP_BRAZERO: + next = ecode + 1; + RMATCH(eptr, next, offset_top, md, eptrb, RM10); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + do next += GET(next, 1); while (*next == OP_ALT); + ecode = next + 1 + LINK_SIZE; + break; + + case OP_BRAMINZERO: + next = ecode + 1; + do next += GET(next, 1); while (*next == OP_ALT); + RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, eptrb, RM11); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + ecode++; + break; + + case OP_SKIPZERO: + next = ecode+1; + do next += GET(next,1); while (*next == OP_ALT); + ecode = next + 1 + LINK_SIZE; + break; + + /* BRAPOSZERO occurs before a possessive bracket group. Don't do anything + here; just jump to the group, with allow_zero set TRUE. */ + + case OP_BRAPOSZERO: + op = *(++ecode); + allow_zero = TRUE; + if (op == OP_CBRAPOS || op == OP_SCBRAPOS) goto POSSESSIVE_CAPTURE; + goto POSSESSIVE_NON_CAPTURE; + + /* End of a group, repeated or non-repeating. */ + + case OP_KET: + case OP_KETRMIN: + case OP_KETRMAX: + case OP_KETRPOS: + prev = ecode - GET(ecode, 1); + + /* If this was a group that remembered the subject start, in order to break + infinite repeats of empty string matches, retrieve the subject start from + the chain. Otherwise, set it NULL. */ + + if (*prev >= OP_SBRA || *prev == OP_ONCE) + { + saved_eptr = eptrb->epb_saved_eptr; /* Value at start of group */ + eptrb = eptrb->epb_prev; /* Backup to previous group */ + } + else saved_eptr = NULL; + + /* If we are at the end of an assertion group or a non-capturing atomic + group, stop matching and return MATCH_MATCH, but record the current high + water mark for use by positive assertions. We also need to record the match + start in case it was changed by \K. */ + + if ((*prev >= OP_ASSERT && *prev <= OP_ASSERTBACK_NOT) || + *prev == OP_ONCE_NC) + { + md->end_match_ptr = eptr; /* For ONCE_NC */ + md->end_offset_top = offset_top; + md->start_match_ptr = mstart; + RRETURN(MATCH_MATCH); /* Sets md->mark */ + } + + /* For capturing groups we have to check the group number back at the start + and if necessary complete handling an extraction by setting the offsets and + bumping the high water mark. Whole-pattern recursion is coded as a recurse + into group 0, so it won't be picked up here. Instead, we catch it when the + OP_END is reached. Other recursion is handled here. We just have to record + the current subject position and start match pointer and give a MATCH + return. */ + + if (*prev == OP_CBRA || *prev == OP_SCBRA || + *prev == OP_CBRAPOS || *prev == OP_SCBRAPOS) + { + number = GET2(prev, 1+LINK_SIZE); + offset = number << 1; + +#ifdef PCRE_DEBUG + printf("end bracket %d", number); + printf("\n"); +#endif + + /* Handle a recursively called group. */ + + if (md->recursive != NULL && md->recursive->group_num == number) + { + md->end_match_ptr = eptr; + md->start_match_ptr = mstart; + RRETURN(MATCH_MATCH); + } + + /* Deal with capturing */ + + md->capture_last = (md->capture_last & OVFLMASK) | number; + if (offset >= md->offset_max) md->capture_last |= OVFLBIT; else + { + /* If offset is greater than offset_top, it means that we are + "skipping" a capturing group, and that group's offsets must be marked + unset. In earlier versions of PCRE, all the offsets were unset at the + start of matching, but this doesn't work because atomic groups and + assertions can cause a value to be set that should later be unset. + Example: matching /(?>(a))b|(a)c/ against "ac". This sets group 1 as + part of the atomic group, but this is not on the final matching path, + so must be unset when 2 is set. (If there is no group 2, there is no + problem, because offset_top will then be 2, indicating no capture.) */ + + if (offset > offset_top) + { + register int *iptr = md->offset_vector + offset_top; + register int *iend = md->offset_vector + offset; + while (iptr < iend) *iptr++ = -1; + } + + /* Now make the extraction */ + + md->offset_vector[offset] = + md->offset_vector[md->offset_end - number]; + md->offset_vector[offset+1] = (int)(eptr - md->start_subject); + if (offset_top <= offset) offset_top = offset + 2; + } + } + + /* OP_KETRPOS is a possessive repeating ket. Remember the current position, + and return the MATCH_KETRPOS. This makes it possible to do the repeats one + at a time from the outer level, thus saving stack. This must precede the + empty string test - in this case that test is done at the outer level. */ + + if (*ecode == OP_KETRPOS) + { + md->start_match_ptr = mstart; /* In case \K reset it */ + md->end_match_ptr = eptr; + md->end_offset_top = offset_top; + RRETURN(MATCH_KETRPOS); + } + + /* For an ordinary non-repeating ket, just continue at this level. This + also happens for a repeating ket if no characters were matched in the + group. This is the forcible breaking of infinite loops as implemented in + Perl 5.005. For a non-repeating atomic group that includes captures, + establish a backup point by processing the rest of the pattern at a lower + level. If this results in a NOMATCH return, pass MATCH_ONCE back to the + original OP_ONCE level, thereby bypassing intermediate backup points, but + resetting any captures that happened along the way. */ + + if (*ecode == OP_KET || eptr == saved_eptr) + { + if (*prev == OP_ONCE) + { + RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM12); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + md->once_target = prev; /* Level at which to change to MATCH_NOMATCH */ + RRETURN(MATCH_ONCE); + } + ecode += 1 + LINK_SIZE; /* Carry on at this level */ + break; + } + + /* The normal repeating kets try the rest of the pattern or restart from + the preceding bracket, in the appropriate order. In the second case, we can + use tail recursion to avoid using another stack frame, unless we have an + an atomic group or an unlimited repeat of a group that can match an empty + string. */ + + if (*ecode == OP_KETRMIN) + { + RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM7); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (*prev == OP_ONCE) + { + RMATCH(eptr, prev, offset_top, md, eptrb, RM8); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + md->once_target = prev; /* Level at which to change to MATCH_NOMATCH */ + RRETURN(MATCH_ONCE); + } + if (*prev >= OP_SBRA) /* Could match an empty string */ + { + RMATCH(eptr, prev, offset_top, md, eptrb, RM50); + RRETURN(rrc); + } + ecode = prev; + goto TAIL_RECURSE; + } + else /* OP_KETRMAX */ + { + RMATCH(eptr, prev, offset_top, md, eptrb, RM13); + if (rrc == MATCH_ONCE && md->once_target == prev) rrc = MATCH_NOMATCH; + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (*prev == OP_ONCE) + { + RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM9); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + md->once_target = prev; + RRETURN(MATCH_ONCE); + } + ecode += 1 + LINK_SIZE; + goto TAIL_RECURSE; + } + /* Control never gets here */ + + /* Not multiline mode: start of subject assertion, unless notbol. */ + + case OP_CIRC: + if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH); + + /* Start of subject assertion */ + + case OP_SOD: + if (eptr != md->start_subject) RRETURN(MATCH_NOMATCH); + ecode++; + break; + + /* Multiline mode: start of subject unless notbol, or after any newline. */ + + case OP_CIRCM: + if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH); + if (eptr != md->start_subject && + (eptr == md->end_subject || !WAS_NEWLINE(eptr))) + RRETURN(MATCH_NOMATCH); + ecode++; + break; + + /* Start of match assertion */ + + case OP_SOM: + if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH); + ecode++; + break; + + /* Reset the start of match point */ + + case OP_SET_SOM: + mstart = eptr; + ecode++; + break; + + /* Multiline mode: assert before any newline, or before end of subject + unless noteol is set. */ + + case OP_DOLLM: + if (eptr < md->end_subject) + { + if (!IS_NEWLINE(eptr)) + { + if (md->partial != 0 && + eptr + 1 >= md->end_subject && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + UCHAR21TEST(eptr) == NLBLOCK->nl[0]) + { + md->hitend = TRUE; + if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); + } + RRETURN(MATCH_NOMATCH); + } + } + else + { + if (md->noteol) RRETURN(MATCH_NOMATCH); + SCHECK_PARTIAL(); + } + ecode++; + break; + + /* Not multiline mode: assert before a terminating newline or before end of + subject unless noteol is set. */ + + case OP_DOLL: + if (md->noteol) RRETURN(MATCH_NOMATCH); + if (!md->endonly) goto ASSERT_NL_OR_EOS; + + /* ... else fall through for endonly */ + + /* End of subject assertion (\z) */ + + case OP_EOD: + if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH); + SCHECK_PARTIAL(); + ecode++; + break; + + /* End of subject or ending \n assertion (\Z) */ + + case OP_EODN: + ASSERT_NL_OR_EOS: + if (eptr < md->end_subject && + (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen)) + { + if (md->partial != 0 && + eptr + 1 >= md->end_subject && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + UCHAR21TEST(eptr) == NLBLOCK->nl[0]) + { + md->hitend = TRUE; + if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); + } + RRETURN(MATCH_NOMATCH); + } + + /* Either at end of string or \n before end. */ + + SCHECK_PARTIAL(); + ecode++; + break; + + /* Word boundary assertions */ + + case OP_NOT_WORD_BOUNDARY: + case OP_WORD_BOUNDARY: + { + + /* Find out if the previous and current characters are "word" characters. + It takes a bit more work in UTF-8 mode. Characters > 255 are assumed to + be "non-word" characters. Remember the earliest consulted character for + partial matching. */ + +#ifdef SUPPORT_UTF + if (utf) + { + /* Get status of previous character */ + + if (eptr == md->start_subject) prev_is_word = FALSE; else + { + PCRE_PUCHAR lastptr = eptr - 1; + BACKCHAR(lastptr); + if (lastptr < md->start_used_ptr) md->start_used_ptr = lastptr; + GETCHAR(c, lastptr); +#ifdef SUPPORT_UCP + if (md->use_ucp) + { + if (c == '_') prev_is_word = TRUE; else + { + int cat = UCD_CATEGORY(c); + prev_is_word = (cat == ucp_L || cat == ucp_N); + } + } + else +#endif + prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0; + } + + /* Get status of next character */ + + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + cur_is_word = FALSE; + } + else + { + GETCHAR(c, eptr); +#ifdef SUPPORT_UCP + if (md->use_ucp) + { + if (c == '_') cur_is_word = TRUE; else + { + int cat = UCD_CATEGORY(c); + cur_is_word = (cat == ucp_L || cat == ucp_N); + } + } + else +#endif + cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0; + } + } + else +#endif + + /* Not in UTF-8 mode, but we may still have PCRE_UCP set, and for + consistency with the behaviour of \w we do use it in this case. */ + + { + /* Get status of previous character */ + + if (eptr == md->start_subject) prev_is_word = FALSE; else + { + if (eptr <= md->start_used_ptr) md->start_used_ptr = eptr - 1; +#ifdef SUPPORT_UCP + if (md->use_ucp) + { + c = eptr[-1]; + if (c == '_') prev_is_word = TRUE; else + { + int cat = UCD_CATEGORY(c); + prev_is_word = (cat == ucp_L || cat == ucp_N); + } + } + else +#endif + prev_is_word = MAX_255(eptr[-1]) + && ((md->ctypes[eptr[-1]] & ctype_word) != 0); + } + + /* Get status of next character */ + + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + cur_is_word = FALSE; + } + else +#ifdef SUPPORT_UCP + if (md->use_ucp) + { + c = *eptr; + if (c == '_') cur_is_word = TRUE; else + { + int cat = UCD_CATEGORY(c); + cur_is_word = (cat == ucp_L || cat == ucp_N); + } + } + else +#endif + cur_is_word = MAX_255(*eptr) + && ((md->ctypes[*eptr] & ctype_word) != 0); + } + + /* Now see if the situation is what we want */ + + if ((*ecode++ == OP_WORD_BOUNDARY)? + cur_is_word == prev_is_word : cur_is_word != prev_is_word) + RRETURN(MATCH_NOMATCH); + } + break; + + /* Match any single character type except newline; have to take care with + CRLF newlines and partial matching. */ + + case OP_ANY: + if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH); + if (md->partial != 0 && + eptr == md->end_subject - 1 && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + UCHAR21TEST(eptr) == NLBLOCK->nl[0]) + { + md->hitend = TRUE; + if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); + } + + /* Fall through */ + + /* Match any single character whatsoever. */ + + case OP_ALLANY: + if (eptr >= md->end_subject) /* DO NOT merge the eptr++ here; it must */ + { /* not be updated before SCHECK_PARTIAL. */ + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + eptr++; +#ifdef SUPPORT_UTF + if (utf) ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++); +#endif + ecode++; + break; + + /* Match a single byte, even in UTF-8 mode. This opcode really does match + any byte, even newline, independent of the setting of PCRE_DOTALL. */ + + case OP_ANYBYTE: + if (eptr >= md->end_subject) /* DO NOT merge the eptr++ here; it must */ + { /* not be updated before SCHECK_PARTIAL. */ + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + eptr++; + ecode++; + break; + + case OP_NOT_DIGIT: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ( +#if defined SUPPORT_UTF || !(defined COMPILE_PCRE8) + c < 256 && +#endif + (md->ctypes[c] & ctype_digit) != 0 + ) + RRETURN(MATCH_NOMATCH); + ecode++; + break; + + case OP_DIGIT: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ( +#if defined SUPPORT_UTF || !(defined COMPILE_PCRE8) + c > 255 || +#endif + (md->ctypes[c] & ctype_digit) == 0 + ) + RRETURN(MATCH_NOMATCH); + ecode++; + break; + + case OP_NOT_WHITESPACE: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ( +#if defined SUPPORT_UTF || !(defined COMPILE_PCRE8) + c < 256 && +#endif + (md->ctypes[c] & ctype_space) != 0 + ) + RRETURN(MATCH_NOMATCH); + ecode++; + break; + + case OP_WHITESPACE: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ( +#if defined SUPPORT_UTF || !(defined COMPILE_PCRE8) + c > 255 || +#endif + (md->ctypes[c] & ctype_space) == 0 + ) + RRETURN(MATCH_NOMATCH); + ecode++; + break; + + case OP_NOT_WORDCHAR: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ( +#if defined SUPPORT_UTF || !(defined COMPILE_PCRE8) + c < 256 && +#endif + (md->ctypes[c] & ctype_word) != 0 + ) + RRETURN(MATCH_NOMATCH); + ecode++; + break; + + case OP_WORDCHAR: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ( +#if defined SUPPORT_UTF || !(defined COMPILE_PCRE8) + c > 255 || +#endif + (md->ctypes[c] & ctype_word) == 0 + ) + RRETURN(MATCH_NOMATCH); + ecode++; + break; + + case OP_ANYNL: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + switch(c) + { + default: RRETURN(MATCH_NOMATCH); + + case CHAR_CR: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + } + else if (UCHAR21TEST(eptr) == CHAR_LF) eptr++; + break; + + case CHAR_LF: + break; + + case CHAR_VT: + case CHAR_FF: + case CHAR_NEL: +#ifndef EBCDIC + case 0x2028: + case 0x2029: +#endif /* Not EBCDIC */ + if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH); + break; + } + ecode++; + break; + + case OP_NOT_HSPACE: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + switch(c) + { + HSPACE_CASES: RRETURN(MATCH_NOMATCH); /* Byte and multibyte cases */ + default: break; + } + ecode++; + break; + + case OP_HSPACE: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + switch(c) + { + HSPACE_CASES: break; /* Byte and multibyte cases */ + default: RRETURN(MATCH_NOMATCH); + } + ecode++; + break; + + case OP_NOT_VSPACE: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + switch(c) + { + VSPACE_CASES: RRETURN(MATCH_NOMATCH); + default: break; + } + ecode++; + break; + + case OP_VSPACE: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + switch(c) + { + VSPACE_CASES: break; + default: RRETURN(MATCH_NOMATCH); + } + ecode++; + break; + +#ifdef SUPPORT_UCP + /* Check the next character by Unicode property. We will get here only + if the support is in the binary; otherwise a compile-time error occurs. */ + + case OP_PROP: + case OP_NOTPROP: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + { + const pcre_uint32 *cp; + const ucd_record *prop = GET_UCD(c); + + switch(ecode[1]) + { + case PT_ANY: + if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH); + break; + + case PT_LAMP: + if ((prop->chartype == ucp_Lu || + prop->chartype == ucp_Ll || + prop->chartype == ucp_Lt) == (op == OP_NOTPROP)) + RRETURN(MATCH_NOMATCH); + break; + + case PT_GC: + if ((ecode[2] != PRIV(ucp_gentype)[prop->chartype]) == (op == OP_PROP)) + RRETURN(MATCH_NOMATCH); + break; + + case PT_PC: + if ((ecode[2] != prop->chartype) == (op == OP_PROP)) + RRETURN(MATCH_NOMATCH); + break; + + case PT_SC: + if ((ecode[2] != prop->script) == (op == OP_PROP)) + RRETURN(MATCH_NOMATCH); + break; + + /* These are specials */ + + case PT_ALNUM: + if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N) == (op == OP_NOTPROP)) + RRETURN(MATCH_NOMATCH); + break; + + /* Perl space used to exclude VT, but from Perl 5.18 it is included, + which means that Perl space and POSIX space are now identical. PCRE + was changed at release 8.34. */ + + case PT_SPACE: /* Perl space */ + case PT_PXSPACE: /* POSIX space */ + switch(c) + { + HSPACE_CASES: + VSPACE_CASES: + if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH); + break; + + default: + if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == + (op == OP_NOTPROP)) RRETURN(MATCH_NOMATCH); + break; + } + break; + + case PT_WORD: + if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N || + c == CHAR_UNDERSCORE) == (op == OP_NOTPROP)) + RRETURN(MATCH_NOMATCH); + break; + + case PT_CLIST: + cp = PRIV(ucd_caseless_sets) + ecode[2]; + for (;;) + { + if (c < *cp) + { if (op == OP_PROP) { RRETURN(MATCH_NOMATCH); } else break; } + if (c == *cp++) + { if (op == OP_PROP) break; else { RRETURN(MATCH_NOMATCH); } } + } + break; + + case PT_UCNC: + if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT || + c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) || + c >= 0xe000) == (op == OP_NOTPROP)) + RRETURN(MATCH_NOMATCH); + break; + + /* This should never occur */ + + default: + RRETURN(PCRE_ERROR_INTERNAL); + } + + ecode += 3; + } + break; + + /* Match an extended Unicode sequence. We will get here only if the support + is in the binary; otherwise a compile-time error occurs. */ + + case OP_EXTUNI: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + else + { + int lgb, rgb; + GETCHARINCTEST(c, eptr); + lgb = UCD_GRAPHBREAK(c); + while (eptr < md->end_subject) + { + int len = 1; + if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); } + rgb = UCD_GRAPHBREAK(c); + if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break; + lgb = rgb; + eptr += len; + } + } + CHECK_PARTIAL(); + ecode++; + break; +#endif /* SUPPORT_UCP */ + + + /* Match a back reference, possibly repeatedly. Look past the end of the + item to see if there is repeat information following. The code is similar + to that for character classes, but repeated for efficiency. Then obey + similar code to character type repeats - written out again for speed. + However, if the referenced string is the empty string, always treat + it as matched, any number of times (otherwise there could be infinite + loops). If the reference is unset, there are two possibilities: + + (a) In the default, Perl-compatible state, set the length negative; + this ensures that every attempt at a match fails. We can't just fail + here, because of the possibility of quantifiers with zero minima. + + (b) If the JavaScript compatibility flag is set, set the length to zero + so that the back reference matches an empty string. + + Otherwise, set the length to the length of what was matched by the + referenced subpattern. + + The OP_REF and OP_REFI opcodes are used for a reference to a numbered group + or to a non-duplicated named group. For a duplicated named group, OP_DNREF + and OP_DNREFI are used. In this case we must scan the list of groups to + which the name refers, and use the first one that is set. */ + + case OP_DNREF: + case OP_DNREFI: + caseless = op == OP_DNREFI; + { + int count = GET2(ecode, 1+IMM2_SIZE); + pcre_uchar *slot = md->name_table + GET2(ecode, 1) * md->name_entry_size; + ecode += 1 + 2*IMM2_SIZE; + + /* Setting the default length first and initializing 'offset' avoids + compiler warnings in the REF_REPEAT code. */ + + length = (md->jscript_compat)? 0 : -1; + offset = 0; + + while (count-- > 0) + { + offset = GET2(slot, 0) << 1; + if (offset < offset_top && md->offset_vector[offset] >= 0) + { + length = md->offset_vector[offset+1] - md->offset_vector[offset]; + break; + } + slot += md->name_entry_size; + } + } + goto REF_REPEAT; + + case OP_REF: + case OP_REFI: + caseless = op == OP_REFI; + offset = GET2(ecode, 1) << 1; /* Doubled ref number */ + ecode += 1 + IMM2_SIZE; + if (offset >= offset_top || md->offset_vector[offset] < 0) + length = (md->jscript_compat)? 0 : -1; + else + length = md->offset_vector[offset+1] - md->offset_vector[offset]; + + /* Set up for repetition, or handle the non-repeated case */ + + REF_REPEAT: + switch (*ecode) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRPLUS: + case OP_CRMINPLUS: + case OP_CRQUERY: + case OP_CRMINQUERY: + c = *ecode++ - OP_CRSTAR; + minimize = (c & 1) != 0; + min = rep_min[c]; /* Pick up values from tables; */ + max = rep_max[c]; /* zero for max => infinity */ + if (max == 0) max = INT_MAX; + break; + + case OP_CRRANGE: + case OP_CRMINRANGE: + minimize = (*ecode == OP_CRMINRANGE); + min = GET2(ecode, 1); + max = GET2(ecode, 1 + IMM2_SIZE); + if (max == 0) max = INT_MAX; + ecode += 1 + 2 * IMM2_SIZE; + break; + + default: /* No repeat follows */ + if ((length = match_ref(offset, eptr, length, md, caseless)) < 0) + { + if (length == -2) eptr = md->end_subject; /* Partial match */ + CHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + eptr += length; + continue; /* With the main loop */ + } + + /* Handle repeated back references. If the length of the reference is + zero, just continue with the main loop. If the length is negative, it + means the reference is unset in non-Java-compatible mode. If the minimum is + zero, we can continue at the same level without recursion. For any other + minimum, carrying on will result in NOMATCH. */ + + if (length == 0) continue; + if (length < 0 && min == 0) continue; + + /* First, ensure the minimum number of matches are present. We get back + the length of the reference string explicitly rather than passing the + address of eptr, so that eptr can be a register variable. */ + + for (i = 1; i <= min; i++) + { + int slength; + if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0) + { + if (slength == -2) eptr = md->end_subject; /* Partial match */ + CHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + eptr += slength; + } + + /* If min = max, continue at the same level without recursion. + They are not both allowed to be zero. */ + + if (min == max) continue; + + /* If minimizing, keep trying and advancing the pointer */ + + if (minimize) + { + for (fi = min;; fi++) + { + int slength; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM14); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0) + { + if (slength == -2) eptr = md->end_subject; /* Partial match */ + CHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + eptr += slength; + } + /* Control never gets here */ + } + + /* If maximizing, find the longest string and work backwards */ + + else + { + pp = eptr; + for (i = min; i < max; i++) + { + int slength; + if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0) + { + /* Can't use CHECK_PARTIAL because we don't want to update eptr in + the soft partial matching case. */ + + if (slength == -2 && md->partial != 0 && + md->end_subject > md->start_used_ptr) + { + md->hitend = TRUE; + if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); + } + break; + } + eptr += slength; + } + + while (eptr >= pp) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM15); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + eptr -= length; + } + RRETURN(MATCH_NOMATCH); + } + /* Control never gets here */ + + /* Match a bit-mapped character class, possibly repeatedly. This op code is + used when all the characters in the class have values in the range 0-255, + and either the matching is caseful, or the characters are in the range + 0-127 when UTF-8 processing is enabled. The only difference between + OP_CLASS and OP_NCLASS occurs when a data character outside the range is + encountered. + + First, look past the end of the item to see if there is repeat information + following. Then obey similar code to character type repeats - written out + again for speed. */ + + case OP_NCLASS: + case OP_CLASS: + { + /* The data variable is saved across frames, so the byte map needs to + be stored there. */ +#define BYTE_MAP ((pcre_uint8 *)data) + data = ecode + 1; /* Save for matching */ + ecode += 1 + (32 / sizeof(pcre_uchar)); /* Advance past the item */ + + switch (*ecode) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRPLUS: + case OP_CRMINPLUS: + case OP_CRQUERY: + case OP_CRMINQUERY: + case OP_CRPOSSTAR: + case OP_CRPOSPLUS: + case OP_CRPOSQUERY: + c = *ecode++ - OP_CRSTAR; + if (c < OP_CRPOSSTAR - OP_CRSTAR) minimize = (c & 1) != 0; + else possessive = TRUE; + min = rep_min[c]; /* Pick up values from tables; */ + max = rep_max[c]; /* zero for max => infinity */ + if (max == 0) max = INT_MAX; + break; + + case OP_CRRANGE: + case OP_CRMINRANGE: + case OP_CRPOSRANGE: + minimize = (*ecode == OP_CRMINRANGE); + possessive = (*ecode == OP_CRPOSRANGE); + min = GET2(ecode, 1); + max = GET2(ecode, 1 + IMM2_SIZE); + if (max == 0) max = INT_MAX; + ecode += 1 + 2 * IMM2_SIZE; + break; + + default: /* No repeat follows */ + min = max = 1; + break; + } + + /* First, ensure the minimum number of matches are present. */ + +#ifdef SUPPORT_UTF + if (utf) + { + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINC(c, eptr); + if (c > 255) + { + if (op == OP_CLASS) RRETURN(MATCH_NOMATCH); + } + else + if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH); + } + } + else +#endif + /* Not UTF mode */ + { + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + c = *eptr++; +#ifndef COMPILE_PCRE8 + if (c > 255) + { + if (op == OP_CLASS) RRETURN(MATCH_NOMATCH); + } + else +#endif + if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH); + } + } + + /* If max == min we can continue with the main loop without the + need to recurse. */ + + if (min == max) continue; + + /* If minimizing, keep testing the rest of the expression and advancing + the pointer while it matches the class. */ + + if (minimize) + { +#ifdef SUPPORT_UTF + if (utf) + { + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM16); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINC(c, eptr); + if (c > 255) + { + if (op == OP_CLASS) RRETURN(MATCH_NOMATCH); + } + else + if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH); + } + } + else +#endif + /* Not UTF mode */ + { + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM17); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + c = *eptr++; +#ifndef COMPILE_PCRE8 + if (c > 255) + { + if (op == OP_CLASS) RRETURN(MATCH_NOMATCH); + } + else +#endif + if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH); + } + } + /* Control never gets here */ + } + + /* If maximizing, find the longest possible run, then work backwards. */ + + else + { + pp = eptr; + +#ifdef SUPPORT_UTF + if (utf) + { + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLEN(c, eptr, len); + if (c > 255) + { + if (op == OP_CLASS) break; + } + else + if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) break; + eptr += len; + } + + if (possessive) continue; /* No backtracking */ + + for (;;) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM18); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (eptr-- <= pp) break; /* Stop if tried at original pos */ + BACKCHAR(eptr); + } + } + else +#endif + /* Not UTF mode */ + { + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + c = *eptr; +#ifndef COMPILE_PCRE8 + if (c > 255) + { + if (op == OP_CLASS) break; + } + else +#endif + if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) break; + eptr++; + } + + if (possessive) continue; /* No backtracking */ + + while (eptr >= pp) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM19); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + eptr--; + } + } + + RRETURN(MATCH_NOMATCH); + } +#undef BYTE_MAP + } + /* Control never gets here */ + + + /* Match an extended character class. In the 8-bit library, this opcode is + encountered only when UTF-8 mode mode is supported. In the 16-bit and + 32-bit libraries, codepoints greater than 255 may be encountered even when + UTF is not supported. */ + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: + { + data = ecode + 1 + LINK_SIZE; /* Save for matching */ + ecode += GET(ecode, 1); /* Advance past the item */ + + switch (*ecode) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRPLUS: + case OP_CRMINPLUS: + case OP_CRQUERY: + case OP_CRMINQUERY: + case OP_CRPOSSTAR: + case OP_CRPOSPLUS: + case OP_CRPOSQUERY: + c = *ecode++ - OP_CRSTAR; + if (c < OP_CRPOSSTAR - OP_CRSTAR) minimize = (c & 1) != 0; + else possessive = TRUE; + min = rep_min[c]; /* Pick up values from tables; */ + max = rep_max[c]; /* zero for max => infinity */ + if (max == 0) max = INT_MAX; + break; + + case OP_CRRANGE: + case OP_CRMINRANGE: + case OP_CRPOSRANGE: + minimize = (*ecode == OP_CRMINRANGE); + possessive = (*ecode == OP_CRPOSRANGE); + min = GET2(ecode, 1); + max = GET2(ecode, 1 + IMM2_SIZE); + if (max == 0) max = INT_MAX; + ecode += 1 + 2 * IMM2_SIZE; + break; + + default: /* No repeat follows */ + min = max = 1; + break; + } + + /* First, ensure the minimum number of matches are present. */ + + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if (!PRIV(xclass)(c, data, utf)) RRETURN(MATCH_NOMATCH); + } + + /* If max == min we can continue with the main loop without the + need to recurse. */ + + if (min == max) continue; + + /* If minimizing, keep testing the rest of the expression and advancing + the pointer while it matches the class. */ + + if (minimize) + { + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM20); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if (!PRIV(xclass)(c, data, utf)) RRETURN(MATCH_NOMATCH); + } + /* Control never gets here */ + } + + /* If maximizing, find the longest possible run, then work backwards. */ + + else + { + pp = eptr; + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } +#ifdef SUPPORT_UTF + GETCHARLENTEST(c, eptr, len); +#else + c = *eptr; +#endif + if (!PRIV(xclass)(c, data, utf)) break; + eptr += len; + } + + if (possessive) continue; /* No backtracking */ + + for(;;) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM21); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (eptr-- <= pp) break; /* Stop if tried at original pos */ +#ifdef SUPPORT_UTF + if (utf) BACKCHAR(eptr); +#endif + } + RRETURN(MATCH_NOMATCH); + } + + /* Control never gets here */ + } +#endif /* End of XCLASS */ + + /* Match a single character, casefully */ + + case OP_CHAR: +#ifdef SUPPORT_UTF + if (utf) + { + length = 1; + ecode++; + GETCHARLEN(fc, ecode, length); + if (length > md->end_subject - eptr) + { + CHECK_PARTIAL(); /* Not SCHECK_PARTIAL() */ + RRETURN(MATCH_NOMATCH); + } + while (length-- > 0) if (*ecode++ != UCHAR21INC(eptr)) RRETURN(MATCH_NOMATCH); + } + else +#endif + /* Not UTF mode */ + { + if (md->end_subject - eptr < 1) + { + SCHECK_PARTIAL(); /* This one can use SCHECK_PARTIAL() */ + RRETURN(MATCH_NOMATCH); + } + if (ecode[1] != *eptr++) RRETURN(MATCH_NOMATCH); + ecode += 2; + } + break; + + /* Match a single character, caselessly. If we are at the end of the + subject, give up immediately. */ + + case OP_CHARI: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + +#ifdef SUPPORT_UTF + if (utf) + { + length = 1; + ecode++; + GETCHARLEN(fc, ecode, length); + + /* If the pattern character's value is < 128, we have only one byte, and + we know that its other case must also be one byte long, so we can use the + fast lookup table. We know that there is at least one byte left in the + subject. */ + + if (fc < 128) + { + pcre_uint32 cc = UCHAR21(eptr); + if (md->lcc[fc] != TABLE_GET(cc, md->lcc, cc)) RRETURN(MATCH_NOMATCH); + ecode++; + eptr++; + } + + /* Otherwise we must pick up the subject character. Note that we cannot + use the value of "length" to check for sufficient bytes left, because the + other case of the character may have more or fewer bytes. */ + + else + { + pcre_uint32 dc; + GETCHARINC(dc, eptr); + ecode += length; + + /* If we have Unicode property support, we can use it to test the other + case of the character, if there is one. */ + + if (fc != dc) + { +#ifdef SUPPORT_UCP + if (dc != UCD_OTHERCASE(fc)) +#endif + RRETURN(MATCH_NOMATCH); + } + } + } + else +#endif /* SUPPORT_UTF */ + + /* Not UTF mode */ + { + if (TABLE_GET(ecode[1], md->lcc, ecode[1]) + != TABLE_GET(*eptr, md->lcc, *eptr)) RRETURN(MATCH_NOMATCH); + eptr++; + ecode += 2; + } + break; + + /* Match a single character repeatedly. */ + + case OP_EXACT: + case OP_EXACTI: + min = max = GET2(ecode, 1); + ecode += 1 + IMM2_SIZE; + goto REPEATCHAR; + + case OP_POSUPTO: + case OP_POSUPTOI: + possessive = TRUE; + /* Fall through */ + + case OP_UPTO: + case OP_UPTOI: + case OP_MINUPTO: + case OP_MINUPTOI: + min = 0; + max = GET2(ecode, 1); + minimize = *ecode == OP_MINUPTO || *ecode == OP_MINUPTOI; + ecode += 1 + IMM2_SIZE; + goto REPEATCHAR; + + case OP_POSSTAR: + case OP_POSSTARI: + possessive = TRUE; + min = 0; + max = INT_MAX; + ecode++; + goto REPEATCHAR; + + case OP_POSPLUS: + case OP_POSPLUSI: + possessive = TRUE; + min = 1; + max = INT_MAX; + ecode++; + goto REPEATCHAR; + + case OP_POSQUERY: + case OP_POSQUERYI: + possessive = TRUE; + min = 0; + max = 1; + ecode++; + goto REPEATCHAR; + + case OP_STAR: + case OP_STARI: + case OP_MINSTAR: + case OP_MINSTARI: + case OP_PLUS: + case OP_PLUSI: + case OP_MINPLUS: + case OP_MINPLUSI: + case OP_QUERY: + case OP_QUERYI: + case OP_MINQUERY: + case OP_MINQUERYI: + c = *ecode++ - ((op < OP_STARI)? OP_STAR : OP_STARI); + minimize = (c & 1) != 0; + min = rep_min[c]; /* Pick up values from tables; */ + max = rep_max[c]; /* zero for max => infinity */ + if (max == 0) max = INT_MAX; + + /* Common code for all repeated single-character matches. We first check + for the minimum number of characters. If the minimum equals the maximum, we + are done. Otherwise, if minimizing, check the rest of the pattern for a + match; if there isn't one, advance up to the maximum, one character at a + time. + + If maximizing, advance up to the maximum number of matching characters, + until eptr is past the end of the maximum run. If possessive, we are + then done (no backing up). Otherwise, match at this position; anything + other than no match is immediately returned. For nomatch, back up one + character, unless we are matching \R and the last thing matched was + \r\n, in which case, back up two bytes. When we reach the first optional + character position, we can save stack by doing a tail recurse. + + The various UTF/non-UTF and caseful/caseless cases are handled separately, + for speed. */ + + REPEATCHAR: +#ifdef SUPPORT_UTF + if (utf) + { + length = 1; + charptr = ecode; + GETCHARLEN(fc, ecode, length); + ecode += length; + + /* Handle multibyte character matching specially here. There is + support for caseless matching if UCP support is present. */ + + if (length > 1) + { +#ifdef SUPPORT_UCP + pcre_uint32 othercase; + if (op >= OP_STARI && /* Caseless */ + (othercase = UCD_OTHERCASE(fc)) != fc) + oclength = PRIV(ord2utf)(othercase, occhars); + else oclength = 0; +#endif /* SUPPORT_UCP */ + + for (i = 1; i <= min; i++) + { + if (eptr <= md->end_subject - length && + memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length; +#ifdef SUPPORT_UCP + else if (oclength > 0 && + eptr <= md->end_subject - oclength && + memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength; +#endif /* SUPPORT_UCP */ + else + { + CHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + } + + if (min == max) continue; + + if (minimize) + { + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM22); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr <= md->end_subject - length && + memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length; +#ifdef SUPPORT_UCP + else if (oclength > 0 && + eptr <= md->end_subject - oclength && + memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength; +#endif /* SUPPORT_UCP */ + else + { + CHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + } + /* Control never gets here */ + } + + else /* Maximize */ + { + pp = eptr; + for (i = min; i < max; i++) + { + if (eptr <= md->end_subject - length && + memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length; +#ifdef SUPPORT_UCP + else if (oclength > 0 && + eptr <= md->end_subject - oclength && + memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength; +#endif /* SUPPORT_UCP */ + else + { + CHECK_PARTIAL(); + break; + } + } + + if (possessive) continue; /* No backtracking */ + for(;;) + { + if (eptr <= pp) goto TAIL_RECURSE; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM23); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); +#ifdef SUPPORT_UCP + eptr--; + BACKCHAR(eptr); +#else /* without SUPPORT_UCP */ + eptr -= length; +#endif /* SUPPORT_UCP */ + } + } + /* Control never gets here */ + } + + /* If the length of a UTF-8 character is 1, we fall through here, and + obey the code as for non-UTF-8 characters below, though in this case the + value of fc will always be < 128. */ + } + else +#endif /* SUPPORT_UTF */ + /* When not in UTF-8 mode, load a single-byte character. */ + fc = *ecode++; + + /* The value of fc at this point is always one character, though we may + or may not be in UTF mode. The code is duplicated for the caseless and + caseful cases, for speed, since matching characters is likely to be quite + common. First, ensure the minimum number of matches are present. If min = + max, continue at the same level without recursing. Otherwise, if + minimizing, keep trying the rest of the expression and advancing one + matching character if failing, up to the maximum. Alternatively, if + maximizing, find the maximum number of characters and work backwards. */ + + DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max, + max, (char *)eptr)); + + if (op >= OP_STARI) /* Caseless */ + { +#ifdef COMPILE_PCRE8 + /* fc must be < 128 if UTF is enabled. */ + foc = md->fcc[fc]; +#else +#ifdef SUPPORT_UTF +#ifdef SUPPORT_UCP + if (utf && fc > 127) + foc = UCD_OTHERCASE(fc); +#else + if (utf && fc > 127) + foc = fc; +#endif /* SUPPORT_UCP */ + else +#endif /* SUPPORT_UTF */ + foc = TABLE_GET(fc, md->fcc, fc); +#endif /* COMPILE_PCRE8 */ + + for (i = 1; i <= min; i++) + { + pcre_uint32 cc; /* Faster than pcre_uchar */ + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + cc = UCHAR21TEST(eptr); + if (fc != cc && foc != cc) RRETURN(MATCH_NOMATCH); + eptr++; + } + if (min == max) continue; + if (minimize) + { + for (fi = min;; fi++) + { + pcre_uint32 cc; /* Faster than pcre_uchar */ + RMATCH(eptr, ecode, offset_top, md, eptrb, RM24); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + cc = UCHAR21TEST(eptr); + if (fc != cc && foc != cc) RRETURN(MATCH_NOMATCH); + eptr++; + } + /* Control never gets here */ + } + else /* Maximize */ + { + pp = eptr; + for (i = min; i < max; i++) + { + pcre_uint32 cc; /* Faster than pcre_uchar */ + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + cc = UCHAR21TEST(eptr); + if (fc != cc && foc != cc) break; + eptr++; + } + if (possessive) continue; /* No backtracking */ + for (;;) + { + if (eptr == pp) goto TAIL_RECURSE; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM25); + eptr--; + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + } + /* Control never gets here */ + } + } + + /* Caseful comparisons (includes all multi-byte characters) */ + + else + { + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (fc != UCHAR21INCTEST(eptr)) RRETURN(MATCH_NOMATCH); + } + + if (min == max) continue; + + if (minimize) + { + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM26); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (fc != UCHAR21INCTEST(eptr)) RRETURN(MATCH_NOMATCH); + } + /* Control never gets here */ + } + else /* Maximize */ + { + pp = eptr; + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + if (fc != UCHAR21TEST(eptr)) break; + eptr++; + } + if (possessive) continue; /* No backtracking */ + for (;;) + { + if (eptr == pp) goto TAIL_RECURSE; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM27); + eptr--; + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + } + /* Control never gets here */ + } + } + /* Control never gets here */ + + /* Match a negated single one-byte character. The character we are + checking can be multibyte. */ + + case OP_NOT: + case OP_NOTI: + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } +#ifdef SUPPORT_UTF + if (utf) + { + register pcre_uint32 ch, och; + + ecode++; + GETCHARINC(ch, ecode); + GETCHARINC(c, eptr); + + if (op == OP_NOT) + { + if (ch == c) RRETURN(MATCH_NOMATCH); + } + else + { +#ifdef SUPPORT_UCP + if (ch > 127) + och = UCD_OTHERCASE(ch); +#else + if (ch > 127) + och = ch; +#endif /* SUPPORT_UCP */ + else + och = TABLE_GET(ch, md->fcc, ch); + if (ch == c || och == c) RRETURN(MATCH_NOMATCH); + } + } + else +#endif + { + register pcre_uint32 ch = ecode[1]; + c = *eptr++; + if (ch == c || (op == OP_NOTI && TABLE_GET(ch, md->fcc, ch) == c)) + RRETURN(MATCH_NOMATCH); + ecode += 2; + } + break; + + /* Match a negated single one-byte character repeatedly. This is almost a + repeat of the code for a repeated single character, but I haven't found a + nice way of commoning these up that doesn't require a test of the + positive/negative option for each character match. Maybe that wouldn't add + very much to the time taken, but character matching *is* what this is all + about... */ + + case OP_NOTEXACT: + case OP_NOTEXACTI: + min = max = GET2(ecode, 1); + ecode += 1 + IMM2_SIZE; + goto REPEATNOTCHAR; + + case OP_NOTUPTO: + case OP_NOTUPTOI: + case OP_NOTMINUPTO: + case OP_NOTMINUPTOI: + min = 0; + max = GET2(ecode, 1); + minimize = *ecode == OP_NOTMINUPTO || *ecode == OP_NOTMINUPTOI; + ecode += 1 + IMM2_SIZE; + goto REPEATNOTCHAR; + + case OP_NOTPOSSTAR: + case OP_NOTPOSSTARI: + possessive = TRUE; + min = 0; + max = INT_MAX; + ecode++; + goto REPEATNOTCHAR; + + case OP_NOTPOSPLUS: + case OP_NOTPOSPLUSI: + possessive = TRUE; + min = 1; + max = INT_MAX; + ecode++; + goto REPEATNOTCHAR; + + case OP_NOTPOSQUERY: + case OP_NOTPOSQUERYI: + possessive = TRUE; + min = 0; + max = 1; + ecode++; + goto REPEATNOTCHAR; + + case OP_NOTPOSUPTO: + case OP_NOTPOSUPTOI: + possessive = TRUE; + min = 0; + max = GET2(ecode, 1); + ecode += 1 + IMM2_SIZE; + goto REPEATNOTCHAR; + + case OP_NOTSTAR: + case OP_NOTSTARI: + case OP_NOTMINSTAR: + case OP_NOTMINSTARI: + case OP_NOTPLUS: + case OP_NOTPLUSI: + case OP_NOTMINPLUS: + case OP_NOTMINPLUSI: + case OP_NOTQUERY: + case OP_NOTQUERYI: + case OP_NOTMINQUERY: + case OP_NOTMINQUERYI: + c = *ecode++ - ((op >= OP_NOTSTARI)? OP_NOTSTARI: OP_NOTSTAR); + minimize = (c & 1) != 0; + min = rep_min[c]; /* Pick up values from tables; */ + max = rep_max[c]; /* zero for max => infinity */ + if (max == 0) max = INT_MAX; + + /* Common code for all repeated single-byte matches. */ + + REPEATNOTCHAR: + GETCHARINCTEST(fc, ecode); + + /* The code is duplicated for the caseless and caseful cases, for speed, + since matching characters is likely to be quite common. First, ensure the + minimum number of matches are present. If min = max, continue at the same + level without recursing. Otherwise, if minimizing, keep trying the rest of + the expression and advancing one matching character if failing, up to the + maximum. Alternatively, if maximizing, find the maximum number of + characters and work backwards. */ + + DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n", fc, min, max, + max, (char *)eptr)); + + if (op >= OP_NOTSTARI) /* Caseless */ + { +#ifdef SUPPORT_UTF +#ifdef SUPPORT_UCP + if (utf && fc > 127) + foc = UCD_OTHERCASE(fc); +#else + if (utf && fc > 127) + foc = fc; +#endif /* SUPPORT_UCP */ + else +#endif /* SUPPORT_UTF */ + foc = TABLE_GET(fc, md->fcc, fc); + +#ifdef SUPPORT_UTF + if (utf) + { + register pcre_uint32 d; + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINC(d, eptr); + if (fc == d || (unsigned int)foc == d) RRETURN(MATCH_NOMATCH); + } + } + else +#endif /* SUPPORT_UTF */ + /* Not UTF mode */ + { + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (fc == *eptr || foc == *eptr) RRETURN(MATCH_NOMATCH); + eptr++; + } + } + + if (min == max) continue; + + if (minimize) + { +#ifdef SUPPORT_UTF + if (utf) + { + register pcre_uint32 d; + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM28); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINC(d, eptr); + if (fc == d || (unsigned int)foc == d) RRETURN(MATCH_NOMATCH); + } + } + else +#endif /*SUPPORT_UTF */ + /* Not UTF mode */ + { + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM29); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (fc == *eptr || foc == *eptr) RRETURN(MATCH_NOMATCH); + eptr++; + } + } + /* Control never gets here */ + } + + /* Maximize case */ + + else + { + pp = eptr; + +#ifdef SUPPORT_UTF + if (utf) + { + register pcre_uint32 d; + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLEN(d, eptr, len); + if (fc == d || (unsigned int)foc == d) break; + eptr += len; + } + if (possessive) continue; /* No backtracking */ + for(;;) + { + if (eptr <= pp) goto TAIL_RECURSE; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM30); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + eptr--; + BACKCHAR(eptr); + } + } + else +#endif /* SUPPORT_UTF */ + /* Not UTF mode */ + { + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + if (fc == *eptr || foc == *eptr) break; + eptr++; + } + if (possessive) continue; /* No backtracking */ + for (;;) + { + if (eptr == pp) goto TAIL_RECURSE; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM31); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + eptr--; + } + } + /* Control never gets here */ + } + } + + /* Caseful comparisons */ + + else + { +#ifdef SUPPORT_UTF + if (utf) + { + register pcre_uint32 d; + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINC(d, eptr); + if (fc == d) RRETURN(MATCH_NOMATCH); + } + } + else +#endif + /* Not UTF mode */ + { + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (fc == *eptr++) RRETURN(MATCH_NOMATCH); + } + } + + if (min == max) continue; + + if (minimize) + { +#ifdef SUPPORT_UTF + if (utf) + { + register pcre_uint32 d; + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM32); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINC(d, eptr); + if (fc == d) RRETURN(MATCH_NOMATCH); + } + } + else +#endif + /* Not UTF mode */ + { + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM33); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (fc == *eptr++) RRETURN(MATCH_NOMATCH); + } + } + /* Control never gets here */ + } + + /* Maximize case */ + + else + { + pp = eptr; + +#ifdef SUPPORT_UTF + if (utf) + { + register pcre_uint32 d; + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLEN(d, eptr, len); + if (fc == d) break; + eptr += len; + } + if (possessive) continue; /* No backtracking */ + for(;;) + { + if (eptr <= pp) goto TAIL_RECURSE; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM34); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + eptr--; + BACKCHAR(eptr); + } + } + else +#endif + /* Not UTF mode */ + { + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + if (fc == *eptr) break; + eptr++; + } + if (possessive) continue; /* No backtracking */ + for (;;) + { + if (eptr == pp) goto TAIL_RECURSE; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM35); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + eptr--; + } + } + /* Control never gets here */ + } + } + /* Control never gets here */ + + /* Match a single character type repeatedly; several different opcodes + share code. This is very similar to the code for single characters, but we + repeat it in the interests of efficiency. */ + + case OP_TYPEEXACT: + min = max = GET2(ecode, 1); + minimize = TRUE; + ecode += 1 + IMM2_SIZE; + goto REPEATTYPE; + + case OP_TYPEUPTO: + case OP_TYPEMINUPTO: + min = 0; + max = GET2(ecode, 1); + minimize = *ecode == OP_TYPEMINUPTO; + ecode += 1 + IMM2_SIZE; + goto REPEATTYPE; + + case OP_TYPEPOSSTAR: + possessive = TRUE; + min = 0; + max = INT_MAX; + ecode++; + goto REPEATTYPE; + + case OP_TYPEPOSPLUS: + possessive = TRUE; + min = 1; + max = INT_MAX; + ecode++; + goto REPEATTYPE; + + case OP_TYPEPOSQUERY: + possessive = TRUE; + min = 0; + max = 1; + ecode++; + goto REPEATTYPE; + + case OP_TYPEPOSUPTO: + possessive = TRUE; + min = 0; + max = GET2(ecode, 1); + ecode += 1 + IMM2_SIZE; + goto REPEATTYPE; + + case OP_TYPESTAR: + case OP_TYPEMINSTAR: + case OP_TYPEPLUS: + case OP_TYPEMINPLUS: + case OP_TYPEQUERY: + case OP_TYPEMINQUERY: + c = *ecode++ - OP_TYPESTAR; + minimize = (c & 1) != 0; + min = rep_min[c]; /* Pick up values from tables; */ + max = rep_max[c]; /* zero for max => infinity */ + if (max == 0) max = INT_MAX; + + /* Common code for all repeated single character type matches. Note that + in UTF-8 mode, '.' matches a character of any length, but for the other + character types, the valid characters are all one-byte long. */ + + REPEATTYPE: + ctype = *ecode++; /* Code for the character type */ + +#ifdef SUPPORT_UCP + if (ctype == OP_PROP || ctype == OP_NOTPROP) + { + prop_fail_result = ctype == OP_NOTPROP; + prop_type = *ecode++; + prop_value = *ecode++; + } + else prop_type = -1; +#endif + + /* First, ensure the minimum number of matches are present. Use inline + code for maximizing the speed, and do the type test once at the start + (i.e. keep it out of the loop). Separate the UTF-8 code completely as that + is tidier. Also separate the UCP code, which can be the same for both UTF-8 + and single-bytes. */ + + if (min > 0) + { +#ifdef SUPPORT_UCP + if (prop_type >= 0) + { + switch(prop_type) + { + case PT_ANY: + if (prop_fail_result) RRETURN(MATCH_NOMATCH); + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + } + break; + + case PT_LAMP: + for (i = 1; i <= min; i++) + { + int chartype; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + chartype = UCD_CHARTYPE(c); + if ((chartype == ucp_Lu || + chartype == ucp_Ll || + chartype == ucp_Lt) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + break; + + case PT_GC: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ((UCD_CATEGORY(c) == prop_value) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + break; + + case PT_PC: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ((UCD_CHARTYPE(c) == prop_value) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + break; + + case PT_SC: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ((UCD_SCRIPT(c) == prop_value) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + break; + + case PT_ALNUM: + for (i = 1; i <= min; i++) + { + int category; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + category = UCD_CATEGORY(c); + if ((category == ucp_L || category == ucp_N) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + break; + + /* Perl space used to exclude VT, but from Perl 5.18 it is included, + which means that Perl space and POSIX space are now identical. PCRE + was changed at release 8.34. */ + + case PT_SPACE: /* Perl space */ + case PT_PXSPACE: /* POSIX space */ + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + switch(c) + { + HSPACE_CASES: + VSPACE_CASES: + if (prop_fail_result) RRETURN(MATCH_NOMATCH); + break; + + default: + if ((UCD_CATEGORY(c) == ucp_Z) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + break; + } + } + break; + + case PT_WORD: + for (i = 1; i <= min; i++) + { + int category; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + category = UCD_CATEGORY(c); + if ((category == ucp_L || category == ucp_N || c == CHAR_UNDERSCORE) + == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + break; + + case PT_CLIST: + for (i = 1; i <= min; i++) + { + const pcre_uint32 *cp; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + cp = PRIV(ucd_caseless_sets) + prop_value; + for (;;) + { + if (c < *cp) + { if (prop_fail_result) break; else { RRETURN(MATCH_NOMATCH); } } + if (c == *cp++) + { if (prop_fail_result) { RRETURN(MATCH_NOMATCH); } else break; } + } + } + break; + + case PT_UCNC: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT || + c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) || + c >= 0xe000) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + break; + + /* This should not occur */ + + default: + RRETURN(PCRE_ERROR_INTERNAL); + } + } + + /* Match extended Unicode sequences. We will get here only if the + support is in the binary; otherwise a compile-time error occurs. */ + + else if (ctype == OP_EXTUNI) + { + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + else + { + int lgb, rgb; + GETCHARINCTEST(c, eptr); + lgb = UCD_GRAPHBREAK(c); + while (eptr < md->end_subject) + { + int len = 1; + if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); } + rgb = UCD_GRAPHBREAK(c); + if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break; + lgb = rgb; + eptr += len; + } + } + CHECK_PARTIAL(); + } + } + + else +#endif /* SUPPORT_UCP */ + +/* Handle all other cases when the coding is UTF-8 */ + +#ifdef SUPPORT_UTF + if (utf) switch(ctype) + { + case OP_ANY: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH); + if (md->partial != 0 && + eptr + 1 >= md->end_subject && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + UCHAR21(eptr) == NLBLOCK->nl[0]) + { + md->hitend = TRUE; + if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); + } + eptr++; + ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++); + } + break; + + case OP_ALLANY: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + eptr++; + ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++); + } + break; + + case OP_ANYBYTE: + if (eptr > md->end_subject - min) RRETURN(MATCH_NOMATCH); + eptr += min; + break; + + case OP_ANYNL: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINC(c, eptr); + switch(c) + { + default: RRETURN(MATCH_NOMATCH); + + case CHAR_CR: + if (eptr < md->end_subject && UCHAR21(eptr) == CHAR_LF) eptr++; + break; + + case CHAR_LF: + break; + + case CHAR_VT: + case CHAR_FF: + case CHAR_NEL: +#ifndef EBCDIC + case 0x2028: + case 0x2029: +#endif /* Not EBCDIC */ + if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH); + break; + } + } + break; + + case OP_NOT_HSPACE: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINC(c, eptr); + switch(c) + { + HSPACE_CASES: RRETURN(MATCH_NOMATCH); /* Byte and multibyte cases */ + default: break; + } + } + break; + + case OP_HSPACE: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINC(c, eptr); + switch(c) + { + HSPACE_CASES: break; /* Byte and multibyte cases */ + default: RRETURN(MATCH_NOMATCH); + } + } + break; + + case OP_NOT_VSPACE: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINC(c, eptr); + switch(c) + { + VSPACE_CASES: RRETURN(MATCH_NOMATCH); + default: break; + } + } + break; + + case OP_VSPACE: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINC(c, eptr); + switch(c) + { + VSPACE_CASES: break; + default: RRETURN(MATCH_NOMATCH); + } + } + break; + + case OP_NOT_DIGIT: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINC(c, eptr); + if (c < 128 && (md->ctypes[c] & ctype_digit) != 0) + RRETURN(MATCH_NOMATCH); + } + break; + + case OP_DIGIT: + for (i = 1; i <= min; i++) + { + pcre_uint32 cc; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + cc = UCHAR21(eptr); + if (cc >= 128 || (md->ctypes[cc] & ctype_digit) == 0) + RRETURN(MATCH_NOMATCH); + eptr++; + /* No need to skip more bytes - we know it's a 1-byte character */ + } + break; + + case OP_NOT_WHITESPACE: + for (i = 1; i <= min; i++) + { + pcre_uint32 cc; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + cc = UCHAR21(eptr); + if (cc < 128 && (md->ctypes[cc] & ctype_space) != 0) + RRETURN(MATCH_NOMATCH); + eptr++; + ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++); + } + break; + + case OP_WHITESPACE: + for (i = 1; i <= min; i++) + { + pcre_uint32 cc; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + cc = UCHAR21(eptr); + if (cc >= 128 || (md->ctypes[cc] & ctype_space) == 0) + RRETURN(MATCH_NOMATCH); + eptr++; + /* No need to skip more bytes - we know it's a 1-byte character */ + } + break; + + case OP_NOT_WORDCHAR: + for (i = 1; i <= min; i++) + { + pcre_uint32 cc; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + cc = UCHAR21(eptr); + if (cc < 128 && (md->ctypes[cc] & ctype_word) != 0) + RRETURN(MATCH_NOMATCH); + eptr++; + ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++); + } + break; + + case OP_WORDCHAR: + for (i = 1; i <= min; i++) + { + pcre_uint32 cc; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + cc = UCHAR21(eptr); + if (cc >= 128 || (md->ctypes[cc] & ctype_word) == 0) + RRETURN(MATCH_NOMATCH); + eptr++; + /* No need to skip more bytes - we know it's a 1-byte character */ + } + break; + + default: + RRETURN(PCRE_ERROR_INTERNAL); + } /* End switch(ctype) */ + + else +#endif /* SUPPORT_UTF */ + + /* Code for the non-UTF-8 case for minimum matching of operators other + than OP_PROP and OP_NOTPROP. */ + + switch(ctype) + { + case OP_ANY: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH); + if (md->partial != 0 && + eptr + 1 >= md->end_subject && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + *eptr == NLBLOCK->nl[0]) + { + md->hitend = TRUE; + if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); + } + eptr++; + } + break; + + case OP_ALLANY: + if (eptr > md->end_subject - min) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + eptr += min; + break; + + case OP_ANYBYTE: + if (eptr > md->end_subject - min) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + eptr += min; + break; + + case OP_ANYNL: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + switch(*eptr++) + { + default: RRETURN(MATCH_NOMATCH); + + case CHAR_CR: + if (eptr < md->end_subject && *eptr == CHAR_LF) eptr++; + break; + + case CHAR_LF: + break; + + case CHAR_VT: + case CHAR_FF: + case CHAR_NEL: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + case 0x2028: + case 0x2029: +#endif + if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH); + break; + } + } + break; + + case OP_NOT_HSPACE: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + switch(*eptr++) + { + default: break; + HSPACE_BYTE_CASES: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + HSPACE_MULTIBYTE_CASES: +#endif + RRETURN(MATCH_NOMATCH); + } + } + break; + + case OP_HSPACE: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + switch(*eptr++) + { + default: RRETURN(MATCH_NOMATCH); + HSPACE_BYTE_CASES: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + HSPACE_MULTIBYTE_CASES: +#endif + break; + } + } + break; + + case OP_NOT_VSPACE: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + switch(*eptr++) + { + VSPACE_BYTE_CASES: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + VSPACE_MULTIBYTE_CASES: +#endif + RRETURN(MATCH_NOMATCH); + default: break; + } + } + break; + + case OP_VSPACE: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + switch(*eptr++) + { + default: RRETURN(MATCH_NOMATCH); + VSPACE_BYTE_CASES: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + VSPACE_MULTIBYTE_CASES: +#endif + break; + } + } + break; + + case OP_NOT_DIGIT: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_digit) != 0) + RRETURN(MATCH_NOMATCH); + eptr++; + } + break; + + case OP_DIGIT: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_digit) == 0) + RRETURN(MATCH_NOMATCH); + eptr++; + } + break; + + case OP_NOT_WHITESPACE: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_space) != 0) + RRETURN(MATCH_NOMATCH); + eptr++; + } + break; + + case OP_WHITESPACE: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_space) == 0) + RRETURN(MATCH_NOMATCH); + eptr++; + } + break; + + case OP_NOT_WORDCHAR: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_word) != 0) + RRETURN(MATCH_NOMATCH); + eptr++; + } + break; + + case OP_WORDCHAR: + for (i = 1; i <= min; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_word) == 0) + RRETURN(MATCH_NOMATCH); + eptr++; + } + break; + + default: + RRETURN(PCRE_ERROR_INTERNAL); + } + } + + /* If min = max, continue at the same level without recursing */ + + if (min == max) continue; + + /* If minimizing, we have to test the rest of the pattern before each + subsequent match. Again, separate the UTF-8 case for speed, and also + separate the UCP cases. */ + + if (minimize) + { +#ifdef SUPPORT_UCP + if (prop_type >= 0) + { + switch(prop_type) + { + case PT_ANY: + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM36); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if (prop_fail_result) RRETURN(MATCH_NOMATCH); + } + /* Control never gets here */ + + case PT_LAMP: + for (fi = min;; fi++) + { + int chartype; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM37); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + chartype = UCD_CHARTYPE(c); + if ((chartype == ucp_Lu || + chartype == ucp_Ll || + chartype == ucp_Lt) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + /* Control never gets here */ + + case PT_GC: + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM38); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ((UCD_CATEGORY(c) == prop_value) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + /* Control never gets here */ + + case PT_PC: + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM39); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ((UCD_CHARTYPE(c) == prop_value) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + /* Control never gets here */ + + case PT_SC: + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM40); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ((UCD_SCRIPT(c) == prop_value) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + /* Control never gets here */ + + case PT_ALNUM: + for (fi = min;; fi++) + { + int category; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM59); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + category = UCD_CATEGORY(c); + if ((category == ucp_L || category == ucp_N) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + /* Control never gets here */ + + /* Perl space used to exclude VT, but from Perl 5.18 it is included, + which means that Perl space and POSIX space are now identical. PCRE + was changed at release 8.34. */ + + case PT_SPACE: /* Perl space */ + case PT_PXSPACE: /* POSIX space */ + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM61); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + switch(c) + { + HSPACE_CASES: + VSPACE_CASES: + if (prop_fail_result) RRETURN(MATCH_NOMATCH); + break; + + default: + if ((UCD_CATEGORY(c) == ucp_Z) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + break; + } + } + /* Control never gets here */ + + case PT_WORD: + for (fi = min;; fi++) + { + int category; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM62); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + category = UCD_CATEGORY(c); + if ((category == ucp_L || + category == ucp_N || + c == CHAR_UNDERSCORE) + == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + /* Control never gets here */ + + case PT_CLIST: + for (fi = min;; fi++) + { + const pcre_uint32 *cp; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM67); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + cp = PRIV(ucd_caseless_sets) + prop_value; + for (;;) + { + if (c < *cp) + { if (prop_fail_result) break; else { RRETURN(MATCH_NOMATCH); } } + if (c == *cp++) + { if (prop_fail_result) { RRETURN(MATCH_NOMATCH); } else break; } + } + } + /* Control never gets here */ + + case PT_UCNC: + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM60); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + GETCHARINCTEST(c, eptr); + if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT || + c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) || + c >= 0xe000) == prop_fail_result) + RRETURN(MATCH_NOMATCH); + } + /* Control never gets here */ + + /* This should never occur */ + default: + RRETURN(PCRE_ERROR_INTERNAL); + } + } + + /* Match extended Unicode sequences. We will get here only if the + support is in the binary; otherwise a compile-time error occurs. */ + + else if (ctype == OP_EXTUNI) + { + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM41); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + else + { + int lgb, rgb; + GETCHARINCTEST(c, eptr); + lgb = UCD_GRAPHBREAK(c); + while (eptr < md->end_subject) + { + int len = 1; + if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); } + rgb = UCD_GRAPHBREAK(c); + if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break; + lgb = rgb; + eptr += len; + } + } + CHECK_PARTIAL(); + } + } + else +#endif /* SUPPORT_UCP */ + +#ifdef SUPPORT_UTF + if (utf) + { + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM42); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (ctype == OP_ANY && IS_NEWLINE(eptr)) + RRETURN(MATCH_NOMATCH); + GETCHARINC(c, eptr); + switch(ctype) + { + case OP_ANY: /* This is the non-NL case */ + if (md->partial != 0 && /* Take care with CRLF partial */ + eptr >= md->end_subject && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + c == NLBLOCK->nl[0]) + { + md->hitend = TRUE; + if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); + } + break; + + case OP_ALLANY: + case OP_ANYBYTE: + break; + + case OP_ANYNL: + switch(c) + { + default: RRETURN(MATCH_NOMATCH); + case CHAR_CR: + if (eptr < md->end_subject && UCHAR21(eptr) == CHAR_LF) eptr++; + break; + + case CHAR_LF: + break; + + case CHAR_VT: + case CHAR_FF: + case CHAR_NEL: +#ifndef EBCDIC + case 0x2028: + case 0x2029: +#endif /* Not EBCDIC */ + if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH); + break; + } + break; + + case OP_NOT_HSPACE: + switch(c) + { + HSPACE_CASES: RRETURN(MATCH_NOMATCH); + default: break; + } + break; + + case OP_HSPACE: + switch(c) + { + HSPACE_CASES: break; + default: RRETURN(MATCH_NOMATCH); + } + break; + + case OP_NOT_VSPACE: + switch(c) + { + VSPACE_CASES: RRETURN(MATCH_NOMATCH); + default: break; + } + break; + + case OP_VSPACE: + switch(c) + { + VSPACE_CASES: break; + default: RRETURN(MATCH_NOMATCH); + } + break; + + case OP_NOT_DIGIT: + if (c < 256 && (md->ctypes[c] & ctype_digit) != 0) + RRETURN(MATCH_NOMATCH); + break; + + case OP_DIGIT: + if (c >= 256 || (md->ctypes[c] & ctype_digit) == 0) + RRETURN(MATCH_NOMATCH); + break; + + case OP_NOT_WHITESPACE: + if (c < 256 && (md->ctypes[c] & ctype_space) != 0) + RRETURN(MATCH_NOMATCH); + break; + + case OP_WHITESPACE: + if (c >= 256 || (md->ctypes[c] & ctype_space) == 0) + RRETURN(MATCH_NOMATCH); + break; + + case OP_NOT_WORDCHAR: + if (c < 256 && (md->ctypes[c] & ctype_word) != 0) + RRETURN(MATCH_NOMATCH); + break; + + case OP_WORDCHAR: + if (c >= 256 || (md->ctypes[c] & ctype_word) == 0) + RRETURN(MATCH_NOMATCH); + break; + + default: + RRETURN(PCRE_ERROR_INTERNAL); + } + } + } + else +#endif + /* Not UTF mode */ + { + for (fi = min;; fi++) + { + RMATCH(eptr, ecode, offset_top, md, eptrb, RM43); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + if (fi >= max) RRETURN(MATCH_NOMATCH); + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + RRETURN(MATCH_NOMATCH); + } + if (ctype == OP_ANY && IS_NEWLINE(eptr)) + RRETURN(MATCH_NOMATCH); + c = *eptr++; + switch(ctype) + { + case OP_ANY: /* This is the non-NL case */ + if (md->partial != 0 && /* Take care with CRLF partial */ + eptr >= md->end_subject && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + c == NLBLOCK->nl[0]) + { + md->hitend = TRUE; + if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); + } + break; + + case OP_ALLANY: + case OP_ANYBYTE: + break; + + case OP_ANYNL: + switch(c) + { + default: RRETURN(MATCH_NOMATCH); + case CHAR_CR: + if (eptr < md->end_subject && *eptr == CHAR_LF) eptr++; + break; + + case CHAR_LF: + break; + + case CHAR_VT: + case CHAR_FF: + case CHAR_NEL: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + case 0x2028: + case 0x2029: +#endif + if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH); + break; + } + break; + + case OP_NOT_HSPACE: + switch(c) + { + default: break; + HSPACE_BYTE_CASES: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + HSPACE_MULTIBYTE_CASES: +#endif + RRETURN(MATCH_NOMATCH); + } + break; + + case OP_HSPACE: + switch(c) + { + default: RRETURN(MATCH_NOMATCH); + HSPACE_BYTE_CASES: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + HSPACE_MULTIBYTE_CASES: +#endif + break; + } + break; + + case OP_NOT_VSPACE: + switch(c) + { + default: break; + VSPACE_BYTE_CASES: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + VSPACE_MULTIBYTE_CASES: +#endif + RRETURN(MATCH_NOMATCH); + } + break; + + case OP_VSPACE: + switch(c) + { + default: RRETURN(MATCH_NOMATCH); + VSPACE_BYTE_CASES: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + VSPACE_MULTIBYTE_CASES: +#endif + break; + } + break; + + case OP_NOT_DIGIT: + if (MAX_255(c) && (md->ctypes[c] & ctype_digit) != 0) RRETURN(MATCH_NOMATCH); + break; + + case OP_DIGIT: + if (!MAX_255(c) || (md->ctypes[c] & ctype_digit) == 0) RRETURN(MATCH_NOMATCH); + break; + + case OP_NOT_WHITESPACE: + if (MAX_255(c) && (md->ctypes[c] & ctype_space) != 0) RRETURN(MATCH_NOMATCH); + break; + + case OP_WHITESPACE: + if (!MAX_255(c) || (md->ctypes[c] & ctype_space) == 0) RRETURN(MATCH_NOMATCH); + break; + + case OP_NOT_WORDCHAR: + if (MAX_255(c) && (md->ctypes[c] & ctype_word) != 0) RRETURN(MATCH_NOMATCH); + break; + + case OP_WORDCHAR: + if (!MAX_255(c) || (md->ctypes[c] & ctype_word) == 0) RRETURN(MATCH_NOMATCH); + break; + + default: + RRETURN(PCRE_ERROR_INTERNAL); + } + } + } + /* Control never gets here */ + } + + /* If maximizing, it is worth using inline code for speed, doing the type + test once at the start (i.e. keep it out of the loop). Again, keep the + UTF-8 and UCP stuff separate. */ + + else + { + pp = eptr; /* Remember where we started */ + +#ifdef SUPPORT_UCP + if (prop_type >= 0) + { + switch(prop_type) + { + case PT_ANY: + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLENTEST(c, eptr, len); + if (prop_fail_result) break; + eptr+= len; + } + break; + + case PT_LAMP: + for (i = min; i < max; i++) + { + int chartype; + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLENTEST(c, eptr, len); + chartype = UCD_CHARTYPE(c); + if ((chartype == ucp_Lu || + chartype == ucp_Ll || + chartype == ucp_Lt) == prop_fail_result) + break; + eptr+= len; + } + break; + + case PT_GC: + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLENTEST(c, eptr, len); + if ((UCD_CATEGORY(c) == prop_value) == prop_fail_result) break; + eptr+= len; + } + break; + + case PT_PC: + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLENTEST(c, eptr, len); + if ((UCD_CHARTYPE(c) == prop_value) == prop_fail_result) break; + eptr+= len; + } + break; + + case PT_SC: + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLENTEST(c, eptr, len); + if ((UCD_SCRIPT(c) == prop_value) == prop_fail_result) break; + eptr+= len; + } + break; + + case PT_ALNUM: + for (i = min; i < max; i++) + { + int category; + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLENTEST(c, eptr, len); + category = UCD_CATEGORY(c); + if ((category == ucp_L || category == ucp_N) == prop_fail_result) + break; + eptr+= len; + } + break; + + /* Perl space used to exclude VT, but from Perl 5.18 it is included, + which means that Perl space and POSIX space are now identical. PCRE + was changed at release 8.34. */ + + case PT_SPACE: /* Perl space */ + case PT_PXSPACE: /* POSIX space */ + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLENTEST(c, eptr, len); + switch(c) + { + HSPACE_CASES: + VSPACE_CASES: + if (prop_fail_result) goto ENDLOOP99; /* Break the loop */ + break; + + default: + if ((UCD_CATEGORY(c) == ucp_Z) == prop_fail_result) + goto ENDLOOP99; /* Break the loop */ + break; + } + eptr+= len; + } + ENDLOOP99: + break; + + case PT_WORD: + for (i = min; i < max; i++) + { + int category; + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLENTEST(c, eptr, len); + category = UCD_CATEGORY(c); + if ((category == ucp_L || category == ucp_N || + c == CHAR_UNDERSCORE) == prop_fail_result) + break; + eptr+= len; + } + break; + + case PT_CLIST: + for (i = min; i < max; i++) + { + const pcre_uint32 *cp; + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLENTEST(c, eptr, len); + cp = PRIV(ucd_caseless_sets) + prop_value; + for (;;) + { + if (c < *cp) + { if (prop_fail_result) break; else goto GOT_MAX; } + if (c == *cp++) + { if (prop_fail_result) goto GOT_MAX; else break; } + } + eptr += len; + } + GOT_MAX: + break; + + case PT_UCNC: + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLENTEST(c, eptr, len); + if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT || + c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) || + c >= 0xe000) == prop_fail_result) + break; + eptr += len; + } + break; + + default: + RRETURN(PCRE_ERROR_INTERNAL); + } + + /* eptr is now past the end of the maximum run */ + + if (possessive) continue; /* No backtracking */ + for(;;) + { + if (eptr <= pp) goto TAIL_RECURSE; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM44); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + eptr--; + if (utf) BACKCHAR(eptr); + } + } + + /* Match extended Unicode grapheme clusters. We will get here only if the + support is in the binary; otherwise a compile-time error occurs. */ + + else if (ctype == OP_EXTUNI) + { + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + else + { + int lgb, rgb; + GETCHARINCTEST(c, eptr); + lgb = UCD_GRAPHBREAK(c); + while (eptr < md->end_subject) + { + int len = 1; + if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); } + rgb = UCD_GRAPHBREAK(c); + if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break; + lgb = rgb; + eptr += len; + } + } + CHECK_PARTIAL(); + } + + /* eptr is now past the end of the maximum run */ + + if (possessive) continue; /* No backtracking */ + + /* We use <= pp rather than == pp to detect the start of the run while + backtracking because the use of \C in UTF mode can cause BACKCHAR to + move back past pp. This is just palliative; the use of \C in UTF mode + is fraught with danger. */ + + for(;;) + { + int lgb, rgb; + PCRE_PUCHAR fptr; + + if (eptr <= pp) goto TAIL_RECURSE; /* At start of char run */ + RMATCH(eptr, ecode, offset_top, md, eptrb, RM45); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + + /* Backtracking over an extended grapheme cluster involves inspecting + the previous two characters (if present) to see if a break is + permitted between them. */ + + eptr--; + if (!utf) c = *eptr; else + { + BACKCHAR(eptr); + GETCHAR(c, eptr); + } + rgb = UCD_GRAPHBREAK(c); + + for (;;) + { + if (eptr <= pp) goto TAIL_RECURSE; /* At start of char run */ + fptr = eptr - 1; + if (!utf) c = *fptr; else + { + BACKCHAR(fptr); + GETCHAR(c, fptr); + } + lgb = UCD_GRAPHBREAK(c); + if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break; + eptr = fptr; + rgb = lgb; + } + } + } + + else +#endif /* SUPPORT_UCP */ + +#ifdef SUPPORT_UTF + if (utf) + { + switch(ctype) + { + case OP_ANY: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + if (IS_NEWLINE(eptr)) break; + if (md->partial != 0 && /* Take care with CRLF partial */ + eptr + 1 >= md->end_subject && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + UCHAR21(eptr) == NLBLOCK->nl[0]) + { + md->hitend = TRUE; + if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); + } + eptr++; + ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++); + } + break; + + case OP_ALLANY: + if (max < INT_MAX) + { + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + eptr++; + ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++); + } + } + else + { + eptr = md->end_subject; /* Unlimited UTF-8 repeat */ + SCHECK_PARTIAL(); + } + break; + + /* The byte case is the same as non-UTF8 */ + + case OP_ANYBYTE: + c = max - min; + if (c > (unsigned int)(md->end_subject - eptr)) + { + eptr = md->end_subject; + SCHECK_PARTIAL(); + } + else eptr += c; + break; + + case OP_ANYNL: + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLEN(c, eptr, len); + if (c == CHAR_CR) + { + if (++eptr >= md->end_subject) break; + if (UCHAR21(eptr) == CHAR_LF) eptr++; + } + else + { + if (c != CHAR_LF && + (md->bsr_anycrlf || + (c != CHAR_VT && c != CHAR_FF && c != CHAR_NEL +#ifndef EBCDIC + && c != 0x2028 && c != 0x2029 +#endif /* Not EBCDIC */ + ))) + break; + eptr += len; + } + } + break; + + case OP_NOT_HSPACE: + case OP_HSPACE: + for (i = min; i < max; i++) + { + BOOL gotspace; + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLEN(c, eptr, len); + switch(c) + { + HSPACE_CASES: gotspace = TRUE; break; + default: gotspace = FALSE; break; + } + if (gotspace == (ctype == OP_NOT_HSPACE)) break; + eptr += len; + } + break; + + case OP_NOT_VSPACE: + case OP_VSPACE: + for (i = min; i < max; i++) + { + BOOL gotspace; + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLEN(c, eptr, len); + switch(c) + { + VSPACE_CASES: gotspace = TRUE; break; + default: gotspace = FALSE; break; + } + if (gotspace == (ctype == OP_NOT_VSPACE)) break; + eptr += len; + } + break; + + case OP_NOT_DIGIT: + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLEN(c, eptr, len); + if (c < 256 && (md->ctypes[c] & ctype_digit) != 0) break; + eptr+= len; + } + break; + + case OP_DIGIT: + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLEN(c, eptr, len); + if (c >= 256 ||(md->ctypes[c] & ctype_digit) == 0) break; + eptr+= len; + } + break; + + case OP_NOT_WHITESPACE: + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLEN(c, eptr, len); + if (c < 256 && (md->ctypes[c] & ctype_space) != 0) break; + eptr+= len; + } + break; + + case OP_WHITESPACE: + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLEN(c, eptr, len); + if (c >= 256 ||(md->ctypes[c] & ctype_space) == 0) break; + eptr+= len; + } + break; + + case OP_NOT_WORDCHAR: + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLEN(c, eptr, len); + if (c < 256 && (md->ctypes[c] & ctype_word) != 0) break; + eptr+= len; + } + break; + + case OP_WORDCHAR: + for (i = min; i < max; i++) + { + int len = 1; + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + GETCHARLEN(c, eptr, len); + if (c >= 256 || (md->ctypes[c] & ctype_word) == 0) break; + eptr+= len; + } + break; + + default: + RRETURN(PCRE_ERROR_INTERNAL); + } + + if (possessive) continue; /* No backtracking */ + for(;;) + { + if (eptr <= pp) goto TAIL_RECURSE; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM46); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + eptr--; + BACKCHAR(eptr); + if (ctype == OP_ANYNL && eptr > pp && UCHAR21(eptr) == CHAR_NL && + UCHAR21(eptr - 1) == CHAR_CR) eptr--; + } + } + else +#endif /* SUPPORT_UTF */ + /* Not UTF mode */ + { + switch(ctype) + { + case OP_ANY: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + if (IS_NEWLINE(eptr)) break; + if (md->partial != 0 && /* Take care with CRLF partial */ + eptr + 1 >= md->end_subject && + NLBLOCK->nltype == NLTYPE_FIXED && + NLBLOCK->nllen == 2 && + *eptr == NLBLOCK->nl[0]) + { + md->hitend = TRUE; + if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); + } + eptr++; + } + break; + + case OP_ALLANY: + case OP_ANYBYTE: + c = max - min; + if (c > (unsigned int)(md->end_subject - eptr)) + { + eptr = md->end_subject; + SCHECK_PARTIAL(); + } + else eptr += c; + break; + + case OP_ANYNL: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + c = *eptr; + if (c == CHAR_CR) + { + if (++eptr >= md->end_subject) break; + if (*eptr == CHAR_LF) eptr++; + } + else + { + if (c != CHAR_LF && (md->bsr_anycrlf || + (c != CHAR_VT && c != CHAR_FF && c != CHAR_NEL +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + && c != 0x2028 && c != 0x2029 +#endif + ))) break; + eptr++; + } + } + break; + + case OP_NOT_HSPACE: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + switch(*eptr) + { + default: eptr++; break; + HSPACE_BYTE_CASES: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + HSPACE_MULTIBYTE_CASES: +#endif + goto ENDLOOP00; + } + } + ENDLOOP00: + break; + + case OP_HSPACE: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + switch(*eptr) + { + default: goto ENDLOOP01; + HSPACE_BYTE_CASES: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + HSPACE_MULTIBYTE_CASES: +#endif + eptr++; break; + } + } + ENDLOOP01: + break; + + case OP_NOT_VSPACE: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + switch(*eptr) + { + default: eptr++; break; + VSPACE_BYTE_CASES: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + VSPACE_MULTIBYTE_CASES: +#endif + goto ENDLOOP02; + } + } + ENDLOOP02: + break; + + case OP_VSPACE: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + switch(*eptr) + { + default: goto ENDLOOP03; + VSPACE_BYTE_CASES: +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + VSPACE_MULTIBYTE_CASES: +#endif + eptr++; break; + } + } + ENDLOOP03: + break; + + case OP_NOT_DIGIT: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_digit) != 0) break; + eptr++; + } + break; + + case OP_DIGIT: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_digit) == 0) break; + eptr++; + } + break; + + case OP_NOT_WHITESPACE: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_space) != 0) break; + eptr++; + } + break; + + case OP_WHITESPACE: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_space) == 0) break; + eptr++; + } + break; + + case OP_NOT_WORDCHAR: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_word) != 0) break; + eptr++; + } + break; + + case OP_WORDCHAR: + for (i = min; i < max; i++) + { + if (eptr >= md->end_subject) + { + SCHECK_PARTIAL(); + break; + } + if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_word) == 0) break; + eptr++; + } + break; + + default: + RRETURN(PCRE_ERROR_INTERNAL); + } + + if (possessive) continue; /* No backtracking */ + for (;;) + { + if (eptr == pp) goto TAIL_RECURSE; + RMATCH(eptr, ecode, offset_top, md, eptrb, RM47); + if (rrc != MATCH_NOMATCH) RRETURN(rrc); + eptr--; + if (ctype == OP_ANYNL && eptr > pp && *eptr == CHAR_LF && + eptr[-1] == CHAR_CR) eptr--; + } + } + + /* Control never gets here */ + } + + /* There's been some horrible disaster. Arrival here can only mean there is + something seriously wrong in the code above or the OP_xxx definitions. */ + + default: + DPRINTF(("Unknown opcode %d\n", *ecode)); + RRETURN(PCRE_ERROR_UNKNOWN_OPCODE); + } + + /* Do not stick any code in here without much thought; it is assumed + that "continue" in the code above comes out to here to repeat the main + loop. */ + + } /* End of main loop */ +/* Control never reaches here */ + + +/* When compiling to use the heap rather than the stack for recursive calls to +match(), the RRETURN() macro jumps here. The number that is saved in +frame->Xwhere indicates which label we actually want to return to. */ + +#ifdef NO_RECURSE +#define LBL(val) case val: goto L_RM##val; +HEAP_RETURN: +switch (frame->Xwhere) + { + LBL( 1) LBL( 2) LBL( 3) LBL( 4) LBL( 5) LBL( 6) LBL( 7) LBL( 8) + LBL( 9) LBL(10) LBL(11) LBL(12) LBL(13) LBL(14) LBL(15) LBL(17) + LBL(19) LBL(24) LBL(25) LBL(26) LBL(27) LBL(29) LBL(31) LBL(33) + LBL(35) LBL(43) LBL(47) LBL(48) LBL(49) LBL(50) LBL(51) LBL(52) + LBL(53) LBL(54) LBL(55) LBL(56) LBL(57) LBL(58) LBL(63) LBL(64) + LBL(65) LBL(66) +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + LBL(20) LBL(21) +#endif +#ifdef SUPPORT_UTF + LBL(16) LBL(18) + LBL(22) LBL(23) LBL(28) LBL(30) + LBL(32) LBL(34) LBL(42) LBL(46) +#ifdef SUPPORT_UCP + LBL(36) LBL(37) LBL(38) LBL(39) LBL(40) LBL(41) LBL(44) LBL(45) + LBL(59) LBL(60) LBL(61) LBL(62) LBL(67) +#endif /* SUPPORT_UCP */ +#endif /* SUPPORT_UTF */ + default: + DPRINTF(("jump error in pcre match: label %d non-existent\n", frame->Xwhere)); + return PCRE_ERROR_INTERNAL; + } +#undef LBL +#endif /* NO_RECURSE */ +} + + +/*************************************************************************** +**************************************************************************** + RECURSION IN THE match() FUNCTION + +Undefine all the macros that were defined above to handle this. */ + +#ifdef NO_RECURSE +#undef eptr +#undef ecode +#undef mstart +#undef offset_top +#undef eptrb +#undef flags + +#undef callpat +#undef charptr +#undef data +#undef next +#undef pp +#undef prev +#undef saved_eptr + +#undef new_recursive + +#undef cur_is_word +#undef condition +#undef prev_is_word + +#undef ctype +#undef length +#undef max +#undef min +#undef number +#undef offset +#undef op +#undef save_capture_last +#undef save_offset1 +#undef save_offset2 +#undef save_offset3 +#undef stacksave + +#undef newptrb + +#endif + +/* These two are defined as macros in both cases */ + +#undef fc +#undef fi + +/*************************************************************************** +***************************************************************************/ + + +#ifdef NO_RECURSE +/************************************************* +* Release allocated heap frames * +*************************************************/ + +/* This function releases all the allocated frames. The base frame is on the +machine stack, and so must not be freed. + +Argument: the address of the base frame +Returns: nothing +*/ + +static void +release_match_heapframes (heapframe *frame_base) +{ +heapframe *nextframe = frame_base->Xnextframe; +while (nextframe != NULL) + { + heapframe *oldframe = nextframe; + nextframe = nextframe->Xnextframe; + (PUBL(stack_free))(oldframe); + } +} +#endif + + +/************************************************* +* Execute a Regular Expression * +*************************************************/ + +/* This function applies a compiled re to a subject string and picks out +portions of the string if it matches. Two elements in the vector are set for +each substring: the offsets to the start and end of the substring. + +Arguments: + argument_re points to the compiled expression + extra_data points to extra data or is NULL + subject points to the subject string + length length of subject string (may contain binary zeros) + start_offset where to start in the subject string + options option bits + offsets points to a vector of ints to be filled in with offsets + offsetcount the number of elements in the vector + +Returns: > 0 => success; value is the number of elements filled in + = 0 => success, but offsets is not big enough + -1 => failed to match + < -1 => some kind of unexpected problem +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_exec(const pcre *argument_re, const pcre_extra *extra_data, + PCRE_SPTR subject, int length, int start_offset, int options, int *offsets, + int offsetcount) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre16_exec(const pcre16 *argument_re, const pcre16_extra *extra_data, + PCRE_SPTR16 subject, int length, int start_offset, int options, int *offsets, + int offsetcount) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre32_exec(const pcre32 *argument_re, const pcre32_extra *extra_data, + PCRE_SPTR32 subject, int length, int start_offset, int options, int *offsets, + int offsetcount) +#endif +{ +int rc, ocount, arg_offset_max; +int newline; +BOOL using_temporary_offsets = FALSE; +BOOL anchored; +BOOL startline; +BOOL firstline; +BOOL utf; +BOOL has_first_char = FALSE; +BOOL has_req_char = FALSE; +pcre_uchar first_char = 0; +pcre_uchar first_char2 = 0; +pcre_uchar req_char = 0; +pcre_uchar req_char2 = 0; +match_data match_block; +match_data *md = &match_block; +const pcre_uint8 *tables; +const pcre_uint8 *start_bits = NULL; +PCRE_PUCHAR start_match = (PCRE_PUCHAR)subject + start_offset; +PCRE_PUCHAR end_subject; +PCRE_PUCHAR start_partial = NULL; +PCRE_PUCHAR match_partial = NULL; +PCRE_PUCHAR req_char_ptr = start_match - 1; + +const pcre_study_data *study; +const REAL_PCRE *re = (const REAL_PCRE *)argument_re; + +#ifdef NO_RECURSE +heapframe frame_zero; +frame_zero.Xprevframe = NULL; /* Marks the top level */ +frame_zero.Xnextframe = NULL; /* None are allocated yet */ +md->match_frames_base = &frame_zero; +#endif + +/* Check for the special magic call that measures the size of the stack used +per recursive call of match(). Without the funny casting for sizeof, a Windows +compiler gave this error: "unary minus operator applied to unsigned type, +result still unsigned". Hopefully the cast fixes that. */ + +if (re == NULL && extra_data == NULL && subject == NULL && length == -999 && + start_offset == -999) +#ifdef NO_RECURSE + return -((int)sizeof(heapframe)); +#else + return match(NULL, NULL, NULL, 0, NULL, NULL, 0); +#endif + +/* Plausibility checks */ + +if ((options & ~PUBLIC_EXEC_OPTIONS) != 0) return PCRE_ERROR_BADOPTION; +if (re == NULL || subject == NULL || (offsets == NULL && offsetcount > 0)) + return PCRE_ERROR_NULL; +if (offsetcount < 0) return PCRE_ERROR_BADCOUNT; +if (length < 0) return PCRE_ERROR_BADLENGTH; +if (start_offset < 0 || start_offset > length) return PCRE_ERROR_BADOFFSET; + +/* Check that the first field in the block is the magic number. If it is not, +return with PCRE_ERROR_BADMAGIC. However, if the magic number is equal to +REVERSED_MAGIC_NUMBER we return with PCRE_ERROR_BADENDIANNESS, which +means that the pattern is likely compiled with different endianness. */ + +if (re->magic_number != MAGIC_NUMBER) + return re->magic_number == REVERSED_MAGIC_NUMBER? + PCRE_ERROR_BADENDIANNESS:PCRE_ERROR_BADMAGIC; +if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE; + +/* These two settings are used in the code for checking a UTF-8 string that +follows immediately afterwards. Other values in the md block are used only +during "normal" pcre_exec() processing, not when the JIT support is in use, +so they are set up later. */ + +/* PCRE_UTF16 has the same value as PCRE_UTF8. */ +utf = md->utf = (re->options & PCRE_UTF8) != 0; +md->partial = ((options & PCRE_PARTIAL_HARD) != 0)? 2 : + ((options & PCRE_PARTIAL_SOFT) != 0)? 1 : 0; + +/* Check a UTF-8 string if required. Pass back the character offset and error +code for an invalid string if a results vector is available. */ + +#ifdef SUPPORT_UTF +if (utf && (options & PCRE_NO_UTF8_CHECK) == 0) + { + int erroroffset; + int errorcode = PRIV(valid_utf)((PCRE_PUCHAR)subject, length, &erroroffset); + if (errorcode != 0) + { + if (offsetcount >= 2) + { + offsets[0] = erroroffset; + offsets[1] = errorcode; + } +#if defined COMPILE_PCRE8 + return (errorcode <= PCRE_UTF8_ERR5 && md->partial > 1)? + PCRE_ERROR_SHORTUTF8 : PCRE_ERROR_BADUTF8; +#elif defined COMPILE_PCRE16 + return (errorcode <= PCRE_UTF16_ERR1 && md->partial > 1)? + PCRE_ERROR_SHORTUTF16 : PCRE_ERROR_BADUTF16; +#elif defined COMPILE_PCRE32 + return PCRE_ERROR_BADUTF32; +#endif + } +#if defined COMPILE_PCRE8 || defined COMPILE_PCRE16 + /* Check that a start_offset points to the start of a UTF character. */ + if (start_offset > 0 && start_offset < length && + NOT_FIRSTCHAR(((PCRE_PUCHAR)subject)[start_offset])) + return PCRE_ERROR_BADUTF8_OFFSET; +#endif + } +#endif + +/* If the pattern was successfully studied with JIT support, run the JIT +executable instead of the rest of this function. Most options must be set at +compile time for the JIT code to be usable. Fallback to the normal code path if +an unsupported flag is set. */ + +#ifdef SUPPORT_JIT +if (extra_data != NULL + && (extra_data->flags & (PCRE_EXTRA_EXECUTABLE_JIT | + PCRE_EXTRA_TABLES)) == PCRE_EXTRA_EXECUTABLE_JIT + && extra_data->executable_jit != NULL + && (options & ~PUBLIC_JIT_EXEC_OPTIONS) == 0) + { + rc = PRIV(jit_exec)(extra_data, (const pcre_uchar *)subject, length, + start_offset, options, offsets, offsetcount); + + /* PCRE_ERROR_NULL means that the selected normal or partial matching + mode is not compiled. In this case we simply fallback to interpreter. */ + + if (rc != PCRE_ERROR_JIT_BADOPTION) return rc; + } +#endif + +/* Carry on with non-JIT matching. This information is for finding all the +numbers associated with a given name, for condition testing. */ + +md->name_table = (pcre_uchar *)re + re->name_table_offset; +md->name_count = re->name_count; +md->name_entry_size = re->name_entry_size; + +/* Fish out the optional data from the extra_data structure, first setting +the default values. */ + +study = NULL; +md->match_limit = MATCH_LIMIT; +md->match_limit_recursion = MATCH_LIMIT_RECURSION; +md->callout_data = NULL; + +/* The table pointer is always in native byte order. */ + +tables = re->tables; + +/* The two limit values override the defaults, whatever their value. */ + +if (extra_data != NULL) + { + unsigned long int flags = extra_data->flags; + if ((flags & PCRE_EXTRA_STUDY_DATA) != 0) + study = (const pcre_study_data *)extra_data->study_data; + if ((flags & PCRE_EXTRA_MATCH_LIMIT) != 0) + md->match_limit = extra_data->match_limit; + if ((flags & PCRE_EXTRA_MATCH_LIMIT_RECURSION) != 0) + md->match_limit_recursion = extra_data->match_limit_recursion; + if ((flags & PCRE_EXTRA_CALLOUT_DATA) != 0) + md->callout_data = extra_data->callout_data; + if ((flags & PCRE_EXTRA_TABLES) != 0) tables = extra_data->tables; + } + +/* Limits in the regex override only if they are smaller. */ + +if ((re->flags & PCRE_MLSET) != 0 && re->limit_match < md->match_limit) + md->match_limit = re->limit_match; + +if ((re->flags & PCRE_RLSET) != 0 && + re->limit_recursion < md->match_limit_recursion) + md->match_limit_recursion = re->limit_recursion; + +/* If the exec call supplied NULL for tables, use the inbuilt ones. This +is a feature that makes it possible to save compiled regex and re-use them +in other programs later. */ + +if (tables == NULL) tables = PRIV(default_tables); + +/* Set up other data */ + +anchored = ((re->options | options) & PCRE_ANCHORED) != 0; +startline = (re->flags & PCRE_STARTLINE) != 0; +firstline = (re->options & PCRE_FIRSTLINE) != 0; + +/* The code starts after the real_pcre block and the capture name table. */ + +md->start_code = (const pcre_uchar *)re + re->name_table_offset + + re->name_count * re->name_entry_size; + +md->start_subject = (PCRE_PUCHAR)subject; +md->start_offset = start_offset; +md->end_subject = md->start_subject + length; +end_subject = md->end_subject; + +md->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0; +md->use_ucp = (re->options & PCRE_UCP) != 0; +md->jscript_compat = (re->options & PCRE_JAVASCRIPT_COMPAT) != 0; +md->ignore_skip_arg = 0; + +/* Some options are unpacked into BOOL variables in the hope that testing +them will be faster than individual option bits. */ + +md->notbol = (options & PCRE_NOTBOL) != 0; +md->noteol = (options & PCRE_NOTEOL) != 0; +md->notempty = (options & PCRE_NOTEMPTY) != 0; +md->notempty_atstart = (options & PCRE_NOTEMPTY_ATSTART) != 0; + +md->hitend = FALSE; +md->mark = md->nomatch_mark = NULL; /* In case never set */ + +md->recursive = NULL; /* No recursion at top level */ +md->hasthen = (re->flags & PCRE_HASTHEN) != 0; + +md->lcc = tables + lcc_offset; +md->fcc = tables + fcc_offset; +md->ctypes = tables + ctypes_offset; + +/* Handle different \R options. */ + +switch (options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) + { + case 0: + if ((re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) != 0) + md->bsr_anycrlf = (re->options & PCRE_BSR_ANYCRLF) != 0; + else +#ifdef BSR_ANYCRLF + md->bsr_anycrlf = TRUE; +#else + md->bsr_anycrlf = FALSE; +#endif + break; + + case PCRE_BSR_ANYCRLF: + md->bsr_anycrlf = TRUE; + break; + + case PCRE_BSR_UNICODE: + md->bsr_anycrlf = FALSE; + break; + + default: return PCRE_ERROR_BADNEWLINE; + } + +/* Handle different types of newline. The three bits give eight cases. If +nothing is set at run time, whatever was used at compile time applies. */ + +switch ((((options & PCRE_NEWLINE_BITS) == 0)? re->options : + (pcre_uint32)options) & PCRE_NEWLINE_BITS) + { + case 0: newline = NEWLINE; break; /* Compile-time default */ + case PCRE_NEWLINE_CR: newline = CHAR_CR; break; + case PCRE_NEWLINE_LF: newline = CHAR_NL; break; + case PCRE_NEWLINE_CR+ + PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break; + case PCRE_NEWLINE_ANY: newline = -1; break; + case PCRE_NEWLINE_ANYCRLF: newline = -2; break; + default: return PCRE_ERROR_BADNEWLINE; + } + +if (newline == -2) + { + md->nltype = NLTYPE_ANYCRLF; + } +else if (newline < 0) + { + md->nltype = NLTYPE_ANY; + } +else + { + md->nltype = NLTYPE_FIXED; + if (newline > 255) + { + md->nllen = 2; + md->nl[0] = (newline >> 8) & 255; + md->nl[1] = newline & 255; + } + else + { + md->nllen = 1; + md->nl[0] = newline; + } + } + +/* Partial matching was originally supported only for a restricted set of +regexes; from release 8.00 there are no restrictions, but the bits are still +defined (though never set). So there's no harm in leaving this code. */ + +if (md->partial && (re->flags & PCRE_NOPARTIAL) != 0) + return PCRE_ERROR_BADPARTIAL; + +/* If the expression has got more back references than the offsets supplied can +hold, we get a temporary chunk of working store to use during the matching. +Otherwise, we can use the vector supplied, rounding down its size to a multiple +of 3. */ + +ocount = offsetcount - (offsetcount % 3); +arg_offset_max = (2*ocount)/3; + +if (re->top_backref > 0 && re->top_backref >= ocount/3) + { + ocount = re->top_backref * 3 + 3; + md->offset_vector = (int *)(PUBL(malloc))(ocount * sizeof(int)); + if (md->offset_vector == NULL) return PCRE_ERROR_NOMEMORY; + using_temporary_offsets = TRUE; + DPRINTF(("Got memory to hold back references\n")); + } +else md->offset_vector = offsets; +md->offset_end = ocount; +md->offset_max = (2*ocount)/3; +md->capture_last = 0; + +/* Reset the working variable associated with each extraction. These should +never be used unless previously set, but they get saved and restored, and so we +initialize them to avoid reading uninitialized locations. Also, unset the +offsets for the matched string. This is really just for tidiness with callouts, +in case they inspect these fields. */ + +if (md->offset_vector != NULL) + { + register int *iptr = md->offset_vector + ocount; + register int *iend = iptr - re->top_bracket; + if (iend < md->offset_vector + 2) iend = md->offset_vector + 2; + while (--iptr >= iend) *iptr = -1; + if (offsetcount > 0) md->offset_vector[0] = -1; + if (offsetcount > 1) md->offset_vector[1] = -1; + } + +/* Set up the first character to match, if available. The first_char value is +never set for an anchored regular expression, but the anchoring may be forced +at run time, so we have to test for anchoring. The first char may be unset for +an unanchored pattern, of course. If there's no first char and the pattern was +studied, there may be a bitmap of possible first characters. */ + +if (!anchored) + { + if ((re->flags & PCRE_FIRSTSET) != 0) + { + has_first_char = TRUE; + first_char = first_char2 = (pcre_uchar)(re->first_char); + if ((re->flags & PCRE_FCH_CASELESS) != 0) + { + first_char2 = TABLE_GET(first_char, md->fcc, first_char); +#if defined SUPPORT_UCP && !(defined COMPILE_PCRE8) + if (utf && first_char > 127) + first_char2 = UCD_OTHERCASE(first_char); +#endif + } + } + else + if (!startline && study != NULL && + (study->flags & PCRE_STUDY_MAPPED) != 0) + start_bits = study->start_bits; + } + +/* For anchored or unanchored matches, there may be a "last known required +character" set. */ + +if ((re->flags & PCRE_REQCHSET) != 0) + { + has_req_char = TRUE; + req_char = req_char2 = (pcre_uchar)(re->req_char); + if ((re->flags & PCRE_RCH_CASELESS) != 0) + { + req_char2 = TABLE_GET(req_char, md->fcc, req_char); +#if defined SUPPORT_UCP && !(defined COMPILE_PCRE8) + if (utf && req_char > 127) + req_char2 = UCD_OTHERCASE(req_char); +#endif + } + } + + +/* ==========================================================================*/ + +/* Loop for handling unanchored repeated matching attempts; for anchored regexs +the loop runs just once. */ + +for(;;) + { + PCRE_PUCHAR save_end_subject = end_subject; + PCRE_PUCHAR new_start_match; + + /* If firstline is TRUE, the start of the match is constrained to the first + line of a multiline string. That is, the match must be before or at the first + newline. Implement this by temporarily adjusting end_subject so that we stop + scanning at a newline. If the match fails at the newline, later code breaks + this loop. */ + + if (firstline) + { + PCRE_PUCHAR t = start_match; +#ifdef SUPPORT_UTF + if (utf) + { + while (t < md->end_subject && !IS_NEWLINE(t)) + { + t++; + ACROSSCHAR(t < end_subject, *t, t++); + } + } + else +#endif + while (t < md->end_subject && !IS_NEWLINE(t)) t++; + end_subject = t; + } + + /* There are some optimizations that avoid running the match if a known + starting point is not found, or if a known later character is not present. + However, there is an option that disables these, for testing and for ensuring + that all callouts do actually occur. The option can be set in the regex by + (*NO_START_OPT) or passed in match-time options. */ + + if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0) + { + /* Advance to a unique first char if there is one. */ + + if (has_first_char) + { + pcre_uchar smc; + + if (first_char != first_char2) + while (start_match < end_subject && + (smc = UCHAR21TEST(start_match)) != first_char && smc != first_char2) + start_match++; + else + while (start_match < end_subject && UCHAR21TEST(start_match) != first_char) + start_match++; + } + + /* Or to just after a linebreak for a multiline match */ + + else if (startline) + { + if (start_match > md->start_subject + start_offset) + { +#ifdef SUPPORT_UTF + if (utf) + { + while (start_match < end_subject && !WAS_NEWLINE(start_match)) + { + start_match++; + ACROSSCHAR(start_match < end_subject, *start_match, + start_match++); + } + } + else +#endif + while (start_match < end_subject && !WAS_NEWLINE(start_match)) + start_match++; + + /* If we have just passed a CR and the newline option is ANY or ANYCRLF, + and we are now at a LF, advance the match position by one more character. + */ + + if (start_match[-1] == CHAR_CR && + (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF) && + start_match < end_subject && + UCHAR21TEST(start_match) == CHAR_NL) + start_match++; + } + } + + /* Or to a non-unique first byte after study */ + + else if (start_bits != NULL) + { + while (start_match < end_subject) + { + register pcre_uint32 c = UCHAR21TEST(start_match); +#ifndef COMPILE_PCRE8 + if (c > 255) c = 255; +#endif + if ((start_bits[c/8] & (1 << (c&7))) != 0) break; + start_match++; + } + } + } /* Starting optimizations */ + + /* Restore fudged end_subject */ + + end_subject = save_end_subject; + + /* The following two optimizations are disabled for partial matching or if + disabling is explicitly requested. */ + + if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0 && !md->partial) + { + /* If the pattern was studied, a minimum subject length may be set. This is + a lower bound; no actual string of that length may actually match the + pattern. Although the value is, strictly, in characters, we treat it as + bytes to avoid spending too much time in this optimization. */ + + if (study != NULL && (study->flags & PCRE_STUDY_MINLEN) != 0 && + (pcre_uint32)(end_subject - start_match) < study->minlength) + { + rc = MATCH_NOMATCH; + break; + } + + /* If req_char is set, we know that that character must appear in the + subject for the match to succeed. If the first character is set, req_char + must be later in the subject; otherwise the test starts at the match point. + This optimization can save a huge amount of backtracking in patterns with + nested unlimited repeats that aren't going to match. Writing separate code + for cased/caseless versions makes it go faster, as does using an + autoincrement and backing off on a match. + + HOWEVER: when the subject string is very, very long, searching to its end + can take a long time, and give bad performance on quite ordinary patterns. + This showed up when somebody was matching something like /^\d+C/ on a + 32-megabyte string... so we don't do this when the string is sufficiently + long. */ + + if (has_req_char && end_subject - start_match < REQ_BYTE_MAX) + { + register PCRE_PUCHAR p = start_match + (has_first_char? 1:0); + + /* We don't need to repeat the search if we haven't yet reached the + place we found it at last time. */ + + if (p > req_char_ptr) + { + if (req_char != req_char2) + { + while (p < end_subject) + { + register pcre_uint32 pp = UCHAR21INCTEST(p); + if (pp == req_char || pp == req_char2) { p--; break; } + } + } + else + { + while (p < end_subject) + { + if (UCHAR21INCTEST(p) == req_char) { p--; break; } + } + } + + /* If we can't find the required character, break the matching loop, + forcing a match failure. */ + + if (p >= end_subject) + { + rc = MATCH_NOMATCH; + break; + } + + /* If we have found the required character, save the point where we + found it, so that we don't search again next time round the loop if + the start hasn't passed this character yet. */ + + req_char_ptr = p; + } + } + } + +#ifdef PCRE_DEBUG /* Sigh. Some compilers never learn. */ + printf(">>>> Match against: "); + pchars(start_match, end_subject - start_match, TRUE, md); + printf("\n"); +#endif + + /* OK, we can now run the match. If "hitend" is set afterwards, remember the + first starting point for which a partial match was found. */ + + md->start_match_ptr = start_match; + md->start_used_ptr = start_match; + md->match_call_count = 0; + md->match_function_type = 0; + md->end_offset_top = 0; + md->skip_arg_count = 0; + rc = match(start_match, md->start_code, start_match, 2, md, NULL, 0); + if (md->hitend && start_partial == NULL) + { + start_partial = md->start_used_ptr; + match_partial = start_match; + } + + switch(rc) + { + /* If MATCH_SKIP_ARG reaches this level it means that a MARK that matched + the SKIP's arg was not found. In this circumstance, Perl ignores the SKIP + entirely. The only way we can do that is to re-do the match at the same + point, with a flag to force SKIP with an argument to be ignored. Just + treating this case as NOMATCH does not work because it does not check other + alternatives in patterns such as A(*SKIP:A)B|AC when the subject is AC. */ + + case MATCH_SKIP_ARG: + new_start_match = start_match; + md->ignore_skip_arg = md->skip_arg_count; + break; + + /* SKIP passes back the next starting point explicitly, but if it is no + greater than the match we have just done, treat it as NOMATCH. */ + + case MATCH_SKIP: + if (md->start_match_ptr > start_match) + { + new_start_match = md->start_match_ptr; + break; + } + /* Fall through */ + + /* NOMATCH and PRUNE advance by one character. THEN at this level acts + exactly like PRUNE. Unset ignore SKIP-with-argument. */ + + case MATCH_NOMATCH: + case MATCH_PRUNE: + case MATCH_THEN: + md->ignore_skip_arg = 0; + new_start_match = start_match + 1; +#ifdef SUPPORT_UTF + if (utf) + ACROSSCHAR(new_start_match < end_subject, *new_start_match, + new_start_match++); +#endif + break; + + /* COMMIT disables the bumpalong, but otherwise behaves as NOMATCH. */ + + case MATCH_COMMIT: + rc = MATCH_NOMATCH; + goto ENDLOOP; + + /* Any other return is either a match, or some kind of error. */ + + default: + goto ENDLOOP; + } + + /* Control reaches here for the various types of "no match at this point" + result. Reset the code to MATCH_NOMATCH for subsequent checking. */ + + rc = MATCH_NOMATCH; + + /* If PCRE_FIRSTLINE is set, the match must happen before or at the first + newline in the subject (though it may continue over the newline). Therefore, + if we have just failed to match, starting at a newline, do not continue. */ + + if (firstline && IS_NEWLINE(start_match)) break; + + /* Advance to new matching position */ + + start_match = new_start_match; + + /* Break the loop if the pattern is anchored or if we have passed the end of + the subject. */ + + if (anchored || start_match > end_subject) break; + + /* If we have just passed a CR and we are now at a LF, and the pattern does + not contain any explicit matches for \r or \n, and the newline option is CRLF + or ANY or ANYCRLF, advance the match position by one more character. In + normal matching start_match will aways be greater than the first position at + this stage, but a failed *SKIP can cause a return at the same point, which is + why the first test exists. */ + + if (start_match > (PCRE_PUCHAR)subject + start_offset && + start_match[-1] == CHAR_CR && + start_match < end_subject && + *start_match == CHAR_NL && + (re->flags & PCRE_HASCRORLF) == 0 && + (md->nltype == NLTYPE_ANY || + md->nltype == NLTYPE_ANYCRLF || + md->nllen == 2)) + start_match++; + + md->mark = NULL; /* Reset for start of next match attempt */ + } /* End of for(;;) "bumpalong" loop */ + +/* ==========================================================================*/ + +/* We reach here when rc is not MATCH_NOMATCH, or if one of the stopping +conditions is true: + +(1) The pattern is anchored or the match was failed by (*COMMIT); + +(2) We are past the end of the subject; + +(3) PCRE_FIRSTLINE is set and we have failed to match at a newline, because + this option requests that a match occur at or before the first newline in + the subject. + +When we have a match and the offset vector is big enough to deal with any +backreferences, captured substring offsets will already be set up. In the case +where we had to get some local store to hold offsets for backreference +processing, copy those that we can. In this case there need not be overflow if +certain parts of the pattern were not used, even though there are more +capturing parentheses than vector slots. */ + +ENDLOOP: + +if (rc == MATCH_MATCH || rc == MATCH_ACCEPT) + { + if (using_temporary_offsets) + { + if (arg_offset_max >= 4) + { + memcpy(offsets + 2, md->offset_vector + 2, + (arg_offset_max - 2) * sizeof(int)); + DPRINTF(("Copied offsets from temporary memory\n")); + } + if (md->end_offset_top > arg_offset_max) md->capture_last |= OVFLBIT; + DPRINTF(("Freeing temporary memory\n")); + (PUBL(free))(md->offset_vector); + } + + /* Set the return code to the number of captured strings, or 0 if there were + too many to fit into the vector. */ + + rc = ((md->capture_last & OVFLBIT) != 0 && + md->end_offset_top >= arg_offset_max)? + 0 : md->end_offset_top/2; + + /* If there is space in the offset vector, set any unused pairs at the end of + the pattern to -1 for backwards compatibility. It is documented that this + happens. In earlier versions, the whole set of potential capturing offsets + was set to -1 each time round the loop, but this is handled differently now. + "Gaps" are set to -1 dynamically instead (this fixes a bug). Thus, it is only + those at the end that need unsetting here. We can't just unset them all at + the start of the whole thing because they may get set in one branch that is + not the final matching branch. */ + + if (md->end_offset_top/2 <= re->top_bracket && offsets != NULL) + { + register int *iptr, *iend; + int resetcount = 2 + re->top_bracket * 2; + if (resetcount > offsetcount) resetcount = offsetcount; + iptr = offsets + md->end_offset_top; + iend = offsets + resetcount; + while (iptr < iend) *iptr++ = -1; + } + + /* If there is space, set up the whole thing as substring 0. The value of + md->start_match_ptr might be modified if \K was encountered on the success + matching path. */ + + if (offsetcount < 2) rc = 0; else + { + offsets[0] = (int)(md->start_match_ptr - md->start_subject); + offsets[1] = (int)(md->end_match_ptr - md->start_subject); + } + + /* Return MARK data if requested */ + + if (extra_data != NULL && (extra_data->flags & PCRE_EXTRA_MARK) != 0) + *(extra_data->mark) = (pcre_uchar *)md->mark; + DPRINTF((">>>> returning %d\n", rc)); +#ifdef NO_RECURSE + release_match_heapframes(&frame_zero); +#endif + return rc; + } + +/* Control gets here if there has been an error, or if the overall match +attempt has failed at all permitted starting positions. */ + +if (using_temporary_offsets) + { + DPRINTF(("Freeing temporary memory\n")); + (PUBL(free))(md->offset_vector); + } + +/* For anything other than nomatch or partial match, just return the code. */ + +if (rc != MATCH_NOMATCH && rc != PCRE_ERROR_PARTIAL) + { + DPRINTF((">>>> error: returning %d\n", rc)); +#ifdef NO_RECURSE + release_match_heapframes(&frame_zero); +#endif + return rc; + } + +/* Handle partial matches - disable any mark data */ + +if (match_partial != NULL) + { + DPRINTF((">>>> returning PCRE_ERROR_PARTIAL\n")); + md->mark = NULL; + if (offsetcount > 1) + { + offsets[0] = (int)(start_partial - (PCRE_PUCHAR)subject); + offsets[1] = (int)(end_subject - (PCRE_PUCHAR)subject); + if (offsetcount > 2) + offsets[2] = (int)(match_partial - (PCRE_PUCHAR)subject); + } + rc = PCRE_ERROR_PARTIAL; + } + +/* This is the classic nomatch case */ + +else + { + DPRINTF((">>>> returning PCRE_ERROR_NOMATCH\n")); + rc = PCRE_ERROR_NOMATCH; + } + +/* Return the MARK data if it has been requested. */ + +if (extra_data != NULL && (extra_data->flags & PCRE_EXTRA_MARK) != 0) + *(extra_data->mark) = (pcre_uchar *)md->nomatch_mark; +#ifdef NO_RECURSE + release_match_heapframes(&frame_zero); +#endif +return rc; +} + +/* End of pcre_exec.c */ diff --git a/deps/pcre/pcre_fullinfo.c b/deps/pcre/pcre_fullinfo.c new file mode 100644 index 00000000000..a6c2ece6ca5 --- /dev/null +++ b/deps/pcre/pcre_fullinfo.c @@ -0,0 +1,245 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2013 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains the external function pcre_fullinfo(), which returns +information about a compiled pattern. */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pcre_internal.h" + + +/************************************************* +* Return info about compiled pattern * +*************************************************/ + +/* This is a newer "info" function which has an extensible interface so +that additional items can be added compatibly. + +Arguments: + argument_re points to compiled code + extra_data points extra data, or NULL + what what information is required + where where to put the information + +Returns: 0 if data returned, negative on error +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_fullinfo(const pcre *argument_re, const pcre_extra *extra_data, + int what, void *where) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre16_fullinfo(const pcre16 *argument_re, const pcre16_extra *extra_data, + int what, void *where) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre32_fullinfo(const pcre32 *argument_re, const pcre32_extra *extra_data, + int what, void *where) +#endif +{ +const REAL_PCRE *re = (const REAL_PCRE *)argument_re; +const pcre_study_data *study = NULL; + +if (re == NULL || where == NULL) return PCRE_ERROR_NULL; + +if (extra_data != NULL && (extra_data->flags & PCRE_EXTRA_STUDY_DATA) != 0) + study = (const pcre_study_data *)extra_data->study_data; + +/* Check that the first field in the block is the magic number. If it is not, +return with PCRE_ERROR_BADMAGIC. However, if the magic number is equal to +REVERSED_MAGIC_NUMBER we return with PCRE_ERROR_BADENDIANNESS, which +means that the pattern is likely compiled with different endianness. */ + +if (re->magic_number != MAGIC_NUMBER) + return re->magic_number == REVERSED_MAGIC_NUMBER? + PCRE_ERROR_BADENDIANNESS:PCRE_ERROR_BADMAGIC; + +/* Check that this pattern was compiled in the correct bit mode */ + +if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE; + +switch (what) + { + case PCRE_INFO_OPTIONS: + *((unsigned long int *)where) = re->options & PUBLIC_COMPILE_OPTIONS; + break; + + case PCRE_INFO_SIZE: + *((size_t *)where) = re->size; + break; + + case PCRE_INFO_STUDYSIZE: + *((size_t *)where) = (study == NULL)? 0 : study->size; + break; + + case PCRE_INFO_JITSIZE: +#ifdef SUPPORT_JIT + *((size_t *)where) = + (extra_data != NULL && + (extra_data->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 && + extra_data->executable_jit != NULL)? + PRIV(jit_get_size)(extra_data->executable_jit) : 0; +#else + *((size_t *)where) = 0; +#endif + break; + + case PCRE_INFO_CAPTURECOUNT: + *((int *)where) = re->top_bracket; + break; + + case PCRE_INFO_BACKREFMAX: + *((int *)where) = re->top_backref; + break; + + case PCRE_INFO_FIRSTBYTE: + *((int *)where) = + ((re->flags & PCRE_FIRSTSET) != 0)? (int)re->first_char : + ((re->flags & PCRE_STARTLINE) != 0)? -1 : -2; + break; + + case PCRE_INFO_FIRSTCHARACTER: + *((pcre_uint32 *)where) = + (re->flags & PCRE_FIRSTSET) != 0 ? re->first_char : 0; + break; + + case PCRE_INFO_FIRSTCHARACTERFLAGS: + *((int *)where) = + ((re->flags & PCRE_FIRSTSET) != 0) ? 1 : + ((re->flags & PCRE_STARTLINE) != 0) ? 2 : 0; + break; + + /* Make sure we pass back the pointer to the bit vector in the external + block, not the internal copy (with flipped integer fields). */ + + case PCRE_INFO_FIRSTTABLE: + *((const pcre_uint8 **)where) = + (study != NULL && (study->flags & PCRE_STUDY_MAPPED) != 0)? + ((const pcre_study_data *)extra_data->study_data)->start_bits : NULL; + break; + + case PCRE_INFO_MINLENGTH: + *((int *)where) = + (study != NULL && (study->flags & PCRE_STUDY_MINLEN) != 0)? + (int)(study->minlength) : -1; + break; + + case PCRE_INFO_JIT: + *((int *)where) = extra_data != NULL && + (extra_data->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 && + extra_data->executable_jit != NULL; + break; + + case PCRE_INFO_LASTLITERAL: + *((int *)where) = + ((re->flags & PCRE_REQCHSET) != 0)? (int)re->req_char : -1; + break; + + case PCRE_INFO_REQUIREDCHAR: + *((pcre_uint32 *)where) = + ((re->flags & PCRE_REQCHSET) != 0) ? re->req_char : 0; + break; + + case PCRE_INFO_REQUIREDCHARFLAGS: + *((int *)where) = + ((re->flags & PCRE_REQCHSET) != 0); + break; + + case PCRE_INFO_NAMEENTRYSIZE: + *((int *)where) = re->name_entry_size; + break; + + case PCRE_INFO_NAMECOUNT: + *((int *)where) = re->name_count; + break; + + case PCRE_INFO_NAMETABLE: + *((const pcre_uchar **)where) = (const pcre_uchar *)re + re->name_table_offset; + break; + + case PCRE_INFO_DEFAULT_TABLES: + *((const pcre_uint8 **)where) = (const pcre_uint8 *)(PRIV(default_tables)); + break; + + /* From release 8.00 this will always return TRUE because NOPARTIAL is + no longer ever set (the restrictions have been removed). */ + + case PCRE_INFO_OKPARTIAL: + *((int *)where) = (re->flags & PCRE_NOPARTIAL) == 0; + break; + + case PCRE_INFO_JCHANGED: + *((int *)where) = (re->flags & PCRE_JCHANGED) != 0; + break; + + case PCRE_INFO_HASCRORLF: + *((int *)where) = (re->flags & PCRE_HASCRORLF) != 0; + break; + + case PCRE_INFO_MAXLOOKBEHIND: + *((int *)where) = re->max_lookbehind; + break; + + case PCRE_INFO_MATCHLIMIT: + if ((re->flags & PCRE_MLSET) == 0) return PCRE_ERROR_UNSET; + *((pcre_uint32 *)where) = re->limit_match; + break; + + case PCRE_INFO_RECURSIONLIMIT: + if ((re->flags & PCRE_RLSET) == 0) return PCRE_ERROR_UNSET; + *((pcre_uint32 *)where) = re->limit_recursion; + break; + + case PCRE_INFO_MATCH_EMPTY: + *((int *)where) = (re->flags & PCRE_MATCH_EMPTY) != 0; + break; + + default: return PCRE_ERROR_BADOPTION; + } + +return 0; +} + +/* End of pcre_fullinfo.c */ diff --git a/deps/pcre/pcre_get.c b/deps/pcre/pcre_get.c new file mode 100644 index 00000000000..9475d5e88cd --- /dev/null +++ b/deps/pcre/pcre_get.c @@ -0,0 +1,669 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2012 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains some convenience functions for extracting substrings +from the subject string after a regex match has succeeded. The original idea +for these functions came from Scott Wimer. */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pcre_internal.h" + + +/************************************************* +* Find number for named string * +*************************************************/ + +/* This function is used by the get_first_set() function below, as well +as being generally available. It assumes that names are unique. + +Arguments: + code the compiled regex + stringname the name whose number is required + +Returns: the number of the named parentheses, or a negative number + (PCRE_ERROR_NOSUBSTRING) if not found +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_get_stringnumber(const pcre *code, const char *stringname) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre16_get_stringnumber(const pcre16 *code, PCRE_SPTR16 stringname) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre32_get_stringnumber(const pcre32 *code, PCRE_SPTR32 stringname) +#endif +{ +int rc; +int entrysize; +int top, bot; +pcre_uchar *nametable; + +#ifdef COMPILE_PCRE8 +if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0) + return rc; +if (top <= 0) return PCRE_ERROR_NOSUBSTRING; + +if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0) + return rc; +if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0) + return rc; +#endif +#ifdef COMPILE_PCRE16 +if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0) + return rc; +if (top <= 0) return PCRE_ERROR_NOSUBSTRING; + +if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0) + return rc; +if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0) + return rc; +#endif +#ifdef COMPILE_PCRE32 +if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0) + return rc; +if (top <= 0) return PCRE_ERROR_NOSUBSTRING; + +if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0) + return rc; +if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0) + return rc; +#endif + +bot = 0; +while (top > bot) + { + int mid = (top + bot) / 2; + pcre_uchar *entry = nametable + entrysize*mid; + int c = STRCMP_UC_UC((pcre_uchar *)stringname, + (pcre_uchar *)(entry + IMM2_SIZE)); + if (c == 0) return GET2(entry, 0); + if (c > 0) bot = mid + 1; else top = mid; + } + +return PCRE_ERROR_NOSUBSTRING; +} + + + +/************************************************* +* Find (multiple) entries for named string * +*************************************************/ + +/* This is used by the get_first_set() function below, as well as being +generally available. It is used when duplicated names are permitted. + +Arguments: + code the compiled regex + stringname the name whose entries required + firstptr where to put the pointer to the first entry + lastptr where to put the pointer to the last entry + +Returns: the length of each entry, or a negative number + (PCRE_ERROR_NOSUBSTRING) if not found +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_get_stringtable_entries(const pcre *code, const char *stringname, + char **firstptr, char **lastptr) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre16_get_stringtable_entries(const pcre16 *code, PCRE_SPTR16 stringname, + PCRE_UCHAR16 **firstptr, PCRE_UCHAR16 **lastptr) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre32_get_stringtable_entries(const pcre32 *code, PCRE_SPTR32 stringname, + PCRE_UCHAR32 **firstptr, PCRE_UCHAR32 **lastptr) +#endif +{ +int rc; +int entrysize; +int top, bot; +pcre_uchar *nametable, *lastentry; + +#ifdef COMPILE_PCRE8 +if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0) + return rc; +if (top <= 0) return PCRE_ERROR_NOSUBSTRING; + +if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0) + return rc; +if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0) + return rc; +#endif +#ifdef COMPILE_PCRE16 +if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0) + return rc; +if (top <= 0) return PCRE_ERROR_NOSUBSTRING; + +if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0) + return rc; +if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0) + return rc; +#endif +#ifdef COMPILE_PCRE32 +if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0) + return rc; +if (top <= 0) return PCRE_ERROR_NOSUBSTRING; + +if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0) + return rc; +if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0) + return rc; +#endif + +lastentry = nametable + entrysize * (top - 1); +bot = 0; +while (top > bot) + { + int mid = (top + bot) / 2; + pcre_uchar *entry = nametable + entrysize*mid; + int c = STRCMP_UC_UC((pcre_uchar *)stringname, + (pcre_uchar *)(entry + IMM2_SIZE)); + if (c == 0) + { + pcre_uchar *first = entry; + pcre_uchar *last = entry; + while (first > nametable) + { + if (STRCMP_UC_UC((pcre_uchar *)stringname, + (pcre_uchar *)(first - entrysize + IMM2_SIZE)) != 0) break; + first -= entrysize; + } + while (last < lastentry) + { + if (STRCMP_UC_UC((pcre_uchar *)stringname, + (pcre_uchar *)(last + entrysize + IMM2_SIZE)) != 0) break; + last += entrysize; + } +#if defined COMPILE_PCRE8 + *firstptr = (char *)first; + *lastptr = (char *)last; +#elif defined COMPILE_PCRE16 + *firstptr = (PCRE_UCHAR16 *)first; + *lastptr = (PCRE_UCHAR16 *)last; +#elif defined COMPILE_PCRE32 + *firstptr = (PCRE_UCHAR32 *)first; + *lastptr = (PCRE_UCHAR32 *)last; +#endif + return entrysize; + } + if (c > 0) bot = mid + 1; else top = mid; + } + +return PCRE_ERROR_NOSUBSTRING; +} + + + +/************************************************* +* Find first set of multiple named strings * +*************************************************/ + +/* This function allows for duplicate names in the table of named substrings. +It returns the number of the first one that was set in a pattern match. + +Arguments: + code the compiled regex + stringname the name of the capturing substring + ovector the vector of matched substrings + stringcount number of captured substrings + +Returns: the number of the first that is set, + or the number of the last one if none are set, + or a negative number on error +*/ + +#if defined COMPILE_PCRE8 +static int +get_first_set(const pcre *code, const char *stringname, int *ovector, + int stringcount) +#elif defined COMPILE_PCRE16 +static int +get_first_set(const pcre16 *code, PCRE_SPTR16 stringname, int *ovector, + int stringcount) +#elif defined COMPILE_PCRE32 +static int +get_first_set(const pcre32 *code, PCRE_SPTR32 stringname, int *ovector, + int stringcount) +#endif +{ +const REAL_PCRE *re = (const REAL_PCRE *)code; +int entrysize; +pcre_uchar *entry; +#if defined COMPILE_PCRE8 +char *first, *last; +#elif defined COMPILE_PCRE16 +PCRE_UCHAR16 *first, *last; +#elif defined COMPILE_PCRE32 +PCRE_UCHAR32 *first, *last; +#endif + +#if defined COMPILE_PCRE8 +if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0) + return pcre_get_stringnumber(code, stringname); +entrysize = pcre_get_stringtable_entries(code, stringname, &first, &last); +#elif defined COMPILE_PCRE16 +if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0) + return pcre16_get_stringnumber(code, stringname); +entrysize = pcre16_get_stringtable_entries(code, stringname, &first, &last); +#elif defined COMPILE_PCRE32 +if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0) + return pcre32_get_stringnumber(code, stringname); +entrysize = pcre32_get_stringtable_entries(code, stringname, &first, &last); +#endif +if (entrysize <= 0) return entrysize; +for (entry = (pcre_uchar *)first; entry <= (pcre_uchar *)last; entry += entrysize) + { + int n = GET2(entry, 0); + if (n < stringcount && ovector[n*2] >= 0) return n; + } +return GET2(entry, 0); +} + + + + +/************************************************* +* Copy captured string to given buffer * +*************************************************/ + +/* This function copies a single captured substring into a given buffer. +Note that we use memcpy() rather than strncpy() in case there are binary zeros +in the string. + +Arguments: + subject the subject string that was matched + ovector pointer to the offsets table + stringcount the number of substrings that were captured + (i.e. the yield of the pcre_exec call, unless + that was zero, in which case it should be 1/3 + of the offset table size) + stringnumber the number of the required substring + buffer where to put the substring + size the size of the buffer + +Returns: if successful: + the length of the copied string, not including the zero + that is put on the end; can be zero + if not successful: + PCRE_ERROR_NOMEMORY (-6) buffer too small + PCRE_ERROR_NOSUBSTRING (-7) no such captured substring +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_copy_substring(const char *subject, int *ovector, int stringcount, + int stringnumber, char *buffer, int size) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre16_copy_substring(PCRE_SPTR16 subject, int *ovector, int stringcount, + int stringnumber, PCRE_UCHAR16 *buffer, int size) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre32_copy_substring(PCRE_SPTR32 subject, int *ovector, int stringcount, + int stringnumber, PCRE_UCHAR32 *buffer, int size) +#endif +{ +int yield; +if (stringnumber < 0 || stringnumber >= stringcount) + return PCRE_ERROR_NOSUBSTRING; +stringnumber *= 2; +yield = ovector[stringnumber+1] - ovector[stringnumber]; +if (size < yield + 1) return PCRE_ERROR_NOMEMORY; +memcpy(buffer, subject + ovector[stringnumber], IN_UCHARS(yield)); +buffer[yield] = 0; +return yield; +} + + + +/************************************************* +* Copy named captured string to given buffer * +*************************************************/ + +/* This function copies a single captured substring into a given buffer, +identifying it by name. If the regex permits duplicate names, the first +substring that is set is chosen. + +Arguments: + code the compiled regex + subject the subject string that was matched + ovector pointer to the offsets table + stringcount the number of substrings that were captured + (i.e. the yield of the pcre_exec call, unless + that was zero, in which case it should be 1/3 + of the offset table size) + stringname the name of the required substring + buffer where to put the substring + size the size of the buffer + +Returns: if successful: + the length of the copied string, not including the zero + that is put on the end; can be zero + if not successful: + PCRE_ERROR_NOMEMORY (-6) buffer too small + PCRE_ERROR_NOSUBSTRING (-7) no such captured substring +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_copy_named_substring(const pcre *code, const char *subject, + int *ovector, int stringcount, const char *stringname, + char *buffer, int size) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre16_copy_named_substring(const pcre16 *code, PCRE_SPTR16 subject, + int *ovector, int stringcount, PCRE_SPTR16 stringname, + PCRE_UCHAR16 *buffer, int size) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre32_copy_named_substring(const pcre32 *code, PCRE_SPTR32 subject, + int *ovector, int stringcount, PCRE_SPTR32 stringname, + PCRE_UCHAR32 *buffer, int size) +#endif +{ +int n = get_first_set(code, stringname, ovector, stringcount); +if (n <= 0) return n; +#if defined COMPILE_PCRE8 +return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size); +#elif defined COMPILE_PCRE16 +return pcre16_copy_substring(subject, ovector, stringcount, n, buffer, size); +#elif defined COMPILE_PCRE32 +return pcre32_copy_substring(subject, ovector, stringcount, n, buffer, size); +#endif +} + + + +/************************************************* +* Copy all captured strings to new store * +*************************************************/ + +/* This function gets one chunk of store and builds a list of pointers and all +of the captured substrings in it. A NULL pointer is put on the end of the list. + +Arguments: + subject the subject string that was matched + ovector pointer to the offsets table + stringcount the number of substrings that were captured + (i.e. the yield of the pcre_exec call, unless + that was zero, in which case it should be 1/3 + of the offset table size) + listptr set to point to the list of pointers + +Returns: if successful: 0 + if not successful: + PCRE_ERROR_NOMEMORY (-6) failed to get store +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_get_substring_list(const char *subject, int *ovector, int stringcount, + const char ***listptr) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre16_get_substring_list(PCRE_SPTR16 subject, int *ovector, int stringcount, + PCRE_SPTR16 **listptr) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre32_get_substring_list(PCRE_SPTR32 subject, int *ovector, int stringcount, + PCRE_SPTR32 **listptr) +#endif +{ +int i; +int size = sizeof(pcre_uchar *); +int double_count = stringcount * 2; +pcre_uchar **stringlist; +pcre_uchar *p; + +for (i = 0; i < double_count; i += 2) + { + size += sizeof(pcre_uchar *) + IN_UCHARS(1); + if (ovector[i+1] > ovector[i]) size += IN_UCHARS(ovector[i+1] - ovector[i]); + } + +stringlist = (pcre_uchar **)(PUBL(malloc))(size); +if (stringlist == NULL) return PCRE_ERROR_NOMEMORY; + +#if defined COMPILE_PCRE8 +*listptr = (const char **)stringlist; +#elif defined COMPILE_PCRE16 +*listptr = (PCRE_SPTR16 *)stringlist; +#elif defined COMPILE_PCRE32 +*listptr = (PCRE_SPTR32 *)stringlist; +#endif +p = (pcre_uchar *)(stringlist + stringcount + 1); + +for (i = 0; i < double_count; i += 2) + { + int len = (ovector[i+1] > ovector[i])? (ovector[i+1] - ovector[i]) : 0; + memcpy(p, subject + ovector[i], IN_UCHARS(len)); + *stringlist++ = p; + p += len; + *p++ = 0; + } + +*stringlist = NULL; +return 0; +} + + + +/************************************************* +* Free store obtained by get_substring_list * +*************************************************/ + +/* This function exists for the benefit of people calling PCRE from non-C +programs that can call its functions, but not free() or (PUBL(free))() +directly. + +Argument: the result of a previous pcre_get_substring_list() +Returns: nothing +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN void PCRE_CALL_CONVENTION +pcre_free_substring_list(const char **pointer) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN void PCRE_CALL_CONVENTION +pcre16_free_substring_list(PCRE_SPTR16 *pointer) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN void PCRE_CALL_CONVENTION +pcre32_free_substring_list(PCRE_SPTR32 *pointer) +#endif +{ +(PUBL(free))((void *)pointer); +} + + + +/************************************************* +* Copy captured string to new store * +*************************************************/ + +/* This function copies a single captured substring into a piece of new +store + +Arguments: + subject the subject string that was matched + ovector pointer to the offsets table + stringcount the number of substrings that were captured + (i.e. the yield of the pcre_exec call, unless + that was zero, in which case it should be 1/3 + of the offset table size) + stringnumber the number of the required substring + stringptr where to put a pointer to the substring + +Returns: if successful: + the length of the string, not including the zero that + is put on the end; can be zero + if not successful: + PCRE_ERROR_NOMEMORY (-6) failed to get store + PCRE_ERROR_NOSUBSTRING (-7) substring not present +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_get_substring(const char *subject, int *ovector, int stringcount, + int stringnumber, const char **stringptr) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre16_get_substring(PCRE_SPTR16 subject, int *ovector, int stringcount, + int stringnumber, PCRE_SPTR16 *stringptr) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre32_get_substring(PCRE_SPTR32 subject, int *ovector, int stringcount, + int stringnumber, PCRE_SPTR32 *stringptr) +#endif +{ +int yield; +pcre_uchar *substring; +if (stringnumber < 0 || stringnumber >= stringcount) + return PCRE_ERROR_NOSUBSTRING; +stringnumber *= 2; +yield = ovector[stringnumber+1] - ovector[stringnumber]; +substring = (pcre_uchar *)(PUBL(malloc))(IN_UCHARS(yield + 1)); +if (substring == NULL) return PCRE_ERROR_NOMEMORY; +memcpy(substring, subject + ovector[stringnumber], IN_UCHARS(yield)); +substring[yield] = 0; +#if defined COMPILE_PCRE8 +*stringptr = (const char *)substring; +#elif defined COMPILE_PCRE16 +*stringptr = (PCRE_SPTR16)substring; +#elif defined COMPILE_PCRE32 +*stringptr = (PCRE_SPTR32)substring; +#endif +return yield; +} + + + +/************************************************* +* Copy named captured string to new store * +*************************************************/ + +/* This function copies a single captured substring, identified by name, into +new store. If the regex permits duplicate names, the first substring that is +set is chosen. + +Arguments: + code the compiled regex + subject the subject string that was matched + ovector pointer to the offsets table + stringcount the number of substrings that were captured + (i.e. the yield of the pcre_exec call, unless + that was zero, in which case it should be 1/3 + of the offset table size) + stringname the name of the required substring + stringptr where to put the pointer + +Returns: if successful: + the length of the copied string, not including the zero + that is put on the end; can be zero + if not successful: + PCRE_ERROR_NOMEMORY (-6) couldn't get memory + PCRE_ERROR_NOSUBSTRING (-7) no such captured substring +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_get_named_substring(const pcre *code, const char *subject, + int *ovector, int stringcount, const char *stringname, + const char **stringptr) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre16_get_named_substring(const pcre16 *code, PCRE_SPTR16 subject, + int *ovector, int stringcount, PCRE_SPTR16 stringname, + PCRE_SPTR16 *stringptr) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre32_get_named_substring(const pcre32 *code, PCRE_SPTR32 subject, + int *ovector, int stringcount, PCRE_SPTR32 stringname, + PCRE_SPTR32 *stringptr) +#endif +{ +int n = get_first_set(code, stringname, ovector, stringcount); +if (n <= 0) return n; +#if defined COMPILE_PCRE8 +return pcre_get_substring(subject, ovector, stringcount, n, stringptr); +#elif defined COMPILE_PCRE16 +return pcre16_get_substring(subject, ovector, stringcount, n, stringptr); +#elif defined COMPILE_PCRE32 +return pcre32_get_substring(subject, ovector, stringcount, n, stringptr); +#endif +} + + + + +/************************************************* +* Free store obtained by get_substring * +*************************************************/ + +/* This function exists for the benefit of people calling PCRE from non-C +programs that can call its functions, but not free() or (PUBL(free))() +directly. + +Argument: the result of a previous pcre_get_substring() +Returns: nothing +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN void PCRE_CALL_CONVENTION +pcre_free_substring(const char *pointer) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN void PCRE_CALL_CONVENTION +pcre16_free_substring(PCRE_SPTR16 pointer) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN void PCRE_CALL_CONVENTION +pcre32_free_substring(PCRE_SPTR32 pointer) +#endif +{ +(PUBL(free))((void *)pointer); +} + +/* End of pcre_get.c */ diff --git a/deps/pcre/pcre_globals.c b/deps/pcre/pcre_globals.c new file mode 100644 index 00000000000..0f106aa9013 --- /dev/null +++ b/deps/pcre/pcre_globals.c @@ -0,0 +1,86 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2014 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains global variables that are exported by the PCRE library. +PCRE is thread-clean and doesn't use any global variables in the normal sense. +However, it calls memory allocation and freeing functions via the four +indirections below, and it can optionally do callouts, using the fifth +indirection. These values can be changed by the caller, but are shared between +all threads. + +For MS Visual Studio and Symbian OS, there are problems in initializing these +variables to non-local functions. In these cases, therefore, an indirection via +a local function is used. + +Also, when compiling for Virtual Pascal, things are done differently, and +global variables are not used. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pcre_internal.h" + +#if defined _MSC_VER || defined __SYMBIAN32__ +static void* LocalPcreMalloc(size_t aSize) + { + return malloc(aSize); + } +static void LocalPcreFree(void* aPtr) + { + free(aPtr); + } +PCRE_EXP_DATA_DEFN void *(*PUBL(malloc))(size_t) = LocalPcreMalloc; +PCRE_EXP_DATA_DEFN void (*PUBL(free))(void *) = LocalPcreFree; +PCRE_EXP_DATA_DEFN void *(*PUBL(stack_malloc))(size_t) = LocalPcreMalloc; +PCRE_EXP_DATA_DEFN void (*PUBL(stack_free))(void *) = LocalPcreFree; +PCRE_EXP_DATA_DEFN int (*PUBL(callout))(PUBL(callout_block) *) = NULL; +PCRE_EXP_DATA_DEFN int (*PUBL(stack_guard))(void) = NULL; + +#elif !defined VPCOMPAT +PCRE_EXP_DATA_DEFN void *(*PUBL(malloc))(size_t) = malloc; +PCRE_EXP_DATA_DEFN void (*PUBL(free))(void *) = free; +PCRE_EXP_DATA_DEFN void *(*PUBL(stack_malloc))(size_t) = malloc; +PCRE_EXP_DATA_DEFN void (*PUBL(stack_free))(void *) = free; +PCRE_EXP_DATA_DEFN int (*PUBL(callout))(PUBL(callout_block) *) = NULL; +PCRE_EXP_DATA_DEFN int (*PUBL(stack_guard))(void) = NULL; +#endif + +/* End of pcre_globals.c */ diff --git a/deps/pcre/pcre_internal.h b/deps/pcre/pcre_internal.h new file mode 100644 index 00000000000..09b09b8289d --- /dev/null +++ b/deps/pcre/pcre_internal.h @@ -0,0 +1,2787 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2016 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + +/* This header contains definitions that are shared between the different +modules, but which are not relevant to the exported API. This includes some +functions whose names all begin with "_pcre_", "_pcre16_" or "_pcre32_" +depending on the PRIV macro. */ + +#ifndef PCRE_INTERNAL_H +#define PCRE_INTERNAL_H + +/* Define PCRE_DEBUG to get debugging output on stdout. */ + +#if 0 +#define PCRE_DEBUG +#endif + +/* PCRE is compiled as an 8 bit library if it is not requested otherwise. */ + +#if !defined COMPILE_PCRE16 && !defined COMPILE_PCRE32 +#define COMPILE_PCRE8 +#endif + +/* If SUPPORT_UCP is defined, SUPPORT_UTF must also be defined. The +"configure" script ensures this, but not everybody uses "configure". */ + +#if defined SUPPORT_UCP && !(defined SUPPORT_UTF) +#define SUPPORT_UTF 1 +#endif + +/* We define SUPPORT_UTF if SUPPORT_UTF8 is enabled for compatibility +reasons with existing code. */ + +#if defined SUPPORT_UTF8 && !(defined SUPPORT_UTF) +#define SUPPORT_UTF 1 +#endif + +/* Fixme: SUPPORT_UTF8 should be eventually disappear from the code. +Until then we define it if SUPPORT_UTF is defined. */ + +#if defined SUPPORT_UTF && !(defined SUPPORT_UTF8) +#define SUPPORT_UTF8 1 +#endif + +/* We do not support both EBCDIC and UTF-8/16/32 at the same time. The "configure" +script prevents both being selected, but not everybody uses "configure". */ + +#if defined EBCDIC && defined SUPPORT_UTF +#error The use of both EBCDIC and SUPPORT_UTF is not supported. +#endif + +/* Use a macro for debugging printing, 'cause that eliminates the use of #ifdef +inline, and there are *still* stupid compilers about that don't like indented +pre-processor statements, or at least there were when I first wrote this. After +all, it had only been about 10 years then... + +It turns out that the Mac Debugging.h header also defines the macro DPRINTF, so +be absolutely sure we get our version. */ + +#undef DPRINTF +#ifdef PCRE_DEBUG +#define DPRINTF(p) printf p +#else +#define DPRINTF(p) /* Nothing */ +#endif + + +/* Standard C headers plus the external interface definition. The only time +setjmp and stdarg are used is when NO_RECURSE is set. */ + +#include +#include +#include +#include +#include +#include + +/* Valgrind (memcheck) support */ + +#ifdef SUPPORT_VALGRIND +#include +#endif + +/* When compiling a DLL for Windows, the exported symbols have to be declared +using some MS magic. I found some useful information on this web page: +http://msdn2.microsoft.com/en-us/library/y4h7bcy6(VS.80).aspx. According to the +information there, using __declspec(dllexport) without "extern" we have a +definition; with "extern" we have a declaration. The settings here override the +setting in pcre.h (which is included below); it defines only PCRE_EXP_DECL, +which is all that is needed for applications (they just import the symbols). We +use: + + PCRE_EXP_DECL for declarations + PCRE_EXP_DEFN for definitions of exported functions + PCRE_EXP_DATA_DEFN for definitions of exported variables + +The reason for the two DEFN macros is that in non-Windows environments, one +does not want to have "extern" before variable definitions because it leads to +compiler warnings. So we distinguish between functions and variables. In +Windows, the two should always be the same. + +The reason for wrapping this in #ifndef PCRE_EXP_DECL is so that pcretest, +which is an application, but needs to import this file in order to "peek" at +internals, can #include pcre.h first to get an application's-eye view. + +In principle, people compiling for non-Windows, non-Unix-like (i.e. uncommon, +special-purpose environments) might want to stick other stuff in front of +exported symbols. That's why, in the non-Windows case, we set PCRE_EXP_DEFN and +PCRE_EXP_DATA_DEFN only if they are not already set. */ + +#ifndef PCRE_EXP_DECL +# define PCRE_EXP_DECL extern +# define PCRE_EXP_DEFN +# define PCRE_EXP_DATA_DEFN +#endif + +/* When compiling with the MSVC compiler, it is sometimes necessary to include +a "calling convention" before exported function names. (This is secondhand +information; I know nothing about MSVC myself). For example, something like + + void __cdecl function(....) + +might be needed. In order so make this easy, all the exported functions have +PCRE_CALL_CONVENTION just before their names. It is rarely needed; if not +set, we ensure here that it has no effect. */ + +#ifndef PCRE_CALL_CONVENTION +#define PCRE_CALL_CONVENTION +#endif + +/* We need to have types that specify unsigned 8, 16 and 32-bit integers. We +cannot determine these outside the compilation (e.g. by running a program as +part of "configure") because PCRE is often cross-compiled for use on other +systems. Instead we make use of the maximum sizes that are available at +preprocessor time in standard C environments. */ + +typedef unsigned char pcre_uint8; + +#if USHRT_MAX == 65535 +typedef unsigned short pcre_uint16; +typedef short pcre_int16; +#define PCRE_UINT16_MAX USHRT_MAX +#define PCRE_INT16_MAX SHRT_MAX +#elif UINT_MAX == 65535 +typedef unsigned int pcre_uint16; +typedef int pcre_int16; +#define PCRE_UINT16_MAX UINT_MAX +#define PCRE_INT16_MAX INT_MAX +#else +#error Cannot determine a type for 16-bit integers +#endif + +#if UINT_MAX == 4294967295U +typedef unsigned int pcre_uint32; +typedef int pcre_int32; +#define PCRE_UINT32_MAX UINT_MAX +#define PCRE_INT32_MAX INT_MAX +#elif ULONG_MAX == 4294967295UL +typedef unsigned long int pcre_uint32; +typedef long int pcre_int32; +#define PCRE_UINT32_MAX ULONG_MAX +#define PCRE_INT32_MAX LONG_MAX +#else +#error Cannot determine a type for 32-bit integers +#endif + +/* When checking for integer overflow in pcre_compile(), we need to handle +large integers. If a 64-bit integer type is available, we can use that. +Otherwise we have to cast to double, which of course requires floating point +arithmetic. Handle this by defining a macro for the appropriate type. If +stdint.h is available, include it; it may define INT64_MAX. Systems that do not +have stdint.h (e.g. Solaris) may have inttypes.h. The macro int64_t may be set +by "configure". */ + +#if defined HAVE_STDINT_H +#include +#elif defined HAVE_INTTYPES_H +#include +#endif + +#if defined INT64_MAX || defined int64_t +#define INT64_OR_DOUBLE int64_t +#else +#define INT64_OR_DOUBLE double +#endif + +/* All character handling must be done as unsigned characters. Otherwise there +are problems with top-bit-set characters and functions such as isspace(). +However, we leave the interface to the outside world as char * or short *, +because that should make things easier for callers. This character type is +called pcre_uchar. + +The IN_UCHARS macro multiply its argument with the byte size of the current +pcre_uchar type. Useful for memcpy and such operations, whose require the +byte size of their input/output buffers. + +The MAX_255 macro checks whether its pcre_uchar input is less than 256. + +The TABLE_GET macro is designed for accessing elements of tables whose contain +exactly 256 items. When the character is able to contain more than 256 +items, some check is needed before accessing these tables. +*/ + +#if defined COMPILE_PCRE8 + +typedef unsigned char pcre_uchar; +#define IN_UCHARS(x) (x) +#define MAX_255(c) 1 +#define TABLE_GET(c, table, default) ((table)[c]) + +#elif defined COMPILE_PCRE16 + +#if USHRT_MAX != 65535 +/* This is a warning message. Change PCRE_UCHAR16 to a 16 bit data type in +pcre.h(.in) and disable (comment out) this message. */ +#error Warning: PCRE_UCHAR16 is not a 16 bit data type. +#endif + +typedef pcre_uint16 pcre_uchar; +#define UCHAR_SHIFT (1) +#define IN_UCHARS(x) ((x) * 2) +#define MAX_255(c) ((c) <= 255u) +#define TABLE_GET(c, table, default) (MAX_255(c)? ((table)[c]):(default)) + +#elif defined COMPILE_PCRE32 + +typedef pcre_uint32 pcre_uchar; +#define UCHAR_SHIFT (2) +#define IN_UCHARS(x) ((x) * 4) +#define MAX_255(c) ((c) <= 255u) +#define TABLE_GET(c, table, default) (MAX_255(c)? ((table)[c]):(default)) + +#else +#error Unsupported compiling mode +#endif /* COMPILE_PCRE[8|16|32] */ + +/* This is an unsigned int value that no character can ever have. UTF-8 +characters only go up to 0x7fffffff (though Unicode doesn't go beyond +0x0010ffff). */ + +#define NOTACHAR 0xffffffff + +/* PCRE is able to support several different kinds of newline (CR, LF, CRLF, +"any" and "anycrlf" at present). The following macros are used to package up +testing for newlines. NLBLOCK, PSSTART, and PSEND are defined in the various +modules to indicate in which datablock the parameters exist, and what the +start/end of string field names are. */ + +#define NLTYPE_FIXED 0 /* Newline is a fixed length string */ +#define NLTYPE_ANY 1 /* Newline is any Unicode line ending */ +#define NLTYPE_ANYCRLF 2 /* Newline is CR, LF, or CRLF */ + +/* This macro checks for a newline at the given position */ + +#define IS_NEWLINE(p) \ + ((NLBLOCK->nltype != NLTYPE_FIXED)? \ + ((p) < NLBLOCK->PSEND && \ + PRIV(is_newline)((p), NLBLOCK->nltype, NLBLOCK->PSEND, \ + &(NLBLOCK->nllen), utf)) \ + : \ + ((p) <= NLBLOCK->PSEND - NLBLOCK->nllen && \ + UCHAR21TEST(p) == NLBLOCK->nl[0] && \ + (NLBLOCK->nllen == 1 || UCHAR21TEST(p+1) == NLBLOCK->nl[1]) \ + ) \ + ) + +/* This macro checks for a newline immediately preceding the given position */ + +#define WAS_NEWLINE(p) \ + ((NLBLOCK->nltype != NLTYPE_FIXED)? \ + ((p) > NLBLOCK->PSSTART && \ + PRIV(was_newline)((p), NLBLOCK->nltype, NLBLOCK->PSSTART, \ + &(NLBLOCK->nllen), utf)) \ + : \ + ((p) >= NLBLOCK->PSSTART + NLBLOCK->nllen && \ + UCHAR21TEST(p - NLBLOCK->nllen) == NLBLOCK->nl[0] && \ + (NLBLOCK->nllen == 1 || UCHAR21TEST(p - NLBLOCK->nllen + 1) == NLBLOCK->nl[1]) \ + ) \ + ) + +/* When PCRE is compiled as a C++ library, the subject pointer can be replaced +with a custom type. This makes it possible, for example, to allow pcre_exec() +to process subject strings that are discontinuous by using a smart pointer +class. It must always be possible to inspect all of the subject string in +pcre_exec() because of the way it backtracks. Two macros are required in the +normal case, for sign-unspecified and unsigned char pointers. The former is +used for the external interface and appears in pcre.h, which is why its name +must begin with PCRE_. */ + +#ifdef CUSTOM_SUBJECT_PTR +#define PCRE_PUCHAR CUSTOM_SUBJECT_PTR +#else +#define PCRE_PUCHAR const pcre_uchar * +#endif + +/* Include the public PCRE header and the definitions of UCP character property +values. */ + +#include "pcre.h" +#include "ucp.h" + +#ifdef COMPILE_PCRE32 +/* Assert that the public PCRE_UCHAR32 is a 32-bit type */ +typedef int __assert_pcre_uchar32_size[sizeof(PCRE_UCHAR32) == 4 ? 1 : -1]; +#endif + +/* When compiling for use with the Virtual Pascal compiler, these functions +need to have their names changed. PCRE must be compiled with the -DVPCOMPAT +option on the command line. */ + +#ifdef VPCOMPAT +#define strlen(s) _strlen(s) +#define strncmp(s1,s2,m) _strncmp(s1,s2,m) +#define memcmp(s,c,n) _memcmp(s,c,n) +#define memcpy(d,s,n) _memcpy(d,s,n) +#define memmove(d,s,n) _memmove(d,s,n) +#define memset(s,c,n) _memset(s,c,n) +#else /* VPCOMPAT */ + +/* To cope with SunOS4 and other systems that lack memmove() but have bcopy(), +define a macro for memmove() if HAVE_MEMMOVE is false, provided that HAVE_BCOPY +is set. Otherwise, include an emulating function for those systems that have +neither (there some non-Unix environments where this is the case). */ + +#ifndef HAVE_MEMMOVE +#undef memmove /* some systems may have a macro */ +#ifdef HAVE_BCOPY +#define memmove(a, b, c) bcopy(b, a, c) +#else /* HAVE_BCOPY */ +static void * +pcre_memmove(void *d, const void *s, size_t n) +{ +size_t i; +unsigned char *dest = (unsigned char *)d; +const unsigned char *src = (const unsigned char *)s; +if (dest > src) + { + dest += n; + src += n; + for (i = 0; i < n; ++i) *(--dest) = *(--src); + return (void *)dest; + } +else + { + for (i = 0; i < n; ++i) *dest++ = *src++; + return (void *)(dest - n); + } +} +#define memmove(a, b, c) pcre_memmove(a, b, c) +#endif /* not HAVE_BCOPY */ +#endif /* not HAVE_MEMMOVE */ +#endif /* not VPCOMPAT */ + + +/* PCRE keeps offsets in its compiled code as 2-byte quantities (always stored +in big-endian order) by default. These are used, for example, to link from the +start of a subpattern to its alternatives and its end. The use of 2 bytes per +offset limits the size of the compiled regex to around 64K, which is big enough +for almost everybody. However, I received a request for an even bigger limit. +For this reason, and also to make the code easier to maintain, the storing and +loading of offsets from the byte string is now handled by the macros that are +defined here. + +The macros are controlled by the value of LINK_SIZE. This defaults to 2 in +the config.h file, but can be overridden by using -D on the command line. This +is automated on Unix systems via the "configure" command. */ + +#if defined COMPILE_PCRE8 + +#if LINK_SIZE == 2 + +#define PUT(a,n,d) \ + (a[n] = (d) >> 8), \ + (a[(n)+1] = (d) & 255) + +#define GET(a,n) \ + (((a)[n] << 8) | (a)[(n)+1]) + +#define MAX_PATTERN_SIZE (1 << 16) + + +#elif LINK_SIZE == 3 + +#define PUT(a,n,d) \ + (a[n] = (d) >> 16), \ + (a[(n)+1] = (d) >> 8), \ + (a[(n)+2] = (d) & 255) + +#define GET(a,n) \ + (((a)[n] << 16) | ((a)[(n)+1] << 8) | (a)[(n)+2]) + +#define MAX_PATTERN_SIZE (1 << 24) + + +#elif LINK_SIZE == 4 + +#define PUT(a,n,d) \ + (a[n] = (d) >> 24), \ + (a[(n)+1] = (d) >> 16), \ + (a[(n)+2] = (d) >> 8), \ + (a[(n)+3] = (d) & 255) + +#define GET(a,n) \ + (((a)[n] << 24) | ((a)[(n)+1] << 16) | ((a)[(n)+2] << 8) | (a)[(n)+3]) + +/* Keep it positive */ +#define MAX_PATTERN_SIZE (1 << 30) + +#else +#error LINK_SIZE must be either 2, 3, or 4 +#endif + +#elif defined COMPILE_PCRE16 + +#if LINK_SIZE == 2 + +/* Redefine LINK_SIZE as a multiple of sizeof(pcre_uchar) */ +#undef LINK_SIZE +#define LINK_SIZE 1 + +#define PUT(a,n,d) \ + (a[n] = (d)) + +#define GET(a,n) \ + (a[n]) + +#define MAX_PATTERN_SIZE (1 << 16) + +#elif LINK_SIZE == 3 || LINK_SIZE == 4 + +/* Redefine LINK_SIZE as a multiple of sizeof(pcre_uchar) */ +#undef LINK_SIZE +#define LINK_SIZE 2 + +#define PUT(a,n,d) \ + (a[n] = (d) >> 16), \ + (a[(n)+1] = (d) & 65535) + +#define GET(a,n) \ + (((a)[n] << 16) | (a)[(n)+1]) + +/* Keep it positive */ +#define MAX_PATTERN_SIZE (1 << 30) + +#else +#error LINK_SIZE must be either 2, 3, or 4 +#endif + +#elif defined COMPILE_PCRE32 + +/* Only supported LINK_SIZE is 4 */ +/* Redefine LINK_SIZE as a multiple of sizeof(pcre_uchar) */ +#undef LINK_SIZE +#define LINK_SIZE 1 + +#define PUT(a,n,d) \ + (a[n] = (d)) + +#define GET(a,n) \ + (a[n]) + +/* Keep it positive */ +#define MAX_PATTERN_SIZE (1 << 30) + +#else +#error Unsupported compiling mode +#endif /* COMPILE_PCRE[8|16|32] */ + +/* Convenience macro defined in terms of the others */ + +#define PUTINC(a,n,d) PUT(a,n,d), a += LINK_SIZE + + +/* PCRE uses some other 2-byte quantities that do not change when the size of +offsets changes. There are used for repeat counts and for other things such as +capturing parenthesis numbers in back references. */ + +#if defined COMPILE_PCRE8 + +#define IMM2_SIZE 2 + +#define PUT2(a,n,d) \ + a[n] = (d) >> 8; \ + a[(n)+1] = (d) & 255 + +/* For reasons that I do not understand, the expression in this GET2 macro is +treated by gcc as a signed expression, even when a is declared as unsigned. It +seems that any kind of arithmetic results in a signed value. */ + +#define GET2(a,n) \ + (unsigned int)(((a)[n] << 8) | (a)[(n)+1]) + +#elif defined COMPILE_PCRE16 + +#define IMM2_SIZE 1 + +#define PUT2(a,n,d) \ + a[n] = d + +#define GET2(a,n) \ + a[n] + +#elif defined COMPILE_PCRE32 + +#define IMM2_SIZE 1 + +#define PUT2(a,n,d) \ + a[n] = d + +#define GET2(a,n) \ + a[n] + +#else +#error Unsupported compiling mode +#endif /* COMPILE_PCRE[8|16|32] */ + +#define PUT2INC(a,n,d) PUT2(a,n,d), a += IMM2_SIZE + +/* The maximum length of a MARK name is currently one data unit; it may be +changed in future to be a fixed number of bytes or to depend on LINK_SIZE. */ + +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 +#define MAX_MARK ((1u << 16) - 1) +#else +#define MAX_MARK ((1u << 8) - 1) +#endif + +/* There is a proposed future special "UTF-21" mode, in which only the lowest +21 bits of a 32-bit character are interpreted as UTF, with the remaining 11 +high-order bits available to the application for other uses. In preparation for +the future implementation of this mode, there are macros that load a data item +and, if in this special mode, mask it to 21 bits. These macros all have names +starting with UCHAR21. In all other modes, including the normal 32-bit +library, the macros all have the same simple definitions. When the new mode is +implemented, it is expected that these definitions will be varied appropriately +using #ifdef when compiling the library that supports the special mode. */ + +#define UCHAR21(eptr) (*(eptr)) +#define UCHAR21TEST(eptr) (*(eptr)) +#define UCHAR21INC(eptr) (*(eptr)++) +#define UCHAR21INCTEST(eptr) (*(eptr)++) + +/* When UTF encoding is being used, a character is no longer just a single +byte in 8-bit mode or a single short in 16-bit mode. The macros for character +handling generate simple sequences when used in the basic mode, and more +complicated ones for UTF characters. GETCHARLENTEST and other macros are not +used when UTF is not supported. To make sure they can never even appear when +UTF support is omitted, we don't even define them. */ + +#ifndef SUPPORT_UTF + +/* #define MAX_VALUE_FOR_SINGLE_CHAR */ +/* #define HAS_EXTRALEN(c) */ +/* #define GET_EXTRALEN(c) */ +/* #define NOT_FIRSTCHAR(c) */ +#define GETCHAR(c, eptr) c = *eptr; +#define GETCHARTEST(c, eptr) c = *eptr; +#define GETCHARINC(c, eptr) c = *eptr++; +#define GETCHARINCTEST(c, eptr) c = *eptr++; +#define GETCHARLEN(c, eptr, len) c = *eptr; +/* #define GETCHARLENTEST(c, eptr, len) */ +/* #define BACKCHAR(eptr) */ +/* #define FORWARDCHAR(eptr) */ +/* #define ACROSSCHAR(condition, eptr, action) */ + +#else /* SUPPORT_UTF */ + +/* Tests whether the code point needs extra characters to decode. */ + +#define HASUTF8EXTRALEN(c) ((c) >= 0xc0) + +/* Base macro to pick up the remaining bytes of a UTF-8 character, not +advancing the pointer. */ + +#define GETUTF8(c, eptr) \ + { \ + if ((c & 0x20) == 0) \ + c = ((c & 0x1f) << 6) | (eptr[1] & 0x3f); \ + else if ((c & 0x10) == 0) \ + c = ((c & 0x0f) << 12) | ((eptr[1] & 0x3f) << 6) | (eptr[2] & 0x3f); \ + else if ((c & 0x08) == 0) \ + c = ((c & 0x07) << 18) | ((eptr[1] & 0x3f) << 12) | \ + ((eptr[2] & 0x3f) << 6) | (eptr[3] & 0x3f); \ + else if ((c & 0x04) == 0) \ + c = ((c & 0x03) << 24) | ((eptr[1] & 0x3f) << 18) | \ + ((eptr[2] & 0x3f) << 12) | ((eptr[3] & 0x3f) << 6) | \ + (eptr[4] & 0x3f); \ + else \ + c = ((c & 0x01) << 30) | ((eptr[1] & 0x3f) << 24) | \ + ((eptr[2] & 0x3f) << 18) | ((eptr[3] & 0x3f) << 12) | \ + ((eptr[4] & 0x3f) << 6) | (eptr[5] & 0x3f); \ + } + +/* Base macro to pick up the remaining bytes of a UTF-8 character, advancing +the pointer. */ + +#define GETUTF8INC(c, eptr) \ + { \ + if ((c & 0x20) == 0) \ + c = ((c & 0x1f) << 6) | (*eptr++ & 0x3f); \ + else if ((c & 0x10) == 0) \ + { \ + c = ((c & 0x0f) << 12) | ((*eptr & 0x3f) << 6) | (eptr[1] & 0x3f); \ + eptr += 2; \ + } \ + else if ((c & 0x08) == 0) \ + { \ + c = ((c & 0x07) << 18) | ((*eptr & 0x3f) << 12) | \ + ((eptr[1] & 0x3f) << 6) | (eptr[2] & 0x3f); \ + eptr += 3; \ + } \ + else if ((c & 0x04) == 0) \ + { \ + c = ((c & 0x03) << 24) | ((*eptr & 0x3f) << 18) | \ + ((eptr[1] & 0x3f) << 12) | ((eptr[2] & 0x3f) << 6) | \ + (eptr[3] & 0x3f); \ + eptr += 4; \ + } \ + else \ + { \ + c = ((c & 0x01) << 30) | ((*eptr & 0x3f) << 24) | \ + ((eptr[1] & 0x3f) << 18) | ((eptr[2] & 0x3f) << 12) | \ + ((eptr[3] & 0x3f) << 6) | (eptr[4] & 0x3f); \ + eptr += 5; \ + } \ + } + +#if defined COMPILE_PCRE8 + +/* These macros were originally written in the form of loops that used data +from the tables whose names start with PRIV(utf8_table). They were rewritten by +a user so as not to use loops, because in some environments this gives a +significant performance advantage, and it seems never to do any harm. */ + +/* Tells the biggest code point which can be encoded as a single character. */ + +#define MAX_VALUE_FOR_SINGLE_CHAR 127 + +/* Tests whether the code point needs extra characters to decode. */ + +#define HAS_EXTRALEN(c) ((c) >= 0xc0) + +/* Returns with the additional number of characters if IS_MULTICHAR(c) is TRUE. +Otherwise it has an undefined behaviour. */ + +#define GET_EXTRALEN(c) (PRIV(utf8_table4)[(c) & 0x3f]) + +/* Returns TRUE, if the given character is not the first character +of a UTF sequence. */ + +#define NOT_FIRSTCHAR(c) (((c) & 0xc0) == 0x80) + +/* Get the next UTF-8 character, not advancing the pointer. This is called when +we know we are in UTF-8 mode. */ + +#define GETCHAR(c, eptr) \ + c = *eptr; \ + if (c >= 0xc0) GETUTF8(c, eptr); + +/* Get the next UTF-8 character, testing for UTF-8 mode, and not advancing the +pointer. */ + +#define GETCHARTEST(c, eptr) \ + c = *eptr; \ + if (utf && c >= 0xc0) GETUTF8(c, eptr); + +/* Get the next UTF-8 character, advancing the pointer. This is called when we +know we are in UTF-8 mode. */ + +#define GETCHARINC(c, eptr) \ + c = *eptr++; \ + if (c >= 0xc0) GETUTF8INC(c, eptr); + +/* Get the next character, testing for UTF-8 mode, and advancing the pointer. +This is called when we don't know if we are in UTF-8 mode. */ + +#define GETCHARINCTEST(c, eptr) \ + c = *eptr++; \ + if (utf && c >= 0xc0) GETUTF8INC(c, eptr); + +/* Base macro to pick up the remaining bytes of a UTF-8 character, not +advancing the pointer, incrementing the length. */ + +#define GETUTF8LEN(c, eptr, len) \ + { \ + if ((c & 0x20) == 0) \ + { \ + c = ((c & 0x1f) << 6) | (eptr[1] & 0x3f); \ + len++; \ + } \ + else if ((c & 0x10) == 0) \ + { \ + c = ((c & 0x0f) << 12) | ((eptr[1] & 0x3f) << 6) | (eptr[2] & 0x3f); \ + len += 2; \ + } \ + else if ((c & 0x08) == 0) \ + {\ + c = ((c & 0x07) << 18) | ((eptr[1] & 0x3f) << 12) | \ + ((eptr[2] & 0x3f) << 6) | (eptr[3] & 0x3f); \ + len += 3; \ + } \ + else if ((c & 0x04) == 0) \ + { \ + c = ((c & 0x03) << 24) | ((eptr[1] & 0x3f) << 18) | \ + ((eptr[2] & 0x3f) << 12) | ((eptr[3] & 0x3f) << 6) | \ + (eptr[4] & 0x3f); \ + len += 4; \ + } \ + else \ + {\ + c = ((c & 0x01) << 30) | ((eptr[1] & 0x3f) << 24) | \ + ((eptr[2] & 0x3f) << 18) | ((eptr[3] & 0x3f) << 12) | \ + ((eptr[4] & 0x3f) << 6) | (eptr[5] & 0x3f); \ + len += 5; \ + } \ + } + +/* Get the next UTF-8 character, not advancing the pointer, incrementing length +if there are extra bytes. This is called when we know we are in UTF-8 mode. */ + +#define GETCHARLEN(c, eptr, len) \ + c = *eptr; \ + if (c >= 0xc0) GETUTF8LEN(c, eptr, len); + +/* Get the next UTF-8 character, testing for UTF-8 mode, not advancing the +pointer, incrementing length if there are extra bytes. This is called when we +do not know if we are in UTF-8 mode. */ + +#define GETCHARLENTEST(c, eptr, len) \ + c = *eptr; \ + if (utf && c >= 0xc0) GETUTF8LEN(c, eptr, len); + +/* If the pointer is not at the start of a character, move it back until +it is. This is called only in UTF-8 mode - we don't put a test within the macro +because almost all calls are already within a block of UTF-8 only code. */ + +#define BACKCHAR(eptr) while((*eptr & 0xc0) == 0x80) eptr-- + +/* Same as above, just in the other direction. */ +#define FORWARDCHAR(eptr) while((*eptr & 0xc0) == 0x80) eptr++ + +/* Same as above, but it allows a fully customizable form. */ +#define ACROSSCHAR(condition, eptr, action) \ + while((condition) && ((eptr) & 0xc0) == 0x80) action + +#elif defined COMPILE_PCRE16 + +/* Tells the biggest code point which can be encoded as a single character. */ + +#define MAX_VALUE_FOR_SINGLE_CHAR 65535 + +/* Tests whether the code point needs extra characters to decode. */ + +#define HAS_EXTRALEN(c) (((c) & 0xfc00) == 0xd800) + +/* Returns with the additional number of characters if IS_MULTICHAR(c) is TRUE. +Otherwise it has an undefined behaviour. */ + +#define GET_EXTRALEN(c) 1 + +/* Returns TRUE, if the given character is not the first character +of a UTF sequence. */ + +#define NOT_FIRSTCHAR(c) (((c) & 0xfc00) == 0xdc00) + +/* Base macro to pick up the low surrogate of a UTF-16 character, not +advancing the pointer. */ + +#define GETUTF16(c, eptr) \ + { c = (((c & 0x3ff) << 10) | (eptr[1] & 0x3ff)) + 0x10000; } + +/* Get the next UTF-16 character, not advancing the pointer. This is called when +we know we are in UTF-16 mode. */ + +#define GETCHAR(c, eptr) \ + c = *eptr; \ + if ((c & 0xfc00) == 0xd800) GETUTF16(c, eptr); + +/* Get the next UTF-16 character, testing for UTF-16 mode, and not advancing the +pointer. */ + +#define GETCHARTEST(c, eptr) \ + c = *eptr; \ + if (utf && (c & 0xfc00) == 0xd800) GETUTF16(c, eptr); + +/* Base macro to pick up the low surrogate of a UTF-16 character, advancing +the pointer. */ + +#define GETUTF16INC(c, eptr) \ + { c = (((c & 0x3ff) << 10) | (*eptr++ & 0x3ff)) + 0x10000; } + +/* Get the next UTF-16 character, advancing the pointer. This is called when we +know we are in UTF-16 mode. */ + +#define GETCHARINC(c, eptr) \ + c = *eptr++; \ + if ((c & 0xfc00) == 0xd800) GETUTF16INC(c, eptr); + +/* Get the next character, testing for UTF-16 mode, and advancing the pointer. +This is called when we don't know if we are in UTF-16 mode. */ + +#define GETCHARINCTEST(c, eptr) \ + c = *eptr++; \ + if (utf && (c & 0xfc00) == 0xd800) GETUTF16INC(c, eptr); + +/* Base macro to pick up the low surrogate of a UTF-16 character, not +advancing the pointer, incrementing the length. */ + +#define GETUTF16LEN(c, eptr, len) \ + { c = (((c & 0x3ff) << 10) | (eptr[1] & 0x3ff)) + 0x10000; len++; } + +/* Get the next UTF-16 character, not advancing the pointer, incrementing +length if there is a low surrogate. This is called when we know we are in +UTF-16 mode. */ + +#define GETCHARLEN(c, eptr, len) \ + c = *eptr; \ + if ((c & 0xfc00) == 0xd800) GETUTF16LEN(c, eptr, len); + +/* Get the next UTF-816character, testing for UTF-16 mode, not advancing the +pointer, incrementing length if there is a low surrogate. This is called when +we do not know if we are in UTF-16 mode. */ + +#define GETCHARLENTEST(c, eptr, len) \ + c = *eptr; \ + if (utf && (c & 0xfc00) == 0xd800) GETUTF16LEN(c, eptr, len); + +/* If the pointer is not at the start of a character, move it back until +it is. This is called only in UTF-16 mode - we don't put a test within the +macro because almost all calls are already within a block of UTF-16 only +code. */ + +#define BACKCHAR(eptr) if ((*eptr & 0xfc00) == 0xdc00) eptr-- + +/* Same as above, just in the other direction. */ +#define FORWARDCHAR(eptr) if ((*eptr & 0xfc00) == 0xdc00) eptr++ + +/* Same as above, but it allows a fully customizable form. */ +#define ACROSSCHAR(condition, eptr, action) \ + if ((condition) && ((eptr) & 0xfc00) == 0xdc00) action + +#elif defined COMPILE_PCRE32 + +/* These are trivial for the 32-bit library, since all UTF-32 characters fit +into one pcre_uchar unit. */ +#define MAX_VALUE_FOR_SINGLE_CHAR (0x10ffffu) +#define HAS_EXTRALEN(c) (0) +#define GET_EXTRALEN(c) (0) +#define NOT_FIRSTCHAR(c) (0) + +/* Get the next UTF-32 character, not advancing the pointer. This is called when +we know we are in UTF-32 mode. */ + +#define GETCHAR(c, eptr) \ + c = *(eptr); + +/* Get the next UTF-32 character, testing for UTF-32 mode, and not advancing the +pointer. */ + +#define GETCHARTEST(c, eptr) \ + c = *(eptr); + +/* Get the next UTF-32 character, advancing the pointer. This is called when we +know we are in UTF-32 mode. */ + +#define GETCHARINC(c, eptr) \ + c = *((eptr)++); + +/* Get the next character, testing for UTF-32 mode, and advancing the pointer. +This is called when we don't know if we are in UTF-32 mode. */ + +#define GETCHARINCTEST(c, eptr) \ + c = *((eptr)++); + +/* Get the next UTF-32 character, not advancing the pointer, not incrementing +length (since all UTF-32 is of length 1). This is called when we know we are in +UTF-32 mode. */ + +#define GETCHARLEN(c, eptr, len) \ + GETCHAR(c, eptr) + +/* Get the next UTF-32character, testing for UTF-32 mode, not advancing the +pointer, not incrementing the length (since all UTF-32 is of length 1). +This is called when we do not know if we are in UTF-32 mode. */ + +#define GETCHARLENTEST(c, eptr, len) \ + GETCHARTEST(c, eptr) + +/* If the pointer is not at the start of a character, move it back until +it is. This is called only in UTF-32 mode - we don't put a test within the +macro because almost all calls are already within a block of UTF-32 only +code. +These are all no-ops since all UTF-32 characters fit into one pcre_uchar. */ + +#define BACKCHAR(eptr) do { } while (0) + +/* Same as above, just in the other direction. */ +#define FORWARDCHAR(eptr) do { } while (0) + +/* Same as above, but it allows a fully customizable form. */ +#define ACROSSCHAR(condition, eptr, action) do { } while (0) + +#else +#error Unsupported compiling mode +#endif /* COMPILE_PCRE[8|16|32] */ + +#endif /* SUPPORT_UTF */ + +/* Tests for Unicode horizontal and vertical whitespace characters must check a +number of different values. Using a switch statement for this generates the +fastest code (no loop, no memory access), and there are several places in the +interpreter code where this happens. In order to ensure that all the case lists +remain in step, we use macros so that there is only one place where the lists +are defined. + +These values are also required as lists in pcre_compile.c when processing \h, +\H, \v and \V in a character class. The lists are defined in pcre_tables.c, but +macros that define the values are here so that all the definitions are +together. The lists must be in ascending character order, terminated by +NOTACHAR (which is 0xffffffff). + +Any changes should ensure that the various macros are kept in step with each +other. NOTE: The values also appear in pcre_jit_compile.c. */ + +/* ------ ASCII/Unicode environments ------ */ + +#ifndef EBCDIC + +#define HSPACE_LIST \ + CHAR_HT, CHAR_SPACE, CHAR_NBSP, \ + 0x1680, 0x180e, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, \ + 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x202f, 0x205f, 0x3000, \ + NOTACHAR + +#define HSPACE_MULTIBYTE_CASES \ + case 0x1680: /* OGHAM SPACE MARK */ \ + case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */ \ + case 0x2000: /* EN QUAD */ \ + case 0x2001: /* EM QUAD */ \ + case 0x2002: /* EN SPACE */ \ + case 0x2003: /* EM SPACE */ \ + case 0x2004: /* THREE-PER-EM SPACE */ \ + case 0x2005: /* FOUR-PER-EM SPACE */ \ + case 0x2006: /* SIX-PER-EM SPACE */ \ + case 0x2007: /* FIGURE SPACE */ \ + case 0x2008: /* PUNCTUATION SPACE */ \ + case 0x2009: /* THIN SPACE */ \ + case 0x200A: /* HAIR SPACE */ \ + case 0x202f: /* NARROW NO-BREAK SPACE */ \ + case 0x205f: /* MEDIUM MATHEMATICAL SPACE */ \ + case 0x3000 /* IDEOGRAPHIC SPACE */ + +#define HSPACE_BYTE_CASES \ + case CHAR_HT: \ + case CHAR_SPACE: \ + case CHAR_NBSP + +#define HSPACE_CASES \ + HSPACE_BYTE_CASES: \ + HSPACE_MULTIBYTE_CASES + +#define VSPACE_LIST \ + CHAR_LF, CHAR_VT, CHAR_FF, CHAR_CR, CHAR_NEL, 0x2028, 0x2029, NOTACHAR + +#define VSPACE_MULTIBYTE_CASES \ + case 0x2028: /* LINE SEPARATOR */ \ + case 0x2029 /* PARAGRAPH SEPARATOR */ + +#define VSPACE_BYTE_CASES \ + case CHAR_LF: \ + case CHAR_VT: \ + case CHAR_FF: \ + case CHAR_CR: \ + case CHAR_NEL + +#define VSPACE_CASES \ + VSPACE_BYTE_CASES: \ + VSPACE_MULTIBYTE_CASES + +/* ------ EBCDIC environments ------ */ + +#else +#define HSPACE_LIST CHAR_HT, CHAR_SPACE, CHAR_NBSP, NOTACHAR + +#define HSPACE_BYTE_CASES \ + case CHAR_HT: \ + case CHAR_SPACE: \ + case CHAR_NBSP + +#define HSPACE_CASES HSPACE_BYTE_CASES + +#ifdef EBCDIC_NL25 +#define VSPACE_LIST \ + CHAR_VT, CHAR_FF, CHAR_CR, CHAR_NEL, CHAR_LF, NOTACHAR +#else +#define VSPACE_LIST \ + CHAR_VT, CHAR_FF, CHAR_CR, CHAR_LF, CHAR_NEL, NOTACHAR +#endif + +#define VSPACE_BYTE_CASES \ + case CHAR_LF: \ + case CHAR_VT: \ + case CHAR_FF: \ + case CHAR_CR: \ + case CHAR_NEL + +#define VSPACE_CASES VSPACE_BYTE_CASES +#endif /* EBCDIC */ + +/* ------ End of whitespace macros ------ */ + + + +/* Private flags containing information about the compiled regex. They used to +live at the top end of the options word, but that got almost full, so they were +moved to a 16-bit flags word - which got almost full, so now they are in a +32-bit flags word. From release 8.00, PCRE_NOPARTIAL is unused, as the +restrictions on partial matching have been lifted. It remains for backwards +compatibility. */ + +#define PCRE_MODE8 0x00000001 /* compiled in 8 bit mode */ +#define PCRE_MODE16 0x00000002 /* compiled in 16 bit mode */ +#define PCRE_MODE32 0x00000004 /* compiled in 32 bit mode */ +#define PCRE_FIRSTSET 0x00000010 /* first_char is set */ +#define PCRE_FCH_CASELESS 0x00000020 /* caseless first char */ +#define PCRE_REQCHSET 0x00000040 /* req_byte is set */ +#define PCRE_RCH_CASELESS 0x00000080 /* caseless requested char */ +#define PCRE_STARTLINE 0x00000100 /* start after \n for multiline */ +#define PCRE_NOPARTIAL 0x00000200 /* can't use partial with this regex */ +#define PCRE_JCHANGED 0x00000400 /* j option used in regex */ +#define PCRE_HASCRORLF 0x00000800 /* explicit \r or \n in pattern */ +#define PCRE_HASTHEN 0x00001000 /* pattern contains (*THEN) */ +#define PCRE_MLSET 0x00002000 /* match limit set by regex */ +#define PCRE_RLSET 0x00004000 /* recursion limit set by regex */ +#define PCRE_MATCH_EMPTY 0x00008000 /* pattern can match empty string */ + +#if defined COMPILE_PCRE8 +#define PCRE_MODE PCRE_MODE8 +#elif defined COMPILE_PCRE16 +#define PCRE_MODE PCRE_MODE16 +#elif defined COMPILE_PCRE32 +#define PCRE_MODE PCRE_MODE32 +#endif +#define PCRE_MODE_MASK (PCRE_MODE8 | PCRE_MODE16 | PCRE_MODE32) + +/* Flags for the "extra" block produced by pcre_study(). */ + +#define PCRE_STUDY_MAPPED 0x0001 /* a map of starting chars exists */ +#define PCRE_STUDY_MINLEN 0x0002 /* a minimum length field exists */ + +/* Masks for identifying the public options that are permitted at compile +time, run time, or study time, respectively. */ + +#define PCRE_NEWLINE_BITS (PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|PCRE_NEWLINE_ANY| \ + PCRE_NEWLINE_ANYCRLF) + +#define PUBLIC_COMPILE_OPTIONS \ + (PCRE_CASELESS|PCRE_EXTENDED|PCRE_ANCHORED|PCRE_MULTILINE| \ + PCRE_DOTALL|PCRE_DOLLAR_ENDONLY|PCRE_EXTRA|PCRE_UNGREEDY|PCRE_UTF8| \ + PCRE_NO_AUTO_CAPTURE|PCRE_NO_AUTO_POSSESS| \ + PCRE_NO_UTF8_CHECK|PCRE_AUTO_CALLOUT|PCRE_FIRSTLINE| \ + PCRE_DUPNAMES|PCRE_NEWLINE_BITS|PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE| \ + PCRE_JAVASCRIPT_COMPAT|PCRE_UCP|PCRE_NO_START_OPTIMIZE|PCRE_NEVER_UTF) + +#define PUBLIC_EXEC_OPTIONS \ + (PCRE_ANCHORED|PCRE_NOTBOL|PCRE_NOTEOL|PCRE_NOTEMPTY|PCRE_NOTEMPTY_ATSTART| \ + PCRE_NO_UTF8_CHECK|PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT|PCRE_NEWLINE_BITS| \ + PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE|PCRE_NO_START_OPTIMIZE) + +#define PUBLIC_DFA_EXEC_OPTIONS \ + (PCRE_ANCHORED|PCRE_NOTBOL|PCRE_NOTEOL|PCRE_NOTEMPTY|PCRE_NOTEMPTY_ATSTART| \ + PCRE_NO_UTF8_CHECK|PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT|PCRE_DFA_SHORTEST| \ + PCRE_DFA_RESTART|PCRE_NEWLINE_BITS|PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE| \ + PCRE_NO_START_OPTIMIZE) + +#define PUBLIC_STUDY_OPTIONS \ + (PCRE_STUDY_JIT_COMPILE|PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE| \ + PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE|PCRE_STUDY_EXTRA_NEEDED) + +#define PUBLIC_JIT_EXEC_OPTIONS \ + (PCRE_NO_UTF8_CHECK|PCRE_NOTBOL|PCRE_NOTEOL|PCRE_NOTEMPTY|\ + PCRE_NOTEMPTY_ATSTART|PCRE_PARTIAL_SOFT|PCRE_PARTIAL_HARD) + +/* Magic number to provide a small check against being handed junk. */ + +#define MAGIC_NUMBER 0x50435245UL /* 'PCRE' */ + +/* This variable is used to detect a loaded regular expression +in different endianness. */ + +#define REVERSED_MAGIC_NUMBER 0x45524350UL /* 'ERCP' */ + +/* The maximum remaining length of subject we are prepared to search for a +req_byte match. */ + +#define REQ_BYTE_MAX 1000 + +/* Miscellaneous definitions. The #ifndef is to pacify compiler warnings in +environments where these macros are defined elsewhere. Unfortunately, there +is no way to do the same for the typedef. */ + +typedef int BOOL; + +#ifndef FALSE +#define FALSE 0 +#define TRUE 1 +#endif + +/* If PCRE is to support UTF-8 on EBCDIC platforms, we cannot use normal +character constants like '*' because the compiler would emit their EBCDIC code, +which is different from their ASCII/UTF-8 code. Instead we define macros for +the characters so that they always use the ASCII/UTF-8 code when UTF-8 support +is enabled. When UTF-8 support is not enabled, the definitions use character +literals. Both character and string versions of each character are needed, and +there are some longer strings as well. + +This means that, on EBCDIC platforms, the PCRE library can handle either +EBCDIC, or UTF-8, but not both. To support both in the same compiled library +would need different lookups depending on whether PCRE_UTF8 was set or not. +This would make it impossible to use characters in switch/case statements, +which would reduce performance. For a theoretical use (which nobody has asked +for) in a minority area (EBCDIC platforms), this is not sensible. Any +application that did need both could compile two versions of the library, using +macros to give the functions distinct names. */ + +#ifndef SUPPORT_UTF + +/* UTF-8 support is not enabled; use the platform-dependent character literals +so that PCRE works in both ASCII and EBCDIC environments, but only in non-UTF +mode. Newline characters are problematic in EBCDIC. Though it has CR and LF +characters, a common practice has been to use its NL (0x15) character as the +line terminator in C-like processing environments. However, sometimes the LF +(0x25) character is used instead, according to this Unicode document: + +http://unicode.org/standard/reports/tr13/tr13-5.html + +PCRE defaults EBCDIC NL to 0x15, but has a build-time option to select 0x25 +instead. Whichever is *not* chosen is defined as NEL. + +In both ASCII and EBCDIC environments, CHAR_NL and CHAR_LF are synonyms for the +same code point. */ + +#ifdef EBCDIC + +#ifndef EBCDIC_NL25 +#define CHAR_NL '\x15' +#define CHAR_NEL '\x25' +#define STR_NL "\x15" +#define STR_NEL "\x25" +#else +#define CHAR_NL '\x25' +#define CHAR_NEL '\x15' +#define STR_NL "\x25" +#define STR_NEL "\x15" +#endif + +#define CHAR_LF CHAR_NL +#define STR_LF STR_NL + +#define CHAR_ESC '\047' +#define CHAR_DEL '\007' +#define CHAR_NBSP '\x41' +#define STR_ESC "\047" +#define STR_DEL "\007" + +#else /* Not EBCDIC */ + +/* In ASCII/Unicode, linefeed is '\n' and we equate this to NL for +compatibility. NEL is the Unicode newline character; make sure it is +a positive value. */ + +#define CHAR_LF '\n' +#define CHAR_NL CHAR_LF +#define CHAR_NEL ((unsigned char)'\x85') +#define CHAR_ESC '\033' +#define CHAR_DEL '\177' +#define CHAR_NBSP ((unsigned char)'\xa0') + +#define STR_LF "\n" +#define STR_NL STR_LF +#define STR_NEL "\x85" +#define STR_ESC "\033" +#define STR_DEL "\177" + +#endif /* EBCDIC */ + +/* The remaining definitions work in both environments. */ + +#define CHAR_NULL '\0' +#define CHAR_HT '\t' +#define CHAR_VT '\v' +#define CHAR_FF '\f' +#define CHAR_CR '\r' +#define CHAR_BS '\b' +#define CHAR_BEL '\a' + +#define CHAR_SPACE ' ' +#define CHAR_EXCLAMATION_MARK '!' +#define CHAR_QUOTATION_MARK '"' +#define CHAR_NUMBER_SIGN '#' +#define CHAR_DOLLAR_SIGN '$' +#define CHAR_PERCENT_SIGN '%' +#define CHAR_AMPERSAND '&' +#define CHAR_APOSTROPHE '\'' +#define CHAR_LEFT_PARENTHESIS '(' +#define CHAR_RIGHT_PARENTHESIS ')' +#define CHAR_ASTERISK '*' +#define CHAR_PLUS '+' +#define CHAR_COMMA ',' +#define CHAR_MINUS '-' +#define CHAR_DOT '.' +#define CHAR_SLASH '/' +#define CHAR_0 '0' +#define CHAR_1 '1' +#define CHAR_2 '2' +#define CHAR_3 '3' +#define CHAR_4 '4' +#define CHAR_5 '5' +#define CHAR_6 '6' +#define CHAR_7 '7' +#define CHAR_8 '8' +#define CHAR_9 '9' +#define CHAR_COLON ':' +#define CHAR_SEMICOLON ';' +#define CHAR_LESS_THAN_SIGN '<' +#define CHAR_EQUALS_SIGN '=' +#define CHAR_GREATER_THAN_SIGN '>' +#define CHAR_QUESTION_MARK '?' +#define CHAR_COMMERCIAL_AT '@' +#define CHAR_A 'A' +#define CHAR_B 'B' +#define CHAR_C 'C' +#define CHAR_D 'D' +#define CHAR_E 'E' +#define CHAR_F 'F' +#define CHAR_G 'G' +#define CHAR_H 'H' +#define CHAR_I 'I' +#define CHAR_J 'J' +#define CHAR_K 'K' +#define CHAR_L 'L' +#define CHAR_M 'M' +#define CHAR_N 'N' +#define CHAR_O 'O' +#define CHAR_P 'P' +#define CHAR_Q 'Q' +#define CHAR_R 'R' +#define CHAR_S 'S' +#define CHAR_T 'T' +#define CHAR_U 'U' +#define CHAR_V 'V' +#define CHAR_W 'W' +#define CHAR_X 'X' +#define CHAR_Y 'Y' +#define CHAR_Z 'Z' +#define CHAR_LEFT_SQUARE_BRACKET '[' +#define CHAR_BACKSLASH '\\' +#define CHAR_RIGHT_SQUARE_BRACKET ']' +#define CHAR_CIRCUMFLEX_ACCENT '^' +#define CHAR_UNDERSCORE '_' +#define CHAR_GRAVE_ACCENT '`' +#define CHAR_a 'a' +#define CHAR_b 'b' +#define CHAR_c 'c' +#define CHAR_d 'd' +#define CHAR_e 'e' +#define CHAR_f 'f' +#define CHAR_g 'g' +#define CHAR_h 'h' +#define CHAR_i 'i' +#define CHAR_j 'j' +#define CHAR_k 'k' +#define CHAR_l 'l' +#define CHAR_m 'm' +#define CHAR_n 'n' +#define CHAR_o 'o' +#define CHAR_p 'p' +#define CHAR_q 'q' +#define CHAR_r 'r' +#define CHAR_s 's' +#define CHAR_t 't' +#define CHAR_u 'u' +#define CHAR_v 'v' +#define CHAR_w 'w' +#define CHAR_x 'x' +#define CHAR_y 'y' +#define CHAR_z 'z' +#define CHAR_LEFT_CURLY_BRACKET '{' +#define CHAR_VERTICAL_LINE '|' +#define CHAR_RIGHT_CURLY_BRACKET '}' +#define CHAR_TILDE '~' + +#define STR_HT "\t" +#define STR_VT "\v" +#define STR_FF "\f" +#define STR_CR "\r" +#define STR_BS "\b" +#define STR_BEL "\a" + +#define STR_SPACE " " +#define STR_EXCLAMATION_MARK "!" +#define STR_QUOTATION_MARK "\"" +#define STR_NUMBER_SIGN "#" +#define STR_DOLLAR_SIGN "$" +#define STR_PERCENT_SIGN "%" +#define STR_AMPERSAND "&" +#define STR_APOSTROPHE "'" +#define STR_LEFT_PARENTHESIS "(" +#define STR_RIGHT_PARENTHESIS ")" +#define STR_ASTERISK "*" +#define STR_PLUS "+" +#define STR_COMMA "," +#define STR_MINUS "-" +#define STR_DOT "." +#define STR_SLASH "/" +#define STR_0 "0" +#define STR_1 "1" +#define STR_2 "2" +#define STR_3 "3" +#define STR_4 "4" +#define STR_5 "5" +#define STR_6 "6" +#define STR_7 "7" +#define STR_8 "8" +#define STR_9 "9" +#define STR_COLON ":" +#define STR_SEMICOLON ";" +#define STR_LESS_THAN_SIGN "<" +#define STR_EQUALS_SIGN "=" +#define STR_GREATER_THAN_SIGN ">" +#define STR_QUESTION_MARK "?" +#define STR_COMMERCIAL_AT "@" +#define STR_A "A" +#define STR_B "B" +#define STR_C "C" +#define STR_D "D" +#define STR_E "E" +#define STR_F "F" +#define STR_G "G" +#define STR_H "H" +#define STR_I "I" +#define STR_J "J" +#define STR_K "K" +#define STR_L "L" +#define STR_M "M" +#define STR_N "N" +#define STR_O "O" +#define STR_P "P" +#define STR_Q "Q" +#define STR_R "R" +#define STR_S "S" +#define STR_T "T" +#define STR_U "U" +#define STR_V "V" +#define STR_W "W" +#define STR_X "X" +#define STR_Y "Y" +#define STR_Z "Z" +#define STR_LEFT_SQUARE_BRACKET "[" +#define STR_BACKSLASH "\\" +#define STR_RIGHT_SQUARE_BRACKET "]" +#define STR_CIRCUMFLEX_ACCENT "^" +#define STR_UNDERSCORE "_" +#define STR_GRAVE_ACCENT "`" +#define STR_a "a" +#define STR_b "b" +#define STR_c "c" +#define STR_d "d" +#define STR_e "e" +#define STR_f "f" +#define STR_g "g" +#define STR_h "h" +#define STR_i "i" +#define STR_j "j" +#define STR_k "k" +#define STR_l "l" +#define STR_m "m" +#define STR_n "n" +#define STR_o "o" +#define STR_p "p" +#define STR_q "q" +#define STR_r "r" +#define STR_s "s" +#define STR_t "t" +#define STR_u "u" +#define STR_v "v" +#define STR_w "w" +#define STR_x "x" +#define STR_y "y" +#define STR_z "z" +#define STR_LEFT_CURLY_BRACKET "{" +#define STR_VERTICAL_LINE "|" +#define STR_RIGHT_CURLY_BRACKET "}" +#define STR_TILDE "~" + +#define STRING_ACCEPT0 "ACCEPT\0" +#define STRING_COMMIT0 "COMMIT\0" +#define STRING_F0 "F\0" +#define STRING_FAIL0 "FAIL\0" +#define STRING_MARK0 "MARK\0" +#define STRING_PRUNE0 "PRUNE\0" +#define STRING_SKIP0 "SKIP\0" +#define STRING_THEN "THEN" + +#define STRING_alpha0 "alpha\0" +#define STRING_lower0 "lower\0" +#define STRING_upper0 "upper\0" +#define STRING_alnum0 "alnum\0" +#define STRING_ascii0 "ascii\0" +#define STRING_blank0 "blank\0" +#define STRING_cntrl0 "cntrl\0" +#define STRING_digit0 "digit\0" +#define STRING_graph0 "graph\0" +#define STRING_print0 "print\0" +#define STRING_punct0 "punct\0" +#define STRING_space0 "space\0" +#define STRING_word0 "word\0" +#define STRING_xdigit "xdigit" + +#define STRING_DEFINE "DEFINE" +#define STRING_WEIRD_STARTWORD "[:<:]]" +#define STRING_WEIRD_ENDWORD "[:>:]]" + +#define STRING_CR_RIGHTPAR "CR)" +#define STRING_LF_RIGHTPAR "LF)" +#define STRING_CRLF_RIGHTPAR "CRLF)" +#define STRING_ANY_RIGHTPAR "ANY)" +#define STRING_ANYCRLF_RIGHTPAR "ANYCRLF)" +#define STRING_BSR_ANYCRLF_RIGHTPAR "BSR_ANYCRLF)" +#define STRING_BSR_UNICODE_RIGHTPAR "BSR_UNICODE)" +#define STRING_UTF8_RIGHTPAR "UTF8)" +#define STRING_UTF16_RIGHTPAR "UTF16)" +#define STRING_UTF32_RIGHTPAR "UTF32)" +#define STRING_UTF_RIGHTPAR "UTF)" +#define STRING_UCP_RIGHTPAR "UCP)" +#define STRING_NO_AUTO_POSSESS_RIGHTPAR "NO_AUTO_POSSESS)" +#define STRING_NO_START_OPT_RIGHTPAR "NO_START_OPT)" +#define STRING_LIMIT_MATCH_EQ "LIMIT_MATCH=" +#define STRING_LIMIT_RECURSION_EQ "LIMIT_RECURSION=" + +#else /* SUPPORT_UTF */ + +/* UTF-8 support is enabled; always use UTF-8 (=ASCII) character codes. This +works in both modes non-EBCDIC platforms, and on EBCDIC platforms in UTF-8 mode +only. */ + +#define CHAR_HT '\011' +#define CHAR_VT '\013' +#define CHAR_FF '\014' +#define CHAR_CR '\015' +#define CHAR_LF '\012' +#define CHAR_NL CHAR_LF +#define CHAR_NEL ((unsigned char)'\x85') +#define CHAR_BS '\010' +#define CHAR_BEL '\007' +#define CHAR_ESC '\033' +#define CHAR_DEL '\177' + +#define CHAR_NULL '\0' +#define CHAR_SPACE '\040' +#define CHAR_EXCLAMATION_MARK '\041' +#define CHAR_QUOTATION_MARK '\042' +#define CHAR_NUMBER_SIGN '\043' +#define CHAR_DOLLAR_SIGN '\044' +#define CHAR_PERCENT_SIGN '\045' +#define CHAR_AMPERSAND '\046' +#define CHAR_APOSTROPHE '\047' +#define CHAR_LEFT_PARENTHESIS '\050' +#define CHAR_RIGHT_PARENTHESIS '\051' +#define CHAR_ASTERISK '\052' +#define CHAR_PLUS '\053' +#define CHAR_COMMA '\054' +#define CHAR_MINUS '\055' +#define CHAR_DOT '\056' +#define CHAR_SLASH '\057' +#define CHAR_0 '\060' +#define CHAR_1 '\061' +#define CHAR_2 '\062' +#define CHAR_3 '\063' +#define CHAR_4 '\064' +#define CHAR_5 '\065' +#define CHAR_6 '\066' +#define CHAR_7 '\067' +#define CHAR_8 '\070' +#define CHAR_9 '\071' +#define CHAR_COLON '\072' +#define CHAR_SEMICOLON '\073' +#define CHAR_LESS_THAN_SIGN '\074' +#define CHAR_EQUALS_SIGN '\075' +#define CHAR_GREATER_THAN_SIGN '\076' +#define CHAR_QUESTION_MARK '\077' +#define CHAR_COMMERCIAL_AT '\100' +#define CHAR_A '\101' +#define CHAR_B '\102' +#define CHAR_C '\103' +#define CHAR_D '\104' +#define CHAR_E '\105' +#define CHAR_F '\106' +#define CHAR_G '\107' +#define CHAR_H '\110' +#define CHAR_I '\111' +#define CHAR_J '\112' +#define CHAR_K '\113' +#define CHAR_L '\114' +#define CHAR_M '\115' +#define CHAR_N '\116' +#define CHAR_O '\117' +#define CHAR_P '\120' +#define CHAR_Q '\121' +#define CHAR_R '\122' +#define CHAR_S '\123' +#define CHAR_T '\124' +#define CHAR_U '\125' +#define CHAR_V '\126' +#define CHAR_W '\127' +#define CHAR_X '\130' +#define CHAR_Y '\131' +#define CHAR_Z '\132' +#define CHAR_LEFT_SQUARE_BRACKET '\133' +#define CHAR_BACKSLASH '\134' +#define CHAR_RIGHT_SQUARE_BRACKET '\135' +#define CHAR_CIRCUMFLEX_ACCENT '\136' +#define CHAR_UNDERSCORE '\137' +#define CHAR_GRAVE_ACCENT '\140' +#define CHAR_a '\141' +#define CHAR_b '\142' +#define CHAR_c '\143' +#define CHAR_d '\144' +#define CHAR_e '\145' +#define CHAR_f '\146' +#define CHAR_g '\147' +#define CHAR_h '\150' +#define CHAR_i '\151' +#define CHAR_j '\152' +#define CHAR_k '\153' +#define CHAR_l '\154' +#define CHAR_m '\155' +#define CHAR_n '\156' +#define CHAR_o '\157' +#define CHAR_p '\160' +#define CHAR_q '\161' +#define CHAR_r '\162' +#define CHAR_s '\163' +#define CHAR_t '\164' +#define CHAR_u '\165' +#define CHAR_v '\166' +#define CHAR_w '\167' +#define CHAR_x '\170' +#define CHAR_y '\171' +#define CHAR_z '\172' +#define CHAR_LEFT_CURLY_BRACKET '\173' +#define CHAR_VERTICAL_LINE '\174' +#define CHAR_RIGHT_CURLY_BRACKET '\175' +#define CHAR_TILDE '\176' +#define CHAR_NBSP ((unsigned char)'\xa0') + +#define STR_HT "\011" +#define STR_VT "\013" +#define STR_FF "\014" +#define STR_CR "\015" +#define STR_NL "\012" +#define STR_BS "\010" +#define STR_BEL "\007" +#define STR_ESC "\033" +#define STR_DEL "\177" + +#define STR_SPACE "\040" +#define STR_EXCLAMATION_MARK "\041" +#define STR_QUOTATION_MARK "\042" +#define STR_NUMBER_SIGN "\043" +#define STR_DOLLAR_SIGN "\044" +#define STR_PERCENT_SIGN "\045" +#define STR_AMPERSAND "\046" +#define STR_APOSTROPHE "\047" +#define STR_LEFT_PARENTHESIS "\050" +#define STR_RIGHT_PARENTHESIS "\051" +#define STR_ASTERISK "\052" +#define STR_PLUS "\053" +#define STR_COMMA "\054" +#define STR_MINUS "\055" +#define STR_DOT "\056" +#define STR_SLASH "\057" +#define STR_0 "\060" +#define STR_1 "\061" +#define STR_2 "\062" +#define STR_3 "\063" +#define STR_4 "\064" +#define STR_5 "\065" +#define STR_6 "\066" +#define STR_7 "\067" +#define STR_8 "\070" +#define STR_9 "\071" +#define STR_COLON "\072" +#define STR_SEMICOLON "\073" +#define STR_LESS_THAN_SIGN "\074" +#define STR_EQUALS_SIGN "\075" +#define STR_GREATER_THAN_SIGN "\076" +#define STR_QUESTION_MARK "\077" +#define STR_COMMERCIAL_AT "\100" +#define STR_A "\101" +#define STR_B "\102" +#define STR_C "\103" +#define STR_D "\104" +#define STR_E "\105" +#define STR_F "\106" +#define STR_G "\107" +#define STR_H "\110" +#define STR_I "\111" +#define STR_J "\112" +#define STR_K "\113" +#define STR_L "\114" +#define STR_M "\115" +#define STR_N "\116" +#define STR_O "\117" +#define STR_P "\120" +#define STR_Q "\121" +#define STR_R "\122" +#define STR_S "\123" +#define STR_T "\124" +#define STR_U "\125" +#define STR_V "\126" +#define STR_W "\127" +#define STR_X "\130" +#define STR_Y "\131" +#define STR_Z "\132" +#define STR_LEFT_SQUARE_BRACKET "\133" +#define STR_BACKSLASH "\134" +#define STR_RIGHT_SQUARE_BRACKET "\135" +#define STR_CIRCUMFLEX_ACCENT "\136" +#define STR_UNDERSCORE "\137" +#define STR_GRAVE_ACCENT "\140" +#define STR_a "\141" +#define STR_b "\142" +#define STR_c "\143" +#define STR_d "\144" +#define STR_e "\145" +#define STR_f "\146" +#define STR_g "\147" +#define STR_h "\150" +#define STR_i "\151" +#define STR_j "\152" +#define STR_k "\153" +#define STR_l "\154" +#define STR_m "\155" +#define STR_n "\156" +#define STR_o "\157" +#define STR_p "\160" +#define STR_q "\161" +#define STR_r "\162" +#define STR_s "\163" +#define STR_t "\164" +#define STR_u "\165" +#define STR_v "\166" +#define STR_w "\167" +#define STR_x "\170" +#define STR_y "\171" +#define STR_z "\172" +#define STR_LEFT_CURLY_BRACKET "\173" +#define STR_VERTICAL_LINE "\174" +#define STR_RIGHT_CURLY_BRACKET "\175" +#define STR_TILDE "\176" + +#define STRING_ACCEPT0 STR_A STR_C STR_C STR_E STR_P STR_T "\0" +#define STRING_COMMIT0 STR_C STR_O STR_M STR_M STR_I STR_T "\0" +#define STRING_F0 STR_F "\0" +#define STRING_FAIL0 STR_F STR_A STR_I STR_L "\0" +#define STRING_MARK0 STR_M STR_A STR_R STR_K "\0" +#define STRING_PRUNE0 STR_P STR_R STR_U STR_N STR_E "\0" +#define STRING_SKIP0 STR_S STR_K STR_I STR_P "\0" +#define STRING_THEN STR_T STR_H STR_E STR_N + +#define STRING_alpha0 STR_a STR_l STR_p STR_h STR_a "\0" +#define STRING_lower0 STR_l STR_o STR_w STR_e STR_r "\0" +#define STRING_upper0 STR_u STR_p STR_p STR_e STR_r "\0" +#define STRING_alnum0 STR_a STR_l STR_n STR_u STR_m "\0" +#define STRING_ascii0 STR_a STR_s STR_c STR_i STR_i "\0" +#define STRING_blank0 STR_b STR_l STR_a STR_n STR_k "\0" +#define STRING_cntrl0 STR_c STR_n STR_t STR_r STR_l "\0" +#define STRING_digit0 STR_d STR_i STR_g STR_i STR_t "\0" +#define STRING_graph0 STR_g STR_r STR_a STR_p STR_h "\0" +#define STRING_print0 STR_p STR_r STR_i STR_n STR_t "\0" +#define STRING_punct0 STR_p STR_u STR_n STR_c STR_t "\0" +#define STRING_space0 STR_s STR_p STR_a STR_c STR_e "\0" +#define STRING_word0 STR_w STR_o STR_r STR_d "\0" +#define STRING_xdigit STR_x STR_d STR_i STR_g STR_i STR_t + +#define STRING_DEFINE STR_D STR_E STR_F STR_I STR_N STR_E +#define STRING_WEIRD_STARTWORD STR_LEFT_SQUARE_BRACKET STR_COLON STR_LESS_THAN_SIGN STR_COLON STR_RIGHT_SQUARE_BRACKET STR_RIGHT_SQUARE_BRACKET +#define STRING_WEIRD_ENDWORD STR_LEFT_SQUARE_BRACKET STR_COLON STR_GREATER_THAN_SIGN STR_COLON STR_RIGHT_SQUARE_BRACKET STR_RIGHT_SQUARE_BRACKET + +#define STRING_CR_RIGHTPAR STR_C STR_R STR_RIGHT_PARENTHESIS +#define STRING_LF_RIGHTPAR STR_L STR_F STR_RIGHT_PARENTHESIS +#define STRING_CRLF_RIGHTPAR STR_C STR_R STR_L STR_F STR_RIGHT_PARENTHESIS +#define STRING_ANY_RIGHTPAR STR_A STR_N STR_Y STR_RIGHT_PARENTHESIS +#define STRING_ANYCRLF_RIGHTPAR STR_A STR_N STR_Y STR_C STR_R STR_L STR_F STR_RIGHT_PARENTHESIS +#define STRING_BSR_ANYCRLF_RIGHTPAR STR_B STR_S STR_R STR_UNDERSCORE STR_A STR_N STR_Y STR_C STR_R STR_L STR_F STR_RIGHT_PARENTHESIS +#define STRING_BSR_UNICODE_RIGHTPAR STR_B STR_S STR_R STR_UNDERSCORE STR_U STR_N STR_I STR_C STR_O STR_D STR_E STR_RIGHT_PARENTHESIS +#define STRING_UTF8_RIGHTPAR STR_U STR_T STR_F STR_8 STR_RIGHT_PARENTHESIS +#define STRING_UTF16_RIGHTPAR STR_U STR_T STR_F STR_1 STR_6 STR_RIGHT_PARENTHESIS +#define STRING_UTF32_RIGHTPAR STR_U STR_T STR_F STR_3 STR_2 STR_RIGHT_PARENTHESIS +#define STRING_UTF_RIGHTPAR STR_U STR_T STR_F STR_RIGHT_PARENTHESIS +#define STRING_UCP_RIGHTPAR STR_U STR_C STR_P STR_RIGHT_PARENTHESIS +#define STRING_NO_AUTO_POSSESS_RIGHTPAR STR_N STR_O STR_UNDERSCORE STR_A STR_U STR_T STR_O STR_UNDERSCORE STR_P STR_O STR_S STR_S STR_E STR_S STR_S STR_RIGHT_PARENTHESIS +#define STRING_NO_START_OPT_RIGHTPAR STR_N STR_O STR_UNDERSCORE STR_S STR_T STR_A STR_R STR_T STR_UNDERSCORE STR_O STR_P STR_T STR_RIGHT_PARENTHESIS +#define STRING_LIMIT_MATCH_EQ STR_L STR_I STR_M STR_I STR_T STR_UNDERSCORE STR_M STR_A STR_T STR_C STR_H STR_EQUALS_SIGN +#define STRING_LIMIT_RECURSION_EQ STR_L STR_I STR_M STR_I STR_T STR_UNDERSCORE STR_R STR_E STR_C STR_U STR_R STR_S STR_I STR_O STR_N STR_EQUALS_SIGN + +#endif /* SUPPORT_UTF */ + +/* Escape items that are just an encoding of a particular data value. */ + +#ifndef ESC_a +#define ESC_a CHAR_BEL +#endif + +#ifndef ESC_e +#define ESC_e CHAR_ESC +#endif + +#ifndef ESC_f +#define ESC_f CHAR_FF +#endif + +#ifndef ESC_n +#define ESC_n CHAR_LF +#endif + +#ifndef ESC_r +#define ESC_r CHAR_CR +#endif + +/* We can't officially use ESC_t because it is a POSIX reserved identifier +(presumably because of all the others like size_t). */ + +#ifndef ESC_tee +#define ESC_tee CHAR_HT +#endif + +/* Codes for different types of Unicode property */ + +#define PT_ANY 0 /* Any property - matches all chars */ +#define PT_LAMP 1 /* L& - the union of Lu, Ll, Lt */ +#define PT_GC 2 /* Specified general characteristic (e.g. L) */ +#define PT_PC 3 /* Specified particular characteristic (e.g. Lu) */ +#define PT_SC 4 /* Script (e.g. Han) */ +#define PT_ALNUM 5 /* Alphanumeric - the union of L and N */ +#define PT_SPACE 6 /* Perl space - Z plus 9,10,12,13 */ +#define PT_PXSPACE 7 /* POSIX space - Z plus 9,10,11,12,13 */ +#define PT_WORD 8 /* Word - L plus N plus underscore */ +#define PT_CLIST 9 /* Pseudo-property: match character list */ +#define PT_UCNC 10 /* Universal Character nameable character */ +#define PT_TABSIZE 11 /* Size of square table for autopossessify tests */ + +/* The following special properties are used only in XCLASS items, when POSIX +classes are specified and PCRE_UCP is set - in other words, for Unicode +handling of these classes. They are not available via the \p or \P escapes like +those in the above list, and so they do not take part in the autopossessifying +table. */ + +#define PT_PXGRAPH 11 /* [:graph:] - characters that mark the paper */ +#define PT_PXPRINT 12 /* [:print:] - [:graph:] plus non-control spaces */ +#define PT_PXPUNCT 13 /* [:punct:] - punctuation characters */ + +/* Flag bits and data types for the extended class (OP_XCLASS) for classes that +contain characters with values greater than 255. */ + +#define XCL_NOT 0x01 /* Flag: this is a negative class */ +#define XCL_MAP 0x02 /* Flag: a 32-byte map is present */ +#define XCL_HASPROP 0x04 /* Flag: property checks are present. */ + +#define XCL_END 0 /* Marks end of individual items */ +#define XCL_SINGLE 1 /* Single item (one multibyte char) follows */ +#define XCL_RANGE 2 /* A range (two multibyte chars) follows */ +#define XCL_PROP 3 /* Unicode property (2-byte property code follows) */ +#define XCL_NOTPROP 4 /* Unicode inverted property (ditto) */ + +/* These are escaped items that aren't just an encoding of a particular data +value such as \n. They must have non-zero values, as check_escape() returns 0 +for a data character. Also, they must appear in the same order as in the +opcode definitions below, up to ESC_z. There's a dummy for OP_ALLANY because it +corresponds to "." in DOTALL mode rather than an escape sequence. It is also +used for [^] in JavaScript compatibility mode, and for \C in non-utf mode. In +non-DOTALL mode, "." behaves like \N. + +The special values ESC_DU, ESC_du, etc. are used instead of ESC_D, ESC_d, etc. +when PCRE_UCP is set and replacement of \d etc by \p sequences is required. +They must be contiguous, and remain in order so that the replacements can be +looked up from a table. + +Negative numbers are used to encode a backreference (\1, \2, \3, etc.) in +check_escape(). There are two tests in the code for an escape +greater than ESC_b and less than ESC_Z to detect the types that may be +repeated. These are the types that consume characters. If any new escapes are +put in between that don't consume a character, that code will have to change. +*/ + +enum { ESC_A = 1, ESC_G, ESC_K, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, + ESC_W, ESC_w, ESC_N, ESC_dum, ESC_C, ESC_P, ESC_p, ESC_R, ESC_H, + ESC_h, ESC_V, ESC_v, ESC_X, ESC_Z, ESC_z, + ESC_E, ESC_Q, ESC_g, ESC_k, + ESC_DU, ESC_du, ESC_SU, ESC_su, ESC_WU, ESC_wu }; + + +/********************** Opcode definitions ******************/ + +/****** NOTE NOTE NOTE ****** + +Starting from 1 (i.e. after OP_END), the values up to OP_EOD must correspond in +order to the list of escapes immediately above. Furthermore, values up to +OP_DOLLM must not be changed without adjusting the table called autoposstab in +pcre_compile.c + +Whenever this list is updated, the two macro definitions that follow must be +updated to match. The possessification table called "opcode_possessify" in +pcre_compile.c must also be updated, and also the tables called "coptable" +and "poptable" in pcre_dfa_exec.c. + +****** NOTE NOTE NOTE ******/ + + +/* The values between FIRST_AUTOTAB_OP and LAST_AUTOTAB_RIGHT_OP, inclusive, +are used in a table for deciding whether a repeated character type can be +auto-possessified. */ + +#define FIRST_AUTOTAB_OP OP_NOT_DIGIT +#define LAST_AUTOTAB_LEFT_OP OP_EXTUNI +#define LAST_AUTOTAB_RIGHT_OP OP_DOLLM + +enum { + OP_END, /* 0 End of pattern */ + + /* Values corresponding to backslashed metacharacters */ + + OP_SOD, /* 1 Start of data: \A */ + OP_SOM, /* 2 Start of match (subject + offset): \G */ + OP_SET_SOM, /* 3 Set start of match (\K) */ + OP_NOT_WORD_BOUNDARY, /* 4 \B */ + OP_WORD_BOUNDARY, /* 5 \b */ + OP_NOT_DIGIT, /* 6 \D */ + OP_DIGIT, /* 7 \d */ + OP_NOT_WHITESPACE, /* 8 \S */ + OP_WHITESPACE, /* 9 \s */ + OP_NOT_WORDCHAR, /* 10 \W */ + OP_WORDCHAR, /* 11 \w */ + + OP_ANY, /* 12 Match any character except newline (\N) */ + OP_ALLANY, /* 13 Match any character */ + OP_ANYBYTE, /* 14 Match any byte (\C); different to OP_ANY for UTF-8 */ + OP_NOTPROP, /* 15 \P (not Unicode property) */ + OP_PROP, /* 16 \p (Unicode property) */ + OP_ANYNL, /* 17 \R (any newline sequence) */ + OP_NOT_HSPACE, /* 18 \H (not horizontal whitespace) */ + OP_HSPACE, /* 19 \h (horizontal whitespace) */ + OP_NOT_VSPACE, /* 20 \V (not vertical whitespace) */ + OP_VSPACE, /* 21 \v (vertical whitespace) */ + OP_EXTUNI, /* 22 \X (extended Unicode sequence */ + OP_EODN, /* 23 End of data or \n at end of data (\Z) */ + OP_EOD, /* 24 End of data (\z) */ + + /* Line end assertions */ + + OP_DOLL, /* 25 End of line - not multiline */ + OP_DOLLM, /* 26 End of line - multiline */ + OP_CIRC, /* 27 Start of line - not multiline */ + OP_CIRCM, /* 28 Start of line - multiline */ + + /* Single characters; caseful must precede the caseless ones */ + + OP_CHAR, /* 29 Match one character, casefully */ + OP_CHARI, /* 30 Match one character, caselessly */ + OP_NOT, /* 31 Match one character, not the given one, casefully */ + OP_NOTI, /* 32 Match one character, not the given one, caselessly */ + + /* The following sets of 13 opcodes must always be kept in step because + the offset from the first one is used to generate the others. */ + + /* Repeated characters; caseful must precede the caseless ones */ + + OP_STAR, /* 33 The maximizing and minimizing versions of */ + OP_MINSTAR, /* 34 these six opcodes must come in pairs, with */ + OP_PLUS, /* 35 the minimizing one second. */ + OP_MINPLUS, /* 36 */ + OP_QUERY, /* 37 */ + OP_MINQUERY, /* 38 */ + + OP_UPTO, /* 39 From 0 to n matches of one character, caseful*/ + OP_MINUPTO, /* 40 */ + OP_EXACT, /* 41 Exactly n matches */ + + OP_POSSTAR, /* 42 Possessified star, caseful */ + OP_POSPLUS, /* 43 Possessified plus, caseful */ + OP_POSQUERY, /* 44 Posesssified query, caseful */ + OP_POSUPTO, /* 45 Possessified upto, caseful */ + + /* Repeated characters; caseless must follow the caseful ones */ + + OP_STARI, /* 46 */ + OP_MINSTARI, /* 47 */ + OP_PLUSI, /* 48 */ + OP_MINPLUSI, /* 49 */ + OP_QUERYI, /* 50 */ + OP_MINQUERYI, /* 51 */ + + OP_UPTOI, /* 52 From 0 to n matches of one character, caseless */ + OP_MINUPTOI, /* 53 */ + OP_EXACTI, /* 54 */ + + OP_POSSTARI, /* 55 Possessified star, caseless */ + OP_POSPLUSI, /* 56 Possessified plus, caseless */ + OP_POSQUERYI, /* 57 Posesssified query, caseless */ + OP_POSUPTOI, /* 58 Possessified upto, caseless */ + + /* The negated ones must follow the non-negated ones, and match them */ + /* Negated repeated character, caseful; must precede the caseless ones */ + + OP_NOTSTAR, /* 59 The maximizing and minimizing versions of */ + OP_NOTMINSTAR, /* 60 these six opcodes must come in pairs, with */ + OP_NOTPLUS, /* 61 the minimizing one second. They must be in */ + OP_NOTMINPLUS, /* 62 exactly the same order as those above. */ + OP_NOTQUERY, /* 63 */ + OP_NOTMINQUERY, /* 64 */ + + OP_NOTUPTO, /* 65 From 0 to n matches, caseful */ + OP_NOTMINUPTO, /* 66 */ + OP_NOTEXACT, /* 67 Exactly n matches */ + + OP_NOTPOSSTAR, /* 68 Possessified versions, caseful */ + OP_NOTPOSPLUS, /* 69 */ + OP_NOTPOSQUERY, /* 70 */ + OP_NOTPOSUPTO, /* 71 */ + + /* Negated repeated character, caseless; must follow the caseful ones */ + + OP_NOTSTARI, /* 72 */ + OP_NOTMINSTARI, /* 73 */ + OP_NOTPLUSI, /* 74 */ + OP_NOTMINPLUSI, /* 75 */ + OP_NOTQUERYI, /* 76 */ + OP_NOTMINQUERYI, /* 77 */ + + OP_NOTUPTOI, /* 78 From 0 to n matches, caseless */ + OP_NOTMINUPTOI, /* 79 */ + OP_NOTEXACTI, /* 80 Exactly n matches */ + + OP_NOTPOSSTARI, /* 81 Possessified versions, caseless */ + OP_NOTPOSPLUSI, /* 82 */ + OP_NOTPOSQUERYI, /* 83 */ + OP_NOTPOSUPTOI, /* 84 */ + + /* Character types */ + + OP_TYPESTAR, /* 85 The maximizing and minimizing versions of */ + OP_TYPEMINSTAR, /* 86 these six opcodes must come in pairs, with */ + OP_TYPEPLUS, /* 87 the minimizing one second. These codes must */ + OP_TYPEMINPLUS, /* 88 be in exactly the same order as those above. */ + OP_TYPEQUERY, /* 89 */ + OP_TYPEMINQUERY, /* 90 */ + + OP_TYPEUPTO, /* 91 From 0 to n matches */ + OP_TYPEMINUPTO, /* 92 */ + OP_TYPEEXACT, /* 93 Exactly n matches */ + + OP_TYPEPOSSTAR, /* 94 Possessified versions */ + OP_TYPEPOSPLUS, /* 95 */ + OP_TYPEPOSQUERY, /* 96 */ + OP_TYPEPOSUPTO, /* 97 */ + + /* These are used for character classes and back references; only the + first six are the same as the sets above. */ + + OP_CRSTAR, /* 98 The maximizing and minimizing versions of */ + OP_CRMINSTAR, /* 99 all these opcodes must come in pairs, with */ + OP_CRPLUS, /* 100 the minimizing one second. These codes must */ + OP_CRMINPLUS, /* 101 be in exactly the same order as those above. */ + OP_CRQUERY, /* 102 */ + OP_CRMINQUERY, /* 103 */ + + OP_CRRANGE, /* 104 These are different to the three sets above. */ + OP_CRMINRANGE, /* 105 */ + + OP_CRPOSSTAR, /* 106 Possessified versions */ + OP_CRPOSPLUS, /* 107 */ + OP_CRPOSQUERY, /* 108 */ + OP_CRPOSRANGE, /* 109 */ + + /* End of quantifier opcodes */ + + OP_CLASS, /* 110 Match a character class, chars < 256 only */ + OP_NCLASS, /* 111 Same, but the bitmap was created from a negative + class - the difference is relevant only when a + character > 255 is encountered. */ + OP_XCLASS, /* 112 Extended class for handling > 255 chars within the + class. This does both positive and negative. */ + OP_REF, /* 113 Match a back reference, casefully */ + OP_REFI, /* 114 Match a back reference, caselessly */ + OP_DNREF, /* 115 Match a duplicate name backref, casefully */ + OP_DNREFI, /* 116 Match a duplicate name backref, caselessly */ + OP_RECURSE, /* 117 Match a numbered subpattern (possibly recursive) */ + OP_CALLOUT, /* 118 Call out to external function if provided */ + + OP_ALT, /* 119 Start of alternation */ + OP_KET, /* 120 End of group that doesn't have an unbounded repeat */ + OP_KETRMAX, /* 121 These two must remain together and in this */ + OP_KETRMIN, /* 122 order. They are for groups the repeat for ever. */ + OP_KETRPOS, /* 123 Possessive unlimited repeat. */ + + /* The assertions must come before BRA, CBRA, ONCE, and COND, and the four + asserts must remain in order. */ + + OP_REVERSE, /* 124 Move pointer back - used in lookbehind assertions */ + OP_ASSERT, /* 125 Positive lookahead */ + OP_ASSERT_NOT, /* 126 Negative lookahead */ + OP_ASSERTBACK, /* 127 Positive lookbehind */ + OP_ASSERTBACK_NOT, /* 128 Negative lookbehind */ + + /* ONCE, ONCE_NC, BRA, BRAPOS, CBRA, CBRAPOS, and COND must come immediately + after the assertions, with ONCE first, as there's a test for >= ONCE for a + subpattern that isn't an assertion. The POS versions must immediately follow + the non-POS versions in each case. */ + + OP_ONCE, /* 129 Atomic group, contains captures */ + OP_ONCE_NC, /* 130 Atomic group containing no captures */ + OP_BRA, /* 131 Start of non-capturing bracket */ + OP_BRAPOS, /* 132 Ditto, with unlimited, possessive repeat */ + OP_CBRA, /* 133 Start of capturing bracket */ + OP_CBRAPOS, /* 134 Ditto, with unlimited, possessive repeat */ + OP_COND, /* 135 Conditional group */ + + /* These five must follow the previous five, in the same order. There's a + check for >= SBRA to distinguish the two sets. */ + + OP_SBRA, /* 136 Start of non-capturing bracket, check empty */ + OP_SBRAPOS, /* 137 Ditto, with unlimited, possessive repeat */ + OP_SCBRA, /* 138 Start of capturing bracket, check empty */ + OP_SCBRAPOS, /* 139 Ditto, with unlimited, possessive repeat */ + OP_SCOND, /* 140 Conditional group, check empty */ + + /* The next two pairs must (respectively) be kept together. */ + + OP_CREF, /* 141 Used to hold a capture number as condition */ + OP_DNCREF, /* 142 Used to point to duplicate names as a condition */ + OP_RREF, /* 143 Used to hold a recursion number as condition */ + OP_DNRREF, /* 144 Used to point to duplicate names as a condition */ + OP_DEF, /* 145 The DEFINE condition */ + + OP_BRAZERO, /* 146 These two must remain together and in this */ + OP_BRAMINZERO, /* 147 order. */ + OP_BRAPOSZERO, /* 148 */ + + /* These are backtracking control verbs */ + + OP_MARK, /* 149 always has an argument */ + OP_PRUNE, /* 150 */ + OP_PRUNE_ARG, /* 151 same, but with argument */ + OP_SKIP, /* 152 */ + OP_SKIP_ARG, /* 153 same, but with argument */ + OP_THEN, /* 154 */ + OP_THEN_ARG, /* 155 same, but with argument */ + OP_COMMIT, /* 156 */ + + /* These are forced failure and success verbs */ + + OP_FAIL, /* 157 */ + OP_ACCEPT, /* 158 */ + OP_ASSERT_ACCEPT, /* 159 Used inside assertions */ + OP_CLOSE, /* 160 Used before OP_ACCEPT to close open captures */ + + /* This is used to skip a subpattern with a {0} quantifier */ + + OP_SKIPZERO, /* 161 */ + + /* This is not an opcode, but is used to check that tables indexed by opcode + are the correct length, in order to catch updating errors - there have been + some in the past. */ + + OP_TABLE_LENGTH +}; + +/* *** NOTE NOTE NOTE *** Whenever the list above is updated, the two macro +definitions that follow must also be updated to match. There are also tables +called "opcode_possessify" in pcre_compile.c and "coptable" and "poptable" in +pcre_dfa_exec.c that must be updated. */ + + +/* This macro defines textual names for all the opcodes. These are used only +for debugging, and some of them are only partial names. The macro is referenced +only in pcre_printint.c, which fills out the full names in many cases (and in +some cases doesn't actually use these names at all). */ + +#define OP_NAME_LIST \ + "End", "\\A", "\\G", "\\K", "\\B", "\\b", "\\D", "\\d", \ + "\\S", "\\s", "\\W", "\\w", "Any", "AllAny", "Anybyte", \ + "notprop", "prop", "\\R", "\\H", "\\h", "\\V", "\\v", \ + "extuni", "\\Z", "\\z", \ + "$", "$", "^", "^", "char", "chari", "not", "noti", \ + "*", "*?", "+", "+?", "?", "??", \ + "{", "{", "{", \ + "*+","++", "?+", "{", \ + "*", "*?", "+", "+?", "?", "??", \ + "{", "{", "{", \ + "*+","++", "?+", "{", \ + "*", "*?", "+", "+?", "?", "??", \ + "{", "{", "{", \ + "*+","++", "?+", "{", \ + "*", "*?", "+", "+?", "?", "??", \ + "{", "{", "{", \ + "*+","++", "?+", "{", \ + "*", "*?", "+", "+?", "?", "??", "{", "{", "{", \ + "*+","++", "?+", "{", \ + "*", "*?", "+", "+?", "?", "??", "{", "{", \ + "*+","++", "?+", "{", \ + "class", "nclass", "xclass", "Ref", "Refi", "DnRef", "DnRefi", \ + "Recurse", "Callout", \ + "Alt", "Ket", "KetRmax", "KetRmin", "KetRpos", \ + "Reverse", "Assert", "Assert not", "AssertB", "AssertB not", \ + "Once", "Once_NC", \ + "Bra", "BraPos", "CBra", "CBraPos", \ + "Cond", \ + "SBra", "SBraPos", "SCBra", "SCBraPos", \ + "SCond", \ + "Cond ref", "Cond dnref", "Cond rec", "Cond dnrec", "Cond def", \ + "Brazero", "Braminzero", "Braposzero", \ + "*MARK", "*PRUNE", "*PRUNE", "*SKIP", "*SKIP", \ + "*THEN", "*THEN", "*COMMIT", "*FAIL", \ + "*ACCEPT", "*ASSERT_ACCEPT", \ + "Close", "Skip zero" + + +/* This macro defines the length of fixed length operations in the compiled +regex. The lengths are used when searching for specific things, and also in the +debugging printing of a compiled regex. We use a macro so that it can be +defined close to the definitions of the opcodes themselves. + +As things have been extended, some of these are no longer fixed lenths, but are +minima instead. For example, the length of a single-character repeat may vary +in UTF-8 mode. The code that uses this table must know about such things. */ + +#define OP_LENGTHS \ + 1, /* End */ \ + 1, 1, 1, 1, 1, /* \A, \G, \K, \B, \b */ \ + 1, 1, 1, 1, 1, 1, /* \D, \d, \S, \s, \W, \w */ \ + 1, 1, 1, /* Any, AllAny, Anybyte */ \ + 3, 3, /* \P, \p */ \ + 1, 1, 1, 1, 1, /* \R, \H, \h, \V, \v */ \ + 1, /* \X */ \ + 1, 1, 1, 1, 1, 1, /* \Z, \z, $, $M ^, ^M */ \ + 2, /* Char - the minimum length */ \ + 2, /* Chari - the minimum length */ \ + 2, /* not */ \ + 2, /* noti */ \ + /* Positive single-char repeats ** These are */ \ + 2, 2, 2, 2, 2, 2, /* *, *?, +, +?, ?, ?? ** minima in */ \ + 2+IMM2_SIZE, 2+IMM2_SIZE, /* upto, minupto ** mode */ \ + 2+IMM2_SIZE, /* exact */ \ + 2, 2, 2, 2+IMM2_SIZE, /* *+, ++, ?+, upto+ */ \ + 2, 2, 2, 2, 2, 2, /* *I, *?I, +I, +?I, ?I, ??I ** UTF-8 */ \ + 2+IMM2_SIZE, 2+IMM2_SIZE, /* upto I, minupto I */ \ + 2+IMM2_SIZE, /* exact I */ \ + 2, 2, 2, 2+IMM2_SIZE, /* *+I, ++I, ?+I, upto+I */ \ + /* Negative single-char repeats - only for chars < 256 */ \ + 2, 2, 2, 2, 2, 2, /* NOT *, *?, +, +?, ?, ?? */ \ + 2+IMM2_SIZE, 2+IMM2_SIZE, /* NOT upto, minupto */ \ + 2+IMM2_SIZE, /* NOT exact */ \ + 2, 2, 2, 2+IMM2_SIZE, /* Possessive NOT *, +, ?, upto */ \ + 2, 2, 2, 2, 2, 2, /* NOT *I, *?I, +I, +?I, ?I, ??I */ \ + 2+IMM2_SIZE, 2+IMM2_SIZE, /* NOT upto I, minupto I */ \ + 2+IMM2_SIZE, /* NOT exact I */ \ + 2, 2, 2, 2+IMM2_SIZE, /* Possessive NOT *I, +I, ?I, upto I */ \ + /* Positive type repeats */ \ + 2, 2, 2, 2, 2, 2, /* Type *, *?, +, +?, ?, ?? */ \ + 2+IMM2_SIZE, 2+IMM2_SIZE, /* Type upto, minupto */ \ + 2+IMM2_SIZE, /* Type exact */ \ + 2, 2, 2, 2+IMM2_SIZE, /* Possessive *+, ++, ?+, upto+ */ \ + /* Character class & ref repeats */ \ + 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */ \ + 1+2*IMM2_SIZE, 1+2*IMM2_SIZE, /* CRRANGE, CRMINRANGE */ \ + 1, 1, 1, 1+2*IMM2_SIZE, /* Possessive *+, ++, ?+, CRPOSRANGE */ \ + 1+(32/sizeof(pcre_uchar)), /* CLASS */ \ + 1+(32/sizeof(pcre_uchar)), /* NCLASS */ \ + 0, /* XCLASS - variable length */ \ + 1+IMM2_SIZE, /* REF */ \ + 1+IMM2_SIZE, /* REFI */ \ + 1+2*IMM2_SIZE, /* DNREF */ \ + 1+2*IMM2_SIZE, /* DNREFI */ \ + 1+LINK_SIZE, /* RECURSE */ \ + 2+2*LINK_SIZE, /* CALLOUT */ \ + 1+LINK_SIZE, /* Alt */ \ + 1+LINK_SIZE, /* Ket */ \ + 1+LINK_SIZE, /* KetRmax */ \ + 1+LINK_SIZE, /* KetRmin */ \ + 1+LINK_SIZE, /* KetRpos */ \ + 1+LINK_SIZE, /* Reverse */ \ + 1+LINK_SIZE, /* Assert */ \ + 1+LINK_SIZE, /* Assert not */ \ + 1+LINK_SIZE, /* Assert behind */ \ + 1+LINK_SIZE, /* Assert behind not */ \ + 1+LINK_SIZE, /* ONCE */ \ + 1+LINK_SIZE, /* ONCE_NC */ \ + 1+LINK_SIZE, /* BRA */ \ + 1+LINK_SIZE, /* BRAPOS */ \ + 1+LINK_SIZE+IMM2_SIZE, /* CBRA */ \ + 1+LINK_SIZE+IMM2_SIZE, /* CBRAPOS */ \ + 1+LINK_SIZE, /* COND */ \ + 1+LINK_SIZE, /* SBRA */ \ + 1+LINK_SIZE, /* SBRAPOS */ \ + 1+LINK_SIZE+IMM2_SIZE, /* SCBRA */ \ + 1+LINK_SIZE+IMM2_SIZE, /* SCBRAPOS */ \ + 1+LINK_SIZE, /* SCOND */ \ + 1+IMM2_SIZE, 1+2*IMM2_SIZE, /* CREF, DNCREF */ \ + 1+IMM2_SIZE, 1+2*IMM2_SIZE, /* RREF, DNRREF */ \ + 1, /* DEF */ \ + 1, 1, 1, /* BRAZERO, BRAMINZERO, BRAPOSZERO */ \ + 3, 1, 3, /* MARK, PRUNE, PRUNE_ARG */ \ + 1, 3, /* SKIP, SKIP_ARG */ \ + 1, 3, /* THEN, THEN_ARG */ \ + 1, 1, 1, 1, /* COMMIT, FAIL, ACCEPT, ASSERT_ACCEPT */ \ + 1+IMM2_SIZE, 1 /* CLOSE, SKIPZERO */ + +/* A magic value for OP_RREF to indicate the "any recursion" condition. */ + +#define RREF_ANY 0xffff + +/* Compile time error code numbers. They are given names so that they can more +easily be tracked. When a new number is added, the table called eint in +pcreposix.c must be updated. */ + +enum { ERR0, ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, + ERR10, ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, + ERR20, ERR21, ERR22, ERR23, ERR24, ERR25, ERR26, ERR27, ERR28, ERR29, + ERR30, ERR31, ERR32, ERR33, ERR34, ERR35, ERR36, ERR37, ERR38, ERR39, + ERR40, ERR41, ERR42, ERR43, ERR44, ERR45, ERR46, ERR47, ERR48, ERR49, + ERR50, ERR51, ERR52, ERR53, ERR54, ERR55, ERR56, ERR57, ERR58, ERR59, + ERR60, ERR61, ERR62, ERR63, ERR64, ERR65, ERR66, ERR67, ERR68, ERR69, + ERR70, ERR71, ERR72, ERR73, ERR74, ERR75, ERR76, ERR77, ERR78, ERR79, + ERR80, ERR81, ERR82, ERR83, ERR84, ERR85, ERR86, ERR87, ERRCOUNT }; + +/* JIT compiling modes. The function list is indexed by them. */ + +enum { JIT_COMPILE, JIT_PARTIAL_SOFT_COMPILE, JIT_PARTIAL_HARD_COMPILE, + JIT_NUMBER_OF_COMPILE_MODES }; + +/* The real format of the start of the pcre block; the index of names and the +code vector run on as long as necessary after the end. We store an explicit +offset to the name table so that if a regex is compiled on one host, saved, and +then run on another where the size of pointers is different, all might still +be well. + +The size of the structure must be a multiple of 8 bytes. For the case of +compiled-on-4 and run-on-8, we include an extra pointer that is always NULL so +that there are an even number of pointers which therefore are a multiple of 8 +bytes. + +It is necessary to fork the struct for the 32 bit library, since it needs to +use pcre_uint32 for first_char and req_char. We can't put an ifdef inside the +typedef because pcretest needs access to the struct of the 8-, 16- and 32-bit +variants. + +*** WARNING *** +When new fields are added to these structures, remember to adjust the code in +pcre_byte_order.c that is concerned with swapping the byte order of the fields +when a compiled regex is reloaded on a host with different endianness. +*** WARNING *** +There is also similar byte-flipping code in pcretest.c, which is used for +testing the byte-flipping features. It must also be kept in step. +*** WARNING *** +*/ + +typedef struct real_pcre8_or_16 { + pcre_uint32 magic_number; + pcre_uint32 size; /* Total that was malloced */ + pcre_uint32 options; /* Public options */ + pcre_uint32 flags; /* Private flags */ + pcre_uint32 limit_match; /* Limit set from regex */ + pcre_uint32 limit_recursion; /* Limit set from regex */ + pcre_uint16 first_char; /* Starting character */ + pcre_uint16 req_char; /* This character must be seen */ + pcre_uint16 max_lookbehind; /* Longest lookbehind (characters) */ + pcre_uint16 top_bracket; /* Highest numbered group */ + pcre_uint16 top_backref; /* Highest numbered back reference */ + pcre_uint16 name_table_offset; /* Offset to name table that follows */ + pcre_uint16 name_entry_size; /* Size of any name items */ + pcre_uint16 name_count; /* Number of name items */ + pcre_uint16 ref_count; /* Reference count */ + pcre_uint16 dummy1; /* To ensure size is a multiple of 8 */ + pcre_uint16 dummy2; /* To ensure size is a multiple of 8 */ + pcre_uint16 dummy3; /* To ensure size is a multiple of 8 */ + const pcre_uint8 *tables; /* Pointer to tables or NULL for std */ + void *nullpad; /* NULL padding */ +} real_pcre8_or_16; + +typedef struct real_pcre8_or_16 real_pcre; +typedef struct real_pcre8_or_16 real_pcre16; + +typedef struct real_pcre32 { + pcre_uint32 magic_number; + pcre_uint32 size; /* Total that was malloced */ + pcre_uint32 options; /* Public options */ + pcre_uint32 flags; /* Private flags */ + pcre_uint32 limit_match; /* Limit set from regex */ + pcre_uint32 limit_recursion; /* Limit set from regex */ + pcre_uint32 first_char; /* Starting character */ + pcre_uint32 req_char; /* This character must be seen */ + pcre_uint16 max_lookbehind; /* Longest lookbehind (characters) */ + pcre_uint16 top_bracket; /* Highest numbered group */ + pcre_uint16 top_backref; /* Highest numbered back reference */ + pcre_uint16 name_table_offset; /* Offset to name table that follows */ + pcre_uint16 name_entry_size; /* Size of any name items */ + pcre_uint16 name_count; /* Number of name items */ + pcre_uint16 ref_count; /* Reference count */ + pcre_uint16 dummy; /* To ensure size is a multiple of 8 */ + const pcre_uint8 *tables; /* Pointer to tables or NULL for std */ + void *nullpad; /* NULL padding */ +} real_pcre32; + +#if defined COMPILE_PCRE8 +#define REAL_PCRE real_pcre +#elif defined COMPILE_PCRE16 +#define REAL_PCRE real_pcre16 +#elif defined COMPILE_PCRE32 +#define REAL_PCRE real_pcre32 +#endif + +/* Assert that the size of REAL_PCRE is divisible by 8 */ +typedef int __assert_real_pcre_size_divisible_8[(sizeof(REAL_PCRE) % 8) == 0 ? 1 : -1]; + +/* Needed in pcretest to access some fields in the real_pcre* structures + * directly. They're unified for 8/16/32 bits since the structs only differ + * after these fields; if that ever changes, need to fork those defines into + * 8/16 and 32 bit versions. */ +#define REAL_PCRE_MAGIC(re) (((REAL_PCRE*)re)->magic_number) +#define REAL_PCRE_SIZE(re) (((REAL_PCRE*)re)->size) +#define REAL_PCRE_OPTIONS(re) (((REAL_PCRE*)re)->options) +#define REAL_PCRE_FLAGS(re) (((REAL_PCRE*)re)->flags) + +/* The format of the block used to store data from pcre_study(). The same +remark (see NOTE above) about extending this structure applies. */ + +typedef struct pcre_study_data { + pcre_uint32 size; /* Total that was malloced */ + pcre_uint32 flags; /* Private flags */ + pcre_uint8 start_bits[32]; /* Starting char bits */ + pcre_uint32 minlength; /* Minimum subject length */ +} pcre_study_data; + +/* Structure for building a chain of open capturing subpatterns during +compiling, so that instructions to close them can be compiled when (*ACCEPT) is +encountered. This is also used to identify subpatterns that contain recursive +back references to themselves, so that they can be made atomic. */ + +typedef struct open_capitem { + struct open_capitem *next; /* Chain link */ + pcre_uint16 number; /* Capture number */ + pcre_uint16 flag; /* Set TRUE if recursive back ref */ +} open_capitem; + +/* Structure for building a list of named groups during the first pass of +compiling. */ + +typedef struct named_group { + const pcre_uchar *name; /* Points to the name in the pattern */ + int length; /* Length of the name */ + pcre_uint32 number; /* Group number */ +} named_group; + +/* Structure for passing "static" information around between the functions +doing the compiling, so that they are thread-safe. */ + +typedef struct compile_data { + const pcre_uint8 *lcc; /* Points to lower casing table */ + const pcre_uint8 *fcc; /* Points to case-flipping table */ + const pcre_uint8 *cbits; /* Points to character type table */ + const pcre_uint8 *ctypes; /* Points to table of type maps */ + const pcre_uchar *start_workspace;/* The start of working space */ + const pcre_uchar *start_code; /* The start of the compiled code */ + const pcre_uchar *start_pattern; /* The start of the pattern */ + const pcre_uchar *end_pattern; /* The end of the pattern */ + pcre_uchar *hwm; /* High watermark of workspace */ + open_capitem *open_caps; /* Chain of open capture items */ + named_group *named_groups; /* Points to vector in pre-compile */ + pcre_uchar *name_table; /* The name/number table */ + int names_found; /* Number of entries so far */ + int name_entry_size; /* Size of each entry */ + int named_group_list_size; /* Number of entries in the list */ + int workspace_size; /* Size of workspace */ + unsigned int bracount; /* Count of capturing parens as we compile */ + int final_bracount; /* Saved value after first pass */ + int max_lookbehind; /* Maximum lookbehind (characters) */ + int top_backref; /* Maximum back reference */ + unsigned int backref_map; /* Bitmap of low back refs */ + unsigned int namedrefcount; /* Number of backreferences by name */ + int parens_depth; /* Depth of nested parentheses */ + int assert_depth; /* Depth of nested assertions */ + pcre_uint32 external_options; /* External (initial) options */ + pcre_uint32 external_flags; /* External flag bits to be set */ + int req_varyopt; /* "After variable item" flag for reqbyte */ + BOOL had_accept; /* (*ACCEPT) encountered */ + BOOL had_pruneorskip; /* (*PRUNE) or (*SKIP) encountered */ + BOOL check_lookbehind; /* Lookbehinds need later checking */ + BOOL dupnames; /* Duplicate names exist */ + BOOL dupgroups; /* Duplicate groups exist: (?| found */ + BOOL iscondassert; /* Next assert is a condition */ + int nltype; /* Newline type */ + int nllen; /* Newline string length */ + pcre_uchar nl[4]; /* Newline string when fixed length */ +} compile_data; + +/* Structure for maintaining a chain of pointers to the currently incomplete +branches, for testing for left recursion while compiling. */ + +typedef struct branch_chain { + struct branch_chain *outer; + pcre_uchar *current_branch; +} branch_chain; + +/* Structure for mutual recursion detection. */ + +typedef struct recurse_check { + struct recurse_check *prev; + const pcre_uchar *group; +} recurse_check; + +/* Structure for items in a linked list that represents an explicit recursive +call within the pattern; used by pcre_exec(). */ + +typedef struct recursion_info { + struct recursion_info *prevrec; /* Previous recursion record (or NULL) */ + unsigned int group_num; /* Number of group that was called */ + int *offset_save; /* Pointer to start of saved offsets */ + int saved_max; /* Number of saved offsets */ + int saved_capture_last; /* Last capture number */ + PCRE_PUCHAR subject_position; /* Position at start of recursion */ +} recursion_info; + +/* A similar structure for pcre_dfa_exec(). */ + +typedef struct dfa_recursion_info { + struct dfa_recursion_info *prevrec; + int group_num; + PCRE_PUCHAR subject_position; +} dfa_recursion_info; + +/* Structure for building a chain of data for holding the values of the subject +pointer at the start of each subpattern, so as to detect when an empty string +has been matched by a subpattern - to break infinite loops; used by +pcre_exec(). */ + +typedef struct eptrblock { + struct eptrblock *epb_prev; + PCRE_PUCHAR epb_saved_eptr; +} eptrblock; + + +/* Structure for passing "static" information around between the functions +doing traditional NFA matching, so that they are thread-safe. */ + +typedef struct match_data { + unsigned long int match_call_count; /* As it says */ + unsigned long int match_limit; /* As it says */ + unsigned long int match_limit_recursion; /* As it says */ + int *offset_vector; /* Offset vector */ + int offset_end; /* One past the end */ + int offset_max; /* The maximum usable for return data */ + int nltype; /* Newline type */ + int nllen; /* Newline string length */ + int name_count; /* Number of names in name table */ + int name_entry_size; /* Size of entry in names table */ + unsigned int skip_arg_count; /* For counting SKIP_ARGs */ + unsigned int ignore_skip_arg; /* For re-run when SKIP arg name not found */ + pcre_uchar *name_table; /* Table of names */ + pcre_uchar nl[4]; /* Newline string when fixed */ + const pcre_uint8 *lcc; /* Points to lower casing table */ + const pcre_uint8 *fcc; /* Points to case-flipping table */ + const pcre_uint8 *ctypes; /* Points to table of type maps */ + BOOL notbol; /* NOTBOL flag */ + BOOL noteol; /* NOTEOL flag */ + BOOL utf; /* UTF-8 / UTF-16 flag */ + BOOL jscript_compat; /* JAVASCRIPT_COMPAT flag */ + BOOL use_ucp; /* PCRE_UCP flag */ + BOOL endonly; /* Dollar not before final \n */ + BOOL notempty; /* Empty string match not wanted */ + BOOL notempty_atstart; /* Empty string match at start not wanted */ + BOOL hitend; /* Hit the end of the subject at some point */ + BOOL bsr_anycrlf; /* \R is just any CRLF, not full Unicode */ + BOOL hasthen; /* Pattern contains (*THEN) */ + const pcre_uchar *start_code; /* For use when recursing */ + PCRE_PUCHAR start_subject; /* Start of the subject string */ + PCRE_PUCHAR end_subject; /* End of the subject string */ + PCRE_PUCHAR start_match_ptr; /* Start of matched string */ + PCRE_PUCHAR end_match_ptr; /* Subject position at end match */ + PCRE_PUCHAR start_used_ptr; /* Earliest consulted character */ + int partial; /* PARTIAL options */ + int end_offset_top; /* Highwater mark at end of match */ + pcre_int32 capture_last; /* Most recent capture number + overflow flag */ + int start_offset; /* The start offset value */ + int match_function_type; /* Set for certain special calls of MATCH() */ + eptrblock *eptrchain; /* Chain of eptrblocks for tail recursions */ + int eptrn; /* Next free eptrblock */ + recursion_info *recursive; /* Linked list of recursion data */ + void *callout_data; /* To pass back to callouts */ + const pcre_uchar *mark; /* Mark pointer to pass back on success */ + const pcre_uchar *nomatch_mark;/* Mark pointer to pass back on failure */ + const pcre_uchar *once_target; /* Where to back up to for atomic groups */ +#ifdef NO_RECURSE + void *match_frames_base; /* For remembering malloc'd frames */ +#endif +} match_data; + +/* A similar structure is used for the same purpose by the DFA matching +functions. */ + +typedef struct dfa_match_data { + const pcre_uchar *start_code; /* Start of the compiled pattern */ + const pcre_uchar *start_subject ; /* Start of the subject string */ + const pcre_uchar *end_subject; /* End of subject string */ + const pcre_uchar *start_used_ptr; /* Earliest consulted character */ + const pcre_uint8 *tables; /* Character tables */ + int start_offset; /* The start offset value */ + int moptions; /* Match options */ + int poptions; /* Pattern options */ + int nltype; /* Newline type */ + int nllen; /* Newline string length */ + pcre_uchar nl[4]; /* Newline string when fixed */ + void *callout_data; /* To pass back to callouts */ + dfa_recursion_info *recursive; /* Linked list of recursion data */ +} dfa_match_data; + +/* Bit definitions for entries in the pcre_ctypes table. */ + +#define ctype_space 0x01 +#define ctype_letter 0x02 +#define ctype_digit 0x04 +#define ctype_xdigit 0x08 +#define ctype_word 0x10 /* alphanumeric or '_' */ +#define ctype_meta 0x80 /* regexp meta char or zero (end pattern) */ + +/* Offsets for the bitmap tables in pcre_cbits. Each table contains a set +of bits for a class map. Some classes are built by combining these tables. */ + +#define cbit_space 0 /* [:space:] or \s */ +#define cbit_xdigit 32 /* [:xdigit:] */ +#define cbit_digit 64 /* [:digit:] or \d */ +#define cbit_upper 96 /* [:upper:] */ +#define cbit_lower 128 /* [:lower:] */ +#define cbit_word 160 /* [:word:] or \w */ +#define cbit_graph 192 /* [:graph:] */ +#define cbit_print 224 /* [:print:] */ +#define cbit_punct 256 /* [:punct:] */ +#define cbit_cntrl 288 /* [:cntrl:] */ +#define cbit_length 320 /* Length of the cbits table */ + +/* Offsets of the various tables from the base tables pointer, and +total length. */ + +#define lcc_offset 0 +#define fcc_offset 256 +#define cbits_offset 512 +#define ctypes_offset (cbits_offset + cbit_length) +#define tables_length (ctypes_offset + 256) + +/* Internal function and data prefixes. */ + +#if defined COMPILE_PCRE8 +#ifndef PUBL +#define PUBL(name) pcre_##name +#endif +#ifndef PRIV +#define PRIV(name) _pcre_##name +#endif +#elif defined COMPILE_PCRE16 +#ifndef PUBL +#define PUBL(name) pcre16_##name +#endif +#ifndef PRIV +#define PRIV(name) _pcre16_##name +#endif +#elif defined COMPILE_PCRE32 +#ifndef PUBL +#define PUBL(name) pcre32_##name +#endif +#ifndef PRIV +#define PRIV(name) _pcre32_##name +#endif +#else +#error Unsupported compiling mode +#endif /* COMPILE_PCRE[8|16|32] */ + +/* Layout of the UCP type table that translates property names into types and +codes. Each entry used to point directly to a name, but to reduce the number of +relocations in shared libraries, it now has an offset into a single string +instead. */ + +typedef struct { + pcre_uint16 name_offset; + pcre_uint16 type; + pcre_uint16 value; +} ucp_type_table; + + +/* Internal shared data tables. These are tables that are used by more than one +of the exported public functions. They have to be "external" in the C sense, +but are not part of the PCRE public API. The data for these tables is in the +pcre_tables.c module. */ + +#ifdef COMPILE_PCRE8 +extern const int PRIV(utf8_table1)[]; +extern const int PRIV(utf8_table1_size); +extern const int PRIV(utf8_table2)[]; +extern const int PRIV(utf8_table3)[]; +extern const pcre_uint8 PRIV(utf8_table4)[]; +#endif /* COMPILE_PCRE8 */ + +extern const char PRIV(utt_names)[]; +extern const ucp_type_table PRIV(utt)[]; +extern const int PRIV(utt_size); + +extern const pcre_uint8 PRIV(OP_lengths)[]; +extern const pcre_uint8 PRIV(default_tables)[]; + +extern const pcre_uint32 PRIV(hspace_list)[]; +extern const pcre_uint32 PRIV(vspace_list)[]; + + +/* Internal shared functions. These are functions that are used by more than +one of the exported public functions. They have to be "external" in the C +sense, but are not part of the PCRE public API. */ + +/* String comparison functions. */ +#if defined COMPILE_PCRE8 + +#define STRCMP_UC_UC(str1, str2) \ + strcmp((char *)(str1), (char *)(str2)) +#define STRCMP_UC_C8(str1, str2) \ + strcmp((char *)(str1), (str2)) +#define STRNCMP_UC_UC(str1, str2, num) \ + strncmp((char *)(str1), (char *)(str2), (num)) +#define STRNCMP_UC_C8(str1, str2, num) \ + strncmp((char *)(str1), (str2), (num)) +#define STRLEN_UC(str) strlen((const char *)str) + +#elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + +extern int PRIV(strcmp_uc_uc)(const pcre_uchar *, + const pcre_uchar *); +extern int PRIV(strcmp_uc_c8)(const pcre_uchar *, + const char *); +extern int PRIV(strncmp_uc_uc)(const pcre_uchar *, + const pcre_uchar *, unsigned int num); +extern int PRIV(strncmp_uc_c8)(const pcre_uchar *, + const char *, unsigned int num); +extern unsigned int PRIV(strlen_uc)(const pcre_uchar *str); + +#define STRCMP_UC_UC(str1, str2) \ + PRIV(strcmp_uc_uc)((str1), (str2)) +#define STRCMP_UC_C8(str1, str2) \ + PRIV(strcmp_uc_c8)((str1), (str2)) +#define STRNCMP_UC_UC(str1, str2, num) \ + PRIV(strncmp_uc_uc)((str1), (str2), (num)) +#define STRNCMP_UC_C8(str1, str2, num) \ + PRIV(strncmp_uc_c8)((str1), (str2), (num)) +#define STRLEN_UC(str) PRIV(strlen_uc)(str) + +#endif /* COMPILE_PCRE[8|16|32] */ + +#if defined COMPILE_PCRE8 || defined COMPILE_PCRE16 + +#define STRCMP_UC_UC_TEST(str1, str2) STRCMP_UC_UC(str1, str2) +#define STRCMP_UC_C8_TEST(str1, str2) STRCMP_UC_C8(str1, str2) + +#elif defined COMPILE_PCRE32 + +extern int PRIV(strcmp_uc_uc_utf)(const pcre_uchar *, + const pcre_uchar *); +extern int PRIV(strcmp_uc_c8_utf)(const pcre_uchar *, + const char *); + +#define STRCMP_UC_UC_TEST(str1, str2) \ + (utf ? PRIV(strcmp_uc_uc_utf)((str1), (str2)) : PRIV(strcmp_uc_uc)((str1), (str2))) +#define STRCMP_UC_C8_TEST(str1, str2) \ + (utf ? PRIV(strcmp_uc_c8_utf)((str1), (str2)) : PRIV(strcmp_uc_c8)((str1), (str2))) + +#endif /* COMPILE_PCRE[8|16|32] */ + +extern const pcre_uchar *PRIV(find_bracket)(const pcre_uchar *, BOOL, int); +extern BOOL PRIV(is_newline)(PCRE_PUCHAR, int, PCRE_PUCHAR, + int *, BOOL); +extern unsigned int PRIV(ord2utf)(pcre_uint32, pcre_uchar *); +extern int PRIV(valid_utf)(PCRE_PUCHAR, int, int *); +extern BOOL PRIV(was_newline)(PCRE_PUCHAR, int, PCRE_PUCHAR, + int *, BOOL); +extern BOOL PRIV(xclass)(pcre_uint32, const pcre_uchar *, BOOL); + +#ifdef SUPPORT_JIT +extern void PRIV(jit_compile)(const REAL_PCRE *, + PUBL(extra) *, int); +extern int PRIV(jit_exec)(const PUBL(extra) *, + const pcre_uchar *, int, int, int, int *, int); +extern void PRIV(jit_free)(void *); +extern int PRIV(jit_get_size)(void *); +extern const char* PRIV(jit_get_target)(void); +#endif + +/* Unicode character database (UCD) */ + +typedef struct { + pcre_uint8 script; /* ucp_Arabic, etc. */ + pcre_uint8 chartype; /* ucp_Cc, etc. (general categories) */ + pcre_uint8 gbprop; /* ucp_gbControl, etc. (grapheme break property) */ + pcre_uint8 caseset; /* offset to multichar other cases or zero */ + pcre_int32 other_case; /* offset to other case, or zero if none */ +} ucd_record; + +extern const pcre_uint32 PRIV(ucd_caseless_sets)[]; +extern const ucd_record PRIV(ucd_records)[]; +extern const pcre_uint8 PRIV(ucd_stage1)[]; +extern const pcre_uint16 PRIV(ucd_stage2)[]; +extern const pcre_uint32 PRIV(ucp_gentype)[]; +extern const pcre_uint32 PRIV(ucp_gbtable)[]; +#ifdef COMPILE_PCRE32 +extern const ucd_record PRIV(dummy_ucd_record)[]; +#endif +#ifdef SUPPORT_JIT +extern const int PRIV(ucp_typerange)[]; +#endif + +#ifdef SUPPORT_UCP +/* UCD access macros */ + +#define UCD_BLOCK_SIZE 128 +#define REAL_GET_UCD(ch) (PRIV(ucd_records) + \ + PRIV(ucd_stage2)[PRIV(ucd_stage1)[(int)(ch) / UCD_BLOCK_SIZE] * \ + UCD_BLOCK_SIZE + (int)(ch) % UCD_BLOCK_SIZE]) + +#ifdef COMPILE_PCRE32 +#define GET_UCD(ch) ((ch > 0x10ffff)? PRIV(dummy_ucd_record) : REAL_GET_UCD(ch)) +#else +#define GET_UCD(ch) REAL_GET_UCD(ch) +#endif + +#define UCD_CHARTYPE(ch) GET_UCD(ch)->chartype +#define UCD_SCRIPT(ch) GET_UCD(ch)->script +#define UCD_CATEGORY(ch) PRIV(ucp_gentype)[UCD_CHARTYPE(ch)] +#define UCD_GRAPHBREAK(ch) GET_UCD(ch)->gbprop +#define UCD_CASESET(ch) GET_UCD(ch)->caseset +#define UCD_OTHERCASE(ch) ((pcre_uint32)((int)ch + (int)(GET_UCD(ch)->other_case))) + +#endif /* SUPPORT_UCP */ + +#endif + +/* End of pcre_internal.h */ diff --git a/deps/pcre/pcre_jit_compile.c b/deps/pcre/pcre_jit_compile.c new file mode 100644 index 00000000000..4dcf8fc2189 --- /dev/null +++ b/deps/pcre/pcre_jit_compile.c @@ -0,0 +1,11913 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2013 University of Cambridge + + The machine code generator part (this module) was written by Zoltan Herczeg + Copyright (c) 2010-2013 + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pcre_internal.h" + +#if defined SUPPORT_JIT + +/* All-in-one: Since we use the JIT compiler only from here, +we just include it. This way we don't need to touch the build +system files. */ + +#define SLJIT_MALLOC(size, allocator_data) (PUBL(malloc))(size) +#define SLJIT_FREE(ptr, allocator_data) (PUBL(free))(ptr) +#define SLJIT_CONFIG_AUTO 1 +#define SLJIT_CONFIG_STATIC 1 +#define SLJIT_VERBOSE 0 +#define SLJIT_DEBUG 0 + +#include "sljit/sljitLir.c" + +#if defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED +#error Unsupported architecture +#endif + +/* Defines for debugging purposes. */ + +/* 1 - Use unoptimized capturing brackets. + 2 - Enable capture_last_ptr (includes option 1). */ +/* #define DEBUG_FORCE_UNOPTIMIZED_CBRAS 2 */ + +/* 1 - Always have a control head. */ +/* #define DEBUG_FORCE_CONTROL_HEAD 1 */ + +/* Allocate memory for the regex stack on the real machine stack. +Fast, but limited size. */ +#define MACHINE_STACK_SIZE 32768 + +/* Growth rate for stack allocated by the OS. Should be the multiply +of page size. */ +#define STACK_GROWTH_RATE 8192 + +/* Enable to check that the allocation could destroy temporaries. */ +#if defined SLJIT_DEBUG && SLJIT_DEBUG +#define DESTROY_REGISTERS 1 +#endif + +/* +Short summary about the backtracking mechanism empolyed by the jit code generator: + +The code generator follows the recursive nature of the PERL compatible regular +expressions. The basic blocks of regular expressions are condition checkers +whose execute different commands depending on the result of the condition check. +The relationship between the operators can be horizontal (concatenation) and +vertical (sub-expression) (See struct backtrack_common for more details). + + 'ab' - 'a' and 'b' regexps are concatenated + 'a+' - 'a' is the sub-expression of the '+' operator + +The condition checkers are boolean (true/false) checkers. Machine code is generated +for the checker itself and for the actions depending on the result of the checker. +The 'true' case is called as the matching path (expected path), and the other is called as +the 'backtrack' path. Branch instructions are expesive for all CPUs, so we avoid taken +branches on the matching path. + + Greedy star operator (*) : + Matching path: match happens. + Backtrack path: match failed. + Non-greedy star operator (*?) : + Matching path: no need to perform a match. + Backtrack path: match is required. + +The following example shows how the code generated for a capturing bracket +with two alternatives. Let A, B, C, D are arbirary regular expressions, and +we have the following regular expression: + + A(B|C)D + +The generated code will be the following: + + A matching path + '(' matching path (pushing arguments to the stack) + B matching path + ')' matching path (pushing arguments to the stack) + D matching path + return with successful match + + D backtrack path + ')' backtrack path (If we arrived from "C" jump to the backtrack of "C") + B backtrack path + C expected path + jump to D matching path + C backtrack path + A backtrack path + + Notice, that the order of backtrack code paths are the opposite of the fast + code paths. In this way the topmost value on the stack is always belong + to the current backtrack code path. The backtrack path must check + whether there is a next alternative. If so, it needs to jump back to + the matching path eventually. Otherwise it needs to clear out its own stack + frame and continue the execution on the backtrack code paths. +*/ + +/* +Saved stack frames: + +Atomic blocks and asserts require reloading the values of private data +when the backtrack mechanism performed. Because of OP_RECURSE, the data +are not necessarly known in compile time, thus we need a dynamic restore +mechanism. + +The stack frames are stored in a chain list, and have the following format: +([ capturing bracket offset ][ start value ][ end value ])+ ... [ 0 ] [ previous head ] + +Thus we can restore the private data to a particular point in the stack. +*/ + +typedef struct jit_arguments { + /* Pointers first. */ + struct sljit_stack *stack; + const pcre_uchar *str; + const pcre_uchar *begin; + const pcre_uchar *end; + int *offsets; + pcre_uchar *mark_ptr; + void *callout_data; + /* Everything else after. */ + sljit_u32 limit_match; + int real_offset_count; + int offset_count; + sljit_u8 notbol; + sljit_u8 noteol; + sljit_u8 notempty; + sljit_u8 notempty_atstart; +} jit_arguments; + +typedef struct executable_functions { + void *executable_funcs[JIT_NUMBER_OF_COMPILE_MODES]; + void *read_only_data_heads[JIT_NUMBER_OF_COMPILE_MODES]; + sljit_uw executable_sizes[JIT_NUMBER_OF_COMPILE_MODES]; + PUBL(jit_callback) callback; + void *userdata; + sljit_u32 top_bracket; + sljit_u32 limit_match; +} executable_functions; + +typedef struct jump_list { + struct sljit_jump *jump; + struct jump_list *next; +} jump_list; + +typedef struct stub_list { + struct sljit_jump *start; + struct sljit_label *quit; + struct stub_list *next; +} stub_list; + +typedef struct label_addr_list { + struct sljit_label *label; + sljit_uw *update_addr; + struct label_addr_list *next; +} label_addr_list; + +enum frame_types { + no_frame = -1, + no_stack = -2 +}; + +enum control_types { + type_mark = 0, + type_then_trap = 1 +}; + +typedef int (SLJIT_FUNC *jit_function)(jit_arguments *args); + +/* The following structure is the key data type for the recursive +code generator. It is allocated by compile_matchingpath, and contains +the arguments for compile_backtrackingpath. Must be the first member +of its descendants. */ +typedef struct backtrack_common { + /* Concatenation stack. */ + struct backtrack_common *prev; + jump_list *nextbacktracks; + /* Internal stack (for component operators). */ + struct backtrack_common *top; + jump_list *topbacktracks; + /* Opcode pointer. */ + pcre_uchar *cc; +} backtrack_common; + +typedef struct assert_backtrack { + backtrack_common common; + jump_list *condfailed; + /* Less than 0 if a frame is not needed. */ + int framesize; + /* Points to our private memory word on the stack. */ + int private_data_ptr; + /* For iterators. */ + struct sljit_label *matchingpath; +} assert_backtrack; + +typedef struct bracket_backtrack { + backtrack_common common; + /* Where to coninue if an alternative is successfully matched. */ + struct sljit_label *alternative_matchingpath; + /* For rmin and rmax iterators. */ + struct sljit_label *recursive_matchingpath; + /* For greedy ? operator. */ + struct sljit_label *zero_matchingpath; + /* Contains the branches of a failed condition. */ + union { + /* Both for OP_COND, OP_SCOND. */ + jump_list *condfailed; + assert_backtrack *assert; + /* For OP_ONCE. Less than 0 if not needed. */ + int framesize; + } u; + /* Points to our private memory word on the stack. */ + int private_data_ptr; +} bracket_backtrack; + +typedef struct bracketpos_backtrack { + backtrack_common common; + /* Points to our private memory word on the stack. */ + int private_data_ptr; + /* Reverting stack is needed. */ + int framesize; + /* Allocated stack size. */ + int stacksize; +} bracketpos_backtrack; + +typedef struct braminzero_backtrack { + backtrack_common common; + struct sljit_label *matchingpath; +} braminzero_backtrack; + +typedef struct char_iterator_backtrack { + backtrack_common common; + /* Next iteration. */ + struct sljit_label *matchingpath; + union { + jump_list *backtracks; + struct { + unsigned int othercasebit; + pcre_uchar chr; + BOOL enabled; + } charpos; + } u; +} char_iterator_backtrack; + +typedef struct ref_iterator_backtrack { + backtrack_common common; + /* Next iteration. */ + struct sljit_label *matchingpath; +} ref_iterator_backtrack; + +typedef struct recurse_entry { + struct recurse_entry *next; + /* Contains the function entry. */ + struct sljit_label *entry; + /* Collects the calls until the function is not created. */ + jump_list *calls; + /* Points to the starting opcode. */ + sljit_sw start; +} recurse_entry; + +typedef struct recurse_backtrack { + backtrack_common common; + BOOL inlined_pattern; +} recurse_backtrack; + +#define OP_THEN_TRAP OP_TABLE_LENGTH + +typedef struct then_trap_backtrack { + backtrack_common common; + /* If then_trap is not NULL, this structure contains the real + then_trap for the backtracking path. */ + struct then_trap_backtrack *then_trap; + /* Points to the starting opcode. */ + sljit_sw start; + /* Exit point for the then opcodes of this alternative. */ + jump_list *quit; + /* Frame size of the current alternative. */ + int framesize; +} then_trap_backtrack; + +#define MAX_RANGE_SIZE 4 + +typedef struct compiler_common { + /* The sljit ceneric compiler. */ + struct sljit_compiler *compiler; + /* First byte code. */ + pcre_uchar *start; + /* Maps private data offset to each opcode. */ + sljit_s32 *private_data_ptrs; + /* Chain list of read-only data ptrs. */ + void *read_only_data_head; + /* Tells whether the capturing bracket is optimized. */ + sljit_u8 *optimized_cbracket; + /* Tells whether the starting offset is a target of then. */ + sljit_u8 *then_offsets; + /* Current position where a THEN must jump. */ + then_trap_backtrack *then_trap; + /* Starting offset of private data for capturing brackets. */ + sljit_s32 cbra_ptr; + /* Output vector starting point. Must be divisible by 2. */ + sljit_s32 ovector_start; + /* Points to the starting character of the current match. */ + sljit_s32 start_ptr; + /* Last known position of the requested byte. */ + sljit_s32 req_char_ptr; + /* Head of the last recursion. */ + sljit_s32 recursive_head_ptr; + /* First inspected character for partial matching. + (Needed for avoiding zero length partial matches.) */ + sljit_s32 start_used_ptr; + /* Starting pointer for partial soft matches. */ + sljit_s32 hit_start; + /* Pointer of the match end position. */ + sljit_s32 match_end_ptr; + /* Points to the marked string. */ + sljit_s32 mark_ptr; + /* Recursive control verb management chain. */ + sljit_s32 control_head_ptr; + /* Points to the last matched capture block index. */ + sljit_s32 capture_last_ptr; + /* Fast forward skipping byte code pointer. */ + pcre_uchar *fast_forward_bc_ptr; + /* Locals used by fast fail optimization. */ + sljit_s32 fast_fail_start_ptr; + sljit_s32 fast_fail_end_ptr; + + /* Flipped and lower case tables. */ + const sljit_u8 *fcc; + sljit_sw lcc; + /* Mode can be PCRE_STUDY_JIT_COMPILE and others. */ + int mode; + /* TRUE, when minlength is greater than 0. */ + BOOL might_be_empty; + /* \K is found in the pattern. */ + BOOL has_set_som; + /* (*SKIP:arg) is found in the pattern. */ + BOOL has_skip_arg; + /* (*THEN) is found in the pattern. */ + BOOL has_then; + /* (*SKIP) or (*SKIP:arg) is found in lookbehind assertion. */ + BOOL has_skip_in_assert_back; + /* Currently in recurse or negative assert. */ + BOOL local_exit; + /* Currently in a positive assert. */ + BOOL positive_assert; + /* Newline control. */ + int nltype; + sljit_u32 nlmax; + sljit_u32 nlmin; + int newline; + int bsr_nltype; + sljit_u32 bsr_nlmax; + sljit_u32 bsr_nlmin; + /* Dollar endonly. */ + int endonly; + /* Tables. */ + sljit_sw ctypes; + /* Named capturing brackets. */ + pcre_uchar *name_table; + sljit_sw name_count; + sljit_sw name_entry_size; + + /* Labels and jump lists. */ + struct sljit_label *partialmatchlabel; + struct sljit_label *quit_label; + struct sljit_label *forced_quit_label; + struct sljit_label *accept_label; + struct sljit_label *ff_newline_shortcut; + stub_list *stubs; + label_addr_list *label_addrs; + recurse_entry *entries; + recurse_entry *currententry; + jump_list *partialmatch; + jump_list *quit; + jump_list *positive_assert_quit; + jump_list *forced_quit; + jump_list *accept; + jump_list *calllimit; + jump_list *stackalloc; + jump_list *revertframes; + jump_list *wordboundary; + jump_list *anynewline; + jump_list *hspace; + jump_list *vspace; + jump_list *casefulcmp; + jump_list *caselesscmp; + jump_list *reset_match; + BOOL jscript_compat; +#ifdef SUPPORT_UTF + BOOL utf; +#ifdef SUPPORT_UCP + BOOL use_ucp; + jump_list *getucd; +#endif +#ifdef COMPILE_PCRE8 + jump_list *utfreadchar; + jump_list *utfreadchar16; + jump_list *utfreadtype8; +#endif +#endif /* SUPPORT_UTF */ +} compiler_common; + +/* For byte_sequence_compare. */ + +typedef struct compare_context { + int length; + int sourcereg; +#if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED + int ucharptr; + union { + sljit_s32 asint; + sljit_u16 asushort; +#if defined COMPILE_PCRE8 + sljit_u8 asbyte; + sljit_u8 asuchars[4]; +#elif defined COMPILE_PCRE16 + sljit_u16 asuchars[2]; +#elif defined COMPILE_PCRE32 + sljit_u32 asuchars[1]; +#endif + } c; + union { + sljit_s32 asint; + sljit_u16 asushort; +#if defined COMPILE_PCRE8 + sljit_u8 asbyte; + sljit_u8 asuchars[4]; +#elif defined COMPILE_PCRE16 + sljit_u16 asuchars[2]; +#elif defined COMPILE_PCRE32 + sljit_u32 asuchars[1]; +#endif + } oc; +#endif +} compare_context; + +/* Undefine sljit macros. */ +#undef CMP + +/* Used for accessing the elements of the stack. */ +#define STACK(i) ((i) * (int)sizeof(sljit_sw)) + +#ifdef SLJIT_PREF_SHIFT_REG +#if SLJIT_PREF_SHIFT_REG == SLJIT_R2 +/* Nothing. */ +#elif SLJIT_PREF_SHIFT_REG == SLJIT_R3 +#define SHIFT_REG_IS_R3 +#else +#error "Unsupported shift register" +#endif +#endif + +#define TMP1 SLJIT_R0 +#ifdef SHIFT_REG_IS_R3 +#define TMP2 SLJIT_R3 +#define TMP3 SLJIT_R2 +#else +#define TMP2 SLJIT_R2 +#define TMP3 SLJIT_R3 +#endif +#define STR_PTR SLJIT_S0 +#define STR_END SLJIT_S1 +#define STACK_TOP SLJIT_R1 +#define STACK_LIMIT SLJIT_S2 +#define COUNT_MATCH SLJIT_S3 +#define ARGUMENTS SLJIT_S4 +#define RETURN_ADDR SLJIT_R4 + +/* Local space layout. */ +/* These two locals can be used by the current opcode. */ +#define LOCALS0 (0 * sizeof(sljit_sw)) +#define LOCALS1 (1 * sizeof(sljit_sw)) +/* Two local variables for possessive quantifiers (char1 cannot use them). */ +#define POSSESSIVE0 (2 * sizeof(sljit_sw)) +#define POSSESSIVE1 (3 * sizeof(sljit_sw)) +/* Max limit of recursions. */ +#define LIMIT_MATCH (4 * sizeof(sljit_sw)) +/* The output vector is stored on the stack, and contains pointers +to characters. The vector data is divided into two groups: the first +group contains the start / end character pointers, and the second is +the start pointers when the end of the capturing group has not yet reached. */ +#define OVECTOR_START (common->ovector_start) +#define OVECTOR(i) (OVECTOR_START + (i) * (sljit_sw)sizeof(sljit_sw)) +#define OVECTOR_PRIV(i) (common->cbra_ptr + (i) * (sljit_sw)sizeof(sljit_sw)) +#define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start]) + +#if defined COMPILE_PCRE8 +#define MOV_UCHAR SLJIT_MOV_U8 +#elif defined COMPILE_PCRE16 +#define MOV_UCHAR SLJIT_MOV_U16 +#elif defined COMPILE_PCRE32 +#define MOV_UCHAR SLJIT_MOV_U32 +#else +#error Unsupported compiling mode +#endif + +/* Shortcuts. */ +#define DEFINE_COMPILER \ + struct sljit_compiler *compiler = common->compiler +#define OP1(op, dst, dstw, src, srcw) \ + sljit_emit_op1(compiler, (op), (dst), (dstw), (src), (srcw)) +#define OP2(op, dst, dstw, src1, src1w, src2, src2w) \ + sljit_emit_op2(compiler, (op), (dst), (dstw), (src1), (src1w), (src2), (src2w)) +#define LABEL() \ + sljit_emit_label(compiler) +#define JUMP(type) \ + sljit_emit_jump(compiler, (type)) +#define JUMPTO(type, label) \ + sljit_set_label(sljit_emit_jump(compiler, (type)), (label)) +#define JUMPHERE(jump) \ + sljit_set_label((jump), sljit_emit_label(compiler)) +#define SET_LABEL(jump, label) \ + sljit_set_label((jump), (label)) +#define CMP(type, src1, src1w, src2, src2w) \ + sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w)) +#define CMPTO(type, src1, src1w, src2, src2w, label) \ + sljit_set_label(sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w)), (label)) +#define OP_FLAGS(op, dst, dstw, type) \ + sljit_emit_op_flags(compiler, (op), (dst), (dstw), (type)) +#define GET_LOCAL_BASE(dst, dstw, offset) \ + sljit_get_local_base(compiler, (dst), (dstw), (offset)) + +#define READ_CHAR_MAX 0x7fffffff + +#define INVALID_UTF_CHAR 888 + +static pcre_uchar *bracketend(pcre_uchar *cc) +{ +SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND)); +do cc += GET(cc, 1); while (*cc == OP_ALT); +SLJIT_ASSERT(*cc >= OP_KET && *cc <= OP_KETRPOS); +cc += 1 + LINK_SIZE; +return cc; +} + +static int no_alternatives(pcre_uchar *cc) +{ +int count = 0; +SLJIT_ASSERT((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND)); +do + { + cc += GET(cc, 1); + count++; + } +while (*cc == OP_ALT); +SLJIT_ASSERT(*cc >= OP_KET && *cc <= OP_KETRPOS); +return count; +} + +/* Functions whose might need modification for all new supported opcodes: + next_opcode + check_opcode_types + set_private_data_ptrs + get_framesize + init_frame + get_private_data_copy_length + copy_private_data + compile_matchingpath + compile_backtrackingpath +*/ + +static pcre_uchar *next_opcode(compiler_common *common, pcre_uchar *cc) +{ +SLJIT_UNUSED_ARG(common); +switch(*cc) + { + case OP_SOD: + case OP_SOM: + case OP_SET_SOM: + case OP_NOT_WORD_BOUNDARY: + case OP_WORD_BOUNDARY: + case OP_NOT_DIGIT: + case OP_DIGIT: + case OP_NOT_WHITESPACE: + case OP_WHITESPACE: + case OP_NOT_WORDCHAR: + case OP_WORDCHAR: + case OP_ANY: + case OP_ALLANY: + case OP_NOTPROP: + case OP_PROP: + case OP_ANYNL: + case OP_NOT_HSPACE: + case OP_HSPACE: + case OP_NOT_VSPACE: + case OP_VSPACE: + case OP_EXTUNI: + case OP_EODN: + case OP_EOD: + case OP_CIRC: + case OP_CIRCM: + case OP_DOLL: + case OP_DOLLM: + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRPLUS: + case OP_CRMINPLUS: + case OP_CRQUERY: + case OP_CRMINQUERY: + case OP_CRRANGE: + case OP_CRMINRANGE: + case OP_CRPOSSTAR: + case OP_CRPOSPLUS: + case OP_CRPOSQUERY: + case OP_CRPOSRANGE: + case OP_CLASS: + case OP_NCLASS: + case OP_REF: + case OP_REFI: + case OP_DNREF: + case OP_DNREFI: + case OP_RECURSE: + case OP_CALLOUT: + case OP_ALT: + case OP_KET: + case OP_KETRMAX: + case OP_KETRMIN: + case OP_KETRPOS: + case OP_REVERSE: + case OP_ASSERT: + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + case OP_ONCE: + case OP_ONCE_NC: + case OP_BRA: + case OP_BRAPOS: + case OP_CBRA: + case OP_CBRAPOS: + case OP_COND: + case OP_SBRA: + case OP_SBRAPOS: + case OP_SCBRA: + case OP_SCBRAPOS: + case OP_SCOND: + case OP_CREF: + case OP_DNCREF: + case OP_RREF: + case OP_DNRREF: + case OP_DEF: + case OP_BRAZERO: + case OP_BRAMINZERO: + case OP_BRAPOSZERO: + case OP_PRUNE: + case OP_SKIP: + case OP_THEN: + case OP_COMMIT: + case OP_FAIL: + case OP_ACCEPT: + case OP_ASSERT_ACCEPT: + case OP_CLOSE: + case OP_SKIPZERO: + return cc + PRIV(OP_lengths)[*cc]; + + case OP_CHAR: + case OP_CHARI: + case OP_NOT: + case OP_NOTI: + case OP_STAR: + case OP_MINSTAR: + case OP_PLUS: + case OP_MINPLUS: + case OP_QUERY: + case OP_MINQUERY: + case OP_UPTO: + case OP_MINUPTO: + case OP_EXACT: + case OP_POSSTAR: + case OP_POSPLUS: + case OP_POSQUERY: + case OP_POSUPTO: + case OP_STARI: + case OP_MINSTARI: + case OP_PLUSI: + case OP_MINPLUSI: + case OP_QUERYI: + case OP_MINQUERYI: + case OP_UPTOI: + case OP_MINUPTOI: + case OP_EXACTI: + case OP_POSSTARI: + case OP_POSPLUSI: + case OP_POSQUERYI: + case OP_POSUPTOI: + case OP_NOTSTAR: + case OP_NOTMINSTAR: + case OP_NOTPLUS: + case OP_NOTMINPLUS: + case OP_NOTQUERY: + case OP_NOTMINQUERY: + case OP_NOTUPTO: + case OP_NOTMINUPTO: + case OP_NOTEXACT: + case OP_NOTPOSSTAR: + case OP_NOTPOSPLUS: + case OP_NOTPOSQUERY: + case OP_NOTPOSUPTO: + case OP_NOTSTARI: + case OP_NOTMINSTARI: + case OP_NOTPLUSI: + case OP_NOTMINPLUSI: + case OP_NOTQUERYI: + case OP_NOTMINQUERYI: + case OP_NOTUPTOI: + case OP_NOTMINUPTOI: + case OP_NOTEXACTI: + case OP_NOTPOSSTARI: + case OP_NOTPOSPLUSI: + case OP_NOTPOSQUERYI: + case OP_NOTPOSUPTOI: + cc += PRIV(OP_lengths)[*cc]; +#ifdef SUPPORT_UTF + if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); +#endif + return cc; + + /* Special cases. */ + case OP_TYPESTAR: + case OP_TYPEMINSTAR: + case OP_TYPEPLUS: + case OP_TYPEMINPLUS: + case OP_TYPEQUERY: + case OP_TYPEMINQUERY: + case OP_TYPEUPTO: + case OP_TYPEMINUPTO: + case OP_TYPEEXACT: + case OP_TYPEPOSSTAR: + case OP_TYPEPOSPLUS: + case OP_TYPEPOSQUERY: + case OP_TYPEPOSUPTO: + return cc + PRIV(OP_lengths)[*cc] - 1; + + case OP_ANYBYTE: +#ifdef SUPPORT_UTF + if (common->utf) return NULL; +#endif + return cc + 1; + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: + return cc + GET(cc, 1); +#endif + + case OP_MARK: + case OP_PRUNE_ARG: + case OP_SKIP_ARG: + case OP_THEN_ARG: + return cc + 1 + 2 + cc[1]; + + default: + /* All opcodes are supported now! */ + SLJIT_UNREACHABLE(); + return NULL; + } +} + +static BOOL check_opcode_types(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend) +{ +int count; +pcre_uchar *slot; +pcre_uchar *assert_back_end = cc - 1; + +/* Calculate important variables (like stack size) and checks whether all opcodes are supported. */ +while (cc < ccend) + { + switch(*cc) + { + case OP_SET_SOM: + common->has_set_som = TRUE; + common->might_be_empty = TRUE; + cc += 1; + break; + + case OP_REF: + case OP_REFI: + common->optimized_cbracket[GET2(cc, 1)] = 0; + cc += 1 + IMM2_SIZE; + break; + + case OP_CBRAPOS: + case OP_SCBRAPOS: + common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0; + cc += 1 + LINK_SIZE + IMM2_SIZE; + break; + + case OP_COND: + case OP_SCOND: + /* Only AUTO_CALLOUT can insert this opcode. We do + not intend to support this case. */ + if (cc[1 + LINK_SIZE] == OP_CALLOUT) + return FALSE; + cc += 1 + LINK_SIZE; + break; + + case OP_CREF: + common->optimized_cbracket[GET2(cc, 1)] = 0; + cc += 1 + IMM2_SIZE; + break; + + case OP_DNREF: + case OP_DNREFI: + case OP_DNCREF: + count = GET2(cc, 1 + IMM2_SIZE); + slot = common->name_table + GET2(cc, 1) * common->name_entry_size; + while (count-- > 0) + { + common->optimized_cbracket[GET2(slot, 0)] = 0; + slot += common->name_entry_size; + } + cc += 1 + 2 * IMM2_SIZE; + break; + + case OP_RECURSE: + /* Set its value only once. */ + if (common->recursive_head_ptr == 0) + { + common->recursive_head_ptr = common->ovector_start; + common->ovector_start += sizeof(sljit_sw); + } + cc += 1 + LINK_SIZE; + break; + + case OP_CALLOUT: + if (common->capture_last_ptr == 0) + { + common->capture_last_ptr = common->ovector_start; + common->ovector_start += sizeof(sljit_sw); + } + cc += 2 + 2 * LINK_SIZE; + break; + + case OP_ASSERTBACK: + slot = bracketend(cc); + if (slot > assert_back_end) + assert_back_end = slot; + cc += 1 + LINK_SIZE; + break; + + case OP_THEN_ARG: + common->has_then = TRUE; + common->control_head_ptr = 1; + /* Fall through. */ + + case OP_PRUNE_ARG: + case OP_MARK: + if (common->mark_ptr == 0) + { + common->mark_ptr = common->ovector_start; + common->ovector_start += sizeof(sljit_sw); + } + cc += 1 + 2 + cc[1]; + break; + + case OP_THEN: + common->has_then = TRUE; + common->control_head_ptr = 1; + cc += 1; + break; + + case OP_SKIP: + if (cc < assert_back_end) + common->has_skip_in_assert_back = TRUE; + cc += 1; + break; + + case OP_SKIP_ARG: + common->control_head_ptr = 1; + common->has_skip_arg = TRUE; + if (cc < assert_back_end) + common->has_skip_in_assert_back = TRUE; + cc += 1 + 2 + cc[1]; + break; + + default: + cc = next_opcode(common, cc); + if (cc == NULL) + return FALSE; + break; + } + } +return TRUE; +} + +static BOOL is_accelerated_repeat(pcre_uchar *cc) +{ +switch(*cc) + { + case OP_TYPESTAR: + case OP_TYPEMINSTAR: + case OP_TYPEPLUS: + case OP_TYPEMINPLUS: + case OP_TYPEPOSSTAR: + case OP_TYPEPOSPLUS: + return (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI); + + case OP_STAR: + case OP_MINSTAR: + case OP_PLUS: + case OP_MINPLUS: + case OP_POSSTAR: + case OP_POSPLUS: + + case OP_STARI: + case OP_MINSTARI: + case OP_PLUSI: + case OP_MINPLUSI: + case OP_POSSTARI: + case OP_POSPLUSI: + + case OP_NOTSTAR: + case OP_NOTMINSTAR: + case OP_NOTPLUS: + case OP_NOTMINPLUS: + case OP_NOTPOSSTAR: + case OP_NOTPOSPLUS: + + case OP_NOTSTARI: + case OP_NOTMINSTARI: + case OP_NOTPLUSI: + case OP_NOTMINPLUSI: + case OP_NOTPOSSTARI: + case OP_NOTPOSPLUSI: + return TRUE; + + case OP_CLASS: + case OP_NCLASS: +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: + cc += (*cc == OP_XCLASS) ? GET(cc, 1) : (int)(1 + (32 / sizeof(pcre_uchar))); +#else + cc += (1 + (32 / sizeof(pcre_uchar))); +#endif + + switch(*cc) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRPLUS: + case OP_CRMINPLUS: + case OP_CRPOSSTAR: + case OP_CRPOSPLUS: + return TRUE; + } + break; + } +return FALSE; +} + +static SLJIT_INLINE BOOL detect_fast_forward_skip(compiler_common *common, int *private_data_start) +{ +pcre_uchar *cc = common->start; +pcre_uchar *end; + +/* Skip not repeated brackets. */ +while (TRUE) + { + switch(*cc) + { + case OP_SOD: + case OP_SOM: + case OP_SET_SOM: + case OP_NOT_WORD_BOUNDARY: + case OP_WORD_BOUNDARY: + case OP_EODN: + case OP_EOD: + case OP_CIRC: + case OP_CIRCM: + case OP_DOLL: + case OP_DOLLM: + /* Zero width assertions. */ + cc++; + continue; + } + + if (*cc != OP_BRA && *cc != OP_CBRA) + break; + + end = cc + GET(cc, 1); + if (*end != OP_KET || PRIVATE_DATA(end) != 0) + return FALSE; + if (*cc == OP_CBRA) + { + if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0) + return FALSE; + cc += IMM2_SIZE; + } + cc += 1 + LINK_SIZE; + } + +if (is_accelerated_repeat(cc)) + { + common->fast_forward_bc_ptr = cc; + common->private_data_ptrs[(cc + 1) - common->start] = *private_data_start; + *private_data_start += sizeof(sljit_sw); + return TRUE; + } +return FALSE; +} + +static SLJIT_INLINE void detect_fast_fail(compiler_common *common, pcre_uchar *cc, int *private_data_start, sljit_s32 depth) +{ + pcre_uchar *next_alt; + + SLJIT_ASSERT(*cc == OP_BRA || *cc == OP_CBRA); + + if (*cc == OP_CBRA && common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0) + return; + + next_alt = bracketend(cc) - (1 + LINK_SIZE); + if (*next_alt != OP_KET || PRIVATE_DATA(next_alt) != 0) + return; + + do + { + next_alt = cc + GET(cc, 1); + + cc += 1 + LINK_SIZE + ((*cc == OP_CBRA) ? IMM2_SIZE : 0); + + while (TRUE) + { + switch(*cc) + { + case OP_SOD: + case OP_SOM: + case OP_SET_SOM: + case OP_NOT_WORD_BOUNDARY: + case OP_WORD_BOUNDARY: + case OP_EODN: + case OP_EOD: + case OP_CIRC: + case OP_CIRCM: + case OP_DOLL: + case OP_DOLLM: + /* Zero width assertions. */ + cc++; + continue; + } + break; + } + + if (depth > 0 && (*cc == OP_BRA || *cc == OP_CBRA)) + detect_fast_fail(common, cc, private_data_start, depth - 1); + + if (is_accelerated_repeat(cc)) + { + common->private_data_ptrs[(cc + 1) - common->start] = *private_data_start; + + if (common->fast_fail_start_ptr == 0) + common->fast_fail_start_ptr = *private_data_start; + + *private_data_start += sizeof(sljit_sw); + common->fast_fail_end_ptr = *private_data_start; + + if (*private_data_start > SLJIT_MAX_LOCAL_SIZE) + return; + } + + cc = next_alt; + } + while (*cc == OP_ALT); +} + +static int get_class_iterator_size(pcre_uchar *cc) +{ +sljit_u32 min; +sljit_u32 max; +switch(*cc) + { + case OP_CRSTAR: + case OP_CRPLUS: + return 2; + + case OP_CRMINSTAR: + case OP_CRMINPLUS: + case OP_CRQUERY: + case OP_CRMINQUERY: + return 1; + + case OP_CRRANGE: + case OP_CRMINRANGE: + min = GET2(cc, 1); + max = GET2(cc, 1 + IMM2_SIZE); + if (max == 0) + return (*cc == OP_CRRANGE) ? 2 : 1; + max -= min; + if (max > 2) + max = 2; + return max; + + default: + return 0; + } +} + +static BOOL detect_repeat(compiler_common *common, pcre_uchar *begin) +{ +pcre_uchar *end = bracketend(begin); +pcre_uchar *next; +pcre_uchar *next_end; +pcre_uchar *max_end; +pcre_uchar type; +sljit_sw length = end - begin; +int min, max, i; + +/* Detect fixed iterations first. */ +if (end[-(1 + LINK_SIZE)] != OP_KET) + return FALSE; + +/* Already detected repeat. */ +if (common->private_data_ptrs[end - common->start - LINK_SIZE] != 0) + return TRUE; + +next = end; +min = 1; +while (1) + { + if (*next != *begin) + break; + next_end = bracketend(next); + if (next_end - next != length || memcmp(begin, next, IN_UCHARS(length)) != 0) + break; + next = next_end; + min++; + } + +if (min == 2) + return FALSE; + +max = 0; +max_end = next; +if (*next == OP_BRAZERO || *next == OP_BRAMINZERO) + { + type = *next; + while (1) + { + if (next[0] != type || next[1] != OP_BRA || next[2 + LINK_SIZE] != *begin) + break; + next_end = bracketend(next + 2 + LINK_SIZE); + if (next_end - next != (length + 2 + LINK_SIZE) || memcmp(begin, next + 2 + LINK_SIZE, IN_UCHARS(length)) != 0) + break; + next = next_end; + max++; + } + + if (next[0] == type && next[1] == *begin && max >= 1) + { + next_end = bracketend(next + 1); + if (next_end - next == (length + 1) && memcmp(begin, next + 1, IN_UCHARS(length)) == 0) + { + for (i = 0; i < max; i++, next_end += 1 + LINK_SIZE) + if (*next_end != OP_KET) + break; + + if (i == max) + { + common->private_data_ptrs[max_end - common->start - LINK_SIZE] = next_end - max_end; + common->private_data_ptrs[max_end - common->start - LINK_SIZE + 1] = (type == OP_BRAZERO) ? OP_UPTO : OP_MINUPTO; + /* +2 the original and the last. */ + common->private_data_ptrs[max_end - common->start - LINK_SIZE + 2] = max + 2; + if (min == 1) + return TRUE; + min--; + max_end -= (1 + LINK_SIZE) + GET(max_end, -LINK_SIZE); + } + } + } + } + +if (min >= 3) + { + common->private_data_ptrs[end - common->start - LINK_SIZE] = max_end - end; + common->private_data_ptrs[end - common->start - LINK_SIZE + 1] = OP_EXACT; + common->private_data_ptrs[end - common->start - LINK_SIZE + 2] = min; + return TRUE; + } + +return FALSE; +} + +#define CASE_ITERATOR_PRIVATE_DATA_1 \ + case OP_MINSTAR: \ + case OP_MINPLUS: \ + case OP_QUERY: \ + case OP_MINQUERY: \ + case OP_MINSTARI: \ + case OP_MINPLUSI: \ + case OP_QUERYI: \ + case OP_MINQUERYI: \ + case OP_NOTMINSTAR: \ + case OP_NOTMINPLUS: \ + case OP_NOTQUERY: \ + case OP_NOTMINQUERY: \ + case OP_NOTMINSTARI: \ + case OP_NOTMINPLUSI: \ + case OP_NOTQUERYI: \ + case OP_NOTMINQUERYI: + +#define CASE_ITERATOR_PRIVATE_DATA_2A \ + case OP_STAR: \ + case OP_PLUS: \ + case OP_STARI: \ + case OP_PLUSI: \ + case OP_NOTSTAR: \ + case OP_NOTPLUS: \ + case OP_NOTSTARI: \ + case OP_NOTPLUSI: + +#define CASE_ITERATOR_PRIVATE_DATA_2B \ + case OP_UPTO: \ + case OP_MINUPTO: \ + case OP_UPTOI: \ + case OP_MINUPTOI: \ + case OP_NOTUPTO: \ + case OP_NOTMINUPTO: \ + case OP_NOTUPTOI: \ + case OP_NOTMINUPTOI: + +#define CASE_ITERATOR_TYPE_PRIVATE_DATA_1 \ + case OP_TYPEMINSTAR: \ + case OP_TYPEMINPLUS: \ + case OP_TYPEQUERY: \ + case OP_TYPEMINQUERY: + +#define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \ + case OP_TYPESTAR: \ + case OP_TYPEPLUS: + +#define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \ + case OP_TYPEUPTO: \ + case OP_TYPEMINUPTO: + +static void set_private_data_ptrs(compiler_common *common, int *private_data_start, pcre_uchar *ccend) +{ +pcre_uchar *cc = common->start; +pcre_uchar *alternative; +pcre_uchar *end = NULL; +int private_data_ptr = *private_data_start; +int space, size, bracketlen; +BOOL repeat_check = TRUE; + +while (cc < ccend) + { + space = 0; + size = 0; + bracketlen = 0; + if (private_data_ptr > SLJIT_MAX_LOCAL_SIZE) + break; + + if (repeat_check && (*cc == OP_ONCE || *cc == OP_ONCE_NC || *cc == OP_BRA || *cc == OP_CBRA || *cc == OP_COND)) + { + if (detect_repeat(common, cc)) + { + /* These brackets are converted to repeats, so no global + based single character repeat is allowed. */ + if (cc >= end) + end = bracketend(cc); + } + } + repeat_check = TRUE; + + switch(*cc) + { + case OP_KET: + if (common->private_data_ptrs[cc + 1 - common->start] != 0) + { + common->private_data_ptrs[cc - common->start] = private_data_ptr; + private_data_ptr += sizeof(sljit_sw); + cc += common->private_data_ptrs[cc + 1 - common->start]; + } + cc += 1 + LINK_SIZE; + break; + + case OP_ASSERT: + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + case OP_ONCE: + case OP_ONCE_NC: + case OP_BRAPOS: + case OP_SBRA: + case OP_SBRAPOS: + case OP_SCOND: + common->private_data_ptrs[cc - common->start] = private_data_ptr; + private_data_ptr += sizeof(sljit_sw); + bracketlen = 1 + LINK_SIZE; + break; + + case OP_CBRAPOS: + case OP_SCBRAPOS: + common->private_data_ptrs[cc - common->start] = private_data_ptr; + private_data_ptr += sizeof(sljit_sw); + bracketlen = 1 + LINK_SIZE + IMM2_SIZE; + break; + + case OP_COND: + /* Might be a hidden SCOND. */ + alternative = cc + GET(cc, 1); + if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN) + { + common->private_data_ptrs[cc - common->start] = private_data_ptr; + private_data_ptr += sizeof(sljit_sw); + } + bracketlen = 1 + LINK_SIZE; + break; + + case OP_BRA: + bracketlen = 1 + LINK_SIZE; + break; + + case OP_CBRA: + case OP_SCBRA: + bracketlen = 1 + LINK_SIZE + IMM2_SIZE; + break; + + case OP_BRAZERO: + case OP_BRAMINZERO: + case OP_BRAPOSZERO: + repeat_check = FALSE; + size = 1; + break; + + CASE_ITERATOR_PRIVATE_DATA_1 + space = 1; + size = -2; + break; + + CASE_ITERATOR_PRIVATE_DATA_2A + space = 2; + size = -2; + break; + + CASE_ITERATOR_PRIVATE_DATA_2B + space = 2; + size = -(2 + IMM2_SIZE); + break; + + CASE_ITERATOR_TYPE_PRIVATE_DATA_1 + space = 1; + size = 1; + break; + + CASE_ITERATOR_TYPE_PRIVATE_DATA_2A + if (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI) + space = 2; + size = 1; + break; + + case OP_TYPEUPTO: + if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI) + space = 2; + size = 1 + IMM2_SIZE; + break; + + case OP_TYPEMINUPTO: + space = 2; + size = 1 + IMM2_SIZE; + break; + + case OP_CLASS: + case OP_NCLASS: + space = get_class_iterator_size(cc + size); + size = 1 + 32 / sizeof(pcre_uchar); + break; + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: + space = get_class_iterator_size(cc + size); + size = GET(cc, 1); + break; +#endif + + default: + cc = next_opcode(common, cc); + SLJIT_ASSERT(cc != NULL); + break; + } + + /* Character iterators, which are not inside a repeated bracket, + gets a private slot instead of allocating it on the stack. */ + if (space > 0 && cc >= end) + { + common->private_data_ptrs[cc - common->start] = private_data_ptr; + private_data_ptr += sizeof(sljit_sw) * space; + } + + if (size != 0) + { + if (size < 0) + { + cc += -size; +#ifdef SUPPORT_UTF + if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); +#endif + } + else + cc += size; + } + + if (bracketlen > 0) + { + if (cc >= end) + { + end = bracketend(cc); + if (end[-1 - LINK_SIZE] == OP_KET) + end = NULL; + } + cc += bracketlen; + } + } +*private_data_start = private_data_ptr; +} + +/* Returns with a frame_types (always < 0) if no need for frame. */ +static int get_framesize(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, BOOL recursive, BOOL *needs_control_head) +{ +int length = 0; +int possessive = 0; +BOOL stack_restore = FALSE; +BOOL setsom_found = recursive; +BOOL setmark_found = recursive; +/* The last capture is a local variable even for recursions. */ +BOOL capture_last_found = FALSE; + +#if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD +SLJIT_ASSERT(common->control_head_ptr != 0); +*needs_control_head = TRUE; +#else +*needs_control_head = FALSE; +#endif + +if (ccend == NULL) + { + ccend = bracketend(cc) - (1 + LINK_SIZE); + if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS)) + { + possessive = length = (common->capture_last_ptr != 0) ? 5 : 3; + /* This is correct regardless of common->capture_last_ptr. */ + capture_last_found = TRUE; + } + cc = next_opcode(common, cc); + } + +SLJIT_ASSERT(cc != NULL); +while (cc < ccend) + switch(*cc) + { + case OP_SET_SOM: + SLJIT_ASSERT(common->has_set_som); + stack_restore = TRUE; + if (!setsom_found) + { + length += 2; + setsom_found = TRUE; + } + cc += 1; + break; + + case OP_MARK: + case OP_PRUNE_ARG: + case OP_THEN_ARG: + SLJIT_ASSERT(common->mark_ptr != 0); + stack_restore = TRUE; + if (!setmark_found) + { + length += 2; + setmark_found = TRUE; + } + if (common->control_head_ptr != 0) + *needs_control_head = TRUE; + cc += 1 + 2 + cc[1]; + break; + + case OP_RECURSE: + stack_restore = TRUE; + if (common->has_set_som && !setsom_found) + { + length += 2; + setsom_found = TRUE; + } + if (common->mark_ptr != 0 && !setmark_found) + { + length += 2; + setmark_found = TRUE; + } + if (common->capture_last_ptr != 0 && !capture_last_found) + { + length += 2; + capture_last_found = TRUE; + } + cc += 1 + LINK_SIZE; + break; + + case OP_CBRA: + case OP_CBRAPOS: + case OP_SCBRA: + case OP_SCBRAPOS: + stack_restore = TRUE; + if (common->capture_last_ptr != 0 && !capture_last_found) + { + length += 2; + capture_last_found = TRUE; + } + length += 3; + cc += 1 + LINK_SIZE + IMM2_SIZE; + break; + + case OP_THEN: + stack_restore = TRUE; + if (common->control_head_ptr != 0) + *needs_control_head = TRUE; + cc ++; + break; + + default: + stack_restore = TRUE; + /* Fall through. */ + + case OP_NOT_WORD_BOUNDARY: + case OP_WORD_BOUNDARY: + case OP_NOT_DIGIT: + case OP_DIGIT: + case OP_NOT_WHITESPACE: + case OP_WHITESPACE: + case OP_NOT_WORDCHAR: + case OP_WORDCHAR: + case OP_ANY: + case OP_ALLANY: + case OP_ANYBYTE: + case OP_NOTPROP: + case OP_PROP: + case OP_ANYNL: + case OP_NOT_HSPACE: + case OP_HSPACE: + case OP_NOT_VSPACE: + case OP_VSPACE: + case OP_EXTUNI: + case OP_EODN: + case OP_EOD: + case OP_CIRC: + case OP_CIRCM: + case OP_DOLL: + case OP_DOLLM: + case OP_CHAR: + case OP_CHARI: + case OP_NOT: + case OP_NOTI: + + case OP_EXACT: + case OP_POSSTAR: + case OP_POSPLUS: + case OP_POSQUERY: + case OP_POSUPTO: + + case OP_EXACTI: + case OP_POSSTARI: + case OP_POSPLUSI: + case OP_POSQUERYI: + case OP_POSUPTOI: + + case OP_NOTEXACT: + case OP_NOTPOSSTAR: + case OP_NOTPOSPLUS: + case OP_NOTPOSQUERY: + case OP_NOTPOSUPTO: + + case OP_NOTEXACTI: + case OP_NOTPOSSTARI: + case OP_NOTPOSPLUSI: + case OP_NOTPOSQUERYI: + case OP_NOTPOSUPTOI: + + case OP_TYPEEXACT: + case OP_TYPEPOSSTAR: + case OP_TYPEPOSPLUS: + case OP_TYPEPOSQUERY: + case OP_TYPEPOSUPTO: + + case OP_CLASS: + case OP_NCLASS: + case OP_XCLASS: + case OP_CALLOUT: + + cc = next_opcode(common, cc); + SLJIT_ASSERT(cc != NULL); + break; + } + +/* Possessive quantifiers can use a special case. */ +if (SLJIT_UNLIKELY(possessive == length)) + return stack_restore ? no_frame : no_stack; + +if (length > 0) + return length + 1; +return stack_restore ? no_frame : no_stack; +} + +static void init_frame(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, int stackpos, int stacktop, BOOL recursive) +{ +DEFINE_COMPILER; +BOOL setsom_found = recursive; +BOOL setmark_found = recursive; +/* The last capture is a local variable even for recursions. */ +BOOL capture_last_found = FALSE; +int offset; + +/* >= 1 + shortest item size (2) */ +SLJIT_UNUSED_ARG(stacktop); +SLJIT_ASSERT(stackpos >= stacktop + 2); + +stackpos = STACK(stackpos); +if (ccend == NULL) + { + ccend = bracketend(cc) - (1 + LINK_SIZE); + if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS)) + cc = next_opcode(common, cc); + } + +SLJIT_ASSERT(cc != NULL); +while (cc < ccend) + switch(*cc) + { + case OP_SET_SOM: + SLJIT_ASSERT(common->has_set_som); + if (!setsom_found) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0)); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0)); + stackpos -= (int)sizeof(sljit_sw); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0); + stackpos -= (int)sizeof(sljit_sw); + setsom_found = TRUE; + } + cc += 1; + break; + + case OP_MARK: + case OP_PRUNE_ARG: + case OP_THEN_ARG: + SLJIT_ASSERT(common->mark_ptr != 0); + if (!setmark_found) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr); + stackpos -= (int)sizeof(sljit_sw); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0); + stackpos -= (int)sizeof(sljit_sw); + setmark_found = TRUE; + } + cc += 1 + 2 + cc[1]; + break; + + case OP_RECURSE: + if (common->has_set_som && !setsom_found) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0)); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0)); + stackpos -= (int)sizeof(sljit_sw); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0); + stackpos -= (int)sizeof(sljit_sw); + setsom_found = TRUE; + } + if (common->mark_ptr != 0 && !setmark_found) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr); + stackpos -= (int)sizeof(sljit_sw); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0); + stackpos -= (int)sizeof(sljit_sw); + setmark_found = TRUE; + } + if (common->capture_last_ptr != 0 && !capture_last_found) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr); + stackpos -= (int)sizeof(sljit_sw); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0); + stackpos -= (int)sizeof(sljit_sw); + capture_last_found = TRUE; + } + cc += 1 + LINK_SIZE; + break; + + case OP_CBRA: + case OP_CBRAPOS: + case OP_SCBRA: + case OP_SCBRAPOS: + if (common->capture_last_ptr != 0 && !capture_last_found) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr); + stackpos -= (int)sizeof(sljit_sw); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0); + stackpos -= (int)sizeof(sljit_sw); + capture_last_found = TRUE; + } + offset = (GET2(cc, 1 + LINK_SIZE)) << 1; + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, OVECTOR(offset)); + stackpos -= (int)sizeof(sljit_sw); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset)); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1)); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0); + stackpos -= (int)sizeof(sljit_sw); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP2, 0); + stackpos -= (int)sizeof(sljit_sw); + + cc += 1 + LINK_SIZE + IMM2_SIZE; + break; + + default: + cc = next_opcode(common, cc); + SLJIT_ASSERT(cc != NULL); + break; + } + +OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, 0); +SLJIT_ASSERT(stackpos == STACK(stacktop)); +} + +static SLJIT_INLINE int get_private_data_copy_length(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, BOOL needs_control_head) +{ +int private_data_length = needs_control_head ? 3 : 2; +int size; +pcre_uchar *alternative; +/* Calculate the sum of the private machine words. */ +while (cc < ccend) + { + size = 0; + switch(*cc) + { + case OP_KET: + if (PRIVATE_DATA(cc) != 0) + { + private_data_length++; + SLJIT_ASSERT(PRIVATE_DATA(cc + 1) != 0); + cc += PRIVATE_DATA(cc + 1); + } + cc += 1 + LINK_SIZE; + break; + + case OP_ASSERT: + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + case OP_ONCE: + case OP_ONCE_NC: + case OP_BRAPOS: + case OP_SBRA: + case OP_SBRAPOS: + case OP_SCOND: + private_data_length++; + SLJIT_ASSERT(PRIVATE_DATA(cc) != 0); + cc += 1 + LINK_SIZE; + break; + + case OP_CBRA: + case OP_SCBRA: + if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0) + private_data_length++; + cc += 1 + LINK_SIZE + IMM2_SIZE; + break; + + case OP_CBRAPOS: + case OP_SCBRAPOS: + private_data_length += 2; + cc += 1 + LINK_SIZE + IMM2_SIZE; + break; + + case OP_COND: + /* Might be a hidden SCOND. */ + alternative = cc + GET(cc, 1); + if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN) + private_data_length++; + cc += 1 + LINK_SIZE; + break; + + CASE_ITERATOR_PRIVATE_DATA_1 + if (PRIVATE_DATA(cc)) + private_data_length++; + cc += 2; +#ifdef SUPPORT_UTF + if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); +#endif + break; + + CASE_ITERATOR_PRIVATE_DATA_2A + if (PRIVATE_DATA(cc)) + private_data_length += 2; + cc += 2; +#ifdef SUPPORT_UTF + if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); +#endif + break; + + CASE_ITERATOR_PRIVATE_DATA_2B + if (PRIVATE_DATA(cc)) + private_data_length += 2; + cc += 2 + IMM2_SIZE; +#ifdef SUPPORT_UTF + if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); +#endif + break; + + CASE_ITERATOR_TYPE_PRIVATE_DATA_1 + if (PRIVATE_DATA(cc)) + private_data_length++; + cc += 1; + break; + + CASE_ITERATOR_TYPE_PRIVATE_DATA_2A + if (PRIVATE_DATA(cc)) + private_data_length += 2; + cc += 1; + break; + + CASE_ITERATOR_TYPE_PRIVATE_DATA_2B + if (PRIVATE_DATA(cc)) + private_data_length += 2; + cc += 1 + IMM2_SIZE; + break; + + case OP_CLASS: + case OP_NCLASS: +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: + size = (*cc == OP_XCLASS) ? GET(cc, 1) : 1 + 32 / (int)sizeof(pcre_uchar); +#else + size = 1 + 32 / (int)sizeof(pcre_uchar); +#endif + if (PRIVATE_DATA(cc)) + private_data_length += get_class_iterator_size(cc + size); + cc += size; + break; + + default: + cc = next_opcode(common, cc); + SLJIT_ASSERT(cc != NULL); + break; + } + } +SLJIT_ASSERT(cc == ccend); +return private_data_length; +} + +static void copy_private_data(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, + BOOL save, int stackptr, int stacktop, BOOL needs_control_head) +{ +DEFINE_COMPILER; +int srcw[2]; +int count, size; +BOOL tmp1next = TRUE; +BOOL tmp1empty = TRUE; +BOOL tmp2empty = TRUE; +pcre_uchar *alternative; +enum { + loop, + end +} status; + +status = loop; +stackptr = STACK(stackptr); +stacktop = STACK(stacktop - 1); + +if (!save) + { + stacktop -= (needs_control_head ? 2 : 1) * sizeof(sljit_sw); + if (stackptr < stacktop) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr); + stackptr += sizeof(sljit_sw); + tmp1empty = FALSE; + } + if (stackptr < stacktop) + { + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr); + stackptr += sizeof(sljit_sw); + tmp2empty = FALSE; + } + /* The tmp1next must be TRUE in either way. */ + } + +SLJIT_ASSERT(common->recursive_head_ptr != 0); + +do + { + count = 0; + if (cc >= ccend) + { + if (!save) + break; + + count = 1; + srcw[0] = common->recursive_head_ptr; + if (needs_control_head) + { + SLJIT_ASSERT(common->control_head_ptr != 0); + count = 2; + srcw[0] = common->control_head_ptr; + srcw[1] = common->recursive_head_ptr; + } + status = end; + } + else switch(*cc) + { + case OP_KET: + if (PRIVATE_DATA(cc) != 0) + { + count = 1; + srcw[0] = PRIVATE_DATA(cc); + SLJIT_ASSERT(PRIVATE_DATA(cc + 1) != 0); + cc += PRIVATE_DATA(cc + 1); + } + cc += 1 + LINK_SIZE; + break; + + case OP_ASSERT: + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + case OP_ONCE: + case OP_ONCE_NC: + case OP_BRAPOS: + case OP_SBRA: + case OP_SBRAPOS: + case OP_SCOND: + count = 1; + srcw[0] = PRIVATE_DATA(cc); + SLJIT_ASSERT(srcw[0] != 0); + cc += 1 + LINK_SIZE; + break; + + case OP_CBRA: + case OP_SCBRA: + if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0) + { + count = 1; + srcw[0] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE)); + } + cc += 1 + LINK_SIZE + IMM2_SIZE; + break; + + case OP_CBRAPOS: + case OP_SCBRAPOS: + count = 2; + srcw[0] = PRIVATE_DATA(cc); + srcw[1] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE)); + SLJIT_ASSERT(srcw[0] != 0 && srcw[1] != 0); + cc += 1 + LINK_SIZE + IMM2_SIZE; + break; + + case OP_COND: + /* Might be a hidden SCOND. */ + alternative = cc + GET(cc, 1); + if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN) + { + count = 1; + srcw[0] = PRIVATE_DATA(cc); + SLJIT_ASSERT(srcw[0] != 0); + } + cc += 1 + LINK_SIZE; + break; + + CASE_ITERATOR_PRIVATE_DATA_1 + if (PRIVATE_DATA(cc)) + { + count = 1; + srcw[0] = PRIVATE_DATA(cc); + } + cc += 2; +#ifdef SUPPORT_UTF + if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); +#endif + break; + + CASE_ITERATOR_PRIVATE_DATA_2A + if (PRIVATE_DATA(cc)) + { + count = 2; + srcw[0] = PRIVATE_DATA(cc); + srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw); + } + cc += 2; +#ifdef SUPPORT_UTF + if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); +#endif + break; + + CASE_ITERATOR_PRIVATE_DATA_2B + if (PRIVATE_DATA(cc)) + { + count = 2; + srcw[0] = PRIVATE_DATA(cc); + srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw); + } + cc += 2 + IMM2_SIZE; +#ifdef SUPPORT_UTF + if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); +#endif + break; + + CASE_ITERATOR_TYPE_PRIVATE_DATA_1 + if (PRIVATE_DATA(cc)) + { + count = 1; + srcw[0] = PRIVATE_DATA(cc); + } + cc += 1; + break; + + CASE_ITERATOR_TYPE_PRIVATE_DATA_2A + if (PRIVATE_DATA(cc)) + { + count = 2; + srcw[0] = PRIVATE_DATA(cc); + srcw[1] = srcw[0] + sizeof(sljit_sw); + } + cc += 1; + break; + + CASE_ITERATOR_TYPE_PRIVATE_DATA_2B + if (PRIVATE_DATA(cc)) + { + count = 2; + srcw[0] = PRIVATE_DATA(cc); + srcw[1] = srcw[0] + sizeof(sljit_sw); + } + cc += 1 + IMM2_SIZE; + break; + + case OP_CLASS: + case OP_NCLASS: +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: + size = (*cc == OP_XCLASS) ? GET(cc, 1) : 1 + 32 / (int)sizeof(pcre_uchar); +#else + size = 1 + 32 / (int)sizeof(pcre_uchar); +#endif + if (PRIVATE_DATA(cc)) + switch(get_class_iterator_size(cc + size)) + { + case 1: + count = 1; + srcw[0] = PRIVATE_DATA(cc); + break; + + case 2: + count = 2; + srcw[0] = PRIVATE_DATA(cc); + srcw[1] = srcw[0] + sizeof(sljit_sw); + break; + + default: + SLJIT_UNREACHABLE(); + break; + } + cc += size; + break; + + default: + cc = next_opcode(common, cc); + SLJIT_ASSERT(cc != NULL); + break; + } + + while (count > 0) + { + count--; + if (save) + { + if (tmp1next) + { + if (!tmp1empty) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0); + stackptr += sizeof(sljit_sw); + } + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), srcw[count]); + tmp1empty = FALSE; + tmp1next = FALSE; + } + else + { + if (!tmp2empty) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0); + stackptr += sizeof(sljit_sw); + } + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), srcw[count]); + tmp2empty = FALSE; + tmp1next = TRUE; + } + } + else + { + if (tmp1next) + { + SLJIT_ASSERT(!tmp1empty); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), srcw[count], TMP1, 0); + tmp1empty = stackptr >= stacktop; + if (!tmp1empty) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr); + stackptr += sizeof(sljit_sw); + } + tmp1next = FALSE; + } + else + { + SLJIT_ASSERT(!tmp2empty); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), srcw[count], TMP2, 0); + tmp2empty = stackptr >= stacktop; + if (!tmp2empty) + { + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr); + stackptr += sizeof(sljit_sw); + } + tmp1next = TRUE; + } + } + } + } +while (status != end); + +if (save) + { + if (tmp1next) + { + if (!tmp1empty) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0); + stackptr += sizeof(sljit_sw); + } + if (!tmp2empty) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0); + stackptr += sizeof(sljit_sw); + } + } + else + { + if (!tmp2empty) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0); + stackptr += sizeof(sljit_sw); + } + if (!tmp1empty) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0); + stackptr += sizeof(sljit_sw); + } + } + } +SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty))); +} + +static SLJIT_INLINE pcre_uchar *set_then_offsets(compiler_common *common, pcre_uchar *cc, sljit_u8 *current_offset) +{ +pcre_uchar *end = bracketend(cc); +BOOL has_alternatives = cc[GET(cc, 1)] == OP_ALT; + +/* Assert captures then. */ +if (*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) + current_offset = NULL; +/* Conditional block does not. */ +if (*cc == OP_COND || *cc == OP_SCOND) + has_alternatives = FALSE; + +cc = next_opcode(common, cc); +if (has_alternatives) + current_offset = common->then_offsets + (cc - common->start); + +while (cc < end) + { + if ((*cc >= OP_ASSERT && *cc <= OP_ASSERTBACK_NOT) || (*cc >= OP_ONCE && *cc <= OP_SCOND)) + cc = set_then_offsets(common, cc, current_offset); + else + { + if (*cc == OP_ALT && has_alternatives) + current_offset = common->then_offsets + (cc + 1 + LINK_SIZE - common->start); + if (*cc >= OP_THEN && *cc <= OP_THEN_ARG && current_offset != NULL) + *current_offset = 1; + cc = next_opcode(common, cc); + } + } + +return end; +} + +#undef CASE_ITERATOR_PRIVATE_DATA_1 +#undef CASE_ITERATOR_PRIVATE_DATA_2A +#undef CASE_ITERATOR_PRIVATE_DATA_2B +#undef CASE_ITERATOR_TYPE_PRIVATE_DATA_1 +#undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2A +#undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2B + +static SLJIT_INLINE BOOL is_powerof2(unsigned int value) +{ +return (value & (value - 1)) == 0; +} + +static SLJIT_INLINE void set_jumps(jump_list *list, struct sljit_label *label) +{ +while (list) + { + /* sljit_set_label is clever enough to do nothing + if either the jump or the label is NULL. */ + SET_LABEL(list->jump, label); + list = list->next; + } +} + +static SLJIT_INLINE void add_jump(struct sljit_compiler *compiler, jump_list **list, struct sljit_jump *jump) +{ +jump_list *list_item = sljit_alloc_memory(compiler, sizeof(jump_list)); +if (list_item) + { + list_item->next = *list; + list_item->jump = jump; + *list = list_item; + } +} + +static void add_stub(compiler_common *common, struct sljit_jump *start) +{ +DEFINE_COMPILER; +stub_list *list_item = sljit_alloc_memory(compiler, sizeof(stub_list)); + +if (list_item) + { + list_item->start = start; + list_item->quit = LABEL(); + list_item->next = common->stubs; + common->stubs = list_item; + } +} + +static void flush_stubs(compiler_common *common) +{ +DEFINE_COMPILER; +stub_list *list_item = common->stubs; + +while (list_item) + { + JUMPHERE(list_item->start); + add_jump(compiler, &common->stackalloc, JUMP(SLJIT_FAST_CALL)); + JUMPTO(SLJIT_JUMP, list_item->quit); + list_item = list_item->next; + } +common->stubs = NULL; +} + +static void add_label_addr(compiler_common *common, sljit_uw *update_addr) +{ +DEFINE_COMPILER; +label_addr_list *label_addr; + +label_addr = sljit_alloc_memory(compiler, sizeof(label_addr_list)); +if (label_addr == NULL) + return; +label_addr->label = LABEL(); +label_addr->update_addr = update_addr; +label_addr->next = common->label_addrs; +common->label_addrs = label_addr; +} + +static SLJIT_INLINE void count_match(compiler_common *common) +{ +DEFINE_COMPILER; + +OP2(SLJIT_SUB | SLJIT_SET_Z, COUNT_MATCH, 0, COUNT_MATCH, 0, SLJIT_IMM, 1); +add_jump(compiler, &common->calllimit, JUMP(SLJIT_ZERO)); +} + +static SLJIT_INLINE void allocate_stack(compiler_common *common, int size) +{ +/* May destroy all locals and registers except TMP2. */ +DEFINE_COMPILER; + +SLJIT_ASSERT(size > 0); +OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw)); +#ifdef DESTROY_REGISTERS +OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 12345); +OP1(SLJIT_MOV, TMP3, 0, TMP1, 0); +OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0); +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, TMP1, 0); +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP1, 0); +#endif +add_stub(common, CMP(SLJIT_LESS, STACK_TOP, 0, STACK_LIMIT, 0)); +} + +static SLJIT_INLINE void free_stack(compiler_common *common, int size) +{ +DEFINE_COMPILER; + +SLJIT_ASSERT(size > 0); +OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw)); +} + +static sljit_uw * allocate_read_only_data(compiler_common *common, sljit_uw size) +{ +DEFINE_COMPILER; +sljit_uw *result; + +if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + return NULL; + +result = (sljit_uw *)SLJIT_MALLOC(size + sizeof(sljit_uw), compiler->allocator_data); +if (SLJIT_UNLIKELY(result == NULL)) + { + sljit_set_compiler_memory_error(compiler); + return NULL; + } + +*(void**)result = common->read_only_data_head; +common->read_only_data_head = (void *)result; +return result + 1; +} + +static void free_read_only_data(void *current, void *allocator_data) +{ +void *next; + +SLJIT_UNUSED_ARG(allocator_data); + +while (current != NULL) + { + next = *(void**)current; + SLJIT_FREE(current, allocator_data); + current = next; + } +} + +static SLJIT_INLINE void reset_ovector(compiler_common *common, int length) +{ +DEFINE_COMPILER; +struct sljit_label *loop; +int i; + +/* At this point we can freely use all temporary registers. */ +SLJIT_ASSERT(length > 1); +/* TMP1 returns with begin - 1. */ +OP2(SLJIT_SUB, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_S0), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, IN_UCHARS(1)); +if (length < 8) + { + for (i = 1; i < length; i++) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(i), SLJIT_R0, 0); + } +else + { + if (sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_SUPP | SLJIT_MEM_STORE | SLJIT_MEM_PRE, SLJIT_R0, SLJIT_MEM1(SLJIT_R1), sizeof(sljit_sw)) == SLJIT_SUCCESS) + { + GET_LOCAL_BASE(SLJIT_R1, 0, OVECTOR_START); + OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_IMM, length - 1); + loop = LABEL(); + sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_STORE | SLJIT_MEM_PRE, SLJIT_R0, SLJIT_MEM1(SLJIT_R1), sizeof(sljit_sw)); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, loop); + } + else + { + GET_LOCAL_BASE(SLJIT_R1, 0, OVECTOR_START + sizeof(sljit_sw)); + OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_IMM, length - 1); + loop = LABEL(); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R1), 0, SLJIT_R0, 0); + OP2(SLJIT_ADD, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, sizeof(sljit_sw)); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, loop); + } + } +} + +static SLJIT_INLINE void reset_fast_fail(compiler_common *common) +{ +DEFINE_COMPILER; +sljit_s32 i; + +SLJIT_ASSERT(common->fast_fail_start_ptr < common->fast_fail_end_ptr); + +OP2(SLJIT_SUB, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); +for (i = common->fast_fail_start_ptr; i < common->fast_fail_end_ptr; i += sizeof(sljit_sw)) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), i, TMP1, 0); +} + +static SLJIT_INLINE void do_reset_match(compiler_common *common, int length) +{ +DEFINE_COMPILER; +struct sljit_label *loop; +int i; + +SLJIT_ASSERT(length > 1); +/* OVECTOR(1) contains the "string begin - 1" constant. */ +if (length > 2) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1)); +if (length < 8) + { + for (i = 2; i < length; i++) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(i), TMP1, 0); + } +else + { + if (sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_SUPP | SLJIT_MEM_STORE | SLJIT_MEM_PRE, TMP1, SLJIT_MEM1(TMP2), sizeof(sljit_sw)) == SLJIT_SUCCESS) + { + GET_LOCAL_BASE(TMP2, 0, OVECTOR_START + sizeof(sljit_sw)); + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, length - 2); + loop = LABEL(); + sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_STORE | SLJIT_MEM_PRE, TMP1, SLJIT_MEM1(TMP2), sizeof(sljit_sw)); + OP2(SLJIT_SUB | SLJIT_SET_Z, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, loop); + } + else + { + GET_LOCAL_BASE(TMP2, 0, OVECTOR_START + 2 * sizeof(sljit_sw)); + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, length - 2); + loop = LABEL(); + OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, TMP1, 0); + OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, sizeof(sljit_sw)); + OP2(SLJIT_SUB | SLJIT_SET_Z, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, loop); + } + } + +OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0); +if (common->mark_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, SLJIT_IMM, 0); +if (common->control_head_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0); +OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack)); +OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_ptr); +OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, end)); +} + +static sljit_sw SLJIT_FUNC do_search_mark(sljit_sw *current, const pcre_uchar *skip_arg) +{ +while (current != NULL) + { + switch (current[1]) + { + case type_then_trap: + break; + + case type_mark: + if (STRCMP_UC_UC(skip_arg, (pcre_uchar *)current[2]) == 0) + return current[3]; + break; + + default: + SLJIT_UNREACHABLE(); + break; + } + SLJIT_ASSERT(current[0] == 0 || current < (sljit_sw*)current[0]); + current = (sljit_sw*)current[0]; + } +return 0; +} + +static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket) +{ +DEFINE_COMPILER; +struct sljit_label *loop; +struct sljit_jump *early_quit; +BOOL has_pre; + +/* At this point we can freely use all registers. */ +OP1(SLJIT_MOV, SLJIT_S2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1)); +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(1), STR_PTR, 0); + +OP1(SLJIT_MOV, SLJIT_R0, 0, ARGUMENTS, 0); +if (common->mark_ptr != 0) + OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr); +OP1(SLJIT_MOV_S32, SLJIT_R1, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, offset_count)); +if (common->mark_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, mark_ptr), SLJIT_R2, 0); +OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, offsets), SLJIT_IMM, sizeof(int)); +OP1(SLJIT_MOV, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, begin)); + +has_pre = sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_SUPP | SLJIT_MEM_PRE, SLJIT_S1, SLJIT_MEM1(SLJIT_S0), sizeof(sljit_sw)) == SLJIT_SUCCESS; +GET_LOCAL_BASE(SLJIT_S0, 0, OVECTOR_START - (has_pre ? sizeof(sljit_sw) : 0)); + +/* Unlikely, but possible */ +early_quit = CMP(SLJIT_EQUAL, SLJIT_R1, 0, SLJIT_IMM, 0); +loop = LABEL(); + +if (has_pre) + sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_PRE, SLJIT_S1, SLJIT_MEM1(SLJIT_S0), sizeof(sljit_sw)); +else + { + OP1(SLJIT_MOV, SLJIT_S1, 0, SLJIT_MEM1(SLJIT_S0), 0); + OP2(SLJIT_ADD, SLJIT_S0, 0, SLJIT_S0, 0, SLJIT_IMM, sizeof(sljit_sw)); + } + +OP2(SLJIT_ADD, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, sizeof(int)); +OP2(SLJIT_SUB, SLJIT_S1, 0, SLJIT_S1, 0, SLJIT_R0, 0); +/* Copy the integer value to the output buffer */ +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 +OP2(SLJIT_ASHR, SLJIT_S1, 0, SLJIT_S1, 0, SLJIT_IMM, UCHAR_SHIFT); +#endif + +OP1(SLJIT_MOV_S32, SLJIT_MEM1(SLJIT_R2), 0, SLJIT_S1, 0); +OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1); +JUMPTO(SLJIT_NOT_ZERO, loop); +JUMPHERE(early_quit); + +/* Calculate the return value, which is the maximum ovector value. */ +if (topbracket > 1) + { + if (sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_SUPP | SLJIT_MEM_PRE, SLJIT_R2, SLJIT_MEM1(SLJIT_R0), -(2 * (sljit_sw)sizeof(sljit_sw))) == SLJIT_SUCCESS) + { + GET_LOCAL_BASE(SLJIT_R0, 0, OVECTOR_START + topbracket * 2 * sizeof(sljit_sw)); + OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_IMM, topbracket + 1); + + /* OVECTOR(0) is never equal to SLJIT_S2. */ + loop = LABEL(); + sljit_emit_mem(compiler, SLJIT_MOV | SLJIT_MEM_PRE, SLJIT_R2, SLJIT_MEM1(SLJIT_R0), -(2 * (sljit_sw)sizeof(sljit_sw))); + OP2(SLJIT_SUB, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1); + CMPTO(SLJIT_EQUAL, SLJIT_R2, 0, SLJIT_S2, 0, loop); + } + else + { + GET_LOCAL_BASE(SLJIT_R0, 0, OVECTOR_START + (topbracket - 1) * 2 * sizeof(sljit_sw)); + OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_IMM, topbracket + 1); + + /* OVECTOR(0) is never equal to SLJIT_S2. */ + loop = LABEL(); + OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R0), 0); + OP2(SLJIT_SUB, SLJIT_R0, 0, SLJIT_R0, 0, SLJIT_IMM, 2 * (sljit_sw)sizeof(sljit_sw)); + OP2(SLJIT_SUB, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1); + CMPTO(SLJIT_EQUAL, SLJIT_R2, 0, SLJIT_S2, 0, loop); + } + OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_R1, 0); + } +else + OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1); +} + +static SLJIT_INLINE void return_with_partial_match(compiler_common *common, struct sljit_label *quit) +{ +DEFINE_COMPILER; +struct sljit_jump *jump; + +SLJIT_COMPILE_ASSERT(STR_END == SLJIT_S1, str_end_must_be_saved_reg2); +SLJIT_ASSERT(common->start_used_ptr != 0 && common->start_ptr != 0 + && (common->mode == JIT_PARTIAL_SOFT_COMPILE ? common->hit_start != 0 : common->hit_start == 0)); + +OP1(SLJIT_MOV, SLJIT_R1, 0, ARGUMENTS, 0); +OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_PARTIAL); +OP1(SLJIT_MOV_S32, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, real_offset_count)); +CMPTO(SLJIT_SIG_LESS, SLJIT_R2, 0, SLJIT_IMM, 2, quit); + +/* Store match begin and end. */ +OP1(SLJIT_MOV, SLJIT_S0, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, begin)); +OP1(SLJIT_MOV, SLJIT_R1, 0, SLJIT_MEM1(SLJIT_R1), SLJIT_OFFSETOF(jit_arguments, offsets)); + +jump = CMP(SLJIT_SIG_LESS, SLJIT_R2, 0, SLJIT_IMM, 3); +OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_ptr : (common->hit_start + (int)sizeof(sljit_sw)), SLJIT_S0, 0); +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 +OP2(SLJIT_ASHR, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, UCHAR_SHIFT); +#endif +OP1(SLJIT_MOV_S32, SLJIT_MEM1(SLJIT_R1), 2 * sizeof(int), SLJIT_R2, 0); +JUMPHERE(jump); + +OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_used_ptr : common->hit_start); +OP2(SLJIT_SUB, SLJIT_S1, 0, STR_END, 0, SLJIT_S0, 0); +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 +OP2(SLJIT_ASHR, SLJIT_S1, 0, SLJIT_S1, 0, SLJIT_IMM, UCHAR_SHIFT); +#endif +OP1(SLJIT_MOV_S32, SLJIT_MEM1(SLJIT_R1), sizeof(int), SLJIT_S1, 0); + +OP2(SLJIT_SUB, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_S0, 0); +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 +OP2(SLJIT_ASHR, SLJIT_R2, 0, SLJIT_R2, 0, SLJIT_IMM, UCHAR_SHIFT); +#endif +OP1(SLJIT_MOV_S32, SLJIT_MEM1(SLJIT_R1), 0, SLJIT_R2, 0); + +JUMPTO(SLJIT_JUMP, quit); +} + +static SLJIT_INLINE void check_start_used_ptr(compiler_common *common) +{ +/* May destroy TMP1. */ +DEFINE_COMPILER; +struct sljit_jump *jump; + +if (common->mode == JIT_PARTIAL_SOFT_COMPILE) + { + /* The value of -1 must be kept for start_used_ptr! */ + OP2(SLJIT_ADD, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, 1); + /* Jumps if start_used_ptr < STR_PTR, or start_used_ptr == -1. Although overwriting + is not necessary if start_used_ptr == STR_PTR, it does not hurt as well. */ + jump = CMP(SLJIT_LESS_EQUAL, TMP1, 0, STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0); + JUMPHERE(jump); + } +else if (common->mode == JIT_PARTIAL_HARD_COMPILE) + { + jump = CMP(SLJIT_LESS_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0); + JUMPHERE(jump); + } +} + +static SLJIT_INLINE BOOL char_has_othercase(compiler_common *common, pcre_uchar *cc) +{ +/* Detects if the character has an othercase. */ +unsigned int c; + +#ifdef SUPPORT_UTF +if (common->utf) + { + GETCHAR(c, cc); + if (c > 127) + { +#ifdef SUPPORT_UCP + return c != UCD_OTHERCASE(c); +#else + return FALSE; +#endif + } +#ifndef COMPILE_PCRE8 + return common->fcc[c] != c; +#endif + } +else +#endif + c = *cc; +return MAX_255(c) ? common->fcc[c] != c : FALSE; +} + +static SLJIT_INLINE unsigned int char_othercase(compiler_common *common, unsigned int c) +{ +/* Returns with the othercase. */ +#ifdef SUPPORT_UTF +if (common->utf && c > 127) + { +#ifdef SUPPORT_UCP + return UCD_OTHERCASE(c); +#else + return c; +#endif + } +#endif +return TABLE_GET(c, common->fcc, c); +} + +static unsigned int char_get_othercase_bit(compiler_common *common, pcre_uchar *cc) +{ +/* Detects if the character and its othercase has only 1 bit difference. */ +unsigned int c, oc, bit; +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 +int n; +#endif + +#ifdef SUPPORT_UTF +if (common->utf) + { + GETCHAR(c, cc); + if (c <= 127) + oc = common->fcc[c]; + else + { +#ifdef SUPPORT_UCP + oc = UCD_OTHERCASE(c); +#else + oc = c; +#endif + } + } +else + { + c = *cc; + oc = TABLE_GET(c, common->fcc, c); + } +#else +c = *cc; +oc = TABLE_GET(c, common->fcc, c); +#endif + +SLJIT_ASSERT(c != oc); + +bit = c ^ oc; +/* Optimized for English alphabet. */ +if (c <= 127 && bit == 0x20) + return (0 << 8) | 0x20; + +/* Since c != oc, they must have at least 1 bit difference. */ +if (!is_powerof2(bit)) + return 0; + +#if defined COMPILE_PCRE8 + +#ifdef SUPPORT_UTF +if (common->utf && c > 127) + { + n = GET_EXTRALEN(*cc); + while ((bit & 0x3f) == 0) + { + n--; + bit >>= 6; + } + return (n << 8) | bit; + } +#endif /* SUPPORT_UTF */ +return (0 << 8) | bit; + +#elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + +#ifdef SUPPORT_UTF +if (common->utf && c > 65535) + { + if (bit >= (1 << 10)) + bit >>= 10; + else + return (bit < 256) ? ((2 << 8) | bit) : ((3 << 8) | (bit >> 8)); + } +#endif /* SUPPORT_UTF */ +return (bit < 256) ? ((0 << 8) | bit) : ((1 << 8) | (bit >> 8)); + +#endif /* COMPILE_PCRE[8|16|32] */ +} + +static void check_partial(compiler_common *common, BOOL force) +{ +/* Checks whether a partial matching is occurred. Does not modify registers. */ +DEFINE_COMPILER; +struct sljit_jump *jump = NULL; + +SLJIT_ASSERT(!force || common->mode != JIT_COMPILE); + +if (common->mode == JIT_COMPILE) + return; + +if (!force) + jump = CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0); +else if (common->mode == JIT_PARTIAL_SOFT_COMPILE) + jump = CMP(SLJIT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, -1); + +if (common->mode == JIT_PARTIAL_SOFT_COMPILE) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0); +else + { + if (common->partialmatchlabel != NULL) + JUMPTO(SLJIT_JUMP, common->partialmatchlabel); + else + add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP)); + } + +if (jump != NULL) + JUMPHERE(jump); +} + +static void check_str_end(compiler_common *common, jump_list **end_reached) +{ +/* Does not affect registers. Usually used in a tight spot. */ +DEFINE_COMPILER; +struct sljit_jump *jump; + +if (common->mode == JIT_COMPILE) + { + add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0)); + return; + } + +jump = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0); +if (common->mode == JIT_PARTIAL_SOFT_COMPILE) + { + add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0); + add_jump(compiler, end_reached, JUMP(SLJIT_JUMP)); + } +else + { + add_jump(compiler, end_reached, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0)); + if (common->partialmatchlabel != NULL) + JUMPTO(SLJIT_JUMP, common->partialmatchlabel); + else + add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP)); + } +JUMPHERE(jump); +} + +static void detect_partial_match(compiler_common *common, jump_list **backtracks) +{ +DEFINE_COMPILER; +struct sljit_jump *jump; + +if (common->mode == JIT_COMPILE) + { + add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0)); + return; + } + +/* Partial matching mode. */ +jump = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0); +add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0)); +if (common->mode == JIT_PARTIAL_SOFT_COMPILE) + { + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0); + add_jump(compiler, backtracks, JUMP(SLJIT_JUMP)); + } +else + { + if (common->partialmatchlabel != NULL) + JUMPTO(SLJIT_JUMP, common->partialmatchlabel); + else + add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP)); + } +JUMPHERE(jump); +} + +static void peek_char(compiler_common *common, sljit_u32 max) +{ +/* Reads the character into TMP1, keeps STR_PTR. +Does not check STR_END. TMP2 Destroyed. */ +DEFINE_COMPILER; +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 +struct sljit_jump *jump; +#endif + +SLJIT_UNUSED_ARG(max); + +OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0); +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 +if (common->utf) + { + if (max < 128) return; + + jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL)); + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0); + JUMPHERE(jump); + } +#endif /* SUPPORT_UTF && !COMPILE_PCRE32 */ + +#if defined SUPPORT_UTF && defined COMPILE_PCRE16 +if (common->utf) + { + if (max < 0xd800) return; + + OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800); + jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1); + /* TMP2 contains the high surrogate. */ + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); + OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40); + OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10); + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff); + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); + JUMPHERE(jump); + } +#endif +} + +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 + +static BOOL is_char7_bitset(const sljit_u8 *bitset, BOOL nclass) +{ +/* Tells whether the character codes below 128 are enough +to determine a match. */ +const sljit_u8 value = nclass ? 0xff : 0; +const sljit_u8 *end = bitset + 32; + +bitset += 16; +do + { + if (*bitset++ != value) + return FALSE; + } +while (bitset < end); +return TRUE; +} + +static void read_char7_type(compiler_common *common, BOOL full_read) +{ +/* Reads the precise character type of a character into TMP1, if the character +is less than 128. Otherwise it returns with zero. Does not check STR_END. The +full_read argument tells whether characters above max are accepted or not. */ +DEFINE_COMPILER; +struct sljit_jump *jump; + +SLJIT_ASSERT(common->utf); + +OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0); +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + +OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes); + +if (full_read) + { + jump = CMP(SLJIT_LESS, TMP2, 0, SLJIT_IMM, 0xc0); + OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0); + JUMPHERE(jump); + } +} + +#endif /* SUPPORT_UTF && COMPILE_PCRE8 */ + +static void read_char_range(compiler_common *common, sljit_u32 min, sljit_u32 max, BOOL update_str_ptr) +{ +/* Reads the precise value of a character into TMP1, if the character is +between min and max (c >= min && c <= max). Otherwise it returns with a value +outside the range. Does not check STR_END. */ +DEFINE_COMPILER; +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 +struct sljit_jump *jump; +#endif +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 +struct sljit_jump *jump2; +#endif + +SLJIT_UNUSED_ARG(update_str_ptr); +SLJIT_UNUSED_ARG(min); +SLJIT_UNUSED_ARG(max); +SLJIT_ASSERT(min <= max); + +OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 +if (common->utf) + { + if (max < 128 && !update_str_ptr) return; + + jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0); + if (min >= 0x10000) + { + OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xf0); + if (update_str_ptr) + OP1(SLJIT_MOV_U8, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); + jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0x7); + OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6); + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f); + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); + OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1)); + OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6); + OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f); + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); + OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2)); + if (!update_str_ptr) + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3)); + OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6); + OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f); + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); + JUMPHERE(jump2); + if (update_str_ptr) + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0); + } + else if (min >= 0x800 && max <= 0xffff) + { + OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xe0); + if (update_str_ptr) + OP1(SLJIT_MOV_U8, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); + jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xf); + OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6); + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f); + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); + OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1)); + if (!update_str_ptr) + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2)); + OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6); + OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f); + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); + JUMPHERE(jump2); + if (update_str_ptr) + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0); + } + else if (max >= 0x800) + add_jump(compiler, (max < 0x10000) ? &common->utfreadchar16 : &common->utfreadchar, JUMP(SLJIT_FAST_CALL)); + else if (max < 128) + { + OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0); + } + else + { + OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); + if (!update_str_ptr) + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + else + OP1(SLJIT_MOV_U8, RETURN_ADDR, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0); + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f); + OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6); + OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f); + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); + if (update_str_ptr) + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, RETURN_ADDR, 0); + } + JUMPHERE(jump); + } +#endif + +#if defined SUPPORT_UTF && defined COMPILE_PCRE16 +if (common->utf) + { + if (max >= 0x10000) + { + OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800); + jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1); + /* TMP2 contains the high surrogate. */ + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); + OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x40); + OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 10); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff); + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); + JUMPHERE(jump); + return; + } + + if (max < 0xd800 && !update_str_ptr) return; + + /* Skip low surrogate if necessary. */ + OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, 0xd800); + jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1); + if (update_str_ptr) + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + if (max >= 0xd800) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0x10000); + JUMPHERE(jump); + } +#endif +} + +static SLJIT_INLINE void read_char(compiler_common *common) +{ +read_char_range(common, 0, READ_CHAR_MAX, TRUE); +} + +static void read_char8_type(compiler_common *common, BOOL update_str_ptr) +{ +/* Reads the character type into TMP1, updates STR_PTR. Does not check STR_END. */ +DEFINE_COMPILER; +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 +struct sljit_jump *jump; +#endif +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 +struct sljit_jump *jump2; +#endif + +SLJIT_UNUSED_ARG(update_str_ptr); + +OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0); +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 +if (common->utf) + { + /* This can be an extra read in some situations, but hopefully + it is needed in most cases. */ + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes); + jump = CMP(SLJIT_LESS, TMP2, 0, SLJIT_IMM, 0xc0); + if (!update_str_ptr) + { + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f); + OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6); + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f); + OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0); + jump2 = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 255); + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes); + JUMPHERE(jump2); + } + else + add_jump(compiler, &common->utfreadtype8, JUMP(SLJIT_FAST_CALL)); + JUMPHERE(jump); + return; + } +#endif /* SUPPORT_UTF && COMPILE_PCRE8 */ + +#if !defined COMPILE_PCRE8 +/* The ctypes array contains only 256 values. */ +OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0); +jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 255); +#endif +OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes); +#if !defined COMPILE_PCRE8 +JUMPHERE(jump); +#endif + +#if defined SUPPORT_UTF && defined COMPILE_PCRE16 +if (common->utf && update_str_ptr) + { + /* Skip low surrogate if necessary. */ + OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xd800); + jump = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0xdc00 - 0xd800 - 1); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + JUMPHERE(jump); + } +#endif /* SUPPORT_UTF && COMPILE_PCRE16 */ +} + +static void skip_char_back(compiler_common *common) +{ +/* Goes one character back. Affects STR_PTR and TMP1. Does not check begin. */ +DEFINE_COMPILER; +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 +#if defined COMPILE_PCRE8 +struct sljit_label *label; + +if (common->utf) + { + label = LABEL(); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1)); + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xc0); + CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, label); + return; + } +#elif defined COMPILE_PCRE16 +if (common->utf) + { + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1)); + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + /* Skip low surrogate if necessary. */ + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xdc00); + OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL); + OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1); + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP1, 0); + return; + } +#endif /* COMPILE_PCRE[8|16] */ +#endif /* SUPPORT_UTF && !COMPILE_PCRE32 */ +OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); +} + +static void check_newlinechar(compiler_common *common, int nltype, jump_list **backtracks, BOOL jumpifmatch) +{ +/* Character comes in TMP1. Checks if it is a newline. TMP2 may be destroyed. */ +DEFINE_COMPILER; +struct sljit_jump *jump; + +if (nltype == NLTYPE_ANY) + { + add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL)); + sljit_set_current_flags(compiler, SLJIT_SET_Z); + add_jump(compiler, backtracks, JUMP(jumpifmatch ? SLJIT_NOT_ZERO : SLJIT_ZERO)); + } +else if (nltype == NLTYPE_ANYCRLF) + { + if (jumpifmatch) + { + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR)); + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL)); + } + else + { + jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL)); + JUMPHERE(jump); + } + } +else + { + SLJIT_ASSERT(nltype == NLTYPE_FIXED && common->newline < 256); + add_jump(compiler, backtracks, CMP(jumpifmatch ? SLJIT_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline)); + } +} + +#ifdef SUPPORT_UTF + +#if defined COMPILE_PCRE8 +static void do_utfreadchar(compiler_common *common) +{ +/* Fast decoding a UTF-8 character. TMP1 contains the first byte +of the character (>= 0xc0). Return char value in TMP1, length in TMP2. */ +DEFINE_COMPILER; +struct sljit_jump *jump; + +sljit_emit_fast_enter(compiler, RETURN_ADDR, 0); +OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); +OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f); +OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6); +OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f); +OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); + +/* Searching for the first zero. */ +OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800); +jump = JUMP(SLJIT_NOT_ZERO); +/* Two byte sequence. */ +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); +OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(2)); +sljit_emit_fast_return(compiler, RETURN_ADDR, 0); + +JUMPHERE(jump); +OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1)); +OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800); +OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6); +OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f); +OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); + +OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x10000); +jump = JUMP(SLJIT_NOT_ZERO); +/* Three byte sequence. */ +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2)); +OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(3)); +sljit_emit_fast_return(compiler, RETURN_ADDR, 0); + +/* Four byte sequence. */ +JUMPHERE(jump); +OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2)); +OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x10000); +OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6); +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3)); +OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f); +OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); +OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(4)); +sljit_emit_fast_return(compiler, RETURN_ADDR, 0); +} + +static void do_utfreadchar16(compiler_common *common) +{ +/* Fast decoding a UTF-8 character. TMP1 contains the first byte +of the character (>= 0xc0). Return value in TMP1. */ +DEFINE_COMPILER; +struct sljit_jump *jump; + +sljit_emit_fast_enter(compiler, RETURN_ADDR, 0); +OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); +OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f); +OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6); +OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f); +OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); + +/* Searching for the first zero. */ +OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x800); +jump = JUMP(SLJIT_NOT_ZERO); +/* Two byte sequence. */ +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); +sljit_emit_fast_return(compiler, RETURN_ADDR, 0); + +JUMPHERE(jump); +OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x400); +OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_NOT_ZERO); +/* This code runs only in 8 bit mode. No need to shift the value. */ +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0); +OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1)); +OP2(SLJIT_XOR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x800); +OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6); +OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f); +OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); +/* Three byte sequence. */ +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2)); +sljit_emit_fast_return(compiler, RETURN_ADDR, 0); +} + +static void do_utfreadtype8(compiler_common *common) +{ +/* Fast decoding a UTF-8 character type. TMP2 contains the first byte +of the character (>= 0xc0). Return value in TMP1. */ +DEFINE_COMPILER; +struct sljit_jump *jump; +struct sljit_jump *compare; + +sljit_emit_fast_enter(compiler, RETURN_ADDR, 0); + +OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0x20); +jump = JUMP(SLJIT_NOT_ZERO); +/* Two byte sequence. */ +OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); +OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f); +/* The upper 5 bits are known at this point. */ +compare = CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, 0x3); +OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6); +OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f); +OP2(SLJIT_OR, TMP2, 0, TMP2, 0, TMP1, 0); +OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes); +sljit_emit_fast_return(compiler, RETURN_ADDR, 0); + +JUMPHERE(compare); +OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0); +sljit_emit_fast_return(compiler, RETURN_ADDR, 0); + +/* We only have types for characters less than 256. */ +JUMPHERE(jump); +OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0); +OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0); +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0); +sljit_emit_fast_return(compiler, RETURN_ADDR, 0); +} + +#endif /* COMPILE_PCRE8 */ + +#endif /* SUPPORT_UTF */ + +#ifdef SUPPORT_UCP + +/* UCD_BLOCK_SIZE must be 128 (see the assert below). */ +#define UCD_BLOCK_MASK 127 +#define UCD_BLOCK_SHIFT 7 + +static void do_getucd(compiler_common *common) +{ +/* Search the UCD record for the character comes in TMP1. +Returns chartype in TMP1 and UCD offset in TMP2. */ +DEFINE_COMPILER; +#ifdef COMPILE_PCRE32 +struct sljit_jump *jump; +#endif + +#if defined SLJIT_DEBUG && SLJIT_DEBUG +/* dummy_ucd_record */ +const ucd_record *record = GET_UCD(INVALID_UTF_CHAR); +SLJIT_ASSERT(record->script == ucp_Common && record->chartype == ucp_Cn && record->gbprop == ucp_gbOther); +SLJIT_ASSERT(record->caseset == 0 && record->other_case == 0); +#endif + +SLJIT_ASSERT(UCD_BLOCK_SIZE == 128 && sizeof(ucd_record) == 8); + +sljit_emit_fast_enter(compiler, RETURN_ADDR, 0); + +#ifdef COMPILE_PCRE32 +if (!common->utf) + { + jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0x10ffff + 1); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, INVALID_UTF_CHAR); + JUMPHERE(jump); + } +#endif + +OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT); +OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_stage1)); +OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK); +OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT); +OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0); +OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_stage2)); +OP1(SLJIT_MOV_U16, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1); +OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype)); +OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3); +sljit_emit_fast_return(compiler, RETURN_ADDR, 0); +} +#endif + +static SLJIT_INLINE struct sljit_label *mainloop_entry(compiler_common *common, BOOL hascrorlf) +{ +DEFINE_COMPILER; +struct sljit_label *mainloop; +struct sljit_label *newlinelabel = NULL; +struct sljit_jump *start; +struct sljit_jump *end = NULL; +struct sljit_jump *end2 = NULL; +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 +struct sljit_jump *singlechar; +#endif +jump_list *newline = NULL; +BOOL newlinecheck = FALSE; +BOOL readuchar = FALSE; + +if (!(hascrorlf || (common->match_end_ptr != 0)) && + (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF || common->newline > 255)) + newlinecheck = TRUE; + +if (common->match_end_ptr != 0) + { + /* Search for the end of the first line. */ + OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0); + + if (common->nltype == NLTYPE_FIXED && common->newline > 255) + { + mainloop = LABEL(); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1)); + OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); + CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, mainloop); + CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, mainloop); + JUMPHERE(end); + OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + } + else + { + end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); + mainloop = LABEL(); + /* Continual stores does not cause data dependency. */ + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr, STR_PTR, 0); + read_char_range(common, common->nlmin, common->nlmax, TRUE); + check_newlinechar(common, common->nltype, &newline, TRUE); + CMPTO(SLJIT_LESS, STR_PTR, 0, STR_END, 0, mainloop); + JUMPHERE(end); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr, STR_PTR, 0); + set_jumps(newline, LABEL()); + } + + OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0); + } + +start = JUMP(SLJIT_JUMP); + +if (newlinecheck) + { + newlinelabel = LABEL(); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + end = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, common->newline & 0xff); + OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL); +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT); +#endif + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0); + end2 = JUMP(SLJIT_JUMP); + } + +mainloop = LABEL(); + +/* Increasing the STR_PTR here requires one less jump in the most common case. */ +#ifdef SUPPORT_UTF +if (common->utf) readuchar = TRUE; +#endif +if (newlinecheck) readuchar = TRUE; + +if (readuchar) + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0); + +if (newlinecheck) + CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, newlinelabel); + +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 +#if defined COMPILE_PCRE8 +if (common->utf) + { + singlechar = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0); + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0); + JUMPHERE(singlechar); + } +#elif defined COMPILE_PCRE16 +if (common->utf) + { + singlechar = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xd800); + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800); + OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL); + OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0); + JUMPHERE(singlechar); + } +#endif /* COMPILE_PCRE[8|16] */ +#endif /* SUPPORT_UTF && !COMPILE_PCRE32 */ +JUMPHERE(start); + +if (newlinecheck) + { + JUMPHERE(end); + JUMPHERE(end2); + } + +return mainloop; +} + +#define MAX_N_CHARS 16 +#define MAX_DIFF_CHARS 6 + +static SLJIT_INLINE void add_prefix_char(pcre_uchar chr, pcre_uchar *chars) +{ +pcre_uchar i, len; + +len = chars[0]; +if (len == 255) + return; + +if (len == 0) + { + chars[0] = 1; + chars[1] = chr; + return; + } + +for (i = len; i > 0; i--) + if (chars[i] == chr) + return; + +if (len >= MAX_DIFF_CHARS - 1) + { + chars[0] = 255; + return; + } + +len++; +chars[len] = chr; +chars[0] = len; +} + +static int scan_prefix(compiler_common *common, pcre_uchar *cc, pcre_uchar *chars, int max_chars, sljit_u32 *rec_count) +{ +/* Recursive function, which scans prefix literals. */ +BOOL last, any, class, caseless; +int len, repeat, len_save, consumed = 0; +sljit_u32 chr; /* Any unicode character. */ +sljit_u8 *bytes, *bytes_end, byte; +pcre_uchar *alternative, *cc_save, *oc; +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 +pcre_uchar othercase[8]; +#elif defined SUPPORT_UTF && defined COMPILE_PCRE16 +pcre_uchar othercase[2]; +#else +pcre_uchar othercase[1]; +#endif + +repeat = 1; +while (TRUE) + { + if (*rec_count == 0) + return 0; + (*rec_count)--; + + last = TRUE; + any = FALSE; + class = FALSE; + caseless = FALSE; + + switch (*cc) + { + case OP_CHARI: + caseless = TRUE; + case OP_CHAR: + last = FALSE; + cc++; + break; + + case OP_SOD: + case OP_SOM: + case OP_SET_SOM: + case OP_NOT_WORD_BOUNDARY: + case OP_WORD_BOUNDARY: + case OP_EODN: + case OP_EOD: + case OP_CIRC: + case OP_CIRCM: + case OP_DOLL: + case OP_DOLLM: + /* Zero width assertions. */ + cc++; + continue; + + case OP_ASSERT: + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + cc = bracketend(cc); + continue; + + case OP_PLUSI: + case OP_MINPLUSI: + case OP_POSPLUSI: + caseless = TRUE; + case OP_PLUS: + case OP_MINPLUS: + case OP_POSPLUS: + cc++; + break; + + case OP_EXACTI: + caseless = TRUE; + case OP_EXACT: + repeat = GET2(cc, 1); + last = FALSE; + cc += 1 + IMM2_SIZE; + break; + + case OP_QUERYI: + case OP_MINQUERYI: + case OP_POSQUERYI: + caseless = TRUE; + case OP_QUERY: + case OP_MINQUERY: + case OP_POSQUERY: + len = 1; + cc++; +#ifdef SUPPORT_UTF + if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc); +#endif + max_chars = scan_prefix(common, cc + len, chars, max_chars, rec_count); + if (max_chars == 0) + return consumed; + last = FALSE; + break; + + case OP_KET: + cc += 1 + LINK_SIZE; + continue; + + case OP_ALT: + cc += GET(cc, 1); + continue; + + case OP_ONCE: + case OP_ONCE_NC: + case OP_BRA: + case OP_BRAPOS: + case OP_CBRA: + case OP_CBRAPOS: + alternative = cc + GET(cc, 1); + while (*alternative == OP_ALT) + { + max_chars = scan_prefix(common, alternative + 1 + LINK_SIZE, chars, max_chars, rec_count); + if (max_chars == 0) + return consumed; + alternative += GET(alternative, 1); + } + + if (*cc == OP_CBRA || *cc == OP_CBRAPOS) + cc += IMM2_SIZE; + cc += 1 + LINK_SIZE; + continue; + + case OP_CLASS: +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 + if (common->utf && !is_char7_bitset((const sljit_u8 *)(cc + 1), FALSE)) + return consumed; +#endif + class = TRUE; + break; + + case OP_NCLASS: +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (common->utf) return consumed; +#endif + class = TRUE; + break; + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (common->utf) return consumed; +#endif + any = TRUE; + cc += GET(cc, 1); + break; +#endif + + case OP_DIGIT: +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 + if (common->utf && !is_char7_bitset((const sljit_u8 *)common->ctypes - cbit_length + cbit_digit, FALSE)) + return consumed; +#endif + any = TRUE; + cc++; + break; + + case OP_WHITESPACE: +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 + if (common->utf && !is_char7_bitset((const sljit_u8 *)common->ctypes - cbit_length + cbit_space, FALSE)) + return consumed; +#endif + any = TRUE; + cc++; + break; + + case OP_WORDCHAR: +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 + if (common->utf && !is_char7_bitset((const sljit_u8 *)common->ctypes - cbit_length + cbit_word, FALSE)) + return consumed; +#endif + any = TRUE; + cc++; + break; + + case OP_NOT: + case OP_NOTI: + cc++; + /* Fall through. */ + case OP_NOT_DIGIT: + case OP_NOT_WHITESPACE: + case OP_NOT_WORDCHAR: + case OP_ANY: + case OP_ALLANY: +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (common->utf) return consumed; +#endif + any = TRUE; + cc++; + break; + +#ifdef SUPPORT_UTF + case OP_NOTPROP: + case OP_PROP: +#ifndef COMPILE_PCRE32 + if (common->utf) return consumed; +#endif + any = TRUE; + cc += 1 + 2; + break; +#endif + + case OP_TYPEEXACT: + repeat = GET2(cc, 1); + cc += 1 + IMM2_SIZE; + continue; + + case OP_NOTEXACT: + case OP_NOTEXACTI: +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (common->utf) return consumed; +#endif + any = TRUE; + repeat = GET2(cc, 1); + cc += 1 + IMM2_SIZE + 1; + break; + + default: + return consumed; + } + + if (any) + { + do + { + chars[0] = 255; + + consumed++; + if (--max_chars == 0) + return consumed; + chars += MAX_DIFF_CHARS; + } + while (--repeat > 0); + + repeat = 1; + continue; + } + + if (class) + { + bytes = (sljit_u8*) (cc + 1); + cc += 1 + 32 / sizeof(pcre_uchar); + + switch (*cc) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRPOSSTAR: + case OP_CRQUERY: + case OP_CRMINQUERY: + case OP_CRPOSQUERY: + max_chars = scan_prefix(common, cc + 1, chars, max_chars, rec_count); + if (max_chars == 0) + return consumed; + break; + + default: + case OP_CRPLUS: + case OP_CRMINPLUS: + case OP_CRPOSPLUS: + break; + + case OP_CRRANGE: + case OP_CRMINRANGE: + case OP_CRPOSRANGE: + repeat = GET2(cc, 1); + if (repeat <= 0) + return consumed; + break; + } + + do + { + if (bytes[31] & 0x80) + chars[0] = 255; + else if (chars[0] != 255) + { + bytes_end = bytes + 32; + chr = 0; + do + { + byte = *bytes++; + SLJIT_ASSERT((chr & 0x7) == 0); + if (byte == 0) + chr += 8; + else + { + do + { + if ((byte & 0x1) != 0) + add_prefix_char(chr, chars); + byte >>= 1; + chr++; + } + while (byte != 0); + chr = (chr + 7) & ~7; + } + } + while (chars[0] != 255 && bytes < bytes_end); + bytes = bytes_end - 32; + } + + consumed++; + if (--max_chars == 0) + return consumed; + chars += MAX_DIFF_CHARS; + } + while (--repeat > 0); + + switch (*cc) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRPOSSTAR: + return consumed; + + case OP_CRQUERY: + case OP_CRMINQUERY: + case OP_CRPOSQUERY: + cc++; + break; + + case OP_CRRANGE: + case OP_CRMINRANGE: + case OP_CRPOSRANGE: + if (GET2(cc, 1) != GET2(cc, 1 + IMM2_SIZE)) + return consumed; + cc += 1 + 2 * IMM2_SIZE; + break; + } + + repeat = 1; + continue; + } + + len = 1; +#ifdef SUPPORT_UTF + if (common->utf && HAS_EXTRALEN(*cc)) len += GET_EXTRALEN(*cc); +#endif + + if (caseless && char_has_othercase(common, cc)) + { +#ifdef SUPPORT_UTF + if (common->utf) + { + GETCHAR(chr, cc); + if ((int)PRIV(ord2utf)(char_othercase(common, chr), othercase) != len) + return consumed; + } + else +#endif + { + chr = *cc; + othercase[0] = TABLE_GET(chr, common->fcc, chr); + } + } + else + { + caseless = FALSE; + othercase[0] = 0; /* Stops compiler warning - PH */ + } + + len_save = len; + cc_save = cc; + while (TRUE) + { + oc = othercase; + do + { + chr = *cc; + add_prefix_char(*cc, chars); + + if (caseless) + add_prefix_char(*oc, chars); + + len--; + consumed++; + if (--max_chars == 0) + return consumed; + chars += MAX_DIFF_CHARS; + cc++; + oc++; + } + while (len > 0); + + if (--repeat == 0) + break; + + len = len_save; + cc = cc_save; + } + + repeat = 1; + if (last) + return consumed; + } +} + +#if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86) && !(defined SUPPORT_VALGRIND) + +static sljit_s32 character_to_int32(pcre_uchar chr) +{ +sljit_s32 value = (sljit_s32)chr; +#if defined COMPILE_PCRE8 +#define SSE2_COMPARE_TYPE_INDEX 0 +return ((unsigned int)value << 24) | ((unsigned int)value << 16) | ((unsigned int)value << 8) | (unsigned int)value; +#elif defined COMPILE_PCRE16 +#define SSE2_COMPARE_TYPE_INDEX 1 +return ((unsigned int)value << 16) | value; +#elif defined COMPILE_PCRE32 +#define SSE2_COMPARE_TYPE_INDEX 2 +return value; +#else +#error "Unsupported unit width" +#endif +} + +static SLJIT_INLINE void fast_forward_first_char2_sse2(compiler_common *common, pcre_uchar char1, pcre_uchar char2) +{ +DEFINE_COMPILER; +struct sljit_label *start; +struct sljit_jump *quit[3]; +struct sljit_jump *nomatch; +sljit_u8 instruction[8]; +sljit_s32 tmp1_ind = sljit_get_register_index(TMP1); +sljit_s32 tmp2_ind = sljit_get_register_index(TMP2); +sljit_s32 str_ptr_ind = sljit_get_register_index(STR_PTR); +BOOL load_twice = FALSE; +pcre_uchar bit; + +bit = char1 ^ char2; +if (!is_powerof2(bit)) + bit = 0; + +if ((char1 != char2) && bit == 0) + load_twice = TRUE; + +quit[0] = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); + +/* First part (unaligned start) */ + +OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(char1 | bit)); + +SLJIT_ASSERT(tmp1_ind < 8 && tmp2_ind == 1); + +/* MOVD xmm, r/m32 */ +instruction[0] = 0x66; +instruction[1] = 0x0f; +instruction[2] = 0x6e; +instruction[3] = 0xc0 | (2 << 3) | tmp1_ind; +sljit_emit_op_custom(compiler, instruction, 4); + +if (char1 != char2) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, character_to_int32(bit != 0 ? bit : char2)); + + /* MOVD xmm, r/m32 */ + instruction[3] = 0xc0 | (3 << 3) | tmp1_ind; + sljit_emit_op_custom(compiler, instruction, 4); + } + +/* PSHUFD xmm1, xmm2/m128, imm8 */ +instruction[2] = 0x70; +instruction[3] = 0xc0 | (2 << 3) | 2; +instruction[4] = 0; +sljit_emit_op_custom(compiler, instruction, 5); + +if (char1 != char2) + { + /* PSHUFD xmm1, xmm2/m128, imm8 */ + instruction[3] = 0xc0 | (3 << 3) | 3; + instruction[4] = 0; + sljit_emit_op_custom(compiler, instruction, 5); + } + +OP2(SLJIT_AND, TMP2, 0, STR_PTR, 0, SLJIT_IMM, 0xf); +OP2(SLJIT_AND, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, ~0xf); + +/* MOVDQA xmm1, xmm2/m128 */ +#if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) + +if (str_ptr_ind < 8) + { + instruction[2] = 0x6f; + instruction[3] = (0 << 3) | str_ptr_ind; + sljit_emit_op_custom(compiler, instruction, 4); + + if (load_twice) + { + instruction[3] = (1 << 3) | str_ptr_ind; + sljit_emit_op_custom(compiler, instruction, 4); + } + } +else + { + instruction[1] = 0x41; + instruction[2] = 0x0f; + instruction[3] = 0x6f; + instruction[4] = (0 << 3) | (str_ptr_ind & 0x7); + sljit_emit_op_custom(compiler, instruction, 5); + + if (load_twice) + { + instruction[4] = (1 << 3) | str_ptr_ind; + sljit_emit_op_custom(compiler, instruction, 5); + } + instruction[1] = 0x0f; + } + +#else + +instruction[2] = 0x6f; +instruction[3] = (0 << 3) | str_ptr_ind; +sljit_emit_op_custom(compiler, instruction, 4); + +if (load_twice) + { + instruction[3] = (1 << 3) | str_ptr_ind; + sljit_emit_op_custom(compiler, instruction, 4); + } + +#endif + +if (bit != 0) + { + /* POR xmm1, xmm2/m128 */ + instruction[2] = 0xeb; + instruction[3] = 0xc0 | (0 << 3) | 3; + sljit_emit_op_custom(compiler, instruction, 4); + } + +/* PCMPEQB/W/D xmm1, xmm2/m128 */ +instruction[2] = 0x74 + SSE2_COMPARE_TYPE_INDEX; +instruction[3] = 0xc0 | (0 << 3) | 2; +sljit_emit_op_custom(compiler, instruction, 4); + +if (load_twice) + { + instruction[3] = 0xc0 | (1 << 3) | 3; + sljit_emit_op_custom(compiler, instruction, 4); + } + +/* PMOVMSKB reg, xmm */ +instruction[2] = 0xd7; +instruction[3] = 0xc0 | (tmp1_ind << 3) | 0; +sljit_emit_op_custom(compiler, instruction, 4); + +if (load_twice) + { + OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP2, 0); + instruction[3] = 0xc0 | (tmp2_ind << 3) | 1; + sljit_emit_op_custom(compiler, instruction, 4); + + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); + OP1(SLJIT_MOV, TMP2, 0, RETURN_ADDR, 0); + } + +OP2(SLJIT_ASHR, TMP1, 0, TMP1, 0, TMP2, 0); + +/* BSF r32, r/m32 */ +instruction[0] = 0x0f; +instruction[1] = 0xbc; +instruction[2] = 0xc0 | (tmp1_ind << 3) | tmp1_ind; +sljit_emit_op_custom(compiler, instruction, 3); +sljit_set_current_flags(compiler, SLJIT_SET_Z); + +nomatch = JUMP(SLJIT_ZERO); + +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0); +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0); +quit[1] = JUMP(SLJIT_JUMP); + +JUMPHERE(nomatch); + +start = LABEL(); +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 16); +quit[2] = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); + +/* Second part (aligned) */ + +instruction[0] = 0x66; +instruction[1] = 0x0f; + +/* MOVDQA xmm1, xmm2/m128 */ +#if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) + +if (str_ptr_ind < 8) + { + instruction[2] = 0x6f; + instruction[3] = (0 << 3) | str_ptr_ind; + sljit_emit_op_custom(compiler, instruction, 4); + + if (load_twice) + { + instruction[3] = (1 << 3) | str_ptr_ind; + sljit_emit_op_custom(compiler, instruction, 4); + } + } +else + { + instruction[1] = 0x41; + instruction[2] = 0x0f; + instruction[3] = 0x6f; + instruction[4] = (0 << 3) | (str_ptr_ind & 0x7); + sljit_emit_op_custom(compiler, instruction, 5); + + if (load_twice) + { + instruction[4] = (1 << 3) | str_ptr_ind; + sljit_emit_op_custom(compiler, instruction, 5); + } + instruction[1] = 0x0f; + } + +#else + +instruction[2] = 0x6f; +instruction[3] = (0 << 3) | str_ptr_ind; +sljit_emit_op_custom(compiler, instruction, 4); + +if (load_twice) + { + instruction[3] = (1 << 3) | str_ptr_ind; + sljit_emit_op_custom(compiler, instruction, 4); + } + +#endif + +if (bit != 0) + { + /* POR xmm1, xmm2/m128 */ + instruction[2] = 0xeb; + instruction[3] = 0xc0 | (0 << 3) | 3; + sljit_emit_op_custom(compiler, instruction, 4); + } + +/* PCMPEQB/W/D xmm1, xmm2/m128 */ +instruction[2] = 0x74 + SSE2_COMPARE_TYPE_INDEX; +instruction[3] = 0xc0 | (0 << 3) | 2; +sljit_emit_op_custom(compiler, instruction, 4); + +if (load_twice) + { + instruction[3] = 0xc0 | (1 << 3) | 3; + sljit_emit_op_custom(compiler, instruction, 4); + } + +/* PMOVMSKB reg, xmm */ +instruction[2] = 0xd7; +instruction[3] = 0xc0 | (tmp1_ind << 3) | 0; +sljit_emit_op_custom(compiler, instruction, 4); + +if (load_twice) + { + instruction[3] = 0xc0 | (tmp2_ind << 3) | 1; + sljit_emit_op_custom(compiler, instruction, 4); + + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0); + } + +/* BSF r32, r/m32 */ +instruction[0] = 0x0f; +instruction[1] = 0xbc; +instruction[2] = 0xc0 | (tmp1_ind << 3) | tmp1_ind; +sljit_emit_op_custom(compiler, instruction, 3); +sljit_set_current_flags(compiler, SLJIT_SET_Z); + +JUMPTO(SLJIT_ZERO, start); + +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0); + +start = LABEL(); +SET_LABEL(quit[0], start); +SET_LABEL(quit[1], start); +SET_LABEL(quit[2], start); +} + +#undef SSE2_COMPARE_TYPE_INDEX + +#endif + +static void fast_forward_first_char2(compiler_common *common, pcre_uchar char1, pcre_uchar char2, sljit_s32 offset) +{ +DEFINE_COMPILER; +struct sljit_label *start; +struct sljit_jump *quit; +struct sljit_jump *found; +pcre_uchar mask; +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 +struct sljit_label *utf_start = NULL; +struct sljit_jump *utf_quit = NULL; +#endif +BOOL has_match_end = (common->match_end_ptr != 0); + +if (offset > 0) + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offset)); + +if (has_match_end) + { + OP1(SLJIT_MOV, TMP3, 0, STR_END, 0); + + OP2(SLJIT_ADD, STR_END, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr, SLJIT_IMM, IN_UCHARS(offset + 1)); + OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, STR_END, 0, TMP3, 0); + sljit_emit_cmov(compiler, SLJIT_GREATER, STR_END, TMP3, 0); + } + +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 +if (common->utf && offset > 0) + utf_start = LABEL(); +#endif + +#if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86) && !(defined SUPPORT_VALGRIND) + +/* SSE2 accelerated first character search. */ + +if (sljit_has_cpu_feature(SLJIT_HAS_SSE2)) + { + fast_forward_first_char2_sse2(common, char1, char2); + + SLJIT_ASSERT(common->mode == JIT_COMPILE || offset == 0); + if (common->mode == JIT_COMPILE) + { + /* In complete mode, we don't need to run a match when STR_PTR == STR_END. */ + SLJIT_ASSERT(common->forced_quit_label == NULL); + OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_NOMATCH); + add_jump(compiler, &common->forced_quit, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0)); + +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (common->utf && offset > 0) + { + SLJIT_ASSERT(common->mode == JIT_COMPILE); + + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-offset)); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); +#if defined COMPILE_PCRE8 + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xc0); + CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, utf_start); +#elif defined COMPILE_PCRE16 + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00); + CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0xdc00, utf_start); +#else +#error "Unknown code width" +#endif + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + } +#endif + + if (offset > 0) + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offset)); + } + else + { + OP2(SLJIT_SUB | SLJIT_SET_GREATER_EQUAL, SLJIT_UNUSED, 0, STR_PTR, 0, STR_END, 0); + if (has_match_end) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr); + sljit_emit_cmov(compiler, SLJIT_GREATER_EQUAL, STR_PTR, TMP1, 0); + } + else + sljit_emit_cmov(compiler, SLJIT_GREATER_EQUAL, STR_PTR, STR_END, 0); + } + + if (has_match_end) + OP1(SLJIT_MOV, STR_END, 0, TMP3, 0); + return; + } + +#endif + +quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); + +start = LABEL(); +OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0); + +if (char1 == char2) + found = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, char1); +else + { + mask = char1 ^ char2; + if (is_powerof2(mask)) + { + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, mask); + found = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, char1 | mask); + } + else + { + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, char1); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, char2); + OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL); + found = JUMP(SLJIT_NOT_ZERO); + } + } + +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); +CMPTO(SLJIT_LESS, STR_PTR, 0, STR_END, 0, start); + +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 +if (common->utf && offset > 0) + utf_quit = JUMP(SLJIT_JUMP); +#endif + +JUMPHERE(found); + +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 +if (common->utf && offset > 0) + { + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-offset)); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); +#if defined COMPILE_PCRE8 + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xc0); + CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, utf_start); +#elif defined COMPILE_PCRE16 + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00); + CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0xdc00, utf_start); +#else +#error "Unknown code width" +#endif + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + JUMPHERE(utf_quit); + } +#endif + +JUMPHERE(quit); + +if (has_match_end) + { + quit = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0); + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr); + if (offset > 0) + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offset)); + JUMPHERE(quit); + OP1(SLJIT_MOV, STR_END, 0, TMP3, 0); + } + +if (offset > 0) + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(offset)); +} + +static SLJIT_INLINE BOOL fast_forward_first_n_chars(compiler_common *common) +{ +DEFINE_COMPILER; +struct sljit_label *start; +struct sljit_jump *quit; +struct sljit_jump *match; +/* bytes[0] represent the number of characters between 0 +and MAX_N_BYTES - 1, 255 represents any character. */ +pcre_uchar chars[MAX_N_CHARS * MAX_DIFF_CHARS]; +sljit_s32 offset; +pcre_uchar mask; +pcre_uchar *char_set, *char_set_end; +int i, max, from; +int range_right = -1, range_len; +sljit_u8 *update_table = NULL; +BOOL in_range; +sljit_u32 rec_count; + +for (i = 0; i < MAX_N_CHARS; i++) + chars[i * MAX_DIFF_CHARS] = 0; + +rec_count = 10000; +max = scan_prefix(common, common->start, chars, MAX_N_CHARS, &rec_count); + +if (max < 1) + return FALSE; + +in_range = FALSE; +/* Prevent compiler "uninitialized" warning */ +from = 0; +range_len = 4 /* minimum length */ - 1; +for (i = 0; i <= max; i++) + { + if (in_range && (i - from) > range_len && (chars[(i - 1) * MAX_DIFF_CHARS] < 255)) + { + range_len = i - from; + range_right = i - 1; + } + + if (i < max && chars[i * MAX_DIFF_CHARS] < 255) + { + SLJIT_ASSERT(chars[i * MAX_DIFF_CHARS] > 0); + if (!in_range) + { + in_range = TRUE; + from = i; + } + } + else + in_range = FALSE; + } + +if (range_right >= 0) + { + update_table = (sljit_u8 *)allocate_read_only_data(common, 256); + if (update_table == NULL) + return TRUE; + memset(update_table, IN_UCHARS(range_len), 256); + + for (i = 0; i < range_len; i++) + { + char_set = chars + ((range_right - i) * MAX_DIFF_CHARS); + SLJIT_ASSERT(char_set[0] > 0 && char_set[0] < 255); + char_set_end = char_set + char_set[0]; + char_set++; + while (char_set <= char_set_end) + { + if (update_table[(*char_set) & 0xff] > IN_UCHARS(i)) + update_table[(*char_set) & 0xff] = IN_UCHARS(i); + char_set++; + } + } + } + +offset = -1; +/* Scan forward. */ +for (i = 0; i < max; i++) + { + if (offset == -1) + { + if (chars[i * MAX_DIFF_CHARS] <= 2) + offset = i; + } + else if (chars[offset * MAX_DIFF_CHARS] == 2 && chars[i * MAX_DIFF_CHARS] <= 2) + { + if (chars[i * MAX_DIFF_CHARS] == 1) + offset = i; + else + { + mask = chars[offset * MAX_DIFF_CHARS + 1] ^ chars[offset * MAX_DIFF_CHARS + 2]; + if (!is_powerof2(mask)) + { + mask = chars[i * MAX_DIFF_CHARS + 1] ^ chars[i * MAX_DIFF_CHARS + 2]; + if (is_powerof2(mask)) + offset = i; + } + } + } + } + +if (range_right < 0) + { + if (offset < 0) + return FALSE; + SLJIT_ASSERT(chars[offset * MAX_DIFF_CHARS] >= 1 && chars[offset * MAX_DIFF_CHARS] <= 2); + /* Works regardless the value is 1 or 2. */ + mask = chars[offset * MAX_DIFF_CHARS + chars[offset * MAX_DIFF_CHARS]]; + fast_forward_first_char2(common, chars[offset * MAX_DIFF_CHARS + 1], mask, offset); + return TRUE; + } + +if (range_right == offset) + offset = -1; + +SLJIT_ASSERT(offset == -1 || (chars[offset * MAX_DIFF_CHARS] >= 1 && chars[offset * MAX_DIFF_CHARS] <= 2)); + +max -= 1; +SLJIT_ASSERT(max > 0); +if (common->match_end_ptr != 0) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr); + OP1(SLJIT_MOV, TMP3, 0, STR_END, 0); + OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max)); + quit = CMP(SLJIT_LESS_EQUAL, STR_END, 0, TMP1, 0); + OP1(SLJIT_MOV, STR_END, 0, TMP1, 0); + JUMPHERE(quit); + } +else + OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max)); + +SLJIT_ASSERT(range_right >= 0); + +#if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) +OP1(SLJIT_MOV, RETURN_ADDR, 0, SLJIT_IMM, (sljit_sw)update_table); +#endif + +start = LABEL(); +quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); + +#if defined COMPILE_PCRE8 || (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN) +OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right)); +#else +OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(range_right + 1) - 1); +#endif + +#if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) +OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM2(RETURN_ADDR, TMP1), 0); +#else +OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)update_table); +#endif +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0); +CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, start); + +if (offset >= 0) + { + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(offset)); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + + if (chars[offset * MAX_DIFF_CHARS] == 1) + CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[offset * MAX_DIFF_CHARS + 1], start); + else + { + mask = chars[offset * MAX_DIFF_CHARS + 1] ^ chars[offset * MAX_DIFF_CHARS + 2]; + if (is_powerof2(mask)) + { + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, mask); + CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[offset * MAX_DIFF_CHARS + 1] | mask, start); + } + else + { + match = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, chars[offset * MAX_DIFF_CHARS + 1]); + CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[offset * MAX_DIFF_CHARS + 2], start); + JUMPHERE(match); + } + } + } + +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 +if (common->utf && offset != 0) + { + if (offset < 0) + { + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + } + else + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1)); +#if defined COMPILE_PCRE8 + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xc0); + CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, start); +#elif defined COMPILE_PCRE16 + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00); + CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0xdc00, start); +#else +#error "Unknown code width" +#endif + if (offset < 0) + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + } +#endif + +if (offset >= 0) + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + +JUMPHERE(quit); + +if (common->match_end_ptr != 0) + { + if (range_right >= 0) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr); + OP1(SLJIT_MOV, STR_END, 0, TMP3, 0); + if (range_right >= 0) + { + quit = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP1, 0); + OP1(SLJIT_MOV, STR_PTR, 0, TMP1, 0); + JUMPHERE(quit); + } + } +else + OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS(max)); +return TRUE; +} + +#undef MAX_N_CHARS +#undef MAX_DIFF_CHARS + +static SLJIT_INLINE void fast_forward_first_char(compiler_common *common, pcre_uchar first_char, BOOL caseless) +{ +pcre_uchar oc; + +oc = first_char; +if (caseless) + { + oc = TABLE_GET(first_char, common->fcc, first_char); +#if defined SUPPORT_UCP && !defined COMPILE_PCRE8 + if (first_char > 127 && common->utf) + oc = UCD_OTHERCASE(first_char); +#endif + } + +fast_forward_first_char2(common, first_char, oc, 0); +} + +static SLJIT_INLINE void fast_forward_newline(compiler_common *common) +{ +DEFINE_COMPILER; +struct sljit_label *loop; +struct sljit_jump *lastchar; +struct sljit_jump *firstchar; +struct sljit_jump *quit; +struct sljit_jump *foundcr = NULL; +struct sljit_jump *notfoundnl; +jump_list *newline = NULL; + +if (common->match_end_ptr != 0) + { + OP1(SLJIT_MOV, TMP3, 0, STR_END, 0); + OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr); + } + +if (common->nltype == NLTYPE_FIXED && common->newline > 255) + { + lastchar = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); + OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str)); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin)); + firstchar = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP2, 0); + + OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(2)); + OP2(SLJIT_SUB | SLJIT_SET_GREATER_EQUAL, SLJIT_UNUSED, 0, STR_PTR, 0, TMP1, 0); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_GREATER_EQUAL); +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCHAR_SHIFT); +#endif + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0); + + loop = LABEL(); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-2)); + OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1)); + CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, loop); + CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, loop); + + JUMPHERE(quit); + JUMPHERE(firstchar); + JUMPHERE(lastchar); + + if (common->match_end_ptr != 0) + OP1(SLJIT_MOV, STR_END, 0, TMP3, 0); + return; + } + +OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0); +OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str)); +firstchar = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP2, 0); +skip_char_back(common); + +loop = LABEL(); +common->ff_newline_shortcut = loop; + +read_char_range(common, common->nlmin, common->nlmax, TRUE); +lastchar = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); +if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF) + foundcr = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR); +check_newlinechar(common, common->nltype, &newline, FALSE); +set_jumps(newline, loop); + +if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF) + { + quit = JUMP(SLJIT_JUMP); + JUMPHERE(foundcr); + notfoundnl = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL); + OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL); +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT); +#endif + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0); + JUMPHERE(notfoundnl); + JUMPHERE(quit); + } +JUMPHERE(lastchar); +JUMPHERE(firstchar); + +if (common->match_end_ptr != 0) + OP1(SLJIT_MOV, STR_END, 0, TMP3, 0); +} + +static BOOL check_class_ranges(compiler_common *common, const sljit_u8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks); + +static SLJIT_INLINE void fast_forward_start_bits(compiler_common *common, const sljit_u8 *start_bits) +{ +DEFINE_COMPILER; +struct sljit_label *start; +struct sljit_jump *quit; +struct sljit_jump *found = NULL; +jump_list *matches = NULL; +#ifndef COMPILE_PCRE8 +struct sljit_jump *jump; +#endif + +if (common->match_end_ptr != 0) + { + OP1(SLJIT_MOV, RETURN_ADDR, 0, STR_END, 0); + OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr); + } + +start = LABEL(); +quit = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); +OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0); +#ifdef SUPPORT_UTF +if (common->utf) + OP1(SLJIT_MOV, TMP3, 0, TMP1, 0); +#endif + +if (!check_class_ranges(common, start_bits, (start_bits[31] & 0x80) != 0, TRUE, &matches)) + { +#ifndef COMPILE_PCRE8 + jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 255); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 255); + JUMPHERE(jump); +#endif + OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7); + OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3); + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)start_bits); + OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0); + OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0); + found = JUMP(SLJIT_NOT_ZERO); + } + +#ifdef SUPPORT_UTF +if (common->utf) + OP1(SLJIT_MOV, TMP1, 0, TMP3, 0); +#endif +OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); +#ifdef SUPPORT_UTF +#if defined COMPILE_PCRE8 +if (common->utf) + { + CMPTO(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0, start); + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0); + } +#elif defined COMPILE_PCRE16 +if (common->utf) + { + CMPTO(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xd800, start); + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800); + OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL); + OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0); + } +#endif /* COMPILE_PCRE[8|16] */ +#endif /* SUPPORT_UTF */ +JUMPTO(SLJIT_JUMP, start); +if (found != NULL) + JUMPHERE(found); +if (matches != NULL) + set_jumps(matches, LABEL()); +JUMPHERE(quit); + +if (common->match_end_ptr != 0) + OP1(SLJIT_MOV, STR_END, 0, RETURN_ADDR, 0); +} + +static SLJIT_INLINE struct sljit_jump *search_requested_char(compiler_common *common, pcre_uchar req_char, BOOL caseless, BOOL has_firstchar) +{ +DEFINE_COMPILER; +struct sljit_label *loop; +struct sljit_jump *toolong; +struct sljit_jump *alreadyfound; +struct sljit_jump *found; +struct sljit_jump *foundoc = NULL; +struct sljit_jump *notfound; +sljit_u32 oc, bit; + +SLJIT_ASSERT(common->req_char_ptr != 0); +OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->req_char_ptr); +OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, REQ_BYTE_MAX); +toolong = CMP(SLJIT_LESS, TMP1, 0, STR_END, 0); +alreadyfound = CMP(SLJIT_LESS, STR_PTR, 0, TMP2, 0); + +if (has_firstchar) + OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); +else + OP1(SLJIT_MOV, TMP1, 0, STR_PTR, 0); + +loop = LABEL(); +notfound = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, STR_END, 0); + +OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(TMP1), 0); +oc = req_char; +if (caseless) + { + oc = TABLE_GET(req_char, common->fcc, req_char); +#if defined SUPPORT_UCP && !(defined COMPILE_PCRE8) + if (req_char > 127 && common->utf) + oc = UCD_OTHERCASE(req_char); +#endif + } +if (req_char == oc) + found = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, req_char); +else + { + bit = req_char ^ oc; + if (is_powerof2(bit)) + { + OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, bit); + found = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, req_char | bit); + } + else + { + found = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, req_char); + foundoc = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, oc); + } + } +OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1)); +JUMPTO(SLJIT_JUMP, loop); + +JUMPHERE(found); +if (foundoc) + JUMPHERE(foundoc); +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->req_char_ptr, TMP1, 0); +JUMPHERE(alreadyfound); +JUMPHERE(toolong); +return notfound; +} + +static void do_revertframes(compiler_common *common) +{ +DEFINE_COMPILER; +struct sljit_jump *jump; +struct sljit_label *mainloop; + +sljit_emit_fast_enter(compiler, RETURN_ADDR, 0); +OP1(SLJIT_MOV, TMP3, 0, STACK_TOP, 0); +GET_LOCAL_BASE(TMP1, 0, 0); + +/* Drop frames until we reach STACK_TOP. */ +mainloop = LABEL(); +OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), -sizeof(sljit_sw)); +jump = CMP(SLJIT_SIG_LESS_EQUAL, TMP2, 0, SLJIT_IMM, 0); + +OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP1, 0); +OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(STACK_TOP), -2 * sizeof(sljit_sw)); +OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), sizeof(sljit_sw), SLJIT_MEM1(STACK_TOP), -3 * sizeof(sljit_sw)); +OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 3 * sizeof(sljit_sw)); +JUMPTO(SLJIT_JUMP, mainloop); + +JUMPHERE(jump); +jump = CMP(SLJIT_NOT_ZERO /* SIG_LESS */, TMP2, 0, SLJIT_IMM, 0); +/* End of reverting values. */ +OP1(SLJIT_MOV, STACK_TOP, 0, TMP3, 0); +sljit_emit_fast_return(compiler, RETURN_ADDR, 0); + +JUMPHERE(jump); +OP1(SLJIT_NEG, TMP2, 0, TMP2, 0); +OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP1, 0); +OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(STACK_TOP), -2 * sizeof(sljit_sw)); +OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 2 * sizeof(sljit_sw)); +JUMPTO(SLJIT_JUMP, mainloop); +} + +static void check_wordboundary(compiler_common *common) +{ +DEFINE_COMPILER; +struct sljit_jump *skipread; +jump_list *skipread_list = NULL; +#if !(defined COMPILE_PCRE8) || defined SUPPORT_UTF +struct sljit_jump *jump; +#endif + +SLJIT_COMPILE_ASSERT(ctype_word == 0x10, ctype_word_must_be_16); + +sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0); +/* Get type of the previous char, and put it to LOCALS1. */ +OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0); +OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin)); +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, SLJIT_IMM, 0); +skipread = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP1, 0); +skip_char_back(common); +check_start_used_ptr(common); +read_char(common); + +/* Testing char type. */ +#ifdef SUPPORT_UCP +if (common->use_ucp) + { + OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1); + jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE); + add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL)); + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL); + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL); + JUMPHERE(jump); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP2, 0); + } +else +#endif + { +#ifndef COMPILE_PCRE8 + jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255); +#elif defined SUPPORT_UTF + /* Here LOCALS1 has already been zeroed. */ + jump = NULL; + if (common->utf) + jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255); +#endif /* COMPILE_PCRE8 */ + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), common->ctypes); + OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 4 /* ctype_word */); + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, TMP1, 0); +#ifndef COMPILE_PCRE8 + JUMPHERE(jump); +#elif defined SUPPORT_UTF + if (jump != NULL) + JUMPHERE(jump); +#endif /* COMPILE_PCRE8 */ + } +JUMPHERE(skipread); + +OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0); +check_str_end(common, &skipread_list); +peek_char(common, READ_CHAR_MAX); + +/* Testing char type. This is a code duplication. */ +#ifdef SUPPORT_UCP +if (common->use_ucp) + { + OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1); + jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE); + add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL)); + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL); + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL); + JUMPHERE(jump); + } +else +#endif + { +#ifndef COMPILE_PCRE8 + /* TMP2 may be destroyed by peek_char. */ + OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0); + jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255); +#elif defined SUPPORT_UTF + OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0); + jump = NULL; + if (common->utf) + jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255); +#endif + OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP1), common->ctypes); + OP2(SLJIT_LSHR, TMP2, 0, TMP2, 0, SLJIT_IMM, 4 /* ctype_word */); + OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 1); +#ifndef COMPILE_PCRE8 + JUMPHERE(jump); +#elif defined SUPPORT_UTF + if (jump != NULL) + JUMPHERE(jump); +#endif /* COMPILE_PCRE8 */ + } +set_jumps(skipread_list, LABEL()); + +OP2(SLJIT_XOR | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1); +sljit_emit_fast_return(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0); +} + +static BOOL check_class_ranges(compiler_common *common, const sljit_u8 *bits, BOOL nclass, BOOL invert, jump_list **backtracks) +{ +/* May destroy TMP1. */ +DEFINE_COMPILER; +int ranges[MAX_RANGE_SIZE]; +sljit_u8 bit, cbit, all; +int i, byte, length = 0; + +bit = bits[0] & 0x1; +/* All bits will be zero or one (since bit is zero or one). */ +all = -bit; + +for (i = 0; i < 256; ) + { + byte = i >> 3; + if ((i & 0x7) == 0 && bits[byte] == all) + i += 8; + else + { + cbit = (bits[byte] >> (i & 0x7)) & 0x1; + if (cbit != bit) + { + if (length >= MAX_RANGE_SIZE) + return FALSE; + ranges[length] = i; + length++; + bit = cbit; + all = -cbit; + } + i++; + } + } + +if (((bit == 0) && nclass) || ((bit == 1) && !nclass)) + { + if (length >= MAX_RANGE_SIZE) + return FALSE; + ranges[length] = 256; + length++; + } + +if (length < 0 || length > 4) + return FALSE; + +bit = bits[0] & 0x1; +if (invert) bit ^= 0x1; + +/* No character is accepted. */ +if (length == 0 && bit == 0) + add_jump(compiler, backtracks, JUMP(SLJIT_JUMP)); + +switch(length) + { + case 0: + /* When bit != 0, all characters are accepted. */ + return TRUE; + + case 1: + add_jump(compiler, backtracks, CMP(bit == 0 ? SLJIT_LESS : SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0])); + return TRUE; + + case 2: + if (ranges[0] + 1 != ranges[1]) + { + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]); + add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_LESS : SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0])); + } + else + add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0])); + return TRUE; + + case 3: + if (bit != 0) + { + add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2])); + if (ranges[0] + 1 != ranges[1]) + { + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]); + add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0])); + } + else + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0])); + return TRUE; + } + + add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[0])); + if (ranges[1] + 1 != ranges[2]) + { + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[1]); + add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[1])); + } + else + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1])); + return TRUE; + + case 4: + if ((ranges[1] - ranges[0]) == (ranges[3] - ranges[2]) + && (ranges[0] | (ranges[2] - ranges[0])) == ranges[2] + && (ranges[1] & (ranges[2] - ranges[0])) == 0 + && is_powerof2(ranges[2] - ranges[0])) + { + SLJIT_ASSERT((ranges[0] & (ranges[2] - ranges[0])) == 0 && (ranges[2] & ranges[3] & (ranges[2] - ranges[0])) != 0); + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[0]); + if (ranges[2] + 1 != ranges[3]) + { + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]); + add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_LESS : SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2])); + } + else + add_jump(compiler, backtracks, CMP(bit != 0 ? SLJIT_EQUAL : SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2])); + return TRUE; + } + + if (bit != 0) + { + i = 0; + if (ranges[0] + 1 != ranges[1]) + { + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]); + add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0])); + i = ranges[0]; + } + else + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[0])); + + if (ranges[2] + 1 != ranges[3]) + { + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2] - i); + add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2])); + } + else + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2] - i)); + return TRUE; + } + + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[0]); + add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[0])); + if (ranges[1] + 1 != ranges[2]) + { + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0]); + add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, ranges[2] - ranges[1])); + } + else + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[1] - ranges[0])); + return TRUE; + + default: + SLJIT_UNREACHABLE(); + return FALSE; + } +} + +static void check_anynewline(compiler_common *common) +{ +/* Check whether TMP1 contains a newline character. TMP2 destroyed. */ +DEFINE_COMPILER; + +sljit_emit_fast_enter(compiler, RETURN_ADDR, 0); + +OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0a); +OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x0d - 0x0a); +OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL); +OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x0a); +#if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32 +#ifdef COMPILE_PCRE8 +if (common->utf) + { +#endif + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2029 - 0x0a); +#ifdef COMPILE_PCRE8 + } +#endif +#endif /* SUPPORT_UTF || COMPILE_PCRE16 || COMPILE_PCRE32 */ +OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL); +sljit_emit_fast_return(compiler, RETURN_ADDR, 0); +} + +static void check_hspace(compiler_common *common) +{ +/* Check whether TMP1 contains a newline character. TMP2 destroyed. */ +DEFINE_COMPILER; + +sljit_emit_fast_enter(compiler, RETURN_ADDR, 0); + +OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x09); +OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL); +OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x20); +OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); +OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xa0); +#if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32 +#ifdef COMPILE_PCRE8 +if (common->utf) + { +#endif + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x1680); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x2000); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x200A - 0x2000); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x202f - 0x2000); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x205f - 0x2000); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x3000 - 0x2000); +#ifdef COMPILE_PCRE8 + } +#endif +#endif /* SUPPORT_UTF || COMPILE_PCRE16 || COMPILE_PCRE32 */ +OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL); + +sljit_emit_fast_return(compiler, RETURN_ADDR, 0); +} + +static void check_vspace(compiler_common *common) +{ +/* Check whether TMP1 contains a newline character. TMP2 destroyed. */ +DEFINE_COMPILER; + +sljit_emit_fast_enter(compiler, RETURN_ADDR, 0); + +OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0a); +OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x0d - 0x0a); +OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL); +OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x0a); +#if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32 +#ifdef COMPILE_PCRE8 +if (common->utf) + { +#endif + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2029 - 0x0a); +#ifdef COMPILE_PCRE8 + } +#endif +#endif /* SUPPORT_UTF || COMPILE_PCRE16 || COMPILE_PCRE32 */ +OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL); + +sljit_emit_fast_return(compiler, RETURN_ADDR, 0); +} + +static void do_casefulcmp(compiler_common *common) +{ +DEFINE_COMPILER; +struct sljit_jump *jump; +struct sljit_label *label; +int char1_reg; +int char2_reg; + +if (sljit_get_register_index(TMP3) < 0) + { + char1_reg = STR_END; + char2_reg = STACK_TOP; + } +else + { + char1_reg = TMP3; + char2_reg = RETURN_ADDR; + } + +sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0); +OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0); + +if (char1_reg == STR_END) + { + OP1(SLJIT_MOV, TMP3, 0, char1_reg, 0); + OP1(SLJIT_MOV, RETURN_ADDR, 0, char2_reg, 0); + } + +if (sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_SUPP | SLJIT_MEM_POST, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)) == SLJIT_SUCCESS) + { + label = LABEL(); + sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_POST, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)); + sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_POST, char2_reg, SLJIT_MEM1(STR_PTR), IN_UCHARS(1)); + jump = CMP(SLJIT_NOT_EQUAL, char1_reg, 0, char2_reg, 0); + OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1)); + JUMPTO(SLJIT_NOT_ZERO, label); + + JUMPHERE(jump); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0); + } +else if (sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_SUPP | SLJIT_MEM_PRE, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)) == SLJIT_SUCCESS) + { + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1)); + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + + label = LABEL(); + sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_PRE, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)); + sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_PRE, char2_reg, SLJIT_MEM1(STR_PTR), IN_UCHARS(1)); + jump = CMP(SLJIT_NOT_EQUAL, char1_reg, 0, char2_reg, 0); + OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1)); + JUMPTO(SLJIT_NOT_ZERO, label); + + JUMPHERE(jump); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + } +else + { + label = LABEL(); + OP1(MOV_UCHAR, char1_reg, 0, SLJIT_MEM1(TMP1), 0); + OP1(MOV_UCHAR, char2_reg, 0, SLJIT_MEM1(STR_PTR), 0); + OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1)); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + jump = CMP(SLJIT_NOT_EQUAL, char1_reg, 0, char2_reg, 0); + OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1)); + JUMPTO(SLJIT_NOT_ZERO, label); + + JUMPHERE(jump); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0); + } + +if (char1_reg == STR_END) + { + OP1(SLJIT_MOV, char1_reg, 0, TMP3, 0); + OP1(SLJIT_MOV, char2_reg, 0, RETURN_ADDR, 0); + } + +sljit_emit_fast_return(compiler, TMP1, 0); +} + +static void do_caselesscmp(compiler_common *common) +{ +DEFINE_COMPILER; +struct sljit_jump *jump; +struct sljit_label *label; +int char1_reg = STR_END; +int char2_reg; +int lcc_table; +int opt_type = 0; + +if (sljit_get_register_index(TMP3) < 0) + { + char2_reg = STACK_TOP; + lcc_table = STACK_LIMIT; + } +else + { + char2_reg = RETURN_ADDR; + lcc_table = TMP3; + } + +if (sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_SUPP | SLJIT_MEM_POST, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)) == SLJIT_SUCCESS) + opt_type = 1; +else if (sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_SUPP | SLJIT_MEM_PRE, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)) == SLJIT_SUCCESS) + opt_type = 2; + +sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0); +OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0); + +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, char1_reg, 0); + +if (char2_reg == STACK_TOP) + { + OP1(SLJIT_MOV, TMP3, 0, char2_reg, 0); + OP1(SLJIT_MOV, RETURN_ADDR, 0, lcc_table, 0); + } + +OP1(SLJIT_MOV, lcc_table, 0, SLJIT_IMM, common->lcc); + +if (opt_type == 1) + { + label = LABEL(); + sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_POST, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)); + sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_POST, char2_reg, SLJIT_MEM1(STR_PTR), IN_UCHARS(1)); + } +else if (opt_type == 2) + { + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1)); + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + + label = LABEL(); + sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_PRE, char1_reg, SLJIT_MEM1(TMP1), IN_UCHARS(1)); + sljit_emit_mem(compiler, MOV_UCHAR | SLJIT_MEM_PRE, char2_reg, SLJIT_MEM1(STR_PTR), IN_UCHARS(1)); + } +else + { + label = LABEL(); + OP1(MOV_UCHAR, char1_reg, 0, SLJIT_MEM1(TMP1), 0); + OP1(MOV_UCHAR, char2_reg, 0, SLJIT_MEM1(STR_PTR), 0); + OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1)); + } + +#ifndef COMPILE_PCRE8 +jump = CMP(SLJIT_GREATER, char1_reg, 0, SLJIT_IMM, 255); +#endif +OP1(SLJIT_MOV_U8, char1_reg, 0, SLJIT_MEM2(lcc_table, char1_reg), 0); +#ifndef COMPILE_PCRE8 +JUMPHERE(jump); +jump = CMP(SLJIT_GREATER, char2_reg, 0, SLJIT_IMM, 255); +#endif +OP1(SLJIT_MOV_U8, char2_reg, 0, SLJIT_MEM2(lcc_table, char2_reg), 0); +#ifndef COMPILE_PCRE8 +JUMPHERE(jump); +#endif + +if (opt_type == 0) + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + +jump = CMP(SLJIT_NOT_EQUAL, char1_reg, 0, char2_reg, 0); +OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, IN_UCHARS(1)); +JUMPTO(SLJIT_NOT_ZERO, label); + +JUMPHERE(jump); +OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0); + +if (opt_type == 2) + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + +if (char2_reg == STACK_TOP) + { + OP1(SLJIT_MOV, char2_reg, 0, TMP3, 0); + OP1(SLJIT_MOV, lcc_table, 0, RETURN_ADDR, 0); + } + +OP1(SLJIT_MOV, char1_reg, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1); +sljit_emit_fast_return(compiler, TMP1, 0); +} + +#if defined SUPPORT_UTF && defined SUPPORT_UCP + +static const pcre_uchar * SLJIT_FUNC do_utf_caselesscmp(pcre_uchar *src1, pcre_uchar *src2, pcre_uchar *end1, pcre_uchar *end2) +{ +/* This function would be ineffective to do in JIT level. */ +sljit_u32 c1, c2; +const ucd_record *ur; +const sljit_u32 *pp; + +while (src1 < end1) + { + if (src2 >= end2) + return (pcre_uchar*)1; + GETCHARINC(c1, src1); + GETCHARINC(c2, src2); + ur = GET_UCD(c2); + if (c1 != c2 && c1 != c2 + ur->other_case) + { + pp = PRIV(ucd_caseless_sets) + ur->caseset; + for (;;) + { + if (c1 < *pp) return NULL; + if (c1 == *pp++) break; + } + } + } +return src2; +} + +#endif /* SUPPORT_UTF && SUPPORT_UCP */ + +static pcre_uchar *byte_sequence_compare(compiler_common *common, BOOL caseless, pcre_uchar *cc, + compare_context *context, jump_list **backtracks) +{ +DEFINE_COMPILER; +unsigned int othercasebit = 0; +pcre_uchar *othercasechar = NULL; +#ifdef SUPPORT_UTF +int utflength; +#endif + +if (caseless && char_has_othercase(common, cc)) + { + othercasebit = char_get_othercase_bit(common, cc); + SLJIT_ASSERT(othercasebit); + /* Extracting bit difference info. */ +#if defined COMPILE_PCRE8 + othercasechar = cc + (othercasebit >> 8); + othercasebit &= 0xff; +#elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + /* Note that this code only handles characters in the BMP. If there + ever are characters outside the BMP whose othercase differs in only one + bit from itself (there currently are none), this code will need to be + revised for COMPILE_PCRE32. */ + othercasechar = cc + (othercasebit >> 9); + if ((othercasebit & 0x100) != 0) + othercasebit = (othercasebit & 0xff) << 8; + else + othercasebit &= 0xff; +#endif /* COMPILE_PCRE[8|16|32] */ + } + +if (context->sourcereg == -1) + { +#if defined COMPILE_PCRE8 +#if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED + if (context->length >= 4) + OP1(SLJIT_MOV_S32, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length); + else if (context->length >= 2) + OP1(SLJIT_MOV_U16, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length); + else +#endif + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length); +#elif defined COMPILE_PCRE16 +#if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED + if (context->length >= 4) + OP1(SLJIT_MOV_S32, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length); + else +#endif + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length); +#elif defined COMPILE_PCRE32 + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -context->length); +#endif /* COMPILE_PCRE[8|16|32] */ + context->sourcereg = TMP2; + } + +#ifdef SUPPORT_UTF +utflength = 1; +if (common->utf && HAS_EXTRALEN(*cc)) + utflength += GET_EXTRALEN(*cc); + +do + { +#endif + + context->length -= IN_UCHARS(1); +#if (defined SLJIT_UNALIGNED && SLJIT_UNALIGNED) && (defined COMPILE_PCRE8 || defined COMPILE_PCRE16) + + /* Unaligned read is supported. */ + if (othercasebit != 0 && othercasechar == cc) + { + context->c.asuchars[context->ucharptr] = *cc | othercasebit; + context->oc.asuchars[context->ucharptr] = othercasebit; + } + else + { + context->c.asuchars[context->ucharptr] = *cc; + context->oc.asuchars[context->ucharptr] = 0; + } + context->ucharptr++; + +#if defined COMPILE_PCRE8 + if (context->ucharptr >= 4 || context->length == 0 || (context->ucharptr == 2 && context->length == 1)) +#else + if (context->ucharptr >= 2 || context->length == 0) +#endif + { + if (context->length >= 4) + OP1(SLJIT_MOV_S32, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length); + else if (context->length >= 2) + OP1(SLJIT_MOV_U16, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length); +#if defined COMPILE_PCRE8 + else if (context->length >= 1) + OP1(SLJIT_MOV_U8, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length); +#endif /* COMPILE_PCRE8 */ + context->sourcereg = context->sourcereg == TMP1 ? TMP2 : TMP1; + + switch(context->ucharptr) + { + case 4 / sizeof(pcre_uchar): + if (context->oc.asint != 0) + OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asint); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asint | context->oc.asint)); + break; + + case 2 / sizeof(pcre_uchar): + if (context->oc.asushort != 0) + OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asushort); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asushort | context->oc.asushort)); + break; + +#ifdef COMPILE_PCRE8 + case 1: + if (context->oc.asbyte != 0) + OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, context->oc.asbyte); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, context->c.asbyte | context->oc.asbyte)); + break; +#endif + + default: + SLJIT_UNREACHABLE(); + break; + } + context->ucharptr = 0; + } + +#else + + /* Unaligned read is unsupported or in 32 bit mode. */ + if (context->length >= 1) + OP1(MOV_UCHAR, context->sourcereg, 0, SLJIT_MEM1(STR_PTR), -context->length); + + context->sourcereg = context->sourcereg == TMP1 ? TMP2 : TMP1; + + if (othercasebit != 0 && othercasechar == cc) + { + OP2(SLJIT_OR, context->sourcereg, 0, context->sourcereg, 0, SLJIT_IMM, othercasebit); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, *cc | othercasebit)); + } + else + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, context->sourcereg, 0, SLJIT_IMM, *cc)); + +#endif + + cc++; +#ifdef SUPPORT_UTF + utflength--; + } +while (utflength > 0); +#endif + +return cc; +} + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + +#define SET_TYPE_OFFSET(value) \ + if ((value) != typeoffset) \ + { \ + if ((value) < typeoffset) \ + OP2(SLJIT_ADD, typereg, 0, typereg, 0, SLJIT_IMM, typeoffset - (value)); \ + else \ + OP2(SLJIT_SUB, typereg, 0, typereg, 0, SLJIT_IMM, (value) - typeoffset); \ + } \ + typeoffset = (value); + +#define SET_CHAR_OFFSET(value) \ + if ((value) != charoffset) \ + { \ + if ((value) < charoffset) \ + OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(charoffset - (value))); \ + else \ + OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)((value) - charoffset)); \ + } \ + charoffset = (value); + +static pcre_uchar *compile_char1_matchingpath(compiler_common *common, pcre_uchar type, pcre_uchar *cc, jump_list **backtracks, BOOL check_str_ptr); + +static void compile_xclass_matchingpath(compiler_common *common, pcre_uchar *cc, jump_list **backtracks) +{ +DEFINE_COMPILER; +jump_list *found = NULL; +jump_list **list = (cc[0] & XCL_NOT) == 0 ? &found : backtracks; +sljit_uw c, charoffset, max = 256, min = READ_CHAR_MAX; +struct sljit_jump *jump = NULL; +pcre_uchar *ccbegin; +int compares, invertcmp, numberofcmps; +#if defined SUPPORT_UTF && (defined COMPILE_PCRE8 || defined COMPILE_PCRE16) +BOOL utf = common->utf; +#endif + +#ifdef SUPPORT_UCP +BOOL needstype = FALSE, needsscript = FALSE, needschar = FALSE; +BOOL charsaved = FALSE; +int typereg = TMP1; +const sljit_u32 *other_cases; +sljit_uw typeoffset; +#endif + +/* Scanning the necessary info. */ +cc++; +ccbegin = cc; +compares = 0; +if (cc[-1] & XCL_MAP) + { + min = 0; + cc += 32 / sizeof(pcre_uchar); + } + +while (*cc != XCL_END) + { + compares++; + if (*cc == XCL_SINGLE) + { + cc ++; + GETCHARINCTEST(c, cc); + if (c > max) max = c; + if (c < min) min = c; +#ifdef SUPPORT_UCP + needschar = TRUE; +#endif + } + else if (*cc == XCL_RANGE) + { + cc ++; + GETCHARINCTEST(c, cc); + if (c < min) min = c; + GETCHARINCTEST(c, cc); + if (c > max) max = c; +#ifdef SUPPORT_UCP + needschar = TRUE; +#endif + } +#ifdef SUPPORT_UCP + else + { + SLJIT_ASSERT(*cc == XCL_PROP || *cc == XCL_NOTPROP); + cc++; + if (*cc == PT_CLIST) + { + other_cases = PRIV(ucd_caseless_sets) + cc[1]; + while (*other_cases != NOTACHAR) + { + if (*other_cases > max) max = *other_cases; + if (*other_cases < min) min = *other_cases; + other_cases++; + } + } + else + { + max = READ_CHAR_MAX; + min = 0; + } + + switch(*cc) + { + case PT_ANY: + /* Any either accepts everything or ignored. */ + if (cc[-1] == XCL_PROP) + { + compile_char1_matchingpath(common, OP_ALLANY, cc, backtracks, FALSE); + if (list == backtracks) + add_jump(compiler, backtracks, JUMP(SLJIT_JUMP)); + return; + } + break; + + case PT_LAMP: + case PT_GC: + case PT_PC: + case PT_ALNUM: + needstype = TRUE; + break; + + case PT_SC: + needsscript = TRUE; + break; + + case PT_SPACE: + case PT_PXSPACE: + case PT_WORD: + case PT_PXGRAPH: + case PT_PXPRINT: + case PT_PXPUNCT: + needstype = TRUE; + needschar = TRUE; + break; + + case PT_CLIST: + case PT_UCNC: + needschar = TRUE; + break; + + default: + SLJIT_UNREACHABLE(); + break; + } + cc += 2; + } +#endif + } +SLJIT_ASSERT(compares > 0); + +/* We are not necessary in utf mode even in 8 bit mode. */ +cc = ccbegin; +read_char_range(common, min, max, (cc[-1] & XCL_NOT) != 0); + +if ((cc[-1] & XCL_HASPROP) == 0) + { + if ((cc[-1] & XCL_MAP) != 0) + { + jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255); + if (!check_class_ranges(common, (const sljit_u8 *)cc, (((const sljit_u8 *)cc)[31] & 0x80) != 0, TRUE, &found)) + { + OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7); + OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3); + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc); + OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0); + OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0); + add_jump(compiler, &found, JUMP(SLJIT_NOT_ZERO)); + } + + add_jump(compiler, backtracks, JUMP(SLJIT_JUMP)); + JUMPHERE(jump); + + cc += 32 / sizeof(pcre_uchar); + } + else + { + OP2(SLJIT_SUB, TMP2, 0, TMP1, 0, SLJIT_IMM, min); + add_jump(compiler, (cc[-1] & XCL_NOT) == 0 ? backtracks : &found, CMP(SLJIT_GREATER, TMP2, 0, SLJIT_IMM, max - min)); + } + } +else if ((cc[-1] & XCL_MAP) != 0) + { + OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0); +#ifdef SUPPORT_UCP + charsaved = TRUE; +#endif + if (!check_class_ranges(common, (const sljit_u8 *)cc, FALSE, TRUE, list)) + { +#ifdef COMPILE_PCRE8 + jump = NULL; + if (common->utf) +#endif + jump = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255); + + OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7); + OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3); + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc); + OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0); + OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0); + add_jump(compiler, list, JUMP(SLJIT_NOT_ZERO)); + +#ifdef COMPILE_PCRE8 + if (common->utf) +#endif + JUMPHERE(jump); + } + + OP1(SLJIT_MOV, TMP1, 0, RETURN_ADDR, 0); + cc += 32 / sizeof(pcre_uchar); + } + +#ifdef SUPPORT_UCP +if (needstype || needsscript) + { + if (needschar && !charsaved) + OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0); + +#ifdef COMPILE_PCRE32 + if (!common->utf) + { + jump = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0x10ffff + 1); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, INVALID_UTF_CHAR); + JUMPHERE(jump); + } +#endif + + OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT); + OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_stage1)); + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK); + OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT); + OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_stage2)); + OP1(SLJIT_MOV_U16, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1); + + /* Before anything else, we deal with scripts. */ + if (needsscript) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, script)); + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3); + + ccbegin = cc; + + while (*cc != XCL_END) + { + if (*cc == XCL_SINGLE) + { + cc ++; + GETCHARINCTEST(c, cc); + } + else if (*cc == XCL_RANGE) + { + cc ++; + GETCHARINCTEST(c, cc); + GETCHARINCTEST(c, cc); + } + else + { + SLJIT_ASSERT(*cc == XCL_PROP || *cc == XCL_NOTPROP); + cc++; + if (*cc == PT_SC) + { + compares--; + invertcmp = (compares == 0 && list != backtracks); + if (cc[-1] == XCL_NOTPROP) + invertcmp ^= 0x1; + jump = CMP(SLJIT_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (int)cc[1]); + add_jump(compiler, compares > 0 ? list : backtracks, jump); + } + cc += 2; + } + } + + cc = ccbegin; + } + + if (needschar) + { + OP1(SLJIT_MOV, TMP1, 0, RETURN_ADDR, 0); + } + + if (needstype) + { + if (!needschar) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype)); + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3); + } + else + { + OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 3); + OP1(SLJIT_MOV_U8, RETURN_ADDR, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype)); + typereg = RETURN_ADDR; + } + } + } +#endif + +/* Generating code. */ +charoffset = 0; +numberofcmps = 0; +#ifdef SUPPORT_UCP +typeoffset = 0; +#endif + +while (*cc != XCL_END) + { + compares--; + invertcmp = (compares == 0 && list != backtracks); + jump = NULL; + + if (*cc == XCL_SINGLE) + { + cc ++; + GETCHARINCTEST(c, cc); + + if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE)) + { + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset)); + OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + numberofcmps++; + } + else if (numberofcmps > 0) + { + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset)); + OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL); + jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp); + numberofcmps = 0; + } + else + { + jump = CMP(SLJIT_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset)); + numberofcmps = 0; + } + } + else if (*cc == XCL_RANGE) + { + cc ++; + GETCHARINCTEST(c, cc); + SET_CHAR_OFFSET(c); + GETCHARINCTEST(c, cc); + + if (numberofcmps < 3 && (*cc == XCL_SINGLE || *cc == XCL_RANGE)) + { + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset)); + OP_FLAGS(numberofcmps == 0 ? SLJIT_MOV : SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL); + numberofcmps++; + } + else if (numberofcmps > 0) + { + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset)); + OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_LESS_EQUAL); + jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp); + numberofcmps = 0; + } + else + { + jump = CMP(SLJIT_LESS_EQUAL ^ invertcmp, TMP1, 0, SLJIT_IMM, (sljit_sw)(c - charoffset)); + numberofcmps = 0; + } + } +#ifdef SUPPORT_UCP + else + { + SLJIT_ASSERT(*cc == XCL_PROP || *cc == XCL_NOTPROP); + if (*cc == XCL_NOTPROP) + invertcmp ^= 0x1; + cc++; + switch(*cc) + { + case PT_ANY: + if (!invertcmp) + jump = JUMP(SLJIT_JUMP); + break; + + case PT_LAMP: + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lu - typeoffset); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Ll - typeoffset); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lt - typeoffset); + OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_EQUAL); + jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp); + break; + + case PT_GC: + c = PRIV(ucp_typerange)[(int)cc[1] * 2]; + SET_TYPE_OFFSET(c); + jump = CMP(SLJIT_LESS_EQUAL ^ invertcmp, typereg, 0, SLJIT_IMM, PRIV(ucp_typerange)[(int)cc[1] * 2 + 1] - c); + break; + + case PT_PC: + jump = CMP(SLJIT_EQUAL ^ invertcmp, typereg, 0, SLJIT_IMM, (int)cc[1] - typeoffset); + break; + + case PT_SC: + compares++; + /* Do nothing. */ + break; + + case PT_SPACE: + case PT_PXSPACE: + SET_CHAR_OFFSET(9); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd - 0x9); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL); + + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x85 - 0x9); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x9); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + + SET_TYPE_OFFSET(ucp_Zl); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Zl); + OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_LESS_EQUAL); + jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp); + break; + + case PT_WORD: + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_UNDERSCORE - charoffset)); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL); + /* Fall through. */ + + case PT_ALNUM: + SET_TYPE_OFFSET(ucp_Ll); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Lu - ucp_Ll); + OP_FLAGS((*cc == PT_ALNUM) ? SLJIT_MOV : SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL); + SET_TYPE_OFFSET(ucp_Nd); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_No - ucp_Nd); + OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_LESS_EQUAL); + jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp); + break; + + case PT_CLIST: + other_cases = PRIV(ucd_caseless_sets) + cc[1]; + + /* At least three characters are required. + Otherwise this case would be handled by the normal code path. */ + SLJIT_ASSERT(other_cases[0] != NOTACHAR && other_cases[1] != NOTACHAR && other_cases[2] != NOTACHAR); + SLJIT_ASSERT(other_cases[0] < other_cases[1] && other_cases[1] < other_cases[2]); + + /* Optimizing character pairs, if their difference is power of 2. */ + if (is_powerof2(other_cases[1] ^ other_cases[0])) + { + if (charoffset == 0) + OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]); + else + { + OP2(SLJIT_ADD, TMP2, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)charoffset); + OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]); + } + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, other_cases[1]); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL); + other_cases += 2; + } + else if (is_powerof2(other_cases[2] ^ other_cases[1])) + { + if (charoffset == 0) + OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, other_cases[2] ^ other_cases[1]); + else + { + OP2(SLJIT_ADD, TMP2, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)charoffset); + OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, other_cases[1] ^ other_cases[0]); + } + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, other_cases[2]); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL); + + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(other_cases[0] - charoffset)); + OP_FLAGS(SLJIT_OR | ((other_cases[3] == NOTACHAR) ? SLJIT_SET_Z : 0), TMP2, 0, SLJIT_EQUAL); + + other_cases += 3; + } + else + { + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(*other_cases++ - charoffset)); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL); + } + + while (*other_cases != NOTACHAR) + { + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(*other_cases++ - charoffset)); + OP_FLAGS(SLJIT_OR | ((*other_cases == NOTACHAR) ? SLJIT_SET_Z : 0), TMP2, 0, SLJIT_EQUAL); + } + jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp); + break; + + case PT_UCNC: + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_DOLLAR_SIGN - charoffset)); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_EQUAL); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_COMMERCIAL_AT - charoffset)); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(CHAR_GRAVE_ACCENT - charoffset)); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + + SET_CHAR_OFFSET(0xa0); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (sljit_sw)(0xd7ff - charoffset)); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_LESS_EQUAL); + SET_CHAR_OFFSET(0); + OP2(SLJIT_SUB | SLJIT_SET_GREATER_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xe000 - 0); + OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_GREATER_EQUAL); + jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp); + break; + + case PT_PXGRAPH: + /* C and Z groups are the farthest two groups. */ + SET_TYPE_OFFSET(ucp_Ll); + OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_GREATER); + + jump = CMP(SLJIT_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll); + + /* In case of ucp_Cf, we overwrite the result. */ + SET_CHAR_OFFSET(0x2066); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL); + + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x2066); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + + JUMPHERE(jump); + jump = CMP(SLJIT_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0); + break; + + case PT_PXPRINT: + /* C and Z groups are the farthest two groups. */ + SET_TYPE_OFFSET(ucp_Ll); + OP2(SLJIT_SUB | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Ll); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_GREATER); + + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Zs - ucp_Ll); + OP_FLAGS(SLJIT_AND, TMP2, 0, SLJIT_NOT_EQUAL); + + jump = CMP(SLJIT_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll); + + /* In case of ucp_Cf, we overwrite the result. */ + SET_CHAR_OFFSET(0x2066); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL); + + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066); + OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL); + + JUMPHERE(jump); + jump = CMP(SLJIT_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0); + break; + + case PT_PXPUNCT: + SET_TYPE_OFFSET(ucp_Sc); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_So - ucp_Sc); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS_EQUAL); + + SET_CHAR_OFFSET(0); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x7f); + OP_FLAGS(SLJIT_AND, TMP2, 0, SLJIT_LESS_EQUAL); + + SET_TYPE_OFFSET(ucp_Pc); + OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, typereg, 0, SLJIT_IMM, ucp_Ps - ucp_Pc); + OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_LESS_EQUAL); + jump = JUMP(SLJIT_NOT_ZERO ^ invertcmp); + break; + + default: + SLJIT_UNREACHABLE(); + break; + } + cc += 2; + } +#endif + + if (jump != NULL) + add_jump(compiler, compares > 0 ? list : backtracks, jump); + } + +if (found != NULL) + set_jumps(found, LABEL()); +} + +#undef SET_TYPE_OFFSET +#undef SET_CHAR_OFFSET + +#endif + +static pcre_uchar *compile_simple_assertion_matchingpath(compiler_common *common, pcre_uchar type, pcre_uchar *cc, jump_list **backtracks) +{ +DEFINE_COMPILER; +int length; +struct sljit_jump *jump[4]; +#ifdef SUPPORT_UTF +struct sljit_label *label; +#endif /* SUPPORT_UTF */ + +switch(type) + { + case OP_SOD: + OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, TMP1, 0)); + return cc; + + case OP_SOM: + OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, TMP1, 0)); + return cc; + + case OP_NOT_WORD_BOUNDARY: + case OP_WORD_BOUNDARY: + add_jump(compiler, &common->wordboundary, JUMP(SLJIT_FAST_CALL)); + sljit_set_current_flags(compiler, SLJIT_SET_Z); + add_jump(compiler, backtracks, JUMP(type == OP_NOT_WORD_BOUNDARY ? SLJIT_NOT_ZERO : SLJIT_ZERO)); + return cc; + + case OP_EODN: + /* Requires rather complex checks. */ + jump[0] = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); + if (common->nltype == NLTYPE_FIXED && common->newline > 255) + { + OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2)); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); + if (common->mode == JIT_COMPILE) + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, STR_END, 0)); + else + { + jump[1] = CMP(SLJIT_EQUAL, TMP2, 0, STR_END, 0); + OP2(SLJIT_SUB | SLJIT_SET_LESS, SLJIT_UNUSED, 0, TMP2, 0, STR_END, 0); + OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_LESS); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff); + OP_FLAGS(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, SLJIT_NOT_EQUAL); + add_jump(compiler, backtracks, JUMP(SLJIT_NOT_EQUAL)); + check_partial(common, TRUE); + add_jump(compiler, backtracks, JUMP(SLJIT_JUMP)); + JUMPHERE(jump[1]); + } + OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff)); + } + else if (common->nltype == NLTYPE_FIXED) + { + OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, STR_END, 0)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline)); + } + else + { + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); + jump[1] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR); + OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2)); + OP2(SLJIT_SUB | SLJIT_SET_Z | SLJIT_SET_GREATER, SLJIT_UNUSED, 0, TMP2, 0, STR_END, 0); + jump[2] = JUMP(SLJIT_GREATER); + add_jump(compiler, backtracks, JUMP(SLJIT_NOT_EQUAL) /* LESS */); + /* Equal. */ + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1)); + jump[3] = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL); + add_jump(compiler, backtracks, JUMP(SLJIT_JUMP)); + + JUMPHERE(jump[1]); + if (common->nltype == NLTYPE_ANYCRLF) + { + OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP2, 0, STR_END, 0)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL)); + } + else + { + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, STR_PTR, 0); + read_char_range(common, common->nlmin, common->nlmax, TRUE); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, STR_END, 0)); + add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL)); + sljit_set_current_flags(compiler, SLJIT_SET_Z); + add_jump(compiler, backtracks, JUMP(SLJIT_ZERO)); + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1); + } + JUMPHERE(jump[2]); + JUMPHERE(jump[3]); + } + JUMPHERE(jump[0]); + check_partial(common, FALSE); + return cc; + + case OP_EOD: + add_jump(compiler, backtracks, CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0)); + check_partial(common, FALSE); + return cc; + + case OP_DOLL: + OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0); + OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, noteol)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0)); + + if (!common->endonly) + compile_simple_assertion_matchingpath(common, OP_EODN, cc, backtracks); + else + { + add_jump(compiler, backtracks, CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0)); + check_partial(common, FALSE); + } + return cc; + + case OP_DOLLM: + jump[1] = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0); + OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0); + OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, noteol)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0)); + check_partial(common, FALSE); + jump[0] = JUMP(SLJIT_JUMP); + JUMPHERE(jump[1]); + + if (common->nltype == NLTYPE_FIXED && common->newline > 255) + { + OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2)); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); + if (common->mode == JIT_COMPILE) + add_jump(compiler, backtracks, CMP(SLJIT_GREATER, TMP2, 0, STR_END, 0)); + else + { + jump[1] = CMP(SLJIT_LESS_EQUAL, TMP2, 0, STR_END, 0); + /* STR_PTR = STR_END - IN_UCHARS(1) */ + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff)); + check_partial(common, TRUE); + add_jump(compiler, backtracks, JUMP(SLJIT_JUMP)); + JUMPHERE(jump[1]); + } + + OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff)); + } + else + { + peek_char(common, common->nlmax); + check_newlinechar(common, common->nltype, backtracks, FALSE); + } + JUMPHERE(jump[0]); + return cc; + + case OP_CIRC: + OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, begin)); + add_jump(compiler, backtracks, CMP(SLJIT_GREATER, STR_PTR, 0, TMP1, 0)); + OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, notbol)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0)); + return cc; + + case OP_CIRCM: + OP1(SLJIT_MOV, TMP2, 0, ARGUMENTS, 0); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, begin)); + jump[1] = CMP(SLJIT_GREATER, STR_PTR, 0, TMP1, 0); + OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(jit_arguments, notbol)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0)); + jump[0] = JUMP(SLJIT_JUMP); + JUMPHERE(jump[1]); + + add_jump(compiler, backtracks, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0)); + if (common->nltype == NLTYPE_FIXED && common->newline > 255) + { + OP2(SLJIT_SUB, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2)); + add_jump(compiler, backtracks, CMP(SLJIT_LESS, TMP2, 0, TMP1, 0)); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-2)); + OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff)); + } + else + { + skip_char_back(common); + read_char_range(common, common->nlmin, common->nlmax, TRUE); + check_newlinechar(common, common->nltype, backtracks, FALSE); + } + JUMPHERE(jump[0]); + return cc; + + case OP_REVERSE: + length = GET(cc, 0); + if (length == 0) + return cc + LINK_SIZE; + OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0); +#ifdef SUPPORT_UTF + if (common->utf) + { + OP1(SLJIT_MOV, TMP3, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin)); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, length); + label = LABEL(); + add_jump(compiler, backtracks, CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP3, 0)); + skip_char_back(common); + OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, TMP2, 0, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, label); + } + else +#endif + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin)); + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(length)); + add_jump(compiler, backtracks, CMP(SLJIT_LESS, STR_PTR, 0, TMP1, 0)); + } + check_start_used_ptr(common); + return cc + LINK_SIZE; + } +SLJIT_UNREACHABLE(); +return cc; +} + +static pcre_uchar *compile_char1_matchingpath(compiler_common *common, pcre_uchar type, pcre_uchar *cc, jump_list **backtracks, BOOL check_str_ptr) +{ +DEFINE_COMPILER; +int length; +unsigned int c, oc, bit; +compare_context context; +struct sljit_jump *jump[3]; +jump_list *end_list; +#ifdef SUPPORT_UTF +struct sljit_label *label; +#ifdef SUPPORT_UCP +pcre_uchar propdata[5]; +#endif +#endif /* SUPPORT_UTF */ + +switch(type) + { + case OP_NOT_DIGIT: + case OP_DIGIT: + /* Digits are usually 0-9, so it is worth to optimize them. */ + if (check_str_ptr) + detect_partial_match(common, backtracks); +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 + if (common->utf && is_char7_bitset((const sljit_u8 *)common->ctypes - cbit_length + cbit_digit, FALSE)) + read_char7_type(common, type == OP_NOT_DIGIT); + else +#endif + read_char8_type(common, type == OP_NOT_DIGIT); + /* Flip the starting bit in the negative case. */ + OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_digit); + add_jump(compiler, backtracks, JUMP(type == OP_DIGIT ? SLJIT_ZERO : SLJIT_NOT_ZERO)); + return cc; + + case OP_NOT_WHITESPACE: + case OP_WHITESPACE: + if (check_str_ptr) + detect_partial_match(common, backtracks); +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 + if (common->utf && is_char7_bitset((const sljit_u8 *)common->ctypes - cbit_length + cbit_space, FALSE)) + read_char7_type(common, type == OP_NOT_WHITESPACE); + else +#endif + read_char8_type(common, type == OP_NOT_WHITESPACE); + OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_space); + add_jump(compiler, backtracks, JUMP(type == OP_WHITESPACE ? SLJIT_ZERO : SLJIT_NOT_ZERO)); + return cc; + + case OP_NOT_WORDCHAR: + case OP_WORDCHAR: + if (check_str_ptr) + detect_partial_match(common, backtracks); +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 + if (common->utf && is_char7_bitset((const sljit_u8 *)common->ctypes - cbit_length + cbit_word, FALSE)) + read_char7_type(common, type == OP_NOT_WORDCHAR); + else +#endif + read_char8_type(common, type == OP_NOT_WORDCHAR); + OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ctype_word); + add_jump(compiler, backtracks, JUMP(type == OP_WORDCHAR ? SLJIT_ZERO : SLJIT_NOT_ZERO)); + return cc; + + case OP_ANY: + if (check_str_ptr) + detect_partial_match(common, backtracks); + read_char_range(common, common->nlmin, common->nlmax, TRUE); + if (common->nltype == NLTYPE_FIXED && common->newline > 255) + { + jump[0] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff); + end_list = NULL; + if (common->mode != JIT_PARTIAL_HARD_COMPILE) + add_jump(compiler, &end_list, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0)); + else + check_str_end(common, &end_list); + + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0); + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline & 0xff)); + set_jumps(end_list, LABEL()); + JUMPHERE(jump[0]); + } + else + check_newlinechar(common, common->nltype, backtracks, TRUE); + return cc; + + case OP_ALLANY: + if (check_str_ptr) + detect_partial_match(common, backtracks); +#ifdef SUPPORT_UTF + if (common->utf) + { + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); +#if defined COMPILE_PCRE8 || defined COMPILE_PCRE16 +#if defined COMPILE_PCRE8 + jump[0] = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0); + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0); +#elif defined COMPILE_PCRE16 + jump[0] = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xd800); + OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800); + OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_EQUAL); + OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0); +#endif + JUMPHERE(jump[0]); +#endif /* COMPILE_PCRE[8|16] */ + return cc; + } +#endif + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + return cc; + + case OP_ANYBYTE: + if (check_str_ptr) + detect_partial_match(common, backtracks); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + return cc; + +#ifdef SUPPORT_UTF +#ifdef SUPPORT_UCP + case OP_NOTPROP: + case OP_PROP: + propdata[0] = XCL_HASPROP; + propdata[1] = type == OP_NOTPROP ? XCL_NOTPROP : XCL_PROP; + propdata[2] = cc[0]; + propdata[3] = cc[1]; + propdata[4] = XCL_END; + if (check_str_ptr) + detect_partial_match(common, backtracks); + compile_xclass_matchingpath(common, propdata, backtracks); + return cc + 2; +#endif +#endif + + case OP_ANYNL: + if (check_str_ptr) + detect_partial_match(common, backtracks); + read_char_range(common, common->bsr_nlmin, common->bsr_nlmax, FALSE); + jump[0] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_CR); + /* We don't need to handle soft partial matching case. */ + end_list = NULL; + if (common->mode != JIT_PARTIAL_HARD_COMPILE) + add_jump(compiler, &end_list, CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0)); + else + check_str_end(common, &end_list); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0); + jump[1] = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_NL); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + jump[2] = JUMP(SLJIT_JUMP); + JUMPHERE(jump[0]); + check_newlinechar(common, common->bsr_nltype, backtracks, FALSE); + set_jumps(end_list, LABEL()); + JUMPHERE(jump[1]); + JUMPHERE(jump[2]); + return cc; + + case OP_NOT_HSPACE: + case OP_HSPACE: + if (check_str_ptr) + detect_partial_match(common, backtracks); + read_char_range(common, 0x9, 0x3000, type == OP_NOT_HSPACE); + add_jump(compiler, &common->hspace, JUMP(SLJIT_FAST_CALL)); + sljit_set_current_flags(compiler, SLJIT_SET_Z); + add_jump(compiler, backtracks, JUMP(type == OP_NOT_HSPACE ? SLJIT_NOT_ZERO : SLJIT_ZERO)); + return cc; + + case OP_NOT_VSPACE: + case OP_VSPACE: + if (check_str_ptr) + detect_partial_match(common, backtracks); + read_char_range(common, 0xa, 0x2029, type == OP_NOT_VSPACE); + add_jump(compiler, &common->vspace, JUMP(SLJIT_FAST_CALL)); + sljit_set_current_flags(compiler, SLJIT_SET_Z); + add_jump(compiler, backtracks, JUMP(type == OP_NOT_VSPACE ? SLJIT_NOT_ZERO : SLJIT_ZERO)); + return cc; + +#ifdef SUPPORT_UCP + case OP_EXTUNI: + if (check_str_ptr) + detect_partial_match(common, backtracks); + read_char(common); + add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL)); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, gbprop)); + /* Optimize register allocation: use a real register. */ + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, STACK_TOP, 0); + OP1(SLJIT_MOV_U8, STACK_TOP, 0, SLJIT_MEM2(TMP1, TMP2), 3); + + label = LABEL(); + jump[0] = CMP(SLJIT_GREATER_EQUAL, STR_PTR, 0, STR_END, 0); + OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0); + read_char(common); + add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL)); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, gbprop)); + OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM2(TMP1, TMP2), 3); + + OP2(SLJIT_SHL, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, 2); + OP1(SLJIT_MOV_U32, TMP1, 0, SLJIT_MEM1(STACK_TOP), (sljit_sw)PRIV(ucp_gbtable)); + OP1(SLJIT_MOV, STACK_TOP, 0, TMP2, 0); + OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0); + OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0); + JUMPTO(SLJIT_NOT_ZERO, label); + + OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0); + JUMPHERE(jump[0]); + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0); + + if (common->mode == JIT_PARTIAL_HARD_COMPILE) + { + jump[0] = CMP(SLJIT_LESS, STR_PTR, 0, STR_END, 0); + /* Since we successfully read a char above, partial matching must occure. */ + check_partial(common, TRUE); + JUMPHERE(jump[0]); + } + return cc; +#endif + + case OP_CHAR: + case OP_CHARI: + length = 1; +#ifdef SUPPORT_UTF + if (common->utf && HAS_EXTRALEN(*cc)) length += GET_EXTRALEN(*cc); +#endif + if (common->mode == JIT_COMPILE && check_str_ptr + && (type == OP_CHAR || !char_has_othercase(common, cc) || char_get_othercase_bit(common, cc) != 0)) + { + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(length)); + add_jump(compiler, backtracks, CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0)); + + context.length = IN_UCHARS(length); + context.sourcereg = -1; +#if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED + context.ucharptr = 0; +#endif + return byte_sequence_compare(common, type == OP_CHARI, cc, &context, backtracks); + } + + if (check_str_ptr) + detect_partial_match(common, backtracks); +#ifdef SUPPORT_UTF + if (common->utf) + { + GETCHAR(c, cc); + } + else +#endif + c = *cc; + + if (type == OP_CHAR || !char_has_othercase(common, cc)) + { + read_char_range(common, c, c, FALSE); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c)); + return cc + length; + } + oc = char_othercase(common, c); + read_char_range(common, c < oc ? c : oc, c > oc ? c : oc, FALSE); + bit = c ^ oc; + if (is_powerof2(bit)) + { + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, bit); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, c | bit)); + return cc + length; + } + jump[0] = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, oc)); + JUMPHERE(jump[0]); + return cc + length; + + case OP_NOT: + case OP_NOTI: + if (check_str_ptr) + detect_partial_match(common, backtracks); + length = 1; +#ifdef SUPPORT_UTF + if (common->utf) + { +#ifdef COMPILE_PCRE8 + c = *cc; + if (c < 128) + { + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(STR_PTR), 0); + if (type == OP_NOT || !char_has_othercase(common, cc)) + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c)); + else + { + /* Since UTF8 code page is fixed, we know that c is in [a-z] or [A-Z] range. */ + OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x20); + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, c | 0x20)); + } + /* Skip the variable-length character. */ + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + jump[0] = CMP(SLJIT_LESS, TMP1, 0, SLJIT_IMM, 0xc0); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0); + JUMPHERE(jump[0]); + return cc + 1; + } + else +#endif /* COMPILE_PCRE8 */ + { + GETCHARLEN(c, cc, length); + } + } + else +#endif /* SUPPORT_UTF */ + c = *cc; + + if (type == OP_NOT || !char_has_othercase(common, cc)) + { + read_char_range(common, c, c, TRUE); + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c)); + } + else + { + oc = char_othercase(common, c); + read_char_range(common, c < oc ? c : oc, c > oc ? c : oc, TRUE); + bit = c ^ oc; + if (is_powerof2(bit)) + { + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, bit); + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c | bit)); + } + else + { + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, c)); + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, oc)); + } + } + return cc + length; + + case OP_CLASS: + case OP_NCLASS: + if (check_str_ptr) + detect_partial_match(common, backtracks); + +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 + bit = (common->utf && is_char7_bitset((const sljit_u8 *)cc, type == OP_NCLASS)) ? 127 : 255; + read_char_range(common, 0, bit, type == OP_NCLASS); +#else + read_char_range(common, 0, 255, type == OP_NCLASS); +#endif + + if (check_class_ranges(common, (const sljit_u8 *)cc, type == OP_NCLASS, FALSE, backtracks)) + return cc + 32 / sizeof(pcre_uchar); + +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 + jump[0] = NULL; + if (common->utf) + { + jump[0] = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, bit); + if (type == OP_CLASS) + { + add_jump(compiler, backtracks, jump[0]); + jump[0] = NULL; + } + } +#elif !defined COMPILE_PCRE8 + jump[0] = CMP(SLJIT_GREATER, TMP1, 0, SLJIT_IMM, 255); + if (type == OP_CLASS) + { + add_jump(compiler, backtracks, jump[0]); + jump[0] = NULL; + } +#endif /* SUPPORT_UTF && COMPILE_PCRE8 */ + + OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7); + OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3); + OP1(SLJIT_MOV_U8, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)cc); + OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0); + OP2(SLJIT_AND | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0); + add_jump(compiler, backtracks, JUMP(SLJIT_ZERO)); + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + if (jump[0] != NULL) + JUMPHERE(jump[0]); +#endif + return cc + 32 / sizeof(pcre_uchar); + +#if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + case OP_XCLASS: + if (check_str_ptr) + detect_partial_match(common, backtracks); + compile_xclass_matchingpath(common, cc + LINK_SIZE, backtracks); + return cc + GET(cc, 0) - 1; +#endif + } +SLJIT_UNREACHABLE(); +return cc; +} + +static SLJIT_INLINE pcre_uchar *compile_charn_matchingpath(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, jump_list **backtracks) +{ +/* This function consumes at least one input character. */ +/* To decrease the number of length checks, we try to concatenate the fixed length character sequences. */ +DEFINE_COMPILER; +pcre_uchar *ccbegin = cc; +compare_context context; +int size; + +context.length = 0; +do + { + if (cc >= ccend) + break; + + if (*cc == OP_CHAR) + { + size = 1; +#ifdef SUPPORT_UTF + if (common->utf && HAS_EXTRALEN(cc[1])) + size += GET_EXTRALEN(cc[1]); +#endif + } + else if (*cc == OP_CHARI) + { + size = 1; +#ifdef SUPPORT_UTF + if (common->utf) + { + if (char_has_othercase(common, cc + 1) && char_get_othercase_bit(common, cc + 1) == 0) + size = 0; + else if (HAS_EXTRALEN(cc[1])) + size += GET_EXTRALEN(cc[1]); + } + else +#endif + if (char_has_othercase(common, cc + 1) && char_get_othercase_bit(common, cc + 1) == 0) + size = 0; + } + else + size = 0; + + cc += 1 + size; + context.length += IN_UCHARS(size); + } +while (size > 0 && context.length <= 128); + +cc = ccbegin; +if (context.length > 0) + { + /* We have a fixed-length byte sequence. */ + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, context.length); + add_jump(compiler, backtracks, CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0)); + + context.sourcereg = -1; +#if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED + context.ucharptr = 0; +#endif + do cc = byte_sequence_compare(common, *cc == OP_CHARI, cc + 1, &context, backtracks); while (context.length > 0); + return cc; + } + +/* A non-fixed length character will be checked if length == 0. */ +return compile_char1_matchingpath(common, *cc, cc + 1, backtracks, TRUE); +} + +/* Forward definitions. */ +static void compile_matchingpath(compiler_common *, pcre_uchar *, pcre_uchar *, backtrack_common *); +static void compile_backtrackingpath(compiler_common *, struct backtrack_common *); + +#define PUSH_BACKTRACK(size, ccstart, error) \ + do \ + { \ + backtrack = sljit_alloc_memory(compiler, (size)); \ + if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) \ + return error; \ + memset(backtrack, 0, size); \ + backtrack->prev = parent->top; \ + backtrack->cc = (ccstart); \ + parent->top = backtrack; \ + } \ + while (0) + +#define PUSH_BACKTRACK_NOVALUE(size, ccstart) \ + do \ + { \ + backtrack = sljit_alloc_memory(compiler, (size)); \ + if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) \ + return; \ + memset(backtrack, 0, size); \ + backtrack->prev = parent->top; \ + backtrack->cc = (ccstart); \ + parent->top = backtrack; \ + } \ + while (0) + +#define BACKTRACK_AS(type) ((type *)backtrack) + +static void compile_dnref_search(compiler_common *common, pcre_uchar *cc, jump_list **backtracks) +{ +/* The OVECTOR offset goes to TMP2. */ +DEFINE_COMPILER; +int count = GET2(cc, 1 + IMM2_SIZE); +pcre_uchar *slot = common->name_table + GET2(cc, 1) * common->name_entry_size; +unsigned int offset; +jump_list *found = NULL; + +SLJIT_ASSERT(*cc == OP_DNREF || *cc == OP_DNREFI); + +OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1)); + +count--; +while (count-- > 0) + { + offset = GET2(slot, 0) << 1; + GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset)); + add_jump(compiler, &found, CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0)); + slot += common->name_entry_size; + } + +offset = GET2(slot, 0) << 1; +GET_LOCAL_BASE(TMP2, 0, OVECTOR(offset)); +if (backtracks != NULL && !common->jscript_compat) + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0)); + +set_jumps(found, LABEL()); +} + +static void compile_ref_matchingpath(compiler_common *common, pcre_uchar *cc, jump_list **backtracks, BOOL withchecks, BOOL emptyfail) +{ +DEFINE_COMPILER; +BOOL ref = (*cc == OP_REF || *cc == OP_REFI); +int offset = 0; +struct sljit_jump *jump = NULL; +struct sljit_jump *partial; +struct sljit_jump *nopartial; + +if (ref) + { + offset = GET2(cc, 1) << 1; + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset)); + /* OVECTOR(1) contains the "string begin - 1" constant. */ + if (withchecks && !common->jscript_compat) + add_jump(compiler, backtracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1))); + } +else + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0); + +#if defined SUPPORT_UTF && defined SUPPORT_UCP +if (common->utf && *cc == OP_REFI) + { + SLJIT_ASSERT(TMP1 == SLJIT_R0 && STACK_TOP == SLJIT_R1); + if (ref) + OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1)); + else + OP1(SLJIT_MOV, SLJIT_R2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw)); + + if (withchecks) + jump = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_R2, 0); + + /* No free saved registers so save data on stack. */ + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, STACK_TOP, 0); + OP1(SLJIT_MOV, SLJIT_R1, 0, STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_R3, 0, STR_END, 0); + sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW) | SLJIT_ARG3(SW) | SLJIT_ARG4(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(do_utf_caselesscmp)); + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0); + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_RETURN_REG, 0); + + if (common->mode == JIT_COMPILE) + add_jump(compiler, backtracks, CMP(SLJIT_LESS_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1)); + else + { + OP2(SLJIT_SUB | SLJIT_SET_Z | SLJIT_SET_LESS, SLJIT_UNUSED, 0, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1); + + add_jump(compiler, backtracks, JUMP(SLJIT_LESS)); + + nopartial = JUMP(SLJIT_NOT_EQUAL); + OP1(SLJIT_MOV, STR_PTR, 0, STR_END, 0); + check_partial(common, FALSE); + add_jump(compiler, backtracks, JUMP(SLJIT_JUMP)); + JUMPHERE(nopartial); + } + } +else +#endif /* SUPPORT_UTF && SUPPORT_UCP */ + { + if (ref) + OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP1, 0); + else + OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw), TMP1, 0); + + if (withchecks) + jump = JUMP(SLJIT_ZERO); + + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0); + partial = CMP(SLJIT_GREATER, STR_PTR, 0, STR_END, 0); + if (common->mode == JIT_COMPILE) + add_jump(compiler, backtracks, partial); + + add_jump(compiler, *cc == OP_REF ? &common->casefulcmp : &common->caselesscmp, JUMP(SLJIT_FAST_CALL)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0)); + + if (common->mode != JIT_COMPILE) + { + nopartial = JUMP(SLJIT_JUMP); + JUMPHERE(partial); + /* TMP2 -= STR_END - STR_PTR */ + OP2(SLJIT_SUB, TMP2, 0, TMP2, 0, STR_PTR, 0); + OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, STR_END, 0); + partial = CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, 0); + OP1(SLJIT_MOV, STR_PTR, 0, STR_END, 0); + add_jump(compiler, *cc == OP_REF ? &common->casefulcmp : &common->caselesscmp, JUMP(SLJIT_FAST_CALL)); + add_jump(compiler, backtracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0)); + JUMPHERE(partial); + check_partial(common, FALSE); + add_jump(compiler, backtracks, JUMP(SLJIT_JUMP)); + JUMPHERE(nopartial); + } + } + +if (jump != NULL) + { + if (emptyfail) + add_jump(compiler, backtracks, jump); + else + JUMPHERE(jump); + } +} + +static SLJIT_INLINE pcre_uchar *compile_ref_iterator_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent) +{ +DEFINE_COMPILER; +BOOL ref = (*cc == OP_REF || *cc == OP_REFI); +backtrack_common *backtrack; +pcre_uchar type; +int offset = 0; +struct sljit_label *label; +struct sljit_jump *zerolength; +struct sljit_jump *jump = NULL; +pcre_uchar *ccbegin = cc; +int min = 0, max = 0; +BOOL minimize; + +PUSH_BACKTRACK(sizeof(ref_iterator_backtrack), cc, NULL); + +if (ref) + offset = GET2(cc, 1) << 1; +else + cc += IMM2_SIZE; +type = cc[1 + IMM2_SIZE]; + +SLJIT_COMPILE_ASSERT((OP_CRSTAR & 0x1) == 0, crstar_opcode_must_be_even); +minimize = (type & 0x1) != 0; +switch(type) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + min = 0; + max = 0; + cc += 1 + IMM2_SIZE + 1; + break; + case OP_CRPLUS: + case OP_CRMINPLUS: + min = 1; + max = 0; + cc += 1 + IMM2_SIZE + 1; + break; + case OP_CRQUERY: + case OP_CRMINQUERY: + min = 0; + max = 1; + cc += 1 + IMM2_SIZE + 1; + break; + case OP_CRRANGE: + case OP_CRMINRANGE: + min = GET2(cc, 1 + IMM2_SIZE + 1); + max = GET2(cc, 1 + IMM2_SIZE + 1 + IMM2_SIZE); + cc += 1 + IMM2_SIZE + 1 + 2 * IMM2_SIZE; + break; + default: + SLJIT_UNREACHABLE(); + break; + } + +if (!minimize) + { + if (min == 0) + { + allocate_stack(common, 2); + if (ref) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset)); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0); + /* Temporary release of STR_PTR. */ + OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw)); + /* Handles both invalid and empty cases. Since the minimum repeat, + is zero the invalid case is basically the same as an empty case. */ + if (ref) + zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1)); + else + { + compile_dnref_search(common, ccbegin, NULL); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, TMP2, 0); + zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw)); + } + /* Restore if not zero length. */ + OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw)); + } + else + { + allocate_stack(common, 1); + if (ref) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset)); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); + if (ref) + { + add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1))); + zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1)); + } + else + { + compile_dnref_search(common, ccbegin, &backtrack->topbacktracks); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, TMP2, 0); + zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw)); + } + } + + if (min > 1 || max > 1) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, SLJIT_IMM, 0); + + label = LABEL(); + if (!ref) + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1); + compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, FALSE, FALSE); + + if (min > 1 || max > 1) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0); + OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, TMP1, 0); + if (min > 1) + CMPTO(SLJIT_LESS, TMP1, 0, SLJIT_IMM, min, label); + if (max > 1) + { + jump = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, max); + allocate_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + JUMPTO(SLJIT_JUMP, label); + JUMPHERE(jump); + } + } + + if (max == 0) + { + /* Includes min > 1 case as well. */ + allocate_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + JUMPTO(SLJIT_JUMP, label); + } + + JUMPHERE(zerolength); + BACKTRACK_AS(ref_iterator_backtrack)->matchingpath = LABEL(); + + count_match(common); + return cc; + } + +allocate_stack(common, ref ? 2 : 3); +if (ref) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset)); +OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); +if (type != OP_CRMINSTAR) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0); + +if (min == 0) + { + /* Handles both invalid and empty cases. Since the minimum repeat, + is zero the invalid case is basically the same as an empty case. */ + if (ref) + zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1)); + else + { + compile_dnref_search(common, ccbegin, NULL); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0); + zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw)); + } + /* Length is non-zero, we can match real repeats. */ + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + jump = JUMP(SLJIT_JUMP); + } +else + { + if (ref) + { + add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1))); + zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1)); + } + else + { + compile_dnref_search(common, ccbegin, &backtrack->topbacktracks); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0); + zerolength = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_MEM1(TMP2), sizeof(sljit_sw)); + } + } + +BACKTRACK_AS(ref_iterator_backtrack)->matchingpath = LABEL(); +if (max > 0) + add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_GREATER_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, max)); + +if (!ref) + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(2)); +compile_ref_matchingpath(common, ccbegin, &backtrack->topbacktracks, TRUE, TRUE); +OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + +if (min > 1) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(1)); + OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0); + CMPTO(SLJIT_LESS, TMP1, 0, SLJIT_IMM, min, BACKTRACK_AS(ref_iterator_backtrack)->matchingpath); + } +else if (max > 0) + OP2(SLJIT_ADD, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 1); + +if (jump != NULL) + JUMPHERE(jump); +JUMPHERE(zerolength); + +count_match(common); +return cc; +} + +static SLJIT_INLINE pcre_uchar *compile_recurse_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent) +{ +DEFINE_COMPILER; +backtrack_common *backtrack; +recurse_entry *entry = common->entries; +recurse_entry *prev = NULL; +sljit_sw start = GET(cc, 1); +pcre_uchar *start_cc; +BOOL needs_control_head; + +PUSH_BACKTRACK(sizeof(recurse_backtrack), cc, NULL); + +/* Inlining simple patterns. */ +if (get_framesize(common, common->start + start, NULL, TRUE, &needs_control_head) == no_stack) + { + start_cc = common->start + start; + compile_matchingpath(common, next_opcode(common, start_cc), bracketend(start_cc) - (1 + LINK_SIZE), backtrack); + BACKTRACK_AS(recurse_backtrack)->inlined_pattern = TRUE; + return cc + 1 + LINK_SIZE; + } + +while (entry != NULL) + { + if (entry->start == start) + break; + prev = entry; + entry = entry->next; + } + +if (entry == NULL) + { + entry = sljit_alloc_memory(compiler, sizeof(recurse_entry)); + if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + return NULL; + entry->next = NULL; + entry->entry = NULL; + entry->calls = NULL; + entry->start = start; + + if (prev != NULL) + prev->next = entry; + else + common->entries = entry; + } + +if (common->has_set_som && common->mark_ptr != 0) + { + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0)); + allocate_stack(common, 2); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0); + } +else if (common->has_set_som || common->mark_ptr != 0) + { + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->has_set_som ? (int)(OVECTOR(0)) : common->mark_ptr); + allocate_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0); + } + +if (entry->entry == NULL) + add_jump(compiler, &entry->calls, JUMP(SLJIT_FAST_CALL)); +else + JUMPTO(SLJIT_FAST_CALL, entry->entry); +/* Leave if the match is failed. */ +add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0)); +return cc + 1 + LINK_SIZE; +} + +static sljit_s32 SLJIT_FUNC do_callout(struct jit_arguments *arguments, PUBL(callout_block) *callout_block, pcre_uchar **jit_ovector) +{ +const pcre_uchar *begin = arguments->begin; +int *offset_vector = arguments->offsets; +int offset_count = arguments->offset_count; +int i; + +if (PUBL(callout) == NULL) + return 0; + +callout_block->version = 2; +callout_block->callout_data = arguments->callout_data; + +/* Offsets in subject. */ +callout_block->subject_length = arguments->end - arguments->begin; +callout_block->start_match = (pcre_uchar*)callout_block->subject - arguments->begin; +callout_block->current_position = (pcre_uchar*)callout_block->offset_vector - arguments->begin; +#if defined COMPILE_PCRE8 +callout_block->subject = (PCRE_SPTR)begin; +#elif defined COMPILE_PCRE16 +callout_block->subject = (PCRE_SPTR16)begin; +#elif defined COMPILE_PCRE32 +callout_block->subject = (PCRE_SPTR32)begin; +#endif + +/* Convert and copy the JIT offset vector to the offset_vector array. */ +callout_block->capture_top = 0; +callout_block->offset_vector = offset_vector; +for (i = 2; i < offset_count; i += 2) + { + offset_vector[i] = jit_ovector[i] - begin; + offset_vector[i + 1] = jit_ovector[i + 1] - begin; + if (jit_ovector[i] >= begin) + callout_block->capture_top = i; + } + +callout_block->capture_top = (callout_block->capture_top >> 1) + 1; +if (offset_count > 0) + offset_vector[0] = -1; +if (offset_count > 1) + offset_vector[1] = -1; +return (*PUBL(callout))(callout_block); +} + +/* Aligning to 8 byte. */ +#define CALLOUT_ARG_SIZE \ + (((int)sizeof(PUBL(callout_block)) + 7) & ~7) + +#define CALLOUT_ARG_OFFSET(arg) \ + SLJIT_OFFSETOF(PUBL(callout_block), arg) + +static SLJIT_INLINE pcre_uchar *compile_callout_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent) +{ +DEFINE_COMPILER; +backtrack_common *backtrack; + +PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL); + +allocate_stack(common, CALLOUT_ARG_SIZE / sizeof(sljit_sw)); + +SLJIT_ASSERT(common->capture_last_ptr != 0); +OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr); +OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0); +OP1(SLJIT_MOV_S32, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(callout_number), SLJIT_IMM, cc[1]); +OP1(SLJIT_MOV_S32, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(capture_last), TMP2, 0); + +/* These pointer sized fields temporarly stores internal variables. */ +OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0)); +OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(offset_vector), STR_PTR, 0); +OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(subject), TMP2, 0); + +if (common->mark_ptr != 0) + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr)); +OP1(SLJIT_MOV_S32, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(pattern_position), SLJIT_IMM, GET(cc, 2)); +OP1(SLJIT_MOV_S32, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(next_item_length), SLJIT_IMM, GET(cc, 2 + LINK_SIZE)); +OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), CALLOUT_ARG_OFFSET(mark), (common->mark_ptr != 0) ? TMP2 : SLJIT_IMM, 0); + +/* Needed to save important temporary registers. */ +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, STACK_TOP, 0); +/* SLJIT_R0 = arguments */ +OP1(SLJIT_MOV, SLJIT_R1, 0, STACK_TOP, 0); +GET_LOCAL_BASE(SLJIT_R2, 0, OVECTOR_START); +sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(S32) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW) | SLJIT_ARG3(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(do_callout)); +OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0); +free_stack(common, CALLOUT_ARG_SIZE / sizeof(sljit_sw)); + +/* Check return value. */ +OP2(SLJIT_SUB32 | SLJIT_SET_Z | SLJIT_SET_SIG_GREATER, SLJIT_UNUSED, 0, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0); +add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_SIG_GREATER32)); +if (common->forced_quit_label == NULL) + add_jump(compiler, &common->forced_quit, JUMP(SLJIT_NOT_EQUAL32) /* SIG_LESS */); +else + JUMPTO(SLJIT_NOT_EQUAL32 /* SIG_LESS */, common->forced_quit_label); +return cc + 2 + 2 * LINK_SIZE; +} + +#undef CALLOUT_ARG_SIZE +#undef CALLOUT_ARG_OFFSET + +static SLJIT_INLINE BOOL assert_needs_str_ptr_saving(pcre_uchar *cc) +{ +while (TRUE) + { + switch (*cc) + { + case OP_NOT_WORD_BOUNDARY: + case OP_WORD_BOUNDARY: + case OP_CIRC: + case OP_CIRCM: + case OP_DOLL: + case OP_DOLLM: + case OP_CALLOUT: + case OP_ALT: + cc += PRIV(OP_lengths)[*cc]; + break; + + case OP_KET: + return FALSE; + + default: + return TRUE; + } + } +} + +static pcre_uchar *compile_assert_matchingpath(compiler_common *common, pcre_uchar *cc, assert_backtrack *backtrack, BOOL conditional) +{ +DEFINE_COMPILER; +int framesize; +int extrasize; +BOOL needs_control_head; +int private_data_ptr; +backtrack_common altbacktrack; +pcre_uchar *ccbegin; +pcre_uchar opcode; +pcre_uchar bra = OP_BRA; +jump_list *tmp = NULL; +jump_list **target = (conditional) ? &backtrack->condfailed : &backtrack->common.topbacktracks; +jump_list **found; +/* Saving previous accept variables. */ +BOOL save_local_exit = common->local_exit; +BOOL save_positive_assert = common->positive_assert; +then_trap_backtrack *save_then_trap = common->then_trap; +struct sljit_label *save_quit_label = common->quit_label; +struct sljit_label *save_accept_label = common->accept_label; +jump_list *save_quit = common->quit; +jump_list *save_positive_assert_quit = common->positive_assert_quit; +jump_list *save_accept = common->accept; +struct sljit_jump *jump; +struct sljit_jump *brajump = NULL; + +/* Assert captures then. */ +common->then_trap = NULL; + +if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO) + { + SLJIT_ASSERT(!conditional); + bra = *cc; + cc++; + } +private_data_ptr = PRIVATE_DATA(cc); +SLJIT_ASSERT(private_data_ptr != 0); +framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head); +backtrack->framesize = framesize; +backtrack->private_data_ptr = private_data_ptr; +opcode = *cc; +SLJIT_ASSERT(opcode >= OP_ASSERT && opcode <= OP_ASSERTBACK_NOT); +found = (opcode == OP_ASSERT || opcode == OP_ASSERTBACK) ? &tmp : target; +ccbegin = cc; +cc += GET(cc, 1); + +if (bra == OP_BRAMINZERO) + { + /* This is a braminzero backtrack path. */ + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + free_stack(common, 1); + brajump = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0); + } + +if (framesize < 0) + { + extrasize = 1; + if (bra == OP_BRA && !assert_needs_str_ptr_saving(ccbegin + 1 + LINK_SIZE)) + extrasize = 0; + + if (needs_control_head) + extrasize++; + + if (framesize == no_frame) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0); + + if (extrasize > 0) + allocate_stack(common, extrasize); + + if (needs_control_head) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr); + + if (extrasize > 0) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + + if (needs_control_head) + { + SLJIT_ASSERT(extrasize == 2); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0); + } + } +else + { + extrasize = needs_control_head ? 3 : 2; + allocate_stack(common, framesize + extrasize); + + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + OP2(SLJIT_ADD, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, (framesize + extrasize) * sizeof(sljit_sw)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP2, 0); + if (needs_control_head) + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + + if (needs_control_head) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP1, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0); + } + else + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0); + + init_frame(common, ccbegin, NULL, framesize + extrasize - 1, extrasize, FALSE); + } + +memset(&altbacktrack, 0, sizeof(backtrack_common)); +if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT) + { + /* Negative assert is stronger than positive assert. */ + common->local_exit = TRUE; + common->quit_label = NULL; + common->quit = NULL; + common->positive_assert = FALSE; + } +else + common->positive_assert = TRUE; +common->positive_assert_quit = NULL; + +while (1) + { + common->accept_label = NULL; + common->accept = NULL; + altbacktrack.top = NULL; + altbacktrack.topbacktracks = NULL; + + if (*ccbegin == OP_ALT && extrasize > 0) + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + + altbacktrack.cc = ccbegin; + compile_matchingpath(common, ccbegin + 1 + LINK_SIZE, cc, &altbacktrack); + if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + { + if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT) + { + common->local_exit = save_local_exit; + common->quit_label = save_quit_label; + common->quit = save_quit; + } + common->positive_assert = save_positive_assert; + common->then_trap = save_then_trap; + common->accept_label = save_accept_label; + common->positive_assert_quit = save_positive_assert_quit; + common->accept = save_accept; + return NULL; + } + common->accept_label = LABEL(); + if (common->accept != NULL) + set_jumps(common->accept, common->accept_label); + + /* Reset stack. */ + if (framesize < 0) + { + if (framesize == no_frame) + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + else if (extrasize > 0) + free_stack(common, extrasize); + + if (needs_control_head) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(-1)); + } + else + { + if ((opcode != OP_ASSERT_NOT && opcode != OP_ASSERTBACK_NOT) || conditional) + { + /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */ + OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + 1) * sizeof(sljit_sw)); + if (needs_control_head) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(-1)); + } + else + { + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + if (needs_control_head) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(-framesize - 2)); + add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL)); + } + } + + if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT) + { + /* We know that STR_PTR was stored on the top of the stack. */ + if (conditional) + { + if (extrasize > 0) + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), needs_control_head ? STACK(-2) : STACK(-1)); + } + else if (bra == OP_BRAZERO) + { + if (framesize < 0) + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(-extrasize)); + else + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-framesize - 1)); + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(-framesize - extrasize)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0); + } + OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw)); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); + } + else if (framesize >= 0) + { + /* For OP_BRA and OP_BRAMINZERO. */ + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-framesize - 1)); + } + } + add_jump(compiler, found, JUMP(SLJIT_JUMP)); + + compile_backtrackingpath(common, altbacktrack.top); + if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + { + if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT) + { + common->local_exit = save_local_exit; + common->quit_label = save_quit_label; + common->quit = save_quit; + } + common->positive_assert = save_positive_assert; + common->then_trap = save_then_trap; + common->accept_label = save_accept_label; + common->positive_assert_quit = save_positive_assert_quit; + common->accept = save_accept; + return NULL; + } + set_jumps(altbacktrack.topbacktracks, LABEL()); + + if (*cc != OP_ALT) + break; + + ccbegin = cc; + cc += GET(cc, 1); + } + +if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT) + { + SLJIT_ASSERT(common->positive_assert_quit == NULL); + /* Makes the check less complicated below. */ + common->positive_assert_quit = common->quit; + } + +/* None of them matched. */ +if (common->positive_assert_quit != NULL) + { + jump = JUMP(SLJIT_JUMP); + set_jumps(common->positive_assert_quit, LABEL()); + SLJIT_ASSERT(framesize != no_stack); + if (framesize < 0) + OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, extrasize * sizeof(sljit_sw)); + else + { + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL)); + OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (framesize + extrasize) * sizeof(sljit_sw)); + } + JUMPHERE(jump); + } + +if (needs_control_head) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(1)); + +if (opcode == OP_ASSERT || opcode == OP_ASSERTBACK) + { + /* Assert is failed. */ + if ((conditional && extrasize > 0) || bra == OP_BRAZERO) + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + + if (framesize < 0) + { + /* The topmost item should be 0. */ + if (bra == OP_BRAZERO) + { + if (extrasize == 2) + free_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); + } + else if (extrasize > 0) + free_stack(common, extrasize); + } + else + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(extrasize - 1)); + /* The topmost item should be 0. */ + if (bra == OP_BRAZERO) + { + free_stack(common, framesize + extrasize - 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); + } + else + free_stack(common, framesize + extrasize); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0); + } + jump = JUMP(SLJIT_JUMP); + if (bra != OP_BRAZERO) + add_jump(compiler, target, jump); + + /* Assert is successful. */ + set_jumps(tmp, LABEL()); + if (framesize < 0) + { + /* We know that STR_PTR was stored on the top of the stack. */ + if (extrasize > 0) + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(-extrasize)); + + /* Keep the STR_PTR on the top of the stack. */ + if (bra == OP_BRAZERO) + { + OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw)); + if (extrasize == 2) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + } + else if (bra == OP_BRAMINZERO) + { + OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw)); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); + } + } + else + { + if (bra == OP_BRA) + { + /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */ + OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + 1) * sizeof(sljit_sw)); + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(-extrasize + 1)); + } + else + { + /* We don't need to keep the STR_PTR, only the previous private_data_ptr. */ + OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + 2) * sizeof(sljit_sw)); + if (extrasize == 2) + { + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + if (bra == OP_BRAMINZERO) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); + } + else + { + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), bra == OP_BRAZERO ? STR_PTR : SLJIT_IMM, 0); + } + } + } + + if (bra == OP_BRAZERO) + { + backtrack->matchingpath = LABEL(); + SET_LABEL(jump, backtrack->matchingpath); + } + else if (bra == OP_BRAMINZERO) + { + JUMPTO(SLJIT_JUMP, backtrack->matchingpath); + JUMPHERE(brajump); + if (framesize >= 0) + { + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-framesize - 1)); + } + set_jumps(backtrack->common.topbacktracks, LABEL()); + } + } +else + { + /* AssertNot is successful. */ + if (framesize < 0) + { + if (extrasize > 0) + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + + if (bra != OP_BRA) + { + if (extrasize == 2) + free_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); + } + else if (extrasize > 0) + free_stack(common, extrasize); + } + else + { + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(extrasize - 1)); + /* The topmost item should be 0. */ + if (bra != OP_BRA) + { + free_stack(common, framesize + extrasize - 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); + } + else + free_stack(common, framesize + extrasize); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0); + } + + if (bra == OP_BRAZERO) + backtrack->matchingpath = LABEL(); + else if (bra == OP_BRAMINZERO) + { + JUMPTO(SLJIT_JUMP, backtrack->matchingpath); + JUMPHERE(brajump); + } + + if (bra != OP_BRA) + { + SLJIT_ASSERT(found == &backtrack->common.topbacktracks); + set_jumps(backtrack->common.topbacktracks, LABEL()); + backtrack->common.topbacktracks = NULL; + } + } + +if (opcode == OP_ASSERT_NOT || opcode == OP_ASSERTBACK_NOT) + { + common->local_exit = save_local_exit; + common->quit_label = save_quit_label; + common->quit = save_quit; + } +common->positive_assert = save_positive_assert; +common->then_trap = save_then_trap; +common->accept_label = save_accept_label; +common->positive_assert_quit = save_positive_assert_quit; +common->accept = save_accept; +return cc + 1 + LINK_SIZE; +} + +static SLJIT_INLINE void match_once_common(compiler_common *common, pcre_uchar ket, int framesize, int private_data_ptr, BOOL has_alternatives, BOOL needs_control_head) +{ +DEFINE_COMPILER; +int stacksize; + +if (framesize < 0) + { + if (framesize == no_frame) + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + else + { + stacksize = needs_control_head ? 1 : 0; + if (ket != OP_KET || has_alternatives) + stacksize++; + + if (stacksize > 0) + free_stack(common, stacksize); + } + + if (needs_control_head) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), (ket != OP_KET || has_alternatives) ? STACK(-2) : STACK(-1)); + + /* TMP2 which is set here used by OP_KETRMAX below. */ + if (ket == OP_KETRMAX) + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(-1)); + else if (ket == OP_KETRMIN) + { + /* Move the STR_PTR to the private_data_ptr. */ + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-1)); + } + } +else + { + stacksize = (ket != OP_KET || has_alternatives) ? 2 : 1; + OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, (framesize + stacksize) * sizeof(sljit_sw)); + if (needs_control_head) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-1)); + + if (ket == OP_KETRMAX) + { + /* TMP2 which is set here used by OP_KETRMAX below. */ + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + } + } +if (needs_control_head) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, TMP1, 0); +} + +static SLJIT_INLINE int match_capture_common(compiler_common *common, int stacksize, int offset, int private_data_ptr) +{ +DEFINE_COMPILER; + +if (common->capture_last_ptr != 0) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, SLJIT_IMM, offset >> 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0); + stacksize++; + } +if (common->optimized_cbracket[offset >> 1] == 0) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset)); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1)); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize + 1), TMP2, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0); + stacksize += 2; + } +return stacksize; +} + +/* + Handling bracketed expressions is probably the most complex part. + + Stack layout naming characters: + S - Push the current STR_PTR + 0 - Push a 0 (NULL) + A - Push the current STR_PTR. Needed for restoring the STR_PTR + before the next alternative. Not pushed if there are no alternatives. + M - Any values pushed by the current alternative. Can be empty, or anything. + C - Push the previous OVECTOR(i), OVECTOR(i+1) and OVECTOR_PRIV(i) to the stack. + L - Push the previous local (pointed by localptr) to the stack + () - opional values stored on the stack + ()* - optonal, can be stored multiple times + + The following list shows the regular expression templates, their PCRE byte codes + and stack layout supported by pcre-sljit. + + (?:) OP_BRA | OP_KET A M + () OP_CBRA | OP_KET C M + (?:)+ OP_BRA | OP_KETRMAX 0 A M S ( A M S )* + OP_SBRA | OP_KETRMAX 0 L M S ( L M S )* + (?:)+? OP_BRA | OP_KETRMIN 0 A M S ( A M S )* + OP_SBRA | OP_KETRMIN 0 L M S ( L M S )* + ()+ OP_CBRA | OP_KETRMAX 0 C M S ( C M S )* + OP_SCBRA | OP_KETRMAX 0 C M S ( C M S )* + ()+? OP_CBRA | OP_KETRMIN 0 C M S ( C M S )* + OP_SCBRA | OP_KETRMIN 0 C M S ( C M S )* + (?:)? OP_BRAZERO | OP_BRA | OP_KET S ( A M 0 ) + (?:)?? OP_BRAMINZERO | OP_BRA | OP_KET S ( A M 0 ) + ()? OP_BRAZERO | OP_CBRA | OP_KET S ( C M 0 ) + ()?? OP_BRAMINZERO | OP_CBRA | OP_KET S ( C M 0 ) + (?:)* OP_BRAZERO | OP_BRA | OP_KETRMAX S 0 ( A M S )* + OP_BRAZERO | OP_SBRA | OP_KETRMAX S 0 ( L M S )* + (?:)*? OP_BRAMINZERO | OP_BRA | OP_KETRMIN S 0 ( A M S )* + OP_BRAMINZERO | OP_SBRA | OP_KETRMIN S 0 ( L M S )* + ()* OP_BRAZERO | OP_CBRA | OP_KETRMAX S 0 ( C M S )* + OP_BRAZERO | OP_SCBRA | OP_KETRMAX S 0 ( C M S )* + ()*? OP_BRAMINZERO | OP_CBRA | OP_KETRMIN S 0 ( C M S )* + OP_BRAMINZERO | OP_SCBRA | OP_KETRMIN S 0 ( C M S )* + + + Stack layout naming characters: + A - Push the alternative index (starting from 0) on the stack. + Not pushed if there is no alternatives. + M - Any values pushed by the current alternative. Can be empty, or anything. + + The next list shows the possible content of a bracket: + (|) OP_*BRA | OP_ALT ... M A + (?()|) OP_*COND | OP_ALT M A + (?>|) OP_ONCE | OP_ALT ... [stack trace] M A + (?>|) OP_ONCE_NC | OP_ALT ... [stack trace] M A + Or nothing, if trace is unnecessary +*/ + +static pcre_uchar *compile_bracket_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent) +{ +DEFINE_COMPILER; +backtrack_common *backtrack; +pcre_uchar opcode; +int private_data_ptr = 0; +int offset = 0; +int i, stacksize; +int repeat_ptr = 0, repeat_length = 0; +int repeat_type = 0, repeat_count = 0; +pcre_uchar *ccbegin; +pcre_uchar *matchingpath; +pcre_uchar *slot; +pcre_uchar bra = OP_BRA; +pcre_uchar ket; +assert_backtrack *assert; +BOOL has_alternatives; +BOOL needs_control_head = FALSE; +struct sljit_jump *jump; +struct sljit_jump *skip; +struct sljit_label *rmax_label = NULL; +struct sljit_jump *braminzero = NULL; + +PUSH_BACKTRACK(sizeof(bracket_backtrack), cc, NULL); + +if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO) + { + bra = *cc; + cc++; + opcode = *cc; + } + +opcode = *cc; +ccbegin = cc; +matchingpath = bracketend(cc) - 1 - LINK_SIZE; +ket = *matchingpath; +if (ket == OP_KET && PRIVATE_DATA(matchingpath) != 0) + { + repeat_ptr = PRIVATE_DATA(matchingpath); + repeat_length = PRIVATE_DATA(matchingpath + 1); + repeat_type = PRIVATE_DATA(matchingpath + 2); + repeat_count = PRIVATE_DATA(matchingpath + 3); + SLJIT_ASSERT(repeat_length != 0 && repeat_type != 0 && repeat_count != 0); + if (repeat_type == OP_UPTO) + ket = OP_KETRMAX; + if (repeat_type == OP_MINUPTO) + ket = OP_KETRMIN; + } + +if ((opcode == OP_COND || opcode == OP_SCOND) && cc[1 + LINK_SIZE] == OP_DEF) + { + /* Drop this bracket_backtrack. */ + parent->top = backtrack->prev; + return matchingpath + 1 + LINK_SIZE + repeat_length; + } + +matchingpath = ccbegin + 1 + LINK_SIZE; +SLJIT_ASSERT(ket == OP_KET || ket == OP_KETRMAX || ket == OP_KETRMIN); +SLJIT_ASSERT(!((bra == OP_BRAZERO && ket == OP_KETRMIN) || (bra == OP_BRAMINZERO && ket == OP_KETRMAX))); +cc += GET(cc, 1); + +has_alternatives = *cc == OP_ALT; +if (SLJIT_UNLIKELY(opcode == OP_COND || opcode == OP_SCOND)) + has_alternatives = (*matchingpath == OP_RREF || *matchingpath == OP_DNRREF || *matchingpath == OP_FAIL) ? FALSE : TRUE; + +if (SLJIT_UNLIKELY(opcode == OP_COND) && (*cc == OP_KETRMAX || *cc == OP_KETRMIN)) + opcode = OP_SCOND; +if (SLJIT_UNLIKELY(opcode == OP_ONCE_NC)) + opcode = OP_ONCE; + +if (opcode == OP_CBRA || opcode == OP_SCBRA) + { + /* Capturing brackets has a pre-allocated space. */ + offset = GET2(ccbegin, 1 + LINK_SIZE); + if (common->optimized_cbracket[offset] == 0) + { + private_data_ptr = OVECTOR_PRIV(offset); + offset <<= 1; + } + else + { + offset <<= 1; + private_data_ptr = OVECTOR(offset); + } + BACKTRACK_AS(bracket_backtrack)->private_data_ptr = private_data_ptr; + matchingpath += IMM2_SIZE; + } +else if (opcode == OP_ONCE || opcode == OP_SBRA || opcode == OP_SCOND) + { + /* Other brackets simply allocate the next entry. */ + private_data_ptr = PRIVATE_DATA(ccbegin); + SLJIT_ASSERT(private_data_ptr != 0); + BACKTRACK_AS(bracket_backtrack)->private_data_ptr = private_data_ptr; + if (opcode == OP_ONCE) + BACKTRACK_AS(bracket_backtrack)->u.framesize = get_framesize(common, ccbegin, NULL, FALSE, &needs_control_head); + } + +/* Instructions before the first alternative. */ +stacksize = 0; +if (ket == OP_KETRMAX || (ket == OP_KETRMIN && bra != OP_BRAMINZERO)) + stacksize++; +if (bra == OP_BRAZERO) + stacksize++; + +if (stacksize > 0) + allocate_stack(common, stacksize); + +stacksize = 0; +if (ket == OP_KETRMAX || (ket == OP_KETRMIN && bra != OP_BRAMINZERO)) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0); + stacksize++; + } + +if (bra == OP_BRAZERO) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0); + +if (bra == OP_BRAMINZERO) + { + /* This is a backtrack path! (Since the try-path of OP_BRAMINZERO matches to the empty string) */ + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + if (ket != OP_KETRMIN) + { + free_stack(common, 1); + braminzero = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0); + } + else + { + if (opcode == OP_ONCE || opcode >= OP_SBRA) + { + jump = CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0); + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(1)); + /* Nothing stored during the first run. */ + skip = JUMP(SLJIT_JUMP); + JUMPHERE(jump); + /* Checking zero-length iteration. */ + if (opcode != OP_ONCE || BACKTRACK_AS(bracket_backtrack)->u.framesize < 0) + { + /* When we come from outside, private_data_ptr contains the previous STR_PTR. */ + braminzero = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + } + else + { + /* Except when the whole stack frame must be saved. */ + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + braminzero = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_MEM1(TMP1), STACK(-BACKTRACK_AS(bracket_backtrack)->u.framesize - 2)); + } + JUMPHERE(skip); + } + else + { + jump = CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0); + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(1)); + JUMPHERE(jump); + } + } + } + +if (repeat_type != 0) + { + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, repeat_count); + if (repeat_type == OP_EXACT) + rmax_label = LABEL(); + } + +if (ket == OP_KETRMIN) + BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL(); + +if (ket == OP_KETRMAX) + { + rmax_label = LABEL(); + if (has_alternatives && opcode != OP_ONCE && opcode < OP_SBRA && repeat_type == 0) + BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = rmax_label; + } + +/* Handling capturing brackets and alternatives. */ +if (opcode == OP_ONCE) + { + stacksize = 0; + if (needs_control_head) + { + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr); + stacksize++; + } + + if (BACKTRACK_AS(bracket_backtrack)->u.framesize < 0) + { + /* Neither capturing brackets nor recursions are found in the block. */ + if (ket == OP_KETRMIN) + { + stacksize += 2; + if (!needs_control_head) + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + } + else + { + if (BACKTRACK_AS(bracket_backtrack)->u.framesize == no_frame) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0); + if (ket == OP_KETRMAX || has_alternatives) + stacksize++; + } + + if (stacksize > 0) + allocate_stack(common, stacksize); + + stacksize = 0; + if (needs_control_head) + { + stacksize++; + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0); + } + + if (ket == OP_KETRMIN) + { + if (needs_control_head) + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0); + if (BACKTRACK_AS(bracket_backtrack)->u.framesize == no_frame) + OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0, SLJIT_IMM, needs_control_head ? (2 * sizeof(sljit_sw)) : sizeof(sljit_sw)); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize + 1), TMP2, 0); + } + else if (ket == OP_KETRMAX || has_alternatives) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0); + } + else + { + if (ket != OP_KET || has_alternatives) + stacksize++; + + stacksize += BACKTRACK_AS(bracket_backtrack)->u.framesize + 1; + allocate_stack(common, stacksize); + + if (needs_control_head) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0); + + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + OP2(SLJIT_ADD, TMP2, 0, STACK_TOP, 0, SLJIT_IMM, stacksize * sizeof(sljit_sw)); + + stacksize = needs_control_head ? 1 : 0; + if (ket != OP_KET || has_alternatives) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP2, 0); + stacksize++; + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0); + } + else + { + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP2, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP1, 0); + } + init_frame(common, ccbegin, NULL, BACKTRACK_AS(bracket_backtrack)->u.framesize + stacksize, stacksize + 1, FALSE); + } + } +else if (opcode == OP_CBRA || opcode == OP_SCBRA) + { + /* Saving the previous values. */ + if (common->optimized_cbracket[offset >> 1] != 0) + { + SLJIT_ASSERT(private_data_ptr == OVECTOR(offset)); + allocate_stack(common, 2); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr + sizeof(sljit_sw)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0); + } + else + { + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + allocate_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0); + } + } +else if (opcode == OP_SBRA || opcode == OP_SCOND) + { + /* Saving the previous value. */ + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + allocate_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0); + } +else if (has_alternatives) + { + /* Pushing the starting string pointer. */ + allocate_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + } + +/* Generating code for the first alternative. */ +if (opcode == OP_COND || opcode == OP_SCOND) + { + if (*matchingpath == OP_CREF) + { + SLJIT_ASSERT(has_alternatives); + add_jump(compiler, &(BACKTRACK_AS(bracket_backtrack)->u.condfailed), + CMP(SLJIT_EQUAL, SLJIT_MEM1(SLJIT_SP), OVECTOR(GET2(matchingpath, 1) << 1), SLJIT_MEM1(SLJIT_SP), OVECTOR(1))); + matchingpath += 1 + IMM2_SIZE; + } + else if (*matchingpath == OP_DNCREF) + { + SLJIT_ASSERT(has_alternatives); + + i = GET2(matchingpath, 1 + IMM2_SIZE); + slot = common->name_table + GET2(matchingpath, 1) * common->name_entry_size; + OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(1)); + OP2(SLJIT_SUB | SLJIT_SET_Z, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(GET2(slot, 0) << 1), TMP1, 0); + slot += common->name_entry_size; + i--; + while (i-- > 0) + { + OP2(SLJIT_SUB, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(GET2(slot, 0) << 1), TMP1, 0); + OP2(SLJIT_OR | SLJIT_SET_Z, TMP2, 0, TMP2, 0, STR_PTR, 0); + slot += common->name_entry_size; + } + OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0); + add_jump(compiler, &(BACKTRACK_AS(bracket_backtrack)->u.condfailed), JUMP(SLJIT_ZERO)); + matchingpath += 1 + 2 * IMM2_SIZE; + } + else if (*matchingpath == OP_RREF || *matchingpath == OP_DNRREF || *matchingpath == OP_FAIL) + { + /* Never has other case. */ + BACKTRACK_AS(bracket_backtrack)->u.condfailed = NULL; + SLJIT_ASSERT(!has_alternatives); + + if (*matchingpath == OP_FAIL) + stacksize = 0; + else if (*matchingpath == OP_RREF) + { + stacksize = GET2(matchingpath, 1); + if (common->currententry == NULL) + stacksize = 0; + else if (stacksize == RREF_ANY) + stacksize = 1; + else if (common->currententry->start == 0) + stacksize = stacksize == 0; + else + stacksize = stacksize == (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE); + + if (stacksize != 0) + matchingpath += 1 + IMM2_SIZE; + } + else + { + if (common->currententry == NULL || common->currententry->start == 0) + stacksize = 0; + else + { + stacksize = GET2(matchingpath, 1 + IMM2_SIZE); + slot = common->name_table + GET2(matchingpath, 1) * common->name_entry_size; + i = (int)GET2(common->start, common->currententry->start + 1 + LINK_SIZE); + while (stacksize > 0) + { + if ((int)GET2(slot, 0) == i) + break; + slot += common->name_entry_size; + stacksize--; + } + } + + if (stacksize != 0) + matchingpath += 1 + 2 * IMM2_SIZE; + } + + /* The stacksize == 0 is a common "else" case. */ + if (stacksize == 0) + { + if (*cc == OP_ALT) + { + matchingpath = cc + 1 + LINK_SIZE; + cc += GET(cc, 1); + } + else + matchingpath = cc; + } + } + else + { + SLJIT_ASSERT(has_alternatives && *matchingpath >= OP_ASSERT && *matchingpath <= OP_ASSERTBACK_NOT); + /* Similar code as PUSH_BACKTRACK macro. */ + assert = sljit_alloc_memory(compiler, sizeof(assert_backtrack)); + if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + return NULL; + memset(assert, 0, sizeof(assert_backtrack)); + assert->common.cc = matchingpath; + BACKTRACK_AS(bracket_backtrack)->u.assert = assert; + matchingpath = compile_assert_matchingpath(common, matchingpath, assert, TRUE); + } + } + +compile_matchingpath(common, matchingpath, cc, backtrack); +if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + return NULL; + +if (opcode == OP_ONCE) + match_once_common(common, ket, BACKTRACK_AS(bracket_backtrack)->u.framesize, private_data_ptr, has_alternatives, needs_control_head); + +stacksize = 0; +if (repeat_type == OP_MINUPTO) + { + /* We need to preserve the counter. TMP2 will be used below. */ + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), repeat_ptr); + stacksize++; + } +if (ket != OP_KET || bra != OP_BRA) + stacksize++; +if (offset != 0) + { + if (common->capture_last_ptr != 0) + stacksize++; + if (common->optimized_cbracket[offset >> 1] == 0) + stacksize += 2; + } +if (has_alternatives && opcode != OP_ONCE) + stacksize++; + +if (stacksize > 0) + allocate_stack(common, stacksize); + +stacksize = 0; +if (repeat_type == OP_MINUPTO) + { + /* TMP2 was set above. */ + OP2(SLJIT_SUB, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP2, 0, SLJIT_IMM, 1); + stacksize++; + } + +if (ket != OP_KET || bra != OP_BRA) + { + if (ket != OP_KET) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0); + else + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0); + stacksize++; + } + +if (offset != 0) + stacksize = match_capture_common(common, stacksize, offset, private_data_ptr); + +if (has_alternatives) + { + if (opcode != OP_ONCE) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0); + if (ket != OP_KETRMAX) + BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL(); + } + +/* Must be after the matchingpath label. */ +if (offset != 0 && common->optimized_cbracket[offset >> 1] != 0) + { + SLJIT_ASSERT(private_data_ptr == OVECTOR(offset + 0)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0); + } + +if (ket == OP_KETRMAX) + { + if (repeat_type != 0) + { + if (has_alternatives) + BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL(); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, rmax_label); + /* Drop STR_PTR for greedy plus quantifier. */ + if (opcode != OP_ONCE) + free_stack(common, 1); + } + else if (opcode == OP_ONCE || opcode >= OP_SBRA) + { + if (has_alternatives) + BACKTRACK_AS(bracket_backtrack)->alternative_matchingpath = LABEL(); + /* Checking zero-length iteration. */ + if (opcode != OP_ONCE) + { + CMPTO(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STR_PTR, 0, rmax_label); + /* Drop STR_PTR for greedy plus quantifier. */ + if (bra != OP_BRAZERO) + free_stack(common, 1); + } + else + /* TMP2 must contain the starting STR_PTR. */ + CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, STR_PTR, 0, rmax_label); + } + else + JUMPTO(SLJIT_JUMP, rmax_label); + BACKTRACK_AS(bracket_backtrack)->recursive_matchingpath = LABEL(); + } + +if (repeat_type == OP_EXACT) + { + count_match(common); + OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, rmax_label); + } +else if (repeat_type == OP_UPTO) + { + /* We need to preserve the counter. */ + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), repeat_ptr); + allocate_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0); + } + +if (bra == OP_BRAZERO) + BACKTRACK_AS(bracket_backtrack)->zero_matchingpath = LABEL(); + +if (bra == OP_BRAMINZERO) + { + /* This is a backtrack path! (From the viewpoint of OP_BRAMINZERO) */ + JUMPTO(SLJIT_JUMP, ((braminzero_backtrack *)parent)->matchingpath); + if (braminzero != NULL) + { + JUMPHERE(braminzero); + /* We need to release the end pointer to perform the + backtrack for the zero-length iteration. When + framesize is < 0, OP_ONCE will do the release itself. */ + if (opcode == OP_ONCE && BACKTRACK_AS(bracket_backtrack)->u.framesize >= 0) + { + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL)); + } + else if (ket == OP_KETRMIN && opcode != OP_ONCE) + free_stack(common, 1); + } + /* Continue to the normal backtrack. */ + } + +if ((ket != OP_KET && bra != OP_BRAMINZERO) || bra == OP_BRAZERO) + count_match(common); + +/* Skip the other alternatives. */ +while (*cc == OP_ALT) + cc += GET(cc, 1); +cc += 1 + LINK_SIZE; + +if (opcode == OP_ONCE) + { + /* We temporarily encode the needs_control_head in the lowest bit. + Note: on the target architectures of SLJIT the ((x << 1) >> 1) returns + the same value for small signed numbers (including negative numbers). */ + BACKTRACK_AS(bracket_backtrack)->u.framesize = ((unsigned int)BACKTRACK_AS(bracket_backtrack)->u.framesize << 1) | (needs_control_head ? 1 : 0); + } +return cc + repeat_length; +} + +static pcre_uchar *compile_bracketpos_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent) +{ +DEFINE_COMPILER; +backtrack_common *backtrack; +pcre_uchar opcode; +int private_data_ptr; +int cbraprivptr = 0; +BOOL needs_control_head; +int framesize; +int stacksize; +int offset = 0; +BOOL zero = FALSE; +pcre_uchar *ccbegin = NULL; +int stack; /* Also contains the offset of control head. */ +struct sljit_label *loop = NULL; +struct jump_list *emptymatch = NULL; + +PUSH_BACKTRACK(sizeof(bracketpos_backtrack), cc, NULL); +if (*cc == OP_BRAPOSZERO) + { + zero = TRUE; + cc++; + } + +opcode = *cc; +private_data_ptr = PRIVATE_DATA(cc); +SLJIT_ASSERT(private_data_ptr != 0); +BACKTRACK_AS(bracketpos_backtrack)->private_data_ptr = private_data_ptr; +switch(opcode) + { + case OP_BRAPOS: + case OP_SBRAPOS: + ccbegin = cc + 1 + LINK_SIZE; + break; + + case OP_CBRAPOS: + case OP_SCBRAPOS: + offset = GET2(cc, 1 + LINK_SIZE); + /* This case cannot be optimized in the same was as + normal capturing brackets. */ + SLJIT_ASSERT(common->optimized_cbracket[offset] == 0); + cbraprivptr = OVECTOR_PRIV(offset); + offset <<= 1; + ccbegin = cc + 1 + LINK_SIZE + IMM2_SIZE; + break; + + default: + SLJIT_UNREACHABLE(); + break; + } + +framesize = get_framesize(common, cc, NULL, FALSE, &needs_control_head); +BACKTRACK_AS(bracketpos_backtrack)->framesize = framesize; +if (framesize < 0) + { + if (offset != 0) + { + stacksize = 2; + if (common->capture_last_ptr != 0) + stacksize++; + } + else + stacksize = 1; + + if (needs_control_head) + stacksize++; + if (!zero) + stacksize++; + + BACKTRACK_AS(bracketpos_backtrack)->stacksize = stacksize; + allocate_stack(common, stacksize); + if (framesize == no_frame) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0); + + stack = 0; + if (offset != 0) + { + stack = 2; + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset)); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1)); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0); + if (common->capture_last_ptr != 0) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), TMP2, 0); + if (needs_control_head) + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr); + if (common->capture_last_ptr != 0) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), TMP1, 0); + stack = 3; + } + } + else + { + if (needs_control_head) + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + stack = 1; + } + + if (needs_control_head) + stack++; + if (!zero) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), SLJIT_IMM, 1); + if (needs_control_head) + { + stack--; + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP2, 0); + } + } +else + { + stacksize = framesize + 1; + if (!zero) + stacksize++; + if (needs_control_head) + stacksize++; + if (offset == 0) + stacksize++; + BACKTRACK_AS(bracketpos_backtrack)->stacksize = stacksize; + + allocate_stack(common, stacksize); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + if (needs_control_head) + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr); + OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), private_data_ptr, STACK_TOP, 0, SLJIT_IMM, stacksize * sizeof(sljit_sw)); + + stack = 0; + if (!zero) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 1); + stack = 1; + } + if (needs_control_head) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP2, 0); + stack++; + } + if (offset == 0) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), STR_PTR, 0); + stack++; + } + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stack), TMP1, 0); + init_frame(common, cc, NULL, stacksize - 1, stacksize - framesize, FALSE); + stack -= 1 + (offset == 0); + } + +if (offset != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), cbraprivptr, STR_PTR, 0); + +loop = LABEL(); +while (*cc != OP_KETRPOS) + { + backtrack->top = NULL; + backtrack->topbacktracks = NULL; + cc += GET(cc, 1); + + compile_matchingpath(common, ccbegin, cc, backtrack); + if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + return NULL; + + if (framesize < 0) + { + if (framesize == no_frame) + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + + if (offset != 0) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), cbraprivptr); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), cbraprivptr, STR_PTR, 0); + if (common->capture_last_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, SLJIT_IMM, offset >> 1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0); + } + else + { + if (opcode == OP_SBRAPOS) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + } + + /* Even if the match is empty, we need to reset the control head. */ + if (needs_control_head) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(stack)); + + if (opcode == OP_SBRAPOS || opcode == OP_SCBRAPOS) + add_jump(compiler, &emptymatch, CMP(SLJIT_EQUAL, TMP1, 0, STR_PTR, 0)); + + if (!zero) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize - 1), SLJIT_IMM, 0); + } + else + { + if (offset != 0) + { + OP2(SLJIT_SUB, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_IMM, stacksize * sizeof(sljit_sw)); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), cbraprivptr); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), cbraprivptr, STR_PTR, 0); + if (common->capture_last_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, SLJIT_IMM, offset >> 1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0); + } + else + { + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + OP2(SLJIT_SUB, STACK_TOP, 0, TMP2, 0, SLJIT_IMM, stacksize * sizeof(sljit_sw)); + if (opcode == OP_SBRAPOS) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP2), STACK(-framesize - 2)); + OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), STACK(-framesize - 2), STR_PTR, 0); + } + + /* Even if the match is empty, we need to reset the control head. */ + if (needs_control_head) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_MEM1(STACK_TOP), STACK(stack)); + + if (opcode == OP_SBRAPOS || opcode == OP_SCBRAPOS) + add_jump(compiler, &emptymatch, CMP(SLJIT_EQUAL, TMP1, 0, STR_PTR, 0)); + + if (!zero) + { + if (framesize < 0) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize - 1), SLJIT_IMM, 0); + else + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); + } + } + + JUMPTO(SLJIT_JUMP, loop); + flush_stubs(common); + + compile_backtrackingpath(common, backtrack->top); + if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + return NULL; + set_jumps(backtrack->topbacktracks, LABEL()); + + if (framesize < 0) + { + if (offset != 0) + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), cbraprivptr); + else + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + } + else + { + if (offset != 0) + { + /* Last alternative. */ + if (*cc == OP_KETRPOS) + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), cbraprivptr); + } + else + { + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(TMP2), STACK(-framesize - 2)); + } + } + + if (*cc == OP_KETRPOS) + break; + ccbegin = cc + 1 + LINK_SIZE; + } + +/* We don't have to restore the control head in case of a failed match. */ + +backtrack->topbacktracks = NULL; +if (!zero) + { + if (framesize < 0) + add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(stacksize - 1), SLJIT_IMM, 0)); + else /* TMP2 is set to [private_data_ptr] above. */ + add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(TMP2), STACK(-stacksize), SLJIT_IMM, 0)); + } + +/* None of them matched. */ +set_jumps(emptymatch, LABEL()); +count_match(common); +return cc + 1 + LINK_SIZE; +} + +static SLJIT_INLINE pcre_uchar *get_iterator_parameters(compiler_common *common, pcre_uchar *cc, pcre_uchar *opcode, pcre_uchar *type, sljit_u32 *max, sljit_u32 *exact, pcre_uchar **end) +{ +int class_len; + +*opcode = *cc; +*exact = 0; + +if (*opcode >= OP_STAR && *opcode <= OP_POSUPTO) + { + cc++; + *type = OP_CHAR; + } +else if (*opcode >= OP_STARI && *opcode <= OP_POSUPTOI) + { + cc++; + *type = OP_CHARI; + *opcode -= OP_STARI - OP_STAR; + } +else if (*opcode >= OP_NOTSTAR && *opcode <= OP_NOTPOSUPTO) + { + cc++; + *type = OP_NOT; + *opcode -= OP_NOTSTAR - OP_STAR; + } +else if (*opcode >= OP_NOTSTARI && *opcode <= OP_NOTPOSUPTOI) + { + cc++; + *type = OP_NOTI; + *opcode -= OP_NOTSTARI - OP_STAR; + } +else if (*opcode >= OP_TYPESTAR && *opcode <= OP_TYPEPOSUPTO) + { + cc++; + *opcode -= OP_TYPESTAR - OP_STAR; + *type = OP_END; + } +else + { + SLJIT_ASSERT(*opcode == OP_CLASS || *opcode == OP_NCLASS || *opcode == OP_XCLASS); + *type = *opcode; + cc++; + class_len = (*type < OP_XCLASS) ? (int)(1 + (32 / sizeof(pcre_uchar))) : GET(cc, 0); + *opcode = cc[class_len - 1]; + + if (*opcode >= OP_CRSTAR && *opcode <= OP_CRMINQUERY) + { + *opcode -= OP_CRSTAR - OP_STAR; + *end = cc + class_len; + + if (*opcode == OP_PLUS || *opcode == OP_MINPLUS) + { + *exact = 1; + *opcode -= OP_PLUS - OP_STAR; + } + } + else if (*opcode >= OP_CRPOSSTAR && *opcode <= OP_CRPOSQUERY) + { + *opcode -= OP_CRPOSSTAR - OP_POSSTAR; + *end = cc + class_len; + + if (*opcode == OP_POSPLUS) + { + *exact = 1; + *opcode = OP_POSSTAR; + } + } + else + { + SLJIT_ASSERT(*opcode == OP_CRRANGE || *opcode == OP_CRMINRANGE || *opcode == OP_CRPOSRANGE); + *max = GET2(cc, (class_len + IMM2_SIZE)); + *exact = GET2(cc, class_len); + + if (*max == 0) + { + if (*opcode == OP_CRPOSRANGE) + *opcode = OP_POSSTAR; + else + *opcode -= OP_CRRANGE - OP_STAR; + } + else + { + *max -= *exact; + if (*max == 0) + *opcode = OP_EXACT; + else if (*max == 1) + { + if (*opcode == OP_CRPOSRANGE) + *opcode = OP_POSQUERY; + else + *opcode -= OP_CRRANGE - OP_QUERY; + } + else + { + if (*opcode == OP_CRPOSRANGE) + *opcode = OP_POSUPTO; + else + *opcode -= OP_CRRANGE - OP_UPTO; + } + } + *end = cc + class_len + 2 * IMM2_SIZE; + } + return cc; + } + +switch(*opcode) + { + case OP_EXACT: + *exact = GET2(cc, 0); + cc += IMM2_SIZE; + break; + + case OP_PLUS: + case OP_MINPLUS: + *exact = 1; + *opcode -= OP_PLUS - OP_STAR; + break; + + case OP_POSPLUS: + *exact = 1; + *opcode = OP_POSSTAR; + break; + + case OP_UPTO: + case OP_MINUPTO: + case OP_POSUPTO: + *max = GET2(cc, 0); + cc += IMM2_SIZE; + break; + } + +if (*type == OP_END) + { + *type = *cc; + *end = next_opcode(common, cc); + cc++; + return cc; + } + +*end = cc + 1; +#ifdef SUPPORT_UTF +if (common->utf && HAS_EXTRALEN(*cc)) *end += GET_EXTRALEN(*cc); +#endif +return cc; +} + +static pcre_uchar *compile_iterator_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent) +{ +DEFINE_COMPILER; +backtrack_common *backtrack; +pcre_uchar opcode; +pcre_uchar type; +sljit_u32 max = 0, exact; +BOOL fast_fail; +sljit_s32 fast_str_ptr; +BOOL charpos_enabled; +pcre_uchar charpos_char; +unsigned int charpos_othercasebit; +pcre_uchar *end; +jump_list *no_match = NULL; +jump_list *no_char1_match = NULL; +struct sljit_jump *jump = NULL; +struct sljit_label *label; +int private_data_ptr = PRIVATE_DATA(cc); +int base = (private_data_ptr == 0) ? SLJIT_MEM1(STACK_TOP) : SLJIT_MEM1(SLJIT_SP); +int offset0 = (private_data_ptr == 0) ? STACK(0) : private_data_ptr; +int offset1 = (private_data_ptr == 0) ? STACK(1) : private_data_ptr + (int)sizeof(sljit_sw); +int tmp_base, tmp_offset; + +PUSH_BACKTRACK(sizeof(char_iterator_backtrack), cc, NULL); + +fast_str_ptr = PRIVATE_DATA(cc + 1); +fast_fail = TRUE; + +SLJIT_ASSERT(common->fast_forward_bc_ptr == NULL || fast_str_ptr == 0 || cc == common->fast_forward_bc_ptr); + +if (cc == common->fast_forward_bc_ptr) + fast_fail = FALSE; +else if (common->fast_fail_start_ptr == 0) + fast_str_ptr = 0; + +SLJIT_ASSERT(common->fast_forward_bc_ptr != NULL || fast_str_ptr == 0 + || (fast_str_ptr >= common->fast_fail_start_ptr && fast_str_ptr <= common->fast_fail_end_ptr)); + +cc = get_iterator_parameters(common, cc, &opcode, &type, &max, &exact, &end); + +if (type != OP_EXTUNI) + { + tmp_base = TMP3; + tmp_offset = 0; + } +else + { + tmp_base = SLJIT_MEM1(SLJIT_SP); + tmp_offset = POSSESSIVE0; + } + +if (fast_fail && fast_str_ptr != 0) + add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), fast_str_ptr)); + +/* Handle fixed part first. */ +if (exact > 1) + { + SLJIT_ASSERT(fast_str_ptr == 0); + if (common->mode == JIT_COMPILE +#ifdef SUPPORT_UTF + && !common->utf +#endif + && type != OP_ANYNL && type != OP_EXTUNI) + { + OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(exact)); + add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_GREATER, TMP1, 0, STR_END, 0)); + OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, exact); + label = LABEL(); + compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks, FALSE); + OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, label); + } + else + { + OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, exact); + label = LABEL(); + compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks, TRUE); + OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, label); + } + } +else if (exact == 1) + compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks, TRUE); + +switch(opcode) + { + case OP_STAR: + case OP_UPTO: + SLJIT_ASSERT(fast_str_ptr == 0 || opcode == OP_STAR); + + if (type == OP_ANYNL || type == OP_EXTUNI) + { + SLJIT_ASSERT(private_data_ptr == 0); + SLJIT_ASSERT(fast_str_ptr == 0); + + allocate_stack(common, 2); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, 0); + + if (opcode == OP_UPTO) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, SLJIT_IMM, max); + + label = LABEL(); + compile_char1_matchingpath(common, type, cc, &BACKTRACK_AS(char_iterator_backtrack)->u.backtracks, TRUE); + if (opcode == OP_UPTO) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0); + OP2(SLJIT_SUB | SLJIT_SET_Z, TMP1, 0, TMP1, 0, SLJIT_IMM, 1); + jump = JUMP(SLJIT_ZERO); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE0, TMP1, 0); + } + + /* We cannot use TMP3 because of this allocate_stack. */ + allocate_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + JUMPTO(SLJIT_JUMP, label); + if (jump != NULL) + JUMPHERE(jump); + } + else + { + charpos_enabled = FALSE; + charpos_char = 0; + charpos_othercasebit = 0; + + if ((type != OP_CHAR && type != OP_CHARI) && (*end == OP_CHAR || *end == OP_CHARI)) + { + charpos_enabled = TRUE; +#ifdef SUPPORT_UTF + charpos_enabled = !common->utf || !HAS_EXTRALEN(end[1]); +#endif + if (charpos_enabled && *end == OP_CHARI && char_has_othercase(common, end + 1)) + { + charpos_othercasebit = char_get_othercase_bit(common, end + 1); + if (charpos_othercasebit == 0) + charpos_enabled = FALSE; + } + + if (charpos_enabled) + { + charpos_char = end[1]; + /* Consumpe the OP_CHAR opcode. */ + end += 2; +#if defined COMPILE_PCRE8 + SLJIT_ASSERT((charpos_othercasebit >> 8) == 0); +#elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + SLJIT_ASSERT((charpos_othercasebit >> 9) == 0); + if ((charpos_othercasebit & 0x100) != 0) + charpos_othercasebit = (charpos_othercasebit & 0xff) << 8; +#endif + if (charpos_othercasebit != 0) + charpos_char |= charpos_othercasebit; + + BACKTRACK_AS(char_iterator_backtrack)->u.charpos.enabled = TRUE; + BACKTRACK_AS(char_iterator_backtrack)->u.charpos.chr = charpos_char; + BACKTRACK_AS(char_iterator_backtrack)->u.charpos.othercasebit = charpos_othercasebit; + } + } + + if (charpos_enabled) + { + if (opcode == OP_UPTO) + OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max + 1); + + /* Search the first instance of charpos_char. */ + jump = JUMP(SLJIT_JUMP); + label = LABEL(); + if (opcode == OP_UPTO) + { + OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1); + add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_ZERO)); + } + compile_char1_matchingpath(common, type, cc, &backtrack->topbacktracks, FALSE); + if (fast_str_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0); + JUMPHERE(jump); + + detect_partial_match(common, &backtrack->topbacktracks); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); + if (charpos_othercasebit != 0) + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, charpos_othercasebit); + CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, charpos_char, label); + + if (private_data_ptr == 0) + allocate_stack(common, 2); + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + OP1(SLJIT_MOV, base, offset1, STR_PTR, 0); + if (opcode == OP_UPTO) + { + OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1); + add_jump(compiler, &no_match, JUMP(SLJIT_ZERO)); + } + + /* Search the last instance of charpos_char. */ + label = LABEL(); + compile_char1_matchingpath(common, type, cc, &no_match, FALSE); + if (fast_str_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0); + detect_partial_match(common, &no_match); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0)); + if (charpos_othercasebit != 0) + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, charpos_othercasebit); + if (opcode == OP_STAR) + { + CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, charpos_char, label); + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + } + else + { + jump = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, charpos_char); + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + JUMPHERE(jump); + } + + if (opcode == OP_UPTO) + { + OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, label); + } + else + JUMPTO(SLJIT_JUMP, label); + + set_jumps(no_match, LABEL()); + OP1(SLJIT_MOV, STR_PTR, 0, base, offset0); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + } +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + else if (common->utf) + { + if (private_data_ptr == 0) + allocate_stack(common, 2); + + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + OP1(SLJIT_MOV, base, offset1, STR_PTR, 0); + + if (opcode == OP_UPTO) + OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max); + + label = LABEL(); + compile_char1_matchingpath(common, type, cc, &no_match, TRUE); + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + + if (opcode == OP_UPTO) + { + OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, label); + } + else + JUMPTO(SLJIT_JUMP, label); + + set_jumps(no_match, LABEL()); + OP1(SLJIT_MOV, STR_PTR, 0, base, offset0); + if (fast_str_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0); + } +#endif + else + { + if (private_data_ptr == 0) + allocate_stack(common, 2); + + OP1(SLJIT_MOV, base, offset1, STR_PTR, 0); + if (opcode == OP_UPTO) + OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max); + + label = LABEL(); + detect_partial_match(common, &no_match); + compile_char1_matchingpath(common, type, cc, &no_char1_match, FALSE); + if (opcode == OP_UPTO) + { + OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, label); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + } + else + JUMPTO(SLJIT_JUMP, label); + + set_jumps(no_char1_match, LABEL()); + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + set_jumps(no_match, LABEL()); + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + if (fast_str_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0); + } + } + BACKTRACK_AS(char_iterator_backtrack)->matchingpath = LABEL(); + break; + + case OP_MINSTAR: + if (private_data_ptr == 0) + allocate_stack(common, 1); + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + BACKTRACK_AS(char_iterator_backtrack)->matchingpath = LABEL(); + if (fast_str_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0); + break; + + case OP_MINUPTO: + SLJIT_ASSERT(fast_str_ptr == 0); + if (private_data_ptr == 0) + allocate_stack(common, 2); + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + OP1(SLJIT_MOV, base, offset1, SLJIT_IMM, max + 1); + BACKTRACK_AS(char_iterator_backtrack)->matchingpath = LABEL(); + break; + + case OP_QUERY: + case OP_MINQUERY: + SLJIT_ASSERT(fast_str_ptr == 0); + if (private_data_ptr == 0) + allocate_stack(common, 1); + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + if (opcode == OP_QUERY) + compile_char1_matchingpath(common, type, cc, &BACKTRACK_AS(char_iterator_backtrack)->u.backtracks, TRUE); + BACKTRACK_AS(char_iterator_backtrack)->matchingpath = LABEL(); + break; + + case OP_EXACT: + break; + + case OP_POSSTAR: +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (common->utf) + { + OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0); + label = LABEL(); + compile_char1_matchingpath(common, type, cc, &no_match, TRUE); + OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0); + JUMPTO(SLJIT_JUMP, label); + set_jumps(no_match, LABEL()); + OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset); + if (fast_str_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0); + break; + } +#endif + label = LABEL(); + detect_partial_match(common, &no_match); + compile_char1_matchingpath(common, type, cc, &no_char1_match, FALSE); + JUMPTO(SLJIT_JUMP, label); + set_jumps(no_char1_match, LABEL()); + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + set_jumps(no_match, LABEL()); + if (fast_str_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), fast_str_ptr, STR_PTR, 0); + break; + + case OP_POSUPTO: + SLJIT_ASSERT(fast_str_ptr == 0); +#if defined SUPPORT_UTF && !defined COMPILE_PCRE32 + if (common->utf) + { + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, STR_PTR, 0); + OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max); + label = LABEL(); + compile_char1_matchingpath(common, type, cc, &no_match, TRUE); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1, STR_PTR, 0); + OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, label); + set_jumps(no_match, LABEL()); + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), POSSESSIVE1); + break; + } +#endif + OP1(SLJIT_MOV, tmp_base, tmp_offset, SLJIT_IMM, max); + label = LABEL(); + detect_partial_match(common, &no_match); + compile_char1_matchingpath(common, type, cc, &no_char1_match, FALSE); + OP2(SLJIT_SUB | SLJIT_SET_Z, tmp_base, tmp_offset, tmp_base, tmp_offset, SLJIT_IMM, 1); + JUMPTO(SLJIT_NOT_ZERO, label); + OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + set_jumps(no_char1_match, LABEL()); + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + set_jumps(no_match, LABEL()); + break; + + case OP_POSQUERY: + SLJIT_ASSERT(fast_str_ptr == 0); + OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0); + compile_char1_matchingpath(common, type, cc, &no_match, TRUE); + OP1(SLJIT_MOV, tmp_base, tmp_offset, STR_PTR, 0); + set_jumps(no_match, LABEL()); + OP1(SLJIT_MOV, STR_PTR, 0, tmp_base, tmp_offset); + break; + + default: + SLJIT_UNREACHABLE(); + break; + } + +count_match(common); +return end; +} + +static SLJIT_INLINE pcre_uchar *compile_fail_accept_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent) +{ +DEFINE_COMPILER; +backtrack_common *backtrack; + +PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL); + +if (*cc == OP_FAIL) + { + add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_JUMP)); + return cc + 1; + } + +if (*cc == OP_ASSERT_ACCEPT || common->currententry != NULL || !common->might_be_empty) + { + /* No need to check notempty conditions. */ + if (common->accept_label == NULL) + add_jump(compiler, &common->accept, JUMP(SLJIT_JUMP)); + else + JUMPTO(SLJIT_JUMP, common->accept_label); + return cc + 1; + } + +if (common->accept_label == NULL) + add_jump(compiler, &common->accept, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0))); +else + CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0), common->accept_label); +OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0); +OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, notempty)); +add_jump(compiler, &backtrack->topbacktracks, CMP(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0)); +OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, notempty_atstart)); +if (common->accept_label == NULL) + add_jump(compiler, &common->accept, CMP(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, 0)); +else + CMPTO(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, 0, common->accept_label); +OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str)); +if (common->accept_label == NULL) + add_jump(compiler, &common->accept, CMP(SLJIT_NOT_EQUAL, TMP2, 0, STR_PTR, 0)); +else + CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, STR_PTR, 0, common->accept_label); +add_jump(compiler, &backtrack->topbacktracks, JUMP(SLJIT_JUMP)); +return cc + 1; +} + +static SLJIT_INLINE pcre_uchar *compile_close_matchingpath(compiler_common *common, pcre_uchar *cc) +{ +DEFINE_COMPILER; +int offset = GET2(cc, 1); +BOOL optimized_cbracket = common->optimized_cbracket[offset] != 0; + +/* Data will be discarded anyway... */ +if (common->currententry != NULL) + return cc + 1 + IMM2_SIZE; + +if (!optimized_cbracket) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR_PRIV(offset)); +offset <<= 1; +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0); +if (!optimized_cbracket) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0); +return cc + 1 + IMM2_SIZE; +} + +static SLJIT_INLINE pcre_uchar *compile_control_verb_matchingpath(compiler_common *common, pcre_uchar *cc, backtrack_common *parent) +{ +DEFINE_COMPILER; +backtrack_common *backtrack; +pcre_uchar opcode = *cc; +pcre_uchar *ccend = cc + 1; + +if (opcode == OP_PRUNE_ARG || opcode == OP_SKIP_ARG || opcode == OP_THEN_ARG) + ccend += 2 + cc[1]; + +PUSH_BACKTRACK(sizeof(backtrack_common), cc, NULL); + +if (opcode == OP_SKIP) + { + allocate_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + return ccend; + } + +if (opcode == OP_PRUNE_ARG || opcode == OP_THEN_ARG) + { + OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, TMP2, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), TMP2, 0); + } + +return ccend; +} + +static pcre_uchar then_trap_opcode[1] = { OP_THEN_TRAP }; + +static SLJIT_INLINE void compile_then_trap_matchingpath(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, backtrack_common *parent) +{ +DEFINE_COMPILER; +backtrack_common *backtrack; +BOOL needs_control_head; +int size; + +PUSH_BACKTRACK_NOVALUE(sizeof(then_trap_backtrack), cc); +common->then_trap = BACKTRACK_AS(then_trap_backtrack); +BACKTRACK_AS(then_trap_backtrack)->common.cc = then_trap_opcode; +BACKTRACK_AS(then_trap_backtrack)->start = (sljit_sw)(cc - common->start); +BACKTRACK_AS(then_trap_backtrack)->framesize = get_framesize(common, cc, ccend, FALSE, &needs_control_head); + +size = BACKTRACK_AS(then_trap_backtrack)->framesize; +size = 3 + (size < 0 ? 0 : size); + +OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr); +allocate_stack(common, size); +if (size > 3) + OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, STACK_TOP, 0, SLJIT_IMM, (size - 3) * sizeof(sljit_sw)); +else + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, STACK_TOP, 0); +OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 1), SLJIT_IMM, BACKTRACK_AS(then_trap_backtrack)->start); +OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 2), SLJIT_IMM, type_then_trap); +OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(size - 3), TMP2, 0); + +size = BACKTRACK_AS(then_trap_backtrack)->framesize; +if (size >= 0) + init_frame(common, cc, ccend, size - 1, 0, FALSE); +} + +static void compile_matchingpath(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, backtrack_common *parent) +{ +DEFINE_COMPILER; +backtrack_common *backtrack; +BOOL has_then_trap = FALSE; +then_trap_backtrack *save_then_trap = NULL; + +SLJIT_ASSERT(*ccend == OP_END || (*ccend >= OP_ALT && *ccend <= OP_KETRPOS)); + +if (common->has_then && common->then_offsets[cc - common->start] != 0) + { + SLJIT_ASSERT(*ccend != OP_END && common->control_head_ptr != 0); + has_then_trap = TRUE; + save_then_trap = common->then_trap; + /* Tail item on backtrack. */ + compile_then_trap_matchingpath(common, cc, ccend, parent); + } + +while (cc < ccend) + { + switch(*cc) + { + case OP_SOD: + case OP_SOM: + case OP_NOT_WORD_BOUNDARY: + case OP_WORD_BOUNDARY: + case OP_EODN: + case OP_EOD: + case OP_DOLL: + case OP_DOLLM: + case OP_CIRC: + case OP_CIRCM: + case OP_REVERSE: + cc = compile_simple_assertion_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks); + break; + + case OP_NOT_DIGIT: + case OP_DIGIT: + case OP_NOT_WHITESPACE: + case OP_WHITESPACE: + case OP_NOT_WORDCHAR: + case OP_WORDCHAR: + case OP_ANY: + case OP_ALLANY: + case OP_ANYBYTE: + case OP_NOTPROP: + case OP_PROP: + case OP_ANYNL: + case OP_NOT_HSPACE: + case OP_HSPACE: + case OP_NOT_VSPACE: + case OP_VSPACE: + case OP_EXTUNI: + case OP_NOT: + case OP_NOTI: + cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE); + break; + + case OP_SET_SOM: + PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0)); + allocate_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(0), STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP2, 0); + cc++; + break; + + case OP_CHAR: + case OP_CHARI: + if (common->mode == JIT_COMPILE) + cc = compile_charn_matchingpath(common, cc, ccend, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks); + else + cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE); + break; + + case OP_STAR: + case OP_MINSTAR: + case OP_PLUS: + case OP_MINPLUS: + case OP_QUERY: + case OP_MINQUERY: + case OP_UPTO: + case OP_MINUPTO: + case OP_EXACT: + case OP_POSSTAR: + case OP_POSPLUS: + case OP_POSQUERY: + case OP_POSUPTO: + case OP_STARI: + case OP_MINSTARI: + case OP_PLUSI: + case OP_MINPLUSI: + case OP_QUERYI: + case OP_MINQUERYI: + case OP_UPTOI: + case OP_MINUPTOI: + case OP_EXACTI: + case OP_POSSTARI: + case OP_POSPLUSI: + case OP_POSQUERYI: + case OP_POSUPTOI: + case OP_NOTSTAR: + case OP_NOTMINSTAR: + case OP_NOTPLUS: + case OP_NOTMINPLUS: + case OP_NOTQUERY: + case OP_NOTMINQUERY: + case OP_NOTUPTO: + case OP_NOTMINUPTO: + case OP_NOTEXACT: + case OP_NOTPOSSTAR: + case OP_NOTPOSPLUS: + case OP_NOTPOSQUERY: + case OP_NOTPOSUPTO: + case OP_NOTSTARI: + case OP_NOTMINSTARI: + case OP_NOTPLUSI: + case OP_NOTMINPLUSI: + case OP_NOTQUERYI: + case OP_NOTMINQUERYI: + case OP_NOTUPTOI: + case OP_NOTMINUPTOI: + case OP_NOTEXACTI: + case OP_NOTPOSSTARI: + case OP_NOTPOSPLUSI: + case OP_NOTPOSQUERYI: + case OP_NOTPOSUPTOI: + case OP_TYPESTAR: + case OP_TYPEMINSTAR: + case OP_TYPEPLUS: + case OP_TYPEMINPLUS: + case OP_TYPEQUERY: + case OP_TYPEMINQUERY: + case OP_TYPEUPTO: + case OP_TYPEMINUPTO: + case OP_TYPEEXACT: + case OP_TYPEPOSSTAR: + case OP_TYPEPOSPLUS: + case OP_TYPEPOSQUERY: + case OP_TYPEPOSUPTO: + cc = compile_iterator_matchingpath(common, cc, parent); + break; + + case OP_CLASS: + case OP_NCLASS: + if (cc[1 + (32 / sizeof(pcre_uchar))] >= OP_CRSTAR && cc[1 + (32 / sizeof(pcre_uchar))] <= OP_CRPOSRANGE) + cc = compile_iterator_matchingpath(common, cc, parent); + else + cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE); + break; + +#if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + case OP_XCLASS: + if (*(cc + GET(cc, 1)) >= OP_CRSTAR && *(cc + GET(cc, 1)) <= OP_CRPOSRANGE) + cc = compile_iterator_matchingpath(common, cc, parent); + else + cc = compile_char1_matchingpath(common, *cc, cc + 1, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE); + break; +#endif + + case OP_REF: + case OP_REFI: + if (cc[1 + IMM2_SIZE] >= OP_CRSTAR && cc[1 + IMM2_SIZE] <= OP_CRPOSRANGE) + cc = compile_ref_iterator_matchingpath(common, cc, parent); + else + { + compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE); + cc += 1 + IMM2_SIZE; + } + break; + + case OP_DNREF: + case OP_DNREFI: + if (cc[1 + 2 * IMM2_SIZE] >= OP_CRSTAR && cc[1 + 2 * IMM2_SIZE] <= OP_CRPOSRANGE) + cc = compile_ref_iterator_matchingpath(common, cc, parent); + else + { + compile_dnref_search(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks); + compile_ref_matchingpath(common, cc, parent->top != NULL ? &parent->top->nextbacktracks : &parent->topbacktracks, TRUE, FALSE); + cc += 1 + 2 * IMM2_SIZE; + } + break; + + case OP_RECURSE: + cc = compile_recurse_matchingpath(common, cc, parent); + break; + + case OP_CALLOUT: + cc = compile_callout_matchingpath(common, cc, parent); + break; + + case OP_ASSERT: + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + PUSH_BACKTRACK_NOVALUE(sizeof(assert_backtrack), cc); + cc = compile_assert_matchingpath(common, cc, BACKTRACK_AS(assert_backtrack), FALSE); + break; + + case OP_BRAMINZERO: + PUSH_BACKTRACK_NOVALUE(sizeof(braminzero_backtrack), cc); + cc = bracketend(cc + 1); + if (*(cc - 1 - LINK_SIZE) != OP_KETRMIN) + { + allocate_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + } + else + { + allocate_stack(common, 2); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), STR_PTR, 0); + } + BACKTRACK_AS(braminzero_backtrack)->matchingpath = LABEL(); + count_match(common); + break; + + case OP_ONCE: + case OP_ONCE_NC: + case OP_BRA: + case OP_CBRA: + case OP_COND: + case OP_SBRA: + case OP_SCBRA: + case OP_SCOND: + cc = compile_bracket_matchingpath(common, cc, parent); + break; + + case OP_BRAZERO: + if (cc[1] > OP_ASSERTBACK_NOT) + cc = compile_bracket_matchingpath(common, cc, parent); + else + { + PUSH_BACKTRACK_NOVALUE(sizeof(assert_backtrack), cc); + cc = compile_assert_matchingpath(common, cc, BACKTRACK_AS(assert_backtrack), FALSE); + } + break; + + case OP_BRAPOS: + case OP_CBRAPOS: + case OP_SBRAPOS: + case OP_SCBRAPOS: + case OP_BRAPOSZERO: + cc = compile_bracketpos_matchingpath(common, cc, parent); + break; + + case OP_MARK: + PUSH_BACKTRACK_NOVALUE(sizeof(backtrack_common), cc); + SLJIT_ASSERT(common->mark_ptr != 0); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), common->mark_ptr); + allocate_stack(common, common->has_skip_arg ? 5 : 1); + OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(common->has_skip_arg ? 4 : 0), TMP2, 0); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)(cc + 2)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, TMP2, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), TMP2, 0); + if (common->has_skip_arg) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, STACK_TOP, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(1), SLJIT_IMM, type_mark); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(2), SLJIT_IMM, (sljit_sw)(cc + 2)); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(3), STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), TMP1, 0); + } + cc += 1 + 2 + cc[1]; + break; + + case OP_PRUNE: + case OP_PRUNE_ARG: + case OP_SKIP: + case OP_SKIP_ARG: + case OP_THEN: + case OP_THEN_ARG: + case OP_COMMIT: + cc = compile_control_verb_matchingpath(common, cc, parent); + break; + + case OP_FAIL: + case OP_ACCEPT: + case OP_ASSERT_ACCEPT: + cc = compile_fail_accept_matchingpath(common, cc, parent); + break; + + case OP_CLOSE: + cc = compile_close_matchingpath(common, cc); + break; + + case OP_SKIPZERO: + cc = bracketend(cc + 1); + break; + + default: + SLJIT_UNREACHABLE(); + return; + } + if (cc == NULL) + return; + } + +if (has_then_trap) + { + /* Head item on backtrack. */ + PUSH_BACKTRACK_NOVALUE(sizeof(then_trap_backtrack), cc); + BACKTRACK_AS(then_trap_backtrack)->common.cc = then_trap_opcode; + BACKTRACK_AS(then_trap_backtrack)->then_trap = common->then_trap; + common->then_trap = save_then_trap; + } +SLJIT_ASSERT(cc == ccend); +} + +#undef PUSH_BACKTRACK +#undef PUSH_BACKTRACK_NOVALUE +#undef BACKTRACK_AS + +#define COMPILE_BACKTRACKINGPATH(current) \ + do \ + { \ + compile_backtrackingpath(common, (current)); \ + if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) \ + return; \ + } \ + while (0) + +#define CURRENT_AS(type) ((type *)current) + +static void compile_iterator_backtrackingpath(compiler_common *common, struct backtrack_common *current) +{ +DEFINE_COMPILER; +pcre_uchar *cc = current->cc; +pcre_uchar opcode; +pcre_uchar type; +sljit_u32 max = 0, exact; +struct sljit_label *label = NULL; +struct sljit_jump *jump = NULL; +jump_list *jumplist = NULL; +pcre_uchar *end; +int private_data_ptr = PRIVATE_DATA(cc); +int base = (private_data_ptr == 0) ? SLJIT_MEM1(STACK_TOP) : SLJIT_MEM1(SLJIT_SP); +int offset0 = (private_data_ptr == 0) ? STACK(0) : private_data_ptr; +int offset1 = (private_data_ptr == 0) ? STACK(1) : private_data_ptr + (int)sizeof(sljit_sw); + +cc = get_iterator_parameters(common, cc, &opcode, &type, &max, &exact, &end); + +switch(opcode) + { + case OP_STAR: + case OP_UPTO: + if (type == OP_ANYNL || type == OP_EXTUNI) + { + SLJIT_ASSERT(private_data_ptr == 0); + set_jumps(CURRENT_AS(char_iterator_backtrack)->u.backtracks, LABEL()); + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + free_stack(common, 1); + CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(char_iterator_backtrack)->matchingpath); + } + else + { + if (CURRENT_AS(char_iterator_backtrack)->u.charpos.enabled) + { + OP1(SLJIT_MOV, STR_PTR, 0, base, offset0); + OP1(SLJIT_MOV, TMP2, 0, base, offset1); + OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1)); + + jump = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, TMP2, 0); + label = LABEL(); + OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1)); + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + if (CURRENT_AS(char_iterator_backtrack)->u.charpos.othercasebit != 0) + OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, CURRENT_AS(char_iterator_backtrack)->u.charpos.othercasebit); + CMPTO(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, CURRENT_AS(char_iterator_backtrack)->u.charpos.chr, CURRENT_AS(char_iterator_backtrack)->matchingpath); + skip_char_back(common); + CMPTO(SLJIT_GREATER, STR_PTR, 0, TMP2, 0, label); + } + else + { + OP1(SLJIT_MOV, STR_PTR, 0, base, offset0); + jump = CMP(SLJIT_LESS_EQUAL, STR_PTR, 0, base, offset1); + skip_char_back(common); + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath); + } + JUMPHERE(jump); + if (private_data_ptr == 0) + free_stack(common, 2); + } + break; + + case OP_MINSTAR: + OP1(SLJIT_MOV, STR_PTR, 0, base, offset0); + compile_char1_matchingpath(common, type, cc, &jumplist, TRUE); + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath); + set_jumps(jumplist, LABEL()); + if (private_data_ptr == 0) + free_stack(common, 1); + break; + + case OP_MINUPTO: + OP1(SLJIT_MOV, TMP1, 0, base, offset1); + OP1(SLJIT_MOV, STR_PTR, 0, base, offset0); + OP2(SLJIT_SUB | SLJIT_SET_Z, TMP1, 0, TMP1, 0, SLJIT_IMM, 1); + add_jump(compiler, &jumplist, JUMP(SLJIT_ZERO)); + + OP1(SLJIT_MOV, base, offset1, TMP1, 0); + compile_char1_matchingpath(common, type, cc, &jumplist, TRUE); + OP1(SLJIT_MOV, base, offset0, STR_PTR, 0); + JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath); + + set_jumps(jumplist, LABEL()); + if (private_data_ptr == 0) + free_stack(common, 2); + break; + + case OP_QUERY: + OP1(SLJIT_MOV, STR_PTR, 0, base, offset0); + OP1(SLJIT_MOV, base, offset0, SLJIT_IMM, 0); + CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(char_iterator_backtrack)->matchingpath); + jump = JUMP(SLJIT_JUMP); + set_jumps(CURRENT_AS(char_iterator_backtrack)->u.backtracks, LABEL()); + OP1(SLJIT_MOV, STR_PTR, 0, base, offset0); + OP1(SLJIT_MOV, base, offset0, SLJIT_IMM, 0); + JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath); + JUMPHERE(jump); + if (private_data_ptr == 0) + free_stack(common, 1); + break; + + case OP_MINQUERY: + OP1(SLJIT_MOV, STR_PTR, 0, base, offset0); + OP1(SLJIT_MOV, base, offset0, SLJIT_IMM, 0); + jump = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0); + compile_char1_matchingpath(common, type, cc, &jumplist, TRUE); + JUMPTO(SLJIT_JUMP, CURRENT_AS(char_iterator_backtrack)->matchingpath); + set_jumps(jumplist, LABEL()); + JUMPHERE(jump); + if (private_data_ptr == 0) + free_stack(common, 1); + break; + + case OP_EXACT: + case OP_POSSTAR: + case OP_POSQUERY: + case OP_POSUPTO: + break; + + default: + SLJIT_UNREACHABLE(); + break; + } + +set_jumps(current->topbacktracks, LABEL()); +} + +static SLJIT_INLINE void compile_ref_iterator_backtrackingpath(compiler_common *common, struct backtrack_common *current) +{ +DEFINE_COMPILER; +pcre_uchar *cc = current->cc; +BOOL ref = (*cc == OP_REF || *cc == OP_REFI); +pcre_uchar type; + +type = cc[ref ? 1 + IMM2_SIZE : 1 + 2 * IMM2_SIZE]; + +if ((type & 0x1) == 0) + { + /* Maximize case. */ + set_jumps(current->topbacktracks, LABEL()); + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + free_stack(common, 1); + CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(ref_iterator_backtrack)->matchingpath); + return; + } + +OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); +CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(ref_iterator_backtrack)->matchingpath); +set_jumps(current->topbacktracks, LABEL()); +free_stack(common, ref ? 2 : 3); +} + +static SLJIT_INLINE void compile_recurse_backtrackingpath(compiler_common *common, struct backtrack_common *current) +{ +DEFINE_COMPILER; + +if (CURRENT_AS(recurse_backtrack)->inlined_pattern) + compile_backtrackingpath(common, current->top); +set_jumps(current->topbacktracks, LABEL()); +if (CURRENT_AS(recurse_backtrack)->inlined_pattern) + return; + +if (common->has_set_som && common->mark_ptr != 0) + { + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(1)); + free_stack(common, 2); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(0), TMP2, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, TMP1, 0); + } +else if (common->has_set_som || common->mark_ptr != 0) + { + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + free_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->has_set_som ? (int)(OVECTOR(0)) : common->mark_ptr, TMP2, 0); + } +} + +static void compile_assert_backtrackingpath(compiler_common *common, struct backtrack_common *current) +{ +DEFINE_COMPILER; +pcre_uchar *cc = current->cc; +pcre_uchar bra = OP_BRA; +struct sljit_jump *brajump = NULL; + +SLJIT_ASSERT(*cc != OP_BRAMINZERO); +if (*cc == OP_BRAZERO) + { + bra = *cc; + cc++; + } + +if (bra == OP_BRAZERO) + { + SLJIT_ASSERT(current->topbacktracks == NULL); + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + } + +if (CURRENT_AS(assert_backtrack)->framesize < 0) + { + set_jumps(current->topbacktracks, LABEL()); + + if (bra == OP_BRAZERO) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); + CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(assert_backtrack)->matchingpath); + free_stack(common, 1); + } + return; + } + +if (bra == OP_BRAZERO) + { + if (*cc == OP_ASSERT_NOT || *cc == OP_ASSERTBACK_NOT) + { + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); + CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(assert_backtrack)->matchingpath); + free_stack(common, 1); + return; + } + free_stack(common, 1); + brajump = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0); + } + +if (*cc == OP_ASSERT || *cc == OP_ASSERTBACK) + { + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), CURRENT_AS(assert_backtrack)->private_data_ptr); + add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), CURRENT_AS(assert_backtrack)->private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-CURRENT_AS(assert_backtrack)->framesize - 1)); + + set_jumps(current->topbacktracks, LABEL()); + } +else + set_jumps(current->topbacktracks, LABEL()); + +if (bra == OP_BRAZERO) + { + /* We know there is enough place on the stack. */ + OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, sizeof(sljit_sw)); + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), SLJIT_IMM, 0); + JUMPTO(SLJIT_JUMP, CURRENT_AS(assert_backtrack)->matchingpath); + JUMPHERE(brajump); + } +} + +static void compile_bracket_backtrackingpath(compiler_common *common, struct backtrack_common *current) +{ +DEFINE_COMPILER; +int opcode, stacksize, alt_count, alt_max; +int offset = 0; +int private_data_ptr = CURRENT_AS(bracket_backtrack)->private_data_ptr; +int repeat_ptr = 0, repeat_type = 0, repeat_count = 0; +pcre_uchar *cc = current->cc; +pcre_uchar *ccbegin; +pcre_uchar *ccprev; +pcre_uchar bra = OP_BRA; +pcre_uchar ket; +assert_backtrack *assert; +sljit_uw *next_update_addr = NULL; +BOOL has_alternatives; +BOOL needs_control_head = FALSE; +struct sljit_jump *brazero = NULL; +struct sljit_jump *alt1 = NULL; +struct sljit_jump *alt2 = NULL; +struct sljit_jump *once = NULL; +struct sljit_jump *cond = NULL; +struct sljit_label *rmin_label = NULL; +struct sljit_label *exact_label = NULL; + +if (*cc == OP_BRAZERO || *cc == OP_BRAMINZERO) + { + bra = *cc; + cc++; + } + +opcode = *cc; +ccbegin = bracketend(cc) - 1 - LINK_SIZE; +ket = *ccbegin; +if (ket == OP_KET && PRIVATE_DATA(ccbegin) != 0) + { + repeat_ptr = PRIVATE_DATA(ccbegin); + repeat_type = PRIVATE_DATA(ccbegin + 2); + repeat_count = PRIVATE_DATA(ccbegin + 3); + SLJIT_ASSERT(repeat_type != 0 && repeat_count != 0); + if (repeat_type == OP_UPTO) + ket = OP_KETRMAX; + if (repeat_type == OP_MINUPTO) + ket = OP_KETRMIN; + } +ccbegin = cc; +cc += GET(cc, 1); +has_alternatives = *cc == OP_ALT; +if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND)) + has_alternatives = (ccbegin[1 + LINK_SIZE] >= OP_ASSERT && ccbegin[1 + LINK_SIZE] <= OP_ASSERTBACK_NOT) || CURRENT_AS(bracket_backtrack)->u.condfailed != NULL; +if (opcode == OP_CBRA || opcode == OP_SCBRA) + offset = (GET2(ccbegin, 1 + LINK_SIZE)) << 1; +if (SLJIT_UNLIKELY(opcode == OP_COND) && (*cc == OP_KETRMAX || *cc == OP_KETRMIN)) + opcode = OP_SCOND; +if (SLJIT_UNLIKELY(opcode == OP_ONCE_NC)) + opcode = OP_ONCE; + +alt_max = has_alternatives ? no_alternatives(ccbegin) : 0; + +/* Decoding the needs_control_head in framesize. */ +if (opcode == OP_ONCE) + { + needs_control_head = (CURRENT_AS(bracket_backtrack)->u.framesize & 0x1) != 0; + CURRENT_AS(bracket_backtrack)->u.framesize >>= 1; + } + +if (ket != OP_KET && repeat_type != 0) + { + /* TMP1 is used in OP_KETRMIN below. */ + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + free_stack(common, 1); + if (repeat_type == OP_UPTO) + OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), repeat_ptr, TMP1, 0, SLJIT_IMM, 1); + else + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), repeat_ptr, TMP1, 0); + } + +if (ket == OP_KETRMAX) + { + if (bra == OP_BRAZERO) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + free_stack(common, 1); + brazero = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, 0); + } + } +else if (ket == OP_KETRMIN) + { + if (bra != OP_BRAMINZERO) + { + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + if (repeat_type != 0) + { + /* TMP1 was set a few lines above. */ + CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, CURRENT_AS(bracket_backtrack)->recursive_matchingpath); + /* Drop STR_PTR for non-greedy plus quantifier. */ + if (opcode != OP_ONCE) + free_stack(common, 1); + } + else if (opcode >= OP_SBRA || opcode == OP_ONCE) + { + /* Checking zero-length iteration. */ + if (opcode != OP_ONCE || CURRENT_AS(bracket_backtrack)->u.framesize < 0) + CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr, CURRENT_AS(bracket_backtrack)->recursive_matchingpath); + else + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_MEM1(TMP1), STACK(-CURRENT_AS(bracket_backtrack)->u.framesize - 2), CURRENT_AS(bracket_backtrack)->recursive_matchingpath); + } + /* Drop STR_PTR for non-greedy plus quantifier. */ + if (opcode != OP_ONCE) + free_stack(common, 1); + } + else + JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->recursive_matchingpath); + } + rmin_label = LABEL(); + if (repeat_type != 0) + OP2(SLJIT_ADD, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1); + } +else if (bra == OP_BRAZERO) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + free_stack(common, 1); + brazero = CMP(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0); + } +else if (repeat_type == OP_EXACT) + { + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1); + exact_label = LABEL(); + } + +if (offset != 0) + { + if (common->capture_last_ptr != 0) + { + SLJIT_ASSERT(common->optimized_cbracket[offset >> 1] == 0); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(1)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, TMP1, 0); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(2)); + free_stack(common, 3); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP2, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP1, 0); + } + else if (common->optimized_cbracket[offset >> 1] == 0) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(1)); + free_stack(common, 2); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP2, 0); + } + } + +if (SLJIT_UNLIKELY(opcode == OP_ONCE)) + { + if (CURRENT_AS(bracket_backtrack)->u.framesize >= 0) + { + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL)); + } + once = JUMP(SLJIT_JUMP); + } +else if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND)) + { + if (has_alternatives) + { + /* Always exactly one alternative. */ + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + free_stack(common, 1); + + alt_max = 2; + alt1 = CMP(SLJIT_EQUAL, TMP1, 0, SLJIT_IMM, sizeof(sljit_uw)); + } + } +else if (has_alternatives) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + free_stack(common, 1); + + if (alt_max > 4) + { + /* Table jump if alt_max is greater than 4. */ + next_update_addr = allocate_read_only_data(common, alt_max * sizeof(sljit_uw)); + if (SLJIT_UNLIKELY(next_update_addr == NULL)) + return; + sljit_emit_ijump(compiler, SLJIT_JUMP, SLJIT_MEM1(TMP1), (sljit_sw)next_update_addr); + add_label_addr(common, next_update_addr++); + } + else + { + if (alt_max == 4) + alt2 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_uw)); + alt1 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, sizeof(sljit_uw)); + } + } + +COMPILE_BACKTRACKINGPATH(current->top); +if (current->topbacktracks) + set_jumps(current->topbacktracks, LABEL()); + +if (SLJIT_UNLIKELY(opcode == OP_COND) || SLJIT_UNLIKELY(opcode == OP_SCOND)) + { + /* Conditional block always has at most one alternative. */ + if (ccbegin[1 + LINK_SIZE] >= OP_ASSERT && ccbegin[1 + LINK_SIZE] <= OP_ASSERTBACK_NOT) + { + SLJIT_ASSERT(has_alternatives); + assert = CURRENT_AS(bracket_backtrack)->u.assert; + if (assert->framesize >= 0 && (ccbegin[1 + LINK_SIZE] == OP_ASSERT || ccbegin[1 + LINK_SIZE] == OP_ASSERTBACK)) + { + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), assert->private_data_ptr); + add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), assert->private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-assert->framesize - 1)); + } + cond = JUMP(SLJIT_JUMP); + set_jumps(CURRENT_AS(bracket_backtrack)->u.assert->condfailed, LABEL()); + } + else if (CURRENT_AS(bracket_backtrack)->u.condfailed != NULL) + { + SLJIT_ASSERT(has_alternatives); + cond = JUMP(SLJIT_JUMP); + set_jumps(CURRENT_AS(bracket_backtrack)->u.condfailed, LABEL()); + } + else + SLJIT_ASSERT(!has_alternatives); + } + +if (has_alternatives) + { + alt_count = sizeof(sljit_uw); + do + { + current->top = NULL; + current->topbacktracks = NULL; + current->nextbacktracks = NULL; + /* Conditional blocks always have an additional alternative, even if it is empty. */ + if (*cc == OP_ALT) + { + ccprev = cc + 1 + LINK_SIZE; + cc += GET(cc, 1); + if (opcode != OP_COND && opcode != OP_SCOND) + { + if (opcode != OP_ONCE) + { + if (private_data_ptr != 0) + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), private_data_ptr); + else + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + } + else + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(needs_control_head ? 1 : 0)); + } + compile_matchingpath(common, ccprev, cc, current); + if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + return; + } + + /* Instructions after the current alternative is successfully matched. */ + /* There is a similar code in compile_bracket_matchingpath. */ + if (opcode == OP_ONCE) + match_once_common(common, ket, CURRENT_AS(bracket_backtrack)->u.framesize, private_data_ptr, has_alternatives, needs_control_head); + + stacksize = 0; + if (repeat_type == OP_MINUPTO) + { + /* We need to preserve the counter. TMP2 will be used below. */ + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_SP), repeat_ptr); + stacksize++; + } + if (ket != OP_KET || bra != OP_BRA) + stacksize++; + if (offset != 0) + { + if (common->capture_last_ptr != 0) + stacksize++; + if (common->optimized_cbracket[offset >> 1] == 0) + stacksize += 2; + } + if (opcode != OP_ONCE) + stacksize++; + + if (stacksize > 0) + allocate_stack(common, stacksize); + + stacksize = 0; + if (repeat_type == OP_MINUPTO) + { + /* TMP2 was set above. */ + OP2(SLJIT_SUB, SLJIT_MEM1(STACK_TOP), STACK(stacksize), TMP2, 0, SLJIT_IMM, 1); + stacksize++; + } + + if (ket != OP_KET || bra != OP_BRA) + { + if (ket != OP_KET) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), STR_PTR, 0); + else + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, 0); + stacksize++; + } + + if (offset != 0) + stacksize = match_capture_common(common, stacksize, offset, private_data_ptr); + + if (opcode != OP_ONCE) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacksize), SLJIT_IMM, alt_count); + + if (offset != 0 && ket == OP_KETRMAX && common->optimized_cbracket[offset >> 1] != 0) + { + /* If ket is not OP_KETRMAX, this code path is executed after the jump to alternative_matchingpath. */ + SLJIT_ASSERT(private_data_ptr == OVECTOR(offset + 0)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), STR_PTR, 0); + } + + JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->alternative_matchingpath); + + if (opcode != OP_ONCE) + { + if (alt_max > 4) + add_label_addr(common, next_update_addr++); + else + { + if (alt_count != 2 * sizeof(sljit_uw)) + { + JUMPHERE(alt1); + if (alt_max == 3 && alt_count == sizeof(sljit_uw)) + alt2 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_uw)); + } + else + { + JUMPHERE(alt2); + if (alt_max == 4) + alt1 = CMP(SLJIT_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, 3 * sizeof(sljit_uw)); + } + } + alt_count += sizeof(sljit_uw); + } + + COMPILE_BACKTRACKINGPATH(current->top); + if (current->topbacktracks) + set_jumps(current->topbacktracks, LABEL()); + SLJIT_ASSERT(!current->nextbacktracks); + } + while (*cc == OP_ALT); + + if (cond != NULL) + { + SLJIT_ASSERT(opcode == OP_COND || opcode == OP_SCOND); + assert = CURRENT_AS(bracket_backtrack)->u.assert; + if ((ccbegin[1 + LINK_SIZE] == OP_ASSERT_NOT || ccbegin[1 + LINK_SIZE] == OP_ASSERTBACK_NOT) && assert->framesize >= 0) + { + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), assert->private_data_ptr); + add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), assert->private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-assert->framesize - 1)); + } + JUMPHERE(cond); + } + + /* Free the STR_PTR. */ + if (private_data_ptr == 0) + free_stack(common, 1); + } + +if (offset != 0) + { + /* Using both tmp register is better for instruction scheduling. */ + if (common->optimized_cbracket[offset >> 1] != 0) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(1)); + free_stack(common, 2); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP2, 0); + } + else + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + free_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0); + } + } +else if (opcode == OP_SBRA || opcode == OP_SCOND) + { + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(0)); + free_stack(common, 1); + } +else if (opcode == OP_ONCE) + { + cc = ccbegin + GET(ccbegin, 1); + stacksize = needs_control_head ? 1 : 0; + + if (CURRENT_AS(bracket_backtrack)->u.framesize >= 0) + { + /* Reset head and drop saved frame. */ + stacksize += CURRENT_AS(bracket_backtrack)->u.framesize + ((ket != OP_KET || *cc == OP_ALT) ? 2 : 1); + } + else if (ket == OP_KETRMAX || (*cc == OP_ALT && ket != OP_KETRMIN)) + { + /* The STR_PTR must be released. */ + stacksize++; + } + + if (stacksize > 0) + free_stack(common, stacksize); + + JUMPHERE(once); + /* Restore previous private_data_ptr */ + if (CURRENT_AS(bracket_backtrack)->u.framesize >= 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-CURRENT_AS(bracket_backtrack)->u.framesize - 1)); + else if (ket == OP_KETRMIN) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(1)); + /* See the comment below. */ + free_stack(common, 2); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), private_data_ptr, TMP1, 0); + } + } + +if (repeat_type == OP_EXACT) + { + OP2(SLJIT_ADD, TMP1, 0, SLJIT_MEM1(SLJIT_SP), repeat_ptr, SLJIT_IMM, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), repeat_ptr, TMP1, 0); + CMPTO(SLJIT_LESS_EQUAL, TMP1, 0, SLJIT_IMM, repeat_count, exact_label); + } +else if (ket == OP_KETRMAX) + { + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + if (bra != OP_BRAZERO) + free_stack(common, 1); + + CMPTO(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0, CURRENT_AS(bracket_backtrack)->recursive_matchingpath); + if (bra == OP_BRAZERO) + { + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(1)); + JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->zero_matchingpath); + JUMPHERE(brazero); + free_stack(common, 1); + } + } +else if (ket == OP_KETRMIN) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + + /* OP_ONCE removes everything in case of a backtrack, so we don't + need to explicitly release the STR_PTR. The extra release would + affect badly the free_stack(2) above. */ + if (opcode != OP_ONCE) + free_stack(common, 1); + CMPTO(SLJIT_NOT_EQUAL, TMP1, 0, SLJIT_IMM, 0, rmin_label); + if (opcode == OP_ONCE) + free_stack(common, bra == OP_BRAMINZERO ? 2 : 1); + else if (bra == OP_BRAMINZERO) + free_stack(common, 1); + } +else if (bra == OP_BRAZERO) + { + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + JUMPTO(SLJIT_JUMP, CURRENT_AS(bracket_backtrack)->zero_matchingpath); + JUMPHERE(brazero); + } +} + +static SLJIT_INLINE void compile_bracketpos_backtrackingpath(compiler_common *common, struct backtrack_common *current) +{ +DEFINE_COMPILER; +int offset; +struct sljit_jump *jump; + +if (CURRENT_AS(bracketpos_backtrack)->framesize < 0) + { + if (*current->cc == OP_CBRAPOS || *current->cc == OP_SCBRAPOS) + { + offset = (GET2(current->cc, 1 + LINK_SIZE)) << 1; + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(1)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset), TMP1, 0); + if (common->capture_last_ptr != 0) + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(2)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(offset + 1), TMP2, 0); + if (common->capture_last_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, TMP1, 0); + } + set_jumps(current->topbacktracks, LABEL()); + free_stack(common, CURRENT_AS(bracketpos_backtrack)->stacksize); + return; + } + +OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), CURRENT_AS(bracketpos_backtrack)->private_data_ptr); +add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL)); + +if (current->topbacktracks) + { + jump = JUMP(SLJIT_JUMP); + set_jumps(current->topbacktracks, LABEL()); + /* Drop the stack frame. */ + free_stack(common, CURRENT_AS(bracketpos_backtrack)->stacksize); + JUMPHERE(jump); + } +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), CURRENT_AS(bracketpos_backtrack)->private_data_ptr, SLJIT_MEM1(STACK_TOP), STACK(-CURRENT_AS(bracketpos_backtrack)->framesize - 1)); +} + +static SLJIT_INLINE void compile_braminzero_backtrackingpath(compiler_common *common, struct backtrack_common *current) +{ +assert_backtrack backtrack; + +current->top = NULL; +current->topbacktracks = NULL; +current->nextbacktracks = NULL; +if (current->cc[1] > OP_ASSERTBACK_NOT) + { + /* Manual call of compile_bracket_matchingpath and compile_bracket_backtrackingpath. */ + compile_bracket_matchingpath(common, current->cc, current); + compile_bracket_backtrackingpath(common, current->top); + } +else + { + memset(&backtrack, 0, sizeof(backtrack)); + backtrack.common.cc = current->cc; + backtrack.matchingpath = CURRENT_AS(braminzero_backtrack)->matchingpath; + /* Manual call of compile_assert_matchingpath. */ + compile_assert_matchingpath(common, current->cc, &backtrack, FALSE); + } +SLJIT_ASSERT(!current->nextbacktracks && !current->topbacktracks); +} + +static SLJIT_INLINE void compile_control_verb_backtrackingpath(compiler_common *common, struct backtrack_common *current) +{ +DEFINE_COMPILER; +pcre_uchar opcode = *current->cc; +struct sljit_label *loop; +struct sljit_jump *jump; + +if (opcode == OP_THEN || opcode == OP_THEN_ARG) + { + if (common->then_trap != NULL) + { + SLJIT_ASSERT(common->control_head_ptr != 0); + + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, type_then_trap); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, common->then_trap->start); + jump = JUMP(SLJIT_JUMP); + + loop = LABEL(); + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + JUMPHERE(jump); + CMPTO(SLJIT_NOT_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(1), TMP1, 0, loop); + CMPTO(SLJIT_NOT_EQUAL, SLJIT_MEM1(STACK_TOP), STACK(2), TMP2, 0, loop); + add_jump(compiler, &common->then_trap->quit, JUMP(SLJIT_JUMP)); + return; + } + else if (common->positive_assert) + { + add_jump(compiler, &common->positive_assert_quit, JUMP(SLJIT_JUMP)); + return; + } + } + +if (common->local_exit) + { + if (common->quit_label == NULL) + add_jump(compiler, &common->quit, JUMP(SLJIT_JUMP)); + else + JUMPTO(SLJIT_JUMP, common->quit_label); + return; + } + +if (opcode == OP_SKIP_ARG) + { + SLJIT_ASSERT(common->control_head_ptr != 0); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS0, STACK_TOP, 0); + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, (sljit_sw)(current->cc + 2)); + sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(do_search_mark)); + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0); + + OP1(SLJIT_MOV, STR_PTR, 0, TMP1, 0); + add_jump(compiler, &common->reset_match, CMP(SLJIT_NOT_EQUAL, STR_PTR, 0, SLJIT_IMM, 0)); + return; + } + +if (opcode == OP_SKIP) + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); +else + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_IMM, 0); +add_jump(compiler, &common->reset_match, JUMP(SLJIT_JUMP)); +} + +static SLJIT_INLINE void compile_then_trap_backtrackingpath(compiler_common *common, struct backtrack_common *current) +{ +DEFINE_COMPILER; +struct sljit_jump *jump; +int size; + +if (CURRENT_AS(then_trap_backtrack)->then_trap) + { + common->then_trap = CURRENT_AS(then_trap_backtrack)->then_trap; + return; + } + +size = CURRENT_AS(then_trap_backtrack)->framesize; +size = 3 + (size < 0 ? 0 : size); + +OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(size - 3)); +free_stack(common, size); +jump = JUMP(SLJIT_JUMP); + +set_jumps(CURRENT_AS(then_trap_backtrack)->quit, LABEL()); +/* STACK_TOP is set by THEN. */ +if (CURRENT_AS(then_trap_backtrack)->framesize >= 0) + add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL)); +OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); +free_stack(common, 3); + +JUMPHERE(jump); +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, TMP1, 0); +} + +static void compile_backtrackingpath(compiler_common *common, struct backtrack_common *current) +{ +DEFINE_COMPILER; +then_trap_backtrack *save_then_trap = common->then_trap; + +while (current) + { + if (current->nextbacktracks != NULL) + set_jumps(current->nextbacktracks, LABEL()); + switch(*current->cc) + { + case OP_SET_SOM: + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + free_stack(common, 1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(0), TMP1, 0); + break; + + case OP_STAR: + case OP_MINSTAR: + case OP_PLUS: + case OP_MINPLUS: + case OP_QUERY: + case OP_MINQUERY: + case OP_UPTO: + case OP_MINUPTO: + case OP_EXACT: + case OP_POSSTAR: + case OP_POSPLUS: + case OP_POSQUERY: + case OP_POSUPTO: + case OP_STARI: + case OP_MINSTARI: + case OP_PLUSI: + case OP_MINPLUSI: + case OP_QUERYI: + case OP_MINQUERYI: + case OP_UPTOI: + case OP_MINUPTOI: + case OP_EXACTI: + case OP_POSSTARI: + case OP_POSPLUSI: + case OP_POSQUERYI: + case OP_POSUPTOI: + case OP_NOTSTAR: + case OP_NOTMINSTAR: + case OP_NOTPLUS: + case OP_NOTMINPLUS: + case OP_NOTQUERY: + case OP_NOTMINQUERY: + case OP_NOTUPTO: + case OP_NOTMINUPTO: + case OP_NOTEXACT: + case OP_NOTPOSSTAR: + case OP_NOTPOSPLUS: + case OP_NOTPOSQUERY: + case OP_NOTPOSUPTO: + case OP_NOTSTARI: + case OP_NOTMINSTARI: + case OP_NOTPLUSI: + case OP_NOTMINPLUSI: + case OP_NOTQUERYI: + case OP_NOTMINQUERYI: + case OP_NOTUPTOI: + case OP_NOTMINUPTOI: + case OP_NOTEXACTI: + case OP_NOTPOSSTARI: + case OP_NOTPOSPLUSI: + case OP_NOTPOSQUERYI: + case OP_NOTPOSUPTOI: + case OP_TYPESTAR: + case OP_TYPEMINSTAR: + case OP_TYPEPLUS: + case OP_TYPEMINPLUS: + case OP_TYPEQUERY: + case OP_TYPEMINQUERY: + case OP_TYPEUPTO: + case OP_TYPEMINUPTO: + case OP_TYPEEXACT: + case OP_TYPEPOSSTAR: + case OP_TYPEPOSPLUS: + case OP_TYPEPOSQUERY: + case OP_TYPEPOSUPTO: + case OP_CLASS: + case OP_NCLASS: +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: +#endif + compile_iterator_backtrackingpath(common, current); + break; + + case OP_REF: + case OP_REFI: + case OP_DNREF: + case OP_DNREFI: + compile_ref_iterator_backtrackingpath(common, current); + break; + + case OP_RECURSE: + compile_recurse_backtrackingpath(common, current); + break; + + case OP_ASSERT: + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + compile_assert_backtrackingpath(common, current); + break; + + case OP_ONCE: + case OP_ONCE_NC: + case OP_BRA: + case OP_CBRA: + case OP_COND: + case OP_SBRA: + case OP_SCBRA: + case OP_SCOND: + compile_bracket_backtrackingpath(common, current); + break; + + case OP_BRAZERO: + if (current->cc[1] > OP_ASSERTBACK_NOT) + compile_bracket_backtrackingpath(common, current); + else + compile_assert_backtrackingpath(common, current); + break; + + case OP_BRAPOS: + case OP_CBRAPOS: + case OP_SBRAPOS: + case OP_SCBRAPOS: + case OP_BRAPOSZERO: + compile_bracketpos_backtrackingpath(common, current); + break; + + case OP_BRAMINZERO: + compile_braminzero_backtrackingpath(common, current); + break; + + case OP_MARK: + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(common->has_skip_arg ? 4 : 0)); + if (common->has_skip_arg) + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + free_stack(common, common->has_skip_arg ? 5 : 1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, TMP1, 0); + if (common->has_skip_arg) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, TMP2, 0); + break; + + case OP_THEN: + case OP_THEN_ARG: + case OP_PRUNE: + case OP_PRUNE_ARG: + case OP_SKIP: + case OP_SKIP_ARG: + compile_control_verb_backtrackingpath(common, current); + break; + + case OP_COMMIT: + if (!common->local_exit) + OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_NOMATCH); + if (common->quit_label == NULL) + add_jump(compiler, &common->quit, JUMP(SLJIT_JUMP)); + else + JUMPTO(SLJIT_JUMP, common->quit_label); + break; + + case OP_CALLOUT: + case OP_FAIL: + case OP_ACCEPT: + case OP_ASSERT_ACCEPT: + set_jumps(current->topbacktracks, LABEL()); + break; + + case OP_THEN_TRAP: + /* A virtual opcode for then traps. */ + compile_then_trap_backtrackingpath(common, current); + break; + + default: + SLJIT_UNREACHABLE(); + break; + } + current = current->prev; + } +common->then_trap = save_then_trap; +} + +static SLJIT_INLINE void compile_recurse(compiler_common *common) +{ +DEFINE_COMPILER; +pcre_uchar *cc = common->start + common->currententry->start; +pcre_uchar *ccbegin = cc + 1 + LINK_SIZE + (*cc == OP_BRA ? 0 : IMM2_SIZE); +pcre_uchar *ccend = bracketend(cc) - (1 + LINK_SIZE); +BOOL needs_control_head; +int framesize = get_framesize(common, cc, NULL, TRUE, &needs_control_head); +int private_data_size = get_private_data_copy_length(common, ccbegin, ccend, needs_control_head); +int alternativesize; +BOOL needs_frame; +backtrack_common altbacktrack; +struct sljit_jump *jump; + +/* Recurse captures then. */ +common->then_trap = NULL; + +SLJIT_ASSERT(*cc == OP_BRA || *cc == OP_CBRA || *cc == OP_CBRAPOS || *cc == OP_SCBRA || *cc == OP_SCBRAPOS); +needs_frame = framesize >= 0; +if (!needs_frame) + framesize = 0; +alternativesize = *(cc + GET(cc, 1)) == OP_ALT ? 1 : 0; + +SLJIT_ASSERT(common->currententry->entry == NULL && common->recursive_head_ptr != 0); +common->currententry->entry = LABEL(); +set_jumps(common->currententry->calls, common->currententry->entry); + +sljit_emit_fast_enter(compiler, TMP2, 0); +count_match(common); +allocate_stack(common, private_data_size + framesize + alternativesize); +OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(private_data_size + framesize + alternativesize - 1), TMP2, 0); +copy_private_data(common, ccbegin, ccend, TRUE, framesize + alternativesize, private_data_size + framesize + alternativesize, needs_control_head); +if (needs_control_head) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0); +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->recursive_head_ptr, STACK_TOP, 0); +if (needs_frame) + init_frame(common, cc, NULL, framesize + alternativesize - 1, alternativesize, TRUE); + +if (alternativesize > 0) + OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(0), STR_PTR, 0); + +memset(&altbacktrack, 0, sizeof(backtrack_common)); +common->quit_label = NULL; +common->accept_label = NULL; +common->quit = NULL; +common->accept = NULL; +altbacktrack.cc = ccbegin; +cc += GET(cc, 1); +while (1) + { + altbacktrack.top = NULL; + altbacktrack.topbacktracks = NULL; + + if (altbacktrack.cc != ccbegin) + OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(STACK_TOP), STACK(0)); + + compile_matchingpath(common, altbacktrack.cc, cc, &altbacktrack); + if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + return; + + add_jump(compiler, &common->accept, JUMP(SLJIT_JUMP)); + + compile_backtrackingpath(common, altbacktrack.top); + if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + return; + set_jumps(altbacktrack.topbacktracks, LABEL()); + + if (*cc != OP_ALT) + break; + + altbacktrack.cc = cc + 1 + LINK_SIZE; + cc += GET(cc, 1); + } + +/* None of them matched. */ +OP1(SLJIT_MOV, TMP3, 0, SLJIT_IMM, 0); +jump = JUMP(SLJIT_JUMP); + +if (common->quit != NULL) + { + set_jumps(common->quit, LABEL()); + OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), common->recursive_head_ptr); + if (needs_frame) + { + OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (framesize + alternativesize) * sizeof(sljit_sw)); + add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL)); + OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (framesize + alternativesize) * sizeof(sljit_sw)); + } + OP1(SLJIT_MOV, TMP3, 0, SLJIT_IMM, 0); + common->quit = NULL; + add_jump(compiler, &common->quit, JUMP(SLJIT_JUMP)); + } + +set_jumps(common->accept, LABEL()); +OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), common->recursive_head_ptr); +if (needs_frame) + { + OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (framesize + alternativesize) * sizeof(sljit_sw)); + add_jump(compiler, &common->revertframes, JUMP(SLJIT_FAST_CALL)); + OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, (framesize + alternativesize) * sizeof(sljit_sw)); + } +OP1(SLJIT_MOV, TMP3, 0, SLJIT_IMM, 1); + +JUMPHERE(jump); +if (common->quit != NULL) + set_jumps(common->quit, LABEL()); +copy_private_data(common, ccbegin, ccend, FALSE, framesize + alternativesize, private_data_size + framesize + alternativesize, needs_control_head); +free_stack(common, private_data_size + framesize + alternativesize); +if (needs_control_head) + { + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), STACK(-3)); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(-2)); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->recursive_head_ptr, TMP1, 0); + OP1(SLJIT_MOV, TMP1, 0, TMP3, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, TMP2, 0); + } +else + { + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), STACK(-2)); + OP1(SLJIT_MOV, TMP1, 0, TMP3, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->recursive_head_ptr, TMP2, 0); + } +sljit_emit_fast_return(compiler, SLJIT_MEM1(STACK_TOP), STACK(-1)); +} + +#undef COMPILE_BACKTRACKINGPATH +#undef CURRENT_AS + +void +PRIV(jit_compile)(const REAL_PCRE *re, PUBL(extra) *extra, int mode) +{ +struct sljit_compiler *compiler; +backtrack_common rootbacktrack; +compiler_common common_data; +compiler_common *common = &common_data; +const sljit_u8 *tables = re->tables; +pcre_study_data *study; +int private_data_size; +pcre_uchar *ccend; +executable_functions *functions; +void *executable_func; +sljit_uw executable_size; +sljit_uw total_length; +label_addr_list *label_addr; +struct sljit_label *mainloop_label = NULL; +struct sljit_label *continue_match_label; +struct sljit_label *empty_match_found_label = NULL; +struct sljit_label *empty_match_backtrack_label = NULL; +struct sljit_label *reset_match_label; +struct sljit_label *quit_label; +struct sljit_jump *jump; +struct sljit_jump *minlength_check_failed = NULL; +struct sljit_jump *reqbyte_notfound = NULL; +struct sljit_jump *empty_match = NULL; + +SLJIT_ASSERT((extra->flags & PCRE_EXTRA_STUDY_DATA) != 0); +study = extra->study_data; + +if (!tables) + tables = PRIV(default_tables); + +memset(&rootbacktrack, 0, sizeof(backtrack_common)); +memset(common, 0, sizeof(compiler_common)); +rootbacktrack.cc = (pcre_uchar *)re + re->name_table_offset + re->name_count * re->name_entry_size; + +common->start = rootbacktrack.cc; +common->read_only_data_head = NULL; +common->fcc = tables + fcc_offset; +common->lcc = (sljit_sw)(tables + lcc_offset); +common->mode = mode; +common->might_be_empty = study->minlength == 0; +common->nltype = NLTYPE_FIXED; +switch(re->options & PCRE_NEWLINE_BITS) + { + case 0: + /* Compile-time default */ + switch(NEWLINE) + { + case -1: common->newline = (CHAR_CR << 8) | CHAR_NL; common->nltype = NLTYPE_ANY; break; + case -2: common->newline = (CHAR_CR << 8) | CHAR_NL; common->nltype = NLTYPE_ANYCRLF; break; + default: common->newline = NEWLINE; break; + } + break; + case PCRE_NEWLINE_CR: common->newline = CHAR_CR; break; + case PCRE_NEWLINE_LF: common->newline = CHAR_NL; break; + case PCRE_NEWLINE_CR+ + PCRE_NEWLINE_LF: common->newline = (CHAR_CR << 8) | CHAR_NL; break; + case PCRE_NEWLINE_ANY: common->newline = (CHAR_CR << 8) | CHAR_NL; common->nltype = NLTYPE_ANY; break; + case PCRE_NEWLINE_ANYCRLF: common->newline = (CHAR_CR << 8) | CHAR_NL; common->nltype = NLTYPE_ANYCRLF; break; + default: return; + } +common->nlmax = READ_CHAR_MAX; +common->nlmin = 0; +if ((re->options & PCRE_BSR_ANYCRLF) != 0) + common->bsr_nltype = NLTYPE_ANYCRLF; +else if ((re->options & PCRE_BSR_UNICODE) != 0) + common->bsr_nltype = NLTYPE_ANY; +else + { +#ifdef BSR_ANYCRLF + common->bsr_nltype = NLTYPE_ANYCRLF; +#else + common->bsr_nltype = NLTYPE_ANY; +#endif + } +common->bsr_nlmax = READ_CHAR_MAX; +common->bsr_nlmin = 0; +common->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0; +common->ctypes = (sljit_sw)(tables + ctypes_offset); +common->name_table = ((pcre_uchar *)re) + re->name_table_offset; +common->name_count = re->name_count; +common->name_entry_size = re->name_entry_size; +common->jscript_compat = (re->options & PCRE_JAVASCRIPT_COMPAT) != 0; +#ifdef SUPPORT_UTF +/* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */ +common->utf = (re->options & PCRE_UTF8) != 0; +#ifdef SUPPORT_UCP +common->use_ucp = (re->options & PCRE_UCP) != 0; +#endif +if (common->utf) + { + if (common->nltype == NLTYPE_ANY) + common->nlmax = 0x2029; + else if (common->nltype == NLTYPE_ANYCRLF) + common->nlmax = (CHAR_CR > CHAR_NL) ? CHAR_CR : CHAR_NL; + else + { + /* We only care about the first newline character. */ + common->nlmax = common->newline & 0xff; + } + + if (common->nltype == NLTYPE_FIXED) + common->nlmin = common->newline & 0xff; + else + common->nlmin = (CHAR_CR < CHAR_NL) ? CHAR_CR : CHAR_NL; + + if (common->bsr_nltype == NLTYPE_ANY) + common->bsr_nlmax = 0x2029; + else + common->bsr_nlmax = (CHAR_CR > CHAR_NL) ? CHAR_CR : CHAR_NL; + common->bsr_nlmin = (CHAR_CR < CHAR_NL) ? CHAR_CR : CHAR_NL; + } +#endif /* SUPPORT_UTF */ +ccend = bracketend(common->start); + +/* Calculate the local space size on the stack. */ +common->ovector_start = LIMIT_MATCH + sizeof(sljit_sw); +common->optimized_cbracket = (sljit_u8 *)SLJIT_MALLOC(re->top_bracket + 1, compiler->allocator_data); +if (!common->optimized_cbracket) + return; +#if defined DEBUG_FORCE_UNOPTIMIZED_CBRAS && DEBUG_FORCE_UNOPTIMIZED_CBRAS == 1 +memset(common->optimized_cbracket, 0, re->top_bracket + 1); +#else +memset(common->optimized_cbracket, 1, re->top_bracket + 1); +#endif + +SLJIT_ASSERT(*common->start == OP_BRA && ccend[-(1 + LINK_SIZE)] == OP_KET); +#if defined DEBUG_FORCE_UNOPTIMIZED_CBRAS && DEBUG_FORCE_UNOPTIMIZED_CBRAS == 2 +common->capture_last_ptr = common->ovector_start; +common->ovector_start += sizeof(sljit_sw); +#endif +if (!check_opcode_types(common, common->start, ccend)) + { + SLJIT_FREE(common->optimized_cbracket, compiler->allocator_data); + return; + } + +/* Checking flags and updating ovector_start. */ +if (mode == JIT_COMPILE && (re->flags & PCRE_REQCHSET) != 0 && (re->options & PCRE_NO_START_OPTIMIZE) == 0) + { + common->req_char_ptr = common->ovector_start; + common->ovector_start += sizeof(sljit_sw); + } +if (mode != JIT_COMPILE) + { + common->start_used_ptr = common->ovector_start; + common->ovector_start += sizeof(sljit_sw); + if (mode == JIT_PARTIAL_SOFT_COMPILE) + { + common->hit_start = common->ovector_start; + common->ovector_start += 2 * sizeof(sljit_sw); + } + } +if ((re->options & PCRE_FIRSTLINE) != 0) + { + common->match_end_ptr = common->ovector_start; + common->ovector_start += sizeof(sljit_sw); + } +#if defined DEBUG_FORCE_CONTROL_HEAD && DEBUG_FORCE_CONTROL_HEAD +common->control_head_ptr = 1; +#endif +if (common->control_head_ptr != 0) + { + common->control_head_ptr = common->ovector_start; + common->ovector_start += sizeof(sljit_sw); + } +if (common->has_set_som) + { + /* Saving the real start pointer is necessary. */ + common->start_ptr = common->ovector_start; + common->ovector_start += sizeof(sljit_sw); + } + +/* Aligning ovector to even number of sljit words. */ +if ((common->ovector_start & sizeof(sljit_sw)) != 0) + common->ovector_start += sizeof(sljit_sw); + +if (common->start_ptr == 0) + common->start_ptr = OVECTOR(0); + +/* Capturing brackets cannot be optimized if callouts are allowed. */ +if (common->capture_last_ptr != 0) + memset(common->optimized_cbracket, 0, re->top_bracket + 1); + +SLJIT_ASSERT(!(common->req_char_ptr != 0 && common->start_used_ptr != 0)); +common->cbra_ptr = OVECTOR_START + (re->top_bracket + 1) * 2 * sizeof(sljit_sw); + +total_length = ccend - common->start; +common->private_data_ptrs = (sljit_s32 *)SLJIT_MALLOC(total_length * (sizeof(sljit_s32) + (common->has_then ? 1 : 0)), compiler->allocator_data); +if (!common->private_data_ptrs) + { + SLJIT_FREE(common->optimized_cbracket, compiler->allocator_data); + return; + } +memset(common->private_data_ptrs, 0, total_length * sizeof(sljit_s32)); + +private_data_size = common->cbra_ptr + (re->top_bracket + 1) * sizeof(sljit_sw); +set_private_data_ptrs(common, &private_data_size, ccend); +if ((re->options & PCRE_ANCHORED) == 0 && (re->options & PCRE_NO_START_OPTIMIZE) == 0) + { + if (!detect_fast_forward_skip(common, &private_data_size) && !common->has_skip_in_assert_back) + detect_fast_fail(common, common->start, &private_data_size, 4); + } + +SLJIT_ASSERT(common->fast_fail_start_ptr <= common->fast_fail_end_ptr); + +if (private_data_size > SLJIT_MAX_LOCAL_SIZE) + { + SLJIT_FREE(common->private_data_ptrs, compiler->allocator_data); + SLJIT_FREE(common->optimized_cbracket, compiler->allocator_data); + return; + } + +if (common->has_then) + { + common->then_offsets = (sljit_u8 *)(common->private_data_ptrs + total_length); + memset(common->then_offsets, 0, total_length); + set_then_offsets(common, common->start, NULL); + } + +compiler = sljit_create_compiler(NULL); +if (!compiler) + { + SLJIT_FREE(common->optimized_cbracket, compiler->allocator_data); + SLJIT_FREE(common->private_data_ptrs, compiler->allocator_data); + return; + } +common->compiler = compiler; + +/* Main pcre_jit_exec entry. */ +sljit_emit_enter(compiler, 0, SLJIT_ARG1(SW), 5, 5, 0, 0, private_data_size); + +/* Register init. */ +reset_ovector(common, (re->top_bracket + 1) * 2); +if (common->req_char_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->req_char_ptr, SLJIT_R0, 0); + +OP1(SLJIT_MOV, ARGUMENTS, 0, SLJIT_S0, 0); +OP1(SLJIT_MOV, TMP1, 0, SLJIT_S0, 0); +OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str)); +OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, end)); +OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, stack)); +OP1(SLJIT_MOV_U32, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, limit_match)); +OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, end)); +OP1(SLJIT_MOV, STACK_LIMIT, 0, SLJIT_MEM1(TMP2), SLJIT_OFFSETOF(struct sljit_stack, start)); +OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1); +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LIMIT_MATCH, TMP1, 0); + +if (common->fast_fail_start_ptr < common->fast_fail_end_ptr) + reset_fast_fail(common); + +if (mode == JIT_PARTIAL_SOFT_COMPILE) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, -1); +if (common->mark_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->mark_ptr, SLJIT_IMM, 0); +if (common->control_head_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->control_head_ptr, SLJIT_IMM, 0); + +/* Main part of the matching */ +if ((re->options & PCRE_ANCHORED) == 0) + { + mainloop_label = mainloop_entry(common, (re->flags & PCRE_HASCRORLF) != 0); + continue_match_label = LABEL(); + /* Forward search if possible. */ + if ((re->options & PCRE_NO_START_OPTIMIZE) == 0) + { + if (mode == JIT_COMPILE && fast_forward_first_n_chars(common)) + ; + else if ((re->flags & PCRE_FIRSTSET) != 0) + fast_forward_first_char(common, (pcre_uchar)re->first_char, (re->flags & PCRE_FCH_CASELESS) != 0); + else if ((re->flags & PCRE_STARTLINE) != 0) + fast_forward_newline(common); + else if (study != NULL && (study->flags & PCRE_STUDY_MAPPED) != 0) + fast_forward_start_bits(common, study->start_bits); + } + } +else + continue_match_label = LABEL(); + +if (mode == JIT_COMPILE && study->minlength > 0 && (re->options & PCRE_NO_START_OPTIMIZE) == 0) + { + OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_NOMATCH); + OP2(SLJIT_ADD, TMP2, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(study->minlength)); + minlength_check_failed = CMP(SLJIT_GREATER, TMP2, 0, STR_END, 0); + } +if (common->req_char_ptr != 0) + reqbyte_notfound = search_requested_char(common, (pcre_uchar)re->req_char, (re->flags & PCRE_RCH_CASELESS) != 0, (re->flags & PCRE_FIRSTSET) != 0); + +/* Store the current STR_PTR in OVECTOR(0). */ +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), OVECTOR(0), STR_PTR, 0); +/* Copy the limit of allowed recursions. */ +OP1(SLJIT_MOV, COUNT_MATCH, 0, SLJIT_MEM1(SLJIT_SP), LIMIT_MATCH); +if (common->capture_last_ptr != 0) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->capture_last_ptr, SLJIT_IMM, -1); +if (common->fast_forward_bc_ptr != NULL) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), PRIVATE_DATA(common->fast_forward_bc_ptr + 1), STR_PTR, 0); + +if (common->start_ptr != OVECTOR(0)) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_ptr, STR_PTR, 0); + +/* Copy the beginning of the string. */ +if (mode == JIT_PARTIAL_SOFT_COMPILE) + { + jump = CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, -1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start + sizeof(sljit_sw), STR_PTR, 0); + JUMPHERE(jump); + } +else if (mode == JIT_PARTIAL_HARD_COMPILE) + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, STR_PTR, 0); + +compile_matchingpath(common, common->start, ccend, &rootbacktrack); +if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + { + sljit_free_compiler(compiler); + SLJIT_FREE(common->optimized_cbracket, compiler->allocator_data); + SLJIT_FREE(common->private_data_ptrs, compiler->allocator_data); + free_read_only_data(common->read_only_data_head, compiler->allocator_data); + return; + } + +if (common->might_be_empty) + { + empty_match = CMP(SLJIT_EQUAL, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), OVECTOR(0)); + empty_match_found_label = LABEL(); + } + +common->accept_label = LABEL(); +if (common->accept != NULL) + set_jumps(common->accept, common->accept_label); + +/* This means we have a match. Update the ovector. */ +copy_ovector(common, re->top_bracket + 1); +common->quit_label = common->forced_quit_label = LABEL(); +if (common->quit != NULL) + set_jumps(common->quit, common->quit_label); +if (common->forced_quit != NULL) + set_jumps(common->forced_quit, common->forced_quit_label); +if (minlength_check_failed != NULL) + SET_LABEL(minlength_check_failed, common->forced_quit_label); +sljit_emit_return(compiler, SLJIT_MOV, SLJIT_RETURN_REG, 0); + +if (mode != JIT_COMPILE) + { + common->partialmatchlabel = LABEL(); + set_jumps(common->partialmatch, common->partialmatchlabel); + return_with_partial_match(common, common->quit_label); + } + +if (common->might_be_empty) + empty_match_backtrack_label = LABEL(); +compile_backtrackingpath(common, rootbacktrack.top); +if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + { + sljit_free_compiler(compiler); + SLJIT_FREE(common->optimized_cbracket, compiler->allocator_data); + SLJIT_FREE(common->private_data_ptrs, compiler->allocator_data); + free_read_only_data(common->read_only_data_head, compiler->allocator_data); + return; + } + +SLJIT_ASSERT(rootbacktrack.prev == NULL); +reset_match_label = LABEL(); + +if (mode == JIT_PARTIAL_SOFT_COMPILE) + { + /* Update hit_start only in the first time. */ + jump = CMP(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, 0); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->start_used_ptr, SLJIT_IMM, -1); + OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), common->hit_start, TMP1, 0); + JUMPHERE(jump); + } + +/* Check we have remaining characters. */ +if ((re->options & PCRE_ANCHORED) == 0 && (re->options & PCRE_FIRSTLINE) != 0) + { + SLJIT_ASSERT(common->match_end_ptr != 0); + OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), common->match_end_ptr); + } + +OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_SP), + (common->fast_forward_bc_ptr != NULL) ? (PRIVATE_DATA(common->fast_forward_bc_ptr + 1)) : common->start_ptr); + +if ((re->options & PCRE_ANCHORED) == 0) + { + if (common->ff_newline_shortcut != NULL) + { + if ((re->options & PCRE_FIRSTLINE) == 0) + CMPTO(SLJIT_LESS, STR_PTR, 0, STR_END, 0, common->ff_newline_shortcut); + /* There cannot be more newlines here. */ + } + else + CMPTO(SLJIT_LESS, STR_PTR, 0, ((re->options & PCRE_FIRSTLINE) == 0) ? STR_END : TMP1, 0, mainloop_label); + } + +/* No more remaining characters. */ +if (reqbyte_notfound != NULL) + JUMPHERE(reqbyte_notfound); + +if (mode == JIT_PARTIAL_SOFT_COMPILE) + CMPTO(SLJIT_NOT_EQUAL, SLJIT_MEM1(SLJIT_SP), common->hit_start, SLJIT_IMM, -1, common->partialmatchlabel); + +OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_NOMATCH); +JUMPTO(SLJIT_JUMP, common->quit_label); + +flush_stubs(common); + +if (common->might_be_empty) + { + JUMPHERE(empty_match); + OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0); + OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, notempty)); + CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, SLJIT_IMM, 0, empty_match_backtrack_label); + OP1(SLJIT_MOV_U8, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, notempty_atstart)); + CMPTO(SLJIT_EQUAL, TMP2, 0, SLJIT_IMM, 0, empty_match_found_label); + OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, str)); + CMPTO(SLJIT_NOT_EQUAL, TMP2, 0, STR_PTR, 0, empty_match_found_label); + JUMPTO(SLJIT_JUMP, empty_match_backtrack_label); + } + +common->fast_forward_bc_ptr = NULL; +common->fast_fail_start_ptr = 0; +common->fast_fail_end_ptr = 0; +common->currententry = common->entries; +common->local_exit = TRUE; +quit_label = common->quit_label; +while (common->currententry != NULL) + { + /* Might add new entries. */ + compile_recurse(common); + if (SLJIT_UNLIKELY(sljit_get_compiler_error(compiler))) + { + sljit_free_compiler(compiler); + SLJIT_FREE(common->optimized_cbracket, compiler->allocator_data); + SLJIT_FREE(common->private_data_ptrs, compiler->allocator_data); + free_read_only_data(common->read_only_data_head, compiler->allocator_data); + return; + } + flush_stubs(common); + common->currententry = common->currententry->next; + } +common->local_exit = FALSE; +common->quit_label = quit_label; + +/* Allocating stack, returns with PCRE_ERROR_JIT_STACKLIMIT if fails. */ +/* This is a (really) rare case. */ +set_jumps(common->stackalloc, LABEL()); +/* RETURN_ADDR is not a saved register. */ +sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_SP), LOCALS0); + +SLJIT_ASSERT(TMP1 == SLJIT_R0 && STACK_TOP == SLJIT_R1); + +OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), LOCALS1, STACK_TOP, 0); +OP1(SLJIT_MOV, SLJIT_R0, 0, ARGUMENTS, 0); +OP2(SLJIT_SUB, SLJIT_R1, 0, STACK_LIMIT, 0, SLJIT_IMM, STACK_GROWTH_RATE); +OP1(SLJIT_MOV, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_R0), SLJIT_OFFSETOF(jit_arguments, stack)); +OP1(SLJIT_MOV, STACK_LIMIT, 0, TMP2, 0); + +sljit_emit_icall(compiler, SLJIT_CALL, SLJIT_RET(SW) | SLJIT_ARG1(SW) | SLJIT_ARG2(SW), SLJIT_IMM, SLJIT_FUNC_OFFSET(sljit_stack_resize)); +jump = CMP(SLJIT_EQUAL, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0); +OP1(SLJIT_MOV, TMP2, 0, STACK_LIMIT, 0); +OP1(SLJIT_MOV, STACK_LIMIT, 0, SLJIT_RETURN_REG, 0); +OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_SP), LOCALS0); +OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(SLJIT_SP), LOCALS1); +sljit_emit_fast_return(compiler, TMP1, 0); + +/* Allocation failed. */ +JUMPHERE(jump); +/* We break the return address cache here, but this is a really rare case. */ +OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_JIT_STACKLIMIT); +JUMPTO(SLJIT_JUMP, common->quit_label); + +/* Call limit reached. */ +set_jumps(common->calllimit, LABEL()); +OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_MATCHLIMIT); +JUMPTO(SLJIT_JUMP, common->quit_label); + +if (common->revertframes != NULL) + { + set_jumps(common->revertframes, LABEL()); + do_revertframes(common); + } +if (common->wordboundary != NULL) + { + set_jumps(common->wordboundary, LABEL()); + check_wordboundary(common); + } +if (common->anynewline != NULL) + { + set_jumps(common->anynewline, LABEL()); + check_anynewline(common); + } +if (common->hspace != NULL) + { + set_jumps(common->hspace, LABEL()); + check_hspace(common); + } +if (common->vspace != NULL) + { + set_jumps(common->vspace, LABEL()); + check_vspace(common); + } +if (common->casefulcmp != NULL) + { + set_jumps(common->casefulcmp, LABEL()); + do_casefulcmp(common); + } +if (common->caselesscmp != NULL) + { + set_jumps(common->caselesscmp, LABEL()); + do_caselesscmp(common); + } +if (common->reset_match != NULL) + { + set_jumps(common->reset_match, LABEL()); + do_reset_match(common, (re->top_bracket + 1) * 2); + CMPTO(SLJIT_GREATER, STR_PTR, 0, TMP1, 0, continue_match_label); + OP1(SLJIT_MOV, STR_PTR, 0, TMP1, 0); + JUMPTO(SLJIT_JUMP, reset_match_label); + } +#ifdef SUPPORT_UTF +#ifdef COMPILE_PCRE8 +if (common->utfreadchar != NULL) + { + set_jumps(common->utfreadchar, LABEL()); + do_utfreadchar(common); + } +if (common->utfreadchar16 != NULL) + { + set_jumps(common->utfreadchar16, LABEL()); + do_utfreadchar16(common); + } +if (common->utfreadtype8 != NULL) + { + set_jumps(common->utfreadtype8, LABEL()); + do_utfreadtype8(common); + } +#endif /* COMPILE_PCRE8 */ +#endif /* SUPPORT_UTF */ +#ifdef SUPPORT_UCP +if (common->getucd != NULL) + { + set_jumps(common->getucd, LABEL()); + do_getucd(common); + } +#endif + +SLJIT_FREE(common->optimized_cbracket, compiler->allocator_data); +SLJIT_FREE(common->private_data_ptrs, compiler->allocator_data); + +executable_func = sljit_generate_code(compiler); +executable_size = sljit_get_generated_code_size(compiler); +label_addr = common->label_addrs; +while (label_addr != NULL) + { + *label_addr->update_addr = sljit_get_label_addr(label_addr->label); + label_addr = label_addr->next; + } +sljit_free_compiler(compiler); +if (executable_func == NULL) + { + free_read_only_data(common->read_only_data_head, compiler->allocator_data); + return; + } + +/* Reuse the function descriptor if possible. */ +if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 && extra->executable_jit != NULL) + functions = (executable_functions *)extra->executable_jit; +else + { + /* Note: If your memory-checker has flagged the allocation below as a + * memory leak, it is probably because you either forgot to call + * pcre_free_study() (or pcre16_free_study()) on the pcre_extra (or + * pcre16_extra) object, or you called said function after having + * cleared the PCRE_EXTRA_EXECUTABLE_JIT bit from the "flags" field + * of the object. (The function will only free the JIT data if the + * bit remains set, as the bit indicates that the pointer to the data + * is valid.) + */ + functions = SLJIT_MALLOC(sizeof(executable_functions), compiler->allocator_data); + if (functions == NULL) + { + /* This case is highly unlikely since we just recently + freed a lot of memory. Not impossible though. */ + sljit_free_code(executable_func); + free_read_only_data(common->read_only_data_head, compiler->allocator_data); + return; + } + memset(functions, 0, sizeof(executable_functions)); + functions->top_bracket = (re->top_bracket + 1) * 2; + functions->limit_match = (re->flags & PCRE_MLSET) != 0 ? re->limit_match : 0; + extra->executable_jit = functions; + extra->flags |= PCRE_EXTRA_EXECUTABLE_JIT; + } + +functions->executable_funcs[mode] = executable_func; +functions->read_only_data_heads[mode] = common->read_only_data_head; +functions->executable_sizes[mode] = executable_size; +} + +static SLJIT_NOINLINE int jit_machine_stack_exec(jit_arguments *arguments, void *executable_func) +{ +union { + void *executable_func; + jit_function call_executable_func; +} convert_executable_func; +sljit_u8 local_space[MACHINE_STACK_SIZE]; +struct sljit_stack local_stack; + +local_stack.min_start = local_space; +local_stack.start = local_space; +local_stack.end = local_space + MACHINE_STACK_SIZE; +local_stack.top = local_space + MACHINE_STACK_SIZE; +arguments->stack = &local_stack; +convert_executable_func.executable_func = executable_func; +return convert_executable_func.call_executable_func(arguments); +} + +int +PRIV(jit_exec)(const PUBL(extra) *extra_data, const pcre_uchar *subject, + int length, int start_offset, int options, int *offsets, int offset_count) +{ +executable_functions *functions = (executable_functions *)extra_data->executable_jit; +union { + void *executable_func; + jit_function call_executable_func; +} convert_executable_func; +jit_arguments arguments; +int max_offset_count; +int retval; +int mode = JIT_COMPILE; + +if ((options & PCRE_PARTIAL_HARD) != 0) + mode = JIT_PARTIAL_HARD_COMPILE; +else if ((options & PCRE_PARTIAL_SOFT) != 0) + mode = JIT_PARTIAL_SOFT_COMPILE; + +if (functions->executable_funcs[mode] == NULL) + return PCRE_ERROR_JIT_BADOPTION; + +/* Sanity checks should be handled by pcre_exec. */ +arguments.str = subject + start_offset; +arguments.begin = subject; +arguments.end = subject + length; +arguments.mark_ptr = NULL; +/* JIT decreases this value less frequently than the interpreter. */ +arguments.limit_match = ((extra_data->flags & PCRE_EXTRA_MATCH_LIMIT) == 0) ? MATCH_LIMIT : (sljit_u32)(extra_data->match_limit); +if (functions->limit_match != 0 && functions->limit_match < arguments.limit_match) + arguments.limit_match = functions->limit_match; +arguments.notbol = (options & PCRE_NOTBOL) != 0; +arguments.noteol = (options & PCRE_NOTEOL) != 0; +arguments.notempty = (options & PCRE_NOTEMPTY) != 0; +arguments.notempty_atstart = (options & PCRE_NOTEMPTY_ATSTART) != 0; +arguments.offsets = offsets; +arguments.callout_data = (extra_data->flags & PCRE_EXTRA_CALLOUT_DATA) != 0 ? extra_data->callout_data : NULL; +arguments.real_offset_count = offset_count; + +/* pcre_exec() rounds offset_count to a multiple of 3, and then uses only 2/3 of +the output vector for storing captured strings, with the remainder used as +workspace. We don't need the workspace here. For compatibility, we limit the +number of captured strings in the same way as pcre_exec(), so that the user +gets the same result with and without JIT. */ + +if (offset_count != 2) + offset_count = ((offset_count - (offset_count % 3)) * 2) / 3; +max_offset_count = functions->top_bracket; +if (offset_count > max_offset_count) + offset_count = max_offset_count; +arguments.offset_count = offset_count; + +if (functions->callback) + arguments.stack = (struct sljit_stack *)functions->callback(functions->userdata); +else + arguments.stack = (struct sljit_stack *)functions->userdata; + +if (arguments.stack == NULL) + retval = jit_machine_stack_exec(&arguments, functions->executable_funcs[mode]); +else + { + convert_executable_func.executable_func = functions->executable_funcs[mode]; + retval = convert_executable_func.call_executable_func(&arguments); + } + +if (retval * 2 > offset_count) + retval = 0; +if ((extra_data->flags & PCRE_EXTRA_MARK) != 0) + *(extra_data->mark) = arguments.mark_ptr; + +return retval; +} + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_jit_exec(const pcre *argument_re, const pcre_extra *extra_data, + PCRE_SPTR subject, int length, int start_offset, int options, + int *offsets, int offset_count, pcre_jit_stack *stack) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre16_jit_exec(const pcre16 *argument_re, const pcre16_extra *extra_data, + PCRE_SPTR16 subject, int length, int start_offset, int options, + int *offsets, int offset_count, pcre16_jit_stack *stack) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre32_jit_exec(const pcre32 *argument_re, const pcre32_extra *extra_data, + PCRE_SPTR32 subject, int length, int start_offset, int options, + int *offsets, int offset_count, pcre32_jit_stack *stack) +#endif +{ +pcre_uchar *subject_ptr = (pcre_uchar *)subject; +executable_functions *functions = (executable_functions *)extra_data->executable_jit; +union { + void *executable_func; + jit_function call_executable_func; +} convert_executable_func; +jit_arguments arguments; +int max_offset_count; +int retval; +int mode = JIT_COMPILE; + +SLJIT_UNUSED_ARG(argument_re); + +/* Plausibility checks */ +if ((options & ~PUBLIC_JIT_EXEC_OPTIONS) != 0) return PCRE_ERROR_JIT_BADOPTION; + +if ((options & PCRE_PARTIAL_HARD) != 0) + mode = JIT_PARTIAL_HARD_COMPILE; +else if ((options & PCRE_PARTIAL_SOFT) != 0) + mode = JIT_PARTIAL_SOFT_COMPILE; + +if (functions == NULL || functions->executable_funcs[mode] == NULL) + return PCRE_ERROR_JIT_BADOPTION; + +/* Sanity checks should be handled by pcre_exec. */ +arguments.stack = (struct sljit_stack *)stack; +arguments.str = subject_ptr + start_offset; +arguments.begin = subject_ptr; +arguments.end = subject_ptr + length; +arguments.mark_ptr = NULL; +/* JIT decreases this value less frequently than the interpreter. */ +arguments.limit_match = ((extra_data->flags & PCRE_EXTRA_MATCH_LIMIT) == 0) ? MATCH_LIMIT : (sljit_u32)(extra_data->match_limit); +if (functions->limit_match != 0 && functions->limit_match < arguments.limit_match) + arguments.limit_match = functions->limit_match; +arguments.notbol = (options & PCRE_NOTBOL) != 0; +arguments.noteol = (options & PCRE_NOTEOL) != 0; +arguments.notempty = (options & PCRE_NOTEMPTY) != 0; +arguments.notempty_atstart = (options & PCRE_NOTEMPTY_ATSTART) != 0; +arguments.offsets = offsets; +arguments.callout_data = (extra_data->flags & PCRE_EXTRA_CALLOUT_DATA) != 0 ? extra_data->callout_data : NULL; +arguments.real_offset_count = offset_count; + +/* pcre_exec() rounds offset_count to a multiple of 3, and then uses only 2/3 of +the output vector for storing captured strings, with the remainder used as +workspace. We don't need the workspace here. For compatibility, we limit the +number of captured strings in the same way as pcre_exec(), so that the user +gets the same result with and without JIT. */ + +if (offset_count != 2) + offset_count = ((offset_count - (offset_count % 3)) * 2) / 3; +max_offset_count = functions->top_bracket; +if (offset_count > max_offset_count) + offset_count = max_offset_count; +arguments.offset_count = offset_count; + +convert_executable_func.executable_func = functions->executable_funcs[mode]; +retval = convert_executable_func.call_executable_func(&arguments); + +if (retval * 2 > offset_count) + retval = 0; +if ((extra_data->flags & PCRE_EXTRA_MARK) != 0) + *(extra_data->mark) = arguments.mark_ptr; + +return retval; +} + +void +PRIV(jit_free)(void *executable_funcs) +{ +int i; +executable_functions *functions = (executable_functions *)executable_funcs; +for (i = 0; i < JIT_NUMBER_OF_COMPILE_MODES; i++) + { + if (functions->executable_funcs[i] != NULL) + sljit_free_code(functions->executable_funcs[i]); + free_read_only_data(functions->read_only_data_heads[i], NULL); + } +SLJIT_FREE(functions, compiler->allocator_data); +} + +int +PRIV(jit_get_size)(void *executable_funcs) +{ +int i; +sljit_uw size = 0; +sljit_uw *executable_sizes = ((executable_functions *)executable_funcs)->executable_sizes; +for (i = 0; i < JIT_NUMBER_OF_COMPILE_MODES; i++) + size += executable_sizes[i]; +return (int)size; +} + +const char* +PRIV(jit_get_target)(void) +{ +return sljit_get_platform_name(); +} + +#if defined COMPILE_PCRE8 +PCRE_EXP_DECL pcre_jit_stack * +pcre_jit_stack_alloc(int startsize, int maxsize) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DECL pcre16_jit_stack * +pcre16_jit_stack_alloc(int startsize, int maxsize) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DECL pcre32_jit_stack * +pcre32_jit_stack_alloc(int startsize, int maxsize) +#endif +{ +if (startsize < 1 || maxsize < 1) + return NULL; +if (startsize > maxsize) + startsize = maxsize; +startsize = (startsize + STACK_GROWTH_RATE - 1) & ~(STACK_GROWTH_RATE - 1); +maxsize = (maxsize + STACK_GROWTH_RATE - 1) & ~(STACK_GROWTH_RATE - 1); +return (PUBL(jit_stack)*)sljit_allocate_stack(startsize, maxsize, NULL); +} + +#if defined COMPILE_PCRE8 +PCRE_EXP_DECL void +pcre_jit_stack_free(pcre_jit_stack *stack) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DECL void +pcre16_jit_stack_free(pcre16_jit_stack *stack) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DECL void +pcre32_jit_stack_free(pcre32_jit_stack *stack) +#endif +{ +sljit_free_stack((struct sljit_stack *)stack, NULL); +} + +#if defined COMPILE_PCRE8 +PCRE_EXP_DECL void +pcre_assign_jit_stack(pcre_extra *extra, pcre_jit_callback callback, void *userdata) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DECL void +pcre16_assign_jit_stack(pcre16_extra *extra, pcre16_jit_callback callback, void *userdata) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DECL void +pcre32_assign_jit_stack(pcre32_extra *extra, pcre32_jit_callback callback, void *userdata) +#endif +{ +executable_functions *functions; +if (extra != NULL && + (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 && + extra->executable_jit != NULL) + { + functions = (executable_functions *)extra->executable_jit; + functions->callback = callback; + functions->userdata = userdata; + } +} + +#if defined COMPILE_PCRE8 +PCRE_EXP_DECL void +pcre_jit_free_unused_memory(void) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DECL void +pcre16_jit_free_unused_memory(void) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DECL void +pcre32_jit_free_unused_memory(void) +#endif +{ +sljit_free_unused_memory_exec(); +} + +#else /* SUPPORT_JIT */ + +/* These are dummy functions to avoid linking errors when JIT support is not +being compiled. */ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DECL pcre_jit_stack * +pcre_jit_stack_alloc(int startsize, int maxsize) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DECL pcre16_jit_stack * +pcre16_jit_stack_alloc(int startsize, int maxsize) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DECL pcre32_jit_stack * +pcre32_jit_stack_alloc(int startsize, int maxsize) +#endif +{ +(void)startsize; +(void)maxsize; +return NULL; +} + +#if defined COMPILE_PCRE8 +PCRE_EXP_DECL void +pcre_jit_stack_free(pcre_jit_stack *stack) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DECL void +pcre16_jit_stack_free(pcre16_jit_stack *stack) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DECL void +pcre32_jit_stack_free(pcre32_jit_stack *stack) +#endif +{ +(void)stack; +} + +#if defined COMPILE_PCRE8 +PCRE_EXP_DECL void +pcre_assign_jit_stack(pcre_extra *extra, pcre_jit_callback callback, void *userdata) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DECL void +pcre16_assign_jit_stack(pcre16_extra *extra, pcre16_jit_callback callback, void *userdata) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DECL void +pcre32_assign_jit_stack(pcre32_extra *extra, pcre32_jit_callback callback, void *userdata) +#endif +{ +(void)extra; +(void)callback; +(void)userdata; +} + +#if defined COMPILE_PCRE8 +PCRE_EXP_DECL void +pcre_jit_free_unused_memory(void) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DECL void +pcre16_jit_free_unused_memory(void) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DECL void +pcre32_jit_free_unused_memory(void) +#endif +{ +} + +#endif + +/* End of pcre_jit_compile.c */ diff --git a/deps/pcre/pcre_maketables.c b/deps/pcre/pcre_maketables.c new file mode 100644 index 00000000000..a44a6eaa905 --- /dev/null +++ b/deps/pcre/pcre_maketables.c @@ -0,0 +1,156 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2012 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains the external function pcre_maketables(), which builds +character tables for PCRE in the current locale. The file is compiled on its +own as part of the PCRE library. However, it is also included in the +compilation of dftables.c, in which case the macro DFTABLES is defined. */ + + +#ifndef DFTABLES +# ifdef HAVE_CONFIG_H +# include "config.h" +# endif +# include "pcre_internal.h" +#endif + + +/************************************************* +* Create PCRE character tables * +*************************************************/ + +/* This function builds a set of character tables for use by PCRE and returns +a pointer to them. They are build using the ctype functions, and consequently +their contents will depend upon the current locale setting. When compiled as +part of the library, the store is obtained via PUBL(malloc)(), but when +compiled inside dftables, use malloc(). + +Arguments: none +Returns: pointer to the contiguous block of data +*/ + +#if defined COMPILE_PCRE8 +const unsigned char * +pcre_maketables(void) +#elif defined COMPILE_PCRE16 +const unsigned char * +pcre16_maketables(void) +#elif defined COMPILE_PCRE32 +const unsigned char * +pcre32_maketables(void) +#endif +{ +unsigned char *yield, *p; +int i; + +#ifndef DFTABLES +yield = (unsigned char*)(PUBL(malloc))(tables_length); +#else +yield = (unsigned char*)malloc(tables_length); +#endif + +if (yield == NULL) return NULL; +p = yield; + +/* First comes the lower casing table */ + +for (i = 0; i < 256; i++) *p++ = tolower(i); + +/* Next the case-flipping table */ + +for (i = 0; i < 256; i++) *p++ = islower(i)? toupper(i) : tolower(i); + +/* Then the character class tables. Don't try to be clever and save effort on +exclusive ones - in some locales things may be different. + +Note that the table for "space" includes everything "isspace" gives, including +VT in the default locale. This makes it work for the POSIX class [:space:]. +From release 8.34 is is also correct for Perl space, because Perl added VT at +release 5.18. + +Note also that it is possible for a character to be alnum or alpha without +being lower or upper, such as "male and female ordinals" (\xAA and \xBA) in the +fr_FR locale (at least under Debian Linux's locales as of 12/2005). So we must +test for alnum specially. */ + +memset(p, 0, cbit_length); +for (i = 0; i < 256; i++) + { + if (isdigit(i)) p[cbit_digit + i/8] |= 1 << (i&7); + if (isupper(i)) p[cbit_upper + i/8] |= 1 << (i&7); + if (islower(i)) p[cbit_lower + i/8] |= 1 << (i&7); + if (isalnum(i)) p[cbit_word + i/8] |= 1 << (i&7); + if (i == '_') p[cbit_word + i/8] |= 1 << (i&7); + if (isspace(i)) p[cbit_space + i/8] |= 1 << (i&7); + if (isxdigit(i))p[cbit_xdigit + i/8] |= 1 << (i&7); + if (isgraph(i)) p[cbit_graph + i/8] |= 1 << (i&7); + if (isprint(i)) p[cbit_print + i/8] |= 1 << (i&7); + if (ispunct(i)) p[cbit_punct + i/8] |= 1 << (i&7); + if (iscntrl(i)) p[cbit_cntrl + i/8] |= 1 << (i&7); + } +p += cbit_length; + +/* Finally, the character type table. In this, we used to exclude VT from the +white space chars, because Perl didn't recognize it as such for \s and for +comments within regexes. However, Perl changed at release 5.18, so PCRE changed +at release 8.34. */ + +for (i = 0; i < 256; i++) + { + int x = 0; + if (isspace(i)) x += ctype_space; + if (isalpha(i)) x += ctype_letter; + if (isdigit(i)) x += ctype_digit; + if (isxdigit(i)) x += ctype_xdigit; + if (isalnum(i) || i == '_') x += ctype_word; + + /* Note: strchr includes the terminating zero in the characters it considers. + In this instance, that is ok because we want binary zero to be flagged as a + meta-character, which in this sense is any character that terminates a run + of data characters. */ + + if (strchr("\\*+?{^.$|()[", i) != 0) x += ctype_meta; + *p++ = x; + } + +return yield; +} + +/* End of pcre_maketables.c */ diff --git a/deps/pcre/pcre_newline.c b/deps/pcre/pcre_newline.c new file mode 100644 index 00000000000..b8f5a4de19c --- /dev/null +++ b/deps/pcre/pcre_newline.c @@ -0,0 +1,210 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2012 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains internal functions for testing newlines when more than +one kind of newline is to be recognized. When a newline is found, its length is +returned. In principle, we could implement several newline "types", each +referring to a different set of newline characters. At present, PCRE supports +only NLTYPE_FIXED, which gets handled without these functions, NLTYPE_ANYCRLF, +and NLTYPE_ANY. The full list of Unicode newline characters is taken from +http://unicode.org/unicode/reports/tr18/. */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pcre_internal.h" + + + +/************************************************* +* Check for newline at given position * +*************************************************/ + +/* It is guaranteed that the initial value of ptr is less than the end of the +string that is being processed. + +Arguments: + ptr pointer to possible newline + type the newline type + endptr pointer to the end of the string + lenptr where to return the length + utf TRUE if in utf mode + +Returns: TRUE or FALSE +*/ + +BOOL +PRIV(is_newline)(PCRE_PUCHAR ptr, int type, PCRE_PUCHAR endptr, int *lenptr, + BOOL utf) +{ +pcre_uint32 c; +(void)utf; +#ifdef SUPPORT_UTF +if (utf) + { + GETCHAR(c, ptr); + } +else +#endif /* SUPPORT_UTF */ + c = *ptr; + +/* Note that this function is called only for ANY or ANYCRLF. */ + +if (type == NLTYPE_ANYCRLF) switch(c) + { + case CHAR_LF: *lenptr = 1; return TRUE; + case CHAR_CR: *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1; + return TRUE; + default: return FALSE; + } + +/* NLTYPE_ANY */ + +else switch(c) + { +#ifdef EBCDIC + case CHAR_NEL: +#endif + case CHAR_LF: + case CHAR_VT: + case CHAR_FF: *lenptr = 1; return TRUE; + + case CHAR_CR: + *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1; + return TRUE; + +#ifndef EBCDIC +#ifdef COMPILE_PCRE8 + case CHAR_NEL: *lenptr = utf? 2 : 1; return TRUE; + case 0x2028: /* LS */ + case 0x2029: *lenptr = 3; return TRUE; /* PS */ +#else /* COMPILE_PCRE16 || COMPILE_PCRE32 */ + case CHAR_NEL: + case 0x2028: /* LS */ + case 0x2029: *lenptr = 1; return TRUE; /* PS */ +#endif /* COMPILE_PCRE8 */ +#endif /* Not EBCDIC */ + + default: return FALSE; + } +} + + + +/************************************************* +* Check for newline at previous position * +*************************************************/ + +/* It is guaranteed that the initial value of ptr is greater than the start of +the string that is being processed. + +Arguments: + ptr pointer to possible newline + type the newline type + startptr pointer to the start of the string + lenptr where to return the length + utf TRUE if in utf mode + +Returns: TRUE or FALSE +*/ + +BOOL +PRIV(was_newline)(PCRE_PUCHAR ptr, int type, PCRE_PUCHAR startptr, int *lenptr, + BOOL utf) +{ +pcre_uint32 c; +(void)utf; +ptr--; +#ifdef SUPPORT_UTF +if (utf) + { + BACKCHAR(ptr); + GETCHAR(c, ptr); + } +else +#endif /* SUPPORT_UTF */ + c = *ptr; + +/* Note that this function is called only for ANY or ANYCRLF. */ + +if (type == NLTYPE_ANYCRLF) switch(c) + { + case CHAR_LF: + *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1; + return TRUE; + + case CHAR_CR: *lenptr = 1; return TRUE; + default: return FALSE; + } + +/* NLTYPE_ANY */ + +else switch(c) + { + case CHAR_LF: + *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1; + return TRUE; + +#ifdef EBCDIC + case CHAR_NEL: +#endif + case CHAR_VT: + case CHAR_FF: + case CHAR_CR: *lenptr = 1; return TRUE; + +#ifndef EBCDIC +#ifdef COMPILE_PCRE8 + case CHAR_NEL: *lenptr = utf? 2 : 1; return TRUE; + case 0x2028: /* LS */ + case 0x2029: *lenptr = 3; return TRUE; /* PS */ +#else /* COMPILE_PCRE16 || COMPILE_PCRE32 */ + case CHAR_NEL: + case 0x2028: /* LS */ + case 0x2029: *lenptr = 1; return TRUE; /* PS */ +#endif /* COMPILE_PCRE8 */ +#endif /* NotEBCDIC */ + + default: return FALSE; + } +} + +/* End of pcre_newline.c */ diff --git a/deps/pcre/pcre_ord2utf8.c b/deps/pcre/pcre_ord2utf8.c new file mode 100644 index 00000000000..95f1beb963e --- /dev/null +++ b/deps/pcre/pcre_ord2utf8.c @@ -0,0 +1,94 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2012 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This file contains a private PCRE function that converts an ordinal +character value into a UTF8 string. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#define COMPILE_PCRE8 + +#include "pcre_internal.h" + +/************************************************* +* Convert character value to UTF-8 * +*************************************************/ + +/* This function takes an integer value in the range 0 - 0x10ffff +and encodes it as a UTF-8 character in 1 to 4 pcre_uchars. + +Arguments: + cvalue the character value + buffer pointer to buffer for result - at least 6 pcre_uchars long + +Returns: number of characters placed in the buffer +*/ + +unsigned +int +PRIV(ord2utf)(pcre_uint32 cvalue, pcre_uchar *buffer) +{ +#ifdef SUPPORT_UTF + +register int i, j; + +for (i = 0; i < PRIV(utf8_table1_size); i++) + if ((int)cvalue <= PRIV(utf8_table1)[i]) break; +buffer += i; +for (j = i; j > 0; j--) + { + *buffer-- = 0x80 | (cvalue & 0x3f); + cvalue >>= 6; + } +*buffer = PRIV(utf8_table2)[i] | cvalue; +return i + 1; + +#else + +(void)(cvalue); /* Keep compiler happy; this function won't ever be */ +(void)(buffer); /* called when SUPPORT_UTF is not defined. */ +return 0; + +#endif +} + +/* End of pcre_ord2utf8.c */ diff --git a/deps/pcre/pcre_printint.c b/deps/pcre/pcre_printint.c new file mode 100644 index 00000000000..60dcb55efbf --- /dev/null +++ b/deps/pcre/pcre_printint.c @@ -0,0 +1,834 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2012 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains a PCRE private debugging function for printing out the +internal form of a compiled regular expression, along with some supporting +local functions. This source file is used in two places: + +(1) It is #included by pcre_compile.c when it is compiled in debugging mode +(PCRE_DEBUG defined in pcre_internal.h). It is not included in production +compiles. In this case PCRE_INCLUDED is defined. + +(2) It is also compiled separately and linked with pcretest.c, which can be +asked to print out a compiled regex for debugging purposes. */ + +#ifndef PCRE_INCLUDED + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +/* For pcretest program. */ +#define PRIV(name) name + +/* We have to include pcre_internal.h because we need the internal info for +displaying the results of pcre_study() and we also need to know about the +internal macros, structures, and other internal data values; pcretest has +"inside information" compared to a program that strictly follows the PCRE API. + +Although pcre_internal.h does itself include pcre.h, we explicitly include it +here before pcre_internal.h so that the PCRE_EXP_xxx macros get set +appropriately for an application, not for building PCRE. */ + +#include "pcre.h" +#include "pcre_internal.h" + +/* These are the funtions that are contained within. It doesn't seem worth +having a separate .h file just for this. */ + +#endif /* PCRE_INCLUDED */ + +#ifdef PCRE_INCLUDED +static /* Keep the following function as private. */ +#endif + +#if defined COMPILE_PCRE8 +void pcre_printint(pcre *external_re, FILE *f, BOOL print_lengths); +#elif defined COMPILE_PCRE16 +void pcre16_printint(pcre *external_re, FILE *f, BOOL print_lengths); +#elif defined COMPILE_PCRE32 +void pcre32_printint(pcre *external_re, FILE *f, BOOL print_lengths); +#endif + +/* Macro that decides whether a character should be output as a literal or in +hexadecimal. We don't use isprint() because that can vary from system to system +(even without the use of locales) and we want the output always to be the same, +for testing purposes. */ + +#ifdef EBCDIC +#define PRINTABLE(c) ((c) >= 64 && (c) < 255) +#else +#define PRINTABLE(c) ((c) >= 32 && (c) < 127) +#endif + +/* The table of operator names. */ + +static const char *priv_OP_names[] = { OP_NAME_LIST }; + +/* This table of operator lengths is not actually used by the working code, +but its size is needed for a check that ensures it is the correct size for the +number of opcodes (thus catching update omissions). */ + +static const pcre_uint8 priv_OP_lengths[] = { OP_LENGTHS }; + + + +/************************************************* +* Print single- or multi-byte character * +*************************************************/ + +static unsigned int +print_char(FILE *f, pcre_uchar *ptr, BOOL utf) +{ +pcre_uint32 c = *ptr; + +#ifndef SUPPORT_UTF + +(void)utf; /* Avoid compiler warning */ +if (PRINTABLE(c)) fprintf(f, "%c", (char)c); +else if (c <= 0x80) fprintf(f, "\\x%02x", c); +else fprintf(f, "\\x{%x}", c); +return 0; + +#else + +#if defined COMPILE_PCRE8 + +if (!utf || (c & 0xc0) != 0xc0) + { + if (PRINTABLE(c)) fprintf(f, "%c", (char)c); + else if (c < 0x80) fprintf(f, "\\x%02x", c); + else fprintf(f, "\\x{%02x}", c); + return 0; + } +else + { + int i; + int a = PRIV(utf8_table4)[c & 0x3f]; /* Number of additional bytes */ + int s = 6*a; + c = (c & PRIV(utf8_table3)[a]) << s; + for (i = 1; i <= a; i++) + { + /* This is a check for malformed UTF-8; it should only occur if the sanity + check has been turned off. Rather than swallow random bytes, just stop if + we hit a bad one. Print it with \X instead of \x as an indication. */ + + if ((ptr[i] & 0xc0) != 0x80) + { + fprintf(f, "\\X{%x}", c); + return i - 1; + } + + /* The byte is OK */ + + s -= 6; + c |= (ptr[i] & 0x3f) << s; + } + fprintf(f, "\\x{%x}", c); + return a; + } + +#elif defined COMPILE_PCRE16 + +if (!utf || (c & 0xfc00) != 0xd800) + { + if (PRINTABLE(c)) fprintf(f, "%c", (char)c); + else if (c <= 0x80) fprintf(f, "\\x%02x", c); + else fprintf(f, "\\x{%02x}", c); + return 0; + } +else + { + /* This is a check for malformed UTF-16; it should only occur if the sanity + check has been turned off. Rather than swallow a low surrogate, just stop if + we hit a bad one. Print it with \X instead of \x as an indication. */ + + if ((ptr[1] & 0xfc00) != 0xdc00) + { + fprintf(f, "\\X{%x}", c); + return 0; + } + + c = (((c & 0x3ff) << 10) | (ptr[1] & 0x3ff)) + 0x10000; + fprintf(f, "\\x{%x}", c); + return 1; + } + +#elif defined COMPILE_PCRE32 + +if (!utf || (c & 0xfffff800u) != 0xd800u) + { + if (PRINTABLE(c)) fprintf(f, "%c", (char)c); + else if (c <= 0x80) fprintf(f, "\\x%02x", c); + else fprintf(f, "\\x{%x}", c); + return 0; + } +else + { + /* This is a check for malformed UTF-32; it should only occur if the sanity + check has been turned off. Rather than swallow a surrogate, just stop if + we hit one. Print it with \X instead of \x as an indication. */ + fprintf(f, "\\X{%x}", c); + return 0; + } + +#endif /* COMPILE_PCRE[8|16|32] */ + +#endif /* SUPPORT_UTF */ +} + +/************************************************* +* Print uchar string (regardless of utf) * +*************************************************/ + +static void +print_puchar(FILE *f, PCRE_PUCHAR ptr) +{ +while (*ptr != '\0') + { + register pcre_uint32 c = *ptr++; + if (PRINTABLE(c)) fprintf(f, "%c", c); else fprintf(f, "\\x{%x}", c); + } +} + +/************************************************* +* Find Unicode property name * +*************************************************/ + +static const char * +get_ucpname(unsigned int ptype, unsigned int pvalue) +{ +#ifdef SUPPORT_UCP +int i; +for (i = PRIV(utt_size) - 1; i >= 0; i--) + { + if (ptype == PRIV(utt)[i].type && pvalue == PRIV(utt)[i].value) break; + } +return (i >= 0)? PRIV(utt_names) + PRIV(utt)[i].name_offset : "??"; +#else +/* It gets harder and harder to shut off unwanted compiler warnings. */ +ptype = ptype * pvalue; +return (ptype == pvalue)? "??" : "??"; +#endif +} + + +/************************************************* +* Print Unicode property value * +*************************************************/ + +/* "Normal" properties can be printed from tables. The PT_CLIST property is a +pseudo-property that contains a pointer to a list of case-equivalent +characters. This is used only when UCP support is available and UTF mode is +selected. It should never occur otherwise, but just in case it does, have +something ready to print. */ + +static void +print_prop(FILE *f, pcre_uchar *code, const char *before, const char *after) +{ +if (code[1] != PT_CLIST) + { + fprintf(f, "%s%s %s%s", before, priv_OP_names[*code], get_ucpname(code[1], + code[2]), after); + } +else + { + const char *not = (*code == OP_PROP)? "" : "not "; +#ifndef SUPPORT_UCP + fprintf(f, "%s%sclist %d%s", before, not, code[2], after); +#else + const pcre_uint32 *p = PRIV(ucd_caseless_sets) + code[2]; + fprintf (f, "%s%sclist", before, not); + while (*p < NOTACHAR) fprintf(f, " %04x", *p++); + fprintf(f, "%s", after); +#endif + } +} + + + + +/************************************************* +* Print compiled regex * +*************************************************/ + +/* Make this function work for a regex with integers either byte order. +However, we assume that what we are passed is a compiled regex. The +print_lengths flag controls whether offsets and lengths of items are printed. +They can be turned off from pcretest so that automatic tests on bytecode can be +written that do not depend on the value of LINK_SIZE. */ + +#ifdef PCRE_INCLUDED +static /* Keep the following function as private. */ +#endif +#if defined COMPILE_PCRE8 +void +pcre_printint(pcre *external_re, FILE *f, BOOL print_lengths) +#elif defined COMPILE_PCRE16 +void +pcre16_printint(pcre *external_re, FILE *f, BOOL print_lengths) +#elif defined COMPILE_PCRE32 +void +pcre32_printint(pcre *external_re, FILE *f, BOOL print_lengths) +#endif +{ +REAL_PCRE *re = (REAL_PCRE *)external_re; +pcre_uchar *codestart, *code; +BOOL utf; + +unsigned int options = re->options; +int offset = re->name_table_offset; +int count = re->name_count; +int size = re->name_entry_size; + +if (re->magic_number != MAGIC_NUMBER) + { + offset = ((offset << 8) & 0xff00) | ((offset >> 8) & 0xff); + count = ((count << 8) & 0xff00) | ((count >> 8) & 0xff); + size = ((size << 8) & 0xff00) | ((size >> 8) & 0xff); + options = ((options << 24) & 0xff000000) | + ((options << 8) & 0x00ff0000) | + ((options >> 8) & 0x0000ff00) | + ((options >> 24) & 0x000000ff); + } + +code = codestart = (pcre_uchar *)re + offset + count * size; +/* PCRE_UTF(16|32) have the same value as PCRE_UTF8. */ +utf = (options & PCRE_UTF8) != 0; + +for(;;) + { + pcre_uchar *ccode; + const char *flag = " "; + pcre_uint32 c; + unsigned int extra = 0; + + if (print_lengths) + fprintf(f, "%3d ", (int)(code - codestart)); + else + fprintf(f, " "); + + switch(*code) + { +/* ========================================================================== */ + /* These cases are never obeyed. This is a fudge that causes a compile- + time error if the vectors OP_names or OP_lengths, which are indexed + by opcode, are not the correct length. It seems to be the only way to do + such a check at compile time, as the sizeof() operator does not work in + the C preprocessor. */ + + case OP_TABLE_LENGTH: + case OP_TABLE_LENGTH + + ((sizeof(priv_OP_names)/sizeof(const char *) == OP_TABLE_LENGTH) && + (sizeof(priv_OP_lengths) == OP_TABLE_LENGTH)): + break; +/* ========================================================================== */ + + case OP_END: + fprintf(f, " %s\n", priv_OP_names[*code]); + fprintf(f, "------------------------------------------------------------------\n"); + return; + + case OP_CHAR: + fprintf(f, " "); + do + { + code++; + code += 1 + print_char(f, code, utf); + } + while (*code == OP_CHAR); + fprintf(f, "\n"); + continue; + + case OP_CHARI: + fprintf(f, " /i "); + do + { + code++; + code += 1 + print_char(f, code, utf); + } + while (*code == OP_CHARI); + fprintf(f, "\n"); + continue; + + case OP_CBRA: + case OP_CBRAPOS: + case OP_SCBRA: + case OP_SCBRAPOS: + if (print_lengths) fprintf(f, "%3d ", GET(code, 1)); + else fprintf(f, " "); + fprintf(f, "%s %d", priv_OP_names[*code], GET2(code, 1+LINK_SIZE)); + break; + + case OP_BRA: + case OP_BRAPOS: + case OP_SBRA: + case OP_SBRAPOS: + case OP_KETRMAX: + case OP_KETRMIN: + case OP_KETRPOS: + case OP_ALT: + case OP_KET: + case OP_ASSERT: + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + case OP_ONCE: + case OP_ONCE_NC: + case OP_COND: + case OP_SCOND: + case OP_REVERSE: + if (print_lengths) fprintf(f, "%3d ", GET(code, 1)); + else fprintf(f, " "); + fprintf(f, "%s", priv_OP_names[*code]); + break; + + case OP_CLOSE: + fprintf(f, " %s %d", priv_OP_names[*code], GET2(code, 1)); + break; + + case OP_CREF: + fprintf(f, "%3d %s", GET2(code,1), priv_OP_names[*code]); + break; + + case OP_DNCREF: + { + pcre_uchar *entry = (pcre_uchar *)re + offset + (GET2(code, 1) * size) + + IMM2_SIZE; + fprintf(f, " %s Cond ref <", flag); + print_puchar(f, entry); + fprintf(f, ">%d", GET2(code, 1 + IMM2_SIZE)); + } + break; + + case OP_RREF: + c = GET2(code, 1); + if (c == RREF_ANY) + fprintf(f, " Cond recurse any"); + else + fprintf(f, " Cond recurse %d", c); + break; + + case OP_DNRREF: + { + pcre_uchar *entry = (pcre_uchar *)re + offset + (GET2(code, 1) * size) + + IMM2_SIZE; + fprintf(f, " %s Cond recurse <", flag); + print_puchar(f, entry); + fprintf(f, ">%d", GET2(code, 1 + IMM2_SIZE)); + } + break; + + case OP_DEF: + fprintf(f, " Cond def"); + break; + + case OP_STARI: + case OP_MINSTARI: + case OP_POSSTARI: + case OP_PLUSI: + case OP_MINPLUSI: + case OP_POSPLUSI: + case OP_QUERYI: + case OP_MINQUERYI: + case OP_POSQUERYI: + flag = "/i"; + /* Fall through */ + case OP_STAR: + case OP_MINSTAR: + case OP_POSSTAR: + case OP_PLUS: + case OP_MINPLUS: + case OP_POSPLUS: + case OP_QUERY: + case OP_MINQUERY: + case OP_POSQUERY: + case OP_TYPESTAR: + case OP_TYPEMINSTAR: + case OP_TYPEPOSSTAR: + case OP_TYPEPLUS: + case OP_TYPEMINPLUS: + case OP_TYPEPOSPLUS: + case OP_TYPEQUERY: + case OP_TYPEMINQUERY: + case OP_TYPEPOSQUERY: + fprintf(f, " %s ", flag); + if (*code >= OP_TYPESTAR) + { + if (code[1] == OP_PROP || code[1] == OP_NOTPROP) + { + print_prop(f, code + 1, "", " "); + extra = 2; + } + else fprintf(f, "%s", priv_OP_names[code[1]]); + } + else extra = print_char(f, code+1, utf); + fprintf(f, "%s", priv_OP_names[*code]); + break; + + case OP_EXACTI: + case OP_UPTOI: + case OP_MINUPTOI: + case OP_POSUPTOI: + flag = "/i"; + /* Fall through */ + case OP_EXACT: + case OP_UPTO: + case OP_MINUPTO: + case OP_POSUPTO: + fprintf(f, " %s ", flag); + extra = print_char(f, code + 1 + IMM2_SIZE, utf); + fprintf(f, "{"); + if (*code != OP_EXACT && *code != OP_EXACTI) fprintf(f, "0,"); + fprintf(f, "%d}", GET2(code,1)); + if (*code == OP_MINUPTO || *code == OP_MINUPTOI) fprintf(f, "?"); + else if (*code == OP_POSUPTO || *code == OP_POSUPTOI) fprintf(f, "+"); + break; + + case OP_TYPEEXACT: + case OP_TYPEUPTO: + case OP_TYPEMINUPTO: + case OP_TYPEPOSUPTO: + if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP) + { + print_prop(f, code + IMM2_SIZE + 1, " ", " "); + extra = 2; + } + else fprintf(f, " %s", priv_OP_names[code[1 + IMM2_SIZE]]); + fprintf(f, "{"); + if (*code != OP_TYPEEXACT) fprintf(f, "0,"); + fprintf(f, "%d}", GET2(code,1)); + if (*code == OP_TYPEMINUPTO) fprintf(f, "?"); + else if (*code == OP_TYPEPOSUPTO) fprintf(f, "+"); + break; + + case OP_NOTI: + flag = "/i"; + /* Fall through */ + case OP_NOT: + fprintf(f, " %s [^", flag); + extra = print_char(f, code + 1, utf); + fprintf(f, "]"); + break; + + case OP_NOTSTARI: + case OP_NOTMINSTARI: + case OP_NOTPOSSTARI: + case OP_NOTPLUSI: + case OP_NOTMINPLUSI: + case OP_NOTPOSPLUSI: + case OP_NOTQUERYI: + case OP_NOTMINQUERYI: + case OP_NOTPOSQUERYI: + flag = "/i"; + /* Fall through */ + + case OP_NOTSTAR: + case OP_NOTMINSTAR: + case OP_NOTPOSSTAR: + case OP_NOTPLUS: + case OP_NOTMINPLUS: + case OP_NOTPOSPLUS: + case OP_NOTQUERY: + case OP_NOTMINQUERY: + case OP_NOTPOSQUERY: + fprintf(f, " %s [^", flag); + extra = print_char(f, code + 1, utf); + fprintf(f, "]%s", priv_OP_names[*code]); + break; + + case OP_NOTEXACTI: + case OP_NOTUPTOI: + case OP_NOTMINUPTOI: + case OP_NOTPOSUPTOI: + flag = "/i"; + /* Fall through */ + + case OP_NOTEXACT: + case OP_NOTUPTO: + case OP_NOTMINUPTO: + case OP_NOTPOSUPTO: + fprintf(f, " %s [^", flag); + extra = print_char(f, code + 1 + IMM2_SIZE, utf); + fprintf(f, "]{"); + if (*code != OP_NOTEXACT && *code != OP_NOTEXACTI) fprintf(f, "0,"); + fprintf(f, "%d}", GET2(code,1)); + if (*code == OP_NOTMINUPTO || *code == OP_NOTMINUPTOI) fprintf(f, "?"); + else + if (*code == OP_NOTPOSUPTO || *code == OP_NOTPOSUPTOI) fprintf(f, "+"); + break; + + case OP_RECURSE: + if (print_lengths) fprintf(f, "%3d ", GET(code, 1)); + else fprintf(f, " "); + fprintf(f, "%s", priv_OP_names[*code]); + break; + + case OP_REFI: + flag = "/i"; + /* Fall through */ + case OP_REF: + fprintf(f, " %s \\%d", flag, GET2(code,1)); + ccode = code + priv_OP_lengths[*code]; + goto CLASS_REF_REPEAT; + + case OP_DNREFI: + flag = "/i"; + /* Fall through */ + case OP_DNREF: + { + pcre_uchar *entry = (pcre_uchar *)re + offset + (GET2(code, 1) * size) + + IMM2_SIZE; + fprintf(f, " %s \\k<", flag); + print_puchar(f, entry); + fprintf(f, ">%d", GET2(code, 1 + IMM2_SIZE)); + } + ccode = code + priv_OP_lengths[*code]; + goto CLASS_REF_REPEAT; + + case OP_CALLOUT: + fprintf(f, " %s %d %d %d", priv_OP_names[*code], code[1], GET(code,2), + GET(code, 2 + LINK_SIZE)); + break; + + case OP_PROP: + case OP_NOTPROP: + print_prop(f, code, " ", ""); + break; + + /* OP_XCLASS cannot occur in 8-bit, non-UTF mode. However, there's no harm + in having this code always here, and it makes it less messy without all + those #ifdefs. */ + + case OP_CLASS: + case OP_NCLASS: + case OP_XCLASS: + { + int i; + unsigned int min, max; + BOOL printmap; + BOOL invertmap = FALSE; + pcre_uint8 *map; + pcre_uint8 inverted_map[32]; + + fprintf(f, " ["); + + if (*code == OP_XCLASS) + { + extra = GET(code, 1); + ccode = code + LINK_SIZE + 1; + printmap = (*ccode & XCL_MAP) != 0; + if ((*ccode & XCL_NOT) != 0) + { + invertmap = (*ccode & XCL_HASPROP) == 0; + fprintf(f, "^"); + } + ccode++; + } + else + { + printmap = TRUE; + ccode = code + 1; + } + + /* Print a bit map */ + + if (printmap) + { + map = (pcre_uint8 *)ccode; + if (invertmap) + { + for (i = 0; i < 32; i++) inverted_map[i] = ~map[i]; + map = inverted_map; + } + + for (i = 0; i < 256; i++) + { + if ((map[i/8] & (1 << (i&7))) != 0) + { + int j; + for (j = i+1; j < 256; j++) + if ((map[j/8] & (1 << (j&7))) == 0) break; + if (i == '-' || i == ']') fprintf(f, "\\"); + if (PRINTABLE(i)) fprintf(f, "%c", i); + else fprintf(f, "\\x%02x", i); + if (--j > i) + { + if (j != i + 1) fprintf(f, "-"); + if (j == '-' || j == ']') fprintf(f, "\\"); + if (PRINTABLE(j)) fprintf(f, "%c", j); + else fprintf(f, "\\x%02x", j); + } + i = j; + } + } + ccode += 32 / sizeof(pcre_uchar); + } + + /* For an XCLASS there is always some additional data */ + + if (*code == OP_XCLASS) + { + pcre_uchar ch; + while ((ch = *ccode++) != XCL_END) + { + BOOL not = FALSE; + const char *notch = ""; + + switch(ch) + { + case XCL_NOTPROP: + not = TRUE; + notch = "^"; + /* Fall through */ + + case XCL_PROP: + { + unsigned int ptype = *ccode++; + unsigned int pvalue = *ccode++; + + switch(ptype) + { + case PT_PXGRAPH: + fprintf(f, "[:%sgraph:]", notch); + break; + + case PT_PXPRINT: + fprintf(f, "[:%sprint:]", notch); + break; + + case PT_PXPUNCT: + fprintf(f, "[:%spunct:]", notch); + break; + + default: + fprintf(f, "\\%c{%s}", (not? 'P':'p'), + get_ucpname(ptype, pvalue)); + break; + } + } + break; + + default: + ccode += 1 + print_char(f, ccode, utf); + if (ch == XCL_RANGE) + { + fprintf(f, "-"); + ccode += 1 + print_char(f, ccode, utf); + } + break; + } + } + } + + /* Indicate a non-UTF class which was created by negation */ + + fprintf(f, "]%s", (*code == OP_NCLASS)? " (neg)" : ""); + + /* Handle repeats after a class or a back reference */ + + CLASS_REF_REPEAT: + switch(*ccode) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRPLUS: + case OP_CRMINPLUS: + case OP_CRQUERY: + case OP_CRMINQUERY: + case OP_CRPOSSTAR: + case OP_CRPOSPLUS: + case OP_CRPOSQUERY: + fprintf(f, "%s", priv_OP_names[*ccode]); + extra += priv_OP_lengths[*ccode]; + break; + + case OP_CRRANGE: + case OP_CRMINRANGE: + case OP_CRPOSRANGE: + min = GET2(ccode,1); + max = GET2(ccode,1 + IMM2_SIZE); + if (max == 0) fprintf(f, "{%u,}", min); + else fprintf(f, "{%u,%u}", min, max); + if (*ccode == OP_CRMINRANGE) fprintf(f, "?"); + else if (*ccode == OP_CRPOSRANGE) fprintf(f, "+"); + extra += priv_OP_lengths[*ccode]; + break; + + /* Do nothing if it's not a repeat; this code stops picky compilers + warning about the lack of a default code path. */ + + default: + break; + } + } + break; + + case OP_MARK: + case OP_PRUNE_ARG: + case OP_SKIP_ARG: + case OP_THEN_ARG: + fprintf(f, " %s ", priv_OP_names[*code]); + print_puchar(f, code + 2); + extra += code[1]; + break; + + case OP_THEN: + fprintf(f, " %s", priv_OP_names[*code]); + break; + + case OP_CIRCM: + case OP_DOLLM: + flag = "/m"; + /* Fall through */ + + /* Anything else is just an item with no data, but possibly a flag. */ + + default: + fprintf(f, " %s %s", flag, priv_OP_names[*code]); + break; + } + + code += priv_OP_lengths[*code] + extra; + fprintf(f, "\n"); + } +} + +/* End of pcre_printint.src */ diff --git a/deps/pcre/pcre_refcount.c b/deps/pcre/pcre_refcount.c new file mode 100644 index 00000000000..79efa90f216 --- /dev/null +++ b/deps/pcre/pcre_refcount.c @@ -0,0 +1,92 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2012 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains the external function pcre_refcount(), which is an +auxiliary function that can be used to maintain a reference count in a compiled +pattern data block. This might be helpful in applications where the block is +shared by different users. */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pcre_internal.h" + + +/************************************************* +* Maintain reference count * +*************************************************/ + +/* The reference count is a 16-bit field, initialized to zero. It is not +possible to transfer a non-zero count from one host to a different host that +has a different byte order - though I can't see why anyone in their right mind +would ever want to do that! + +Arguments: + argument_re points to compiled code + adjust value to add to the count + +Returns: the (possibly updated) count value (a non-negative number), or + a negative error number +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_refcount(pcre *argument_re, int adjust) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre16_refcount(pcre16 *argument_re, int adjust) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN int PCRE_CALL_CONVENTION +pcre32_refcount(pcre32 *argument_re, int adjust) +#endif +{ +REAL_PCRE *re = (REAL_PCRE *)argument_re; +if (re == NULL) return PCRE_ERROR_NULL; +if (re->magic_number != MAGIC_NUMBER) return PCRE_ERROR_BADMAGIC; +if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE; +re->ref_count = (-adjust > re->ref_count)? 0 : + (adjust + re->ref_count > 65535)? 65535 : + re->ref_count + adjust; +return re->ref_count; +} + +/* End of pcre_refcount.c */ diff --git a/deps/pcre/pcre_string_utils.c b/deps/pcre/pcre_string_utils.c new file mode 100644 index 00000000000..25eacc85073 --- /dev/null +++ b/deps/pcre/pcre_string_utils.c @@ -0,0 +1,211 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2014 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains internal functions for comparing and finding the length +of strings for different data item sizes. */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pcre_internal.h" + +#ifndef COMPILE_PCRE8 + +/************************************************* +* Compare string utilities * +*************************************************/ + +/* The following two functions compares two strings. Basically a strcmp +for non 8 bit characters. + +Arguments: + str1 first string + str2 second string + +Returns: 0 if both string are equal (like strcmp), 1 otherwise +*/ + +int +PRIV(strcmp_uc_uc)(const pcre_uchar *str1, const pcre_uchar *str2) +{ +pcre_uchar c1; +pcre_uchar c2; + +while (*str1 != '\0' || *str2 != '\0') + { + c1 = *str1++; + c2 = *str2++; + if (c1 != c2) + return ((c1 > c2) << 1) - 1; + } +/* Both length and characters must be equal. */ +return 0; +} + +#ifdef COMPILE_PCRE32 + +int +PRIV(strcmp_uc_uc_utf)(const pcre_uchar *str1, const pcre_uchar *str2) +{ +pcre_uchar c1; +pcre_uchar c2; + +while (*str1 != '\0' || *str2 != '\0') + { + c1 = UCHAR21INC(str1); + c2 = UCHAR21INC(str2); + if (c1 != c2) + return ((c1 > c2) << 1) - 1; + } +/* Both length and characters must be equal. */ +return 0; +} + +#endif /* COMPILE_PCRE32 */ + +int +PRIV(strcmp_uc_c8)(const pcre_uchar *str1, const char *str2) +{ +const pcre_uint8 *ustr2 = (pcre_uint8 *)str2; +pcre_uchar c1; +pcre_uchar c2; + +while (*str1 != '\0' || *ustr2 != '\0') + { + c1 = *str1++; + c2 = (pcre_uchar)*ustr2++; + if (c1 != c2) + return ((c1 > c2) << 1) - 1; + } +/* Both length and characters must be equal. */ +return 0; +} + +#ifdef COMPILE_PCRE32 + +int +PRIV(strcmp_uc_c8_utf)(const pcre_uchar *str1, const char *str2) +{ +const pcre_uint8 *ustr2 = (pcre_uint8 *)str2; +pcre_uchar c1; +pcre_uchar c2; + +while (*str1 != '\0' || *ustr2 != '\0') + { + c1 = UCHAR21INC(str1); + c2 = (pcre_uchar)*ustr2++; + if (c1 != c2) + return ((c1 > c2) << 1) - 1; + } +/* Both length and characters must be equal. */ +return 0; +} + +#endif /* COMPILE_PCRE32 */ + +/* The following two functions compares two, fixed length +strings. Basically an strncmp for non 8 bit characters. + +Arguments: + str1 first string + str2 second string + num size of the string + +Returns: 0 if both string are equal (like strcmp), 1 otherwise +*/ + +int +PRIV(strncmp_uc_uc)(const pcre_uchar *str1, const pcre_uchar *str2, unsigned int num) +{ +pcre_uchar c1; +pcre_uchar c2; + +while (num-- > 0) + { + c1 = *str1++; + c2 = *str2++; + if (c1 != c2) + return ((c1 > c2) << 1) - 1; + } +/* Both length and characters must be equal. */ +return 0; +} + +int +PRIV(strncmp_uc_c8)(const pcre_uchar *str1, const char *str2, unsigned int num) +{ +const pcre_uint8 *ustr2 = (pcre_uint8 *)str2; +pcre_uchar c1; +pcre_uchar c2; + +while (num-- > 0) + { + c1 = *str1++; + c2 = (pcre_uchar)*ustr2++; + if (c1 != c2) + return ((c1 > c2) << 1) - 1; + } +/* Both length and characters must be equal. */ +return 0; +} + +/* The following function returns with the length of +a zero terminated string. Basically an strlen for non 8 bit characters. + +Arguments: + str string + +Returns: length of the string +*/ + +unsigned int +PRIV(strlen_uc)(const pcre_uchar *str) +{ +unsigned int len = 0; +while (*str++ != 0) + len++; +return len; +} + +#endif /* !COMPILE_PCRE8 */ + +/* End of pcre_string_utils.c */ diff --git a/deps/pcre/pcre_study.c b/deps/pcre/pcre_study.c new file mode 100644 index 00000000000..d9d4960d84e --- /dev/null +++ b/deps/pcre/pcre_study.c @@ -0,0 +1,1686 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2012 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains the external function pcre_study(), along with local +supporting functions. */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pcre_internal.h" + +#define SET_BIT(c) start_bits[c/8] |= (1 << (c&7)) + +/* Returns from set_start_bits() */ + +enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE, SSB_UNKNOWN }; + + + +/************************************************* +* Find the minimum subject length for a group * +*************************************************/ + +/* Scan a parenthesized group and compute the minimum length of subject that +is needed to match it. This is a lower bound; it does not mean there is a +string of that length that matches. In UTF8 mode, the result is in characters +rather than bytes. + +Arguments: + re compiled pattern block + code pointer to start of group (the bracket) + startcode pointer to start of the whole pattern's code + options the compiling options + recurses chain of recurse_check to catch mutual recursion + countptr pointer to call count (to catch over complexity) + +Returns: the minimum length + -1 if \C in UTF-8 mode or (*ACCEPT) was encountered + -2 internal error (missing capturing bracket) + -3 internal error (opcode not listed) +*/ + +static int +find_minlength(const REAL_PCRE *re, const pcre_uchar *code, + const pcre_uchar *startcode, int options, recurse_check *recurses, + int *countptr) +{ +int length = -1; +/* PCRE_UTF16 has the same value as PCRE_UTF8. */ +BOOL utf = (options & PCRE_UTF8) != 0; +BOOL had_recurse = FALSE; +recurse_check this_recurse; +register int branchlength = 0; +register pcre_uchar *cc = (pcre_uchar *)code + 1 + LINK_SIZE; + +if ((*countptr)++ > 1000) return -1; /* too complex */ + +if (*code == OP_CBRA || *code == OP_SCBRA || + *code == OP_CBRAPOS || *code == OP_SCBRAPOS) cc += IMM2_SIZE; + +/* Scan along the opcodes for this branch. If we get to the end of the +branch, check the length against that of the other branches. */ + +for (;;) + { + int d, min; + pcre_uchar *cs, *ce; + register pcre_uchar op = *cc; + + switch (op) + { + case OP_COND: + case OP_SCOND: + + /* If there is only one branch in a condition, the implied branch has zero + length, so we don't add anything. This covers the DEFINE "condition" + automatically. */ + + cs = cc + GET(cc, 1); + if (*cs != OP_ALT) + { + cc = cs + 1 + LINK_SIZE; + break; + } + + /* Otherwise we can fall through and treat it the same as any other + subpattern. */ + + case OP_CBRA: + case OP_SCBRA: + case OP_BRA: + case OP_SBRA: + case OP_CBRAPOS: + case OP_SCBRAPOS: + case OP_BRAPOS: + case OP_SBRAPOS: + case OP_ONCE: + case OP_ONCE_NC: + d = find_minlength(re, cc, startcode, options, recurses, countptr); + if (d < 0) return d; + branchlength += d; + do cc += GET(cc, 1); while (*cc == OP_ALT); + cc += 1 + LINK_SIZE; + break; + + /* ACCEPT makes things far too complicated; we have to give up. */ + + case OP_ACCEPT: + case OP_ASSERT_ACCEPT: + return -1; + + /* Reached end of a branch; if it's a ket it is the end of a nested + call. If it's ALT it is an alternation in a nested call. If it is END it's + the end of the outer call. All can be handled by the same code. If an + ACCEPT was previously encountered, use the length that was in force at that + time, and pass back the shortest ACCEPT length. */ + + case OP_ALT: + case OP_KET: + case OP_KETRMAX: + case OP_KETRMIN: + case OP_KETRPOS: + case OP_END: + if (length < 0 || (!had_recurse && branchlength < length)) + length = branchlength; + if (op != OP_ALT) return length; + cc += 1 + LINK_SIZE; + branchlength = 0; + had_recurse = FALSE; + break; + + /* Skip over assertive subpatterns */ + + case OP_ASSERT: + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + do cc += GET(cc, 1); while (*cc == OP_ALT); + /* Fall through */ + + /* Skip over things that don't match chars */ + + case OP_REVERSE: + case OP_CREF: + case OP_DNCREF: + case OP_RREF: + case OP_DNRREF: + case OP_DEF: + case OP_CALLOUT: + case OP_SOD: + case OP_SOM: + case OP_EOD: + case OP_EODN: + case OP_CIRC: + case OP_CIRCM: + case OP_DOLL: + case OP_DOLLM: + case OP_NOT_WORD_BOUNDARY: + case OP_WORD_BOUNDARY: + cc += PRIV(OP_lengths)[*cc]; + break; + + /* Skip over a subpattern that has a {0} or {0,x} quantifier */ + + case OP_BRAZERO: + case OP_BRAMINZERO: + case OP_BRAPOSZERO: + case OP_SKIPZERO: + cc += PRIV(OP_lengths)[*cc]; + do cc += GET(cc, 1); while (*cc == OP_ALT); + cc += 1 + LINK_SIZE; + break; + + /* Handle literal characters and + repetitions */ + + case OP_CHAR: + case OP_CHARI: + case OP_NOT: + case OP_NOTI: + case OP_PLUS: + case OP_PLUSI: + case OP_MINPLUS: + case OP_MINPLUSI: + case OP_POSPLUS: + case OP_POSPLUSI: + case OP_NOTPLUS: + case OP_NOTPLUSI: + case OP_NOTMINPLUS: + case OP_NOTMINPLUSI: + case OP_NOTPOSPLUS: + case OP_NOTPOSPLUSI: + branchlength++; + cc += 2; +#ifdef SUPPORT_UTF + if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); +#endif + break; + + case OP_TYPEPLUS: + case OP_TYPEMINPLUS: + case OP_TYPEPOSPLUS: + branchlength++; + cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2; + break; + + /* Handle exact repetitions. The count is already in characters, but we + need to skip over a multibyte character in UTF8 mode. */ + + case OP_EXACT: + case OP_EXACTI: + case OP_NOTEXACT: + case OP_NOTEXACTI: + branchlength += GET2(cc,1); + cc += 2 + IMM2_SIZE; +#ifdef SUPPORT_UTF + if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); +#endif + break; + + case OP_TYPEEXACT: + branchlength += GET2(cc,1); + cc += 2 + IMM2_SIZE + ((cc[1 + IMM2_SIZE] == OP_PROP + || cc[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0); + break; + + /* Handle single-char non-literal matchers */ + + case OP_PROP: + case OP_NOTPROP: + cc += 2; + /* Fall through */ + + case OP_NOT_DIGIT: + case OP_DIGIT: + case OP_NOT_WHITESPACE: + case OP_WHITESPACE: + case OP_NOT_WORDCHAR: + case OP_WORDCHAR: + case OP_ANY: + case OP_ALLANY: + case OP_EXTUNI: + case OP_HSPACE: + case OP_NOT_HSPACE: + case OP_VSPACE: + case OP_NOT_VSPACE: + branchlength++; + cc++; + break; + + /* "Any newline" might match two characters, but it also might match just + one. */ + + case OP_ANYNL: + branchlength += 1; + cc++; + break; + + /* The single-byte matcher means we can't proceed in UTF-8 mode. (In + non-UTF-8 mode \C will actually be turned into OP_ALLANY, so won't ever + appear, but leave the code, just in case.) */ + + case OP_ANYBYTE: +#ifdef SUPPORT_UTF + if (utf) return -1; +#endif + branchlength++; + cc++; + break; + + /* For repeated character types, we have to test for \p and \P, which have + an extra two bytes of parameters. */ + + case OP_TYPESTAR: + case OP_TYPEMINSTAR: + case OP_TYPEQUERY: + case OP_TYPEMINQUERY: + case OP_TYPEPOSSTAR: + case OP_TYPEPOSQUERY: + if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2; + cc += PRIV(OP_lengths)[op]; + break; + + case OP_TYPEUPTO: + case OP_TYPEMINUPTO: + case OP_TYPEPOSUPTO: + if (cc[1 + IMM2_SIZE] == OP_PROP + || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2; + cc += PRIV(OP_lengths)[op]; + break; + + /* Check a class for variable quantification */ + + case OP_CLASS: + case OP_NCLASS: +#if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + case OP_XCLASS: + /* The original code caused an unsigned overflow in 64 bit systems, + so now we use a conditional statement. */ + if (op == OP_XCLASS) + cc += GET(cc, 1); + else + cc += PRIV(OP_lengths)[OP_CLASS]; +#else + cc += PRIV(OP_lengths)[OP_CLASS]; +#endif + + switch (*cc) + { + case OP_CRPLUS: + case OP_CRMINPLUS: + case OP_CRPOSPLUS: + branchlength++; + /* Fall through */ + + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRQUERY: + case OP_CRMINQUERY: + case OP_CRPOSSTAR: + case OP_CRPOSQUERY: + cc++; + break; + + case OP_CRRANGE: + case OP_CRMINRANGE: + case OP_CRPOSRANGE: + branchlength += GET2(cc,1); + cc += 1 + 2 * IMM2_SIZE; + break; + + default: + branchlength++; + break; + } + break; + + /* Backreferences and subroutine calls are treated in the same way: we find + the minimum length for the subpattern. A recursion, however, causes an + a flag to be set that causes the length of this branch to be ignored. The + logic is that a recursion can only make sense if there is another + alternation that stops the recursing. That will provide the minimum length + (when no recursion happens). A backreference within the group that it is + referencing behaves in the same way. + + If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket + matches an empty string (by default it causes a matching failure), so in + that case we must set the minimum length to zero. */ + + case OP_DNREF: /* Duplicate named pattern back reference */ + case OP_DNREFI: + if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) + { + int count = GET2(cc, 1+IMM2_SIZE); + pcre_uchar *slot = (pcre_uchar *)re + + re->name_table_offset + GET2(cc, 1) * re->name_entry_size; + d = INT_MAX; + while (count-- > 0) + { + ce = cs = (pcre_uchar *)PRIV(find_bracket)(startcode, utf, GET2(slot, 0)); + if (cs == NULL) return -2; + do ce += GET(ce, 1); while (*ce == OP_ALT); + if (cc > cs && cc < ce) /* Simple recursion */ + { + d = 0; + had_recurse = TRUE; + break; + } + else + { + recurse_check *r = recurses; + for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break; + if (r != NULL) /* Mutual recursion */ + { + d = 0; + had_recurse = TRUE; + break; + } + else + { + int dd; + this_recurse.prev = recurses; + this_recurse.group = cs; + dd = find_minlength(re, cs, startcode, options, &this_recurse, + countptr); + if (dd < d) d = dd; + } + } + slot += re->name_entry_size; + } + } + else d = 0; + cc += 1 + 2*IMM2_SIZE; + goto REPEAT_BACK_REFERENCE; + + case OP_REF: /* Single back reference */ + case OP_REFI: + if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) + { + ce = cs = (pcre_uchar *)PRIV(find_bracket)(startcode, utf, GET2(cc, 1)); + if (cs == NULL) return -2; + do ce += GET(ce, 1); while (*ce == OP_ALT); + if (cc > cs && cc < ce) /* Simple recursion */ + { + d = 0; + had_recurse = TRUE; + } + else + { + recurse_check *r = recurses; + for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break; + if (r != NULL) /* Mutual recursion */ + { + d = 0; + had_recurse = TRUE; + } + else + { + this_recurse.prev = recurses; + this_recurse.group = cs; + d = find_minlength(re, cs, startcode, options, &this_recurse, + countptr); + } + } + } + else d = 0; + cc += 1 + IMM2_SIZE; + + /* Handle repeated back references */ + + REPEAT_BACK_REFERENCE: + switch (*cc) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRQUERY: + case OP_CRMINQUERY: + case OP_CRPOSSTAR: + case OP_CRPOSQUERY: + min = 0; + cc++; + break; + + case OP_CRPLUS: + case OP_CRMINPLUS: + case OP_CRPOSPLUS: + min = 1; + cc++; + break; + + case OP_CRRANGE: + case OP_CRMINRANGE: + case OP_CRPOSRANGE: + min = GET2(cc, 1); + cc += 1 + 2 * IMM2_SIZE; + break; + + default: + min = 1; + break; + } + + branchlength += min * d; + break; + + /* We can easily detect direct recursion, but not mutual recursion. This is + caught by a recursion depth count. */ + + case OP_RECURSE: + cs = ce = (pcre_uchar *)startcode + GET(cc, 1); + do ce += GET(ce, 1); while (*ce == OP_ALT); + if (cc > cs && cc < ce) /* Simple recursion */ + had_recurse = TRUE; + else + { + recurse_check *r = recurses; + for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break; + if (r != NULL) /* Mutual recursion */ + had_recurse = TRUE; + else + { + this_recurse.prev = recurses; + this_recurse.group = cs; + branchlength += find_minlength(re, cs, startcode, options, + &this_recurse, countptr); + } + } + cc += 1 + LINK_SIZE; + break; + + /* Anything else does not or need not match a character. We can get the + item's length from the table, but for those that can match zero occurrences + of a character, we must take special action for UTF-8 characters. As it + happens, the "NOT" versions of these opcodes are used at present only for + ASCII characters, so they could be omitted from this list. However, in + future that may change, so we include them here so as not to leave a + gotcha for a future maintainer. */ + + case OP_UPTO: + case OP_UPTOI: + case OP_NOTUPTO: + case OP_NOTUPTOI: + case OP_MINUPTO: + case OP_MINUPTOI: + case OP_NOTMINUPTO: + case OP_NOTMINUPTOI: + case OP_POSUPTO: + case OP_POSUPTOI: + case OP_NOTPOSUPTO: + case OP_NOTPOSUPTOI: + + case OP_STAR: + case OP_STARI: + case OP_NOTSTAR: + case OP_NOTSTARI: + case OP_MINSTAR: + case OP_MINSTARI: + case OP_NOTMINSTAR: + case OP_NOTMINSTARI: + case OP_POSSTAR: + case OP_POSSTARI: + case OP_NOTPOSSTAR: + case OP_NOTPOSSTARI: + + case OP_QUERY: + case OP_QUERYI: + case OP_NOTQUERY: + case OP_NOTQUERYI: + case OP_MINQUERY: + case OP_MINQUERYI: + case OP_NOTMINQUERY: + case OP_NOTMINQUERYI: + case OP_POSQUERY: + case OP_POSQUERYI: + case OP_NOTPOSQUERY: + case OP_NOTPOSQUERYI: + + cc += PRIV(OP_lengths)[op]; +#ifdef SUPPORT_UTF + if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); +#endif + break; + + /* Skip these, but we need to add in the name length. */ + + case OP_MARK: + case OP_PRUNE_ARG: + case OP_SKIP_ARG: + case OP_THEN_ARG: + cc += PRIV(OP_lengths)[op] + cc[1]; + break; + + /* The remaining opcodes are just skipped over. */ + + case OP_CLOSE: + case OP_COMMIT: + case OP_FAIL: + case OP_PRUNE: + case OP_SET_SOM: + case OP_SKIP: + case OP_THEN: + cc += PRIV(OP_lengths)[op]; + break; + + /* This should not occur: we list all opcodes explicitly so that when + new ones get added they are properly considered. */ + + default: + return -3; + } + } +/* Control never gets here */ +} + + + +/************************************************* +* Set a bit and maybe its alternate case * +*************************************************/ + +/* Given a character, set its first byte's bit in the table, and also the +corresponding bit for the other version of a letter if we are caseless. In +UTF-8 mode, for characters greater than 127, we can only do the caseless thing +when Unicode property support is available. + +Arguments: + start_bits points to the bit map + p points to the character + caseless the caseless flag + cd the block with char table pointers + utf TRUE for UTF-8 / UTF-16 / UTF-32 mode + +Returns: pointer after the character +*/ + +static const pcre_uchar * +set_table_bit(pcre_uint8 *start_bits, const pcre_uchar *p, BOOL caseless, + compile_data *cd, BOOL utf) +{ +pcre_uint32 c = *p; + +#ifdef COMPILE_PCRE8 +SET_BIT(c); + +#ifdef SUPPORT_UTF +if (utf && c > 127) + { + GETCHARINC(c, p); +#ifdef SUPPORT_UCP + if (caseless) + { + pcre_uchar buff[6]; + c = UCD_OTHERCASE(c); + (void)PRIV(ord2utf)(c, buff); + SET_BIT(buff[0]); + } +#endif /* Not SUPPORT_UCP */ + return p; + } +#else /* Not SUPPORT_UTF */ +(void)(utf); /* Stops warning for unused parameter */ +#endif /* SUPPORT_UTF */ + +/* Not UTF-8 mode, or character is less than 127. */ + +if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]); +return p + 1; +#endif /* COMPILE_PCRE8 */ + +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 +if (c > 0xff) + { + c = 0xff; + caseless = FALSE; + } +SET_BIT(c); + +#ifdef SUPPORT_UTF +if (utf && c > 127) + { + GETCHARINC(c, p); +#ifdef SUPPORT_UCP + if (caseless) + { + c = UCD_OTHERCASE(c); + if (c > 0xff) + c = 0xff; + SET_BIT(c); + } +#endif /* SUPPORT_UCP */ + return p; + } +#else /* Not SUPPORT_UTF */ +(void)(utf); /* Stops warning for unused parameter */ +#endif /* SUPPORT_UTF */ + +if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]); +return p + 1; +#endif +} + + + +/************************************************* +* Set bits for a positive character type * +*************************************************/ + +/* This function sets starting bits for a character type. In UTF-8 mode, we can +only do a direct setting for bytes less than 128, as otherwise there can be +confusion with bytes in the middle of UTF-8 characters. In a "traditional" +environment, the tables will only recognize ASCII characters anyway, but in at +least one Windows environment, some higher bytes bits were set in the tables. +So we deal with that case by considering the UTF-8 encoding. + +Arguments: + start_bits the starting bitmap + cbit type the type of character wanted + table_limit 32 for non-UTF-8; 16 for UTF-8 + cd the block with char table pointers + +Returns: nothing +*/ + +static void +set_type_bits(pcre_uint8 *start_bits, int cbit_type, unsigned int table_limit, + compile_data *cd) +{ +register pcre_uint32 c; +for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type]; +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 +if (table_limit == 32) return; +for (c = 128; c < 256; c++) + { + if ((cd->cbits[c/8] & (1 << (c&7))) != 0) + { + pcre_uchar buff[6]; + (void)PRIV(ord2utf)(c, buff); + SET_BIT(buff[0]); + } + } +#endif +} + + +/************************************************* +* Set bits for a negative character type * +*************************************************/ + +/* This function sets starting bits for a negative character type such as \D. +In UTF-8 mode, we can only do a direct setting for bytes less than 128, as +otherwise there can be confusion with bytes in the middle of UTF-8 characters. +Unlike in the positive case, where we can set appropriate starting bits for +specific high-valued UTF-8 characters, in this case we have to set the bits for +all high-valued characters. The lowest is 0xc2, but we overkill by starting at +0xc0 (192) for simplicity. + +Arguments: + start_bits the starting bitmap + cbit type the type of character wanted + table_limit 32 for non-UTF-8; 16 for UTF-8 + cd the block with char table pointers + +Returns: nothing +*/ + +static void +set_nottype_bits(pcre_uint8 *start_bits, int cbit_type, unsigned int table_limit, + compile_data *cd) +{ +register pcre_uint32 c; +for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type]; +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 +if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff; +#endif +} + + + +/************************************************* +* Create bitmap of starting bytes * +*************************************************/ + +/* This function scans a compiled unanchored expression recursively and +attempts to build a bitmap of the set of possible starting bytes. As time goes +by, we may be able to get more clever at doing this. The SSB_CONTINUE return is +useful for parenthesized groups in patterns such as (a*)b where the group +provides some optional starting bytes but scanning must continue at the outer +level to find at least one mandatory byte. At the outermost level, this +function fails unless the result is SSB_DONE. + +Arguments: + code points to an expression + start_bits points to a 32-byte table, initialized to 0 + utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode + cd the block with char table pointers + +Returns: SSB_FAIL => Failed to find any starting bytes + SSB_DONE => Found mandatory starting bytes + SSB_CONTINUE => Found optional starting bytes + SSB_UNKNOWN => Hit an unrecognized opcode +*/ + +static int +set_start_bits(const pcre_uchar *code, pcre_uint8 *start_bits, BOOL utf, + compile_data *cd) +{ +register pcre_uint32 c; +int yield = SSB_DONE; +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 +int table_limit = utf? 16:32; +#else +int table_limit = 32; +#endif + +#if 0 +/* ========================================================================= */ +/* The following comment and code was inserted in January 1999. In May 2006, +when it was observed to cause compiler warnings about unused values, I took it +out again. If anybody is still using OS/2, they will have to put it back +manually. */ + +/* This next statement and the later reference to dummy are here in order to +trick the optimizer of the IBM C compiler for OS/2 into generating correct +code. Apparently IBM isn't going to fix the problem, and we would rather not +disable optimization (in this module it actually makes a big difference, and +the pcre module can use all the optimization it can get). */ + +volatile int dummy; +/* ========================================================================= */ +#endif + +do + { + BOOL try_next = TRUE; + const pcre_uchar *tcode = code + 1 + LINK_SIZE; + + if (*code == OP_CBRA || *code == OP_SCBRA || + *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += IMM2_SIZE; + + while (try_next) /* Loop for items in this branch */ + { + int rc; + + switch(*tcode) + { + /* If we reach something we don't understand, it means a new opcode has + been created that hasn't been added to this code. Hopefully this problem + will be discovered during testing. */ + + default: + return SSB_UNKNOWN; + + /* Fail for a valid opcode that implies no starting bits. */ + + case OP_ACCEPT: + case OP_ASSERT_ACCEPT: + case OP_ALLANY: + case OP_ANY: + case OP_ANYBYTE: + case OP_CIRC: + case OP_CIRCM: + case OP_CLOSE: + case OP_COMMIT: + case OP_COND: + case OP_CREF: + case OP_DEF: + case OP_DNCREF: + case OP_DNREF: + case OP_DNREFI: + case OP_DNRREF: + case OP_DOLL: + case OP_DOLLM: + case OP_END: + case OP_EOD: + case OP_EODN: + case OP_EXTUNI: + case OP_FAIL: + case OP_MARK: + case OP_NOT: + case OP_NOTEXACT: + case OP_NOTEXACTI: + case OP_NOTI: + case OP_NOTMINPLUS: + case OP_NOTMINPLUSI: + case OP_NOTMINQUERY: + case OP_NOTMINQUERYI: + case OP_NOTMINSTAR: + case OP_NOTMINSTARI: + case OP_NOTMINUPTO: + case OP_NOTMINUPTOI: + case OP_NOTPLUS: + case OP_NOTPLUSI: + case OP_NOTPOSPLUS: + case OP_NOTPOSPLUSI: + case OP_NOTPOSQUERY: + case OP_NOTPOSQUERYI: + case OP_NOTPOSSTAR: + case OP_NOTPOSSTARI: + case OP_NOTPOSUPTO: + case OP_NOTPOSUPTOI: + case OP_NOTPROP: + case OP_NOTQUERY: + case OP_NOTQUERYI: + case OP_NOTSTAR: + case OP_NOTSTARI: + case OP_NOTUPTO: + case OP_NOTUPTOI: + case OP_NOT_HSPACE: + case OP_NOT_VSPACE: + case OP_PRUNE: + case OP_PRUNE_ARG: + case OP_RECURSE: + case OP_REF: + case OP_REFI: + case OP_REVERSE: + case OP_RREF: + case OP_SCOND: + case OP_SET_SOM: + case OP_SKIP: + case OP_SKIP_ARG: + case OP_SOD: + case OP_SOM: + case OP_THEN: + case OP_THEN_ARG: + return SSB_FAIL; + + /* A "real" property test implies no starting bits, but the fake property + PT_CLIST identifies a list of characters. These lists are short, as they + are used for characters with more than one "other case", so there is no + point in recognizing them for OP_NOTPROP. */ + + case OP_PROP: + if (tcode[1] != PT_CLIST) return SSB_FAIL; + { + const pcre_uint32 *p = PRIV(ucd_caseless_sets) + tcode[2]; + while ((c = *p++) < NOTACHAR) + { +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 + if (utf) + { + pcre_uchar buff[6]; + (void)PRIV(ord2utf)(c, buff); + c = buff[0]; + } +#endif + if (c > 0xff) SET_BIT(0xff); else SET_BIT(c); + } + } + try_next = FALSE; + break; + + /* We can ignore word boundary tests. */ + + case OP_WORD_BOUNDARY: + case OP_NOT_WORD_BOUNDARY: + tcode++; + break; + + /* If we hit a bracket or a positive lookahead assertion, recurse to set + bits from within the subpattern. If it can't find anything, we have to + give up. If it finds some mandatory character(s), we are done for this + branch. Otherwise, carry on scanning after the subpattern. */ + + case OP_BRA: + case OP_SBRA: + case OP_CBRA: + case OP_SCBRA: + case OP_BRAPOS: + case OP_SBRAPOS: + case OP_CBRAPOS: + case OP_SCBRAPOS: + case OP_ONCE: + case OP_ONCE_NC: + case OP_ASSERT: + rc = set_start_bits(tcode, start_bits, utf, cd); + if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc; + if (rc == SSB_DONE) try_next = FALSE; else + { + do tcode += GET(tcode, 1); while (*tcode == OP_ALT); + tcode += 1 + LINK_SIZE; + } + break; + + /* If we hit ALT or KET, it means we haven't found anything mandatory in + this branch, though we might have found something optional. For ALT, we + continue with the next alternative, but we have to arrange that the final + result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET, + return SSB_CONTINUE: if this is the top level, that indicates failure, + but after a nested subpattern, it causes scanning to continue. */ + + case OP_ALT: + yield = SSB_CONTINUE; + try_next = FALSE; + break; + + case OP_KET: + case OP_KETRMAX: + case OP_KETRMIN: + case OP_KETRPOS: + return SSB_CONTINUE; + + /* Skip over callout */ + + case OP_CALLOUT: + tcode += 2 + 2*LINK_SIZE; + break; + + /* Skip over lookbehind and negative lookahead assertions */ + + case OP_ASSERT_NOT: + case OP_ASSERTBACK: + case OP_ASSERTBACK_NOT: + do tcode += GET(tcode, 1); while (*tcode == OP_ALT); + tcode += 1 + LINK_SIZE; + break; + + /* BRAZERO does the bracket, but carries on. */ + + case OP_BRAZERO: + case OP_BRAMINZERO: + case OP_BRAPOSZERO: + rc = set_start_bits(++tcode, start_bits, utf, cd); + if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc; +/* ========================================================================= + See the comment at the head of this function concerning the next line, + which was an old fudge for the benefit of OS/2. + dummy = 1; + ========================================================================= */ + do tcode += GET(tcode,1); while (*tcode == OP_ALT); + tcode += 1 + LINK_SIZE; + break; + + /* SKIPZERO skips the bracket. */ + + case OP_SKIPZERO: + tcode++; + do tcode += GET(tcode,1); while (*tcode == OP_ALT); + tcode += 1 + LINK_SIZE; + break; + + /* Single-char * or ? sets the bit and tries the next item */ + + case OP_STAR: + case OP_MINSTAR: + case OP_POSSTAR: + case OP_QUERY: + case OP_MINQUERY: + case OP_POSQUERY: + tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf); + break; + + case OP_STARI: + case OP_MINSTARI: + case OP_POSSTARI: + case OP_QUERYI: + case OP_MINQUERYI: + case OP_POSQUERYI: + tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf); + break; + + /* Single-char upto sets the bit and tries the next */ + + case OP_UPTO: + case OP_MINUPTO: + case OP_POSUPTO: + tcode = set_table_bit(start_bits, tcode + 1 + IMM2_SIZE, FALSE, cd, utf); + break; + + case OP_UPTOI: + case OP_MINUPTOI: + case OP_POSUPTOI: + tcode = set_table_bit(start_bits, tcode + 1 + IMM2_SIZE, TRUE, cd, utf); + break; + + /* At least one single char sets the bit and stops */ + + case OP_EXACT: + tcode += IMM2_SIZE; + /* Fall through */ + case OP_CHAR: + case OP_PLUS: + case OP_MINPLUS: + case OP_POSPLUS: + (void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf); + try_next = FALSE; + break; + + case OP_EXACTI: + tcode += IMM2_SIZE; + /* Fall through */ + case OP_CHARI: + case OP_PLUSI: + case OP_MINPLUSI: + case OP_POSPLUSI: + (void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf); + try_next = FALSE; + break; + + /* Special spacing and line-terminating items. These recognize specific + lists of characters. The difference between VSPACE and ANYNL is that the + latter can match the two-character CRLF sequence, but that is not + relevant for finding the first character, so their code here is + identical. */ + + case OP_HSPACE: + SET_BIT(CHAR_HT); + SET_BIT(CHAR_SPACE); +#ifdef SUPPORT_UTF + if (utf) + { +#ifdef COMPILE_PCRE8 + SET_BIT(0xC2); /* For U+00A0 */ + SET_BIT(0xE1); /* For U+1680, U+180E */ + SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */ + SET_BIT(0xE3); /* For U+3000 */ +#elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + SET_BIT(0xA0); + SET_BIT(0xFF); /* For characters > 255 */ +#endif /* COMPILE_PCRE[8|16|32] */ + } + else +#endif /* SUPPORT_UTF */ + { +#ifndef EBCDIC + SET_BIT(0xA0); +#endif /* Not EBCDIC */ +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + SET_BIT(0xFF); /* For characters > 255 */ +#endif /* COMPILE_PCRE[16|32] */ + } + try_next = FALSE; + break; + + case OP_ANYNL: + case OP_VSPACE: + SET_BIT(CHAR_LF); + SET_BIT(CHAR_VT); + SET_BIT(CHAR_FF); + SET_BIT(CHAR_CR); +#ifdef SUPPORT_UTF + if (utf) + { +#ifdef COMPILE_PCRE8 + SET_BIT(0xC2); /* For U+0085 */ + SET_BIT(0xE2); /* For U+2028, U+2029 */ +#elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + SET_BIT(CHAR_NEL); + SET_BIT(0xFF); /* For characters > 255 */ +#endif /* COMPILE_PCRE[8|16|32] */ + } + else +#endif /* SUPPORT_UTF */ + { + SET_BIT(CHAR_NEL); +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + SET_BIT(0xFF); /* For characters > 255 */ +#endif + } + try_next = FALSE; + break; + + /* Single character types set the bits and stop. Note that if PCRE_UCP + is set, we do not see these op codes because \d etc are converted to + properties. Therefore, these apply in the case when only characters less + than 256 are recognized to match the types. */ + + case OP_NOT_DIGIT: + set_nottype_bits(start_bits, cbit_digit, table_limit, cd); + try_next = FALSE; + break; + + case OP_DIGIT: + set_type_bits(start_bits, cbit_digit, table_limit, cd); + try_next = FALSE; + break; + + /* The cbit_space table has vertical tab as whitespace; we no longer + have to play fancy tricks because Perl added VT to its whitespace at + release 5.18. PCRE added it at release 8.34. */ + + case OP_NOT_WHITESPACE: + set_nottype_bits(start_bits, cbit_space, table_limit, cd); + try_next = FALSE; + break; + + case OP_WHITESPACE: + set_type_bits(start_bits, cbit_space, table_limit, cd); + try_next = FALSE; + break; + + case OP_NOT_WORDCHAR: + set_nottype_bits(start_bits, cbit_word, table_limit, cd); + try_next = FALSE; + break; + + case OP_WORDCHAR: + set_type_bits(start_bits, cbit_word, table_limit, cd); + try_next = FALSE; + break; + + /* One or more character type fudges the pointer and restarts, knowing + it will hit a single character type and stop there. */ + + case OP_TYPEPLUS: + case OP_TYPEMINPLUS: + case OP_TYPEPOSPLUS: + tcode++; + break; + + case OP_TYPEEXACT: + tcode += 1 + IMM2_SIZE; + break; + + /* Zero or more repeats of character types set the bits and then + try again. */ + + case OP_TYPEUPTO: + case OP_TYPEMINUPTO: + case OP_TYPEPOSUPTO: + tcode += IMM2_SIZE; /* Fall through */ + + case OP_TYPESTAR: + case OP_TYPEMINSTAR: + case OP_TYPEPOSSTAR: + case OP_TYPEQUERY: + case OP_TYPEMINQUERY: + case OP_TYPEPOSQUERY: + switch(tcode[1]) + { + default: + case OP_ANY: + case OP_ALLANY: + return SSB_FAIL; + + case OP_HSPACE: + SET_BIT(CHAR_HT); + SET_BIT(CHAR_SPACE); +#ifdef SUPPORT_UTF + if (utf) + { +#ifdef COMPILE_PCRE8 + SET_BIT(0xC2); /* For U+00A0 */ + SET_BIT(0xE1); /* For U+1680, U+180E */ + SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */ + SET_BIT(0xE3); /* For U+3000 */ +#elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + SET_BIT(0xA0); + SET_BIT(0xFF); /* For characters > 255 */ +#endif /* COMPILE_PCRE[8|16|32] */ + } + else +#endif /* SUPPORT_UTF */ +#ifndef EBCDIC + SET_BIT(0xA0); +#endif /* Not EBCDIC */ + break; + + case OP_ANYNL: + case OP_VSPACE: + SET_BIT(CHAR_LF); + SET_BIT(CHAR_VT); + SET_BIT(CHAR_FF); + SET_BIT(CHAR_CR); +#ifdef SUPPORT_UTF + if (utf) + { +#ifdef COMPILE_PCRE8 + SET_BIT(0xC2); /* For U+0085 */ + SET_BIT(0xE2); /* For U+2028, U+2029 */ +#elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + SET_BIT(CHAR_NEL); + SET_BIT(0xFF); /* For characters > 255 */ +#endif /* COMPILE_PCRE16 */ + } + else +#endif /* SUPPORT_UTF */ + SET_BIT(CHAR_NEL); + break; + + case OP_NOT_DIGIT: + set_nottype_bits(start_bits, cbit_digit, table_limit, cd); + break; + + case OP_DIGIT: + set_type_bits(start_bits, cbit_digit, table_limit, cd); + break; + + /* The cbit_space table has vertical tab as whitespace; we no longer + have to play fancy tricks because Perl added VT to its whitespace at + release 5.18. PCRE added it at release 8.34. */ + + case OP_NOT_WHITESPACE: + set_nottype_bits(start_bits, cbit_space, table_limit, cd); + break; + + case OP_WHITESPACE: + set_type_bits(start_bits, cbit_space, table_limit, cd); + break; + + case OP_NOT_WORDCHAR: + set_nottype_bits(start_bits, cbit_word, table_limit, cd); + break; + + case OP_WORDCHAR: + set_type_bits(start_bits, cbit_word, table_limit, cd); + break; + } + + tcode += 2; + break; + + /* Character class where all the information is in a bit map: set the + bits and either carry on or not, according to the repeat count. If it was + a negative class, and we are operating with UTF-8 characters, any byte + with a value >= 0xc4 is a potentially valid starter because it starts a + character with a value > 255. */ + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + case OP_XCLASS: + if ((tcode[1 + LINK_SIZE] & XCL_HASPROP) != 0) + return SSB_FAIL; + /* All bits are set. */ + if ((tcode[1 + LINK_SIZE] & XCL_MAP) == 0 && (tcode[1 + LINK_SIZE] & XCL_NOT) != 0) + return SSB_FAIL; +#endif + /* Fall through */ + + case OP_NCLASS: +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 + if (utf) + { + start_bits[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */ + memset(start_bits+25, 0xff, 7); /* Bits for 0xc9 - 0xff */ + } +#endif +#if defined COMPILE_PCRE16 || defined COMPILE_PCRE32 + SET_BIT(0xFF); /* For characters > 255 */ +#endif + /* Fall through */ + + case OP_CLASS: + { + pcre_uint8 *map; +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + map = NULL; + if (*tcode == OP_XCLASS) + { + if ((tcode[1 + LINK_SIZE] & XCL_MAP) != 0) + map = (pcre_uint8 *)(tcode + 1 + LINK_SIZE + 1); + tcode += GET(tcode, 1); + } + else +#endif + { + tcode++; + map = (pcre_uint8 *)tcode; + tcode += 32 / sizeof(pcre_uchar); + } + + /* In UTF-8 mode, the bits in a bit map correspond to character + values, not to byte values. However, the bit map we are constructing is + for byte values. So we have to do a conversion for characters whose + value is > 127. In fact, there are only two possible starting bytes for + characters in the range 128 - 255. */ + +#if defined SUPPORT_UTF || !defined COMPILE_PCRE8 + if (map != NULL) +#endif + { +#if defined SUPPORT_UTF && defined COMPILE_PCRE8 + if (utf) + { + for (c = 0; c < 16; c++) start_bits[c] |= map[c]; + for (c = 128; c < 256; c++) + { + if ((map[c/8] & (1 << (c&7))) != 0) + { + int d = (c >> 6) | 0xc0; /* Set bit for this starter */ + start_bits[d/8] |= (1 << (d&7)); /* and then skip on to the */ + c = (c & 0xc0) + 0x40 - 1; /* next relevant character. */ + } + } + } + else +#endif + { + /* In non-UTF-8 mode, the two bit maps are completely compatible. */ + for (c = 0; c < 32; c++) start_bits[c] |= map[c]; + } + } + + /* Advance past the bit map, and act on what follows. For a zero + minimum repeat, continue; otherwise stop processing. */ + + switch (*tcode) + { + case OP_CRSTAR: + case OP_CRMINSTAR: + case OP_CRQUERY: + case OP_CRMINQUERY: + case OP_CRPOSSTAR: + case OP_CRPOSQUERY: + tcode++; + break; + + case OP_CRRANGE: + case OP_CRMINRANGE: + case OP_CRPOSRANGE: + if (GET2(tcode, 1) == 0) tcode += 1 + 2 * IMM2_SIZE; + else try_next = FALSE; + break; + + default: + try_next = FALSE; + break; + } + } + break; /* End of bitmap class handling */ + + } /* End of switch */ + } /* End of try_next loop */ + + code += GET(code, 1); /* Advance to next branch */ + } +while (*code == OP_ALT); +return yield; +} + + + + + +/************************************************* +* Study a compiled expression * +*************************************************/ + +/* This function is handed a compiled expression that it must study to produce +information that will speed up the matching. It returns a pcre[16]_extra block +which then gets handed back to pcre_exec(). + +Arguments: + re points to the compiled expression + options contains option bits + errorptr points to where to place error messages; + set NULL unless error + +Returns: pointer to a pcre[16]_extra block, with study_data filled in and + the appropriate flags set; + NULL on error or if no optimization possible +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION +pcre_study(const pcre *external_re, int options, const char **errorptr) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN pcre16_extra * PCRE_CALL_CONVENTION +pcre16_study(const pcre16 *external_re, int options, const char **errorptr) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN pcre32_extra * PCRE_CALL_CONVENTION +pcre32_study(const pcre32 *external_re, int options, const char **errorptr) +#endif +{ +int min; +int count = 0; +BOOL bits_set = FALSE; +pcre_uint8 start_bits[32]; +PUBL(extra) *extra = NULL; +pcre_study_data *study; +const pcre_uint8 *tables; +pcre_uchar *code; +compile_data compile_block; +const REAL_PCRE *re = (const REAL_PCRE *)external_re; + + +*errorptr = NULL; + +if (re == NULL || re->magic_number != MAGIC_NUMBER) + { + *errorptr = "argument is not a compiled regular expression"; + return NULL; + } + +if ((re->flags & PCRE_MODE) == 0) + { +#if defined COMPILE_PCRE8 + *errorptr = "argument not compiled in 8 bit mode"; +#elif defined COMPILE_PCRE16 + *errorptr = "argument not compiled in 16 bit mode"; +#elif defined COMPILE_PCRE32 + *errorptr = "argument not compiled in 32 bit mode"; +#endif + return NULL; + } + +if ((options & ~PUBLIC_STUDY_OPTIONS) != 0) + { + *errorptr = "unknown or incorrect option bit(s) set"; + return NULL; + } + +code = (pcre_uchar *)re + re->name_table_offset + + (re->name_count * re->name_entry_size); + +/* For an anchored pattern, or an unanchored pattern that has a first char, or +a multiline pattern that matches only at "line starts", there is no point in +seeking a list of starting bytes. */ + +if ((re->options & PCRE_ANCHORED) == 0 && + (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0) + { + int rc; + + /* Set the character tables in the block that is passed around */ + + tables = re->tables; + +#if defined COMPILE_PCRE8 + if (tables == NULL) + (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES, + (void *)(&tables)); +#elif defined COMPILE_PCRE16 + if (tables == NULL) + (void)pcre16_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES, + (void *)(&tables)); +#elif defined COMPILE_PCRE32 + if (tables == NULL) + (void)pcre32_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES, + (void *)(&tables)); +#endif + + compile_block.lcc = tables + lcc_offset; + compile_block.fcc = tables + fcc_offset; + compile_block.cbits = tables + cbits_offset; + compile_block.ctypes = tables + ctypes_offset; + + /* See if we can find a fixed set of initial characters for the pattern. */ + + memset(start_bits, 0, 32 * sizeof(pcre_uint8)); + rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0, + &compile_block); + bits_set = rc == SSB_DONE; + if (rc == SSB_UNKNOWN) + { + *errorptr = "internal error: opcode not recognized"; + return NULL; + } + } + +/* Find the minimum length of subject string. */ + +switch(min = find_minlength(re, code, code, re->options, NULL, &count)) + { + case -2: *errorptr = "internal error: missing capturing bracket"; return NULL; + case -3: *errorptr = "internal error: opcode not recognized"; return NULL; + default: break; + } + +/* If a set of starting bytes has been identified, or if the minimum length is +greater than zero, or if JIT optimization has been requested, or if +PCRE_STUDY_EXTRA_NEEDED is set, get a pcre[16]_extra block and a +pcre_study_data block. The study data is put in the latter, which is pointed to +by the former, which may also get additional data set later by the calling +program. At the moment, the size of pcre_study_data is fixed. We nevertheless +save it in a field for returning via the pcre_fullinfo() function so that if it +becomes variable in the future, we don't have to change that code. */ + +if (bits_set || min > 0 || (options & ( +#ifdef SUPPORT_JIT + PCRE_STUDY_JIT_COMPILE | PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE | + PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE | +#endif + PCRE_STUDY_EXTRA_NEEDED)) != 0) + { + extra = (PUBL(extra) *)(PUBL(malloc)) + (sizeof(PUBL(extra)) + sizeof(pcre_study_data)); + if (extra == NULL) + { + *errorptr = "failed to get memory"; + return NULL; + } + + study = (pcre_study_data *)((char *)extra + sizeof(PUBL(extra))); + extra->flags = PCRE_EXTRA_STUDY_DATA; + extra->study_data = study; + + study->size = sizeof(pcre_study_data); + study->flags = 0; + + /* Set the start bits always, to avoid unset memory errors if the + study data is written to a file, but set the flag only if any of the bits + are set, to save time looking when none are. */ + + if (bits_set) + { + study->flags |= PCRE_STUDY_MAPPED; + memcpy(study->start_bits, start_bits, sizeof(start_bits)); + } + else memset(study->start_bits, 0, 32 * sizeof(pcre_uint8)); + +#ifdef PCRE_DEBUG + if (bits_set) + { + pcre_uint8 *ptr = start_bits; + int i; + + printf("Start bits:\n"); + for (i = 0; i < 32; i++) + printf("%3d: %02x%s", i * 8, *ptr++, ((i + 1) & 0x7) != 0? " " : "\n"); + } +#endif + + /* Always set the minlength value in the block, because the JIT compiler + makes use of it. However, don't set the bit unless the length is greater than + zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time + checking the zero case. */ + + if (min > 0) + { + study->flags |= PCRE_STUDY_MINLEN; + study->minlength = min; + } + else study->minlength = 0; + + /* If JIT support was compiled and requested, attempt the JIT compilation. + If no starting bytes were found, and the minimum length is zero, and JIT + compilation fails, abandon the extra block and return NULL, unless + PCRE_STUDY_EXTRA_NEEDED is set. */ + +#ifdef SUPPORT_JIT + extra->executable_jit = NULL; + if ((options & PCRE_STUDY_JIT_COMPILE) != 0) + PRIV(jit_compile)(re, extra, JIT_COMPILE); + if ((options & PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE) != 0) + PRIV(jit_compile)(re, extra, JIT_PARTIAL_SOFT_COMPILE); + if ((options & PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE) != 0) + PRIV(jit_compile)(re, extra, JIT_PARTIAL_HARD_COMPILE); + + if (study->flags == 0 && (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) == 0 && + (options & PCRE_STUDY_EXTRA_NEEDED) == 0) + { +#if defined COMPILE_PCRE8 + pcre_free_study(extra); +#elif defined COMPILE_PCRE16 + pcre16_free_study(extra); +#elif defined COMPILE_PCRE32 + pcre32_free_study(extra); +#endif + extra = NULL; + } +#endif + } + +return extra; +} + + +/************************************************* +* Free the study data * +*************************************************/ + +/* This function frees the memory that was obtained by pcre_study(). + +Argument: a pointer to the pcre[16]_extra block +Returns: nothing +*/ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN void +pcre_free_study(pcre_extra *extra) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN void +pcre16_free_study(pcre16_extra *extra) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN void +pcre32_free_study(pcre32_extra *extra) +#endif +{ +if (extra == NULL) + return; +#ifdef SUPPORT_JIT +if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 && + extra->executable_jit != NULL) + PRIV(jit_free)(extra->executable_jit); +#endif +PUBL(free)(extra); +} + +/* End of pcre_study.c */ diff --git a/deps/pcre/pcre_tables.c b/deps/pcre/pcre_tables.c new file mode 100644 index 00000000000..5e18e8cf904 --- /dev/null +++ b/deps/pcre/pcre_tables.c @@ -0,0 +1,727 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2017 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + +#ifndef PCRE_INCLUDED + +/* This module contains some fixed tables that are used by more than one of the +PCRE code modules. The tables are also #included by the pcretest program, which +uses macros to change their names from _pcre_xxx to xxxx, thereby avoiding name +clashes with the library. */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pcre_internal.h" + +#endif /* PCRE_INCLUDED */ + +/* Table of sizes for the fixed-length opcodes. It's defined in a macro so that +the definition is next to the definition of the opcodes in pcre_internal.h. */ + +const pcre_uint8 PRIV(OP_lengths)[] = { OP_LENGTHS }; + +/* Tables of horizontal and vertical whitespace characters, suitable for +adding to classes. */ + +const pcre_uint32 PRIV(hspace_list)[] = { HSPACE_LIST }; +const pcre_uint32 PRIV(vspace_list)[] = { VSPACE_LIST }; + + + +/************************************************* +* Tables for UTF-8 support * +*************************************************/ + +/* These are the breakpoints for different numbers of bytes in a UTF-8 +character. */ + +#if (defined SUPPORT_UTF && defined COMPILE_PCRE8) \ + || (defined PCRE_INCLUDED && (defined SUPPORT_PCRE16 || defined SUPPORT_PCRE32)) + +/* These tables are also required by pcretest in 16- or 32-bit mode. */ + +const int PRIV(utf8_table1)[] = + { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff}; + +const int PRIV(utf8_table1_size) = sizeof(PRIV(utf8_table1)) / sizeof(int); + +/* These are the indicator bits and the mask for the data bits to set in the +first byte of a character, indexed by the number of additional bytes. */ + +const int PRIV(utf8_table2)[] = { 0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc}; +const int PRIV(utf8_table3)[] = { 0xff, 0x1f, 0x0f, 0x07, 0x03, 0x01}; + +/* Table of the number of extra bytes, indexed by the first byte masked with +0x3f. The highest number for a valid UTF-8 first byte is in fact 0x3d. */ + +const pcre_uint8 PRIV(utf8_table4)[] = { + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, + 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5 }; + +#endif /* (SUPPORT_UTF && COMPILE_PCRE8) || (PCRE_INCLUDED && SUPPORT_PCRE[16|32])*/ + +#ifdef SUPPORT_UTF + +/* Table to translate from particular type value to the general value. */ + +const pcre_uint32 PRIV(ucp_gentype)[] = { + ucp_C, ucp_C, ucp_C, ucp_C, ucp_C, /* Cc, Cf, Cn, Co, Cs */ + ucp_L, ucp_L, ucp_L, ucp_L, ucp_L, /* Ll, Lu, Lm, Lo, Lt */ + ucp_M, ucp_M, ucp_M, /* Mc, Me, Mn */ + ucp_N, ucp_N, ucp_N, /* Nd, Nl, No */ + ucp_P, ucp_P, ucp_P, ucp_P, ucp_P, /* Pc, Pd, Pe, Pf, Pi */ + ucp_P, ucp_P, /* Ps, Po */ + ucp_S, ucp_S, ucp_S, ucp_S, /* Sc, Sk, Sm, So */ + ucp_Z, ucp_Z, ucp_Z /* Zl, Zp, Zs */ +}; + +/* This table encodes the rules for finding the end of an extended grapheme +cluster. Every code point has a grapheme break property which is one of the +ucp_gbXX values defined in ucp.h. The 2-dimensional table is indexed by the +properties of two adjacent code points. The left property selects a word from +the table, and the right property selects a bit from that word like this: + + ucp_gbtable[left-property] & (1 << right-property) + +The value is non-zero if a grapheme break is NOT permitted between the relevant +two code points. The breaking rules are as follows: + +1. Break at the start and end of text (pretty obviously). + +2. Do not break between a CR and LF; otherwise, break before and after + controls. + +3. Do not break Hangul syllable sequences, the rules for which are: + + L may be followed by L, V, LV or LVT + LV or V may be followed by V or T + LVT or T may be followed by T + +4. Do not break before extending characters. + +The next two rules are only for extended grapheme clusters (but that's what we +are implementing). + +5. Do not break before SpacingMarks. + +6. Do not break after Prepend characters. + +7. Otherwise, break everywhere. +*/ + +const pcre_uint32 PRIV(ucp_gbtable[]) = { + (1< 0x10ffff is not permitted +PCRE_UTF8_ERR14 3-byte character with value 0xd000-0xdfff is not permitted +PCRE_UTF8_ERR15 Overlong 2-byte sequence +PCRE_UTF8_ERR16 Overlong 3-byte sequence +PCRE_UTF8_ERR17 Overlong 4-byte sequence +PCRE_UTF8_ERR18 Overlong 5-byte sequence (won't ever occur) +PCRE_UTF8_ERR19 Overlong 6-byte sequence (won't ever occur) +PCRE_UTF8_ERR20 Isolated 0x80 byte (not within UTF-8 character) +PCRE_UTF8_ERR21 Byte with the illegal value 0xfe or 0xff +PCRE_UTF8_ERR22 Unused (was non-character) + +Arguments: + string points to the string + length length of string, or -1 if the string is zero-terminated + errp pointer to an error position offset variable + +Returns: = 0 if the string is a valid UTF-8 string + > 0 otherwise, setting the offset of the bad character +*/ + +int +PRIV(valid_utf)(PCRE_PUCHAR string, int length, int *erroroffset) +{ +#ifdef SUPPORT_UTF +register PCRE_PUCHAR p; + +if (length < 0) + { + for (p = string; *p != 0; p++); + length = (int)(p - string); + } + +for (p = string; length-- > 0; p++) + { + register pcre_uchar ab, c, d; + + c = *p; + if (c < 128) continue; /* ASCII character */ + + if (c < 0xc0) /* Isolated 10xx xxxx byte */ + { + *erroroffset = (int)(p - string); + return PCRE_UTF8_ERR20; + } + + if (c >= 0xfe) /* Invalid 0xfe or 0xff bytes */ + { + *erroroffset = (int)(p - string); + return PCRE_UTF8_ERR21; + } + + ab = PRIV(utf8_table4)[c & 0x3f]; /* Number of additional bytes */ + if (length < ab) + { + *erroroffset = (int)(p - string); /* Missing bytes */ + return ab - length; /* Codes ERR1 to ERR5 */ + } + length -= ab; /* Length remaining */ + + /* Check top bits in the second byte */ + + if (((d = *(++p)) & 0xc0) != 0x80) + { + *erroroffset = (int)(p - string) - 1; + return PCRE_UTF8_ERR6; + } + + /* For each length, check that the remaining bytes start with the 0x80 bit + set and not the 0x40 bit. Then check for an overlong sequence, and for the + excluded range 0xd800 to 0xdfff. */ + + switch (ab) + { + /* 2-byte character. No further bytes to check for 0x80. Check first byte + for for xx00 000x (overlong sequence). */ + + case 1: if ((c & 0x3e) == 0) + { + *erroroffset = (int)(p - string) - 1; + return PCRE_UTF8_ERR15; + } + break; + + /* 3-byte character. Check third byte for 0x80. Then check first 2 bytes + for 1110 0000, xx0x xxxx (overlong sequence) or + 1110 1101, 1010 xxxx (0xd800 - 0xdfff) */ + + case 2: + if ((*(++p) & 0xc0) != 0x80) /* Third byte */ + { + *erroroffset = (int)(p - string) - 2; + return PCRE_UTF8_ERR7; + } + if (c == 0xe0 && (d & 0x20) == 0) + { + *erroroffset = (int)(p - string) - 2; + return PCRE_UTF8_ERR16; + } + if (c == 0xed && d >= 0xa0) + { + *erroroffset = (int)(p - string) - 2; + return PCRE_UTF8_ERR14; + } + break; + + /* 4-byte character. Check 3rd and 4th bytes for 0x80. Then check first 2 + bytes for for 1111 0000, xx00 xxxx (overlong sequence), then check for a + character greater than 0x0010ffff (f4 8f bf bf) */ + + case 3: + if ((*(++p) & 0xc0) != 0x80) /* Third byte */ + { + *erroroffset = (int)(p - string) - 2; + return PCRE_UTF8_ERR7; + } + if ((*(++p) & 0xc0) != 0x80) /* Fourth byte */ + { + *erroroffset = (int)(p - string) - 3; + return PCRE_UTF8_ERR8; + } + if (c == 0xf0 && (d & 0x30) == 0) + { + *erroroffset = (int)(p - string) - 3; + return PCRE_UTF8_ERR17; + } + if (c > 0xf4 || (c == 0xf4 && d > 0x8f)) + { + *erroroffset = (int)(p - string) - 3; + return PCRE_UTF8_ERR13; + } + break; + + /* 5-byte and 6-byte characters are not allowed by RFC 3629, and will be + rejected by the length test below. However, we do the appropriate tests + here so that overlong sequences get diagnosed, and also in case there is + ever an option for handling these larger code points. */ + + /* 5-byte character. Check 3rd, 4th, and 5th bytes for 0x80. Then check for + 1111 1000, xx00 0xxx */ + + case 4: + if ((*(++p) & 0xc0) != 0x80) /* Third byte */ + { + *erroroffset = (int)(p - string) - 2; + return PCRE_UTF8_ERR7; + } + if ((*(++p) & 0xc0) != 0x80) /* Fourth byte */ + { + *erroroffset = (int)(p - string) - 3; + return PCRE_UTF8_ERR8; + } + if ((*(++p) & 0xc0) != 0x80) /* Fifth byte */ + { + *erroroffset = (int)(p - string) - 4; + return PCRE_UTF8_ERR9; + } + if (c == 0xf8 && (d & 0x38) == 0) + { + *erroroffset = (int)(p - string) - 4; + return PCRE_UTF8_ERR18; + } + break; + + /* 6-byte character. Check 3rd-6th bytes for 0x80. Then check for + 1111 1100, xx00 00xx. */ + + case 5: + if ((*(++p) & 0xc0) != 0x80) /* Third byte */ + { + *erroroffset = (int)(p - string) - 2; + return PCRE_UTF8_ERR7; + } + if ((*(++p) & 0xc0) != 0x80) /* Fourth byte */ + { + *erroroffset = (int)(p - string) - 3; + return PCRE_UTF8_ERR8; + } + if ((*(++p) & 0xc0) != 0x80) /* Fifth byte */ + { + *erroroffset = (int)(p - string) - 4; + return PCRE_UTF8_ERR9; + } + if ((*(++p) & 0xc0) != 0x80) /* Sixth byte */ + { + *erroroffset = (int)(p - string) - 5; + return PCRE_UTF8_ERR10; + } + if (c == 0xfc && (d & 0x3c) == 0) + { + *erroroffset = (int)(p - string) - 5; + return PCRE_UTF8_ERR19; + } + break; + } + + /* Character is valid under RFC 2279, but 4-byte and 5-byte characters are + excluded by RFC 3629. The pointer p is currently at the last byte of the + character. */ + + if (ab > 3) + { + *erroroffset = (int)(p - string) - ab; + return (ab == 4)? PCRE_UTF8_ERR11 : PCRE_UTF8_ERR12; + } + } + +#else /* Not SUPPORT_UTF */ +(void)(string); /* Keep picky compilers happy */ +(void)(length); +(void)(erroroffset); +#endif + +return PCRE_UTF8_ERR0; /* This indicates success */ +} + +/* End of pcre_valid_utf8.c */ diff --git a/deps/pcre/pcre_version.c b/deps/pcre/pcre_version.c new file mode 100644 index 00000000000..ae86ff28bc8 --- /dev/null +++ b/deps/pcre/pcre_version.c @@ -0,0 +1,98 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2012 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains the external function pcre_version(), which returns a +string that identifies the PCRE version that is in use. */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pcre_internal.h" + + +/************************************************* +* Return version string * +*************************************************/ + +/* These macros are the standard way of turning unquoted text into C strings. +They allow macros like PCRE_MAJOR to be defined without quotes, which is +convenient for user programs that want to test its value. */ + +#define STRING(a) # a +#define XSTRING(s) STRING(s) + +/* A problem turned up with PCRE_PRERELEASE, which is defined empty for +production releases. Originally, it was used naively in this code: + + return XSTRING(PCRE_MAJOR) + "." XSTRING(PCRE_MINOR) + XSTRING(PCRE_PRERELEASE) + " " XSTRING(PCRE_DATE); + +However, when PCRE_PRERELEASE is empty, this leads to an attempted expansion of +STRING(). The C standard states: "If (before argument substitution) any +argument consists of no preprocessing tokens, the behavior is undefined." It +turns out the gcc treats this case as a single empty string - which is what we +really want - but Visual C grumbles about the lack of an argument for the +macro. Unfortunately, both are within their rights. To cope with both ways of +handling this, I had resort to some messy hackery that does a test at run time. +I could find no way of detecting that a macro is defined as an empty string at +pre-processor time. This hack uses a standard trick for avoiding calling +the STRING macro with an empty argument when doing the test. */ + +#if defined COMPILE_PCRE8 +PCRE_EXP_DEFN const char * PCRE_CALL_CONVENTION +pcre_version(void) +#elif defined COMPILE_PCRE16 +PCRE_EXP_DEFN const char * PCRE_CALL_CONVENTION +pcre16_version(void) +#elif defined COMPILE_PCRE32 +PCRE_EXP_DEFN const char * PCRE_CALL_CONVENTION +pcre32_version(void) +#endif +{ +return (XSTRING(Z PCRE_PRERELEASE)[1] == 0)? + XSTRING(PCRE_MAJOR.PCRE_MINOR PCRE_DATE) : + XSTRING(PCRE_MAJOR.PCRE_MINOR) XSTRING(PCRE_PRERELEASE PCRE_DATE); +} + +/* End of pcre_version.c */ diff --git a/deps/pcre/pcre_xclass.c b/deps/pcre/pcre_xclass.c new file mode 100644 index 00000000000..ef759a589a6 --- /dev/null +++ b/deps/pcre/pcre_xclass.c @@ -0,0 +1,268 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2013 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module contains an internal function that is used to match an extended +class. It is used by both pcre_exec() and pcre_def_exec(). */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "pcre_internal.h" + + +/************************************************* +* Match character against an XCLASS * +*************************************************/ + +/* This function is called to match a character against an extended class that +might contain values > 255 and/or Unicode properties. + +Arguments: + c the character + data points to the flag byte of the XCLASS data + +Returns: TRUE if character matches, else FALSE +*/ + +BOOL +PRIV(xclass)(pcre_uint32 c, const pcre_uchar *data, BOOL utf) +{ +pcre_uchar t; +BOOL negated = (*data & XCL_NOT) != 0; + +(void)utf; +#ifdef COMPILE_PCRE8 +/* In 8 bit mode, this must always be TRUE. Help the compiler to know that. */ +utf = TRUE; +#endif + +/* Character values < 256 are matched against a bitmap, if one is present. If +not, we still carry on, because there may be ranges that start below 256 in the +additional data. */ + +if (c < 256) + { + if ((*data & XCL_HASPROP) == 0) + { + if ((*data & XCL_MAP) == 0) return negated; + return (((pcre_uint8 *)(data + 1))[c/8] & (1 << (c&7))) != 0; + } + if ((*data & XCL_MAP) != 0 && + (((pcre_uint8 *)(data + 1))[c/8] & (1 << (c&7))) != 0) + return !negated; /* char found */ + } + +/* First skip the bit map if present. Then match against the list of Unicode +properties or large chars or ranges that end with a large char. We won't ever +encounter XCL_PROP or XCL_NOTPROP when UCP support is not compiled. */ + +if ((*data++ & XCL_MAP) != 0) data += 32 / sizeof(pcre_uchar); + +while ((t = *data++) != XCL_END) + { + pcre_uint32 x, y; + if (t == XCL_SINGLE) + { +#ifdef SUPPORT_UTF + if (utf) + { + GETCHARINC(x, data); /* macro generates multiple statements */ + } + else +#endif + x = *data++; + if (c == x) return !negated; + } + else if (t == XCL_RANGE) + { +#ifdef SUPPORT_UTF + if (utf) + { + GETCHARINC(x, data); /* macro generates multiple statements */ + GETCHARINC(y, data); /* macro generates multiple statements */ + } + else +#endif + { + x = *data++; + y = *data++; + } + if (c >= x && c <= y) return !negated; + } + +#ifdef SUPPORT_UCP + else /* XCL_PROP & XCL_NOTPROP */ + { + const ucd_record *prop = GET_UCD(c); + BOOL isprop = t == XCL_PROP; + + switch(*data) + { + case PT_ANY: + if (isprop) return !negated; + break; + + case PT_LAMP: + if ((prop->chartype == ucp_Lu || prop->chartype == ucp_Ll || + prop->chartype == ucp_Lt) == isprop) return !negated; + break; + + case PT_GC: + if ((data[1] == PRIV(ucp_gentype)[prop->chartype]) == isprop) + return !negated; + break; + + case PT_PC: + if ((data[1] == prop->chartype) == isprop) return !negated; + break; + + case PT_SC: + if ((data[1] == prop->script) == isprop) return !negated; + break; + + case PT_ALNUM: + if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N) == isprop) + return !negated; + break; + + /* Perl space used to exclude VT, but from Perl 5.18 it is included, + which means that Perl space and POSIX space are now identical. PCRE + was changed at release 8.34. */ + + case PT_SPACE: /* Perl space */ + case PT_PXSPACE: /* POSIX space */ + switch(c) + { + HSPACE_CASES: + VSPACE_CASES: + if (isprop) return !negated; + break; + + default: + if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == isprop) + return !negated; + break; + } + break; + + case PT_WORD: + if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L || + PRIV(ucp_gentype)[prop->chartype] == ucp_N || c == CHAR_UNDERSCORE) + == isprop) + return !negated; + break; + + case PT_UCNC: + if (c < 0xa0) + { + if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT || + c == CHAR_GRAVE_ACCENT) == isprop) + return !negated; + } + else + { + if ((c < 0xd800 || c > 0xdfff) == isprop) + return !negated; + } + break; + + /* The following three properties can occur only in an XCLASS, as there + is no \p or \P coding for them. */ + + /* Graphic character. Implement this as not Z (space or separator) and + not C (other), except for Cf (format) with a few exceptions. This seems + to be what Perl does. The exceptional characters are: + + U+061C Arabic Letter Mark + U+180E Mongolian Vowel Separator + U+2066 - U+2069 Various "isolate"s + */ + + case PT_PXGRAPH: + if ((PRIV(ucp_gentype)[prop->chartype] != ucp_Z && + (PRIV(ucp_gentype)[prop->chartype] != ucp_C || + (prop->chartype == ucp_Cf && + c != 0x061c && c != 0x180e && (c < 0x2066 || c > 0x2069)) + )) == isprop) + return !negated; + break; + + /* Printable character: same as graphic, with the addition of Zs, i.e. + not Zl and not Zp, and U+180E. */ + + case PT_PXPRINT: + if ((prop->chartype != ucp_Zl && + prop->chartype != ucp_Zp && + (PRIV(ucp_gentype)[prop->chartype] != ucp_C || + (prop->chartype == ucp_Cf && + c != 0x061c && (c < 0x2066 || c > 0x2069)) + )) == isprop) + return !negated; + break; + + /* Punctuation: all Unicode punctuation, plus ASCII characters that + Unicode treats as symbols rather than punctuation, for Perl + compatibility (these are $+<=>^`|~). */ + + case PT_PXPUNCT: + if ((PRIV(ucp_gentype)[prop->chartype] == ucp_P || + (c < 128 && PRIV(ucp_gentype)[prop->chartype] == ucp_S)) == isprop) + return !negated; + break; + + /* This should never occur, but compilers may mutter if there is no + default. */ + + default: + return FALSE; + } + + data += 2; + } +#endif /* SUPPORT_UCP */ + } + +return negated; /* char did not match */ +} + +/* End of pcre_xclass.c */ diff --git a/deps/pcre/pcreposix.c b/deps/pcre/pcreposix.c new file mode 100644 index 00000000000..f9e423b316f --- /dev/null +++ b/deps/pcre/pcreposix.c @@ -0,0 +1,420 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +/* PCRE is a library of functions to support regular expressions whose syntax +and semantics are as close as possible to those of the Perl 5 language. + + Written by Philip Hazel + Copyright (c) 1997-2020 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + + +/* This module is a wrapper that provides a POSIX API to the underlying PCRE +functions. */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +/* We include pcre.h before pcre_internal.h so that the PCRE library functions +are declared as "import" for Windows by defining PCRE_EXP_DECL as "import". +This is needed even though pcre_internal.h itself includes pcre.h, because it +does so after it has set PCRE_EXP_DECL to "export" if it is not already set. */ + +#include "pcre.h" +#include "pcre_internal.h" +#include "pcreposix.h" + + +/* Table to translate PCRE compile time error codes into POSIX error codes. */ + +static const int eint[] = { + 0, /* no error */ + PCRE_REG_EESCAPE, /* \ at end of pattern */ + PCRE_REG_EESCAPE, /* \c at end of pattern */ + PCRE_REG_EESCAPE, /* unrecognized character follows \ */ + PCRE_REG_BADBR, /* numbers out of order in {} quantifier */ + /* 5 */ + PCRE_REG_BADBR, /* number too big in {} quantifier */ + PCRE_REG_EBRACK, /* missing terminating ] for character class */ + PCRE_REG_ECTYPE, /* invalid escape sequence in character class */ + PCRE_REG_ERANGE, /* range out of order in character class */ + PCRE_REG_BADRPT, /* nothing to repeat */ + /* 10 */ + PCRE_REG_BADRPT, /* operand of unlimited repeat could match the empty string */ + PCRE_REG_ASSERT, /* internal error: unexpected repeat */ + PCRE_REG_BADPAT, /* unrecognized character after (? */ + PCRE_REG_BADPAT, /* POSIX named classes are supported only within a class */ + PCRE_REG_EPAREN, /* missing ) */ + /* 15 */ + PCRE_REG_ESUBREG, /* reference to non-existent subpattern */ + PCRE_REG_INVARG, /* erroffset passed as NULL */ + PCRE_REG_INVARG, /* unknown option bit(s) set */ + PCRE_REG_EPAREN, /* missing ) after comment */ + PCRE_REG_ESIZE, /* parentheses nested too deeply */ + /* 20 */ + PCRE_REG_ESIZE, /* regular expression too large */ + PCRE_REG_ESPACE, /* failed to get memory */ + PCRE_REG_EPAREN, /* unmatched parentheses */ + PCRE_REG_ASSERT, /* internal error: code overflow */ + PCRE_REG_BADPAT, /* unrecognized character after (?< */ + /* 25 */ + PCRE_REG_BADPAT, /* lookbehind assertion is not fixed length */ + PCRE_REG_BADPAT, /* malformed number or name after (?( */ + PCRE_REG_BADPAT, /* conditional group contains more than two branches */ + PCRE_REG_BADPAT, /* assertion expected after (?( */ + PCRE_REG_BADPAT, /* (?R or (?[+-]digits must be followed by ) */ + /* 30 */ + PCRE_REG_ECTYPE, /* unknown POSIX class name */ + PCRE_REG_BADPAT, /* POSIX collating elements are not supported */ + PCRE_REG_INVARG, /* this version of PCRE is not compiled with PCRE_UTF8 support */ + PCRE_REG_BADPAT, /* spare error */ + PCRE_REG_BADPAT, /* character value in \x{} or \o{} is too large */ + /* 35 */ + PCRE_REG_BADPAT, /* invalid condition (?(0) */ + PCRE_REG_BADPAT, /* \C not allowed in lookbehind assertion */ + PCRE_REG_EESCAPE, /* PCRE does not support \L, \l, \N, \U, or \u */ + PCRE_REG_BADPAT, /* number after (?C is > 255 */ + PCRE_REG_BADPAT, /* closing ) for (?C expected */ + /* 40 */ + PCRE_REG_BADPAT, /* recursive call could loop indefinitely */ + PCRE_REG_BADPAT, /* unrecognized character after (?P */ + PCRE_REG_BADPAT, /* syntax error in subpattern name (missing terminator) */ + PCRE_REG_BADPAT, /* two named subpatterns have the same name */ + PCRE_REG_BADPAT, /* invalid UTF-8 string */ + /* 45 */ + PCRE_REG_BADPAT, /* support for \P, \p, and \X has not been compiled */ + PCRE_REG_BADPAT, /* malformed \P or \p sequence */ + PCRE_REG_BADPAT, /* unknown property name after \P or \p */ + PCRE_REG_BADPAT, /* subpattern name is too long (maximum 32 characters) */ + PCRE_REG_BADPAT, /* too many named subpatterns (maximum 10,000) */ + /* 50 */ + PCRE_REG_BADPAT, /* repeated subpattern is too long */ + PCRE_REG_BADPAT, /* octal value is greater than \377 (not in UTF-8 mode) */ + PCRE_REG_BADPAT, /* internal error: overran compiling workspace */ + PCRE_REG_BADPAT, /* internal error: previously-checked referenced subpattern not found */ + PCRE_REG_BADPAT, /* DEFINE group contains more than one branch */ + /* 55 */ + PCRE_REG_BADPAT, /* repeating a DEFINE group is not allowed */ + PCRE_REG_INVARG, /* inconsistent NEWLINE options */ + PCRE_REG_BADPAT, /* \g is not followed followed by an (optionally braced) non-zero number */ + PCRE_REG_BADPAT, /* a numbered reference must not be zero */ + PCRE_REG_BADPAT, /* an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT) */ + /* 60 */ + PCRE_REG_BADPAT, /* (*VERB) not recognized */ + PCRE_REG_BADPAT, /* number is too big */ + PCRE_REG_BADPAT, /* subpattern name expected */ + PCRE_REG_BADPAT, /* digit expected after (?+ */ + PCRE_REG_BADPAT, /* ] is an invalid data character in JavaScript compatibility mode */ + /* 65 */ + PCRE_REG_BADPAT, /* different names for subpatterns of the same number are not allowed */ + PCRE_REG_BADPAT, /* (*MARK) must have an argument */ + PCRE_REG_INVARG, /* this version of PCRE is not compiled with PCRE_UCP support */ + PCRE_REG_BADPAT, /* \c must be followed by an ASCII character */ + PCRE_REG_BADPAT, /* \k is not followed by a braced, angle-bracketed, or quoted name */ + /* 70 */ + PCRE_REG_BADPAT, /* internal error: unknown opcode in find_fixedlength() */ + PCRE_REG_BADPAT, /* \N is not supported in a class */ + PCRE_REG_BADPAT, /* too many forward references */ + PCRE_REG_BADPAT, /* disallowed UTF-8/16/32 code point (>= 0xd800 && <= 0xdfff) */ + PCRE_REG_BADPAT, /* invalid UTF-16 string (should not occur) */ + /* 75 */ + PCRE_REG_BADPAT, /* overlong MARK name */ + PCRE_REG_BADPAT, /* character value in \u.... sequence is too large */ + PCRE_REG_BADPAT, /* invalid UTF-32 string (should not occur) */ + PCRE_REG_BADPAT, /* setting UTF is disabled by the application */ + PCRE_REG_BADPAT, /* non-hex character in \\x{} (closing brace missing?) */ + /* 80 */ + PCRE_REG_BADPAT, /* non-octal character in \o{} (closing brace missing?) */ + PCRE_REG_BADPAT, /* missing opening brace after \o */ + PCRE_REG_BADPAT, /* parentheses too deeply nested */ + PCRE_REG_BADPAT, /* invalid range in character class */ + PCRE_REG_BADPAT, /* group name must start with a non-digit */ + /* 85 */ + PCRE_REG_BADPAT, /* parentheses too deeply nested (stack check) */ + PCRE_REG_BADPAT, /* missing digits in \x{} or \o{} */ + PCRE_REG_BADPAT /* pattern too complicated */ +}; + +/* Table of texts corresponding to POSIX error codes */ + +static const char *const pstring[] = { + "", /* Dummy for value 0 */ + "internal error", /* REG_ASSERT */ + "invalid repeat counts in {}", /* BADBR */ + "pattern error", /* BADPAT */ + "? * + invalid", /* BADRPT */ + "unbalanced {}", /* EBRACE */ + "unbalanced []", /* EBRACK */ + "collation error - not relevant", /* ECOLLATE */ + "bad class", /* ECTYPE */ + "bad escape sequence", /* EESCAPE */ + "empty expression", /* EMPTY */ + "unbalanced ()", /* EPAREN */ + "bad range inside []", /* ERANGE */ + "expression too big", /* ESIZE */ + "failed to get memory", /* ESPACE */ + "bad back reference", /* ESUBREG */ + "bad argument", /* INVARG */ + "match failed" /* NOMATCH */ +}; + + + + +/************************************************* +* Translate error code to string * +*************************************************/ + +PCREPOSIX_EXP_DEFN size_t PCRE_CALL_CONVENTION +pcre_regerror(int errcode, const pcre_regex_t *preg, char *errbuf, size_t errbuf_size) +{ +const char *message, *addmessage; +size_t length, addlength; + +message = (errcode >= (int)(sizeof(pstring)/sizeof(char *)))? + "unknown error code" : pstring[errcode]; +length = strlen(message) + 1; + +addmessage = " at offset "; +addlength = (preg != NULL && (int)preg->re_erroffset != -1)? + strlen(addmessage) + 6 : 0; + +if (errbuf_size > 0) + { + if (addlength > 0 && errbuf_size >= length + addlength) + sprintf(errbuf, "%s%s%-6d", message, addmessage, (int)preg->re_erroffset); + else + { + strncpy(errbuf, message, errbuf_size - 1); + errbuf[errbuf_size-1] = 0; + } + } + +return length + addlength; +} + + + + +/************************************************* +* Free store held by a regex * +*************************************************/ + +PCREPOSIX_EXP_DEFN void PCRE_CALL_CONVENTION +pcre_regfree(pcre_regex_t *preg) +{ +(PUBL(free))(preg->re_pcre); +} + + + + +/************************************************* +* Compile a regular expression * +*************************************************/ + +/* +Arguments: + preg points to a structure for recording the compiled expression + pattern the pattern to compile + cflags compilation flags + +Returns: 0 on success + various non-zero codes on failure +*/ + +PCREPOSIX_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_regcomp(pcre_regex_t *preg, const char *pattern, int cflags) +{ +const char *errorptr; +int erroffset; +int errorcode; +int options = 0; +int re_nsub = 0; + +if ((cflags & PCRE_REG_ICASE) != 0) options |= PCRE_CASELESS; +if ((cflags & PCRE_REG_NEWLINE) != 0) options |= PCRE_MULTILINE; +if ((cflags & PCRE_REG_DOTALL) != 0) options |= PCRE_DOTALL; +if ((cflags & PCRE_REG_NOSUB) != 0) options |= PCRE_NO_AUTO_CAPTURE; +if ((cflags & PCRE_REG_UTF8) != 0) options |= PCRE_UTF8; +if ((cflags & PCRE_REG_UCP) != 0) options |= PCRE_UCP; +if ((cflags & PCRE_REG_UNGREEDY) != 0) options |= PCRE_UNGREEDY; + +preg->re_pcre = pcre_compile2(pattern, options, &errorcode, &errorptr, + &erroffset, NULL); +preg->re_erroffset = erroffset; + +/* Safety: if the error code is too big for the translation vector (which +should not happen, but we all make mistakes), return REG_BADPAT. */ + +if (preg->re_pcre == NULL) + { + return (errorcode < (int)(sizeof(eint)/sizeof(const int)))? + eint[errorcode] : PCRE_REG_BADPAT; + } + +(void)pcre_fullinfo((const pcre *)preg->re_pcre, NULL, PCRE_INFO_CAPTURECOUNT, + &re_nsub); +preg->re_nsub = (size_t)re_nsub; +preg->re_erroffset = (size_t)(-1); /* No meaning after successful compile */ +return 0; +} + + + + +/************************************************* +* Match a regular expression * +*************************************************/ + +/* Unfortunately, PCRE requires 3 ints of working space for each captured +substring, so we have to get and release working store instead of just using +the POSIX structures as was done in earlier releases when PCRE needed only 2 +ints. However, if the number of possible capturing brackets is small, use a +block of store on the stack, to reduce the use of malloc/free. The threshold is +in a macro that can be changed at configure time. + +If REG_NOSUB was specified at compile time, the PCRE_NO_AUTO_CAPTURE flag will +be set. When this is the case, the nmatch and pmatch arguments are ignored, and +the only result is yes/no/error. */ + +PCREPOSIX_EXP_DEFN int PCRE_CALL_CONVENTION +pcre_regexec(const pcre_regex_t *preg, const char *string, size_t nmatch, + pcre_regmatch_t pmatch[], int eflags) +{ +int rc, so, eo; +int options = 0; +int *ovector = NULL; +int small_ovector[POSIX_MALLOC_THRESHOLD * 3]; +BOOL allocated_ovector = FALSE; +BOOL nosub = + (REAL_PCRE_OPTIONS((const pcre *)preg->re_pcre) & PCRE_NO_AUTO_CAPTURE) != 0; + +if ((eflags & PCRE_REG_NOTBOL) != 0) options |= PCRE_NOTBOL; +if ((eflags & PCRE_REG_NOTEOL) != 0) options |= PCRE_NOTEOL; +if ((eflags & PCRE_REG_NOTEMPTY) != 0) options |= PCRE_NOTEMPTY; + +/* When no string data is being returned, or no vector has been passed in which +to put it, ensure that nmatch is zero. Otherwise, ensure the vector for holding +the return data is large enough. */ + +if (nosub || pmatch == NULL) nmatch = 0; + +else if (nmatch > 0) + { + if (nmatch <= POSIX_MALLOC_THRESHOLD) + { + ovector = &(small_ovector[0]); + } + else + { + if (nmatch > INT_MAX/(sizeof(int) * 3)) return PCRE_REG_ESPACE; + ovector = (int *)malloc(sizeof(int) * nmatch * 3); + if (ovector == NULL) return PCRE_REG_ESPACE; + allocated_ovector = TRUE; + } + } + +/* REG_STARTEND is a BSD extension, to allow for non-NUL-terminated strings. +The man page from OS X says "REG_STARTEND affects only the location of the +string, not how it is matched". That is why the "so" value is used to bump the +start location rather than being passed as a PCRE "starting offset". */ + +if ((eflags & PCRE_REG_STARTEND) != 0) + { + if (pmatch == NULL) return PCRE_REG_INVARG; + so = pmatch[0].rm_so; + eo = pmatch[0].rm_eo; + } +else + { + so = 0; + eo = (int)strlen(string); + } + +rc = pcre_exec((const pcre *)preg->re_pcre, NULL, string + so, (eo - so), + 0, options, ovector, (int)(nmatch * 3)); + +if (rc == 0) rc = (int)nmatch; /* All captured slots were filled in */ + +/* Successful match */ + +if (rc >= 0) + { + size_t i; + if (!nosub) + { + for (i = 0; i < (size_t)rc; i++) + { + pmatch[i].rm_so = (ovector[i*2] < 0)? -1 : ovector[i*2] + so; + pmatch[i].rm_eo = (ovector[i*2+1] < 0)? -1: ovector[i*2+1] + so; + } + if (allocated_ovector) free(ovector); + for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1; + } + return 0; + } + +/* Unsuccessful match */ + +if (allocated_ovector) free(ovector); +switch(rc) + { +/* ========================================================================== */ + /* These cases are never obeyed. This is a fudge that causes a compile-time + error if the vector eint, which is indexed by compile-time error number, is + not the correct length. It seems to be the only way to do such a check at + compile time, as the sizeof() operator does not work in the C preprocessor. + As all the PCRE_ERROR_xxx values are negative, we can use 0 and 1. */ + + case 0: + case (sizeof(eint)/sizeof(int) == ERRCOUNT): + return PCRE_REG_ASSERT; +/* ========================================================================== */ + + case PCRE_ERROR_NOMATCH: return PCRE_REG_NOMATCH; + case PCRE_ERROR_NULL: return PCRE_REG_INVARG; + case PCRE_ERROR_BADOPTION: return PCRE_REG_INVARG; + case PCRE_ERROR_BADMAGIC: return PCRE_REG_INVARG; + case PCRE_ERROR_UNKNOWN_NODE: return PCRE_REG_ASSERT; + case PCRE_ERROR_NOMEMORY: return PCRE_REG_ESPACE; + case PCRE_ERROR_MATCHLIMIT: return PCRE_REG_ESPACE; + case PCRE_ERROR_BADUTF8: return PCRE_REG_INVARG; + case PCRE_ERROR_BADUTF8_OFFSET: return PCRE_REG_INVARG; + case PCRE_ERROR_BADMODE: return PCRE_REG_INVARG; + default: return PCRE_REG_ASSERT; + } +} + +/* End of pcreposix.c */ diff --git a/deps/pcre/pcreposix.h b/deps/pcre/pcreposix.h new file mode 100644 index 00000000000..70a195c1a80 --- /dev/null +++ b/deps/pcre/pcreposix.h @@ -0,0 +1,117 @@ +/************************************************* +* Perl-Compatible Regular Expressions * +*************************************************/ + +#ifndef _PCREPOSIX_H +#define _PCREPOSIX_H + +/* This is the header for the POSIX wrapper interface to the PCRE Perl- +Compatible Regular Expression library. It defines the things POSIX says should +be there. I hope. + + Copyright (c) 1997-2012 University of Cambridge + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + +/* Have to include stdlib.h in order to ensure that size_t is defined. */ + +#include + +#define PCREPOSIX_EXP_DEFN extern +#define PCREPOSIX_EXP_DECL extern + +/* Options, mostly defined by POSIX, but with some extras. */ + +#define PCRE_REG_ICASE 0x0001 /* Maps to PCRE_CASELESS */ +#define PCRE_REG_NEWLINE 0x0002 /* Maps to PCRE_MULTILINE */ +#define PCRE_REG_NOTBOL 0x0004 /* Maps to PCRE_NOTBOL */ +#define PCRE_REG_NOTEOL 0x0008 /* Maps to PCRE_NOTEOL */ +#define PCRE_REG_DOTALL 0x0010 /* NOT defined by POSIX; maps to PCRE_DOTALL */ +#define PCRE_REG_NOSUB 0x0020 /* Maps to PCRE_NO_AUTO_CAPTURE */ +#define PCRE_REG_UTF8 0x0040 /* NOT defined by POSIX; maps to PCRE_UTF8 */ +#define PCRE_REG_STARTEND 0x0080 /* BSD feature: pass subject string by so,eo */ +#define PCRE_REG_NOTEMPTY 0x0100 /* NOT defined by POSIX; maps to PCRE_NOTEMPTY */ +#define PCRE_REG_UNGREEDY 0x0200 /* NOT defined by POSIX; maps to PCRE_UNGREEDY */ +#define PCRE_REG_UCP 0x0400 /* NOT defined by POSIX; maps to PCRE_UCP */ + +/* This is not used by PCRE, but by defining it we make it easier +to slot PCRE into existing programs that make POSIX calls. */ + +#define PCRE_REG_EXTENDED 0 + +/* Error values. Not all these are relevant or used by the wrapper. */ + +enum { + PCRE_REG_ASSERT = 1, /* internal error ? */ + PCRE_REG_BADBR, /* invalid repeat counts in {} */ + PCRE_REG_BADPAT, /* pattern error */ + PCRE_REG_BADRPT, /* ? * + invalid */ + PCRE_REG_EBRACE, /* unbalanced {} */ + PCRE_REG_EBRACK, /* unbalanced [] */ + PCRE_REG_ECOLLATE, /* collation error - not relevant */ + PCRE_REG_ECTYPE, /* bad class */ + PCRE_REG_EESCAPE, /* bad escape sequence */ + PCRE_REG_EMPTY, /* empty expression */ + PCRE_REG_EPAREN, /* unbalanced () */ + PCRE_REG_ERANGE, /* bad range inside [] */ + PCRE_REG_ESIZE, /* expression too big */ + PCRE_REG_ESPACE, /* failed to get memory */ + PCRE_REG_ESUBREG, /* bad back reference */ + PCRE_REG_INVARG, /* bad argument */ + PCRE_REG_NOMATCH /* match failed */ +}; + + +/* The structure representing a compiled regular expression. */ + +typedef struct { + void *re_pcre; + size_t re_nsub; + size_t re_erroffset; +} pcre_regex_t; + +/* The structure in which a captured offset is returned. */ + +typedef int pcre_regoff_t; + +typedef struct { + pcre_regoff_t rm_so; + pcre_regoff_t rm_eo; +} pcre_regmatch_t; + +/* The functions */ + +PCREPOSIX_EXP_DECL int pcre_regcomp(pcre_regex_t *, const char *, int); +PCREPOSIX_EXP_DECL int pcre_regexec(const pcre_regex_t *, const char *, size_t, + pcre_regmatch_t *, int); +PCREPOSIX_EXP_DECL size_t pcre_regerror(int, const pcre_regex_t *, char *, size_t); +PCREPOSIX_EXP_DECL void pcre_regfree(pcre_regex_t *); + +#endif /* End of pcreposix.h */ diff --git a/deps/pcre/ucp.h b/deps/pcre/ucp.h new file mode 100644 index 00000000000..2fa00296e42 --- /dev/null +++ b/deps/pcre/ucp.h @@ -0,0 +1,224 @@ +/************************************************* +* Unicode Property Table handler * +*************************************************/ + +#ifndef _UCP_H +#define _UCP_H + +/* This file contains definitions of the property values that are returned by +the UCD access macros. New values that are added for new releases of Unicode +should always be at the end of each enum, for backwards compatibility. + +IMPORTANT: Note also that the specific numeric values of the enums have to be +the same as the values that are generated by the maint/MultiStage2.py script, +where the equivalent property descriptive names are listed in vectors. + +ALSO: The specific values of the first two enums are assumed for the table +called catposstab in pcre_compile.c. */ + +/* These are the general character categories. */ + +enum { + ucp_C, /* Other */ + ucp_L, /* Letter */ + ucp_M, /* Mark */ + ucp_N, /* Number */ + ucp_P, /* Punctuation */ + ucp_S, /* Symbol */ + ucp_Z /* Separator */ +}; + +/* These are the particular character categories. */ + +enum { + ucp_Cc, /* Control */ + ucp_Cf, /* Format */ + ucp_Cn, /* Unassigned */ + ucp_Co, /* Private use */ + ucp_Cs, /* Surrogate */ + ucp_Ll, /* Lower case letter */ + ucp_Lm, /* Modifier letter */ + ucp_Lo, /* Other letter */ + ucp_Lt, /* Title case letter */ + ucp_Lu, /* Upper case letter */ + ucp_Mc, /* Spacing mark */ + ucp_Me, /* Enclosing mark */ + ucp_Mn, /* Non-spacing mark */ + ucp_Nd, /* Decimal number */ + ucp_Nl, /* Letter number */ + ucp_No, /* Other number */ + ucp_Pc, /* Connector punctuation */ + ucp_Pd, /* Dash punctuation */ + ucp_Pe, /* Close punctuation */ + ucp_Pf, /* Final punctuation */ + ucp_Pi, /* Initial punctuation */ + ucp_Po, /* Other punctuation */ + ucp_Ps, /* Open punctuation */ + ucp_Sc, /* Currency symbol */ + ucp_Sk, /* Modifier symbol */ + ucp_Sm, /* Mathematical symbol */ + ucp_So, /* Other symbol */ + ucp_Zl, /* Line separator */ + ucp_Zp, /* Paragraph separator */ + ucp_Zs /* Space separator */ +}; + +/* These are grapheme break properties. Note that the code for processing them +assumes that the values are less than 16. If more values are added that take +the number to 16 or more, the code will have to be rewritten. */ + +enum { + ucp_gbCR, /* 0 */ + ucp_gbLF, /* 1 */ + ucp_gbControl, /* 2 */ + ucp_gbExtend, /* 3 */ + ucp_gbPrepend, /* 4 */ + ucp_gbSpacingMark, /* 5 */ + ucp_gbL, /* 6 Hangul syllable type L */ + ucp_gbV, /* 7 Hangul syllable type V */ + ucp_gbT, /* 8 Hangul syllable type T */ + ucp_gbLV, /* 9 Hangul syllable type LV */ + ucp_gbLVT, /* 10 Hangul syllable type LVT */ + ucp_gbRegionalIndicator, /* 11 */ + ucp_gbOther /* 12 */ +}; + +/* These are the script identifications. */ + +enum { + ucp_Arabic, + ucp_Armenian, + ucp_Bengali, + ucp_Bopomofo, + ucp_Braille, + ucp_Buginese, + ucp_Buhid, + ucp_Canadian_Aboriginal, + ucp_Cherokee, + ucp_Common, + ucp_Coptic, + ucp_Cypriot, + ucp_Cyrillic, + ucp_Deseret, + ucp_Devanagari, + ucp_Ethiopic, + ucp_Georgian, + ucp_Glagolitic, + ucp_Gothic, + ucp_Greek, + ucp_Gujarati, + ucp_Gurmukhi, + ucp_Han, + ucp_Hangul, + ucp_Hanunoo, + ucp_Hebrew, + ucp_Hiragana, + ucp_Inherited, + ucp_Kannada, + ucp_Katakana, + ucp_Kharoshthi, + ucp_Khmer, + ucp_Lao, + ucp_Latin, + ucp_Limbu, + ucp_Linear_B, + ucp_Malayalam, + ucp_Mongolian, + ucp_Myanmar, + ucp_New_Tai_Lue, + ucp_Ogham, + ucp_Old_Italic, + ucp_Old_Persian, + ucp_Oriya, + ucp_Osmanya, + ucp_Runic, + ucp_Shavian, + ucp_Sinhala, + ucp_Syloti_Nagri, + ucp_Syriac, + ucp_Tagalog, + ucp_Tagbanwa, + ucp_Tai_Le, + ucp_Tamil, + ucp_Telugu, + ucp_Thaana, + ucp_Thai, + ucp_Tibetan, + ucp_Tifinagh, + ucp_Ugaritic, + ucp_Yi, + /* New for Unicode 5.0: */ + ucp_Balinese, + ucp_Cuneiform, + ucp_Nko, + ucp_Phags_Pa, + ucp_Phoenician, + /* New for Unicode 5.1: */ + ucp_Carian, + ucp_Cham, + ucp_Kayah_Li, + ucp_Lepcha, + ucp_Lycian, + ucp_Lydian, + ucp_Ol_Chiki, + ucp_Rejang, + ucp_Saurashtra, + ucp_Sundanese, + ucp_Vai, + /* New for Unicode 5.2: */ + ucp_Avestan, + ucp_Bamum, + ucp_Egyptian_Hieroglyphs, + ucp_Imperial_Aramaic, + ucp_Inscriptional_Pahlavi, + ucp_Inscriptional_Parthian, + ucp_Javanese, + ucp_Kaithi, + ucp_Lisu, + ucp_Meetei_Mayek, + ucp_Old_South_Arabian, + ucp_Old_Turkic, + ucp_Samaritan, + ucp_Tai_Tham, + ucp_Tai_Viet, + /* New for Unicode 6.0.0: */ + ucp_Batak, + ucp_Brahmi, + ucp_Mandaic, + /* New for Unicode 6.1.0: */ + ucp_Chakma, + ucp_Meroitic_Cursive, + ucp_Meroitic_Hieroglyphs, + ucp_Miao, + ucp_Sharada, + ucp_Sora_Sompeng, + ucp_Takri, + /* New for Unicode 7.0.0: */ + ucp_Bassa_Vah, + ucp_Caucasian_Albanian, + ucp_Duployan, + ucp_Elbasan, + ucp_Grantha, + ucp_Khojki, + ucp_Khudawadi, + ucp_Linear_A, + ucp_Mahajani, + ucp_Manichaean, + ucp_Mende_Kikakui, + ucp_Modi, + ucp_Mro, + ucp_Nabataean, + ucp_Old_North_Arabian, + ucp_Old_Permic, + ucp_Pahawh_Hmong, + ucp_Palmyrene, + ucp_Psalter_Pahlavi, + ucp_Pau_Cin_Hau, + ucp_Siddham, + ucp_Tirhuta, + ucp_Warang_Citi +}; + +#endif + +/* End of ucp.h */ diff --git a/deps/regex/config.h b/deps/regex/config.h deleted file mode 100644 index 95370690e02..00000000000 --- a/deps/regex/config.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef _REGEX_CONFIG_H_ -#define _REGEX_CONFIG_H_ - -# define GAWK -# define NO_MBSUPPORT - -#endif diff --git a/deps/regex/regcomp.c b/deps/regex/regcomp.c deleted file mode 100644 index 43bffbc21f5..00000000000 --- a/deps/regex/regcomp.c +++ /dev/null @@ -1,3857 +0,0 @@ -/* Extended regular expression matching and search library. - Copyright (C) 2002-2007,2009,2010 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Isamu Hasegawa . - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA. */ - -static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern, - size_t length, reg_syntax_t syntax); -static void re_compile_fastmap_iter (regex_t *bufp, - const re_dfastate_t *init_state, - char *fastmap); -static reg_errcode_t init_dfa (re_dfa_t *dfa, size_t pat_len); -#ifdef RE_ENABLE_I18N -static void free_charset (re_charset_t *cset); -#endif /* RE_ENABLE_I18N */ -static void free_workarea_compile (regex_t *preg); -static reg_errcode_t create_initial_state (re_dfa_t *dfa); -#ifdef RE_ENABLE_I18N -static void optimize_utf8 (re_dfa_t *dfa); -#endif -static reg_errcode_t analyze (regex_t *preg); -static reg_errcode_t preorder (bin_tree_t *root, - reg_errcode_t (fn (void *, bin_tree_t *)), - void *extra); -static reg_errcode_t postorder (bin_tree_t *root, - reg_errcode_t (fn (void *, bin_tree_t *)), - void *extra); -static reg_errcode_t optimize_subexps (void *extra, bin_tree_t *node); -static reg_errcode_t lower_subexps (void *extra, bin_tree_t *node); -static bin_tree_t *lower_subexp (reg_errcode_t *err, regex_t *preg, - bin_tree_t *node); -static reg_errcode_t calc_first (void *extra, bin_tree_t *node); -static reg_errcode_t calc_next (void *extra, bin_tree_t *node); -static reg_errcode_t link_nfa_nodes (void *extra, bin_tree_t *node); -static int duplicate_node (re_dfa_t *dfa, int org_idx, unsigned int constraint); -static int search_duplicated_node (const re_dfa_t *dfa, int org_node, - unsigned int constraint); -static reg_errcode_t calc_eclosure (re_dfa_t *dfa); -static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, - int node, int root); -static reg_errcode_t calc_inveclosure (re_dfa_t *dfa); -static int fetch_number (re_string_t *input, re_token_t *token, - reg_syntax_t syntax); -static int peek_token (re_token_t *token, re_string_t *input, - reg_syntax_t syntax) internal_function; -static bin_tree_t *parse (re_string_t *regexp, regex_t *preg, - reg_syntax_t syntax, reg_errcode_t *err); -static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg, - re_token_t *token, reg_syntax_t syntax, - int nest, reg_errcode_t *err); -static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg, - re_token_t *token, reg_syntax_t syntax, - int nest, reg_errcode_t *err); -static bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg, - re_token_t *token, reg_syntax_t syntax, - int nest, reg_errcode_t *err); -static bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg, - re_token_t *token, reg_syntax_t syntax, - int nest, reg_errcode_t *err); -static bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp, - re_dfa_t *dfa, re_token_t *token, - reg_syntax_t syntax, reg_errcode_t *err); -static bin_tree_t *parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, - re_token_t *token, reg_syntax_t syntax, - reg_errcode_t *err); -static reg_errcode_t parse_bracket_element (bracket_elem_t *elem, - re_string_t *regexp, - re_token_t *token, int token_len, - re_dfa_t *dfa, - reg_syntax_t syntax, - int accept_hyphen); -static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem, - re_string_t *regexp, - re_token_t *token); -#ifdef RE_ENABLE_I18N -static reg_errcode_t build_equiv_class (bitset_t sbcset, - re_charset_t *mbcset, - int *equiv_class_alloc, - const unsigned char *name); -static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans, - bitset_t sbcset, - re_charset_t *mbcset, - int *char_class_alloc, - const char *class_name, - reg_syntax_t syntax); -#else /* not RE_ENABLE_I18N */ -static reg_errcode_t build_equiv_class (bitset_t sbcset, - const unsigned char *name); -static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans, - bitset_t sbcset, - const char *class_name, - reg_syntax_t syntax); -#endif /* not RE_ENABLE_I18N */ -static bin_tree_t *build_charclass_op (re_dfa_t *dfa, - RE_TRANSLATE_TYPE trans, - const char *class_name, - const char *extra, - int non_match, reg_errcode_t *err); -static bin_tree_t *create_tree (re_dfa_t *dfa, - bin_tree_t *left, bin_tree_t *right, - re_token_type_t type); -static bin_tree_t *create_token_tree (re_dfa_t *dfa, - bin_tree_t *left, bin_tree_t *right, - const re_token_t *token); -static bin_tree_t *duplicate_tree (const bin_tree_t *src, re_dfa_t *dfa); -static void free_token (re_token_t *node); -static reg_errcode_t free_tree (void *extra, bin_tree_t *node); -static reg_errcode_t mark_opt_subexp (void *extra, bin_tree_t *node); - -/* This table gives an error message for each of the error codes listed - in regex.h. Obviously the order here has to be same as there. - POSIX doesn't require that we do anything for REG_NOERROR, - but why not be nice? */ - -const char __re_error_msgid[] attribute_hidden = - { -#define REG_NOERROR_IDX 0 - gettext_noop ("Success") /* REG_NOERROR */ - "\0" -#define REG_NOMATCH_IDX (REG_NOERROR_IDX + sizeof "Success") - gettext_noop ("No match") /* REG_NOMATCH */ - "\0" -#define REG_BADPAT_IDX (REG_NOMATCH_IDX + sizeof "No match") - gettext_noop ("Invalid regular expression") /* REG_BADPAT */ - "\0" -#define REG_ECOLLATE_IDX (REG_BADPAT_IDX + sizeof "Invalid regular expression") - gettext_noop ("Invalid collation character") /* REG_ECOLLATE */ - "\0" -#define REG_ECTYPE_IDX (REG_ECOLLATE_IDX + sizeof "Invalid collation character") - gettext_noop ("Invalid character class name") /* REG_ECTYPE */ - "\0" -#define REG_EESCAPE_IDX (REG_ECTYPE_IDX + sizeof "Invalid character class name") - gettext_noop ("Trailing backslash") /* REG_EESCAPE */ - "\0" -#define REG_ESUBREG_IDX (REG_EESCAPE_IDX + sizeof "Trailing backslash") - gettext_noop ("Invalid back reference") /* REG_ESUBREG */ - "\0" -#define REG_EBRACK_IDX (REG_ESUBREG_IDX + sizeof "Invalid back reference") - gettext_noop ("Unmatched [ or [^") /* REG_EBRACK */ - "\0" -#define REG_EPAREN_IDX (REG_EBRACK_IDX + sizeof "Unmatched [ or [^") - gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */ - "\0" -#define REG_EBRACE_IDX (REG_EPAREN_IDX + sizeof "Unmatched ( or \\(") - gettext_noop ("Unmatched \\{") /* REG_EBRACE */ - "\0" -#define REG_BADBR_IDX (REG_EBRACE_IDX + sizeof "Unmatched \\{") - gettext_noop ("Invalid content of \\{\\}") /* REG_BADBR */ - "\0" -#define REG_ERANGE_IDX (REG_BADBR_IDX + sizeof "Invalid content of \\{\\}") - gettext_noop ("Invalid range end") /* REG_ERANGE */ - "\0" -#define REG_ESPACE_IDX (REG_ERANGE_IDX + sizeof "Invalid range end") - gettext_noop ("Memory exhausted") /* REG_ESPACE */ - "\0" -#define REG_BADRPT_IDX (REG_ESPACE_IDX + sizeof "Memory exhausted") - gettext_noop ("Invalid preceding regular expression") /* REG_BADRPT */ - "\0" -#define REG_EEND_IDX (REG_BADRPT_IDX + sizeof "Invalid preceding regular expression") - gettext_noop ("Premature end of regular expression") /* REG_EEND */ - "\0" -#define REG_ESIZE_IDX (REG_EEND_IDX + sizeof "Premature end of regular expression") - gettext_noop ("Regular expression too big") /* REG_ESIZE */ - "\0" -#define REG_ERPAREN_IDX (REG_ESIZE_IDX + sizeof "Regular expression too big") - gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */ - }; - -const size_t __re_error_msgid_idx[] attribute_hidden = - { - REG_NOERROR_IDX, - REG_NOMATCH_IDX, - REG_BADPAT_IDX, - REG_ECOLLATE_IDX, - REG_ECTYPE_IDX, - REG_EESCAPE_IDX, - REG_ESUBREG_IDX, - REG_EBRACK_IDX, - REG_EPAREN_IDX, - REG_EBRACE_IDX, - REG_BADBR_IDX, - REG_ERANGE_IDX, - REG_ESPACE_IDX, - REG_BADRPT_IDX, - REG_EEND_IDX, - REG_ESIZE_IDX, - REG_ERPAREN_IDX - }; - -/* Entry points for GNU code. */ - - -#ifdef ZOS_USS - -/* For ZOS USS we must define btowc */ - -wchar_t -btowc (int c) -{ - wchar_t wtmp[2]; - char tmp[2]; - - tmp[0] = c; - tmp[1] = 0; - - mbtowc (wtmp, tmp, 1); - return wtmp[0]; -} -#endif - -/* re_compile_pattern is the GNU regular expression compiler: it - compiles PATTERN (of length LENGTH) and puts the result in BUFP. - Returns 0 if the pattern was valid, otherwise an error string. - - Assumes the `allocated' (and perhaps `buffer') and `translate' fields - are set in BUFP on entry. */ - -const char * -re_compile_pattern (const char *pattern, - size_t length, - struct re_pattern_buffer *bufp) -{ - reg_errcode_t ret; - - /* And GNU code determines whether or not to get register information - by passing null for the REGS argument to re_match, etc., not by - setting no_sub, unless RE_NO_SUB is set. */ - bufp->no_sub = !!(re_syntax_options & RE_NO_SUB); - - /* Match anchors at newline. */ - bufp->newline_anchor = 1; - - ret = re_compile_internal (bufp, pattern, length, re_syntax_options); - - if (!ret) - return NULL; - return gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]); -} -#ifdef _LIBC -weak_alias (__re_compile_pattern, re_compile_pattern) -#endif - -/* Set by `re_set_syntax' to the current regexp syntax to recognize. Can - also be assigned to arbitrarily: each pattern buffer stores its own - syntax, so it can be changed between regex compilations. */ -/* This has no initializer because initialized variables in Emacs - become read-only after dumping. */ -reg_syntax_t re_syntax_options; - - -/* Specify the precise syntax of regexps for compilation. This provides - for compatibility for various utilities which historically have - different, incompatible syntaxes. - - The argument SYNTAX is a bit mask comprised of the various bits - defined in regex.h. We return the old syntax. */ - -reg_syntax_t -re_set_syntax (reg_syntax_t syntax) -{ - reg_syntax_t ret = re_syntax_options; - - re_syntax_options = syntax; - return ret; -} -#ifdef _LIBC -weak_alias (__re_set_syntax, re_set_syntax) -#endif - -int -re_compile_fastmap (struct re_pattern_buffer *bufp) -{ - re_dfa_t *dfa = (re_dfa_t *) bufp->buffer; - char *fastmap = bufp->fastmap; - - memset (fastmap, '\0', sizeof (char) * SBC_MAX); - re_compile_fastmap_iter (bufp, dfa->init_state, fastmap); - if (dfa->init_state != dfa->init_state_word) - re_compile_fastmap_iter (bufp, dfa->init_state_word, fastmap); - if (dfa->init_state != dfa->init_state_nl) - re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap); - if (dfa->init_state != dfa->init_state_begbuf) - re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap); - bufp->fastmap_accurate = 1; - return 0; -} -#ifdef _LIBC -weak_alias (__re_compile_fastmap, re_compile_fastmap) -#endif - -static inline void -__attribute ((always_inline)) -re_set_fastmap (char *fastmap, int icase, int ch) -{ - fastmap[ch] = 1; - if (icase) - fastmap[tolower (ch)] = 1; -} - -/* Helper function for re_compile_fastmap. - Compile fastmap for the initial_state INIT_STATE. */ - -static void -re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state, - char *fastmap) -{ - volatile re_dfa_t *dfa = (re_dfa_t *) bufp->buffer; - int node_cnt; - int icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE)); - for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt) - { - int node = init_state->nodes.elems[node_cnt]; - re_token_type_t type = dfa->nodes[node].type; - - if (type == CHARACTER) - { - re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c); -#ifdef RE_ENABLE_I18N - if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1) - { - unsigned char *buf = re_malloc (unsigned char, dfa->mb_cur_max), *p; - wchar_t wc; - mbstate_t state; - - p = buf; - *p++ = dfa->nodes[node].opr.c; - while (++node < dfa->nodes_len - && dfa->nodes[node].type == CHARACTER - && dfa->nodes[node].mb_partial) - *p++ = dfa->nodes[node].opr.c; - memset (&state, '\0', sizeof (state)); - if (__mbrtowc (&wc, (const char *) buf, p - buf, - &state) == p - buf - && (__wcrtomb ((char *) buf, towlower (wc), &state) - != (size_t) -1)) - re_set_fastmap (fastmap, 0, buf[0]); - re_free (buf); - } -#endif - } - else if (type == SIMPLE_BRACKET) - { - int i, ch; - for (i = 0, ch = 0; i < BITSET_WORDS; ++i) - { - int j; - bitset_word_t w = dfa->nodes[node].opr.sbcset[i]; - for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch) - if (w & ((bitset_word_t) 1 << j)) - re_set_fastmap (fastmap, icase, ch); - } - } -#ifdef RE_ENABLE_I18N - else if (type == COMPLEX_BRACKET) - { - re_charset_t *cset = dfa->nodes[node].opr.mbcset; - int i; - -# ifdef _LIBC - /* See if we have to try all bytes which start multiple collation - elements. - e.g. In da_DK, we want to catch 'a' since "aa" is a valid - collation element, and don't catch 'b' since 'b' is - the only collation element which starts from 'b' (and - it is caught by SIMPLE_BRACKET). */ - if (_NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES) != 0 - && (cset->ncoll_syms || cset->nranges)) - { - const int32_t *table = (const int32_t *) - _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB); - for (i = 0; i < SBC_MAX; ++i) - if (table[i] < 0) - re_set_fastmap (fastmap, icase, i); - } -# endif /* _LIBC */ - - /* See if we have to start the match at all multibyte characters, - i.e. where we would not find an invalid sequence. This only - applies to multibyte character sets; for single byte character - sets, the SIMPLE_BRACKET again suffices. */ - if (dfa->mb_cur_max > 1 - && (cset->nchar_classes || cset->non_match || cset->nranges -# ifdef _LIBC - || cset->nequiv_classes -# endif /* _LIBC */ - )) - { - unsigned char c = 0; - do - { - mbstate_t mbs; - memset (&mbs, 0, sizeof (mbs)); - if (__mbrtowc (NULL, (char *) &c, 1, &mbs) == (size_t) -2) - re_set_fastmap (fastmap, false, (int) c); - } - while (++c != 0); - } - - else - { - /* ... Else catch all bytes which can start the mbchars. */ - for (i = 0; i < cset->nmbchars; ++i) - { - char buf[256]; - mbstate_t state; - memset (&state, '\0', sizeof (state)); - if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1) - re_set_fastmap (fastmap, icase, *(unsigned char *) buf); - if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1) - { - if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state) - != (size_t) -1) - re_set_fastmap (fastmap, false, *(unsigned char *) buf); - } - } - } - } -#endif /* RE_ENABLE_I18N */ - else if (type == OP_PERIOD -#ifdef RE_ENABLE_I18N - || type == OP_UTF8_PERIOD -#endif /* RE_ENABLE_I18N */ - || type == END_OF_RE) - { - memset (fastmap, '\1', sizeof (char) * SBC_MAX); - if (type == END_OF_RE) - bufp->can_be_null = 1; - return; - } - } -} - -/* Entry point for POSIX code. */ -/* regcomp takes a regular expression as a string and compiles it. - - PREG is a regex_t *. We do not expect any fields to be initialized, - since POSIX says we shouldn't. Thus, we set - - `buffer' to the compiled pattern; - `used' to the length of the compiled pattern; - `syntax' to RE_SYNTAX_POSIX_EXTENDED if the - REG_EXTENDED bit in CFLAGS is set; otherwise, to - RE_SYNTAX_POSIX_BASIC; - `newline_anchor' to REG_NEWLINE being set in CFLAGS; - `fastmap' to an allocated space for the fastmap; - `fastmap_accurate' to zero; - `re_nsub' to the number of subexpressions in PATTERN. - - PATTERN is the address of the pattern string. - - CFLAGS is a series of bits which affect compilation. - - If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we - use POSIX basic syntax. - - If REG_NEWLINE is set, then . and [^...] don't match newline. - Also, regexec will try a match beginning after every newline. - - If REG_ICASE is set, then we considers upper- and lowercase - versions of letters to be equivalent when matching. - - If REG_NOSUB is set, then when PREG is passed to regexec, that - routine will report only success or failure, and nothing about the - registers. - - It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for - the return codes and their meanings.) */ - -int -regcomp (regex_t *__restrict preg, - const char *__restrict pattern, - int cflags) -{ - reg_errcode_t ret; - reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED - : RE_SYNTAX_POSIX_BASIC); - - preg->buffer = NULL; - preg->allocated = 0; - preg->used = 0; - - /* Try to allocate space for the fastmap. */ - preg->fastmap = re_malloc (char, SBC_MAX); - if (BE (preg->fastmap == NULL, 0)) - return REG_ESPACE; - - syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0; - - /* If REG_NEWLINE is set, newlines are treated differently. */ - if (cflags & REG_NEWLINE) - { /* REG_NEWLINE implies neither . nor [^...] match newline. */ - syntax &= ~RE_DOT_NEWLINE; - syntax |= RE_HAT_LISTS_NOT_NEWLINE; - /* It also changes the matching behavior. */ - preg->newline_anchor = 1; - } - else - preg->newline_anchor = 0; - preg->no_sub = !!(cflags & REG_NOSUB); - preg->translate = NULL; - - ret = re_compile_internal (preg, pattern, strlen (pattern), syntax); - - /* POSIX doesn't distinguish between an unmatched open-group and an - unmatched close-group: both are REG_EPAREN. */ - if (ret == REG_ERPAREN) - ret = REG_EPAREN; - - /* We have already checked preg->fastmap != NULL. */ - if (BE (ret == REG_NOERROR, 1)) - /* Compute the fastmap now, since regexec cannot modify the pattern - buffer. This function never fails in this implementation. */ - (void) re_compile_fastmap (preg); - else - { - /* Some error occurred while compiling the expression. */ - re_free (preg->fastmap); - preg->fastmap = NULL; - } - - return (int) ret; -} -#ifdef _LIBC -weak_alias (__regcomp, regcomp) -#endif - -/* Returns a message corresponding to an error code, ERRCODE, returned - from either regcomp or regexec. We don't use PREG here. */ - -size_t -regerror(int errcode, UNUSED const regex_t *__restrict preg, - char *__restrict errbuf, size_t errbuf_size) -{ - const char *msg; - size_t msg_size; - - if (BE (errcode < 0 - || errcode >= (int) (sizeof (__re_error_msgid_idx) - / sizeof (__re_error_msgid_idx[0])), 0)) - /* Only error codes returned by the rest of the code should be passed - to this routine. If we are given anything else, or if other regex - code generates an invalid error code, then the program has a bug. - Dump core so we can fix it. */ - abort (); - - msg = gettext (__re_error_msgid + __re_error_msgid_idx[errcode]); - - msg_size = strlen (msg) + 1; /* Includes the null. */ - - if (BE (errbuf_size != 0, 1)) - { - if (BE (msg_size > errbuf_size, 0)) - { - memcpy (errbuf, msg, errbuf_size - 1); - errbuf[errbuf_size - 1] = 0; - } - else - memcpy (errbuf, msg, msg_size); - } - - return msg_size; -} -#ifdef _LIBC -weak_alias (__regerror, regerror) -#endif - - -#ifdef RE_ENABLE_I18N -/* This static array is used for the map to single-byte characters when - UTF-8 is used. Otherwise we would allocate memory just to initialize - it the same all the time. UTF-8 is the preferred encoding so this is - a worthwhile optimization. */ -#if __GNUC__ >= 3 -static const bitset_t utf8_sb_map = { - /* Set the first 128 bits. */ - [0 ... 0x80 / BITSET_WORD_BITS - 1] = BITSET_WORD_MAX -}; -#else /* ! (__GNUC__ >= 3) */ -static bitset_t utf8_sb_map; -#endif /* __GNUC__ >= 3 */ -#endif /* RE_ENABLE_I18N */ - - -static void -free_dfa_content (re_dfa_t *dfa) -{ - unsigned int i; - int j; - - if (dfa->nodes) - for (i = 0; i < dfa->nodes_len; ++i) - free_token (dfa->nodes + i); - re_free (dfa->nexts); - for (i = 0; i < dfa->nodes_len; ++i) - { - if (dfa->eclosures != NULL) - re_node_set_free (dfa->eclosures + i); - if (dfa->inveclosures != NULL) - re_node_set_free (dfa->inveclosures + i); - if (dfa->edests != NULL) - re_node_set_free (dfa->edests + i); - } - re_free (dfa->edests); - re_free (dfa->eclosures); - re_free (dfa->inveclosures); - re_free (dfa->nodes); - - if (dfa->state_table) - for (i = 0; i <= dfa->state_hash_mask; ++i) - { - struct re_state_table_entry *entry = dfa->state_table + i; - for (j = 0; j < entry->num; ++j) - { - re_dfastate_t *state = entry->array[j]; - free_state (state); - } - re_free (entry->array); - } - re_free (dfa->state_table); -#ifdef RE_ENABLE_I18N - if (dfa->sb_char != utf8_sb_map) - re_free (dfa->sb_char); -#endif - re_free (dfa->subexp_map); -#ifdef DEBUG - re_free (dfa->re_str); -#endif - - re_free (dfa); -} - - -/* Free dynamically allocated space used by PREG. */ - -void -regfree (regex_t *preg) -{ - re_dfa_t *dfa = (re_dfa_t *) preg->buffer; - if (BE (dfa != NULL, 1)) - free_dfa_content (dfa); - preg->buffer = NULL; - preg->allocated = 0; - - re_free (preg->fastmap); - preg->fastmap = NULL; - - re_free (preg->translate); - preg->translate = NULL; -} -#ifdef _LIBC -weak_alias (__regfree, regfree) -#endif - -/* Entry points compatible with 4.2 BSD regex library. We don't define - them unless specifically requested. */ - -#if defined _REGEX_RE_COMP || defined _LIBC - -/* BSD has one and only one pattern buffer. */ -static struct re_pattern_buffer re_comp_buf; - -char * -# ifdef _LIBC -/* Make these definitions weak in libc, so POSIX programs can redefine - these names if they don't use our functions, and still use - regcomp/regexec above without link errors. */ -weak_function -# endif -re_comp (s) - const char *s; -{ - reg_errcode_t ret; - char *fastmap; - - if (!s) - { - if (!re_comp_buf.buffer) - return gettext ("No previous regular expression"); - return 0; - } - - if (re_comp_buf.buffer) - { - fastmap = re_comp_buf.fastmap; - re_comp_buf.fastmap = NULL; - __regfree (&re_comp_buf); - memset (&re_comp_buf, '\0', sizeof (re_comp_buf)); - re_comp_buf.fastmap = fastmap; - } - - if (re_comp_buf.fastmap == NULL) - { - re_comp_buf.fastmap = (char *) malloc (SBC_MAX); - if (re_comp_buf.fastmap == NULL) - return (char *) gettext (__re_error_msgid - + __re_error_msgid_idx[(int) REG_ESPACE]); - } - - /* Since `re_exec' always passes NULL for the `regs' argument, we - don't need to initialize the pattern buffer fields which affect it. */ - - /* Match anchors at newlines. */ - re_comp_buf.newline_anchor = 1; - - ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options); - - if (!ret) - return NULL; - - /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */ - return (char *) gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]); -} - -#ifdef _LIBC -libc_freeres_fn (free_mem) -{ - __regfree (&re_comp_buf); -} -#endif - -#endif /* _REGEX_RE_COMP */ - -/* Internal entry point. - Compile the regular expression PATTERN, whose length is LENGTH. - SYNTAX indicate regular expression's syntax. */ - -static reg_errcode_t -re_compile_internal (regex_t *preg, const char * pattern, size_t length, - reg_syntax_t syntax) -{ - reg_errcode_t err = REG_NOERROR; - re_dfa_t *dfa; - re_string_t regexp; - - /* Initialize the pattern buffer. */ - preg->fastmap_accurate = 0; - preg->syntax = syntax; - preg->not_bol = preg->not_eol = 0; - preg->used = 0; - preg->re_nsub = 0; - preg->can_be_null = 0; - preg->regs_allocated = REGS_UNALLOCATED; - - /* Initialize the dfa. */ - dfa = (re_dfa_t *) preg->buffer; - if (BE (preg->allocated < sizeof (re_dfa_t), 0)) - { - /* If zero allocated, but buffer is non-null, try to realloc - enough space. This loses if buffer's address is bogus, but - that is the user's responsibility. If ->buffer is NULL this - is a simple allocation. */ - dfa = re_realloc (preg->buffer, re_dfa_t, 1); - if (dfa == NULL) - return REG_ESPACE; - preg->allocated = sizeof (re_dfa_t); - preg->buffer = (unsigned char *) dfa; - } - preg->used = sizeof (re_dfa_t); - - err = init_dfa (dfa, length); - if (BE (err != REG_NOERROR, 0)) - { - free_dfa_content (dfa); - preg->buffer = NULL; - preg->allocated = 0; - return err; - } -#ifdef DEBUG - /* Note: length+1 will not overflow since it is checked in init_dfa. */ - dfa->re_str = re_malloc (char, length + 1); - strncpy (dfa->re_str, pattern, length + 1); -#endif - - __libc_lock_init (dfa->lock); - - err = re_string_construct (®exp, pattern, length, preg->translate, - syntax & RE_ICASE, dfa); - if (BE (err != REG_NOERROR, 0)) - { - re_compile_internal_free_return: - free_workarea_compile (preg); - re_string_destruct (®exp); - free_dfa_content (dfa); - preg->buffer = NULL; - preg->allocated = 0; - return err; - } - - /* Parse the regular expression, and build a structure tree. */ - preg->re_nsub = 0; - dfa->str_tree = parse (®exp, preg, syntax, &err); - if (BE (dfa->str_tree == NULL, 0)) - goto re_compile_internal_free_return; - - /* Analyze the tree and create the nfa. */ - err = analyze (preg); - if (BE (err != REG_NOERROR, 0)) - goto re_compile_internal_free_return; - -#ifdef RE_ENABLE_I18N - /* If possible, do searching in single byte encoding to speed things up. */ - if (dfa->is_utf8 && !(syntax & RE_ICASE) && preg->translate == NULL) - optimize_utf8 (dfa); -#endif - - /* Then create the initial state of the dfa. */ - err = create_initial_state (dfa); - - /* Release work areas. */ - free_workarea_compile (preg); - re_string_destruct (®exp); - - if (BE (err != REG_NOERROR, 0)) - { - free_dfa_content (dfa); - preg->buffer = NULL; - preg->allocated = 0; - } - - return err; -} - -/* Initialize DFA. We use the length of the regular expression PAT_LEN - as the initial length of some arrays. */ - -static reg_errcode_t -init_dfa (re_dfa_t *dfa, size_t pat_len) -{ - unsigned int table_size; - - memset (dfa, '\0', sizeof (re_dfa_t)); - - /* Force allocation of str_tree_storage the first time. */ - dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE; - - /* Avoid overflows. */ - if (pat_len == SIZE_MAX) - return REG_ESPACE; - - dfa->nodes_alloc = pat_len + 1; - dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc); - - /* table_size = 2 ^ ceil(log pat_len) */ - for (table_size = 1; ; table_size <<= 1) - if (table_size > pat_len) - break; - - dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size); - dfa->state_hash_mask = table_size - 1; - - dfa->mb_cur_max = MB_CUR_MAX; -#ifdef _LIBC - if (dfa->mb_cur_max == 6 - && strcmp (_NL_CURRENT (LC_CTYPE, _NL_CTYPE_CODESET_NAME), "UTF-8") == 0) - dfa->is_utf8 = 1; - dfa->map_notascii = (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MAP_TO_NONASCII) - != 0); -#else - dfa->is_utf8 = 1; - /* We check exhaustively in the loop below if this charset is a - superset of ASCII. */ - dfa->map_notascii = 0; -#endif - -#ifdef RE_ENABLE_I18N - if (dfa->mb_cur_max > 1) - { - if (dfa->is_utf8) - { -#if !defined(__GNUC__) || __GNUC__ < 3 - static short utf8_sb_map_inited = 0; - - if (! utf8_sb_map_inited) - { - int i; - - utf8_sb_map_inited = 0; - for (i = 0; i <= 0x80 / BITSET_WORD_BITS - 1; i++) - utf8_sb_map[i] = BITSET_WORD_MAX; - } -#endif - dfa->sb_char = (re_bitset_ptr_t) utf8_sb_map; - } - else - { - int i, j, ch; - - dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1); - if (BE (dfa->sb_char == NULL, 0)) - return REG_ESPACE; - - /* Set the bits corresponding to single byte chars. */ - for (i = 0, ch = 0; i < BITSET_WORDS; ++i) - for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch) - { - wint_t wch = __btowc (ch); - if (wch != WEOF) - dfa->sb_char[i] |= (bitset_word_t) 1 << j; -# ifndef _LIBC - if (isascii (ch) && wch != ch) - dfa->map_notascii = 1; -# endif - } - } - } -#endif - - if (BE (dfa->nodes == NULL || dfa->state_table == NULL, 0)) - return REG_ESPACE; - return REG_NOERROR; -} - -/* Initialize WORD_CHAR table, which indicate which character is - "word". In this case "word" means that it is the word construction - character used by some operators like "\<", "\>", etc. */ - -static void -internal_function -init_word_char (re_dfa_t *dfa) -{ - int i, j, ch; - dfa->word_ops_used = 1; - for (i = 0, ch = 0; i < BITSET_WORDS; ++i) - for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch) - if (isalnum (ch) || ch == '_') - dfa->word_char[i] |= (bitset_word_t) 1 << j; -} - -/* Free the work area which are only used while compiling. */ - -static void -free_workarea_compile (regex_t *preg) -{ - re_dfa_t *dfa = (re_dfa_t *) preg->buffer; - bin_tree_storage_t *storage, *next; - for (storage = dfa->str_tree_storage; storage; storage = next) - { - next = storage->next; - re_free (storage); - } - dfa->str_tree_storage = NULL; - dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE; - dfa->str_tree = NULL; - re_free (dfa->org_indices); - dfa->org_indices = NULL; -} - -/* Create initial states for all contexts. */ - -static reg_errcode_t -create_initial_state (re_dfa_t *dfa) -{ - int first, i; - reg_errcode_t err; - re_node_set init_nodes; - - /* Initial states have the epsilon closure of the node which is - the first node of the regular expression. */ - first = dfa->str_tree->first->node_idx; - dfa->init_node = first; - err = re_node_set_init_copy (&init_nodes, dfa->eclosures + first); - if (BE (err != REG_NOERROR, 0)) - return err; - - /* The back-references which are in initial states can epsilon transit, - since in this case all of the subexpressions can be null. - Then we add epsilon closures of the nodes which are the next nodes of - the back-references. */ - if (dfa->nbackref > 0) - for (i = 0; i < init_nodes.nelem; ++i) - { - int node_idx = init_nodes.elems[i]; - re_token_type_t type = dfa->nodes[node_idx].type; - - int clexp_idx; - if (type != OP_BACK_REF) - continue; - for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx) - { - re_token_t *clexp_node; - clexp_node = dfa->nodes + init_nodes.elems[clexp_idx]; - if (clexp_node->type == OP_CLOSE_SUBEXP - && clexp_node->opr.idx == dfa->nodes[node_idx].opr.idx) - break; - } - if (clexp_idx == init_nodes.nelem) - continue; - - if (type == OP_BACK_REF) - { - int dest_idx = dfa->edests[node_idx].elems[0]; - if (!re_node_set_contains (&init_nodes, dest_idx)) - { - reg_errcode_t err = re_node_set_merge (&init_nodes, - dfa->eclosures - + dest_idx); - if (err != REG_NOERROR) - return err; - i = 0; - } - } - } - - /* It must be the first time to invoke acquire_state. */ - dfa->init_state = re_acquire_state_context (&err, dfa, &init_nodes, 0); - /* We don't check ERR here, since the initial state must not be NULL. */ - if (BE (dfa->init_state == NULL, 0)) - return err; - if (dfa->init_state->has_constraint) - { - dfa->init_state_word = re_acquire_state_context (&err, dfa, &init_nodes, - CONTEXT_WORD); - dfa->init_state_nl = re_acquire_state_context (&err, dfa, &init_nodes, - CONTEXT_NEWLINE); - dfa->init_state_begbuf = re_acquire_state_context (&err, dfa, - &init_nodes, - CONTEXT_NEWLINE - | CONTEXT_BEGBUF); - if (BE (dfa->init_state_word == NULL || dfa->init_state_nl == NULL - || dfa->init_state_begbuf == NULL, 0)) - return err; - } - else - dfa->init_state_word = dfa->init_state_nl - = dfa->init_state_begbuf = dfa->init_state; - - re_node_set_free (&init_nodes); - return REG_NOERROR; -} - -#ifdef RE_ENABLE_I18N -/* If it is possible to do searching in single byte encoding instead of UTF-8 - to speed things up, set dfa->mb_cur_max to 1, clear is_utf8 and change - DFA nodes where needed. */ - -static void -optimize_utf8 (re_dfa_t *dfa) -{ - int node, i, mb_chars = 0, has_period = 0; - - for (node = 0; node < dfa->nodes_len; ++node) - switch (dfa->nodes[node].type) - { - case CHARACTER: - if (dfa->nodes[node].opr.c >= 0x80) - mb_chars = 1; - break; - case ANCHOR: - switch (dfa->nodes[node].opr.ctx_type) - { - case LINE_FIRST: - case LINE_LAST: - case BUF_FIRST: - case BUF_LAST: - break; - default: - /* Word anchors etc. cannot be handled. It's okay to test - opr.ctx_type since constraints (for all DFA nodes) are - created by ORing one or more opr.ctx_type values. */ - return; - } - break; - case OP_PERIOD: - has_period = 1; - break; - case OP_BACK_REF: - case OP_ALT: - case END_OF_RE: - case OP_DUP_ASTERISK: - case OP_OPEN_SUBEXP: - case OP_CLOSE_SUBEXP: - break; - case COMPLEX_BRACKET: - return; - case SIMPLE_BRACKET: - /* Just double check. The non-ASCII range starts at 0x80. */ - assert (0x80 % BITSET_WORD_BITS == 0); - for (i = 0x80 / BITSET_WORD_BITS; i < BITSET_WORDS; ++i) - if (dfa->nodes[node].opr.sbcset[i]) - return; - break; - default: - abort (); - } - - if (mb_chars || has_period) - for (node = 0; node < dfa->nodes_len; ++node) - { - if (dfa->nodes[node].type == CHARACTER - && dfa->nodes[node].opr.c >= 0x80) - dfa->nodes[node].mb_partial = 0; - else if (dfa->nodes[node].type == OP_PERIOD) - dfa->nodes[node].type = OP_UTF8_PERIOD; - } - - /* The search can be in single byte locale. */ - dfa->mb_cur_max = 1; - dfa->is_utf8 = 0; - dfa->has_mb_node = dfa->nbackref > 0 || has_period; -} -#endif - -/* Analyze the structure tree, and calculate "first", "next", "edest", - "eclosure", and "inveclosure". */ - -static reg_errcode_t -analyze (regex_t *preg) -{ - re_dfa_t *dfa = (re_dfa_t *) preg->buffer; - reg_errcode_t ret; - - /* Allocate arrays. */ - dfa->nexts = re_malloc (int, dfa->nodes_alloc); - dfa->org_indices = re_malloc (int, dfa->nodes_alloc); - dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc); - dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc); - if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL - || dfa->eclosures == NULL, 0)) - return REG_ESPACE; - - dfa->subexp_map = re_malloc (int, preg->re_nsub); - if (dfa->subexp_map != NULL) - { - unsigned int i; - for (i = 0; i < preg->re_nsub; i++) - dfa->subexp_map[i] = i; - preorder (dfa->str_tree, optimize_subexps, dfa); - for (i = 0; i < preg->re_nsub; i++) - if (dfa->subexp_map[i] != (int)i) - break; - if (i == preg->re_nsub) - { - free (dfa->subexp_map); - dfa->subexp_map = NULL; - } - } - - ret = postorder (dfa->str_tree, lower_subexps, preg); - if (BE (ret != REG_NOERROR, 0)) - return ret; - ret = postorder (dfa->str_tree, calc_first, dfa); - if (BE (ret != REG_NOERROR, 0)) - return ret; - preorder (dfa->str_tree, calc_next, dfa); - ret = preorder (dfa->str_tree, link_nfa_nodes, dfa); - if (BE (ret != REG_NOERROR, 0)) - return ret; - ret = calc_eclosure (dfa); - if (BE (ret != REG_NOERROR, 0)) - return ret; - - /* We only need this during the prune_impossible_nodes pass in regexec.c; - skip it if p_i_n will not run, as calc_inveclosure can be quadratic. */ - if ((!preg->no_sub && preg->re_nsub > 0 && dfa->has_plural_match) - || dfa->nbackref) - { - dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_len); - if (BE (dfa->inveclosures == NULL, 0)) - return REG_ESPACE; - ret = calc_inveclosure (dfa); - } - - return ret; -} - -/* Our parse trees are very unbalanced, so we cannot use a stack to - implement parse tree visits. Instead, we use parent pointers and - some hairy code in these two functions. */ -static reg_errcode_t -postorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)), - void *extra) -{ - bin_tree_t *node, *prev; - - for (node = root; ; ) - { - /* Descend down the tree, preferably to the left (or to the right - if that's the only child). */ - while (node->left || node->right) - if (node->left) - node = node->left; - else - node = node->right; - - do - { - reg_errcode_t err = fn (extra, node); - if (BE (err != REG_NOERROR, 0)) - return err; - if (node->parent == NULL) - return REG_NOERROR; - prev = node; - node = node->parent; - } - /* Go up while we have a node that is reached from the right. */ - while (node->right == prev || node->right == NULL); - node = node->right; - } -} - -static reg_errcode_t -preorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)), - void *extra) -{ - bin_tree_t *node; - - for (node = root; ; ) - { - reg_errcode_t err = fn (extra, node); - if (BE (err != REG_NOERROR, 0)) - return err; - - /* Go to the left node, or up and to the right. */ - if (node->left) - node = node->left; - else - { - bin_tree_t *prev = NULL; - while (node->right == prev || node->right == NULL) - { - prev = node; - node = node->parent; - if (!node) - return REG_NOERROR; - } - node = node->right; - } - } -} - -/* Optimization pass: if a SUBEXP is entirely contained, strip it and tell - re_search_internal to map the inner one's opr.idx to this one's. Adjust - backreferences as well. Requires a preorder visit. */ -static reg_errcode_t -optimize_subexps (void *extra, bin_tree_t *node) -{ - re_dfa_t *dfa = (re_dfa_t *) extra; - - if (node->token.type == OP_BACK_REF && dfa->subexp_map) - { - int idx = node->token.opr.idx; - node->token.opr.idx = dfa->subexp_map[idx]; - dfa->used_bkref_map |= 1 << node->token.opr.idx; - } - - else if (node->token.type == SUBEXP - && node->left && node->left->token.type == SUBEXP) - { - int other_idx = node->left->token.opr.idx; - - node->left = node->left->left; - if (node->left) - node->left->parent = node; - - dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx]; - if (other_idx < BITSET_WORD_BITS) - dfa->used_bkref_map &= ~((bitset_word_t) 1 << other_idx); - } - - return REG_NOERROR; -} - -/* Lowering pass: Turn each SUBEXP node into the appropriate concatenation - of OP_OPEN_SUBEXP, the body of the SUBEXP (if any) and OP_CLOSE_SUBEXP. */ -static reg_errcode_t -lower_subexps (void *extra, bin_tree_t *node) -{ - regex_t *preg = (regex_t *) extra; - reg_errcode_t err = REG_NOERROR; - - if (node->left && node->left->token.type == SUBEXP) - { - node->left = lower_subexp (&err, preg, node->left); - if (node->left) - node->left->parent = node; - } - if (node->right && node->right->token.type == SUBEXP) - { - node->right = lower_subexp (&err, preg, node->right); - if (node->right) - node->right->parent = node; - } - - return err; -} - -static bin_tree_t * -lower_subexp (reg_errcode_t *err, regex_t *preg, bin_tree_t *node) -{ - re_dfa_t *dfa = (re_dfa_t *) preg->buffer; - bin_tree_t *body = node->left; - bin_tree_t *op, *cls, *tree1, *tree; - - if (preg->no_sub - /* We do not optimize empty subexpressions, because otherwise we may - have bad CONCAT nodes with NULL children. This is obviously not - very common, so we do not lose much. An example that triggers - this case is the sed "script" /\(\)/x. */ - && node->left != NULL - && (node->token.opr.idx >= BITSET_WORD_BITS - || !(dfa->used_bkref_map - & ((bitset_word_t) 1 << node->token.opr.idx)))) - return node->left; - - /* Convert the SUBEXP node to the concatenation of an - OP_OPEN_SUBEXP, the contents, and an OP_CLOSE_SUBEXP. */ - op = create_tree (dfa, NULL, NULL, OP_OPEN_SUBEXP); - cls = create_tree (dfa, NULL, NULL, OP_CLOSE_SUBEXP); - tree1 = body ? create_tree (dfa, body, cls, CONCAT) : cls; - tree = create_tree (dfa, op, tree1, CONCAT); - if (BE (tree == NULL || tree1 == NULL || op == NULL || cls == NULL, 0)) - { - *err = REG_ESPACE; - return NULL; - } - - op->token.opr.idx = cls->token.opr.idx = node->token.opr.idx; - op->token.opt_subexp = cls->token.opt_subexp = node->token.opt_subexp; - return tree; -} - -/* Pass 1 in building the NFA: compute FIRST and create unlinked automaton - nodes. Requires a postorder visit. */ -static reg_errcode_t -calc_first (void *extra, bin_tree_t *node) -{ - re_dfa_t *dfa = (re_dfa_t *) extra; - if (node->token.type == CONCAT) - { - node->first = node->left->first; - node->node_idx = node->left->node_idx; - } - else - { - node->first = node; - node->node_idx = re_dfa_add_node (dfa, node->token); - if (BE (node->node_idx == -1, 0)) - return REG_ESPACE; - if (node->token.type == ANCHOR) - dfa->nodes[node->node_idx].constraint = node->token.opr.ctx_type; - } - return REG_NOERROR; -} - -/* Pass 2: compute NEXT on the tree. Preorder visit. */ -static reg_errcode_t -calc_next (UNUSED void *extra, bin_tree_t *node) -{ - switch (node->token.type) - { - case OP_DUP_ASTERISK: - node->left->next = node; - break; - case CONCAT: - node->left->next = node->right->first; - node->right->next = node->next; - break; - default: - if (node->left) - node->left->next = node->next; - if (node->right) - node->right->next = node->next; - break; - } - return REG_NOERROR; -} - -/* Pass 3: link all DFA nodes to their NEXT node (any order will do). */ -static reg_errcode_t -link_nfa_nodes (void *extra, bin_tree_t *node) -{ - re_dfa_t *dfa = (re_dfa_t *) extra; - int idx = node->node_idx; - reg_errcode_t err = REG_NOERROR; - - switch (node->token.type) - { - case CONCAT: - break; - - case END_OF_RE: - assert (node->next == NULL); - break; - - case OP_DUP_ASTERISK: - case OP_ALT: - { - int left, right; - dfa->has_plural_match = 1; - if (node->left != NULL) - left = node->left->first->node_idx; - else - left = node->next->node_idx; - if (node->right != NULL) - right = node->right->first->node_idx; - else - right = node->next->node_idx; - assert (left > -1); - assert (right > -1); - err = re_node_set_init_2 (dfa->edests + idx, left, right); - } - break; - - case ANCHOR: - case OP_OPEN_SUBEXP: - case OP_CLOSE_SUBEXP: - err = re_node_set_init_1 (dfa->edests + idx, node->next->node_idx); - break; - - case OP_BACK_REF: - dfa->nexts[idx] = node->next->node_idx; - if (node->token.type == OP_BACK_REF) - err = re_node_set_init_1 (dfa->edests + idx, dfa->nexts[idx]); - break; - - default: - assert (!IS_EPSILON_NODE (node->token.type)); - dfa->nexts[idx] = node->next->node_idx; - break; - } - - return err; -} - -/* Duplicate the epsilon closure of the node ROOT_NODE. - Note that duplicated nodes have constraint INIT_CONSTRAINT in addition - to their own constraint. */ - -static reg_errcode_t -internal_function -duplicate_node_closure (re_dfa_t *dfa, int top_org_node, int top_clone_node, - int root_node, unsigned int init_constraint) -{ - int org_node, clone_node, ret; - unsigned int constraint = init_constraint; - for (org_node = top_org_node, clone_node = top_clone_node;;) - { - int org_dest, clone_dest; - if (dfa->nodes[org_node].type == OP_BACK_REF) - { - /* If the back reference epsilon-transit, its destination must - also have the constraint. Then duplicate the epsilon closure - of the destination of the back reference, and store it in - edests of the back reference. */ - org_dest = dfa->nexts[org_node]; - re_node_set_empty (dfa->edests + clone_node); - clone_dest = duplicate_node (dfa, org_dest, constraint); - if (BE (clone_dest == -1, 0)) - return REG_ESPACE; - dfa->nexts[clone_node] = dfa->nexts[org_node]; - ret = re_node_set_insert (dfa->edests + clone_node, clone_dest); - if (BE (ret < 0, 0)) - return REG_ESPACE; - } - else if (dfa->edests[org_node].nelem == 0) - { - /* In case of the node can't epsilon-transit, don't duplicate the - destination and store the original destination as the - destination of the node. */ - dfa->nexts[clone_node] = dfa->nexts[org_node]; - break; - } - else if (dfa->edests[org_node].nelem == 1) - { - /* In case of the node can epsilon-transit, and it has only one - destination. */ - org_dest = dfa->edests[org_node].elems[0]; - re_node_set_empty (dfa->edests + clone_node); - /* If the node is root_node itself, it means the epsilon clsoure - has a loop. Then tie it to the destination of the root_node. */ - if (org_node == root_node && clone_node != org_node) - { - ret = re_node_set_insert (dfa->edests + clone_node, org_dest); - if (BE (ret < 0, 0)) - return REG_ESPACE; - break; - } - /* In case of the node has another constraint, add it. */ - constraint |= dfa->nodes[org_node].constraint; - clone_dest = duplicate_node (dfa, org_dest, constraint); - if (BE (clone_dest == -1, 0)) - return REG_ESPACE; - ret = re_node_set_insert (dfa->edests + clone_node, clone_dest); - if (BE (ret < 0, 0)) - return REG_ESPACE; - } - else /* dfa->edests[org_node].nelem == 2 */ - { - /* In case of the node can epsilon-transit, and it has two - destinations. In the bin_tree_t and DFA, that's '|' and '*'. */ - org_dest = dfa->edests[org_node].elems[0]; - re_node_set_empty (dfa->edests + clone_node); - /* Search for a duplicated node which satisfies the constraint. */ - clone_dest = search_duplicated_node (dfa, org_dest, constraint); - if (clone_dest == -1) - { - /* There is no such duplicated node, create a new one. */ - reg_errcode_t err; - clone_dest = duplicate_node (dfa, org_dest, constraint); - if (BE (clone_dest == -1, 0)) - return REG_ESPACE; - ret = re_node_set_insert (dfa->edests + clone_node, clone_dest); - if (BE (ret < 0, 0)) - return REG_ESPACE; - err = duplicate_node_closure (dfa, org_dest, clone_dest, - root_node, constraint); - if (BE (err != REG_NOERROR, 0)) - return err; - } - else - { - /* There is a duplicated node which satisfies the constraint, - use it to avoid infinite loop. */ - ret = re_node_set_insert (dfa->edests + clone_node, clone_dest); - if (BE (ret < 0, 0)) - return REG_ESPACE; - } - - org_dest = dfa->edests[org_node].elems[1]; - clone_dest = duplicate_node (dfa, org_dest, constraint); - if (BE (clone_dest == -1, 0)) - return REG_ESPACE; - ret = re_node_set_insert (dfa->edests + clone_node, clone_dest); - if (BE (ret < 0, 0)) - return REG_ESPACE; - } - org_node = org_dest; - clone_node = clone_dest; - } - return REG_NOERROR; -} - -/* Search for a node which is duplicated from the node ORG_NODE, and - satisfies the constraint CONSTRAINT. */ - -static int -search_duplicated_node (const re_dfa_t *dfa, int org_node, - unsigned int constraint) -{ - int idx; - for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx) - { - if (org_node == dfa->org_indices[idx] - && constraint == dfa->nodes[idx].constraint) - return idx; /* Found. */ - } - return -1; /* Not found. */ -} - -/* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT. - Return the index of the new node, or -1 if insufficient storage is - available. */ - -static int -duplicate_node (re_dfa_t *dfa, int org_idx, unsigned int constraint) -{ - int dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]); - if (BE (dup_idx != -1, 1)) - { - dfa->nodes[dup_idx].constraint = constraint; - dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].constraint; - dfa->nodes[dup_idx].duplicated = 1; - - /* Store the index of the original node. */ - dfa->org_indices[dup_idx] = org_idx; - } - return dup_idx; -} - -static reg_errcode_t -calc_inveclosure (re_dfa_t *dfa) -{ - int ret; - unsigned int src, idx; - for (idx = 0; idx < dfa->nodes_len; ++idx) - re_node_set_init_empty (dfa->inveclosures + idx); - - for (src = 0; src < dfa->nodes_len; ++src) - { - int *elems = dfa->eclosures[src].elems; - int idx; - for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx) - { - ret = re_node_set_insert_last (dfa->inveclosures + elems[idx], src); - if (BE (ret == -1, 0)) - return REG_ESPACE; - } - } - - return REG_NOERROR; -} - -/* Calculate "eclosure" for all the node in DFA. */ - -static reg_errcode_t -calc_eclosure (re_dfa_t *dfa) -{ - size_t node_idx; - int incomplete; -#ifdef DEBUG - assert (dfa->nodes_len > 0); -#endif - incomplete = 0; - /* For each nodes, calculate epsilon closure. */ - for (node_idx = 0; ; ++node_idx) - { - reg_errcode_t err; - re_node_set eclosure_elem; - if (node_idx == dfa->nodes_len) - { - if (!incomplete) - break; - incomplete = 0; - node_idx = 0; - } - -#ifdef DEBUG - assert (dfa->eclosures[node_idx].nelem != -1); -#endif - - /* If we have already calculated, skip it. */ - if (dfa->eclosures[node_idx].nelem != 0) - continue; - /* Calculate epsilon closure of `node_idx'. */ - err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, 1); - if (BE (err != REG_NOERROR, 0)) - return err; - - if (dfa->eclosures[node_idx].nelem == 0) - { - incomplete = 1; - re_node_set_free (&eclosure_elem); - } - } - return REG_NOERROR; -} - -/* Calculate epsilon closure of NODE. */ - -static reg_errcode_t -calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, int node, int root) -{ - reg_errcode_t err; - int i; - re_node_set eclosure; - int ret; - int incomplete = 0; - err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1); - if (BE (err != REG_NOERROR, 0)) - return err; - - /* This indicates that we are calculating this node now. - We reference this value to avoid infinite loop. */ - dfa->eclosures[node].nelem = -1; - - /* If the current node has constraints, duplicate all nodes - since they must inherit the constraints. */ - if (dfa->nodes[node].constraint - && dfa->edests[node].nelem - && !dfa->nodes[dfa->edests[node].elems[0]].duplicated) - { - err = duplicate_node_closure (dfa, node, node, node, - dfa->nodes[node].constraint); - if (BE (err != REG_NOERROR, 0)) - return err; - } - - /* Expand each epsilon destination nodes. */ - if (IS_EPSILON_NODE(dfa->nodes[node].type)) - for (i = 0; i < dfa->edests[node].nelem; ++i) - { - re_node_set eclosure_elem; - int edest = dfa->edests[node].elems[i]; - /* If calculating the epsilon closure of `edest' is in progress, - return intermediate result. */ - if (dfa->eclosures[edest].nelem == -1) - { - incomplete = 1; - continue; - } - /* If we haven't calculated the epsilon closure of `edest' yet, - calculate now. Otherwise use calculated epsilon closure. */ - if (dfa->eclosures[edest].nelem == 0) - { - err = calc_eclosure_iter (&eclosure_elem, dfa, edest, 0); - if (BE (err != REG_NOERROR, 0)) - return err; - } - else - eclosure_elem = dfa->eclosures[edest]; - /* Merge the epsilon closure of `edest'. */ - err = re_node_set_merge (&eclosure, &eclosure_elem); - if (BE (err != REG_NOERROR, 0)) - return err; - /* If the epsilon closure of `edest' is incomplete, - the epsilon closure of this node is also incomplete. */ - if (dfa->eclosures[edest].nelem == 0) - { - incomplete = 1; - re_node_set_free (&eclosure_elem); - } - } - - /* An epsilon closure includes itself. */ - ret = re_node_set_insert (&eclosure, node); - if (BE (ret < 0, 0)) - return REG_ESPACE; - if (incomplete && !root) - dfa->eclosures[node].nelem = 0; - else - dfa->eclosures[node] = eclosure; - *new_set = eclosure; - return REG_NOERROR; -} - -/* Functions for token which are used in the parser. */ - -/* Fetch a token from INPUT. - We must not use this function inside bracket expressions. */ - -static void -internal_function -fetch_token (re_token_t *result, re_string_t *input, reg_syntax_t syntax) -{ - re_string_skip_bytes (input, peek_token (result, input, syntax)); -} - -/* Peek a token from INPUT, and return the length of the token. - We must not use this function inside bracket expressions. */ - -static int -internal_function -peek_token (re_token_t *token, re_string_t *input, reg_syntax_t syntax) -{ - unsigned char c; - - if (re_string_eoi (input)) - { - token->type = END_OF_RE; - return 0; - } - - c = re_string_peek_byte (input, 0); - token->opr.c = c; - - token->word_char = 0; -#ifdef RE_ENABLE_I18N - token->mb_partial = 0; - if (input->mb_cur_max > 1 && - !re_string_first_byte (input, re_string_cur_idx (input))) - { - token->type = CHARACTER; - token->mb_partial = 1; - return 1; - } -#endif - if (c == '\\') - { - unsigned char c2; - if (re_string_cur_idx (input) + 1 >= re_string_length (input)) - { - token->type = BACK_SLASH; - return 1; - } - - c2 = re_string_peek_byte_case (input, 1); - token->opr.c = c2; - token->type = CHARACTER; -#ifdef RE_ENABLE_I18N - if (input->mb_cur_max > 1) - { - wint_t wc = re_string_wchar_at (input, - re_string_cur_idx (input) + 1); - token->word_char = IS_WIDE_WORD_CHAR (wc) != 0; - } - else -#endif - token->word_char = IS_WORD_CHAR (c2) != 0; - - switch (c2) - { - case '|': - if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_NO_BK_VBAR)) - token->type = OP_ALT; - break; - case '1': case '2': case '3': case '4': case '5': - case '6': case '7': case '8': case '9': - if (!(syntax & RE_NO_BK_REFS)) - { - token->type = OP_BACK_REF; - token->opr.idx = c2 - '1'; - } - break; - case '<': - if (!(syntax & RE_NO_GNU_OPS)) - { - token->type = ANCHOR; - token->opr.ctx_type = WORD_FIRST; - } - break; - case '>': - if (!(syntax & RE_NO_GNU_OPS)) - { - token->type = ANCHOR; - token->opr.ctx_type = WORD_LAST; - } - break; - case 'b': - if (!(syntax & RE_NO_GNU_OPS)) - { - token->type = ANCHOR; - token->opr.ctx_type = WORD_DELIM; - } - break; - case 'B': - if (!(syntax & RE_NO_GNU_OPS)) - { - token->type = ANCHOR; - token->opr.ctx_type = NOT_WORD_DELIM; - } - break; - case 'w': - if (!(syntax & RE_NO_GNU_OPS)) - token->type = OP_WORD; - break; - case 'W': - if (!(syntax & RE_NO_GNU_OPS)) - token->type = OP_NOTWORD; - break; - case 's': - if (!(syntax & RE_NO_GNU_OPS)) - token->type = OP_SPACE; - break; - case 'S': - if (!(syntax & RE_NO_GNU_OPS)) - token->type = OP_NOTSPACE; - break; - case '`': - if (!(syntax & RE_NO_GNU_OPS)) - { - token->type = ANCHOR; - token->opr.ctx_type = BUF_FIRST; - } - break; - case '\'': - if (!(syntax & RE_NO_GNU_OPS)) - { - token->type = ANCHOR; - token->opr.ctx_type = BUF_LAST; - } - break; - case '(': - if (!(syntax & RE_NO_BK_PARENS)) - token->type = OP_OPEN_SUBEXP; - break; - case ')': - if (!(syntax & RE_NO_BK_PARENS)) - token->type = OP_CLOSE_SUBEXP; - break; - case '+': - if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM)) - token->type = OP_DUP_PLUS; - break; - case '?': - if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM)) - token->type = OP_DUP_QUESTION; - break; - case '{': - if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES))) - token->type = OP_OPEN_DUP_NUM; - break; - case '}': - if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES))) - token->type = OP_CLOSE_DUP_NUM; - break; - default: - break; - } - return 2; - } - - token->type = CHARACTER; -#ifdef RE_ENABLE_I18N - if (input->mb_cur_max > 1) - { - wint_t wc = re_string_wchar_at (input, re_string_cur_idx (input)); - token->word_char = IS_WIDE_WORD_CHAR (wc) != 0; - } - else -#endif - token->word_char = IS_WORD_CHAR (token->opr.c); - - switch (c) - { - case '\n': - if (syntax & RE_NEWLINE_ALT) - token->type = OP_ALT; - break; - case '|': - if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_NO_BK_VBAR)) - token->type = OP_ALT; - break; - case '*': - token->type = OP_DUP_ASTERISK; - break; - case '+': - if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM)) - token->type = OP_DUP_PLUS; - break; - case '?': - if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM)) - token->type = OP_DUP_QUESTION; - break; - case '{': - if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES)) - token->type = OP_OPEN_DUP_NUM; - break; - case '}': - if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES)) - token->type = OP_CLOSE_DUP_NUM; - break; - case '(': - if (syntax & RE_NO_BK_PARENS) - token->type = OP_OPEN_SUBEXP; - break; - case ')': - if (syntax & RE_NO_BK_PARENS) - token->type = OP_CLOSE_SUBEXP; - break; - case '[': - token->type = OP_OPEN_BRACKET; - break; - case '.': - token->type = OP_PERIOD; - break; - case '^': - if (!(syntax & (RE_CONTEXT_INDEP_ANCHORS | RE_CARET_ANCHORS_HERE)) && - re_string_cur_idx (input) != 0) - { - char prev = re_string_peek_byte (input, -1); - if (!(syntax & RE_NEWLINE_ALT) || prev != '\n') - break; - } - token->type = ANCHOR; - token->opr.ctx_type = LINE_FIRST; - break; - case '$': - if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) && - re_string_cur_idx (input) + 1 != re_string_length (input)) - { - re_token_t next; - re_string_skip_bytes (input, 1); - peek_token (&next, input, syntax); - re_string_skip_bytes (input, -1); - if (next.type != OP_ALT && next.type != OP_CLOSE_SUBEXP) - break; - } - token->type = ANCHOR; - token->opr.ctx_type = LINE_LAST; - break; - default: - break; - } - return 1; -} - -/* Peek a token from INPUT, and return the length of the token. - We must not use this function out of bracket expressions. */ - -static int -internal_function -peek_token_bracket (re_token_t *token, re_string_t *input, reg_syntax_t syntax) -{ - unsigned char c; - if (re_string_eoi (input)) - { - token->type = END_OF_RE; - return 0; - } - c = re_string_peek_byte (input, 0); - token->opr.c = c; - -#ifdef RE_ENABLE_I18N - if (input->mb_cur_max > 1 && - !re_string_first_byte (input, re_string_cur_idx (input))) - { - token->type = CHARACTER; - return 1; - } -#endif /* RE_ENABLE_I18N */ - - if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) - && re_string_cur_idx (input) + 1 < re_string_length (input)) - { - /* In this case, '\' escape a character. */ - unsigned char c2; - re_string_skip_bytes (input, 1); - c2 = re_string_peek_byte (input, 0); - token->opr.c = c2; - token->type = CHARACTER; - return 1; - } - if (c == '[') /* '[' is a special char in a bracket exps. */ - { - unsigned char c2; - int token_len; - if (re_string_cur_idx (input) + 1 < re_string_length (input)) - c2 = re_string_peek_byte (input, 1); - else - c2 = 0; - token->opr.c = c2; - token_len = 2; - switch (c2) - { - case '.': - token->type = OP_OPEN_COLL_ELEM; - break; - case '=': - token->type = OP_OPEN_EQUIV_CLASS; - break; - case ':': - if (syntax & RE_CHAR_CLASSES) - { - token->type = OP_OPEN_CHAR_CLASS; - break; - } - /* else fall through. */ - default: - token->type = CHARACTER; - token->opr.c = c; - token_len = 1; - break; - } - return token_len; - } - switch (c) - { - case '-': - token->type = OP_CHARSET_RANGE; - break; - case ']': - token->type = OP_CLOSE_BRACKET; - break; - case '^': - token->type = OP_NON_MATCH_LIST; - break; - default: - token->type = CHARACTER; - } - return 1; -} - -/* Functions for parser. */ - -/* Entry point of the parser. - Parse the regular expression REGEXP and return the structure tree. - If an error is occured, ERR is set by error code, and return NULL. - This function build the following tree, from regular expression : - CAT - / \ - / \ - EOR - - CAT means concatenation. - EOR means end of regular expression. */ - -static bin_tree_t * -parse (re_string_t *regexp, regex_t *preg, reg_syntax_t syntax, - reg_errcode_t *err) -{ - re_dfa_t *dfa = (re_dfa_t *) preg->buffer; - bin_tree_t *tree, *eor, *root; - re_token_t current_token; - dfa->syntax = syntax; - fetch_token (¤t_token, regexp, syntax | RE_CARET_ANCHORS_HERE); - tree = parse_reg_exp (regexp, preg, ¤t_token, syntax, 0, err); - if (BE (*err != REG_NOERROR && tree == NULL, 0)) - return NULL; - eor = create_tree (dfa, NULL, NULL, END_OF_RE); - if (tree != NULL) - root = create_tree (dfa, tree, eor, CONCAT); - else - root = eor; - if (BE (eor == NULL || root == NULL, 0)) - { - *err = REG_ESPACE; - return NULL; - } - return root; -} - -/* This function build the following tree, from regular expression - |: - ALT - / \ - / \ - - - ALT means alternative, which represents the operator `|'. */ - -static bin_tree_t * -parse_reg_exp (re_string_t *regexp, regex_t *preg, re_token_t *token, - reg_syntax_t syntax, int nest, reg_errcode_t *err) -{ - re_dfa_t *dfa = (re_dfa_t *) preg->buffer; - bin_tree_t *tree, *branch = NULL; - tree = parse_branch (regexp, preg, token, syntax, nest, err); - if (BE (*err != REG_NOERROR && tree == NULL, 0)) - return NULL; - - while (token->type == OP_ALT) - { - fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE); - if (token->type != OP_ALT && token->type != END_OF_RE - && (nest == 0 || token->type != OP_CLOSE_SUBEXP)) - { - branch = parse_branch (regexp, preg, token, syntax, nest, err); - if (BE (*err != REG_NOERROR && branch == NULL, 0)) - return NULL; - } - else - branch = NULL; - tree = create_tree (dfa, tree, branch, OP_ALT); - if (BE (tree == NULL, 0)) - { - *err = REG_ESPACE; - return NULL; - } - } - return tree; -} - -/* This function build the following tree, from regular expression - : - CAT - / \ - / \ - - - CAT means concatenation. */ - -static bin_tree_t * -parse_branch (re_string_t *regexp, regex_t *preg, re_token_t *token, - reg_syntax_t syntax, int nest, reg_errcode_t *err) -{ - bin_tree_t *tree, *exp; - re_dfa_t *dfa = (re_dfa_t *) preg->buffer; - tree = parse_expression (regexp, preg, token, syntax, nest, err); - if (BE (*err != REG_NOERROR && tree == NULL, 0)) - return NULL; - - while (token->type != OP_ALT && token->type != END_OF_RE - && (nest == 0 || token->type != OP_CLOSE_SUBEXP)) - { - exp = parse_expression (regexp, preg, token, syntax, nest, err); - if (BE (*err != REG_NOERROR && exp == NULL, 0)) - { - return NULL; - } - if (tree != NULL && exp != NULL) - { - tree = create_tree (dfa, tree, exp, CONCAT); - if (tree == NULL) - { - *err = REG_ESPACE; - return NULL; - } - } - else if (tree == NULL) - tree = exp; - /* Otherwise exp == NULL, we don't need to create new tree. */ - } - return tree; -} - -/* This function build the following tree, from regular expression a*: - * - | - a -*/ - -static bin_tree_t * -parse_expression (re_string_t *regexp, regex_t *preg, re_token_t *token, - reg_syntax_t syntax, int nest, reg_errcode_t *err) -{ - re_dfa_t *dfa = (re_dfa_t *) preg->buffer; - bin_tree_t *tree; - switch (token->type) - { - case CHARACTER: - tree = create_token_tree (dfa, NULL, NULL, token); - if (BE (tree == NULL, 0)) - { - *err = REG_ESPACE; - return NULL; - } -#ifdef RE_ENABLE_I18N - if (dfa->mb_cur_max > 1) - { - while (!re_string_eoi (regexp) - && !re_string_first_byte (regexp, re_string_cur_idx (regexp))) - { - bin_tree_t *mbc_remain; - fetch_token (token, regexp, syntax); - mbc_remain = create_token_tree (dfa, NULL, NULL, token); - tree = create_tree (dfa, tree, mbc_remain, CONCAT); - if (BE (mbc_remain == NULL || tree == NULL, 0)) - { - *err = REG_ESPACE; - return NULL; - } - } - } -#endif - break; - case OP_OPEN_SUBEXP: - tree = parse_sub_exp (regexp, preg, token, syntax, nest + 1, err); - if (BE (*err != REG_NOERROR && tree == NULL, 0)) - return NULL; - break; - case OP_OPEN_BRACKET: - tree = parse_bracket_exp (regexp, dfa, token, syntax, err); - if (BE (*err != REG_NOERROR && tree == NULL, 0)) - return NULL; - break; - case OP_BACK_REF: - if (!BE (dfa->completed_bkref_map & (1 << token->opr.idx), 1)) - { - *err = REG_ESUBREG; - return NULL; - } - dfa->used_bkref_map |= 1 << token->opr.idx; - tree = create_token_tree (dfa, NULL, NULL, token); - if (BE (tree == NULL, 0)) - { - *err = REG_ESPACE; - return NULL; - } - ++dfa->nbackref; - dfa->has_mb_node = 1; - break; - case OP_OPEN_DUP_NUM: - if (syntax & RE_CONTEXT_INVALID_DUP) - { - *err = REG_BADRPT; - return NULL; - } - /* FALLTHROUGH */ - case OP_DUP_ASTERISK: - case OP_DUP_PLUS: - case OP_DUP_QUESTION: - if (syntax & RE_CONTEXT_INVALID_OPS) - { - *err = REG_BADRPT; - return NULL; - } - else if (syntax & RE_CONTEXT_INDEP_OPS) - { - fetch_token (token, regexp, syntax); - return parse_expression (regexp, preg, token, syntax, nest, err); - } - /* else fall through */ - case OP_CLOSE_SUBEXP: - if ((token->type == OP_CLOSE_SUBEXP) && - !(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)) - { - *err = REG_ERPAREN; - return NULL; - } - /* else fall through */ - case OP_CLOSE_DUP_NUM: - /* We treat it as a normal character. */ - - /* Then we can these characters as normal characters. */ - token->type = CHARACTER; - /* mb_partial and word_char bits should be initialized already - by peek_token. */ - tree = create_token_tree (dfa, NULL, NULL, token); - if (BE (tree == NULL, 0)) - { - *err = REG_ESPACE; - return NULL; - } - break; - case ANCHOR: - if ((token->opr.ctx_type - & (WORD_DELIM | NOT_WORD_DELIM | WORD_FIRST | WORD_LAST)) - && dfa->word_ops_used == 0) - init_word_char (dfa); - if (token->opr.ctx_type == WORD_DELIM - || token->opr.ctx_type == NOT_WORD_DELIM) - { - bin_tree_t *tree_first, *tree_last; - if (token->opr.ctx_type == WORD_DELIM) - { - token->opr.ctx_type = WORD_FIRST; - tree_first = create_token_tree (dfa, NULL, NULL, token); - token->opr.ctx_type = WORD_LAST; - } - else - { - token->opr.ctx_type = INSIDE_WORD; - tree_first = create_token_tree (dfa, NULL, NULL, token); - token->opr.ctx_type = INSIDE_NOTWORD; - } - tree_last = create_token_tree (dfa, NULL, NULL, token); - tree = create_tree (dfa, tree_first, tree_last, OP_ALT); - if (BE (tree_first == NULL || tree_last == NULL || tree == NULL, 0)) - { - *err = REG_ESPACE; - return NULL; - } - } - else - { - tree = create_token_tree (dfa, NULL, NULL, token); - if (BE (tree == NULL, 0)) - { - *err = REG_ESPACE; - return NULL; - } - } - /* We must return here, since ANCHORs can't be followed - by repetition operators. - eg. RE"^*" is invalid or "", - it must not be "". */ - fetch_token (token, regexp, syntax); - return tree; - case OP_PERIOD: - tree = create_token_tree (dfa, NULL, NULL, token); - if (BE (tree == NULL, 0)) - { - *err = REG_ESPACE; - return NULL; - } - if (dfa->mb_cur_max > 1) - dfa->has_mb_node = 1; - break; - case OP_WORD: - case OP_NOTWORD: - tree = build_charclass_op (dfa, regexp->trans, - "alnum", - "_", - token->type == OP_NOTWORD, err); - if (BE (*err != REG_NOERROR && tree == NULL, 0)) - return NULL; - break; - case OP_SPACE: - case OP_NOTSPACE: - tree = build_charclass_op (dfa, regexp->trans, - "space", - "", - token->type == OP_NOTSPACE, err); - if (BE (*err != REG_NOERROR && tree == NULL, 0)) - return NULL; - break; - case OP_ALT: - case END_OF_RE: - return NULL; - case BACK_SLASH: - *err = REG_EESCAPE; - return NULL; - default: - /* Must not happen? */ -#ifdef DEBUG - assert (0); -#endif - return NULL; - } - fetch_token (token, regexp, syntax); - - while (token->type == OP_DUP_ASTERISK || token->type == OP_DUP_PLUS - || token->type == OP_DUP_QUESTION || token->type == OP_OPEN_DUP_NUM) - { - tree = parse_dup_op (tree, regexp, dfa, token, syntax, err); - if (BE (*err != REG_NOERROR && tree == NULL, 0)) - return NULL; - /* In BRE consecutive duplications are not allowed. */ - if ((syntax & RE_CONTEXT_INVALID_DUP) - && (token->type == OP_DUP_ASTERISK - || token->type == OP_OPEN_DUP_NUM)) - { - *err = REG_BADRPT; - return NULL; - } - } - - return tree; -} - -/* This function build the following tree, from regular expression - (): - SUBEXP - | - -*/ - -static bin_tree_t * -parse_sub_exp (re_string_t *regexp, regex_t *preg, re_token_t *token, - reg_syntax_t syntax, int nest, reg_errcode_t *err) -{ - re_dfa_t *dfa = (re_dfa_t *) preg->buffer; - bin_tree_t *tree; - size_t cur_nsub; - cur_nsub = preg->re_nsub++; - - fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE); - - /* The subexpression may be a null string. */ - if (token->type == OP_CLOSE_SUBEXP) - tree = NULL; - else - { - tree = parse_reg_exp (regexp, preg, token, syntax, nest, err); - if (BE (*err == REG_NOERROR && token->type != OP_CLOSE_SUBEXP, 0)) - *err = REG_EPAREN; - if (BE (*err != REG_NOERROR, 0)) - return NULL; - } - - if (cur_nsub <= '9' - '1') - dfa->completed_bkref_map |= 1 << cur_nsub; - - tree = create_tree (dfa, tree, NULL, SUBEXP); - if (BE (tree == NULL, 0)) - { - *err = REG_ESPACE; - return NULL; - } - tree->token.opr.idx = cur_nsub; - return tree; -} - -/* This function parse repetition operators like "*", "+", "{1,3}" etc. */ - -static bin_tree_t * -parse_dup_op (bin_tree_t *elem, re_string_t *regexp, re_dfa_t *dfa, - re_token_t *token, reg_syntax_t syntax, reg_errcode_t *err) -{ - bin_tree_t *tree = NULL, *old_tree = NULL; - int i, start, end, start_idx = re_string_cur_idx (regexp); -#ifndef RE_TOKEN_INIT_BUG - re_token_t start_token = *token; -#else - re_token_t start_token; - - memcpy ((void *) &start_token, (void *) token, sizeof start_token); -#endif - - if (token->type == OP_OPEN_DUP_NUM) - { - end = 0; - start = fetch_number (regexp, token, syntax); - if (start == -1) - { - if (token->type == CHARACTER && token->opr.c == ',') - start = 0; /* We treat "{,m}" as "{0,m}". */ - else - { - *err = REG_BADBR; /* {} is invalid. */ - return NULL; - } - } - if (BE (start != -2, 1)) - { - /* We treat "{n}" as "{n,n}". */ - end = ((token->type == OP_CLOSE_DUP_NUM) ? start - : ((token->type == CHARACTER && token->opr.c == ',') - ? fetch_number (regexp, token, syntax) : -2)); - } - if (BE (start == -2 || end == -2, 0)) - { - /* Invalid sequence. */ - if (BE (!(syntax & RE_INVALID_INTERVAL_ORD), 0)) - { - if (token->type == END_OF_RE) - *err = REG_EBRACE; - else - *err = REG_BADBR; - - return NULL; - } - - /* If the syntax bit is set, rollback. */ - re_string_set_index (regexp, start_idx); - *token = start_token; - token->type = CHARACTER; - /* mb_partial and word_char bits should be already initialized by - peek_token. */ - return elem; - } - - if (BE ((end != -1 && start > end) || token->type != OP_CLOSE_DUP_NUM, 0)) - { - /* First number greater than second. */ - *err = REG_BADBR; - return NULL; - } - } - else - { - start = (token->type == OP_DUP_PLUS) ? 1 : 0; - end = (token->type == OP_DUP_QUESTION) ? 1 : -1; - } - - fetch_token (token, regexp, syntax); - - if (BE (elem == NULL, 0)) - return NULL; - if (BE (start == 0 && end == 0, 0)) - { - postorder (elem, free_tree, NULL); - return NULL; - } - - /* Extract "{n,m}" to "...{0,}". */ - if (BE (start > 0, 0)) - { - tree = elem; - for (i = 2; i <= start; ++i) - { - elem = duplicate_tree (elem, dfa); - tree = create_tree (dfa, tree, elem, CONCAT); - if (BE (elem == NULL || tree == NULL, 0)) - goto parse_dup_op_espace; - } - - if (start == end) - return tree; - - /* Duplicate ELEM before it is marked optional. */ - elem = duplicate_tree (elem, dfa); - old_tree = tree; - } - else - old_tree = NULL; - - if (elem->token.type == SUBEXP) - postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx); - - tree = create_tree (dfa, elem, NULL, (end == -1 ? OP_DUP_ASTERISK : OP_ALT)); - if (BE (tree == NULL, 0)) - goto parse_dup_op_espace; - - /* This loop is actually executed only when end != -1, - to rewrite {0,n} as ((...?)?)?... We have - already created the start+1-th copy. */ - for (i = start + 2; i <= end; ++i) - { - elem = duplicate_tree (elem, dfa); - tree = create_tree (dfa, tree, elem, CONCAT); - if (BE (elem == NULL || tree == NULL, 0)) - goto parse_dup_op_espace; - - tree = create_tree (dfa, tree, NULL, OP_ALT); - if (BE (tree == NULL, 0)) - goto parse_dup_op_espace; - } - - if (old_tree) - tree = create_tree (dfa, old_tree, tree, CONCAT); - - return tree; - - parse_dup_op_espace: - *err = REG_ESPACE; - return NULL; -} - -/* Size of the names for collating symbol/equivalence_class/character_class. - I'm not sure, but maybe enough. */ -#define BRACKET_NAME_BUF_SIZE 32 - -#ifndef _LIBC - /* Local function for parse_bracket_exp only used in case of NOT _LIBC. - Build the range expression which starts from START_ELEM, and ends - at END_ELEM. The result are written to MBCSET and SBCSET. - RANGE_ALLOC is the allocated size of mbcset->range_starts, and - mbcset->range_ends, is a pointer argument sinse we may - update it. */ - -static reg_errcode_t -internal_function -# ifdef RE_ENABLE_I18N -build_range_exp (bitset_t sbcset, re_charset_t *mbcset, int *range_alloc, - bracket_elem_t *start_elem, bracket_elem_t *end_elem) -# else /* not RE_ENABLE_I18N */ -build_range_exp (bitset_t sbcset, bracket_elem_t *start_elem, - bracket_elem_t *end_elem) -# endif /* not RE_ENABLE_I18N */ -{ - unsigned int start_ch, end_ch; - /* Equivalence Classes and Character Classes can't be a range start/end. */ - if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS - || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS, - 0)) - return REG_ERANGE; - - /* We can handle no multi character collating elements without libc - support. */ - if (BE ((start_elem->type == COLL_SYM - && strlen ((char *) start_elem->opr.name) > 1) - || (end_elem->type == COLL_SYM - && strlen ((char *) end_elem->opr.name) > 1), 0)) - return REG_ECOLLATE; - -# ifdef RE_ENABLE_I18N - { - wchar_t wc; - wint_t start_wc; - wint_t end_wc; - wchar_t cmp_buf[6] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'}; - - start_ch = ((start_elem->type == SB_CHAR) ? start_elem->opr.ch - : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0] - : 0)); - end_ch = ((end_elem->type == SB_CHAR) ? end_elem->opr.ch - : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0] - : 0)); -#ifdef GAWK - /* - * Fedora Core 2, maybe others, have broken `btowc' that returns -1 - * for any value > 127. Sigh. Note that `start_ch' and `end_ch' are - * unsigned, so we don't have sign extension problems. - */ - start_wc = ((start_elem->type == SB_CHAR || start_elem->type == COLL_SYM) - ? start_ch : start_elem->opr.wch); - end_wc = ((end_elem->type == SB_CHAR || end_elem->type == COLL_SYM) - ? end_ch : end_elem->opr.wch); -#else - start_wc = ((start_elem->type == SB_CHAR || start_elem->type == COLL_SYM) - ? __btowc (start_ch) : start_elem->opr.wch); - end_wc = ((end_elem->type == SB_CHAR || end_elem->type == COLL_SYM) - ? __btowc (end_ch) : end_elem->opr.wch); -#endif - if (start_wc == WEOF || end_wc == WEOF) - return REG_ECOLLATE; - cmp_buf[0] = start_wc; - cmp_buf[4] = end_wc; - if (wcscoll (cmp_buf, cmp_buf + 4) > 0) - return REG_ERANGE; - - /* Got valid collation sequence values, add them as a new entry. - However, for !_LIBC we have no collation elements: if the - character set is single byte, the single byte character set - that we build below suffices. parse_bracket_exp passes - no MBCSET if dfa->mb_cur_max == 1. */ - if (mbcset) - { - /* Check the space of the arrays. */ - if (BE (*range_alloc == mbcset->nranges, 0)) - { - /* There is not enough space, need realloc. */ - wchar_t *new_array_start, *new_array_end; - int new_nranges; - - /* +1 in case of mbcset->nranges is 0. */ - new_nranges = 2 * mbcset->nranges + 1; - /* Use realloc since mbcset->range_starts and mbcset->range_ends - are NULL if *range_alloc == 0. */ - new_array_start = re_realloc (mbcset->range_starts, wchar_t, - new_nranges); - new_array_end = re_realloc (mbcset->range_ends, wchar_t, - new_nranges); - - if (BE (new_array_start == NULL || new_array_end == NULL, 0)) - return REG_ESPACE; - - mbcset->range_starts = new_array_start; - mbcset->range_ends = new_array_end; - *range_alloc = new_nranges; - } - - mbcset->range_starts[mbcset->nranges] = start_wc; - mbcset->range_ends[mbcset->nranges++] = end_wc; - } - - /* Build the table for single byte characters. */ - for (wc = 0; wc < SBC_MAX; ++wc) - { - cmp_buf[2] = wc; - if (wcscoll (cmp_buf, cmp_buf + 2) <= 0 - && wcscoll (cmp_buf + 2, cmp_buf + 4) <= 0) - bitset_set (sbcset, wc); - } - } -# else /* not RE_ENABLE_I18N */ - { - unsigned int ch; - start_ch = ((start_elem->type == SB_CHAR ) ? start_elem->opr.ch - : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0] - : 0)); - end_ch = ((end_elem->type == SB_CHAR ) ? end_elem->opr.ch - : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0] - : 0)); - if (start_ch > end_ch) - return REG_ERANGE; - /* Build the table for single byte characters. */ - for (ch = 0; ch < SBC_MAX; ++ch) - if (start_ch <= ch && ch <= end_ch) - bitset_set (sbcset, ch); - } -# endif /* not RE_ENABLE_I18N */ - return REG_NOERROR; -} -#endif /* not _LIBC */ - -#ifndef _LIBC -/* Helper function for parse_bracket_exp only used in case of NOT _LIBC.. - Build the collating element which is represented by NAME. - The result are written to MBCSET and SBCSET. - COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a - pointer argument since we may update it. */ - -static reg_errcode_t -internal_function -# ifdef RE_ENABLE_I18N -build_collating_symbol (bitset_t sbcset, re_charset_t *mbcset, - int *coll_sym_alloc, const unsigned char *name) -# else /* not RE_ENABLE_I18N */ -build_collating_symbol (bitset_t sbcset, const unsigned char *name) -# endif /* not RE_ENABLE_I18N */ -{ - size_t name_len = strlen ((const char *) name); - if (BE (name_len != 1, 0)) - return REG_ECOLLATE; - else - { - bitset_set (sbcset, name[0]); - return REG_NOERROR; - } -} -#endif /* not _LIBC */ - -/* This function parse bracket expression like "[abc]", "[a-c]", - "[[.a-a.]]" etc. */ - -static bin_tree_t * -parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token, - reg_syntax_t syntax, reg_errcode_t *err) -{ -#ifdef _LIBC - const unsigned char *collseqmb; - const char *collseqwc; - uint32_t nrules; - int32_t table_size; - const int32_t *symb_table; - const unsigned char *extra; - - /* Local function for parse_bracket_exp used in _LIBC environement. - Seek the collating symbol entry correspondings to NAME. - Return the index of the symbol in the SYMB_TABLE. */ - - auto inline int32_t - __attribute ((always_inline)) - seek_collating_symbol_entry (name, name_len) - const unsigned char *name; - size_t name_len; - { - int32_t hash = elem_hash ((const char *) name, name_len); - int32_t elem = hash % table_size; - if (symb_table[2 * elem] != 0) - { - int32_t second = hash % (table_size - 2) + 1; - - do - { - /* First compare the hashing value. */ - if (symb_table[2 * elem] == hash - /* Compare the length of the name. */ - && name_len == extra[symb_table[2 * elem + 1]] - /* Compare the name. */ - && memcmp (name, &extra[symb_table[2 * elem + 1] + 1], - name_len) == 0) - { - /* Yep, this is the entry. */ - break; - } - - /* Next entry. */ - elem += second; - } - while (symb_table[2 * elem] != 0); - } - return elem; - } - - /* Local function for parse_bracket_exp used in _LIBC environment. - Look up the collation sequence value of BR_ELEM. - Return the value if succeeded, UINT_MAX otherwise. */ - - auto inline unsigned int - __attribute ((always_inline)) - lookup_collation_sequence_value (br_elem) - bracket_elem_t *br_elem; - { - if (br_elem->type == SB_CHAR) - { - /* - if (MB_CUR_MAX == 1) - */ - if (nrules == 0) - return collseqmb[br_elem->opr.ch]; - else - { - wint_t wc = __btowc (br_elem->opr.ch); - return __collseq_table_lookup (collseqwc, wc); - } - } - else if (br_elem->type == MB_CHAR) - { - if (nrules != 0) - return __collseq_table_lookup (collseqwc, br_elem->opr.wch); - } - else if (br_elem->type == COLL_SYM) - { - size_t sym_name_len = strlen ((char *) br_elem->opr.name); - if (nrules != 0) - { - int32_t elem, idx; - elem = seek_collating_symbol_entry (br_elem->opr.name, - sym_name_len); - if (symb_table[2 * elem] != 0) - { - /* We found the entry. */ - idx = symb_table[2 * elem + 1]; - /* Skip the name of collating element name. */ - idx += 1 + extra[idx]; - /* Skip the byte sequence of the collating element. */ - idx += 1 + extra[idx]; - /* Adjust for the alignment. */ - idx = (idx + 3) & ~3; - /* Skip the multibyte collation sequence value. */ - idx += sizeof (unsigned int); - /* Skip the wide char sequence of the collating element. */ - idx += sizeof (unsigned int) * - (1 + *(unsigned int *) (extra + idx)); - /* Return the collation sequence value. */ - return *(unsigned int *) (extra + idx); - } - else if (symb_table[2 * elem] == 0 && sym_name_len == 1) - { - /* No valid character. Match it as a single byte - character. */ - return collseqmb[br_elem->opr.name[0]]; - } - } - else if (sym_name_len == 1) - return collseqmb[br_elem->opr.name[0]]; - } - return UINT_MAX; - } - - /* Local function for parse_bracket_exp used in _LIBC environement. - Build the range expression which starts from START_ELEM, and ends - at END_ELEM. The result are written to MBCSET and SBCSET. - RANGE_ALLOC is the allocated size of mbcset->range_starts, and - mbcset->range_ends, is a pointer argument sinse we may - update it. */ - - auto inline reg_errcode_t - __attribute ((always_inline)) - build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem) - re_charset_t *mbcset; - int *range_alloc; - bitset_t sbcset; - bracket_elem_t *start_elem, *end_elem; - { - unsigned int ch; - uint32_t start_collseq; - uint32_t end_collseq; - - /* Equivalence Classes and Character Classes can't be a range - start/end. */ - if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS - || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS, - 0)) - return REG_ERANGE; - - start_collseq = lookup_collation_sequence_value (start_elem); - end_collseq = lookup_collation_sequence_value (end_elem); - /* Check start/end collation sequence values. */ - if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0)) - return REG_ECOLLATE; - if (BE ((syntax & RE_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0)) - return REG_ERANGE; - - /* Got valid collation sequence values, add them as a new entry. - However, if we have no collation elements, and the character set - is single byte, the single byte character set that we - build below suffices. */ - if (nrules > 0 || dfa->mb_cur_max > 1) - { - /* Check the space of the arrays. */ - if (BE (*range_alloc == mbcset->nranges, 0)) - { - /* There is not enough space, need realloc. */ - uint32_t *new_array_start; - uint32_t *new_array_end; - int new_nranges; - - /* +1 in case of mbcset->nranges is 0. */ - new_nranges = 2 * mbcset->nranges + 1; - new_array_start = re_realloc (mbcset->range_starts, uint32_t, - new_nranges); - new_array_end = re_realloc (mbcset->range_ends, uint32_t, - new_nranges); - - if (BE (new_array_start == NULL || new_array_end == NULL, 0)) - return REG_ESPACE; - - mbcset->range_starts = new_array_start; - mbcset->range_ends = new_array_end; - *range_alloc = new_nranges; - } - - mbcset->range_starts[mbcset->nranges] = start_collseq; - mbcset->range_ends[mbcset->nranges++] = end_collseq; - } - - /* Build the table for single byte characters. */ - for (ch = 0; ch < SBC_MAX; ch++) - { - uint32_t ch_collseq; - /* - if (MB_CUR_MAX == 1) - */ - if (nrules == 0) - ch_collseq = collseqmb[ch]; - else - ch_collseq = __collseq_table_lookup (collseqwc, __btowc (ch)); - if (start_collseq <= ch_collseq && ch_collseq <= end_collseq) - bitset_set (sbcset, ch); - } - return REG_NOERROR; - } - - /* Local function for parse_bracket_exp used in _LIBC environement. - Build the collating element which is represented by NAME. - The result are written to MBCSET and SBCSET. - COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a - pointer argument sinse we may update it. */ - - auto inline reg_errcode_t - __attribute ((always_inline)) - build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name) - re_charset_t *mbcset; - int *coll_sym_alloc; - bitset_t sbcset; - const unsigned char *name; - { - int32_t elem, idx; - size_t name_len = strlen ((const char *) name); - if (nrules != 0) - { - elem = seek_collating_symbol_entry (name, name_len); - if (symb_table[2 * elem] != 0) - { - /* We found the entry. */ - idx = symb_table[2 * elem + 1]; - /* Skip the name of collating element name. */ - idx += 1 + extra[idx]; - } - else if (symb_table[2 * elem] == 0 && name_len == 1) - { - /* No valid character, treat it as a normal - character. */ - bitset_set (sbcset, name[0]); - return REG_NOERROR; - } - else - return REG_ECOLLATE; - - /* Got valid collation sequence, add it as a new entry. */ - /* Check the space of the arrays. */ - if (BE (*coll_sym_alloc == mbcset->ncoll_syms, 0)) - { - /* Not enough, realloc it. */ - /* +1 in case of mbcset->ncoll_syms is 0. */ - int new_coll_sym_alloc = 2 * mbcset->ncoll_syms + 1; - /* Use realloc since mbcset->coll_syms is NULL - if *alloc == 0. */ - int32_t *new_coll_syms = re_realloc (mbcset->coll_syms, int32_t, - new_coll_sym_alloc); - if (BE (new_coll_syms == NULL, 0)) - return REG_ESPACE; - mbcset->coll_syms = new_coll_syms; - *coll_sym_alloc = new_coll_sym_alloc; - } - mbcset->coll_syms[mbcset->ncoll_syms++] = idx; - return REG_NOERROR; - } - else - { - if (BE (name_len != 1, 0)) - return REG_ECOLLATE; - else - { - bitset_set (sbcset, name[0]); - return REG_NOERROR; - } - } - } -#endif - - re_token_t br_token; - re_bitset_ptr_t sbcset; -#ifdef RE_ENABLE_I18N - re_charset_t *mbcset; - int coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0; - int equiv_class_alloc = 0, char_class_alloc = 0; -#endif /* not RE_ENABLE_I18N */ - int non_match = 0; - bin_tree_t *work_tree; - int token_len; - int first_round = 1; -#ifdef _LIBC - collseqmb = (const unsigned char *) - _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB); - nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES); - if (nrules) - { - /* - if (MB_CUR_MAX > 1) - */ - collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC); - table_size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_SYMB_HASH_SIZEMB); - symb_table = (const int32_t *) _NL_CURRENT (LC_COLLATE, - _NL_COLLATE_SYMB_TABLEMB); - extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE, - _NL_COLLATE_SYMB_EXTRAMB); - } -#endif - sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1); -#ifdef RE_ENABLE_I18N - mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1); -#endif /* RE_ENABLE_I18N */ -#ifdef RE_ENABLE_I18N - if (BE (sbcset == NULL || mbcset == NULL, 0)) -#else - if (BE (sbcset == NULL, 0)) -#endif /* RE_ENABLE_I18N */ - { - *err = REG_ESPACE; - return NULL; - } - - token_len = peek_token_bracket (token, regexp, syntax); - if (BE (token->type == END_OF_RE, 0)) - { - *err = REG_BADPAT; - goto parse_bracket_exp_free_return; - } - if (token->type == OP_NON_MATCH_LIST) - { -#ifdef RE_ENABLE_I18N - mbcset->non_match = 1; -#endif /* not RE_ENABLE_I18N */ - non_match = 1; - if (syntax & RE_HAT_LISTS_NOT_NEWLINE) - bitset_set (sbcset, '\n'); - re_string_skip_bytes (regexp, token_len); /* Skip a token. */ - token_len = peek_token_bracket (token, regexp, syntax); - if (BE (token->type == END_OF_RE, 0)) - { - *err = REG_BADPAT; - goto parse_bracket_exp_free_return; - } - } - - /* We treat the first ']' as a normal character. */ - if (token->type == OP_CLOSE_BRACKET) - token->type = CHARACTER; - - while (1) - { - bracket_elem_t start_elem, end_elem; - unsigned char start_name_buf[BRACKET_NAME_BUF_SIZE]; - unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE]; - reg_errcode_t ret; - int token_len2 = 0, is_range_exp = 0; - re_token_t token2; - - start_elem.opr.name = start_name_buf; - ret = parse_bracket_element (&start_elem, regexp, token, token_len, dfa, - syntax, first_round); - if (BE (ret != REG_NOERROR, 0)) - { - *err = ret; - goto parse_bracket_exp_free_return; - } - first_round = 0; - - /* Get information about the next token. We need it in any case. */ - token_len = peek_token_bracket (token, regexp, syntax); - - /* Do not check for ranges if we know they are not allowed. */ - if (start_elem.type != CHAR_CLASS && start_elem.type != EQUIV_CLASS) - { - if (BE (token->type == END_OF_RE, 0)) - { - *err = REG_EBRACK; - goto parse_bracket_exp_free_return; - } - if (token->type == OP_CHARSET_RANGE) - { - re_string_skip_bytes (regexp, token_len); /* Skip '-'. */ - token_len2 = peek_token_bracket (&token2, regexp, syntax); - if (BE (token2.type == END_OF_RE, 0)) - { - *err = REG_EBRACK; - goto parse_bracket_exp_free_return; - } - if (token2.type == OP_CLOSE_BRACKET) - { - /* We treat the last '-' as a normal character. */ - re_string_skip_bytes (regexp, -token_len); - token->type = CHARACTER; - } - else - is_range_exp = 1; - } - } - - if (is_range_exp == 1) - { - end_elem.opr.name = end_name_buf; - ret = parse_bracket_element (&end_elem, regexp, &token2, token_len2, - dfa, syntax, 1); - if (BE (ret != REG_NOERROR, 0)) - { - *err = ret; - goto parse_bracket_exp_free_return; - } - - token_len = peek_token_bracket (token, regexp, syntax); - -#ifdef _LIBC - *err = build_range_exp (sbcset, mbcset, &range_alloc, - &start_elem, &end_elem); -#else -# ifdef RE_ENABLE_I18N - *err = build_range_exp (sbcset, - dfa->mb_cur_max > 1 ? mbcset : NULL, - &range_alloc, &start_elem, &end_elem); -# else - *err = build_range_exp (sbcset, &start_elem, &end_elem); -# endif -#endif /* RE_ENABLE_I18N */ - if (BE (*err != REG_NOERROR, 0)) - goto parse_bracket_exp_free_return; - } - else - { - switch (start_elem.type) - { - case SB_CHAR: - bitset_set (sbcset, start_elem.opr.ch); - break; -#ifdef RE_ENABLE_I18N - case MB_CHAR: - /* Check whether the array has enough space. */ - if (BE (mbchar_alloc == mbcset->nmbchars, 0)) - { - wchar_t *new_mbchars; - /* Not enough, realloc it. */ - /* +1 in case of mbcset->nmbchars is 0. */ - mbchar_alloc = 2 * mbcset->nmbchars + 1; - /* Use realloc since array is NULL if *alloc == 0. */ - new_mbchars = re_realloc (mbcset->mbchars, wchar_t, - mbchar_alloc); - if (BE (new_mbchars == NULL, 0)) - goto parse_bracket_exp_espace; - mbcset->mbchars = new_mbchars; - } - mbcset->mbchars[mbcset->nmbchars++] = start_elem.opr.wch; - break; -#endif /* RE_ENABLE_I18N */ - case EQUIV_CLASS: - *err = build_equiv_class (sbcset, -#ifdef RE_ENABLE_I18N - mbcset, &equiv_class_alloc, -#endif /* RE_ENABLE_I18N */ - start_elem.opr.name); - if (BE (*err != REG_NOERROR, 0)) - goto parse_bracket_exp_free_return; - break; - case COLL_SYM: - *err = build_collating_symbol (sbcset, -#ifdef RE_ENABLE_I18N - mbcset, &coll_sym_alloc, -#endif /* RE_ENABLE_I18N */ - start_elem.opr.name); - if (BE (*err != REG_NOERROR, 0)) - goto parse_bracket_exp_free_return; - break; - case CHAR_CLASS: - *err = build_charclass (regexp->trans, sbcset, -#ifdef RE_ENABLE_I18N - mbcset, &char_class_alloc, -#endif /* RE_ENABLE_I18N */ - (const char *) start_elem.opr.name, syntax); - if (BE (*err != REG_NOERROR, 0)) - goto parse_bracket_exp_free_return; - break; - default: - assert (0); - break; - } - } - if (BE (token->type == END_OF_RE, 0)) - { - *err = REG_EBRACK; - goto parse_bracket_exp_free_return; - } - if (token->type == OP_CLOSE_BRACKET) - break; - } - - re_string_skip_bytes (regexp, token_len); /* Skip a token. */ - - /* If it is non-matching list. */ - if (non_match) - bitset_not (sbcset); - -#ifdef RE_ENABLE_I18N - /* Ensure only single byte characters are set. */ - if (dfa->mb_cur_max > 1) - bitset_mask (sbcset, dfa->sb_char); - - if (mbcset->nmbchars || mbcset->ncoll_syms || mbcset->nequiv_classes - || mbcset->nranges || (dfa->mb_cur_max > 1 && (mbcset->nchar_classes - || mbcset->non_match))) - { - bin_tree_t *mbc_tree; - int sbc_idx; - /* Build a tree for complex bracket. */ - dfa->has_mb_node = 1; - br_token.type = COMPLEX_BRACKET; - br_token.opr.mbcset = mbcset; - mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token); - if (BE (mbc_tree == NULL, 0)) - goto parse_bracket_exp_espace; - for (sbc_idx = 0; sbc_idx < BITSET_WORDS; ++sbc_idx) - if (sbcset[sbc_idx]) - break; - /* If there are no bits set in sbcset, there is no point - of having both SIMPLE_BRACKET and COMPLEX_BRACKET. */ - if (sbc_idx < BITSET_WORDS) - { - /* Build a tree for simple bracket. */ - br_token.type = SIMPLE_BRACKET; - br_token.opr.sbcset = sbcset; - work_tree = create_token_tree (dfa, NULL, NULL, &br_token); - if (BE (work_tree == NULL, 0)) - goto parse_bracket_exp_espace; - - /* Then join them by ALT node. */ - work_tree = create_tree (dfa, work_tree, mbc_tree, OP_ALT); - if (BE (work_tree == NULL, 0)) - goto parse_bracket_exp_espace; - } - else - { - re_free (sbcset); - work_tree = mbc_tree; - } - } - else -#endif /* not RE_ENABLE_I18N */ - { -#ifdef RE_ENABLE_I18N - free_charset (mbcset); -#endif - /* Build a tree for simple bracket. */ - br_token.type = SIMPLE_BRACKET; - br_token.opr.sbcset = sbcset; - work_tree = create_token_tree (dfa, NULL, NULL, &br_token); - if (BE (work_tree == NULL, 0)) - goto parse_bracket_exp_espace; - } - return work_tree; - - parse_bracket_exp_espace: - *err = REG_ESPACE; - parse_bracket_exp_free_return: - re_free (sbcset); -#ifdef RE_ENABLE_I18N - free_charset (mbcset); -#endif /* RE_ENABLE_I18N */ - return NULL; -} - -/* Parse an element in the bracket expression. */ - -static reg_errcode_t -parse_bracket_element (bracket_elem_t *elem, re_string_t *regexp, - re_token_t *token, int token_len, UNUSED re_dfa_t *dfa, - reg_syntax_t syntax, int accept_hyphen) -{ -#ifdef RE_ENABLE_I18N - int cur_char_size; - cur_char_size = re_string_char_size_at (regexp, re_string_cur_idx (regexp)); - if (cur_char_size > 1) - { - elem->type = MB_CHAR; - elem->opr.wch = re_string_wchar_at (regexp, re_string_cur_idx (regexp)); - re_string_skip_bytes (regexp, cur_char_size); - return REG_NOERROR; - } -#endif /* RE_ENABLE_I18N */ - re_string_skip_bytes (regexp, token_len); /* Skip a token. */ - if (token->type == OP_OPEN_COLL_ELEM || token->type == OP_OPEN_CHAR_CLASS - || token->type == OP_OPEN_EQUIV_CLASS) - return parse_bracket_symbol (elem, regexp, token); - if (BE (token->type == OP_CHARSET_RANGE, 0) && !accept_hyphen) - { - /* A '-' must only appear as anything but a range indicator before - the closing bracket. Everything else is an error. */ - re_token_t token2; - (void) peek_token_bracket (&token2, regexp, syntax); - if (token2.type != OP_CLOSE_BRACKET) - /* The actual error value is not standardized since this whole - case is undefined. But ERANGE makes good sense. */ - return REG_ERANGE; - } - elem->type = SB_CHAR; - elem->opr.ch = token->opr.c; - return REG_NOERROR; -} - -/* Parse a bracket symbol in the bracket expression. Bracket symbols are - such as [::], [..], and - [==]. */ - -static reg_errcode_t -parse_bracket_symbol (bracket_elem_t *elem, re_string_t *regexp, - re_token_t *token) -{ - unsigned char ch, delim = token->opr.c; - int i = 0; - if (re_string_eoi(regexp)) - return REG_EBRACK; - for (;; ++i) - { - if (i >= BRACKET_NAME_BUF_SIZE) - return REG_EBRACK; - if (token->type == OP_OPEN_CHAR_CLASS) - ch = re_string_fetch_byte_case (regexp); - else - ch = re_string_fetch_byte (regexp); - if (re_string_eoi(regexp)) - return REG_EBRACK; - if (ch == delim && re_string_peek_byte (regexp, 0) == ']') - break; - elem->opr.name[i] = ch; - } - re_string_skip_bytes (regexp, 1); - elem->opr.name[i] = '\0'; - switch (token->type) - { - case OP_OPEN_COLL_ELEM: - elem->type = COLL_SYM; - break; - case OP_OPEN_EQUIV_CLASS: - elem->type = EQUIV_CLASS; - break; - case OP_OPEN_CHAR_CLASS: - elem->type = CHAR_CLASS; - break; - default: - break; - } - return REG_NOERROR; -} - - /* Helper function for parse_bracket_exp. - Build the equivalence class which is represented by NAME. - The result are written to MBCSET and SBCSET. - EQUIV_CLASS_ALLOC is the allocated size of mbcset->equiv_classes, - is a pointer argument sinse we may update it. */ - -static reg_errcode_t -#ifdef RE_ENABLE_I18N -build_equiv_class (bitset_t sbcset, re_charset_t *mbcset, - int *equiv_class_alloc, const unsigned char *name) -#else /* not RE_ENABLE_I18N */ -build_equiv_class (bitset_t sbcset, const unsigned char *name) -#endif /* not RE_ENABLE_I18N */ -{ -#ifdef _LIBC - uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES); - if (nrules != 0) - { - const int32_t *table, *indirect; - const unsigned char *weights, *extra, *cp; - unsigned char char_buf[2]; - int32_t idx1, idx2; - unsigned int ch; - size_t len; - /* This #include defines a local function! */ -# include - /* Calculate the index for equivalence class. */ - cp = name; - table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB); - weights = (const unsigned char *) _NL_CURRENT (LC_COLLATE, - _NL_COLLATE_WEIGHTMB); - extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE, - _NL_COLLATE_EXTRAMB); - indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE, - _NL_COLLATE_INDIRECTMB); - idx1 = findidx (&cp); - if (BE (idx1 == 0 || cp < name + strlen ((const char *) name), 0)) - /* This isn't a valid character. */ - return REG_ECOLLATE; - - /* Build single byte matcing table for this equivalence class. */ - char_buf[1] = (unsigned char) '\0'; - len = weights[idx1 & 0xffffff]; - for (ch = 0; ch < SBC_MAX; ++ch) - { - char_buf[0] = ch; - cp = char_buf; - idx2 = findidx (&cp); -/* - idx2 = table[ch]; -*/ - if (idx2 == 0) - /* This isn't a valid character. */ - continue; - /* Compare only if the length matches and the collation rule - index is the same. */ - if (len == weights[idx2 & 0xffffff] && (idx1 >> 24) == (idx2 >> 24)) - { - int cnt = 0; - - while (cnt <= len && - weights[(idx1 & 0xffffff) + 1 + cnt] - == weights[(idx2 & 0xffffff) + 1 + cnt]) - ++cnt; - - if (cnt > len) - bitset_set (sbcset, ch); - } - } - /* Check whether the array has enough space. */ - if (BE (*equiv_class_alloc == mbcset->nequiv_classes, 0)) - { - /* Not enough, realloc it. */ - /* +1 in case of mbcset->nequiv_classes is 0. */ - int new_equiv_class_alloc = 2 * mbcset->nequiv_classes + 1; - /* Use realloc since the array is NULL if *alloc == 0. */ - int32_t *new_equiv_classes = re_realloc (mbcset->equiv_classes, - int32_t, - new_equiv_class_alloc); - if (BE (new_equiv_classes == NULL, 0)) - return REG_ESPACE; - mbcset->equiv_classes = new_equiv_classes; - *equiv_class_alloc = new_equiv_class_alloc; - } - mbcset->equiv_classes[mbcset->nequiv_classes++] = idx1; - } - else -#endif /* _LIBC */ - { - if (BE (strlen ((const char *) name) != 1, 0)) - return REG_ECOLLATE; - bitset_set (sbcset, *name); - } - return REG_NOERROR; -} - - /* Helper function for parse_bracket_exp. - Build the character class which is represented by NAME. - The result are written to MBCSET and SBCSET. - CHAR_CLASS_ALLOC is the allocated size of mbcset->char_classes, - is a pointer argument sinse we may update it. */ - -static reg_errcode_t -#ifdef RE_ENABLE_I18N -build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset, - re_charset_t *mbcset, int *char_class_alloc, - const char *class_name, reg_syntax_t syntax) -#else /* not RE_ENABLE_I18N */ -build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset, - const char *class_name, reg_syntax_t syntax) -#endif /* not RE_ENABLE_I18N */ -{ - int i; - - /* In case of REG_ICASE "upper" and "lower" match the both of - upper and lower cases. */ - if ((syntax & RE_ICASE) - && (strcmp (class_name, "upper") == 0 || strcmp (class_name, "lower") == 0)) - class_name = "alpha"; - -#ifdef RE_ENABLE_I18N - /* Check the space of the arrays. */ - if (BE (*char_class_alloc == mbcset->nchar_classes, 0)) - { - /* Not enough, realloc it. */ - /* +1 in case of mbcset->nchar_classes is 0. */ - int new_char_class_alloc = 2 * mbcset->nchar_classes + 1; - /* Use realloc since array is NULL if *alloc == 0. */ - wctype_t *new_char_classes = re_realloc (mbcset->char_classes, wctype_t, - new_char_class_alloc); - if (BE (new_char_classes == NULL, 0)) - return REG_ESPACE; - mbcset->char_classes = new_char_classes; - *char_class_alloc = new_char_class_alloc; - } - mbcset->char_classes[mbcset->nchar_classes++] = __wctype (class_name); -#endif /* RE_ENABLE_I18N */ - -#define BUILD_CHARCLASS_LOOP(ctype_func) \ - do { \ - if (BE (trans != NULL, 0)) \ - { \ - for (i = 0; i < SBC_MAX; ++i) \ - if (ctype_func (i)) \ - bitset_set (sbcset, trans[i]); \ - } \ - else \ - { \ - for (i = 0; i < SBC_MAX; ++i) \ - if (ctype_func (i)) \ - bitset_set (sbcset, i); \ - } \ - } while (0) - - if (strcmp (class_name, "alnum") == 0) - BUILD_CHARCLASS_LOOP (isalnum); - else if (strcmp (class_name, "cntrl") == 0) - BUILD_CHARCLASS_LOOP (iscntrl); - else if (strcmp (class_name, "lower") == 0) - BUILD_CHARCLASS_LOOP (islower); - else if (strcmp (class_name, "space") == 0) - BUILD_CHARCLASS_LOOP (isspace); - else if (strcmp (class_name, "alpha") == 0) - BUILD_CHARCLASS_LOOP (isalpha); - else if (strcmp (class_name, "digit") == 0) - BUILD_CHARCLASS_LOOP (isdigit); - else if (strcmp (class_name, "print") == 0) - BUILD_CHARCLASS_LOOP (isprint); - else if (strcmp (class_name, "upper") == 0) - BUILD_CHARCLASS_LOOP (isupper); - else if (strcmp (class_name, "blank") == 0) -#ifndef GAWK - BUILD_CHARCLASS_LOOP (isblank); -#else - /* see comments above */ - BUILD_CHARCLASS_LOOP (is_blank); -#endif - else if (strcmp (class_name, "graph") == 0) - BUILD_CHARCLASS_LOOP (isgraph); - else if (strcmp (class_name, "punct") == 0) - BUILD_CHARCLASS_LOOP (ispunct); - else if (strcmp (class_name, "xdigit") == 0) - BUILD_CHARCLASS_LOOP (isxdigit); - else - return REG_ECTYPE; - - return REG_NOERROR; -} - -static bin_tree_t * -build_charclass_op (re_dfa_t *dfa, RE_TRANSLATE_TYPE trans, - const char *class_name, - const char *extra, int non_match, - reg_errcode_t *err) -{ - re_bitset_ptr_t sbcset; -#ifdef RE_ENABLE_I18N - re_charset_t *mbcset; - int alloc = 0; -#endif /* not RE_ENABLE_I18N */ - reg_errcode_t ret; - re_token_t br_token; - bin_tree_t *tree; - - sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1); -#ifdef RE_ENABLE_I18N - mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1); -#endif /* RE_ENABLE_I18N */ - -#ifdef RE_ENABLE_I18N - if (BE (sbcset == NULL || mbcset == NULL, 0)) -#else /* not RE_ENABLE_I18N */ - if (BE (sbcset == NULL, 0)) -#endif /* not RE_ENABLE_I18N */ - { - *err = REG_ESPACE; - return NULL; - } - - if (non_match) - { -#ifdef RE_ENABLE_I18N - mbcset->non_match = 1; -#endif /* not RE_ENABLE_I18N */ - } - - /* We don't care the syntax in this case. */ - ret = build_charclass (trans, sbcset, -#ifdef RE_ENABLE_I18N - mbcset, &alloc, -#endif /* RE_ENABLE_I18N */ - class_name, 0); - - if (BE (ret != REG_NOERROR, 0)) - { - re_free (sbcset); -#ifdef RE_ENABLE_I18N - free_charset (mbcset); -#endif /* RE_ENABLE_I18N */ - *err = ret; - return NULL; - } - /* \w match '_' also. */ - for (; *extra; extra++) - bitset_set (sbcset, *extra); - - /* If it is non-matching list. */ - if (non_match) - bitset_not (sbcset); - -#ifdef RE_ENABLE_I18N - /* Ensure only single byte characters are set. */ - if (dfa->mb_cur_max > 1) - bitset_mask (sbcset, dfa->sb_char); -#endif - - /* Build a tree for simple bracket. */ - br_token.type = SIMPLE_BRACKET; - br_token.opr.sbcset = sbcset; - tree = create_token_tree (dfa, NULL, NULL, &br_token); - if (BE (tree == NULL, 0)) - goto build_word_op_espace; - -#ifdef RE_ENABLE_I18N - if (dfa->mb_cur_max > 1) - { - bin_tree_t *mbc_tree; - /* Build a tree for complex bracket. */ - br_token.type = COMPLEX_BRACKET; - br_token.opr.mbcset = mbcset; - dfa->has_mb_node = 1; - mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token); - if (BE (mbc_tree == NULL, 0)) - goto build_word_op_espace; - /* Then join them by ALT node. */ - tree = create_tree (dfa, tree, mbc_tree, OP_ALT); - if (BE (mbc_tree != NULL, 1)) - return tree; - } - else - { - free_charset (mbcset); - return tree; - } -#else /* not RE_ENABLE_I18N */ - return tree; -#endif /* not RE_ENABLE_I18N */ - - build_word_op_espace: - re_free (sbcset); -#ifdef RE_ENABLE_I18N - free_charset (mbcset); -#endif /* RE_ENABLE_I18N */ - *err = REG_ESPACE; - return NULL; -} - -/* This is intended for the expressions like "a{1,3}". - Fetch a number from `input', and return the number. - Return -1, if the number field is empty like "{,1}". - Return -2, If an error is occured. */ - -static int -fetch_number (re_string_t *input, re_token_t *token, reg_syntax_t syntax) -{ - int num = -1; - unsigned char c; - while (1) - { - fetch_token (token, input, syntax); - c = token->opr.c; - if (BE (token->type == END_OF_RE, 0)) - return -2; - if (token->type == OP_CLOSE_DUP_NUM || c == ',') - break; - num = ((token->type != CHARACTER || c < '0' || '9' < c || num == -2) - ? -2 : ((num == -1) ? c - '0' : num * 10 + c - '0')); - num = (num > RE_DUP_MAX) ? -2 : num; - } - return num; -} - -#ifdef RE_ENABLE_I18N -static void -free_charset (re_charset_t *cset) -{ - re_free (cset->mbchars); -# ifdef _LIBC - re_free (cset->coll_syms); - re_free (cset->equiv_classes); - re_free (cset->range_starts); - re_free (cset->range_ends); -# endif - re_free (cset->char_classes); - re_free (cset); -} -#endif /* RE_ENABLE_I18N */ - -/* Functions for binary tree operation. */ - -/* Create a tree node. */ - -static bin_tree_t * -create_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right, - re_token_type_t type) -{ - re_token_t t; - t.type = type; - return create_token_tree (dfa, left, right, &t); -} - -static bin_tree_t * -create_token_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right, - const re_token_t *token) -{ - bin_tree_t *tree; - if (BE (dfa->str_tree_storage_idx == BIN_TREE_STORAGE_SIZE, 0)) - { - bin_tree_storage_t *storage = re_malloc (bin_tree_storage_t, 1); - - if (storage == NULL) - return NULL; - storage->next = dfa->str_tree_storage; - dfa->str_tree_storage = storage; - dfa->str_tree_storage_idx = 0; - } - tree = &dfa->str_tree_storage->data[dfa->str_tree_storage_idx++]; - - tree->parent = NULL; - tree->left = left; - tree->right = right; - tree->token = *token; - tree->token.duplicated = 0; - tree->token.opt_subexp = 0; - tree->first = NULL; - tree->next = NULL; - tree->node_idx = -1; - - if (left != NULL) - left->parent = tree; - if (right != NULL) - right->parent = tree; - return tree; -} - -/* Mark the tree SRC as an optional subexpression. - To be called from preorder or postorder. */ - -static reg_errcode_t -mark_opt_subexp (void *extra, bin_tree_t *node) -{ - int idx = (int) (long) extra; - if (node->token.type == SUBEXP && node->token.opr.idx == idx) - node->token.opt_subexp = 1; - - return REG_NOERROR; -} - -/* Free the allocated memory inside NODE. */ - -static void -free_token (re_token_t *node) -{ -#ifdef RE_ENABLE_I18N - if (node->type == COMPLEX_BRACKET && node->duplicated == 0) - free_charset (node->opr.mbcset); - else -#endif /* RE_ENABLE_I18N */ - if (node->type == SIMPLE_BRACKET && node->duplicated == 0) - re_free (node->opr.sbcset); -} - -/* Worker function for tree walking. Free the allocated memory inside NODE - and its children. */ - -static reg_errcode_t -free_tree (UNUSED void *extra, bin_tree_t *node) -{ - free_token (&node->token); - return REG_NOERROR; -} - - -/* Duplicate the node SRC, and return new node. This is a preorder - visit similar to the one implemented by the generic visitor, but - we need more infrastructure to maintain two parallel trees --- so, - it's easier to duplicate. */ - -static bin_tree_t * -duplicate_tree (const bin_tree_t *root, re_dfa_t *dfa) -{ - const bin_tree_t *node; - bin_tree_t *dup_root; - bin_tree_t **p_new = &dup_root, *dup_node = root->parent; - - for (node = root; ; ) - { - /* Create a new tree and link it back to the current parent. */ - *p_new = create_token_tree (dfa, NULL, NULL, &node->token); - if (*p_new == NULL) - return NULL; - (*p_new)->parent = dup_node; - (*p_new)->token.duplicated = 1; - dup_node = *p_new; - - /* Go to the left node, or up and to the right. */ - if (node->left) - { - node = node->left; - p_new = &dup_node->left; - } - else - { - const bin_tree_t *prev = NULL; - while (node->right == prev || node->right == NULL) - { - prev = node; - node = node->parent; - dup_node = dup_node->parent; - if (!node) - return dup_root; - } - node = node->right; - p_new = &dup_node->right; - } - } -} diff --git a/deps/regex/regex.c b/deps/regex/regex.c deleted file mode 100644 index f9a8c9bf1c2..00000000000 --- a/deps/regex/regex.c +++ /dev/null @@ -1,85 +0,0 @@ -/* Extended regular expression matching and search library. - Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Isamu Hasegawa . - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA. */ - -#include "config.h" - -/* Make sure noone compiles this code with a C++ compiler. */ -#ifdef __cplusplus -# error "This is C code, use a C compiler" -#endif - -#ifdef _LIBC -/* We have to keep the namespace clean. */ -# define regfree(preg) __regfree (preg) -# define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef) -# define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags) -# define regerror(errcode, preg, errbuf, errbuf_size) \ - __regerror(errcode, preg, errbuf, errbuf_size) -# define re_set_registers(bu, re, nu, st, en) \ - __re_set_registers (bu, re, nu, st, en) -# define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \ - __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop) -# define re_match(bufp, string, size, pos, regs) \ - __re_match (bufp, string, size, pos, regs) -# define re_search(bufp, string, size, startpos, range, regs) \ - __re_search (bufp, string, size, startpos, range, regs) -# define re_compile_pattern(pattern, length, bufp) \ - __re_compile_pattern (pattern, length, bufp) -# define re_set_syntax(syntax) __re_set_syntax (syntax) -# define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \ - __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop) -# define re_compile_fastmap(bufp) __re_compile_fastmap (bufp) - -# include "../locale/localeinfo.h" -#endif - -#if defined (_MSC_VER) -#include /* for size_t */ -#endif - -/* On some systems, limits.h sets RE_DUP_MAX to a lower value than - GNU regex allows. Include it before , which correctly - #undefs RE_DUP_MAX and sets it to the right value. */ -#include - -#ifdef GAWK -#undef alloca -#define alloca alloca_is_bad_you_should_never_use_it -#endif -#include -#include "regex_internal.h" - -#include "regex_internal.c" -#ifdef GAWK -#define bool int -#define true (1) -#define false (0) -#endif -#include "regcomp.c" -#include "regexec.c" - -/* Binary backward compatibility. */ -#if _LIBC -# include -# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3) -link_warning (re_max_failures, "the 're_max_failures' variable is obsolete and will go away.") -int re_max_failures = 2000; -# endif -#endif diff --git a/deps/regex/regex.h b/deps/regex/regex.h deleted file mode 100644 index 61c96838721..00000000000 --- a/deps/regex/regex.h +++ /dev/null @@ -1,582 +0,0 @@ -#include -#include - -/* Definitions for data structures and routines for the regular - expression library. - Copyright (C) 1985,1989-93,1995-98,2000,2001,2002,2003,2005,2006,2008 - Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA. */ - -#ifndef _REGEX_H -#define _REGEX_H 1 - -#ifdef HAVE_STDDEF_H -#include -#endif - -#ifdef HAVE_SYS_TYPES_H -#include -#endif - -#ifndef _LIBC -#define __USE_GNU 1 -#endif - -/* Allow the use in C++ code. */ -#ifdef __cplusplus -extern "C" { -#endif - -/* The following two types have to be signed and unsigned integer type - wide enough to hold a value of a pointer. For most ANSI compilers - ptrdiff_t and size_t should be likely OK. Still size of these two - types is 2 for Microsoft C. Ugh... */ -typedef long int s_reg_t; -typedef unsigned long int active_reg_t; - -/* The following bits are used to determine the regexp syntax we - recognize. The set/not-set meanings are chosen so that Emacs syntax - remains the value 0. The bits are given in alphabetical order, and - the definitions shifted by one from the previous bit; thus, when we - add or remove a bit, only one other definition need change. */ -typedef unsigned long int reg_syntax_t; - -#ifdef __USE_GNU -/* If this bit is not set, then \ inside a bracket expression is literal. - If set, then such a \ quotes the following character. */ -# define RE_BACKSLASH_ESCAPE_IN_LISTS ((unsigned long int) 1) - -/* If this bit is not set, then + and ? are operators, and \+ and \? are - literals. - If set, then \+ and \? are operators and + and ? are literals. */ -# define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1) - -/* If this bit is set, then character classes are supported. They are: - [:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:], - [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:]. - If not set, then character classes are not supported. */ -# define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1) - -/* If this bit is set, then ^ and $ are always anchors (outside bracket - expressions, of course). - If this bit is not set, then it depends: - ^ is an anchor if it is at the beginning of a regular - expression or after an open-group or an alternation operator; - $ is an anchor if it is at the end of a regular expression, or - before a close-group or an alternation operator. - - This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because - POSIX draft 11.2 says that * etc. in leading positions is undefined. - We already implemented a previous draft which made those constructs - invalid, though, so we haven't changed the code back. */ -# define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1) - -/* If this bit is set, then special characters are always special - regardless of where they are in the pattern. - If this bit is not set, then special characters are special only in - some contexts; otherwise they are ordinary. Specifically, - * + ? and intervals are only special when not after the beginning, - open-group, or alternation operator. */ -# define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1) - -/* If this bit is set, then *, +, ?, and { cannot be first in an re or - immediately after an alternation or begin-group operator. */ -# define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1) - -/* If this bit is set, then . matches newline. - If not set, then it doesn't. */ -# define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1) - -/* If this bit is set, then . doesn't match NUL. - If not set, then it does. */ -# define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1) - -/* If this bit is set, nonmatching lists [^...] do not match newline. - If not set, they do. */ -# define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1) - -/* If this bit is set, either \{...\} or {...} defines an - interval, depending on RE_NO_BK_BRACES. - If not set, \{, \}, {, and } are literals. */ -# define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1) - -/* If this bit is set, +, ? and | aren't recognized as operators. - If not set, they are. */ -# define RE_LIMITED_OPS (RE_INTERVALS << 1) - -/* If this bit is set, newline is an alternation operator. - If not set, newline is literal. */ -# define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1) - -/* If this bit is set, then `{...}' defines an interval, and \{ and \} - are literals. - If not set, then `\{...\}' defines an interval. */ -# define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1) - -/* If this bit is set, (...) defines a group, and \( and \) are literals. - If not set, \(...\) defines a group, and ( and ) are literals. */ -# define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1) - -/* If this bit is set, then \ matches . - If not set, then \ is a back-reference. */ -# define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1) - -/* If this bit is set, then | is an alternation operator, and \| is literal. - If not set, then \| is an alternation operator, and | is literal. */ -# define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1) - -/* If this bit is set, then an ending range point collating higher - than the starting range point, as in [z-a], is invalid. - If not set, then when ending range point collates higher than the - starting range point, the range is ignored. */ -# define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1) - -/* If this bit is set, then an unmatched ) is ordinary. - If not set, then an unmatched ) is invalid. */ -# define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1) - -/* If this bit is set, succeed as soon as we match the whole pattern, - without further backtracking. */ -# define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1) - -/* If this bit is set, do not process the GNU regex operators. - If not set, then the GNU regex operators are recognized. */ -# define RE_NO_GNU_OPS (RE_NO_POSIX_BACKTRACKING << 1) - -/* If this bit is set, a syntactically invalid interval is treated as - a string of ordinary characters. For example, the ERE 'a{1' is - treated as 'a\{1'. */ -# define RE_INVALID_INTERVAL_ORD (RE_NO_GNU_OPS << 1) - -/* If this bit is set, then ignore case when matching. - If not set, then case is significant. */ -# define RE_ICASE (RE_INVALID_INTERVAL_ORD << 1) - -/* This bit is used internally like RE_CONTEXT_INDEP_ANCHORS but only - for ^, because it is difficult to scan the regex backwards to find - whether ^ should be special. */ -# define RE_CARET_ANCHORS_HERE (RE_ICASE << 1) - -/* If this bit is set, then \{ cannot be first in an bre or - immediately after an alternation or begin-group operator. */ -# define RE_CONTEXT_INVALID_DUP (RE_CARET_ANCHORS_HERE << 1) - -/* If this bit is set, then no_sub will be set to 1 during - re_compile_pattern. */ -#define RE_NO_SUB (RE_CONTEXT_INVALID_DUP << 1) -#endif - -/* This global variable defines the particular regexp syntax to use (for - some interfaces). When a regexp is compiled, the syntax used is - stored in the pattern buffer, so changing this does not affect - already-compiled regexps. */ -extern reg_syntax_t re_syntax_options; - -#ifdef __USE_GNU -/* Define combinations of the above bits for the standard possibilities. - (The [[[ comments delimit what gets put into the Texinfo file, so - don't delete them!) */ -/* [[[begin syntaxes]]] */ -#define RE_SYNTAX_EMACS 0 - -#define RE_SYNTAX_AWK \ - (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \ - | RE_NO_BK_PARENS | RE_NO_BK_REFS \ - | RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \ - | RE_DOT_NEWLINE | RE_CONTEXT_INDEP_ANCHORS \ - | RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS) - -#define RE_SYNTAX_GNU_AWK \ - ((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS \ - | RE_INVALID_INTERVAL_ORD) \ - & ~(RE_DOT_NOT_NULL | RE_CONTEXT_INDEP_OPS \ - | RE_CONTEXT_INVALID_OPS )) - -#define RE_SYNTAX_POSIX_AWK \ - (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS \ - | RE_INTERVALS | RE_NO_GNU_OPS \ - | RE_INVALID_INTERVAL_ORD) - -#define RE_SYNTAX_GREP \ - (RE_BK_PLUS_QM | RE_CHAR_CLASSES \ - | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \ - | RE_NEWLINE_ALT) - -#define RE_SYNTAX_EGREP \ - (RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \ - | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \ - | RE_NEWLINE_ALT | RE_NO_BK_PARENS \ - | RE_NO_BK_VBAR) - -#define RE_SYNTAX_POSIX_EGREP \ - (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES \ - | RE_INVALID_INTERVAL_ORD) - -/* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */ -#define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC - -#define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC - -/* Syntax bits common to both basic and extended POSIX regex syntax. */ -#define _RE_SYNTAX_POSIX_COMMON \ - (RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \ - | RE_INTERVALS | RE_NO_EMPTY_RANGES) - -#define RE_SYNTAX_POSIX_BASIC \ - (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM | RE_CONTEXT_INVALID_DUP) - -/* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes - RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this - isn't minimal, since other operators, such as \`, aren't disabled. */ -#define RE_SYNTAX_POSIX_MINIMAL_BASIC \ - (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS) - -#define RE_SYNTAX_POSIX_EXTENDED \ - (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \ - | RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \ - | RE_NO_BK_PARENS | RE_NO_BK_VBAR \ - | RE_CONTEXT_INVALID_OPS | RE_UNMATCHED_RIGHT_PAREN_ORD) - -/* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INDEP_OPS is - removed and RE_NO_BK_REFS is added. */ -#define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \ - (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \ - | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \ - | RE_NO_BK_PARENS | RE_NO_BK_REFS \ - | RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD) -/* [[[end syntaxes]]] */ - -/* Maximum number of duplicates an interval can allow. Some systems - (erroneously) define this in other header files, but we want our - value, so remove any previous define. */ -# ifdef RE_DUP_MAX -# undef RE_DUP_MAX -# endif -/* If sizeof(int) == 2, then ((1 << 15) - 1) overflows. */ -# define RE_DUP_MAX (0x7fff) -#endif - - -/* POSIX `cflags' bits (i.e., information for `regcomp'). */ - -/* If this bit is set, then use extended regular expression syntax. - If not set, then use basic regular expression syntax. */ -#define REG_EXTENDED 1 - -/* If this bit is set, then ignore case when matching. - If not set, then case is significant. */ -#define REG_ICASE (REG_EXTENDED << 1) - -/* If this bit is set, then anchors do not match at newline - characters in the string. - If not set, then anchors do match at newlines. */ -#define REG_NEWLINE (REG_ICASE << 1) - -/* If this bit is set, then report only success or fail in regexec. - If not set, then returns differ between not matching and errors. */ -#define REG_NOSUB (REG_NEWLINE << 1) - - -/* POSIX `eflags' bits (i.e., information for regexec). */ - -/* If this bit is set, then the beginning-of-line operator doesn't match - the beginning of the string (presumably because it's not the - beginning of a line). - If not set, then the beginning-of-line operator does match the - beginning of the string. */ -#define REG_NOTBOL 1 - -/* Like REG_NOTBOL, except for the end-of-line. */ -#define REG_NOTEOL (1 << 1) - -/* Use PMATCH[0] to delimit the start and end of the search in the - buffer. */ -#define REG_STARTEND (1 << 2) - - -/* If any error codes are removed, changed, or added, update the - `re_error_msg' table in regex.c. */ -typedef enum -{ -#if defined _XOPEN_SOURCE || defined __USE_XOPEN2K - REG_ENOSYS = -1, /* This will never happen for this implementation. */ -#endif - - REG_NOERROR = 0, /* Success. */ - REG_NOMATCH, /* Didn't find a match (for regexec). */ - - /* POSIX regcomp return error codes. (In the order listed in the - standard.) */ - REG_BADPAT, /* Invalid pattern. */ - REG_ECOLLATE, /* Inalid collating element. */ - REG_ECTYPE, /* Invalid character class name. */ - REG_EESCAPE, /* Trailing backslash. */ - REG_ESUBREG, /* Invalid back reference. */ - REG_EBRACK, /* Unmatched left bracket. */ - REG_EPAREN, /* Parenthesis imbalance. */ - REG_EBRACE, /* Unmatched \{. */ - REG_BADBR, /* Invalid contents of \{\}. */ - REG_ERANGE, /* Invalid range end. */ - REG_ESPACE, /* Ran out of memory. */ - REG_BADRPT, /* No preceding re for repetition op. */ - - /* Error codes we've added. */ - REG_EEND, /* Premature end. */ - REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */ - REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */ -} reg_errcode_t; - -/* This data structure represents a compiled pattern. Before calling - the pattern compiler, the fields `buffer', `allocated', `fastmap', - `translate', and `no_sub' can be set. After the pattern has been - compiled, the `re_nsub' field is available. All other fields are - private to the regex routines. */ - -#ifndef RE_TRANSLATE_TYPE -# define __RE_TRANSLATE_TYPE unsigned char * -# ifdef __USE_GNU -# define RE_TRANSLATE_TYPE __RE_TRANSLATE_TYPE -# endif -#endif - -#ifdef __USE_GNU -# define __REPB_PREFIX(name) name -#else -# define __REPB_PREFIX(name) __##name -#endif - -struct re_pattern_buffer -{ - /* Space that holds the compiled pattern. It is declared as - `unsigned char *' because its elements are sometimes used as - array indexes. */ - unsigned char *__REPB_PREFIX(buffer); - - /* Number of bytes to which `buffer' points. */ - unsigned long int __REPB_PREFIX(allocated); - - /* Number of bytes actually used in `buffer'. */ - unsigned long int __REPB_PREFIX(used); - - /* Syntax setting with which the pattern was compiled. */ - reg_syntax_t __REPB_PREFIX(syntax); - - /* Pointer to a fastmap, if any, otherwise zero. re_search uses the - fastmap, if there is one, to skip over impossible starting points - for matches. */ - char *__REPB_PREFIX(fastmap); - - /* Either a translate table to apply to all characters before - comparing them, or zero for no translation. The translation is - applied to a pattern when it is compiled and to a string when it - is matched. */ - __RE_TRANSLATE_TYPE __REPB_PREFIX(translate); - - /* Number of subexpressions found by the compiler. */ - size_t re_nsub; - - /* Zero if this pattern cannot match the empty string, one else. - Well, in truth it's used only in `re_search_2', to see whether or - not we should use the fastmap, so we don't set this absolutely - perfectly; see `re_compile_fastmap' (the `duplicate' case). */ - unsigned __REPB_PREFIX(can_be_null) : 1; - - /* If REGS_UNALLOCATED, allocate space in the `regs' structure - for `max (RE_NREGS, re_nsub + 1)' groups. - If REGS_REALLOCATE, reallocate space if necessary. - If REGS_FIXED, use what's there. */ -#ifdef __USE_GNU -# define REGS_UNALLOCATED 0 -# define REGS_REALLOCATE 1 -# define REGS_FIXED 2 -#endif - unsigned __REPB_PREFIX(regs_allocated) : 2; - - /* Set to zero when `regex_compile' compiles a pattern; set to one - by `re_compile_fastmap' if it updates the fastmap. */ - unsigned __REPB_PREFIX(fastmap_accurate) : 1; - - /* If set, `re_match_2' does not return information about - subexpressions. */ - unsigned __REPB_PREFIX(no_sub) : 1; - - /* If set, a beginning-of-line anchor doesn't match at the beginning - of the string. */ - unsigned __REPB_PREFIX(not_bol) : 1; - - /* Similarly for an end-of-line anchor. */ - unsigned __REPB_PREFIX(not_eol) : 1; - - /* If true, an anchor at a newline matches. */ - unsigned __REPB_PREFIX(newline_anchor) : 1; -}; - -typedef struct re_pattern_buffer regex_t; - -/* Type for byte offsets within the string. POSIX mandates this. */ -typedef int regoff_t; - - -#ifdef __USE_GNU -/* This is the structure we store register match data in. See - regex.texinfo for a full description of what registers match. */ -struct re_registers -{ - unsigned num_regs; - regoff_t *start; - regoff_t *end; -}; - - -/* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer, - `re_match_2' returns information about at least this many registers - the first time a `regs' structure is passed. */ -# ifndef RE_NREGS -# define RE_NREGS 30 -# endif -#endif - - -/* POSIX specification for registers. Aside from the different names than - `re_registers', POSIX uses an array of structures, instead of a - structure of arrays. */ -typedef struct -{ - regoff_t rm_so; /* Byte offset from string's start to substring's start. */ - regoff_t rm_eo; /* Byte offset from string's start to substring's end. */ -} regmatch_t; - -/* Declarations for routines. */ - -#ifdef __USE_GNU -/* Sets the current default syntax to SYNTAX, and return the old syntax. - You can also simply assign to the `re_syntax_options' variable. */ -extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax); - -/* Compile the regular expression PATTERN, with length LENGTH - and syntax given by the global `re_syntax_options', into the buffer - BUFFER. Return NULL if successful, and an error string if not. */ -extern const char *re_compile_pattern (const char *__pattern, size_t __length, - struct re_pattern_buffer *__buffer); - - -/* Compile a fastmap for the compiled pattern in BUFFER; used to - accelerate searches. Return 0 if successful and -2 if was an - internal error. */ -extern int re_compile_fastmap (struct re_pattern_buffer *__buffer); - - -/* Search in the string STRING (with length LENGTH) for the pattern - compiled into BUFFER. Start searching at position START, for RANGE - characters. Return the starting position of the match, -1 for no - match, or -2 for an internal error. Also return register - information in REGS (if REGS and BUFFER->no_sub are nonzero). */ -extern int re_search (struct re_pattern_buffer *__buffer, const char *__cstring, - int __length, int __start, int __range, - struct re_registers *__regs); - - -/* Like `re_search', but search in the concatenation of STRING1 and - STRING2. Also, stop searching at index START + STOP. */ -extern int re_search_2 (struct re_pattern_buffer *__buffer, - const char *__string1, int __length1, - const char *__string2, int __length2, int __start, - int __range, struct re_registers *__regs, int __stop); - - -/* Like `re_search', but return how many characters in STRING the regexp - in BUFFER matched, starting at position START. */ -extern int re_match (struct re_pattern_buffer *__buffer, const char *__cstring, - int __length, int __start, struct re_registers *__regs); - - -/* Relates to `re_match' as `re_search_2' relates to `re_search'. */ -extern int re_match_2 (struct re_pattern_buffer *__buffer, - const char *__string1, int __length1, - const char *__string2, int __length2, int __start, - struct re_registers *__regs, int __stop); - - -/* Set REGS to hold NUM_REGS registers, storing them in STARTS and - ENDS. Subsequent matches using BUFFER and REGS will use this memory - for recording register information. STARTS and ENDS must be - allocated with malloc, and must each be at least `NUM_REGS * sizeof - (regoff_t)' bytes long. - - If NUM_REGS == 0, then subsequent matches should allocate their own - register data. - - Unless this function is called, the first search or match using - PATTERN_BUFFER will allocate its own register data, without - freeing the old data. */ -extern void re_set_registers (struct re_pattern_buffer *__buffer, - struct re_registers *__regs, - unsigned int __num_regs, - regoff_t *__starts, regoff_t *__ends); -#endif /* Use GNU */ - -#if defined _REGEX_RE_COMP || (defined _LIBC && defined __USE_BSD) -# ifndef _CRAY -/* 4.2 bsd compatibility. */ -extern char *re_comp (const char *); -extern int re_exec (const char *); -# endif -#endif - -/* GCC 2.95 and later have "__restrict"; C99 compilers have - "restrict", and "configure" may have defined "restrict". */ -#ifndef __restrict -# if ! (2 < __GNUC__ || (2 == __GNUC__ && 95 <= __GNUC_MINOR__)) -# if defined restrict || 199901L <= __STDC_VERSION__ -# define __restrict restrict -# else -# define __restrict -# endif -# endif -#endif -/* gcc 3.1 and up support the [restrict] syntax. */ -#ifndef __restrict_arr -# if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) \ - && !defined __GNUG__ -# define __restrict_arr __restrict -# else -# define __restrict_arr -# endif -#endif - -/* POSIX compatibility. */ -extern int regcomp (regex_t *__restrict __preg, - const char *__restrict __pattern, - int __cflags); - -extern int regexec (const regex_t *__restrict __preg, - const char *__restrict __cstring, size_t __nmatch, - regmatch_t __pmatch[__restrict_arr], - int __eflags); - -extern size_t regerror (int __errcode, const regex_t *__restrict __preg, - char *__restrict __errbuf, size_t __errbuf_size); - -extern void regfree (regex_t *__preg); - - -#ifdef __cplusplus -} -#endif /* C++ */ - -#endif /* regex.h */ diff --git a/deps/regex/regex_internal.c b/deps/regex/regex_internal.c deleted file mode 100644 index 193854cf5b6..00000000000 --- a/deps/regex/regex_internal.c +++ /dev/null @@ -1,1744 +0,0 @@ -/* Extended regular expression matching and search library. - Copyright (C) 2002-2006, 2010 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Isamu Hasegawa . - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA. */ - -static void re_string_construct_common (const char *str, int len, - re_string_t *pstr, - RE_TRANSLATE_TYPE trans, int icase, - const re_dfa_t *dfa) internal_function; -static re_dfastate_t *create_ci_newstate (const re_dfa_t *dfa, - const re_node_set *nodes, - unsigned int hash) internal_function; -static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa, - const re_node_set *nodes, - unsigned int context, - unsigned int hash) internal_function; - -#ifdef GAWK -#undef MAX /* safety */ -static int -MAX(size_t a, size_t b) -{ - return (a > b ? a : b); -} -#endif - -/* Functions for string operation. */ - -/* This function allocate the buffers. It is necessary to call - re_string_reconstruct before using the object. */ - -static reg_errcode_t -internal_function -re_string_allocate (re_string_t *pstr, const char *str, int len, int init_len, - RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa) -{ - reg_errcode_t ret; - int init_buf_len; - - /* Ensure at least one character fits into the buffers. */ - if (init_len < dfa->mb_cur_max) - init_len = dfa->mb_cur_max; - init_buf_len = (len + 1 < init_len) ? len + 1: init_len; - re_string_construct_common (str, len, pstr, trans, icase, dfa); - - ret = re_string_realloc_buffers (pstr, init_buf_len); - if (BE (ret != REG_NOERROR, 0)) - return ret; - - pstr->word_char = dfa->word_char; - pstr->word_ops_used = dfa->word_ops_used; - pstr->mbs = pstr->mbs_allocated ? pstr->mbs : (unsigned char *) str; - pstr->valid_len = (pstr->mbs_allocated || dfa->mb_cur_max > 1) ? 0 : len; - pstr->valid_raw_len = pstr->valid_len; - return REG_NOERROR; -} - -/* This function allocate the buffers, and initialize them. */ - -static reg_errcode_t -internal_function -re_string_construct (re_string_t *pstr, const char *str, int len, - RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa) -{ - reg_errcode_t ret; - memset (pstr, '\0', sizeof (re_string_t)); - re_string_construct_common (str, len, pstr, trans, icase, dfa); - - if (len > 0) - { - ret = re_string_realloc_buffers (pstr, len + 1); - if (BE (ret != REG_NOERROR, 0)) - return ret; - } - pstr->mbs = pstr->mbs_allocated ? pstr->mbs : (unsigned char *) str; - - if (icase) - { -#ifdef RE_ENABLE_I18N - if (dfa->mb_cur_max > 1) - { - while (1) - { - ret = build_wcs_upper_buffer (pstr); - if (BE (ret != REG_NOERROR, 0)) - return ret; - if (pstr->valid_raw_len >= len) - break; - if (pstr->bufs_len > pstr->valid_len + dfa->mb_cur_max) - break; - ret = re_string_realloc_buffers (pstr, pstr->bufs_len * 2); - if (BE (ret != REG_NOERROR, 0)) - return ret; - } - } - else -#endif /* RE_ENABLE_I18N */ - build_upper_buffer (pstr); - } - else - { -#ifdef RE_ENABLE_I18N - if (dfa->mb_cur_max > 1) - build_wcs_buffer (pstr); - else -#endif /* RE_ENABLE_I18N */ - { - if (trans != NULL) - re_string_translate_buffer (pstr); - else - { - pstr->valid_len = pstr->bufs_len; - pstr->valid_raw_len = pstr->bufs_len; - } - } - } - - return REG_NOERROR; -} - -/* Helper functions for re_string_allocate, and re_string_construct. */ - -static reg_errcode_t -internal_function -re_string_realloc_buffers (re_string_t *pstr, int new_buf_len) -{ -#ifdef RE_ENABLE_I18N - if (pstr->mb_cur_max > 1) - { - wint_t *new_wcs; - - /* Avoid overflow in realloc. */ - const size_t max_object_size = MAX (sizeof (wint_t), sizeof (int)); - if (BE (SIZE_MAX / max_object_size < new_buf_len, 0)) - return REG_ESPACE; - - new_wcs = re_realloc (pstr->wcs, wint_t, new_buf_len); - if (BE (new_wcs == NULL, 0)) - return REG_ESPACE; - pstr->wcs = new_wcs; - if (pstr->offsets != NULL) - { - int *new_offsets = re_realloc (pstr->offsets, int, new_buf_len); - if (BE (new_offsets == NULL, 0)) - return REG_ESPACE; - pstr->offsets = new_offsets; - } - } -#endif /* RE_ENABLE_I18N */ - if (pstr->mbs_allocated) - { - unsigned char *new_mbs = re_realloc (pstr->mbs, unsigned char, - new_buf_len); - if (BE (new_mbs == NULL, 0)) - return REG_ESPACE; - pstr->mbs = new_mbs; - } - pstr->bufs_len = new_buf_len; - return REG_NOERROR; -} - - -static void -internal_function -re_string_construct_common (const char *str, int len, re_string_t *pstr, - RE_TRANSLATE_TYPE trans, int icase, - const re_dfa_t *dfa) -{ - pstr->raw_mbs = (const unsigned char *) str; - pstr->len = len; - pstr->raw_len = len; - pstr->trans = trans; - pstr->icase = icase ? 1 : 0; - pstr->mbs_allocated = (trans != NULL || icase); - pstr->mb_cur_max = dfa->mb_cur_max; - pstr->is_utf8 = dfa->is_utf8; - pstr->map_notascii = dfa->map_notascii; - pstr->stop = pstr->len; - pstr->raw_stop = pstr->stop; -} - -#ifdef RE_ENABLE_I18N - -/* Build wide character buffer PSTR->WCS. - If the byte sequence of the string are: - (0), (1), (0), (1), - Then wide character buffer will be: - , WEOF , , WEOF , - We use WEOF for padding, they indicate that the position isn't - a first byte of a multibyte character. - - Note that this function assumes PSTR->VALID_LEN elements are already - built and starts from PSTR->VALID_LEN. */ - -static void -internal_function -build_wcs_buffer (re_string_t *pstr) -{ -#ifdef _LIBC - unsigned char buf[MB_LEN_MAX]; - assert (MB_LEN_MAX >= pstr->mb_cur_max); -#else - unsigned char buf[64]; -#endif - mbstate_t prev_st; - int byte_idx, end_idx, remain_len; - size_t mbclen; - - /* Build the buffers from pstr->valid_len to either pstr->len or - pstr->bufs_len. */ - end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len; - for (byte_idx = pstr->valid_len; byte_idx < end_idx;) - { - wchar_t wc; - const char *p; - - remain_len = end_idx - byte_idx; - prev_st = pstr->cur_state; - /* Apply the translation if we need. */ - if (BE (pstr->trans != NULL, 0)) - { - int i, ch; - - for (i = 0; i < pstr->mb_cur_max && i < remain_len; ++i) - { - ch = pstr->raw_mbs [pstr->raw_mbs_idx + byte_idx + i]; - buf[i] = pstr->mbs[byte_idx + i] = pstr->trans[ch]; - } - p = (const char *) buf; - } - else - p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + byte_idx; - mbclen = __mbrtowc (&wc, p, remain_len, &pstr->cur_state); - if (BE (mbclen == (size_t) -2, 0)) - { - /* The buffer doesn't have enough space, finish to build. */ - pstr->cur_state = prev_st; - break; - } - else if (BE (mbclen == (size_t) -1 || mbclen == 0, 0)) - { - /* We treat these cases as a singlebyte character. */ - mbclen = 1; - wc = (wchar_t) pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx]; - if (BE (pstr->trans != NULL, 0)) - wc = pstr->trans[wc]; - pstr->cur_state = prev_st; - } - - /* Write wide character and padding. */ - pstr->wcs[byte_idx++] = wc; - /* Write paddings. */ - for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;) - pstr->wcs[byte_idx++] = WEOF; - } - pstr->valid_len = byte_idx; - pstr->valid_raw_len = byte_idx; -} - -/* Build wide character buffer PSTR->WCS like build_wcs_buffer, - but for REG_ICASE. */ - -static reg_errcode_t -internal_function -build_wcs_upper_buffer (re_string_t *pstr) -{ - mbstate_t prev_st; - int src_idx, byte_idx, end_idx, remain_len; - size_t mbclen; -#ifdef _LIBC - char buf[MB_LEN_MAX]; - assert (MB_LEN_MAX >= pstr->mb_cur_max); -#else - char buf[64]; -#endif - - byte_idx = pstr->valid_len; - end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len; - - /* The following optimization assumes that ASCII characters can be - mapped to wide characters with a simple cast. */ - if (! pstr->map_notascii && pstr->trans == NULL && !pstr->offsets_needed) - { - while (byte_idx < end_idx) - { - wchar_t wc; - - if (isascii (pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx]) - && mbsinit (&pstr->cur_state)) - { - /* In case of a singlebyte character. */ - pstr->mbs[byte_idx] - = toupper (pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx]); - /* The next step uses the assumption that wchar_t is encoded - ASCII-safe: all ASCII values can be converted like this. */ - pstr->wcs[byte_idx] = (wchar_t) pstr->mbs[byte_idx]; - ++byte_idx; - continue; - } - - remain_len = end_idx - byte_idx; - prev_st = pstr->cur_state; - mbclen = __mbrtowc (&wc, - ((const char *) pstr->raw_mbs + pstr->raw_mbs_idx - + byte_idx), remain_len, &pstr->cur_state); - if (BE (mbclen + 2 > 2, 1)) - { - wchar_t wcu = wc; - if (iswlower (wc)) - { - size_t mbcdlen; - - wcu = towupper (wc); - mbcdlen = wcrtomb (buf, wcu, &prev_st); - if (BE (mbclen == mbcdlen, 1)) - memcpy (pstr->mbs + byte_idx, buf, mbclen); - else - { - src_idx = byte_idx; - goto offsets_needed; - } - } - else - memcpy (pstr->mbs + byte_idx, - pstr->raw_mbs + pstr->raw_mbs_idx + byte_idx, mbclen); - pstr->wcs[byte_idx++] = wcu; - /* Write paddings. */ - for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;) - pstr->wcs[byte_idx++] = WEOF; - } - else if (mbclen == (size_t) -1 || mbclen == 0) - { - /* It is an invalid character or '\0'. Just use the byte. */ - int ch = pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx]; - pstr->mbs[byte_idx] = ch; - /* And also cast it to wide char. */ - pstr->wcs[byte_idx++] = (wchar_t) ch; - if (BE (mbclen == (size_t) -1, 0)) - pstr->cur_state = prev_st; - } - else - { - /* The buffer doesn't have enough space, finish to build. */ - pstr->cur_state = prev_st; - break; - } - } - pstr->valid_len = byte_idx; - pstr->valid_raw_len = byte_idx; - return REG_NOERROR; - } - else - for (src_idx = pstr->valid_raw_len; byte_idx < end_idx;) - { - wchar_t wc; - const char *p; - offsets_needed: - remain_len = end_idx - byte_idx; - prev_st = pstr->cur_state; - if (BE (pstr->trans != NULL, 0)) - { - int i, ch; - - for (i = 0; i < pstr->mb_cur_max && i < remain_len; ++i) - { - ch = pstr->raw_mbs [pstr->raw_mbs_idx + src_idx + i]; - buf[i] = pstr->trans[ch]; - } - p = (const char *) buf; - } - else - p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx; - mbclen = __mbrtowc (&wc, p, remain_len, &pstr->cur_state); - if (BE (mbclen + 2 > 2, 1)) - { - wchar_t wcu = wc; - if (iswlower (wc)) - { - size_t mbcdlen; - - wcu = towupper (wc); - mbcdlen = wcrtomb ((char *) buf, wcu, &prev_st); - if (BE (mbclen == mbcdlen, 1)) - memcpy (pstr->mbs + byte_idx, buf, mbclen); - else if (mbcdlen != (size_t) -1) - { - size_t i; - - if (byte_idx + mbcdlen > pstr->bufs_len) - { - pstr->cur_state = prev_st; - break; - } - - if (pstr->offsets == NULL) - { - pstr->offsets = re_malloc (int, pstr->bufs_len); - - if (pstr->offsets == NULL) - return REG_ESPACE; - } - if (!pstr->offsets_needed) - { - for (i = 0; i < (size_t) byte_idx; ++i) - pstr->offsets[i] = i; - pstr->offsets_needed = 1; - } - - memcpy (pstr->mbs + byte_idx, buf, mbcdlen); - pstr->wcs[byte_idx] = wcu; - pstr->offsets[byte_idx] = src_idx; - for (i = 1; i < mbcdlen; ++i) - { - pstr->offsets[byte_idx + i] - = src_idx + (i < mbclen ? i : mbclen - 1); - pstr->wcs[byte_idx + i] = WEOF; - } - pstr->len += mbcdlen - mbclen; - if (pstr->raw_stop > src_idx) - pstr->stop += mbcdlen - mbclen; - end_idx = (pstr->bufs_len > pstr->len) - ? pstr->len : pstr->bufs_len; - byte_idx += mbcdlen; - src_idx += mbclen; - continue; - } - else - memcpy (pstr->mbs + byte_idx, p, mbclen); - } - else - memcpy (pstr->mbs + byte_idx, p, mbclen); - - if (BE (pstr->offsets_needed != 0, 0)) - { - size_t i; - for (i = 0; i < mbclen; ++i) - pstr->offsets[byte_idx + i] = src_idx + i; - } - src_idx += mbclen; - - pstr->wcs[byte_idx++] = wcu; - /* Write paddings. */ - for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;) - pstr->wcs[byte_idx++] = WEOF; - } - else if (mbclen == (size_t) -1 || mbclen == 0) - { - /* It is an invalid character or '\0'. Just use the byte. */ - int ch = pstr->raw_mbs[pstr->raw_mbs_idx + src_idx]; - - if (BE (pstr->trans != NULL, 0)) - ch = pstr->trans [ch]; - pstr->mbs[byte_idx] = ch; - - if (BE (pstr->offsets_needed != 0, 0)) - pstr->offsets[byte_idx] = src_idx; - ++src_idx; - - /* And also cast it to wide char. */ - pstr->wcs[byte_idx++] = (wchar_t) ch; - if (BE (mbclen == (size_t) -1, 0)) - pstr->cur_state = prev_st; - } - else - { - /* The buffer doesn't have enough space, finish to build. */ - pstr->cur_state = prev_st; - break; - } - } - pstr->valid_len = byte_idx; - pstr->valid_raw_len = src_idx; - return REG_NOERROR; -} - -/* Skip characters until the index becomes greater than NEW_RAW_IDX. - Return the index. */ - -static int -internal_function -re_string_skip_chars (re_string_t *pstr, int new_raw_idx, wint_t *last_wc) -{ - mbstate_t prev_st; - int rawbuf_idx; - size_t mbclen; - wint_t wc = WEOF; - - /* Skip the characters which are not necessary to check. */ - for (rawbuf_idx = pstr->raw_mbs_idx + pstr->valid_raw_len; - rawbuf_idx < new_raw_idx;) - { - wchar_t wc2; - int remain_len = pstr->len - rawbuf_idx; - prev_st = pstr->cur_state; - mbclen = __mbrtowc (&wc2, (const char *) pstr->raw_mbs + rawbuf_idx, - remain_len, &pstr->cur_state); - if (BE (mbclen == (size_t) -2 || mbclen == (size_t) -1 || mbclen == 0, 0)) - { - /* We treat these cases as a single byte character. */ - if (mbclen == 0 || remain_len == 0) - wc = L'\0'; - else - wc = *(unsigned char *) (pstr->raw_mbs + rawbuf_idx); - mbclen = 1; - pstr->cur_state = prev_st; - } - else - wc = (wint_t) wc2; - /* Then proceed the next character. */ - rawbuf_idx += mbclen; - } - *last_wc = (wint_t) wc; - return rawbuf_idx; -} -#endif /* RE_ENABLE_I18N */ - -/* Build the buffer PSTR->MBS, and apply the translation if we need. - This function is used in case of REG_ICASE. */ - -static void -internal_function -build_upper_buffer (re_string_t *pstr) -{ - int char_idx, end_idx; - end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len; - - for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx) - { - int ch = pstr->raw_mbs[pstr->raw_mbs_idx + char_idx]; - if (BE (pstr->trans != NULL, 0)) - ch = pstr->trans[ch]; - if (islower (ch)) - pstr->mbs[char_idx] = toupper (ch); - else - pstr->mbs[char_idx] = ch; - } - pstr->valid_len = char_idx; - pstr->valid_raw_len = char_idx; -} - -/* Apply TRANS to the buffer in PSTR. */ - -static void -internal_function -re_string_translate_buffer (re_string_t *pstr) -{ - int buf_idx, end_idx; - end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len; - - for (buf_idx = pstr->valid_len; buf_idx < end_idx; ++buf_idx) - { - int ch = pstr->raw_mbs[pstr->raw_mbs_idx + buf_idx]; - pstr->mbs[buf_idx] = pstr->trans[ch]; - } - - pstr->valid_len = buf_idx; - pstr->valid_raw_len = buf_idx; -} - -/* This function re-construct the buffers. - Concretely, convert to wide character in case of pstr->mb_cur_max > 1, - convert to upper case in case of REG_ICASE, apply translation. */ - -static reg_errcode_t -internal_function -re_string_reconstruct (re_string_t *pstr, int idx, int eflags) -{ - int offset = idx - pstr->raw_mbs_idx; - if (BE (offset < 0, 0)) - { - /* Reset buffer. */ -#ifdef RE_ENABLE_I18N - if (pstr->mb_cur_max > 1) - memset (&pstr->cur_state, '\0', sizeof (mbstate_t)); -#endif /* RE_ENABLE_I18N */ - pstr->len = pstr->raw_len; - pstr->stop = pstr->raw_stop; - pstr->valid_len = 0; - pstr->raw_mbs_idx = 0; - pstr->valid_raw_len = 0; - pstr->offsets_needed = 0; - pstr->tip_context = ((eflags & REG_NOTBOL) ? CONTEXT_BEGBUF - : CONTEXT_NEWLINE | CONTEXT_BEGBUF); - if (!pstr->mbs_allocated) - pstr->mbs = (unsigned char *) pstr->raw_mbs; - offset = idx; - } - - if (BE (offset != 0, 1)) - { - /* Should the already checked characters be kept? */ - if (BE (offset < pstr->valid_raw_len, 1)) - { - /* Yes, move them to the front of the buffer. */ -#ifdef RE_ENABLE_I18N - if (BE (pstr->offsets_needed, 0)) - { - int low = 0, high = pstr->valid_len, mid; - do - { - mid = (high + low) / 2; - if (pstr->offsets[mid] > offset) - high = mid; - else if (pstr->offsets[mid] < offset) - low = mid + 1; - else - break; - } - while (low < high); - if (pstr->offsets[mid] < offset) - ++mid; - pstr->tip_context = re_string_context_at (pstr, mid - 1, - eflags); - /* This can be quite complicated, so handle specially - only the common and easy case where the character with - different length representation of lower and upper - case is present at or after offset. */ - if (pstr->valid_len > offset - && mid == offset && pstr->offsets[mid] == offset) - { - memmove (pstr->wcs, pstr->wcs + offset, - (pstr->valid_len - offset) * sizeof (wint_t)); - memmove (pstr->mbs, pstr->mbs + offset, pstr->valid_len - offset); - pstr->valid_len -= offset; - pstr->valid_raw_len -= offset; - for (low = 0; low < pstr->valid_len; low++) - pstr->offsets[low] = pstr->offsets[low + offset] - offset; - } - else - { - /* Otherwise, just find out how long the partial multibyte - character at offset is and fill it with WEOF/255. */ - pstr->len = pstr->raw_len - idx + offset; - pstr->stop = pstr->raw_stop - idx + offset; - pstr->offsets_needed = 0; - while (mid > 0 && pstr->offsets[mid - 1] == offset) - --mid; - while (mid < pstr->valid_len) - if (pstr->wcs[mid] != WEOF) - break; - else - ++mid; - if (mid == pstr->valid_len) - pstr->valid_len = 0; - else - { - pstr->valid_len = pstr->offsets[mid] - offset; - if (pstr->valid_len) - { - for (low = 0; low < pstr->valid_len; ++low) - pstr->wcs[low] = WEOF; - memset (pstr->mbs, 255, pstr->valid_len); - } - } - pstr->valid_raw_len = pstr->valid_len; - } - } - else -#endif - { - pstr->tip_context = re_string_context_at (pstr, offset - 1, - eflags); -#ifdef RE_ENABLE_I18N - if (pstr->mb_cur_max > 1) - memmove (pstr->wcs, pstr->wcs + offset, - (pstr->valid_len - offset) * sizeof (wint_t)); -#endif /* RE_ENABLE_I18N */ - if (BE (pstr->mbs_allocated, 0)) - memmove (pstr->mbs, pstr->mbs + offset, - pstr->valid_len - offset); - pstr->valid_len -= offset; - pstr->valid_raw_len -= offset; -#if DEBUG - assert (pstr->valid_len > 0); -#endif - } - } - else - { -#ifdef RE_ENABLE_I18N - /* No, skip all characters until IDX. */ - int prev_valid_len = pstr->valid_len; - - if (BE (pstr->offsets_needed, 0)) - { - pstr->len = pstr->raw_len - idx + offset; - pstr->stop = pstr->raw_stop - idx + offset; - pstr->offsets_needed = 0; - } -#endif - pstr->valid_len = 0; -#ifdef RE_ENABLE_I18N - if (pstr->mb_cur_max > 1) - { - int wcs_idx; - wint_t wc = WEOF; - - if (pstr->is_utf8) - { - const unsigned char *raw, *p, *end; - - /* Special case UTF-8. Multi-byte chars start with any - byte other than 0x80 - 0xbf. */ - raw = pstr->raw_mbs + pstr->raw_mbs_idx; - end = raw + (offset - pstr->mb_cur_max); - if (end < pstr->raw_mbs) - end = pstr->raw_mbs; - p = raw + offset - 1; -#ifdef _LIBC - /* We know the wchar_t encoding is UCS4, so for the simple - case, ASCII characters, skip the conversion step. */ - if (isascii (*p) && BE (pstr->trans == NULL, 1)) - { - memset (&pstr->cur_state, '\0', sizeof (mbstate_t)); - /* pstr->valid_len = 0; */ - wc = (wchar_t) *p; - } - else -#endif - for (; p >= end; --p) - if ((*p & 0xc0) != 0x80) - { - mbstate_t cur_state; - wchar_t wc2; - int mlen = raw + pstr->len - p; - unsigned char buf[6]; - size_t mbclen; - - if (BE (pstr->trans != NULL, 0)) - { - int i = mlen < 6 ? mlen : 6; - while (--i >= 0) - buf[i] = pstr->trans[p[i]]; - } - /* XXX Don't use mbrtowc, we know which conversion - to use (UTF-8 -> UCS4). */ - memset (&cur_state, 0, sizeof (cur_state)); - mbclen = __mbrtowc (&wc2, (const char *) p, mlen, - &cur_state); - if (raw + offset - p <= mbclen - && mbclen < (size_t) -2) - { - memset (&pstr->cur_state, '\0', - sizeof (mbstate_t)); - pstr->valid_len = mbclen - (raw + offset - p); - wc = wc2; - } - break; - } - } - - if (wc == WEOF) - pstr->valid_len = re_string_skip_chars (pstr, idx, &wc) - idx; - if (wc == WEOF) - pstr->tip_context - = re_string_context_at (pstr, prev_valid_len - 1, eflags); - else - pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0) - && IS_WIDE_WORD_CHAR (wc)) - ? CONTEXT_WORD - : ((IS_WIDE_NEWLINE (wc) - && pstr->newline_anchor) - ? CONTEXT_NEWLINE : 0)); - if (BE (pstr->valid_len, 0)) - { - for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx) - pstr->wcs[wcs_idx] = WEOF; - if (pstr->mbs_allocated) - memset (pstr->mbs, 255, pstr->valid_len); - } - pstr->valid_raw_len = pstr->valid_len; - } - else -#endif /* RE_ENABLE_I18N */ - { - int c = pstr->raw_mbs[pstr->raw_mbs_idx + offset - 1]; - pstr->valid_raw_len = 0; - if (pstr->trans) - c = pstr->trans[c]; - pstr->tip_context = (bitset_contain (pstr->word_char, c) - ? CONTEXT_WORD - : ((IS_NEWLINE (c) && pstr->newline_anchor) - ? CONTEXT_NEWLINE : 0)); - } - } - if (!BE (pstr->mbs_allocated, 0)) - pstr->mbs += offset; - } - pstr->raw_mbs_idx = idx; - pstr->len -= offset; - pstr->stop -= offset; - - /* Then build the buffers. */ -#ifdef RE_ENABLE_I18N - if (pstr->mb_cur_max > 1) - { - if (pstr->icase) - { - reg_errcode_t ret = build_wcs_upper_buffer (pstr); - if (BE (ret != REG_NOERROR, 0)) - return ret; - } - else - build_wcs_buffer (pstr); - } - else -#endif /* RE_ENABLE_I18N */ - if (BE (pstr->mbs_allocated, 0)) - { - if (pstr->icase) - build_upper_buffer (pstr); - else if (pstr->trans != NULL) - re_string_translate_buffer (pstr); - } - else - pstr->valid_len = pstr->len; - - pstr->cur_idx = 0; - return REG_NOERROR; -} - -static unsigned char -internal_function __attribute ((pure)) -re_string_peek_byte_case (const re_string_t *pstr, int idx) -{ - int ch, off; - - /* Handle the common (easiest) cases first. */ - if (BE (!pstr->mbs_allocated, 1)) - return re_string_peek_byte (pstr, idx); - -#ifdef RE_ENABLE_I18N - if (pstr->mb_cur_max > 1 - && ! re_string_is_single_byte_char (pstr, pstr->cur_idx + idx)) - return re_string_peek_byte (pstr, idx); -#endif - - off = pstr->cur_idx + idx; -#ifdef RE_ENABLE_I18N - if (pstr->offsets_needed) - off = pstr->offsets[off]; -#endif - - ch = pstr->raw_mbs[pstr->raw_mbs_idx + off]; - -#ifdef RE_ENABLE_I18N - /* Ensure that e.g. for tr_TR.UTF-8 BACKSLASH DOTLESS SMALL LETTER I - this function returns CAPITAL LETTER I instead of first byte of - DOTLESS SMALL LETTER I. The latter would confuse the parser, - since peek_byte_case doesn't advance cur_idx in any way. */ - if (pstr->offsets_needed && !isascii (ch)) - return re_string_peek_byte (pstr, idx); -#endif - - return ch; -} - -static unsigned char -internal_function __attribute ((pure)) -re_string_fetch_byte_case (re_string_t *pstr) -{ - if (BE (!pstr->mbs_allocated, 1)) - return re_string_fetch_byte (pstr); - -#ifdef RE_ENABLE_I18N - if (pstr->offsets_needed) - { - int off, ch; - - /* For tr_TR.UTF-8 [[:islower:]] there is - [[: CAPITAL LETTER I WITH DOT lower:]] in mbs. Skip - in that case the whole multi-byte character and return - the original letter. On the other side, with - [[: DOTLESS SMALL LETTER I return [[:I, as doing - anything else would complicate things too much. */ - - if (!re_string_first_byte (pstr, pstr->cur_idx)) - return re_string_fetch_byte (pstr); - - off = pstr->offsets[pstr->cur_idx]; - ch = pstr->raw_mbs[pstr->raw_mbs_idx + off]; - - if (! isascii (ch)) - return re_string_fetch_byte (pstr); - - re_string_skip_bytes (pstr, - re_string_char_size_at (pstr, pstr->cur_idx)); - return ch; - } -#endif - - return pstr->raw_mbs[pstr->raw_mbs_idx + pstr->cur_idx++]; -} - -static void -internal_function -re_string_destruct (re_string_t *pstr) -{ -#ifdef RE_ENABLE_I18N - re_free (pstr->wcs); - re_free (pstr->offsets); -#endif /* RE_ENABLE_I18N */ - if (pstr->mbs_allocated) - re_free (pstr->mbs); -} - -/* Return the context at IDX in INPUT. */ - -static unsigned int -internal_function -re_string_context_at (const re_string_t *input, int idx, int eflags) -{ - int c; - if (BE (idx < 0, 0)) - /* In this case, we use the value stored in input->tip_context, - since we can't know the character in input->mbs[-1] here. */ - return input->tip_context; - if (BE (idx == input->len, 0)) - return ((eflags & REG_NOTEOL) ? CONTEXT_ENDBUF - : CONTEXT_NEWLINE | CONTEXT_ENDBUF); -#ifdef RE_ENABLE_I18N - if (input->mb_cur_max > 1) - { - wint_t wc; - int wc_idx = idx; - while(input->wcs[wc_idx] == WEOF) - { -#ifdef DEBUG - /* It must not happen. */ - assert (wc_idx >= 0); -#endif - --wc_idx; - if (wc_idx < 0) - return input->tip_context; - } - wc = input->wcs[wc_idx]; - if (BE (input->word_ops_used != 0, 0) && IS_WIDE_WORD_CHAR (wc)) - return CONTEXT_WORD; - return (IS_WIDE_NEWLINE (wc) && input->newline_anchor - ? CONTEXT_NEWLINE : 0); - } - else -#endif - { - c = re_string_byte_at (input, idx); - if (bitset_contain (input->word_char, c)) - return CONTEXT_WORD; - return IS_NEWLINE (c) && input->newline_anchor ? CONTEXT_NEWLINE : 0; - } -} - -/* Functions for set operation. */ - -static reg_errcode_t -internal_function -re_node_set_alloc (re_node_set *set, int size) -{ - /* - * ADR: valgrind says size can be 0, which then doesn't - * free the block of size 0. Harumph. This seems - * to work ok, though. - */ - if (size == 0) - { - memset(set, 0, sizeof(*set)); - return REG_NOERROR; - } - set->alloc = size; - set->nelem = 0; - set->elems = re_malloc (int, size); - if (BE (set->elems == NULL, 0)) - return REG_ESPACE; - return REG_NOERROR; -} - -static reg_errcode_t -internal_function -re_node_set_init_1 (re_node_set *set, int elem) -{ - set->alloc = 1; - set->nelem = 1; - set->elems = re_malloc (int, 1); - if (BE (set->elems == NULL, 0)) - { - set->alloc = set->nelem = 0; - return REG_ESPACE; - } - set->elems[0] = elem; - return REG_NOERROR; -} - -static reg_errcode_t -internal_function -re_node_set_init_2 (re_node_set *set, int elem1, int elem2) -{ - set->alloc = 2; - set->elems = re_malloc (int, 2); - if (BE (set->elems == NULL, 0)) - return REG_ESPACE; - if (elem1 == elem2) - { - set->nelem = 1; - set->elems[0] = elem1; - } - else - { - set->nelem = 2; - if (elem1 < elem2) - { - set->elems[0] = elem1; - set->elems[1] = elem2; - } - else - { - set->elems[0] = elem2; - set->elems[1] = elem1; - } - } - return REG_NOERROR; -} - -static reg_errcode_t -internal_function -re_node_set_init_copy (re_node_set *dest, const re_node_set *src) -{ - dest->nelem = src->nelem; - if (src->nelem > 0) - { - dest->alloc = dest->nelem; - dest->elems = re_malloc (int, dest->alloc); - if (BE (dest->elems == NULL, 0)) - { - dest->alloc = dest->nelem = 0; - return REG_ESPACE; - } - memcpy (dest->elems, src->elems, src->nelem * sizeof (int)); - } - else - re_node_set_init_empty (dest); - return REG_NOERROR; -} - -/* Calculate the intersection of the sets SRC1 and SRC2. And merge it to - DEST. Return value indicate the error code or REG_NOERROR if succeeded. - Note: We assume dest->elems is NULL, when dest->alloc is 0. */ - -static reg_errcode_t -internal_function -re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1, - const re_node_set *src2) -{ - int i1, i2, is, id, delta, sbase; - if (src1->nelem == 0 || src2->nelem == 0) - return REG_NOERROR; - - /* We need dest->nelem + 2 * elems_in_intersection; this is a - conservative estimate. */ - if (src1->nelem + src2->nelem + dest->nelem > dest->alloc) - { - int new_alloc = src1->nelem + src2->nelem + dest->alloc; - int *new_elems = re_realloc (dest->elems, int, new_alloc); - if (BE (new_elems == NULL, 0)) - return REG_ESPACE; - dest->elems = new_elems; - dest->alloc = new_alloc; - } - - /* Find the items in the intersection of SRC1 and SRC2, and copy - into the top of DEST those that are not already in DEST itself. */ - sbase = dest->nelem + src1->nelem + src2->nelem; - i1 = src1->nelem - 1; - i2 = src2->nelem - 1; - id = dest->nelem - 1; - for (;;) - { - if (src1->elems[i1] == src2->elems[i2]) - { - /* Try to find the item in DEST. Maybe we could binary search? */ - while (id >= 0 && dest->elems[id] > src1->elems[i1]) - --id; - - if (id < 0 || dest->elems[id] != src1->elems[i1]) - dest->elems[--sbase] = src1->elems[i1]; - - if (--i1 < 0 || --i2 < 0) - break; - } - - /* Lower the highest of the two items. */ - else if (src1->elems[i1] < src2->elems[i2]) - { - if (--i2 < 0) - break; - } - else - { - if (--i1 < 0) - break; - } - } - - id = dest->nelem - 1; - is = dest->nelem + src1->nelem + src2->nelem - 1; - delta = is - sbase + 1; - - /* Now copy. When DELTA becomes zero, the remaining - DEST elements are already in place; this is more or - less the same loop that is in re_node_set_merge. */ - dest->nelem += delta; - if (delta > 0 && id >= 0) - for (;;) - { - if (dest->elems[is] > dest->elems[id]) - { - /* Copy from the top. */ - dest->elems[id + delta--] = dest->elems[is--]; - if (delta == 0) - break; - } - else - { - /* Slide from the bottom. */ - dest->elems[id + delta] = dest->elems[id]; - if (--id < 0) - break; - } - } - - /* Copy remaining SRC elements. */ - memcpy (dest->elems, dest->elems + sbase, delta * sizeof (int)); - - return REG_NOERROR; -} - -/* Calculate the union set of the sets SRC1 and SRC2. And store it to - DEST. Return value indicate the error code or REG_NOERROR if succeeded. */ - -static reg_errcode_t -internal_function -re_node_set_init_union (re_node_set *dest, const re_node_set *src1, - const re_node_set *src2) -{ - int i1, i2, id; - if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0) - { - dest->alloc = src1->nelem + src2->nelem; - dest->elems = re_malloc (int, dest->alloc); - if (BE (dest->elems == NULL, 0)) - return REG_ESPACE; - } - else - { - if (src1 != NULL && src1->nelem > 0) - return re_node_set_init_copy (dest, src1); - else if (src2 != NULL && src2->nelem > 0) - return re_node_set_init_copy (dest, src2); - else - re_node_set_init_empty (dest); - return REG_NOERROR; - } - for (i1 = i2 = id = 0 ; i1 < src1->nelem && i2 < src2->nelem ;) - { - if (src1->elems[i1] > src2->elems[i2]) - { - dest->elems[id++] = src2->elems[i2++]; - continue; - } - if (src1->elems[i1] == src2->elems[i2]) - ++i2; - dest->elems[id++] = src1->elems[i1++]; - } - if (i1 < src1->nelem) - { - memcpy (dest->elems + id, src1->elems + i1, - (src1->nelem - i1) * sizeof (int)); - id += src1->nelem - i1; - } - else if (i2 < src2->nelem) - { - memcpy (dest->elems + id, src2->elems + i2, - (src2->nelem - i2) * sizeof (int)); - id += src2->nelem - i2; - } - dest->nelem = id; - return REG_NOERROR; -} - -/* Calculate the union set of the sets DEST and SRC. And store it to - DEST. Return value indicate the error code or REG_NOERROR if succeeded. */ - -static reg_errcode_t -internal_function -re_node_set_merge (re_node_set *dest, const re_node_set *src) -{ - int is, id, sbase, delta; - if (src == NULL || src->nelem == 0) - return REG_NOERROR; - if (dest->alloc < 2 * src->nelem + dest->nelem) - { - int new_alloc = 2 * (src->nelem + dest->alloc); - int *new_buffer = re_realloc (dest->elems, int, new_alloc); - if (BE (new_buffer == NULL, 0)) - return REG_ESPACE; - dest->elems = new_buffer; - dest->alloc = new_alloc; - } - - if (BE (dest->nelem == 0, 0)) - { - dest->nelem = src->nelem; - memcpy (dest->elems, src->elems, src->nelem * sizeof (int)); - return REG_NOERROR; - } - - /* Copy into the top of DEST the items of SRC that are not - found in DEST. Maybe we could binary search in DEST? */ - for (sbase = dest->nelem + 2 * src->nelem, - is = src->nelem - 1, id = dest->nelem - 1; is >= 0 && id >= 0; ) - { - if (dest->elems[id] == src->elems[is]) - is--, id--; - else if (dest->elems[id] < src->elems[is]) - dest->elems[--sbase] = src->elems[is--]; - else /* if (dest->elems[id] > src->elems[is]) */ - --id; - } - - if (is >= 0) - { - /* If DEST is exhausted, the remaining items of SRC must be unique. */ - sbase -= is + 1; - memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (int)); - } - - id = dest->nelem - 1; - is = dest->nelem + 2 * src->nelem - 1; - delta = is - sbase + 1; - if (delta == 0) - return REG_NOERROR; - - /* Now copy. When DELTA becomes zero, the remaining - DEST elements are already in place. */ - dest->nelem += delta; - for (;;) - { - if (dest->elems[is] > dest->elems[id]) - { - /* Copy from the top. */ - dest->elems[id + delta--] = dest->elems[is--]; - if (delta == 0) - break; - } - else - { - /* Slide from the bottom. */ - dest->elems[id + delta] = dest->elems[id]; - if (--id < 0) - { - /* Copy remaining SRC elements. */ - memcpy (dest->elems, dest->elems + sbase, - delta * sizeof (int)); - break; - } - } - } - - return REG_NOERROR; -} - -/* Insert the new element ELEM to the re_node_set* SET. - SET should not already have ELEM. - return -1 if an error is occured, return 1 otherwise. */ - -static int -internal_function -re_node_set_insert (re_node_set *set, int elem) -{ - int idx; - /* In case the set is empty. */ - if (set->alloc == 0) - { - if (BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1)) - return 1; - else - return -1; - } - - if (BE (set->nelem, 0) == 0) - { - /* We already guaranteed above that set->alloc != 0. */ - set->elems[0] = elem; - ++set->nelem; - return 1; - } - - /* Realloc if we need. */ - if (set->alloc == set->nelem) - { - int *new_elems; - set->alloc = set->alloc * 2; - new_elems = re_realloc (set->elems, int, set->alloc); - if (BE (new_elems == NULL, 0)) - return -1; - set->elems = new_elems; - } - - /* Move the elements which follows the new element. Test the - first element separately to skip a check in the inner loop. */ - if (elem < set->elems[0]) - { - idx = 0; - for (idx = set->nelem; idx > 0; idx--) - set->elems[idx] = set->elems[idx - 1]; - } - else - { - for (idx = set->nelem; set->elems[idx - 1] > elem; idx--) - set->elems[idx] = set->elems[idx - 1]; - } - - /* Insert the new element. */ - set->elems[idx] = elem; - ++set->nelem; - return 1; -} - -/* Insert the new element ELEM to the re_node_set* SET. - SET should not already have any element greater than or equal to ELEM. - Return -1 if an error is occured, return 1 otherwise. */ - -static int -internal_function -re_node_set_insert_last (re_node_set *set, int elem) -{ - /* Realloc if we need. */ - if (set->alloc == set->nelem) - { - int *new_elems; - set->alloc = (set->alloc + 1) * 2; - new_elems = re_realloc (set->elems, int, set->alloc); - if (BE (new_elems == NULL, 0)) - return -1; - set->elems = new_elems; - } - - /* Insert the new element. */ - set->elems[set->nelem++] = elem; - return 1; -} - -/* Compare two node sets SET1 and SET2. - return 1 if SET1 and SET2 are equivalent, return 0 otherwise. */ - -static int -internal_function __attribute ((pure)) -re_node_set_compare (const re_node_set *set1, const re_node_set *set2) -{ - int i; - if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem) - return 0; - for (i = set1->nelem ; --i >= 0 ; ) - if (set1->elems[i] != set2->elems[i]) - return 0; - return 1; -} - -/* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise. */ - -static int -internal_function __attribute ((pure)) -re_node_set_contains (const re_node_set *set, int elem) -{ - unsigned int idx, right, mid; - if (set->nelem <= 0) - return 0; - - /* Binary search the element. */ - idx = 0; - right = set->nelem - 1; - while (idx < right) - { - mid = (idx + right) / 2; - if (set->elems[mid] < elem) - idx = mid + 1; - else - right = mid; - } - return set->elems[idx] == elem ? idx + 1 : 0; -} - -static void -internal_function -re_node_set_remove_at (re_node_set *set, int idx) -{ - if (idx < 0 || idx >= set->nelem) - return; - --set->nelem; - for (; idx < set->nelem; idx++) - set->elems[idx] = set->elems[idx + 1]; -} - - -/* Add the token TOKEN to dfa->nodes, and return the index of the token. - Or return -1, if an error will be occured. */ - -static int -internal_function -re_dfa_add_node (re_dfa_t *dfa, re_token_t token) -{ - if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0)) - { - size_t new_nodes_alloc = dfa->nodes_alloc * 2; - int *new_nexts, *new_indices; - re_node_set *new_edests, *new_eclosures; - re_token_t *new_nodes; - - /* Avoid overflows in realloc. */ - const size_t max_object_size = MAX (sizeof (re_token_t), - MAX (sizeof (re_node_set), - sizeof (int))); - if (BE (SIZE_MAX / max_object_size < new_nodes_alloc, 0)) - return -1; - - new_nodes = re_realloc (dfa->nodes, re_token_t, new_nodes_alloc); - if (BE (new_nodes == NULL, 0)) - return -1; - dfa->nodes = new_nodes; - new_nexts = re_realloc (dfa->nexts, int, new_nodes_alloc); - new_indices = re_realloc (dfa->org_indices, int, new_nodes_alloc); - new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc); - new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc); - if (BE (new_nexts == NULL || new_indices == NULL - || new_edests == NULL || new_eclosures == NULL, 0)) - return -1; - dfa->nexts = new_nexts; - dfa->org_indices = new_indices; - dfa->edests = new_edests; - dfa->eclosures = new_eclosures; - dfa->nodes_alloc = new_nodes_alloc; - } - dfa->nodes[dfa->nodes_len] = token; - dfa->nodes[dfa->nodes_len].constraint = 0; -#ifdef RE_ENABLE_I18N - dfa->nodes[dfa->nodes_len].accept_mb = - (token.type == OP_PERIOD && dfa->mb_cur_max > 1) || token.type == COMPLEX_BRACKET; -#endif - dfa->nexts[dfa->nodes_len] = -1; - re_node_set_init_empty (dfa->edests + dfa->nodes_len); - re_node_set_init_empty (dfa->eclosures + dfa->nodes_len); - return dfa->nodes_len++; -} - -static inline unsigned int -internal_function -calc_state_hash (const re_node_set *nodes, unsigned int context) -{ - unsigned int hash = nodes->nelem + context; - int i; - for (i = 0 ; i < nodes->nelem ; i++) - hash += nodes->elems[i]; - return hash; -} - -/* Search for the state whose node_set is equivalent to NODES. - Return the pointer to the state, if we found it in the DFA. - Otherwise create the new one and return it. In case of an error - return NULL and set the error code in ERR. - Note: - We assume NULL as the invalid state, then it is possible that - return value is NULL and ERR is REG_NOERROR. - - We never return non-NULL value in case of any errors, it is for - optimization. */ - -static re_dfastate_t * -internal_function -re_acquire_state (reg_errcode_t *err, const re_dfa_t *dfa, - const re_node_set *nodes) -{ - unsigned int hash; - re_dfastate_t *new_state; - struct re_state_table_entry *spot; - int i; - if (BE (nodes->nelem == 0, 0)) - { - *err = REG_NOERROR; - return NULL; - } - hash = calc_state_hash (nodes, 0); - spot = dfa->state_table + (hash & dfa->state_hash_mask); - - for (i = 0 ; i < spot->num ; i++) - { - re_dfastate_t *state = spot->array[i]; - if (hash != state->hash) - continue; - if (re_node_set_compare (&state->nodes, nodes)) - return state; - } - - /* There are no appropriate state in the dfa, create the new one. */ - new_state = create_ci_newstate (dfa, nodes, hash); - if (BE (new_state == NULL, 0)) - *err = REG_ESPACE; - - return new_state; -} - -/* Search for the state whose node_set is equivalent to NODES and - whose context is equivalent to CONTEXT. - Return the pointer to the state, if we found it in the DFA. - Otherwise create the new one and return it. In case of an error - return NULL and set the error code in ERR. - Note: - We assume NULL as the invalid state, then it is possible that - return value is NULL and ERR is REG_NOERROR. - - We never return non-NULL value in case of any errors, it is for - optimization. */ - -static re_dfastate_t * -internal_function -re_acquire_state_context (reg_errcode_t *err, const re_dfa_t *dfa, - const re_node_set *nodes, unsigned int context) -{ - unsigned int hash; - re_dfastate_t *new_state; - struct re_state_table_entry *spot; - int i; - if (nodes->nelem == 0) - { - *err = REG_NOERROR; - return NULL; - } - hash = calc_state_hash (nodes, context); - spot = dfa->state_table + (hash & dfa->state_hash_mask); - - for (i = 0 ; i < spot->num ; i++) - { - re_dfastate_t *state = spot->array[i]; - if (state->hash == hash - && state->context == context - && re_node_set_compare (state->entrance_nodes, nodes)) - return state; - } - /* There are no appropriate state in `dfa', create the new one. */ - new_state = create_cd_newstate (dfa, nodes, context, hash); - if (BE (new_state == NULL, 0)) - *err = REG_ESPACE; - - return new_state; -} - -/* Finish initialization of the new state NEWSTATE, and using its hash value - HASH put in the appropriate bucket of DFA's state table. Return value - indicates the error code if failed. */ - -static reg_errcode_t -register_state (const re_dfa_t *dfa, re_dfastate_t *newstate, - unsigned int hash) -{ - struct re_state_table_entry *spot; - reg_errcode_t err; - int i; - - newstate->hash = hash; - err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem); - if (BE (err != REG_NOERROR, 0)) - return REG_ESPACE; - for (i = 0; i < newstate->nodes.nelem; i++) - { - int elem = newstate->nodes.elems[i]; - if (!IS_EPSILON_NODE (dfa->nodes[elem].type)) - if (re_node_set_insert_last (&newstate->non_eps_nodes, elem) < 0) - return REG_ESPACE; - } - - spot = dfa->state_table + (hash & dfa->state_hash_mask); - if (BE (spot->alloc <= spot->num, 0)) - { - int new_alloc = 2 * spot->num + 2; - re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *, - new_alloc); - if (BE (new_array == NULL, 0)) - return REG_ESPACE; - spot->array = new_array; - spot->alloc = new_alloc; - } - spot->array[spot->num++] = newstate; - return REG_NOERROR; -} - -static void -free_state (re_dfastate_t *state) -{ - re_node_set_free (&state->non_eps_nodes); - re_node_set_free (&state->inveclosure); - if (state->entrance_nodes != &state->nodes) - { - re_node_set_free (state->entrance_nodes); - re_free (state->entrance_nodes); - } - re_node_set_free (&state->nodes); - re_free (state->word_trtable); - re_free (state->trtable); - re_free (state); -} - -/* Create the new state which is independ of contexts. - Return the new state if succeeded, otherwise return NULL. */ - -static re_dfastate_t * -internal_function -create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes, - unsigned int hash) -{ - int i; - reg_errcode_t err; - re_dfastate_t *newstate; - - newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1); - if (BE (newstate == NULL, 0)) - return NULL; - err = re_node_set_init_copy (&newstate->nodes, nodes); - if (BE (err != REG_NOERROR, 0)) - { - re_free (newstate); - return NULL; - } - - newstate->entrance_nodes = &newstate->nodes; - for (i = 0 ; i < nodes->nelem ; i++) - { - re_token_t *node = dfa->nodes + nodes->elems[i]; - re_token_type_t type = node->type; - if (type == CHARACTER && !node->constraint) - continue; -#ifdef RE_ENABLE_I18N - newstate->accept_mb |= node->accept_mb; -#endif /* RE_ENABLE_I18N */ - - /* If the state has the halt node, the state is a halt state. */ - if (type == END_OF_RE) - newstate->halt = 1; - else if (type == OP_BACK_REF) - newstate->has_backref = 1; - else if (type == ANCHOR || node->constraint) - newstate->has_constraint = 1; - } - err = register_state (dfa, newstate, hash); - if (BE (err != REG_NOERROR, 0)) - { - free_state (newstate); - newstate = NULL; - } - return newstate; -} - -/* Create the new state which is depend on the context CONTEXT. - Return the new state if succeeded, otherwise return NULL. */ - -static re_dfastate_t * -internal_function -create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes, - unsigned int context, unsigned int hash) -{ - int i, nctx_nodes = 0; - reg_errcode_t err; - re_dfastate_t *newstate; - - newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1); - if (BE (newstate == NULL, 0)) - return NULL; - err = re_node_set_init_copy (&newstate->nodes, nodes); - if (BE (err != REG_NOERROR, 0)) - { - re_free (newstate); - return NULL; - } - - newstate->context = context; - newstate->entrance_nodes = &newstate->nodes; - - for (i = 0 ; i < nodes->nelem ; i++) - { - re_token_t *node = dfa->nodes + nodes->elems[i]; - re_token_type_t type = node->type; - unsigned int constraint = node->constraint; - - if (type == CHARACTER && !constraint) - continue; -#ifdef RE_ENABLE_I18N - newstate->accept_mb |= node->accept_mb; -#endif /* RE_ENABLE_I18N */ - - /* If the state has the halt node, the state is a halt state. */ - if (type == END_OF_RE) - newstate->halt = 1; - else if (type == OP_BACK_REF) - newstate->has_backref = 1; - - if (constraint) - { - if (newstate->entrance_nodes == &newstate->nodes) - { - newstate->entrance_nodes = re_malloc (re_node_set, 1); - if (BE (newstate->entrance_nodes == NULL, 0)) - { - free_state (newstate); - return NULL; - } - if (re_node_set_init_copy (newstate->entrance_nodes, nodes) - != REG_NOERROR) - return NULL; - nctx_nodes = 0; - newstate->has_constraint = 1; - } - - if (NOT_SATISFY_PREV_CONSTRAINT (constraint,context)) - { - re_node_set_remove_at (&newstate->nodes, i - nctx_nodes); - ++nctx_nodes; - } - } - } - err = register_state (dfa, newstate, hash); - if (BE (err != REG_NOERROR, 0)) - { - free_state (newstate); - newstate = NULL; - } - return newstate; -} diff --git a/deps/regex/regex_internal.h b/deps/regex/regex_internal.h deleted file mode 100644 index 53ccebecd8e..00000000000 --- a/deps/regex/regex_internal.h +++ /dev/null @@ -1,819 +0,0 @@ -/* Extended regular expression matching and search library. - Copyright (C) 2002-2005, 2007, 2008, 2010 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Isamu Hasegawa . - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -#ifndef _REGEX_INTERNAL_H -#define _REGEX_INTERNAL_H 1 - -#include -#include -#include -#include -#include - -#ifndef UNUSED -# ifdef __GNUC__ -# define UNUSED __attribute__((unused)) -# else -# define UNUSED -# endif -#endif - -#if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC -# include -#endif -#if defined HAVE_LOCALE_H || defined _LIBC -# include -#endif -#if defined HAVE_WCHAR_H || defined _LIBC -# include -#endif /* HAVE_WCHAR_H || _LIBC */ -#if defined HAVE_WCTYPE_H || defined _LIBC -# include -#endif /* HAVE_WCTYPE_H || _LIBC */ -#if defined HAVE_STDBOOL_H || defined _LIBC -# include -#endif /* HAVE_STDBOOL_H || _LIBC */ -#if !defined(ZOS_USS) -#if defined HAVE_STDINT_H || defined _LIBC -# include -#endif /* HAVE_STDINT_H || _LIBC */ -#endif /* !ZOS_USS */ -#if defined _LIBC -# include -#else -# define __libc_lock_define(CLASS,NAME) -# define __libc_lock_init(NAME) do { } while (0) -# define __libc_lock_lock(NAME) do { } while (0) -# define __libc_lock_unlock(NAME) do { } while (0) -#endif - -#ifndef GAWK -/* In case that the system doesn't have isblank(). */ -#if !defined _LIBC && !defined HAVE_ISBLANK && !defined isblank -# define isblank(ch) ((ch) == ' ' || (ch) == '\t') -#endif -#else /* GAWK */ -/* - * This is a mess. On glibc systems you have to define - * a magic constant to get isblank() out of , since it's - * a C99 function. To heck with all that and borrow a page from - * dfa.c's book. - */ - -static int -is_blank (int c) -{ - return (c == ' ' || c == '\t'); -} -#endif /* GAWK */ - -#ifdef _LIBC -# ifndef _RE_DEFINE_LOCALE_FUNCTIONS -# define _RE_DEFINE_LOCALE_FUNCTIONS 1 -# include -# include -# include -# endif -#endif - -/* This is for other GNU distributions with internationalized messages. */ -#if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC -# include -# ifdef _LIBC -# undef gettext -# define gettext(msgid) \ - INTUSE(__dcgettext) (_libc_intl_domainname, msgid, LC_MESSAGES) -# endif -#else -# define gettext(msgid) (msgid) -#endif - -#ifndef gettext_noop -/* This define is so xgettext can find the internationalizable - strings. */ -# define gettext_noop(String) String -#endif - -/* For loser systems without the definition. */ -#ifndef SIZE_MAX -# define SIZE_MAX ((size_t) -1) -#endif - -#ifndef NO_MBSUPPORT -#include "mbsupport.h" /* gawk */ -#endif -#ifndef MB_CUR_MAX -#define MB_CUR_MAX 1 -#endif - -#if (defined MBS_SUPPORT) || _LIBC -# define RE_ENABLE_I18N -#endif - -#if __GNUC__ >= 3 -# define BE(expr, val) __builtin_expect (expr, val) -#else -# define BE(expr, val) (expr) -# ifdef inline -# undef inline -# endif -# define inline -#endif - -/* Number of single byte character. */ -#define SBC_MAX 256 - -#define COLL_ELEM_LEN_MAX 8 - -/* The character which represents newline. */ -#define NEWLINE_CHAR '\n' -#define WIDE_NEWLINE_CHAR L'\n' - -/* Rename to standard API for using out of glibc. */ -#ifndef _LIBC -# ifdef __wctype -# undef __wctype -# endif -# define __wctype wctype -# ifdef __iswctype -# undef __iswctype -# endif -# define __iswctype iswctype -# define __btowc btowc -# define __mbrtowc mbrtowc -#undef __mempcpy /* GAWK */ -# define __mempcpy mempcpy -# define __wcrtomb wcrtomb -# define __regfree regfree -# define attribute_hidden -#endif /* not _LIBC */ - -#ifdef __GNUC__ -# define __attribute(arg) __attribute__ (arg) -#else -# define __attribute(arg) -#endif - -extern const char __re_error_msgid[] attribute_hidden; -extern const size_t __re_error_msgid_idx[] attribute_hidden; - -/* An integer used to represent a set of bits. It must be unsigned, - and must be at least as wide as unsigned int. */ -typedef unsigned long int bitset_word_t; -/* All bits set in a bitset_word_t. */ -#define BITSET_WORD_MAX ULONG_MAX -/* Number of bits in a bitset_word_t. Cast to int as most code use it - * like that for counting */ -#define BITSET_WORD_BITS ((int)(sizeof (bitset_word_t) * CHAR_BIT)) -/* Number of bitset_word_t in a bit_set. */ -#define BITSET_WORDS (SBC_MAX / BITSET_WORD_BITS) -typedef bitset_word_t bitset_t[BITSET_WORDS]; -typedef bitset_word_t *re_bitset_ptr_t; -typedef const bitset_word_t *re_const_bitset_ptr_t; - -#define bitset_set(set,i) \ - (set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS) -#define bitset_clear(set,i) \ - (set[i / BITSET_WORD_BITS] &= ~((bitset_word_t) 1 << i % BITSET_WORD_BITS)) -#define bitset_contain(set,i) \ - (set[i / BITSET_WORD_BITS] & ((bitset_word_t) 1 << i % BITSET_WORD_BITS)) -#define bitset_empty(set) memset (set, '\0', sizeof (bitset_t)) -#define bitset_set_all(set) memset (set, '\xff', sizeof (bitset_t)) -#define bitset_copy(dest,src) memcpy (dest, src, sizeof (bitset_t)) - -#define PREV_WORD_CONSTRAINT 0x0001 -#define PREV_NOTWORD_CONSTRAINT 0x0002 -#define NEXT_WORD_CONSTRAINT 0x0004 -#define NEXT_NOTWORD_CONSTRAINT 0x0008 -#define PREV_NEWLINE_CONSTRAINT 0x0010 -#define NEXT_NEWLINE_CONSTRAINT 0x0020 -#define PREV_BEGBUF_CONSTRAINT 0x0040 -#define NEXT_ENDBUF_CONSTRAINT 0x0080 -#define WORD_DELIM_CONSTRAINT 0x0100 -#define NOT_WORD_DELIM_CONSTRAINT 0x0200 - -typedef enum -{ - INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT, - WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT, - WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT, - INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT, - LINE_FIRST = PREV_NEWLINE_CONSTRAINT, - LINE_LAST = NEXT_NEWLINE_CONSTRAINT, - BUF_FIRST = PREV_BEGBUF_CONSTRAINT, - BUF_LAST = NEXT_ENDBUF_CONSTRAINT, - WORD_DELIM = WORD_DELIM_CONSTRAINT, - NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT -} re_context_type; - -typedef struct -{ - int alloc; - int nelem; - int *elems; -} re_node_set; - -typedef enum -{ - NON_TYPE = 0, - - /* Node type, These are used by token, node, tree. */ - CHARACTER = 1, - END_OF_RE = 2, - SIMPLE_BRACKET = 3, - OP_BACK_REF = 4, - OP_PERIOD = 5, -#ifdef RE_ENABLE_I18N - COMPLEX_BRACKET = 6, - OP_UTF8_PERIOD = 7, -#endif /* RE_ENABLE_I18N */ - - /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used - when the debugger shows values of this enum type. */ -#define EPSILON_BIT 8 - OP_OPEN_SUBEXP = EPSILON_BIT | 0, - OP_CLOSE_SUBEXP = EPSILON_BIT | 1, - OP_ALT = EPSILON_BIT | 2, - OP_DUP_ASTERISK = EPSILON_BIT | 3, - ANCHOR = EPSILON_BIT | 4, - - /* Tree type, these are used only by tree. */ - CONCAT = 16, - SUBEXP = 17, - - /* Token type, these are used only by token. */ - OP_DUP_PLUS = 18, - OP_DUP_QUESTION, - OP_OPEN_BRACKET, - OP_CLOSE_BRACKET, - OP_CHARSET_RANGE, - OP_OPEN_DUP_NUM, - OP_CLOSE_DUP_NUM, - OP_NON_MATCH_LIST, - OP_OPEN_COLL_ELEM, - OP_CLOSE_COLL_ELEM, - OP_OPEN_EQUIV_CLASS, - OP_CLOSE_EQUIV_CLASS, - OP_OPEN_CHAR_CLASS, - OP_CLOSE_CHAR_CLASS, - OP_WORD, - OP_NOTWORD, - OP_SPACE, - OP_NOTSPACE, - BACK_SLASH - -} re_token_type_t; - -#ifdef RE_ENABLE_I18N -typedef struct -{ - /* Multibyte characters. */ - wchar_t *mbchars; - - /* Collating symbols. */ -# ifdef _LIBC - int32_t *coll_syms; -# endif - - /* Equivalence classes. */ -# ifdef _LIBC - int32_t *equiv_classes; -# endif - - /* Range expressions. */ -# ifdef _LIBC - uint32_t *range_starts; - uint32_t *range_ends; -# else /* not _LIBC */ - wchar_t *range_starts; - wchar_t *range_ends; -# endif /* not _LIBC */ - - /* Character classes. */ - wctype_t *char_classes; - - /* If this character set is the non-matching list. */ - unsigned int non_match : 1; - - /* # of multibyte characters. */ - int nmbchars; - - /* # of collating symbols. */ - int ncoll_syms; - - /* # of equivalence classes. */ - int nequiv_classes; - - /* # of range expressions. */ - int nranges; - - /* # of character classes. */ - int nchar_classes; -} re_charset_t; -#endif /* RE_ENABLE_I18N */ - -typedef struct -{ - union - { - unsigned char c; /* for CHARACTER */ - re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */ -#ifdef RE_ENABLE_I18N - re_charset_t *mbcset; /* for COMPLEX_BRACKET */ -#endif /* RE_ENABLE_I18N */ - int idx; /* for BACK_REF */ - re_context_type ctx_type; /* for ANCHOR */ - } opr; -#if __GNUC__ >= 2 - re_token_type_t type : 8; -#else - re_token_type_t type; -#endif - unsigned int constraint : 10; /* context constraint */ - unsigned int duplicated : 1; - unsigned int opt_subexp : 1; -#ifdef RE_ENABLE_I18N - unsigned int accept_mb : 1; - /* These 2 bits can be moved into the union if needed (e.g. if running out - of bits; move opr.c to opr.c.c and move the flags to opr.c.flags). */ - unsigned int mb_partial : 1; -#endif - unsigned int word_char : 1; -} re_token_t; - -#define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT) - -struct re_string_t -{ - /* Indicate the raw buffer which is the original string passed as an - argument of regexec(), re_search(), etc.. */ - const unsigned char *raw_mbs; - /* Store the multibyte string. In case of "case insensitive mode" like - REG_ICASE, upper cases of the string are stored, otherwise MBS points - the same address that RAW_MBS points. */ - unsigned char *mbs; -#ifdef RE_ENABLE_I18N - /* Store the wide character string which is corresponding to MBS. */ - wint_t *wcs; - int *offsets; - mbstate_t cur_state; -#endif - /* Index in RAW_MBS. Each character mbs[i] corresponds to - raw_mbs[raw_mbs_idx + i]. */ - int raw_mbs_idx; - /* The length of the valid characters in the buffers. */ - int valid_len; - /* The corresponding number of bytes in raw_mbs array. */ - int valid_raw_len; - /* The length of the buffers MBS and WCS. */ - int bufs_len; - /* The index in MBS, which is updated by re_string_fetch_byte. */ - int cur_idx; - /* length of RAW_MBS array. */ - int raw_len; - /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */ - int len; - /* End of the buffer may be shorter than its length in the cases such - as re_match_2, re_search_2. Then, we use STOP for end of the buffer - instead of LEN. */ - int raw_stop; - /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */ - int stop; - - /* The context of mbs[0]. We store the context independently, since - the context of mbs[0] may be different from raw_mbs[0], which is - the beginning of the input string. */ - unsigned int tip_context; - /* The translation passed as a part of an argument of re_compile_pattern. */ - RE_TRANSLATE_TYPE trans; - /* Copy of re_dfa_t's word_char. */ - re_const_bitset_ptr_t word_char; - /* 1 if REG_ICASE. */ - unsigned char icase; - unsigned char is_utf8; - unsigned char map_notascii; - unsigned char mbs_allocated; - unsigned char offsets_needed; - unsigned char newline_anchor; - unsigned char word_ops_used; - int mb_cur_max; -}; -typedef struct re_string_t re_string_t; - - -struct re_dfa_t; -typedef struct re_dfa_t re_dfa_t; - -#ifndef _LIBC -# ifdef __i386__ -# define internal_function __attribute ((regparm (3), stdcall)) -# else -# define internal_function -# endif -#endif - -#ifndef NOT_IN_libc -static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr, - int new_buf_len) - internal_function; -# ifdef RE_ENABLE_I18N -static void build_wcs_buffer (re_string_t *pstr) internal_function; -static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr) - internal_function; -# endif /* RE_ENABLE_I18N */ -static void build_upper_buffer (re_string_t *pstr) internal_function; -static void re_string_translate_buffer (re_string_t *pstr) internal_function; -static unsigned int re_string_context_at (const re_string_t *input, int idx, - int eflags) - internal_function __attribute ((pure)); -#endif -#define re_string_peek_byte(pstr, offset) \ - ((pstr)->mbs[(pstr)->cur_idx + offset]) -#define re_string_fetch_byte(pstr) \ - ((pstr)->mbs[(pstr)->cur_idx++]) -#define re_string_first_byte(pstr, idx) \ - ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF) -#define re_string_is_single_byte_char(pstr, idx) \ - ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \ - || (pstr)->wcs[(idx) + 1] != WEOF)) -#define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx) -#define re_string_cur_idx(pstr) ((pstr)->cur_idx) -#define re_string_get_buffer(pstr) ((pstr)->mbs) -#define re_string_length(pstr) ((pstr)->len) -#define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx]) -#define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx)) -#define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx)) - -#ifndef _LIBC -# if HAVE_ALLOCA -# if (_MSC_VER) -# include -# define __libc_use_alloca(n) 0 -# else -# include -/* The OS usually guarantees only one guard page at the bottom of the stack, - and a page size can be as small as 4096 bytes. So we cannot safely - allocate anything larger than 4096 bytes. Also care for the possibility - of a few compiler-allocated temporary stack slots. */ -# define __libc_use_alloca(n) ((n) < 4032) -# endif -# else -/* alloca is implemented with malloc, so just use malloc. */ -# define __libc_use_alloca(n) 0 -# endif -#endif - -#define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t))) -/* SunOS 4.1.x realloc doesn't accept null pointers: pre-Standard C. Sigh. */ -#define re_realloc(p,t,n) ((p != NULL) ? (t *) realloc (p,(n)*sizeof(t)) : (t *) calloc(n,sizeof(t))) -#define re_free(p) free (p) - -struct bin_tree_t -{ - struct bin_tree_t *parent; - struct bin_tree_t *left; - struct bin_tree_t *right; - struct bin_tree_t *first; - struct bin_tree_t *next; - - re_token_t token; - - /* `node_idx' is the index in dfa->nodes, if `type' == 0. - Otherwise `type' indicate the type of this node. */ - int node_idx; -}; -typedef struct bin_tree_t bin_tree_t; - -#define BIN_TREE_STORAGE_SIZE \ - ((1024 - sizeof (void *)) / sizeof (bin_tree_t)) - -struct bin_tree_storage_t -{ - struct bin_tree_storage_t *next; - bin_tree_t data[BIN_TREE_STORAGE_SIZE]; -}; -typedef struct bin_tree_storage_t bin_tree_storage_t; - -#define CONTEXT_WORD 1 -#define CONTEXT_NEWLINE (CONTEXT_WORD << 1) -#define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1) -#define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1) - -#define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD) -#define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE) -#define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF) -#define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF) -#define IS_ORDINARY_CONTEXT(c) ((c) == 0) - -#define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_') -#define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR) -#define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_') -#define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR) - -#define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \ - ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \ - || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \ - || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\ - || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context))) - -#define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \ - ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \ - || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \ - || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \ - || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context))) - -struct re_dfastate_t -{ - unsigned int hash; - re_node_set nodes; - re_node_set non_eps_nodes; - re_node_set inveclosure; - re_node_set *entrance_nodes; - struct re_dfastate_t **trtable, **word_trtable; - unsigned int context : 4; - unsigned int halt : 1; - /* If this state can accept `multi byte'. - Note that we refer to multibyte characters, and multi character - collating elements as `multi byte'. */ - unsigned int accept_mb : 1; - /* If this state has backreference node(s). */ - unsigned int has_backref : 1; - unsigned int has_constraint : 1; -}; -typedef struct re_dfastate_t re_dfastate_t; - -struct re_state_table_entry -{ - int num; - int alloc; - re_dfastate_t **array; -}; - -/* Array type used in re_sub_match_last_t and re_sub_match_top_t. */ - -typedef struct -{ - int next_idx; - int alloc; - re_dfastate_t **array; -} state_array_t; - -/* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */ - -typedef struct -{ - int node; - int str_idx; /* The position NODE match at. */ - state_array_t path; -} re_sub_match_last_t; - -/* Store information about the node NODE whose type is OP_OPEN_SUBEXP. - And information about the node, whose type is OP_CLOSE_SUBEXP, - corresponding to NODE is stored in LASTS. */ - -typedef struct -{ - int str_idx; - int node; - state_array_t *path; - int alasts; /* Allocation size of LASTS. */ - int nlasts; /* The number of LASTS. */ - re_sub_match_last_t **lasts; -} re_sub_match_top_t; - -struct re_backref_cache_entry -{ - int node; - int str_idx; - int subexp_from; - int subexp_to; - char more; - char unused; - unsigned short int eps_reachable_subexps_map; -}; - -typedef struct -{ - /* The string object corresponding to the input string. */ - re_string_t input; -#if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L) - const re_dfa_t *const dfa; -#else - const re_dfa_t *dfa; -#endif - /* EFLAGS of the argument of regexec. */ - int eflags; - /* Where the matching ends. */ - int match_last; - int last_node; - /* The state log used by the matcher. */ - re_dfastate_t **state_log; - int state_log_top; - /* Back reference cache. */ - int nbkref_ents; - int abkref_ents; - struct re_backref_cache_entry *bkref_ents; - int max_mb_elem_len; - int nsub_tops; - int asub_tops; - re_sub_match_top_t **sub_tops; -} re_match_context_t; - -typedef struct -{ - re_dfastate_t **sifted_states; - re_dfastate_t **limited_states; - int last_node; - int last_str_idx; - re_node_set limits; -} re_sift_context_t; - -struct re_fail_stack_ent_t -{ - int idx; - int node; - regmatch_t *regs; - re_node_set eps_via_nodes; -}; - -struct re_fail_stack_t -{ - int num; - int alloc; - struct re_fail_stack_ent_t *stack; -}; - -struct re_dfa_t -{ - re_token_t *nodes; - size_t nodes_alloc; - size_t nodes_len; - int *nexts; - int *org_indices; - re_node_set *edests; - re_node_set *eclosures; - re_node_set *inveclosures; - struct re_state_table_entry *state_table; - re_dfastate_t *init_state; - re_dfastate_t *init_state_word; - re_dfastate_t *init_state_nl; - re_dfastate_t *init_state_begbuf; - bin_tree_t *str_tree; - bin_tree_storage_t *str_tree_storage; - re_bitset_ptr_t sb_char; - int str_tree_storage_idx; - - /* number of subexpressions `re_nsub' is in regex_t. */ - unsigned int state_hash_mask; - int init_node; - int nbackref; /* The number of backreference in this dfa. */ - - /* Bitmap expressing which backreference is used. */ - bitset_word_t used_bkref_map; - bitset_word_t completed_bkref_map; - - unsigned int has_plural_match : 1; - /* If this dfa has "multibyte node", which is a backreference or - a node which can accept multibyte character or multi character - collating element. */ - unsigned int has_mb_node : 1; - unsigned int is_utf8 : 1; - unsigned int map_notascii : 1; - unsigned int word_ops_used : 1; - int mb_cur_max; - bitset_t word_char; - reg_syntax_t syntax; - int *subexp_map; -#ifdef DEBUG - char* re_str; -#endif -#if defined _LIBC - __libc_lock_define (, lock) -#endif -}; - -#define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set)) -#define re_node_set_remove(set,id) \ - (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1)) -#define re_node_set_empty(p) ((p)->nelem = 0) -#define re_node_set_free(set) re_free ((set)->elems) - - -typedef enum -{ - SB_CHAR, - MB_CHAR, - EQUIV_CLASS, - COLL_SYM, - CHAR_CLASS -} bracket_elem_type; - -typedef struct -{ - bracket_elem_type type; - union - { - unsigned char ch; - unsigned char *name; - wchar_t wch; - } opr; -} bracket_elem_t; - - -/* Inline functions for bitset operation. */ -static inline void -bitset_not (bitset_t set) -{ - int bitset_i; - for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i) - set[bitset_i] = ~set[bitset_i]; -} - -static inline void -bitset_merge (bitset_t dest, const bitset_t src) -{ - int bitset_i; - for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i) - dest[bitset_i] |= src[bitset_i]; -} - -static inline void -bitset_mask (bitset_t dest, const bitset_t src) -{ - int bitset_i; - for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i) - dest[bitset_i] &= src[bitset_i]; -} - -#ifdef RE_ENABLE_I18N -/* Inline functions for re_string. */ -static inline int -internal_function __attribute ((pure)) -re_string_char_size_at (const re_string_t *pstr, int idx) -{ - int byte_idx; - if (pstr->mb_cur_max == 1) - return 1; - for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx) - if (pstr->wcs[idx + byte_idx] != WEOF) - break; - return byte_idx; -} - -static inline wint_t -internal_function __attribute ((pure)) -re_string_wchar_at (const re_string_t *pstr, int idx) -{ - if (pstr->mb_cur_max == 1) - return (wint_t) pstr->mbs[idx]; - return (wint_t) pstr->wcs[idx]; -} - -# ifndef NOT_IN_libc -static int -internal_function __attribute ((pure)) -re_string_elem_size_at (const re_string_t *pstr, int idx) -{ -# ifdef _LIBC - const unsigned char *p, *extra; - const int32_t *table, *indirect; - int32_t tmp; -# include - uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES); - - if (nrules != 0) - { - table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB); - extra = (const unsigned char *) - _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB); - indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE, - _NL_COLLATE_INDIRECTMB); - p = pstr->mbs + idx; - tmp = findidx (&p); - return p - pstr->mbs - idx; - } - else -# endif /* _LIBC */ - return 1; -} -# endif -#endif /* RE_ENABLE_I18N */ - -#endif /* _REGEX_INTERNAL_H */ diff --git a/deps/regex/regexec.c b/deps/regex/regexec.c deleted file mode 100644 index 0a1602e5aa8..00000000000 --- a/deps/regex/regexec.c +++ /dev/null @@ -1,4369 +0,0 @@ -/* Extended regular expression matching and search library. - Copyright (C) 2002-2005, 2007, 2009, 2010 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Isamu Hasegawa . - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA. */ - -static reg_errcode_t match_ctx_init (re_match_context_t *cache, int eflags, - int n) internal_function; -static void match_ctx_clean (re_match_context_t *mctx) internal_function; -static void match_ctx_free (re_match_context_t *cache) internal_function; -static reg_errcode_t match_ctx_add_entry (re_match_context_t *cache, int node, - int str_idx, int from, int to) - internal_function; -static int search_cur_bkref_entry (const re_match_context_t *mctx, int str_idx) - internal_function; -static reg_errcode_t match_ctx_add_subtop (re_match_context_t *mctx, int node, - int str_idx) internal_function; -static re_sub_match_last_t * match_ctx_add_sublast (re_sub_match_top_t *subtop, - int node, int str_idx) - internal_function; -static void sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts, - re_dfastate_t **limited_sts, int last_node, - int last_str_idx) - internal_function; -static reg_errcode_t re_search_internal (const regex_t *preg, - const char *string, int length, - int start, int range, int stop, - size_t nmatch, regmatch_t pmatch[], - int eflags); -static int re_search_2_stub (struct re_pattern_buffer *bufp, - const char *string1, int length1, - const char *string2, int length2, - int start, int range, struct re_registers *regs, - int stop, int ret_len); -static int re_search_stub (struct re_pattern_buffer *bufp, - const char *string, int length, int start, - int range, int stop, struct re_registers *regs, - int ret_len); -static unsigned re_copy_regs (struct re_registers *regs, regmatch_t *pmatch, - unsigned int nregs, int regs_allocated); -static reg_errcode_t prune_impossible_nodes (re_match_context_t *mctx); -static int check_matching (re_match_context_t *mctx, int fl_longest_match, - int *p_match_first) internal_function; -static int check_halt_state_context (const re_match_context_t *mctx, - const re_dfastate_t *state, int idx) - internal_function; -static void update_regs (const re_dfa_t *dfa, regmatch_t *pmatch, - regmatch_t *prev_idx_match, int cur_node, - int cur_idx, int nmatch) internal_function; -static reg_errcode_t push_fail_stack (struct re_fail_stack_t *fs, - int str_idx, int dest_node, int nregs, - regmatch_t *regs, - re_node_set *eps_via_nodes) - internal_function; -static reg_errcode_t set_regs (const regex_t *preg, - const re_match_context_t *mctx, - size_t nmatch, regmatch_t *pmatch, - int fl_backtrack) internal_function; -static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs) - internal_function; - -#ifdef RE_ENABLE_I18N -static int sift_states_iter_mb (const re_match_context_t *mctx, - re_sift_context_t *sctx, - int node_idx, int str_idx, int max_str_idx) - internal_function; -#endif /* RE_ENABLE_I18N */ -static reg_errcode_t sift_states_backward (const re_match_context_t *mctx, - re_sift_context_t *sctx) - internal_function; -static reg_errcode_t build_sifted_states (const re_match_context_t *mctx, - re_sift_context_t *sctx, int str_idx, - re_node_set *cur_dest) - internal_function; -static reg_errcode_t update_cur_sifted_state (const re_match_context_t *mctx, - re_sift_context_t *sctx, - int str_idx, - re_node_set *dest_nodes) - internal_function; -static reg_errcode_t add_epsilon_src_nodes (const re_dfa_t *dfa, - re_node_set *dest_nodes, - const re_node_set *candidates) - internal_function; -static int check_dst_limits (const re_match_context_t *mctx, - re_node_set *limits, - int dst_node, int dst_idx, int src_node, - int src_idx) internal_function; -static int check_dst_limits_calc_pos_1 (const re_match_context_t *mctx, - int boundaries, int subexp_idx, - int from_node, int bkref_idx) - internal_function; -static int check_dst_limits_calc_pos (const re_match_context_t *mctx, - int limit, int subexp_idx, - int node, int str_idx, - int bkref_idx) internal_function; -static reg_errcode_t check_subexp_limits (const re_dfa_t *dfa, - re_node_set *dest_nodes, - const re_node_set *candidates, - re_node_set *limits, - struct re_backref_cache_entry *bkref_ents, - int str_idx) internal_function; -static reg_errcode_t sift_states_bkref (const re_match_context_t *mctx, - re_sift_context_t *sctx, - int str_idx, const re_node_set *candidates) - internal_function; -static reg_errcode_t merge_state_array (const re_dfa_t *dfa, - re_dfastate_t **dst, - re_dfastate_t **src, int num) - internal_function; -static re_dfastate_t *find_recover_state (reg_errcode_t *err, - re_match_context_t *mctx) internal_function; -static re_dfastate_t *transit_state (reg_errcode_t *err, - re_match_context_t *mctx, - re_dfastate_t *state) internal_function; -static re_dfastate_t *merge_state_with_log (reg_errcode_t *err, - re_match_context_t *mctx, - re_dfastate_t *next_state) - internal_function; -static reg_errcode_t check_subexp_matching_top (re_match_context_t *mctx, - re_node_set *cur_nodes, - int str_idx) internal_function; -#if 0 -static re_dfastate_t *transit_state_sb (reg_errcode_t *err, - re_match_context_t *mctx, - re_dfastate_t *pstate) - internal_function; -#endif -#ifdef RE_ENABLE_I18N -static reg_errcode_t transit_state_mb (re_match_context_t *mctx, - re_dfastate_t *pstate) - internal_function; -#endif /* RE_ENABLE_I18N */ -static reg_errcode_t transit_state_bkref (re_match_context_t *mctx, - const re_node_set *nodes) - internal_function; -static reg_errcode_t get_subexp (re_match_context_t *mctx, - int bkref_node, int bkref_str_idx) - internal_function; -static reg_errcode_t get_subexp_sub (re_match_context_t *mctx, - const re_sub_match_top_t *sub_top, - re_sub_match_last_t *sub_last, - int bkref_node, int bkref_str) - internal_function; -static int find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes, - int subexp_idx, int type) internal_function; -static reg_errcode_t check_arrival (re_match_context_t *mctx, - state_array_t *path, int top_node, - int top_str, int last_node, int last_str, - int type) internal_function; -static reg_errcode_t check_arrival_add_next_nodes (re_match_context_t *mctx, - int str_idx, - re_node_set *cur_nodes, - re_node_set *next_nodes) - internal_function; -static reg_errcode_t check_arrival_expand_ecl (const re_dfa_t *dfa, - re_node_set *cur_nodes, - int ex_subexp, int type) - internal_function; -static reg_errcode_t check_arrival_expand_ecl_sub (const re_dfa_t *dfa, - re_node_set *dst_nodes, - int target, int ex_subexp, - int type) internal_function; -static reg_errcode_t expand_bkref_cache (re_match_context_t *mctx, - re_node_set *cur_nodes, int cur_str, - int subexp_num, int type) - internal_function; -static int build_trtable (const re_dfa_t *dfa, - re_dfastate_t *state) internal_function; -#ifdef RE_ENABLE_I18N -static int check_node_accept_bytes (const re_dfa_t *dfa, int node_idx, - const re_string_t *input, int idx) - internal_function; -# ifdef _LIBC -static unsigned int find_collation_sequence_value (const unsigned char *mbs, - size_t name_len) - internal_function; -# endif /* _LIBC */ -#endif /* RE_ENABLE_I18N */ -static int group_nodes_into_DFAstates (const re_dfa_t *dfa, - const re_dfastate_t *state, - re_node_set *states_node, - bitset_t *states_ch) internal_function; -static int check_node_accept (const re_match_context_t *mctx, - const re_token_t *node, int idx) - internal_function; -static reg_errcode_t extend_buffers (re_match_context_t *mctx) - internal_function; - -/* Entry point for POSIX code. */ - -/* regexec searches for a given pattern, specified by PREG, in the - string STRING. - - If NMATCH is zero or REG_NOSUB was set in the cflags argument to - `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at - least NMATCH elements, and we set them to the offsets of the - corresponding matched substrings. - - EFLAGS specifies `execution flags' which affect matching: if - REG_NOTBOL is set, then ^ does not match at the beginning of the - string; if REG_NOTEOL is set, then $ does not match at the end. - - We return 0 if we find a match and REG_NOMATCH if not. */ - -int -regexec ( - const regex_t *__restrict preg, - const char *__restrict string, - size_t nmatch, - regmatch_t pmatch[], - int eflags) -{ - reg_errcode_t err; - int start, length; - - if (eflags & ~(REG_NOTBOL | REG_NOTEOL | REG_STARTEND)) - return REG_BADPAT; - - if (eflags & REG_STARTEND) - { - start = pmatch[0].rm_so; - length = pmatch[0].rm_eo; - } - else - { - start = 0; - length = strlen (string); - } - - __libc_lock_lock (dfa->lock); - if (preg->no_sub) - err = re_search_internal (preg, string, length, start, length - start, - length, 0, NULL, eflags); - else - err = re_search_internal (preg, string, length, start, length - start, - length, nmatch, pmatch, eflags); - __libc_lock_unlock (dfa->lock); - return err != REG_NOERROR; -} - -#ifdef _LIBC -# include -versioned_symbol (libc, __regexec, regexec, GLIBC_2_3_4); - -# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4) -__typeof__ (__regexec) __compat_regexec; - -int -attribute_compat_text_section -__compat_regexec (const regex_t *__restrict preg, - const char *__restrict string, size_t nmatch, - regmatch_t pmatch[], int eflags) -{ - return regexec (preg, string, nmatch, pmatch, - eflags & (REG_NOTBOL | REG_NOTEOL)); -} -compat_symbol (libc, __compat_regexec, regexec, GLIBC_2_0); -# endif -#endif - -/* Entry points for GNU code. */ - -/* re_match, re_search, re_match_2, re_search_2 - - The former two functions operate on STRING with length LENGTH, - while the later two operate on concatenation of STRING1 and STRING2 - with lengths LENGTH1 and LENGTH2, respectively. - - re_match() matches the compiled pattern in BUFP against the string, - starting at index START. - - re_search() first tries matching at index START, then it tries to match - starting from index START + 1, and so on. The last start position tried - is START + RANGE. (Thus RANGE = 0 forces re_search to operate the same - way as re_match().) - - The parameter STOP of re_{match,search}_2 specifies that no match exceeding - the first STOP characters of the concatenation of the strings should be - concerned. - - If REGS is not NULL, and BUFP->no_sub is not set, the offsets of the match - and all groups is stroed in REGS. (For the "_2" variants, the offsets are - computed relative to the concatenation, not relative to the individual - strings.) - - On success, re_match* functions return the length of the match, re_search* - return the position of the start of the match. Return value -1 means no - match was found and -2 indicates an internal error. */ - -int -re_match (struct re_pattern_buffer *bufp, - const char *string, - int length, - int start, - struct re_registers *regs) -{ - return re_search_stub (bufp, string, length, start, 0, length, regs, 1); -} -#ifdef _LIBC -weak_alias (__re_match, re_match) -#endif - -int -re_search (struct re_pattern_buffer *bufp, - const char *string, - int length, int start, int range, - struct re_registers *regs) -{ - return re_search_stub (bufp, string, length, start, range, length, regs, 0); -} -#ifdef _LIBC -weak_alias (__re_search, re_search) -#endif - -int -re_match_2 (struct re_pattern_buffer *bufp, - const char *string1, int length1, - const char *string2, int length2, int start, - struct re_registers *regs, int stop) -{ - return re_search_2_stub (bufp, string1, length1, string2, length2, - start, 0, regs, stop, 1); -} -#ifdef _LIBC -weak_alias (__re_match_2, re_match_2) -#endif - -int -re_search_2 (struct re_pattern_buffer *bufp, - const char *string1, int length1, - const char *string2, int length2, int start, - int range, struct re_registers *regs, int stop) -{ - return re_search_2_stub (bufp, string1, length1, string2, length2, - start, range, regs, stop, 0); -} -#ifdef _LIBC -weak_alias (__re_search_2, re_search_2) -#endif - -static int -re_search_2_stub (struct re_pattern_buffer *bufp, - const char *string1, int length1, - const char *string2, int length2, int start, - int range, struct re_registers *regs, - int stop, int ret_len) -{ - const char *str; - int rval; - int len = length1 + length2; - int free_str = 0; - - if (BE (length1 < 0 || length2 < 0 || stop < 0, 0)) - return -2; - - /* Concatenate the strings. */ - if (length2 > 0) - if (length1 > 0) - { - char *s = re_malloc (char, len); - - if (BE (s == NULL, 0)) - return -2; - memcpy (s, string1, length1); - memcpy (s + length1, string2, length2); - str = s; - free_str = 1; - } - else - str = string2; - else - str = string1; - - rval = re_search_stub (bufp, str, len, start, range, stop, regs, ret_len); - if (free_str) - re_free ((char *) str); - return rval; -} - -/* The parameters have the same meaning as those of re_search. - Additional parameters: - If RET_LEN is nonzero the length of the match is returned (re_match style); - otherwise the position of the match is returned. */ - -static int -re_search_stub (struct re_pattern_buffer *bufp, - const char *string, int length, int start, - int range, int stop, - struct re_registers *regs, int ret_len) -{ - reg_errcode_t result; - regmatch_t *pmatch; - int nregs, rval; - int eflags = 0; - - /* Check for out-of-range. */ - if (BE (start < 0 || start > length, 0)) - return -1; - if (BE (start + range > length, 0)) - range = length - start; - else if (BE (start + range < 0, 0)) - range = -start; - - __libc_lock_lock (dfa->lock); - - eflags |= (bufp->not_bol) ? REG_NOTBOL : 0; - eflags |= (bufp->not_eol) ? REG_NOTEOL : 0; - - /* Compile fastmap if we haven't yet. */ - if (range > 0 && bufp->fastmap != NULL && !bufp->fastmap_accurate) - re_compile_fastmap (bufp); - - if (BE (bufp->no_sub, 0)) - regs = NULL; - - /* We need at least 1 register. */ - if (regs == NULL) - nregs = 1; - else if (BE (bufp->regs_allocated == REGS_FIXED && - regs->num_regs < bufp->re_nsub + 1, 0)) - { - nregs = regs->num_regs; - if (BE (nregs < 1, 0)) - { - /* Nothing can be copied to regs. */ - regs = NULL; - nregs = 1; - } - } - else - nregs = bufp->re_nsub + 1; - pmatch = re_malloc (regmatch_t, nregs); - if (BE (pmatch == NULL, 0)) - { - rval = -2; - goto out; - } - - result = re_search_internal (bufp, string, length, start, range, stop, - nregs, pmatch, eflags); - - rval = 0; - - /* I hope we needn't fill ther regs with -1's when no match was found. */ - if (result != REG_NOERROR) - rval = -1; - else if (regs != NULL) - { - /* If caller wants register contents data back, copy them. */ - bufp->regs_allocated = re_copy_regs (regs, pmatch, nregs, - bufp->regs_allocated); - if (BE (bufp->regs_allocated == REGS_UNALLOCATED, 0)) - rval = -2; - } - - if (BE (rval == 0, 1)) - { - if (ret_len) - { - assert (pmatch[0].rm_so == start); - rval = pmatch[0].rm_eo - start; - } - else - rval = pmatch[0].rm_so; - } - re_free (pmatch); - out: - __libc_lock_unlock (dfa->lock); - return rval; -} - -static unsigned -re_copy_regs (struct re_registers *regs, - regmatch_t *pmatch, - unsigned int nregs, int regs_allocated) -{ - int rval = REGS_REALLOCATE; - unsigned int i; - unsigned int need_regs = nregs + 1; - /* We need one extra element beyond `num_regs' for the `-1' marker GNU code - uses. */ - - /* Have the register data arrays been allocated? */ - if (regs_allocated == REGS_UNALLOCATED) - { /* No. So allocate them with malloc. */ - regs->start = re_malloc (regoff_t, need_regs); - if (BE (regs->start == NULL, 0)) - return REGS_UNALLOCATED; - regs->end = re_malloc (regoff_t, need_regs); - if (BE (regs->end == NULL, 0)) - { - re_free (regs->start); - return REGS_UNALLOCATED; - } - regs->num_regs = need_regs; - } - else if (regs_allocated == REGS_REALLOCATE) - { /* Yes. If we need more elements than were already - allocated, reallocate them. If we need fewer, just - leave it alone. */ - if (BE (need_regs > regs->num_regs, 0)) - { - regoff_t *new_start = re_realloc (regs->start, regoff_t, need_regs); - regoff_t *new_end; - if (BE (new_start == NULL, 0)) - return REGS_UNALLOCATED; - new_end = re_realloc (regs->end, regoff_t, need_regs); - if (BE (new_end == NULL, 0)) - { - re_free (new_start); - return REGS_UNALLOCATED; - } - regs->start = new_start; - regs->end = new_end; - regs->num_regs = need_regs; - } - } - else - { - assert (regs_allocated == REGS_FIXED); - /* This function may not be called with REGS_FIXED and nregs too big. */ - assert (regs->num_regs >= nregs); - rval = REGS_FIXED; - } - - /* Copy the regs. */ - for (i = 0; i < nregs; ++i) - { - regs->start[i] = pmatch[i].rm_so; - regs->end[i] = pmatch[i].rm_eo; - } - for ( ; i < regs->num_regs; ++i) - regs->start[i] = regs->end[i] = -1; - - return rval; -} - -/* Set REGS to hold NUM_REGS registers, storing them in STARTS and - ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use - this memory for recording register information. STARTS and ENDS - must be allocated using the malloc library routine, and must each - be at least NUM_REGS * sizeof (regoff_t) bytes long. - - If NUM_REGS == 0, then subsequent matches should allocate their own - register data. - - Unless this function is called, the first search or match using - PATTERN_BUFFER will allocate its own register data, without - freeing the old data. */ - -void -re_set_registers (struct re_pattern_buffer *bufp, - struct re_registers *regs, - unsigned num_regs, - regoff_t *starts, - regoff_t *ends) -{ - if (num_regs) - { - bufp->regs_allocated = REGS_REALLOCATE; - regs->num_regs = num_regs; - regs->start = starts; - regs->end = ends; - } - else - { - bufp->regs_allocated = REGS_UNALLOCATED; - regs->num_regs = 0; - regs->start = regs->end = (regoff_t *) 0; - } -} -#ifdef _LIBC -weak_alias (__re_set_registers, re_set_registers) -#endif - -/* Entry points compatible with 4.2 BSD regex library. We don't define - them unless specifically requested. */ - -#if defined _REGEX_RE_COMP || defined _LIBC -int -# ifdef _LIBC -weak_function -# endif -re_exec (s) - const char *s; -{ - return 0 == regexec (&re_comp_buf, s, 0, NULL, 0); -} -#endif /* _REGEX_RE_COMP */ - -/* Internal entry point. */ - -/* Searches for a compiled pattern PREG in the string STRING, whose - length is LENGTH. NMATCH, PMATCH, and EFLAGS have the same - mingings with regexec. START, and RANGE have the same meanings - with re_search. - Return REG_NOERROR if we find a match, and REG_NOMATCH if not, - otherwise return the error code. - Note: We assume front end functions already check ranges. - (START + RANGE >= 0 && START + RANGE <= LENGTH) */ - -static reg_errcode_t -re_search_internal (const regex_t *preg, - const char *string, - int length, int start, int range, int stop, - size_t nmatch, regmatch_t pmatch[], - int eflags) -{ - reg_errcode_t err; - const re_dfa_t *dfa = (const re_dfa_t *) preg->buffer; - int left_lim, right_lim, incr; - int fl_longest_match, match_first, match_kind, match_last = -1; - unsigned int extra_nmatch; - int sb, ch; -#if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L) - re_match_context_t mctx = { .dfa = dfa }; -#else - re_match_context_t mctx; -#endif - char *fastmap = (preg->fastmap != NULL && preg->fastmap_accurate - && range && !preg->can_be_null) ? preg->fastmap : NULL; - RE_TRANSLATE_TYPE t = preg->translate; - -#if !(defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)) - memset (&mctx, '\0', sizeof (re_match_context_t)); - mctx.dfa = dfa; -#endif - - extra_nmatch = (nmatch > preg->re_nsub) ? nmatch - (preg->re_nsub + 1) : 0; - nmatch -= extra_nmatch; - - /* Check if the DFA haven't been compiled. */ - if (BE (preg->used == 0 || dfa->init_state == NULL - || dfa->init_state_word == NULL || dfa->init_state_nl == NULL - || dfa->init_state_begbuf == NULL, 0)) - return REG_NOMATCH; - -#ifdef DEBUG - /* We assume front-end functions already check them. */ - assert (start + range >= 0 && start + range <= length); -#endif - - /* If initial states with non-begbuf contexts have no elements, - the regex must be anchored. If preg->newline_anchor is set, - we'll never use init_state_nl, so do not check it. */ - if (dfa->init_state->nodes.nelem == 0 - && dfa->init_state_word->nodes.nelem == 0 - && (dfa->init_state_nl->nodes.nelem == 0 - || !preg->newline_anchor)) - { - if (start != 0 && start + range != 0) - return REG_NOMATCH; - start = range = 0; - } - - /* We must check the longest matching, if nmatch > 0. */ - fl_longest_match = (nmatch != 0 || dfa->nbackref); - - err = re_string_allocate (&mctx.input, string, length, dfa->nodes_len + 1, - preg->translate, preg->syntax & RE_ICASE, dfa); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - mctx.input.stop = stop; - mctx.input.raw_stop = stop; - mctx.input.newline_anchor = preg->newline_anchor; - - err = match_ctx_init (&mctx, eflags, dfa->nbackref * 2); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - - /* We will log all the DFA states through which the dfa pass, - if nmatch > 1, or this dfa has "multibyte node", which is a - back-reference or a node which can accept multibyte character or - multi character collating element. */ - if (nmatch > 1 || dfa->has_mb_node) - { - /* Avoid overflow. */ - if (BE (SIZE_MAX / sizeof (re_dfastate_t *) <= (size_t)mctx.input.bufs_len, 0)) - { - err = REG_ESPACE; - goto free_return; - } - - mctx.state_log = re_malloc (re_dfastate_t *, mctx.input.bufs_len + 1); - if (BE (mctx.state_log == NULL, 0)) - { - err = REG_ESPACE; - goto free_return; - } - } - else - mctx.state_log = NULL; - - match_first = start; - mctx.input.tip_context = (eflags & REG_NOTBOL) ? CONTEXT_BEGBUF - : CONTEXT_NEWLINE | CONTEXT_BEGBUF; - - /* Check incrementally whether of not the input string match. */ - incr = (range < 0) ? -1 : 1; - left_lim = (range < 0) ? start + range : start; - right_lim = (range < 0) ? start : start + range; - sb = dfa->mb_cur_max == 1; - match_kind = - (fastmap - ? ((sb || !(preg->syntax & RE_ICASE || t) ? 4 : 0) - | (range >= 0 ? 2 : 0) - | (t != NULL ? 1 : 0)) - : 8); - - for (;; match_first += incr) - { - err = REG_NOMATCH; - if (match_first < left_lim || right_lim < match_first) - goto free_return; - - /* Advance as rapidly as possible through the string, until we - find a plausible place to start matching. This may be done - with varying efficiency, so there are various possibilities: - only the most common of them are specialized, in order to - save on code size. We use a switch statement for speed. */ - switch (match_kind) - { - case 8: - /* No fastmap. */ - break; - - case 7: - /* Fastmap with single-byte translation, match forward. */ - while (BE (match_first < right_lim, 1) - && !fastmap[t[(unsigned char) string[match_first]]]) - ++match_first; - goto forward_match_found_start_or_reached_end; - - case 6: - /* Fastmap without translation, match forward. */ - while (BE (match_first < right_lim, 1) - && !fastmap[(unsigned char) string[match_first]]) - ++match_first; - - forward_match_found_start_or_reached_end: - if (BE (match_first == right_lim, 0)) - { - ch = match_first >= length - ? 0 : (unsigned char) string[match_first]; - if (!fastmap[t ? t[ch] : ch]) - goto free_return; - } - break; - - case 4: - case 5: - /* Fastmap without multi-byte translation, match backwards. */ - while (match_first >= left_lim) - { - ch = match_first >= length - ? 0 : (unsigned char) string[match_first]; - if (fastmap[t ? t[ch] : ch]) - break; - --match_first; - } - if (match_first < left_lim) - goto free_return; - break; - - default: - /* In this case, we can't determine easily the current byte, - since it might be a component byte of a multibyte - character. Then we use the constructed buffer instead. */ - for (;;) - { - /* If MATCH_FIRST is out of the valid range, reconstruct the - buffers. */ - unsigned int offset = match_first - mctx.input.raw_mbs_idx; - if (BE (offset >= (unsigned int) mctx.input.valid_raw_len, 0)) - { - err = re_string_reconstruct (&mctx.input, match_first, - eflags); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - - offset = match_first - mctx.input.raw_mbs_idx; - } - /* If MATCH_FIRST is out of the buffer, leave it as '\0'. - Note that MATCH_FIRST must not be smaller than 0. */ - ch = (match_first >= length - ? 0 : re_string_byte_at (&mctx.input, offset)); - if (fastmap[ch]) - break; - match_first += incr; - if (match_first < left_lim || match_first > right_lim) - { - err = REG_NOMATCH; - goto free_return; - } - } - break; - } - - /* Reconstruct the buffers so that the matcher can assume that - the matching starts from the beginning of the buffer. */ - err = re_string_reconstruct (&mctx.input, match_first, eflags); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - -#ifdef RE_ENABLE_I18N - /* Don't consider this char as a possible match start if it part, - yet isn't the head, of a multibyte character. */ - if (!sb && !re_string_first_byte (&mctx.input, 0)) - continue; -#endif - - /* It seems to be appropriate one, then use the matcher. */ - /* We assume that the matching starts from 0. */ - mctx.state_log_top = mctx.nbkref_ents = mctx.max_mb_elem_len = 0; - match_last = check_matching (&mctx, fl_longest_match, - range >= 0 ? &match_first : NULL); - if (match_last != -1) - { - if (BE (match_last == -2, 0)) - { - err = REG_ESPACE; - goto free_return; - } - else - { - mctx.match_last = match_last; - if ((!preg->no_sub && nmatch > 1) || dfa->nbackref) - { - re_dfastate_t *pstate = mctx.state_log[match_last]; - mctx.last_node = check_halt_state_context (&mctx, pstate, - match_last); - } - if ((!preg->no_sub && nmatch > 1 && dfa->has_plural_match) - || dfa->nbackref) - { - err = prune_impossible_nodes (&mctx); - if (err == REG_NOERROR) - break; - if (BE (err != REG_NOMATCH, 0)) - goto free_return; - match_last = -1; - } - else - break; /* We found a match. */ - } - } - - match_ctx_clean (&mctx); - } - -#ifdef DEBUG - assert (match_last != -1); - assert (err == REG_NOERROR); -#endif - - /* Set pmatch[] if we need. */ - if (nmatch > 0) - { - unsigned int reg_idx; - - /* Initialize registers. */ - for (reg_idx = 1; reg_idx < nmatch; ++reg_idx) - pmatch[reg_idx].rm_so = pmatch[reg_idx].rm_eo = -1; - - /* Set the points where matching start/end. */ - pmatch[0].rm_so = 0; - pmatch[0].rm_eo = mctx.match_last; - - if (!preg->no_sub && nmatch > 1) - { - err = set_regs (preg, &mctx, nmatch, pmatch, - dfa->has_plural_match && dfa->nbackref > 0); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - } - - /* At last, add the offset to the each registers, since we slided - the buffers so that we could assume that the matching starts - from 0. */ - for (reg_idx = 0; reg_idx < nmatch; ++reg_idx) - if (pmatch[reg_idx].rm_so != -1) - { -#ifdef RE_ENABLE_I18N - if (BE (mctx.input.offsets_needed != 0, 0)) - { - pmatch[reg_idx].rm_so = - (pmatch[reg_idx].rm_so == mctx.input.valid_len - ? mctx.input.valid_raw_len - : mctx.input.offsets[pmatch[reg_idx].rm_so]); - pmatch[reg_idx].rm_eo = - (pmatch[reg_idx].rm_eo == mctx.input.valid_len - ? mctx.input.valid_raw_len - : mctx.input.offsets[pmatch[reg_idx].rm_eo]); - } -#else - assert (mctx.input.offsets_needed == 0); -#endif - pmatch[reg_idx].rm_so += match_first; - pmatch[reg_idx].rm_eo += match_first; - } - for (reg_idx = 0; reg_idx < extra_nmatch; ++reg_idx) - { - pmatch[nmatch + reg_idx].rm_so = -1; - pmatch[nmatch + reg_idx].rm_eo = -1; - } - - if (dfa->subexp_map) - for (reg_idx = 0; reg_idx + 1 < nmatch; reg_idx++) - if (dfa->subexp_map[reg_idx] != (int)reg_idx) - { - pmatch[reg_idx + 1].rm_so - = pmatch[dfa->subexp_map[reg_idx] + 1].rm_so; - pmatch[reg_idx + 1].rm_eo - = pmatch[dfa->subexp_map[reg_idx] + 1].rm_eo; - } - } - - free_return: - re_free (mctx.state_log); - if (dfa->nbackref) - match_ctx_free (&mctx); - re_string_destruct (&mctx.input); - return err; -} - -static reg_errcode_t -prune_impossible_nodes (re_match_context_t *mctx) -{ - const re_dfa_t *const dfa = mctx->dfa; - int halt_node, match_last; - reg_errcode_t ret; - re_dfastate_t **sifted_states; - re_dfastate_t **lim_states = NULL; - re_sift_context_t sctx; -#ifdef DEBUG - assert (mctx->state_log != NULL); -#endif - match_last = mctx->match_last; - halt_node = mctx->last_node; - - /* Avoid overflow. */ - if (BE (SIZE_MAX / sizeof (re_dfastate_t *) <= (size_t)match_last, 0)) - return REG_ESPACE; - - sifted_states = re_malloc (re_dfastate_t *, match_last + 1); - if (BE (sifted_states == NULL, 0)) - { - ret = REG_ESPACE; - goto free_return; - } - if (dfa->nbackref) - { - lim_states = re_malloc (re_dfastate_t *, match_last + 1); - if (BE (lim_states == NULL, 0)) - { - ret = REG_ESPACE; - goto free_return; - } - while (1) - { - memset (lim_states, '\0', - sizeof (re_dfastate_t *) * (match_last + 1)); - sift_ctx_init (&sctx, sifted_states, lim_states, halt_node, - match_last); - ret = sift_states_backward (mctx, &sctx); - re_node_set_free (&sctx.limits); - if (BE (ret != REG_NOERROR, 0)) - goto free_return; - if (sifted_states[0] != NULL || lim_states[0] != NULL) - break; - do - { - --match_last; - if (match_last < 0) - { - ret = REG_NOMATCH; - goto free_return; - } - } while (mctx->state_log[match_last] == NULL - || !mctx->state_log[match_last]->halt); - halt_node = check_halt_state_context (mctx, - mctx->state_log[match_last], - match_last); - } - ret = merge_state_array (dfa, sifted_states, lim_states, - match_last + 1); - re_free (lim_states); - lim_states = NULL; - if (BE (ret != REG_NOERROR, 0)) - goto free_return; - } - else - { - sift_ctx_init (&sctx, sifted_states, lim_states, halt_node, match_last); - ret = sift_states_backward (mctx, &sctx); - re_node_set_free (&sctx.limits); - if (BE (ret != REG_NOERROR, 0)) - goto free_return; - if (sifted_states[0] == NULL) - { - ret = REG_NOMATCH; - goto free_return; - } - } - re_free (mctx->state_log); - mctx->state_log = sifted_states; - sifted_states = NULL; - mctx->last_node = halt_node; - mctx->match_last = match_last; - ret = REG_NOERROR; - free_return: - re_free (sifted_states); - re_free (lim_states); - return ret; -} - -/* Acquire an initial state and return it. - We must select appropriate initial state depending on the context, - since initial states may have constraints like "\<", "^", etc.. */ - -static inline re_dfastate_t * -__attribute ((always_inline)) internal_function -acquire_init_state_context (reg_errcode_t *err, const re_match_context_t *mctx, - int idx) -{ - const re_dfa_t *const dfa = mctx->dfa; - if (dfa->init_state->has_constraint) - { - unsigned int context; - context = re_string_context_at (&mctx->input, idx - 1, mctx->eflags); - if (IS_WORD_CONTEXT (context)) - return dfa->init_state_word; - else if (IS_ORDINARY_CONTEXT (context)) - return dfa->init_state; - else if (IS_BEGBUF_CONTEXT (context) && IS_NEWLINE_CONTEXT (context)) - return dfa->init_state_begbuf; - else if (IS_NEWLINE_CONTEXT (context)) - return dfa->init_state_nl; - else if (IS_BEGBUF_CONTEXT (context)) - { - /* It is relatively rare case, then calculate on demand. */ - return re_acquire_state_context (err, dfa, - dfa->init_state->entrance_nodes, - context); - } - else - /* Must not happen? */ - return dfa->init_state; - } - else - return dfa->init_state; -} - -/* Check whether the regular expression match input string INPUT or not, - and return the index where the matching end, return -1 if not match, - or return -2 in case of an error. - FL_LONGEST_MATCH means we want the POSIX longest matching. - If P_MATCH_FIRST is not NULL, and the match fails, it is set to the - next place where we may want to try matching. - Note that the matcher assume that the maching starts from the current - index of the buffer. */ - -static int -internal_function -check_matching (re_match_context_t *mctx, int fl_longest_match, - int *p_match_first) -{ - const re_dfa_t *const dfa = mctx->dfa; - reg_errcode_t err; - int match = 0; - int match_last = -1; - int cur_str_idx = re_string_cur_idx (&mctx->input); - re_dfastate_t *cur_state; - int at_init_state = p_match_first != NULL; - int next_start_idx = cur_str_idx; - - err = REG_NOERROR; - cur_state = acquire_init_state_context (&err, mctx, cur_str_idx); - /* An initial state must not be NULL (invalid). */ - if (BE (cur_state == NULL, 0)) - { - assert (err == REG_ESPACE); - return -2; - } - - if (mctx->state_log != NULL) - { - mctx->state_log[cur_str_idx] = cur_state; - - /* Check OP_OPEN_SUBEXP in the initial state in case that we use them - later. E.g. Processing back references. */ - if (BE (dfa->nbackref, 0)) - { - at_init_state = 0; - err = check_subexp_matching_top (mctx, &cur_state->nodes, 0); - if (BE (err != REG_NOERROR, 0)) - return err; - - if (cur_state->has_backref) - { - err = transit_state_bkref (mctx, &cur_state->nodes); - if (BE (err != REG_NOERROR, 0)) - return err; - } - } - } - - /* If the RE accepts NULL string. */ - if (BE (cur_state->halt, 0)) - { - if (!cur_state->has_constraint - || check_halt_state_context (mctx, cur_state, cur_str_idx)) - { - if (!fl_longest_match) - return cur_str_idx; - else - { - match_last = cur_str_idx; - match = 1; - } - } - } - - while (!re_string_eoi (&mctx->input)) - { - re_dfastate_t *old_state = cur_state; - int next_char_idx = re_string_cur_idx (&mctx->input) + 1; - - if (BE (next_char_idx >= mctx->input.bufs_len, 0) - || (BE (next_char_idx >= mctx->input.valid_len, 0) - && mctx->input.valid_len < mctx->input.len)) - { - err = extend_buffers (mctx); - if (BE (err != REG_NOERROR, 0)) - { - assert (err == REG_ESPACE); - return -2; - } - } - - cur_state = transit_state (&err, mctx, cur_state); - if (mctx->state_log != NULL) - cur_state = merge_state_with_log (&err, mctx, cur_state); - - if (cur_state == NULL) - { - /* Reached the invalid state or an error. Try to recover a valid - state using the state log, if available and if we have not - already found a valid (even if not the longest) match. */ - if (BE (err != REG_NOERROR, 0)) - return -2; - - if (mctx->state_log == NULL - || (match && !fl_longest_match) - || (cur_state = find_recover_state (&err, mctx)) == NULL) - break; - } - - if (BE (at_init_state, 0)) - { - if (old_state == cur_state) - next_start_idx = next_char_idx; - else - at_init_state = 0; - } - - if (cur_state->halt) - { - /* Reached a halt state. - Check the halt state can satisfy the current context. */ - if (!cur_state->has_constraint - || check_halt_state_context (mctx, cur_state, - re_string_cur_idx (&mctx->input))) - { - /* We found an appropriate halt state. */ - match_last = re_string_cur_idx (&mctx->input); - match = 1; - - /* We found a match, do not modify match_first below. */ - p_match_first = NULL; - if (!fl_longest_match) - break; - } - } - } - - if (p_match_first) - *p_match_first += next_start_idx; - - return match_last; -} - -/* Check NODE match the current context. */ - -static int -internal_function -check_halt_node_context (const re_dfa_t *dfa, int node, unsigned int context) -{ - re_token_type_t type = dfa->nodes[node].type; - unsigned int constraint = dfa->nodes[node].constraint; - if (type != END_OF_RE) - return 0; - if (!constraint) - return 1; - if (NOT_SATISFY_NEXT_CONSTRAINT (constraint, context)) - return 0; - return 1; -} - -/* Check the halt state STATE match the current context. - Return 0 if not match, if the node, STATE has, is a halt node and - match the context, return the node. */ - -static int -internal_function -check_halt_state_context (const re_match_context_t *mctx, - const re_dfastate_t *state, int idx) -{ - int i; - unsigned int context; -#ifdef DEBUG - assert (state->halt); -#endif - context = re_string_context_at (&mctx->input, idx, mctx->eflags); - for (i = 0; i < state->nodes.nelem; ++i) - if (check_halt_node_context (mctx->dfa, state->nodes.elems[i], context)) - return state->nodes.elems[i]; - return 0; -} - -/* Compute the next node to which "NFA" transit from NODE("NFA" is a NFA - corresponding to the DFA). - Return the destination node, and update EPS_VIA_NODES, return -1 in case - of errors. */ - -static int -internal_function -proceed_next_node (const re_match_context_t *mctx, int nregs, regmatch_t *regs, - int *pidx, int node, re_node_set *eps_via_nodes, - struct re_fail_stack_t *fs) -{ - const re_dfa_t *const dfa = mctx->dfa; - int i, err; - if (IS_EPSILON_NODE (dfa->nodes[node].type)) - { - re_node_set *cur_nodes = &mctx->state_log[*pidx]->nodes; - re_node_set *edests = &dfa->edests[node]; - int dest_node; - err = re_node_set_insert (eps_via_nodes, node); - if (BE (err < 0, 0)) - return -2; - /* Pick up a valid destination, or return -1 if none is found. */ - for (dest_node = -1, i = 0; i < edests->nelem; ++i) - { - int candidate = edests->elems[i]; - if (!re_node_set_contains (cur_nodes, candidate)) - continue; - if (dest_node == -1) - dest_node = candidate; - - else - { - /* In order to avoid infinite loop like "(a*)*", return the second - epsilon-transition if the first was already considered. */ - if (re_node_set_contains (eps_via_nodes, dest_node)) - return candidate; - - /* Otherwise, push the second epsilon-transition on the fail stack. */ - else if (fs != NULL - && push_fail_stack (fs, *pidx, candidate, nregs, regs, - eps_via_nodes)) - return -2; - - /* We know we are going to exit. */ - break; - } - } - return dest_node; - } - else - { - int naccepted = 0; - re_token_type_t type = dfa->nodes[node].type; - -#ifdef RE_ENABLE_I18N - if (dfa->nodes[node].accept_mb) - naccepted = check_node_accept_bytes (dfa, node, &mctx->input, *pidx); - else -#endif /* RE_ENABLE_I18N */ - if (type == OP_BACK_REF) - { - int subexp_idx = dfa->nodes[node].opr.idx + 1; - naccepted = regs[subexp_idx].rm_eo - regs[subexp_idx].rm_so; - if (fs != NULL) - { - if (regs[subexp_idx].rm_so == -1 || regs[subexp_idx].rm_eo == -1) - return -1; - else if (naccepted) - { - char *buf = (char *) re_string_get_buffer (&mctx->input); - if (memcmp (buf + regs[subexp_idx].rm_so, buf + *pidx, - naccepted) != 0) - return -1; - } - } - - if (naccepted == 0) - { - int dest_node; - err = re_node_set_insert (eps_via_nodes, node); - if (BE (err < 0, 0)) - return -2; - dest_node = dfa->edests[node].elems[0]; - if (re_node_set_contains (&mctx->state_log[*pidx]->nodes, - dest_node)) - return dest_node; - } - } - - if (naccepted != 0 - || check_node_accept (mctx, dfa->nodes + node, *pidx)) - { - int dest_node = dfa->nexts[node]; - *pidx = (naccepted == 0) ? *pidx + 1 : *pidx + naccepted; - if (fs && (*pidx > mctx->match_last || mctx->state_log[*pidx] == NULL - || !re_node_set_contains (&mctx->state_log[*pidx]->nodes, - dest_node))) - return -1; - re_node_set_empty (eps_via_nodes); - return dest_node; - } - } - return -1; -} - -static reg_errcode_t -internal_function -push_fail_stack (struct re_fail_stack_t *fs, int str_idx, int dest_node, - int nregs, regmatch_t *regs, re_node_set *eps_via_nodes) -{ - reg_errcode_t err; - int num = fs->num++; - if (fs->num == fs->alloc) - { - struct re_fail_stack_ent_t *new_array; - new_array = realloc (fs->stack, (sizeof (struct re_fail_stack_ent_t) - * fs->alloc * 2)); - if (new_array == NULL) - return REG_ESPACE; - fs->alloc *= 2; - fs->stack = new_array; - } - fs->stack[num].idx = str_idx; - fs->stack[num].node = dest_node; - fs->stack[num].regs = re_malloc (regmatch_t, nregs); - if (fs->stack[num].regs == NULL) - return REG_ESPACE; - memcpy (fs->stack[num].regs, regs, sizeof (regmatch_t) * nregs); - err = re_node_set_init_copy (&fs->stack[num].eps_via_nodes, eps_via_nodes); - return err; -} - -static int -internal_function -pop_fail_stack (struct re_fail_stack_t *fs, int *pidx, int nregs, - regmatch_t *regs, re_node_set *eps_via_nodes) -{ - int num = --fs->num; - assert (num >= 0); - *pidx = fs->stack[num].idx; - memcpy (regs, fs->stack[num].regs, sizeof (regmatch_t) * nregs); - re_node_set_free (eps_via_nodes); - re_free (fs->stack[num].regs); - *eps_via_nodes = fs->stack[num].eps_via_nodes; - return fs->stack[num].node; -} - -/* Set the positions where the subexpressions are starts/ends to registers - PMATCH. - Note: We assume that pmatch[0] is already set, and - pmatch[i].rm_so == pmatch[i].rm_eo == -1 for 0 < i < nmatch. */ - -static reg_errcode_t -internal_function -set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch, - regmatch_t *pmatch, int fl_backtrack) -{ - const re_dfa_t *dfa = (const re_dfa_t *) preg->buffer; - int idx, cur_node; - re_node_set eps_via_nodes; - struct re_fail_stack_t *fs; - struct re_fail_stack_t fs_body = { 0, 2, NULL }; - regmatch_t *prev_idx_match; - int prev_idx_match_malloced = 0; - -#ifdef DEBUG - assert (nmatch > 1); - assert (mctx->state_log != NULL); -#endif - if (fl_backtrack) - { - fs = &fs_body; - fs->stack = re_malloc (struct re_fail_stack_ent_t, fs->alloc); - if (fs->stack == NULL) - return REG_ESPACE; - } - else - fs = NULL; - - cur_node = dfa->init_node; - re_node_set_init_empty (&eps_via_nodes); - -#ifdef HAVE_ALLOCA - if (__libc_use_alloca (nmatch * sizeof (regmatch_t))) - prev_idx_match = (regmatch_t *) alloca (nmatch * sizeof (regmatch_t)); - else -#endif - { - prev_idx_match = re_malloc (regmatch_t, nmatch); - if (prev_idx_match == NULL) - { - free_fail_stack_return (fs); - return REG_ESPACE; - } - prev_idx_match_malloced = 1; - } - memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch); - - for (idx = pmatch[0].rm_so; idx <= pmatch[0].rm_eo ;) - { - update_regs (dfa, pmatch, prev_idx_match, cur_node, idx, nmatch); - - if (idx == pmatch[0].rm_eo && cur_node == mctx->last_node) - { - unsigned int reg_idx; - if (fs) - { - for (reg_idx = 0; reg_idx < nmatch; ++reg_idx) - if (pmatch[reg_idx].rm_so > -1 && pmatch[reg_idx].rm_eo == -1) - break; - if (reg_idx == nmatch) - { - re_node_set_free (&eps_via_nodes); - if (prev_idx_match_malloced) - re_free (prev_idx_match); - return free_fail_stack_return (fs); - } - cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch, - &eps_via_nodes); - } - else - { - re_node_set_free (&eps_via_nodes); - if (prev_idx_match_malloced) - re_free (prev_idx_match); - return REG_NOERROR; - } - } - - /* Proceed to next node. */ - cur_node = proceed_next_node (mctx, nmatch, pmatch, &idx, cur_node, - &eps_via_nodes, fs); - - if (BE (cur_node < 0, 0)) - { - if (BE (cur_node == -2, 0)) - { - re_node_set_free (&eps_via_nodes); - if (prev_idx_match_malloced) - re_free (prev_idx_match); - free_fail_stack_return (fs); - return REG_ESPACE; - } - if (fs) - cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch, - &eps_via_nodes); - else - { - re_node_set_free (&eps_via_nodes); - if (prev_idx_match_malloced) - re_free (prev_idx_match); - return REG_NOMATCH; - } - } - } - re_node_set_free (&eps_via_nodes); - if (prev_idx_match_malloced) - re_free (prev_idx_match); - return free_fail_stack_return (fs); -} - -static reg_errcode_t -internal_function -free_fail_stack_return (struct re_fail_stack_t *fs) -{ - if (fs) - { - int fs_idx; - for (fs_idx = 0; fs_idx < fs->num; ++fs_idx) - { - re_node_set_free (&fs->stack[fs_idx].eps_via_nodes); - re_free (fs->stack[fs_idx].regs); - } - re_free (fs->stack); - } - return REG_NOERROR; -} - -static void -internal_function -update_regs (const re_dfa_t *dfa, regmatch_t *pmatch, - regmatch_t *prev_idx_match, int cur_node, int cur_idx, int nmatch) -{ - int type = dfa->nodes[cur_node].type; - if (type == OP_OPEN_SUBEXP) - { - int reg_num = dfa->nodes[cur_node].opr.idx + 1; - - /* We are at the first node of this sub expression. */ - if (reg_num < nmatch) - { - pmatch[reg_num].rm_so = cur_idx; - pmatch[reg_num].rm_eo = -1; - } - } - else if (type == OP_CLOSE_SUBEXP) - { - int reg_num = dfa->nodes[cur_node].opr.idx + 1; - if (reg_num < nmatch) - { - /* We are at the last node of this sub expression. */ - if (pmatch[reg_num].rm_so < cur_idx) - { - pmatch[reg_num].rm_eo = cur_idx; - /* This is a non-empty match or we are not inside an optional - subexpression. Accept this right away. */ - memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch); - } - else - { - if (dfa->nodes[cur_node].opt_subexp - && prev_idx_match[reg_num].rm_so != -1) - /* We transited through an empty match for an optional - subexpression, like (a?)*, and this is not the subexp's - first match. Copy back the old content of the registers - so that matches of an inner subexpression are undone as - well, like in ((a?))*. */ - memcpy (pmatch, prev_idx_match, sizeof (regmatch_t) * nmatch); - else - /* We completed a subexpression, but it may be part of - an optional one, so do not update PREV_IDX_MATCH. */ - pmatch[reg_num].rm_eo = cur_idx; - } - } - } -} - -/* This function checks the STATE_LOG from the SCTX->last_str_idx to 0 - and sift the nodes in each states according to the following rules. - Updated state_log will be wrote to STATE_LOG. - - Rules: We throw away the Node `a' in the STATE_LOG[STR_IDX] if... - 1. When STR_IDX == MATCH_LAST(the last index in the state_log): - If `a' isn't the LAST_NODE and `a' can't epsilon transit to - the LAST_NODE, we throw away the node `a'. - 2. When 0 <= STR_IDX < MATCH_LAST and `a' accepts - string `s' and transit to `b': - i. If 'b' isn't in the STATE_LOG[STR_IDX+strlen('s')], we throw - away the node `a'. - ii. If 'b' is in the STATE_LOG[STR_IDX+strlen('s')] but 'b' is - thrown away, we throw away the node `a'. - 3. When 0 <= STR_IDX < MATCH_LAST and 'a' epsilon transit to 'b': - i. If 'b' isn't in the STATE_LOG[STR_IDX], we throw away the - node `a'. - ii. If 'b' is in the STATE_LOG[STR_IDX] but 'b' is thrown away, - we throw away the node `a'. */ - -#define STATE_NODE_CONTAINS(state,node) \ - ((state) != NULL && re_node_set_contains (&(state)->nodes, node)) - -static reg_errcode_t -internal_function -sift_states_backward (const re_match_context_t *mctx, re_sift_context_t *sctx) -{ - reg_errcode_t err; - int null_cnt = 0; - int str_idx = sctx->last_str_idx; - re_node_set cur_dest; - -#ifdef DEBUG - assert (mctx->state_log != NULL && mctx->state_log[str_idx] != NULL); -#endif - - /* Build sifted state_log[str_idx]. It has the nodes which can epsilon - transit to the last_node and the last_node itself. */ - err = re_node_set_init_1 (&cur_dest, sctx->last_node); - if (BE (err != REG_NOERROR, 0)) - return err; - err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - - /* Then check each states in the state_log. */ - while (str_idx > 0) - { - /* Update counters. */ - null_cnt = (sctx->sifted_states[str_idx] == NULL) ? null_cnt + 1 : 0; - if (null_cnt > mctx->max_mb_elem_len) - { - memset (sctx->sifted_states, '\0', - sizeof (re_dfastate_t *) * str_idx); - re_node_set_free (&cur_dest); - return REG_NOERROR; - } - re_node_set_empty (&cur_dest); - --str_idx; - - if (mctx->state_log[str_idx]) - { - err = build_sifted_states (mctx, sctx, str_idx, &cur_dest); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - } - - /* Add all the nodes which satisfy the following conditions: - - It can epsilon transit to a node in CUR_DEST. - - It is in CUR_SRC. - And update state_log. */ - err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - } - err = REG_NOERROR; - free_return: - re_node_set_free (&cur_dest); - return err; -} - -static reg_errcode_t -internal_function -build_sifted_states (const re_match_context_t *mctx, re_sift_context_t *sctx, - int str_idx, re_node_set *cur_dest) -{ - const re_dfa_t *const dfa = mctx->dfa; - const re_node_set *cur_src = &mctx->state_log[str_idx]->non_eps_nodes; - int i; - - /* Then build the next sifted state. - We build the next sifted state on `cur_dest', and update - `sifted_states[str_idx]' with `cur_dest'. - Note: - `cur_dest' is the sifted state from `state_log[str_idx + 1]'. - `cur_src' points the node_set of the old `state_log[str_idx]' - (with the epsilon nodes pre-filtered out). */ - for (i = 0; i < cur_src->nelem; i++) - { - int prev_node = cur_src->elems[i]; - int naccepted = 0; - int ret; - -#ifdef DEBUG - re_token_type_t type = dfa->nodes[prev_node].type; - assert (!IS_EPSILON_NODE (type)); -#endif -#ifdef RE_ENABLE_I18N - /* If the node may accept `multi byte'. */ - if (dfa->nodes[prev_node].accept_mb) - naccepted = sift_states_iter_mb (mctx, sctx, prev_node, - str_idx, sctx->last_str_idx); -#endif /* RE_ENABLE_I18N */ - - /* We don't check backreferences here. - See update_cur_sifted_state(). */ - if (!naccepted - && check_node_accept (mctx, dfa->nodes + prev_node, str_idx) - && STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + 1], - dfa->nexts[prev_node])) - naccepted = 1; - - if (naccepted == 0) - continue; - - if (sctx->limits.nelem) - { - int to_idx = str_idx + naccepted; - if (check_dst_limits (mctx, &sctx->limits, - dfa->nexts[prev_node], to_idx, - prev_node, str_idx)) - continue; - } - ret = re_node_set_insert (cur_dest, prev_node); - if (BE (ret == -1, 0)) - return REG_ESPACE; - } - - return REG_NOERROR; -} - -/* Helper functions. */ - -static reg_errcode_t -internal_function -clean_state_log_if_needed (re_match_context_t *mctx, int next_state_log_idx) -{ - int top = mctx->state_log_top; - - if (next_state_log_idx >= mctx->input.bufs_len - || (next_state_log_idx >= mctx->input.valid_len - && mctx->input.valid_len < mctx->input.len)) - { - reg_errcode_t err; - err = extend_buffers (mctx); - if (BE (err != REG_NOERROR, 0)) - return err; - } - - if (top < next_state_log_idx) - { - memset (mctx->state_log + top + 1, '\0', - sizeof (re_dfastate_t *) * (next_state_log_idx - top)); - mctx->state_log_top = next_state_log_idx; - } - return REG_NOERROR; -} - -static reg_errcode_t -internal_function -merge_state_array (const re_dfa_t *dfa, re_dfastate_t **dst, - re_dfastate_t **src, int num) -{ - int st_idx; - reg_errcode_t err; - for (st_idx = 0; st_idx < num; ++st_idx) - { - if (dst[st_idx] == NULL) - dst[st_idx] = src[st_idx]; - else if (src[st_idx] != NULL) - { - re_node_set merged_set; - err = re_node_set_init_union (&merged_set, &dst[st_idx]->nodes, - &src[st_idx]->nodes); - if (BE (err != REG_NOERROR, 0)) - return err; - dst[st_idx] = re_acquire_state (&err, dfa, &merged_set); - re_node_set_free (&merged_set); - if (BE (err != REG_NOERROR, 0)) - return err; - } - } - return REG_NOERROR; -} - -static reg_errcode_t -internal_function -update_cur_sifted_state (const re_match_context_t *mctx, - re_sift_context_t *sctx, int str_idx, - re_node_set *dest_nodes) -{ - const re_dfa_t *const dfa = mctx->dfa; - reg_errcode_t err = REG_NOERROR; - const re_node_set *candidates; - candidates = ((mctx->state_log[str_idx] == NULL) ? NULL - : &mctx->state_log[str_idx]->nodes); - - if (dest_nodes->nelem == 0) - sctx->sifted_states[str_idx] = NULL; - else - { - if (candidates) - { - /* At first, add the nodes which can epsilon transit to a node in - DEST_NODE. */ - err = add_epsilon_src_nodes (dfa, dest_nodes, candidates); - if (BE (err != REG_NOERROR, 0)) - return err; - - /* Then, check the limitations in the current sift_context. */ - if (sctx->limits.nelem) - { - err = check_subexp_limits (dfa, dest_nodes, candidates, &sctx->limits, - mctx->bkref_ents, str_idx); - if (BE (err != REG_NOERROR, 0)) - return err; - } - } - - sctx->sifted_states[str_idx] = re_acquire_state (&err, dfa, dest_nodes); - if (BE (err != REG_NOERROR, 0)) - return err; - } - - if (candidates && mctx->state_log[str_idx]->has_backref) - { - err = sift_states_bkref (mctx, sctx, str_idx, candidates); - if (BE (err != REG_NOERROR, 0)) - return err; - } - return REG_NOERROR; -} - -static reg_errcode_t -internal_function -add_epsilon_src_nodes (const re_dfa_t *dfa, re_node_set *dest_nodes, - const re_node_set *candidates) -{ - reg_errcode_t err = REG_NOERROR; - int i; - - re_dfastate_t *state = re_acquire_state (&err, dfa, dest_nodes); - if (BE (err != REG_NOERROR, 0)) - return err; - - if (!state->inveclosure.alloc) - { - err = re_node_set_alloc (&state->inveclosure, dest_nodes->nelem); - if (BE (err != REG_NOERROR, 0)) - return REG_ESPACE; - for (i = 0; i < dest_nodes->nelem; i++) - { - err = re_node_set_merge (&state->inveclosure, - dfa->inveclosures + dest_nodes->elems[i]); - if (BE (err != REG_NOERROR, 0)) - return REG_ESPACE; - } - } - return re_node_set_add_intersect (dest_nodes, candidates, - &state->inveclosure); -} - -static reg_errcode_t -internal_function -sub_epsilon_src_nodes (const re_dfa_t *dfa, int node, re_node_set *dest_nodes, - const re_node_set *candidates) -{ - int ecl_idx; - reg_errcode_t err; - re_node_set *inv_eclosure = dfa->inveclosures + node; - re_node_set except_nodes; - re_node_set_init_empty (&except_nodes); - for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx) - { - int cur_node = inv_eclosure->elems[ecl_idx]; - if (cur_node == node) - continue; - if (IS_EPSILON_NODE (dfa->nodes[cur_node].type)) - { - int edst1 = dfa->edests[cur_node].elems[0]; - int edst2 = ((dfa->edests[cur_node].nelem > 1) - ? dfa->edests[cur_node].elems[1] : -1); - if ((!re_node_set_contains (inv_eclosure, edst1) - && re_node_set_contains (dest_nodes, edst1)) - || (edst2 > 0 - && !re_node_set_contains (inv_eclosure, edst2) - && re_node_set_contains (dest_nodes, edst2))) - { - err = re_node_set_add_intersect (&except_nodes, candidates, - dfa->inveclosures + cur_node); - if (BE (err != REG_NOERROR, 0)) - { - re_node_set_free (&except_nodes); - return err; - } - } - } - } - for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx) - { - int cur_node = inv_eclosure->elems[ecl_idx]; - if (!re_node_set_contains (&except_nodes, cur_node)) - { - int idx = re_node_set_contains (dest_nodes, cur_node) - 1; - re_node_set_remove_at (dest_nodes, idx); - } - } - re_node_set_free (&except_nodes); - return REG_NOERROR; -} - -static int -internal_function -check_dst_limits (const re_match_context_t *mctx, re_node_set *limits, - int dst_node, int dst_idx, int src_node, int src_idx) -{ - const re_dfa_t *const dfa = mctx->dfa; - int lim_idx, src_pos, dst_pos; - - int dst_bkref_idx = search_cur_bkref_entry (mctx, dst_idx); - int src_bkref_idx = search_cur_bkref_entry (mctx, src_idx); - for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx) - { - int subexp_idx; - struct re_backref_cache_entry *ent; - ent = mctx->bkref_ents + limits->elems[lim_idx]; - subexp_idx = dfa->nodes[ent->node].opr.idx; - - dst_pos = check_dst_limits_calc_pos (mctx, limits->elems[lim_idx], - subexp_idx, dst_node, dst_idx, - dst_bkref_idx); - src_pos = check_dst_limits_calc_pos (mctx, limits->elems[lim_idx], - subexp_idx, src_node, src_idx, - src_bkref_idx); - - /* In case of: - ( ) - ( ) - ( ) */ - if (src_pos == dst_pos) - continue; /* This is unrelated limitation. */ - else - return 1; - } - return 0; -} - -static int -internal_function -check_dst_limits_calc_pos_1 (const re_match_context_t *mctx, int boundaries, - int subexp_idx, int from_node, int bkref_idx) -{ - const re_dfa_t *const dfa = mctx->dfa; - const re_node_set *eclosures = dfa->eclosures + from_node; - int node_idx; - - /* Else, we are on the boundary: examine the nodes on the epsilon - closure. */ - for (node_idx = 0; node_idx < eclosures->nelem; ++node_idx) - { - int node = eclosures->elems[node_idx]; - switch (dfa->nodes[node].type) - { - case OP_BACK_REF: - if (bkref_idx != -1) - { - struct re_backref_cache_entry *ent = mctx->bkref_ents + bkref_idx; - do - { - int dst, cpos; - - if (ent->node != node) - continue; - - if (subexp_idx < BITSET_WORD_BITS - && !(ent->eps_reachable_subexps_map - & ((bitset_word_t) 1 << subexp_idx))) - continue; - - /* Recurse trying to reach the OP_OPEN_SUBEXP and - OP_CLOSE_SUBEXP cases below. But, if the - destination node is the same node as the source - node, don't recurse because it would cause an - infinite loop: a regex that exhibits this behavior - is ()\1*\1* */ - dst = dfa->edests[node].elems[0]; - if (dst == from_node) - { - if (boundaries & 1) - return -1; - else /* if (boundaries & 2) */ - return 0; - } - - cpos = - check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx, - dst, bkref_idx); - if (cpos == -1 /* && (boundaries & 1) */) - return -1; - if (cpos == 0 && (boundaries & 2)) - return 0; - - if (subexp_idx < BITSET_WORD_BITS) - ent->eps_reachable_subexps_map - &= ~((bitset_word_t) 1 << subexp_idx); - } - while (ent++->more); - } - break; - - case OP_OPEN_SUBEXP: - if ((boundaries & 1) && subexp_idx == dfa->nodes[node].opr.idx) - return -1; - break; - - case OP_CLOSE_SUBEXP: - if ((boundaries & 2) && subexp_idx == dfa->nodes[node].opr.idx) - return 0; - break; - - default: - break; - } - } - - return (boundaries & 2) ? 1 : 0; -} - -static int -internal_function -check_dst_limits_calc_pos (const re_match_context_t *mctx, int limit, - int subexp_idx, int from_node, int str_idx, - int bkref_idx) -{ - struct re_backref_cache_entry *lim = mctx->bkref_ents + limit; - int boundaries; - - /* If we are outside the range of the subexpression, return -1 or 1. */ - if (str_idx < lim->subexp_from) - return -1; - - if (lim->subexp_to < str_idx) - return 1; - - /* If we are within the subexpression, return 0. */ - boundaries = (str_idx == lim->subexp_from); - boundaries |= (str_idx == lim->subexp_to) << 1; - if (boundaries == 0) - return 0; - - /* Else, examine epsilon closure. */ - return check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx, - from_node, bkref_idx); -} - -/* Check the limitations of sub expressions LIMITS, and remove the nodes - which are against limitations from DEST_NODES. */ - -static reg_errcode_t -internal_function -check_subexp_limits (const re_dfa_t *dfa, re_node_set *dest_nodes, - const re_node_set *candidates, re_node_set *limits, - struct re_backref_cache_entry *bkref_ents, int str_idx) -{ - reg_errcode_t err; - int node_idx, lim_idx; - - for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx) - { - int subexp_idx; - struct re_backref_cache_entry *ent; - ent = bkref_ents + limits->elems[lim_idx]; - - if (str_idx <= ent->subexp_from || ent->str_idx < str_idx) - continue; /* This is unrelated limitation. */ - - subexp_idx = dfa->nodes[ent->node].opr.idx; - if (ent->subexp_to == str_idx) - { - int ops_node = -1; - int cls_node = -1; - for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx) - { - int node = dest_nodes->elems[node_idx]; - re_token_type_t type = dfa->nodes[node].type; - if (type == OP_OPEN_SUBEXP - && subexp_idx == dfa->nodes[node].opr.idx) - ops_node = node; - else if (type == OP_CLOSE_SUBEXP - && subexp_idx == dfa->nodes[node].opr.idx) - cls_node = node; - } - - /* Check the limitation of the open subexpression. */ - /* Note that (ent->subexp_to = str_idx != ent->subexp_from). */ - if (ops_node >= 0) - { - err = sub_epsilon_src_nodes (dfa, ops_node, dest_nodes, - candidates); - if (BE (err != REG_NOERROR, 0)) - return err; - } - - /* Check the limitation of the close subexpression. */ - if (cls_node >= 0) - for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx) - { - int node = dest_nodes->elems[node_idx]; - if (!re_node_set_contains (dfa->inveclosures + node, - cls_node) - && !re_node_set_contains (dfa->eclosures + node, - cls_node)) - { - /* It is against this limitation. - Remove it form the current sifted state. */ - err = sub_epsilon_src_nodes (dfa, node, dest_nodes, - candidates); - if (BE (err != REG_NOERROR, 0)) - return err; - --node_idx; - } - } - } - else /* (ent->subexp_to != str_idx) */ - { - for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx) - { - int node = dest_nodes->elems[node_idx]; - re_token_type_t type = dfa->nodes[node].type; - if (type == OP_CLOSE_SUBEXP || type == OP_OPEN_SUBEXP) - { - if (subexp_idx != dfa->nodes[node].opr.idx) - continue; - /* It is against this limitation. - Remove it form the current sifted state. */ - err = sub_epsilon_src_nodes (dfa, node, dest_nodes, - candidates); - if (BE (err != REG_NOERROR, 0)) - return err; - } - } - } - } - return REG_NOERROR; -} - -static reg_errcode_t -internal_function -sift_states_bkref (const re_match_context_t *mctx, re_sift_context_t *sctx, - int str_idx, const re_node_set *candidates) -{ - const re_dfa_t *const dfa = mctx->dfa; - reg_errcode_t err; - int node_idx, node; - re_sift_context_t local_sctx; - int first_idx = search_cur_bkref_entry (mctx, str_idx); - - if (first_idx == -1) - return REG_NOERROR; - - local_sctx.sifted_states = NULL; /* Mark that it hasn't been initialized. */ - - for (node_idx = 0; node_idx < candidates->nelem; ++node_idx) - { - int enabled_idx; - re_token_type_t type; - struct re_backref_cache_entry *entry; - node = candidates->elems[node_idx]; - type = dfa->nodes[node].type; - /* Avoid infinite loop for the REs like "()\1+". */ - if (node == sctx->last_node && str_idx == sctx->last_str_idx) - continue; - if (type != OP_BACK_REF) - continue; - - entry = mctx->bkref_ents + first_idx; - enabled_idx = first_idx; - do - { - int subexp_len; - int to_idx; - int dst_node; - int ret; - re_dfastate_t *cur_state; - - if (entry->node != node) - continue; - subexp_len = entry->subexp_to - entry->subexp_from; - to_idx = str_idx + subexp_len; - dst_node = (subexp_len ? dfa->nexts[node] - : dfa->edests[node].elems[0]); - - if (to_idx > sctx->last_str_idx - || sctx->sifted_states[to_idx] == NULL - || !STATE_NODE_CONTAINS (sctx->sifted_states[to_idx], dst_node) - || check_dst_limits (mctx, &sctx->limits, node, - str_idx, dst_node, to_idx)) - continue; - - if (local_sctx.sifted_states == NULL) - { - local_sctx = *sctx; - err = re_node_set_init_copy (&local_sctx.limits, &sctx->limits); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - } - local_sctx.last_node = node; - local_sctx.last_str_idx = str_idx; - ret = re_node_set_insert (&local_sctx.limits, enabled_idx); - if (BE (ret < 0, 0)) - { - err = REG_ESPACE; - goto free_return; - } - cur_state = local_sctx.sifted_states[str_idx]; - err = sift_states_backward (mctx, &local_sctx); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - if (sctx->limited_states != NULL) - { - err = merge_state_array (dfa, sctx->limited_states, - local_sctx.sifted_states, - str_idx + 1); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - } - local_sctx.sifted_states[str_idx] = cur_state; - re_node_set_remove (&local_sctx.limits, enabled_idx); - - /* mctx->bkref_ents may have changed, reload the pointer. */ - entry = mctx->bkref_ents + enabled_idx; - } - while (enabled_idx++, entry++->more); - } - err = REG_NOERROR; - free_return: - if (local_sctx.sifted_states != NULL) - { - re_node_set_free (&local_sctx.limits); - } - - return err; -} - - -#ifdef RE_ENABLE_I18N -static int -internal_function -sift_states_iter_mb (const re_match_context_t *mctx, re_sift_context_t *sctx, - int node_idx, int str_idx, int max_str_idx) -{ - const re_dfa_t *const dfa = mctx->dfa; - int naccepted; - /* Check the node can accept `multi byte'. */ - naccepted = check_node_accept_bytes (dfa, node_idx, &mctx->input, str_idx); - if (naccepted > 0 && str_idx + naccepted <= max_str_idx && - !STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + naccepted], - dfa->nexts[node_idx])) - /* The node can't accept the `multi byte', or the - destination was already thrown away, then the node - could't accept the current input `multi byte'. */ - naccepted = 0; - /* Otherwise, it is sure that the node could accept - `naccepted' bytes input. */ - return naccepted; -} -#endif /* RE_ENABLE_I18N */ - - -/* Functions for state transition. */ - -/* Return the next state to which the current state STATE will transit by - accepting the current input byte, and update STATE_LOG if necessary. - If STATE can accept a multibyte char/collating element/back reference - update the destination of STATE_LOG. */ - -static re_dfastate_t * -internal_function -transit_state (reg_errcode_t *err, re_match_context_t *mctx, - re_dfastate_t *state) -{ - re_dfastate_t **trtable; - unsigned char ch; - -#ifdef RE_ENABLE_I18N - /* If the current state can accept multibyte. */ - if (BE (state->accept_mb, 0)) - { - *err = transit_state_mb (mctx, state); - if (BE (*err != REG_NOERROR, 0)) - return NULL; - } -#endif /* RE_ENABLE_I18N */ - - /* Then decide the next state with the single byte. */ -#if 0 - if (0) - /* don't use transition table */ - return transit_state_sb (err, mctx, state); -#endif - - /* Use transition table */ - ch = re_string_fetch_byte (&mctx->input); - for (;;) - { - trtable = state->trtable; - if (BE (trtable != NULL, 1)) - return trtable[ch]; - - trtable = state->word_trtable; - if (BE (trtable != NULL, 1)) - { - unsigned int context; - context - = re_string_context_at (&mctx->input, - re_string_cur_idx (&mctx->input) - 1, - mctx->eflags); - if (IS_WORD_CONTEXT (context)) - return trtable[ch + SBC_MAX]; - else - return trtable[ch]; - } - - if (!build_trtable (mctx->dfa, state)) - { - *err = REG_ESPACE; - return NULL; - } - - /* Retry, we now have a transition table. */ - } -} - -/* Update the state_log if we need */ -re_dfastate_t * -internal_function -merge_state_with_log (reg_errcode_t *err, re_match_context_t *mctx, - re_dfastate_t *next_state) -{ - const re_dfa_t *const dfa = mctx->dfa; - int cur_idx = re_string_cur_idx (&mctx->input); - - if (cur_idx > mctx->state_log_top) - { - mctx->state_log[cur_idx] = next_state; - mctx->state_log_top = cur_idx; - } - else if (mctx->state_log[cur_idx] == 0) - { - mctx->state_log[cur_idx] = next_state; - } - else - { - re_dfastate_t *pstate; - unsigned int context; - re_node_set next_nodes, *log_nodes, *table_nodes = NULL; - /* If (state_log[cur_idx] != 0), it implies that cur_idx is - the destination of a multibyte char/collating element/ - back reference. Then the next state is the union set of - these destinations and the results of the transition table. */ - pstate = mctx->state_log[cur_idx]; - log_nodes = pstate->entrance_nodes; - if (next_state != NULL) - { - table_nodes = next_state->entrance_nodes; - *err = re_node_set_init_union (&next_nodes, table_nodes, - log_nodes); - if (BE (*err != REG_NOERROR, 0)) - return NULL; - } - else - next_nodes = *log_nodes; - /* Note: We already add the nodes of the initial state, - then we don't need to add them here. */ - - context = re_string_context_at (&mctx->input, - re_string_cur_idx (&mctx->input) - 1, - mctx->eflags); - next_state = mctx->state_log[cur_idx] - = re_acquire_state_context (err, dfa, &next_nodes, context); - /* We don't need to check errors here, since the return value of - this function is next_state and ERR is already set. */ - - if (table_nodes != NULL) - re_node_set_free (&next_nodes); - } - - if (BE (dfa->nbackref, 0) && next_state != NULL) - { - /* Check OP_OPEN_SUBEXP in the current state in case that we use them - later. We must check them here, since the back references in the - next state might use them. */ - *err = check_subexp_matching_top (mctx, &next_state->nodes, - cur_idx); - if (BE (*err != REG_NOERROR, 0)) - return NULL; - - /* If the next state has back references. */ - if (next_state->has_backref) - { - *err = transit_state_bkref (mctx, &next_state->nodes); - if (BE (*err != REG_NOERROR, 0)) - return NULL; - next_state = mctx->state_log[cur_idx]; - } - } - - return next_state; -} - -/* Skip bytes in the input that correspond to part of a - multi-byte match, then look in the log for a state - from which to restart matching. */ -re_dfastate_t * -internal_function -find_recover_state (reg_errcode_t *err, re_match_context_t *mctx) -{ - re_dfastate_t *cur_state; - do - { - int max = mctx->state_log_top; - int cur_str_idx = re_string_cur_idx (&mctx->input); - - do - { - if (++cur_str_idx > max) - return NULL; - re_string_skip_bytes (&mctx->input, 1); - } - while (mctx->state_log[cur_str_idx] == NULL); - - cur_state = merge_state_with_log (err, mctx, NULL); - } - while (*err == REG_NOERROR && cur_state == NULL); - return cur_state; -} - -/* Helper functions for transit_state. */ - -/* From the node set CUR_NODES, pick up the nodes whose types are - OP_OPEN_SUBEXP and which have corresponding back references in the regular - expression. And register them to use them later for evaluating the - correspoding back references. */ - -static reg_errcode_t -internal_function -check_subexp_matching_top (re_match_context_t *mctx, re_node_set *cur_nodes, - int str_idx) -{ - const re_dfa_t *const dfa = mctx->dfa; - int node_idx; - reg_errcode_t err; - - /* TODO: This isn't efficient. - Because there might be more than one nodes whose types are - OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all - nodes. - E.g. RE: (a){2} */ - for (node_idx = 0; node_idx < cur_nodes->nelem; ++node_idx) - { - int node = cur_nodes->elems[node_idx]; - if (dfa->nodes[node].type == OP_OPEN_SUBEXP - && dfa->nodes[node].opr.idx < BITSET_WORD_BITS - && (dfa->used_bkref_map - & ((bitset_word_t) 1 << dfa->nodes[node].opr.idx))) - { - err = match_ctx_add_subtop (mctx, node, str_idx); - if (BE (err != REG_NOERROR, 0)) - return err; - } - } - return REG_NOERROR; -} - -#if 0 -/* Return the next state to which the current state STATE will transit by - accepting the current input byte. */ - -static re_dfastate_t * -transit_state_sb (reg_errcode_t *err, re_match_context_t *mctx, - re_dfastate_t *state) -{ - const re_dfa_t *const dfa = mctx->dfa; - re_node_set next_nodes; - re_dfastate_t *next_state; - int node_cnt, cur_str_idx = re_string_cur_idx (&mctx->input); - unsigned int context; - - *err = re_node_set_alloc (&next_nodes, state->nodes.nelem + 1); - if (BE (*err != REG_NOERROR, 0)) - return NULL; - for (node_cnt = 0; node_cnt < state->nodes.nelem; ++node_cnt) - { - int cur_node = state->nodes.elems[node_cnt]; - if (check_node_accept (mctx, dfa->nodes + cur_node, cur_str_idx)) - { - *err = re_node_set_merge (&next_nodes, - dfa->eclosures + dfa->nexts[cur_node]); - if (BE (*err != REG_NOERROR, 0)) - { - re_node_set_free (&next_nodes); - return NULL; - } - } - } - context = re_string_context_at (&mctx->input, cur_str_idx, mctx->eflags); - next_state = re_acquire_state_context (err, dfa, &next_nodes, context); - /* We don't need to check errors here, since the return value of - this function is next_state and ERR is already set. */ - - re_node_set_free (&next_nodes); - re_string_skip_bytes (&mctx->input, 1); - return next_state; -} -#endif - -#ifdef RE_ENABLE_I18N -static reg_errcode_t -internal_function -transit_state_mb (re_match_context_t *mctx, re_dfastate_t *pstate) -{ - const re_dfa_t *const dfa = mctx->dfa; - reg_errcode_t err; - int i; - - for (i = 0; i < pstate->nodes.nelem; ++i) - { - re_node_set dest_nodes, *new_nodes; - int cur_node_idx = pstate->nodes.elems[i]; - int naccepted, dest_idx; - unsigned int context; - re_dfastate_t *dest_state; - - if (!dfa->nodes[cur_node_idx].accept_mb) - continue; - - if (dfa->nodes[cur_node_idx].constraint) - { - context = re_string_context_at (&mctx->input, - re_string_cur_idx (&mctx->input), - mctx->eflags); - if (NOT_SATISFY_NEXT_CONSTRAINT (dfa->nodes[cur_node_idx].constraint, - context)) - continue; - } - - /* How many bytes the node can accept? */ - naccepted = check_node_accept_bytes (dfa, cur_node_idx, &mctx->input, - re_string_cur_idx (&mctx->input)); - if (naccepted == 0) - continue; - - /* The node can accepts `naccepted' bytes. */ - dest_idx = re_string_cur_idx (&mctx->input) + naccepted; - mctx->max_mb_elem_len = ((mctx->max_mb_elem_len < naccepted) ? naccepted - : mctx->max_mb_elem_len); - err = clean_state_log_if_needed (mctx, dest_idx); - if (BE (err != REG_NOERROR, 0)) - return err; -#ifdef DEBUG - assert (dfa->nexts[cur_node_idx] != -1); -#endif - new_nodes = dfa->eclosures + dfa->nexts[cur_node_idx]; - - dest_state = mctx->state_log[dest_idx]; - if (dest_state == NULL) - dest_nodes = *new_nodes; - else - { - err = re_node_set_init_union (&dest_nodes, - dest_state->entrance_nodes, new_nodes); - if (BE (err != REG_NOERROR, 0)) - return err; - } - context = re_string_context_at (&mctx->input, dest_idx - 1, - mctx->eflags); - mctx->state_log[dest_idx] - = re_acquire_state_context (&err, dfa, &dest_nodes, context); - if (dest_state != NULL) - re_node_set_free (&dest_nodes); - if (BE (mctx->state_log[dest_idx] == NULL && err != REG_NOERROR, 0)) - return err; - } - return REG_NOERROR; -} -#endif /* RE_ENABLE_I18N */ - -static reg_errcode_t -internal_function -transit_state_bkref (re_match_context_t *mctx, const re_node_set *nodes) -{ - const re_dfa_t *const dfa = mctx->dfa; - reg_errcode_t err; - int i; - int cur_str_idx = re_string_cur_idx (&mctx->input); - - for (i = 0; i < nodes->nelem; ++i) - { - int dest_str_idx, prev_nelem, bkc_idx; - int node_idx = nodes->elems[i]; - unsigned int context; - const re_token_t *node = dfa->nodes + node_idx; - re_node_set *new_dest_nodes; - - /* Check whether `node' is a backreference or not. */ - if (node->type != OP_BACK_REF) - continue; - - if (node->constraint) - { - context = re_string_context_at (&mctx->input, cur_str_idx, - mctx->eflags); - if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context)) - continue; - } - - /* `node' is a backreference. - Check the substring which the substring matched. */ - bkc_idx = mctx->nbkref_ents; - err = get_subexp (mctx, node_idx, cur_str_idx); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - - /* And add the epsilon closures (which is `new_dest_nodes') of - the backreference to appropriate state_log. */ -#ifdef DEBUG - assert (dfa->nexts[node_idx] != -1); -#endif - for (; bkc_idx < mctx->nbkref_ents; ++bkc_idx) - { - int subexp_len; - re_dfastate_t *dest_state; - struct re_backref_cache_entry *bkref_ent; - bkref_ent = mctx->bkref_ents + bkc_idx; - if (bkref_ent->node != node_idx || bkref_ent->str_idx != cur_str_idx) - continue; - subexp_len = bkref_ent->subexp_to - bkref_ent->subexp_from; - new_dest_nodes = (subexp_len == 0 - ? dfa->eclosures + dfa->edests[node_idx].elems[0] - : dfa->eclosures + dfa->nexts[node_idx]); - dest_str_idx = (cur_str_idx + bkref_ent->subexp_to - - bkref_ent->subexp_from); - context = re_string_context_at (&mctx->input, dest_str_idx - 1, - mctx->eflags); - dest_state = mctx->state_log[dest_str_idx]; - prev_nelem = ((mctx->state_log[cur_str_idx] == NULL) ? 0 - : mctx->state_log[cur_str_idx]->nodes.nelem); - /* Add `new_dest_node' to state_log. */ - if (dest_state == NULL) - { - mctx->state_log[dest_str_idx] - = re_acquire_state_context (&err, dfa, new_dest_nodes, - context); - if (BE (mctx->state_log[dest_str_idx] == NULL - && err != REG_NOERROR, 0)) - goto free_return; - } - else - { - re_node_set dest_nodes; - err = re_node_set_init_union (&dest_nodes, - dest_state->entrance_nodes, - new_dest_nodes); - if (BE (err != REG_NOERROR, 0)) - { - re_node_set_free (&dest_nodes); - goto free_return; - } - mctx->state_log[dest_str_idx] - = re_acquire_state_context (&err, dfa, &dest_nodes, context); - re_node_set_free (&dest_nodes); - if (BE (mctx->state_log[dest_str_idx] == NULL - && err != REG_NOERROR, 0)) - goto free_return; - } - /* We need to check recursively if the backreference can epsilon - transit. */ - if (subexp_len == 0 - && mctx->state_log[cur_str_idx]->nodes.nelem > prev_nelem) - { - err = check_subexp_matching_top (mctx, new_dest_nodes, - cur_str_idx); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - err = transit_state_bkref (mctx, new_dest_nodes); - if (BE (err != REG_NOERROR, 0)) - goto free_return; - } - } - } - err = REG_NOERROR; - free_return: - return err; -} - -/* Enumerate all the candidates which the backreference BKREF_NODE can match - at BKREF_STR_IDX, and register them by match_ctx_add_entry(). - Note that we might collect inappropriate candidates here. - However, the cost of checking them strictly here is too high, then we - delay these checking for prune_impossible_nodes(). */ - -static reg_errcode_t -internal_function -get_subexp (re_match_context_t *mctx, int bkref_node, int bkref_str_idx) -{ - const re_dfa_t *const dfa = mctx->dfa; - int subexp_num, sub_top_idx; - const char *buf = (const char *) re_string_get_buffer (&mctx->input); - /* Return if we have already checked BKREF_NODE at BKREF_STR_IDX. */ - int cache_idx = search_cur_bkref_entry (mctx, bkref_str_idx); - if (cache_idx != -1) - { - const struct re_backref_cache_entry *entry - = mctx->bkref_ents + cache_idx; - do - if (entry->node == bkref_node) - return REG_NOERROR; /* We already checked it. */ - while (entry++->more); - } - - subexp_num = dfa->nodes[bkref_node].opr.idx; - - /* For each sub expression */ - for (sub_top_idx = 0; sub_top_idx < mctx->nsub_tops; ++sub_top_idx) - { - reg_errcode_t err; - re_sub_match_top_t *sub_top = mctx->sub_tops[sub_top_idx]; - re_sub_match_last_t *sub_last; - int sub_last_idx, sl_str, bkref_str_off; - - if (dfa->nodes[sub_top->node].opr.idx != subexp_num) - continue; /* It isn't related. */ - - sl_str = sub_top->str_idx; - bkref_str_off = bkref_str_idx; - /* At first, check the last node of sub expressions we already - evaluated. */ - for (sub_last_idx = 0; sub_last_idx < sub_top->nlasts; ++sub_last_idx) - { - int sl_str_diff; - sub_last = sub_top->lasts[sub_last_idx]; - sl_str_diff = sub_last->str_idx - sl_str; - /* The matched string by the sub expression match with the substring - at the back reference? */ - if (sl_str_diff > 0) - { - if (BE (bkref_str_off + sl_str_diff > mctx->input.valid_len, 0)) - { - /* Not enough chars for a successful match. */ - if (bkref_str_off + sl_str_diff > mctx->input.len) - break; - - err = clean_state_log_if_needed (mctx, - bkref_str_off - + sl_str_diff); - if (BE (err != REG_NOERROR, 0)) - return err; - buf = (const char *) re_string_get_buffer (&mctx->input); - } - if (memcmp (buf + bkref_str_off, buf + sl_str, sl_str_diff) != 0) - /* We don't need to search this sub expression any more. */ - break; - } - bkref_str_off += sl_str_diff; - sl_str += sl_str_diff; - err = get_subexp_sub (mctx, sub_top, sub_last, bkref_node, - bkref_str_idx); - - /* Reload buf, since the preceding call might have reallocated - the buffer. */ - buf = (const char *) re_string_get_buffer (&mctx->input); - - if (err == REG_NOMATCH) - continue; - if (BE (err != REG_NOERROR, 0)) - return err; - } - - if (sub_last_idx < sub_top->nlasts) - continue; - if (sub_last_idx > 0) - ++sl_str; - /* Then, search for the other last nodes of the sub expression. */ - for (; sl_str <= bkref_str_idx; ++sl_str) - { - int cls_node, sl_str_off; - const re_node_set *nodes; - sl_str_off = sl_str - sub_top->str_idx; - /* The matched string by the sub expression match with the substring - at the back reference? */ - if (sl_str_off > 0) - { - if (BE (bkref_str_off >= mctx->input.valid_len, 0)) - { - /* If we are at the end of the input, we cannot match. */ - if (bkref_str_off >= mctx->input.len) - break; - - err = extend_buffers (mctx); - if (BE (err != REG_NOERROR, 0)) - return err; - - buf = (const char *) re_string_get_buffer (&mctx->input); - } - if (buf [bkref_str_off++] != buf[sl_str - 1]) - break; /* We don't need to search this sub expression - any more. */ - } - if (mctx->state_log[sl_str] == NULL) - continue; - /* Does this state have a ')' of the sub expression? */ - nodes = &mctx->state_log[sl_str]->nodes; - cls_node = find_subexp_node (dfa, nodes, subexp_num, - OP_CLOSE_SUBEXP); - if (cls_node == -1) - continue; /* No. */ - if (sub_top->path == NULL) - { - sub_top->path = calloc (sizeof (state_array_t), - sl_str - sub_top->str_idx + 1); - if (sub_top->path == NULL) - return REG_ESPACE; - } - /* Can the OP_OPEN_SUBEXP node arrive the OP_CLOSE_SUBEXP node - in the current context? */ - err = check_arrival (mctx, sub_top->path, sub_top->node, - sub_top->str_idx, cls_node, sl_str, - OP_CLOSE_SUBEXP); - if (err == REG_NOMATCH) - continue; - if (BE (err != REG_NOERROR, 0)) - return err; - sub_last = match_ctx_add_sublast (sub_top, cls_node, sl_str); - if (BE (sub_last == NULL, 0)) - return REG_ESPACE; - err = get_subexp_sub (mctx, sub_top, sub_last, bkref_node, - bkref_str_idx); - if (err == REG_NOMATCH) - continue; - } - } - return REG_NOERROR; -} - -/* Helper functions for get_subexp(). */ - -/* Check SUB_LAST can arrive to the back reference BKREF_NODE at BKREF_STR. - If it can arrive, register the sub expression expressed with SUB_TOP - and SUB_LAST. */ - -static reg_errcode_t -internal_function -get_subexp_sub (re_match_context_t *mctx, const re_sub_match_top_t *sub_top, - re_sub_match_last_t *sub_last, int bkref_node, int bkref_str) -{ - reg_errcode_t err; - int to_idx; - /* Can the subexpression arrive the back reference? */ - err = check_arrival (mctx, &sub_last->path, sub_last->node, - sub_last->str_idx, bkref_node, bkref_str, - OP_OPEN_SUBEXP); - if (err != REG_NOERROR) - return err; - err = match_ctx_add_entry (mctx, bkref_node, bkref_str, sub_top->str_idx, - sub_last->str_idx); - if (BE (err != REG_NOERROR, 0)) - return err; - to_idx = bkref_str + sub_last->str_idx - sub_top->str_idx; - return clean_state_log_if_needed (mctx, to_idx); -} - -/* Find the first node which is '(' or ')' and whose index is SUBEXP_IDX. - Search '(' if FL_OPEN, or search ')' otherwise. - TODO: This function isn't efficient... - Because there might be more than one nodes whose types are - OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all - nodes. - E.g. RE: (a){2} */ - -static int -internal_function -find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes, - int subexp_idx, int type) -{ - int cls_idx; - for (cls_idx = 0; cls_idx < nodes->nelem; ++cls_idx) - { - int cls_node = nodes->elems[cls_idx]; - const re_token_t *node = dfa->nodes + cls_node; - if (node->type == type - && node->opr.idx == subexp_idx) - return cls_node; - } - return -1; -} - -/* Check whether the node TOP_NODE at TOP_STR can arrive to the node - LAST_NODE at LAST_STR. We record the path onto PATH since it will be - heavily reused. - Return REG_NOERROR if it can arrive, or REG_NOMATCH otherwise. */ - -static reg_errcode_t -internal_function -check_arrival (re_match_context_t *mctx, state_array_t *path, int top_node, - int top_str, int last_node, int last_str, int type) -{ - const re_dfa_t *const dfa = mctx->dfa; - reg_errcode_t err = REG_NOERROR; - int subexp_num, backup_cur_idx, str_idx, null_cnt; - re_dfastate_t *cur_state = NULL; - re_node_set *cur_nodes, next_nodes; - re_dfastate_t **backup_state_log; - unsigned int context; - - subexp_num = dfa->nodes[top_node].opr.idx; - /* Extend the buffer if we need. */ - if (BE (path->alloc < last_str + mctx->max_mb_elem_len + 1, 0)) - { - re_dfastate_t **new_array; - int old_alloc = path->alloc; - path->alloc += last_str + mctx->max_mb_elem_len + 1; - new_array = re_realloc (path->array, re_dfastate_t *, path->alloc); - if (BE (new_array == NULL, 0)) - { - path->alloc = old_alloc; - return REG_ESPACE; - } - path->array = new_array; - memset (new_array + old_alloc, '\0', - sizeof (re_dfastate_t *) * (path->alloc - old_alloc)); - } - - str_idx = path->next_idx ? path->next_idx : top_str; - - /* Temporary modify MCTX. */ - backup_state_log = mctx->state_log; - backup_cur_idx = mctx->input.cur_idx; - mctx->state_log = path->array; - mctx->input.cur_idx = str_idx; - - /* Setup initial node set. */ - context = re_string_context_at (&mctx->input, str_idx - 1, mctx->eflags); - if (str_idx == top_str) - { - err = re_node_set_init_1 (&next_nodes, top_node); - if (BE (err != REG_NOERROR, 0)) - return err; - err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type); - if (BE (err != REG_NOERROR, 0)) - { - re_node_set_free (&next_nodes); - return err; - } - } - else - { - cur_state = mctx->state_log[str_idx]; - if (cur_state && cur_state->has_backref) - { - err = re_node_set_init_copy (&next_nodes, &cur_state->nodes); - if (BE (err != REG_NOERROR, 0)) - return err; - } - else - re_node_set_init_empty (&next_nodes); - } - if (str_idx == top_str || (cur_state && cur_state->has_backref)) - { - if (next_nodes.nelem) - { - err = expand_bkref_cache (mctx, &next_nodes, str_idx, - subexp_num, type); - if (BE (err != REG_NOERROR, 0)) - { - re_node_set_free (&next_nodes); - return err; - } - } - cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context); - if (BE (cur_state == NULL && err != REG_NOERROR, 0)) - { - re_node_set_free (&next_nodes); - return err; - } - mctx->state_log[str_idx] = cur_state; - } - - for (null_cnt = 0; str_idx < last_str && null_cnt <= mctx->max_mb_elem_len;) - { - re_node_set_empty (&next_nodes); - if (mctx->state_log[str_idx + 1]) - { - err = re_node_set_merge (&next_nodes, - &mctx->state_log[str_idx + 1]->nodes); - if (BE (err != REG_NOERROR, 0)) - { - re_node_set_free (&next_nodes); - return err; - } - } - if (cur_state) - { - err = check_arrival_add_next_nodes (mctx, str_idx, - &cur_state->non_eps_nodes, - &next_nodes); - if (BE (err != REG_NOERROR, 0)) - { - re_node_set_free (&next_nodes); - return err; - } - } - ++str_idx; - if (next_nodes.nelem) - { - err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type); - if (BE (err != REG_NOERROR, 0)) - { - re_node_set_free (&next_nodes); - return err; - } - err = expand_bkref_cache (mctx, &next_nodes, str_idx, - subexp_num, type); - if (BE (err != REG_NOERROR, 0)) - { - re_node_set_free (&next_nodes); - return err; - } - } - context = re_string_context_at (&mctx->input, str_idx - 1, mctx->eflags); - cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context); - if (BE (cur_state == NULL && err != REG_NOERROR, 0)) - { - re_node_set_free (&next_nodes); - return err; - } - mctx->state_log[str_idx] = cur_state; - null_cnt = cur_state == NULL ? null_cnt + 1 : 0; - } - re_node_set_free (&next_nodes); - cur_nodes = (mctx->state_log[last_str] == NULL ? NULL - : &mctx->state_log[last_str]->nodes); - path->next_idx = str_idx; - - /* Fix MCTX. */ - mctx->state_log = backup_state_log; - mctx->input.cur_idx = backup_cur_idx; - - /* Then check the current node set has the node LAST_NODE. */ - if (cur_nodes != NULL && re_node_set_contains (cur_nodes, last_node)) - return REG_NOERROR; - - return REG_NOMATCH; -} - -/* Helper functions for check_arrival. */ - -/* Calculate the destination nodes of CUR_NODES at STR_IDX, and append them - to NEXT_NODES. - TODO: This function is similar to the functions transit_state*(), - however this function has many additional works. - Can't we unify them? */ - -static reg_errcode_t -internal_function -check_arrival_add_next_nodes (re_match_context_t *mctx, int str_idx, - re_node_set *cur_nodes, re_node_set *next_nodes) -{ - const re_dfa_t *const dfa = mctx->dfa; - int result; - int cur_idx; -#ifdef RE_ENABLE_I18N - reg_errcode_t err = REG_NOERROR; -#endif - re_node_set union_set; - re_node_set_init_empty (&union_set); - for (cur_idx = 0; cur_idx < cur_nodes->nelem; ++cur_idx) - { - int naccepted = 0; - int cur_node = cur_nodes->elems[cur_idx]; -#ifdef DEBUG - re_token_type_t type = dfa->nodes[cur_node].type; - assert (!IS_EPSILON_NODE (type)); -#endif -#ifdef RE_ENABLE_I18N - /* If the node may accept `multi byte'. */ - if (dfa->nodes[cur_node].accept_mb) - { - naccepted = check_node_accept_bytes (dfa, cur_node, &mctx->input, - str_idx); - if (naccepted > 1) - { - re_dfastate_t *dest_state; - int next_node = dfa->nexts[cur_node]; - int next_idx = str_idx + naccepted; - dest_state = mctx->state_log[next_idx]; - re_node_set_empty (&union_set); - if (dest_state) - { - err = re_node_set_merge (&union_set, &dest_state->nodes); - if (BE (err != REG_NOERROR, 0)) - { - re_node_set_free (&union_set); - return err; - } - } - result = re_node_set_insert (&union_set, next_node); - if (BE (result < 0, 0)) - { - re_node_set_free (&union_set); - return REG_ESPACE; - } - mctx->state_log[next_idx] = re_acquire_state (&err, dfa, - &union_set); - if (BE (mctx->state_log[next_idx] == NULL - && err != REG_NOERROR, 0)) - { - re_node_set_free (&union_set); - return err; - } - } - } -#endif /* RE_ENABLE_I18N */ - if (naccepted - || check_node_accept (mctx, dfa->nodes + cur_node, str_idx)) - { - result = re_node_set_insert (next_nodes, dfa->nexts[cur_node]); - if (BE (result < 0, 0)) - { - re_node_set_free (&union_set); - return REG_ESPACE; - } - } - } - re_node_set_free (&union_set); - return REG_NOERROR; -} - -/* For all the nodes in CUR_NODES, add the epsilon closures of them to - CUR_NODES, however exclude the nodes which are: - - inside the sub expression whose number is EX_SUBEXP, if FL_OPEN. - - out of the sub expression whose number is EX_SUBEXP, if !FL_OPEN. -*/ - -static reg_errcode_t -internal_function -check_arrival_expand_ecl (const re_dfa_t *dfa, re_node_set *cur_nodes, - int ex_subexp, int type) -{ - reg_errcode_t err; - int idx, outside_node; - re_node_set new_nodes; -#ifdef DEBUG - assert (cur_nodes->nelem); -#endif - err = re_node_set_alloc (&new_nodes, cur_nodes->nelem); - if (BE (err != REG_NOERROR, 0)) - return err; - /* Create a new node set NEW_NODES with the nodes which are epsilon - closures of the node in CUR_NODES. */ - - for (idx = 0; idx < cur_nodes->nelem; ++idx) - { - int cur_node = cur_nodes->elems[idx]; - const re_node_set *eclosure = dfa->eclosures + cur_node; - outside_node = find_subexp_node (dfa, eclosure, ex_subexp, type); - if (outside_node == -1) - { - /* There are no problematic nodes, just merge them. */ - err = re_node_set_merge (&new_nodes, eclosure); - if (BE (err != REG_NOERROR, 0)) - { - re_node_set_free (&new_nodes); - return err; - } - } - else - { - /* There are problematic nodes, re-calculate incrementally. */ - err = check_arrival_expand_ecl_sub (dfa, &new_nodes, cur_node, - ex_subexp, type); - if (BE (err != REG_NOERROR, 0)) - { - re_node_set_free (&new_nodes); - return err; - } - } - } - re_node_set_free (cur_nodes); - *cur_nodes = new_nodes; - return REG_NOERROR; -} - -/* Helper function for check_arrival_expand_ecl. - Check incrementally the epsilon closure of TARGET, and if it isn't - problematic append it to DST_NODES. */ - -static reg_errcode_t -internal_function -check_arrival_expand_ecl_sub (const re_dfa_t *dfa, re_node_set *dst_nodes, - int target, int ex_subexp, int type) -{ - int cur_node; - for (cur_node = target; !re_node_set_contains (dst_nodes, cur_node);) - { - int err; - - if (dfa->nodes[cur_node].type == type - && dfa->nodes[cur_node].opr.idx == ex_subexp) - { - if (type == OP_CLOSE_SUBEXP) - { - err = re_node_set_insert (dst_nodes, cur_node); - if (BE (err == -1, 0)) - return REG_ESPACE; - } - break; - } - err = re_node_set_insert (dst_nodes, cur_node); - if (BE (err == -1, 0)) - return REG_ESPACE; - if (dfa->edests[cur_node].nelem == 0) - break; - if (dfa->edests[cur_node].nelem == 2) - { - err = check_arrival_expand_ecl_sub (dfa, dst_nodes, - dfa->edests[cur_node].elems[1], - ex_subexp, type); - if (BE (err != REG_NOERROR, 0)) - return err; - } - cur_node = dfa->edests[cur_node].elems[0]; - } - return REG_NOERROR; -} - - -/* For all the back references in the current state, calculate the - destination of the back references by the appropriate entry - in MCTX->BKREF_ENTS. */ - -static reg_errcode_t -internal_function -expand_bkref_cache (re_match_context_t *mctx, re_node_set *cur_nodes, - int cur_str, int subexp_num, int type) -{ - const re_dfa_t *const dfa = mctx->dfa; - reg_errcode_t err; - int cache_idx_start = search_cur_bkref_entry (mctx, cur_str); - struct re_backref_cache_entry *ent; - - if (cache_idx_start == -1) - return REG_NOERROR; - - restart: - ent = mctx->bkref_ents + cache_idx_start; - do - { - int to_idx, next_node; - - /* Is this entry ENT is appropriate? */ - if (!re_node_set_contains (cur_nodes, ent->node)) - continue; /* No. */ - - to_idx = cur_str + ent->subexp_to - ent->subexp_from; - /* Calculate the destination of the back reference, and append it - to MCTX->STATE_LOG. */ - if (to_idx == cur_str) - { - /* The backreference did epsilon transit, we must re-check all the - node in the current state. */ - re_node_set new_dests; - reg_errcode_t err2, err3; - next_node = dfa->edests[ent->node].elems[0]; - if (re_node_set_contains (cur_nodes, next_node)) - continue; - err = re_node_set_init_1 (&new_dests, next_node); - err2 = check_arrival_expand_ecl (dfa, &new_dests, subexp_num, type); - err3 = re_node_set_merge (cur_nodes, &new_dests); - re_node_set_free (&new_dests); - if (BE (err != REG_NOERROR || err2 != REG_NOERROR - || err3 != REG_NOERROR, 0)) - { - err = (err != REG_NOERROR ? err - : (err2 != REG_NOERROR ? err2 : err3)); - return err; - } - /* TODO: It is still inefficient... */ - goto restart; - } - else - { - re_node_set union_set; - next_node = dfa->nexts[ent->node]; - if (mctx->state_log[to_idx]) - { - int ret; - if (re_node_set_contains (&mctx->state_log[to_idx]->nodes, - next_node)) - continue; - err = re_node_set_init_copy (&union_set, - &mctx->state_log[to_idx]->nodes); - ret = re_node_set_insert (&union_set, next_node); - if (BE (err != REG_NOERROR || ret < 0, 0)) - { - re_node_set_free (&union_set); - err = err != REG_NOERROR ? err : REG_ESPACE; - return err; - } - } - else - { - err = re_node_set_init_1 (&union_set, next_node); - if (BE (err != REG_NOERROR, 0)) - return err; - } - mctx->state_log[to_idx] = re_acquire_state (&err, dfa, &union_set); - re_node_set_free (&union_set); - if (BE (mctx->state_log[to_idx] == NULL - && err != REG_NOERROR, 0)) - return err; - } - } - while (ent++->more); - return REG_NOERROR; -} - -/* Build transition table for the state. - Return 1 if succeeded, otherwise return NULL. */ - -static int -internal_function -build_trtable (const re_dfa_t *dfa, re_dfastate_t *state) -{ - reg_errcode_t err; - int i, j, ch, need_word_trtable = 0; - bitset_word_t elem, mask; - bool dests_node_malloced = false; - bool dest_states_malloced = false; - int ndests; /* Number of the destination states from `state'. */ - re_dfastate_t **trtable; - re_dfastate_t **dest_states = NULL, **dest_states_word, **dest_states_nl; - re_node_set follows, *dests_node; - bitset_t *dests_ch; - bitset_t acceptable; - - struct dests_alloc - { - re_node_set dests_node[SBC_MAX]; - bitset_t dests_ch[SBC_MAX]; - } *dests_alloc; - - /* We build DFA states which corresponds to the destination nodes - from `state'. `dests_node[i]' represents the nodes which i-th - destination state contains, and `dests_ch[i]' represents the - characters which i-th destination state accepts. */ -#ifdef HAVE_ALLOCA - if (__libc_use_alloca (sizeof (struct dests_alloc))) - dests_alloc = (struct dests_alloc *) alloca (sizeof (struct dests_alloc)); - else -#endif - { - dests_alloc = re_malloc (struct dests_alloc, 1); - if (BE (dests_alloc == NULL, 0)) - return 0; - dests_node_malloced = true; - } - dests_node = dests_alloc->dests_node; - dests_ch = dests_alloc->dests_ch; - - /* Initialize transiton table. */ - state->word_trtable = state->trtable = NULL; - - /* At first, group all nodes belonging to `state' into several - destinations. */ - ndests = group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch); - if (BE (ndests <= 0, 0)) - { - if (dests_node_malloced) - free (dests_alloc); - /* Return 0 in case of an error, 1 otherwise. */ - if (ndests == 0) - { - state->trtable = (re_dfastate_t **) - calloc (sizeof (re_dfastate_t *), SBC_MAX); - return 1; - } - return 0; - } - - err = re_node_set_alloc (&follows, ndests + 1); - if (BE (err != REG_NOERROR, 0)) - goto out_free; - - /* Avoid arithmetic overflow in size calculation. */ - if (BE ((((SIZE_MAX - (sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX) - / (3 * sizeof (re_dfastate_t *))) - < (size_t)ndests), - 0)) - goto out_free; - -#ifdef HAVE_ALLOCA - if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX - + ndests * 3 * sizeof (re_dfastate_t *))) - dest_states = (re_dfastate_t **) - alloca (ndests * 3 * sizeof (re_dfastate_t *)); - else -#endif - { - dest_states = (re_dfastate_t **) - malloc (ndests * 3 * sizeof (re_dfastate_t *)); - if (BE (dest_states == NULL, 0)) - { -out_free: - if (dest_states_malloced) - free (dest_states); - re_node_set_free (&follows); - for (i = 0; i < ndests; ++i) - re_node_set_free (dests_node + i); - if (dests_node_malloced) - free (dests_alloc); - return 0; - } - dest_states_malloced = true; - } - dest_states_word = dest_states + ndests; - dest_states_nl = dest_states_word + ndests; - bitset_empty (acceptable); - - /* Then build the states for all destinations. */ - for (i = 0; i < ndests; ++i) - { - int next_node; - re_node_set_empty (&follows); - /* Merge the follows of this destination states. */ - for (j = 0; j < dests_node[i].nelem; ++j) - { - next_node = dfa->nexts[dests_node[i].elems[j]]; - if (next_node != -1) - { - err = re_node_set_merge (&follows, dfa->eclosures + next_node); - if (BE (err != REG_NOERROR, 0)) - goto out_free; - } - } - dest_states[i] = re_acquire_state_context (&err, dfa, &follows, 0); - if (BE (dest_states[i] == NULL && err != REG_NOERROR, 0)) - goto out_free; - /* If the new state has context constraint, - build appropriate states for these contexts. */ - if (dest_states[i]->has_constraint) - { - dest_states_word[i] = re_acquire_state_context (&err, dfa, &follows, - CONTEXT_WORD); - if (BE (dest_states_word[i] == NULL && err != REG_NOERROR, 0)) - goto out_free; - - if (dest_states[i] != dest_states_word[i] && dfa->mb_cur_max > 1) - need_word_trtable = 1; - - dest_states_nl[i] = re_acquire_state_context (&err, dfa, &follows, - CONTEXT_NEWLINE); - if (BE (dest_states_nl[i] == NULL && err != REG_NOERROR, 0)) - goto out_free; - } - else - { - dest_states_word[i] = dest_states[i]; - dest_states_nl[i] = dest_states[i]; - } - bitset_merge (acceptable, dests_ch[i]); - } - - if (!BE (need_word_trtable, 0)) - { - /* We don't care about whether the following character is a word - character, or we are in a single-byte character set so we can - discern by looking at the character code: allocate a - 256-entry transition table. */ - trtable = state->trtable = - (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), SBC_MAX); - if (BE (trtable == NULL, 0)) - goto out_free; - - /* For all characters ch...: */ - for (i = 0; i < BITSET_WORDS; ++i) - for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1; - elem; - mask <<= 1, elem >>= 1, ++ch) - if (BE (elem & 1, 0)) - { - /* There must be exactly one destination which accepts - character ch. See group_nodes_into_DFAstates. */ - for (j = 0; (dests_ch[j][i] & mask) == 0; ++j) - ; - - /* j-th destination accepts the word character ch. */ - if (dfa->word_char[i] & mask) - trtable[ch] = dest_states_word[j]; - else - trtable[ch] = dest_states[j]; - } - } - else - { - /* We care about whether the following character is a word - character, and we are in a multi-byte character set: discern - by looking at the character code: build two 256-entry - transition tables, one starting at trtable[0] and one - starting at trtable[SBC_MAX]. */ - trtable = state->word_trtable = - (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), 2 * SBC_MAX); - if (BE (trtable == NULL, 0)) - goto out_free; - - /* For all characters ch...: */ - for (i = 0; i < BITSET_WORDS; ++i) - for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1; - elem; - mask <<= 1, elem >>= 1, ++ch) - if (BE (elem & 1, 0)) - { - /* There must be exactly one destination which accepts - character ch. See group_nodes_into_DFAstates. */ - for (j = 0; (dests_ch[j][i] & mask) == 0; ++j) - ; - - /* j-th destination accepts the word character ch. */ - trtable[ch] = dest_states[j]; - trtable[ch + SBC_MAX] = dest_states_word[j]; - } - } - - /* new line */ - if (bitset_contain (acceptable, NEWLINE_CHAR)) - { - /* The current state accepts newline character. */ - for (j = 0; j < ndests; ++j) - if (bitset_contain (dests_ch[j], NEWLINE_CHAR)) - { - /* k-th destination accepts newline character. */ - trtable[NEWLINE_CHAR] = dest_states_nl[j]; - if (need_word_trtable) - trtable[NEWLINE_CHAR + SBC_MAX] = dest_states_nl[j]; - /* There must be only one destination which accepts - newline. See group_nodes_into_DFAstates. */ - break; - } - } - - if (dest_states_malloced) - free (dest_states); - - re_node_set_free (&follows); - for (i = 0; i < ndests; ++i) - re_node_set_free (dests_node + i); - - if (dests_node_malloced) - free (dests_alloc); - - return 1; -} - -/* Group all nodes belonging to STATE into several destinations. - Then for all destinations, set the nodes belonging to the destination - to DESTS_NODE[i] and set the characters accepted by the destination - to DEST_CH[i]. This function return the number of destinations. */ - -static int -internal_function -group_nodes_into_DFAstates (const re_dfa_t *dfa, const re_dfastate_t *state, - re_node_set *dests_node, bitset_t *dests_ch) -{ - reg_errcode_t err; - int result; - int i, j, k; - int ndests; /* Number of the destinations from `state'. */ - bitset_t accepts; /* Characters a node can accept. */ - const re_node_set *cur_nodes = &state->nodes; - bitset_empty (accepts); - ndests = 0; - - /* For all the nodes belonging to `state', */ - for (i = 0; i < cur_nodes->nelem; ++i) - { - re_token_t *node = &dfa->nodes[cur_nodes->elems[i]]; - re_token_type_t type = node->type; - unsigned int constraint = node->constraint; - - /* Enumerate all single byte character this node can accept. */ - if (type == CHARACTER) - bitset_set (accepts, node->opr.c); - else if (type == SIMPLE_BRACKET) - { - bitset_merge (accepts, node->opr.sbcset); - } - else if (type == OP_PERIOD) - { -#ifdef RE_ENABLE_I18N - if (dfa->mb_cur_max > 1) - bitset_merge (accepts, dfa->sb_char); - else -#endif - bitset_set_all (accepts); - if (!(dfa->syntax & RE_DOT_NEWLINE)) - bitset_clear (accepts, '\n'); - if (dfa->syntax & RE_DOT_NOT_NULL) - bitset_clear (accepts, '\0'); - } -#ifdef RE_ENABLE_I18N - else if (type == OP_UTF8_PERIOD) - { - memset (accepts, '\xff', sizeof (bitset_t) / 2); - if (!(dfa->syntax & RE_DOT_NEWLINE)) - bitset_clear (accepts, '\n'); - if (dfa->syntax & RE_DOT_NOT_NULL) - bitset_clear (accepts, '\0'); - } -#endif - else - continue; - - /* Check the `accepts' and sift the characters which are not - match it the context. */ - if (constraint) - { - if (constraint & NEXT_NEWLINE_CONSTRAINT) - { - bool accepts_newline = bitset_contain (accepts, NEWLINE_CHAR); - bitset_empty (accepts); - if (accepts_newline) - bitset_set (accepts, NEWLINE_CHAR); - else - continue; - } - if (constraint & NEXT_ENDBUF_CONSTRAINT) - { - bitset_empty (accepts); - continue; - } - - if (constraint & NEXT_WORD_CONSTRAINT) - { - bitset_word_t any_set = 0; - if (type == CHARACTER && !node->word_char) - { - bitset_empty (accepts); - continue; - } -#ifdef RE_ENABLE_I18N - if (dfa->mb_cur_max > 1) - for (j = 0; j < BITSET_WORDS; ++j) - any_set |= (accepts[j] &= (dfa->word_char[j] | ~dfa->sb_char[j])); - else -#endif - for (j = 0; j < BITSET_WORDS; ++j) - any_set |= (accepts[j] &= dfa->word_char[j]); - if (!any_set) - continue; - } - if (constraint & NEXT_NOTWORD_CONSTRAINT) - { - bitset_word_t any_set = 0; - if (type == CHARACTER && node->word_char) - { - bitset_empty (accepts); - continue; - } -#ifdef RE_ENABLE_I18N - if (dfa->mb_cur_max > 1) - for (j = 0; j < BITSET_WORDS; ++j) - any_set |= (accepts[j] &= ~(dfa->word_char[j] & dfa->sb_char[j])); - else -#endif - for (j = 0; j < BITSET_WORDS; ++j) - any_set |= (accepts[j] &= ~dfa->word_char[j]); - if (!any_set) - continue; - } - } - - /* Then divide `accepts' into DFA states, or create a new - state. Above, we make sure that accepts is not empty. */ - for (j = 0; j < ndests; ++j) - { - bitset_t intersec; /* Intersection sets, see below. */ - bitset_t remains; - /* Flags, see below. */ - bitset_word_t has_intersec, not_subset, not_consumed; - - /* Optimization, skip if this state doesn't accept the character. */ - if (type == CHARACTER && !bitset_contain (dests_ch[j], node->opr.c)) - continue; - - /* Enumerate the intersection set of this state and `accepts'. */ - has_intersec = 0; - for (k = 0; k < BITSET_WORDS; ++k) - has_intersec |= intersec[k] = accepts[k] & dests_ch[j][k]; - /* And skip if the intersection set is empty. */ - if (!has_intersec) - continue; - - /* Then check if this state is a subset of `accepts'. */ - not_subset = not_consumed = 0; - for (k = 0; k < BITSET_WORDS; ++k) - { - not_subset |= remains[k] = ~accepts[k] & dests_ch[j][k]; - not_consumed |= accepts[k] = accepts[k] & ~dests_ch[j][k]; - } - - /* If this state isn't a subset of `accepts', create a - new group state, which has the `remains'. */ - if (not_subset) - { - bitset_copy (dests_ch[ndests], remains); - bitset_copy (dests_ch[j], intersec); - err = re_node_set_init_copy (dests_node + ndests, &dests_node[j]); - if (BE (err != REG_NOERROR, 0)) - goto error_return; - ++ndests; - } - - /* Put the position in the current group. */ - result = re_node_set_insert (&dests_node[j], cur_nodes->elems[i]); - if (BE (result < 0, 0)) - goto error_return; - - /* If all characters are consumed, go to next node. */ - if (!not_consumed) - break; - } - /* Some characters remain, create a new group. */ - if (j == ndests) - { - bitset_copy (dests_ch[ndests], accepts); - err = re_node_set_init_1 (dests_node + ndests, cur_nodes->elems[i]); - if (BE (err != REG_NOERROR, 0)) - goto error_return; - ++ndests; - bitset_empty (accepts); - } - } - return ndests; - error_return: - for (j = 0; j < ndests; ++j) - re_node_set_free (dests_node + j); - return -1; -} - -#ifdef RE_ENABLE_I18N -/* Check how many bytes the node `dfa->nodes[node_idx]' accepts. - Return the number of the bytes the node accepts. - STR_IDX is the current index of the input string. - - This function handles the nodes which can accept one character, or - one collating element like '.', '[a-z]', opposite to the other nodes - can only accept one byte. */ - -static int -internal_function -check_node_accept_bytes (const re_dfa_t *dfa, int node_idx, - const re_string_t *input, int str_idx) -{ - const re_token_t *node = dfa->nodes + node_idx; - int char_len, elem_len; - int i; - wint_t wc; - - if (BE (node->type == OP_UTF8_PERIOD, 0)) - { - unsigned char c = re_string_byte_at (input, str_idx), d; - if (BE (c < 0xc2, 1)) - return 0; - - if (str_idx + 2 > input->len) - return 0; - - d = re_string_byte_at (input, str_idx + 1); - if (c < 0xe0) - return (d < 0x80 || d > 0xbf) ? 0 : 2; - else if (c < 0xf0) - { - char_len = 3; - if (c == 0xe0 && d < 0xa0) - return 0; - } - else if (c < 0xf8) - { - char_len = 4; - if (c == 0xf0 && d < 0x90) - return 0; - } - else if (c < 0xfc) - { - char_len = 5; - if (c == 0xf8 && d < 0x88) - return 0; - } - else if (c < 0xfe) - { - char_len = 6; - if (c == 0xfc && d < 0x84) - return 0; - } - else - return 0; - - if (str_idx + char_len > input->len) - return 0; - - for (i = 1; i < char_len; ++i) - { - d = re_string_byte_at (input, str_idx + i); - if (d < 0x80 || d > 0xbf) - return 0; - } - return char_len; - } - - char_len = re_string_char_size_at (input, str_idx); - if (node->type == OP_PERIOD) - { - if (char_len <= 1) - return 0; - /* FIXME: I don't think this if is needed, as both '\n' - and '\0' are char_len == 1. */ - /* '.' accepts any one character except the following two cases. */ - if ((!(dfa->syntax & RE_DOT_NEWLINE) && - re_string_byte_at (input, str_idx) == '\n') || - ((dfa->syntax & RE_DOT_NOT_NULL) && - re_string_byte_at (input, str_idx) == '\0')) - return 0; - return char_len; - } - - elem_len = re_string_elem_size_at (input, str_idx); - wc = __btowc(*(input->mbs+str_idx)); - if (((elem_len <= 1 && char_len <= 1) || char_len == 0) && (wc != WEOF && wc < SBC_MAX)) - return 0; - - if (node->type == COMPLEX_BRACKET) - { - const re_charset_t *cset = node->opr.mbcset; -# ifdef _LIBC - const unsigned char *pin - = ((const unsigned char *) re_string_get_buffer (input) + str_idx); - int j; - uint32_t nrules; -# endif /* _LIBC */ - int match_len = 0; - wchar_t wc = ((cset->nranges || cset->nchar_classes || cset->nmbchars) - ? re_string_wchar_at (input, str_idx) : 0); - - /* match with multibyte character? */ - for (i = 0; i < cset->nmbchars; ++i) - if (wc == cset->mbchars[i]) - { - match_len = char_len; - goto check_node_accept_bytes_match; - } - /* match with character_class? */ - for (i = 0; i < cset->nchar_classes; ++i) - { - wctype_t wt = cset->char_classes[i]; - if (__iswctype (wc, wt)) - { - match_len = char_len; - goto check_node_accept_bytes_match; - } - } - -# ifdef _LIBC - nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES); - if (nrules != 0) - { - unsigned int in_collseq = 0; - const int32_t *table, *indirect; - const unsigned char *weights, *extra; - const char *collseqwc; - /* This #include defines a local function! */ -# include - - /* match with collating_symbol? */ - if (cset->ncoll_syms) - extra = (const unsigned char *) - _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB); - for (i = 0; i < cset->ncoll_syms; ++i) - { - const unsigned char *coll_sym = extra + cset->coll_syms[i]; - /* Compare the length of input collating element and - the length of current collating element. */ - if (*coll_sym != elem_len) - continue; - /* Compare each bytes. */ - for (j = 0; j < *coll_sym; j++) - if (pin[j] != coll_sym[1 + j]) - break; - if (j == *coll_sym) - { - /* Match if every bytes is equal. */ - match_len = j; - goto check_node_accept_bytes_match; - } - } - - if (cset->nranges) - { - if (elem_len <= char_len) - { - collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC); - in_collseq = __collseq_table_lookup (collseqwc, wc); - } - else - in_collseq = find_collation_sequence_value (pin, elem_len); - } - /* match with range expression? */ - for (i = 0; i < cset->nranges; ++i) - if (cset->range_starts[i] <= in_collseq - && in_collseq <= cset->range_ends[i]) - { - match_len = elem_len; - goto check_node_accept_bytes_match; - } - - /* match with equivalence_class? */ - if (cset->nequiv_classes) - { - const unsigned char *cp = pin; - table = (const int32_t *) - _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB); - weights = (const unsigned char *) - _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB); - extra = (const unsigned char *) - _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB); - indirect = (const int32_t *) - _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB); - int32_t idx = findidx (&cp); - if (idx > 0) - for (i = 0; i < cset->nequiv_classes; ++i) - { - int32_t equiv_class_idx = cset->equiv_classes[i]; - size_t weight_len = weights[idx & 0xffffff]; - if (weight_len == weights[equiv_class_idx & 0xffffff] - && (idx >> 24) == (equiv_class_idx >> 24)) - { - int cnt = 0; - - idx &= 0xffffff; - equiv_class_idx &= 0xffffff; - - while (cnt <= weight_len - && (weights[equiv_class_idx + 1 + cnt] - == weights[idx + 1 + cnt])) - ++cnt; - if (cnt > weight_len) - { - match_len = elem_len; - goto check_node_accept_bytes_match; - } - } - } - } - } - else -# endif /* _LIBC */ - { - /* match with range expression? */ -#if __GNUC__ >= 2 - wchar_t cmp_buf[] = {L'\0', L'\0', wc, L'\0', L'\0', L'\0'}; -#else - wchar_t cmp_buf[] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'}; - cmp_buf[2] = wc; -#endif - for (i = 0; i < cset->nranges; ++i) - { - cmp_buf[0] = cset->range_starts[i]; - cmp_buf[4] = cset->range_ends[i]; - if (wcscoll (cmp_buf, cmp_buf + 2) <= 0 - && wcscoll (cmp_buf + 2, cmp_buf + 4) <= 0) - { - match_len = char_len; - goto check_node_accept_bytes_match; - } - } - } - check_node_accept_bytes_match: - if (!cset->non_match) - return match_len; - else - { - if (match_len > 0) - return 0; - else - return (elem_len > char_len) ? elem_len : char_len; - } - } - return 0; -} - -# ifdef _LIBC -static unsigned int -internal_function -find_collation_sequence_value (const unsigned char *mbs, size_t mbs_len) -{ - uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES); - if (nrules == 0) - { - if (mbs_len == 1) - { - /* No valid character. Match it as a single byte character. */ - const unsigned char *collseq = (const unsigned char *) - _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB); - return collseq[mbs[0]]; - } - return UINT_MAX; - } - else - { - int32_t idx; - const unsigned char *extra = (const unsigned char *) - _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB); - int32_t extrasize = (const unsigned char *) - _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB + 1) - extra; - - for (idx = 0; idx < extrasize;) - { - int mbs_cnt, found = 0; - int32_t elem_mbs_len; - /* Skip the name of collating element name. */ - idx = idx + extra[idx] + 1; - elem_mbs_len = extra[idx++]; - if (mbs_len == elem_mbs_len) - { - for (mbs_cnt = 0; mbs_cnt < elem_mbs_len; ++mbs_cnt) - if (extra[idx + mbs_cnt] != mbs[mbs_cnt]) - break; - if (mbs_cnt == elem_mbs_len) - /* Found the entry. */ - found = 1; - } - /* Skip the byte sequence of the collating element. */ - idx += elem_mbs_len; - /* Adjust for the alignment. */ - idx = (idx + 3) & ~3; - /* Skip the collation sequence value. */ - idx += sizeof (uint32_t); - /* Skip the wide char sequence of the collating element. */ - idx = idx + sizeof (uint32_t) * (extra[idx] + 1); - /* If we found the entry, return the sequence value. */ - if (found) - return *(uint32_t *) (extra + idx); - /* Skip the collation sequence value. */ - idx += sizeof (uint32_t); - } - return UINT_MAX; - } -} -# endif /* _LIBC */ -#endif /* RE_ENABLE_I18N */ - -/* Check whether the node accepts the byte which is IDX-th - byte of the INPUT. */ - -static int -internal_function -check_node_accept (const re_match_context_t *mctx, const re_token_t *node, - int idx) -{ - unsigned char ch; - ch = re_string_byte_at (&mctx->input, idx); - switch (node->type) - { - case CHARACTER: - if (node->opr.c != ch) - return 0; - break; - - case SIMPLE_BRACKET: - if (!bitset_contain (node->opr.sbcset, ch)) - return 0; - break; - -#ifdef RE_ENABLE_I18N - case OP_UTF8_PERIOD: - if (ch >= 0x80) - return 0; - /* FALLTHROUGH */ -#endif - case OP_PERIOD: - if ((ch == '\n' && !(mctx->dfa->syntax & RE_DOT_NEWLINE)) - || (ch == '\0' && (mctx->dfa->syntax & RE_DOT_NOT_NULL))) - return 0; - break; - - default: - return 0; - } - - if (node->constraint) - { - /* The node has constraints. Check whether the current context - satisfies the constraints. */ - unsigned int context = re_string_context_at (&mctx->input, idx, - mctx->eflags); - if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context)) - return 0; - } - - return 1; -} - -/* Extend the buffers, if the buffers have run out. */ - -static reg_errcode_t -internal_function -extend_buffers (re_match_context_t *mctx) -{ - reg_errcode_t ret; - re_string_t *pstr = &mctx->input; - - /* Avoid overflow. */ - if (BE (INT_MAX / 2 / sizeof (re_dfastate_t *) <= (size_t)pstr->bufs_len, 0)) - return REG_ESPACE; - - /* Double the lengthes of the buffers. */ - ret = re_string_realloc_buffers (pstr, pstr->bufs_len * 2); - if (BE (ret != REG_NOERROR, 0)) - return ret; - - if (mctx->state_log != NULL) - { - /* And double the length of state_log. */ - /* XXX We have no indication of the size of this buffer. If this - allocation fail we have no indication that the state_log array - does not have the right size. */ - re_dfastate_t **new_array = re_realloc (mctx->state_log, re_dfastate_t *, - pstr->bufs_len + 1); - if (BE (new_array == NULL, 0)) - return REG_ESPACE; - mctx->state_log = new_array; - } - - /* Then reconstruct the buffers. */ - if (pstr->icase) - { -#ifdef RE_ENABLE_I18N - if (pstr->mb_cur_max > 1) - { - ret = build_wcs_upper_buffer (pstr); - if (BE (ret != REG_NOERROR, 0)) - return ret; - } - else -#endif /* RE_ENABLE_I18N */ - build_upper_buffer (pstr); - } - else - { -#ifdef RE_ENABLE_I18N - if (pstr->mb_cur_max > 1) - build_wcs_buffer (pstr); - else -#endif /* RE_ENABLE_I18N */ - { - if (pstr->trans != NULL) - re_string_translate_buffer (pstr); - } - } - return REG_NOERROR; -} - - -/* Functions for matching context. */ - -/* Initialize MCTX. */ - -static reg_errcode_t -internal_function -match_ctx_init (re_match_context_t *mctx, int eflags, int n) -{ - mctx->eflags = eflags; - mctx->match_last = -1; - if (n > 0) - { - mctx->bkref_ents = re_malloc (struct re_backref_cache_entry, n); - mctx->sub_tops = re_malloc (re_sub_match_top_t *, n); - if (BE (mctx->bkref_ents == NULL || mctx->sub_tops == NULL, 0)) - return REG_ESPACE; - } - /* Already zero-ed by the caller. - else - mctx->bkref_ents = NULL; - mctx->nbkref_ents = 0; - mctx->nsub_tops = 0; */ - mctx->abkref_ents = n; - mctx->max_mb_elem_len = 1; - mctx->asub_tops = n; - return REG_NOERROR; -} - -/* Clean the entries which depend on the current input in MCTX. - This function must be invoked when the matcher changes the start index - of the input, or changes the input string. */ - -static void -internal_function -match_ctx_clean (re_match_context_t *mctx) -{ - int st_idx; - for (st_idx = 0; st_idx < mctx->nsub_tops; ++st_idx) - { - int sl_idx; - re_sub_match_top_t *top = mctx->sub_tops[st_idx]; - for (sl_idx = 0; sl_idx < top->nlasts; ++sl_idx) - { - re_sub_match_last_t *last = top->lasts[sl_idx]; - re_free (last->path.array); - re_free (last); - } - re_free (top->lasts); - if (top->path) - { - re_free (top->path->array); - re_free (top->path); - } - free (top); - } - - mctx->nsub_tops = 0; - mctx->nbkref_ents = 0; -} - -/* Free all the memory associated with MCTX. */ - -static void -internal_function -match_ctx_free (re_match_context_t *mctx) -{ - /* First, free all the memory associated with MCTX->SUB_TOPS. */ - match_ctx_clean (mctx); - re_free (mctx->sub_tops); - re_free (mctx->bkref_ents); -} - -/* Add a new backreference entry to MCTX. - Note that we assume that caller never call this function with duplicate - entry, and call with STR_IDX which isn't smaller than any existing entry. -*/ - -static reg_errcode_t -internal_function -match_ctx_add_entry (re_match_context_t *mctx, int node, int str_idx, int from, - int to) -{ - if (mctx->nbkref_ents >= mctx->abkref_ents) - { - struct re_backref_cache_entry* new_entry; - new_entry = re_realloc (mctx->bkref_ents, struct re_backref_cache_entry, - mctx->abkref_ents * 2); - if (BE (new_entry == NULL, 0)) - { - re_free (mctx->bkref_ents); - return REG_ESPACE; - } - mctx->bkref_ents = new_entry; - memset (mctx->bkref_ents + mctx->nbkref_ents, '\0', - sizeof (struct re_backref_cache_entry) * mctx->abkref_ents); - mctx->abkref_ents *= 2; - } - if (mctx->nbkref_ents > 0 - && mctx->bkref_ents[mctx->nbkref_ents - 1].str_idx == str_idx) - mctx->bkref_ents[mctx->nbkref_ents - 1].more = 1; - - mctx->bkref_ents[mctx->nbkref_ents].node = node; - mctx->bkref_ents[mctx->nbkref_ents].str_idx = str_idx; - mctx->bkref_ents[mctx->nbkref_ents].subexp_from = from; - mctx->bkref_ents[mctx->nbkref_ents].subexp_to = to; - - /* This is a cache that saves negative results of check_dst_limits_calc_pos. - If bit N is clear, means that this entry won't epsilon-transition to - an OP_OPEN_SUBEXP or OP_CLOSE_SUBEXP for the N+1-th subexpression. If - it is set, check_dst_limits_calc_pos_1 will recurse and try to find one - such node. - - A backreference does not epsilon-transition unless it is empty, so set - to all zeros if FROM != TO. */ - mctx->bkref_ents[mctx->nbkref_ents].eps_reachable_subexps_map - = (from == to ? ~0 : 0); - - mctx->bkref_ents[mctx->nbkref_ents++].more = 0; - if (mctx->max_mb_elem_len < to - from) - mctx->max_mb_elem_len = to - from; - return REG_NOERROR; -} - -/* Search for the first entry which has the same str_idx, or -1 if none is - found. Note that MCTX->BKREF_ENTS is already sorted by MCTX->STR_IDX. */ - -static int -internal_function -search_cur_bkref_entry (const re_match_context_t *mctx, int str_idx) -{ - int left, right, mid, last; - last = right = mctx->nbkref_ents; - for (left = 0; left < right;) - { - mid = (left + right) / 2; - if (mctx->bkref_ents[mid].str_idx < str_idx) - left = mid + 1; - else - right = mid; - } - if (left < last && mctx->bkref_ents[left].str_idx == str_idx) - return left; - else - return -1; -} - -/* Register the node NODE, whose type is OP_OPEN_SUBEXP, and which matches - at STR_IDX. */ - -static reg_errcode_t -internal_function -match_ctx_add_subtop (re_match_context_t *mctx, int node, int str_idx) -{ -#ifdef DEBUG - assert (mctx->sub_tops != NULL); - assert (mctx->asub_tops > 0); -#endif - if (BE (mctx->nsub_tops == mctx->asub_tops, 0)) - { - int new_asub_tops = mctx->asub_tops * 2; - re_sub_match_top_t **new_array = re_realloc (mctx->sub_tops, - re_sub_match_top_t *, - new_asub_tops); - if (BE (new_array == NULL, 0)) - return REG_ESPACE; - mctx->sub_tops = new_array; - mctx->asub_tops = new_asub_tops; - } - mctx->sub_tops[mctx->nsub_tops] = calloc (1, sizeof (re_sub_match_top_t)); - if (BE (mctx->sub_tops[mctx->nsub_tops] == NULL, 0)) - return REG_ESPACE; - mctx->sub_tops[mctx->nsub_tops]->node = node; - mctx->sub_tops[mctx->nsub_tops++]->str_idx = str_idx; - return REG_NOERROR; -} - -/* Register the node NODE, whose type is OP_CLOSE_SUBEXP, and which matches - at STR_IDX, whose corresponding OP_OPEN_SUBEXP is SUB_TOP. */ - -static re_sub_match_last_t * -internal_function -match_ctx_add_sublast (re_sub_match_top_t *subtop, int node, int str_idx) -{ - re_sub_match_last_t *new_entry; - if (BE (subtop->nlasts == subtop->alasts, 0)) - { - int new_alasts = 2 * subtop->alasts + 1; - re_sub_match_last_t **new_array = re_realloc (subtop->lasts, - re_sub_match_last_t *, - new_alasts); - if (BE (new_array == NULL, 0)) - return NULL; - subtop->lasts = new_array; - subtop->alasts = new_alasts; - } - new_entry = calloc (1, sizeof (re_sub_match_last_t)); - if (BE (new_entry != NULL, 1)) - { - subtop->lasts[subtop->nlasts] = new_entry; - new_entry->node = node; - new_entry->str_idx = str_idx; - ++subtop->nlasts; - } - return new_entry; -} - -static void -internal_function -sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts, - re_dfastate_t **limited_sts, int last_node, int last_str_idx) -{ - sctx->sifted_states = sifted_sts; - sctx->limited_states = limited_sts; - sctx->last_node = last_node; - sctx->last_str_idx = last_str_idx; - re_node_set_init_empty (&sctx->limits); -} diff --git a/deps/winhttp/CMakeLists.txt b/deps/winhttp/CMakeLists.txt new file mode 100644 index 00000000000..1a87989b97f --- /dev/null +++ b/deps/winhttp/CMakeLists.txt @@ -0,0 +1,24 @@ +find_program(DLLTOOL dlltool CMAKE_FIND_ROOT_PATH_BOTH) +if(NOT DLLTOOL) + message(FATAL_ERROR "Could not find dlltool command") +endif() + +set(LIBWINHTTP_PATH "${PROJECT_BINARY_DIR}/deps/winhttp") +set(LIBWINHTTP_PATH ${LIBWINHTTP_PATH} PARENT_SCOPE) +file(MAKE_DIRECTORY ${LIBWINHTTP_PATH}) + +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(WINHTTP_DEF "winhttp64.def") +else() + set(WINHTTP_DEF "winhttp.def") +endif() + +add_custom_command( + OUTPUT ${LIBWINHTTP_PATH}/libwinhttp.a + COMMAND ${DLLTOOL} -d ${WINHTTP_DEF} -k -D winhttp.dll -l libwinhttp.a + DEPENDS ${WINHTTP_DEF} + WORKING_DIRECTORY ${LIBWINHTTP_PATH}) + +set_source_files_properties( + ${CMAKE_CURRENT_SOURCE_DIR}/src/transports/winhttp.c + PROPERTIES OBJECT_DEPENDS ${LIBWINHTTP_PATH}/libwinhttp.a) diff --git a/deps/winhttp/COPYING.GPL b/deps/winhttp/COPYING.GPL new file mode 100644 index 00000000000..da695ebdb47 --- /dev/null +++ b/deps/winhttp/COPYING.GPL @@ -0,0 +1,993 @@ + libgit2 is Copyright (C) the libgit2 contributors, + unless otherwise stated. See the AUTHORS file for details. + + Note that the only valid version of the GPL as far as this project + is concerned is _this_ particular version of the license (ie v2, not + v2.2 or v3.x or whatever), unless explicitly otherwise stated. + +---------------------------------------------------------------------- + + LINKING EXCEPTION + + In addition to the permissions in the GNU General Public License, + the authors give you unlimited permission to link the compiled + version of this library into combinations with other programs, + and to distribute those combinations without any restriction + coming from the use of this file. (The General Public License + restrictions do apply in other respects; for example, they cover + modification of the file, and distribution when not linked into + a combined executable.) + +---------------------------------------------------------------------- + + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program 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 program 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 program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. + +---------------------------------------------------------------------- + +The bundled ZLib code is licensed under the ZLib license: + +Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler + + 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. + + Jean-loup Gailly Mark Adler + jloup@gzip.org madler@alumni.caltech.edu + +---------------------------------------------------------------------- + +The Clar framework is licensed under the ISC license: + +Copyright (c) 2011-2015 Vicent Marti + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +---------------------------------------------------------------------- + +The regex library (deps/regex/) is licensed under the GNU LGPL +(available at the end of this file). + +Definitions for data structures and routines for the regular +expression library. + +Copyright (C) 1985,1989-93,1995-98,2000,2001,2002,2003,2005,2006,2008 +Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +The GNU C Library 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 +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with the GNU C Library; if not, write to the Free +Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +---------------------------------------------------------------------- + +The bundled winhttp definition files (deps/winhttp/) are licensed under +the GNU LGPL (available at the end of this file). + +Copyright (C) 2007 Francois Gouget + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library 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 +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + +---------------------------------------------------------------------- + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + +---------------------------------------------------------------------- + +The bundled SHA1 collision detection code is licensed under the MIT license: + +MIT License + +Copyright (c) 2017: + Marc Stevens + Cryptology Group + Centrum Wiskunde & Informatica + P.O. Box 94079, 1090 GB Amsterdam, Netherlands + marc@marc-stevens.nl + + Dan Shumow + Microsoft Research + danshu@microsoft.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/deps/winhttp/COPYING.LGPL b/deps/winhttp/COPYING.LGPL new file mode 100644 index 00000000000..4362b49151d --- /dev/null +++ b/deps/winhttp/COPYING.LGPL @@ -0,0 +1,502 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/deps/winhttp/urlmon.h b/deps/winhttp/urlmon.h new file mode 100644 index 00000000000..4143d501e4f --- /dev/null +++ b/deps/winhttp/urlmon.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#if defined(__MINGW_VERSION) || defined(__MINGW32_VERSION) + +#ifndef __CUSTOM_URLMON_H +#define __CUSTOM_URLMON_H + +typedef struct IInternetSecurityManager IInternetSecurityManager; + +typedef struct IInternetSecurityManagerVtbl +{ + HRESULT(STDMETHODCALLTYPE *QueryInterface)(IInternetSecurityManager *, REFIID, void **); + ULONG(STDMETHODCALLTYPE *AddRef)(IInternetSecurityManager *); + ULONG(STDMETHODCALLTYPE *Release)(IInternetSecurityManager *); + LPVOID SetSecuritySite; + LPVOID GetSecuritySite; + HRESULT(STDMETHODCALLTYPE *MapUrlToZone)(IInternetSecurityManager *, LPCWSTR, DWORD *, DWORD); + LPVOID GetSecurityId; + LPVOID ProcessUrlAction; + LPVOID QueryCustomPolicy; + LPVOID SetZoneMapping; + LPVOID GetZoneMappings; +} IInternetSecurityManagerVtbl; + +struct IInternetSecurityManager +{ + CONST_VTBL struct IInternetSecurityManagerVtbl *lpVtbl; +}; + +#define URLZONE_LOCAL_MACHINE 0 +#define URLZONE_INTRANET 1 +#define URLZONE_TRUSTED 2 + +#endif /* __CUSTOM_URLMON_H */ + +#else + +#include_next + +#endif diff --git a/deps/winhttp/winhttp.def b/deps/winhttp/winhttp.def new file mode 100644 index 00000000000..eecce59c3ad --- /dev/null +++ b/deps/winhttp/winhttp.def @@ -0,0 +1,29 @@ +LIBRARY WINHTTP +EXPORTS +WinHttpAddRequestHeaders@16 +WinHttpCheckPlatform@0 +WinHttpCloseHandle@4 +WinHttpConnect@16 +WinHttpCrackUrl@16 +WinHttpCreateUrl@16 +WinHttpDetectAutoProxyConfigUrl@8 +WinHttpGetDefaultProxyConfiguration@4 +WinHttpGetIEProxyConfigForCurrentUser@4 +WinHttpGetProxyForUrl@16 +WinHttpOpen@20 +WinHttpOpenRequest@28 +WinHttpQueryAuthSchemes@16 +WinHttpQueryDataAvailable@8 +WinHttpQueryHeaders@24 +WinHttpQueryOption@16 +WinHttpReadData@16 +WinHttpReceiveResponse@8 +WinHttpSendRequest@28 +WinHttpSetCredentials@24 +WinHttpSetDefaultProxyConfiguration@4 +WinHttpSetOption@16 +WinHttpSetStatusCallback@16 +WinHttpSetTimeouts@20 +WinHttpTimeFromSystemTime@8 +WinHttpTimeToSystemTime@8 +WinHttpWriteData@16 diff --git a/deps/winhttp/winhttp.h b/deps/winhttp/winhttp.h new file mode 100644 index 00000000000..b7fef1c4b47 --- /dev/null +++ b/deps/winhttp/winhttp.h @@ -0,0 +1,594 @@ +/* + * Copyright (C) 2007 Francois Gouget + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#if defined(__MINGW_VERSION) || defined(__MINGW32_VERSION) + +#ifndef __WINE_WINHTTP_H +#define __WINE_WINHTTP_H + +#ifdef _WIN64 +#include +#else +#include +#endif + +#define WINHTTPAPI +#define BOOLAPI WINHTTPAPI BOOL WINAPI + + +typedef LPVOID HINTERNET; +typedef HINTERNET *LPHINTERNET; + +#define INTERNET_DEFAULT_PORT 0 +#define INTERNET_DEFAULT_HTTP_PORT 80 +#define INTERNET_DEFAULT_HTTPS_PORT 443 +typedef WORD INTERNET_PORT; +typedef INTERNET_PORT *LPINTERNET_PORT; + +#define INTERNET_SCHEME_HTTP 1 +#define INTERNET_SCHEME_HTTPS 2 +typedef int INTERNET_SCHEME, *LPINTERNET_SCHEME; + +#define ICU_ESCAPE 0x80000000 + +/* flags for WinHttpOpen */ +#define WINHTTP_FLAG_ASYNC 0x10000000 + +/* flags for WinHttpOpenRequest */ +#define WINHTTP_FLAG_ESCAPE_PERCENT 0x00000004 +#define WINHTTP_FLAG_NULL_CODEPAGE 0x00000008 +#define WINHTTP_FLAG_ESCAPE_DISABLE 0x00000040 +#define WINHTTP_FLAG_ESCAPE_DISABLE_QUERY 0x00000080 +#define WINHTTP_FLAG_BYPASS_PROXY_CACHE 0x00000100 +#define WINHTTP_FLAG_REFRESH WINHTTP_FLAG_BYPASS_PROXY_CACHE +#define WINHTTP_FLAG_SECURE 0x00800000 + +#define WINHTTP_ACCESS_TYPE_DEFAULT_PROXY 0 +#define WINHTTP_ACCESS_TYPE_NO_PROXY 1 +#define WINHTTP_ACCESS_TYPE_NAMED_PROXY 3 + +#define WINHTTP_NO_PROXY_NAME NULL +#define WINHTTP_NO_PROXY_BYPASS NULL + +#define WINHTTP_NO_REFERER NULL +#define WINHTTP_DEFAULT_ACCEPT_TYPES NULL + +#define WINHTTP_NO_ADDITIONAL_HEADERS NULL +#define WINHTTP_NO_REQUEST_DATA NULL + +#define WINHTTP_HEADER_NAME_BY_INDEX NULL +#define WINHTTP_NO_OUTPUT_BUFFER NULL +#define WINHTTP_NO_HEADER_INDEX NULL + +#define WINHTTP_ADDREQ_INDEX_MASK 0x0000FFFF +#define WINHTTP_ADDREQ_FLAGS_MASK 0xFFFF0000 +#define WINHTTP_ADDREQ_FLAG_ADD_IF_NEW 0x10000000 +#define WINHTTP_ADDREQ_FLAG_ADD 0x20000000 +#define WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA 0x40000000 +#define WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON 0x01000000 +#define WINHTTP_ADDREQ_FLAG_COALESCE WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA +#define WINHTTP_ADDREQ_FLAG_REPLACE 0x80000000 + +#define WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH 0 + +/* flags for WinHttp{Set/Query}Options */ +#define WINHTTP_FIRST_OPTION WINHTTP_OPTION_CALLBACK +#define WINHTTP_OPTION_CALLBACK 1 +#define WINHTTP_OPTION_RESOLVE_TIMEOUT 2 +#define WINHTTP_OPTION_CONNECT_TIMEOUT 3 +#define WINHTTP_OPTION_CONNECT_RETRIES 4 +#define WINHTTP_OPTION_SEND_TIMEOUT 5 +#define WINHTTP_OPTION_RECEIVE_TIMEOUT 6 +#define WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT 7 +#define WINHTTP_OPTION_HANDLE_TYPE 9 +#define WINHTTP_OPTION_READ_BUFFER_SIZE 12 +#define WINHTTP_OPTION_WRITE_BUFFER_SIZE 13 +#define WINHTTP_OPTION_PARENT_HANDLE 21 +#define WINHTTP_OPTION_EXTENDED_ERROR 24 +#define WINHTTP_OPTION_SECURITY_FLAGS 31 +#define WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT 32 +#define WINHTTP_OPTION_URL 34 +#define WINHTTP_OPTION_SECURITY_KEY_BITNESS 36 +#define WINHTTP_OPTION_PROXY 38 +#define WINHTTP_OPTION_USER_AGENT 41 +#define WINHTTP_OPTION_CONTEXT_VALUE 45 +#define WINHTTP_OPTION_CLIENT_CERT_CONTEXT 47 +#define WINHTTP_OPTION_REQUEST_PRIORITY 58 +#define WINHTTP_OPTION_HTTP_VERSION 59 +#define WINHTTP_OPTION_DISABLE_FEATURE 63 +#define WINHTTP_OPTION_CODEPAGE 68 +#define WINHTTP_OPTION_MAX_CONNS_PER_SERVER 73 +#define WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER 74 +#define WINHTTP_OPTION_AUTOLOGON_POLICY 77 +#define WINHTTP_OPTION_SERVER_CERT_CONTEXT 78 +#define WINHTTP_OPTION_ENABLE_FEATURE 79 +#define WINHTTP_OPTION_WORKER_THREAD_COUNT 80 +#define WINHTTP_OPTION_PASSPORT_COBRANDING_TEXT 81 +#define WINHTTP_OPTION_PASSPORT_COBRANDING_URL 82 +#define WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH 83 +#define WINHTTP_OPTION_SECURE_PROTOCOLS 84 +#define WINHTTP_OPTION_ENABLETRACING 85 +#define WINHTTP_OPTION_PASSPORT_SIGN_OUT 86 +#define WINHTTP_OPTION_PASSPORT_RETURN_URL 87 +#define WINHTTP_OPTION_REDIRECT_POLICY 88 +#define WINHTTP_OPTION_MAX_HTTP_AUTOMATIC_REDIRECTS 89 +#define WINHTTP_OPTION_MAX_HTTP_STATUS_CONTINUE 90 +#define WINHTTP_OPTION_MAX_RESPONSE_HEADER_SIZE 91 +#define WINHTTP_OPTION_MAX_RESPONSE_DRAIN_SIZE 92 +#define WINHTTP_OPTION_CONNECTION_INFO 93 +#define WINHTTP_OPTION_CLIENT_CERT_ISSUER_LIST 94 +#define WINHTTP_OPTION_SPN 96 +#define WINHTTP_OPTION_GLOBAL_PROXY_CREDS 97 +#define WINHTTP_OPTION_GLOBAL_SERVER_CREDS 98 +#define WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT 99 +#define WINHTTP_OPTION_REJECT_USERPWD_IN_URL 100 +#define WINHTTP_OPTION_USE_GLOBAL_SERVER_CREDENTIALS 101 +#define WINHTTP_LAST_OPTION WINHTTP_OPTION_USE_GLOBAL_SERVER_CREDENTIALS +#define WINHTTP_OPTION_USERNAME 0x1000 +#define WINHTTP_OPTION_PASSWORD 0x1001 +#define WINHTTP_OPTION_PROXY_USERNAME 0x1002 +#define WINHTTP_OPTION_PROXY_PASSWORD 0x1003 + +#define WINHTTP_CONNS_PER_SERVER_UNLIMITED 0xFFFFFFFF + +#define WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM 0 +#define WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW 1 +#define WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH 2 +#define WINHTTP_AUTOLOGON_SECURITY_LEVEL_DEFAULT WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM + +#define WINHTTP_OPTION_REDIRECT_POLICY_NEVER 0 +#define WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP 1 +#define WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS 2 +#define WINHTTP_OPTION_REDIRECT_POLICY_LAST WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS +#define WINHTTP_OPTION_REDIRECT_POLICY_DEFAULT WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP + +#define WINHTTP_DISABLE_PASSPORT_AUTH 0x00000000 +#define WINHTTP_ENABLE_PASSPORT_AUTH 0x10000000 +#define WINHTTP_DISABLE_PASSPORT_KEYRING 0x20000000 +#define WINHTTP_ENABLE_PASSPORT_KEYRING 0x40000000 + +#define WINHTTP_DISABLE_COOKIES 0x00000001 +#define WINHTTP_DISABLE_REDIRECTS 0x00000002 +#define WINHTTP_DISABLE_AUTHENTICATION 0x00000004 +#define WINHTTP_DISABLE_KEEP_ALIVE 0x00000008 +#define WINHTTP_ENABLE_SSL_REVOCATION 0x00000001 +#define WINHTTP_ENABLE_SSL_REVERT_IMPERSONATION 0x00000002 +#define WINHTTP_DISABLE_SPN_SERVER_PORT 0x00000000 +#define WINHTTP_ENABLE_SPN_SERVER_PORT 0x00000001 +#define WINHTTP_OPTION_SPN_MASK WINHTTP_ENABLE_SPN_SERVER_PORT + +/* Options for WinHttpOpenRequest */ +#define WINHTTP_NO_REFERER NULL +#define WINHTTP_DEFAULT_ACCEPT_TYPES NULL + +/* Options for WinHttpSendRequest */ +#define WINHTTP_NO_ADDITIONAL_HEADERS NULL +#define WINHTTP_NO_REQUEST_DATA NULL + +/* WinHTTP error codes */ +#define WINHTTP_ERROR_BASE 12000 +#define ERROR_WINHTTP_OUT_OF_HANDLES (WINHTTP_ERROR_BASE + 1) +#define ERROR_WINHTTP_TIMEOUT (WINHTTP_ERROR_BASE + 2) +#define ERROR_WINHTTP_INTERNAL_ERROR (WINHTTP_ERROR_BASE + 4) +#define ERROR_WINHTTP_INVALID_URL (WINHTTP_ERROR_BASE + 5) +#define ERROR_WINHTTP_UNRECOGNIZED_SCHEME (WINHTTP_ERROR_BASE + 6) +#define ERROR_WINHTTP_NAME_NOT_RESOLVED (WINHTTP_ERROR_BASE + 7) +#define ERROR_WINHTTP_INVALID_OPTION (WINHTTP_ERROR_BASE + 9) +#define ERROR_WINHTTP_OPTION_NOT_SETTABLE (WINHTTP_ERROR_BASE + 11) +#define ERROR_WINHTTP_SHUTDOWN (WINHTTP_ERROR_BASE + 12) +#define ERROR_WINHTTP_LOGIN_FAILURE (WINHTTP_ERROR_BASE + 15) +#define ERROR_WINHTTP_OPERATION_CANCELLED (WINHTTP_ERROR_BASE + 17) +#define ERROR_WINHTTP_INCORRECT_HANDLE_TYPE (WINHTTP_ERROR_BASE + 18) +#define ERROR_WINHTTP_INCORRECT_HANDLE_STATE (WINHTTP_ERROR_BASE + 19) +#define ERROR_WINHTTP_CANNOT_CONNECT (WINHTTP_ERROR_BASE + 29) +#define ERROR_WINHTTP_CONNECTION_ERROR (WINHTTP_ERROR_BASE + 30) +#define ERROR_WINHTTP_RESEND_REQUEST (WINHTTP_ERROR_BASE + 32) +#define ERROR_WINHTTP_SECURE_CERT_DATE_INVALID (WINHTTP_ERROR_BASE + 37) +#define ERROR_WINHTTP_SECURE_CERT_CN_INVALID (WINHTTP_ERROR_BASE + 38) +#define ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED (WINHTTP_ERROR_BASE + 44) +#define ERROR_WINHTTP_SECURE_INVALID_CA (WINHTTP_ERROR_BASE + 45) +#define ERROR_WINHTTP_SECURE_CERT_REV_FAILED (WINHTTP_ERROR_BASE + 57) +#define ERROR_WINHTTP_CANNOT_CALL_BEFORE_OPEN (WINHTTP_ERROR_BASE + 100) +#define ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND (WINHTTP_ERROR_BASE + 101) +#define ERROR_WINHTTP_CANNOT_CALL_AFTER_SEND (WINHTTP_ERROR_BASE + 102) +#define ERROR_WINHTTP_CANNOT_CALL_AFTER_OPEN (WINHTTP_ERROR_BASE + 103) +#define ERROR_WINHTTP_HEADER_NOT_FOUND (WINHTTP_ERROR_BASE + 150) +#define ERROR_WINHTTP_INVALID_SERVER_RESPONSE (WINHTTP_ERROR_BASE + 152) +#define ERROR_WINHTTP_INVALID_HEADER (WINHTTP_ERROR_BASE + 153) +#define ERROR_WINHTTP_INVALID_QUERY_REQUEST (WINHTTP_ERROR_BASE + 154) +#define ERROR_WINHTTP_HEADER_ALREADY_EXISTS (WINHTTP_ERROR_BASE + 155) +#define ERROR_WINHTTP_REDIRECT_FAILED (WINHTTP_ERROR_BASE + 156) +#define ERROR_WINHTTP_SECURE_CHANNEL_ERROR (WINHTTP_ERROR_BASE + 157) +#define ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT (WINHTTP_ERROR_BASE + 166) +#define ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT (WINHTTP_ERROR_BASE + 167) +#define ERROR_WINHTTP_SECURE_INVALID_CERT (WINHTTP_ERROR_BASE + 169) +#define ERROR_WINHTTP_SECURE_CERT_REVOKED (WINHTTP_ERROR_BASE + 170) +#define ERROR_WINHTTP_NOT_INITIALIZED (WINHTTP_ERROR_BASE + 172) +#define ERROR_WINHTTP_SECURE_FAILURE (WINHTTP_ERROR_BASE + 175) +#define ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR (WINHTTP_ERROR_BASE + 178) +#define ERROR_WINHTTP_SECURE_CERT_WRONG_USAGE (WINHTTP_ERROR_BASE + 179) +#define ERROR_WINHTTP_AUTODETECTION_FAILED (WINHTTP_ERROR_BASE + 180) +#define ERROR_WINHTTP_HEADER_COUNT_EXCEEDED (WINHTTP_ERROR_BASE + 181) +#define ERROR_WINHTTP_HEADER_SIZE_OVERFLOW (WINHTTP_ERROR_BASE + 182) +#define ERROR_WINHTTP_CHUNKED_ENCODING_HEADER_SIZE_OVERFLOW (WINHTTP_ERROR_BASE + 183) +#define ERROR_WINHTTP_RESPONSE_DRAIN_OVERFLOW (WINHTTP_ERROR_BASE + 184) +#define ERROR_WINHTTP_CLIENT_CERT_NO_PRIVATE_KEY (WINHTTP_ERROR_BASE + 185) +#define ERROR_WINHTTP_CLIENT_CERT_NO_ACCESS_PRIVATE_KEY (WINHTTP_ERROR_BASE + 186) +#define WINHTTP_ERROR_LAST (WINHTTP_ERROR_BASE + 186) + +/* WinHttp status codes */ +#define HTTP_STATUS_CONTINUE 100 +#define HTTP_STATUS_SWITCH_PROTOCOLS 101 +#define HTTP_STATUS_OK 200 +#define HTTP_STATUS_CREATED 201 +#define HTTP_STATUS_ACCEPTED 202 +#define HTTP_STATUS_PARTIAL 203 +#define HTTP_STATUS_NO_CONTENT 204 +#define HTTP_STATUS_RESET_CONTENT 205 +#define HTTP_STATUS_PARTIAL_CONTENT 206 +#define HTTP_STATUS_WEBDAV_MULTI_STATUS 207 +#define HTTP_STATUS_AMBIGUOUS 300 +#define HTTP_STATUS_MOVED 301 +#define HTTP_STATUS_REDIRECT 302 +#define HTTP_STATUS_REDIRECT_METHOD 303 +#define HTTP_STATUS_NOT_MODIFIED 304 +#define HTTP_STATUS_USE_PROXY 305 +#define HTTP_STATUS_REDIRECT_KEEP_VERB 307 +#define HTTP_STATUS_BAD_REQUEST 400 +#define HTTP_STATUS_DENIED 401 +#define HTTP_STATUS_PAYMENT_REQ 402 +#define HTTP_STATUS_FORBIDDEN 403 +#define HTTP_STATUS_NOT_FOUND 404 +#define HTTP_STATUS_BAD_METHOD 405 +#define HTTP_STATUS_NONE_ACCEPTABLE 406 +#define HTTP_STATUS_PROXY_AUTH_REQ 407 +#define HTTP_STATUS_REQUEST_TIMEOUT 408 +#define HTTP_STATUS_CONFLICT 409 +#define HTTP_STATUS_GONE 410 +#define HTTP_STATUS_LENGTH_REQUIRED 411 +#define HTTP_STATUS_PRECOND_FAILED 412 +#define HTTP_STATUS_REQUEST_TOO_LARGE 413 +#define HTTP_STATUS_URI_TOO_LONG 414 +#define HTTP_STATUS_UNSUPPORTED_MEDIA 415 +#define HTTP_STATUS_RETRY_WITH 449 +#define HTTP_STATUS_SERVER_ERROR 500 +#define HTTP_STATUS_NOT_SUPPORTED 501 +#define HTTP_STATUS_BAD_GATEWAY 502 +#define HTTP_STATUS_SERVICE_UNAVAIL 503 +#define HTTP_STATUS_GATEWAY_TIMEOUT 504 +#define HTTP_STATUS_VERSION_NOT_SUP 505 +#define HTTP_STATUS_FIRST HTTP_STATUS_CONTINUE +#define HTTP_STATUS_LAST HTTP_STATUS_VERSION_NOT_SUP + +#define SECURITY_FLAG_IGNORE_UNKNOWN_CA 0x00000100 +#define SECURITY_FLAG_IGNORE_CERT_DATE_INVALID 0x00002000 +#define SECURITY_FLAG_IGNORE_CERT_CN_INVALID 0x00001000 +#define SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE 0x00000200 +#define SECURITY_FLAG_SECURE 0x00000001 +#define SECURITY_FLAG_STRENGTH_WEAK 0x10000000 +#define SECURITY_FLAG_STRENGTH_MEDIUM 0x40000000 +#define SECURITY_FLAG_STRENGTH_STRONG 0x20000000 + +#define ICU_NO_ENCODE 0x20000000 +#define ICU_DECODE 0x10000000 +#define ICU_NO_META 0x08000000 +#define ICU_ENCODE_SPACES_ONLY 0x04000000 +#define ICU_BROWSER_MODE 0x02000000 +#define ICU_ENCODE_PERCENT 0x00001000 + +/* Query flags */ +#define WINHTTP_QUERY_MIME_VERSION 0 +#define WINHTTP_QUERY_CONTENT_TYPE 1 +#define WINHTTP_QUERY_CONTENT_TRANSFER_ENCODING 2 +#define WINHTTP_QUERY_CONTENT_ID 3 +#define WINHTTP_QUERY_CONTENT_DESCRIPTION 4 +#define WINHTTP_QUERY_CONTENT_LENGTH 5 +#define WINHTTP_QUERY_CONTENT_LANGUAGE 6 +#define WINHTTP_QUERY_ALLOW 7 +#define WINHTTP_QUERY_PUBLIC 8 +#define WINHTTP_QUERY_DATE 9 +#define WINHTTP_QUERY_EXPIRES 10 +#define WINHTTP_QUERY_LAST_MODIFIED 11 +#define WINHTTP_QUERY_MESSAGE_ID 12 +#define WINHTTP_QUERY_URI 13 +#define WINHTTP_QUERY_DERIVED_FROM 14 +#define WINHTTP_QUERY_COST 15 +#define WINHTTP_QUERY_LINK 16 +#define WINHTTP_QUERY_PRAGMA 17 +#define WINHTTP_QUERY_VERSION 18 +#define WINHTTP_QUERY_STATUS_CODE 19 +#define WINHTTP_QUERY_STATUS_TEXT 20 +#define WINHTTP_QUERY_RAW_HEADERS 21 +#define WINHTTP_QUERY_RAW_HEADERS_CRLF 22 +#define WINHTTP_QUERY_CONNECTION 23 +#define WINHTTP_QUERY_ACCEPT 24 +#define WINHTTP_QUERY_ACCEPT_CHARSET 25 +#define WINHTTP_QUERY_ACCEPT_ENCODING 26 +#define WINHTTP_QUERY_ACCEPT_LANGUAGE 27 +#define WINHTTP_QUERY_AUTHORIZATION 28 +#define WINHTTP_QUERY_CONTENT_ENCODING 29 +#define WINHTTP_QUERY_FORWARDED 30 +#define WINHTTP_QUERY_FROM 31 +#define WINHTTP_QUERY_IF_MODIFIED_SINCE 32 +#define WINHTTP_QUERY_LOCATION 33 +#define WINHTTP_QUERY_ORIG_URI 34 +#define WINHTTP_QUERY_REFERER 35 +#define WINHTTP_QUERY_RETRY_AFTER 36 +#define WINHTTP_QUERY_SERVER 37 +#define WINHTTP_QUERY_TITLE 38 +#define WINHTTP_QUERY_USER_AGENT 39 +#define WINHTTP_QUERY_WWW_AUTHENTICATE 40 +#define WINHTTP_QUERY_PROXY_AUTHENTICATE 41 +#define WINHTTP_QUERY_ACCEPT_RANGES 42 +#define WINHTTP_QUERY_SET_COOKIE 43 +#define WINHTTP_QUERY_COOKIE 44 +#define WINHTTP_QUERY_REQUEST_METHOD 45 +#define WINHTTP_QUERY_REFRESH 46 +#define WINHTTP_QUERY_CONTENT_DISPOSITION 47 +#define WINHTTP_QUERY_AGE 48 +#define WINHTTP_QUERY_CACHE_CONTROL 49 +#define WINHTTP_QUERY_CONTENT_BASE 50 +#define WINHTTP_QUERY_CONTENT_LOCATION 51 +#define WINHTTP_QUERY_CONTENT_MD5 52 +#define WINHTTP_QUERY_CONTENT_RANGE 53 +#define WINHTTP_QUERY_ETAG 54 +#define WINHTTP_QUERY_HOST 55 +#define WINHTTP_QUERY_IF_MATCH 56 +#define WINHTTP_QUERY_IF_NONE_MATCH 57 +#define WINHTTP_QUERY_IF_RANGE 58 +#define WINHTTP_QUERY_IF_UNMODIFIED_SINCE 59 +#define WINHTTP_QUERY_MAX_FORWARDS 60 +#define WINHTTP_QUERY_PROXY_AUTHORIZATION 61 +#define WINHTTP_QUERY_RANGE 62 +#define WINHTTP_QUERY_TRANSFER_ENCODING 63 +#define WINHTTP_QUERY_UPGRADE 64 +#define WINHTTP_QUERY_VARY 65 +#define WINHTTP_QUERY_VIA 66 +#define WINHTTP_QUERY_WARNING 67 +#define WINHTTP_QUERY_EXPECT 68 +#define WINHTTP_QUERY_PROXY_CONNECTION 69 +#define WINHTTP_QUERY_UNLESS_MODIFIED_SINCE 70 +#define WINHTTP_QUERY_PROXY_SUPPORT 75 +#define WINHTTP_QUERY_AUTHENTICATION_INFO 76 +#define WINHTTP_QUERY_PASSPORT_URLS 77 +#define WINHTTP_QUERY_PASSPORT_CONFIG 78 +#define WINHTTP_QUERY_MAX 78 +#define WINHTTP_QUERY_CUSTOM 65535 +#define WINHTTP_QUERY_FLAG_REQUEST_HEADERS 0x80000000 +#define WINHTTP_QUERY_FLAG_SYSTEMTIME 0x40000000 +#define WINHTTP_QUERY_FLAG_NUMBER 0x20000000 + +/* Callback options */ +#define WINHTTP_CALLBACK_STATUS_RESOLVING_NAME 0x00000001 +#define WINHTTP_CALLBACK_STATUS_NAME_RESOLVED 0x00000002 +#define WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER 0x00000004 +#define WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER 0x00000008 +#define WINHTTP_CALLBACK_STATUS_SENDING_REQUEST 0x00000010 +#define WINHTTP_CALLBACK_STATUS_REQUEST_SENT 0x00000020 +#define WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE 0x00000040 +#define WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED 0x00000080 +#define WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION 0x00000100 +#define WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED 0x00000200 +#define WINHTTP_CALLBACK_STATUS_HANDLE_CREATED 0x00000400 +#define WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING 0x00000800 +#define WINHTTP_CALLBACK_STATUS_DETECTING_PROXY 0x00001000 +#define WINHTTP_CALLBACK_STATUS_REDIRECT 0x00004000 +#define WINHTTP_CALLBACK_STATUS_INTERMEDIATE_RESPONSE 0x00008000 +#define WINHTTP_CALLBACK_STATUS_SECURE_FAILURE 0x00010000 +#define WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE 0x00020000 +#define WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE 0x00040000 +#define WINHTTP_CALLBACK_STATUS_READ_COMPLETE 0x00080000 +#define WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE 0x00100000 +#define WINHTTP_CALLBACK_STATUS_REQUEST_ERROR 0x00200000 +#define WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE 0x00400000 +#define WINHTTP_CALLBACK_FLAG_RESOLVE_NAME (WINHTTP_CALLBACK_STATUS_RESOLVING_NAME | WINHTTP_CALLBACK_STATUS_NAME_RESOLVED) +#define WINHTTP_CALLBACK_FLAG_CONNECT_TO_SERVER (WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER | WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER) +#define WINHTTP_CALLBACK_FLAG_SEND_REQUEST (WINHTTP_CALLBACK_STATUS_SENDING_REQUEST | WINHTTP_CALLBACK_STATUS_REQUEST_SENT) +#define WINHTTP_CALLBACK_FLAG_RECEIVE_RESPONSE (WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE | WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED) +#define WINHTTP_CALLBACK_FLAG_CLOSE_CONNECTION (WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION | WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED) +#define WINHTTP_CALLBACK_FLAG_HANDLES (WINHTTP_CALLBACK_STATUS_HANDLE_CREATED | WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING) +#define WINHTTP_CALLBACK_FLAG_DETECTING_PROXY WINHTTP_CALLBACK_STATUS_DETECTING_PROXY +#define WINHTTP_CALLBACK_FLAG_REDIRECT WINHTTP_CALLBACK_STATUS_REDIRECT +#define WINHTTP_CALLBACK_FLAG_INTERMEDIATE_RESPONSE WINHTTP_CALLBACK_STATUS_INTERMEDIATE_RESPONSE +#define WINHTTP_CALLBACK_FLAG_SECURE_FAILURE WINHTTP_CALLBACK_STATUS_SECURE_FAILURE +#define WINHTTP_CALLBACK_FLAG_SENDREQUEST_COMPLETE WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE +#define WINHTTP_CALLBACK_FLAG_HEADERS_AVAILABLE WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE +#define WINHTTP_CALLBACK_FLAG_DATA_AVAILABLE WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE +#define WINHTTP_CALLBACK_FLAG_READ_COMPLETE WINHTTP_CALLBACK_STATUS_READ_COMPLETE +#define WINHTTP_CALLBACK_FLAG_WRITE_COMPLETE WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE +#define WINHTTP_CALLBACK_FLAG_REQUEST_ERROR WINHTTP_CALLBACK_STATUS_REQUEST_ERROR +#define WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS (WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE | WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE \ + | WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE | WINHTTP_CALLBACK_STATUS_READ_COMPLETE \ + | WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE | WINHTTP_CALLBACK_STATUS_REQUEST_ERROR) +#define WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS 0xffffffff +#define WINHTTP_INVALID_STATUS_CALLBACK ((WINHTTP_STATUS_CALLBACK)(-1)) + +#define API_RECEIVE_RESPONSE (1) +#define API_QUERY_DATA_AVAILABLE (2) +#define API_READ_DATA (3) +#define API_WRITE_DATA (4) +#define API_SEND_REQUEST (5) + +#define WINHTTP_HANDLE_TYPE_SESSION 1 +#define WINHTTP_HANDLE_TYPE_CONNECT 2 +#define WINHTTP_HANDLE_TYPE_REQUEST 3 + +#define WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED 0x00000001 +#define WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT 0x00000002 +#define WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED 0x00000004 +#define WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA 0x00000008 +#define WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID 0x00000010 +#define WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID 0x00000020 +#define WINHTTP_CALLBACK_STATUS_FLAG_CERT_WRONG_USAGE 0x00000040 +#define WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR 0x80000000 + +#define WINHTTP_FLAG_SECURE_PROTOCOL_SSL2 0x00000008 +#define WINHTTP_FLAG_SECURE_PROTOCOL_SSL3 0x00000020 +#define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 0x00000080 +#define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 0x00000200 +#define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 0x00000800 +#define WINHTTP_FLAG_SECURE_PROTOCOL_ALL (WINHTTP_FLAG_SECURE_PROTOCOL_SSL2 | WINHTTP_FLAG_SECURE_PROTOCOL_SSL3 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1) + +#define WINHTTP_AUTH_SCHEME_BASIC 0x00000001 +#define WINHTTP_AUTH_SCHEME_NTLM 0x00000002 +#define WINHTTP_AUTH_SCHEME_PASSPORT 0x00000004 +#define WINHTTP_AUTH_SCHEME_DIGEST 0x00000008 +#define WINHTTP_AUTH_SCHEME_NEGOTIATE 0x00000010 + +#define WINHTTP_AUTH_TARGET_SERVER 0x00000000 +#define WINHTTP_AUTH_TARGET_PROXY 0x00000001 + +#define WINHTTP_TIME_FORMAT_BUFSIZE 62 + +typedef struct +{ + DWORD dwStructSize; + LPWSTR lpszScheme; + DWORD dwSchemeLength; + INTERNET_SCHEME nScheme; + LPWSTR lpszHostName; + DWORD dwHostNameLength; + INTERNET_PORT nPort; + LPWSTR lpszUserName; + DWORD dwUserNameLength; + LPWSTR lpszPassword; + DWORD dwPasswordLength; + LPWSTR lpszUrlPath; + DWORD dwUrlPathLength; + LPWSTR lpszExtraInfo; + DWORD dwExtraInfoLength; +} URL_COMPONENTS, *LPURL_COMPONENTS; +typedef URL_COMPONENTS URL_COMPONENTSW; +typedef LPURL_COMPONENTS LPURL_COMPONENTSW; + +typedef struct +{ + DWORD_PTR dwResult; + DWORD dwError; +} WINHTTP_ASYNC_RESULT, *LPWINHTTP_ASYNC_RESULT; + +typedef struct +{ + FILETIME ftExpiry; + FILETIME ftStart; + LPWSTR lpszSubjectInfo; + LPWSTR lpszIssuerInfo; + LPWSTR lpszProtocolName; + LPWSTR lpszSignatureAlgName; + LPWSTR lpszEncryptionAlgName; + DWORD dwKeySize; +} WINHTTP_CERTIFICATE_INFO; + +typedef struct +{ + DWORD dwAccessType; + LPWSTR lpszProxy; + LPWSTR lpszProxyBypass; +} WINHTTP_PROXY_INFO, *LPWINHTTP_PROXY_INFO; +typedef WINHTTP_PROXY_INFO WINHTTP_PROXY_INFOW; +typedef LPWINHTTP_PROXY_INFO LPWINHTTP_PROXY_INFOW; + +typedef struct +{ + BOOL fAutoDetect; + LPWSTR lpszAutoConfigUrl; + LPWSTR lpszProxy; + LPWSTR lpszProxyBypass; +} WINHTTP_CURRENT_USER_IE_PROXY_CONFIG; + +typedef VOID (CALLBACK *WINHTTP_STATUS_CALLBACK)(HINTERNET,DWORD_PTR,DWORD,LPVOID,DWORD); + +#define WINHTTP_AUTO_DETECT_TYPE_DHCP 0x00000001 +#define WINHTTP_AUTO_DETECT_TYPE_DNS_A 0x00000002 + +#define WINHTTP_AUTOPROXY_AUTO_DETECT 0x00000001 +#define WINHTTP_AUTOPROXY_CONFIG_URL 0x00000002 +#define WINHTTP_AUTOPROXY_RUN_INPROCESS 0x00010000 +#define WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY 0x00020000 + +typedef struct +{ + DWORD dwFlags; + DWORD dwAutoDetectFlags; + LPCWSTR lpszAutoConfigUrl; + LPVOID lpvReserved; + DWORD dwReserved; + BOOL fAutoLogonIfChallenged; +} WINHTTP_AUTOPROXY_OPTIONS; + +typedef struct +{ + DWORD dwMajorVersion; + DWORD dwMinorVersion; +} HTTP_VERSION_INFO, *LPHTTP_VERSION_INFO; + +#ifdef _WS2DEF_ +typedef struct +{ + DWORD cbSize; + SOCKADDR_STORAGE LocalAddress; + SOCKADDR_STORAGE RemoteAddress; +} WINHTTP_CONNECTION_INFO; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +BOOL WINAPI WinHttpAddRequestHeaders(HINTERNET,LPCWSTR,DWORD,DWORD); +BOOL WINAPI WinHttpDetectAutoProxyConfigUrl(DWORD,LPWSTR*); +BOOL WINAPI WinHttpCheckPlatform(void); +BOOL WINAPI WinHttpCloseHandle(HINTERNET); +HINTERNET WINAPI WinHttpConnect(HINTERNET,LPCWSTR,INTERNET_PORT,DWORD); +BOOL WINAPI WinHttpCrackUrl(LPCWSTR,DWORD,DWORD,LPURL_COMPONENTS); +BOOL WINAPI WinHttpCreateUrl(LPURL_COMPONENTS,DWORD,LPWSTR,LPDWORD); +BOOL WINAPI WinHttpGetDefaultProxyConfiguration(WINHTTP_PROXY_INFO*); +BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser(WINHTTP_CURRENT_USER_IE_PROXY_CONFIG*); +BOOL WINAPI WinHttpGetProxyForUrl(HINTERNET,LPCWSTR,WINHTTP_AUTOPROXY_OPTIONS*,WINHTTP_PROXY_INFO*); +HINTERNET WINAPI WinHttpOpen(LPCWSTR,DWORD,LPCWSTR,LPCWSTR,DWORD); +HINTERNET WINAPI WinHttpOpenRequest(HINTERNET,LPCWSTR,LPCWSTR,LPCWSTR,LPCWSTR,LPCWSTR*,DWORD); +BOOL WINAPI WinHttpQueryAuthParams(HINTERNET,DWORD,LPVOID*); +BOOL WINAPI WinHttpQueryAuthSchemes(HINTERNET,LPDWORD,LPDWORD,LPDWORD); +BOOL WINAPI WinHttpQueryDataAvailable(HINTERNET,LPDWORD); +BOOL WINAPI WinHttpQueryHeaders(HINTERNET,DWORD,LPCWSTR,LPVOID,LPDWORD,LPDWORD); +BOOL WINAPI WinHttpQueryOption(HINTERNET,DWORD,LPVOID,LPDWORD); +BOOL WINAPI WinHttpReadData(HINTERNET,LPVOID,DWORD,LPDWORD); +BOOL WINAPI WinHttpReceiveResponse(HINTERNET,LPVOID); +BOOL WINAPI WinHttpSendRequest(HINTERNET,LPCWSTR,DWORD,LPVOID,DWORD,DWORD,DWORD_PTR); +BOOL WINAPI WinHttpSetDefaultProxyConfiguration(WINHTTP_PROXY_INFO*); +BOOL WINAPI WinHttpSetCredentials(HINTERNET,DWORD,DWORD,LPCWSTR,LPCWSTR,LPVOID); +BOOL WINAPI WinHttpSetOption(HINTERNET,DWORD,LPVOID,DWORD); +WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback(HINTERNET,WINHTTP_STATUS_CALLBACK,DWORD,DWORD_PTR); +BOOL WINAPI WinHttpSetTimeouts(HINTERNET,int,int,int,int); +BOOL WINAPI WinHttpTimeFromSystemTime(const SYSTEMTIME *,LPWSTR); +BOOL WINAPI WinHttpTimeToSystemTime(LPCWSTR,SYSTEMTIME*); +BOOL WINAPI WinHttpWriteData(HINTERNET,LPCVOID,DWORD,LPDWORD); + +#ifdef __cplusplus +} +#endif + +#include + +#endif /* __WINE_WINHTTP_H */ + +#else + +#include_next + +#endif diff --git a/deps/winhttp/winhttp64.def b/deps/winhttp/winhttp64.def new file mode 100644 index 00000000000..bfad3a0cef5 --- /dev/null +++ b/deps/winhttp/winhttp64.def @@ -0,0 +1,29 @@ +LIBRARY WINHTTP +EXPORTS +WinHttpAddRequestHeaders +WinHttpCheckPlatform +WinHttpCloseHandle +WinHttpConnect +WinHttpCrackUrl +WinHttpCreateUrl +WinHttpDetectAutoProxyConfigUrl +WinHttpGetDefaultProxyConfiguration +WinHttpGetIEProxyConfigForCurrentUser +WinHttpGetProxyForUrl +WinHttpOpen +WinHttpOpenRequest +WinHttpQueryAuthSchemes +WinHttpQueryDataAvailable +WinHttpQueryHeaders +WinHttpQueryOption +WinHttpReadData +WinHttpReceiveResponse +WinHttpSendRequest +WinHttpSetCredentials +WinHttpSetDefaultProxyConfiguration +WinHttpSetOption +WinHttpSetStatusCallback +WinHttpSetTimeouts +WinHttpTimeFromSystemTime +WinHttpTimeToSystemTime +WinHttpWriteData diff --git a/deps/xdiff/CMakeLists.txt b/deps/xdiff/CMakeLists.txt new file mode 100644 index 00000000000..743ac636f0a --- /dev/null +++ b/deps/xdiff/CMakeLists.txt @@ -0,0 +1,28 @@ + +file(GLOB SRC_XDIFF "*.c" "*.h") +list(SORT SRC_XDIFF) + +add_library(xdiff OBJECT ${SRC_XDIFF}) +target_include_directories(xdiff SYSTEM PRIVATE + "${PROJECT_SOURCE_DIR}/include" + "${PROJECT_SOURCE_DIR}/src/util" + "${PROJECT_BINARY_DIR}/src/util" + ${LIBGIT2_SYSTEM_INCLUDES} + ${LIBGIT2_DEPENDENCY_INCLUDES}) + +# the xdiff dependency is not (yet) warning-free, disable warnings +# as errors for the xdiff sources until we've sorted them out +if(MSVC) + set_source_files_properties(xdiffi.c PROPERTIES COMPILE_FLAGS -WX-) + set_source_files_properties(xemit.c PROPERTIES COMPILE_FLAGS -WX-) + set_source_files_properties(xhistogram.c PROPERTIES COMPILE_FLAGS -WX-) + set_source_files_properties(xmerge.c PROPERTIES COMPILE_FLAGS -WX-) + set_source_files_properties(xutils.c PROPERTIES COMPILE_FLAGS -WX-) + set_source_files_properties(xpatience.c PROPERTIES COMPILE_FLAGS -WX-) +else() + set_source_files_properties(xdiffi.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare -Wno-unused-parameter") + set_source_files_properties(xemit.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare -Wno-unused-parameter") + set_source_files_properties(xhistogram.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare") + set_source_files_properties(xutils.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare") + set_source_files_properties(xpatience.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare") +endif() diff --git a/deps/xdiff/git-xdiff.h b/deps/xdiff/git-xdiff.h new file mode 100644 index 00000000000..1450ab3dd21 --- /dev/null +++ b/deps/xdiff/git-xdiff.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +/* + * This file provides the necessary indirection between xdiff and + * libgit2. libgit2-specific functionality should live here, so + * that git and libgit2 can share a common xdiff implementation. + */ + +#ifndef INCLUDE_git_xdiff_h__ +#define INCLUDE_git_xdiff_h__ + +#include "regexp.h" + +/* Work around C90-conformance issues */ +#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) +# if defined(_MSC_VER) +# define inline __inline +# elif defined(__GNUC__) +# define inline __inline__ +# else +# define inline +# endif +#endif + +#define XDL_UNUSED GIT_UNUSED_ARG + +#define xdl_malloc(x) git__malloc(x) +#define xdl_calloc(n, sz) git__calloc(n, sz) +#define xdl_free(ptr) git__free(ptr) +#define xdl_realloc(ptr, x) git__realloc(ptr, x) + +#define XDL_BUG(msg) GIT_ASSERT(!msg) + +#define xdl_regex_t git_regexp +#define xdl_regmatch_t git_regmatch + +GIT_INLINE(int) xdl_regexec_buf( + const xdl_regex_t *preg, const char *buf, size_t size, + size_t nmatch, xdl_regmatch_t pmatch[], int eflags) +{ + GIT_UNUSED(preg); + GIT_UNUSED(buf); + GIT_UNUSED(size); + GIT_UNUSED(nmatch); + GIT_UNUSED(pmatch); + GIT_UNUSED(eflags); + GIT_ASSERT("not implemented"); + return -1; +} + +#endif diff --git a/deps/xdiff/xdiff.h b/deps/xdiff/xdiff.h new file mode 100644 index 00000000000..fb47f63fbf7 --- /dev/null +++ b/deps/xdiff/xdiff.h @@ -0,0 +1,150 @@ +/* + * LibXDiff by Davide Libenzi ( File Differential Library ) + * Copyright (C) 2003 Davide Libenzi + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see + * . + * + * Davide Libenzi + * + */ + +#if !defined(XDIFF_H) +#define XDIFF_H + +#ifdef __cplusplus +extern "C" { +#endif /* #ifdef __cplusplus */ + +#include "git-xdiff.h" + +/* xpparm_t.flags */ +#define XDF_NEED_MINIMAL (1 << 0) + +#define XDF_IGNORE_WHITESPACE (1 << 1) +#define XDF_IGNORE_WHITESPACE_CHANGE (1 << 2) +#define XDF_IGNORE_WHITESPACE_AT_EOL (1 << 3) +#define XDF_IGNORE_CR_AT_EOL (1 << 4) +#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | \ + XDF_IGNORE_WHITESPACE_CHANGE | \ + XDF_IGNORE_WHITESPACE_AT_EOL | \ + XDF_IGNORE_CR_AT_EOL) + +#define XDF_IGNORE_BLANK_LINES (1 << 7) + +#define XDF_PATIENCE_DIFF (1 << 14) +#define XDF_HISTOGRAM_DIFF (1 << 15) +#define XDF_DIFF_ALGORITHM_MASK (XDF_PATIENCE_DIFF | XDF_HISTOGRAM_DIFF) +#define XDF_DIFF_ALG(x) ((x) & XDF_DIFF_ALGORITHM_MASK) + +#define XDF_INDENT_HEURISTIC (1 << 23) + +/* xdemitconf_t.flags */ +#define XDL_EMIT_FUNCNAMES (1 << 0) +#define XDL_EMIT_NO_HUNK_HDR (1 << 1) +#define XDL_EMIT_FUNCCONTEXT (1 << 2) + +/* merge simplification levels */ +#define XDL_MERGE_MINIMAL 0 +#define XDL_MERGE_EAGER 1 +#define XDL_MERGE_ZEALOUS 2 +#define XDL_MERGE_ZEALOUS_ALNUM 3 + +/* merge favor modes */ +#define XDL_MERGE_FAVOR_OURS 1 +#define XDL_MERGE_FAVOR_THEIRS 2 +#define XDL_MERGE_FAVOR_UNION 3 + +/* merge output styles */ +#define XDL_MERGE_DIFF3 1 +#define XDL_MERGE_ZEALOUS_DIFF3 2 + +typedef struct s_mmfile { + char *ptr; + long size; +} mmfile_t; + +typedef struct s_mmbuffer { + char *ptr; + long size; +} mmbuffer_t; + +typedef struct s_xpparam { + unsigned long flags; + + /* -I */ + xdl_regex_t **ignore_regex; + size_t ignore_regex_nr; + + /* See Documentation/diff-options.txt. */ + char **anchors; + size_t anchors_nr; +} xpparam_t; + +typedef struct s_xdemitcb { + void *priv; + int (*out_hunk)(void *, + long old_begin, long old_nr, + long new_begin, long new_nr, + const char *func, long funclen); + int (*out_line)(void *, mmbuffer_t *, int); +} xdemitcb_t; + +typedef long (*find_func_t)(const char *line, long line_len, char *buffer, long buffer_size, void *priv); + +typedef int (*xdl_emit_hunk_consume_func_t)(long start_a, long count_a, + long start_b, long count_b, + void *cb_data); + +typedef struct s_xdemitconf { + long ctxlen; + long interhunkctxlen; + unsigned long flags; + find_func_t find_func; + void *find_func_priv; + xdl_emit_hunk_consume_func_t hunk_func; +} xdemitconf_t; + +typedef struct s_bdiffparam { + long bsize; +} bdiffparam_t; + + +void *xdl_mmfile_first(mmfile_t *mmf, long *size); +long xdl_mmfile_size(mmfile_t *mmf); + +int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, + xdemitconf_t const *xecfg, xdemitcb_t *ecb); + +typedef struct s_xmparam { + xpparam_t xpp; + int marker_size; + int level; + int favor; + int style; + const char *ancestor; /* label for orig */ + const char *file1; /* label for mf1 */ + const char *file2; /* label for mf2 */ +} xmparam_t; + +#define DEFAULT_CONFLICT_MARKER_SIZE 7 + +int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2, + xmparam_t const *xmp, mmbuffer_t *result); + +#ifdef __cplusplus +} +#endif /* #ifdef __cplusplus */ + +#endif /* #if !defined(XDIFF_H) */ diff --git a/deps/xdiff/xdiffi.c b/deps/xdiff/xdiffi.c new file mode 100644 index 00000000000..ea36143af2c --- /dev/null +++ b/deps/xdiff/xdiffi.c @@ -0,0 +1,1089 @@ +/* + * LibXDiff by Davide Libenzi ( File Differential Library ) + * Copyright (C) 2003 Davide Libenzi + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see + * . + * + * Davide Libenzi + * + */ + +#include "xinclude.h" + +#define XDL_MAX_COST_MIN 256 +#define XDL_HEUR_MIN_COST 256 +#define XDL_LINE_MAX (long)((1UL << (CHAR_BIT * sizeof(long) - 1)) - 1) +#define XDL_SNAKE_CNT 20 +#define XDL_K_HEUR 4 + +typedef struct s_xdpsplit { + long i1, i2; + int min_lo, min_hi; +} xdpsplit_t; + +/* + * See "An O(ND) Difference Algorithm and its Variations", by Eugene Myers. + * Basically considers a "box" (off1, off2, lim1, lim2) and scan from both + * the forward diagonal starting from (off1, off2) and the backward diagonal + * starting from (lim1, lim2). If the K values on the same diagonal crosses + * returns the furthest point of reach. We might encounter expensive edge cases + * using this algorithm, so a little bit of heuristic is needed to cut the + * search and to return a suboptimal point. + */ +static long xdl_split(unsigned long const *ha1, long off1, long lim1, + unsigned long const *ha2, long off2, long lim2, + long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl, + xdalgoenv_t *xenv) { + long dmin = off1 - lim2, dmax = lim1 - off2; + long fmid = off1 - off2, bmid = lim1 - lim2; + long odd = (fmid - bmid) & 1; + long fmin = fmid, fmax = fmid; + long bmin = bmid, bmax = bmid; + long ec, d, i1, i2, prev1, best, dd, v, k; + + /* + * Set initial diagonal values for both forward and backward path. + */ + kvdf[fmid] = off1; + kvdb[bmid] = lim1; + + for (ec = 1;; ec++) { + int got_snake = 0; + + /* + * We need to extend the diagonal "domain" by one. If the next + * values exits the box boundaries we need to change it in the + * opposite direction because (max - min) must be a power of + * two. + * + * Also we initialize the external K value to -1 so that we can + * avoid extra conditions in the check inside the core loop. + */ + if (fmin > dmin) + kvdf[--fmin - 1] = -1; + else + ++fmin; + if (fmax < dmax) + kvdf[++fmax + 1] = -1; + else + --fmax; + + for (d = fmax; d >= fmin; d -= 2) { + if (kvdf[d - 1] >= kvdf[d + 1]) + i1 = kvdf[d - 1] + 1; + else + i1 = kvdf[d + 1]; + prev1 = i1; + i2 = i1 - d; + for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++); + if (i1 - prev1 > xenv->snake_cnt) + got_snake = 1; + kvdf[d] = i1; + if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) { + spl->i1 = i1; + spl->i2 = i2; + spl->min_lo = spl->min_hi = 1; + return ec; + } + } + + /* + * We need to extend the diagonal "domain" by one. If the next + * values exits the box boundaries we need to change it in the + * opposite direction because (max - min) must be a power of + * two. + * + * Also we initialize the external K value to -1 so that we can + * avoid extra conditions in the check inside the core loop. + */ + if (bmin > dmin) + kvdb[--bmin - 1] = XDL_LINE_MAX; + else + ++bmin; + if (bmax < dmax) + kvdb[++bmax + 1] = XDL_LINE_MAX; + else + --bmax; + + for (d = bmax; d >= bmin; d -= 2) { + if (kvdb[d - 1] < kvdb[d + 1]) + i1 = kvdb[d - 1]; + else + i1 = kvdb[d + 1] - 1; + prev1 = i1; + i2 = i1 - d; + for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--); + if (prev1 - i1 > xenv->snake_cnt) + got_snake = 1; + kvdb[d] = i1; + if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) { + spl->i1 = i1; + spl->i2 = i2; + spl->min_lo = spl->min_hi = 1; + return ec; + } + } + + if (need_min) + continue; + + /* + * If the edit cost is above the heuristic trigger and if + * we got a good snake, we sample current diagonals to see + * if some of them have reached an "interesting" path. Our + * measure is a function of the distance from the diagonal + * corner (i1 + i2) penalized with the distance from the + * mid diagonal itself. If this value is above the current + * edit cost times a magic factor (XDL_K_HEUR) we consider + * it interesting. + */ + if (got_snake && ec > xenv->heur_min) { + for (best = 0, d = fmax; d >= fmin; d -= 2) { + dd = d > fmid ? d - fmid: fmid - d; + i1 = kvdf[d]; + i2 = i1 - d; + v = (i1 - off1) + (i2 - off2) - dd; + + if (v > XDL_K_HEUR * ec && v > best && + off1 + xenv->snake_cnt <= i1 && i1 < lim1 && + off2 + xenv->snake_cnt <= i2 && i2 < lim2) { + for (k = 1; ha1[i1 - k] == ha2[i2 - k]; k++) + if (k == xenv->snake_cnt) { + best = v; + spl->i1 = i1; + spl->i2 = i2; + break; + } + } + } + if (best > 0) { + spl->min_lo = 1; + spl->min_hi = 0; + return ec; + } + + for (best = 0, d = bmax; d >= bmin; d -= 2) { + dd = d > bmid ? d - bmid: bmid - d; + i1 = kvdb[d]; + i2 = i1 - d; + v = (lim1 - i1) + (lim2 - i2) - dd; + + if (v > XDL_K_HEUR * ec && v > best && + off1 < i1 && i1 <= lim1 - xenv->snake_cnt && + off2 < i2 && i2 <= lim2 - xenv->snake_cnt) { + for (k = 0; ha1[i1 + k] == ha2[i2 + k]; k++) + if (k == xenv->snake_cnt - 1) { + best = v; + spl->i1 = i1; + spl->i2 = i2; + break; + } + } + } + if (best > 0) { + spl->min_lo = 0; + spl->min_hi = 1; + return ec; + } + } + + /* + * Enough is enough. We spent too much time here and now we + * collect the furthest reaching path using the (i1 + i2) + * measure. + */ + if (ec >= xenv->mxcost) { + long fbest, fbest1, bbest, bbest1; + + fbest = fbest1 = -1; + for (d = fmax; d >= fmin; d -= 2) { + i1 = XDL_MIN(kvdf[d], lim1); + i2 = i1 - d; + if (lim2 < i2) + i1 = lim2 + d, i2 = lim2; + if (fbest < i1 + i2) { + fbest = i1 + i2; + fbest1 = i1; + } + } + + bbest = bbest1 = XDL_LINE_MAX; + for (d = bmax; d >= bmin; d -= 2) { + i1 = XDL_MAX(off1, kvdb[d]); + i2 = i1 - d; + if (i2 < off2) + i1 = off2 + d, i2 = off2; + if (i1 + i2 < bbest) { + bbest = i1 + i2; + bbest1 = i1; + } + } + + if ((lim1 + lim2) - bbest < fbest - (off1 + off2)) { + spl->i1 = fbest1; + spl->i2 = fbest - fbest1; + spl->min_lo = 1; + spl->min_hi = 0; + } else { + spl->i1 = bbest1; + spl->i2 = bbest - bbest1; + spl->min_lo = 0; + spl->min_hi = 1; + } + return ec; + } + } +} + + +/* + * Rule: "Divide et Impera" (divide & conquer). Recursively split the box in + * sub-boxes by calling the box splitting function. Note that the real job + * (marking changed lines) is done in the two boundary reaching checks. + */ +int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1, + diffdata_t *dd2, long off2, long lim2, + long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv) { + unsigned long const *ha1 = dd1->ha, *ha2 = dd2->ha; + + /* + * Shrink the box by walking through each diagonal snake (SW and NE). + */ + for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++); + for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--); + + /* + * If one dimension is empty, then all records on the other one must + * be obviously changed. + */ + if (off1 == lim1) { + char *rchg2 = dd2->rchg; + long *rindex2 = dd2->rindex; + + for (; off2 < lim2; off2++) + rchg2[rindex2[off2]] = 1; + } else if (off2 == lim2) { + char *rchg1 = dd1->rchg; + long *rindex1 = dd1->rindex; + + for (; off1 < lim1; off1++) + rchg1[rindex1[off1]] = 1; + } else { + xdpsplit_t spl; + spl.i1 = spl.i2 = 0; + + /* + * Divide ... + */ + if (xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb, + need_min, &spl, xenv) < 0) { + + return -1; + } + + /* + * ... et Impera. + */ + if (xdl_recs_cmp(dd1, off1, spl.i1, dd2, off2, spl.i2, + kvdf, kvdb, spl.min_lo, xenv) < 0 || + xdl_recs_cmp(dd1, spl.i1, lim1, dd2, spl.i2, lim2, + kvdf, kvdb, spl.min_hi, xenv) < 0) { + + return -1; + } + } + + return 0; +} + + +int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, + xdfenv_t *xe) { + long ndiags; + long *kvd, *kvdf, *kvdb; + xdalgoenv_t xenv; + diffdata_t dd1, dd2; + int res; + + if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) + return -1; + + if (XDF_DIFF_ALG(xpp->flags) == XDF_PATIENCE_DIFF) { + res = xdl_do_patience_diff(xpp, xe); + goto out; + } + + if (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF) { + res = xdl_do_histogram_diff(xpp, xe); + goto out; + } + + /* + * Allocate and setup K vectors to be used by the differential + * algorithm. + * + * One is to store the forward path and one to store the backward path. + */ + ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3; + if (!XDL_ALLOC_ARRAY(kvd, 2 * ndiags + 2)) { + + xdl_free_env(xe); + return -1; + } + kvdf = kvd; + kvdb = kvdf + ndiags; + kvdf += xe->xdf2.nreff + 1; + kvdb += xe->xdf2.nreff + 1; + + xenv.mxcost = xdl_bogosqrt(ndiags); + if (xenv.mxcost < XDL_MAX_COST_MIN) + xenv.mxcost = XDL_MAX_COST_MIN; + xenv.snake_cnt = XDL_SNAKE_CNT; + xenv.heur_min = XDL_HEUR_MIN_COST; + + dd1.nrec = xe->xdf1.nreff; + dd1.ha = xe->xdf1.ha; + dd1.rchg = xe->xdf1.rchg; + dd1.rindex = xe->xdf1.rindex; + dd2.nrec = xe->xdf2.nreff; + dd2.ha = xe->xdf2.ha; + dd2.rchg = xe->xdf2.rchg; + dd2.rindex = xe->xdf2.rindex; + + res = xdl_recs_cmp(&dd1, 0, dd1.nrec, &dd2, 0, dd2.nrec, + kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) != 0, + &xenv); + xdl_free(kvd); + out: + if (res < 0) + xdl_free_env(xe); + + return res; +} + + +static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2) { + xdchange_t *xch; + + if (!(xch = (xdchange_t *) xdl_malloc(sizeof(xdchange_t)))) + return NULL; + + xch->next = xscr; + xch->i1 = i1; + xch->i2 = i2; + xch->chg1 = chg1; + xch->chg2 = chg2; + xch->ignore = 0; + + return xch; +} + + +static int recs_match(xrecord_t *rec1, xrecord_t *rec2) +{ + return (rec1->ha == rec2->ha); +} + +/* + * If a line is indented more than this, get_indent() just returns this value. + * This avoids having to do absurd amounts of work for data that are not + * human-readable text, and also ensures that the output of get_indent fits + * within an int. + */ +#define MAX_INDENT 200 + +/* + * Return the amount of indentation of the specified line, treating TAB as 8 + * columns. Return -1 if line is empty or contains only whitespace. Clamp the + * output value at MAX_INDENT. + */ +static int get_indent(xrecord_t *rec) +{ + long i; + int ret = 0; + + for (i = 0; i < rec->size; i++) { + char c = rec->ptr[i]; + + if (!XDL_ISSPACE(c)) + return ret; + else if (c == ' ') + ret += 1; + else if (c == '\t') + ret += 8 - ret % 8; + /* ignore other whitespace characters */ + + if (ret >= MAX_INDENT) + return MAX_INDENT; + } + + /* The line contains only whitespace. */ + return -1; +} + +/* + * If more than this number of consecutive blank rows are found, just return + * this value. This avoids requiring O(N^2) work for pathological cases, and + * also ensures that the output of score_split fits in an int. + */ +#define MAX_BLANKS 20 + +/* Characteristics measured about a hypothetical split position. */ +struct split_measurement { + /* + * Is the split at the end of the file (aside from any blank lines)? + */ + int end_of_file; + + /* + * How much is the line immediately following the split indented (or -1 + * if the line is blank): + */ + int indent; + + /* + * How many consecutive lines above the split are blank? + */ + int pre_blank; + + /* + * How much is the nearest non-blank line above the split indented (or + * -1 if there is no such line)? + */ + int pre_indent; + + /* + * How many lines after the line following the split are blank? + */ + int post_blank; + + /* + * How much is the nearest non-blank line after the line following the + * split indented (or -1 if there is no such line)? + */ + int post_indent; +}; + +struct split_score { + /* The effective indent of this split (smaller is preferred). */ + int effective_indent; + + /* Penalty for this split (smaller is preferred). */ + int penalty; +}; + +/* + * Fill m with information about a hypothetical split of xdf above line split. + */ +static void measure_split(const xdfile_t *xdf, long split, + struct split_measurement *m) +{ + long i; + + if (split >= xdf->nrec) { + m->end_of_file = 1; + m->indent = -1; + } else { + m->end_of_file = 0; + m->indent = get_indent(xdf->recs[split]); + } + + m->pre_blank = 0; + m->pre_indent = -1; + for (i = split - 1; i >= 0; i--) { + m->pre_indent = get_indent(xdf->recs[i]); + if (m->pre_indent != -1) + break; + m->pre_blank += 1; + if (m->pre_blank == MAX_BLANKS) { + m->pre_indent = 0; + break; + } + } + + m->post_blank = 0; + m->post_indent = -1; + for (i = split + 1; i < xdf->nrec; i++) { + m->post_indent = get_indent(xdf->recs[i]); + if (m->post_indent != -1) + break; + m->post_blank += 1; + if (m->post_blank == MAX_BLANKS) { + m->post_indent = 0; + break; + } + } +} + +/* + * The empirically-determined weight factors used by score_split() below. + * Larger values means that the position is a less favorable place to split. + * + * Note that scores are only ever compared against each other, so multiplying + * all of these weight/penalty values by the same factor wouldn't change the + * heuristic's behavior. Still, we need to set that arbitrary scale *somehow*. + * In practice, these numbers are chosen to be large enough that they can be + * adjusted relative to each other with sufficient precision despite using + * integer math. + */ + +/* Penalty if there are no non-blank lines before the split */ +#define START_OF_FILE_PENALTY 1 + +/* Penalty if there are no non-blank lines after the split */ +#define END_OF_FILE_PENALTY 21 + +/* Multiplier for the number of blank lines around the split */ +#define TOTAL_BLANK_WEIGHT (-30) + +/* Multiplier for the number of blank lines after the split */ +#define POST_BLANK_WEIGHT 6 + +/* + * Penalties applied if the line is indented more than its predecessor + */ +#define RELATIVE_INDENT_PENALTY (-4) +#define RELATIVE_INDENT_WITH_BLANK_PENALTY 10 + +/* + * Penalties applied if the line is indented less than both its predecessor and + * its successor + */ +#define RELATIVE_OUTDENT_PENALTY 24 +#define RELATIVE_OUTDENT_WITH_BLANK_PENALTY 17 + +/* + * Penalties applied if the line is indented less than its predecessor but not + * less than its successor + */ +#define RELATIVE_DEDENT_PENALTY 23 +#define RELATIVE_DEDENT_WITH_BLANK_PENALTY 17 + +/* + * We only consider whether the sum of the effective indents for splits are + * less than (-1), equal to (0), or greater than (+1) each other. The resulting + * value is multiplied by the following weight and combined with the penalty to + * determine the better of two scores. + */ +#define INDENT_WEIGHT 60 + +/* + * How far do we slide a hunk at most? + */ +#define INDENT_HEURISTIC_MAX_SLIDING 100 + +/* + * Compute a badness score for the hypothetical split whose measurements are + * stored in m. The weight factors were determined empirically using the tools + * and corpus described in + * + * https://github.com/mhagger/diff-slider-tools + * + * Also see that project if you want to improve the weights based on, for + * example, a larger or more diverse corpus. + */ +static void score_add_split(const struct split_measurement *m, struct split_score *s) +{ + /* + * A place to accumulate penalty factors (positive makes this index more + * favored): + */ + int post_blank, total_blank, indent, any_blanks; + + if (m->pre_indent == -1 && m->pre_blank == 0) + s->penalty += START_OF_FILE_PENALTY; + + if (m->end_of_file) + s->penalty += END_OF_FILE_PENALTY; + + /* + * Set post_blank to the number of blank lines following the split, + * including the line immediately after the split: + */ + post_blank = (m->indent == -1) ? 1 + m->post_blank : 0; + total_blank = m->pre_blank + post_blank; + + /* Penalties based on nearby blank lines: */ + s->penalty += TOTAL_BLANK_WEIGHT * total_blank; + s->penalty += POST_BLANK_WEIGHT * post_blank; + + if (m->indent != -1) + indent = m->indent; + else + indent = m->post_indent; + + any_blanks = (total_blank != 0); + + /* Note that the effective indent is -1 at the end of the file: */ + s->effective_indent += indent; + + if (indent == -1) { + /* No additional adjustments needed. */ + } else if (m->pre_indent == -1) { + /* No additional adjustments needed. */ + } else if (indent > m->pre_indent) { + /* + * The line is indented more than its predecessor. + */ + s->penalty += any_blanks ? + RELATIVE_INDENT_WITH_BLANK_PENALTY : + RELATIVE_INDENT_PENALTY; + } else if (indent == m->pre_indent) { + /* + * The line has the same indentation level as its predecessor. + * No additional adjustments needed. + */ + } else { + /* + * The line is indented less than its predecessor. It could be + * the block terminator of the previous block, but it could + * also be the start of a new block (e.g., an "else" block, or + * maybe the previous block didn't have a block terminator). + * Try to distinguish those cases based on what comes next: + */ + if (m->post_indent != -1 && m->post_indent > indent) { + /* + * The following line is indented more. So it is likely + * that this line is the start of a block. + */ + s->penalty += any_blanks ? + RELATIVE_OUTDENT_WITH_BLANK_PENALTY : + RELATIVE_OUTDENT_PENALTY; + } else { + /* + * That was probably the end of a block. + */ + s->penalty += any_blanks ? + RELATIVE_DEDENT_WITH_BLANK_PENALTY : + RELATIVE_DEDENT_PENALTY; + } + } +} + +static int score_cmp(struct split_score *s1, struct split_score *s2) +{ + /* -1 if s1.effective_indent < s2->effective_indent, etc. */ + int cmp_indents = ((s1->effective_indent > s2->effective_indent) - + (s1->effective_indent < s2->effective_indent)); + + return INDENT_WEIGHT * cmp_indents + (s1->penalty - s2->penalty); +} + +/* + * Represent a group of changed lines in an xdfile_t (i.e., a contiguous group + * of lines that was inserted or deleted from the corresponding version of the + * file). We consider there to be such a group at the beginning of the file, at + * the end of the file, and between any two unchanged lines, though most such + * groups will usually be empty. + * + * If the first line in a group is equal to the line following the group, then + * the group can be slid down. Similarly, if the last line in a group is equal + * to the line preceding the group, then the group can be slid up. See + * group_slide_down() and group_slide_up(). + * + * Note that loops that are testing for changed lines in xdf->rchg do not need + * index bounding since the array is prepared with a zero at position -1 and N. + */ +struct xdlgroup { + /* + * The index of the first changed line in the group, or the index of + * the unchanged line above which the (empty) group is located. + */ + long start; + + /* + * The index of the first unchanged line after the group. For an empty + * group, end is equal to start. + */ + long end; +}; + +/* + * Initialize g to point at the first group in xdf. + */ +static void group_init(xdfile_t *xdf, struct xdlgroup *g) +{ + g->start = g->end = 0; + while (xdf->rchg[g->end]) + g->end++; +} + +/* + * Move g to describe the next (possibly empty) group in xdf and return 0. If g + * is already at the end of the file, do nothing and return -1. + */ +static inline int group_next(xdfile_t *xdf, struct xdlgroup *g) +{ + if (g->end == xdf->nrec) + return -1; + + g->start = g->end + 1; + for (g->end = g->start; xdf->rchg[g->end]; g->end++) + ; + + return 0; +} + +/* + * Move g to describe the previous (possibly empty) group in xdf and return 0. + * If g is already at the beginning of the file, do nothing and return -1. + */ +static inline int group_previous(xdfile_t *xdf, struct xdlgroup *g) +{ + if (g->start == 0) + return -1; + + g->end = g->start - 1; + for (g->start = g->end; xdf->rchg[g->start - 1]; g->start--) + ; + + return 0; +} + +/* + * If g can be slid toward the end of the file, do so, and if it bumps into a + * following group, expand this group to include it. Return 0 on success or -1 + * if g cannot be slid down. + */ +static int group_slide_down(xdfile_t *xdf, struct xdlgroup *g) +{ + if (g->end < xdf->nrec && + recs_match(xdf->recs[g->start], xdf->recs[g->end])) { + xdf->rchg[g->start++] = 0; + xdf->rchg[g->end++] = 1; + + while (xdf->rchg[g->end]) + g->end++; + + return 0; + } else { + return -1; + } +} + +/* + * If g can be slid toward the beginning of the file, do so, and if it bumps + * into a previous group, expand this group to include it. Return 0 on success + * or -1 if g cannot be slid up. + */ +static int group_slide_up(xdfile_t *xdf, struct xdlgroup *g) +{ + if (g->start > 0 && + recs_match(xdf->recs[g->start - 1], xdf->recs[g->end - 1])) { + xdf->rchg[--g->start] = 1; + xdf->rchg[--g->end] = 0; + + while (xdf->rchg[g->start - 1]) + g->start--; + + return 0; + } else { + return -1; + } +} + +/* + * Move back and forward change groups for a consistent and pretty diff output. + * This also helps in finding joinable change groups and reducing the diff + * size. + */ +int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) { + struct xdlgroup g, go; + long earliest_end, end_matching_other; + long groupsize; + + group_init(xdf, &g); + group_init(xdfo, &go); + + while (1) { + /* + * If the group is empty in the to-be-compacted file, skip it: + */ + if (g.end == g.start) + goto next; + + /* + * Now shift the change up and then down as far as possible in + * each direction. If it bumps into any other changes, merge + * them. + */ + do { + groupsize = g.end - g.start; + + /* + * Keep track of the last "end" index that causes this + * group to align with a group of changed lines in the + * other file. -1 indicates that we haven't found such + * a match yet: + */ + end_matching_other = -1; + + /* Shift the group backward as much as possible: */ + while (!group_slide_up(xdf, &g)) + if (group_previous(xdfo, &go)) + XDL_BUG("group sync broken sliding up"); + + /* + * This is this highest that this group can be shifted. + * Record its end index: + */ + earliest_end = g.end; + + if (go.end > go.start) + end_matching_other = g.end; + + /* Now shift the group forward as far as possible: */ + while (1) { + if (group_slide_down(xdf, &g)) + break; + if (group_next(xdfo, &go)) + XDL_BUG("group sync broken sliding down"); + + if (go.end > go.start) + end_matching_other = g.end; + } + } while (groupsize != g.end - g.start); + + /* + * If the group can be shifted, then we can possibly use this + * freedom to produce a more intuitive diff. + * + * The group is currently shifted as far down as possible, so + * the heuristics below only have to handle upwards shifts. + */ + + if (g.end == earliest_end) { + /* no shifting was possible */ + } else if (end_matching_other != -1) { + /* + * Move the possibly merged group of changes back to + * line up with the last group of changes from the + * other file that it can align with. + */ + while (go.end == go.start) { + if (group_slide_up(xdf, &g)) + XDL_BUG("match disappeared"); + if (group_previous(xdfo, &go)) + XDL_BUG("group sync broken sliding to match"); + } + } else if (flags & XDF_INDENT_HEURISTIC) { + /* + * Indent heuristic: a group of pure add/delete lines + * implies two splits, one between the end of the + * "before" context and the start of the group, and + * another between the end of the group and the + * beginning of the "after" context. Some splits are + * aesthetically better and some are worse. We compute + * a badness "score" for each split, and add the scores + * for the two splits to define a "score" for each + * position that the group can be shifted to. Then we + * pick the shift with the lowest score. + */ + long shift, best_shift = -1; + struct split_score best_score; + + shift = earliest_end; + if (g.end - groupsize - 1 > shift) + shift = g.end - groupsize - 1; + if (g.end - INDENT_HEURISTIC_MAX_SLIDING > shift) + shift = g.end - INDENT_HEURISTIC_MAX_SLIDING; + for (; shift <= g.end; shift++) { + struct split_measurement m; + struct split_score score = {0, 0}; + + measure_split(xdf, shift, &m); + score_add_split(&m, &score); + measure_split(xdf, shift - groupsize, &m); + score_add_split(&m, &score); + if (best_shift == -1 || + score_cmp(&score, &best_score) <= 0) { + best_score.effective_indent = score.effective_indent; + best_score.penalty = score.penalty; + best_shift = shift; + } + } + + while (g.end > best_shift) { + if (group_slide_up(xdf, &g)) + XDL_BUG("best shift unreached"); + if (group_previous(xdfo, &go)) + XDL_BUG("group sync broken sliding to blank line"); + } + } + + next: + /* Move past the just-processed group: */ + if (group_next(xdf, &g)) + break; + if (group_next(xdfo, &go)) + XDL_BUG("group sync broken moving to next group"); + } + + if (!group_next(xdfo, &go)) + XDL_BUG("group sync broken at end of file"); + + return 0; +} + + +int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr) { + xdchange_t *cscr = NULL, *xch; + char *rchg1 = xe->xdf1.rchg, *rchg2 = xe->xdf2.rchg; + long i1, i2, l1, l2; + + /* + * Trivial. Collects "groups" of changes and creates an edit script. + */ + for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--) + if (rchg1[i1 - 1] || rchg2[i2 - 1]) { + for (l1 = i1; rchg1[i1 - 1]; i1--); + for (l2 = i2; rchg2[i2 - 1]; i2--); + + if (!(xch = xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) { + xdl_free_script(cscr); + return -1; + } + cscr = xch; + } + + *xscr = cscr; + + return 0; +} + + +void xdl_free_script(xdchange_t *xscr) { + xdchange_t *xch; + + while ((xch = xscr) != NULL) { + xscr = xscr->next; + xdl_free(xch); + } +} + +static int xdl_call_hunk_func(xdfenv_t *xe XDL_UNUSED, xdchange_t *xscr, xdemitcb_t *ecb, + xdemitconf_t const *xecfg) +{ + xdchange_t *xch, *xche; + + for (xch = xscr; xch; xch = xche->next) { + xche = xdl_get_hunk(&xch, xecfg); + if (!xch) + break; + if (xecfg->hunk_func(xch->i1, xche->i1 + xche->chg1 - xch->i1, + xch->i2, xche->i2 + xche->chg2 - xch->i2, + ecb->priv) < 0) + return -1; + } + return 0; +} + +static void xdl_mark_ignorable_lines(xdchange_t *xscr, xdfenv_t *xe, long flags) +{ + xdchange_t *xch; + + for (xch = xscr; xch; xch = xch->next) { + int ignore = 1; + xrecord_t **rec; + long i; + + rec = &xe->xdf1.recs[xch->i1]; + for (i = 0; i < xch->chg1 && ignore; i++) + ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags); + + rec = &xe->xdf2.recs[xch->i2]; + for (i = 0; i < xch->chg2 && ignore; i++) + ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags); + + xch->ignore = ignore; + } +} + +static int record_matches_regex(xrecord_t *rec, xpparam_t const *xpp) { + xdl_regmatch_t regmatch; + int i; + + for (i = 0; i < xpp->ignore_regex_nr; i++) + if (!xdl_regexec_buf(xpp->ignore_regex[i], rec->ptr, rec->size, 1, + ®match, 0)) + return 1; + + return 0; +} + +static void xdl_mark_ignorable_regex(xdchange_t *xscr, const xdfenv_t *xe, + xpparam_t const *xpp) +{ + xdchange_t *xch; + + for (xch = xscr; xch; xch = xch->next) { + xrecord_t **rec; + int ignore = 1; + long i; + + /* + * Do not override --ignore-blank-lines. + */ + if (xch->ignore) + continue; + + rec = &xe->xdf1.recs[xch->i1]; + for (i = 0; i < xch->chg1 && ignore; i++) + ignore = record_matches_regex(rec[i], xpp); + + rec = &xe->xdf2.recs[xch->i2]; + for (i = 0; i < xch->chg2 && ignore; i++) + ignore = record_matches_regex(rec[i], xpp); + + xch->ignore = ignore; + } +} + +int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, + xdemitconf_t const *xecfg, xdemitcb_t *ecb) { + xdchange_t *xscr; + xdfenv_t xe; + emit_func_t ef = xecfg->hunk_func ? xdl_call_hunk_func : xdl_emit_diff; + + if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) { + + return -1; + } + if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 || + xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 || + xdl_build_script(&xe, &xscr) < 0) { + + xdl_free_env(&xe); + return -1; + } + if (xscr) { + if (xpp->flags & XDF_IGNORE_BLANK_LINES) + xdl_mark_ignorable_lines(xscr, &xe, xpp->flags); + + if (xpp->ignore_regex) + xdl_mark_ignorable_regex(xscr, &xe, xpp); + + if (ef(&xe, xscr, ecb, xecfg) < 0) { + + xdl_free_script(xscr); + xdl_free_env(&xe); + return -1; + } + xdl_free_script(xscr); + } + xdl_free_env(&xe); + + return 0; +} diff --git a/src/xdiff/xdiffi.h b/deps/xdiff/xdiffi.h similarity index 82% rename from src/xdiff/xdiffi.h rename to deps/xdiff/xdiffi.h index 7a92ea9c4d8..126c9d8ff4e 100644 --- a/src/xdiff/xdiffi.h +++ b/deps/xdiff/xdiffi.h @@ -13,8 +13,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * License along with this library; if not, see + * . * * Davide Libenzi * @@ -41,6 +41,7 @@ typedef struct s_xdchange { struct s_xdchange *next; long i1, i2; long chg1, chg2; + int ignore; } xdchange_t; @@ -55,9 +56,7 @@ int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr); void xdl_free_script(xdchange_t *xscr); int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, xdemitconf_t const *xecfg); -int xdl_do_patience_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, - xdfenv_t *env); -int xdl_do_histogram_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, - xdfenv_t *env); +int xdl_do_patience_diff(xpparam_t const *xpp, xdfenv_t *env); +int xdl_do_histogram_diff(xpparam_t const *xpp, xdfenv_t *env); #endif /* #if !defined(XDIFFI_H) */ diff --git a/deps/xdiff/xemit.c b/deps/xdiff/xemit.c new file mode 100644 index 00000000000..75f0fe49866 --- /dev/null +++ b/deps/xdiff/xemit.c @@ -0,0 +1,330 @@ +/* + * LibXDiff by Davide Libenzi ( File Differential Library ) + * Copyright (C) 2003 Davide Libenzi + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see + * . + * + * Davide Libenzi + * + */ + +#include "xinclude.h" + +static long xdl_get_rec(xdfile_t *xdf, long ri, char const **rec) { + + *rec = xdf->recs[ri]->ptr; + + return xdf->recs[ri]->size; +} + + +static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *ecb) { + long size, psize = strlen(pre); + char const *rec; + + size = xdl_get_rec(xdf, ri, &rec); + if (xdl_emit_diffrec(rec, size, pre, psize, ecb) < 0) { + + return -1; + } + + return 0; +} + + +/* + * Starting at the passed change atom, find the latest change atom to be included + * inside the differential hunk according to the specified configuration. + * Also advance xscr if the first changes must be discarded. + */ +xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg) +{ + xdchange_t *xch, *xchp, *lxch; + long max_common = 2 * xecfg->ctxlen + xecfg->interhunkctxlen; + long max_ignorable = xecfg->ctxlen; + unsigned long ignored = 0; /* number of ignored blank lines */ + + /* remove ignorable changes that are too far before other changes */ + for (xchp = *xscr; xchp && xchp->ignore; xchp = xchp->next) { + xch = xchp->next; + + if (xch == NULL || + xch->i1 - (xchp->i1 + xchp->chg1) >= max_ignorable) + *xscr = xch; + } + + if (!*xscr) + return NULL; + + lxch = *xscr; + + for (xchp = *xscr, xch = xchp->next; xch; xchp = xch, xch = xch->next) { + long distance = xch->i1 - (xchp->i1 + xchp->chg1); + if (distance > max_common) + break; + + if (distance < max_ignorable && (!xch->ignore || lxch == xchp)) { + lxch = xch; + ignored = 0; + } else if (distance < max_ignorable && xch->ignore) { + ignored += xch->chg2; + } else if (lxch != xchp && + xch->i1 + ignored - (lxch->i1 + lxch->chg1) > max_common) { + break; + } else if (!xch->ignore) { + lxch = xch; + ignored = 0; + } else { + ignored += xch->chg2; + } + } + + return lxch; +} + + +static long def_ff(const char *rec, long len, char *buf, long sz) +{ + if (len > 0 && + (isalpha((unsigned char)*rec) || /* identifier? */ + *rec == '_' || /* also identifier? */ + *rec == '$')) { /* identifiers from VMS and other esoterico */ + if (len > sz) + len = sz; + while (0 < len && isspace((unsigned char)rec[len - 1])) + len--; + memcpy(buf, rec, len); + return len; + } + return -1; +} + +static long match_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri, + char *buf, long sz) +{ + const char *rec; + long len = xdl_get_rec(xdf, ri, &rec); + if (!xecfg->find_func) + return def_ff(rec, len, buf, sz); + return xecfg->find_func(rec, len, buf, sz, xecfg->find_func_priv); +} + +static int is_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri) +{ + char dummy[1]; + return match_func_rec(xdf, xecfg, ri, dummy, sizeof(dummy)) >= 0; +} + +struct func_line { + long len; + char buf[80]; +}; + +static long get_func_line(xdfenv_t *xe, xdemitconf_t const *xecfg, + struct func_line *func_line, long start, long limit) +{ + long l, size, step = (start > limit) ? -1 : 1; + char *buf, dummy[1]; + + buf = func_line ? func_line->buf : dummy; + size = func_line ? sizeof(func_line->buf) : sizeof(dummy); + + for (l = start; l != limit && 0 <= l && l < xe->xdf1.nrec; l += step) { + long len = match_func_rec(&xe->xdf1, xecfg, l, buf, size); + if (len >= 0) { + if (func_line) + func_line->len = len; + return l; + } + } + return -1; +} + +static int is_empty_rec(xdfile_t *xdf, long ri) +{ + const char *rec; + long len = xdl_get_rec(xdf, ri, &rec); + + while (len > 0 && XDL_ISSPACE(*rec)) { + rec++; + len--; + } + return !len; +} + +int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, + xdemitconf_t const *xecfg) { + long s1, s2, e1, e2, lctx; + xdchange_t *xch, *xche; + long funclineprev = -1; + struct func_line func_line = { 0 }; + + for (xch = xscr; xch; xch = xche->next) { + xdchange_t *xchp = xch; + xche = xdl_get_hunk(&xch, xecfg); + if (!xch) + break; + +pre_context_calculation: + s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0); + s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0); + + if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) { + long fs1, i1 = xch->i1; + + /* Appended chunk? */ + if (i1 >= xe->xdf1.nrec) { + long i2 = xch->i2; + + /* + * We don't need additional context if + * a whole function was added. + */ + while (i2 < xe->xdf2.nrec) { + if (is_func_rec(&xe->xdf2, xecfg, i2)) + goto post_context_calculation; + i2++; + } + + /* + * Otherwise get more context from the + * pre-image. + */ + i1 = xe->xdf1.nrec - 1; + } + + fs1 = get_func_line(xe, xecfg, NULL, i1, -1); + while (fs1 > 0 && !is_empty_rec(&xe->xdf1, fs1 - 1) && + !is_func_rec(&xe->xdf1, xecfg, fs1 - 1)) + fs1--; + if (fs1 < 0) + fs1 = 0; + if (fs1 < s1) { + s2 = XDL_MAX(s2 - (s1 - fs1), 0); + s1 = fs1; + + /* + * Did we extend context upwards into an + * ignored change? + */ + while (xchp != xch && + xchp->i1 + xchp->chg1 <= s1 && + xchp->i2 + xchp->chg2 <= s2) + xchp = xchp->next; + + /* If so, show it after all. */ + if (xchp != xch) { + xch = xchp; + goto pre_context_calculation; + } + } + } + + post_context_calculation: + lctx = xecfg->ctxlen; + lctx = XDL_MIN(lctx, xe->xdf1.nrec - (xche->i1 + xche->chg1)); + lctx = XDL_MIN(lctx, xe->xdf2.nrec - (xche->i2 + xche->chg2)); + + e1 = xche->i1 + xche->chg1 + lctx; + e2 = xche->i2 + xche->chg2 + lctx; + + if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) { + long fe1 = get_func_line(xe, xecfg, NULL, + xche->i1 + xche->chg1, + xe->xdf1.nrec); + while (fe1 > 0 && is_empty_rec(&xe->xdf1, fe1 - 1)) + fe1--; + if (fe1 < 0) + fe1 = xe->xdf1.nrec; + if (fe1 > e1) { + e2 = XDL_MIN(e2 + (fe1 - e1), xe->xdf2.nrec); + e1 = fe1; + } + + /* + * Overlap with next change? Then include it + * in the current hunk and start over to find + * its new end. + */ + if (xche->next) { + long l = XDL_MIN(xche->next->i1, + xe->xdf1.nrec - 1); + if (l - xecfg->ctxlen <= e1 || + get_func_line(xe, xecfg, NULL, l, e1) < 0) { + xche = xche->next; + goto post_context_calculation; + } + } + } + + /* + * Emit current hunk header. + */ + + if (xecfg->flags & XDL_EMIT_FUNCNAMES) { + get_func_line(xe, xecfg, &func_line, + s1 - 1, funclineprev); + funclineprev = s1 - 1; + } + if (!(xecfg->flags & XDL_EMIT_NO_HUNK_HDR) && + xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2, + func_line.buf, func_line.len, ecb) < 0) + return -1; + + /* + * Emit pre-context. + */ + for (; s2 < xch->i2; s2++) + if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) + return -1; + + for (s1 = xch->i1, s2 = xch->i2;; xch = xch->next) { + /* + * Merge previous with current change atom. + */ + for (; s1 < xch->i1 && s2 < xch->i2; s1++, s2++) + if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) + return -1; + + /* + * Removes lines from the first file. + */ + for (s1 = xch->i1; s1 < xch->i1 + xch->chg1; s1++) + if (xdl_emit_record(&xe->xdf1, s1, "-", ecb) < 0) + return -1; + + /* + * Adds lines from the second file. + */ + for (s2 = xch->i2; s2 < xch->i2 + xch->chg2; s2++) + if (xdl_emit_record(&xe->xdf2, s2, "+", ecb) < 0) + return -1; + + if (xch == xche) + break; + s1 = xch->i1 + xch->chg1; + s2 = xch->i2 + xch->chg2; + } + + /* + * Emit post-context. + */ + for (s2 = xche->i2 + xche->chg2; s2 < e2; s2++) + if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) + return -1; + } + + return 0; +} diff --git a/src/xdiff/xemit.h b/deps/xdiff/xemit.h similarity index 82% rename from src/xdiff/xemit.h rename to deps/xdiff/xemit.h index c2e2e830273..1b9887e670d 100644 --- a/src/xdiff/xemit.h +++ b/deps/xdiff/xemit.h @@ -13,8 +13,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * License along with this library; if not, see + * . * * Davide Libenzi * @@ -27,7 +27,7 @@ typedef int (*emit_func_t)(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, xdemitconf_t const *xecfg); -xdchange_t *xdl_get_hunk(xdchange_t *xscr, xdemitconf_t const *xecfg); +xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg); int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, xdemitconf_t const *xecfg); diff --git a/deps/xdiff/xhistogram.c b/deps/xdiff/xhistogram.c new file mode 100644 index 00000000000..16a8fe2f3f3 --- /dev/null +++ b/deps/xdiff/xhistogram.c @@ -0,0 +1,370 @@ +/* + * Copyright (C) 2010, Google Inc. + * and other copyright owners as documented in JGit's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "xinclude.h" + +#define MAX_PTR UINT_MAX +#define MAX_CNT UINT_MAX + +#define LINE_END(n) (line##n + count##n - 1) +#define LINE_END_PTR(n) (*line##n + *count##n - 1) + +struct histindex { + struct record { + unsigned int ptr, cnt; + struct record *next; + } **records, /* an occurrence */ + **line_map; /* map of line to record chain */ + chastore_t rcha; + unsigned int *next_ptrs; + unsigned int table_bits, + records_size, + line_map_size; + + unsigned int max_chain_length, + key_shift, + ptr_shift; + + unsigned int cnt, + has_common; + + xdfenv_t *env; + xpparam_t const *xpp; +}; + +struct region { + unsigned int begin1, end1; + unsigned int begin2, end2; +}; + +#define LINE_MAP(i, a) (i->line_map[(a) - i->ptr_shift]) + +#define NEXT_PTR(index, ptr) \ + (index->next_ptrs[(ptr) - index->ptr_shift]) + +#define CNT(index, ptr) \ + ((LINE_MAP(index, ptr))->cnt) + +#define REC(env, s, l) \ + (env->xdf##s.recs[l - 1]) + +static int cmp_recs(xrecord_t *r1, xrecord_t *r2) +{ + return r1->ha == r2->ha; + +} + +#define CMP(i, s1, l1, s2, l2) \ + (cmp_recs(REC(i->env, s1, l1), REC(i->env, s2, l2))) + +#define TABLE_HASH(index, side, line) \ + XDL_HASHLONG((REC(index->env, side, line))->ha, index->table_bits) + +static int scanA(struct histindex *index, int line1, int count1) +{ + unsigned int ptr, tbl_idx; + unsigned int chain_len; + struct record **rec_chain, *rec; + + for (ptr = LINE_END(1); line1 <= ptr; ptr--) { + tbl_idx = TABLE_HASH(index, 1, ptr); + rec_chain = index->records + tbl_idx; + rec = *rec_chain; + + chain_len = 0; + while (rec) { + if (CMP(index, 1, rec->ptr, 1, ptr)) { + /* + * ptr is identical to another element. Insert + * it onto the front of the existing element + * chain. + */ + NEXT_PTR(index, ptr) = rec->ptr; + rec->ptr = ptr; + /* cap rec->cnt at MAX_CNT */ + rec->cnt = XDL_MIN(MAX_CNT, rec->cnt + 1); + LINE_MAP(index, ptr) = rec; + goto continue_scan; + } + + rec = rec->next; + chain_len++; + } + + if (chain_len == index->max_chain_length) + return -1; + + /* + * This is the first time we have ever seen this particular + * element in the sequence. Construct a new chain for it. + */ + if (!(rec = xdl_cha_alloc(&index->rcha))) + return -1; + rec->ptr = ptr; + rec->cnt = 1; + rec->next = *rec_chain; + *rec_chain = rec; + LINE_MAP(index, ptr) = rec; + +continue_scan: + ; /* no op */ + } + + return 0; +} + +static int try_lcs(struct histindex *index, struct region *lcs, int b_ptr, + int line1, int count1, int line2, int count2) +{ + unsigned int b_next = b_ptr + 1; + struct record *rec = index->records[TABLE_HASH(index, 2, b_ptr)]; + unsigned int as, ae, bs, be, np, rc; + int should_break; + + for (; rec; rec = rec->next) { + if (rec->cnt > index->cnt) { + if (!index->has_common) + index->has_common = CMP(index, 1, rec->ptr, 2, b_ptr); + continue; + } + + as = rec->ptr; + if (!CMP(index, 1, as, 2, b_ptr)) + continue; + + index->has_common = 1; + for (;;) { + should_break = 0; + np = NEXT_PTR(index, as); + bs = b_ptr; + ae = as; + be = bs; + rc = rec->cnt; + + while (line1 < as && line2 < bs + && CMP(index, 1, as - 1, 2, bs - 1)) { + as--; + bs--; + if (1 < rc) + rc = XDL_MIN(rc, CNT(index, as)); + } + while (ae < LINE_END(1) && be < LINE_END(2) + && CMP(index, 1, ae + 1, 2, be + 1)) { + ae++; + be++; + if (1 < rc) + rc = XDL_MIN(rc, CNT(index, ae)); + } + + if (b_next <= be) + b_next = be + 1; + if (lcs->end1 - lcs->begin1 < ae - as || rc < index->cnt) { + lcs->begin1 = as; + lcs->begin2 = bs; + lcs->end1 = ae; + lcs->end2 = be; + index->cnt = rc; + } + + if (np == 0) + break; + + while (np <= ae) { + np = NEXT_PTR(index, np); + if (np == 0) { + should_break = 1; + break; + } + } + + if (should_break) + break; + + as = np; + } + } + return b_next; +} + +static int fall_back_to_classic_diff(xpparam_t const *xpp, xdfenv_t *env, + int line1, int count1, int line2, int count2) +{ + xpparam_t xpparam; + + memset(&xpparam, 0, sizeof(xpparam)); + xpparam.flags = xpp->flags & ~XDF_DIFF_ALGORITHM_MASK; + + return xdl_fall_back_diff(env, &xpparam, + line1, count1, line2, count2); +} + +static inline void free_index(struct histindex *index) +{ + xdl_free(index->records); + xdl_free(index->line_map); + xdl_free(index->next_ptrs); + xdl_cha_free(&index->rcha); +} + +static int find_lcs(xpparam_t const *xpp, xdfenv_t *env, + struct region *lcs, + int line1, int count1, int line2, int count2) +{ + int b_ptr; + int ret = -1; + struct histindex index; + + memset(&index, 0, sizeof(index)); + + index.env = env; + index.xpp = xpp; + + index.records = NULL; + index.line_map = NULL; + /* in case of early xdl_cha_free() */ + index.rcha.head = NULL; + + index.table_bits = xdl_hashbits(count1); + index.records_size = 1 << index.table_bits; + if (!XDL_CALLOC_ARRAY(index.records, index.records_size)) + goto cleanup; + + index.line_map_size = count1; + if (!XDL_CALLOC_ARRAY(index.line_map, index.line_map_size)) + goto cleanup; + + if (!XDL_CALLOC_ARRAY(index.next_ptrs, index.line_map_size)) + goto cleanup; + + /* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */ + if (xdl_cha_init(&index.rcha, sizeof(struct record), count1 / 4 + 1) < 0) + goto cleanup; + + index.ptr_shift = line1; + index.max_chain_length = 64; + + if (scanA(&index, line1, count1)) + goto cleanup; + + index.cnt = index.max_chain_length + 1; + + for (b_ptr = line2; b_ptr <= LINE_END(2); ) + b_ptr = try_lcs(&index, lcs, b_ptr, line1, count1, line2, count2); + + if (index.has_common && index.max_chain_length < index.cnt) + ret = 1; + else + ret = 0; + +cleanup: + free_index(&index); + return ret; +} + +static int histogram_diff(xpparam_t const *xpp, xdfenv_t *env, + int line1, int count1, int line2, int count2) +{ + struct region lcs; + int lcs_found; + int result; +redo: + result = -1; + + if (count1 <= 0 && count2 <= 0) + return 0; + + if (LINE_END(1) >= MAX_PTR) + return -1; + + if (!count1) { + while(count2--) + env->xdf2.rchg[line2++ - 1] = 1; + return 0; + } else if (!count2) { + while(count1--) + env->xdf1.rchg[line1++ - 1] = 1; + return 0; + } + + memset(&lcs, 0, sizeof(lcs)); + lcs_found = find_lcs(xpp, env, &lcs, line1, count1, line2, count2); + if (lcs_found < 0) + goto out; + else if (lcs_found) + result = fall_back_to_classic_diff(xpp, env, line1, count1, line2, count2); + else { + if (lcs.begin1 == 0 && lcs.begin2 == 0) { + while (count1--) + env->xdf1.rchg[line1++ - 1] = 1; + while (count2--) + env->xdf2.rchg[line2++ - 1] = 1; + result = 0; + } else { + result = histogram_diff(xpp, env, + line1, lcs.begin1 - line1, + line2, lcs.begin2 - line2); + if (result) + goto out; + /* + * result = histogram_diff(xpp, env, + * lcs.end1 + 1, LINE_END(1) - lcs.end1, + * lcs.end2 + 1, LINE_END(2) - lcs.end2); + * but let's optimize tail recursion ourself: + */ + count1 = LINE_END(1) - lcs.end1; + line1 = lcs.end1 + 1; + count2 = LINE_END(2) - lcs.end2; + line2 = lcs.end2 + 1; + goto redo; + } + } +out: + return result; +} + +int xdl_do_histogram_diff(xpparam_t const *xpp, xdfenv_t *env) +{ + return histogram_diff(xpp, env, + env->xdf1.dstart + 1, env->xdf1.dend - env->xdf1.dstart + 1, + env->xdf2.dstart + 1, env->xdf2.dend - env->xdf2.dstart + 1); +} diff --git a/src/xdiff/xinclude.h b/deps/xdiff/xinclude.h similarity index 76% rename from src/xdiff/xinclude.h rename to deps/xdiff/xinclude.h index 4a1cde9092b..75db1d8f357 100644 --- a/src/xdiff/xinclude.h +++ b/deps/xdiff/xinclude.h @@ -13,8 +13,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * License along with this library; if not, see + * . * * Davide Libenzi * @@ -23,17 +23,7 @@ #if !defined(XINCLUDE_H) #define XINCLUDE_H -#include -#include -#include -#include -#include - -#ifdef _WIN32 -#else -#include -#endif - +#include "git-xdiff.h" #include "xmacros.h" #include "xdiff.h" #include "xtypes.h" diff --git a/deps/xdiff/xmacros.h b/deps/xdiff/xmacros.h new file mode 100644 index 00000000000..8487bb396fa --- /dev/null +++ b/deps/xdiff/xmacros.h @@ -0,0 +1,71 @@ +/* + * LibXDiff by Davide Libenzi ( File Differential Library ) + * Copyright (C) 2003 Davide Libenzi + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see + * . + * + * Davide Libenzi + * + */ + +#if !defined(XMACROS_H) +#define XMACROS_H + + + + +#define XDL_MIN(a, b) ((a) < (b) ? (a): (b)) +#define XDL_MAX(a, b) ((a) > (b) ? (a): (b)) +#define XDL_ABS(v) ((v) >= 0 ? (v): -(v)) +#define XDL_ISDIGIT(c) ((c) >= '0' && (c) <= '9') +#define XDL_ISSPACE(c) (isspace((unsigned char)(c))) +#define XDL_ADDBITS(v,b) ((v) + ((v) >> (b))) +#define XDL_MASKBITS(b) ((1UL << (b)) - 1) +#define XDL_HASHLONG(v,b) (XDL_ADDBITS((unsigned long)(v), b) & XDL_MASKBITS(b)) +#define XDL_LE32_PUT(p, v) \ +do { \ + unsigned char *__p = (unsigned char *) (p); \ + *__p++ = (unsigned char) (v); \ + *__p++ = (unsigned char) ((v) >> 8); \ + *__p++ = (unsigned char) ((v) >> 16); \ + *__p = (unsigned char) ((v) >> 24); \ +} while (0) +#define XDL_LE32_GET(p, v) \ +do { \ + unsigned char const *__p = (unsigned char const *) (p); \ + (v) = (unsigned long) __p[0] | ((unsigned long) __p[1]) << 8 | \ + ((unsigned long) __p[2]) << 16 | ((unsigned long) __p[3]) << 24; \ +} while (0) + +/* Allocate an array of nr elements, returns NULL on failure */ +#define XDL_ALLOC_ARRAY(p, nr) \ + ((p) = SIZE_MAX / sizeof(*(p)) >= (size_t)(nr) \ + ? xdl_malloc((nr) * sizeof(*(p))) \ + : NULL) + +/* Allocate an array of nr zeroed out elements, returns NULL on failure */ +#define XDL_CALLOC_ARRAY(p, nr) ((p) = xdl_calloc(nr, sizeof(*(p)))) + +/* + * Ensure array p can accommodate at least nr elements, growing the + * array and updating alloc (which is the number of allocated + * elements) as necessary. Frees p and returns -1 on failure, returns + * 0 on success + */ +#define XDL_ALLOC_GROW(p, nr, alloc) \ + (-!((nr) <= (alloc) || \ + ((p) = xdl_alloc_grow_helper((p), (nr), &(alloc), sizeof(*(p)))))) + +#endif /* #if !defined(XMACROS_H) */ diff --git a/deps/xdiff/xmerge.c b/deps/xdiff/xmerge.c new file mode 100644 index 00000000000..6ebf73a933a --- /dev/null +++ b/deps/xdiff/xmerge.c @@ -0,0 +1,739 @@ +/* + * LibXDiff by Davide Libenzi ( File Differential Library ) + * Copyright (C) 2003-2006 Davide Libenzi, Johannes E. Schindelin + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see + * . + * + * Davide Libenzi + * + */ + +#include "xinclude.h" + +typedef struct s_xdmerge { + struct s_xdmerge *next; + /* + * 0 = conflict, + * 1 = no conflict, take first, + * 2 = no conflict, take second. + * 3 = no conflict, take both. + */ + int mode; + /* + * These point at the respective postimages. E.g. is + * how side #1 wants to change the common ancestor; if there is no + * overlap, lines before i1 in the postimage of side #1 appear + * in the merge result as a region touched by neither side. + */ + long i1, i2; + long chg1, chg2; + /* + * These point at the preimage; of course there is just one + * preimage, that is from the shared common ancestor. + */ + long i0; + long chg0; +} xdmerge_t; + +static int xdl_append_merge(xdmerge_t **merge, int mode, + long i0, long chg0, + long i1, long chg1, + long i2, long chg2) +{ + xdmerge_t *m = *merge; + if (m && (i1 <= m->i1 + m->chg1 || i2 <= m->i2 + m->chg2)) { + if (mode != m->mode) + m->mode = 0; + m->chg0 = i0 + chg0 - m->i0; + m->chg1 = i1 + chg1 - m->i1; + m->chg2 = i2 + chg2 - m->i2; + } else { + m = xdl_malloc(sizeof(xdmerge_t)); + if (!m) + return -1; + m->next = NULL; + m->mode = mode; + m->i0 = i0; + m->chg0 = chg0; + m->i1 = i1; + m->chg1 = chg1; + m->i2 = i2; + m->chg2 = chg2; + if (*merge) + (*merge)->next = m; + *merge = m; + } + return 0; +} + +static int xdl_cleanup_merge(xdmerge_t *c) +{ + int count = 0; + xdmerge_t *next_c; + + /* were there conflicts? */ + for (; c; c = next_c) { + if (c->mode == 0) + count++; + next_c = c->next; + xdl_free(c); + } + return count; +} + +static int xdl_merge_cmp_lines(xdfenv_t *xe1, int i1, xdfenv_t *xe2, int i2, + int line_count, long flags) +{ + int i; + xrecord_t **rec1 = xe1->xdf2.recs + i1; + xrecord_t **rec2 = xe2->xdf2.recs + i2; + + for (i = 0; i < line_count; i++) { + int result = xdl_recmatch(rec1[i]->ptr, rec1[i]->size, + rec2[i]->ptr, rec2[i]->size, flags); + if (!result) + return -1; + } + return 0; +} + +static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest) +{ + xrecord_t **recs; + int size = 0; + + recs = (use_orig ? xe->xdf1.recs : xe->xdf2.recs) + i; + + if (count < 1) + return 0; + + for (i = 0; i < count; size += recs[i++]->size) + if (dest) + memcpy(dest + size, recs[i]->ptr, recs[i]->size); + if (add_nl) { + i = recs[count - 1]->size; + if (i == 0 || recs[count - 1]->ptr[i - 1] != '\n') { + if (needs_cr) { + if (dest) + dest[size] = '\r'; + size++; + } + + if (dest) + dest[size] = '\n'; + size++; + } + } + return size; +} + +static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest) +{ + return xdl_recs_copy_0(0, xe, i, count, needs_cr, add_nl, dest); +} + +static int xdl_orig_copy(xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest) +{ + return xdl_recs_copy_0(1, xe, i, count, needs_cr, add_nl, dest); +} + +/* + * Returns 1 if the i'th line ends in CR/LF (if it is the last line and + * has no eol, the preceding line, if any), 0 if it ends in LF-only, and + * -1 if the line ending cannot be determined. + */ +static int is_eol_crlf(xdfile_t *file, int i) +{ + long size; + + if (i < file->nrec - 1) + /* All lines before the last *must* end in LF */ + return (size = file->recs[i]->size) > 1 && + file->recs[i]->ptr[size - 2] == '\r'; + if (!file->nrec) + /* Cannot determine eol style from empty file */ + return -1; + if ((size = file->recs[i]->size) && + file->recs[i]->ptr[size - 1] == '\n') + /* Last line; ends in LF; Is it CR/LF? */ + return size > 1 && + file->recs[i]->ptr[size - 2] == '\r'; + if (!i) + /* The only line has no eol */ + return -1; + /* Determine eol from second-to-last line */ + return (size = file->recs[i - 1]->size) > 1 && + file->recs[i - 1]->ptr[size - 2] == '\r'; +} + +static int is_cr_needed(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m) +{ + int needs_cr; + + /* Match post-images' preceding, or first, lines' end-of-line style */ + needs_cr = is_eol_crlf(&xe1->xdf2, m->i1 ? m->i1 - 1 : 0); + if (needs_cr) + needs_cr = is_eol_crlf(&xe2->xdf2, m->i2 ? m->i2 - 1 : 0); + /* Look at pre-image's first line, unless we already settled on LF */ + if (needs_cr) + needs_cr = is_eol_crlf(&xe1->xdf1, 0); + /* If still undecided, use LF-only */ + return needs_cr < 0 ? 0 : needs_cr; +} + +static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1, + xdfenv_t *xe2, const char *name2, + const char *name3, + int size, int i, int style, + xdmerge_t *m, char *dest, int marker_size) +{ + int marker1_size = (name1 ? strlen(name1) + 1 : 0); + int marker2_size = (name2 ? strlen(name2) + 1 : 0); + int marker3_size = (name3 ? strlen(name3) + 1 : 0); + int needs_cr = is_cr_needed(xe1, xe2, m); + + if (marker_size <= 0) + marker_size = DEFAULT_CONFLICT_MARKER_SIZE; + + /* Before conflicting part */ + size += xdl_recs_copy(xe1, i, m->i1 - i, 0, 0, + dest ? dest + size : NULL); + + if (!dest) { + size += marker_size + 1 + needs_cr + marker1_size; + } else { + memset(dest + size, '<', marker_size); + size += marker_size; + if (marker1_size) { + dest[size] = ' '; + memcpy(dest + size + 1, name1, marker1_size - 1); + size += marker1_size; + } + if (needs_cr) + dest[size++] = '\r'; + dest[size++] = '\n'; + } + + /* Postimage from side #1 */ + size += xdl_recs_copy(xe1, m->i1, m->chg1, needs_cr, 1, + dest ? dest + size : NULL); + + if (style == XDL_MERGE_DIFF3 || style == XDL_MERGE_ZEALOUS_DIFF3) { + /* Shared preimage */ + if (!dest) { + size += marker_size + 1 + needs_cr + marker3_size; + } else { + memset(dest + size, '|', marker_size); + size += marker_size; + if (marker3_size) { + dest[size] = ' '; + memcpy(dest + size + 1, name3, marker3_size - 1); + size += marker3_size; + } + if (needs_cr) + dest[size++] = '\r'; + dest[size++] = '\n'; + } + size += xdl_orig_copy(xe1, m->i0, m->chg0, needs_cr, 1, + dest ? dest + size : NULL); + } + + if (!dest) { + size += marker_size + 1 + needs_cr; + } else { + memset(dest + size, '=', marker_size); + size += marker_size; + if (needs_cr) + dest[size++] = '\r'; + dest[size++] = '\n'; + } + + /* Postimage from side #2 */ + size += xdl_recs_copy(xe2, m->i2, m->chg2, needs_cr, 1, + dest ? dest + size : NULL); + if (!dest) { + size += marker_size + 1 + needs_cr + marker2_size; + } else { + memset(dest + size, '>', marker_size); + size += marker_size; + if (marker2_size) { + dest[size] = ' '; + memcpy(dest + size + 1, name2, marker2_size - 1); + size += marker2_size; + } + if (needs_cr) + dest[size++] = '\r'; + dest[size++] = '\n'; + } + return size; +} + +static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1, + xdfenv_t *xe2, const char *name2, + const char *ancestor_name, + int favor, + xdmerge_t *m, char *dest, int style, + int marker_size) +{ + int size, i; + + for (size = i = 0; m; m = m->next) { + if (favor && !m->mode) + m->mode = favor; + + if (m->mode == 0) + size = fill_conflict_hunk(xe1, name1, xe2, name2, + ancestor_name, + size, i, style, m, dest, + marker_size); + else if (m->mode & 3) { + /* Before conflicting part */ + size += xdl_recs_copy(xe1, i, m->i1 - i, 0, 0, + dest ? dest + size : NULL); + /* Postimage from side #1 */ + if (m->mode & 1) { + int needs_cr = is_cr_needed(xe1, xe2, m); + + size += xdl_recs_copy(xe1, m->i1, m->chg1, needs_cr, (m->mode & 2), + dest ? dest + size : NULL); + } + /* Postimage from side #2 */ + if (m->mode & 2) + size += xdl_recs_copy(xe2, m->i2, m->chg2, 0, 0, + dest ? dest + size : NULL); + } else + continue; + i = m->i1 + m->chg1; + } + size += xdl_recs_copy(xe1, i, xe1->xdf2.nrec - i, 0, 0, + dest ? dest + size : NULL); + return size; +} + +static int recmatch(xrecord_t *rec1, xrecord_t *rec2, unsigned long flags) +{ + return xdl_recmatch(rec1->ptr, rec1->size, + rec2->ptr, rec2->size, flags); +} + +/* + * Remove any common lines from the beginning and end of the conflicted region. + */ +static void xdl_refine_zdiff3_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m, + xpparam_t const *xpp) +{ + xrecord_t **rec1 = xe1->xdf2.recs, **rec2 = xe2->xdf2.recs; + for (; m; m = m->next) { + /* let's handle just the conflicts */ + if (m->mode) + continue; + + while(m->chg1 && m->chg2 && + recmatch(rec1[m->i1], rec2[m->i2], xpp->flags)) { + m->chg1--; + m->chg2--; + m->i1++; + m->i2++; + } + while (m->chg1 && m->chg2 && + recmatch(rec1[m->i1 + m->chg1 - 1], + rec2[m->i2 + m->chg2 - 1], xpp->flags)) { + m->chg1--; + m->chg2--; + } + } +} + +/* + * Sometimes, changes are not quite identical, but differ in only a few + * lines. Try hard to show only these few lines as conflicting. + */ +static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m, + xpparam_t const *xpp) +{ + for (; m; m = m->next) { + mmfile_t t1, t2; + xdfenv_t xe; + xdchange_t *xscr, *x; + int i1 = m->i1, i2 = m->i2; + + /* let's handle just the conflicts */ + if (m->mode) + continue; + + /* no sense refining a conflict when one side is empty */ + if (m->chg1 == 0 || m->chg2 == 0) + continue; + + /* + * This probably does not work outside git, since + * we have a very simple mmfile structure. + */ + t1.ptr = (char *)xe1->xdf2.recs[m->i1]->ptr; + t1.size = xe1->xdf2.recs[m->i1 + m->chg1 - 1]->ptr + + xe1->xdf2.recs[m->i1 + m->chg1 - 1]->size - t1.ptr; + t2.ptr = (char *)xe2->xdf2.recs[m->i2]->ptr; + t2.size = xe2->xdf2.recs[m->i2 + m->chg2 - 1]->ptr + + xe2->xdf2.recs[m->i2 + m->chg2 - 1]->size - t2.ptr; + if (xdl_do_diff(&t1, &t2, xpp, &xe) < 0) + return -1; + if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 || + xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 || + xdl_build_script(&xe, &xscr) < 0) { + xdl_free_env(&xe); + return -1; + } + if (!xscr) { + /* If this happens, the changes are identical. */ + xdl_free_env(&xe); + m->mode = 4; + continue; + } + x = xscr; + m->i1 = xscr->i1 + i1; + m->chg1 = xscr->chg1; + m->i2 = xscr->i2 + i2; + m->chg2 = xscr->chg2; + while (xscr->next) { + xdmerge_t *m2 = xdl_malloc(sizeof(xdmerge_t)); + if (!m2) { + xdl_free_env(&xe); + xdl_free_script(x); + return -1; + } + xscr = xscr->next; + m2->next = m->next; + m->next = m2; + m = m2; + m->mode = 0; + m->i1 = xscr->i1 + i1; + m->chg1 = xscr->chg1; + m->i2 = xscr->i2 + i2; + m->chg2 = xscr->chg2; + } + xdl_free_env(&xe); + xdl_free_script(x); + } + return 0; +} + +static int line_contains_alnum(const char *ptr, long size) +{ + while (size--) + if (isalnum((unsigned char)*(ptr++))) + return 1; + return 0; +} + +static int lines_contain_alnum(xdfenv_t *xe, int i, int chg) +{ + for (; chg; chg--, i++) + if (line_contains_alnum(xe->xdf2.recs[i]->ptr, + xe->xdf2.recs[i]->size)) + return 1; + return 0; +} + +/* + * This function merges m and m->next, marking everything between those hunks + * as conflicting, too. + */ +static void xdl_merge_two_conflicts(xdmerge_t *m) +{ + xdmerge_t *next_m = m->next; + m->chg1 = next_m->i1 + next_m->chg1 - m->i1; + m->chg2 = next_m->i2 + next_m->chg2 - m->i2; + m->next = next_m->next; + xdl_free(next_m); +} + +/* + * If there are less than 3 non-conflicting lines between conflicts, + * it appears simpler -- because it takes up less (or as many) lines -- + * if the lines are moved into the conflicts. + */ +static int xdl_simplify_non_conflicts(xdfenv_t *xe1, xdmerge_t *m, + int simplify_if_no_alnum) +{ + int result = 0; + + if (!m) + return result; + for (;;) { + xdmerge_t *next_m = m->next; + int begin, end; + + if (!next_m) + return result; + + begin = m->i1 + m->chg1; + end = next_m->i1; + + if (m->mode != 0 || next_m->mode != 0 || + (end - begin > 3 && + (!simplify_if_no_alnum || + lines_contain_alnum(xe1, begin, end - begin)))) { + m = next_m; + } else { + result++; + xdl_merge_two_conflicts(m); + } + } +} + +/* + * level == 0: mark all overlapping changes as conflict + * level == 1: mark overlapping changes as conflict only if not identical + * level == 2: analyze non-identical changes for minimal conflict set + * level == 3: analyze non-identical changes for minimal conflict set, but + * treat hunks not containing any letter or number as conflicting + * + * returns < 0 on error, == 0 for no conflicts, else number of conflicts + */ +static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, + xdfenv_t *xe2, xdchange_t *xscr2, + xmparam_t const *xmp, mmbuffer_t *result) +{ + xdmerge_t *changes, *c; + xpparam_t const *xpp = &xmp->xpp; + const char *const ancestor_name = xmp->ancestor; + const char *const name1 = xmp->file1; + const char *const name2 = xmp->file2; + int i0, i1, i2, chg0, chg1, chg2; + int level = xmp->level; + int style = xmp->style; + int favor = xmp->favor; + + /* + * XDL_MERGE_DIFF3 does not attempt to refine conflicts by looking + * at common areas of sides 1 & 2, because the base (side 0) does + * not match and is being shown. Similarly, simplification of + * non-conflicts is also skipped due to the skipping of conflict + * refinement. + * + * XDL_MERGE_ZEALOUS_DIFF3, on the other hand, will attempt to + * refine conflicts looking for common areas of sides 1 & 2. + * However, since the base is being shown and does not match, + * it will only look for common areas at the beginning or end + * of the conflict block. Since XDL_MERGE_ZEALOUS_DIFF3's + * conflict refinement is much more limited in this fashion, the + * conflict simplification will be skipped. + */ + if (style == XDL_MERGE_DIFF3 || style == XDL_MERGE_ZEALOUS_DIFF3) { + /* + * "diff3 -m" output does not make sense for anything + * more aggressive than XDL_MERGE_EAGER. + */ + if (XDL_MERGE_EAGER < level) + level = XDL_MERGE_EAGER; + } + + c = changes = NULL; + + while (xscr1 && xscr2) { + if (!changes) + changes = c; + if (xscr1->i1 + xscr1->chg1 < xscr2->i1) { + i0 = xscr1->i1; + i1 = xscr1->i2; + i2 = xscr2->i2 - xscr2->i1 + xscr1->i1; + chg0 = xscr1->chg1; + chg1 = xscr1->chg2; + chg2 = xscr1->chg1; + if (xdl_append_merge(&c, 1, + i0, chg0, i1, chg1, i2, chg2)) { + xdl_cleanup_merge(changes); + return -1; + } + xscr1 = xscr1->next; + continue; + } + if (xscr2->i1 + xscr2->chg1 < xscr1->i1) { + i0 = xscr2->i1; + i1 = xscr1->i2 - xscr1->i1 + xscr2->i1; + i2 = xscr2->i2; + chg0 = xscr2->chg1; + chg1 = xscr2->chg1; + chg2 = xscr2->chg2; + if (xdl_append_merge(&c, 2, + i0, chg0, i1, chg1, i2, chg2)) { + xdl_cleanup_merge(changes); + return -1; + } + xscr2 = xscr2->next; + continue; + } + if (level == XDL_MERGE_MINIMAL || xscr1->i1 != xscr2->i1 || + xscr1->chg1 != xscr2->chg1 || + xscr1->chg2 != xscr2->chg2 || + xdl_merge_cmp_lines(xe1, xscr1->i2, + xe2, xscr2->i2, + xscr1->chg2, xpp->flags)) { + /* conflict */ + int off = xscr1->i1 - xscr2->i1; + int ffo = off + xscr1->chg1 - xscr2->chg1; + + i0 = xscr1->i1; + i1 = xscr1->i2; + i2 = xscr2->i2; + if (off > 0) { + i0 -= off; + i1 -= off; + } + else + i2 += off; + chg0 = xscr1->i1 + xscr1->chg1 - i0; + chg1 = xscr1->i2 + xscr1->chg2 - i1; + chg2 = xscr2->i2 + xscr2->chg2 - i2; + if (ffo < 0) { + chg0 -= ffo; + chg1 -= ffo; + } else + chg2 += ffo; + if (xdl_append_merge(&c, 0, + i0, chg0, i1, chg1, i2, chg2)) { + xdl_cleanup_merge(changes); + return -1; + } + } + + i1 = xscr1->i1 + xscr1->chg1; + i2 = xscr2->i1 + xscr2->chg1; + + if (i1 >= i2) + xscr2 = xscr2->next; + if (i2 >= i1) + xscr1 = xscr1->next; + } + while (xscr1) { + if (!changes) + changes = c; + i0 = xscr1->i1; + i1 = xscr1->i2; + i2 = xscr1->i1 + xe2->xdf2.nrec - xe2->xdf1.nrec; + chg0 = xscr1->chg1; + chg1 = xscr1->chg2; + chg2 = xscr1->chg1; + if (xdl_append_merge(&c, 1, + i0, chg0, i1, chg1, i2, chg2)) { + xdl_cleanup_merge(changes); + return -1; + } + xscr1 = xscr1->next; + } + while (xscr2) { + if (!changes) + changes = c; + i0 = xscr2->i1; + i1 = xscr2->i1 + xe1->xdf2.nrec - xe1->xdf1.nrec; + i2 = xscr2->i2; + chg0 = xscr2->chg1; + chg1 = xscr2->chg1; + chg2 = xscr2->chg2; + if (xdl_append_merge(&c, 2, + i0, chg0, i1, chg1, i2, chg2)) { + xdl_cleanup_merge(changes); + return -1; + } + xscr2 = xscr2->next; + } + if (!changes) + changes = c; + /* refine conflicts */ + if (style == XDL_MERGE_ZEALOUS_DIFF3) { + xdl_refine_zdiff3_conflicts(xe1, xe2, changes, xpp); + } else if (XDL_MERGE_ZEALOUS <= level && + (xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0 || + xdl_simplify_non_conflicts(xe1, changes, + XDL_MERGE_ZEALOUS < level) < 0)) { + xdl_cleanup_merge(changes); + return -1; + } + /* output */ + if (result) { + int marker_size = xmp->marker_size; + int size = xdl_fill_merge_buffer(xe1, name1, xe2, name2, + ancestor_name, + favor, changes, NULL, style, + marker_size); + result->ptr = xdl_malloc(size); + if (!result->ptr) { + xdl_cleanup_merge(changes); + return -1; + } + result->size = size; + xdl_fill_merge_buffer(xe1, name1, xe2, name2, + ancestor_name, favor, changes, + result->ptr, style, marker_size); + } + return xdl_cleanup_merge(changes); +} + +int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2, + xmparam_t const *xmp, mmbuffer_t *result) +{ + xdchange_t *xscr1 = NULL, *xscr2 = NULL; + xdfenv_t xe1, xe2; + int status = -1; + xpparam_t const *xpp = &xmp->xpp; + + result->ptr = NULL; + result->size = 0; + + if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0) + return -1; + + if (xdl_do_diff(orig, mf2, xpp, &xe2) < 0) + goto free_xe1; /* avoid double free of xe2 */ + + if (xdl_change_compact(&xe1.xdf1, &xe1.xdf2, xpp->flags) < 0 || + xdl_change_compact(&xe1.xdf2, &xe1.xdf1, xpp->flags) < 0 || + xdl_build_script(&xe1, &xscr1) < 0) + goto out; + + if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 || + xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 || + xdl_build_script(&xe2, &xscr2) < 0) + goto out; + + if (!xscr1) { + result->ptr = xdl_malloc(mf2->size); + if (!result->ptr) + goto out; + status = 0; + memcpy(result->ptr, mf2->ptr, mf2->size); + result->size = mf2->size; + } else if (!xscr2) { + result->ptr = xdl_malloc(mf1->size); + if (!result->ptr) + goto out; + status = 0; + memcpy(result->ptr, mf1->ptr, mf1->size); + result->size = mf1->size; + } else { + status = xdl_do_merge(&xe1, xscr1, + &xe2, xscr2, + xmp, result); + } + out: + xdl_free_script(xscr1); + xdl_free_script(xscr2); + + xdl_free_env(&xe2); + free_xe1: + xdl_free_env(&xe1); + + return status; +} diff --git a/src/xdiff/xpatience.c b/deps/xdiff/xpatience.c similarity index 79% rename from src/xdiff/xpatience.c rename to deps/xdiff/xpatience.c index fdd7d0263f5..a2d8955537f 100644 --- a/src/xdiff/xpatience.c +++ b/deps/xdiff/xpatience.c @@ -1,6 +1,6 @@ /* * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003-2009 Davide Libenzi, Johannes E. Schindelin + * Copyright (C) 2003-2016 Davide Libenzi, Johannes E. Schindelin * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -13,15 +13,13 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * License along with this library; if not, see + * . * * Davide Libenzi * */ #include "xinclude.h" -#include "xtypes.h" -#include "xdiff.h" /* * The basic idea of patience diff is to find lines that are unique in @@ -62,20 +60,36 @@ struct hashmap { * initially, "next" reflects only the order in file1. */ struct entry *next, *previous; + + /* + * If 1, this entry can serve as an anchor. See + * Documentation/diff-options.txt for more information. + */ + unsigned anchor : 1; } *entries, *first, *last; /* were common records found? */ unsigned long has_matches; - mmfile_t *file1, *file2; xdfenv_t *env; xpparam_t const *xpp; }; +static int is_anchor(xpparam_t const *xpp, const char *line) +{ + int i; + for (i = 0; i < xpp->anchors_nr; i++) { + if (!strncmp(line, xpp->anchors[i], strlen(xpp->anchors[i]))) + return 1; + } + return 0; +} + /* The argument "pass" is 1 for the first file, 2 for the second. */ -static void insert_record(int line, struct hashmap *map, int pass) +static void insert_record(xpparam_t const *xpp, int line, struct hashmap *map, + int pass) { xrecord_t **records = pass == 1 ? map->env->xdf1.recs : map->env->xdf2.recs; - xrecord_t *record = records[line - 1], *other; + xrecord_t *record = records[line - 1]; /* * After xdl_prepare_env() (or more precisely, due to * xdl_classify_record()), the "ha" member of the records (AKA lines) @@ -89,11 +103,7 @@ static void insert_record(int line, struct hashmap *map, int pass) int index = (int)((record->ha << 1) % map->alloc); while (map->entries[index].line1) { - other = map->env->xdf1.recs[map->entries[index].line1 - 1]; - if (map->entries[index].hash != record->ha || - !xdl_recmatch(record->ptr, record->size, - other->ptr, other->size, - map->xpp->flags)) { + if (map->entries[index].hash != record->ha) { if (++index >= map->alloc) index = 0; continue; @@ -110,6 +120,7 @@ static void insert_record(int line, struct hashmap *map, int pass) return; map->entries[index].line1 = line; map->entries[index].hash = record->ha; + map->entries[index].anchor = is_anchor(xpp, map->env->xdf1.recs[line - 1]->ptr); if (!map->first) map->first = map->entries + index; if (map->last) { @@ -127,31 +138,25 @@ static void insert_record(int line, struct hashmap *map, int pass) * * It is assumed that env has been prepared using xdl_prepare(). */ -static int fill_hashmap(mmfile_t *file1, mmfile_t *file2, - xpparam_t const *xpp, xdfenv_t *env, +static int fill_hashmap(xpparam_t const *xpp, xdfenv_t *env, struct hashmap *result, int line1, int count1, int line2, int count2) { - result->file1 = file1; - result->file2 = file2; result->xpp = xpp; result->env = env; /* We know exactly how large we want the hash map */ result->alloc = count1 * 2; - result->entries = (struct entry *) - xdl_malloc(result->alloc * sizeof(struct entry)); - if (!result->entries) + if (!XDL_CALLOC_ARRAY(result->entries, result->alloc)) return -1; - memset(result->entries, 0, result->alloc * sizeof(struct entry)); /* First, fill with entries from the first file */ while (count1--) - insert_record(line1++, result, 1); + insert_record(xpp, line1++, result, 1); /* Then search for matches in the second file */ while (count2--) - insert_record(line2++, result, 2); + insert_record(xpp, line2++, result, 2); return 0; } @@ -166,7 +171,7 @@ static int binary_search(struct entry **sequence, int longest, int left = -1, right = longest; while (left + 1 < right) { - int middle = (left + right) / 2; + int middle = left + (right - left) / 2; /* by construction, no two entries can be equal */ if (sequence[middle]->line2 > entry->line2) right = middle; @@ -186,26 +191,44 @@ static int binary_search(struct entry **sequence, int longest, * item per sequence length: the sequence with the smallest last * element (in terms of line2). */ -static struct entry *find_longest_common_sequence(struct hashmap *map) +static int find_longest_common_sequence(struct hashmap *map, struct entry **res) { - struct entry **sequence = xdl_malloc(map->nr * sizeof(struct entry *)); + struct entry **sequence; int longest = 0, i; struct entry *entry; + /* + * If not -1, this entry in sequence must never be overridden. + * Therefore, overriding entries before this has no effect, so + * do not do that either. + */ + int anchor_i = -1; + + if (!XDL_ALLOC_ARRAY(sequence, map->nr)) + return -1; + for (entry = map->first; entry; entry = entry->next) { if (!entry->line2 || entry->line2 == NON_UNIQUE) continue; i = binary_search(sequence, longest, entry); entry->previous = i < 0 ? NULL : sequence[i]; - sequence[++i] = entry; - if (i == longest) + ++i; + if (i <= anchor_i) + continue; + sequence[i] = entry; + if (entry->anchor) { + anchor_i = i; + longest = anchor_i + 1; + } else if (i == longest) { longest++; + } } /* No common unique lines were found */ if (!longest) { + *res = NULL; xdl_free(sequence); - return NULL; + return 0; } /* Iterate starting at the last element, adjusting the "next" members */ @@ -215,20 +238,19 @@ static struct entry *find_longest_common_sequence(struct hashmap *map) entry->previous->next = entry; entry = entry->previous; } + *res = entry; xdl_free(sequence); - return entry; + return 0; } static int match(struct hashmap *map, int line1, int line2) { xrecord_t *record1 = map->env->xdf1.recs[line1 - 1]; xrecord_t *record2 = map->env->xdf2.recs[line2 - 1]; - return xdl_recmatch(record1->ptr, record1->size, - record2->ptr, record2->size, map->xpp->flags); + return record1->ha == record2->ha; } -static int patience_diff(mmfile_t *file1, mmfile_t *file2, - xpparam_t const *xpp, xdfenv_t *env, +static int patience_diff(xpparam_t const *xpp, xdfenv_t *env, int line1, int count1, int line2, int count2); static int walk_common_sequence(struct hashmap *map, struct entry *first, @@ -259,11 +281,7 @@ static int walk_common_sequence(struct hashmap *map, struct entry *first, /* Recurse */ if (next1 > line1 || next2 > line2) { - struct hashmap submap; - - memset(&submap, 0, sizeof(submap)); - if (patience_diff(map->file1, map->file2, - map->xpp, map->env, + if (patience_diff(map->xpp, map->env, line1, next1 - line1, line2, next2 - line2)) return -1; @@ -288,7 +306,9 @@ static int fall_back_to_classic_diff(struct hashmap *map, int line1, int count1, int line2, int count2) { xpparam_t xpp; - xpp.flags = map->xpp->flags & ~XDF_PATIENCE_DIFF; + + memset(&xpp, 0, sizeof(xpp)); + xpp.flags = map->xpp->flags & ~XDF_DIFF_ALGORITHM_MASK; return xdl_fall_back_diff(map->env, &xpp, line1, count1, line2, count2); @@ -300,8 +320,7 @@ static int fall_back_to_classic_diff(struct hashmap *map, * * This function assumes that env was prepared with xdl_prepare_env(). */ -static int patience_diff(mmfile_t *file1, mmfile_t *file2, - xpparam_t const *xpp, xdfenv_t *env, +static int patience_diff(xpparam_t const *xpp, xdfenv_t *env, int line1, int count1, int line2, int count2) { struct hashmap map; @@ -320,7 +339,7 @@ static int patience_diff(mmfile_t *file1, mmfile_t *file2, } memset(&map, 0, sizeof(map)); - if (fill_hashmap(file1, file2, xpp, env, &map, + if (fill_hashmap(xpp, env, &map, line1, count1, line2, count2)) return -1; @@ -334,25 +353,21 @@ static int patience_diff(mmfile_t *file1, mmfile_t *file2, return 0; } - first = find_longest_common_sequence(&map); + result = find_longest_common_sequence(&map, &first); + if (result) + goto out; if (first) result = walk_common_sequence(&map, first, line1, count1, line2, count2); else result = fall_back_to_classic_diff(&map, line1, count1, line2, count2); - + out: xdl_free(map.entries); return result; } -int xdl_do_patience_diff(mmfile_t *file1, mmfile_t *file2, - xpparam_t const *xpp, xdfenv_t *env) +int xdl_do_patience_diff(xpparam_t const *xpp, xdfenv_t *env) { - if (xdl_prepare_env(file1, file2, xpp, env) < 0) - return -1; - - /* environment is cleaned up in xdl_diff() */ - return patience_diff(file1, file2, xpp, env, - 1, env->xdf1.nrec, 1, env->xdf2.nrec); + return patience_diff(xpp, env, 1, env->xdf1.nrec, 1, env->xdf2.nrec); } diff --git a/src/xdiff/xprepare.c b/deps/xdiff/xprepare.c similarity index 83% rename from src/xdiff/xprepare.c rename to deps/xdiff/xprepare.c index e419f4f7260..c84549f6c50 100644 --- a/src/xdiff/xprepare.c +++ b/deps/xdiff/xprepare.c @@ -13,8 +13,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * License along with this library; if not, see + * . * * Davide Libenzi * @@ -78,15 +78,14 @@ static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) { return -1; } - if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) { + if (!XDL_CALLOC_ARRAY(cf->rchash, cf->hsize)) { xdl_cha_free(&cf->ncha); return -1; } - memset(cf->rchash, 0, cf->hsize * sizeof(xdlclass_t *)); cf->alloc = size; - if (!(cf->rcrecs = (xdlclass_t **) xdl_malloc(cf->alloc * sizeof(xdlclass_t *)))) { + if (!XDL_ALLOC_ARRAY(cf->rcrecs, cf->alloc)) { xdl_free(cf->rchash); xdl_cha_free(&cf->ncha); @@ -112,7 +111,6 @@ static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t long hi; char const *line; xdlclass_t *rcrec; - xdlclass_t **rcrecs; line = rec->ptr; hi = (long) XDL_HASHLONG(rec->ha, cf->hbits); @@ -128,14 +126,8 @@ static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t return -1; } rcrec->idx = cf->count++; - if (cf->count > cf->alloc) { - cf->alloc *= 2; - if (!(rcrecs = (xdlclass_t **) xdl_realloc(cf->rcrecs, cf->alloc * sizeof(xdlclass_t *)))) { - + if (XDL_ALLOC_GROW(cf->rcrecs, cf->count, cf->alloc)) return -1; - } - cf->rcrecs = rcrecs; - } cf->rcrecs[rcrec->idx] = rcrec; rcrec->line = line; rcrec->size = rec->size; @@ -164,7 +156,7 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_ unsigned long hav; char const *blk, *cur, *top, *prev; xrecord_t *crec; - xrecord_t **recs, **rrecs; + xrecord_t **recs; xrecord_t **rhash; unsigned long *ha; char *rchg; @@ -178,51 +170,42 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_ if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) goto abort; - if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) + if (!XDL_ALLOC_ARRAY(recs, narec)) goto abort; - if (xpp->flags & XDF_HISTOGRAM_DIFF) - hbits = hsize = 0; - else { - hbits = xdl_hashbits((unsigned int) narec); - hsize = 1 << hbits; - if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) - goto abort; - memset(rhash, 0, hsize * sizeof(xrecord_t *)); - } + hbits = xdl_hashbits((unsigned int) narec); + hsize = 1 << hbits; + if (!XDL_CALLOC_ARRAY(rhash, hsize)) + goto abort; nrec = 0; - if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) { + if ((cur = blk = xdl_mmfile_first(mf, &bsize))) { for (top = blk + bsize; cur < top; ) { prev = cur; hav = xdl_hash_record(&cur, top, xpp->flags); - if (nrec >= narec) { - narec *= 2; - if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) - goto abort; - recs = rrecs; - } + if (XDL_ALLOC_GROW(recs, nrec + 1, narec)) + goto abort; if (!(crec = xdl_cha_alloc(&xdf->rcha))) goto abort; crec->ptr = prev; crec->size = (long) (cur - prev); crec->ha = hav; recs[nrec++] = crec; - - if (!(xpp->flags & XDF_HISTOGRAM_DIFF) && - xdl_classify_record(pass, cf, rhash, hbits, crec) < 0) + if (xdl_classify_record(pass, cf, rhash, hbits, crec) < 0) goto abort; } } - if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) + if (!XDL_CALLOC_ARRAY(rchg, nrec + 2)) goto abort; - memset(rchg, 0, (nrec + 2) * sizeof(char)); - if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) - goto abort; - if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) - goto abort; + if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) && + (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) { + if (!XDL_ALLOC_ARRAY(rindex, nrec + 1)) + goto abort; + if (!XDL_ALLOC_ARRAY(ha, nrec + 1)) + goto abort; + } xdf->nrec = nrec; xdf->recs = recs; @@ -273,16 +256,14 @@ int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, * (nrecs) will be updated correctly anyway by * xdl_prepare_ctx(). */ - sample = xpp->flags & XDF_HISTOGRAM_DIFF ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1; + sample = (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF + ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1); enl1 = xdl_guess_lines(mf1, sample) + 1; enl2 = xdl_guess_lines(mf2, sample) + 1; - if (!(xpp->flags & XDF_HISTOGRAM_DIFF) && - xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) { - + if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) return -1; - } if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) { @@ -296,17 +277,17 @@ int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, return -1; } - if (!(xpp->flags & XDF_PATIENCE_DIFF) && - !(xpp->flags & XDF_HISTOGRAM_DIFF) && - xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) { + if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) && + (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) && + xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) { xdl_free_ctx(&xe->xdf2); xdl_free_ctx(&xe->xdf1); + xdl_free_classifier(&cf); return -1; } - if (!(xpp->flags & XDF_HISTOGRAM_DIFF)) - xdl_free_classifier(&cf); + xdl_free_classifier(&cf); return 0; } @@ -388,11 +369,8 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd xdlclass_t *rcrec; char *dis, *dis1, *dis2; - if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) { - + if (!XDL_CALLOC_ARRAY(dis, xdf1->nrec + xdf2->nrec + 2)) return -1; - } - memset(dis, 0, xdf1->nrec + xdf2->nrec + 2); dis1 = dis; dis2 = dis1 + xdf1->nrec + 1; diff --git a/src/xdiff/xprepare.h b/deps/xdiff/xprepare.h similarity index 86% rename from src/xdiff/xprepare.h rename to deps/xdiff/xprepare.h index 8fb06a53745..947d9fc1bb8 100644 --- a/src/xdiff/xprepare.h +++ b/deps/xdiff/xprepare.h @@ -13,8 +13,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * License along with this library; if not, see + * . * * Davide Libenzi * diff --git a/src/xdiff/xtypes.h b/deps/xdiff/xtypes.h similarity index 90% rename from src/xdiff/xtypes.h rename to deps/xdiff/xtypes.h index 2511aef8d89..8442bd436ef 100644 --- a/src/xdiff/xtypes.h +++ b/deps/xdiff/xtypes.h @@ -13,8 +13,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * License along with this library; if not, see + * . * * Davide Libenzi * diff --git a/src/xdiff/xutils.c b/deps/xdiff/xutils.c similarity index 76% rename from src/xdiff/xutils.c rename to deps/xdiff/xutils.c index bb7bdee4925..9e36f24875d 100644 --- a/src/xdiff/xutils.c +++ b/deps/xdiff/xutils.c @@ -13,8 +13,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * License along with this library; if not, see + * . * * Davide Libenzi * @@ -23,8 +23,6 @@ #include "xinclude.h" - - long xdl_bogosqrt(long n) { long i; @@ -52,7 +50,7 @@ int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize, mb[2].size = strlen(mb[2].ptr); i++; } - if (ecb->outf(ecb->priv, mb, i) < 0) { + if (ecb->out_line(ecb->priv, mb, i) < 0) { return -1; } @@ -62,14 +60,14 @@ int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize, void *xdl_mmfile_first(mmfile_t *mmf, long *size) { - *size = (long)mmf->size; + *size = mmf->size; return mmf->ptr; } long xdl_mmfile_size(mmfile_t *mmf) { - return (long)mmf->size; + return mmf->size; } @@ -120,40 +118,11 @@ void *xdl_cha_alloc(chastore_t *cha) { return data; } - -void *xdl_cha_first(chastore_t *cha) { - chanode_t *sncur; - - if (!(cha->sncur = sncur = cha->head)) - return NULL; - - cha->scurr = 0; - - return (char *) sncur + sizeof(chanode_t) + cha->scurr; -} - - -void *xdl_cha_next(chastore_t *cha) { - chanode_t *sncur; - - if (!(sncur = cha->sncur)) - return NULL; - cha->scurr += cha->isize; - if (cha->scurr == sncur->icurr) { - if (!(sncur = cha->sncur = sncur->next)) - return NULL; - cha->scurr = 0; - } - - return (char *) sncur + sizeof(chanode_t) + cha->scurr; -} - - long xdl_guess_lines(mmfile_t *mf, long sample) { long nl = 0, size, tsize = 0; char const *data, *cur, *top; - if ((cur = data = xdl_mmfile_first(mf, &size)) != NULL) { + if ((cur = data = xdl_mmfile_first(mf, &size))) { for (top = data + size; nl < sample && cur < top; ) { nl++; if (!(cur = memchr(cur, '\n', top - cur))) @@ -170,6 +139,37 @@ long xdl_guess_lines(mmfile_t *mf, long sample) { return nl + 1; } +int xdl_blankline(const char *line, long size, long flags) +{ + long i; + + if (!(flags & XDF_WHITESPACE_FLAGS)) + return (size <= 1); + + for (i = 0; i < size && XDL_ISSPACE(line[i]); i++) + ; + + return (i == size); +} + +/* + * Have we eaten everything on the line, except for an optional + * CR at the very end? + */ +static int ends_with_optional_cr(const char *l, long s, long i) +{ + int complete = s && l[s-1] == '\n'; + + if (complete) + s--; + if (s == i) + return 1; + /* do not ignore CR at the end of an incomplete line */ + if (complete && s == i + 1 && l[i] == '\r') + return 1; + return 0; +} + int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags) { int i1, i2; @@ -184,7 +184,8 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags) /* * -w matches everything that matches with -b, and -b in turn - * matches everything that matches with --ignore-space-at-eol. + * matches everything that matches with --ignore-space-at-eol, + * which in turn matches everything that matches with --ignore-cr-at-eol. * * Each flavor of ignoring needs different logic to skip whitespaces * while we have both sides to compare. @@ -214,8 +215,18 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags) return 0; } } else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL) { - while (i1 < s1 && i2 < s2 && l1[i1++] == l2[i2++]) - ; /* keep going */ + while (i1 < s1 && i2 < s2 && l1[i1] == l2[i2]) { + i1++; + i2++; + } + } else if (flags & XDF_IGNORE_CR_AT_EOL) { + /* Find the first difference and see how the line ends */ + while (i1 < s1 && i2 < s2 && l1[i1] == l2[i2]) { + i1++; + i2++; + } + return (ends_with_optional_cr(l1, s1, i1) && + ends_with_optional_cr(l2, s2, i2)); } /* @@ -242,9 +253,16 @@ static unsigned long xdl_hash_record_with_whitespace(char const **data, char const *top, long flags) { unsigned long ha = 5381; char const *ptr = *data; + int cr_at_eol_only = (flags & XDF_WHITESPACE_FLAGS) == XDF_IGNORE_CR_AT_EOL; for (; ptr < top && *ptr != '\n'; ptr++) { - if (XDL_ISSPACE(*ptr)) { + if (cr_at_eol_only) { + /* do not ignore CR at the end of an incomplete line */ + if (*ptr == '\r' && + (ptr + 1 < top && ptr[1] == '\n')) + continue; + } + else if (XDL_ISSPACE(*ptr)) { const char *ptr2 = ptr; int at_eol; while (ptr + 1 < top && XDL_ISSPACE(ptr[1]) @@ -276,7 +294,6 @@ static unsigned long xdl_hash_record_with_whitespace(char const **data, return ha; } - unsigned long xdl_hash_record(char const **data, char const *top, long flags) { unsigned long ha = 5381; char const *ptr = *data; @@ -293,7 +310,6 @@ unsigned long xdl_hash_record(char const **data, char const *top, long flags) { return ha; } - unsigned int xdl_hashbits(unsigned int size) { unsigned int val = 1, bits = 0; @@ -321,25 +337,12 @@ int xdl_num_out(char *out, long val) { *str++ = '0'; *str = '\0'; - return (int)(str - out); + return str - out; } - -long xdl_atol(char const *str, char const **next) { - long val, base; - char const *top; - - for (top = str; XDL_ISDIGIT(*top); top++); - if (next) - *next = top; - for (val = 0, base = 1, top--; top >= str; top--, base *= 10) - val += base * (long)(*top - '0'); - return val; -} - - -int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, - const char *func, long funclen, xdemitcb_t *ecb) { +static int xdl_format_hunk_hdr(long s1, long c1, long s2, long c2, + const char *func, long funclen, + xdemitcb_t *ecb) { int nb = 0; mmbuffer_t mb; char buf[128]; @@ -372,8 +375,8 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, nb += 3; if (func && funclen) { buf[nb++] = ' '; - if (funclen > (long)sizeof(buf) - nb - 1) - funclen = (long)sizeof(buf) - nb - 1; + if (funclen > sizeof(buf) - nb - 1) + funclen = sizeof(buf) - nb - 1; memcpy(buf + nb, func, funclen); nb += funclen; } @@ -381,9 +384,21 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, mb.ptr = buf; mb.size = nb; - if (ecb->outf(ecb->priv, &mb, 1) < 0) + if (ecb->out_line(ecb->priv, &mb, 1) < 0) return -1; + return 0; +} +int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, + const char *func, long funclen, + xdemitcb_t *ecb) { + if (!ecb->out_hunk) + return xdl_format_hunk_hdr(s1, c1, s2, c2, func, funclen, ecb); + if (ecb->out_hunk(ecb->priv, + c1 ? s1 : s1 - 1, c1, + c2 ? s2 : s2 - 1, c2, + func, funclen) < 0) + return -1; return 0; } @@ -417,3 +432,20 @@ int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp, return 0; } + +void* xdl_alloc_grow_helper(void *p, long nr, long *alloc, size_t size) +{ + void *tmp = NULL; + size_t n = ((LONG_MAX - 16) / 2 >= *alloc) ? 2 * *alloc + 16 : LONG_MAX; + if (nr > n) + n = nr; + if (SIZE_MAX / size >= n) + tmp = xdl_realloc(p, n * size); + if (tmp) { + *alloc = n; + } else { + xdl_free(p); + *alloc = 0; + } + return tmp; +} diff --git a/src/xdiff/xutils.h b/deps/xdiff/xutils.h similarity index 85% rename from src/xdiff/xutils.h rename to deps/xdiff/xutils.h index 714719a89cb..fd0bba94e8b 100644 --- a/src/xdiff/xutils.h +++ b/deps/xdiff/xutils.h @@ -13,8 +13,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * License along with this library; if not, see + * . * * Davide Libenzi * @@ -31,19 +31,18 @@ int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize, int xdl_cha_init(chastore_t *cha, long isize, long icount); void xdl_cha_free(chastore_t *cha); void *xdl_cha_alloc(chastore_t *cha); -void *xdl_cha_first(chastore_t *cha); -void *xdl_cha_next(chastore_t *cha); long xdl_guess_lines(mmfile_t *mf, long sample); +int xdl_blankline(const char *line, long size, long flags); int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags); unsigned long xdl_hash_record(char const **data, char const *top, long flags); unsigned int xdl_hashbits(unsigned int size); int xdl_num_out(char *out, long val); -long xdl_atol(char const *str, char const **next); int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, const char *func, long funclen, xdemitcb_t *ecb); int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp, int line1, int count1, int line2, int count2); - +/* Do not call this function, use XDL_ALLOC_GROW instead */ +void* xdl_alloc_grow_helper(void* p, long nr, long* alloc, size_t size); #endif /* #if !defined(XUTILS_H) */ diff --git a/deps/zlib/CMakeLists.txt b/deps/zlib/CMakeLists.txt new file mode 100644 index 00000000000..078ce69b32e --- /dev/null +++ b/deps/zlib/CMakeLists.txt @@ -0,0 +1,11 @@ +disable_warnings(implicit-fallthrough) +add_definitions(-DNO_VIZ -DSTDC -DNO_GZIP -DHAVE_SYS_TYPES_H -DHAVE_STDINT_H -DHAVE_STDDEF_H) + +if(MINGW OR MSYS) + add_definitions(-DZ_HAVE_UNISTD_H -D_LFS64_LARGEFILE -D_LARGEFILE64_SOURCE=1) +endif() + +file(GLOB SRC_ZLIB "*.c" "*.h") +list(SORT SRC_ZLIB) +include_directories(".") +add_library(zlib OBJECT ${SRC_ZLIB}) diff --git a/deps/zlib/LICENSE b/deps/zlib/LICENSE new file mode 100644 index 00000000000..ab8ee6f7142 --- /dev/null +++ b/deps/zlib/LICENSE @@ -0,0 +1,22 @@ +Copyright notice: + + (C) 1995-2022 Jean-loup Gailly and Mark Adler + + 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. + + Jean-loup Gailly Mark Adler + jloup@gzip.org madler@alumni.caltech.edu diff --git a/deps/zlib/adler32.c b/deps/zlib/adler32.c index 65ad6a5adc4..04b81d29bad 100644 --- a/deps/zlib/adler32.c +++ b/deps/zlib/adler32.c @@ -1,5 +1,5 @@ /* adler32.c -- compute the Adler-32 checksum of a data stream - * Copyright (C) 1995-2007 Mark Adler + * Copyright (C) 1995-2011, 2016 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -7,11 +7,7 @@ #include "zutil.h" -#define local static - -local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2); - -#define BASE 65521UL /* largest prime smaller than 65536 */ +#define BASE 65521U /* largest prime smaller than 65536 */ #define NMAX 5552 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ @@ -21,47 +17,48 @@ local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2); #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); #define DO16(buf) DO8(buf,0); DO8(buf,8); -/* use NO_DIVIDE if your processor does not do division in hardware */ +/* use NO_DIVIDE if your processor does not do division in hardware -- + try it both ways to see which is faster */ #ifdef NO_DIVIDE -# define MOD(a) \ +/* note that this assumes BASE is 65521, where 65536 % 65521 == 15 + (thank you to John Reiser for pointing this out) */ +# define CHOP(a) \ do { \ - if (a >= (BASE << 16)) a -= (BASE << 16); \ - if (a >= (BASE << 15)) a -= (BASE << 15); \ - if (a >= (BASE << 14)) a -= (BASE << 14); \ - if (a >= (BASE << 13)) a -= (BASE << 13); \ - if (a >= (BASE << 12)) a -= (BASE << 12); \ - if (a >= (BASE << 11)) a -= (BASE << 11); \ - if (a >= (BASE << 10)) a -= (BASE << 10); \ - if (a >= (BASE << 9)) a -= (BASE << 9); \ - if (a >= (BASE << 8)) a -= (BASE << 8); \ - if (a >= (BASE << 7)) a -= (BASE << 7); \ - if (a >= (BASE << 6)) a -= (BASE << 6); \ - if (a >= (BASE << 5)) a -= (BASE << 5); \ - if (a >= (BASE << 4)) a -= (BASE << 4); \ - if (a >= (BASE << 3)) a -= (BASE << 3); \ - if (a >= (BASE << 2)) a -= (BASE << 2); \ - if (a >= (BASE << 1)) a -= (BASE << 1); \ + unsigned long tmp = a >> 16; \ + a &= 0xffffUL; \ + a += (tmp << 4) - tmp; \ + } while (0) +# define MOD28(a) \ + do { \ + CHOP(a); \ if (a >= BASE) a -= BASE; \ } while (0) -# define MOD4(a) \ +# define MOD(a) \ do { \ - if (a >= (BASE << 4)) a -= (BASE << 4); \ - if (a >= (BASE << 3)) a -= (BASE << 3); \ - if (a >= (BASE << 2)) a -= (BASE << 2); \ - if (a >= (BASE << 1)) a -= (BASE << 1); \ + CHOP(a); \ + MOD28(a); \ + } while (0) +# define MOD63(a) \ + do { /* this assumes a is not negative */ \ + z_off64_t tmp = a >> 32; \ + a &= 0xffffffffL; \ + a += (tmp << 8) - (tmp << 5) + tmp; \ + tmp = a >> 16; \ + a &= 0xffffL; \ + a += (tmp << 4) - tmp; \ + tmp = a >> 16; \ + a &= 0xffffL; \ + a += (tmp << 4) - tmp; \ if (a >= BASE) a -= BASE; \ } while (0) #else # define MOD(a) a %= BASE -# define MOD4(a) a %= BASE +# define MOD28(a) a %= BASE +# define MOD63(a) a %= BASE #endif /* ========================================================================= */ -uLong ZEXPORT adler32(adler, buf, len) - uLong adler; - const Bytef *buf; - uInt len; -{ +uLong ZEXPORT adler32_z(uLong adler, const Bytef *buf, z_size_t len) { unsigned long sum2; unsigned n; @@ -92,7 +89,7 @@ uLong ZEXPORT adler32(adler, buf, len) } if (adler >= BASE) adler -= BASE; - MOD4(sum2); /* only added so many BASE's */ + MOD28(sum2); /* only added so many BASE's */ return adler | (sum2 << 16); } @@ -128,17 +125,23 @@ uLong ZEXPORT adler32(adler, buf, len) } /* ========================================================================= */ -local uLong adler32_combine_(adler1, adler2, len2) - uLong adler1; - uLong adler2; - z_off64_t len2; -{ +uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len) { + return adler32_z(adler, buf, len); +} + +/* ========================================================================= */ +local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2) { unsigned long sum1; unsigned long sum2; unsigned rem; + /* for negative len, return invalid adler32 as a clue for debugging */ + if (len2 < 0) + return 0xffffffffUL; + /* the derivation of this formula is left as an exercise for the reader */ - rem = (unsigned)(len2 % BASE); + MOD63(len2); /* assumes len2 >= 0 */ + rem = (unsigned)len2; sum1 = adler1 & 0xffff; sum2 = rem * sum1; MOD(sum2); @@ -146,24 +149,16 @@ local uLong adler32_combine_(adler1, adler2, len2) sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; if (sum1 >= BASE) sum1 -= BASE; if (sum1 >= BASE) sum1 -= BASE; - if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1); + if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1); if (sum2 >= BASE) sum2 -= BASE; return sum1 | (sum2 << 16); } /* ========================================================================= */ -uLong ZEXPORT adler32_combine(adler1, adler2, len2) - uLong adler1; - uLong adler2; - z_off_t len2; -{ +uLong ZEXPORT adler32_combine(uLong adler1, uLong adler2, z_off_t len2) { return adler32_combine_(adler1, adler2, len2); } -uLong ZEXPORT adler32_combine64(adler1, adler2, len2) - uLong adler1; - uLong adler2; - z_off64_t len2; -{ +uLong ZEXPORT adler32_combine64(uLong adler1, uLong adler2, z_off64_t len2) { return adler32_combine_(adler1, adler2, len2); } diff --git a/deps/zlib/crc32.c b/deps/zlib/crc32.c index 91be372d224..6c38f5c04c6 100644 --- a/deps/zlib/crc32.c +++ b/deps/zlib/crc32.c @@ -1,12 +1,10 @@ /* crc32.c -- compute the CRC-32 of a data stream - * Copyright (C) 1995-2006, 2010 Mark Adler + * Copyright (C) 1995-2022 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h * - * Thanks to Rodney Brown for his contribution of faster - * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing - * tables for updating the shift register in one step with three exclusive-ors - * instead of four steps with four exclusive-ors. This results in about a - * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. + * This interleaved implementation of a CRC makes use of pipelined multiple + * arithmetic-logic units, commonly found in modern CPU cores. It is due to + * Kadatch and Jenkins (2010). See doc/crc-doc.1.0.pdf in this distribution. */ /* @(#) $Id$ */ @@ -14,9 +12,12 @@ /* Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore protection on the static variables used to control the first-use generation - of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should + of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should first call get_crc_table() to initialize the tables before allowing more than one thread to use crc32(). + + MAKECRCH can be #defined to write out crc32.h. A main() routine is also + produced, so that this one source file can be compiled to an executable. */ #ifdef MAKECRCH @@ -26,417 +27,1023 @@ # endif /* !DYNAMIC_CRC_TABLE */ #endif /* MAKECRCH */ -#include "zutil.h" /* for STDC and FAR definitions */ +#include "zutil.h" /* for Z_U4, Z_U8, z_crc_t, and FAR definitions */ + + /* + A CRC of a message is computed on N braids of words in the message, where + each word consists of W bytes (4 or 8). If N is 3, for example, then three + running sparse CRCs are calculated respectively on each braid, at these + indices in the array of words: 0, 3, 6, ..., 1, 4, 7, ..., and 2, 5, 8, ... + This is done starting at a word boundary, and continues until as many blocks + of N * W bytes as are available have been processed. The results are combined + into a single CRC at the end. For this code, N must be in the range 1..6 and + W must be 4 or 8. The upper limit on N can be increased if desired by adding + more #if blocks, extending the patterns apparent in the code. In addition, + crc32.h would need to be regenerated, if the maximum N value is increased. + + N and W are chosen empirically by benchmarking the execution time on a given + processor. The choices for N and W below were based on testing on Intel Kaby + Lake i7, AMD Ryzen 7, ARM Cortex-A57, Sparc64-VII, PowerPC POWER9, and MIPS64 + Octeon II processors. The Intel, AMD, and ARM processors were all fastest + with N=5, W=8. The Sparc, PowerPC, and MIPS64 were all fastest at N=5, W=4. + They were all tested with either gcc or clang, all using the -O3 optimization + level. Your mileage may vary. + */ + +/* Define N */ +#ifdef Z_TESTN +# define N Z_TESTN +#else +# define N 5 +#endif +#if N < 1 || N > 6 +# error N must be in 1..6 +#endif -#define local static +/* + z_crc_t must be at least 32 bits. z_word_t must be at least as long as + z_crc_t. It is assumed here that z_word_t is either 32 bits or 64 bits, and + that bytes are eight bits. + */ -/* Find a four-byte integer type for crc32_little() and crc32_big(). */ -#ifndef NOBYFOUR -# ifdef STDC /* need ANSI C limits.h to determine sizes */ -# include -# define BYFOUR -# if (UINT_MAX == 0xffffffffUL) - typedef unsigned int u4; +/* + Define W and the associated z_word_t type. If W is not defined, then a + braided calculation is not used, and the associated tables and code are not + compiled. + */ +#ifdef Z_TESTW +# if Z_TESTW-1 != -1 +# define W Z_TESTW +# endif +#else +# ifdef MAKECRCH +# define W 8 /* required for MAKECRCH */ +# else +# if defined(__x86_64__) || defined(__aarch64__) +# define W 8 # else -# if (ULONG_MAX == 0xffffffffUL) - typedef unsigned long u4; -# else -# if (USHRT_MAX == 0xffffffffUL) - typedef unsigned short u4; -# else -# undef BYFOUR /* can't find a four-byte integer type! */ -# endif -# endif +# define W 4 # endif -# endif /* STDC */ -#endif /* !NOBYFOUR */ - -/* Definitions for doing the crc four data bytes at a time. */ -#ifdef BYFOUR -# define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \ - (((w)&0xff00)<<8)+(((w)&0xff)<<24)) - local unsigned long crc32_little OF((unsigned long, - const unsigned char FAR *, unsigned)); - local unsigned long crc32_big OF((unsigned long, - const unsigned char FAR *, unsigned)); -# define TBLS 8 -#else -# define TBLS 1 -#endif /* BYFOUR */ +# endif +#endif +#ifdef W +# if W == 8 && defined(Z_U8) + typedef Z_U8 z_word_t; +# elif defined(Z_U4) +# undef W +# define W 4 + typedef Z_U4 z_word_t; +# else +# undef W +# endif +#endif -/* Local functions for crc concatenation */ -local unsigned long gf2_matrix_times OF((unsigned long *mat, - unsigned long vec)); -local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); -local uLong crc32_combine_(uLong crc1, uLong crc2, z_off64_t len2); +/* If available, use the ARM processor CRC32 instruction. */ +#if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32) && W == 8 +# define ARMCRC32 +#endif +#if defined(W) && (!defined(ARMCRC32) || defined(DYNAMIC_CRC_TABLE)) +/* + Swap the bytes in a z_word_t to convert between little and big endian. Any + self-respecting compiler will optimize this to a single machine byte-swap + instruction, if one is available. This assumes that word_t is either 32 bits + or 64 bits. + */ +local z_word_t byte_swap(z_word_t word) { +# if W == 8 + return + (word & 0xff00000000000000) >> 56 | + (word & 0xff000000000000) >> 40 | + (word & 0xff0000000000) >> 24 | + (word & 0xff00000000) >> 8 | + (word & 0xff000000) << 8 | + (word & 0xff0000) << 24 | + (word & 0xff00) << 40 | + (word & 0xff) << 56; +# else /* W == 4 */ + return + (word & 0xff000000) >> 24 | + (word & 0xff0000) >> 8 | + (word & 0xff00) << 8 | + (word & 0xff) << 24; +# endif +} +#endif #ifdef DYNAMIC_CRC_TABLE +/* ========================================================================= + * Table of powers of x for combining CRC-32s, filled in by make_crc_table() + * below. + */ + local z_crc_t FAR x2n_table[32]; +#else +/* ========================================================================= + * Tables for byte-wise and braided CRC-32 calculations, and a table of powers + * of x for combining CRC-32s, all made by make_crc_table(). + */ +# include "crc32.h" +#endif + +/* CRC polynomial. */ +#define POLY 0xedb88320 /* p(x) reflected, with x^32 implied */ + +/* + Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC polynomial, + reflected. For speed, this requires that a not be zero. + */ +local z_crc_t multmodp(z_crc_t a, z_crc_t b) { + z_crc_t m, p; + + m = (z_crc_t)1 << 31; + p = 0; + for (;;) { + if (a & m) { + p ^= b; + if ((a & (m - 1)) == 0) + break; + } + m >>= 1; + b = b & 1 ? (b >> 1) ^ POLY : b >> 1; + } + return p; +} + +/* + Return x^(n * 2^k) modulo p(x). Requires that x2n_table[] has been + initialized. + */ +local z_crc_t x2nmodp(z_off64_t n, unsigned k) { + z_crc_t p; + + p = (z_crc_t)1 << 31; /* x^0 == 1 */ + while (n) { + if (n & 1) + p = multmodp(x2n_table[k & 31], p); + n >>= 1; + k++; + } + return p; +} -local volatile int crc_table_empty = 1; -local unsigned long FAR crc_table[TBLS][256]; -local void make_crc_table OF((void)); +#ifdef DYNAMIC_CRC_TABLE +/* ========================================================================= + * Build the tables for byte-wise and braided CRC-32 calculations, and a table + * of powers of x for combining CRC-32s. + */ +local z_crc_t FAR crc_table[256]; +#ifdef W + local z_word_t FAR crc_big_table[256]; + local z_crc_t FAR crc_braid_table[W][256]; + local z_word_t FAR crc_braid_big_table[W][256]; + local void braid(z_crc_t [][256], z_word_t [][256], int, int); +#endif #ifdef MAKECRCH - local void write_table OF((FILE *, const unsigned long FAR *)); + local void write_table(FILE *, const z_crc_t FAR *, int); + local void write_table32hi(FILE *, const z_word_t FAR *, int); + local void write_table64(FILE *, const z_word_t FAR *, int); #endif /* MAKECRCH */ + +/* + Define a once() function depending on the availability of atomics. If this is + compiled with DYNAMIC_CRC_TABLE defined, and if CRCs will be computed in + multiple threads, and if atomics are not available, then get_crc_table() must + be called to initialize the tables and must return before any threads are + allowed to compute or combine CRCs. + */ + +/* Definition of once functionality. */ +typedef struct once_s once_t; + +/* Check for the availability of atomics. */ +#if defined(__STDC__) && __STDC_VERSION__ >= 201112L && \ + !defined(__STDC_NO_ATOMICS__) + +#include + +/* Structure for once(), which must be initialized with ONCE_INIT. */ +struct once_s { + atomic_flag begun; + atomic_int done; +}; +#define ONCE_INIT {ATOMIC_FLAG_INIT, 0} + +/* + Run the provided init() function exactly once, even if multiple threads + invoke once() at the same time. The state must be a once_t initialized with + ONCE_INIT. + */ +local void once(once_t *state, void (*init)(void)) { + if (!atomic_load(&state->done)) { + if (atomic_flag_test_and_set(&state->begun)) + while (!atomic_load(&state->done)) + ; + else { + init(); + atomic_store(&state->done, 1); + } + } +} + +#else /* no atomics */ + +/* Structure for once(), which must be initialized with ONCE_INIT. */ +struct once_s { + volatile int begun; + volatile int done; +}; +#define ONCE_INIT {0, 0} + +/* Test and set. Alas, not atomic, but tries to minimize the period of + vulnerability. */ +local int test_and_set(int volatile *flag) { + int was; + + was = *flag; + *flag = 1; + return was; +} + +/* Run the provided init() function once. This is not thread-safe. */ +local void once(once_t *state, void (*init)(void)) { + if (!state->done) { + if (test_and_set(&state->begun)) + while (!state->done) + ; + else { + init(); + state->done = 1; + } + } +} + +#endif + +/* State for once(). */ +local once_t made = ONCE_INIT; + /* Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. Polynomials over GF(2) are represented in binary, one bit per coefficient, - with the lowest powers in the most significant bit. Then adding polynomials + with the lowest powers in the most significant bit. Then adding polynomials is just exclusive-or, and multiplying a polynomial by x is a right shift by - one. If we call the above polynomial p, and represent a byte as the + one. If we call the above polynomial p, and represent a byte as the polynomial q, also with the lowest power in the most significant bit (so the - byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, + byte 0xb1 is the polynomial x^7+x^3+x^2+1), then the CRC is (q*x^32) mod p, where a mod b means the remainder after dividing a by b. This calculation is done using the shift-register method of multiplying and - taking the remainder. The register is initialized to zero, and for each + taking the remainder. The register is initialized to zero, and for each incoming bit, x^32 is added mod p to the register if the bit is a one (where - x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by - x (which is shifting right by one and adding x^32 mod p if the bit shifted - out is a one). We start with the highest power (least significant bit) of - q and repeat for all eight bits of q. - - The first table is simply the CRC of all possible eight bit values. This is - all the information needed to generate CRCs on data a byte at a time for all - combinations of CRC register values and incoming bytes. The remaining tables - allow for word-at-a-time CRC calculation for both big-endian and little- - endian machines, where a word is four bytes. -*/ -local void make_crc_table() -{ - unsigned long c; - int n, k; - unsigned long poly; /* polynomial exclusive-or pattern */ - /* terms of polynomial defining this crc (except x^32): */ - static volatile int first = 1; /* flag to limit concurrent making */ - static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; - - /* See if another task is already doing this (not thread-safe, but better - than nothing -- significantly reduces duration of vulnerability in - case the advice about DYNAMIC_CRC_TABLE is ignored) */ - if (first) { - first = 0; - - /* make exclusive-or pattern from polynomial (0xedb88320UL) */ - poly = 0UL; - for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++) - poly |= 1UL << (31 - p[n]); - - /* generate a crc for every 8-bit value */ - for (n = 0; n < 256; n++) { - c = (unsigned long)n; - for (k = 0; k < 8; k++) - c = c & 1 ? poly ^ (c >> 1) : c >> 1; - crc_table[0][n] = c; - } + x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by x + (which is shifting right by one and adding x^32 mod p if the bit shifted out + is a one). We start with the highest power (least significant bit) of q and + repeat for all eight bits of q. -#ifdef BYFOUR - /* generate crc for each value followed by one, two, and three zeros, - and then the byte reversal of those as well as the first table */ - for (n = 0; n < 256; n++) { - c = crc_table[0][n]; - crc_table[4][n] = REV(c); - for (k = 1; k < 4; k++) { - c = crc_table[0][c & 0xff] ^ (c >> 8); - crc_table[k][n] = c; - crc_table[k + 4][n] = REV(c); - } - } -#endif /* BYFOUR */ + The table is simply the CRC of all possible eight bit values. This is all the + information needed to generate CRCs on data a byte at a time for all + combinations of CRC register values and incoming bytes. + */ - crc_table_empty = 0; - } - else { /* not first */ - /* wait for the other guy to finish (not efficient, but rare) */ - while (crc_table_empty) - ; +local void make_crc_table(void) { + unsigned i, j, n; + z_crc_t p; + + /* initialize the CRC of bytes tables */ + for (i = 0; i < 256; i++) { + p = i; + for (j = 0; j < 8; j++) + p = p & 1 ? (p >> 1) ^ POLY : p >> 1; + crc_table[i] = p; +#ifdef W + crc_big_table[i] = byte_swap(p); +#endif } + /* initialize the x^2^n mod p(x) table */ + p = (z_crc_t)1 << 30; /* x^1 */ + x2n_table[0] = p; + for (n = 1; n < 32; n++) + x2n_table[n] = p = multmodp(p, p); + +#ifdef W + /* initialize the braiding tables -- needs x2n_table[] */ + braid(crc_braid_table, crc_braid_big_table, N, W); +#endif + #ifdef MAKECRCH - /* write out CRC tables to crc32.h */ { + /* + The crc32.h header file contains tables for both 32-bit and 64-bit + z_word_t's, and so requires a 64-bit type be available. In that case, + z_word_t must be defined to be 64-bits. This code then also generates + and writes out the tables for the case that z_word_t is 32 bits. + */ +#if !defined(W) || W != 8 +# error Need a 64-bit integer type in order to generate crc32.h. +#endif FILE *out; + int k, n; + z_crc_t ltl[8][256]; + z_word_t big[8][256]; out = fopen("crc32.h", "w"); if (out == NULL) return; - fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); - fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); - fprintf(out, "local const unsigned long FAR "); - fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); - write_table(out, crc_table[0]); -# ifdef BYFOUR - fprintf(out, "#ifdef BYFOUR\n"); - for (k = 1; k < 8; k++) { - fprintf(out, " },\n {\n"); - write_table(out, crc_table[k]); + + /* write out little-endian CRC table to crc32.h */ + fprintf(out, + "/* crc32.h -- tables for rapid CRC calculation\n" + " * Generated automatically by crc32.c\n */\n" + "\n" + "local const z_crc_t FAR crc_table[] = {\n" + " "); + write_table(out, crc_table, 256); + fprintf(out, + "};\n"); + + /* write out big-endian CRC table for 64-bit z_word_t to crc32.h */ + fprintf(out, + "\n" + "#ifdef W\n" + "\n" + "#if W == 8\n" + "\n" + "local const z_word_t FAR crc_big_table[] = {\n" + " "); + write_table64(out, crc_big_table, 256); + fprintf(out, + "};\n"); + + /* write out big-endian CRC table for 32-bit z_word_t to crc32.h */ + fprintf(out, + "\n" + "#else /* W == 4 */\n" + "\n" + "local const z_word_t FAR crc_big_table[] = {\n" + " "); + write_table32hi(out, crc_big_table, 256); + fprintf(out, + "};\n" + "\n" + "#endif\n"); + + /* write out braid tables for each value of N */ + for (n = 1; n <= 6; n++) { + fprintf(out, + "\n" + "#if N == %d\n", n); + + /* compute braid tables for this N and 64-bit word_t */ + braid(ltl, big, n, 8); + + /* write out braid tables for 64-bit z_word_t to crc32.h */ + fprintf(out, + "\n" + "#if W == 8\n" + "\n" + "local const z_crc_t FAR crc_braid_table[][256] = {\n"); + for (k = 0; k < 8; k++) { + fprintf(out, " {"); + write_table(out, ltl[k], 256); + fprintf(out, "}%s", k < 7 ? ",\n" : ""); + } + fprintf(out, + "};\n" + "\n" + "local const z_word_t FAR crc_braid_big_table[][256] = {\n"); + for (k = 0; k < 8; k++) { + fprintf(out, " {"); + write_table64(out, big[k], 256); + fprintf(out, "}%s", k < 7 ? ",\n" : ""); + } + fprintf(out, + "};\n"); + + /* compute braid tables for this N and 32-bit word_t */ + braid(ltl, big, n, 4); + + /* write out braid tables for 32-bit z_word_t to crc32.h */ + fprintf(out, + "\n" + "#else /* W == 4 */\n" + "\n" + "local const z_crc_t FAR crc_braid_table[][256] = {\n"); + for (k = 0; k < 4; k++) { + fprintf(out, " {"); + write_table(out, ltl[k], 256); + fprintf(out, "}%s", k < 3 ? ",\n" : ""); + } + fprintf(out, + "};\n" + "\n" + "local const z_word_t FAR crc_braid_big_table[][256] = {\n"); + for (k = 0; k < 4; k++) { + fprintf(out, " {"); + write_table32hi(out, big[k], 256); + fprintf(out, "}%s", k < 3 ? ",\n" : ""); + } + fprintf(out, + "};\n" + "\n" + "#endif\n" + "\n" + "#endif\n"); } - fprintf(out, "#endif\n"); -# endif /* BYFOUR */ - fprintf(out, " }\n};\n"); + fprintf(out, + "\n" + "#endif\n"); + + /* write out zeros operator table to crc32.h */ + fprintf(out, + "\n" + "local const z_crc_t FAR x2n_table[] = {\n" + " "); + write_table(out, x2n_table, 32); + fprintf(out, + "};\n"); fclose(out); } #endif /* MAKECRCH */ } #ifdef MAKECRCH -local void write_table(out, table) - FILE *out; - const unsigned long FAR *table; -{ + +/* + Write the 32-bit values in table[0..k-1] to out, five per line in + hexadecimal separated by commas. + */ +local void write_table(FILE *out, const z_crc_t FAR *table, int k) { + int n; + + for (n = 0; n < k; n++) + fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ", + (unsigned long)(table[n]), + n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", ")); +} + +/* + Write the high 32-bits of each value in table[0..k-1] to out, five per line + in hexadecimal separated by commas. + */ +local void write_table32hi(FILE *out, const z_word_t FAR *table, int k) { int n; - for (n = 0; n < 256; n++) - fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n], - n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); + for (n = 0; n < k; n++) + fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ", + (unsigned long)(table[n] >> 32), + n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", ")); } + +/* + Write the 64-bit values in table[0..k-1] to out, three per line in + hexadecimal separated by commas. This assumes that if there is a 64-bit + type, then there is also a long long integer type, and it is at least 64 + bits. If not, then the type cast and format string can be adjusted + accordingly. + */ +local void write_table64(FILE *out, const z_word_t FAR *table, int k) { + int n; + + for (n = 0; n < k; n++) + fprintf(out, "%s0x%016llx%s", n == 0 || n % 3 ? "" : " ", + (unsigned long long)(table[n]), + n == k - 1 ? "" : (n % 3 == 2 ? ",\n" : ", ")); +} + +/* Actually do the deed. */ +int main(void) { + make_crc_table(); + return 0; +} + #endif /* MAKECRCH */ -#else /* !DYNAMIC_CRC_TABLE */ -/* ======================================================================== - * Tables of CRC-32s of all single-byte values, made by make_crc_table(). +#ifdef W +/* + Generate the little and big-endian braid tables for the given n and z_word_t + size w. Each array must have room for w blocks of 256 elements. */ -#include "crc32.h" +local void braid(z_crc_t ltl[][256], z_word_t big[][256], int n, int w) { + int k; + z_crc_t i, p, q; + for (k = 0; k < w; k++) { + p = x2nmodp((n * w + 3 - k) << 3, 0); + ltl[k][0] = 0; + big[w - 1 - k][0] = 0; + for (i = 1; i < 256; i++) { + ltl[k][i] = q = multmodp(i << 24, p); + big[w - 1 - k][i] = byte_swap(q); + } + } +} +#endif + #endif /* DYNAMIC_CRC_TABLE */ /* ========================================================================= - * This function can be used by asm versions of crc32() + * This function can be used by asm versions of crc32(), and to force the + * generation of the CRC tables in a threaded application. */ -const unsigned long FAR * ZEXPORT get_crc_table() -{ +const z_crc_t FAR * ZEXPORT get_crc_table(void) { #ifdef DYNAMIC_CRC_TABLE - if (crc_table_empty) - make_crc_table(); + once(&made, make_crc_table); #endif /* DYNAMIC_CRC_TABLE */ - return (const unsigned long FAR *)crc_table; + return (const z_crc_t FAR *)crc_table; } -/* ========================================================================= */ -#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) -#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 +/* ========================================================================= + * Use ARM machine instructions if available. This will compute the CRC about + * ten times faster than the braided calculation. This code does not check for + * the presence of the CRC instruction at run time. __ARM_FEATURE_CRC32 will + * only be defined if the compilation specifies an ARM processor architecture + * that has the instructions. For example, compiling with -march=armv8.1-a or + * -march=armv8-a+crc, or -march=native if the compile machine has the crc32 + * instructions. + */ +#ifdef ARMCRC32 -/* ========================================================================= */ -unsigned long ZEXPORT crc32(crc, buf, len) - unsigned long crc; - const unsigned char FAR *buf; - uInt len; -{ - if (buf == Z_NULL) return 0UL; +/* + Constants empirically determined to maximize speed. These values are from + measurements on a Cortex-A57. Your mileage may vary. + */ +#define Z_BATCH 3990 /* number of words in a batch */ +#define Z_BATCH_ZEROS 0xa10d3d0c /* computed from Z_BATCH = 3990 */ +#define Z_BATCH_MIN 800 /* fewest words in a final batch */ + +unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf, + z_size_t len) { + z_crc_t val; + z_word_t crc1, crc2; + const z_word_t *word; + z_word_t val0, val1, val2; + z_size_t last, last2, i; + z_size_t num; + + /* Return initial CRC, if requested. */ + if (buf == Z_NULL) return 0; #ifdef DYNAMIC_CRC_TABLE - if (crc_table_empty) - make_crc_table(); + once(&made, make_crc_table); #endif /* DYNAMIC_CRC_TABLE */ -#ifdef BYFOUR - if (sizeof(void *) == sizeof(ptrdiff_t)) { - u4 endian; + /* Pre-condition the CRC */ + crc = (~crc) & 0xffffffff; - endian = 1; - if (*((unsigned char *)(&endian))) - return crc32_little(crc, buf, len); - else - return crc32_big(crc, buf, len); - } -#endif /* BYFOUR */ - crc = crc ^ 0xffffffffUL; - while (len >= 8) { - DO8; - len -= 8; + /* Compute the CRC up to a word boundary. */ + while (len && ((z_size_t)buf & 7) != 0) { + len--; + val = *buf++; + __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val)); } - if (len) do { - DO1; - } while (--len); - return crc ^ 0xffffffffUL; -} -#ifdef BYFOUR + /* Prepare to compute the CRC on full 64-bit words word[0..num-1]. */ + word = (z_word_t const *)buf; + num = len >> 3; + len &= 7; -/* ========================================================================= */ -#define DOLIT4 c ^= *buf4++; \ - c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ - crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] -#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 + /* Do three interleaved CRCs to realize the throughput of one crc32x + instruction per cycle. Each CRC is calculated on Z_BATCH words. The + three CRCs are combined into a single CRC after each set of batches. */ + while (num >= 3 * Z_BATCH) { + crc1 = 0; + crc2 = 0; + for (i = 0; i < Z_BATCH; i++) { + val0 = word[i]; + val1 = word[i + Z_BATCH]; + val2 = word[i + 2 * Z_BATCH]; + __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0)); + __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1)); + __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2)); + } + word += 3 * Z_BATCH; + num -= 3 * Z_BATCH; + crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc1; + crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc2; + } -/* ========================================================================= */ -local unsigned long crc32_little(crc, buf, len) - unsigned long crc; - const unsigned char FAR *buf; - unsigned len; -{ - register u4 c; - register const u4 FAR *buf4; - - c = (u4)crc; - c = ~c; - while (len && ((ptrdiff_t)buf & 3)) { - c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); - len--; + /* Do one last smaller batch with the remaining words, if there are enough + to pay for the combination of CRCs. */ + last = num / 3; + if (last >= Z_BATCH_MIN) { + last2 = last << 1; + crc1 = 0; + crc2 = 0; + for (i = 0; i < last; i++) { + val0 = word[i]; + val1 = word[i + last]; + val2 = word[i + last2]; + __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0)); + __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1)); + __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2)); + } + word += 3 * last; + num -= 3 * last; + val = x2nmodp(last, 6); + crc = multmodp(val, crc) ^ crc1; + crc = multmodp(val, crc) ^ crc2; } - buf4 = (const u4 FAR *)(const void FAR *)buf; - while (len >= 32) { - DOLIT32; - len -= 32; + /* Compute the CRC on any remaining words. */ + for (i = 0; i < num; i++) { + val0 = word[i]; + __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0)); } - while (len >= 4) { - DOLIT4; - len -= 4; + word += num; + + /* Complete the CRC on any remaining bytes. */ + buf = (const unsigned char FAR *)word; + while (len) { + len--; + val = *buf++; + __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val)); } - buf = (const unsigned char FAR *)buf4; - if (len) do { - c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); - } while (--len); - c = ~c; - return (unsigned long)c; + /* Return the CRC, post-conditioned. */ + return crc ^ 0xffffffff; } -/* ========================================================================= */ -#define DOBIG4 c ^= *++buf4; \ - c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ - crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] -#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 +#else + +#ifdef W + +/* + Return the CRC of the W bytes in the word_t data, taking the + least-significant byte of the word as the first byte of data, without any pre + or post conditioning. This is used to combine the CRCs of each braid. + */ +local z_crc_t crc_word(z_word_t data) { + int k; + for (k = 0; k < W; k++) + data = (data >> 8) ^ crc_table[data & 0xff]; + return (z_crc_t)data; +} + +local z_word_t crc_word_big(z_word_t data) { + int k; + for (k = 0; k < W; k++) + data = (data << 8) ^ + crc_big_table[(data >> ((W - 1) << 3)) & 0xff]; + return data; +} + +#endif /* ========================================================================= */ -local unsigned long crc32_big(crc, buf, len) - unsigned long crc; - const unsigned char FAR *buf; - unsigned len; -{ - register u4 c; - register const u4 FAR *buf4; - - c = REV((u4)crc); - c = ~c; - while (len && ((ptrdiff_t)buf & 3)) { - c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); - len--; +unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf, + z_size_t len) { + /* Return initial CRC, if requested. */ + if (buf == Z_NULL) return 0; + +#ifdef DYNAMIC_CRC_TABLE + once(&made, make_crc_table); +#endif /* DYNAMIC_CRC_TABLE */ + + /* Pre-condition the CRC */ + crc = (~crc) & 0xffffffff; + +#ifdef W + + /* If provided enough bytes, do a braided CRC calculation. */ + if (len >= N * W + W - 1) { + z_size_t blks; + z_word_t const *words; + unsigned endian; + int k; + + /* Compute the CRC up to a z_word_t boundary. */ + while (len && ((z_size_t)buf & (W - 1)) != 0) { + len--; + crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; + } + + /* Compute the CRC on as many N z_word_t blocks as are available. */ + blks = len / (N * W); + len -= blks * N * W; + words = (z_word_t const *)buf; + + /* Do endian check at execution time instead of compile time, since ARM + processors can change the endianness at execution time. If the + compiler knows what the endianness will be, it can optimize out the + check and the unused branch. */ + endian = 1; + if (*(unsigned char *)&endian) { + /* Little endian. */ + + z_crc_t crc0; + z_word_t word0; +#if N > 1 + z_crc_t crc1; + z_word_t word1; +#if N > 2 + z_crc_t crc2; + z_word_t word2; +#if N > 3 + z_crc_t crc3; + z_word_t word3; +#if N > 4 + z_crc_t crc4; + z_word_t word4; +#if N > 5 + z_crc_t crc5; + z_word_t word5; +#endif +#endif +#endif +#endif +#endif + + /* Initialize the CRC for each braid. */ + crc0 = crc; +#if N > 1 + crc1 = 0; +#if N > 2 + crc2 = 0; +#if N > 3 + crc3 = 0; +#if N > 4 + crc4 = 0; +#if N > 5 + crc5 = 0; +#endif +#endif +#endif +#endif +#endif + + /* + Process the first blks-1 blocks, computing the CRCs on each braid + independently. + */ + while (--blks) { + /* Load the word for each braid into registers. */ + word0 = crc0 ^ words[0]; +#if N > 1 + word1 = crc1 ^ words[1]; +#if N > 2 + word2 = crc2 ^ words[2]; +#if N > 3 + word3 = crc3 ^ words[3]; +#if N > 4 + word4 = crc4 ^ words[4]; +#if N > 5 + word5 = crc5 ^ words[5]; +#endif +#endif +#endif +#endif +#endif + words += N; + + /* Compute and update the CRC for each word. The loop should + get unrolled. */ + crc0 = crc_braid_table[0][word0 & 0xff]; +#if N > 1 + crc1 = crc_braid_table[0][word1 & 0xff]; +#if N > 2 + crc2 = crc_braid_table[0][word2 & 0xff]; +#if N > 3 + crc3 = crc_braid_table[0][word3 & 0xff]; +#if N > 4 + crc4 = crc_braid_table[0][word4 & 0xff]; +#if N > 5 + crc5 = crc_braid_table[0][word5 & 0xff]; +#endif +#endif +#endif +#endif +#endif + for (k = 1; k < W; k++) { + crc0 ^= crc_braid_table[k][(word0 >> (k << 3)) & 0xff]; +#if N > 1 + crc1 ^= crc_braid_table[k][(word1 >> (k << 3)) & 0xff]; +#if N > 2 + crc2 ^= crc_braid_table[k][(word2 >> (k << 3)) & 0xff]; +#if N > 3 + crc3 ^= crc_braid_table[k][(word3 >> (k << 3)) & 0xff]; +#if N > 4 + crc4 ^= crc_braid_table[k][(word4 >> (k << 3)) & 0xff]; +#if N > 5 + crc5 ^= crc_braid_table[k][(word5 >> (k << 3)) & 0xff]; +#endif +#endif +#endif +#endif +#endif + } + } + + /* + Process the last block, combining the CRCs of the N braids at the + same time. + */ + crc = crc_word(crc0 ^ words[0]); +#if N > 1 + crc = crc_word(crc1 ^ words[1] ^ crc); +#if N > 2 + crc = crc_word(crc2 ^ words[2] ^ crc); +#if N > 3 + crc = crc_word(crc3 ^ words[3] ^ crc); +#if N > 4 + crc = crc_word(crc4 ^ words[4] ^ crc); +#if N > 5 + crc = crc_word(crc5 ^ words[5] ^ crc); +#endif +#endif +#endif +#endif +#endif + words += N; + } + else { + /* Big endian. */ + + z_word_t crc0, word0, comb; +#if N > 1 + z_word_t crc1, word1; +#if N > 2 + z_word_t crc2, word2; +#if N > 3 + z_word_t crc3, word3; +#if N > 4 + z_word_t crc4, word4; +#if N > 5 + z_word_t crc5, word5; +#endif +#endif +#endif +#endif +#endif + + /* Initialize the CRC for each braid. */ + crc0 = byte_swap(crc); +#if N > 1 + crc1 = 0; +#if N > 2 + crc2 = 0; +#if N > 3 + crc3 = 0; +#if N > 4 + crc4 = 0; +#if N > 5 + crc5 = 0; +#endif +#endif +#endif +#endif +#endif + + /* + Process the first blks-1 blocks, computing the CRCs on each braid + independently. + */ + while (--blks) { + /* Load the word for each braid into registers. */ + word0 = crc0 ^ words[0]; +#if N > 1 + word1 = crc1 ^ words[1]; +#if N > 2 + word2 = crc2 ^ words[2]; +#if N > 3 + word3 = crc3 ^ words[3]; +#if N > 4 + word4 = crc4 ^ words[4]; +#if N > 5 + word5 = crc5 ^ words[5]; +#endif +#endif +#endif +#endif +#endif + words += N; + + /* Compute and update the CRC for each word. The loop should + get unrolled. */ + crc0 = crc_braid_big_table[0][word0 & 0xff]; +#if N > 1 + crc1 = crc_braid_big_table[0][word1 & 0xff]; +#if N > 2 + crc2 = crc_braid_big_table[0][word2 & 0xff]; +#if N > 3 + crc3 = crc_braid_big_table[0][word3 & 0xff]; +#if N > 4 + crc4 = crc_braid_big_table[0][word4 & 0xff]; +#if N > 5 + crc5 = crc_braid_big_table[0][word5 & 0xff]; +#endif +#endif +#endif +#endif +#endif + for (k = 1; k < W; k++) { + crc0 ^= crc_braid_big_table[k][(word0 >> (k << 3)) & 0xff]; +#if N > 1 + crc1 ^= crc_braid_big_table[k][(word1 >> (k << 3)) & 0xff]; +#if N > 2 + crc2 ^= crc_braid_big_table[k][(word2 >> (k << 3)) & 0xff]; +#if N > 3 + crc3 ^= crc_braid_big_table[k][(word3 >> (k << 3)) & 0xff]; +#if N > 4 + crc4 ^= crc_braid_big_table[k][(word4 >> (k << 3)) & 0xff]; +#if N > 5 + crc5 ^= crc_braid_big_table[k][(word5 >> (k << 3)) & 0xff]; +#endif +#endif +#endif +#endif +#endif + } + } + + /* + Process the last block, combining the CRCs of the N braids at the + same time. + */ + comb = crc_word_big(crc0 ^ words[0]); +#if N > 1 + comb = crc_word_big(crc1 ^ words[1] ^ comb); +#if N > 2 + comb = crc_word_big(crc2 ^ words[2] ^ comb); +#if N > 3 + comb = crc_word_big(crc3 ^ words[3] ^ comb); +#if N > 4 + comb = crc_word_big(crc4 ^ words[4] ^ comb); +#if N > 5 + comb = crc_word_big(crc5 ^ words[5] ^ comb); +#endif +#endif +#endif +#endif +#endif + words += N; + crc = byte_swap(comb); + } + + /* + Update the pointer to the remaining bytes to process. + */ + buf = (unsigned char const *)words; } - buf4 = (const u4 FAR *)(const void FAR *)buf; - buf4--; - while (len >= 32) { - DOBIG32; - len -= 32; +#endif /* W */ + + /* Complete the computation of the CRC on any remaining bytes. */ + while (len >= 8) { + len -= 8; + crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; + crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; + crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; + crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; + crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; + crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; + crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; + crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; } - while (len >= 4) { - DOBIG4; - len -= 4; + while (len) { + len--; + crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; } - buf4++; - buf = (const unsigned char FAR *)buf4; - - if (len) do { - c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); - } while (--len); - c = ~c; - return (unsigned long)(REV(c)); -} -#endif /* BYFOUR */ + /* Return the CRC, post-conditioned. */ + return crc ^ 0xffffffff; +} -#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ +#endif /* ========================================================================= */ -local unsigned long gf2_matrix_times(mat, vec) - unsigned long *mat; - unsigned long vec; -{ - unsigned long sum; - - sum = 0; - while (vec) { - if (vec & 1) - sum ^= *mat; - vec >>= 1; - mat++; - } - return sum; +unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, + uInt len) { + return crc32_z(crc, buf, len); } /* ========================================================================= */ -local void gf2_matrix_square(square, mat) - unsigned long *square; - unsigned long *mat; -{ - int n; - - for (n = 0; n < GF2_DIM; n++) - square[n] = gf2_matrix_times(mat, mat[n]); +uLong ZEXPORT crc32_combine64(uLong crc1, uLong crc2, z_off64_t len2) { +#ifdef DYNAMIC_CRC_TABLE + once(&made, make_crc_table); +#endif /* DYNAMIC_CRC_TABLE */ + return multmodp(x2nmodp(len2, 3), crc1) ^ (crc2 & 0xffffffff); } /* ========================================================================= */ -local uLong crc32_combine_(crc1, crc2, len2) - uLong crc1; - uLong crc2; - z_off64_t len2; -{ - int n; - unsigned long row; - unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ - unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ - - /* degenerate case (also disallow negative lengths) */ - if (len2 <= 0) - return crc1; - - /* put operator for one zero bit in odd */ - odd[0] = 0xedb88320UL; /* CRC-32 polynomial */ - row = 1; - for (n = 1; n < GF2_DIM; n++) { - odd[n] = row; - row <<= 1; - } +uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2) { + return crc32_combine64(crc1, crc2, (z_off64_t)len2); +} - /* put operator for two zero bits in even */ - gf2_matrix_square(even, odd); - - /* put operator for four zero bits in odd */ - gf2_matrix_square(odd, even); - - /* apply len2 zeros to crc1 (first square will put the operator for one - zero byte, eight zero bits, in even) */ - do { - /* apply zeros operator for this bit of len2 */ - gf2_matrix_square(even, odd); - if (len2 & 1) - crc1 = gf2_matrix_times(even, crc1); - len2 >>= 1; - - /* if no more bits set, then done */ - if (len2 == 0) - break; - - /* another iteration of the loop with odd and even swapped */ - gf2_matrix_square(odd, even); - if (len2 & 1) - crc1 = gf2_matrix_times(odd, crc1); - len2 >>= 1; - - /* if no more bits set, then done */ - } while (len2 != 0); - - /* return combined crc */ - crc1 ^= crc2; - return crc1; +/* ========================================================================= */ +uLong ZEXPORT crc32_combine_gen64(z_off64_t len2) { +#ifdef DYNAMIC_CRC_TABLE + once(&made, make_crc_table); +#endif /* DYNAMIC_CRC_TABLE */ + return x2nmodp(len2, 3); } /* ========================================================================= */ -uLong ZEXPORT crc32_combine(crc1, crc2, len2) - uLong crc1; - uLong crc2; - z_off_t len2; -{ - return crc32_combine_(crc1, crc2, len2); +uLong ZEXPORT crc32_combine_gen(z_off_t len2) { + return crc32_combine_gen64((z_off64_t)len2); } -uLong ZEXPORT crc32_combine64(crc1, crc2, len2) - uLong crc1; - uLong crc2; - z_off64_t len2; -{ - return crc32_combine_(crc1, crc2, len2); +/* ========================================================================= */ +uLong ZEXPORT crc32_combine_op(uLong crc1, uLong crc2, uLong op) { + return multmodp(op, crc1) ^ (crc2 & 0xffffffff); } diff --git a/deps/zlib/crc32.h b/deps/zlib/crc32.h index 8053b6117c0..137df68d616 100644 --- a/deps/zlib/crc32.h +++ b/deps/zlib/crc32.h @@ -2,440 +2,9445 @@ * Generated automatically by crc32.c */ -local const unsigned long FAR crc_table[TBLS][256] = -{ - { - 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL, - 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL, - 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL, - 0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL, - 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL, - 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL, - 0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL, - 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL, - 0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL, - 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL, - 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL, - 0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL, - 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL, - 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL, - 0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL, - 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL, - 0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL, - 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL, - 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL, - 0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL, - 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL, - 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL, - 0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL, - 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL, - 0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL, - 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL, - 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL, - 0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL, - 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL, - 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL, - 0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL, - 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL, - 0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL, - 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL, - 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL, - 0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL, - 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL, - 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL, - 0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL, - 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL, - 0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL, - 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL, - 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL, - 0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL, - 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL, - 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL, - 0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL, - 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL, - 0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL, - 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, - 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, - 0x2d02ef8dUL -#ifdef BYFOUR - }, - { - 0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL, - 0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL, - 0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL, - 0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL, - 0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL, - 0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL, - 0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL, - 0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL, - 0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL, - 0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL, - 0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL, - 0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL, - 0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL, - 0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL, - 0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL, - 0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL, - 0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL, - 0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL, - 0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL, - 0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL, - 0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL, - 0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL, - 0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL, - 0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL, - 0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL, - 0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL, - 0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL, - 0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL, - 0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL, - 0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL, - 0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL, - 0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL, - 0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL, - 0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL, - 0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL, - 0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL, - 0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL, - 0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL, - 0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL, - 0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL, - 0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL, - 0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL, - 0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL, - 0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL, - 0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL, - 0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL, - 0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL, - 0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL, - 0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL, - 0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL, - 0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL, - 0x9324fd72UL - }, - { - 0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL, - 0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL, - 0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL, - 0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL, - 0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL, - 0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL, - 0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL, - 0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL, - 0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL, - 0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL, - 0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL, - 0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL, - 0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL, - 0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL, - 0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL, - 0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL, - 0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL, - 0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL, - 0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL, - 0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL, - 0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL, - 0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL, - 0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL, - 0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL, - 0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL, - 0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL, - 0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL, - 0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL, - 0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL, - 0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL, - 0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL, - 0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL, - 0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL, - 0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL, - 0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL, - 0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL, - 0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL, - 0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL, - 0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL, - 0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL, - 0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL, - 0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL, - 0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL, - 0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL, - 0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL, - 0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL, - 0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL, - 0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL, - 0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL, - 0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL, - 0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL, - 0xbe9834edUL - }, - { - 0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL, - 0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL, - 0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL, - 0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL, - 0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL, - 0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL, - 0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL, - 0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL, - 0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL, - 0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL, - 0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL, - 0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL, - 0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL, - 0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL, - 0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL, - 0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL, - 0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL, - 0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL, - 0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL, - 0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL, - 0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL, - 0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL, - 0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL, - 0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL, - 0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL, - 0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL, - 0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL, - 0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL, - 0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL, - 0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL, - 0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL, - 0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL, - 0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL, - 0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL, - 0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL, - 0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL, - 0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL, - 0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL, - 0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL, - 0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL, - 0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL, - 0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL, - 0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL, - 0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL, - 0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL, - 0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL, - 0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL, - 0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL, - 0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL, - 0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL, - 0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL, - 0xde0506f1UL - }, - { - 0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL, - 0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL, - 0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL, - 0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL, - 0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL, - 0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL, - 0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL, - 0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL, - 0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL, - 0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL, - 0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL, - 0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL, - 0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL, - 0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL, - 0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL, - 0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL, - 0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL, - 0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL, - 0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL, - 0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL, - 0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL, - 0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL, - 0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL, - 0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL, - 0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL, - 0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL, - 0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL, - 0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL, - 0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL, - 0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL, - 0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL, - 0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL, - 0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL, - 0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL, - 0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL, - 0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL, - 0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL, - 0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL, - 0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL, - 0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL, - 0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL, - 0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL, - 0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL, - 0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL, - 0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL, - 0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL, - 0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL, - 0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL, - 0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL, - 0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL, - 0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL, - 0x8def022dUL - }, - { - 0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL, - 0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL, - 0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL, - 0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL, - 0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL, - 0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL, - 0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL, - 0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL, - 0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL, - 0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL, - 0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL, - 0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL, - 0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL, - 0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL, - 0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL, - 0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL, - 0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL, - 0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL, - 0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL, - 0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL, - 0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL, - 0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL, - 0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL, - 0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL, - 0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL, - 0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL, - 0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL, - 0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL, - 0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL, - 0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL, - 0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL, - 0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL, - 0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL, - 0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL, - 0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL, - 0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL, - 0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL, - 0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL, - 0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL, - 0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL, - 0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL, - 0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL, - 0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL, - 0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL, - 0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL, - 0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL, - 0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL, - 0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL, - 0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL, - 0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL, - 0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL, - 0x72fd2493UL - }, - { - 0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL, - 0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL, - 0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL, - 0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL, - 0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL, - 0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL, - 0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL, - 0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL, - 0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL, - 0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL, - 0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL, - 0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL, - 0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL, - 0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL, - 0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL, - 0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL, - 0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL, - 0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL, - 0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL, - 0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL, - 0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL, - 0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL, - 0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL, - 0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL, - 0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL, - 0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL, - 0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL, - 0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL, - 0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL, - 0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL, - 0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL, - 0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL, - 0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL, - 0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL, - 0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL, - 0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL, - 0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL, - 0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL, - 0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL, - 0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL, - 0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL, - 0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL, - 0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL, - 0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL, - 0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL, - 0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL, - 0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL, - 0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL, - 0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL, - 0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL, - 0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL, - 0xed3498beUL - }, - { - 0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL, - 0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL, - 0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL, - 0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL, - 0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL, - 0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL, - 0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL, - 0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL, - 0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL, - 0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL, - 0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL, - 0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL, - 0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL, - 0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL, - 0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL, - 0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL, - 0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL, - 0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL, - 0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL, - 0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL, - 0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL, - 0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL, - 0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL, - 0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL, - 0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL, - 0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL, - 0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL, - 0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL, - 0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL, - 0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL, - 0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL, - 0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL, - 0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL, - 0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL, - 0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL, - 0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL, - 0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL, - 0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL, - 0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL, - 0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL, - 0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL, - 0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL, - 0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL, - 0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL, - 0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL, - 0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL, - 0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL, - 0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL, - 0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL, - 0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL, - 0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL, - 0xf10605deUL +local const z_crc_t FAR crc_table[] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, + 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, + 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, + 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, + 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, + 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, + 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, + 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, + 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, + 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, + 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, + 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, + 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, + 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, + 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, + 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, + 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, + 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, + 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, + 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, + 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, + 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, + 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, + 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, + 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, + 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, + 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, + 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, + 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, + 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, + 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, + 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, + 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, + 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, + 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, + 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, + 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, + 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, + 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, + 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, + 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, + 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, + 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, + 0x2d02ef8d}; + +#ifdef W + +#if W == 8 + +local const z_word_t FAR crc_big_table[] = { + 0x0000000000000000, 0x9630077700000000, 0x2c610eee00000000, + 0xba51099900000000, 0x19c46d0700000000, 0x8ff46a7000000000, + 0x35a563e900000000, 0xa395649e00000000, 0x3288db0e00000000, + 0xa4b8dc7900000000, 0x1ee9d5e000000000, 0x88d9d29700000000, + 0x2b4cb60900000000, 0xbd7cb17e00000000, 0x072db8e700000000, + 0x911dbf9000000000, 0x6410b71d00000000, 0xf220b06a00000000, + 0x4871b9f300000000, 0xde41be8400000000, 0x7dd4da1a00000000, + 0xebe4dd6d00000000, 0x51b5d4f400000000, 0xc785d38300000000, + 0x56986c1300000000, 0xc0a86b6400000000, 0x7af962fd00000000, + 0xecc9658a00000000, 0x4f5c011400000000, 0xd96c066300000000, + 0x633d0ffa00000000, 0xf50d088d00000000, 0xc8206e3b00000000, + 0x5e10694c00000000, 0xe44160d500000000, 0x727167a200000000, + 0xd1e4033c00000000, 0x47d4044b00000000, 0xfd850dd200000000, + 0x6bb50aa500000000, 0xfaa8b53500000000, 0x6c98b24200000000, + 0xd6c9bbdb00000000, 0x40f9bcac00000000, 0xe36cd83200000000, + 0x755cdf4500000000, 0xcf0dd6dc00000000, 0x593dd1ab00000000, + 0xac30d92600000000, 0x3a00de5100000000, 0x8051d7c800000000, + 0x1661d0bf00000000, 0xb5f4b42100000000, 0x23c4b35600000000, + 0x9995bacf00000000, 0x0fa5bdb800000000, 0x9eb8022800000000, + 0x0888055f00000000, 0xb2d90cc600000000, 0x24e90bb100000000, + 0x877c6f2f00000000, 0x114c685800000000, 0xab1d61c100000000, + 0x3d2d66b600000000, 0x9041dc7600000000, 0x0671db0100000000, + 0xbc20d29800000000, 0x2a10d5ef00000000, 0x8985b17100000000, + 0x1fb5b60600000000, 0xa5e4bf9f00000000, 0x33d4b8e800000000, + 0xa2c9077800000000, 0x34f9000f00000000, 0x8ea8099600000000, + 0x18980ee100000000, 0xbb0d6a7f00000000, 0x2d3d6d0800000000, + 0x976c649100000000, 0x015c63e600000000, 0xf4516b6b00000000, + 0x62616c1c00000000, 0xd830658500000000, 0x4e0062f200000000, + 0xed95066c00000000, 0x7ba5011b00000000, 0xc1f4088200000000, + 0x57c40ff500000000, 0xc6d9b06500000000, 0x50e9b71200000000, + 0xeab8be8b00000000, 0x7c88b9fc00000000, 0xdf1ddd6200000000, + 0x492dda1500000000, 0xf37cd38c00000000, 0x654cd4fb00000000, + 0x5861b24d00000000, 0xce51b53a00000000, 0x7400bca300000000, + 0xe230bbd400000000, 0x41a5df4a00000000, 0xd795d83d00000000, + 0x6dc4d1a400000000, 0xfbf4d6d300000000, 0x6ae9694300000000, + 0xfcd96e3400000000, 0x468867ad00000000, 0xd0b860da00000000, + 0x732d044400000000, 0xe51d033300000000, 0x5f4c0aaa00000000, + 0xc97c0ddd00000000, 0x3c71055000000000, 0xaa41022700000000, + 0x10100bbe00000000, 0x86200cc900000000, 0x25b5685700000000, + 0xb3856f2000000000, 0x09d466b900000000, 0x9fe461ce00000000, + 0x0ef9de5e00000000, 0x98c9d92900000000, 0x2298d0b000000000, + 0xb4a8d7c700000000, 0x173db35900000000, 0x810db42e00000000, + 0x3b5cbdb700000000, 0xad6cbac000000000, 0x2083b8ed00000000, + 0xb6b3bf9a00000000, 0x0ce2b60300000000, 0x9ad2b17400000000, + 0x3947d5ea00000000, 0xaf77d29d00000000, 0x1526db0400000000, + 0x8316dc7300000000, 0x120b63e300000000, 0x843b649400000000, + 0x3e6a6d0d00000000, 0xa85a6a7a00000000, 0x0bcf0ee400000000, + 0x9dff099300000000, 0x27ae000a00000000, 0xb19e077d00000000, + 0x44930ff000000000, 0xd2a3088700000000, 0x68f2011e00000000, + 0xfec2066900000000, 0x5d5762f700000000, 0xcb67658000000000, + 0x71366c1900000000, 0xe7066b6e00000000, 0x761bd4fe00000000, + 0xe02bd38900000000, 0x5a7ada1000000000, 0xcc4add6700000000, + 0x6fdfb9f900000000, 0xf9efbe8e00000000, 0x43beb71700000000, + 0xd58eb06000000000, 0xe8a3d6d600000000, 0x7e93d1a100000000, + 0xc4c2d83800000000, 0x52f2df4f00000000, 0xf167bbd100000000, + 0x6757bca600000000, 0xdd06b53f00000000, 0x4b36b24800000000, + 0xda2b0dd800000000, 0x4c1b0aaf00000000, 0xf64a033600000000, + 0x607a044100000000, 0xc3ef60df00000000, 0x55df67a800000000, + 0xef8e6e3100000000, 0x79be694600000000, 0x8cb361cb00000000, + 0x1a8366bc00000000, 0xa0d26f2500000000, 0x36e2685200000000, + 0x95770ccc00000000, 0x03470bbb00000000, 0xb916022200000000, + 0x2f26055500000000, 0xbe3bbac500000000, 0x280bbdb200000000, + 0x925ab42b00000000, 0x046ab35c00000000, 0xa7ffd7c200000000, + 0x31cfd0b500000000, 0x8b9ed92c00000000, 0x1daede5b00000000, + 0xb0c2649b00000000, 0x26f263ec00000000, 0x9ca36a7500000000, + 0x0a936d0200000000, 0xa906099c00000000, 0x3f360eeb00000000, + 0x8567077200000000, 0x1357000500000000, 0x824abf9500000000, + 0x147ab8e200000000, 0xae2bb17b00000000, 0x381bb60c00000000, + 0x9b8ed29200000000, 0x0dbed5e500000000, 0xb7efdc7c00000000, + 0x21dfdb0b00000000, 0xd4d2d38600000000, 0x42e2d4f100000000, + 0xf8b3dd6800000000, 0x6e83da1f00000000, 0xcd16be8100000000, + 0x5b26b9f600000000, 0xe177b06f00000000, 0x7747b71800000000, + 0xe65a088800000000, 0x706a0fff00000000, 0xca3b066600000000, + 0x5c0b011100000000, 0xff9e658f00000000, 0x69ae62f800000000, + 0xd3ff6b6100000000, 0x45cf6c1600000000, 0x78e20aa000000000, + 0xeed20dd700000000, 0x5483044e00000000, 0xc2b3033900000000, + 0x612667a700000000, 0xf71660d000000000, 0x4d47694900000000, + 0xdb776e3e00000000, 0x4a6ad1ae00000000, 0xdc5ad6d900000000, + 0x660bdf4000000000, 0xf03bd83700000000, 0x53aebca900000000, + 0xc59ebbde00000000, 0x7fcfb24700000000, 0xe9ffb53000000000, + 0x1cf2bdbd00000000, 0x8ac2baca00000000, 0x3093b35300000000, + 0xa6a3b42400000000, 0x0536d0ba00000000, 0x9306d7cd00000000, + 0x2957de5400000000, 0xbf67d92300000000, 0x2e7a66b300000000, + 0xb84a61c400000000, 0x021b685d00000000, 0x942b6f2a00000000, + 0x37be0bb400000000, 0xa18e0cc300000000, 0x1bdf055a00000000, + 0x8def022d00000000}; + +#else /* W == 4 */ + +local const z_word_t FAR crc_big_table[] = { + 0x00000000, 0x96300777, 0x2c610eee, 0xba510999, 0x19c46d07, + 0x8ff46a70, 0x35a563e9, 0xa395649e, 0x3288db0e, 0xa4b8dc79, + 0x1ee9d5e0, 0x88d9d297, 0x2b4cb609, 0xbd7cb17e, 0x072db8e7, + 0x911dbf90, 0x6410b71d, 0xf220b06a, 0x4871b9f3, 0xde41be84, + 0x7dd4da1a, 0xebe4dd6d, 0x51b5d4f4, 0xc785d383, 0x56986c13, + 0xc0a86b64, 0x7af962fd, 0xecc9658a, 0x4f5c0114, 0xd96c0663, + 0x633d0ffa, 0xf50d088d, 0xc8206e3b, 0x5e10694c, 0xe44160d5, + 0x727167a2, 0xd1e4033c, 0x47d4044b, 0xfd850dd2, 0x6bb50aa5, + 0xfaa8b535, 0x6c98b242, 0xd6c9bbdb, 0x40f9bcac, 0xe36cd832, + 0x755cdf45, 0xcf0dd6dc, 0x593dd1ab, 0xac30d926, 0x3a00de51, + 0x8051d7c8, 0x1661d0bf, 0xb5f4b421, 0x23c4b356, 0x9995bacf, + 0x0fa5bdb8, 0x9eb80228, 0x0888055f, 0xb2d90cc6, 0x24e90bb1, + 0x877c6f2f, 0x114c6858, 0xab1d61c1, 0x3d2d66b6, 0x9041dc76, + 0x0671db01, 0xbc20d298, 0x2a10d5ef, 0x8985b171, 0x1fb5b606, + 0xa5e4bf9f, 0x33d4b8e8, 0xa2c90778, 0x34f9000f, 0x8ea80996, + 0x18980ee1, 0xbb0d6a7f, 0x2d3d6d08, 0x976c6491, 0x015c63e6, + 0xf4516b6b, 0x62616c1c, 0xd8306585, 0x4e0062f2, 0xed95066c, + 0x7ba5011b, 0xc1f40882, 0x57c40ff5, 0xc6d9b065, 0x50e9b712, + 0xeab8be8b, 0x7c88b9fc, 0xdf1ddd62, 0x492dda15, 0xf37cd38c, + 0x654cd4fb, 0x5861b24d, 0xce51b53a, 0x7400bca3, 0xe230bbd4, + 0x41a5df4a, 0xd795d83d, 0x6dc4d1a4, 0xfbf4d6d3, 0x6ae96943, + 0xfcd96e34, 0x468867ad, 0xd0b860da, 0x732d0444, 0xe51d0333, + 0x5f4c0aaa, 0xc97c0ddd, 0x3c710550, 0xaa410227, 0x10100bbe, + 0x86200cc9, 0x25b56857, 0xb3856f20, 0x09d466b9, 0x9fe461ce, + 0x0ef9de5e, 0x98c9d929, 0x2298d0b0, 0xb4a8d7c7, 0x173db359, + 0x810db42e, 0x3b5cbdb7, 0xad6cbac0, 0x2083b8ed, 0xb6b3bf9a, + 0x0ce2b603, 0x9ad2b174, 0x3947d5ea, 0xaf77d29d, 0x1526db04, + 0x8316dc73, 0x120b63e3, 0x843b6494, 0x3e6a6d0d, 0xa85a6a7a, + 0x0bcf0ee4, 0x9dff0993, 0x27ae000a, 0xb19e077d, 0x44930ff0, + 0xd2a30887, 0x68f2011e, 0xfec20669, 0x5d5762f7, 0xcb676580, + 0x71366c19, 0xe7066b6e, 0x761bd4fe, 0xe02bd389, 0x5a7ada10, + 0xcc4add67, 0x6fdfb9f9, 0xf9efbe8e, 0x43beb717, 0xd58eb060, + 0xe8a3d6d6, 0x7e93d1a1, 0xc4c2d838, 0x52f2df4f, 0xf167bbd1, + 0x6757bca6, 0xdd06b53f, 0x4b36b248, 0xda2b0dd8, 0x4c1b0aaf, + 0xf64a0336, 0x607a0441, 0xc3ef60df, 0x55df67a8, 0xef8e6e31, + 0x79be6946, 0x8cb361cb, 0x1a8366bc, 0xa0d26f25, 0x36e26852, + 0x95770ccc, 0x03470bbb, 0xb9160222, 0x2f260555, 0xbe3bbac5, + 0x280bbdb2, 0x925ab42b, 0x046ab35c, 0xa7ffd7c2, 0x31cfd0b5, + 0x8b9ed92c, 0x1daede5b, 0xb0c2649b, 0x26f263ec, 0x9ca36a75, + 0x0a936d02, 0xa906099c, 0x3f360eeb, 0x85670772, 0x13570005, + 0x824abf95, 0x147ab8e2, 0xae2bb17b, 0x381bb60c, 0x9b8ed292, + 0x0dbed5e5, 0xb7efdc7c, 0x21dfdb0b, 0xd4d2d386, 0x42e2d4f1, + 0xf8b3dd68, 0x6e83da1f, 0xcd16be81, 0x5b26b9f6, 0xe177b06f, + 0x7747b718, 0xe65a0888, 0x706a0fff, 0xca3b0666, 0x5c0b0111, + 0xff9e658f, 0x69ae62f8, 0xd3ff6b61, 0x45cf6c16, 0x78e20aa0, + 0xeed20dd7, 0x5483044e, 0xc2b30339, 0x612667a7, 0xf71660d0, + 0x4d476949, 0xdb776e3e, 0x4a6ad1ae, 0xdc5ad6d9, 0x660bdf40, + 0xf03bd837, 0x53aebca9, 0xc59ebbde, 0x7fcfb247, 0xe9ffb530, + 0x1cf2bdbd, 0x8ac2baca, 0x3093b353, 0xa6a3b424, 0x0536d0ba, + 0x9306d7cd, 0x2957de54, 0xbf67d923, 0x2e7a66b3, 0xb84a61c4, + 0x021b685d, 0x942b6f2a, 0x37be0bb4, 0xa18e0cc3, 0x1bdf055a, + 0x8def022d}; + +#endif + +#if N == 1 + +#if W == 8 + +local const z_crc_t FAR crc_braid_table[][256] = { + {0x00000000, 0xccaa009e, 0x4225077d, 0x8e8f07e3, 0x844a0efa, + 0x48e00e64, 0xc66f0987, 0x0ac50919, 0xd3e51bb5, 0x1f4f1b2b, + 0x91c01cc8, 0x5d6a1c56, 0x57af154f, 0x9b0515d1, 0x158a1232, + 0xd92012ac, 0x7cbb312b, 0xb01131b5, 0x3e9e3656, 0xf23436c8, + 0xf8f13fd1, 0x345b3f4f, 0xbad438ac, 0x767e3832, 0xaf5e2a9e, + 0x63f42a00, 0xed7b2de3, 0x21d12d7d, 0x2b142464, 0xe7be24fa, + 0x69312319, 0xa59b2387, 0xf9766256, 0x35dc62c8, 0xbb53652b, + 0x77f965b5, 0x7d3c6cac, 0xb1966c32, 0x3f196bd1, 0xf3b36b4f, + 0x2a9379e3, 0xe639797d, 0x68b67e9e, 0xa41c7e00, 0xaed97719, + 0x62737787, 0xecfc7064, 0x205670fa, 0x85cd537d, 0x496753e3, + 0xc7e85400, 0x0b42549e, 0x01875d87, 0xcd2d5d19, 0x43a25afa, + 0x8f085a64, 0x562848c8, 0x9a824856, 0x140d4fb5, 0xd8a74f2b, + 0xd2624632, 0x1ec846ac, 0x9047414f, 0x5ced41d1, 0x299dc2ed, + 0xe537c273, 0x6bb8c590, 0xa712c50e, 0xadd7cc17, 0x617dcc89, + 0xeff2cb6a, 0x2358cbf4, 0xfa78d958, 0x36d2d9c6, 0xb85dde25, + 0x74f7debb, 0x7e32d7a2, 0xb298d73c, 0x3c17d0df, 0xf0bdd041, + 0x5526f3c6, 0x998cf358, 0x1703f4bb, 0xdba9f425, 0xd16cfd3c, + 0x1dc6fda2, 0x9349fa41, 0x5fe3fadf, 0x86c3e873, 0x4a69e8ed, + 0xc4e6ef0e, 0x084cef90, 0x0289e689, 0xce23e617, 0x40ace1f4, + 0x8c06e16a, 0xd0eba0bb, 0x1c41a025, 0x92cea7c6, 0x5e64a758, + 0x54a1ae41, 0x980baedf, 0x1684a93c, 0xda2ea9a2, 0x030ebb0e, + 0xcfa4bb90, 0x412bbc73, 0x8d81bced, 0x8744b5f4, 0x4beeb56a, + 0xc561b289, 0x09cbb217, 0xac509190, 0x60fa910e, 0xee7596ed, + 0x22df9673, 0x281a9f6a, 0xe4b09ff4, 0x6a3f9817, 0xa6959889, + 0x7fb58a25, 0xb31f8abb, 0x3d908d58, 0xf13a8dc6, 0xfbff84df, + 0x37558441, 0xb9da83a2, 0x7570833c, 0x533b85da, 0x9f918544, + 0x111e82a7, 0xddb48239, 0xd7718b20, 0x1bdb8bbe, 0x95548c5d, + 0x59fe8cc3, 0x80de9e6f, 0x4c749ef1, 0xc2fb9912, 0x0e51998c, + 0x04949095, 0xc83e900b, 0x46b197e8, 0x8a1b9776, 0x2f80b4f1, + 0xe32ab46f, 0x6da5b38c, 0xa10fb312, 0xabcaba0b, 0x6760ba95, + 0xe9efbd76, 0x2545bde8, 0xfc65af44, 0x30cfafda, 0xbe40a839, + 0x72eaa8a7, 0x782fa1be, 0xb485a120, 0x3a0aa6c3, 0xf6a0a65d, + 0xaa4de78c, 0x66e7e712, 0xe868e0f1, 0x24c2e06f, 0x2e07e976, + 0xe2ade9e8, 0x6c22ee0b, 0xa088ee95, 0x79a8fc39, 0xb502fca7, + 0x3b8dfb44, 0xf727fbda, 0xfde2f2c3, 0x3148f25d, 0xbfc7f5be, + 0x736df520, 0xd6f6d6a7, 0x1a5cd639, 0x94d3d1da, 0x5879d144, + 0x52bcd85d, 0x9e16d8c3, 0x1099df20, 0xdc33dfbe, 0x0513cd12, + 0xc9b9cd8c, 0x4736ca6f, 0x8b9ccaf1, 0x8159c3e8, 0x4df3c376, + 0xc37cc495, 0x0fd6c40b, 0x7aa64737, 0xb60c47a9, 0x3883404a, + 0xf42940d4, 0xfeec49cd, 0x32464953, 0xbcc94eb0, 0x70634e2e, + 0xa9435c82, 0x65e95c1c, 0xeb665bff, 0x27cc5b61, 0x2d095278, + 0xe1a352e6, 0x6f2c5505, 0xa386559b, 0x061d761c, 0xcab77682, + 0x44387161, 0x889271ff, 0x825778e6, 0x4efd7878, 0xc0727f9b, + 0x0cd87f05, 0xd5f86da9, 0x19526d37, 0x97dd6ad4, 0x5b776a4a, + 0x51b26353, 0x9d1863cd, 0x1397642e, 0xdf3d64b0, 0x83d02561, + 0x4f7a25ff, 0xc1f5221c, 0x0d5f2282, 0x079a2b9b, 0xcb302b05, + 0x45bf2ce6, 0x89152c78, 0x50353ed4, 0x9c9f3e4a, 0x121039a9, + 0xdeba3937, 0xd47f302e, 0x18d530b0, 0x965a3753, 0x5af037cd, + 0xff6b144a, 0x33c114d4, 0xbd4e1337, 0x71e413a9, 0x7b211ab0, + 0xb78b1a2e, 0x39041dcd, 0xf5ae1d53, 0x2c8e0fff, 0xe0240f61, + 0x6eab0882, 0xa201081c, 0xa8c40105, 0x646e019b, 0xeae10678, + 0x264b06e6}, + {0x00000000, 0xa6770bb4, 0x979f1129, 0x31e81a9d, 0xf44f2413, + 0x52382fa7, 0x63d0353a, 0xc5a73e8e, 0x33ef4e67, 0x959845d3, + 0xa4705f4e, 0x020754fa, 0xc7a06a74, 0x61d761c0, 0x503f7b5d, + 0xf64870e9, 0x67de9cce, 0xc1a9977a, 0xf0418de7, 0x56368653, + 0x9391b8dd, 0x35e6b369, 0x040ea9f4, 0xa279a240, 0x5431d2a9, + 0xf246d91d, 0xc3aec380, 0x65d9c834, 0xa07ef6ba, 0x0609fd0e, + 0x37e1e793, 0x9196ec27, 0xcfbd399c, 0x69ca3228, 0x582228b5, + 0xfe552301, 0x3bf21d8f, 0x9d85163b, 0xac6d0ca6, 0x0a1a0712, + 0xfc5277fb, 0x5a257c4f, 0x6bcd66d2, 0xcdba6d66, 0x081d53e8, + 0xae6a585c, 0x9f8242c1, 0x39f54975, 0xa863a552, 0x0e14aee6, + 0x3ffcb47b, 0x998bbfcf, 0x5c2c8141, 0xfa5b8af5, 0xcbb39068, + 0x6dc49bdc, 0x9b8ceb35, 0x3dfbe081, 0x0c13fa1c, 0xaa64f1a8, + 0x6fc3cf26, 0xc9b4c492, 0xf85cde0f, 0x5e2bd5bb, 0x440b7579, + 0xe27c7ecd, 0xd3946450, 0x75e36fe4, 0xb044516a, 0x16335ade, + 0x27db4043, 0x81ac4bf7, 0x77e43b1e, 0xd19330aa, 0xe07b2a37, + 0x460c2183, 0x83ab1f0d, 0x25dc14b9, 0x14340e24, 0xb2430590, + 0x23d5e9b7, 0x85a2e203, 0xb44af89e, 0x123df32a, 0xd79acda4, + 0x71edc610, 0x4005dc8d, 0xe672d739, 0x103aa7d0, 0xb64dac64, + 0x87a5b6f9, 0x21d2bd4d, 0xe47583c3, 0x42028877, 0x73ea92ea, + 0xd59d995e, 0x8bb64ce5, 0x2dc14751, 0x1c295dcc, 0xba5e5678, + 0x7ff968f6, 0xd98e6342, 0xe86679df, 0x4e11726b, 0xb8590282, + 0x1e2e0936, 0x2fc613ab, 0x89b1181f, 0x4c162691, 0xea612d25, + 0xdb8937b8, 0x7dfe3c0c, 0xec68d02b, 0x4a1fdb9f, 0x7bf7c102, + 0xdd80cab6, 0x1827f438, 0xbe50ff8c, 0x8fb8e511, 0x29cfeea5, + 0xdf879e4c, 0x79f095f8, 0x48188f65, 0xee6f84d1, 0x2bc8ba5f, + 0x8dbfb1eb, 0xbc57ab76, 0x1a20a0c2, 0x8816eaf2, 0x2e61e146, + 0x1f89fbdb, 0xb9fef06f, 0x7c59cee1, 0xda2ec555, 0xebc6dfc8, + 0x4db1d47c, 0xbbf9a495, 0x1d8eaf21, 0x2c66b5bc, 0x8a11be08, + 0x4fb68086, 0xe9c18b32, 0xd82991af, 0x7e5e9a1b, 0xefc8763c, + 0x49bf7d88, 0x78576715, 0xde206ca1, 0x1b87522f, 0xbdf0599b, + 0x8c184306, 0x2a6f48b2, 0xdc27385b, 0x7a5033ef, 0x4bb82972, + 0xedcf22c6, 0x28681c48, 0x8e1f17fc, 0xbff70d61, 0x198006d5, + 0x47abd36e, 0xe1dcd8da, 0xd034c247, 0x7643c9f3, 0xb3e4f77d, + 0x1593fcc9, 0x247be654, 0x820cede0, 0x74449d09, 0xd23396bd, + 0xe3db8c20, 0x45ac8794, 0x800bb91a, 0x267cb2ae, 0x1794a833, + 0xb1e3a387, 0x20754fa0, 0x86024414, 0xb7ea5e89, 0x119d553d, + 0xd43a6bb3, 0x724d6007, 0x43a57a9a, 0xe5d2712e, 0x139a01c7, + 0xb5ed0a73, 0x840510ee, 0x22721b5a, 0xe7d525d4, 0x41a22e60, + 0x704a34fd, 0xd63d3f49, 0xcc1d9f8b, 0x6a6a943f, 0x5b828ea2, + 0xfdf58516, 0x3852bb98, 0x9e25b02c, 0xafcdaab1, 0x09baa105, + 0xfff2d1ec, 0x5985da58, 0x686dc0c5, 0xce1acb71, 0x0bbdf5ff, + 0xadcafe4b, 0x9c22e4d6, 0x3a55ef62, 0xabc30345, 0x0db408f1, + 0x3c5c126c, 0x9a2b19d8, 0x5f8c2756, 0xf9fb2ce2, 0xc813367f, + 0x6e643dcb, 0x982c4d22, 0x3e5b4696, 0x0fb35c0b, 0xa9c457bf, + 0x6c636931, 0xca146285, 0xfbfc7818, 0x5d8b73ac, 0x03a0a617, + 0xa5d7ada3, 0x943fb73e, 0x3248bc8a, 0xf7ef8204, 0x519889b0, + 0x6070932d, 0xc6079899, 0x304fe870, 0x9638e3c4, 0xa7d0f959, + 0x01a7f2ed, 0xc400cc63, 0x6277c7d7, 0x539fdd4a, 0xf5e8d6fe, + 0x647e3ad9, 0xc209316d, 0xf3e12bf0, 0x55962044, 0x90311eca, + 0x3646157e, 0x07ae0fe3, 0xa1d90457, 0x579174be, 0xf1e67f0a, + 0xc00e6597, 0x66796e23, 0xa3de50ad, 0x05a95b19, 0x34414184, + 0x92364a30}, + {0x00000000, 0xcb5cd3a5, 0x4dc8a10b, 0x869472ae, 0x9b914216, + 0x50cd91b3, 0xd659e31d, 0x1d0530b8, 0xec53826d, 0x270f51c8, + 0xa19b2366, 0x6ac7f0c3, 0x77c2c07b, 0xbc9e13de, 0x3a0a6170, + 0xf156b2d5, 0x03d6029b, 0xc88ad13e, 0x4e1ea390, 0x85427035, + 0x9847408d, 0x531b9328, 0xd58fe186, 0x1ed33223, 0xef8580f6, + 0x24d95353, 0xa24d21fd, 0x6911f258, 0x7414c2e0, 0xbf481145, + 0x39dc63eb, 0xf280b04e, 0x07ac0536, 0xccf0d693, 0x4a64a43d, + 0x81387798, 0x9c3d4720, 0x57619485, 0xd1f5e62b, 0x1aa9358e, + 0xebff875b, 0x20a354fe, 0xa6372650, 0x6d6bf5f5, 0x706ec54d, + 0xbb3216e8, 0x3da66446, 0xf6fab7e3, 0x047a07ad, 0xcf26d408, + 0x49b2a6a6, 0x82ee7503, 0x9feb45bb, 0x54b7961e, 0xd223e4b0, + 0x197f3715, 0xe82985c0, 0x23755665, 0xa5e124cb, 0x6ebdf76e, + 0x73b8c7d6, 0xb8e41473, 0x3e7066dd, 0xf52cb578, 0x0f580a6c, + 0xc404d9c9, 0x4290ab67, 0x89cc78c2, 0x94c9487a, 0x5f959bdf, + 0xd901e971, 0x125d3ad4, 0xe30b8801, 0x28575ba4, 0xaec3290a, + 0x659ffaaf, 0x789aca17, 0xb3c619b2, 0x35526b1c, 0xfe0eb8b9, + 0x0c8e08f7, 0xc7d2db52, 0x4146a9fc, 0x8a1a7a59, 0x971f4ae1, + 0x5c439944, 0xdad7ebea, 0x118b384f, 0xe0dd8a9a, 0x2b81593f, + 0xad152b91, 0x6649f834, 0x7b4cc88c, 0xb0101b29, 0x36846987, + 0xfdd8ba22, 0x08f40f5a, 0xc3a8dcff, 0x453cae51, 0x8e607df4, + 0x93654d4c, 0x58399ee9, 0xdeadec47, 0x15f13fe2, 0xe4a78d37, + 0x2ffb5e92, 0xa96f2c3c, 0x6233ff99, 0x7f36cf21, 0xb46a1c84, + 0x32fe6e2a, 0xf9a2bd8f, 0x0b220dc1, 0xc07ede64, 0x46eaacca, + 0x8db67f6f, 0x90b34fd7, 0x5bef9c72, 0xdd7beedc, 0x16273d79, + 0xe7718fac, 0x2c2d5c09, 0xaab92ea7, 0x61e5fd02, 0x7ce0cdba, + 0xb7bc1e1f, 0x31286cb1, 0xfa74bf14, 0x1eb014d8, 0xd5ecc77d, + 0x5378b5d3, 0x98246676, 0x852156ce, 0x4e7d856b, 0xc8e9f7c5, + 0x03b52460, 0xf2e396b5, 0x39bf4510, 0xbf2b37be, 0x7477e41b, + 0x6972d4a3, 0xa22e0706, 0x24ba75a8, 0xefe6a60d, 0x1d661643, + 0xd63ac5e6, 0x50aeb748, 0x9bf264ed, 0x86f75455, 0x4dab87f0, + 0xcb3ff55e, 0x006326fb, 0xf135942e, 0x3a69478b, 0xbcfd3525, + 0x77a1e680, 0x6aa4d638, 0xa1f8059d, 0x276c7733, 0xec30a496, + 0x191c11ee, 0xd240c24b, 0x54d4b0e5, 0x9f886340, 0x828d53f8, + 0x49d1805d, 0xcf45f2f3, 0x04192156, 0xf54f9383, 0x3e134026, + 0xb8873288, 0x73dbe12d, 0x6eded195, 0xa5820230, 0x2316709e, + 0xe84aa33b, 0x1aca1375, 0xd196c0d0, 0x5702b27e, 0x9c5e61db, + 0x815b5163, 0x4a0782c6, 0xcc93f068, 0x07cf23cd, 0xf6999118, + 0x3dc542bd, 0xbb513013, 0x700de3b6, 0x6d08d30e, 0xa65400ab, + 0x20c07205, 0xeb9ca1a0, 0x11e81eb4, 0xdab4cd11, 0x5c20bfbf, + 0x977c6c1a, 0x8a795ca2, 0x41258f07, 0xc7b1fda9, 0x0ced2e0c, + 0xfdbb9cd9, 0x36e74f7c, 0xb0733dd2, 0x7b2fee77, 0x662adecf, + 0xad760d6a, 0x2be27fc4, 0xe0beac61, 0x123e1c2f, 0xd962cf8a, + 0x5ff6bd24, 0x94aa6e81, 0x89af5e39, 0x42f38d9c, 0xc467ff32, + 0x0f3b2c97, 0xfe6d9e42, 0x35314de7, 0xb3a53f49, 0x78f9ecec, + 0x65fcdc54, 0xaea00ff1, 0x28347d5f, 0xe368aefa, 0x16441b82, + 0xdd18c827, 0x5b8cba89, 0x90d0692c, 0x8dd55994, 0x46898a31, + 0xc01df89f, 0x0b412b3a, 0xfa1799ef, 0x314b4a4a, 0xb7df38e4, + 0x7c83eb41, 0x6186dbf9, 0xaada085c, 0x2c4e7af2, 0xe712a957, + 0x15921919, 0xdececabc, 0x585ab812, 0x93066bb7, 0x8e035b0f, + 0x455f88aa, 0xc3cbfa04, 0x089729a1, 0xf9c19b74, 0x329d48d1, + 0xb4093a7f, 0x7f55e9da, 0x6250d962, 0xa90c0ac7, 0x2f987869, + 0xe4c4abcc}, + {0x00000000, 0x3d6029b0, 0x7ac05360, 0x47a07ad0, 0xf580a6c0, + 0xc8e08f70, 0x8f40f5a0, 0xb220dc10, 0x30704bc1, 0x0d106271, + 0x4ab018a1, 0x77d03111, 0xc5f0ed01, 0xf890c4b1, 0xbf30be61, + 0x825097d1, 0x60e09782, 0x5d80be32, 0x1a20c4e2, 0x2740ed52, + 0x95603142, 0xa80018f2, 0xefa06222, 0xd2c04b92, 0x5090dc43, + 0x6df0f5f3, 0x2a508f23, 0x1730a693, 0xa5107a83, 0x98705333, + 0xdfd029e3, 0xe2b00053, 0xc1c12f04, 0xfca106b4, 0xbb017c64, + 0x866155d4, 0x344189c4, 0x0921a074, 0x4e81daa4, 0x73e1f314, + 0xf1b164c5, 0xccd14d75, 0x8b7137a5, 0xb6111e15, 0x0431c205, + 0x3951ebb5, 0x7ef19165, 0x4391b8d5, 0xa121b886, 0x9c419136, + 0xdbe1ebe6, 0xe681c256, 0x54a11e46, 0x69c137f6, 0x2e614d26, + 0x13016496, 0x9151f347, 0xac31daf7, 0xeb91a027, 0xd6f18997, + 0x64d15587, 0x59b17c37, 0x1e1106e7, 0x23712f57, 0x58f35849, + 0x659371f9, 0x22330b29, 0x1f532299, 0xad73fe89, 0x9013d739, + 0xd7b3ade9, 0xead38459, 0x68831388, 0x55e33a38, 0x124340e8, + 0x2f236958, 0x9d03b548, 0xa0639cf8, 0xe7c3e628, 0xdaa3cf98, + 0x3813cfcb, 0x0573e67b, 0x42d39cab, 0x7fb3b51b, 0xcd93690b, + 0xf0f340bb, 0xb7533a6b, 0x8a3313db, 0x0863840a, 0x3503adba, + 0x72a3d76a, 0x4fc3feda, 0xfde322ca, 0xc0830b7a, 0x872371aa, + 0xba43581a, 0x9932774d, 0xa4525efd, 0xe3f2242d, 0xde920d9d, + 0x6cb2d18d, 0x51d2f83d, 0x167282ed, 0x2b12ab5d, 0xa9423c8c, + 0x9422153c, 0xd3826fec, 0xeee2465c, 0x5cc29a4c, 0x61a2b3fc, + 0x2602c92c, 0x1b62e09c, 0xf9d2e0cf, 0xc4b2c97f, 0x8312b3af, + 0xbe729a1f, 0x0c52460f, 0x31326fbf, 0x7692156f, 0x4bf23cdf, + 0xc9a2ab0e, 0xf4c282be, 0xb362f86e, 0x8e02d1de, 0x3c220dce, + 0x0142247e, 0x46e25eae, 0x7b82771e, 0xb1e6b092, 0x8c869922, + 0xcb26e3f2, 0xf646ca42, 0x44661652, 0x79063fe2, 0x3ea64532, + 0x03c66c82, 0x8196fb53, 0xbcf6d2e3, 0xfb56a833, 0xc6368183, + 0x74165d93, 0x49767423, 0x0ed60ef3, 0x33b62743, 0xd1062710, + 0xec660ea0, 0xabc67470, 0x96a65dc0, 0x248681d0, 0x19e6a860, + 0x5e46d2b0, 0x6326fb00, 0xe1766cd1, 0xdc164561, 0x9bb63fb1, + 0xa6d61601, 0x14f6ca11, 0x2996e3a1, 0x6e369971, 0x5356b0c1, + 0x70279f96, 0x4d47b626, 0x0ae7ccf6, 0x3787e546, 0x85a73956, + 0xb8c710e6, 0xff676a36, 0xc2074386, 0x4057d457, 0x7d37fde7, + 0x3a978737, 0x07f7ae87, 0xb5d77297, 0x88b75b27, 0xcf1721f7, + 0xf2770847, 0x10c70814, 0x2da721a4, 0x6a075b74, 0x576772c4, + 0xe547aed4, 0xd8278764, 0x9f87fdb4, 0xa2e7d404, 0x20b743d5, + 0x1dd76a65, 0x5a7710b5, 0x67173905, 0xd537e515, 0xe857cca5, + 0xaff7b675, 0x92979fc5, 0xe915e8db, 0xd475c16b, 0x93d5bbbb, + 0xaeb5920b, 0x1c954e1b, 0x21f567ab, 0x66551d7b, 0x5b3534cb, + 0xd965a31a, 0xe4058aaa, 0xa3a5f07a, 0x9ec5d9ca, 0x2ce505da, + 0x11852c6a, 0x562556ba, 0x6b457f0a, 0x89f57f59, 0xb49556e9, + 0xf3352c39, 0xce550589, 0x7c75d999, 0x4115f029, 0x06b58af9, + 0x3bd5a349, 0xb9853498, 0x84e51d28, 0xc34567f8, 0xfe254e48, + 0x4c059258, 0x7165bbe8, 0x36c5c138, 0x0ba5e888, 0x28d4c7df, + 0x15b4ee6f, 0x521494bf, 0x6f74bd0f, 0xdd54611f, 0xe03448af, + 0xa794327f, 0x9af41bcf, 0x18a48c1e, 0x25c4a5ae, 0x6264df7e, + 0x5f04f6ce, 0xed242ade, 0xd044036e, 0x97e479be, 0xaa84500e, + 0x4834505d, 0x755479ed, 0x32f4033d, 0x0f942a8d, 0xbdb4f69d, + 0x80d4df2d, 0xc774a5fd, 0xfa148c4d, 0x78441b9c, 0x4524322c, + 0x028448fc, 0x3fe4614c, 0x8dc4bd5c, 0xb0a494ec, 0xf704ee3c, + 0xca64c78c}, + {0x00000000, 0xb8bc6765, 0xaa09c88b, 0x12b5afee, 0x8f629757, + 0x37def032, 0x256b5fdc, 0x9dd738b9, 0xc5b428ef, 0x7d084f8a, + 0x6fbde064, 0xd7018701, 0x4ad6bfb8, 0xf26ad8dd, 0xe0df7733, + 0x58631056, 0x5019579f, 0xe8a530fa, 0xfa109f14, 0x42acf871, + 0xdf7bc0c8, 0x67c7a7ad, 0x75720843, 0xcdce6f26, 0x95ad7f70, + 0x2d111815, 0x3fa4b7fb, 0x8718d09e, 0x1acfe827, 0xa2738f42, + 0xb0c620ac, 0x087a47c9, 0xa032af3e, 0x188ec85b, 0x0a3b67b5, + 0xb28700d0, 0x2f503869, 0x97ec5f0c, 0x8559f0e2, 0x3de59787, + 0x658687d1, 0xdd3ae0b4, 0xcf8f4f5a, 0x7733283f, 0xeae41086, + 0x525877e3, 0x40edd80d, 0xf851bf68, 0xf02bf8a1, 0x48979fc4, + 0x5a22302a, 0xe29e574f, 0x7f496ff6, 0xc7f50893, 0xd540a77d, + 0x6dfcc018, 0x359fd04e, 0x8d23b72b, 0x9f9618c5, 0x272a7fa0, + 0xbafd4719, 0x0241207c, 0x10f48f92, 0xa848e8f7, 0x9b14583d, + 0x23a83f58, 0x311d90b6, 0x89a1f7d3, 0x1476cf6a, 0xaccaa80f, + 0xbe7f07e1, 0x06c36084, 0x5ea070d2, 0xe61c17b7, 0xf4a9b859, + 0x4c15df3c, 0xd1c2e785, 0x697e80e0, 0x7bcb2f0e, 0xc377486b, + 0xcb0d0fa2, 0x73b168c7, 0x6104c729, 0xd9b8a04c, 0x446f98f5, + 0xfcd3ff90, 0xee66507e, 0x56da371b, 0x0eb9274d, 0xb6054028, + 0xa4b0efc6, 0x1c0c88a3, 0x81dbb01a, 0x3967d77f, 0x2bd27891, + 0x936e1ff4, 0x3b26f703, 0x839a9066, 0x912f3f88, 0x299358ed, + 0xb4446054, 0x0cf80731, 0x1e4da8df, 0xa6f1cfba, 0xfe92dfec, + 0x462eb889, 0x549b1767, 0xec277002, 0x71f048bb, 0xc94c2fde, + 0xdbf98030, 0x6345e755, 0x6b3fa09c, 0xd383c7f9, 0xc1366817, + 0x798a0f72, 0xe45d37cb, 0x5ce150ae, 0x4e54ff40, 0xf6e89825, + 0xae8b8873, 0x1637ef16, 0x048240f8, 0xbc3e279d, 0x21e91f24, + 0x99557841, 0x8be0d7af, 0x335cb0ca, 0xed59b63b, 0x55e5d15e, + 0x47507eb0, 0xffec19d5, 0x623b216c, 0xda874609, 0xc832e9e7, + 0x708e8e82, 0x28ed9ed4, 0x9051f9b1, 0x82e4565f, 0x3a58313a, + 0xa78f0983, 0x1f336ee6, 0x0d86c108, 0xb53aa66d, 0xbd40e1a4, + 0x05fc86c1, 0x1749292f, 0xaff54e4a, 0x322276f3, 0x8a9e1196, + 0x982bbe78, 0x2097d91d, 0x78f4c94b, 0xc048ae2e, 0xd2fd01c0, + 0x6a4166a5, 0xf7965e1c, 0x4f2a3979, 0x5d9f9697, 0xe523f1f2, + 0x4d6b1905, 0xf5d77e60, 0xe762d18e, 0x5fdeb6eb, 0xc2098e52, + 0x7ab5e937, 0x680046d9, 0xd0bc21bc, 0x88df31ea, 0x3063568f, + 0x22d6f961, 0x9a6a9e04, 0x07bda6bd, 0xbf01c1d8, 0xadb46e36, + 0x15080953, 0x1d724e9a, 0xa5ce29ff, 0xb77b8611, 0x0fc7e174, + 0x9210d9cd, 0x2aacbea8, 0x38191146, 0x80a57623, 0xd8c66675, + 0x607a0110, 0x72cfaefe, 0xca73c99b, 0x57a4f122, 0xef189647, + 0xfdad39a9, 0x45115ecc, 0x764dee06, 0xcef18963, 0xdc44268d, + 0x64f841e8, 0xf92f7951, 0x41931e34, 0x5326b1da, 0xeb9ad6bf, + 0xb3f9c6e9, 0x0b45a18c, 0x19f00e62, 0xa14c6907, 0x3c9b51be, + 0x842736db, 0x96929935, 0x2e2efe50, 0x2654b999, 0x9ee8defc, + 0x8c5d7112, 0x34e11677, 0xa9362ece, 0x118a49ab, 0x033fe645, + 0xbb838120, 0xe3e09176, 0x5b5cf613, 0x49e959fd, 0xf1553e98, + 0x6c820621, 0xd43e6144, 0xc68bceaa, 0x7e37a9cf, 0xd67f4138, + 0x6ec3265d, 0x7c7689b3, 0xc4caeed6, 0x591dd66f, 0xe1a1b10a, + 0xf3141ee4, 0x4ba87981, 0x13cb69d7, 0xab770eb2, 0xb9c2a15c, + 0x017ec639, 0x9ca9fe80, 0x241599e5, 0x36a0360b, 0x8e1c516e, + 0x866616a7, 0x3eda71c2, 0x2c6fde2c, 0x94d3b949, 0x090481f0, + 0xb1b8e695, 0xa30d497b, 0x1bb12e1e, 0x43d23e48, 0xfb6e592d, + 0xe9dbf6c3, 0x516791a6, 0xccb0a91f, 0x740cce7a, 0x66b96194, + 0xde0506f1}, + {0x00000000, 0x01c26a37, 0x0384d46e, 0x0246be59, 0x0709a8dc, + 0x06cbc2eb, 0x048d7cb2, 0x054f1685, 0x0e1351b8, 0x0fd13b8f, + 0x0d9785d6, 0x0c55efe1, 0x091af964, 0x08d89353, 0x0a9e2d0a, + 0x0b5c473d, 0x1c26a370, 0x1de4c947, 0x1fa2771e, 0x1e601d29, + 0x1b2f0bac, 0x1aed619b, 0x18abdfc2, 0x1969b5f5, 0x1235f2c8, + 0x13f798ff, 0x11b126a6, 0x10734c91, 0x153c5a14, 0x14fe3023, + 0x16b88e7a, 0x177ae44d, 0x384d46e0, 0x398f2cd7, 0x3bc9928e, + 0x3a0bf8b9, 0x3f44ee3c, 0x3e86840b, 0x3cc03a52, 0x3d025065, + 0x365e1758, 0x379c7d6f, 0x35dac336, 0x3418a901, 0x3157bf84, + 0x3095d5b3, 0x32d36bea, 0x331101dd, 0x246be590, 0x25a98fa7, + 0x27ef31fe, 0x262d5bc9, 0x23624d4c, 0x22a0277b, 0x20e69922, + 0x2124f315, 0x2a78b428, 0x2bbade1f, 0x29fc6046, 0x283e0a71, + 0x2d711cf4, 0x2cb376c3, 0x2ef5c89a, 0x2f37a2ad, 0x709a8dc0, + 0x7158e7f7, 0x731e59ae, 0x72dc3399, 0x7793251c, 0x76514f2b, + 0x7417f172, 0x75d59b45, 0x7e89dc78, 0x7f4bb64f, 0x7d0d0816, + 0x7ccf6221, 0x798074a4, 0x78421e93, 0x7a04a0ca, 0x7bc6cafd, + 0x6cbc2eb0, 0x6d7e4487, 0x6f38fade, 0x6efa90e9, 0x6bb5866c, + 0x6a77ec5b, 0x68315202, 0x69f33835, 0x62af7f08, 0x636d153f, + 0x612bab66, 0x60e9c151, 0x65a6d7d4, 0x6464bde3, 0x662203ba, + 0x67e0698d, 0x48d7cb20, 0x4915a117, 0x4b531f4e, 0x4a917579, + 0x4fde63fc, 0x4e1c09cb, 0x4c5ab792, 0x4d98dda5, 0x46c49a98, + 0x4706f0af, 0x45404ef6, 0x448224c1, 0x41cd3244, 0x400f5873, + 0x4249e62a, 0x438b8c1d, 0x54f16850, 0x55330267, 0x5775bc3e, + 0x56b7d609, 0x53f8c08c, 0x523aaabb, 0x507c14e2, 0x51be7ed5, + 0x5ae239e8, 0x5b2053df, 0x5966ed86, 0x58a487b1, 0x5deb9134, + 0x5c29fb03, 0x5e6f455a, 0x5fad2f6d, 0xe1351b80, 0xe0f771b7, + 0xe2b1cfee, 0xe373a5d9, 0xe63cb35c, 0xe7fed96b, 0xe5b86732, + 0xe47a0d05, 0xef264a38, 0xeee4200f, 0xeca29e56, 0xed60f461, + 0xe82fe2e4, 0xe9ed88d3, 0xebab368a, 0xea695cbd, 0xfd13b8f0, + 0xfcd1d2c7, 0xfe976c9e, 0xff5506a9, 0xfa1a102c, 0xfbd87a1b, + 0xf99ec442, 0xf85cae75, 0xf300e948, 0xf2c2837f, 0xf0843d26, + 0xf1465711, 0xf4094194, 0xf5cb2ba3, 0xf78d95fa, 0xf64fffcd, + 0xd9785d60, 0xd8ba3757, 0xdafc890e, 0xdb3ee339, 0xde71f5bc, + 0xdfb39f8b, 0xddf521d2, 0xdc374be5, 0xd76b0cd8, 0xd6a966ef, + 0xd4efd8b6, 0xd52db281, 0xd062a404, 0xd1a0ce33, 0xd3e6706a, + 0xd2241a5d, 0xc55efe10, 0xc49c9427, 0xc6da2a7e, 0xc7184049, + 0xc25756cc, 0xc3953cfb, 0xc1d382a2, 0xc011e895, 0xcb4dafa8, + 0xca8fc59f, 0xc8c97bc6, 0xc90b11f1, 0xcc440774, 0xcd866d43, + 0xcfc0d31a, 0xce02b92d, 0x91af9640, 0x906dfc77, 0x922b422e, + 0x93e92819, 0x96a63e9c, 0x976454ab, 0x9522eaf2, 0x94e080c5, + 0x9fbcc7f8, 0x9e7eadcf, 0x9c381396, 0x9dfa79a1, 0x98b56f24, + 0x99770513, 0x9b31bb4a, 0x9af3d17d, 0x8d893530, 0x8c4b5f07, + 0x8e0de15e, 0x8fcf8b69, 0x8a809dec, 0x8b42f7db, 0x89044982, + 0x88c623b5, 0x839a6488, 0x82580ebf, 0x801eb0e6, 0x81dcdad1, + 0x8493cc54, 0x8551a663, 0x8717183a, 0x86d5720d, 0xa9e2d0a0, + 0xa820ba97, 0xaa6604ce, 0xaba46ef9, 0xaeeb787c, 0xaf29124b, + 0xad6fac12, 0xacadc625, 0xa7f18118, 0xa633eb2f, 0xa4755576, + 0xa5b73f41, 0xa0f829c4, 0xa13a43f3, 0xa37cfdaa, 0xa2be979d, + 0xb5c473d0, 0xb40619e7, 0xb640a7be, 0xb782cd89, 0xb2cddb0c, + 0xb30fb13b, 0xb1490f62, 0xb08b6555, 0xbbd72268, 0xba15485f, + 0xb853f606, 0xb9919c31, 0xbcde8ab4, 0xbd1ce083, 0xbf5a5eda, + 0xbe9834ed}, + {0x00000000, 0x191b3141, 0x32366282, 0x2b2d53c3, 0x646cc504, + 0x7d77f445, 0x565aa786, 0x4f4196c7, 0xc8d98a08, 0xd1c2bb49, + 0xfaefe88a, 0xe3f4d9cb, 0xacb54f0c, 0xb5ae7e4d, 0x9e832d8e, + 0x87981ccf, 0x4ac21251, 0x53d92310, 0x78f470d3, 0x61ef4192, + 0x2eaed755, 0x37b5e614, 0x1c98b5d7, 0x05838496, 0x821b9859, + 0x9b00a918, 0xb02dfadb, 0xa936cb9a, 0xe6775d5d, 0xff6c6c1c, + 0xd4413fdf, 0xcd5a0e9e, 0x958424a2, 0x8c9f15e3, 0xa7b24620, + 0xbea97761, 0xf1e8e1a6, 0xe8f3d0e7, 0xc3de8324, 0xdac5b265, + 0x5d5daeaa, 0x44469feb, 0x6f6bcc28, 0x7670fd69, 0x39316bae, + 0x202a5aef, 0x0b07092c, 0x121c386d, 0xdf4636f3, 0xc65d07b2, + 0xed705471, 0xf46b6530, 0xbb2af3f7, 0xa231c2b6, 0x891c9175, + 0x9007a034, 0x179fbcfb, 0x0e848dba, 0x25a9de79, 0x3cb2ef38, + 0x73f379ff, 0x6ae848be, 0x41c51b7d, 0x58de2a3c, 0xf0794f05, + 0xe9627e44, 0xc24f2d87, 0xdb541cc6, 0x94158a01, 0x8d0ebb40, + 0xa623e883, 0xbf38d9c2, 0x38a0c50d, 0x21bbf44c, 0x0a96a78f, + 0x138d96ce, 0x5ccc0009, 0x45d73148, 0x6efa628b, 0x77e153ca, + 0xbabb5d54, 0xa3a06c15, 0x888d3fd6, 0x91960e97, 0xded79850, + 0xc7cca911, 0xece1fad2, 0xf5facb93, 0x7262d75c, 0x6b79e61d, + 0x4054b5de, 0x594f849f, 0x160e1258, 0x0f152319, 0x243870da, + 0x3d23419b, 0x65fd6ba7, 0x7ce65ae6, 0x57cb0925, 0x4ed03864, + 0x0191aea3, 0x188a9fe2, 0x33a7cc21, 0x2abcfd60, 0xad24e1af, + 0xb43fd0ee, 0x9f12832d, 0x8609b26c, 0xc94824ab, 0xd05315ea, + 0xfb7e4629, 0xe2657768, 0x2f3f79f6, 0x362448b7, 0x1d091b74, + 0x04122a35, 0x4b53bcf2, 0x52488db3, 0x7965de70, 0x607eef31, + 0xe7e6f3fe, 0xfefdc2bf, 0xd5d0917c, 0xcccba03d, 0x838a36fa, + 0x9a9107bb, 0xb1bc5478, 0xa8a76539, 0x3b83984b, 0x2298a90a, + 0x09b5fac9, 0x10aecb88, 0x5fef5d4f, 0x46f46c0e, 0x6dd93fcd, + 0x74c20e8c, 0xf35a1243, 0xea412302, 0xc16c70c1, 0xd8774180, + 0x9736d747, 0x8e2de606, 0xa500b5c5, 0xbc1b8484, 0x71418a1a, + 0x685abb5b, 0x4377e898, 0x5a6cd9d9, 0x152d4f1e, 0x0c367e5f, + 0x271b2d9c, 0x3e001cdd, 0xb9980012, 0xa0833153, 0x8bae6290, + 0x92b553d1, 0xddf4c516, 0xc4eff457, 0xefc2a794, 0xf6d996d5, + 0xae07bce9, 0xb71c8da8, 0x9c31de6b, 0x852aef2a, 0xca6b79ed, + 0xd37048ac, 0xf85d1b6f, 0xe1462a2e, 0x66de36e1, 0x7fc507a0, + 0x54e85463, 0x4df36522, 0x02b2f3e5, 0x1ba9c2a4, 0x30849167, + 0x299fa026, 0xe4c5aeb8, 0xfdde9ff9, 0xd6f3cc3a, 0xcfe8fd7b, + 0x80a96bbc, 0x99b25afd, 0xb29f093e, 0xab84387f, 0x2c1c24b0, + 0x350715f1, 0x1e2a4632, 0x07317773, 0x4870e1b4, 0x516bd0f5, + 0x7a468336, 0x635db277, 0xcbfad74e, 0xd2e1e60f, 0xf9ccb5cc, + 0xe0d7848d, 0xaf96124a, 0xb68d230b, 0x9da070c8, 0x84bb4189, + 0x03235d46, 0x1a386c07, 0x31153fc4, 0x280e0e85, 0x674f9842, + 0x7e54a903, 0x5579fac0, 0x4c62cb81, 0x8138c51f, 0x9823f45e, + 0xb30ea79d, 0xaa1596dc, 0xe554001b, 0xfc4f315a, 0xd7626299, + 0xce7953d8, 0x49e14f17, 0x50fa7e56, 0x7bd72d95, 0x62cc1cd4, + 0x2d8d8a13, 0x3496bb52, 0x1fbbe891, 0x06a0d9d0, 0x5e7ef3ec, + 0x4765c2ad, 0x6c48916e, 0x7553a02f, 0x3a1236e8, 0x230907a9, + 0x0824546a, 0x113f652b, 0x96a779e4, 0x8fbc48a5, 0xa4911b66, + 0xbd8a2a27, 0xf2cbbce0, 0xebd08da1, 0xc0fdde62, 0xd9e6ef23, + 0x14bce1bd, 0x0da7d0fc, 0x268a833f, 0x3f91b27e, 0x70d024b9, + 0x69cb15f8, 0x42e6463b, 0x5bfd777a, 0xdc656bb5, 0xc57e5af4, + 0xee530937, 0xf7483876, 0xb809aeb1, 0xa1129ff0, 0x8a3fcc33, + 0x9324fd72}, + {0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, + 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, + 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, + 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, + 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, + 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, + 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, + 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, + 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, + 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, + 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, + 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, + 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, + 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, + 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, + 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, + 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, + 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, + 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, + 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, + 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, + 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, + 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, + 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, + 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, + 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, + 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, + 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, + 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, + 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, + 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, + 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, + 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, + 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, + 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, + 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, + 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, + 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, + 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, + 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, + 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, + 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, + 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, + 0x2d02ef8d}}; + +local const z_word_t FAR crc_braid_big_table[][256] = { + {0x0000000000000000, 0x9630077700000000, 0x2c610eee00000000, + 0xba51099900000000, 0x19c46d0700000000, 0x8ff46a7000000000, + 0x35a563e900000000, 0xa395649e00000000, 0x3288db0e00000000, + 0xa4b8dc7900000000, 0x1ee9d5e000000000, 0x88d9d29700000000, + 0x2b4cb60900000000, 0xbd7cb17e00000000, 0x072db8e700000000, + 0x911dbf9000000000, 0x6410b71d00000000, 0xf220b06a00000000, + 0x4871b9f300000000, 0xde41be8400000000, 0x7dd4da1a00000000, + 0xebe4dd6d00000000, 0x51b5d4f400000000, 0xc785d38300000000, + 0x56986c1300000000, 0xc0a86b6400000000, 0x7af962fd00000000, + 0xecc9658a00000000, 0x4f5c011400000000, 0xd96c066300000000, + 0x633d0ffa00000000, 0xf50d088d00000000, 0xc8206e3b00000000, + 0x5e10694c00000000, 0xe44160d500000000, 0x727167a200000000, + 0xd1e4033c00000000, 0x47d4044b00000000, 0xfd850dd200000000, + 0x6bb50aa500000000, 0xfaa8b53500000000, 0x6c98b24200000000, + 0xd6c9bbdb00000000, 0x40f9bcac00000000, 0xe36cd83200000000, + 0x755cdf4500000000, 0xcf0dd6dc00000000, 0x593dd1ab00000000, + 0xac30d92600000000, 0x3a00de5100000000, 0x8051d7c800000000, + 0x1661d0bf00000000, 0xb5f4b42100000000, 0x23c4b35600000000, + 0x9995bacf00000000, 0x0fa5bdb800000000, 0x9eb8022800000000, + 0x0888055f00000000, 0xb2d90cc600000000, 0x24e90bb100000000, + 0x877c6f2f00000000, 0x114c685800000000, 0xab1d61c100000000, + 0x3d2d66b600000000, 0x9041dc7600000000, 0x0671db0100000000, + 0xbc20d29800000000, 0x2a10d5ef00000000, 0x8985b17100000000, + 0x1fb5b60600000000, 0xa5e4bf9f00000000, 0x33d4b8e800000000, + 0xa2c9077800000000, 0x34f9000f00000000, 0x8ea8099600000000, + 0x18980ee100000000, 0xbb0d6a7f00000000, 0x2d3d6d0800000000, + 0x976c649100000000, 0x015c63e600000000, 0xf4516b6b00000000, + 0x62616c1c00000000, 0xd830658500000000, 0x4e0062f200000000, + 0xed95066c00000000, 0x7ba5011b00000000, 0xc1f4088200000000, + 0x57c40ff500000000, 0xc6d9b06500000000, 0x50e9b71200000000, + 0xeab8be8b00000000, 0x7c88b9fc00000000, 0xdf1ddd6200000000, + 0x492dda1500000000, 0xf37cd38c00000000, 0x654cd4fb00000000, + 0x5861b24d00000000, 0xce51b53a00000000, 0x7400bca300000000, + 0xe230bbd400000000, 0x41a5df4a00000000, 0xd795d83d00000000, + 0x6dc4d1a400000000, 0xfbf4d6d300000000, 0x6ae9694300000000, + 0xfcd96e3400000000, 0x468867ad00000000, 0xd0b860da00000000, + 0x732d044400000000, 0xe51d033300000000, 0x5f4c0aaa00000000, + 0xc97c0ddd00000000, 0x3c71055000000000, 0xaa41022700000000, + 0x10100bbe00000000, 0x86200cc900000000, 0x25b5685700000000, + 0xb3856f2000000000, 0x09d466b900000000, 0x9fe461ce00000000, + 0x0ef9de5e00000000, 0x98c9d92900000000, 0x2298d0b000000000, + 0xb4a8d7c700000000, 0x173db35900000000, 0x810db42e00000000, + 0x3b5cbdb700000000, 0xad6cbac000000000, 0x2083b8ed00000000, + 0xb6b3bf9a00000000, 0x0ce2b60300000000, 0x9ad2b17400000000, + 0x3947d5ea00000000, 0xaf77d29d00000000, 0x1526db0400000000, + 0x8316dc7300000000, 0x120b63e300000000, 0x843b649400000000, + 0x3e6a6d0d00000000, 0xa85a6a7a00000000, 0x0bcf0ee400000000, + 0x9dff099300000000, 0x27ae000a00000000, 0xb19e077d00000000, + 0x44930ff000000000, 0xd2a3088700000000, 0x68f2011e00000000, + 0xfec2066900000000, 0x5d5762f700000000, 0xcb67658000000000, + 0x71366c1900000000, 0xe7066b6e00000000, 0x761bd4fe00000000, + 0xe02bd38900000000, 0x5a7ada1000000000, 0xcc4add6700000000, + 0x6fdfb9f900000000, 0xf9efbe8e00000000, 0x43beb71700000000, + 0xd58eb06000000000, 0xe8a3d6d600000000, 0x7e93d1a100000000, + 0xc4c2d83800000000, 0x52f2df4f00000000, 0xf167bbd100000000, + 0x6757bca600000000, 0xdd06b53f00000000, 0x4b36b24800000000, + 0xda2b0dd800000000, 0x4c1b0aaf00000000, 0xf64a033600000000, + 0x607a044100000000, 0xc3ef60df00000000, 0x55df67a800000000, + 0xef8e6e3100000000, 0x79be694600000000, 0x8cb361cb00000000, + 0x1a8366bc00000000, 0xa0d26f2500000000, 0x36e2685200000000, + 0x95770ccc00000000, 0x03470bbb00000000, 0xb916022200000000, + 0x2f26055500000000, 0xbe3bbac500000000, 0x280bbdb200000000, + 0x925ab42b00000000, 0x046ab35c00000000, 0xa7ffd7c200000000, + 0x31cfd0b500000000, 0x8b9ed92c00000000, 0x1daede5b00000000, + 0xb0c2649b00000000, 0x26f263ec00000000, 0x9ca36a7500000000, + 0x0a936d0200000000, 0xa906099c00000000, 0x3f360eeb00000000, + 0x8567077200000000, 0x1357000500000000, 0x824abf9500000000, + 0x147ab8e200000000, 0xae2bb17b00000000, 0x381bb60c00000000, + 0x9b8ed29200000000, 0x0dbed5e500000000, 0xb7efdc7c00000000, + 0x21dfdb0b00000000, 0xd4d2d38600000000, 0x42e2d4f100000000, + 0xf8b3dd6800000000, 0x6e83da1f00000000, 0xcd16be8100000000, + 0x5b26b9f600000000, 0xe177b06f00000000, 0x7747b71800000000, + 0xe65a088800000000, 0x706a0fff00000000, 0xca3b066600000000, + 0x5c0b011100000000, 0xff9e658f00000000, 0x69ae62f800000000, + 0xd3ff6b6100000000, 0x45cf6c1600000000, 0x78e20aa000000000, + 0xeed20dd700000000, 0x5483044e00000000, 0xc2b3033900000000, + 0x612667a700000000, 0xf71660d000000000, 0x4d47694900000000, + 0xdb776e3e00000000, 0x4a6ad1ae00000000, 0xdc5ad6d900000000, + 0x660bdf4000000000, 0xf03bd83700000000, 0x53aebca900000000, + 0xc59ebbde00000000, 0x7fcfb24700000000, 0xe9ffb53000000000, + 0x1cf2bdbd00000000, 0x8ac2baca00000000, 0x3093b35300000000, + 0xa6a3b42400000000, 0x0536d0ba00000000, 0x9306d7cd00000000, + 0x2957de5400000000, 0xbf67d92300000000, 0x2e7a66b300000000, + 0xb84a61c400000000, 0x021b685d00000000, 0x942b6f2a00000000, + 0x37be0bb400000000, 0xa18e0cc300000000, 0x1bdf055a00000000, + 0x8def022d00000000}, + {0x0000000000000000, 0x41311b1900000000, 0x8262363200000000, + 0xc3532d2b00000000, 0x04c56c6400000000, 0x45f4777d00000000, + 0x86a75a5600000000, 0xc796414f00000000, 0x088ad9c800000000, + 0x49bbc2d100000000, 0x8ae8effa00000000, 0xcbd9f4e300000000, + 0x0c4fb5ac00000000, 0x4d7eaeb500000000, 0x8e2d839e00000000, + 0xcf1c988700000000, 0x5112c24a00000000, 0x1023d95300000000, + 0xd370f47800000000, 0x9241ef6100000000, 0x55d7ae2e00000000, + 0x14e6b53700000000, 0xd7b5981c00000000, 0x9684830500000000, + 0x59981b8200000000, 0x18a9009b00000000, 0xdbfa2db000000000, + 0x9acb36a900000000, 0x5d5d77e600000000, 0x1c6c6cff00000000, + 0xdf3f41d400000000, 0x9e0e5acd00000000, 0xa224849500000000, + 0xe3159f8c00000000, 0x2046b2a700000000, 0x6177a9be00000000, + 0xa6e1e8f100000000, 0xe7d0f3e800000000, 0x2483dec300000000, + 0x65b2c5da00000000, 0xaaae5d5d00000000, 0xeb9f464400000000, + 0x28cc6b6f00000000, 0x69fd707600000000, 0xae6b313900000000, + 0xef5a2a2000000000, 0x2c09070b00000000, 0x6d381c1200000000, + 0xf33646df00000000, 0xb2075dc600000000, 0x715470ed00000000, + 0x30656bf400000000, 0xf7f32abb00000000, 0xb6c231a200000000, + 0x75911c8900000000, 0x34a0079000000000, 0xfbbc9f1700000000, + 0xba8d840e00000000, 0x79dea92500000000, 0x38efb23c00000000, + 0xff79f37300000000, 0xbe48e86a00000000, 0x7d1bc54100000000, + 0x3c2ade5800000000, 0x054f79f000000000, 0x447e62e900000000, + 0x872d4fc200000000, 0xc61c54db00000000, 0x018a159400000000, + 0x40bb0e8d00000000, 0x83e823a600000000, 0xc2d938bf00000000, + 0x0dc5a03800000000, 0x4cf4bb2100000000, 0x8fa7960a00000000, + 0xce968d1300000000, 0x0900cc5c00000000, 0x4831d74500000000, + 0x8b62fa6e00000000, 0xca53e17700000000, 0x545dbbba00000000, + 0x156ca0a300000000, 0xd63f8d8800000000, 0x970e969100000000, + 0x5098d7de00000000, 0x11a9ccc700000000, 0xd2fae1ec00000000, + 0x93cbfaf500000000, 0x5cd7627200000000, 0x1de6796b00000000, + 0xdeb5544000000000, 0x9f844f5900000000, 0x58120e1600000000, + 0x1923150f00000000, 0xda70382400000000, 0x9b41233d00000000, + 0xa76bfd6500000000, 0xe65ae67c00000000, 0x2509cb5700000000, + 0x6438d04e00000000, 0xa3ae910100000000, 0xe29f8a1800000000, + 0x21cca73300000000, 0x60fdbc2a00000000, 0xafe124ad00000000, + 0xeed03fb400000000, 0x2d83129f00000000, 0x6cb2098600000000, + 0xab2448c900000000, 0xea1553d000000000, 0x29467efb00000000, + 0x687765e200000000, 0xf6793f2f00000000, 0xb748243600000000, + 0x741b091d00000000, 0x352a120400000000, 0xf2bc534b00000000, + 0xb38d485200000000, 0x70de657900000000, 0x31ef7e6000000000, + 0xfef3e6e700000000, 0xbfc2fdfe00000000, 0x7c91d0d500000000, + 0x3da0cbcc00000000, 0xfa368a8300000000, 0xbb07919a00000000, + 0x7854bcb100000000, 0x3965a7a800000000, 0x4b98833b00000000, + 0x0aa9982200000000, 0xc9fab50900000000, 0x88cbae1000000000, + 0x4f5def5f00000000, 0x0e6cf44600000000, 0xcd3fd96d00000000, + 0x8c0ec27400000000, 0x43125af300000000, 0x022341ea00000000, + 0xc1706cc100000000, 0x804177d800000000, 0x47d7369700000000, + 0x06e62d8e00000000, 0xc5b500a500000000, 0x84841bbc00000000, + 0x1a8a417100000000, 0x5bbb5a6800000000, 0x98e8774300000000, + 0xd9d96c5a00000000, 0x1e4f2d1500000000, 0x5f7e360c00000000, + 0x9c2d1b2700000000, 0xdd1c003e00000000, 0x120098b900000000, + 0x533183a000000000, 0x9062ae8b00000000, 0xd153b59200000000, + 0x16c5f4dd00000000, 0x57f4efc400000000, 0x94a7c2ef00000000, + 0xd596d9f600000000, 0xe9bc07ae00000000, 0xa88d1cb700000000, + 0x6bde319c00000000, 0x2aef2a8500000000, 0xed796bca00000000, + 0xac4870d300000000, 0x6f1b5df800000000, 0x2e2a46e100000000, + 0xe136de6600000000, 0xa007c57f00000000, 0x6354e85400000000, + 0x2265f34d00000000, 0xe5f3b20200000000, 0xa4c2a91b00000000, + 0x6791843000000000, 0x26a09f2900000000, 0xb8aec5e400000000, + 0xf99fdefd00000000, 0x3accf3d600000000, 0x7bfde8cf00000000, + 0xbc6ba98000000000, 0xfd5ab29900000000, 0x3e099fb200000000, + 0x7f3884ab00000000, 0xb0241c2c00000000, 0xf115073500000000, + 0x32462a1e00000000, 0x7377310700000000, 0xb4e1704800000000, + 0xf5d06b5100000000, 0x3683467a00000000, 0x77b25d6300000000, + 0x4ed7facb00000000, 0x0fe6e1d200000000, 0xccb5ccf900000000, + 0x8d84d7e000000000, 0x4a1296af00000000, 0x0b238db600000000, + 0xc870a09d00000000, 0x8941bb8400000000, 0x465d230300000000, + 0x076c381a00000000, 0xc43f153100000000, 0x850e0e2800000000, + 0x42984f6700000000, 0x03a9547e00000000, 0xc0fa795500000000, + 0x81cb624c00000000, 0x1fc5388100000000, 0x5ef4239800000000, + 0x9da70eb300000000, 0xdc9615aa00000000, 0x1b0054e500000000, + 0x5a314ffc00000000, 0x996262d700000000, 0xd85379ce00000000, + 0x174fe14900000000, 0x567efa5000000000, 0x952dd77b00000000, + 0xd41ccc6200000000, 0x138a8d2d00000000, 0x52bb963400000000, + 0x91e8bb1f00000000, 0xd0d9a00600000000, 0xecf37e5e00000000, + 0xadc2654700000000, 0x6e91486c00000000, 0x2fa0537500000000, + 0xe836123a00000000, 0xa907092300000000, 0x6a54240800000000, + 0x2b653f1100000000, 0xe479a79600000000, 0xa548bc8f00000000, + 0x661b91a400000000, 0x272a8abd00000000, 0xe0bccbf200000000, + 0xa18dd0eb00000000, 0x62defdc000000000, 0x23efe6d900000000, + 0xbde1bc1400000000, 0xfcd0a70d00000000, 0x3f838a2600000000, + 0x7eb2913f00000000, 0xb924d07000000000, 0xf815cb6900000000, + 0x3b46e64200000000, 0x7a77fd5b00000000, 0xb56b65dc00000000, + 0xf45a7ec500000000, 0x370953ee00000000, 0x763848f700000000, + 0xb1ae09b800000000, 0xf09f12a100000000, 0x33cc3f8a00000000, + 0x72fd249300000000}, + {0x0000000000000000, 0x376ac20100000000, 0x6ed4840300000000, + 0x59be460200000000, 0xdca8090700000000, 0xebc2cb0600000000, + 0xb27c8d0400000000, 0x85164f0500000000, 0xb851130e00000000, + 0x8f3bd10f00000000, 0xd685970d00000000, 0xe1ef550c00000000, + 0x64f91a0900000000, 0x5393d80800000000, 0x0a2d9e0a00000000, + 0x3d475c0b00000000, 0x70a3261c00000000, 0x47c9e41d00000000, + 0x1e77a21f00000000, 0x291d601e00000000, 0xac0b2f1b00000000, + 0x9b61ed1a00000000, 0xc2dfab1800000000, 0xf5b5691900000000, + 0xc8f2351200000000, 0xff98f71300000000, 0xa626b11100000000, + 0x914c731000000000, 0x145a3c1500000000, 0x2330fe1400000000, + 0x7a8eb81600000000, 0x4de47a1700000000, 0xe0464d3800000000, + 0xd72c8f3900000000, 0x8e92c93b00000000, 0xb9f80b3a00000000, + 0x3cee443f00000000, 0x0b84863e00000000, 0x523ac03c00000000, + 0x6550023d00000000, 0x58175e3600000000, 0x6f7d9c3700000000, + 0x36c3da3500000000, 0x01a9183400000000, 0x84bf573100000000, + 0xb3d5953000000000, 0xea6bd33200000000, 0xdd01113300000000, + 0x90e56b2400000000, 0xa78fa92500000000, 0xfe31ef2700000000, + 0xc95b2d2600000000, 0x4c4d622300000000, 0x7b27a02200000000, + 0x2299e62000000000, 0x15f3242100000000, 0x28b4782a00000000, + 0x1fdeba2b00000000, 0x4660fc2900000000, 0x710a3e2800000000, + 0xf41c712d00000000, 0xc376b32c00000000, 0x9ac8f52e00000000, + 0xada2372f00000000, 0xc08d9a7000000000, 0xf7e7587100000000, + 0xae591e7300000000, 0x9933dc7200000000, 0x1c25937700000000, + 0x2b4f517600000000, 0x72f1177400000000, 0x459bd57500000000, + 0x78dc897e00000000, 0x4fb64b7f00000000, 0x16080d7d00000000, + 0x2162cf7c00000000, 0xa474807900000000, 0x931e427800000000, + 0xcaa0047a00000000, 0xfdcac67b00000000, 0xb02ebc6c00000000, + 0x87447e6d00000000, 0xdefa386f00000000, 0xe990fa6e00000000, + 0x6c86b56b00000000, 0x5bec776a00000000, 0x0252316800000000, + 0x3538f36900000000, 0x087faf6200000000, 0x3f156d6300000000, + 0x66ab2b6100000000, 0x51c1e96000000000, 0xd4d7a66500000000, + 0xe3bd646400000000, 0xba03226600000000, 0x8d69e06700000000, + 0x20cbd74800000000, 0x17a1154900000000, 0x4e1f534b00000000, + 0x7975914a00000000, 0xfc63de4f00000000, 0xcb091c4e00000000, + 0x92b75a4c00000000, 0xa5dd984d00000000, 0x989ac44600000000, + 0xaff0064700000000, 0xf64e404500000000, 0xc124824400000000, + 0x4432cd4100000000, 0x73580f4000000000, 0x2ae6494200000000, + 0x1d8c8b4300000000, 0x5068f15400000000, 0x6702335500000000, + 0x3ebc755700000000, 0x09d6b75600000000, 0x8cc0f85300000000, + 0xbbaa3a5200000000, 0xe2147c5000000000, 0xd57ebe5100000000, + 0xe839e25a00000000, 0xdf53205b00000000, 0x86ed665900000000, + 0xb187a45800000000, 0x3491eb5d00000000, 0x03fb295c00000000, + 0x5a456f5e00000000, 0x6d2fad5f00000000, 0x801b35e100000000, + 0xb771f7e000000000, 0xeecfb1e200000000, 0xd9a573e300000000, + 0x5cb33ce600000000, 0x6bd9fee700000000, 0x3267b8e500000000, + 0x050d7ae400000000, 0x384a26ef00000000, 0x0f20e4ee00000000, + 0x569ea2ec00000000, 0x61f460ed00000000, 0xe4e22fe800000000, + 0xd388ede900000000, 0x8a36abeb00000000, 0xbd5c69ea00000000, + 0xf0b813fd00000000, 0xc7d2d1fc00000000, 0x9e6c97fe00000000, + 0xa90655ff00000000, 0x2c101afa00000000, 0x1b7ad8fb00000000, + 0x42c49ef900000000, 0x75ae5cf800000000, 0x48e900f300000000, + 0x7f83c2f200000000, 0x263d84f000000000, 0x115746f100000000, + 0x944109f400000000, 0xa32bcbf500000000, 0xfa958df700000000, + 0xcdff4ff600000000, 0x605d78d900000000, 0x5737bad800000000, + 0x0e89fcda00000000, 0x39e33edb00000000, 0xbcf571de00000000, + 0x8b9fb3df00000000, 0xd221f5dd00000000, 0xe54b37dc00000000, + 0xd80c6bd700000000, 0xef66a9d600000000, 0xb6d8efd400000000, + 0x81b22dd500000000, 0x04a462d000000000, 0x33cea0d100000000, + 0x6a70e6d300000000, 0x5d1a24d200000000, 0x10fe5ec500000000, + 0x27949cc400000000, 0x7e2adac600000000, 0x494018c700000000, + 0xcc5657c200000000, 0xfb3c95c300000000, 0xa282d3c100000000, + 0x95e811c000000000, 0xa8af4dcb00000000, 0x9fc58fca00000000, + 0xc67bc9c800000000, 0xf1110bc900000000, 0x740744cc00000000, + 0x436d86cd00000000, 0x1ad3c0cf00000000, 0x2db902ce00000000, + 0x4096af9100000000, 0x77fc6d9000000000, 0x2e422b9200000000, + 0x1928e99300000000, 0x9c3ea69600000000, 0xab54649700000000, + 0xf2ea229500000000, 0xc580e09400000000, 0xf8c7bc9f00000000, + 0xcfad7e9e00000000, 0x9613389c00000000, 0xa179fa9d00000000, + 0x246fb59800000000, 0x1305779900000000, 0x4abb319b00000000, + 0x7dd1f39a00000000, 0x3035898d00000000, 0x075f4b8c00000000, + 0x5ee10d8e00000000, 0x698bcf8f00000000, 0xec9d808a00000000, + 0xdbf7428b00000000, 0x8249048900000000, 0xb523c68800000000, + 0x88649a8300000000, 0xbf0e588200000000, 0xe6b01e8000000000, + 0xd1dadc8100000000, 0x54cc938400000000, 0x63a6518500000000, + 0x3a18178700000000, 0x0d72d58600000000, 0xa0d0e2a900000000, + 0x97ba20a800000000, 0xce0466aa00000000, 0xf96ea4ab00000000, + 0x7c78ebae00000000, 0x4b1229af00000000, 0x12ac6fad00000000, + 0x25c6adac00000000, 0x1881f1a700000000, 0x2feb33a600000000, + 0x765575a400000000, 0x413fb7a500000000, 0xc429f8a000000000, + 0xf3433aa100000000, 0xaafd7ca300000000, 0x9d97bea200000000, + 0xd073c4b500000000, 0xe71906b400000000, 0xbea740b600000000, + 0x89cd82b700000000, 0x0cdbcdb200000000, 0x3bb10fb300000000, + 0x620f49b100000000, 0x55658bb000000000, 0x6822d7bb00000000, + 0x5f4815ba00000000, 0x06f653b800000000, 0x319c91b900000000, + 0xb48adebc00000000, 0x83e01cbd00000000, 0xda5e5abf00000000, + 0xed3498be00000000}, + {0x0000000000000000, 0x6567bcb800000000, 0x8bc809aa00000000, + 0xeeafb51200000000, 0x5797628f00000000, 0x32f0de3700000000, + 0xdc5f6b2500000000, 0xb938d79d00000000, 0xef28b4c500000000, + 0x8a4f087d00000000, 0x64e0bd6f00000000, 0x018701d700000000, + 0xb8bfd64a00000000, 0xddd86af200000000, 0x3377dfe000000000, + 0x5610635800000000, 0x9f57195000000000, 0xfa30a5e800000000, + 0x149f10fa00000000, 0x71f8ac4200000000, 0xc8c07bdf00000000, + 0xada7c76700000000, 0x4308727500000000, 0x266fcecd00000000, + 0x707fad9500000000, 0x1518112d00000000, 0xfbb7a43f00000000, + 0x9ed0188700000000, 0x27e8cf1a00000000, 0x428f73a200000000, + 0xac20c6b000000000, 0xc9477a0800000000, 0x3eaf32a000000000, + 0x5bc88e1800000000, 0xb5673b0a00000000, 0xd00087b200000000, + 0x6938502f00000000, 0x0c5fec9700000000, 0xe2f0598500000000, + 0x8797e53d00000000, 0xd187866500000000, 0xb4e03add00000000, + 0x5a4f8fcf00000000, 0x3f28337700000000, 0x8610e4ea00000000, + 0xe377585200000000, 0x0dd8ed4000000000, 0x68bf51f800000000, + 0xa1f82bf000000000, 0xc49f974800000000, 0x2a30225a00000000, + 0x4f579ee200000000, 0xf66f497f00000000, 0x9308f5c700000000, + 0x7da740d500000000, 0x18c0fc6d00000000, 0x4ed09f3500000000, + 0x2bb7238d00000000, 0xc518969f00000000, 0xa07f2a2700000000, + 0x1947fdba00000000, 0x7c20410200000000, 0x928ff41000000000, + 0xf7e848a800000000, 0x3d58149b00000000, 0x583fa82300000000, + 0xb6901d3100000000, 0xd3f7a18900000000, 0x6acf761400000000, + 0x0fa8caac00000000, 0xe1077fbe00000000, 0x8460c30600000000, + 0xd270a05e00000000, 0xb7171ce600000000, 0x59b8a9f400000000, + 0x3cdf154c00000000, 0x85e7c2d100000000, 0xe0807e6900000000, + 0x0e2fcb7b00000000, 0x6b4877c300000000, 0xa20f0dcb00000000, + 0xc768b17300000000, 0x29c7046100000000, 0x4ca0b8d900000000, + 0xf5986f4400000000, 0x90ffd3fc00000000, 0x7e5066ee00000000, + 0x1b37da5600000000, 0x4d27b90e00000000, 0x284005b600000000, + 0xc6efb0a400000000, 0xa3880c1c00000000, 0x1ab0db8100000000, + 0x7fd7673900000000, 0x9178d22b00000000, 0xf41f6e9300000000, + 0x03f7263b00000000, 0x66909a8300000000, 0x883f2f9100000000, + 0xed58932900000000, 0x546044b400000000, 0x3107f80c00000000, + 0xdfa84d1e00000000, 0xbacff1a600000000, 0xecdf92fe00000000, + 0x89b82e4600000000, 0x67179b5400000000, 0x027027ec00000000, + 0xbb48f07100000000, 0xde2f4cc900000000, 0x3080f9db00000000, + 0x55e7456300000000, 0x9ca03f6b00000000, 0xf9c783d300000000, + 0x176836c100000000, 0x720f8a7900000000, 0xcb375de400000000, + 0xae50e15c00000000, 0x40ff544e00000000, 0x2598e8f600000000, + 0x73888bae00000000, 0x16ef371600000000, 0xf840820400000000, + 0x9d273ebc00000000, 0x241fe92100000000, 0x4178559900000000, + 0xafd7e08b00000000, 0xcab05c3300000000, 0x3bb659ed00000000, + 0x5ed1e55500000000, 0xb07e504700000000, 0xd519ecff00000000, + 0x6c213b6200000000, 0x094687da00000000, 0xe7e932c800000000, + 0x828e8e7000000000, 0xd49eed2800000000, 0xb1f9519000000000, + 0x5f56e48200000000, 0x3a31583a00000000, 0x83098fa700000000, + 0xe66e331f00000000, 0x08c1860d00000000, 0x6da63ab500000000, + 0xa4e140bd00000000, 0xc186fc0500000000, 0x2f29491700000000, + 0x4a4ef5af00000000, 0xf376223200000000, 0x96119e8a00000000, + 0x78be2b9800000000, 0x1dd9972000000000, 0x4bc9f47800000000, + 0x2eae48c000000000, 0xc001fdd200000000, 0xa566416a00000000, + 0x1c5e96f700000000, 0x79392a4f00000000, 0x97969f5d00000000, + 0xf2f123e500000000, 0x05196b4d00000000, 0x607ed7f500000000, + 0x8ed162e700000000, 0xebb6de5f00000000, 0x528e09c200000000, + 0x37e9b57a00000000, 0xd946006800000000, 0xbc21bcd000000000, + 0xea31df8800000000, 0x8f56633000000000, 0x61f9d62200000000, + 0x049e6a9a00000000, 0xbda6bd0700000000, 0xd8c101bf00000000, + 0x366eb4ad00000000, 0x5309081500000000, 0x9a4e721d00000000, + 0xff29cea500000000, 0x11867bb700000000, 0x74e1c70f00000000, + 0xcdd9109200000000, 0xa8beac2a00000000, 0x4611193800000000, + 0x2376a58000000000, 0x7566c6d800000000, 0x10017a6000000000, + 0xfeaecf7200000000, 0x9bc973ca00000000, 0x22f1a45700000000, + 0x479618ef00000000, 0xa939adfd00000000, 0xcc5e114500000000, + 0x06ee4d7600000000, 0x6389f1ce00000000, 0x8d2644dc00000000, + 0xe841f86400000000, 0x51792ff900000000, 0x341e934100000000, + 0xdab1265300000000, 0xbfd69aeb00000000, 0xe9c6f9b300000000, + 0x8ca1450b00000000, 0x620ef01900000000, 0x07694ca100000000, + 0xbe519b3c00000000, 0xdb36278400000000, 0x3599929600000000, + 0x50fe2e2e00000000, 0x99b9542600000000, 0xfcdee89e00000000, + 0x12715d8c00000000, 0x7716e13400000000, 0xce2e36a900000000, + 0xab498a1100000000, 0x45e63f0300000000, 0x208183bb00000000, + 0x7691e0e300000000, 0x13f65c5b00000000, 0xfd59e94900000000, + 0x983e55f100000000, 0x2106826c00000000, 0x44613ed400000000, + 0xaace8bc600000000, 0xcfa9377e00000000, 0x38417fd600000000, + 0x5d26c36e00000000, 0xb389767c00000000, 0xd6eecac400000000, + 0x6fd61d5900000000, 0x0ab1a1e100000000, 0xe41e14f300000000, + 0x8179a84b00000000, 0xd769cb1300000000, 0xb20e77ab00000000, + 0x5ca1c2b900000000, 0x39c67e0100000000, 0x80fea99c00000000, + 0xe599152400000000, 0x0b36a03600000000, 0x6e511c8e00000000, + 0xa716668600000000, 0xc271da3e00000000, 0x2cde6f2c00000000, + 0x49b9d39400000000, 0xf081040900000000, 0x95e6b8b100000000, + 0x7b490da300000000, 0x1e2eb11b00000000, 0x483ed24300000000, + 0x2d596efb00000000, 0xc3f6dbe900000000, 0xa691675100000000, + 0x1fa9b0cc00000000, 0x7ace0c7400000000, 0x9461b96600000000, + 0xf10605de00000000}, + {0x0000000000000000, 0xb029603d00000000, 0x6053c07a00000000, + 0xd07aa04700000000, 0xc0a680f500000000, 0x708fe0c800000000, + 0xa0f5408f00000000, 0x10dc20b200000000, 0xc14b703000000000, + 0x7162100d00000000, 0xa118b04a00000000, 0x1131d07700000000, + 0x01edf0c500000000, 0xb1c490f800000000, 0x61be30bf00000000, + 0xd197508200000000, 0x8297e06000000000, 0x32be805d00000000, + 0xe2c4201a00000000, 0x52ed402700000000, 0x4231609500000000, + 0xf21800a800000000, 0x2262a0ef00000000, 0x924bc0d200000000, + 0x43dc905000000000, 0xf3f5f06d00000000, 0x238f502a00000000, + 0x93a6301700000000, 0x837a10a500000000, 0x3353709800000000, + 0xe329d0df00000000, 0x5300b0e200000000, 0x042fc1c100000000, + 0xb406a1fc00000000, 0x647c01bb00000000, 0xd455618600000000, + 0xc489413400000000, 0x74a0210900000000, 0xa4da814e00000000, + 0x14f3e17300000000, 0xc564b1f100000000, 0x754dd1cc00000000, + 0xa537718b00000000, 0x151e11b600000000, 0x05c2310400000000, + 0xb5eb513900000000, 0x6591f17e00000000, 0xd5b8914300000000, + 0x86b821a100000000, 0x3691419c00000000, 0xe6ebe1db00000000, + 0x56c281e600000000, 0x461ea15400000000, 0xf637c16900000000, + 0x264d612e00000000, 0x9664011300000000, 0x47f3519100000000, + 0xf7da31ac00000000, 0x27a091eb00000000, 0x9789f1d600000000, + 0x8755d16400000000, 0x377cb15900000000, 0xe706111e00000000, + 0x572f712300000000, 0x4958f35800000000, 0xf971936500000000, + 0x290b332200000000, 0x9922531f00000000, 0x89fe73ad00000000, + 0x39d7139000000000, 0xe9adb3d700000000, 0x5984d3ea00000000, + 0x8813836800000000, 0x383ae35500000000, 0xe840431200000000, + 0x5869232f00000000, 0x48b5039d00000000, 0xf89c63a000000000, + 0x28e6c3e700000000, 0x98cfa3da00000000, 0xcbcf133800000000, + 0x7be6730500000000, 0xab9cd34200000000, 0x1bb5b37f00000000, + 0x0b6993cd00000000, 0xbb40f3f000000000, 0x6b3a53b700000000, + 0xdb13338a00000000, 0x0a84630800000000, 0xbaad033500000000, + 0x6ad7a37200000000, 0xdafec34f00000000, 0xca22e3fd00000000, + 0x7a0b83c000000000, 0xaa71238700000000, 0x1a5843ba00000000, + 0x4d77329900000000, 0xfd5e52a400000000, 0x2d24f2e300000000, + 0x9d0d92de00000000, 0x8dd1b26c00000000, 0x3df8d25100000000, + 0xed82721600000000, 0x5dab122b00000000, 0x8c3c42a900000000, + 0x3c15229400000000, 0xec6f82d300000000, 0x5c46e2ee00000000, + 0x4c9ac25c00000000, 0xfcb3a26100000000, 0x2cc9022600000000, + 0x9ce0621b00000000, 0xcfe0d2f900000000, 0x7fc9b2c400000000, + 0xafb3128300000000, 0x1f9a72be00000000, 0x0f46520c00000000, + 0xbf6f323100000000, 0x6f15927600000000, 0xdf3cf24b00000000, + 0x0eaba2c900000000, 0xbe82c2f400000000, 0x6ef862b300000000, + 0xded1028e00000000, 0xce0d223c00000000, 0x7e24420100000000, + 0xae5ee24600000000, 0x1e77827b00000000, 0x92b0e6b100000000, + 0x2299868c00000000, 0xf2e326cb00000000, 0x42ca46f600000000, + 0x5216664400000000, 0xe23f067900000000, 0x3245a63e00000000, + 0x826cc60300000000, 0x53fb968100000000, 0xe3d2f6bc00000000, + 0x33a856fb00000000, 0x838136c600000000, 0x935d167400000000, + 0x2374764900000000, 0xf30ed60e00000000, 0x4327b63300000000, + 0x102706d100000000, 0xa00e66ec00000000, 0x7074c6ab00000000, + 0xc05da69600000000, 0xd081862400000000, 0x60a8e61900000000, + 0xb0d2465e00000000, 0x00fb266300000000, 0xd16c76e100000000, + 0x614516dc00000000, 0xb13fb69b00000000, 0x0116d6a600000000, + 0x11caf61400000000, 0xa1e3962900000000, 0x7199366e00000000, + 0xc1b0565300000000, 0x969f277000000000, 0x26b6474d00000000, + 0xf6cce70a00000000, 0x46e5873700000000, 0x5639a78500000000, + 0xe610c7b800000000, 0x366a67ff00000000, 0x864307c200000000, + 0x57d4574000000000, 0xe7fd377d00000000, 0x3787973a00000000, + 0x87aef70700000000, 0x9772d7b500000000, 0x275bb78800000000, + 0xf72117cf00000000, 0x470877f200000000, 0x1408c71000000000, + 0xa421a72d00000000, 0x745b076a00000000, 0xc472675700000000, + 0xd4ae47e500000000, 0x648727d800000000, 0xb4fd879f00000000, + 0x04d4e7a200000000, 0xd543b72000000000, 0x656ad71d00000000, + 0xb510775a00000000, 0x0539176700000000, 0x15e537d500000000, + 0xa5cc57e800000000, 0x75b6f7af00000000, 0xc59f979200000000, + 0xdbe815e900000000, 0x6bc175d400000000, 0xbbbbd59300000000, + 0x0b92b5ae00000000, 0x1b4e951c00000000, 0xab67f52100000000, + 0x7b1d556600000000, 0xcb34355b00000000, 0x1aa365d900000000, + 0xaa8a05e400000000, 0x7af0a5a300000000, 0xcad9c59e00000000, + 0xda05e52c00000000, 0x6a2c851100000000, 0xba56255600000000, + 0x0a7f456b00000000, 0x597ff58900000000, 0xe95695b400000000, + 0x392c35f300000000, 0x890555ce00000000, 0x99d9757c00000000, + 0x29f0154100000000, 0xf98ab50600000000, 0x49a3d53b00000000, + 0x983485b900000000, 0x281de58400000000, 0xf86745c300000000, + 0x484e25fe00000000, 0x5892054c00000000, 0xe8bb657100000000, + 0x38c1c53600000000, 0x88e8a50b00000000, 0xdfc7d42800000000, + 0x6feeb41500000000, 0xbf94145200000000, 0x0fbd746f00000000, + 0x1f6154dd00000000, 0xaf4834e000000000, 0x7f3294a700000000, + 0xcf1bf49a00000000, 0x1e8ca41800000000, 0xaea5c42500000000, + 0x7edf646200000000, 0xcef6045f00000000, 0xde2a24ed00000000, + 0x6e0344d000000000, 0xbe79e49700000000, 0x0e5084aa00000000, + 0x5d50344800000000, 0xed79547500000000, 0x3d03f43200000000, + 0x8d2a940f00000000, 0x9df6b4bd00000000, 0x2ddfd48000000000, + 0xfda574c700000000, 0x4d8c14fa00000000, 0x9c1b447800000000, + 0x2c32244500000000, 0xfc48840200000000, 0x4c61e43f00000000, + 0x5cbdc48d00000000, 0xec94a4b000000000, 0x3cee04f700000000, + 0x8cc764ca00000000}, + {0x0000000000000000, 0xa5d35ccb00000000, 0x0ba1c84d00000000, + 0xae72948600000000, 0x1642919b00000000, 0xb391cd5000000000, + 0x1de359d600000000, 0xb830051d00000000, 0x6d8253ec00000000, + 0xc8510f2700000000, 0x66239ba100000000, 0xc3f0c76a00000000, + 0x7bc0c27700000000, 0xde139ebc00000000, 0x70610a3a00000000, + 0xd5b256f100000000, 0x9b02d60300000000, 0x3ed18ac800000000, + 0x90a31e4e00000000, 0x3570428500000000, 0x8d40479800000000, + 0x28931b5300000000, 0x86e18fd500000000, 0x2332d31e00000000, + 0xf68085ef00000000, 0x5353d92400000000, 0xfd214da200000000, + 0x58f2116900000000, 0xe0c2147400000000, 0x451148bf00000000, + 0xeb63dc3900000000, 0x4eb080f200000000, 0x3605ac0700000000, + 0x93d6f0cc00000000, 0x3da4644a00000000, 0x9877388100000000, + 0x20473d9c00000000, 0x8594615700000000, 0x2be6f5d100000000, + 0x8e35a91a00000000, 0x5b87ffeb00000000, 0xfe54a32000000000, + 0x502637a600000000, 0xf5f56b6d00000000, 0x4dc56e7000000000, + 0xe81632bb00000000, 0x4664a63d00000000, 0xe3b7faf600000000, + 0xad077a0400000000, 0x08d426cf00000000, 0xa6a6b24900000000, + 0x0375ee8200000000, 0xbb45eb9f00000000, 0x1e96b75400000000, + 0xb0e423d200000000, 0x15377f1900000000, 0xc08529e800000000, + 0x6556752300000000, 0xcb24e1a500000000, 0x6ef7bd6e00000000, + 0xd6c7b87300000000, 0x7314e4b800000000, 0xdd66703e00000000, + 0x78b52cf500000000, 0x6c0a580f00000000, 0xc9d904c400000000, + 0x67ab904200000000, 0xc278cc8900000000, 0x7a48c99400000000, + 0xdf9b955f00000000, 0x71e901d900000000, 0xd43a5d1200000000, + 0x01880be300000000, 0xa45b572800000000, 0x0a29c3ae00000000, + 0xaffa9f6500000000, 0x17ca9a7800000000, 0xb219c6b300000000, + 0x1c6b523500000000, 0xb9b80efe00000000, 0xf7088e0c00000000, + 0x52dbd2c700000000, 0xfca9464100000000, 0x597a1a8a00000000, + 0xe14a1f9700000000, 0x4499435c00000000, 0xeaebd7da00000000, + 0x4f388b1100000000, 0x9a8adde000000000, 0x3f59812b00000000, + 0x912b15ad00000000, 0x34f8496600000000, 0x8cc84c7b00000000, + 0x291b10b000000000, 0x8769843600000000, 0x22bad8fd00000000, + 0x5a0ff40800000000, 0xffdca8c300000000, 0x51ae3c4500000000, + 0xf47d608e00000000, 0x4c4d659300000000, 0xe99e395800000000, + 0x47ecadde00000000, 0xe23ff11500000000, 0x378da7e400000000, + 0x925efb2f00000000, 0x3c2c6fa900000000, 0x99ff336200000000, + 0x21cf367f00000000, 0x841c6ab400000000, 0x2a6efe3200000000, + 0x8fbda2f900000000, 0xc10d220b00000000, 0x64de7ec000000000, + 0xcaacea4600000000, 0x6f7fb68d00000000, 0xd74fb39000000000, + 0x729cef5b00000000, 0xdcee7bdd00000000, 0x793d271600000000, + 0xac8f71e700000000, 0x095c2d2c00000000, 0xa72eb9aa00000000, + 0x02fde56100000000, 0xbacde07c00000000, 0x1f1ebcb700000000, + 0xb16c283100000000, 0x14bf74fa00000000, 0xd814b01e00000000, + 0x7dc7ecd500000000, 0xd3b5785300000000, 0x7666249800000000, + 0xce56218500000000, 0x6b857d4e00000000, 0xc5f7e9c800000000, + 0x6024b50300000000, 0xb596e3f200000000, 0x1045bf3900000000, + 0xbe372bbf00000000, 0x1be4777400000000, 0xa3d4726900000000, + 0x06072ea200000000, 0xa875ba2400000000, 0x0da6e6ef00000000, + 0x4316661d00000000, 0xe6c53ad600000000, 0x48b7ae5000000000, + 0xed64f29b00000000, 0x5554f78600000000, 0xf087ab4d00000000, + 0x5ef53fcb00000000, 0xfb26630000000000, 0x2e9435f100000000, + 0x8b47693a00000000, 0x2535fdbc00000000, 0x80e6a17700000000, + 0x38d6a46a00000000, 0x9d05f8a100000000, 0x33776c2700000000, + 0x96a430ec00000000, 0xee111c1900000000, 0x4bc240d200000000, + 0xe5b0d45400000000, 0x4063889f00000000, 0xf8538d8200000000, + 0x5d80d14900000000, 0xf3f245cf00000000, 0x5621190400000000, + 0x83934ff500000000, 0x2640133e00000000, 0x883287b800000000, + 0x2de1db7300000000, 0x95d1de6e00000000, 0x300282a500000000, + 0x9e70162300000000, 0x3ba34ae800000000, 0x7513ca1a00000000, + 0xd0c096d100000000, 0x7eb2025700000000, 0xdb615e9c00000000, + 0x63515b8100000000, 0xc682074a00000000, 0x68f093cc00000000, + 0xcd23cf0700000000, 0x189199f600000000, 0xbd42c53d00000000, + 0x133051bb00000000, 0xb6e30d7000000000, 0x0ed3086d00000000, + 0xab0054a600000000, 0x0572c02000000000, 0xa0a19ceb00000000, + 0xb41ee81100000000, 0x11cdb4da00000000, 0xbfbf205c00000000, + 0x1a6c7c9700000000, 0xa25c798a00000000, 0x078f254100000000, + 0xa9fdb1c700000000, 0x0c2eed0c00000000, 0xd99cbbfd00000000, + 0x7c4fe73600000000, 0xd23d73b000000000, 0x77ee2f7b00000000, + 0xcfde2a6600000000, 0x6a0d76ad00000000, 0xc47fe22b00000000, + 0x61acbee000000000, 0x2f1c3e1200000000, 0x8acf62d900000000, + 0x24bdf65f00000000, 0x816eaa9400000000, 0x395eaf8900000000, + 0x9c8df34200000000, 0x32ff67c400000000, 0x972c3b0f00000000, + 0x429e6dfe00000000, 0xe74d313500000000, 0x493fa5b300000000, + 0xececf97800000000, 0x54dcfc6500000000, 0xf10fa0ae00000000, + 0x5f7d342800000000, 0xfaae68e300000000, 0x821b441600000000, + 0x27c818dd00000000, 0x89ba8c5b00000000, 0x2c69d09000000000, + 0x9459d58d00000000, 0x318a894600000000, 0x9ff81dc000000000, + 0x3a2b410b00000000, 0xef9917fa00000000, 0x4a4a4b3100000000, + 0xe438dfb700000000, 0x41eb837c00000000, 0xf9db866100000000, + 0x5c08daaa00000000, 0xf27a4e2c00000000, 0x57a912e700000000, + 0x1919921500000000, 0xbccacede00000000, 0x12b85a5800000000, + 0xb76b069300000000, 0x0f5b038e00000000, 0xaa885f4500000000, + 0x04facbc300000000, 0xa129970800000000, 0x749bc1f900000000, + 0xd1489d3200000000, 0x7f3a09b400000000, 0xdae9557f00000000, + 0x62d9506200000000, 0xc70a0ca900000000, 0x6978982f00000000, + 0xccabc4e400000000}, + {0x0000000000000000, 0xb40b77a600000000, 0x29119f9700000000, + 0x9d1ae83100000000, 0x13244ff400000000, 0xa72f385200000000, + 0x3a35d06300000000, 0x8e3ea7c500000000, 0x674eef3300000000, + 0xd345989500000000, 0x4e5f70a400000000, 0xfa54070200000000, + 0x746aa0c700000000, 0xc061d76100000000, 0x5d7b3f5000000000, + 0xe97048f600000000, 0xce9cde6700000000, 0x7a97a9c100000000, + 0xe78d41f000000000, 0x5386365600000000, 0xddb8919300000000, + 0x69b3e63500000000, 0xf4a90e0400000000, 0x40a279a200000000, + 0xa9d2315400000000, 0x1dd946f200000000, 0x80c3aec300000000, + 0x34c8d96500000000, 0xbaf67ea000000000, 0x0efd090600000000, + 0x93e7e13700000000, 0x27ec969100000000, 0x9c39bdcf00000000, + 0x2832ca6900000000, 0xb528225800000000, 0x012355fe00000000, + 0x8f1df23b00000000, 0x3b16859d00000000, 0xa60c6dac00000000, + 0x12071a0a00000000, 0xfb7752fc00000000, 0x4f7c255a00000000, + 0xd266cd6b00000000, 0x666dbacd00000000, 0xe8531d0800000000, + 0x5c586aae00000000, 0xc142829f00000000, 0x7549f53900000000, + 0x52a563a800000000, 0xe6ae140e00000000, 0x7bb4fc3f00000000, + 0xcfbf8b9900000000, 0x41812c5c00000000, 0xf58a5bfa00000000, + 0x6890b3cb00000000, 0xdc9bc46d00000000, 0x35eb8c9b00000000, + 0x81e0fb3d00000000, 0x1cfa130c00000000, 0xa8f164aa00000000, + 0x26cfc36f00000000, 0x92c4b4c900000000, 0x0fde5cf800000000, + 0xbbd52b5e00000000, 0x79750b4400000000, 0xcd7e7ce200000000, + 0x506494d300000000, 0xe46fe37500000000, 0x6a5144b000000000, + 0xde5a331600000000, 0x4340db2700000000, 0xf74bac8100000000, + 0x1e3be47700000000, 0xaa3093d100000000, 0x372a7be000000000, + 0x83210c4600000000, 0x0d1fab8300000000, 0xb914dc2500000000, + 0x240e341400000000, 0x900543b200000000, 0xb7e9d52300000000, + 0x03e2a28500000000, 0x9ef84ab400000000, 0x2af33d1200000000, + 0xa4cd9ad700000000, 0x10c6ed7100000000, 0x8ddc054000000000, + 0x39d772e600000000, 0xd0a73a1000000000, 0x64ac4db600000000, + 0xf9b6a58700000000, 0x4dbdd22100000000, 0xc38375e400000000, + 0x7788024200000000, 0xea92ea7300000000, 0x5e999dd500000000, + 0xe54cb68b00000000, 0x5147c12d00000000, 0xcc5d291c00000000, + 0x78565eba00000000, 0xf668f97f00000000, 0x42638ed900000000, + 0xdf7966e800000000, 0x6b72114e00000000, 0x820259b800000000, + 0x36092e1e00000000, 0xab13c62f00000000, 0x1f18b18900000000, + 0x9126164c00000000, 0x252d61ea00000000, 0xb83789db00000000, + 0x0c3cfe7d00000000, 0x2bd068ec00000000, 0x9fdb1f4a00000000, + 0x02c1f77b00000000, 0xb6ca80dd00000000, 0x38f4271800000000, + 0x8cff50be00000000, 0x11e5b88f00000000, 0xa5eecf2900000000, + 0x4c9e87df00000000, 0xf895f07900000000, 0x658f184800000000, + 0xd1846fee00000000, 0x5fbac82b00000000, 0xebb1bf8d00000000, + 0x76ab57bc00000000, 0xc2a0201a00000000, 0xf2ea168800000000, + 0x46e1612e00000000, 0xdbfb891f00000000, 0x6ff0feb900000000, + 0xe1ce597c00000000, 0x55c52eda00000000, 0xc8dfc6eb00000000, + 0x7cd4b14d00000000, 0x95a4f9bb00000000, 0x21af8e1d00000000, + 0xbcb5662c00000000, 0x08be118a00000000, 0x8680b64f00000000, + 0x328bc1e900000000, 0xaf9129d800000000, 0x1b9a5e7e00000000, + 0x3c76c8ef00000000, 0x887dbf4900000000, 0x1567577800000000, + 0xa16c20de00000000, 0x2f52871b00000000, 0x9b59f0bd00000000, + 0x0643188c00000000, 0xb2486f2a00000000, 0x5b3827dc00000000, + 0xef33507a00000000, 0x7229b84b00000000, 0xc622cfed00000000, + 0x481c682800000000, 0xfc171f8e00000000, 0x610df7bf00000000, + 0xd506801900000000, 0x6ed3ab4700000000, 0xdad8dce100000000, + 0x47c234d000000000, 0xf3c9437600000000, 0x7df7e4b300000000, + 0xc9fc931500000000, 0x54e67b2400000000, 0xe0ed0c8200000000, + 0x099d447400000000, 0xbd9633d200000000, 0x208cdbe300000000, + 0x9487ac4500000000, 0x1ab90b8000000000, 0xaeb27c2600000000, + 0x33a8941700000000, 0x87a3e3b100000000, 0xa04f752000000000, + 0x1444028600000000, 0x895eeab700000000, 0x3d559d1100000000, + 0xb36b3ad400000000, 0x07604d7200000000, 0x9a7aa54300000000, + 0x2e71d2e500000000, 0xc7019a1300000000, 0x730aedb500000000, + 0xee10058400000000, 0x5a1b722200000000, 0xd425d5e700000000, + 0x602ea24100000000, 0xfd344a7000000000, 0x493f3dd600000000, + 0x8b9f1dcc00000000, 0x3f946a6a00000000, 0xa28e825b00000000, + 0x1685f5fd00000000, 0x98bb523800000000, 0x2cb0259e00000000, + 0xb1aacdaf00000000, 0x05a1ba0900000000, 0xecd1f2ff00000000, + 0x58da855900000000, 0xc5c06d6800000000, 0x71cb1ace00000000, + 0xfff5bd0b00000000, 0x4bfecaad00000000, 0xd6e4229c00000000, + 0x62ef553a00000000, 0x4503c3ab00000000, 0xf108b40d00000000, + 0x6c125c3c00000000, 0xd8192b9a00000000, 0x56278c5f00000000, + 0xe22cfbf900000000, 0x7f3613c800000000, 0xcb3d646e00000000, + 0x224d2c9800000000, 0x96465b3e00000000, 0x0b5cb30f00000000, + 0xbf57c4a900000000, 0x3169636c00000000, 0x856214ca00000000, + 0x1878fcfb00000000, 0xac738b5d00000000, 0x17a6a00300000000, + 0xa3add7a500000000, 0x3eb73f9400000000, 0x8abc483200000000, + 0x0482eff700000000, 0xb089985100000000, 0x2d93706000000000, + 0x999807c600000000, 0x70e84f3000000000, 0xc4e3389600000000, + 0x59f9d0a700000000, 0xedf2a70100000000, 0x63cc00c400000000, + 0xd7c7776200000000, 0x4add9f5300000000, 0xfed6e8f500000000, + 0xd93a7e6400000000, 0x6d3109c200000000, 0xf02be1f300000000, + 0x4420965500000000, 0xca1e319000000000, 0x7e15463600000000, + 0xe30fae0700000000, 0x5704d9a100000000, 0xbe74915700000000, + 0x0a7fe6f100000000, 0x97650ec000000000, 0x236e796600000000, + 0xad50dea300000000, 0x195ba90500000000, 0x8441413400000000, + 0x304a369200000000}, + {0x0000000000000000, 0x9e00aacc00000000, 0x7d07254200000000, + 0xe3078f8e00000000, 0xfa0e4a8400000000, 0x640ee04800000000, + 0x87096fc600000000, 0x1909c50a00000000, 0xb51be5d300000000, + 0x2b1b4f1f00000000, 0xc81cc09100000000, 0x561c6a5d00000000, + 0x4f15af5700000000, 0xd115059b00000000, 0x32128a1500000000, + 0xac1220d900000000, 0x2b31bb7c00000000, 0xb53111b000000000, + 0x56369e3e00000000, 0xc83634f200000000, 0xd13ff1f800000000, + 0x4f3f5b3400000000, 0xac38d4ba00000000, 0x32387e7600000000, + 0x9e2a5eaf00000000, 0x002af46300000000, 0xe32d7bed00000000, + 0x7d2dd12100000000, 0x6424142b00000000, 0xfa24bee700000000, + 0x1923316900000000, 0x87239ba500000000, 0x566276f900000000, + 0xc862dc3500000000, 0x2b6553bb00000000, 0xb565f97700000000, + 0xac6c3c7d00000000, 0x326c96b100000000, 0xd16b193f00000000, + 0x4f6bb3f300000000, 0xe379932a00000000, 0x7d7939e600000000, + 0x9e7eb66800000000, 0x007e1ca400000000, 0x1977d9ae00000000, + 0x8777736200000000, 0x6470fcec00000000, 0xfa70562000000000, + 0x7d53cd8500000000, 0xe353674900000000, 0x0054e8c700000000, + 0x9e54420b00000000, 0x875d870100000000, 0x195d2dcd00000000, + 0xfa5aa24300000000, 0x645a088f00000000, 0xc848285600000000, + 0x5648829a00000000, 0xb54f0d1400000000, 0x2b4fa7d800000000, + 0x324662d200000000, 0xac46c81e00000000, 0x4f41479000000000, + 0xd141ed5c00000000, 0xedc29d2900000000, 0x73c237e500000000, + 0x90c5b86b00000000, 0x0ec512a700000000, 0x17ccd7ad00000000, + 0x89cc7d6100000000, 0x6acbf2ef00000000, 0xf4cb582300000000, + 0x58d978fa00000000, 0xc6d9d23600000000, 0x25de5db800000000, + 0xbbdef77400000000, 0xa2d7327e00000000, 0x3cd798b200000000, + 0xdfd0173c00000000, 0x41d0bdf000000000, 0xc6f3265500000000, + 0x58f38c9900000000, 0xbbf4031700000000, 0x25f4a9db00000000, + 0x3cfd6cd100000000, 0xa2fdc61d00000000, 0x41fa499300000000, + 0xdffae35f00000000, 0x73e8c38600000000, 0xede8694a00000000, + 0x0eefe6c400000000, 0x90ef4c0800000000, 0x89e6890200000000, + 0x17e623ce00000000, 0xf4e1ac4000000000, 0x6ae1068c00000000, + 0xbba0ebd000000000, 0x25a0411c00000000, 0xc6a7ce9200000000, + 0x58a7645e00000000, 0x41aea15400000000, 0xdfae0b9800000000, + 0x3ca9841600000000, 0xa2a92eda00000000, 0x0ebb0e0300000000, + 0x90bba4cf00000000, 0x73bc2b4100000000, 0xedbc818d00000000, + 0xf4b5448700000000, 0x6ab5ee4b00000000, 0x89b261c500000000, + 0x17b2cb0900000000, 0x909150ac00000000, 0x0e91fa6000000000, + 0xed9675ee00000000, 0x7396df2200000000, 0x6a9f1a2800000000, + 0xf49fb0e400000000, 0x17983f6a00000000, 0x899895a600000000, + 0x258ab57f00000000, 0xbb8a1fb300000000, 0x588d903d00000000, + 0xc68d3af100000000, 0xdf84fffb00000000, 0x4184553700000000, + 0xa283dab900000000, 0x3c83707500000000, 0xda853b5300000000, + 0x4485919f00000000, 0xa7821e1100000000, 0x3982b4dd00000000, + 0x208b71d700000000, 0xbe8bdb1b00000000, 0x5d8c549500000000, + 0xc38cfe5900000000, 0x6f9ede8000000000, 0xf19e744c00000000, + 0x1299fbc200000000, 0x8c99510e00000000, 0x9590940400000000, + 0x0b903ec800000000, 0xe897b14600000000, 0x76971b8a00000000, + 0xf1b4802f00000000, 0x6fb42ae300000000, 0x8cb3a56d00000000, + 0x12b30fa100000000, 0x0bbacaab00000000, 0x95ba606700000000, + 0x76bdefe900000000, 0xe8bd452500000000, 0x44af65fc00000000, + 0xdaafcf3000000000, 0x39a840be00000000, 0xa7a8ea7200000000, + 0xbea12f7800000000, 0x20a185b400000000, 0xc3a60a3a00000000, + 0x5da6a0f600000000, 0x8ce74daa00000000, 0x12e7e76600000000, + 0xf1e068e800000000, 0x6fe0c22400000000, 0x76e9072e00000000, + 0xe8e9ade200000000, 0x0bee226c00000000, 0x95ee88a000000000, + 0x39fca87900000000, 0xa7fc02b500000000, 0x44fb8d3b00000000, + 0xdafb27f700000000, 0xc3f2e2fd00000000, 0x5df2483100000000, + 0xbef5c7bf00000000, 0x20f56d7300000000, 0xa7d6f6d600000000, + 0x39d65c1a00000000, 0xdad1d39400000000, 0x44d1795800000000, + 0x5dd8bc5200000000, 0xc3d8169e00000000, 0x20df991000000000, + 0xbedf33dc00000000, 0x12cd130500000000, 0x8ccdb9c900000000, + 0x6fca364700000000, 0xf1ca9c8b00000000, 0xe8c3598100000000, + 0x76c3f34d00000000, 0x95c47cc300000000, 0x0bc4d60f00000000, + 0x3747a67a00000000, 0xa9470cb600000000, 0x4a40833800000000, + 0xd44029f400000000, 0xcd49ecfe00000000, 0x5349463200000000, + 0xb04ec9bc00000000, 0x2e4e637000000000, 0x825c43a900000000, + 0x1c5ce96500000000, 0xff5b66eb00000000, 0x615bcc2700000000, + 0x7852092d00000000, 0xe652a3e100000000, 0x05552c6f00000000, + 0x9b5586a300000000, 0x1c761d0600000000, 0x8276b7ca00000000, + 0x6171384400000000, 0xff71928800000000, 0xe678578200000000, + 0x7878fd4e00000000, 0x9b7f72c000000000, 0x057fd80c00000000, + 0xa96df8d500000000, 0x376d521900000000, 0xd46add9700000000, + 0x4a6a775b00000000, 0x5363b25100000000, 0xcd63189d00000000, + 0x2e64971300000000, 0xb0643ddf00000000, 0x6125d08300000000, + 0xff257a4f00000000, 0x1c22f5c100000000, 0x82225f0d00000000, + 0x9b2b9a0700000000, 0x052b30cb00000000, 0xe62cbf4500000000, + 0x782c158900000000, 0xd43e355000000000, 0x4a3e9f9c00000000, + 0xa939101200000000, 0x3739bade00000000, 0x2e307fd400000000, + 0xb030d51800000000, 0x53375a9600000000, 0xcd37f05a00000000, + 0x4a146bff00000000, 0xd414c13300000000, 0x37134ebd00000000, + 0xa913e47100000000, 0xb01a217b00000000, 0x2e1a8bb700000000, + 0xcd1d043900000000, 0x531daef500000000, 0xff0f8e2c00000000, + 0x610f24e000000000, 0x8208ab6e00000000, 0x1c0801a200000000, + 0x0501c4a800000000, 0x9b016e6400000000, 0x7806e1ea00000000, + 0xe6064b2600000000}}; + +#else /* W == 4 */ + +local const z_crc_t FAR crc_braid_table[][256] = { + {0x00000000, 0xb8bc6765, 0xaa09c88b, 0x12b5afee, 0x8f629757, + 0x37def032, 0x256b5fdc, 0x9dd738b9, 0xc5b428ef, 0x7d084f8a, + 0x6fbde064, 0xd7018701, 0x4ad6bfb8, 0xf26ad8dd, 0xe0df7733, + 0x58631056, 0x5019579f, 0xe8a530fa, 0xfa109f14, 0x42acf871, + 0xdf7bc0c8, 0x67c7a7ad, 0x75720843, 0xcdce6f26, 0x95ad7f70, + 0x2d111815, 0x3fa4b7fb, 0x8718d09e, 0x1acfe827, 0xa2738f42, + 0xb0c620ac, 0x087a47c9, 0xa032af3e, 0x188ec85b, 0x0a3b67b5, + 0xb28700d0, 0x2f503869, 0x97ec5f0c, 0x8559f0e2, 0x3de59787, + 0x658687d1, 0xdd3ae0b4, 0xcf8f4f5a, 0x7733283f, 0xeae41086, + 0x525877e3, 0x40edd80d, 0xf851bf68, 0xf02bf8a1, 0x48979fc4, + 0x5a22302a, 0xe29e574f, 0x7f496ff6, 0xc7f50893, 0xd540a77d, + 0x6dfcc018, 0x359fd04e, 0x8d23b72b, 0x9f9618c5, 0x272a7fa0, + 0xbafd4719, 0x0241207c, 0x10f48f92, 0xa848e8f7, 0x9b14583d, + 0x23a83f58, 0x311d90b6, 0x89a1f7d3, 0x1476cf6a, 0xaccaa80f, + 0xbe7f07e1, 0x06c36084, 0x5ea070d2, 0xe61c17b7, 0xf4a9b859, + 0x4c15df3c, 0xd1c2e785, 0x697e80e0, 0x7bcb2f0e, 0xc377486b, + 0xcb0d0fa2, 0x73b168c7, 0x6104c729, 0xd9b8a04c, 0x446f98f5, + 0xfcd3ff90, 0xee66507e, 0x56da371b, 0x0eb9274d, 0xb6054028, + 0xa4b0efc6, 0x1c0c88a3, 0x81dbb01a, 0x3967d77f, 0x2bd27891, + 0x936e1ff4, 0x3b26f703, 0x839a9066, 0x912f3f88, 0x299358ed, + 0xb4446054, 0x0cf80731, 0x1e4da8df, 0xa6f1cfba, 0xfe92dfec, + 0x462eb889, 0x549b1767, 0xec277002, 0x71f048bb, 0xc94c2fde, + 0xdbf98030, 0x6345e755, 0x6b3fa09c, 0xd383c7f9, 0xc1366817, + 0x798a0f72, 0xe45d37cb, 0x5ce150ae, 0x4e54ff40, 0xf6e89825, + 0xae8b8873, 0x1637ef16, 0x048240f8, 0xbc3e279d, 0x21e91f24, + 0x99557841, 0x8be0d7af, 0x335cb0ca, 0xed59b63b, 0x55e5d15e, + 0x47507eb0, 0xffec19d5, 0x623b216c, 0xda874609, 0xc832e9e7, + 0x708e8e82, 0x28ed9ed4, 0x9051f9b1, 0x82e4565f, 0x3a58313a, + 0xa78f0983, 0x1f336ee6, 0x0d86c108, 0xb53aa66d, 0xbd40e1a4, + 0x05fc86c1, 0x1749292f, 0xaff54e4a, 0x322276f3, 0x8a9e1196, + 0x982bbe78, 0x2097d91d, 0x78f4c94b, 0xc048ae2e, 0xd2fd01c0, + 0x6a4166a5, 0xf7965e1c, 0x4f2a3979, 0x5d9f9697, 0xe523f1f2, + 0x4d6b1905, 0xf5d77e60, 0xe762d18e, 0x5fdeb6eb, 0xc2098e52, + 0x7ab5e937, 0x680046d9, 0xd0bc21bc, 0x88df31ea, 0x3063568f, + 0x22d6f961, 0x9a6a9e04, 0x07bda6bd, 0xbf01c1d8, 0xadb46e36, + 0x15080953, 0x1d724e9a, 0xa5ce29ff, 0xb77b8611, 0x0fc7e174, + 0x9210d9cd, 0x2aacbea8, 0x38191146, 0x80a57623, 0xd8c66675, + 0x607a0110, 0x72cfaefe, 0xca73c99b, 0x57a4f122, 0xef189647, + 0xfdad39a9, 0x45115ecc, 0x764dee06, 0xcef18963, 0xdc44268d, + 0x64f841e8, 0xf92f7951, 0x41931e34, 0x5326b1da, 0xeb9ad6bf, + 0xb3f9c6e9, 0x0b45a18c, 0x19f00e62, 0xa14c6907, 0x3c9b51be, + 0x842736db, 0x96929935, 0x2e2efe50, 0x2654b999, 0x9ee8defc, + 0x8c5d7112, 0x34e11677, 0xa9362ece, 0x118a49ab, 0x033fe645, + 0xbb838120, 0xe3e09176, 0x5b5cf613, 0x49e959fd, 0xf1553e98, + 0x6c820621, 0xd43e6144, 0xc68bceaa, 0x7e37a9cf, 0xd67f4138, + 0x6ec3265d, 0x7c7689b3, 0xc4caeed6, 0x591dd66f, 0xe1a1b10a, + 0xf3141ee4, 0x4ba87981, 0x13cb69d7, 0xab770eb2, 0xb9c2a15c, + 0x017ec639, 0x9ca9fe80, 0x241599e5, 0x36a0360b, 0x8e1c516e, + 0x866616a7, 0x3eda71c2, 0x2c6fde2c, 0x94d3b949, 0x090481f0, + 0xb1b8e695, 0xa30d497b, 0x1bb12e1e, 0x43d23e48, 0xfb6e592d, + 0xe9dbf6c3, 0x516791a6, 0xccb0a91f, 0x740cce7a, 0x66b96194, + 0xde0506f1}, + {0x00000000, 0x01c26a37, 0x0384d46e, 0x0246be59, 0x0709a8dc, + 0x06cbc2eb, 0x048d7cb2, 0x054f1685, 0x0e1351b8, 0x0fd13b8f, + 0x0d9785d6, 0x0c55efe1, 0x091af964, 0x08d89353, 0x0a9e2d0a, + 0x0b5c473d, 0x1c26a370, 0x1de4c947, 0x1fa2771e, 0x1e601d29, + 0x1b2f0bac, 0x1aed619b, 0x18abdfc2, 0x1969b5f5, 0x1235f2c8, + 0x13f798ff, 0x11b126a6, 0x10734c91, 0x153c5a14, 0x14fe3023, + 0x16b88e7a, 0x177ae44d, 0x384d46e0, 0x398f2cd7, 0x3bc9928e, + 0x3a0bf8b9, 0x3f44ee3c, 0x3e86840b, 0x3cc03a52, 0x3d025065, + 0x365e1758, 0x379c7d6f, 0x35dac336, 0x3418a901, 0x3157bf84, + 0x3095d5b3, 0x32d36bea, 0x331101dd, 0x246be590, 0x25a98fa7, + 0x27ef31fe, 0x262d5bc9, 0x23624d4c, 0x22a0277b, 0x20e69922, + 0x2124f315, 0x2a78b428, 0x2bbade1f, 0x29fc6046, 0x283e0a71, + 0x2d711cf4, 0x2cb376c3, 0x2ef5c89a, 0x2f37a2ad, 0x709a8dc0, + 0x7158e7f7, 0x731e59ae, 0x72dc3399, 0x7793251c, 0x76514f2b, + 0x7417f172, 0x75d59b45, 0x7e89dc78, 0x7f4bb64f, 0x7d0d0816, + 0x7ccf6221, 0x798074a4, 0x78421e93, 0x7a04a0ca, 0x7bc6cafd, + 0x6cbc2eb0, 0x6d7e4487, 0x6f38fade, 0x6efa90e9, 0x6bb5866c, + 0x6a77ec5b, 0x68315202, 0x69f33835, 0x62af7f08, 0x636d153f, + 0x612bab66, 0x60e9c151, 0x65a6d7d4, 0x6464bde3, 0x662203ba, + 0x67e0698d, 0x48d7cb20, 0x4915a117, 0x4b531f4e, 0x4a917579, + 0x4fde63fc, 0x4e1c09cb, 0x4c5ab792, 0x4d98dda5, 0x46c49a98, + 0x4706f0af, 0x45404ef6, 0x448224c1, 0x41cd3244, 0x400f5873, + 0x4249e62a, 0x438b8c1d, 0x54f16850, 0x55330267, 0x5775bc3e, + 0x56b7d609, 0x53f8c08c, 0x523aaabb, 0x507c14e2, 0x51be7ed5, + 0x5ae239e8, 0x5b2053df, 0x5966ed86, 0x58a487b1, 0x5deb9134, + 0x5c29fb03, 0x5e6f455a, 0x5fad2f6d, 0xe1351b80, 0xe0f771b7, + 0xe2b1cfee, 0xe373a5d9, 0xe63cb35c, 0xe7fed96b, 0xe5b86732, + 0xe47a0d05, 0xef264a38, 0xeee4200f, 0xeca29e56, 0xed60f461, + 0xe82fe2e4, 0xe9ed88d3, 0xebab368a, 0xea695cbd, 0xfd13b8f0, + 0xfcd1d2c7, 0xfe976c9e, 0xff5506a9, 0xfa1a102c, 0xfbd87a1b, + 0xf99ec442, 0xf85cae75, 0xf300e948, 0xf2c2837f, 0xf0843d26, + 0xf1465711, 0xf4094194, 0xf5cb2ba3, 0xf78d95fa, 0xf64fffcd, + 0xd9785d60, 0xd8ba3757, 0xdafc890e, 0xdb3ee339, 0xde71f5bc, + 0xdfb39f8b, 0xddf521d2, 0xdc374be5, 0xd76b0cd8, 0xd6a966ef, + 0xd4efd8b6, 0xd52db281, 0xd062a404, 0xd1a0ce33, 0xd3e6706a, + 0xd2241a5d, 0xc55efe10, 0xc49c9427, 0xc6da2a7e, 0xc7184049, + 0xc25756cc, 0xc3953cfb, 0xc1d382a2, 0xc011e895, 0xcb4dafa8, + 0xca8fc59f, 0xc8c97bc6, 0xc90b11f1, 0xcc440774, 0xcd866d43, + 0xcfc0d31a, 0xce02b92d, 0x91af9640, 0x906dfc77, 0x922b422e, + 0x93e92819, 0x96a63e9c, 0x976454ab, 0x9522eaf2, 0x94e080c5, + 0x9fbcc7f8, 0x9e7eadcf, 0x9c381396, 0x9dfa79a1, 0x98b56f24, + 0x99770513, 0x9b31bb4a, 0x9af3d17d, 0x8d893530, 0x8c4b5f07, + 0x8e0de15e, 0x8fcf8b69, 0x8a809dec, 0x8b42f7db, 0x89044982, + 0x88c623b5, 0x839a6488, 0x82580ebf, 0x801eb0e6, 0x81dcdad1, + 0x8493cc54, 0x8551a663, 0x8717183a, 0x86d5720d, 0xa9e2d0a0, + 0xa820ba97, 0xaa6604ce, 0xaba46ef9, 0xaeeb787c, 0xaf29124b, + 0xad6fac12, 0xacadc625, 0xa7f18118, 0xa633eb2f, 0xa4755576, + 0xa5b73f41, 0xa0f829c4, 0xa13a43f3, 0xa37cfdaa, 0xa2be979d, + 0xb5c473d0, 0xb40619e7, 0xb640a7be, 0xb782cd89, 0xb2cddb0c, + 0xb30fb13b, 0xb1490f62, 0xb08b6555, 0xbbd72268, 0xba15485f, + 0xb853f606, 0xb9919c31, 0xbcde8ab4, 0xbd1ce083, 0xbf5a5eda, + 0xbe9834ed}, + {0x00000000, 0x191b3141, 0x32366282, 0x2b2d53c3, 0x646cc504, + 0x7d77f445, 0x565aa786, 0x4f4196c7, 0xc8d98a08, 0xd1c2bb49, + 0xfaefe88a, 0xe3f4d9cb, 0xacb54f0c, 0xb5ae7e4d, 0x9e832d8e, + 0x87981ccf, 0x4ac21251, 0x53d92310, 0x78f470d3, 0x61ef4192, + 0x2eaed755, 0x37b5e614, 0x1c98b5d7, 0x05838496, 0x821b9859, + 0x9b00a918, 0xb02dfadb, 0xa936cb9a, 0xe6775d5d, 0xff6c6c1c, + 0xd4413fdf, 0xcd5a0e9e, 0x958424a2, 0x8c9f15e3, 0xa7b24620, + 0xbea97761, 0xf1e8e1a6, 0xe8f3d0e7, 0xc3de8324, 0xdac5b265, + 0x5d5daeaa, 0x44469feb, 0x6f6bcc28, 0x7670fd69, 0x39316bae, + 0x202a5aef, 0x0b07092c, 0x121c386d, 0xdf4636f3, 0xc65d07b2, + 0xed705471, 0xf46b6530, 0xbb2af3f7, 0xa231c2b6, 0x891c9175, + 0x9007a034, 0x179fbcfb, 0x0e848dba, 0x25a9de79, 0x3cb2ef38, + 0x73f379ff, 0x6ae848be, 0x41c51b7d, 0x58de2a3c, 0xf0794f05, + 0xe9627e44, 0xc24f2d87, 0xdb541cc6, 0x94158a01, 0x8d0ebb40, + 0xa623e883, 0xbf38d9c2, 0x38a0c50d, 0x21bbf44c, 0x0a96a78f, + 0x138d96ce, 0x5ccc0009, 0x45d73148, 0x6efa628b, 0x77e153ca, + 0xbabb5d54, 0xa3a06c15, 0x888d3fd6, 0x91960e97, 0xded79850, + 0xc7cca911, 0xece1fad2, 0xf5facb93, 0x7262d75c, 0x6b79e61d, + 0x4054b5de, 0x594f849f, 0x160e1258, 0x0f152319, 0x243870da, + 0x3d23419b, 0x65fd6ba7, 0x7ce65ae6, 0x57cb0925, 0x4ed03864, + 0x0191aea3, 0x188a9fe2, 0x33a7cc21, 0x2abcfd60, 0xad24e1af, + 0xb43fd0ee, 0x9f12832d, 0x8609b26c, 0xc94824ab, 0xd05315ea, + 0xfb7e4629, 0xe2657768, 0x2f3f79f6, 0x362448b7, 0x1d091b74, + 0x04122a35, 0x4b53bcf2, 0x52488db3, 0x7965de70, 0x607eef31, + 0xe7e6f3fe, 0xfefdc2bf, 0xd5d0917c, 0xcccba03d, 0x838a36fa, + 0x9a9107bb, 0xb1bc5478, 0xa8a76539, 0x3b83984b, 0x2298a90a, + 0x09b5fac9, 0x10aecb88, 0x5fef5d4f, 0x46f46c0e, 0x6dd93fcd, + 0x74c20e8c, 0xf35a1243, 0xea412302, 0xc16c70c1, 0xd8774180, + 0x9736d747, 0x8e2de606, 0xa500b5c5, 0xbc1b8484, 0x71418a1a, + 0x685abb5b, 0x4377e898, 0x5a6cd9d9, 0x152d4f1e, 0x0c367e5f, + 0x271b2d9c, 0x3e001cdd, 0xb9980012, 0xa0833153, 0x8bae6290, + 0x92b553d1, 0xddf4c516, 0xc4eff457, 0xefc2a794, 0xf6d996d5, + 0xae07bce9, 0xb71c8da8, 0x9c31de6b, 0x852aef2a, 0xca6b79ed, + 0xd37048ac, 0xf85d1b6f, 0xe1462a2e, 0x66de36e1, 0x7fc507a0, + 0x54e85463, 0x4df36522, 0x02b2f3e5, 0x1ba9c2a4, 0x30849167, + 0x299fa026, 0xe4c5aeb8, 0xfdde9ff9, 0xd6f3cc3a, 0xcfe8fd7b, + 0x80a96bbc, 0x99b25afd, 0xb29f093e, 0xab84387f, 0x2c1c24b0, + 0x350715f1, 0x1e2a4632, 0x07317773, 0x4870e1b4, 0x516bd0f5, + 0x7a468336, 0x635db277, 0xcbfad74e, 0xd2e1e60f, 0xf9ccb5cc, + 0xe0d7848d, 0xaf96124a, 0xb68d230b, 0x9da070c8, 0x84bb4189, + 0x03235d46, 0x1a386c07, 0x31153fc4, 0x280e0e85, 0x674f9842, + 0x7e54a903, 0x5579fac0, 0x4c62cb81, 0x8138c51f, 0x9823f45e, + 0xb30ea79d, 0xaa1596dc, 0xe554001b, 0xfc4f315a, 0xd7626299, + 0xce7953d8, 0x49e14f17, 0x50fa7e56, 0x7bd72d95, 0x62cc1cd4, + 0x2d8d8a13, 0x3496bb52, 0x1fbbe891, 0x06a0d9d0, 0x5e7ef3ec, + 0x4765c2ad, 0x6c48916e, 0x7553a02f, 0x3a1236e8, 0x230907a9, + 0x0824546a, 0x113f652b, 0x96a779e4, 0x8fbc48a5, 0xa4911b66, + 0xbd8a2a27, 0xf2cbbce0, 0xebd08da1, 0xc0fdde62, 0xd9e6ef23, + 0x14bce1bd, 0x0da7d0fc, 0x268a833f, 0x3f91b27e, 0x70d024b9, + 0x69cb15f8, 0x42e6463b, 0x5bfd777a, 0xdc656bb5, 0xc57e5af4, + 0xee530937, 0xf7483876, 0xb809aeb1, 0xa1129ff0, 0x8a3fcc33, + 0x9324fd72}, + {0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, + 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, + 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, + 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, + 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, + 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, + 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, + 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, + 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, + 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, + 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, + 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, + 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, + 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, + 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, + 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, + 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, + 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, + 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, + 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, + 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, + 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, + 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, + 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, + 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, + 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, + 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, + 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, + 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, + 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, + 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, + 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, + 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, + 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, + 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, + 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, + 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, + 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, + 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, + 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, + 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, + 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, + 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, + 0x2d02ef8d}}; + +local const z_word_t FAR crc_braid_big_table[][256] = { + {0x00000000, 0x96300777, 0x2c610eee, 0xba510999, 0x19c46d07, + 0x8ff46a70, 0x35a563e9, 0xa395649e, 0x3288db0e, 0xa4b8dc79, + 0x1ee9d5e0, 0x88d9d297, 0x2b4cb609, 0xbd7cb17e, 0x072db8e7, + 0x911dbf90, 0x6410b71d, 0xf220b06a, 0x4871b9f3, 0xde41be84, + 0x7dd4da1a, 0xebe4dd6d, 0x51b5d4f4, 0xc785d383, 0x56986c13, + 0xc0a86b64, 0x7af962fd, 0xecc9658a, 0x4f5c0114, 0xd96c0663, + 0x633d0ffa, 0xf50d088d, 0xc8206e3b, 0x5e10694c, 0xe44160d5, + 0x727167a2, 0xd1e4033c, 0x47d4044b, 0xfd850dd2, 0x6bb50aa5, + 0xfaa8b535, 0x6c98b242, 0xd6c9bbdb, 0x40f9bcac, 0xe36cd832, + 0x755cdf45, 0xcf0dd6dc, 0x593dd1ab, 0xac30d926, 0x3a00de51, + 0x8051d7c8, 0x1661d0bf, 0xb5f4b421, 0x23c4b356, 0x9995bacf, + 0x0fa5bdb8, 0x9eb80228, 0x0888055f, 0xb2d90cc6, 0x24e90bb1, + 0x877c6f2f, 0x114c6858, 0xab1d61c1, 0x3d2d66b6, 0x9041dc76, + 0x0671db01, 0xbc20d298, 0x2a10d5ef, 0x8985b171, 0x1fb5b606, + 0xa5e4bf9f, 0x33d4b8e8, 0xa2c90778, 0x34f9000f, 0x8ea80996, + 0x18980ee1, 0xbb0d6a7f, 0x2d3d6d08, 0x976c6491, 0x015c63e6, + 0xf4516b6b, 0x62616c1c, 0xd8306585, 0x4e0062f2, 0xed95066c, + 0x7ba5011b, 0xc1f40882, 0x57c40ff5, 0xc6d9b065, 0x50e9b712, + 0xeab8be8b, 0x7c88b9fc, 0xdf1ddd62, 0x492dda15, 0xf37cd38c, + 0x654cd4fb, 0x5861b24d, 0xce51b53a, 0x7400bca3, 0xe230bbd4, + 0x41a5df4a, 0xd795d83d, 0x6dc4d1a4, 0xfbf4d6d3, 0x6ae96943, + 0xfcd96e34, 0x468867ad, 0xd0b860da, 0x732d0444, 0xe51d0333, + 0x5f4c0aaa, 0xc97c0ddd, 0x3c710550, 0xaa410227, 0x10100bbe, + 0x86200cc9, 0x25b56857, 0xb3856f20, 0x09d466b9, 0x9fe461ce, + 0x0ef9de5e, 0x98c9d929, 0x2298d0b0, 0xb4a8d7c7, 0x173db359, + 0x810db42e, 0x3b5cbdb7, 0xad6cbac0, 0x2083b8ed, 0xb6b3bf9a, + 0x0ce2b603, 0x9ad2b174, 0x3947d5ea, 0xaf77d29d, 0x1526db04, + 0x8316dc73, 0x120b63e3, 0x843b6494, 0x3e6a6d0d, 0xa85a6a7a, + 0x0bcf0ee4, 0x9dff0993, 0x27ae000a, 0xb19e077d, 0x44930ff0, + 0xd2a30887, 0x68f2011e, 0xfec20669, 0x5d5762f7, 0xcb676580, + 0x71366c19, 0xe7066b6e, 0x761bd4fe, 0xe02bd389, 0x5a7ada10, + 0xcc4add67, 0x6fdfb9f9, 0xf9efbe8e, 0x43beb717, 0xd58eb060, + 0xe8a3d6d6, 0x7e93d1a1, 0xc4c2d838, 0x52f2df4f, 0xf167bbd1, + 0x6757bca6, 0xdd06b53f, 0x4b36b248, 0xda2b0dd8, 0x4c1b0aaf, + 0xf64a0336, 0x607a0441, 0xc3ef60df, 0x55df67a8, 0xef8e6e31, + 0x79be6946, 0x8cb361cb, 0x1a8366bc, 0xa0d26f25, 0x36e26852, + 0x95770ccc, 0x03470bbb, 0xb9160222, 0x2f260555, 0xbe3bbac5, + 0x280bbdb2, 0x925ab42b, 0x046ab35c, 0xa7ffd7c2, 0x31cfd0b5, + 0x8b9ed92c, 0x1daede5b, 0xb0c2649b, 0x26f263ec, 0x9ca36a75, + 0x0a936d02, 0xa906099c, 0x3f360eeb, 0x85670772, 0x13570005, + 0x824abf95, 0x147ab8e2, 0xae2bb17b, 0x381bb60c, 0x9b8ed292, + 0x0dbed5e5, 0xb7efdc7c, 0x21dfdb0b, 0xd4d2d386, 0x42e2d4f1, + 0xf8b3dd68, 0x6e83da1f, 0xcd16be81, 0x5b26b9f6, 0xe177b06f, + 0x7747b718, 0xe65a0888, 0x706a0fff, 0xca3b0666, 0x5c0b0111, + 0xff9e658f, 0x69ae62f8, 0xd3ff6b61, 0x45cf6c16, 0x78e20aa0, + 0xeed20dd7, 0x5483044e, 0xc2b30339, 0x612667a7, 0xf71660d0, + 0x4d476949, 0xdb776e3e, 0x4a6ad1ae, 0xdc5ad6d9, 0x660bdf40, + 0xf03bd837, 0x53aebca9, 0xc59ebbde, 0x7fcfb247, 0xe9ffb530, + 0x1cf2bdbd, 0x8ac2baca, 0x3093b353, 0xa6a3b424, 0x0536d0ba, + 0x9306d7cd, 0x2957de54, 0xbf67d923, 0x2e7a66b3, 0xb84a61c4, + 0x021b685d, 0x942b6f2a, 0x37be0bb4, 0xa18e0cc3, 0x1bdf055a, + 0x8def022d}, + {0x00000000, 0x41311b19, 0x82623632, 0xc3532d2b, 0x04c56c64, + 0x45f4777d, 0x86a75a56, 0xc796414f, 0x088ad9c8, 0x49bbc2d1, + 0x8ae8effa, 0xcbd9f4e3, 0x0c4fb5ac, 0x4d7eaeb5, 0x8e2d839e, + 0xcf1c9887, 0x5112c24a, 0x1023d953, 0xd370f478, 0x9241ef61, + 0x55d7ae2e, 0x14e6b537, 0xd7b5981c, 0x96848305, 0x59981b82, + 0x18a9009b, 0xdbfa2db0, 0x9acb36a9, 0x5d5d77e6, 0x1c6c6cff, + 0xdf3f41d4, 0x9e0e5acd, 0xa2248495, 0xe3159f8c, 0x2046b2a7, + 0x6177a9be, 0xa6e1e8f1, 0xe7d0f3e8, 0x2483dec3, 0x65b2c5da, + 0xaaae5d5d, 0xeb9f4644, 0x28cc6b6f, 0x69fd7076, 0xae6b3139, + 0xef5a2a20, 0x2c09070b, 0x6d381c12, 0xf33646df, 0xb2075dc6, + 0x715470ed, 0x30656bf4, 0xf7f32abb, 0xb6c231a2, 0x75911c89, + 0x34a00790, 0xfbbc9f17, 0xba8d840e, 0x79dea925, 0x38efb23c, + 0xff79f373, 0xbe48e86a, 0x7d1bc541, 0x3c2ade58, 0x054f79f0, + 0x447e62e9, 0x872d4fc2, 0xc61c54db, 0x018a1594, 0x40bb0e8d, + 0x83e823a6, 0xc2d938bf, 0x0dc5a038, 0x4cf4bb21, 0x8fa7960a, + 0xce968d13, 0x0900cc5c, 0x4831d745, 0x8b62fa6e, 0xca53e177, + 0x545dbbba, 0x156ca0a3, 0xd63f8d88, 0x970e9691, 0x5098d7de, + 0x11a9ccc7, 0xd2fae1ec, 0x93cbfaf5, 0x5cd76272, 0x1de6796b, + 0xdeb55440, 0x9f844f59, 0x58120e16, 0x1923150f, 0xda703824, + 0x9b41233d, 0xa76bfd65, 0xe65ae67c, 0x2509cb57, 0x6438d04e, + 0xa3ae9101, 0xe29f8a18, 0x21cca733, 0x60fdbc2a, 0xafe124ad, + 0xeed03fb4, 0x2d83129f, 0x6cb20986, 0xab2448c9, 0xea1553d0, + 0x29467efb, 0x687765e2, 0xf6793f2f, 0xb7482436, 0x741b091d, + 0x352a1204, 0xf2bc534b, 0xb38d4852, 0x70de6579, 0x31ef7e60, + 0xfef3e6e7, 0xbfc2fdfe, 0x7c91d0d5, 0x3da0cbcc, 0xfa368a83, + 0xbb07919a, 0x7854bcb1, 0x3965a7a8, 0x4b98833b, 0x0aa99822, + 0xc9fab509, 0x88cbae10, 0x4f5def5f, 0x0e6cf446, 0xcd3fd96d, + 0x8c0ec274, 0x43125af3, 0x022341ea, 0xc1706cc1, 0x804177d8, + 0x47d73697, 0x06e62d8e, 0xc5b500a5, 0x84841bbc, 0x1a8a4171, + 0x5bbb5a68, 0x98e87743, 0xd9d96c5a, 0x1e4f2d15, 0x5f7e360c, + 0x9c2d1b27, 0xdd1c003e, 0x120098b9, 0x533183a0, 0x9062ae8b, + 0xd153b592, 0x16c5f4dd, 0x57f4efc4, 0x94a7c2ef, 0xd596d9f6, + 0xe9bc07ae, 0xa88d1cb7, 0x6bde319c, 0x2aef2a85, 0xed796bca, + 0xac4870d3, 0x6f1b5df8, 0x2e2a46e1, 0xe136de66, 0xa007c57f, + 0x6354e854, 0x2265f34d, 0xe5f3b202, 0xa4c2a91b, 0x67918430, + 0x26a09f29, 0xb8aec5e4, 0xf99fdefd, 0x3accf3d6, 0x7bfde8cf, + 0xbc6ba980, 0xfd5ab299, 0x3e099fb2, 0x7f3884ab, 0xb0241c2c, + 0xf1150735, 0x32462a1e, 0x73773107, 0xb4e17048, 0xf5d06b51, + 0x3683467a, 0x77b25d63, 0x4ed7facb, 0x0fe6e1d2, 0xccb5ccf9, + 0x8d84d7e0, 0x4a1296af, 0x0b238db6, 0xc870a09d, 0x8941bb84, + 0x465d2303, 0x076c381a, 0xc43f1531, 0x850e0e28, 0x42984f67, + 0x03a9547e, 0xc0fa7955, 0x81cb624c, 0x1fc53881, 0x5ef42398, + 0x9da70eb3, 0xdc9615aa, 0x1b0054e5, 0x5a314ffc, 0x996262d7, + 0xd85379ce, 0x174fe149, 0x567efa50, 0x952dd77b, 0xd41ccc62, + 0x138a8d2d, 0x52bb9634, 0x91e8bb1f, 0xd0d9a006, 0xecf37e5e, + 0xadc26547, 0x6e91486c, 0x2fa05375, 0xe836123a, 0xa9070923, + 0x6a542408, 0x2b653f11, 0xe479a796, 0xa548bc8f, 0x661b91a4, + 0x272a8abd, 0xe0bccbf2, 0xa18dd0eb, 0x62defdc0, 0x23efe6d9, + 0xbde1bc14, 0xfcd0a70d, 0x3f838a26, 0x7eb2913f, 0xb924d070, + 0xf815cb69, 0x3b46e642, 0x7a77fd5b, 0xb56b65dc, 0xf45a7ec5, + 0x370953ee, 0x763848f7, 0xb1ae09b8, 0xf09f12a1, 0x33cc3f8a, + 0x72fd2493}, + {0x00000000, 0x376ac201, 0x6ed48403, 0x59be4602, 0xdca80907, + 0xebc2cb06, 0xb27c8d04, 0x85164f05, 0xb851130e, 0x8f3bd10f, + 0xd685970d, 0xe1ef550c, 0x64f91a09, 0x5393d808, 0x0a2d9e0a, + 0x3d475c0b, 0x70a3261c, 0x47c9e41d, 0x1e77a21f, 0x291d601e, + 0xac0b2f1b, 0x9b61ed1a, 0xc2dfab18, 0xf5b56919, 0xc8f23512, + 0xff98f713, 0xa626b111, 0x914c7310, 0x145a3c15, 0x2330fe14, + 0x7a8eb816, 0x4de47a17, 0xe0464d38, 0xd72c8f39, 0x8e92c93b, + 0xb9f80b3a, 0x3cee443f, 0x0b84863e, 0x523ac03c, 0x6550023d, + 0x58175e36, 0x6f7d9c37, 0x36c3da35, 0x01a91834, 0x84bf5731, + 0xb3d59530, 0xea6bd332, 0xdd011133, 0x90e56b24, 0xa78fa925, + 0xfe31ef27, 0xc95b2d26, 0x4c4d6223, 0x7b27a022, 0x2299e620, + 0x15f32421, 0x28b4782a, 0x1fdeba2b, 0x4660fc29, 0x710a3e28, + 0xf41c712d, 0xc376b32c, 0x9ac8f52e, 0xada2372f, 0xc08d9a70, + 0xf7e75871, 0xae591e73, 0x9933dc72, 0x1c259377, 0x2b4f5176, + 0x72f11774, 0x459bd575, 0x78dc897e, 0x4fb64b7f, 0x16080d7d, + 0x2162cf7c, 0xa4748079, 0x931e4278, 0xcaa0047a, 0xfdcac67b, + 0xb02ebc6c, 0x87447e6d, 0xdefa386f, 0xe990fa6e, 0x6c86b56b, + 0x5bec776a, 0x02523168, 0x3538f369, 0x087faf62, 0x3f156d63, + 0x66ab2b61, 0x51c1e960, 0xd4d7a665, 0xe3bd6464, 0xba032266, + 0x8d69e067, 0x20cbd748, 0x17a11549, 0x4e1f534b, 0x7975914a, + 0xfc63de4f, 0xcb091c4e, 0x92b75a4c, 0xa5dd984d, 0x989ac446, + 0xaff00647, 0xf64e4045, 0xc1248244, 0x4432cd41, 0x73580f40, + 0x2ae64942, 0x1d8c8b43, 0x5068f154, 0x67023355, 0x3ebc7557, + 0x09d6b756, 0x8cc0f853, 0xbbaa3a52, 0xe2147c50, 0xd57ebe51, + 0xe839e25a, 0xdf53205b, 0x86ed6659, 0xb187a458, 0x3491eb5d, + 0x03fb295c, 0x5a456f5e, 0x6d2fad5f, 0x801b35e1, 0xb771f7e0, + 0xeecfb1e2, 0xd9a573e3, 0x5cb33ce6, 0x6bd9fee7, 0x3267b8e5, + 0x050d7ae4, 0x384a26ef, 0x0f20e4ee, 0x569ea2ec, 0x61f460ed, + 0xe4e22fe8, 0xd388ede9, 0x8a36abeb, 0xbd5c69ea, 0xf0b813fd, + 0xc7d2d1fc, 0x9e6c97fe, 0xa90655ff, 0x2c101afa, 0x1b7ad8fb, + 0x42c49ef9, 0x75ae5cf8, 0x48e900f3, 0x7f83c2f2, 0x263d84f0, + 0x115746f1, 0x944109f4, 0xa32bcbf5, 0xfa958df7, 0xcdff4ff6, + 0x605d78d9, 0x5737bad8, 0x0e89fcda, 0x39e33edb, 0xbcf571de, + 0x8b9fb3df, 0xd221f5dd, 0xe54b37dc, 0xd80c6bd7, 0xef66a9d6, + 0xb6d8efd4, 0x81b22dd5, 0x04a462d0, 0x33cea0d1, 0x6a70e6d3, + 0x5d1a24d2, 0x10fe5ec5, 0x27949cc4, 0x7e2adac6, 0x494018c7, + 0xcc5657c2, 0xfb3c95c3, 0xa282d3c1, 0x95e811c0, 0xa8af4dcb, + 0x9fc58fca, 0xc67bc9c8, 0xf1110bc9, 0x740744cc, 0x436d86cd, + 0x1ad3c0cf, 0x2db902ce, 0x4096af91, 0x77fc6d90, 0x2e422b92, + 0x1928e993, 0x9c3ea696, 0xab546497, 0xf2ea2295, 0xc580e094, + 0xf8c7bc9f, 0xcfad7e9e, 0x9613389c, 0xa179fa9d, 0x246fb598, + 0x13057799, 0x4abb319b, 0x7dd1f39a, 0x3035898d, 0x075f4b8c, + 0x5ee10d8e, 0x698bcf8f, 0xec9d808a, 0xdbf7428b, 0x82490489, + 0xb523c688, 0x88649a83, 0xbf0e5882, 0xe6b01e80, 0xd1dadc81, + 0x54cc9384, 0x63a65185, 0x3a181787, 0x0d72d586, 0xa0d0e2a9, + 0x97ba20a8, 0xce0466aa, 0xf96ea4ab, 0x7c78ebae, 0x4b1229af, + 0x12ac6fad, 0x25c6adac, 0x1881f1a7, 0x2feb33a6, 0x765575a4, + 0x413fb7a5, 0xc429f8a0, 0xf3433aa1, 0xaafd7ca3, 0x9d97bea2, + 0xd073c4b5, 0xe71906b4, 0xbea740b6, 0x89cd82b7, 0x0cdbcdb2, + 0x3bb10fb3, 0x620f49b1, 0x55658bb0, 0x6822d7bb, 0x5f4815ba, + 0x06f653b8, 0x319c91b9, 0xb48adebc, 0x83e01cbd, 0xda5e5abf, + 0xed3498be}, + {0x00000000, 0x6567bcb8, 0x8bc809aa, 0xeeafb512, 0x5797628f, + 0x32f0de37, 0xdc5f6b25, 0xb938d79d, 0xef28b4c5, 0x8a4f087d, + 0x64e0bd6f, 0x018701d7, 0xb8bfd64a, 0xddd86af2, 0x3377dfe0, + 0x56106358, 0x9f571950, 0xfa30a5e8, 0x149f10fa, 0x71f8ac42, + 0xc8c07bdf, 0xada7c767, 0x43087275, 0x266fcecd, 0x707fad95, + 0x1518112d, 0xfbb7a43f, 0x9ed01887, 0x27e8cf1a, 0x428f73a2, + 0xac20c6b0, 0xc9477a08, 0x3eaf32a0, 0x5bc88e18, 0xb5673b0a, + 0xd00087b2, 0x6938502f, 0x0c5fec97, 0xe2f05985, 0x8797e53d, + 0xd1878665, 0xb4e03add, 0x5a4f8fcf, 0x3f283377, 0x8610e4ea, + 0xe3775852, 0x0dd8ed40, 0x68bf51f8, 0xa1f82bf0, 0xc49f9748, + 0x2a30225a, 0x4f579ee2, 0xf66f497f, 0x9308f5c7, 0x7da740d5, + 0x18c0fc6d, 0x4ed09f35, 0x2bb7238d, 0xc518969f, 0xa07f2a27, + 0x1947fdba, 0x7c204102, 0x928ff410, 0xf7e848a8, 0x3d58149b, + 0x583fa823, 0xb6901d31, 0xd3f7a189, 0x6acf7614, 0x0fa8caac, + 0xe1077fbe, 0x8460c306, 0xd270a05e, 0xb7171ce6, 0x59b8a9f4, + 0x3cdf154c, 0x85e7c2d1, 0xe0807e69, 0x0e2fcb7b, 0x6b4877c3, + 0xa20f0dcb, 0xc768b173, 0x29c70461, 0x4ca0b8d9, 0xf5986f44, + 0x90ffd3fc, 0x7e5066ee, 0x1b37da56, 0x4d27b90e, 0x284005b6, + 0xc6efb0a4, 0xa3880c1c, 0x1ab0db81, 0x7fd76739, 0x9178d22b, + 0xf41f6e93, 0x03f7263b, 0x66909a83, 0x883f2f91, 0xed589329, + 0x546044b4, 0x3107f80c, 0xdfa84d1e, 0xbacff1a6, 0xecdf92fe, + 0x89b82e46, 0x67179b54, 0x027027ec, 0xbb48f071, 0xde2f4cc9, + 0x3080f9db, 0x55e74563, 0x9ca03f6b, 0xf9c783d3, 0x176836c1, + 0x720f8a79, 0xcb375de4, 0xae50e15c, 0x40ff544e, 0x2598e8f6, + 0x73888bae, 0x16ef3716, 0xf8408204, 0x9d273ebc, 0x241fe921, + 0x41785599, 0xafd7e08b, 0xcab05c33, 0x3bb659ed, 0x5ed1e555, + 0xb07e5047, 0xd519ecff, 0x6c213b62, 0x094687da, 0xe7e932c8, + 0x828e8e70, 0xd49eed28, 0xb1f95190, 0x5f56e482, 0x3a31583a, + 0x83098fa7, 0xe66e331f, 0x08c1860d, 0x6da63ab5, 0xa4e140bd, + 0xc186fc05, 0x2f294917, 0x4a4ef5af, 0xf3762232, 0x96119e8a, + 0x78be2b98, 0x1dd99720, 0x4bc9f478, 0x2eae48c0, 0xc001fdd2, + 0xa566416a, 0x1c5e96f7, 0x79392a4f, 0x97969f5d, 0xf2f123e5, + 0x05196b4d, 0x607ed7f5, 0x8ed162e7, 0xebb6de5f, 0x528e09c2, + 0x37e9b57a, 0xd9460068, 0xbc21bcd0, 0xea31df88, 0x8f566330, + 0x61f9d622, 0x049e6a9a, 0xbda6bd07, 0xd8c101bf, 0x366eb4ad, + 0x53090815, 0x9a4e721d, 0xff29cea5, 0x11867bb7, 0x74e1c70f, + 0xcdd91092, 0xa8beac2a, 0x46111938, 0x2376a580, 0x7566c6d8, + 0x10017a60, 0xfeaecf72, 0x9bc973ca, 0x22f1a457, 0x479618ef, + 0xa939adfd, 0xcc5e1145, 0x06ee4d76, 0x6389f1ce, 0x8d2644dc, + 0xe841f864, 0x51792ff9, 0x341e9341, 0xdab12653, 0xbfd69aeb, + 0xe9c6f9b3, 0x8ca1450b, 0x620ef019, 0x07694ca1, 0xbe519b3c, + 0xdb362784, 0x35999296, 0x50fe2e2e, 0x99b95426, 0xfcdee89e, + 0x12715d8c, 0x7716e134, 0xce2e36a9, 0xab498a11, 0x45e63f03, + 0x208183bb, 0x7691e0e3, 0x13f65c5b, 0xfd59e949, 0x983e55f1, + 0x2106826c, 0x44613ed4, 0xaace8bc6, 0xcfa9377e, 0x38417fd6, + 0x5d26c36e, 0xb389767c, 0xd6eecac4, 0x6fd61d59, 0x0ab1a1e1, + 0xe41e14f3, 0x8179a84b, 0xd769cb13, 0xb20e77ab, 0x5ca1c2b9, + 0x39c67e01, 0x80fea99c, 0xe5991524, 0x0b36a036, 0x6e511c8e, + 0xa7166686, 0xc271da3e, 0x2cde6f2c, 0x49b9d394, 0xf0810409, + 0x95e6b8b1, 0x7b490da3, 0x1e2eb11b, 0x483ed243, 0x2d596efb, + 0xc3f6dbe9, 0xa6916751, 0x1fa9b0cc, 0x7ace0c74, 0x9461b966, + 0xf10605de}}; + +#endif + +#endif + +#if N == 2 + +#if W == 8 + +local const z_crc_t FAR crc_braid_table[][256] = { + {0x00000000, 0xae689191, 0x87a02563, 0x29c8b4f2, 0xd4314c87, + 0x7a59dd16, 0x539169e4, 0xfdf9f875, 0x73139f4f, 0xdd7b0ede, + 0xf4b3ba2c, 0x5adb2bbd, 0xa722d3c8, 0x094a4259, 0x2082f6ab, + 0x8eea673a, 0xe6273e9e, 0x484faf0f, 0x61871bfd, 0xcfef8a6c, + 0x32167219, 0x9c7ee388, 0xb5b6577a, 0x1bdec6eb, 0x9534a1d1, + 0x3b5c3040, 0x129484b2, 0xbcfc1523, 0x4105ed56, 0xef6d7cc7, + 0xc6a5c835, 0x68cd59a4, 0x173f7b7d, 0xb957eaec, 0x909f5e1e, + 0x3ef7cf8f, 0xc30e37fa, 0x6d66a66b, 0x44ae1299, 0xeac68308, + 0x642ce432, 0xca4475a3, 0xe38cc151, 0x4de450c0, 0xb01da8b5, + 0x1e753924, 0x37bd8dd6, 0x99d51c47, 0xf11845e3, 0x5f70d472, + 0x76b86080, 0xd8d0f111, 0x25290964, 0x8b4198f5, 0xa2892c07, + 0x0ce1bd96, 0x820bdaac, 0x2c634b3d, 0x05abffcf, 0xabc36e5e, + 0x563a962b, 0xf85207ba, 0xd19ab348, 0x7ff222d9, 0x2e7ef6fa, + 0x8016676b, 0xa9ded399, 0x07b64208, 0xfa4fba7d, 0x54272bec, + 0x7def9f1e, 0xd3870e8f, 0x5d6d69b5, 0xf305f824, 0xdacd4cd6, + 0x74a5dd47, 0x895c2532, 0x2734b4a3, 0x0efc0051, 0xa09491c0, + 0xc859c864, 0x663159f5, 0x4ff9ed07, 0xe1917c96, 0x1c6884e3, + 0xb2001572, 0x9bc8a180, 0x35a03011, 0xbb4a572b, 0x1522c6ba, + 0x3cea7248, 0x9282e3d9, 0x6f7b1bac, 0xc1138a3d, 0xe8db3ecf, + 0x46b3af5e, 0x39418d87, 0x97291c16, 0xbee1a8e4, 0x10893975, + 0xed70c100, 0x43185091, 0x6ad0e463, 0xc4b875f2, 0x4a5212c8, + 0xe43a8359, 0xcdf237ab, 0x639aa63a, 0x9e635e4f, 0x300bcfde, + 0x19c37b2c, 0xb7abeabd, 0xdf66b319, 0x710e2288, 0x58c6967a, + 0xf6ae07eb, 0x0b57ff9e, 0xa53f6e0f, 0x8cf7dafd, 0x229f4b6c, + 0xac752c56, 0x021dbdc7, 0x2bd50935, 0x85bd98a4, 0x784460d1, + 0xd62cf140, 0xffe445b2, 0x518cd423, 0x5cfdedf4, 0xf2957c65, + 0xdb5dc897, 0x75355906, 0x88cca173, 0x26a430e2, 0x0f6c8410, + 0xa1041581, 0x2fee72bb, 0x8186e32a, 0xa84e57d8, 0x0626c649, + 0xfbdf3e3c, 0x55b7afad, 0x7c7f1b5f, 0xd2178ace, 0xbadad36a, + 0x14b242fb, 0x3d7af609, 0x93126798, 0x6eeb9fed, 0xc0830e7c, + 0xe94bba8e, 0x47232b1f, 0xc9c94c25, 0x67a1ddb4, 0x4e696946, + 0xe001f8d7, 0x1df800a2, 0xb3909133, 0x9a5825c1, 0x3430b450, + 0x4bc29689, 0xe5aa0718, 0xcc62b3ea, 0x620a227b, 0x9ff3da0e, + 0x319b4b9f, 0x1853ff6d, 0xb63b6efc, 0x38d109c6, 0x96b99857, + 0xbf712ca5, 0x1119bd34, 0xece04541, 0x4288d4d0, 0x6b406022, + 0xc528f1b3, 0xade5a817, 0x038d3986, 0x2a458d74, 0x842d1ce5, + 0x79d4e490, 0xd7bc7501, 0xfe74c1f3, 0x501c5062, 0xdef63758, + 0x709ea6c9, 0x5956123b, 0xf73e83aa, 0x0ac77bdf, 0xa4afea4e, + 0x8d675ebc, 0x230fcf2d, 0x72831b0e, 0xdceb8a9f, 0xf5233e6d, + 0x5b4baffc, 0xa6b25789, 0x08dac618, 0x211272ea, 0x8f7ae37b, + 0x01908441, 0xaff815d0, 0x8630a122, 0x285830b3, 0xd5a1c8c6, + 0x7bc95957, 0x5201eda5, 0xfc697c34, 0x94a42590, 0x3accb401, + 0x130400f3, 0xbd6c9162, 0x40956917, 0xeefdf886, 0xc7354c74, + 0x695ddde5, 0xe7b7badf, 0x49df2b4e, 0x60179fbc, 0xce7f0e2d, + 0x3386f658, 0x9dee67c9, 0xb426d33b, 0x1a4e42aa, 0x65bc6073, + 0xcbd4f1e2, 0xe21c4510, 0x4c74d481, 0xb18d2cf4, 0x1fe5bd65, + 0x362d0997, 0x98459806, 0x16afff3c, 0xb8c76ead, 0x910fda5f, + 0x3f674bce, 0xc29eb3bb, 0x6cf6222a, 0x453e96d8, 0xeb560749, + 0x839b5eed, 0x2df3cf7c, 0x043b7b8e, 0xaa53ea1f, 0x57aa126a, + 0xf9c283fb, 0xd00a3709, 0x7e62a698, 0xf088c1a2, 0x5ee05033, + 0x7728e4c1, 0xd9407550, 0x24b98d25, 0x8ad11cb4, 0xa319a846, + 0x0d7139d7}, + {0x00000000, 0xb9fbdbe8, 0xa886b191, 0x117d6a79, 0x8a7c6563, + 0x3387be8b, 0x22fad4f2, 0x9b010f1a, 0xcf89cc87, 0x7672176f, + 0x670f7d16, 0xdef4a6fe, 0x45f5a9e4, 0xfc0e720c, 0xed731875, + 0x5488c39d, 0x44629f4f, 0xfd9944a7, 0xece42ede, 0x551ff536, + 0xce1efa2c, 0x77e521c4, 0x66984bbd, 0xdf639055, 0x8beb53c8, + 0x32108820, 0x236de259, 0x9a9639b1, 0x019736ab, 0xb86ced43, + 0xa911873a, 0x10ea5cd2, 0x88c53e9e, 0x313ee576, 0x20438f0f, + 0x99b854e7, 0x02b95bfd, 0xbb428015, 0xaa3fea6c, 0x13c43184, + 0x474cf219, 0xfeb729f1, 0xefca4388, 0x56319860, 0xcd30977a, + 0x74cb4c92, 0x65b626eb, 0xdc4dfd03, 0xcca7a1d1, 0x755c7a39, + 0x64211040, 0xdddacba8, 0x46dbc4b2, 0xff201f5a, 0xee5d7523, + 0x57a6aecb, 0x032e6d56, 0xbad5b6be, 0xaba8dcc7, 0x1253072f, + 0x89520835, 0x30a9d3dd, 0x21d4b9a4, 0x982f624c, 0xcafb7b7d, + 0x7300a095, 0x627dcaec, 0xdb861104, 0x40871e1e, 0xf97cc5f6, + 0xe801af8f, 0x51fa7467, 0x0572b7fa, 0xbc896c12, 0xadf4066b, + 0x140fdd83, 0x8f0ed299, 0x36f50971, 0x27886308, 0x9e73b8e0, + 0x8e99e432, 0x37623fda, 0x261f55a3, 0x9fe48e4b, 0x04e58151, + 0xbd1e5ab9, 0xac6330c0, 0x1598eb28, 0x411028b5, 0xf8ebf35d, + 0xe9969924, 0x506d42cc, 0xcb6c4dd6, 0x7297963e, 0x63eafc47, + 0xda1127af, 0x423e45e3, 0xfbc59e0b, 0xeab8f472, 0x53432f9a, + 0xc8422080, 0x71b9fb68, 0x60c49111, 0xd93f4af9, 0x8db78964, + 0x344c528c, 0x253138f5, 0x9ccae31d, 0x07cbec07, 0xbe3037ef, + 0xaf4d5d96, 0x16b6867e, 0x065cdaac, 0xbfa70144, 0xaeda6b3d, + 0x1721b0d5, 0x8c20bfcf, 0x35db6427, 0x24a60e5e, 0x9d5dd5b6, + 0xc9d5162b, 0x702ecdc3, 0x6153a7ba, 0xd8a87c52, 0x43a97348, + 0xfa52a8a0, 0xeb2fc2d9, 0x52d41931, 0x4e87f0bb, 0xf77c2b53, + 0xe601412a, 0x5ffa9ac2, 0xc4fb95d8, 0x7d004e30, 0x6c7d2449, + 0xd586ffa1, 0x810e3c3c, 0x38f5e7d4, 0x29888dad, 0x90735645, + 0x0b72595f, 0xb28982b7, 0xa3f4e8ce, 0x1a0f3326, 0x0ae56ff4, + 0xb31eb41c, 0xa263de65, 0x1b98058d, 0x80990a97, 0x3962d17f, + 0x281fbb06, 0x91e460ee, 0xc56ca373, 0x7c97789b, 0x6dea12e2, + 0xd411c90a, 0x4f10c610, 0xf6eb1df8, 0xe7967781, 0x5e6dac69, + 0xc642ce25, 0x7fb915cd, 0x6ec47fb4, 0xd73fa45c, 0x4c3eab46, + 0xf5c570ae, 0xe4b81ad7, 0x5d43c13f, 0x09cb02a2, 0xb030d94a, + 0xa14db333, 0x18b668db, 0x83b767c1, 0x3a4cbc29, 0x2b31d650, + 0x92ca0db8, 0x8220516a, 0x3bdb8a82, 0x2aa6e0fb, 0x935d3b13, + 0x085c3409, 0xb1a7efe1, 0xa0da8598, 0x19215e70, 0x4da99ded, + 0xf4524605, 0xe52f2c7c, 0x5cd4f794, 0xc7d5f88e, 0x7e2e2366, + 0x6f53491f, 0xd6a892f7, 0x847c8bc6, 0x3d87502e, 0x2cfa3a57, + 0x9501e1bf, 0x0e00eea5, 0xb7fb354d, 0xa6865f34, 0x1f7d84dc, + 0x4bf54741, 0xf20e9ca9, 0xe373f6d0, 0x5a882d38, 0xc1892222, + 0x7872f9ca, 0x690f93b3, 0xd0f4485b, 0xc01e1489, 0x79e5cf61, + 0x6898a518, 0xd1637ef0, 0x4a6271ea, 0xf399aa02, 0xe2e4c07b, + 0x5b1f1b93, 0x0f97d80e, 0xb66c03e6, 0xa711699f, 0x1eeab277, + 0x85ebbd6d, 0x3c106685, 0x2d6d0cfc, 0x9496d714, 0x0cb9b558, + 0xb5426eb0, 0xa43f04c9, 0x1dc4df21, 0x86c5d03b, 0x3f3e0bd3, + 0x2e4361aa, 0x97b8ba42, 0xc33079df, 0x7acba237, 0x6bb6c84e, + 0xd24d13a6, 0x494c1cbc, 0xf0b7c754, 0xe1caad2d, 0x583176c5, + 0x48db2a17, 0xf120f1ff, 0xe05d9b86, 0x59a6406e, 0xc2a74f74, + 0x7b5c949c, 0x6a21fee5, 0xd3da250d, 0x8752e690, 0x3ea93d78, + 0x2fd45701, 0x962f8ce9, 0x0d2e83f3, 0xb4d5581b, 0xa5a83262, + 0x1c53e98a}, + {0x00000000, 0x9d0fe176, 0xe16ec4ad, 0x7c6125db, 0x19ac8f1b, + 0x84a36e6d, 0xf8c24bb6, 0x65cdaac0, 0x33591e36, 0xae56ff40, + 0xd237da9b, 0x4f383bed, 0x2af5912d, 0xb7fa705b, 0xcb9b5580, + 0x5694b4f6, 0x66b23c6c, 0xfbbddd1a, 0x87dcf8c1, 0x1ad319b7, + 0x7f1eb377, 0xe2115201, 0x9e7077da, 0x037f96ac, 0x55eb225a, + 0xc8e4c32c, 0xb485e6f7, 0x298a0781, 0x4c47ad41, 0xd1484c37, + 0xad2969ec, 0x3026889a, 0xcd6478d8, 0x506b99ae, 0x2c0abc75, + 0xb1055d03, 0xd4c8f7c3, 0x49c716b5, 0x35a6336e, 0xa8a9d218, + 0xfe3d66ee, 0x63328798, 0x1f53a243, 0x825c4335, 0xe791e9f5, + 0x7a9e0883, 0x06ff2d58, 0x9bf0cc2e, 0xabd644b4, 0x36d9a5c2, + 0x4ab88019, 0xd7b7616f, 0xb27acbaf, 0x2f752ad9, 0x53140f02, + 0xce1bee74, 0x988f5a82, 0x0580bbf4, 0x79e19e2f, 0xe4ee7f59, + 0x8123d599, 0x1c2c34ef, 0x604d1134, 0xfd42f042, 0x41b9f7f1, + 0xdcb61687, 0xa0d7335c, 0x3dd8d22a, 0x581578ea, 0xc51a999c, + 0xb97bbc47, 0x24745d31, 0x72e0e9c7, 0xefef08b1, 0x938e2d6a, + 0x0e81cc1c, 0x6b4c66dc, 0xf64387aa, 0x8a22a271, 0x172d4307, + 0x270bcb9d, 0xba042aeb, 0xc6650f30, 0x5b6aee46, 0x3ea74486, + 0xa3a8a5f0, 0xdfc9802b, 0x42c6615d, 0x1452d5ab, 0x895d34dd, + 0xf53c1106, 0x6833f070, 0x0dfe5ab0, 0x90f1bbc6, 0xec909e1d, + 0x719f7f6b, 0x8cdd8f29, 0x11d26e5f, 0x6db34b84, 0xf0bcaaf2, + 0x95710032, 0x087ee144, 0x741fc49f, 0xe91025e9, 0xbf84911f, + 0x228b7069, 0x5eea55b2, 0xc3e5b4c4, 0xa6281e04, 0x3b27ff72, + 0x4746daa9, 0xda493bdf, 0xea6fb345, 0x77605233, 0x0b0177e8, + 0x960e969e, 0xf3c33c5e, 0x6eccdd28, 0x12adf8f3, 0x8fa21985, + 0xd936ad73, 0x44394c05, 0x385869de, 0xa55788a8, 0xc09a2268, + 0x5d95c31e, 0x21f4e6c5, 0xbcfb07b3, 0x8373efe2, 0x1e7c0e94, + 0x621d2b4f, 0xff12ca39, 0x9adf60f9, 0x07d0818f, 0x7bb1a454, + 0xe6be4522, 0xb02af1d4, 0x2d2510a2, 0x51443579, 0xcc4bd40f, + 0xa9867ecf, 0x34899fb9, 0x48e8ba62, 0xd5e75b14, 0xe5c1d38e, + 0x78ce32f8, 0x04af1723, 0x99a0f655, 0xfc6d5c95, 0x6162bde3, + 0x1d039838, 0x800c794e, 0xd698cdb8, 0x4b972cce, 0x37f60915, + 0xaaf9e863, 0xcf3442a3, 0x523ba3d5, 0x2e5a860e, 0xb3556778, + 0x4e17973a, 0xd318764c, 0xaf795397, 0x3276b2e1, 0x57bb1821, + 0xcab4f957, 0xb6d5dc8c, 0x2bda3dfa, 0x7d4e890c, 0xe041687a, + 0x9c204da1, 0x012facd7, 0x64e20617, 0xf9ede761, 0x858cc2ba, + 0x188323cc, 0x28a5ab56, 0xb5aa4a20, 0xc9cb6ffb, 0x54c48e8d, + 0x3109244d, 0xac06c53b, 0xd067e0e0, 0x4d680196, 0x1bfcb560, + 0x86f35416, 0xfa9271cd, 0x679d90bb, 0x02503a7b, 0x9f5fdb0d, + 0xe33efed6, 0x7e311fa0, 0xc2ca1813, 0x5fc5f965, 0x23a4dcbe, + 0xbeab3dc8, 0xdb669708, 0x4669767e, 0x3a0853a5, 0xa707b2d3, + 0xf1930625, 0x6c9ce753, 0x10fdc288, 0x8df223fe, 0xe83f893e, + 0x75306848, 0x09514d93, 0x945eace5, 0xa478247f, 0x3977c509, + 0x4516e0d2, 0xd81901a4, 0xbdd4ab64, 0x20db4a12, 0x5cba6fc9, + 0xc1b58ebf, 0x97213a49, 0x0a2edb3f, 0x764ffee4, 0xeb401f92, + 0x8e8db552, 0x13825424, 0x6fe371ff, 0xf2ec9089, 0x0fae60cb, + 0x92a181bd, 0xeec0a466, 0x73cf4510, 0x1602efd0, 0x8b0d0ea6, + 0xf76c2b7d, 0x6a63ca0b, 0x3cf77efd, 0xa1f89f8b, 0xdd99ba50, + 0x40965b26, 0x255bf1e6, 0xb8541090, 0xc435354b, 0x593ad43d, + 0x691c5ca7, 0xf413bdd1, 0x8872980a, 0x157d797c, 0x70b0d3bc, + 0xedbf32ca, 0x91de1711, 0x0cd1f667, 0x5a454291, 0xc74aa3e7, + 0xbb2b863c, 0x2624674a, 0x43e9cd8a, 0xdee62cfc, 0xa2870927, + 0x3f88e851}, + {0x00000000, 0xdd96d985, 0x605cb54b, 0xbdca6cce, 0xc0b96a96, + 0x1d2fb313, 0xa0e5dfdd, 0x7d730658, 0x5a03d36d, 0x87950ae8, + 0x3a5f6626, 0xe7c9bfa3, 0x9abab9fb, 0x472c607e, 0xfae60cb0, + 0x2770d535, 0xb407a6da, 0x69917f5f, 0xd45b1391, 0x09cdca14, + 0x74becc4c, 0xa92815c9, 0x14e27907, 0xc974a082, 0xee0475b7, + 0x3392ac32, 0x8e58c0fc, 0x53ce1979, 0x2ebd1f21, 0xf32bc6a4, + 0x4ee1aa6a, 0x937773ef, 0xb37e4bf5, 0x6ee89270, 0xd322febe, + 0x0eb4273b, 0x73c72163, 0xae51f8e6, 0x139b9428, 0xce0d4dad, + 0xe97d9898, 0x34eb411d, 0x89212dd3, 0x54b7f456, 0x29c4f20e, + 0xf4522b8b, 0x49984745, 0x940e9ec0, 0x0779ed2f, 0xdaef34aa, + 0x67255864, 0xbab381e1, 0xc7c087b9, 0x1a565e3c, 0xa79c32f2, + 0x7a0aeb77, 0x5d7a3e42, 0x80ece7c7, 0x3d268b09, 0xe0b0528c, + 0x9dc354d4, 0x40558d51, 0xfd9fe19f, 0x2009381a, 0xbd8d91ab, + 0x601b482e, 0xddd124e0, 0x0047fd65, 0x7d34fb3d, 0xa0a222b8, + 0x1d684e76, 0xc0fe97f3, 0xe78e42c6, 0x3a189b43, 0x87d2f78d, + 0x5a442e08, 0x27372850, 0xfaa1f1d5, 0x476b9d1b, 0x9afd449e, + 0x098a3771, 0xd41ceef4, 0x69d6823a, 0xb4405bbf, 0xc9335de7, + 0x14a58462, 0xa96fe8ac, 0x74f93129, 0x5389e41c, 0x8e1f3d99, + 0x33d55157, 0xee4388d2, 0x93308e8a, 0x4ea6570f, 0xf36c3bc1, + 0x2efae244, 0x0ef3da5e, 0xd36503db, 0x6eaf6f15, 0xb339b690, + 0xce4ab0c8, 0x13dc694d, 0xae160583, 0x7380dc06, 0x54f00933, + 0x8966d0b6, 0x34acbc78, 0xe93a65fd, 0x944963a5, 0x49dfba20, + 0xf415d6ee, 0x29830f6b, 0xbaf47c84, 0x6762a501, 0xdaa8c9cf, + 0x073e104a, 0x7a4d1612, 0xa7dbcf97, 0x1a11a359, 0xc7877adc, + 0xe0f7afe9, 0x3d61766c, 0x80ab1aa2, 0x5d3dc327, 0x204ec57f, + 0xfdd81cfa, 0x40127034, 0x9d84a9b1, 0xa06a2517, 0x7dfcfc92, + 0xc036905c, 0x1da049d9, 0x60d34f81, 0xbd459604, 0x008ffaca, + 0xdd19234f, 0xfa69f67a, 0x27ff2fff, 0x9a354331, 0x47a39ab4, + 0x3ad09cec, 0xe7464569, 0x5a8c29a7, 0x871af022, 0x146d83cd, + 0xc9fb5a48, 0x74313686, 0xa9a7ef03, 0xd4d4e95b, 0x094230de, + 0xb4885c10, 0x691e8595, 0x4e6e50a0, 0x93f88925, 0x2e32e5eb, + 0xf3a43c6e, 0x8ed73a36, 0x5341e3b3, 0xee8b8f7d, 0x331d56f8, + 0x13146ee2, 0xce82b767, 0x7348dba9, 0xaede022c, 0xd3ad0474, + 0x0e3bddf1, 0xb3f1b13f, 0x6e6768ba, 0x4917bd8f, 0x9481640a, + 0x294b08c4, 0xf4ddd141, 0x89aed719, 0x54380e9c, 0xe9f26252, + 0x3464bbd7, 0xa713c838, 0x7a8511bd, 0xc74f7d73, 0x1ad9a4f6, + 0x67aaa2ae, 0xba3c7b2b, 0x07f617e5, 0xda60ce60, 0xfd101b55, + 0x2086c2d0, 0x9d4cae1e, 0x40da779b, 0x3da971c3, 0xe03fa846, + 0x5df5c488, 0x80631d0d, 0x1de7b4bc, 0xc0716d39, 0x7dbb01f7, + 0xa02dd872, 0xdd5ede2a, 0x00c807af, 0xbd026b61, 0x6094b2e4, + 0x47e467d1, 0x9a72be54, 0x27b8d29a, 0xfa2e0b1f, 0x875d0d47, + 0x5acbd4c2, 0xe701b80c, 0x3a976189, 0xa9e01266, 0x7476cbe3, + 0xc9bca72d, 0x142a7ea8, 0x695978f0, 0xb4cfa175, 0x0905cdbb, + 0xd493143e, 0xf3e3c10b, 0x2e75188e, 0x93bf7440, 0x4e29adc5, + 0x335aab9d, 0xeecc7218, 0x53061ed6, 0x8e90c753, 0xae99ff49, + 0x730f26cc, 0xcec54a02, 0x13539387, 0x6e2095df, 0xb3b64c5a, + 0x0e7c2094, 0xd3eaf911, 0xf49a2c24, 0x290cf5a1, 0x94c6996f, + 0x495040ea, 0x342346b2, 0xe9b59f37, 0x547ff3f9, 0x89e92a7c, + 0x1a9e5993, 0xc7088016, 0x7ac2ecd8, 0xa754355d, 0xda273305, + 0x07b1ea80, 0xba7b864e, 0x67ed5fcb, 0x409d8afe, 0x9d0b537b, + 0x20c13fb5, 0xfd57e630, 0x8024e068, 0x5db239ed, 0xe0785523, + 0x3dee8ca6}, + {0x00000000, 0x9ba54c6f, 0xec3b9e9f, 0x779ed2f0, 0x03063b7f, + 0x98a37710, 0xef3da5e0, 0x7498e98f, 0x060c76fe, 0x9da93a91, + 0xea37e861, 0x7192a40e, 0x050a4d81, 0x9eaf01ee, 0xe931d31e, + 0x72949f71, 0x0c18edfc, 0x97bda193, 0xe0237363, 0x7b863f0c, + 0x0f1ed683, 0x94bb9aec, 0xe325481c, 0x78800473, 0x0a149b02, + 0x91b1d76d, 0xe62f059d, 0x7d8a49f2, 0x0912a07d, 0x92b7ec12, + 0xe5293ee2, 0x7e8c728d, 0x1831dbf8, 0x83949797, 0xf40a4567, + 0x6faf0908, 0x1b37e087, 0x8092ace8, 0xf70c7e18, 0x6ca93277, + 0x1e3dad06, 0x8598e169, 0xf2063399, 0x69a37ff6, 0x1d3b9679, + 0x869eda16, 0xf10008e6, 0x6aa54489, 0x14293604, 0x8f8c7a6b, + 0xf812a89b, 0x63b7e4f4, 0x172f0d7b, 0x8c8a4114, 0xfb1493e4, + 0x60b1df8b, 0x122540fa, 0x89800c95, 0xfe1ede65, 0x65bb920a, + 0x11237b85, 0x8a8637ea, 0xfd18e51a, 0x66bda975, 0x3063b7f0, + 0xabc6fb9f, 0xdc58296f, 0x47fd6500, 0x33658c8f, 0xa8c0c0e0, + 0xdf5e1210, 0x44fb5e7f, 0x366fc10e, 0xadca8d61, 0xda545f91, + 0x41f113fe, 0x3569fa71, 0xaeccb61e, 0xd95264ee, 0x42f72881, + 0x3c7b5a0c, 0xa7de1663, 0xd040c493, 0x4be588fc, 0x3f7d6173, + 0xa4d82d1c, 0xd346ffec, 0x48e3b383, 0x3a772cf2, 0xa1d2609d, + 0xd64cb26d, 0x4de9fe02, 0x3971178d, 0xa2d45be2, 0xd54a8912, + 0x4eefc57d, 0x28526c08, 0xb3f72067, 0xc469f297, 0x5fccbef8, + 0x2b545777, 0xb0f11b18, 0xc76fc9e8, 0x5cca8587, 0x2e5e1af6, + 0xb5fb5699, 0xc2658469, 0x59c0c806, 0x2d582189, 0xb6fd6de6, + 0xc163bf16, 0x5ac6f379, 0x244a81f4, 0xbfefcd9b, 0xc8711f6b, + 0x53d45304, 0x274cba8b, 0xbce9f6e4, 0xcb772414, 0x50d2687b, + 0x2246f70a, 0xb9e3bb65, 0xce7d6995, 0x55d825fa, 0x2140cc75, + 0xbae5801a, 0xcd7b52ea, 0x56de1e85, 0x60c76fe0, 0xfb62238f, + 0x8cfcf17f, 0x1759bd10, 0x63c1549f, 0xf86418f0, 0x8ffaca00, + 0x145f866f, 0x66cb191e, 0xfd6e5571, 0x8af08781, 0x1155cbee, + 0x65cd2261, 0xfe686e0e, 0x89f6bcfe, 0x1253f091, 0x6cdf821c, + 0xf77ace73, 0x80e41c83, 0x1b4150ec, 0x6fd9b963, 0xf47cf50c, + 0x83e227fc, 0x18476b93, 0x6ad3f4e2, 0xf176b88d, 0x86e86a7d, + 0x1d4d2612, 0x69d5cf9d, 0xf27083f2, 0x85ee5102, 0x1e4b1d6d, + 0x78f6b418, 0xe353f877, 0x94cd2a87, 0x0f6866e8, 0x7bf08f67, + 0xe055c308, 0x97cb11f8, 0x0c6e5d97, 0x7efac2e6, 0xe55f8e89, + 0x92c15c79, 0x09641016, 0x7dfcf999, 0xe659b5f6, 0x91c76706, + 0x0a622b69, 0x74ee59e4, 0xef4b158b, 0x98d5c77b, 0x03708b14, + 0x77e8629b, 0xec4d2ef4, 0x9bd3fc04, 0x0076b06b, 0x72e22f1a, + 0xe9476375, 0x9ed9b185, 0x057cfdea, 0x71e41465, 0xea41580a, + 0x9ddf8afa, 0x067ac695, 0x50a4d810, 0xcb01947f, 0xbc9f468f, + 0x273a0ae0, 0x53a2e36f, 0xc807af00, 0xbf997df0, 0x243c319f, + 0x56a8aeee, 0xcd0de281, 0xba933071, 0x21367c1e, 0x55ae9591, + 0xce0bd9fe, 0xb9950b0e, 0x22304761, 0x5cbc35ec, 0xc7197983, + 0xb087ab73, 0x2b22e71c, 0x5fba0e93, 0xc41f42fc, 0xb381900c, + 0x2824dc63, 0x5ab04312, 0xc1150f7d, 0xb68bdd8d, 0x2d2e91e2, + 0x59b6786d, 0xc2133402, 0xb58de6f2, 0x2e28aa9d, 0x489503e8, + 0xd3304f87, 0xa4ae9d77, 0x3f0bd118, 0x4b933897, 0xd03674f8, + 0xa7a8a608, 0x3c0dea67, 0x4e997516, 0xd53c3979, 0xa2a2eb89, + 0x3907a7e6, 0x4d9f4e69, 0xd63a0206, 0xa1a4d0f6, 0x3a019c99, + 0x448dee14, 0xdf28a27b, 0xa8b6708b, 0x33133ce4, 0x478bd56b, + 0xdc2e9904, 0xabb04bf4, 0x3015079b, 0x428198ea, 0xd924d485, + 0xaeba0675, 0x351f4a1a, 0x4187a395, 0xda22effa, 0xadbc3d0a, + 0x36197165}, + {0x00000000, 0xc18edfc0, 0x586cb9c1, 0x99e26601, 0xb0d97382, + 0x7157ac42, 0xe8b5ca43, 0x293b1583, 0xbac3e145, 0x7b4d3e85, + 0xe2af5884, 0x23218744, 0x0a1a92c7, 0xcb944d07, 0x52762b06, + 0x93f8f4c6, 0xaef6c4cb, 0x6f781b0b, 0xf69a7d0a, 0x3714a2ca, + 0x1e2fb749, 0xdfa16889, 0x46430e88, 0x87cdd148, 0x1435258e, + 0xd5bbfa4e, 0x4c599c4f, 0x8dd7438f, 0xa4ec560c, 0x656289cc, + 0xfc80efcd, 0x3d0e300d, 0x869c8fd7, 0x47125017, 0xdef03616, + 0x1f7ee9d6, 0x3645fc55, 0xf7cb2395, 0x6e294594, 0xafa79a54, + 0x3c5f6e92, 0xfdd1b152, 0x6433d753, 0xa5bd0893, 0x8c861d10, + 0x4d08c2d0, 0xd4eaa4d1, 0x15647b11, 0x286a4b1c, 0xe9e494dc, + 0x7006f2dd, 0xb1882d1d, 0x98b3389e, 0x593de75e, 0xc0df815f, + 0x01515e9f, 0x92a9aa59, 0x53277599, 0xcac51398, 0x0b4bcc58, + 0x2270d9db, 0xe3fe061b, 0x7a1c601a, 0xbb92bfda, 0xd64819ef, + 0x17c6c62f, 0x8e24a02e, 0x4faa7fee, 0x66916a6d, 0xa71fb5ad, + 0x3efdd3ac, 0xff730c6c, 0x6c8bf8aa, 0xad05276a, 0x34e7416b, + 0xf5699eab, 0xdc528b28, 0x1ddc54e8, 0x843e32e9, 0x45b0ed29, + 0x78bedd24, 0xb93002e4, 0x20d264e5, 0xe15cbb25, 0xc867aea6, + 0x09e97166, 0x900b1767, 0x5185c8a7, 0xc27d3c61, 0x03f3e3a1, + 0x9a1185a0, 0x5b9f5a60, 0x72a44fe3, 0xb32a9023, 0x2ac8f622, + 0xeb4629e2, 0x50d49638, 0x915a49f8, 0x08b82ff9, 0xc936f039, + 0xe00de5ba, 0x21833a7a, 0xb8615c7b, 0x79ef83bb, 0xea17777d, + 0x2b99a8bd, 0xb27bcebc, 0x73f5117c, 0x5ace04ff, 0x9b40db3f, + 0x02a2bd3e, 0xc32c62fe, 0xfe2252f3, 0x3fac8d33, 0xa64eeb32, + 0x67c034f2, 0x4efb2171, 0x8f75feb1, 0x169798b0, 0xd7194770, + 0x44e1b3b6, 0x856f6c76, 0x1c8d0a77, 0xdd03d5b7, 0xf438c034, + 0x35b61ff4, 0xac5479f5, 0x6ddaa635, 0x77e1359f, 0xb66fea5f, + 0x2f8d8c5e, 0xee03539e, 0xc738461d, 0x06b699dd, 0x9f54ffdc, + 0x5eda201c, 0xcd22d4da, 0x0cac0b1a, 0x954e6d1b, 0x54c0b2db, + 0x7dfba758, 0xbc757898, 0x25971e99, 0xe419c159, 0xd917f154, + 0x18992e94, 0x817b4895, 0x40f59755, 0x69ce82d6, 0xa8405d16, + 0x31a23b17, 0xf02ce4d7, 0x63d41011, 0xa25acfd1, 0x3bb8a9d0, + 0xfa367610, 0xd30d6393, 0x1283bc53, 0x8b61da52, 0x4aef0592, + 0xf17dba48, 0x30f36588, 0xa9110389, 0x689fdc49, 0x41a4c9ca, + 0x802a160a, 0x19c8700b, 0xd846afcb, 0x4bbe5b0d, 0x8a3084cd, + 0x13d2e2cc, 0xd25c3d0c, 0xfb67288f, 0x3ae9f74f, 0xa30b914e, + 0x62854e8e, 0x5f8b7e83, 0x9e05a143, 0x07e7c742, 0xc6691882, + 0xef520d01, 0x2edcd2c1, 0xb73eb4c0, 0x76b06b00, 0xe5489fc6, + 0x24c64006, 0xbd242607, 0x7caaf9c7, 0x5591ec44, 0x941f3384, + 0x0dfd5585, 0xcc738a45, 0xa1a92c70, 0x6027f3b0, 0xf9c595b1, + 0x384b4a71, 0x11705ff2, 0xd0fe8032, 0x491ce633, 0x889239f3, + 0x1b6acd35, 0xdae412f5, 0x430674f4, 0x8288ab34, 0xabb3beb7, + 0x6a3d6177, 0xf3df0776, 0x3251d8b6, 0x0f5fe8bb, 0xced1377b, + 0x5733517a, 0x96bd8eba, 0xbf869b39, 0x7e0844f9, 0xe7ea22f8, + 0x2664fd38, 0xb59c09fe, 0x7412d63e, 0xedf0b03f, 0x2c7e6fff, + 0x05457a7c, 0xc4cba5bc, 0x5d29c3bd, 0x9ca71c7d, 0x2735a3a7, + 0xe6bb7c67, 0x7f591a66, 0xbed7c5a6, 0x97ecd025, 0x56620fe5, + 0xcf8069e4, 0x0e0eb624, 0x9df642e2, 0x5c789d22, 0xc59afb23, + 0x041424e3, 0x2d2f3160, 0xeca1eea0, 0x754388a1, 0xb4cd5761, + 0x89c3676c, 0x484db8ac, 0xd1afdead, 0x1021016d, 0x391a14ee, + 0xf894cb2e, 0x6176ad2f, 0xa0f872ef, 0x33008629, 0xf28e59e9, + 0x6b6c3fe8, 0xaae2e028, 0x83d9f5ab, 0x42572a6b, 0xdbb54c6a, + 0x1a3b93aa}, + {0x00000000, 0xefc26b3e, 0x04f5d03d, 0xeb37bb03, 0x09eba07a, + 0xe629cb44, 0x0d1e7047, 0xe2dc1b79, 0x13d740f4, 0xfc152bca, + 0x172290c9, 0xf8e0fbf7, 0x1a3ce08e, 0xf5fe8bb0, 0x1ec930b3, + 0xf10b5b8d, 0x27ae81e8, 0xc86cead6, 0x235b51d5, 0xcc993aeb, + 0x2e452192, 0xc1874aac, 0x2ab0f1af, 0xc5729a91, 0x3479c11c, + 0xdbbbaa22, 0x308c1121, 0xdf4e7a1f, 0x3d926166, 0xd2500a58, + 0x3967b15b, 0xd6a5da65, 0x4f5d03d0, 0xa09f68ee, 0x4ba8d3ed, + 0xa46ab8d3, 0x46b6a3aa, 0xa974c894, 0x42437397, 0xad8118a9, + 0x5c8a4324, 0xb348281a, 0x587f9319, 0xb7bdf827, 0x5561e35e, + 0xbaa38860, 0x51943363, 0xbe56585d, 0x68f38238, 0x8731e906, + 0x6c065205, 0x83c4393b, 0x61182242, 0x8eda497c, 0x65edf27f, + 0x8a2f9941, 0x7b24c2cc, 0x94e6a9f2, 0x7fd112f1, 0x901379cf, + 0x72cf62b6, 0x9d0d0988, 0x763ab28b, 0x99f8d9b5, 0x9eba07a0, + 0x71786c9e, 0x9a4fd79d, 0x758dbca3, 0x9751a7da, 0x7893cce4, + 0x93a477e7, 0x7c661cd9, 0x8d6d4754, 0x62af2c6a, 0x89989769, + 0x665afc57, 0x8486e72e, 0x6b448c10, 0x80733713, 0x6fb15c2d, + 0xb9148648, 0x56d6ed76, 0xbde15675, 0x52233d4b, 0xb0ff2632, + 0x5f3d4d0c, 0xb40af60f, 0x5bc89d31, 0xaac3c6bc, 0x4501ad82, + 0xae361681, 0x41f47dbf, 0xa32866c6, 0x4cea0df8, 0xa7ddb6fb, + 0x481fddc5, 0xd1e70470, 0x3e256f4e, 0xd512d44d, 0x3ad0bf73, + 0xd80ca40a, 0x37cecf34, 0xdcf97437, 0x333b1f09, 0xc2304484, + 0x2df22fba, 0xc6c594b9, 0x2907ff87, 0xcbdbe4fe, 0x24198fc0, + 0xcf2e34c3, 0x20ec5ffd, 0xf6498598, 0x198beea6, 0xf2bc55a5, + 0x1d7e3e9b, 0xffa225e2, 0x10604edc, 0xfb57f5df, 0x14959ee1, + 0xe59ec56c, 0x0a5cae52, 0xe16b1551, 0x0ea97e6f, 0xec756516, + 0x03b70e28, 0xe880b52b, 0x0742de15, 0xe6050901, 0x09c7623f, + 0xe2f0d93c, 0x0d32b202, 0xefeea97b, 0x002cc245, 0xeb1b7946, + 0x04d91278, 0xf5d249f5, 0x1a1022cb, 0xf12799c8, 0x1ee5f2f6, + 0xfc39e98f, 0x13fb82b1, 0xf8cc39b2, 0x170e528c, 0xc1ab88e9, + 0x2e69e3d7, 0xc55e58d4, 0x2a9c33ea, 0xc8402893, 0x278243ad, + 0xccb5f8ae, 0x23779390, 0xd27cc81d, 0x3dbea323, 0xd6891820, + 0x394b731e, 0xdb976867, 0x34550359, 0xdf62b85a, 0x30a0d364, + 0xa9580ad1, 0x469a61ef, 0xadaddaec, 0x426fb1d2, 0xa0b3aaab, + 0x4f71c195, 0xa4467a96, 0x4b8411a8, 0xba8f4a25, 0x554d211b, + 0xbe7a9a18, 0x51b8f126, 0xb364ea5f, 0x5ca68161, 0xb7913a62, + 0x5853515c, 0x8ef68b39, 0x6134e007, 0x8a035b04, 0x65c1303a, + 0x871d2b43, 0x68df407d, 0x83e8fb7e, 0x6c2a9040, 0x9d21cbcd, + 0x72e3a0f3, 0x99d41bf0, 0x761670ce, 0x94ca6bb7, 0x7b080089, + 0x903fbb8a, 0x7ffdd0b4, 0x78bf0ea1, 0x977d659f, 0x7c4ade9c, + 0x9388b5a2, 0x7154aedb, 0x9e96c5e5, 0x75a17ee6, 0x9a6315d8, + 0x6b684e55, 0x84aa256b, 0x6f9d9e68, 0x805ff556, 0x6283ee2f, + 0x8d418511, 0x66763e12, 0x89b4552c, 0x5f118f49, 0xb0d3e477, + 0x5be45f74, 0xb426344a, 0x56fa2f33, 0xb938440d, 0x520fff0e, + 0xbdcd9430, 0x4cc6cfbd, 0xa304a483, 0x48331f80, 0xa7f174be, + 0x452d6fc7, 0xaaef04f9, 0x41d8bffa, 0xae1ad4c4, 0x37e20d71, + 0xd820664f, 0x3317dd4c, 0xdcd5b672, 0x3e09ad0b, 0xd1cbc635, + 0x3afc7d36, 0xd53e1608, 0x24354d85, 0xcbf726bb, 0x20c09db8, + 0xcf02f686, 0x2ddeedff, 0xc21c86c1, 0x292b3dc2, 0xc6e956fc, + 0x104c8c99, 0xff8ee7a7, 0x14b95ca4, 0xfb7b379a, 0x19a72ce3, + 0xf66547dd, 0x1d52fcde, 0xf29097e0, 0x039bcc6d, 0xec59a753, + 0x076e1c50, 0xe8ac776e, 0x0a706c17, 0xe5b20729, 0x0e85bc2a, + 0xe147d714}, + {0x00000000, 0x177b1443, 0x2ef62886, 0x398d3cc5, 0x5dec510c, + 0x4a97454f, 0x731a798a, 0x64616dc9, 0xbbd8a218, 0xaca3b65b, + 0x952e8a9e, 0x82559edd, 0xe634f314, 0xf14fe757, 0xc8c2db92, + 0xdfb9cfd1, 0xacc04271, 0xbbbb5632, 0x82366af7, 0x954d7eb4, + 0xf12c137d, 0xe657073e, 0xdfda3bfb, 0xc8a12fb8, 0x1718e069, + 0x0063f42a, 0x39eec8ef, 0x2e95dcac, 0x4af4b165, 0x5d8fa526, + 0x640299e3, 0x73798da0, 0x82f182a3, 0x958a96e0, 0xac07aa25, + 0xbb7cbe66, 0xdf1dd3af, 0xc866c7ec, 0xf1ebfb29, 0xe690ef6a, + 0x392920bb, 0x2e5234f8, 0x17df083d, 0x00a41c7e, 0x64c571b7, + 0x73be65f4, 0x4a335931, 0x5d484d72, 0x2e31c0d2, 0x394ad491, + 0x00c7e854, 0x17bcfc17, 0x73dd91de, 0x64a6859d, 0x5d2bb958, + 0x4a50ad1b, 0x95e962ca, 0x82927689, 0xbb1f4a4c, 0xac645e0f, + 0xc80533c6, 0xdf7e2785, 0xe6f31b40, 0xf1880f03, 0xde920307, + 0xc9e91744, 0xf0642b81, 0xe71f3fc2, 0x837e520b, 0x94054648, + 0xad887a8d, 0xbaf36ece, 0x654aa11f, 0x7231b55c, 0x4bbc8999, + 0x5cc79dda, 0x38a6f013, 0x2fdde450, 0x1650d895, 0x012bccd6, + 0x72524176, 0x65295535, 0x5ca469f0, 0x4bdf7db3, 0x2fbe107a, + 0x38c50439, 0x014838fc, 0x16332cbf, 0xc98ae36e, 0xdef1f72d, + 0xe77ccbe8, 0xf007dfab, 0x9466b262, 0x831da621, 0xba909ae4, + 0xadeb8ea7, 0x5c6381a4, 0x4b1895e7, 0x7295a922, 0x65eebd61, + 0x018fd0a8, 0x16f4c4eb, 0x2f79f82e, 0x3802ec6d, 0xe7bb23bc, + 0xf0c037ff, 0xc94d0b3a, 0xde361f79, 0xba5772b0, 0xad2c66f3, + 0x94a15a36, 0x83da4e75, 0xf0a3c3d5, 0xe7d8d796, 0xde55eb53, + 0xc92eff10, 0xad4f92d9, 0xba34869a, 0x83b9ba5f, 0x94c2ae1c, + 0x4b7b61cd, 0x5c00758e, 0x658d494b, 0x72f65d08, 0x169730c1, + 0x01ec2482, 0x38611847, 0x2f1a0c04, 0x6655004f, 0x712e140c, + 0x48a328c9, 0x5fd83c8a, 0x3bb95143, 0x2cc24500, 0x154f79c5, + 0x02346d86, 0xdd8da257, 0xcaf6b614, 0xf37b8ad1, 0xe4009e92, + 0x8061f35b, 0x971ae718, 0xae97dbdd, 0xb9eccf9e, 0xca95423e, + 0xddee567d, 0xe4636ab8, 0xf3187efb, 0x97791332, 0x80020771, + 0xb98f3bb4, 0xaef42ff7, 0x714de026, 0x6636f465, 0x5fbbc8a0, + 0x48c0dce3, 0x2ca1b12a, 0x3bdaa569, 0x025799ac, 0x152c8def, + 0xe4a482ec, 0xf3df96af, 0xca52aa6a, 0xdd29be29, 0xb948d3e0, + 0xae33c7a3, 0x97befb66, 0x80c5ef25, 0x5f7c20f4, 0x480734b7, + 0x718a0872, 0x66f11c31, 0x029071f8, 0x15eb65bb, 0x2c66597e, + 0x3b1d4d3d, 0x4864c09d, 0x5f1fd4de, 0x6692e81b, 0x71e9fc58, + 0x15889191, 0x02f385d2, 0x3b7eb917, 0x2c05ad54, 0xf3bc6285, + 0xe4c776c6, 0xdd4a4a03, 0xca315e40, 0xae503389, 0xb92b27ca, + 0x80a61b0f, 0x97dd0f4c, 0xb8c70348, 0xafbc170b, 0x96312bce, + 0x814a3f8d, 0xe52b5244, 0xf2504607, 0xcbdd7ac2, 0xdca66e81, + 0x031fa150, 0x1464b513, 0x2de989d6, 0x3a929d95, 0x5ef3f05c, + 0x4988e41f, 0x7005d8da, 0x677ecc99, 0x14074139, 0x037c557a, + 0x3af169bf, 0x2d8a7dfc, 0x49eb1035, 0x5e900476, 0x671d38b3, + 0x70662cf0, 0xafdfe321, 0xb8a4f762, 0x8129cba7, 0x9652dfe4, + 0xf233b22d, 0xe548a66e, 0xdcc59aab, 0xcbbe8ee8, 0x3a3681eb, + 0x2d4d95a8, 0x14c0a96d, 0x03bbbd2e, 0x67dad0e7, 0x70a1c4a4, + 0x492cf861, 0x5e57ec22, 0x81ee23f3, 0x969537b0, 0xaf180b75, + 0xb8631f36, 0xdc0272ff, 0xcb7966bc, 0xf2f45a79, 0xe58f4e3a, + 0x96f6c39a, 0x818dd7d9, 0xb800eb1c, 0xaf7bff5f, 0xcb1a9296, + 0xdc6186d5, 0xe5ecba10, 0xf297ae53, 0x2d2e6182, 0x3a5575c1, + 0x03d84904, 0x14a35d47, 0x70c2308e, 0x67b924cd, 0x5e341808, + 0x494f0c4b}}; + +local const z_word_t FAR crc_braid_big_table[][256] = { + {0x0000000000000000, 0x43147b1700000000, 0x8628f62e00000000, + 0xc53c8d3900000000, 0x0c51ec5d00000000, 0x4f45974a00000000, + 0x8a791a7300000000, 0xc96d616400000000, 0x18a2d8bb00000000, + 0x5bb6a3ac00000000, 0x9e8a2e9500000000, 0xdd9e558200000000, + 0x14f334e600000000, 0x57e74ff100000000, 0x92dbc2c800000000, + 0xd1cfb9df00000000, 0x7142c0ac00000000, 0x3256bbbb00000000, + 0xf76a368200000000, 0xb47e4d9500000000, 0x7d132cf100000000, + 0x3e0757e600000000, 0xfb3bdadf00000000, 0xb82fa1c800000000, + 0x69e0181700000000, 0x2af4630000000000, 0xefc8ee3900000000, + 0xacdc952e00000000, 0x65b1f44a00000000, 0x26a58f5d00000000, + 0xe399026400000000, 0xa08d797300000000, 0xa382f18200000000, + 0xe0968a9500000000, 0x25aa07ac00000000, 0x66be7cbb00000000, + 0xafd31ddf00000000, 0xecc766c800000000, 0x29fbebf100000000, + 0x6aef90e600000000, 0xbb20293900000000, 0xf834522e00000000, + 0x3d08df1700000000, 0x7e1ca40000000000, 0xb771c56400000000, + 0xf465be7300000000, 0x3159334a00000000, 0x724d485d00000000, + 0xd2c0312e00000000, 0x91d44a3900000000, 0x54e8c70000000000, + 0x17fcbc1700000000, 0xde91dd7300000000, 0x9d85a66400000000, + 0x58b92b5d00000000, 0x1bad504a00000000, 0xca62e99500000000, + 0x8976928200000000, 0x4c4a1fbb00000000, 0x0f5e64ac00000000, + 0xc63305c800000000, 0x85277edf00000000, 0x401bf3e600000000, + 0x030f88f100000000, 0x070392de00000000, 0x4417e9c900000000, + 0x812b64f000000000, 0xc23f1fe700000000, 0x0b527e8300000000, + 0x4846059400000000, 0x8d7a88ad00000000, 0xce6ef3ba00000000, + 0x1fa14a6500000000, 0x5cb5317200000000, 0x9989bc4b00000000, + 0xda9dc75c00000000, 0x13f0a63800000000, 0x50e4dd2f00000000, + 0x95d8501600000000, 0xd6cc2b0100000000, 0x7641527200000000, + 0x3555296500000000, 0xf069a45c00000000, 0xb37ddf4b00000000, + 0x7a10be2f00000000, 0x3904c53800000000, 0xfc38480100000000, + 0xbf2c331600000000, 0x6ee38ac900000000, 0x2df7f1de00000000, + 0xe8cb7ce700000000, 0xabdf07f000000000, 0x62b2669400000000, + 0x21a61d8300000000, 0xe49a90ba00000000, 0xa78eebad00000000, + 0xa481635c00000000, 0xe795184b00000000, 0x22a9957200000000, + 0x61bdee6500000000, 0xa8d08f0100000000, 0xebc4f41600000000, + 0x2ef8792f00000000, 0x6dec023800000000, 0xbc23bbe700000000, + 0xff37c0f000000000, 0x3a0b4dc900000000, 0x791f36de00000000, + 0xb07257ba00000000, 0xf3662cad00000000, 0x365aa19400000000, + 0x754eda8300000000, 0xd5c3a3f000000000, 0x96d7d8e700000000, + 0x53eb55de00000000, 0x10ff2ec900000000, 0xd9924fad00000000, + 0x9a8634ba00000000, 0x5fbab98300000000, 0x1caec29400000000, + 0xcd617b4b00000000, 0x8e75005c00000000, 0x4b498d6500000000, + 0x085df67200000000, 0xc130971600000000, 0x8224ec0100000000, + 0x4718613800000000, 0x040c1a2f00000000, 0x4f00556600000000, + 0x0c142e7100000000, 0xc928a34800000000, 0x8a3cd85f00000000, + 0x4351b93b00000000, 0x0045c22c00000000, 0xc5794f1500000000, + 0x866d340200000000, 0x57a28ddd00000000, 0x14b6f6ca00000000, + 0xd18a7bf300000000, 0x929e00e400000000, 0x5bf3618000000000, + 0x18e71a9700000000, 0xdddb97ae00000000, 0x9ecfecb900000000, + 0x3e4295ca00000000, 0x7d56eedd00000000, 0xb86a63e400000000, + 0xfb7e18f300000000, 0x3213799700000000, 0x7107028000000000, + 0xb43b8fb900000000, 0xf72ff4ae00000000, 0x26e04d7100000000, + 0x65f4366600000000, 0xa0c8bb5f00000000, 0xe3dcc04800000000, + 0x2ab1a12c00000000, 0x69a5da3b00000000, 0xac99570200000000, + 0xef8d2c1500000000, 0xec82a4e400000000, 0xaf96dff300000000, + 0x6aaa52ca00000000, 0x29be29dd00000000, 0xe0d348b900000000, + 0xa3c733ae00000000, 0x66fbbe9700000000, 0x25efc58000000000, + 0xf4207c5f00000000, 0xb734074800000000, 0x72088a7100000000, + 0x311cf16600000000, 0xf871900200000000, 0xbb65eb1500000000, + 0x7e59662c00000000, 0x3d4d1d3b00000000, 0x9dc0644800000000, + 0xded41f5f00000000, 0x1be8926600000000, 0x58fce97100000000, + 0x9191881500000000, 0xd285f30200000000, 0x17b97e3b00000000, + 0x54ad052c00000000, 0x8562bcf300000000, 0xc676c7e400000000, + 0x034a4add00000000, 0x405e31ca00000000, 0x893350ae00000000, + 0xca272bb900000000, 0x0f1ba68000000000, 0x4c0fdd9700000000, + 0x4803c7b800000000, 0x0b17bcaf00000000, 0xce2b319600000000, + 0x8d3f4a8100000000, 0x44522be500000000, 0x074650f200000000, + 0xc27addcb00000000, 0x816ea6dc00000000, 0x50a11f0300000000, + 0x13b5641400000000, 0xd689e92d00000000, 0x959d923a00000000, + 0x5cf0f35e00000000, 0x1fe4884900000000, 0xdad8057000000000, + 0x99cc7e6700000000, 0x3941071400000000, 0x7a557c0300000000, + 0xbf69f13a00000000, 0xfc7d8a2d00000000, 0x3510eb4900000000, + 0x7604905e00000000, 0xb3381d6700000000, 0xf02c667000000000, + 0x21e3dfaf00000000, 0x62f7a4b800000000, 0xa7cb298100000000, + 0xe4df529600000000, 0x2db233f200000000, 0x6ea648e500000000, + 0xab9ac5dc00000000, 0xe88ebecb00000000, 0xeb81363a00000000, + 0xa8954d2d00000000, 0x6da9c01400000000, 0x2ebdbb0300000000, + 0xe7d0da6700000000, 0xa4c4a17000000000, 0x61f82c4900000000, + 0x22ec575e00000000, 0xf323ee8100000000, 0xb037959600000000, + 0x750b18af00000000, 0x361f63b800000000, 0xff7202dc00000000, + 0xbc6679cb00000000, 0x795af4f200000000, 0x3a4e8fe500000000, + 0x9ac3f69600000000, 0xd9d78d8100000000, 0x1ceb00b800000000, + 0x5fff7baf00000000, 0x96921acb00000000, 0xd58661dc00000000, + 0x10baece500000000, 0x53ae97f200000000, 0x82612e2d00000000, + 0xc175553a00000000, 0x0449d80300000000, 0x475da31400000000, + 0x8e30c27000000000, 0xcd24b96700000000, 0x0818345e00000000, + 0x4b0c4f4900000000}, + {0x0000000000000000, 0x3e6bc2ef00000000, 0x3dd0f50400000000, + 0x03bb37eb00000000, 0x7aa0eb0900000000, 0x44cb29e600000000, + 0x47701e0d00000000, 0x791bdce200000000, 0xf440d71300000000, + 0xca2b15fc00000000, 0xc990221700000000, 0xf7fbe0f800000000, + 0x8ee03c1a00000000, 0xb08bfef500000000, 0xb330c91e00000000, + 0x8d5b0bf100000000, 0xe881ae2700000000, 0xd6ea6cc800000000, + 0xd5515b2300000000, 0xeb3a99cc00000000, 0x9221452e00000000, + 0xac4a87c100000000, 0xaff1b02a00000000, 0x919a72c500000000, + 0x1cc1793400000000, 0x22aabbdb00000000, 0x21118c3000000000, + 0x1f7a4edf00000000, 0x6661923d00000000, 0x580a50d200000000, + 0x5bb1673900000000, 0x65daa5d600000000, 0xd0035d4f00000000, + 0xee689fa000000000, 0xedd3a84b00000000, 0xd3b86aa400000000, + 0xaaa3b64600000000, 0x94c874a900000000, 0x9773434200000000, + 0xa91881ad00000000, 0x24438a5c00000000, 0x1a2848b300000000, + 0x19937f5800000000, 0x27f8bdb700000000, 0x5ee3615500000000, + 0x6088a3ba00000000, 0x6333945100000000, 0x5d5856be00000000, + 0x3882f36800000000, 0x06e9318700000000, 0x0552066c00000000, + 0x3b39c48300000000, 0x4222186100000000, 0x7c49da8e00000000, + 0x7ff2ed6500000000, 0x41992f8a00000000, 0xccc2247b00000000, + 0xf2a9e69400000000, 0xf112d17f00000000, 0xcf79139000000000, + 0xb662cf7200000000, 0x88090d9d00000000, 0x8bb23a7600000000, + 0xb5d9f89900000000, 0xa007ba9e00000000, 0x9e6c787100000000, + 0x9dd74f9a00000000, 0xa3bc8d7500000000, 0xdaa7519700000000, + 0xe4cc937800000000, 0xe777a49300000000, 0xd91c667c00000000, + 0x54476d8d00000000, 0x6a2caf6200000000, 0x6997988900000000, + 0x57fc5a6600000000, 0x2ee7868400000000, 0x108c446b00000000, + 0x1337738000000000, 0x2d5cb16f00000000, 0x488614b900000000, + 0x76edd65600000000, 0x7556e1bd00000000, 0x4b3d235200000000, + 0x3226ffb000000000, 0x0c4d3d5f00000000, 0x0ff60ab400000000, + 0x319dc85b00000000, 0xbcc6c3aa00000000, 0x82ad014500000000, + 0x811636ae00000000, 0xbf7df44100000000, 0xc66628a300000000, + 0xf80dea4c00000000, 0xfbb6dda700000000, 0xc5dd1f4800000000, + 0x7004e7d100000000, 0x4e6f253e00000000, 0x4dd412d500000000, + 0x73bfd03a00000000, 0x0aa40cd800000000, 0x34cfce3700000000, + 0x3774f9dc00000000, 0x091f3b3300000000, 0x844430c200000000, + 0xba2ff22d00000000, 0xb994c5c600000000, 0x87ff072900000000, + 0xfee4dbcb00000000, 0xc08f192400000000, 0xc3342ecf00000000, + 0xfd5fec2000000000, 0x988549f600000000, 0xa6ee8b1900000000, + 0xa555bcf200000000, 0x9b3e7e1d00000000, 0xe225a2ff00000000, + 0xdc4e601000000000, 0xdff557fb00000000, 0xe19e951400000000, + 0x6cc59ee500000000, 0x52ae5c0a00000000, 0x51156be100000000, + 0x6f7ea90e00000000, 0x166575ec00000000, 0x280eb70300000000, + 0x2bb580e800000000, 0x15de420700000000, 0x010905e600000000, + 0x3f62c70900000000, 0x3cd9f0e200000000, 0x02b2320d00000000, + 0x7ba9eeef00000000, 0x45c22c0000000000, 0x46791beb00000000, + 0x7812d90400000000, 0xf549d2f500000000, 0xcb22101a00000000, + 0xc89927f100000000, 0xf6f2e51e00000000, 0x8fe939fc00000000, + 0xb182fb1300000000, 0xb239ccf800000000, 0x8c520e1700000000, + 0xe988abc100000000, 0xd7e3692e00000000, 0xd4585ec500000000, + 0xea339c2a00000000, 0x932840c800000000, 0xad43822700000000, + 0xaef8b5cc00000000, 0x9093772300000000, 0x1dc87cd200000000, + 0x23a3be3d00000000, 0x201889d600000000, 0x1e734b3900000000, + 0x676897db00000000, 0x5903553400000000, 0x5ab862df00000000, + 0x64d3a03000000000, 0xd10a58a900000000, 0xef619a4600000000, + 0xecdaadad00000000, 0xd2b16f4200000000, 0xabaab3a000000000, + 0x95c1714f00000000, 0x967a46a400000000, 0xa811844b00000000, + 0x254a8fba00000000, 0x1b214d5500000000, 0x189a7abe00000000, + 0x26f1b85100000000, 0x5fea64b300000000, 0x6181a65c00000000, + 0x623a91b700000000, 0x5c51535800000000, 0x398bf68e00000000, + 0x07e0346100000000, 0x045b038a00000000, 0x3a30c16500000000, + 0x432b1d8700000000, 0x7d40df6800000000, 0x7efbe88300000000, + 0x40902a6c00000000, 0xcdcb219d00000000, 0xf3a0e37200000000, + 0xf01bd49900000000, 0xce70167600000000, 0xb76bca9400000000, + 0x8900087b00000000, 0x8abb3f9000000000, 0xb4d0fd7f00000000, + 0xa10ebf7800000000, 0x9f657d9700000000, 0x9cde4a7c00000000, + 0xa2b5889300000000, 0xdbae547100000000, 0xe5c5969e00000000, + 0xe67ea17500000000, 0xd815639a00000000, 0x554e686b00000000, + 0x6b25aa8400000000, 0x689e9d6f00000000, 0x56f55f8000000000, + 0x2fee836200000000, 0x1185418d00000000, 0x123e766600000000, + 0x2c55b48900000000, 0x498f115f00000000, 0x77e4d3b000000000, + 0x745fe45b00000000, 0x4a3426b400000000, 0x332ffa5600000000, + 0x0d4438b900000000, 0x0eff0f5200000000, 0x3094cdbd00000000, + 0xbdcfc64c00000000, 0x83a404a300000000, 0x801f334800000000, + 0xbe74f1a700000000, 0xc76f2d4500000000, 0xf904efaa00000000, + 0xfabfd84100000000, 0xc4d41aae00000000, 0x710de23700000000, + 0x4f6620d800000000, 0x4cdd173300000000, 0x72b6d5dc00000000, + 0x0bad093e00000000, 0x35c6cbd100000000, 0x367dfc3a00000000, + 0x08163ed500000000, 0x854d352400000000, 0xbb26f7cb00000000, + 0xb89dc02000000000, 0x86f602cf00000000, 0xffedde2d00000000, + 0xc1861cc200000000, 0xc23d2b2900000000, 0xfc56e9c600000000, + 0x998c4c1000000000, 0xa7e78eff00000000, 0xa45cb91400000000, + 0x9a377bfb00000000, 0xe32ca71900000000, 0xdd4765f600000000, + 0xdefc521d00000000, 0xe09790f200000000, 0x6dcc9b0300000000, + 0x53a759ec00000000, 0x501c6e0700000000, 0x6e77ace800000000, + 0x176c700a00000000, 0x2907b2e500000000, 0x2abc850e00000000, + 0x14d747e100000000}, + {0x0000000000000000, 0xc0df8ec100000000, 0xc1b96c5800000000, + 0x0166e29900000000, 0x8273d9b000000000, 0x42ac577100000000, + 0x43cab5e800000000, 0x83153b2900000000, 0x45e1c3ba00000000, + 0x853e4d7b00000000, 0x8458afe200000000, 0x4487212300000000, + 0xc7921a0a00000000, 0x074d94cb00000000, 0x062b765200000000, + 0xc6f4f89300000000, 0xcbc4f6ae00000000, 0x0b1b786f00000000, + 0x0a7d9af600000000, 0xcaa2143700000000, 0x49b72f1e00000000, + 0x8968a1df00000000, 0x880e434600000000, 0x48d1cd8700000000, + 0x8e25351400000000, 0x4efabbd500000000, 0x4f9c594c00000000, + 0x8f43d78d00000000, 0x0c56eca400000000, 0xcc89626500000000, + 0xcdef80fc00000000, 0x0d300e3d00000000, 0xd78f9c8600000000, + 0x1750124700000000, 0x1636f0de00000000, 0xd6e97e1f00000000, + 0x55fc453600000000, 0x9523cbf700000000, 0x9445296e00000000, + 0x549aa7af00000000, 0x926e5f3c00000000, 0x52b1d1fd00000000, + 0x53d7336400000000, 0x9308bda500000000, 0x101d868c00000000, + 0xd0c2084d00000000, 0xd1a4ead400000000, 0x117b641500000000, + 0x1c4b6a2800000000, 0xdc94e4e900000000, 0xddf2067000000000, + 0x1d2d88b100000000, 0x9e38b39800000000, 0x5ee73d5900000000, + 0x5f81dfc000000000, 0x9f5e510100000000, 0x59aaa99200000000, + 0x9975275300000000, 0x9813c5ca00000000, 0x58cc4b0b00000000, + 0xdbd9702200000000, 0x1b06fee300000000, 0x1a601c7a00000000, + 0xdabf92bb00000000, 0xef1948d600000000, 0x2fc6c61700000000, + 0x2ea0248e00000000, 0xee7faa4f00000000, 0x6d6a916600000000, + 0xadb51fa700000000, 0xacd3fd3e00000000, 0x6c0c73ff00000000, + 0xaaf88b6c00000000, 0x6a2705ad00000000, 0x6b41e73400000000, + 0xab9e69f500000000, 0x288b52dc00000000, 0xe854dc1d00000000, + 0xe9323e8400000000, 0x29edb04500000000, 0x24ddbe7800000000, + 0xe40230b900000000, 0xe564d22000000000, 0x25bb5ce100000000, + 0xa6ae67c800000000, 0x6671e90900000000, 0x67170b9000000000, + 0xa7c8855100000000, 0x613c7dc200000000, 0xa1e3f30300000000, + 0xa085119a00000000, 0x605a9f5b00000000, 0xe34fa47200000000, + 0x23902ab300000000, 0x22f6c82a00000000, 0xe22946eb00000000, + 0x3896d45000000000, 0xf8495a9100000000, 0xf92fb80800000000, + 0x39f036c900000000, 0xbae50de000000000, 0x7a3a832100000000, + 0x7b5c61b800000000, 0xbb83ef7900000000, 0x7d7717ea00000000, + 0xbda8992b00000000, 0xbcce7bb200000000, 0x7c11f57300000000, + 0xff04ce5a00000000, 0x3fdb409b00000000, 0x3ebda20200000000, + 0xfe622cc300000000, 0xf35222fe00000000, 0x338dac3f00000000, + 0x32eb4ea600000000, 0xf234c06700000000, 0x7121fb4e00000000, + 0xb1fe758f00000000, 0xb098971600000000, 0x704719d700000000, + 0xb6b3e14400000000, 0x766c6f8500000000, 0x770a8d1c00000000, + 0xb7d503dd00000000, 0x34c038f400000000, 0xf41fb63500000000, + 0xf57954ac00000000, 0x35a6da6d00000000, 0x9f35e17700000000, + 0x5fea6fb600000000, 0x5e8c8d2f00000000, 0x9e5303ee00000000, + 0x1d4638c700000000, 0xdd99b60600000000, 0xdcff549f00000000, + 0x1c20da5e00000000, 0xdad422cd00000000, 0x1a0bac0c00000000, + 0x1b6d4e9500000000, 0xdbb2c05400000000, 0x58a7fb7d00000000, + 0x987875bc00000000, 0x991e972500000000, 0x59c119e400000000, + 0x54f117d900000000, 0x942e991800000000, 0x95487b8100000000, + 0x5597f54000000000, 0xd682ce6900000000, 0x165d40a800000000, + 0x173ba23100000000, 0xd7e42cf000000000, 0x1110d46300000000, + 0xd1cf5aa200000000, 0xd0a9b83b00000000, 0x107636fa00000000, + 0x93630dd300000000, 0x53bc831200000000, 0x52da618b00000000, + 0x9205ef4a00000000, 0x48ba7df100000000, 0x8865f33000000000, + 0x890311a900000000, 0x49dc9f6800000000, 0xcac9a44100000000, + 0x0a162a8000000000, 0x0b70c81900000000, 0xcbaf46d800000000, + 0x0d5bbe4b00000000, 0xcd84308a00000000, 0xcce2d21300000000, + 0x0c3d5cd200000000, 0x8f2867fb00000000, 0x4ff7e93a00000000, + 0x4e910ba300000000, 0x8e4e856200000000, 0x837e8b5f00000000, + 0x43a1059e00000000, 0x42c7e70700000000, 0x821869c600000000, + 0x010d52ef00000000, 0xc1d2dc2e00000000, 0xc0b43eb700000000, + 0x006bb07600000000, 0xc69f48e500000000, 0x0640c62400000000, + 0x072624bd00000000, 0xc7f9aa7c00000000, 0x44ec915500000000, + 0x84331f9400000000, 0x8555fd0d00000000, 0x458a73cc00000000, + 0x702ca9a100000000, 0xb0f3276000000000, 0xb195c5f900000000, + 0x714a4b3800000000, 0xf25f701100000000, 0x3280fed000000000, + 0x33e61c4900000000, 0xf339928800000000, 0x35cd6a1b00000000, + 0xf512e4da00000000, 0xf474064300000000, 0x34ab888200000000, + 0xb7beb3ab00000000, 0x77613d6a00000000, 0x7607dff300000000, + 0xb6d8513200000000, 0xbbe85f0f00000000, 0x7b37d1ce00000000, + 0x7a51335700000000, 0xba8ebd9600000000, 0x399b86bf00000000, + 0xf944087e00000000, 0xf822eae700000000, 0x38fd642600000000, + 0xfe099cb500000000, 0x3ed6127400000000, 0x3fb0f0ed00000000, + 0xff6f7e2c00000000, 0x7c7a450500000000, 0xbca5cbc400000000, + 0xbdc3295d00000000, 0x7d1ca79c00000000, 0xa7a3352700000000, + 0x677cbbe600000000, 0x661a597f00000000, 0xa6c5d7be00000000, + 0x25d0ec9700000000, 0xe50f625600000000, 0xe46980cf00000000, + 0x24b60e0e00000000, 0xe242f69d00000000, 0x229d785c00000000, + 0x23fb9ac500000000, 0xe324140400000000, 0x60312f2d00000000, + 0xa0eea1ec00000000, 0xa188437500000000, 0x6157cdb400000000, + 0x6c67c38900000000, 0xacb84d4800000000, 0xaddeafd100000000, + 0x6d01211000000000, 0xee141a3900000000, 0x2ecb94f800000000, + 0x2fad766100000000, 0xef72f8a000000000, 0x2986003300000000, + 0xe9598ef200000000, 0xe83f6c6b00000000, 0x28e0e2aa00000000, + 0xabf5d98300000000, 0x6b2a574200000000, 0x6a4cb5db00000000, + 0xaa933b1a00000000}, + {0x0000000000000000, 0x6f4ca59b00000000, 0x9f9e3bec00000000, + 0xf0d29e7700000000, 0x7f3b060300000000, 0x1077a39800000000, + 0xe0a53def00000000, 0x8fe9987400000000, 0xfe760c0600000000, + 0x913aa99d00000000, 0x61e837ea00000000, 0x0ea4927100000000, + 0x814d0a0500000000, 0xee01af9e00000000, 0x1ed331e900000000, + 0x719f947200000000, 0xfced180c00000000, 0x93a1bd9700000000, + 0x637323e000000000, 0x0c3f867b00000000, 0x83d61e0f00000000, + 0xec9abb9400000000, 0x1c4825e300000000, 0x7304807800000000, + 0x029b140a00000000, 0x6dd7b19100000000, 0x9d052fe600000000, + 0xf2498a7d00000000, 0x7da0120900000000, 0x12ecb79200000000, + 0xe23e29e500000000, 0x8d728c7e00000000, 0xf8db311800000000, + 0x9797948300000000, 0x67450af400000000, 0x0809af6f00000000, + 0x87e0371b00000000, 0xe8ac928000000000, 0x187e0cf700000000, + 0x7732a96c00000000, 0x06ad3d1e00000000, 0x69e1988500000000, + 0x993306f200000000, 0xf67fa36900000000, 0x79963b1d00000000, + 0x16da9e8600000000, 0xe60800f100000000, 0x8944a56a00000000, + 0x0436291400000000, 0x6b7a8c8f00000000, 0x9ba812f800000000, + 0xf4e4b76300000000, 0x7b0d2f1700000000, 0x14418a8c00000000, + 0xe49314fb00000000, 0x8bdfb16000000000, 0xfa40251200000000, + 0x950c808900000000, 0x65de1efe00000000, 0x0a92bb6500000000, + 0x857b231100000000, 0xea37868a00000000, 0x1ae518fd00000000, + 0x75a9bd6600000000, 0xf0b7633000000000, 0x9ffbc6ab00000000, + 0x6f2958dc00000000, 0x0065fd4700000000, 0x8f8c653300000000, + 0xe0c0c0a800000000, 0x10125edf00000000, 0x7f5efb4400000000, + 0x0ec16f3600000000, 0x618dcaad00000000, 0x915f54da00000000, + 0xfe13f14100000000, 0x71fa693500000000, 0x1eb6ccae00000000, + 0xee6452d900000000, 0x8128f74200000000, 0x0c5a7b3c00000000, + 0x6316dea700000000, 0x93c440d000000000, 0xfc88e54b00000000, + 0x73617d3f00000000, 0x1c2dd8a400000000, 0xecff46d300000000, + 0x83b3e34800000000, 0xf22c773a00000000, 0x9d60d2a100000000, + 0x6db24cd600000000, 0x02fee94d00000000, 0x8d17713900000000, + 0xe25bd4a200000000, 0x12894ad500000000, 0x7dc5ef4e00000000, + 0x086c522800000000, 0x6720f7b300000000, 0x97f269c400000000, + 0xf8becc5f00000000, 0x7757542b00000000, 0x181bf1b000000000, + 0xe8c96fc700000000, 0x8785ca5c00000000, 0xf61a5e2e00000000, + 0x9956fbb500000000, 0x698465c200000000, 0x06c8c05900000000, + 0x8921582d00000000, 0xe66dfdb600000000, 0x16bf63c100000000, + 0x79f3c65a00000000, 0xf4814a2400000000, 0x9bcdefbf00000000, + 0x6b1f71c800000000, 0x0453d45300000000, 0x8bba4c2700000000, + 0xe4f6e9bc00000000, 0x142477cb00000000, 0x7b68d25000000000, + 0x0af7462200000000, 0x65bbe3b900000000, 0x95697dce00000000, + 0xfa25d85500000000, 0x75cc402100000000, 0x1a80e5ba00000000, + 0xea527bcd00000000, 0x851ede5600000000, 0xe06fc76000000000, + 0x8f2362fb00000000, 0x7ff1fc8c00000000, 0x10bd591700000000, + 0x9f54c16300000000, 0xf01864f800000000, 0x00cafa8f00000000, + 0x6f865f1400000000, 0x1e19cb6600000000, 0x71556efd00000000, + 0x8187f08a00000000, 0xeecb551100000000, 0x6122cd6500000000, + 0x0e6e68fe00000000, 0xfebcf68900000000, 0x91f0531200000000, + 0x1c82df6c00000000, 0x73ce7af700000000, 0x831ce48000000000, + 0xec50411b00000000, 0x63b9d96f00000000, 0x0cf57cf400000000, + 0xfc27e28300000000, 0x936b471800000000, 0xe2f4d36a00000000, + 0x8db876f100000000, 0x7d6ae88600000000, 0x12264d1d00000000, + 0x9dcfd56900000000, 0xf28370f200000000, 0x0251ee8500000000, + 0x6d1d4b1e00000000, 0x18b4f67800000000, 0x77f853e300000000, + 0x872acd9400000000, 0xe866680f00000000, 0x678ff07b00000000, + 0x08c355e000000000, 0xf811cb9700000000, 0x975d6e0c00000000, + 0xe6c2fa7e00000000, 0x898e5fe500000000, 0x795cc19200000000, + 0x1610640900000000, 0x99f9fc7d00000000, 0xf6b559e600000000, + 0x0667c79100000000, 0x692b620a00000000, 0xe459ee7400000000, + 0x8b154bef00000000, 0x7bc7d59800000000, 0x148b700300000000, + 0x9b62e87700000000, 0xf42e4dec00000000, 0x04fcd39b00000000, + 0x6bb0760000000000, 0x1a2fe27200000000, 0x756347e900000000, + 0x85b1d99e00000000, 0xeafd7c0500000000, 0x6514e47100000000, + 0x0a5841ea00000000, 0xfa8adf9d00000000, 0x95c67a0600000000, + 0x10d8a45000000000, 0x7f9401cb00000000, 0x8f469fbc00000000, + 0xe00a3a2700000000, 0x6fe3a25300000000, 0x00af07c800000000, + 0xf07d99bf00000000, 0x9f313c2400000000, 0xeeaea85600000000, + 0x81e20dcd00000000, 0x713093ba00000000, 0x1e7c362100000000, + 0x9195ae5500000000, 0xfed90bce00000000, 0x0e0b95b900000000, + 0x6147302200000000, 0xec35bc5c00000000, 0x837919c700000000, + 0x73ab87b000000000, 0x1ce7222b00000000, 0x930eba5f00000000, + 0xfc421fc400000000, 0x0c9081b300000000, 0x63dc242800000000, + 0x1243b05a00000000, 0x7d0f15c100000000, 0x8ddd8bb600000000, + 0xe2912e2d00000000, 0x6d78b65900000000, 0x023413c200000000, + 0xf2e68db500000000, 0x9daa282e00000000, 0xe803954800000000, + 0x874f30d300000000, 0x779daea400000000, 0x18d10b3f00000000, + 0x9738934b00000000, 0xf87436d000000000, 0x08a6a8a700000000, + 0x67ea0d3c00000000, 0x1675994e00000000, 0x79393cd500000000, + 0x89eba2a200000000, 0xe6a7073900000000, 0x694e9f4d00000000, + 0x06023ad600000000, 0xf6d0a4a100000000, 0x999c013a00000000, + 0x14ee8d4400000000, 0x7ba228df00000000, 0x8b70b6a800000000, + 0xe43c133300000000, 0x6bd58b4700000000, 0x04992edc00000000, + 0xf44bb0ab00000000, 0x9b07153000000000, 0xea98814200000000, + 0x85d424d900000000, 0x7506baae00000000, 0x1a4a1f3500000000, + 0x95a3874100000000, 0xfaef22da00000000, 0x0a3dbcad00000000, + 0x6571193600000000}, + {0x0000000000000000, 0x85d996dd00000000, 0x4bb55c6000000000, + 0xce6ccabd00000000, 0x966ab9c000000000, 0x13b32f1d00000000, + 0xdddfe5a000000000, 0x5806737d00000000, 0x6dd3035a00000000, + 0xe80a958700000000, 0x26665f3a00000000, 0xa3bfc9e700000000, + 0xfbb9ba9a00000000, 0x7e602c4700000000, 0xb00ce6fa00000000, + 0x35d5702700000000, 0xdaa607b400000000, 0x5f7f916900000000, + 0x91135bd400000000, 0x14cacd0900000000, 0x4cccbe7400000000, + 0xc91528a900000000, 0x0779e21400000000, 0x82a074c900000000, + 0xb77504ee00000000, 0x32ac923300000000, 0xfcc0588e00000000, + 0x7919ce5300000000, 0x211fbd2e00000000, 0xa4c62bf300000000, + 0x6aaae14e00000000, 0xef73779300000000, 0xf54b7eb300000000, + 0x7092e86e00000000, 0xbefe22d300000000, 0x3b27b40e00000000, + 0x6321c77300000000, 0xe6f851ae00000000, 0x28949b1300000000, + 0xad4d0dce00000000, 0x98987de900000000, 0x1d41eb3400000000, + 0xd32d218900000000, 0x56f4b75400000000, 0x0ef2c42900000000, + 0x8b2b52f400000000, 0x4547984900000000, 0xc09e0e9400000000, + 0x2fed790700000000, 0xaa34efda00000000, 0x6458256700000000, + 0xe181b3ba00000000, 0xb987c0c700000000, 0x3c5e561a00000000, + 0xf2329ca700000000, 0x77eb0a7a00000000, 0x423e7a5d00000000, + 0xc7e7ec8000000000, 0x098b263d00000000, 0x8c52b0e000000000, + 0xd454c39d00000000, 0x518d554000000000, 0x9fe19ffd00000000, + 0x1a38092000000000, 0xab918dbd00000000, 0x2e481b6000000000, + 0xe024d1dd00000000, 0x65fd470000000000, 0x3dfb347d00000000, + 0xb822a2a000000000, 0x764e681d00000000, 0xf397fec000000000, + 0xc6428ee700000000, 0x439b183a00000000, 0x8df7d28700000000, + 0x082e445a00000000, 0x5028372700000000, 0xd5f1a1fa00000000, + 0x1b9d6b4700000000, 0x9e44fd9a00000000, 0x71378a0900000000, + 0xf4ee1cd400000000, 0x3a82d66900000000, 0xbf5b40b400000000, + 0xe75d33c900000000, 0x6284a51400000000, 0xace86fa900000000, + 0x2931f97400000000, 0x1ce4895300000000, 0x993d1f8e00000000, + 0x5751d53300000000, 0xd28843ee00000000, 0x8a8e309300000000, + 0x0f57a64e00000000, 0xc13b6cf300000000, 0x44e2fa2e00000000, + 0x5edaf30e00000000, 0xdb0365d300000000, 0x156faf6e00000000, + 0x90b639b300000000, 0xc8b04ace00000000, 0x4d69dc1300000000, + 0x830516ae00000000, 0x06dc807300000000, 0x3309f05400000000, + 0xb6d0668900000000, 0x78bcac3400000000, 0xfd653ae900000000, + 0xa563499400000000, 0x20badf4900000000, 0xeed615f400000000, + 0x6b0f832900000000, 0x847cf4ba00000000, 0x01a5626700000000, + 0xcfc9a8da00000000, 0x4a103e0700000000, 0x12164d7a00000000, + 0x97cfdba700000000, 0x59a3111a00000000, 0xdc7a87c700000000, + 0xe9aff7e000000000, 0x6c76613d00000000, 0xa21aab8000000000, + 0x27c33d5d00000000, 0x7fc54e2000000000, 0xfa1cd8fd00000000, + 0x3470124000000000, 0xb1a9849d00000000, 0x17256aa000000000, + 0x92fcfc7d00000000, 0x5c9036c000000000, 0xd949a01d00000000, + 0x814fd36000000000, 0x049645bd00000000, 0xcafa8f0000000000, + 0x4f2319dd00000000, 0x7af669fa00000000, 0xff2fff2700000000, + 0x3143359a00000000, 0xb49aa34700000000, 0xec9cd03a00000000, + 0x694546e700000000, 0xa7298c5a00000000, 0x22f01a8700000000, + 0xcd836d1400000000, 0x485afbc900000000, 0x8636317400000000, + 0x03efa7a900000000, 0x5be9d4d400000000, 0xde30420900000000, + 0x105c88b400000000, 0x95851e6900000000, 0xa0506e4e00000000, + 0x2589f89300000000, 0xebe5322e00000000, 0x6e3ca4f300000000, + 0x363ad78e00000000, 0xb3e3415300000000, 0x7d8f8bee00000000, + 0xf8561d3300000000, 0xe26e141300000000, 0x67b782ce00000000, + 0xa9db487300000000, 0x2c02deae00000000, 0x7404add300000000, + 0xf1dd3b0e00000000, 0x3fb1f1b300000000, 0xba68676e00000000, + 0x8fbd174900000000, 0x0a64819400000000, 0xc4084b2900000000, + 0x41d1ddf400000000, 0x19d7ae8900000000, 0x9c0e385400000000, + 0x5262f2e900000000, 0xd7bb643400000000, 0x38c813a700000000, + 0xbd11857a00000000, 0x737d4fc700000000, 0xf6a4d91a00000000, + 0xaea2aa6700000000, 0x2b7b3cba00000000, 0xe517f60700000000, + 0x60ce60da00000000, 0x551b10fd00000000, 0xd0c2862000000000, + 0x1eae4c9d00000000, 0x9b77da4000000000, 0xc371a93d00000000, + 0x46a83fe000000000, 0x88c4f55d00000000, 0x0d1d638000000000, + 0xbcb4e71d00000000, 0x396d71c000000000, 0xf701bb7d00000000, + 0x72d82da000000000, 0x2ade5edd00000000, 0xaf07c80000000000, + 0x616b02bd00000000, 0xe4b2946000000000, 0xd167e44700000000, + 0x54be729a00000000, 0x9ad2b82700000000, 0x1f0b2efa00000000, + 0x470d5d8700000000, 0xc2d4cb5a00000000, 0x0cb801e700000000, + 0x8961973a00000000, 0x6612e0a900000000, 0xe3cb767400000000, + 0x2da7bcc900000000, 0xa87e2a1400000000, 0xf078596900000000, + 0x75a1cfb400000000, 0xbbcd050900000000, 0x3e1493d400000000, + 0x0bc1e3f300000000, 0x8e18752e00000000, 0x4074bf9300000000, + 0xc5ad294e00000000, 0x9dab5a3300000000, 0x1872ccee00000000, + 0xd61e065300000000, 0x53c7908e00000000, 0x49ff99ae00000000, + 0xcc260f7300000000, 0x024ac5ce00000000, 0x8793531300000000, + 0xdf95206e00000000, 0x5a4cb6b300000000, 0x94207c0e00000000, + 0x11f9ead300000000, 0x242c9af400000000, 0xa1f50c2900000000, + 0x6f99c69400000000, 0xea40504900000000, 0xb246233400000000, + 0x379fb5e900000000, 0xf9f37f5400000000, 0x7c2ae98900000000, + 0x93599e1a00000000, 0x168008c700000000, 0xd8ecc27a00000000, + 0x5d3554a700000000, 0x053327da00000000, 0x80eab10700000000, + 0x4e867bba00000000, 0xcb5fed6700000000, 0xfe8a9d4000000000, + 0x7b530b9d00000000, 0xb53fc12000000000, 0x30e657fd00000000, + 0x68e0248000000000, 0xed39b25d00000000, 0x235578e000000000, + 0xa68cee3d00000000}, + {0x0000000000000000, 0x76e10f9d00000000, 0xadc46ee100000000, + 0xdb25617c00000000, 0x1b8fac1900000000, 0x6d6ea38400000000, + 0xb64bc2f800000000, 0xc0aacd6500000000, 0x361e593300000000, + 0x40ff56ae00000000, 0x9bda37d200000000, 0xed3b384f00000000, + 0x2d91f52a00000000, 0x5b70fab700000000, 0x80559bcb00000000, + 0xf6b4945600000000, 0x6c3cb26600000000, 0x1addbdfb00000000, + 0xc1f8dc8700000000, 0xb719d31a00000000, 0x77b31e7f00000000, + 0x015211e200000000, 0xda77709e00000000, 0xac967f0300000000, + 0x5a22eb5500000000, 0x2cc3e4c800000000, 0xf7e685b400000000, + 0x81078a2900000000, 0x41ad474c00000000, 0x374c48d100000000, + 0xec6929ad00000000, 0x9a88263000000000, 0xd87864cd00000000, + 0xae996b5000000000, 0x75bc0a2c00000000, 0x035d05b100000000, + 0xc3f7c8d400000000, 0xb516c74900000000, 0x6e33a63500000000, + 0x18d2a9a800000000, 0xee663dfe00000000, 0x9887326300000000, + 0x43a2531f00000000, 0x35435c8200000000, 0xf5e991e700000000, + 0x83089e7a00000000, 0x582dff0600000000, 0x2eccf09b00000000, + 0xb444d6ab00000000, 0xc2a5d93600000000, 0x1980b84a00000000, + 0x6f61b7d700000000, 0xafcb7ab200000000, 0xd92a752f00000000, + 0x020f145300000000, 0x74ee1bce00000000, 0x825a8f9800000000, + 0xf4bb800500000000, 0x2f9ee17900000000, 0x597feee400000000, + 0x99d5238100000000, 0xef342c1c00000000, 0x34114d6000000000, + 0x42f042fd00000000, 0xf1f7b94100000000, 0x8716b6dc00000000, + 0x5c33d7a000000000, 0x2ad2d83d00000000, 0xea78155800000000, + 0x9c991ac500000000, 0x47bc7bb900000000, 0x315d742400000000, + 0xc7e9e07200000000, 0xb108efef00000000, 0x6a2d8e9300000000, + 0x1ccc810e00000000, 0xdc664c6b00000000, 0xaa8743f600000000, + 0x71a2228a00000000, 0x07432d1700000000, 0x9dcb0b2700000000, + 0xeb2a04ba00000000, 0x300f65c600000000, 0x46ee6a5b00000000, + 0x8644a73e00000000, 0xf0a5a8a300000000, 0x2b80c9df00000000, + 0x5d61c64200000000, 0xabd5521400000000, 0xdd345d8900000000, + 0x06113cf500000000, 0x70f0336800000000, 0xb05afe0d00000000, + 0xc6bbf19000000000, 0x1d9e90ec00000000, 0x6b7f9f7100000000, + 0x298fdd8c00000000, 0x5f6ed21100000000, 0x844bb36d00000000, + 0xf2aabcf000000000, 0x3200719500000000, 0x44e17e0800000000, + 0x9fc41f7400000000, 0xe92510e900000000, 0x1f9184bf00000000, + 0x69708b2200000000, 0xb255ea5e00000000, 0xc4b4e5c300000000, + 0x041e28a600000000, 0x72ff273b00000000, 0xa9da464700000000, + 0xdf3b49da00000000, 0x45b36fea00000000, 0x3352607700000000, + 0xe877010b00000000, 0x9e960e9600000000, 0x5e3cc3f300000000, + 0x28ddcc6e00000000, 0xf3f8ad1200000000, 0x8519a28f00000000, + 0x73ad36d900000000, 0x054c394400000000, 0xde69583800000000, + 0xa88857a500000000, 0x68229ac000000000, 0x1ec3955d00000000, + 0xc5e6f42100000000, 0xb307fbbc00000000, 0xe2ef738300000000, + 0x940e7c1e00000000, 0x4f2b1d6200000000, 0x39ca12ff00000000, + 0xf960df9a00000000, 0x8f81d00700000000, 0x54a4b17b00000000, + 0x2245bee600000000, 0xd4f12ab000000000, 0xa210252d00000000, + 0x7935445100000000, 0x0fd44bcc00000000, 0xcf7e86a900000000, + 0xb99f893400000000, 0x62bae84800000000, 0x145be7d500000000, + 0x8ed3c1e500000000, 0xf832ce7800000000, 0x2317af0400000000, + 0x55f6a09900000000, 0x955c6dfc00000000, 0xe3bd626100000000, + 0x3898031d00000000, 0x4e790c8000000000, 0xb8cd98d600000000, + 0xce2c974b00000000, 0x1509f63700000000, 0x63e8f9aa00000000, + 0xa34234cf00000000, 0xd5a33b5200000000, 0x0e865a2e00000000, + 0x786755b300000000, 0x3a97174e00000000, 0x4c7618d300000000, + 0x975379af00000000, 0xe1b2763200000000, 0x2118bb5700000000, + 0x57f9b4ca00000000, 0x8cdcd5b600000000, 0xfa3dda2b00000000, + 0x0c894e7d00000000, 0x7a6841e000000000, 0xa14d209c00000000, + 0xd7ac2f0100000000, 0x1706e26400000000, 0x61e7edf900000000, + 0xbac28c8500000000, 0xcc23831800000000, 0x56aba52800000000, + 0x204aaab500000000, 0xfb6fcbc900000000, 0x8d8ec45400000000, + 0x4d24093100000000, 0x3bc506ac00000000, 0xe0e067d000000000, + 0x9601684d00000000, 0x60b5fc1b00000000, 0x1654f38600000000, + 0xcd7192fa00000000, 0xbb909d6700000000, 0x7b3a500200000000, + 0x0ddb5f9f00000000, 0xd6fe3ee300000000, 0xa01f317e00000000, + 0x1318cac200000000, 0x65f9c55f00000000, 0xbedca42300000000, + 0xc83dabbe00000000, 0x089766db00000000, 0x7e76694600000000, + 0xa553083a00000000, 0xd3b207a700000000, 0x250693f100000000, + 0x53e79c6c00000000, 0x88c2fd1000000000, 0xfe23f28d00000000, + 0x3e893fe800000000, 0x4868307500000000, 0x934d510900000000, + 0xe5ac5e9400000000, 0x7f2478a400000000, 0x09c5773900000000, + 0xd2e0164500000000, 0xa40119d800000000, 0x64abd4bd00000000, + 0x124adb2000000000, 0xc96fba5c00000000, 0xbf8eb5c100000000, + 0x493a219700000000, 0x3fdb2e0a00000000, 0xe4fe4f7600000000, + 0x921f40eb00000000, 0x52b58d8e00000000, 0x2454821300000000, + 0xff71e36f00000000, 0x8990ecf200000000, 0xcb60ae0f00000000, + 0xbd81a19200000000, 0x66a4c0ee00000000, 0x1045cf7300000000, + 0xd0ef021600000000, 0xa60e0d8b00000000, 0x7d2b6cf700000000, + 0x0bca636a00000000, 0xfd7ef73c00000000, 0x8b9ff8a100000000, + 0x50ba99dd00000000, 0x265b964000000000, 0xe6f15b2500000000, + 0x901054b800000000, 0x4b3535c400000000, 0x3dd43a5900000000, + 0xa75c1c6900000000, 0xd1bd13f400000000, 0x0a98728800000000, + 0x7c797d1500000000, 0xbcd3b07000000000, 0xca32bfed00000000, + 0x1117de9100000000, 0x67f6d10c00000000, 0x9142455a00000000, + 0xe7a34ac700000000, 0x3c862bbb00000000, 0x4a67242600000000, + 0x8acde94300000000, 0xfc2ce6de00000000, 0x270987a200000000, + 0x51e8883f00000000}, + {0x0000000000000000, 0xe8dbfbb900000000, 0x91b186a800000000, + 0x796a7d1100000000, 0x63657c8a00000000, 0x8bbe873300000000, + 0xf2d4fa2200000000, 0x1a0f019b00000000, 0x87cc89cf00000000, + 0x6f17727600000000, 0x167d0f6700000000, 0xfea6f4de00000000, + 0xe4a9f54500000000, 0x0c720efc00000000, 0x751873ed00000000, + 0x9dc3885400000000, 0x4f9f624400000000, 0xa74499fd00000000, + 0xde2ee4ec00000000, 0x36f51f5500000000, 0x2cfa1ece00000000, + 0xc421e57700000000, 0xbd4b986600000000, 0x559063df00000000, + 0xc853eb8b00000000, 0x2088103200000000, 0x59e26d2300000000, + 0xb139969a00000000, 0xab36970100000000, 0x43ed6cb800000000, + 0x3a8711a900000000, 0xd25cea1000000000, 0x9e3ec58800000000, + 0x76e53e3100000000, 0x0f8f432000000000, 0xe754b89900000000, + 0xfd5bb90200000000, 0x158042bb00000000, 0x6cea3faa00000000, + 0x8431c41300000000, 0x19f24c4700000000, 0xf129b7fe00000000, + 0x8843caef00000000, 0x6098315600000000, 0x7a9730cd00000000, + 0x924ccb7400000000, 0xeb26b66500000000, 0x03fd4ddc00000000, + 0xd1a1a7cc00000000, 0x397a5c7500000000, 0x4010216400000000, + 0xa8cbdadd00000000, 0xb2c4db4600000000, 0x5a1f20ff00000000, + 0x23755dee00000000, 0xcbaea65700000000, 0x566d2e0300000000, + 0xbeb6d5ba00000000, 0xc7dca8ab00000000, 0x2f07531200000000, + 0x3508528900000000, 0xddd3a93000000000, 0xa4b9d42100000000, + 0x4c622f9800000000, 0x7d7bfbca00000000, 0x95a0007300000000, + 0xecca7d6200000000, 0x041186db00000000, 0x1e1e874000000000, + 0xf6c57cf900000000, 0x8faf01e800000000, 0x6774fa5100000000, + 0xfab7720500000000, 0x126c89bc00000000, 0x6b06f4ad00000000, + 0x83dd0f1400000000, 0x99d20e8f00000000, 0x7109f53600000000, + 0x0863882700000000, 0xe0b8739e00000000, 0x32e4998e00000000, + 0xda3f623700000000, 0xa3551f2600000000, 0x4b8ee49f00000000, + 0x5181e50400000000, 0xb95a1ebd00000000, 0xc03063ac00000000, + 0x28eb981500000000, 0xb528104100000000, 0x5df3ebf800000000, + 0x249996e900000000, 0xcc426d5000000000, 0xd64d6ccb00000000, + 0x3e96977200000000, 0x47fcea6300000000, 0xaf2711da00000000, + 0xe3453e4200000000, 0x0b9ec5fb00000000, 0x72f4b8ea00000000, + 0x9a2f435300000000, 0x802042c800000000, 0x68fbb97100000000, + 0x1191c46000000000, 0xf94a3fd900000000, 0x6489b78d00000000, + 0x8c524c3400000000, 0xf538312500000000, 0x1de3ca9c00000000, + 0x07eccb0700000000, 0xef3730be00000000, 0x965d4daf00000000, + 0x7e86b61600000000, 0xacda5c0600000000, 0x4401a7bf00000000, + 0x3d6bdaae00000000, 0xd5b0211700000000, 0xcfbf208c00000000, + 0x2764db3500000000, 0x5e0ea62400000000, 0xb6d55d9d00000000, + 0x2b16d5c900000000, 0xc3cd2e7000000000, 0xbaa7536100000000, + 0x527ca8d800000000, 0x4873a94300000000, 0xa0a852fa00000000, + 0xd9c22feb00000000, 0x3119d45200000000, 0xbbf0874e00000000, + 0x532b7cf700000000, 0x2a4101e600000000, 0xc29afa5f00000000, + 0xd895fbc400000000, 0x304e007d00000000, 0x49247d6c00000000, + 0xa1ff86d500000000, 0x3c3c0e8100000000, 0xd4e7f53800000000, + 0xad8d882900000000, 0x4556739000000000, 0x5f59720b00000000, + 0xb78289b200000000, 0xcee8f4a300000000, 0x26330f1a00000000, + 0xf46fe50a00000000, 0x1cb41eb300000000, 0x65de63a200000000, + 0x8d05981b00000000, 0x970a998000000000, 0x7fd1623900000000, + 0x06bb1f2800000000, 0xee60e49100000000, 0x73a36cc500000000, + 0x9b78977c00000000, 0xe212ea6d00000000, 0x0ac911d400000000, + 0x10c6104f00000000, 0xf81debf600000000, 0x817796e700000000, + 0x69ac6d5e00000000, 0x25ce42c600000000, 0xcd15b97f00000000, + 0xb47fc46e00000000, 0x5ca43fd700000000, 0x46ab3e4c00000000, + 0xae70c5f500000000, 0xd71ab8e400000000, 0x3fc1435d00000000, + 0xa202cb0900000000, 0x4ad930b000000000, 0x33b34da100000000, + 0xdb68b61800000000, 0xc167b78300000000, 0x29bc4c3a00000000, + 0x50d6312b00000000, 0xb80dca9200000000, 0x6a51208200000000, + 0x828adb3b00000000, 0xfbe0a62a00000000, 0x133b5d9300000000, + 0x09345c0800000000, 0xe1efa7b100000000, 0x9885daa000000000, + 0x705e211900000000, 0xed9da94d00000000, 0x054652f400000000, + 0x7c2c2fe500000000, 0x94f7d45c00000000, 0x8ef8d5c700000000, + 0x66232e7e00000000, 0x1f49536f00000000, 0xf792a8d600000000, + 0xc68b7c8400000000, 0x2e50873d00000000, 0x573afa2c00000000, + 0xbfe1019500000000, 0xa5ee000e00000000, 0x4d35fbb700000000, + 0x345f86a600000000, 0xdc847d1f00000000, 0x4147f54b00000000, + 0xa99c0ef200000000, 0xd0f673e300000000, 0x382d885a00000000, + 0x222289c100000000, 0xcaf9727800000000, 0xb3930f6900000000, + 0x5b48f4d000000000, 0x89141ec000000000, 0x61cfe57900000000, + 0x18a5986800000000, 0xf07e63d100000000, 0xea71624a00000000, + 0x02aa99f300000000, 0x7bc0e4e200000000, 0x931b1f5b00000000, + 0x0ed8970f00000000, 0xe6036cb600000000, 0x9f6911a700000000, + 0x77b2ea1e00000000, 0x6dbdeb8500000000, 0x8566103c00000000, + 0xfc0c6d2d00000000, 0x14d7969400000000, 0x58b5b90c00000000, + 0xb06e42b500000000, 0xc9043fa400000000, 0x21dfc41d00000000, + 0x3bd0c58600000000, 0xd30b3e3f00000000, 0xaa61432e00000000, + 0x42bab89700000000, 0xdf7930c300000000, 0x37a2cb7a00000000, + 0x4ec8b66b00000000, 0xa6134dd200000000, 0xbc1c4c4900000000, + 0x54c7b7f000000000, 0x2dadcae100000000, 0xc576315800000000, + 0x172adb4800000000, 0xfff120f100000000, 0x869b5de000000000, + 0x6e40a65900000000, 0x744fa7c200000000, 0x9c945c7b00000000, + 0xe5fe216a00000000, 0x0d25dad300000000, 0x90e6528700000000, + 0x783da93e00000000, 0x0157d42f00000000, 0xe98c2f9600000000, + 0xf3832e0d00000000, 0x1b58d5b400000000, 0x6232a8a500000000, + 0x8ae9531c00000000}, + {0x0000000000000000, 0x919168ae00000000, 0x6325a08700000000, + 0xf2b4c82900000000, 0x874c31d400000000, 0x16dd597a00000000, + 0xe469915300000000, 0x75f8f9fd00000000, 0x4f9f137300000000, + 0xde0e7bdd00000000, 0x2cbab3f400000000, 0xbd2bdb5a00000000, + 0xc8d322a700000000, 0x59424a0900000000, 0xabf6822000000000, + 0x3a67ea8e00000000, 0x9e3e27e600000000, 0x0faf4f4800000000, + 0xfd1b876100000000, 0x6c8aefcf00000000, 0x1972163200000000, + 0x88e37e9c00000000, 0x7a57b6b500000000, 0xebc6de1b00000000, + 0xd1a1349500000000, 0x40305c3b00000000, 0xb284941200000000, + 0x2315fcbc00000000, 0x56ed054100000000, 0xc77c6def00000000, + 0x35c8a5c600000000, 0xa459cd6800000000, 0x7d7b3f1700000000, + 0xecea57b900000000, 0x1e5e9f9000000000, 0x8fcff73e00000000, + 0xfa370ec300000000, 0x6ba6666d00000000, 0x9912ae4400000000, + 0x0883c6ea00000000, 0x32e42c6400000000, 0xa37544ca00000000, + 0x51c18ce300000000, 0xc050e44d00000000, 0xb5a81db000000000, + 0x2439751e00000000, 0xd68dbd3700000000, 0x471cd59900000000, + 0xe34518f100000000, 0x72d4705f00000000, 0x8060b87600000000, + 0x11f1d0d800000000, 0x6409292500000000, 0xf598418b00000000, + 0x072c89a200000000, 0x96bde10c00000000, 0xacda0b8200000000, + 0x3d4b632c00000000, 0xcfffab0500000000, 0x5e6ec3ab00000000, + 0x2b963a5600000000, 0xba0752f800000000, 0x48b39ad100000000, + 0xd922f27f00000000, 0xfaf67e2e00000000, 0x6b67168000000000, + 0x99d3dea900000000, 0x0842b60700000000, 0x7dba4ffa00000000, + 0xec2b275400000000, 0x1e9fef7d00000000, 0x8f0e87d300000000, + 0xb5696d5d00000000, 0x24f805f300000000, 0xd64ccdda00000000, + 0x47dda57400000000, 0x32255c8900000000, 0xa3b4342700000000, + 0x5100fc0e00000000, 0xc09194a000000000, 0x64c859c800000000, + 0xf559316600000000, 0x07edf94f00000000, 0x967c91e100000000, + 0xe384681c00000000, 0x721500b200000000, 0x80a1c89b00000000, + 0x1130a03500000000, 0x2b574abb00000000, 0xbac6221500000000, + 0x4872ea3c00000000, 0xd9e3829200000000, 0xac1b7b6f00000000, + 0x3d8a13c100000000, 0xcf3edbe800000000, 0x5eafb34600000000, + 0x878d413900000000, 0x161c299700000000, 0xe4a8e1be00000000, + 0x7539891000000000, 0x00c170ed00000000, 0x9150184300000000, + 0x63e4d06a00000000, 0xf275b8c400000000, 0xc812524a00000000, + 0x59833ae400000000, 0xab37f2cd00000000, 0x3aa69a6300000000, + 0x4f5e639e00000000, 0xdecf0b3000000000, 0x2c7bc31900000000, + 0xbdeaabb700000000, 0x19b366df00000000, 0x88220e7100000000, + 0x7a96c65800000000, 0xeb07aef600000000, 0x9eff570b00000000, + 0x0f6e3fa500000000, 0xfddaf78c00000000, 0x6c4b9f2200000000, + 0x562c75ac00000000, 0xc7bd1d0200000000, 0x3509d52b00000000, + 0xa498bd8500000000, 0xd160447800000000, 0x40f12cd600000000, + 0xb245e4ff00000000, 0x23d48c5100000000, 0xf4edfd5c00000000, + 0x657c95f200000000, 0x97c85ddb00000000, 0x0659357500000000, + 0x73a1cc8800000000, 0xe230a42600000000, 0x10846c0f00000000, + 0x811504a100000000, 0xbb72ee2f00000000, 0x2ae3868100000000, + 0xd8574ea800000000, 0x49c6260600000000, 0x3c3edffb00000000, + 0xadafb75500000000, 0x5f1b7f7c00000000, 0xce8a17d200000000, + 0x6ad3daba00000000, 0xfb42b21400000000, 0x09f67a3d00000000, + 0x9867129300000000, 0xed9feb6e00000000, 0x7c0e83c000000000, + 0x8eba4be900000000, 0x1f2b234700000000, 0x254cc9c900000000, + 0xb4dda16700000000, 0x4669694e00000000, 0xd7f801e000000000, + 0xa200f81d00000000, 0x339190b300000000, 0xc125589a00000000, + 0x50b4303400000000, 0x8996c24b00000000, 0x1807aae500000000, + 0xeab362cc00000000, 0x7b220a6200000000, 0x0edaf39f00000000, + 0x9f4b9b3100000000, 0x6dff531800000000, 0xfc6e3bb600000000, + 0xc609d13800000000, 0x5798b99600000000, 0xa52c71bf00000000, + 0x34bd191100000000, 0x4145e0ec00000000, 0xd0d4884200000000, + 0x2260406b00000000, 0xb3f128c500000000, 0x17a8e5ad00000000, + 0x86398d0300000000, 0x748d452a00000000, 0xe51c2d8400000000, + 0x90e4d47900000000, 0x0175bcd700000000, 0xf3c174fe00000000, + 0x62501c5000000000, 0x5837f6de00000000, 0xc9a69e7000000000, + 0x3b12565900000000, 0xaa833ef700000000, 0xdf7bc70a00000000, + 0x4eeaafa400000000, 0xbc5e678d00000000, 0x2dcf0f2300000000, + 0x0e1b837200000000, 0x9f8aebdc00000000, 0x6d3e23f500000000, + 0xfcaf4b5b00000000, 0x8957b2a600000000, 0x18c6da0800000000, + 0xea72122100000000, 0x7be37a8f00000000, 0x4184900100000000, + 0xd015f8af00000000, 0x22a1308600000000, 0xb330582800000000, + 0xc6c8a1d500000000, 0x5759c97b00000000, 0xa5ed015200000000, + 0x347c69fc00000000, 0x9025a49400000000, 0x01b4cc3a00000000, + 0xf300041300000000, 0x62916cbd00000000, 0x1769954000000000, + 0x86f8fdee00000000, 0x744c35c700000000, 0xe5dd5d6900000000, + 0xdfbab7e700000000, 0x4e2bdf4900000000, 0xbc9f176000000000, + 0x2d0e7fce00000000, 0x58f6863300000000, 0xc967ee9d00000000, + 0x3bd326b400000000, 0xaa424e1a00000000, 0x7360bc6500000000, + 0xe2f1d4cb00000000, 0x10451ce200000000, 0x81d4744c00000000, + 0xf42c8db100000000, 0x65bde51f00000000, 0x97092d3600000000, + 0x0698459800000000, 0x3cffaf1600000000, 0xad6ec7b800000000, + 0x5fda0f9100000000, 0xce4b673f00000000, 0xbbb39ec200000000, + 0x2a22f66c00000000, 0xd8963e4500000000, 0x490756eb00000000, + 0xed5e9b8300000000, 0x7ccff32d00000000, 0x8e7b3b0400000000, + 0x1fea53aa00000000, 0x6a12aa5700000000, 0xfb83c2f900000000, + 0x09370ad000000000, 0x98a6627e00000000, 0xa2c188f000000000, + 0x3350e05e00000000, 0xc1e4287700000000, 0x507540d900000000, + 0x258db92400000000, 0xb41cd18a00000000, 0x46a819a300000000, + 0xd739710d00000000}}; + +#else /* W == 4 */ + +local const z_crc_t FAR crc_braid_table[][256] = { + {0x00000000, 0xccaa009e, 0x4225077d, 0x8e8f07e3, 0x844a0efa, + 0x48e00e64, 0xc66f0987, 0x0ac50919, 0xd3e51bb5, 0x1f4f1b2b, + 0x91c01cc8, 0x5d6a1c56, 0x57af154f, 0x9b0515d1, 0x158a1232, + 0xd92012ac, 0x7cbb312b, 0xb01131b5, 0x3e9e3656, 0xf23436c8, + 0xf8f13fd1, 0x345b3f4f, 0xbad438ac, 0x767e3832, 0xaf5e2a9e, + 0x63f42a00, 0xed7b2de3, 0x21d12d7d, 0x2b142464, 0xe7be24fa, + 0x69312319, 0xa59b2387, 0xf9766256, 0x35dc62c8, 0xbb53652b, + 0x77f965b5, 0x7d3c6cac, 0xb1966c32, 0x3f196bd1, 0xf3b36b4f, + 0x2a9379e3, 0xe639797d, 0x68b67e9e, 0xa41c7e00, 0xaed97719, + 0x62737787, 0xecfc7064, 0x205670fa, 0x85cd537d, 0x496753e3, + 0xc7e85400, 0x0b42549e, 0x01875d87, 0xcd2d5d19, 0x43a25afa, + 0x8f085a64, 0x562848c8, 0x9a824856, 0x140d4fb5, 0xd8a74f2b, + 0xd2624632, 0x1ec846ac, 0x9047414f, 0x5ced41d1, 0x299dc2ed, + 0xe537c273, 0x6bb8c590, 0xa712c50e, 0xadd7cc17, 0x617dcc89, + 0xeff2cb6a, 0x2358cbf4, 0xfa78d958, 0x36d2d9c6, 0xb85dde25, + 0x74f7debb, 0x7e32d7a2, 0xb298d73c, 0x3c17d0df, 0xf0bdd041, + 0x5526f3c6, 0x998cf358, 0x1703f4bb, 0xdba9f425, 0xd16cfd3c, + 0x1dc6fda2, 0x9349fa41, 0x5fe3fadf, 0x86c3e873, 0x4a69e8ed, + 0xc4e6ef0e, 0x084cef90, 0x0289e689, 0xce23e617, 0x40ace1f4, + 0x8c06e16a, 0xd0eba0bb, 0x1c41a025, 0x92cea7c6, 0x5e64a758, + 0x54a1ae41, 0x980baedf, 0x1684a93c, 0xda2ea9a2, 0x030ebb0e, + 0xcfa4bb90, 0x412bbc73, 0x8d81bced, 0x8744b5f4, 0x4beeb56a, + 0xc561b289, 0x09cbb217, 0xac509190, 0x60fa910e, 0xee7596ed, + 0x22df9673, 0x281a9f6a, 0xe4b09ff4, 0x6a3f9817, 0xa6959889, + 0x7fb58a25, 0xb31f8abb, 0x3d908d58, 0xf13a8dc6, 0xfbff84df, + 0x37558441, 0xb9da83a2, 0x7570833c, 0x533b85da, 0x9f918544, + 0x111e82a7, 0xddb48239, 0xd7718b20, 0x1bdb8bbe, 0x95548c5d, + 0x59fe8cc3, 0x80de9e6f, 0x4c749ef1, 0xc2fb9912, 0x0e51998c, + 0x04949095, 0xc83e900b, 0x46b197e8, 0x8a1b9776, 0x2f80b4f1, + 0xe32ab46f, 0x6da5b38c, 0xa10fb312, 0xabcaba0b, 0x6760ba95, + 0xe9efbd76, 0x2545bde8, 0xfc65af44, 0x30cfafda, 0xbe40a839, + 0x72eaa8a7, 0x782fa1be, 0xb485a120, 0x3a0aa6c3, 0xf6a0a65d, + 0xaa4de78c, 0x66e7e712, 0xe868e0f1, 0x24c2e06f, 0x2e07e976, + 0xe2ade9e8, 0x6c22ee0b, 0xa088ee95, 0x79a8fc39, 0xb502fca7, + 0x3b8dfb44, 0xf727fbda, 0xfde2f2c3, 0x3148f25d, 0xbfc7f5be, + 0x736df520, 0xd6f6d6a7, 0x1a5cd639, 0x94d3d1da, 0x5879d144, + 0x52bcd85d, 0x9e16d8c3, 0x1099df20, 0xdc33dfbe, 0x0513cd12, + 0xc9b9cd8c, 0x4736ca6f, 0x8b9ccaf1, 0x8159c3e8, 0x4df3c376, + 0xc37cc495, 0x0fd6c40b, 0x7aa64737, 0xb60c47a9, 0x3883404a, + 0xf42940d4, 0xfeec49cd, 0x32464953, 0xbcc94eb0, 0x70634e2e, + 0xa9435c82, 0x65e95c1c, 0xeb665bff, 0x27cc5b61, 0x2d095278, + 0xe1a352e6, 0x6f2c5505, 0xa386559b, 0x061d761c, 0xcab77682, + 0x44387161, 0x889271ff, 0x825778e6, 0x4efd7878, 0xc0727f9b, + 0x0cd87f05, 0xd5f86da9, 0x19526d37, 0x97dd6ad4, 0x5b776a4a, + 0x51b26353, 0x9d1863cd, 0x1397642e, 0xdf3d64b0, 0x83d02561, + 0x4f7a25ff, 0xc1f5221c, 0x0d5f2282, 0x079a2b9b, 0xcb302b05, + 0x45bf2ce6, 0x89152c78, 0x50353ed4, 0x9c9f3e4a, 0x121039a9, + 0xdeba3937, 0xd47f302e, 0x18d530b0, 0x965a3753, 0x5af037cd, + 0xff6b144a, 0x33c114d4, 0xbd4e1337, 0x71e413a9, 0x7b211ab0, + 0xb78b1a2e, 0x39041dcd, 0xf5ae1d53, 0x2c8e0fff, 0xe0240f61, + 0x6eab0882, 0xa201081c, 0xa8c40105, 0x646e019b, 0xeae10678, + 0x264b06e6}, + {0x00000000, 0xa6770bb4, 0x979f1129, 0x31e81a9d, 0xf44f2413, + 0x52382fa7, 0x63d0353a, 0xc5a73e8e, 0x33ef4e67, 0x959845d3, + 0xa4705f4e, 0x020754fa, 0xc7a06a74, 0x61d761c0, 0x503f7b5d, + 0xf64870e9, 0x67de9cce, 0xc1a9977a, 0xf0418de7, 0x56368653, + 0x9391b8dd, 0x35e6b369, 0x040ea9f4, 0xa279a240, 0x5431d2a9, + 0xf246d91d, 0xc3aec380, 0x65d9c834, 0xa07ef6ba, 0x0609fd0e, + 0x37e1e793, 0x9196ec27, 0xcfbd399c, 0x69ca3228, 0x582228b5, + 0xfe552301, 0x3bf21d8f, 0x9d85163b, 0xac6d0ca6, 0x0a1a0712, + 0xfc5277fb, 0x5a257c4f, 0x6bcd66d2, 0xcdba6d66, 0x081d53e8, + 0xae6a585c, 0x9f8242c1, 0x39f54975, 0xa863a552, 0x0e14aee6, + 0x3ffcb47b, 0x998bbfcf, 0x5c2c8141, 0xfa5b8af5, 0xcbb39068, + 0x6dc49bdc, 0x9b8ceb35, 0x3dfbe081, 0x0c13fa1c, 0xaa64f1a8, + 0x6fc3cf26, 0xc9b4c492, 0xf85cde0f, 0x5e2bd5bb, 0x440b7579, + 0xe27c7ecd, 0xd3946450, 0x75e36fe4, 0xb044516a, 0x16335ade, + 0x27db4043, 0x81ac4bf7, 0x77e43b1e, 0xd19330aa, 0xe07b2a37, + 0x460c2183, 0x83ab1f0d, 0x25dc14b9, 0x14340e24, 0xb2430590, + 0x23d5e9b7, 0x85a2e203, 0xb44af89e, 0x123df32a, 0xd79acda4, + 0x71edc610, 0x4005dc8d, 0xe672d739, 0x103aa7d0, 0xb64dac64, + 0x87a5b6f9, 0x21d2bd4d, 0xe47583c3, 0x42028877, 0x73ea92ea, + 0xd59d995e, 0x8bb64ce5, 0x2dc14751, 0x1c295dcc, 0xba5e5678, + 0x7ff968f6, 0xd98e6342, 0xe86679df, 0x4e11726b, 0xb8590282, + 0x1e2e0936, 0x2fc613ab, 0x89b1181f, 0x4c162691, 0xea612d25, + 0xdb8937b8, 0x7dfe3c0c, 0xec68d02b, 0x4a1fdb9f, 0x7bf7c102, + 0xdd80cab6, 0x1827f438, 0xbe50ff8c, 0x8fb8e511, 0x29cfeea5, + 0xdf879e4c, 0x79f095f8, 0x48188f65, 0xee6f84d1, 0x2bc8ba5f, + 0x8dbfb1eb, 0xbc57ab76, 0x1a20a0c2, 0x8816eaf2, 0x2e61e146, + 0x1f89fbdb, 0xb9fef06f, 0x7c59cee1, 0xda2ec555, 0xebc6dfc8, + 0x4db1d47c, 0xbbf9a495, 0x1d8eaf21, 0x2c66b5bc, 0x8a11be08, + 0x4fb68086, 0xe9c18b32, 0xd82991af, 0x7e5e9a1b, 0xefc8763c, + 0x49bf7d88, 0x78576715, 0xde206ca1, 0x1b87522f, 0xbdf0599b, + 0x8c184306, 0x2a6f48b2, 0xdc27385b, 0x7a5033ef, 0x4bb82972, + 0xedcf22c6, 0x28681c48, 0x8e1f17fc, 0xbff70d61, 0x198006d5, + 0x47abd36e, 0xe1dcd8da, 0xd034c247, 0x7643c9f3, 0xb3e4f77d, + 0x1593fcc9, 0x247be654, 0x820cede0, 0x74449d09, 0xd23396bd, + 0xe3db8c20, 0x45ac8794, 0x800bb91a, 0x267cb2ae, 0x1794a833, + 0xb1e3a387, 0x20754fa0, 0x86024414, 0xb7ea5e89, 0x119d553d, + 0xd43a6bb3, 0x724d6007, 0x43a57a9a, 0xe5d2712e, 0x139a01c7, + 0xb5ed0a73, 0x840510ee, 0x22721b5a, 0xe7d525d4, 0x41a22e60, + 0x704a34fd, 0xd63d3f49, 0xcc1d9f8b, 0x6a6a943f, 0x5b828ea2, + 0xfdf58516, 0x3852bb98, 0x9e25b02c, 0xafcdaab1, 0x09baa105, + 0xfff2d1ec, 0x5985da58, 0x686dc0c5, 0xce1acb71, 0x0bbdf5ff, + 0xadcafe4b, 0x9c22e4d6, 0x3a55ef62, 0xabc30345, 0x0db408f1, + 0x3c5c126c, 0x9a2b19d8, 0x5f8c2756, 0xf9fb2ce2, 0xc813367f, + 0x6e643dcb, 0x982c4d22, 0x3e5b4696, 0x0fb35c0b, 0xa9c457bf, + 0x6c636931, 0xca146285, 0xfbfc7818, 0x5d8b73ac, 0x03a0a617, + 0xa5d7ada3, 0x943fb73e, 0x3248bc8a, 0xf7ef8204, 0x519889b0, + 0x6070932d, 0xc6079899, 0x304fe870, 0x9638e3c4, 0xa7d0f959, + 0x01a7f2ed, 0xc400cc63, 0x6277c7d7, 0x539fdd4a, 0xf5e8d6fe, + 0x647e3ad9, 0xc209316d, 0xf3e12bf0, 0x55962044, 0x90311eca, + 0x3646157e, 0x07ae0fe3, 0xa1d90457, 0x579174be, 0xf1e67f0a, + 0xc00e6597, 0x66796e23, 0xa3de50ad, 0x05a95b19, 0x34414184, + 0x92364a30}, + {0x00000000, 0xcb5cd3a5, 0x4dc8a10b, 0x869472ae, 0x9b914216, + 0x50cd91b3, 0xd659e31d, 0x1d0530b8, 0xec53826d, 0x270f51c8, + 0xa19b2366, 0x6ac7f0c3, 0x77c2c07b, 0xbc9e13de, 0x3a0a6170, + 0xf156b2d5, 0x03d6029b, 0xc88ad13e, 0x4e1ea390, 0x85427035, + 0x9847408d, 0x531b9328, 0xd58fe186, 0x1ed33223, 0xef8580f6, + 0x24d95353, 0xa24d21fd, 0x6911f258, 0x7414c2e0, 0xbf481145, + 0x39dc63eb, 0xf280b04e, 0x07ac0536, 0xccf0d693, 0x4a64a43d, + 0x81387798, 0x9c3d4720, 0x57619485, 0xd1f5e62b, 0x1aa9358e, + 0xebff875b, 0x20a354fe, 0xa6372650, 0x6d6bf5f5, 0x706ec54d, + 0xbb3216e8, 0x3da66446, 0xf6fab7e3, 0x047a07ad, 0xcf26d408, + 0x49b2a6a6, 0x82ee7503, 0x9feb45bb, 0x54b7961e, 0xd223e4b0, + 0x197f3715, 0xe82985c0, 0x23755665, 0xa5e124cb, 0x6ebdf76e, + 0x73b8c7d6, 0xb8e41473, 0x3e7066dd, 0xf52cb578, 0x0f580a6c, + 0xc404d9c9, 0x4290ab67, 0x89cc78c2, 0x94c9487a, 0x5f959bdf, + 0xd901e971, 0x125d3ad4, 0xe30b8801, 0x28575ba4, 0xaec3290a, + 0x659ffaaf, 0x789aca17, 0xb3c619b2, 0x35526b1c, 0xfe0eb8b9, + 0x0c8e08f7, 0xc7d2db52, 0x4146a9fc, 0x8a1a7a59, 0x971f4ae1, + 0x5c439944, 0xdad7ebea, 0x118b384f, 0xe0dd8a9a, 0x2b81593f, + 0xad152b91, 0x6649f834, 0x7b4cc88c, 0xb0101b29, 0x36846987, + 0xfdd8ba22, 0x08f40f5a, 0xc3a8dcff, 0x453cae51, 0x8e607df4, + 0x93654d4c, 0x58399ee9, 0xdeadec47, 0x15f13fe2, 0xe4a78d37, + 0x2ffb5e92, 0xa96f2c3c, 0x6233ff99, 0x7f36cf21, 0xb46a1c84, + 0x32fe6e2a, 0xf9a2bd8f, 0x0b220dc1, 0xc07ede64, 0x46eaacca, + 0x8db67f6f, 0x90b34fd7, 0x5bef9c72, 0xdd7beedc, 0x16273d79, + 0xe7718fac, 0x2c2d5c09, 0xaab92ea7, 0x61e5fd02, 0x7ce0cdba, + 0xb7bc1e1f, 0x31286cb1, 0xfa74bf14, 0x1eb014d8, 0xd5ecc77d, + 0x5378b5d3, 0x98246676, 0x852156ce, 0x4e7d856b, 0xc8e9f7c5, + 0x03b52460, 0xf2e396b5, 0x39bf4510, 0xbf2b37be, 0x7477e41b, + 0x6972d4a3, 0xa22e0706, 0x24ba75a8, 0xefe6a60d, 0x1d661643, + 0xd63ac5e6, 0x50aeb748, 0x9bf264ed, 0x86f75455, 0x4dab87f0, + 0xcb3ff55e, 0x006326fb, 0xf135942e, 0x3a69478b, 0xbcfd3525, + 0x77a1e680, 0x6aa4d638, 0xa1f8059d, 0x276c7733, 0xec30a496, + 0x191c11ee, 0xd240c24b, 0x54d4b0e5, 0x9f886340, 0x828d53f8, + 0x49d1805d, 0xcf45f2f3, 0x04192156, 0xf54f9383, 0x3e134026, + 0xb8873288, 0x73dbe12d, 0x6eded195, 0xa5820230, 0x2316709e, + 0xe84aa33b, 0x1aca1375, 0xd196c0d0, 0x5702b27e, 0x9c5e61db, + 0x815b5163, 0x4a0782c6, 0xcc93f068, 0x07cf23cd, 0xf6999118, + 0x3dc542bd, 0xbb513013, 0x700de3b6, 0x6d08d30e, 0xa65400ab, + 0x20c07205, 0xeb9ca1a0, 0x11e81eb4, 0xdab4cd11, 0x5c20bfbf, + 0x977c6c1a, 0x8a795ca2, 0x41258f07, 0xc7b1fda9, 0x0ced2e0c, + 0xfdbb9cd9, 0x36e74f7c, 0xb0733dd2, 0x7b2fee77, 0x662adecf, + 0xad760d6a, 0x2be27fc4, 0xe0beac61, 0x123e1c2f, 0xd962cf8a, + 0x5ff6bd24, 0x94aa6e81, 0x89af5e39, 0x42f38d9c, 0xc467ff32, + 0x0f3b2c97, 0xfe6d9e42, 0x35314de7, 0xb3a53f49, 0x78f9ecec, + 0x65fcdc54, 0xaea00ff1, 0x28347d5f, 0xe368aefa, 0x16441b82, + 0xdd18c827, 0x5b8cba89, 0x90d0692c, 0x8dd55994, 0x46898a31, + 0xc01df89f, 0x0b412b3a, 0xfa1799ef, 0x314b4a4a, 0xb7df38e4, + 0x7c83eb41, 0x6186dbf9, 0xaada085c, 0x2c4e7af2, 0xe712a957, + 0x15921919, 0xdececabc, 0x585ab812, 0x93066bb7, 0x8e035b0f, + 0x455f88aa, 0xc3cbfa04, 0x089729a1, 0xf9c19b74, 0x329d48d1, + 0xb4093a7f, 0x7f55e9da, 0x6250d962, 0xa90c0ac7, 0x2f987869, + 0xe4c4abcc}, + {0x00000000, 0x3d6029b0, 0x7ac05360, 0x47a07ad0, 0xf580a6c0, + 0xc8e08f70, 0x8f40f5a0, 0xb220dc10, 0x30704bc1, 0x0d106271, + 0x4ab018a1, 0x77d03111, 0xc5f0ed01, 0xf890c4b1, 0xbf30be61, + 0x825097d1, 0x60e09782, 0x5d80be32, 0x1a20c4e2, 0x2740ed52, + 0x95603142, 0xa80018f2, 0xefa06222, 0xd2c04b92, 0x5090dc43, + 0x6df0f5f3, 0x2a508f23, 0x1730a693, 0xa5107a83, 0x98705333, + 0xdfd029e3, 0xe2b00053, 0xc1c12f04, 0xfca106b4, 0xbb017c64, + 0x866155d4, 0x344189c4, 0x0921a074, 0x4e81daa4, 0x73e1f314, + 0xf1b164c5, 0xccd14d75, 0x8b7137a5, 0xb6111e15, 0x0431c205, + 0x3951ebb5, 0x7ef19165, 0x4391b8d5, 0xa121b886, 0x9c419136, + 0xdbe1ebe6, 0xe681c256, 0x54a11e46, 0x69c137f6, 0x2e614d26, + 0x13016496, 0x9151f347, 0xac31daf7, 0xeb91a027, 0xd6f18997, + 0x64d15587, 0x59b17c37, 0x1e1106e7, 0x23712f57, 0x58f35849, + 0x659371f9, 0x22330b29, 0x1f532299, 0xad73fe89, 0x9013d739, + 0xd7b3ade9, 0xead38459, 0x68831388, 0x55e33a38, 0x124340e8, + 0x2f236958, 0x9d03b548, 0xa0639cf8, 0xe7c3e628, 0xdaa3cf98, + 0x3813cfcb, 0x0573e67b, 0x42d39cab, 0x7fb3b51b, 0xcd93690b, + 0xf0f340bb, 0xb7533a6b, 0x8a3313db, 0x0863840a, 0x3503adba, + 0x72a3d76a, 0x4fc3feda, 0xfde322ca, 0xc0830b7a, 0x872371aa, + 0xba43581a, 0x9932774d, 0xa4525efd, 0xe3f2242d, 0xde920d9d, + 0x6cb2d18d, 0x51d2f83d, 0x167282ed, 0x2b12ab5d, 0xa9423c8c, + 0x9422153c, 0xd3826fec, 0xeee2465c, 0x5cc29a4c, 0x61a2b3fc, + 0x2602c92c, 0x1b62e09c, 0xf9d2e0cf, 0xc4b2c97f, 0x8312b3af, + 0xbe729a1f, 0x0c52460f, 0x31326fbf, 0x7692156f, 0x4bf23cdf, + 0xc9a2ab0e, 0xf4c282be, 0xb362f86e, 0x8e02d1de, 0x3c220dce, + 0x0142247e, 0x46e25eae, 0x7b82771e, 0xb1e6b092, 0x8c869922, + 0xcb26e3f2, 0xf646ca42, 0x44661652, 0x79063fe2, 0x3ea64532, + 0x03c66c82, 0x8196fb53, 0xbcf6d2e3, 0xfb56a833, 0xc6368183, + 0x74165d93, 0x49767423, 0x0ed60ef3, 0x33b62743, 0xd1062710, + 0xec660ea0, 0xabc67470, 0x96a65dc0, 0x248681d0, 0x19e6a860, + 0x5e46d2b0, 0x6326fb00, 0xe1766cd1, 0xdc164561, 0x9bb63fb1, + 0xa6d61601, 0x14f6ca11, 0x2996e3a1, 0x6e369971, 0x5356b0c1, + 0x70279f96, 0x4d47b626, 0x0ae7ccf6, 0x3787e546, 0x85a73956, + 0xb8c710e6, 0xff676a36, 0xc2074386, 0x4057d457, 0x7d37fde7, + 0x3a978737, 0x07f7ae87, 0xb5d77297, 0x88b75b27, 0xcf1721f7, + 0xf2770847, 0x10c70814, 0x2da721a4, 0x6a075b74, 0x576772c4, + 0xe547aed4, 0xd8278764, 0x9f87fdb4, 0xa2e7d404, 0x20b743d5, + 0x1dd76a65, 0x5a7710b5, 0x67173905, 0xd537e515, 0xe857cca5, + 0xaff7b675, 0x92979fc5, 0xe915e8db, 0xd475c16b, 0x93d5bbbb, + 0xaeb5920b, 0x1c954e1b, 0x21f567ab, 0x66551d7b, 0x5b3534cb, + 0xd965a31a, 0xe4058aaa, 0xa3a5f07a, 0x9ec5d9ca, 0x2ce505da, + 0x11852c6a, 0x562556ba, 0x6b457f0a, 0x89f57f59, 0xb49556e9, + 0xf3352c39, 0xce550589, 0x7c75d999, 0x4115f029, 0x06b58af9, + 0x3bd5a349, 0xb9853498, 0x84e51d28, 0xc34567f8, 0xfe254e48, + 0x4c059258, 0x7165bbe8, 0x36c5c138, 0x0ba5e888, 0x28d4c7df, + 0x15b4ee6f, 0x521494bf, 0x6f74bd0f, 0xdd54611f, 0xe03448af, + 0xa794327f, 0x9af41bcf, 0x18a48c1e, 0x25c4a5ae, 0x6264df7e, + 0x5f04f6ce, 0xed242ade, 0xd044036e, 0x97e479be, 0xaa84500e, + 0x4834505d, 0x755479ed, 0x32f4033d, 0x0f942a8d, 0xbdb4f69d, + 0x80d4df2d, 0xc774a5fd, 0xfa148c4d, 0x78441b9c, 0x4524322c, + 0x028448fc, 0x3fe4614c, 0x8dc4bd5c, 0xb0a494ec, 0xf704ee3c, + 0xca64c78c}}; + +local const z_word_t FAR crc_braid_big_table[][256] = { + {0x00000000, 0xb029603d, 0x6053c07a, 0xd07aa047, 0xc0a680f5, + 0x708fe0c8, 0xa0f5408f, 0x10dc20b2, 0xc14b7030, 0x7162100d, + 0xa118b04a, 0x1131d077, 0x01edf0c5, 0xb1c490f8, 0x61be30bf, + 0xd1975082, 0x8297e060, 0x32be805d, 0xe2c4201a, 0x52ed4027, + 0x42316095, 0xf21800a8, 0x2262a0ef, 0x924bc0d2, 0x43dc9050, + 0xf3f5f06d, 0x238f502a, 0x93a63017, 0x837a10a5, 0x33537098, + 0xe329d0df, 0x5300b0e2, 0x042fc1c1, 0xb406a1fc, 0x647c01bb, + 0xd4556186, 0xc4894134, 0x74a02109, 0xa4da814e, 0x14f3e173, + 0xc564b1f1, 0x754dd1cc, 0xa537718b, 0x151e11b6, 0x05c23104, + 0xb5eb5139, 0x6591f17e, 0xd5b89143, 0x86b821a1, 0x3691419c, + 0xe6ebe1db, 0x56c281e6, 0x461ea154, 0xf637c169, 0x264d612e, + 0x96640113, 0x47f35191, 0xf7da31ac, 0x27a091eb, 0x9789f1d6, + 0x8755d164, 0x377cb159, 0xe706111e, 0x572f7123, 0x4958f358, + 0xf9719365, 0x290b3322, 0x9922531f, 0x89fe73ad, 0x39d71390, + 0xe9adb3d7, 0x5984d3ea, 0x88138368, 0x383ae355, 0xe8404312, + 0x5869232f, 0x48b5039d, 0xf89c63a0, 0x28e6c3e7, 0x98cfa3da, + 0xcbcf1338, 0x7be67305, 0xab9cd342, 0x1bb5b37f, 0x0b6993cd, + 0xbb40f3f0, 0x6b3a53b7, 0xdb13338a, 0x0a846308, 0xbaad0335, + 0x6ad7a372, 0xdafec34f, 0xca22e3fd, 0x7a0b83c0, 0xaa712387, + 0x1a5843ba, 0x4d773299, 0xfd5e52a4, 0x2d24f2e3, 0x9d0d92de, + 0x8dd1b26c, 0x3df8d251, 0xed827216, 0x5dab122b, 0x8c3c42a9, + 0x3c152294, 0xec6f82d3, 0x5c46e2ee, 0x4c9ac25c, 0xfcb3a261, + 0x2cc90226, 0x9ce0621b, 0xcfe0d2f9, 0x7fc9b2c4, 0xafb31283, + 0x1f9a72be, 0x0f46520c, 0xbf6f3231, 0x6f159276, 0xdf3cf24b, + 0x0eaba2c9, 0xbe82c2f4, 0x6ef862b3, 0xded1028e, 0xce0d223c, + 0x7e244201, 0xae5ee246, 0x1e77827b, 0x92b0e6b1, 0x2299868c, + 0xf2e326cb, 0x42ca46f6, 0x52166644, 0xe23f0679, 0x3245a63e, + 0x826cc603, 0x53fb9681, 0xe3d2f6bc, 0x33a856fb, 0x838136c6, + 0x935d1674, 0x23747649, 0xf30ed60e, 0x4327b633, 0x102706d1, + 0xa00e66ec, 0x7074c6ab, 0xc05da696, 0xd0818624, 0x60a8e619, + 0xb0d2465e, 0x00fb2663, 0xd16c76e1, 0x614516dc, 0xb13fb69b, + 0x0116d6a6, 0x11caf614, 0xa1e39629, 0x7199366e, 0xc1b05653, + 0x969f2770, 0x26b6474d, 0xf6cce70a, 0x46e58737, 0x5639a785, + 0xe610c7b8, 0x366a67ff, 0x864307c2, 0x57d45740, 0xe7fd377d, + 0x3787973a, 0x87aef707, 0x9772d7b5, 0x275bb788, 0xf72117cf, + 0x470877f2, 0x1408c710, 0xa421a72d, 0x745b076a, 0xc4726757, + 0xd4ae47e5, 0x648727d8, 0xb4fd879f, 0x04d4e7a2, 0xd543b720, + 0x656ad71d, 0xb510775a, 0x05391767, 0x15e537d5, 0xa5cc57e8, + 0x75b6f7af, 0xc59f9792, 0xdbe815e9, 0x6bc175d4, 0xbbbbd593, + 0x0b92b5ae, 0x1b4e951c, 0xab67f521, 0x7b1d5566, 0xcb34355b, + 0x1aa365d9, 0xaa8a05e4, 0x7af0a5a3, 0xcad9c59e, 0xda05e52c, + 0x6a2c8511, 0xba562556, 0x0a7f456b, 0x597ff589, 0xe95695b4, + 0x392c35f3, 0x890555ce, 0x99d9757c, 0x29f01541, 0xf98ab506, + 0x49a3d53b, 0x983485b9, 0x281de584, 0xf86745c3, 0x484e25fe, + 0x5892054c, 0xe8bb6571, 0x38c1c536, 0x88e8a50b, 0xdfc7d428, + 0x6feeb415, 0xbf941452, 0x0fbd746f, 0x1f6154dd, 0xaf4834e0, + 0x7f3294a7, 0xcf1bf49a, 0x1e8ca418, 0xaea5c425, 0x7edf6462, + 0xcef6045f, 0xde2a24ed, 0x6e0344d0, 0xbe79e497, 0x0e5084aa, + 0x5d503448, 0xed795475, 0x3d03f432, 0x8d2a940f, 0x9df6b4bd, + 0x2ddfd480, 0xfda574c7, 0x4d8c14fa, 0x9c1b4478, 0x2c322445, + 0xfc488402, 0x4c61e43f, 0x5cbdc48d, 0xec94a4b0, 0x3cee04f7, + 0x8cc764ca}, + {0x00000000, 0xa5d35ccb, 0x0ba1c84d, 0xae729486, 0x1642919b, + 0xb391cd50, 0x1de359d6, 0xb830051d, 0x6d8253ec, 0xc8510f27, + 0x66239ba1, 0xc3f0c76a, 0x7bc0c277, 0xde139ebc, 0x70610a3a, + 0xd5b256f1, 0x9b02d603, 0x3ed18ac8, 0x90a31e4e, 0x35704285, + 0x8d404798, 0x28931b53, 0x86e18fd5, 0x2332d31e, 0xf68085ef, + 0x5353d924, 0xfd214da2, 0x58f21169, 0xe0c21474, 0x451148bf, + 0xeb63dc39, 0x4eb080f2, 0x3605ac07, 0x93d6f0cc, 0x3da4644a, + 0x98773881, 0x20473d9c, 0x85946157, 0x2be6f5d1, 0x8e35a91a, + 0x5b87ffeb, 0xfe54a320, 0x502637a6, 0xf5f56b6d, 0x4dc56e70, + 0xe81632bb, 0x4664a63d, 0xe3b7faf6, 0xad077a04, 0x08d426cf, + 0xa6a6b249, 0x0375ee82, 0xbb45eb9f, 0x1e96b754, 0xb0e423d2, + 0x15377f19, 0xc08529e8, 0x65567523, 0xcb24e1a5, 0x6ef7bd6e, + 0xd6c7b873, 0x7314e4b8, 0xdd66703e, 0x78b52cf5, 0x6c0a580f, + 0xc9d904c4, 0x67ab9042, 0xc278cc89, 0x7a48c994, 0xdf9b955f, + 0x71e901d9, 0xd43a5d12, 0x01880be3, 0xa45b5728, 0x0a29c3ae, + 0xaffa9f65, 0x17ca9a78, 0xb219c6b3, 0x1c6b5235, 0xb9b80efe, + 0xf7088e0c, 0x52dbd2c7, 0xfca94641, 0x597a1a8a, 0xe14a1f97, + 0x4499435c, 0xeaebd7da, 0x4f388b11, 0x9a8adde0, 0x3f59812b, + 0x912b15ad, 0x34f84966, 0x8cc84c7b, 0x291b10b0, 0x87698436, + 0x22bad8fd, 0x5a0ff408, 0xffdca8c3, 0x51ae3c45, 0xf47d608e, + 0x4c4d6593, 0xe99e3958, 0x47ecadde, 0xe23ff115, 0x378da7e4, + 0x925efb2f, 0x3c2c6fa9, 0x99ff3362, 0x21cf367f, 0x841c6ab4, + 0x2a6efe32, 0x8fbda2f9, 0xc10d220b, 0x64de7ec0, 0xcaacea46, + 0x6f7fb68d, 0xd74fb390, 0x729cef5b, 0xdcee7bdd, 0x793d2716, + 0xac8f71e7, 0x095c2d2c, 0xa72eb9aa, 0x02fde561, 0xbacde07c, + 0x1f1ebcb7, 0xb16c2831, 0x14bf74fa, 0xd814b01e, 0x7dc7ecd5, + 0xd3b57853, 0x76662498, 0xce562185, 0x6b857d4e, 0xc5f7e9c8, + 0x6024b503, 0xb596e3f2, 0x1045bf39, 0xbe372bbf, 0x1be47774, + 0xa3d47269, 0x06072ea2, 0xa875ba24, 0x0da6e6ef, 0x4316661d, + 0xe6c53ad6, 0x48b7ae50, 0xed64f29b, 0x5554f786, 0xf087ab4d, + 0x5ef53fcb, 0xfb266300, 0x2e9435f1, 0x8b47693a, 0x2535fdbc, + 0x80e6a177, 0x38d6a46a, 0x9d05f8a1, 0x33776c27, 0x96a430ec, + 0xee111c19, 0x4bc240d2, 0xe5b0d454, 0x4063889f, 0xf8538d82, + 0x5d80d149, 0xf3f245cf, 0x56211904, 0x83934ff5, 0x2640133e, + 0x883287b8, 0x2de1db73, 0x95d1de6e, 0x300282a5, 0x9e701623, + 0x3ba34ae8, 0x7513ca1a, 0xd0c096d1, 0x7eb20257, 0xdb615e9c, + 0x63515b81, 0xc682074a, 0x68f093cc, 0xcd23cf07, 0x189199f6, + 0xbd42c53d, 0x133051bb, 0xb6e30d70, 0x0ed3086d, 0xab0054a6, + 0x0572c020, 0xa0a19ceb, 0xb41ee811, 0x11cdb4da, 0xbfbf205c, + 0x1a6c7c97, 0xa25c798a, 0x078f2541, 0xa9fdb1c7, 0x0c2eed0c, + 0xd99cbbfd, 0x7c4fe736, 0xd23d73b0, 0x77ee2f7b, 0xcfde2a66, + 0x6a0d76ad, 0xc47fe22b, 0x61acbee0, 0x2f1c3e12, 0x8acf62d9, + 0x24bdf65f, 0x816eaa94, 0x395eaf89, 0x9c8df342, 0x32ff67c4, + 0x972c3b0f, 0x429e6dfe, 0xe74d3135, 0x493fa5b3, 0xececf978, + 0x54dcfc65, 0xf10fa0ae, 0x5f7d3428, 0xfaae68e3, 0x821b4416, + 0x27c818dd, 0x89ba8c5b, 0x2c69d090, 0x9459d58d, 0x318a8946, + 0x9ff81dc0, 0x3a2b410b, 0xef9917fa, 0x4a4a4b31, 0xe438dfb7, + 0x41eb837c, 0xf9db8661, 0x5c08daaa, 0xf27a4e2c, 0x57a912e7, + 0x19199215, 0xbccacede, 0x12b85a58, 0xb76b0693, 0x0f5b038e, + 0xaa885f45, 0x04facbc3, 0xa1299708, 0x749bc1f9, 0xd1489d32, + 0x7f3a09b4, 0xdae9557f, 0x62d95062, 0xc70a0ca9, 0x6978982f, + 0xccabc4e4}, + {0x00000000, 0xb40b77a6, 0x29119f97, 0x9d1ae831, 0x13244ff4, + 0xa72f3852, 0x3a35d063, 0x8e3ea7c5, 0x674eef33, 0xd3459895, + 0x4e5f70a4, 0xfa540702, 0x746aa0c7, 0xc061d761, 0x5d7b3f50, + 0xe97048f6, 0xce9cde67, 0x7a97a9c1, 0xe78d41f0, 0x53863656, + 0xddb89193, 0x69b3e635, 0xf4a90e04, 0x40a279a2, 0xa9d23154, + 0x1dd946f2, 0x80c3aec3, 0x34c8d965, 0xbaf67ea0, 0x0efd0906, + 0x93e7e137, 0x27ec9691, 0x9c39bdcf, 0x2832ca69, 0xb5282258, + 0x012355fe, 0x8f1df23b, 0x3b16859d, 0xa60c6dac, 0x12071a0a, + 0xfb7752fc, 0x4f7c255a, 0xd266cd6b, 0x666dbacd, 0xe8531d08, + 0x5c586aae, 0xc142829f, 0x7549f539, 0x52a563a8, 0xe6ae140e, + 0x7bb4fc3f, 0xcfbf8b99, 0x41812c5c, 0xf58a5bfa, 0x6890b3cb, + 0xdc9bc46d, 0x35eb8c9b, 0x81e0fb3d, 0x1cfa130c, 0xa8f164aa, + 0x26cfc36f, 0x92c4b4c9, 0x0fde5cf8, 0xbbd52b5e, 0x79750b44, + 0xcd7e7ce2, 0x506494d3, 0xe46fe375, 0x6a5144b0, 0xde5a3316, + 0x4340db27, 0xf74bac81, 0x1e3be477, 0xaa3093d1, 0x372a7be0, + 0x83210c46, 0x0d1fab83, 0xb914dc25, 0x240e3414, 0x900543b2, + 0xb7e9d523, 0x03e2a285, 0x9ef84ab4, 0x2af33d12, 0xa4cd9ad7, + 0x10c6ed71, 0x8ddc0540, 0x39d772e6, 0xd0a73a10, 0x64ac4db6, + 0xf9b6a587, 0x4dbdd221, 0xc38375e4, 0x77880242, 0xea92ea73, + 0x5e999dd5, 0xe54cb68b, 0x5147c12d, 0xcc5d291c, 0x78565eba, + 0xf668f97f, 0x42638ed9, 0xdf7966e8, 0x6b72114e, 0x820259b8, + 0x36092e1e, 0xab13c62f, 0x1f18b189, 0x9126164c, 0x252d61ea, + 0xb83789db, 0x0c3cfe7d, 0x2bd068ec, 0x9fdb1f4a, 0x02c1f77b, + 0xb6ca80dd, 0x38f42718, 0x8cff50be, 0x11e5b88f, 0xa5eecf29, + 0x4c9e87df, 0xf895f079, 0x658f1848, 0xd1846fee, 0x5fbac82b, + 0xebb1bf8d, 0x76ab57bc, 0xc2a0201a, 0xf2ea1688, 0x46e1612e, + 0xdbfb891f, 0x6ff0feb9, 0xe1ce597c, 0x55c52eda, 0xc8dfc6eb, + 0x7cd4b14d, 0x95a4f9bb, 0x21af8e1d, 0xbcb5662c, 0x08be118a, + 0x8680b64f, 0x328bc1e9, 0xaf9129d8, 0x1b9a5e7e, 0x3c76c8ef, + 0x887dbf49, 0x15675778, 0xa16c20de, 0x2f52871b, 0x9b59f0bd, + 0x0643188c, 0xb2486f2a, 0x5b3827dc, 0xef33507a, 0x7229b84b, + 0xc622cfed, 0x481c6828, 0xfc171f8e, 0x610df7bf, 0xd5068019, + 0x6ed3ab47, 0xdad8dce1, 0x47c234d0, 0xf3c94376, 0x7df7e4b3, + 0xc9fc9315, 0x54e67b24, 0xe0ed0c82, 0x099d4474, 0xbd9633d2, + 0x208cdbe3, 0x9487ac45, 0x1ab90b80, 0xaeb27c26, 0x33a89417, + 0x87a3e3b1, 0xa04f7520, 0x14440286, 0x895eeab7, 0x3d559d11, + 0xb36b3ad4, 0x07604d72, 0x9a7aa543, 0x2e71d2e5, 0xc7019a13, + 0x730aedb5, 0xee100584, 0x5a1b7222, 0xd425d5e7, 0x602ea241, + 0xfd344a70, 0x493f3dd6, 0x8b9f1dcc, 0x3f946a6a, 0xa28e825b, + 0x1685f5fd, 0x98bb5238, 0x2cb0259e, 0xb1aacdaf, 0x05a1ba09, + 0xecd1f2ff, 0x58da8559, 0xc5c06d68, 0x71cb1ace, 0xfff5bd0b, + 0x4bfecaad, 0xd6e4229c, 0x62ef553a, 0x4503c3ab, 0xf108b40d, + 0x6c125c3c, 0xd8192b9a, 0x56278c5f, 0xe22cfbf9, 0x7f3613c8, + 0xcb3d646e, 0x224d2c98, 0x96465b3e, 0x0b5cb30f, 0xbf57c4a9, + 0x3169636c, 0x856214ca, 0x1878fcfb, 0xac738b5d, 0x17a6a003, + 0xa3add7a5, 0x3eb73f94, 0x8abc4832, 0x0482eff7, 0xb0899851, + 0x2d937060, 0x999807c6, 0x70e84f30, 0xc4e33896, 0x59f9d0a7, + 0xedf2a701, 0x63cc00c4, 0xd7c77762, 0x4add9f53, 0xfed6e8f5, + 0xd93a7e64, 0x6d3109c2, 0xf02be1f3, 0x44209655, 0xca1e3190, + 0x7e154636, 0xe30fae07, 0x5704d9a1, 0xbe749157, 0x0a7fe6f1, + 0x97650ec0, 0x236e7966, 0xad50dea3, 0x195ba905, 0x84414134, + 0x304a3692}, + {0x00000000, 0x9e00aacc, 0x7d072542, 0xe3078f8e, 0xfa0e4a84, + 0x640ee048, 0x87096fc6, 0x1909c50a, 0xb51be5d3, 0x2b1b4f1f, + 0xc81cc091, 0x561c6a5d, 0x4f15af57, 0xd115059b, 0x32128a15, + 0xac1220d9, 0x2b31bb7c, 0xb53111b0, 0x56369e3e, 0xc83634f2, + 0xd13ff1f8, 0x4f3f5b34, 0xac38d4ba, 0x32387e76, 0x9e2a5eaf, + 0x002af463, 0xe32d7bed, 0x7d2dd121, 0x6424142b, 0xfa24bee7, + 0x19233169, 0x87239ba5, 0x566276f9, 0xc862dc35, 0x2b6553bb, + 0xb565f977, 0xac6c3c7d, 0x326c96b1, 0xd16b193f, 0x4f6bb3f3, + 0xe379932a, 0x7d7939e6, 0x9e7eb668, 0x007e1ca4, 0x1977d9ae, + 0x87777362, 0x6470fcec, 0xfa705620, 0x7d53cd85, 0xe3536749, + 0x0054e8c7, 0x9e54420b, 0x875d8701, 0x195d2dcd, 0xfa5aa243, + 0x645a088f, 0xc8482856, 0x5648829a, 0xb54f0d14, 0x2b4fa7d8, + 0x324662d2, 0xac46c81e, 0x4f414790, 0xd141ed5c, 0xedc29d29, + 0x73c237e5, 0x90c5b86b, 0x0ec512a7, 0x17ccd7ad, 0x89cc7d61, + 0x6acbf2ef, 0xf4cb5823, 0x58d978fa, 0xc6d9d236, 0x25de5db8, + 0xbbdef774, 0xa2d7327e, 0x3cd798b2, 0xdfd0173c, 0x41d0bdf0, + 0xc6f32655, 0x58f38c99, 0xbbf40317, 0x25f4a9db, 0x3cfd6cd1, + 0xa2fdc61d, 0x41fa4993, 0xdffae35f, 0x73e8c386, 0xede8694a, + 0x0eefe6c4, 0x90ef4c08, 0x89e68902, 0x17e623ce, 0xf4e1ac40, + 0x6ae1068c, 0xbba0ebd0, 0x25a0411c, 0xc6a7ce92, 0x58a7645e, + 0x41aea154, 0xdfae0b98, 0x3ca98416, 0xa2a92eda, 0x0ebb0e03, + 0x90bba4cf, 0x73bc2b41, 0xedbc818d, 0xf4b54487, 0x6ab5ee4b, + 0x89b261c5, 0x17b2cb09, 0x909150ac, 0x0e91fa60, 0xed9675ee, + 0x7396df22, 0x6a9f1a28, 0xf49fb0e4, 0x17983f6a, 0x899895a6, + 0x258ab57f, 0xbb8a1fb3, 0x588d903d, 0xc68d3af1, 0xdf84fffb, + 0x41845537, 0xa283dab9, 0x3c837075, 0xda853b53, 0x4485919f, + 0xa7821e11, 0x3982b4dd, 0x208b71d7, 0xbe8bdb1b, 0x5d8c5495, + 0xc38cfe59, 0x6f9ede80, 0xf19e744c, 0x1299fbc2, 0x8c99510e, + 0x95909404, 0x0b903ec8, 0xe897b146, 0x76971b8a, 0xf1b4802f, + 0x6fb42ae3, 0x8cb3a56d, 0x12b30fa1, 0x0bbacaab, 0x95ba6067, + 0x76bdefe9, 0xe8bd4525, 0x44af65fc, 0xdaafcf30, 0x39a840be, + 0xa7a8ea72, 0xbea12f78, 0x20a185b4, 0xc3a60a3a, 0x5da6a0f6, + 0x8ce74daa, 0x12e7e766, 0xf1e068e8, 0x6fe0c224, 0x76e9072e, + 0xe8e9ade2, 0x0bee226c, 0x95ee88a0, 0x39fca879, 0xa7fc02b5, + 0x44fb8d3b, 0xdafb27f7, 0xc3f2e2fd, 0x5df24831, 0xbef5c7bf, + 0x20f56d73, 0xa7d6f6d6, 0x39d65c1a, 0xdad1d394, 0x44d17958, + 0x5dd8bc52, 0xc3d8169e, 0x20df9910, 0xbedf33dc, 0x12cd1305, + 0x8ccdb9c9, 0x6fca3647, 0xf1ca9c8b, 0xe8c35981, 0x76c3f34d, + 0x95c47cc3, 0x0bc4d60f, 0x3747a67a, 0xa9470cb6, 0x4a408338, + 0xd44029f4, 0xcd49ecfe, 0x53494632, 0xb04ec9bc, 0x2e4e6370, + 0x825c43a9, 0x1c5ce965, 0xff5b66eb, 0x615bcc27, 0x7852092d, + 0xe652a3e1, 0x05552c6f, 0x9b5586a3, 0x1c761d06, 0x8276b7ca, + 0x61713844, 0xff719288, 0xe6785782, 0x7878fd4e, 0x9b7f72c0, + 0x057fd80c, 0xa96df8d5, 0x376d5219, 0xd46add97, 0x4a6a775b, + 0x5363b251, 0xcd63189d, 0x2e649713, 0xb0643ddf, 0x6125d083, + 0xff257a4f, 0x1c22f5c1, 0x82225f0d, 0x9b2b9a07, 0x052b30cb, + 0xe62cbf45, 0x782c1589, 0xd43e3550, 0x4a3e9f9c, 0xa9391012, + 0x3739bade, 0x2e307fd4, 0xb030d518, 0x53375a96, 0xcd37f05a, + 0x4a146bff, 0xd414c133, 0x37134ebd, 0xa913e471, 0xb01a217b, + 0x2e1a8bb7, 0xcd1d0439, 0x531daef5, 0xff0f8e2c, 0x610f24e0, + 0x8208ab6e, 0x1c0801a2, 0x0501c4a8, 0x9b016e64, 0x7806e1ea, + 0xe6064b26}}; + #endif - } -}; + +#endif + +#if N == 3 + +#if W == 8 + +local const z_crc_t FAR crc_braid_table[][256] = { + {0x00000000, 0x81256527, 0xd93bcc0f, 0x581ea928, 0x69069e5f, + 0xe823fb78, 0xb03d5250, 0x31183777, 0xd20d3cbe, 0x53285999, + 0x0b36f0b1, 0x8a139596, 0xbb0ba2e1, 0x3a2ec7c6, 0x62306eee, + 0xe3150bc9, 0x7f6b7f3d, 0xfe4e1a1a, 0xa650b332, 0x2775d615, + 0x166de162, 0x97488445, 0xcf562d6d, 0x4e73484a, 0xad664383, + 0x2c4326a4, 0x745d8f8c, 0xf578eaab, 0xc460dddc, 0x4545b8fb, + 0x1d5b11d3, 0x9c7e74f4, 0xfed6fe7a, 0x7ff39b5d, 0x27ed3275, + 0xa6c85752, 0x97d06025, 0x16f50502, 0x4eebac2a, 0xcfcec90d, + 0x2cdbc2c4, 0xadfea7e3, 0xf5e00ecb, 0x74c56bec, 0x45dd5c9b, + 0xc4f839bc, 0x9ce69094, 0x1dc3f5b3, 0x81bd8147, 0x0098e460, + 0x58864d48, 0xd9a3286f, 0xe8bb1f18, 0x699e7a3f, 0x3180d317, + 0xb0a5b630, 0x53b0bdf9, 0xd295d8de, 0x8a8b71f6, 0x0bae14d1, + 0x3ab623a6, 0xbb934681, 0xe38defa9, 0x62a88a8e, 0x26dcfab5, + 0xa7f99f92, 0xffe736ba, 0x7ec2539d, 0x4fda64ea, 0xceff01cd, + 0x96e1a8e5, 0x17c4cdc2, 0xf4d1c60b, 0x75f4a32c, 0x2dea0a04, + 0xaccf6f23, 0x9dd75854, 0x1cf23d73, 0x44ec945b, 0xc5c9f17c, + 0x59b78588, 0xd892e0af, 0x808c4987, 0x01a92ca0, 0x30b11bd7, + 0xb1947ef0, 0xe98ad7d8, 0x68afb2ff, 0x8bbab936, 0x0a9fdc11, + 0x52817539, 0xd3a4101e, 0xe2bc2769, 0x6399424e, 0x3b87eb66, + 0xbaa28e41, 0xd80a04cf, 0x592f61e8, 0x0131c8c0, 0x8014ade7, + 0xb10c9a90, 0x3029ffb7, 0x6837569f, 0xe91233b8, 0x0a073871, + 0x8b225d56, 0xd33cf47e, 0x52199159, 0x6301a62e, 0xe224c309, + 0xba3a6a21, 0x3b1f0f06, 0xa7617bf2, 0x26441ed5, 0x7e5ab7fd, + 0xff7fd2da, 0xce67e5ad, 0x4f42808a, 0x175c29a2, 0x96794c85, + 0x756c474c, 0xf449226b, 0xac578b43, 0x2d72ee64, 0x1c6ad913, + 0x9d4fbc34, 0xc551151c, 0x4474703b, 0x4db9f56a, 0xcc9c904d, + 0x94823965, 0x15a75c42, 0x24bf6b35, 0xa59a0e12, 0xfd84a73a, + 0x7ca1c21d, 0x9fb4c9d4, 0x1e91acf3, 0x468f05db, 0xc7aa60fc, + 0xf6b2578b, 0x779732ac, 0x2f899b84, 0xaeacfea3, 0x32d28a57, + 0xb3f7ef70, 0xebe94658, 0x6acc237f, 0x5bd41408, 0xdaf1712f, + 0x82efd807, 0x03cabd20, 0xe0dfb6e9, 0x61fad3ce, 0x39e47ae6, + 0xb8c11fc1, 0x89d928b6, 0x08fc4d91, 0x50e2e4b9, 0xd1c7819e, + 0xb36f0b10, 0x324a6e37, 0x6a54c71f, 0xeb71a238, 0xda69954f, + 0x5b4cf068, 0x03525940, 0x82773c67, 0x616237ae, 0xe0475289, + 0xb859fba1, 0x397c9e86, 0x0864a9f1, 0x8941ccd6, 0xd15f65fe, + 0x507a00d9, 0xcc04742d, 0x4d21110a, 0x153fb822, 0x941add05, + 0xa502ea72, 0x24278f55, 0x7c39267d, 0xfd1c435a, 0x1e094893, + 0x9f2c2db4, 0xc732849c, 0x4617e1bb, 0x770fd6cc, 0xf62ab3eb, + 0xae341ac3, 0x2f117fe4, 0x6b650fdf, 0xea406af8, 0xb25ec3d0, + 0x337ba6f7, 0x02639180, 0x8346f4a7, 0xdb585d8f, 0x5a7d38a8, + 0xb9683361, 0x384d5646, 0x6053ff6e, 0xe1769a49, 0xd06ead3e, + 0x514bc819, 0x09556131, 0x88700416, 0x140e70e2, 0x952b15c5, + 0xcd35bced, 0x4c10d9ca, 0x7d08eebd, 0xfc2d8b9a, 0xa43322b2, + 0x25164795, 0xc6034c5c, 0x4726297b, 0x1f388053, 0x9e1de574, + 0xaf05d203, 0x2e20b724, 0x763e1e0c, 0xf71b7b2b, 0x95b3f1a5, + 0x14969482, 0x4c883daa, 0xcdad588d, 0xfcb56ffa, 0x7d900add, + 0x258ea3f5, 0xa4abc6d2, 0x47becd1b, 0xc69ba83c, 0x9e850114, + 0x1fa06433, 0x2eb85344, 0xaf9d3663, 0xf7839f4b, 0x76a6fa6c, + 0xead88e98, 0x6bfdebbf, 0x33e34297, 0xb2c627b0, 0x83de10c7, + 0x02fb75e0, 0x5ae5dcc8, 0xdbc0b9ef, 0x38d5b226, 0xb9f0d701, + 0xe1ee7e29, 0x60cb1b0e, 0x51d32c79, 0xd0f6495e, 0x88e8e076, + 0x09cd8551}, + {0x00000000, 0x9b73ead4, 0xed96d3e9, 0x76e5393d, 0x005ca193, + 0x9b2f4b47, 0xedca727a, 0x76b998ae, 0x00b94326, 0x9bcaa9f2, + 0xed2f90cf, 0x765c7a1b, 0x00e5e2b5, 0x9b960861, 0xed73315c, + 0x7600db88, 0x0172864c, 0x9a016c98, 0xece455a5, 0x7797bf71, + 0x012e27df, 0x9a5dcd0b, 0xecb8f436, 0x77cb1ee2, 0x01cbc56a, + 0x9ab82fbe, 0xec5d1683, 0x772efc57, 0x019764f9, 0x9ae48e2d, + 0xec01b710, 0x77725dc4, 0x02e50c98, 0x9996e64c, 0xef73df71, + 0x740035a5, 0x02b9ad0b, 0x99ca47df, 0xef2f7ee2, 0x745c9436, + 0x025c4fbe, 0x992fa56a, 0xefca9c57, 0x74b97683, 0x0200ee2d, + 0x997304f9, 0xef963dc4, 0x74e5d710, 0x03978ad4, 0x98e46000, + 0xee01593d, 0x7572b3e9, 0x03cb2b47, 0x98b8c193, 0xee5df8ae, + 0x752e127a, 0x032ec9f2, 0x985d2326, 0xeeb81a1b, 0x75cbf0cf, + 0x03726861, 0x980182b5, 0xeee4bb88, 0x7597515c, 0x05ca1930, + 0x9eb9f3e4, 0xe85ccad9, 0x732f200d, 0x0596b8a3, 0x9ee55277, + 0xe8006b4a, 0x7373819e, 0x05735a16, 0x9e00b0c2, 0xe8e589ff, + 0x7396632b, 0x052ffb85, 0x9e5c1151, 0xe8b9286c, 0x73cac2b8, + 0x04b89f7c, 0x9fcb75a8, 0xe92e4c95, 0x725da641, 0x04e43eef, + 0x9f97d43b, 0xe972ed06, 0x720107d2, 0x0401dc5a, 0x9f72368e, + 0xe9970fb3, 0x72e4e567, 0x045d7dc9, 0x9f2e971d, 0xe9cbae20, + 0x72b844f4, 0x072f15a8, 0x9c5cff7c, 0xeab9c641, 0x71ca2c95, + 0x0773b43b, 0x9c005eef, 0xeae567d2, 0x71968d06, 0x0796568e, + 0x9ce5bc5a, 0xea008567, 0x71736fb3, 0x07caf71d, 0x9cb91dc9, + 0xea5c24f4, 0x712fce20, 0x065d93e4, 0x9d2e7930, 0xebcb400d, + 0x70b8aad9, 0x06013277, 0x9d72d8a3, 0xeb97e19e, 0x70e40b4a, + 0x06e4d0c2, 0x9d973a16, 0xeb72032b, 0x7001e9ff, 0x06b87151, + 0x9dcb9b85, 0xeb2ea2b8, 0x705d486c, 0x0b943260, 0x90e7d8b4, + 0xe602e189, 0x7d710b5d, 0x0bc893f3, 0x90bb7927, 0xe65e401a, + 0x7d2daace, 0x0b2d7146, 0x905e9b92, 0xe6bba2af, 0x7dc8487b, + 0x0b71d0d5, 0x90023a01, 0xe6e7033c, 0x7d94e9e8, 0x0ae6b42c, + 0x91955ef8, 0xe77067c5, 0x7c038d11, 0x0aba15bf, 0x91c9ff6b, + 0xe72cc656, 0x7c5f2c82, 0x0a5ff70a, 0x912c1dde, 0xe7c924e3, + 0x7cbace37, 0x0a035699, 0x9170bc4d, 0xe7958570, 0x7ce66fa4, + 0x09713ef8, 0x9202d42c, 0xe4e7ed11, 0x7f9407c5, 0x092d9f6b, + 0x925e75bf, 0xe4bb4c82, 0x7fc8a656, 0x09c87dde, 0x92bb970a, + 0xe45eae37, 0x7f2d44e3, 0x0994dc4d, 0x92e73699, 0xe4020fa4, + 0x7f71e570, 0x0803b8b4, 0x93705260, 0xe5956b5d, 0x7ee68189, + 0x085f1927, 0x932cf3f3, 0xe5c9cace, 0x7eba201a, 0x08bafb92, + 0x93c91146, 0xe52c287b, 0x7e5fc2af, 0x08e65a01, 0x9395b0d5, + 0xe57089e8, 0x7e03633c, 0x0e5e2b50, 0x952dc184, 0xe3c8f8b9, + 0x78bb126d, 0x0e028ac3, 0x95716017, 0xe394592a, 0x78e7b3fe, + 0x0ee76876, 0x959482a2, 0xe371bb9f, 0x7802514b, 0x0ebbc9e5, + 0x95c82331, 0xe32d1a0c, 0x785ef0d8, 0x0f2cad1c, 0x945f47c8, + 0xe2ba7ef5, 0x79c99421, 0x0f700c8f, 0x9403e65b, 0xe2e6df66, + 0x799535b2, 0x0f95ee3a, 0x94e604ee, 0xe2033dd3, 0x7970d707, + 0x0fc94fa9, 0x94baa57d, 0xe25f9c40, 0x792c7694, 0x0cbb27c8, + 0x97c8cd1c, 0xe12df421, 0x7a5e1ef5, 0x0ce7865b, 0x97946c8f, + 0xe17155b2, 0x7a02bf66, 0x0c0264ee, 0x97718e3a, 0xe194b707, + 0x7ae75dd3, 0x0c5ec57d, 0x972d2fa9, 0xe1c81694, 0x7abbfc40, + 0x0dc9a184, 0x96ba4b50, 0xe05f726d, 0x7b2c98b9, 0x0d950017, + 0x96e6eac3, 0xe003d3fe, 0x7b70392a, 0x0d70e2a2, 0x96030876, + 0xe0e6314b, 0x7b95db9f, 0x0d2c4331, 0x965fa9e5, 0xe0ba90d8, + 0x7bc97a0c}, + {0x00000000, 0x172864c0, 0x2e50c980, 0x3978ad40, 0x5ca19300, + 0x4b89f7c0, 0x72f15a80, 0x65d93e40, 0xb9432600, 0xae6b42c0, + 0x9713ef80, 0x803b8b40, 0xe5e2b500, 0xf2cad1c0, 0xcbb27c80, + 0xdc9a1840, 0xa9f74a41, 0xbedf2e81, 0x87a783c1, 0x908fe701, + 0xf556d941, 0xe27ebd81, 0xdb0610c1, 0xcc2e7401, 0x10b46c41, + 0x079c0881, 0x3ee4a5c1, 0x29ccc101, 0x4c15ff41, 0x5b3d9b81, + 0x624536c1, 0x756d5201, 0x889f92c3, 0x9fb7f603, 0xa6cf5b43, + 0xb1e73f83, 0xd43e01c3, 0xc3166503, 0xfa6ec843, 0xed46ac83, + 0x31dcb4c3, 0x26f4d003, 0x1f8c7d43, 0x08a41983, 0x6d7d27c3, + 0x7a554303, 0x432dee43, 0x54058a83, 0x2168d882, 0x3640bc42, + 0x0f381102, 0x181075c2, 0x7dc94b82, 0x6ae12f42, 0x53998202, + 0x44b1e6c2, 0x982bfe82, 0x8f039a42, 0xb67b3702, 0xa15353c2, + 0xc48a6d82, 0xd3a20942, 0xeadaa402, 0xfdf2c0c2, 0xca4e23c7, + 0xdd664707, 0xe41eea47, 0xf3368e87, 0x96efb0c7, 0x81c7d407, + 0xb8bf7947, 0xaf971d87, 0x730d05c7, 0x64256107, 0x5d5dcc47, + 0x4a75a887, 0x2fac96c7, 0x3884f207, 0x01fc5f47, 0x16d43b87, + 0x63b96986, 0x74910d46, 0x4de9a006, 0x5ac1c4c6, 0x3f18fa86, + 0x28309e46, 0x11483306, 0x066057c6, 0xdafa4f86, 0xcdd22b46, + 0xf4aa8606, 0xe382e2c6, 0x865bdc86, 0x9173b846, 0xa80b1506, + 0xbf2371c6, 0x42d1b104, 0x55f9d5c4, 0x6c817884, 0x7ba91c44, + 0x1e702204, 0x095846c4, 0x3020eb84, 0x27088f44, 0xfb929704, + 0xecbaf3c4, 0xd5c25e84, 0xc2ea3a44, 0xa7330404, 0xb01b60c4, + 0x8963cd84, 0x9e4ba944, 0xeb26fb45, 0xfc0e9f85, 0xc57632c5, + 0xd25e5605, 0xb7876845, 0xa0af0c85, 0x99d7a1c5, 0x8effc505, + 0x5265dd45, 0x454db985, 0x7c3514c5, 0x6b1d7005, 0x0ec44e45, + 0x19ec2a85, 0x209487c5, 0x37bce305, 0x4fed41cf, 0x58c5250f, + 0x61bd884f, 0x7695ec8f, 0x134cd2cf, 0x0464b60f, 0x3d1c1b4f, + 0x2a347f8f, 0xf6ae67cf, 0xe186030f, 0xd8feae4f, 0xcfd6ca8f, + 0xaa0ff4cf, 0xbd27900f, 0x845f3d4f, 0x9377598f, 0xe61a0b8e, + 0xf1326f4e, 0xc84ac20e, 0xdf62a6ce, 0xbabb988e, 0xad93fc4e, + 0x94eb510e, 0x83c335ce, 0x5f592d8e, 0x4871494e, 0x7109e40e, + 0x662180ce, 0x03f8be8e, 0x14d0da4e, 0x2da8770e, 0x3a8013ce, + 0xc772d30c, 0xd05ab7cc, 0xe9221a8c, 0xfe0a7e4c, 0x9bd3400c, + 0x8cfb24cc, 0xb583898c, 0xa2abed4c, 0x7e31f50c, 0x691991cc, + 0x50613c8c, 0x4749584c, 0x2290660c, 0x35b802cc, 0x0cc0af8c, + 0x1be8cb4c, 0x6e85994d, 0x79adfd8d, 0x40d550cd, 0x57fd340d, + 0x32240a4d, 0x250c6e8d, 0x1c74c3cd, 0x0b5ca70d, 0xd7c6bf4d, + 0xc0eedb8d, 0xf99676cd, 0xeebe120d, 0x8b672c4d, 0x9c4f488d, + 0xa537e5cd, 0xb21f810d, 0x85a36208, 0x928b06c8, 0xabf3ab88, + 0xbcdbcf48, 0xd902f108, 0xce2a95c8, 0xf7523888, 0xe07a5c48, + 0x3ce04408, 0x2bc820c8, 0x12b08d88, 0x0598e948, 0x6041d708, + 0x7769b3c8, 0x4e111e88, 0x59397a48, 0x2c542849, 0x3b7c4c89, + 0x0204e1c9, 0x152c8509, 0x70f5bb49, 0x67dddf89, 0x5ea572c9, + 0x498d1609, 0x95170e49, 0x823f6a89, 0xbb47c7c9, 0xac6fa309, + 0xc9b69d49, 0xde9ef989, 0xe7e654c9, 0xf0ce3009, 0x0d3cf0cb, + 0x1a14940b, 0x236c394b, 0x34445d8b, 0x519d63cb, 0x46b5070b, + 0x7fcdaa4b, 0x68e5ce8b, 0xb47fd6cb, 0xa357b20b, 0x9a2f1f4b, + 0x8d077b8b, 0xe8de45cb, 0xfff6210b, 0xc68e8c4b, 0xd1a6e88b, + 0xa4cbba8a, 0xb3e3de4a, 0x8a9b730a, 0x9db317ca, 0xf86a298a, + 0xef424d4a, 0xd63ae00a, 0xc11284ca, 0x1d889c8a, 0x0aa0f84a, + 0x33d8550a, 0x24f031ca, 0x41290f8a, 0x56016b4a, 0x6f79c60a, + 0x7851a2ca}, + {0x00000000, 0x9fda839e, 0xe4c4017d, 0x7b1e82e3, 0x12f904bb, + 0x8d238725, 0xf63d05c6, 0x69e78658, 0x25f20976, 0xba288ae8, + 0xc136080b, 0x5eec8b95, 0x370b0dcd, 0xa8d18e53, 0xd3cf0cb0, + 0x4c158f2e, 0x4be412ec, 0xd43e9172, 0xaf201391, 0x30fa900f, + 0x591d1657, 0xc6c795c9, 0xbdd9172a, 0x220394b4, 0x6e161b9a, + 0xf1cc9804, 0x8ad21ae7, 0x15089979, 0x7cef1f21, 0xe3359cbf, + 0x982b1e5c, 0x07f19dc2, 0x97c825d8, 0x0812a646, 0x730c24a5, + 0xecd6a73b, 0x85312163, 0x1aeba2fd, 0x61f5201e, 0xfe2fa380, + 0xb23a2cae, 0x2de0af30, 0x56fe2dd3, 0xc924ae4d, 0xa0c32815, + 0x3f19ab8b, 0x44072968, 0xdbddaaf6, 0xdc2c3734, 0x43f6b4aa, + 0x38e83649, 0xa732b5d7, 0xced5338f, 0x510fb011, 0x2a1132f2, + 0xb5cbb16c, 0xf9de3e42, 0x6604bddc, 0x1d1a3f3f, 0x82c0bca1, + 0xeb273af9, 0x74fdb967, 0x0fe33b84, 0x9039b81a, 0xf4e14df1, + 0x6b3bce6f, 0x10254c8c, 0x8fffcf12, 0xe618494a, 0x79c2cad4, + 0x02dc4837, 0x9d06cba9, 0xd1134487, 0x4ec9c719, 0x35d745fa, + 0xaa0dc664, 0xc3ea403c, 0x5c30c3a2, 0x272e4141, 0xb8f4c2df, + 0xbf055f1d, 0x20dfdc83, 0x5bc15e60, 0xc41bddfe, 0xadfc5ba6, + 0x3226d838, 0x49385adb, 0xd6e2d945, 0x9af7566b, 0x052dd5f5, + 0x7e335716, 0xe1e9d488, 0x880e52d0, 0x17d4d14e, 0x6cca53ad, + 0xf310d033, 0x63296829, 0xfcf3ebb7, 0x87ed6954, 0x1837eaca, + 0x71d06c92, 0xee0aef0c, 0x95146def, 0x0aceee71, 0x46db615f, + 0xd901e2c1, 0xa21f6022, 0x3dc5e3bc, 0x542265e4, 0xcbf8e67a, + 0xb0e66499, 0x2f3ce707, 0x28cd7ac5, 0xb717f95b, 0xcc097bb8, + 0x53d3f826, 0x3a347e7e, 0xa5eefde0, 0xdef07f03, 0x412afc9d, + 0x0d3f73b3, 0x92e5f02d, 0xe9fb72ce, 0x7621f150, 0x1fc67708, + 0x801cf496, 0xfb027675, 0x64d8f5eb, 0x32b39da3, 0xad691e3d, + 0xd6779cde, 0x49ad1f40, 0x204a9918, 0xbf901a86, 0xc48e9865, + 0x5b541bfb, 0x174194d5, 0x889b174b, 0xf38595a8, 0x6c5f1636, + 0x05b8906e, 0x9a6213f0, 0xe17c9113, 0x7ea6128d, 0x79578f4f, + 0xe68d0cd1, 0x9d938e32, 0x02490dac, 0x6bae8bf4, 0xf474086a, + 0x8f6a8a89, 0x10b00917, 0x5ca58639, 0xc37f05a7, 0xb8618744, + 0x27bb04da, 0x4e5c8282, 0xd186011c, 0xaa9883ff, 0x35420061, + 0xa57bb87b, 0x3aa13be5, 0x41bfb906, 0xde653a98, 0xb782bcc0, + 0x28583f5e, 0x5346bdbd, 0xcc9c3e23, 0x8089b10d, 0x1f533293, + 0x644db070, 0xfb9733ee, 0x9270b5b6, 0x0daa3628, 0x76b4b4cb, + 0xe96e3755, 0xee9faa97, 0x71452909, 0x0a5babea, 0x95812874, + 0xfc66ae2c, 0x63bc2db2, 0x18a2af51, 0x87782ccf, 0xcb6da3e1, + 0x54b7207f, 0x2fa9a29c, 0xb0732102, 0xd994a75a, 0x464e24c4, + 0x3d50a627, 0xa28a25b9, 0xc652d052, 0x598853cc, 0x2296d12f, + 0xbd4c52b1, 0xd4abd4e9, 0x4b715777, 0x306fd594, 0xafb5560a, + 0xe3a0d924, 0x7c7a5aba, 0x0764d859, 0x98be5bc7, 0xf159dd9f, + 0x6e835e01, 0x159ddce2, 0x8a475f7c, 0x8db6c2be, 0x126c4120, + 0x6972c3c3, 0xf6a8405d, 0x9f4fc605, 0x0095459b, 0x7b8bc778, + 0xe45144e6, 0xa844cbc8, 0x379e4856, 0x4c80cab5, 0xd35a492b, + 0xbabdcf73, 0x25674ced, 0x5e79ce0e, 0xc1a34d90, 0x519af58a, + 0xce407614, 0xb55ef4f7, 0x2a847769, 0x4363f131, 0xdcb972af, + 0xa7a7f04c, 0x387d73d2, 0x7468fcfc, 0xebb27f62, 0x90acfd81, + 0x0f767e1f, 0x6691f847, 0xf94b7bd9, 0x8255f93a, 0x1d8f7aa4, + 0x1a7ee766, 0x85a464f8, 0xfebae61b, 0x61606585, 0x0887e3dd, + 0x975d6043, 0xec43e2a0, 0x7399613e, 0x3f8cee10, 0xa0566d8e, + 0xdb48ef6d, 0x44926cf3, 0x2d75eaab, 0xb2af6935, 0xc9b1ebd6, + 0x566b6848}, + {0x00000000, 0x65673b46, 0xcace768c, 0xafa94dca, 0x4eedeb59, + 0x2b8ad01f, 0x84239dd5, 0xe144a693, 0x9ddbd6b2, 0xf8bcedf4, + 0x5715a03e, 0x32729b78, 0xd3363deb, 0xb65106ad, 0x19f84b67, + 0x7c9f7021, 0xe0c6ab25, 0x85a19063, 0x2a08dda9, 0x4f6fe6ef, + 0xae2b407c, 0xcb4c7b3a, 0x64e536f0, 0x01820db6, 0x7d1d7d97, + 0x187a46d1, 0xb7d30b1b, 0xd2b4305d, 0x33f096ce, 0x5697ad88, + 0xf93ee042, 0x9c59db04, 0x1afc500b, 0x7f9b6b4d, 0xd0322687, + 0xb5551dc1, 0x5411bb52, 0x31768014, 0x9edfcdde, 0xfbb8f698, + 0x872786b9, 0xe240bdff, 0x4de9f035, 0x288ecb73, 0xc9ca6de0, + 0xacad56a6, 0x03041b6c, 0x6663202a, 0xfa3afb2e, 0x9f5dc068, + 0x30f48da2, 0x5593b6e4, 0xb4d71077, 0xd1b02b31, 0x7e1966fb, + 0x1b7e5dbd, 0x67e12d9c, 0x028616da, 0xad2f5b10, 0xc8486056, + 0x290cc6c5, 0x4c6bfd83, 0xe3c2b049, 0x86a58b0f, 0x35f8a016, + 0x509f9b50, 0xff36d69a, 0x9a51eddc, 0x7b154b4f, 0x1e727009, + 0xb1db3dc3, 0xd4bc0685, 0xa82376a4, 0xcd444de2, 0x62ed0028, + 0x078a3b6e, 0xe6ce9dfd, 0x83a9a6bb, 0x2c00eb71, 0x4967d037, + 0xd53e0b33, 0xb0593075, 0x1ff07dbf, 0x7a9746f9, 0x9bd3e06a, + 0xfeb4db2c, 0x511d96e6, 0x347aada0, 0x48e5dd81, 0x2d82e6c7, + 0x822bab0d, 0xe74c904b, 0x060836d8, 0x636f0d9e, 0xccc64054, + 0xa9a17b12, 0x2f04f01d, 0x4a63cb5b, 0xe5ca8691, 0x80adbdd7, + 0x61e91b44, 0x048e2002, 0xab276dc8, 0xce40568e, 0xb2df26af, + 0xd7b81de9, 0x78115023, 0x1d766b65, 0xfc32cdf6, 0x9955f6b0, + 0x36fcbb7a, 0x539b803c, 0xcfc25b38, 0xaaa5607e, 0x050c2db4, + 0x606b16f2, 0x812fb061, 0xe4488b27, 0x4be1c6ed, 0x2e86fdab, + 0x52198d8a, 0x377eb6cc, 0x98d7fb06, 0xfdb0c040, 0x1cf466d3, + 0x79935d95, 0xd63a105f, 0xb35d2b19, 0x6bf1402c, 0x0e967b6a, + 0xa13f36a0, 0xc4580de6, 0x251cab75, 0x407b9033, 0xefd2ddf9, + 0x8ab5e6bf, 0xf62a969e, 0x934dadd8, 0x3ce4e012, 0x5983db54, + 0xb8c77dc7, 0xdda04681, 0x72090b4b, 0x176e300d, 0x8b37eb09, + 0xee50d04f, 0x41f99d85, 0x249ea6c3, 0xc5da0050, 0xa0bd3b16, + 0x0f1476dc, 0x6a734d9a, 0x16ec3dbb, 0x738b06fd, 0xdc224b37, + 0xb9457071, 0x5801d6e2, 0x3d66eda4, 0x92cfa06e, 0xf7a89b28, + 0x710d1027, 0x146a2b61, 0xbbc366ab, 0xdea45ded, 0x3fe0fb7e, + 0x5a87c038, 0xf52e8df2, 0x9049b6b4, 0xecd6c695, 0x89b1fdd3, + 0x2618b019, 0x437f8b5f, 0xa23b2dcc, 0xc75c168a, 0x68f55b40, + 0x0d926006, 0x91cbbb02, 0xf4ac8044, 0x5b05cd8e, 0x3e62f6c8, + 0xdf26505b, 0xba416b1d, 0x15e826d7, 0x708f1d91, 0x0c106db0, + 0x697756f6, 0xc6de1b3c, 0xa3b9207a, 0x42fd86e9, 0x279abdaf, + 0x8833f065, 0xed54cb23, 0x5e09e03a, 0x3b6edb7c, 0x94c796b6, + 0xf1a0adf0, 0x10e40b63, 0x75833025, 0xda2a7def, 0xbf4d46a9, + 0xc3d23688, 0xa6b50dce, 0x091c4004, 0x6c7b7b42, 0x8d3fddd1, + 0xe858e697, 0x47f1ab5d, 0x2296901b, 0xbecf4b1f, 0xdba87059, + 0x74013d93, 0x116606d5, 0xf022a046, 0x95459b00, 0x3aecd6ca, + 0x5f8bed8c, 0x23149dad, 0x4673a6eb, 0xe9daeb21, 0x8cbdd067, + 0x6df976f4, 0x089e4db2, 0xa7370078, 0xc2503b3e, 0x44f5b031, + 0x21928b77, 0x8e3bc6bd, 0xeb5cfdfb, 0x0a185b68, 0x6f7f602e, + 0xc0d62de4, 0xa5b116a2, 0xd92e6683, 0xbc495dc5, 0x13e0100f, + 0x76872b49, 0x97c38dda, 0xf2a4b69c, 0x5d0dfb56, 0x386ac010, + 0xa4331b14, 0xc1542052, 0x6efd6d98, 0x0b9a56de, 0xeadef04d, + 0x8fb9cb0b, 0x201086c1, 0x4577bd87, 0x39e8cda6, 0x5c8ff6e0, + 0xf326bb2a, 0x9641806c, 0x770526ff, 0x12621db9, 0xbdcb5073, + 0xd8ac6b35}, + {0x00000000, 0xd7e28058, 0x74b406f1, 0xa35686a9, 0xe9680de2, + 0x3e8a8dba, 0x9ddc0b13, 0x4a3e8b4b, 0x09a11d85, 0xde439ddd, + 0x7d151b74, 0xaaf79b2c, 0xe0c91067, 0x372b903f, 0x947d1696, + 0x439f96ce, 0x13423b0a, 0xc4a0bb52, 0x67f63dfb, 0xb014bda3, + 0xfa2a36e8, 0x2dc8b6b0, 0x8e9e3019, 0x597cb041, 0x1ae3268f, + 0xcd01a6d7, 0x6e57207e, 0xb9b5a026, 0xf38b2b6d, 0x2469ab35, + 0x873f2d9c, 0x50ddadc4, 0x26847614, 0xf166f64c, 0x523070e5, + 0x85d2f0bd, 0xcfec7bf6, 0x180efbae, 0xbb587d07, 0x6cbafd5f, + 0x2f256b91, 0xf8c7ebc9, 0x5b916d60, 0x8c73ed38, 0xc64d6673, + 0x11afe62b, 0xb2f96082, 0x651be0da, 0x35c64d1e, 0xe224cd46, + 0x41724bef, 0x9690cbb7, 0xdcae40fc, 0x0b4cc0a4, 0xa81a460d, + 0x7ff8c655, 0x3c67509b, 0xeb85d0c3, 0x48d3566a, 0x9f31d632, + 0xd50f5d79, 0x02eddd21, 0xa1bb5b88, 0x7659dbd0, 0x4d08ec28, + 0x9aea6c70, 0x39bcead9, 0xee5e6a81, 0xa460e1ca, 0x73826192, + 0xd0d4e73b, 0x07366763, 0x44a9f1ad, 0x934b71f5, 0x301df75c, + 0xe7ff7704, 0xadc1fc4f, 0x7a237c17, 0xd975fabe, 0x0e977ae6, + 0x5e4ad722, 0x89a8577a, 0x2afed1d3, 0xfd1c518b, 0xb722dac0, + 0x60c05a98, 0xc396dc31, 0x14745c69, 0x57ebcaa7, 0x80094aff, + 0x235fcc56, 0xf4bd4c0e, 0xbe83c745, 0x6961471d, 0xca37c1b4, + 0x1dd541ec, 0x6b8c9a3c, 0xbc6e1a64, 0x1f389ccd, 0xc8da1c95, + 0x82e497de, 0x55061786, 0xf650912f, 0x21b21177, 0x622d87b9, + 0xb5cf07e1, 0x16998148, 0xc17b0110, 0x8b458a5b, 0x5ca70a03, + 0xfff18caa, 0x28130cf2, 0x78cea136, 0xaf2c216e, 0x0c7aa7c7, + 0xdb98279f, 0x91a6acd4, 0x46442c8c, 0xe512aa25, 0x32f02a7d, + 0x716fbcb3, 0xa68d3ceb, 0x05dbba42, 0xd2393a1a, 0x9807b151, + 0x4fe53109, 0xecb3b7a0, 0x3b5137f8, 0x9a11d850, 0x4df35808, + 0xeea5dea1, 0x39475ef9, 0x7379d5b2, 0xa49b55ea, 0x07cdd343, + 0xd02f531b, 0x93b0c5d5, 0x4452458d, 0xe704c324, 0x30e6437c, + 0x7ad8c837, 0xad3a486f, 0x0e6ccec6, 0xd98e4e9e, 0x8953e35a, + 0x5eb16302, 0xfde7e5ab, 0x2a0565f3, 0x603beeb8, 0xb7d96ee0, + 0x148fe849, 0xc36d6811, 0x80f2fedf, 0x57107e87, 0xf446f82e, + 0x23a47876, 0x699af33d, 0xbe787365, 0x1d2ef5cc, 0xcacc7594, + 0xbc95ae44, 0x6b772e1c, 0xc821a8b5, 0x1fc328ed, 0x55fda3a6, + 0x821f23fe, 0x2149a557, 0xf6ab250f, 0xb534b3c1, 0x62d63399, + 0xc180b530, 0x16623568, 0x5c5cbe23, 0x8bbe3e7b, 0x28e8b8d2, + 0xff0a388a, 0xafd7954e, 0x78351516, 0xdb6393bf, 0x0c8113e7, + 0x46bf98ac, 0x915d18f4, 0x320b9e5d, 0xe5e91e05, 0xa67688cb, + 0x71940893, 0xd2c28e3a, 0x05200e62, 0x4f1e8529, 0x98fc0571, + 0x3baa83d8, 0xec480380, 0xd7193478, 0x00fbb420, 0xa3ad3289, + 0x744fb2d1, 0x3e71399a, 0xe993b9c2, 0x4ac53f6b, 0x9d27bf33, + 0xdeb829fd, 0x095aa9a5, 0xaa0c2f0c, 0x7deeaf54, 0x37d0241f, + 0xe032a447, 0x436422ee, 0x9486a2b6, 0xc45b0f72, 0x13b98f2a, + 0xb0ef0983, 0x670d89db, 0x2d330290, 0xfad182c8, 0x59870461, + 0x8e658439, 0xcdfa12f7, 0x1a1892af, 0xb94e1406, 0x6eac945e, + 0x24921f15, 0xf3709f4d, 0x502619e4, 0x87c499bc, 0xf19d426c, + 0x267fc234, 0x8529449d, 0x52cbc4c5, 0x18f54f8e, 0xcf17cfd6, + 0x6c41497f, 0xbba3c927, 0xf83c5fe9, 0x2fdedfb1, 0x8c885918, + 0x5b6ad940, 0x1154520b, 0xc6b6d253, 0x65e054fa, 0xb202d4a2, + 0xe2df7966, 0x353df93e, 0x966b7f97, 0x4189ffcf, 0x0bb77484, + 0xdc55f4dc, 0x7f037275, 0xa8e1f22d, 0xeb7e64e3, 0x3c9ce4bb, + 0x9fca6212, 0x4828e24a, 0x02166901, 0xd5f4e959, 0x76a26ff0, + 0xa140efa8}, + {0x00000000, 0xef52b6e1, 0x05d46b83, 0xea86dd62, 0x0ba8d706, + 0xe4fa61e7, 0x0e7cbc85, 0xe12e0a64, 0x1751ae0c, 0xf80318ed, + 0x1285c58f, 0xfdd7736e, 0x1cf9790a, 0xf3abcfeb, 0x192d1289, + 0xf67fa468, 0x2ea35c18, 0xc1f1eaf9, 0x2b77379b, 0xc425817a, + 0x250b8b1e, 0xca593dff, 0x20dfe09d, 0xcf8d567c, 0x39f2f214, + 0xd6a044f5, 0x3c269997, 0xd3742f76, 0x325a2512, 0xdd0893f3, + 0x378e4e91, 0xd8dcf870, 0x5d46b830, 0xb2140ed1, 0x5892d3b3, + 0xb7c06552, 0x56ee6f36, 0xb9bcd9d7, 0x533a04b5, 0xbc68b254, + 0x4a17163c, 0xa545a0dd, 0x4fc37dbf, 0xa091cb5e, 0x41bfc13a, + 0xaeed77db, 0x446baab9, 0xab391c58, 0x73e5e428, 0x9cb752c9, + 0x76318fab, 0x9963394a, 0x784d332e, 0x971f85cf, 0x7d9958ad, + 0x92cbee4c, 0x64b44a24, 0x8be6fcc5, 0x616021a7, 0x8e329746, + 0x6f1c9d22, 0x804e2bc3, 0x6ac8f6a1, 0x859a4040, 0xba8d7060, + 0x55dfc681, 0xbf591be3, 0x500bad02, 0xb125a766, 0x5e771187, + 0xb4f1cce5, 0x5ba37a04, 0xaddcde6c, 0x428e688d, 0xa808b5ef, + 0x475a030e, 0xa674096a, 0x4926bf8b, 0xa3a062e9, 0x4cf2d408, + 0x942e2c78, 0x7b7c9a99, 0x91fa47fb, 0x7ea8f11a, 0x9f86fb7e, + 0x70d44d9f, 0x9a5290fd, 0x7500261c, 0x837f8274, 0x6c2d3495, + 0x86abe9f7, 0x69f95f16, 0x88d75572, 0x6785e393, 0x8d033ef1, + 0x62518810, 0xe7cbc850, 0x08997eb1, 0xe21fa3d3, 0x0d4d1532, + 0xec631f56, 0x0331a9b7, 0xe9b774d5, 0x06e5c234, 0xf09a665c, + 0x1fc8d0bd, 0xf54e0ddf, 0x1a1cbb3e, 0xfb32b15a, 0x146007bb, + 0xfee6dad9, 0x11b46c38, 0xc9689448, 0x263a22a9, 0xccbcffcb, + 0x23ee492a, 0xc2c0434e, 0x2d92f5af, 0xc71428cd, 0x28469e2c, + 0xde393a44, 0x316b8ca5, 0xdbed51c7, 0x34bfe726, 0xd591ed42, + 0x3ac35ba3, 0xd04586c1, 0x3f173020, 0xae6be681, 0x41395060, + 0xabbf8d02, 0x44ed3be3, 0xa5c33187, 0x4a918766, 0xa0175a04, + 0x4f45ece5, 0xb93a488d, 0x5668fe6c, 0xbcee230e, 0x53bc95ef, + 0xb2929f8b, 0x5dc0296a, 0xb746f408, 0x581442e9, 0x80c8ba99, + 0x6f9a0c78, 0x851cd11a, 0x6a4e67fb, 0x8b606d9f, 0x6432db7e, + 0x8eb4061c, 0x61e6b0fd, 0x97991495, 0x78cba274, 0x924d7f16, + 0x7d1fc9f7, 0x9c31c393, 0x73637572, 0x99e5a810, 0x76b71ef1, + 0xf32d5eb1, 0x1c7fe850, 0xf6f93532, 0x19ab83d3, 0xf88589b7, + 0x17d73f56, 0xfd51e234, 0x120354d5, 0xe47cf0bd, 0x0b2e465c, + 0xe1a89b3e, 0x0efa2ddf, 0xefd427bb, 0x0086915a, 0xea004c38, + 0x0552fad9, 0xdd8e02a9, 0x32dcb448, 0xd85a692a, 0x3708dfcb, + 0xd626d5af, 0x3974634e, 0xd3f2be2c, 0x3ca008cd, 0xcadfaca5, + 0x258d1a44, 0xcf0bc726, 0x205971c7, 0xc1777ba3, 0x2e25cd42, + 0xc4a31020, 0x2bf1a6c1, 0x14e696e1, 0xfbb42000, 0x1132fd62, + 0xfe604b83, 0x1f4e41e7, 0xf01cf706, 0x1a9a2a64, 0xf5c89c85, + 0x03b738ed, 0xece58e0c, 0x0663536e, 0xe931e58f, 0x081fefeb, + 0xe74d590a, 0x0dcb8468, 0xe2993289, 0x3a45caf9, 0xd5177c18, + 0x3f91a17a, 0xd0c3179b, 0x31ed1dff, 0xdebfab1e, 0x3439767c, + 0xdb6bc09d, 0x2d1464f5, 0xc246d214, 0x28c00f76, 0xc792b997, + 0x26bcb3f3, 0xc9ee0512, 0x2368d870, 0xcc3a6e91, 0x49a02ed1, + 0xa6f29830, 0x4c744552, 0xa326f3b3, 0x4208f9d7, 0xad5a4f36, + 0x47dc9254, 0xa88e24b5, 0x5ef180dd, 0xb1a3363c, 0x5b25eb5e, + 0xb4775dbf, 0x555957db, 0xba0be13a, 0x508d3c58, 0xbfdf8ab9, + 0x670372c9, 0x8851c428, 0x62d7194a, 0x8d85afab, 0x6caba5cf, + 0x83f9132e, 0x697fce4c, 0x862d78ad, 0x7052dcc5, 0x9f006a24, + 0x7586b746, 0x9ad401a7, 0x7bfa0bc3, 0x94a8bd22, 0x7e2e6040, + 0x917cd6a1}, + {0x00000000, 0x87a6cb43, 0xd43c90c7, 0x539a5b84, 0x730827cf, + 0xf4aeec8c, 0xa734b708, 0x20927c4b, 0xe6104f9e, 0x61b684dd, + 0x322cdf59, 0xb58a141a, 0x95186851, 0x12bea312, 0x4124f896, + 0xc68233d5, 0x1751997d, 0x90f7523e, 0xc36d09ba, 0x44cbc2f9, + 0x6459beb2, 0xe3ff75f1, 0xb0652e75, 0x37c3e536, 0xf141d6e3, + 0x76e71da0, 0x257d4624, 0xa2db8d67, 0x8249f12c, 0x05ef3a6f, + 0x567561eb, 0xd1d3aaa8, 0x2ea332fa, 0xa905f9b9, 0xfa9fa23d, + 0x7d39697e, 0x5dab1535, 0xda0dde76, 0x899785f2, 0x0e314eb1, + 0xc8b37d64, 0x4f15b627, 0x1c8feda3, 0x9b2926e0, 0xbbbb5aab, + 0x3c1d91e8, 0x6f87ca6c, 0xe821012f, 0x39f2ab87, 0xbe5460c4, + 0xedce3b40, 0x6a68f003, 0x4afa8c48, 0xcd5c470b, 0x9ec61c8f, + 0x1960d7cc, 0xdfe2e419, 0x58442f5a, 0x0bde74de, 0x8c78bf9d, + 0xaceac3d6, 0x2b4c0895, 0x78d65311, 0xff709852, 0x5d4665f4, + 0xdae0aeb7, 0x897af533, 0x0edc3e70, 0x2e4e423b, 0xa9e88978, + 0xfa72d2fc, 0x7dd419bf, 0xbb562a6a, 0x3cf0e129, 0x6f6abaad, + 0xe8cc71ee, 0xc85e0da5, 0x4ff8c6e6, 0x1c629d62, 0x9bc45621, + 0x4a17fc89, 0xcdb137ca, 0x9e2b6c4e, 0x198da70d, 0x391fdb46, + 0xbeb91005, 0xed234b81, 0x6a8580c2, 0xac07b317, 0x2ba17854, + 0x783b23d0, 0xff9de893, 0xdf0f94d8, 0x58a95f9b, 0x0b33041f, + 0x8c95cf5c, 0x73e5570e, 0xf4439c4d, 0xa7d9c7c9, 0x207f0c8a, + 0x00ed70c1, 0x874bbb82, 0xd4d1e006, 0x53772b45, 0x95f51890, + 0x1253d3d3, 0x41c98857, 0xc66f4314, 0xe6fd3f5f, 0x615bf41c, + 0x32c1af98, 0xb56764db, 0x64b4ce73, 0xe3120530, 0xb0885eb4, + 0x372e95f7, 0x17bce9bc, 0x901a22ff, 0xc380797b, 0x4426b238, + 0x82a481ed, 0x05024aae, 0x5698112a, 0xd13eda69, 0xf1aca622, + 0x760a6d61, 0x259036e5, 0xa236fda6, 0xba8ccbe8, 0x3d2a00ab, + 0x6eb05b2f, 0xe916906c, 0xc984ec27, 0x4e222764, 0x1db87ce0, + 0x9a1eb7a3, 0x5c9c8476, 0xdb3a4f35, 0x88a014b1, 0x0f06dff2, + 0x2f94a3b9, 0xa83268fa, 0xfba8337e, 0x7c0ef83d, 0xaddd5295, + 0x2a7b99d6, 0x79e1c252, 0xfe470911, 0xded5755a, 0x5973be19, + 0x0ae9e59d, 0x8d4f2ede, 0x4bcd1d0b, 0xcc6bd648, 0x9ff18dcc, + 0x1857468f, 0x38c53ac4, 0xbf63f187, 0xecf9aa03, 0x6b5f6140, + 0x942ff912, 0x13893251, 0x401369d5, 0xc7b5a296, 0xe727dedd, + 0x6081159e, 0x331b4e1a, 0xb4bd8559, 0x723fb68c, 0xf5997dcf, + 0xa603264b, 0x21a5ed08, 0x01379143, 0x86915a00, 0xd50b0184, + 0x52adcac7, 0x837e606f, 0x04d8ab2c, 0x5742f0a8, 0xd0e43beb, + 0xf07647a0, 0x77d08ce3, 0x244ad767, 0xa3ec1c24, 0x656e2ff1, + 0xe2c8e4b2, 0xb152bf36, 0x36f47475, 0x1666083e, 0x91c0c37d, + 0xc25a98f9, 0x45fc53ba, 0xe7caae1c, 0x606c655f, 0x33f63edb, + 0xb450f598, 0x94c289d3, 0x13644290, 0x40fe1914, 0xc758d257, + 0x01dae182, 0x867c2ac1, 0xd5e67145, 0x5240ba06, 0x72d2c64d, + 0xf5740d0e, 0xa6ee568a, 0x21489dc9, 0xf09b3761, 0x773dfc22, + 0x24a7a7a6, 0xa3016ce5, 0x839310ae, 0x0435dbed, 0x57af8069, + 0xd0094b2a, 0x168b78ff, 0x912db3bc, 0xc2b7e838, 0x4511237b, + 0x65835f30, 0xe2259473, 0xb1bfcff7, 0x361904b4, 0xc9699ce6, + 0x4ecf57a5, 0x1d550c21, 0x9af3c762, 0xba61bb29, 0x3dc7706a, + 0x6e5d2bee, 0xe9fbe0ad, 0x2f79d378, 0xa8df183b, 0xfb4543bf, + 0x7ce388fc, 0x5c71f4b7, 0xdbd73ff4, 0x884d6470, 0x0febaf33, + 0xde38059b, 0x599eced8, 0x0a04955c, 0x8da25e1f, 0xad302254, + 0x2a96e917, 0x790cb293, 0xfeaa79d0, 0x38284a05, 0xbf8e8146, + 0xec14dac2, 0x6bb21181, 0x4b206dca, 0xcc86a689, 0x9f1cfd0d, + 0x18ba364e}}; + +local const z_word_t FAR crc_braid_big_table[][256] = { + {0x0000000000000000, 0x43cba68700000000, 0xc7903cd400000000, + 0x845b9a5300000000, 0xcf27087300000000, 0x8cecaef400000000, + 0x08b734a700000000, 0x4b7c922000000000, 0x9e4f10e600000000, + 0xdd84b66100000000, 0x59df2c3200000000, 0x1a148ab500000000, + 0x5168189500000000, 0x12a3be1200000000, 0x96f8244100000000, + 0xd53382c600000000, 0x7d99511700000000, 0x3e52f79000000000, + 0xba096dc300000000, 0xf9c2cb4400000000, 0xb2be596400000000, + 0xf175ffe300000000, 0x752e65b000000000, 0x36e5c33700000000, + 0xe3d641f100000000, 0xa01de77600000000, 0x24467d2500000000, + 0x678ddba200000000, 0x2cf1498200000000, 0x6f3aef0500000000, + 0xeb61755600000000, 0xa8aad3d100000000, 0xfa32a32e00000000, + 0xb9f905a900000000, 0x3da29ffa00000000, 0x7e69397d00000000, + 0x3515ab5d00000000, 0x76de0dda00000000, 0xf285978900000000, + 0xb14e310e00000000, 0x647db3c800000000, 0x27b6154f00000000, + 0xa3ed8f1c00000000, 0xe026299b00000000, 0xab5abbbb00000000, + 0xe8911d3c00000000, 0x6cca876f00000000, 0x2f0121e800000000, + 0x87abf23900000000, 0xc46054be00000000, 0x403bceed00000000, + 0x03f0686a00000000, 0x488cfa4a00000000, 0x0b475ccd00000000, + 0x8f1cc69e00000000, 0xccd7601900000000, 0x19e4e2df00000000, + 0x5a2f445800000000, 0xde74de0b00000000, 0x9dbf788c00000000, + 0xd6c3eaac00000000, 0x95084c2b00000000, 0x1153d67800000000, + 0x529870ff00000000, 0xf465465d00000000, 0xb7aee0da00000000, + 0x33f57a8900000000, 0x703edc0e00000000, 0x3b424e2e00000000, + 0x7889e8a900000000, 0xfcd272fa00000000, 0xbf19d47d00000000, + 0x6a2a56bb00000000, 0x29e1f03c00000000, 0xadba6a6f00000000, + 0xee71cce800000000, 0xa50d5ec800000000, 0xe6c6f84f00000000, + 0x629d621c00000000, 0x2156c49b00000000, 0x89fc174a00000000, + 0xca37b1cd00000000, 0x4e6c2b9e00000000, 0x0da78d1900000000, + 0x46db1f3900000000, 0x0510b9be00000000, 0x814b23ed00000000, + 0xc280856a00000000, 0x17b307ac00000000, 0x5478a12b00000000, + 0xd0233b7800000000, 0x93e89dff00000000, 0xd8940fdf00000000, + 0x9b5fa95800000000, 0x1f04330b00000000, 0x5ccf958c00000000, + 0x0e57e57300000000, 0x4d9c43f400000000, 0xc9c7d9a700000000, + 0x8a0c7f2000000000, 0xc170ed0000000000, 0x82bb4b8700000000, + 0x06e0d1d400000000, 0x452b775300000000, 0x9018f59500000000, + 0xd3d3531200000000, 0x5788c94100000000, 0x14436fc600000000, + 0x5f3ffde600000000, 0x1cf45b6100000000, 0x98afc13200000000, + 0xdb6467b500000000, 0x73ceb46400000000, 0x300512e300000000, + 0xb45e88b000000000, 0xf7952e3700000000, 0xbce9bc1700000000, + 0xff221a9000000000, 0x7b7980c300000000, 0x38b2264400000000, + 0xed81a48200000000, 0xae4a020500000000, 0x2a11985600000000, + 0x69da3ed100000000, 0x22a6acf100000000, 0x616d0a7600000000, + 0xe536902500000000, 0xa6fd36a200000000, 0xe8cb8cba00000000, + 0xab002a3d00000000, 0x2f5bb06e00000000, 0x6c9016e900000000, + 0x27ec84c900000000, 0x6427224e00000000, 0xe07cb81d00000000, + 0xa3b71e9a00000000, 0x76849c5c00000000, 0x354f3adb00000000, + 0xb114a08800000000, 0xf2df060f00000000, 0xb9a3942f00000000, + 0xfa6832a800000000, 0x7e33a8fb00000000, 0x3df80e7c00000000, + 0x9552ddad00000000, 0xd6997b2a00000000, 0x52c2e17900000000, + 0x110947fe00000000, 0x5a75d5de00000000, 0x19be735900000000, + 0x9de5e90a00000000, 0xde2e4f8d00000000, 0x0b1dcd4b00000000, + 0x48d66bcc00000000, 0xcc8df19f00000000, 0x8f46571800000000, + 0xc43ac53800000000, 0x87f163bf00000000, 0x03aaf9ec00000000, + 0x40615f6b00000000, 0x12f92f9400000000, 0x5132891300000000, + 0xd569134000000000, 0x96a2b5c700000000, 0xddde27e700000000, + 0x9e15816000000000, 0x1a4e1b3300000000, 0x5985bdb400000000, + 0x8cb63f7200000000, 0xcf7d99f500000000, 0x4b2603a600000000, + 0x08eda52100000000, 0x4391370100000000, 0x005a918600000000, + 0x84010bd500000000, 0xc7caad5200000000, 0x6f607e8300000000, + 0x2cabd80400000000, 0xa8f0425700000000, 0xeb3be4d000000000, + 0xa04776f000000000, 0xe38cd07700000000, 0x67d74a2400000000, + 0x241ceca300000000, 0xf12f6e6500000000, 0xb2e4c8e200000000, + 0x36bf52b100000000, 0x7574f43600000000, 0x3e08661600000000, + 0x7dc3c09100000000, 0xf9985ac200000000, 0xba53fc4500000000, + 0x1caecae700000000, 0x5f656c6000000000, 0xdb3ef63300000000, + 0x98f550b400000000, 0xd389c29400000000, 0x9042641300000000, + 0x1419fe4000000000, 0x57d258c700000000, 0x82e1da0100000000, + 0xc12a7c8600000000, 0x4571e6d500000000, 0x06ba405200000000, + 0x4dc6d27200000000, 0x0e0d74f500000000, 0x8a56eea600000000, + 0xc99d482100000000, 0x61379bf000000000, 0x22fc3d7700000000, + 0xa6a7a72400000000, 0xe56c01a300000000, 0xae10938300000000, + 0xeddb350400000000, 0x6980af5700000000, 0x2a4b09d000000000, + 0xff788b1600000000, 0xbcb32d9100000000, 0x38e8b7c200000000, + 0x7b23114500000000, 0x305f836500000000, 0x739425e200000000, + 0xf7cfbfb100000000, 0xb404193600000000, 0xe69c69c900000000, + 0xa557cf4e00000000, 0x210c551d00000000, 0x62c7f39a00000000, + 0x29bb61ba00000000, 0x6a70c73d00000000, 0xee2b5d6e00000000, + 0xade0fbe900000000, 0x78d3792f00000000, 0x3b18dfa800000000, + 0xbf4345fb00000000, 0xfc88e37c00000000, 0xb7f4715c00000000, + 0xf43fd7db00000000, 0x70644d8800000000, 0x33afeb0f00000000, + 0x9b0538de00000000, 0xd8ce9e5900000000, 0x5c95040a00000000, + 0x1f5ea28d00000000, 0x542230ad00000000, 0x17e9962a00000000, + 0x93b20c7900000000, 0xd079aafe00000000, 0x054a283800000000, + 0x46818ebf00000000, 0xc2da14ec00000000, 0x8111b26b00000000, + 0xca6d204b00000000, 0x89a686cc00000000, 0x0dfd1c9f00000000, + 0x4e36ba1800000000}, + {0x0000000000000000, 0xe1b652ef00000000, 0x836bd40500000000, + 0x62dd86ea00000000, 0x06d7a80b00000000, 0xe761fae400000000, + 0x85bc7c0e00000000, 0x640a2ee100000000, 0x0cae511700000000, + 0xed1803f800000000, 0x8fc5851200000000, 0x6e73d7fd00000000, + 0x0a79f91c00000000, 0xebcfabf300000000, 0x89122d1900000000, + 0x68a47ff600000000, 0x185ca32e00000000, 0xf9eaf1c100000000, + 0x9b37772b00000000, 0x7a8125c400000000, 0x1e8b0b2500000000, + 0xff3d59ca00000000, 0x9de0df2000000000, 0x7c568dcf00000000, + 0x14f2f23900000000, 0xf544a0d600000000, 0x9799263c00000000, + 0x762f74d300000000, 0x12255a3200000000, 0xf39308dd00000000, + 0x914e8e3700000000, 0x70f8dcd800000000, 0x30b8465d00000000, + 0xd10e14b200000000, 0xb3d3925800000000, 0x5265c0b700000000, + 0x366fee5600000000, 0xd7d9bcb900000000, 0xb5043a5300000000, + 0x54b268bc00000000, 0x3c16174a00000000, 0xdda045a500000000, + 0xbf7dc34f00000000, 0x5ecb91a000000000, 0x3ac1bf4100000000, + 0xdb77edae00000000, 0xb9aa6b4400000000, 0x581c39ab00000000, + 0x28e4e57300000000, 0xc952b79c00000000, 0xab8f317600000000, + 0x4a39639900000000, 0x2e334d7800000000, 0xcf851f9700000000, + 0xad58997d00000000, 0x4ceecb9200000000, 0x244ab46400000000, + 0xc5fce68b00000000, 0xa721606100000000, 0x4697328e00000000, + 0x229d1c6f00000000, 0xc32b4e8000000000, 0xa1f6c86a00000000, + 0x40409a8500000000, 0x60708dba00000000, 0x81c6df5500000000, + 0xe31b59bf00000000, 0x02ad0b5000000000, 0x66a725b100000000, + 0x8711775e00000000, 0xe5ccf1b400000000, 0x047aa35b00000000, + 0x6cdedcad00000000, 0x8d688e4200000000, 0xefb508a800000000, + 0x0e035a4700000000, 0x6a0974a600000000, 0x8bbf264900000000, + 0xe962a0a300000000, 0x08d4f24c00000000, 0x782c2e9400000000, + 0x999a7c7b00000000, 0xfb47fa9100000000, 0x1af1a87e00000000, + 0x7efb869f00000000, 0x9f4dd47000000000, 0xfd90529a00000000, + 0x1c26007500000000, 0x74827f8300000000, 0x95342d6c00000000, + 0xf7e9ab8600000000, 0x165ff96900000000, 0x7255d78800000000, + 0x93e3856700000000, 0xf13e038d00000000, 0x1088516200000000, + 0x50c8cbe700000000, 0xb17e990800000000, 0xd3a31fe200000000, + 0x32154d0d00000000, 0x561f63ec00000000, 0xb7a9310300000000, + 0xd574b7e900000000, 0x34c2e50600000000, 0x5c669af000000000, + 0xbdd0c81f00000000, 0xdf0d4ef500000000, 0x3ebb1c1a00000000, + 0x5ab132fb00000000, 0xbb07601400000000, 0xd9dae6fe00000000, + 0x386cb41100000000, 0x489468c900000000, 0xa9223a2600000000, + 0xcbffbccc00000000, 0x2a49ee2300000000, 0x4e43c0c200000000, + 0xaff5922d00000000, 0xcd2814c700000000, 0x2c9e462800000000, + 0x443a39de00000000, 0xa58c6b3100000000, 0xc751eddb00000000, + 0x26e7bf3400000000, 0x42ed91d500000000, 0xa35bc33a00000000, + 0xc18645d000000000, 0x2030173f00000000, 0x81e66bae00000000, + 0x6050394100000000, 0x028dbfab00000000, 0xe33bed4400000000, + 0x8731c3a500000000, 0x6687914a00000000, 0x045a17a000000000, + 0xe5ec454f00000000, 0x8d483ab900000000, 0x6cfe685600000000, + 0x0e23eebc00000000, 0xef95bc5300000000, 0x8b9f92b200000000, + 0x6a29c05d00000000, 0x08f446b700000000, 0xe942145800000000, + 0x99bac88000000000, 0x780c9a6f00000000, 0x1ad11c8500000000, + 0xfb674e6a00000000, 0x9f6d608b00000000, 0x7edb326400000000, + 0x1c06b48e00000000, 0xfdb0e66100000000, 0x9514999700000000, + 0x74a2cb7800000000, 0x167f4d9200000000, 0xf7c91f7d00000000, + 0x93c3319c00000000, 0x7275637300000000, 0x10a8e59900000000, + 0xf11eb77600000000, 0xb15e2df300000000, 0x50e87f1c00000000, + 0x3235f9f600000000, 0xd383ab1900000000, 0xb78985f800000000, + 0x563fd71700000000, 0x34e251fd00000000, 0xd554031200000000, + 0xbdf07ce400000000, 0x5c462e0b00000000, 0x3e9ba8e100000000, + 0xdf2dfa0e00000000, 0xbb27d4ef00000000, 0x5a91860000000000, + 0x384c00ea00000000, 0xd9fa520500000000, 0xa9028edd00000000, + 0x48b4dc3200000000, 0x2a695ad800000000, 0xcbdf083700000000, + 0xafd526d600000000, 0x4e63743900000000, 0x2cbef2d300000000, + 0xcd08a03c00000000, 0xa5acdfca00000000, 0x441a8d2500000000, + 0x26c70bcf00000000, 0xc771592000000000, 0xa37b77c100000000, + 0x42cd252e00000000, 0x2010a3c400000000, 0xc1a6f12b00000000, + 0xe196e61400000000, 0x0020b4fb00000000, 0x62fd321100000000, + 0x834b60fe00000000, 0xe7414e1f00000000, 0x06f71cf000000000, + 0x642a9a1a00000000, 0x859cc8f500000000, 0xed38b70300000000, + 0x0c8ee5ec00000000, 0x6e53630600000000, 0x8fe531e900000000, + 0xebef1f0800000000, 0x0a594de700000000, 0x6884cb0d00000000, + 0x893299e200000000, 0xf9ca453a00000000, 0x187c17d500000000, + 0x7aa1913f00000000, 0x9b17c3d000000000, 0xff1ded3100000000, + 0x1eabbfde00000000, 0x7c76393400000000, 0x9dc06bdb00000000, + 0xf564142d00000000, 0x14d246c200000000, 0x760fc02800000000, + 0x97b992c700000000, 0xf3b3bc2600000000, 0x1205eec900000000, + 0x70d8682300000000, 0x916e3acc00000000, 0xd12ea04900000000, + 0x3098f2a600000000, 0x5245744c00000000, 0xb3f326a300000000, + 0xd7f9084200000000, 0x364f5aad00000000, 0x5492dc4700000000, + 0xb5248ea800000000, 0xdd80f15e00000000, 0x3c36a3b100000000, + 0x5eeb255b00000000, 0xbf5d77b400000000, 0xdb57595500000000, + 0x3ae10bba00000000, 0x583c8d5000000000, 0xb98adfbf00000000, + 0xc972036700000000, 0x28c4518800000000, 0x4a19d76200000000, + 0xabaf858d00000000, 0xcfa5ab6c00000000, 0x2e13f98300000000, + 0x4cce7f6900000000, 0xad782d8600000000, 0xc5dc527000000000, + 0x246a009f00000000, 0x46b7867500000000, 0xa701d49a00000000, + 0xc30bfa7b00000000, 0x22bda89400000000, 0x40602e7e00000000, + 0xa1d67c9100000000}, + {0x0000000000000000, 0x5880e2d700000000, 0xf106b47400000000, + 0xa98656a300000000, 0xe20d68e900000000, 0xba8d8a3e00000000, + 0x130bdc9d00000000, 0x4b8b3e4a00000000, 0x851da10900000000, + 0xdd9d43de00000000, 0x741b157d00000000, 0x2c9bf7aa00000000, + 0x6710c9e000000000, 0x3f902b3700000000, 0x96167d9400000000, + 0xce969f4300000000, 0x0a3b421300000000, 0x52bba0c400000000, + 0xfb3df66700000000, 0xa3bd14b000000000, 0xe8362afa00000000, + 0xb0b6c82d00000000, 0x19309e8e00000000, 0x41b07c5900000000, + 0x8f26e31a00000000, 0xd7a601cd00000000, 0x7e20576e00000000, + 0x26a0b5b900000000, 0x6d2b8bf300000000, 0x35ab692400000000, + 0x9c2d3f8700000000, 0xc4addd5000000000, 0x1476842600000000, + 0x4cf666f100000000, 0xe570305200000000, 0xbdf0d28500000000, + 0xf67beccf00000000, 0xaefb0e1800000000, 0x077d58bb00000000, + 0x5ffdba6c00000000, 0x916b252f00000000, 0xc9ebc7f800000000, + 0x606d915b00000000, 0x38ed738c00000000, 0x73664dc600000000, + 0x2be6af1100000000, 0x8260f9b200000000, 0xdae01b6500000000, + 0x1e4dc63500000000, 0x46cd24e200000000, 0xef4b724100000000, + 0xb7cb909600000000, 0xfc40aedc00000000, 0xa4c04c0b00000000, + 0x0d461aa800000000, 0x55c6f87f00000000, 0x9b50673c00000000, + 0xc3d085eb00000000, 0x6a56d34800000000, 0x32d6319f00000000, + 0x795d0fd500000000, 0x21dded0200000000, 0x885bbba100000000, + 0xd0db597600000000, 0x28ec084d00000000, 0x706cea9a00000000, + 0xd9eabc3900000000, 0x816a5eee00000000, 0xcae160a400000000, + 0x9261827300000000, 0x3be7d4d000000000, 0x6367360700000000, + 0xadf1a94400000000, 0xf5714b9300000000, 0x5cf71d3000000000, + 0x0477ffe700000000, 0x4ffcc1ad00000000, 0x177c237a00000000, + 0xbefa75d900000000, 0xe67a970e00000000, 0x22d74a5e00000000, + 0x7a57a88900000000, 0xd3d1fe2a00000000, 0x8b511cfd00000000, + 0xc0da22b700000000, 0x985ac06000000000, 0x31dc96c300000000, + 0x695c741400000000, 0xa7caeb5700000000, 0xff4a098000000000, + 0x56cc5f2300000000, 0x0e4cbdf400000000, 0x45c783be00000000, + 0x1d47616900000000, 0xb4c137ca00000000, 0xec41d51d00000000, + 0x3c9a8c6b00000000, 0x641a6ebc00000000, 0xcd9c381f00000000, + 0x951cdac800000000, 0xde97e48200000000, 0x8617065500000000, + 0x2f9150f600000000, 0x7711b22100000000, 0xb9872d6200000000, + 0xe107cfb500000000, 0x4881991600000000, 0x10017bc100000000, + 0x5b8a458b00000000, 0x030aa75c00000000, 0xaa8cf1ff00000000, + 0xf20c132800000000, 0x36a1ce7800000000, 0x6e212caf00000000, + 0xc7a77a0c00000000, 0x9f2798db00000000, 0xd4aca69100000000, + 0x8c2c444600000000, 0x25aa12e500000000, 0x7d2af03200000000, + 0xb3bc6f7100000000, 0xeb3c8da600000000, 0x42badb0500000000, + 0x1a3a39d200000000, 0x51b1079800000000, 0x0931e54f00000000, + 0xa0b7b3ec00000000, 0xf837513b00000000, 0x50d8119a00000000, + 0x0858f34d00000000, 0xa1dea5ee00000000, 0xf95e473900000000, + 0xb2d5797300000000, 0xea559ba400000000, 0x43d3cd0700000000, + 0x1b532fd000000000, 0xd5c5b09300000000, 0x8d45524400000000, + 0x24c304e700000000, 0x7c43e63000000000, 0x37c8d87a00000000, + 0x6f483aad00000000, 0xc6ce6c0e00000000, 0x9e4e8ed900000000, + 0x5ae3538900000000, 0x0263b15e00000000, 0xabe5e7fd00000000, + 0xf365052a00000000, 0xb8ee3b6000000000, 0xe06ed9b700000000, + 0x49e88f1400000000, 0x11686dc300000000, 0xdffef28000000000, + 0x877e105700000000, 0x2ef846f400000000, 0x7678a42300000000, + 0x3df39a6900000000, 0x657378be00000000, 0xccf52e1d00000000, + 0x9475ccca00000000, 0x44ae95bc00000000, 0x1c2e776b00000000, + 0xb5a821c800000000, 0xed28c31f00000000, 0xa6a3fd5500000000, + 0xfe231f8200000000, 0x57a5492100000000, 0x0f25abf600000000, + 0xc1b334b500000000, 0x9933d66200000000, 0x30b580c100000000, + 0x6835621600000000, 0x23be5c5c00000000, 0x7b3ebe8b00000000, + 0xd2b8e82800000000, 0x8a380aff00000000, 0x4e95d7af00000000, + 0x1615357800000000, 0xbf9363db00000000, 0xe713810c00000000, + 0xac98bf4600000000, 0xf4185d9100000000, 0x5d9e0b3200000000, + 0x051ee9e500000000, 0xcb8876a600000000, 0x9308947100000000, + 0x3a8ec2d200000000, 0x620e200500000000, 0x29851e4f00000000, + 0x7105fc9800000000, 0xd883aa3b00000000, 0x800348ec00000000, + 0x783419d700000000, 0x20b4fb0000000000, 0x8932ada300000000, + 0xd1b24f7400000000, 0x9a39713e00000000, 0xc2b993e900000000, + 0x6b3fc54a00000000, 0x33bf279d00000000, 0xfd29b8de00000000, + 0xa5a95a0900000000, 0x0c2f0caa00000000, 0x54afee7d00000000, + 0x1f24d03700000000, 0x47a432e000000000, 0xee22644300000000, + 0xb6a2869400000000, 0x720f5bc400000000, 0x2a8fb91300000000, + 0x8309efb000000000, 0xdb890d6700000000, 0x9002332d00000000, + 0xc882d1fa00000000, 0x6104875900000000, 0x3984658e00000000, + 0xf712facd00000000, 0xaf92181a00000000, 0x06144eb900000000, + 0x5e94ac6e00000000, 0x151f922400000000, 0x4d9f70f300000000, + 0xe419265000000000, 0xbc99c48700000000, 0x6c429df100000000, + 0x34c27f2600000000, 0x9d44298500000000, 0xc5c4cb5200000000, + 0x8e4ff51800000000, 0xd6cf17cf00000000, 0x7f49416c00000000, + 0x27c9a3bb00000000, 0xe95f3cf800000000, 0xb1dfde2f00000000, + 0x1859888c00000000, 0x40d96a5b00000000, 0x0b52541100000000, + 0x53d2b6c600000000, 0xfa54e06500000000, 0xa2d402b200000000, + 0x6679dfe200000000, 0x3ef93d3500000000, 0x977f6b9600000000, + 0xcfff894100000000, 0x8474b70b00000000, 0xdcf455dc00000000, + 0x7572037f00000000, 0x2df2e1a800000000, 0xe3647eeb00000000, + 0xbbe49c3c00000000, 0x1262ca9f00000000, 0x4ae2284800000000, + 0x0169160200000000, 0x59e9f4d500000000, 0xf06fa27600000000, + 0xa8ef40a100000000}, + {0x0000000000000000, 0x463b676500000000, 0x8c76ceca00000000, + 0xca4da9af00000000, 0x59ebed4e00000000, 0x1fd08a2b00000000, + 0xd59d238400000000, 0x93a644e100000000, 0xb2d6db9d00000000, + 0xf4edbcf800000000, 0x3ea0155700000000, 0x789b723200000000, + 0xeb3d36d300000000, 0xad0651b600000000, 0x674bf81900000000, + 0x21709f7c00000000, 0x25abc6e000000000, 0x6390a18500000000, + 0xa9dd082a00000000, 0xefe66f4f00000000, 0x7c402bae00000000, + 0x3a7b4ccb00000000, 0xf036e56400000000, 0xb60d820100000000, + 0x977d1d7d00000000, 0xd1467a1800000000, 0x1b0bd3b700000000, + 0x5d30b4d200000000, 0xce96f03300000000, 0x88ad975600000000, + 0x42e03ef900000000, 0x04db599c00000000, 0x0b50fc1a00000000, + 0x4d6b9b7f00000000, 0x872632d000000000, 0xc11d55b500000000, + 0x52bb115400000000, 0x1480763100000000, 0xdecddf9e00000000, + 0x98f6b8fb00000000, 0xb986278700000000, 0xffbd40e200000000, + 0x35f0e94d00000000, 0x73cb8e2800000000, 0xe06dcac900000000, + 0xa656adac00000000, 0x6c1b040300000000, 0x2a20636600000000, + 0x2efb3afa00000000, 0x68c05d9f00000000, 0xa28df43000000000, + 0xe4b6935500000000, 0x7710d7b400000000, 0x312bb0d100000000, + 0xfb66197e00000000, 0xbd5d7e1b00000000, 0x9c2de16700000000, + 0xda16860200000000, 0x105b2fad00000000, 0x566048c800000000, + 0xc5c60c2900000000, 0x83fd6b4c00000000, 0x49b0c2e300000000, + 0x0f8ba58600000000, 0x16a0f83500000000, 0x509b9f5000000000, + 0x9ad636ff00000000, 0xdced519a00000000, 0x4f4b157b00000000, + 0x0970721e00000000, 0xc33ddbb100000000, 0x8506bcd400000000, + 0xa47623a800000000, 0xe24d44cd00000000, 0x2800ed6200000000, + 0x6e3b8a0700000000, 0xfd9dcee600000000, 0xbba6a98300000000, + 0x71eb002c00000000, 0x37d0674900000000, 0x330b3ed500000000, + 0x753059b000000000, 0xbf7df01f00000000, 0xf946977a00000000, + 0x6ae0d39b00000000, 0x2cdbb4fe00000000, 0xe6961d5100000000, + 0xa0ad7a3400000000, 0x81dde54800000000, 0xc7e6822d00000000, + 0x0dab2b8200000000, 0x4b904ce700000000, 0xd836080600000000, + 0x9e0d6f6300000000, 0x5440c6cc00000000, 0x127ba1a900000000, + 0x1df0042f00000000, 0x5bcb634a00000000, 0x9186cae500000000, + 0xd7bdad8000000000, 0x441be96100000000, 0x02208e0400000000, + 0xc86d27ab00000000, 0x8e5640ce00000000, 0xaf26dfb200000000, + 0xe91db8d700000000, 0x2350117800000000, 0x656b761d00000000, + 0xf6cd32fc00000000, 0xb0f6559900000000, 0x7abbfc3600000000, + 0x3c809b5300000000, 0x385bc2cf00000000, 0x7e60a5aa00000000, + 0xb42d0c0500000000, 0xf2166b6000000000, 0x61b02f8100000000, + 0x278b48e400000000, 0xedc6e14b00000000, 0xabfd862e00000000, + 0x8a8d195200000000, 0xccb67e3700000000, 0x06fbd79800000000, + 0x40c0b0fd00000000, 0xd366f41c00000000, 0x955d937900000000, + 0x5f103ad600000000, 0x192b5db300000000, 0x2c40f16b00000000, + 0x6a7b960e00000000, 0xa0363fa100000000, 0xe60d58c400000000, + 0x75ab1c2500000000, 0x33907b4000000000, 0xf9ddd2ef00000000, + 0xbfe6b58a00000000, 0x9e962af600000000, 0xd8ad4d9300000000, + 0x12e0e43c00000000, 0x54db835900000000, 0xc77dc7b800000000, + 0x8146a0dd00000000, 0x4b0b097200000000, 0x0d306e1700000000, + 0x09eb378b00000000, 0x4fd050ee00000000, 0x859df94100000000, + 0xc3a69e2400000000, 0x5000dac500000000, 0x163bbda000000000, + 0xdc76140f00000000, 0x9a4d736a00000000, 0xbb3dec1600000000, + 0xfd068b7300000000, 0x374b22dc00000000, 0x717045b900000000, + 0xe2d6015800000000, 0xa4ed663d00000000, 0x6ea0cf9200000000, + 0x289ba8f700000000, 0x27100d7100000000, 0x612b6a1400000000, + 0xab66c3bb00000000, 0xed5da4de00000000, 0x7efbe03f00000000, + 0x38c0875a00000000, 0xf28d2ef500000000, 0xb4b6499000000000, + 0x95c6d6ec00000000, 0xd3fdb18900000000, 0x19b0182600000000, + 0x5f8b7f4300000000, 0xcc2d3ba200000000, 0x8a165cc700000000, + 0x405bf56800000000, 0x0660920d00000000, 0x02bbcb9100000000, + 0x4480acf400000000, 0x8ecd055b00000000, 0xc8f6623e00000000, + 0x5b5026df00000000, 0x1d6b41ba00000000, 0xd726e81500000000, + 0x911d8f7000000000, 0xb06d100c00000000, 0xf656776900000000, + 0x3c1bdec600000000, 0x7a20b9a300000000, 0xe986fd4200000000, + 0xafbd9a2700000000, 0x65f0338800000000, 0x23cb54ed00000000, + 0x3ae0095e00000000, 0x7cdb6e3b00000000, 0xb696c79400000000, + 0xf0ada0f100000000, 0x630be41000000000, 0x2530837500000000, + 0xef7d2ada00000000, 0xa9464dbf00000000, 0x8836d2c300000000, + 0xce0db5a600000000, 0x04401c0900000000, 0x427b7b6c00000000, + 0xd1dd3f8d00000000, 0x97e658e800000000, 0x5dabf14700000000, + 0x1b90962200000000, 0x1f4bcfbe00000000, 0x5970a8db00000000, + 0x933d017400000000, 0xd506661100000000, 0x46a022f000000000, + 0x009b459500000000, 0xcad6ec3a00000000, 0x8ced8b5f00000000, + 0xad9d142300000000, 0xeba6734600000000, 0x21ebdae900000000, + 0x67d0bd8c00000000, 0xf476f96d00000000, 0xb24d9e0800000000, + 0x780037a700000000, 0x3e3b50c200000000, 0x31b0f54400000000, + 0x778b922100000000, 0xbdc63b8e00000000, 0xfbfd5ceb00000000, + 0x685b180a00000000, 0x2e607f6f00000000, 0xe42dd6c000000000, + 0xa216b1a500000000, 0x83662ed900000000, 0xc55d49bc00000000, + 0x0f10e01300000000, 0x492b877600000000, 0xda8dc39700000000, + 0x9cb6a4f200000000, 0x56fb0d5d00000000, 0x10c06a3800000000, + 0x141b33a400000000, 0x522054c100000000, 0x986dfd6e00000000, + 0xde569a0b00000000, 0x4df0deea00000000, 0x0bcbb98f00000000, + 0xc186102000000000, 0x87bd774500000000, 0xa6cde83900000000, + 0xe0f68f5c00000000, 0x2abb26f300000000, 0x6c80419600000000, + 0xff26057700000000, 0xb91d621200000000, 0x7350cbbd00000000, + 0x356bacd800000000}, + {0x0000000000000000, 0x9e83da9f00000000, 0x7d01c4e400000000, + 0xe3821e7b00000000, 0xbb04f91200000000, 0x2587238d00000000, + 0xc6053df600000000, 0x5886e76900000000, 0x7609f22500000000, + 0xe88a28ba00000000, 0x0b0836c100000000, 0x958bec5e00000000, + 0xcd0d0b3700000000, 0x538ed1a800000000, 0xb00ccfd300000000, + 0x2e8f154c00000000, 0xec12e44b00000000, 0x72913ed400000000, + 0x911320af00000000, 0x0f90fa3000000000, 0x57161d5900000000, + 0xc995c7c600000000, 0x2a17d9bd00000000, 0xb494032200000000, + 0x9a1b166e00000000, 0x0498ccf100000000, 0xe71ad28a00000000, + 0x7999081500000000, 0x211fef7c00000000, 0xbf9c35e300000000, + 0x5c1e2b9800000000, 0xc29df10700000000, 0xd825c89700000000, + 0x46a6120800000000, 0xa5240c7300000000, 0x3ba7d6ec00000000, + 0x6321318500000000, 0xfda2eb1a00000000, 0x1e20f56100000000, + 0x80a32ffe00000000, 0xae2c3ab200000000, 0x30afe02d00000000, + 0xd32dfe5600000000, 0x4dae24c900000000, 0x1528c3a000000000, + 0x8bab193f00000000, 0x6829074400000000, 0xf6aadddb00000000, + 0x34372cdc00000000, 0xaab4f64300000000, 0x4936e83800000000, + 0xd7b532a700000000, 0x8f33d5ce00000000, 0x11b00f5100000000, + 0xf232112a00000000, 0x6cb1cbb500000000, 0x423edef900000000, + 0xdcbd046600000000, 0x3f3f1a1d00000000, 0xa1bcc08200000000, + 0xf93a27eb00000000, 0x67b9fd7400000000, 0x843be30f00000000, + 0x1ab8399000000000, 0xf14de1f400000000, 0x6fce3b6b00000000, + 0x8c4c251000000000, 0x12cfff8f00000000, 0x4a4918e600000000, + 0xd4cac27900000000, 0x3748dc0200000000, 0xa9cb069d00000000, + 0x874413d100000000, 0x19c7c94e00000000, 0xfa45d73500000000, + 0x64c60daa00000000, 0x3c40eac300000000, 0xa2c3305c00000000, + 0x41412e2700000000, 0xdfc2f4b800000000, 0x1d5f05bf00000000, + 0x83dcdf2000000000, 0x605ec15b00000000, 0xfedd1bc400000000, + 0xa65bfcad00000000, 0x38d8263200000000, 0xdb5a384900000000, + 0x45d9e2d600000000, 0x6b56f79a00000000, 0xf5d52d0500000000, + 0x1657337e00000000, 0x88d4e9e100000000, 0xd0520e8800000000, + 0x4ed1d41700000000, 0xad53ca6c00000000, 0x33d010f300000000, + 0x2968296300000000, 0xb7ebf3fc00000000, 0x5469ed8700000000, + 0xcaea371800000000, 0x926cd07100000000, 0x0cef0aee00000000, + 0xef6d149500000000, 0x71eece0a00000000, 0x5f61db4600000000, + 0xc1e201d900000000, 0x22601fa200000000, 0xbce3c53d00000000, + 0xe465225400000000, 0x7ae6f8cb00000000, 0x9964e6b000000000, + 0x07e73c2f00000000, 0xc57acd2800000000, 0x5bf917b700000000, + 0xb87b09cc00000000, 0x26f8d35300000000, 0x7e7e343a00000000, + 0xe0fdeea500000000, 0x037ff0de00000000, 0x9dfc2a4100000000, + 0xb3733f0d00000000, 0x2df0e59200000000, 0xce72fbe900000000, + 0x50f1217600000000, 0x0877c61f00000000, 0x96f41c8000000000, + 0x757602fb00000000, 0xebf5d86400000000, 0xa39db33200000000, + 0x3d1e69ad00000000, 0xde9c77d600000000, 0x401fad4900000000, + 0x18994a2000000000, 0x861a90bf00000000, 0x65988ec400000000, + 0xfb1b545b00000000, 0xd594411700000000, 0x4b179b8800000000, + 0xa89585f300000000, 0x36165f6c00000000, 0x6e90b80500000000, + 0xf013629a00000000, 0x13917ce100000000, 0x8d12a67e00000000, + 0x4f8f577900000000, 0xd10c8de600000000, 0x328e939d00000000, + 0xac0d490200000000, 0xf48bae6b00000000, 0x6a0874f400000000, + 0x898a6a8f00000000, 0x1709b01000000000, 0x3986a55c00000000, + 0xa7057fc300000000, 0x448761b800000000, 0xda04bb2700000000, + 0x82825c4e00000000, 0x1c0186d100000000, 0xff8398aa00000000, + 0x6100423500000000, 0x7bb87ba500000000, 0xe53ba13a00000000, + 0x06b9bf4100000000, 0x983a65de00000000, 0xc0bc82b700000000, + 0x5e3f582800000000, 0xbdbd465300000000, 0x233e9ccc00000000, + 0x0db1898000000000, 0x9332531f00000000, 0x70b04d6400000000, + 0xee3397fb00000000, 0xb6b5709200000000, 0x2836aa0d00000000, + 0xcbb4b47600000000, 0x55376ee900000000, 0x97aa9fee00000000, + 0x0929457100000000, 0xeaab5b0a00000000, 0x7428819500000000, + 0x2cae66fc00000000, 0xb22dbc6300000000, 0x51afa21800000000, + 0xcf2c788700000000, 0xe1a36dcb00000000, 0x7f20b75400000000, + 0x9ca2a92f00000000, 0x022173b000000000, 0x5aa794d900000000, + 0xc4244e4600000000, 0x27a6503d00000000, 0xb9258aa200000000, + 0x52d052c600000000, 0xcc53885900000000, 0x2fd1962200000000, + 0xb1524cbd00000000, 0xe9d4abd400000000, 0x7757714b00000000, + 0x94d56f3000000000, 0x0a56b5af00000000, 0x24d9a0e300000000, + 0xba5a7a7c00000000, 0x59d8640700000000, 0xc75bbe9800000000, + 0x9fdd59f100000000, 0x015e836e00000000, 0xe2dc9d1500000000, + 0x7c5f478a00000000, 0xbec2b68d00000000, 0x20416c1200000000, + 0xc3c3726900000000, 0x5d40a8f600000000, 0x05c64f9f00000000, + 0x9b45950000000000, 0x78c78b7b00000000, 0xe64451e400000000, + 0xc8cb44a800000000, 0x56489e3700000000, 0xb5ca804c00000000, + 0x2b495ad300000000, 0x73cfbdba00000000, 0xed4c672500000000, + 0x0ece795e00000000, 0x904da3c100000000, 0x8af59a5100000000, + 0x147640ce00000000, 0xf7f45eb500000000, 0x6977842a00000000, + 0x31f1634300000000, 0xaf72b9dc00000000, 0x4cf0a7a700000000, + 0xd2737d3800000000, 0xfcfc687400000000, 0x627fb2eb00000000, + 0x81fdac9000000000, 0x1f7e760f00000000, 0x47f8916600000000, + 0xd97b4bf900000000, 0x3af9558200000000, 0xa47a8f1d00000000, + 0x66e77e1a00000000, 0xf864a48500000000, 0x1be6bafe00000000, + 0x8565606100000000, 0xdde3870800000000, 0x43605d9700000000, + 0xa0e243ec00000000, 0x3e61997300000000, 0x10ee8c3f00000000, + 0x8e6d56a000000000, 0x6def48db00000000, 0xf36c924400000000, + 0xabea752d00000000, 0x3569afb200000000, 0xd6ebb1c900000000, + 0x48686b5600000000}, + {0x0000000000000000, 0xc064281700000000, 0x80c9502e00000000, + 0x40ad783900000000, 0x0093a15c00000000, 0xc0f7894b00000000, + 0x805af17200000000, 0x403ed96500000000, 0x002643b900000000, + 0xc0426bae00000000, 0x80ef139700000000, 0x408b3b8000000000, + 0x00b5e2e500000000, 0xc0d1caf200000000, 0x807cb2cb00000000, + 0x40189adc00000000, 0x414af7a900000000, 0x812edfbe00000000, + 0xc183a78700000000, 0x01e78f9000000000, 0x41d956f500000000, + 0x81bd7ee200000000, 0xc11006db00000000, 0x01742ecc00000000, + 0x416cb41000000000, 0x81089c0700000000, 0xc1a5e43e00000000, + 0x01c1cc2900000000, 0x41ff154c00000000, 0x819b3d5b00000000, + 0xc136456200000000, 0x01526d7500000000, 0xc3929f8800000000, + 0x03f6b79f00000000, 0x435bcfa600000000, 0x833fe7b100000000, + 0xc3013ed400000000, 0x036516c300000000, 0x43c86efa00000000, + 0x83ac46ed00000000, 0xc3b4dc3100000000, 0x03d0f42600000000, + 0x437d8c1f00000000, 0x8319a40800000000, 0xc3277d6d00000000, + 0x0343557a00000000, 0x43ee2d4300000000, 0x838a055400000000, + 0x82d8682100000000, 0x42bc403600000000, 0x0211380f00000000, + 0xc275101800000000, 0x824bc97d00000000, 0x422fe16a00000000, + 0x0282995300000000, 0xc2e6b14400000000, 0x82fe2b9800000000, + 0x429a038f00000000, 0x02377bb600000000, 0xc25353a100000000, + 0x826d8ac400000000, 0x4209a2d300000000, 0x02a4daea00000000, + 0xc2c0f2fd00000000, 0xc7234eca00000000, 0x074766dd00000000, + 0x47ea1ee400000000, 0x878e36f300000000, 0xc7b0ef9600000000, + 0x07d4c78100000000, 0x4779bfb800000000, 0x871d97af00000000, + 0xc7050d7300000000, 0x0761256400000000, 0x47cc5d5d00000000, + 0x87a8754a00000000, 0xc796ac2f00000000, 0x07f2843800000000, + 0x475ffc0100000000, 0x873bd41600000000, 0x8669b96300000000, + 0x460d917400000000, 0x06a0e94d00000000, 0xc6c4c15a00000000, + 0x86fa183f00000000, 0x469e302800000000, 0x0633481100000000, + 0xc657600600000000, 0x864ffada00000000, 0x462bd2cd00000000, + 0x0686aaf400000000, 0xc6e282e300000000, 0x86dc5b8600000000, + 0x46b8739100000000, 0x06150ba800000000, 0xc67123bf00000000, + 0x04b1d14200000000, 0xc4d5f95500000000, 0x8478816c00000000, + 0x441ca97b00000000, 0x0422701e00000000, 0xc446580900000000, + 0x84eb203000000000, 0x448f082700000000, 0x049792fb00000000, + 0xc4f3baec00000000, 0x845ec2d500000000, 0x443aeac200000000, + 0x040433a700000000, 0xc4601bb000000000, 0x84cd638900000000, + 0x44a94b9e00000000, 0x45fb26eb00000000, 0x859f0efc00000000, + 0xc53276c500000000, 0x05565ed200000000, 0x456887b700000000, + 0x850cafa000000000, 0xc5a1d79900000000, 0x05c5ff8e00000000, + 0x45dd655200000000, 0x85b94d4500000000, 0xc514357c00000000, + 0x05701d6b00000000, 0x454ec40e00000000, 0x852aec1900000000, + 0xc587942000000000, 0x05e3bc3700000000, 0xcf41ed4f00000000, + 0x0f25c55800000000, 0x4f88bd6100000000, 0x8fec957600000000, + 0xcfd24c1300000000, 0x0fb6640400000000, 0x4f1b1c3d00000000, + 0x8f7f342a00000000, 0xcf67aef600000000, 0x0f0386e100000000, + 0x4faefed800000000, 0x8fcad6cf00000000, 0xcff40faa00000000, + 0x0f9027bd00000000, 0x4f3d5f8400000000, 0x8f59779300000000, + 0x8e0b1ae600000000, 0x4e6f32f100000000, 0x0ec24ac800000000, + 0xcea662df00000000, 0x8e98bbba00000000, 0x4efc93ad00000000, + 0x0e51eb9400000000, 0xce35c38300000000, 0x8e2d595f00000000, + 0x4e49714800000000, 0x0ee4097100000000, 0xce80216600000000, + 0x8ebef80300000000, 0x4edad01400000000, 0x0e77a82d00000000, + 0xce13803a00000000, 0x0cd372c700000000, 0xccb75ad000000000, + 0x8c1a22e900000000, 0x4c7e0afe00000000, 0x0c40d39b00000000, + 0xcc24fb8c00000000, 0x8c8983b500000000, 0x4cedaba200000000, + 0x0cf5317e00000000, 0xcc91196900000000, 0x8c3c615000000000, + 0x4c58494700000000, 0x0c66902200000000, 0xcc02b83500000000, + 0x8cafc00c00000000, 0x4ccbe81b00000000, 0x4d99856e00000000, + 0x8dfdad7900000000, 0xcd50d54000000000, 0x0d34fd5700000000, + 0x4d0a243200000000, 0x8d6e0c2500000000, 0xcdc3741c00000000, + 0x0da75c0b00000000, 0x4dbfc6d700000000, 0x8ddbeec000000000, + 0xcd7696f900000000, 0x0d12beee00000000, 0x4d2c678b00000000, + 0x8d484f9c00000000, 0xcde537a500000000, 0x0d811fb200000000, + 0x0862a38500000000, 0xc8068b9200000000, 0x88abf3ab00000000, + 0x48cfdbbc00000000, 0x08f102d900000000, 0xc8952ace00000000, + 0x883852f700000000, 0x485c7ae000000000, 0x0844e03c00000000, + 0xc820c82b00000000, 0x888db01200000000, 0x48e9980500000000, + 0x08d7416000000000, 0xc8b3697700000000, 0x881e114e00000000, + 0x487a395900000000, 0x4928542c00000000, 0x894c7c3b00000000, + 0xc9e1040200000000, 0x09852c1500000000, 0x49bbf57000000000, + 0x89dfdd6700000000, 0xc972a55e00000000, 0x09168d4900000000, + 0x490e179500000000, 0x896a3f8200000000, 0xc9c747bb00000000, + 0x09a36fac00000000, 0x499db6c900000000, 0x89f99ede00000000, + 0xc954e6e700000000, 0x0930cef000000000, 0xcbf03c0d00000000, + 0x0b94141a00000000, 0x4b396c2300000000, 0x8b5d443400000000, + 0xcb639d5100000000, 0x0b07b54600000000, 0x4baacd7f00000000, + 0x8bcee56800000000, 0xcbd67fb400000000, 0x0bb257a300000000, + 0x4b1f2f9a00000000, 0x8b7b078d00000000, 0xcb45dee800000000, + 0x0b21f6ff00000000, 0x4b8c8ec600000000, 0x8be8a6d100000000, + 0x8abacba400000000, 0x4adee3b300000000, 0x0a739b8a00000000, + 0xca17b39d00000000, 0x8a296af800000000, 0x4a4d42ef00000000, + 0x0ae03ad600000000, 0xca8412c100000000, 0x8a9c881d00000000, + 0x4af8a00a00000000, 0x0a55d83300000000, 0xca31f02400000000, + 0x8a0f294100000000, 0x4a6b015600000000, 0x0ac6796f00000000, + 0xcaa2517800000000}, + {0x0000000000000000, 0xd4ea739b00000000, 0xe9d396ed00000000, + 0x3d39e57600000000, 0x93a15c0000000000, 0x474b2f9b00000000, + 0x7a72caed00000000, 0xae98b97600000000, 0x2643b90000000000, + 0xf2a9ca9b00000000, 0xcf902fed00000000, 0x1b7a5c7600000000, + 0xb5e2e50000000000, 0x6108969b00000000, 0x5c3173ed00000000, + 0x88db007600000000, 0x4c86720100000000, 0x986c019a00000000, + 0xa555e4ec00000000, 0x71bf977700000000, 0xdf272e0100000000, + 0x0bcd5d9a00000000, 0x36f4b8ec00000000, 0xe21ecb7700000000, + 0x6ac5cb0100000000, 0xbe2fb89a00000000, 0x83165dec00000000, + 0x57fc2e7700000000, 0xf964970100000000, 0x2d8ee49a00000000, + 0x10b701ec00000000, 0xc45d727700000000, 0x980ce50200000000, + 0x4ce6969900000000, 0x71df73ef00000000, 0xa535007400000000, + 0x0badb90200000000, 0xdf47ca9900000000, 0xe27e2fef00000000, + 0x36945c7400000000, 0xbe4f5c0200000000, 0x6aa52f9900000000, + 0x579ccaef00000000, 0x8376b97400000000, 0x2dee000200000000, + 0xf904739900000000, 0xc43d96ef00000000, 0x10d7e57400000000, + 0xd48a970300000000, 0x0060e49800000000, 0x3d5901ee00000000, + 0xe9b3727500000000, 0x472bcb0300000000, 0x93c1b89800000000, + 0xaef85dee00000000, 0x7a122e7500000000, 0xf2c92e0300000000, + 0x26235d9800000000, 0x1b1ab8ee00000000, 0xcff0cb7500000000, + 0x6168720300000000, 0xb582019800000000, 0x88bbe4ee00000000, + 0x5c51977500000000, 0x3019ca0500000000, 0xe4f3b99e00000000, + 0xd9ca5ce800000000, 0x0d202f7300000000, 0xa3b8960500000000, + 0x7752e59e00000000, 0x4a6b00e800000000, 0x9e81737300000000, + 0x165a730500000000, 0xc2b0009e00000000, 0xff89e5e800000000, + 0x2b63967300000000, 0x85fb2f0500000000, 0x51115c9e00000000, + 0x6c28b9e800000000, 0xb8c2ca7300000000, 0x7c9fb80400000000, + 0xa875cb9f00000000, 0x954c2ee900000000, 0x41a65d7200000000, + 0xef3ee40400000000, 0x3bd4979f00000000, 0x06ed72e900000000, + 0xd207017200000000, 0x5adc010400000000, 0x8e36729f00000000, + 0xb30f97e900000000, 0x67e5e47200000000, 0xc97d5d0400000000, + 0x1d972e9f00000000, 0x20aecbe900000000, 0xf444b87200000000, + 0xa8152f0700000000, 0x7cff5c9c00000000, 0x41c6b9ea00000000, + 0x952cca7100000000, 0x3bb4730700000000, 0xef5e009c00000000, + 0xd267e5ea00000000, 0x068d967100000000, 0x8e56960700000000, + 0x5abce59c00000000, 0x678500ea00000000, 0xb36f737100000000, + 0x1df7ca0700000000, 0xc91db99c00000000, 0xf4245cea00000000, + 0x20ce2f7100000000, 0xe4935d0600000000, 0x30792e9d00000000, + 0x0d40cbeb00000000, 0xd9aab87000000000, 0x7732010600000000, + 0xa3d8729d00000000, 0x9ee197eb00000000, 0x4a0be47000000000, + 0xc2d0e40600000000, 0x163a979d00000000, 0x2b0372eb00000000, + 0xffe9017000000000, 0x5171b80600000000, 0x859bcb9d00000000, + 0xb8a22eeb00000000, 0x6c485d7000000000, 0x6032940b00000000, + 0xb4d8e79000000000, 0x89e102e600000000, 0x5d0b717d00000000, + 0xf393c80b00000000, 0x2779bb9000000000, 0x1a405ee600000000, + 0xceaa2d7d00000000, 0x46712d0b00000000, 0x929b5e9000000000, + 0xafa2bbe600000000, 0x7b48c87d00000000, 0xd5d0710b00000000, + 0x013a029000000000, 0x3c03e7e600000000, 0xe8e9947d00000000, + 0x2cb4e60a00000000, 0xf85e959100000000, 0xc56770e700000000, + 0x118d037c00000000, 0xbf15ba0a00000000, 0x6bffc99100000000, + 0x56c62ce700000000, 0x822c5f7c00000000, 0x0af75f0a00000000, + 0xde1d2c9100000000, 0xe324c9e700000000, 0x37ceba7c00000000, + 0x9956030a00000000, 0x4dbc709100000000, 0x708595e700000000, + 0xa46fe67c00000000, 0xf83e710900000000, 0x2cd4029200000000, + 0x11ede7e400000000, 0xc507947f00000000, 0x6b9f2d0900000000, + 0xbf755e9200000000, 0x824cbbe400000000, 0x56a6c87f00000000, + 0xde7dc80900000000, 0x0a97bb9200000000, 0x37ae5ee400000000, + 0xe3442d7f00000000, 0x4ddc940900000000, 0x9936e79200000000, + 0xa40f02e400000000, 0x70e5717f00000000, 0xb4b8030800000000, + 0x6052709300000000, 0x5d6b95e500000000, 0x8981e67e00000000, + 0x27195f0800000000, 0xf3f32c9300000000, 0xcecac9e500000000, + 0x1a20ba7e00000000, 0x92fbba0800000000, 0x4611c99300000000, + 0x7b282ce500000000, 0xafc25f7e00000000, 0x015ae60800000000, + 0xd5b0959300000000, 0xe88970e500000000, 0x3c63037e00000000, + 0x502b5e0e00000000, 0x84c12d9500000000, 0xb9f8c8e300000000, + 0x6d12bb7800000000, 0xc38a020e00000000, 0x1760719500000000, + 0x2a5994e300000000, 0xfeb3e77800000000, 0x7668e70e00000000, + 0xa282949500000000, 0x9fbb71e300000000, 0x4b51027800000000, + 0xe5c9bb0e00000000, 0x3123c89500000000, 0x0c1a2de300000000, + 0xd8f05e7800000000, 0x1cad2c0f00000000, 0xc8475f9400000000, + 0xf57ebae200000000, 0x2194c97900000000, 0x8f0c700f00000000, + 0x5be6039400000000, 0x66dfe6e200000000, 0xb235957900000000, + 0x3aee950f00000000, 0xee04e69400000000, 0xd33d03e200000000, + 0x07d7707900000000, 0xa94fc90f00000000, 0x7da5ba9400000000, + 0x409c5fe200000000, 0x94762c7900000000, 0xc827bb0c00000000, + 0x1ccdc89700000000, 0x21f42de100000000, 0xf51e5e7a00000000, + 0x5b86e70c00000000, 0x8f6c949700000000, 0xb25571e100000000, + 0x66bf027a00000000, 0xee64020c00000000, 0x3a8e719700000000, + 0x07b794e100000000, 0xd35de77a00000000, 0x7dc55e0c00000000, + 0xa92f2d9700000000, 0x9416c8e100000000, 0x40fcbb7a00000000, + 0x84a1c90d00000000, 0x504bba9600000000, 0x6d725fe000000000, + 0xb9982c7b00000000, 0x1700950d00000000, 0xc3eae69600000000, + 0xfed303e000000000, 0x2a39707b00000000, 0xa2e2700d00000000, + 0x7608039600000000, 0x4b31e6e000000000, 0x9fdb957b00000000, + 0x31432c0d00000000, 0xe5a95f9600000000, 0xd890bae000000000, + 0x0c7ac97b00000000}, + {0x0000000000000000, 0x2765258100000000, 0x0fcc3bd900000000, + 0x28a91e5800000000, 0x5f9e066900000000, 0x78fb23e800000000, + 0x50523db000000000, 0x7737183100000000, 0xbe3c0dd200000000, + 0x9959285300000000, 0xb1f0360b00000000, 0x9695138a00000000, + 0xe1a20bbb00000000, 0xc6c72e3a00000000, 0xee6e306200000000, + 0xc90b15e300000000, 0x3d7f6b7f00000000, 0x1a1a4efe00000000, + 0x32b350a600000000, 0x15d6752700000000, 0x62e16d1600000000, + 0x4584489700000000, 0x6d2d56cf00000000, 0x4a48734e00000000, + 0x834366ad00000000, 0xa426432c00000000, 0x8c8f5d7400000000, + 0xabea78f500000000, 0xdcdd60c400000000, 0xfbb8454500000000, + 0xd3115b1d00000000, 0xf4747e9c00000000, 0x7afed6fe00000000, + 0x5d9bf37f00000000, 0x7532ed2700000000, 0x5257c8a600000000, + 0x2560d09700000000, 0x0205f51600000000, 0x2aaceb4e00000000, + 0x0dc9cecf00000000, 0xc4c2db2c00000000, 0xe3a7fead00000000, + 0xcb0ee0f500000000, 0xec6bc57400000000, 0x9b5cdd4500000000, + 0xbc39f8c400000000, 0x9490e69c00000000, 0xb3f5c31d00000000, + 0x4781bd8100000000, 0x60e4980000000000, 0x484d865800000000, + 0x6f28a3d900000000, 0x181fbbe800000000, 0x3f7a9e6900000000, + 0x17d3803100000000, 0x30b6a5b000000000, 0xf9bdb05300000000, + 0xded895d200000000, 0xf6718b8a00000000, 0xd114ae0b00000000, + 0xa623b63a00000000, 0x814693bb00000000, 0xa9ef8de300000000, + 0x8e8aa86200000000, 0xb5fadc2600000000, 0x929ff9a700000000, + 0xba36e7ff00000000, 0x9d53c27e00000000, 0xea64da4f00000000, + 0xcd01ffce00000000, 0xe5a8e19600000000, 0xc2cdc41700000000, + 0x0bc6d1f400000000, 0x2ca3f47500000000, 0x040aea2d00000000, + 0x236fcfac00000000, 0x5458d79d00000000, 0x733df21c00000000, + 0x5b94ec4400000000, 0x7cf1c9c500000000, 0x8885b75900000000, + 0xafe092d800000000, 0x87498c8000000000, 0xa02ca90100000000, + 0xd71bb13000000000, 0xf07e94b100000000, 0xd8d78ae900000000, + 0xffb2af6800000000, 0x36b9ba8b00000000, 0x11dc9f0a00000000, + 0x3975815200000000, 0x1e10a4d300000000, 0x6927bce200000000, + 0x4e42996300000000, 0x66eb873b00000000, 0x418ea2ba00000000, + 0xcf040ad800000000, 0xe8612f5900000000, 0xc0c8310100000000, + 0xe7ad148000000000, 0x909a0cb100000000, 0xb7ff293000000000, + 0x9f56376800000000, 0xb83312e900000000, 0x7138070a00000000, + 0x565d228b00000000, 0x7ef43cd300000000, 0x5991195200000000, + 0x2ea6016300000000, 0x09c324e200000000, 0x216a3aba00000000, + 0x060f1f3b00000000, 0xf27b61a700000000, 0xd51e442600000000, + 0xfdb75a7e00000000, 0xdad27fff00000000, 0xade567ce00000000, + 0x8a80424f00000000, 0xa2295c1700000000, 0x854c799600000000, + 0x4c476c7500000000, 0x6b2249f400000000, 0x438b57ac00000000, + 0x64ee722d00000000, 0x13d96a1c00000000, 0x34bc4f9d00000000, + 0x1c1551c500000000, 0x3b70744400000000, 0x6af5b94d00000000, + 0x4d909ccc00000000, 0x6539829400000000, 0x425ca71500000000, + 0x356bbf2400000000, 0x120e9aa500000000, 0x3aa784fd00000000, + 0x1dc2a17c00000000, 0xd4c9b49f00000000, 0xf3ac911e00000000, + 0xdb058f4600000000, 0xfc60aac700000000, 0x8b57b2f600000000, + 0xac32977700000000, 0x849b892f00000000, 0xa3feacae00000000, + 0x578ad23200000000, 0x70eff7b300000000, 0x5846e9eb00000000, + 0x7f23cc6a00000000, 0x0814d45b00000000, 0x2f71f1da00000000, + 0x07d8ef8200000000, 0x20bdca0300000000, 0xe9b6dfe000000000, + 0xced3fa6100000000, 0xe67ae43900000000, 0xc11fc1b800000000, + 0xb628d98900000000, 0x914dfc0800000000, 0xb9e4e25000000000, + 0x9e81c7d100000000, 0x100b6fb300000000, 0x376e4a3200000000, + 0x1fc7546a00000000, 0x38a271eb00000000, 0x4f9569da00000000, + 0x68f04c5b00000000, 0x4059520300000000, 0x673c778200000000, + 0xae37626100000000, 0x895247e000000000, 0xa1fb59b800000000, + 0x869e7c3900000000, 0xf1a9640800000000, 0xd6cc418900000000, + 0xfe655fd100000000, 0xd9007a5000000000, 0x2d7404cc00000000, + 0x0a11214d00000000, 0x22b83f1500000000, 0x05dd1a9400000000, + 0x72ea02a500000000, 0x558f272400000000, 0x7d26397c00000000, + 0x5a431cfd00000000, 0x9348091e00000000, 0xb42d2c9f00000000, + 0x9c8432c700000000, 0xbbe1174600000000, 0xccd60f7700000000, + 0xebb32af600000000, 0xc31a34ae00000000, 0xe47f112f00000000, + 0xdf0f656b00000000, 0xf86a40ea00000000, 0xd0c35eb200000000, + 0xf7a67b3300000000, 0x8091630200000000, 0xa7f4468300000000, + 0x8f5d58db00000000, 0xa8387d5a00000000, 0x613368b900000000, + 0x46564d3800000000, 0x6eff536000000000, 0x499a76e100000000, + 0x3ead6ed000000000, 0x19c84b5100000000, 0x3161550900000000, + 0x1604708800000000, 0xe2700e1400000000, 0xc5152b9500000000, + 0xedbc35cd00000000, 0xcad9104c00000000, 0xbdee087d00000000, + 0x9a8b2dfc00000000, 0xb22233a400000000, 0x9547162500000000, + 0x5c4c03c600000000, 0x7b29264700000000, 0x5380381f00000000, + 0x74e51d9e00000000, 0x03d205af00000000, 0x24b7202e00000000, + 0x0c1e3e7600000000, 0x2b7b1bf700000000, 0xa5f1b39500000000, + 0x8294961400000000, 0xaa3d884c00000000, 0x8d58adcd00000000, + 0xfa6fb5fc00000000, 0xdd0a907d00000000, 0xf5a38e2500000000, + 0xd2c6aba400000000, 0x1bcdbe4700000000, 0x3ca89bc600000000, + 0x1401859e00000000, 0x3364a01f00000000, 0x4453b82e00000000, + 0x63369daf00000000, 0x4b9f83f700000000, 0x6cfaa67600000000, + 0x988ed8ea00000000, 0xbfebfd6b00000000, 0x9742e33300000000, + 0xb027c6b200000000, 0xc710de8300000000, 0xe075fb0200000000, + 0xc8dce55a00000000, 0xefb9c0db00000000, 0x26b2d53800000000, + 0x01d7f0b900000000, 0x297eeee100000000, 0x0e1bcb6000000000, + 0x792cd35100000000, 0x5e49f6d000000000, 0x76e0e88800000000, + 0x5185cd0900000000}}; + +#else /* W == 4 */ + +local const z_crc_t FAR crc_braid_table[][256] = { + {0x00000000, 0x9ba54c6f, 0xec3b9e9f, 0x779ed2f0, 0x03063b7f, + 0x98a37710, 0xef3da5e0, 0x7498e98f, 0x060c76fe, 0x9da93a91, + 0xea37e861, 0x7192a40e, 0x050a4d81, 0x9eaf01ee, 0xe931d31e, + 0x72949f71, 0x0c18edfc, 0x97bda193, 0xe0237363, 0x7b863f0c, + 0x0f1ed683, 0x94bb9aec, 0xe325481c, 0x78800473, 0x0a149b02, + 0x91b1d76d, 0xe62f059d, 0x7d8a49f2, 0x0912a07d, 0x92b7ec12, + 0xe5293ee2, 0x7e8c728d, 0x1831dbf8, 0x83949797, 0xf40a4567, + 0x6faf0908, 0x1b37e087, 0x8092ace8, 0xf70c7e18, 0x6ca93277, + 0x1e3dad06, 0x8598e169, 0xf2063399, 0x69a37ff6, 0x1d3b9679, + 0x869eda16, 0xf10008e6, 0x6aa54489, 0x14293604, 0x8f8c7a6b, + 0xf812a89b, 0x63b7e4f4, 0x172f0d7b, 0x8c8a4114, 0xfb1493e4, + 0x60b1df8b, 0x122540fa, 0x89800c95, 0xfe1ede65, 0x65bb920a, + 0x11237b85, 0x8a8637ea, 0xfd18e51a, 0x66bda975, 0x3063b7f0, + 0xabc6fb9f, 0xdc58296f, 0x47fd6500, 0x33658c8f, 0xa8c0c0e0, + 0xdf5e1210, 0x44fb5e7f, 0x366fc10e, 0xadca8d61, 0xda545f91, + 0x41f113fe, 0x3569fa71, 0xaeccb61e, 0xd95264ee, 0x42f72881, + 0x3c7b5a0c, 0xa7de1663, 0xd040c493, 0x4be588fc, 0x3f7d6173, + 0xa4d82d1c, 0xd346ffec, 0x48e3b383, 0x3a772cf2, 0xa1d2609d, + 0xd64cb26d, 0x4de9fe02, 0x3971178d, 0xa2d45be2, 0xd54a8912, + 0x4eefc57d, 0x28526c08, 0xb3f72067, 0xc469f297, 0x5fccbef8, + 0x2b545777, 0xb0f11b18, 0xc76fc9e8, 0x5cca8587, 0x2e5e1af6, + 0xb5fb5699, 0xc2658469, 0x59c0c806, 0x2d582189, 0xb6fd6de6, + 0xc163bf16, 0x5ac6f379, 0x244a81f4, 0xbfefcd9b, 0xc8711f6b, + 0x53d45304, 0x274cba8b, 0xbce9f6e4, 0xcb772414, 0x50d2687b, + 0x2246f70a, 0xb9e3bb65, 0xce7d6995, 0x55d825fa, 0x2140cc75, + 0xbae5801a, 0xcd7b52ea, 0x56de1e85, 0x60c76fe0, 0xfb62238f, + 0x8cfcf17f, 0x1759bd10, 0x63c1549f, 0xf86418f0, 0x8ffaca00, + 0x145f866f, 0x66cb191e, 0xfd6e5571, 0x8af08781, 0x1155cbee, + 0x65cd2261, 0xfe686e0e, 0x89f6bcfe, 0x1253f091, 0x6cdf821c, + 0xf77ace73, 0x80e41c83, 0x1b4150ec, 0x6fd9b963, 0xf47cf50c, + 0x83e227fc, 0x18476b93, 0x6ad3f4e2, 0xf176b88d, 0x86e86a7d, + 0x1d4d2612, 0x69d5cf9d, 0xf27083f2, 0x85ee5102, 0x1e4b1d6d, + 0x78f6b418, 0xe353f877, 0x94cd2a87, 0x0f6866e8, 0x7bf08f67, + 0xe055c308, 0x97cb11f8, 0x0c6e5d97, 0x7efac2e6, 0xe55f8e89, + 0x92c15c79, 0x09641016, 0x7dfcf999, 0xe659b5f6, 0x91c76706, + 0x0a622b69, 0x74ee59e4, 0xef4b158b, 0x98d5c77b, 0x03708b14, + 0x77e8629b, 0xec4d2ef4, 0x9bd3fc04, 0x0076b06b, 0x72e22f1a, + 0xe9476375, 0x9ed9b185, 0x057cfdea, 0x71e41465, 0xea41580a, + 0x9ddf8afa, 0x067ac695, 0x50a4d810, 0xcb01947f, 0xbc9f468f, + 0x273a0ae0, 0x53a2e36f, 0xc807af00, 0xbf997df0, 0x243c319f, + 0x56a8aeee, 0xcd0de281, 0xba933071, 0x21367c1e, 0x55ae9591, + 0xce0bd9fe, 0xb9950b0e, 0x22304761, 0x5cbc35ec, 0xc7197983, + 0xb087ab73, 0x2b22e71c, 0x5fba0e93, 0xc41f42fc, 0xb381900c, + 0x2824dc63, 0x5ab04312, 0xc1150f7d, 0xb68bdd8d, 0x2d2e91e2, + 0x59b6786d, 0xc2133402, 0xb58de6f2, 0x2e28aa9d, 0x489503e8, + 0xd3304f87, 0xa4ae9d77, 0x3f0bd118, 0x4b933897, 0xd03674f8, + 0xa7a8a608, 0x3c0dea67, 0x4e997516, 0xd53c3979, 0xa2a2eb89, + 0x3907a7e6, 0x4d9f4e69, 0xd63a0206, 0xa1a4d0f6, 0x3a019c99, + 0x448dee14, 0xdf28a27b, 0xa8b6708b, 0x33133ce4, 0x478bd56b, + 0xdc2e9904, 0xabb04bf4, 0x3015079b, 0x428198ea, 0xd924d485, + 0xaeba0675, 0x351f4a1a, 0x4187a395, 0xda22effa, 0xadbc3d0a, + 0x36197165}, + {0x00000000, 0xc18edfc0, 0x586cb9c1, 0x99e26601, 0xb0d97382, + 0x7157ac42, 0xe8b5ca43, 0x293b1583, 0xbac3e145, 0x7b4d3e85, + 0xe2af5884, 0x23218744, 0x0a1a92c7, 0xcb944d07, 0x52762b06, + 0x93f8f4c6, 0xaef6c4cb, 0x6f781b0b, 0xf69a7d0a, 0x3714a2ca, + 0x1e2fb749, 0xdfa16889, 0x46430e88, 0x87cdd148, 0x1435258e, + 0xd5bbfa4e, 0x4c599c4f, 0x8dd7438f, 0xa4ec560c, 0x656289cc, + 0xfc80efcd, 0x3d0e300d, 0x869c8fd7, 0x47125017, 0xdef03616, + 0x1f7ee9d6, 0x3645fc55, 0xf7cb2395, 0x6e294594, 0xafa79a54, + 0x3c5f6e92, 0xfdd1b152, 0x6433d753, 0xa5bd0893, 0x8c861d10, + 0x4d08c2d0, 0xd4eaa4d1, 0x15647b11, 0x286a4b1c, 0xe9e494dc, + 0x7006f2dd, 0xb1882d1d, 0x98b3389e, 0x593de75e, 0xc0df815f, + 0x01515e9f, 0x92a9aa59, 0x53277599, 0xcac51398, 0x0b4bcc58, + 0x2270d9db, 0xe3fe061b, 0x7a1c601a, 0xbb92bfda, 0xd64819ef, + 0x17c6c62f, 0x8e24a02e, 0x4faa7fee, 0x66916a6d, 0xa71fb5ad, + 0x3efdd3ac, 0xff730c6c, 0x6c8bf8aa, 0xad05276a, 0x34e7416b, + 0xf5699eab, 0xdc528b28, 0x1ddc54e8, 0x843e32e9, 0x45b0ed29, + 0x78bedd24, 0xb93002e4, 0x20d264e5, 0xe15cbb25, 0xc867aea6, + 0x09e97166, 0x900b1767, 0x5185c8a7, 0xc27d3c61, 0x03f3e3a1, + 0x9a1185a0, 0x5b9f5a60, 0x72a44fe3, 0xb32a9023, 0x2ac8f622, + 0xeb4629e2, 0x50d49638, 0x915a49f8, 0x08b82ff9, 0xc936f039, + 0xe00de5ba, 0x21833a7a, 0xb8615c7b, 0x79ef83bb, 0xea17777d, + 0x2b99a8bd, 0xb27bcebc, 0x73f5117c, 0x5ace04ff, 0x9b40db3f, + 0x02a2bd3e, 0xc32c62fe, 0xfe2252f3, 0x3fac8d33, 0xa64eeb32, + 0x67c034f2, 0x4efb2171, 0x8f75feb1, 0x169798b0, 0xd7194770, + 0x44e1b3b6, 0x856f6c76, 0x1c8d0a77, 0xdd03d5b7, 0xf438c034, + 0x35b61ff4, 0xac5479f5, 0x6ddaa635, 0x77e1359f, 0xb66fea5f, + 0x2f8d8c5e, 0xee03539e, 0xc738461d, 0x06b699dd, 0x9f54ffdc, + 0x5eda201c, 0xcd22d4da, 0x0cac0b1a, 0x954e6d1b, 0x54c0b2db, + 0x7dfba758, 0xbc757898, 0x25971e99, 0xe419c159, 0xd917f154, + 0x18992e94, 0x817b4895, 0x40f59755, 0x69ce82d6, 0xa8405d16, + 0x31a23b17, 0xf02ce4d7, 0x63d41011, 0xa25acfd1, 0x3bb8a9d0, + 0xfa367610, 0xd30d6393, 0x1283bc53, 0x8b61da52, 0x4aef0592, + 0xf17dba48, 0x30f36588, 0xa9110389, 0x689fdc49, 0x41a4c9ca, + 0x802a160a, 0x19c8700b, 0xd846afcb, 0x4bbe5b0d, 0x8a3084cd, + 0x13d2e2cc, 0xd25c3d0c, 0xfb67288f, 0x3ae9f74f, 0xa30b914e, + 0x62854e8e, 0x5f8b7e83, 0x9e05a143, 0x07e7c742, 0xc6691882, + 0xef520d01, 0x2edcd2c1, 0xb73eb4c0, 0x76b06b00, 0xe5489fc6, + 0x24c64006, 0xbd242607, 0x7caaf9c7, 0x5591ec44, 0x941f3384, + 0x0dfd5585, 0xcc738a45, 0xa1a92c70, 0x6027f3b0, 0xf9c595b1, + 0x384b4a71, 0x11705ff2, 0xd0fe8032, 0x491ce633, 0x889239f3, + 0x1b6acd35, 0xdae412f5, 0x430674f4, 0x8288ab34, 0xabb3beb7, + 0x6a3d6177, 0xf3df0776, 0x3251d8b6, 0x0f5fe8bb, 0xced1377b, + 0x5733517a, 0x96bd8eba, 0xbf869b39, 0x7e0844f9, 0xe7ea22f8, + 0x2664fd38, 0xb59c09fe, 0x7412d63e, 0xedf0b03f, 0x2c7e6fff, + 0x05457a7c, 0xc4cba5bc, 0x5d29c3bd, 0x9ca71c7d, 0x2735a3a7, + 0xe6bb7c67, 0x7f591a66, 0xbed7c5a6, 0x97ecd025, 0x56620fe5, + 0xcf8069e4, 0x0e0eb624, 0x9df642e2, 0x5c789d22, 0xc59afb23, + 0x041424e3, 0x2d2f3160, 0xeca1eea0, 0x754388a1, 0xb4cd5761, + 0x89c3676c, 0x484db8ac, 0xd1afdead, 0x1021016d, 0x391a14ee, + 0xf894cb2e, 0x6176ad2f, 0xa0f872ef, 0x33008629, 0xf28e59e9, + 0x6b6c3fe8, 0xaae2e028, 0x83d9f5ab, 0x42572a6b, 0xdbb54c6a, + 0x1a3b93aa}, + {0x00000000, 0xefc26b3e, 0x04f5d03d, 0xeb37bb03, 0x09eba07a, + 0xe629cb44, 0x0d1e7047, 0xe2dc1b79, 0x13d740f4, 0xfc152bca, + 0x172290c9, 0xf8e0fbf7, 0x1a3ce08e, 0xf5fe8bb0, 0x1ec930b3, + 0xf10b5b8d, 0x27ae81e8, 0xc86cead6, 0x235b51d5, 0xcc993aeb, + 0x2e452192, 0xc1874aac, 0x2ab0f1af, 0xc5729a91, 0x3479c11c, + 0xdbbbaa22, 0x308c1121, 0xdf4e7a1f, 0x3d926166, 0xd2500a58, + 0x3967b15b, 0xd6a5da65, 0x4f5d03d0, 0xa09f68ee, 0x4ba8d3ed, + 0xa46ab8d3, 0x46b6a3aa, 0xa974c894, 0x42437397, 0xad8118a9, + 0x5c8a4324, 0xb348281a, 0x587f9319, 0xb7bdf827, 0x5561e35e, + 0xbaa38860, 0x51943363, 0xbe56585d, 0x68f38238, 0x8731e906, + 0x6c065205, 0x83c4393b, 0x61182242, 0x8eda497c, 0x65edf27f, + 0x8a2f9941, 0x7b24c2cc, 0x94e6a9f2, 0x7fd112f1, 0x901379cf, + 0x72cf62b6, 0x9d0d0988, 0x763ab28b, 0x99f8d9b5, 0x9eba07a0, + 0x71786c9e, 0x9a4fd79d, 0x758dbca3, 0x9751a7da, 0x7893cce4, + 0x93a477e7, 0x7c661cd9, 0x8d6d4754, 0x62af2c6a, 0x89989769, + 0x665afc57, 0x8486e72e, 0x6b448c10, 0x80733713, 0x6fb15c2d, + 0xb9148648, 0x56d6ed76, 0xbde15675, 0x52233d4b, 0xb0ff2632, + 0x5f3d4d0c, 0xb40af60f, 0x5bc89d31, 0xaac3c6bc, 0x4501ad82, + 0xae361681, 0x41f47dbf, 0xa32866c6, 0x4cea0df8, 0xa7ddb6fb, + 0x481fddc5, 0xd1e70470, 0x3e256f4e, 0xd512d44d, 0x3ad0bf73, + 0xd80ca40a, 0x37cecf34, 0xdcf97437, 0x333b1f09, 0xc2304484, + 0x2df22fba, 0xc6c594b9, 0x2907ff87, 0xcbdbe4fe, 0x24198fc0, + 0xcf2e34c3, 0x20ec5ffd, 0xf6498598, 0x198beea6, 0xf2bc55a5, + 0x1d7e3e9b, 0xffa225e2, 0x10604edc, 0xfb57f5df, 0x14959ee1, + 0xe59ec56c, 0x0a5cae52, 0xe16b1551, 0x0ea97e6f, 0xec756516, + 0x03b70e28, 0xe880b52b, 0x0742de15, 0xe6050901, 0x09c7623f, + 0xe2f0d93c, 0x0d32b202, 0xefeea97b, 0x002cc245, 0xeb1b7946, + 0x04d91278, 0xf5d249f5, 0x1a1022cb, 0xf12799c8, 0x1ee5f2f6, + 0xfc39e98f, 0x13fb82b1, 0xf8cc39b2, 0x170e528c, 0xc1ab88e9, + 0x2e69e3d7, 0xc55e58d4, 0x2a9c33ea, 0xc8402893, 0x278243ad, + 0xccb5f8ae, 0x23779390, 0xd27cc81d, 0x3dbea323, 0xd6891820, + 0x394b731e, 0xdb976867, 0x34550359, 0xdf62b85a, 0x30a0d364, + 0xa9580ad1, 0x469a61ef, 0xadaddaec, 0x426fb1d2, 0xa0b3aaab, + 0x4f71c195, 0xa4467a96, 0x4b8411a8, 0xba8f4a25, 0x554d211b, + 0xbe7a9a18, 0x51b8f126, 0xb364ea5f, 0x5ca68161, 0xb7913a62, + 0x5853515c, 0x8ef68b39, 0x6134e007, 0x8a035b04, 0x65c1303a, + 0x871d2b43, 0x68df407d, 0x83e8fb7e, 0x6c2a9040, 0x9d21cbcd, + 0x72e3a0f3, 0x99d41bf0, 0x761670ce, 0x94ca6bb7, 0x7b080089, + 0x903fbb8a, 0x7ffdd0b4, 0x78bf0ea1, 0x977d659f, 0x7c4ade9c, + 0x9388b5a2, 0x7154aedb, 0x9e96c5e5, 0x75a17ee6, 0x9a6315d8, + 0x6b684e55, 0x84aa256b, 0x6f9d9e68, 0x805ff556, 0x6283ee2f, + 0x8d418511, 0x66763e12, 0x89b4552c, 0x5f118f49, 0xb0d3e477, + 0x5be45f74, 0xb426344a, 0x56fa2f33, 0xb938440d, 0x520fff0e, + 0xbdcd9430, 0x4cc6cfbd, 0xa304a483, 0x48331f80, 0xa7f174be, + 0x452d6fc7, 0xaaef04f9, 0x41d8bffa, 0xae1ad4c4, 0x37e20d71, + 0xd820664f, 0x3317dd4c, 0xdcd5b672, 0x3e09ad0b, 0xd1cbc635, + 0x3afc7d36, 0xd53e1608, 0x24354d85, 0xcbf726bb, 0x20c09db8, + 0xcf02f686, 0x2ddeedff, 0xc21c86c1, 0x292b3dc2, 0xc6e956fc, + 0x104c8c99, 0xff8ee7a7, 0x14b95ca4, 0xfb7b379a, 0x19a72ce3, + 0xf66547dd, 0x1d52fcde, 0xf29097e0, 0x039bcc6d, 0xec59a753, + 0x076e1c50, 0xe8ac776e, 0x0a706c17, 0xe5b20729, 0x0e85bc2a, + 0xe147d714}, + {0x00000000, 0x177b1443, 0x2ef62886, 0x398d3cc5, 0x5dec510c, + 0x4a97454f, 0x731a798a, 0x64616dc9, 0xbbd8a218, 0xaca3b65b, + 0x952e8a9e, 0x82559edd, 0xe634f314, 0xf14fe757, 0xc8c2db92, + 0xdfb9cfd1, 0xacc04271, 0xbbbb5632, 0x82366af7, 0x954d7eb4, + 0xf12c137d, 0xe657073e, 0xdfda3bfb, 0xc8a12fb8, 0x1718e069, + 0x0063f42a, 0x39eec8ef, 0x2e95dcac, 0x4af4b165, 0x5d8fa526, + 0x640299e3, 0x73798da0, 0x82f182a3, 0x958a96e0, 0xac07aa25, + 0xbb7cbe66, 0xdf1dd3af, 0xc866c7ec, 0xf1ebfb29, 0xe690ef6a, + 0x392920bb, 0x2e5234f8, 0x17df083d, 0x00a41c7e, 0x64c571b7, + 0x73be65f4, 0x4a335931, 0x5d484d72, 0x2e31c0d2, 0x394ad491, + 0x00c7e854, 0x17bcfc17, 0x73dd91de, 0x64a6859d, 0x5d2bb958, + 0x4a50ad1b, 0x95e962ca, 0x82927689, 0xbb1f4a4c, 0xac645e0f, + 0xc80533c6, 0xdf7e2785, 0xe6f31b40, 0xf1880f03, 0xde920307, + 0xc9e91744, 0xf0642b81, 0xe71f3fc2, 0x837e520b, 0x94054648, + 0xad887a8d, 0xbaf36ece, 0x654aa11f, 0x7231b55c, 0x4bbc8999, + 0x5cc79dda, 0x38a6f013, 0x2fdde450, 0x1650d895, 0x012bccd6, + 0x72524176, 0x65295535, 0x5ca469f0, 0x4bdf7db3, 0x2fbe107a, + 0x38c50439, 0x014838fc, 0x16332cbf, 0xc98ae36e, 0xdef1f72d, + 0xe77ccbe8, 0xf007dfab, 0x9466b262, 0x831da621, 0xba909ae4, + 0xadeb8ea7, 0x5c6381a4, 0x4b1895e7, 0x7295a922, 0x65eebd61, + 0x018fd0a8, 0x16f4c4eb, 0x2f79f82e, 0x3802ec6d, 0xe7bb23bc, + 0xf0c037ff, 0xc94d0b3a, 0xde361f79, 0xba5772b0, 0xad2c66f3, + 0x94a15a36, 0x83da4e75, 0xf0a3c3d5, 0xe7d8d796, 0xde55eb53, + 0xc92eff10, 0xad4f92d9, 0xba34869a, 0x83b9ba5f, 0x94c2ae1c, + 0x4b7b61cd, 0x5c00758e, 0x658d494b, 0x72f65d08, 0x169730c1, + 0x01ec2482, 0x38611847, 0x2f1a0c04, 0x6655004f, 0x712e140c, + 0x48a328c9, 0x5fd83c8a, 0x3bb95143, 0x2cc24500, 0x154f79c5, + 0x02346d86, 0xdd8da257, 0xcaf6b614, 0xf37b8ad1, 0xe4009e92, + 0x8061f35b, 0x971ae718, 0xae97dbdd, 0xb9eccf9e, 0xca95423e, + 0xddee567d, 0xe4636ab8, 0xf3187efb, 0x97791332, 0x80020771, + 0xb98f3bb4, 0xaef42ff7, 0x714de026, 0x6636f465, 0x5fbbc8a0, + 0x48c0dce3, 0x2ca1b12a, 0x3bdaa569, 0x025799ac, 0x152c8def, + 0xe4a482ec, 0xf3df96af, 0xca52aa6a, 0xdd29be29, 0xb948d3e0, + 0xae33c7a3, 0x97befb66, 0x80c5ef25, 0x5f7c20f4, 0x480734b7, + 0x718a0872, 0x66f11c31, 0x029071f8, 0x15eb65bb, 0x2c66597e, + 0x3b1d4d3d, 0x4864c09d, 0x5f1fd4de, 0x6692e81b, 0x71e9fc58, + 0x15889191, 0x02f385d2, 0x3b7eb917, 0x2c05ad54, 0xf3bc6285, + 0xe4c776c6, 0xdd4a4a03, 0xca315e40, 0xae503389, 0xb92b27ca, + 0x80a61b0f, 0x97dd0f4c, 0xb8c70348, 0xafbc170b, 0x96312bce, + 0x814a3f8d, 0xe52b5244, 0xf2504607, 0xcbdd7ac2, 0xdca66e81, + 0x031fa150, 0x1464b513, 0x2de989d6, 0x3a929d95, 0x5ef3f05c, + 0x4988e41f, 0x7005d8da, 0x677ecc99, 0x14074139, 0x037c557a, + 0x3af169bf, 0x2d8a7dfc, 0x49eb1035, 0x5e900476, 0x671d38b3, + 0x70662cf0, 0xafdfe321, 0xb8a4f762, 0x8129cba7, 0x9652dfe4, + 0xf233b22d, 0xe548a66e, 0xdcc59aab, 0xcbbe8ee8, 0x3a3681eb, + 0x2d4d95a8, 0x14c0a96d, 0x03bbbd2e, 0x67dad0e7, 0x70a1c4a4, + 0x492cf861, 0x5e57ec22, 0x81ee23f3, 0x969537b0, 0xaf180b75, + 0xb8631f36, 0xdc0272ff, 0xcb7966bc, 0xf2f45a79, 0xe58f4e3a, + 0x96f6c39a, 0x818dd7d9, 0xb800eb1c, 0xaf7bff5f, 0xcb1a9296, + 0xdc6186d5, 0xe5ecba10, 0xf297ae53, 0x2d2e6182, 0x3a5575c1, + 0x03d84904, 0x14a35d47, 0x70c2308e, 0x67b924cd, 0x5e341808, + 0x494f0c4b}}; + +local const z_word_t FAR crc_braid_big_table[][256] = { + {0x00000000, 0x43147b17, 0x8628f62e, 0xc53c8d39, 0x0c51ec5d, + 0x4f45974a, 0x8a791a73, 0xc96d6164, 0x18a2d8bb, 0x5bb6a3ac, + 0x9e8a2e95, 0xdd9e5582, 0x14f334e6, 0x57e74ff1, 0x92dbc2c8, + 0xd1cfb9df, 0x7142c0ac, 0x3256bbbb, 0xf76a3682, 0xb47e4d95, + 0x7d132cf1, 0x3e0757e6, 0xfb3bdadf, 0xb82fa1c8, 0x69e01817, + 0x2af46300, 0xefc8ee39, 0xacdc952e, 0x65b1f44a, 0x26a58f5d, + 0xe3990264, 0xa08d7973, 0xa382f182, 0xe0968a95, 0x25aa07ac, + 0x66be7cbb, 0xafd31ddf, 0xecc766c8, 0x29fbebf1, 0x6aef90e6, + 0xbb202939, 0xf834522e, 0x3d08df17, 0x7e1ca400, 0xb771c564, + 0xf465be73, 0x3159334a, 0x724d485d, 0xd2c0312e, 0x91d44a39, + 0x54e8c700, 0x17fcbc17, 0xde91dd73, 0x9d85a664, 0x58b92b5d, + 0x1bad504a, 0xca62e995, 0x89769282, 0x4c4a1fbb, 0x0f5e64ac, + 0xc63305c8, 0x85277edf, 0x401bf3e6, 0x030f88f1, 0x070392de, + 0x4417e9c9, 0x812b64f0, 0xc23f1fe7, 0x0b527e83, 0x48460594, + 0x8d7a88ad, 0xce6ef3ba, 0x1fa14a65, 0x5cb53172, 0x9989bc4b, + 0xda9dc75c, 0x13f0a638, 0x50e4dd2f, 0x95d85016, 0xd6cc2b01, + 0x76415272, 0x35552965, 0xf069a45c, 0xb37ddf4b, 0x7a10be2f, + 0x3904c538, 0xfc384801, 0xbf2c3316, 0x6ee38ac9, 0x2df7f1de, + 0xe8cb7ce7, 0xabdf07f0, 0x62b26694, 0x21a61d83, 0xe49a90ba, + 0xa78eebad, 0xa481635c, 0xe795184b, 0x22a99572, 0x61bdee65, + 0xa8d08f01, 0xebc4f416, 0x2ef8792f, 0x6dec0238, 0xbc23bbe7, + 0xff37c0f0, 0x3a0b4dc9, 0x791f36de, 0xb07257ba, 0xf3662cad, + 0x365aa194, 0x754eda83, 0xd5c3a3f0, 0x96d7d8e7, 0x53eb55de, + 0x10ff2ec9, 0xd9924fad, 0x9a8634ba, 0x5fbab983, 0x1caec294, + 0xcd617b4b, 0x8e75005c, 0x4b498d65, 0x085df672, 0xc1309716, + 0x8224ec01, 0x47186138, 0x040c1a2f, 0x4f005566, 0x0c142e71, + 0xc928a348, 0x8a3cd85f, 0x4351b93b, 0x0045c22c, 0xc5794f15, + 0x866d3402, 0x57a28ddd, 0x14b6f6ca, 0xd18a7bf3, 0x929e00e4, + 0x5bf36180, 0x18e71a97, 0xdddb97ae, 0x9ecfecb9, 0x3e4295ca, + 0x7d56eedd, 0xb86a63e4, 0xfb7e18f3, 0x32137997, 0x71070280, + 0xb43b8fb9, 0xf72ff4ae, 0x26e04d71, 0x65f43666, 0xa0c8bb5f, + 0xe3dcc048, 0x2ab1a12c, 0x69a5da3b, 0xac995702, 0xef8d2c15, + 0xec82a4e4, 0xaf96dff3, 0x6aaa52ca, 0x29be29dd, 0xe0d348b9, + 0xa3c733ae, 0x66fbbe97, 0x25efc580, 0xf4207c5f, 0xb7340748, + 0x72088a71, 0x311cf166, 0xf8719002, 0xbb65eb15, 0x7e59662c, + 0x3d4d1d3b, 0x9dc06448, 0xded41f5f, 0x1be89266, 0x58fce971, + 0x91918815, 0xd285f302, 0x17b97e3b, 0x54ad052c, 0x8562bcf3, + 0xc676c7e4, 0x034a4add, 0x405e31ca, 0x893350ae, 0xca272bb9, + 0x0f1ba680, 0x4c0fdd97, 0x4803c7b8, 0x0b17bcaf, 0xce2b3196, + 0x8d3f4a81, 0x44522be5, 0x074650f2, 0xc27addcb, 0x816ea6dc, + 0x50a11f03, 0x13b56414, 0xd689e92d, 0x959d923a, 0x5cf0f35e, + 0x1fe48849, 0xdad80570, 0x99cc7e67, 0x39410714, 0x7a557c03, + 0xbf69f13a, 0xfc7d8a2d, 0x3510eb49, 0x7604905e, 0xb3381d67, + 0xf02c6670, 0x21e3dfaf, 0x62f7a4b8, 0xa7cb2981, 0xe4df5296, + 0x2db233f2, 0x6ea648e5, 0xab9ac5dc, 0xe88ebecb, 0xeb81363a, + 0xa8954d2d, 0x6da9c014, 0x2ebdbb03, 0xe7d0da67, 0xa4c4a170, + 0x61f82c49, 0x22ec575e, 0xf323ee81, 0xb0379596, 0x750b18af, + 0x361f63b8, 0xff7202dc, 0xbc6679cb, 0x795af4f2, 0x3a4e8fe5, + 0x9ac3f696, 0xd9d78d81, 0x1ceb00b8, 0x5fff7baf, 0x96921acb, + 0xd58661dc, 0x10baece5, 0x53ae97f2, 0x82612e2d, 0xc175553a, + 0x0449d803, 0x475da314, 0x8e30c270, 0xcd24b967, 0x0818345e, + 0x4b0c4f49}, + {0x00000000, 0x3e6bc2ef, 0x3dd0f504, 0x03bb37eb, 0x7aa0eb09, + 0x44cb29e6, 0x47701e0d, 0x791bdce2, 0xf440d713, 0xca2b15fc, + 0xc9902217, 0xf7fbe0f8, 0x8ee03c1a, 0xb08bfef5, 0xb330c91e, + 0x8d5b0bf1, 0xe881ae27, 0xd6ea6cc8, 0xd5515b23, 0xeb3a99cc, + 0x9221452e, 0xac4a87c1, 0xaff1b02a, 0x919a72c5, 0x1cc17934, + 0x22aabbdb, 0x21118c30, 0x1f7a4edf, 0x6661923d, 0x580a50d2, + 0x5bb16739, 0x65daa5d6, 0xd0035d4f, 0xee689fa0, 0xedd3a84b, + 0xd3b86aa4, 0xaaa3b646, 0x94c874a9, 0x97734342, 0xa91881ad, + 0x24438a5c, 0x1a2848b3, 0x19937f58, 0x27f8bdb7, 0x5ee36155, + 0x6088a3ba, 0x63339451, 0x5d5856be, 0x3882f368, 0x06e93187, + 0x0552066c, 0x3b39c483, 0x42221861, 0x7c49da8e, 0x7ff2ed65, + 0x41992f8a, 0xccc2247b, 0xf2a9e694, 0xf112d17f, 0xcf791390, + 0xb662cf72, 0x88090d9d, 0x8bb23a76, 0xb5d9f899, 0xa007ba9e, + 0x9e6c7871, 0x9dd74f9a, 0xa3bc8d75, 0xdaa75197, 0xe4cc9378, + 0xe777a493, 0xd91c667c, 0x54476d8d, 0x6a2caf62, 0x69979889, + 0x57fc5a66, 0x2ee78684, 0x108c446b, 0x13377380, 0x2d5cb16f, + 0x488614b9, 0x76edd656, 0x7556e1bd, 0x4b3d2352, 0x3226ffb0, + 0x0c4d3d5f, 0x0ff60ab4, 0x319dc85b, 0xbcc6c3aa, 0x82ad0145, + 0x811636ae, 0xbf7df441, 0xc66628a3, 0xf80dea4c, 0xfbb6dda7, + 0xc5dd1f48, 0x7004e7d1, 0x4e6f253e, 0x4dd412d5, 0x73bfd03a, + 0x0aa40cd8, 0x34cfce37, 0x3774f9dc, 0x091f3b33, 0x844430c2, + 0xba2ff22d, 0xb994c5c6, 0x87ff0729, 0xfee4dbcb, 0xc08f1924, + 0xc3342ecf, 0xfd5fec20, 0x988549f6, 0xa6ee8b19, 0xa555bcf2, + 0x9b3e7e1d, 0xe225a2ff, 0xdc4e6010, 0xdff557fb, 0xe19e9514, + 0x6cc59ee5, 0x52ae5c0a, 0x51156be1, 0x6f7ea90e, 0x166575ec, + 0x280eb703, 0x2bb580e8, 0x15de4207, 0x010905e6, 0x3f62c709, + 0x3cd9f0e2, 0x02b2320d, 0x7ba9eeef, 0x45c22c00, 0x46791beb, + 0x7812d904, 0xf549d2f5, 0xcb22101a, 0xc89927f1, 0xf6f2e51e, + 0x8fe939fc, 0xb182fb13, 0xb239ccf8, 0x8c520e17, 0xe988abc1, + 0xd7e3692e, 0xd4585ec5, 0xea339c2a, 0x932840c8, 0xad438227, + 0xaef8b5cc, 0x90937723, 0x1dc87cd2, 0x23a3be3d, 0x201889d6, + 0x1e734b39, 0x676897db, 0x59035534, 0x5ab862df, 0x64d3a030, + 0xd10a58a9, 0xef619a46, 0xecdaadad, 0xd2b16f42, 0xabaab3a0, + 0x95c1714f, 0x967a46a4, 0xa811844b, 0x254a8fba, 0x1b214d55, + 0x189a7abe, 0x26f1b851, 0x5fea64b3, 0x6181a65c, 0x623a91b7, + 0x5c515358, 0x398bf68e, 0x07e03461, 0x045b038a, 0x3a30c165, + 0x432b1d87, 0x7d40df68, 0x7efbe883, 0x40902a6c, 0xcdcb219d, + 0xf3a0e372, 0xf01bd499, 0xce701676, 0xb76bca94, 0x8900087b, + 0x8abb3f90, 0xb4d0fd7f, 0xa10ebf78, 0x9f657d97, 0x9cde4a7c, + 0xa2b58893, 0xdbae5471, 0xe5c5969e, 0xe67ea175, 0xd815639a, + 0x554e686b, 0x6b25aa84, 0x689e9d6f, 0x56f55f80, 0x2fee8362, + 0x1185418d, 0x123e7666, 0x2c55b489, 0x498f115f, 0x77e4d3b0, + 0x745fe45b, 0x4a3426b4, 0x332ffa56, 0x0d4438b9, 0x0eff0f52, + 0x3094cdbd, 0xbdcfc64c, 0x83a404a3, 0x801f3348, 0xbe74f1a7, + 0xc76f2d45, 0xf904efaa, 0xfabfd841, 0xc4d41aae, 0x710de237, + 0x4f6620d8, 0x4cdd1733, 0x72b6d5dc, 0x0bad093e, 0x35c6cbd1, + 0x367dfc3a, 0x08163ed5, 0x854d3524, 0xbb26f7cb, 0xb89dc020, + 0x86f602cf, 0xffedde2d, 0xc1861cc2, 0xc23d2b29, 0xfc56e9c6, + 0x998c4c10, 0xa7e78eff, 0xa45cb914, 0x9a377bfb, 0xe32ca719, + 0xdd4765f6, 0xdefc521d, 0xe09790f2, 0x6dcc9b03, 0x53a759ec, + 0x501c6e07, 0x6e77ace8, 0x176c700a, 0x2907b2e5, 0x2abc850e, + 0x14d747e1}, + {0x00000000, 0xc0df8ec1, 0xc1b96c58, 0x0166e299, 0x8273d9b0, + 0x42ac5771, 0x43cab5e8, 0x83153b29, 0x45e1c3ba, 0x853e4d7b, + 0x8458afe2, 0x44872123, 0xc7921a0a, 0x074d94cb, 0x062b7652, + 0xc6f4f893, 0xcbc4f6ae, 0x0b1b786f, 0x0a7d9af6, 0xcaa21437, + 0x49b72f1e, 0x8968a1df, 0x880e4346, 0x48d1cd87, 0x8e253514, + 0x4efabbd5, 0x4f9c594c, 0x8f43d78d, 0x0c56eca4, 0xcc896265, + 0xcdef80fc, 0x0d300e3d, 0xd78f9c86, 0x17501247, 0x1636f0de, + 0xd6e97e1f, 0x55fc4536, 0x9523cbf7, 0x9445296e, 0x549aa7af, + 0x926e5f3c, 0x52b1d1fd, 0x53d73364, 0x9308bda5, 0x101d868c, + 0xd0c2084d, 0xd1a4ead4, 0x117b6415, 0x1c4b6a28, 0xdc94e4e9, + 0xddf20670, 0x1d2d88b1, 0x9e38b398, 0x5ee73d59, 0x5f81dfc0, + 0x9f5e5101, 0x59aaa992, 0x99752753, 0x9813c5ca, 0x58cc4b0b, + 0xdbd97022, 0x1b06fee3, 0x1a601c7a, 0xdabf92bb, 0xef1948d6, + 0x2fc6c617, 0x2ea0248e, 0xee7faa4f, 0x6d6a9166, 0xadb51fa7, + 0xacd3fd3e, 0x6c0c73ff, 0xaaf88b6c, 0x6a2705ad, 0x6b41e734, + 0xab9e69f5, 0x288b52dc, 0xe854dc1d, 0xe9323e84, 0x29edb045, + 0x24ddbe78, 0xe40230b9, 0xe564d220, 0x25bb5ce1, 0xa6ae67c8, + 0x6671e909, 0x67170b90, 0xa7c88551, 0x613c7dc2, 0xa1e3f303, + 0xa085119a, 0x605a9f5b, 0xe34fa472, 0x23902ab3, 0x22f6c82a, + 0xe22946eb, 0x3896d450, 0xf8495a91, 0xf92fb808, 0x39f036c9, + 0xbae50de0, 0x7a3a8321, 0x7b5c61b8, 0xbb83ef79, 0x7d7717ea, + 0xbda8992b, 0xbcce7bb2, 0x7c11f573, 0xff04ce5a, 0x3fdb409b, + 0x3ebda202, 0xfe622cc3, 0xf35222fe, 0x338dac3f, 0x32eb4ea6, + 0xf234c067, 0x7121fb4e, 0xb1fe758f, 0xb0989716, 0x704719d7, + 0xb6b3e144, 0x766c6f85, 0x770a8d1c, 0xb7d503dd, 0x34c038f4, + 0xf41fb635, 0xf57954ac, 0x35a6da6d, 0x9f35e177, 0x5fea6fb6, + 0x5e8c8d2f, 0x9e5303ee, 0x1d4638c7, 0xdd99b606, 0xdcff549f, + 0x1c20da5e, 0xdad422cd, 0x1a0bac0c, 0x1b6d4e95, 0xdbb2c054, + 0x58a7fb7d, 0x987875bc, 0x991e9725, 0x59c119e4, 0x54f117d9, + 0x942e9918, 0x95487b81, 0x5597f540, 0xd682ce69, 0x165d40a8, + 0x173ba231, 0xd7e42cf0, 0x1110d463, 0xd1cf5aa2, 0xd0a9b83b, + 0x107636fa, 0x93630dd3, 0x53bc8312, 0x52da618b, 0x9205ef4a, + 0x48ba7df1, 0x8865f330, 0x890311a9, 0x49dc9f68, 0xcac9a441, + 0x0a162a80, 0x0b70c819, 0xcbaf46d8, 0x0d5bbe4b, 0xcd84308a, + 0xcce2d213, 0x0c3d5cd2, 0x8f2867fb, 0x4ff7e93a, 0x4e910ba3, + 0x8e4e8562, 0x837e8b5f, 0x43a1059e, 0x42c7e707, 0x821869c6, + 0x010d52ef, 0xc1d2dc2e, 0xc0b43eb7, 0x006bb076, 0xc69f48e5, + 0x0640c624, 0x072624bd, 0xc7f9aa7c, 0x44ec9155, 0x84331f94, + 0x8555fd0d, 0x458a73cc, 0x702ca9a1, 0xb0f32760, 0xb195c5f9, + 0x714a4b38, 0xf25f7011, 0x3280fed0, 0x33e61c49, 0xf3399288, + 0x35cd6a1b, 0xf512e4da, 0xf4740643, 0x34ab8882, 0xb7beb3ab, + 0x77613d6a, 0x7607dff3, 0xb6d85132, 0xbbe85f0f, 0x7b37d1ce, + 0x7a513357, 0xba8ebd96, 0x399b86bf, 0xf944087e, 0xf822eae7, + 0x38fd6426, 0xfe099cb5, 0x3ed61274, 0x3fb0f0ed, 0xff6f7e2c, + 0x7c7a4505, 0xbca5cbc4, 0xbdc3295d, 0x7d1ca79c, 0xa7a33527, + 0x677cbbe6, 0x661a597f, 0xa6c5d7be, 0x25d0ec97, 0xe50f6256, + 0xe46980cf, 0x24b60e0e, 0xe242f69d, 0x229d785c, 0x23fb9ac5, + 0xe3241404, 0x60312f2d, 0xa0eea1ec, 0xa1884375, 0x6157cdb4, + 0x6c67c389, 0xacb84d48, 0xaddeafd1, 0x6d012110, 0xee141a39, + 0x2ecb94f8, 0x2fad7661, 0xef72f8a0, 0x29860033, 0xe9598ef2, + 0xe83f6c6b, 0x28e0e2aa, 0xabf5d983, 0x6b2a5742, 0x6a4cb5db, + 0xaa933b1a}, + {0x00000000, 0x6f4ca59b, 0x9f9e3bec, 0xf0d29e77, 0x7f3b0603, + 0x1077a398, 0xe0a53def, 0x8fe99874, 0xfe760c06, 0x913aa99d, + 0x61e837ea, 0x0ea49271, 0x814d0a05, 0xee01af9e, 0x1ed331e9, + 0x719f9472, 0xfced180c, 0x93a1bd97, 0x637323e0, 0x0c3f867b, + 0x83d61e0f, 0xec9abb94, 0x1c4825e3, 0x73048078, 0x029b140a, + 0x6dd7b191, 0x9d052fe6, 0xf2498a7d, 0x7da01209, 0x12ecb792, + 0xe23e29e5, 0x8d728c7e, 0xf8db3118, 0x97979483, 0x67450af4, + 0x0809af6f, 0x87e0371b, 0xe8ac9280, 0x187e0cf7, 0x7732a96c, + 0x06ad3d1e, 0x69e19885, 0x993306f2, 0xf67fa369, 0x79963b1d, + 0x16da9e86, 0xe60800f1, 0x8944a56a, 0x04362914, 0x6b7a8c8f, + 0x9ba812f8, 0xf4e4b763, 0x7b0d2f17, 0x14418a8c, 0xe49314fb, + 0x8bdfb160, 0xfa402512, 0x950c8089, 0x65de1efe, 0x0a92bb65, + 0x857b2311, 0xea37868a, 0x1ae518fd, 0x75a9bd66, 0xf0b76330, + 0x9ffbc6ab, 0x6f2958dc, 0x0065fd47, 0x8f8c6533, 0xe0c0c0a8, + 0x10125edf, 0x7f5efb44, 0x0ec16f36, 0x618dcaad, 0x915f54da, + 0xfe13f141, 0x71fa6935, 0x1eb6ccae, 0xee6452d9, 0x8128f742, + 0x0c5a7b3c, 0x6316dea7, 0x93c440d0, 0xfc88e54b, 0x73617d3f, + 0x1c2dd8a4, 0xecff46d3, 0x83b3e348, 0xf22c773a, 0x9d60d2a1, + 0x6db24cd6, 0x02fee94d, 0x8d177139, 0xe25bd4a2, 0x12894ad5, + 0x7dc5ef4e, 0x086c5228, 0x6720f7b3, 0x97f269c4, 0xf8becc5f, + 0x7757542b, 0x181bf1b0, 0xe8c96fc7, 0x8785ca5c, 0xf61a5e2e, + 0x9956fbb5, 0x698465c2, 0x06c8c059, 0x8921582d, 0xe66dfdb6, + 0x16bf63c1, 0x79f3c65a, 0xf4814a24, 0x9bcdefbf, 0x6b1f71c8, + 0x0453d453, 0x8bba4c27, 0xe4f6e9bc, 0x142477cb, 0x7b68d250, + 0x0af74622, 0x65bbe3b9, 0x95697dce, 0xfa25d855, 0x75cc4021, + 0x1a80e5ba, 0xea527bcd, 0x851ede56, 0xe06fc760, 0x8f2362fb, + 0x7ff1fc8c, 0x10bd5917, 0x9f54c163, 0xf01864f8, 0x00cafa8f, + 0x6f865f14, 0x1e19cb66, 0x71556efd, 0x8187f08a, 0xeecb5511, + 0x6122cd65, 0x0e6e68fe, 0xfebcf689, 0x91f05312, 0x1c82df6c, + 0x73ce7af7, 0x831ce480, 0xec50411b, 0x63b9d96f, 0x0cf57cf4, + 0xfc27e283, 0x936b4718, 0xe2f4d36a, 0x8db876f1, 0x7d6ae886, + 0x12264d1d, 0x9dcfd569, 0xf28370f2, 0x0251ee85, 0x6d1d4b1e, + 0x18b4f678, 0x77f853e3, 0x872acd94, 0xe866680f, 0x678ff07b, + 0x08c355e0, 0xf811cb97, 0x975d6e0c, 0xe6c2fa7e, 0x898e5fe5, + 0x795cc192, 0x16106409, 0x99f9fc7d, 0xf6b559e6, 0x0667c791, + 0x692b620a, 0xe459ee74, 0x8b154bef, 0x7bc7d598, 0x148b7003, + 0x9b62e877, 0xf42e4dec, 0x04fcd39b, 0x6bb07600, 0x1a2fe272, + 0x756347e9, 0x85b1d99e, 0xeafd7c05, 0x6514e471, 0x0a5841ea, + 0xfa8adf9d, 0x95c67a06, 0x10d8a450, 0x7f9401cb, 0x8f469fbc, + 0xe00a3a27, 0x6fe3a253, 0x00af07c8, 0xf07d99bf, 0x9f313c24, + 0xeeaea856, 0x81e20dcd, 0x713093ba, 0x1e7c3621, 0x9195ae55, + 0xfed90bce, 0x0e0b95b9, 0x61473022, 0xec35bc5c, 0x837919c7, + 0x73ab87b0, 0x1ce7222b, 0x930eba5f, 0xfc421fc4, 0x0c9081b3, + 0x63dc2428, 0x1243b05a, 0x7d0f15c1, 0x8ddd8bb6, 0xe2912e2d, + 0x6d78b659, 0x023413c2, 0xf2e68db5, 0x9daa282e, 0xe8039548, + 0x874f30d3, 0x779daea4, 0x18d10b3f, 0x9738934b, 0xf87436d0, + 0x08a6a8a7, 0x67ea0d3c, 0x1675994e, 0x79393cd5, 0x89eba2a2, + 0xe6a70739, 0x694e9f4d, 0x06023ad6, 0xf6d0a4a1, 0x999c013a, + 0x14ee8d44, 0x7ba228df, 0x8b70b6a8, 0xe43c1333, 0x6bd58b47, + 0x04992edc, 0xf44bb0ab, 0x9b071530, 0xea988142, 0x85d424d9, + 0x7506baae, 0x1a4a1f35, 0x95a38741, 0xfaef22da, 0x0a3dbcad, + 0x65711936}}; + +#endif + +#endif + +#if N == 4 + +#if W == 8 + +local const z_crc_t FAR crc_braid_table[][256] = { + {0x00000000, 0xf1da05aa, 0x38c50d15, 0xc91f08bf, 0x718a1a2a, + 0x80501f80, 0x494f173f, 0xb8951295, 0xe3143454, 0x12ce31fe, + 0xdbd13941, 0x2a0b3ceb, 0x929e2e7e, 0x63442bd4, 0xaa5b236b, + 0x5b8126c1, 0x1d596ee9, 0xec836b43, 0x259c63fc, 0xd4466656, + 0x6cd374c3, 0x9d097169, 0x541679d6, 0xa5cc7c7c, 0xfe4d5abd, + 0x0f975f17, 0xc68857a8, 0x37525202, 0x8fc74097, 0x7e1d453d, + 0xb7024d82, 0x46d84828, 0x3ab2ddd2, 0xcb68d878, 0x0277d0c7, + 0xf3add56d, 0x4b38c7f8, 0xbae2c252, 0x73fdcaed, 0x8227cf47, + 0xd9a6e986, 0x287cec2c, 0xe163e493, 0x10b9e139, 0xa82cf3ac, + 0x59f6f606, 0x90e9feb9, 0x6133fb13, 0x27ebb33b, 0xd631b691, + 0x1f2ebe2e, 0xeef4bb84, 0x5661a911, 0xa7bbacbb, 0x6ea4a404, + 0x9f7ea1ae, 0xc4ff876f, 0x352582c5, 0xfc3a8a7a, 0x0de08fd0, + 0xb5759d45, 0x44af98ef, 0x8db09050, 0x7c6a95fa, 0x7565bba4, + 0x84bfbe0e, 0x4da0b6b1, 0xbc7ab31b, 0x04efa18e, 0xf535a424, + 0x3c2aac9b, 0xcdf0a931, 0x96718ff0, 0x67ab8a5a, 0xaeb482e5, + 0x5f6e874f, 0xe7fb95da, 0x16219070, 0xdf3e98cf, 0x2ee49d65, + 0x683cd54d, 0x99e6d0e7, 0x50f9d858, 0xa123ddf2, 0x19b6cf67, + 0xe86ccacd, 0x2173c272, 0xd0a9c7d8, 0x8b28e119, 0x7af2e4b3, + 0xb3edec0c, 0x4237e9a6, 0xfaa2fb33, 0x0b78fe99, 0xc267f626, + 0x33bdf38c, 0x4fd76676, 0xbe0d63dc, 0x77126b63, 0x86c86ec9, + 0x3e5d7c5c, 0xcf8779f6, 0x06987149, 0xf74274e3, 0xacc35222, + 0x5d195788, 0x94065f37, 0x65dc5a9d, 0xdd494808, 0x2c934da2, + 0xe58c451d, 0x145640b7, 0x528e089f, 0xa3540d35, 0x6a4b058a, + 0x9b910020, 0x230412b5, 0xd2de171f, 0x1bc11fa0, 0xea1b1a0a, + 0xb19a3ccb, 0x40403961, 0x895f31de, 0x78853474, 0xc01026e1, + 0x31ca234b, 0xf8d52bf4, 0x090f2e5e, 0xeacb7748, 0x1b1172e2, + 0xd20e7a5d, 0x23d47ff7, 0x9b416d62, 0x6a9b68c8, 0xa3846077, + 0x525e65dd, 0x09df431c, 0xf80546b6, 0x311a4e09, 0xc0c04ba3, + 0x78555936, 0x898f5c9c, 0x40905423, 0xb14a5189, 0xf79219a1, + 0x06481c0b, 0xcf5714b4, 0x3e8d111e, 0x8618038b, 0x77c20621, + 0xbedd0e9e, 0x4f070b34, 0x14862df5, 0xe55c285f, 0x2c4320e0, + 0xdd99254a, 0x650c37df, 0x94d63275, 0x5dc93aca, 0xac133f60, + 0xd079aa9a, 0x21a3af30, 0xe8bca78f, 0x1966a225, 0xa1f3b0b0, + 0x5029b51a, 0x9936bda5, 0x68ecb80f, 0x336d9ece, 0xc2b79b64, + 0x0ba893db, 0xfa729671, 0x42e784e4, 0xb33d814e, 0x7a2289f1, + 0x8bf88c5b, 0xcd20c473, 0x3cfac1d9, 0xf5e5c966, 0x043fcccc, + 0xbcaade59, 0x4d70dbf3, 0x846fd34c, 0x75b5d6e6, 0x2e34f027, + 0xdfeef58d, 0x16f1fd32, 0xe72bf898, 0x5fbeea0d, 0xae64efa7, + 0x677be718, 0x96a1e2b2, 0x9faeccec, 0x6e74c946, 0xa76bc1f9, + 0x56b1c453, 0xee24d6c6, 0x1ffed36c, 0xd6e1dbd3, 0x273bde79, + 0x7cbaf8b8, 0x8d60fd12, 0x447ff5ad, 0xb5a5f007, 0x0d30e292, + 0xfceae738, 0x35f5ef87, 0xc42fea2d, 0x82f7a205, 0x732da7af, + 0xba32af10, 0x4be8aaba, 0xf37db82f, 0x02a7bd85, 0xcbb8b53a, + 0x3a62b090, 0x61e39651, 0x903993fb, 0x59269b44, 0xa8fc9eee, + 0x10698c7b, 0xe1b389d1, 0x28ac816e, 0xd97684c4, 0xa51c113e, + 0x54c61494, 0x9dd91c2b, 0x6c031981, 0xd4960b14, 0x254c0ebe, + 0xec530601, 0x1d8903ab, 0x4608256a, 0xb7d220c0, 0x7ecd287f, + 0x8f172dd5, 0x37823f40, 0xc6583aea, 0x0f473255, 0xfe9d37ff, + 0xb8457fd7, 0x499f7a7d, 0x808072c2, 0x715a7768, 0xc9cf65fd, + 0x38156057, 0xf10a68e8, 0x00d06d42, 0x5b514b83, 0xaa8b4e29, + 0x63944696, 0x924e433c, 0x2adb51a9, 0xdb015403, 0x121e5cbc, + 0xe3c45916}, + {0x00000000, 0x0ee7e8d1, 0x1dcfd1a2, 0x13283973, 0x3b9fa344, + 0x35784b95, 0x265072e6, 0x28b79a37, 0x773f4688, 0x79d8ae59, + 0x6af0972a, 0x64177ffb, 0x4ca0e5cc, 0x42470d1d, 0x516f346e, + 0x5f88dcbf, 0xee7e8d10, 0xe09965c1, 0xf3b15cb2, 0xfd56b463, + 0xd5e12e54, 0xdb06c685, 0xc82efff6, 0xc6c91727, 0x9941cb98, + 0x97a62349, 0x848e1a3a, 0x8a69f2eb, 0xa2de68dc, 0xac39800d, + 0xbf11b97e, 0xb1f651af, 0x078c1c61, 0x096bf4b0, 0x1a43cdc3, + 0x14a42512, 0x3c13bf25, 0x32f457f4, 0x21dc6e87, 0x2f3b8656, + 0x70b35ae9, 0x7e54b238, 0x6d7c8b4b, 0x639b639a, 0x4b2cf9ad, + 0x45cb117c, 0x56e3280f, 0x5804c0de, 0xe9f29171, 0xe71579a0, + 0xf43d40d3, 0xfadaa802, 0xd26d3235, 0xdc8adae4, 0xcfa2e397, + 0xc1450b46, 0x9ecdd7f9, 0x902a3f28, 0x8302065b, 0x8de5ee8a, + 0xa55274bd, 0xabb59c6c, 0xb89da51f, 0xb67a4dce, 0x0f1838c2, + 0x01ffd013, 0x12d7e960, 0x1c3001b1, 0x34879b86, 0x3a607357, + 0x29484a24, 0x27afa2f5, 0x78277e4a, 0x76c0969b, 0x65e8afe8, + 0x6b0f4739, 0x43b8dd0e, 0x4d5f35df, 0x5e770cac, 0x5090e47d, + 0xe166b5d2, 0xef815d03, 0xfca96470, 0xf24e8ca1, 0xdaf91696, + 0xd41efe47, 0xc736c734, 0xc9d12fe5, 0x9659f35a, 0x98be1b8b, + 0x8b9622f8, 0x8571ca29, 0xadc6501e, 0xa321b8cf, 0xb00981bc, + 0xbeee696d, 0x089424a3, 0x0673cc72, 0x155bf501, 0x1bbc1dd0, + 0x330b87e7, 0x3dec6f36, 0x2ec45645, 0x2023be94, 0x7fab622b, + 0x714c8afa, 0x6264b389, 0x6c835b58, 0x4434c16f, 0x4ad329be, + 0x59fb10cd, 0x571cf81c, 0xe6eaa9b3, 0xe80d4162, 0xfb257811, + 0xf5c290c0, 0xdd750af7, 0xd392e226, 0xc0badb55, 0xce5d3384, + 0x91d5ef3b, 0x9f3207ea, 0x8c1a3e99, 0x82fdd648, 0xaa4a4c7f, + 0xa4ada4ae, 0xb7859ddd, 0xb962750c, 0x1e307184, 0x10d79955, + 0x03ffa026, 0x0d1848f7, 0x25afd2c0, 0x2b483a11, 0x38600362, + 0x3687ebb3, 0x690f370c, 0x67e8dfdd, 0x74c0e6ae, 0x7a270e7f, + 0x52909448, 0x5c777c99, 0x4f5f45ea, 0x41b8ad3b, 0xf04efc94, + 0xfea91445, 0xed812d36, 0xe366c5e7, 0xcbd15fd0, 0xc536b701, + 0xd61e8e72, 0xd8f966a3, 0x8771ba1c, 0x899652cd, 0x9abe6bbe, + 0x9459836f, 0xbcee1958, 0xb209f189, 0xa121c8fa, 0xafc6202b, + 0x19bc6de5, 0x175b8534, 0x0473bc47, 0x0a945496, 0x2223cea1, + 0x2cc42670, 0x3fec1f03, 0x310bf7d2, 0x6e832b6d, 0x6064c3bc, + 0x734cfacf, 0x7dab121e, 0x551c8829, 0x5bfb60f8, 0x48d3598b, + 0x4634b15a, 0xf7c2e0f5, 0xf9250824, 0xea0d3157, 0xe4ead986, + 0xcc5d43b1, 0xc2baab60, 0xd1929213, 0xdf757ac2, 0x80fda67d, + 0x8e1a4eac, 0x9d3277df, 0x93d59f0e, 0xbb620539, 0xb585ede8, + 0xa6add49b, 0xa84a3c4a, 0x11284946, 0x1fcfa197, 0x0ce798e4, + 0x02007035, 0x2ab7ea02, 0x245002d3, 0x37783ba0, 0x399fd371, + 0x66170fce, 0x68f0e71f, 0x7bd8de6c, 0x753f36bd, 0x5d88ac8a, + 0x536f445b, 0x40477d28, 0x4ea095f9, 0xff56c456, 0xf1b12c87, + 0xe29915f4, 0xec7efd25, 0xc4c96712, 0xca2e8fc3, 0xd906b6b0, + 0xd7e15e61, 0x886982de, 0x868e6a0f, 0x95a6537c, 0x9b41bbad, + 0xb3f6219a, 0xbd11c94b, 0xae39f038, 0xa0de18e9, 0x16a45527, + 0x1843bdf6, 0x0b6b8485, 0x058c6c54, 0x2d3bf663, 0x23dc1eb2, + 0x30f427c1, 0x3e13cf10, 0x619b13af, 0x6f7cfb7e, 0x7c54c20d, + 0x72b32adc, 0x5a04b0eb, 0x54e3583a, 0x47cb6149, 0x492c8998, + 0xf8dad837, 0xf63d30e6, 0xe5150995, 0xebf2e144, 0xc3457b73, + 0xcda293a2, 0xde8aaad1, 0xd06d4200, 0x8fe59ebf, 0x8102766e, + 0x922a4f1d, 0x9ccda7cc, 0xb47a3dfb, 0xba9dd52a, 0xa9b5ec59, + 0xa7520488}, + {0x00000000, 0x3c60e308, 0x78c1c610, 0x44a12518, 0xf1838c20, + 0xcde36f28, 0x89424a30, 0xb522a938, 0x38761e01, 0x0416fd09, + 0x40b7d811, 0x7cd73b19, 0xc9f59221, 0xf5957129, 0xb1345431, + 0x8d54b739, 0x70ec3c02, 0x4c8cdf0a, 0x082dfa12, 0x344d191a, + 0x816fb022, 0xbd0f532a, 0xf9ae7632, 0xc5ce953a, 0x489a2203, + 0x74fac10b, 0x305be413, 0x0c3b071b, 0xb919ae23, 0x85794d2b, + 0xc1d86833, 0xfdb88b3b, 0xe1d87804, 0xddb89b0c, 0x9919be14, + 0xa5795d1c, 0x105bf424, 0x2c3b172c, 0x689a3234, 0x54fad13c, + 0xd9ae6605, 0xe5ce850d, 0xa16fa015, 0x9d0f431d, 0x282dea25, + 0x144d092d, 0x50ec2c35, 0x6c8ccf3d, 0x91344406, 0xad54a70e, + 0xe9f58216, 0xd595611e, 0x60b7c826, 0x5cd72b2e, 0x18760e36, + 0x2416ed3e, 0xa9425a07, 0x9522b90f, 0xd1839c17, 0xede37f1f, + 0x58c1d627, 0x64a1352f, 0x20001037, 0x1c60f33f, 0x18c1f649, + 0x24a11541, 0x60003059, 0x5c60d351, 0xe9427a69, 0xd5229961, + 0x9183bc79, 0xade35f71, 0x20b7e848, 0x1cd70b40, 0x58762e58, + 0x6416cd50, 0xd1346468, 0xed548760, 0xa9f5a278, 0x95954170, + 0x682dca4b, 0x544d2943, 0x10ec0c5b, 0x2c8cef53, 0x99ae466b, + 0xa5cea563, 0xe16f807b, 0xdd0f6373, 0x505bd44a, 0x6c3b3742, + 0x289a125a, 0x14faf152, 0xa1d8586a, 0x9db8bb62, 0xd9199e7a, + 0xe5797d72, 0xf9198e4d, 0xc5796d45, 0x81d8485d, 0xbdb8ab55, + 0x089a026d, 0x34fae165, 0x705bc47d, 0x4c3b2775, 0xc16f904c, + 0xfd0f7344, 0xb9ae565c, 0x85ceb554, 0x30ec1c6c, 0x0c8cff64, + 0x482dda7c, 0x744d3974, 0x89f5b24f, 0xb5955147, 0xf134745f, + 0xcd549757, 0x78763e6f, 0x4416dd67, 0x00b7f87f, 0x3cd71b77, + 0xb183ac4e, 0x8de34f46, 0xc9426a5e, 0xf5228956, 0x4000206e, + 0x7c60c366, 0x38c1e67e, 0x04a10576, 0x3183ec92, 0x0de30f9a, + 0x49422a82, 0x7522c98a, 0xc00060b2, 0xfc6083ba, 0xb8c1a6a2, + 0x84a145aa, 0x09f5f293, 0x3595119b, 0x71343483, 0x4d54d78b, + 0xf8767eb3, 0xc4169dbb, 0x80b7b8a3, 0xbcd75bab, 0x416fd090, + 0x7d0f3398, 0x39ae1680, 0x05cef588, 0xb0ec5cb0, 0x8c8cbfb8, + 0xc82d9aa0, 0xf44d79a8, 0x7919ce91, 0x45792d99, 0x01d80881, + 0x3db8eb89, 0x889a42b1, 0xb4faa1b9, 0xf05b84a1, 0xcc3b67a9, + 0xd05b9496, 0xec3b779e, 0xa89a5286, 0x94fab18e, 0x21d818b6, + 0x1db8fbbe, 0x5919dea6, 0x65793dae, 0xe82d8a97, 0xd44d699f, + 0x90ec4c87, 0xac8caf8f, 0x19ae06b7, 0x25cee5bf, 0x616fc0a7, + 0x5d0f23af, 0xa0b7a894, 0x9cd74b9c, 0xd8766e84, 0xe4168d8c, + 0x513424b4, 0x6d54c7bc, 0x29f5e2a4, 0x159501ac, 0x98c1b695, + 0xa4a1559d, 0xe0007085, 0xdc60938d, 0x69423ab5, 0x5522d9bd, + 0x1183fca5, 0x2de31fad, 0x29421adb, 0x1522f9d3, 0x5183dccb, + 0x6de33fc3, 0xd8c196fb, 0xe4a175f3, 0xa00050eb, 0x9c60b3e3, + 0x113404da, 0x2d54e7d2, 0x69f5c2ca, 0x559521c2, 0xe0b788fa, + 0xdcd76bf2, 0x98764eea, 0xa416ade2, 0x59ae26d9, 0x65cec5d1, + 0x216fe0c9, 0x1d0f03c1, 0xa82daaf9, 0x944d49f1, 0xd0ec6ce9, + 0xec8c8fe1, 0x61d838d8, 0x5db8dbd0, 0x1919fec8, 0x25791dc0, + 0x905bb4f8, 0xac3b57f0, 0xe89a72e8, 0xd4fa91e0, 0xc89a62df, + 0xf4fa81d7, 0xb05ba4cf, 0x8c3b47c7, 0x3919eeff, 0x05790df7, + 0x41d828ef, 0x7db8cbe7, 0xf0ec7cde, 0xcc8c9fd6, 0x882dbace, + 0xb44d59c6, 0x016ff0fe, 0x3d0f13f6, 0x79ae36ee, 0x45ced5e6, + 0xb8765edd, 0x8416bdd5, 0xc0b798cd, 0xfcd77bc5, 0x49f5d2fd, + 0x759531f5, 0x313414ed, 0x0d54f7e5, 0x800040dc, 0xbc60a3d4, + 0xf8c186cc, 0xc4a165c4, 0x7183ccfc, 0x4de32ff4, 0x09420aec, + 0x3522e9e4}, + {0x00000000, 0x6307d924, 0xc60fb248, 0xa5086b6c, 0x576e62d1, + 0x3469bbf5, 0x9161d099, 0xf26609bd, 0xaedcc5a2, 0xcddb1c86, + 0x68d377ea, 0x0bd4aece, 0xf9b2a773, 0x9ab57e57, 0x3fbd153b, + 0x5cbacc1f, 0x86c88d05, 0xe5cf5421, 0x40c73f4d, 0x23c0e669, + 0xd1a6efd4, 0xb2a136f0, 0x17a95d9c, 0x74ae84b8, 0x281448a7, + 0x4b139183, 0xee1bfaef, 0x8d1c23cb, 0x7f7a2a76, 0x1c7df352, + 0xb975983e, 0xda72411a, 0xd6e01c4b, 0xb5e7c56f, 0x10efae03, + 0x73e87727, 0x818e7e9a, 0xe289a7be, 0x4781ccd2, 0x248615f6, + 0x783cd9e9, 0x1b3b00cd, 0xbe336ba1, 0xdd34b285, 0x2f52bb38, + 0x4c55621c, 0xe95d0970, 0x8a5ad054, 0x5028914e, 0x332f486a, + 0x96272306, 0xf520fa22, 0x0746f39f, 0x64412abb, 0xc14941d7, + 0xa24e98f3, 0xfef454ec, 0x9df38dc8, 0x38fbe6a4, 0x5bfc3f80, + 0xa99a363d, 0xca9def19, 0x6f958475, 0x0c925d51, 0x76b13ed7, + 0x15b6e7f3, 0xb0be8c9f, 0xd3b955bb, 0x21df5c06, 0x42d88522, + 0xe7d0ee4e, 0x84d7376a, 0xd86dfb75, 0xbb6a2251, 0x1e62493d, + 0x7d659019, 0x8f0399a4, 0xec044080, 0x490c2bec, 0x2a0bf2c8, + 0xf079b3d2, 0x937e6af6, 0x3676019a, 0x5571d8be, 0xa717d103, + 0xc4100827, 0x6118634b, 0x021fba6f, 0x5ea57670, 0x3da2af54, + 0x98aac438, 0xfbad1d1c, 0x09cb14a1, 0x6acccd85, 0xcfc4a6e9, + 0xacc37fcd, 0xa051229c, 0xc356fbb8, 0x665e90d4, 0x055949f0, + 0xf73f404d, 0x94389969, 0x3130f205, 0x52372b21, 0x0e8de73e, + 0x6d8a3e1a, 0xc8825576, 0xab858c52, 0x59e385ef, 0x3ae45ccb, + 0x9fec37a7, 0xfcebee83, 0x2699af99, 0x459e76bd, 0xe0961dd1, + 0x8391c4f5, 0x71f7cd48, 0x12f0146c, 0xb7f87f00, 0xd4ffa624, + 0x88456a3b, 0xeb42b31f, 0x4e4ad873, 0x2d4d0157, 0xdf2b08ea, + 0xbc2cd1ce, 0x1924baa2, 0x7a236386, 0xed627dae, 0x8e65a48a, + 0x2b6dcfe6, 0x486a16c2, 0xba0c1f7f, 0xd90bc65b, 0x7c03ad37, + 0x1f047413, 0x43beb80c, 0x20b96128, 0x85b10a44, 0xe6b6d360, + 0x14d0dadd, 0x77d703f9, 0xd2df6895, 0xb1d8b1b1, 0x6baaf0ab, + 0x08ad298f, 0xada542e3, 0xcea29bc7, 0x3cc4927a, 0x5fc34b5e, + 0xfacb2032, 0x99ccf916, 0xc5763509, 0xa671ec2d, 0x03798741, + 0x607e5e65, 0x921857d8, 0xf11f8efc, 0x5417e590, 0x37103cb4, + 0x3b8261e5, 0x5885b8c1, 0xfd8dd3ad, 0x9e8a0a89, 0x6cec0334, + 0x0febda10, 0xaae3b17c, 0xc9e46858, 0x955ea447, 0xf6597d63, + 0x5351160f, 0x3056cf2b, 0xc230c696, 0xa1371fb2, 0x043f74de, + 0x6738adfa, 0xbd4aece0, 0xde4d35c4, 0x7b455ea8, 0x1842878c, + 0xea248e31, 0x89235715, 0x2c2b3c79, 0x4f2ce55d, 0x13962942, + 0x7091f066, 0xd5999b0a, 0xb69e422e, 0x44f84b93, 0x27ff92b7, + 0x82f7f9db, 0xe1f020ff, 0x9bd34379, 0xf8d49a5d, 0x5ddcf131, + 0x3edb2815, 0xccbd21a8, 0xafbaf88c, 0x0ab293e0, 0x69b54ac4, + 0x350f86db, 0x56085fff, 0xf3003493, 0x9007edb7, 0x6261e40a, + 0x01663d2e, 0xa46e5642, 0xc7698f66, 0x1d1bce7c, 0x7e1c1758, + 0xdb147c34, 0xb813a510, 0x4a75acad, 0x29727589, 0x8c7a1ee5, + 0xef7dc7c1, 0xb3c70bde, 0xd0c0d2fa, 0x75c8b996, 0x16cf60b2, + 0xe4a9690f, 0x87aeb02b, 0x22a6db47, 0x41a10263, 0x4d335f32, + 0x2e348616, 0x8b3ced7a, 0xe83b345e, 0x1a5d3de3, 0x795ae4c7, + 0xdc528fab, 0xbf55568f, 0xe3ef9a90, 0x80e843b4, 0x25e028d8, + 0x46e7f1fc, 0xb481f841, 0xd7862165, 0x728e4a09, 0x1189932d, + 0xcbfbd237, 0xa8fc0b13, 0x0df4607f, 0x6ef3b95b, 0x9c95b0e6, + 0xff9269c2, 0x5a9a02ae, 0x399ddb8a, 0x65271795, 0x0620ceb1, + 0xa328a5dd, 0xc02f7cf9, 0x32497544, 0x514eac60, 0xf446c70c, + 0x97411e28}, + {0x00000000, 0x01b5fd1d, 0x036bfa3a, 0x02de0727, 0x06d7f474, + 0x07620969, 0x05bc0e4e, 0x0409f353, 0x0dafe8e8, 0x0c1a15f5, + 0x0ec412d2, 0x0f71efcf, 0x0b781c9c, 0x0acde181, 0x0813e6a6, + 0x09a61bbb, 0x1b5fd1d0, 0x1aea2ccd, 0x18342bea, 0x1981d6f7, + 0x1d8825a4, 0x1c3dd8b9, 0x1ee3df9e, 0x1f562283, 0x16f03938, + 0x1745c425, 0x159bc302, 0x142e3e1f, 0x1027cd4c, 0x11923051, + 0x134c3776, 0x12f9ca6b, 0x36bfa3a0, 0x370a5ebd, 0x35d4599a, + 0x3461a487, 0x306857d4, 0x31ddaac9, 0x3303adee, 0x32b650f3, + 0x3b104b48, 0x3aa5b655, 0x387bb172, 0x39ce4c6f, 0x3dc7bf3c, + 0x3c724221, 0x3eac4506, 0x3f19b81b, 0x2de07270, 0x2c558f6d, + 0x2e8b884a, 0x2f3e7557, 0x2b378604, 0x2a827b19, 0x285c7c3e, + 0x29e98123, 0x204f9a98, 0x21fa6785, 0x232460a2, 0x22919dbf, + 0x26986eec, 0x272d93f1, 0x25f394d6, 0x244669cb, 0x6d7f4740, + 0x6ccaba5d, 0x6e14bd7a, 0x6fa14067, 0x6ba8b334, 0x6a1d4e29, + 0x68c3490e, 0x6976b413, 0x60d0afa8, 0x616552b5, 0x63bb5592, + 0x620ea88f, 0x66075bdc, 0x67b2a6c1, 0x656ca1e6, 0x64d95cfb, + 0x76209690, 0x77956b8d, 0x754b6caa, 0x74fe91b7, 0x70f762e4, + 0x71429ff9, 0x739c98de, 0x722965c3, 0x7b8f7e78, 0x7a3a8365, + 0x78e48442, 0x7951795f, 0x7d588a0c, 0x7ced7711, 0x7e337036, + 0x7f868d2b, 0x5bc0e4e0, 0x5a7519fd, 0x58ab1eda, 0x591ee3c7, + 0x5d171094, 0x5ca2ed89, 0x5e7ceaae, 0x5fc917b3, 0x566f0c08, + 0x57daf115, 0x5504f632, 0x54b10b2f, 0x50b8f87c, 0x510d0561, + 0x53d30246, 0x5266ff5b, 0x409f3530, 0x412ac82d, 0x43f4cf0a, + 0x42413217, 0x4648c144, 0x47fd3c59, 0x45233b7e, 0x4496c663, + 0x4d30ddd8, 0x4c8520c5, 0x4e5b27e2, 0x4feedaff, 0x4be729ac, + 0x4a52d4b1, 0x488cd396, 0x49392e8b, 0xdafe8e80, 0xdb4b739d, + 0xd99574ba, 0xd82089a7, 0xdc297af4, 0xdd9c87e9, 0xdf4280ce, + 0xdef77dd3, 0xd7516668, 0xd6e49b75, 0xd43a9c52, 0xd58f614f, + 0xd186921c, 0xd0336f01, 0xd2ed6826, 0xd358953b, 0xc1a15f50, + 0xc014a24d, 0xc2caa56a, 0xc37f5877, 0xc776ab24, 0xc6c35639, + 0xc41d511e, 0xc5a8ac03, 0xcc0eb7b8, 0xcdbb4aa5, 0xcf654d82, + 0xced0b09f, 0xcad943cc, 0xcb6cbed1, 0xc9b2b9f6, 0xc80744eb, + 0xec412d20, 0xedf4d03d, 0xef2ad71a, 0xee9f2a07, 0xea96d954, + 0xeb232449, 0xe9fd236e, 0xe848de73, 0xe1eec5c8, 0xe05b38d5, + 0xe2853ff2, 0xe330c2ef, 0xe73931bc, 0xe68ccca1, 0xe452cb86, + 0xe5e7369b, 0xf71efcf0, 0xf6ab01ed, 0xf47506ca, 0xf5c0fbd7, + 0xf1c90884, 0xf07cf599, 0xf2a2f2be, 0xf3170fa3, 0xfab11418, + 0xfb04e905, 0xf9daee22, 0xf86f133f, 0xfc66e06c, 0xfdd31d71, + 0xff0d1a56, 0xfeb8e74b, 0xb781c9c0, 0xb63434dd, 0xb4ea33fa, + 0xb55fcee7, 0xb1563db4, 0xb0e3c0a9, 0xb23dc78e, 0xb3883a93, + 0xba2e2128, 0xbb9bdc35, 0xb945db12, 0xb8f0260f, 0xbcf9d55c, + 0xbd4c2841, 0xbf922f66, 0xbe27d27b, 0xacde1810, 0xad6be50d, + 0xafb5e22a, 0xae001f37, 0xaa09ec64, 0xabbc1179, 0xa962165e, + 0xa8d7eb43, 0xa171f0f8, 0xa0c40de5, 0xa21a0ac2, 0xa3aff7df, + 0xa7a6048c, 0xa613f991, 0xa4cdfeb6, 0xa57803ab, 0x813e6a60, + 0x808b977d, 0x8255905a, 0x83e06d47, 0x87e99e14, 0x865c6309, + 0x8482642e, 0x85379933, 0x8c918288, 0x8d247f95, 0x8ffa78b2, + 0x8e4f85af, 0x8a4676fc, 0x8bf38be1, 0x892d8cc6, 0x889871db, + 0x9a61bbb0, 0x9bd446ad, 0x990a418a, 0x98bfbc97, 0x9cb64fc4, + 0x9d03b2d9, 0x9fddb5fe, 0x9e6848e3, 0x97ce5358, 0x967bae45, + 0x94a5a962, 0x9510547f, 0x9119a72c, 0x90ac5a31, 0x92725d16, + 0x93c7a00b}, + {0x00000000, 0x6e8c1b41, 0xdd183682, 0xb3942dc3, 0x61416b45, + 0x0fcd7004, 0xbc595dc7, 0xd2d54686, 0xc282d68a, 0xac0ecdcb, + 0x1f9ae008, 0x7116fb49, 0xa3c3bdcf, 0xcd4fa68e, 0x7edb8b4d, + 0x1057900c, 0x5e74ab55, 0x30f8b014, 0x836c9dd7, 0xede08696, + 0x3f35c010, 0x51b9db51, 0xe22df692, 0x8ca1edd3, 0x9cf67ddf, + 0xf27a669e, 0x41ee4b5d, 0x2f62501c, 0xfdb7169a, 0x933b0ddb, + 0x20af2018, 0x4e233b59, 0xbce956aa, 0xd2654deb, 0x61f16028, + 0x0f7d7b69, 0xdda83def, 0xb32426ae, 0x00b00b6d, 0x6e3c102c, + 0x7e6b8020, 0x10e79b61, 0xa373b6a2, 0xcdffade3, 0x1f2aeb65, + 0x71a6f024, 0xc232dde7, 0xacbec6a6, 0xe29dfdff, 0x8c11e6be, + 0x3f85cb7d, 0x5109d03c, 0x83dc96ba, 0xed508dfb, 0x5ec4a038, + 0x3048bb79, 0x201f2b75, 0x4e933034, 0xfd071df7, 0x938b06b6, + 0x415e4030, 0x2fd25b71, 0x9c4676b2, 0xf2ca6df3, 0xa2a3ab15, + 0xcc2fb054, 0x7fbb9d97, 0x113786d6, 0xc3e2c050, 0xad6edb11, + 0x1efaf6d2, 0x7076ed93, 0x60217d9f, 0x0ead66de, 0xbd394b1d, + 0xd3b5505c, 0x016016da, 0x6fec0d9b, 0xdc782058, 0xb2f43b19, + 0xfcd70040, 0x925b1b01, 0x21cf36c2, 0x4f432d83, 0x9d966b05, + 0xf31a7044, 0x408e5d87, 0x2e0246c6, 0x3e55d6ca, 0x50d9cd8b, + 0xe34de048, 0x8dc1fb09, 0x5f14bd8f, 0x3198a6ce, 0x820c8b0d, + 0xec80904c, 0x1e4afdbf, 0x70c6e6fe, 0xc352cb3d, 0xadded07c, + 0x7f0b96fa, 0x11878dbb, 0xa213a078, 0xcc9fbb39, 0xdcc82b35, + 0xb2443074, 0x01d01db7, 0x6f5c06f6, 0xbd894070, 0xd3055b31, + 0x609176f2, 0x0e1d6db3, 0x403e56ea, 0x2eb24dab, 0x9d266068, + 0xf3aa7b29, 0x217f3daf, 0x4ff326ee, 0xfc670b2d, 0x92eb106c, + 0x82bc8060, 0xec309b21, 0x5fa4b6e2, 0x3128ada3, 0xe3fdeb25, + 0x8d71f064, 0x3ee5dda7, 0x5069c6e6, 0x9e36506b, 0xf0ba4b2a, + 0x432e66e9, 0x2da27da8, 0xff773b2e, 0x91fb206f, 0x226f0dac, + 0x4ce316ed, 0x5cb486e1, 0x32389da0, 0x81acb063, 0xef20ab22, + 0x3df5eda4, 0x5379f6e5, 0xe0eddb26, 0x8e61c067, 0xc042fb3e, + 0xaecee07f, 0x1d5acdbc, 0x73d6d6fd, 0xa103907b, 0xcf8f8b3a, + 0x7c1ba6f9, 0x1297bdb8, 0x02c02db4, 0x6c4c36f5, 0xdfd81b36, + 0xb1540077, 0x638146f1, 0x0d0d5db0, 0xbe997073, 0xd0156b32, + 0x22df06c1, 0x4c531d80, 0xffc73043, 0x914b2b02, 0x439e6d84, + 0x2d1276c5, 0x9e865b06, 0xf00a4047, 0xe05dd04b, 0x8ed1cb0a, + 0x3d45e6c9, 0x53c9fd88, 0x811cbb0e, 0xef90a04f, 0x5c048d8c, + 0x328896cd, 0x7cabad94, 0x1227b6d5, 0xa1b39b16, 0xcf3f8057, + 0x1deac6d1, 0x7366dd90, 0xc0f2f053, 0xae7eeb12, 0xbe297b1e, + 0xd0a5605f, 0x63314d9c, 0x0dbd56dd, 0xdf68105b, 0xb1e40b1a, + 0x027026d9, 0x6cfc3d98, 0x3c95fb7e, 0x5219e03f, 0xe18dcdfc, + 0x8f01d6bd, 0x5dd4903b, 0x33588b7a, 0x80cca6b9, 0xee40bdf8, + 0xfe172df4, 0x909b36b5, 0x230f1b76, 0x4d830037, 0x9f5646b1, + 0xf1da5df0, 0x424e7033, 0x2cc26b72, 0x62e1502b, 0x0c6d4b6a, + 0xbff966a9, 0xd1757de8, 0x03a03b6e, 0x6d2c202f, 0xdeb80dec, + 0xb03416ad, 0xa06386a1, 0xceef9de0, 0x7d7bb023, 0x13f7ab62, + 0xc122ede4, 0xafaef6a5, 0x1c3adb66, 0x72b6c027, 0x807cadd4, + 0xeef0b695, 0x5d649b56, 0x33e88017, 0xe13dc691, 0x8fb1ddd0, + 0x3c25f013, 0x52a9eb52, 0x42fe7b5e, 0x2c72601f, 0x9fe64ddc, + 0xf16a569d, 0x23bf101b, 0x4d330b5a, 0xfea72699, 0x902b3dd8, + 0xde080681, 0xb0841dc0, 0x03103003, 0x6d9c2b42, 0xbf496dc4, + 0xd1c57685, 0x62515b46, 0x0cdd4007, 0x1c8ad00b, 0x7206cb4a, + 0xc192e689, 0xaf1efdc8, 0x7dcbbb4e, 0x1347a00f, 0xa0d38dcc, + 0xce5f968d}, + {0x00000000, 0xe71da697, 0x154a4b6f, 0xf257edf8, 0x2a9496de, + 0xcd893049, 0x3fdeddb1, 0xd8c37b26, 0x55292dbc, 0xb2348b2b, + 0x406366d3, 0xa77ec044, 0x7fbdbb62, 0x98a01df5, 0x6af7f00d, + 0x8dea569a, 0xaa525b78, 0x4d4ffdef, 0xbf181017, 0x5805b680, + 0x80c6cda6, 0x67db6b31, 0x958c86c9, 0x7291205e, 0xff7b76c4, + 0x1866d053, 0xea313dab, 0x0d2c9b3c, 0xd5efe01a, 0x32f2468d, + 0xc0a5ab75, 0x27b80de2, 0x8fd5b0b1, 0x68c81626, 0x9a9ffbde, + 0x7d825d49, 0xa541266f, 0x425c80f8, 0xb00b6d00, 0x5716cb97, + 0xdafc9d0d, 0x3de13b9a, 0xcfb6d662, 0x28ab70f5, 0xf0680bd3, + 0x1775ad44, 0xe52240bc, 0x023fe62b, 0x2587ebc9, 0xc29a4d5e, + 0x30cda0a6, 0xd7d00631, 0x0f137d17, 0xe80edb80, 0x1a593678, + 0xfd4490ef, 0x70aec675, 0x97b360e2, 0x65e48d1a, 0x82f92b8d, + 0x5a3a50ab, 0xbd27f63c, 0x4f701bc4, 0xa86dbd53, 0xc4da6723, + 0x23c7c1b4, 0xd1902c4c, 0x368d8adb, 0xee4ef1fd, 0x0953576a, + 0xfb04ba92, 0x1c191c05, 0x91f34a9f, 0x76eeec08, 0x84b901f0, + 0x63a4a767, 0xbb67dc41, 0x5c7a7ad6, 0xae2d972e, 0x493031b9, + 0x6e883c5b, 0x89959acc, 0x7bc27734, 0x9cdfd1a3, 0x441caa85, + 0xa3010c12, 0x5156e1ea, 0xb64b477d, 0x3ba111e7, 0xdcbcb770, + 0x2eeb5a88, 0xc9f6fc1f, 0x11358739, 0xf62821ae, 0x047fcc56, + 0xe3626ac1, 0x4b0fd792, 0xac127105, 0x5e459cfd, 0xb9583a6a, + 0x619b414c, 0x8686e7db, 0x74d10a23, 0x93ccacb4, 0x1e26fa2e, + 0xf93b5cb9, 0x0b6cb141, 0xec7117d6, 0x34b26cf0, 0xd3afca67, + 0x21f8279f, 0xc6e58108, 0xe15d8cea, 0x06402a7d, 0xf417c785, + 0x130a6112, 0xcbc91a34, 0x2cd4bca3, 0xde83515b, 0x399ef7cc, + 0xb474a156, 0x536907c1, 0xa13eea39, 0x46234cae, 0x9ee03788, + 0x79fd911f, 0x8baa7ce7, 0x6cb7da70, 0x52c5c807, 0xb5d86e90, + 0x478f8368, 0xa09225ff, 0x78515ed9, 0x9f4cf84e, 0x6d1b15b6, + 0x8a06b321, 0x07ece5bb, 0xe0f1432c, 0x12a6aed4, 0xf5bb0843, + 0x2d787365, 0xca65d5f2, 0x3832380a, 0xdf2f9e9d, 0xf897937f, + 0x1f8a35e8, 0xedddd810, 0x0ac07e87, 0xd20305a1, 0x351ea336, + 0xc7494ece, 0x2054e859, 0xadbebec3, 0x4aa31854, 0xb8f4f5ac, + 0x5fe9533b, 0x872a281d, 0x60378e8a, 0x92606372, 0x757dc5e5, + 0xdd1078b6, 0x3a0dde21, 0xc85a33d9, 0x2f47954e, 0xf784ee68, + 0x109948ff, 0xe2cea507, 0x05d30390, 0x8839550a, 0x6f24f39d, + 0x9d731e65, 0x7a6eb8f2, 0xa2adc3d4, 0x45b06543, 0xb7e788bb, + 0x50fa2e2c, 0x774223ce, 0x905f8559, 0x620868a1, 0x8515ce36, + 0x5dd6b510, 0xbacb1387, 0x489cfe7f, 0xaf8158e8, 0x226b0e72, + 0xc576a8e5, 0x3721451d, 0xd03ce38a, 0x08ff98ac, 0xefe23e3b, + 0x1db5d3c3, 0xfaa87554, 0x961faf24, 0x710209b3, 0x8355e44b, + 0x644842dc, 0xbc8b39fa, 0x5b969f6d, 0xa9c17295, 0x4edcd402, + 0xc3368298, 0x242b240f, 0xd67cc9f7, 0x31616f60, 0xe9a21446, + 0x0ebfb2d1, 0xfce85f29, 0x1bf5f9be, 0x3c4df45c, 0xdb5052cb, + 0x2907bf33, 0xce1a19a4, 0x16d96282, 0xf1c4c415, 0x039329ed, + 0xe48e8f7a, 0x6964d9e0, 0x8e797f77, 0x7c2e928f, 0x9b333418, + 0x43f04f3e, 0xa4ede9a9, 0x56ba0451, 0xb1a7a2c6, 0x19ca1f95, + 0xfed7b902, 0x0c8054fa, 0xeb9df26d, 0x335e894b, 0xd4432fdc, + 0x2614c224, 0xc10964b3, 0x4ce33229, 0xabfe94be, 0x59a97946, + 0xbeb4dfd1, 0x6677a4f7, 0x816a0260, 0x733def98, 0x9420490f, + 0xb39844ed, 0x5485e27a, 0xa6d20f82, 0x41cfa915, 0x990cd233, + 0x7e1174a4, 0x8c46995c, 0x6b5b3fcb, 0xe6b16951, 0x01accfc6, + 0xf3fb223e, 0x14e684a9, 0xcc25ff8f, 0x2b385918, 0xd96fb4e0, + 0x3e721277}, + {0x00000000, 0xa58b900e, 0x9066265d, 0x35edb653, 0xfbbd4afb, + 0x5e36daf5, 0x6bdb6ca6, 0xce50fca8, 0x2c0b93b7, 0x898003b9, + 0xbc6db5ea, 0x19e625e4, 0xd7b6d94c, 0x723d4942, 0x47d0ff11, + 0xe25b6f1f, 0x5817276e, 0xfd9cb760, 0xc8710133, 0x6dfa913d, + 0xa3aa6d95, 0x0621fd9b, 0x33cc4bc8, 0x9647dbc6, 0x741cb4d9, + 0xd19724d7, 0xe47a9284, 0x41f1028a, 0x8fa1fe22, 0x2a2a6e2c, + 0x1fc7d87f, 0xba4c4871, 0xb02e4edc, 0x15a5ded2, 0x20486881, + 0x85c3f88f, 0x4b930427, 0xee189429, 0xdbf5227a, 0x7e7eb274, + 0x9c25dd6b, 0x39ae4d65, 0x0c43fb36, 0xa9c86b38, 0x67989790, + 0xc213079e, 0xf7feb1cd, 0x527521c3, 0xe83969b2, 0x4db2f9bc, + 0x785f4fef, 0xddd4dfe1, 0x13842349, 0xb60fb347, 0x83e20514, + 0x2669951a, 0xc432fa05, 0x61b96a0b, 0x5454dc58, 0xf1df4c56, + 0x3f8fb0fe, 0x9a0420f0, 0xafe996a3, 0x0a6206ad, 0xbb2d9bf9, + 0x1ea60bf7, 0x2b4bbda4, 0x8ec02daa, 0x4090d102, 0xe51b410c, + 0xd0f6f75f, 0x757d6751, 0x9726084e, 0x32ad9840, 0x07402e13, + 0xa2cbbe1d, 0x6c9b42b5, 0xc910d2bb, 0xfcfd64e8, 0x5976f4e6, + 0xe33abc97, 0x46b12c99, 0x735c9aca, 0xd6d70ac4, 0x1887f66c, + 0xbd0c6662, 0x88e1d031, 0x2d6a403f, 0xcf312f20, 0x6ababf2e, + 0x5f57097d, 0xfadc9973, 0x348c65db, 0x9107f5d5, 0xa4ea4386, + 0x0161d388, 0x0b03d525, 0xae88452b, 0x9b65f378, 0x3eee6376, + 0xf0be9fde, 0x55350fd0, 0x60d8b983, 0xc553298d, 0x27084692, + 0x8283d69c, 0xb76e60cf, 0x12e5f0c1, 0xdcb50c69, 0x793e9c67, + 0x4cd32a34, 0xe958ba3a, 0x5314f24b, 0xf69f6245, 0xc372d416, + 0x66f94418, 0xa8a9b8b0, 0x0d2228be, 0x38cf9eed, 0x9d440ee3, + 0x7f1f61fc, 0xda94f1f2, 0xef7947a1, 0x4af2d7af, 0x84a22b07, + 0x2129bb09, 0x14c40d5a, 0xb14f9d54, 0xad2a31b3, 0x08a1a1bd, + 0x3d4c17ee, 0x98c787e0, 0x56977b48, 0xf31ceb46, 0xc6f15d15, + 0x637acd1b, 0x8121a204, 0x24aa320a, 0x11478459, 0xb4cc1457, + 0x7a9ce8ff, 0xdf1778f1, 0xeafacea2, 0x4f715eac, 0xf53d16dd, + 0x50b686d3, 0x655b3080, 0xc0d0a08e, 0x0e805c26, 0xab0bcc28, + 0x9ee67a7b, 0x3b6dea75, 0xd936856a, 0x7cbd1564, 0x4950a337, + 0xecdb3339, 0x228bcf91, 0x87005f9f, 0xb2ede9cc, 0x176679c2, + 0x1d047f6f, 0xb88fef61, 0x8d625932, 0x28e9c93c, 0xe6b93594, + 0x4332a59a, 0x76df13c9, 0xd35483c7, 0x310fecd8, 0x94847cd6, + 0xa169ca85, 0x04e25a8b, 0xcab2a623, 0x6f39362d, 0x5ad4807e, + 0xff5f1070, 0x45135801, 0xe098c80f, 0xd5757e5c, 0x70feee52, + 0xbeae12fa, 0x1b2582f4, 0x2ec834a7, 0x8b43a4a9, 0x6918cbb6, + 0xcc935bb8, 0xf97eedeb, 0x5cf57de5, 0x92a5814d, 0x372e1143, + 0x02c3a710, 0xa748371e, 0x1607aa4a, 0xb38c3a44, 0x86618c17, + 0x23ea1c19, 0xedbae0b1, 0x483170bf, 0x7ddcc6ec, 0xd85756e2, + 0x3a0c39fd, 0x9f87a9f3, 0xaa6a1fa0, 0x0fe18fae, 0xc1b17306, + 0x643ae308, 0x51d7555b, 0xf45cc555, 0x4e108d24, 0xeb9b1d2a, + 0xde76ab79, 0x7bfd3b77, 0xb5adc7df, 0x102657d1, 0x25cbe182, + 0x8040718c, 0x621b1e93, 0xc7908e9d, 0xf27d38ce, 0x57f6a8c0, + 0x99a65468, 0x3c2dc466, 0x09c07235, 0xac4be23b, 0xa629e496, + 0x03a27498, 0x364fc2cb, 0x93c452c5, 0x5d94ae6d, 0xf81f3e63, + 0xcdf28830, 0x6879183e, 0x8a227721, 0x2fa9e72f, 0x1a44517c, + 0xbfcfc172, 0x719f3dda, 0xd414add4, 0xe1f91b87, 0x44728b89, + 0xfe3ec3f8, 0x5bb553f6, 0x6e58e5a5, 0xcbd375ab, 0x05838903, + 0xa008190d, 0x95e5af5e, 0x306e3f50, 0xd235504f, 0x77bec041, + 0x42537612, 0xe7d8e61c, 0x29881ab4, 0x8c038aba, 0xb9ee3ce9, + 0x1c65ace7}}; + +local const z_word_t FAR crc_braid_big_table[][256] = { + {0x0000000000000000, 0x0e908ba500000000, 0x5d26669000000000, + 0x53b6ed3500000000, 0xfb4abdfb00000000, 0xf5da365e00000000, + 0xa66cdb6b00000000, 0xa8fc50ce00000000, 0xb7930b2c00000000, + 0xb903808900000000, 0xeab56dbc00000000, 0xe425e61900000000, + 0x4cd9b6d700000000, 0x42493d7200000000, 0x11ffd04700000000, + 0x1f6f5be200000000, 0x6e27175800000000, 0x60b79cfd00000000, + 0x330171c800000000, 0x3d91fa6d00000000, 0x956daaa300000000, + 0x9bfd210600000000, 0xc84bcc3300000000, 0xc6db479600000000, + 0xd9b41c7400000000, 0xd72497d100000000, 0x84927ae400000000, + 0x8a02f14100000000, 0x22fea18f00000000, 0x2c6e2a2a00000000, + 0x7fd8c71f00000000, 0x71484cba00000000, 0xdc4e2eb000000000, + 0xd2dea51500000000, 0x8168482000000000, 0x8ff8c38500000000, + 0x2704934b00000000, 0x299418ee00000000, 0x7a22f5db00000000, + 0x74b27e7e00000000, 0x6bdd259c00000000, 0x654dae3900000000, + 0x36fb430c00000000, 0x386bc8a900000000, 0x9097986700000000, + 0x9e0713c200000000, 0xcdb1fef700000000, 0xc321755200000000, + 0xb26939e800000000, 0xbcf9b24d00000000, 0xef4f5f7800000000, + 0xe1dfd4dd00000000, 0x4923841300000000, 0x47b30fb600000000, + 0x1405e28300000000, 0x1a95692600000000, 0x05fa32c400000000, + 0x0b6ab96100000000, 0x58dc545400000000, 0x564cdff100000000, + 0xfeb08f3f00000000, 0xf020049a00000000, 0xa396e9af00000000, + 0xad06620a00000000, 0xf99b2dbb00000000, 0xf70ba61e00000000, + 0xa4bd4b2b00000000, 0xaa2dc08e00000000, 0x02d1904000000000, + 0x0c411be500000000, 0x5ff7f6d000000000, 0x51677d7500000000, + 0x4e08269700000000, 0x4098ad3200000000, 0x132e400700000000, + 0x1dbecba200000000, 0xb5429b6c00000000, 0xbbd210c900000000, + 0xe864fdfc00000000, 0xe6f4765900000000, 0x97bc3ae300000000, + 0x992cb14600000000, 0xca9a5c7300000000, 0xc40ad7d600000000, + 0x6cf6871800000000, 0x62660cbd00000000, 0x31d0e18800000000, + 0x3f406a2d00000000, 0x202f31cf00000000, 0x2ebfba6a00000000, + 0x7d09575f00000000, 0x7399dcfa00000000, 0xdb658c3400000000, + 0xd5f5079100000000, 0x8643eaa400000000, 0x88d3610100000000, + 0x25d5030b00000000, 0x2b4588ae00000000, 0x78f3659b00000000, + 0x7663ee3e00000000, 0xde9fbef000000000, 0xd00f355500000000, + 0x83b9d86000000000, 0x8d2953c500000000, 0x9246082700000000, + 0x9cd6838200000000, 0xcf606eb700000000, 0xc1f0e51200000000, + 0x690cb5dc00000000, 0x679c3e7900000000, 0x342ad34c00000000, + 0x3aba58e900000000, 0x4bf2145300000000, 0x45629ff600000000, + 0x16d472c300000000, 0x1844f96600000000, 0xb0b8a9a800000000, + 0xbe28220d00000000, 0xed9ecf3800000000, 0xe30e449d00000000, + 0xfc611f7f00000000, 0xf2f194da00000000, 0xa14779ef00000000, + 0xafd7f24a00000000, 0x072ba28400000000, 0x09bb292100000000, + 0x5a0dc41400000000, 0x549d4fb100000000, 0xb3312aad00000000, + 0xbda1a10800000000, 0xee174c3d00000000, 0xe087c79800000000, + 0x487b975600000000, 0x46eb1cf300000000, 0x155df1c600000000, + 0x1bcd7a6300000000, 0x04a2218100000000, 0x0a32aa2400000000, + 0x5984471100000000, 0x5714ccb400000000, 0xffe89c7a00000000, + 0xf17817df00000000, 0xa2cefaea00000000, 0xac5e714f00000000, + 0xdd163df500000000, 0xd386b65000000000, 0x80305b6500000000, + 0x8ea0d0c000000000, 0x265c800e00000000, 0x28cc0bab00000000, + 0x7b7ae69e00000000, 0x75ea6d3b00000000, 0x6a8536d900000000, + 0x6415bd7c00000000, 0x37a3504900000000, 0x3933dbec00000000, + 0x91cf8b2200000000, 0x9f5f008700000000, 0xcce9edb200000000, + 0xc279661700000000, 0x6f7f041d00000000, 0x61ef8fb800000000, + 0x3259628d00000000, 0x3cc9e92800000000, 0x9435b9e600000000, + 0x9aa5324300000000, 0xc913df7600000000, 0xc78354d300000000, + 0xd8ec0f3100000000, 0xd67c849400000000, 0x85ca69a100000000, + 0x8b5ae20400000000, 0x23a6b2ca00000000, 0x2d36396f00000000, + 0x7e80d45a00000000, 0x70105fff00000000, 0x0158134500000000, + 0x0fc898e000000000, 0x5c7e75d500000000, 0x52eefe7000000000, + 0xfa12aebe00000000, 0xf482251b00000000, 0xa734c82e00000000, + 0xa9a4438b00000000, 0xb6cb186900000000, 0xb85b93cc00000000, + 0xebed7ef900000000, 0xe57df55c00000000, 0x4d81a59200000000, + 0x43112e3700000000, 0x10a7c30200000000, 0x1e3748a700000000, + 0x4aaa071600000000, 0x443a8cb300000000, 0x178c618600000000, + 0x191cea2300000000, 0xb1e0baed00000000, 0xbf70314800000000, + 0xecc6dc7d00000000, 0xe25657d800000000, 0xfd390c3a00000000, + 0xf3a9879f00000000, 0xa01f6aaa00000000, 0xae8fe10f00000000, + 0x0673b1c100000000, 0x08e33a6400000000, 0x5b55d75100000000, + 0x55c55cf400000000, 0x248d104e00000000, 0x2a1d9beb00000000, + 0x79ab76de00000000, 0x773bfd7b00000000, 0xdfc7adb500000000, + 0xd157261000000000, 0x82e1cb2500000000, 0x8c71408000000000, + 0x931e1b6200000000, 0x9d8e90c700000000, 0xce387df200000000, + 0xc0a8f65700000000, 0x6854a69900000000, 0x66c42d3c00000000, + 0x3572c00900000000, 0x3be24bac00000000, 0x96e429a600000000, + 0x9874a20300000000, 0xcbc24f3600000000, 0xc552c49300000000, + 0x6dae945d00000000, 0x633e1ff800000000, 0x3088f2cd00000000, + 0x3e18796800000000, 0x2177228a00000000, 0x2fe7a92f00000000, + 0x7c51441a00000000, 0x72c1cfbf00000000, 0xda3d9f7100000000, + 0xd4ad14d400000000, 0x871bf9e100000000, 0x898b724400000000, + 0xf8c33efe00000000, 0xf653b55b00000000, 0xa5e5586e00000000, + 0xab75d3cb00000000, 0x0389830500000000, 0x0d1908a000000000, + 0x5eafe59500000000, 0x503f6e3000000000, 0x4f5035d200000000, + 0x41c0be7700000000, 0x1276534200000000, 0x1ce6d8e700000000, + 0xb41a882900000000, 0xba8a038c00000000, 0xe93ceeb900000000, + 0xe7ac651c00000000}, + {0x0000000000000000, 0x97a61de700000000, 0x6f4b4a1500000000, + 0xf8ed57f200000000, 0xde96942a00000000, 0x493089cd00000000, + 0xb1ddde3f00000000, 0x267bc3d800000000, 0xbc2d295500000000, + 0x2b8b34b200000000, 0xd366634000000000, 0x44c07ea700000000, + 0x62bbbd7f00000000, 0xf51da09800000000, 0x0df0f76a00000000, + 0x9a56ea8d00000000, 0x785b52aa00000000, 0xeffd4f4d00000000, + 0x171018bf00000000, 0x80b6055800000000, 0xa6cdc68000000000, + 0x316bdb6700000000, 0xc9868c9500000000, 0x5e20917200000000, + 0xc4767bff00000000, 0x53d0661800000000, 0xab3d31ea00000000, + 0x3c9b2c0d00000000, 0x1ae0efd500000000, 0x8d46f23200000000, + 0x75aba5c000000000, 0xe20db82700000000, 0xb1b0d58f00000000, + 0x2616c86800000000, 0xdefb9f9a00000000, 0x495d827d00000000, + 0x6f2641a500000000, 0xf8805c4200000000, 0x006d0bb000000000, + 0x97cb165700000000, 0x0d9dfcda00000000, 0x9a3be13d00000000, + 0x62d6b6cf00000000, 0xf570ab2800000000, 0xd30b68f000000000, + 0x44ad751700000000, 0xbc4022e500000000, 0x2be63f0200000000, + 0xc9eb872500000000, 0x5e4d9ac200000000, 0xa6a0cd3000000000, + 0x3106d0d700000000, 0x177d130f00000000, 0x80db0ee800000000, + 0x7836591a00000000, 0xef9044fd00000000, 0x75c6ae7000000000, + 0xe260b39700000000, 0x1a8de46500000000, 0x8d2bf98200000000, + 0xab503a5a00000000, 0x3cf627bd00000000, 0xc41b704f00000000, + 0x53bd6da800000000, 0x2367dac400000000, 0xb4c1c72300000000, + 0x4c2c90d100000000, 0xdb8a8d3600000000, 0xfdf14eee00000000, + 0x6a57530900000000, 0x92ba04fb00000000, 0x051c191c00000000, + 0x9f4af39100000000, 0x08ecee7600000000, 0xf001b98400000000, + 0x67a7a46300000000, 0x41dc67bb00000000, 0xd67a7a5c00000000, + 0x2e972dae00000000, 0xb931304900000000, 0x5b3c886e00000000, + 0xcc9a958900000000, 0x3477c27b00000000, 0xa3d1df9c00000000, + 0x85aa1c4400000000, 0x120c01a300000000, 0xeae1565100000000, + 0x7d474bb600000000, 0xe711a13b00000000, 0x70b7bcdc00000000, + 0x885aeb2e00000000, 0x1ffcf6c900000000, 0x3987351100000000, + 0xae2128f600000000, 0x56cc7f0400000000, 0xc16a62e300000000, + 0x92d70f4b00000000, 0x057112ac00000000, 0xfd9c455e00000000, + 0x6a3a58b900000000, 0x4c419b6100000000, 0xdbe7868600000000, + 0x230ad17400000000, 0xb4accc9300000000, 0x2efa261e00000000, + 0xb95c3bf900000000, 0x41b16c0b00000000, 0xd61771ec00000000, + 0xf06cb23400000000, 0x67caafd300000000, 0x9f27f82100000000, + 0x0881e5c600000000, 0xea8c5de100000000, 0x7d2a400600000000, + 0x85c717f400000000, 0x12610a1300000000, 0x341ac9cb00000000, + 0xa3bcd42c00000000, 0x5b5183de00000000, 0xccf79e3900000000, + 0x56a174b400000000, 0xc107695300000000, 0x39ea3ea100000000, + 0xae4c234600000000, 0x8837e09e00000000, 0x1f91fd7900000000, + 0xe77caa8b00000000, 0x70dab76c00000000, 0x07c8c55200000000, + 0x906ed8b500000000, 0x68838f4700000000, 0xff2592a000000000, + 0xd95e517800000000, 0x4ef84c9f00000000, 0xb6151b6d00000000, + 0x21b3068a00000000, 0xbbe5ec0700000000, 0x2c43f1e000000000, + 0xd4aea61200000000, 0x4308bbf500000000, 0x6573782d00000000, + 0xf2d565ca00000000, 0x0a38323800000000, 0x9d9e2fdf00000000, + 0x7f9397f800000000, 0xe8358a1f00000000, 0x10d8dded00000000, + 0x877ec00a00000000, 0xa10503d200000000, 0x36a31e3500000000, + 0xce4e49c700000000, 0x59e8542000000000, 0xc3bebead00000000, + 0x5418a34a00000000, 0xacf5f4b800000000, 0x3b53e95f00000000, + 0x1d282a8700000000, 0x8a8e376000000000, 0x7263609200000000, + 0xe5c57d7500000000, 0xb67810dd00000000, 0x21de0d3a00000000, + 0xd9335ac800000000, 0x4e95472f00000000, 0x68ee84f700000000, + 0xff48991000000000, 0x07a5cee200000000, 0x9003d30500000000, + 0x0a55398800000000, 0x9df3246f00000000, 0x651e739d00000000, + 0xf2b86e7a00000000, 0xd4c3ada200000000, 0x4365b04500000000, + 0xbb88e7b700000000, 0x2c2efa5000000000, 0xce23427700000000, + 0x59855f9000000000, 0xa168086200000000, 0x36ce158500000000, + 0x10b5d65d00000000, 0x8713cbba00000000, 0x7ffe9c4800000000, + 0xe85881af00000000, 0x720e6b2200000000, 0xe5a876c500000000, + 0x1d45213700000000, 0x8ae33cd000000000, 0xac98ff0800000000, + 0x3b3ee2ef00000000, 0xc3d3b51d00000000, 0x5475a8fa00000000, + 0x24af1f9600000000, 0xb309027100000000, 0x4be4558300000000, + 0xdc42486400000000, 0xfa398bbc00000000, 0x6d9f965b00000000, + 0x9572c1a900000000, 0x02d4dc4e00000000, 0x988236c300000000, + 0x0f242b2400000000, 0xf7c97cd600000000, 0x606f613100000000, + 0x4614a2e900000000, 0xd1b2bf0e00000000, 0x295fe8fc00000000, + 0xbef9f51b00000000, 0x5cf44d3c00000000, 0xcb5250db00000000, + 0x33bf072900000000, 0xa4191ace00000000, 0x8262d91600000000, + 0x15c4c4f100000000, 0xed29930300000000, 0x7a8f8ee400000000, + 0xe0d9646900000000, 0x777f798e00000000, 0x8f922e7c00000000, + 0x1834339b00000000, 0x3e4ff04300000000, 0xa9e9eda400000000, + 0x5104ba5600000000, 0xc6a2a7b100000000, 0x951fca1900000000, + 0x02b9d7fe00000000, 0xfa54800c00000000, 0x6df29deb00000000, + 0x4b895e3300000000, 0xdc2f43d400000000, 0x24c2142600000000, + 0xb36409c100000000, 0x2932e34c00000000, 0xbe94feab00000000, + 0x4679a95900000000, 0xd1dfb4be00000000, 0xf7a4776600000000, + 0x60026a8100000000, 0x98ef3d7300000000, 0x0f49209400000000, + 0xed4498b300000000, 0x7ae2855400000000, 0x820fd2a600000000, + 0x15a9cf4100000000, 0x33d20c9900000000, 0xa474117e00000000, + 0x5c99468c00000000, 0xcb3f5b6b00000000, 0x5169b1e600000000, + 0xc6cfac0100000000, 0x3e22fbf300000000, 0xa984e61400000000, + 0x8fff25cc00000000, 0x1859382b00000000, 0xe0b46fd900000000, + 0x7712723e00000000}, + {0x0000000000000000, 0x411b8c6e00000000, 0x823618dd00000000, + 0xc32d94b300000000, 0x456b416100000000, 0x0470cd0f00000000, + 0xc75d59bc00000000, 0x8646d5d200000000, 0x8ad682c200000000, + 0xcbcd0eac00000000, 0x08e09a1f00000000, 0x49fb167100000000, + 0xcfbdc3a300000000, 0x8ea64fcd00000000, 0x4d8bdb7e00000000, + 0x0c90571000000000, 0x55ab745e00000000, 0x14b0f83000000000, + 0xd79d6c8300000000, 0x9686e0ed00000000, 0x10c0353f00000000, + 0x51dbb95100000000, 0x92f62de200000000, 0xd3eda18c00000000, + 0xdf7df69c00000000, 0x9e667af200000000, 0x5d4bee4100000000, + 0x1c50622f00000000, 0x9a16b7fd00000000, 0xdb0d3b9300000000, + 0x1820af2000000000, 0x593b234e00000000, 0xaa56e9bc00000000, + 0xeb4d65d200000000, 0x2860f16100000000, 0x697b7d0f00000000, + 0xef3da8dd00000000, 0xae2624b300000000, 0x6d0bb00000000000, + 0x2c103c6e00000000, 0x20806b7e00000000, 0x619be71000000000, + 0xa2b673a300000000, 0xe3adffcd00000000, 0x65eb2a1f00000000, + 0x24f0a67100000000, 0xe7dd32c200000000, 0xa6c6beac00000000, + 0xfffd9de200000000, 0xbee6118c00000000, 0x7dcb853f00000000, + 0x3cd0095100000000, 0xba96dc8300000000, 0xfb8d50ed00000000, + 0x38a0c45e00000000, 0x79bb483000000000, 0x752b1f2000000000, + 0x3430934e00000000, 0xf71d07fd00000000, 0xb6068b9300000000, + 0x30405e4100000000, 0x715bd22f00000000, 0xb276469c00000000, + 0xf36dcaf200000000, 0x15aba3a200000000, 0x54b02fcc00000000, + 0x979dbb7f00000000, 0xd686371100000000, 0x50c0e2c300000000, + 0x11db6ead00000000, 0xd2f6fa1e00000000, 0x93ed767000000000, + 0x9f7d216000000000, 0xde66ad0e00000000, 0x1d4b39bd00000000, + 0x5c50b5d300000000, 0xda16600100000000, 0x9b0dec6f00000000, + 0x582078dc00000000, 0x193bf4b200000000, 0x4000d7fc00000000, + 0x011b5b9200000000, 0xc236cf2100000000, 0x832d434f00000000, + 0x056b969d00000000, 0x44701af300000000, 0x875d8e4000000000, + 0xc646022e00000000, 0xcad6553e00000000, 0x8bcdd95000000000, + 0x48e04de300000000, 0x09fbc18d00000000, 0x8fbd145f00000000, + 0xcea6983100000000, 0x0d8b0c8200000000, 0x4c9080ec00000000, + 0xbffd4a1e00000000, 0xfee6c67000000000, 0x3dcb52c300000000, + 0x7cd0dead00000000, 0xfa960b7f00000000, 0xbb8d871100000000, + 0x78a013a200000000, 0x39bb9fcc00000000, 0x352bc8dc00000000, + 0x743044b200000000, 0xb71dd00100000000, 0xf6065c6f00000000, + 0x704089bd00000000, 0x315b05d300000000, 0xf276916000000000, + 0xb36d1d0e00000000, 0xea563e4000000000, 0xab4db22e00000000, + 0x6860269d00000000, 0x297baaf300000000, 0xaf3d7f2100000000, + 0xee26f34f00000000, 0x2d0b67fc00000000, 0x6c10eb9200000000, + 0x6080bc8200000000, 0x219b30ec00000000, 0xe2b6a45f00000000, + 0xa3ad283100000000, 0x25ebfde300000000, 0x64f0718d00000000, + 0xa7dde53e00000000, 0xe6c6695000000000, 0x6b50369e00000000, + 0x2a4bbaf000000000, 0xe9662e4300000000, 0xa87da22d00000000, + 0x2e3b77ff00000000, 0x6f20fb9100000000, 0xac0d6f2200000000, + 0xed16e34c00000000, 0xe186b45c00000000, 0xa09d383200000000, + 0x63b0ac8100000000, 0x22ab20ef00000000, 0xa4edf53d00000000, + 0xe5f6795300000000, 0x26dbede000000000, 0x67c0618e00000000, + 0x3efb42c000000000, 0x7fe0ceae00000000, 0xbccd5a1d00000000, + 0xfdd6d67300000000, 0x7b9003a100000000, 0x3a8b8fcf00000000, + 0xf9a61b7c00000000, 0xb8bd971200000000, 0xb42dc00200000000, + 0xf5364c6c00000000, 0x361bd8df00000000, 0x770054b100000000, + 0xf146816300000000, 0xb05d0d0d00000000, 0x737099be00000000, + 0x326b15d000000000, 0xc106df2200000000, 0x801d534c00000000, + 0x4330c7ff00000000, 0x022b4b9100000000, 0x846d9e4300000000, + 0xc576122d00000000, 0x065b869e00000000, 0x47400af000000000, + 0x4bd05de000000000, 0x0acbd18e00000000, 0xc9e6453d00000000, + 0x88fdc95300000000, 0x0ebb1c8100000000, 0x4fa090ef00000000, + 0x8c8d045c00000000, 0xcd96883200000000, 0x94adab7c00000000, + 0xd5b6271200000000, 0x169bb3a100000000, 0x57803fcf00000000, + 0xd1c6ea1d00000000, 0x90dd667300000000, 0x53f0f2c000000000, + 0x12eb7eae00000000, 0x1e7b29be00000000, 0x5f60a5d000000000, + 0x9c4d316300000000, 0xdd56bd0d00000000, 0x5b1068df00000000, + 0x1a0be4b100000000, 0xd926700200000000, 0x983dfc6c00000000, + 0x7efb953c00000000, 0x3fe0195200000000, 0xfccd8de100000000, + 0xbdd6018f00000000, 0x3b90d45d00000000, 0x7a8b583300000000, + 0xb9a6cc8000000000, 0xf8bd40ee00000000, 0xf42d17fe00000000, + 0xb5369b9000000000, 0x761b0f2300000000, 0x3700834d00000000, + 0xb146569f00000000, 0xf05ddaf100000000, 0x33704e4200000000, + 0x726bc22c00000000, 0x2b50e16200000000, 0x6a4b6d0c00000000, + 0xa966f9bf00000000, 0xe87d75d100000000, 0x6e3ba00300000000, + 0x2f202c6d00000000, 0xec0db8de00000000, 0xad1634b000000000, + 0xa18663a000000000, 0xe09defce00000000, 0x23b07b7d00000000, + 0x62abf71300000000, 0xe4ed22c100000000, 0xa5f6aeaf00000000, + 0x66db3a1c00000000, 0x27c0b67200000000, 0xd4ad7c8000000000, + 0x95b6f0ee00000000, 0x569b645d00000000, 0x1780e83300000000, + 0x91c63de100000000, 0xd0ddb18f00000000, 0x13f0253c00000000, + 0x52eba95200000000, 0x5e7bfe4200000000, 0x1f60722c00000000, + 0xdc4de69f00000000, 0x9d566af100000000, 0x1b10bf2300000000, + 0x5a0b334d00000000, 0x9926a7fe00000000, 0xd83d2b9000000000, + 0x810608de00000000, 0xc01d84b000000000, 0x0330100300000000, + 0x422b9c6d00000000, 0xc46d49bf00000000, 0x8576c5d100000000, + 0x465b516200000000, 0x0740dd0c00000000, 0x0bd08a1c00000000, + 0x4acb067200000000, 0x89e692c100000000, 0xc8fd1eaf00000000, + 0x4ebbcb7d00000000, 0x0fa0471300000000, 0xcc8dd3a000000000, + 0x8d965fce00000000}, + {0x0000000000000000, 0x1dfdb50100000000, 0x3afa6b0300000000, + 0x2707de0200000000, 0x74f4d70600000000, 0x6909620700000000, + 0x4e0ebc0500000000, 0x53f3090400000000, 0xe8e8af0d00000000, + 0xf5151a0c00000000, 0xd212c40e00000000, 0xcfef710f00000000, + 0x9c1c780b00000000, 0x81e1cd0a00000000, 0xa6e6130800000000, + 0xbb1ba60900000000, 0xd0d15f1b00000000, 0xcd2cea1a00000000, + 0xea2b341800000000, 0xf7d6811900000000, 0xa425881d00000000, + 0xb9d83d1c00000000, 0x9edfe31e00000000, 0x8322561f00000000, + 0x3839f01600000000, 0x25c4451700000000, 0x02c39b1500000000, + 0x1f3e2e1400000000, 0x4ccd271000000000, 0x5130921100000000, + 0x76374c1300000000, 0x6bcaf91200000000, 0xa0a3bf3600000000, + 0xbd5e0a3700000000, 0x9a59d43500000000, 0x87a4613400000000, + 0xd457683000000000, 0xc9aadd3100000000, 0xeead033300000000, + 0xf350b63200000000, 0x484b103b00000000, 0x55b6a53a00000000, + 0x72b17b3800000000, 0x6f4cce3900000000, 0x3cbfc73d00000000, + 0x2142723c00000000, 0x0645ac3e00000000, 0x1bb8193f00000000, + 0x7072e02d00000000, 0x6d8f552c00000000, 0x4a888b2e00000000, + 0x57753e2f00000000, 0x0486372b00000000, 0x197b822a00000000, + 0x3e7c5c2800000000, 0x2381e92900000000, 0x989a4f2000000000, + 0x8567fa2100000000, 0xa260242300000000, 0xbf9d912200000000, + 0xec6e982600000000, 0xf1932d2700000000, 0xd694f32500000000, + 0xcb69462400000000, 0x40477f6d00000000, 0x5dbaca6c00000000, + 0x7abd146e00000000, 0x6740a16f00000000, 0x34b3a86b00000000, + 0x294e1d6a00000000, 0x0e49c36800000000, 0x13b4766900000000, + 0xa8afd06000000000, 0xb552656100000000, 0x9255bb6300000000, + 0x8fa80e6200000000, 0xdc5b076600000000, 0xc1a6b26700000000, + 0xe6a16c6500000000, 0xfb5cd96400000000, 0x9096207600000000, + 0x8d6b957700000000, 0xaa6c4b7500000000, 0xb791fe7400000000, + 0xe462f77000000000, 0xf99f427100000000, 0xde989c7300000000, + 0xc365297200000000, 0x787e8f7b00000000, 0x65833a7a00000000, + 0x4284e47800000000, 0x5f79517900000000, 0x0c8a587d00000000, + 0x1177ed7c00000000, 0x3670337e00000000, 0x2b8d867f00000000, + 0xe0e4c05b00000000, 0xfd19755a00000000, 0xda1eab5800000000, + 0xc7e31e5900000000, 0x9410175d00000000, 0x89eda25c00000000, + 0xaeea7c5e00000000, 0xb317c95f00000000, 0x080c6f5600000000, + 0x15f1da5700000000, 0x32f6045500000000, 0x2f0bb15400000000, + 0x7cf8b85000000000, 0x61050d5100000000, 0x4602d35300000000, + 0x5bff665200000000, 0x30359f4000000000, 0x2dc82a4100000000, + 0x0acff44300000000, 0x1732414200000000, 0x44c1484600000000, + 0x593cfd4700000000, 0x7e3b234500000000, 0x63c6964400000000, + 0xd8dd304d00000000, 0xc520854c00000000, 0xe2275b4e00000000, + 0xffdaee4f00000000, 0xac29e74b00000000, 0xb1d4524a00000000, + 0x96d38c4800000000, 0x8b2e394900000000, 0x808efeda00000000, + 0x9d734bdb00000000, 0xba7495d900000000, 0xa78920d800000000, + 0xf47a29dc00000000, 0xe9879cdd00000000, 0xce8042df00000000, + 0xd37df7de00000000, 0x686651d700000000, 0x759be4d600000000, + 0x529c3ad400000000, 0x4f618fd500000000, 0x1c9286d100000000, + 0x016f33d000000000, 0x2668edd200000000, 0x3b9558d300000000, + 0x505fa1c100000000, 0x4da214c000000000, 0x6aa5cac200000000, + 0x77587fc300000000, 0x24ab76c700000000, 0x3956c3c600000000, + 0x1e511dc400000000, 0x03aca8c500000000, 0xb8b70ecc00000000, + 0xa54abbcd00000000, 0x824d65cf00000000, 0x9fb0d0ce00000000, + 0xcc43d9ca00000000, 0xd1be6ccb00000000, 0xf6b9b2c900000000, + 0xeb4407c800000000, 0x202d41ec00000000, 0x3dd0f4ed00000000, + 0x1ad72aef00000000, 0x072a9fee00000000, 0x54d996ea00000000, + 0x492423eb00000000, 0x6e23fde900000000, 0x73de48e800000000, + 0xc8c5eee100000000, 0xd5385be000000000, 0xf23f85e200000000, + 0xefc230e300000000, 0xbc3139e700000000, 0xa1cc8ce600000000, + 0x86cb52e400000000, 0x9b36e7e500000000, 0xf0fc1ef700000000, + 0xed01abf600000000, 0xca0675f400000000, 0xd7fbc0f500000000, + 0x8408c9f100000000, 0x99f57cf000000000, 0xbef2a2f200000000, + 0xa30f17f300000000, 0x1814b1fa00000000, 0x05e904fb00000000, + 0x22eedaf900000000, 0x3f136ff800000000, 0x6ce066fc00000000, + 0x711dd3fd00000000, 0x561a0dff00000000, 0x4be7b8fe00000000, + 0xc0c981b700000000, 0xdd3434b600000000, 0xfa33eab400000000, + 0xe7ce5fb500000000, 0xb43d56b100000000, 0xa9c0e3b000000000, + 0x8ec73db200000000, 0x933a88b300000000, 0x28212eba00000000, + 0x35dc9bbb00000000, 0x12db45b900000000, 0x0f26f0b800000000, + 0x5cd5f9bc00000000, 0x41284cbd00000000, 0x662f92bf00000000, + 0x7bd227be00000000, 0x1018deac00000000, 0x0de56bad00000000, + 0x2ae2b5af00000000, 0x371f00ae00000000, 0x64ec09aa00000000, + 0x7911bcab00000000, 0x5e1662a900000000, 0x43ebd7a800000000, + 0xf8f071a100000000, 0xe50dc4a000000000, 0xc20a1aa200000000, + 0xdff7afa300000000, 0x8c04a6a700000000, 0x91f913a600000000, + 0xb6fecda400000000, 0xab0378a500000000, 0x606a3e8100000000, + 0x7d978b8000000000, 0x5a90558200000000, 0x476de08300000000, + 0x149ee98700000000, 0x09635c8600000000, 0x2e64828400000000, + 0x3399378500000000, 0x8882918c00000000, 0x957f248d00000000, + 0xb278fa8f00000000, 0xaf854f8e00000000, 0xfc76468a00000000, + 0xe18bf38b00000000, 0xc68c2d8900000000, 0xdb71988800000000, + 0xb0bb619a00000000, 0xad46d49b00000000, 0x8a410a9900000000, + 0x97bcbf9800000000, 0xc44fb69c00000000, 0xd9b2039d00000000, + 0xfeb5dd9f00000000, 0xe348689e00000000, 0x5853ce9700000000, + 0x45ae7b9600000000, 0x62a9a59400000000, 0x7f54109500000000, + 0x2ca7199100000000, 0x315aac9000000000, 0x165d729200000000, + 0x0ba0c79300000000}, + {0x0000000000000000, 0x24d9076300000000, 0x48b20fc600000000, + 0x6c6b08a500000000, 0xd1626e5700000000, 0xf5bb693400000000, + 0x99d0619100000000, 0xbd0966f200000000, 0xa2c5dcae00000000, + 0x861cdbcd00000000, 0xea77d36800000000, 0xceaed40b00000000, + 0x73a7b2f900000000, 0x577eb59a00000000, 0x3b15bd3f00000000, + 0x1fccba5c00000000, 0x058dc88600000000, 0x2154cfe500000000, + 0x4d3fc74000000000, 0x69e6c02300000000, 0xd4efa6d100000000, + 0xf036a1b200000000, 0x9c5da91700000000, 0xb884ae7400000000, + 0xa748142800000000, 0x8391134b00000000, 0xeffa1bee00000000, + 0xcb231c8d00000000, 0x762a7a7f00000000, 0x52f37d1c00000000, + 0x3e9875b900000000, 0x1a4172da00000000, 0x4b1ce0d600000000, + 0x6fc5e7b500000000, 0x03aeef1000000000, 0x2777e87300000000, + 0x9a7e8e8100000000, 0xbea789e200000000, 0xd2cc814700000000, + 0xf615862400000000, 0xe9d93c7800000000, 0xcd003b1b00000000, + 0xa16b33be00000000, 0x85b234dd00000000, 0x38bb522f00000000, + 0x1c62554c00000000, 0x70095de900000000, 0x54d05a8a00000000, + 0x4e91285000000000, 0x6a482f3300000000, 0x0623279600000000, + 0x22fa20f500000000, 0x9ff3460700000000, 0xbb2a416400000000, + 0xd74149c100000000, 0xf3984ea200000000, 0xec54f4fe00000000, + 0xc88df39d00000000, 0xa4e6fb3800000000, 0x803ffc5b00000000, + 0x3d369aa900000000, 0x19ef9dca00000000, 0x7584956f00000000, + 0x515d920c00000000, 0xd73eb17600000000, 0xf3e7b61500000000, + 0x9f8cbeb000000000, 0xbb55b9d300000000, 0x065cdf2100000000, + 0x2285d84200000000, 0x4eeed0e700000000, 0x6a37d78400000000, + 0x75fb6dd800000000, 0x51226abb00000000, 0x3d49621e00000000, + 0x1990657d00000000, 0xa499038f00000000, 0x804004ec00000000, + 0xec2b0c4900000000, 0xc8f20b2a00000000, 0xd2b379f000000000, + 0xf66a7e9300000000, 0x9a01763600000000, 0xbed8715500000000, + 0x03d117a700000000, 0x270810c400000000, 0x4b63186100000000, + 0x6fba1f0200000000, 0x7076a55e00000000, 0x54afa23d00000000, + 0x38c4aa9800000000, 0x1c1dadfb00000000, 0xa114cb0900000000, + 0x85cdcc6a00000000, 0xe9a6c4cf00000000, 0xcd7fc3ac00000000, + 0x9c2251a000000000, 0xb8fb56c300000000, 0xd4905e6600000000, + 0xf049590500000000, 0x4d403ff700000000, 0x6999389400000000, + 0x05f2303100000000, 0x212b375200000000, 0x3ee78d0e00000000, + 0x1a3e8a6d00000000, 0x765582c800000000, 0x528c85ab00000000, + 0xef85e35900000000, 0xcb5ce43a00000000, 0xa737ec9f00000000, + 0x83eeebfc00000000, 0x99af992600000000, 0xbd769e4500000000, + 0xd11d96e000000000, 0xf5c4918300000000, 0x48cdf77100000000, + 0x6c14f01200000000, 0x007ff8b700000000, 0x24a6ffd400000000, + 0x3b6a458800000000, 0x1fb342eb00000000, 0x73d84a4e00000000, + 0x57014d2d00000000, 0xea082bdf00000000, 0xced12cbc00000000, + 0xa2ba241900000000, 0x8663237a00000000, 0xae7d62ed00000000, + 0x8aa4658e00000000, 0xe6cf6d2b00000000, 0xc2166a4800000000, + 0x7f1f0cba00000000, 0x5bc60bd900000000, 0x37ad037c00000000, + 0x1374041f00000000, 0x0cb8be4300000000, 0x2861b92000000000, + 0x440ab18500000000, 0x60d3b6e600000000, 0xdddad01400000000, + 0xf903d77700000000, 0x9568dfd200000000, 0xb1b1d8b100000000, + 0xabf0aa6b00000000, 0x8f29ad0800000000, 0xe342a5ad00000000, + 0xc79ba2ce00000000, 0x7a92c43c00000000, 0x5e4bc35f00000000, + 0x3220cbfa00000000, 0x16f9cc9900000000, 0x093576c500000000, + 0x2dec71a600000000, 0x4187790300000000, 0x655e7e6000000000, + 0xd857189200000000, 0xfc8e1ff100000000, 0x90e5175400000000, + 0xb43c103700000000, 0xe561823b00000000, 0xc1b8855800000000, + 0xadd38dfd00000000, 0x890a8a9e00000000, 0x3403ec6c00000000, + 0x10daeb0f00000000, 0x7cb1e3aa00000000, 0x5868e4c900000000, + 0x47a45e9500000000, 0x637d59f600000000, 0x0f16515300000000, + 0x2bcf563000000000, 0x96c630c200000000, 0xb21f37a100000000, + 0xde743f0400000000, 0xfaad386700000000, 0xe0ec4abd00000000, + 0xc4354dde00000000, 0xa85e457b00000000, 0x8c87421800000000, + 0x318e24ea00000000, 0x1557238900000000, 0x793c2b2c00000000, + 0x5de52c4f00000000, 0x4229961300000000, 0x66f0917000000000, + 0x0a9b99d500000000, 0x2e429eb600000000, 0x934bf84400000000, + 0xb792ff2700000000, 0xdbf9f78200000000, 0xff20f0e100000000, + 0x7943d39b00000000, 0x5d9ad4f800000000, 0x31f1dc5d00000000, + 0x1528db3e00000000, 0xa821bdcc00000000, 0x8cf8baaf00000000, + 0xe093b20a00000000, 0xc44ab56900000000, 0xdb860f3500000000, + 0xff5f085600000000, 0x933400f300000000, 0xb7ed079000000000, + 0x0ae4616200000000, 0x2e3d660100000000, 0x42566ea400000000, + 0x668f69c700000000, 0x7cce1b1d00000000, 0x58171c7e00000000, + 0x347c14db00000000, 0x10a513b800000000, 0xadac754a00000000, + 0x8975722900000000, 0xe51e7a8c00000000, 0xc1c77def00000000, + 0xde0bc7b300000000, 0xfad2c0d000000000, 0x96b9c87500000000, + 0xb260cf1600000000, 0x0f69a9e400000000, 0x2bb0ae8700000000, + 0x47dba62200000000, 0x6302a14100000000, 0x325f334d00000000, + 0x1686342e00000000, 0x7aed3c8b00000000, 0x5e343be800000000, + 0xe33d5d1a00000000, 0xc7e45a7900000000, 0xab8f52dc00000000, + 0x8f5655bf00000000, 0x909aefe300000000, 0xb443e88000000000, + 0xd828e02500000000, 0xfcf1e74600000000, 0x41f881b400000000, + 0x652186d700000000, 0x094a8e7200000000, 0x2d93891100000000, + 0x37d2fbcb00000000, 0x130bfca800000000, 0x7f60f40d00000000, + 0x5bb9f36e00000000, 0xe6b0959c00000000, 0xc26992ff00000000, + 0xae029a5a00000000, 0x8adb9d3900000000, 0x9517276500000000, + 0xb1ce200600000000, 0xdda528a300000000, 0xf97c2fc000000000, + 0x4475493200000000, 0x60ac4e5100000000, 0x0cc746f400000000, + 0x281e419700000000}, + {0x0000000000000000, 0x08e3603c00000000, 0x10c6c17800000000, + 0x1825a14400000000, 0x208c83f100000000, 0x286fe3cd00000000, + 0x304a428900000000, 0x38a922b500000000, 0x011e763800000000, + 0x09fd160400000000, 0x11d8b74000000000, 0x193bd77c00000000, + 0x2192f5c900000000, 0x297195f500000000, 0x315434b100000000, + 0x39b7548d00000000, 0x023cec7000000000, 0x0adf8c4c00000000, + 0x12fa2d0800000000, 0x1a194d3400000000, 0x22b06f8100000000, + 0x2a530fbd00000000, 0x3276aef900000000, 0x3a95cec500000000, + 0x03229a4800000000, 0x0bc1fa7400000000, 0x13e45b3000000000, + 0x1b073b0c00000000, 0x23ae19b900000000, 0x2b4d798500000000, + 0x3368d8c100000000, 0x3b8bb8fd00000000, 0x0478d8e100000000, + 0x0c9bb8dd00000000, 0x14be199900000000, 0x1c5d79a500000000, + 0x24f45b1000000000, 0x2c173b2c00000000, 0x34329a6800000000, + 0x3cd1fa5400000000, 0x0566aed900000000, 0x0d85cee500000000, + 0x15a06fa100000000, 0x1d430f9d00000000, 0x25ea2d2800000000, + 0x2d094d1400000000, 0x352cec5000000000, 0x3dcf8c6c00000000, + 0x0644349100000000, 0x0ea754ad00000000, 0x1682f5e900000000, + 0x1e6195d500000000, 0x26c8b76000000000, 0x2e2bd75c00000000, + 0x360e761800000000, 0x3eed162400000000, 0x075a42a900000000, + 0x0fb9229500000000, 0x179c83d100000000, 0x1f7fe3ed00000000, + 0x27d6c15800000000, 0x2f35a16400000000, 0x3710002000000000, + 0x3ff3601c00000000, 0x49f6c11800000000, 0x4115a12400000000, + 0x5930006000000000, 0x51d3605c00000000, 0x697a42e900000000, + 0x619922d500000000, 0x79bc839100000000, 0x715fe3ad00000000, + 0x48e8b72000000000, 0x400bd71c00000000, 0x582e765800000000, + 0x50cd166400000000, 0x686434d100000000, 0x608754ed00000000, + 0x78a2f5a900000000, 0x7041959500000000, 0x4bca2d6800000000, + 0x43294d5400000000, 0x5b0cec1000000000, 0x53ef8c2c00000000, + 0x6b46ae9900000000, 0x63a5cea500000000, 0x7b806fe100000000, + 0x73630fdd00000000, 0x4ad45b5000000000, 0x42373b6c00000000, + 0x5a129a2800000000, 0x52f1fa1400000000, 0x6a58d8a100000000, + 0x62bbb89d00000000, 0x7a9e19d900000000, 0x727d79e500000000, + 0x4d8e19f900000000, 0x456d79c500000000, 0x5d48d88100000000, + 0x55abb8bd00000000, 0x6d029a0800000000, 0x65e1fa3400000000, + 0x7dc45b7000000000, 0x75273b4c00000000, 0x4c906fc100000000, + 0x44730ffd00000000, 0x5c56aeb900000000, 0x54b5ce8500000000, + 0x6c1cec3000000000, 0x64ff8c0c00000000, 0x7cda2d4800000000, + 0x74394d7400000000, 0x4fb2f58900000000, 0x475195b500000000, + 0x5f7434f100000000, 0x579754cd00000000, 0x6f3e767800000000, + 0x67dd164400000000, 0x7ff8b70000000000, 0x771bd73c00000000, + 0x4eac83b100000000, 0x464fe38d00000000, 0x5e6a42c900000000, + 0x568922f500000000, 0x6e20004000000000, 0x66c3607c00000000, + 0x7ee6c13800000000, 0x7605a10400000000, 0x92ec833100000000, + 0x9a0fe30d00000000, 0x822a424900000000, 0x8ac9227500000000, + 0xb26000c000000000, 0xba8360fc00000000, 0xa2a6c1b800000000, + 0xaa45a18400000000, 0x93f2f50900000000, 0x9b11953500000000, + 0x8334347100000000, 0x8bd7544d00000000, 0xb37e76f800000000, + 0xbb9d16c400000000, 0xa3b8b78000000000, 0xab5bd7bc00000000, + 0x90d06f4100000000, 0x98330f7d00000000, 0x8016ae3900000000, + 0x88f5ce0500000000, 0xb05cecb000000000, 0xb8bf8c8c00000000, + 0xa09a2dc800000000, 0xa8794df400000000, 0x91ce197900000000, + 0x992d794500000000, 0x8108d80100000000, 0x89ebb83d00000000, + 0xb1429a8800000000, 0xb9a1fab400000000, 0xa1845bf000000000, + 0xa9673bcc00000000, 0x96945bd000000000, 0x9e773bec00000000, + 0x86529aa800000000, 0x8eb1fa9400000000, 0xb618d82100000000, + 0xbefbb81d00000000, 0xa6de195900000000, 0xae3d796500000000, + 0x978a2de800000000, 0x9f694dd400000000, 0x874cec9000000000, + 0x8faf8cac00000000, 0xb706ae1900000000, 0xbfe5ce2500000000, + 0xa7c06f6100000000, 0xaf230f5d00000000, 0x94a8b7a000000000, + 0x9c4bd79c00000000, 0x846e76d800000000, 0x8c8d16e400000000, + 0xb424345100000000, 0xbcc7546d00000000, 0xa4e2f52900000000, + 0xac01951500000000, 0x95b6c19800000000, 0x9d55a1a400000000, + 0x857000e000000000, 0x8d9360dc00000000, 0xb53a426900000000, + 0xbdd9225500000000, 0xa5fc831100000000, 0xad1fe32d00000000, + 0xdb1a422900000000, 0xd3f9221500000000, 0xcbdc835100000000, + 0xc33fe36d00000000, 0xfb96c1d800000000, 0xf375a1e400000000, + 0xeb5000a000000000, 0xe3b3609c00000000, 0xda04341100000000, + 0xd2e7542d00000000, 0xcac2f56900000000, 0xc221955500000000, + 0xfa88b7e000000000, 0xf26bd7dc00000000, 0xea4e769800000000, + 0xe2ad16a400000000, 0xd926ae5900000000, 0xd1c5ce6500000000, + 0xc9e06f2100000000, 0xc1030f1d00000000, 0xf9aa2da800000000, + 0xf1494d9400000000, 0xe96cecd000000000, 0xe18f8cec00000000, + 0xd838d86100000000, 0xd0dbb85d00000000, 0xc8fe191900000000, + 0xc01d792500000000, 0xf8b45b9000000000, 0xf0573bac00000000, + 0xe8729ae800000000, 0xe091fad400000000, 0xdf629ac800000000, + 0xd781faf400000000, 0xcfa45bb000000000, 0xc7473b8c00000000, + 0xffee193900000000, 0xf70d790500000000, 0xef28d84100000000, + 0xe7cbb87d00000000, 0xde7cecf000000000, 0xd69f8ccc00000000, + 0xceba2d8800000000, 0xc6594db400000000, 0xfef06f0100000000, + 0xf6130f3d00000000, 0xee36ae7900000000, 0xe6d5ce4500000000, + 0xdd5e76b800000000, 0xd5bd168400000000, 0xcd98b7c000000000, + 0xc57bd7fc00000000, 0xfdd2f54900000000, 0xf531957500000000, + 0xed14343100000000, 0xe5f7540d00000000, 0xdc40008000000000, + 0xd4a360bc00000000, 0xcc86c1f800000000, 0xc465a1c400000000, + 0xfccc837100000000, 0xf42fe34d00000000, 0xec0a420900000000, + 0xe4e9223500000000}, + {0x0000000000000000, 0xd1e8e70e00000000, 0xa2d1cf1d00000000, + 0x7339281300000000, 0x44a39f3b00000000, 0x954b783500000000, + 0xe672502600000000, 0x379ab72800000000, 0x88463f7700000000, + 0x59aed87900000000, 0x2a97f06a00000000, 0xfb7f176400000000, + 0xcce5a04c00000000, 0x1d0d474200000000, 0x6e346f5100000000, + 0xbfdc885f00000000, 0x108d7eee00000000, 0xc16599e000000000, + 0xb25cb1f300000000, 0x63b456fd00000000, 0x542ee1d500000000, + 0x85c606db00000000, 0xf6ff2ec800000000, 0x2717c9c600000000, + 0x98cb419900000000, 0x4923a69700000000, 0x3a1a8e8400000000, + 0xebf2698a00000000, 0xdc68dea200000000, 0x0d8039ac00000000, + 0x7eb911bf00000000, 0xaf51f6b100000000, 0x611c8c0700000000, + 0xb0f46b0900000000, 0xc3cd431a00000000, 0x1225a41400000000, + 0x25bf133c00000000, 0xf457f43200000000, 0x876edc2100000000, + 0x56863b2f00000000, 0xe95ab37000000000, 0x38b2547e00000000, + 0x4b8b7c6d00000000, 0x9a639b6300000000, 0xadf92c4b00000000, + 0x7c11cb4500000000, 0x0f28e35600000000, 0xdec0045800000000, + 0x7191f2e900000000, 0xa07915e700000000, 0xd3403df400000000, + 0x02a8dafa00000000, 0x35326dd200000000, 0xe4da8adc00000000, + 0x97e3a2cf00000000, 0x460b45c100000000, 0xf9d7cd9e00000000, + 0x283f2a9000000000, 0x5b06028300000000, 0x8aeee58d00000000, + 0xbd7452a500000000, 0x6c9cb5ab00000000, 0x1fa59db800000000, + 0xce4d7ab600000000, 0xc238180f00000000, 0x13d0ff0100000000, + 0x60e9d71200000000, 0xb101301c00000000, 0x869b873400000000, + 0x5773603a00000000, 0x244a482900000000, 0xf5a2af2700000000, + 0x4a7e277800000000, 0x9b96c07600000000, 0xe8afe86500000000, + 0x39470f6b00000000, 0x0eddb84300000000, 0xdf355f4d00000000, + 0xac0c775e00000000, 0x7de4905000000000, 0xd2b566e100000000, + 0x035d81ef00000000, 0x7064a9fc00000000, 0xa18c4ef200000000, + 0x9616f9da00000000, 0x47fe1ed400000000, 0x34c736c700000000, + 0xe52fd1c900000000, 0x5af3599600000000, 0x8b1bbe9800000000, + 0xf822968b00000000, 0x29ca718500000000, 0x1e50c6ad00000000, + 0xcfb821a300000000, 0xbc8109b000000000, 0x6d69eebe00000000, + 0xa324940800000000, 0x72cc730600000000, 0x01f55b1500000000, + 0xd01dbc1b00000000, 0xe7870b3300000000, 0x366fec3d00000000, + 0x4556c42e00000000, 0x94be232000000000, 0x2b62ab7f00000000, + 0xfa8a4c7100000000, 0x89b3646200000000, 0x585b836c00000000, + 0x6fc1344400000000, 0xbe29d34a00000000, 0xcd10fb5900000000, + 0x1cf81c5700000000, 0xb3a9eae600000000, 0x62410de800000000, + 0x117825fb00000000, 0xc090c2f500000000, 0xf70a75dd00000000, + 0x26e292d300000000, 0x55dbbac000000000, 0x84335dce00000000, + 0x3befd59100000000, 0xea07329f00000000, 0x993e1a8c00000000, + 0x48d6fd8200000000, 0x7f4c4aaa00000000, 0xaea4ada400000000, + 0xdd9d85b700000000, 0x0c7562b900000000, 0x8471301e00000000, + 0x5599d71000000000, 0x26a0ff0300000000, 0xf748180d00000000, + 0xc0d2af2500000000, 0x113a482b00000000, 0x6203603800000000, + 0xb3eb873600000000, 0x0c370f6900000000, 0xdddfe86700000000, + 0xaee6c07400000000, 0x7f0e277a00000000, 0x4894905200000000, + 0x997c775c00000000, 0xea455f4f00000000, 0x3badb84100000000, + 0x94fc4ef000000000, 0x4514a9fe00000000, 0x362d81ed00000000, + 0xe7c566e300000000, 0xd05fd1cb00000000, 0x01b736c500000000, + 0x728e1ed600000000, 0xa366f9d800000000, 0x1cba718700000000, + 0xcd52968900000000, 0xbe6bbe9a00000000, 0x6f83599400000000, + 0x5819eebc00000000, 0x89f109b200000000, 0xfac821a100000000, + 0x2b20c6af00000000, 0xe56dbc1900000000, 0x34855b1700000000, + 0x47bc730400000000, 0x9654940a00000000, 0xa1ce232200000000, + 0x7026c42c00000000, 0x031fec3f00000000, 0xd2f70b3100000000, + 0x6d2b836e00000000, 0xbcc3646000000000, 0xcffa4c7300000000, + 0x1e12ab7d00000000, 0x29881c5500000000, 0xf860fb5b00000000, + 0x8b59d34800000000, 0x5ab1344600000000, 0xf5e0c2f700000000, + 0x240825f900000000, 0x57310dea00000000, 0x86d9eae400000000, + 0xb1435dcc00000000, 0x60abbac200000000, 0x139292d100000000, + 0xc27a75df00000000, 0x7da6fd8000000000, 0xac4e1a8e00000000, + 0xdf77329d00000000, 0x0e9fd59300000000, 0x390562bb00000000, + 0xe8ed85b500000000, 0x9bd4ada600000000, 0x4a3c4aa800000000, + 0x4649281100000000, 0x97a1cf1f00000000, 0xe498e70c00000000, + 0x3570000200000000, 0x02eab72a00000000, 0xd302502400000000, + 0xa03b783700000000, 0x71d39f3900000000, 0xce0f176600000000, + 0x1fe7f06800000000, 0x6cded87b00000000, 0xbd363f7500000000, + 0x8aac885d00000000, 0x5b446f5300000000, 0x287d474000000000, + 0xf995a04e00000000, 0x56c456ff00000000, 0x872cb1f100000000, + 0xf41599e200000000, 0x25fd7eec00000000, 0x1267c9c400000000, + 0xc38f2eca00000000, 0xb0b606d900000000, 0x615ee1d700000000, + 0xde82698800000000, 0x0f6a8e8600000000, 0x7c53a69500000000, + 0xadbb419b00000000, 0x9a21f6b300000000, 0x4bc911bd00000000, + 0x38f039ae00000000, 0xe918dea000000000, 0x2755a41600000000, + 0xf6bd431800000000, 0x85846b0b00000000, 0x546c8c0500000000, + 0x63f63b2d00000000, 0xb21edc2300000000, 0xc127f43000000000, + 0x10cf133e00000000, 0xaf139b6100000000, 0x7efb7c6f00000000, + 0x0dc2547c00000000, 0xdc2ab37200000000, 0xebb0045a00000000, + 0x3a58e35400000000, 0x4961cb4700000000, 0x98892c4900000000, + 0x37d8daf800000000, 0xe6303df600000000, 0x950915e500000000, + 0x44e1f2eb00000000, 0x737b45c300000000, 0xa293a2cd00000000, + 0xd1aa8ade00000000, 0x00426dd000000000, 0xbf9ee58f00000000, + 0x6e76028100000000, 0x1d4f2a9200000000, 0xcca7cd9c00000000, + 0xfb3d7ab400000000, 0x2ad59dba00000000, 0x59ecb5a900000000, + 0x880452a700000000}, + {0x0000000000000000, 0xaa05daf100000000, 0x150dc53800000000, + 0xbf081fc900000000, 0x2a1a8a7100000000, 0x801f508000000000, + 0x3f174f4900000000, 0x951295b800000000, 0x543414e300000000, + 0xfe31ce1200000000, 0x4139d1db00000000, 0xeb3c0b2a00000000, + 0x7e2e9e9200000000, 0xd42b446300000000, 0x6b235baa00000000, + 0xc126815b00000000, 0xe96e591d00000000, 0x436b83ec00000000, + 0xfc639c2500000000, 0x566646d400000000, 0xc374d36c00000000, + 0x6971099d00000000, 0xd679165400000000, 0x7c7ccca500000000, + 0xbd5a4dfe00000000, 0x175f970f00000000, 0xa85788c600000000, + 0x0252523700000000, 0x9740c78f00000000, 0x3d451d7e00000000, + 0x824d02b700000000, 0x2848d84600000000, 0xd2ddb23a00000000, + 0x78d868cb00000000, 0xc7d0770200000000, 0x6dd5adf300000000, + 0xf8c7384b00000000, 0x52c2e2ba00000000, 0xedcafd7300000000, + 0x47cf278200000000, 0x86e9a6d900000000, 0x2cec7c2800000000, + 0x93e463e100000000, 0x39e1b91000000000, 0xacf32ca800000000, + 0x06f6f65900000000, 0xb9fee99000000000, 0x13fb336100000000, + 0x3bb3eb2700000000, 0x91b631d600000000, 0x2ebe2e1f00000000, + 0x84bbf4ee00000000, 0x11a9615600000000, 0xbbacbba700000000, + 0x04a4a46e00000000, 0xaea17e9f00000000, 0x6f87ffc400000000, + 0xc582253500000000, 0x7a8a3afc00000000, 0xd08fe00d00000000, + 0x459d75b500000000, 0xef98af4400000000, 0x5090b08d00000000, + 0xfa956a7c00000000, 0xa4bb657500000000, 0x0ebebf8400000000, + 0xb1b6a04d00000000, 0x1bb37abc00000000, 0x8ea1ef0400000000, + 0x24a435f500000000, 0x9bac2a3c00000000, 0x31a9f0cd00000000, + 0xf08f719600000000, 0x5a8aab6700000000, 0xe582b4ae00000000, + 0x4f876e5f00000000, 0xda95fbe700000000, 0x7090211600000000, + 0xcf983edf00000000, 0x659de42e00000000, 0x4dd53c6800000000, + 0xe7d0e69900000000, 0x58d8f95000000000, 0xf2dd23a100000000, + 0x67cfb61900000000, 0xcdca6ce800000000, 0x72c2732100000000, + 0xd8c7a9d000000000, 0x19e1288b00000000, 0xb3e4f27a00000000, + 0x0cecedb300000000, 0xa6e9374200000000, 0x33fba2fa00000000, + 0x99fe780b00000000, 0x26f667c200000000, 0x8cf3bd3300000000, + 0x7666d74f00000000, 0xdc630dbe00000000, 0x636b127700000000, + 0xc96ec88600000000, 0x5c7c5d3e00000000, 0xf67987cf00000000, + 0x4971980600000000, 0xe37442f700000000, 0x2252c3ac00000000, + 0x8857195d00000000, 0x375f069400000000, 0x9d5adc6500000000, + 0x084849dd00000000, 0xa24d932c00000000, 0x1d458ce500000000, + 0xb740561400000000, 0x9f088e5200000000, 0x350d54a300000000, + 0x8a054b6a00000000, 0x2000919b00000000, 0xb512042300000000, + 0x1f17ded200000000, 0xa01fc11b00000000, 0x0a1a1bea00000000, + 0xcb3c9ab100000000, 0x6139404000000000, 0xde315f8900000000, + 0x7434857800000000, 0xe12610c000000000, 0x4b23ca3100000000, + 0xf42bd5f800000000, 0x5e2e0f0900000000, 0x4877cbea00000000, + 0xe272111b00000000, 0x5d7a0ed200000000, 0xf77fd42300000000, + 0x626d419b00000000, 0xc8689b6a00000000, 0x776084a300000000, + 0xdd655e5200000000, 0x1c43df0900000000, 0xb64605f800000000, + 0x094e1a3100000000, 0xa34bc0c000000000, 0x3659557800000000, + 0x9c5c8f8900000000, 0x2354904000000000, 0x89514ab100000000, + 0xa11992f700000000, 0x0b1c480600000000, 0xb41457cf00000000, + 0x1e118d3e00000000, 0x8b03188600000000, 0x2106c27700000000, + 0x9e0eddbe00000000, 0x340b074f00000000, 0xf52d861400000000, + 0x5f285ce500000000, 0xe020432c00000000, 0x4a2599dd00000000, + 0xdf370c6500000000, 0x7532d69400000000, 0xca3ac95d00000000, + 0x603f13ac00000000, 0x9aaa79d000000000, 0x30afa32100000000, + 0x8fa7bce800000000, 0x25a2661900000000, 0xb0b0f3a100000000, + 0x1ab5295000000000, 0xa5bd369900000000, 0x0fb8ec6800000000, + 0xce9e6d3300000000, 0x649bb7c200000000, 0xdb93a80b00000000, + 0x719672fa00000000, 0xe484e74200000000, 0x4e813db300000000, + 0xf189227a00000000, 0x5b8cf88b00000000, 0x73c420cd00000000, + 0xd9c1fa3c00000000, 0x66c9e5f500000000, 0xcccc3f0400000000, + 0x59deaabc00000000, 0xf3db704d00000000, 0x4cd36f8400000000, + 0xe6d6b57500000000, 0x27f0342e00000000, 0x8df5eedf00000000, + 0x32fdf11600000000, 0x98f82be700000000, 0x0deabe5f00000000, + 0xa7ef64ae00000000, 0x18e77b6700000000, 0xb2e2a19600000000, + 0xecccae9f00000000, 0x46c9746e00000000, 0xf9c16ba700000000, + 0x53c4b15600000000, 0xc6d624ee00000000, 0x6cd3fe1f00000000, + 0xd3dbe1d600000000, 0x79de3b2700000000, 0xb8f8ba7c00000000, + 0x12fd608d00000000, 0xadf57f4400000000, 0x07f0a5b500000000, + 0x92e2300d00000000, 0x38e7eafc00000000, 0x87eff53500000000, + 0x2dea2fc400000000, 0x05a2f78200000000, 0xafa72d7300000000, + 0x10af32ba00000000, 0xbaaae84b00000000, 0x2fb87df300000000, + 0x85bda70200000000, 0x3ab5b8cb00000000, 0x90b0623a00000000, + 0x5196e36100000000, 0xfb93399000000000, 0x449b265900000000, + 0xee9efca800000000, 0x7b8c691000000000, 0xd189b3e100000000, + 0x6e81ac2800000000, 0xc48476d900000000, 0x3e111ca500000000, + 0x9414c65400000000, 0x2b1cd99d00000000, 0x8119036c00000000, + 0x140b96d400000000, 0xbe0e4c2500000000, 0x010653ec00000000, + 0xab03891d00000000, 0x6a25084600000000, 0xc020d2b700000000, + 0x7f28cd7e00000000, 0xd52d178f00000000, 0x403f823700000000, + 0xea3a58c600000000, 0x5532470f00000000, 0xff379dfe00000000, + 0xd77f45b800000000, 0x7d7a9f4900000000, 0xc272808000000000, + 0x68775a7100000000, 0xfd65cfc900000000, 0x5760153800000000, + 0xe8680af100000000, 0x426dd00000000000, 0x834b515b00000000, + 0x294e8baa00000000, 0x9646946300000000, 0x3c434e9200000000, + 0xa951db2a00000000, 0x035401db00000000, 0xbc5c1e1200000000, + 0x1659c4e300000000}}; + +#else /* W == 4 */ + +local const z_crc_t FAR crc_braid_table[][256] = { + {0x00000000, 0xae689191, 0x87a02563, 0x29c8b4f2, 0xd4314c87, + 0x7a59dd16, 0x539169e4, 0xfdf9f875, 0x73139f4f, 0xdd7b0ede, + 0xf4b3ba2c, 0x5adb2bbd, 0xa722d3c8, 0x094a4259, 0x2082f6ab, + 0x8eea673a, 0xe6273e9e, 0x484faf0f, 0x61871bfd, 0xcfef8a6c, + 0x32167219, 0x9c7ee388, 0xb5b6577a, 0x1bdec6eb, 0x9534a1d1, + 0x3b5c3040, 0x129484b2, 0xbcfc1523, 0x4105ed56, 0xef6d7cc7, + 0xc6a5c835, 0x68cd59a4, 0x173f7b7d, 0xb957eaec, 0x909f5e1e, + 0x3ef7cf8f, 0xc30e37fa, 0x6d66a66b, 0x44ae1299, 0xeac68308, + 0x642ce432, 0xca4475a3, 0xe38cc151, 0x4de450c0, 0xb01da8b5, + 0x1e753924, 0x37bd8dd6, 0x99d51c47, 0xf11845e3, 0x5f70d472, + 0x76b86080, 0xd8d0f111, 0x25290964, 0x8b4198f5, 0xa2892c07, + 0x0ce1bd96, 0x820bdaac, 0x2c634b3d, 0x05abffcf, 0xabc36e5e, + 0x563a962b, 0xf85207ba, 0xd19ab348, 0x7ff222d9, 0x2e7ef6fa, + 0x8016676b, 0xa9ded399, 0x07b64208, 0xfa4fba7d, 0x54272bec, + 0x7def9f1e, 0xd3870e8f, 0x5d6d69b5, 0xf305f824, 0xdacd4cd6, + 0x74a5dd47, 0x895c2532, 0x2734b4a3, 0x0efc0051, 0xa09491c0, + 0xc859c864, 0x663159f5, 0x4ff9ed07, 0xe1917c96, 0x1c6884e3, + 0xb2001572, 0x9bc8a180, 0x35a03011, 0xbb4a572b, 0x1522c6ba, + 0x3cea7248, 0x9282e3d9, 0x6f7b1bac, 0xc1138a3d, 0xe8db3ecf, + 0x46b3af5e, 0x39418d87, 0x97291c16, 0xbee1a8e4, 0x10893975, + 0xed70c100, 0x43185091, 0x6ad0e463, 0xc4b875f2, 0x4a5212c8, + 0xe43a8359, 0xcdf237ab, 0x639aa63a, 0x9e635e4f, 0x300bcfde, + 0x19c37b2c, 0xb7abeabd, 0xdf66b319, 0x710e2288, 0x58c6967a, + 0xf6ae07eb, 0x0b57ff9e, 0xa53f6e0f, 0x8cf7dafd, 0x229f4b6c, + 0xac752c56, 0x021dbdc7, 0x2bd50935, 0x85bd98a4, 0x784460d1, + 0xd62cf140, 0xffe445b2, 0x518cd423, 0x5cfdedf4, 0xf2957c65, + 0xdb5dc897, 0x75355906, 0x88cca173, 0x26a430e2, 0x0f6c8410, + 0xa1041581, 0x2fee72bb, 0x8186e32a, 0xa84e57d8, 0x0626c649, + 0xfbdf3e3c, 0x55b7afad, 0x7c7f1b5f, 0xd2178ace, 0xbadad36a, + 0x14b242fb, 0x3d7af609, 0x93126798, 0x6eeb9fed, 0xc0830e7c, + 0xe94bba8e, 0x47232b1f, 0xc9c94c25, 0x67a1ddb4, 0x4e696946, + 0xe001f8d7, 0x1df800a2, 0xb3909133, 0x9a5825c1, 0x3430b450, + 0x4bc29689, 0xe5aa0718, 0xcc62b3ea, 0x620a227b, 0x9ff3da0e, + 0x319b4b9f, 0x1853ff6d, 0xb63b6efc, 0x38d109c6, 0x96b99857, + 0xbf712ca5, 0x1119bd34, 0xece04541, 0x4288d4d0, 0x6b406022, + 0xc528f1b3, 0xade5a817, 0x038d3986, 0x2a458d74, 0x842d1ce5, + 0x79d4e490, 0xd7bc7501, 0xfe74c1f3, 0x501c5062, 0xdef63758, + 0x709ea6c9, 0x5956123b, 0xf73e83aa, 0x0ac77bdf, 0xa4afea4e, + 0x8d675ebc, 0x230fcf2d, 0x72831b0e, 0xdceb8a9f, 0xf5233e6d, + 0x5b4baffc, 0xa6b25789, 0x08dac618, 0x211272ea, 0x8f7ae37b, + 0x01908441, 0xaff815d0, 0x8630a122, 0x285830b3, 0xd5a1c8c6, + 0x7bc95957, 0x5201eda5, 0xfc697c34, 0x94a42590, 0x3accb401, + 0x130400f3, 0xbd6c9162, 0x40956917, 0xeefdf886, 0xc7354c74, + 0x695ddde5, 0xe7b7badf, 0x49df2b4e, 0x60179fbc, 0xce7f0e2d, + 0x3386f658, 0x9dee67c9, 0xb426d33b, 0x1a4e42aa, 0x65bc6073, + 0xcbd4f1e2, 0xe21c4510, 0x4c74d481, 0xb18d2cf4, 0x1fe5bd65, + 0x362d0997, 0x98459806, 0x16afff3c, 0xb8c76ead, 0x910fda5f, + 0x3f674bce, 0xc29eb3bb, 0x6cf6222a, 0x453e96d8, 0xeb560749, + 0x839b5eed, 0x2df3cf7c, 0x043b7b8e, 0xaa53ea1f, 0x57aa126a, + 0xf9c283fb, 0xd00a3709, 0x7e62a698, 0xf088c1a2, 0x5ee05033, + 0x7728e4c1, 0xd9407550, 0x24b98d25, 0x8ad11cb4, 0xa319a846, + 0x0d7139d7}, + {0x00000000, 0xb9fbdbe8, 0xa886b191, 0x117d6a79, 0x8a7c6563, + 0x3387be8b, 0x22fad4f2, 0x9b010f1a, 0xcf89cc87, 0x7672176f, + 0x670f7d16, 0xdef4a6fe, 0x45f5a9e4, 0xfc0e720c, 0xed731875, + 0x5488c39d, 0x44629f4f, 0xfd9944a7, 0xece42ede, 0x551ff536, + 0xce1efa2c, 0x77e521c4, 0x66984bbd, 0xdf639055, 0x8beb53c8, + 0x32108820, 0x236de259, 0x9a9639b1, 0x019736ab, 0xb86ced43, + 0xa911873a, 0x10ea5cd2, 0x88c53e9e, 0x313ee576, 0x20438f0f, + 0x99b854e7, 0x02b95bfd, 0xbb428015, 0xaa3fea6c, 0x13c43184, + 0x474cf219, 0xfeb729f1, 0xefca4388, 0x56319860, 0xcd30977a, + 0x74cb4c92, 0x65b626eb, 0xdc4dfd03, 0xcca7a1d1, 0x755c7a39, + 0x64211040, 0xdddacba8, 0x46dbc4b2, 0xff201f5a, 0xee5d7523, + 0x57a6aecb, 0x032e6d56, 0xbad5b6be, 0xaba8dcc7, 0x1253072f, + 0x89520835, 0x30a9d3dd, 0x21d4b9a4, 0x982f624c, 0xcafb7b7d, + 0x7300a095, 0x627dcaec, 0xdb861104, 0x40871e1e, 0xf97cc5f6, + 0xe801af8f, 0x51fa7467, 0x0572b7fa, 0xbc896c12, 0xadf4066b, + 0x140fdd83, 0x8f0ed299, 0x36f50971, 0x27886308, 0x9e73b8e0, + 0x8e99e432, 0x37623fda, 0x261f55a3, 0x9fe48e4b, 0x04e58151, + 0xbd1e5ab9, 0xac6330c0, 0x1598eb28, 0x411028b5, 0xf8ebf35d, + 0xe9969924, 0x506d42cc, 0xcb6c4dd6, 0x7297963e, 0x63eafc47, + 0xda1127af, 0x423e45e3, 0xfbc59e0b, 0xeab8f472, 0x53432f9a, + 0xc8422080, 0x71b9fb68, 0x60c49111, 0xd93f4af9, 0x8db78964, + 0x344c528c, 0x253138f5, 0x9ccae31d, 0x07cbec07, 0xbe3037ef, + 0xaf4d5d96, 0x16b6867e, 0x065cdaac, 0xbfa70144, 0xaeda6b3d, + 0x1721b0d5, 0x8c20bfcf, 0x35db6427, 0x24a60e5e, 0x9d5dd5b6, + 0xc9d5162b, 0x702ecdc3, 0x6153a7ba, 0xd8a87c52, 0x43a97348, + 0xfa52a8a0, 0xeb2fc2d9, 0x52d41931, 0x4e87f0bb, 0xf77c2b53, + 0xe601412a, 0x5ffa9ac2, 0xc4fb95d8, 0x7d004e30, 0x6c7d2449, + 0xd586ffa1, 0x810e3c3c, 0x38f5e7d4, 0x29888dad, 0x90735645, + 0x0b72595f, 0xb28982b7, 0xa3f4e8ce, 0x1a0f3326, 0x0ae56ff4, + 0xb31eb41c, 0xa263de65, 0x1b98058d, 0x80990a97, 0x3962d17f, + 0x281fbb06, 0x91e460ee, 0xc56ca373, 0x7c97789b, 0x6dea12e2, + 0xd411c90a, 0x4f10c610, 0xf6eb1df8, 0xe7967781, 0x5e6dac69, + 0xc642ce25, 0x7fb915cd, 0x6ec47fb4, 0xd73fa45c, 0x4c3eab46, + 0xf5c570ae, 0xe4b81ad7, 0x5d43c13f, 0x09cb02a2, 0xb030d94a, + 0xa14db333, 0x18b668db, 0x83b767c1, 0x3a4cbc29, 0x2b31d650, + 0x92ca0db8, 0x8220516a, 0x3bdb8a82, 0x2aa6e0fb, 0x935d3b13, + 0x085c3409, 0xb1a7efe1, 0xa0da8598, 0x19215e70, 0x4da99ded, + 0xf4524605, 0xe52f2c7c, 0x5cd4f794, 0xc7d5f88e, 0x7e2e2366, + 0x6f53491f, 0xd6a892f7, 0x847c8bc6, 0x3d87502e, 0x2cfa3a57, + 0x9501e1bf, 0x0e00eea5, 0xb7fb354d, 0xa6865f34, 0x1f7d84dc, + 0x4bf54741, 0xf20e9ca9, 0xe373f6d0, 0x5a882d38, 0xc1892222, + 0x7872f9ca, 0x690f93b3, 0xd0f4485b, 0xc01e1489, 0x79e5cf61, + 0x6898a518, 0xd1637ef0, 0x4a6271ea, 0xf399aa02, 0xe2e4c07b, + 0x5b1f1b93, 0x0f97d80e, 0xb66c03e6, 0xa711699f, 0x1eeab277, + 0x85ebbd6d, 0x3c106685, 0x2d6d0cfc, 0x9496d714, 0x0cb9b558, + 0xb5426eb0, 0xa43f04c9, 0x1dc4df21, 0x86c5d03b, 0x3f3e0bd3, + 0x2e4361aa, 0x97b8ba42, 0xc33079df, 0x7acba237, 0x6bb6c84e, + 0xd24d13a6, 0x494c1cbc, 0xf0b7c754, 0xe1caad2d, 0x583176c5, + 0x48db2a17, 0xf120f1ff, 0xe05d9b86, 0x59a6406e, 0xc2a74f74, + 0x7b5c949c, 0x6a21fee5, 0xd3da250d, 0x8752e690, 0x3ea93d78, + 0x2fd45701, 0x962f8ce9, 0x0d2e83f3, 0xb4d5581b, 0xa5a83262, + 0x1c53e98a}, + {0x00000000, 0x9d0fe176, 0xe16ec4ad, 0x7c6125db, 0x19ac8f1b, + 0x84a36e6d, 0xf8c24bb6, 0x65cdaac0, 0x33591e36, 0xae56ff40, + 0xd237da9b, 0x4f383bed, 0x2af5912d, 0xb7fa705b, 0xcb9b5580, + 0x5694b4f6, 0x66b23c6c, 0xfbbddd1a, 0x87dcf8c1, 0x1ad319b7, + 0x7f1eb377, 0xe2115201, 0x9e7077da, 0x037f96ac, 0x55eb225a, + 0xc8e4c32c, 0xb485e6f7, 0x298a0781, 0x4c47ad41, 0xd1484c37, + 0xad2969ec, 0x3026889a, 0xcd6478d8, 0x506b99ae, 0x2c0abc75, + 0xb1055d03, 0xd4c8f7c3, 0x49c716b5, 0x35a6336e, 0xa8a9d218, + 0xfe3d66ee, 0x63328798, 0x1f53a243, 0x825c4335, 0xe791e9f5, + 0x7a9e0883, 0x06ff2d58, 0x9bf0cc2e, 0xabd644b4, 0x36d9a5c2, + 0x4ab88019, 0xd7b7616f, 0xb27acbaf, 0x2f752ad9, 0x53140f02, + 0xce1bee74, 0x988f5a82, 0x0580bbf4, 0x79e19e2f, 0xe4ee7f59, + 0x8123d599, 0x1c2c34ef, 0x604d1134, 0xfd42f042, 0x41b9f7f1, + 0xdcb61687, 0xa0d7335c, 0x3dd8d22a, 0x581578ea, 0xc51a999c, + 0xb97bbc47, 0x24745d31, 0x72e0e9c7, 0xefef08b1, 0x938e2d6a, + 0x0e81cc1c, 0x6b4c66dc, 0xf64387aa, 0x8a22a271, 0x172d4307, + 0x270bcb9d, 0xba042aeb, 0xc6650f30, 0x5b6aee46, 0x3ea74486, + 0xa3a8a5f0, 0xdfc9802b, 0x42c6615d, 0x1452d5ab, 0x895d34dd, + 0xf53c1106, 0x6833f070, 0x0dfe5ab0, 0x90f1bbc6, 0xec909e1d, + 0x719f7f6b, 0x8cdd8f29, 0x11d26e5f, 0x6db34b84, 0xf0bcaaf2, + 0x95710032, 0x087ee144, 0x741fc49f, 0xe91025e9, 0xbf84911f, + 0x228b7069, 0x5eea55b2, 0xc3e5b4c4, 0xa6281e04, 0x3b27ff72, + 0x4746daa9, 0xda493bdf, 0xea6fb345, 0x77605233, 0x0b0177e8, + 0x960e969e, 0xf3c33c5e, 0x6eccdd28, 0x12adf8f3, 0x8fa21985, + 0xd936ad73, 0x44394c05, 0x385869de, 0xa55788a8, 0xc09a2268, + 0x5d95c31e, 0x21f4e6c5, 0xbcfb07b3, 0x8373efe2, 0x1e7c0e94, + 0x621d2b4f, 0xff12ca39, 0x9adf60f9, 0x07d0818f, 0x7bb1a454, + 0xe6be4522, 0xb02af1d4, 0x2d2510a2, 0x51443579, 0xcc4bd40f, + 0xa9867ecf, 0x34899fb9, 0x48e8ba62, 0xd5e75b14, 0xe5c1d38e, + 0x78ce32f8, 0x04af1723, 0x99a0f655, 0xfc6d5c95, 0x6162bde3, + 0x1d039838, 0x800c794e, 0xd698cdb8, 0x4b972cce, 0x37f60915, + 0xaaf9e863, 0xcf3442a3, 0x523ba3d5, 0x2e5a860e, 0xb3556778, + 0x4e17973a, 0xd318764c, 0xaf795397, 0x3276b2e1, 0x57bb1821, + 0xcab4f957, 0xb6d5dc8c, 0x2bda3dfa, 0x7d4e890c, 0xe041687a, + 0x9c204da1, 0x012facd7, 0x64e20617, 0xf9ede761, 0x858cc2ba, + 0x188323cc, 0x28a5ab56, 0xb5aa4a20, 0xc9cb6ffb, 0x54c48e8d, + 0x3109244d, 0xac06c53b, 0xd067e0e0, 0x4d680196, 0x1bfcb560, + 0x86f35416, 0xfa9271cd, 0x679d90bb, 0x02503a7b, 0x9f5fdb0d, + 0xe33efed6, 0x7e311fa0, 0xc2ca1813, 0x5fc5f965, 0x23a4dcbe, + 0xbeab3dc8, 0xdb669708, 0x4669767e, 0x3a0853a5, 0xa707b2d3, + 0xf1930625, 0x6c9ce753, 0x10fdc288, 0x8df223fe, 0xe83f893e, + 0x75306848, 0x09514d93, 0x945eace5, 0xa478247f, 0x3977c509, + 0x4516e0d2, 0xd81901a4, 0xbdd4ab64, 0x20db4a12, 0x5cba6fc9, + 0xc1b58ebf, 0x97213a49, 0x0a2edb3f, 0x764ffee4, 0xeb401f92, + 0x8e8db552, 0x13825424, 0x6fe371ff, 0xf2ec9089, 0x0fae60cb, + 0x92a181bd, 0xeec0a466, 0x73cf4510, 0x1602efd0, 0x8b0d0ea6, + 0xf76c2b7d, 0x6a63ca0b, 0x3cf77efd, 0xa1f89f8b, 0xdd99ba50, + 0x40965b26, 0x255bf1e6, 0xb8541090, 0xc435354b, 0x593ad43d, + 0x691c5ca7, 0xf413bdd1, 0x8872980a, 0x157d797c, 0x70b0d3bc, + 0xedbf32ca, 0x91de1711, 0x0cd1f667, 0x5a454291, 0xc74aa3e7, + 0xbb2b863c, 0x2624674a, 0x43e9cd8a, 0xdee62cfc, 0xa2870927, + 0x3f88e851}, + {0x00000000, 0xdd96d985, 0x605cb54b, 0xbdca6cce, 0xc0b96a96, + 0x1d2fb313, 0xa0e5dfdd, 0x7d730658, 0x5a03d36d, 0x87950ae8, + 0x3a5f6626, 0xe7c9bfa3, 0x9abab9fb, 0x472c607e, 0xfae60cb0, + 0x2770d535, 0xb407a6da, 0x69917f5f, 0xd45b1391, 0x09cdca14, + 0x74becc4c, 0xa92815c9, 0x14e27907, 0xc974a082, 0xee0475b7, + 0x3392ac32, 0x8e58c0fc, 0x53ce1979, 0x2ebd1f21, 0xf32bc6a4, + 0x4ee1aa6a, 0x937773ef, 0xb37e4bf5, 0x6ee89270, 0xd322febe, + 0x0eb4273b, 0x73c72163, 0xae51f8e6, 0x139b9428, 0xce0d4dad, + 0xe97d9898, 0x34eb411d, 0x89212dd3, 0x54b7f456, 0x29c4f20e, + 0xf4522b8b, 0x49984745, 0x940e9ec0, 0x0779ed2f, 0xdaef34aa, + 0x67255864, 0xbab381e1, 0xc7c087b9, 0x1a565e3c, 0xa79c32f2, + 0x7a0aeb77, 0x5d7a3e42, 0x80ece7c7, 0x3d268b09, 0xe0b0528c, + 0x9dc354d4, 0x40558d51, 0xfd9fe19f, 0x2009381a, 0xbd8d91ab, + 0x601b482e, 0xddd124e0, 0x0047fd65, 0x7d34fb3d, 0xa0a222b8, + 0x1d684e76, 0xc0fe97f3, 0xe78e42c6, 0x3a189b43, 0x87d2f78d, + 0x5a442e08, 0x27372850, 0xfaa1f1d5, 0x476b9d1b, 0x9afd449e, + 0x098a3771, 0xd41ceef4, 0x69d6823a, 0xb4405bbf, 0xc9335de7, + 0x14a58462, 0xa96fe8ac, 0x74f93129, 0x5389e41c, 0x8e1f3d99, + 0x33d55157, 0xee4388d2, 0x93308e8a, 0x4ea6570f, 0xf36c3bc1, + 0x2efae244, 0x0ef3da5e, 0xd36503db, 0x6eaf6f15, 0xb339b690, + 0xce4ab0c8, 0x13dc694d, 0xae160583, 0x7380dc06, 0x54f00933, + 0x8966d0b6, 0x34acbc78, 0xe93a65fd, 0x944963a5, 0x49dfba20, + 0xf415d6ee, 0x29830f6b, 0xbaf47c84, 0x6762a501, 0xdaa8c9cf, + 0x073e104a, 0x7a4d1612, 0xa7dbcf97, 0x1a11a359, 0xc7877adc, + 0xe0f7afe9, 0x3d61766c, 0x80ab1aa2, 0x5d3dc327, 0x204ec57f, + 0xfdd81cfa, 0x40127034, 0x9d84a9b1, 0xa06a2517, 0x7dfcfc92, + 0xc036905c, 0x1da049d9, 0x60d34f81, 0xbd459604, 0x008ffaca, + 0xdd19234f, 0xfa69f67a, 0x27ff2fff, 0x9a354331, 0x47a39ab4, + 0x3ad09cec, 0xe7464569, 0x5a8c29a7, 0x871af022, 0x146d83cd, + 0xc9fb5a48, 0x74313686, 0xa9a7ef03, 0xd4d4e95b, 0x094230de, + 0xb4885c10, 0x691e8595, 0x4e6e50a0, 0x93f88925, 0x2e32e5eb, + 0xf3a43c6e, 0x8ed73a36, 0x5341e3b3, 0xee8b8f7d, 0x331d56f8, + 0x13146ee2, 0xce82b767, 0x7348dba9, 0xaede022c, 0xd3ad0474, + 0x0e3bddf1, 0xb3f1b13f, 0x6e6768ba, 0x4917bd8f, 0x9481640a, + 0x294b08c4, 0xf4ddd141, 0x89aed719, 0x54380e9c, 0xe9f26252, + 0x3464bbd7, 0xa713c838, 0x7a8511bd, 0xc74f7d73, 0x1ad9a4f6, + 0x67aaa2ae, 0xba3c7b2b, 0x07f617e5, 0xda60ce60, 0xfd101b55, + 0x2086c2d0, 0x9d4cae1e, 0x40da779b, 0x3da971c3, 0xe03fa846, + 0x5df5c488, 0x80631d0d, 0x1de7b4bc, 0xc0716d39, 0x7dbb01f7, + 0xa02dd872, 0xdd5ede2a, 0x00c807af, 0xbd026b61, 0x6094b2e4, + 0x47e467d1, 0x9a72be54, 0x27b8d29a, 0xfa2e0b1f, 0x875d0d47, + 0x5acbd4c2, 0xe701b80c, 0x3a976189, 0xa9e01266, 0x7476cbe3, + 0xc9bca72d, 0x142a7ea8, 0x695978f0, 0xb4cfa175, 0x0905cdbb, + 0xd493143e, 0xf3e3c10b, 0x2e75188e, 0x93bf7440, 0x4e29adc5, + 0x335aab9d, 0xeecc7218, 0x53061ed6, 0x8e90c753, 0xae99ff49, + 0x730f26cc, 0xcec54a02, 0x13539387, 0x6e2095df, 0xb3b64c5a, + 0x0e7c2094, 0xd3eaf911, 0xf49a2c24, 0x290cf5a1, 0x94c6996f, + 0x495040ea, 0x342346b2, 0xe9b59f37, 0x547ff3f9, 0x89e92a7c, + 0x1a9e5993, 0xc7088016, 0x7ac2ecd8, 0xa754355d, 0xda273305, + 0x07b1ea80, 0xba7b864e, 0x67ed5fcb, 0x409d8afe, 0x9d0b537b, + 0x20c13fb5, 0xfd57e630, 0x8024e068, 0x5db239ed, 0xe0785523, + 0x3dee8ca6}}; + +local const z_word_t FAR crc_braid_big_table[][256] = { + {0x00000000, 0x85d996dd, 0x4bb55c60, 0xce6ccabd, 0x966ab9c0, + 0x13b32f1d, 0xdddfe5a0, 0x5806737d, 0x6dd3035a, 0xe80a9587, + 0x26665f3a, 0xa3bfc9e7, 0xfbb9ba9a, 0x7e602c47, 0xb00ce6fa, + 0x35d57027, 0xdaa607b4, 0x5f7f9169, 0x91135bd4, 0x14cacd09, + 0x4cccbe74, 0xc91528a9, 0x0779e214, 0x82a074c9, 0xb77504ee, + 0x32ac9233, 0xfcc0588e, 0x7919ce53, 0x211fbd2e, 0xa4c62bf3, + 0x6aaae14e, 0xef737793, 0xf54b7eb3, 0x7092e86e, 0xbefe22d3, + 0x3b27b40e, 0x6321c773, 0xe6f851ae, 0x28949b13, 0xad4d0dce, + 0x98987de9, 0x1d41eb34, 0xd32d2189, 0x56f4b754, 0x0ef2c429, + 0x8b2b52f4, 0x45479849, 0xc09e0e94, 0x2fed7907, 0xaa34efda, + 0x64582567, 0xe181b3ba, 0xb987c0c7, 0x3c5e561a, 0xf2329ca7, + 0x77eb0a7a, 0x423e7a5d, 0xc7e7ec80, 0x098b263d, 0x8c52b0e0, + 0xd454c39d, 0x518d5540, 0x9fe19ffd, 0x1a380920, 0xab918dbd, + 0x2e481b60, 0xe024d1dd, 0x65fd4700, 0x3dfb347d, 0xb822a2a0, + 0x764e681d, 0xf397fec0, 0xc6428ee7, 0x439b183a, 0x8df7d287, + 0x082e445a, 0x50283727, 0xd5f1a1fa, 0x1b9d6b47, 0x9e44fd9a, + 0x71378a09, 0xf4ee1cd4, 0x3a82d669, 0xbf5b40b4, 0xe75d33c9, + 0x6284a514, 0xace86fa9, 0x2931f974, 0x1ce48953, 0x993d1f8e, + 0x5751d533, 0xd28843ee, 0x8a8e3093, 0x0f57a64e, 0xc13b6cf3, + 0x44e2fa2e, 0x5edaf30e, 0xdb0365d3, 0x156faf6e, 0x90b639b3, + 0xc8b04ace, 0x4d69dc13, 0x830516ae, 0x06dc8073, 0x3309f054, + 0xb6d06689, 0x78bcac34, 0xfd653ae9, 0xa5634994, 0x20badf49, + 0xeed615f4, 0x6b0f8329, 0x847cf4ba, 0x01a56267, 0xcfc9a8da, + 0x4a103e07, 0x12164d7a, 0x97cfdba7, 0x59a3111a, 0xdc7a87c7, + 0xe9aff7e0, 0x6c76613d, 0xa21aab80, 0x27c33d5d, 0x7fc54e20, + 0xfa1cd8fd, 0x34701240, 0xb1a9849d, 0x17256aa0, 0x92fcfc7d, + 0x5c9036c0, 0xd949a01d, 0x814fd360, 0x049645bd, 0xcafa8f00, + 0x4f2319dd, 0x7af669fa, 0xff2fff27, 0x3143359a, 0xb49aa347, + 0xec9cd03a, 0x694546e7, 0xa7298c5a, 0x22f01a87, 0xcd836d14, + 0x485afbc9, 0x86363174, 0x03efa7a9, 0x5be9d4d4, 0xde304209, + 0x105c88b4, 0x95851e69, 0xa0506e4e, 0x2589f893, 0xebe5322e, + 0x6e3ca4f3, 0x363ad78e, 0xb3e34153, 0x7d8f8bee, 0xf8561d33, + 0xe26e1413, 0x67b782ce, 0xa9db4873, 0x2c02deae, 0x7404add3, + 0xf1dd3b0e, 0x3fb1f1b3, 0xba68676e, 0x8fbd1749, 0x0a648194, + 0xc4084b29, 0x41d1ddf4, 0x19d7ae89, 0x9c0e3854, 0x5262f2e9, + 0xd7bb6434, 0x38c813a7, 0xbd11857a, 0x737d4fc7, 0xf6a4d91a, + 0xaea2aa67, 0x2b7b3cba, 0xe517f607, 0x60ce60da, 0x551b10fd, + 0xd0c28620, 0x1eae4c9d, 0x9b77da40, 0xc371a93d, 0x46a83fe0, + 0x88c4f55d, 0x0d1d6380, 0xbcb4e71d, 0x396d71c0, 0xf701bb7d, + 0x72d82da0, 0x2ade5edd, 0xaf07c800, 0x616b02bd, 0xe4b29460, + 0xd167e447, 0x54be729a, 0x9ad2b827, 0x1f0b2efa, 0x470d5d87, + 0xc2d4cb5a, 0x0cb801e7, 0x8961973a, 0x6612e0a9, 0xe3cb7674, + 0x2da7bcc9, 0xa87e2a14, 0xf0785969, 0x75a1cfb4, 0xbbcd0509, + 0x3e1493d4, 0x0bc1e3f3, 0x8e18752e, 0x4074bf93, 0xc5ad294e, + 0x9dab5a33, 0x1872ccee, 0xd61e0653, 0x53c7908e, 0x49ff99ae, + 0xcc260f73, 0x024ac5ce, 0x87935313, 0xdf95206e, 0x5a4cb6b3, + 0x94207c0e, 0x11f9ead3, 0x242c9af4, 0xa1f50c29, 0x6f99c694, + 0xea405049, 0xb2462334, 0x379fb5e9, 0xf9f37f54, 0x7c2ae989, + 0x93599e1a, 0x168008c7, 0xd8ecc27a, 0x5d3554a7, 0x053327da, + 0x80eab107, 0x4e867bba, 0xcb5fed67, 0xfe8a9d40, 0x7b530b9d, + 0xb53fc120, 0x30e657fd, 0x68e02480, 0xed39b25d, 0x235578e0, + 0xa68cee3d}, + {0x00000000, 0x76e10f9d, 0xadc46ee1, 0xdb25617c, 0x1b8fac19, + 0x6d6ea384, 0xb64bc2f8, 0xc0aacd65, 0x361e5933, 0x40ff56ae, + 0x9bda37d2, 0xed3b384f, 0x2d91f52a, 0x5b70fab7, 0x80559bcb, + 0xf6b49456, 0x6c3cb266, 0x1addbdfb, 0xc1f8dc87, 0xb719d31a, + 0x77b31e7f, 0x015211e2, 0xda77709e, 0xac967f03, 0x5a22eb55, + 0x2cc3e4c8, 0xf7e685b4, 0x81078a29, 0x41ad474c, 0x374c48d1, + 0xec6929ad, 0x9a882630, 0xd87864cd, 0xae996b50, 0x75bc0a2c, + 0x035d05b1, 0xc3f7c8d4, 0xb516c749, 0x6e33a635, 0x18d2a9a8, + 0xee663dfe, 0x98873263, 0x43a2531f, 0x35435c82, 0xf5e991e7, + 0x83089e7a, 0x582dff06, 0x2eccf09b, 0xb444d6ab, 0xc2a5d936, + 0x1980b84a, 0x6f61b7d7, 0xafcb7ab2, 0xd92a752f, 0x020f1453, + 0x74ee1bce, 0x825a8f98, 0xf4bb8005, 0x2f9ee179, 0x597feee4, + 0x99d52381, 0xef342c1c, 0x34114d60, 0x42f042fd, 0xf1f7b941, + 0x8716b6dc, 0x5c33d7a0, 0x2ad2d83d, 0xea781558, 0x9c991ac5, + 0x47bc7bb9, 0x315d7424, 0xc7e9e072, 0xb108efef, 0x6a2d8e93, + 0x1ccc810e, 0xdc664c6b, 0xaa8743f6, 0x71a2228a, 0x07432d17, + 0x9dcb0b27, 0xeb2a04ba, 0x300f65c6, 0x46ee6a5b, 0x8644a73e, + 0xf0a5a8a3, 0x2b80c9df, 0x5d61c642, 0xabd55214, 0xdd345d89, + 0x06113cf5, 0x70f03368, 0xb05afe0d, 0xc6bbf190, 0x1d9e90ec, + 0x6b7f9f71, 0x298fdd8c, 0x5f6ed211, 0x844bb36d, 0xf2aabcf0, + 0x32007195, 0x44e17e08, 0x9fc41f74, 0xe92510e9, 0x1f9184bf, + 0x69708b22, 0xb255ea5e, 0xc4b4e5c3, 0x041e28a6, 0x72ff273b, + 0xa9da4647, 0xdf3b49da, 0x45b36fea, 0x33526077, 0xe877010b, + 0x9e960e96, 0x5e3cc3f3, 0x28ddcc6e, 0xf3f8ad12, 0x8519a28f, + 0x73ad36d9, 0x054c3944, 0xde695838, 0xa88857a5, 0x68229ac0, + 0x1ec3955d, 0xc5e6f421, 0xb307fbbc, 0xe2ef7383, 0x940e7c1e, + 0x4f2b1d62, 0x39ca12ff, 0xf960df9a, 0x8f81d007, 0x54a4b17b, + 0x2245bee6, 0xd4f12ab0, 0xa210252d, 0x79354451, 0x0fd44bcc, + 0xcf7e86a9, 0xb99f8934, 0x62bae848, 0x145be7d5, 0x8ed3c1e5, + 0xf832ce78, 0x2317af04, 0x55f6a099, 0x955c6dfc, 0xe3bd6261, + 0x3898031d, 0x4e790c80, 0xb8cd98d6, 0xce2c974b, 0x1509f637, + 0x63e8f9aa, 0xa34234cf, 0xd5a33b52, 0x0e865a2e, 0x786755b3, + 0x3a97174e, 0x4c7618d3, 0x975379af, 0xe1b27632, 0x2118bb57, + 0x57f9b4ca, 0x8cdcd5b6, 0xfa3dda2b, 0x0c894e7d, 0x7a6841e0, + 0xa14d209c, 0xd7ac2f01, 0x1706e264, 0x61e7edf9, 0xbac28c85, + 0xcc238318, 0x56aba528, 0x204aaab5, 0xfb6fcbc9, 0x8d8ec454, + 0x4d240931, 0x3bc506ac, 0xe0e067d0, 0x9601684d, 0x60b5fc1b, + 0x1654f386, 0xcd7192fa, 0xbb909d67, 0x7b3a5002, 0x0ddb5f9f, + 0xd6fe3ee3, 0xa01f317e, 0x1318cac2, 0x65f9c55f, 0xbedca423, + 0xc83dabbe, 0x089766db, 0x7e766946, 0xa553083a, 0xd3b207a7, + 0x250693f1, 0x53e79c6c, 0x88c2fd10, 0xfe23f28d, 0x3e893fe8, + 0x48683075, 0x934d5109, 0xe5ac5e94, 0x7f2478a4, 0x09c57739, + 0xd2e01645, 0xa40119d8, 0x64abd4bd, 0x124adb20, 0xc96fba5c, + 0xbf8eb5c1, 0x493a2197, 0x3fdb2e0a, 0xe4fe4f76, 0x921f40eb, + 0x52b58d8e, 0x24548213, 0xff71e36f, 0x8990ecf2, 0xcb60ae0f, + 0xbd81a192, 0x66a4c0ee, 0x1045cf73, 0xd0ef0216, 0xa60e0d8b, + 0x7d2b6cf7, 0x0bca636a, 0xfd7ef73c, 0x8b9ff8a1, 0x50ba99dd, + 0x265b9640, 0xe6f15b25, 0x901054b8, 0x4b3535c4, 0x3dd43a59, + 0xa75c1c69, 0xd1bd13f4, 0x0a987288, 0x7c797d15, 0xbcd3b070, + 0xca32bfed, 0x1117de91, 0x67f6d10c, 0x9142455a, 0xe7a34ac7, + 0x3c862bbb, 0x4a672426, 0x8acde943, 0xfc2ce6de, 0x270987a2, + 0x51e8883f}, + {0x00000000, 0xe8dbfbb9, 0x91b186a8, 0x796a7d11, 0x63657c8a, + 0x8bbe8733, 0xf2d4fa22, 0x1a0f019b, 0x87cc89cf, 0x6f177276, + 0x167d0f67, 0xfea6f4de, 0xe4a9f545, 0x0c720efc, 0x751873ed, + 0x9dc38854, 0x4f9f6244, 0xa74499fd, 0xde2ee4ec, 0x36f51f55, + 0x2cfa1ece, 0xc421e577, 0xbd4b9866, 0x559063df, 0xc853eb8b, + 0x20881032, 0x59e26d23, 0xb139969a, 0xab369701, 0x43ed6cb8, + 0x3a8711a9, 0xd25cea10, 0x9e3ec588, 0x76e53e31, 0x0f8f4320, + 0xe754b899, 0xfd5bb902, 0x158042bb, 0x6cea3faa, 0x8431c413, + 0x19f24c47, 0xf129b7fe, 0x8843caef, 0x60983156, 0x7a9730cd, + 0x924ccb74, 0xeb26b665, 0x03fd4ddc, 0xd1a1a7cc, 0x397a5c75, + 0x40102164, 0xa8cbdadd, 0xb2c4db46, 0x5a1f20ff, 0x23755dee, + 0xcbaea657, 0x566d2e03, 0xbeb6d5ba, 0xc7dca8ab, 0x2f075312, + 0x35085289, 0xddd3a930, 0xa4b9d421, 0x4c622f98, 0x7d7bfbca, + 0x95a00073, 0xecca7d62, 0x041186db, 0x1e1e8740, 0xf6c57cf9, + 0x8faf01e8, 0x6774fa51, 0xfab77205, 0x126c89bc, 0x6b06f4ad, + 0x83dd0f14, 0x99d20e8f, 0x7109f536, 0x08638827, 0xe0b8739e, + 0x32e4998e, 0xda3f6237, 0xa3551f26, 0x4b8ee49f, 0x5181e504, + 0xb95a1ebd, 0xc03063ac, 0x28eb9815, 0xb5281041, 0x5df3ebf8, + 0x249996e9, 0xcc426d50, 0xd64d6ccb, 0x3e969772, 0x47fcea63, + 0xaf2711da, 0xe3453e42, 0x0b9ec5fb, 0x72f4b8ea, 0x9a2f4353, + 0x802042c8, 0x68fbb971, 0x1191c460, 0xf94a3fd9, 0x6489b78d, + 0x8c524c34, 0xf5383125, 0x1de3ca9c, 0x07eccb07, 0xef3730be, + 0x965d4daf, 0x7e86b616, 0xacda5c06, 0x4401a7bf, 0x3d6bdaae, + 0xd5b02117, 0xcfbf208c, 0x2764db35, 0x5e0ea624, 0xb6d55d9d, + 0x2b16d5c9, 0xc3cd2e70, 0xbaa75361, 0x527ca8d8, 0x4873a943, + 0xa0a852fa, 0xd9c22feb, 0x3119d452, 0xbbf0874e, 0x532b7cf7, + 0x2a4101e6, 0xc29afa5f, 0xd895fbc4, 0x304e007d, 0x49247d6c, + 0xa1ff86d5, 0x3c3c0e81, 0xd4e7f538, 0xad8d8829, 0x45567390, + 0x5f59720b, 0xb78289b2, 0xcee8f4a3, 0x26330f1a, 0xf46fe50a, + 0x1cb41eb3, 0x65de63a2, 0x8d05981b, 0x970a9980, 0x7fd16239, + 0x06bb1f28, 0xee60e491, 0x73a36cc5, 0x9b78977c, 0xe212ea6d, + 0x0ac911d4, 0x10c6104f, 0xf81debf6, 0x817796e7, 0x69ac6d5e, + 0x25ce42c6, 0xcd15b97f, 0xb47fc46e, 0x5ca43fd7, 0x46ab3e4c, + 0xae70c5f5, 0xd71ab8e4, 0x3fc1435d, 0xa202cb09, 0x4ad930b0, + 0x33b34da1, 0xdb68b618, 0xc167b783, 0x29bc4c3a, 0x50d6312b, + 0xb80dca92, 0x6a512082, 0x828adb3b, 0xfbe0a62a, 0x133b5d93, + 0x09345c08, 0xe1efa7b1, 0x9885daa0, 0x705e2119, 0xed9da94d, + 0x054652f4, 0x7c2c2fe5, 0x94f7d45c, 0x8ef8d5c7, 0x66232e7e, + 0x1f49536f, 0xf792a8d6, 0xc68b7c84, 0x2e50873d, 0x573afa2c, + 0xbfe10195, 0xa5ee000e, 0x4d35fbb7, 0x345f86a6, 0xdc847d1f, + 0x4147f54b, 0xa99c0ef2, 0xd0f673e3, 0x382d885a, 0x222289c1, + 0xcaf97278, 0xb3930f69, 0x5b48f4d0, 0x89141ec0, 0x61cfe579, + 0x18a59868, 0xf07e63d1, 0xea71624a, 0x02aa99f3, 0x7bc0e4e2, + 0x931b1f5b, 0x0ed8970f, 0xe6036cb6, 0x9f6911a7, 0x77b2ea1e, + 0x6dbdeb85, 0x8566103c, 0xfc0c6d2d, 0x14d79694, 0x58b5b90c, + 0xb06e42b5, 0xc9043fa4, 0x21dfc41d, 0x3bd0c586, 0xd30b3e3f, + 0xaa61432e, 0x42bab897, 0xdf7930c3, 0x37a2cb7a, 0x4ec8b66b, + 0xa6134dd2, 0xbc1c4c49, 0x54c7b7f0, 0x2dadcae1, 0xc5763158, + 0x172adb48, 0xfff120f1, 0x869b5de0, 0x6e40a659, 0x744fa7c2, + 0x9c945c7b, 0xe5fe216a, 0x0d25dad3, 0x90e65287, 0x783da93e, + 0x0157d42f, 0xe98c2f96, 0xf3832e0d, 0x1b58d5b4, 0x6232a8a5, + 0x8ae9531c}, + {0x00000000, 0x919168ae, 0x6325a087, 0xf2b4c829, 0x874c31d4, + 0x16dd597a, 0xe4699153, 0x75f8f9fd, 0x4f9f1373, 0xde0e7bdd, + 0x2cbab3f4, 0xbd2bdb5a, 0xc8d322a7, 0x59424a09, 0xabf68220, + 0x3a67ea8e, 0x9e3e27e6, 0x0faf4f48, 0xfd1b8761, 0x6c8aefcf, + 0x19721632, 0x88e37e9c, 0x7a57b6b5, 0xebc6de1b, 0xd1a13495, + 0x40305c3b, 0xb2849412, 0x2315fcbc, 0x56ed0541, 0xc77c6def, + 0x35c8a5c6, 0xa459cd68, 0x7d7b3f17, 0xecea57b9, 0x1e5e9f90, + 0x8fcff73e, 0xfa370ec3, 0x6ba6666d, 0x9912ae44, 0x0883c6ea, + 0x32e42c64, 0xa37544ca, 0x51c18ce3, 0xc050e44d, 0xb5a81db0, + 0x2439751e, 0xd68dbd37, 0x471cd599, 0xe34518f1, 0x72d4705f, + 0x8060b876, 0x11f1d0d8, 0x64092925, 0xf598418b, 0x072c89a2, + 0x96bde10c, 0xacda0b82, 0x3d4b632c, 0xcfffab05, 0x5e6ec3ab, + 0x2b963a56, 0xba0752f8, 0x48b39ad1, 0xd922f27f, 0xfaf67e2e, + 0x6b671680, 0x99d3dea9, 0x0842b607, 0x7dba4ffa, 0xec2b2754, + 0x1e9fef7d, 0x8f0e87d3, 0xb5696d5d, 0x24f805f3, 0xd64ccdda, + 0x47dda574, 0x32255c89, 0xa3b43427, 0x5100fc0e, 0xc09194a0, + 0x64c859c8, 0xf5593166, 0x07edf94f, 0x967c91e1, 0xe384681c, + 0x721500b2, 0x80a1c89b, 0x1130a035, 0x2b574abb, 0xbac62215, + 0x4872ea3c, 0xd9e38292, 0xac1b7b6f, 0x3d8a13c1, 0xcf3edbe8, + 0x5eafb346, 0x878d4139, 0x161c2997, 0xe4a8e1be, 0x75398910, + 0x00c170ed, 0x91501843, 0x63e4d06a, 0xf275b8c4, 0xc812524a, + 0x59833ae4, 0xab37f2cd, 0x3aa69a63, 0x4f5e639e, 0xdecf0b30, + 0x2c7bc319, 0xbdeaabb7, 0x19b366df, 0x88220e71, 0x7a96c658, + 0xeb07aef6, 0x9eff570b, 0x0f6e3fa5, 0xfddaf78c, 0x6c4b9f22, + 0x562c75ac, 0xc7bd1d02, 0x3509d52b, 0xa498bd85, 0xd1604478, + 0x40f12cd6, 0xb245e4ff, 0x23d48c51, 0xf4edfd5c, 0x657c95f2, + 0x97c85ddb, 0x06593575, 0x73a1cc88, 0xe230a426, 0x10846c0f, + 0x811504a1, 0xbb72ee2f, 0x2ae38681, 0xd8574ea8, 0x49c62606, + 0x3c3edffb, 0xadafb755, 0x5f1b7f7c, 0xce8a17d2, 0x6ad3daba, + 0xfb42b214, 0x09f67a3d, 0x98671293, 0xed9feb6e, 0x7c0e83c0, + 0x8eba4be9, 0x1f2b2347, 0x254cc9c9, 0xb4dda167, 0x4669694e, + 0xd7f801e0, 0xa200f81d, 0x339190b3, 0xc125589a, 0x50b43034, + 0x8996c24b, 0x1807aae5, 0xeab362cc, 0x7b220a62, 0x0edaf39f, + 0x9f4b9b31, 0x6dff5318, 0xfc6e3bb6, 0xc609d138, 0x5798b996, + 0xa52c71bf, 0x34bd1911, 0x4145e0ec, 0xd0d48842, 0x2260406b, + 0xb3f128c5, 0x17a8e5ad, 0x86398d03, 0x748d452a, 0xe51c2d84, + 0x90e4d479, 0x0175bcd7, 0xf3c174fe, 0x62501c50, 0x5837f6de, + 0xc9a69e70, 0x3b125659, 0xaa833ef7, 0xdf7bc70a, 0x4eeaafa4, + 0xbc5e678d, 0x2dcf0f23, 0x0e1b8372, 0x9f8aebdc, 0x6d3e23f5, + 0xfcaf4b5b, 0x8957b2a6, 0x18c6da08, 0xea721221, 0x7be37a8f, + 0x41849001, 0xd015f8af, 0x22a13086, 0xb3305828, 0xc6c8a1d5, + 0x5759c97b, 0xa5ed0152, 0x347c69fc, 0x9025a494, 0x01b4cc3a, + 0xf3000413, 0x62916cbd, 0x17699540, 0x86f8fdee, 0x744c35c7, + 0xe5dd5d69, 0xdfbab7e7, 0x4e2bdf49, 0xbc9f1760, 0x2d0e7fce, + 0x58f68633, 0xc967ee9d, 0x3bd326b4, 0xaa424e1a, 0x7360bc65, + 0xe2f1d4cb, 0x10451ce2, 0x81d4744c, 0xf42c8db1, 0x65bde51f, + 0x97092d36, 0x06984598, 0x3cffaf16, 0xad6ec7b8, 0x5fda0f91, + 0xce4b673f, 0xbbb39ec2, 0x2a22f66c, 0xd8963e45, 0x490756eb, + 0xed5e9b83, 0x7ccff32d, 0x8e7b3b04, 0x1fea53aa, 0x6a12aa57, + 0xfb83c2f9, 0x09370ad0, 0x98a6627e, 0xa2c188f0, 0x3350e05e, + 0xc1e42877, 0x507540d9, 0x258db924, 0xb41cd18a, 0x46a819a3, + 0xd739710d}}; + +#endif + +#endif + +#if N == 5 + +#if W == 8 + +local const z_crc_t FAR crc_braid_table[][256] = { + {0x00000000, 0xaf449247, 0x85f822cf, 0x2abcb088, 0xd08143df, + 0x7fc5d198, 0x55796110, 0xfa3df357, 0x7a7381ff, 0xd53713b8, + 0xff8ba330, 0x50cf3177, 0xaaf2c220, 0x05b65067, 0x2f0ae0ef, + 0x804e72a8, 0xf4e703fe, 0x5ba391b9, 0x711f2131, 0xde5bb376, + 0x24664021, 0x8b22d266, 0xa19e62ee, 0x0edaf0a9, 0x8e948201, + 0x21d01046, 0x0b6ca0ce, 0xa4283289, 0x5e15c1de, 0xf1515399, + 0xdbede311, 0x74a97156, 0x32bf01bd, 0x9dfb93fa, 0xb7472372, + 0x1803b135, 0xe23e4262, 0x4d7ad025, 0x67c660ad, 0xc882f2ea, + 0x48cc8042, 0xe7881205, 0xcd34a28d, 0x627030ca, 0x984dc39d, + 0x370951da, 0x1db5e152, 0xb2f17315, 0xc6580243, 0x691c9004, + 0x43a0208c, 0xece4b2cb, 0x16d9419c, 0xb99dd3db, 0x93216353, + 0x3c65f114, 0xbc2b83bc, 0x136f11fb, 0x39d3a173, 0x96973334, + 0x6caac063, 0xc3ee5224, 0xe952e2ac, 0x461670eb, 0x657e037a, + 0xca3a913d, 0xe08621b5, 0x4fc2b3f2, 0xb5ff40a5, 0x1abbd2e2, + 0x3007626a, 0x9f43f02d, 0x1f0d8285, 0xb04910c2, 0x9af5a04a, + 0x35b1320d, 0xcf8cc15a, 0x60c8531d, 0x4a74e395, 0xe53071d2, + 0x91990084, 0x3edd92c3, 0x1461224b, 0xbb25b00c, 0x4118435b, + 0xee5cd11c, 0xc4e06194, 0x6ba4f3d3, 0xebea817b, 0x44ae133c, + 0x6e12a3b4, 0xc15631f3, 0x3b6bc2a4, 0x942f50e3, 0xbe93e06b, + 0x11d7722c, 0x57c102c7, 0xf8859080, 0xd2392008, 0x7d7db24f, + 0x87404118, 0x2804d35f, 0x02b863d7, 0xadfcf190, 0x2db28338, + 0x82f6117f, 0xa84aa1f7, 0x070e33b0, 0xfd33c0e7, 0x527752a0, + 0x78cbe228, 0xd78f706f, 0xa3260139, 0x0c62937e, 0x26de23f6, + 0x899ab1b1, 0x73a742e6, 0xdce3d0a1, 0xf65f6029, 0x591bf26e, + 0xd95580c6, 0x76111281, 0x5cada209, 0xf3e9304e, 0x09d4c319, + 0xa690515e, 0x8c2ce1d6, 0x23687391, 0xcafc06f4, 0x65b894b3, + 0x4f04243b, 0xe040b67c, 0x1a7d452b, 0xb539d76c, 0x9f8567e4, + 0x30c1f5a3, 0xb08f870b, 0x1fcb154c, 0x3577a5c4, 0x9a333783, + 0x600ec4d4, 0xcf4a5693, 0xe5f6e61b, 0x4ab2745c, 0x3e1b050a, + 0x915f974d, 0xbbe327c5, 0x14a7b582, 0xee9a46d5, 0x41ded492, + 0x6b62641a, 0xc426f65d, 0x446884f5, 0xeb2c16b2, 0xc190a63a, + 0x6ed4347d, 0x94e9c72a, 0x3bad556d, 0x1111e5e5, 0xbe5577a2, + 0xf8430749, 0x5707950e, 0x7dbb2586, 0xd2ffb7c1, 0x28c24496, + 0x8786d6d1, 0xad3a6659, 0x027ef41e, 0x823086b6, 0x2d7414f1, + 0x07c8a479, 0xa88c363e, 0x52b1c569, 0xfdf5572e, 0xd749e7a6, + 0x780d75e1, 0x0ca404b7, 0xa3e096f0, 0x895c2678, 0x2618b43f, + 0xdc254768, 0x7361d52f, 0x59dd65a7, 0xf699f7e0, 0x76d78548, + 0xd993170f, 0xf32fa787, 0x5c6b35c0, 0xa656c697, 0x091254d0, + 0x23aee458, 0x8cea761f, 0xaf82058e, 0x00c697c9, 0x2a7a2741, + 0x853eb506, 0x7f034651, 0xd047d416, 0xfafb649e, 0x55bff6d9, + 0xd5f18471, 0x7ab51636, 0x5009a6be, 0xff4d34f9, 0x0570c7ae, + 0xaa3455e9, 0x8088e561, 0x2fcc7726, 0x5b650670, 0xf4219437, + 0xde9d24bf, 0x71d9b6f8, 0x8be445af, 0x24a0d7e8, 0x0e1c6760, + 0xa158f527, 0x2116878f, 0x8e5215c8, 0xa4eea540, 0x0baa3707, + 0xf197c450, 0x5ed35617, 0x746fe69f, 0xdb2b74d8, 0x9d3d0433, + 0x32799674, 0x18c526fc, 0xb781b4bb, 0x4dbc47ec, 0xe2f8d5ab, + 0xc8446523, 0x6700f764, 0xe74e85cc, 0x480a178b, 0x62b6a703, + 0xcdf23544, 0x37cfc613, 0x988b5454, 0xb237e4dc, 0x1d73769b, + 0x69da07cd, 0xc69e958a, 0xec222502, 0x4366b745, 0xb95b4412, + 0x161fd655, 0x3ca366dd, 0x93e7f49a, 0x13a98632, 0xbced1475, + 0x9651a4fd, 0x391536ba, 0xc328c5ed, 0x6c6c57aa, 0x46d0e722, + 0xe9947565}, + {0x00000000, 0x4e890ba9, 0x9d121752, 0xd39b1cfb, 0xe15528e5, + 0xafdc234c, 0x7c473fb7, 0x32ce341e, 0x19db578b, 0x57525c22, + 0x84c940d9, 0xca404b70, 0xf88e7f6e, 0xb60774c7, 0x659c683c, + 0x2b156395, 0x33b6af16, 0x7d3fa4bf, 0xaea4b844, 0xe02db3ed, + 0xd2e387f3, 0x9c6a8c5a, 0x4ff190a1, 0x01789b08, 0x2a6df89d, + 0x64e4f334, 0xb77fefcf, 0xf9f6e466, 0xcb38d078, 0x85b1dbd1, + 0x562ac72a, 0x18a3cc83, 0x676d5e2c, 0x29e45585, 0xfa7f497e, + 0xb4f642d7, 0x863876c9, 0xc8b17d60, 0x1b2a619b, 0x55a36a32, + 0x7eb609a7, 0x303f020e, 0xe3a41ef5, 0xad2d155c, 0x9fe32142, + 0xd16a2aeb, 0x02f13610, 0x4c783db9, 0x54dbf13a, 0x1a52fa93, + 0xc9c9e668, 0x8740edc1, 0xb58ed9df, 0xfb07d276, 0x289cce8d, + 0x6615c524, 0x4d00a6b1, 0x0389ad18, 0xd012b1e3, 0x9e9bba4a, + 0xac558e54, 0xe2dc85fd, 0x31479906, 0x7fce92af, 0xcedabc58, + 0x8053b7f1, 0x53c8ab0a, 0x1d41a0a3, 0x2f8f94bd, 0x61069f14, + 0xb29d83ef, 0xfc148846, 0xd701ebd3, 0x9988e07a, 0x4a13fc81, + 0x049af728, 0x3654c336, 0x78ddc89f, 0xab46d464, 0xe5cfdfcd, + 0xfd6c134e, 0xb3e518e7, 0x607e041c, 0x2ef70fb5, 0x1c393bab, + 0x52b03002, 0x812b2cf9, 0xcfa22750, 0xe4b744c5, 0xaa3e4f6c, + 0x79a55397, 0x372c583e, 0x05e26c20, 0x4b6b6789, 0x98f07b72, + 0xd67970db, 0xa9b7e274, 0xe73ee9dd, 0x34a5f526, 0x7a2cfe8f, + 0x48e2ca91, 0x066bc138, 0xd5f0ddc3, 0x9b79d66a, 0xb06cb5ff, + 0xfee5be56, 0x2d7ea2ad, 0x63f7a904, 0x51399d1a, 0x1fb096b3, + 0xcc2b8a48, 0x82a281e1, 0x9a014d62, 0xd48846cb, 0x07135a30, + 0x499a5199, 0x7b546587, 0x35dd6e2e, 0xe64672d5, 0xa8cf797c, + 0x83da1ae9, 0xcd531140, 0x1ec80dbb, 0x50410612, 0x628f320c, + 0x2c0639a5, 0xff9d255e, 0xb1142ef7, 0x46c47ef1, 0x084d7558, + 0xdbd669a3, 0x955f620a, 0xa7915614, 0xe9185dbd, 0x3a834146, + 0x740a4aef, 0x5f1f297a, 0x119622d3, 0xc20d3e28, 0x8c843581, + 0xbe4a019f, 0xf0c30a36, 0x235816cd, 0x6dd11d64, 0x7572d1e7, + 0x3bfbda4e, 0xe860c6b5, 0xa6e9cd1c, 0x9427f902, 0xdaaef2ab, + 0x0935ee50, 0x47bce5f9, 0x6ca9866c, 0x22208dc5, 0xf1bb913e, + 0xbf329a97, 0x8dfcae89, 0xc375a520, 0x10eeb9db, 0x5e67b272, + 0x21a920dd, 0x6f202b74, 0xbcbb378f, 0xf2323c26, 0xc0fc0838, + 0x8e750391, 0x5dee1f6a, 0x136714c3, 0x38727756, 0x76fb7cff, + 0xa5606004, 0xebe96bad, 0xd9275fb3, 0x97ae541a, 0x443548e1, + 0x0abc4348, 0x121f8fcb, 0x5c968462, 0x8f0d9899, 0xc1849330, + 0xf34aa72e, 0xbdc3ac87, 0x6e58b07c, 0x20d1bbd5, 0x0bc4d840, + 0x454dd3e9, 0x96d6cf12, 0xd85fc4bb, 0xea91f0a5, 0xa418fb0c, + 0x7783e7f7, 0x390aec5e, 0x881ec2a9, 0xc697c900, 0x150cd5fb, + 0x5b85de52, 0x694bea4c, 0x27c2e1e5, 0xf459fd1e, 0xbad0f6b7, + 0x91c59522, 0xdf4c9e8b, 0x0cd78270, 0x425e89d9, 0x7090bdc7, + 0x3e19b66e, 0xed82aa95, 0xa30ba13c, 0xbba86dbf, 0xf5216616, + 0x26ba7aed, 0x68337144, 0x5afd455a, 0x14744ef3, 0xc7ef5208, + 0x896659a1, 0xa2733a34, 0xecfa319d, 0x3f612d66, 0x71e826cf, + 0x432612d1, 0x0daf1978, 0xde340583, 0x90bd0e2a, 0xef739c85, + 0xa1fa972c, 0x72618bd7, 0x3ce8807e, 0x0e26b460, 0x40afbfc9, + 0x9334a332, 0xddbda89b, 0xf6a8cb0e, 0xb821c0a7, 0x6bbadc5c, + 0x2533d7f5, 0x17fde3eb, 0x5974e842, 0x8aeff4b9, 0xc466ff10, + 0xdcc53393, 0x924c383a, 0x41d724c1, 0x0f5e2f68, 0x3d901b76, + 0x731910df, 0xa0820c24, 0xee0b078d, 0xc51e6418, 0x8b976fb1, + 0x580c734a, 0x168578e3, 0x244b4cfd, 0x6ac24754, 0xb9595baf, + 0xf7d05006}, + {0x00000000, 0x8d88fde2, 0xc060fd85, 0x4de80067, 0x5bb0fd4b, + 0xd63800a9, 0x9bd000ce, 0x1658fd2c, 0xb761fa96, 0x3ae90774, + 0x77010713, 0xfa89faf1, 0xecd107dd, 0x6159fa3f, 0x2cb1fa58, + 0xa13907ba, 0xb5b2f36d, 0x383a0e8f, 0x75d20ee8, 0xf85af30a, + 0xee020e26, 0x638af3c4, 0x2e62f3a3, 0xa3ea0e41, 0x02d309fb, + 0x8f5bf419, 0xc2b3f47e, 0x4f3b099c, 0x5963f4b0, 0xd4eb0952, + 0x99030935, 0x148bf4d7, 0xb014e09b, 0x3d9c1d79, 0x70741d1e, + 0xfdfce0fc, 0xeba41dd0, 0x662ce032, 0x2bc4e055, 0xa64c1db7, + 0x07751a0d, 0x8afde7ef, 0xc715e788, 0x4a9d1a6a, 0x5cc5e746, + 0xd14d1aa4, 0x9ca51ac3, 0x112de721, 0x05a613f6, 0x882eee14, + 0xc5c6ee73, 0x484e1391, 0x5e16eebd, 0xd39e135f, 0x9e761338, + 0x13feeeda, 0xb2c7e960, 0x3f4f1482, 0x72a714e5, 0xff2fe907, + 0xe977142b, 0x64ffe9c9, 0x2917e9ae, 0xa49f144c, 0xbb58c777, + 0x36d03a95, 0x7b383af2, 0xf6b0c710, 0xe0e83a3c, 0x6d60c7de, + 0x2088c7b9, 0xad003a5b, 0x0c393de1, 0x81b1c003, 0xcc59c064, + 0x41d13d86, 0x5789c0aa, 0xda013d48, 0x97e93d2f, 0x1a61c0cd, + 0x0eea341a, 0x8362c9f8, 0xce8ac99f, 0x4302347d, 0x555ac951, + 0xd8d234b3, 0x953a34d4, 0x18b2c936, 0xb98bce8c, 0x3403336e, + 0x79eb3309, 0xf463ceeb, 0xe23b33c7, 0x6fb3ce25, 0x225bce42, + 0xafd333a0, 0x0b4c27ec, 0x86c4da0e, 0xcb2cda69, 0x46a4278b, + 0x50fcdaa7, 0xdd742745, 0x909c2722, 0x1d14dac0, 0xbc2ddd7a, + 0x31a52098, 0x7c4d20ff, 0xf1c5dd1d, 0xe79d2031, 0x6a15ddd3, + 0x27fdddb4, 0xaa752056, 0xbefed481, 0x33762963, 0x7e9e2904, + 0xf316d4e6, 0xe54e29ca, 0x68c6d428, 0x252ed44f, 0xa8a629ad, + 0x099f2e17, 0x8417d3f5, 0xc9ffd392, 0x44772e70, 0x522fd35c, + 0xdfa72ebe, 0x924f2ed9, 0x1fc7d33b, 0xadc088af, 0x2048754d, + 0x6da0752a, 0xe02888c8, 0xf67075e4, 0x7bf88806, 0x36108861, + 0xbb987583, 0x1aa17239, 0x97298fdb, 0xdac18fbc, 0x5749725e, + 0x41118f72, 0xcc997290, 0x817172f7, 0x0cf98f15, 0x18727bc2, + 0x95fa8620, 0xd8128647, 0x559a7ba5, 0x43c28689, 0xce4a7b6b, + 0x83a27b0c, 0x0e2a86ee, 0xaf138154, 0x229b7cb6, 0x6f737cd1, + 0xe2fb8133, 0xf4a37c1f, 0x792b81fd, 0x34c3819a, 0xb94b7c78, + 0x1dd46834, 0x905c95d6, 0xddb495b1, 0x503c6853, 0x4664957f, + 0xcbec689d, 0x860468fa, 0x0b8c9518, 0xaab592a2, 0x273d6f40, + 0x6ad56f27, 0xe75d92c5, 0xf1056fe9, 0x7c8d920b, 0x3165926c, + 0xbced6f8e, 0xa8669b59, 0x25ee66bb, 0x680666dc, 0xe58e9b3e, + 0xf3d66612, 0x7e5e9bf0, 0x33b69b97, 0xbe3e6675, 0x1f0761cf, + 0x928f9c2d, 0xdf679c4a, 0x52ef61a8, 0x44b79c84, 0xc93f6166, + 0x84d76101, 0x095f9ce3, 0x16984fd8, 0x9b10b23a, 0xd6f8b25d, + 0x5b704fbf, 0x4d28b293, 0xc0a04f71, 0x8d484f16, 0x00c0b2f4, + 0xa1f9b54e, 0x2c7148ac, 0x619948cb, 0xec11b529, 0xfa494805, + 0x77c1b5e7, 0x3a29b580, 0xb7a14862, 0xa32abcb5, 0x2ea24157, + 0x634a4130, 0xeec2bcd2, 0xf89a41fe, 0x7512bc1c, 0x38fabc7b, + 0xb5724199, 0x144b4623, 0x99c3bbc1, 0xd42bbba6, 0x59a34644, + 0x4ffbbb68, 0xc273468a, 0x8f9b46ed, 0x0213bb0f, 0xa68caf43, + 0x2b0452a1, 0x66ec52c6, 0xeb64af24, 0xfd3c5208, 0x70b4afea, + 0x3d5caf8d, 0xb0d4526f, 0x11ed55d5, 0x9c65a837, 0xd18da850, + 0x5c0555b2, 0x4a5da89e, 0xc7d5557c, 0x8a3d551b, 0x07b5a8f9, + 0x133e5c2e, 0x9eb6a1cc, 0xd35ea1ab, 0x5ed65c49, 0x488ea165, + 0xc5065c87, 0x88ee5ce0, 0x0566a102, 0xa45fa6b8, 0x29d75b5a, + 0x643f5b3d, 0xe9b7a6df, 0xffef5bf3, 0x7267a611, 0x3f8fa676, + 0xb2075b94}, + {0x00000000, 0x80f0171f, 0xda91287f, 0x5a613f60, 0x6e5356bf, + 0xeea341a0, 0xb4c27ec0, 0x343269df, 0xdca6ad7e, 0x5c56ba61, + 0x06378501, 0x86c7921e, 0xb2f5fbc1, 0x3205ecde, 0x6864d3be, + 0xe894c4a1, 0x623c5cbd, 0xe2cc4ba2, 0xb8ad74c2, 0x385d63dd, + 0x0c6f0a02, 0x8c9f1d1d, 0xd6fe227d, 0x560e3562, 0xbe9af1c3, + 0x3e6ae6dc, 0x640bd9bc, 0xe4fbcea3, 0xd0c9a77c, 0x5039b063, + 0x0a588f03, 0x8aa8981c, 0xc478b97a, 0x4488ae65, 0x1ee99105, + 0x9e19861a, 0xaa2befc5, 0x2adbf8da, 0x70bac7ba, 0xf04ad0a5, + 0x18de1404, 0x982e031b, 0xc24f3c7b, 0x42bf2b64, 0x768d42bb, + 0xf67d55a4, 0xac1c6ac4, 0x2cec7ddb, 0xa644e5c7, 0x26b4f2d8, + 0x7cd5cdb8, 0xfc25daa7, 0xc817b378, 0x48e7a467, 0x12869b07, + 0x92768c18, 0x7ae248b9, 0xfa125fa6, 0xa07360c6, 0x208377d9, + 0x14b11e06, 0x94410919, 0xce203679, 0x4ed02166, 0x538074b5, + 0xd37063aa, 0x89115cca, 0x09e14bd5, 0x3dd3220a, 0xbd233515, + 0xe7420a75, 0x67b21d6a, 0x8f26d9cb, 0x0fd6ced4, 0x55b7f1b4, + 0xd547e6ab, 0xe1758f74, 0x6185986b, 0x3be4a70b, 0xbb14b014, + 0x31bc2808, 0xb14c3f17, 0xeb2d0077, 0x6bdd1768, 0x5fef7eb7, + 0xdf1f69a8, 0x857e56c8, 0x058e41d7, 0xed1a8576, 0x6dea9269, + 0x378bad09, 0xb77bba16, 0x8349d3c9, 0x03b9c4d6, 0x59d8fbb6, + 0xd928eca9, 0x97f8cdcf, 0x1708dad0, 0x4d69e5b0, 0xcd99f2af, + 0xf9ab9b70, 0x795b8c6f, 0x233ab30f, 0xa3caa410, 0x4b5e60b1, + 0xcbae77ae, 0x91cf48ce, 0x113f5fd1, 0x250d360e, 0xa5fd2111, + 0xff9c1e71, 0x7f6c096e, 0xf5c49172, 0x7534866d, 0x2f55b90d, + 0xafa5ae12, 0x9b97c7cd, 0x1b67d0d2, 0x4106efb2, 0xc1f6f8ad, + 0x29623c0c, 0xa9922b13, 0xf3f31473, 0x7303036c, 0x47316ab3, + 0xc7c17dac, 0x9da042cc, 0x1d5055d3, 0xa700e96a, 0x27f0fe75, + 0x7d91c115, 0xfd61d60a, 0xc953bfd5, 0x49a3a8ca, 0x13c297aa, + 0x933280b5, 0x7ba64414, 0xfb56530b, 0xa1376c6b, 0x21c77b74, + 0x15f512ab, 0x950505b4, 0xcf643ad4, 0x4f942dcb, 0xc53cb5d7, + 0x45cca2c8, 0x1fad9da8, 0x9f5d8ab7, 0xab6fe368, 0x2b9ff477, + 0x71fecb17, 0xf10edc08, 0x199a18a9, 0x996a0fb6, 0xc30b30d6, + 0x43fb27c9, 0x77c94e16, 0xf7395909, 0xad586669, 0x2da87176, + 0x63785010, 0xe388470f, 0xb9e9786f, 0x39196f70, 0x0d2b06af, + 0x8ddb11b0, 0xd7ba2ed0, 0x574a39cf, 0xbfdefd6e, 0x3f2eea71, + 0x654fd511, 0xe5bfc20e, 0xd18dabd1, 0x517dbcce, 0x0b1c83ae, + 0x8bec94b1, 0x01440cad, 0x81b41bb2, 0xdbd524d2, 0x5b2533cd, + 0x6f175a12, 0xefe74d0d, 0xb586726d, 0x35766572, 0xdde2a1d3, + 0x5d12b6cc, 0x077389ac, 0x87839eb3, 0xb3b1f76c, 0x3341e073, + 0x6920df13, 0xe9d0c80c, 0xf4809ddf, 0x74708ac0, 0x2e11b5a0, + 0xaee1a2bf, 0x9ad3cb60, 0x1a23dc7f, 0x4042e31f, 0xc0b2f400, + 0x282630a1, 0xa8d627be, 0xf2b718de, 0x72470fc1, 0x4675661e, + 0xc6857101, 0x9ce44e61, 0x1c14597e, 0x96bcc162, 0x164cd67d, + 0x4c2de91d, 0xccddfe02, 0xf8ef97dd, 0x781f80c2, 0x227ebfa2, + 0xa28ea8bd, 0x4a1a6c1c, 0xcaea7b03, 0x908b4463, 0x107b537c, + 0x24493aa3, 0xa4b92dbc, 0xfed812dc, 0x7e2805c3, 0x30f824a5, + 0xb00833ba, 0xea690cda, 0x6a991bc5, 0x5eab721a, 0xde5b6505, + 0x843a5a65, 0x04ca4d7a, 0xec5e89db, 0x6cae9ec4, 0x36cfa1a4, + 0xb63fb6bb, 0x820ddf64, 0x02fdc87b, 0x589cf71b, 0xd86ce004, + 0x52c47818, 0xd2346f07, 0x88555067, 0x08a54778, 0x3c972ea7, + 0xbc6739b8, 0xe60606d8, 0x66f611c7, 0x8e62d566, 0x0e92c279, + 0x54f3fd19, 0xd403ea06, 0xe03183d9, 0x60c194c6, 0x3aa0aba6, + 0xba50bcb9}, + {0x00000000, 0x9570d495, 0xf190af6b, 0x64e07bfe, 0x38505897, + 0xad208c02, 0xc9c0f7fc, 0x5cb02369, 0x70a0b12e, 0xe5d065bb, + 0x81301e45, 0x1440cad0, 0x48f0e9b9, 0xdd803d2c, 0xb96046d2, + 0x2c109247, 0xe141625c, 0x7431b6c9, 0x10d1cd37, 0x85a119a2, + 0xd9113acb, 0x4c61ee5e, 0x288195a0, 0xbdf14135, 0x91e1d372, + 0x049107e7, 0x60717c19, 0xf501a88c, 0xa9b18be5, 0x3cc15f70, + 0x5821248e, 0xcd51f01b, 0x19f3c2f9, 0x8c83166c, 0xe8636d92, + 0x7d13b907, 0x21a39a6e, 0xb4d34efb, 0xd0333505, 0x4543e190, + 0x695373d7, 0xfc23a742, 0x98c3dcbc, 0x0db30829, 0x51032b40, + 0xc473ffd5, 0xa093842b, 0x35e350be, 0xf8b2a0a5, 0x6dc27430, + 0x09220fce, 0x9c52db5b, 0xc0e2f832, 0x55922ca7, 0x31725759, + 0xa40283cc, 0x8812118b, 0x1d62c51e, 0x7982bee0, 0xecf26a75, + 0xb042491c, 0x25329d89, 0x41d2e677, 0xd4a232e2, 0x33e785f2, + 0xa6975167, 0xc2772a99, 0x5707fe0c, 0x0bb7dd65, 0x9ec709f0, + 0xfa27720e, 0x6f57a69b, 0x434734dc, 0xd637e049, 0xb2d79bb7, + 0x27a74f22, 0x7b176c4b, 0xee67b8de, 0x8a87c320, 0x1ff717b5, + 0xd2a6e7ae, 0x47d6333b, 0x233648c5, 0xb6469c50, 0xeaf6bf39, + 0x7f866bac, 0x1b661052, 0x8e16c4c7, 0xa2065680, 0x37768215, + 0x5396f9eb, 0xc6e62d7e, 0x9a560e17, 0x0f26da82, 0x6bc6a17c, + 0xfeb675e9, 0x2a14470b, 0xbf64939e, 0xdb84e860, 0x4ef43cf5, + 0x12441f9c, 0x8734cb09, 0xe3d4b0f7, 0x76a46462, 0x5ab4f625, + 0xcfc422b0, 0xab24594e, 0x3e548ddb, 0x62e4aeb2, 0xf7947a27, + 0x937401d9, 0x0604d54c, 0xcb552557, 0x5e25f1c2, 0x3ac58a3c, + 0xafb55ea9, 0xf3057dc0, 0x6675a955, 0x0295d2ab, 0x97e5063e, + 0xbbf59479, 0x2e8540ec, 0x4a653b12, 0xdf15ef87, 0x83a5ccee, + 0x16d5187b, 0x72356385, 0xe745b710, 0x67cf0be4, 0xf2bfdf71, + 0x965fa48f, 0x032f701a, 0x5f9f5373, 0xcaef87e6, 0xae0ffc18, + 0x3b7f288d, 0x176fbaca, 0x821f6e5f, 0xe6ff15a1, 0x738fc134, + 0x2f3fe25d, 0xba4f36c8, 0xdeaf4d36, 0x4bdf99a3, 0x868e69b8, + 0x13febd2d, 0x771ec6d3, 0xe26e1246, 0xbede312f, 0x2baee5ba, + 0x4f4e9e44, 0xda3e4ad1, 0xf62ed896, 0x635e0c03, 0x07be77fd, + 0x92cea368, 0xce7e8001, 0x5b0e5494, 0x3fee2f6a, 0xaa9efbff, + 0x7e3cc91d, 0xeb4c1d88, 0x8fac6676, 0x1adcb2e3, 0x466c918a, + 0xd31c451f, 0xb7fc3ee1, 0x228cea74, 0x0e9c7833, 0x9becaca6, + 0xff0cd758, 0x6a7c03cd, 0x36cc20a4, 0xa3bcf431, 0xc75c8fcf, + 0x522c5b5a, 0x9f7dab41, 0x0a0d7fd4, 0x6eed042a, 0xfb9dd0bf, + 0xa72df3d6, 0x325d2743, 0x56bd5cbd, 0xc3cd8828, 0xefdd1a6f, + 0x7aadcefa, 0x1e4db504, 0x8b3d6191, 0xd78d42f8, 0x42fd966d, + 0x261ded93, 0xb36d3906, 0x54288e16, 0xc1585a83, 0xa5b8217d, + 0x30c8f5e8, 0x6c78d681, 0xf9080214, 0x9de879ea, 0x0898ad7f, + 0x24883f38, 0xb1f8ebad, 0xd5189053, 0x406844c6, 0x1cd867af, + 0x89a8b33a, 0xed48c8c4, 0x78381c51, 0xb569ec4a, 0x201938df, + 0x44f94321, 0xd18997b4, 0x8d39b4dd, 0x18496048, 0x7ca91bb6, + 0xe9d9cf23, 0xc5c95d64, 0x50b989f1, 0x3459f20f, 0xa129269a, + 0xfd9905f3, 0x68e9d166, 0x0c09aa98, 0x99797e0d, 0x4ddb4cef, + 0xd8ab987a, 0xbc4be384, 0x293b3711, 0x758b1478, 0xe0fbc0ed, + 0x841bbb13, 0x116b6f86, 0x3d7bfdc1, 0xa80b2954, 0xcceb52aa, + 0x599b863f, 0x052ba556, 0x905b71c3, 0xf4bb0a3d, 0x61cbdea8, + 0xac9a2eb3, 0x39eafa26, 0x5d0a81d8, 0xc87a554d, 0x94ca7624, + 0x01baa2b1, 0x655ad94f, 0xf02a0dda, 0xdc3a9f9d, 0x494a4b08, + 0x2daa30f6, 0xb8dae463, 0xe46ac70a, 0x711a139f, 0x15fa6861, + 0x808abcf4}, + {0x00000000, 0xcf9e17c8, 0x444d29d1, 0x8bd33e19, 0x889a53a2, + 0x4704446a, 0xccd77a73, 0x03496dbb, 0xca45a105, 0x05dbb6cd, + 0x8e0888d4, 0x41969f1c, 0x42dff2a7, 0x8d41e56f, 0x0692db76, + 0xc90cccbe, 0x4ffa444b, 0x80645383, 0x0bb76d9a, 0xc4297a52, + 0xc76017e9, 0x08fe0021, 0x832d3e38, 0x4cb329f0, 0x85bfe54e, + 0x4a21f286, 0xc1f2cc9f, 0x0e6cdb57, 0x0d25b6ec, 0xc2bba124, + 0x49689f3d, 0x86f688f5, 0x9ff48896, 0x506a9f5e, 0xdbb9a147, + 0x1427b68f, 0x176edb34, 0xd8f0ccfc, 0x5323f2e5, 0x9cbde52d, + 0x55b12993, 0x9a2f3e5b, 0x11fc0042, 0xde62178a, 0xdd2b7a31, + 0x12b56df9, 0x996653e0, 0x56f84428, 0xd00eccdd, 0x1f90db15, + 0x9443e50c, 0x5bddf2c4, 0x58949f7f, 0x970a88b7, 0x1cd9b6ae, + 0xd347a166, 0x1a4b6dd8, 0xd5d57a10, 0x5e064409, 0x919853c1, + 0x92d13e7a, 0x5d4f29b2, 0xd69c17ab, 0x19020063, 0xe498176d, + 0x2b0600a5, 0xa0d53ebc, 0x6f4b2974, 0x6c0244cf, 0xa39c5307, + 0x284f6d1e, 0xe7d17ad6, 0x2eddb668, 0xe143a1a0, 0x6a909fb9, + 0xa50e8871, 0xa647e5ca, 0x69d9f202, 0xe20acc1b, 0x2d94dbd3, + 0xab625326, 0x64fc44ee, 0xef2f7af7, 0x20b16d3f, 0x23f80084, + 0xec66174c, 0x67b52955, 0xa82b3e9d, 0x6127f223, 0xaeb9e5eb, + 0x256adbf2, 0xeaf4cc3a, 0xe9bda181, 0x2623b649, 0xadf08850, + 0x626e9f98, 0x7b6c9ffb, 0xb4f28833, 0x3f21b62a, 0xf0bfa1e2, + 0xf3f6cc59, 0x3c68db91, 0xb7bbe588, 0x7825f240, 0xb1293efe, + 0x7eb72936, 0xf564172f, 0x3afa00e7, 0x39b36d5c, 0xf62d7a94, + 0x7dfe448d, 0xb2605345, 0x3496dbb0, 0xfb08cc78, 0x70dbf261, + 0xbf45e5a9, 0xbc0c8812, 0x73929fda, 0xf841a1c3, 0x37dfb60b, + 0xfed37ab5, 0x314d6d7d, 0xba9e5364, 0x750044ac, 0x76492917, + 0xb9d73edf, 0x320400c6, 0xfd9a170e, 0x1241289b, 0xdddf3f53, + 0x560c014a, 0x99921682, 0x9adb7b39, 0x55456cf1, 0xde9652e8, + 0x11084520, 0xd804899e, 0x179a9e56, 0x9c49a04f, 0x53d7b787, + 0x509eda3c, 0x9f00cdf4, 0x14d3f3ed, 0xdb4de425, 0x5dbb6cd0, + 0x92257b18, 0x19f64501, 0xd66852c9, 0xd5213f72, 0x1abf28ba, + 0x916c16a3, 0x5ef2016b, 0x97fecdd5, 0x5860da1d, 0xd3b3e404, + 0x1c2df3cc, 0x1f649e77, 0xd0fa89bf, 0x5b29b7a6, 0x94b7a06e, + 0x8db5a00d, 0x422bb7c5, 0xc9f889dc, 0x06669e14, 0x052ff3af, + 0xcab1e467, 0x4162da7e, 0x8efccdb6, 0x47f00108, 0x886e16c0, + 0x03bd28d9, 0xcc233f11, 0xcf6a52aa, 0x00f44562, 0x8b277b7b, + 0x44b96cb3, 0xc24fe446, 0x0dd1f38e, 0x8602cd97, 0x499cda5f, + 0x4ad5b7e4, 0x854ba02c, 0x0e989e35, 0xc10689fd, 0x080a4543, + 0xc794528b, 0x4c476c92, 0x83d97b5a, 0x809016e1, 0x4f0e0129, + 0xc4dd3f30, 0x0b4328f8, 0xf6d93ff6, 0x3947283e, 0xb2941627, + 0x7d0a01ef, 0x7e436c54, 0xb1dd7b9c, 0x3a0e4585, 0xf590524d, + 0x3c9c9ef3, 0xf302893b, 0x78d1b722, 0xb74fa0ea, 0xb406cd51, + 0x7b98da99, 0xf04be480, 0x3fd5f348, 0xb9237bbd, 0x76bd6c75, + 0xfd6e526c, 0x32f045a4, 0x31b9281f, 0xfe273fd7, 0x75f401ce, + 0xba6a1606, 0x7366dab8, 0xbcf8cd70, 0x372bf369, 0xf8b5e4a1, + 0xfbfc891a, 0x34629ed2, 0xbfb1a0cb, 0x702fb703, 0x692db760, + 0xa6b3a0a8, 0x2d609eb1, 0xe2fe8979, 0xe1b7e4c2, 0x2e29f30a, + 0xa5facd13, 0x6a64dadb, 0xa3681665, 0x6cf601ad, 0xe7253fb4, + 0x28bb287c, 0x2bf245c7, 0xe46c520f, 0x6fbf6c16, 0xa0217bde, + 0x26d7f32b, 0xe949e4e3, 0x629adafa, 0xad04cd32, 0xae4da089, + 0x61d3b741, 0xea008958, 0x259e9e90, 0xec92522e, 0x230c45e6, + 0xa8df7bff, 0x67416c37, 0x6408018c, 0xab961644, 0x2045285d, + 0xefdb3f95}, + {0x00000000, 0x24825136, 0x4904a26c, 0x6d86f35a, 0x920944d8, + 0xb68b15ee, 0xdb0de6b4, 0xff8fb782, 0xff638ff1, 0xdbe1dec7, + 0xb6672d9d, 0x92e57cab, 0x6d6acb29, 0x49e89a1f, 0x246e6945, + 0x00ec3873, 0x25b619a3, 0x01344895, 0x6cb2bbcf, 0x4830eaf9, + 0xb7bf5d7b, 0x933d0c4d, 0xfebbff17, 0xda39ae21, 0xdad59652, + 0xfe57c764, 0x93d1343e, 0xb7536508, 0x48dcd28a, 0x6c5e83bc, + 0x01d870e6, 0x255a21d0, 0x4b6c3346, 0x6fee6270, 0x0268912a, + 0x26eac01c, 0xd965779e, 0xfde726a8, 0x9061d5f2, 0xb4e384c4, + 0xb40fbcb7, 0x908ded81, 0xfd0b1edb, 0xd9894fed, 0x2606f86f, + 0x0284a959, 0x6f025a03, 0x4b800b35, 0x6eda2ae5, 0x4a587bd3, + 0x27de8889, 0x035cd9bf, 0xfcd36e3d, 0xd8513f0b, 0xb5d7cc51, + 0x91559d67, 0x91b9a514, 0xb53bf422, 0xd8bd0778, 0xfc3f564e, + 0x03b0e1cc, 0x2732b0fa, 0x4ab443a0, 0x6e361296, 0x96d8668c, + 0xb25a37ba, 0xdfdcc4e0, 0xfb5e95d6, 0x04d12254, 0x20537362, + 0x4dd58038, 0x6957d10e, 0x69bbe97d, 0x4d39b84b, 0x20bf4b11, + 0x043d1a27, 0xfbb2ada5, 0xdf30fc93, 0xb2b60fc9, 0x96345eff, + 0xb36e7f2f, 0x97ec2e19, 0xfa6add43, 0xdee88c75, 0x21673bf7, + 0x05e56ac1, 0x6863999b, 0x4ce1c8ad, 0x4c0df0de, 0x688fa1e8, + 0x050952b2, 0x218b0384, 0xde04b406, 0xfa86e530, 0x9700166a, + 0xb382475c, 0xddb455ca, 0xf93604fc, 0x94b0f7a6, 0xb032a690, + 0x4fbd1112, 0x6b3f4024, 0x06b9b37e, 0x223be248, 0x22d7da3b, + 0x06558b0d, 0x6bd37857, 0x4f512961, 0xb0de9ee3, 0x945ccfd5, + 0xf9da3c8f, 0xdd586db9, 0xf8024c69, 0xdc801d5f, 0xb106ee05, + 0x9584bf33, 0x6a0b08b1, 0x4e895987, 0x230faadd, 0x078dfbeb, + 0x0761c398, 0x23e392ae, 0x4e6561f4, 0x6ae730c2, 0x95688740, + 0xb1ead676, 0xdc6c252c, 0xf8ee741a, 0xf6c1cb59, 0xd2439a6f, + 0xbfc56935, 0x9b473803, 0x64c88f81, 0x404adeb7, 0x2dcc2ded, + 0x094e7cdb, 0x09a244a8, 0x2d20159e, 0x40a6e6c4, 0x6424b7f2, + 0x9bab0070, 0xbf295146, 0xd2afa21c, 0xf62df32a, 0xd377d2fa, + 0xf7f583cc, 0x9a737096, 0xbef121a0, 0x417e9622, 0x65fcc714, + 0x087a344e, 0x2cf86578, 0x2c145d0b, 0x08960c3d, 0x6510ff67, + 0x4192ae51, 0xbe1d19d3, 0x9a9f48e5, 0xf719bbbf, 0xd39bea89, + 0xbdadf81f, 0x992fa929, 0xf4a95a73, 0xd02b0b45, 0x2fa4bcc7, + 0x0b26edf1, 0x66a01eab, 0x42224f9d, 0x42ce77ee, 0x664c26d8, + 0x0bcad582, 0x2f4884b4, 0xd0c73336, 0xf4456200, 0x99c3915a, + 0xbd41c06c, 0x981be1bc, 0xbc99b08a, 0xd11f43d0, 0xf59d12e6, + 0x0a12a564, 0x2e90f452, 0x43160708, 0x6794563e, 0x67786e4d, + 0x43fa3f7b, 0x2e7ccc21, 0x0afe9d17, 0xf5712a95, 0xd1f37ba3, + 0xbc7588f9, 0x98f7d9cf, 0x6019add5, 0x449bfce3, 0x291d0fb9, + 0x0d9f5e8f, 0xf210e90d, 0xd692b83b, 0xbb144b61, 0x9f961a57, + 0x9f7a2224, 0xbbf87312, 0xd67e8048, 0xf2fcd17e, 0x0d7366fc, + 0x29f137ca, 0x4477c490, 0x60f595a6, 0x45afb476, 0x612de540, + 0x0cab161a, 0x2829472c, 0xd7a6f0ae, 0xf324a198, 0x9ea252c2, + 0xba2003f4, 0xbacc3b87, 0x9e4e6ab1, 0xf3c899eb, 0xd74ac8dd, + 0x28c57f5f, 0x0c472e69, 0x61c1dd33, 0x45438c05, 0x2b759e93, + 0x0ff7cfa5, 0x62713cff, 0x46f36dc9, 0xb97cda4b, 0x9dfe8b7d, + 0xf0787827, 0xd4fa2911, 0xd4161162, 0xf0944054, 0x9d12b30e, + 0xb990e238, 0x461f55ba, 0x629d048c, 0x0f1bf7d6, 0x2b99a6e0, + 0x0ec38730, 0x2a41d606, 0x47c7255c, 0x6345746a, 0x9ccac3e8, + 0xb84892de, 0xd5ce6184, 0xf14c30b2, 0xf1a008c1, 0xd52259f7, + 0xb8a4aaad, 0x9c26fb9b, 0x63a94c19, 0x472b1d2f, 0x2aadee75, + 0x0e2fbf43}, + {0x00000000, 0x36f290f3, 0x6de521e6, 0x5b17b115, 0xdbca43cc, + 0xed38d33f, 0xb62f622a, 0x80ddf2d9, 0x6ce581d9, 0x5a17112a, + 0x0100a03f, 0x37f230cc, 0xb72fc215, 0x81dd52e6, 0xdacae3f3, + 0xec387300, 0xd9cb03b2, 0xef399341, 0xb42e2254, 0x82dcb2a7, + 0x0201407e, 0x34f3d08d, 0x6fe46198, 0x5916f16b, 0xb52e826b, + 0x83dc1298, 0xd8cba38d, 0xee39337e, 0x6ee4c1a7, 0x58165154, + 0x0301e041, 0x35f370b2, 0x68e70125, 0x5e1591d6, 0x050220c3, + 0x33f0b030, 0xb32d42e9, 0x85dfd21a, 0xdec8630f, 0xe83af3fc, + 0x040280fc, 0x32f0100f, 0x69e7a11a, 0x5f1531e9, 0xdfc8c330, + 0xe93a53c3, 0xb22de2d6, 0x84df7225, 0xb12c0297, 0x87de9264, + 0xdcc92371, 0xea3bb382, 0x6ae6415b, 0x5c14d1a8, 0x070360bd, + 0x31f1f04e, 0xddc9834e, 0xeb3b13bd, 0xb02ca2a8, 0x86de325b, + 0x0603c082, 0x30f15071, 0x6be6e164, 0x5d147197, 0xd1ce024a, + 0xe73c92b9, 0xbc2b23ac, 0x8ad9b35f, 0x0a044186, 0x3cf6d175, + 0x67e16060, 0x5113f093, 0xbd2b8393, 0x8bd91360, 0xd0cea275, + 0xe63c3286, 0x66e1c05f, 0x501350ac, 0x0b04e1b9, 0x3df6714a, + 0x080501f8, 0x3ef7910b, 0x65e0201e, 0x5312b0ed, 0xd3cf4234, + 0xe53dd2c7, 0xbe2a63d2, 0x88d8f321, 0x64e08021, 0x521210d2, + 0x0905a1c7, 0x3ff73134, 0xbf2ac3ed, 0x89d8531e, 0xd2cfe20b, + 0xe43d72f8, 0xb929036f, 0x8fdb939c, 0xd4cc2289, 0xe23eb27a, + 0x62e340a3, 0x5411d050, 0x0f066145, 0x39f4f1b6, 0xd5cc82b6, + 0xe33e1245, 0xb829a350, 0x8edb33a3, 0x0e06c17a, 0x38f45189, + 0x63e3e09c, 0x5511706f, 0x60e200dd, 0x5610902e, 0x0d07213b, + 0x3bf5b1c8, 0xbb284311, 0x8ddad3e2, 0xd6cd62f7, 0xe03ff204, + 0x0c078104, 0x3af511f7, 0x61e2a0e2, 0x57103011, 0xd7cdc2c8, + 0xe13f523b, 0xba28e32e, 0x8cda73dd, 0x78ed02d5, 0x4e1f9226, + 0x15082333, 0x23fab3c0, 0xa3274119, 0x95d5d1ea, 0xcec260ff, + 0xf830f00c, 0x1408830c, 0x22fa13ff, 0x79eda2ea, 0x4f1f3219, + 0xcfc2c0c0, 0xf9305033, 0xa227e126, 0x94d571d5, 0xa1260167, + 0x97d49194, 0xccc32081, 0xfa31b072, 0x7aec42ab, 0x4c1ed258, + 0x1709634d, 0x21fbf3be, 0xcdc380be, 0xfb31104d, 0xa026a158, + 0x96d431ab, 0x1609c372, 0x20fb5381, 0x7bece294, 0x4d1e7267, + 0x100a03f0, 0x26f89303, 0x7def2216, 0x4b1db2e5, 0xcbc0403c, + 0xfd32d0cf, 0xa62561da, 0x90d7f129, 0x7cef8229, 0x4a1d12da, + 0x110aa3cf, 0x27f8333c, 0xa725c1e5, 0x91d75116, 0xcac0e003, + 0xfc3270f0, 0xc9c10042, 0xff3390b1, 0xa42421a4, 0x92d6b157, + 0x120b438e, 0x24f9d37d, 0x7fee6268, 0x491cf29b, 0xa524819b, + 0x93d61168, 0xc8c1a07d, 0xfe33308e, 0x7eeec257, 0x481c52a4, + 0x130be3b1, 0x25f97342, 0xa923009f, 0x9fd1906c, 0xc4c62179, + 0xf234b18a, 0x72e94353, 0x441bd3a0, 0x1f0c62b5, 0x29fef246, + 0xc5c68146, 0xf33411b5, 0xa823a0a0, 0x9ed13053, 0x1e0cc28a, + 0x28fe5279, 0x73e9e36c, 0x451b739f, 0x70e8032d, 0x461a93de, + 0x1d0d22cb, 0x2bffb238, 0xab2240e1, 0x9dd0d012, 0xc6c76107, + 0xf035f1f4, 0x1c0d82f4, 0x2aff1207, 0x71e8a312, 0x471a33e1, + 0xc7c7c138, 0xf13551cb, 0xaa22e0de, 0x9cd0702d, 0xc1c401ba, + 0xf7369149, 0xac21205c, 0x9ad3b0af, 0x1a0e4276, 0x2cfcd285, + 0x77eb6390, 0x4119f363, 0xad218063, 0x9bd31090, 0xc0c4a185, + 0xf6363176, 0x76ebc3af, 0x4019535c, 0x1b0ee249, 0x2dfc72ba, + 0x180f0208, 0x2efd92fb, 0x75ea23ee, 0x4318b31d, 0xc3c541c4, + 0xf537d137, 0xae206022, 0x98d2f0d1, 0x74ea83d1, 0x42181322, + 0x190fa237, 0x2ffd32c4, 0xaf20c01d, 0x99d250ee, 0xc2c5e1fb, + 0xf4377108}}; + +local const z_word_t FAR crc_braid_big_table[][256] = { + {0x0000000000000000, 0xf390f23600000000, 0xe621e56d00000000, + 0x15b1175b00000000, 0xcc43cadb00000000, 0x3fd338ed00000000, + 0x2a622fb600000000, 0xd9f2dd8000000000, 0xd981e56c00000000, + 0x2a11175a00000000, 0x3fa0000100000000, 0xcc30f23700000000, + 0x15c22fb700000000, 0xe652dd8100000000, 0xf3e3cada00000000, + 0x007338ec00000000, 0xb203cbd900000000, 0x419339ef00000000, + 0x54222eb400000000, 0xa7b2dc8200000000, 0x7e40010200000000, + 0x8dd0f33400000000, 0x9861e46f00000000, 0x6bf1165900000000, + 0x6b822eb500000000, 0x9812dc8300000000, 0x8da3cbd800000000, + 0x7e3339ee00000000, 0xa7c1e46e00000000, 0x5451165800000000, + 0x41e0010300000000, 0xb270f33500000000, 0x2501e76800000000, + 0xd691155e00000000, 0xc320020500000000, 0x30b0f03300000000, + 0xe9422db300000000, 0x1ad2df8500000000, 0x0f63c8de00000000, + 0xfcf33ae800000000, 0xfc80020400000000, 0x0f10f03200000000, + 0x1aa1e76900000000, 0xe931155f00000000, 0x30c3c8df00000000, + 0xc3533ae900000000, 0xd6e22db200000000, 0x2572df8400000000, + 0x97022cb100000000, 0x6492de8700000000, 0x7123c9dc00000000, + 0x82b33bea00000000, 0x5b41e66a00000000, 0xa8d1145c00000000, + 0xbd60030700000000, 0x4ef0f13100000000, 0x4e83c9dd00000000, + 0xbd133beb00000000, 0xa8a22cb000000000, 0x5b32de8600000000, + 0x82c0030600000000, 0x7150f13000000000, 0x64e1e66b00000000, + 0x9771145d00000000, 0x4a02ced100000000, 0xb9923ce700000000, + 0xac232bbc00000000, 0x5fb3d98a00000000, 0x8641040a00000000, + 0x75d1f63c00000000, 0x6060e16700000000, 0x93f0135100000000, + 0x93832bbd00000000, 0x6013d98b00000000, 0x75a2ced000000000, + 0x86323ce600000000, 0x5fc0e16600000000, 0xac50135000000000, + 0xb9e1040b00000000, 0x4a71f63d00000000, 0xf801050800000000, + 0x0b91f73e00000000, 0x1e20e06500000000, 0xedb0125300000000, + 0x3442cfd300000000, 0xc7d23de500000000, 0xd2632abe00000000, + 0x21f3d88800000000, 0x2180e06400000000, 0xd210125200000000, + 0xc7a1050900000000, 0x3431f73f00000000, 0xedc32abf00000000, + 0x1e53d88900000000, 0x0be2cfd200000000, 0xf8723de400000000, + 0x6f0329b900000000, 0x9c93db8f00000000, 0x8922ccd400000000, + 0x7ab23ee200000000, 0xa340e36200000000, 0x50d0115400000000, + 0x4561060f00000000, 0xb6f1f43900000000, 0xb682ccd500000000, + 0x45123ee300000000, 0x50a329b800000000, 0xa333db8e00000000, + 0x7ac1060e00000000, 0x8951f43800000000, 0x9ce0e36300000000, + 0x6f70115500000000, 0xdd00e26000000000, 0x2e90105600000000, + 0x3b21070d00000000, 0xc8b1f53b00000000, 0x114328bb00000000, + 0xe2d3da8d00000000, 0xf762cdd600000000, 0x04f23fe000000000, + 0x0481070c00000000, 0xf711f53a00000000, 0xe2a0e26100000000, + 0x1130105700000000, 0xc8c2cdd700000000, 0x3b523fe100000000, + 0x2ee328ba00000000, 0xdd73da8c00000000, 0xd502ed7800000000, + 0x26921f4e00000000, 0x3323081500000000, 0xc0b3fa2300000000, + 0x194127a300000000, 0xead1d59500000000, 0xff60c2ce00000000, + 0x0cf030f800000000, 0x0c83081400000000, 0xff13fa2200000000, + 0xeaa2ed7900000000, 0x19321f4f00000000, 0xc0c0c2cf00000000, + 0x335030f900000000, 0x26e127a200000000, 0xd571d59400000000, + 0x670126a100000000, 0x9491d49700000000, 0x8120c3cc00000000, + 0x72b031fa00000000, 0xab42ec7a00000000, 0x58d21e4c00000000, + 0x4d63091700000000, 0xbef3fb2100000000, 0xbe80c3cd00000000, + 0x4d1031fb00000000, 0x58a126a000000000, 0xab31d49600000000, + 0x72c3091600000000, 0x8153fb2000000000, 0x94e2ec7b00000000, + 0x67721e4d00000000, 0xf0030a1000000000, 0x0393f82600000000, + 0x1622ef7d00000000, 0xe5b21d4b00000000, 0x3c40c0cb00000000, + 0xcfd032fd00000000, 0xda6125a600000000, 0x29f1d79000000000, + 0x2982ef7c00000000, 0xda121d4a00000000, 0xcfa30a1100000000, + 0x3c33f82700000000, 0xe5c125a700000000, 0x1651d79100000000, + 0x03e0c0ca00000000, 0xf07032fc00000000, 0x4200c1c900000000, + 0xb19033ff00000000, 0xa42124a400000000, 0x57b1d69200000000, + 0x8e430b1200000000, 0x7dd3f92400000000, 0x6862ee7f00000000, + 0x9bf21c4900000000, 0x9b8124a500000000, 0x6811d69300000000, + 0x7da0c1c800000000, 0x8e3033fe00000000, 0x57c2ee7e00000000, + 0xa4521c4800000000, 0xb1e30b1300000000, 0x4273f92500000000, + 0x9f0023a900000000, 0x6c90d19f00000000, 0x7921c6c400000000, + 0x8ab134f200000000, 0x5343e97200000000, 0xa0d31b4400000000, + 0xb5620c1f00000000, 0x46f2fe2900000000, 0x4681c6c500000000, + 0xb51134f300000000, 0xa0a023a800000000, 0x5330d19e00000000, + 0x8ac20c1e00000000, 0x7952fe2800000000, 0x6ce3e97300000000, + 0x9f731b4500000000, 0x2d03e87000000000, 0xde931a4600000000, + 0xcb220d1d00000000, 0x38b2ff2b00000000, 0xe14022ab00000000, + 0x12d0d09d00000000, 0x0761c7c600000000, 0xf4f135f000000000, + 0xf4820d1c00000000, 0x0712ff2a00000000, 0x12a3e87100000000, + 0xe1331a4700000000, 0x38c1c7c700000000, 0xcb5135f100000000, + 0xdee022aa00000000, 0x2d70d09c00000000, 0xba01c4c100000000, + 0x499136f700000000, 0x5c2021ac00000000, 0xafb0d39a00000000, + 0x76420e1a00000000, 0x85d2fc2c00000000, 0x9063eb7700000000, + 0x63f3194100000000, 0x638021ad00000000, 0x9010d39b00000000, + 0x85a1c4c000000000, 0x763136f600000000, 0xafc3eb7600000000, + 0x5c53194000000000, 0x49e20e1b00000000, 0xba72fc2d00000000, + 0x08020f1800000000, 0xfb92fd2e00000000, 0xee23ea7500000000, + 0x1db3184300000000, 0xc441c5c300000000, 0x37d137f500000000, + 0x226020ae00000000, 0xd1f0d29800000000, 0xd183ea7400000000, + 0x2213184200000000, 0x37a20f1900000000, 0xc432fd2f00000000, + 0x1dc020af00000000, 0xee50d29900000000, 0xfbe1c5c200000000, + 0x087137f400000000}, + {0x0000000000000000, 0x3651822400000000, 0x6ca2044900000000, + 0x5af3866d00000000, 0xd844099200000000, 0xee158bb600000000, + 0xb4e60ddb00000000, 0x82b78fff00000000, 0xf18f63ff00000000, + 0xc7dee1db00000000, 0x9d2d67b600000000, 0xab7ce59200000000, + 0x29cb6a6d00000000, 0x1f9ae84900000000, 0x45696e2400000000, + 0x7338ec0000000000, 0xa319b62500000000, 0x9548340100000000, + 0xcfbbb26c00000000, 0xf9ea304800000000, 0x7b5dbfb700000000, + 0x4d0c3d9300000000, 0x17ffbbfe00000000, 0x21ae39da00000000, + 0x5296d5da00000000, 0x64c757fe00000000, 0x3e34d19300000000, + 0x086553b700000000, 0x8ad2dc4800000000, 0xbc835e6c00000000, + 0xe670d80100000000, 0xd0215a2500000000, 0x46336c4b00000000, + 0x7062ee6f00000000, 0x2a91680200000000, 0x1cc0ea2600000000, + 0x9e7765d900000000, 0xa826e7fd00000000, 0xf2d5619000000000, + 0xc484e3b400000000, 0xb7bc0fb400000000, 0x81ed8d9000000000, + 0xdb1e0bfd00000000, 0xed4f89d900000000, 0x6ff8062600000000, + 0x59a9840200000000, 0x035a026f00000000, 0x350b804b00000000, + 0xe52ada6e00000000, 0xd37b584a00000000, 0x8988de2700000000, + 0xbfd95c0300000000, 0x3d6ed3fc00000000, 0x0b3f51d800000000, + 0x51ccd7b500000000, 0x679d559100000000, 0x14a5b99100000000, + 0x22f43bb500000000, 0x7807bdd800000000, 0x4e563ffc00000000, + 0xcce1b00300000000, 0xfab0322700000000, 0xa043b44a00000000, + 0x9612366e00000000, 0x8c66d89600000000, 0xba375ab200000000, + 0xe0c4dcdf00000000, 0xd6955efb00000000, 0x5422d10400000000, + 0x6273532000000000, 0x3880d54d00000000, 0x0ed1576900000000, + 0x7de9bb6900000000, 0x4bb8394d00000000, 0x114bbf2000000000, + 0x271a3d0400000000, 0xa5adb2fb00000000, 0x93fc30df00000000, + 0xc90fb6b200000000, 0xff5e349600000000, 0x2f7f6eb300000000, + 0x192eec9700000000, 0x43dd6afa00000000, 0x758ce8de00000000, + 0xf73b672100000000, 0xc16ae50500000000, 0x9b99636800000000, + 0xadc8e14c00000000, 0xdef00d4c00000000, 0xe8a18f6800000000, + 0xb252090500000000, 0x84038b2100000000, 0x06b404de00000000, + 0x30e586fa00000000, 0x6a16009700000000, 0x5c4782b300000000, + 0xca55b4dd00000000, 0xfc0436f900000000, 0xa6f7b09400000000, + 0x90a632b000000000, 0x1211bd4f00000000, 0x24403f6b00000000, + 0x7eb3b90600000000, 0x48e23b2200000000, 0x3bdad72200000000, + 0x0d8b550600000000, 0x5778d36b00000000, 0x6129514f00000000, + 0xe39edeb000000000, 0xd5cf5c9400000000, 0x8f3cdaf900000000, + 0xb96d58dd00000000, 0x694c02f800000000, 0x5f1d80dc00000000, + 0x05ee06b100000000, 0x33bf849500000000, 0xb1080b6a00000000, + 0x8759894e00000000, 0xddaa0f2300000000, 0xebfb8d0700000000, + 0x98c3610700000000, 0xae92e32300000000, 0xf461654e00000000, + 0xc230e76a00000000, 0x4087689500000000, 0x76d6eab100000000, + 0x2c256cdc00000000, 0x1a74eef800000000, 0x59cbc1f600000000, + 0x6f9a43d200000000, 0x3569c5bf00000000, 0x0338479b00000000, + 0x818fc86400000000, 0xb7de4a4000000000, 0xed2dcc2d00000000, + 0xdb7c4e0900000000, 0xa844a20900000000, 0x9e15202d00000000, + 0xc4e6a64000000000, 0xf2b7246400000000, 0x7000ab9b00000000, + 0x465129bf00000000, 0x1ca2afd200000000, 0x2af32df600000000, + 0xfad277d300000000, 0xcc83f5f700000000, 0x9670739a00000000, + 0xa021f1be00000000, 0x22967e4100000000, 0x14c7fc6500000000, + 0x4e347a0800000000, 0x7865f82c00000000, 0x0b5d142c00000000, + 0x3d0c960800000000, 0x67ff106500000000, 0x51ae924100000000, + 0xd3191dbe00000000, 0xe5489f9a00000000, 0xbfbb19f700000000, + 0x89ea9bd300000000, 0x1ff8adbd00000000, 0x29a92f9900000000, + 0x735aa9f400000000, 0x450b2bd000000000, 0xc7bca42f00000000, + 0xf1ed260b00000000, 0xab1ea06600000000, 0x9d4f224200000000, + 0xee77ce4200000000, 0xd8264c6600000000, 0x82d5ca0b00000000, + 0xb484482f00000000, 0x3633c7d000000000, 0x006245f400000000, + 0x5a91c39900000000, 0x6cc041bd00000000, 0xbce11b9800000000, + 0x8ab099bc00000000, 0xd0431fd100000000, 0xe6129df500000000, + 0x64a5120a00000000, 0x52f4902e00000000, 0x0807164300000000, + 0x3e56946700000000, 0x4d6e786700000000, 0x7b3ffa4300000000, + 0x21cc7c2e00000000, 0x179dfe0a00000000, 0x952a71f500000000, + 0xa37bf3d100000000, 0xf98875bc00000000, 0xcfd9f79800000000, + 0xd5ad196000000000, 0xe3fc9b4400000000, 0xb90f1d2900000000, + 0x8f5e9f0d00000000, 0x0de910f200000000, 0x3bb892d600000000, + 0x614b14bb00000000, 0x571a969f00000000, 0x24227a9f00000000, + 0x1273f8bb00000000, 0x48807ed600000000, 0x7ed1fcf200000000, + 0xfc66730d00000000, 0xca37f12900000000, 0x90c4774400000000, + 0xa695f56000000000, 0x76b4af4500000000, 0x40e52d6100000000, + 0x1a16ab0c00000000, 0x2c47292800000000, 0xaef0a6d700000000, + 0x98a124f300000000, 0xc252a29e00000000, 0xf40320ba00000000, + 0x873bccba00000000, 0xb16a4e9e00000000, 0xeb99c8f300000000, + 0xddc84ad700000000, 0x5f7fc52800000000, 0x692e470c00000000, + 0x33ddc16100000000, 0x058c434500000000, 0x939e752b00000000, + 0xa5cff70f00000000, 0xff3c716200000000, 0xc96df34600000000, + 0x4bda7cb900000000, 0x7d8bfe9d00000000, 0x277878f000000000, + 0x1129fad400000000, 0x621116d400000000, 0x544094f000000000, + 0x0eb3129d00000000, 0x38e290b900000000, 0xba551f4600000000, + 0x8c049d6200000000, 0xd6f71b0f00000000, 0xe0a6992b00000000, + 0x3087c30e00000000, 0x06d6412a00000000, 0x5c25c74700000000, + 0x6a74456300000000, 0xe8c3ca9c00000000, 0xde9248b800000000, + 0x8461ced500000000, 0xb2304cf100000000, 0xc108a0f100000000, + 0xf75922d500000000, 0xadaaa4b800000000, 0x9bfb269c00000000, + 0x194ca96300000000, 0x2f1d2b4700000000, 0x75eead2a00000000, + 0x43bf2f0e00000000}, + {0x0000000000000000, 0xc8179ecf00000000, 0xd1294d4400000000, + 0x193ed38b00000000, 0xa2539a8800000000, 0x6a44044700000000, + 0x737ad7cc00000000, 0xbb6d490300000000, 0x05a145ca00000000, + 0xcdb6db0500000000, 0xd488088e00000000, 0x1c9f964100000000, + 0xa7f2df4200000000, 0x6fe5418d00000000, 0x76db920600000000, + 0xbecc0cc900000000, 0x4b44fa4f00000000, 0x8353648000000000, + 0x9a6db70b00000000, 0x527a29c400000000, 0xe91760c700000000, + 0x2100fe0800000000, 0x383e2d8300000000, 0xf029b34c00000000, + 0x4ee5bf8500000000, 0x86f2214a00000000, 0x9fccf2c100000000, + 0x57db6c0e00000000, 0xecb6250d00000000, 0x24a1bbc200000000, + 0x3d9f684900000000, 0xf588f68600000000, 0x9688f49f00000000, + 0x5e9f6a5000000000, 0x47a1b9db00000000, 0x8fb6271400000000, + 0x34db6e1700000000, 0xfcccf0d800000000, 0xe5f2235300000000, + 0x2de5bd9c00000000, 0x9329b15500000000, 0x5b3e2f9a00000000, + 0x4200fc1100000000, 0x8a1762de00000000, 0x317a2bdd00000000, + 0xf96db51200000000, 0xe053669900000000, 0x2844f85600000000, + 0xddcc0ed000000000, 0x15db901f00000000, 0x0ce5439400000000, + 0xc4f2dd5b00000000, 0x7f9f945800000000, 0xb7880a9700000000, + 0xaeb6d91c00000000, 0x66a147d300000000, 0xd86d4b1a00000000, + 0x107ad5d500000000, 0x0944065e00000000, 0xc153989100000000, + 0x7a3ed19200000000, 0xb2294f5d00000000, 0xab179cd600000000, + 0x6300021900000000, 0x6d1798e400000000, 0xa500062b00000000, + 0xbc3ed5a000000000, 0x74294b6f00000000, 0xcf44026c00000000, + 0x07539ca300000000, 0x1e6d4f2800000000, 0xd67ad1e700000000, + 0x68b6dd2e00000000, 0xa0a143e100000000, 0xb99f906a00000000, + 0x71880ea500000000, 0xcae547a600000000, 0x02f2d96900000000, + 0x1bcc0ae200000000, 0xd3db942d00000000, 0x265362ab00000000, + 0xee44fc6400000000, 0xf77a2fef00000000, 0x3f6db12000000000, + 0x8400f82300000000, 0x4c1766ec00000000, 0x5529b56700000000, + 0x9d3e2ba800000000, 0x23f2276100000000, 0xebe5b9ae00000000, + 0xf2db6a2500000000, 0x3accf4ea00000000, 0x81a1bde900000000, + 0x49b6232600000000, 0x5088f0ad00000000, 0x989f6e6200000000, + 0xfb9f6c7b00000000, 0x3388f2b400000000, 0x2ab6213f00000000, + 0xe2a1bff000000000, 0x59ccf6f300000000, 0x91db683c00000000, + 0x88e5bbb700000000, 0x40f2257800000000, 0xfe3e29b100000000, + 0x3629b77e00000000, 0x2f1764f500000000, 0xe700fa3a00000000, + 0x5c6db33900000000, 0x947a2df600000000, 0x8d44fe7d00000000, + 0x455360b200000000, 0xb0db963400000000, 0x78cc08fb00000000, + 0x61f2db7000000000, 0xa9e545bf00000000, 0x12880cbc00000000, + 0xda9f927300000000, 0xc3a141f800000000, 0x0bb6df3700000000, + 0xb57ad3fe00000000, 0x7d6d4d3100000000, 0x64539eba00000000, + 0xac44007500000000, 0x1729497600000000, 0xdf3ed7b900000000, + 0xc600043200000000, 0x0e179afd00000000, 0x9b28411200000000, + 0x533fdfdd00000000, 0x4a010c5600000000, 0x8216929900000000, + 0x397bdb9a00000000, 0xf16c455500000000, 0xe85296de00000000, + 0x2045081100000000, 0x9e8904d800000000, 0x569e9a1700000000, + 0x4fa0499c00000000, 0x87b7d75300000000, 0x3cda9e5000000000, + 0xf4cd009f00000000, 0xedf3d31400000000, 0x25e44ddb00000000, + 0xd06cbb5d00000000, 0x187b259200000000, 0x0145f61900000000, + 0xc95268d600000000, 0x723f21d500000000, 0xba28bf1a00000000, + 0xa3166c9100000000, 0x6b01f25e00000000, 0xd5cdfe9700000000, + 0x1dda605800000000, 0x04e4b3d300000000, 0xccf32d1c00000000, + 0x779e641f00000000, 0xbf89fad000000000, 0xa6b7295b00000000, + 0x6ea0b79400000000, 0x0da0b58d00000000, 0xc5b72b4200000000, + 0xdc89f8c900000000, 0x149e660600000000, 0xaff32f0500000000, + 0x67e4b1ca00000000, 0x7eda624100000000, 0xb6cdfc8e00000000, + 0x0801f04700000000, 0xc0166e8800000000, 0xd928bd0300000000, + 0x113f23cc00000000, 0xaa526acf00000000, 0x6245f40000000000, + 0x7b7b278b00000000, 0xb36cb94400000000, 0x46e44fc200000000, + 0x8ef3d10d00000000, 0x97cd028600000000, 0x5fda9c4900000000, + 0xe4b7d54a00000000, 0x2ca04b8500000000, 0x359e980e00000000, + 0xfd8906c100000000, 0x43450a0800000000, 0x8b5294c700000000, + 0x926c474c00000000, 0x5a7bd98300000000, 0xe116908000000000, + 0x29010e4f00000000, 0x303fddc400000000, 0xf828430b00000000, + 0xf63fd9f600000000, 0x3e28473900000000, 0x271694b200000000, + 0xef010a7d00000000, 0x546c437e00000000, 0x9c7bddb100000000, + 0x85450e3a00000000, 0x4d5290f500000000, 0xf39e9c3c00000000, + 0x3b8902f300000000, 0x22b7d17800000000, 0xeaa04fb700000000, + 0x51cd06b400000000, 0x99da987b00000000, 0x80e44bf000000000, + 0x48f3d53f00000000, 0xbd7b23b900000000, 0x756cbd7600000000, + 0x6c526efd00000000, 0xa445f03200000000, 0x1f28b93100000000, + 0xd73f27fe00000000, 0xce01f47500000000, 0x06166aba00000000, + 0xb8da667300000000, 0x70cdf8bc00000000, 0x69f32b3700000000, + 0xa1e4b5f800000000, 0x1a89fcfb00000000, 0xd29e623400000000, + 0xcba0b1bf00000000, 0x03b72f7000000000, 0x60b72d6900000000, + 0xa8a0b3a600000000, 0xb19e602d00000000, 0x7989fee200000000, + 0xc2e4b7e100000000, 0x0af3292e00000000, 0x13cdfaa500000000, + 0xdbda646a00000000, 0x651668a300000000, 0xad01f66c00000000, + 0xb43f25e700000000, 0x7c28bb2800000000, 0xc745f22b00000000, + 0x0f526ce400000000, 0x166cbf6f00000000, 0xde7b21a000000000, + 0x2bf3d72600000000, 0xe3e449e900000000, 0xfada9a6200000000, + 0x32cd04ad00000000, 0x89a04dae00000000, 0x41b7d36100000000, + 0x588900ea00000000, 0x909e9e2500000000, 0x2e5292ec00000000, + 0xe6450c2300000000, 0xff7bdfa800000000, 0x376c416700000000, + 0x8c01086400000000, 0x441696ab00000000, 0x5d28452000000000, + 0x953fdbef00000000}, + {0x0000000000000000, 0x95d4709500000000, 0x6baf90f100000000, + 0xfe7be06400000000, 0x9758503800000000, 0x028c20ad00000000, + 0xfcf7c0c900000000, 0x6923b05c00000000, 0x2eb1a07000000000, + 0xbb65d0e500000000, 0x451e308100000000, 0xd0ca401400000000, + 0xb9e9f04800000000, 0x2c3d80dd00000000, 0xd24660b900000000, + 0x4792102c00000000, 0x5c6241e100000000, 0xc9b6317400000000, + 0x37cdd11000000000, 0xa219a18500000000, 0xcb3a11d900000000, + 0x5eee614c00000000, 0xa095812800000000, 0x3541f1bd00000000, + 0x72d3e19100000000, 0xe707910400000000, 0x197c716000000000, + 0x8ca801f500000000, 0xe58bb1a900000000, 0x705fc13c00000000, + 0x8e24215800000000, 0x1bf051cd00000000, 0xf9c2f31900000000, + 0x6c16838c00000000, 0x926d63e800000000, 0x07b9137d00000000, + 0x6e9aa32100000000, 0xfb4ed3b400000000, 0x053533d000000000, + 0x90e1434500000000, 0xd773536900000000, 0x42a723fc00000000, + 0xbcdcc39800000000, 0x2908b30d00000000, 0x402b035100000000, + 0xd5ff73c400000000, 0x2b8493a000000000, 0xbe50e33500000000, + 0xa5a0b2f800000000, 0x3074c26d00000000, 0xce0f220900000000, + 0x5bdb529c00000000, 0x32f8e2c000000000, 0xa72c925500000000, + 0x5957723100000000, 0xcc8302a400000000, 0x8b11128800000000, + 0x1ec5621d00000000, 0xe0be827900000000, 0x756af2ec00000000, + 0x1c4942b000000000, 0x899d322500000000, 0x77e6d24100000000, + 0xe232a2d400000000, 0xf285e73300000000, 0x675197a600000000, + 0x992a77c200000000, 0x0cfe075700000000, 0x65ddb70b00000000, + 0xf009c79e00000000, 0x0e7227fa00000000, 0x9ba6576f00000000, + 0xdc34474300000000, 0x49e037d600000000, 0xb79bd7b200000000, + 0x224fa72700000000, 0x4b6c177b00000000, 0xdeb867ee00000000, + 0x20c3878a00000000, 0xb517f71f00000000, 0xaee7a6d200000000, + 0x3b33d64700000000, 0xc548362300000000, 0x509c46b600000000, + 0x39bff6ea00000000, 0xac6b867f00000000, 0x5210661b00000000, + 0xc7c4168e00000000, 0x805606a200000000, 0x1582763700000000, + 0xebf9965300000000, 0x7e2de6c600000000, 0x170e569a00000000, + 0x82da260f00000000, 0x7ca1c66b00000000, 0xe975b6fe00000000, + 0x0b47142a00000000, 0x9e9364bf00000000, 0x60e884db00000000, + 0xf53cf44e00000000, 0x9c1f441200000000, 0x09cb348700000000, + 0xf7b0d4e300000000, 0x6264a47600000000, 0x25f6b45a00000000, + 0xb022c4cf00000000, 0x4e5924ab00000000, 0xdb8d543e00000000, + 0xb2aee46200000000, 0x277a94f700000000, 0xd901749300000000, + 0x4cd5040600000000, 0x572555cb00000000, 0xc2f1255e00000000, + 0x3c8ac53a00000000, 0xa95eb5af00000000, 0xc07d05f300000000, + 0x55a9756600000000, 0xabd2950200000000, 0x3e06e59700000000, + 0x7994f5bb00000000, 0xec40852e00000000, 0x123b654a00000000, + 0x87ef15df00000000, 0xeecca58300000000, 0x7b18d51600000000, + 0x8563357200000000, 0x10b745e700000000, 0xe40bcf6700000000, + 0x71dfbff200000000, 0x8fa45f9600000000, 0x1a702f0300000000, + 0x73539f5f00000000, 0xe687efca00000000, 0x18fc0fae00000000, + 0x8d287f3b00000000, 0xcaba6f1700000000, 0x5f6e1f8200000000, + 0xa115ffe600000000, 0x34c18f7300000000, 0x5de23f2f00000000, + 0xc8364fba00000000, 0x364dafde00000000, 0xa399df4b00000000, + 0xb8698e8600000000, 0x2dbdfe1300000000, 0xd3c61e7700000000, + 0x46126ee200000000, 0x2f31debe00000000, 0xbae5ae2b00000000, + 0x449e4e4f00000000, 0xd14a3eda00000000, 0x96d82ef600000000, + 0x030c5e6300000000, 0xfd77be0700000000, 0x68a3ce9200000000, + 0x01807ece00000000, 0x94540e5b00000000, 0x6a2fee3f00000000, + 0xfffb9eaa00000000, 0x1dc93c7e00000000, 0x881d4ceb00000000, + 0x7666ac8f00000000, 0xe3b2dc1a00000000, 0x8a916c4600000000, + 0x1f451cd300000000, 0xe13efcb700000000, 0x74ea8c2200000000, + 0x33789c0e00000000, 0xa6acec9b00000000, 0x58d70cff00000000, + 0xcd037c6a00000000, 0xa420cc3600000000, 0x31f4bca300000000, + 0xcf8f5cc700000000, 0x5a5b2c5200000000, 0x41ab7d9f00000000, + 0xd47f0d0a00000000, 0x2a04ed6e00000000, 0xbfd09dfb00000000, + 0xd6f32da700000000, 0x43275d3200000000, 0xbd5cbd5600000000, + 0x2888cdc300000000, 0x6f1addef00000000, 0xfacead7a00000000, + 0x04b54d1e00000000, 0x91613d8b00000000, 0xf8428dd700000000, + 0x6d96fd4200000000, 0x93ed1d2600000000, 0x06396db300000000, + 0x168e285400000000, 0x835a58c100000000, 0x7d21b8a500000000, + 0xe8f5c83000000000, 0x81d6786c00000000, 0x140208f900000000, + 0xea79e89d00000000, 0x7fad980800000000, 0x383f882400000000, + 0xadebf8b100000000, 0x539018d500000000, 0xc644684000000000, + 0xaf67d81c00000000, 0x3ab3a88900000000, 0xc4c848ed00000000, + 0x511c387800000000, 0x4aec69b500000000, 0xdf38192000000000, + 0x2143f94400000000, 0xb49789d100000000, 0xddb4398d00000000, + 0x4860491800000000, 0xb61ba97c00000000, 0x23cfd9e900000000, + 0x645dc9c500000000, 0xf189b95000000000, 0x0ff2593400000000, + 0x9a2629a100000000, 0xf30599fd00000000, 0x66d1e96800000000, + 0x98aa090c00000000, 0x0d7e799900000000, 0xef4cdb4d00000000, + 0x7a98abd800000000, 0x84e34bbc00000000, 0x11373b2900000000, + 0x78148b7500000000, 0xedc0fbe000000000, 0x13bb1b8400000000, + 0x866f6b1100000000, 0xc1fd7b3d00000000, 0x54290ba800000000, + 0xaa52ebcc00000000, 0x3f869b5900000000, 0x56a52b0500000000, + 0xc3715b9000000000, 0x3d0abbf400000000, 0xa8decb6100000000, + 0xb32e9aac00000000, 0x26faea3900000000, 0xd8810a5d00000000, + 0x4d557ac800000000, 0x2476ca9400000000, 0xb1a2ba0100000000, + 0x4fd95a6500000000, 0xda0d2af000000000, 0x9d9f3adc00000000, + 0x084b4a4900000000, 0xf630aa2d00000000, 0x63e4dab800000000, + 0x0ac76ae400000000, 0x9f131a7100000000, 0x6168fa1500000000, + 0xf4bc8a8000000000}, + {0x0000000000000000, 0x1f17f08000000000, 0x7f2891da00000000, + 0x603f615a00000000, 0xbf56536e00000000, 0xa041a3ee00000000, + 0xc07ec2b400000000, 0xdf69323400000000, 0x7eada6dc00000000, + 0x61ba565c00000000, 0x0185370600000000, 0x1e92c78600000000, + 0xc1fbf5b200000000, 0xdeec053200000000, 0xbed3646800000000, + 0xa1c494e800000000, 0xbd5c3c6200000000, 0xa24bcce200000000, + 0xc274adb800000000, 0xdd635d3800000000, 0x020a6f0c00000000, + 0x1d1d9f8c00000000, 0x7d22fed600000000, 0x62350e5600000000, + 0xc3f19abe00000000, 0xdce66a3e00000000, 0xbcd90b6400000000, + 0xa3cefbe400000000, 0x7ca7c9d000000000, 0x63b0395000000000, + 0x038f580a00000000, 0x1c98a88a00000000, 0x7ab978c400000000, + 0x65ae884400000000, 0x0591e91e00000000, 0x1a86199e00000000, + 0xc5ef2baa00000000, 0xdaf8db2a00000000, 0xbac7ba7000000000, + 0xa5d04af000000000, 0x0414de1800000000, 0x1b032e9800000000, + 0x7b3c4fc200000000, 0x642bbf4200000000, 0xbb428d7600000000, + 0xa4557df600000000, 0xc46a1cac00000000, 0xdb7dec2c00000000, + 0xc7e544a600000000, 0xd8f2b42600000000, 0xb8cdd57c00000000, + 0xa7da25fc00000000, 0x78b317c800000000, 0x67a4e74800000000, + 0x079b861200000000, 0x188c769200000000, 0xb948e27a00000000, + 0xa65f12fa00000000, 0xc66073a000000000, 0xd977832000000000, + 0x061eb11400000000, 0x1909419400000000, 0x793620ce00000000, + 0x6621d04e00000000, 0xb574805300000000, 0xaa6370d300000000, + 0xca5c118900000000, 0xd54be10900000000, 0x0a22d33d00000000, + 0x153523bd00000000, 0x750a42e700000000, 0x6a1db26700000000, + 0xcbd9268f00000000, 0xd4ced60f00000000, 0xb4f1b75500000000, + 0xabe647d500000000, 0x748f75e100000000, 0x6b98856100000000, + 0x0ba7e43b00000000, 0x14b014bb00000000, 0x0828bc3100000000, + 0x173f4cb100000000, 0x77002deb00000000, 0x6817dd6b00000000, + 0xb77eef5f00000000, 0xa8691fdf00000000, 0xc8567e8500000000, + 0xd7418e0500000000, 0x76851aed00000000, 0x6992ea6d00000000, + 0x09ad8b3700000000, 0x16ba7bb700000000, 0xc9d3498300000000, + 0xd6c4b90300000000, 0xb6fbd85900000000, 0xa9ec28d900000000, + 0xcfcdf89700000000, 0xd0da081700000000, 0xb0e5694d00000000, + 0xaff299cd00000000, 0x709babf900000000, 0x6f8c5b7900000000, + 0x0fb33a2300000000, 0x10a4caa300000000, 0xb1605e4b00000000, + 0xae77aecb00000000, 0xce48cf9100000000, 0xd15f3f1100000000, + 0x0e360d2500000000, 0x1121fda500000000, 0x711e9cff00000000, + 0x6e096c7f00000000, 0x7291c4f500000000, 0x6d86347500000000, + 0x0db9552f00000000, 0x12aea5af00000000, 0xcdc7979b00000000, + 0xd2d0671b00000000, 0xb2ef064100000000, 0xadf8f6c100000000, + 0x0c3c622900000000, 0x132b92a900000000, 0x7314f3f300000000, + 0x6c03037300000000, 0xb36a314700000000, 0xac7dc1c700000000, + 0xcc42a09d00000000, 0xd355501d00000000, 0x6ae900a700000000, + 0x75fef02700000000, 0x15c1917d00000000, 0x0ad661fd00000000, + 0xd5bf53c900000000, 0xcaa8a34900000000, 0xaa97c21300000000, + 0xb580329300000000, 0x1444a67b00000000, 0x0b5356fb00000000, + 0x6b6c37a100000000, 0x747bc72100000000, 0xab12f51500000000, + 0xb405059500000000, 0xd43a64cf00000000, 0xcb2d944f00000000, + 0xd7b53cc500000000, 0xc8a2cc4500000000, 0xa89dad1f00000000, + 0xb78a5d9f00000000, 0x68e36fab00000000, 0x77f49f2b00000000, + 0x17cbfe7100000000, 0x08dc0ef100000000, 0xa9189a1900000000, + 0xb60f6a9900000000, 0xd6300bc300000000, 0xc927fb4300000000, + 0x164ec97700000000, 0x095939f700000000, 0x696658ad00000000, + 0x7671a82d00000000, 0x1050786300000000, 0x0f4788e300000000, + 0x6f78e9b900000000, 0x706f193900000000, 0xaf062b0d00000000, + 0xb011db8d00000000, 0xd02ebad700000000, 0xcf394a5700000000, + 0x6efddebf00000000, 0x71ea2e3f00000000, 0x11d54f6500000000, + 0x0ec2bfe500000000, 0xd1ab8dd100000000, 0xcebc7d5100000000, + 0xae831c0b00000000, 0xb194ec8b00000000, 0xad0c440100000000, + 0xb21bb48100000000, 0xd224d5db00000000, 0xcd33255b00000000, + 0x125a176f00000000, 0x0d4de7ef00000000, 0x6d7286b500000000, + 0x7265763500000000, 0xd3a1e2dd00000000, 0xccb6125d00000000, + 0xac89730700000000, 0xb39e838700000000, 0x6cf7b1b300000000, + 0x73e0413300000000, 0x13df206900000000, 0x0cc8d0e900000000, + 0xdf9d80f400000000, 0xc08a707400000000, 0xa0b5112e00000000, + 0xbfa2e1ae00000000, 0x60cbd39a00000000, 0x7fdc231a00000000, + 0x1fe3424000000000, 0x00f4b2c000000000, 0xa130262800000000, + 0xbe27d6a800000000, 0xde18b7f200000000, 0xc10f477200000000, + 0x1e66754600000000, 0x017185c600000000, 0x614ee49c00000000, + 0x7e59141c00000000, 0x62c1bc9600000000, 0x7dd64c1600000000, + 0x1de92d4c00000000, 0x02feddcc00000000, 0xdd97eff800000000, + 0xc2801f7800000000, 0xa2bf7e2200000000, 0xbda88ea200000000, + 0x1c6c1a4a00000000, 0x037beaca00000000, 0x63448b9000000000, + 0x7c537b1000000000, 0xa33a492400000000, 0xbc2db9a400000000, + 0xdc12d8fe00000000, 0xc305287e00000000, 0xa524f83000000000, + 0xba3308b000000000, 0xda0c69ea00000000, 0xc51b996a00000000, + 0x1a72ab5e00000000, 0x05655bde00000000, 0x655a3a8400000000, + 0x7a4dca0400000000, 0xdb895eec00000000, 0xc49eae6c00000000, + 0xa4a1cf3600000000, 0xbbb63fb600000000, 0x64df0d8200000000, + 0x7bc8fd0200000000, 0x1bf79c5800000000, 0x04e06cd800000000, + 0x1878c45200000000, 0x076f34d200000000, 0x6750558800000000, + 0x7847a50800000000, 0xa72e973c00000000, 0xb83967bc00000000, + 0xd80606e600000000, 0xc711f66600000000, 0x66d5628e00000000, + 0x79c2920e00000000, 0x19fdf35400000000, 0x06ea03d400000000, + 0xd98331e000000000, 0xc694c16000000000, 0xa6aba03a00000000, + 0xb9bc50ba00000000}, + {0x0000000000000000, 0xe2fd888d00000000, 0x85fd60c000000000, + 0x6700e84d00000000, 0x4bfdb05b00000000, 0xa90038d600000000, + 0xce00d09b00000000, 0x2cfd581600000000, 0x96fa61b700000000, + 0x7407e93a00000000, 0x1307017700000000, 0xf1fa89fa00000000, + 0xdd07d1ec00000000, 0x3ffa596100000000, 0x58fab12c00000000, + 0xba0739a100000000, 0x6df3b2b500000000, 0x8f0e3a3800000000, + 0xe80ed27500000000, 0x0af35af800000000, 0x260e02ee00000000, + 0xc4f38a6300000000, 0xa3f3622e00000000, 0x410eeaa300000000, + 0xfb09d30200000000, 0x19f45b8f00000000, 0x7ef4b3c200000000, + 0x9c093b4f00000000, 0xb0f4635900000000, 0x5209ebd400000000, + 0x3509039900000000, 0xd7f48b1400000000, 0x9be014b000000000, + 0x791d9c3d00000000, 0x1e1d747000000000, 0xfce0fcfd00000000, + 0xd01da4eb00000000, 0x32e02c6600000000, 0x55e0c42b00000000, + 0xb71d4ca600000000, 0x0d1a750700000000, 0xefe7fd8a00000000, + 0x88e715c700000000, 0x6a1a9d4a00000000, 0x46e7c55c00000000, + 0xa41a4dd100000000, 0xc31aa59c00000000, 0x21e72d1100000000, + 0xf613a60500000000, 0x14ee2e8800000000, 0x73eec6c500000000, + 0x91134e4800000000, 0xbdee165e00000000, 0x5f139ed300000000, + 0x3813769e00000000, 0xdaeefe1300000000, 0x60e9c7b200000000, + 0x82144f3f00000000, 0xe514a77200000000, 0x07e92fff00000000, + 0x2b1477e900000000, 0xc9e9ff6400000000, 0xaee9172900000000, + 0x4c149fa400000000, 0x77c758bb00000000, 0x953ad03600000000, + 0xf23a387b00000000, 0x10c7b0f600000000, 0x3c3ae8e000000000, + 0xdec7606d00000000, 0xb9c7882000000000, 0x5b3a00ad00000000, + 0xe13d390c00000000, 0x03c0b18100000000, 0x64c059cc00000000, + 0x863dd14100000000, 0xaac0895700000000, 0x483d01da00000000, + 0x2f3de99700000000, 0xcdc0611a00000000, 0x1a34ea0e00000000, + 0xf8c9628300000000, 0x9fc98ace00000000, 0x7d34024300000000, + 0x51c95a5500000000, 0xb334d2d800000000, 0xd4343a9500000000, + 0x36c9b21800000000, 0x8cce8bb900000000, 0x6e33033400000000, + 0x0933eb7900000000, 0xebce63f400000000, 0xc7333be200000000, + 0x25ceb36f00000000, 0x42ce5b2200000000, 0xa033d3af00000000, + 0xec274c0b00000000, 0x0edac48600000000, 0x69da2ccb00000000, + 0x8b27a44600000000, 0xa7dafc5000000000, 0x452774dd00000000, + 0x22279c9000000000, 0xc0da141d00000000, 0x7add2dbc00000000, + 0x9820a53100000000, 0xff204d7c00000000, 0x1dddc5f100000000, + 0x31209de700000000, 0xd3dd156a00000000, 0xb4ddfd2700000000, + 0x562075aa00000000, 0x81d4febe00000000, 0x6329763300000000, + 0x04299e7e00000000, 0xe6d416f300000000, 0xca294ee500000000, + 0x28d4c66800000000, 0x4fd42e2500000000, 0xad29a6a800000000, + 0x172e9f0900000000, 0xf5d3178400000000, 0x92d3ffc900000000, + 0x702e774400000000, 0x5cd32f5200000000, 0xbe2ea7df00000000, + 0xd92e4f9200000000, 0x3bd3c71f00000000, 0xaf88c0ad00000000, + 0x4d75482000000000, 0x2a75a06d00000000, 0xc88828e000000000, + 0xe47570f600000000, 0x0688f87b00000000, 0x6188103600000000, + 0x837598bb00000000, 0x3972a11a00000000, 0xdb8f299700000000, + 0xbc8fc1da00000000, 0x5e72495700000000, 0x728f114100000000, + 0x907299cc00000000, 0xf772718100000000, 0x158ff90c00000000, + 0xc27b721800000000, 0x2086fa9500000000, 0x478612d800000000, + 0xa57b9a5500000000, 0x8986c24300000000, 0x6b7b4ace00000000, + 0x0c7ba28300000000, 0xee862a0e00000000, 0x548113af00000000, + 0xb67c9b2200000000, 0xd17c736f00000000, 0x3381fbe200000000, + 0x1f7ca3f400000000, 0xfd812b7900000000, 0x9a81c33400000000, + 0x787c4bb900000000, 0x3468d41d00000000, 0xd6955c9000000000, + 0xb195b4dd00000000, 0x53683c5000000000, 0x7f95644600000000, + 0x9d68eccb00000000, 0xfa68048600000000, 0x18958c0b00000000, + 0xa292b5aa00000000, 0x406f3d2700000000, 0x276fd56a00000000, + 0xc5925de700000000, 0xe96f05f100000000, 0x0b928d7c00000000, + 0x6c92653100000000, 0x8e6fedbc00000000, 0x599b66a800000000, + 0xbb66ee2500000000, 0xdc66066800000000, 0x3e9b8ee500000000, + 0x1266d6f300000000, 0xf09b5e7e00000000, 0x979bb63300000000, + 0x75663ebe00000000, 0xcf61071f00000000, 0x2d9c8f9200000000, + 0x4a9c67df00000000, 0xa861ef5200000000, 0x849cb74400000000, + 0x66613fc900000000, 0x0161d78400000000, 0xe39c5f0900000000, + 0xd84f981600000000, 0x3ab2109b00000000, 0x5db2f8d600000000, + 0xbf4f705b00000000, 0x93b2284d00000000, 0x714fa0c000000000, + 0x164f488d00000000, 0xf4b2c00000000000, 0x4eb5f9a100000000, + 0xac48712c00000000, 0xcb48996100000000, 0x29b511ec00000000, + 0x054849fa00000000, 0xe7b5c17700000000, 0x80b5293a00000000, + 0x6248a1b700000000, 0xb5bc2aa300000000, 0x5741a22e00000000, + 0x30414a6300000000, 0xd2bcc2ee00000000, 0xfe419af800000000, + 0x1cbc127500000000, 0x7bbcfa3800000000, 0x994172b500000000, + 0x23464b1400000000, 0xc1bbc39900000000, 0xa6bb2bd400000000, + 0x4446a35900000000, 0x68bbfb4f00000000, 0x8a4673c200000000, + 0xed469b8f00000000, 0x0fbb130200000000, 0x43af8ca600000000, + 0xa152042b00000000, 0xc652ec6600000000, 0x24af64eb00000000, + 0x08523cfd00000000, 0xeaafb47000000000, 0x8daf5c3d00000000, + 0x6f52d4b000000000, 0xd555ed1100000000, 0x37a8659c00000000, + 0x50a88dd100000000, 0xb255055c00000000, 0x9ea85d4a00000000, + 0x7c55d5c700000000, 0x1b553d8a00000000, 0xf9a8b50700000000, + 0x2e5c3e1300000000, 0xcca1b69e00000000, 0xaba15ed300000000, + 0x495cd65e00000000, 0x65a18e4800000000, 0x875c06c500000000, + 0xe05cee8800000000, 0x02a1660500000000, 0xb8a65fa400000000, + 0x5a5bd72900000000, 0x3d5b3f6400000000, 0xdfa6b7e900000000, + 0xf35befff00000000, 0x11a6677200000000, 0x76a68f3f00000000, + 0x945b07b200000000}, + {0x0000000000000000, 0xa90b894e00000000, 0x5217129d00000000, + 0xfb1c9bd300000000, 0xe52855e100000000, 0x4c23dcaf00000000, + 0xb73f477c00000000, 0x1e34ce3200000000, 0x8b57db1900000000, + 0x225c525700000000, 0xd940c98400000000, 0x704b40ca00000000, + 0x6e7f8ef800000000, 0xc77407b600000000, 0x3c689c6500000000, + 0x9563152b00000000, 0x16afb63300000000, 0xbfa43f7d00000000, + 0x44b8a4ae00000000, 0xedb32de000000000, 0xf387e3d200000000, + 0x5a8c6a9c00000000, 0xa190f14f00000000, 0x089b780100000000, + 0x9df86d2a00000000, 0x34f3e46400000000, 0xcfef7fb700000000, + 0x66e4f6f900000000, 0x78d038cb00000000, 0xd1dbb18500000000, + 0x2ac72a5600000000, 0x83cca31800000000, 0x2c5e6d6700000000, + 0x8555e42900000000, 0x7e497ffa00000000, 0xd742f6b400000000, + 0xc976388600000000, 0x607db1c800000000, 0x9b612a1b00000000, + 0x326aa35500000000, 0xa709b67e00000000, 0x0e023f3000000000, + 0xf51ea4e300000000, 0x5c152dad00000000, 0x4221e39f00000000, + 0xeb2a6ad100000000, 0x1036f10200000000, 0xb93d784c00000000, + 0x3af1db5400000000, 0x93fa521a00000000, 0x68e6c9c900000000, + 0xc1ed408700000000, 0xdfd98eb500000000, 0x76d207fb00000000, + 0x8dce9c2800000000, 0x24c5156600000000, 0xb1a6004d00000000, + 0x18ad890300000000, 0xe3b112d000000000, 0x4aba9b9e00000000, + 0x548e55ac00000000, 0xfd85dce200000000, 0x0699473100000000, + 0xaf92ce7f00000000, 0x58bcdace00000000, 0xf1b7538000000000, + 0x0aabc85300000000, 0xa3a0411d00000000, 0xbd948f2f00000000, + 0x149f066100000000, 0xef839db200000000, 0x468814fc00000000, + 0xd3eb01d700000000, 0x7ae0889900000000, 0x81fc134a00000000, + 0x28f79a0400000000, 0x36c3543600000000, 0x9fc8dd7800000000, + 0x64d446ab00000000, 0xcddfcfe500000000, 0x4e136cfd00000000, + 0xe718e5b300000000, 0x1c047e6000000000, 0xb50ff72e00000000, + 0xab3b391c00000000, 0x0230b05200000000, 0xf92c2b8100000000, + 0x5027a2cf00000000, 0xc544b7e400000000, 0x6c4f3eaa00000000, + 0x9753a57900000000, 0x3e582c3700000000, 0x206ce20500000000, + 0x89676b4b00000000, 0x727bf09800000000, 0xdb7079d600000000, + 0x74e2b7a900000000, 0xdde93ee700000000, 0x26f5a53400000000, + 0x8ffe2c7a00000000, 0x91cae24800000000, 0x38c16b0600000000, + 0xc3ddf0d500000000, 0x6ad6799b00000000, 0xffb56cb000000000, + 0x56bee5fe00000000, 0xada27e2d00000000, 0x04a9f76300000000, + 0x1a9d395100000000, 0xb396b01f00000000, 0x488a2bcc00000000, + 0xe181a28200000000, 0x624d019a00000000, 0xcb4688d400000000, + 0x305a130700000000, 0x99519a4900000000, 0x8765547b00000000, + 0x2e6edd3500000000, 0xd57246e600000000, 0x7c79cfa800000000, + 0xe91ada8300000000, 0x401153cd00000000, 0xbb0dc81e00000000, + 0x1206415000000000, 0x0c328f6200000000, 0xa539062c00000000, + 0x5e259dff00000000, 0xf72e14b100000000, 0xf17ec44600000000, + 0x58754d0800000000, 0xa369d6db00000000, 0x0a625f9500000000, + 0x145691a700000000, 0xbd5d18e900000000, 0x4641833a00000000, + 0xef4a0a7400000000, 0x7a291f5f00000000, 0xd322961100000000, + 0x283e0dc200000000, 0x8135848c00000000, 0x9f014abe00000000, + 0x360ac3f000000000, 0xcd16582300000000, 0x641dd16d00000000, + 0xe7d1727500000000, 0x4edafb3b00000000, 0xb5c660e800000000, + 0x1ccde9a600000000, 0x02f9279400000000, 0xabf2aeda00000000, + 0x50ee350900000000, 0xf9e5bc4700000000, 0x6c86a96c00000000, + 0xc58d202200000000, 0x3e91bbf100000000, 0x979a32bf00000000, + 0x89aefc8d00000000, 0x20a575c300000000, 0xdbb9ee1000000000, + 0x72b2675e00000000, 0xdd20a92100000000, 0x742b206f00000000, + 0x8f37bbbc00000000, 0x263c32f200000000, 0x3808fcc000000000, + 0x9103758e00000000, 0x6a1fee5d00000000, 0xc314671300000000, + 0x5677723800000000, 0xff7cfb7600000000, 0x046060a500000000, + 0xad6be9eb00000000, 0xb35f27d900000000, 0x1a54ae9700000000, + 0xe148354400000000, 0x4843bc0a00000000, 0xcb8f1f1200000000, + 0x6284965c00000000, 0x99980d8f00000000, 0x309384c100000000, + 0x2ea74af300000000, 0x87acc3bd00000000, 0x7cb0586e00000000, + 0xd5bbd12000000000, 0x40d8c40b00000000, 0xe9d34d4500000000, + 0x12cfd69600000000, 0xbbc45fd800000000, 0xa5f091ea00000000, + 0x0cfb18a400000000, 0xf7e7837700000000, 0x5eec0a3900000000, + 0xa9c21e8800000000, 0x00c997c600000000, 0xfbd50c1500000000, + 0x52de855b00000000, 0x4cea4b6900000000, 0xe5e1c22700000000, + 0x1efd59f400000000, 0xb7f6d0ba00000000, 0x2295c59100000000, + 0x8b9e4cdf00000000, 0x7082d70c00000000, 0xd9895e4200000000, + 0xc7bd907000000000, 0x6eb6193e00000000, 0x95aa82ed00000000, + 0x3ca10ba300000000, 0xbf6da8bb00000000, 0x166621f500000000, + 0xed7aba2600000000, 0x4471336800000000, 0x5a45fd5a00000000, + 0xf34e741400000000, 0x0852efc700000000, 0xa159668900000000, + 0x343a73a200000000, 0x9d31faec00000000, 0x662d613f00000000, + 0xcf26e87100000000, 0xd112264300000000, 0x7819af0d00000000, + 0x830534de00000000, 0x2a0ebd9000000000, 0x859c73ef00000000, + 0x2c97faa100000000, 0xd78b617200000000, 0x7e80e83c00000000, + 0x60b4260e00000000, 0xc9bfaf4000000000, 0x32a3349300000000, + 0x9ba8bddd00000000, 0x0ecba8f600000000, 0xa7c021b800000000, + 0x5cdcba6b00000000, 0xf5d7332500000000, 0xebe3fd1700000000, + 0x42e8745900000000, 0xb9f4ef8a00000000, 0x10ff66c400000000, + 0x9333c5dc00000000, 0x3a384c9200000000, 0xc124d74100000000, + 0x682f5e0f00000000, 0x761b903d00000000, 0xdf10197300000000, + 0x240c82a000000000, 0x8d070bee00000000, 0x18641ec500000000, + 0xb16f978b00000000, 0x4a730c5800000000, 0xe378851600000000, + 0xfd4c4b2400000000, 0x5447c26a00000000, 0xaf5b59b900000000, + 0x0650d0f700000000}, + {0x0000000000000000, 0x479244af00000000, 0xcf22f88500000000, + 0x88b0bc2a00000000, 0xdf4381d000000000, 0x98d1c57f00000000, + 0x1061795500000000, 0x57f33dfa00000000, 0xff81737a00000000, + 0xb81337d500000000, 0x30a38bff00000000, 0x7731cf5000000000, + 0x20c2f2aa00000000, 0x6750b60500000000, 0xefe00a2f00000000, + 0xa8724e8000000000, 0xfe03e7f400000000, 0xb991a35b00000000, + 0x31211f7100000000, 0x76b35bde00000000, 0x2140662400000000, + 0x66d2228b00000000, 0xee629ea100000000, 0xa9f0da0e00000000, + 0x0182948e00000000, 0x4610d02100000000, 0xcea06c0b00000000, + 0x893228a400000000, 0xdec1155e00000000, 0x995351f100000000, + 0x11e3eddb00000000, 0x5671a97400000000, 0xbd01bf3200000000, + 0xfa93fb9d00000000, 0x722347b700000000, 0x35b1031800000000, + 0x62423ee200000000, 0x25d07a4d00000000, 0xad60c66700000000, + 0xeaf282c800000000, 0x4280cc4800000000, 0x051288e700000000, + 0x8da234cd00000000, 0xca30706200000000, 0x9dc34d9800000000, + 0xda51093700000000, 0x52e1b51d00000000, 0x1573f1b200000000, + 0x430258c600000000, 0x04901c6900000000, 0x8c20a04300000000, + 0xcbb2e4ec00000000, 0x9c41d91600000000, 0xdbd39db900000000, + 0x5363219300000000, 0x14f1653c00000000, 0xbc832bbc00000000, + 0xfb116f1300000000, 0x73a1d33900000000, 0x3433979600000000, + 0x63c0aa6c00000000, 0x2452eec300000000, 0xace252e900000000, + 0xeb70164600000000, 0x7a037e6500000000, 0x3d913aca00000000, + 0xb52186e000000000, 0xf2b3c24f00000000, 0xa540ffb500000000, + 0xe2d2bb1a00000000, 0x6a62073000000000, 0x2df0439f00000000, + 0x85820d1f00000000, 0xc21049b000000000, 0x4aa0f59a00000000, + 0x0d32b13500000000, 0x5ac18ccf00000000, 0x1d53c86000000000, + 0x95e3744a00000000, 0xd27130e500000000, 0x8400999100000000, + 0xc392dd3e00000000, 0x4b22611400000000, 0x0cb025bb00000000, + 0x5b43184100000000, 0x1cd15cee00000000, 0x9461e0c400000000, + 0xd3f3a46b00000000, 0x7b81eaeb00000000, 0x3c13ae4400000000, + 0xb4a3126e00000000, 0xf33156c100000000, 0xa4c26b3b00000000, + 0xe3502f9400000000, 0x6be093be00000000, 0x2c72d71100000000, + 0xc702c15700000000, 0x809085f800000000, 0x082039d200000000, + 0x4fb27d7d00000000, 0x1841408700000000, 0x5fd3042800000000, + 0xd763b80200000000, 0x90f1fcad00000000, 0x3883b22d00000000, + 0x7f11f68200000000, 0xf7a14aa800000000, 0xb0330e0700000000, + 0xe7c033fd00000000, 0xa052775200000000, 0x28e2cb7800000000, + 0x6f708fd700000000, 0x390126a300000000, 0x7e93620c00000000, + 0xf623de2600000000, 0xb1b19a8900000000, 0xe642a77300000000, + 0xa1d0e3dc00000000, 0x29605ff600000000, 0x6ef21b5900000000, + 0xc68055d900000000, 0x8112117600000000, 0x09a2ad5c00000000, + 0x4e30e9f300000000, 0x19c3d40900000000, 0x5e5190a600000000, + 0xd6e12c8c00000000, 0x9173682300000000, 0xf406fcca00000000, + 0xb394b86500000000, 0x3b24044f00000000, 0x7cb640e000000000, + 0x2b457d1a00000000, 0x6cd739b500000000, 0xe467859f00000000, + 0xa3f5c13000000000, 0x0b878fb000000000, 0x4c15cb1f00000000, + 0xc4a5773500000000, 0x8337339a00000000, 0xd4c40e6000000000, + 0x93564acf00000000, 0x1be6f6e500000000, 0x5c74b24a00000000, + 0x0a051b3e00000000, 0x4d975f9100000000, 0xc527e3bb00000000, + 0x82b5a71400000000, 0xd5469aee00000000, 0x92d4de4100000000, + 0x1a64626b00000000, 0x5df626c400000000, 0xf584684400000000, + 0xb2162ceb00000000, 0x3aa690c100000000, 0x7d34d46e00000000, + 0x2ac7e99400000000, 0x6d55ad3b00000000, 0xe5e5111100000000, + 0xa27755be00000000, 0x490743f800000000, 0x0e95075700000000, + 0x8625bb7d00000000, 0xc1b7ffd200000000, 0x9644c22800000000, + 0xd1d6868700000000, 0x59663aad00000000, 0x1ef47e0200000000, + 0xb686308200000000, 0xf114742d00000000, 0x79a4c80700000000, + 0x3e368ca800000000, 0x69c5b15200000000, 0x2e57f5fd00000000, + 0xa6e749d700000000, 0xe1750d7800000000, 0xb704a40c00000000, + 0xf096e0a300000000, 0x78265c8900000000, 0x3fb4182600000000, + 0x684725dc00000000, 0x2fd5617300000000, 0xa765dd5900000000, + 0xe0f799f600000000, 0x4885d77600000000, 0x0f1793d900000000, + 0x87a72ff300000000, 0xc0356b5c00000000, 0x97c656a600000000, + 0xd054120900000000, 0x58e4ae2300000000, 0x1f76ea8c00000000, + 0x8e0582af00000000, 0xc997c60000000000, 0x41277a2a00000000, + 0x06b53e8500000000, 0x5146037f00000000, 0x16d447d000000000, + 0x9e64fbfa00000000, 0xd9f6bf5500000000, 0x7184f1d500000000, + 0x3616b57a00000000, 0xbea6095000000000, 0xf9344dff00000000, + 0xaec7700500000000, 0xe95534aa00000000, 0x61e5888000000000, + 0x2677cc2f00000000, 0x7006655b00000000, 0x379421f400000000, + 0xbf249dde00000000, 0xf8b6d97100000000, 0xaf45e48b00000000, + 0xe8d7a02400000000, 0x60671c0e00000000, 0x27f558a100000000, + 0x8f87162100000000, 0xc815528e00000000, 0x40a5eea400000000, + 0x0737aa0b00000000, 0x50c497f100000000, 0x1756d35e00000000, + 0x9fe66f7400000000, 0xd8742bdb00000000, 0x33043d9d00000000, + 0x7496793200000000, 0xfc26c51800000000, 0xbbb481b700000000, + 0xec47bc4d00000000, 0xabd5f8e200000000, 0x236544c800000000, + 0x64f7006700000000, 0xcc854ee700000000, 0x8b170a4800000000, + 0x03a7b66200000000, 0x4435f2cd00000000, 0x13c6cf3700000000, + 0x54548b9800000000, 0xdce437b200000000, 0x9b76731d00000000, + 0xcd07da6900000000, 0x8a959ec600000000, 0x022522ec00000000, + 0x45b7664300000000, 0x12445bb900000000, 0x55d61f1600000000, + 0xdd66a33c00000000, 0x9af4e79300000000, 0x3286a91300000000, + 0x7514edbc00000000, 0xfda4519600000000, 0xba36153900000000, + 0xedc528c300000000, 0xaa576c6c00000000, 0x22e7d04600000000, + 0x657594e900000000}}; + +#else /* W == 4 */ + +local const z_crc_t FAR crc_braid_table[][256] = { + {0x00000000, 0x65673b46, 0xcace768c, 0xafa94dca, 0x4eedeb59, + 0x2b8ad01f, 0x84239dd5, 0xe144a693, 0x9ddbd6b2, 0xf8bcedf4, + 0x5715a03e, 0x32729b78, 0xd3363deb, 0xb65106ad, 0x19f84b67, + 0x7c9f7021, 0xe0c6ab25, 0x85a19063, 0x2a08dda9, 0x4f6fe6ef, + 0xae2b407c, 0xcb4c7b3a, 0x64e536f0, 0x01820db6, 0x7d1d7d97, + 0x187a46d1, 0xb7d30b1b, 0xd2b4305d, 0x33f096ce, 0x5697ad88, + 0xf93ee042, 0x9c59db04, 0x1afc500b, 0x7f9b6b4d, 0xd0322687, + 0xb5551dc1, 0x5411bb52, 0x31768014, 0x9edfcdde, 0xfbb8f698, + 0x872786b9, 0xe240bdff, 0x4de9f035, 0x288ecb73, 0xc9ca6de0, + 0xacad56a6, 0x03041b6c, 0x6663202a, 0xfa3afb2e, 0x9f5dc068, + 0x30f48da2, 0x5593b6e4, 0xb4d71077, 0xd1b02b31, 0x7e1966fb, + 0x1b7e5dbd, 0x67e12d9c, 0x028616da, 0xad2f5b10, 0xc8486056, + 0x290cc6c5, 0x4c6bfd83, 0xe3c2b049, 0x86a58b0f, 0x35f8a016, + 0x509f9b50, 0xff36d69a, 0x9a51eddc, 0x7b154b4f, 0x1e727009, + 0xb1db3dc3, 0xd4bc0685, 0xa82376a4, 0xcd444de2, 0x62ed0028, + 0x078a3b6e, 0xe6ce9dfd, 0x83a9a6bb, 0x2c00eb71, 0x4967d037, + 0xd53e0b33, 0xb0593075, 0x1ff07dbf, 0x7a9746f9, 0x9bd3e06a, + 0xfeb4db2c, 0x511d96e6, 0x347aada0, 0x48e5dd81, 0x2d82e6c7, + 0x822bab0d, 0xe74c904b, 0x060836d8, 0x636f0d9e, 0xccc64054, + 0xa9a17b12, 0x2f04f01d, 0x4a63cb5b, 0xe5ca8691, 0x80adbdd7, + 0x61e91b44, 0x048e2002, 0xab276dc8, 0xce40568e, 0xb2df26af, + 0xd7b81de9, 0x78115023, 0x1d766b65, 0xfc32cdf6, 0x9955f6b0, + 0x36fcbb7a, 0x539b803c, 0xcfc25b38, 0xaaa5607e, 0x050c2db4, + 0x606b16f2, 0x812fb061, 0xe4488b27, 0x4be1c6ed, 0x2e86fdab, + 0x52198d8a, 0x377eb6cc, 0x98d7fb06, 0xfdb0c040, 0x1cf466d3, + 0x79935d95, 0xd63a105f, 0xb35d2b19, 0x6bf1402c, 0x0e967b6a, + 0xa13f36a0, 0xc4580de6, 0x251cab75, 0x407b9033, 0xefd2ddf9, + 0x8ab5e6bf, 0xf62a969e, 0x934dadd8, 0x3ce4e012, 0x5983db54, + 0xb8c77dc7, 0xdda04681, 0x72090b4b, 0x176e300d, 0x8b37eb09, + 0xee50d04f, 0x41f99d85, 0x249ea6c3, 0xc5da0050, 0xa0bd3b16, + 0x0f1476dc, 0x6a734d9a, 0x16ec3dbb, 0x738b06fd, 0xdc224b37, + 0xb9457071, 0x5801d6e2, 0x3d66eda4, 0x92cfa06e, 0xf7a89b28, + 0x710d1027, 0x146a2b61, 0xbbc366ab, 0xdea45ded, 0x3fe0fb7e, + 0x5a87c038, 0xf52e8df2, 0x9049b6b4, 0xecd6c695, 0x89b1fdd3, + 0x2618b019, 0x437f8b5f, 0xa23b2dcc, 0xc75c168a, 0x68f55b40, + 0x0d926006, 0x91cbbb02, 0xf4ac8044, 0x5b05cd8e, 0x3e62f6c8, + 0xdf26505b, 0xba416b1d, 0x15e826d7, 0x708f1d91, 0x0c106db0, + 0x697756f6, 0xc6de1b3c, 0xa3b9207a, 0x42fd86e9, 0x279abdaf, + 0x8833f065, 0xed54cb23, 0x5e09e03a, 0x3b6edb7c, 0x94c796b6, + 0xf1a0adf0, 0x10e40b63, 0x75833025, 0xda2a7def, 0xbf4d46a9, + 0xc3d23688, 0xa6b50dce, 0x091c4004, 0x6c7b7b42, 0x8d3fddd1, + 0xe858e697, 0x47f1ab5d, 0x2296901b, 0xbecf4b1f, 0xdba87059, + 0x74013d93, 0x116606d5, 0xf022a046, 0x95459b00, 0x3aecd6ca, + 0x5f8bed8c, 0x23149dad, 0x4673a6eb, 0xe9daeb21, 0x8cbdd067, + 0x6df976f4, 0x089e4db2, 0xa7370078, 0xc2503b3e, 0x44f5b031, + 0x21928b77, 0x8e3bc6bd, 0xeb5cfdfb, 0x0a185b68, 0x6f7f602e, + 0xc0d62de4, 0xa5b116a2, 0xd92e6683, 0xbc495dc5, 0x13e0100f, + 0x76872b49, 0x97c38dda, 0xf2a4b69c, 0x5d0dfb56, 0x386ac010, + 0xa4331b14, 0xc1542052, 0x6efd6d98, 0x0b9a56de, 0xeadef04d, + 0x8fb9cb0b, 0x201086c1, 0x4577bd87, 0x39e8cda6, 0x5c8ff6e0, + 0xf326bb2a, 0x9641806c, 0x770526ff, 0x12621db9, 0xbdcb5073, + 0xd8ac6b35}, + {0x00000000, 0xd7e28058, 0x74b406f1, 0xa35686a9, 0xe9680de2, + 0x3e8a8dba, 0x9ddc0b13, 0x4a3e8b4b, 0x09a11d85, 0xde439ddd, + 0x7d151b74, 0xaaf79b2c, 0xe0c91067, 0x372b903f, 0x947d1696, + 0x439f96ce, 0x13423b0a, 0xc4a0bb52, 0x67f63dfb, 0xb014bda3, + 0xfa2a36e8, 0x2dc8b6b0, 0x8e9e3019, 0x597cb041, 0x1ae3268f, + 0xcd01a6d7, 0x6e57207e, 0xb9b5a026, 0xf38b2b6d, 0x2469ab35, + 0x873f2d9c, 0x50ddadc4, 0x26847614, 0xf166f64c, 0x523070e5, + 0x85d2f0bd, 0xcfec7bf6, 0x180efbae, 0xbb587d07, 0x6cbafd5f, + 0x2f256b91, 0xf8c7ebc9, 0x5b916d60, 0x8c73ed38, 0xc64d6673, + 0x11afe62b, 0xb2f96082, 0x651be0da, 0x35c64d1e, 0xe224cd46, + 0x41724bef, 0x9690cbb7, 0xdcae40fc, 0x0b4cc0a4, 0xa81a460d, + 0x7ff8c655, 0x3c67509b, 0xeb85d0c3, 0x48d3566a, 0x9f31d632, + 0xd50f5d79, 0x02eddd21, 0xa1bb5b88, 0x7659dbd0, 0x4d08ec28, + 0x9aea6c70, 0x39bcead9, 0xee5e6a81, 0xa460e1ca, 0x73826192, + 0xd0d4e73b, 0x07366763, 0x44a9f1ad, 0x934b71f5, 0x301df75c, + 0xe7ff7704, 0xadc1fc4f, 0x7a237c17, 0xd975fabe, 0x0e977ae6, + 0x5e4ad722, 0x89a8577a, 0x2afed1d3, 0xfd1c518b, 0xb722dac0, + 0x60c05a98, 0xc396dc31, 0x14745c69, 0x57ebcaa7, 0x80094aff, + 0x235fcc56, 0xf4bd4c0e, 0xbe83c745, 0x6961471d, 0xca37c1b4, + 0x1dd541ec, 0x6b8c9a3c, 0xbc6e1a64, 0x1f389ccd, 0xc8da1c95, + 0x82e497de, 0x55061786, 0xf650912f, 0x21b21177, 0x622d87b9, + 0xb5cf07e1, 0x16998148, 0xc17b0110, 0x8b458a5b, 0x5ca70a03, + 0xfff18caa, 0x28130cf2, 0x78cea136, 0xaf2c216e, 0x0c7aa7c7, + 0xdb98279f, 0x91a6acd4, 0x46442c8c, 0xe512aa25, 0x32f02a7d, + 0x716fbcb3, 0xa68d3ceb, 0x05dbba42, 0xd2393a1a, 0x9807b151, + 0x4fe53109, 0xecb3b7a0, 0x3b5137f8, 0x9a11d850, 0x4df35808, + 0xeea5dea1, 0x39475ef9, 0x7379d5b2, 0xa49b55ea, 0x07cdd343, + 0xd02f531b, 0x93b0c5d5, 0x4452458d, 0xe704c324, 0x30e6437c, + 0x7ad8c837, 0xad3a486f, 0x0e6ccec6, 0xd98e4e9e, 0x8953e35a, + 0x5eb16302, 0xfde7e5ab, 0x2a0565f3, 0x603beeb8, 0xb7d96ee0, + 0x148fe849, 0xc36d6811, 0x80f2fedf, 0x57107e87, 0xf446f82e, + 0x23a47876, 0x699af33d, 0xbe787365, 0x1d2ef5cc, 0xcacc7594, + 0xbc95ae44, 0x6b772e1c, 0xc821a8b5, 0x1fc328ed, 0x55fda3a6, + 0x821f23fe, 0x2149a557, 0xf6ab250f, 0xb534b3c1, 0x62d63399, + 0xc180b530, 0x16623568, 0x5c5cbe23, 0x8bbe3e7b, 0x28e8b8d2, + 0xff0a388a, 0xafd7954e, 0x78351516, 0xdb6393bf, 0x0c8113e7, + 0x46bf98ac, 0x915d18f4, 0x320b9e5d, 0xe5e91e05, 0xa67688cb, + 0x71940893, 0xd2c28e3a, 0x05200e62, 0x4f1e8529, 0x98fc0571, + 0x3baa83d8, 0xec480380, 0xd7193478, 0x00fbb420, 0xa3ad3289, + 0x744fb2d1, 0x3e71399a, 0xe993b9c2, 0x4ac53f6b, 0x9d27bf33, + 0xdeb829fd, 0x095aa9a5, 0xaa0c2f0c, 0x7deeaf54, 0x37d0241f, + 0xe032a447, 0x436422ee, 0x9486a2b6, 0xc45b0f72, 0x13b98f2a, + 0xb0ef0983, 0x670d89db, 0x2d330290, 0xfad182c8, 0x59870461, + 0x8e658439, 0xcdfa12f7, 0x1a1892af, 0xb94e1406, 0x6eac945e, + 0x24921f15, 0xf3709f4d, 0x502619e4, 0x87c499bc, 0xf19d426c, + 0x267fc234, 0x8529449d, 0x52cbc4c5, 0x18f54f8e, 0xcf17cfd6, + 0x6c41497f, 0xbba3c927, 0xf83c5fe9, 0x2fdedfb1, 0x8c885918, + 0x5b6ad940, 0x1154520b, 0xc6b6d253, 0x65e054fa, 0xb202d4a2, + 0xe2df7966, 0x353df93e, 0x966b7f97, 0x4189ffcf, 0x0bb77484, + 0xdc55f4dc, 0x7f037275, 0xa8e1f22d, 0xeb7e64e3, 0x3c9ce4bb, + 0x9fca6212, 0x4828e24a, 0x02166901, 0xd5f4e959, 0x76a26ff0, + 0xa140efa8}, + {0x00000000, 0xef52b6e1, 0x05d46b83, 0xea86dd62, 0x0ba8d706, + 0xe4fa61e7, 0x0e7cbc85, 0xe12e0a64, 0x1751ae0c, 0xf80318ed, + 0x1285c58f, 0xfdd7736e, 0x1cf9790a, 0xf3abcfeb, 0x192d1289, + 0xf67fa468, 0x2ea35c18, 0xc1f1eaf9, 0x2b77379b, 0xc425817a, + 0x250b8b1e, 0xca593dff, 0x20dfe09d, 0xcf8d567c, 0x39f2f214, + 0xd6a044f5, 0x3c269997, 0xd3742f76, 0x325a2512, 0xdd0893f3, + 0x378e4e91, 0xd8dcf870, 0x5d46b830, 0xb2140ed1, 0x5892d3b3, + 0xb7c06552, 0x56ee6f36, 0xb9bcd9d7, 0x533a04b5, 0xbc68b254, + 0x4a17163c, 0xa545a0dd, 0x4fc37dbf, 0xa091cb5e, 0x41bfc13a, + 0xaeed77db, 0x446baab9, 0xab391c58, 0x73e5e428, 0x9cb752c9, + 0x76318fab, 0x9963394a, 0x784d332e, 0x971f85cf, 0x7d9958ad, + 0x92cbee4c, 0x64b44a24, 0x8be6fcc5, 0x616021a7, 0x8e329746, + 0x6f1c9d22, 0x804e2bc3, 0x6ac8f6a1, 0x859a4040, 0xba8d7060, + 0x55dfc681, 0xbf591be3, 0x500bad02, 0xb125a766, 0x5e771187, + 0xb4f1cce5, 0x5ba37a04, 0xaddcde6c, 0x428e688d, 0xa808b5ef, + 0x475a030e, 0xa674096a, 0x4926bf8b, 0xa3a062e9, 0x4cf2d408, + 0x942e2c78, 0x7b7c9a99, 0x91fa47fb, 0x7ea8f11a, 0x9f86fb7e, + 0x70d44d9f, 0x9a5290fd, 0x7500261c, 0x837f8274, 0x6c2d3495, + 0x86abe9f7, 0x69f95f16, 0x88d75572, 0x6785e393, 0x8d033ef1, + 0x62518810, 0xe7cbc850, 0x08997eb1, 0xe21fa3d3, 0x0d4d1532, + 0xec631f56, 0x0331a9b7, 0xe9b774d5, 0x06e5c234, 0xf09a665c, + 0x1fc8d0bd, 0xf54e0ddf, 0x1a1cbb3e, 0xfb32b15a, 0x146007bb, + 0xfee6dad9, 0x11b46c38, 0xc9689448, 0x263a22a9, 0xccbcffcb, + 0x23ee492a, 0xc2c0434e, 0x2d92f5af, 0xc71428cd, 0x28469e2c, + 0xde393a44, 0x316b8ca5, 0xdbed51c7, 0x34bfe726, 0xd591ed42, + 0x3ac35ba3, 0xd04586c1, 0x3f173020, 0xae6be681, 0x41395060, + 0xabbf8d02, 0x44ed3be3, 0xa5c33187, 0x4a918766, 0xa0175a04, + 0x4f45ece5, 0xb93a488d, 0x5668fe6c, 0xbcee230e, 0x53bc95ef, + 0xb2929f8b, 0x5dc0296a, 0xb746f408, 0x581442e9, 0x80c8ba99, + 0x6f9a0c78, 0x851cd11a, 0x6a4e67fb, 0x8b606d9f, 0x6432db7e, + 0x8eb4061c, 0x61e6b0fd, 0x97991495, 0x78cba274, 0x924d7f16, + 0x7d1fc9f7, 0x9c31c393, 0x73637572, 0x99e5a810, 0x76b71ef1, + 0xf32d5eb1, 0x1c7fe850, 0xf6f93532, 0x19ab83d3, 0xf88589b7, + 0x17d73f56, 0xfd51e234, 0x120354d5, 0xe47cf0bd, 0x0b2e465c, + 0xe1a89b3e, 0x0efa2ddf, 0xefd427bb, 0x0086915a, 0xea004c38, + 0x0552fad9, 0xdd8e02a9, 0x32dcb448, 0xd85a692a, 0x3708dfcb, + 0xd626d5af, 0x3974634e, 0xd3f2be2c, 0x3ca008cd, 0xcadfaca5, + 0x258d1a44, 0xcf0bc726, 0x205971c7, 0xc1777ba3, 0x2e25cd42, + 0xc4a31020, 0x2bf1a6c1, 0x14e696e1, 0xfbb42000, 0x1132fd62, + 0xfe604b83, 0x1f4e41e7, 0xf01cf706, 0x1a9a2a64, 0xf5c89c85, + 0x03b738ed, 0xece58e0c, 0x0663536e, 0xe931e58f, 0x081fefeb, + 0xe74d590a, 0x0dcb8468, 0xe2993289, 0x3a45caf9, 0xd5177c18, + 0x3f91a17a, 0xd0c3179b, 0x31ed1dff, 0xdebfab1e, 0x3439767c, + 0xdb6bc09d, 0x2d1464f5, 0xc246d214, 0x28c00f76, 0xc792b997, + 0x26bcb3f3, 0xc9ee0512, 0x2368d870, 0xcc3a6e91, 0x49a02ed1, + 0xa6f29830, 0x4c744552, 0xa326f3b3, 0x4208f9d7, 0xad5a4f36, + 0x47dc9254, 0xa88e24b5, 0x5ef180dd, 0xb1a3363c, 0x5b25eb5e, + 0xb4775dbf, 0x555957db, 0xba0be13a, 0x508d3c58, 0xbfdf8ab9, + 0x670372c9, 0x8851c428, 0x62d7194a, 0x8d85afab, 0x6caba5cf, + 0x83f9132e, 0x697fce4c, 0x862d78ad, 0x7052dcc5, 0x9f006a24, + 0x7586b746, 0x9ad401a7, 0x7bfa0bc3, 0x94a8bd22, 0x7e2e6040, + 0x917cd6a1}, + {0x00000000, 0x87a6cb43, 0xd43c90c7, 0x539a5b84, 0x730827cf, + 0xf4aeec8c, 0xa734b708, 0x20927c4b, 0xe6104f9e, 0x61b684dd, + 0x322cdf59, 0xb58a141a, 0x95186851, 0x12bea312, 0x4124f896, + 0xc68233d5, 0x1751997d, 0x90f7523e, 0xc36d09ba, 0x44cbc2f9, + 0x6459beb2, 0xe3ff75f1, 0xb0652e75, 0x37c3e536, 0xf141d6e3, + 0x76e71da0, 0x257d4624, 0xa2db8d67, 0x8249f12c, 0x05ef3a6f, + 0x567561eb, 0xd1d3aaa8, 0x2ea332fa, 0xa905f9b9, 0xfa9fa23d, + 0x7d39697e, 0x5dab1535, 0xda0dde76, 0x899785f2, 0x0e314eb1, + 0xc8b37d64, 0x4f15b627, 0x1c8feda3, 0x9b2926e0, 0xbbbb5aab, + 0x3c1d91e8, 0x6f87ca6c, 0xe821012f, 0x39f2ab87, 0xbe5460c4, + 0xedce3b40, 0x6a68f003, 0x4afa8c48, 0xcd5c470b, 0x9ec61c8f, + 0x1960d7cc, 0xdfe2e419, 0x58442f5a, 0x0bde74de, 0x8c78bf9d, + 0xaceac3d6, 0x2b4c0895, 0x78d65311, 0xff709852, 0x5d4665f4, + 0xdae0aeb7, 0x897af533, 0x0edc3e70, 0x2e4e423b, 0xa9e88978, + 0xfa72d2fc, 0x7dd419bf, 0xbb562a6a, 0x3cf0e129, 0x6f6abaad, + 0xe8cc71ee, 0xc85e0da5, 0x4ff8c6e6, 0x1c629d62, 0x9bc45621, + 0x4a17fc89, 0xcdb137ca, 0x9e2b6c4e, 0x198da70d, 0x391fdb46, + 0xbeb91005, 0xed234b81, 0x6a8580c2, 0xac07b317, 0x2ba17854, + 0x783b23d0, 0xff9de893, 0xdf0f94d8, 0x58a95f9b, 0x0b33041f, + 0x8c95cf5c, 0x73e5570e, 0xf4439c4d, 0xa7d9c7c9, 0x207f0c8a, + 0x00ed70c1, 0x874bbb82, 0xd4d1e006, 0x53772b45, 0x95f51890, + 0x1253d3d3, 0x41c98857, 0xc66f4314, 0xe6fd3f5f, 0x615bf41c, + 0x32c1af98, 0xb56764db, 0x64b4ce73, 0xe3120530, 0xb0885eb4, + 0x372e95f7, 0x17bce9bc, 0x901a22ff, 0xc380797b, 0x4426b238, + 0x82a481ed, 0x05024aae, 0x5698112a, 0xd13eda69, 0xf1aca622, + 0x760a6d61, 0x259036e5, 0xa236fda6, 0xba8ccbe8, 0x3d2a00ab, + 0x6eb05b2f, 0xe916906c, 0xc984ec27, 0x4e222764, 0x1db87ce0, + 0x9a1eb7a3, 0x5c9c8476, 0xdb3a4f35, 0x88a014b1, 0x0f06dff2, + 0x2f94a3b9, 0xa83268fa, 0xfba8337e, 0x7c0ef83d, 0xaddd5295, + 0x2a7b99d6, 0x79e1c252, 0xfe470911, 0xded5755a, 0x5973be19, + 0x0ae9e59d, 0x8d4f2ede, 0x4bcd1d0b, 0xcc6bd648, 0x9ff18dcc, + 0x1857468f, 0x38c53ac4, 0xbf63f187, 0xecf9aa03, 0x6b5f6140, + 0x942ff912, 0x13893251, 0x401369d5, 0xc7b5a296, 0xe727dedd, + 0x6081159e, 0x331b4e1a, 0xb4bd8559, 0x723fb68c, 0xf5997dcf, + 0xa603264b, 0x21a5ed08, 0x01379143, 0x86915a00, 0xd50b0184, + 0x52adcac7, 0x837e606f, 0x04d8ab2c, 0x5742f0a8, 0xd0e43beb, + 0xf07647a0, 0x77d08ce3, 0x244ad767, 0xa3ec1c24, 0x656e2ff1, + 0xe2c8e4b2, 0xb152bf36, 0x36f47475, 0x1666083e, 0x91c0c37d, + 0xc25a98f9, 0x45fc53ba, 0xe7caae1c, 0x606c655f, 0x33f63edb, + 0xb450f598, 0x94c289d3, 0x13644290, 0x40fe1914, 0xc758d257, + 0x01dae182, 0x867c2ac1, 0xd5e67145, 0x5240ba06, 0x72d2c64d, + 0xf5740d0e, 0xa6ee568a, 0x21489dc9, 0xf09b3761, 0x773dfc22, + 0x24a7a7a6, 0xa3016ce5, 0x839310ae, 0x0435dbed, 0x57af8069, + 0xd0094b2a, 0x168b78ff, 0x912db3bc, 0xc2b7e838, 0x4511237b, + 0x65835f30, 0xe2259473, 0xb1bfcff7, 0x361904b4, 0xc9699ce6, + 0x4ecf57a5, 0x1d550c21, 0x9af3c762, 0xba61bb29, 0x3dc7706a, + 0x6e5d2bee, 0xe9fbe0ad, 0x2f79d378, 0xa8df183b, 0xfb4543bf, + 0x7ce388fc, 0x5c71f4b7, 0xdbd73ff4, 0x884d6470, 0x0febaf33, + 0xde38059b, 0x599eced8, 0x0a04955c, 0x8da25e1f, 0xad302254, + 0x2a96e917, 0x790cb293, 0xfeaa79d0, 0x38284a05, 0xbf8e8146, + 0xec14dac2, 0x6bb21181, 0x4b206dca, 0xcc86a689, 0x9f1cfd0d, + 0x18ba364e}}; + +local const z_word_t FAR crc_braid_big_table[][256] = { + {0x00000000, 0x43cba687, 0xc7903cd4, 0x845b9a53, 0xcf270873, + 0x8cecaef4, 0x08b734a7, 0x4b7c9220, 0x9e4f10e6, 0xdd84b661, + 0x59df2c32, 0x1a148ab5, 0x51681895, 0x12a3be12, 0x96f82441, + 0xd53382c6, 0x7d995117, 0x3e52f790, 0xba096dc3, 0xf9c2cb44, + 0xb2be5964, 0xf175ffe3, 0x752e65b0, 0x36e5c337, 0xe3d641f1, + 0xa01de776, 0x24467d25, 0x678ddba2, 0x2cf14982, 0x6f3aef05, + 0xeb617556, 0xa8aad3d1, 0xfa32a32e, 0xb9f905a9, 0x3da29ffa, + 0x7e69397d, 0x3515ab5d, 0x76de0dda, 0xf2859789, 0xb14e310e, + 0x647db3c8, 0x27b6154f, 0xa3ed8f1c, 0xe026299b, 0xab5abbbb, + 0xe8911d3c, 0x6cca876f, 0x2f0121e8, 0x87abf239, 0xc46054be, + 0x403bceed, 0x03f0686a, 0x488cfa4a, 0x0b475ccd, 0x8f1cc69e, + 0xccd76019, 0x19e4e2df, 0x5a2f4458, 0xde74de0b, 0x9dbf788c, + 0xd6c3eaac, 0x95084c2b, 0x1153d678, 0x529870ff, 0xf465465d, + 0xb7aee0da, 0x33f57a89, 0x703edc0e, 0x3b424e2e, 0x7889e8a9, + 0xfcd272fa, 0xbf19d47d, 0x6a2a56bb, 0x29e1f03c, 0xadba6a6f, + 0xee71cce8, 0xa50d5ec8, 0xe6c6f84f, 0x629d621c, 0x2156c49b, + 0x89fc174a, 0xca37b1cd, 0x4e6c2b9e, 0x0da78d19, 0x46db1f39, + 0x0510b9be, 0x814b23ed, 0xc280856a, 0x17b307ac, 0x5478a12b, + 0xd0233b78, 0x93e89dff, 0xd8940fdf, 0x9b5fa958, 0x1f04330b, + 0x5ccf958c, 0x0e57e573, 0x4d9c43f4, 0xc9c7d9a7, 0x8a0c7f20, + 0xc170ed00, 0x82bb4b87, 0x06e0d1d4, 0x452b7753, 0x9018f595, + 0xd3d35312, 0x5788c941, 0x14436fc6, 0x5f3ffde6, 0x1cf45b61, + 0x98afc132, 0xdb6467b5, 0x73ceb464, 0x300512e3, 0xb45e88b0, + 0xf7952e37, 0xbce9bc17, 0xff221a90, 0x7b7980c3, 0x38b22644, + 0xed81a482, 0xae4a0205, 0x2a119856, 0x69da3ed1, 0x22a6acf1, + 0x616d0a76, 0xe5369025, 0xa6fd36a2, 0xe8cb8cba, 0xab002a3d, + 0x2f5bb06e, 0x6c9016e9, 0x27ec84c9, 0x6427224e, 0xe07cb81d, + 0xa3b71e9a, 0x76849c5c, 0x354f3adb, 0xb114a088, 0xf2df060f, + 0xb9a3942f, 0xfa6832a8, 0x7e33a8fb, 0x3df80e7c, 0x9552ddad, + 0xd6997b2a, 0x52c2e179, 0x110947fe, 0x5a75d5de, 0x19be7359, + 0x9de5e90a, 0xde2e4f8d, 0x0b1dcd4b, 0x48d66bcc, 0xcc8df19f, + 0x8f465718, 0xc43ac538, 0x87f163bf, 0x03aaf9ec, 0x40615f6b, + 0x12f92f94, 0x51328913, 0xd5691340, 0x96a2b5c7, 0xddde27e7, + 0x9e158160, 0x1a4e1b33, 0x5985bdb4, 0x8cb63f72, 0xcf7d99f5, + 0x4b2603a6, 0x08eda521, 0x43913701, 0x005a9186, 0x84010bd5, + 0xc7caad52, 0x6f607e83, 0x2cabd804, 0xa8f04257, 0xeb3be4d0, + 0xa04776f0, 0xe38cd077, 0x67d74a24, 0x241ceca3, 0xf12f6e65, + 0xb2e4c8e2, 0x36bf52b1, 0x7574f436, 0x3e086616, 0x7dc3c091, + 0xf9985ac2, 0xba53fc45, 0x1caecae7, 0x5f656c60, 0xdb3ef633, + 0x98f550b4, 0xd389c294, 0x90426413, 0x1419fe40, 0x57d258c7, + 0x82e1da01, 0xc12a7c86, 0x4571e6d5, 0x06ba4052, 0x4dc6d272, + 0x0e0d74f5, 0x8a56eea6, 0xc99d4821, 0x61379bf0, 0x22fc3d77, + 0xa6a7a724, 0xe56c01a3, 0xae109383, 0xeddb3504, 0x6980af57, + 0x2a4b09d0, 0xff788b16, 0xbcb32d91, 0x38e8b7c2, 0x7b231145, + 0x305f8365, 0x739425e2, 0xf7cfbfb1, 0xb4041936, 0xe69c69c9, + 0xa557cf4e, 0x210c551d, 0x62c7f39a, 0x29bb61ba, 0x6a70c73d, + 0xee2b5d6e, 0xade0fbe9, 0x78d3792f, 0x3b18dfa8, 0xbf4345fb, + 0xfc88e37c, 0xb7f4715c, 0xf43fd7db, 0x70644d88, 0x33afeb0f, + 0x9b0538de, 0xd8ce9e59, 0x5c95040a, 0x1f5ea28d, 0x542230ad, + 0x17e9962a, 0x93b20c79, 0xd079aafe, 0x054a2838, 0x46818ebf, + 0xc2da14ec, 0x8111b26b, 0xca6d204b, 0x89a686cc, 0x0dfd1c9f, + 0x4e36ba18}, + {0x00000000, 0xe1b652ef, 0x836bd405, 0x62dd86ea, 0x06d7a80b, + 0xe761fae4, 0x85bc7c0e, 0x640a2ee1, 0x0cae5117, 0xed1803f8, + 0x8fc58512, 0x6e73d7fd, 0x0a79f91c, 0xebcfabf3, 0x89122d19, + 0x68a47ff6, 0x185ca32e, 0xf9eaf1c1, 0x9b37772b, 0x7a8125c4, + 0x1e8b0b25, 0xff3d59ca, 0x9de0df20, 0x7c568dcf, 0x14f2f239, + 0xf544a0d6, 0x9799263c, 0x762f74d3, 0x12255a32, 0xf39308dd, + 0x914e8e37, 0x70f8dcd8, 0x30b8465d, 0xd10e14b2, 0xb3d39258, + 0x5265c0b7, 0x366fee56, 0xd7d9bcb9, 0xb5043a53, 0x54b268bc, + 0x3c16174a, 0xdda045a5, 0xbf7dc34f, 0x5ecb91a0, 0x3ac1bf41, + 0xdb77edae, 0xb9aa6b44, 0x581c39ab, 0x28e4e573, 0xc952b79c, + 0xab8f3176, 0x4a396399, 0x2e334d78, 0xcf851f97, 0xad58997d, + 0x4ceecb92, 0x244ab464, 0xc5fce68b, 0xa7216061, 0x4697328e, + 0x229d1c6f, 0xc32b4e80, 0xa1f6c86a, 0x40409a85, 0x60708dba, + 0x81c6df55, 0xe31b59bf, 0x02ad0b50, 0x66a725b1, 0x8711775e, + 0xe5ccf1b4, 0x047aa35b, 0x6cdedcad, 0x8d688e42, 0xefb508a8, + 0x0e035a47, 0x6a0974a6, 0x8bbf2649, 0xe962a0a3, 0x08d4f24c, + 0x782c2e94, 0x999a7c7b, 0xfb47fa91, 0x1af1a87e, 0x7efb869f, + 0x9f4dd470, 0xfd90529a, 0x1c260075, 0x74827f83, 0x95342d6c, + 0xf7e9ab86, 0x165ff969, 0x7255d788, 0x93e38567, 0xf13e038d, + 0x10885162, 0x50c8cbe7, 0xb17e9908, 0xd3a31fe2, 0x32154d0d, + 0x561f63ec, 0xb7a93103, 0xd574b7e9, 0x34c2e506, 0x5c669af0, + 0xbdd0c81f, 0xdf0d4ef5, 0x3ebb1c1a, 0x5ab132fb, 0xbb076014, + 0xd9dae6fe, 0x386cb411, 0x489468c9, 0xa9223a26, 0xcbffbccc, + 0x2a49ee23, 0x4e43c0c2, 0xaff5922d, 0xcd2814c7, 0x2c9e4628, + 0x443a39de, 0xa58c6b31, 0xc751eddb, 0x26e7bf34, 0x42ed91d5, + 0xa35bc33a, 0xc18645d0, 0x2030173f, 0x81e66bae, 0x60503941, + 0x028dbfab, 0xe33bed44, 0x8731c3a5, 0x6687914a, 0x045a17a0, + 0xe5ec454f, 0x8d483ab9, 0x6cfe6856, 0x0e23eebc, 0xef95bc53, + 0x8b9f92b2, 0x6a29c05d, 0x08f446b7, 0xe9421458, 0x99bac880, + 0x780c9a6f, 0x1ad11c85, 0xfb674e6a, 0x9f6d608b, 0x7edb3264, + 0x1c06b48e, 0xfdb0e661, 0x95149997, 0x74a2cb78, 0x167f4d92, + 0xf7c91f7d, 0x93c3319c, 0x72756373, 0x10a8e599, 0xf11eb776, + 0xb15e2df3, 0x50e87f1c, 0x3235f9f6, 0xd383ab19, 0xb78985f8, + 0x563fd717, 0x34e251fd, 0xd5540312, 0xbdf07ce4, 0x5c462e0b, + 0x3e9ba8e1, 0xdf2dfa0e, 0xbb27d4ef, 0x5a918600, 0x384c00ea, + 0xd9fa5205, 0xa9028edd, 0x48b4dc32, 0x2a695ad8, 0xcbdf0837, + 0xafd526d6, 0x4e637439, 0x2cbef2d3, 0xcd08a03c, 0xa5acdfca, + 0x441a8d25, 0x26c70bcf, 0xc7715920, 0xa37b77c1, 0x42cd252e, + 0x2010a3c4, 0xc1a6f12b, 0xe196e614, 0x0020b4fb, 0x62fd3211, + 0x834b60fe, 0xe7414e1f, 0x06f71cf0, 0x642a9a1a, 0x859cc8f5, + 0xed38b703, 0x0c8ee5ec, 0x6e536306, 0x8fe531e9, 0xebef1f08, + 0x0a594de7, 0x6884cb0d, 0x893299e2, 0xf9ca453a, 0x187c17d5, + 0x7aa1913f, 0x9b17c3d0, 0xff1ded31, 0x1eabbfde, 0x7c763934, + 0x9dc06bdb, 0xf564142d, 0x14d246c2, 0x760fc028, 0x97b992c7, + 0xf3b3bc26, 0x1205eec9, 0x70d86823, 0x916e3acc, 0xd12ea049, + 0x3098f2a6, 0x5245744c, 0xb3f326a3, 0xd7f90842, 0x364f5aad, + 0x5492dc47, 0xb5248ea8, 0xdd80f15e, 0x3c36a3b1, 0x5eeb255b, + 0xbf5d77b4, 0xdb575955, 0x3ae10bba, 0x583c8d50, 0xb98adfbf, + 0xc9720367, 0x28c45188, 0x4a19d762, 0xabaf858d, 0xcfa5ab6c, + 0x2e13f983, 0x4cce7f69, 0xad782d86, 0xc5dc5270, 0x246a009f, + 0x46b78675, 0xa701d49a, 0xc30bfa7b, 0x22bda894, 0x40602e7e, + 0xa1d67c91}, + {0x00000000, 0x5880e2d7, 0xf106b474, 0xa98656a3, 0xe20d68e9, + 0xba8d8a3e, 0x130bdc9d, 0x4b8b3e4a, 0x851da109, 0xdd9d43de, + 0x741b157d, 0x2c9bf7aa, 0x6710c9e0, 0x3f902b37, 0x96167d94, + 0xce969f43, 0x0a3b4213, 0x52bba0c4, 0xfb3df667, 0xa3bd14b0, + 0xe8362afa, 0xb0b6c82d, 0x19309e8e, 0x41b07c59, 0x8f26e31a, + 0xd7a601cd, 0x7e20576e, 0x26a0b5b9, 0x6d2b8bf3, 0x35ab6924, + 0x9c2d3f87, 0xc4addd50, 0x14768426, 0x4cf666f1, 0xe5703052, + 0xbdf0d285, 0xf67beccf, 0xaefb0e18, 0x077d58bb, 0x5ffdba6c, + 0x916b252f, 0xc9ebc7f8, 0x606d915b, 0x38ed738c, 0x73664dc6, + 0x2be6af11, 0x8260f9b2, 0xdae01b65, 0x1e4dc635, 0x46cd24e2, + 0xef4b7241, 0xb7cb9096, 0xfc40aedc, 0xa4c04c0b, 0x0d461aa8, + 0x55c6f87f, 0x9b50673c, 0xc3d085eb, 0x6a56d348, 0x32d6319f, + 0x795d0fd5, 0x21dded02, 0x885bbba1, 0xd0db5976, 0x28ec084d, + 0x706cea9a, 0xd9eabc39, 0x816a5eee, 0xcae160a4, 0x92618273, + 0x3be7d4d0, 0x63673607, 0xadf1a944, 0xf5714b93, 0x5cf71d30, + 0x0477ffe7, 0x4ffcc1ad, 0x177c237a, 0xbefa75d9, 0xe67a970e, + 0x22d74a5e, 0x7a57a889, 0xd3d1fe2a, 0x8b511cfd, 0xc0da22b7, + 0x985ac060, 0x31dc96c3, 0x695c7414, 0xa7caeb57, 0xff4a0980, + 0x56cc5f23, 0x0e4cbdf4, 0x45c783be, 0x1d476169, 0xb4c137ca, + 0xec41d51d, 0x3c9a8c6b, 0x641a6ebc, 0xcd9c381f, 0x951cdac8, + 0xde97e482, 0x86170655, 0x2f9150f6, 0x7711b221, 0xb9872d62, + 0xe107cfb5, 0x48819916, 0x10017bc1, 0x5b8a458b, 0x030aa75c, + 0xaa8cf1ff, 0xf20c1328, 0x36a1ce78, 0x6e212caf, 0xc7a77a0c, + 0x9f2798db, 0xd4aca691, 0x8c2c4446, 0x25aa12e5, 0x7d2af032, + 0xb3bc6f71, 0xeb3c8da6, 0x42badb05, 0x1a3a39d2, 0x51b10798, + 0x0931e54f, 0xa0b7b3ec, 0xf837513b, 0x50d8119a, 0x0858f34d, + 0xa1dea5ee, 0xf95e4739, 0xb2d57973, 0xea559ba4, 0x43d3cd07, + 0x1b532fd0, 0xd5c5b093, 0x8d455244, 0x24c304e7, 0x7c43e630, + 0x37c8d87a, 0x6f483aad, 0xc6ce6c0e, 0x9e4e8ed9, 0x5ae35389, + 0x0263b15e, 0xabe5e7fd, 0xf365052a, 0xb8ee3b60, 0xe06ed9b7, + 0x49e88f14, 0x11686dc3, 0xdffef280, 0x877e1057, 0x2ef846f4, + 0x7678a423, 0x3df39a69, 0x657378be, 0xccf52e1d, 0x9475ccca, + 0x44ae95bc, 0x1c2e776b, 0xb5a821c8, 0xed28c31f, 0xa6a3fd55, + 0xfe231f82, 0x57a54921, 0x0f25abf6, 0xc1b334b5, 0x9933d662, + 0x30b580c1, 0x68356216, 0x23be5c5c, 0x7b3ebe8b, 0xd2b8e828, + 0x8a380aff, 0x4e95d7af, 0x16153578, 0xbf9363db, 0xe713810c, + 0xac98bf46, 0xf4185d91, 0x5d9e0b32, 0x051ee9e5, 0xcb8876a6, + 0x93089471, 0x3a8ec2d2, 0x620e2005, 0x29851e4f, 0x7105fc98, + 0xd883aa3b, 0x800348ec, 0x783419d7, 0x20b4fb00, 0x8932ada3, + 0xd1b24f74, 0x9a39713e, 0xc2b993e9, 0x6b3fc54a, 0x33bf279d, + 0xfd29b8de, 0xa5a95a09, 0x0c2f0caa, 0x54afee7d, 0x1f24d037, + 0x47a432e0, 0xee226443, 0xb6a28694, 0x720f5bc4, 0x2a8fb913, + 0x8309efb0, 0xdb890d67, 0x9002332d, 0xc882d1fa, 0x61048759, + 0x3984658e, 0xf712facd, 0xaf92181a, 0x06144eb9, 0x5e94ac6e, + 0x151f9224, 0x4d9f70f3, 0xe4192650, 0xbc99c487, 0x6c429df1, + 0x34c27f26, 0x9d442985, 0xc5c4cb52, 0x8e4ff518, 0xd6cf17cf, + 0x7f49416c, 0x27c9a3bb, 0xe95f3cf8, 0xb1dfde2f, 0x1859888c, + 0x40d96a5b, 0x0b525411, 0x53d2b6c6, 0xfa54e065, 0xa2d402b2, + 0x6679dfe2, 0x3ef93d35, 0x977f6b96, 0xcfff8941, 0x8474b70b, + 0xdcf455dc, 0x7572037f, 0x2df2e1a8, 0xe3647eeb, 0xbbe49c3c, + 0x1262ca9f, 0x4ae22848, 0x01691602, 0x59e9f4d5, 0xf06fa276, + 0xa8ef40a1}, + {0x00000000, 0x463b6765, 0x8c76ceca, 0xca4da9af, 0x59ebed4e, + 0x1fd08a2b, 0xd59d2384, 0x93a644e1, 0xb2d6db9d, 0xf4edbcf8, + 0x3ea01557, 0x789b7232, 0xeb3d36d3, 0xad0651b6, 0x674bf819, + 0x21709f7c, 0x25abc6e0, 0x6390a185, 0xa9dd082a, 0xefe66f4f, + 0x7c402bae, 0x3a7b4ccb, 0xf036e564, 0xb60d8201, 0x977d1d7d, + 0xd1467a18, 0x1b0bd3b7, 0x5d30b4d2, 0xce96f033, 0x88ad9756, + 0x42e03ef9, 0x04db599c, 0x0b50fc1a, 0x4d6b9b7f, 0x872632d0, + 0xc11d55b5, 0x52bb1154, 0x14807631, 0xdecddf9e, 0x98f6b8fb, + 0xb9862787, 0xffbd40e2, 0x35f0e94d, 0x73cb8e28, 0xe06dcac9, + 0xa656adac, 0x6c1b0403, 0x2a206366, 0x2efb3afa, 0x68c05d9f, + 0xa28df430, 0xe4b69355, 0x7710d7b4, 0x312bb0d1, 0xfb66197e, + 0xbd5d7e1b, 0x9c2de167, 0xda168602, 0x105b2fad, 0x566048c8, + 0xc5c60c29, 0x83fd6b4c, 0x49b0c2e3, 0x0f8ba586, 0x16a0f835, + 0x509b9f50, 0x9ad636ff, 0xdced519a, 0x4f4b157b, 0x0970721e, + 0xc33ddbb1, 0x8506bcd4, 0xa47623a8, 0xe24d44cd, 0x2800ed62, + 0x6e3b8a07, 0xfd9dcee6, 0xbba6a983, 0x71eb002c, 0x37d06749, + 0x330b3ed5, 0x753059b0, 0xbf7df01f, 0xf946977a, 0x6ae0d39b, + 0x2cdbb4fe, 0xe6961d51, 0xa0ad7a34, 0x81dde548, 0xc7e6822d, + 0x0dab2b82, 0x4b904ce7, 0xd8360806, 0x9e0d6f63, 0x5440c6cc, + 0x127ba1a9, 0x1df0042f, 0x5bcb634a, 0x9186cae5, 0xd7bdad80, + 0x441be961, 0x02208e04, 0xc86d27ab, 0x8e5640ce, 0xaf26dfb2, + 0xe91db8d7, 0x23501178, 0x656b761d, 0xf6cd32fc, 0xb0f65599, + 0x7abbfc36, 0x3c809b53, 0x385bc2cf, 0x7e60a5aa, 0xb42d0c05, + 0xf2166b60, 0x61b02f81, 0x278b48e4, 0xedc6e14b, 0xabfd862e, + 0x8a8d1952, 0xccb67e37, 0x06fbd798, 0x40c0b0fd, 0xd366f41c, + 0x955d9379, 0x5f103ad6, 0x192b5db3, 0x2c40f16b, 0x6a7b960e, + 0xa0363fa1, 0xe60d58c4, 0x75ab1c25, 0x33907b40, 0xf9ddd2ef, + 0xbfe6b58a, 0x9e962af6, 0xd8ad4d93, 0x12e0e43c, 0x54db8359, + 0xc77dc7b8, 0x8146a0dd, 0x4b0b0972, 0x0d306e17, 0x09eb378b, + 0x4fd050ee, 0x859df941, 0xc3a69e24, 0x5000dac5, 0x163bbda0, + 0xdc76140f, 0x9a4d736a, 0xbb3dec16, 0xfd068b73, 0x374b22dc, + 0x717045b9, 0xe2d60158, 0xa4ed663d, 0x6ea0cf92, 0x289ba8f7, + 0x27100d71, 0x612b6a14, 0xab66c3bb, 0xed5da4de, 0x7efbe03f, + 0x38c0875a, 0xf28d2ef5, 0xb4b64990, 0x95c6d6ec, 0xd3fdb189, + 0x19b01826, 0x5f8b7f43, 0xcc2d3ba2, 0x8a165cc7, 0x405bf568, + 0x0660920d, 0x02bbcb91, 0x4480acf4, 0x8ecd055b, 0xc8f6623e, + 0x5b5026df, 0x1d6b41ba, 0xd726e815, 0x911d8f70, 0xb06d100c, + 0xf6567769, 0x3c1bdec6, 0x7a20b9a3, 0xe986fd42, 0xafbd9a27, + 0x65f03388, 0x23cb54ed, 0x3ae0095e, 0x7cdb6e3b, 0xb696c794, + 0xf0ada0f1, 0x630be410, 0x25308375, 0xef7d2ada, 0xa9464dbf, + 0x8836d2c3, 0xce0db5a6, 0x04401c09, 0x427b7b6c, 0xd1dd3f8d, + 0x97e658e8, 0x5dabf147, 0x1b909622, 0x1f4bcfbe, 0x5970a8db, + 0x933d0174, 0xd5066611, 0x46a022f0, 0x009b4595, 0xcad6ec3a, + 0x8ced8b5f, 0xad9d1423, 0xeba67346, 0x21ebdae9, 0x67d0bd8c, + 0xf476f96d, 0xb24d9e08, 0x780037a7, 0x3e3b50c2, 0x31b0f544, + 0x778b9221, 0xbdc63b8e, 0xfbfd5ceb, 0x685b180a, 0x2e607f6f, + 0xe42dd6c0, 0xa216b1a5, 0x83662ed9, 0xc55d49bc, 0x0f10e013, + 0x492b8776, 0xda8dc397, 0x9cb6a4f2, 0x56fb0d5d, 0x10c06a38, + 0x141b33a4, 0x522054c1, 0x986dfd6e, 0xde569a0b, 0x4df0deea, + 0x0bcbb98f, 0xc1861020, 0x87bd7745, 0xa6cde839, 0xe0f68f5c, + 0x2abb26f3, 0x6c804196, 0xff260577, 0xb91d6212, 0x7350cbbd, + 0x356bacd8}}; + +#endif + +#endif + +#if N == 6 + +#if W == 8 + +local const z_crc_t FAR crc_braid_table[][256] = { + {0x00000000, 0x3db1ecdc, 0x7b63d9b8, 0x46d23564, 0xf6c7b370, + 0xcb765fac, 0x8da46ac8, 0xb0158614, 0x36fe60a1, 0x0b4f8c7d, + 0x4d9db919, 0x702c55c5, 0xc039d3d1, 0xfd883f0d, 0xbb5a0a69, + 0x86ebe6b5, 0x6dfcc142, 0x504d2d9e, 0x169f18fa, 0x2b2ef426, + 0x9b3b7232, 0xa68a9eee, 0xe058ab8a, 0xdde94756, 0x5b02a1e3, + 0x66b34d3f, 0x2061785b, 0x1dd09487, 0xadc51293, 0x9074fe4f, + 0xd6a6cb2b, 0xeb1727f7, 0xdbf98284, 0xe6486e58, 0xa09a5b3c, + 0x9d2bb7e0, 0x2d3e31f4, 0x108fdd28, 0x565de84c, 0x6bec0490, + 0xed07e225, 0xd0b60ef9, 0x96643b9d, 0xabd5d741, 0x1bc05155, + 0x2671bd89, 0x60a388ed, 0x5d126431, 0xb60543c6, 0x8bb4af1a, + 0xcd669a7e, 0xf0d776a2, 0x40c2f0b6, 0x7d731c6a, 0x3ba1290e, + 0x0610c5d2, 0x80fb2367, 0xbd4acfbb, 0xfb98fadf, 0xc6291603, + 0x763c9017, 0x4b8d7ccb, 0x0d5f49af, 0x30eea573, 0x6c820349, + 0x5133ef95, 0x17e1daf1, 0x2a50362d, 0x9a45b039, 0xa7f45ce5, + 0xe1266981, 0xdc97855d, 0x5a7c63e8, 0x67cd8f34, 0x211fba50, + 0x1cae568c, 0xacbbd098, 0x910a3c44, 0xd7d80920, 0xea69e5fc, + 0x017ec20b, 0x3ccf2ed7, 0x7a1d1bb3, 0x47acf76f, 0xf7b9717b, + 0xca089da7, 0x8cdaa8c3, 0xb16b441f, 0x3780a2aa, 0x0a314e76, + 0x4ce37b12, 0x715297ce, 0xc14711da, 0xfcf6fd06, 0xba24c862, + 0x879524be, 0xb77b81cd, 0x8aca6d11, 0xcc185875, 0xf1a9b4a9, + 0x41bc32bd, 0x7c0dde61, 0x3adfeb05, 0x076e07d9, 0x8185e16c, + 0xbc340db0, 0xfae638d4, 0xc757d408, 0x7742521c, 0x4af3bec0, + 0x0c218ba4, 0x31906778, 0xda87408f, 0xe736ac53, 0xa1e49937, + 0x9c5575eb, 0x2c40f3ff, 0x11f11f23, 0x57232a47, 0x6a92c69b, + 0xec79202e, 0xd1c8ccf2, 0x971af996, 0xaaab154a, 0x1abe935e, + 0x270f7f82, 0x61dd4ae6, 0x5c6ca63a, 0xd9040692, 0xe4b5ea4e, + 0xa267df2a, 0x9fd633f6, 0x2fc3b5e2, 0x1272593e, 0x54a06c5a, + 0x69118086, 0xeffa6633, 0xd24b8aef, 0x9499bf8b, 0xa9285357, + 0x193dd543, 0x248c399f, 0x625e0cfb, 0x5fefe027, 0xb4f8c7d0, + 0x89492b0c, 0xcf9b1e68, 0xf22af2b4, 0x423f74a0, 0x7f8e987c, + 0x395cad18, 0x04ed41c4, 0x8206a771, 0xbfb74bad, 0xf9657ec9, + 0xc4d49215, 0x74c11401, 0x4970f8dd, 0x0fa2cdb9, 0x32132165, + 0x02fd8416, 0x3f4c68ca, 0x799e5dae, 0x442fb172, 0xf43a3766, + 0xc98bdbba, 0x8f59eede, 0xb2e80202, 0x3403e4b7, 0x09b2086b, + 0x4f603d0f, 0x72d1d1d3, 0xc2c457c7, 0xff75bb1b, 0xb9a78e7f, + 0x841662a3, 0x6f014554, 0x52b0a988, 0x14629cec, 0x29d37030, + 0x99c6f624, 0xa4771af8, 0xe2a52f9c, 0xdf14c340, 0x59ff25f5, + 0x644ec929, 0x229cfc4d, 0x1f2d1091, 0xaf389685, 0x92897a59, + 0xd45b4f3d, 0xe9eaa3e1, 0xb58605db, 0x8837e907, 0xcee5dc63, + 0xf35430bf, 0x4341b6ab, 0x7ef05a77, 0x38226f13, 0x059383cf, + 0x8378657a, 0xbec989a6, 0xf81bbcc2, 0xc5aa501e, 0x75bfd60a, + 0x480e3ad6, 0x0edc0fb2, 0x336de36e, 0xd87ac499, 0xe5cb2845, + 0xa3191d21, 0x9ea8f1fd, 0x2ebd77e9, 0x130c9b35, 0x55deae51, + 0x686f428d, 0xee84a438, 0xd33548e4, 0x95e77d80, 0xa856915c, + 0x18431748, 0x25f2fb94, 0x6320cef0, 0x5e91222c, 0x6e7f875f, + 0x53ce6b83, 0x151c5ee7, 0x28adb23b, 0x98b8342f, 0xa509d8f3, + 0xe3dbed97, 0xde6a014b, 0x5881e7fe, 0x65300b22, 0x23e23e46, + 0x1e53d29a, 0xae46548e, 0x93f7b852, 0xd5258d36, 0xe89461ea, + 0x0383461d, 0x3e32aac1, 0x78e09fa5, 0x45517379, 0xf544f56d, + 0xc8f519b1, 0x8e272cd5, 0xb396c009, 0x357d26bc, 0x08ccca60, + 0x4e1eff04, 0x73af13d8, 0xc3ba95cc, 0xfe0b7910, 0xb8d94c74, + 0x8568a0a8}, + {0x00000000, 0x69790b65, 0xd2f216ca, 0xbb8b1daf, 0x7e952bd5, + 0x17ec20b0, 0xac673d1f, 0xc51e367a, 0xfd2a57aa, 0x94535ccf, + 0x2fd84160, 0x46a14a05, 0x83bf7c7f, 0xeac6771a, 0x514d6ab5, + 0x383461d0, 0x2125a915, 0x485ca270, 0xf3d7bfdf, 0x9aaeb4ba, + 0x5fb082c0, 0x36c989a5, 0x8d42940a, 0xe43b9f6f, 0xdc0ffebf, + 0xb576f5da, 0x0efde875, 0x6784e310, 0xa29ad56a, 0xcbe3de0f, + 0x7068c3a0, 0x1911c8c5, 0x424b522a, 0x2b32594f, 0x90b944e0, + 0xf9c04f85, 0x3cde79ff, 0x55a7729a, 0xee2c6f35, 0x87556450, + 0xbf610580, 0xd6180ee5, 0x6d93134a, 0x04ea182f, 0xc1f42e55, + 0xa88d2530, 0x1306389f, 0x7a7f33fa, 0x636efb3f, 0x0a17f05a, + 0xb19cedf5, 0xd8e5e690, 0x1dfbd0ea, 0x7482db8f, 0xcf09c620, + 0xa670cd45, 0x9e44ac95, 0xf73da7f0, 0x4cb6ba5f, 0x25cfb13a, + 0xe0d18740, 0x89a88c25, 0x3223918a, 0x5b5a9aef, 0x8496a454, + 0xedefaf31, 0x5664b29e, 0x3f1db9fb, 0xfa038f81, 0x937a84e4, + 0x28f1994b, 0x4188922e, 0x79bcf3fe, 0x10c5f89b, 0xab4ee534, + 0xc237ee51, 0x0729d82b, 0x6e50d34e, 0xd5dbcee1, 0xbca2c584, + 0xa5b30d41, 0xccca0624, 0x77411b8b, 0x1e3810ee, 0xdb262694, + 0xb25f2df1, 0x09d4305e, 0x60ad3b3b, 0x58995aeb, 0x31e0518e, + 0x8a6b4c21, 0xe3124744, 0x260c713e, 0x4f757a5b, 0xf4fe67f4, + 0x9d876c91, 0xc6ddf67e, 0xafa4fd1b, 0x142fe0b4, 0x7d56ebd1, + 0xb848ddab, 0xd131d6ce, 0x6abacb61, 0x03c3c004, 0x3bf7a1d4, + 0x528eaab1, 0xe905b71e, 0x807cbc7b, 0x45628a01, 0x2c1b8164, + 0x97909ccb, 0xfee997ae, 0xe7f85f6b, 0x8e81540e, 0x350a49a1, + 0x5c7342c4, 0x996d74be, 0xf0147fdb, 0x4b9f6274, 0x22e66911, + 0x1ad208c1, 0x73ab03a4, 0xc8201e0b, 0xa159156e, 0x64472314, + 0x0d3e2871, 0xb6b535de, 0xdfcc3ebb, 0xd25c4ee9, 0xbb25458c, + 0x00ae5823, 0x69d75346, 0xacc9653c, 0xc5b06e59, 0x7e3b73f6, + 0x17427893, 0x2f761943, 0x460f1226, 0xfd840f89, 0x94fd04ec, + 0x51e33296, 0x389a39f3, 0x8311245c, 0xea682f39, 0xf379e7fc, + 0x9a00ec99, 0x218bf136, 0x48f2fa53, 0x8deccc29, 0xe495c74c, + 0x5f1edae3, 0x3667d186, 0x0e53b056, 0x672abb33, 0xdca1a69c, + 0xb5d8adf9, 0x70c69b83, 0x19bf90e6, 0xa2348d49, 0xcb4d862c, + 0x90171cc3, 0xf96e17a6, 0x42e50a09, 0x2b9c016c, 0xee823716, + 0x87fb3c73, 0x3c7021dc, 0x55092ab9, 0x6d3d4b69, 0x0444400c, + 0xbfcf5da3, 0xd6b656c6, 0x13a860bc, 0x7ad16bd9, 0xc15a7676, + 0xa8237d13, 0xb132b5d6, 0xd84bbeb3, 0x63c0a31c, 0x0ab9a879, + 0xcfa79e03, 0xa6de9566, 0x1d5588c9, 0x742c83ac, 0x4c18e27c, + 0x2561e919, 0x9eeaf4b6, 0xf793ffd3, 0x328dc9a9, 0x5bf4c2cc, + 0xe07fdf63, 0x8906d406, 0x56caeabd, 0x3fb3e1d8, 0x8438fc77, + 0xed41f712, 0x285fc168, 0x4126ca0d, 0xfaadd7a2, 0x93d4dcc7, + 0xabe0bd17, 0xc299b672, 0x7912abdd, 0x106ba0b8, 0xd57596c2, + 0xbc0c9da7, 0x07878008, 0x6efe8b6d, 0x77ef43a8, 0x1e9648cd, + 0xa51d5562, 0xcc645e07, 0x097a687d, 0x60036318, 0xdb887eb7, + 0xb2f175d2, 0x8ac51402, 0xe3bc1f67, 0x583702c8, 0x314e09ad, + 0xf4503fd7, 0x9d2934b2, 0x26a2291d, 0x4fdb2278, 0x1481b897, + 0x7df8b3f2, 0xc673ae5d, 0xaf0aa538, 0x6a149342, 0x036d9827, + 0xb8e68588, 0xd19f8eed, 0xe9abef3d, 0x80d2e458, 0x3b59f9f7, + 0x5220f292, 0x973ec4e8, 0xfe47cf8d, 0x45ccd222, 0x2cb5d947, + 0x35a41182, 0x5cdd1ae7, 0xe7560748, 0x8e2f0c2d, 0x4b313a57, + 0x22483132, 0x99c32c9d, 0xf0ba27f8, 0xc88e4628, 0xa1f74d4d, + 0x1a7c50e2, 0x73055b87, 0xb61b6dfd, 0xdf626698, 0x64e97b37, + 0x0d907052}, + {0x00000000, 0x7fc99b93, 0xff933726, 0x805aacb5, 0x2457680d, + 0x5b9ef39e, 0xdbc45f2b, 0xa40dc4b8, 0x48aed01a, 0x37674b89, + 0xb73de73c, 0xc8f47caf, 0x6cf9b817, 0x13302384, 0x936a8f31, + 0xeca314a2, 0x915da034, 0xee943ba7, 0x6ece9712, 0x11070c81, + 0xb50ac839, 0xcac353aa, 0x4a99ff1f, 0x3550648c, 0xd9f3702e, + 0xa63aebbd, 0x26604708, 0x59a9dc9b, 0xfda41823, 0x826d83b0, + 0x02372f05, 0x7dfeb496, 0xf9ca4629, 0x8603ddba, 0x0659710f, + 0x7990ea9c, 0xdd9d2e24, 0xa254b5b7, 0x220e1902, 0x5dc78291, + 0xb1649633, 0xcead0da0, 0x4ef7a115, 0x313e3a86, 0x9533fe3e, + 0xeafa65ad, 0x6aa0c918, 0x1569528b, 0x6897e61d, 0x175e7d8e, + 0x9704d13b, 0xe8cd4aa8, 0x4cc08e10, 0x33091583, 0xb353b936, + 0xcc9a22a5, 0x20393607, 0x5ff0ad94, 0xdfaa0121, 0xa0639ab2, + 0x046e5e0a, 0x7ba7c599, 0xfbfd692c, 0x8434f2bf, 0x28e58a13, + 0x572c1180, 0xd776bd35, 0xa8bf26a6, 0x0cb2e21e, 0x737b798d, + 0xf321d538, 0x8ce84eab, 0x604b5a09, 0x1f82c19a, 0x9fd86d2f, + 0xe011f6bc, 0x441c3204, 0x3bd5a997, 0xbb8f0522, 0xc4469eb1, + 0xb9b82a27, 0xc671b1b4, 0x462b1d01, 0x39e28692, 0x9def422a, + 0xe226d9b9, 0x627c750c, 0x1db5ee9f, 0xf116fa3d, 0x8edf61ae, + 0x0e85cd1b, 0x714c5688, 0xd5419230, 0xaa8809a3, 0x2ad2a516, + 0x551b3e85, 0xd12fcc3a, 0xaee657a9, 0x2ebcfb1c, 0x5175608f, + 0xf578a437, 0x8ab13fa4, 0x0aeb9311, 0x75220882, 0x99811c20, + 0xe64887b3, 0x66122b06, 0x19dbb095, 0xbdd6742d, 0xc21fefbe, + 0x4245430b, 0x3d8cd898, 0x40726c0e, 0x3fbbf79d, 0xbfe15b28, + 0xc028c0bb, 0x64250403, 0x1bec9f90, 0x9bb63325, 0xe47fa8b6, + 0x08dcbc14, 0x77152787, 0xf74f8b32, 0x888610a1, 0x2c8bd419, + 0x53424f8a, 0xd318e33f, 0xacd178ac, 0x51cb1426, 0x2e028fb5, + 0xae582300, 0xd191b893, 0x759c7c2b, 0x0a55e7b8, 0x8a0f4b0d, + 0xf5c6d09e, 0x1965c43c, 0x66ac5faf, 0xe6f6f31a, 0x993f6889, + 0x3d32ac31, 0x42fb37a2, 0xc2a19b17, 0xbd680084, 0xc096b412, + 0xbf5f2f81, 0x3f058334, 0x40cc18a7, 0xe4c1dc1f, 0x9b08478c, + 0x1b52eb39, 0x649b70aa, 0x88386408, 0xf7f1ff9b, 0x77ab532e, + 0x0862c8bd, 0xac6f0c05, 0xd3a69796, 0x53fc3b23, 0x2c35a0b0, + 0xa801520f, 0xd7c8c99c, 0x57926529, 0x285bfeba, 0x8c563a02, + 0xf39fa191, 0x73c50d24, 0x0c0c96b7, 0xe0af8215, 0x9f661986, + 0x1f3cb533, 0x60f52ea0, 0xc4f8ea18, 0xbb31718b, 0x3b6bdd3e, + 0x44a246ad, 0x395cf23b, 0x469569a8, 0xc6cfc51d, 0xb9065e8e, + 0x1d0b9a36, 0x62c201a5, 0xe298ad10, 0x9d513683, 0x71f22221, + 0x0e3bb9b2, 0x8e611507, 0xf1a88e94, 0x55a54a2c, 0x2a6cd1bf, + 0xaa367d0a, 0xd5ffe699, 0x792e9e35, 0x06e705a6, 0x86bda913, + 0xf9743280, 0x5d79f638, 0x22b06dab, 0xa2eac11e, 0xdd235a8d, + 0x31804e2f, 0x4e49d5bc, 0xce137909, 0xb1dae29a, 0x15d72622, + 0x6a1ebdb1, 0xea441104, 0x958d8a97, 0xe8733e01, 0x97baa592, + 0x17e00927, 0x682992b4, 0xcc24560c, 0xb3edcd9f, 0x33b7612a, + 0x4c7efab9, 0xa0ddee1b, 0xdf147588, 0x5f4ed93d, 0x208742ae, + 0x848a8616, 0xfb431d85, 0x7b19b130, 0x04d02aa3, 0x80e4d81c, + 0xff2d438f, 0x7f77ef3a, 0x00be74a9, 0xa4b3b011, 0xdb7a2b82, + 0x5b208737, 0x24e91ca4, 0xc84a0806, 0xb7839395, 0x37d93f20, + 0x4810a4b3, 0xec1d600b, 0x93d4fb98, 0x138e572d, 0x6c47ccbe, + 0x11b97828, 0x6e70e3bb, 0xee2a4f0e, 0x91e3d49d, 0x35ee1025, + 0x4a278bb6, 0xca7d2703, 0xb5b4bc90, 0x5917a832, 0x26de33a1, + 0xa6849f14, 0xd94d0487, 0x7d40c03f, 0x02895bac, 0x82d3f719, + 0xfd1a6c8a}, + {0x00000000, 0xa396284c, 0x9c5d56d9, 0x3fcb7e95, 0xe3cbabf3, + 0x405d83bf, 0x7f96fd2a, 0xdc00d566, 0x1ce651a7, 0xbf7079eb, + 0x80bb077e, 0x232d2f32, 0xff2dfa54, 0x5cbbd218, 0x6370ac8d, + 0xc0e684c1, 0x39cca34e, 0x9a5a8b02, 0xa591f597, 0x0607dddb, + 0xda0708bd, 0x799120f1, 0x465a5e64, 0xe5cc7628, 0x252af2e9, + 0x86bcdaa5, 0xb977a430, 0x1ae18c7c, 0xc6e1591a, 0x65777156, + 0x5abc0fc3, 0xf92a278f, 0x7399469c, 0xd00f6ed0, 0xefc41045, + 0x4c523809, 0x9052ed6f, 0x33c4c523, 0x0c0fbbb6, 0xaf9993fa, + 0x6f7f173b, 0xcce93f77, 0xf32241e2, 0x50b469ae, 0x8cb4bcc8, + 0x2f229484, 0x10e9ea11, 0xb37fc25d, 0x4a55e5d2, 0xe9c3cd9e, + 0xd608b30b, 0x759e9b47, 0xa99e4e21, 0x0a08666d, 0x35c318f8, + 0x965530b4, 0x56b3b475, 0xf5259c39, 0xcaeee2ac, 0x6978cae0, + 0xb5781f86, 0x16ee37ca, 0x2925495f, 0x8ab36113, 0xe7328d38, + 0x44a4a574, 0x7b6fdbe1, 0xd8f9f3ad, 0x04f926cb, 0xa76f0e87, + 0x98a47012, 0x3b32585e, 0xfbd4dc9f, 0x5842f4d3, 0x67898a46, + 0xc41fa20a, 0x181f776c, 0xbb895f20, 0x844221b5, 0x27d409f9, + 0xdefe2e76, 0x7d68063a, 0x42a378af, 0xe13550e3, 0x3d358585, + 0x9ea3adc9, 0xa168d35c, 0x02fefb10, 0xc2187fd1, 0x618e579d, + 0x5e452908, 0xfdd30144, 0x21d3d422, 0x8245fc6e, 0xbd8e82fb, + 0x1e18aab7, 0x94abcba4, 0x373de3e8, 0x08f69d7d, 0xab60b531, + 0x77606057, 0xd4f6481b, 0xeb3d368e, 0x48ab1ec2, 0x884d9a03, + 0x2bdbb24f, 0x1410ccda, 0xb786e496, 0x6b8631f0, 0xc81019bc, + 0xf7db6729, 0x544d4f65, 0xad6768ea, 0x0ef140a6, 0x313a3e33, + 0x92ac167f, 0x4eacc319, 0xed3aeb55, 0xd2f195c0, 0x7167bd8c, + 0xb181394d, 0x12171101, 0x2ddc6f94, 0x8e4a47d8, 0x524a92be, + 0xf1dcbaf2, 0xce17c467, 0x6d81ec2b, 0x15141c31, 0xb682347d, + 0x89494ae8, 0x2adf62a4, 0xf6dfb7c2, 0x55499f8e, 0x6a82e11b, + 0xc914c957, 0x09f24d96, 0xaa6465da, 0x95af1b4f, 0x36393303, + 0xea39e665, 0x49afce29, 0x7664b0bc, 0xd5f298f0, 0x2cd8bf7f, + 0x8f4e9733, 0xb085e9a6, 0x1313c1ea, 0xcf13148c, 0x6c853cc0, + 0x534e4255, 0xf0d86a19, 0x303eeed8, 0x93a8c694, 0xac63b801, + 0x0ff5904d, 0xd3f5452b, 0x70636d67, 0x4fa813f2, 0xec3e3bbe, + 0x668d5aad, 0xc51b72e1, 0xfad00c74, 0x59462438, 0x8546f15e, + 0x26d0d912, 0x191ba787, 0xba8d8fcb, 0x7a6b0b0a, 0xd9fd2346, + 0xe6365dd3, 0x45a0759f, 0x99a0a0f9, 0x3a3688b5, 0x05fdf620, + 0xa66bde6c, 0x5f41f9e3, 0xfcd7d1af, 0xc31caf3a, 0x608a8776, + 0xbc8a5210, 0x1f1c7a5c, 0x20d704c9, 0x83412c85, 0x43a7a844, + 0xe0318008, 0xdffafe9d, 0x7c6cd6d1, 0xa06c03b7, 0x03fa2bfb, + 0x3c31556e, 0x9fa77d22, 0xf2269109, 0x51b0b945, 0x6e7bc7d0, + 0xcdedef9c, 0x11ed3afa, 0xb27b12b6, 0x8db06c23, 0x2e26446f, + 0xeec0c0ae, 0x4d56e8e2, 0x729d9677, 0xd10bbe3b, 0x0d0b6b5d, + 0xae9d4311, 0x91563d84, 0x32c015c8, 0xcbea3247, 0x687c1a0b, + 0x57b7649e, 0xf4214cd2, 0x282199b4, 0x8bb7b1f8, 0xb47ccf6d, + 0x17eae721, 0xd70c63e0, 0x749a4bac, 0x4b513539, 0xe8c71d75, + 0x34c7c813, 0x9751e05f, 0xa89a9eca, 0x0b0cb686, 0x81bfd795, + 0x2229ffd9, 0x1de2814c, 0xbe74a900, 0x62747c66, 0xc1e2542a, + 0xfe292abf, 0x5dbf02f3, 0x9d598632, 0x3ecfae7e, 0x0104d0eb, + 0xa292f8a7, 0x7e922dc1, 0xdd04058d, 0xe2cf7b18, 0x41595354, + 0xb87374db, 0x1be55c97, 0x242e2202, 0x87b80a4e, 0x5bb8df28, + 0xf82ef764, 0xc7e589f1, 0x6473a1bd, 0xa495257c, 0x07030d30, + 0x38c873a5, 0x9b5e5be9, 0x475e8e8f, 0xe4c8a6c3, 0xdb03d856, + 0x7895f01a}, + {0x00000000, 0x2a283862, 0x545070c4, 0x7e7848a6, 0xa8a0e188, + 0x8288d9ea, 0xfcf0914c, 0xd6d8a92e, 0x8a30c551, 0xa018fd33, + 0xde60b595, 0xf4488df7, 0x229024d9, 0x08b81cbb, 0x76c0541d, + 0x5ce86c7f, 0xcf108ce3, 0xe538b481, 0x9b40fc27, 0xb168c445, + 0x67b06d6b, 0x4d985509, 0x33e01daf, 0x19c825cd, 0x452049b2, + 0x6f0871d0, 0x11703976, 0x3b580114, 0xed80a83a, 0xc7a89058, + 0xb9d0d8fe, 0x93f8e09c, 0x45501f87, 0x6f7827e5, 0x11006f43, + 0x3b285721, 0xedf0fe0f, 0xc7d8c66d, 0xb9a08ecb, 0x9388b6a9, + 0xcf60dad6, 0xe548e2b4, 0x9b30aa12, 0xb1189270, 0x67c03b5e, + 0x4de8033c, 0x33904b9a, 0x19b873f8, 0x8a409364, 0xa068ab06, + 0xde10e3a0, 0xf438dbc2, 0x22e072ec, 0x08c84a8e, 0x76b00228, + 0x5c983a4a, 0x00705635, 0x2a586e57, 0x542026f1, 0x7e081e93, + 0xa8d0b7bd, 0x82f88fdf, 0xfc80c779, 0xd6a8ff1b, 0x8aa03f0e, + 0xa088076c, 0xdef04fca, 0xf4d877a8, 0x2200de86, 0x0828e6e4, + 0x7650ae42, 0x5c789620, 0x0090fa5f, 0x2ab8c23d, 0x54c08a9b, + 0x7ee8b2f9, 0xa8301bd7, 0x821823b5, 0xfc606b13, 0xd6485371, + 0x45b0b3ed, 0x6f988b8f, 0x11e0c329, 0x3bc8fb4b, 0xed105265, + 0xc7386a07, 0xb94022a1, 0x93681ac3, 0xcf8076bc, 0xe5a84ede, + 0x9bd00678, 0xb1f83e1a, 0x67209734, 0x4d08af56, 0x3370e7f0, + 0x1958df92, 0xcff02089, 0xe5d818eb, 0x9ba0504d, 0xb188682f, + 0x6750c101, 0x4d78f963, 0x3300b1c5, 0x192889a7, 0x45c0e5d8, + 0x6fe8ddba, 0x1190951c, 0x3bb8ad7e, 0xed600450, 0xc7483c32, + 0xb9307494, 0x93184cf6, 0x00e0ac6a, 0x2ac89408, 0x54b0dcae, + 0x7e98e4cc, 0xa8404de2, 0x82687580, 0xfc103d26, 0xd6380544, + 0x8ad0693b, 0xa0f85159, 0xde8019ff, 0xf4a8219d, 0x227088b3, + 0x0858b0d1, 0x7620f877, 0x5c08c015, 0xce31785d, 0xe419403f, + 0x9a610899, 0xb04930fb, 0x669199d5, 0x4cb9a1b7, 0x32c1e911, + 0x18e9d173, 0x4401bd0c, 0x6e29856e, 0x1051cdc8, 0x3a79f5aa, + 0xeca15c84, 0xc68964e6, 0xb8f12c40, 0x92d91422, 0x0121f4be, + 0x2b09ccdc, 0x5571847a, 0x7f59bc18, 0xa9811536, 0x83a92d54, + 0xfdd165f2, 0xd7f95d90, 0x8b1131ef, 0xa139098d, 0xdf41412b, + 0xf5697949, 0x23b1d067, 0x0999e805, 0x77e1a0a3, 0x5dc998c1, + 0x8b6167da, 0xa1495fb8, 0xdf31171e, 0xf5192f7c, 0x23c18652, + 0x09e9be30, 0x7791f696, 0x5db9cef4, 0x0151a28b, 0x2b799ae9, + 0x5501d24f, 0x7f29ea2d, 0xa9f14303, 0x83d97b61, 0xfda133c7, + 0xd7890ba5, 0x4471eb39, 0x6e59d35b, 0x10219bfd, 0x3a09a39f, + 0xecd10ab1, 0xc6f932d3, 0xb8817a75, 0x92a94217, 0xce412e68, + 0xe469160a, 0x9a115eac, 0xb03966ce, 0x66e1cfe0, 0x4cc9f782, + 0x32b1bf24, 0x18998746, 0x44914753, 0x6eb97f31, 0x10c13797, + 0x3ae90ff5, 0xec31a6db, 0xc6199eb9, 0xb861d61f, 0x9249ee7d, + 0xcea18202, 0xe489ba60, 0x9af1f2c6, 0xb0d9caa4, 0x6601638a, + 0x4c295be8, 0x3251134e, 0x18792b2c, 0x8b81cbb0, 0xa1a9f3d2, + 0xdfd1bb74, 0xf5f98316, 0x23212a38, 0x0909125a, 0x77715afc, + 0x5d59629e, 0x01b10ee1, 0x2b993683, 0x55e17e25, 0x7fc94647, + 0xa911ef69, 0x8339d70b, 0xfd419fad, 0xd769a7cf, 0x01c158d4, + 0x2be960b6, 0x55912810, 0x7fb91072, 0xa961b95c, 0x8349813e, + 0xfd31c998, 0xd719f1fa, 0x8bf19d85, 0xa1d9a5e7, 0xdfa1ed41, + 0xf589d523, 0x23517c0d, 0x0979446f, 0x77010cc9, 0x5d2934ab, + 0xced1d437, 0xe4f9ec55, 0x9a81a4f3, 0xb0a99c91, 0x667135bf, + 0x4c590ddd, 0x3221457b, 0x18097d19, 0x44e11166, 0x6ec92904, + 0x10b161a2, 0x3a9959c0, 0xec41f0ee, 0xc669c88c, 0xb811802a, + 0x9239b848}, + {0x00000000, 0x4713f6fb, 0x8e27edf6, 0xc9341b0d, 0xc73eddad, + 0x802d2b56, 0x4919305b, 0x0e0ac6a0, 0x550cbd1b, 0x121f4be0, + 0xdb2b50ed, 0x9c38a616, 0x923260b6, 0xd521964d, 0x1c158d40, + 0x5b067bbb, 0xaa197a36, 0xed0a8ccd, 0x243e97c0, 0x632d613b, + 0x6d27a79b, 0x2a345160, 0xe3004a6d, 0xa413bc96, 0xff15c72d, + 0xb80631d6, 0x71322adb, 0x3621dc20, 0x382b1a80, 0x7f38ec7b, + 0xb60cf776, 0xf11f018d, 0x8f43f22d, 0xc85004d6, 0x01641fdb, + 0x4677e920, 0x487d2f80, 0x0f6ed97b, 0xc65ac276, 0x8149348d, + 0xda4f4f36, 0x9d5cb9cd, 0x5468a2c0, 0x137b543b, 0x1d71929b, + 0x5a626460, 0x93567f6d, 0xd4458996, 0x255a881b, 0x62497ee0, + 0xab7d65ed, 0xec6e9316, 0xe26455b6, 0xa577a34d, 0x6c43b840, + 0x2b504ebb, 0x70563500, 0x3745c3fb, 0xfe71d8f6, 0xb9622e0d, + 0xb768e8ad, 0xf07b1e56, 0x394f055b, 0x7e5cf3a0, 0xc5f6e21b, + 0x82e514e0, 0x4bd10fed, 0x0cc2f916, 0x02c83fb6, 0x45dbc94d, + 0x8cefd240, 0xcbfc24bb, 0x90fa5f00, 0xd7e9a9fb, 0x1eddb2f6, + 0x59ce440d, 0x57c482ad, 0x10d77456, 0xd9e36f5b, 0x9ef099a0, + 0x6fef982d, 0x28fc6ed6, 0xe1c875db, 0xa6db8320, 0xa8d14580, + 0xefc2b37b, 0x26f6a876, 0x61e55e8d, 0x3ae32536, 0x7df0d3cd, + 0xb4c4c8c0, 0xf3d73e3b, 0xfdddf89b, 0xbace0e60, 0x73fa156d, + 0x34e9e396, 0x4ab51036, 0x0da6e6cd, 0xc492fdc0, 0x83810b3b, + 0x8d8bcd9b, 0xca983b60, 0x03ac206d, 0x44bfd696, 0x1fb9ad2d, + 0x58aa5bd6, 0x919e40db, 0xd68db620, 0xd8877080, 0x9f94867b, + 0x56a09d76, 0x11b36b8d, 0xe0ac6a00, 0xa7bf9cfb, 0x6e8b87f6, + 0x2998710d, 0x2792b7ad, 0x60814156, 0xa9b55a5b, 0xeea6aca0, + 0xb5a0d71b, 0xf2b321e0, 0x3b873aed, 0x7c94cc16, 0x729e0ab6, + 0x358dfc4d, 0xfcb9e740, 0xbbaa11bb, 0x509cc277, 0x178f348c, + 0xdebb2f81, 0x99a8d97a, 0x97a21fda, 0xd0b1e921, 0x1985f22c, + 0x5e9604d7, 0x05907f6c, 0x42838997, 0x8bb7929a, 0xcca46461, + 0xc2aea2c1, 0x85bd543a, 0x4c894f37, 0x0b9ab9cc, 0xfa85b841, + 0xbd964eba, 0x74a255b7, 0x33b1a34c, 0x3dbb65ec, 0x7aa89317, + 0xb39c881a, 0xf48f7ee1, 0xaf89055a, 0xe89af3a1, 0x21aee8ac, + 0x66bd1e57, 0x68b7d8f7, 0x2fa42e0c, 0xe6903501, 0xa183c3fa, + 0xdfdf305a, 0x98ccc6a1, 0x51f8ddac, 0x16eb2b57, 0x18e1edf7, + 0x5ff21b0c, 0x96c60001, 0xd1d5f6fa, 0x8ad38d41, 0xcdc07bba, + 0x04f460b7, 0x43e7964c, 0x4ded50ec, 0x0afea617, 0xc3cabd1a, + 0x84d94be1, 0x75c64a6c, 0x32d5bc97, 0xfbe1a79a, 0xbcf25161, + 0xb2f897c1, 0xf5eb613a, 0x3cdf7a37, 0x7bcc8ccc, 0x20caf777, + 0x67d9018c, 0xaeed1a81, 0xe9feec7a, 0xe7f42ada, 0xa0e7dc21, + 0x69d3c72c, 0x2ec031d7, 0x956a206c, 0xd279d697, 0x1b4dcd9a, + 0x5c5e3b61, 0x5254fdc1, 0x15470b3a, 0xdc731037, 0x9b60e6cc, + 0xc0669d77, 0x87756b8c, 0x4e417081, 0x0952867a, 0x075840da, + 0x404bb621, 0x897fad2c, 0xce6c5bd7, 0x3f735a5a, 0x7860aca1, + 0xb154b7ac, 0xf6474157, 0xf84d87f7, 0xbf5e710c, 0x766a6a01, + 0x31799cfa, 0x6a7fe741, 0x2d6c11ba, 0xe4580ab7, 0xa34bfc4c, + 0xad413aec, 0xea52cc17, 0x2366d71a, 0x647521e1, 0x1a29d241, + 0x5d3a24ba, 0x940e3fb7, 0xd31dc94c, 0xdd170fec, 0x9a04f917, + 0x5330e21a, 0x142314e1, 0x4f256f5a, 0x083699a1, 0xc10282ac, + 0x86117457, 0x881bb2f7, 0xcf08440c, 0x063c5f01, 0x412fa9fa, + 0xb030a877, 0xf7235e8c, 0x3e174581, 0x7904b37a, 0x770e75da, + 0x301d8321, 0xf929982c, 0xbe3a6ed7, 0xe53c156c, 0xa22fe397, + 0x6b1bf89a, 0x2c080e61, 0x2202c8c1, 0x65113e3a, 0xac252537, + 0xeb36d3cc}, + {0x00000000, 0xa13984ee, 0x99020f9d, 0x383b8b73, 0xe975197b, + 0x484c9d95, 0x707716e6, 0xd14e9208, 0x099b34b7, 0xa8a2b059, + 0x90993b2a, 0x31a0bfc4, 0xe0ee2dcc, 0x41d7a922, 0x79ec2251, + 0xd8d5a6bf, 0x1336696e, 0xb20fed80, 0x8a3466f3, 0x2b0de21d, + 0xfa437015, 0x5b7af4fb, 0x63417f88, 0xc278fb66, 0x1aad5dd9, + 0xbb94d937, 0x83af5244, 0x2296d6aa, 0xf3d844a2, 0x52e1c04c, + 0x6ada4b3f, 0xcbe3cfd1, 0x266cd2dc, 0x87555632, 0xbf6edd41, + 0x1e5759af, 0xcf19cba7, 0x6e204f49, 0x561bc43a, 0xf72240d4, + 0x2ff7e66b, 0x8ece6285, 0xb6f5e9f6, 0x17cc6d18, 0xc682ff10, + 0x67bb7bfe, 0x5f80f08d, 0xfeb97463, 0x355abbb2, 0x94633f5c, + 0xac58b42f, 0x0d6130c1, 0xdc2fa2c9, 0x7d162627, 0x452dad54, + 0xe41429ba, 0x3cc18f05, 0x9df80beb, 0xa5c38098, 0x04fa0476, + 0xd5b4967e, 0x748d1290, 0x4cb699e3, 0xed8f1d0d, 0x4cd9a5b8, + 0xede02156, 0xd5dbaa25, 0x74e22ecb, 0xa5acbcc3, 0x0495382d, + 0x3caeb35e, 0x9d9737b0, 0x4542910f, 0xe47b15e1, 0xdc409e92, + 0x7d791a7c, 0xac378874, 0x0d0e0c9a, 0x353587e9, 0x940c0307, + 0x5fefccd6, 0xfed64838, 0xc6edc34b, 0x67d447a5, 0xb69ad5ad, + 0x17a35143, 0x2f98da30, 0x8ea15ede, 0x5674f861, 0xf74d7c8f, + 0xcf76f7fc, 0x6e4f7312, 0xbf01e11a, 0x1e3865f4, 0x2603ee87, + 0x873a6a69, 0x6ab57764, 0xcb8cf38a, 0xf3b778f9, 0x528efc17, + 0x83c06e1f, 0x22f9eaf1, 0x1ac26182, 0xbbfbe56c, 0x632e43d3, + 0xc217c73d, 0xfa2c4c4e, 0x5b15c8a0, 0x8a5b5aa8, 0x2b62de46, + 0x13595535, 0xb260d1db, 0x79831e0a, 0xd8ba9ae4, 0xe0811197, + 0x41b89579, 0x90f60771, 0x31cf839f, 0x09f408ec, 0xa8cd8c02, + 0x70182abd, 0xd121ae53, 0xe91a2520, 0x4823a1ce, 0x996d33c6, + 0x3854b728, 0x006f3c5b, 0xa156b8b5, 0x99b34b70, 0x388acf9e, + 0x00b144ed, 0xa188c003, 0x70c6520b, 0xd1ffd6e5, 0xe9c45d96, + 0x48fdd978, 0x90287fc7, 0x3111fb29, 0x092a705a, 0xa813f4b4, + 0x795d66bc, 0xd864e252, 0xe05f6921, 0x4166edcf, 0x8a85221e, + 0x2bbca6f0, 0x13872d83, 0xb2bea96d, 0x63f03b65, 0xc2c9bf8b, + 0xfaf234f8, 0x5bcbb016, 0x831e16a9, 0x22279247, 0x1a1c1934, + 0xbb259dda, 0x6a6b0fd2, 0xcb528b3c, 0xf369004f, 0x525084a1, + 0xbfdf99ac, 0x1ee61d42, 0x26dd9631, 0x87e412df, 0x56aa80d7, + 0xf7930439, 0xcfa88f4a, 0x6e910ba4, 0xb644ad1b, 0x177d29f5, + 0x2f46a286, 0x8e7f2668, 0x5f31b460, 0xfe08308e, 0xc633bbfd, + 0x670a3f13, 0xace9f0c2, 0x0dd0742c, 0x35ebff5f, 0x94d27bb1, + 0x459ce9b9, 0xe4a56d57, 0xdc9ee624, 0x7da762ca, 0xa572c475, + 0x044b409b, 0x3c70cbe8, 0x9d494f06, 0x4c07dd0e, 0xed3e59e0, + 0xd505d293, 0x743c567d, 0xd56aeec8, 0x74536a26, 0x4c68e155, + 0xed5165bb, 0x3c1ff7b3, 0x9d26735d, 0xa51df82e, 0x04247cc0, + 0xdcf1da7f, 0x7dc85e91, 0x45f3d5e2, 0xe4ca510c, 0x3584c304, + 0x94bd47ea, 0xac86cc99, 0x0dbf4877, 0xc65c87a6, 0x67650348, + 0x5f5e883b, 0xfe670cd5, 0x2f299edd, 0x8e101a33, 0xb62b9140, + 0x171215ae, 0xcfc7b311, 0x6efe37ff, 0x56c5bc8c, 0xf7fc3862, + 0x26b2aa6a, 0x878b2e84, 0xbfb0a5f7, 0x1e892119, 0xf3063c14, + 0x523fb8fa, 0x6a043389, 0xcb3db767, 0x1a73256f, 0xbb4aa181, + 0x83712af2, 0x2248ae1c, 0xfa9d08a3, 0x5ba48c4d, 0x639f073e, + 0xc2a683d0, 0x13e811d8, 0xb2d19536, 0x8aea1e45, 0x2bd39aab, + 0xe030557a, 0x4109d194, 0x79325ae7, 0xd80bde09, 0x09454c01, + 0xa87cc8ef, 0x9047439c, 0x317ec772, 0xe9ab61cd, 0x4892e523, + 0x70a96e50, 0xd190eabe, 0x00de78b6, 0xa1e7fc58, 0x99dc772b, + 0x38e5f3c5}, + {0x00000000, 0xe81790a1, 0x0b5e2703, 0xe349b7a2, 0x16bc4e06, + 0xfeabdea7, 0x1de26905, 0xf5f5f9a4, 0x2d789c0c, 0xc56f0cad, + 0x2626bb0f, 0xce312bae, 0x3bc4d20a, 0xd3d342ab, 0x309af509, + 0xd88d65a8, 0x5af13818, 0xb2e6a8b9, 0x51af1f1b, 0xb9b88fba, + 0x4c4d761e, 0xa45ae6bf, 0x4713511d, 0xaf04c1bc, 0x7789a414, + 0x9f9e34b5, 0x7cd78317, 0x94c013b6, 0x6135ea12, 0x89227ab3, + 0x6a6bcd11, 0x827c5db0, 0xb5e27030, 0x5df5e091, 0xbebc5733, + 0x56abc792, 0xa35e3e36, 0x4b49ae97, 0xa8001935, 0x40178994, + 0x989aec3c, 0x708d7c9d, 0x93c4cb3f, 0x7bd35b9e, 0x8e26a23a, + 0x6631329b, 0x85788539, 0x6d6f1598, 0xef134828, 0x0704d889, + 0xe44d6f2b, 0x0c5aff8a, 0xf9af062e, 0x11b8968f, 0xf2f1212d, + 0x1ae6b18c, 0xc26bd424, 0x2a7c4485, 0xc935f327, 0x21226386, + 0xd4d79a22, 0x3cc00a83, 0xdf89bd21, 0x379e2d80, 0xb0b5e621, + 0x58a27680, 0xbbebc122, 0x53fc5183, 0xa609a827, 0x4e1e3886, + 0xad578f24, 0x45401f85, 0x9dcd7a2d, 0x75daea8c, 0x96935d2e, + 0x7e84cd8f, 0x8b71342b, 0x6366a48a, 0x802f1328, 0x68388389, + 0xea44de39, 0x02534e98, 0xe11af93a, 0x090d699b, 0xfcf8903f, + 0x14ef009e, 0xf7a6b73c, 0x1fb1279d, 0xc73c4235, 0x2f2bd294, + 0xcc626536, 0x2475f597, 0xd1800c33, 0x39979c92, 0xdade2b30, + 0x32c9bb91, 0x05579611, 0xed4006b0, 0x0e09b112, 0xe61e21b3, + 0x13ebd817, 0xfbfc48b6, 0x18b5ff14, 0xf0a26fb5, 0x282f0a1d, + 0xc0389abc, 0x23712d1e, 0xcb66bdbf, 0x3e93441b, 0xd684d4ba, + 0x35cd6318, 0xdddaf3b9, 0x5fa6ae09, 0xb7b13ea8, 0x54f8890a, + 0xbcef19ab, 0x491ae00f, 0xa10d70ae, 0x4244c70c, 0xaa5357ad, + 0x72de3205, 0x9ac9a2a4, 0x79801506, 0x919785a7, 0x64627c03, + 0x8c75eca2, 0x6f3c5b00, 0x872bcba1, 0xba1aca03, 0x520d5aa2, + 0xb144ed00, 0x59537da1, 0xaca68405, 0x44b114a4, 0xa7f8a306, + 0x4fef33a7, 0x9762560f, 0x7f75c6ae, 0x9c3c710c, 0x742be1ad, + 0x81de1809, 0x69c988a8, 0x8a803f0a, 0x6297afab, 0xe0ebf21b, + 0x08fc62ba, 0xebb5d518, 0x03a245b9, 0xf657bc1d, 0x1e402cbc, + 0xfd099b1e, 0x151e0bbf, 0xcd936e17, 0x2584feb6, 0xc6cd4914, + 0x2edad9b5, 0xdb2f2011, 0x3338b0b0, 0xd0710712, 0x386697b3, + 0x0ff8ba33, 0xe7ef2a92, 0x04a69d30, 0xecb10d91, 0x1944f435, + 0xf1536494, 0x121ad336, 0xfa0d4397, 0x2280263f, 0xca97b69e, + 0x29de013c, 0xc1c9919d, 0x343c6839, 0xdc2bf898, 0x3f624f3a, + 0xd775df9b, 0x5509822b, 0xbd1e128a, 0x5e57a528, 0xb6403589, + 0x43b5cc2d, 0xaba25c8c, 0x48ebeb2e, 0xa0fc7b8f, 0x78711e27, + 0x90668e86, 0x732f3924, 0x9b38a985, 0x6ecd5021, 0x86dac080, + 0x65937722, 0x8d84e783, 0x0aaf2c22, 0xe2b8bc83, 0x01f10b21, + 0xe9e69b80, 0x1c136224, 0xf404f285, 0x174d4527, 0xff5ad586, + 0x27d7b02e, 0xcfc0208f, 0x2c89972d, 0xc49e078c, 0x316bfe28, + 0xd97c6e89, 0x3a35d92b, 0xd222498a, 0x505e143a, 0xb849849b, + 0x5b003339, 0xb317a398, 0x46e25a3c, 0xaef5ca9d, 0x4dbc7d3f, + 0xa5abed9e, 0x7d268836, 0x95311897, 0x7678af35, 0x9e6f3f94, + 0x6b9ac630, 0x838d5691, 0x60c4e133, 0x88d37192, 0xbf4d5c12, + 0x575accb3, 0xb4137b11, 0x5c04ebb0, 0xa9f11214, 0x41e682b5, + 0xa2af3517, 0x4ab8a5b6, 0x9235c01e, 0x7a2250bf, 0x996be71d, + 0x717c77bc, 0x84898e18, 0x6c9e1eb9, 0x8fd7a91b, 0x67c039ba, + 0xe5bc640a, 0x0dabf4ab, 0xeee24309, 0x06f5d3a8, 0xf3002a0c, + 0x1b17baad, 0xf85e0d0f, 0x10499dae, 0xc8c4f806, 0x20d368a7, + 0xc39adf05, 0x2b8d4fa4, 0xde78b600, 0x366f26a1, 0xd5269103, + 0x3d3101a2}}; + +local const z_word_t FAR crc_braid_big_table[][256] = { + {0x0000000000000000, 0xa19017e800000000, 0x03275e0b00000000, + 0xa2b749e300000000, 0x064ebc1600000000, 0xa7deabfe00000000, + 0x0569e21d00000000, 0xa4f9f5f500000000, 0x0c9c782d00000000, + 0xad0c6fc500000000, 0x0fbb262600000000, 0xae2b31ce00000000, + 0x0ad2c43b00000000, 0xab42d3d300000000, 0x09f59a3000000000, + 0xa8658dd800000000, 0x1838f15a00000000, 0xb9a8e6b200000000, + 0x1b1faf5100000000, 0xba8fb8b900000000, 0x1e764d4c00000000, + 0xbfe65aa400000000, 0x1d51134700000000, 0xbcc104af00000000, + 0x14a4897700000000, 0xb5349e9f00000000, 0x1783d77c00000000, + 0xb613c09400000000, 0x12ea356100000000, 0xb37a228900000000, + 0x11cd6b6a00000000, 0xb05d7c8200000000, 0x3070e2b500000000, + 0x91e0f55d00000000, 0x3357bcbe00000000, 0x92c7ab5600000000, + 0x363e5ea300000000, 0x97ae494b00000000, 0x351900a800000000, + 0x9489174000000000, 0x3cec9a9800000000, 0x9d7c8d7000000000, + 0x3fcbc49300000000, 0x9e5bd37b00000000, 0x3aa2268e00000000, + 0x9b32316600000000, 0x3985788500000000, 0x98156f6d00000000, + 0x284813ef00000000, 0x89d8040700000000, 0x2b6f4de400000000, + 0x8aff5a0c00000000, 0x2e06aff900000000, 0x8f96b81100000000, + 0x2d21f1f200000000, 0x8cb1e61a00000000, 0x24d46bc200000000, + 0x85447c2a00000000, 0x27f335c900000000, 0x8663222100000000, + 0x229ad7d400000000, 0x830ac03c00000000, 0x21bd89df00000000, + 0x802d9e3700000000, 0x21e6b5b000000000, 0x8076a25800000000, + 0x22c1ebbb00000000, 0x8351fc5300000000, 0x27a809a600000000, + 0x86381e4e00000000, 0x248f57ad00000000, 0x851f404500000000, + 0x2d7acd9d00000000, 0x8ceada7500000000, 0x2e5d939600000000, + 0x8fcd847e00000000, 0x2b34718b00000000, 0x8aa4666300000000, + 0x28132f8000000000, 0x8983386800000000, 0x39de44ea00000000, + 0x984e530200000000, 0x3af91ae100000000, 0x9b690d0900000000, + 0x3f90f8fc00000000, 0x9e00ef1400000000, 0x3cb7a6f700000000, + 0x9d27b11f00000000, 0x35423cc700000000, 0x94d22b2f00000000, + 0x366562cc00000000, 0x97f5752400000000, 0x330c80d100000000, + 0x929c973900000000, 0x302bdeda00000000, 0x91bbc93200000000, + 0x1196570500000000, 0xb00640ed00000000, 0x12b1090e00000000, + 0xb3211ee600000000, 0x17d8eb1300000000, 0xb648fcfb00000000, + 0x14ffb51800000000, 0xb56fa2f000000000, 0x1d0a2f2800000000, + 0xbc9a38c000000000, 0x1e2d712300000000, 0xbfbd66cb00000000, + 0x1b44933e00000000, 0xbad484d600000000, 0x1863cd3500000000, + 0xb9f3dadd00000000, 0x09aea65f00000000, 0xa83eb1b700000000, + 0x0a89f85400000000, 0xab19efbc00000000, 0x0fe01a4900000000, + 0xae700da100000000, 0x0cc7444200000000, 0xad5753aa00000000, + 0x0532de7200000000, 0xa4a2c99a00000000, 0x0615807900000000, + 0xa785979100000000, 0x037c626400000000, 0xa2ec758c00000000, + 0x005b3c6f00000000, 0xa1cb2b8700000000, 0x03ca1aba00000000, + 0xa25a0d5200000000, 0x00ed44b100000000, 0xa17d535900000000, + 0x0584a6ac00000000, 0xa414b14400000000, 0x06a3f8a700000000, + 0xa733ef4f00000000, 0x0f56629700000000, 0xaec6757f00000000, + 0x0c713c9c00000000, 0xade12b7400000000, 0x0918de8100000000, + 0xa888c96900000000, 0x0a3f808a00000000, 0xabaf976200000000, + 0x1bf2ebe000000000, 0xba62fc0800000000, 0x18d5b5eb00000000, + 0xb945a20300000000, 0x1dbc57f600000000, 0xbc2c401e00000000, + 0x1e9b09fd00000000, 0xbf0b1e1500000000, 0x176e93cd00000000, + 0xb6fe842500000000, 0x1449cdc600000000, 0xb5d9da2e00000000, + 0x11202fdb00000000, 0xb0b0383300000000, 0x120771d000000000, + 0xb397663800000000, 0x33baf80f00000000, 0x922aefe700000000, + 0x309da60400000000, 0x910db1ec00000000, 0x35f4441900000000, + 0x946453f100000000, 0x36d31a1200000000, 0x97430dfa00000000, + 0x3f26802200000000, 0x9eb697ca00000000, 0x3c01de2900000000, + 0x9d91c9c100000000, 0x39683c3400000000, 0x98f82bdc00000000, + 0x3a4f623f00000000, 0x9bdf75d700000000, 0x2b82095500000000, + 0x8a121ebd00000000, 0x28a5575e00000000, 0x893540b600000000, + 0x2dccb54300000000, 0x8c5ca2ab00000000, 0x2eebeb4800000000, + 0x8f7bfca000000000, 0x271e717800000000, 0x868e669000000000, + 0x24392f7300000000, 0x85a9389b00000000, 0x2150cd6e00000000, + 0x80c0da8600000000, 0x2277936500000000, 0x83e7848d00000000, + 0x222caf0a00000000, 0x83bcb8e200000000, 0x210bf10100000000, + 0x809be6e900000000, 0x2462131c00000000, 0x85f204f400000000, + 0x27454d1700000000, 0x86d55aff00000000, 0x2eb0d72700000000, + 0x8f20c0cf00000000, 0x2d97892c00000000, 0x8c079ec400000000, + 0x28fe6b3100000000, 0x896e7cd900000000, 0x2bd9353a00000000, + 0x8a4922d200000000, 0x3a145e5000000000, 0x9b8449b800000000, + 0x3933005b00000000, 0x98a317b300000000, 0x3c5ae24600000000, + 0x9dcaf5ae00000000, 0x3f7dbc4d00000000, 0x9eedaba500000000, + 0x3688267d00000000, 0x9718319500000000, 0x35af787600000000, + 0x943f6f9e00000000, 0x30c69a6b00000000, 0x91568d8300000000, + 0x33e1c46000000000, 0x9271d38800000000, 0x125c4dbf00000000, + 0xb3cc5a5700000000, 0x117b13b400000000, 0xb0eb045c00000000, + 0x1412f1a900000000, 0xb582e64100000000, 0x1735afa200000000, + 0xb6a5b84a00000000, 0x1ec0359200000000, 0xbf50227a00000000, + 0x1de76b9900000000, 0xbc777c7100000000, 0x188e898400000000, + 0xb91e9e6c00000000, 0x1ba9d78f00000000, 0xba39c06700000000, + 0x0a64bce500000000, 0xabf4ab0d00000000, 0x0943e2ee00000000, + 0xa8d3f50600000000, 0x0c2a00f300000000, 0xadba171b00000000, + 0x0f0d5ef800000000, 0xae9d491000000000, 0x06f8c4c800000000, + 0xa768d32000000000, 0x05df9ac300000000, 0xa44f8d2b00000000, + 0x00b678de00000000, 0xa1266f3600000000, 0x039126d500000000, + 0xa201313d00000000}, + {0x0000000000000000, 0xee8439a100000000, 0x9d0f029900000000, + 0x738b3b3800000000, 0x7b1975e900000000, 0x959d4c4800000000, + 0xe616777000000000, 0x08924ed100000000, 0xb7349b0900000000, + 0x59b0a2a800000000, 0x2a3b999000000000, 0xc4bfa03100000000, + 0xcc2deee000000000, 0x22a9d74100000000, 0x5122ec7900000000, + 0xbfa6d5d800000000, 0x6e69361300000000, 0x80ed0fb200000000, + 0xf366348a00000000, 0x1de20d2b00000000, 0x157043fa00000000, + 0xfbf47a5b00000000, 0x887f416300000000, 0x66fb78c200000000, + 0xd95dad1a00000000, 0x37d994bb00000000, 0x4452af8300000000, + 0xaad6962200000000, 0xa244d8f300000000, 0x4cc0e15200000000, + 0x3f4bda6a00000000, 0xd1cfe3cb00000000, 0xdcd26c2600000000, + 0x3256558700000000, 0x41dd6ebf00000000, 0xaf59571e00000000, + 0xa7cb19cf00000000, 0x494f206e00000000, 0x3ac41b5600000000, + 0xd44022f700000000, 0x6be6f72f00000000, 0x8562ce8e00000000, + 0xf6e9f5b600000000, 0x186dcc1700000000, 0x10ff82c600000000, + 0xfe7bbb6700000000, 0x8df0805f00000000, 0x6374b9fe00000000, + 0xb2bb5a3500000000, 0x5c3f639400000000, 0x2fb458ac00000000, + 0xc130610d00000000, 0xc9a22fdc00000000, 0x2726167d00000000, + 0x54ad2d4500000000, 0xba2914e400000000, 0x058fc13c00000000, + 0xeb0bf89d00000000, 0x9880c3a500000000, 0x7604fa0400000000, + 0x7e96b4d500000000, 0x90128d7400000000, 0xe399b64c00000000, + 0x0d1d8fed00000000, 0xb8a5d94c00000000, 0x5621e0ed00000000, + 0x25aadbd500000000, 0xcb2ee27400000000, 0xc3bcaca500000000, + 0x2d38950400000000, 0x5eb3ae3c00000000, 0xb037979d00000000, + 0x0f91424500000000, 0xe1157be400000000, 0x929e40dc00000000, + 0x7c1a797d00000000, 0x748837ac00000000, 0x9a0c0e0d00000000, + 0xe987353500000000, 0x07030c9400000000, 0xd6ccef5f00000000, + 0x3848d6fe00000000, 0x4bc3edc600000000, 0xa547d46700000000, + 0xadd59ab600000000, 0x4351a31700000000, 0x30da982f00000000, + 0xde5ea18e00000000, 0x61f8745600000000, 0x8f7c4df700000000, + 0xfcf776cf00000000, 0x12734f6e00000000, 0x1ae101bf00000000, + 0xf465381e00000000, 0x87ee032600000000, 0x696a3a8700000000, + 0x6477b56a00000000, 0x8af38ccb00000000, 0xf978b7f300000000, + 0x17fc8e5200000000, 0x1f6ec08300000000, 0xf1eaf92200000000, + 0x8261c21a00000000, 0x6ce5fbbb00000000, 0xd3432e6300000000, + 0x3dc717c200000000, 0x4e4c2cfa00000000, 0xa0c8155b00000000, + 0xa85a5b8a00000000, 0x46de622b00000000, 0x3555591300000000, + 0xdbd160b200000000, 0x0a1e837900000000, 0xe49abad800000000, + 0x971181e000000000, 0x7995b84100000000, 0x7107f69000000000, + 0x9f83cf3100000000, 0xec08f40900000000, 0x028ccda800000000, + 0xbd2a187000000000, 0x53ae21d100000000, 0x20251ae900000000, + 0xcea1234800000000, 0xc6336d9900000000, 0x28b7543800000000, + 0x5b3c6f0000000000, 0xb5b856a100000000, 0x704bb39900000000, + 0x9ecf8a3800000000, 0xed44b10000000000, 0x03c088a100000000, + 0x0b52c67000000000, 0xe5d6ffd100000000, 0x965dc4e900000000, + 0x78d9fd4800000000, 0xc77f289000000000, 0x29fb113100000000, + 0x5a702a0900000000, 0xb4f413a800000000, 0xbc665d7900000000, + 0x52e264d800000000, 0x21695fe000000000, 0xcfed664100000000, + 0x1e22858a00000000, 0xf0a6bc2b00000000, 0x832d871300000000, + 0x6da9beb200000000, 0x653bf06300000000, 0x8bbfc9c200000000, + 0xf834f2fa00000000, 0x16b0cb5b00000000, 0xa9161e8300000000, + 0x4792272200000000, 0x34191c1a00000000, 0xda9d25bb00000000, + 0xd20f6b6a00000000, 0x3c8b52cb00000000, 0x4f0069f300000000, + 0xa184505200000000, 0xac99dfbf00000000, 0x421de61e00000000, + 0x3196dd2600000000, 0xdf12e48700000000, 0xd780aa5600000000, + 0x390493f700000000, 0x4a8fa8cf00000000, 0xa40b916e00000000, + 0x1bad44b600000000, 0xf5297d1700000000, 0x86a2462f00000000, + 0x68267f8e00000000, 0x60b4315f00000000, 0x8e3008fe00000000, + 0xfdbb33c600000000, 0x133f0a6700000000, 0xc2f0e9ac00000000, + 0x2c74d00d00000000, 0x5fffeb3500000000, 0xb17bd29400000000, + 0xb9e99c4500000000, 0x576da5e400000000, 0x24e69edc00000000, + 0xca62a77d00000000, 0x75c472a500000000, 0x9b404b0400000000, + 0xe8cb703c00000000, 0x064f499d00000000, 0x0edd074c00000000, + 0xe0593eed00000000, 0x93d205d500000000, 0x7d563c7400000000, + 0xc8ee6ad500000000, 0x266a537400000000, 0x55e1684c00000000, + 0xbb6551ed00000000, 0xb3f71f3c00000000, 0x5d73269d00000000, + 0x2ef81da500000000, 0xc07c240400000000, 0x7fdaf1dc00000000, + 0x915ec87d00000000, 0xe2d5f34500000000, 0x0c51cae400000000, + 0x04c3843500000000, 0xea47bd9400000000, 0x99cc86ac00000000, + 0x7748bf0d00000000, 0xa6875cc600000000, 0x4803656700000000, + 0x3b885e5f00000000, 0xd50c67fe00000000, 0xdd9e292f00000000, + 0x331a108e00000000, 0x40912bb600000000, 0xae15121700000000, + 0x11b3c7cf00000000, 0xff37fe6e00000000, 0x8cbcc55600000000, + 0x6238fcf700000000, 0x6aaab22600000000, 0x842e8b8700000000, + 0xf7a5b0bf00000000, 0x1921891e00000000, 0x143c06f300000000, + 0xfab83f5200000000, 0x8933046a00000000, 0x67b73dcb00000000, + 0x6f25731a00000000, 0x81a14abb00000000, 0xf22a718300000000, + 0x1cae482200000000, 0xa3089dfa00000000, 0x4d8ca45b00000000, + 0x3e079f6300000000, 0xd083a6c200000000, 0xd811e81300000000, + 0x3695d1b200000000, 0x451eea8a00000000, 0xab9ad32b00000000, + 0x7a5530e000000000, 0x94d1094100000000, 0xe75a327900000000, + 0x09de0bd800000000, 0x014c450900000000, 0xefc87ca800000000, + 0x9c43479000000000, 0x72c77e3100000000, 0xcd61abe900000000, + 0x23e5924800000000, 0x506ea97000000000, 0xbeea90d100000000, + 0xb678de0000000000, 0x58fce7a100000000, 0x2b77dc9900000000, + 0xc5f3e53800000000}, + {0x0000000000000000, 0xfbf6134700000000, 0xf6ed278e00000000, + 0x0d1b34c900000000, 0xaddd3ec700000000, 0x562b2d8000000000, + 0x5b30194900000000, 0xa0c60a0e00000000, 0x1bbd0c5500000000, + 0xe04b1f1200000000, 0xed502bdb00000000, 0x16a6389c00000000, + 0xb660329200000000, 0x4d9621d500000000, 0x408d151c00000000, + 0xbb7b065b00000000, 0x367a19aa00000000, 0xcd8c0aed00000000, + 0xc0973e2400000000, 0x3b612d6300000000, 0x9ba7276d00000000, + 0x6051342a00000000, 0x6d4a00e300000000, 0x96bc13a400000000, + 0x2dc715ff00000000, 0xd63106b800000000, 0xdb2a327100000000, + 0x20dc213600000000, 0x801a2b3800000000, 0x7bec387f00000000, + 0x76f70cb600000000, 0x8d011ff100000000, 0x2df2438f00000000, + 0xd60450c800000000, 0xdb1f640100000000, 0x20e9774600000000, + 0x802f7d4800000000, 0x7bd96e0f00000000, 0x76c25ac600000000, + 0x8d34498100000000, 0x364f4fda00000000, 0xcdb95c9d00000000, + 0xc0a2685400000000, 0x3b547b1300000000, 0x9b92711d00000000, + 0x6064625a00000000, 0x6d7f569300000000, 0x968945d400000000, + 0x1b885a2500000000, 0xe07e496200000000, 0xed657dab00000000, + 0x16936eec00000000, 0xb65564e200000000, 0x4da377a500000000, + 0x40b8436c00000000, 0xbb4e502b00000000, 0x0035567000000000, + 0xfbc3453700000000, 0xf6d871fe00000000, 0x0d2e62b900000000, + 0xade868b700000000, 0x561e7bf000000000, 0x5b054f3900000000, + 0xa0f35c7e00000000, 0x1be2f6c500000000, 0xe014e58200000000, + 0xed0fd14b00000000, 0x16f9c20c00000000, 0xb63fc80200000000, + 0x4dc9db4500000000, 0x40d2ef8c00000000, 0xbb24fccb00000000, + 0x005ffa9000000000, 0xfba9e9d700000000, 0xf6b2dd1e00000000, + 0x0d44ce5900000000, 0xad82c45700000000, 0x5674d71000000000, + 0x5b6fe3d900000000, 0xa099f09e00000000, 0x2d98ef6f00000000, + 0xd66efc2800000000, 0xdb75c8e100000000, 0x2083dba600000000, + 0x8045d1a800000000, 0x7bb3c2ef00000000, 0x76a8f62600000000, + 0x8d5ee56100000000, 0x3625e33a00000000, 0xcdd3f07d00000000, + 0xc0c8c4b400000000, 0x3b3ed7f300000000, 0x9bf8ddfd00000000, + 0x600eceba00000000, 0x6d15fa7300000000, 0x96e3e93400000000, + 0x3610b54a00000000, 0xcde6a60d00000000, 0xc0fd92c400000000, + 0x3b0b818300000000, 0x9bcd8b8d00000000, 0x603b98ca00000000, + 0x6d20ac0300000000, 0x96d6bf4400000000, 0x2dadb91f00000000, + 0xd65baa5800000000, 0xdb409e9100000000, 0x20b68dd600000000, + 0x807087d800000000, 0x7b86949f00000000, 0x769da05600000000, + 0x8d6bb31100000000, 0x006aace000000000, 0xfb9cbfa700000000, + 0xf6878b6e00000000, 0x0d71982900000000, 0xadb7922700000000, + 0x5641816000000000, 0x5b5ab5a900000000, 0xa0aca6ee00000000, + 0x1bd7a0b500000000, 0xe021b3f200000000, 0xed3a873b00000000, + 0x16cc947c00000000, 0xb60a9e7200000000, 0x4dfc8d3500000000, + 0x40e7b9fc00000000, 0xbb11aabb00000000, 0x77c29c5000000000, + 0x8c348f1700000000, 0x812fbbde00000000, 0x7ad9a89900000000, + 0xda1fa29700000000, 0x21e9b1d000000000, 0x2cf2851900000000, + 0xd704965e00000000, 0x6c7f900500000000, 0x9789834200000000, + 0x9a92b78b00000000, 0x6164a4cc00000000, 0xc1a2aec200000000, + 0x3a54bd8500000000, 0x374f894c00000000, 0xccb99a0b00000000, + 0x41b885fa00000000, 0xba4e96bd00000000, 0xb755a27400000000, + 0x4ca3b13300000000, 0xec65bb3d00000000, 0x1793a87a00000000, + 0x1a889cb300000000, 0xe17e8ff400000000, 0x5a0589af00000000, + 0xa1f39ae800000000, 0xace8ae2100000000, 0x571ebd6600000000, + 0xf7d8b76800000000, 0x0c2ea42f00000000, 0x013590e600000000, + 0xfac383a100000000, 0x5a30dfdf00000000, 0xa1c6cc9800000000, + 0xacddf85100000000, 0x572beb1600000000, 0xf7ede11800000000, + 0x0c1bf25f00000000, 0x0100c69600000000, 0xfaf6d5d100000000, + 0x418dd38a00000000, 0xba7bc0cd00000000, 0xb760f40400000000, + 0x4c96e74300000000, 0xec50ed4d00000000, 0x17a6fe0a00000000, + 0x1abdcac300000000, 0xe14bd98400000000, 0x6c4ac67500000000, + 0x97bcd53200000000, 0x9aa7e1fb00000000, 0x6151f2bc00000000, + 0xc197f8b200000000, 0x3a61ebf500000000, 0x377adf3c00000000, + 0xcc8ccc7b00000000, 0x77f7ca2000000000, 0x8c01d96700000000, + 0x811aedae00000000, 0x7aecfee900000000, 0xda2af4e700000000, + 0x21dce7a000000000, 0x2cc7d36900000000, 0xd731c02e00000000, + 0x6c206a9500000000, 0x97d679d200000000, 0x9acd4d1b00000000, + 0x613b5e5c00000000, 0xc1fd545200000000, 0x3a0b471500000000, + 0x371073dc00000000, 0xcce6609b00000000, 0x779d66c000000000, + 0x8c6b758700000000, 0x8170414e00000000, 0x7a86520900000000, + 0xda40580700000000, 0x21b64b4000000000, 0x2cad7f8900000000, + 0xd75b6cce00000000, 0x5a5a733f00000000, 0xa1ac607800000000, + 0xacb754b100000000, 0x574147f600000000, 0xf7874df800000000, + 0x0c715ebf00000000, 0x016a6a7600000000, 0xfa9c793100000000, + 0x41e77f6a00000000, 0xba116c2d00000000, 0xb70a58e400000000, + 0x4cfc4ba300000000, 0xec3a41ad00000000, 0x17cc52ea00000000, + 0x1ad7662300000000, 0xe121756400000000, 0x41d2291a00000000, + 0xba243a5d00000000, 0xb73f0e9400000000, 0x4cc91dd300000000, + 0xec0f17dd00000000, 0x17f9049a00000000, 0x1ae2305300000000, + 0xe114231400000000, 0x5a6f254f00000000, 0xa199360800000000, + 0xac8202c100000000, 0x5774118600000000, 0xf7b21b8800000000, + 0x0c4408cf00000000, 0x015f3c0600000000, 0xfaa92f4100000000, + 0x77a830b000000000, 0x8c5e23f700000000, 0x8145173e00000000, + 0x7ab3047900000000, 0xda750e7700000000, 0x21831d3000000000, + 0x2c9829f900000000, 0xd76e3abe00000000, 0x6c153ce500000000, + 0x97e32fa200000000, 0x9af81b6b00000000, 0x610e082c00000000, + 0xc1c8022200000000, 0x3a3e116500000000, 0x372525ac00000000, + 0xccd336eb00000000}, + {0x0000000000000000, 0x6238282a00000000, 0xc470505400000000, + 0xa648787e00000000, 0x88e1a0a800000000, 0xead9888200000000, + 0x4c91f0fc00000000, 0x2ea9d8d600000000, 0x51c5308a00000000, + 0x33fd18a000000000, 0x95b560de00000000, 0xf78d48f400000000, + 0xd924902200000000, 0xbb1cb80800000000, 0x1d54c07600000000, + 0x7f6ce85c00000000, 0xe38c10cf00000000, 0x81b438e500000000, + 0x27fc409b00000000, 0x45c468b100000000, 0x6b6db06700000000, + 0x0955984d00000000, 0xaf1de03300000000, 0xcd25c81900000000, + 0xb249204500000000, 0xd071086f00000000, 0x7639701100000000, + 0x1401583b00000000, 0x3aa880ed00000000, 0x5890a8c700000000, + 0xfed8d0b900000000, 0x9ce0f89300000000, 0x871f504500000000, + 0xe527786f00000000, 0x436f001100000000, 0x2157283b00000000, + 0x0ffef0ed00000000, 0x6dc6d8c700000000, 0xcb8ea0b900000000, + 0xa9b6889300000000, 0xd6da60cf00000000, 0xb4e248e500000000, + 0x12aa309b00000000, 0x709218b100000000, 0x5e3bc06700000000, + 0x3c03e84d00000000, 0x9a4b903300000000, 0xf873b81900000000, + 0x6493408a00000000, 0x06ab68a000000000, 0xa0e310de00000000, + 0xc2db38f400000000, 0xec72e02200000000, 0x8e4ac80800000000, + 0x2802b07600000000, 0x4a3a985c00000000, 0x3556700000000000, + 0x576e582a00000000, 0xf126205400000000, 0x931e087e00000000, + 0xbdb7d0a800000000, 0xdf8ff88200000000, 0x79c780fc00000000, + 0x1bffa8d600000000, 0x0e3fa08a00000000, 0x6c0788a000000000, + 0xca4ff0de00000000, 0xa877d8f400000000, 0x86de002200000000, + 0xe4e6280800000000, 0x42ae507600000000, 0x2096785c00000000, + 0x5ffa900000000000, 0x3dc2b82a00000000, 0x9b8ac05400000000, + 0xf9b2e87e00000000, 0xd71b30a800000000, 0xb523188200000000, + 0x136b60fc00000000, 0x715348d600000000, 0xedb3b04500000000, + 0x8f8b986f00000000, 0x29c3e01100000000, 0x4bfbc83b00000000, + 0x655210ed00000000, 0x076a38c700000000, 0xa12240b900000000, + 0xc31a689300000000, 0xbc7680cf00000000, 0xde4ea8e500000000, + 0x7806d09b00000000, 0x1a3ef8b100000000, 0x3497206700000000, + 0x56af084d00000000, 0xf0e7703300000000, 0x92df581900000000, + 0x8920f0cf00000000, 0xeb18d8e500000000, 0x4d50a09b00000000, + 0x2f6888b100000000, 0x01c1506700000000, 0x63f9784d00000000, + 0xc5b1003300000000, 0xa789281900000000, 0xd8e5c04500000000, + 0xbadde86f00000000, 0x1c95901100000000, 0x7eadb83b00000000, + 0x500460ed00000000, 0x323c48c700000000, 0x947430b900000000, + 0xf64c189300000000, 0x6aace00000000000, 0x0894c82a00000000, + 0xaedcb05400000000, 0xcce4987e00000000, 0xe24d40a800000000, + 0x8075688200000000, 0x263d10fc00000000, 0x440538d600000000, + 0x3b69d08a00000000, 0x5951f8a000000000, 0xff1980de00000000, + 0x9d21a8f400000000, 0xb388702200000000, 0xd1b0580800000000, + 0x77f8207600000000, 0x15c0085c00000000, 0x5d7831ce00000000, + 0x3f4019e400000000, 0x9908619a00000000, 0xfb3049b000000000, + 0xd599916600000000, 0xb7a1b94c00000000, 0x11e9c13200000000, + 0x73d1e91800000000, 0x0cbd014400000000, 0x6e85296e00000000, + 0xc8cd511000000000, 0xaaf5793a00000000, 0x845ca1ec00000000, + 0xe66489c600000000, 0x402cf1b800000000, 0x2214d99200000000, + 0xbef4210100000000, 0xdccc092b00000000, 0x7a84715500000000, + 0x18bc597f00000000, 0x361581a900000000, 0x542da98300000000, + 0xf265d1fd00000000, 0x905df9d700000000, 0xef31118b00000000, + 0x8d0939a100000000, 0x2b4141df00000000, 0x497969f500000000, + 0x67d0b12300000000, 0x05e8990900000000, 0xa3a0e17700000000, + 0xc198c95d00000000, 0xda67618b00000000, 0xb85f49a100000000, + 0x1e1731df00000000, 0x7c2f19f500000000, 0x5286c12300000000, + 0x30bee90900000000, 0x96f6917700000000, 0xf4ceb95d00000000, + 0x8ba2510100000000, 0xe99a792b00000000, 0x4fd2015500000000, + 0x2dea297f00000000, 0x0343f1a900000000, 0x617bd98300000000, + 0xc733a1fd00000000, 0xa50b89d700000000, 0x39eb714400000000, + 0x5bd3596e00000000, 0xfd9b211000000000, 0x9fa3093a00000000, + 0xb10ad1ec00000000, 0xd332f9c600000000, 0x757a81b800000000, + 0x1742a99200000000, 0x682e41ce00000000, 0x0a1669e400000000, + 0xac5e119a00000000, 0xce6639b000000000, 0xe0cfe16600000000, + 0x82f7c94c00000000, 0x24bfb13200000000, 0x4687991800000000, + 0x5347914400000000, 0x317fb96e00000000, 0x9737c11000000000, + 0xf50fe93a00000000, 0xdba631ec00000000, 0xb99e19c600000000, + 0x1fd661b800000000, 0x7dee499200000000, 0x0282a1ce00000000, + 0x60ba89e400000000, 0xc6f2f19a00000000, 0xa4cad9b000000000, + 0x8a63016600000000, 0xe85b294c00000000, 0x4e13513200000000, + 0x2c2b791800000000, 0xb0cb818b00000000, 0xd2f3a9a100000000, + 0x74bbd1df00000000, 0x1683f9f500000000, 0x382a212300000000, + 0x5a12090900000000, 0xfc5a717700000000, 0x9e62595d00000000, + 0xe10eb10100000000, 0x8336992b00000000, 0x257ee15500000000, + 0x4746c97f00000000, 0x69ef11a900000000, 0x0bd7398300000000, + 0xad9f41fd00000000, 0xcfa769d700000000, 0xd458c10100000000, + 0xb660e92b00000000, 0x1028915500000000, 0x7210b97f00000000, + 0x5cb961a900000000, 0x3e81498300000000, 0x98c931fd00000000, + 0xfaf119d700000000, 0x859df18b00000000, 0xe7a5d9a100000000, + 0x41eda1df00000000, 0x23d589f500000000, 0x0d7c512300000000, + 0x6f44790900000000, 0xc90c017700000000, 0xab34295d00000000, + 0x37d4d1ce00000000, 0x55ecf9e400000000, 0xf3a4819a00000000, + 0x919ca9b000000000, 0xbf35716600000000, 0xdd0d594c00000000, + 0x7b45213200000000, 0x197d091800000000, 0x6611e14400000000, + 0x0429c96e00000000, 0xa261b11000000000, 0xc059993a00000000, + 0xeef041ec00000000, 0x8cc869c600000000, 0x2a8011b800000000, + 0x48b8399200000000}, + {0x0000000000000000, 0x4c2896a300000000, 0xd9565d9c00000000, + 0x957ecb3f00000000, 0xf3abcbe300000000, 0xbf835d4000000000, + 0x2afd967f00000000, 0x66d500dc00000000, 0xa751e61c00000000, + 0xeb7970bf00000000, 0x7e07bb8000000000, 0x322f2d2300000000, + 0x54fa2dff00000000, 0x18d2bb5c00000000, 0x8dac706300000000, + 0xc184e6c000000000, 0x4ea3cc3900000000, 0x028b5a9a00000000, + 0x97f591a500000000, 0xdbdd070600000000, 0xbd0807da00000000, + 0xf120917900000000, 0x645e5a4600000000, 0x2876cce500000000, + 0xe9f22a2500000000, 0xa5dabc8600000000, 0x30a477b900000000, + 0x7c8ce11a00000000, 0x1a59e1c600000000, 0x5671776500000000, + 0xc30fbc5a00000000, 0x8f272af900000000, 0x9c46997300000000, + 0xd06e0fd000000000, 0x4510c4ef00000000, 0x0938524c00000000, + 0x6fed529000000000, 0x23c5c43300000000, 0xb6bb0f0c00000000, + 0xfa9399af00000000, 0x3b177f6f00000000, 0x773fe9cc00000000, + 0xe24122f300000000, 0xae69b45000000000, 0xc8bcb48c00000000, + 0x8494222f00000000, 0x11eae91000000000, 0x5dc27fb300000000, + 0xd2e5554a00000000, 0x9ecdc3e900000000, 0x0bb308d600000000, + 0x479b9e7500000000, 0x214e9ea900000000, 0x6d66080a00000000, + 0xf818c33500000000, 0xb430559600000000, 0x75b4b35600000000, + 0x399c25f500000000, 0xace2eeca00000000, 0xe0ca786900000000, + 0x861f78b500000000, 0xca37ee1600000000, 0x5f49252900000000, + 0x1361b38a00000000, 0x388d32e700000000, 0x74a5a44400000000, + 0xe1db6f7b00000000, 0xadf3f9d800000000, 0xcb26f90400000000, + 0x870e6fa700000000, 0x1270a49800000000, 0x5e58323b00000000, + 0x9fdcd4fb00000000, 0xd3f4425800000000, 0x468a896700000000, + 0x0aa21fc400000000, 0x6c771f1800000000, 0x205f89bb00000000, + 0xb521428400000000, 0xf909d42700000000, 0x762efede00000000, + 0x3a06687d00000000, 0xaf78a34200000000, 0xe35035e100000000, + 0x8585353d00000000, 0xc9ada39e00000000, 0x5cd368a100000000, + 0x10fbfe0200000000, 0xd17f18c200000000, 0x9d578e6100000000, + 0x0829455e00000000, 0x4401d3fd00000000, 0x22d4d32100000000, + 0x6efc458200000000, 0xfb828ebd00000000, 0xb7aa181e00000000, + 0xa4cbab9400000000, 0xe8e33d3700000000, 0x7d9df60800000000, + 0x31b560ab00000000, 0x5760607700000000, 0x1b48f6d400000000, + 0x8e363deb00000000, 0xc21eab4800000000, 0x039a4d8800000000, + 0x4fb2db2b00000000, 0xdacc101400000000, 0x96e486b700000000, + 0xf031866b00000000, 0xbc1910c800000000, 0x2967dbf700000000, + 0x654f4d5400000000, 0xea6867ad00000000, 0xa640f10e00000000, + 0x333e3a3100000000, 0x7f16ac9200000000, 0x19c3ac4e00000000, + 0x55eb3aed00000000, 0xc095f1d200000000, 0x8cbd677100000000, + 0x4d3981b100000000, 0x0111171200000000, 0x946fdc2d00000000, + 0xd8474a8e00000000, 0xbe924a5200000000, 0xf2badcf100000000, + 0x67c417ce00000000, 0x2bec816d00000000, 0x311c141500000000, + 0x7d3482b600000000, 0xe84a498900000000, 0xa462df2a00000000, + 0xc2b7dff600000000, 0x8e9f495500000000, 0x1be1826a00000000, + 0x57c914c900000000, 0x964df20900000000, 0xda6564aa00000000, + 0x4f1baf9500000000, 0x0333393600000000, 0x65e639ea00000000, + 0x29ceaf4900000000, 0xbcb0647600000000, 0xf098f2d500000000, + 0x7fbfd82c00000000, 0x33974e8f00000000, 0xa6e985b000000000, + 0xeac1131300000000, 0x8c1413cf00000000, 0xc03c856c00000000, + 0x55424e5300000000, 0x196ad8f000000000, 0xd8ee3e3000000000, + 0x94c6a89300000000, 0x01b863ac00000000, 0x4d90f50f00000000, + 0x2b45f5d300000000, 0x676d637000000000, 0xf213a84f00000000, + 0xbe3b3eec00000000, 0xad5a8d6600000000, 0xe1721bc500000000, + 0x740cd0fa00000000, 0x3824465900000000, 0x5ef1468500000000, + 0x12d9d02600000000, 0x87a71b1900000000, 0xcb8f8dba00000000, + 0x0a0b6b7a00000000, 0x4623fdd900000000, 0xd35d36e600000000, + 0x9f75a04500000000, 0xf9a0a09900000000, 0xb588363a00000000, + 0x20f6fd0500000000, 0x6cde6ba600000000, 0xe3f9415f00000000, + 0xafd1d7fc00000000, 0x3aaf1cc300000000, 0x76878a6000000000, + 0x10528abc00000000, 0x5c7a1c1f00000000, 0xc904d72000000000, + 0x852c418300000000, 0x44a8a74300000000, 0x088031e000000000, + 0x9dfefadf00000000, 0xd1d66c7c00000000, 0xb7036ca000000000, + 0xfb2bfa0300000000, 0x6e55313c00000000, 0x227da79f00000000, + 0x099126f200000000, 0x45b9b05100000000, 0xd0c77b6e00000000, + 0x9cefedcd00000000, 0xfa3aed1100000000, 0xb6127bb200000000, + 0x236cb08d00000000, 0x6f44262e00000000, 0xaec0c0ee00000000, + 0xe2e8564d00000000, 0x77969d7200000000, 0x3bbe0bd100000000, + 0x5d6b0b0d00000000, 0x11439dae00000000, 0x843d569100000000, + 0xc815c03200000000, 0x4732eacb00000000, 0x0b1a7c6800000000, + 0x9e64b75700000000, 0xd24c21f400000000, 0xb499212800000000, + 0xf8b1b78b00000000, 0x6dcf7cb400000000, 0x21e7ea1700000000, + 0xe0630cd700000000, 0xac4b9a7400000000, 0x3935514b00000000, + 0x751dc7e800000000, 0x13c8c73400000000, 0x5fe0519700000000, + 0xca9e9aa800000000, 0x86b60c0b00000000, 0x95d7bf8100000000, + 0xd9ff292200000000, 0x4c81e21d00000000, 0x00a974be00000000, + 0x667c746200000000, 0x2a54e2c100000000, 0xbf2a29fe00000000, + 0xf302bf5d00000000, 0x3286599d00000000, 0x7eaecf3e00000000, + 0xebd0040100000000, 0xa7f892a200000000, 0xc12d927e00000000, + 0x8d0504dd00000000, 0x187bcfe200000000, 0x5453594100000000, + 0xdb7473b800000000, 0x975ce51b00000000, 0x02222e2400000000, + 0x4e0ab88700000000, 0x28dfb85b00000000, 0x64f72ef800000000, + 0xf189e5c700000000, 0xbda1736400000000, 0x7c2595a400000000, + 0x300d030700000000, 0xa573c83800000000, 0xe95b5e9b00000000, + 0x8f8e5e4700000000, 0xc3a6c8e400000000, 0x56d803db00000000, + 0x1af0957800000000}, + {0x0000000000000000, 0x939bc97f00000000, 0x263793ff00000000, + 0xb5ac5a8000000000, 0x0d68572400000000, 0x9ef39e5b00000000, + 0x2b5fc4db00000000, 0xb8c40da400000000, 0x1ad0ae4800000000, + 0x894b673700000000, 0x3ce73db700000000, 0xaf7cf4c800000000, + 0x17b8f96c00000000, 0x8423301300000000, 0x318f6a9300000000, + 0xa214a3ec00000000, 0x34a05d9100000000, 0xa73b94ee00000000, + 0x1297ce6e00000000, 0x810c071100000000, 0x39c80ab500000000, + 0xaa53c3ca00000000, 0x1fff994a00000000, 0x8c64503500000000, + 0x2e70f3d900000000, 0xbdeb3aa600000000, 0x0847602600000000, + 0x9bdca95900000000, 0x2318a4fd00000000, 0xb0836d8200000000, + 0x052f370200000000, 0x96b4fe7d00000000, 0x2946caf900000000, + 0xbadd038600000000, 0x0f71590600000000, 0x9cea907900000000, + 0x242e9ddd00000000, 0xb7b554a200000000, 0x02190e2200000000, + 0x9182c75d00000000, 0x339664b100000000, 0xa00dadce00000000, + 0x15a1f74e00000000, 0x863a3e3100000000, 0x3efe339500000000, + 0xad65faea00000000, 0x18c9a06a00000000, 0x8b52691500000000, + 0x1de6976800000000, 0x8e7d5e1700000000, 0x3bd1049700000000, + 0xa84acde800000000, 0x108ec04c00000000, 0x8315093300000000, + 0x36b953b300000000, 0xa5229acc00000000, 0x0736392000000000, + 0x94adf05f00000000, 0x2101aadf00000000, 0xb29a63a000000000, + 0x0a5e6e0400000000, 0x99c5a77b00000000, 0x2c69fdfb00000000, + 0xbff2348400000000, 0x138ae52800000000, 0x80112c5700000000, + 0x35bd76d700000000, 0xa626bfa800000000, 0x1ee2b20c00000000, + 0x8d797b7300000000, 0x38d521f300000000, 0xab4ee88c00000000, + 0x095a4b6000000000, 0x9ac1821f00000000, 0x2f6dd89f00000000, + 0xbcf611e000000000, 0x04321c4400000000, 0x97a9d53b00000000, + 0x22058fbb00000000, 0xb19e46c400000000, 0x272ab8b900000000, + 0xb4b171c600000000, 0x011d2b4600000000, 0x9286e23900000000, + 0x2a42ef9d00000000, 0xb9d926e200000000, 0x0c757c6200000000, + 0x9feeb51d00000000, 0x3dfa16f100000000, 0xae61df8e00000000, + 0x1bcd850e00000000, 0x88564c7100000000, 0x309241d500000000, + 0xa30988aa00000000, 0x16a5d22a00000000, 0x853e1b5500000000, + 0x3acc2fd100000000, 0xa957e6ae00000000, 0x1cfbbc2e00000000, + 0x8f60755100000000, 0x37a478f500000000, 0xa43fb18a00000000, + 0x1193eb0a00000000, 0x8208227500000000, 0x201c819900000000, + 0xb38748e600000000, 0x062b126600000000, 0x95b0db1900000000, + 0x2d74d6bd00000000, 0xbeef1fc200000000, 0x0b43454200000000, + 0x98d88c3d00000000, 0x0e6c724000000000, 0x9df7bb3f00000000, + 0x285be1bf00000000, 0xbbc028c000000000, 0x0304256400000000, + 0x909fec1b00000000, 0x2533b69b00000000, 0xb6a87fe400000000, + 0x14bcdc0800000000, 0x8727157700000000, 0x328b4ff700000000, + 0xa110868800000000, 0x19d48b2c00000000, 0x8a4f425300000000, + 0x3fe318d300000000, 0xac78d1ac00000000, 0x2614cb5100000000, + 0xb58f022e00000000, 0x002358ae00000000, 0x93b891d100000000, + 0x2b7c9c7500000000, 0xb8e7550a00000000, 0x0d4b0f8a00000000, + 0x9ed0c6f500000000, 0x3cc4651900000000, 0xaf5fac6600000000, + 0x1af3f6e600000000, 0x89683f9900000000, 0x31ac323d00000000, + 0xa237fb4200000000, 0x179ba1c200000000, 0x840068bd00000000, + 0x12b496c000000000, 0x812f5fbf00000000, 0x3483053f00000000, + 0xa718cc4000000000, 0x1fdcc1e400000000, 0x8c47089b00000000, + 0x39eb521b00000000, 0xaa709b6400000000, 0x0864388800000000, + 0x9bfff1f700000000, 0x2e53ab7700000000, 0xbdc8620800000000, + 0x050c6fac00000000, 0x9697a6d300000000, 0x233bfc5300000000, + 0xb0a0352c00000000, 0x0f5201a800000000, 0x9cc9c8d700000000, + 0x2965925700000000, 0xbafe5b2800000000, 0x023a568c00000000, + 0x91a19ff300000000, 0x240dc57300000000, 0xb7960c0c00000000, + 0x1582afe000000000, 0x8619669f00000000, 0x33b53c1f00000000, + 0xa02ef56000000000, 0x18eaf8c400000000, 0x8b7131bb00000000, + 0x3edd6b3b00000000, 0xad46a24400000000, 0x3bf25c3900000000, + 0xa869954600000000, 0x1dc5cfc600000000, 0x8e5e06b900000000, + 0x369a0b1d00000000, 0xa501c26200000000, 0x10ad98e200000000, + 0x8336519d00000000, 0x2122f27100000000, 0xb2b93b0e00000000, + 0x0715618e00000000, 0x948ea8f100000000, 0x2c4aa55500000000, + 0xbfd16c2a00000000, 0x0a7d36aa00000000, 0x99e6ffd500000000, + 0x359e2e7900000000, 0xa605e70600000000, 0x13a9bd8600000000, + 0x803274f900000000, 0x38f6795d00000000, 0xab6db02200000000, + 0x1ec1eaa200000000, 0x8d5a23dd00000000, 0x2f4e803100000000, + 0xbcd5494e00000000, 0x097913ce00000000, 0x9ae2dab100000000, + 0x2226d71500000000, 0xb1bd1e6a00000000, 0x041144ea00000000, + 0x978a8d9500000000, 0x013e73e800000000, 0x92a5ba9700000000, + 0x2709e01700000000, 0xb492296800000000, 0x0c5624cc00000000, + 0x9fcdedb300000000, 0x2a61b73300000000, 0xb9fa7e4c00000000, + 0x1beedda000000000, 0x887514df00000000, 0x3dd94e5f00000000, + 0xae42872000000000, 0x16868a8400000000, 0x851d43fb00000000, + 0x30b1197b00000000, 0xa32ad00400000000, 0x1cd8e48000000000, + 0x8f432dff00000000, 0x3aef777f00000000, 0xa974be0000000000, + 0x11b0b3a400000000, 0x822b7adb00000000, 0x3787205b00000000, + 0xa41ce92400000000, 0x06084ac800000000, 0x959383b700000000, + 0x203fd93700000000, 0xb3a4104800000000, 0x0b601dec00000000, + 0x98fbd49300000000, 0x2d578e1300000000, 0xbecc476c00000000, + 0x2878b91100000000, 0xbbe3706e00000000, 0x0e4f2aee00000000, + 0x9dd4e39100000000, 0x2510ee3500000000, 0xb68b274a00000000, + 0x03277dca00000000, 0x90bcb4b500000000, 0x32a8175900000000, + 0xa133de2600000000, 0x149f84a600000000, 0x87044dd900000000, + 0x3fc0407d00000000, 0xac5b890200000000, 0x19f7d38200000000, + 0x8a6c1afd00000000}, + {0x0000000000000000, 0x650b796900000000, 0xca16f2d200000000, + 0xaf1d8bbb00000000, 0xd52b957e00000000, 0xb020ec1700000000, + 0x1f3d67ac00000000, 0x7a361ec500000000, 0xaa572afd00000000, + 0xcf5c539400000000, 0x6041d82f00000000, 0x054aa14600000000, + 0x7f7cbf8300000000, 0x1a77c6ea00000000, 0xb56a4d5100000000, + 0xd061343800000000, 0x15a9252100000000, 0x70a25c4800000000, + 0xdfbfd7f300000000, 0xbab4ae9a00000000, 0xc082b05f00000000, + 0xa589c93600000000, 0x0a94428d00000000, 0x6f9f3be400000000, + 0xbffe0fdc00000000, 0xdaf576b500000000, 0x75e8fd0e00000000, + 0x10e3846700000000, 0x6ad59aa200000000, 0x0fdee3cb00000000, + 0xa0c3687000000000, 0xc5c8111900000000, 0x2a524b4200000000, + 0x4f59322b00000000, 0xe044b99000000000, 0x854fc0f900000000, + 0xff79de3c00000000, 0x9a72a75500000000, 0x356f2cee00000000, + 0x5064558700000000, 0x800561bf00000000, 0xe50e18d600000000, + 0x4a13936d00000000, 0x2f18ea0400000000, 0x552ef4c100000000, + 0x30258da800000000, 0x9f38061300000000, 0xfa337f7a00000000, + 0x3ffb6e6300000000, 0x5af0170a00000000, 0xf5ed9cb100000000, + 0x90e6e5d800000000, 0xead0fb1d00000000, 0x8fdb827400000000, + 0x20c609cf00000000, 0x45cd70a600000000, 0x95ac449e00000000, + 0xf0a73df700000000, 0x5fbab64c00000000, 0x3ab1cf2500000000, + 0x4087d1e000000000, 0x258ca88900000000, 0x8a91233200000000, + 0xef9a5a5b00000000, 0x54a4968400000000, 0x31afefed00000000, + 0x9eb2645600000000, 0xfbb91d3f00000000, 0x818f03fa00000000, + 0xe4847a9300000000, 0x4b99f12800000000, 0x2e92884100000000, + 0xfef3bc7900000000, 0x9bf8c51000000000, 0x34e54eab00000000, + 0x51ee37c200000000, 0x2bd8290700000000, 0x4ed3506e00000000, + 0xe1cedbd500000000, 0x84c5a2bc00000000, 0x410db3a500000000, + 0x2406cacc00000000, 0x8b1b417700000000, 0xee10381e00000000, + 0x942626db00000000, 0xf12d5fb200000000, 0x5e30d40900000000, + 0x3b3bad6000000000, 0xeb5a995800000000, 0x8e51e03100000000, + 0x214c6b8a00000000, 0x444712e300000000, 0x3e710c2600000000, + 0x5b7a754f00000000, 0xf467fef400000000, 0x916c879d00000000, + 0x7ef6ddc600000000, 0x1bfda4af00000000, 0xb4e02f1400000000, + 0xd1eb567d00000000, 0xabdd48b800000000, 0xced631d100000000, + 0x61cbba6a00000000, 0x04c0c30300000000, 0xd4a1f73b00000000, + 0xb1aa8e5200000000, 0x1eb705e900000000, 0x7bbc7c8000000000, + 0x018a624500000000, 0x64811b2c00000000, 0xcb9c909700000000, + 0xae97e9fe00000000, 0x6b5ff8e700000000, 0x0e54818e00000000, + 0xa1490a3500000000, 0xc442735c00000000, 0xbe746d9900000000, + 0xdb7f14f000000000, 0x74629f4b00000000, 0x1169e62200000000, + 0xc108d21a00000000, 0xa403ab7300000000, 0x0b1e20c800000000, + 0x6e1559a100000000, 0x1423476400000000, 0x71283e0d00000000, + 0xde35b5b600000000, 0xbb3eccdf00000000, 0xe94e5cd200000000, + 0x8c4525bb00000000, 0x2358ae0000000000, 0x4653d76900000000, + 0x3c65c9ac00000000, 0x596eb0c500000000, 0xf6733b7e00000000, + 0x9378421700000000, 0x4319762f00000000, 0x26120f4600000000, + 0x890f84fd00000000, 0xec04fd9400000000, 0x9632e35100000000, + 0xf3399a3800000000, 0x5c24118300000000, 0x392f68ea00000000, + 0xfce779f300000000, 0x99ec009a00000000, 0x36f18b2100000000, + 0x53faf24800000000, 0x29ccec8d00000000, 0x4cc795e400000000, + 0xe3da1e5f00000000, 0x86d1673600000000, 0x56b0530e00000000, + 0x33bb2a6700000000, 0x9ca6a1dc00000000, 0xf9add8b500000000, + 0x839bc67000000000, 0xe690bf1900000000, 0x498d34a200000000, + 0x2c864dcb00000000, 0xc31c179000000000, 0xa6176ef900000000, + 0x090ae54200000000, 0x6c019c2b00000000, 0x163782ee00000000, + 0x733cfb8700000000, 0xdc21703c00000000, 0xb92a095500000000, + 0x694b3d6d00000000, 0x0c40440400000000, 0xa35dcfbf00000000, + 0xc656b6d600000000, 0xbc60a81300000000, 0xd96bd17a00000000, + 0x76765ac100000000, 0x137d23a800000000, 0xd6b532b100000000, + 0xb3be4bd800000000, 0x1ca3c06300000000, 0x79a8b90a00000000, + 0x039ea7cf00000000, 0x6695dea600000000, 0xc988551d00000000, + 0xac832c7400000000, 0x7ce2184c00000000, 0x19e9612500000000, + 0xb6f4ea9e00000000, 0xd3ff93f700000000, 0xa9c98d3200000000, + 0xccc2f45b00000000, 0x63df7fe000000000, 0x06d4068900000000, + 0xbdeaca5600000000, 0xd8e1b33f00000000, 0x77fc388400000000, + 0x12f741ed00000000, 0x68c15f2800000000, 0x0dca264100000000, + 0xa2d7adfa00000000, 0xc7dcd49300000000, 0x17bde0ab00000000, + 0x72b699c200000000, 0xddab127900000000, 0xb8a06b1000000000, + 0xc29675d500000000, 0xa79d0cbc00000000, 0x0880870700000000, + 0x6d8bfe6e00000000, 0xa843ef7700000000, 0xcd48961e00000000, + 0x62551da500000000, 0x075e64cc00000000, 0x7d687a0900000000, + 0x1863036000000000, 0xb77e88db00000000, 0xd275f1b200000000, + 0x0214c58a00000000, 0x671fbce300000000, 0xc802375800000000, + 0xad094e3100000000, 0xd73f50f400000000, 0xb234299d00000000, + 0x1d29a22600000000, 0x7822db4f00000000, 0x97b8811400000000, + 0xf2b3f87d00000000, 0x5dae73c600000000, 0x38a50aaf00000000, + 0x4293146a00000000, 0x27986d0300000000, 0x8885e6b800000000, + 0xed8e9fd100000000, 0x3defabe900000000, 0x58e4d28000000000, + 0xf7f9593b00000000, 0x92f2205200000000, 0xe8c43e9700000000, + 0x8dcf47fe00000000, 0x22d2cc4500000000, 0x47d9b52c00000000, + 0x8211a43500000000, 0xe71add5c00000000, 0x480756e700000000, + 0x2d0c2f8e00000000, 0x573a314b00000000, 0x3231482200000000, + 0x9d2cc39900000000, 0xf827baf000000000, 0x28468ec800000000, + 0x4d4df7a100000000, 0xe2507c1a00000000, 0x875b057300000000, + 0xfd6d1bb600000000, 0x986662df00000000, 0x377be96400000000, + 0x5270900d00000000}, + {0x0000000000000000, 0xdcecb13d00000000, 0xb8d9637b00000000, + 0x6435d24600000000, 0x70b3c7f600000000, 0xac5f76cb00000000, + 0xc86aa48d00000000, 0x148615b000000000, 0xa160fe3600000000, + 0x7d8c4f0b00000000, 0x19b99d4d00000000, 0xc5552c7000000000, + 0xd1d339c000000000, 0x0d3f88fd00000000, 0x690a5abb00000000, + 0xb5e6eb8600000000, 0x42c1fc6d00000000, 0x9e2d4d5000000000, + 0xfa189f1600000000, 0x26f42e2b00000000, 0x32723b9b00000000, + 0xee9e8aa600000000, 0x8aab58e000000000, 0x5647e9dd00000000, + 0xe3a1025b00000000, 0x3f4db36600000000, 0x5b78612000000000, + 0x8794d01d00000000, 0x9312c5ad00000000, 0x4ffe749000000000, + 0x2bcba6d600000000, 0xf72717eb00000000, 0x8482f9db00000000, + 0x586e48e600000000, 0x3c5b9aa000000000, 0xe0b72b9d00000000, + 0xf4313e2d00000000, 0x28dd8f1000000000, 0x4ce85d5600000000, + 0x9004ec6b00000000, 0x25e207ed00000000, 0xf90eb6d000000000, + 0x9d3b649600000000, 0x41d7d5ab00000000, 0x5551c01b00000000, + 0x89bd712600000000, 0xed88a36000000000, 0x3164125d00000000, + 0xc64305b600000000, 0x1aafb48b00000000, 0x7e9a66cd00000000, + 0xa276d7f000000000, 0xb6f0c24000000000, 0x6a1c737d00000000, + 0x0e29a13b00000000, 0xd2c5100600000000, 0x6723fb8000000000, + 0xbbcf4abd00000000, 0xdffa98fb00000000, 0x031629c600000000, + 0x17903c7600000000, 0xcb7c8d4b00000000, 0xaf495f0d00000000, + 0x73a5ee3000000000, 0x4903826c00000000, 0x95ef335100000000, + 0xf1dae11700000000, 0x2d36502a00000000, 0x39b0459a00000000, + 0xe55cf4a700000000, 0x816926e100000000, 0x5d8597dc00000000, + 0xe8637c5a00000000, 0x348fcd6700000000, 0x50ba1f2100000000, + 0x8c56ae1c00000000, 0x98d0bbac00000000, 0x443c0a9100000000, + 0x2009d8d700000000, 0xfce569ea00000000, 0x0bc27e0100000000, + 0xd72ecf3c00000000, 0xb31b1d7a00000000, 0x6ff7ac4700000000, + 0x7b71b9f700000000, 0xa79d08ca00000000, 0xc3a8da8c00000000, + 0x1f446bb100000000, 0xaaa2803700000000, 0x764e310a00000000, + 0x127be34c00000000, 0xce97527100000000, 0xda1147c100000000, + 0x06fdf6fc00000000, 0x62c824ba00000000, 0xbe24958700000000, + 0xcd817bb700000000, 0x116dca8a00000000, 0x755818cc00000000, + 0xa9b4a9f100000000, 0xbd32bc4100000000, 0x61de0d7c00000000, + 0x05ebdf3a00000000, 0xd9076e0700000000, 0x6ce1858100000000, + 0xb00d34bc00000000, 0xd438e6fa00000000, 0x08d457c700000000, + 0x1c52427700000000, 0xc0bef34a00000000, 0xa48b210c00000000, + 0x7867903100000000, 0x8f4087da00000000, 0x53ac36e700000000, + 0x3799e4a100000000, 0xeb75559c00000000, 0xfff3402c00000000, + 0x231ff11100000000, 0x472a235700000000, 0x9bc6926a00000000, + 0x2e2079ec00000000, 0xf2ccc8d100000000, 0x96f91a9700000000, + 0x4a15abaa00000000, 0x5e93be1a00000000, 0x827f0f2700000000, + 0xe64add6100000000, 0x3aa66c5c00000000, 0x920604d900000000, + 0x4eeab5e400000000, 0x2adf67a200000000, 0xf633d69f00000000, + 0xe2b5c32f00000000, 0x3e59721200000000, 0x5a6ca05400000000, + 0x8680116900000000, 0x3366faef00000000, 0xef8a4bd200000000, + 0x8bbf999400000000, 0x575328a900000000, 0x43d53d1900000000, + 0x9f398c2400000000, 0xfb0c5e6200000000, 0x27e0ef5f00000000, + 0xd0c7f8b400000000, 0x0c2b498900000000, 0x681e9bcf00000000, + 0xb4f22af200000000, 0xa0743f4200000000, 0x7c988e7f00000000, + 0x18ad5c3900000000, 0xc441ed0400000000, 0x71a7068200000000, + 0xad4bb7bf00000000, 0xc97e65f900000000, 0x1592d4c400000000, + 0x0114c17400000000, 0xddf8704900000000, 0xb9cda20f00000000, + 0x6521133200000000, 0x1684fd0200000000, 0xca684c3f00000000, + 0xae5d9e7900000000, 0x72b12f4400000000, 0x66373af400000000, + 0xbadb8bc900000000, 0xdeee598f00000000, 0x0202e8b200000000, + 0xb7e4033400000000, 0x6b08b20900000000, 0x0f3d604f00000000, + 0xd3d1d17200000000, 0xc757c4c200000000, 0x1bbb75ff00000000, + 0x7f8ea7b900000000, 0xa362168400000000, 0x5445016f00000000, + 0x88a9b05200000000, 0xec9c621400000000, 0x3070d32900000000, + 0x24f6c69900000000, 0xf81a77a400000000, 0x9c2fa5e200000000, + 0x40c314df00000000, 0xf525ff5900000000, 0x29c94e6400000000, + 0x4dfc9c2200000000, 0x91102d1f00000000, 0x859638af00000000, + 0x597a899200000000, 0x3d4f5bd400000000, 0xe1a3eae900000000, + 0xdb0586b500000000, 0x07e9378800000000, 0x63dce5ce00000000, + 0xbf3054f300000000, 0xabb6414300000000, 0x775af07e00000000, + 0x136f223800000000, 0xcf83930500000000, 0x7a65788300000000, + 0xa689c9be00000000, 0xc2bc1bf800000000, 0x1e50aac500000000, + 0x0ad6bf7500000000, 0xd63a0e4800000000, 0xb20fdc0e00000000, + 0x6ee36d3300000000, 0x99c47ad800000000, 0x4528cbe500000000, + 0x211d19a300000000, 0xfdf1a89e00000000, 0xe977bd2e00000000, + 0x359b0c1300000000, 0x51aede5500000000, 0x8d426f6800000000, + 0x38a484ee00000000, 0xe44835d300000000, 0x807de79500000000, + 0x5c9156a800000000, 0x4817431800000000, 0x94fbf22500000000, + 0xf0ce206300000000, 0x2c22915e00000000, 0x5f877f6e00000000, + 0x836bce5300000000, 0xe75e1c1500000000, 0x3bb2ad2800000000, + 0x2f34b89800000000, 0xf3d809a500000000, 0x97eddbe300000000, + 0x4b016ade00000000, 0xfee7815800000000, 0x220b306500000000, + 0x463ee22300000000, 0x9ad2531e00000000, 0x8e5446ae00000000, + 0x52b8f79300000000, 0x368d25d500000000, 0xea6194e800000000, + 0x1d46830300000000, 0xc1aa323e00000000, 0xa59fe07800000000, + 0x7973514500000000, 0x6df544f500000000, 0xb119f5c800000000, + 0xd52c278e00000000, 0x09c096b300000000, 0xbc267d3500000000, + 0x60cacc0800000000, 0x04ff1e4e00000000, 0xd813af7300000000, + 0xcc95bac300000000, 0x10790bfe00000000, 0x744cd9b800000000, + 0xa8a0688500000000}}; + +#else /* W == 4 */ + +local const z_crc_t FAR crc_braid_table[][256] = { + {0x00000000, 0x81256527, 0xd93bcc0f, 0x581ea928, 0x69069e5f, + 0xe823fb78, 0xb03d5250, 0x31183777, 0xd20d3cbe, 0x53285999, + 0x0b36f0b1, 0x8a139596, 0xbb0ba2e1, 0x3a2ec7c6, 0x62306eee, + 0xe3150bc9, 0x7f6b7f3d, 0xfe4e1a1a, 0xa650b332, 0x2775d615, + 0x166de162, 0x97488445, 0xcf562d6d, 0x4e73484a, 0xad664383, + 0x2c4326a4, 0x745d8f8c, 0xf578eaab, 0xc460dddc, 0x4545b8fb, + 0x1d5b11d3, 0x9c7e74f4, 0xfed6fe7a, 0x7ff39b5d, 0x27ed3275, + 0xa6c85752, 0x97d06025, 0x16f50502, 0x4eebac2a, 0xcfcec90d, + 0x2cdbc2c4, 0xadfea7e3, 0xf5e00ecb, 0x74c56bec, 0x45dd5c9b, + 0xc4f839bc, 0x9ce69094, 0x1dc3f5b3, 0x81bd8147, 0x0098e460, + 0x58864d48, 0xd9a3286f, 0xe8bb1f18, 0x699e7a3f, 0x3180d317, + 0xb0a5b630, 0x53b0bdf9, 0xd295d8de, 0x8a8b71f6, 0x0bae14d1, + 0x3ab623a6, 0xbb934681, 0xe38defa9, 0x62a88a8e, 0x26dcfab5, + 0xa7f99f92, 0xffe736ba, 0x7ec2539d, 0x4fda64ea, 0xceff01cd, + 0x96e1a8e5, 0x17c4cdc2, 0xf4d1c60b, 0x75f4a32c, 0x2dea0a04, + 0xaccf6f23, 0x9dd75854, 0x1cf23d73, 0x44ec945b, 0xc5c9f17c, + 0x59b78588, 0xd892e0af, 0x808c4987, 0x01a92ca0, 0x30b11bd7, + 0xb1947ef0, 0xe98ad7d8, 0x68afb2ff, 0x8bbab936, 0x0a9fdc11, + 0x52817539, 0xd3a4101e, 0xe2bc2769, 0x6399424e, 0x3b87eb66, + 0xbaa28e41, 0xd80a04cf, 0x592f61e8, 0x0131c8c0, 0x8014ade7, + 0xb10c9a90, 0x3029ffb7, 0x6837569f, 0xe91233b8, 0x0a073871, + 0x8b225d56, 0xd33cf47e, 0x52199159, 0x6301a62e, 0xe224c309, + 0xba3a6a21, 0x3b1f0f06, 0xa7617bf2, 0x26441ed5, 0x7e5ab7fd, + 0xff7fd2da, 0xce67e5ad, 0x4f42808a, 0x175c29a2, 0x96794c85, + 0x756c474c, 0xf449226b, 0xac578b43, 0x2d72ee64, 0x1c6ad913, + 0x9d4fbc34, 0xc551151c, 0x4474703b, 0x4db9f56a, 0xcc9c904d, + 0x94823965, 0x15a75c42, 0x24bf6b35, 0xa59a0e12, 0xfd84a73a, + 0x7ca1c21d, 0x9fb4c9d4, 0x1e91acf3, 0x468f05db, 0xc7aa60fc, + 0xf6b2578b, 0x779732ac, 0x2f899b84, 0xaeacfea3, 0x32d28a57, + 0xb3f7ef70, 0xebe94658, 0x6acc237f, 0x5bd41408, 0xdaf1712f, + 0x82efd807, 0x03cabd20, 0xe0dfb6e9, 0x61fad3ce, 0x39e47ae6, + 0xb8c11fc1, 0x89d928b6, 0x08fc4d91, 0x50e2e4b9, 0xd1c7819e, + 0xb36f0b10, 0x324a6e37, 0x6a54c71f, 0xeb71a238, 0xda69954f, + 0x5b4cf068, 0x03525940, 0x82773c67, 0x616237ae, 0xe0475289, + 0xb859fba1, 0x397c9e86, 0x0864a9f1, 0x8941ccd6, 0xd15f65fe, + 0x507a00d9, 0xcc04742d, 0x4d21110a, 0x153fb822, 0x941add05, + 0xa502ea72, 0x24278f55, 0x7c39267d, 0xfd1c435a, 0x1e094893, + 0x9f2c2db4, 0xc732849c, 0x4617e1bb, 0x770fd6cc, 0xf62ab3eb, + 0xae341ac3, 0x2f117fe4, 0x6b650fdf, 0xea406af8, 0xb25ec3d0, + 0x337ba6f7, 0x02639180, 0x8346f4a7, 0xdb585d8f, 0x5a7d38a8, + 0xb9683361, 0x384d5646, 0x6053ff6e, 0xe1769a49, 0xd06ead3e, + 0x514bc819, 0x09556131, 0x88700416, 0x140e70e2, 0x952b15c5, + 0xcd35bced, 0x4c10d9ca, 0x7d08eebd, 0xfc2d8b9a, 0xa43322b2, + 0x25164795, 0xc6034c5c, 0x4726297b, 0x1f388053, 0x9e1de574, + 0xaf05d203, 0x2e20b724, 0x763e1e0c, 0xf71b7b2b, 0x95b3f1a5, + 0x14969482, 0x4c883daa, 0xcdad588d, 0xfcb56ffa, 0x7d900add, + 0x258ea3f5, 0xa4abc6d2, 0x47becd1b, 0xc69ba83c, 0x9e850114, + 0x1fa06433, 0x2eb85344, 0xaf9d3663, 0xf7839f4b, 0x76a6fa6c, + 0xead88e98, 0x6bfdebbf, 0x33e34297, 0xb2c627b0, 0x83de10c7, + 0x02fb75e0, 0x5ae5dcc8, 0xdbc0b9ef, 0x38d5b226, 0xb9f0d701, + 0xe1ee7e29, 0x60cb1b0e, 0x51d32c79, 0xd0f6495e, 0x88e8e076, + 0x09cd8551}, + {0x00000000, 0x9b73ead4, 0xed96d3e9, 0x76e5393d, 0x005ca193, + 0x9b2f4b47, 0xedca727a, 0x76b998ae, 0x00b94326, 0x9bcaa9f2, + 0xed2f90cf, 0x765c7a1b, 0x00e5e2b5, 0x9b960861, 0xed73315c, + 0x7600db88, 0x0172864c, 0x9a016c98, 0xece455a5, 0x7797bf71, + 0x012e27df, 0x9a5dcd0b, 0xecb8f436, 0x77cb1ee2, 0x01cbc56a, + 0x9ab82fbe, 0xec5d1683, 0x772efc57, 0x019764f9, 0x9ae48e2d, + 0xec01b710, 0x77725dc4, 0x02e50c98, 0x9996e64c, 0xef73df71, + 0x740035a5, 0x02b9ad0b, 0x99ca47df, 0xef2f7ee2, 0x745c9436, + 0x025c4fbe, 0x992fa56a, 0xefca9c57, 0x74b97683, 0x0200ee2d, + 0x997304f9, 0xef963dc4, 0x74e5d710, 0x03978ad4, 0x98e46000, + 0xee01593d, 0x7572b3e9, 0x03cb2b47, 0x98b8c193, 0xee5df8ae, + 0x752e127a, 0x032ec9f2, 0x985d2326, 0xeeb81a1b, 0x75cbf0cf, + 0x03726861, 0x980182b5, 0xeee4bb88, 0x7597515c, 0x05ca1930, + 0x9eb9f3e4, 0xe85ccad9, 0x732f200d, 0x0596b8a3, 0x9ee55277, + 0xe8006b4a, 0x7373819e, 0x05735a16, 0x9e00b0c2, 0xe8e589ff, + 0x7396632b, 0x052ffb85, 0x9e5c1151, 0xe8b9286c, 0x73cac2b8, + 0x04b89f7c, 0x9fcb75a8, 0xe92e4c95, 0x725da641, 0x04e43eef, + 0x9f97d43b, 0xe972ed06, 0x720107d2, 0x0401dc5a, 0x9f72368e, + 0xe9970fb3, 0x72e4e567, 0x045d7dc9, 0x9f2e971d, 0xe9cbae20, + 0x72b844f4, 0x072f15a8, 0x9c5cff7c, 0xeab9c641, 0x71ca2c95, + 0x0773b43b, 0x9c005eef, 0xeae567d2, 0x71968d06, 0x0796568e, + 0x9ce5bc5a, 0xea008567, 0x71736fb3, 0x07caf71d, 0x9cb91dc9, + 0xea5c24f4, 0x712fce20, 0x065d93e4, 0x9d2e7930, 0xebcb400d, + 0x70b8aad9, 0x06013277, 0x9d72d8a3, 0xeb97e19e, 0x70e40b4a, + 0x06e4d0c2, 0x9d973a16, 0xeb72032b, 0x7001e9ff, 0x06b87151, + 0x9dcb9b85, 0xeb2ea2b8, 0x705d486c, 0x0b943260, 0x90e7d8b4, + 0xe602e189, 0x7d710b5d, 0x0bc893f3, 0x90bb7927, 0xe65e401a, + 0x7d2daace, 0x0b2d7146, 0x905e9b92, 0xe6bba2af, 0x7dc8487b, + 0x0b71d0d5, 0x90023a01, 0xe6e7033c, 0x7d94e9e8, 0x0ae6b42c, + 0x91955ef8, 0xe77067c5, 0x7c038d11, 0x0aba15bf, 0x91c9ff6b, + 0xe72cc656, 0x7c5f2c82, 0x0a5ff70a, 0x912c1dde, 0xe7c924e3, + 0x7cbace37, 0x0a035699, 0x9170bc4d, 0xe7958570, 0x7ce66fa4, + 0x09713ef8, 0x9202d42c, 0xe4e7ed11, 0x7f9407c5, 0x092d9f6b, + 0x925e75bf, 0xe4bb4c82, 0x7fc8a656, 0x09c87dde, 0x92bb970a, + 0xe45eae37, 0x7f2d44e3, 0x0994dc4d, 0x92e73699, 0xe4020fa4, + 0x7f71e570, 0x0803b8b4, 0x93705260, 0xe5956b5d, 0x7ee68189, + 0x085f1927, 0x932cf3f3, 0xe5c9cace, 0x7eba201a, 0x08bafb92, + 0x93c91146, 0xe52c287b, 0x7e5fc2af, 0x08e65a01, 0x9395b0d5, + 0xe57089e8, 0x7e03633c, 0x0e5e2b50, 0x952dc184, 0xe3c8f8b9, + 0x78bb126d, 0x0e028ac3, 0x95716017, 0xe394592a, 0x78e7b3fe, + 0x0ee76876, 0x959482a2, 0xe371bb9f, 0x7802514b, 0x0ebbc9e5, + 0x95c82331, 0xe32d1a0c, 0x785ef0d8, 0x0f2cad1c, 0x945f47c8, + 0xe2ba7ef5, 0x79c99421, 0x0f700c8f, 0x9403e65b, 0xe2e6df66, + 0x799535b2, 0x0f95ee3a, 0x94e604ee, 0xe2033dd3, 0x7970d707, + 0x0fc94fa9, 0x94baa57d, 0xe25f9c40, 0x792c7694, 0x0cbb27c8, + 0x97c8cd1c, 0xe12df421, 0x7a5e1ef5, 0x0ce7865b, 0x97946c8f, + 0xe17155b2, 0x7a02bf66, 0x0c0264ee, 0x97718e3a, 0xe194b707, + 0x7ae75dd3, 0x0c5ec57d, 0x972d2fa9, 0xe1c81694, 0x7abbfc40, + 0x0dc9a184, 0x96ba4b50, 0xe05f726d, 0x7b2c98b9, 0x0d950017, + 0x96e6eac3, 0xe003d3fe, 0x7b70392a, 0x0d70e2a2, 0x96030876, + 0xe0e6314b, 0x7b95db9f, 0x0d2c4331, 0x965fa9e5, 0xe0ba90d8, + 0x7bc97a0c}, + {0x00000000, 0x172864c0, 0x2e50c980, 0x3978ad40, 0x5ca19300, + 0x4b89f7c0, 0x72f15a80, 0x65d93e40, 0xb9432600, 0xae6b42c0, + 0x9713ef80, 0x803b8b40, 0xe5e2b500, 0xf2cad1c0, 0xcbb27c80, + 0xdc9a1840, 0xa9f74a41, 0xbedf2e81, 0x87a783c1, 0x908fe701, + 0xf556d941, 0xe27ebd81, 0xdb0610c1, 0xcc2e7401, 0x10b46c41, + 0x079c0881, 0x3ee4a5c1, 0x29ccc101, 0x4c15ff41, 0x5b3d9b81, + 0x624536c1, 0x756d5201, 0x889f92c3, 0x9fb7f603, 0xa6cf5b43, + 0xb1e73f83, 0xd43e01c3, 0xc3166503, 0xfa6ec843, 0xed46ac83, + 0x31dcb4c3, 0x26f4d003, 0x1f8c7d43, 0x08a41983, 0x6d7d27c3, + 0x7a554303, 0x432dee43, 0x54058a83, 0x2168d882, 0x3640bc42, + 0x0f381102, 0x181075c2, 0x7dc94b82, 0x6ae12f42, 0x53998202, + 0x44b1e6c2, 0x982bfe82, 0x8f039a42, 0xb67b3702, 0xa15353c2, + 0xc48a6d82, 0xd3a20942, 0xeadaa402, 0xfdf2c0c2, 0xca4e23c7, + 0xdd664707, 0xe41eea47, 0xf3368e87, 0x96efb0c7, 0x81c7d407, + 0xb8bf7947, 0xaf971d87, 0x730d05c7, 0x64256107, 0x5d5dcc47, + 0x4a75a887, 0x2fac96c7, 0x3884f207, 0x01fc5f47, 0x16d43b87, + 0x63b96986, 0x74910d46, 0x4de9a006, 0x5ac1c4c6, 0x3f18fa86, + 0x28309e46, 0x11483306, 0x066057c6, 0xdafa4f86, 0xcdd22b46, + 0xf4aa8606, 0xe382e2c6, 0x865bdc86, 0x9173b846, 0xa80b1506, + 0xbf2371c6, 0x42d1b104, 0x55f9d5c4, 0x6c817884, 0x7ba91c44, + 0x1e702204, 0x095846c4, 0x3020eb84, 0x27088f44, 0xfb929704, + 0xecbaf3c4, 0xd5c25e84, 0xc2ea3a44, 0xa7330404, 0xb01b60c4, + 0x8963cd84, 0x9e4ba944, 0xeb26fb45, 0xfc0e9f85, 0xc57632c5, + 0xd25e5605, 0xb7876845, 0xa0af0c85, 0x99d7a1c5, 0x8effc505, + 0x5265dd45, 0x454db985, 0x7c3514c5, 0x6b1d7005, 0x0ec44e45, + 0x19ec2a85, 0x209487c5, 0x37bce305, 0x4fed41cf, 0x58c5250f, + 0x61bd884f, 0x7695ec8f, 0x134cd2cf, 0x0464b60f, 0x3d1c1b4f, + 0x2a347f8f, 0xf6ae67cf, 0xe186030f, 0xd8feae4f, 0xcfd6ca8f, + 0xaa0ff4cf, 0xbd27900f, 0x845f3d4f, 0x9377598f, 0xe61a0b8e, + 0xf1326f4e, 0xc84ac20e, 0xdf62a6ce, 0xbabb988e, 0xad93fc4e, + 0x94eb510e, 0x83c335ce, 0x5f592d8e, 0x4871494e, 0x7109e40e, + 0x662180ce, 0x03f8be8e, 0x14d0da4e, 0x2da8770e, 0x3a8013ce, + 0xc772d30c, 0xd05ab7cc, 0xe9221a8c, 0xfe0a7e4c, 0x9bd3400c, + 0x8cfb24cc, 0xb583898c, 0xa2abed4c, 0x7e31f50c, 0x691991cc, + 0x50613c8c, 0x4749584c, 0x2290660c, 0x35b802cc, 0x0cc0af8c, + 0x1be8cb4c, 0x6e85994d, 0x79adfd8d, 0x40d550cd, 0x57fd340d, + 0x32240a4d, 0x250c6e8d, 0x1c74c3cd, 0x0b5ca70d, 0xd7c6bf4d, + 0xc0eedb8d, 0xf99676cd, 0xeebe120d, 0x8b672c4d, 0x9c4f488d, + 0xa537e5cd, 0xb21f810d, 0x85a36208, 0x928b06c8, 0xabf3ab88, + 0xbcdbcf48, 0xd902f108, 0xce2a95c8, 0xf7523888, 0xe07a5c48, + 0x3ce04408, 0x2bc820c8, 0x12b08d88, 0x0598e948, 0x6041d708, + 0x7769b3c8, 0x4e111e88, 0x59397a48, 0x2c542849, 0x3b7c4c89, + 0x0204e1c9, 0x152c8509, 0x70f5bb49, 0x67dddf89, 0x5ea572c9, + 0x498d1609, 0x95170e49, 0x823f6a89, 0xbb47c7c9, 0xac6fa309, + 0xc9b69d49, 0xde9ef989, 0xe7e654c9, 0xf0ce3009, 0x0d3cf0cb, + 0x1a14940b, 0x236c394b, 0x34445d8b, 0x519d63cb, 0x46b5070b, + 0x7fcdaa4b, 0x68e5ce8b, 0xb47fd6cb, 0xa357b20b, 0x9a2f1f4b, + 0x8d077b8b, 0xe8de45cb, 0xfff6210b, 0xc68e8c4b, 0xd1a6e88b, + 0xa4cbba8a, 0xb3e3de4a, 0x8a9b730a, 0x9db317ca, 0xf86a298a, + 0xef424d4a, 0xd63ae00a, 0xc11284ca, 0x1d889c8a, 0x0aa0f84a, + 0x33d8550a, 0x24f031ca, 0x41290f8a, 0x56016b4a, 0x6f79c60a, + 0x7851a2ca}, + {0x00000000, 0x9fda839e, 0xe4c4017d, 0x7b1e82e3, 0x12f904bb, + 0x8d238725, 0xf63d05c6, 0x69e78658, 0x25f20976, 0xba288ae8, + 0xc136080b, 0x5eec8b95, 0x370b0dcd, 0xa8d18e53, 0xd3cf0cb0, + 0x4c158f2e, 0x4be412ec, 0xd43e9172, 0xaf201391, 0x30fa900f, + 0x591d1657, 0xc6c795c9, 0xbdd9172a, 0x220394b4, 0x6e161b9a, + 0xf1cc9804, 0x8ad21ae7, 0x15089979, 0x7cef1f21, 0xe3359cbf, + 0x982b1e5c, 0x07f19dc2, 0x97c825d8, 0x0812a646, 0x730c24a5, + 0xecd6a73b, 0x85312163, 0x1aeba2fd, 0x61f5201e, 0xfe2fa380, + 0xb23a2cae, 0x2de0af30, 0x56fe2dd3, 0xc924ae4d, 0xa0c32815, + 0x3f19ab8b, 0x44072968, 0xdbddaaf6, 0xdc2c3734, 0x43f6b4aa, + 0x38e83649, 0xa732b5d7, 0xced5338f, 0x510fb011, 0x2a1132f2, + 0xb5cbb16c, 0xf9de3e42, 0x6604bddc, 0x1d1a3f3f, 0x82c0bca1, + 0xeb273af9, 0x74fdb967, 0x0fe33b84, 0x9039b81a, 0xf4e14df1, + 0x6b3bce6f, 0x10254c8c, 0x8fffcf12, 0xe618494a, 0x79c2cad4, + 0x02dc4837, 0x9d06cba9, 0xd1134487, 0x4ec9c719, 0x35d745fa, + 0xaa0dc664, 0xc3ea403c, 0x5c30c3a2, 0x272e4141, 0xb8f4c2df, + 0xbf055f1d, 0x20dfdc83, 0x5bc15e60, 0xc41bddfe, 0xadfc5ba6, + 0x3226d838, 0x49385adb, 0xd6e2d945, 0x9af7566b, 0x052dd5f5, + 0x7e335716, 0xe1e9d488, 0x880e52d0, 0x17d4d14e, 0x6cca53ad, + 0xf310d033, 0x63296829, 0xfcf3ebb7, 0x87ed6954, 0x1837eaca, + 0x71d06c92, 0xee0aef0c, 0x95146def, 0x0aceee71, 0x46db615f, + 0xd901e2c1, 0xa21f6022, 0x3dc5e3bc, 0x542265e4, 0xcbf8e67a, + 0xb0e66499, 0x2f3ce707, 0x28cd7ac5, 0xb717f95b, 0xcc097bb8, + 0x53d3f826, 0x3a347e7e, 0xa5eefde0, 0xdef07f03, 0x412afc9d, + 0x0d3f73b3, 0x92e5f02d, 0xe9fb72ce, 0x7621f150, 0x1fc67708, + 0x801cf496, 0xfb027675, 0x64d8f5eb, 0x32b39da3, 0xad691e3d, + 0xd6779cde, 0x49ad1f40, 0x204a9918, 0xbf901a86, 0xc48e9865, + 0x5b541bfb, 0x174194d5, 0x889b174b, 0xf38595a8, 0x6c5f1636, + 0x05b8906e, 0x9a6213f0, 0xe17c9113, 0x7ea6128d, 0x79578f4f, + 0xe68d0cd1, 0x9d938e32, 0x02490dac, 0x6bae8bf4, 0xf474086a, + 0x8f6a8a89, 0x10b00917, 0x5ca58639, 0xc37f05a7, 0xb8618744, + 0x27bb04da, 0x4e5c8282, 0xd186011c, 0xaa9883ff, 0x35420061, + 0xa57bb87b, 0x3aa13be5, 0x41bfb906, 0xde653a98, 0xb782bcc0, + 0x28583f5e, 0x5346bdbd, 0xcc9c3e23, 0x8089b10d, 0x1f533293, + 0x644db070, 0xfb9733ee, 0x9270b5b6, 0x0daa3628, 0x76b4b4cb, + 0xe96e3755, 0xee9faa97, 0x71452909, 0x0a5babea, 0x95812874, + 0xfc66ae2c, 0x63bc2db2, 0x18a2af51, 0x87782ccf, 0xcb6da3e1, + 0x54b7207f, 0x2fa9a29c, 0xb0732102, 0xd994a75a, 0x464e24c4, + 0x3d50a627, 0xa28a25b9, 0xc652d052, 0x598853cc, 0x2296d12f, + 0xbd4c52b1, 0xd4abd4e9, 0x4b715777, 0x306fd594, 0xafb5560a, + 0xe3a0d924, 0x7c7a5aba, 0x0764d859, 0x98be5bc7, 0xf159dd9f, + 0x6e835e01, 0x159ddce2, 0x8a475f7c, 0x8db6c2be, 0x126c4120, + 0x6972c3c3, 0xf6a8405d, 0x9f4fc605, 0x0095459b, 0x7b8bc778, + 0xe45144e6, 0xa844cbc8, 0x379e4856, 0x4c80cab5, 0xd35a492b, + 0xbabdcf73, 0x25674ced, 0x5e79ce0e, 0xc1a34d90, 0x519af58a, + 0xce407614, 0xb55ef4f7, 0x2a847769, 0x4363f131, 0xdcb972af, + 0xa7a7f04c, 0x387d73d2, 0x7468fcfc, 0xebb27f62, 0x90acfd81, + 0x0f767e1f, 0x6691f847, 0xf94b7bd9, 0x8255f93a, 0x1d8f7aa4, + 0x1a7ee766, 0x85a464f8, 0xfebae61b, 0x61606585, 0x0887e3dd, + 0x975d6043, 0xec43e2a0, 0x7399613e, 0x3f8cee10, 0xa0566d8e, + 0xdb48ef6d, 0x44926cf3, 0x2d75eaab, 0xb2af6935, 0xc9b1ebd6, + 0x566b6848}}; + +local const z_word_t FAR crc_braid_big_table[][256] = { + {0x00000000, 0x9e83da9f, 0x7d01c4e4, 0xe3821e7b, 0xbb04f912, + 0x2587238d, 0xc6053df6, 0x5886e769, 0x7609f225, 0xe88a28ba, + 0x0b0836c1, 0x958bec5e, 0xcd0d0b37, 0x538ed1a8, 0xb00ccfd3, + 0x2e8f154c, 0xec12e44b, 0x72913ed4, 0x911320af, 0x0f90fa30, + 0x57161d59, 0xc995c7c6, 0x2a17d9bd, 0xb4940322, 0x9a1b166e, + 0x0498ccf1, 0xe71ad28a, 0x79990815, 0x211fef7c, 0xbf9c35e3, + 0x5c1e2b98, 0xc29df107, 0xd825c897, 0x46a61208, 0xa5240c73, + 0x3ba7d6ec, 0x63213185, 0xfda2eb1a, 0x1e20f561, 0x80a32ffe, + 0xae2c3ab2, 0x30afe02d, 0xd32dfe56, 0x4dae24c9, 0x1528c3a0, + 0x8bab193f, 0x68290744, 0xf6aadddb, 0x34372cdc, 0xaab4f643, + 0x4936e838, 0xd7b532a7, 0x8f33d5ce, 0x11b00f51, 0xf232112a, + 0x6cb1cbb5, 0x423edef9, 0xdcbd0466, 0x3f3f1a1d, 0xa1bcc082, + 0xf93a27eb, 0x67b9fd74, 0x843be30f, 0x1ab83990, 0xf14de1f4, + 0x6fce3b6b, 0x8c4c2510, 0x12cfff8f, 0x4a4918e6, 0xd4cac279, + 0x3748dc02, 0xa9cb069d, 0x874413d1, 0x19c7c94e, 0xfa45d735, + 0x64c60daa, 0x3c40eac3, 0xa2c3305c, 0x41412e27, 0xdfc2f4b8, + 0x1d5f05bf, 0x83dcdf20, 0x605ec15b, 0xfedd1bc4, 0xa65bfcad, + 0x38d82632, 0xdb5a3849, 0x45d9e2d6, 0x6b56f79a, 0xf5d52d05, + 0x1657337e, 0x88d4e9e1, 0xd0520e88, 0x4ed1d417, 0xad53ca6c, + 0x33d010f3, 0x29682963, 0xb7ebf3fc, 0x5469ed87, 0xcaea3718, + 0x926cd071, 0x0cef0aee, 0xef6d1495, 0x71eece0a, 0x5f61db46, + 0xc1e201d9, 0x22601fa2, 0xbce3c53d, 0xe4652254, 0x7ae6f8cb, + 0x9964e6b0, 0x07e73c2f, 0xc57acd28, 0x5bf917b7, 0xb87b09cc, + 0x26f8d353, 0x7e7e343a, 0xe0fdeea5, 0x037ff0de, 0x9dfc2a41, + 0xb3733f0d, 0x2df0e592, 0xce72fbe9, 0x50f12176, 0x0877c61f, + 0x96f41c80, 0x757602fb, 0xebf5d864, 0xa39db332, 0x3d1e69ad, + 0xde9c77d6, 0x401fad49, 0x18994a20, 0x861a90bf, 0x65988ec4, + 0xfb1b545b, 0xd5944117, 0x4b179b88, 0xa89585f3, 0x36165f6c, + 0x6e90b805, 0xf013629a, 0x13917ce1, 0x8d12a67e, 0x4f8f5779, + 0xd10c8de6, 0x328e939d, 0xac0d4902, 0xf48bae6b, 0x6a0874f4, + 0x898a6a8f, 0x1709b010, 0x3986a55c, 0xa7057fc3, 0x448761b8, + 0xda04bb27, 0x82825c4e, 0x1c0186d1, 0xff8398aa, 0x61004235, + 0x7bb87ba5, 0xe53ba13a, 0x06b9bf41, 0x983a65de, 0xc0bc82b7, + 0x5e3f5828, 0xbdbd4653, 0x233e9ccc, 0x0db18980, 0x9332531f, + 0x70b04d64, 0xee3397fb, 0xb6b57092, 0x2836aa0d, 0xcbb4b476, + 0x55376ee9, 0x97aa9fee, 0x09294571, 0xeaab5b0a, 0x74288195, + 0x2cae66fc, 0xb22dbc63, 0x51afa218, 0xcf2c7887, 0xe1a36dcb, + 0x7f20b754, 0x9ca2a92f, 0x022173b0, 0x5aa794d9, 0xc4244e46, + 0x27a6503d, 0xb9258aa2, 0x52d052c6, 0xcc538859, 0x2fd19622, + 0xb1524cbd, 0xe9d4abd4, 0x7757714b, 0x94d56f30, 0x0a56b5af, + 0x24d9a0e3, 0xba5a7a7c, 0x59d86407, 0xc75bbe98, 0x9fdd59f1, + 0x015e836e, 0xe2dc9d15, 0x7c5f478a, 0xbec2b68d, 0x20416c12, + 0xc3c37269, 0x5d40a8f6, 0x05c64f9f, 0x9b459500, 0x78c78b7b, + 0xe64451e4, 0xc8cb44a8, 0x56489e37, 0xb5ca804c, 0x2b495ad3, + 0x73cfbdba, 0xed4c6725, 0x0ece795e, 0x904da3c1, 0x8af59a51, + 0x147640ce, 0xf7f45eb5, 0x6977842a, 0x31f16343, 0xaf72b9dc, + 0x4cf0a7a7, 0xd2737d38, 0xfcfc6874, 0x627fb2eb, 0x81fdac90, + 0x1f7e760f, 0x47f89166, 0xd97b4bf9, 0x3af95582, 0xa47a8f1d, + 0x66e77e1a, 0xf864a485, 0x1be6bafe, 0x85656061, 0xdde38708, + 0x43605d97, 0xa0e243ec, 0x3e619973, 0x10ee8c3f, 0x8e6d56a0, + 0x6def48db, 0xf36c9244, 0xabea752d, 0x3569afb2, 0xd6ebb1c9, + 0x48686b56}, + {0x00000000, 0xc0642817, 0x80c9502e, 0x40ad7839, 0x0093a15c, + 0xc0f7894b, 0x805af172, 0x403ed965, 0x002643b9, 0xc0426bae, + 0x80ef1397, 0x408b3b80, 0x00b5e2e5, 0xc0d1caf2, 0x807cb2cb, + 0x40189adc, 0x414af7a9, 0x812edfbe, 0xc183a787, 0x01e78f90, + 0x41d956f5, 0x81bd7ee2, 0xc11006db, 0x01742ecc, 0x416cb410, + 0x81089c07, 0xc1a5e43e, 0x01c1cc29, 0x41ff154c, 0x819b3d5b, + 0xc1364562, 0x01526d75, 0xc3929f88, 0x03f6b79f, 0x435bcfa6, + 0x833fe7b1, 0xc3013ed4, 0x036516c3, 0x43c86efa, 0x83ac46ed, + 0xc3b4dc31, 0x03d0f426, 0x437d8c1f, 0x8319a408, 0xc3277d6d, + 0x0343557a, 0x43ee2d43, 0x838a0554, 0x82d86821, 0x42bc4036, + 0x0211380f, 0xc2751018, 0x824bc97d, 0x422fe16a, 0x02829953, + 0xc2e6b144, 0x82fe2b98, 0x429a038f, 0x02377bb6, 0xc25353a1, + 0x826d8ac4, 0x4209a2d3, 0x02a4daea, 0xc2c0f2fd, 0xc7234eca, + 0x074766dd, 0x47ea1ee4, 0x878e36f3, 0xc7b0ef96, 0x07d4c781, + 0x4779bfb8, 0x871d97af, 0xc7050d73, 0x07612564, 0x47cc5d5d, + 0x87a8754a, 0xc796ac2f, 0x07f28438, 0x475ffc01, 0x873bd416, + 0x8669b963, 0x460d9174, 0x06a0e94d, 0xc6c4c15a, 0x86fa183f, + 0x469e3028, 0x06334811, 0xc6576006, 0x864ffada, 0x462bd2cd, + 0x0686aaf4, 0xc6e282e3, 0x86dc5b86, 0x46b87391, 0x06150ba8, + 0xc67123bf, 0x04b1d142, 0xc4d5f955, 0x8478816c, 0x441ca97b, + 0x0422701e, 0xc4465809, 0x84eb2030, 0x448f0827, 0x049792fb, + 0xc4f3baec, 0x845ec2d5, 0x443aeac2, 0x040433a7, 0xc4601bb0, + 0x84cd6389, 0x44a94b9e, 0x45fb26eb, 0x859f0efc, 0xc53276c5, + 0x05565ed2, 0x456887b7, 0x850cafa0, 0xc5a1d799, 0x05c5ff8e, + 0x45dd6552, 0x85b94d45, 0xc514357c, 0x05701d6b, 0x454ec40e, + 0x852aec19, 0xc5879420, 0x05e3bc37, 0xcf41ed4f, 0x0f25c558, + 0x4f88bd61, 0x8fec9576, 0xcfd24c13, 0x0fb66404, 0x4f1b1c3d, + 0x8f7f342a, 0xcf67aef6, 0x0f0386e1, 0x4faefed8, 0x8fcad6cf, + 0xcff40faa, 0x0f9027bd, 0x4f3d5f84, 0x8f597793, 0x8e0b1ae6, + 0x4e6f32f1, 0x0ec24ac8, 0xcea662df, 0x8e98bbba, 0x4efc93ad, + 0x0e51eb94, 0xce35c383, 0x8e2d595f, 0x4e497148, 0x0ee40971, + 0xce802166, 0x8ebef803, 0x4edad014, 0x0e77a82d, 0xce13803a, + 0x0cd372c7, 0xccb75ad0, 0x8c1a22e9, 0x4c7e0afe, 0x0c40d39b, + 0xcc24fb8c, 0x8c8983b5, 0x4cedaba2, 0x0cf5317e, 0xcc911969, + 0x8c3c6150, 0x4c584947, 0x0c669022, 0xcc02b835, 0x8cafc00c, + 0x4ccbe81b, 0x4d99856e, 0x8dfdad79, 0xcd50d540, 0x0d34fd57, + 0x4d0a2432, 0x8d6e0c25, 0xcdc3741c, 0x0da75c0b, 0x4dbfc6d7, + 0x8ddbeec0, 0xcd7696f9, 0x0d12beee, 0x4d2c678b, 0x8d484f9c, + 0xcde537a5, 0x0d811fb2, 0x0862a385, 0xc8068b92, 0x88abf3ab, + 0x48cfdbbc, 0x08f102d9, 0xc8952ace, 0x883852f7, 0x485c7ae0, + 0x0844e03c, 0xc820c82b, 0x888db012, 0x48e99805, 0x08d74160, + 0xc8b36977, 0x881e114e, 0x487a3959, 0x4928542c, 0x894c7c3b, + 0xc9e10402, 0x09852c15, 0x49bbf570, 0x89dfdd67, 0xc972a55e, + 0x09168d49, 0x490e1795, 0x896a3f82, 0xc9c747bb, 0x09a36fac, + 0x499db6c9, 0x89f99ede, 0xc954e6e7, 0x0930cef0, 0xcbf03c0d, + 0x0b94141a, 0x4b396c23, 0x8b5d4434, 0xcb639d51, 0x0b07b546, + 0x4baacd7f, 0x8bcee568, 0xcbd67fb4, 0x0bb257a3, 0x4b1f2f9a, + 0x8b7b078d, 0xcb45dee8, 0x0b21f6ff, 0x4b8c8ec6, 0x8be8a6d1, + 0x8abacba4, 0x4adee3b3, 0x0a739b8a, 0xca17b39d, 0x8a296af8, + 0x4a4d42ef, 0x0ae03ad6, 0xca8412c1, 0x8a9c881d, 0x4af8a00a, + 0x0a55d833, 0xca31f024, 0x8a0f2941, 0x4a6b0156, 0x0ac6796f, + 0xcaa25178}, + {0x00000000, 0xd4ea739b, 0xe9d396ed, 0x3d39e576, 0x93a15c00, + 0x474b2f9b, 0x7a72caed, 0xae98b976, 0x2643b900, 0xf2a9ca9b, + 0xcf902fed, 0x1b7a5c76, 0xb5e2e500, 0x6108969b, 0x5c3173ed, + 0x88db0076, 0x4c867201, 0x986c019a, 0xa555e4ec, 0x71bf9777, + 0xdf272e01, 0x0bcd5d9a, 0x36f4b8ec, 0xe21ecb77, 0x6ac5cb01, + 0xbe2fb89a, 0x83165dec, 0x57fc2e77, 0xf9649701, 0x2d8ee49a, + 0x10b701ec, 0xc45d7277, 0x980ce502, 0x4ce69699, 0x71df73ef, + 0xa5350074, 0x0badb902, 0xdf47ca99, 0xe27e2fef, 0x36945c74, + 0xbe4f5c02, 0x6aa52f99, 0x579ccaef, 0x8376b974, 0x2dee0002, + 0xf9047399, 0xc43d96ef, 0x10d7e574, 0xd48a9703, 0x0060e498, + 0x3d5901ee, 0xe9b37275, 0x472bcb03, 0x93c1b898, 0xaef85dee, + 0x7a122e75, 0xf2c92e03, 0x26235d98, 0x1b1ab8ee, 0xcff0cb75, + 0x61687203, 0xb5820198, 0x88bbe4ee, 0x5c519775, 0x3019ca05, + 0xe4f3b99e, 0xd9ca5ce8, 0x0d202f73, 0xa3b89605, 0x7752e59e, + 0x4a6b00e8, 0x9e817373, 0x165a7305, 0xc2b0009e, 0xff89e5e8, + 0x2b639673, 0x85fb2f05, 0x51115c9e, 0x6c28b9e8, 0xb8c2ca73, + 0x7c9fb804, 0xa875cb9f, 0x954c2ee9, 0x41a65d72, 0xef3ee404, + 0x3bd4979f, 0x06ed72e9, 0xd2070172, 0x5adc0104, 0x8e36729f, + 0xb30f97e9, 0x67e5e472, 0xc97d5d04, 0x1d972e9f, 0x20aecbe9, + 0xf444b872, 0xa8152f07, 0x7cff5c9c, 0x41c6b9ea, 0x952cca71, + 0x3bb47307, 0xef5e009c, 0xd267e5ea, 0x068d9671, 0x8e569607, + 0x5abce59c, 0x678500ea, 0xb36f7371, 0x1df7ca07, 0xc91db99c, + 0xf4245cea, 0x20ce2f71, 0xe4935d06, 0x30792e9d, 0x0d40cbeb, + 0xd9aab870, 0x77320106, 0xa3d8729d, 0x9ee197eb, 0x4a0be470, + 0xc2d0e406, 0x163a979d, 0x2b0372eb, 0xffe90170, 0x5171b806, + 0x859bcb9d, 0xb8a22eeb, 0x6c485d70, 0x6032940b, 0xb4d8e790, + 0x89e102e6, 0x5d0b717d, 0xf393c80b, 0x2779bb90, 0x1a405ee6, + 0xceaa2d7d, 0x46712d0b, 0x929b5e90, 0xafa2bbe6, 0x7b48c87d, + 0xd5d0710b, 0x013a0290, 0x3c03e7e6, 0xe8e9947d, 0x2cb4e60a, + 0xf85e9591, 0xc56770e7, 0x118d037c, 0xbf15ba0a, 0x6bffc991, + 0x56c62ce7, 0x822c5f7c, 0x0af75f0a, 0xde1d2c91, 0xe324c9e7, + 0x37ceba7c, 0x9956030a, 0x4dbc7091, 0x708595e7, 0xa46fe67c, + 0xf83e7109, 0x2cd40292, 0x11ede7e4, 0xc507947f, 0x6b9f2d09, + 0xbf755e92, 0x824cbbe4, 0x56a6c87f, 0xde7dc809, 0x0a97bb92, + 0x37ae5ee4, 0xe3442d7f, 0x4ddc9409, 0x9936e792, 0xa40f02e4, + 0x70e5717f, 0xb4b80308, 0x60527093, 0x5d6b95e5, 0x8981e67e, + 0x27195f08, 0xf3f32c93, 0xcecac9e5, 0x1a20ba7e, 0x92fbba08, + 0x4611c993, 0x7b282ce5, 0xafc25f7e, 0x015ae608, 0xd5b09593, + 0xe88970e5, 0x3c63037e, 0x502b5e0e, 0x84c12d95, 0xb9f8c8e3, + 0x6d12bb78, 0xc38a020e, 0x17607195, 0x2a5994e3, 0xfeb3e778, + 0x7668e70e, 0xa2829495, 0x9fbb71e3, 0x4b510278, 0xe5c9bb0e, + 0x3123c895, 0x0c1a2de3, 0xd8f05e78, 0x1cad2c0f, 0xc8475f94, + 0xf57ebae2, 0x2194c979, 0x8f0c700f, 0x5be60394, 0x66dfe6e2, + 0xb2359579, 0x3aee950f, 0xee04e694, 0xd33d03e2, 0x07d77079, + 0xa94fc90f, 0x7da5ba94, 0x409c5fe2, 0x94762c79, 0xc827bb0c, + 0x1ccdc897, 0x21f42de1, 0xf51e5e7a, 0x5b86e70c, 0x8f6c9497, + 0xb25571e1, 0x66bf027a, 0xee64020c, 0x3a8e7197, 0x07b794e1, + 0xd35de77a, 0x7dc55e0c, 0xa92f2d97, 0x9416c8e1, 0x40fcbb7a, + 0x84a1c90d, 0x504bba96, 0x6d725fe0, 0xb9982c7b, 0x1700950d, + 0xc3eae696, 0xfed303e0, 0x2a39707b, 0xa2e2700d, 0x76080396, + 0x4b31e6e0, 0x9fdb957b, 0x31432c0d, 0xe5a95f96, 0xd890bae0, + 0x0c7ac97b}, + {0x00000000, 0x27652581, 0x0fcc3bd9, 0x28a91e58, 0x5f9e0669, + 0x78fb23e8, 0x50523db0, 0x77371831, 0xbe3c0dd2, 0x99592853, + 0xb1f0360b, 0x9695138a, 0xe1a20bbb, 0xc6c72e3a, 0xee6e3062, + 0xc90b15e3, 0x3d7f6b7f, 0x1a1a4efe, 0x32b350a6, 0x15d67527, + 0x62e16d16, 0x45844897, 0x6d2d56cf, 0x4a48734e, 0x834366ad, + 0xa426432c, 0x8c8f5d74, 0xabea78f5, 0xdcdd60c4, 0xfbb84545, + 0xd3115b1d, 0xf4747e9c, 0x7afed6fe, 0x5d9bf37f, 0x7532ed27, + 0x5257c8a6, 0x2560d097, 0x0205f516, 0x2aaceb4e, 0x0dc9cecf, + 0xc4c2db2c, 0xe3a7fead, 0xcb0ee0f5, 0xec6bc574, 0x9b5cdd45, + 0xbc39f8c4, 0x9490e69c, 0xb3f5c31d, 0x4781bd81, 0x60e49800, + 0x484d8658, 0x6f28a3d9, 0x181fbbe8, 0x3f7a9e69, 0x17d38031, + 0x30b6a5b0, 0xf9bdb053, 0xded895d2, 0xf6718b8a, 0xd114ae0b, + 0xa623b63a, 0x814693bb, 0xa9ef8de3, 0x8e8aa862, 0xb5fadc26, + 0x929ff9a7, 0xba36e7ff, 0x9d53c27e, 0xea64da4f, 0xcd01ffce, + 0xe5a8e196, 0xc2cdc417, 0x0bc6d1f4, 0x2ca3f475, 0x040aea2d, + 0x236fcfac, 0x5458d79d, 0x733df21c, 0x5b94ec44, 0x7cf1c9c5, + 0x8885b759, 0xafe092d8, 0x87498c80, 0xa02ca901, 0xd71bb130, + 0xf07e94b1, 0xd8d78ae9, 0xffb2af68, 0x36b9ba8b, 0x11dc9f0a, + 0x39758152, 0x1e10a4d3, 0x6927bce2, 0x4e429963, 0x66eb873b, + 0x418ea2ba, 0xcf040ad8, 0xe8612f59, 0xc0c83101, 0xe7ad1480, + 0x909a0cb1, 0xb7ff2930, 0x9f563768, 0xb83312e9, 0x7138070a, + 0x565d228b, 0x7ef43cd3, 0x59911952, 0x2ea60163, 0x09c324e2, + 0x216a3aba, 0x060f1f3b, 0xf27b61a7, 0xd51e4426, 0xfdb75a7e, + 0xdad27fff, 0xade567ce, 0x8a80424f, 0xa2295c17, 0x854c7996, + 0x4c476c75, 0x6b2249f4, 0x438b57ac, 0x64ee722d, 0x13d96a1c, + 0x34bc4f9d, 0x1c1551c5, 0x3b707444, 0x6af5b94d, 0x4d909ccc, + 0x65398294, 0x425ca715, 0x356bbf24, 0x120e9aa5, 0x3aa784fd, + 0x1dc2a17c, 0xd4c9b49f, 0xf3ac911e, 0xdb058f46, 0xfc60aac7, + 0x8b57b2f6, 0xac329777, 0x849b892f, 0xa3feacae, 0x578ad232, + 0x70eff7b3, 0x5846e9eb, 0x7f23cc6a, 0x0814d45b, 0x2f71f1da, + 0x07d8ef82, 0x20bdca03, 0xe9b6dfe0, 0xced3fa61, 0xe67ae439, + 0xc11fc1b8, 0xb628d989, 0x914dfc08, 0xb9e4e250, 0x9e81c7d1, + 0x100b6fb3, 0x376e4a32, 0x1fc7546a, 0x38a271eb, 0x4f9569da, + 0x68f04c5b, 0x40595203, 0x673c7782, 0xae376261, 0x895247e0, + 0xa1fb59b8, 0x869e7c39, 0xf1a96408, 0xd6cc4189, 0xfe655fd1, + 0xd9007a50, 0x2d7404cc, 0x0a11214d, 0x22b83f15, 0x05dd1a94, + 0x72ea02a5, 0x558f2724, 0x7d26397c, 0x5a431cfd, 0x9348091e, + 0xb42d2c9f, 0x9c8432c7, 0xbbe11746, 0xccd60f77, 0xebb32af6, + 0xc31a34ae, 0xe47f112f, 0xdf0f656b, 0xf86a40ea, 0xd0c35eb2, + 0xf7a67b33, 0x80916302, 0xa7f44683, 0x8f5d58db, 0xa8387d5a, + 0x613368b9, 0x46564d38, 0x6eff5360, 0x499a76e1, 0x3ead6ed0, + 0x19c84b51, 0x31615509, 0x16047088, 0xe2700e14, 0xc5152b95, + 0xedbc35cd, 0xcad9104c, 0xbdee087d, 0x9a8b2dfc, 0xb22233a4, + 0x95471625, 0x5c4c03c6, 0x7b292647, 0x5380381f, 0x74e51d9e, + 0x03d205af, 0x24b7202e, 0x0c1e3e76, 0x2b7b1bf7, 0xa5f1b395, + 0x82949614, 0xaa3d884c, 0x8d58adcd, 0xfa6fb5fc, 0xdd0a907d, + 0xf5a38e25, 0xd2c6aba4, 0x1bcdbe47, 0x3ca89bc6, 0x1401859e, + 0x3364a01f, 0x4453b82e, 0x63369daf, 0x4b9f83f7, 0x6cfaa676, + 0x988ed8ea, 0xbfebfd6b, 0x9742e333, 0xb027c6b2, 0xc710de83, + 0xe075fb02, 0xc8dce55a, 0xefb9c0db, 0x26b2d538, 0x01d7f0b9, + 0x297eeee1, 0x0e1bcb60, 0x792cd351, 0x5e49f6d0, 0x76e0e888, + 0x5185cd09}}; + +#endif + +#endif + +#endif + +local const z_crc_t FAR x2n_table[] = { + 0x40000000, 0x20000000, 0x08000000, 0x00800000, 0x00008000, + 0xedb88320, 0xb1e6b092, 0xa06a2517, 0xed627dae, 0x88d14467, + 0xd7bbfe6a, 0xec447f11, 0x8e7ea170, 0x6427800e, 0x4d47bae0, + 0x09fe548f, 0x83852d0f, 0x30362f1a, 0x7b5a9cc3, 0x31fec169, + 0x9fec022a, 0x6c8dedc4, 0x15d6874d, 0x5fde7a4e, 0xbad90e37, + 0x2e4e5eef, 0x4eaba214, 0xa8a472c0, 0x429a969e, 0x148d302a, + 0xc40ba6d0, 0xc4e22c3c}; diff --git a/deps/zlib/deflate.c b/deps/zlib/deflate.c index 5c4022f3d47..012ea8148e8 100644 --- a/deps/zlib/deflate.c +++ b/deps/zlib/deflate.c @@ -1,5 +1,5 @@ /* deflate.c -- compress data using the deflation algorithm - * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler + * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -37,7 +37,7 @@ * REFERENCES * * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". - * Available in http://www.ietf.org/rfc/rfc1951.txt + * Available in http://tools.ietf.org/html/rfc1951 * * A description of the Rabin and Karp algorithm is given in the book * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. @@ -52,7 +52,7 @@ #include "deflate.h" const char deflate_copyright[] = - " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler "; + " deflate 1.3.1 Copyright 1995-2024 Jean-loup Gailly and Mark Adler "; /* If you use the zlib library in a product, an acknowledgment is welcome in the documentation of your product. If for some reason you cannot @@ -60,9 +60,6 @@ const char deflate_copyright[] = copyright string in the executable of your product. */ -/* =========================================================================== - * Function prototypes. - */ typedef enum { need_more, /* block not completed, need more input or more output */ block_done, /* block flush performed */ @@ -70,32 +67,16 @@ typedef enum { finish_done /* finish done, accept no more input or output */ } block_state; -typedef block_state (*compress_func) OF((deflate_state *s, int flush)); +typedef block_state (*compress_func)(deflate_state *s, int flush); /* Compression function. Returns the block state after the call. */ -local void fill_window OF((deflate_state *s)); -local block_state deflate_stored OF((deflate_state *s, int flush)); -local block_state deflate_fast OF((deflate_state *s, int flush)); +local block_state deflate_stored(deflate_state *s, int flush); +local block_state deflate_fast(deflate_state *s, int flush); #ifndef FASTEST -local block_state deflate_slow OF((deflate_state *s, int flush)); -#endif -local block_state deflate_rle OF((deflate_state *s, int flush)); -local block_state deflate_huff OF((deflate_state *s, int flush)); -local void lm_init OF((deflate_state *s)); -local void putShortMSB OF((deflate_state *s, uInt b)); -local void flush_pending OF((z_streamp strm)); -local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); -#ifdef ASMV - void match_init OF((void)); /* asm code initialization */ - uInt longest_match OF((deflate_state *s, IPos cur_match)); -#else -local uInt longest_match OF((deflate_state *s, IPos cur_match)); -#endif - -#ifdef DEBUG -local void check_match OF((deflate_state *s, IPos start, IPos match, - int length)); +local block_state deflate_slow(deflate_state *s, int flush); #endif +local block_state deflate_rle(deflate_state *s, int flush); +local block_state deflate_huff(deflate_state *s, int flush); /* =========================================================================== * Local data @@ -148,20 +129,16 @@ local const config configuration_table[10] = { * meaning. */ -#define EQUAL 0 -/* result of memcmp for equal strings */ - -#ifndef NO_DUMMY_DECL -struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ -#endif +/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */ +#define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0)) /* =========================================================================== * Update a hash value with the given input byte - * IN assertion: all calls to to UPDATE_HASH are made with consecutive - * input characters, so that a running hash key can be computed from the - * previous key instead of complete recalculation each time. + * IN assertion: all calls to UPDATE_HASH are made with consecutive input + * characters, so that a running hash key can be computed from the previous + * key instead of complete recalculation each time. */ -#define UPDATE_HASH(s,h,c) (h = (((h)<hash_shift) ^ (c)) & s->hash_mask) +#define UPDATE_HASH(s,h,c) (h = (((h) << s->hash_shift) ^ (c)) & s->hash_mask) /* =========================================================================== @@ -170,9 +147,9 @@ struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ * the previous length of the hash chain. * If this file is compiled with -DFASTEST, the compression level is forced * to 1, and no hash chains are maintained. - * IN assertion: all calls to to INSERT_STRING are made with consecutive - * input characters and the first MIN_MATCH bytes of str are valid - * (except for the last MIN_MATCH-1 bytes of the input file). + * IN assertion: all calls to INSERT_STRING are made with consecutive input + * characters and the first MIN_MATCH bytes of str are valid (except for + * the last MIN_MATCH-1 bytes of the input file). */ #ifdef FASTEST #define INSERT_STRING(s, str, match_head) \ @@ -191,42 +168,221 @@ struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ * prev[] will be initialized on the fly. */ #define CLEAR_HASH(s) \ - s->head[s->hash_size-1] = NIL; \ - zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); + do { \ + s->head[s->hash_size - 1] = NIL; \ + zmemzero((Bytef *)s->head, \ + (unsigned)(s->hash_size - 1)*sizeof(*s->head)); \ + } while (0) + +/* =========================================================================== + * Slide the hash table when sliding the window down (could be avoided with 32 + * bit values at the expense of memory usage). We slide even when level == 0 to + * keep the hash table consistent if we switch back to level > 0 later. + */ +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) + __attribute__((no_sanitize("memory"))) +# endif +#endif +local void slide_hash(deflate_state *s) { + unsigned n, m; + Posf *p; + uInt wsize = s->w_size; + + n = s->hash_size; + p = &s->head[n]; + do { + m = *--p; + *p = (Pos)(m >= wsize ? m - wsize : NIL); + } while (--n); + n = wsize; +#ifndef FASTEST + p = &s->prev[n]; + do { + m = *--p; + *p = (Pos)(m >= wsize ? m - wsize : NIL); + /* If n is not on any hash chain, prev[n] is garbage but + * its value will never be used. + */ + } while (--n); +#endif +} + +/* =========================================================================== + * Read a new buffer from the current input stream, update the adler32 + * and total number of bytes read. All deflate() input goes through + * this function so some applications may wish to modify it to avoid + * allocating a large strm->next_in buffer and copying from it. + * (See also flush_pending()). + */ +local unsigned read_buf(z_streamp strm, Bytef *buf, unsigned size) { + unsigned len = strm->avail_in; + + if (len > size) len = size; + if (len == 0) return 0; + + strm->avail_in -= len; + + zmemcpy(buf, strm->next_in, len); + if (strm->state->wrap == 1) { + strm->adler = adler32(strm->adler, buf, len); + } +#ifdef GZIP + else if (strm->state->wrap == 2) { + strm->adler = crc32(strm->adler, buf, len); + } +#endif + strm->next_in += len; + strm->total_in += len; + + return len; +} + +/* =========================================================================== + * Fill the window when the lookahead becomes insufficient. + * Updates strstart and lookahead. + * + * IN assertion: lookahead < MIN_LOOKAHEAD + * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD + * At least one byte has been read, or avail_in == 0; reads are + * performed for at least two bytes (required for the zip translate_eol + * option -- not supported here). + */ +local void fill_window(deflate_state *s) { + unsigned n; + unsigned more; /* Amount of free space at the end of the window. */ + uInt wsize = s->w_size; + + Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); + + do { + more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); + + /* Deal with !@#$% 64K limit: */ + if (sizeof(int) <= 2) { + if (more == 0 && s->strstart == 0 && s->lookahead == 0) { + more = wsize; + + } else if (more == (unsigned)(-1)) { + /* Very unlikely, but possible on 16 bit machine if + * strstart == 0 && lookahead == 1 (input done a byte at time) + */ + more--; + } + } + + /* If the window is almost full and there is insufficient lookahead, + * move the upper half to the lower one to make room in the upper half. + */ + if (s->strstart >= wsize + MAX_DIST(s)) { + + zmemcpy(s->window, s->window + wsize, (unsigned)wsize - more); + s->match_start -= wsize; + s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ + s->block_start -= (long) wsize; + if (s->insert > s->strstart) + s->insert = s->strstart; + slide_hash(s); + more += wsize; + } + if (s->strm->avail_in == 0) break; + + /* If there was no sliding: + * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && + * more == window_size - lookahead - strstart + * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) + * => more >= window_size - 2*WSIZE + 2 + * In the BIG_MEM or MMAP case (not yet supported), + * window_size == input_size + MIN_LOOKAHEAD && + * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. + * Otherwise, window_size == 2*WSIZE so more >= 2. + * If there was sliding, more >= WSIZE. So in all cases, more >= 2. + */ + Assert(more >= 2, "more < 2"); + + n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); + s->lookahead += n; + + /* Initialize the hash value now that we have some input: */ + if (s->lookahead + s->insert >= MIN_MATCH) { + uInt str = s->strstart - s->insert; + s->ins_h = s->window[str]; + UPDATE_HASH(s, s->ins_h, s->window[str + 1]); +#if MIN_MATCH != 3 + Call UPDATE_HASH() MIN_MATCH-3 more times +#endif + while (s->insert) { + UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); +#ifndef FASTEST + s->prev[str & s->w_mask] = s->head[s->ins_h]; +#endif + s->head[s->ins_h] = (Pos)str; + str++; + s->insert--; + if (s->lookahead + s->insert < MIN_MATCH) + break; + } + } + /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, + * but this is not important since only literal bytes will be emitted. + */ + + } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); + + /* If the WIN_INIT bytes after the end of the current data have never been + * written, then zero those bytes in order to avoid memory check reports of + * the use of uninitialized (or uninitialised as Julian writes) bytes by + * the longest match routines. Update the high water mark for the next + * time through here. WIN_INIT is set to MAX_MATCH since the longest match + * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. + */ + if (s->high_water < s->window_size) { + ulg curr = s->strstart + (ulg)(s->lookahead); + ulg init; + + if (s->high_water < curr) { + /* Previous high water mark below current data -- zero WIN_INIT + * bytes or up to end of window, whichever is less. + */ + init = s->window_size - curr; + if (init > WIN_INIT) + init = WIN_INIT; + zmemzero(s->window + curr, (unsigned)init); + s->high_water = curr + init; + } + else if (s->high_water < (ulg)curr + WIN_INIT) { + /* High water mark at or above current data, but below current data + * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up + * to end of window, whichever is less. + */ + init = (ulg)curr + WIN_INIT - s->high_water; + if (init > s->window_size - s->high_water) + init = s->window_size - s->high_water; + zmemzero(s->window + s->high_water, (unsigned)init); + s->high_water += init; + } + } + + Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, + "not enough room for search"); +} /* ========================================================================= */ -int ZEXPORT deflateInit_(strm, level, version, stream_size) - z_streamp strm; - int level; - const char *version; - int stream_size; -{ +int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version, + int stream_size) { return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, version, stream_size); /* To do: ignore strm->next_in if we use it as window */ } /* ========================================================================= */ -int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, - version, stream_size) - z_streamp strm; - int level; - int method; - int windowBits; - int memLevel; - int strategy; - const char *version; - int stream_size; -{ +int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, + int windowBits, int memLevel, int strategy, + const char *version, int stream_size) { deflate_state *s; int wrap = 1; static const char my_version[] = ZLIB_VERSION; - ushf *overlay; - /* We overlay pending_buf and d_buf+l_buf. This works since the average - * output size for (length,distance) codes is <= 24 bits. - */ - if (version == Z_NULL || version[0] != my_version[0] || stream_size != sizeof(z_stream)) { return Z_VERSION_ERROR; @@ -235,10 +391,19 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, strm->msg = Z_NULL; if (strm->zalloc == (alloc_func)0) { +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else strm->zalloc = zcalloc; strm->opaque = (voidpf)0; +#endif } - if (strm->zfree == (free_func)0) strm->zfree = zcfree; + if (strm->zfree == (free_func)0) +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zfree = zcfree; +#endif #ifdef FASTEST if (level != 0) level = 1; @@ -248,6 +413,8 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, if (windowBits < 0) { /* suppress zlib wrapper */ wrap = 0; + if (windowBits < -15) + return Z_STREAM_ERROR; windowBits = -windowBits; } #ifdef GZIP @@ -258,7 +425,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, #endif if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || - strategy < 0 || strategy > Z_FIXED) { + strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) { return Z_STREAM_ERROR; } if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ @@ -266,17 +433,18 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, if (s == Z_NULL) return Z_MEM_ERROR; strm->state = (struct internal_state FAR *)s; s->strm = strm; + s->status = INIT_STATE; /* to pass state test in deflateReset() */ s->wrap = wrap; s->gzhead = Z_NULL; - s->w_bits = windowBits; + s->w_bits = (uInt)windowBits; s->w_size = 1 << s->w_bits; s->w_mask = s->w_size - 1; - s->hash_bits = memLevel + 7; + s->hash_bits = (uInt)memLevel + 7; s->hash_size = 1 << s->hash_bits; s->hash_mask = s->hash_size - 1; - s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); + s->hash_shift = ((s->hash_bits + MIN_MATCH-1) / MIN_MATCH); s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); @@ -286,19 +454,67 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ - overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); - s->pending_buf = (uchf *) overlay; - s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); + /* We overlay pending_buf and sym_buf. This works since the average size + * for length/distance pairs over any compressed block is assured to be 31 + * bits or less. + * + * Analysis: The longest fixed codes are a length code of 8 bits plus 5 + * extra bits, for lengths 131 to 257. The longest fixed distance codes are + * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest + * possible fixed-codes length/distance pair is then 31 bits total. + * + * sym_buf starts one-fourth of the way into pending_buf. So there are + * three bytes in sym_buf for every four bytes in pending_buf. Each symbol + * in sym_buf is three bytes -- two for the distance and one for the + * literal/length. As each symbol is consumed, the pointer to the next + * sym_buf value to read moves forward three bytes. From that symbol, up to + * 31 bits are written to pending_buf. The closest the written pending_buf + * bits gets to the next sym_buf symbol to read is just before the last + * code is written. At that time, 31*(n - 2) bits have been written, just + * after 24*(n - 2) bits have been consumed from sym_buf. sym_buf starts at + * 8*n bits into pending_buf. (Note that the symbol buffer fills when n - 1 + * symbols are written.) The closest the writing gets to what is unread is + * then n + 14 bits. Here n is lit_bufsize, which is 16384 by default, and + * can range from 128 to 32768. + * + * Therefore, at a minimum, there are 142 bits of space between what is + * written and what is read in the overlain buffers, so the symbols cannot + * be overwritten by the compressed data. That space is actually 139 bits, + * due to the three-bit fixed-code block header. + * + * That covers the case where either Z_FIXED is specified, forcing fixed + * codes, or when the use of fixed codes is chosen, because that choice + * results in a smaller compressed block than dynamic codes. That latter + * condition then assures that the above analysis also covers all dynamic + * blocks. A dynamic-code block will only be chosen to be emitted if it has + * fewer bits than a fixed-code block would for the same set of symbols. + * Therefore its average symbol length is assured to be less than 31. So + * the compressed data for a dynamic block also cannot overwrite the + * symbols from which it is being constructed. + */ + + s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, LIT_BUFS); + s->pending_buf_size = (ulg)s->lit_bufsize * 4; if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || s->pending_buf == Z_NULL) { s->status = FINISH_STATE; - strm->msg = (char*)ERR_MSG(Z_MEM_ERROR); + strm->msg = ERR_MSG(Z_MEM_ERROR); deflateEnd (strm); return Z_MEM_ERROR; } - s->d_buf = overlay + s->lit_bufsize/sizeof(ush); - s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; +#ifdef LIT_MEM + s->d_buf = (ushf *)(s->pending_buf + (s->lit_bufsize << 1)); + s->l_buf = s->pending_buf + (s->lit_bufsize << 2); + s->sym_end = s->lit_bufsize - 1; +#else + s->sym_buf = s->pending_buf + s->lit_bufsize; + s->sym_end = (s->lit_bufsize - 1) * 3; +#endif + /* We avoid equality with lit_bufsize*3 because of wraparound at 64K + * on 16 bit machines and because stored blocks are restricted to + * 64K-1 bytes. + */ s->level = level; s->strategy = strategy; @@ -307,56 +523,119 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, return deflateReset(strm); } +/* ========================================================================= + * Check for a valid deflate stream state. Return 0 if ok, 1 if not. + */ +local int deflateStateCheck(z_streamp strm) { + deflate_state *s; + if (strm == Z_NULL || + strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) + return 1; + s = strm->state; + if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE && +#ifdef GZIP + s->status != GZIP_STATE && +#endif + s->status != EXTRA_STATE && + s->status != NAME_STATE && + s->status != COMMENT_STATE && + s->status != HCRC_STATE && + s->status != BUSY_STATE && + s->status != FINISH_STATE)) + return 1; + return 0; +} + /* ========================================================================= */ -int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) - z_streamp strm; - const Bytef *dictionary; - uInt dictLength; -{ +int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary, + uInt dictLength) { deflate_state *s; - uInt length = dictLength; - uInt n; - IPos hash_head = 0; + uInt str, n; + int wrap; + unsigned avail; + z_const unsigned char *next; - if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL || - strm->state->wrap == 2 || - (strm->state->wrap == 1 && strm->state->status != INIT_STATE)) + if (deflateStateCheck(strm) || dictionary == Z_NULL) return Z_STREAM_ERROR; - s = strm->state; - if (s->wrap) - strm->adler = adler32(strm->adler, dictionary, dictLength); + wrap = s->wrap; + if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) + return Z_STREAM_ERROR; - if (length < MIN_MATCH) return Z_OK; - if (length > s->w_size) { - length = s->w_size; - dictionary += dictLength - length; /* use the tail of the dictionary */ + /* when using zlib wrappers, compute Adler-32 for provided dictionary */ + if (wrap == 1) + strm->adler = adler32(strm->adler, dictionary, dictLength); + s->wrap = 0; /* avoid computing Adler-32 in read_buf */ + + /* if dictionary would fill window, just replace the history */ + if (dictLength >= s->w_size) { + if (wrap == 0) { /* already empty otherwise */ + CLEAR_HASH(s); + s->strstart = 0; + s->block_start = 0L; + s->insert = 0; + } + dictionary += dictLength - s->w_size; /* use the tail */ + dictLength = s->w_size; } - zmemcpy(s->window, dictionary, length); - s->strstart = length; - s->block_start = (long)length; - /* Insert all strings in the hash table (except for the last two bytes). - * s->lookahead stays null, so s->ins_h will be recomputed at the next - * call of fill_window. - */ - s->ins_h = s->window[0]; - UPDATE_HASH(s, s->ins_h, s->window[1]); - for (n = 0; n <= length - MIN_MATCH; n++) { - INSERT_STRING(s, n, hash_head); + /* insert dictionary into window and hash */ + avail = strm->avail_in; + next = strm->next_in; + strm->avail_in = dictLength; + strm->next_in = (z_const Bytef *)dictionary; + fill_window(s); + while (s->lookahead >= MIN_MATCH) { + str = s->strstart; + n = s->lookahead - (MIN_MATCH-1); + do { + UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); +#ifndef FASTEST + s->prev[str & s->w_mask] = s->head[s->ins_h]; +#endif + s->head[s->ins_h] = (Pos)str; + str++; + } while (--n); + s->strstart = str; + s->lookahead = MIN_MATCH-1; + fill_window(s); } - if (hash_head) hash_head = 0; /* to make compiler happy */ + s->strstart += s->lookahead; + s->block_start = (long)s->strstart; + s->insert = s->lookahead; + s->lookahead = 0; + s->match_length = s->prev_length = MIN_MATCH-1; + s->match_available = 0; + strm->next_in = next; + strm->avail_in = avail; + s->wrap = wrap; + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflateGetDictionary(z_streamp strm, Bytef *dictionary, + uInt *dictLength) { + deflate_state *s; + uInt len; + + if (deflateStateCheck(strm)) + return Z_STREAM_ERROR; + s = strm->state; + len = s->strstart + s->lookahead; + if (len > s->w_size) + len = s->w_size; + if (dictionary != Z_NULL && len) + zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len); + if (dictLength != Z_NULL) + *dictLength = len; return Z_OK; } /* ========================================================================= */ -int ZEXPORT deflateReset (strm) - z_streamp strm; -{ +int ZEXPORT deflateResetKeep(z_streamp strm) { deflate_state *s; - if (strm == Z_NULL || strm->state == Z_NULL || - strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { + if (deflateStateCheck(strm)) { return Z_STREAM_ERROR; } @@ -371,54 +650,110 @@ int ZEXPORT deflateReset (strm) if (s->wrap < 0) { s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ } - s->status = s->wrap ? INIT_STATE : BUSY_STATE; + s->status = +#ifdef GZIP + s->wrap == 2 ? GZIP_STATE : +#endif + INIT_STATE; strm->adler = #ifdef GZIP s->wrap == 2 ? crc32(0L, Z_NULL, 0) : #endif adler32(0L, Z_NULL, 0); - s->last_flush = Z_NO_FLUSH; + s->last_flush = -2; _tr_init(s); - lm_init(s); return Z_OK; } +/* =========================================================================== + * Initialize the "longest match" routines for a new zlib stream + */ +local void lm_init(deflate_state *s) { + s->window_size = (ulg)2L*s->w_size; + + CLEAR_HASH(s); + + /* Set the default configuration parameters: + */ + s->max_lazy_match = configuration_table[s->level].max_lazy; + s->good_match = configuration_table[s->level].good_length; + s->nice_match = configuration_table[s->level].nice_length; + s->max_chain_length = configuration_table[s->level].max_chain; + + s->strstart = 0; + s->block_start = 0L; + s->lookahead = 0; + s->insert = 0; + s->match_length = s->prev_length = MIN_MATCH-1; + s->match_available = 0; + s->ins_h = 0; +} + +/* ========================================================================= */ +int ZEXPORT deflateReset(z_streamp strm) { + int ret; + + ret = deflateResetKeep(strm); + if (ret == Z_OK) + lm_init(strm->state); + return ret; +} + /* ========================================================================= */ -int ZEXPORT deflateSetHeader (strm, head) - z_streamp strm; - gz_headerp head; -{ - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; - if (strm->state->wrap != 2) return Z_STREAM_ERROR; +int ZEXPORT deflateSetHeader(z_streamp strm, gz_headerp head) { + if (deflateStateCheck(strm) || strm->state->wrap != 2) + return Z_STREAM_ERROR; strm->state->gzhead = head; return Z_OK; } /* ========================================================================= */ -int ZEXPORT deflatePrime (strm, bits, value) - z_streamp strm; - int bits; - int value; -{ - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; - strm->state->bi_valid = bits; - strm->state->bi_buf = (ush)(value & ((1 << bits) - 1)); +int ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits) { + if (deflateStateCheck(strm)) return Z_STREAM_ERROR; + if (pending != Z_NULL) + *pending = strm->state->pending; + if (bits != Z_NULL) + *bits = strm->state->bi_valid; return Z_OK; } /* ========================================================================= */ -int ZEXPORT deflateParams(strm, level, strategy) - z_streamp strm; - int level; - int strategy; -{ +int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) { + deflate_state *s; + int put; + + if (deflateStateCheck(strm)) return Z_STREAM_ERROR; + s = strm->state; +#ifdef LIT_MEM + if (bits < 0 || bits > 16 || + (uchf *)s->d_buf < s->pending_out + ((Buf_size + 7) >> 3)) + return Z_BUF_ERROR; +#else + if (bits < 0 || bits > 16 || + s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3)) + return Z_BUF_ERROR; +#endif + do { + put = Buf_size - s->bi_valid; + if (put > bits) + put = bits; + s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid); + s->bi_valid += put; + _tr_flush_bits(s); + value >>= put; + bits -= put; + } while (bits); + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflateParams(z_streamp strm, int level, int strategy) { deflate_state *s; compress_func func; - int err = Z_OK; - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (deflateStateCheck(strm)) return Z_STREAM_ERROR; s = strm->state; #ifdef FASTEST @@ -432,11 +767,22 @@ int ZEXPORT deflateParams(strm, level, strategy) func = configuration_table[s->level].func; if ((strategy != s->strategy || func != configuration_table[level].func) && - strm->total_in != 0) { + s->last_flush != -2) { /* Flush the last buffer: */ - err = deflate(strm, Z_BLOCK); + int err = deflate(strm, Z_BLOCK); + if (err == Z_STREAM_ERROR) + return err; + if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead) + return Z_BUF_ERROR; } if (s->level != level) { + if (s->level == 0 && s->matches != 0) { + if (s->matches == 1) + slide_hash(s); + else + CLEAR_HASH(s); + s->matches = 0; + } s->level = level; s->max_lazy_match = configuration_table[level].max_lazy; s->good_match = configuration_table[level].good_length; @@ -444,60 +790,65 @@ int ZEXPORT deflateParams(strm, level, strategy) s->max_chain_length = configuration_table[level].max_chain; } s->strategy = strategy; - return err; + return Z_OK; } /* ========================================================================= */ -int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) - z_streamp strm; - int good_length; - int max_lazy; - int nice_length; - int max_chain; -{ +int ZEXPORT deflateTune(z_streamp strm, int good_length, int max_lazy, + int nice_length, int max_chain) { deflate_state *s; - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (deflateStateCheck(strm)) return Z_STREAM_ERROR; s = strm->state; - s->good_match = good_length; - s->max_lazy_match = max_lazy; + s->good_match = (uInt)good_length; + s->max_lazy_match = (uInt)max_lazy; s->nice_match = nice_length; - s->max_chain_length = max_chain; + s->max_chain_length = (uInt)max_chain; return Z_OK; } /* ========================================================================= - * For the default windowBits of 15 and memLevel of 8, this function returns - * a close to exact, as well as small, upper bound on the compressed size. - * They are coded as constants here for a reason--if the #define's are - * changed, then this function needs to be changed as well. The return - * value for 15 and 8 only works for those exact settings. + * For the default windowBits of 15 and memLevel of 8, this function returns a + * close to exact, as well as small, upper bound on the compressed size. This + * is an expansion of ~0.03%, plus a small constant. * - * For any setting other than those defaults for windowBits and memLevel, - * the value returned is a conservative worst case for the maximum expansion - * resulting from using fixed blocks instead of stored blocks, which deflate - * can emit on compressed data for some combinations of the parameters. + * For any setting other than those defaults for windowBits and memLevel, one + * of two worst case bounds is returned. This is at most an expansion of ~4% or + * ~13%, plus a small constant. * - * This function could be more sophisticated to provide closer upper bounds for - * every combination of windowBits and memLevel. But even the conservative - * upper bound of about 14% expansion does not seem onerous for output buffer - * allocation. + * Both the 0.03% and 4% derive from the overhead of stored blocks. The first + * one is for stored blocks of 16383 bytes (memLevel == 8), whereas the second + * is for stored blocks of 127 bytes (the worst case memLevel == 1). The + * expansion results from five bytes of header for each stored block. + * + * The larger expansion of 13% results from a window size less than or equal to + * the symbols buffer size (windowBits <= memLevel + 7). In that case some of + * the data being compressed may have slid out of the sliding window, impeding + * a stored block from being emitted. Then the only choice is a fixed or + * dynamic block, where a fixed block limits the maximum expansion to 9 bits + * per 8-bit byte, plus 10 bits for every block. The smallest block size for + * which this can occur is 255 (memLevel == 2). + * + * Shifts are used to approximate divisions, for speed. */ -uLong ZEXPORT deflateBound(strm, sourceLen) - z_streamp strm; - uLong sourceLen; -{ +uLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen) { deflate_state *s; - uLong complen, wraplen; - Bytef *str; + uLong fixedlen, storelen, wraplen; + + /* upper bound for fixed blocks with 9-bit literals and length 255 + (memLevel == 2, which is the lowest that may not use stored blocks) -- + ~13% overhead plus a small constant */ + fixedlen = sourceLen + (sourceLen >> 3) + (sourceLen >> 8) + + (sourceLen >> 9) + 4; - /* conservative upper bound for compressed data */ - complen = sourceLen + - ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5; + /* upper bound for stored blocks with length 127 (memLevel == 1) -- + ~4% overhead plus a small constant */ + storelen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) + + (sourceLen >> 11) + 7; - /* if can't get parameters, return conservative bound plus zlib wrapper */ - if (strm == Z_NULL || strm->state == Z_NULL) - return complen + 6; + /* if can't get parameters, return larger bound plus a zlib wrapper */ + if (deflateStateCheck(strm)) + return (fixedlen > storelen ? fixedlen : storelen) + 6; /* compute wrapper length */ s = strm->state; @@ -508,9 +859,11 @@ uLong ZEXPORT deflateBound(strm, sourceLen) case 1: /* zlib wrapper */ wraplen = 6 + (s->strstart ? 4 : 0); break; +#ifdef GZIP case 2: /* gzip wrapper */ wraplen = 18; if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ + Bytef *str; if (s->gzhead->extra != Z_NULL) wraplen += 2 + s->gzhead->extra_len; str = s->gzhead->name; @@ -527,15 +880,18 @@ uLong ZEXPORT deflateBound(strm, sourceLen) wraplen += 2; } break; +#endif default: /* for compiler happiness */ wraplen = 6; } - /* if not default parameters, return conservative bound */ + /* if not default parameters, return one of the conservative bounds */ if (s->w_bits != 15 || s->hash_bits != 8 + 7) - return complen + wraplen; + return (s->w_bits <= s->hash_bits && s->level ? fixedlen : storelen) + + wraplen; - /* default settings: return tight bound for that case */ + /* default settings: return tight bound for that case -- ~0.03% overhead + plus a small constant */ return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + (sourceLen >> 25) + 13 - 6 + wraplen; } @@ -545,271 +901,277 @@ uLong ZEXPORT deflateBound(strm, sourceLen) * IN assertion: the stream state is correct and there is enough room in * pending_buf. */ -local void putShortMSB (s, b) - deflate_state *s; - uInt b; -{ +local void putShortMSB(deflate_state *s, uInt b) { put_byte(s, (Byte)(b >> 8)); put_byte(s, (Byte)(b & 0xff)); } /* ========================================================================= - * Flush as much pending output as possible. All deflate() output goes - * through this function so some applications may wish to modify it - * to avoid allocating a large strm->next_out buffer and copying into it. - * (See also read_buf()). + * Flush as much pending output as possible. All deflate() output, except for + * some deflate_stored() output, goes through this function so some + * applications may wish to modify it to avoid allocating a large + * strm->next_out buffer and copying into it. (See also read_buf()). */ -local void flush_pending(strm) - z_streamp strm; -{ - unsigned len = strm->state->pending; +local void flush_pending(z_streamp strm) { + unsigned len; + deflate_state *s = strm->state; + _tr_flush_bits(s); + len = s->pending; if (len > strm->avail_out) len = strm->avail_out; if (len == 0) return; - zmemcpy(strm->next_out, strm->state->pending_out, len); + zmemcpy(strm->next_out, s->pending_out, len); strm->next_out += len; - strm->state->pending_out += len; + s->pending_out += len; strm->total_out += len; - strm->avail_out -= len; - strm->state->pending -= len; - if (strm->state->pending == 0) { - strm->state->pending_out = strm->state->pending_buf; + strm->avail_out -= len; + s->pending -= len; + if (s->pending == 0) { + s->pending_out = s->pending_buf; } } +/* =========================================================================== + * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1]. + */ +#define HCRC_UPDATE(beg) \ + do { \ + if (s->gzhead->hcrc && s->pending > (beg)) \ + strm->adler = crc32(strm->adler, s->pending_buf + (beg), \ + s->pending - (beg)); \ + } while (0) + /* ========================================================================= */ -int ZEXPORT deflate (strm, flush) - z_streamp strm; - int flush; -{ +int ZEXPORT deflate(z_streamp strm, int flush) { int old_flush; /* value of flush param for previous deflate call */ deflate_state *s; - if (strm == Z_NULL || strm->state == Z_NULL || - flush > Z_BLOCK || flush < 0) { + if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) { return Z_STREAM_ERROR; } s = strm->state; if (strm->next_out == Z_NULL || - (strm->next_in == Z_NULL && strm->avail_in != 0) || + (strm->avail_in != 0 && strm->next_in == Z_NULL) || (s->status == FINISH_STATE && flush != Z_FINISH)) { ERR_RETURN(strm, Z_STREAM_ERROR); } if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); - s->strm = strm; /* just in case */ old_flush = s->last_flush; s->last_flush = flush; + /* Flush as much pending output as possible */ + if (s->pending != 0) { + flush_pending(strm); + if (strm->avail_out == 0) { + /* Since avail_out is 0, deflate will be called again with + * more output space, but possibly with both pending and + * avail_in equal to zero. There won't be anything to do, + * but this is not an error situation so make sure we + * return OK instead of BUF_ERROR at next call of deflate: + */ + s->last_flush = -1; + return Z_OK; + } + + /* Make sure there is something to do and avoid duplicate consecutive + * flushes. For repeated and useless calls with Z_FINISH, we keep + * returning Z_STREAM_END instead of Z_BUF_ERROR. + */ + } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && + flush != Z_FINISH) { + ERR_RETURN(strm, Z_BUF_ERROR); + } + + /* User must not provide more input after the first FINISH: */ + if (s->status == FINISH_STATE && strm->avail_in != 0) { + ERR_RETURN(strm, Z_BUF_ERROR); + } + /* Write the header */ + if (s->status == INIT_STATE && s->wrap == 0) + s->status = BUSY_STATE; if (s->status == INIT_STATE) { -#ifdef GZIP - if (s->wrap == 2) { - strm->adler = crc32(0L, Z_NULL, 0); - put_byte(s, 31); - put_byte(s, 139); - put_byte(s, 8); - if (s->gzhead == Z_NULL) { - put_byte(s, 0); - put_byte(s, 0); - put_byte(s, 0); - put_byte(s, 0); - put_byte(s, 0); - put_byte(s, s->level == 9 ? 2 : - (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? - 4 : 0)); - put_byte(s, OS_CODE); - s->status = BUSY_STATE; - } - else { - put_byte(s, (s->gzhead->text ? 1 : 0) + - (s->gzhead->hcrc ? 2 : 0) + - (s->gzhead->extra == Z_NULL ? 0 : 4) + - (s->gzhead->name == Z_NULL ? 0 : 8) + - (s->gzhead->comment == Z_NULL ? 0 : 16) - ); - put_byte(s, (Byte)(s->gzhead->time & 0xff)); - put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); - put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); - put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); - put_byte(s, s->level == 9 ? 2 : - (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? - 4 : 0)); - put_byte(s, s->gzhead->os & 0xff); - if (s->gzhead->extra != Z_NULL) { - put_byte(s, s->gzhead->extra_len & 0xff); - put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); - } - if (s->gzhead->hcrc) - strm->adler = crc32(strm->adler, s->pending_buf, - s->pending); - s->gzindex = 0; - s->status = EXTRA_STATE; - } - } + /* zlib header */ + uInt header = (Z_DEFLATED + ((s->w_bits - 8) << 4)) << 8; + uInt level_flags; + + if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) + level_flags = 0; + else if (s->level < 6) + level_flags = 1; + else if (s->level == 6) + level_flags = 2; else -#endif - { - uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; - uInt level_flags; - - if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) - level_flags = 0; - else if (s->level < 6) - level_flags = 1; - else if (s->level == 6) - level_flags = 2; - else - level_flags = 3; - header |= (level_flags << 6); - if (s->strstart != 0) header |= PRESET_DICT; - header += 31 - (header % 31); + level_flags = 3; + header |= (level_flags << 6); + if (s->strstart != 0) header |= PRESET_DICT; + header += 31 - (header % 31); + + putShortMSB(s, header); + /* Save the adler32 of the preset dictionary: */ + if (s->strstart != 0) { + putShortMSB(s, (uInt)(strm->adler >> 16)); + putShortMSB(s, (uInt)(strm->adler & 0xffff)); + } + strm->adler = adler32(0L, Z_NULL, 0); + s->status = BUSY_STATE; + + /* Compression must start with an empty pending buffer */ + flush_pending(strm); + if (s->pending != 0) { + s->last_flush = -1; + return Z_OK; + } + } +#ifdef GZIP + if (s->status == GZIP_STATE) { + /* gzip header */ + strm->adler = crc32(0L, Z_NULL, 0); + put_byte(s, 31); + put_byte(s, 139); + put_byte(s, 8); + if (s->gzhead == Z_NULL) { + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, s->level == 9 ? 2 : + (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? + 4 : 0)); + put_byte(s, OS_CODE); s->status = BUSY_STATE; - putShortMSB(s, header); - /* Save the adler32 of the preset dictionary: */ - if (s->strstart != 0) { - putShortMSB(s, (uInt)(strm->adler >> 16)); - putShortMSB(s, (uInt)(strm->adler & 0xffff)); + /* Compression must start with an empty pending buffer */ + flush_pending(strm); + if (s->pending != 0) { + s->last_flush = -1; + return Z_OK; } - strm->adler = adler32(0L, Z_NULL, 0); + } + else { + put_byte(s, (s->gzhead->text ? 1 : 0) + + (s->gzhead->hcrc ? 2 : 0) + + (s->gzhead->extra == Z_NULL ? 0 : 4) + + (s->gzhead->name == Z_NULL ? 0 : 8) + + (s->gzhead->comment == Z_NULL ? 0 : 16) + ); + put_byte(s, (Byte)(s->gzhead->time & 0xff)); + put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); + put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); + put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); + put_byte(s, s->level == 9 ? 2 : + (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? + 4 : 0)); + put_byte(s, s->gzhead->os & 0xff); + if (s->gzhead->extra != Z_NULL) { + put_byte(s, s->gzhead->extra_len & 0xff); + put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); + } + if (s->gzhead->hcrc) + strm->adler = crc32(strm->adler, s->pending_buf, + s->pending); + s->gzindex = 0; + s->status = EXTRA_STATE; } } -#ifdef GZIP if (s->status == EXTRA_STATE) { if (s->gzhead->extra != Z_NULL) { - uInt beg = s->pending; /* start of bytes to update crc */ - - while (s->gzindex < (s->gzhead->extra_len & 0xffff)) { - if (s->pending == s->pending_buf_size) { - if (s->gzhead->hcrc && s->pending > beg) - strm->adler = crc32(strm->adler, s->pending_buf + beg, - s->pending - beg); - flush_pending(strm); - beg = s->pending; - if (s->pending == s->pending_buf_size) - break; + ulg beg = s->pending; /* start of bytes to update crc */ + uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex; + while (s->pending + left > s->pending_buf_size) { + uInt copy = s->pending_buf_size - s->pending; + zmemcpy(s->pending_buf + s->pending, + s->gzhead->extra + s->gzindex, copy); + s->pending = s->pending_buf_size; + HCRC_UPDATE(beg); + s->gzindex += copy; + flush_pending(strm); + if (s->pending != 0) { + s->last_flush = -1; + return Z_OK; } - put_byte(s, s->gzhead->extra[s->gzindex]); - s->gzindex++; - } - if (s->gzhead->hcrc && s->pending > beg) - strm->adler = crc32(strm->adler, s->pending_buf + beg, - s->pending - beg); - if (s->gzindex == s->gzhead->extra_len) { - s->gzindex = 0; - s->status = NAME_STATE; + beg = 0; + left -= copy; } + zmemcpy(s->pending_buf + s->pending, + s->gzhead->extra + s->gzindex, left); + s->pending += left; + HCRC_UPDATE(beg); + s->gzindex = 0; } - else - s->status = NAME_STATE; + s->status = NAME_STATE; } if (s->status == NAME_STATE) { if (s->gzhead->name != Z_NULL) { - uInt beg = s->pending; /* start of bytes to update crc */ + ulg beg = s->pending; /* start of bytes to update crc */ int val; - do { if (s->pending == s->pending_buf_size) { - if (s->gzhead->hcrc && s->pending > beg) - strm->adler = crc32(strm->adler, s->pending_buf + beg, - s->pending - beg); + HCRC_UPDATE(beg); flush_pending(strm); - beg = s->pending; - if (s->pending == s->pending_buf_size) { - val = 1; - break; + if (s->pending != 0) { + s->last_flush = -1; + return Z_OK; } + beg = 0; } val = s->gzhead->name[s->gzindex++]; put_byte(s, val); } while (val != 0); - if (s->gzhead->hcrc && s->pending > beg) - strm->adler = crc32(strm->adler, s->pending_buf + beg, - s->pending - beg); - if (val == 0) { - s->gzindex = 0; - s->status = COMMENT_STATE; - } + HCRC_UPDATE(beg); + s->gzindex = 0; } - else - s->status = COMMENT_STATE; + s->status = COMMENT_STATE; } if (s->status == COMMENT_STATE) { if (s->gzhead->comment != Z_NULL) { - uInt beg = s->pending; /* start of bytes to update crc */ + ulg beg = s->pending; /* start of bytes to update crc */ int val; - do { if (s->pending == s->pending_buf_size) { - if (s->gzhead->hcrc && s->pending > beg) - strm->adler = crc32(strm->adler, s->pending_buf + beg, - s->pending - beg); + HCRC_UPDATE(beg); flush_pending(strm); - beg = s->pending; - if (s->pending == s->pending_buf_size) { - val = 1; - break; + if (s->pending != 0) { + s->last_flush = -1; + return Z_OK; } + beg = 0; } val = s->gzhead->comment[s->gzindex++]; put_byte(s, val); } while (val != 0); - if (s->gzhead->hcrc && s->pending > beg) - strm->adler = crc32(strm->adler, s->pending_buf + beg, - s->pending - beg); - if (val == 0) - s->status = HCRC_STATE; + HCRC_UPDATE(beg); } - else - s->status = HCRC_STATE; + s->status = HCRC_STATE; } if (s->status == HCRC_STATE) { if (s->gzhead->hcrc) { - if (s->pending + 2 > s->pending_buf_size) + if (s->pending + 2 > s->pending_buf_size) { flush_pending(strm); - if (s->pending + 2 <= s->pending_buf_size) { - put_byte(s, (Byte)(strm->adler & 0xff)); - put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); - strm->adler = crc32(0L, Z_NULL, 0); - s->status = BUSY_STATE; + if (s->pending != 0) { + s->last_flush = -1; + return Z_OK; + } } + put_byte(s, (Byte)(strm->adler & 0xff)); + put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); + strm->adler = crc32(0L, Z_NULL, 0); } - else - s->status = BUSY_STATE; - } -#endif + s->status = BUSY_STATE; - /* Flush as much pending output as possible */ - if (s->pending != 0) { + /* Compression must start with an empty pending buffer */ flush_pending(strm); - if (strm->avail_out == 0) { - /* Since avail_out is 0, deflate will be called again with - * more output space, but possibly with both pending and - * avail_in equal to zero. There won't be anything to do, - * but this is not an error situation so make sure we - * return OK instead of BUF_ERROR at next call of deflate: - */ + if (s->pending != 0) { s->last_flush = -1; return Z_OK; } - - /* Make sure there is something to do and avoid duplicate consecutive - * flushes. For repeated and useless calls with Z_FINISH, we keep - * returning Z_STREAM_END instead of Z_BUF_ERROR. - */ - } else if (strm->avail_in == 0 && flush <= old_flush && - flush != Z_FINISH) { - ERR_RETURN(strm, Z_BUF_ERROR); - } - - /* User must not provide more input after the first FINISH: */ - if (s->status == FINISH_STATE && strm->avail_in != 0) { - ERR_RETURN(strm, Z_BUF_ERROR); } +#endif /* Start a new block or continue the current one. */ @@ -817,9 +1179,10 @@ int ZEXPORT deflate (strm, flush) (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { block_state bstate; - bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : - (s->strategy == Z_RLE ? deflate_rle(s, flush) : - (*(configuration_table[s->level].func))(s, flush)); + bstate = s->level == 0 ? deflate_stored(s, flush) : + s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : + s->strategy == Z_RLE ? deflate_rle(s, flush) : + (*(configuration_table[s->level].func))(s, flush); if (bstate == finish_started || bstate == finish_done) { s->status = FINISH_STATE; @@ -850,6 +1213,7 @@ int ZEXPORT deflate (strm, flush) if (s->lookahead == 0) { s->strstart = 0; s->block_start = 0L; + s->insert = 0; } } } @@ -860,7 +1224,6 @@ int ZEXPORT deflate (strm, flush) } } } - Assert(strm->avail_out > 0, "bug2"); if (flush != Z_FINISH) return Z_OK; if (s->wrap <= 0) return Z_STREAM_END; @@ -892,23 +1255,12 @@ int ZEXPORT deflate (strm, flush) } /* ========================================================================= */ -int ZEXPORT deflateEnd (strm) - z_streamp strm; -{ +int ZEXPORT deflateEnd(z_streamp strm) { int status; - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (deflateStateCheck(strm)) return Z_STREAM_ERROR; status = strm->state->status; - if (status != INIT_STATE && - status != EXTRA_STATE && - status != NAME_STATE && - status != COMMENT_STATE && - status != HCRC_STATE && - status != BUSY_STATE && - status != FINISH_STATE) { - return Z_STREAM_ERROR; - } /* Deallocate in reverse order of allocations: */ TRY_FREE(strm, strm->state->pending_buf); @@ -927,37 +1279,34 @@ int ZEXPORT deflateEnd (strm) * To simplify the source, this is not supported for 16-bit MSDOS (which * doesn't have enough memory anyway to duplicate compression states). */ -int ZEXPORT deflateCopy (dest, source) - z_streamp dest; - z_streamp source; -{ +int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) { #ifdef MAXSEG_64K + (void)dest; + (void)source; return Z_STREAM_ERROR; #else deflate_state *ds; deflate_state *ss; - ushf *overlay; - if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { + if (deflateStateCheck(source) || dest == Z_NULL) { return Z_STREAM_ERROR; } ss = source->state; - zmemcpy(dest, source, sizeof(z_stream)); + zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); if (ds == Z_NULL) return Z_MEM_ERROR; dest->state = (struct internal_state FAR *) ds; - zmemcpy(ds, ss, sizeof(deflate_state)); + zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state)); ds->strm = dest; ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); - overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); - ds->pending_buf = (uchf *) overlay; + ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, LIT_BUFS); if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || ds->pending_buf == Z_NULL) { @@ -966,13 +1315,17 @@ int ZEXPORT deflateCopy (dest, source) } /* following zmemcpy do not work for 16-bit MSDOS */ zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); - zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos)); - zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos)); - zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); + zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos)); + zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos)); + zmemcpy(ds->pending_buf, ss->pending_buf, ds->lit_bufsize * LIT_BUFS); ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); - ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); - ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; +#ifdef LIT_MEM + ds->d_buf = (ushf *)(ds->pending_buf + (ds->lit_bufsize << 1)); + ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2); +#else + ds->sym_buf = ds->pending_buf + ds->lit_bufsize; +#endif ds->l_desc.dyn_tree = ds->dyn_ltree; ds->d_desc.dyn_tree = ds->dyn_dtree; @@ -982,70 +1335,6 @@ int ZEXPORT deflateCopy (dest, source) #endif /* MAXSEG_64K */ } -/* =========================================================================== - * Read a new buffer from the current input stream, update the adler32 - * and total number of bytes read. All deflate() input goes through - * this function so some applications may wish to modify it to avoid - * allocating a large strm->next_in buffer and copying from it. - * (See also flush_pending()). - */ -local int read_buf(strm, buf, size) - z_streamp strm; - Bytef *buf; - unsigned size; -{ - unsigned len = strm->avail_in; - - if (len > size) len = size; - if (len == 0) return 0; - - strm->avail_in -= len; - - if (strm->state->wrap == 1) { - strm->adler = adler32(strm->adler, strm->next_in, len); - } -#ifdef GZIP - else if (strm->state->wrap == 2) { - strm->adler = crc32(strm->adler, strm->next_in, len); - } -#endif - zmemcpy(buf, strm->next_in, len); - strm->next_in += len; - strm->total_in += len; - - return (int)len; -} - -/* =========================================================================== - * Initialize the "longest match" routines for a new zlib stream - */ -local void lm_init (s) - deflate_state *s; -{ - s->window_size = (ulg)2L*s->w_size; - - CLEAR_HASH(s); - - /* Set the default configuration parameters: - */ - s->max_lazy_match = configuration_table[s->level].max_lazy; - s->good_match = configuration_table[s->level].good_length; - s->nice_match = configuration_table[s->level].nice_length; - s->max_chain_length = configuration_table[s->level].max_chain; - - s->strstart = 0; - s->block_start = 0L; - s->lookahead = 0; - s->match_length = s->prev_length = MIN_MATCH-1; - s->match_available = 0; - s->ins_h = 0; -#ifndef FASTEST -#ifdef ASMV - match_init(); /* initialize the asm code */ -#endif -#endif -} - #ifndef FASTEST /* =========================================================================== * Set match_start to the longest match starting at the given string and @@ -1056,19 +1345,12 @@ local void lm_init (s) * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 * OUT assertion: the match length is not greater than s->lookahead. */ -#ifndef ASMV -/* For 80x86 and 680x0, an optimized version will be provided in match.asm or - * match.S. The code will be functionally equivalent. - */ -local uInt longest_match(s, cur_match) - deflate_state *s; - IPos cur_match; /* current match */ -{ +local uInt longest_match(deflate_state *s, IPos cur_match) { unsigned chain_length = s->max_chain_length;/* max hash chain length */ register Bytef *scan = s->window + s->strstart; /* current string */ - register Bytef *match; /* matched string */ + register Bytef *match; /* matched string */ register int len; /* length of current match */ - int best_len = s->prev_length; /* best match length so far */ + int best_len = (int)s->prev_length; /* best match length so far */ int nice_match = s->nice_match; /* stop if match long enough */ IPos limit = s->strstart > (IPos)MAX_DIST(s) ? s->strstart - (IPos)MAX_DIST(s) : NIL; @@ -1084,10 +1366,10 @@ local uInt longest_match(s, cur_match) */ register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; register ush scan_start = *(ushf*)scan; - register ush scan_end = *(ushf*)(scan+best_len-1); + register ush scan_end = *(ushf*)(scan + best_len - 1); #else register Bytef *strend = s->window + s->strstart + MAX_MATCH; - register Byte scan_end1 = scan[best_len-1]; + register Byte scan_end1 = scan[best_len - 1]; register Byte scan_end = scan[best_len]; #endif @@ -1103,9 +1385,10 @@ local uInt longest_match(s, cur_match) /* Do not look for matches beyond the end of the input. This is necessary * to make deflate deterministic. */ - if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; + if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead; - Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); + Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, + "need lookahead"); do { Assert(cur_match < s->strstart, "no future"); @@ -1123,43 +1406,44 @@ local uInt longest_match(s, cur_match) /* This code assumes sizeof(unsigned short) == 2. Do not use * UNALIGNED_OK if your compiler uses a different size. */ - if (*(ushf*)(match+best_len-1) != scan_end || + if (*(ushf*)(match + best_len - 1) != scan_end || *(ushf*)match != scan_start) continue; /* It is not necessary to compare scan[2] and match[2] since they are * always equal when the other bytes match, given that the hash keys * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at - * strstart+3, +5, ... up to strstart+257. We check for insufficient + * strstart + 3, + 5, up to strstart + 257. We check for insufficient * lookahead only every 4th comparison; the 128th check will be made - * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is + * at strstart + 257. If MAX_MATCH-2 is not a multiple of 8, it is * necessary to put more guard bytes at the end of the window, or * to check more often for insufficient lookahead. */ Assert(scan[2] == match[2], "scan[2]?"); scan++, match++; do { - } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) && - *(ushf*)(scan+=2) == *(ushf*)(match+=2) && - *(ushf*)(scan+=2) == *(ushf*)(match+=2) && - *(ushf*)(scan+=2) == *(ushf*)(match+=2) && + } while (*(ushf*)(scan += 2) == *(ushf*)(match += 2) && + *(ushf*)(scan += 2) == *(ushf*)(match += 2) && + *(ushf*)(scan += 2) == *(ushf*)(match += 2) && + *(ushf*)(scan += 2) == *(ushf*)(match += 2) && scan < strend); /* The funny "do {}" generates better code on most compilers */ - /* Here, scan <= window+strstart+257 */ - Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + /* Here, scan <= window + strstart + 257 */ + Assert(scan <= s->window + (unsigned)(s->window_size - 1), + "wild scan"); if (*scan == *match) scan++; - len = (MAX_MATCH - 1) - (int)(strend-scan); + len = (MAX_MATCH - 1) - (int)(strend - scan); scan = strend - (MAX_MATCH-1); #else /* UNALIGNED_OK */ - if (match[best_len] != scan_end || - match[best_len-1] != scan_end1 || - *match != *scan || - *++match != scan[1]) continue; + if (match[best_len] != scan_end || + match[best_len - 1] != scan_end1 || + *match != *scan || + *++match != scan[1]) continue; - /* The check at best_len-1 can be removed because it will be made + /* The check at best_len - 1 can be removed because it will be made * again later. (This heuristic is not always a win.) * It is not necessary to compare scan[2] and match[2] since they * are always equal when the other bytes match, given that @@ -1169,7 +1453,7 @@ local uInt longest_match(s, cur_match) Assert(*scan == *match, "match[2]?"); /* We check for insufficient lookahead only every 8th comparison; - * the 256th check will be made at strstart+258. + * the 256th check will be made at strstart + 258. */ do { } while (*++scan == *++match && *++scan == *++match && @@ -1178,7 +1462,8 @@ local uInt longest_match(s, cur_match) *++scan == *++match && *++scan == *++match && scan < strend); - Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + Assert(scan <= s->window + (unsigned)(s->window_size - 1), + "wild scan"); len = MAX_MATCH - (int)(strend - scan); scan = strend - MAX_MATCH; @@ -1190,9 +1475,9 @@ local uInt longest_match(s, cur_match) best_len = len; if (len >= nice_match) break; #ifdef UNALIGNED_OK - scan_end = *(ushf*)(scan+best_len-1); + scan_end = *(ushf*)(scan + best_len - 1); #else - scan_end1 = scan[best_len-1]; + scan_end1 = scan[best_len - 1]; scan_end = scan[best_len]; #endif } @@ -1202,17 +1487,13 @@ local uInt longest_match(s, cur_match) if ((uInt)best_len <= s->lookahead) return (uInt)best_len; return s->lookahead; } -#endif /* ASMV */ #else /* FASTEST */ /* --------------------------------------------------------------------------- * Optimized version for FASTEST only */ -local uInt longest_match(s, cur_match) - deflate_state *s; - IPos cur_match; /* current match */ -{ +local uInt longest_match(deflate_state *s, IPos cur_match) { register Bytef *scan = s->window + s->strstart; /* current string */ register Bytef *match; /* matched string */ register int len; /* length of current match */ @@ -1223,7 +1504,8 @@ local uInt longest_match(s, cur_match) */ Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); - Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); + Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, + "need lookahead"); Assert(cur_match < s->strstart, "no future"); @@ -1233,7 +1515,7 @@ local uInt longest_match(s, cur_match) */ if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; - /* The check at best_len-1 can be removed because it will be made + /* The check at best_len - 1 can be removed because it will be made * again later. (This heuristic is not always a win.) * It is not necessary to compare scan[2] and match[2] since they * are always equal when the other bytes match, given that @@ -1243,7 +1525,7 @@ local uInt longest_match(s, cur_match) Assert(*scan == *match, "match[2]?"); /* We check for insufficient lookahead only every 8th comparison; - * the 256th check will be made at strstart+258. + * the 256th check will be made at strstart + 258. */ do { } while (*++scan == *++match && *++scan == *++match && @@ -1252,7 +1534,7 @@ local uInt longest_match(s, cur_match) *++scan == *++match && *++scan == *++match && scan < strend); - Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + Assert(scan <= s->window + (unsigned)(s->window_size - 1), "wild scan"); len = MAX_MATCH - (int)(strend - scan); @@ -1264,170 +1546,41 @@ local uInt longest_match(s, cur_match) #endif /* FASTEST */ -#ifdef DEBUG +#ifdef ZLIB_DEBUG + +#define EQUAL 0 +/* result of memcmp for equal strings */ + /* =========================================================================== * Check that the match at match_start is indeed a match. */ -local void check_match(s, start, match, length) - deflate_state *s; - IPos start, match; - int length; -{ +local void check_match(deflate_state *s, IPos start, IPos match, int length) { /* check that the match is indeed a match */ - if (zmemcmp(s->window + match, - s->window + start, length) != EQUAL) { - fprintf(stderr, " start %u, match %u, length %d\n", - start, match, length); + Bytef *back = s->window + (int)match, *here = s->window + start; + IPos len = length; + if (match == (IPos)-1) { + /* match starts one byte before the current window -- just compare the + subsequent length-1 bytes */ + back++; + here++; + len--; + } + if (zmemcmp(back, here, len) != EQUAL) { + fprintf(stderr, " start %u, match %d, length %d\n", + start, (int)match, length); do { - fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); - } while (--length != 0); + fprintf(stderr, "(%02x %02x)", *back++, *here++); + } while (--len != 0); z_error("invalid match"); } if (z_verbose > 1) { - fprintf(stderr,"\\[%d,%d]", start-match, length); + fprintf(stderr,"\\[%d,%d]", start - match, length); do { putc(s->window[start++], stderr); } while (--length != 0); } } #else # define check_match(s, start, match, length) -#endif /* DEBUG */ - -/* =========================================================================== - * Fill the window when the lookahead becomes insufficient. - * Updates strstart and lookahead. - * - * IN assertion: lookahead < MIN_LOOKAHEAD - * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD - * At least one byte has been read, or avail_in == 0; reads are - * performed for at least two bytes (required for the zip translate_eol - * option -- not supported here). - */ -local void fill_window(s) - deflate_state *s; -{ - register unsigned n, m; - register Posf *p; - unsigned more; /* Amount of free space at the end of the window. */ - uInt wsize = s->w_size; - - do { - more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); - - /* Deal with !@#$% 64K limit: */ - if (sizeof(int) <= 2) { - if (more == 0 && s->strstart == 0 && s->lookahead == 0) { - more = wsize; - - } else if (more == (unsigned)(-1)) { - /* Very unlikely, but possible on 16 bit machine if - * strstart == 0 && lookahead == 1 (input done a byte at time) - */ - more--; - } - } - - /* If the window is almost full and there is insufficient lookahead, - * move the upper half to the lower one to make room in the upper half. - */ - if (s->strstart >= wsize+MAX_DIST(s)) { - - zmemcpy(s->window, s->window+wsize, (unsigned)wsize); - s->match_start -= wsize; - s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ - s->block_start -= (long) wsize; - - /* Slide the hash table (could be avoided with 32 bit values - at the expense of memory usage). We slide even when level == 0 - to keep the hash table consistent if we switch back to level > 0 - later. (Using level 0 permanently is not an optimal usage of - zlib, so we don't care about this pathological case.) - */ - n = s->hash_size; - p = &s->head[n]; - do { - m = *--p; - *p = (Pos)(m >= wsize ? m-wsize : NIL); - } while (--n); - - n = wsize; -#ifndef FASTEST - p = &s->prev[n]; - do { - m = *--p; - *p = (Pos)(m >= wsize ? m-wsize : NIL); - /* If n is not on any hash chain, prev[n] is garbage but - * its value will never be used. - */ - } while (--n); -#endif - more += wsize; - } - if (s->strm->avail_in == 0) return; - - /* If there was no sliding: - * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && - * more == window_size - lookahead - strstart - * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) - * => more >= window_size - 2*WSIZE + 2 - * In the BIG_MEM or MMAP case (not yet supported), - * window_size == input_size + MIN_LOOKAHEAD && - * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. - * Otherwise, window_size == 2*WSIZE so more >= 2. - * If there was sliding, more >= WSIZE. So in all cases, more >= 2. - */ - Assert(more >= 2, "more < 2"); - - n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); - s->lookahead += n; - - /* Initialize the hash value now that we have some input: */ - if (s->lookahead >= MIN_MATCH) { - s->ins_h = s->window[s->strstart]; - UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); -#if MIN_MATCH != 3 - Call UPDATE_HASH() MIN_MATCH-3 more times -#endif - } - /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, - * but this is not important since only literal bytes will be emitted. - */ - - } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); - - /* If the WIN_INIT bytes after the end of the current data have never been - * written, then zero those bytes in order to avoid memory check reports of - * the use of uninitialized (or uninitialised as Julian writes) bytes by - * the longest match routines. Update the high water mark for the next - * time through here. WIN_INIT is set to MAX_MATCH since the longest match - * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. - */ - if (s->high_water < s->window_size) { - ulg curr = s->strstart + (ulg)(s->lookahead); - ulg init; - - if (s->high_water < curr) { - /* Previous high water mark below current data -- zero WIN_INIT - * bytes or up to end of window, whichever is less. - */ - init = s->window_size - curr; - if (init > WIN_INIT) - init = WIN_INIT; - zmemzero(s->window + curr, (unsigned)init); - s->high_water = curr + init; - } - else if (s->high_water < (ulg)curr + WIN_INIT) { - /* High water mark at or above current data, but below current data - * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up - * to end of window, whichever is less. - */ - init = (ulg)curr + WIN_INIT - s->high_water; - if (init > s->window_size - s->high_water) - init = s->window_size - s->high_water; - zmemzero(s->window + s->high_water, (unsigned)init); - s->high_water += init; - } - } -} +#endif /* ZLIB_DEBUG */ /* =========================================================================== * Flush the current block, with given end-of-file flag. @@ -1450,64 +1603,202 @@ local void fill_window(s) if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ } +/* Maximum stored block length in deflate format (not including header). */ +#define MAX_STORED 65535 + +/* Minimum of a and b. */ +#define MIN(a, b) ((a) > (b) ? (b) : (a)) + /* =========================================================================== * Copy without compression as much as possible from the input stream, return * the current block state. - * This function does not insert new strings in the dictionary since - * uncompressible data is probably not useful. This function is used - * only for the level=0 compression option. - * NOTE: this function should be optimized to avoid extra copying from - * window to pending_buf. + * + * In case deflateParams() is used to later switch to a non-zero compression + * level, s->matches (otherwise unused when storing) keeps track of the number + * of hash table slides to perform. If s->matches is 1, then one hash table + * slide will be done when switching. If s->matches is 2, the maximum value + * allowed here, then the hash table will be cleared, since two or more slides + * is the same as a clear. + * + * deflate_stored() is written to minimize the number of times an input byte is + * copied. It is most efficient with large input and output buffers, which + * maximizes the opportunities to have a single copy from next_in to next_out. */ -local block_state deflate_stored(s, flush) - deflate_state *s; - int flush; -{ - /* Stored blocks are limited to 0xffff bytes, pending_buf is limited - * to pending_buf_size, and each stored block has a 5 byte header: +local block_state deflate_stored(deflate_state *s, int flush) { + /* Smallest worthy block size when not flushing or finishing. By default + * this is 32K. This can be as small as 507 bytes for memLevel == 1. For + * large input and output buffers, the stored block size will be larger. */ - ulg max_block_size = 0xffff; - ulg max_start; + unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size); - if (max_block_size > s->pending_buf_size - 5) { - max_block_size = s->pending_buf_size - 5; - } - - /* Copy as much as possible from input to output: */ - for (;;) { - /* Fill the window as much as possible: */ - if (s->lookahead <= 1) { - - Assert(s->strstart < s->w_size+MAX_DIST(s) || - s->block_start >= (long)s->w_size, "slide too late"); + /* Copy as many min_block or larger stored blocks directly to next_out as + * possible. If flushing, copy the remaining available input to next_out as + * stored blocks, if there is enough space. + */ + unsigned len, left, have, last = 0; + unsigned used = s->strm->avail_in; + do { + /* Set len to the maximum size block that we can copy directly with the + * available input data and output space. Set left to how much of that + * would be copied from what's left in the window. + */ + len = MAX_STORED; /* maximum deflate stored block length */ + have = (s->bi_valid + 42) >> 3; /* number of header bytes */ + if (s->strm->avail_out < have) /* need room for header */ + break; + /* maximum stored block length that will fit in avail_out: */ + have = s->strm->avail_out - have; + left = s->strstart - s->block_start; /* bytes left in window */ + if (len > (ulg)left + s->strm->avail_in) + len = left + s->strm->avail_in; /* limit len to the input */ + if (len > have) + len = have; /* limit len to the output */ + + /* If the stored block would be less than min_block in length, or if + * unable to copy all of the available input when flushing, then try + * copying to the window and the pending buffer instead. Also don't + * write an empty block when flushing -- deflate() does that. + */ + if (len < min_block && ((len == 0 && flush != Z_FINISH) || + flush == Z_NO_FLUSH || + len != left + s->strm->avail_in)) + break; - fill_window(s); - if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more; + /* Make a dummy stored block in pending to get the header bytes, + * including any pending bits. This also updates the debugging counts. + */ + last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0; + _tr_stored_block(s, (char *)0, 0L, last); + + /* Replace the lengths in the dummy stored block with len. */ + s->pending_buf[s->pending - 4] = len; + s->pending_buf[s->pending - 3] = len >> 8; + s->pending_buf[s->pending - 2] = ~len; + s->pending_buf[s->pending - 1] = ~len >> 8; + + /* Write the stored block header bytes. */ + flush_pending(s->strm); + +#ifdef ZLIB_DEBUG + /* Update debugging counts for the data about to be copied. */ + s->compressed_len += len << 3; + s->bits_sent += len << 3; +#endif - if (s->lookahead == 0) break; /* flush the current block */ + /* Copy uncompressed bytes from the window to next_out. */ + if (left) { + if (left > len) + left = len; + zmemcpy(s->strm->next_out, s->window + s->block_start, left); + s->strm->next_out += left; + s->strm->avail_out -= left; + s->strm->total_out += left; + s->block_start += left; + len -= left; } - Assert(s->block_start >= 0L, "block gone"); - - s->strstart += s->lookahead; - s->lookahead = 0; - - /* Emit a stored block if pending_buf will be full: */ - max_start = s->block_start + max_block_size; - if (s->strstart == 0 || (ulg)s->strstart >= max_start) { - /* strstart == 0 is possible when wraparound on 16-bit machine */ - s->lookahead = (uInt)(s->strstart - max_start); - s->strstart = (uInt)max_start; - FLUSH_BLOCK(s, 0); + + /* Copy uncompressed bytes directly from next_in to next_out, updating + * the check value. + */ + if (len) { + read_buf(s->strm, s->strm->next_out, len); + s->strm->next_out += len; + s->strm->avail_out -= len; + s->strm->total_out += len; } - /* Flush if we may have to slide, otherwise block_start may become - * negative and the data will be gone: + } while (last == 0); + + /* Update the sliding window with the last s->w_size bytes of the copied + * data, or append all of the copied data to the existing window if less + * than s->w_size bytes were copied. Also update the number of bytes to + * insert in the hash tables, in the event that deflateParams() switches to + * a non-zero compression level. + */ + used -= s->strm->avail_in; /* number of input bytes directly copied */ + if (used) { + /* If any input was used, then no unused input remains in the window, + * therefore s->block_start == s->strstart. */ - if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) { - FLUSH_BLOCK(s, 0); + if (used >= s->w_size) { /* supplant the previous history */ + s->matches = 2; /* clear hash */ + zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size); + s->strstart = s->w_size; + s->insert = s->strstart; } + else { + if (s->window_size - s->strstart <= used) { + /* Slide the window down. */ + s->strstart -= s->w_size; + zmemcpy(s->window, s->window + s->w_size, s->strstart); + if (s->matches < 2) + s->matches++; /* add a pending slide_hash() */ + if (s->insert > s->strstart) + s->insert = s->strstart; + } + zmemcpy(s->window + s->strstart, s->strm->next_in - used, used); + s->strstart += used; + s->insert += MIN(used, s->w_size - s->insert); + } + s->block_start = s->strstart; + } + if (s->high_water < s->strstart) + s->high_water = s->strstart; + + /* If the last block was written to next_out, then done. */ + if (last) + return finish_done; + + /* If flushing and all input has been consumed, then done. */ + if (flush != Z_NO_FLUSH && flush != Z_FINISH && + s->strm->avail_in == 0 && (long)s->strstart == s->block_start) + return block_done; + + /* Fill the window with any remaining input. */ + have = s->window_size - s->strstart; + if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) { + /* Slide the window down. */ + s->block_start -= s->w_size; + s->strstart -= s->w_size; + zmemcpy(s->window, s->window + s->w_size, s->strstart); + if (s->matches < 2) + s->matches++; /* add a pending slide_hash() */ + have += s->w_size; /* more space now */ + if (s->insert > s->strstart) + s->insert = s->strstart; + } + if (have > s->strm->avail_in) + have = s->strm->avail_in; + if (have) { + read_buf(s->strm, s->window + s->strstart, have); + s->strstart += have; + s->insert += MIN(have, s->w_size - s->insert); } - FLUSH_BLOCK(s, flush == Z_FINISH); - return flush == Z_FINISH ? finish_done : block_done; + if (s->high_water < s->strstart) + s->high_water = s->strstart; + + /* There was not enough avail_out to write a complete worthy or flushed + * stored block to next_out. Write a stored block to pending instead, if we + * have enough input for a worthy block, or if flushing and there is enough + * room for the remaining input as a stored block in the pending buffer. + */ + have = (s->bi_valid + 42) >> 3; /* number of header bytes */ + /* maximum stored block length that will fit in pending: */ + have = MIN(s->pending_buf_size - have, MAX_STORED); + min_block = MIN(have, s->w_size); + left = s->strstart - s->block_start; + if (left >= min_block || + ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH && + s->strm->avail_in == 0 && left <= have)) { + len = MIN(left, have); + last = flush == Z_FINISH && s->strm->avail_in == 0 && + len == left ? 1 : 0; + _tr_stored_block(s, (charf *)s->window + s->block_start, len, last); + s->block_start += len; + flush_pending(s->strm); + } + + /* We've done all we can with the available input and output. */ + return last ? finish_started : need_more; } /* =========================================================================== @@ -1517,10 +1808,7 @@ local block_state deflate_stored(s, flush) * new strings in the dictionary only for unmatched strings or for short * matches. It is used only for the fast compression options. */ -local block_state deflate_fast(s, flush) - deflate_state *s; - int flush; -{ +local block_state deflate_fast(deflate_state *s, int flush) { IPos hash_head; /* head of the hash chain */ int bflush; /* set if current block must be flushed */ @@ -1538,7 +1826,7 @@ local block_state deflate_fast(s, flush) if (s->lookahead == 0) break; /* flush the current block */ } - /* Insert the string window[strstart .. strstart+2] in the + /* Insert the string window[strstart .. strstart + 2] in the * dictionary, and set hash_head to the head of the hash chain: */ hash_head = NIL; @@ -1586,7 +1874,7 @@ local block_state deflate_fast(s, flush) s->strstart += s->match_length; s->match_length = 0; s->ins_h = s->window[s->strstart]; - UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); + UPDATE_HASH(s, s->ins_h, s->window[s->strstart + 1]); #if MIN_MATCH != 3 Call UPDATE_HASH() MIN_MATCH-3 more times #endif @@ -1597,14 +1885,20 @@ local block_state deflate_fast(s, flush) } else { /* No match, output a literal byte */ Tracevv((stderr,"%c", s->window[s->strstart])); - _tr_tally_lit (s, s->window[s->strstart], bflush); + _tr_tally_lit(s, s->window[s->strstart], bflush); s->lookahead--; s->strstart++; } if (bflush) FLUSH_BLOCK(s, 0); } - FLUSH_BLOCK(s, flush == Z_FINISH); - return flush == Z_FINISH ? finish_done : block_done; + s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; + if (flush == Z_FINISH) { + FLUSH_BLOCK(s, 1); + return finish_done; + } + if (s->sym_next) + FLUSH_BLOCK(s, 0); + return block_done; } #ifndef FASTEST @@ -1613,10 +1907,7 @@ local block_state deflate_fast(s, flush) * evaluation for matches: a match is finally adopted only if there is * no better match at the next window position. */ -local block_state deflate_slow(s, flush) - deflate_state *s; - int flush; -{ +local block_state deflate_slow(deflate_state *s, int flush) { IPos hash_head; /* head of hash chain */ int bflush; /* set if current block must be flushed */ @@ -1635,7 +1926,7 @@ local block_state deflate_slow(s, flush) if (s->lookahead == 0) break; /* flush the current block */ } - /* Insert the string window[strstart .. strstart+2] in the + /* Insert the string window[strstart .. strstart + 2] in the * dictionary, and set hash_head to the head of the hash chain: */ hash_head = NIL; @@ -1677,17 +1968,17 @@ local block_state deflate_slow(s, flush) uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; /* Do not insert strings in hash table beyond this. */ - check_match(s, s->strstart-1, s->prev_match, s->prev_length); + check_match(s, s->strstart - 1, s->prev_match, s->prev_length); - _tr_tally_dist(s, s->strstart -1 - s->prev_match, + _tr_tally_dist(s, s->strstart - 1 - s->prev_match, s->prev_length - MIN_MATCH, bflush); /* Insert in hash table all strings up to the end of the match. - * strstart-1 and strstart are already inserted. If there is not + * strstart - 1 and strstart are already inserted. If there is not * enough lookahead, the last two strings are not inserted in * the hash table. */ - s->lookahead -= s->prev_length-1; + s->lookahead -= s->prev_length - 1; s->prev_length -= 2; do { if (++s->strstart <= max_insert) { @@ -1705,8 +1996,8 @@ local block_state deflate_slow(s, flush) * single literal. If there was a match but the current match * is longer, truncate the previous match to a single literal. */ - Tracevv((stderr,"%c", s->window[s->strstart-1])); - _tr_tally_lit(s, s->window[s->strstart-1], bflush); + Tracevv((stderr,"%c", s->window[s->strstart - 1])); + _tr_tally_lit(s, s->window[s->strstart - 1], bflush); if (bflush) { FLUSH_BLOCK_ONLY(s, 0); } @@ -1724,12 +2015,18 @@ local block_state deflate_slow(s, flush) } Assert (flush != Z_NO_FLUSH, "no flush?"); if (s->match_available) { - Tracevv((stderr,"%c", s->window[s->strstart-1])); - _tr_tally_lit(s, s->window[s->strstart-1], bflush); + Tracevv((stderr,"%c", s->window[s->strstart - 1])); + _tr_tally_lit(s, s->window[s->strstart - 1], bflush); s->match_available = 0; } - FLUSH_BLOCK(s, flush == Z_FINISH); - return flush == Z_FINISH ? finish_done : block_done; + s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; + if (flush == Z_FINISH) { + FLUSH_BLOCK(s, 1); + return finish_done; + } + if (s->sym_next) + FLUSH_BLOCK(s, 0); + return block_done; } #endif /* FASTEST */ @@ -1738,10 +2035,7 @@ local block_state deflate_slow(s, flush) * one. Do not maintain a hash table. (It will be regenerated if this run of * deflate switches away from Z_RLE.) */ -local block_state deflate_rle(s, flush) - deflate_state *s; - int flush; -{ +local block_state deflate_rle(deflate_state *s, int flush) { int bflush; /* set if current block must be flushed */ uInt prev; /* byte at distance one to match */ Bytef *scan, *strend; /* scan goes up to strend for length of run */ @@ -1749,11 +2043,11 @@ local block_state deflate_rle(s, flush) for (;;) { /* Make sure that we always have enough lookahead, except * at the end of the input file. We need MAX_MATCH bytes - * for the longest encodable run. + * for the longest run, plus one for the unrolled loop. */ - if (s->lookahead < MAX_MATCH) { + if (s->lookahead <= MAX_MATCH) { fill_window(s); - if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) { + if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) { return need_more; } if (s->lookahead == 0) break; /* flush the current block */ @@ -1772,10 +2066,12 @@ local block_state deflate_rle(s, flush) prev == *++scan && prev == *++scan && prev == *++scan && prev == *++scan && scan < strend); - s->match_length = MAX_MATCH - (int)(strend - scan); + s->match_length = MAX_MATCH - (uInt)(strend - scan); if (s->match_length > s->lookahead) s->match_length = s->lookahead; } + Assert(scan <= s->window + (uInt)(s->window_size - 1), + "wild scan"); } /* Emit match if have run of MIN_MATCH or longer, else emit literal */ @@ -1790,24 +2086,27 @@ local block_state deflate_rle(s, flush) } else { /* No match, output a literal byte */ Tracevv((stderr,"%c", s->window[s->strstart])); - _tr_tally_lit (s, s->window[s->strstart], bflush); + _tr_tally_lit(s, s->window[s->strstart], bflush); s->lookahead--; s->strstart++; } if (bflush) FLUSH_BLOCK(s, 0); } - FLUSH_BLOCK(s, flush == Z_FINISH); - return flush == Z_FINISH ? finish_done : block_done; + s->insert = 0; + if (flush == Z_FINISH) { + FLUSH_BLOCK(s, 1); + return finish_done; + } + if (s->sym_next) + FLUSH_BLOCK(s, 0); + return block_done; } /* =========================================================================== * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. * (It will be regenerated if this run of deflate switches away from Huffman.) */ -local block_state deflate_huff(s, flush) - deflate_state *s; - int flush; -{ +local block_state deflate_huff(deflate_state *s, int flush) { int bflush; /* set if current block must be flushed */ for (;;) { @@ -1824,11 +2123,17 @@ local block_state deflate_huff(s, flush) /* Output a literal byte */ s->match_length = 0; Tracevv((stderr,"%c", s->window[s->strstart])); - _tr_tally_lit (s, s->window[s->strstart], bflush); + _tr_tally_lit(s, s->window[s->strstart], bflush); s->lookahead--; s->strstart++; if (bflush) FLUSH_BLOCK(s, 0); } - FLUSH_BLOCK(s, flush == Z_FINISH); - return flush == Z_FINISH ? finish_done : block_done; + s->insert = 0; + if (flush == Z_FINISH) { + FLUSH_BLOCK(s, 1); + return finish_done; + } + if (s->sym_next) + FLUSH_BLOCK(s, 0); + return block_done; } diff --git a/deps/zlib/deflate.h b/deps/zlib/deflate.h index d7d26f8a96c..300c6ada62b 100644 --- a/deps/zlib/deflate.h +++ b/deps/zlib/deflate.h @@ -1,5 +1,5 @@ /* deflate.h -- internal compression state - * Copyright (C) 1995-2010 Jean-loup Gailly + * Copyright (C) 1995-2024 Jean-loup Gailly * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -23,6 +23,10 @@ # define GZIP #endif +/* define LIT_MEM to slightly increase the speed of deflate (order 1% to 2%) at + the cost of a larger memory footprint */ +/* #define LIT_MEM */ + /* =========================================================================== * Internal compression state. */ @@ -48,13 +52,19 @@ #define MAX_BITS 15 /* All codes must not exceed MAX_BITS bits */ -#define INIT_STATE 42 -#define EXTRA_STATE 69 -#define NAME_STATE 73 -#define COMMENT_STATE 91 -#define HCRC_STATE 103 -#define BUSY_STATE 113 -#define FINISH_STATE 666 +#define Buf_size 16 +/* size of bit buffer in bi_buf */ + +#define INIT_STATE 42 /* zlib header -> BUSY_STATE */ +#ifdef GZIP +# define GZIP_STATE 57 /* gzip header -> BUSY_STATE | EXTRA_STATE */ +#endif +#define EXTRA_STATE 69 /* gzip extra block -> NAME_STATE */ +#define NAME_STATE 73 /* gzip file name -> COMMENT_STATE */ +#define COMMENT_STATE 91 /* gzip comment -> HCRC_STATE */ +#define HCRC_STATE 103 /* gzip header CRC -> BUSY_STATE */ +#define BUSY_STATE 113 /* deflate -> FINISH_STATE */ +#define FINISH_STATE 666 /* stream complete */ /* Stream status */ @@ -80,7 +90,7 @@ typedef struct static_tree_desc_s static_tree_desc; typedef struct tree_desc_s { ct_data *dyn_tree; /* the dynamic tree */ int max_code; /* largest code with non zero frequency */ - static_tree_desc *stat_desc; /* the corresponding static tree */ + const static_tree_desc *stat_desc; /* the corresponding static tree */ } FAR tree_desc; typedef ush Pos; @@ -97,11 +107,11 @@ typedef struct internal_state { Bytef *pending_buf; /* output still pending */ ulg pending_buf_size; /* size of pending_buf */ Bytef *pending_out; /* next pending byte to output to the stream */ - uInt pending; /* nb of bytes in the pending buffer */ + ulg pending; /* nb of bytes in the pending buffer */ int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ gz_headerp gzhead; /* gzip header information to write */ - uInt gzindex; /* where in extra, name, or comment */ - Byte method; /* STORED (for zip only) or DEFLATED */ + ulg gzindex; /* where in extra, name, or comment */ + Byte method; /* can only be DEFLATED */ int last_flush; /* value of flush param for previous deflate call */ /* used by deflate.c: */ @@ -188,7 +198,7 @@ typedef struct internal_state { int nice_match; /* Stop searching when current match exceeds this */ /* used by trees.c: */ - /* Didn't use ct_data typedef below to supress compiler warning */ + /* Didn't use ct_data typedef below to suppress compiler warning */ struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ @@ -211,7 +221,14 @@ typedef struct internal_state { /* Depth of each subtree used as tie breaker for trees of equal frequency */ - uchf *l_buf; /* buffer for literals or lengths */ +#ifdef LIT_MEM +# define LIT_BUFS 5 + ushf *d_buf; /* buffer for distances */ + uchf *l_buf; /* buffer for literals/lengths */ +#else +# define LIT_BUFS 4 + uchf *sym_buf; /* buffer for distances and literals/lengths */ +#endif uInt lit_bufsize; /* Size of match buffer for literals/lengths. There are 4 reasons for @@ -233,20 +250,15 @@ typedef struct internal_state { * - I can't count above 4 */ - uInt last_lit; /* running index in l_buf */ - - ushf *d_buf; - /* Buffer for distances. To simplify the code, d_buf and l_buf have - * the same number of elements. To use different lengths, an extra flag - * array would be necessary. - */ + uInt sym_next; /* running index in symbol buffer */ + uInt sym_end; /* symbol table full when sym_next reaches this */ ulg opt_len; /* bit length of current block with optimal trees */ ulg static_len; /* bit length of current block with static trees */ uInt matches; /* number of string matches in current block */ - int last_eob_len; /* bit length of EOB code for last block */ + uInt insert; /* bytes at end of window left to insert */ -#ifdef DEBUG +#ifdef ZLIB_DEBUG ulg compressed_len; /* total bit length of compressed file mod 2^32 */ ulg bits_sent; /* bit length of compressed data sent mod 2^32 */ #endif @@ -272,7 +284,7 @@ typedef struct internal_state { /* Output a byte on the stream. * IN assertion: there is enough room in pending_buf. */ -#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);} +#define put_byte(s, c) {s->pending_buf[s->pending++] = (Bytef)(c);} #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) @@ -290,13 +302,14 @@ typedef struct internal_state { memory checker errors from longest match routines */ /* in trees.c */ -void ZLIB_INTERNAL _tr_init OF((deflate_state *s)); -int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc)); -void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf, - ulg stored_len, int last)); -void ZLIB_INTERNAL _tr_align OF((deflate_state *s)); -void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, - ulg stored_len, int last)); +void ZLIB_INTERNAL _tr_init(deflate_state *s); +int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc); +void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf, + ulg stored_len, int last); +void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s); +void ZLIB_INTERNAL _tr_align(deflate_state *s); +void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, + ulg stored_len, int last); #define d_code(dist) \ ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]) @@ -305,7 +318,7 @@ void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, * used. */ -#ifndef DEBUG +#ifndef ZLIB_DEBUG /* Inline versions of _tr_tally for speed: */ #if defined(GEN_TREES_H) || !defined(STDC) @@ -316,24 +329,46 @@ void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, extern const uch ZLIB_INTERNAL _dist_code[]; #endif +#ifdef LIT_MEM # define _tr_tally_lit(s, c, flush) \ - { uch cc = (uch)(c); \ - s->d_buf[s->last_lit] = 0; \ - s->l_buf[s->last_lit++] = cc; \ + { uch cc = (c); \ + s->d_buf[s->sym_next] = 0; \ + s->l_buf[s->sym_next++] = cc; \ s->dyn_ltree[cc].Freq++; \ - flush = (s->last_lit == s->lit_bufsize-1); \ + flush = (s->sym_next == s->sym_end); \ } # define _tr_tally_dist(s, distance, length, flush) \ { uch len = (uch)(length); \ ush dist = (ush)(distance); \ - s->d_buf[s->last_lit] = dist; \ - s->l_buf[s->last_lit++] = len; \ + s->d_buf[s->sym_next] = dist; \ + s->l_buf[s->sym_next++] = len; \ dist--; \ s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ s->dyn_dtree[d_code(dist)].Freq++; \ - flush = (s->last_lit == s->lit_bufsize-1); \ + flush = (s->sym_next == s->sym_end); \ } #else +# define _tr_tally_lit(s, c, flush) \ + { uch cc = (c); \ + s->sym_buf[s->sym_next++] = 0; \ + s->sym_buf[s->sym_next++] = 0; \ + s->sym_buf[s->sym_next++] = cc; \ + s->dyn_ltree[cc].Freq++; \ + flush = (s->sym_next == s->sym_end); \ + } +# define _tr_tally_dist(s, distance, length, flush) \ + { uch len = (uch)(length); \ + ush dist = (ush)(distance); \ + s->sym_buf[s->sym_next++] = (uch)dist; \ + s->sym_buf[s->sym_next++] = (uch)(dist >> 8); \ + s->sym_buf[s->sym_next++] = len; \ + dist--; \ + s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ + s->dyn_dtree[d_code(dist)].Freq++; \ + flush = (s->sym_next == s->sym_end); \ + } +#endif +#else # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) # define _tr_tally_dist(s, distance, length, flush) \ flush = _tr_tally(s, distance, length) diff --git a/deps/zlib/gzguts.h b/deps/zlib/gzguts.h new file mode 100644 index 00000000000..eba72085bb7 --- /dev/null +++ b/deps/zlib/gzguts.h @@ -0,0 +1,214 @@ +/* gzguts.h -- zlib internal header definitions for gz* operations + * Copyright (C) 2004-2024 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#ifdef _LARGEFILE64_SOURCE +# ifndef _LARGEFILE_SOURCE +# define _LARGEFILE_SOURCE 1 +# endif +# undef _FILE_OFFSET_BITS +# undef _TIME_BITS +#endif + +#ifdef HAVE_HIDDEN +# define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) +#else +# define ZLIB_INTERNAL +#endif + +#include +#include "zlib.h" +#ifdef STDC +# include +# include +# include +#endif + +#ifndef _POSIX_SOURCE +# define _POSIX_SOURCE +#endif +#include + +#ifdef _WIN32 +# include +#endif + +#if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32) +# include +#endif + +#if defined(_WIN32) +# define WIDECHAR +#endif + +#ifdef WINAPI_FAMILY +# define open _open +# define read _read +# define write _write +# define close _close +#endif + +#ifdef NO_DEFLATE /* for compatibility with old definition */ +# define NO_GZCOMPRESS +#endif + +#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) +# ifndef HAVE_VSNPRINTF +# define HAVE_VSNPRINTF +# endif +#endif + +#if defined(__CYGWIN__) +# ifndef HAVE_VSNPRINTF +# define HAVE_VSNPRINTF +# endif +#endif + +#if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410) +# ifndef HAVE_VSNPRINTF +# define HAVE_VSNPRINTF +# endif +#endif + +#ifndef HAVE_VSNPRINTF +# ifdef MSDOS +/* vsnprintf may exist on some MS-DOS compilers (DJGPP?), + but for now we just assume it doesn't. */ +# define NO_vsnprintf +# endif +# ifdef __TURBOC__ +# define NO_vsnprintf +# endif +# ifdef WIN32 +/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ +# if !defined(vsnprintf) && !defined(NO_vsnprintf) +# if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 ) +# define vsnprintf _vsnprintf +# endif +# endif +# endif +# ifdef __SASC +# define NO_vsnprintf +# endif +# ifdef VMS +# define NO_vsnprintf +# endif +# ifdef __OS400__ +# define NO_vsnprintf +# endif +# ifdef __MVS__ +# define NO_vsnprintf +# endif +#endif + +/* unlike snprintf (which is required in C99), _snprintf does not guarantee + null termination of the result -- however this is only used in gzlib.c where + the result is assured to fit in the space provided */ +#if defined(_MSC_VER) && _MSC_VER < 1900 +# define snprintf _snprintf +#endif + +#ifndef local +# define local static +#endif +/* since "static" is used to mean two completely different things in C, we + define "local" for the non-static meaning of "static", for readability + (compile with -Dlocal if your debugger can't find static symbols) */ + +/* gz* functions always use library allocation functions */ +#ifndef STDC + extern voidp malloc(uInt size); + extern void free(voidpf ptr); +#endif + +/* get errno and strerror definition */ +#if defined UNDER_CE +# include +# define zstrerror() gz_strwinerror((DWORD)GetLastError()) +#else +# ifndef NO_STRERROR +# include +# define zstrerror() strerror(errno) +# else +# define zstrerror() "stdio error (consult errno)" +# endif +#endif + +/* provide prototypes for these when building zlib without LFS */ +#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 + ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *); + ZEXTERN z_off64_t ZEXPORT gzseek64(gzFile, z_off64_t, int); + ZEXTERN z_off64_t ZEXPORT gztell64(gzFile); + ZEXTERN z_off64_t ZEXPORT gzoffset64(gzFile); +#endif + +/* default memLevel */ +#if MAX_MEM_LEVEL >= 8 +# define DEF_MEM_LEVEL 8 +#else +# define DEF_MEM_LEVEL MAX_MEM_LEVEL +#endif + +/* default i/o buffer size -- double this for output when reading (this and + twice this must be able to fit in an unsigned type) */ +#define GZBUFSIZE 8192 + +/* gzip modes, also provide a little integrity check on the passed structure */ +#define GZ_NONE 0 +#define GZ_READ 7247 +#define GZ_WRITE 31153 +#define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */ + +/* values for gz_state how */ +#define LOOK 0 /* look for a gzip header */ +#define COPY 1 /* copy input directly */ +#define GZIP 2 /* decompress a gzip stream */ + +/* internal gzip file state data structure */ +typedef struct { + /* exposed contents for gzgetc() macro */ + struct gzFile_s x; /* "x" for exposed */ + /* x.have: number of bytes available at x.next */ + /* x.next: next output data to deliver or write */ + /* x.pos: current position in uncompressed data */ + /* used for both reading and writing */ + int mode; /* see gzip modes above */ + int fd; /* file descriptor */ + char *path; /* path or fd for error messages */ + unsigned size; /* buffer size, zero if not allocated yet */ + unsigned want; /* requested buffer size, default is GZBUFSIZE */ + unsigned char *in; /* input buffer (double-sized when writing) */ + unsigned char *out; /* output buffer (double-sized when reading) */ + int direct; /* 0 if processing gzip, 1 if transparent */ + /* just for reading */ + int how; /* 0: get header, 1: copy, 2: decompress */ + z_off64_t start; /* where the gzip data started, for rewinding */ + int eof; /* true if end of input file reached */ + int past; /* true if read requested past end */ + /* just for writing */ + int level; /* compression level */ + int strategy; /* compression strategy */ + int reset; /* true if a reset is pending after a Z_FINISH */ + /* seek request */ + z_off64_t skip; /* amount to skip (already rewound if backwards) */ + int seek; /* true if seek request pending */ + /* error information */ + int err; /* error code */ + char *msg; /* error message */ + /* zlib inflate or deflate stream */ + z_stream strm; /* stream structure in-place (not a pointer) */ +} gz_state; +typedef gz_state FAR *gz_statep; + +/* shared functions */ +void ZLIB_INTERNAL gz_error(gz_statep, int, const char *); +#if defined UNDER_CE +char ZLIB_INTERNAL *gz_strwinerror(DWORD error); +#endif + +/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t + value -- needed when comparing unsigned to z_off64_t, which is signed + (possible z_off64_t types off_t, off64_t, and long are all signed) */ +unsigned ZLIB_INTERNAL gz_intmax(void); +#define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) diff --git a/deps/zlib/infback.c b/deps/zlib/infback.c new file mode 100644 index 00000000000..e7b25b307a3 --- /dev/null +++ b/deps/zlib/infback.c @@ -0,0 +1,628 @@ +/* infback.c -- inflate using a call-back interface + * Copyright (C) 1995-2022 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* + This code is largely copied from inflate.c. Normally either infback.o or + inflate.o would be linked into an application--not both. The interface + with inffast.c is retained so that optimized assembler-coded versions of + inflate_fast() can be used with either inflate.c or infback.c. + */ + +#include "zutil.h" +#include "inftrees.h" +#include "inflate.h" +#include "inffast.h" + +/* + strm provides memory allocation functions in zalloc and zfree, or + Z_NULL to use the library memory allocation functions. + + windowBits is in the range 8..15, and window is a user-supplied + window and output buffer that is 2**windowBits bytes. + */ +int ZEXPORT inflateBackInit_(z_streamp strm, int windowBits, + unsigned char FAR *window, const char *version, + int stream_size) { + struct inflate_state FAR *state; + + if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || + stream_size != (int)(sizeof(z_stream))) + return Z_VERSION_ERROR; + if (strm == Z_NULL || window == Z_NULL || + windowBits < 8 || windowBits > 15) + return Z_STREAM_ERROR; + strm->msg = Z_NULL; /* in case we return an error */ + if (strm->zalloc == (alloc_func)0) { +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zalloc = zcalloc; + strm->opaque = (voidpf)0; +#endif + } + if (strm->zfree == (free_func)0) +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zfree = zcfree; +#endif + state = (struct inflate_state FAR *)ZALLOC(strm, 1, + sizeof(struct inflate_state)); + if (state == Z_NULL) return Z_MEM_ERROR; + Tracev((stderr, "inflate: allocated\n")); + strm->state = (struct internal_state FAR *)state; + state->dmax = 32768U; + state->wbits = (uInt)windowBits; + state->wsize = 1U << windowBits; + state->window = window; + state->wnext = 0; + state->whave = 0; + state->sane = 1; + return Z_OK; +} + +/* + Return state with length and distance decoding tables and index sizes set to + fixed code decoding. Normally this returns fixed tables from inffixed.h. + If BUILDFIXED is defined, then instead this routine builds the tables the + first time it's called, and returns those tables the first time and + thereafter. This reduces the size of the code by about 2K bytes, in + exchange for a little execution time. However, BUILDFIXED should not be + used for threaded applications, since the rewriting of the tables and virgin + may not be thread-safe. + */ +local void fixedtables(struct inflate_state FAR *state) { +#ifdef BUILDFIXED + static int virgin = 1; + static code *lenfix, *distfix; + static code fixed[544]; + + /* build fixed huffman tables if first call (may not be thread safe) */ + if (virgin) { + unsigned sym, bits; + static code *next; + + /* literal/length table */ + sym = 0; + while (sym < 144) state->lens[sym++] = 8; + while (sym < 256) state->lens[sym++] = 9; + while (sym < 280) state->lens[sym++] = 7; + while (sym < 288) state->lens[sym++] = 8; + next = fixed; + lenfix = next; + bits = 9; + inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); + + /* distance table */ + sym = 0; + while (sym < 32) state->lens[sym++] = 5; + distfix = next; + bits = 5; + inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); + + /* do this just once */ + virgin = 0; + } +#else /* !BUILDFIXED */ +# include "inffixed.h" +#endif /* BUILDFIXED */ + state->lencode = lenfix; + state->lenbits = 9; + state->distcode = distfix; + state->distbits = 5; +} + +/* Macros for inflateBack(): */ + +/* Load returned state from inflate_fast() */ +#define LOAD() \ + do { \ + put = strm->next_out; \ + left = strm->avail_out; \ + next = strm->next_in; \ + have = strm->avail_in; \ + hold = state->hold; \ + bits = state->bits; \ + } while (0) + +/* Set state from registers for inflate_fast() */ +#define RESTORE() \ + do { \ + strm->next_out = put; \ + strm->avail_out = left; \ + strm->next_in = next; \ + strm->avail_in = have; \ + state->hold = hold; \ + state->bits = bits; \ + } while (0) + +/* Clear the input bit accumulator */ +#define INITBITS() \ + do { \ + hold = 0; \ + bits = 0; \ + } while (0) + +/* Assure that some input is available. If input is requested, but denied, + then return a Z_BUF_ERROR from inflateBack(). */ +#define PULL() \ + do { \ + if (have == 0) { \ + have = in(in_desc, &next); \ + if (have == 0) { \ + next = Z_NULL; \ + ret = Z_BUF_ERROR; \ + goto inf_leave; \ + } \ + } \ + } while (0) + +/* Get a byte of input into the bit accumulator, or return from inflateBack() + with an error if there is no input available. */ +#define PULLBYTE() \ + do { \ + PULL(); \ + have--; \ + hold += (unsigned long)(*next++) << bits; \ + bits += 8; \ + } while (0) + +/* Assure that there are at least n bits in the bit accumulator. If there is + not enough available input to do that, then return from inflateBack() with + an error. */ +#define NEEDBITS(n) \ + do { \ + while (bits < (unsigned)(n)) \ + PULLBYTE(); \ + } while (0) + +/* Return the low n bits of the bit accumulator (n < 16) */ +#define BITS(n) \ + ((unsigned)hold & ((1U << (n)) - 1)) + +/* Remove n bits from the bit accumulator */ +#define DROPBITS(n) \ + do { \ + hold >>= (n); \ + bits -= (unsigned)(n); \ + } while (0) + +/* Remove zero to seven bits as needed to go to a byte boundary */ +#define BYTEBITS() \ + do { \ + hold >>= bits & 7; \ + bits -= bits & 7; \ + } while (0) + +/* Assure that some output space is available, by writing out the window + if it's full. If the write fails, return from inflateBack() with a + Z_BUF_ERROR. */ +#define ROOM() \ + do { \ + if (left == 0) { \ + put = state->window; \ + left = state->wsize; \ + state->whave = left; \ + if (out(out_desc, put, left)) { \ + ret = Z_BUF_ERROR; \ + goto inf_leave; \ + } \ + } \ + } while (0) + +/* + strm provides the memory allocation functions and window buffer on input, + and provides information on the unused input on return. For Z_DATA_ERROR + returns, strm will also provide an error message. + + in() and out() are the call-back input and output functions. When + inflateBack() needs more input, it calls in(). When inflateBack() has + filled the window with output, or when it completes with data in the + window, it calls out() to write out the data. The application must not + change the provided input until in() is called again or inflateBack() + returns. The application must not change the window/output buffer until + inflateBack() returns. + + in() and out() are called with a descriptor parameter provided in the + inflateBack() call. This parameter can be a structure that provides the + information required to do the read or write, as well as accumulated + information on the input and output such as totals and check values. + + in() should return zero on failure. out() should return non-zero on + failure. If either in() or out() fails, than inflateBack() returns a + Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it + was in() or out() that caused in the error. Otherwise, inflateBack() + returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format + error, or Z_MEM_ERROR if it could not allocate memory for the state. + inflateBack() can also return Z_STREAM_ERROR if the input parameters + are not correct, i.e. strm is Z_NULL or the state was not initialized. + */ +int ZEXPORT inflateBack(z_streamp strm, in_func in, void FAR *in_desc, + out_func out, void FAR *out_desc) { + struct inflate_state FAR *state; + z_const unsigned char FAR *next; /* next input */ + unsigned char FAR *put; /* next output */ + unsigned have, left; /* available input and output */ + unsigned long hold; /* bit buffer */ + unsigned bits; /* bits in bit buffer */ + unsigned copy; /* number of stored or match bytes to copy */ + unsigned char FAR *from; /* where to copy match bytes from */ + code here; /* current decoding table entry */ + code last; /* parent table entry */ + unsigned len; /* length to copy for repeats, bits to drop */ + int ret; /* return code */ + static const unsigned short order[19] = /* permutation of code lengths */ + {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; + + /* Check that the strm exists and that the state was initialized */ + if (strm == Z_NULL || strm->state == Z_NULL) + return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + + /* Reset the state */ + strm->msg = Z_NULL; + state->mode = TYPE; + state->last = 0; + state->whave = 0; + next = strm->next_in; + have = next != Z_NULL ? strm->avail_in : 0; + hold = 0; + bits = 0; + put = state->window; + left = state->wsize; + + /* Inflate until end of block marked as last */ + for (;;) + switch (state->mode) { + case TYPE: + /* determine and dispatch block type */ + if (state->last) { + BYTEBITS(); + state->mode = DONE; + break; + } + NEEDBITS(3); + state->last = BITS(1); + DROPBITS(1); + switch (BITS(2)) { + case 0: /* stored block */ + Tracev((stderr, "inflate: stored block%s\n", + state->last ? " (last)" : "")); + state->mode = STORED; + break; + case 1: /* fixed block */ + fixedtables(state); + Tracev((stderr, "inflate: fixed codes block%s\n", + state->last ? " (last)" : "")); + state->mode = LEN; /* decode codes */ + break; + case 2: /* dynamic block */ + Tracev((stderr, "inflate: dynamic codes block%s\n", + state->last ? " (last)" : "")); + state->mode = TABLE; + break; + case 3: + strm->msg = (char *)"invalid block type"; + state->mode = BAD; + } + DROPBITS(2); + break; + + case STORED: + /* get and verify stored block length */ + BYTEBITS(); /* go to byte boundary */ + NEEDBITS(32); + if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { + strm->msg = (char *)"invalid stored block lengths"; + state->mode = BAD; + break; + } + state->length = (unsigned)hold & 0xffff; + Tracev((stderr, "inflate: stored length %u\n", + state->length)); + INITBITS(); + + /* copy stored block from input to output */ + while (state->length != 0) { + copy = state->length; + PULL(); + ROOM(); + if (copy > have) copy = have; + if (copy > left) copy = left; + zmemcpy(put, next, copy); + have -= copy; + next += copy; + left -= copy; + put += copy; + state->length -= copy; + } + Tracev((stderr, "inflate: stored end\n")); + state->mode = TYPE; + break; + + case TABLE: + /* get dynamic table entries descriptor */ + NEEDBITS(14); + state->nlen = BITS(5) + 257; + DROPBITS(5); + state->ndist = BITS(5) + 1; + DROPBITS(5); + state->ncode = BITS(4) + 4; + DROPBITS(4); +#ifndef PKZIP_BUG_WORKAROUND + if (state->nlen > 286 || state->ndist > 30) { + strm->msg = (char *)"too many length or distance symbols"; + state->mode = BAD; + break; + } +#endif + Tracev((stderr, "inflate: table sizes ok\n")); + + /* get code length code lengths (not a typo) */ + state->have = 0; + while (state->have < state->ncode) { + NEEDBITS(3); + state->lens[order[state->have++]] = (unsigned short)BITS(3); + DROPBITS(3); + } + while (state->have < 19) + state->lens[order[state->have++]] = 0; + state->next = state->codes; + state->lencode = (code const FAR *)(state->next); + state->lenbits = 7; + ret = inflate_table(CODES, state->lens, 19, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid code lengths set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: code lengths ok\n")); + + /* get length and distance code code lengths */ + state->have = 0; + while (state->have < state->nlen + state->ndist) { + for (;;) { + here = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if (here.val < 16) { + DROPBITS(here.bits); + state->lens[state->have++] = here.val; + } + else { + if (here.val == 16) { + NEEDBITS(here.bits + 2); + DROPBITS(here.bits); + if (state->have == 0) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + len = (unsigned)(state->lens[state->have - 1]); + copy = 3 + BITS(2); + DROPBITS(2); + } + else if (here.val == 17) { + NEEDBITS(here.bits + 3); + DROPBITS(here.bits); + len = 0; + copy = 3 + BITS(3); + DROPBITS(3); + } + else { + NEEDBITS(here.bits + 7); + DROPBITS(here.bits); + len = 0; + copy = 11 + BITS(7); + DROPBITS(7); + } + if (state->have + copy > state->nlen + state->ndist) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + while (copy--) + state->lens[state->have++] = (unsigned short)len; + } + } + + /* handle error breaks in while */ + if (state->mode == BAD) break; + + /* check for end-of-block code (better have one) */ + if (state->lens[256] == 0) { + strm->msg = (char *)"invalid code -- missing end-of-block"; + state->mode = BAD; + break; + } + + /* build code tables -- note: do not change the lenbits or distbits + values here (9 and 6) without reading the comments in inftrees.h + concerning the ENOUGH constants, which depend on those values */ + state->next = state->codes; + state->lencode = (code const FAR *)(state->next); + state->lenbits = 9; + ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid literal/lengths set"; + state->mode = BAD; + break; + } + state->distcode = (code const FAR *)(state->next); + state->distbits = 6; + ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, + &(state->next), &(state->distbits), state->work); + if (ret) { + strm->msg = (char *)"invalid distances set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: codes ok\n")); + state->mode = LEN; + /* fallthrough */ + + case LEN: + /* use inflate_fast() if we have enough input and output */ + if (have >= 6 && left >= 258) { + RESTORE(); + if (state->whave < state->wsize) + state->whave = state->wsize - left; + inflate_fast(strm, state->wsize); + LOAD(); + break; + } + + /* get a literal, length, or end-of-block code */ + for (;;) { + here = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if (here.op && (here.op & 0xf0) == 0) { + last = here; + for (;;) { + here = state->lencode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + here.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + } + DROPBITS(here.bits); + state->length = (unsigned)here.val; + + /* process literal */ + if (here.op == 0) { + Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? + "inflate: literal '%c'\n" : + "inflate: literal 0x%02x\n", here.val)); + ROOM(); + *put++ = (unsigned char)(state->length); + left--; + state->mode = LEN; + break; + } + + /* process end of block */ + if (here.op & 32) { + Tracevv((stderr, "inflate: end of block\n")); + state->mode = TYPE; + break; + } + + /* invalid code */ + if (here.op & 64) { + strm->msg = (char *)"invalid literal/length code"; + state->mode = BAD; + break; + } + + /* length code -- get extra bits, if any */ + state->extra = (unsigned)(here.op) & 15; + if (state->extra != 0) { + NEEDBITS(state->extra); + state->length += BITS(state->extra); + DROPBITS(state->extra); + } + Tracevv((stderr, "inflate: length %u\n", state->length)); + + /* get distance code */ + for (;;) { + here = state->distcode[BITS(state->distbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if ((here.op & 0xf0) == 0) { + last = here; + for (;;) { + here = state->distcode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + here.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + } + DROPBITS(here.bits); + if (here.op & 64) { + strm->msg = (char *)"invalid distance code"; + state->mode = BAD; + break; + } + state->offset = (unsigned)here.val; + + /* get distance extra bits, if any */ + state->extra = (unsigned)(here.op) & 15; + if (state->extra != 0) { + NEEDBITS(state->extra); + state->offset += BITS(state->extra); + DROPBITS(state->extra); + } + if (state->offset > state->wsize - (state->whave < state->wsize ? + left : 0)) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } + Tracevv((stderr, "inflate: distance %u\n", state->offset)); + + /* copy match from window to output */ + do { + ROOM(); + copy = state->wsize - state->offset; + if (copy < left) { + from = put + copy; + copy = left - copy; + } + else { + from = put - state->offset; + copy = left; + } + if (copy > state->length) copy = state->length; + state->length -= copy; + left -= copy; + do { + *put++ = *from++; + } while (--copy); + } while (state->length != 0); + break; + + case DONE: + /* inflate stream terminated properly */ + ret = Z_STREAM_END; + goto inf_leave; + + case BAD: + ret = Z_DATA_ERROR; + goto inf_leave; + + default: + /* can't happen, but makes compilers happy */ + ret = Z_STREAM_ERROR; + goto inf_leave; + } + + /* Write leftover output and return unused input */ + inf_leave: + if (left < state->wsize) { + if (out(out_desc, state->window, state->wsize - left) && + ret == Z_STREAM_END) + ret = Z_BUF_ERROR; + } + strm->next_in = next; + strm->avail_in = have; + return ret; +} + +int ZEXPORT inflateBackEnd(z_streamp strm) { + if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) + return Z_STREAM_ERROR; + ZFREE(strm, strm->state); + strm->state = Z_NULL; + Tracev((stderr, "inflate: end\n")); + return Z_OK; +} diff --git a/deps/zlib/inffast.c b/deps/zlib/inffast.c index 2f1d60b43b8..9354676e786 100644 --- a/deps/zlib/inffast.c +++ b/deps/zlib/inffast.c @@ -1,5 +1,5 @@ /* inffast.c -- fast decoding - * Copyright (C) 1995-2008, 2010 Mark Adler + * Copyright (C) 1995-2017 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -8,26 +8,9 @@ #include "inflate.h" #include "inffast.h" -#ifndef ASMINF - -/* Allow machine dependent optimization for post-increment or pre-increment. - Based on testing to date, - Pre-increment preferred for: - - PowerPC G3 (Adler) - - MIPS R5000 (Randers-Pehrson) - Post-increment preferred for: - - none - No measurable difference: - - Pentium III (Anderson) - - M68060 (Nikl) - */ -#ifdef POSTINC -# define OFF 0 -# define PUP(a) *(a)++ +#ifdef ASMINF +# pragma message("Assembler code may have bugs -- use at your own risk") #else -# define OFF 1 -# define PUP(a) *++(a) -#endif /* Decode literal, length, and distance codes and write out the resulting @@ -64,13 +47,10 @@ requires strm->avail_out >= 258 for each loop to avoid checking for output space. */ -void ZLIB_INTERNAL inflate_fast(strm, start) -z_streamp strm; -unsigned start; /* inflate()'s starting value for strm->avail_out */ -{ +void ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start) { struct inflate_state FAR *state; - unsigned char FAR *in; /* local strm->next_in */ - unsigned char FAR *last; /* while in < last, enough input available */ + z_const unsigned char FAR *in; /* local strm->next_in */ + z_const unsigned char FAR *last; /* have enough input while in < last */ unsigned char FAR *out; /* local strm->next_out */ unsigned char FAR *beg; /* inflate()'s initial strm->next_out */ unsigned char FAR *end; /* while out < end, enough space available */ @@ -87,7 +67,7 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ code const FAR *dcode; /* local strm->distcode */ unsigned lmask; /* mask for first level of length codes */ unsigned dmask; /* mask for first level of distance codes */ - code here; /* retrieved table entry */ + code const *here; /* retrieved table entry */ unsigned op; /* code bits, operation, extra bits, or */ /* window position, window bytes to copy */ unsigned len; /* match length, unused bytes */ @@ -96,9 +76,9 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ /* copy state to local variables */ state = (struct inflate_state FAR *)strm->state; - in = strm->next_in - OFF; + in = strm->next_in; last = in + (strm->avail_in - 5); - out = strm->next_out - OFF; + out = strm->next_out; beg = out - (start - strm->avail_out); end = out + (strm->avail_out - 257); #ifdef INFLATE_STRICT @@ -119,29 +99,29 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ input data or output space */ do { if (bits < 15) { - hold += (unsigned long)(PUP(in)) << bits; + hold += (unsigned long)(*in++) << bits; bits += 8; - hold += (unsigned long)(PUP(in)) << bits; + hold += (unsigned long)(*in++) << bits; bits += 8; } - here = lcode[hold & lmask]; + here = lcode + (hold & lmask); dolen: - op = (unsigned)(here.bits); + op = (unsigned)(here->bits); hold >>= op; bits -= op; - op = (unsigned)(here.op); + op = (unsigned)(here->op); if (op == 0) { /* literal */ - Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? + Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ? "inflate: literal '%c'\n" : - "inflate: literal 0x%02x\n", here.val)); - PUP(out) = (unsigned char)(here.val); + "inflate: literal 0x%02x\n", here->val)); + *out++ = (unsigned char)(here->val); } else if (op & 16) { /* length base */ - len = (unsigned)(here.val); + len = (unsigned)(here->val); op &= 15; /* number of extra bits */ if (op) { if (bits < op) { - hold += (unsigned long)(PUP(in)) << bits; + hold += (unsigned long)(*in++) << bits; bits += 8; } len += (unsigned)hold & ((1U << op) - 1); @@ -150,25 +130,25 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ } Tracevv((stderr, "inflate: length %u\n", len)); if (bits < 15) { - hold += (unsigned long)(PUP(in)) << bits; + hold += (unsigned long)(*in++) << bits; bits += 8; - hold += (unsigned long)(PUP(in)) << bits; + hold += (unsigned long)(*in++) << bits; bits += 8; } - here = dcode[hold & dmask]; + here = dcode + (hold & dmask); dodist: - op = (unsigned)(here.bits); + op = (unsigned)(here->bits); hold >>= op; bits -= op; - op = (unsigned)(here.op); + op = (unsigned)(here->op); if (op & 16) { /* distance base */ - dist = (unsigned)(here.val); + dist = (unsigned)(here->val); op &= 15; /* number of extra bits */ if (bits < op) { - hold += (unsigned long)(PUP(in)) << bits; + hold += (unsigned long)(*in++) << bits; bits += 8; if (bits < op) { - hold += (unsigned long)(PUP(in)) << bits; + hold += (unsigned long)(*in++) << bits; bits += 8; } } @@ -196,30 +176,30 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR if (len <= op - whave) { do { - PUP(out) = 0; + *out++ = 0; } while (--len); continue; } len -= op - whave; do { - PUP(out) = 0; + *out++ = 0; } while (--op > whave); if (op == 0) { from = out - dist; do { - PUP(out) = PUP(from); + *out++ = *from++; } while (--len); continue; } #endif } - from = window - OFF; + from = window; if (wnext == 0) { /* very common case */ from += wsize - op; if (op < len) { /* some from window */ len -= op; do { - PUP(out) = PUP(from); + *out++ = *from++; } while (--op); from = out - dist; /* rest from output */ } @@ -230,14 +210,14 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ if (op < len) { /* some from end of window */ len -= op; do { - PUP(out) = PUP(from); + *out++ = *from++; } while (--op); - from = window - OFF; + from = window; if (wnext < len) { /* some from start of window */ op = wnext; len -= op; do { - PUP(out) = PUP(from); + *out++ = *from++; } while (--op); from = out - dist; /* rest from output */ } @@ -248,40 +228,40 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ if (op < len) { /* some from window */ len -= op; do { - PUP(out) = PUP(from); + *out++ = *from++; } while (--op); from = out - dist; /* rest from output */ } } while (len > 2) { - PUP(out) = PUP(from); - PUP(out) = PUP(from); - PUP(out) = PUP(from); + *out++ = *from++; + *out++ = *from++; + *out++ = *from++; len -= 3; } if (len) { - PUP(out) = PUP(from); + *out++ = *from++; if (len > 1) - PUP(out) = PUP(from); + *out++ = *from++; } } else { from = out - dist; /* copy direct from output */ do { /* minimum length is three */ - PUP(out) = PUP(from); - PUP(out) = PUP(from); - PUP(out) = PUP(from); + *out++ = *from++; + *out++ = *from++; + *out++ = *from++; len -= 3; } while (len > 2); if (len) { - PUP(out) = PUP(from); + *out++ = *from++; if (len > 1) - PUP(out) = PUP(from); + *out++ = *from++; } } } else if ((op & 64) == 0) { /* 2nd level distance code */ - here = dcode[here.val + (hold & ((1U << op) - 1))]; + here = dcode + here->val + (hold & ((1U << op) - 1)); goto dodist; } else { @@ -291,7 +271,7 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ } } else if ((op & 64) == 0) { /* 2nd level length code */ - here = lcode[here.val + (hold & ((1U << op) - 1))]; + here = lcode + here->val + (hold & ((1U << op) - 1)); goto dolen; } else if (op & 32) { /* end-of-block */ @@ -313,8 +293,8 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ hold &= (1U << bits) - 1; /* update state and return */ - strm->next_in = in + OFF; - strm->next_out = out + OFF; + strm->next_in = in; + strm->next_out = out; strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last)); strm->avail_out = (unsigned)(out < end ? 257 + (end - out) : 257 - (out - end)); diff --git a/deps/zlib/inffast.h b/deps/zlib/inffast.h index e5c1aa4ca8c..49c6d156c5c 100644 --- a/deps/zlib/inffast.h +++ b/deps/zlib/inffast.h @@ -8,4 +8,4 @@ subject to change. Applications should only use zlib.h. */ -void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start)); +void ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start); diff --git a/deps/zlib/inffixed.h b/deps/zlib/inffixed.h index 75ed4b5978d..d6283277694 100644 --- a/deps/zlib/inffixed.h +++ b/deps/zlib/inffixed.h @@ -2,9 +2,9 @@ * Generated automatically by makefixed(). */ - /* WARNING: this file should *not* be used by applications. It - is part of the implementation of the compression library and - is subject to change. Applications should only use zlib.h. + /* WARNING: this file should *not* be used by applications. + It is part of the implementation of this library and is + subject to change. Applications should only use zlib.h. */ static const code lenfix[512] = { diff --git a/deps/zlib/inflate.c b/deps/zlib/inflate.c index a8431abeacf..94ecff015a9 100644 --- a/deps/zlib/inflate.c +++ b/deps/zlib/inflate.c @@ -1,5 +1,5 @@ /* inflate.c -- zlib decompression - * Copyright (C) 1995-2010 Mark Adler + * Copyright (C) 1995-2022 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -91,33 +91,33 @@ # endif #endif -/* function prototypes */ -local void fixedtables OF((struct inflate_state FAR *state)); -local int updatewindow OF((z_streamp strm, unsigned out)); -#ifdef BUILDFIXED - void makefixed OF((void)); -#endif -local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf, - unsigned len)); +local int inflateStateCheck(z_streamp strm) { + struct inflate_state FAR *state; + if (strm == Z_NULL || + strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) + return 1; + state = (struct inflate_state FAR *)strm->state; + if (state == Z_NULL || state->strm != strm || + state->mode < HEAD || state->mode > SYNC) + return 1; + return 0; +} -int ZEXPORT inflateReset(strm) -z_streamp strm; -{ +int ZEXPORT inflateResetKeep(z_streamp strm) { struct inflate_state FAR *state; - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; state = (struct inflate_state FAR *)strm->state; strm->total_in = strm->total_out = state->total = 0; strm->msg = Z_NULL; - strm->adler = 1; /* to support ill-conceived Java test suite */ + if (state->wrap) /* to support ill-conceived Java test suite */ + strm->adler = state->wrap & 1; state->mode = HEAD; state->last = 0; state->havedict = 0; + state->flags = -1; state->dmax = 32768U; state->head = Z_NULL; - state->wsize = 0; - state->whave = 0; - state->wnext = 0; state->hold = 0; state->bits = 0; state->lencode = state->distcode = state->next = state->codes; @@ -127,24 +127,34 @@ z_streamp strm; return Z_OK; } -int ZEXPORT inflateReset2(strm, windowBits) -z_streamp strm; -int windowBits; -{ +int ZEXPORT inflateReset(z_streamp strm) { + struct inflate_state FAR *state; + + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + state->wsize = 0; + state->whave = 0; + state->wnext = 0; + return inflateResetKeep(strm); +} + +int ZEXPORT inflateReset2(z_streamp strm, int windowBits) { int wrap; struct inflate_state FAR *state; /* get the state */ - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; state = (struct inflate_state FAR *)strm->state; /* extract wrap request from windowBits parameter */ if (windowBits < 0) { + if (windowBits < -15) + return Z_STREAM_ERROR; wrap = 0; windowBits = -windowBits; } else { - wrap = (windowBits >> 4) + 1; + wrap = (windowBits >> 4) + 5; #ifdef GUNZIP if (windowBits < 48) windowBits &= 15; @@ -165,12 +175,8 @@ int windowBits; return inflateReset(strm); } -int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size) -z_streamp strm; -int windowBits; -const char *version; -int stream_size; -{ +int ZEXPORT inflateInit2_(z_streamp strm, int windowBits, + const char *version, int stream_size) { int ret; struct inflate_state FAR *state; @@ -180,16 +186,27 @@ int stream_size; if (strm == Z_NULL) return Z_STREAM_ERROR; strm->msg = Z_NULL; /* in case we return an error */ if (strm->zalloc == (alloc_func)0) { +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else strm->zalloc = zcalloc; strm->opaque = (voidpf)0; +#endif } - if (strm->zfree == (free_func)0) strm->zfree = zcfree; + if (strm->zfree == (free_func)0) +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zfree = zcfree; +#endif state = (struct inflate_state FAR *) ZALLOC(strm, 1, sizeof(struct inflate_state)); if (state == Z_NULL) return Z_MEM_ERROR; Tracev((stderr, "inflate: allocated\n")); strm->state = (struct internal_state FAR *)state; + state->strm = strm; state->window = Z_NULL; + state->mode = HEAD; /* to pass state test in inflateReset2() */ ret = inflateReset2(strm, windowBits); if (ret != Z_OK) { ZFREE(strm, state); @@ -198,32 +215,27 @@ int stream_size; return ret; } -int ZEXPORT inflateInit_(strm, version, stream_size) -z_streamp strm; -const char *version; -int stream_size; -{ +int ZEXPORT inflateInit_(z_streamp strm, const char *version, + int stream_size) { return inflateInit2_(strm, DEF_WBITS, version, stream_size); } -int ZEXPORT inflatePrime(strm, bits, value) -z_streamp strm; -int bits; -int value; -{ +int ZEXPORT inflatePrime(z_streamp strm, int bits, int value) { struct inflate_state FAR *state; - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + if (bits == 0) + return Z_OK; state = (struct inflate_state FAR *)strm->state; if (bits < 0) { state->hold = 0; state->bits = 0; return Z_OK; } - if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR; + if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR; value &= (1L << bits) - 1; - state->hold += value << state->bits; - state->bits += bits; + state->hold += (unsigned)value << state->bits; + state->bits += (uInt)bits; return Z_OK; } @@ -237,9 +249,7 @@ int value; used for threaded applications, since the rewriting of the tables and virgin may not be thread-safe. */ -local void fixedtables(state) -struct inflate_state FAR *state; -{ +local void fixedtables(struct inflate_state FAR *state) { #ifdef BUILDFIXED static int virgin = 1; static code *lenfix, *distfix; @@ -301,7 +311,7 @@ struct inflate_state FAR *state; a.out > inffixed.h */ -void makefixed() +void makefixed(void) { unsigned low, size; struct inflate_state state; @@ -321,8 +331,8 @@ void makefixed() low = 0; for (;;) { if ((low % 7) == 0) printf("\n "); - printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits, - state.lencode[low].val); + printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op, + state.lencode[low].bits, state.lencode[low].val); if (++low == size) break; putchar(','); } @@ -355,12 +365,9 @@ void makefixed() output will fall in the output data, making match copies simpler and faster. The advantage may be dependent on the size of the processor's data caches. */ -local int updatewindow(strm, out) -z_streamp strm; -unsigned out; -{ +local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) { struct inflate_state FAR *state; - unsigned copy, dist; + unsigned dist; state = (struct inflate_state FAR *)strm->state; @@ -380,19 +387,18 @@ unsigned out; } /* copy state->wsize or less output bytes into the circular window */ - copy = out - strm->avail_out; if (copy >= state->wsize) { - zmemcpy(state->window, strm->next_out - state->wsize, state->wsize); + zmemcpy(state->window, end - state->wsize, state->wsize); state->wnext = 0; state->whave = state->wsize; } else { dist = state->wsize - state->wnext; if (dist > copy) dist = copy; - zmemcpy(state->window + state->wnext, strm->next_out - copy, dist); + zmemcpy(state->window + state->wnext, end - copy, dist); copy -= dist; if (copy) { - zmemcpy(state->window, strm->next_out - copy, copy); + zmemcpy(state->window, end - copy, copy); state->wnext = copy; state->whave = state->wsize; } @@ -409,10 +415,10 @@ unsigned out; /* check function to use adler32() for zlib or crc32() for gzip */ #ifdef GUNZIP -# define UPDATE(check, buf, len) \ +# define UPDATE_CHECK(check, buf, len) \ (state->flags ? crc32(check, buf, len) : adler32(check, buf, len)) #else -# define UPDATE(check, buf, len) adler32(check, buf, len) +# define UPDATE_CHECK(check, buf, len) adler32(check, buf, len) #endif /* check macros for header crc */ @@ -499,11 +505,6 @@ unsigned out; bits -= bits & 7; \ } while (0) -/* Reverse the bytes in a 32-bit value */ -#define REVERSE(q) \ - ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ - (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) - /* inflate() uses a state machine to process as much input data and generate as much output data as possible before returning. The state machine is @@ -586,12 +587,9 @@ unsigned out; will return Z_BUF_ERROR if it has not reached the end of the stream. */ -int ZEXPORT inflate(strm, flush) -z_streamp strm; -int flush; -{ +int ZEXPORT inflate(z_streamp strm, int flush) { struct inflate_state FAR *state; - unsigned char FAR *next; /* next input */ + z_const unsigned char FAR *next; /* next input */ unsigned char FAR *put; /* next output */ unsigned have, left; /* available input and output */ unsigned long hold; /* bit buffer */ @@ -609,7 +607,7 @@ int flush; static const unsigned short order[19] = /* permutation of code lengths */ {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; - if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL || + if (inflateStateCheck(strm) || strm->next_out == Z_NULL || (strm->next_in == Z_NULL && strm->avail_in != 0)) return Z_STREAM_ERROR; @@ -629,13 +627,14 @@ int flush; NEEDBITS(16); #ifdef GUNZIP if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ + if (state->wbits == 0) + state->wbits = 15; state->check = crc32(0L, Z_NULL, 0); CRC2(state->check, hold); INITBITS(); state->mode = FLAGS; break; } - state->flags = 0; /* expect zlib header */ if (state->head != Z_NULL) state->head->done = -1; if (!(state->wrap & 1) || /* check if zlib header allowed */ @@ -656,12 +655,13 @@ int flush; len = BITS(4) + 8; if (state->wbits == 0) state->wbits = len; - else if (len > state->wbits) { + if (len > 15 || len > state->wbits) { strm->msg = (char *)"invalid window size"; state->mode = BAD; break; } state->dmax = 1U << len; + state->flags = 0; /* indicate zlib header */ Tracev((stderr, "inflate: zlib header ok\n")); strm->adler = state->check = adler32(0L, Z_NULL, 0); state->mode = hold & 0x200 ? DICTID : TYPE; @@ -683,50 +683,59 @@ int flush; } if (state->head != Z_NULL) state->head->text = (int)((hold >> 8) & 1); - if (state->flags & 0x0200) CRC2(state->check, hold); + if ((state->flags & 0x0200) && (state->wrap & 4)) + CRC2(state->check, hold); INITBITS(); state->mode = TIME; + /* fallthrough */ case TIME: NEEDBITS(32); if (state->head != Z_NULL) state->head->time = hold; - if (state->flags & 0x0200) CRC4(state->check, hold); + if ((state->flags & 0x0200) && (state->wrap & 4)) + CRC4(state->check, hold); INITBITS(); state->mode = OS; + /* fallthrough */ case OS: NEEDBITS(16); if (state->head != Z_NULL) { state->head->xflags = (int)(hold & 0xff); state->head->os = (int)(hold >> 8); } - if (state->flags & 0x0200) CRC2(state->check, hold); + if ((state->flags & 0x0200) && (state->wrap & 4)) + CRC2(state->check, hold); INITBITS(); state->mode = EXLEN; + /* fallthrough */ case EXLEN: if (state->flags & 0x0400) { NEEDBITS(16); state->length = (unsigned)(hold); if (state->head != Z_NULL) state->head->extra_len = (unsigned)hold; - if (state->flags & 0x0200) CRC2(state->check, hold); + if ((state->flags & 0x0200) && (state->wrap & 4)) + CRC2(state->check, hold); INITBITS(); } else if (state->head != Z_NULL) state->head->extra = Z_NULL; state->mode = EXTRA; + /* fallthrough */ case EXTRA: if (state->flags & 0x0400) { copy = state->length; if (copy > have) copy = have; if (copy) { if (state->head != Z_NULL && - state->head->extra != Z_NULL) { - len = state->head->extra_len - state->length; + state->head->extra != Z_NULL && + (len = state->head->extra_len - state->length) < + state->head->extra_max) { zmemcpy(state->head->extra + len, next, len + copy > state->head->extra_max ? state->head->extra_max - len : copy); } - if (state->flags & 0x0200) + if ((state->flags & 0x0200) && (state->wrap & 4)) state->check = crc32(state->check, next, copy); have -= copy; next += copy; @@ -736,6 +745,7 @@ int flush; } state->length = 0; state->mode = NAME; + /* fallthrough */ case NAME: if (state->flags & 0x0800) { if (have == 0) goto inf_leave; @@ -745,9 +755,9 @@ int flush; if (state->head != Z_NULL && state->head->name != Z_NULL && state->length < state->head->name_max) - state->head->name[state->length++] = len; + state->head->name[state->length++] = (Bytef)len; } while (len && copy < have); - if (state->flags & 0x0200) + if ((state->flags & 0x0200) && (state->wrap & 4)) state->check = crc32(state->check, next, copy); have -= copy; next += copy; @@ -757,6 +767,7 @@ int flush; state->head->name = Z_NULL; state->length = 0; state->mode = COMMENT; + /* fallthrough */ case COMMENT: if (state->flags & 0x1000) { if (have == 0) goto inf_leave; @@ -766,9 +777,9 @@ int flush; if (state->head != Z_NULL && state->head->comment != Z_NULL && state->length < state->head->comm_max) - state->head->comment[state->length++] = len; + state->head->comment[state->length++] = (Bytef)len; } while (len && copy < have); - if (state->flags & 0x0200) + if ((state->flags & 0x0200) && (state->wrap & 4)) state->check = crc32(state->check, next, copy); have -= copy; next += copy; @@ -777,10 +788,11 @@ int flush; else if (state->head != Z_NULL) state->head->comment = Z_NULL; state->mode = HCRC; + /* fallthrough */ case HCRC: if (state->flags & 0x0200) { NEEDBITS(16); - if (hold != (state->check & 0xffff)) { + if ((state->wrap & 4) && hold != (state->check & 0xffff)) { strm->msg = (char *)"header crc mismatch"; state->mode = BAD; break; @@ -797,9 +809,10 @@ int flush; #endif case DICTID: NEEDBITS(32); - strm->adler = state->check = REVERSE(hold); + strm->adler = state->check = ZSWAP32(hold); INITBITS(); state->mode = DICT; + /* fallthrough */ case DICT: if (state->havedict == 0) { RESTORE(); @@ -807,8 +820,10 @@ int flush; } strm->adler = state->check = adler32(0L, Z_NULL, 0); state->mode = TYPE; + /* fallthrough */ case TYPE: if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave; + /* fallthrough */ case TYPEDO: if (state->last) { BYTEBITS(); @@ -859,8 +874,10 @@ int flush; INITBITS(); state->mode = COPY_; if (flush == Z_TREES) goto inf_leave; + /* fallthrough */ case COPY_: state->mode = COPY; + /* fallthrough */ case COPY: copy = state->length; if (copy) { @@ -896,6 +913,7 @@ int flush; Tracev((stderr, "inflate: table sizes ok\n")); state->have = 0; state->mode = LENLENS; + /* fallthrough */ case LENLENS: while (state->have < state->ncode) { NEEDBITS(3); @@ -905,7 +923,7 @@ int flush; while (state->have < 19) state->lens[order[state->have++]] = 0; state->next = state->codes; - state->lencode = (code const FAR *)(state->next); + state->lencode = (const code FAR *)(state->next); state->lenbits = 7; ret = inflate_table(CODES, state->lens, 19, &(state->next), &(state->lenbits), state->work); @@ -917,6 +935,7 @@ int flush; Tracev((stderr, "inflate: code lengths ok\n")); state->have = 0; state->mode = CODELENS; + /* fallthrough */ case CODELENS: while (state->have < state->nlen + state->ndist) { for (;;) { @@ -925,7 +944,6 @@ int flush; PULLBYTE(); } if (here.val < 16) { - NEEDBITS(here.bits); DROPBITS(here.bits); state->lens[state->have++] = here.val; } @@ -980,7 +998,7 @@ int flush; values here (9 and 6) without reading the comments in inftrees.h concerning the ENOUGH constants, which depend on those values */ state->next = state->codes; - state->lencode = (code const FAR *)(state->next); + state->lencode = (const code FAR *)(state->next); state->lenbits = 9; ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), &(state->lenbits), state->work); @@ -989,7 +1007,7 @@ int flush; state->mode = BAD; break; } - state->distcode = (code const FAR *)(state->next); + state->distcode = (const code FAR *)(state->next); state->distbits = 6; ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, &(state->next), &(state->distbits), state->work); @@ -1001,8 +1019,10 @@ int flush; Tracev((stderr, "inflate: codes ok\n")); state->mode = LEN_; if (flush == Z_TREES) goto inf_leave; + /* fallthrough */ case LEN_: state->mode = LEN; + /* fallthrough */ case LEN: if (have >= 6 && left >= 258) { RESTORE(); @@ -1052,6 +1072,7 @@ int flush; } state->extra = (unsigned)(here.op) & 15; state->mode = LENEXT; + /* fallthrough */ case LENEXT: if (state->extra) { NEEDBITS(state->extra); @@ -1062,6 +1083,7 @@ int flush; Tracevv((stderr, "inflate: length %u\n", state->length)); state->was = state->length; state->mode = DIST; + /* fallthrough */ case DIST: for (;;) { here = state->distcode[BITS(state->distbits)]; @@ -1089,6 +1111,7 @@ int flush; state->offset = (unsigned)here.val; state->extra = (unsigned)(here.op) & 15; state->mode = DISTEXT; + /* fallthrough */ case DISTEXT: if (state->extra) { NEEDBITS(state->extra); @@ -1105,6 +1128,7 @@ int flush; #endif Tracevv((stderr, "inflate: distance %u\n", state->offset)); state->mode = MATCH; + /* fallthrough */ case MATCH: if (left == 0) goto inf_leave; copy = out - left; @@ -1162,15 +1186,15 @@ int flush; out -= left; strm->total_out += out; state->total += out; - if (out) + if ((state->wrap & 4) && out) strm->adler = state->check = - UPDATE(state->check, put - out, out); + UPDATE_CHECK(state->check, put - out, out); out = left; - if (( + if ((state->wrap & 4) && ( #ifdef GUNZIP state->flags ? hold : #endif - REVERSE(hold)) != state->check) { + ZSWAP32(hold)) != state->check) { strm->msg = (char *)"incorrect data check"; state->mode = BAD; break; @@ -1180,10 +1204,11 @@ int flush; } #ifdef GUNZIP state->mode = LENGTH; + /* fallthrough */ case LENGTH: if (state->wrap && state->flags) { NEEDBITS(32); - if (hold != (state->total & 0xffffffffUL)) { + if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) { strm->msg = (char *)"incorrect length check"; state->mode = BAD; break; @@ -1193,6 +1218,7 @@ int flush; } #endif state->mode = DONE; + /* fallthrough */ case DONE: ret = Z_STREAM_END; goto inf_leave; @@ -1202,6 +1228,7 @@ int flush; case MEM: return Z_MEM_ERROR; case SYNC: + /* fallthrough */ default: return Z_STREAM_ERROR; } @@ -1214,8 +1241,9 @@ int flush; */ inf_leave: RESTORE(); - if (state->wsize || (state->mode < CHECK && out != strm->avail_out)) - if (updatewindow(strm, out)) { + if (state->wsize || (out != strm->avail_out && state->mode < BAD && + (state->mode < CHECK || flush != Z_FINISH))) + if (updatewindow(strm, strm->next_out, out - strm->avail_out)) { state->mode = MEM; return Z_MEM_ERROR; } @@ -1224,10 +1252,10 @@ int flush; strm->total_in += in; strm->total_out += out; state->total += out; - if (state->wrap && out) + if ((state->wrap & 4) && out) strm->adler = state->check = - UPDATE(state->check, strm->next_out - out, out); - strm->data_type = state->bits + (state->last ? 64 : 0) + + UPDATE_CHECK(state->check, strm->next_out - out, out); + strm->data_type = (int)state->bits + (state->last ? 64 : 0) + (state->mode == TYPE ? 128 : 0) + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) @@ -1235,11 +1263,9 @@ int flush; return ret; } -int ZEXPORT inflateEnd(strm) -z_streamp strm; -{ +int ZEXPORT inflateEnd(z_streamp strm) { struct inflate_state FAR *state; - if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; state = (struct inflate_state FAR *)strm->state; if (state->window != Z_NULL) ZFREE(strm, state->window); @@ -1249,56 +1275,63 @@ z_streamp strm; return Z_OK; } -int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) -z_streamp strm; -const Bytef *dictionary; -uInt dictLength; -{ +int ZEXPORT inflateGetDictionary(z_streamp strm, Bytef *dictionary, + uInt *dictLength) { + struct inflate_state FAR *state; + + /* check state */ + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + + /* copy dictionary */ + if (state->whave && dictionary != Z_NULL) { + zmemcpy(dictionary, state->window + state->wnext, + state->whave - state->wnext); + zmemcpy(dictionary + state->whave - state->wnext, + state->window, state->wnext); + } + if (dictLength != Z_NULL) + *dictLength = state->whave; + return Z_OK; +} + +int ZEXPORT inflateSetDictionary(z_streamp strm, const Bytef *dictionary, + uInt dictLength) { struct inflate_state FAR *state; - unsigned long id; + unsigned long dictid; + int ret; /* check state */ - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; state = (struct inflate_state FAR *)strm->state; if (state->wrap != 0 && state->mode != DICT) return Z_STREAM_ERROR; - /* check for correct dictionary id */ + /* check for correct dictionary identifier */ if (state->mode == DICT) { - id = adler32(0L, Z_NULL, 0); - id = adler32(id, dictionary, dictLength); - if (id != state->check) + dictid = adler32(0L, Z_NULL, 0); + dictid = adler32(dictid, dictionary, dictLength); + if (dictid != state->check) return Z_DATA_ERROR; } - /* copy dictionary to window */ - if (updatewindow(strm, strm->avail_out)) { + /* copy dictionary to window using updatewindow(), which will amend the + existing dictionary if appropriate */ + ret = updatewindow(strm, dictionary + dictLength, dictLength); + if (ret) { state->mode = MEM; return Z_MEM_ERROR; } - if (dictLength > state->wsize) { - zmemcpy(state->window, dictionary + dictLength - state->wsize, - state->wsize); - state->whave = state->wsize; - } - else { - zmemcpy(state->window + state->wsize - dictLength, dictionary, - dictLength); - state->whave = dictLength; - } state->havedict = 1; Tracev((stderr, "inflate: dictionary set\n")); return Z_OK; } -int ZEXPORT inflateGetHeader(strm, head) -z_streamp strm; -gz_headerp head; -{ +int ZEXPORT inflateGetHeader(z_streamp strm, gz_headerp head) { struct inflate_state FAR *state; /* check state */ - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; state = (struct inflate_state FAR *)strm->state; if ((state->wrap & 2) == 0) return Z_STREAM_ERROR; @@ -1319,11 +1352,8 @@ gz_headerp head; called again with more data and the *have state. *have is initialized to zero for the first call. */ -local unsigned syncsearch(have, buf, len) -unsigned FAR *have; -unsigned char FAR *buf; -unsigned len; -{ +local unsigned syncsearch(unsigned FAR *have, const unsigned char FAR *buf, + unsigned len) { unsigned got; unsigned next; @@ -1342,23 +1372,22 @@ unsigned len; return next; } -int ZEXPORT inflateSync(strm) -z_streamp strm; -{ +int ZEXPORT inflateSync(z_streamp strm) { unsigned len; /* number of bytes to look at or looked at */ + int flags; /* temporary to save header status */ unsigned long in, out; /* temporary to save total_in and total_out */ unsigned char buf[4]; /* to restore bit buffer to byte string */ struct inflate_state FAR *state; /* check parameters */ - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; state = (struct inflate_state FAR *)strm->state; if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; /* if first time, start search in bit buffer */ if (state->mode != SYNC) { state->mode = SYNC; - state->hold <<= state->bits & 7; + state->hold >>= state->bits & 7; state->bits -= state->bits & 7; len = 0; while (state->bits >= 8) { @@ -1378,9 +1407,15 @@ z_streamp strm; /* return no joy or set up to restart inflate() on a new block */ if (state->have != 4) return Z_DATA_ERROR; + if (state->flags == -1) + state->wrap = 0; /* if no header yet, treat as raw */ + else + state->wrap &= ~4; /* no point in computing a check value now */ + flags = state->flags; in = strm->total_in; out = strm->total_out; inflateReset(strm); strm->total_in = in; strm->total_out = out; + state->flags = flags; state->mode = TYPE; return Z_OK; } @@ -1393,28 +1428,22 @@ z_streamp strm; block. When decompressing, PPP checks that at the end of input packet, inflate is waiting for these length bytes. */ -int ZEXPORT inflateSyncPoint(strm) -z_streamp strm; -{ +int ZEXPORT inflateSyncPoint(z_streamp strm) { struct inflate_state FAR *state; - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; state = (struct inflate_state FAR *)strm->state; return state->mode == STORED && state->bits == 0; } -int ZEXPORT inflateCopy(dest, source) -z_streamp dest; -z_streamp source; -{ +int ZEXPORT inflateCopy(z_streamp dest, z_streamp source) { struct inflate_state FAR *state; struct inflate_state FAR *copy; unsigned char FAR *window; unsigned wsize; /* check input */ - if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL || - source->zalloc == (alloc_func)0 || source->zfree == (free_func)0) + if (inflateStateCheck(source) || dest == Z_NULL) return Z_STREAM_ERROR; state = (struct inflate_state FAR *)source->state; @@ -1433,8 +1462,9 @@ z_streamp source; } /* copy state */ - zmemcpy(dest, source, sizeof(z_stream)); - zmemcpy(copy, state, sizeof(struct inflate_state)); + zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); + zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state)); + copy->strm = dest; if (state->lencode >= state->codes && state->lencode <= state->codes + ENOUGH - 1) { copy->lencode = copy->codes + (state->lencode - state->codes); @@ -1450,31 +1480,47 @@ z_streamp source; return Z_OK; } -int ZEXPORT inflateUndermine(strm, subvert) -z_streamp strm; -int subvert; -{ +int ZEXPORT inflateUndermine(z_streamp strm, int subvert) { struct inflate_state FAR *state; - if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; state = (struct inflate_state FAR *)strm->state; - state->sane = !subvert; #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR + state->sane = !subvert; return Z_OK; #else + (void)subvert; state->sane = 1; return Z_DATA_ERROR; #endif } -long ZEXPORT inflateMark(strm) -z_streamp strm; -{ +int ZEXPORT inflateValidate(z_streamp strm, int check) { struct inflate_state FAR *state; - if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16; + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; state = (struct inflate_state FAR *)strm->state; - return ((long)(state->back) << 16) + + if (check && state->wrap) + state->wrap |= 4; + else + state->wrap &= ~4; + return Z_OK; +} + +long ZEXPORT inflateMark(z_streamp strm) { + struct inflate_state FAR *state; + + if (inflateStateCheck(strm)) + return -(1L << 16); + state = (struct inflate_state FAR *)strm->state; + return (long)(((unsigned long)((long)state->back)) << 16) + (state->mode == COPY ? state->length : (state->mode == MATCH ? state->was - state->length : 0)); } + +unsigned long ZEXPORT inflateCodesUsed(z_streamp strm) { + struct inflate_state FAR *state; + if (inflateStateCheck(strm)) return (unsigned long)-1; + state = (struct inflate_state FAR *)strm->state; + return (unsigned long)(state->next - state->codes); +} diff --git a/deps/zlib/inflate.h b/deps/zlib/inflate.h index 95f4986d400..f127b6b1fa5 100644 --- a/deps/zlib/inflate.h +++ b/deps/zlib/inflate.h @@ -1,5 +1,5 @@ /* inflate.h -- internal inflate state definition - * Copyright (C) 1995-2009 Mark Adler + * Copyright (C) 1995-2019 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -18,7 +18,7 @@ /* Possible inflate modes between inflate() calls */ typedef enum { - HEAD, /* i: waiting for magic header */ + HEAD = 16180, /* i: waiting for magic header */ FLAGS, /* i: waiting for method and flags (gzip) */ TIME, /* i: waiting for modification time (gzip) */ OS, /* i: waiting for extra flags and operating system (gzip) */ @@ -77,13 +77,17 @@ typedef enum { CHECK -> LENGTH -> DONE */ -/* state maintained between inflate() calls. Approximately 10K bytes. */ +/* State maintained between inflate() calls -- approximately 7K bytes, not + including the allocated sliding window, which is up to 32K bytes. */ struct inflate_state { + z_streamp strm; /* pointer back to this zlib stream */ inflate_mode mode; /* current inflate mode */ int last; /* true if processing last block */ - int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ + int wrap; /* bit 0 true for zlib, bit 1 true for gzip, + bit 2 true to validate check value */ int havedict; /* true if dictionary provided */ - int flags; /* gzip header method and flags (0 if zlib) */ + int flags; /* gzip header method and flags, 0 if zlib, or + -1 if raw or no header yet */ unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ unsigned long check; /* protected copy of check value */ unsigned long total; /* protected copy of output count */ diff --git a/deps/zlib/inftrees.c b/deps/zlib/inftrees.c index 11e9c52accb..98cfe164458 100644 --- a/deps/zlib/inftrees.c +++ b/deps/zlib/inftrees.c @@ -1,5 +1,5 @@ /* inftrees.c -- generate Huffman trees for efficient decoding - * Copyright (C) 1995-2010 Mark Adler + * Copyright (C) 1995-2024 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -9,7 +9,7 @@ #define MAXBITS 15 const char inflate_copyright[] = - " inflate 1.2.5 Copyright 1995-2010 Mark Adler "; + " inflate 1.3.1 Copyright 1995-2024 Mark Adler "; /* If you use the zlib library in a product, an acknowledgment is welcome in the documentation of your product. If for some reason you cannot @@ -29,14 +29,9 @@ const char inflate_copyright[] = table index bits. It will differ if the request is greater than the longest code or if it is less than the shortest code. */ -int ZLIB_INTERNAL inflate_table(type, lens, codes, table, bits, work) -codetype type; -unsigned short FAR *lens; -unsigned codes; -code FAR * FAR *table; -unsigned FAR *bits; -unsigned short FAR *work; -{ +int ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens, + unsigned codes, code FAR * FAR *table, + unsigned FAR *bits, unsigned short FAR *work) { unsigned len; /* a code's length in bits */ unsigned sym; /* index of code symbols */ unsigned min, max; /* minimum and maximum code lengths */ @@ -54,7 +49,7 @@ unsigned short FAR *work; code FAR *next; /* next available space in table */ const unsigned short FAR *base; /* base value table to use */ const unsigned short FAR *extra; /* extra bits table to use */ - int end; /* use base and extra for symbol > end */ + unsigned match; /* use base and extra for symbol >= match */ unsigned short count[MAXBITS+1]; /* number of codes of each length */ unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ static const unsigned short lbase[31] = { /* Length codes 257..285 base */ @@ -62,7 +57,7 @@ unsigned short FAR *work; 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; static const unsigned short lext[31] = { /* Length codes 257..285 extra */ 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, - 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 73, 195}; + 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 203, 77}; static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, @@ -181,19 +176,17 @@ unsigned short FAR *work; switch (type) { case CODES: base = extra = work; /* dummy value--not used */ - end = 19; + match = 20; break; case LENS: base = lbase; - base -= 257; extra = lext; - extra -= 257; - end = 256; + match = 257; break; - default: /* DISTS */ + default: /* DISTS */ base = dbase; extra = dext; - end = -1; + match = 0; } /* initialize state for loop */ @@ -208,21 +201,21 @@ unsigned short FAR *work; mask = used - 1; /* mask for comparing low */ /* check available table space */ - if ((type == LENS && used >= ENOUGH_LENS) || - (type == DISTS && used >= ENOUGH_DISTS)) + if ((type == LENS && used > ENOUGH_LENS) || + (type == DISTS && used > ENOUGH_DISTS)) return 1; /* process all codes and make table entries */ for (;;) { /* create table entry */ here.bits = (unsigned char)(len - drop); - if ((int)(work[sym]) < end) { + if (work[sym] + 1U < match) { here.op = (unsigned char)0; here.val = work[sym]; } - else if ((int)(work[sym]) > end) { - here.op = (unsigned char)(extra[work[sym]]); - here.val = base[work[sym]]; + else if (work[sym] >= match) { + here.op = (unsigned char)(extra[work[sym] - match]); + here.val = base[work[sym] - match]; } else { here.op = (unsigned char)(32 + 64); /* end of block */ @@ -277,8 +270,8 @@ unsigned short FAR *work; /* check for enough space */ used += 1U << curr; - if ((type == LENS && used >= ENOUGH_LENS) || - (type == DISTS && used >= ENOUGH_DISTS)) + if ((type == LENS && used > ENOUGH_LENS) || + (type == DISTS && used > ENOUGH_DISTS)) return 1; /* point entry in root table to sub-table */ @@ -289,38 +282,14 @@ unsigned short FAR *work; } } - /* - Fill in rest of table for incomplete codes. This loop is similar to the - loop above in incrementing huff for table indices. It is assumed that - len is equal to curr + drop, so there is no loop needed to increment - through high index bits. When the current sub-table is filled, the loop - drops back to the root table to fill in any remaining entries there. - */ - here.op = (unsigned char)64; /* invalid code marker */ - here.bits = (unsigned char)(len - drop); - here.val = (unsigned short)0; - while (huff != 0) { - /* when done with sub-table, drop back to root table */ - if (drop != 0 && (huff & mask) != low) { - drop = 0; - len = root; - next = *table; - here.bits = (unsigned char)len; - } - - /* put invalid code marker in table */ - next[huff >> drop] = here; - - /* backwards increment the len-bit code huff */ - incr = 1U << (len - 1); - while (huff & incr) - incr >>= 1; - if (incr != 0) { - huff &= incr - 1; - huff += incr; - } - else - huff = 0; + /* fill in remaining table entry if code is incomplete (guaranteed to have + at most one remaining entry, since if the code is incomplete, the + maximum code length that was allowed to get this far is one bit) */ + if (huff != 0) { + here.op = (unsigned char)64; /* invalid code marker */ + here.bits = (unsigned char)(len - drop); + here.val = (unsigned short)0; + next[huff] = here; } /* set return parameters */ diff --git a/deps/zlib/inftrees.h b/deps/zlib/inftrees.h index baa53a0b1a1..396f74b5da7 100644 --- a/deps/zlib/inftrees.h +++ b/deps/zlib/inftrees.h @@ -38,11 +38,11 @@ typedef struct { /* Maximum size of the dynamic table. The maximum number of code structures is 1444, which is the sum of 852 for literal/length codes and 592 for distance codes. These values were found by exhaustive searches using the program - examples/enough.c found in the zlib distribtution. The arguments to that + examples/enough.c found in the zlib distribution. The arguments to that program are the number of symbols, the initial root table size, and the maximum bit length of a code. "enough 286 9 15" for literal/length codes - returns returns 852, and "enough 30 6 15" for distance codes returns 592. - The initial root table size (9 or 6) is found in the fifth argument of the + returns 852, and "enough 30 6 15" for distance codes returns 592. The + initial root table size (9 or 6) is found in the fifth argument of the inflate_table() calls in inflate.c and infback.c. If the root table size is changed, then these maximum sizes would be need to be recalculated and updated. */ @@ -57,6 +57,6 @@ typedef enum { DISTS } codetype; -int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens, - unsigned codes, code FAR * FAR *table, - unsigned FAR *bits, unsigned short FAR *work)); +int ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens, + unsigned codes, code FAR * FAR *table, + unsigned FAR *bits, unsigned short FAR *work); diff --git a/deps/zlib/trees.c b/deps/zlib/trees.c index 3e9a138c7ef..6a523ef34e3 100644 --- a/deps/zlib/trees.c +++ b/deps/zlib/trees.c @@ -1,5 +1,5 @@ /* trees.c -- output deflated data using Huffman coding - * Copyright (C) 1995-2010 Jean-loup Gailly + * Copyright (C) 1995-2024 Jean-loup Gailly * detect_data_type() function provided freely by Cosmin Truta, 2006 * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -36,7 +36,7 @@ #include "deflate.h" -#ifdef DEBUG +#ifdef ZLIB_DEBUG # include #endif @@ -74,11 +74,6 @@ local const uch bl_order[BL_CODES] * probability, to avoid transmitting the lengths for unused bit length codes. */ -#define Buf_size (8 * 2*sizeof(char)) -/* Number of bits used within bi_buf. (bi_buf might be implemented on - * more than 16 bits on some systems.) - */ - /* =========================================================================== * Local data. These are initialized only once. */ @@ -127,80 +122,140 @@ struct static_tree_desc_s { int max_length; /* max bit length for the codes */ }; -local static_tree_desc static_l_desc = +#ifdef NO_INIT_GLOBAL_POINTERS +# define TCONST +#else +# define TCONST const +#endif + +local TCONST static_tree_desc static_l_desc = {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS}; -local static_tree_desc static_d_desc = +local TCONST static_tree_desc static_d_desc = {static_dtree, extra_dbits, 0, D_CODES, MAX_BITS}; -local static_tree_desc static_bl_desc = +local TCONST static_tree_desc static_bl_desc = {(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS}; /* =========================================================================== - * Local (static) routines in this file. + * Output a short LSB first on the stream. + * IN assertion: there is enough room in pendingBuf. */ +#define put_short(s, w) { \ + put_byte(s, (uch)((w) & 0xff)); \ + put_byte(s, (uch)((ush)(w) >> 8)); \ +} -local void tr_static_init OF((void)); -local void init_block OF((deflate_state *s)); -local void pqdownheap OF((deflate_state *s, ct_data *tree, int k)); -local void gen_bitlen OF((deflate_state *s, tree_desc *desc)); -local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count)); -local void build_tree OF((deflate_state *s, tree_desc *desc)); -local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code)); -local void send_tree OF((deflate_state *s, ct_data *tree, int max_code)); -local int build_bl_tree OF((deflate_state *s)); -local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes, - int blcodes)); -local void compress_block OF((deflate_state *s, ct_data *ltree, - ct_data *dtree)); -local int detect_data_type OF((deflate_state *s)); -local unsigned bi_reverse OF((unsigned value, int length)); -local void bi_windup OF((deflate_state *s)); -local void bi_flush OF((deflate_state *s)); -local void copy_block OF((deflate_state *s, charf *buf, unsigned len, - int header)); +/* =========================================================================== + * Reverse the first len bits of a code, using straightforward code (a faster + * method would use a table) + * IN assertion: 1 <= len <= 15 + */ +local unsigned bi_reverse(unsigned code, int len) { + register unsigned res = 0; + do { + res |= code & 1; + code >>= 1, res <<= 1; + } while (--len > 0); + return res >> 1; +} + +/* =========================================================================== + * Flush the bit buffer, keeping at most 7 bits in it. + */ +local void bi_flush(deflate_state *s) { + if (s->bi_valid == 16) { + put_short(s, s->bi_buf); + s->bi_buf = 0; + s->bi_valid = 0; + } else if (s->bi_valid >= 8) { + put_byte(s, (Byte)s->bi_buf); + s->bi_buf >>= 8; + s->bi_valid -= 8; + } +} + +/* =========================================================================== + * Flush the bit buffer and align the output on a byte boundary + */ +local void bi_windup(deflate_state *s) { + if (s->bi_valid > 8) { + put_short(s, s->bi_buf); + } else if (s->bi_valid > 0) { + put_byte(s, (Byte)s->bi_buf); + } + s->bi_buf = 0; + s->bi_valid = 0; +#ifdef ZLIB_DEBUG + s->bits_sent = (s->bits_sent + 7) & ~7; +#endif +} + +/* =========================================================================== + * Generate the codes for a given tree and bit counts (which need not be + * optimal). + * IN assertion: the array bl_count contains the bit length statistics for + * the given tree and the field len is set for all tree elements. + * OUT assertion: the field code is set for all tree elements of non + * zero code length. + */ +local void gen_codes(ct_data *tree, int max_code, ushf *bl_count) { + ush next_code[MAX_BITS+1]; /* next code value for each bit length */ + unsigned code = 0; /* running code value */ + int bits; /* bit index */ + int n; /* code index */ + + /* The distribution counts are first used to generate the code values + * without bit reversal. + */ + for (bits = 1; bits <= MAX_BITS; bits++) { + code = (code + bl_count[bits - 1]) << 1; + next_code[bits] = (ush)code; + } + /* Check that the bit counts in bl_count are consistent. The last code + * must be all ones. + */ + Assert (code + bl_count[MAX_BITS] - 1 == (1 << MAX_BITS) - 1, + "inconsistent bit counts"); + Tracev((stderr,"\ngen_codes: max_code %d ", max_code)); + + for (n = 0; n <= max_code; n++) { + int len = tree[n].Len; + if (len == 0) continue; + /* Now reverse the bits */ + tree[n].Code = (ush)bi_reverse(next_code[len]++, len); + + Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", + n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len] - 1)); + } +} #ifdef GEN_TREES_H -local void gen_trees_header OF((void)); +local void gen_trees_header(void); #endif -#ifndef DEBUG +#ifndef ZLIB_DEBUG # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) /* Send a code of the given tree. c and tree must not have side effects */ -#else /* DEBUG */ +#else /* !ZLIB_DEBUG */ # define send_code(s, c, tree) \ { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \ send_bits(s, tree[c].Code, tree[c].Len); } #endif -/* =========================================================================== - * Output a short LSB first on the stream. - * IN assertion: there is enough room in pendingBuf. - */ -#define put_short(s, w) { \ - put_byte(s, (uch)((w) & 0xff)); \ - put_byte(s, (uch)((ush)(w) >> 8)); \ -} - /* =========================================================================== * Send a value on a given number of bits. * IN assertion: length <= 16 and value fits in length bits. */ -#ifdef DEBUG -local void send_bits OF((deflate_state *s, int value, int length)); - -local void send_bits(s, value, length) - deflate_state *s; - int value; /* value to send */ - int length; /* number of bits */ -{ +#ifdef ZLIB_DEBUG +local void send_bits(deflate_state *s, int value, int length) { Tracevv((stderr," l %2d v %4x ", length, value)); Assert(length > 0 && length <= 15, "invalid length"); s->bits_sent += (ulg)length; /* If not enough room in bi_buf, use (valid) bits from bi_buf and - * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid)) + * (16 - bi_valid) bits from value, leaving (width - (16 - bi_valid)) * unused bits in value. */ if (s->bi_valid > (int)Buf_size - length) { @@ -213,12 +268,12 @@ local void send_bits(s, value, length) s->bi_valid += length; } } -#else /* !DEBUG */ +#else /* !ZLIB_DEBUG */ #define send_bits(s, value, length) \ { int len = length;\ if (s->bi_valid > (int)Buf_size - len) {\ - int val = value;\ + int val = (int)value;\ s->bi_buf |= (ush)val << s->bi_valid;\ put_short(s, s->bi_buf);\ s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\ @@ -228,7 +283,7 @@ local void send_bits(s, value, length) s->bi_valid += len;\ }\ } -#endif /* DEBUG */ +#endif /* ZLIB_DEBUG */ /* the arguments must not have side effects */ @@ -236,8 +291,7 @@ local void send_bits(s, value, length) /* =========================================================================== * Initialize the various 'constant' tables. */ -local void tr_static_init() -{ +local void tr_static_init(void) { #if defined(GEN_TREES_H) || !defined(STDC) static int static_init_done = 0; int n; /* iterates over tree elements */ @@ -263,7 +317,7 @@ local void tr_static_init() length = 0; for (code = 0; code < LENGTH_CODES-1; code++) { base_length[code] = length; - for (n = 0; n < (1< dist code (0..29) */ dist = 0; for (code = 0 ; code < 16; code++) { base_dist[code] = dist; - for (n = 0; n < (1<>= 7; /* from now on, all distances are divided by 128 */ for ( ; code < D_CODES; code++) { base_dist[code] = dist << 7; - for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) { + for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) { _dist_code[256 + dist++] = (uch)code; } } - Assert (dist == 256, "tr_static_init: 256+dist != 512"); + Assert (dist == 256, "tr_static_init: 256 + dist != 512"); /* Construct the codes of the static literal tree */ for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; @@ -319,19 +373,18 @@ local void tr_static_init() } /* =========================================================================== - * Genererate the file trees.h describing the static trees. + * Generate the file trees.h describing the static trees. */ #ifdef GEN_TREES_H -# ifndef DEBUG +# ifndef ZLIB_DEBUG # include # endif # define SEPARATOR(i, last, width) \ ((i) == (last)? "\n};\n\n" : \ - ((i) % (width) == (width)-1 ? ",\n" : ", ")) + ((i) % (width) == (width) - 1 ? ",\n" : ", ")) -void gen_trees_header() -{ +void gen_trees_header(void) { FILE *header = fopen("trees.h", "w"); int i; @@ -380,12 +433,26 @@ void gen_trees_header() } #endif /* GEN_TREES_H */ +/* =========================================================================== + * Initialize a new block. + */ +local void init_block(deflate_state *s) { + int n; /* iterates over tree elements */ + + /* Initialize the trees. */ + for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; + for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; + for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; + + s->dyn_ltree[END_BLOCK].Freq = 1; + s->opt_len = s->static_len = 0L; + s->sym_next = s->matches = 0; +} + /* =========================================================================== * Initialize the tree data structures for a new zlib stream. */ -void ZLIB_INTERNAL _tr_init(s) - deflate_state *s; -{ +void ZLIB_INTERNAL _tr_init(deflate_state *s) { tr_static_init(); s->l_desc.dyn_tree = s->dyn_ltree; @@ -399,8 +466,7 @@ void ZLIB_INTERNAL _tr_init(s) s->bi_buf = 0; s->bi_valid = 0; - s->last_eob_len = 8; /* enough lookahead for inflate */ -#ifdef DEBUG +#ifdef ZLIB_DEBUG s->compressed_len = 0L; s->bits_sent = 0L; #endif @@ -409,24 +475,6 @@ void ZLIB_INTERNAL _tr_init(s) init_block(s); } -/* =========================================================================== - * Initialize a new block. - */ -local void init_block(s) - deflate_state *s; -{ - int n; /* iterates over tree elements */ - - /* Initialize the trees. */ - for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; - for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; - for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; - - s->dyn_ltree[END_BLOCK].Freq = 1; - s->opt_len = s->static_len = 0L; - s->last_lit = s->matches = 0; -} - #define SMALLEST 1 /* Index within the heap array of least frequent node in the Huffman tree */ @@ -456,17 +504,13 @@ local void init_block(s) * when the heap property is re-established (each father smaller than its * two sons). */ -local void pqdownheap(s, tree, k) - deflate_state *s; - ct_data *tree; /* the tree to restore */ - int k; /* node to move down */ -{ +local void pqdownheap(deflate_state *s, ct_data *tree, int k) { int v = s->heap[k]; int j = k << 1; /* left son of k */ while (j <= s->heap_len) { /* Set j to the smallest of the two sons: */ if (j < s->heap_len && - smaller(tree, s->heap[j+1], s->heap[j], s->depth)) { + smaller(tree, s->heap[j + 1], s->heap[j], s->depth)) { j++; } /* Exit if v is smaller than both sons */ @@ -491,10 +535,7 @@ local void pqdownheap(s, tree, k) * The length opt_len is updated; static_len is also updated if stree is * not null. */ -local void gen_bitlen(s, desc) - deflate_state *s; - tree_desc *desc; /* the tree descriptor */ -{ +local void gen_bitlen(deflate_state *s, tree_desc *desc) { ct_data *tree = desc->dyn_tree; int max_code = desc->max_code; const ct_data *stree = desc->stat_desc->static_tree; @@ -515,7 +556,7 @@ local void gen_bitlen(s, desc) */ tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */ - for (h = s->heap_max+1; h < HEAP_SIZE; h++) { + for (h = s->heap_max + 1; h < HEAP_SIZE; h++) { n = s->heap[h]; bits = tree[tree[n].Dad].Len + 1; if (bits > max_length) bits = max_length, overflow++; @@ -526,22 +567,22 @@ local void gen_bitlen(s, desc) s->bl_count[bits]++; xbits = 0; - if (n >= base) xbits = extra[n-base]; + if (n >= base) xbits = extra[n - base]; f = tree[n].Freq; - s->opt_len += (ulg)f * (bits + xbits); - if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits); + s->opt_len += (ulg)f * (unsigned)(bits + xbits); + if (stree) s->static_len += (ulg)f * (unsigned)(stree[n].Len + xbits); } if (overflow == 0) return; - Trace((stderr,"\nbit length overflow\n")); + Tracev((stderr,"\nbit length overflow\n")); /* This happens for example on obj2 and pic of the Calgary corpus */ /* Find the first bit length which could increase: */ do { - bits = max_length-1; + bits = max_length - 1; while (s->bl_count[bits] == 0) bits--; - s->bl_count[bits]--; /* move one leaf down the tree */ - s->bl_count[bits+1] += 2; /* move one overflow item as its brother */ + s->bl_count[bits]--; /* move one leaf down the tree */ + s->bl_count[bits + 1] += 2; /* move one overflow item as its brother */ s->bl_count[max_length]--; /* The brother of the overflow item also moves one step up, * but this does not affect bl_count[max_length] @@ -560,9 +601,8 @@ local void gen_bitlen(s, desc) m = s->heap[--h]; if (m > max_code) continue; if ((unsigned) tree[m].Len != (unsigned) bits) { - Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); - s->opt_len += ((long)bits - (long)tree[m].Len) - *(long)tree[m].Freq; + Tracev((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); + s->opt_len += ((ulg)bits - tree[m].Len) * tree[m].Freq; tree[m].Len = (ush)bits; } n--; @@ -570,47 +610,9 @@ local void gen_bitlen(s, desc) } } -/* =========================================================================== - * Generate the codes for a given tree and bit counts (which need not be - * optimal). - * IN assertion: the array bl_count contains the bit length statistics for - * the given tree and the field len is set for all tree elements. - * OUT assertion: the field code is set for all tree elements of non - * zero code length. - */ -local void gen_codes (tree, max_code, bl_count) - ct_data *tree; /* the tree to decorate */ - int max_code; /* largest code with non zero frequency */ - ushf *bl_count; /* number of codes at each bit length */ -{ - ush next_code[MAX_BITS+1]; /* next code value for each bit length */ - ush code = 0; /* running code value */ - int bits; /* bit index */ - int n; /* code index */ - - /* The distribution counts are first used to generate the code values - * without bit reversal. - */ - for (bits = 1; bits <= MAX_BITS; bits++) { - next_code[bits] = code = (code + bl_count[bits-1]) << 1; - } - /* Check that the bit counts in bl_count are consistent. The last code - * must be all ones. - */ - Assert (code + bl_count[MAX_BITS]-1 == (1< +#endif /* =========================================================================== * Construct one Huffman tree and assigns the code bit strings and lengths. @@ -620,10 +622,7 @@ local void gen_codes (tree, max_code, bl_count) * and corresponding code. The length opt_len is updated; static_len is * also updated if stree is not null. The field max_code is set. */ -local void build_tree(s, desc) - deflate_state *s; - tree_desc *desc; /* the tree descriptor */ -{ +local void build_tree(deflate_state *s, tree_desc *desc) { ct_data *tree = desc->dyn_tree; const ct_data *stree = desc->stat_desc->static_tree; int elems = desc->stat_desc->elems; @@ -632,7 +631,7 @@ local void build_tree(s, desc) int node; /* new node being created */ /* Construct the initial heap, with least frequent element in - * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. + * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n + 1]. * heap[0] is not used. */ s->heap_len = 0, s->heap_max = HEAP_SIZE; @@ -660,7 +659,7 @@ local void build_tree(s, desc) } desc->max_code = max_code; - /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, + /* The elements heap[heap_len/2 + 1 .. heap_len] are leaves of the tree, * establish sub-heaps of increasing lengths: */ for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n); @@ -708,11 +707,7 @@ local void build_tree(s, desc) * Scan a literal or distance tree to determine the frequencies of the codes * in the bit length tree. */ -local void scan_tree (s, tree, max_code) - deflate_state *s; - ct_data *tree; /* the tree to be scanned */ - int max_code; /* and its largest code of non zero frequency */ -{ +local void scan_tree(deflate_state *s, ct_data *tree, int max_code) { int n; /* iterates over all tree elements */ int prevlen = -1; /* last emitted length */ int curlen; /* length of current code */ @@ -722,14 +717,14 @@ local void scan_tree (s, tree, max_code) int min_count = 4; /* min repeat count */ if (nextlen == 0) max_count = 138, min_count = 3; - tree[max_code+1].Len = (ush)0xffff; /* guard */ + tree[max_code + 1].Len = (ush)0xffff; /* guard */ for (n = 0; n <= max_code; n++) { - curlen = nextlen; nextlen = tree[n+1].Len; + curlen = nextlen; nextlen = tree[n + 1].Len; if (++count < max_count && curlen == nextlen) { continue; } else if (count < min_count) { - s->bl_tree[curlen].Freq += (ush)count; + s->bl_tree[curlen].Freq += count; } else if (curlen != 0) { if (curlen != prevlen) s->bl_tree[curlen].Freq++; s->bl_tree[REP_3_6].Freq++; @@ -753,11 +748,7 @@ local void scan_tree (s, tree, max_code) * Send a literal or distance tree in compressed form, using the codes in * bl_tree. */ -local void send_tree (s, tree, max_code) - deflate_state *s; - ct_data *tree; /* the tree to be scanned */ - int max_code; /* and its largest code of non zero frequency */ -{ +local void send_tree(deflate_state *s, ct_data *tree, int max_code) { int n; /* iterates over all tree elements */ int prevlen = -1; /* last emitted length */ int curlen; /* length of current code */ @@ -766,11 +757,11 @@ local void send_tree (s, tree, max_code) int max_count = 7; /* max repeat count */ int min_count = 4; /* min repeat count */ - /* tree[max_code+1].Len = -1; */ /* guard already set */ + /* tree[max_code + 1].Len = -1; */ /* guard already set */ if (nextlen == 0) max_count = 138, min_count = 3; for (n = 0; n <= max_code; n++) { - curlen = nextlen; nextlen = tree[n+1].Len; + curlen = nextlen; nextlen = tree[n + 1].Len; if (++count < max_count && curlen == nextlen) { continue; } else if (count < min_count) { @@ -781,13 +772,13 @@ local void send_tree (s, tree, max_code) send_code(s, curlen, s->bl_tree); count--; } Assert(count >= 3 && count <= 6, " 3_6?"); - send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2); + send_code(s, REP_3_6, s->bl_tree); send_bits(s, count - 3, 2); } else if (count <= 10) { - send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3); + send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count - 3, 3); } else { - send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7); + send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count - 11, 7); } count = 0; prevlen = curlen; if (nextlen == 0) { @@ -804,9 +795,7 @@ local void send_tree (s, tree, max_code) * Construct the Huffman tree for the bit lengths and return the index in * bl_order of the last bit length code to send. */ -local int build_bl_tree(s) - deflate_state *s; -{ +local int build_bl_tree(deflate_state *s) { int max_blindex; /* index of last bit length code of non zero freq */ /* Determine the bit length frequencies for literal and distance trees */ @@ -815,8 +804,8 @@ local int build_bl_tree(s) /* Build the bit length tree: */ build_tree(s, (tree_desc *)(&(s->bl_desc))); - /* opt_len now includes the length of the tree representations, except - * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. + /* opt_len now includes the length of the tree representations, except the + * lengths of the bit lengths codes and the 5 + 5 + 4 bits for the counts. */ /* Determine the number of bit length codes to send. The pkzip format @@ -827,7 +816,7 @@ local int build_bl_tree(s) if (s->bl_tree[bl_order[max_blindex]].Len != 0) break; } /* Update opt_len to include the bit length tree and counts */ - s->opt_len += 3*(max_blindex+1) + 5+5+4; + s->opt_len += 3*((ulg)max_blindex + 1) + 5 + 5 + 4; Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", s->opt_len, s->static_len)); @@ -839,95 +828,172 @@ local int build_bl_tree(s) * lengths of the bit length codes, the literal tree and the distance tree. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. */ -local void send_all_trees(s, lcodes, dcodes, blcodes) - deflate_state *s; - int lcodes, dcodes, blcodes; /* number of codes for each tree */ -{ +local void send_all_trees(deflate_state *s, int lcodes, int dcodes, + int blcodes) { int rank; /* index in bl_order */ Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, "too many codes"); Tracev((stderr, "\nbl counts: ")); - send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */ - send_bits(s, dcodes-1, 5); - send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */ + send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */ + send_bits(s, dcodes - 1, 5); + send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */ for (rank = 0; rank < blcodes; rank++) { Tracev((stderr, "\nbl code %2d ", bl_order[rank])); send_bits(s, s->bl_tree[bl_order[rank]].Len, 3); } Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); - send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */ + send_tree(s, (ct_data *)s->dyn_ltree, lcodes - 1); /* literal tree */ Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); - send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */ + send_tree(s, (ct_data *)s->dyn_dtree, dcodes - 1); /* distance tree */ Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); } /* =========================================================================== * Send a stored block */ -void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last) - deflate_state *s; - charf *buf; /* input block */ - ulg stored_len; /* length of input block */ - int last; /* one if this is the last block for a file */ -{ - send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */ -#ifdef DEBUG +void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, + ulg stored_len, int last) { + send_bits(s, (STORED_BLOCK<<1) + last, 3); /* send block type */ + bi_windup(s); /* align on byte boundary */ + put_short(s, (ush)stored_len); + put_short(s, (ush)~stored_len); + if (stored_len) + zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len); + s->pending += stored_len; +#ifdef ZLIB_DEBUG s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; s->compressed_len += (stored_len + 4) << 3; + s->bits_sent += 2*16; + s->bits_sent += stored_len << 3; #endif - copy_block(s, buf, (unsigned)stored_len, 1); /* with header */ +} + +/* =========================================================================== + * Flush the bits in the bit buffer to pending output (leaves at most 7 bits) + */ +void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s) { + bi_flush(s); } /* =========================================================================== * Send one empty static block to give enough lookahead for inflate. * This takes 10 bits, of which 7 may remain in the bit buffer. - * The current inflate code requires 9 bits of lookahead. If the - * last two codes for the previous block (real code plus EOB) were coded - * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode - * the last real code. In this case we send two empty static blocks instead - * of one. (There are no problems if the previous block is stored or fixed.) - * To simplify the code, we assume the worst case of last real code encoded - * on one bit only. */ -void ZLIB_INTERNAL _tr_align(s) - deflate_state *s; -{ +void ZLIB_INTERNAL _tr_align(deflate_state *s) { send_bits(s, STATIC_TREES<<1, 3); send_code(s, END_BLOCK, static_ltree); -#ifdef DEBUG +#ifdef ZLIB_DEBUG s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ #endif bi_flush(s); - /* Of the 10 bits for the empty block, we have already sent - * (10 - bi_valid) bits. The lookahead for the last real code (before - * the EOB of the previous block) was thus at least one plus the length - * of the EOB plus what we have just sent of the empty static block. - */ - if (1 + s->last_eob_len + 10 - s->bi_valid < 9) { - send_bits(s, STATIC_TREES<<1, 3); - send_code(s, END_BLOCK, static_ltree); -#ifdef DEBUG - s->compressed_len += 10L; +} + +/* =========================================================================== + * Send the block data compressed using the given Huffman trees + */ +local void compress_block(deflate_state *s, const ct_data *ltree, + const ct_data *dtree) { + unsigned dist; /* distance of matched string */ + int lc; /* match length or unmatched char (if dist == 0) */ + unsigned sx = 0; /* running index in symbol buffers */ + unsigned code; /* the code to send */ + int extra; /* number of extra bits to send */ + + if (s->sym_next != 0) do { +#ifdef LIT_MEM + dist = s->d_buf[sx]; + lc = s->l_buf[sx++]; +#else + dist = s->sym_buf[sx++] & 0xff; + dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8; + lc = s->sym_buf[sx++]; #endif - bi_flush(s); - } - s->last_eob_len = 7; + if (dist == 0) { + send_code(s, lc, ltree); /* send a literal byte */ + Tracecv(isgraph(lc), (stderr," '%c' ", lc)); + } else { + /* Here, lc is the match length - MIN_MATCH */ + code = _length_code[lc]; + send_code(s, code + LITERALS + 1, ltree); /* send length code */ + extra = extra_lbits[code]; + if (extra != 0) { + lc -= base_length[code]; + send_bits(s, lc, extra); /* send the extra length bits */ + } + dist--; /* dist is now the match distance - 1 */ + code = d_code(dist); + Assert (code < D_CODES, "bad d_code"); + + send_code(s, code, dtree); /* send the distance code */ + extra = extra_dbits[code]; + if (extra != 0) { + dist -= (unsigned)base_dist[code]; + send_bits(s, dist, extra); /* send the extra distance bits */ + } + } /* literal or match pair ? */ + + /* Check for no overlay of pending_buf on needed symbols */ +#ifdef LIT_MEM + Assert(s->pending < 2 * (s->lit_bufsize + sx), "pendingBuf overflow"); +#else + Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow"); +#endif + + } while (sx < s->sym_next); + + send_code(s, END_BLOCK, ltree); +} + +/* =========================================================================== + * Check if the data type is TEXT or BINARY, using the following algorithm: + * - TEXT if the two conditions below are satisfied: + * a) There are no non-portable control characters belonging to the + * "block list" (0..6, 14..25, 28..31). + * b) There is at least one printable character belonging to the + * "allow list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). + * - BINARY otherwise. + * - The following partially-portable control characters form a + * "gray list" that is ignored in this detection algorithm: + * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). + * IN assertion: the fields Freq of dyn_ltree are set. + */ +local int detect_data_type(deflate_state *s) { + /* block_mask is the bit mask of block-listed bytes + * set bits 0..6, 14..25, and 28..31 + * 0xf3ffc07f = binary 11110011111111111100000001111111 + */ + unsigned long block_mask = 0xf3ffc07fUL; + int n; + + /* Check for non-textual ("block-listed") bytes. */ + for (n = 0; n <= 31; n++, block_mask >>= 1) + if ((block_mask & 1) && (s->dyn_ltree[n].Freq != 0)) + return Z_BINARY; + + /* Check for textual ("allow-listed") bytes. */ + if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 + || s->dyn_ltree[13].Freq != 0) + return Z_TEXT; + for (n = 32; n < LITERALS; n++) + if (s->dyn_ltree[n].Freq != 0) + return Z_TEXT; + + /* There are no "block-listed" or "allow-listed" bytes: + * this stream either is empty or has tolerated ("gray-listed") bytes only. + */ + return Z_BINARY; } /* =========================================================================== * Determine the best encoding for the current block: dynamic trees, static - * trees or store, and output the encoded block to the zip file. + * trees or store, and write out the encoded block. */ -void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) - deflate_state *s; - charf *buf; /* input block, or NULL if too old */ - ulg stored_len; /* length of input block */ - int last; /* one if this is the last block for a file */ -{ +void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf, + ulg stored_len, int last) { ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ int max_blindex = 0; /* index of last bit length code of non zero freq */ @@ -956,14 +1022,17 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) max_blindex = build_bl_tree(s); /* Determine the best encoding. Compute the block lengths in bytes. */ - opt_lenb = (s->opt_len+3+7)>>3; - static_lenb = (s->static_len+3+7)>>3; + opt_lenb = (s->opt_len + 3 + 7) >> 3; + static_lenb = (s->static_len + 3 + 7) >> 3; Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, - s->last_lit)); + s->sym_next / 3)); - if (static_lenb <= opt_lenb) opt_lenb = static_lenb; +#ifndef FORCE_STATIC + if (static_lenb <= opt_lenb || s->strategy == Z_FIXED) +#endif + opt_lenb = static_lenb; } else { Assert(buf != (char*)0, "lost buf"); @@ -973,7 +1042,7 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) #ifdef FORCE_STORED if (buf != (char*)0) { /* force stored block */ #else - if (stored_len+4 <= opt_lenb && buf != (char*)0) { + if (stored_len + 4 <= opt_lenb && buf != (char*)0) { /* 4: two words for the lengths */ #endif /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. @@ -984,22 +1053,20 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) */ _tr_stored_block(s, buf, stored_len, last); -#ifdef FORCE_STATIC - } else if (static_lenb >= 0) { /* force static trees */ -#else - } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) { -#endif - send_bits(s, (STATIC_TREES<<1)+last, 3); - compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree); -#ifdef DEBUG + } else if (static_lenb == opt_lenb) { + send_bits(s, (STATIC_TREES<<1) + last, 3); + compress_block(s, (const ct_data *)static_ltree, + (const ct_data *)static_dtree); +#ifdef ZLIB_DEBUG s->compressed_len += 3 + s->static_len; #endif } else { - send_bits(s, (DYN_TREES<<1)+last, 3); - send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, - max_blindex+1); - compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree); -#ifdef DEBUG + send_bits(s, (DYN_TREES<<1) + last, 3); + send_all_trees(s, s->l_desc.max_code + 1, s->d_desc.max_code + 1, + max_blindex + 1); + compress_block(s, (const ct_data *)s->dyn_ltree, + (const ct_data *)s->dyn_dtree); +#ifdef ZLIB_DEBUG s->compressed_len += 3 + s->opt_len; #endif } @@ -1011,25 +1078,27 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) if (last) { bi_windup(s); -#ifdef DEBUG +#ifdef ZLIB_DEBUG s->compressed_len += 7; /* align on byte boundary */ #endif } - Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, - s->compressed_len-7*last)); + Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len >> 3, + s->compressed_len - 7*last)); } /* =========================================================================== * Save the match info and tally the frequency counts. Return true if * the current block must be flushed. */ -int ZLIB_INTERNAL _tr_tally (s, dist, lc) - deflate_state *s; - unsigned dist; /* distance of matched string */ - unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ -{ - s->d_buf[s->last_lit] = (ush)dist; - s->l_buf[s->last_lit++] = (uch)lc; +int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc) { +#ifdef LIT_MEM + s->d_buf[s->sym_next] = (ush)dist; + s->l_buf[s->sym_next++] = (uch)lc; +#else + s->sym_buf[s->sym_next++] = (uch)dist; + s->sym_buf[s->sym_next++] = (uch)(dist >> 8); + s->sym_buf[s->sym_next++] = (uch)lc; +#endif if (dist == 0) { /* lc is the unmatched char */ s->dyn_ltree[lc].Freq++; @@ -1041,204 +1110,8 @@ int ZLIB_INTERNAL _tr_tally (s, dist, lc) (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); - s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++; + s->dyn_ltree[_length_code[lc] + LITERALS + 1].Freq++; s->dyn_dtree[d_code(dist)].Freq++; } - -#ifdef TRUNCATE_BLOCK - /* Try to guess if it is profitable to stop the current block here */ - if ((s->last_lit & 0x1fff) == 0 && s->level > 2) { - /* Compute an upper bound for the compressed length */ - ulg out_length = (ulg)s->last_lit*8L; - ulg in_length = (ulg)((long)s->strstart - s->block_start); - int dcode; - for (dcode = 0; dcode < D_CODES; dcode++) { - out_length += (ulg)s->dyn_dtree[dcode].Freq * - (5L+extra_dbits[dcode]); - } - out_length >>= 3; - Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", - s->last_lit, in_length, out_length, - 100L - out_length*100L/in_length)); - if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1; - } -#endif - return (s->last_lit == s->lit_bufsize-1); - /* We avoid equality with lit_bufsize because of wraparound at 64K - * on 16 bit machines and because stored blocks are restricted to - * 64K-1 bytes. - */ -} - -/* =========================================================================== - * Send the block data compressed using the given Huffman trees - */ -local void compress_block(s, ltree, dtree) - deflate_state *s; - ct_data *ltree; /* literal tree */ - ct_data *dtree; /* distance tree */ -{ - unsigned dist; /* distance of matched string */ - int lc; /* match length or unmatched char (if dist == 0) */ - unsigned lx = 0; /* running index in l_buf */ - unsigned code; /* the code to send */ - int extra; /* number of extra bits to send */ - - if (s->last_lit != 0) do { - dist = s->d_buf[lx]; - lc = s->l_buf[lx++]; - if (dist == 0) { - send_code(s, lc, ltree); /* send a literal byte */ - Tracecv(isgraph(lc), (stderr," '%c' ", lc)); - } else { - /* Here, lc is the match length - MIN_MATCH */ - code = _length_code[lc]; - send_code(s, code+LITERALS+1, ltree); /* send the length code */ - extra = extra_lbits[code]; - if (extra != 0) { - lc -= base_length[code]; - send_bits(s, lc, extra); /* send the extra length bits */ - } - dist--; /* dist is now the match distance - 1 */ - code = d_code(dist); - Assert (code < D_CODES, "bad d_code"); - - send_code(s, code, dtree); /* send the distance code */ - extra = extra_dbits[code]; - if (extra != 0) { - dist -= base_dist[code]; - send_bits(s, dist, extra); /* send the extra distance bits */ - } - } /* literal or match pair ? */ - - /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ - Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, - "pendingBuf overflow"); - - } while (lx < s->last_lit); - - send_code(s, END_BLOCK, ltree); - s->last_eob_len = ltree[END_BLOCK].Len; -} - -/* =========================================================================== - * Check if the data type is TEXT or BINARY, using the following algorithm: - * - TEXT if the two conditions below are satisfied: - * a) There are no non-portable control characters belonging to the - * "black list" (0..6, 14..25, 28..31). - * b) There is at least one printable character belonging to the - * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). - * - BINARY otherwise. - * - The following partially-portable control characters form a - * "gray list" that is ignored in this detection algorithm: - * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). - * IN assertion: the fields Freq of dyn_ltree are set. - */ -local int detect_data_type(s) - deflate_state *s; -{ - /* black_mask is the bit mask of black-listed bytes - * set bits 0..6, 14..25, and 28..31 - * 0xf3ffc07f = binary 11110011111111111100000001111111 - */ - unsigned long black_mask = 0xf3ffc07fUL; - int n; - - /* Check for non-textual ("black-listed") bytes. */ - for (n = 0; n <= 31; n++, black_mask >>= 1) - if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0)) - return Z_BINARY; - - /* Check for textual ("white-listed") bytes. */ - if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 - || s->dyn_ltree[13].Freq != 0) - return Z_TEXT; - for (n = 32; n < LITERALS; n++) - if (s->dyn_ltree[n].Freq != 0) - return Z_TEXT; - - /* There are no "black-listed" or "white-listed" bytes: - * this stream either is empty or has tolerated ("gray-listed") bytes only. - */ - return Z_BINARY; -} - -/* =========================================================================== - * Reverse the first len bits of a code, using straightforward code (a faster - * method would use a table) - * IN assertion: 1 <= len <= 15 - */ -local unsigned bi_reverse(code, len) - unsigned code; /* the value to invert */ - int len; /* its bit length */ -{ - register unsigned res = 0; - do { - res |= code & 1; - code >>= 1, res <<= 1; - } while (--len > 0); - return res >> 1; -} - -/* =========================================================================== - * Flush the bit buffer, keeping at most 7 bits in it. - */ -local void bi_flush(s) - deflate_state *s; -{ - if (s->bi_valid == 16) { - put_short(s, s->bi_buf); - s->bi_buf = 0; - s->bi_valid = 0; - } else if (s->bi_valid >= 8) { - put_byte(s, (Byte)s->bi_buf); - s->bi_buf >>= 8; - s->bi_valid -= 8; - } -} - -/* =========================================================================== - * Flush the bit buffer and align the output on a byte boundary - */ -local void bi_windup(s) - deflate_state *s; -{ - if (s->bi_valid > 8) { - put_short(s, s->bi_buf); - } else if (s->bi_valid > 0) { - put_byte(s, (Byte)s->bi_buf); - } - s->bi_buf = 0; - s->bi_valid = 0; -#ifdef DEBUG - s->bits_sent = (s->bits_sent+7) & ~7; -#endif -} - -/* =========================================================================== - * Copy a stored block, storing first the length and its - * one's complement if requested. - */ -local void copy_block(s, buf, len, header) - deflate_state *s; - charf *buf; /* the input data */ - unsigned len; /* its length */ - int header; /* true if block header must be written */ -{ - bi_windup(s); /* align on byte boundary */ - s->last_eob_len = 8; /* enough lookahead for inflate */ - - if (header) { - put_short(s, (ush)len); - put_short(s, (ush)~len); -#ifdef DEBUG - s->bits_sent += 2*16; -#endif - } -#ifdef DEBUG - s->bits_sent += (ulg)len<<3; -#endif - while (len--) { - put_byte(s, *buf++); - } + return (s->sym_next == s->sym_end); } diff --git a/deps/zlib/zconf.h b/deps/zlib/zconf.h index 15081436128..62adc8d8431 100644 --- a/deps/zlib/zconf.h +++ b/deps/zlib/zconf.h @@ -1,5 +1,5 @@ /* zconf.h -- configuration of the zlib compression library - * Copyright (C) 1995-2010 Jean-loup Gailly. + * Copyright (C) 1995-2024 Jean-loup Gailly, Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -8,47 +8,536 @@ #ifndef ZCONF_H #define ZCONF_H -#include "../../src/common.h" +/* + * If you *really* need a unique prefix for all types and library functions, + * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. + * Even better than compiling with -DZ_PREFIX would be to use configure to set + * this permanently in zconf.h using "./configure --zprefix". + */ +#ifdef Z_PREFIX /* may be set to #if 1 by ./configure */ +# define Z_PREFIX_SET + +/* all linked symbols and init macros */ +# define _dist_code z__dist_code +# define _length_code z__length_code +# define _tr_align z__tr_align +# define _tr_flush_bits z__tr_flush_bits +# define _tr_flush_block z__tr_flush_block +# define _tr_init z__tr_init +# define _tr_stored_block z__tr_stored_block +# define _tr_tally z__tr_tally +# define adler32 z_adler32 +# define adler32_combine z_adler32_combine +# define adler32_combine64 z_adler32_combine64 +# define adler32_z z_adler32_z +# ifndef Z_SOLO +# define compress z_compress +# define compress2 z_compress2 +# define compressBound z_compressBound +# endif +# define crc32 z_crc32 +# define crc32_combine z_crc32_combine +# define crc32_combine64 z_crc32_combine64 +# define crc32_combine_gen z_crc32_combine_gen +# define crc32_combine_gen64 z_crc32_combine_gen64 +# define crc32_combine_op z_crc32_combine_op +# define crc32_z z_crc32_z +# define deflate z_deflate +# define deflateBound z_deflateBound +# define deflateCopy z_deflateCopy +# define deflateEnd z_deflateEnd +# define deflateGetDictionary z_deflateGetDictionary +# define deflateInit z_deflateInit +# define deflateInit2 z_deflateInit2 +# define deflateInit2_ z_deflateInit2_ +# define deflateInit_ z_deflateInit_ +# define deflateParams z_deflateParams +# define deflatePending z_deflatePending +# define deflatePrime z_deflatePrime +# define deflateReset z_deflateReset +# define deflateResetKeep z_deflateResetKeep +# define deflateSetDictionary z_deflateSetDictionary +# define deflateSetHeader z_deflateSetHeader +# define deflateTune z_deflateTune +# define deflate_copyright z_deflate_copyright +# define get_crc_table z_get_crc_table +# ifndef Z_SOLO +# define gz_error z_gz_error +# define gz_intmax z_gz_intmax +# define gz_strwinerror z_gz_strwinerror +# define gzbuffer z_gzbuffer +# define gzclearerr z_gzclearerr +# define gzclose z_gzclose +# define gzclose_r z_gzclose_r +# define gzclose_w z_gzclose_w +# define gzdirect z_gzdirect +# define gzdopen z_gzdopen +# define gzeof z_gzeof +# define gzerror z_gzerror +# define gzflush z_gzflush +# define gzfread z_gzfread +# define gzfwrite z_gzfwrite +# define gzgetc z_gzgetc +# define gzgetc_ z_gzgetc_ +# define gzgets z_gzgets +# define gzoffset z_gzoffset +# define gzoffset64 z_gzoffset64 +# define gzopen z_gzopen +# define gzopen64 z_gzopen64 +# ifdef _WIN32 +# define gzopen_w z_gzopen_w +# endif +# define gzprintf z_gzprintf +# define gzputc z_gzputc +# define gzputs z_gzputs +# define gzread z_gzread +# define gzrewind z_gzrewind +# define gzseek z_gzseek +# define gzseek64 z_gzseek64 +# define gzsetparams z_gzsetparams +# define gztell z_gztell +# define gztell64 z_gztell64 +# define gzungetc z_gzungetc +# define gzvprintf z_gzvprintf +# define gzwrite z_gzwrite +# endif +# define inflate z_inflate +# define inflateBack z_inflateBack +# define inflateBackEnd z_inflateBackEnd +# define inflateBackInit z_inflateBackInit +# define inflateBackInit_ z_inflateBackInit_ +# define inflateCodesUsed z_inflateCodesUsed +# define inflateCopy z_inflateCopy +# define inflateEnd z_inflateEnd +# define inflateGetDictionary z_inflateGetDictionary +# define inflateGetHeader z_inflateGetHeader +# define inflateInit z_inflateInit +# define inflateInit2 z_inflateInit2 +# define inflateInit2_ z_inflateInit2_ +# define inflateInit_ z_inflateInit_ +# define inflateMark z_inflateMark +# define inflatePrime z_inflatePrime +# define inflateReset z_inflateReset +# define inflateReset2 z_inflateReset2 +# define inflateResetKeep z_inflateResetKeep +# define inflateSetDictionary z_inflateSetDictionary +# define inflateSync z_inflateSync +# define inflateSyncPoint z_inflateSyncPoint +# define inflateUndermine z_inflateUndermine +# define inflateValidate z_inflateValidate +# define inflate_copyright z_inflate_copyright +# define inflate_fast z_inflate_fast +# define inflate_table z_inflate_table +# ifndef Z_SOLO +# define uncompress z_uncompress +# define uncompress2 z_uncompress2 +# endif +# define zError z_zError +# ifndef Z_SOLO +# define zcalloc z_zcalloc +# define zcfree z_zcfree +# endif +# define zlibCompileFlags z_zlibCompileFlags +# define zlibVersion z_zlibVersion + +/* all zlib typedefs in zlib.h and zconf.h */ +# define Byte z_Byte +# define Bytef z_Bytef +# define alloc_func z_alloc_func +# define charf z_charf +# define free_func z_free_func +# ifndef Z_SOLO +# define gzFile z_gzFile +# endif +# define gz_header z_gz_header +# define gz_headerp z_gz_headerp +# define in_func z_in_func +# define intf z_intf +# define out_func z_out_func +# define uInt z_uInt +# define uIntf z_uIntf +# define uLong z_uLong +# define uLongf z_uLongf +# define voidp z_voidp +# define voidpc z_voidpc +# define voidpf z_voidpf + +/* all zlib structs in zlib.h and zconf.h */ +# define gz_header_s z_gz_header_s +# define internal_state z_internal_state + +#endif + +#if defined(__MSDOS__) && !defined(MSDOS) +# define MSDOS +#endif +#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2) +# define OS2 +#endif +#if defined(_WINDOWS) && !defined(WINDOWS) +# define WINDOWS +#endif +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__) +# ifndef WIN32 +# define WIN32 +# endif +#endif +#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32) +# if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__) +# ifndef SYS16BIT +# define SYS16BIT +# endif +# endif +#endif + +/* + * Compile with -DMAXSEG_64K if the alloc function cannot allocate more + * than 64k bytes at a time (needed on systems with 16-bit int). + */ +#ifdef SYS16BIT +# define MAXSEG_64K +#endif +#ifdef MSDOS +# define UNALIGNED_OK +#endif + +#ifdef __STDC_VERSION__ +# ifndef STDC +# define STDC +# endif +# if __STDC_VERSION__ >= 199901L +# ifndef STDC99 +# define STDC99 +# endif +# endif +#endif +#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus)) +# define STDC +#endif +#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__)) +# define STDC +#endif +#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32)) +# define STDC +#endif +#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__)) +# define STDC +#endif + +#if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */ +# define STDC +#endif -/* Jeez, don't complain about non-prototype - * forms, we didn't write zlib */ -#if defined(_MSC_VER) -# pragma warning( disable : 4131 ) +#ifndef STDC +# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ +# define const /* note: need a more gentle solution here */ +# endif +#endif + +#if defined(ZLIB_CONST) && !defined(z_const) +# define z_const const +#else +# define z_const +#endif + +#ifdef Z_SOLO +# ifdef _WIN64 + typedef unsigned long long z_size_t; +# else + typedef unsigned long z_size_t; +# endif +#else +# define z_longlong long long +# if defined(NO_SIZE_T) + typedef unsigned NO_SIZE_T z_size_t; +# elif defined(STDC) +# include + typedef size_t z_size_t; +# else + typedef unsigned long z_size_t; +# endif +# undef z_longlong #endif /* Maximum value for memLevel in deflateInit2 */ -#define MAX_MEM_LEVEL 9 +#ifndef MAX_MEM_LEVEL +# ifdef MAXSEG_64K +# define MAX_MEM_LEVEL 8 +# else +# define MAX_MEM_LEVEL 9 +# endif +#endif /* Maximum value for windowBits in deflateInit2 and inflateInit2. * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files * created by gzip. (Files created by minigzip can still be extracted by * gzip.) */ -#define MAX_WBITS 15 /* 32K LZ77 window */ +#ifndef MAX_WBITS +# define MAX_WBITS 15 /* 32K LZ77 window */ +#endif + +/* The memory requirements for deflate are (in bytes): + (1 << (windowBits+2)) + (1 << (memLevel+9)) + that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) + plus a few kilobytes for small objects. For example, if you want to reduce + the default memory requirements from 256K to 128K, compile with + make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" + Of course this will generally degrade compression (there's no free lunch). + + The memory requirements for inflate are (in bytes) 1 << windowBits + that is, 32K for windowBits=15 (default value) plus about 7 kilobytes + for small objects. +*/ + + /* Type declarations */ + +#ifndef OF /* function prototypes */ +# ifdef STDC +# define OF(args) args +# else +# define OF(args) () +# endif +#endif + +/* The following definitions for FAR are needed only for MSDOS mixed + * model programming (small or medium model with some far allocations). + * This was tested only with MSC; for other MSDOS compilers you may have + * to define NO_MEMCPY in zutil.h. If you don't need the mixed model, + * just define FAR to be empty. + */ +#ifdef SYS16BIT +# if defined(M_I86SM) || defined(M_I86MM) + /* MSC small or medium model */ +# define SMALL_MEDIUM +# ifdef _MSC_VER +# define FAR _far +# else +# define FAR far +# endif +# endif +# if (defined(__SMALL__) || defined(__MEDIUM__)) + /* Turbo C small or medium model */ +# define SMALL_MEDIUM +# ifdef __BORLANDC__ +# define FAR _far +# else +# define FAR far +# endif +# endif +#endif + +#if defined(WINDOWS) || defined(WIN32) + /* If building or using zlib as a DLL, define ZLIB_DLL. + * This is not mandatory, but it offers a little performance increase. + */ +# ifdef ZLIB_DLL +# if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500)) +# ifdef ZLIB_INTERNAL +# define ZEXTERN extern __declspec(dllexport) +# else +# define ZEXTERN extern __declspec(dllimport) +# endif +# endif +# endif /* ZLIB_DLL */ + /* If building or using zlib with the WINAPI/WINAPIV calling convention, + * define ZLIB_WINAPI. + * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI. + */ +# ifdef ZLIB_WINAPI +# ifdef FAR +# undef FAR +# endif +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include + /* No need for _export, use ZLIB.DEF instead. */ + /* For complete Windows compatibility, use WINAPI, not __stdcall. */ +# define ZEXPORT WINAPI +# ifdef WIN32 +# define ZEXPORTVA WINAPIV +# else +# define ZEXPORTVA FAR CDECL +# endif +# endif +#endif + +#if defined (__BEOS__) +# ifdef ZLIB_DLL +# ifdef ZLIB_INTERNAL +# define ZEXPORT __declspec(dllexport) +# define ZEXPORTVA __declspec(dllexport) +# else +# define ZEXPORT __declspec(dllimport) +# define ZEXPORTVA __declspec(dllimport) +# endif +# endif +#endif + +#ifndef ZEXTERN +# define ZEXTERN extern +#endif +#ifndef ZEXPORT +# define ZEXPORT +#endif +#ifndef ZEXPORTVA +# define ZEXPORTVA +#endif -#define ZEXTERN extern -#define ZEXPORT -#define ZEXPORTVA #ifndef FAR -# define FAR +# define FAR #endif -#define OF(args) args +#if !defined(__MACTYPES__) typedef unsigned char Byte; /* 8 bits */ +#endif typedef unsigned int uInt; /* 16 bits or more */ typedef unsigned long uLong; /* 32 bits or more */ -typedef Byte FAR Bytef; +#ifdef SMALL_MEDIUM + /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */ +# define Bytef Byte FAR +#else + typedef Byte FAR Bytef; +#endif typedef char FAR charf; typedef int FAR intf; typedef uInt FAR uIntf; typedef uLong FAR uLongf; -typedef void const *voidpc; -typedef void FAR *voidpf; -typedef void *voidp; +#ifdef STDC + typedef void const *voidpc; + typedef void FAR *voidpf; + typedef void *voidp; +#else + typedef Byte const *voidpc; + typedef Byte FAR *voidpf; + typedef Byte *voidp; +#endif + +#if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC) +# include +# if (UINT_MAX == 0xffffffffUL) +# define Z_U4 unsigned +# elif (ULONG_MAX == 0xffffffffUL) +# define Z_U4 unsigned long +# elif (USHRT_MAX == 0xffffffffUL) +# define Z_U4 unsigned short +# endif +#endif + +#ifdef Z_U4 + typedef Z_U4 z_crc_t; +#else + typedef unsigned long z_crc_t; +#endif + +#ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ +# define Z_HAVE_UNISTD_H +#endif + +#ifdef HAVE_STDARG_H /* may be set to #if 1 by ./configure */ +# define Z_HAVE_STDARG_H +#endif + +#ifdef STDC +# ifndef Z_SOLO +# include /* for off_t */ +# endif +#endif + +#if defined(STDC) || defined(Z_HAVE_STDARG_H) +# ifndef Z_SOLO +# include /* for va_list */ +# endif +#endif + +#ifdef _WIN32 +# ifndef Z_SOLO +# include /* for wchar_t */ +# endif +#endif -#define z_off_t git_off_t -#define z_off64_t z_off_t +/* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and + * "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even + * though the former does not conform to the LFS document), but considering + * both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as + * equivalently requesting no 64-bit operations + */ +#if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1 +# undef _LARGEFILE64_SOURCE +#endif + +#ifndef Z_HAVE_UNISTD_H +# ifdef __WATCOMC__ +# define Z_HAVE_UNISTD_H +# endif +#endif +#ifndef Z_HAVE_UNISTD_H +# if defined(_LARGEFILE64_SOURCE) && !defined(_WIN32) +# define Z_HAVE_UNISTD_H +# endif +#endif +#ifndef Z_SOLO +# if defined(Z_HAVE_UNISTD_H) +# include /* for SEEK_*, off_t, and _LFS64_LARGEFILE */ +# ifdef VMS +# include /* for off_t */ +# endif +# ifndef z_off_t +# define z_off_t off_t +# endif +# endif +#endif + +#if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0 +# define Z_LFS64 +#endif + +#if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64) +# define Z_LARGE64 +#endif + +#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS-0 == 64 && defined(Z_LFS64) +# define Z_WANT64 +#endif + +#if !defined(SEEK_SET) && !defined(Z_SOLO) +# define SEEK_SET 0 /* Seek from beginning of file. */ +# define SEEK_CUR 1 /* Seek from current position. */ +# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ +#endif + +#ifndef z_off_t +# define z_off_t long +#endif + +#if !defined(_WIN32) && defined(Z_LARGE64) +# define z_off64_t off64_t +#else +# if defined(_WIN32) && !defined(__GNUC__) +# define z_off64_t __int64 +# else +# define z_off64_t z_off_t +# endif +#endif + +/* MVS linker does not support external names larger than 8 bytes */ +#if defined(__MVS__) + #pragma map(deflateInit_,"DEIN") + #pragma map(deflateInit2_,"DEIN2") + #pragma map(deflateEnd,"DEEND") + #pragma map(deflateBound,"DEBND") + #pragma map(inflateInit_,"ININ") + #pragma map(inflateInit2_,"ININ2") + #pragma map(inflateEnd,"INEND") + #pragma map(inflateSync,"INSY") + #pragma map(inflateSetDictionary,"INSEDI") + #pragma map(compressBound,"CMBND") + #pragma map(inflate_table,"INTABL") + #pragma map(inflate_fast,"INFA") + #pragma map(inflate_copyright,"INCOPY") +#endif #endif /* ZCONF_H */ diff --git a/deps/zlib/zlib.h b/deps/zlib/zlib.h index bfbba83e8ee..8d4b932eaf6 100644 --- a/deps/zlib/zlib.h +++ b/deps/zlib/zlib.h @@ -1,7 +1,7 @@ /* zlib.h -- interface of the 'zlib' general purpose compression library - version 1.2.5, April 19th, 2010 + version 1.3.1, January 22nd, 2024 - Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler + Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -24,8 +24,8 @@ The data format used by the zlib library is described by RFCs (Request for - Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt - (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). + Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950 + (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format). */ #ifndef ZLIB_H @@ -37,11 +37,11 @@ extern "C" { #endif -#define ZLIB_VERSION "1.2.5" -#define ZLIB_VERNUM 0x1250 +#define ZLIB_VERSION "1.3.1" +#define ZLIB_VERNUM 0x1310 #define ZLIB_VER_MAJOR 1 -#define ZLIB_VER_MINOR 2 -#define ZLIB_VER_REVISION 5 +#define ZLIB_VER_MINOR 3 +#define ZLIB_VER_REVISION 1 #define ZLIB_VER_SUBREVISION 0 /* @@ -65,7 +65,8 @@ extern "C" { with "gz". The gzip format is different from the zlib format. gzip is a gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. - This library can optionally read and write gzip streams in memory as well. + This library can optionally read and write gzip and raw deflate streams in + memory as well. The zlib format was designed to be compact and fast for use in memory and on communications channels. The gzip format was designed for single- @@ -74,32 +75,33 @@ extern "C" { The library does not install any signal handler. The decoder checks the consistency of the compressed data, so the library should never crash - even in case of corrupted input. + even in the case of corrupted input. */ -typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); -typedef void (*free_func) OF((voidpf opaque, voidpf address)); +typedef voidpf (*alloc_func)(voidpf opaque, uInt items, uInt size); +typedef void (*free_func)(voidpf opaque, voidpf address); struct internal_state; typedef struct z_stream_s { - Bytef *next_in; /* next input byte */ + z_const Bytef *next_in; /* next input byte */ uInt avail_in; /* number of bytes available at next_in */ - uLong total_in; /* total nb of input bytes read so far */ + uLong total_in; /* total number of input bytes read so far */ - Bytef *next_out; /* next output byte should be put there */ + Bytef *next_out; /* next output byte will go here */ uInt avail_out; /* remaining free space at next_out */ - uLong total_out; /* total nb of bytes output so far */ + uLong total_out; /* total number of bytes output so far */ - char *msg; /* last error message, NULL if no error */ + z_const char *msg; /* last error message, NULL if no error */ struct internal_state FAR *state; /* not visible by applications */ alloc_func zalloc; /* used to allocate the internal state */ free_func zfree; /* used to free the internal state */ voidpf opaque; /* private data object passed to zalloc and zfree */ - int data_type; /* best guess about the data type: binary or text */ - uLong adler; /* adler32 value of the uncompressed data */ + int data_type; /* best guess about the data type: binary or text + for deflate, or the decoding state for inflate */ + uLong adler; /* Adler-32 or CRC-32 value of the uncompressed data */ uLong reserved; /* reserved for future use */ } z_stream; @@ -142,7 +144,9 @@ typedef gz_header FAR *gz_headerp; zalloc must return Z_NULL if there is not enough memory for the object. If zlib is used in a multi-threaded application, zalloc and zfree must be - thread safe. + thread safe. In that case, zlib is thread-safe. When zalloc and zfree are + Z_NULL on entry to the initialization function, they are set to internal + routines that use the standard library functions malloc() and free(). On 16-bit systems, the functions zalloc and zfree must be able to allocate exactly 65536 bytes, but will not be required to allocate more than this if @@ -155,7 +159,7 @@ typedef gz_header FAR *gz_headerp; The fields total_in and total_out can be used for statistics or progress reports. After compression, total_in holds the total size of the - uncompressed data and may be saved for use in the decompressor (particularly + uncompressed data and may be saved for use by the decompressor (particularly if the decompressor wants to decompress everything in a single step). */ @@ -200,7 +204,7 @@ typedef gz_header FAR *gz_headerp; #define Z_TEXT 1 #define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ #define Z_UNKNOWN 2 -/* Possible values of the data_type field (though see inflate()) */ +/* Possible values of the data_type field for deflate() */ #define Z_DEFLATED 8 /* The deflate compression method (the only one supported in this version) */ @@ -213,7 +217,7 @@ typedef gz_header FAR *gz_headerp; /* basic functions */ -ZEXTERN const char * ZEXPORT zlibVersion OF((void)); +ZEXTERN const char * ZEXPORT zlibVersion(void); /* The application can compare zlibVersion and ZLIB_VERSION for consistency. If the first character differs, the library code actually used is not compatible with the zlib.h header file used by the application. This check @@ -221,12 +225,12 @@ ZEXTERN const char * ZEXPORT zlibVersion OF((void)); */ /* -ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); +ZEXTERN int ZEXPORT deflateInit(z_streamp strm, int level); Initializes the internal stream state for compression. The fields zalloc, zfree and opaque must be initialized before by the caller. If zalloc and zfree are set to Z_NULL, deflateInit updates them to use default - allocation functions. + allocation functions. total_in, total_out, adler, and msg are initialized. The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: 1 gives best speed, 9 gives best compression, 0 gives no compression at all @@ -243,7 +247,7 @@ ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); */ -ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); +ZEXTERN int ZEXPORT deflate(z_streamp strm, int flush); /* deflate compresses as much data as possible, and stops when the input buffer becomes empty or the output buffer becomes full. It may introduce @@ -258,11 +262,11 @@ ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); enough room in the output buffer), next_in and avail_in are updated and processing will resume at this point for the next call of deflate(). - - Provide more output starting at next_out and update next_out and avail_out + - Generate more output starting at next_out and update next_out and avail_out accordingly. This action is forced if the parameter flush is non zero. Forcing flush frequently degrades the compression ratio, so this parameter - should be set only when necessary (in interactive applications). Some - output may be provided even if flush is not set. + should be set only when necessary. Some output may be provided even if + flush is zero. Before the call of deflate(), the application should ensure that at least one of the actions is possible, by providing more input and/or consuming more @@ -271,7 +275,9 @@ ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); output when it wants, for example when the output buffer is full (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK and with zero avail_out, it must be called again after making room in the output - buffer because there might be more output pending. + buffer because there might be more output pending. See deflatePending(), + which can be used if desired to determine whether or not there is more output + in that case. Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to decide how much data to accumulate before producing output, in order to @@ -292,8 +298,8 @@ ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); input data so far will be available to the decompressor, as for Z_SYNC_FLUSH. This completes the current deflate block and follows it with an empty fixed codes block that is 10 bits long. This assures that enough bytes are output - in order for the decompressor to finish the block before the empty fixed code - block. + in order for the decompressor to finish the block before the empty fixed + codes block. If flush is set to Z_BLOCK, a deflate block is completed and emitted, as for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to @@ -314,42 +320,47 @@ ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); with the same value of the flush parameter and more output space (updated avail_out), until the flush is complete (deflate returns with non-zero avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that - avail_out is greater than six to avoid repeated flush markers due to - avail_out == 0 on return. + avail_out is greater than six when the flush marker begins, in order to avoid + repeated flush markers upon calling deflate() again when avail_out == 0. If the parameter flush is set to Z_FINISH, pending input is processed, pending output is flushed and deflate returns with Z_STREAM_END if there was - enough output space; if deflate returns with Z_OK, this function must be - called again with Z_FINISH and more output space (updated avail_out) but no - more input data, until it returns with Z_STREAM_END or an error. After - deflate has returned Z_STREAM_END, the only possible operations on the stream - are deflateReset or deflateEnd. - - Z_FINISH can be used immediately after deflateInit if all the compression - is to be done in a single step. In this case, avail_out must be at least the - value returned by deflateBound (see below). If deflate does not return - Z_STREAM_END, then it must be called again as described above. - - deflate() sets strm->adler to the adler32 checksum of all input read - so far (that is, total_in bytes). + enough output space. If deflate returns with Z_OK or Z_BUF_ERROR, this + function must be called again with Z_FINISH and more output space (updated + avail_out) but no more input data, until it returns with Z_STREAM_END or an + error. After deflate has returned Z_STREAM_END, the only possible operations + on the stream are deflateReset or deflateEnd. + + Z_FINISH can be used in the first deflate call after deflateInit if all the + compression is to be done in a single step. In order to complete in one + call, avail_out must be at least the value returned by deflateBound (see + below). Then deflate is guaranteed to return Z_STREAM_END. If not enough + output space is provided, deflate will not return Z_STREAM_END, and it must + be called again as described above. + + deflate() sets strm->adler to the Adler-32 checksum of all input read + so far (that is, total_in bytes). If a gzip stream is being generated, then + strm->adler will be the CRC-32 checksum of the input read so far. (See + deflateInit2 below.) deflate() may update strm->data_type if it can make a good guess about - the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered - binary. This field is only for information purposes and does not affect the - compression algorithm in any manner. + the input data type (Z_BINARY or Z_TEXT). If in doubt, the data is + considered binary. This field is only for information purposes and does not + affect the compression algorithm in any manner. deflate() returns Z_OK if some progress has been made (more input processed or more output produced), Z_STREAM_END if all input has been consumed and all output has been produced (only when flush is set to Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example - if next_in or next_out was Z_NULL), Z_BUF_ERROR if no progress is possible - (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not - fatal, and deflate() can be called again with more input and more output - space to continue compressing. + if next_in or next_out was Z_NULL or the state was inadvertently written over + by the application), or Z_BUF_ERROR if no progress is possible (for example + avail_in or avail_out was zero). Note that Z_BUF_ERROR is not fatal, and + deflate() can be called again with more input and more output space to + continue compressing. */ -ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); +ZEXTERN int ZEXPORT deflateEnd(z_streamp strm); /* All dynamically allocated data structures for this stream are freed. This function discards any unprocessed input and does not flush any pending @@ -364,31 +375,30 @@ ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); /* -ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); +ZEXTERN int ZEXPORT inflateInit(z_streamp strm); Initializes the internal stream state for decompression. The fields next_in, avail_in, zalloc, zfree and opaque must be initialized before by - the caller. If next_in is not Z_NULL and avail_in is large enough (the - exact value depends on the compression method), inflateInit determines the - compression method from the zlib header and allocates all data structures - accordingly; otherwise the allocation will be deferred to the first call of - inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to - use default allocation functions. + the caller. In the current version of inflate, the provided input is not + read or consumed. The allocation of a sliding window will be deferred to + the first call of inflate (if the decompression does not complete on the + first call). If zalloc and zfree are set to Z_NULL, inflateInit updates + them to use default allocation functions. total_in, total_out, adler, and + msg are initialized. inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_VERSION_ERROR if the zlib library version is incompatible with the version assumed by the caller, or Z_STREAM_ERROR if the parameters are invalid, such as a null pointer to the structure. msg is set to null if - there is no error message. inflateInit does not perform any decompression - apart from possibly reading the zlib header if present: actual decompression - will be done by inflate(). (So next_in and avail_in may be modified, but - next_out and avail_out are unused and unchanged.) The current implementation - of inflateInit() does not process any header information -- that is deferred - until inflate() is called. + there is no error message. inflateInit does not perform any decompression. + Actual decompression will be done by inflate(). So next_in, and avail_in, + next_out, and avail_out are unused and unchanged. The current + implementation of inflateInit() does not process any header information -- + that is deferred until inflate() is called. */ -ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); +ZEXTERN int ZEXPORT inflate(z_streamp strm, int flush); /* inflate decompresses as much data as possible, and stops when the input buffer becomes empty or the output buffer becomes full. It may introduce @@ -400,17 +410,20 @@ ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); - Decompress more input starting at next_in and update next_in and avail_in accordingly. If not all input can be processed (because there is not - enough room in the output buffer), next_in is updated and processing will - resume at this point for the next call of inflate(). + enough room in the output buffer), then next_in and avail_in are updated + accordingly, and processing will resume at this point for the next call of + inflate(). - - Provide more output starting at next_out and update next_out and avail_out + - Generate more output starting at next_out and update next_out and avail_out accordingly. inflate() provides as much output as possible, until there is no more input data or no more space in the output buffer (see below about the flush parameter). Before the call of inflate(), the application should ensure that at least one of the actions is possible, by providing more input and/or consuming more - output, and updating the next_* and avail_* values accordingly. The + output, and updating the next_* and avail_* values accordingly. If the + caller of inflate() does not provide both available input and available + output space, it is possible that there will be no progress made. The application can consume the uncompressed output when it wants, for example when the output buffer is full (avail_out == 0), or after each call of inflate(). If inflate returns Z_OK and with zero avail_out, it must be @@ -427,7 +440,7 @@ ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); gets to the end of that block, or when it runs out of data. The Z_BLOCK option assists in appending to or combining deflate streams. - Also to assist in this, on return inflate() will set strm->data_type to the + To assist in this, on return inflate() always sets strm->data_type to the number of unused bits in the last byte taken from strm->next_in, plus 64 if inflate() is currently decoding the last block in the deflate stream, plus 128 if inflate() returned immediately after decoding an end-of-block code or @@ -451,60 +464,68 @@ ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); error. However if all decompression is to be performed in a single step (a single call of inflate), the parameter flush should be set to Z_FINISH. In this case all pending input is processed and all pending output is flushed; - avail_out must be large enough to hold all the uncompressed data. (The size - of the uncompressed data may have been saved by the compressor for this - purpose.) The next operation on this stream must be inflateEnd to deallocate - the decompression state. The use of Z_FINISH is never required, but can be - used to inform inflate that a faster approach may be used for the single - inflate() call. + avail_out must be large enough to hold all of the uncompressed data for the + operation to complete. (The size of the uncompressed data may have been + saved by the compressor for this purpose.) The use of Z_FINISH is not + required to perform an inflation in one step. However it may be used to + inform inflate that a faster approach can be used for the single inflate() + call. Z_FINISH also informs inflate to not maintain a sliding window if the + stream completes, which reduces inflate's memory footprint. If the stream + does not complete, either because not all of the stream is provided or not + enough output space is provided, then a sliding window will be allocated and + inflate() can be called again to continue the operation as if Z_NO_FLUSH had + been used. In this implementation, inflate() always flushes as much output as possible to the output buffer, and always uses the faster approach on the - first call. So the only effect of the flush parameter in this implementation - is on the return value of inflate(), as noted below, or when it returns early - because Z_BLOCK or Z_TREES is used. + first call. So the effects of the flush parameter in this implementation are + on the return value of inflate() as noted below, when inflate() returns early + when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of + memory for a sliding window when Z_FINISH is used. If a preset dictionary is needed after this call (see inflateSetDictionary - below), inflate sets strm->adler to the adler32 checksum of the dictionary + below), inflate sets strm->adler to the Adler-32 checksum of the dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise it sets - strm->adler to the adler32 checksum of all output produced so far (that is, + strm->adler to the Adler-32 checksum of all output produced so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described - below. At the end of the stream, inflate() checks that its computed adler32 + below. At the end of the stream, inflate() checks that its computed Adler-32 checksum is equal to that saved by the compressor and returns Z_STREAM_END only if the checksum is correct. inflate() can decompress and check either zlib-wrapped or gzip-wrapped deflate data. The header type is detected automatically, if requested when initializing with inflateInit2(). Any information contained in the gzip - header is not retained, so applications that need that information should - instead use raw inflate, see inflateInit2() below, or inflateBack() and - perform their own processing of the gzip header and trailer. + header is not retained unless inflateGetHeader() is used. When processing + gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output + produced so far. The CRC-32 is checked against the gzip trailer, as is the + uncompressed length, modulo 2^32. inflate() returns Z_OK if some progress has been made (more input processed or more output produced), Z_STREAM_END if the end of the compressed data has been reached and all uncompressed output has been produced, Z_NEED_DICT if a preset dictionary is needed at this point, Z_DATA_ERROR if the input data was corrupted (input stream not conforming to the zlib format or incorrect check - value), Z_STREAM_ERROR if the stream structure was inconsistent (for example - next_in or next_out was Z_NULL), Z_MEM_ERROR if there was not enough memory, - Z_BUF_ERROR if no progress is possible or if there was not enough room in the - output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and + value, in which case strm->msg points to a string with a more specific + error), Z_STREAM_ERROR if the stream structure was inconsistent (for example + next_in or next_out was Z_NULL, or the state was inadvertently written over + by the application), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR + if no progress was possible or if there was not enough room in the output + buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and inflate() can be called again with more input and more output space to continue decompressing. If Z_DATA_ERROR is returned, the application may then call inflateSync() to look for a good compression block if a partial - recovery of the data is desired. + recovery of the data is to be attempted. */ -ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); +ZEXTERN int ZEXPORT inflateEnd(z_streamp strm); /* All dynamically allocated data structures for this stream are freed. This function discards any unprocessed input and does not flush any pending output. - inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state - was inconsistent. In the error case, msg may be set but then points to a - static string (which must not be deallocated). + inflateEnd returns Z_OK if success, or Z_STREAM_ERROR if the stream state + was inconsistent. */ @@ -515,16 +536,15 @@ ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); */ /* -ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, - int level, - int method, - int windowBits, - int memLevel, - int strategy)); +ZEXTERN int ZEXPORT deflateInit2(z_streamp strm, + int level, + int method, + int windowBits, + int memLevel, + int strategy); This is another version of deflateInit with more compression options. The - fields next_in, zalloc, zfree and opaque must be initialized before by the - caller. + fields zalloc, zfree and opaque must be initialized before by the caller. The method parameter is the compression method. It must be Z_DEFLATED in this version of the library. @@ -535,16 +555,29 @@ ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, compression at the expense of memory usage. The default value is 15 if deflateInit is used instead. + For the current implementation of deflate(), a windowBits value of 8 (a + window size of 256 bytes) is not supported. As a result, a request for 8 + will result in 9 (a 512-byte window). In that case, providing 8 to + inflateInit2() will result in an error when the zlib header with 9 is + checked against the initialization of inflate(). The remedy is to not use 8 + with deflateInit2() with this initialization, or at least in that case use 9 + with inflateInit2(). + windowBits can also be -8..-15 for raw deflate. In this case, -windowBits determines the window size. deflate() will then generate raw deflate data - with no zlib header or trailer, and will not compute an adler32 check value. + with no zlib header or trailer, and will not compute a check value. windowBits can also be greater than 15 for optional gzip encoding. Add 16 to windowBits to write a simple gzip header and trailer around the compressed data instead of a zlib wrapper. The gzip header will have no file name, no extra data, no comment, no modification time (set to zero), no - header crc, and the operating system will be set to 255 (unknown). If a - gzip stream is being written, strm->adler is a crc32 instead of an adler32. + header crc, and the operating system will be set to the appropriate value, + if the operating system was determined at compile time. If a gzip stream is + being written, strm->adler is a CRC-32 instead of an Adler-32. + + For raw deflate or gzip encoding, a request for a 256-byte window is + rejected as invalid, since only the zlib header provides a means of + transmitting the window size to the decompressor. The memLevel parameter specifies how much memory should be allocated for the internal compression state. memLevel=1 uses minimum memory but is @@ -575,15 +608,20 @@ ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, compression: this will be done by deflate(). */ -ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, - const Bytef *dictionary, - uInt dictLength)); +ZEXTERN int ZEXPORT deflateSetDictionary(z_streamp strm, + const Bytef *dictionary, + uInt dictLength); /* Initializes the compression dictionary from the given byte sequence - without producing any compressed output. This function must be called - immediately after deflateInit, deflateInit2 or deflateReset, before any call - of deflate. The compressor and decompressor must use exactly the same - dictionary (see inflateSetDictionary). + without producing any compressed output. When using the zlib format, this + function must be called immediately after deflateInit, deflateInit2 or + deflateReset, and before any call of deflate. When doing raw deflate, this + function must be called either before any call of deflate, or immediately + after the completion of a deflate block, i.e. after all input has been + consumed and all output has been delivered when using any of the flush + options Z_BLOCK, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, or Z_FULL_FLUSH. The + compressor and decompressor must use exactly the same dictionary (see + inflateSetDictionary). The dictionary should consist of strings (byte sequences) that are likely to be encountered later in the data to be compressed, with the most commonly @@ -600,22 +638,44 @@ ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, addition, the current implementation of deflate will use at most the window size minus 262 bytes of the provided dictionary. - Upon return of this function, strm->adler is set to the adler32 value + Upon return of this function, strm->adler is set to the Adler-32 value of the dictionary; the decompressor may later use this value to determine - which dictionary has been used by the compressor. (The adler32 value + which dictionary has been used by the compressor. (The Adler-32 value applies to the whole dictionary even if only a subset of the dictionary is actually used by the compressor.) If a raw deflate was requested, then the - adler32 value is not computed and strm->adler is not set. + Adler-32 value is not computed and strm->adler is not set. deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is inconsistent (for example if deflate has already been called for this stream - or if the compression method is bsort). deflateSetDictionary does not - perform any compression: this will be done by deflate(). + or if not at a block boundary for raw deflate). deflateSetDictionary does + not perform any compression: this will be done by deflate(). */ -ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, - z_streamp source)); +ZEXTERN int ZEXPORT deflateGetDictionary(z_streamp strm, + Bytef *dictionary, + uInt *dictLength); +/* + Returns the sliding dictionary being maintained by deflate. dictLength is + set to the number of bytes in the dictionary, and that many bytes are copied + to dictionary. dictionary must have enough space, where 32768 bytes is + always enough. If deflateGetDictionary() is called with dictionary equal to + Z_NULL, then only the dictionary length is returned, and nothing is copied. + Similarly, if dictLength is Z_NULL, then it is not set. + + deflateGetDictionary() may return a length less than the window size, even + when more than the window size in input has been provided. It may return up + to 258 bytes less in that case, due to how zlib's implementation of deflate + manages the sliding window and lookahead for matches, where matches can be + up to 258 bytes long. If the application needs the last window-size bytes of + input, then that would need to be saved by the application outside of zlib. + + deflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the + stream state is inconsistent. +*/ + +ZEXTERN int ZEXPORT deflateCopy(z_streamp dest, + z_streamp source); /* Sets the destination stream as a complete copy of the source stream. @@ -632,43 +692,60 @@ ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, destination. */ -ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm)); +ZEXTERN int ZEXPORT deflateReset(z_streamp strm); /* - This function is equivalent to deflateEnd followed by deflateInit, - but does not free and reallocate all the internal compression state. The - stream will keep the same compression level and any other attributes that - may have been set by deflateInit2. + This function is equivalent to deflateEnd followed by deflateInit, but + does not free and reallocate the internal compression state. The stream + will leave the compression level and any other attributes that may have been + set unchanged. total_in, total_out, adler, and msg are initialized. deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source stream state was inconsistent (such as zalloc or state being Z_NULL). */ -ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, - int level, - int strategy)); +ZEXTERN int ZEXPORT deflateParams(z_streamp strm, + int level, + int strategy); /* Dynamically update the compression level and compression strategy. The - interpretation of level and strategy is as in deflateInit2. This can be + interpretation of level and strategy is as in deflateInit2(). This can be used to switch between compression and straight copy of the input data, or to switch to a different kind of input data requiring a different strategy. - If the compression level is changed, the input available so far is - compressed with the old level (and may be flushed); the new level will take - effect only at the next call of deflate(). - - Before the call of deflateParams, the stream state must be set as for - a call of deflate(), since the currently available input may have to be - compressed and flushed. In particular, strm->avail_out must be non-zero. - - deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source - stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR if - strm->avail_out was zero. -*/ - -ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm, - int good_length, - int max_lazy, - int nice_length, - int max_chain)); + If the compression approach (which is a function of the level) or the + strategy is changed, and if there have been any deflate() calls since the + state was initialized or reset, then the input available so far is + compressed with the old level and strategy using deflate(strm, Z_BLOCK). + There are three approaches for the compression levels 0, 1..3, and 4..9 + respectively. The new level and strategy will take effect at the next call + of deflate(). + + If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does + not have enough output space to complete, then the parameter change will not + take effect. In this case, deflateParams() can be called again with the + same parameters and more output space to try again. + + In order to assure a change in the parameters on the first try, the + deflate stream should be flushed using deflate() with Z_BLOCK or other flush + request until strm.avail_out is not zero, before calling deflateParams(). + Then no more input data should be provided before the deflateParams() call. + If this is done, the old level and strategy will be applied to the data + compressed before deflateParams(), and the new level and strategy will be + applied to the data compressed after deflateParams(). + + deflateParams returns Z_OK on success, Z_STREAM_ERROR if the source stream + state was inconsistent or if a parameter was invalid, or Z_BUF_ERROR if + there was not enough output space to complete the compression of the + available input data before a change in the strategy or approach. Note that + in the case of a Z_BUF_ERROR, the parameters are not changed. A return + value of Z_BUF_ERROR is not fatal, in which case deflateParams() can be + retried with more output space. +*/ + +ZEXTERN int ZEXPORT deflateTune(z_streamp strm, + int good_length, + int max_lazy, + int nice_length, + int max_chain); /* Fine tune deflate's internal compression parameters. This should only be used by someone who understands the algorithm used by zlib's deflate for @@ -681,19 +758,39 @@ ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm, returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream. */ -ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, - uLong sourceLen)); +ZEXTERN uLong ZEXPORT deflateBound(z_streamp strm, + uLong sourceLen); /* deflateBound() returns an upper bound on the compressed size after deflation of sourceLen bytes. It must be called after deflateInit() or deflateInit2(), and after deflateSetHeader(), if used. This would be used to allocate an output buffer for deflation in a single pass, and so would be - called before deflate(). -*/ + called before deflate(). If that first deflate() call is provided the + sourceLen input bytes, an output buffer allocated to the size returned by + deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed + to return Z_STREAM_END. Note that it is possible for the compressed size to + be larger than the value returned by deflateBound() if flush options other + than Z_FINISH or Z_NO_FLUSH are used. +*/ + +ZEXTERN int ZEXPORT deflatePending(z_streamp strm, + unsigned *pending, + int *bits); +/* + deflatePending() returns the number of bytes and bits of output that have + been generated, but not yet provided in the available output. The bytes not + provided would be due to the available output space having being consumed. + The number of bits of output not provided are between 0 and 7, where they + await more bits to join them in order to fill out a full byte. If pending + or bits are Z_NULL, then those values are not set. + + deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. + */ -ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm, - int bits, - int value)); +ZEXTERN int ZEXPORT deflatePrime(z_streamp strm, + int bits, + int value); /* deflatePrime() inserts bits in the deflate output stream. The intent is that this function is used to start off the deflate output with the bits @@ -703,12 +800,13 @@ ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm, than or equal to 16, and that many of the least significant bits of value will be inserted in the output. - deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source - stream state was inconsistent. + deflatePrime returns Z_OK if success, Z_BUF_ERROR if there was not enough + room in the internal buffer to insert the bits, or Z_STREAM_ERROR if the + source stream state was inconsistent. */ -ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm, - gz_headerp head)); +ZEXTERN int ZEXPORT deflateSetHeader(z_streamp strm, + gz_headerp head); /* deflateSetHeader() provides gzip header information for when a gzip stream is requested by deflateInit2(). deflateSetHeader() may be called @@ -724,16 +822,17 @@ ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm, gzip file" and give up. If deflateSetHeader is not used, the default gzip header has text false, - the time set to zero, and os set to 255, with no extra, name, or comment - fields. The gzip header is returned to the default state by deflateReset(). + the time set to zero, and os set to the current operating system, with no + extra, name, or comment fields. The gzip header is returned to the default + state by deflateReset(). deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source stream state was inconsistent. */ /* -ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, - int windowBits)); +ZEXTERN int ZEXPORT inflateInit2(z_streamp strm, + int windowBits); This is another version of inflateInit with an extra parameter. The fields next_in, avail_in, zalloc, zfree and opaque must be initialized @@ -758,7 +857,7 @@ ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, is for use with other formats that use the deflate compressed data format such as zip. Those formats provide their own check values. If a custom format is developed using the raw deflate format for compressed data, it is - recommended that a check value such as an adler32 or a crc32 be applied to + recommended that a check value such as an Adler-32 or a CRC-32 be applied to the uncompressed data as is done in the zlib, gzip, and zip formats. For most applications, the zlib format should be used as is. Note that comments above on the use in deflateInit2() applies to the magnitude of windowBits. @@ -767,7 +866,12 @@ ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, 32 to windowBits to enable zlib and gzip decoding with automatic header detection, or add 16 to decode only the gzip format (the zlib format will return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a - crc32 instead of an adler32. + CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see + below), inflate() will *not* automatically decode concatenated gzip members. + inflate() will return Z_STREAM_END at the end of the gzip member. The state + would need to be reset to continue decoding a subsequent gzip member. This + *must* be done if there is more data after a gzip member, in order for the + decompression to be compliant with the gzip standard (RFC 1952). inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_VERSION_ERROR if the zlib library version is incompatible with the @@ -781,45 +885,65 @@ ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, deferred until inflate() is called. */ -ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm, - const Bytef *dictionary, - uInt dictLength)); +ZEXTERN int ZEXPORT inflateSetDictionary(z_streamp strm, + const Bytef *dictionary, + uInt dictLength); /* Initializes the decompression dictionary from the given uncompressed byte sequence. This function must be called immediately after a call of inflate, if that call returned Z_NEED_DICT. The dictionary chosen by the compressor - can be determined from the adler32 value returned by that call of inflate. + can be determined from the Adler-32 value returned by that call of inflate. The compressor and decompressor must use exactly the same dictionary (see - deflateSetDictionary). For raw inflate, this function can be called - immediately after inflateInit2() or inflateReset() and before any call of - inflate() to set the dictionary. The application must insure that the - dictionary that was used for compression is provided. + deflateSetDictionary). For raw inflate, this function can be called at any + time to set the dictionary. If the provided dictionary is smaller than the + window and there is already data in the window, then the provided dictionary + will amend what's there. The application must insure that the dictionary + that was used for compression is provided. inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the - expected one (incorrect adler32 value). inflateSetDictionary does not + expected one (incorrect Adler-32 value). inflateSetDictionary does not perform any decompression: this will be done by subsequent calls of inflate(). */ -ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm)); +ZEXTERN int ZEXPORT inflateGetDictionary(z_streamp strm, + Bytef *dictionary, + uInt *dictLength); /* - Skips invalid compressed data until a full flush point (see above the - description of deflate with Z_FULL_FLUSH) can be found, or until all + Returns the sliding dictionary being maintained by inflate. dictLength is + set to the number of bytes in the dictionary, and that many bytes are copied + to dictionary. dictionary must have enough space, where 32768 bytes is + always enough. If inflateGetDictionary() is called with dictionary equal to + Z_NULL, then only the dictionary length is returned, and nothing is copied. + Similarly, if dictLength is Z_NULL, then it is not set. + + inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the + stream state is inconsistent. +*/ + +ZEXTERN int ZEXPORT inflateSync(z_streamp strm); +/* + Skips invalid compressed data until a possible full flush point (see above + for the description of deflate with Z_FULL_FLUSH) can be found, or until all available input is skipped. No output is provided. - inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR - if no more input was provided, Z_DATA_ERROR if no flush point has been - found, or Z_STREAM_ERROR if the stream structure was inconsistent. In the - success case, the application may save the current current value of total_in + inflateSync searches for a 00 00 FF FF pattern in the compressed data. + All full flush points have this pattern, but not all occurrences of this + pattern are full flush points. + + inflateSync returns Z_OK if a possible full flush point has been found, + Z_BUF_ERROR if no more input was provided, Z_DATA_ERROR if no flush point + has been found, or Z_STREAM_ERROR if the stream structure was inconsistent. + In the success case, the application may save the current value of total_in which indicates where valid compressed data was found. In the error case, the application may repeatedly call inflateSync, providing more input each time, until success or end of the input data. */ -ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest, - z_streamp source)); +ZEXTERN int ZEXPORT inflateCopy(z_streamp dest, + z_streamp source); /* Sets the destination stream as a complete copy of the source stream. @@ -834,31 +958,34 @@ ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest, destination. */ -ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); +ZEXTERN int ZEXPORT inflateReset(z_streamp strm); /* This function is equivalent to inflateEnd followed by inflateInit, - but does not free and reallocate all the internal decompression state. The + but does not free and reallocate the internal decompression state. The stream will keep attributes that may have been set by inflateInit2. + total_in, total_out, adler, and msg are initialized. inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source stream state was inconsistent (such as zalloc or state being Z_NULL). */ -ZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm, - int windowBits)); +ZEXTERN int ZEXPORT inflateReset2(z_streamp strm, + int windowBits); /* This function is the same as inflateReset, but it also permits changing the wrap and window size requests. The windowBits parameter is interpreted - the same as it is for inflateInit2. + the same as it is for inflateInit2. If the window size is changed, then the + memory allocated for the window is freed, and the window will be reallocated + by inflate() if needed. inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source stream state was inconsistent (such as zalloc or state being Z_NULL), or if the windowBits parameter is invalid. */ -ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm, - int bits, - int value)); +ZEXTERN int ZEXPORT inflatePrime(z_streamp strm, + int bits, + int value); /* This function inserts bits in the inflate input stream. The intent is that this function is used to start inflating at a bit position in the @@ -877,7 +1004,7 @@ ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm, stream state was inconsistent. */ -ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm)); +ZEXTERN long ZEXPORT inflateMark(z_streamp strm); /* This function returns two values, one in the lower 16 bits of the return value, and the other in the remaining upper bits, obtained by shifting the @@ -901,12 +1028,12 @@ ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm)); location in the input stream can be determined from avail_in and data_type as noted in the description for the Z_BLOCK flush parameter for inflate. - inflateMark returns the value noted above or -1 << 16 if the provided + inflateMark returns the value noted above, or -65536 if the provided source stream state was inconsistent. */ -ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm, - gz_headerp head)); +ZEXTERN int ZEXPORT inflateGetHeader(z_streamp strm, + gz_headerp head); /* inflateGetHeader() requests that gzip header information be stored in the provided gz_header structure. inflateGetHeader() may be called after @@ -946,8 +1073,8 @@ ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm, */ /* -ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits, - unsigned char FAR *window)); +ZEXTERN int ZEXPORT inflateBackInit(z_streamp strm, int windowBits, + unsigned char FAR *window); Initialize the internal stream state for decompression using inflateBack() calls. The fields zalloc, zfree and opaque in strm must be initialized @@ -962,24 +1089,26 @@ ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits, See inflateBack() for the usage of these routines. inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of - the paramaters are invalid, Z_MEM_ERROR if the internal state could not be + the parameters are invalid, Z_MEM_ERROR if the internal state could not be allocated, or Z_VERSION_ERROR if the version of the library does not match the version of the header file. */ -typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *)); -typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned)); +typedef unsigned (*in_func)(void FAR *, + z_const unsigned char FAR * FAR *); +typedef int (*out_func)(void FAR *, unsigned char FAR *, unsigned); -ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm, - in_func in, void FAR *in_desc, - out_func out, void FAR *out_desc)); +ZEXTERN int ZEXPORT inflateBack(z_streamp strm, + in_func in, void FAR *in_desc, + out_func out, void FAR *out_desc); /* inflateBack() does a raw inflate with a single call using a call-back - interface for input and output. This is more efficient than inflate() for - file i/o applications in that it avoids copying between the output and the - sliding window by simply making the window itself the output buffer. This - function trusts the application to not change the output buffer passed by - the output function, at least until inflateBack() returns. + interface for input and output. This is potentially more efficient than + inflate() for file i/o applications, in that it avoids copying between the + output and the sliding window by simply making the window itself the output + buffer. inflate() can be faster on modern CPUs when used with large + buffers. inflateBack() trusts the application to not change the output + buffer passed by the output function, at least until inflateBack() returns. inflateBackInit() must be called first to allocate the internal state and to initialize the state with the user-provided window buffer. @@ -991,9 +1120,9 @@ ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm, This routine would normally be used in a utility that reads zip or gzip files and writes out uncompressed files. The utility would decode the header and process the trailer on its own, hence this routine expects only - the raw deflate stream to decompress. This is different from the normal - behavior of inflate(), which expects either a zlib or gzip header and - trailer around the deflate stream. + the raw deflate stream to decompress. This is different from the default + behavior of inflate(), which expects a zlib header and trailer around the + deflate stream. inflateBack() uses two subroutines supplied by the caller that are then called by inflateBack() for input and output. inflateBack() calls those @@ -1002,12 +1131,12 @@ ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm, parameters and return types are defined above in the in_func and out_func typedefs. inflateBack() will call in(in_desc, &buf) which should return the number of bytes of provided input, and a pointer to that input in buf. If - there is no input available, in() must return zero--buf is ignored in that - case--and inflateBack() will return a buffer error. inflateBack() will call - out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out() - should return zero on success, or non-zero on failure. If out() returns - non-zero, inflateBack() will return with an error. Neither in() nor out() - are permitted to change the contents of the window provided to + there is no input available, in() must return zero -- buf is ignored in that + case -- and inflateBack() will return a buffer error. inflateBack() will + call out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. + out() should return zero on success, or non-zero on failure. If out() + returns non-zero, inflateBack() will return with an error. Neither in() nor + out() are permitted to change the contents of the window provided to inflateBackInit(), which is also the buffer that out() uses to write from. The length written by out() will be at most the window size. Any non-zero amount of input may be provided by in(). @@ -1035,11 +1164,11 @@ ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm, using strm->next_in which will be Z_NULL only if in() returned an error. If strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning non-zero. (in() will always be called before out(), so strm->next_in is - assured to be defined if out() returns non-zero.) Note that inflateBack() + assured to be defined if out() returns non-zero.) Note that inflateBack() cannot return Z_OK. */ -ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm)); +ZEXTERN int ZEXPORT inflateBackEnd(z_streamp strm); /* All memory allocated by inflateBackInit() is freed. @@ -1047,7 +1176,7 @@ ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm)); state was inconsistent. */ -ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); +ZEXTERN uLong ZEXPORT zlibCompileFlags(void); /* Return flags indicating compile-time options. Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: @@ -1057,7 +1186,7 @@ ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); 7.6: size of z_off_t Compiler, assembler, and debug options: - 8: DEBUG + 8: ZLIB_DEBUG 9: ASMV or ASMINF -- use ASM code 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention 11: 0 (reserved) @@ -1088,6 +1217,7 @@ ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); 27-31: 0 (reserved) */ +#ifndef Z_SOLO /* utility functions */ @@ -1099,45 +1229,46 @@ ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); you need special options. */ -ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, - const Bytef *source, uLong sourceLen)); +ZEXTERN int ZEXPORT compress(Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen); /* Compresses the source buffer into the destination buffer. sourceLen is the byte length of the source buffer. Upon entry, destLen is the total size of the destination buffer, which must be at least the value returned by compressBound(sourceLen). Upon exit, destLen is the actual size of the - compressed buffer. + compressed data. compress() is equivalent to compress2() with a level + parameter of Z_DEFAULT_COMPRESSION. compress returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if there was not enough room in the output buffer. */ -ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen, - const Bytef *source, uLong sourceLen, - int level)); +ZEXTERN int ZEXPORT compress2(Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen, + int level); /* Compresses the source buffer into the destination buffer. The level parameter has the same meaning as in deflateInit. sourceLen is the byte length of the source buffer. Upon entry, destLen is the total size of the destination buffer, which must be at least the value returned by compressBound(sourceLen). Upon exit, destLen is the actual size of the - compressed buffer. + compressed data. compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if there was not enough room in the output buffer, Z_STREAM_ERROR if the level parameter is invalid. */ -ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen)); +ZEXTERN uLong ZEXPORT compressBound(uLong sourceLen); /* compressBound() returns an upper bound on the compressed size after compress() or compress2() on sourceLen bytes. It would be used before a compress() or compress2() call to allocate the destination buffer. */ -ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, - const Bytef *source, uLong sourceLen)); +ZEXTERN int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen); /* Decompresses the source buffer into the destination buffer. sourceLen is the byte length of the source buffer. Upon entry, destLen is the total size @@ -1145,13 +1276,22 @@ ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, uncompressed data. (The size of the uncompressed data must have been saved previously by the compressor and transmitted to the decompressor by some mechanism outside the scope of this compression library.) Upon exit, destLen - is the actual size of the uncompressed buffer. + is the actual size of the uncompressed data. uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if there was not enough room in the output - buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. + buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. In + the case where there is not enough room, uncompress() will fill the output + buffer with the uncompressed data up to that point. */ +ZEXTERN int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, + const Bytef *source, uLong *sourceLen); +/* + Same as uncompress, except that sourceLen is a pointer, where the + length of the source is *sourceLen. On return, *sourceLen is the number of + source bytes consumed. +*/ /* gzip file access functions */ @@ -1162,23 +1302,38 @@ ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, wrapper, documented in RFC 1952, wrapped around a deflate stream. */ -typedef voidp gzFile; /* opaque gzip file descriptor */ +typedef struct gzFile_s *gzFile; /* semi-opaque gzip file descriptor */ /* -ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); +ZEXTERN gzFile ZEXPORT gzopen(const char *path, const char *mode); + + Open the gzip (.gz) file at path for reading and decompressing, or + compressing and writing. The mode parameter is as in fopen ("rb" or "wb") + but can also include a compression level ("wb9") or a strategy: 'f' for + filtered data as in "wb6f", 'h' for Huffman-only compression as in "wb1h", + 'R' for run-length encoding as in "wb1R", or 'F' for fixed code compression + as in "wb9F". (See the description of deflateInit2 for more information + about the strategy parameter.) 'T' will request transparent writing or + appending with no compression and not using the gzip format. + + "a" can be used instead of "w" to request that the gzip stream that will + be written be appended to the file. "+" will result in an error, since + reading and writing to the same gzip file is not supported. The addition of + "x" when writing will create the file exclusively, which fails if the file + already exists. On systems that support it, the addition of "e" when + reading or writing will set the flag to close the file on an execve() call. - Opens a gzip (.gz) file for reading or writing. The mode parameter is as - in fopen ("rb" or "wb") but can also include a compression level ("wb9") or - a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman-only - compression as in "wb1h", 'R' for run-length encoding as in "wb1R", or 'F' - for fixed code compression as in "wb9F". (See the description of - deflateInit2 for more information about the strategy parameter.) Also "a" - can be used instead of "w" to request that the gzip stream that will be - written be appended to the file. "+" will result in an error, since reading - and writing to the same gzip file is not supported. + These functions, as well as gzip, will read and decode a sequence of gzip + streams in a file. The append function of gzopen() can be used to create + such a file. (Also see gzflush() for another way to do this.) When + appending, gzopen does not test whether the file begins with a gzip stream, + nor does it look for the end of the gzip streams to begin appending. gzopen + will simply append a gzip stream to the existing file. gzopen can be used to read a file which is not in gzip format; in this - case gzread will directly read from the file without decompression. + case gzread will directly read from the file without decompression. When + reading, this will be detected automatically by looking for the magic two- + byte gzip header. gzopen returns NULL if the file could not be opened, if there was insufficient memory to allocate the gzFile state, or if an invalid mode was @@ -1187,17 +1342,21 @@ ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); file could not be opened. */ -ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); +ZEXTERN gzFile ZEXPORT gzdopen(int fd, const char *mode); /* - gzdopen associates a gzFile with the file descriptor fd. File descriptors - are obtained from calls like open, dup, creat, pipe or fileno (if the file - has been previously opened with fopen). The mode parameter is as in gzopen. + Associate a gzFile with the file descriptor fd. File descriptors are + obtained from calls like open, dup, creat, pipe or fileno (if the file has + been previously opened with fopen). The mode parameter is as in gzopen. The next call of gzclose on the returned gzFile will also close the file descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor fd. If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd, mode);. The duplicated descriptor should be saved to avoid a leak, since - gzdopen does not close fd if it fails. + gzdopen does not close fd if it fails. If you are using fileno() to get the + file descriptor from a FILE *, then you will have to use dup() to avoid + double-close()ing the file descriptor. Both gzclose() and fclose() will + close the associated file descriptor, so they need to have different file + descriptors. gzdopen returns NULL if there was insufficient memory to allocate the gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not @@ -1206,16 +1365,15 @@ ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); will not detect if fd is invalid (unless fd is -1). */ -ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size)); +ZEXTERN int ZEXPORT gzbuffer(gzFile file, unsigned size); /* - Set the internal buffer size used by this library's functions. The - default buffer size is 8192 bytes. This function must be called after - gzopen() or gzdopen(), and before any other calls that read or write the - file. The buffer memory allocation is always deferred to the first read or - write. Two buffers are allocated, either both of the specified size when - writing, or one of the specified size and the other twice that size when - reading. A larger buffer size of, for example, 64K or 128K bytes will - noticeably increase the speed of decompression (reading). + Set the internal buffer size used by this library's functions for file to + size. The default buffer size is 8192 bytes. This function must be called + after gzopen() or gzdopen(), and before any other calls that read or write + the file. The buffer memory allocation is always deferred to the first read + or write. Three times that size in buffer space is allocated. A larger + buffer size of, for example, 64K or 128K bytes will noticeably increase the + speed of decompression (reading). The new buffer size also affects the maximum length for gzprintf(). @@ -1223,91 +1381,149 @@ ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size)); too late. */ -ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); +ZEXTERN int ZEXPORT gzsetparams(gzFile file, int level, int strategy); /* - Dynamically update the compression level or strategy. See the description - of deflateInit2 for the meaning of these parameters. + Dynamically update the compression level and strategy for file. See the + description of deflateInit2 for the meaning of these parameters. Previously + provided data is flushed before applying the parameter changes. - gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not - opened for writing. + gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not + opened for writing, Z_ERRNO if there is an error writing the flushed data, + or Z_MEM_ERROR if there is a memory allocation error. */ -ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); +ZEXTERN int ZEXPORT gzread(gzFile file, voidp buf, unsigned len); /* - Reads the given number of uncompressed bytes from the compressed file. If - the input file was not in gzip format, gzread copies the given number of - bytes into the buffer. + Read and decompress up to len uncompressed bytes from file into buf. If + the input file is not in gzip format, gzread copies the given number of + bytes into the buffer directly from the file. After reaching the end of a gzip stream in the input, gzread will continue - to read, looking for another gzip stream, or failing that, reading the rest - of the input file directly without decompression. The entire input file - will be read if gzread is called until it returns less than the requested - len. + to read, looking for another gzip stream. Any number of gzip streams may be + concatenated in the input file, and will all be decompressed by gzread(). + If something other than a gzip stream is encountered after a gzip stream, + that remaining trailing garbage is ignored (and no error is returned). + + gzread can be used to read a gzip file that is being concurrently written. + Upon reaching the end of the input, gzread will return with the available + data. If the error code returned by gzerror is Z_OK or Z_BUF_ERROR, then + gzclearerr can be used to clear the end of file indicator in order to permit + gzread to be tried again. Z_OK indicates that a gzip stream was completed + on the last gzread. Z_BUF_ERROR indicates that the input file ended in the + middle of a gzip stream. Note that gzread does not return -1 in the event + of an incomplete gzip stream. This error is deferred until gzclose(), which + will return Z_BUF_ERROR if the last gzread ended in the middle of a gzip + stream. Alternatively, gzerror can be used before gzclose to detect this + case. gzread returns the number of uncompressed bytes actually read, less than - len for end of file, or -1 for error. + len for end of file, or -1 for error. If len is too large to fit in an int, + then nothing is read, -1 is returned, and the error state is set to + Z_STREAM_ERROR. */ -ZEXTERN int ZEXPORT gzwrite OF((gzFile file, - voidpc buf, unsigned len)); +ZEXTERN z_size_t ZEXPORT gzfread(voidp buf, z_size_t size, z_size_t nitems, + gzFile file); /* - Writes the given number of uncompressed bytes into the compressed file. - gzwrite returns the number of uncompressed bytes written or 0 in case of - error. + Read and decompress up to nitems items of size size from file into buf, + otherwise operating as gzread() does. This duplicates the interface of + stdio's fread(), with size_t request and return types. If the library + defines size_t, then z_size_t is identical to size_t. If not, then z_size_t + is an unsigned integer type that can contain a pointer. + + gzfread() returns the number of full items read of size size, or zero if + the end of the file was reached and a full item could not be read, or if + there was an error. gzerror() must be consulted if zero is returned in + order to determine if there was an error. If the multiplication of size and + nitems overflows, i.e. the product does not fit in a z_size_t, then nothing + is read, zero is returned, and the error state is set to Z_STREAM_ERROR. + + In the event that the end of file is reached and only a partial item is + available at the end, i.e. the remaining uncompressed data length is not a + multiple of size, then the final partial item is nevertheless read into buf + and the end-of-file flag is set. The length of the partial item read is not + provided, but could be inferred from the result of gztell(). This behavior + is the same as the behavior of fread() implementations in common libraries, + but it prevents the direct use of gzfread() to read a concurrently written + file, resetting and retrying on end-of-file, when size is not 1. */ -ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...)); +ZEXTERN int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len); /* - Converts, formats, and writes the arguments to the compressed file under - control of the format string, as in fprintf. gzprintf returns the number of - uncompressed bytes actually written, or 0 in case of error. The number of - uncompressed bytes written is limited to 8191, or one less than the buffer - size given to gzbuffer(). The caller should assure that this limit is not - exceeded. If it is exceeded, then gzprintf() will return an error (0) with - nothing written. In this case, there may also be a buffer overflow with - unpredictable consequences, which is possible only if zlib was compiled with - the insecure functions sprintf() or vsprintf() because the secure snprintf() - or vsnprintf() functions were not available. This can be determined using - zlibCompileFlags(). + Compress and write the len uncompressed bytes at buf to file. gzwrite + returns the number of uncompressed bytes written or 0 in case of error. */ -ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); +ZEXTERN z_size_t ZEXPORT gzfwrite(voidpc buf, z_size_t size, + z_size_t nitems, gzFile file); /* - Writes the given null-terminated string to the compressed file, excluding + Compress and write nitems items of size size from buf to file, duplicating + the interface of stdio's fwrite(), with size_t request and return types. If + the library defines size_t, then z_size_t is identical to size_t. If not, + then z_size_t is an unsigned integer type that can contain a pointer. + + gzfwrite() returns the number of full items written of size size, or zero + if there was an error. If the multiplication of size and nitems overflows, + i.e. the product does not fit in a z_size_t, then nothing is written, zero + is returned, and the error state is set to Z_STREAM_ERROR. +*/ + +ZEXTERN int ZEXPORTVA gzprintf(gzFile file, const char *format, ...); +/* + Convert, format, compress, and write the arguments (...) to file under + control of the string format, as in fprintf. gzprintf returns the number of + uncompressed bytes actually written, or a negative zlib error code in case + of error. The number of uncompressed bytes written is limited to 8191, or + one less than the buffer size given to gzbuffer(). The caller should assure + that this limit is not exceeded. If it is exceeded, then gzprintf() will + return an error (0) with nothing written. In this case, there may also be a + buffer overflow with unpredictable consequences, which is possible only if + zlib was compiled with the insecure functions sprintf() or vsprintf(), + because the secure snprintf() or vsnprintf() functions were not available. + This can be determined using zlibCompileFlags(). +*/ + +ZEXTERN int ZEXPORT gzputs(gzFile file, const char *s); +/* + Compress and write the given null-terminated string s to file, excluding the terminating null character. gzputs returns the number of characters written, or -1 in case of error. */ -ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); +ZEXTERN char * ZEXPORT gzgets(gzFile file, char *buf, int len); /* - Reads bytes from the compressed file until len-1 characters are read, or a - newline character is read and transferred to buf, or an end-of-file - condition is encountered. If any characters are read or if len == 1, the - string is terminated with a null character. If no characters are read due - to an end-of-file or len < 1, then the buffer is left untouched. + Read and decompress bytes from file into buf, until len-1 characters are + read, or until a newline character is read and transferred to buf, or an + end-of-file condition is encountered. If any characters are read or if len + is one, the string is terminated with a null character. If no characters + are read due to an end-of-file or len is less than one, then the buffer is + left untouched. gzgets returns buf which is a null-terminated string, or it returns NULL for end-of-file or in case of error. If there was an error, the contents at buf are indeterminate. */ -ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); +ZEXTERN int ZEXPORT gzputc(gzFile file, int c); /* - Writes c, converted to an unsigned char, into the compressed file. gzputc + Compress and write c, converted to an unsigned char, into file. gzputc returns the value that was written, or -1 in case of error. */ -ZEXTERN int ZEXPORT gzgetc OF((gzFile file)); +ZEXTERN int ZEXPORT gzgetc(gzFile file); /* - Reads one byte from the compressed file. gzgetc returns this byte or -1 - in case of end of file or error. + Read and decompress one byte from file. gzgetc returns this byte or -1 + in case of end of file or error. This is implemented as a macro for speed. + As such, it does not do all of the checking the other functions do. I.e. + it does not check to see if file is NULL, nor whether the structure file + points to has been clobbered or not. */ -ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); +ZEXTERN int ZEXPORT gzungetc(int c, gzFile file); /* - Push one character back onto the stream to be read as the first character - on the next read. At least one character of push-back is allowed. + Push c back onto the stream for file to be read as the first character on + the next read. At least one character of push-back is always allowed. gzungetc() returns the character pushed, or -1 on failure. gzungetc() will fail if c is -1, and may fail if a character has been pushed but not read yet. If gzungetc is used immediately after gzopen or gzdopen, at least the @@ -1316,27 +1532,27 @@ ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); gzseek() or gzrewind(). */ -ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); +ZEXTERN int ZEXPORT gzflush(gzFile file, int flush); /* - Flushes all pending output into the compressed file. The parameter flush - is as in the deflate() function. The return value is the zlib error number - (see function gzerror below). gzflush is only permitted when writing. + Flush all pending output to file. The parameter flush is as in the + deflate() function. The return value is the zlib error number (see function + gzerror below). gzflush is only permitted when writing. If the flush parameter is Z_FINISH, the remaining data is written and the gzip stream is completed in the output. If gzwrite() is called again, a new gzip stream will be started in the output. gzread() is able to read such - concatented gzip streams. + concatenated gzip streams. gzflush should be called only when strictly necessary because it will degrade compression if called too often. */ /* -ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, - z_off_t offset, int whence)); +ZEXTERN z_off_t ZEXPORT gzseek(gzFile file, + z_off_t offset, int whence); - Sets the starting position for the next gzread or gzwrite on the given - compressed file. The offset represents a number of bytes in the + Set the starting position to offset relative to whence for the next gzread + or gzwrite on file. The offset represents a number of bytes in the uncompressed data stream. The whence parameter is defined as in lseek(2); the value SEEK_END is not supported. @@ -1351,55 +1567,53 @@ ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, would be before the current position. */ -ZEXTERN int ZEXPORT gzrewind OF((gzFile file)); +ZEXTERN int ZEXPORT gzrewind(gzFile file); /* - Rewinds the given file. This function is supported only for reading. + Rewind file. This function is supported only for reading. - gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) + gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET). */ /* -ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); +ZEXTERN z_off_t ZEXPORT gztell(gzFile file); - Returns the starting position for the next gzread or gzwrite on the given - compressed file. This position represents a number of bytes in the - uncompressed data stream, and is zero when starting, even if appending or - reading a gzip stream from the middle of a file using gzdopen(). + Return the starting position for the next gzread or gzwrite on file. + This position represents a number of bytes in the uncompressed data stream, + and is zero when starting, even if appending or reading a gzip stream from + the middle of a file using gzdopen(). gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) */ /* -ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file)); +ZEXTERN z_off_t ZEXPORT gzoffset(gzFile file); - Returns the current offset in the file being read or written. This offset - includes the count of bytes that precede the gzip stream, for example when - appending or when using gzdopen() for reading. When reading, the offset - does not include as yet unused buffered input. This information can be used - for a progress indicator. On error, gzoffset() returns -1. + Return the current compressed (actual) read or write offset of file. This + offset includes the count of bytes that precede the gzip stream, for example + when appending or when using gzdopen() for reading. When reading, the + offset does not include as yet unused buffered input. This information can + be used for a progress indicator. On error, gzoffset() returns -1. */ -ZEXTERN int ZEXPORT gzeof OF((gzFile file)); +ZEXTERN int ZEXPORT gzeof(gzFile file); /* - Returns true (1) if the end-of-file indicator has been set while reading, - false (0) otherwise. Note that the end-of-file indicator is set only if the - read tried to go past the end of the input, but came up short. Therefore, - just like feof(), gzeof() may return false even if there is no more data to - read, in the event that the last read request was for the exact number of - bytes remaining in the input file. This will happen if the input file size - is an exact multiple of the buffer size. + Return true (1) if the end-of-file indicator for file has been set while + reading, false (0) otherwise. Note that the end-of-file indicator is set + only if the read tried to go past the end of the input, but came up short. + Therefore, just like feof(), gzeof() may return false even if there is no + more data to read, in the event that the last read request was for the exact + number of bytes remaining in the input file. This will happen if the input + file size is an exact multiple of the buffer size. If gzeof() returns true, then the read functions will return no more data, unless the end-of-file indicator is reset by gzclearerr() and the input file has grown since the previous end of file was detected. */ -ZEXTERN int ZEXPORT gzdirect OF((gzFile file)); +ZEXTERN int ZEXPORT gzdirect(gzFile file); /* - Returns true (1) if file is being copied directly while reading, or false - (0) if file is a gzip stream being decompressed. This state can change from - false to true while reading the input file if the end of a gzip stream is - reached, but is followed by data that is not another gzip stream. + Return true (1) if file is being copied directly while reading, or false + (0) if file is a gzip stream being decompressed. If the input file is empty, gzdirect() will return true, since the input does not contain a gzip stream. @@ -1408,22 +1622,30 @@ ZEXTERN int ZEXPORT gzdirect OF((gzFile file)); cause buffers to be allocated to allow reading the file to determine if it is a gzip file. Therefore if gzbuffer() is used, it should be called before gzdirect(). + + When writing, gzdirect() returns true (1) if transparent writing was + requested ("wT" for the gzopen() mode), or false (0) otherwise. (Note: + gzdirect() is not needed when writing. Transparent writing must be + explicitly requested, so the application already knows the answer. When + linking statically, using gzdirect() will include all of the zlib code for + gzip file reading and decompression, which may not be desired.) */ -ZEXTERN int ZEXPORT gzclose OF((gzFile file)); +ZEXTERN int ZEXPORT gzclose(gzFile file); /* - Flushes all pending output if necessary, closes the compressed file and - deallocates the (de)compression state. Note that once file is closed, you + Flush all pending output for file, if necessary, close file and + deallocate the (de)compression state. Note that once file is closed, you cannot call gzerror with file, since its structures have been deallocated. gzclose must not be called more than once on the same file, just as free must not be called more than once on the same allocation. gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a - file operation error, or Z_OK on success. + file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the + last read ended in the middle of a gzip stream, or Z_OK on success. */ -ZEXTERN int ZEXPORT gzclose_r OF((gzFile file)); -ZEXTERN int ZEXPORT gzclose_w OF((gzFile file)); +ZEXTERN int ZEXPORT gzclose_r(gzFile file); +ZEXTERN int ZEXPORT gzclose_w(gzFile file); /* Same as gzclose(), but gzclose_r() is only for use when reading, and gzclose_w() is only for use when writing or appending. The advantage to @@ -1434,12 +1656,12 @@ ZEXTERN int ZEXPORT gzclose_w OF((gzFile file)); zlib library. */ -ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); +ZEXTERN const char * ZEXPORT gzerror(gzFile file, int *errnum); /* - Returns the error message for the last error which occurred on the given - compressed file. errnum is set to zlib error number. If an error occurred - in the file system and not in the compression library, errnum is set to - Z_ERRNO and the application may consult errno to get the exact error code. + Return the error message for the last error which occurred on file. + errnum is set to zlib error number. If an error occurred in the file system + and not in the compression library, errnum is set to Z_ERRNO and the + application may consult errno to get the exact error code. The application must not modify the returned string. Future calls to this function may invalidate the previously returned string. If file is @@ -1450,13 +1672,14 @@ ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); functions above that do not distinguish those cases in their return values. */ -ZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); +ZEXTERN void ZEXPORT gzclearerr(gzFile file); /* - Clears the error and end-of-file flags for file. This is analogous to the + Clear the error and end-of-file flags for file. This is analogous to the clearerr() function in stdio. This is useful for continuing to read a gzip file that is being written concurrently. */ +#endif /* !Z_SOLO */ /* checksum functions */ @@ -1466,13 +1689,14 @@ ZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); library. */ -ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); +ZEXTERN uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len); /* Update a running Adler-32 checksum with the bytes buf[0..len-1] and - return the updated checksum. If buf is Z_NULL, this function returns the - required initial value for the checksum. + return the updated checksum. An Adler-32 value is in the range of a 32-bit + unsigned integer. If buf is Z_NULL, this function returns the required + initial value for the checksum. - An Adler-32 checksum is almost as reliable as a CRC32 but can be computed + An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed much faster. Usage example: @@ -1485,23 +1709,31 @@ ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); if (adler != original_adler) error(); */ +ZEXTERN uLong ZEXPORT adler32_z(uLong adler, const Bytef *buf, + z_size_t len); +/* + Same as adler32(), but with a size_t length. +*/ + /* -ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2, - z_off_t len2)); +ZEXTERN uLong ZEXPORT adler32_combine(uLong adler1, uLong adler2, + z_off_t len2); Combine two Adler-32 checksums into one. For two sequences of bytes, seq1 and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of - seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. + seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. Note + that the z_off_t type (like off_t) is a signed integer. If len2 is + negative, the result has no meaning or utility. */ -ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); +ZEXTERN uLong ZEXPORT crc32(uLong crc, const Bytef *buf, uInt len); /* Update a running CRC-32 with the bytes buf[0..len-1] and return the - updated CRC-32. If buf is Z_NULL, this function returns the required - initial value for the for the crc. Pre- and post-conditioning (one's - complement) is performed within this function so it shouldn't be done by the - application. + updated CRC-32. A CRC-32 value is in the range of a 32-bit unsigned integer. + If buf is Z_NULL, this function returns the required initial value for the + crc. Pre- and post-conditioning (one's complement) is performed within this + function so it shouldn't be done by the application. Usage example: @@ -1513,14 +1745,34 @@ ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); if (crc != original_crc) error(); */ +ZEXTERN uLong ZEXPORT crc32_z(uLong crc, const Bytef *buf, + z_size_t len); /* -ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2)); + Same as crc32(), but with a size_t length. +*/ + +/* +ZEXTERN uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2); Combine two CRC-32 check values into one. For two sequences of bytes, seq1 and seq2 with lengths len1 and len2, CRC-32 check values were calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32 check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and - len2. + len2. len2 must be non-negative. +*/ + +/* +ZEXTERN uLong ZEXPORT crc32_combine_gen(z_off_t len2); + + Return the operator corresponding to length len2, to be used with + crc32_combine_op(). len2 must be non-negative. +*/ + +ZEXTERN uLong ZEXPORT crc32_combine_op(uLong crc1, uLong crc2, uLong op); +/* + Give the same result as crc32_combine(), using op in place of len2. op is + is generated from len2 by crc32_combine_gen(). This will be faster than + crc32_combine() if the generated op is used more than once. */ @@ -1529,32 +1781,73 @@ ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2)); /* deflateInit and inflateInit are macros to allow checking the zlib version * and the compiler's view of z_stream: */ -ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level, - const char *version, int stream_size)); -ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, - const char *version, int stream_size)); -ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method, - int windowBits, int memLevel, - int strategy, const char *version, - int stream_size)); -ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, - const char *version, int stream_size)); -ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits, - unsigned char FAR *window, - const char *version, - int stream_size)); -#define deflateInit(strm, level) \ - deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream)) -#define inflateInit(strm) \ - inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream)) -#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ - deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ - (strategy), ZLIB_VERSION, sizeof(z_stream)) -#define inflateInit2(strm, windowBits) \ - inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream)) -#define inflateBackInit(strm, windowBits, window) \ - inflateBackInit_((strm), (windowBits), (window), \ - ZLIB_VERSION, sizeof(z_stream)) +ZEXTERN int ZEXPORT deflateInit_(z_streamp strm, int level, + const char *version, int stream_size); +ZEXTERN int ZEXPORT inflateInit_(z_streamp strm, + const char *version, int stream_size); +ZEXTERN int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, + int windowBits, int memLevel, + int strategy, const char *version, + int stream_size); +ZEXTERN int ZEXPORT inflateInit2_(z_streamp strm, int windowBits, + const char *version, int stream_size); +ZEXTERN int ZEXPORT inflateBackInit_(z_streamp strm, int windowBits, + unsigned char FAR *window, + const char *version, + int stream_size); +#ifdef Z_PREFIX_SET +# define z_deflateInit(strm, level) \ + deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_inflateInit(strm) \ + inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ + (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_inflateInit2(strm, windowBits) \ + inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ + (int)sizeof(z_stream)) +# define z_inflateBackInit(strm, windowBits, window) \ + inflateBackInit_((strm), (windowBits), (window), \ + ZLIB_VERSION, (int)sizeof(z_stream)) +#else +# define deflateInit(strm, level) \ + deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) +# define inflateInit(strm) \ + inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) +# define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ + (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) +# define inflateInit2(strm, windowBits) \ + inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ + (int)sizeof(z_stream)) +# define inflateBackInit(strm, windowBits, window) \ + inflateBackInit_((strm), (windowBits), (window), \ + ZLIB_VERSION, (int)sizeof(z_stream)) +#endif + +#ifndef Z_SOLO + +/* gzgetc() macro and its supporting function and exposed data structure. Note + * that the real internal state is much larger than the exposed structure. + * This abbreviated structure exposes just enough for the gzgetc() macro. The + * user should not mess with these exposed elements, since their names or + * behavior could change in the future, perhaps even capriciously. They can + * only be used by the gzgetc() macro. You have been warned. + */ +struct gzFile_s { + unsigned have; + unsigned char *next; + z_off64_t pos; +}; +ZEXTERN int ZEXPORT gzgetc_(gzFile file); /* backward compatibility */ +#ifdef Z_PREFIX_SET +# undef z_gzgetc +# define z_gzgetc(g) \ + ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) +#else +# define gzgetc(g) \ + ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) +#endif /* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if @@ -1562,49 +1855,81 @@ ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits, * functions are changed to 64 bits) -- in case these are set on systems * without large file support, _LFS64_LARGEFILE must also be true */ -#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0 - ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); - ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); - ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); - ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); - ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t)); - ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t)); +#ifdef Z_LARGE64 + ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *); + ZEXTERN z_off64_t ZEXPORT gzseek64(gzFile, z_off64_t, int); + ZEXTERN z_off64_t ZEXPORT gztell64(gzFile); + ZEXTERN z_off64_t ZEXPORT gzoffset64(gzFile); + ZEXTERN uLong ZEXPORT adler32_combine64(uLong, uLong, z_off64_t); + ZEXTERN uLong ZEXPORT crc32_combine64(uLong, uLong, z_off64_t); + ZEXTERN uLong ZEXPORT crc32_combine_gen64(z_off64_t); #endif -#if !defined(ZLIB_INTERNAL) && _FILE_OFFSET_BITS-0 == 64 && _LFS64_LARGEFILE-0 -# define gzopen gzopen64 -# define gzseek gzseek64 -# define gztell gztell64 -# define gzoffset gzoffset64 -# define adler32_combine adler32_combine64 -# define crc32_combine crc32_combine64 -# ifdef _LARGEFILE64_SOURCE - ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); - ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int)); - ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile)); - ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile)); - ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); - ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); +#if !defined(ZLIB_INTERNAL) && defined(Z_WANT64) +# ifdef Z_PREFIX_SET +# define z_gzopen z_gzopen64 +# define z_gzseek z_gzseek64 +# define z_gztell z_gztell64 +# define z_gzoffset z_gzoffset64 +# define z_adler32_combine z_adler32_combine64 +# define z_crc32_combine z_crc32_combine64 +# define z_crc32_combine_gen z_crc32_combine_gen64 +# else +# define gzopen gzopen64 +# define gzseek gzseek64 +# define gztell gztell64 +# define gzoffset gzoffset64 +# define adler32_combine adler32_combine64 +# define crc32_combine crc32_combine64 +# define crc32_combine_gen crc32_combine_gen64 +# endif +# ifndef Z_LARGE64 + ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *); + ZEXTERN z_off_t ZEXPORT gzseek64(gzFile, z_off_t, int); + ZEXTERN z_off_t ZEXPORT gztell64(gzFile); + ZEXTERN z_off_t ZEXPORT gzoffset64(gzFile); + ZEXTERN uLong ZEXPORT adler32_combine64(uLong, uLong, z_off_t); + ZEXTERN uLong ZEXPORT crc32_combine64(uLong, uLong, z_off_t); + ZEXTERN uLong ZEXPORT crc32_combine_gen64(z_off_t); # endif #else - ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *)); - ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int)); - ZEXTERN z_off_t ZEXPORT gztell OF((gzFile)); - ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile)); - ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); - ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); + ZEXTERN gzFile ZEXPORT gzopen(const char *, const char *); + ZEXTERN z_off_t ZEXPORT gzseek(gzFile, z_off_t, int); + ZEXTERN z_off_t ZEXPORT gztell(gzFile); + ZEXTERN z_off_t ZEXPORT gzoffset(gzFile); + ZEXTERN uLong ZEXPORT adler32_combine(uLong, uLong, z_off_t); + ZEXTERN uLong ZEXPORT crc32_combine(uLong, uLong, z_off_t); + ZEXTERN uLong ZEXPORT crc32_combine_gen(z_off_t); #endif -/* hack for buggy compilers */ -#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL) - struct internal_state {int dummy;}; -#endif +#else /* Z_SOLO */ + + ZEXTERN uLong ZEXPORT adler32_combine(uLong, uLong, z_off_t); + ZEXTERN uLong ZEXPORT crc32_combine(uLong, uLong, z_off_t); + ZEXTERN uLong ZEXPORT crc32_combine_gen(z_off_t); + +#endif /* !Z_SOLO */ /* undocumented functions */ -ZEXTERN const char * ZEXPORT zError OF((int)); -ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp)); -ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void)); -ZEXTERN int ZEXPORT inflateUndermine OF((z_streamp, int)); +ZEXTERN const char * ZEXPORT zError(int); +ZEXTERN int ZEXPORT inflateSyncPoint(z_streamp); +ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table(void); +ZEXTERN int ZEXPORT inflateUndermine(z_streamp, int); +ZEXTERN int ZEXPORT inflateValidate(z_streamp, int); +ZEXTERN unsigned long ZEXPORT inflateCodesUsed(z_streamp); +ZEXTERN int ZEXPORT inflateResetKeep(z_streamp); +ZEXTERN int ZEXPORT deflateResetKeep(z_streamp); +#if defined(_WIN32) && !defined(Z_SOLO) +ZEXTERN gzFile ZEXPORT gzopen_w(const wchar_t *path, + const char *mode); +#endif +#if defined(STDC) || defined(Z_HAVE_STDARG_H) +# ifndef Z_SOLO +ZEXTERN int ZEXPORTVA gzvprintf(gzFile file, + const char *format, + va_list va); +# endif +#endif #ifdef __cplusplus } diff --git a/deps/zlib/zutil.c b/deps/zlib/zutil.c index 898ed345b0e..b1c5d2d3c6d 100644 --- a/deps/zlib/zutil.c +++ b/deps/zlib/zutil.c @@ -1,36 +1,34 @@ /* zutil.c -- target dependent utility functions for the compression library - * Copyright (C) 1995-2005, 2010 Jean-loup Gailly. + * Copyright (C) 1995-2017 Jean-loup Gailly * For conditions of distribution and use, see copyright notice in zlib.h */ /* @(#) $Id$ */ #include "zutil.h" - -#ifndef NO_DUMMY_DECL -struct internal_state {int dummy;}; /* for buggy compilers */ +#ifndef Z_SOLO +# include "gzguts.h" #endif -const char * const z_errmsg[10] = { -"need dictionary", /* Z_NEED_DICT 2 */ -"stream end", /* Z_STREAM_END 1 */ -"", /* Z_OK 0 */ -"file error", /* Z_ERRNO (-1) */ -"stream error", /* Z_STREAM_ERROR (-2) */ -"data error", /* Z_DATA_ERROR (-3) */ -"insufficient memory", /* Z_MEM_ERROR (-4) */ -"buffer error", /* Z_BUF_ERROR (-5) */ -"incompatible version",/* Z_VERSION_ERROR (-6) */ -""}; - - -const char * ZEXPORT zlibVersion() -{ +z_const char * const z_errmsg[10] = { + (z_const char *)"need dictionary", /* Z_NEED_DICT 2 */ + (z_const char *)"stream end", /* Z_STREAM_END 1 */ + (z_const char *)"", /* Z_OK 0 */ + (z_const char *)"file error", /* Z_ERRNO (-1) */ + (z_const char *)"stream error", /* Z_STREAM_ERROR (-2) */ + (z_const char *)"data error", /* Z_DATA_ERROR (-3) */ + (z_const char *)"insufficient memory", /* Z_MEM_ERROR (-4) */ + (z_const char *)"buffer error", /* Z_BUF_ERROR (-5) */ + (z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */ + (z_const char *)"" +}; + + +const char * ZEXPORT zlibVersion(void) { return ZLIB_VERSION; } -uLong ZEXPORT zlibCompileFlags() -{ +uLong ZEXPORT zlibCompileFlags(void) { uLong flags; flags = 0; @@ -58,12 +56,14 @@ uLong ZEXPORT zlibCompileFlags() case 8: flags += 2 << 6; break; default: flags += 3 << 6; } -#ifdef DEBUG +#ifdef ZLIB_DEBUG flags += 1 << 8; #endif + /* #if defined(ASMV) || defined(ASMINF) flags += 1 << 9; #endif + */ #ifdef ZLIB_WINAPI flags += 1 << 10; #endif @@ -85,43 +85,41 @@ uLong ZEXPORT zlibCompileFlags() #ifdef FASTEST flags += 1L << 21; #endif -#ifdef STDC +#if defined(STDC) || defined(Z_HAVE_STDARG_H) # ifdef NO_vsnprintf - flags += 1L << 25; + flags += 1L << 25; # ifdef HAS_vsprintf_void - flags += 1L << 26; + flags += 1L << 26; # endif # else # ifdef HAS_vsnprintf_void - flags += 1L << 26; + flags += 1L << 26; # endif # endif #else - flags += 1L << 24; + flags += 1L << 24; # ifdef NO_snprintf - flags += 1L << 25; + flags += 1L << 25; # ifdef HAS_sprintf_void - flags += 1L << 26; + flags += 1L << 26; # endif # else # ifdef HAS_snprintf_void - flags += 1L << 26; + flags += 1L << 26; # endif # endif #endif return flags; } -#ifdef DEBUG - +#ifdef ZLIB_DEBUG +#include # ifndef verbose # define verbose 0 # endif int ZLIB_INTERNAL z_verbose = verbose; -void ZLIB_INTERNAL z_error (m) - char *m; -{ +void ZLIB_INTERNAL z_error(char *m) { fprintf(stderr, "%s\n", m); exit(1); } @@ -130,14 +128,12 @@ void ZLIB_INTERNAL z_error (m) /* exported to allow conversion of error code to string for compress() and * uncompress() */ -const char * ZEXPORT zError(err) - int err; -{ +const char * ZEXPORT zError(int err) { return ERR_MSG(err); } -#if defined(_WIN32_WCE) - /* The Microsoft C Run-Time Library for Windows CE doesn't have +#if defined(_WIN32_WCE) && _WIN32_WCE < 0x800 + /* The older Microsoft C Run-Time Library for Windows CE doesn't have * errno. We define it as a global variable to simplify porting. * Its value is always 0 and should not be used. */ @@ -146,22 +142,14 @@ const char * ZEXPORT zError(err) #ifndef HAVE_MEMCPY -void ZLIB_INTERNAL zmemcpy(dest, source, len) - Bytef* dest; - const Bytef* source; - uInt len; -{ +void ZLIB_INTERNAL zmemcpy(Bytef* dest, const Bytef* source, uInt len) { if (len == 0) return; do { *dest++ = *source++; /* ??? to be unrolled */ } while (--len != 0); } -int ZLIB_INTERNAL zmemcmp(s1, s2, len) - const Bytef* s1; - const Bytef* s2; - uInt len; -{ +int ZLIB_INTERNAL zmemcmp(const Bytef* s1, const Bytef* s2, uInt len) { uInt j; for (j = 0; j < len; j++) { @@ -170,10 +158,7 @@ int ZLIB_INTERNAL zmemcmp(s1, s2, len) return 0; } -void ZLIB_INTERNAL zmemzero(dest, len) - Bytef* dest; - uInt len; -{ +void ZLIB_INTERNAL zmemzero(Bytef* dest, uInt len) { if (len == 0) return; do { *dest++ = 0; /* ??? to be unrolled */ @@ -181,6 +166,7 @@ void ZLIB_INTERNAL zmemzero(dest, len) } #endif +#ifndef Z_SOLO #ifdef SYS16BIT @@ -213,11 +199,12 @@ local ptr_table table[MAX_PTR]; * a protected system like OS/2. Use Microsoft C instead. */ -voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size) -{ - voidpf buf = opaque; /* just to make some compilers happy */ +voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) { + voidpf buf; ulg bsize = (ulg)items*size; + (void)opaque; + /* If we allocate less than 65520 bytes, we assume that farmalloc * will return a usable pointer which doesn't have to be normalized. */ @@ -237,9 +224,11 @@ voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size) return buf; } -void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) -{ +void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) { int n; + + (void)opaque; + if (*(ush*)&ptr != 0) { /* object < 64K */ farfree(ptr); return; @@ -255,7 +244,6 @@ void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) next_ptr--; return; } - ptr = opaque; /* just to make some compilers happy */ Assert(0, "zcfree: ptr not found"); } @@ -272,15 +260,13 @@ void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) # define _hfree hfree #endif -voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size) -{ - if (opaque) opaque = 0; /* to make compiler happy */ +voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size) { + (void)opaque; return _halloc((long)items, size); } -void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) -{ - if (opaque) opaque = 0; /* to make compiler happy */ +void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) { + (void)opaque; _hfree(ptr); } @@ -292,27 +278,22 @@ void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) #ifndef MY_ZCALLOC /* Any system without a special alloc function */ #ifndef STDC -extern voidp malloc OF((uInt size)); -extern voidp calloc OF((uInt items, uInt size)); -extern void free OF((voidpf ptr)); +extern voidp malloc(uInt size); +extern voidp calloc(uInt items, uInt size); +extern void free(voidpf ptr); #endif -voidpf ZLIB_INTERNAL zcalloc (opaque, items, size) - voidpf opaque; - unsigned items; - unsigned size; -{ - if (opaque) items += size - size; /* make compiler happy */ +voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) { + (void)opaque; return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : (voidpf)calloc(items, size); } -void ZLIB_INTERNAL zcfree (opaque, ptr) - voidpf opaque; - voidpf ptr; -{ +void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) { + (void)opaque; free(ptr); - if (opaque) return; /* make compiler happy */ } #endif /* MY_ZCALLOC */ + +#endif /* !Z_SOLO */ diff --git a/deps/zlib/zutil.h b/deps/zlib/zutil.h index 258fa88799a..48dd7febae6 100644 --- a/deps/zlib/zutil.h +++ b/deps/zlib/zutil.h @@ -1,5 +1,5 @@ /* zutil.h -- internal interface and configuration of the compression library - * Copyright (C) 1995-2010 Jean-loup Gailly. + * Copyright (C) 1995-2024 Jean-loup Gailly, Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -13,7 +13,7 @@ #ifndef ZUTIL_H #define ZUTIL_H -#if ((__GNUC__-0) * 10 + __GNUC_MINOR__-0 >= 33) && !defined(NO_VIZ) +#ifdef HAVE_HIDDEN # define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) #else # define ZLIB_INTERNAL @@ -21,7 +21,7 @@ #include "zlib.h" -#ifdef STDC +#if defined(STDC) && !defined(Z_SOLO) # if !(defined(_WIN32_WCE) && defined(_MSC_VER)) # include # endif @@ -32,7 +32,9 @@ #ifndef local # define local static #endif -/* compile with -Dlocal if your debugger can't find static symbols */ +/* since "static" is used to mean two completely different things in C, we + define "local" for the non-static meaning of "static", for readability + (compile with -Dlocal if your debugger can't find static symbols) */ typedef unsigned char uch; typedef uch FAR uchf; @@ -40,13 +42,24 @@ typedef unsigned short ush; typedef ush FAR ushf; typedef unsigned long ulg; -extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ +#if !defined(Z_U8) && !defined(Z_SOLO) && defined(STDC) +# include +# if (ULONG_MAX == 0xffffffffffffffff) +# define Z_U8 unsigned long +# elif (ULLONG_MAX == 0xffffffffffffffff) +# define Z_U8 unsigned long long +# elif (UINT_MAX == 0xffffffffffffffff) +# define Z_U8 unsigned +# endif +#endif + +extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ /* (size given to avoid silly warnings with Visual C++) */ -#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)] +#define ERR_MSG(err) z_errmsg[(err) < -6 || (err) > 2 ? 9 : 2 - (err)] #define ERR_RETURN(strm,err) \ - return (strm->msg = (char*)ERR_MSG(err), (err)) + return (strm->msg = ERR_MSG(err), (err)) /* To be used only when the state is known to be valid */ /* common constants */ @@ -78,97 +91,94 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32)) # define OS_CODE 0x00 -# if defined(__TURBOC__) || defined(__BORLANDC__) -# if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) - /* Allow compilation with ANSI keywords only enabled */ - void _Cdecl farfree( void *block ); - void *_Cdecl farmalloc( unsigned long nbytes ); -# else -# include +# ifndef Z_SOLO +# if defined(__TURBOC__) || defined(__BORLANDC__) +# if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) + /* Allow compilation with ANSI keywords only enabled */ + void _Cdecl farfree( void *block ); + void *_Cdecl farmalloc( unsigned long nbytes ); +# else +# include +# endif +# else /* MSC or DJGPP */ +# include # endif -# else /* MSC or DJGPP */ -# include # endif #endif #ifdef AMIGA -# define OS_CODE 0x01 +# define OS_CODE 1 #endif #if defined(VAXC) || defined(VMS) -# define OS_CODE 0x02 +# define OS_CODE 2 # define F_OPEN(name, mode) \ fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512") #endif +#ifdef __370__ +# if __TARGET_LIB__ < 0x20000000 +# define OS_CODE 4 +# elif __TARGET_LIB__ < 0x40000000 +# define OS_CODE 11 +# else +# define OS_CODE 8 +# endif +#endif + #if defined(ATARI) || defined(atarist) -# define OS_CODE 0x05 +# define OS_CODE 5 #endif #ifdef OS2 -# define OS_CODE 0x06 -# ifdef M_I86 +# define OS_CODE 6 +# if defined(M_I86) && !defined(Z_SOLO) # include # endif #endif -#if defined(MACOS) || defined(TARGET_OS_MAC) -# define OS_CODE 0x07 -# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os -# include /* for fdopen */ -# else -# ifndef fdopen -# define fdopen(fd,mode) NULL /* No fdopen() */ -# endif -# endif +#if defined(MACOS) +# define OS_CODE 7 #endif -#ifdef TOPS20 -# define OS_CODE 0x0a +#ifdef __acorn +# define OS_CODE 13 #endif -#ifdef WIN32 -# ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */ -# define OS_CODE 0x0b -# endif +#if defined(WIN32) && !defined(__CYGWIN__) +# define OS_CODE 10 #endif -#ifdef __50SERIES /* Prime/PRIMOS */ -# define OS_CODE 0x0f +#ifdef _BEOS_ +# define OS_CODE 16 #endif -#if defined(_BEOS_) || defined(RISCOS) -# define fdopen(fd,mode) NULL /* No fdopen() */ +#ifdef __TOS_OS400__ +# define OS_CODE 18 #endif -#if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX -# if defined(_WIN32_WCE) -# define fdopen(fd,mode) NULL /* No fdopen() */ -# ifndef _PTRDIFF_T_DEFINED - typedef int ptrdiff_t; -# define _PTRDIFF_T_DEFINED -# endif -# else -# define fdopen(fd,type) _fdopen(fd,type) -# endif +#ifdef __APPLE__ +# define OS_CODE 19 #endif -#if defined(__BORLANDC__) +#if defined(__BORLANDC__) && !defined(MSDOS) #pragma warn -8004 #pragma warn -8008 #pragma warn -8066 #endif /* provide prototypes for these when building zlib without LFS */ -#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 - ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); - ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); +#if !defined(_WIN32) && \ + (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0) + ZEXTERN uLong ZEXPORT adler32_combine64(uLong, uLong, z_off_t); + ZEXTERN uLong ZEXPORT crc32_combine64(uLong, uLong, z_off_t); + ZEXTERN uLong ZEXPORT crc32_combine_gen64(z_off_t); #endif /* common defaults */ #ifndef OS_CODE -# define OS_CODE 0x03 /* assume Unix */ +# define OS_CODE 3 /* assume Unix */ #endif #ifndef F_OPEN @@ -177,42 +187,7 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ /* functions */ -#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) -# ifndef HAVE_VSNPRINTF -# define HAVE_VSNPRINTF -# endif -#endif -#if defined(__CYGWIN__) -# ifndef HAVE_VSNPRINTF -# define HAVE_VSNPRINTF -# endif -#endif -#ifndef HAVE_VSNPRINTF -# ifdef MSDOS - /* vsnprintf may exist on some MS-DOS compilers (DJGPP?), - but for now we just assume it doesn't. */ -# define NO_vsnprintf -# endif -# ifdef __TURBOC__ -# define NO_vsnprintf -# endif -# ifdef WIN32 - /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ -# if !defined(vsnprintf) && !defined(NO_vsnprintf) -# if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 ) -# define vsnprintf _vsnprintf -# endif -# endif -# endif -# ifdef __SASC -# define NO_vsnprintf -# endif -#endif -#ifdef VMS -# define NO_vsnprintf -#endif - -#if defined(pyr) +#if defined(pyr) || defined(Z_SOLO) # define NO_MEMCPY #endif #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__) @@ -236,16 +211,16 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ # define zmemzero(dest, len) memset(dest, 0, len) # endif #else - void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len)); - int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len)); - void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len)); + void ZLIB_INTERNAL zmemcpy(Bytef* dest, const Bytef* source, uInt len); + int ZLIB_INTERNAL zmemcmp(const Bytef* s1, const Bytef* s2, uInt len); + void ZLIB_INTERNAL zmemzero(Bytef* dest, uInt len); #endif /* Diagnostic functions */ -#ifdef DEBUG +#ifdef ZLIB_DEBUG # include extern int ZLIB_INTERNAL z_verbose; - extern void ZLIB_INTERNAL z_error OF((char *m)); + extern void ZLIB_INTERNAL z_error(char *m); # define Assert(cond,msg) {if(!(cond)) z_error(msg);} # define Trace(x) {if (z_verbose>=0) fprintf x ;} # define Tracev(x) {if (z_verbose>0) fprintf x ;} @@ -261,14 +236,19 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ # define Tracecv(c,x) #endif - -voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items, - unsigned size)); -void ZLIB_INTERNAL zcfree OF((voidpf opaque, voidpf ptr)); +#ifndef Z_SOLO + voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, + unsigned size); + void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr); +#endif #define ZALLOC(strm, items, size) \ (*((strm)->zalloc))((strm)->opaque, (items), (size)) #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) #define TRY_FREE(s, p) {if (p) ZFREE(s, p);} +/* Reverse the bytes in a 32-bit value */ +#define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ + (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) + #endif /* ZUTIL_H */ diff --git a/docs/api-stability.md b/docs/api-stability.md new file mode 100644 index 00000000000..a02e864d1ad --- /dev/null +++ b/docs/api-stability.md @@ -0,0 +1,63 @@ +The maintainers of the libgit2 project believe that having a stable API +to program against is important for our users and the ecosystem - whether +you're building against the libgit2 C APIs directly, creating a wrapper to +a managed language, or programming against one of those managed wrappers +like LibGit2Sharp or Rugged. + +Our API stability considerations are: + +* Our standard API is considered stable through a major release. + + * We define our "standard API" to be anything included in the "git2.h" + header - in other words, anything defined in a header in the `git2` + directory. + + * APIs will maintain their signature and will not be removed within a + major release, but new APIs may be added. + + * Any APIs may be marked as deprecated within a major release, but will + not be removed until the next major release (at the earliest). You + may define `GIT_DEPRECATE_HARD` to produce compiler warnings if you + target these deprecated APIs. + + * We consider API compatibility to be against the C APIs. That means + that we may use macros to keep API compatibility - for example, if we + rename a structure from `git_widget_options` to `git_foobar_options` + then we would `#define git_widget_options git_foobar_options` to retain + API compatibility. Note that this does _not_ provide ABI compatibility. + +* Our systems API is only considered stable through a _minor_ release. + + * We define our "systems API" to be anything included in the `git2/sys` + directory. These are not "standard" APIs but are mechanisms to extend + libgit2 by adding new extensions - for example, a custom HTTPS transport, + TLS engine, or merge strategy. + + * Additionally, the cmake options and the resulting constants that it + produces to be "systems API". + + * Generally these mechanism are well defined and will not need significant + changes, but are considered a part of the library itself and may need + + * Systems API changes will be noted specially within a release's changelog. + +* Our ABI is only considered stable through a _minor_ release. + + * Our ABI consists of actual symbol names in the library, the function + signatures, and the actual layout of structures. These are only + stable within minor releases, they are not stable within major releases + (yet). + + * Since many FFIs use ABIs directly (for example, .NET P/Invoke or Rust), + this instability is unfortunate. + + * In a future major release, we will begin providing ABI stability + throughout the major release cycle. + + * ABI changes will be noted specially within a release's changelog. + +* Point releases are _generally_ only for bugfixes, and generally do _not_ + include new features. This means that point releases generally do _not_ + include new APIs. Point releases will never break API, systems API or + ABI compatibility. + diff --git a/docs/buffers.md b/docs/buffers.md new file mode 100644 index 00000000000..2f2148bc527 --- /dev/null +++ b/docs/buffers.md @@ -0,0 +1,63 @@ +Memory allocation and ownership +------------------------------- + +Any library needs to _take_ data from users, and then _return_ data to +users. With some types this is simple - integer parameters and return +types are trivial. But with more complex data types, things are more +complicated. Even something seemingly simple, like a C string, requires +discipline: we cannot simple return an allocated hunk of memory for +callers to `free`, since some systems have multiple allocators and users +cannot necessarily reason about the allocator used and which corresponding +deallocation function to call to free the memory. + +## Objects + +Most types in libgit2 are "opaque" types, which we treat as "objects" (even +though C is "not an object oriented language"). You may create an object - +for example, with `git_odb_new`, or libgit2 may return you an object as an +"out" parameter - for example, with `git_repository_open`. With any of +these objects, you should call their corresponding `_free` function (for +example, `git_odb_free` or `git_repository_free`) when you are done using +them. + +## Structures + +libgit2 will often take _input_ as structures (for example, options +structures like `git_merge_options`). Rarely, libgit2 will return data in +a structure. This is typically used for simpler data types, like `git_buf` +and `git_strarray`. Users should allocate the structure themselves (either +on the stack or the heap) and pass a pointer to it. Since libgit2 does not +allocate the structure itself, only the data inside of it, the deallocation +functions are suffixed with `_dispose` instead of `_free`, since they only +free the data _inside_ the structure. + +## Strings or continuous memory buffers (`git_buf`) + +libgit2 typically _takes_ NUL-terminated strings ("C strings") with a +`const char *`, and typically _takes_ raw data with a `const char *` and a +corresponding `size_t` for its size. libgit2 typically _returns_ strings +or raw data in a `git_buf` structure. The given data buffer will always be +NUL terminated (even if it contains binary data) and the `size` member will +always contain the size (in bytes) of the contents of the pointer (excluding +the NUL terminator). + +In other words, if a `git_buf` contains the string `foo` then the memory +buffer will be { `f`, `o`, `o`, `\0` } and the size will be `3`. + +Callers _must_ initialize the buffer with `GIT_BUF_INIT` (or by setting +all the members to `0`) when it is created, before passing a pointer +to the buffer to libgit2 for the first time. + +Subsequent calls to libgit2 APIs that take a buffer can re-use a +buffer that was previously used. The buffer will be cleared and grown +to accommodate the new contents (if necessary). The new data will +written into the buffer, overwriting the previous contents. This +allows callers to reduce the number of allocations performed by the +library. + +Callers must call `git_buf_dispose` when they have finished. + +Note that the deprecated `git_diff_format_email` API does not follow +this behavior; subsequent calls will concatenate data to the buffer +instead of rewriting it. Users should move to the new `git_email` +APIs that follow the `git_buf` standards. diff --git a/docs/changelog.md b/docs/changelog.md new file mode 100644 index 00000000000..591e515113a --- /dev/null +++ b/docs/changelog.md @@ -0,0 +1,3224 @@ +v1.9.0 +------ + +This is release v1.9.0, "Schwibbogen". As usual, it contains numerous +bug fixes, compatibility improvements, and new features. + +This is expected to be the final release in the libgit2 v1.x lineage. +libgit2 v2.0 is expected to be the next version, with support for +SHA256 moving to "supported" status (out of "experimental" status). +This means that v2.0 will have API and ABI changes to support SHA256. + +## Major changes + +* **Documentation improvements** + We've launched a new website for our API reference docs at + https://libgit2.org/docs/reference/main/. To support this, + we've updated the documentation to ensure that all APIs are + well-documented, and added docurium-style specifiers to indicate + more depth about the API surface. + + We now also publish a JSON blob with the API structure and the + documentation that may be helpful for binding authors. + +* **TLS cipher updates** + libgit2 has updated our TLS cipher selection to match the + "compatibility" cipher suite settings as [documented by + Mozilla](https://wiki.mozilla.org/Security/Server_Side_TLS). + +* **Blame improvements** + The blame API now contains committer information and commit summaries + for blame hunks, and the ability to get information about the line of + text that was modified. In addition, a CLI blame command has been added + so that the blame functionality can be benchmarked by our benchmark + suite. + +* **More CLI commands** + libgit2 has added `blame` and `init` commands, which have allowed for + [further benchmarking](https://benchmarks.libgit2.org/) and several API + improvements and git compatibility updates. + +* **Warning when configuring without SHA1DC** + Users are encouraged to use SHA1DC, which is _git's hash_; users + should not use SHA1 in the general case. Users will now be warned + if they try to configure cmake with a SHA1 backend (`-DUSE_SHA1=...`). + +## Breaking changes + +There are several ABI-breaking changes that integrators, particularly +maintainers of bindings or FFI users, may want to be aware of. + +* **Blame hunk structure updates** (ABI breaking change) + There are numerous additions to the `git_blame_hunk` structure to + accommodate more information about the blame process. + +* **Checkout strategy updates** (ABI breaking change) + The values for `GIT_CHECKOUT_SAFE` and `GIT_CHECKOUT_NONE` have been + updated. `GIT_CHECKOUT_SAFE` is now `0`; this was implicitly the + default value (with the options constructors setting that as the + checkout strategy). It is now the default if the checkout strategy + is set to `0`. This allows for an overall code simplification in the + library. + +* **Configuration entry member removal** (ABI breaking change) + The `git_config_entry` structure no longer contains a `free` member; + this was an oversight as end-users should not try to free that + structure. + +* **Configuration backend function changes** (ABI breaking change) + `git_config_backend`s should now return `git_config_backend_entry` + objects instead of `git_config_entry` objects. This allows backends + to provide a mechanism to nicely free the configuration entries that + they provide. + +* **`update_refs` callback for remotes** (ABI breaking change) + The `update_refs` callback was added to the `git_remote_callbacks` + structure to provide additional information about updated refs; + in particular, the `git_refspec` is included for more information + about the remote ref. The `update_refs` callback will be + preferred over the now deprecated `update_tips` callback. + +## What's Changed + +### New features + +* The `git_signature_default_from_env` API will now produce a pair + of `git_signature`s representing the author, and the committer, + taking the `GIT_AUTHOR_NAME` and `GIT_COMMITTER_NAME` environment + variables into account. Added by @u-quark in + https://github.com/libgit2/libgit2/pull/6706 + +* packbuilder can now be interrupted from a callback. Added @roberth + in https://github.com/libgit2/libgit2/pull/6874 + +* libgit2 now claims to honor the `preciousObject` repository extension. + This extension indicates that the client will never delete objects + (in other words, will not garbage collect). libgit2 has no functionality + to remove objects, so it implicitly obeys this in all cases. Added + by @ethomson in https://github.com/libgit2/libgit2/pull/6886 + +* Push status will be reported even when a push fails. This is useful + to give information from the server about possible updates, even when + the overall status failed. Added by @yerseg in + https://github.com/libgit2/libgit2/pull/6876 + +* You can now generate a thin pack from a mempack instance using + `git_mempack_write_thin_pack`. Added by @roberth in + https://github.com/libgit2/libgit2/pull/6875 + +* The new `LIBGIT2_VERSION_CHECK` macro will indicate whether the version + of libgit2 being compiled against is at least the version specified. + For example: `#if LIBGIT2_VERSION_CHECK(1, 6, 3)` is true for libgit2 + version 1.6.3 or newer. In addition, the new `LIBGIT2_VERSION_NUMBER` + macro will return an integer version representing the libgit2 version + number. For example, for version 1.6.3, `LIBGIT2_VERSION_NUMBER` will + evaluate to `010603`. Added by @HamedMasafi in + https://github.com/libgit2/libgit2/pull/6882 + +* Custom X509 certificates can be added to OpenSSL's certificate store + using the `GIT_OPT_ADD_SSL_X509_CERT` option. Added by @yerseg in + https://github.com/libgit2/libgit2/pull/6877 + +* The libgit2 compatibility CLI now has a `git blame` command. Added by + @ethomson in https://github.com/libgit2/libgit2/pull/6907 + +* Remote callbacks now provide an `update_refs` callback so that users + can now get the `refspec` of the updated reference during push. This + gives more complete information about the remote reference that was + updated. Added by @ethomson in + https://github.com/libgit2/libgit2/pull/6559 + +* An optional FIPS-compliant mode for hashing is now available; you + can set `-DUSE_SHA256=OpenSSL-FIPS` to enable it. Added by @marcind-dot + in https://github.com/libgit2/libgit2/pull/6906 + +* The git-compatible CLI now supports the `git init` command, which has + been useful in identifying API improvements and incompatibilities with + git. Added by @ethomson in https://github.com/libgit2/libgit2/pull/6984 + +* Consumers can now query more information about how libgit2 was + compiled, and query the "backends" that libgit2 uses. Added by + @ethomson in https://github.com/libgit2/libgit2/pull/6971 + +### Bug fixes + +* Fix constness issue introduced in #6716 by @ethomson in + https://github.com/libgit2/libgit2/pull/6829 +* odb: conditional `git_hash_ctx_cleanup` in `git_odb_stream` by + @gensmusic in https://github.com/libgit2/libgit2/pull/6836 +* Fix shallow root maintenance during fetch by @kcsaul in + https://github.com/libgit2/libgit2/pull/6846 +* Headers cleanup by @anatol in + https://github.com/libgit2/libgit2/pull/6842 +* http: Initialize `on_status` when using the http-parser backend by + @civodul in https://github.com/libgit2/libgit2/pull/6870 +* Leak in `truncate_racily_clean` in index.c by @lstoppa in + https://github.com/libgit2/libgit2/pull/6884 +* ssh: Omit port option from ssh command unless specified in remote url + by @jayong93 in https://github.com/libgit2/libgit2/pull/6845 +* diff: print the file header on `GIT_DIFF_FORMAT_PATCH_HEADER` by + @carlosmn in https://github.com/libgit2/libgit2/pull/6888 +* Add more robust reporting to SecureTransport errors on macos by + @vcfxb in https://github.com/libgit2/libgit2/pull/6848 +* transport: do not filter tags based on ref dir in local by @rindeal + in https://github.com/libgit2/libgit2/pull/6881 +* push: handle tags to blobs by @ethomson in + https://github.com/libgit2/libgit2/pull/6898 +* Fixes for OpenSSL dynamic by @ethomson in + https://github.com/libgit2/libgit2/pull/6901 +* realpath: unbreak build on OpenBSD by @ajacoutot in + https://github.com/libgit2/libgit2/pull/6932 +* util/win32: Continue if access is denied when deleting a folder by + @lrm29 in https://github.com/libgit2/libgit2/pull/6929 +* object: `git_object_short_id` fails with core.abbrev string values by + @lrm29 in https://github.com/libgit2/libgit2/pull/6944 +* Clear data after negotiation by @lrm29 in + https://github.com/libgit2/libgit2/pull/6947 +* smart: ignore shallow/unshallow packets during ACK processing by + @kempniu in https://github.com/libgit2/libgit2/pull/6973 + +### Security fixes + +* ssh: Include rsa-sha2-256 and rsa-sha2-512 in the list of hostkey types + by @lrm29 in https://github.com/libgit2/libgit2/pull/6938 +* TLS: v1.2 and updated cipher list by @ethomson in + https://github.com/libgit2/libgit2/pull/6960 + +### Code cleanups + +* checkout: make safe checkout the default by @ethomson in + https://github.com/libgit2/libgit2/pull/6037 +* url: track whether url explicitly specified a port by @ethomson in + https://github.com/libgit2/libgit2/pull/6851 +* config: remove `free` ptr from `git_config_entry` by @ethomson in + https://github.com/libgit2/libgit2/pull/6804 +* Add SecCopyErrorMessageString for iOS and update README for iOS by + @Kyle-Ye in https://github.com/libgit2/libgit2/pull/6862 +* vector: free is now dispose by @ethomson in + https://github.com/libgit2/libgit2/pull/6896 +* hashmap: a libgit2-idiomatic khash by @ethomson in + https://github.com/libgit2/libgit2/pull/6897 +* hashmap: asserts by @ethomson in + https://github.com/libgit2/libgit2/pull/6902 +* hashmap: further asserts by @ethomson in + https://github.com/libgit2/libgit2/pull/6904 +* Make `GIT_WIN32` an internal declaration by @ethomson in + https://github.com/libgit2/libgit2/pull/6940 +* pathspec: additional pathspec wildcard tests by @ethomson in + https://github.com/libgit2/libgit2/pull/6959 +* repo: don't require option when `template_path` is specified by @ethomson + in https://github.com/libgit2/libgit2/pull/6983 +* options: update X509 cert constant by @ethomson in + https://github.com/libgit2/libgit2/pull/6974 +* remote: Handle fetching negative refspecs by @ryan-ph in + https://github.com/libgit2/libgit2/pull/6962 +* Restore tls v1.0 support temporarily by @ethomson in + https://github.com/libgit2/libgit2/pull/6964 +* SHA256 improvements by @ethomson in + https://github.com/libgit2/libgit2/pull/6965 + +### Benchmarks + +* Add benchmarks for blame by @ethomson in + https://github.com/libgit2/libgit2/pull/6920 + +### Build and CI improvements + +* README: add experimental builds to ci table by @ethomson in + https://github.com/libgit2/libgit2/pull/6816 +* Update stransport.c by @ethomson in + https://github.com/libgit2/libgit2/pull/6891 +* stransport: initialize for iOS by @ethomson in + https://github.com/libgit2/libgit2/pull/6893 +* CI updates by @ethomson in + https://github.com/libgit2/libgit2/pull/6895 +* Configurable C standard by @ethomson in + https://github.com/libgit2/libgit2/pull/6911 +* cmake: update python locator by @ethomson in + https://github.com/libgit2/libgit2/pull/6915 +* ci: don't run Windows SHA256 gitdaemon tests by @ethomson in + https://github.com/libgit2/libgit2/pull/6916 +* cmake-standard c standards by @ethomson in + https://github.com/libgit2/libgit2/pull/6914 +* ci: don't run Windows SHA256 gitdaemon tests by @ethomson in + https://github.com/libgit2/libgit2/pull/6919 +* Improve dependency selection in CMake by @ethomson in + https://github.com/libgit2/libgit2/pull/6924 +* ci: port latest fixes to nightlies by @ethomson in + https://github.com/libgit2/libgit2/pull/6926 +* cmake: warn for not using sha1dc by @ethomson in + https://github.com/libgit2/libgit2/pull/6986 + +### Documentation improvements + +* Fix docs for `git_odb_stream_read` return value. by @ehuss in + https://github.com/libgit2/libgit2/pull/6837 +* docs: Add instructions to build examples by @thymusvulgaris in + https://github.com/libgit2/libgit2/pull/6839 +* Fix contradictory phrase in SECURITY.md by @Kyle-Ye in + https://github.com/libgit2/libgit2/pull/6859 +* Update README.md by @Kyle-Ye in + https://github.com/libgit2/libgit2/pull/6860 +* README updates by @ethomson in + https://github.com/libgit2/libgit2/pull/6908 +* typo: s/size on bytes/size in bytes/ by @John-Colvin in + https://github.com/libgit2/libgit2/pull/6909 +* readme: add OpenSSF best practices badge by @ethomson in + https://github.com/libgit2/libgit2/pull/6925 +* Update documentation of `merge_base_many` by @Caleb-T-Owens in + https://github.com/libgit2/libgit2/pull/6927 +* Include documentation generator by @ethomson in + https://github.com/libgit2/libgit2/pull/6945 +* Update documentation generation workflow by @ethomson in + https://github.com/libgit2/libgit2/pull/6948 +* Improve documentation and validate during CI by @ethomson in + https://github.com/libgit2/libgit2/pull/6949 +* Add search functionality to our docs generator by @ethomson in + https://github.com/libgit2/libgit2/pull/6953 +* Documentation: don't resort versions by @ethomson in + https://github.com/libgit2/libgit2/pull/6954 +* Documentation: update `refdb_backend` docs by @ethomson in + https://github.com/libgit2/libgit2/pull/6955 +* Documentation: clean up old documentation by @ethomson in + https://github.com/libgit2/libgit2/pull/6957 +* docs: remind people about `git_libgit2_init` by @ethomson in + https://github.com/libgit2/libgit2/pull/6958 +* Update changelog with v1.8.4 content by @ethomson in + https://github.com/libgit2/libgit2/pull/6961 + +### Git compatibility fixes + +* Limit `.gitattributes` and `.gitignore` files to 100 MiB by @csware in + https://github.com/libgit2/libgit2/pull/6834 +* refs: Handle normalizing negative refspecs by @ryan-ph in + https://github.com/libgit2/libgit2/pull/6951 +* repo: put a newline on the .git link file by @ethomson in + https://github.com/libgit2/libgit2/pull/6981 + +### Dependency updates + +* zlib: update bundled zlib to v1.3.1 by @ethomson in + https://github.com/libgit2/libgit2/pull/6905 +* Update ntlmclient dependency by @ethomson in + https://github.com/libgit2/libgit2/pull/6912 + +### Other changes + +* Create FUNDING.json by @BenJam in + https://github.com/libgit2/libgit2/pull/6853 + +## New Contributors + +* @gensmusic made their first contribution in + https://github.com/libgit2/libgit2/pull/6836 +* @u-quark made their first contribution in + https://github.com/libgit2/libgit2/pull/6706 +* @thymusvulgaris made their first contribution in + https://github.com/libgit2/libgit2/pull/6839 +* @anatol made their first contribution in + https://github.com/libgit2/libgit2/pull/6842 +* @BenJam made their first contribution in + https://github.com/libgit2/libgit2/pull/6853 +* @Kyle-Ye made their first contribution in + https://github.com/libgit2/libgit2/pull/6859 +* @civodul made their first contribution in + https://github.com/libgit2/libgit2/pull/6870 +* @lstoppa made their first contribution in + https://github.com/libgit2/libgit2/pull/6884 +* @jayong93 made their first contribution in + https://github.com/libgit2/libgit2/pull/6845 +* @roberth made their first contribution in + https://github.com/libgit2/libgit2/pull/6874 +* @vcfxb made their first contribution in + https://github.com/libgit2/libgit2/pull/6848 +* @yerseg made their first contribution in + https://github.com/libgit2/libgit2/pull/6876 +* @rindeal made their first contribution in + https://github.com/libgit2/libgit2/pull/6881 +* @HamedMasafi made their first contribution in + https://github.com/libgit2/libgit2/pull/6882 +* @John-Colvin made their first contribution in + https://github.com/libgit2/libgit2/pull/6909 +* @marcind-dot made their first contribution in + https://github.com/libgit2/libgit2/pull/6906 +* @ajacoutot made their first contribution in + https://github.com/libgit2/libgit2/pull/6932 +* @Caleb-T-Owens made their first contribution in + https://github.com/libgit2/libgit2/pull/6927 +* @ryan-ph made their first contribution in + https://github.com/libgit2/libgit2/pull/6951 +* @bmarques1995 made their first contribution in + https://github.com/libgit2/libgit2/pull/6840 + +**Full Changelog**: https://github.com/libgit2/libgit2/compare/v1.8.4...v1.9.0 + +v1.8.4 +------ + +We erroneously shipped v1.8.3 without actually including the change +in v1.8.2. This release re-re-introduces the pre-v1.8.0 `commit` +constness behavior. + +## What's Changed + +### Bug fixes + +* Fix constness issue introduced in #6716 by @ethomson in https://github.com/libgit2/libgit2/pull/6829 + +**Full Changelog**: https://github.com/libgit2/libgit2/compare/v1.8.3...v1.8.4 + +v1.8.3 +------ + +This release fixes a bug introduced in v1.8.1 for users of the legacy +[Node.js http-parser](https://github.com/nodejs/http-parser) +dependency. + +## What's Changed + +### Bug fixes + +* http: Backport on_status initialize fix for http-parser by @ethomson in https://github.com/libgit2/libgit2/pull/6931 + +v1.8.2 +------ + +This release reverts a const-correctness change introduced in +v1.8.0 for the `git_commit_create` functions. We now retain the +const-behavior for the `commits` arguments from prior to v1.8.0. + +This change was meant to resolve compatibility issues with bindings +and downstream users. + +## What's Changed + +### New features + +* Introduce a stricter debugging allocator for testing by @ethomson in https://github.com/libgit2/libgit2/pull/6811 + +### Bug fixes + +* Fix constness issue introduced in #6716 by @ethomson in https://github.com/libgit2/libgit2/pull/6829 + +### Build and CI improvements + +* README: add experimental builds to ci table by @ethomson in https://github.com/libgit2/libgit2/pull/6816 + +**Full Changelog**: https://github.com/libgit2/libgit2/compare/v1.8.1...v1.8.2 + +v1.8.1 +------ + +This release primarily includes straightforward bugfixes, as well as +new functionality to have more control over the HTTP User-Agent header. +However, there is an API change from v1.8 that was required for +improved compatibility. + +In v1.8, libgit2 introduced the `report_unchanged ` member in the +`git_fetch_options` structure. We mistakenly introduced this as a +bitfield, which is not suitable for our public API. To correct this +mistake, we have _removed_ the `report_unchanged ` member. To support +the report unchanged tips option, users can set the `update_fetchhead` +member to include the `GIT_REMOTE_UPDATE_REPORT_UNCHANGED` value. + +The libgit2 projects regrets the API change, but this was required to +support cross-platform compatibility. + +## What's Changed + +### New features + +* Allow more control over the user-agent by @ethomson in + https://github.com/libgit2/libgit2/pull/6788 + +### Bug fixes + +* commit: Fix git_commit_create_from_stage without author and + committer by @florianpircher in + https://github.com/libgit2/libgit2/pull/6781 +* process.c: fix environ for macOS by @barracuda156 in + https://github.com/libgit2/libgit2/pull/6792 +* Bounds check for pack index read by @ConradIrwin in + https://github.com/libgit2/libgit2/pull/6796 +* transport: provide a useful error message during cancellation + by @ethomson in https://github.com/libgit2/libgit2/pull/6802 +* transport: support sha256 oids by @ethomson in + https://github.com/libgit2/libgit2/pull/6803 +* Revparse: Correctly accept ref with '@' at the end by @csware in + https://github.com/libgit2/libgit2/pull/6809 +* remote: drop bitfields in git_remote_fetch_options by @ethomson in + https://github.com/libgit2/libgit2/pull/6806 +* examples: fix memory leak in for-each-ref.c by @qaqland in + https://github.com/libgit2/libgit2/pull/6808 +* xdiff: use proper free function by @ethomson in + https://github.com/libgit2/libgit2/pull/6810 +* rand: avoid uninitialized loadavg warnings by @ethomson in + https://github.com/libgit2/libgit2/pull/6812 +* cli: include alloca on illumos / solaris / sunos by @ethomson in + https://github.com/libgit2/libgit2/pull/6813 +* Update git_array allocator to obey strict aliasing rules + by @ethomson in https://github.com/libgit2/libgit2/pull/6814 +* tree: avoid mixed signedness comparison by @ethomson in + https://github.com/libgit2/libgit2/pull/6815 + +### Build and CI improvements + +* ci: update nightly workflows by @ethomson in + https://github.com/libgit2/libgit2/pull/6773 +* ci: give all nightly builds a unique id by @ethomson in + https://github.com/libgit2/libgit2/pull/6782 +* cmake: remove workaround that isn't compatible with Windows on + ARM by @hackhaslam in https://github.com/libgit2/libgit2/pull/6794 + +### Documentation improvements + +* Docs meta-updates by @ethomson in + https://github.com/libgit2/libgit2/pull/6787 + +### Dependency updates + +* Enable llhttp for HTTP parsing by @sgallagher in + https://github.com/libgit2/libgit2/pull/6713 + +## New Contributors + +* @florianpircher made their first contribution in + https://github.com/libgit2/libgit2/pull/6781 +* @barracuda156 made their first contribution in + https://github.com/libgit2/libgit2/pull/6792 +* @sgallagher made their first contribution in + https://github.com/libgit2/libgit2/pull/6713 +* @ConradIrwin made their first contribution in + https://github.com/libgit2/libgit2/pull/6796 +* @qaqland made their first contribution in + https://github.com/libgit2/libgit2/pull/6808 + +**Full Changelog**: https://github.com/libgit2/libgit2/compare/v1.8.0...v1.8.1 + +v1.8 +---- + +This is release v1.8.0, "Das Fliegende Klassenzimmer". This release +includes optional, experimental support for invoking OpenSSH to fetch +and push, an easier mechanism to perform the default behavior of +`git commit`, and has many improvements for worktrees. This release +also includes many other new features and bugfixes. + +## Major changes + +* **Executable SSH (OpenSSH) support** + libgit2 can now invoke the command-line OpenSSH to fetch from and push + to remotes over SSH. This support takes the place of libssh2 support. + To use it, configure libgit2 with `cmake -DUSE_SSH=exec`, and please + report any problems that you discover. By @ethomson in + https://github.com/libgit2/libgit2/pull/6617 + +* **Simplified commit creation** + The `git_commit_create_from_stage` API was introduced to allow users to + better emulate the behavior of `git commit` without needing to provide + unnecessary information. The current state of the index is committed to + the current branch. By @ethomson in + https://github.com/libgit2/libgit2/pull/6716 + +* **Worktree improvements** + A number of worktree improvements have been made for better + compatibility with core git. First, libgit2 now understands per-worktree + references, thanks to @csware in + https://github.com/libgit2/libgit2/pull/6387. Worktree-specific + configuration is now supported, thanks to @vermiculus in + https://github.com/libgit2/libgit2/pull/6202. And improved compatibility + with `git worktree add` is now supported, thanks to @herrerog in + https://github.com/libgit2/libgit2/pull/5319. + +## Breaking changes + +* **Adding `WORKTREE` configuration level** (ABI breaking change) + To support worktree configurations at the appropriate level (higher + priority than local configuration, but lower priority than app-specific + configuration), the `GIT_CONFIG_LEVEL_WORKTREE` level was introduced at + priority 6. `GIT_CONFIG_LEVEL_APP` now begins at priority 7. + +* **Changes to `git_config_entry`** (ABI breaking change) + The `git_config_entry` structure now contains information about the + `backend_type` and `origin_path`. The unused `payload` value has been + removed. + +* **`git_push_options` includes remote push options** (ABI breaking change) + The `git_push_options` structure now contains a value for remote push + options. + +## Other changes + +### New features + +* config: provide an "origin" for config entries by @ethomson in + https://github.com/libgit2/libgit2/pull/6615 +* cli: add a `git config` command by @ethomson in + https://github.com/libgit2/libgit2/pull/6616 +* Add OpenSSH support by @ethomson in + https://github.com/libgit2/libgit2/pull/6617 +* remote: optionally report unchanged tips by @ethomson in + https://github.com/libgit2/libgit2/pull/6645 +* Support setting oid type for in-memory repositories by @kcsaul in + https://github.com/libgit2/libgit2/pull/6671 +* cli: add `index-pack` command by @ethomson in + https://github.com/libgit2/libgit2/pull/6681 +* Add `git_repository_commit_parents` to identify the parents of the next + commit given the repository state by @ethomson in + https://github.com/libgit2/libgit2/pull/6707 +* commit: introduce `git_commit_create_from_stage` by @ethomson in + https://github.com/libgit2/libgit2/pull/6716 +* set SSH timeout by @vafada in + https://github.com/libgit2/libgit2/pull/6721 +* Implement push options on push by @russell in + https://github.com/libgit2/libgit2/pull/6439 +* Support index.skipHash true config by @parnic in + https://github.com/libgit2/libgit2/pull/6738 +* worktree: mimic 'git worktree add' behavior. by @herrerog in + https://github.com/libgit2/libgit2/pull/5319 +* Support the extension for worktree-specific config by @vermiculus in + https://github.com/libgit2/libgit2/pull/6202 +* Separate config reader and writer backend priorities (for worktree + configs) by @ethomson in https://github.com/libgit2/libgit2/pull/6756 +* fetch: enable deepening/shortening shallow clones by @kempniu in + https://github.com/libgit2/libgit2/pull/6662 + +### Bug fixes + +* repository: make cleanup safe for re-use with grafts by @carlosmn in + https://github.com/libgit2/libgit2/pull/6600 +* fix: Add missing include for `oidarray`. by @dvzrv in + https://github.com/libgit2/libgit2/pull/6608 +* ssh: fix `known_hosts` leak in `_git_ssh_setup_conn` by @steven9724 in + https://github.com/libgit2/libgit2/pull/6599 +* proxy: Return an error for invalid proxy URLs instead of crashing by + @lrm29 in https://github.com/libgit2/libgit2/pull/6597 +* errors: refactoring - never return `NULL` in `git_error_last()` by + @ethomson in https://github.com/libgit2/libgit2/pull/6625 +* Reject potential option injections over ssh by @carlosmn in + https://github.com/libgit2/libgit2/pull/6636 +* remote: fix memory leak in `git_remote_download()` by @7Ji in + https://github.com/libgit2/libgit2/pull/6651 +* git2: Fix crash when called w/o parameters by @csware in + https://github.com/libgit2/libgit2/pull/6673 +* Avoid macro redefinition of `ENABLE_INTSAFE_SIGNED_FUNCTIONS` by @csware + in https://github.com/libgit2/libgit2/pull/6666 +* util: suppress some uninitialized variable warnings by @boretrk in + https://github.com/libgit2/libgit2/pull/6659 +* push: set generic error in `push_negotiation` cb by @ethomson in + https://github.com/libgit2/libgit2/pull/6675 +* process: test `/usr/bin/false` on BSDs by @ethomson in + https://github.com/libgit2/libgit2/pull/6677 +* clone: don't mix up "http://url" with "http:/url" when figuring out if we + should do a local clone by @boretrk in + https://github.com/libgit2/libgit2/pull/6361 +* Several compatibility fixes by @ethomson in + https://github.com/libgit2/libgit2/pull/6678 +* Git blame buffer gives the wrong result in many cases where there are + by @thosey in https://github.com/libgit2/libgit2/pull/6572 +* Fix 'path cannot exist in repository' during diff for in-memory repository + by @kcsaul in https://github.com/libgit2/libgit2/pull/6683 +* process: don't try to close the status by @ethomson in + https://github.com/libgit2/libgit2/pull/6693 +* Minor bug fixes by @ethomson in + https://github.com/libgit2/libgit2/pull/6695 +* Bypass shallow clone support for in-memory repositories by @kcsaul in + https://github.com/libgit2/libgit2/pull/6684 +* examples: use `unsigned` int for bitfields by @ethomson in + https://github.com/libgit2/libgit2/pull/6699 +* Fix some bugs caught by UBscan by @ethomson in + https://github.com/libgit2/libgit2/pull/6700 +* `git_diff_find_similar` doesn't always remove unmodified deltas by @yori + in https://github.com/libgit2/libgit2/pull/6642 +* httpclient: clear `client->parser.data` after use by @ethomson in + https://github.com/libgit2/libgit2/pull/6705 +* Do not normalize `safe.directory` paths by @csware in + https://github.com/libgit2/libgit2/pull/6668 +* clone: don't swallow error in `should_checkout` by @ethomson in + https://github.com/libgit2/libgit2/pull/6727 +* Correct index add directory/file conflict detection by @ethomson in + https://github.com/libgit2/libgit2/pull/6729 +* Correct `git_revparse_single` and add revparse fuzzing by @ethomson in + https://github.com/libgit2/libgit2/pull/6730 +* config: properly delete or rename section containing multivars by + @samueltardieu in https://github.com/libgit2/libgit2/pull/6723 +* revparse: ensure bare '@' is truly bare by @ethomson in + https://github.com/libgit2/libgit2/pull/6742 +* repo: ensure we can initialize win32 paths by @ethomson in + https://github.com/libgit2/libgit2/pull/6743 +* Swap `GIT_DIFF_LINE_(ADD|DEL)_EOFNL` to match other Diffs by @xphoniex in + https://github.com/libgit2/libgit2/pull/6240 +* diff: fix test for SHA256 support in `diff_from_buffer` by @ethomson in + https://github.com/libgit2/libgit2/pull/6745 +* http: support empty http.proxy config setting by @ethomson in + https://github.com/libgit2/libgit2/pull/6744 +* More `safe.directory` improvements by @ethomson in + https://github.com/libgit2/libgit2/pull/6739 +* Ensure that completely ignored diff is empty by @ethomson in + https://github.com/libgit2/libgit2/pull/5893 +* Fix broken regexp that matches submodule names containing ".path" by + @csware in https://github.com/libgit2/libgit2/pull/6749 +* Fix memory leaks by @csware in + https://github.com/libgit2/libgit2/pull/6748 +* Make `refdb_fs` (hopefully) fully aware of per worktree refs by @csware in + https://github.com/libgit2/libgit2/pull/6387 +* fix log example by @albfan in https://github.com/libgit2/libgit2/pull/6359 +* fetch: fail on depth for local transport by @ethomson in + https://github.com/libgit2/libgit2/pull/6757 +* Fix message trailer parsing by @ethomson in + https://github.com/libgit2/libgit2/pull/6761 +* config: correct fetching the `HIGHEST_LEVEL` config by @ethomson in + https://github.com/libgit2/libgit2/pull/6766 +* Avoid some API breaking changes in v1.8 by @ethomson in + https://github.com/libgit2/libgit2/pull/6768 + +### Build and CI improvements + +* meta: update version numbers to v1.8 by @ethomson in + https://github.com/libgit2/libgit2/pull/6596 +* Revert "CMake: Search for ssh2 instead of libssh2." by @ethomson in + https://github.com/libgit2/libgit2/pull/6619 +* cmake: fix openssl build on win32 by @lazka in + https://github.com/libgit2/libgit2/pull/6626 +* ci: retry flaky online tests by @ethomson in + https://github.com/libgit2/libgit2/pull/6628 +* ci: update to macOS 12 by @ethomson in + https://github.com/libgit2/libgit2/pull/6629 +* Use `#!/bin/bash` for script with bash-specific commands by @roehling in + https://github.com/libgit2/libgit2/pull/6581 +* ci: overwrite nonsense in `/usr/local` during macOS setup by @ethomson in + https://github.com/libgit2/libgit2/pull/6664 +* release: add a compatibility label by @ethomson in + https://github.com/libgit2/libgit2/pull/6676 +* actions: set permissions by @ethomson in + https://github.com/libgit2/libgit2/pull/6680 +* cmake: rename FindIconv to avoid collision with cmake by @ethomson in + https://github.com/libgit2/libgit2/pull/6682 +* ci: allow workflows to read and write packages by @ethomson in + https://github.com/libgit2/libgit2/pull/6687 +* ci: allow workflows to push changes by @ethomson in + https://github.com/libgit2/libgit2/pull/6688 +* tests: remove test for strcasecmp by @boretrk in + https://github.com/libgit2/libgit2/pull/6691 +* CI fixes by @ethomson in + https://github.com/libgit2/libgit2/pull/6694 +* ci: improvements to prepare for Cygwin support by @ethomson in + https://github.com/libgit2/libgit2/pull/6696 +* Yet more CI improvements by @ethomson in + https://github.com/libgit2/libgit2/pull/6697 +* Fix nightly builds by @ethomson in + https://github.com/libgit2/libgit2/pull/6709 +* Benchmarks: add a site to view results by @ethomson in + https://github.com/libgit2/libgit2/pull/6715 +* `GIT_RAND_GETENTROPY`: do not include `sys/random.h` by @semarie in + https://github.com/libgit2/libgit2/pull/6736 +* add dl to `LIBGIT2_SYSTEM_LIBS` by @christopherfujino in + https://github.com/libgit2/libgit2/pull/6631 +* meta: add dependency tag to release.yml by @ethomson in + https://github.com/libgit2/libgit2/pull/6740 +* CI: fix our nightlies by @ethomson in + https://github.com/libgit2/libgit2/pull/6751 +* trace: Re-enable tests as tracing is now enabled by default by @lrm29 in + https://github.com/libgit2/libgit2/pull/6752 +* tests: don't free an unininitialized repo by @ethomson in + https://github.com/libgit2/libgit2/pull/6763 +* ci: reduce ASLR randomization for TSAN by @ethomson in + https://github.com/libgit2/libgit2/pull/6764 +* packbuilder: adjust nondeterministic tests by @ethomson in + https://github.com/libgit2/libgit2/pull/6762 +* Allow libgit2 to be compiled with mbedtls3. by @adamharrison in + https://github.com/libgit2/libgit2/pull/6759 +* build: update to latest actions versions by @ethomson in + https://github.com/libgit2/libgit2/pull/6765 +* ctype: cast characters to unsigned when classifying characters by + @boretrk in https://github.com/libgit2/libgit2/pull/6679 and + @ethomson in https://github.com/libgit2/libgit2/pull/6770 +* valgrind: suppress OpenSSL warnings by @ethomson in https://github.com/libgit2/libgit2/pull/6769 + +### Documentation improvements + +* README.md: Fix link to conan packages by @lrm29 in + https://github.com/libgit2/libgit2/pull/6621 +* README: replace gmaster with GitButler by @ethomson in + https://github.com/libgit2/libgit2/pull/6692 +* blame example: Fix support for line range in CLI by @wetneb in + https://github.com/libgit2/libgit2/pull/6638 +* Support authentication in push example by @pluehne in + https://github.com/libgit2/libgit2/pull/5904 +* docs: fix mistake in attr.h by @DavHau in + https://github.com/libgit2/libgit2/pull/6714 +* Fix broken links by @csware in + https://github.com/libgit2/libgit2/pull/6747 + +### Platform compatibility fixes + +* stransport: macOS: replace `errSSLNetworkTimeout`, with hard-coded + value by @mascguy in https://github.com/libgit2/libgit2/pull/6610 + +### Git compatibility fixes + +* Do not trim dots from usernames by @georgthegreat in + https://github.com/libgit2/libgit2/pull/6657 +* merge: fix incorrect rename detection for empty files by @herrerog in + https://github.com/libgit2/libgit2/pull/6717 + +### Dependency updates + +* zlib: upgrade bundled zlib to v1.3 by @ethomson in + https://github.com/libgit2/libgit2/pull/6698 +* ntlmclient: update to latest upstream ntlmclient by @ethomson in + https://github.com/libgit2/libgit2/pull/6704 + +## New Contributors + +* @dvzrv made their first contribution in + https://github.com/libgit2/libgit2/pull/6608 +* @mascguy made their first contribution in + https://github.com/libgit2/libgit2/pull/6610 +* @steven9724 made their first contribution in + https://github.com/libgit2/libgit2/pull/6599 +* @lazka made their first contribution in + https://github.com/libgit2/libgit2/pull/6626 +* @roehling made their first contribution in + https://github.com/libgit2/libgit2/pull/6581 +* @7Ji made their first contribution in + https://github.com/libgit2/libgit2/pull/6651 +* @kempniu made their first contribution in + https://github.com/libgit2/libgit2/pull/6662 +* @thosey made their first contribution in + https://github.com/libgit2/libgit2/pull/6572 +* @wetneb made their first contribution in + https://github.com/libgit2/libgit2/pull/6638 +* @yori made their first contribution in + https://github.com/libgit2/libgit2/pull/6642 +* @pluehne made their first contribution in + https://github.com/libgit2/libgit2/pull/5904 +* @DavHau made their first contribution in + https://github.com/libgit2/libgit2/pull/6714 +* @vafada made their first contribution in + https://github.com/libgit2/libgit2/pull/6721 +* @semarie made their first contribution in + https://github.com/libgit2/libgit2/pull/6736 +* @christopherfujino made their first contribution in + https://github.com/libgit2/libgit2/pull/6631 +* @parnic made their first contribution in + https://github.com/libgit2/libgit2/pull/6738 +* @samueltardieu made their first contribution in + https://github.com/libgit2/libgit2/pull/6723 +* @xphoniex made their first contribution in + https://github.com/libgit2/libgit2/pull/6240 +* @adamharrison made their first contribution in + https://github.com/libgit2/libgit2/pull/6759 + +**Full Changelog**: https://github.com/libgit2/libgit2/compare/v1.7.0...v1.8.0 + +v1.7 +---- + +This is release v1.7.0, "Kleine Raupe Nimmersatt". This release adds +shallow clone support, completes the experimental SHA256 support, +adds Schannel support for Windows, and includes many other new +features and bugfixes. + +## Major changes + +* **Shallow clone support** + libgit2 now supports shallow clone and shallow repositories, thanks + to a significant investment from many community members -- hundreds + of commits by many contributors. + + * Shallow (#6396) with some fixes from review by @ethomson in + https://github.com/libgit2/libgit2/pull/6557 + * Shallow Clone Support by @lya001 in + https://github.com/libgit2/libgit2/pull/6396 + * Shallow support v2 by @pks-t in + https://github.com/libgit2/libgit2/pull/5254 + +* **SHA256 support** + libgit2 should now support SHA256 repositories using the + `extensions.objectFormat` configuration option when the library is + built with `EXPERIMENTAL_SHA256=ON`. Users are encouraged to begin + testing their applications with this option and provide bug reports + and feedback. This _is_ a breaking API change; SHA256 support will + be enabled by default in libgit2 v2.0. + + * sha256: less hardcoded SHA1 types and lengths by @ethomson in + https://github.com/libgit2/libgit2/pull/6549 + * Support SHA256 in git_repository_wrap_odb by @ethomson in + https://github.com/libgit2/libgit2/pull/6556 + +* **Schannel and SSPI for Windows** + libgit2 now supports the Windows Schannel and SSPI APIs for HTTPS + support on Windows, when configured with `USE_HTTPS=Schannel`. + Setting this option will not use the existing WinHTTP support, but + will use libgit2's standard HTTP client stack with Windows TLS + primitives. Windows users are encouraged to begin testing their + applications with this option and provide bug reports and feedback. + This will be enabled by default in a future version of libgit2. + + * Introduce Schannel and SSPI for Windows by @ethomson in + https://github.com/libgit2/libgit2/pull/6533 + +## Breaking changes + +* **Simplify custom pluggable allocator** (System API / ABI breaking change) + The `git_allocator` structure (configurable by the + `GIT_OPT_SET_ALLOCATOR` option) now only contains `gmalloc`, + `grealloc` and `gfree` members. This simplifies both the work needed + by an implementer _and_ allows more flexibility and correctness in + libgit2 itself, especially during out-of-memory situations and + errors during bootstrapping. + + * tests: add allocator with limited number of bytes by @ethomson in + https://github.com/libgit2/libgit2/pull/6563 + +## Other changes + +### New features +* repo: honor environment variables for more scenarios by @ethomson in + https://github.com/libgit2/libgit2/pull/6544 +* Introduce timeouts on sockets by @ethomson in + https://github.com/libgit2/libgit2/pull/6535 + +### Performance improvements +* midx: do not try to look at every object in the index by @carlosmn in + https://github.com/libgit2/libgit2/pull/6585 +* Partial fix for #6532: insert-by-date order. by @arroz in + https://github.com/libgit2/libgit2/pull/6539 + +### Bug fixes +* repo: don't allow repeated extensions by @ethomson in + https://github.com/libgit2/libgit2/pull/6505 +* config: return `GIT_ENOTFOUND` for missing programdata by @ethomson in + https://github.com/libgit2/libgit2/pull/6547 +* Fix missing oid type for "fake" repositories by @oreiche in + https://github.com/libgit2/libgit2/pull/6554 +* Thread-local storage: handle failure cases by @ethomson in + https://github.com/libgit2/libgit2/pull/5722 +* midx: allow unknown chunk ids in multi-pack index files by @carlosmn in + https://github.com/libgit2/libgit2/pull/6583 +* pack: cast the number of objects to size_t by @carlosmn in + https://github.com/libgit2/libgit2/pull/6584 +* Fixes #6344: git_branch_move now renames the reflog instead of deleting + by @arroz in https://github.com/libgit2/libgit2/pull/6345 +* #6576 git_diff_index_to_workdir reverse now loads untracked content by + @arroz in https://github.com/libgit2/libgit2/pull/6577 + +### Build and CI improvements +* meta: the main branch is now v1.7.0 by @ethomson in + https://github.com/libgit2/libgit2/pull/6516 +* xdiff: move xdiff to 'deps' by @ethomson in + https://github.com/libgit2/libgit2/pull/6482 +* util: detect all possible qsort_r and qsort_s variants by + @DimitryAndric in https://github.com/libgit2/libgit2/pull/6555 +* Work around -Werror problems when detecting qsort variants by + @DimitryAndric in https://github.com/libgit2/libgit2/pull/6558 +* actions: simplify execution with composite action by @ethomson in + https://github.com/libgit2/libgit2/pull/6488 +* CMake: Search for ssh2 instead of libssh2. by @Faless in + https://github.com/libgit2/libgit2/pull/6586 + +### Documentation improvements +* docs: fix IRC server from freenode to libera by @vincenzopalazzo in + https://github.com/libgit2/libgit2/pull/6590 + +### Dependency upgrades +* Update xdiff to git 2.40.1's version by @ethomson in + https://github.com/libgit2/libgit2/pull/6561 +* deps: update pcre to 8.45 by @ethomson in + https://github.com/libgit2/libgit2/pull/6593 + +## New Contributors +* @oreiche made their first contribution in + https://github.com/libgit2/libgit2/pull/6554 +* @DimitryAndric made their first contribution in + https://github.com/libgit2/libgit2/pull/6555 +* @vincenzopalazzo made their first contribution in + https://github.com/libgit2/libgit2/pull/6590 +* @Faless made their first contribution in + https://github.com/libgit2/libgit2/pull/6586 + +**Full Changelog**: https://github.com/libgit2/libgit2/compare/v1.6.3...v1.7.0 + +v1.6.3 +------ + +## What's Changed + +### Bug fixes + +* odb: restore `git_odb_open` by @ethomson in https://github.com/libgit2/libgit2/pull/6520 +* Ensure that `git_index_add_all` handles ignored directories by @ethomson in https://github.com/libgit2/libgit2/pull/6521 +* pack: use 64 bits for the number of objects by @carlosmn in https://github.com/libgit2/libgit2/pull/6530 + +### Build and CI improvements + +* Remove unused wditer variable by @georgthegreat in https://github.com/libgit2/libgit2/pull/6518 +* fs_path: let root run the ownership tests by @ethomson in https://github.com/libgit2/libgit2/pull/6513 +* sysdir: Do not declare win32 functions on non-win32 platforms by @Batchyx in https://github.com/libgit2/libgit2/pull/6527 +* cmake: don't include `include/git2` by @ethomson in https://github.com/libgit2/libgit2/pull/6529 + +## New Contributors +* @georgthegreat made their first contribution in https://github.com/libgit2/libgit2/pull/6518 + +**Full Changelog**: https://github.com/libgit2/libgit2/compare/v1.6.2...v1.6.3 + +v1.6.2 +------ + +## What's Changed +### Bug fixes + +* remote: always populate old id in update tips by @ethomson in https://github.com/libgit2/libgit2/pull/6506 + The update tips callback would not always be properly provided with an empty (`0000000...`) OID for new refs. + +* Revert #6503 by @ethomson in https://github.com/libgit2/libgit2/pull/6511 + The certificate callback added port information for callbacks in #6503, but the format was ambiguous with IPv6 addresses. Revert this change temporarily. + +* Add `git_odb_backend_loose` back by @ethomson in https://github.com/libgit2/libgit2/pull/6512 + During SHA256 refactoring, the `git_odb_backend_loose` API was accidentally removed. Add it back. + +* meta: configure pkg-config .pc correctly by @ethomson in https://github.com/libgit2/libgit2/pull/6514 + During SHA256 refactoring, the pkg-config `.pc` file was erroneously renamed to `git2` instead of `libgit2`. Repair this. + +**Full Changelog**: https://github.com/libgit2/libgit2/compare/v1.6.1...v1.6.2 + +v1.6 +---- + +This is release v1.6.1, "Hubbeliges Krokodil". This release adds experimental SHA256 support and includes many new features and bugfixes. This release replaces libgit2 v1.6.0, which did not correctly update its version number(s). + +## What's Changed + +### New features + +* **Support for bare repositories with SHA256 support (experimental)** by @ethomson in https://github.com/libgit2/libgit2/pull/6191 + You can configure experimental SHA256 support in libgit2 with `cmake -DEXPERIMENTAL_SHA256=ON` during project setup. This is useful for considering future integrations, work on clients, and work on language bindings. At present, working with bare repositories should largely work, including remote operations. But many pieces of functionality - including working with the index - are not yet supported. As a result, **libgit2 with SHA256 support should not be used in production or released with package distribution.** + +* **Support the notion of a home directory separately from global configuration directory** by @ethomson in https://github.com/libgit2/libgit2/pull/6455 and https://github.com/libgit2/libgit2/pull/6456 + Callers and language bindings can now configure the home directory that libgit2 uses for file lookups (eg, the `.ssh` directory). This configuration is separate from the git global configuration path. + +* **stash: partial stash specific files** by @gitkraken-jacobw in https://github.com/libgit2/libgit2/pull/6330 + A stash can be created with only specific files, using a pathspec. This is similar to the `git stash push` command. + +* **push: revparse refspec source, so you can push things that are not refs** by @sven-of-cord in https://github.com/libgit2/libgit2/pull/6362 + Pushes can be performed using refspecs instead of only references. + +* **Support OpenSSL3** by @ethomson in https://github.com/libgit2/libgit2/pull/6464 and https://github.com/libgit2/libgit2/pull/6471 + OpenSSL 3 is now supported, both when compiled directly and dynamically loaded. + +### Bug fixes +* winhttp: support long custom headers by @kcsaul in https://github.com/libgit2/libgit2/pull/6363 +* Fix memory leak by @csware in https://github.com/libgit2/libgit2/pull/6382 +* Don't fail the whole clone if you can't find a default branch by @torvalds in https://github.com/libgit2/libgit2/pull/6369 +* #6366: When a worktree is missing, return `GIT_ENOTFOUND`. by @arroz in https://github.com/libgit2/libgit2/pull/6395 +* commit-graph: only verify csum on `git_commit_graph_open()`. by @derrickstolee in https://github.com/libgit2/libgit2/pull/6420 +* Ignore missing 'safe.directory' config during ownership checks by @kcsaul in https://github.com/libgit2/libgit2/pull/6408 +* Fix leak in `git_tag_create_from_buffer` by @julianmesa-gitkraken in https://github.com/libgit2/libgit2/pull/6421 +* http: Update httpclient options when reusing an existing connection. by @slackner in https://github.com/libgit2/libgit2/pull/6416 +* Add support for `safe.directory *` by @csware in https://github.com/libgit2/libgit2/pull/6429 +* URL parsing for google-compatible URLs by @ethomson in https://github.com/libgit2/libgit2/pull/6326 +* Fixes #6433: `git_submodule_update` fails to update configured but missing submodule by @tagesuhu in https://github.com/libgit2/libgit2/pull/6434 +* transport: fix capabilities calculation by @russell in https://github.com/libgit2/libgit2/pull/6435 +* push: use resolved oid as the source by @ethomson in https://github.com/libgit2/libgit2/pull/6452 +* Use `git_clone__submodule` to avoid file checks in workdir by @abizjak in https://github.com/libgit2/libgit2/pull/6444 +* #6422: handle dangling symbolic refs gracefully by @arroz in https://github.com/libgit2/libgit2/pull/6423 +* `diff_file`: Fix crash when freeing a patch representing an empty untracked file by @jorio in https://github.com/libgit2/libgit2/pull/6475 +* clone: clean up options on failure by @ethomson in https://github.com/libgit2/libgit2/pull/6479 +* stash: update strarray usage by @ethomson in https://github.com/libgit2/libgit2/pull/6487 +* #6491: Sets `oid_type` on repos open with `git_repository_open_bare` by @arroz in https://github.com/libgit2/libgit2/pull/6492 +* Handle Win32 shares by @ethomson in https://github.com/libgit2/libgit2/pull/6493 +* Make failure to connect to ssh-agent non-fatal by @fxcoudert in https://github.com/libgit2/libgit2/pull/6497 +* odb: don't unconditionally add `oid_type` to stream by @ethomson in https://github.com/libgit2/libgit2/pull/6499 +* Pass hostkey & port to host verify callback by @fxcoudert in https://github.com/libgit2/libgit2/pull/6503 + +### Code cleanups +* meta: update version number to v1.6.0-alpha by @ethomson in https://github.com/libgit2/libgit2/pull/6352 +* sha256: indirection for experimental functions by @ethomson in https://github.com/libgit2/libgit2/pull/6354 +* Delete `create.c.bak` by @lrm29 in https://github.com/libgit2/libgit2/pull/6398 +* Support non-cmake builds with an in-tree `experimental.h` by @ethomson in https://github.com/libgit2/libgit2/pull/6405 + +### Build and CI improvements +* tests: skip flaky-ass googlesource tests by @ethomson in https://github.com/libgit2/libgit2/pull/6353 +* clar: remove ftrunacte from libgit2 tests by @boretrk in https://github.com/libgit2/libgit2/pull/6357 +* CI Improvements by @ethomson in https://github.com/libgit2/libgit2/pull/6403 +* fix compile on Windows with `-DWIN32_LEAN_AND_MEAN` by @christoph-cullmann in https://github.com/libgit2/libgit2/pull/6373 +* Fixes #6365 : Uppercase windows.h include fails build in case-sensitive OS by @Vinz2008 in https://github.com/libgit2/libgit2/pull/6377 +* ci: update version numbers of actions by @ethomson in https://github.com/libgit2/libgit2/pull/6448 +* thread: avoid warnings when building without threads by @ethomson in https://github.com/libgit2/libgit2/pull/6432 +* src: hide unused hmac() prototype by @0-wiz-0 in https://github.com/libgit2/libgit2/pull/6458 +* tests: update clar test runner by @ethomson in https://github.com/libgit2/libgit2/pull/6459 +* ci: always create test summaries, even on failure by @ethomson in https://github.com/libgit2/libgit2/pull/6460 +* Fix build failure with `-DEMBED_SSH_PATH` by @vicr123 in https://github.com/libgit2/libgit2/pull/6374 +* Define correct `off64_t` for AIX by @bzEq in https://github.com/libgit2/libgit2/pull/6376 +* Fix some warnings in main by @ethomson in https://github.com/libgit2/libgit2/pull/6480 +* strarray: remove deprecated declaration by @ethomson in https://github.com/libgit2/libgit2/pull/6486 +* tests: always unset `HTTP_PROXY` before starting tests by @ethomson in https://github.com/libgit2/libgit2/pull/6498 + +### Documentation improvements +* add 2-clause BSD license to COPYING by @martinvonz in https://github.com/libgit2/libgit2/pull/6413 +* Add new PHP bindings project to language bindings section of README.md by @RogerGee in https://github.com/libgit2/libgit2/pull/6473 +* README: clarify the linking exception by @ethomson in https://github.com/libgit2/libgit2/pull/6494 +* Correct the definition of "empty" in the docs for `git_repository_is_empty` by @timrogers in https://github.com/libgit2/libgit2/pull/6500 + +## New Contributors +* @christoph-cullmann made their first contribution in https://github.com/libgit2/libgit2/pull/6373 +* @Vinz2008 made their first contribution in https://github.com/libgit2/libgit2/pull/6377 +* @torvalds made their first contribution in https://github.com/libgit2/libgit2/pull/6369 +* @derrickstolee made their first contribution in https://github.com/libgit2/libgit2/pull/6420 +* @julianmesa-gitkraken made their first contribution in https://github.com/libgit2/libgit2/pull/6421 +* @slackner made their first contribution in https://github.com/libgit2/libgit2/pull/6416 +* @martinvonz made their first contribution in https://github.com/libgit2/libgit2/pull/6413 +* @tagesuhu made their first contribution in https://github.com/libgit2/libgit2/pull/6434 +* @russell made their first contribution in https://github.com/libgit2/libgit2/pull/6435 +* @sven-of-cord made their first contribution in https://github.com/libgit2/libgit2/pull/6362 +* @0-wiz-0 made their first contribution in https://github.com/libgit2/libgit2/pull/6458 +* @abizjak made their first contribution in https://github.com/libgit2/libgit2/pull/6444 +* @vicr123 made their first contribution in https://github.com/libgit2/libgit2/pull/6374 +* @bzEq made their first contribution in https://github.com/libgit2/libgit2/pull/6376 +* @gitkraken-jacobw made their first contribution in https://github.com/libgit2/libgit2/pull/6330 +* @fxcoudert made their first contribution in https://github.com/libgit2/libgit2/pull/6497 +* @timrogers made their first contribution in https://github.com/libgit2/libgit2/pull/6500 + +**Full Changelog**: https://github.com/libgit2/libgit2/compare/v1.5.0...v1.6.0 + +v1.5 +---- + +This is release v1.5.0, "Stubentiger". This release adds the basis for an experimental CLI, continues preparing for SHA256 support, adds a benchmarking utility, and has numerous new features and bugfixes. + +## What's Changed +### New features +* The beginnings of a git-compatible CLI for testing and benchmarking by @ethomson in https://github.com/libgit2/libgit2/pull/6133 +* Add `clone` support to the CLI @ethomson in https://github.com/libgit2/libgit2/pull/6274 +* A benchmarking suite to compare libgit2 functionality against git by @ethomson in https://github.com/libgit2/libgit2/pull/6235 +* SHA256: add a SHA256 implementation backend by @ethomson in https://github.com/libgit2/libgit2/pull/6144 +* SHA256: support dynamically loaded openssl by @ethomson in https://github.com/libgit2/libgit2/pull/6258 +* Transport: introduce `git_transport_smart_remote_connect_options` by @lhchavez in https://github.com/libgit2/libgit2/pull/6278 +### Bug fixes +* Free parent and ref in lg2_commit before returning. by @apnadkarni in https://github.com/libgit2/libgit2/pull/6219 +* xdiff: use xdl_free not free by @ethomson in https://github.com/libgit2/libgit2/pull/6223 +* remote: do store the update_tips callback error value by @carlosmn in https://github.com/libgit2/libgit2/pull/6226 +* win32: `find_system_dirs` does not return `GIT_ENOTFOUND` by @ethomson in https://github.com/libgit2/libgit2/pull/6228 +* Some minor fixes for issues discovered by coverity by @ethomson in https://github.com/libgit2/libgit2/pull/6238 +* Fix a string concatenation bug when validating extensions by @bierbaum in https://github.com/libgit2/libgit2/pull/6246 +* fetch: support OID refspec without dst by @ethomson in https://github.com/libgit2/libgit2/pull/6251 +* Fix crash when regenerating a patch with unquoted spaces in filename by @jorio in https://github.com/libgit2/libgit2/pull/6244 +* midx: Fix an undefined behavior (left-shift signed overflow) by @lhchavez in https://github.com/libgit2/libgit2/pull/6260 +* Validate repository directory ownership by @ethomson in https://github.com/libgit2/libgit2/pull/6266 +* midx: fix large offset table check. by @ccstolley in https://github.com/libgit2/libgit2/pull/6309 +* midx: do not verify the checksum on load by @carlosmn in https://github.com/libgit2/libgit2/pull/6291 +* revparse: Remove error-prone, redundant test by @dongcarl in https://github.com/libgit2/libgit2/pull/6299 +* refs: fix missing error message by @zawata in https://github.com/libgit2/libgit2/pull/6305 +* CLI: progress updates by @ethomson in https://github.com/libgit2/libgit2/pull/6319 +* A couple of simplications around mwindow by @carlosmn in https://github.com/libgit2/libgit2/pull/6288 +* config: update config entry iteration lifecycle by @ethomson in https://github.com/libgit2/libgit2/pull/6320 +* repo: allow administrator to own the configuration by @ethomson in https://github.com/libgit2/libgit2/pull/6321 +* filter: Fix Segfault by @zawata in https://github.com/libgit2/libgit2/pull/6303 +* ntlmclient: LibreSSL 3.5 removed HMAC_CTX_cleanup by @vishwin in https://github.com/libgit2/libgit2/pull/6340 +* Fix internal git_sysdir_find* function usage within public git_config_find* functions by @kcsaul in https://github.com/libgit2/libgit2/pull/6335 +* fix interactive rebase detect. by @i-tengfei in https://github.com/libgit2/libgit2/pull/6334 +* cmake: drop posix dependency from pcre* detection by @jpalus in https://github.com/libgit2/libgit2/pull/6333 +* Fix erroneously lax configuration ownership checks by @ethomson in https://github.com/libgit2/libgit2/pull/6341 +* pack: don't pretend we support pack files v3 by @ethomson in https://github.com/libgit2/libgit2/pull/6347 +* Fix creation of branches and tags with invalid names by @lya001 in https://github.com/libgit2/libgit2/pull/6348 +### Security fixes +* Fixes for CVE 2022-29187 by @ethomson in https://github.com/libgit2/libgit2/pull/6349 +* zlib: update bundled zlib to v1.2.12 by @ethomson in https://github.com/libgit2/libgit2/pull/6350 +### Code cleanups +* sha256: refactoring in preparation for sha256 by @ethomson in https://github.com/libgit2/libgit2/pull/6265 +* remote: Delete a now-inexistent API declaration by @lhchavez in https://github.com/libgit2/libgit2/pull/6276 +* Fix missing include by @cschlack in https://github.com/libgit2/libgit2/pull/6277 +### Build and CI improvements +* meta: show build status for v1.3 and v1.4 branches by @ethomson in https://github.com/libgit2/libgit2/pull/6216 +* cmake: Fix package name for system http-parser by @mgorny in https://github.com/libgit2/libgit2/pull/6217 +* meta: update version number to v1.5.0-alpha by @ethomson in https://github.com/libgit2/libgit2/pull/6220 +* cmake: export libraries needed to compile against libgit2 by @ethomson in https://github.com/libgit2/libgit2/pull/6239 +* clone: update bitbucket tests by @ethomson in https://github.com/libgit2/libgit2/pull/6252 +* diff: don't stat empty file on arm32 (flaky test) by @ethomson in https://github.com/libgit2/libgit2/pull/6259 +* tests: support flaky stat by @ethomson in https://github.com/libgit2/libgit2/pull/6262 +* Include test results data in CI by @ethomson in https://github.com/libgit2/libgit2/pull/6306 +* Add a .clang-format with our style by @ethomson in https://github.com/libgit2/libgit2/pull/6023 +* CI: limits actions scheduled workflows to the main repo by @ethomson in https://github.com/libgit2/libgit2/pull/6342 +* ci: update dockerfiles for mbedTLS new url by @ethomson in https://github.com/libgit2/libgit2/pull/6343 +### Documentation improvements +* Add Pharo to language bindings by @theseion in https://github.com/libgit2/libgit2/pull/6310 +* Add link to Tcl bindings for libgit2 by @apnadkarni in https://github.com/libgit2/libgit2/pull/6318 +* fix couple of typos by @SkinnyMind in https://github.com/libgit2/libgit2/pull/6287 +* update documentation for default status options by @ethomson in https://github.com/libgit2/libgit2/pull/6322 + +## New Contributors +* @bierbaum made their first contribution in https://github.com/libgit2/libgit2/pull/6246 +* @dongcarl made their first contribution in https://github.com/libgit2/libgit2/pull/6299 +* @SkinnyMind made their first contribution in https://github.com/libgit2/libgit2/pull/6287 +* @zawata made their first contribution in https://github.com/libgit2/libgit2/pull/6305 +* @vishwin made their first contribution in https://github.com/libgit2/libgit2/pull/6340 +* @i-tengfei made their first contribution in https://github.com/libgit2/libgit2/pull/6334 +* @jpalus made their first contribution in https://github.com/libgit2/libgit2/pull/6333 +* @lya001 made their first contribution in https://github.com/libgit2/libgit2/pull/6348 + +**Full Changelog**: https://github.com/libgit2/libgit2/compare/v1.4.0...v1.5.0 + +v1.4 +---- + +This is release v1.4.0, "Fisematenten". This release includes several new features and bugfixes, improves compatibility with git, and begins preparation for SHA256 support in a future release. + +## What's Changed +### New features +* diff: update rename limit to 1000 to match git's behavior by @ethomson in https://github.com/libgit2/libgit2/pull/6092 +* odb: support checking for object existence without refresh by @joshtriplett in https://github.com/libgit2/libgit2/pull/6107 +* object: provide a low-level mechanism to validate whether a raw object is valid (`git_object_rawcontent_is_valid`) by @ethomson in https://github.com/libgit2/libgit2/pull/6128 +* blob: provide a function to identify binary content by @ethomson in https://github.com/libgit2/libgit2/pull/6142 +* status: add `rename_threshold` to `git_status_options`. by @arroz in https://github.com/libgit2/libgit2/pull/6158 +* remote: support `http.followRedirects` (`false` and `initial`) and follow initial redirects by default by @ethomson in https://github.com/libgit2/libgit2/pull/6175 +* remote: support scp style paths with ports (`[git@github.com:22]:libgit2/libgit2`) by @ethomson in https://github.com/libgit2/libgit2/pull/6167 +* win32: update git for windows configuration file location compatibility by @csware in https://github.com/libgit2/libgit2/pull/6151 and @ethomson in https://github.com/libgit2/libgit2/pull/6180 +* refs: speed up packed reference lookups when packed refs are sorted by @ccstolley in https://github.com/libgit2/libgit2/pull/6138 +* merge: support zdiff3 conflict styles by @ethomson in https://github.com/libgit2/libgit2/pull/6195 +* remote: support fetching by object id (using "+oid:ref" refspec syntax) by @ethomson in https://github.com/libgit2/libgit2/pull/6203 +* merge: callers can specify virtual-base building behavior and to optionally accept conflict markers as a resolution by @boretrk in https://github.com/libgit2/libgit2/pull/6204 + +### Bug fixes +* Fix a gcc 11 warning in src/threadstate.c by @lhchavez in https://github.com/libgit2/libgit2/pull/6115 +* Fix a gcc 11 warning in src/thread.h by @lhchavez in https://github.com/libgit2/libgit2/pull/6116 +* cmake: re-enable WinHTTP by @ethomson in https://github.com/libgit2/libgit2/pull/6120 +* Fix repo init when template dir is non-existent by @ammgws in https://github.com/libgit2/libgit2/pull/6106 +* cmake: use project-specific root variable instead of CMAKE_SOURCE_DIR by @Qix- in https://github.com/libgit2/libgit2/pull/6146 +* Better revparse compatibility for at time notation by @yoichi in https://github.com/libgit2/libgit2/pull/6095 +* remotes: fix insteadOf/pushInsteadOf handling by @mkhl in https://github.com/libgit2/libgit2/pull/6101 +* git_commit_summary: ignore lines with spaces by @stforek in https://github.com/libgit2/libgit2/pull/6125 +* Config parsing by @csware in https://github.com/libgit2/libgit2/pull/6124 +* config: handle empty conditional in includeIf by @ethomson in https://github.com/libgit2/libgit2/pull/6165 +* #6154 git_status_list_new case insensitive fix by @arroz in https://github.com/libgit2/libgit2/pull/6159 +* futils_mktmp: don't use umask by @boretrk in https://github.com/libgit2/libgit2/pull/6178 +* revparse: support bare '@' by @ethomson in https://github.com/libgit2/libgit2/pull/6196 +* odb: check for write failures by @ethomson in https://github.com/libgit2/libgit2/pull/6206 +* push: Prepare pack before sending pack header. by @ccstolley in https://github.com/libgit2/libgit2/pull/6205 +* mktmp: improve our temp file creation by @ethomson in https://github.com/libgit2/libgit2/pull/6207 +* diff_file: fix crash if size of diffed file changes in workdir by @jorio in https://github.com/libgit2/libgit2/pull/6208 +* merge: comment conflicts lines in MERGE_MSG by @ethomson in https://github.com/libgit2/libgit2/pull/6197 +* Fix crashes in example programs on Windows (sprintf_s not compatible with snprintf) by @apnadkarni in https://github.com/libgit2/libgit2/pull/6212 + +### Code cleanups +* Introduce `git_remote_connect_options` by @ethomson in https://github.com/libgit2/libgit2/pull/6161 +* hash: separate hashes and git_oid by @ethomson in https://github.com/libgit2/libgit2/pull/6082 +* `git_buf`: now a public-only API (`git_str` is our internal API) by @ethomson in https://github.com/libgit2/libgit2/pull/6078 +* cmake: cleanups and consistency by @ethomson in https://github.com/libgit2/libgit2/pull/6084 +* path: refactor utility path functions by @ethomson in https://github.com/libgit2/libgit2/pull/6104 +* str: git_str_free is never a function by @ethomson in https://github.com/libgit2/libgit2/pull/6111 +* cmake refactorings by @ethomson in https://github.com/libgit2/libgit2/pull/6112 +* Add missing-declarations warning globally by @ethomson in https://github.com/libgit2/libgit2/pull/6113 +* cmake: further refactorings by @ethomson in https://github.com/libgit2/libgit2/pull/6114 +* tag: set validity to 0 by default by @ethomson in https://github.com/libgit2/libgit2/pull/6119 +* util: minor cleanup and refactoring to the date class by @ethomson in https://github.com/libgit2/libgit2/pull/6121 +* Minor code cleanups by @ethomson in https://github.com/libgit2/libgit2/pull/6122 +* Fix a long long that crept past by @NattyNarwhal in https://github.com/libgit2/libgit2/pull/6094 +* remote: refactor insteadof application by @ethomson in https://github.com/libgit2/libgit2/pull/6147 +* ntmlclient: fix linking with libressl by @boretrk in https://github.com/libgit2/libgit2/pull/6157 +* c99: change single bit flags to unsigned by @boretrk in https://github.com/libgit2/libgit2/pull/6179 +* Fix typos by @rex4539 in https://github.com/libgit2/libgit2/pull/6164 +* diff_driver: split global_drivers array into separate elements by @boretrk in https://github.com/libgit2/libgit2/pull/6184 +* cmake: disable some gnu extensions by @boretrk in https://github.com/libgit2/libgit2/pull/6185 +* Disabling setting `CMAKE_FIND_LIBRARY_SUFFIXES` on Apple platforms. by @arroz in https://github.com/libgit2/libgit2/pull/6153 +* C90: add inline macro to xdiff and mbedtls by @boretrk in https://github.com/libgit2/libgit2/pull/6200 +* SHA256: early preparation by @ethomson in https://github.com/libgit2/libgit2/pull/6192 + +### CI improvements +* tests: rename test runner to `libgit2_tests`, build option to `BUILD_TESTS`. by @ethomson in https://github.com/libgit2/libgit2/pull/6083 +* ci: only update docs on push by @ethomson in https://github.com/libgit2/libgit2/pull/6108 +* Pedantic header test by @boretrk in https://github.com/libgit2/libgit2/pull/6086 +* ci: build with ssh on nightly by @ethomson in https://github.com/libgit2/libgit2/pull/6148 +* ci: improve the name in CI runs by @ethomson in https://github.com/libgit2/libgit2/pull/6198 + +### Documentation improvements +* Document that `git_odb` is thread-safe by @joshtriplett in https://github.com/libgit2/libgit2/pull/6109 +* Improve documentation by @punkymaniac in https://github.com/libgit2/libgit2/pull/6168 + +### Other changes +* libgit2_clar is now libgit2_tests by @mkhl in https://github.com/libgit2/libgit2/pull/6100 +* Remove PSGit from Language Bindings section of README by @cestrand in https://github.com/libgit2/libgit2/pull/6150 +* COPYING: remove regex copyright, add PCRE copyright by @ethomson in https://github.com/libgit2/libgit2/pull/6187 +* meta: add a release configuration file by @ethomson in https://github.com/libgit2/libgit2/pull/6211 + +## New Contributors +* @mkhl made their first contribution in https://github.com/libgit2/libgit2/pull/6100 +* @ammgws made their first contribution in https://github.com/libgit2/libgit2/pull/6106 +* @yoichi made their first contribution in https://github.com/libgit2/libgit2/pull/6095 +* @stforek made their first contribution in https://github.com/libgit2/libgit2/pull/6125 +* @cestrand made their first contribution in https://github.com/libgit2/libgit2/pull/6150 +* @rex4539 made their first contribution in https://github.com/libgit2/libgit2/pull/6164 +* @jorio made their first contribution in https://github.com/libgit2/libgit2/pull/6208 + +**Full Changelog**: https://github.com/libgit2/libgit2/compare/v1.3.0...v1.4.0 + +v1.3 +---- + +This is release v1.3.0, "Zugunruhe". This release includes only minor new features that will be helpful for users to have an orderly transition to the v2.0 lineage. + +## New Features +* Support custom git extensions by @ethomson in https://github.com/libgit2/libgit2/pull/6031 +* Introduce `git_email_create`; deprecate `git_diff_format_email` by @ethomson in https://github.com/libgit2/libgit2/pull/6061 + +## Deprecated APIs +* `git_oidarray_free` is deprecated; callers should use `git_oidarray_dispose` + +## Bug fixes +* #6028: Check if `threadstate->error_t.message` is not `git_buf__initbuf` before freeing. by @arroz in https://github.com/libgit2/libgit2/pull/6029 +* remote: Mark `git_remote_name_is_valid` as `GIT_EXTERN` by @lhchavez in https://github.com/libgit2/libgit2/pull/6032 +* Fix config parsing for multiline with multiple quoted comment chars by @basile-henry in https://github.com/libgit2/libgit2/pull/6043 +* indexer: Avoid one `mmap(2)`/`munmap(2)` pair per `git_indexer_append` call by @lhchavez in https://github.com/libgit2/libgit2/pull/6039 +* merge: Check file mode when resolving renames by @ccstolley in https://github.com/libgit2/libgit2/pull/6060 +* Allow proxy options when connecting with a detached remote. by @lrm29 in https://github.com/libgit2/libgit2/pull/6058 +* win32: allow empty environment variables by @ethomson in https://github.com/libgit2/libgit2/pull/6063 +* Fixes for deprecated APIs by @ethomson in https://github.com/libgit2/libgit2/pull/6066 +* filter: use a `git_oid` in filter options, not a pointer by @ethomson in https://github.com/libgit2/libgit2/pull/6067 +* diff: update `GIT_DIFF_IGNORE_BLANK_LINES` by @ethomson in https://github.com/libgit2/libgit2/pull/6068 +* Attribute lookups are always on relative paths by @ethomson in https://github.com/libgit2/libgit2/pull/6073 +* Handle long paths when querying attributes by @ethomson in https://github.com/libgit2/libgit2/pull/6075 + +## Code cleanups +* notes: use a buffer internally by @ethomson in https://github.com/libgit2/libgit2/pull/6047 +* Fix coding style for pointer by @punkymaniac in https://github.com/libgit2/libgit2/pull/6045 +* Use __typeof__ GNUC keyword for ISO C compatibility by @duncanthomson in https://github.com/libgit2/libgit2/pull/6041 +* Discover libssh2 without pkg-config by @stac47 in https://github.com/libgit2/libgit2/pull/6053 +* Longpath filter bug by @lrm29 in https://github.com/libgit2/libgit2/pull/6055 +* Add test to ensure empty proxy env behaves like unset env by @sathieu in https://github.com/libgit2/libgit2/pull/6052 +* Stdint header condition has been reverted. by @lolgear in https://github.com/libgit2/libgit2/pull/6020 +* buf: `common_prefix` takes a string array by @ethomson in https://github.com/libgit2/libgit2/pull/6077 +* oidarray: introduce `git_oidarray_dispose` by @ethomson in https://github.com/libgit2/libgit2/pull/6076 +* examples: Free the git_config and git_config_entry after use by @257 in https://github.com/libgit2/libgit2/pull/6071 + +## CI Improvements +* ci: pull libssh2 from www.libssh2.org by @ethomson in https://github.com/libgit2/libgit2/pull/6064 + +## Documentation changes +* Update README.md by @shijinglu in https://github.com/libgit2/libgit2/pull/6050 + +## New Contributors +* @basile-henry made their first contribution in https://github.com/libgit2/libgit2/pull/6043 +* @duncanthomson made their first contribution in https://github.com/libgit2/libgit2/pull/6041 +* @stac47 made their first contribution in https://github.com/libgit2/libgit2/pull/6053 +* @shijinglu made their first contribution in https://github.com/libgit2/libgit2/pull/6050 +* @ccstolley made their first contribution in https://github.com/libgit2/libgit2/pull/6060 +* @sathieu made their first contribution in https://github.com/libgit2/libgit2/pull/6052 +* @257 made their first contribution in https://github.com/libgit2/libgit2/pull/6071 + +**Full Changelog**: https://github.com/libgit2/libgit2/compare/v1.2.0...v1.3.0 + +--------------------------------------------------------------------- + +v1.2 +----- + +This is release v1.2.0, "Absacker". This release includes many new features: in particular, support for commit graphs, multi-pack indexes, and `core.longpaths` support. + +This is meant to be the final minor release in the v1 lineage. v2.0 will be the next major release and will remove deprecated APIs and may include breaking changes. + +## Deprecated APIs + +* revspec: rename git_revparse_mode_t to git_revspec_t by @ethomson in https://github.com/libgit2/libgit2/pull/5786 +* tree: deprecate `git_treebuilder_write_with_buffer` by @ethomson in https://github.com/libgit2/libgit2/pull/5815 +* Deprecate `is_valid_name` functions; replace with `name_is_valid` functions by @ethomson in https://github.com/libgit2/libgit2/pull/5659 +* filter: stop taking git_buf as user input by @ethomson in https://github.com/libgit2/libgit2/pull/5859 +* remote: introduce remote_ready_cb, deprecate resolve_url callback by @ethomson in https://github.com/libgit2/libgit2/pull/6012 +* Introduce `create_commit_cb`, deprecate `signing_cb` by @ethomson in https://github.com/libgit2/libgit2/pull/6016 +* filter: filter drivers stop taking git_buf as user input by @ethomson in https://github.com/libgit2/libgit2/pull/6011 +* buf: deprecate public git_buf writing functions by @ethomson in https://github.com/libgit2/libgit2/pull/6017 + +## New features + +* winhttp: support optional client cert by @ianhattendorf in https://github.com/libgit2/libgit2/pull/5384 +* Add support for additional SSH hostkey types. by @arroz in https://github.com/libgit2/libgit2/pull/5750 +* Handle ipv6 addresses by @ethomson in https://github.com/libgit2/libgit2/pull/5741 +* zlib: Add support for building with Chromium's zlib implementation by @lhchavez in https://github.com/libgit2/libgit2/pull/5748 +* commit-graph: Introduce a parser for commit-graph files by @lhchavez in https://github.com/libgit2/libgit2/pull/5762 +* patch: add owner accessor by @KOLANICH in https://github.com/libgit2/libgit2/pull/5731 +* commit-graph: Support lookups of entries in a commit-graph by @lhchavez in https://github.com/libgit2/libgit2/pull/5763 +* commit-graph: Introduce `git_commit_graph_needs_refresh()` by @lhchavez in https://github.com/libgit2/libgit2/pull/5764 +* Working directory path validation by @ethomson in https://github.com/libgit2/libgit2/pull/5823 +* Support `core.longpaths` on Windows by @ethomson in https://github.com/libgit2/libgit2/pull/5857 +* git_reference_create_matching: Treat all-zero OID as "must be absent" by @novalis in https://github.com/libgit2/libgit2/pull/5842 +* diff:add option to ignore blank line changes by @yuuri in https://github.com/libgit2/libgit2/pull/5853 +* [Submodule] Git submodule dup by @lolgear in https://github.com/libgit2/libgit2/pull/5890 +* commit-graph: Use the commit-graph in revwalks by @lhchavez in https://github.com/libgit2/libgit2/pull/5765 +* commit-graph: Introduce `git_commit_list_generation_cmp` by @lhchavez in https://github.com/libgit2/libgit2/pull/5766 +* graph: Create `git_graph_reachable_from_any()` by @lhchavez in https://github.com/libgit2/libgit2/pull/5767 +* Support reading attributes from a specific commit by @ethomson in https://github.com/libgit2/libgit2/pull/5952 +* [Branch] Branch upstream with format by @lolgear in https://github.com/libgit2/libgit2/pull/5861 +* Dynamically load OpenSSL (optionally) by @ethomson in https://github.com/libgit2/libgit2/pull/5974 +* Set refs/remotes/origin/HEAD to default branch when branch is specified by @A-Ovchinnikov-mx in https://github.com/libgit2/libgit2/pull/6010 +* midx: Add a way to write multi-pack-index files by @lhchavez in https://github.com/libgit2/libgit2/pull/5404 +* Use error code GIT_EAUTH for authentication failures by @josharian in https://github.com/libgit2/libgit2/pull/5395 +* midx: Introduce git_odb_write_multi_pack_index() by @lhchavez in https://github.com/libgit2/libgit2/pull/5405 +* Checkout dry-run by @J0Nes90 in https://github.com/libgit2/libgit2/pull/5841 +* mbedTLS: Fix setting certificate directory by @mikezackles in https://github.com/libgit2/libgit2/pull/6004 +* remote: introduce remote_ready_cb, deprecate resolve_url callback by @ethomson in https://github.com/libgit2/libgit2/pull/6012 +* Introduce `create_commit_cb`, deprecate `signing_cb` by @ethomson in https://github.com/libgit2/libgit2/pull/6016 +* commit-graph: Add a way to write commit-graph files by @lhchavez in https://github.com/libgit2/libgit2/pull/5778 + +## Bug fixes + +* Define `git___load` when building with `-DTHREADSAFE=OFF` by @lhchavez in https://github.com/libgit2/libgit2/pull/5664 +* Make the Windows leak detection more robust by @lhchavez in https://github.com/libgit2/libgit2/pull/5661 +* Refactor "global" state by @ethomson in https://github.com/libgit2/libgit2/pull/5546 +* threadstate: rename tlsdata when building w/o threads by @ethomson in https://github.com/libgit2/libgit2/pull/5668 +* Include `${MBEDTLS_INCLUDE_DIR}` when compiling `crypt_mbedtls.c` by @staticfloat in https://github.com/libgit2/libgit2/pull/5685 +* Fix the `-DTHREADSAFE=OFF` build by @lhchavez in https://github.com/libgit2/libgit2/pull/5690 +* Add missing worktree_dir check and test case by @rbmclean in https://github.com/libgit2/libgit2/pull/5692 +* msvc crtdbg -> win32 leakcheck by @ethomson in https://github.com/libgit2/libgit2/pull/5580 +* Introduce GIT_ASSERT macros by @ethomson in https://github.com/libgit2/libgit2/pull/5327 +* Also add the raw hostkey to `git_cert_hostkey` by @lhchavez in https://github.com/libgit2/libgit2/pull/5704 +* Make the odb race-free by @lhchavez in https://github.com/libgit2/libgit2/pull/5595 +* Make the pack and mwindow implementations data-race-free by @lhchavez in https://github.com/libgit2/libgit2/pull/5593 +* Thread-free implementation by @ethomson in https://github.com/libgit2/libgit2/pull/5719 +* Thread-local storage: a generic internal library (with no allocations) by @ethomson in https://github.com/libgit2/libgit2/pull/5720 +* Friendlier getting started in the lack of git_libgit2_init by @ethomson in https://github.com/libgit2/libgit2/pull/5578 +* Make git__strntol64() ~70%* faster by @lhchavez in https://github.com/libgit2/libgit2/pull/5735 +* Cache the parsed submodule config when diffing by @lhchavez in https://github.com/libgit2/libgit2/pull/5727 +* pack: continue zlib while we can make progress by @ethomson in https://github.com/libgit2/libgit2/pull/5740 +* Avoid using `__builtin_mul_overflow` with the clang+32-bit combo by @lhchavez in https://github.com/libgit2/libgit2/pull/5742 +* repository: use intptr_t's in the config map cache by @ethomson in https://github.com/libgit2/libgit2/pull/5746 +* Build with NO_MMAP by @0xdky in https://github.com/libgit2/libgit2/pull/5583 +* Add documentation for git_blob_filter_options.version by @JoshuaS3 in https://github.com/libgit2/libgit2/pull/5759 +* blob: fix name of `GIT_BLOB_FILTER_ATTRIBUTES_FROM_HEAD` by @ethomson in https://github.com/libgit2/libgit2/pull/5760 +* Cope with empty default branch by @ethomson in https://github.com/libgit2/libgit2/pull/5770 +* README: instructions for using libgit2 without compiling by @ethomson in https://github.com/libgit2/libgit2/pull/5772 +* Use `p_pwrite`/`p_pread` consistently throughout the codebase by @lhchavez in https://github.com/libgit2/libgit2/pull/5769 +* midx: Fix a bug in `git_midx_needs_refresh()` by @lhchavez in https://github.com/libgit2/libgit2/pull/5768 +* mwindow: Fix a bug in the LRU window finding code by @lhchavez in https://github.com/libgit2/libgit2/pull/5783 +* refdb_fs: Check git_sortedcache wlock/rlock errors by @mamapanda in https://github.com/libgit2/libgit2/pull/5800 +* index: Check git_vector_dup error in write_entries by @mamapanda in https://github.com/libgit2/libgit2/pull/5801 +* Fix documentation formating on repository.h by @punkymaniac in https://github.com/libgit2/libgit2/pull/5806 +* include: fix typos in comments by @tniessen in https://github.com/libgit2/libgit2/pull/5805 +* Fix some typos by @aaronfranke in https://github.com/libgit2/libgit2/pull/5797 +* Check git_signature_dup failure by @mamapanda in https://github.com/libgit2/libgit2/pull/5817 +* merge: Check insert_head_ids error in create_virtual_base by @mamapanda in https://github.com/libgit2/libgit2/pull/5818 +* winhttp: skip certificate check if unable to send request by @ianhattendorf in https://github.com/libgit2/libgit2/pull/5814 +* Default to GIT_BRANCH_DEFAULT if init.defaultBranch is empty string by @ianhattendorf in https://github.com/libgit2/libgit2/pull/5832 +* Fix diff_entrycount -> diff_num_deltas doc typo by @mjsir911 in https://github.com/libgit2/libgit2/pull/5838 +* repo: specify init.defaultbranch is meant to be a branch name by @carlosmn in https://github.com/libgit2/libgit2/pull/5835 +* repo: remove an inappropriate use of PASSTHROUGH by @carlosmn in https://github.com/libgit2/libgit2/pull/5834 +* src: fix typos in header files by @tniessen in https://github.com/libgit2/libgit2/pull/5843 +* test: clean up memory leaks by @ethomson in https://github.com/libgit2/libgit2/pull/5858 +* buf: remove unnecessary buf_text namespace by @ethomson in https://github.com/libgit2/libgit2/pull/5860 +* Fix bug in git_diff_find_similar. by @staktrace in https://github.com/libgit2/libgit2/pull/5839 +* Fix issues with Proxy Authentication after httpclient refactor by @implausible in https://github.com/libgit2/libgit2/pull/5852 +* tests: clean up memory leak, fail on leak for win32 by @ethomson in https://github.com/libgit2/libgit2/pull/5892 +* Tolerate readlink size less than st_size by @dtolnay in https://github.com/libgit2/libgit2/pull/5900 +* Define WINHTTP_NO_CLIENT_CERT_CONTEXT if needed by @jacquesg in https://github.com/libgit2/libgit2/pull/5929 +* Update from regex to pcre licensing information in docs/contributing.md by @boretrk in https://github.com/libgit2/libgit2/pull/5916 +* Consider files executable only if the user can execute them by @novalis in https://github.com/libgit2/libgit2/pull/5915 +* git__timer: Limit ITimer usage to AmigaOS4 by @boretrk in https://github.com/libgit2/libgit2/pull/5936 +* Fix memory leak in git_smart__connect by @punkymaniac in https://github.com/libgit2/libgit2/pull/5908 +* config: fix included configs not refreshed more than once by @Batchyx in https://github.com/libgit2/libgit2/pull/5926 +* Fix wrong time_t used in function by @NattyNarwhal in https://github.com/libgit2/libgit2/pull/5938 +* fix check for ignoring of negate rules by @palmin in https://github.com/libgit2/libgit2/pull/5824 +* Make `FIND_PACKAGE(PythonInterp)` prefer `python3` by @lhchavez in https://github.com/libgit2/libgit2/pull/5913 +* git__timer: Allow compilation on systems without CLOCK_MONOTONIC by @boretrk in https://github.com/libgit2/libgit2/pull/5945 +* stdintification: use int64_t and INT64_C instead of long long by @NattyNarwhal in https://github.com/libgit2/libgit2/pull/5941 +* Optional stricter allocation checking (for `malloc(0)` cases) by @ethomson in https://github.com/libgit2/libgit2/pull/5951 +* Variadic arguments aren't in C89 by @NattyNarwhal in https://github.com/libgit2/libgit2/pull/5948 +* Fix typo in general.c by @Crayon2000 in https://github.com/libgit2/libgit2/pull/5954 +* common.h: use inline when compiling for C99 and later by @boretrk in https://github.com/libgit2/libgit2/pull/5953 +* Fix one memory leak in master by @lhchavez in https://github.com/libgit2/libgit2/pull/5957 +* tests: reset odb backend priority by @ethomson in https://github.com/libgit2/libgit2/pull/5961 +* cmake: extended futimens checking on macOS by @ethomson in https://github.com/libgit2/libgit2/pull/5962 +* amiga: use ';' as path list separator on AmigaOS by @boretrk in https://github.com/libgit2/libgit2/pull/5978 +* Respect the force flag on refspecs in git_remote_fetch by @alexjg in https://github.com/libgit2/libgit2/pull/5854 +* Fix LIBGIT2_FILENAME not being passed to the resource compiler by @jairbubbles in https://github.com/libgit2/libgit2/pull/5994 +* sha1dc: remove conditional for by @boretrk in https://github.com/libgit2/libgit2/pull/5997 +* openssl: don't fail when we can't customize allocators by @ethomson in https://github.com/libgit2/libgit2/pull/5999 +* C11 warnings by @boretrk in https://github.com/libgit2/libgit2/pull/6005 +* open: input validation for empty segments in path by @boretrk in https://github.com/libgit2/libgit2/pull/5950 +* Introduce GIT_WARN_UNUSED_RESULT by @lhchavez in https://github.com/libgit2/libgit2/pull/5802 +* GCC C11 warnings by @boretrk in https://github.com/libgit2/libgit2/pull/6006 +* array: check dereference from void * type by @boretrk in https://github.com/libgit2/libgit2/pull/6007 +* Homogenize semantics for atomic-related functions by @lhchavez in https://github.com/libgit2/libgit2/pull/5747 +* git_array_alloc: return objects of correct type by @boretrk in https://github.com/libgit2/libgit2/pull/6008 +* CMake. hash sha1 header has been added. by @lolgear in https://github.com/libgit2/libgit2/pull/6013 +* tests: change comments to c89 style by @boretrk in https://github.com/libgit2/libgit2/pull/6015 +* Set Host Header to match CONNECT authority target by @lollipopman in https://github.com/libgit2/libgit2/pull/6022 +* Fix worktree iteration when repository has no common directory by @kcsaul in https://github.com/libgit2/libgit2/pull/5943 + +## Documentation improvements + +* Update README.md for additional Delphi bindings by @todaysoftware in https://github.com/libgit2/libgit2/pull/5831 +* Fix documentation formatting by @punkymaniac in https://github.com/libgit2/libgit2/pull/5850 +* docs: fix incorrect comment marker by @tiennou in https://github.com/libgit2/libgit2/pull/5897 +* Patch documentation by @punkymaniac in https://github.com/libgit2/libgit2/pull/5903 +* Fix misleading doc for `git_index_find` by @arxanas in https://github.com/libgit2/libgit2/pull/5910 +* docs: stop mentioning libgit2's "master" branch by @Batchyx in https://github.com/libgit2/libgit2/pull/5925 +* docs: fix some missing includes that cause Docurium to error out by @tiennou in https://github.com/libgit2/libgit2/pull/5917 +* Patch documentation by @punkymaniac in https://github.com/libgit2/libgit2/pull/5940 + +## Development improvements + +* WIP: .devcontainer: settings for a codespace workflow by @ethomson in https://github.com/libgit2/libgit2/pull/5508 + +## CI Improvements + +* Add a ThreadSanitizer build by @lhchavez in https://github.com/libgit2/libgit2/pull/5597 +* ci: more GitHub Actions by @ethomson in https://github.com/libgit2/libgit2/pull/5706 +* ci: run coverity in the nightly builds by @ethomson in https://github.com/libgit2/libgit2/pull/5707 +* ci: only report main branch in README status by @ethomson in https://github.com/libgit2/libgit2/pull/5708 +* Fix the `ENABLE_WERROR=ON` build in Groovy Gorilla (gcc 10.2) by @lhchavez in https://github.com/libgit2/libgit2/pull/5715 +* Re-enable the RC4 test by @carlosmn in https://github.com/libgit2/libgit2/pull/4418 +* ci: run codeql by @ethomson in https://github.com/libgit2/libgit2/pull/5709 +* github-actions: Also rename the main branch here by @lhchavez in https://github.com/libgit2/libgit2/pull/5771 +* ci: don't use ninja on macOS by @ethomson in https://github.com/libgit2/libgit2/pull/5780 +* ci: use GitHub for storing mingw-w64 build dependency by @ethomson in https://github.com/libgit2/libgit2/pull/5855 +* docker: remove the entrypoint by @ethomson in https://github.com/libgit2/libgit2/pull/5980 +* http: don't require a password by @ethomson in https://github.com/libgit2/libgit2/pull/5972 +* ci: update nightly to use source path by @ethomson in https://github.com/libgit2/libgit2/pull/5989 +* ci: add centos 7 and centos 8 by @ethomson in https://github.com/libgit2/libgit2/pull/5992 +* ci: update centos builds by @ethomson in https://github.com/libgit2/libgit2/pull/5995 +* ci: tag new containers with the latest tag by @ethomson in https://github.com/libgit2/libgit2/pull/6000 + +## Dependency updates + +* ntlm: [ntlmclient](https://github.com/ethomson/ntlmclient) is now v0.9.1 + +**Full Changelog**: https://github.com/libgit2/libgit2/compare/v1.1.0...v1.2.0 + +--------------------------------------------------------------------- + +v1.1 +---- + +This is release v1.1, "Fernweh". + +### Changes or improvements + +* Our bundled PCRE dependency has been updated to 8.44. + +* The `refs/remotes/origin/HEAD` file will be created at clone time to + point to the origin's default branch. + +* libgit2 now uses the `__atomic_` intrinsics instead of `__sync_` + intrinsics on supported gcc and clang versions. + +* The `init.defaultBranch` setting is now respected and `master` is + no longer the hardcoded as the default branch name. + +* Patch files that do not contain an `index` line can now be parsed. + +* Configuration files with multi-line values can now contain quotes + split across multiple lines. + +* Windows clients now attempt to use TLS1.3 when available. + +* Servers that request an upgrade to a newer HTTP version are + silently ignored instead of erroneously failing. + +* Users can pass `NULL` to the options argument to + `git_describe_commit`. + +* Clones and fetches of very large packfiles now succeeds on 32-bit + platforms. + +* Custom reference database backends can now handle the repository's + `HEAD` correctly. + +* Repositories with a large number of packfiles no longer exhaust the + number of file descriptors. + +* The test framework now supports TAP output when the `-t` flag is + specified. + +* The test framework can now specify an exact match to a test + function using a trailing `$`. + +* All checkout types support `GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH`. + +* `git_blame` now can ignore whitespace changes using the option + `GIT_BLAME_IGNORE_WHITESPACE`. + +* Several new examples have been created, including an examples for + commit, add and push. + +* Mode changes during rename are now supported in patch application. + +* `git_checkout_head` now correctly removes untracked files in a + subdirectory when the `FORCE | REMOVE_UNTRACKED` options are specified. + +v1.0.1 +------ + +This is a bugfix release with the following changes: + +- Calculating information about renamed files during merges is more + efficient because dissimilarity about files is now being cached and + no longer needs to be recomputed. + +- The `git_worktree_prune_init_options` has been correctly restored for + backward compatibility. In v1.0 it was incorrectly deprecated with a + typo. + +- The optional ntlmclient dependency now supports NetBSD. + +- A bug where attempting to stash on a bare repository may have failed + has been fixed. + +- Configuration files that are unreadable due to permissions are now + silently ignored, and treated as if they do not exist. This matches + git's behavior; previously this case would have been an error. + +- v4 index files are now correctly written; previously we would read + them correctly but would not write the prefix-compression accurately, + causing corruption. + +- A bug where the smart HTTP transport could not read large data packets + has been fixed. Previously, fetching from servers like Gerrit, that + sent large data packets, would error. + +--------------------------------------------------------------------- + +v1.0 +---- + +This is release v1.0 "Luftschloss", which is the first stabe release of +libgit2. The API will stay compatible across all releases of the same major +version. This release includes bugfixes only and supersedes v0.99, which will +stop being maintained. Both v0.27 and v0.28 stay supported in accordance with +our release policy. + +### Changes or improvements + +- CMake was converted to make use of the GNUInstallDirs module for both our + pkgconfig and install targets in favor of our custom build options + `BIN_INSTALL_DIR`, `LIB_INSTALL_DIR` and `INCLUDE_INSTALL_DIR`. Instead, you + can now use CMakes standard variables `CMAKE_INSTALL_BINDIR`, + `CMAKE_INSTALL_LIBDIR` and `CMAKE_INSTALL_INCLUDEDIR`. + +- Some CMake build options accepted either a specific value or a boolean value + to disable the option altogether or use automatic detection. We only accepted + "ON" or "OFF", but none of the other values CMake recognizes as boolean. This + was aligned with CMake's understanding of booleans. + +- The installed pkgconfig file contained incorrect values for both `libdir` and + `includedir` variables. + +- If using pcre2 for regular expressions, then we incorrectly added "pcre2" + instead of "pcre2-8" to our pkgconfig dependencies, which was corrected. + +- Fixed building the bundled ntlmclient dependency on FreeBSD, OpenBSD and + SunOS. + +- When writing symlinks on Windows, we incorrectly handled relative symlink + targets, which was corrected. + +- When using the HTTP protocol via macOS' SecureTransport implementation, reads + could stall at the end of the session and only continue after a timeout of 60 + seconds was reached. + +- The filesystem-based reference callback didn't corectly initialize the backend + version. + +- A segmentation fault was fixed when calling `git_blame_buffer()` for files + that were modified and added to the index. + +- A backwards-incompatible change was introduced when we moved some structures + from "git2/credentials.h" into "git2/sys/credentials.h". This was fixed in the + case where you do not use hard deprecation. + +- Improved error handling in various places. + + +v0.99 +----- + +This is v0.99 "Torschlusspanik". This will be the last minor release +before libgit2 v1.0. We expect to only respond to bugs in this release, +to stabilize it for next major release. + +It contains significant refactorings, but is expected to be API-compatible +with v0.28.0. + +### Changes or improvements + +* When fetching from an anonymous remote using a URL with authentication + information provided in the URL (eg `https://foo:bar@example.com/repo`), + we would erroneously include the literal URL in the FETCH_HEAD file. + We now remove that to match git's behavior. + +* Some credential structures, enums and values have been renamed: + `git_cred` is now `git_credential`. `git_credtype_t` is now + `git_credential_t`. Functions and types beginning with + `git_cred_` now begin with `git_credential`, and constants beginning + with `GIT_CREDTYPE` now begin with `GIT_CREDENTIAL`. The former names + are deprecated. + +* Several function signatures have been changed to return an `int` to + indicate error conditions. We encourage you to check them for errors + in the standard way. + + * `git_attr_cache_flush` + * `git_error_set_str` + * `git_index_name_clear` + * `git_index_reuc_clear` + * `git_libgit2_version` + * `git_mempack_reset` + * `git_oid_cpy` + * `git_oid_fmt` + * `git_oid_fromraw` + * `git_oid_nfmt` + * `git_oid_pathfmt` + * `git_remote_stop` + * `git_remote_disconnect` + * `git_repository__cleanup` + * `git_repository_set_config` + * `git_repository_set_index` + * `git_repository_set_odb` + * `git_repository_set_refdb` + * `git_revwalk_reset` + * `git_revwalk_simplify_first_parent` + * `git_revwalk_sorting` + * `git_treebuilder_clear` + * `git_treebuilder_filter` + +* The NTLM and Negotiate authentication mechanisms are now supported when + talking to git implementations hosted on Apache or nginx servers. + +* The `HEAD` symbolic reference can no longer be deleted. + +* `git_merge_driver_source_repo` no longer returns a `const git_repository *`, + it now returns a non-`const` `git_repository *`. + +* Relative symbolic links are now supported on Windows when `core.symlinks` + is enabled. + +* Servers that provide query parameters with a redirect are now supported. + +* `git_submodule_sync` will now resolve relative URLs. + +* When creating git endpoint URLs, double-slashes are no longer used when + the given git URL has a trailing slash. + +* On Windows, a `DllMain` function is no longer included and thread-local + storage has moved to fiber-local storage in order to prevent race + conditions during shutdown. + +* The tracing mechanism (`GIT_TRACE`) is now enabled by default and does + not need to be explicitly enabled in CMake. + +* The size of Git objects is now represented by `git_object_size_t` + instead of `off_t`. + +* Binary patches without data can now be parsed. + +* A configuration snapshot can now be created from another configuration + snapshot, not just a "true" configuration object. + +* The `git_commit_with_signature` API will now ensure that referenced + objects exist in the object database. + +* Stash messages containing newlines will now be replaced with spaces; + they will no longer be (erroneously) written to the repository. + +* `git_commit_create_with_signature` now verifies the commit information + to ensure that it points to a valid tree and valid parents. + +* `git_apply` has an option `GIT_APPLY_CHECK` that will only do a dry-run. + The index and working directory will remain unmodified, and application + will report if it would have worked. + +* Patches produced by Mercurial (those that lack some git extended headers) + can now be parsed and applied. + +* Reference locks are obeyed correctly on POSIX platforms, instead of + being removed. + +* Patches with empty new files can now be read and applied. + +* `git_apply_to_tree` can now correctly apply patches that add new files. + +* The program data configuration on Windows (`C:\ProgramData\Git\config`) + must be owned by an administrator, a system account or the current user + to be read. + +* `git_blob_filtered_content` is now deprecated in favor of `git_blob_filter`. + +* Configuration files can now be included conditionally using the + `onbranch` conditional. + +* Checkout can now properly create and remove symbolic links to directories + on Windows. + +* Stash no longer recomputes trees when committing a worktree, for + improved performance. + +* Repository templates can now include a `HEAD` file to default the + initial default branch. + +* Some configuration structures, enums and values have been renamed: + `git_cvar_map` is now `git_configmap`, `git_cvar_t` is now + `git_configmap_t`, `GIT_CVAR_FALSE` is now `GIT_CONFIGMAP_FALSE`, + `GIT_CVAR_TRUE` is now `GIT_CONFIGMAP_TRUE`, `GIT_CVAR_INT32` is now + `GIT_CONFIGMAP_INT32`, and `GIT_CVAR_STRING` is now `GIT_CONFIGMAP_STRING`. + The former names are deprecated. + +* Repositories can now be created at the root of a Windows drive. + +* Configuration lookups are now more efficiently cached. + +* `git_commit_create_with_signature` now supports a `NULL` signature, + which will create a commit without adding a signature. + +* When a repository lacks an `info` "common directory", we will no + longer erroneously return `GIT_ENOTFOUND` for all attribute lookups. + +* Several attribute macros have been renamed: `GIT_ATTR_TRUE` is now + `GIT_ATTR_IS_TRUE`, `GIT_ATTR_FALSE` is now `GIT_ATTR_IS_FALSE`, + `GIT_ATTR_UNSPECIFIED` is now `GIT_ATTR_IS_UNSPECIFIED`. The + attribute enum `git_attr_t` is now `git_attr_value_t` and its + values have been renamed: `GIT_ATTR_UNSPECIFIED_T` is now + `GIT_ATTR_VALUE_UNSPECIFIED`, `GIT_ATTR_TRUE_T` is now + `GIT_ATTR_VALUE_TRUE`, `GIT_ATTR_FALSE_T` is now `GIT_ATTR_VALUE_FALSE`, + and `GIT_ATTR_VALUE_T` is now `GIT_ATTR_VALUE_STRING`. The + former names are deprecated. + +* `git_object__size` is now `git_object_size`. The former name is + deprecated. + +* `git_tag_create_frombuffer` is now `git_tag_create_from_buffer`. The + former name is deprecated. + +* Several blob creation functions have been renamed: + `git_blob_create_frombuffer` is now named `git_blob_create_from_buffer`, + `git_blob_create_fromdisk` is now named `git_blob_create_from_disk`, + `git_blob_create_fromworkdir` is now named `git_blob_create_from_workdir`, + `git_blob_create_fromstream` is now named `git_blob_create_from_stream`, + and `git_blob_create_fromstream_commit` is now named + `git_blob_create_from_stream_commit`. The former names are deprecated. + +* The function `git_oid_iszero` is now named `git_oid_is_zero`. The + former name is deprecated. + +* Pattern matching is now done using `wildmatch` instead of `fnmatch` + for compatibility with git. + +* The option initialization functions suffixed by `init_options` are now + suffixed with `options_init`. (For example, `git_checkout_init_options` + is now `git_checkout_options_init`.) The former names are deprecated. + +* NTLM2 authentication is now supported on non-Windows platforms. + +* The `git_cred_sign_callback` callback is now named `git_cred_sign_cb`. + The `git_cred_ssh_interactive_callback` callback is now named + `git_cred_ssh_interactive_cb`. + +* Ignore files now: + + * honor escaped trailing whitespace. + * do not incorrectly negate sibling paths of a negated pattern. + * honor rules that stop ignoring files after a wildcard + +* Attribute files now: + + * honor leading and trailing whitespace. + * treat paths beginning with `\` as absolute only on Windows. + * properly handle escaped characters. + * stop reading macros defined in subdirectories + +* The C locale is now correctly used when parsing regular expressions. + +* The system PCRE2 or PCRE regular expression libraries are now used + when `regcomp_l` is not available on the system. If none of these + are available on the system, an included version of PCRE is used. + +* Wildcards in reference specifications are now supported beyond simply + a bare wildcard (`*`) for compatibility with git. + +* When `git_ignore_path_is_ignored` is provided a path with a trailing + slash (eg, `dir/`), it will now treat it as a directory for the + purposes of ignore matching. + +* Patches that add or remove a file with a space in the path can now + be correctly parsed. + +* The `git_remote_completion_type` type is now `git_remote_completion_t`. + The former name is deprecated. + +* The `git_odb_backend_malloc` is now `git_odb_backend_data_alloc`. The + former name is deprecated. + +* The `git_transfer_progress_cb` callback is now `git_indexer_progress_cb` + and the `git_transfer_progress` structure is now `git_indexer_progress`. + The former names are deprecated. + +* The example projects are now contained in a single `lg2` executable + for ease of use. + +* libgit2 now correctly handles more URLs, such as + `http://example.com:/repo.git` (colon but no port), + `http://example.com` (no path), + and `http://example.com:8080/` (path is /, nonstandard port). + +* A carefully constructed commit object with a very large number + of parents may lead to potential out-of-bounds writes or + potential denial of service. + +* The ProgramData configuration file is always read for compatibility + with Git for Windows and Portable Git installations. The ProgramData + location is not necessarily writable only by administrators, so we + now ensure that the configuration file is owned by the administrator + or the current user. + +### API additions + +* The SSH host key now supports SHA-256 when `GIT_CERT_SSH_SHA256` is set. + +* The diff format option `GIT_DIFF_FORMAT_PATCH_ID` can now be used to + emit an output like `git patch-id`. + +* The `git_apply_options_init` function will initialize a + `git_apply_options` structure. + +* The remote callbacks structure adds a `git_url_resolve_cb` callback + that is invoked when connecting to a server, so that applications + may edit or replace the URL before connection. + +* The information about the original `HEAD` in a rebase operation is + available with `git_rebase_orig_head_name`. Its ID is available with + `git_rebase_orig_head_id`. The `onto` reference name is available with + `git_rebase_onto_name` and its ID is available with `git_rebase_onto_id`. + +* ODB backends can now free backend data when an error occurs during its + backend data creation using `git_odb_backend_data_free`. + +* Options may be specified to `git_repository_foreach_head` to control + its behavior: `GIT_REPOSITORY_FOREACH_HEAD_SKIP_REPO` will not skip + the main repository's HEAD reference, while + `GIT_REPOSITORY_FOREACH_HEAD_SKIP_WORKTREES` will now skip the + worktree HEAD references. + +* The `GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS` option can be specified to + `git_libgit2_opts()` to avoid looking for `.keep` files that correspond + to packfiles. This setting can improve performance when packfiles are + stored on high-latency filesystems like network filesystems. + +* Blobs can now be filtered with `git_blob_filter`, which allows for + options to be set with `git_blob_filter_options`, including + `GIT_FILTER_NO_SYSTEM_ATTRIBUTES` to disable filtering with system-level + attributes in `/etc/gitattributes` and `GIT_ATTR_CHECK_INCLUDE_HEAD` to + enable filtering with `.gitattributes` files in the HEAD revision. + +### API removals + +* The unused `git_headlist_cb` function declaration was removed. + +* The unused `git_time_monotonic` API is removed. + +* The erroneously exported `inttypes.h` header was removed. + +# Security Fixes + +- CVE-2019-1348: the fast-import stream command "feature + export-marks=path" allows writing to arbitrary file paths. As + libgit2 does not offer any interface for fast-import, it is not + susceptible to this vulnerability. + +- CVE-2019-1349: by using NTFS 8.3 short names, backslashes or + alternate filesystreams, it is possible to cause submodules to + be written into pre-existing directories during a recursive + clone using git. As libgit2 rejects cloning into non-empty + directories by default, it is not susceptible to this + vulnerability. + +- CVE-2019-1350: recursive clones may lead to arbitrary remote + code executing due to improper quoting of command line + arguments. As libgit2 uses libssh2, which does not require us + to perform command line parsing, it is not susceptible to this + vulnerability. + +- CVE-2019-1351: Windows provides the ability to substitute + drive letters with arbitrary letters, including multi-byte + Unicode letters. To fix any potential issues arising from + interpreting such paths as relative paths, we have extended + detection of DOS drive prefixes to accomodate for such cases. + +- CVE-2019-1352: by using NTFS-style alternative file streams for + the ".git" directory, it is possible to overwrite parts of the + repository. While this has been fixed in the past for Windows, + the same vulnerability may also exist on other systems that + write to NTFS filesystems. We now reject any paths starting + with ".git:" on all systems. + +- CVE-2019-1353: by using NTFS-style 8.3 short names, it was + possible to write to the ".git" directory and thus overwrite + parts of the repository, leading to possible remote code + execution. While this problem was already fixed in the past for + Windows, other systems accessing NTFS filesystems are + vulnerable to this issue too. We now enable NTFS protecions by + default on all systems to fix this attack vector. + +- CVE-2019-1354: on Windows, backslashes are not a valid part of + a filename but are instead interpreted as directory separators. + As other platforms allowed to use such paths, it was possible + to write such invalid entries into a Git repository and was + thus an attack vector to write into the ".git" dierctory. We + now reject any entries starting with ".git\" on all systems. + +- CVE-2019-1387: it is possible to let a submodule's git + directory point into a sibling's submodule directory, which may + result in overwriting parts of the Git repository and thus lead + to arbitrary command execution. As libgit2 doesn't provide any + way to do submodule clones natively, it is not susceptible to + this vulnerability. Users of libgit2 that have implemented + recursive submodule clones manually are encouraged to review + their implementation for this vulnerability. + +### Breaking API changes + +* The "private" implementation details of the `git_cred` structure have been + moved to a dedicated `git2/sys/cred.h` header, to clarify that the underlying + structures are only provided for custom transport implementers. + The breaking change is that the `username` member of the underlying struct + is now hidden, and a new `git_cred_get_username` function has been provided. + +* Some errors of class `GIT_ERROR_NET` now have class `GIT_ERROR_HTTP`. + Most authentication failures now have error code `GIT_EAUTH` instead of `GIT_ERROR`. + +### Breaking CMake configuration changes + +* The CMake option to use a system http-parser library, instead of the + bundled dependency, has changed. This is due to a deficiency in + http-parser that we have fixed in our implementation. The bundled + library is now the default, but if you wish to force the use of the + system http-parser implementation despite incompatibilities, you can + specify `-DUSE_HTTP_PARSER=system` to CMake. + +* The interactions between `USE_HTTPS` and `SHA1_BACKEND` have been + streamlined. The detection was moved to a new `USE_SHA1`, modeled after + `USE_HTTPS`, which takes the values "CollisionDetection/Backend/Generic", to + better match how the "hashing backend" is selected, the default (ON) being + "CollisionDetection". If you were using `SHA1_BACKEND` previously, you'll + need to check the value you've used, or switch to the autodetection. + +### Authors + +The following individuals provided changes that were included in this +release: + +* Aaron Patterson +* Alberto Fanjul +* Anders Borum +* Augie Fackler +* Augustin Fabre +* Ayush Shridhar +* brian m. carlson +* buddyspike +* Carlos Martín Nieto +* cheese1 +* Dan Skorupski +* Daniel Cohen Gindi +* Dave Lee +* David Brooks +* David Turner +* Denis Laxalde +* Dhruva Krishnamurthy +* Dominik Ritter +* Drew DeVault +* Edward Thomson +* Eric Huss +* Erik Aigner +* Etienne Samson +* Gregory Herrero +* Heiko Voigt +* Ian Hattendorf +* Jacques Germishuys +* Janardhan Pulivarthi +* Jason Haslam +* Johannes Schindelin +* Jordan Wallet +* Josh Bleecher Snyder +* kas +* kdj0c +* Laurence McGlashan +* lhchavez +* Lukas Berk +* Max Kostyukevich +* Patrick Steinhardt +* pcpthm +* Remy Suen +* Robert Coup +* romkatv +* Scott Furry +* Sebastian Henke +* Stefan Widgren +* Steve King Jr +* Sven Strickroth +* Tobias Nießen +* Tyler Ang-Wanek +* Tyler Wanek + +--------------------------------------------------------------------- + +v0.28 +----- + +### Changes or improvements + +* The library is now always built with cdecl calling conventions on + Windows; the ability to build a stdcall library has been removed. + +* Reference log creation now honors `core.logallrefupdates=always`. + +* Fix some issues with the error-reporting in the OpenSSL backend. + +* HTTP proxy support is now builtin; libcurl is no longer used to support + proxies and is removed as a dependency. + +* Certificate and credential callbacks can now return `GIT_PASSTHROUGH` + to decline to act; libgit2 will behave as if there was no callback set + in the first place. + +* The line-ending filtering logic - when checking out files - has been + updated to match newer git (>= git 2.9) for proper interoperability. + +* Symbolic links are now supported on Windows when `core.symlinks` is set + to `true`. + +* Submodules with names which attempt to perform path traversal now have their + configuration ignored. Such names were blindly appended to the + `$GIT_DIR/modules` and a malicious name could lead to an attacker writing to + an arbitrary location. This matches git's handling of CVE-2018-11235. + +* Object validation is now performed during tree creation in the + `git_index_write_tree_to` API. + +* Configuration variable may now be specified on the same line as a section + header; previously this was erroneously a parser error. + +* When an HTTP server supports both NTLM and Negotiate authentication + mechanisms, we would previously fail to authenticate with any mechanism. + +* The `GIT_OPT_SET_PACK_MAX_OBJECTS` option can now set the maximum + number of objects allowed in a packfile being downloaded; this can help + limit the maximum memory used when fetching from an untrusted remote. + +* Line numbers in diffs loaded from patch files were not being populated; + they are now included in the results. + +* The repository's index is reloaded from disk at the beginning of + `git_merge` operations to ensure that it is up-to-date. + +* Mailmap handling APIs have been introduced, and the new commit APIs + `git_commit_committer_with_mailmap` and `git_commit_author_with_mailmap` + will use the mailmap to resolve the committer and author information. + In addition, blame will use the mailmap given when the + `GIT_BLAME_USE_MAILMAP` option. + +* Ignore handling for files in ignored folders would be ignored. + +* Worktrees can now be backed by bare repositories. + +* Trailing spaces are supported in `.gitignore` files, these spaces were + previously (and erroneously) treated as part of the pattern. + +* The library can now be built with mbedTLS support for HTTPS. + +* The diff status character 'T' will now be presented by the + `git_diff_status_char` API for diff entries that change type. + +* Revision walks previously would sometimes include commits that should + have been ignored; this is corrected. + +* Revision walks are now more efficient when the output is unsorted; + we now avoid walking all the way to the beginning of history unnecessarily. + +* Error-handling around index extension loading has been fixed. We were + previously always misreporting a truncated index (#4858). + +### API additions + +* The index may now be iterated atomically using `git_index_iterator`. + +* Remote objects can now be created with extended options using the + `git_remote_create_with_opts` API. + +* Diff objects can now be applied as changes to the working directory, + index or both, emulating the `git apply` command. Additionally, + `git_apply_to_tree` can apply those changes to a tree object as a + fully in-memory operation. + +* You can now swap out memory allocators via the + `GIT_OPT_SET_ALLOCATOR` option with `git_libgit2_opts()`. + +* You can now ensure that functions do not discard unwritten changes to the + index via the `GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY` option to + `git_libgit2_opts()`. This will cause functions that implicitly re-read + the index (eg, `git_checkout`) to fail if you have staged changes to the + index but you have not written the index to disk. (Unless the checkout + has the FORCE flag specified.) + + At present, this defaults to off, but we intend to enable this more + broadly in the future, as a warning or error. We encourage you to + examine your code to ensure that you are not relying on the current + behavior that implicitly removes staged changes. + +* Reference specifications can be parsed from an arbitrary string with + the `git_refspec_parse` API. + +* You can now get the name and path of worktrees using the + `git_worktree_name` and `git_worktree_path` APIs, respectively. + +* The `ref` field has been added to `git_worktree_add_options` to enable + the creation of a worktree from a pre-existing branch. + +* It's now possible to analyze merge relationships between any two + references, not just against `HEAD`, using `git_merge_analysis_for_ref`. + +### API removals + +* The `git_buf_free` API is deprecated; it has been renamed to + `git_buf_dispose` for consistency. The `git_buf_free` API will be + retained for backward compatibility for the foreseeable future. + +* The `git_otype` enumeration and its members are deprecated and have + been renamed for consistency. The `GIT_OBJ_` enumeration values are + now prefixed with `GIT_OBJECT_`. The old enumerations and macros + will be retained for backward compatibility for the foreseeable future. + +* Several index-related APIs have been renamed for consistency. The + `GIT_IDXENTRY_` enumeration values and macros have been renamed to + be prefixed with `GIT_INDEX_ENTRY_`. The `GIT_INDEXCAP` enumeration + values are now prefixed with `GIT_INDEX_CAPABILITY_`. The old + enumerations and macros will be retained for backward compatibility + for the foreseeable future. + +* The error functions and enumeration values have been renamed for + consistency. The `giterr_` functions and values prefix have been + renamed to be prefixed with `git_error_`; similarly, the `GITERR_` + constants have been renamed to be prefixed with `GIT_ERROR_`. + The old enumerations and macros will be retained for backward + compatibility for the foreseeable future. + +### Breaking API changes + +* The default checkout strategy changed from `DRY_RUN` to `SAFE` (#4531). + +* Adding a symlink as .gitmodules into the index from the workdir or checking + out such files is not allowed as this can make a Git implementation write + outside of the repository and bypass the fsck checks for CVE-2018-11235. + +--------------------------------------------------------------------- + +v0.27 +--------- + +### Changes or improvements + +* Improved `p_unlink` in `posix_w32.c` to try and make a file writable + before sleeping in the retry loop to prevent unnecessary calls to sleep. + +* The CMake build infrastructure has been improved to speed up building time. + +* A new CMake option "-DUSE_HTTPS=" makes it possible to explicitly + choose an HTTP backend. + +* A new CMake option "-DSHA1_BACKEND=" makes it possible to explicitly + choose an SHA1 backend. The collision-detecting backend is now the default. + +* A new CMake option "-DUSE_BUNDLED_ZLIB" makes it possible to explicitly use + the bundled zlib library. + +* A new CMake option "-DENABLE_REPRODUCIBLE_BUILDS" makes it possible to + generate a reproducible static archive. This requires support from your + toolchain. + +* The minimum required CMake version has been bumped to 2.8.11. + +* Writing to a configuration file now preserves the case of the key given by the + caller for the case-insensitive portions of the key (existing sections are + used even if they don't match). + +* We now support conditional includes in configuration files. + +* Fix for handling re-reading of configuration files with includes. + +* Fix for reading patches which contain exact renames only. + +* Fix for reading patches with whitespace in the compared files' paths. + +* We will now fill `FETCH_HEAD` from all passed refspecs instead of overwriting + with the last one. + +* There is a new diff option, `GIT_DIFF_INDENT_HEURISTIC` which activates a + heuristic which takes into account whitespace and indentation in order to + produce better diffs when dealing with ambiguous diff hunks. + +* Fix for pattern-based ignore rules where files ignored by a rule cannot be + un-ignored by another rule. + +* Sockets opened by libgit2 are now being closed on exec(3) if the platform + supports it. + +* Fix for peeling annotated tags from packed-refs files. + +* Fix reading huge loose objects from the object database. + +* Fix files not being treated as modified when only the file mode has changed. + +* We now explicitly reject adding submodules to the index via + `git_index_add_frombuffer`. + +* Fix handling of `GIT_DIFF_FIND_RENAMES_FROM_REWRITES` raising `SIGABRT` when + one file has been deleted and another file has been rewritten. + +* Fix for WinHTTP not properly handling NTLM and Negotiate challenges. + +* When using SSH-based transports, we now repeatedly ask for the passphrase to + decrypt the private key in case a wrong passphrase is being provided. + +* When generating conflict markers, they will now use the same line endings as + the rest of the file. + +### API additions + +* The `git_merge_file_options` structure now contains a new setting, + `marker_size`. This allows users to set the size of markers that + delineate the sides of merged files in the output conflict file. + By default this is 7 (`GIT_MERGE_CONFLICT_MARKER_SIZE`), which + produces output markers like `<<<<<<<` and `>>>>>>>`. + +* `git_remote_create_detached()` creates a remote that is not associated + to any repository (and does not apply configuration like 'insteadof' rules). + This is mostly useful for e.g. emulating `git ls-remote` behavior. + +* `git_diff_patchid()` lets you generate patch IDs for diffs. + +* `git_status_options` now has an additional field `baseline` to allow creating + status lists against different trees. + +* New family of functions to allow creating notes for a specific notes commit + instead of for a notes reference. + +* New family of functions to allow parsing message trailers. This API is still + experimental and may change in future releases. + +### API removals + +### Breaking API changes + +* Signatures now distinguish between +0000 and -0000 UTC offsets. + +* The certificate check callback in the WinHTTP transport will now receive the + `message_cb_payload` instead of the `cred_acquire_payload`. + +* We are now reading symlinked directories under .git/refs. + +* We now refuse creating branches named "HEAD". + +* We now refuse reading and writing all-zero object IDs into the + object database. + +* We now read the effective user's configuration file instead of the real user's + configuration in case libgit2 runs as part of a setuid binary. + +* The `git_odb_open_rstream` function and its `readstream` callback in the + `git_odb_backend` interface have changed their signatures to allow providing + the object's size and type to the caller. + +--------------------------------------------------------------------- + +v0.26 +----- + +### Changes or improvements + +* Support for opening, creating and modifying worktrees. + +* We can now detect SHA1 collisions resulting from the SHAttered attack. These + checks can be enabled at build time via `-DUSE_SHA1DC`. + +* Fix for missing implementation of `git_merge_driver_source` getters. + +* Fix for installed pkg-config file being broken when the prefix contains + spaces. + +* We now detect when the hashsum of on-disk objects does not match their + expected hashsum. + +* We now support open-ended ranges (e.g. "master..", "...master") in our + revision range parsing code. + +* We now correctly compute ignores with leading "/" in subdirectories. + +* We now optionally call `fsync` on loose objects, packfiles and their indexes, + loose references and packed reference files. + +* We can now build against OpenSSL v1.1 and against LibreSSL. + +* `GIT_MERGE_OPTIONS_INIT` now includes a setting to perform rename detection. + This aligns this structure with the default by `git_merge` and + `git_merge_trees` when `NULL` was provided for the options. + +* Improvements for reading index v4 files. + +* Perform additional retries for filesystem operations on Windows when files + are temporarily locked by other processes. + +### API additions + +* New family of functions to handle worktrees: + + * `git_worktree_list()` lets you look up worktrees for a repository. + * `git_worktree_lookup()` lets you get a specific worktree. + * `git_worktree_open_from_repository()` lets you get the associated worktree + of a repository. + a worktree. + * `git_worktree_add` lets you create new worktrees. + * `git_worktree_prune` lets you remove worktrees from disk. + * `git_worktree_lock()` and `git_worktree_unlock()` let you lock + respectively unlock a worktree. + * `git_repository_open_from_worktree()` lets you open a repository via + * `git_repository_head_for_worktree()` lets you get the current `HEAD` for a + linked worktree. + * `git_repository_head_detached_for_worktree()` lets you check whether a + linked worktree is in detached HEAD mode. + +* `git_repository_item_path()` lets you retrieve paths for various repository + files. + +* `git_repository_commondir()` lets you retrieve the common directory of a + repository. + +* `git_branch_is_checked_out()` allows you to check whether a branch is checked + out in a repository or any of its worktrees. + +* `git_repository_submodule_cache_all()` and + `git_repository_submodule_cache_clear()` functions allow you to prime or clear + the submodule cache of a repository. + +* You can disable strict hash verifications via the + `GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION` option with `git_libgit2_opts()`. + +* You can enable us calling `fsync` for various files inside the ".git" + directory by setting the `GIT_OPT_ENABLE_FSYNC_GITDIR` option with + `git_libgit2_opts()`. + +* You can now enable "offset deltas" when creating packfiles and negotiating + packfiles with a remote server by setting `GIT_OPT_ENABLE_OFS_DELTA` option + with `GIT_libgit2_opts()`. + +* You can now set the default share mode on Windows for opening files using + `GIT_OPT_SET_WINDOWS_SHAREMODE` option with `git_libgit2_opts()`. + You can query the current share mode with `GIT_OPT_GET_WINDOWS_SHAREMODE`. + +* `git_transport_smart_proxy_options()' enables you to get the proxy options for + smart transports. + +* The `GIT_FILTER_INIT` macro and the `git_filter_init` function are provided + to initialize a `git_filter` structure. + +### Breaking API changes + +* `clone_checkout_strategy` has been removed from + `git_submodule_update_option`. The checkout strategy used to clone will + be the same strategy specified in `checkout_opts`. + +v0.25 +------- + +### Changes or improvements + +* Fix repository discovery with `git_repository_discover` and + `git_repository_open_ext` to match git's handling of a ceiling + directory at the current directory. git only checks ceiling + directories when its search ascends to a parent directory. A ceiling + directory matching the starting directory will not prevent git from + finding a repository in the starting directory or a parent directory. + +* Do not fail when deleting remotes in the presence of broken + global configs which contain branches. + +* Support for reading and writing git index v4 files + +* Improve the performance of the revwalk and bring us closer to git's code. + +* The reference db has improved support for concurrency and returns `GIT_ELOCKED` + when an operation could not be performed due to locking. + +* Nanosecond resolution is now activated by default, following git's change to + do this. + +* We now restrict the set of ciphers we let OpenSSL use by default. + +* Users can now register their own merge drivers for use with `.gitattributes`. + The library also gained built-in support for the union merge driver. + +* The default for creating references is now to validate that the object does + exist. + +* Add `git_proxy_options` which is used by the different networking + implementations to let the caller specify the proxy settings instead of + relying on the environment variables. + +### API additions + +* You can now get the user-agent used by libgit2 using the + `GIT_OPT_GET_USER_AGENT` option with `git_libgit2_opts()`. + It is the counterpart to `GIT_OPT_SET_USER_AGENT`. + +* The `GIT_OPT_SET_SSL_CIPHERS` option for `git_libgit2_opts()` lets you specify + a custom list of ciphers to use for OpenSSL. + +* `git_commit_create_buffer()` creates a commit and writes it into a + user-provided buffer instead of writing it into the object db. Combine it with + `git_commit_create_with_signature()` in order to create a commit with a + cryptographic signature. + +* `git_blob_create_fromstream()` and + `git_blob_create_fromstream_commit()` allow you to create a blob by + writing into a stream. Useful when you do not know the final size or + want to copy the contents from another stream. + +* New flags for `git_repository_open_ext`: + + * `GIT_REPOSITORY_OPEN_NO_DOTGIT` - Do not check for a repository by + appending `/.git` to the `start_path`; only open the repository if + `start_path` itself points to the git directory. + * `GIT_REPOSITORY_OPEN_FROM_ENV` - Find and open a git repository, + respecting the environment variables used by the git command-line + tools. If set, `git_repository_open_ext` will ignore the other + flags and the `ceiling_dirs` argument, and will allow a NULL + `path` to use `GIT_DIR` or search from the current directory. The + search for a repository will respect `$GIT_CEILING_DIRECTORIES` + and `$GIT_DISCOVERY_ACROSS_FILESYSTEM`. The opened repository + will respect `$GIT_INDEX_FILE`, `$GIT_NAMESPACE`, + `$GIT_OBJECT_DIRECTORY`, and `$GIT_ALTERNATE_OBJECT_DIRECTORIES`. + In the future, this flag will also cause `git_repository_open_ext` + to respect `$GIT_WORK_TREE` and `$GIT_COMMON_DIR`; currently, + `git_repository_open_ext` with this flag will error out if either + `$GIT_WORK_TREE` or `$GIT_COMMON_DIR` is set. + +* `git_diff_from_buffer()` can create a `git_diff` object from the contents + of a git-style patch file. + +* `git_index_version()` and `git_index_set_version()` to get and set + the index version + +* `git_odb_expand_ids()` lets you check for the existence of multiple + objects at once. + +* The new `git_blob_dup()`, `git_commit_dup()`, `git_tag_dup()` and + `git_tree_dup()` functions provide type-specific wrappers for + `git_object_dup()` to reduce noise and increase type safety for callers. + +* `git_reference_dup()` lets you duplicate a reference to aid in ownership + management and cleanup. + +* `git_signature_from_buffer()` lets you create a signature from a string in the + format that appear in objects. + +* `git_tree_create_updated()` lets you create a tree based on another one + together with a list of updates. For the covered update cases, it's more + efficient than the `git_index` route. + +* `git_apply_patch()` applies hunks from a `git_patch` to a buffer. + +* `git_diff_to_buf()` lets you print an entire diff directory to a buffer, + similar to how `git_patch_to_buf()` works. + +* `git_proxy_init_options()` is added to initialize a `git_proxy_options` + structure at run-time. + +* `git_merge_driver_register()`, `git_merge_driver_unregister()` let you + register and unregister a custom merge driver to be used when `.gitattributes` + specifies it. + +* `git_merge_driver_lookup()` can be used to look up a merge driver by name. + +* `git_merge_driver_source_repo()`, `git_merge_driver_source_ancestor()`, + `git_merge_driver_source_ours()`, `git_merge_driver_source_theirs()`, + `git_merge_driver_source_file_options()` added as accessors to + `git_merge_driver_source`. + +### API removals + +* `git_blob_create_fromchunks()` has been removed in favour of + `git_blob_create_fromstream()`. + +### Breaking API changes + +* `git_packbuilder_object_count` and `git_packbuilder_written` now + return a `size_t` instead of a `uint32_t` for more thorough + compatibility with the rest of the library. + +* `git_packbuiler_progress` now provides explicitly sized `uint32_t` + values instead of `unsigned int`. + +* `git_diff_file` now includes an `id_abbrev` field that reflects the + number of nibbles set in the `id` field. + +* `git_odb_backend` now has a `freshen` function pointer. This optional + function pointer is similar to the `exists` function, but it will update + a last-used marker. For filesystem-based object databases, this updates + the timestamp of the file containing the object, to indicate "freshness". + If this is `NULL`, then it will not be called and the `exists` function + will be used instead. + +* `git_remote_connect()` now accepts `git_proxy_options` argument, and + `git_fetch_options` and `git_push_options` each have a `proxy_opts` field. + +* `git_merge_options` now provides a `default_driver` that can be used + to provide the name of a merge driver to be used to handle files changed + during a merge. + +--------------------------------------------------------------------- + +v0.24 +------- + +### Changes or improvements + +* Custom merge drivers can now be registered, which allows callers to + configure callbacks to honor `merge=driver` configuration in + `.gitattributes`. + +* Custom filters can now be registered with wildcard attributes, for + example `filter=*`. Consumers should examine the attributes parameter + of the `check` function for details. + +* Symlinks are now followed when locking a file, which can be + necessary when multiple worktrees share a base repository. + +* You can now set your own user-agent to be sent for HTTP requests by + using the `GIT_OPT_SET_USER_AGENT` with `git_libgit2_opts()`. + +* You can set custom HTTP header fields to be sent along with requests + by passing them in the fetch and push options. + +* Tree objects are now assumed to be sorted. If a tree is not + correctly formed, it will give bad results. This is the git approach + and cuts a significant amount of time when reading the trees. + +* Filter registration is now protected against concurrent + registration. + +* Filenames which are not valid on Windows in an index no longer cause + to fail to parse it on that OS. + +* Rebases can now be performed purely in-memory, without touching the + repository's workdir. + +* When adding objects to the index, or when creating new tree or commit + objects, the inputs are validated to ensure that the dependent objects + exist and are of the correct type. This object validation can be + disabled with the GIT_OPT_ENABLE_STRICT_OBJECT_CREATION option. + +* The WinHTTP transport's handling of bad credentials now behaves like + the others, asking for credentials again. + +### API additions + +* `git_config_lock()` has been added, which allow for + transactional/atomic complex updates to the configuration, removing + the opportunity for concurrent operations and not committing any + changes until the unlock. + +* `git_diff_options` added a new callback `progress_cb` to report on the + progress of the diff as files are being compared. The documentation of + the existing callback `notify_cb` was updated to reflect that it only + gets called when new deltas are added to the diff. + +* `git_fetch_options` and `git_push_options` have gained a `custom_headers` + field to set the extra HTTP header fields to send. + +* `git_stream_register_tls()` lets you register a callback to be used + as the constructor for a TLS stream instead of the libgit2 built-in + one. + +* `git_commit_header_field()` allows you to look up a specific header + field in a commit. + +* `git_commit_extract_signature()` extracts the signature from a + commit and gives you both the signature and the signed data so you + can verify it. + +### API removals + +* No APIs were removed in this version. + +### Breaking API changes + +* The `git_merge_tree_flag_t` is now `git_merge_flag_t`. Subsequently, + its members are no longer prefixed with `GIT_MERGE_TREE_FLAG` but are + now prefixed with `GIT_MERGE_FLAG`, and the `tree_flags` field of the + `git_merge_options` structure is now named `flags`. + +* The `git_merge_file_flags_t` enum is now `git_merge_file_flag_t` for + consistency with other enum type names. + +* `git_cert` descendent types now have a proper `parent` member + +* It is the responsibility of the refdb backend to decide what to do + with the reflog on ref deletion. The file-based backend must delete + it, a database-backed one may wish to archive it. + +* `git_config_backend` has gained two entries. `lock` and `unlock` + with which to implement the transactional/atomic semantics for the + configuration backend. + +* `git_index_add` and `git_index_conflict_add()` will now use the case + as provided by the caller on case insensitive systems. Previous + versions would keep the case as it existed in the index. This does + not affect the higher-level `git_index_add_bypath` or + `git_index_add_frombuffer` functions. + +* The `notify_payload` field of `git_diff_options` was renamed to `payload` + to reflect that it's also the payload for the new progress callback. + +* The `git_config_level_t` enum has gained a higher-priority value + `GIT_CONFIG_LEVEL_PROGRAMDATA` which represent a rough Windows equivalent + to the system level configuration. + +* `git_rebase_options` now has a `merge_options` field. + +* The index no longer performs locking itself. This is not something + users of the library should have been relying on as it's not part of + the concurrency guarantees. + +* `git_remote_connect()` now takes a `custom_headers` argument to set + the extra HTTP header fields to send. + +--------------------------------------------------------------------- + +v0.23 +------ + +### Changes or improvements + +* Patience and minimal diff drivers can now be used for merges. + +* Merges can now ignore whitespace changes. + +* Updated binary identification in CRLF filtering to avoid false positives in + UTF-8 files. + +* Rename and copy detection is enabled for small files. + +* Checkout can now handle an initial checkout of a repository, making + `GIT_CHECKOUT_SAFE_CREATE` unnecessary for users of clone. + +* The signature parameter in the ref-modifying functions has been + removed. Use `git_repository_set_ident()` and + `git_repository_ident()` to override the signature to be used. + +* The local transport now auto-scales the number of threads to use + when creating the packfile instead of sticking to one. + +* Reference renaming now uses the right id for the old value. + +* The annotated version of branch creation, HEAD detaching and reset + allow for specifying the expression from the user to be put into the + reflog. + +* `git_rebase_commit` now returns `GIT_EUNMERGED` when you attempt to + commit with unstaged changes. + +* On Mac OS X, we now use SecureTransport to provide the cryptographic + support for HTTPS connections insead of OpenSSL. + +* Checkout can now accept an index for the baseline computations via the + `baseline_index` member. + +* The configuration for fetching is no longer stored inside the + `git_remote` struct but has been moved to a `git_fetch_options`. The + remote functions now take these options or the callbacks instead of + setting them beforehand. + +* `git_submodule` instances are no longer cached or shared across + lookup. Each submodule represents the configuration at the time of + loading. + +* The index now uses diffs for `add_all()` and `update_all()` which + gives it a speed boost and closer semantics to git. + +* The ssh transport now reports the stderr output from the server as + the error message, which allows you to get the "repository not + found" messages. + +* `git_index_conflict_add()` will remove staged entries that exist for + conflicted paths. + +* The flags for a `git_diff_file` will now have the `GIT_DIFF_FLAG_EXISTS` + bit set when a file exists on that side of the diff. This is useful + for understanding whether a side of the diff exists in the presence of + a conflict. + +* The constructor for a write-stream into the odb now takes + `git_off_t` instead of `size_t` for the size of the blob, which + allows putting large files into the odb on 32-bit systems. + +* The remote's push and pull URLs now honor the url.$URL.insteadOf + configuration. This allows modifying URL prefixes to a custom + value via gitconfig. + +* `git_diff_foreach`, `git_diff_blobs`, `git_diff_blob_to_buffer`, + and `git_diff_buffers` now accept a new binary callback of type + `git_diff_binary_cb` that includes the binary diff information. + +* The race condition mitigations described in `racy-git.txt` have been + implemented. + +* If libcurl is installed, we will use it to connect to HTTP(S) + servers. + +### API additions + +* The `git_merge_options` gained a `file_flags` member. + +* Parsing and retrieving a configuration value as a path is exposed + via `git_config_parse_path()` and `git_config_get_path()` + respectively. + +* `git_repository_set_ident()` and `git_repository_ident()` serve to + set and query which identity will be used when writing to the + reflog. + +* `git_config_entry_free()` frees a config entry. + +* `git_config_get_string_buf()` provides a way to safely retrieve a + string from a non-snapshot configuration. + +* `git_annotated_commit_from_revspec()` allows to get an annotated + commit from an extended sha synatx string. + +* `git_repository_set_head_detached_from_annotated()`, + `git_branch_create_from_annotated()` and + `git_reset_from_annotated()` allow for the caller to provide an + annotated commit through which they can control what expression is + put into the reflog as the source/target. + +* `git_index_add_frombuffer()` can now create a blob from memory + buffer and add it to the index which is attached to a repository. + +* The structure `git_fetch_options` has been added to determine the + runtime configuration for fetching, such as callbacks, pruning and + autotag behaviour. It has the runtime initializer + `git_fetch_init_options()`. + +* The enum `git_fetch_prune_t` has been added, letting you specify the + pruning behaviour for a fetch. + +* A push operation will notify the caller of what updates it indends + to perform on the remote, which provides similar information to + git's pre-push hook. + +* `git_stash_apply()` can now apply a stashed state from the stash list, + placing the data into the working directory and index. + +* `git_stash_pop()` will apply a stashed state (like `git_stash_apply()`) + but will remove the stashed state after a successful application. + +* A new error code `GIT_EEOF` indicates an early EOF from the + server. This typically indicates an error with the URL or + configuration of the server, and tools can use this to show messages + about failing to communicate with the server. + +* A new error code `GIT_EINVALID` indicates that an argument to a + function is invalid, or an invalid operation was requested. + +* `git_diff_index_to_workdir()` and `git_diff_tree_to_index()` will now + produce deltas of type `GIT_DELTA_CONFLICTED` to indicate that the index + side of the delta is a conflict. + +* The `git_status` family of functions will now produce status of type + `GIT_STATUS_CONFLICTED` to indicate that a conflict exists for that file + in the index. + +* `git_index_entry_is_conflict()` is a utility function to determine if + a given index entry has a non-zero stage entry, indicating that it is + one side of a conflict. + +* It is now possible to pass a keypair via a buffer instead of a + path. For this, `GIT_CREDTYPE_SSH_MEMORY` and + `git_cred_ssh_key_memory_new()` have been added. + +* `git_filter_list_contains` will indicate whether a particular + filter will be run in the given filter list. + +* `git_commit_header_field()` has been added, which allows retrieving + the contents of an arbitrary header field. + +* `git_submodule_set_branch()` allows to set the configured branch for + a submodule. + +### API removals + +* `git_remote_save()` and `git_remote_clear_refspecs()` have been + removed. Remote's configuration is changed via the configuration + directly or through a convenience function which performs changes to + the configuration directly. + +* `git_remote_set_callbacks()`, `git_remote_get_callbacks()` and + `git_remote_set_transport()` have been removed and the remote no + longer stores this configuration. + +* `git_remote_set_fetch_refpecs()` and + `git_remote_set_push_refspecs()` have been removed. There is no + longer a way to set the base refspecs at run-time. + +* `git_submodule_save()` has been removed. The submodules are no + longer configured via the objects. + +* `git_submodule_reload_all()` has been removed as we no longer cache + submodules. + +### Breaking API changes + +* `git_smart_subtransport_cb` now has a `param` parameter. + +* The `git_merge_options` structure member `flags` has been renamed + to `tree_flags`. + +* The `git_merge_file_options` structure member `flags` is now + an unsigned int. It was previously a `git_merge_file_flags_t`. + +* `GIT_CHECKOUT_SAFE_CREATE` has been removed. Most users will generally + be able to switch to `GIT_CHECKOUT_SAFE`, but if you require missing + file handling during checkout, you may now use `GIT_CHECKOUT_SAFE | + GIT_CHECKOUT_RECREATE_MISSING`. + +* The `git_clone_options` and `git_submodule_update_options` + structures no longer have a `signature` field. + +* The following functions have removed the signature and/or log message + parameters in favour of git-emulating ones. + + * `git_branch_create()`, `git_branch_move()` + * `git_rebase_init()`, `git_rebase_abort()` + * `git_reference_symbolic_create_matching()`, + `git_reference_symbolic_create()`, `git_reference_create()`, + `git_reference_create_matching()`, + `git_reference_symbolic_set_target()`, + `git_reference_set_target()`, `git_reference_rename()` + * `git_remote_update_tips()`, `git_remote_fetch()`, `git_remote_push()` + * `git_repository_set_head()`, + `git_repository_set_head_detached()`, + `git_repository_detach_head()` + * `git_reset()` + +* `git_config_get_entry()` now gives back a ref-counted + `git_config_entry`. You must free it when you no longer need it. + +* `git_config_get_string()` will return an error if used on a + non-snapshot configuration, as there can be no guarantee that the + returned pointer is valid. + +* `git_note_default_ref()` now uses a `git_buf` to return the string, + as the string is otherwise not guaranteed to stay allocated. + +* `git_rebase_operation_current()` will return `GIT_REBASE_NO_OPERATION` + if it is called immediately after creating a rebase session but before + you have applied the first patch. + +* `git_rebase_options` now contains a `git_checkout_options` struct + that will be used for functions that modify the working directory, + namely `git_rebase_init`, `git_rebase_next` and + `git_rebase_abort`. As a result, `git_rebase_open` now also takes + a `git_rebase_options` and only the `git_rebase_init` and + `git_rebase_open` functions take a `git_rebase_options`, where they + will persist the options to subsequent `git_rebase` calls. + +* The `git_clone_options` struct now has fetch options in a + `fetch_opts` field instead of remote callbacks in + `remote_callbacks`. + +* The remote callbacks has gained a new member `push_negotiation` + which gets called before sending the update commands to the server. + +* The following functions no longer act on a remote instance but + change the repository's configuration. Their signatures have changed + accordingly: + + * `git_remote_set_url()`, `git_remote_seturl()` + * `git_remote_add_fetch()`, `git_remote_add_push()` and + * `git_remote_set_autotag()` + +* `git_remote_connect()` and `git_remote_prune()` now take a pointer + to the callbacks. + +* `git_remote_fetch()` and `git_remote_download()` now take a pointer + to fetch options which determine the runtime configuration. + +* The `git_remote_autotag_option_t` values have been changed. It has + gained a `_UNSPECIFIED` default value to specify no override for the + configured setting. + +* `git_remote_update_tips()` now takes a pointer to the callbacks as + well as a boolean whether to write `FETCH_HEAD` and the autotag + setting. + +* `git_remote_create_anonymous()` no longer takes a fetch refspec as + url-only remotes cannot have configured refspecs. + +* The `git_submodule_update_options` struct now has fetch options in + the `fetch_opts` field instead of callbacks in the + `remote_callbacks` field. + +* The following functions no longer act on a submodule instance but + change the repository's configuration. Their signatures have changed + accordingly: + + * `git_submodule_set_url()`, `git_submodule_set_ignore()`, + `git_submodule_set_update()`, + `git_submodule_set_fetch_recurse_submodules()`. + +* `git_submodule_status()` no longer takes a submodule instance but a + repsitory, a submodule name and an ignore setting. + +* The `push` function in the `git_transport` interface now takes a + pointer to the remote callbacks. + +* The `git_index_entry` struct's fields' types have been changed to + more accurately reflect what is in fact stored in the + index. Specifically, time and file size are 32 bits intead of 64, as + these values are truncated. + +* `GIT_EMERGECONFLICT` is now `GIT_ECONFLICT`, which more accurately + describes the nature of the error. + +* It is no longer allowed to call `git_buf_grow()` on buffers + borrowing the memory they point to. + +--------------------------------------------------------------------- + +v0.22 +------ + +### Changes or improvements + +* `git_signature_new()` now requires a non-empty email address. + +* Use CommonCrypto libraries for SHA-1 calculation on Mac OS X. + +* Disable SSL compression and SSLv2 and SSLv3 ciphers in favor of TLSv1 + in OpenSSL. + +* The fetch behavior of remotes with autotag set to `GIT_REMOTE_DOWNLOAD_TAGS_ALL` + has been changed to match git 1.9.0 and later. In this mode, libgit2 now + fetches all tags in addition to whatever else needs to be fetched. + +* `git_checkout()` now handles case-changing renames correctly on + case-insensitive filesystems; for example renaming "readme" to "README". + +* The search for libssh2 is now done via pkg-config instead of a + custom search of a few directories. + +* Add support for core.protectHFS and core.protectNTFS. Add more + validation for filenames which we write such as references. + +* The local transport now generates textual progress output like + git-upload-pack does ("counting objects"). + +* `git_checkout_index()` can now check out an in-memory index that is not + necessarily the repository's index, so you may check out an index + that was produced by git_merge and friends while retaining the cached + information. + +* Remove the default timeout for receiving / sending data over HTTP using + the WinHTTP transport layer. + +* Add SPNEGO (Kerberos) authentication using GSSAPI on Unix systems. + +* Provide built-in objects for the empty blob (e69de29) and empty + tree (4b825dc) objects. + +* The index' tree cache is now filled upon read-tree and write-tree + and the cache is written to disk. + +* LF -> CRLF filter refuses to handle mixed-EOL files + +* LF -> CRLF filter now runs when * text = auto (with Git for Windows 1.9.4) + +* File unlocks are atomic again via rename. Read-only files on Windows are + made read-write if necessary. + +* Share open packfiles across repositories to share descriptors and mmaps. + +* Use a map for the treebuilder, making insertion O(1) + +* The build system now accepts an option EMBED_SSH_PATH which when set + tells it to include a copy of libssh2 at the given location. This is + enabled for MSVC. + +* Add support for refspecs with the asterisk in the middle of a + pattern. + +* Fetching now performs opportunistic updates. To achieve this, we + introduce a difference between active and passive refspecs, which + make `git_remote_download()` and `git_remote_fetch()` to take a list of + resfpecs to be the active list, similarly to how git fetch accepts a + list on the command-line. + +* The THREADSAFE option to build libgit2 with threading support has + been flipped to be on by default. + +* The remote object has learnt to prune remote-tracking branches. If + the remote is configured to do so, this will happen via + `git_remote_fetch()`. You can also call `git_remote_prune()` after + connecting or fetching to perform the prune. + + +### API additions + +* Introduce `git_buf_text_is_binary()` and `git_buf_text_contains_nul()` for + consumers to perform binary detection on a git_buf. + +* `git_branch_upstream_remote()` has been introduced to provide the + branch..remote configuration value. + +* Introduce `git_describe_commit()` and `git_describe_workdir()` to provide + a description of the current commit (and working tree, respectively) + based on the nearest tag or reference + +* Introduce `git_merge_bases()` and the `git_oidarray` type to expose all + merge bases between two commits. + +* Introduce `git_merge_bases_many()` to expose all merge bases between + multiple commits. + +* Introduce rebase functionality (using the merge algorithm only). + Introduce `git_rebase_init()` to begin a new rebase session, + `git_rebase_open()` to open an in-progress rebase session, + `git_rebase_commit()` to commit the current rebase operation, + `git_rebase_next()` to apply the next rebase operation, + `git_rebase_abort()` to abort an in-progress rebase and `git_rebase_finish()` + to complete a rebase operation. + +* Introduce `git_note_author()` and `git_note_committer()` to get the author + and committer information on a `git_note`, respectively. + +* A factory function for ssh has been added which allows to change the + path of the programs to execute for receive-pack and upload-pack on + the server, `git_transport_ssh_with_paths()`. + +* The ssh transport supports asking the remote host for accepted + credential types as well as multiple challeges using a single + connection. This requires to know which username you want to connect + as, so this introduces the USERNAME credential type which the ssh + transport will use to ask for the username. + +* The `GIT_EPEEL` error code has been introduced when we cannot peel a tag + to the requested object type; if the given object otherwise cannot be + peeled, `GIT_EINVALIDSPEC` is returned. + +* Introduce `GIT_REPOSITORY_INIT_RELATIVE_GITLINK` to use relative paths + when writing gitlinks, as is used by git core for submodules. + +* `git_remote_prune()` has been added. See above for description. + + +* Introduce reference transactions, which allow multiple references to + be locked at the same time and updates be queued. This also allows + us to safely update a reflog with arbitrary contents, as we need to + do for stash. + +### API removals + +* `git_remote_supported_url()` and `git_remote_is_valid_url()` have been + removed as they have become essentially useless with rsync-style ssh paths. + +* `git_clone_into()` and `git_clone_local_into()` have been removed from the + public API in favour of `git_clone callbacks`. + +* The option to ignore certificate errors via `git_remote_cert_check()` + is no longer present. Instead, `git_remote_callbacks` has gained a new + entry which lets the user perform their own certificate checks. + +### Breaking API changes + +* `git_cherry_pick()` is now `git_cherrypick()`. + +* The `git_submodule_update()` function was renamed to + `git_submodule_update_strategy()`. `git_submodule_update()` is now used to + provide functionalty similar to "git submodule update". + +* `git_treebuilder_create()` was renamed to `git_treebuilder_new()` to better + reflect it being a constructor rather than something which writes to + disk. + +* `git_treebuilder_new()` (was `git_treebuilder_create()`) now takes a + repository so that it can query repository configuration. + Subsequently, `git_treebuilder_write()` no longer takes a repository. + +* `git_threads_init()` and `git_threads_shutdown()` have been renamed to + `git_libgit2_init()` and `git_libgit2_shutdown()` to better explain what + their purpose is, as it's grown to be more than just about threads. + +* `git_libgit2_init()` and `git_libgit2_shutdown()` now return the number of + initializations of the library, so consumers may schedule work on the + first initialization. + +* The `git_transport_register()` function no longer takes a priority and takes + a URL scheme name (eg "http") instead of a prefix like "http://" + +* `git_index_name_entrycount()` and `git_index_reuc_entrycount()` now + return size_t instead of unsigned int. + +* The `context_lines` and `interhunk_lines` fields in `git_diff`_options are + now `uint32_t` instead of `uint16_t`. This allows to set them to `UINT_MAX`, + in effect asking for "infinite" context e.g. to iterate over all the + unmodified lines of a diff. + +* `git_status_file()` now takes an exact path. Use `git_status_list_new()` if + pathspec searching is needed. + +* `git_note_create()` has changed the position of the notes reference + name to match `git_note_remove()`. + +* Rename `git_remote_load()` to `git_remote_lookup()` to bring it in line + with the rest of the lookup functions. + +* `git_remote_rename()` now takes the repository and the remote's + current name. Accepting a remote indicates we want to change it, + which we only did partially. It is much clearer if we accept a name + and no loaded objects are changed. + +* `git_remote_delete()` now accepts the repository and the remote's name + instead of a loaded remote. + +* `git_merge_head` is now `git_annotated_commit`, to better reflect its usage + for multiple functions (including rebase) + +* The `git_clone_options` struct no longer provides the `ignore_cert_errors` or + `remote_name` members for remote customization. + + Instead, the `git_clone_options` struct has two new members, `remote_cb` and + `remote_cb_payload`, which allow the caller to completely override the remote + creation process. If needed, the caller can use this callback to give their + remote a name other than the default (origin) or disable cert checking. + + The `remote_callbacks` member has been preserved for convenience, although it + is not used when a remote creation callback is supplied. + +* The `git_clone`_options struct now provides `repository_cb` and + `repository_cb_payload` to allow the user to create a repository with + custom options. + +* The `git_push` struct to perform a push has been replaced with + `git_remote_upload()`. The refspecs and options are passed as a + function argument. `git_push_update_tips()` is now also + `git_remote_update_tips()` and the callbacks are in the same struct as + the rest. + +* The `git_remote_set_transport()` function now sets a transport factory function, + rather than a pre-existing transport instance. + +* The `git_transport` structure definition has moved into the sys/transport.h + file. + +* libgit2 no longer automatically sets the OpenSSL locking + functions. This is not something which we can know to do. A + last-resort convenience function is provided in sys/openssl.h, + `git_openssl_set_locking()` which can be used to set the locking. + +* `git_reference_*()` functions use mmap() + binary search for packed + refs lookups when using the fs backend. Previously all entries were + read into a hashtable, which could be slow for repositories with a + large number of refs. diff --git a/docs/checkout-internals.md b/docs/checkout-internals.md index cb646da5d2d..e0b2583b54b 100644 --- a/docs/checkout-internals.md +++ b/docs/checkout-internals.md @@ -66,24 +66,26 @@ Key - Bi - ignored blob (WD only) - T1,T2,T3 - trees with different SHAs, - Ti - ignored tree (WD only) +- S1,S2 - submodules with different SHAs +- Sd - dirty submodule (WD only) - x - nothing Diff with 2 non-workdir iterators --------------------------------- - Old New - --- --- - 0 x x - nothing - 1 x B1 - added blob - 2 x T1 - added tree - 3 B1 x - removed blob - 4 B1 B1 - unmodified blob - 5 B1 B2 - modified blob - 6 B1 T1 - typechange blob -> tree - 7 T1 x - removed tree - 8 T1 B1 - typechange tree -> blob - 9 T1 T1 - unmodified tree - 10 T1 T2 - modified tree (implies modified/added/removed blob inside) +| | Old | New | | +|----|-----|-----|------------------------------------------------------------| +| 0 | x | x | nothing | +| 1 | x | B1 | added blob | +| 2 | x | T1 | added tree | +| 3 | B1 | x | removed blob | +| 4 | B1 | B1 | unmodified blob | +| 5 | B1 | B2 | modified blob | +| 6 | B1 | T1 | typechange blob -> tree | +| 7 | T1 | x | removed tree | +| 8 | T1 | B1 | typechange tree -> blob | +| 9 | T1 | T1 | unmodified tree | +| 10 | T1 | T2 | modified tree (implies modified/added/removed blob inside) | Now, let's make the "New" iterator into a working directory iterator, so @@ -92,23 +94,23 @@ we replace "added" items with either untracked or ignored, like this: Diff with non-work & workdir iterators -------------------------------------- - Old New-WD - --- ------ - 0 x x - nothing - 1 x B1 - untracked blob - 2 x Bi - ignored file - 3 x T1 - untracked tree - 4 x Ti - ignored tree - 5 B1 x - removed blob - 6 B1 B1 - unmodified blob - 7 B1 B2 - modified blob - 8 B1 T1 - typechange blob -> tree - 9 B1 Ti - removed blob AND ignored tree as separate items - 10 T1 x - removed tree - 11 T1 B1 - typechange tree -> blob - 12 T1 Bi - removed tree AND ignored blob as separate items - 13 T1 T1 - unmodified tree - 14 T1 T2 - modified tree (implies modified/added/removed blob inside) +| | Old | New | | +|----|-----|-----|------------------------------------------------------------| +| 0 | x | x | nothing | +| 1 | x | B1 | untracked blob | +| 2 | x | Bi | ignored file | +| 3 | x | T1 | untracked tree | +| 4 | x | Ti | ignored tree | +| 5 | B1 | x | removed blob | +| 6 | B1 | B1 | unmodified blob | +| 7 | B1 | B2 | modified blob | +| 8 | B1 | T1 | typechange blob -> tree | +| 9 | B1 | Ti | removed blob AND ignored tree as separate items | +| 10 | T1 | x | removed tree | +| 11 | T1 | B1 | typechange tree -> blob | +| 12 | T1 | Bi | removed tree AND ignored blob as separate items | +| 13 | T1 | T1 | unmodified tree | +| 14 | T1 | T2 | modified tree (implies modified/added/removed blob inside) | Note: if there is a corresponding entry in the old tree, then a working directory item won't be ignored (i.e. no Bi or Ti for tracked items). @@ -122,46 +124,68 @@ Checkout From 3 Iterators (2 not workdir, 1 workdir) (base == old HEAD; target == what to checkout; actual == working dir) - base target actual/workdir - ---- ------ ------ - 0 x x x - nothing - 1 x x B1/Bi/T1/Ti - untracked/ignored blob/tree (SAFE) - 2+ x B1 x - add blob (SAFE) - 3 x B1 B1 - independently added blob (FORCEABLE-2) - 4* x B1 B2/Bi/T1/Ti - add blob with content conflict (FORCEABLE-2) - 5+ x T1 x - add tree (SAFE) - 6* x T1 B1/Bi - add tree with blob conflict (FORCEABLE-2) - 7 x T1 T1/i - independently added tree (SAFE+MISSING) - 8 B1 x x - independently deleted blob (SAFE+MISSING) - 9- B1 x B1 - delete blob (SAFE) - 10- B1 x B2 - delete of modified blob (FORCEABLE-1) - 11 B1 x T1/Ti - independently deleted blob AND untrack/ign tree (SAFE+MISSING !!!) - 12 B1 B1 x - locally deleted blob (DIRTY || SAFE+CREATE) - 13+ B1 B2 x - update to deleted blob (SAFE+MISSING) - 14 B1 B1 B1 - unmodified file (SAFE) - 15 B1 B1 B2 - locally modified file (DIRTY) - 16+ B1 B2 B1 - update unmodified blob (SAFE) - 17 B1 B2 B2 - independently updated blob (FORCEABLE-1) - 18+ B1 B2 B3 - update to modified blob (FORCEABLE-1) - 19 B1 B1 T1/Ti - locally deleted blob AND untrack/ign tree (DIRTY) - 20* B1 B2 T1/Ti - update to deleted blob AND untrack/ign tree (F-1) - 21+ B1 T1 x - add tree with locally deleted blob (SAFE+MISSING) - 22* B1 T1 B1 - add tree AND deleted blob (SAFE) - 23* B1 T1 B2 - add tree with delete of modified blob (F-1) - 24 B1 T1 T1 - add tree with deleted blob (F-1) - 25 T1 x x - independently deleted tree (SAFE+MISSING) - 26 T1 x B1/Bi - independently deleted tree AND untrack/ign blob (F-1) - 27- T1 x T1 - deleted tree (MAYBE SAFE) - 28+ T1 B1 x - deleted tree AND added blob (SAFE+MISSING) - 29 T1 B1 B1 - independently typechanged tree -> blob (F-1) - 30+ T1 B1 B2 - typechange tree->blob with conflicting blob (F-1) - 31* T1 B1 T1/T2 - typechange tree->blob (MAYBE SAFE) - 32+ T1 T1 x - restore locally deleted tree (SAFE+MISSING) - 33 T1 T1 B1/Bi - locally typechange tree->untrack/ign blob (DIRTY) - 34 T1 T1 T1/T2 - unmodified tree (MAYBE SAFE) - 35+ T1 T2 x - update locally deleted tree (SAFE+MISSING) - 36* T1 T2 B1/Bi - update to tree with typechanged tree->blob conflict (F-1) - 37 T1 T2 T1/T2/T3 - update to existing tree (MAYBE SAFE) +| |base | target | actual/workdir | | +|-----|-----|------- |----------------|--------------------------------------------------------------------| +| 0 | x | x | x | nothing | +| 1 | x | x | B1/Bi/T1/Ti | untracked/ignored blob/tree (SAFE) | +| 2+ | x | B1 | x | add blob (SAFE) | +| 3 | x | B1 | B1 | independently added blob (FORCEABLE-2) | +| 4* | x | B1 | B2/Bi/T1/Ti | add blob with content conflict (FORCEABLE-2) | +| 5+ | x | T1 | x | add tree (SAFE) | +| 6* | x | T1 | B1/Bi | add tree with blob conflict (FORCEABLE-2) | +| 7 | x | T1 | T1/i | independently added tree (SAFE+MISSING) | +| 8 | B1 | x | x | independently deleted blob (SAFE+MISSING) | +| 9- | B1 | x | B1 | delete blob (SAFE) | +| 10- | B1 | x | B2 | delete of modified blob (FORCEABLE-1) | +| 11 | B1 | x | T1/Ti | independently deleted blob AND untrack/ign tree (SAFE+MISSING !!!) | +| 12 | B1 | B1 | x | locally deleted blob (DIRTY || SAFE+CREATE) | +| 13+ | B1 | B2 | x | update to deleted blob (SAFE+MISSING) | +| 14 | B1 | B1 | B1 | unmodified file (SAFE) | +| 15 | B1 | B1 | B2 | locally modified file (DIRTY) | +| 16+ | B1 | B2 | B1 | update unmodified blob (SAFE) | +| 17 | B1 | B2 | B2 | independently updated blob (FORCEABLE-1) | +| 18+ | B1 | B2 | B3 | update to modified blob (FORCEABLE-1) | +| 19 | B1 | B1 | T1/Ti | locally deleted blob AND untrack/ign tree (DIRTY) | +| 20* | B1 | B2 | T1/Ti | update to deleted blob AND untrack/ign tree (F-1) | +| 21+ | B1 | T1 | x | add tree with locally deleted blob (SAFE+MISSING) | +| 22* | B1 | T1 | B1 | add tree AND deleted blob (SAFE) | +| 23* | B1 | T1 | B2 | add tree with delete of modified blob (F-1) | +| 24 | B1 | T1 | T1 | add tree with deleted blob (F-1) | +| 25 | T1 | x | x | independently deleted tree (SAFE+MISSING) | +| 26 | T1 | x | B1/Bi | independently deleted tree AND untrack/ign blob (F-1) | +| 27- | T1 | x | T1 | deleted tree (MAYBE SAFE) | +| 28+ | T1 | B1 | x | deleted tree AND added blob (SAFE+MISSING) | +| 29 | T1 | B1 | B1 | independently typechanged tree -> blob (F-1) | +| 30+ | T1 | B1 | B2 | typechange tree->blob with conflicting blob (F-1) | +| 31* | T1 | B1 | T1/T2 | typechange tree->blob (MAYBE SAFE) | +| 32+ | T1 | T1 | x | restore locally deleted tree (SAFE+MISSING) | +| 33 | T1 | T1 | B1/Bi | locally typechange tree->untrack/ign blob (DIRTY) | +| 34 | T1 | T1 | T1/T2 | unmodified tree (MAYBE SAFE) | +| 35+ | T1 | T2 | x | update locally deleted tree (SAFE+MISSING) | +| 36* | T1 | T2 | B1/Bi | update to tree with typechanged tree->blob conflict (F-1) | +| 37 | T1 | T2 | T1/T2/T3 | update to existing tree (MAYBE SAFE) | +| 38+ | x | S1 | x | add submodule (SAFE) | +| 39 | x | S1 | S1/Sd | independently added submodule (SUBMODULE) | +| 40* | x | S1 | B1 | add submodule with blob confilct (FORCEABLE) | +| 41* | x | S1 | T1 | add submodule with tree conflict (FORCEABLE) | +| 42 | S1 | x | S1/Sd | deleted submodule (SUBMODULE) | +| 43 | S1 | x | x | independently deleted submodule (SUBMODULE) | +| 44 | S1 | x | B1 | independently deleted submodule with added blob (SAFE+MISSING) | +| 45 | S1 | x | T1 | independently deleted submodule with added tree (SAFE+MISSING) | +| 46 | S1 | S1 | x | locally deleted submodule (SUBMODULE) | +| 47+ | S1 | S2 | x | update locally deleted submodule (SAFE) | +| 48 | S1 | S1 | S2 | locally updated submodule commit (SUBMODULE) | +| 49 | S1 | S2 | S1 | updated submodule commit (SUBMODULE) | +| 50+ | S1 | B1 | x | add blob with locally deleted submodule (SAFE+MISSING) | +| 51* | S1 | B1 | S1 | typechange submodule->blob (SAFE) | +| 52* | S1 | B1 | Sd | typechange dirty submodule->blob (SAFE!?!?) | +| 53+ | S1 | T1 | x | add tree with locally deleted submodule (SAFE+MISSING) | +| 54* | S1 | T1 | S1/Sd | typechange submodule->tree (MAYBE SAFE) | +| 55+ | B1 | S1 | x | add submodule with locally deleted blob (SAFE+MISSING) | +| 56* | B1 | S1 | B1 | typechange blob->submodule (SAFE) | +| 57+ | T1 | S1 | x | add submodule with locally deleted tree (SAFE+MISSING) | +| 58* | T1 | S1 | T1 | typechange tree->submodule (SAFE) | + The number is followed by ' ' if no change is needed or '+' if the case needs to write to disk or '-' if something must be deleted and '*' if @@ -169,35 +193,39 @@ there should be a delete followed by an write. There are four tiers of safe cases: -- SAFE == completely safe to update -- SAFE+MISSING == safe except the workdir is missing the expect content -- MAYBE SAFE == safe if workdir tree matches (or is missing) baseline +* SAFE == completely safe to update +* SAFE+MISSING == safe except the workdir is missing the expect content +* MAYBE SAFE == safe if workdir tree matches (or is missing) baseline content, which is unknown at this point -- FORCEABLE == conflict unless FORCE is given -- DIRTY == no conflict but change is not applied unless FORCE +* FORCEABLE == conflict unless FORCE is given +* DIRTY == no conflict but change is not applied unless FORCE +* SUBMODULE == no conflict and no change is applied unless a deleted + submodule dir is empty Some slightly unusual circumstances: - 8 - parent dir is only deleted when file is, so parent will be left if - empty even though it would be deleted if the file were present - 11 - core git does not consider this a conflict but attempts to delete T1 - and gives "unable to unlink file" error yet does not skip the rest - of the operation - 12 - without FORCE file is left deleted (i.e. not restored) so new wd is - dirty (and warning message "D file" is printed), with FORCE, file is - restored. - 24 - This should be considered MAYBE SAFE since effectively it is 7 and 8 - combined, but core git considers this a conflict unless forced. - 26 - This combines two cases (1 & 25) (and also implied 8 for tree content) - which are ok on their own, but core git treat this as a conflict. - If not forced, this is a conflict. If forced, this actually doesn't - have to write anything and leaves the new blob as an untracked file. - 32 - This is the only case where the baseline and target values match - and yet we will still write to the working directory. In all other - cases, if baseline == target, we don't touch the workdir (it is - either already right or is "dirty"). However, since this case also - implies that a ?/B1/x case will exist as well, it can be skipped. +* 8 - parent dir is only deleted when file is, so parent will be left if + empty even though it would be deleted if the file were present +* 11 - core git does not consider this a conflict but attempts to delete T1 + and gives "unable to unlink file" error yet does not skip the rest + of the operation +* 12 - without FORCE file is left deleted (i.e. not restored) so new wd is + dirty (and warning message "D file" is printed), with FORCE, file is + restored. +* 24 - This should be considered MAYBE SAFE since effectively it is 7 and 8 + combined, but core git considers this a conflict unless forced. +* 26 - This combines two cases (1 & 25) (and also implied 8 for tree content) + which are ok on their own, but core git treat this as a conflict. + If not forced, this is a conflict. If forced, this actually doesn't + have to write anything and leaves the new blob as an untracked file. +* 32 - This is the only case where the baseline and target values match + and yet we will still write to the working directory. In all other + cases, if baseline == target, we don't touch the workdir (it is + either already right or is "dirty"). However, since this case also + implies that a ?/B1/x case will exist as well, it can be skipped. +* 41 - It's not clear how core git distinguishes this case from 39 (mode?). +* 52 - Core git makes destructive changes without any warning when the + submodule is dirty and the type changes to a blob. Cases 3, 17, 24, 26, and 29 are all considered conflicts even though none of them will require making any updates to the working directory. - diff --git a/docs/code_of_conduct.md b/docs/code_of_conduct.md new file mode 100644 index 00000000000..0a0e4ebab94 --- /dev/null +++ b/docs/code_of_conduct.md @@ -0,0 +1,75 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and +orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or +advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at [libgit2@gmail.com][email]. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at [http://contributor-covenant.org/version/1/4][version] + +[email]: mailto:libgit2@gmail.com +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/docs/coding-style.md b/docs/coding-style.md new file mode 100644 index 00000000000..b8b94d69ca8 --- /dev/null +++ b/docs/coding-style.md @@ -0,0 +1,364 @@ +# libgit2 Coding Style + +This documentation describes the preferred coding style for the libgit2 project. +While not all parts of our code base conform to this coding style, the outlined +rules are what we aim for. + +Note that in no case do we accept changes that convert huge parts of the code +base to use our coding style. Instead, it is encouraged to modernize small parts +of code you're going to modify anyway for a given change you want to introduce. +A good rule to follow is the Boy Scout Rule: "Leave the campground cleaner than +you found it." + +## C Coding Style + +The following sections define the coding style for all code files and headers. + +### Indentation and Alignment + +Code is indented by tabs, where a tab is 8 spaces. Each opening scope increases +the indentation level. + +```c +int foobar(int void) +{ + if (condition) + doit(); + /* Body */ +} +``` + +Switch statements have their `case`s aligned with the `switch` keyword. Case +bodies are indented by an additional level. Case bodies should not open their +own scope to declare variables. + +```c +switch (c) { +case 'a': +case 'b': + return 0; +default: + return -1; +} +``` + +Multi-line conditions should be aligned with the opening brace of the current +statement: + +```c +if (one_very_long_condition(c) && + another_very_long_condition(c)) + doit(); +``` + +### Spaces + +There must be no space between the function and its arguments, arguments must be +separated by a space: + +```c +int doit(int first_arg, int second_arg); +doit(1, 2); +``` + +For any binary or ternary operators, the arguments and separator must be +separated by a space: + +```c +1 + 2; +x ? x : NULL; +``` + +Unary operators do not have a space between them and the argument they refer to: + +```c +*c +&c +``` + +The `sizeof` operator always must not have a space and must use braces around +the type: + +``` +sizeof(int) +``` + +There must be a space after the keywords `if`, `switch`, `case`, `do` and +`while`. + +### Braces + +Functions must have their opening brace on the following line: + +```c +void foobar(void) +{ + doit(); +} +``` + +For conditions, braces should be placed on the same line as the condition: + +```c +if (condition(c)) { + doit(); + dothat(); +} + +while (true) { + doit(); +} +``` + +In case a condition's body has a single line, only, it's allowed to omit braces, +except if any of its `else if` or `else` branches has more than one line: + +```c +if (condition(c)) + doit(); + +if (condition(c)) + doit(); +else if (other_condition(c)) + doit(); + +/* This example must use braces as the `else if` requires them. */ +if (condition(c)) { + doit(); +} else if (other_condition(c)) { + doit(); + dothat(); +} else { + abort(); +} +``` + +### Comments + +Comments must use C-style `/* */` comments. C++-style `// `comments are not +allowed in our codebase. This is a strict requirement as libgit2 tries to be +compliant with the ISO C90 standard, which only allows C-style comments. + +Single-line comments may have their opening and closing tag on the same line: + +```c +/* This is a short comment. */ +``` + +For multi-line comments, the opening and closing tag should be empty: + +```c +/* + * This is a rather long and potentially really unwiedly but informative + * multiline comment that helps quite a lot. + */ +``` + +Public functions must have documentation that explain their usage, internal +functions should have a comment. We use Docurium to generate documentation +derived from these comments, which uses syntax similar to Doxygen. The first +line should be a short summary of what the function does. More in-depth +explanation should be separated from that first line by an empty line. +Parameters and return values should be documented via `@return` and `@param` +tags: + +```c +/* + * Froznicate the string. + * + * Froznicate the string by foobaring its internal structure into a more obvious + * translation. Note that the returned string is a newly allocated string that + * shall be `free`d by the caller. + * + * @param s String to froznicate + * @return A newly allocated string or `NULL` in case an error occurred. + */ +char *froznicate(const char *s); +``` + +### Variables + +Variables must be declared at the beginning of their scope. This is a strict +requirement as libgit2 tries to be compliant with the ISO C90 standard, which +forbids mixed declarations and code: + +```c +void foobar(void) +{ + char *c = NULL; + int a, b; + + a = 0; + b = 1; + + return c; +} +``` + +### Naming + +Variables must have all-lowercase names. In case a variable name has multiple +words, words should be separated by an underscore `_` character. While +recommended to use descriptive naming, common variable names like `i` for +indices are allowed. + +All public functions must have a `git` prefix as well as a prefix indicating +their respective subsystem. E.g. a function that opens a repository should be +called `git_repository_open()`. Functions that are not public but declared in +an internal header file for use by other subsystems should follow the same +naming pattern. File-local static functions must not have a `git` prefix, but +should have a prefix indicating their respective subsystem. + +All structures declared in the libgit2 project must have a `typedef`, we do not +use `struct type` variables. Type names follow the same schema as functions. + +### Error Handling + +The libgit2 project mostly uses error codes to indicate errors. Error codes are +always of type `int`, where `0` indicates success and a negative error code +indicates an error case. In some cases, positive error codes may be used to +indicate special cases. Returned values that are not an error code should be +returned via an out parameter. Out parameters must always come first in the list +of arguments. + +```c +int doit(const char **out, int arg) +{ + if (!arg) + return -1; + *out = "Got an argument"; + return 0; +} +``` + +To avoid repetitive and fragile error handling in case a function has resources +that need to be free'd, we use `goto out`s: + +```c +int doit(char **out, int arg) +{ + int error = 0; + char *c; + + c = malloc(strlen("Got an argument") + 1); + if (!c) { + error = -1; + goto out; + } + + if (!arg) { + error = -1; + goto out; + } + + strcpy(c, "Got an argument") + *out = c; + +out: + if (error) + free(c); + return error; +} +``` + +When calling functions that return an error code, you should assign the error +code to an `error` variable and, in case an error case is indicated and no +custom error handling is required, return that error code: + +```c +int foobar(void) +{ + int error; + + if ((error = doit()) < 0) + return error; + + return 0; +} +``` + +When doing multiple function calls where all of the functions return an error +code, it's common practice to chain these calls together: + +```c +int doit(void) +{ + int error; + + if ((error = dothis()) < 0 || + (error = dothat()) < 0) + return error; + + return 0; +} +``` + +## CMake Coding Style + +The following section defines the coding style for our CMake build system. + +### Indentation + +Code is indented by tabs, where a tab is 8 spaces. Each opening scope increases +the indentation level. + +```cmake +if(CONDITION) + doit() +endif() +``` + +### Spaces + +There must be no space between keywords and their opening brace. While this is +the same as in our C codebase for function calls, this also applies to +conditional keywords. This is done to avoid the awkward-looking `else ()` +statement. + +```cmake +if(CONDITION) + doit() +else() + dothat() +endif() +``` + +### Case + +While CMake is completely case-insensitive when it comes to function calls, we +want to agree on a common coding style for this. To reduce the danger of +repetitive strain injuries, all function calls should be lower-case (NB: this is +not currently the case yet, but introduced as a new coding style by this +document). + +Variables are written all-uppercase. In contrast to functions, variables are +case-sensitive in CMake. As CMake itself uses upper-case variables in all +places, we should follow suit and do the same. + +Control flow keywords must be all lowercase. In contrast to that, test keywords +must be all uppercase: + +```cmake +if(NOT CONDITION) + doit() +elseif(FOO AND BAR) + dothat() +endif() +``` + +### Targets + +CMake code should not use functions that modify the global scope but prefer +their targeted equivalents, instead. E.g. instead of using +`include_directories()`, you must use `target_include_directories()`. An +exception to this rule is setting up global compiler flags like warnings or +flags required to set up the build type. + +### Dependencies + +Dependencies should not be discovered or set up in the main "CMakeLists.txt" +module. Instead, they should either have their own module in our top-level +"cmake/" directory or have a "CMakeLists.txt" in their respective "deps/" +directory in case it is a vendored library. All dependencies should expose +interface library targets that can be linked against with +`target_link_libraries()`. diff --git a/docs/contributing.md b/docs/contributing.md new file mode 100644 index 00000000000..b39eda1d1d7 --- /dev/null +++ b/docs/contributing.md @@ -0,0 +1,183 @@ +# Welcome to libgit2! + +We're making it easy to do interesting things with git, and we'd love to have +your help. + +## Licensing + +By contributing to libgit2, you agree to release your contribution under +the terms of the license. Except for the `examples` and the +`deps` directories, all code is released under the [GPL v2 with +linking exception](../COPYING). + +The `examples` code is governed by the +[CC0 Public Domain Dedication](../examples/COPYING), so that you may copy +from them into your own application. + +The bundled dependencies in the `deps` directories are governed +by the following licenses: + +- http-parser is licensed under [MIT license](../deps/http-parser/COPYING) +- pcre is governed by [BSD license](../deps/pcre/LICENCE) +- winhttp is governed by [LGPL v2.1+](../deps/winhttp/COPYING.LGPL) and [GPL v2 with linking exception](../deps/winhttp/COPYING.GPL) +- zlib is governed by [zlib license](../deps/zlib/COPYING) + +## Discussion & Chat + +We hang out in the [#libgit2](https://web.libera.chat/#libgit2) channel on +[libera](https://libera.chat). + +Also, feel free to open an +[Issue](https://github.com/libgit2/libgit2/issues/new) to start a discussion +about any concerns you have. We like to use Issues for that so there is an +easily accessible permanent record of the conversation. + +## Libgit2 Versions + +The `main` branch is the main branch where development happens. +Releases are tagged +(e.g. [v0.21.0](https://github.com/libgit2/libgit2/releases/tag/v0.21.0) ) +and when a critical bug fix needs to be backported, it will be done on a +`-maint` maintenance branch. + +## Reporting Bugs + +First, know which version of libgit2 your problem is in and include it in +your bug report. This can either be a tag (e.g. +[v0.17.0](https://github.com/libgit2/libgit2/releases/tag/v0.17.0)) or a +commit SHA +(e.g. [01be7863](https://github.com/libgit2/libgit2/commit/01be7863)). +Using [`git describe`](http://git-scm.com/docs/git-describe) is a +great way to tell us what version you're working with. + +If you're not running against the latest `main` branch version, +please compile and test against that to avoid re-reporting an issue that's +already been fixed. + +It's *incredibly* helpful to be able to reproduce the problem. Please +include a list of steps, a bit of code, and/or a zipped repository (if +possible). Note that some of the libgit2 developers are employees of +GitHub, so if your repository is private, find us on IRC and we'll figure +out a way to help you. + +## Pull Requests + +Our work flow is a [typical GitHub +flow](https://guides.github.com/introduction/flow/index.html), where +contributors fork the [libgit2 repository](https://github.com/libgit2/libgit2), +make their changes on branch, and submit a +[Pull Request](https://help.github.com/articles/using-pull-requests) +(a.k.a. "PR"). Pull requests should usually be targeted at the `main` +branch. + +Life will be a lot easier for you (and us) if you follow this pattern +(i.e. fork, named branch, submit PR). If you use your fork's `main` +branch directly, things can get messy. + +Please include a nice description of your changes when you submit your PR; +if we have to read the whole diff to figure out why you're contributing +in the first place, you're less likely to get feedback and have your change +merged in. + +In addition to outlining your thought process in the PR's description, please +also try to document it in your commits. We welcome it if every commit has a +description of why you have been doing your changes alongside with your +reasoning why this is a good idea. The messages shall be written in +present-tense and in an imperative style (e.g. "Add feature foobar", not "Added +feature foobar" or "Adding feature foobar"). Lines should be wrapped at 80 +characters so people with small screens are able to read the commit messages in +their terminal without any problem. + +To make it easier to attribute commits to certain parts of our code base, we +also prefer to have the commit subject be prefixed with a "scope". E.g. if you +are changing code in our merging subsystem, make sure to prefix the subject with +"merge:". The first word following the colon shall start with an lowercase +letter. The maximum line length for the subject is 70 characters, preferably +shorter. + +If you are starting to work on a particular area, feel free to submit a PR +that highlights your work in progress (and note in the PR title that it's +not ready to merge). These early PRs are welcome and will help in getting +visibility for your fix, allow others to comment early on the changes and +also let others know that you are currently working on something. + +Before wrapping up a PR, you should be sure to: + +* Write tests to cover any functional changes +* Update documentation for any changed public APIs +* Add to the [`changelog.md`](changelog.md) file describing any major changes + +## Unit Tests + +We believe that our unit tests allow us to keep the quality of libgit2 +high: any new changes must not cause unit test failures, and new changes +should include unit tests that cover the bug fixes or new features. +For bug fixes, we prefer unit tests that illustrate the failure before +the change, but pass with your changes. + +In addition to new tests, please ensure that your changes do not cause +any other test failures. Running the entire test suite is helpful +before you submit a pull request. When you build libgit2, the test +suite will also be built. You can run most of the tests by simply running +the resultant `libgit2_tests` binary. If you want to run a specific +unit test, you can name it with the `-s` option. For example: + + libgit2_tests -sstatus::worktree::long_filenames + +Or you can run an entire class of tests. For example, to run all the +worktree status tests: + + libgit2_tests -sstatus::worktree + +The default test run is fairly exhaustive, but it will exclude some +unit tests by default: in particular, those that talk to network +servers and the tests that manipulate the filesystem in onerous +ways (and may need to have special privileges to run). To run the +network tests: + + libgit2_tests -ionline + +In addition, various tests may be enabled by environment variables, +like the ones that write exceptionally large repositories or manipulate +the filesystem structure in unexpected ways. These tests *may be +dangerous* to run on a normal machine and may harm your filesystem. It's +not recommended that you run these; instead, the continuous integration +servers will run these (in a sandbox). + +## Porting Code From Other Open-Source Projects + +`libgit2` is licensed under the terms of the GPL v2 with a linking +exception. Any code brought in must be compatible with those terms. + +The most common case is porting code from core Git. Git is a pure GPL +project, which means that in order to port code to this project, we need the +explicit permission of the author. Check the +[`git.git-authors`](https://github.com/libgit2/libgit2/blob/main/git.git-authors) +file for authors who have already consented. + +Other licenses have other requirements; check the license of the library +you're porting code *from* to see what you need to do. As a general rule, +MIT and BSD (3-clause) licenses are typically no problem. Apache 2.0 +license typically doesn't work due to GPL incompatibility. + +If your pull request uses code from core Git, another project, or code +from a forum / Stack Overflow, then *please* flag this in your PR and make +sure you've given proper credit to the original author in the code +snippet. + +## Style Guide + +The public API of `libgit2` is [ANSI C](http://en.wikipedia.org/wiki/ANSI_C) +(a.k.a. C89) compatible. Internally, `libgit2` is written using a portable +subset of C99 - in order to compile with GCC, Clang, MSVC, etc., we keep +local variable declarations at the tops of blocks only and avoid `//` style +comments. Additionally, `libgit2` follows some extra conventions for +function and type naming, code formatting, and testing. + +We like to keep the source code consistent and easy to read. Maintaining +this takes some discipline, but it's been more than worth it. Take a look +at the [conventions file](conventions.md). + +## Starter Projects + +See our [projects list](projects.md). diff --git a/docs/conventions.md b/docs/conventions.md new file mode 100644 index 00000000000..a017db11c2c --- /dev/null +++ b/docs/conventions.md @@ -0,0 +1,266 @@ +# Libgit2 Conventions + +We like to keep the source consistent and readable. Herein are some +guidelines that should help with that. + +## External API + +We have a few rules to avoid surprising ways of calling functions and +some rules for consumers of the library to avoid stepping on each +other's toes. + + - Property accessors return the value directly (e.g. an `int` or + `const char *`) but if a function can fail, we return a `int` value + and the output parameters go first in the parameter list, followed + by the object that a function is operating on, and then any other + arguments the function may need. + + - If a function returns an object as a return value, that function is + a getter and the object's lifetime is tied to the parent + object. Objects which are returned as the first argument as a + pointer-to-pointer are owned by the caller and it is responsible + for freeing it. Strings are returned via `git_buf` in order to + allow for re-use and safe freeing. + + - Most of what libgit2 does relates to I/O so as a general rule + you should assume that any function can fail due to errors as even + getting data from the filesystem can result in all sorts of errors + and complex failure cases. + + - Paths inside the Git system are separated by a slash (0x2F). If a + function accepts a path on disk, then backslashes (0x5C) are also + accepted on Windows. + + - Do not mix allocators. If something has been allocated by libgit2, + you do not know which is the right free function in the general + case. Use the free functions provided for each object type. + +## Compatibility + +`libgit2` runs on many different platforms with many different compilers. + +The public API of `libgit2` is [ANSI C](http://en.wikipedia.org/wiki/ANSI_C) +(a.k.a. C89) compatible. + +Internally, `libgit2` is written using a portable subset of C99 - in order +to maximize compatibility (e.g. with MSVC) we avoid certain C99 +extensions. Specifically, we keep local variable declarations at the tops +of blocks only and we avoid `//` style comments. + +Also, to the greatest extent possible, we try to avoid lots of `#ifdef`s +inside the core code base. This is somewhat unavoidable, but since it can +really hamper maintainability, we keep it to a minimum. + +## Match Surrounding Code + +If there is one rule to take away from this document, it is *new code should +match the surrounding code in a way that makes it impossible to distinguish +the new from the old.* Consistency is more important to us than anyone's +personal opinion about where braces should be placed or spaces vs. tabs. + +If a section of code is being completely rewritten, it is okay to bring it +in line with the standards that are laid out here, but we will not accept +submissions that contain a large number of changes that are merely +reformatting. + +## Naming Things + +All external types and functions start with `git_` and all `#define` macros +start with `GIT_`. The `libgit2` API is mostly broken into related +functional modules each with a corresponding header. All functions in a +module should be named like `git_modulename_functioname()` +(e.g. `git_repository_open()`). + +Functions with a single output parameter should name that parameter `out`. +Multiple outputs should be named `foo_out`, `bar_out`, etc. + +Parameters of type `git_oid` should be named `id`, or `foo_id`. Calls that +return an OID should be named `git_foo_id`. + +Where a callback function is used, the function should also include a +user-supplied extra input that is a `void *` named "payload" that will be +passed through to the callback at each invocation. + +## Typedefs + +Wherever possible, use `typedef`. In some cases, if a structure is just a +collection of function pointers, the pointer types don't need to be +separately typedef'd, but loose function pointer types should be. + +## Exports + +All exported functions must be declared as: + +```c +GIT_EXTERN(result_type) git_modulename_functionname(arg_list); +``` + +## Internals + +Functions whose *modulename* is followed by two underscores, +for example `git_odb__read_packed`, are semi-private functions. +They are primarily intended for use within the library itself, +and may disappear or change their signature in a future release. + +## Parameters + +Out parameters come first. + +Whenever possible, pass argument pointers as `const`. Some structures (such +as `git_repository` and `git_index`) have mutable internal structure that +prevents this. + +Callbacks should always take a `void *` payload as their last parameter. +Callback pointers are grouped with their payloads, and typically come last +when passed as arguments: + +```c +int git_foo(git_repository *repo, git_foo_cb callback, void *payload); +``` + +## Memory Ownership + +Some APIs allocate memory which the caller is responsible for freeing; others +return a pointer into a buffer that's owned by some other object. Make this +explicit in the documentation. + +## Return codes + +Most public APIs should return an `int` error code. As is typical with most +C library functions, a zero value indicates success and a negative value +indicates failure. + +Some bindings will transform these returned error codes into exception +types, so returning a semantically appropriate error code is important. +Check +[`include/git2/errors.h`](https://github.com/libgit2/libgit2/blob/development/include/git2/errors.h) +for the return codes already defined. + +In your implementation, use `git_error_set()` to provide extended error +information to callers. + +If a `libgit2` function internally invokes another function that reports an +error, but the error is not propagated up, use `git_error_clear()` to prevent +callers from getting the wrong error message later on. + + +## Structs + +Most public types should be opaque, e.g.: + +```C +typedef struct git_odb git_odb; +``` + +...with allocation functions returning an "instance" created within +the library, and not within the application. This allows the type +to grow (or shrink) in size without rebuilding client code. + +To preserve ABI compatibility, include an `int version` field in all transparent +structures, and initialize to the latest version in the constructor call. +Increment the "latest" version whenever the structure changes, and try to only +append to the end of the structure. + +## Option Structures + +If a function's parameter count is too high, it may be desirable to package +up the options in a structure. Make them transparent, include a version +field, and provide an initializer constant or constructor. Using these +structures should be this easy: + +```C +git_foo_options opts = GIT_FOO_OPTIONS_INIT; +opts.baz = BAZ_OPTION_ONE; +git_foo(&opts); +``` + +## Enumerations + +Typedef all enumerated types. If each option stands alone, use the enum +type for passing them as parameters; if they are flags to be OR'ed together, +pass them as `unsigned int` or `uint32_t` or some appropriate type. + +## Code Layout + +Try to keep lines less than 80 characters long. This is a loose +requirement, but going significantly over 80 columns is not nice. + +Use common sense to wrap most code lines; public function declarations +can use a couple of different styles: + +```c +/** All on one line is okay if it fits */ +GIT_EXTERN(int) git_foo_simple(git_oid *id); + +/** Otherwise one argument per line is a good next step */ +GIT_EXTERN(int) git_foo_id( + git_oid **out, + int a, + int b); +``` + +Indent with tabs; set your editor's tab width to eight for best effect. + +Avoid trailing whitespace and only commit Unix-style newlines (i.e. no CRLF +in the repository - just set `core.autocrlf` to true if you are writing code +on a Windows machine). + +## Documentation + +All comments should conform to Doxygen "javadoc" style conventions for +formatting the public API documentation. Try to document every parameter, +and keep the comments up to date if you change the parameter list. + +## Public Header Template + +Use this template when creating a new public header. + +```C +#ifndef INCLUDE_git_${filename}_h__ +#define INCLUDE_git_${filename}_h__ + +#include "git/common.h" + +/** + * @file git/${filename}.h + * @brief Git some description + * @defgroup git_${filename} some description routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/* ... definitions ... */ + +/** @} */ +GIT_END_DECL +#endif +``` + +## Inlined functions + +All inlined functions must be declared as: + +```C +GIT_INLINE(result_type) git_modulename_functionname(arg_list); +``` + +`GIT_INLINE` (or `inline`) should not be used in public headers in order +to preserve ANSI C compatibility. + +## Tests + +`libgit2` uses the [clar](https://github.com/vmg/clar) testing framework. + +All PRs should have corresponding tests. + +* If the PR fixes an existing issue, the test should fail prior to applying + the PR and succeed after applying it. +* If the PR is for new functionality, then the tests should exercise that + new functionality to a certain extent. We don't require 100% coverage + right now (although we are getting stricter over time). + +When adding new tests, we prefer if you attempt to reuse existing test data +(in `tests-clar/resources/`) if possible. If you are going to add new test +repositories, please try to strip them of unnecessary files (e.g. sample +hooks, etc). diff --git a/docs/diff-internals.md b/docs/diff-internals.md new file mode 100644 index 00000000000..da4c5a17c57 --- /dev/null +++ b/docs/diff-internals.md @@ -0,0 +1,92 @@ +Diff is broken into four phases: + +1. Building a list of things that have changed. These changes are called + deltas (git_diff_delta objects) and are grouped into a git_diff_list. +2. Applying file similarity measurement for rename and copy detection (and + to potentially split files that have changed radically). This step is + optional. +3. Computing the textual diff for each delta. Not all deltas have a + meaningful textual diff. For those that do, the textual diff can + either be generated on the fly and passed to output callbacks or can be + turned into a git_diff_patch object. +4. Formatting the diff and/or patch into standard text formats (such as + patches, raw lists, etc). + +In the source code, step 1 is implemented in `src/diff.c`, step 2 in +`src/diff_tform.c`, step 3 in `src/diff_patch.c`, and step 4 in +`src/diff_print.c`. Additionally, when it comes to accessing file +content, everything goes through diff drivers that are implemented in +`src/diff_driver.c`. + +External Objects +---------------- + +* `git_diff_options` represents user choices about how a diff should be + performed and is passed to most diff generating functions. +* `git_diff_file` represents an item on one side of a possible delta +* `git_diff_delta` represents a pair of items that have changed in some + way - it contains two `git_diff_file` plus a status and other stuff. +* `git_diff_list` is a list of deltas along with information about how + those particular deltas were found. +* `git_diff_patch` represents the actual diff between a pair of items. In + some cases, a delta may not have a corresponding patch, if the objects + are binary, for example. The content of a patch will be a set of hunks + and lines. +* A `hunk` is range of lines described by a `git_diff_range` (i.e. "lines + 10-20 in the old file became lines 12-23 in the new"). It will have a + header that compactly represents that information, and it will have a + number of lines of context surrounding added and deleted lines. +* A `line` is simple a line of data along with a `git_diff_line_t` value + that tells how the data should be interpreted (e.g. context or added). + +Internal Objects +---------------- + +* `git_diff_file_content` is an internal structure that represents the + data on one side of an item to be diffed; it is an augmented + `git_diff_file` with more flags and the actual file data. + + * it is created from a repository plus a) a git_diff_file, b) a git_blob, + or c) raw data and size + * there are three main operations on git_diff_file_content: + + * _initialization_ sets up the data structure and does what it can up to, + but not including loading and looking at the actual data + * _loading_ loads the data, preprocesses it (i.e. applies filters) and + potentially analyzes it (to decide if binary) + * _free_ releases loaded data and frees any allocated memory + +* The internal structure of a `git_diff_patch` stores the actual diff + between a pair of `git_diff_file_content` items + + * it may be "unset" if the items are not diffable + * "empty" if the items are the same + * otherwise it will consist of a set of hunks each of which covers some + number of lines of context, additions and deletions + * a patch is created from two git_diff_file_content items + * a patch is fully instantiated in three phases: + + * initial creation and initialization + * loading of data and preliminary data examination + * diffing of data and optional storage of diffs + * (TBD) if a patch is asked to store the diffs and the size of the diff + is significantly smaller than the raw data of the two sides, then the + patch may be flattened using a pool of string data + +* `git_diff_output` is an internal structure that represents an output + target for a `git_diff_patch` + * It consists of file, hunk, and line callbacks, plus a payload + * There is a standard flattened output that can be used for plain text output + * Typically we use a `git_xdiff_output` which drives the callbacks via the + xdiff code taken from core Git. + +* `git_diff_driver` is an internal structure that encapsulates the logic + for a given type of file + * a driver is looked up based on the name and mode of a file. + * the driver can then be used to: + * determine if a file is binary (by attributes, by git_diff_options + settings, or by examining the content) + * give you a function pointer that is used to evaluate function context + for hunk headers + * At some point, the logic for getting a filtered version of file content + or calculating the OID of a file may be moved into the driver. diff --git a/docs/differences-from-git.md b/docs/differences-from-git.md new file mode 100644 index 00000000000..3f4650806a7 --- /dev/null +++ b/docs/differences-from-git.md @@ -0,0 +1,27 @@ +# Differences from Git + +In some instances, the functionality of libgit2 deviates slightly from Git. This can be because of technical limitations when developing a library, licensing limitations when converting functionality from Git to libgit2, or various other reasons. + +Repository and Workdir Path Reporting +------------------------------------- + +When asking Git for the absolute path of a repository via `git rev-parse --absolute-git-dir`, it will output the path to the ".git" folder without a trailing slash. In contrast to that, the call `git_repository_path(repo)` will return the path with a trailing slash: + +``` +git rev-parse --absolute-git-dir -> /home/user/projects/libgit2/.git +git_repository_path(repo) -> /home/user/projects/libgit2/.git/ +``` + +The same difference exists when listing worktrees: + +``` +git worktree list -> /home/user/projects/libgit2 +git_repository_workdir(repo) -> /home/user/projects/libgit2/ +``` + +Windows Junction Points +----------------------- + +In libgit2, junction points are treated like symbolic links. They're handled specially in `git_win32__file_attribute_to_stat` in `src/win/w32_util.h`. This means that libgit2 tracks the directory itself as a link. + +In Git for Windows, junction points are treated like regular directories. This means that Git for Windows tracks the contents of the directory. diff --git a/docs/error-handling.md b/docs/error-handling.md index 655afeba8a8..13ce78f5f6c 100644 --- a/docs/error-handling.md +++ b/docs/error-handling.md @@ -1,111 +1,284 @@ Error reporting in libgit2 ========================== -Error reporting is performed on an explicit `git_error **` argument, which appears at the end of all API calls that can return an error. Yes, this does clutter the API. - -When a function fails, an error is set on the error variable **and** returns one of the generic error codes. +Libgit2 tries to follow the POSIX style: functions return an `int` value +with 0 (zero) indicating success and negative values indicating an error. +There are specific negative error codes for each "expected failure" +(e.g. `GIT_ENOTFOUND` for files that take a path which might be missing) +and a generic error code (-1) for all critical or non-specific failures +(e.g. running out of memory or system corruption). + +When a negative value is returned, an error message is also set. The +message can be accessed via the `git_error_last` function which will return a +pointer to a `git_error` structure containing the error message text and +the class of error (i.e. what part of the library generated the error). + +For instance: An object lookup by SHA prefix (`git_object_lookup_prefix`) +has two expected failure cases: the SHA is not found at all which returns +`GIT_ENOTFOUND` or the SHA prefix is ambiguous (i.e. two or more objects +share the prefix) which returns `GIT_EAMBIGUOUS`. There are any number of +critical failures (such as a packfile being corrupted, a loose object +having the wrong access permissions, etc.) all of which will return -1. +When the object lookup is successful, it will return 0. + +If libgit2 was compiled with threads enabled (`-DUSE_THREADS=ON` when using +CMake), then the error message will be kept in thread-local storage, so it +will not be modified by other threads. If threads are not enabled, then +the error message is in global data. + +All of the error return codes, the `git_error` type, the error access +functions, and the error classes are defined in `include/git2/errors.h`. +See the documentation there for details on the APIs for accessing, +clearing, and even setting error codes. + +When writing libgit2 code, please be smart and conservative when returning +error codes. Functions usually have a maximum of two or three "expected +errors" and in most cases only one. If you feel there are more possible +expected error scenarios, then the API you are writing may be at too high +a level for core libgit2. + +Example usage +------------- + +When using libgit2, you will typically capture the return value from +functions using an `int` variable and check to see if it is negative. +When that happens, you can, if you wish, look at the specific value or +look at the error message that was generated. ~~~c -int git_repository_open(git_repository **repository, const char *path, git_error **error) { - // perform some opening - if (p_exists(path) < 0) { - giterr_set(error, GITERR_REPOSITORY, "The path '%s' doesn't exist", path); - return GIT_ENOTFOUND; - } + git_repository *repo; + int error = git_repository_open(&repo, "path/to/repo"); - ... + if (error < 0) { + fprintf(stderr, "Could not open repository: %s\n", git_error_last()->message); + exit(1); + } - if (try_to_parse(path, error) < 0) - return GIT_ERROR; + ... use `repo` here ... - ... + git_repository_free(repo); /* void function - no error return code */ } ~~~ -The simple error API --------------------- +Some of the error return values do have meaning. Optionally, you can look +at the specific error values to decide what to do. -- `void giterr_set(git_error **, int, const char *, ...)`: the main function used to set an error. It allocates a new error object and stores it in the passed error pointer. It has no return value. The arguments for `giterr_set` are as follows: +~~~c +{ + git_repository *repo; + const char *path = "path/to/repo"; + int error = git_repository_open(&repo, path); + + if (error < 0) { + if (error == GIT_ENOTFOUND) + fprintf(stderr, "Could not find repository at path '%s'\n", path); + else + fprintf(stderr, "Unable to open repository: %s\n", + git_error_last()->message); + exit(1); + } - - `git_error **error_ptr`: the pointer where the error will be created. - - `int error_class`: the class for the error. This is **not** an error code: this is an specific enum that specifies the error family. The point is to map these families 1-1 with Exception types on higher level languages (e.g. GitRepositoryException) - - `const char *error_str, ...`: the error string, with optional formatting arguments + ... happy ... +} +~~~ -- `void giterr_free(git_error *)`: takes an error and frees it. This function is available in the external API. +Some of the higher-level language bindings may use a range of information +from libgit2 to convert error return codes into exceptions, including the +specific error return codes and even the class of error and the error +message returned by `git_error_last`, but the full range of that logic is +beyond the scope of this document. -- `void giterr_clear(git_error **)`: clears an error previously set in an error pointer, setting it to NULL and calling `giterr_free` on it. +Example internal implementation +------------------------------- -- `void giterr_propagate(git_error **, git_error *)`: moves an error to a given error pointer, handling the case when the error pointer is NULL (in that case the error gets freed, because it cannot be propagated). +Internally, libgit2 detects error scenarios, records error messages, and +returns error values. Errors from low-level functions are generally +passed upwards (unless the higher level can either handle the error or +wants to translate the error into something more meaningful). -The new error code return values --------------------------------- +~~~c +int git_repository_open(git_repository **repository, const char *path) +{ + /* perform some logic to open the repository */ + if (p_exists(path) < 0) { + git_error_set(GIT_ERROR_REPOSITORY, "The path '%s' doesn't exist", path); + return GIT_ENOTFOUND; + } -We are doing this the POSIX way: one error code for each "expected failure", and a generic error code for all the critical failures. + ... +} +~~~ -For instance: A reference lookup can have an expected failure (which is when the reference cannot be found), and a critical failure (which could be any of a long list of things that could go wrong, such as the refs packfile being corrupted, a loose ref being written with the wrong permissions, etc). We cannot have distinct error codes for every single error in the library, hence `git_reference_lookup` would return GIT_SUCCESS if the operation was successful, GIT_ENOTFOUND when the reference doesn't exist, and GIT_ERROR when an error happens -- **the error is then detailed in the `git_error` parameter**. +Note that some error codes have been defined with a specific meaning in the +context of callbacks: +- `GIT_EUSER` provides a way to bubble up a non libgit2-related failure, which + allows it to be preserved all the way up to the initial function call (a `git_cred` + setup trying to access an unavailable LDAP server for instance). +- `GIT_EPASSTHROUGH` provides a way to tell libgit2 that it should behave as if + no callback was provided. This is of special interest to bindings, which would + always provide a C function as a "trampoline", and decide at runtime what to do. -Please be smart when returning error codes. Functions have max two "expected errors", and in most cases only one. +The public error API +-------------------- + +- `const git_error *git_error_last(void)`: The main function used to look up + the last error. This may return NULL if no error has occurred. + Otherwise this should return a `git_error` object indicating the class + of error and the error message that was generated by the library. + Do not use this function unless the prior call to a libgit2 API + returned an error, as it can otherwise give misleading results. + libgit2's error strings are not cleared aggressively, + and this function may return an error string that reflects a prior error, + possibly even reflecting internal state. + + The last error is stored in thread-local storage when libgit2 is + compiled with thread support, so you do not have to worry about another + thread overwriting the value. When thread support is off, the last + error is a global value. + + _Note_ There are some known bugs in the library where this may return + NULL even when an error code was generated. Please report these as + bugs, but in the meantime, please code defensively and check for NULL + when calling this function. + +- `void git_error_clear(void)`: This function clears the last error. The + library will call this when an error is generated by low level function + and the higher level function handles the error. + + _Note_ There are some known bugs in the library where a low level + function's error message is not cleared by higher level code that + handles the error and returns zero. Please report these as bugs, but in + the meantime, a zero return value from a libgit2 API does not guarantee + that `git_error_last()` will return NULL. + +- `void git_error_set(int error_class, const char *message)`: This + function can be used when writing a custom backend module to set the + libgit2 error message. See the documentation on this function for its + use. Normal usage of libgit2 will probably never need to call this API. + +- `void git_error_set_oom(void)`: This is a standard function for reporting + an out-of-memory error. It is written in a manner that it doesn't have + to allocate any extra memory in order to record the error, so this is + the best way to report that scenario. + +Deviations from the standard +---------------------------- + +There are some public functions that do not return `int` values. There +are two primary cases: + +* `void` return values: If a function has a `void` return, then it will + never fail. This primary will be used for object destructors. + +* `git_xyz *` return values: These are simple accessor functions where the + only meaningful error would typically be looking something up by index + and having the index be out of bounds. In those cases, the function + will typically return NULL. + +* Boolean return values: There are some cases where a function cannot fail + and wants to return a boolean value. In those cases, we try to return 1 + for true and 0 for false. These cases are rare and the return value for + the function should probably be an `unsigned int` to denote these cases. + If you find an exception, please open an issue and let's fix it. + +There are a few other exceptions to these rules here and there in the +library, but those are extremely rare and should probably be converted +over to other to more standard patterns for usage. Feel free to open +issues pointing these out. + +There are some known bugs in the library where some functions may return a +negative value but not set an error message and some other functions may +return zero (no error) and yet leave an error message set. Please report +these cases as issues and they will be fixed. In the meanwhile, please +code defensively, checking that the return value of `git_error_last` is not +NULL before using it, and not relying on `git_error_last` to return NULL when +a function returns 0 for success. + +The internal error API +---------------------- + +- `void git_error_set(int error_class, const char *fmt, ...)`: This is the + main internal function for setting an error. It works like `printf` to + format the error message. See the notes of `git_error_set_str` for a + general description of how error messages are stored (and also about + special handling for `error_class` of `GIT_ERROR_OS`). Writing error messages ---------------------- Here are some guidelines when writing error messages: -- Use proper English, and an impersonal or past tenses: *The given path does not exist*, *Failed to lookup object in ODB* +- Use proper English, and an impersonal or past tenses: *The given path + does not exist*, *Failed to lookup object in ODB* -- Use short, direct and objective messages. **One line, max**. libgit2 is a low level library: think that all the messages reported will be thrown as Ruby or Python exceptions. Think how long are common exception messages in those languages. +- Use short, direct and objective messages. **One line, max**. libgit2 is + a low level library: think that all the messages reported will be thrown + as Ruby or Python exceptions. Think how long are common exception + messages in those languages. -- **Do not add redundant information to the error message**, specially information that can be inferred from the context. +- **Do not add redundant information to the error message**, specially + information that can be inferred from the context. - E.g. in `git_repository_open`, do not report a message like "Failed to open repository: path not found". Somebody is - calling that function. If it fails, he already knows that the repository failed to open! + E.g. in `git_repository_open`, do not report a message like "Failed to + open repository: path not found". Somebody is calling that + function. If it fails, they already know that the repository failed to + open! General guidelines for error reporting -------------------------------------- -- We never handle programming errors with these functions. Programming errors are `assert`ed, and when their source is internal, fixed as soon as possible. This is C, people. - - Example of programming errors that would **not** be handled: passing NULL to a function that expects a valid pointer; passing a `git_tree` to a function that expects a `git_commit`. All these cases need to be identified with `assert` and fixed asap. +- Libgit2 does not handle programming errors with these + functions. Programming errors are `assert`ed, and when their source is + internal, fixed as soon as possible. This is C, people. - Example of a runtime error: failing to parse a `git_tree` because it contains invalid data. Failing to open a file because it doesn't exist on disk. These errors would be handled, and a `git_error` would be set. + Example of programming errors that would **not** be handled: passing + NULL to a function that expects a valid pointer; passing a `git_tree` + to a function that expects a `git_commit`. All these cases need to be + identified with `assert` and fixed asap. -- The `git_error **` argument is always the last in the signature of all API calls. No exceptions. + Example of a runtime error: failing to parse a `git_tree` because it + contains invalid data. Failing to open a file because it doesn't exist + on disk. These errors are handled, a meaningful error message is set, + and an error code is returned. -- When the programmer (or us, internally) doesn't need error handling, he can pass `NULL` to the `git_error **` param. This means that the errors won't be *reported*, but obviously they still will be handled (i.e. the failing function will interrupt and return cleanly). This is transparently handled by `giterr_set` +- In general, *do not* try to overwrite errors internally and *do* + propagate error codes from lower level functions to the higher level. + There are some cases where propagating an error code will be more + confusing rather than less, so there are some exceptions to this rule, + but the default behavior should be to simply clean up and pass the error + on up to the caller. -- `git_error *` **must be initialized to `NULL` before passing its value to a function!!** + **WRONG** ~~~c - git_error *err; - git_error *good_error = NULL; - - git_foo_func(arg1, arg2, &error); // invalid: `error` is not initialized - git_foo_func2(arg1, arg2, &good_error); // OK! - git_foo_func3(arg1, arg2, NULL); // OK! But no error reporting! - ~~~ - -- Piling up errors is an error! Don't do this! Errors must always be free'd when a function returns. + int git_commit_parent(...) + { + ... - ~~~c - git_error *error = NULL; + if (git_commit_lookup(parent, repo, parent_id) < 0) { + git_error_set(GIT_ERROR_COMMIT, "Overwrite lookup error message"); + return -1; /* mask error code */ + } - git_foo_func1(arg1, &error); - git_foo_func2(arg2, &error); // WRONG! What if func1 failed? `error` would leak! + ... + } ~~~ -- Likewise: do not rethrow errors internally! + **RIGHT** ~~~c - int git_commit_create(..., git_error **error) + int git_commit_parent(...) { - if (git_reference_exists("HEAD", error) < 0) { - /* HEAD does not exist; create it so we can commit... */ - if (git_reference_create("HEAD", error) < 0) { - /* error could be rethrown */ - } - } + ... -- Remember that errors are now allocated, and hence they need to be free'd after they've been used. Failure to do so internally (e.g. in the already seen examples of error piling) will be reported by Valgrind, so we can easily find where are we rethrowing errors. + error = git_commit_lookup(parent, repo, parent_id); + if (error < 0) { + /* cleanup intermediate objects if necessary */ + /* leave error message and propagate error code */ + return error; + } -- Remember that any function that fails **will set an error object**, and that object will be freed. + ... + } + ~~~ diff --git a/docs/fuzzing.md b/docs/fuzzing.md new file mode 100644 index 00000000000..2bf4ccca915 --- /dev/null +++ b/docs/fuzzing.md @@ -0,0 +1,72 @@ +# Fuzzing + +libgit2 is currently using [libFuzzer](https://libfuzzer.info) to perform +automated fuzz testing. libFuzzer only works with clang. + +## Prerequisites for building fuzz targets: + +1. All the prerequisites for [building libgit2](https://github.com/libgit2/libgit2). +2. A recent version of clang. 6.0 is preferred. [pre-build Debian/Ubuntu + packages](https://github.com/libgit2/libgit2) + +## Build + +1. Create a build directory beneath the libgit2 source directory, and change + into it: `mkdir build && cd build` +2. Choose one sanitizers to add. The currently supported sanitizers are + [`address`](https://clang.llvm.org/docs/AddressSanitizer.html), + [`undefined`](https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html), + and [`leak`/`address,leak`](https://clang.llvm.org/docs/LeakSanitizer.html). +3. Create the cmake build environment and configure the build with the + sanitizer chosen: `CC=/usr/bin/clang-6.0 CFLAGS="-fsanitize=address" cmake + -DBUILD_TESTS=OFF -DBUILD_FUZZERS=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo ..`. + Note that building the fuzzer targets is incompatible with the + tests and examples. +4. Build libgit2: `cmake --build .` +5. Exit the cmake build environment: `cd ..` + +## Run the fuzz targets + +1. `ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolize + LSAN_OPTIONS=allocator_may_return_null=1 + ASAN_OPTIONS=allocator_may_return_null=1 ./build/fuzzers/packfile_fuzzer + fuzzers/corpora/packfile/` + +The `LSAN_OPTIONS` and `ASAN_OPTIONS` are there to allow `malloc(3)` to return +`NULL`, which is expected if a huge chunk of memory is allocated. The +`LLVM_PROFILE_FILE` environment string can also be added to override the path +where libFuzzer will write the coverage report. + +## Get coverage + +In order to get coverage information, you need to add the "-fcoverage-mapping" +and "-fprofile-instr-generate CFLAGS, and then run the fuzz target with +`-runs=0`. That will produce a file called `default.profraw` (this behavior can +be overridden by setting the `LLVM_PROFILE_FILE="yourfile.profraw"` environment +variable). + +1. `llvm-profdata-6.0 merge -sparse default.profraw -o + fuzz_packfile_raw.profdata` transforms the data from a sparse representation + into a format that can be used by the other tools. +2. `llvm-cov-6.0 report ./build/fuzz/fuzz_packfile_raw + -instr-profile=fuzz_packfile_raw.profdata` shows a high-level per-file + coverage report. +3. `llvm-cov-6.0 show ./build/fuzz/fuzz_packfile_raw + -instr-profile=fuzz_packfile_raw.profdata [source file]` shows a line-by-line + coverage analysis of all the codebase (or a single source file). + +## Standalone mode + +In order to ensure that there are no regresions, each fuzzer target can be run +in a standalone mode. This can be done by passing `-DUSE_STANDALONE_FUZZERS=ON`. +This makes it compatible with gcc. This does not use the fuzzing engine, but +just invokes every file in the chosen corpus. + +In order to get full coverage, though, you might want to also enable one of the +sanitizers. You might need a recent version of clang to get full support. + +## References + +* [libFuzzer](https://llvm.org/docs/LibFuzzer.html) documentation. +* [Source-based Code + Coverage](https://clang.llvm.org/docs/SourceBasedCodeCoverage.html). diff --git a/docs/merge-df_conflicts.txt b/docs/merge-df_conflicts.txt new file mode 100644 index 00000000000..09780ee2d22 --- /dev/null +++ b/docs/merge-df_conflicts.txt @@ -0,0 +1,41 @@ +Anc / Our / Thr represent the ancestor / ours / theirs side of a merge +from branch "branch" into HEAD. Workdir represents the expected files in +the working directory. Index represents the expected files in the index, +with stage markers. + + Anc Our Thr Workdir Index +1 D D + D/F D/F D/F [0] + +2 D D+ D~HEAD (mod/del) D/F [0] + D/F D/F D [1] + D [2] + +3 D D D/F D/F [0] + D/F + +4 D D+ D~branch (mod/del) D/F [0] + D/F D/F D [1] + D [3] + +5 D D/F (add/add) D/F [2] + D/F D/F [3] + D/F + +6 D/F D/F D D [0] + D + +7 D/F D/F+ D/F (mod/del) D/F [1] + D D~branch (fil/dir) D/F [2] + D [3] + +8 D/F D/F D D [0] + D + +9 D/F D/F+ D/F (mod/del) D/F [1] + D D~HEAD (fil/dir) D [2] + D/F [3] + +10 D/F D/F (fil/dir) D/F [0] + D D~HEAD D [2] + D diff --git a/docs/projects.md b/docs/projects.md new file mode 100644 index 00000000000..1b9f47648c4 --- /dev/null +++ b/docs/projects.md @@ -0,0 +1,93 @@ +Projects For LibGit2 +==================== + +So, you want to start helping out with `libgit2`? That's fantastic! We +welcome contributions and we promise we'll try to be nice. + +This is a list of libgit2 related projects that new contributors can take +on. It includes a number of good starter projects as well as some larger +ideas that no one is actively working on. + +## Before You Start + +Please start by reading the [README.md](../README.md), +[contributing.md](contributing.md), and [conventions.md](conventions.md) +files before diving into one of these projects. Those explain our work +flow and coding conventions to help ensure that your work will be easily +integrated into libgit2. + +Next, work through the build instructions and make sure you can clone the +repository, compile it, and run the tests successfully. That will make +sure that your development environment is set up correctly and you are +ready to start on libgit2 development. + +## Starter Projects + +These are good small projects to get started with libgit2. + +* Look at the `examples/` programs, find an existing one that mirrors a + core Git command and add a missing command-line option. There are many + gaps right now and this helps demonstrate how to use the library. Here + are some specific ideas (though there are many more): + * Fix the `examples/diff.c` implementation of the `-B` + (a.k.a. `--break-rewrites`) command line option to actually look for + the optional `[][/]` configuration values. There is an + existing comment that reads `/* TODO: parse thresholds */`. The + trick to this one will be doing it in a manner that is clean and + simple, but still handles the various cases correctly (e.g. `-B/70%` + is apparently a legal setting). + * As an extension to the matching idea for `examples/log.c`, add the + `-i` option to use `strcasestr()` for matches. + * For `examples/log.c`, implement the `--first-parent` option now that + libgit2 supports it in the revwalk API. +* Pick a Git command that is not already emulated in `examples/` and write + a new example that mirrors the behavior. Examples don't have to be + perfect emulations, but should demonstrate how to use the libgit2 APIs + to get results that are similar to Git commands. This lets you (and us) + easily exercise a particular facet of the API and measure compatibility + and feature parity with core git. +* Submit a PR to clarify documentation! While we do try to document all of + the APIs, your fresh eyes on the documentation will find areas that are + confusing much more easily. + +If none of these appeal to you, take a look at our issues list to see if +there are any unresolved issues you'd like to jump in on. + +## Larger Projects + +These are ideas for larger projects mostly taken from our backlog of +[Issues](https://github.com/libgit2/libgit2/issues). Please don't dive +into one of these as a first project for libgit2 - we'd rather get to +know you first by successfully shipping your work on one of the smaller +projects above. + +Some of these projects are broken down into subprojects and/or have +some incremental steps listed towards the larger goal. Those steps +might make good smaller projects by themselves. + +* Port part of the Git test suite to run against the command line emulation + in `examples/` + * Pick a Git command that is emulated in our `examples/` area + * Extract the Git tests that exercise that command + * Convert the tests to call our emulation + * These tests could go in `examples/tests/`... +* Add hooks API to enumerate and manage hooks (not run them at this point) + * Enumeration of available hooks + * Lookup API to see which hooks have a script and get the script + * Read/write API to load a hook script and write a hook script + * Eventually, callback API to invoke a hook callback when libgit2 + executes the action in question +* Isolate logic of ignore evaluation into a standalone API +* Upgrade internal libxdiff code to latest from core Git +* Tree builder improvements: + * Extend to allow building a tree hierarchy +* Apply-patch API +* Add a patch editing API to enable "git add -p" type operations +* Textconv API to filter binary data before generating diffs (something + like the current Filter API, probably). +* Performance profiling and improvement +* Support "git replace" ref replacements +* Include conflicts in diff results and in status + * GIT_DELTA_CONFLICT for items in conflict (with multiple files) + * Appropriate flags for status +* Support sparse checkout (i.e. "core.sparsecheckout" and ".git/info/sparse-checkout") diff --git a/docs/release.md b/docs/release.md new file mode 100644 index 00000000000..87fccaabe63 --- /dev/null +++ b/docs/release.md @@ -0,0 +1,83 @@ +# Releasing the library + +We have three kinds of releases: "full" releases, maintenance releases and security releases. Full ones release the state of the `main` branch whereas maintenance releases provide bugfixes building on top of the currently released series. Security releases are also for the current series but only contain security fixes on top of the previous release. + +## Full release + +We aim to release once every six months. We start the process by opening an issue. This is accompanied with a feature freeze. From now until the release, only bug fixes are to be merged. Use the following as a base for the issue + + Release v0.X + + Let's release v0.X, codenamed: + + - [ ] Bump the versions in the headers (`include/git2/version.h`) + - [ ] Bump the versions in the clib manifest (`package.json`) + - [ ] Make a release candidate + - [ ] Plug any final leaks + - [ ] Fix any last-minute issues + - [ ] Make sure changelog.md reflects everything worth discussing + - [ ] Update the version in changelog.md and the header + - [ ] Produce a release candidate + - [ ] Tag + - [ ] Create maint/v0.X + - [ ] Update any bindings the core team works with + +We tag at least one release candidate. This RC must carry the new version in the headers, including the SOVERSION. If there are no significant issues found, we can go straight to the release after a single RC. This is up to the discretion of the release manager. There is no set time to have the candidate out, but we should we should give downstream projects at least a week to give feedback. + +Preparing the first release candidate includes updating the version number of libgit2 to the new version number. To do so, a pull request shall be submitted that adjusts the version number in the following places: + +- docs/changelog.md +- include/git2/version.h +- package.json + +As soon as the pull request is merged, the merge commit shall be tagged with a lightweight tag. + +The tagging happens via GitHub's "releases" tab which lets us attach release notes to a particular tag. In the description we include the changes in `docs/changelog.md` between the last full release. Use the following as a base for the release notes + + This is the first release of the v0.X series, . The changelog follows. + +followed by the three sections in the changelog. For release candidates we can avoid copying the full changelog and only include any new entries. + +During the freeze, and certainly after the first release candidate, any bindings the core team work with should be updated in order to discover any issues that might come up with the multitude of approaches to memory management, embedding or linking. + +Create a branch `maint/v0.X` at the current state of `main` after you've created the tag. This will be used for maintenance releases and lets our dependents track the latest state of the series. + +## Maintenance release + +Every once in a while, when we feel we've accumulated a significant amount of backportable fixes in the mainline branch, we produce a maintenance release in order to provide fixes or improvements for those who track the releases. This also lets our users and integrators receive updates without having to upgrade to the next full release. + +As a rule of thumb, it's a good idea to produce a maintenance release for the current series when we're getting ready for a full release. This gives the (still) current series a last round of fixes without having to upgrade (which with us potentially means adjusting to API changes). + +Start by opening an issue. Use the following as a base. + + Release v0.X.Y + + Enough fixes have accumulated, let's release v0.X.Y + + - [ ] Select the changes we want to backport + - [ ] Update maint/v0.X + - [ ] Tag + +The list of changes to backport does not need to be comprehensive and we might not backport something if the code in mainline has diverged significantly. These fixes do not include those which require API or ABI changes as we release under the same SOVERSION. + +Do not merge into the `maint/v0.X` until we are getting ready to produce a new release. There is always the possibility that we will need to produce a security release and those must only include the relevant security fixes and not arbitrary fixes we were planning on releasing at some point. + +Here we do not use release candidates as the changes are supposed to be small and proven. + +## Security releases + +This is the same as a maintenance release, except that the fix itself will most likely be developed in a private repository and will only be visible to a select group of people until the release. + +We have committed to providing security fixes for the latest two released versions. E.g. if the latest version is v0.28.x, then we will provide security fixes for both v0.28.x and v0.27.y. + +## Updating documentation + +We use docurium to generate our documentation. It is a tool written in ruby which leverages libclang's documentation parser. Install docurium + + gem install docurium + +and run it against our description file with the tip of `main` checked out. + + cm doc api.docurium + +It will start up a few processes and write out the results as a new commit onto the `gh-pages` branch. That can be pushed to GitHub to update what will show up on our documentation reference site. diff --git a/docs/threading.md b/docs/threading.md new file mode 100644 index 00000000000..de085c807cc --- /dev/null +++ b/docs/threading.md @@ -0,0 +1,110 @@ +Threading in libgit2 +================== + +Unless otherwise specified, libgit2 objects cannot be safely accessed by +multiple threads simultaneously. + +There are also caveats on the cryptographic libraries libgit2 or its +dependencies link to (more on this later). For libgit2 itself, +provided you take the following into consideration you won't run into +issues: + +Sharing objects +--------------- + +Use an object from a single thread at a time. Most data structures do +not guard against concurrent access themselves. This is because they +are rarely used in isolation and it makes more sense to synchronize +access via a larger lock or similar mechanism. + +There are some objects which are read-only/immutable and are thus safe +to share across threads, such as references and configuration +snapshots. + +The `git_odb` object uses locking internally, and is thread-safe to use from +multiple threads simultaneously. + +Error messages +-------------- + +The error message is thread-local. The `git_error_last()` call must +happen on the same thread as the error in order to get the +message. Often this will be the case regardless, but if you use +something like the [GCD](http://en.wikipedia.org/wiki/Grand_Central_Dispatch) +on macOS (where code is executed on an arbitrary thread), the code +must make sure to retrieve the error code on the thread where the error +happened. + +Threading and cryptographic libraries +======================================= + +On Windows +---------- + +When built as a native Windows DLL, libgit2 uses WinCNG and WinHTTP, +both of which are thread-safe. You do not need to do anything special. + +When using libssh2 which itself uses WinCNG, there are no special +steps necessary. If you are using a MinGW or similar environment where +libssh2 uses OpenSSL or libgcrypt, then the general case affects +you. + +On macOS +----------- + +By default we make use of CommonCrypto and SecureTransport for cryptographic +support. These are thread-safe and you do not need to do anything special. + +Note that libssh2 may still use OpenSSL itself. In that case, the +general case still affects you if you use ssh. + +General Case +------------ + +libgit2 will default to OpenSSL for HTTPS transport (except on Windows and +macOS, as mentioned above). On any system, mbedTLS _may_ be optionally +enabled as the security provider. OpenSSL is thread-safe starting at +version 1.1.0. If your copy of libgit2 is linked against that version, +you do not need to take any further steps. + +Older versions of OpenSSL are made to be thread-implementation agnostic, and the +users of the library must set which locking function it should use. libgit2 +cannot know what to set as the user of libgit2 may also be using OpenSSL independently and +the locking settings must then live outside the lifetime of libgit2. + +Even if libgit2 doesn't use OpenSSL directly, OpenSSL can still be used by +libssh2 depending on the configuration. If OpenSSL is used by +more than one library, you only need to set up threading for OpenSSL once. + +If libgit2 is linked against OpenSSL < 1.1.0, it provides a last-resort convenience function +`git_openssl_set_locking()` (available in `sys/openssl.h`) to use the +platform-native mutex mechanisms to perform the locking, which you can use +if you do not want to use OpenSSL outside of libgit2, or you +know that libgit2 will outlive the rest of the operations. It is then not +safe to use OpenSSL multi-threaded after libgit2's shutdown function +has been called. Note `git_openssl_set_locking()` only works if +libgit2 uses OpenSSL directly - if OpenSSL is only used as a dependency +of libssh2 as described above, `git_openssl_set_locking()` is a no-op. + +If your programming language offers a package/bindings for OpenSSL, +you should very strongly prefer to use that in order to set up +locking, as they provide a level of coordination which is impossible +when using this function. + +See the +[OpenSSL documentation](https://www.openssl.org/docs/crypto/threads.html) +on threading for more details, and http://trac.libssh2.org/wiki/MultiThreading +for a specific example of providing the threading callbacks. + +libssh2 may be linked against OpenSSL or libgcrypt. If it uses OpenSSL, +see the above paragraphs. If it uses libgcrypt, then you need to +set up its locking before using it multi-threaded. libgit2 has no +direct connection to libgcrypt and thus has no convenience functions for +it (but libgcrypt has macros). Read libgcrypt's +[threading documentation for more information](http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html) + +It is your responsibility as an application author or packager to know +what your dependencies are linked against and to take the appropriate +steps to ensure the cryptographic libraries are thread-safe. We agree +that this situation is far from ideal but at this time it is something +the application authors need to deal with. diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md new file mode 100644 index 00000000000..085fff831d3 --- /dev/null +++ b/docs/troubleshooting.md @@ -0,0 +1,13 @@ +Troubleshooting libgit2 Problems +================================ + +CMake Failures +-------------- + +* **`Asked for OpenSSL TLS backend, but it wasn't found`** + CMake cannot find your SSL/TLS libraries. By default, libgit2 always + builds with HTTPS support, and you are encouraged to install the + OpenSSL libraries for your system (eg, `apt-get install libssl-dev`). + + For development, if you simply want to disable HTTPS support entirely, + pass the `-DUSE_HTTPS=OFF` argument to `cmake` when configuring it. diff --git a/docs/win32-longpaths.md b/docs/win32-longpaths.md new file mode 100644 index 00000000000..a18152fc90d --- /dev/null +++ b/docs/win32-longpaths.md @@ -0,0 +1,36 @@ +core.longpaths support +====================== + +Historically, Windows has limited absolute path lengths to `MAX_PATH` +(260) characters. + +Unfortunately, 260 characters is a punishing small maximum. This is +especially true for developers where dependencies may have dependencies +in a folder, each dependency themselves having dependencies in a +sub-folder, ad (seemingly) infinitum. + +So although the Windows APIs _by default_ honor this 260 character +maximum, you can get around this by using separate APIs. Git honors a +`core.longpaths` configuration option that allows some paths on Windows +to exceed these 260 character limits. + +And because they've gone and done it, that means that libgit2 has to +honor this value, too. + +Since `core.longpaths` is a _configuration option_ that means that we +need to be able to resolve a configuration - including in _the repository +itself_ in order to know whether long paths should be supported. + +Therefore, in libgit2, `core.longpaths` affects paths in working +directories _only_. Paths to the repository, and to items inside the +`.git` folder, must be no longer than 260 characters. + +This definition is required to avoid a paradoxical setting: if you +had a repository in a folder that was 280 characters long, how would +you know whether `core.longpaths` support should be enabled? Even if +`core.longpaths` was set to true in a system configuration file, the +repository itself may set `core.longpaths` to false in _its_ configuration +file, which you could only read if `core.longpaths` were set to true. + +Thus, `core.longpaths` must _only_ apply to working directory items, +and cannot apply to the `.git` folder or its contents. diff --git a/examples/.gitignore b/examples/.gitignore deleted file mode 100644 index e40bfc29f28..00000000000 --- a/examples/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -general -showindex -diff -*.dSYM diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 00000000000..986daae59df --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,17 @@ +# examples: code usage examples of libgit2 + +file(GLOB SRC_EXAMPLES *.c *.h) + +add_executable(lg2 ${SRC_EXAMPLES}) + +# Ensure that we do not use deprecated functions internally +add_definitions(-DGIT_DEPRECATE_HARD) + +target_include_directories(lg2 PRIVATE ${LIBGIT2_INCLUDES} ${LIBGIT2_DEPENDENCY_INCLUDES}) +target_include_directories(lg2 SYSTEM PRIVATE ${LIBGIT2_SYSTEM_INCLUDES}) + +if(WIN32 OR ANDROID) + target_link_libraries(lg2 libgit2package) +else() + target_link_libraries(lg2 libgit2package pthread) +endif() diff --git a/examples/COPYING b/examples/COPYING new file mode 100644 index 00000000000..0e259d42c99 --- /dev/null +++ b/examples/COPYING @@ -0,0 +1,121 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/examples/Makefile b/examples/Makefile deleted file mode 100644 index b306d4800b2..00000000000 --- a/examples/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -.PHONY: all - -CC = gcc -CFLAGS = -g -I../include -I../src -Wall -Wextra -Wmissing-prototypes -Wno-missing-field-initializers -LFLAGS = -L../build -lgit2 -lz -APPS = general showindex diff - -all: $(APPS) - -% : %.c - $(CC) -o $@ $(CFLAGS) $< $(LFLAGS) - -clean: - $(RM) $(APPS) - $(RM) -r *.dSYM diff --git a/examples/README.md b/examples/README.md index f2b6d7d234f..0f1f253877a 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,11 +1,22 @@ libgit2 examples ================ -These examples are meant as thin, easy-to-read snippets for Docurium -(https://github.com/github/docurium) rather than full-blown -implementations of Git commands. They are not vetted as carefully -for bugs, error handling, or cross-platform compatibility as the -rest of the code in libgit2, so copy with some caution. +These examples are a mixture of basic emulation of core Git command line +functions and simple snippets demonstrating libgit2 API usage (for use +with Docurium). As a whole, they are not vetted carefully for bugs, error +handling, and cross-platform compatibility in the same manner as the rest +of the code in libgit2, so copy with caution. -For HTML versions, check "Examples" at http://libgit2.github.com/libgit2 +That being said, you are welcome to copy code from these examples as +desired when using libgit2. They have been [released to the public domain][cc0], +so there are no restrictions on their use. +[cc0]: COPYING + +For annotated HTML versions, see the "Examples" section of: + + https://libgit2.org/libgit2 + +such as: + + https://libgit2.org/libgit2/ex/HEAD/general.html diff --git a/examples/add.c b/examples/add.c new file mode 100644 index 00000000000..1c93b11404a --- /dev/null +++ b/examples/add.c @@ -0,0 +1,157 @@ +/* + * libgit2 "add" example - shows how to modify the index + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/** + * The following example demonstrates how to add files with libgit2. + * + * It will use the repository in the current working directory, and act + * on files passed as its parameters. + * + * Recognized options are: + * -v/--verbose: show the file's status after acting on it. + * -n/--dry-run: do not actually change the index. + * -u/--update: update the index instead of adding to it. + */ + +enum index_mode { + INDEX_NONE, + INDEX_ADD +}; + +struct index_options { + int dry_run; + int verbose; + git_repository *repo; + enum index_mode mode; + int add_update; +}; + +/* Forward declarations for helpers */ +static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args); +int print_matched_cb(const char *path, const char *matched_pathspec, void *payload); + +int lg2_add(git_repository *repo, int argc, char **argv) +{ + git_index_matched_path_cb matched_cb = NULL; + git_index *index; + git_strarray array = {0}; + struct index_options options = {0}; + struct args_info args = ARGS_INFO_INIT; + + options.mode = INDEX_ADD; + + /* Parse the options & arguments. */ + parse_opts(NULL, &options, &args); + strarray_from_args(&array, &args); + + /* Grab the repository's index. */ + check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL); + + /* Setup a callback if the requested options need it */ + if (options.verbose || options.dry_run) { + matched_cb = &print_matched_cb; + } + + options.repo = repo; + + /* Perform the requested action with the index and files */ + if (options.add_update) { + git_index_update_all(index, &array, matched_cb, &options); + } else { + git_index_add_all(index, &array, 0, matched_cb, &options); + } + + /* Cleanup memory */ + git_index_write(index); + git_index_free(index); + + return 0; +} + +/* + * This callback is called for each file under consideration by + * git_index_(update|add)_all above. + * It makes uses of the callback's ability to abort the action. + */ +int print_matched_cb(const char *path, const char *matched_pathspec, void *payload) +{ + struct index_options *opts = (struct index_options *)(payload); + int ret; + unsigned status; + (void)matched_pathspec; + + /* Get the file status */ + if (git_status_file(&status, opts->repo, path) < 0) + return -1; + + if ((status & GIT_STATUS_WT_MODIFIED) || (status & GIT_STATUS_WT_NEW)) { + printf("add '%s'\n", path); + ret = 0; + } else { + ret = 1; + } + + if (opts->dry_run) + ret = 1; + + return ret; +} + +static void print_usage(void) +{ + fprintf(stderr, "usage: add [options] [--] file-spec [file-spec] [...]\n\n"); + fprintf(stderr, "\t-n, --dry-run dry run\n"); + fprintf(stderr, "\t-v, --verbose be verbose\n"); + fprintf(stderr, "\t-u, --update update tracked files\n"); + exit(1); +} + +static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args) +{ + if (args->argc <= 1) + print_usage(); + + for (args->pos = 1; args->pos < args->argc; ++args->pos) { + const char *curr = args->argv[args->pos]; + + if (curr[0] != '-') { + if (!strcmp("add", curr)) { + opts->mode = INDEX_ADD; + continue; + } else if (opts->mode == INDEX_NONE) { + fprintf(stderr, "missing command: %s", curr); + print_usage(); + break; + } else { + /* We might be looking at a filename */ + break; + } + } else if (match_bool_arg(&opts->verbose, args, "--verbose") || + match_bool_arg(&opts->dry_run, args, "--dry-run") || + match_str_arg(repo_path, args, "--git-dir") || + (opts->mode == INDEX_ADD && match_bool_arg(&opts->add_update, args, "--update"))) { + continue; + } else if (match_bool_arg(NULL, args, "--help")) { + print_usage(); + break; + } else if (match_arg_separator(args)) { + break; + } else { + fprintf(stderr, "Unsupported option %s.\n", curr); + print_usage(); + } + } +} diff --git a/examples/args.c b/examples/args.c new file mode 100644 index 00000000000..533e1579b5a --- /dev/null +++ b/examples/args.c @@ -0,0 +1,197 @@ +#include "common.h" +#include "args.h" + +size_t is_prefixed(const char *str, const char *pfx) +{ + size_t len = strlen(pfx); + return strncmp(str, pfx, len) ? 0 : len; +} + +int optional_str_arg( + const char **out, struct args_info *args, const char *opt, const char *def) +{ + const char *found = args->argv[args->pos]; + size_t len = is_prefixed(found, opt); + + if (!len) + return 0; + + if (!found[len]) { + if (args->pos + 1 == args->argc) { + *out = def; + return 1; + } + args->pos += 1; + *out = args->argv[args->pos]; + return 1; + } + + if (found[len] == '=') { + *out = found + len + 1; + return 1; + } + + return 0; +} + +int match_str_arg( + const char **out, struct args_info *args, const char *opt) +{ + const char *found = args->argv[args->pos]; + size_t len = is_prefixed(found, opt); + + if (!len) + return 0; + + if (!found[len]) { + if (args->pos + 1 == args->argc) + fatal("expected value following argument", opt); + args->pos += 1; + *out = args->argv[args->pos]; + return 1; + } + + if (found[len] == '=') { + *out = found + len + 1; + return 1; + } + + return 0; +} + +static const char *match_numeric_arg(struct args_info *args, const char *opt) +{ + const char *found = args->argv[args->pos]; + size_t len = is_prefixed(found, opt); + + if (!len) + return NULL; + + if (!found[len]) { + if (args->pos + 1 == args->argc) + fatal("expected numeric value following argument", opt); + args->pos += 1; + found = args->argv[args->pos]; + } else { + found = found + len; + if (*found == '=') + found++; + } + + return found; +} + +int match_uint16_arg( + uint16_t *out, struct args_info *args, const char *opt) +{ + const char *found = match_numeric_arg(args, opt); + uint16_t val; + char *endptr = NULL; + + if (!found) + return 0; + + val = (uint16_t)strtoul(found, &endptr, 0); + if (!endptr || *endptr != '\0') + fatal("expected number after argument", opt); + + if (out) + *out = val; + return 1; +} + +int match_uint32_arg( + uint32_t *out, struct args_info *args, const char *opt) +{ + const char *found = match_numeric_arg(args, opt); + uint16_t val; + char *endptr = NULL; + + if (!found) + return 0; + + val = (uint32_t)strtoul(found, &endptr, 0); + if (!endptr || *endptr != '\0') + fatal("expected number after argument", opt); + + if (out) + *out = val; + return 1; +} + +static int match_int_internal( + int *out, const char *str, int allow_negative, const char *opt) +{ + char *endptr = NULL; + int val = (int)strtol(str, &endptr, 10); + + if (!endptr || *endptr != '\0') + fatal("expected number", opt); + else if (val < 0 && !allow_negative) + fatal("negative values are not allowed", opt); + + if (out) + *out = val; + + return 1; +} + +int match_bool_arg(int *out, struct args_info *args, const char *opt) +{ + const char *found = args->argv[args->pos]; + + if (!strcmp(found, opt)) { + *out = 1; + return 1; + } + + if (!strncmp(found, "--no-", strlen("--no-")) && + !strcmp(found + strlen("--no-"), opt + 2)) { + *out = 0; + return 1; + } + + *out = -1; + return 0; +} + +int is_integer(int *out, const char *str, int allow_negative) +{ + return match_int_internal(out, str, allow_negative, NULL); +} + +int match_int_arg( + int *out, struct args_info *args, const char *opt, int allow_negative) +{ + const char *found = match_numeric_arg(args, opt); + if (!found) + return 0; + return match_int_internal(out, found, allow_negative, opt); +} + +int match_arg_separator(struct args_info *args) +{ + if (args->opts_done) + return 1; + + if (strcmp(args->argv[args->pos], "--") != 0) + return 0; + + args->opts_done = 1; + args->pos++; + return 1; +} + +void strarray_from_args(git_strarray *array, struct args_info *args) +{ + size_t i; + + array->count = args->argc - args->pos; + array->strings = calloc(array->count, sizeof(char *)); + assert(array->strings != NULL); + + for (i = 0; args->pos < args->argc; ++args->pos) { + array->strings[i++] = args->argv[args->pos]; + } + args->pos = args->argc; +} diff --git a/examples/args.h b/examples/args.h new file mode 100644 index 00000000000..4db0493bb2d --- /dev/null +++ b/examples/args.h @@ -0,0 +1,90 @@ +#ifndef INCLUDE_examples_args_h__ +#define INCLUDE_examples_args_h__ + +/** + * Argument-processing helper structure + */ +struct args_info { + int argc; + char **argv; + int pos; + unsigned int opts_done : 1; /**< Did we see a -- separator */ +}; +#define ARGS_INFO_INIT { argc, argv, 0, 0 } +#define ARGS_CURRENT(args) args->argv[args->pos] + +/** + * Check if a string has the given prefix. Returns 0 if not prefixed + * or the length of the prefix if it is. + */ +extern size_t is_prefixed(const char *str, const char *pfx); + +/** + * Match an integer string, returning 1 if matched, 0 if not. + */ +extern int is_integer(int *out, const char *str, int allow_negative); + +/** + * Check current `args` entry against `opt` string. If it matches + * exactly, take the next arg as a string; if it matches as a prefix with + * an equal sign, take the remainder as a string; if value not supplied, + * default value `def` will be given. otherwise return 0. + */ +extern int optional_str_arg( + const char **out, struct args_info *args, const char *opt, const char *def); + +/** + * Check current `args` entry against `opt` string. If it matches + * exactly, take the next arg as a string; if it matches as a prefix with + * an equal sign, take the remainder as a string; otherwise return 0. + */ +extern int match_str_arg( + const char **out, struct args_info *args, const char *opt); + +/** + * Check current `args` entry against `opt` string parsing as uint16. If + * `opt` matches exactly, take the next arg as a uint16_t value; if `opt` + * is a prefix (equal sign optional), take the remainder of the arg as a + * uint16_t value; otherwise return 0. + */ +extern int match_uint16_arg( + uint16_t *out, struct args_info *args, const char *opt); + +/** + * Check current `args` entry against `opt` string parsing as uint32. If + * `opt` matches exactly, take the next arg as a uint16_t value; if `opt` + * is a prefix (equal sign optional), take the remainder of the arg as a + * uint32_t value; otherwise return 0. + */ +extern int match_uint32_arg( + uint32_t *out, struct args_info *args, const char *opt); + +/** + * Check current `args` entry against `opt` string parsing as int. If + * `opt` matches exactly, take the next arg as an int value; if it matches + * as a prefix (equal sign optional), take the remainder of the arg as a + * int value; otherwise return 0. + */ +extern int match_int_arg( + int *out, struct args_info *args, const char *opt, int allow_negative); + +/** + * Check current `args` entry against a "bool" `opt` (ie. --[no-]progress). + * If `opt` matches positively, out will be set to 1, or if `opt` matches + * negatively, out will be set to 0, and in both cases 1 will be returned. + * If neither the positive or the negative form of opt matched, out will be -1, + * and 0 will be returned. + */ +extern int match_bool_arg(int *out, struct args_info *args, const char *opt); + +/** + * Check if we're processing past the single -- separator + */ +extern int match_arg_separator(struct args_info *args); + +/** + * Consume all remaining arguments in a git_strarray + */ +extern void strarray_from_args(git_strarray *array, struct args_info *args); + +#endif diff --git a/examples/blame.c b/examples/blame.c new file mode 100644 index 00000000000..1e2869a6395 --- /dev/null +++ b/examples/blame.c @@ -0,0 +1,202 @@ +/* + * libgit2 "blame" example - shows how to use the blame API + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/** + * This example demonstrates how to invoke the libgit2 blame API to roughly + * simulate the output of `git blame` and a few of its command line arguments. + */ + +struct blame_opts { + char *path; + char *commitspec; + int C; + int M; + int start_line; + int end_line; + int F; +}; +static void parse_opts(struct blame_opts *o, int argc, char *argv[]); + +int lg2_blame(git_repository *repo, int argc, char *argv[]) +{ + int line, break_on_null_hunk; + git_object_size_t i, rawsize; + char spec[1024] = {0}; + struct blame_opts o = {0}; + const char *rawdata; + git_revspec revspec = {0}; + git_blame_options blameopts = GIT_BLAME_OPTIONS_INIT; + git_blame *blame = NULL; + git_blob *blob; + git_object *obj; + + parse_opts(&o, argc, argv); + if (o.M) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES; + if (o.C) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES; + if (o.F) blameopts.flags |= GIT_BLAME_FIRST_PARENT; + if (o.start_line && o.end_line) { + blameopts.min_line = o.start_line; + blameopts.max_line = o.end_line; + } + + /** + * The commit range comes in "committish" form. Use the rev-parse API to + * nail down the end points. + */ + if (o.commitspec) { + check_lg2(git_revparse(&revspec, repo, o.commitspec), "Couldn't parse commit spec", NULL); + if (revspec.flags & GIT_REVSPEC_SINGLE) { + git_oid_cpy(&blameopts.newest_commit, git_object_id(revspec.from)); + git_object_free(revspec.from); + } else { + git_oid_cpy(&blameopts.oldest_commit, git_object_id(revspec.from)); + git_oid_cpy(&blameopts.newest_commit, git_object_id(revspec.to)); + git_object_free(revspec.from); + git_object_free(revspec.to); + } + } + + /** Run the blame. */ + check_lg2(git_blame_file(&blame, repo, o.path, &blameopts), "Blame error", NULL); + + /** + * Get the raw data inside the blob for output. We use the + * `committish:path/to/file.txt` format to find it. + */ + if (git_oid_is_zero(&blameopts.newest_commit)) + strcpy(spec, "HEAD"); + else + git_oid_tostr(spec, sizeof(spec), &blameopts.newest_commit); + strcat(spec, ":"); + strcat(spec, o.path); + + check_lg2(git_revparse_single(&obj, repo, spec), "Object lookup error", NULL); + check_lg2(git_blob_lookup(&blob, repo, git_object_id(obj)), "Blob lookup error", NULL); + git_object_free(obj); + + rawdata = git_blob_rawcontent(blob); + rawsize = git_blob_rawsize(blob); + + /** Produce the output. */ + line = 1; + i = 0; + break_on_null_hunk = 0; + while (i < rawsize) { + const char *eol = memchr(rawdata + i, '\n', (size_t)(rawsize - i)); + char oid[10] = {0}; + const git_blame_hunk *hunk = git_blame_hunk_byline(blame, line); + + if (break_on_null_hunk && !hunk) + break; + + if (hunk) { + char sig[128] = {0}; + break_on_null_hunk = 1; + + git_oid_tostr(oid, 10, &hunk->final_commit_id); + snprintf(sig, 30, "%s <%s>", hunk->final_signature->name, hunk->final_signature->email); + + printf("%s ( %-30s %3d) %.*s\n", + oid, + sig, + line, + (int)(eol - rawdata - i), + rawdata + i); + } + + i = (int)(eol - rawdata + 1); + line++; + } + + /** Cleanup. */ + git_blob_free(blob); + git_blame_free(blame); + + return 0; +} + +/** Tell the user how to make this thing work. */ +static void usage(const char *msg, const char *arg) +{ + if (msg && arg) + fprintf(stderr, "%s: %s\n", msg, arg); + else if (msg) + fprintf(stderr, "%s\n", msg); + fprintf(stderr, "usage: blame [options] [] \n"); + fprintf(stderr, "\n"); + fprintf(stderr, " example: `HEAD~10..HEAD`, or `1234abcd`\n"); + fprintf(stderr, " -L process only line range n-m, counting from 1\n"); + fprintf(stderr, " -M find line moves within and across files\n"); + fprintf(stderr, " -C find line copies within and across files\n"); + fprintf(stderr, " -F follow only the first parent commits\n"); + fprintf(stderr, "\n"); + exit(1); +} + +/** Parse the arguments. */ +static void parse_opts(struct blame_opts *o, int argc, char *argv[]) +{ + int i; + char *bare_args[3] = {0}; + + if (argc < 2) usage(NULL, NULL); + + for (i=1; i= 3) + usage("Invalid argument set", NULL); + bare_args[i] = a; + } + else if (!strcmp(a, "--")) + continue; + else if (!strcasecmp(a, "-M")) + o->M = 1; + else if (!strcasecmp(a, "-C")) + o->C = 1; + else if (!strcasecmp(a, "-F")) + o->F = 1; + else if (!strcasecmp(a, "-L")) { + i++; a = argv[i]; + if (i >= argc) fatal("Not enough arguments to -L", NULL); + check_lg2(sscanf(a, "%d,%d", &o->start_line, &o->end_line)-2, "-L format error", NULL); + } + else { + /* commit range */ + if (o->commitspec) fatal("Only one commit spec allowed", NULL); + o->commitspec = a; + } + } + + /* Handle the bare arguments */ + if (!bare_args[0]) usage("Please specify a path", NULL); + o->path = bare_args[0]; + if (bare_args[1]) { + /* */ + o->path = bare_args[1]; + o->commitspec = bare_args[0]; + } + if (bare_args[2]) { + /* */ + char spec[128] = {0}; + o->path = bare_args[2]; + sprintf(spec, "%s..%s", bare_args[0], bare_args[1]); + o->commitspec = spec; + } +} diff --git a/examples/cat-file.c b/examples/cat-file.c new file mode 100644 index 00000000000..741edb4841f --- /dev/null +++ b/examples/cat-file.c @@ -0,0 +1,239 @@ +/* + * libgit2 "cat-file" example - shows how to print data from the ODB + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +static void print_signature(const char *header, const git_signature *sig) +{ + char sign; + int offset, hours, minutes; + + if (!sig) + return; + + offset = sig->when.offset; + if (offset < 0) { + sign = '-'; + offset = -offset; + } else { + sign = '+'; + } + + hours = offset / 60; + minutes = offset % 60; + + printf("%s %s <%s> %ld %c%02d%02d\n", + header, sig->name, sig->email, (long)sig->when.time, + sign, hours, minutes); +} + +/** Printing out a blob is simple, get the contents and print */ +static void show_blob(const git_blob *blob) +{ + /* ? Does this need crlf filtering? */ + fwrite(git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob), 1, stdout); +} + +/** Show each entry with its type, id and attributes */ +static void show_tree(const git_tree *tree) +{ + size_t i, max_i = (int)git_tree_entrycount(tree); + char oidstr[GIT_OID_SHA1_HEXSIZE + 1]; + const git_tree_entry *te; + + for (i = 0; i < max_i; ++i) { + te = git_tree_entry_byindex(tree, i); + + git_oid_tostr(oidstr, sizeof(oidstr), git_tree_entry_id(te)); + + printf("%06o %s %s\t%s\n", + git_tree_entry_filemode(te), + git_object_type2string(git_tree_entry_type(te)), + oidstr, git_tree_entry_name(te)); + } +} + +/** + * Commits and tags have a few interesting fields in their header. + */ +static void show_commit(const git_commit *commit) +{ + unsigned int i, max_i; + char oidstr[GIT_OID_SHA1_HEXSIZE + 1]; + + git_oid_tostr(oidstr, sizeof(oidstr), git_commit_tree_id(commit)); + printf("tree %s\n", oidstr); + + max_i = (unsigned int)git_commit_parentcount(commit); + for (i = 0; i < max_i; ++i) { + git_oid_tostr(oidstr, sizeof(oidstr), git_commit_parent_id(commit, i)); + printf("parent %s\n", oidstr); + } + + print_signature("author", git_commit_author(commit)); + print_signature("committer", git_commit_committer(commit)); + + if (git_commit_message(commit)) + printf("\n%s\n", git_commit_message(commit)); +} + +static void show_tag(const git_tag *tag) +{ + char oidstr[GIT_OID_SHA1_HEXSIZE + 1]; + + git_oid_tostr(oidstr, sizeof(oidstr), git_tag_target_id(tag));; + printf("object %s\n", oidstr); + printf("type %s\n", git_object_type2string(git_tag_target_type(tag))); + printf("tag %s\n", git_tag_name(tag)); + print_signature("tagger", git_tag_tagger(tag)); + + if (git_tag_message(tag)) + printf("\n%s\n", git_tag_message(tag)); +} + +typedef enum { + SHOW_TYPE = 1, + SHOW_SIZE = 2, + SHOW_NONE = 3, + SHOW_PRETTY = 4 +} catfile_mode; + +/* Forward declarations for option-parsing helper */ +struct catfile_options { + const char *dir; + const char *rev; + catfile_mode action; + int verbose; +}; + +static void parse_opts(struct catfile_options *o, int argc, char *argv[]); + + +/** Entry point for this command */ +int lg2_cat_file(git_repository *repo, int argc, char *argv[]) +{ + struct catfile_options o = { ".", NULL, 0, 0 }; + git_object *obj = NULL; + char oidstr[GIT_OID_SHA1_HEXSIZE + 1]; + + parse_opts(&o, argc, argv); + + check_lg2(git_revparse_single(&obj, repo, o.rev), + "Could not resolve", o.rev); + + if (o.verbose) { + char oidstr[GIT_OID_SHA1_HEXSIZE + 1]; + git_oid_tostr(oidstr, sizeof(oidstr), git_object_id(obj)); + + printf("%s %s\n--\n", + git_object_type2string(git_object_type(obj)), oidstr); + } + + switch (o.action) { + case SHOW_TYPE: + printf("%s\n", git_object_type2string(git_object_type(obj))); + break; + case SHOW_SIZE: { + git_odb *odb; + git_odb_object *odbobj; + + check_lg2(git_repository_odb(&odb, repo), "Could not open ODB", NULL); + check_lg2(git_odb_read(&odbobj, odb, git_object_id(obj)), + "Could not find obj", NULL); + + printf("%ld\n", (long)git_odb_object_size(odbobj)); + + git_odb_object_free(odbobj); + git_odb_free(odb); + } + break; + case SHOW_NONE: + /* just want return result */ + break; + case SHOW_PRETTY: + + switch (git_object_type(obj)) { + case GIT_OBJECT_BLOB: + show_blob((const git_blob *)obj); + break; + case GIT_OBJECT_COMMIT: + show_commit((const git_commit *)obj); + break; + case GIT_OBJECT_TREE: + show_tree((const git_tree *)obj); + break; + case GIT_OBJECT_TAG: + show_tag((const git_tag *)obj); + break; + default: + printf("unknown %s\n", oidstr); + break; + } + break; + } + + git_object_free(obj); + + return 0; +} + +/** Print out usage information */ +static void usage(const char *message, const char *arg) +{ + if (message && arg) + fprintf(stderr, "%s: %s\n", message, arg); + else if (message) + fprintf(stderr, "%s\n", message); + fprintf(stderr, + "usage: cat-file (-t | -s | -e | -p) [-v] [-q] " + "[-h|--help] [--git-dir=] \n"); + exit(1); +} + +/** Parse the command-line options taken from git */ +static void parse_opts(struct catfile_options *o, int argc, char *argv[]) +{ + struct args_info args = ARGS_INFO_INIT; + + for (args.pos = 1; args.pos < argc; ++args.pos) { + char *a = argv[args.pos]; + + if (a[0] != '-') { + if (o->rev != NULL) + usage("Only one rev should be provided", NULL); + else + o->rev = a; + } + else if (!strcmp(a, "-t")) + o->action = SHOW_TYPE; + else if (!strcmp(a, "-s")) + o->action = SHOW_SIZE; + else if (!strcmp(a, "-e")) + o->action = SHOW_NONE; + else if (!strcmp(a, "-p")) + o->action = SHOW_PRETTY; + else if (!strcmp(a, "-q")) + o->verbose = 0; + else if (!strcmp(a, "-v")) + o->verbose = 1; + else if (!strcmp(a, "--help") || !strcmp(a, "-h")) + usage(NULL, NULL); + else if (!match_str_arg(&o->dir, &args, "--git-dir")) + usage("Unknown option", a); + } + + if (!o->action || !o->rev) + usage(NULL, NULL); + +} diff --git a/examples/checkout.c b/examples/checkout.c new file mode 100644 index 00000000000..82567cdc432 --- /dev/null +++ b/examples/checkout.c @@ -0,0 +1,290 @@ +/* + * libgit2 "checkout" example - shows how to perform checkouts + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/* Define the printf format specifier to use for size_t output */ +#if defined(_MSC_VER) || defined(__MINGW32__) +# define PRIuZ "Iu" +# define PRIxZ "Ix" +# define PRIdZ "Id" +#else +# define PRIuZ "zu" +# define PRIxZ "zx" +# define PRIdZ "zd" +#endif + +/** + * The following example demonstrates how to do checkouts with libgit2. + * + * Recognized options are : + * --force: force the checkout to happen. + * --[no-]progress: show checkout progress, on by default. + * --perf: show performance data. + */ + +typedef struct { + unsigned int force : 1; + unsigned int progress : 1; + unsigned int perf : 1; +} checkout_options; + +static void print_usage(void) +{ + fprintf(stderr, "usage: checkout [options] \n" + "Options are :\n" + " --git-dir: use the following git repository.\n" + " --force: force the checkout.\n" + " --[no-]progress: show checkout progress.\n" + " --perf: show performance data.\n"); + exit(1); +} + +static void parse_options(const char **repo_path, checkout_options *opts, struct args_info *args) +{ + if (args->argc <= 1) + print_usage(); + + memset(opts, 0, sizeof(*opts)); + + /* Default values */ + opts->progress = 1; + + for (args->pos = 1; args->pos < args->argc; ++args->pos) { + const char *curr = args->argv[args->pos]; + int bool_arg; + + if (match_arg_separator(args)) { + break; + } else if (!strcmp(curr, "--force")) { + opts->force = 1; + } else if (match_bool_arg(&bool_arg, args, "--progress")) { + opts->progress = bool_arg; + } else if (match_bool_arg(&bool_arg, args, "--perf")) { + opts->perf = bool_arg; + } else if (match_str_arg(repo_path, args, "--git-dir")) { + continue; + } else { + break; + } + } +} + +/** + * This function is called to report progression, ie. it's called once with + * a NULL path and the number of total steps, then for each subsequent path, + * the current completed_step value. + */ +static void print_checkout_progress(const char *path, size_t completed_steps, size_t total_steps, void *payload) +{ + (void)payload; + if (path == NULL) { + printf("checkout started: %" PRIuZ " steps\n", total_steps); + } else { + printf("checkout: %s %" PRIuZ "/%" PRIuZ "\n", path, completed_steps, total_steps); + } +} + +/** + * This function is called when the checkout completes, and is used to report the + * number of syscalls performed. + */ +static void print_perf_data(const git_checkout_perfdata *perfdata, void *payload) +{ + (void)payload; + printf("perf: stat: %" PRIuZ " mkdir: %" PRIuZ " chmod: %" PRIuZ "\n", + perfdata->stat_calls, perfdata->mkdir_calls, perfdata->chmod_calls); +} + +/** + * This is the main "checkout " function, responsible for performing + * a branch-based checkout. + */ +static int perform_checkout_ref(git_repository *repo, git_annotated_commit *target, const char *target_ref, checkout_options *opts) +{ + git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; + git_reference *ref = NULL, *branch = NULL; + git_commit *target_commit = NULL; + int err; + + /** Setup our checkout options from the parsed options */ + checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; + if (opts->force) + checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE; + + if (opts->progress) + checkout_opts.progress_cb = print_checkout_progress; + + if (opts->perf) + checkout_opts.perfdata_cb = print_perf_data; + + /** Grab the commit we're interested to move to */ + err = git_commit_lookup(&target_commit, repo, git_annotated_commit_id(target)); + if (err != 0) { + fprintf(stderr, "failed to lookup commit: %s\n", git_error_last()->message); + goto cleanup; + } + + /** + * Perform the checkout so the workdir corresponds to what target_commit + * contains. + * + * Note that it's okay to pass a git_commit here, because it will be + * peeled to a tree. + */ + err = git_checkout_tree(repo, (const git_object *)target_commit, &checkout_opts); + if (err != 0) { + fprintf(stderr, "failed to checkout tree: %s\n", git_error_last()->message); + goto cleanup; + } + + /** + * Now that the checkout has completed, we have to update HEAD. + * + * Depending on the "origin" of target (ie. it's an OID or a branch name), + * we might need to detach HEAD. + */ + if (git_annotated_commit_ref(target)) { + const char *target_head; + + if ((err = git_reference_lookup(&ref, repo, git_annotated_commit_ref(target))) < 0) + goto error; + + if (git_reference_is_remote(ref)) { + if ((err = git_branch_create_from_annotated(&branch, repo, target_ref, target, 0)) < 0) + goto error; + target_head = git_reference_name(branch); + } else { + target_head = git_annotated_commit_ref(target); + } + + err = git_repository_set_head(repo, target_head); + } else { + err = git_repository_set_head_detached_from_annotated(repo, target); + } + +error: + if (err != 0) { + fprintf(stderr, "failed to update HEAD reference: %s\n", git_error_last()->message); + goto cleanup; + } + +cleanup: + git_commit_free(target_commit); + git_reference_free(branch); + git_reference_free(ref); + + return err; +} + +/** + * This corresponds to `git switch --guess`: if a given ref does + * not exist, git will by default try to guess the reference by + * seeing whether any remote has a branch called . If there + * is a single remote only that has it, then it is assumed to be + * the desired reference and a local branch is created for it. + * + * The following is a simplified implementation. It will not try + * to check whether the ref is unique across all remotes. + */ +static int guess_refish(git_annotated_commit **out, git_repository *repo, const char *ref) +{ + git_strarray remotes = { NULL, 0 }; + git_reference *remote_ref = NULL; + int error; + size_t i; + + if ((error = git_remote_list(&remotes, repo)) < 0) + goto out; + + for (i = 0; i < remotes.count; i++) { + char *refname = NULL; + size_t reflen; + + reflen = snprintf(refname, 0, "refs/remotes/%s/%s", remotes.strings[i], ref); + if ((refname = malloc(reflen + 1)) == NULL) { + error = -1; + goto next; + } + snprintf(refname, reflen + 1, "refs/remotes/%s/%s", remotes.strings[i], ref); + + if ((error = git_reference_lookup(&remote_ref, repo, refname)) < 0) + goto next; + + break; +next: + free(refname); + if (error < 0 && error != GIT_ENOTFOUND) + break; + } + + if (!remote_ref) { + error = GIT_ENOTFOUND; + goto out; + } + + if ((error = git_annotated_commit_from_ref(out, repo, remote_ref)) < 0) + goto out; + +out: + git_reference_free(remote_ref); + git_strarray_dispose(&remotes); + return error; +} + +/** That example's entry point */ +int lg2_checkout(git_repository *repo, int argc, char **argv) +{ + struct args_info args = ARGS_INFO_INIT; + checkout_options opts; + git_repository_state_t state; + git_annotated_commit *checkout_target = NULL; + int err = 0; + const char *path = "."; + + /** Parse our command line options */ + parse_options(&path, &opts, &args); + + /** Make sure we're not about to checkout while something else is going on */ + state = git_repository_state(repo); + if (state != GIT_REPOSITORY_STATE_NONE) { + fprintf(stderr, "repository is in unexpected state %d\n", state); + goto cleanup; + } + + if (match_arg_separator(&args)) { + /** + * Try to checkout the given path + */ + + fprintf(stderr, "unhandled path-based checkout\n"); + err = 1; + goto cleanup; + } else { + /** + * Try to resolve a "refish" argument to a target libgit2 can use + */ + if ((err = resolve_refish(&checkout_target, repo, args.argv[args.pos])) < 0 && + (err = guess_refish(&checkout_target, repo, args.argv[args.pos])) < 0) { + fprintf(stderr, "failed to resolve %s: %s\n", args.argv[args.pos], git_error_last()->message); + goto cleanup; + } + err = perform_checkout_ref(repo, checkout_target, args.argv[args.pos], &opts); + } + +cleanup: + git_annotated_commit_free(checkout_target); + + return err; +} diff --git a/examples/clone.c b/examples/clone.c new file mode 100644 index 00000000000..22d9d9b6122 --- /dev/null +++ b/examples/clone.c @@ -0,0 +1,104 @@ +#include "common.h" + +typedef struct progress_data { + git_indexer_progress fetch_progress; + size_t completed_steps; + size_t total_steps; + const char *path; +} progress_data; + +static void print_progress(const progress_data *pd) +{ + int network_percent = pd->fetch_progress.total_objects > 0 ? + (100*pd->fetch_progress.received_objects) / pd->fetch_progress.total_objects : + 0; + int index_percent = pd->fetch_progress.total_objects > 0 ? + (100*pd->fetch_progress.indexed_objects) / pd->fetch_progress.total_objects : + 0; + + int checkout_percent = pd->total_steps > 0 + ? (int)((100 * pd->completed_steps) / pd->total_steps) + : 0; + size_t kbytes = pd->fetch_progress.received_bytes / 1024; + + if (pd->fetch_progress.total_objects && + pd->fetch_progress.received_objects == pd->fetch_progress.total_objects) { + printf("Resolving deltas %u/%u\r", + pd->fetch_progress.indexed_deltas, + pd->fetch_progress.total_deltas); + } else { + printf("net %3d%% (%4" PRIuZ " kb, %5u/%5u) / idx %3d%% (%5u/%5u) / chk %3d%% (%4" PRIuZ "/%4" PRIuZ")%s\n", + network_percent, kbytes, + pd->fetch_progress.received_objects, pd->fetch_progress.total_objects, + index_percent, pd->fetch_progress.indexed_objects, pd->fetch_progress.total_objects, + checkout_percent, + pd->completed_steps, pd->total_steps, + pd->path); + } +} + +static int sideband_progress(const char *str, int len, void *payload) +{ + (void)payload; /* unused */ + + printf("remote: %.*s", len, str); + fflush(stdout); + return 0; +} + +static int fetch_progress(const git_indexer_progress *stats, void *payload) +{ + progress_data *pd = (progress_data*)payload; + pd->fetch_progress = *stats; + print_progress(pd); + return 0; +} +static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload) +{ + progress_data *pd = (progress_data*)payload; + pd->completed_steps = cur; + pd->total_steps = tot; + pd->path = path; + print_progress(pd); +} + + +int lg2_clone(git_repository *repo, int argc, char **argv) +{ + progress_data pd = {{0}}; + git_repository *cloned_repo = NULL; + git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; + git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; + const char *url = argv[1]; + const char *path = argv[2]; + int error; + + (void)repo; /* unused */ + + /* Validate args */ + if (argc < 3) { + printf ("USAGE: %s \n", argv[0]); + return -1; + } + + /* Set up options */ + checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; + checkout_opts.progress_cb = checkout_progress; + checkout_opts.progress_payload = &pd; + clone_opts.checkout_opts = checkout_opts; + clone_opts.fetch_opts.callbacks.sideband_progress = sideband_progress; + clone_opts.fetch_opts.callbacks.transfer_progress = &fetch_progress; + clone_opts.fetch_opts.callbacks.credentials = cred_acquire_cb; + clone_opts.fetch_opts.callbacks.payload = &pd; + + /* Do the clone */ + error = git_clone(&cloned_repo, url, path, &clone_opts); + printf("\n"); + if (error != 0) { + const git_error *err = git_error_last(); + if (err) printf("ERROR %d: %s\n", err->klass, err->message); + else printf("ERROR %d: no detailed info\n", error); + } + else if (cloned_repo) git_repository_free(cloned_repo); + return error; +} diff --git a/examples/commit.c b/examples/commit.c new file mode 100644 index 00000000000..c6e0a8dc447 --- /dev/null +++ b/examples/commit.c @@ -0,0 +1,88 @@ +/* + * libgit2 "commit" example - shows how to create a git commit + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/** + * This example demonstrates the libgit2 commit APIs to roughly + * simulate `git commit` with the commit message argument. + * + * This does not have: + * + * - Robust error handling + * - Most of the `git commit` options + * + * This does have: + * + * - Example of performing a git commit with a comment + * + */ +int lg2_commit(git_repository *repo, int argc, char **argv) +{ + const char *opt = argv[1]; + const char *comment = argv[2]; + int error; + + git_oid commit_oid,tree_oid; + git_tree *tree; + git_index *index; + git_object *parent = NULL; + git_reference *ref = NULL; + git_signature *author_signature, *committer_signature; + + /* Validate args */ + if (argc < 3 || strcmp(opt, "-m") != 0) { + printf ("USAGE: %s -m \n", argv[0]); + return -1; + } + + error = git_revparse_ext(&parent, &ref, repo, "HEAD"); + if (error == GIT_ENOTFOUND) { + printf("HEAD not found. Creating first commit\n"); + error = 0; + } else if (error != 0) { + const git_error *err = git_error_last(); + if (err) printf("ERROR %d: %s\n", err->klass, err->message); + else printf("ERROR %d: no detailed info\n", error); + } + + check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL); + check_lg2(git_index_write_tree(&tree_oid, index), "Could not write tree", NULL);; + check_lg2(git_index_write(index), "Could not write index", NULL);; + + check_lg2(git_tree_lookup(&tree, repo, &tree_oid), "Error looking up tree", NULL); + + check_lg2(git_signature_default_from_env(&author_signature, &committer_signature, repo), + "Error creating signature", NULL); + + check_lg2(git_commit_create_v( + &commit_oid, + repo, + "HEAD", + author_signature, + committer_signature, + NULL, + comment, + tree, + parent ? 1 : 0, parent), "Error creating commit", NULL); + + git_index_free(index); + git_signature_free(author_signature); + git_signature_free(committer_signature); + git_tree_free(tree); + git_object_free(parent); + git_reference_free(ref); + + return error; +} diff --git a/examples/common.c b/examples/common.c new file mode 100644 index 00000000000..b068b8488c7 --- /dev/null +++ b/examples/common.c @@ -0,0 +1,260 @@ +/* + * Utilities library for libgit2 examples + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + + +#include "common.h" + +#ifndef _WIN32 +# include +#endif +#include + +void check_lg2(int error, const char *message, const char *extra) +{ + const git_error *lg2err; + const char *lg2msg = "", *lg2spacer = ""; + + if (!error) + return; + + if ((lg2err = git_error_last()) != NULL && lg2err->message != NULL) { + lg2msg = lg2err->message; + lg2spacer = " - "; + } + + if (extra) + fprintf(stderr, "%s '%s' [%d]%s%s\n", + message, extra, error, lg2spacer, lg2msg); + else + fprintf(stderr, "%s [%d]%s%s\n", + message, error, lg2spacer, lg2msg); + + exit(1); +} + +void fatal(const char *message, const char *extra) +{ + if (extra) + fprintf(stderr, "%s %s\n", message, extra); + else + fprintf(stderr, "%s\n", message); + + exit(1); +} + +int diff_output( + const git_diff_delta *d, + const git_diff_hunk *h, + const git_diff_line *l, + void *p) +{ + FILE *fp = (FILE*)p; + + (void)d; (void)h; + + if (!fp) + fp = stdout; + + if (l->origin == GIT_DIFF_LINE_CONTEXT || + l->origin == GIT_DIFF_LINE_ADDITION || + l->origin == GIT_DIFF_LINE_DELETION) + fputc(l->origin, fp); + + fwrite(l->content, 1, l->content_len, fp); + + return 0; +} + +void treeish_to_tree( + git_tree **out, git_repository *repo, const char *treeish) +{ + git_object *obj = NULL; + + check_lg2( + git_revparse_single(&obj, repo, treeish), + "looking up object", treeish); + + check_lg2( + git_object_peel((git_object **)out, obj, GIT_OBJECT_TREE), + "resolving object to tree", treeish); + + git_object_free(obj); +} + +void *xrealloc(void *oldp, size_t newsz) +{ + void *p = realloc(oldp, newsz); + if (p == NULL) { + fprintf(stderr, "Cannot allocate memory, exiting.\n"); + exit(1); + } + return p; +} + +int resolve_refish(git_annotated_commit **commit, git_repository *repo, const char *refish) +{ + git_reference *ref; + git_object *obj; + int err = 0; + + assert(commit != NULL); + + err = git_reference_dwim(&ref, repo, refish); + if (err == GIT_OK) { + git_annotated_commit_from_ref(commit, repo, ref); + git_reference_free(ref); + return 0; + } + + err = git_revparse_single(&obj, repo, refish); + if (err == GIT_OK) { + err = git_annotated_commit_lookup(commit, repo, git_object_id(obj)); + git_object_free(obj); + } + + return err; +} + +static int readline(char **out) +{ + int c, error = 0, length = 0, allocated = 0; + char *line = NULL; + + errno = 0; + + while ((c = getchar()) != EOF) { + if (length == allocated) { + allocated += 16; + + if ((line = realloc(line, allocated)) == NULL) { + error = -1; + goto error; + } + } + + if (c == '\n') + break; + + line[length++] = c; + } + + if (errno != 0) { + error = -1; + goto error; + } + + line[length] = '\0'; + *out = line; + line = NULL; + error = length; +error: + free(line); + return error; +} + +static int ask(char **out, const char *prompt, char optional) +{ + printf("%s ", prompt); + fflush(stdout); + + if (!readline(out) && !optional) { + fprintf(stderr, "Could not read response: %s", strerror(errno)); + return -1; + } + + return 0; +} + +int cred_acquire_cb(git_credential **out, + const char *url, + const char *username_from_url, + unsigned int allowed_types, + void *payload) +{ + char *username = NULL, *password = NULL, *privkey = NULL, *pubkey = NULL; + int error = 1; + + UNUSED(url); + UNUSED(payload); + + if (username_from_url) { + if ((username = strdup(username_from_url)) == NULL) + goto out; + } else if ((error = ask(&username, "Username:", 0)) < 0) { + goto out; + } + + if (allowed_types & GIT_CREDENTIAL_SSH_KEY) { + int n; + + if ((error = ask(&privkey, "SSH Key:", 0)) < 0 || + (error = ask(&password, "Password:", 1)) < 0) + goto out; + + if ((n = snprintf(NULL, 0, "%s.pub", privkey)) < 0 || + (pubkey = malloc(n + 1)) == NULL || + (n = snprintf(pubkey, n + 1, "%s.pub", privkey)) < 0) + goto out; + + error = git_credential_ssh_key_new(out, username, pubkey, privkey, password); + } else if (allowed_types & GIT_CREDENTIAL_USERPASS_PLAINTEXT) { + if ((error = ask(&password, "Password:", 1)) < 0) + goto out; + + error = git_credential_userpass_plaintext_new(out, username, password); + } else if (allowed_types & GIT_CREDENTIAL_USERNAME) { + error = git_credential_username_new(out, username); + } + +out: + free(username); + free(password); + free(privkey); + free(pubkey); + return error; +} + +char *read_file(const char *path) +{ + ssize_t total = 0; + char *buf = NULL; + struct stat st; + int fd = -1; + + if ((fd = open(path, O_RDONLY)) < 0 || fstat(fd, &st) < 0) + goto out; + + if ((buf = malloc(st.st_size + 1)) == NULL) + goto out; + + while (total < st.st_size) { + ssize_t bytes = read(fd, buf + total, st.st_size - total); + if (bytes <= 0) { + if (errno == EAGAIN || errno == EINTR) + continue; + free(buf); + buf = NULL; + goto out; + } + total += bytes; + } + + buf[total] = '\0'; + +out: + if (fd >= 0) + close(fd); + return buf; +} + diff --git a/examples/common.h b/examples/common.h new file mode 100644 index 00000000000..901c0414613 --- /dev/null +++ b/examples/common.h @@ -0,0 +1,136 @@ +/* + * Utilities library for libgit2 examples + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ +#ifndef INCLUDE_examples_common_h__ +#define INCLUDE_examples_common_h__ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +# include +# include +# define open _open +# define read _read +# define close _close +# define ssize_t int +# define sleep(a) Sleep(a * 1000) +#else +# include +#endif + +#ifndef PRIuZ +/* Define the printf format specifier to use for size_t output */ +#if defined(_MSC_VER) || defined(__MINGW32__) +# define PRIuZ "Iu" +#else +# define PRIuZ "zu" +#endif +#endif + +#ifdef _MSC_VER +#define snprintf _snprintf +#define strcasecmp strcmpi +#endif + +#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x)) +#define UNUSED(x) (void)(x) + +#include "args.h" + +extern int lg2_add(git_repository *repo, int argc, char **argv); +extern int lg2_blame(git_repository *repo, int argc, char **argv); +extern int lg2_cat_file(git_repository *repo, int argc, char **argv); +extern int lg2_checkout(git_repository *repo, int argc, char **argv); +extern int lg2_clone(git_repository *repo, int argc, char **argv); +extern int lg2_commit(git_repository *repo, int argc, char **argv); +extern int lg2_config(git_repository *repo, int argc, char **argv); +extern int lg2_describe(git_repository *repo, int argc, char **argv); +extern int lg2_diff(git_repository *repo, int argc, char **argv); +extern int lg2_fetch(git_repository *repo, int argc, char **argv); +extern int lg2_for_each_ref(git_repository *repo, int argc, char **argv); +extern int lg2_general(git_repository *repo, int argc, char **argv); +extern int lg2_index_pack(git_repository *repo, int argc, char **argv); +extern int lg2_init(git_repository *repo, int argc, char **argv); +extern int lg2_log(git_repository *repo, int argc, char **argv); +extern int lg2_ls_files(git_repository *repo, int argc, char **argv); +extern int lg2_ls_remote(git_repository *repo, int argc, char **argv); +extern int lg2_merge(git_repository *repo, int argc, char **argv); +extern int lg2_push(git_repository *repo, int argc, char **argv); +extern int lg2_remote(git_repository *repo, int argc, char **argv); +extern int lg2_rev_list(git_repository *repo, int argc, char **argv); +extern int lg2_rev_parse(git_repository *repo, int argc, char **argv); +extern int lg2_show_index(git_repository *repo, int argc, char **argv); +extern int lg2_stash(git_repository *repo, int argc, char **argv); +extern int lg2_status(git_repository *repo, int argc, char **argv); +extern int lg2_tag(git_repository *repo, int argc, char **argv); + +/** + * Check libgit2 error code, printing error to stderr on failure and + * exiting the program. + */ +extern void check_lg2(int error, const char *message, const char *extra); + +/** + * Read a file into a buffer + * + * @param path The path to the file that shall be read + * @return NUL-terminated buffer if the file was successfully read, NULL-pointer otherwise + */ +extern char *read_file(const char *path); + +/** + * Exit the program, printing error to stderr + */ +extern void fatal(const char *message, const char *extra); + +/** + * Basic output function for plain text diff output + * Pass `FILE*` such as `stdout` or `stderr` as payload (or NULL == `stdout`) + */ +extern int diff_output( + const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*); + +/** + * Convert a treeish argument to an actual tree; this will call check_lg2 + * and exit the program if `treeish` cannot be resolved to a tree + */ +extern void treeish_to_tree( + git_tree **out, git_repository *repo, const char *treeish); + +/** + * A realloc that exits on failure + */ +extern void *xrealloc(void *oldp, size_t newsz); + +/** + * Convert a refish to an annotated commit. + */ +extern int resolve_refish(git_annotated_commit **commit, git_repository *repo, const char *refish); + +/** + * Acquire credentials via command line + */ +extern int cred_acquire_cb(git_credential **out, + const char *url, + const char *username_from_url, + unsigned int allowed_types, + void *payload); + +#endif diff --git a/examples/config.c b/examples/config.c new file mode 100644 index 00000000000..6e14ce8c866 --- /dev/null +++ b/examples/config.c @@ -0,0 +1,71 @@ +/* + * libgit2 "config" example - shows how to use the config API + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +static int config_get(git_config *cfg, const char *key) +{ + git_config_entry *entry; + int error; + + if ((error = git_config_get_entry(&entry, cfg, key)) < 0) { + if (error != GIT_ENOTFOUND) + printf("Unable to get configuration: %s\n", git_error_last()->message); + return 1; + } + + puts(entry->value); + + /* Free the git_config_entry after use with `git_config_entry_free()`. */ + git_config_entry_free(entry); + + return 0; +} + +static int config_set(git_config *cfg, const char *key, const char *value) +{ + if (git_config_set_string(cfg, key, value) < 0) { + printf("Unable to set configuration: %s\n", git_error_last()->message); + return 1; + } + return 0; +} + +int lg2_config(git_repository *repo, int argc, char **argv) +{ + git_config *cfg; + int error; + + if ((error = git_repository_config(&cfg, repo)) < 0) { + printf("Unable to obtain repository config: %s\n", git_error_last()->message); + goto out; + } + + if (argc == 2) { + error = config_get(cfg, argv[1]); + } else if (argc == 3) { + error = config_set(cfg, argv[1], argv[2]); + } else { + printf("USAGE: %s config []\n", argv[0]); + error = 1; + } + + /** + * The configuration file must be freed once it's no longer + * being used by the user. + */ + git_config_free(cfg); +out: + return error; +} diff --git a/examples/describe.c b/examples/describe.c new file mode 100644 index 00000000000..1236272a190 --- /dev/null +++ b/examples/describe.c @@ -0,0 +1,162 @@ +/* + * libgit2 "describe" example - shows how to describe commits + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/** + * The following example partially reimplements the `git describe` command + * and some of its options. + * + * These commands should work: + + * - Describe HEAD with default options (`describe`) + * - Describe specified revision (`describe master~2`) + * - Describe specified revisions (`describe master~2 HEAD~3`) + * - Describe HEAD with dirty state suffix (`describe --dirty=*`) + * - Describe consider all refs (`describe --all master`) + * - Describe consider lightweight tags (`describe --tags temp-tag`) + * - Describe show non-default abbreviated size (`describe --abbrev=10`) + * - Describe always output the long format if matches a tag (`describe --long v1.0`) + * - Describe consider only tags of specified pattern (`describe --match v*-release`) + * - Describe show the fallback result (`describe --always`) + * - Describe follow only the first parent commit (`describe --first-parent`) + * + * The command line parsing logic is simplified and doesn't handle + * all of the use cases. + */ + +/** describe_options represents the parsed command line options */ +struct describe_options { + const char **commits; + size_t commit_count; + git_describe_options describe_options; + git_describe_format_options format_options; +}; + +static void opts_add_commit(struct describe_options *opts, const char *commit) +{ + size_t sz; + + assert(opts != NULL); + + sz = ++opts->commit_count * sizeof(opts->commits[0]); + opts->commits = xrealloc((void *) opts->commits, sz); + opts->commits[opts->commit_count - 1] = commit; +} + +static void do_describe_single(git_repository *repo, struct describe_options *opts, const char *rev) +{ + git_object *commit; + git_describe_result *describe_result; + git_buf buf = { 0 }; + + if (rev) { + check_lg2(git_revparse_single(&commit, repo, rev), + "Failed to lookup rev", rev); + + check_lg2(git_describe_commit(&describe_result, commit, &opts->describe_options), + "Failed to describe rev", rev); + } + else + check_lg2(git_describe_workdir(&describe_result, repo, &opts->describe_options), + "Failed to describe workdir", NULL); + + check_lg2(git_describe_format(&buf, describe_result, &opts->format_options), + "Failed to format describe rev", rev); + + printf("%s\n", buf.ptr); +} + +static void do_describe(git_repository *repo, struct describe_options *opts) +{ + if (opts->commit_count == 0) + do_describe_single(repo, opts, NULL); + else + { + size_t i; + for (i = 0; i < opts->commit_count; i++) + do_describe_single(repo, opts, opts->commits[i]); + } +} + +static void print_usage(void) +{ + fprintf(stderr, "usage: see `git help describe`\n"); + exit(1); +} + +/** Parse command line arguments */ +static void parse_options(struct describe_options *opts, int argc, char **argv) +{ + struct args_info args = ARGS_INFO_INIT; + + for (args.pos = 1; args.pos < argc; ++args.pos) { + const char *curr = argv[args.pos]; + + if (curr[0] != '-') { + opts_add_commit(opts, curr); + } else if (!strcmp(curr, "--all")) { + opts->describe_options.describe_strategy = GIT_DESCRIBE_ALL; + } else if (!strcmp(curr, "--tags")) { + opts->describe_options.describe_strategy = GIT_DESCRIBE_TAGS; + } else if (!strcmp(curr, "--exact-match")) { + opts->describe_options.max_candidates_tags = 0; + } else if (!strcmp(curr, "--long")) { + opts->format_options.always_use_long_format = 1; + } else if (!strcmp(curr, "--always")) { + opts->describe_options.show_commit_oid_as_fallback = 1; + } else if (!strcmp(curr, "--first-parent")) { + opts->describe_options.only_follow_first_parent = 1; + } else if (optional_str_arg(&opts->format_options.dirty_suffix, &args, "--dirty", "-dirty")) { + } else if (match_int_arg((int *)&opts->format_options.abbreviated_size, &args, "--abbrev", 0)) { + } else if (match_int_arg((int *)&opts->describe_options.max_candidates_tags, &args, "--candidates", 0)) { + } else if (match_str_arg(&opts->describe_options.pattern, &args, "--match")) { + } else { + print_usage(); + } + } + + if (opts->commit_count > 0) { + if (opts->format_options.dirty_suffix) + fatal("--dirty is incompatible with commit-ishes", NULL); + } + else { + if (!opts->format_options.dirty_suffix || !opts->format_options.dirty_suffix[0]) { + opts_add_commit(opts, "HEAD"); + } + } +} + +/** Initialize describe_options struct */ +static void describe_options_init(struct describe_options *opts) +{ + memset(opts, 0, sizeof(*opts)); + + opts->commits = NULL; + opts->commit_count = 0; + git_describe_options_init(&opts->describe_options, GIT_DESCRIBE_OPTIONS_VERSION); + git_describe_format_options_init(&opts->format_options, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION); +} + +int lg2_describe(git_repository *repo, int argc, char **argv) +{ + struct describe_options opts; + + describe_options_init(&opts); + parse_options(&opts, argc, argv); + + do_describe(repo, &opts); + + return 0; +} diff --git a/examples/diff.c b/examples/diff.c index 17bf8427e5e..ed8fbd60d42 100644 --- a/examples/diff.c +++ b/examples/diff.c @@ -1,60 +1,31 @@ -#include -#include -#include -#include +/* + * libgit2 "diff" example - shows how to use the diff API + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ -static void check(int error, const char *message) -{ - if (error) { - fprintf(stderr, "%s (%d)\n", message, error); - exit(1); - } -} +#include "common.h" -static int resolve_to_tree( - git_repository *repo, const char *identifier, git_tree **tree) -{ - int err = 0; - size_t len = strlen(identifier); - git_oid oid; - git_object *obj = NULL; - - /* try to resolve as OID */ - if (git_oid_fromstrn(&oid, identifier, len) == 0) - git_object_lookup_prefix(&obj, repo, &oid, len, GIT_OBJ_ANY); - - /* try to resolve as reference */ - if (obj == NULL) { - git_reference *ref, *resolved; - if (git_reference_lookup(&ref, repo, identifier) == 0) { - git_reference_resolve(&resolved, ref); - git_reference_free(ref); - if (resolved) { - git_object_lookup(&obj, repo, git_reference_target(resolved), GIT_OBJ_ANY); - git_reference_free(resolved); - } - } - } +/** + * This example demonstrates the use of the libgit2 diff APIs to + * create `git_diff` objects and display them, emulating a number of + * core Git `diff` command line options. + * + * This covers on a portion of the core Git diff options and doesn't + * have particularly good error handling, but it should show most of + * the core libgit2 diff APIs, including various types of diffs and + * how to do renaming detection and patch formatting. + */ - if (obj == NULL) - return GIT_ENOTFOUND; - - switch (git_object_type(obj)) { - case GIT_OBJ_TREE: - *tree = (git_tree *)obj; - break; - case GIT_OBJ_COMMIT: - err = git_commit_tree(tree, (git_commit *)obj); - git_object_free(obj); - break; - default: - err = GIT_ENOTFOUND; - } - - return err; -} - -char *colors[] = { +static const char *colors[] = { "\033[m", /* reset */ "\033[1m", /* bold */ "\033[31m", /* red */ @@ -62,61 +33,170 @@ char *colors[] = { "\033[36m" /* cyan */ }; -static int printer( - const git_diff_delta *delta, - const git_diff_range *range, - char usage, - const char *line, - size_t line_len, - void *data) +enum { + OUTPUT_DIFF = (1 << 0), + OUTPUT_STAT = (1 << 1), + OUTPUT_SHORTSTAT = (1 << 2), + OUTPUT_NUMSTAT = (1 << 3), + OUTPUT_SUMMARY = (1 << 4) +}; + +enum { + CACHE_NORMAL = 0, + CACHE_ONLY = 1, + CACHE_NONE = 2 +}; + +/** The 'diff_options' struct captures all the various parsed command line options. */ +struct diff_options { + git_diff_options diffopts; + git_diff_find_options findopts; + int color; + int no_index; + int cache; + int output; + git_diff_format_t format; + const char *treeish1; + const char *treeish2; + const char *dir; +}; + +/** These functions are implemented at the end */ +static void usage(const char *message, const char *arg); +static void parse_opts(struct diff_options *o, int argc, char *argv[]); +static int color_printer( + const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*); +static void diff_print_stats(git_diff *diff, struct diff_options *o); +static void compute_diff_no_index(git_diff **diff, struct diff_options *o); + +int lg2_diff(git_repository *repo, int argc, char *argv[]) { - int *last_color = data, color = 0; + git_tree *t1 = NULL, *t2 = NULL; + git_diff *diff; + struct diff_options o = { + GIT_DIFF_OPTIONS_INIT, GIT_DIFF_FIND_OPTIONS_INIT, + -1, -1, 0, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "." + }; - (void)delta; (void)range; (void)line_len; + parse_opts(&o, argc, argv); - if (*last_color >= 0) { - switch (usage) { - case GIT_DIFF_LINE_ADDITION: color = 3; break; - case GIT_DIFF_LINE_DELETION: color = 2; break; - case GIT_DIFF_LINE_ADD_EOFNL: color = 3; break; - case GIT_DIFF_LINE_DEL_EOFNL: color = 2; break; - case GIT_DIFF_LINE_FILE_HDR: color = 1; break; - case GIT_DIFF_LINE_HUNK_HDR: color = 4; break; - default: color = 0; - } - if (color != *last_color) { - if (*last_color == 1 || color == 1) - fputs(colors[0], stdout); - fputs(colors[color], stdout); - *last_color = color; + /** + * Possible argument patterns: + * + * * <sha1> <sha2> + * * <sha1> --cached + * * <sha1> + * * --cached + * * --nocache (don't use index data in diff at all) + * * --no-index <file1> <file2> + * * nothing + * + * Currently ranged arguments like <sha1>..<sha2> and <sha1>...<sha2> + * are not supported in this example + */ + + if (o.no_index >= 0) { + compute_diff_no_index(&diff, &o); + } else { + if (o.treeish1) + treeish_to_tree(&t1, repo, o.treeish1); + if (o.treeish2) + treeish_to_tree(&t2, repo, o.treeish2); + + if (t1 && t2) + check_lg2( + git_diff_tree_to_tree(&diff, repo, t1, t2, &o.diffopts), + "diff trees", NULL); + else if (o.cache != CACHE_NORMAL) { + if (!t1) + treeish_to_tree(&t1, repo, "HEAD"); + + if (o.cache == CACHE_NONE) + check_lg2( + git_diff_tree_to_workdir(&diff, repo, t1, &o.diffopts), + "diff tree to working directory", NULL); + else + check_lg2( + git_diff_tree_to_index(&diff, repo, t1, NULL, &o.diffopts), + "diff tree to index", NULL); } + else if (t1) + check_lg2( + git_diff_tree_to_workdir_with_index(&diff, repo, t1, &o.diffopts), + "diff tree to working directory", NULL); + else + check_lg2( + git_diff_index_to_workdir(&diff, repo, NULL, &o.diffopts), + "diff index to working directory", NULL); + + /** Apply rename and copy detection if requested. */ + + if ((o.findopts.flags & GIT_DIFF_FIND_ALL) != 0) + check_lg2( + git_diff_find_similar(diff, &o.findopts), + "finding renames and copies", NULL); + } + + /** Generate simple output using libgit2 display helper. */ + + if (!o.output) + o.output = OUTPUT_DIFF; + + if (o.output != OUTPUT_DIFF) + diff_print_stats(diff, &o); + + if ((o.output & OUTPUT_DIFF) != 0) { + if (o.color >= 0) + fputs(colors[0], stdout); + + check_lg2( + git_diff_print(diff, o.format, color_printer, &o.color), + "displaying diff", NULL); + + if (o.color >= 0) + fputs(colors[0], stdout); } - fputs(line, stdout); + /** Cleanup before exiting. */ + git_diff_free(diff); + git_tree_free(t1); + git_tree_free(t2); + return 0; } -static int check_uint16_param(const char *arg, const char *pattern, uint16_t *val) -{ - size_t len = strlen(pattern); - uint16_t strval; - char *endptr = NULL; - if (strncmp(arg, pattern, len)) - return 0; - strval = strtoul(arg + len, &endptr, 0); - if (endptr == arg) - return 0; - *val = strval; - return 1; -} +static void compute_diff_no_index(git_diff **diff, struct diff_options *o) { + git_patch *patch = NULL; + char *file1_str = NULL; + char *file2_str = NULL; + git_buf buf = {0}; -static int check_str_param(const char *arg, const char *pattern, const char **val) -{ - size_t len = strlen(pattern); - if (strncmp(arg, pattern, len)) - return 0; - *val = (const char *)(arg + len); - return 1; + if (!o->treeish1 || !o->treeish2) { + usage("two files should be provided as arguments", NULL); + } + file1_str = read_file(o->treeish1); + if (file1_str == NULL) { + usage("file cannot be read", o->treeish1); + } + file2_str = read_file(o->treeish2); + if (file2_str == NULL) { + usage("file cannot be read", o->treeish2); + } + check_lg2( + git_patch_from_buffers(&patch, file1_str, strlen(file1_str), o->treeish1, file2_str, strlen(file2_str), o->treeish2, &o->diffopts), + "patch buffers", NULL); + check_lg2( + git_patch_to_buf(&buf, patch), + "patch to buf", NULL); + + check_lg2( + git_diff_from_buffer(diff, buf.ptr, buf.size), + "diff from patch", NULL); + + git_patch_free(patch); + git_buf_dispose(&buf); + free(file1_str); + free(file2_str); } static void usage(const char *message, const char *arg) @@ -129,112 +209,163 @@ static void usage(const char *message, const char *arg) exit(1); } -int main(int argc, char *argv[]) +/** This implements very rudimentary colorized output. */ +static int color_printer( + const git_diff_delta *delta, + const git_diff_hunk *hunk, + const git_diff_line *line, + void *data) { - git_repository *repo = NULL; - git_tree *t1 = NULL, *t2 = NULL; - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff; - int i, color = -1, compact = 0, cached = 0; - char *a, *dir = ".", *treeish1 = NULL, *treeish2 = NULL; + int *last_color = data, color = 0; + + (void)delta; (void)hunk; + + if (*last_color >= 0) { + switch (line->origin) { + case GIT_DIFF_LINE_ADDITION: color = 3; break; + case GIT_DIFF_LINE_DELETION: color = 2; break; + case GIT_DIFF_LINE_ADD_EOFNL: color = 3; break; + case GIT_DIFF_LINE_DEL_EOFNL: color = 2; break; + case GIT_DIFF_LINE_FILE_HDR: color = 1; break; + case GIT_DIFF_LINE_HUNK_HDR: color = 4; break; + default: break; + } - /* parse arguments as copied from git-diff */ + if (color != *last_color) { + if (*last_color == 1 || color == 1) + fputs(colors[0], stdout); + fputs(colors[color], stdout); + *last_color = color; + } + } + + return diff_output(delta, hunk, line, stdout); +} + +/** Parse arguments as copied from git-diff. */ +static void parse_opts(struct diff_options *o, int argc, char *argv[]) +{ + struct args_info args = ARGS_INFO_INIT; - for (i = 1; i < argc; ++i) { - a = argv[i]; + for (args.pos = 1; args.pos < argc; ++args.pos) { + const char *a = argv[args.pos]; if (a[0] != '-') { - if (treeish1 == NULL) - treeish1 = a; - else if (treeish2 == NULL) - treeish2 = a; + if (o->treeish1 == NULL) + o->treeish1 = a; + else if (o->treeish2 == NULL) + o->treeish2 = a; else usage("Only one or two tree identifiers can be provided", NULL); } else if (!strcmp(a, "-p") || !strcmp(a, "-u") || - !strcmp(a, "--patch")) - compact = 0; - else if (!strcmp(a, "--cached")) - cached = 1; - else if (!strcmp(a, "--name-status")) - compact = 1; - else if (!strcmp(a, "--color")) - color = 0; + !strcmp(a, "--patch")) { + o->output |= OUTPUT_DIFF; + o->format = GIT_DIFF_FORMAT_PATCH; + } + else if (!strcmp(a, "--cached")) { + o->cache = CACHE_ONLY; + if (o->no_index >= 0) usage("--cached and --no-index are incompatible", NULL); + } else if (!strcmp(a, "--nocache")) + o->cache = CACHE_NONE; + else if (!strcmp(a, "--name-only") || !strcmp(a, "--format=name")) + o->format = GIT_DIFF_FORMAT_NAME_ONLY; + else if (!strcmp(a, "--name-status") || + !strcmp(a, "--format=name-status")) + o->format = GIT_DIFF_FORMAT_NAME_STATUS; + else if (!strcmp(a, "--raw") || !strcmp(a, "--format=raw")) + o->format = GIT_DIFF_FORMAT_RAW; + else if (!strcmp(a, "--format=diff-index")) { + o->format = GIT_DIFF_FORMAT_RAW; + o->diffopts.id_abbrev = 40; + } + else if (!strcmp(a, "--no-index")) { + o->no_index = 0; + if (o->cache == CACHE_ONLY) usage("--cached and --no-index are incompatible", NULL); + } else if (!strcmp(a, "--color")) + o->color = 0; else if (!strcmp(a, "--no-color")) - color = -1; + o->color = -1; else if (!strcmp(a, "-R")) - opts.flags |= GIT_DIFF_REVERSE; + o->diffopts.flags |= GIT_DIFF_REVERSE; else if (!strcmp(a, "-a") || !strcmp(a, "--text")) - opts.flags |= GIT_DIFF_FORCE_TEXT; + o->diffopts.flags |= GIT_DIFF_FORCE_TEXT; else if (!strcmp(a, "--ignore-space-at-eol")) - opts.flags |= GIT_DIFF_IGNORE_WHITESPACE_EOL; + o->diffopts.flags |= GIT_DIFF_IGNORE_WHITESPACE_EOL; else if (!strcmp(a, "-b") || !strcmp(a, "--ignore-space-change")) - opts.flags |= GIT_DIFF_IGNORE_WHITESPACE_CHANGE; + o->diffopts.flags |= GIT_DIFF_IGNORE_WHITESPACE_CHANGE; else if (!strcmp(a, "-w") || !strcmp(a, "--ignore-all-space")) - opts.flags |= GIT_DIFF_IGNORE_WHITESPACE; + o->diffopts.flags |= GIT_DIFF_IGNORE_WHITESPACE; else if (!strcmp(a, "--ignored")) - opts.flags |= GIT_DIFF_INCLUDE_IGNORED; + o->diffopts.flags |= GIT_DIFF_INCLUDE_IGNORED; else if (!strcmp(a, "--untracked")) - opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED; - else if (!check_uint16_param(a, "-U", &opts.context_lines) && - !check_uint16_param(a, "--unified=", &opts.context_lines) && - !check_uint16_param(a, "--inter-hunk-context=", - &opts.interhunk_lines) && - !check_str_param(a, "--src-prefix=", &opts.old_prefix) && - !check_str_param(a, "--dst-prefix=", &opts.new_prefix)) - usage("Unknown arg", a); + o->diffopts.flags |= GIT_DIFF_INCLUDE_UNTRACKED; + else if (!strcmp(a, "--patience")) + o->diffopts.flags |= GIT_DIFF_PATIENCE; + else if (!strcmp(a, "--minimal")) + o->diffopts.flags |= GIT_DIFF_MINIMAL; + else if (!strcmp(a, "--stat")) + o->output |= OUTPUT_STAT; + else if (!strcmp(a, "--numstat")) + o->output |= OUTPUT_NUMSTAT; + else if (!strcmp(a, "--shortstat")) + o->output |= OUTPUT_SHORTSTAT; + else if (!strcmp(a, "--summary")) + o->output |= OUTPUT_SUMMARY; + else if (match_uint16_arg( + &o->findopts.rename_threshold, &args, "-M") || + match_uint16_arg( + &o->findopts.rename_threshold, &args, "--find-renames")) + o->findopts.flags |= GIT_DIFF_FIND_RENAMES; + else if (match_uint16_arg( + &o->findopts.copy_threshold, &args, "-C") || + match_uint16_arg( + &o->findopts.copy_threshold, &args, "--find-copies")) + o->findopts.flags |= GIT_DIFF_FIND_COPIES; + else if (!strcmp(a, "--find-copies-harder")) + o->findopts.flags |= GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED; + else if (is_prefixed(a, "-B") || is_prefixed(a, "--break-rewrites")) + /* TODO: parse thresholds */ + o->findopts.flags |= GIT_DIFF_FIND_REWRITES; + else if (!match_uint32_arg( + &o->diffopts.context_lines, &args, "-U") && + !match_uint32_arg( + &o->diffopts.context_lines, &args, "--unified") && + !match_uint32_arg( + &o->diffopts.interhunk_lines, &args, "--inter-hunk-context") && + !match_uint16_arg( + &o->diffopts.id_abbrev, &args, "--abbrev") && + !match_str_arg(&o->diffopts.old_prefix, &args, "--src-prefix") && + !match_str_arg(&o->diffopts.new_prefix, &args, "--dst-prefix") && + !match_str_arg(&o->dir, &args, "--git-dir")) + usage("Unknown command line argument", a); } +} - /* open repo */ - - check(git_repository_open_ext(&repo, dir, 0, NULL), - "Could not open repository"); - - if (treeish1) - check(resolve_to_tree(repo, treeish1, &t1), "Looking up first tree"); - if (treeish2) - check(resolve_to_tree(repo, treeish2, &t2), "Looking up second tree"); - - /* */ - /* --cached */ - /* */ - /* --cached */ - /* nothing */ - - if (t1 && t2) - check(git_diff_tree_to_tree(&diff, repo, t1, t2, &opts), "Diff"); - else if (t1 && cached) - check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff"); - else if (t1) { - git_diff_list *diff2; - check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff"); - check(git_diff_index_to_workdir(&diff2, repo, NULL, &opts), "Diff"); - check(git_diff_merge(diff, diff2), "Merge diffs"); - git_diff_list_free(diff2); - } - else if (cached) { - check(resolve_to_tree(repo, "HEAD", &t1), "looking up HEAD"); - check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff"); - } - else - check(git_diff_index_to_workdir(&diff, repo, NULL, &opts), "Diff"); +/** Display diff output with "--stat", "--numstat", or "--shortstat" */ +static void diff_print_stats(git_diff *diff, struct diff_options *o) +{ + git_diff_stats *stats; + git_buf b = GIT_BUF_INIT; + git_diff_stats_format_t format = 0; - if (color >= 0) - fputs(colors[0], stdout); + check_lg2( + git_diff_get_stats(&stats, diff), "generating stats for diff", NULL); - if (compact) - check(git_diff_print_compact(diff, printer, &color), "Displaying diff"); - else - check(git_diff_print_patch(diff, printer, &color), "Displaying diff"); + if (o->output & OUTPUT_STAT) + format |= GIT_DIFF_STATS_FULL; + if (o->output & OUTPUT_SHORTSTAT) + format |= GIT_DIFF_STATS_SHORT; + if (o->output & OUTPUT_NUMSTAT) + format |= GIT_DIFF_STATS_NUMBER; + if (o->output & OUTPUT_SUMMARY) + format |= GIT_DIFF_STATS_INCLUDE_SUMMARY; - if (color >= 0) - fputs(colors[0], stdout); + check_lg2( + git_diff_stats_to_buf(&b, stats, format, 80), "formatting stats", NULL); - git_diff_list_free(diff); - git_tree_free(t1); - git_tree_free(t2); - git_repository_free(repo); + fputs(b.ptr, stdout); - return 0; + git_buf_dispose(&b); + git_diff_stats_free(stats); } - diff --git a/examples/fetch.c b/examples/fetch.c new file mode 100644 index 00000000000..a8b3527a8cb --- /dev/null +++ b/examples/fetch.c @@ -0,0 +1,113 @@ +#include "common.h" + +static int progress_cb(const char *str, int len, void *data) +{ + (void)data; + printf("remote: %.*s", len, str); + fflush(stdout); /* We don't have the \n to force the flush */ + return 0; +} + +/** + * This function gets called for each remote-tracking branch that gets + * updated. The message we output depends on whether it's a new one or + * an update. + */ +static int update_cb(const char *refname, const git_oid *a, const git_oid *b, git_refspec *spec, void *data) +{ + char a_str[GIT_OID_SHA1_HEXSIZE+1], b_str[GIT_OID_SHA1_HEXSIZE+1]; + git_buf remote_name; + (void)data; + + if (git_refspec_rtransform(&remote_name, spec, refname) < 0) + return -1; + + git_oid_fmt(b_str, b); + b_str[GIT_OID_SHA1_HEXSIZE] = '\0'; + + if (git_oid_is_zero(a)) { + printf("[new] %.20s %s -> %s\n", b_str, remote_name.ptr, refname); + } else { + git_oid_fmt(a_str, a); + a_str[GIT_OID_SHA1_HEXSIZE] = '\0'; + printf("[updated] %.10s..%.10s %s -> %s\n", a_str, b_str, remote_name.ptr, refname); + } + + return 0; +} + +/** + * This gets called during the download and indexing. Here we show + * processed and total objects in the pack and the amount of received + * data. Most frontends will probably want to show a percentage and + * the download rate. + */ +static int transfer_progress_cb(const git_indexer_progress *stats, void *payload) +{ + (void)payload; + + if (stats->received_objects == stats->total_objects) { + printf("Resolving deltas %u/%u\r", + stats->indexed_deltas, stats->total_deltas); + } else if (stats->total_objects > 0) { + printf("Received %u/%u objects (%u) in %" PRIuZ " bytes\r", + stats->received_objects, stats->total_objects, + stats->indexed_objects, stats->received_bytes); + } + return 0; +} + +/** Entry point for this command */ +int lg2_fetch(git_repository *repo, int argc, char **argv) +{ + git_remote *remote = NULL; + const git_indexer_progress *stats; + git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT; + + if (argc < 2) { + fprintf(stderr, "usage: %s fetch \n", argv[-1]); + return EXIT_FAILURE; + } + + /* Figure out whether it's a named remote or a URL */ + printf("Fetching %s for repo %p\n", argv[1], repo); + if (git_remote_lookup(&remote, repo, argv[1]) < 0) + if (git_remote_create_anonymous(&remote, repo, argv[1]) < 0) + goto on_error; + + /* Set up the callbacks (only update_tips for now) */ + fetch_opts.callbacks.update_refs = &update_cb; + fetch_opts.callbacks.sideband_progress = &progress_cb; + fetch_opts.callbacks.transfer_progress = transfer_progress_cb; + fetch_opts.callbacks.credentials = cred_acquire_cb; + + /** + * Perform the fetch with the configured refspecs from the + * config. Update the reflog for the updated references with + * "fetch". + */ + if (git_remote_fetch(remote, NULL, &fetch_opts, "fetch") < 0) + goto on_error; + + /** + * If there are local objects (we got a thin pack), then tell + * the user how many objects we saved from having to cross the + * network. + */ + stats = git_remote_stats(remote); + if (stats->local_objects > 0) { + printf("\rReceived %u/%u objects in %" PRIuZ " bytes (used %u local objects)\n", + stats->indexed_objects, stats->total_objects, stats->received_bytes, stats->local_objects); + } else{ + printf("\rReceived %u/%u objects in %" PRIuZ "bytes\n", + stats->indexed_objects, stats->total_objects, stats->received_bytes); + } + + git_remote_free(remote); + + return 0; + + on_error: + git_remote_free(remote); + return -1; +} diff --git a/examples/for-each-ref.c b/examples/for-each-ref.c new file mode 100644 index 00000000000..f745bc38cb9 --- /dev/null +++ b/examples/for-each-ref.c @@ -0,0 +1,46 @@ +#include +#include "common.h" + +static int show_ref(git_reference *ref, void *data) +{ + git_repository *repo = data; + git_reference *resolved = NULL; + char hex[GIT_OID_SHA1_HEXSIZE+1]; + const git_oid *oid; + git_object *obj; + + if (git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC) + check_lg2(git_reference_resolve(&resolved, ref), + "Unable to resolve symbolic reference", + git_reference_name(ref)); + + oid = git_reference_target(resolved ? resolved : ref); + git_oid_fmt(hex, oid); + hex[GIT_OID_SHA1_HEXSIZE] = 0; + check_lg2(git_object_lookup(&obj, repo, oid, GIT_OBJECT_ANY), + "Unable to lookup object", hex); + + printf("%s %-6s\t%s\n", + hex, + git_object_type2string(git_object_type(obj)), + git_reference_name(ref)); + + git_object_free(obj); + git_reference_free(ref); + if (resolved) + git_reference_free(resolved); + return 0; +} + +int lg2_for_each_ref(git_repository *repo, int argc, char **argv) +{ + UNUSED(argv); + + if (argc != 1) + fatal("Sorry, no for-each-ref options supported yet", NULL); + + check_lg2(git_reference_foreach(repo, show_ref, repo), + "Could not iterate over references", NULL); + + return 0; +} diff --git a/examples/general.c b/examples/general.c index 9ea264ec610..727a406ed3d 100644 --- a/examples/general.c +++ b/examples/general.c @@ -1,451 +1,798 @@ -// [**libgit2**][lg] is a portable, pure C implementation of the Git core methods -// provided as a re-entrant linkable library with a solid API, allowing you -// to write native speed custom Git applications in any language which -// supports C bindings. -// -// This file is an example of using that API in a real, compilable C file. -// As the API is updated, this file will be updated to demonstrate the -// new functionality. -// -// If you're trying to write something in C using [libgit2][lg], you will also want -// to check out the generated [API documentation][ap] and the [Usage Guide][ug]. We've -// tried to link to the relevant sections of the API docs in each section in this file. -// -// **libgit2** only implements the core plumbing functions, not really the higher -// level porcelain stuff. For a primer on Git Internals that you will need to know -// to work with Git at this level, check out [Chapter 9][pg] of the Pro Git book. -// -// [lg]: http://libgit2.github.com -// [ap]: http://libgit2.github.com/libgit2 -// [ug]: http://libgit2.github.com/api.html -// [pg]: http://progit.org/book/ch9-0.html - -// ### Includes - -// Including the `git2.h` header will include all the other libgit2 headers that you need. -// It should be the only thing you need to include in order to compile properly and get -// all the libgit2 API. -#include -#include - -int main (int argc, char** argv) +/* + * libgit2 "general" example - shows basic libgit2 concepts + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +/** + * [**libgit2**][lg] is a portable, pure C implementation of the Git core + * methods provided as a re-entrant linkable library with a solid API, + * allowing you to write native speed custom Git applications in any + * language which supports C bindings. + * + * This file is an example of using that API in a real, compilable C file. + * As the API is updated, this file will be updated to demonstrate the new + * functionality. + * + * If you're trying to write something in C using [libgit2][lg], you should + * also check out the generated [API documentation][ap]. We try to link to + * the relevant sections of the API docs in each section in this file. + * + * **libgit2** (for the most part) only implements the core plumbing + * functions, not really the higher level porcelain stuff. For a primer on + * Git Internals that you will need to know to work with Git at this level, + * check out [Chapter 10][pg] of the Pro Git book. + * + * [lg]: https://libgit2.org + * [ap]: https://libgit2.org/libgit2 + * [pg]: https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain + */ + +#include "common.h" + +/** + * ### Includes + * + * Including the `git2.h` header will include all the other libgit2 headers + * that you need. It should be the only thing you need to include in order + * to compile properly and get all the libgit2 API. + */ +#include "git2.h" + +static void oid_parsing(git_oid *out); +static void object_database(git_repository *repo, git_oid *oid); +static void commit_writing(git_repository *repo); +static void commit_parsing(git_repository *repo); +static void tag_parsing(git_repository *repo); +static void tree_parsing(git_repository *repo); +static void blob_parsing(git_repository *repo); +static void revwalking(git_repository *repo); +static void index_walking(git_repository *repo); +static void reference_listing(git_repository *repo); +static void config_files(const char *repo_path, git_repository *repo); + +/** + * Almost all libgit2 functions return 0 on success or negative on error. + * This is not production quality error checking, but should be sufficient + * as an example. + */ +static void check_error(int error_code, const char *action) { - // ### Opening the Repository - - // There are a couple of methods for opening a repository, this being the simplest. - // There are also [methods][me] for specifying the index file and work tree locations, here - // we are assuming they are in the normal places. - // - // [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository - git_repository *repo; - if (argc > 1) { - git_repository_open(&repo, argv[1]); - } else { - git_repository_open(&repo, "/opt/libgit2-test/.git"); - } - - // ### SHA-1 Value Conversions - - // For our first example, we will convert a 40 character hex value to the 20 byte raw SHA1 value. - printf("*Hex to Raw*\n"); - char hex[] = "fd6e612585290339ea8bf39c692a7ff6a29cb7c3"; - - // The `git_oid` is the structure that keeps the SHA value. We will use this throughout the example - // for storing the value of the current SHA key we're working with. - git_oid oid; - git_oid_fromstr(&oid, hex); - - // Once we've converted the string into the oid value, we can get the raw value of the SHA. - printf("Raw 20 bytes: [%.20s]\n", (&oid)->id); - - // Next we will convert the 20 byte raw SHA1 value to a human readable 40 char hex value. - printf("\n*Raw to Hex*\n"); - char out[41]; - out[40] = '\0'; - - // If you have a oid, you can easily get the hex value of the SHA as well. - git_oid_fmt(out, &oid); - printf("SHA hex string: %s\n", out); - - // ### Working with the Object Database - // **libgit2** provides [direct access][odb] to the object database. - // The object database is where the actual objects are stored in Git. For - // working with raw objects, we'll need to get this structure from the - // repository. - // [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb - git_odb *odb; - git_repository_odb(&odb, repo); - - // #### Raw Object Reading - - printf("\n*Raw Object Read*\n"); - git_odb_object *obj; - git_otype otype; - const unsigned char *data; - const char *str_type; - int error; - - // We can read raw objects directly from the object database if we have the oid (SHA) - // of the object. This allows us to access objects without knowing thier type and inspect - // the raw bytes unparsed. - error = git_odb_read(&obj, odb, &oid); - - // A raw object only has three properties - the type (commit, blob, tree or tag), the size - // of the raw data and the raw, unparsed data itself. For a commit or tag, that raw data - // is human readable plain ASCII text. For a blob it is just file contents, so it could be - // text or binary data. For a tree it is a special binary format, so it's unlikely to be - // hugely helpful as a raw object. - data = (const unsigned char *)git_odb_object_data(obj); - otype = git_odb_object_type(obj); - - // We provide methods to convert from the object type which is an enum, to a string - // representation of that value (and vice-versa). - str_type = git_object_type2string(otype); - printf("object length and type: %d, %s\n", - (int)git_odb_object_size(obj), - str_type); - - // For proper memory management, close the object when you are done with it or it will leak - // memory. - git_odb_object_free(obj); - - // #### Raw Object Writing - - printf("\n*Raw Object Write*\n"); - - // You can also write raw object data to Git. This is pretty cool because it gives you - // direct access to the key/value properties of Git. Here we'll write a new blob object - // that just contains a simple string. Notice that we have to specify the object type as - // the `git_otype` enum. - git_odb_write(&oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB); - - // Now that we've written the object, we can check out what SHA1 was generated when the - // object was written to our database. - git_oid_fmt(out, &oid); - printf("Written Object: %s\n", out); - - // ### Object Parsing - // libgit2 has methods to parse every object type in Git so you don't have to work directly - // with the raw data. This is much faster and simpler than trying to deal with the raw data - // yourself. - - // #### Commit Parsing - // [Parsing commit objects][pco] is simple and gives you access to all the data in the commit - // - the // author (name, email, datetime), committer (same), tree, message, encoding and parent(s). - // [pco]: http://libgit2.github.com/libgit2/#HEAD/group/commit - - printf("\n*Commit Parsing*\n"); - - git_commit *commit; - git_oid_fromstr(&oid, "f0877d0b841d75172ec404fc9370173dfffc20d1"); - - error = git_commit_lookup(&commit, repo, &oid); - - const git_signature *author, *cmtter; - const char *message; - time_t ctime; - unsigned int parents, p; - - // Each of the properties of the commit object are accessible via methods, including commonly - // needed variations, such as `git_commit_time` which returns the author time and `_message` - // which gives you the commit message. - message = git_commit_message(commit); - author = git_commit_author(commit); - cmtter = git_commit_committer(commit); - ctime = git_commit_time(commit); - - // The author and committer methods return [git_signature] structures, which give you name, email - // and `when`, which is a `git_time` structure, giving you a timestamp and timezone offset. - printf("Author: %s (%s)\n", author->name, author->email); - - // Commits can have zero or more parents. The first (root) commit will have no parents, most commits - // will have one, which is the commit it was based on, and merge commits will have two or more. - // Commits can technically have any number, though it's pretty rare to have more than two. - parents = git_commit_parentcount(commit); - for (p = 0;p < parents;p++) { - git_commit *parent; - git_commit_parent(&parent, commit, p); - git_oid_fmt(out, git_commit_id(parent)); - printf("Parent: %s\n", out); - git_commit_free(parent); - } - - // Don't forget to close the object to prevent memory leaks. You will have to do this for - // all the objects you open and parse. - git_commit_free(commit); - - // #### Writing Commits - // - // libgit2 provides a couple of methods to create commit objects easily as well. There are four - // different create signatures, we'll just show one of them here. You can read about the other - // ones in the [commit API docs][cd]. - // [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit - - printf("\n*Commit Writing*\n"); - git_oid tree_id, parent_id, commit_id; - git_tree *tree; - git_commit *parent; - - // Creating signatures for an authoring identity and time is pretty simple - you will need to have - // this to create a commit in order to specify who created it and when. Default values for the name - // and email should be found in the `user.name` and `user.email` configuration options. See the `config` - // section of this example file to see how to access config values. - git_signature_new((git_signature **)&author, "Scott Chacon", "schacon@gmail.com", - 123456789, 60); - git_signature_new((git_signature **)&cmtter, "Scott A Chacon", "scott@github.com", - 987654321, 90); - - // Commit objects need a tree to point to and optionally one or more parents. Here we're creating oid - // objects to create the commit with, but you can also use - git_oid_fromstr(&tree_id, "28873d96b4e8f4e33ea30f4c682fd325f7ba56ac"); - git_tree_lookup(&tree, repo, &tree_id); - git_oid_fromstr(&parent_id, "f0877d0b841d75172ec404fc9370173dfffc20d1"); - git_commit_lookup(&parent, repo, &parent_id); - - // Here we actually create the commit object with a single call with all the values we need to create - // the commit. The SHA key is written to the `commit_id` variable here. - git_commit_create_v( - &commit_id, /* out id */ - repo, - NULL, /* do not update the HEAD */ - author, - cmtter, - NULL, /* use default message encoding */ - "example commit", - tree, - 1, parent); - - // Now we can take a look at the commit SHA we've generated. - git_oid_fmt(out, &commit_id); - printf("New Commit: %s\n", out); - - // #### Tag Parsing - // You can parse and create tags with the [tag management API][tm], which functions very similarly - // to the commit lookup, parsing and creation methods, since the objects themselves are very similar. - // [tm]: http://libgit2.github.com/libgit2/#HEAD/group/tag - printf("\n*Tag Parsing*\n"); - git_tag *tag; - const char *tmessage, *tname; - git_otype ttype; - - // We create an oid for the tag object if we know the SHA and look it up in the repository the same - // way that we would a commit (or any other) object. - git_oid_fromstr(&oid, "bc422d45275aca289c51d79830b45cecebff7c3a"); - - error = git_tag_lookup(&tag, repo, &oid); - - // Now that we have the tag object, we can extract the information it generally contains: the target - // (usually a commit object), the type of the target object (usually 'commit'), the name ('v1.0'), - // the tagger (a git_signature - name, email, timestamp), and the tag message. - git_tag_target((git_object **)&commit, tag); - tname = git_tag_name(tag); // "test" - ttype = git_tag_target_type(tag); // GIT_OBJ_COMMIT (otype enum) - tmessage = git_tag_message(tag); // "tag message\n" - printf("Tag Message: %s\n", tmessage); - - git_commit_free(commit); - - // #### Tree Parsing - // [Tree parsing][tp] is a bit different than the other objects, in that we have a subtype which is the - // tree entry. This is not an actual object type in Git, but a useful structure for parsing and - // traversing tree entries. - // - // [tp]: http://libgit2.github.com/libgit2/#HEAD/group/tree - printf("\n*Tree Parsing*\n"); - - const git_tree_entry *entry; - git_object *objt; - - // Create the oid and lookup the tree object just like the other objects. - git_oid_fromstr(&oid, "2a741c18ac5ff082a7caaec6e74db3075a1906b5"); - git_tree_lookup(&tree, repo, &oid); - - // Getting the count of entries in the tree so you can iterate over them if you want to. - size_t cnt = git_tree_entrycount(tree); // 3 - printf("tree entries: %d\n", (int)cnt); - - entry = git_tree_entry_byindex(tree, 0); - printf("Entry name: %s\n", git_tree_entry_name(entry)); // "hello.c" - - // You can also access tree entries by name if you know the name of the entry you're looking for. - entry = git_tree_entry_byname(tree, "hello.c"); - git_tree_entry_name(entry); // "hello.c" - - // Once you have the entry object, you can access the content or subtree (or commit, in the case - // of submodules) that it points to. You can also get the mode if you want. - git_tree_entry_to_object(&objt, repo, entry); // blob - - // Remember to close the looked-up object once you are done using it - git_object_free(objt); - - // #### Blob Parsing - // - // The last object type is the simplest and requires the least parsing help. Blobs are just file - // contents and can contain anything, there is no structure to it. The main advantage to using the - // [simple blob api][ba] is that when you're creating blobs you don't have to calculate the size - // of the content. There is also a helper for reading a file from disk and writing it to the db and - // getting the oid back so you don't have to do all those steps yourself. - // - // [ba]: http://libgit2.github.com/libgit2/#HEAD/group/blob - - printf("\n*Blob Parsing*\n"); - git_blob *blob; - - git_oid_fromstr(&oid, "af7574ea73f7b166f869ef1a39be126d9a186ae0"); - git_blob_lookup(&blob, repo, &oid); - - // You can access a buffer with the raw contents of the blob directly. - // Note that this buffer may not be contain ASCII data for certain blobs (e.g. binary files): - // do not consider the buffer a NULL-terminated string, and use the `git_blob_rawsize` attribute to - // find out its exact size in bytes - printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); // 8 - git_blob_rawcontent(blob); // "content" - - // ### Revwalking - // - // The libgit2 [revision walking api][rw] provides methods to traverse the directed graph created - // by the parent pointers of the commit objects. Since all commits point back to the commit that - // came directly before them, you can walk this parentage as a graph and find all the commits that - // were ancestors of (reachable from) a given starting point. This can allow you to create `git log` - // type functionality. - // - // [rw]: http://libgit2.github.com/libgit2/#HEAD/group/revwalk - - printf("\n*Revwalking*\n"); - git_revwalk *walk; - git_commit *wcommit; - - git_oid_fromstr(&oid, "f0877d0b841d75172ec404fc9370173dfffc20d1"); - - // To use the revwalker, create a new walker, tell it how you want to sort the output and then push - // one or more starting points onto the walker. If you want to emulate the output of `git log` you - // would push the SHA of the commit that HEAD points to into the walker and then start traversing them. - // You can also 'hide' commits that you want to stop at or not see any of their ancestors. So if you - // want to emulate `git log branch1..branch2`, you would push the oid of `branch2` and hide the oid - // of `branch1`. - git_revwalk_new(&walk, repo); - git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE); - git_revwalk_push(walk, &oid); - - const git_signature *cauth; - const char *cmsg; - - // Now that we have the starting point pushed onto the walker, we can start asking for ancestors. It - // will return them in the sorting order we asked for as commit oids. - // We can then lookup and parse the commited pointed at by the returned OID; - // note that this operation is specially fast since the raw contents of the commit object will - // be cached in memory - while ((git_revwalk_next(&oid, walk)) == 0) { - error = git_commit_lookup(&wcommit, repo, &oid); - cmsg = git_commit_message(wcommit); - cauth = git_commit_author(wcommit); - printf("%s (%s)\n", cmsg, cauth->email); - git_commit_free(wcommit); - } - - // Like the other objects, be sure to free the revwalker when you're done to prevent memory leaks. - // Also, make sure that the repository being walked it not deallocated while the walk is in - // progress, or it will result in undefined behavior - git_revwalk_free(walk); - - // ### Index File Manipulation - // - // The [index file API][gi] allows you to read, traverse, update and write the Git index file - // (sometimes thought of as the staging area). - // - // [gi]: http://libgit2.github.com/libgit2/#HEAD/group/index - - printf("\n*Index Walking*\n"); - - git_index *index; - unsigned int i, ecount; - - // You can either open the index from the standard location in an open repository, as we're doing - // here, or you can open and manipulate any index file with `git_index_open_bare()`. The index - // for the repository will be located and loaded from disk. - git_repository_index(&index, repo); - - // For each entry in the index, you can get a bunch of information including the SHA (oid), path - // and mode which map to the tree objects that are written out. It also has filesystem properties - // to help determine what to inspect for changes (ctime, mtime, dev, ino, uid, gid, file_size and flags) - // All these properties are exported publicly in the `git_index_entry` struct - ecount = git_index_entrycount(index); - for (i = 0; i < ecount; ++i) { - const git_index_entry *e = git_index_get_byindex(index, i); - - printf("path: %s\n", e->path); - printf("mtime: %d\n", (int)e->mtime.seconds); - printf("fs: %d\n", (int)e->file_size); - } - - git_index_free(index); - - // ### References - // - // The [reference API][ref] allows you to list, resolve, create and update references such as - // branches, tags and remote references (everything in the .git/refs directory). - // - // [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference - - printf("\n*Reference Listing*\n"); - - // Here we will implement something like `git for-each-ref` simply listing out all available - // references and the object SHA they resolve to. - git_strarray ref_list; - git_reference_list(&ref_list, repo, GIT_REF_LISTALL); - - const char *refname; - git_reference *ref; - - // Now that we have the list of reference names, we can lookup each ref one at a time and - // resolve them to the SHA, then print both values out. - for (i = 0; i < ref_list.count; ++i) { - refname = ref_list.strings[i]; - git_reference_lookup(&ref, repo, refname); - - switch (git_reference_type(ref)) { - case GIT_REF_OID: - git_oid_fmt(out, git_reference_target(ref)); - printf("%s [%s]\n", refname, out); - break; - - case GIT_REF_SYMBOLIC: - printf("%s => %s\n", refname, git_reference_symbolic_target(ref)); - break; - default: - fprintf(stderr, "Unexpected reference type\n"); - exit(1); - } - } - - git_strarray_free(&ref_list); - - // ### Config Files - // - // The [config API][config] allows you to list and updatee config values in - // any of the accessible config file locations (system, global, local). - // - // [config]: http://libgit2.github.com/libgit2/#HEAD/group/config - - printf("\n*Config Listing*\n"); - - const char *email; - int32_t j; - - git_config *cfg; - - // Open a config object so we can read global values from it. - git_config_open_ondisk(&cfg, "~/.gitconfig"); - - git_config_get_int32(&j, cfg, "help.autocorrect"); - printf("Autocorrect: %d\n", j); - - git_config_get_string(&email, cfg, "user.email"); - printf("Email: %s\n", email); - - // Finally, when you're done with the repository, you can free it as well. - git_repository_free(repo); - - return 0; + const git_error *error = git_error_last(); + if (!error_code) + return; + + printf("Error %d %s - %s\n", error_code, action, + (error && error->message) ? error->message : "???"); + + exit(1); +} + +int lg2_general(git_repository *repo, int argc, char** argv) +{ + int error; + git_oid oid; + char *repo_path; + + /** + * Initialize the library, this will set up any global state which libgit2 needs + * including threading and crypto + */ + git_libgit2_init(); + + /** + * ### Opening the Repository + * + * There are a couple of methods for opening a repository, this being the + * simplest. There are also [methods][me] for specifying the index file + * and work tree locations, here we assume they are in the normal places. + * + * (Try running this program against tests/resources/testrepo.git.) + * + * [me]: https://libgit2.org/libgit2/#HEAD/group/repository + */ + repo_path = (argc > 1) ? argv[1] : "/opt/libgit2-test/.git"; + + error = git_repository_open(&repo, repo_path); + check_error(error, "opening repository"); + + oid_parsing(&oid); + object_database(repo, &oid); + commit_writing(repo); + commit_parsing(repo); + tag_parsing(repo); + tree_parsing(repo); + blob_parsing(repo); + revwalking(repo); + index_walking(repo); + reference_listing(repo); + config_files(repo_path, repo); + + /** + * Finally, when you're done with the repository, you can free it as well. + */ + git_repository_free(repo); + + return 0; } +/** + * ### SHA-1 Value Conversions + */ +static void oid_parsing(git_oid *oid) +{ + char out[GIT_OID_SHA1_HEXSIZE+1]; + char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"; + + printf("*Hex to Raw*\n"); + + /** + * For our first example, we will convert a 40 character hex value to the + * 20 byte raw SHA1 value. + * + * The `git_oid` is the structure that keeps the SHA value. We will use + * this throughout the example for storing the value of the current SHA + * key we're working with. + */ +#ifdef GIT_EXPERIMENTAL_SHA256 + git_oid_from_string(oid, hex, GIT_OID_SHA1); +#else + git_oid_fromstr(oid, hex); +#endif + + /* + * Once we've converted the string into the oid value, we can get the raw + * value of the SHA by accessing `oid.id` + * + * Next we will convert the 20 byte raw SHA1 value to a human readable 40 + * char hex value. + */ + printf("\n*Raw to Hex*\n"); + out[GIT_OID_SHA1_HEXSIZE] = '\0'; + + /** + * If you have a oid, you can easily get the hex value of the SHA as well. + */ + git_oid_fmt(out, oid); + printf("SHA hex string: %s\n", out); +} + +/** + * ### Working with the Object Database + * + * **libgit2** provides [direct access][odb] to the object database. The + * object database is where the actual objects are stored in Git. For + * working with raw objects, we'll need to get this structure from the + * repository. + * + * [odb]: https://libgit2.org/libgit2/#HEAD/group/odb + */ +static void object_database(git_repository *repo, git_oid *oid) +{ + char oid_hex[GIT_OID_SHA1_HEXSIZE+1] = { 0 }; + const unsigned char *data; + const char *str_type; + int error; + git_odb_object *obj; + git_odb *odb; + git_object_t otype; + + git_repository_odb(&odb, repo); + + /** + * #### Raw Object Reading + */ + + printf("\n*Raw Object Read*\n"); + + /** + * We can read raw objects directly from the object database if we have + * the oid (SHA) of the object. This allows us to access objects without + * knowing their type and inspect the raw bytes unparsed. + */ + error = git_odb_read(&obj, odb, oid); + check_error(error, "finding object in repository"); + + /** + * A raw object only has three properties - the type (commit, blob, tree + * or tag), the size of the raw data and the raw, unparsed data itself. + * For a commit or tag, that raw data is human readable plain ASCII + * text. For a blob it is just file contents, so it could be text or + * binary data. For a tree it is a special binary format, so it's unlikely + * to be hugely helpful as a raw object. + */ + data = (const unsigned char *)git_odb_object_data(obj); + otype = git_odb_object_type(obj); + + /** + * We provide methods to convert from the object type which is an enum, to + * a string representation of that value (and vice-versa). + */ + str_type = git_object_type2string(otype); + printf("object length and type: %d, %s\nobject data: %s\n", + (int)git_odb_object_size(obj), + str_type, data); + + /** + * For proper memory management, close the object when you are done with + * it or it will leak memory. + */ + git_odb_object_free(obj); + + /** + * #### Raw Object Writing + */ + + printf("\n*Raw Object Write*\n"); + + /** + * You can also write raw object data to Git. This is pretty cool because + * it gives you direct access to the key/value properties of Git. Here + * we'll write a new blob object that just contains a simple string. + * Notice that we have to specify the object type as the `git_otype` enum. + */ + git_odb_write(oid, odb, "test data", sizeof("test data") - 1, GIT_OBJECT_BLOB); + + /** + * Now that we've written the object, we can check out what SHA1 was + * generated when the object was written to our database. + */ + git_oid_fmt(oid_hex, oid); + printf("Written Object: %s\n", oid_hex); + + /** + * Free the object database after usage. + */ + git_odb_free(odb); +} + +/** + * #### Writing Commits + * + * libgit2 provides a couple of methods to create commit objects easily as + * well. There are four different create signatures, we'll just show one + * of them here. You can read about the other ones in the [commit API + * docs][cd]. + * + * [cd]: https://libgit2.org/libgit2/#HEAD/group/commit + */ +static void commit_writing(git_repository *repo) +{ + git_oid tree_id, parent_id, commit_id; + git_tree *tree; + git_commit *parent; + git_signature *author, *committer; + char oid_hex[GIT_OID_SHA1_HEXSIZE+1] = { 0 }; + + printf("\n*Commit Writing*\n"); + + /** + * Creating signatures for an authoring identity and time is simple. You + * will need to do this to specify who created a commit and when. Default + * values for the name and email should be found in the `user.name` and + * `user.email` configuration options. See the `config` section of this + * example file to see how to access config values. + */ + git_signature_new(&author, + "Scott Chacon", "schacon@gmail.com", 123456789, 60); + git_signature_new(&committer, + "Scott A Chacon", "scott@github.com", 987654321, 90); + + /** + * Commit objects need a tree to point to and optionally one or more + * parents. Here we're creating oid objects to create the commit with, + * but you can also use + */ +#ifdef GIT_EXPERIMENTAL_SHA256 + git_oid_from_string(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1", GIT_OID_SHA1); + git_oid_from_string(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1); +#else + git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1"); + git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); +#endif + git_tree_lookup(&tree, repo, &tree_id); + git_commit_lookup(&parent, repo, &parent_id); + + /** + * Here we actually create the commit object with a single call with all + * the values we need to create the commit. The SHA key is written to the + * `commit_id` variable here. + */ + git_commit_create_v( + &commit_id, /* out id */ + repo, + NULL, /* do not update the HEAD */ + author, + committer, + NULL, /* use default message encoding */ + "example commit", + tree, + 1, parent); + + /** + * Now we can take a look at the commit SHA we've generated. + */ + git_oid_fmt(oid_hex, &commit_id); + printf("New Commit: %s\n", oid_hex); + + /** + * Free all objects used in the meanwhile. + */ + git_tree_free(tree); + git_commit_free(parent); + git_signature_free(author); + git_signature_free(committer); +} + +/** + * ### Object Parsing + * + * libgit2 has methods to parse every object type in Git so you don't have + * to work directly with the raw data. This is much faster and simpler + * than trying to deal with the raw data yourself. + */ + +/** + * #### Commit Parsing + * + * [Parsing commit objects][pco] is simple and gives you access to all the + * data in the commit - the author (name, email, datetime), committer + * (same), tree, message, encoding and parent(s). + * + * [pco]: https://libgit2.org/libgit2/#HEAD/group/commit + */ +static void commit_parsing(git_repository *repo) +{ + const git_signature *author, *cmtter; + git_commit *commit, *parent; + git_oid oid; + char oid_hex[GIT_OID_SHA1_HEXSIZE+1]; + const char *message; + unsigned int parents, p; + int error; + time_t time; + + printf("\n*Commit Parsing*\n"); + +#ifdef GIT_EXPERIMENTAL_SHA256 + git_oid_from_string(&oid, "8496071c1b46c854b31185ea97743be6a8774479", GIT_OID_SHA1); +#else + git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479"); +#endif + + error = git_commit_lookup(&commit, repo, &oid); + check_error(error, "looking up commit"); + + /** + * Each of the properties of the commit object are accessible via methods, + * including commonly needed variations, such as `git_commit_time` which + * returns the committer time and `git_commit_message` which gives you the + * commit message (as a NUL-terminated string). + */ + message = git_commit_message(commit); + author = git_commit_author(commit); + cmtter = git_commit_committer(commit); + time = git_commit_time(commit); + + /** + * The author and committer methods return [git_signature] structures, + * which give you name, email and `when`, which is a `git_time` structure, + * giving you a timestamp and timezone offset. + */ + printf("Author: %s (%s)\nCommitter: %s (%s)\nDate: %s\nMessage: %s\n", + author->name, author->email, + cmtter->name, cmtter->email, + ctime(&time), message); + + /** + * Commits can have zero or more parents. The first (root) commit will + * have no parents, most commits will have one (i.e. the commit it was + * based on) and merge commits will have two or more. Commits can + * technically have any number, though it's rare to have more than two. + */ + parents = git_commit_parentcount(commit); + for (p = 0;p < parents;p++) { + memset(oid_hex, 0, sizeof(oid_hex)); + + git_commit_parent(&parent, commit, p); + git_oid_fmt(oid_hex, git_commit_id(parent)); + printf("Parent: %s\n", oid_hex); + git_commit_free(parent); + } + + git_commit_free(commit); +} + +/** + * #### Tag Parsing + * + * You can parse and create tags with the [tag management API][tm], which + * functions very similarly to the commit lookup, parsing and creation + * methods, since the objects themselves are very similar. + * + * [tm]: https://libgit2.org/libgit2/#HEAD/group/tag + */ +static void tag_parsing(git_repository *repo) +{ + git_commit *commit; + git_object_t type; + git_tag *tag; + git_oid oid; + const char *name, *message; + int error; + + printf("\n*Tag Parsing*\n"); + + /** + * We create an oid for the tag object if we know the SHA and look it up + * the same way that we would a commit (or any other object). + */ +#ifdef GIT_EXPERIMENTAL_SHA256 + git_oid_from_string(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1", GIT_OID_SHA1); +#else + git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); +#endif + + error = git_tag_lookup(&tag, repo, &oid); + check_error(error, "looking up tag"); + + /** + * Now that we have the tag object, we can extract the information it + * generally contains: the target (usually a commit object), the type of + * the target object (usually 'commit'), the name ('v1.0'), the tagger (a + * git_signature - name, email, timestamp), and the tag message. + */ + git_tag_target((git_object **)&commit, tag); + name = git_tag_name(tag); /* "test" */ + type = git_tag_target_type(tag); /* GIT_OBJECT_COMMIT (object_t enum) */ + message = git_tag_message(tag); /* "tag message\n" */ + printf("Tag Name: %s\nTag Type: %s\nTag Message: %s\n", + name, git_object_type2string(type), message); + + /** + * Free both the commit and tag after usage. + */ + git_commit_free(commit); + git_tag_free(tag); +} + +/** + * #### Tree Parsing + * + * [Tree parsing][tp] is a bit different than the other objects, in that + * we have a subtype which is the tree entry. This is not an actual + * object type in Git, but a useful structure for parsing and traversing + * tree entries. + * + * [tp]: https://libgit2.org/libgit2/#HEAD/group/tree + */ +static void tree_parsing(git_repository *repo) +{ + const git_tree_entry *entry; + size_t cnt; + git_object *obj; + git_tree *tree; + git_oid oid; + + printf("\n*Tree Parsing*\n"); + + /** + * Create the oid and lookup the tree object just like the other objects. + */ +#ifdef GIT_EXPERIMENTAL_SHA256 + git_oid_from_string(&oid, "f60079018b664e4e79329a7ef9559c8d9e0378d1", GIT_OID_SHA1); +#else + git_oid_fromstr(&oid, "f60079018b664e4e79329a7ef9559c8d9e0378d1"); +#endif + git_tree_lookup(&tree, repo, &oid); + + /** + * Getting the count of entries in the tree so you can iterate over them + * if you want to. + */ + cnt = git_tree_entrycount(tree); /* 2 */ + printf("tree entries: %d\n", (int) cnt); + + entry = git_tree_entry_byindex(tree, 0); + printf("Entry name: %s\n", git_tree_entry_name(entry)); /* "README" */ + + /** + * You can also access tree entries by name if you know the name of the + * entry you're looking for. + */ + entry = git_tree_entry_byname(tree, "README"); + git_tree_entry_name(entry); /* "README" */ + + /** + * Once you have the entry object, you can access the content or subtree + * (or commit, in the case of submodules) that it points to. You can also + * get the mode if you want. + */ + git_tree_entry_to_object(&obj, repo, entry); /* blob */ + + /** + * Remember to close the looked-up object and tree once you are done using it + */ + git_object_free(obj); + git_tree_free(tree); +} + +/** + * #### Blob Parsing + * + * The last object type is the simplest and requires the least parsing + * help. Blobs are just file contents and can contain anything, there is + * no structure to it. The main advantage to using the [simple blob + * api][ba] is that when you're creating blobs you don't have to calculate + * the size of the content. There is also a helper for reading a file + * from disk and writing it to the db and getting the oid back so you + * don't have to do all those steps yourself. + * + * [ba]: https://libgit2.org/libgit2/#HEAD/group/blob + */ +static void blob_parsing(git_repository *repo) +{ + git_blob *blob; + git_oid oid; + + printf("\n*Blob Parsing*\n"); + +#ifdef GIT_EXPERIMENTAL_SHA256 + git_oid_from_string(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OID_SHA1); +#else + git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08"); +#endif + git_blob_lookup(&blob, repo, &oid); + + /** + * You can access a buffer with the raw contents of the blob directly. + * Note that this buffer may not be contain ASCII data for certain blobs + * (e.g. binary files): do not consider the buffer a NULL-terminated + * string, and use the `git_blob_rawsize` attribute to find out its exact + * size in bytes + * */ + printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); /* 8 */ + git_blob_rawcontent(blob); /* "content" */ + + /** + * Free the blob after usage. + */ + git_blob_free(blob); +} + +/** + * ### Revwalking + * + * The libgit2 [revision walking api][rw] provides methods to traverse the + * directed graph created by the parent pointers of the commit objects. + * Since all commits point back to the commit that came directly before + * them, you can walk this parentage as a graph and find all the commits + * that were ancestors of (reachable from) a given starting point. This + * can allow you to create `git log` type functionality. + * + * [rw]: https://libgit2.org/libgit2/#HEAD/group/revwalk + */ +static void revwalking(git_repository *repo) +{ + const git_signature *cauth; + const char *cmsg; + int error; + git_revwalk *walk; + git_commit *wcommit; + git_oid oid; + + printf("\n*Revwalking*\n"); + +#ifdef GIT_EXPERIMENTAL_SHA256 + git_oid_from_string(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1); +#else + git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); +#endif + + /** + * To use the revwalker, create a new walker, tell it how you want to sort + * the output and then push one or more starting points onto the walker. + * If you want to emulate the output of `git log` you would push the SHA + * of the commit that HEAD points to into the walker and then start + * traversing them. You can also 'hide' commits that you want to stop at + * or not see any of their ancestors. So if you want to emulate `git log + * branch1..branch2`, you would push the oid of `branch2` and hide the oid + * of `branch1`. + */ + git_revwalk_new(&walk, repo); + git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE); + git_revwalk_push(walk, &oid); + + /** + * Now that we have the starting point pushed onto the walker, we start + * asking for ancestors. It will return them in the sorting order we asked + * for as commit oids. We can then lookup and parse the committed pointed + * at by the returned OID; note that this operation is specially fast + * since the raw contents of the commit object will be cached in memory + */ + while ((git_revwalk_next(&oid, walk)) == 0) { + error = git_commit_lookup(&wcommit, repo, &oid); + check_error(error, "looking up commit during revwalk"); + + cmsg = git_commit_message(wcommit); + cauth = git_commit_author(wcommit); + printf("%s (%s)\n", cmsg, cauth->email); + + git_commit_free(wcommit); + } + + /** + * Like the other objects, be sure to free the revwalker when you're done + * to prevent memory leaks. Also, make sure that the repository being + * walked it not deallocated while the walk is in progress, or it will + * result in undefined behavior + */ + git_revwalk_free(walk); +} + +/** + * ### Index File Manipulation * + * The [index file API][gi] allows you to read, traverse, update and write + * the Git index file (sometimes thought of as the staging area). + * + * [gi]: https://libgit2.org/libgit2/#HEAD/group/index + */ +static void index_walking(git_repository *repo) +{ + git_index *index; + size_t i, ecount; + + printf("\n*Index Walking*\n"); + + /** + * You can either open the index from the standard location in an open + * repository, as we're doing here, or you can open and manipulate any + * index file with `git_index_open_bare()`. The index for the repository + * will be located and loaded from disk. + */ + git_repository_index(&index, repo); + + /** + * For each entry in the index, you can get a bunch of information + * including the SHA (oid), path and mode which map to the tree objects + * that are written out. It also has filesystem properties to help + * determine what to inspect for changes (ctime, mtime, dev, ino, uid, + * gid, file_size and flags) All these properties are exported publicly in + * the `git_index_entry` struct + */ + ecount = git_index_entrycount(index); + for (i = 0; i < ecount; ++i) { + const git_index_entry *e = git_index_get_byindex(index, i); + + printf("path: %s\n", e->path); + printf("mtime: %d\n", (int)e->mtime.seconds); + printf("fs: %d\n", (int)e->file_size); + } + + git_index_free(index); +} + +/** + * ### References + * + * The [reference API][ref] allows you to list, resolve, create and update + * references such as branches, tags and remote references (everything in + * the .git/refs directory). + * + * [ref]: https://libgit2.org/libgit2/#HEAD/group/reference + */ +static void reference_listing(git_repository *repo) +{ + git_strarray ref_list; + unsigned i; + + printf("\n*Reference Listing*\n"); + + /** + * Here we will implement something like `git for-each-ref` simply listing + * out all available references and the object SHA they resolve to. + * + * Now that we have the list of reference names, we can lookup each ref + * one at a time and resolve them to the SHA, then print both values out. + */ + + git_reference_list(&ref_list, repo); + + for (i = 0; i < ref_list.count; ++i) { + git_reference *ref; + char oid_hex[GIT_OID_SHA1_HEXSIZE+1] = GIT_OID_SHA1_HEXZERO; + const char *refname; + + refname = ref_list.strings[i]; + git_reference_lookup(&ref, repo, refname); + + switch (git_reference_type(ref)) { + case GIT_REFERENCE_DIRECT: + git_oid_fmt(oid_hex, git_reference_target(ref)); + printf("%s [%s]\n", refname, oid_hex); + break; + + case GIT_REFERENCE_SYMBOLIC: + printf("%s => %s\n", refname, git_reference_symbolic_target(ref)); + break; + default: + fprintf(stderr, "Unexpected reference type\n"); + exit(1); + } + + git_reference_free(ref); + } + + git_strarray_dispose(&ref_list); +} + +/** + * ### Config Files + * + * The [config API][config] allows you to list and update config values + * in any of the accessible config file locations (system, global, local). + * + * [config]: https://libgit2.org/libgit2/#HEAD/group/config + */ +static void config_files(const char *repo_path, git_repository* repo) +{ + const char *email; + char config_path[256]; + int32_t autocorrect; + git_config *cfg; + git_config *snap_cfg; + int error_code; + + printf("\n*Config Listing*\n"); + + /** + * Open a config object so we can read global values from it. + */ + sprintf(config_path, "%s/config", repo_path); + check_error(git_config_open_ondisk(&cfg, config_path), "opening config"); + + if (git_config_get_int32(&autocorrect, cfg, "help.autocorrect") == 0) + printf("Autocorrect: %d\n", autocorrect); + + check_error(git_repository_config_snapshot(&snap_cfg, repo), "config snapshot"); + git_config_get_string(&email, snap_cfg, "user.email"); + printf("Email: %s\n", email); + + error_code = git_config_get_int32(&autocorrect, cfg, "help.autocorrect"); + switch (error_code) + { + case 0: + printf("Autocorrect: %d\n", autocorrect); + break; + case GIT_ENOTFOUND: + printf("Autocorrect: Undefined\n"); + break; + default: + check_error(error_code, "get_int32 failed"); + } + git_config_free(cfg); + + check_error(git_repository_config_snapshot(&snap_cfg, repo), "config snapshot"); + error_code = git_config_get_string(&email, snap_cfg, "user.email"); + switch (error_code) + { + case 0: + printf("Email: %s\n", email); + break; + case GIT_ENOTFOUND: + printf("Email: Undefined\n"); + break; + default: + check_error(error_code, "get_string failed"); + } + + git_config_free(snap_cfg); +} diff --git a/examples/index-pack.c b/examples/index-pack.c new file mode 100644 index 00000000000..e9f32b8b291 --- /dev/null +++ b/examples/index-pack.c @@ -0,0 +1,76 @@ +#include "common.h" + +/* + * This could be run in the main loop whilst the application waits for + * the indexing to finish in a worker thread + */ +static int index_cb(const git_indexer_progress *stats, void *data) +{ + (void)data; + printf("\rProcessing %u of %u", stats->indexed_objects, stats->total_objects); + + return 0; +} + +int lg2_index_pack(git_repository *repo, int argc, char **argv) +{ + git_indexer *idx; + git_indexer_progress stats = {0, 0}; + int error; + int fd; + ssize_t read_bytes; + char buf[512]; + + (void)repo; + + if (argc < 2) { + fprintf(stderr, "usage: %s index-pack \n", argv[-1]); + return EXIT_FAILURE; + } + +#ifdef GIT_EXPERIMENTAL_SHA256 + error = git_indexer_new(&idx, ".", NULL); +#else + error = git_indexer_new(&idx, ".", 0, NULL, NULL); +#endif + + if (error < 0) { + puts("bad idx"); + return -1; + } + + + if ((fd = open(argv[1], 0)) < 0) { + perror("open"); + return -1; + } + + do { + read_bytes = read(fd, buf, sizeof(buf)); + if (read_bytes < 0) + break; + + if ((error = git_indexer_append(idx, buf, read_bytes, &stats)) < 0) + goto cleanup; + + index_cb(&stats, NULL); + } while (read_bytes > 0); + + if (read_bytes < 0) { + error = -1; + perror("failed reading"); + goto cleanup; + } + + if ((error = git_indexer_commit(idx, &stats)) < 0) + goto cleanup; + + printf("\rIndexing %u of %u\n", stats.indexed_objects, stats.total_objects); + + puts(git_indexer_name(idx)); + + cleanup: + close(fd); + git_indexer_free(idx); + return error; +} diff --git a/examples/init.c b/examples/init.c new file mode 100644 index 00000000000..036c156ab08 --- /dev/null +++ b/examples/init.c @@ -0,0 +1,249 @@ +/* + * libgit2 "init" example - shows how to initialize a new repo + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/** + * This is a sample program that is similar to "git init". See the + * documentation for that (try "git help init") to understand what this + * program is emulating. + * + * This demonstrates using the libgit2 APIs to initialize a new repository. + * + * This also contains a special additional option that regular "git init" + * does not support which is "--initial-commit" to make a first empty commit. + * That is demonstrated in the "create_initial_commit" helper function. + */ + +/** Forward declarations of helpers */ +struct init_opts { + int no_options; + int quiet; + int bare; + int initial_commit; + uint32_t shared; + const char *template; + const char *gitdir; + const char *dir; +}; +static void create_initial_commit(git_repository *repo); +static void parse_opts(struct init_opts *o, int argc, char *argv[]); + +int lg2_init(git_repository *repo, int argc, char *argv[]) +{ + struct init_opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 }; + + parse_opts(&o, argc, argv); + + /* Initialize repository. */ + + if (o.no_options) { + /** + * No options were specified, so let's demonstrate the default + * simple case of git_repository_init() API usage... + */ + check_lg2(git_repository_init(&repo, o.dir, 0), + "Could not initialize repository", NULL); + } + else { + /** + * Some command line options were specified, so we'll use the + * extended init API to handle them + */ + git_repository_init_options initopts = GIT_REPOSITORY_INIT_OPTIONS_INIT; + initopts.flags = GIT_REPOSITORY_INIT_MKPATH; + + if (o.bare) + initopts.flags |= GIT_REPOSITORY_INIT_BARE; + + if (o.template) { + initopts.flags |= GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE; + initopts.template_path = o.template; + } + + if (o.gitdir) { + /** + * If you specified a separate git directory, then initialize + * the repository at that path and use the second path as the + * working directory of the repository (with a git-link file) + */ + initopts.workdir_path = o.dir; + o.dir = o.gitdir; + } + + if (o.shared != 0) + initopts.mode = o.shared; + + check_lg2(git_repository_init_ext(&repo, o.dir, &initopts), + "Could not initialize repository", NULL); + } + + /** Print a message to stdout like "git init" does. */ + + if (!o.quiet) { + if (o.bare || o.gitdir) + o.dir = git_repository_path(repo); + else + o.dir = git_repository_workdir(repo); + + printf("Initialized empty Git repository in %s\n", o.dir); + } + + /** + * As an extension to the basic "git init" command, this example + * gives the option to create an empty initial commit. This is + * mostly to demonstrate what it takes to do that, but also some + * people like to have that empty base commit in their repo. + */ + if (o.initial_commit) { + create_initial_commit(repo); + printf("Created empty initial commit\n"); + } + + git_repository_free(repo); + + return 0; +} + +/** + * Unlike regular "git init", this example shows how to create an initial + * empty commit in the repository. This is the helper function that does + * that. + */ +static void create_initial_commit(git_repository *repo) +{ + git_signature *author_sig = NULL, *committer_sig = NULL; + git_index *index; + git_oid tree_id, commit_id; + git_tree *tree; + + /** First use the config to initialize a commit signature for the user. */ + + if ((git_signature_default_from_env(&author_sig, &committer_sig, repo) < 0)) + fatal("Unable to create a commit signature.", + "Perhaps 'user.name' and 'user.email' are not set"); + + /* Now let's create an empty tree for this commit */ + + if (git_repository_index(&index, repo) < 0) + fatal("Could not open repository index", NULL); + + /** + * Outside of this example, you could call git_index_add_bypath() + * here to put actual files into the index. For our purposes, we'll + * leave it empty for now. + */ + + if (git_index_write_tree(&tree_id, index) < 0) + fatal("Unable to write initial tree from index", NULL); + + git_index_free(index); + + if (git_tree_lookup(&tree, repo, &tree_id) < 0) + fatal("Could not look up initial tree", NULL); + + /** + * Ready to create the initial commit. + * + * Normally creating a commit would involve looking up the current + * HEAD commit and making that be the parent of the initial commit, + * but here this is the first commit so there will be no parent. + */ + + if (git_commit_create_v( + &commit_id, repo, "HEAD", author_sig, committer_sig, + NULL, "Initial commit", tree, 0) < 0) + fatal("Could not create the initial commit", NULL); + + /** Clean up so we don't leak memory. */ + + git_tree_free(tree); + git_signature_free(author_sig); + git_signature_free(committer_sig); +} + +static void usage(const char *error, const char *arg) +{ + fprintf(stderr, "error: %s '%s'\n", error, arg); + fprintf(stderr, + "usage: init [-q | --quiet] [--bare] [--template=]\n" + " [--shared[=perms]] [--initial-commit]\n" + " [--separate-git-dir] \n"); + exit(1); +} + +/** Parse the tail of the --shared= argument. */ +static uint32_t parse_shared(const char *shared) +{ + if (!strcmp(shared, "false") || !strcmp(shared, "umask")) + return GIT_REPOSITORY_INIT_SHARED_UMASK; + + else if (!strcmp(shared, "true") || !strcmp(shared, "group")) + return GIT_REPOSITORY_INIT_SHARED_GROUP; + + else if (!strcmp(shared, "all") || !strcmp(shared, "world") || + !strcmp(shared, "everybody")) + return GIT_REPOSITORY_INIT_SHARED_ALL; + + else if (shared[0] == '0') { + long val; + char *end = NULL; + val = strtol(shared + 1, &end, 8); + if (end == shared + 1 || *end != 0) + usage("invalid octal value for --shared", shared); + return (uint32_t)val; + } + + else + usage("unknown value for --shared", shared); + + return 0; +} + +static void parse_opts(struct init_opts *o, int argc, char *argv[]) +{ + struct args_info args = ARGS_INFO_INIT; + const char *sharedarg; + + /** Process arguments. */ + + for (args.pos = 1; args.pos < argc; ++args.pos) { + char *a = argv[args.pos]; + + if (a[0] == '-') + o->no_options = 0; + + if (a[0] != '-') { + if (o->dir != NULL) + usage("extra argument", a); + o->dir = a; + } + else if (!strcmp(a, "-q") || !strcmp(a, "--quiet")) + o->quiet = 1; + else if (!strcmp(a, "--bare")) + o->bare = 1; + else if (!strcmp(a, "--shared")) + o->shared = GIT_REPOSITORY_INIT_SHARED_GROUP; + else if (!strcmp(a, "--initial-commit")) + o->initial_commit = 1; + else if (match_str_arg(&sharedarg, &args, "--shared")) + o->shared = parse_shared(sharedarg); + else if (!match_str_arg(&o->template, &args, "--template") || + !match_str_arg(&o->gitdir, &args, "--separate-git-dir")) + usage("unknown option", a); + } + + if (!o->dir) + usage("must specify directory to init", ""); +} diff --git a/examples/lg2.c b/examples/lg2.c new file mode 100644 index 00000000000..7946bc21578 --- /dev/null +++ b/examples/lg2.c @@ -0,0 +1,124 @@ +#include "common.h" + +/* This part is not strictly libgit2-dependent, but you can use this + * as a starting point for a git-like tool */ + +typedef int (*git_command_fn)(git_repository *, int , char **); + +struct { + char *name; + git_command_fn fn; + char requires_repo; +} commands[] = { + { "add", lg2_add, 1 }, + { "blame", lg2_blame, 1 }, + { "cat-file", lg2_cat_file, 1 }, + { "checkout", lg2_checkout, 1 }, + { "clone", lg2_clone, 0 }, + { "commit", lg2_commit, 1 }, + { "config", lg2_config, 1 }, + { "describe", lg2_describe, 1 }, + { "diff", lg2_diff, 1 }, + { "fetch", lg2_fetch, 1 }, + { "for-each-ref", lg2_for_each_ref, 1 }, + { "general", lg2_general, 0 }, + { "index-pack", lg2_index_pack, 1 }, + { "init", lg2_init, 0 }, + { "log", lg2_log, 1 }, + { "ls-files", lg2_ls_files, 1 }, + { "ls-remote", lg2_ls_remote, 1 }, + { "merge", lg2_merge, 1 }, + { "push", lg2_push, 1 }, + { "remote", lg2_remote, 1 }, + { "rev-list", lg2_rev_list, 1 }, + { "rev-parse", lg2_rev_parse, 1 }, + { "show-index", lg2_show_index, 0 }, + { "stash", lg2_stash, 1 }, + { "status", lg2_status, 1 }, + { "tag", lg2_tag, 1 }, +}; + +static int run_command(git_command_fn fn, git_repository *repo, struct args_info args) +{ + int error; + + /* Run the command. If something goes wrong, print the error message to stderr */ + error = fn(repo, args.argc - args.pos, &args.argv[args.pos]); + if (error < 0) { + if (git_error_last() == NULL) + fprintf(stderr, "Error without message"); + else + fprintf(stderr, "Bad news:\n %s\n", git_error_last()->message); + } + + return !!error; +} + +static int usage(const char *prog) +{ + size_t i; + + fprintf(stderr, "usage: %s ...\n\nAvailable commands:\n\n", prog); + for (i = 0; i < ARRAY_SIZE(commands); i++) + fprintf(stderr, "\t%s\n", commands[i].name); + + exit(EXIT_FAILURE); +} + +int main(int argc, char **argv) +{ + struct args_info args = ARGS_INFO_INIT; + git_repository *repo = NULL; + const char *git_dir = NULL; + int return_code = 1; + size_t i; + + if (argc < 2) + usage(argv[0]); + + git_libgit2_init(); + + for (args.pos = 1; args.pos < args.argc; ++args.pos) { + char *a = args.argv[args.pos]; + + if (a[0] != '-') { + /* non-arg */ + break; + } else if (optional_str_arg(&git_dir, &args, "--git-dir", ".git")) { + continue; + } else if (match_arg_separator(&args)) { + break; + } + } + + if (args.pos == args.argc) + usage(argv[0]); + + if (!git_dir) + git_dir = "."; + + for (i = 0; i < ARRAY_SIZE(commands); ++i) { + if (strcmp(args.argv[args.pos], commands[i].name)) + continue; + + /* + * Before running the actual command, create an instance + * of the local repository and pass it to the function. + * */ + if (commands[i].requires_repo) { + check_lg2(git_repository_open_ext(&repo, git_dir, 0, NULL), + "Unable to open repository '%s'", git_dir); + } + + return_code = run_command(commands[i].fn, repo, args); + goto shutdown; + } + + fprintf(stderr, "Command not found: %s\n", argv[1]); + +shutdown: + git_repository_free(repo); + git_libgit2_shutdown(); + + return return_code; +} diff --git a/examples/log.c b/examples/log.c new file mode 100644 index 00000000000..62a6eb5858f --- /dev/null +++ b/examples/log.c @@ -0,0 +1,497 @@ +/* + * libgit2 "log" example - shows how to walk history and get commit info + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/** + * This example demonstrates the libgit2 rev walker APIs to roughly + * simulate the output of `git log` and a few of command line arguments. + * `git log` has many many options and this only shows a few of them. + * + * This does not have: + * + * - Robust error handling + * - Colorized or paginated output formatting + * - Most of the `git log` options + * + * This does have: + * + * - Examples of translating command line arguments to equivalent libgit2 + * revwalker configuration calls + * - Simplified options to apply pathspec limits and to show basic diffs + */ + +/** log_state represents walker being configured while handling options */ +struct log_state { + git_repository *repo; + const char *repodir; + git_revwalk *walker; + int hide; + int sorting; + int revisions; +}; + +/** utility functions that are called to configure the walker */ +static void set_sorting(struct log_state *s, unsigned int sort_mode); +static void push_rev(struct log_state *s, git_object *obj, int hide); +static int add_revision(struct log_state *s, const char *revstr); + +/** log_options holds other command line options that affect log output */ +struct log_options { + int show_diff; + int show_oneline; + int show_log_size; + int skip, limit; + int min_parents, max_parents; + git_time_t before; + git_time_t after; + const char *author; + const char *committer; + const char *grep; +}; + +/** utility functions that parse options and help with log output */ +static int parse_options( + struct log_state *s, struct log_options *opt, int argc, char **argv); +static void print_time(const git_time *intime, const char *prefix); +static void print_commit(git_commit *commit, struct log_options *opts); +static int match_with_parent(git_commit *commit, int i, git_diff_options *); + +/** utility functions for filtering */ +static int signature_matches(const git_signature *sig, const char *filter); +static int log_message_matches(const git_commit *commit, const char *filter); + +int lg2_log(git_repository *repo, int argc, char *argv[]) +{ + int i, count = 0, printed = 0, parents, last_arg; + struct log_state s; + struct log_options opt; + git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; + git_oid oid; + git_commit *commit = NULL; + git_pathspec *ps = NULL; + + memset(&s, 0, sizeof(s)); + + /** Parse arguments and set up revwalker. */ + s.repo = repo; + last_arg = parse_options(&s, &opt, argc, argv); + + diffopts.pathspec.strings = &argv[last_arg]; + diffopts.pathspec.count = argc - last_arg; + if (diffopts.pathspec.count > 0) + check_lg2(git_pathspec_new(&ps, &diffopts.pathspec), + "Building pathspec", NULL); + + if (!s.revisions) + add_revision(&s, NULL); + + /** Use the revwalker to traverse the history. */ + + printed = count = 0; + + for (; !git_revwalk_next(&oid, s.walker); git_commit_free(commit)) { + check_lg2(git_commit_lookup(&commit, s.repo, &oid), + "Failed to look up commit", NULL); + + parents = (int)git_commit_parentcount(commit); + if (parents < opt.min_parents) + continue; + if (opt.max_parents > 0 && parents > opt.max_parents) + continue; + + if (diffopts.pathspec.count > 0) { + int unmatched = parents; + + if (parents == 0) { + git_tree *tree; + check_lg2(git_commit_tree(&tree, commit), "Get tree", NULL); + if (git_pathspec_match_tree( + NULL, tree, GIT_PATHSPEC_NO_MATCH_ERROR, ps) != 0) + unmatched = 1; + git_tree_free(tree); + } else if (parents == 1) { + unmatched = match_with_parent(commit, 0, &diffopts) ? 0 : 1; + } else { + for (i = 0; i < parents; ++i) { + if (match_with_parent(commit, i, &diffopts)) + unmatched--; + } + } + + if (unmatched > 0) + continue; + } + + if (!signature_matches(git_commit_author(commit), opt.author)) + continue; + + if (!signature_matches(git_commit_committer(commit), opt.committer)) + continue; + + if (!log_message_matches(commit, opt.grep)) + continue; + + if (count++ < opt.skip) + continue; + if (opt.limit != -1 && printed++ >= opt.limit) { + git_commit_free(commit); + break; + } + + print_commit(commit, &opt); + + if (opt.show_diff) { + git_tree *a = NULL, *b = NULL; + git_diff *diff = NULL; + + if (parents > 1) + continue; + check_lg2(git_commit_tree(&b, commit), "Get tree", NULL); + if (parents == 1) { + git_commit *parent; + check_lg2(git_commit_parent(&parent, commit, 0), "Get parent", NULL); + check_lg2(git_commit_tree(&a, parent), "Tree for parent", NULL); + git_commit_free(parent); + } + + check_lg2(git_diff_tree_to_tree( + &diff, git_commit_owner(commit), a, b, &diffopts), + "Diff commit with parent", NULL); + check_lg2( + git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, diff_output, NULL), + "Displaying diff", NULL); + + git_diff_free(diff); + git_tree_free(a); + git_tree_free(b); + } + } + + git_pathspec_free(ps); + git_revwalk_free(s.walker); + + return 0; +} + +/** Determine if the given git_signature does not contain the filter text. */ +static int signature_matches(const git_signature *sig, const char *filter) { + if (filter == NULL) + return 1; + + if (sig != NULL && + (strstr(sig->name, filter) != NULL || + strstr(sig->email, filter) != NULL)) + return 1; + + return 0; +} + +static int log_message_matches(const git_commit *commit, const char *filter) { + const char *message = NULL; + + if (filter == NULL) + return 1; + + if ((message = git_commit_message(commit)) != NULL && + strstr(message, filter) != NULL) + return 1; + + return 0; +} + +/** Push object (for hide or show) onto revwalker. */ +static void push_rev(struct log_state *s, git_object *obj, int hide) +{ + hide = s->hide ^ hide; + + /** Create revwalker on demand if it doesn't already exist. */ + if (!s->walker) { + check_lg2(git_revwalk_new(&s->walker, s->repo), + "Could not create revision walker", NULL); + git_revwalk_sorting(s->walker, s->sorting); + } + + if (!obj) + check_lg2(git_revwalk_push_head(s->walker), + "Could not find repository HEAD", NULL); + else if (hide) + check_lg2(git_revwalk_hide(s->walker, git_object_id(obj)), + "Reference does not refer to a commit", NULL); + else + check_lg2(git_revwalk_push(s->walker, git_object_id(obj)), + "Reference does not refer to a commit", NULL); + + git_object_free(obj); +} + +/** Parse revision string and add revs to walker. */ +static int add_revision(struct log_state *s, const char *revstr) +{ + git_revspec revs; + int hide = 0; + + if (!revstr) { + push_rev(s, NULL, hide); + return 0; + } + + if (*revstr == '^') { + revs.flags = GIT_REVSPEC_SINGLE; + hide = !hide; + + if (git_revparse_single(&revs.from, s->repo, revstr + 1) < 0) + return -1; + } else if (git_revparse(&revs, s->repo, revstr) < 0) + return -1; + + if ((revs.flags & GIT_REVSPEC_SINGLE) != 0) + push_rev(s, revs.from, hide); + else { + push_rev(s, revs.to, hide); + + if ((revs.flags & GIT_REVSPEC_MERGE_BASE) != 0) { + git_oid base; + check_lg2(git_merge_base(&base, s->repo, + git_object_id(revs.from), git_object_id(revs.to)), + "Could not find merge base", revstr); + check_lg2( + git_object_lookup(&revs.to, s->repo, &base, GIT_OBJECT_COMMIT), + "Could not find merge base commit", NULL); + + push_rev(s, revs.to, hide); + } + + push_rev(s, revs.from, !hide); + } + + return 0; +} + +/** Update revwalker with sorting mode. */ +static void set_sorting(struct log_state *s, unsigned int sort_mode) +{ + /** Open repo on demand if it isn't already open. */ + if (!s->repo) { + if (!s->repodir) s->repodir = "."; + check_lg2(git_repository_open_ext(&s->repo, s->repodir, 0, NULL), + "Could not open repository", s->repodir); + } + + /** Create revwalker on demand if it doesn't already exist. */ + if (!s->walker) + check_lg2(git_revwalk_new(&s->walker, s->repo), + "Could not create revision walker", NULL); + + if (sort_mode == GIT_SORT_REVERSE) + s->sorting = s->sorting ^ GIT_SORT_REVERSE; + else + s->sorting = sort_mode | (s->sorting & GIT_SORT_REVERSE); + + git_revwalk_sorting(s->walker, s->sorting); +} + +/** Helper to format a git_time value like Git. */ +static void print_time(const git_time *intime, const char *prefix) +{ + char sign, out[32]; + struct tm *intm; + int offset, hours, minutes; + time_t t; + + offset = intime->offset; + if (offset < 0) { + sign = '-'; + offset = -offset; + } else { + sign = '+'; + } + + hours = offset / 60; + minutes = offset % 60; + + t = (time_t)intime->time + (intime->offset * 60); + + intm = gmtime(&t); + strftime(out, sizeof(out), "%a %b %e %T %Y", intm); + + printf("%s%s %c%02d%02d\n", prefix, out, sign, hours, minutes); +} + +/** Helper to print a commit object. */ +static void print_commit(git_commit *commit, struct log_options *opts) +{ + char buf[GIT_OID_SHA1_HEXSIZE + 1]; + int i, count; + const git_signature *sig; + const char *scan, *eol; + + git_oid_tostr(buf, sizeof(buf), git_commit_id(commit)); + + if (opts->show_oneline) { + printf("%s ", buf); + } else { + printf("commit %s\n", buf); + + if (opts->show_log_size) { + printf("log size %d\n", (int)strlen(git_commit_message(commit))); + } + + if ((count = (int)git_commit_parentcount(commit)) > 1) { + printf("Merge:"); + for (i = 0; i < count; ++i) { + git_oid_tostr(buf, 8, git_commit_parent_id(commit, i)); + printf(" %s", buf); + } + printf("\n"); + } + + if ((sig = git_commit_author(commit)) != NULL) { + printf("Author: %s <%s>\n", sig->name, sig->email); + print_time(&sig->when, "Date: "); + } + printf("\n"); + } + + for (scan = git_commit_message(commit); scan && *scan; ) { + for (eol = scan; *eol && *eol != '\n'; ++eol) /* find eol */; + + if (opts->show_oneline) + printf("%.*s\n", (int)(eol - scan), scan); + else + printf(" %.*s\n", (int)(eol - scan), scan); + scan = *eol ? eol + 1 : NULL; + if (opts->show_oneline) + break; + } + if (!opts->show_oneline) + printf("\n"); +} + +/** Helper to find how many files in a commit changed from its nth parent. */ +static int match_with_parent(git_commit *commit, int i, git_diff_options *opts) +{ + git_commit *parent; + git_tree *a, *b; + git_diff *diff; + int ndeltas; + + check_lg2( + git_commit_parent(&parent, commit, (size_t)i), "Get parent", NULL); + check_lg2(git_commit_tree(&a, parent), "Tree for parent", NULL); + check_lg2(git_commit_tree(&b, commit), "Tree for commit", NULL); + check_lg2( + git_diff_tree_to_tree(&diff, git_commit_owner(commit), a, b, opts), + "Checking diff between parent and commit", NULL); + + ndeltas = (int)git_diff_num_deltas(diff); + + git_diff_free(diff); + git_tree_free(a); + git_tree_free(b); + git_commit_free(parent); + + return ndeltas > 0; +} + +/** Print a usage message for the program. */ +static void usage(const char *message, const char *arg) +{ + if (message && arg) + fprintf(stderr, "%s: %s\n", message, arg); + else if (message) + fprintf(stderr, "%s\n", message); + fprintf(stderr, "usage: log []\n"); + exit(1); +} + +/** Parse some log command line options. */ +static int parse_options( + struct log_state *s, struct log_options *opt, int argc, char **argv) +{ + struct args_info args = ARGS_INFO_INIT; + s->sorting = GIT_SORT_TIME; + + memset(opt, 0, sizeof(*opt)); + opt->max_parents = -1; + opt->limit = -1; + + for (args.pos = 1; args.pos < argc; ++args.pos) { + const char *a = argv[args.pos]; + + if (a[0] != '-') { + if (!add_revision(s, a)) + s->revisions++; + else + /** Try failed revision parse as filename. */ + break; + } else if (match_arg_separator(&args)) { + break; + } + else if (!strcmp(a, "--date-order")) + set_sorting(s, GIT_SORT_TIME); + else if (!strcmp(a, "--topo-order")) + set_sorting(s, GIT_SORT_TOPOLOGICAL); + else if (!strcmp(a, "--reverse")) + set_sorting(s, GIT_SORT_REVERSE); + else if (match_str_arg(&opt->author, &args, "--author")) + /** Found valid --author */ + ; + else if (match_str_arg(&opt->committer, &args, "--committer")) + /** Found valid --committer */ + ; + else if (match_str_arg(&opt->grep, &args, "--grep")) + /** Found valid --grep */ + ; + else if (match_str_arg(&s->repodir, &args, "--git-dir")) + /** Found git-dir. */ + ; + else if (match_int_arg(&opt->skip, &args, "--skip", 0)) + /** Found valid --skip. */ + ; + else if (match_int_arg(&opt->limit, &args, "--max-count", 0)) + /** Found valid --max-count. */ + ; + else if (a[1] >= '0' && a[1] <= '9') + is_integer(&opt->limit, a + 1, 0); + else if (match_int_arg(&opt->limit, &args, "-n", 0)) + /** Found valid -n. */ + ; + else if (!strcmp(a, "--merges")) + opt->min_parents = 2; + else if (!strcmp(a, "--no-merges")) + opt->max_parents = 1; + else if (!strcmp(a, "--no-min-parents")) + opt->min_parents = 0; + else if (!strcmp(a, "--no-max-parents")) + opt->max_parents = -1; + else if (match_int_arg(&opt->max_parents, &args, "--max-parents=", 1)) + /** Found valid --max-parents. */ + ; + else if (match_int_arg(&opt->min_parents, &args, "--min-parents=", 0)) + /** Found valid --min_parents. */ + ; + else if (!strcmp(a, "-p") || !strcmp(a, "-u") || !strcmp(a, "--patch")) + opt->show_diff = 1; + else if (!strcmp(a, "--log-size")) + opt->show_log_size = 1; + else if (!strcmp(a, "--oneline")) + opt->show_oneline = 1; + else + usage("Unsupported argument", a); + } + + return args.pos; +} + diff --git a/examples/ls-files.c b/examples/ls-files.c new file mode 100644 index 00000000000..a23506962a1 --- /dev/null +++ b/examples/ls-files.c @@ -0,0 +1,131 @@ +/* + * libgit2 "ls-files" example - shows how to view all files currently in the index + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/** + * This example demonstrates the libgit2 index APIs to roughly + * simulate the output of `git ls-files`. + * `git ls-files` has many options and this currently does not show them. + * + * `git ls-files` base command shows all paths in the index at that time. + * This includes staged and committed files, but unstaged files will not display. + * + * This currently supports the default behavior and the `--error-unmatch` option. + */ + +struct ls_options { + int error_unmatch; + char *files[1024]; + size_t file_count; +}; + +static void usage(const char *message, const char *arg) +{ + if (message && arg) + fprintf(stderr, "%s: %s\n", message, arg); + else if (message) + fprintf(stderr, "%s\n", message); + fprintf(stderr, "usage: ls-files [--error-unmatch] [--] [...]\n"); + exit(1); +} + +static int parse_options(struct ls_options *opts, int argc, char *argv[]) +{ + int parsing_files = 0; + int i; + + memset(opts, 0, sizeof(struct ls_options)); + + if (argc < 2) + return 0; + + for (i = 1; i < argc; ++i) { + char *a = argv[i]; + + /* if it doesn't start with a '-' or is after the '--' then it is a file */ + if (a[0] != '-' || parsing_files) { + parsing_files = 1; + + /* watch for overflows (just in case) */ + if (opts->file_count == 1024) { + fprintf(stderr, "ls-files can only support 1024 files at this time.\n"); + return -1; + } + + opts->files[opts->file_count++] = a; + } else if (!strcmp(a, "--")) { + parsing_files = 1; + } else if (!strcmp(a, "--error-unmatch")) { + opts->error_unmatch = 1; + } else { + usage("Unsupported argument", a); + return -1; + } + } + + return 0; +} + +static int print_paths(struct ls_options *opts, git_index *index) +{ + size_t i; + const git_index_entry *entry; + + /* if there are no files explicitly listed by the user print all entries in the index */ + if (opts->file_count == 0) { + size_t entry_count = git_index_entrycount(index); + + for (i = 0; i < entry_count; i++) { + entry = git_index_get_byindex(index, i); + puts(entry->path); + } + return 0; + } + + /* loop through the files found in the args and print them if they exist */ + for (i = 0; i < opts->file_count; ++i) { + const char *path = opts->files[i]; + + if ((entry = git_index_get_bypath(index, path, GIT_INDEX_STAGE_NORMAL)) != NULL) { + puts(path); + } else if (opts->error_unmatch) { + fprintf(stderr, "error: pathspec '%s' did not match any file(s) known to git.\n", path); + fprintf(stderr, "Did you forget to 'git add'?\n"); + return -1; + } + } + + return 0; +} + +int lg2_ls_files(git_repository *repo, int argc, char *argv[]) +{ + git_index *index = NULL; + struct ls_options opts; + int error; + + if ((error = parse_options(&opts, argc, argv)) < 0) + return error; + + if ((error = git_repository_index(&index, repo)) < 0) + goto cleanup; + + error = print_paths(&opts, index); + +cleanup: + git_index_free(index); + + return error; +} diff --git a/examples/ls-remote.c b/examples/ls-remote.c new file mode 100644 index 00000000000..24caae7126e --- /dev/null +++ b/examples/ls-remote.c @@ -0,0 +1,60 @@ +#include "common.h" + +static int use_remote(git_repository *repo, char *name) +{ + git_remote *remote = NULL; + int error; + const git_remote_head **refs; + size_t refs_len, i; + git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT; + + /* Find the remote by name */ + error = git_remote_lookup(&remote, repo, name); + if (error < 0) { + error = git_remote_create_anonymous(&remote, repo, name); + if (error < 0) + goto cleanup; + } + + /** + * Connect to the remote and call the printing function for + * each of the remote references. + */ + callbacks.credentials = cred_acquire_cb; + + error = git_remote_connect(remote, GIT_DIRECTION_FETCH, &callbacks, NULL, NULL); + if (error < 0) + goto cleanup; + + /** + * Get the list of references on the remote and print out + * their name next to what they point to. + */ + if (git_remote_ls(&refs, &refs_len, remote) < 0) + goto cleanup; + + for (i = 0; i < refs_len; i++) { + char oid[GIT_OID_SHA1_HEXSIZE + 1] = {0}; + git_oid_fmt(oid, &refs[i]->oid); + printf("%s\t%s\n", oid, refs[i]->name); + } + +cleanup: + git_remote_free(remote); + return error; +} + +/** Entry point for this command */ +int lg2_ls_remote(git_repository *repo, int argc, char **argv) +{ + int error; + + if (argc < 2) { + fprintf(stderr, "usage: %s ls-remote \n", argv[-1]); + return EXIT_FAILURE; + } + + error = use_remote(repo, argv[1]); + + return error; +} diff --git a/examples/merge.c b/examples/merge.c new file mode 100644 index 00000000000..7a76912cd24 --- /dev/null +++ b/examples/merge.c @@ -0,0 +1,361 @@ +/* + * libgit2 "merge" example - shows how to perform merges + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/** The following example demonstrates how to do merges with libgit2. + * + * It will merge whatever commit-ish you pass in into the current branch. + * + * Recognized options are : + * --no-commit: don't actually commit the merge. + * + */ + +struct merge_options { + const char **heads; + size_t heads_count; + + git_annotated_commit **annotated; + size_t annotated_count; + + unsigned int no_commit : 1; +}; + +static void print_usage(void) +{ + fprintf(stderr, "usage: merge [--no-commit] \n"); + exit(1); +} + +static void merge_options_init(struct merge_options *opts) +{ + memset(opts, 0, sizeof(*opts)); + + opts->heads = NULL; + opts->heads_count = 0; + opts->annotated = NULL; + opts->annotated_count = 0; +} + +static void opts_add_refish(struct merge_options *opts, const char *refish) +{ + size_t sz; + + assert(opts != NULL); + + sz = ++opts->heads_count * sizeof(opts->heads[0]); + opts->heads = xrealloc((void *) opts->heads, sz); + opts->heads[opts->heads_count - 1] = refish; +} + +static void parse_options(const char **repo_path, struct merge_options *opts, int argc, char **argv) +{ + struct args_info args = ARGS_INFO_INIT; + + if (argc <= 1) + print_usage(); + + for (args.pos = 1; args.pos < argc; ++args.pos) { + const char *curr = argv[args.pos]; + + if (curr[0] != '-') { + opts_add_refish(opts, curr); + } else if (!strcmp(curr, "--no-commit")) { + opts->no_commit = 1; + } else if (match_str_arg(repo_path, &args, "--git-dir")) { + continue; + } else { + print_usage(); + } + } +} + +static int resolve_heads(git_repository *repo, struct merge_options *opts) +{ + git_annotated_commit **annotated = calloc(opts->heads_count, sizeof(git_annotated_commit *)); + size_t annotated_count = 0, i; + int err = 0; + + for (i = 0; i < opts->heads_count; i++) { + err = resolve_refish(&annotated[annotated_count++], repo, opts->heads[i]); + if (err != 0) { + fprintf(stderr, "failed to resolve refish %s: %s\n", opts->heads[i], git_error_last()->message); + annotated_count--; + continue; + } + } + + if (annotated_count != opts->heads_count) { + fprintf(stderr, "unable to parse some refish\n"); + free(annotated); + return -1; + } + + opts->annotated = annotated; + opts->annotated_count = annotated_count; + return 0; +} + +static int perform_fastforward(git_repository *repo, const git_oid *target_oid, int is_unborn) +{ + git_checkout_options ff_checkout_options = GIT_CHECKOUT_OPTIONS_INIT; + git_reference *target_ref; + git_reference *new_target_ref; + git_object *target = NULL; + int err = 0; + + if (is_unborn) { + const char *symbolic_ref; + git_reference *head_ref; + + /* HEAD reference is unborn, lookup manually so we don't try to resolve it */ + err = git_reference_lookup(&head_ref, repo, "HEAD"); + if (err != 0) { + fprintf(stderr, "failed to lookup HEAD ref\n"); + return -1; + } + + /* Grab the reference HEAD should be pointing to */ + symbolic_ref = git_reference_symbolic_target(head_ref); + + /* Create our master reference on the target OID */ + err = git_reference_create(&target_ref, repo, symbolic_ref, target_oid, 0, NULL); + if (err != 0) { + fprintf(stderr, "failed to create master reference\n"); + return -1; + } + + git_reference_free(head_ref); + } else { + /* HEAD exists, just lookup and resolve */ + err = git_repository_head(&target_ref, repo); + if (err != 0) { + fprintf(stderr, "failed to get HEAD reference\n"); + return -1; + } + } + + /* Lookup the target object */ + err = git_object_lookup(&target, repo, target_oid, GIT_OBJECT_COMMIT); + if (err != 0) { + fprintf(stderr, "failed to lookup OID %s\n", git_oid_tostr_s(target_oid)); + return -1; + } + + /* Checkout the result so the workdir is in the expected state */ + ff_checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE; + err = git_checkout_tree(repo, target, &ff_checkout_options); + if (err != 0) { + fprintf(stderr, "failed to checkout HEAD reference\n"); + return -1; + } + + /* Move the target reference to the target OID */ + err = git_reference_set_target(&new_target_ref, target_ref, target_oid, NULL); + if (err != 0) { + fprintf(stderr, "failed to move HEAD reference\n"); + return -1; + } + + git_reference_free(target_ref); + git_reference_free(new_target_ref); + git_object_free(target); + + return 0; +} + +static void output_conflicts(git_index *index) +{ + git_index_conflict_iterator *conflicts; + const git_index_entry *ancestor; + const git_index_entry *our; + const git_index_entry *their; + int err = 0; + + check_lg2(git_index_conflict_iterator_new(&conflicts, index), "failed to create conflict iterator", NULL); + + while ((err = git_index_conflict_next(&ancestor, &our, &their, conflicts)) == 0) { + fprintf(stderr, "conflict: a:%s o:%s t:%s\n", + ancestor ? ancestor->path : "NULL", + our->path ? our->path : "NULL", + their->path ? their->path : "NULL"); + } + + if (err != GIT_ITEROVER) { + fprintf(stderr, "error iterating conflicts\n"); + } + + git_index_conflict_iterator_free(conflicts); +} + +static int create_merge_commit(git_repository *repo, git_index *index, struct merge_options *opts) +{ + git_oid tree_oid, commit_oid; + git_tree *tree; + git_signature *sign; + git_reference *merge_ref = NULL; + git_annotated_commit *merge_commit; + git_reference *head_ref; + git_commit **parents = calloc(opts->annotated_count + 1, sizeof(git_commit *)); + const char *msg_target = NULL; + size_t msglen = 0; + char *msg; + size_t i; + int err; + + /* Grab our needed references */ + check_lg2(git_repository_head(&head_ref, repo), "failed to get repo HEAD", NULL); + if (resolve_refish(&merge_commit, repo, opts->heads[0])) { + fprintf(stderr, "failed to resolve refish %s", opts->heads[0]); + free(parents); + return -1; + } + + /* Maybe that's a ref, so DWIM it */ + err = git_reference_dwim(&merge_ref, repo, opts->heads[0]); + check_lg2(err, "failed to DWIM reference", git_error_last()->message); + + /* Grab a signature */ + check_lg2(git_signature_now(&sign, "Me", "me@example.com"), "failed to create signature", NULL); + +#define MERGE_COMMIT_MSG "Merge %s '%s'" + /* Prepare a standard merge commit message */ + if (merge_ref != NULL) { + check_lg2(git_branch_name(&msg_target, merge_ref), "failed to get branch name of merged ref", NULL); + } else { + msg_target = git_oid_tostr_s(git_annotated_commit_id(merge_commit)); + } + + msglen = snprintf(NULL, 0, MERGE_COMMIT_MSG, (merge_ref ? "branch" : "commit"), msg_target); + if (msglen > 0) msglen++; + msg = malloc(msglen); + err = snprintf(msg, msglen, MERGE_COMMIT_MSG, (merge_ref ? "branch" : "commit"), msg_target); + + /* This is only to silence the compiler */ + if (err < 0) goto cleanup; + + /* Setup our parent commits */ + err = git_reference_peel((git_object **)&parents[0], head_ref, GIT_OBJECT_COMMIT); + check_lg2(err, "failed to peel head reference", NULL); + for (i = 0; i < opts->annotated_count; i++) { + git_commit_lookup(&parents[i + 1], repo, git_annotated_commit_id(opts->annotated[i])); + } + + /* Prepare our commit tree */ + check_lg2(git_index_write_tree(&tree_oid, index), "failed to write merged tree", NULL); + check_lg2(git_tree_lookup(&tree, repo, &tree_oid), "failed to lookup tree", NULL); + + /* Commit time ! */ + err = git_commit_create(&commit_oid, + repo, git_reference_name(head_ref), + sign, sign, + NULL, msg, + tree, + opts->annotated_count + 1, (const git_commit **)parents); + check_lg2(err, "failed to create commit", NULL); + + /* We're done merging, cleanup the repository state */ + git_repository_state_cleanup(repo); + +cleanup: + free(parents); + return err; +} + +int lg2_merge(git_repository *repo, int argc, char **argv) +{ + struct merge_options opts; + git_index *index; + git_repository_state_t state; + git_merge_analysis_t analysis; + git_merge_preference_t preference; + const char *path = "."; + int err = 0; + + merge_options_init(&opts); + parse_options(&path, &opts, argc, argv); + + state = git_repository_state(repo); + if (state != GIT_REPOSITORY_STATE_NONE) { + fprintf(stderr, "repository is in unexpected state %d\n", state); + goto cleanup; + } + + err = resolve_heads(repo, &opts); + if (err != 0) + goto cleanup; + + err = git_merge_analysis(&analysis, &preference, + repo, + (const git_annotated_commit **)opts.annotated, + opts.annotated_count); + check_lg2(err, "merge analysis failed", NULL); + + if (analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE) { + printf("Already up-to-date\n"); + return 0; + } else if (analysis & GIT_MERGE_ANALYSIS_UNBORN || + (analysis & GIT_MERGE_ANALYSIS_FASTFORWARD && + !(preference & GIT_MERGE_PREFERENCE_NO_FASTFORWARD))) { + const git_oid *target_oid; + if (analysis & GIT_MERGE_ANALYSIS_UNBORN) { + printf("Unborn\n"); + } else { + printf("Fast-forward\n"); + } + + /* Since this is a fast-forward, there can be only one merge head */ + target_oid = git_annotated_commit_id(opts.annotated[0]); + assert(opts.annotated_count == 1); + + return perform_fastforward(repo, target_oid, (analysis & GIT_MERGE_ANALYSIS_UNBORN)); + } else if (analysis & GIT_MERGE_ANALYSIS_NORMAL) { + git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT; + git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; + + merge_opts.flags = 0; + merge_opts.file_flags = GIT_MERGE_FILE_STYLE_DIFF3; + + checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE|GIT_CHECKOUT_ALLOW_CONFLICTS; + + if (preference & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY) { + printf("Fast-forward is preferred, but only a merge is possible\n"); + return -1; + } + + err = git_merge(repo, + (const git_annotated_commit **)opts.annotated, opts.annotated_count, + &merge_opts, &checkout_opts); + check_lg2(err, "merge failed", NULL); + } + + /* If we get here, we actually performed the merge above */ + + check_lg2(git_repository_index(&index, repo), "failed to get repository index", NULL); + + if (git_index_has_conflicts(index)) { + /* Handle conflicts */ + output_conflicts(index); + } else if (!opts.no_commit) { + create_merge_commit(repo, index, &opts); + printf("Merge made\n"); + } + +cleanup: + free((char **)opts.heads); + free(opts.annotated); + + return 0; +} diff --git a/examples/network/.gitignore b/examples/network/.gitignore deleted file mode 100644 index 1b48e66ed98..00000000000 --- a/examples/network/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/git2 diff --git a/examples/network/Makefile b/examples/network/Makefile deleted file mode 100644 index 60969bd87f4..00000000000 --- a/examples/network/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -default: all - -CC = gcc -CFLAGS += -g -CFLAGS += -I../../include -LDFLAGS += -L../../build -L../.. -lgit2 -lpthread - -OBJECTS = \ - git2.o \ - ls-remote.o \ - fetch.o \ - clone.o \ - index-pack.o - -all: $(OBJECTS) - $(CC) $(CFLAGS) $(LDFLAGS) -o git2 $(OBJECTS) - -clean: - $(RM) $(OBJECTS) - $(RM) git2 diff --git a/examples/network/clone.c b/examples/network/clone.c deleted file mode 100644 index 63072eea042..00000000000 --- a/examples/network/clone.c +++ /dev/null @@ -1,117 +0,0 @@ -#include "common.h" -#include -#include -#include -#include -#include -#ifndef _WIN32 -# include -# include -#endif - -/* Shamelessly borrowed from http://stackoverflow.com/questions/3417837/ */ -#ifdef UNUSED -#elif defined(__GNUC__) -# define UNUSED(x) UNUSED_ ## x __attribute__((unused)) -#elif defined(__LCLINT__) -# define UNUSED(x) /*@unused@*/ x -#else -# define UNUSED(x) x -#endif - -typedef struct progress_data { - git_transfer_progress fetch_progress; - size_t completed_steps; - size_t total_steps; - const char *path; -} progress_data; - -static void print_progress(const progress_data *pd) -{ - int network_percent = (100*pd->fetch_progress.received_objects) / pd->fetch_progress.total_objects; - int index_percent = (100*pd->fetch_progress.indexed_objects) / pd->fetch_progress.total_objects; - int checkout_percent = pd->total_steps > 0 - ? (100 * pd->completed_steps) / pd->total_steps - : 0.f; - int kbytes = pd->fetch_progress.received_bytes / 1024; - - printf("net %3d%% (%4d kb, %5d/%5d) / idx %3d%% (%5d/%5d) / chk %3d%% (%4" PRIuZ "/%4" PRIuZ ") %s\n", - network_percent, kbytes, - pd->fetch_progress.received_objects, pd->fetch_progress.total_objects, - index_percent, pd->fetch_progress.indexed_objects, pd->fetch_progress.total_objects, - checkout_percent, - pd->completed_steps, pd->total_steps, - pd->path); -} - -static void fetch_progress(const git_transfer_progress *stats, void *payload) -{ - progress_data *pd = (progress_data*)payload; - pd->fetch_progress = *stats; - print_progress(pd); -} -static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload) -{ - progress_data *pd = (progress_data*)payload; - pd->completed_steps = cur; - pd->total_steps = tot; - pd->path = path; - print_progress(pd); -} - -static int cred_acquire(git_cred **out, - const char * UNUSED(url), - unsigned int UNUSED(allowed_types), - void * UNUSED(payload)) -{ - char username[128] = {0}; - char password[128] = {0}; - - printf("Username: "); - scanf("%s", username); - - /* Yup. Right there on your terminal. Careful where you copy/paste output. */ - printf("Password: "); - scanf("%s", password); - - return git_cred_userpass_plaintext_new(out, username, password); -} - -int do_clone(git_repository *repo, int argc, char **argv) -{ - progress_data pd = {{0}}; - git_repository *cloned_repo = NULL; - git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; - git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT; - const char *url = argv[1]; - const char *path = argv[2]; - int error; - - (void)repo; // unused - - // Validate args - if (argc < 3) { - printf ("USAGE: %s \n", argv[0]); - return -1; - } - - // Set up options - checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - checkout_opts.progress_cb = checkout_progress; - checkout_opts.progress_payload = &pd; - clone_opts.checkout_opts = checkout_opts; - clone_opts.fetch_progress_cb = &fetch_progress; - clone_opts.fetch_progress_payload = &pd; - clone_opts.cred_acquire_cb = cred_acquire; - - // Do the clone - error = git_clone(&cloned_repo, url, path, &clone_opts); - printf("\n"); - if (error != 0) { - const git_error *err = giterr_last(); - if (err) printf("ERROR %d: %s\n", err->klass, err->message); - else printf("ERROR %d: no detailed info\n", error); - } - else if (cloned_repo) git_repository_free(cloned_repo); - return error; -} diff --git a/examples/network/common.h b/examples/network/common.h deleted file mode 100644 index a4cfa1a7ed0..00000000000 --- a/examples/network/common.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef __COMMON_H__ -#define __COMMON_H__ - -#include - -typedef int (*git_cb)(git_repository *, int , char **); - -int ls_remote(git_repository *repo, int argc, char **argv); -int parse_pkt_line(git_repository *repo, int argc, char **argv); -int show_remote(git_repository *repo, int argc, char **argv); -int fetch(git_repository *repo, int argc, char **argv); -int index_pack(git_repository *repo, int argc, char **argv); -int do_clone(git_repository *repo, int argc, char **argv); - -#ifndef PRIuZ -/* Define the printf format specifer to use for size_t output */ -#if defined(_MSC_VER) || defined(__MINGW32__) -# define PRIuZ "Iu" -#else -# define PRIuZ "zu" -#endif -#endif - -#endif /* __COMMON_H__ */ diff --git a/examples/network/fetch.c b/examples/network/fetch.c deleted file mode 100644 index d5caad4de27..00000000000 --- a/examples/network/fetch.c +++ /dev/null @@ -1,143 +0,0 @@ -#include "common.h" -#include -#include -#include -#include -#ifndef _WIN32 -# include -# include -#endif - -struct dl_data { - git_remote *remote; - int ret; - int finished; -}; - -static void progress_cb(const char *str, int len, void *data) -{ - data = data; - printf("remote: %.*s", len, str); - fflush(stdout); /* We don't have the \n to force the flush */ -} - -static void *download(void *ptr) -{ - struct dl_data *data = (struct dl_data *)ptr; - - // Connect to the remote end specifying that we want to fetch - // information from it. - if (git_remote_connect(data->remote, GIT_DIRECTION_FETCH) < 0) { - data->ret = -1; - goto exit; - } - - // Download the packfile and index it. This function updates the - // amount of received data and the indexer stats which lets you - // inform the user about progress. - if (git_remote_download(data->remote, NULL, NULL) < 0) { - data->ret = -1; - goto exit; - } - - data->ret = 0; - -exit: - data->finished = 1; - return &data->ret; -} - -static int update_cb(const char *refname, const git_oid *a, const git_oid *b, void *data) -{ - char a_str[GIT_OID_HEXSZ+1], b_str[GIT_OID_HEXSZ+1]; - data = data; - - git_oid_fmt(b_str, b); - b_str[GIT_OID_HEXSZ] = '\0'; - - if (git_oid_iszero(a)) { - printf("[new] %.20s %s\n", b_str, refname); - } else { - git_oid_fmt(a_str, a); - a_str[GIT_OID_HEXSZ] = '\0'; - printf("[updated] %.10s..%.10s %s\n", a_str, b_str, refname); - } - - return 0; -} - -int fetch(git_repository *repo, int argc, char **argv) -{ - git_remote *remote = NULL; - const git_transfer_progress *stats; - struct dl_data data; - git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT; -#ifndef _WIN32 - pthread_t worker; -#endif - - argc = argc; - // Figure out whether it's a named remote or a URL - printf("Fetching %s for repo %p\n", argv[1], repo); - if (git_remote_load(&remote, repo, argv[1]) < 0) { - if (git_remote_create_inmemory(&remote, repo, NULL, argv[1]) < 0) - return -1; - } - - // Set up the callbacks (only update_tips for now) - callbacks.update_tips = &update_cb; - callbacks.progress = &progress_cb; - git_remote_set_callbacks(remote, &callbacks); - - // Set up the information for the background worker thread - data.remote = remote; - data.ret = 0; - data.finished = 0; - - stats = git_remote_stats(remote); - -#ifdef _WIN32 - download(&data); -#else - pthread_create(&worker, NULL, download, &data); - - // Loop while the worker thread is still running. Here we show processed - // and total objects in the pack and the amount of received - // data. Most frontends will probably want to show a percentage and - // the download rate. - do { - usleep(10000); - - if (stats->total_objects > 0) - printf("Received %d/%d objects (%d) in %" PRIuZ " bytes\r", - stats->received_objects, stats->total_objects, - stats->indexed_objects, stats->received_bytes); - } while (!data.finished); - - if (data.ret < 0) - goto on_error; - - pthread_join(worker, NULL); -#endif - - printf("\rReceived %d/%d objects in %zu bytes\n", - stats->indexed_objects, stats->total_objects, stats->received_bytes); - - // Disconnect the underlying connection to prevent from idling. - git_remote_disconnect(remote); - - // Update the references in the remote's namespace to point to the - // right commits. This may be needed even if there was no packfile - // to download, which can happen e.g. when the branches have been - // changed but all the neede objects are available locally. - if (git_remote_update_tips(remote) < 0) - return -1; - - git_remote_free(remote); - - return 0; - - on_error: - git_remote_free(remote); - return -1; -} diff --git a/examples/network/git2.c b/examples/network/git2.c deleted file mode 100644 index ecb16630b65..00000000000 --- a/examples/network/git2.c +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include -#include - -#include "common.h" - -// This part is not strictly libgit2-dependent, but you can use this -// as a starting point for a git-like tool - -struct { - char *name; - git_cb fn; -} commands[] = { - {"ls-remote", ls_remote}, - {"fetch", fetch}, - {"clone", do_clone}, - {"index-pack", index_pack}, - { NULL, NULL} -}; - -static int run_command(git_cb fn, int argc, char **argv) -{ - int error; - git_repository *repo; - -// Before running the actual command, create an instance of the local -// repository and pass it to the function. - - error = git_repository_open(&repo, ".git"); - if (error < 0) - repo = NULL; - - // Run the command. If something goes wrong, print the error message to stderr - error = fn(repo, argc, argv); - if (error < 0) { - if (giterr_last() == NULL) - fprintf(stderr, "Error without message"); - else - fprintf(stderr, "Bad news:\n %s\n", giterr_last()->message); - } - - if(repo) - git_repository_free(repo); - - return !!error; -} - -int main(int argc, char **argv) -{ - int i; - - if (argc < 2) { - fprintf(stderr, "usage: %s [repo]\n", argv[0]); - exit(EXIT_FAILURE); - } - - for (i = 0; commands[i].name != NULL; ++i) { - if (!strcmp(argv[1], commands[i].name)) - return run_command(commands[i].fn, --argc, ++argv); - } - - fprintf(stderr, "Command not found: %s\n", argv[1]); - return 1; -} diff --git a/examples/network/index-pack.c b/examples/network/index-pack.c deleted file mode 100644 index 3fc4f328857..00000000000 --- a/examples/network/index-pack.c +++ /dev/null @@ -1,87 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#ifdef _WIN32 -# include -# include - -# define open _open -# define read _read -# define close _close - -#define ssize_t unsigned int -#else -# include -#endif -#include "common.h" - -// This could be run in the main loop whilst the application waits for -// the indexing to finish in a worker thread -static int index_cb(const git_transfer_progress *stats, void *data) -{ - data = data; - printf("\rProcessing %d of %d", stats->indexed_objects, stats->total_objects); - - return 0; -} - -int index_pack(git_repository *repo, int argc, char **argv) -{ - git_indexer_stream *idx; - git_transfer_progress stats = {0, 0}; - int error; - char hash[GIT_OID_HEXSZ + 1] = {0}; - int fd; - ssize_t read_bytes; - char buf[512]; - - repo = repo; - if (argc < 2) { - fprintf(stderr, "I need a packfile\n"); - return EXIT_FAILURE; - } - - if (git_indexer_stream_new(&idx, ".", NULL, NULL) < 0) { - puts("bad idx"); - return -1; - } - - if ((fd = open(argv[1], 0)) < 0) { - perror("open"); - return -1; - } - - do { - read_bytes = read(fd, buf, sizeof(buf)); - if (read_bytes < 0) - break; - - if ((error = git_indexer_stream_add(idx, buf, read_bytes, &stats)) < 0) - goto cleanup; - - index_cb(&stats, NULL); - } while (read_bytes > 0); - - if (read_bytes < 0) { - error = -1; - perror("failed reading"); - goto cleanup; - } - - if ((error = git_indexer_stream_finalize(idx, &stats)) < 0) - goto cleanup; - - printf("\rIndexing %d of %d\n", stats.indexed_objects, stats.total_objects); - - git_oid_fmt(hash, git_indexer_stream_hash(idx)); - puts(hash); - - cleanup: - close(fd); - git_indexer_stream_free(idx); - return error; -} diff --git a/examples/network/ls-remote.c b/examples/network/ls-remote.c deleted file mode 100644 index 737eeacd396..00000000000 --- a/examples/network/ls-remote.c +++ /dev/null @@ -1,79 +0,0 @@ -#include -#include -#include -#include -#include "common.h" - -static int show_ref__cb(git_remote_head *head, void *payload) -{ - char oid[GIT_OID_HEXSZ + 1] = {0}; - - payload = payload; - git_oid_fmt(oid, &head->oid); - printf("%s\t%s\n", oid, head->name); - return 0; -} - -static int use_unnamed(git_repository *repo, const char *url) -{ - git_remote *remote = NULL; - int error; - - // Create an instance of a remote from the URL. The transport to use - // is detected from the URL - error = git_remote_create_inmemory(&remote, repo, NULL, url); - if (error < 0) - goto cleanup; - - // When connecting, the underlying code needs to know wether we - // want to push or fetch - error = git_remote_connect(remote, GIT_DIRECTION_FETCH); - if (error < 0) - goto cleanup; - - // With git_remote_ls we can retrieve the advertised heads - error = git_remote_ls(remote, &show_ref__cb, NULL); - -cleanup: - git_remote_free(remote); - return error; -} - -static int use_remote(git_repository *repo, char *name) -{ - git_remote *remote = NULL; - int error; - - // Find the remote by name - error = git_remote_load(&remote, repo, name); - if (error < 0) - goto cleanup; - - error = git_remote_connect(remote, GIT_DIRECTION_FETCH); - if (error < 0) - goto cleanup; - - error = git_remote_ls(remote, &show_ref__cb, NULL); - -cleanup: - git_remote_free(remote); - return error; -} - -// This gets called to do the work. The remote can be given either as -// the name of a configured remote or an URL. - -int ls_remote(git_repository *repo, int argc, char **argv) -{ - int error; - - argc = argc; - /* If there's a ':' in the name, assume it's an URL */ - if (strchr(argv[1], ':') != NULL) { - error = use_unnamed(repo, argv[1]); - } else { - error = use_remote(repo, argv[1]); - } - - return error; -} diff --git a/examples/push.c b/examples/push.c new file mode 100644 index 00000000000..5113eed394b --- /dev/null +++ b/examples/push.c @@ -0,0 +1,61 @@ +/* + * libgit2 "push" example - shows how to push to remote + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/** + * This example demonstrates the libgit2 push API to roughly + * simulate `git push`. + * + * This does not have: + * + * - Robust error handling + * - Any of the `git push` options + * + * This does have: + * + * - Example of push to origin/master + * + */ + +/** Entry point for this command */ +int lg2_push(git_repository *repo, int argc, char **argv) { + git_push_options options; + git_remote_callbacks callbacks; + git_remote* remote = NULL; + char *refspec = "refs/heads/master"; + const git_strarray refspecs = { + &refspec, + 1 + }; + + /* Validate args */ + if (argc > 1) { + printf ("USAGE: %s\n\nsorry, no arguments supported yet\n", argv[0]); + return -1; + } + + check_lg2(git_remote_lookup(&remote, repo, "origin" ), "Unable to lookup remote", NULL); + + check_lg2(git_remote_init_callbacks(&callbacks, GIT_REMOTE_CALLBACKS_VERSION), "Error initializing remote callbacks", NULL); + callbacks.credentials = cred_acquire_cb; + + check_lg2(git_push_options_init(&options, GIT_PUSH_OPTIONS_VERSION ), "Error initializing push", NULL); + options.callbacks = callbacks; + + check_lg2(git_remote_push(remote, &refspecs, &options), "Error pushing", NULL); + + printf("pushed\n"); + return 0; +} diff --git a/examples/remote.c b/examples/remote.c new file mode 100644 index 00000000000..14fac030cd1 --- /dev/null +++ b/examples/remote.c @@ -0,0 +1,256 @@ +/* + * libgit2 "remote" example - shows how to modify remotes for a repo + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/** + * This is a sample program that is similar to "git remote". See the + * documentation for that (try "git help remote") to understand what this + * program is emulating. + * + * This demonstrates using the libgit2 APIs to modify remotes of a repository. + */ + +enum subcmd { + subcmd_add, + subcmd_remove, + subcmd_rename, + subcmd_seturl, + subcmd_show +}; + +struct remote_opts { + enum subcmd cmd; + + /* for command-specific args */ + int argc; + char **argv; +}; + +static int cmd_add(git_repository *repo, struct remote_opts *o); +static int cmd_remove(git_repository *repo, struct remote_opts *o); +static int cmd_rename(git_repository *repo, struct remote_opts *o); +static int cmd_seturl(git_repository *repo, struct remote_opts *o); +static int cmd_show(git_repository *repo, struct remote_opts *o); + +static void parse_subcmd( + struct remote_opts *opt, int argc, char **argv); +static void usage(const char *msg, const char *arg); + +int lg2_remote(git_repository *repo, int argc, char *argv[]) +{ + int retval = 0; + struct remote_opts opt = {0}; + + parse_subcmd(&opt, argc, argv); + + switch (opt.cmd) + { + case subcmd_add: + retval = cmd_add(repo, &opt); + break; + case subcmd_remove: + retval = cmd_remove(repo, &opt); + break; + case subcmd_rename: + retval = cmd_rename(repo, &opt); + break; + case subcmd_seturl: + retval = cmd_seturl(repo, &opt); + break; + case subcmd_show: + retval = cmd_show(repo, &opt); + break; + } + + return retval; +} + +static int cmd_add(git_repository *repo, struct remote_opts *o) +{ + char *name, *url; + git_remote *remote = {0}; + + if (o->argc != 2) + usage("you need to specify a name and URL", NULL); + + name = o->argv[0]; + url = o->argv[1]; + + check_lg2(git_remote_create(&remote, repo, name, url), + "could not create remote", NULL); + + return 0; +} + +static int cmd_remove(git_repository *repo, struct remote_opts *o) +{ + char *name; + + if (o->argc != 1) + usage("you need to specify a name", NULL); + + name = o->argv[0]; + + check_lg2(git_remote_delete(repo, name), + "could not delete remote", name); + + return 0; +} + +static int cmd_rename(git_repository *repo, struct remote_opts *o) +{ + int i, retval; + char *old, *new; + git_strarray problems = {0}; + + if (o->argc != 2) + usage("you need to specify old and new remote name", NULL); + + old = o->argv[0]; + new = o->argv[1]; + + retval = git_remote_rename(&problems, repo, old, new); + if (!retval) + return 0; + + for (i = 0; i < (int) problems.count; i++) { + puts(problems.strings[0]); + } + + git_strarray_dispose(&problems); + + return retval; +} + +static int cmd_seturl(git_repository *repo, struct remote_opts *o) +{ + int i, retval, push = 0; + char *name = NULL, *url = NULL; + + for (i = 0; i < o->argc; i++) { + char *arg = o->argv[i]; + + if (!strcmp(arg, "--push")) { + push = 1; + } else if (arg[0] != '-' && name == NULL) { + name = arg; + } else if (arg[0] != '-' && url == NULL) { + url = arg; + } else { + usage("invalid argument to set-url", arg); + } + } + + if (name == NULL || url == NULL) + usage("you need to specify remote and the new URL", NULL); + + if (push) + retval = git_remote_set_pushurl(repo, name, url); + else + retval = git_remote_set_url(repo, name, url); + + check_lg2(retval, "could not set URL", url); + + return 0; +} + +static int cmd_show(git_repository *repo, struct remote_opts *o) +{ + int i; + const char *arg, *name, *fetch, *push; + int verbose = 0; + git_strarray remotes = {0}; + git_remote *remote = {0}; + + for (i = 0; i < o->argc; i++) { + arg = o->argv[i]; + + if (!strcmp(arg, "-v") || !strcmp(arg, "--verbose")) { + verbose = 1; + } + } + + check_lg2(git_remote_list(&remotes, repo), + "could not retrieve remotes", NULL); + + for (i = 0; i < (int) remotes.count; i++) { + name = remotes.strings[i]; + if (!verbose) { + puts(name); + continue; + } + + check_lg2(git_remote_lookup(&remote, repo, name), + "could not look up remote", name); + + fetch = git_remote_url(remote); + if (fetch) + printf("%s\t%s (fetch)\n", name, fetch); + push = git_remote_pushurl(remote); + /* use fetch URL if no distinct push URL has been set */ + push = push ? push : fetch; + if (push) + printf("%s\t%s (push)\n", name, push); + + git_remote_free(remote); + } + + git_strarray_dispose(&remotes); + + return 0; +} + +static void parse_subcmd( + struct remote_opts *opt, int argc, char **argv) +{ + char *arg = argv[1]; + enum subcmd cmd = 0; + + if (argc < 2) + usage("no command specified", NULL); + + if (!strcmp(arg, "add")) { + cmd = subcmd_add; + } else if (!strcmp(arg, "remove")) { + cmd = subcmd_remove; + } else if (!strcmp(arg, "rename")) { + cmd = subcmd_rename; + } else if (!strcmp(arg, "set-url")) { + cmd = subcmd_seturl; + } else if (!strcmp(arg, "show")) { + cmd = subcmd_show; + } else { + usage("command is not valid", arg); + } + opt->cmd = cmd; + + opt->argc = argc - 2; /* executable and subcommand are removed */ + opt->argv = argv + 2; +} + +static void usage(const char *msg, const char *arg) +{ + fputs("usage: remote add \n", stderr); + fputs(" remote remove \n", stderr); + fputs(" remote rename \n", stderr); + fputs(" remote set-url [--push] \n", stderr); + fputs(" remote show [-v|--verbose]\n", stderr); + + if (msg && !arg) + fprintf(stderr, "\n%s\n", msg); + else if (msg && arg) + fprintf(stderr, "\n%s: %s\n", msg, arg); + exit(1); +} diff --git a/examples/rev-list.c b/examples/rev-list.c new file mode 100644 index 00000000000..a7598b8247d --- /dev/null +++ b/examples/rev-list.c @@ -0,0 +1,158 @@ +/* + * libgit2 "rev-list" example - shows how to transform a rev-spec into a list + * of commit ids + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +#include + +static int revwalk_parse_options(git_sort_t *sort, struct args_info *args); +static int revwalk_parse_revs(git_repository *repo, git_revwalk *walk, struct args_info *args); + +int lg2_rev_list(git_repository *repo, int argc, char **argv) +{ + struct args_info args = ARGS_INFO_INIT; + git_revwalk *walk; + git_oid oid; + git_sort_t sort; + char buf[GIT_OID_SHA1_HEXSIZE+1]; + + check_lg2(revwalk_parse_options(&sort, &args), "parsing options", NULL); + + check_lg2(git_revwalk_new(&walk, repo), "allocating revwalk", NULL); + git_revwalk_sorting(walk, sort); + check_lg2(revwalk_parse_revs(repo, walk, &args), "parsing revs", NULL); + + while (!git_revwalk_next(&oid, walk)) { + git_oid_fmt(buf, &oid); + buf[GIT_OID_SHA1_HEXSIZE] = '\0'; + printf("%s\n", buf); + } + + git_revwalk_free(walk); + return 0; +} + +static int push_commit(git_revwalk *walk, const git_oid *oid, int hide) +{ + if (hide) + return git_revwalk_hide(walk, oid); + else + return git_revwalk_push(walk, oid); +} + +static int push_spec(git_repository *repo, git_revwalk *walk, const char *spec, int hide) +{ + int error; + git_object *obj; + + if ((error = git_revparse_single(&obj, repo, spec)) < 0) + return error; + + error = push_commit(walk, git_object_id(obj), hide); + git_object_free(obj); + return error; +} + +static int push_range(git_repository *repo, git_revwalk *walk, const char *range, int hide) +{ + git_revspec revspec; + int error = 0; + + if ((error = git_revparse(&revspec, repo, range))) + return error; + + if (revspec.flags & GIT_REVSPEC_MERGE_BASE) { + /* TODO: support "..." */ + return GIT_EINVALIDSPEC; + } + + if ((error = push_commit(walk, git_object_id(revspec.from), !hide))) + goto out; + + error = push_commit(walk, git_object_id(revspec.to), hide); + +out: + git_object_free(revspec.from); + git_object_free(revspec.to); + return error; +} + +static void print_usage(void) +{ + fprintf(stderr, "rev-list [--git-dir=dir] [--topo-order|--date-order] [--reverse] \n"); + exit(-1); +} + +static int revwalk_parse_options(git_sort_t *sort, struct args_info *args) +{ + assert(sort && args); + *sort = GIT_SORT_NONE; + + if (args->argc < 1) + print_usage(); + + for (args->pos = 1; args->pos < args->argc; ++args->pos) { + const char *curr = args->argv[args->pos]; + + if (!strcmp(curr, "--topo-order")) { + *sort |= GIT_SORT_TOPOLOGICAL; + } else if (!strcmp(curr, "--date-order")) { + *sort |= GIT_SORT_TIME; + } else if (!strcmp(curr, "--reverse")) { + *sort |= (*sort & ~GIT_SORT_REVERSE) ^ GIT_SORT_REVERSE; + } else { + break; + } + } + return 0; +} + +static int revwalk_parse_revs(git_repository *repo, git_revwalk *walk, struct args_info *args) +{ + int hide, error; + git_oid oid; + + hide = 0; + for (; args->pos < args->argc; ++args->pos) { + const char *curr = args->argv[args->pos]; + + if (!strcmp(curr, "--not")) { + hide = !hide; + } else if (curr[0] == '^') { + if ((error = push_spec(repo, walk, curr + 1, !hide))) + return error; + } else if (strstr(curr, "..")) { + if ((error = push_range(repo, walk, curr, hide))) + return error; + } else { + if (push_spec(repo, walk, curr, hide) == 0) + continue; + +#ifdef GIT_EXPERIMENTAL_SHA256 + if ((error = git_oid_from_string(&oid, curr, GIT_OID_SHA1))) + return error; +#else + if ((error = git_oid_fromstr(&oid, curr))) + return error; +#endif + + if ((error = push_commit(walk, &oid, hide))) + return error; + } + } + + return 0; +} + diff --git a/examples/rev-parse.c b/examples/rev-parse.c new file mode 100644 index 00000000000..3f68d79b799 --- /dev/null +++ b/examples/rev-parse.c @@ -0,0 +1,102 @@ +/* + * libgit2 "rev-parse" example - shows how to parse revspecs + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/** Forward declarations for helpers. */ +struct parse_state { + const char *repodir; + const char *spec; + int not; +}; +static void parse_opts(struct parse_state *ps, int argc, char *argv[]); +static int parse_revision(git_repository *repo, struct parse_state *ps); + +int lg2_rev_parse(git_repository *repo, int argc, char *argv[]) +{ + struct parse_state ps = {0}; + + parse_opts(&ps, argc, argv); + + check_lg2(parse_revision(repo, &ps), "Parsing", NULL); + + return 0; +} + +static void usage(const char *message, const char *arg) +{ + if (message && arg) + fprintf(stderr, "%s: %s\n", message, arg); + else if (message) + fprintf(stderr, "%s\n", message); + fprintf(stderr, "usage: rev-parse [ --option ] ...\n"); + exit(1); +} + +static void parse_opts(struct parse_state *ps, int argc, char *argv[]) +{ + struct args_info args = ARGS_INFO_INIT; + + for (args.pos=1; args.pos < argc; ++args.pos) { + const char *a = argv[args.pos]; + + if (a[0] != '-') { + if (ps->spec) + usage("Too many specs", a); + ps->spec = a; + } else if (!strcmp(a, "--not")) + ps->not = !ps->not; + else if (!match_str_arg(&ps->repodir, &args, "--git-dir")) + usage("Cannot handle argument", a); + } +} + +static int parse_revision(git_repository *repo, struct parse_state *ps) +{ + git_revspec rs; + char str[GIT_OID_SHA1_HEXSIZE + 1]; + + check_lg2(git_revparse(&rs, repo, ps->spec), "Could not parse", ps->spec); + + if ((rs.flags & GIT_REVSPEC_SINGLE) != 0) { + git_oid_tostr(str, sizeof(str), git_object_id(rs.from)); + printf("%s\n", str); + git_object_free(rs.from); + } + else if ((rs.flags & GIT_REVSPEC_RANGE) != 0) { + git_oid_tostr(str, sizeof(str), git_object_id(rs.to)); + printf("%s\n", str); + git_object_free(rs.to); + + if ((rs.flags & GIT_REVSPEC_MERGE_BASE) != 0) { + git_oid base; + check_lg2(git_merge_base(&base, repo, + git_object_id(rs.from), git_object_id(rs.to)), + "Could not find merge base", ps->spec); + + git_oid_tostr(str, sizeof(str), &base); + printf("%s\n", str); + } + + git_oid_tostr(str, sizeof(str), git_object_id(rs.from)); + printf("^%s\n", str); + git_object_free(rs.from); + } + else { + fatal("Invalid results from git_revparse", ps->spec); + } + + return 0; +} + diff --git a/examples/show-index.c b/examples/show-index.c new file mode 100644 index 00000000000..fb797e04beb --- /dev/null +++ b/examples/show-index.c @@ -0,0 +1,66 @@ +/* + * libgit2 "showindex" example - shows how to extract data from the index + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +int lg2_show_index(git_repository *repo, int argc, char **argv) +{ + git_index *index; + size_t i, ecount; + char *dir = "."; + size_t dirlen; + char out[GIT_OID_SHA1_HEXSIZE+1]; + out[GIT_OID_SHA1_HEXSIZE] = '\0'; + + if (argc > 2) + fatal("usage: showindex []", NULL); + if (argc > 1) + dir = argv[1]; + + dirlen = strlen(dir); + if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) { + check_lg2(git_index_open(&index, dir), "could not open index", dir); + } else { + check_lg2(git_repository_open_ext(&repo, dir, 0, NULL), "could not open repository", dir); + check_lg2(git_repository_index(&index, repo), "could not open repository index", NULL); + git_repository_free(repo); + } + + git_index_read(index, 0); + + ecount = git_index_entrycount(index); + if (!ecount) + printf("Empty index\n"); + + for (i = 0; i < ecount; ++i) { + const git_index_entry *e = git_index_get_byindex(index, i); + + git_oid_fmt(out, &e->id); + + printf("File Path: %s\n", e->path); + printf(" Stage: %d\n", git_index_entry_stage(e)); + printf(" Blob SHA: %s\n", out); + printf("File Mode: %07o\n", e->mode); + printf("File Size: %d bytes\n", (int)e->file_size); + printf("Dev/Inode: %d/%d\n", (int)e->dev, (int)e->ino); + printf(" UID/GID: %d/%d\n", (int)e->uid, (int)e->gid); + printf(" ctime: %d\n", (int)e->ctime.seconds); + printf(" mtime: %d\n", (int)e->mtime.seconds); + printf("\n"); + } + + git_index_free(index); + + return 0; +} diff --git a/examples/showindex.c b/examples/showindex.c deleted file mode 100644 index e92a9c8de91..00000000000 --- a/examples/showindex.c +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include - -int main (int argc, char** argv) -{ - git_repository *repo = NULL; - git_index *index; - unsigned int i, ecount; - char *dir = "."; - size_t dirlen; - char out[41]; - out[40] = '\0'; - - if (argc > 1) - dir = argv[1]; - if (!dir || argc > 2) { - fprintf(stderr, "usage: showindex []\n"); - return 1; - } - - dirlen = strlen(dir); - if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) { - if (git_index_open(&index, dir) < 0) { - fprintf(stderr, "could not open index: %s\n", dir); - return 1; - } - } else { - if (git_repository_open_ext(&repo, dir, 0, NULL) < 0) { - fprintf(stderr, "could not open repository: %s\n", dir); - return 1; - } - if (git_repository_index(&index, repo) < 0) { - fprintf(stderr, "could not open repository index\n"); - return 1; - } - } - - git_index_read(index); - - ecount = git_index_entrycount(index); - if (!ecount) - printf("Empty index\n"); - - for (i = 0; i < ecount; ++i) { - const git_index_entry *e = git_index_get_byindex(index, i); - - git_oid_fmt(out, &e->oid); - - printf("File Path: %s\n", e->path); - printf(" Stage: %d\n", git_index_entry_stage(e)); - printf(" Blob SHA: %s\n", out); - printf("File Mode: %07o\n", e->mode); - printf("File Size: %d bytes\n", (int)e->file_size); - printf("Dev/Inode: %d/%d\n", (int)e->dev, (int)e->ino); - printf(" UID/GID: %d/%d\n", (int)e->uid, (int)e->gid); - printf(" ctime: %d\n", (int)e->ctime.seconds); - printf(" mtime: %d\n", (int)e->mtime.seconds); - printf("\n"); - } - - git_index_free(index); - git_repository_free(repo); - - return 0; -} - diff --git a/examples/stash.c b/examples/stash.c new file mode 100644 index 00000000000..197724364f6 --- /dev/null +++ b/examples/stash.c @@ -0,0 +1,157 @@ +/* + * libgit2 "stash" example - shows how to use the stash API + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include + +#include "common.h" + +enum subcmd { + SUBCMD_APPLY, + SUBCMD_LIST, + SUBCMD_POP, + SUBCMD_PUSH +}; + +struct opts { + enum subcmd cmd; + int argc; + char **argv; +}; + +static void usage(const char *fmt, ...) +{ + va_list ap; + + fputs("usage: git stash list\n", stderr); + fputs(" or: git stash ( pop | apply )\n", stderr); + fputs(" or: git stash [push]\n", stderr); + fputs("\n", stderr); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + exit(1); +} + +static void parse_subcommand(struct opts *opts, int argc, char *argv[]) +{ + char *arg = (argc < 2) ? "push" : argv[1]; + enum subcmd cmd; + + if (!strcmp(arg, "apply")) { + cmd = SUBCMD_APPLY; + } else if (!strcmp(arg, "list")) { + cmd = SUBCMD_LIST; + } else if (!strcmp(arg, "pop")) { + cmd = SUBCMD_POP; + } else if (!strcmp(arg, "push")) { + cmd = SUBCMD_PUSH; + } else { + usage("invalid command %s", arg); + return; + } + + opts->cmd = cmd; + opts->argc = (argc < 2) ? argc - 1 : argc - 2; + opts->argv = argv; +} + +static int cmd_apply(git_repository *repo, struct opts *opts) +{ + if (opts->argc) + usage("apply does not accept any parameters"); + + check_lg2(git_stash_apply(repo, 0, NULL), + "Unable to apply stash", NULL); + + return 0; +} + +static int list_stash_cb(size_t index, const char *message, + const git_oid *stash_id, void *payload) +{ + UNUSED(stash_id); + UNUSED(payload); + printf("stash@{%"PRIuZ"}: %s\n", index, message); + return 0; +} + +static int cmd_list(git_repository *repo, struct opts *opts) +{ + if (opts->argc) + usage("list does not accept any parameters"); + + check_lg2(git_stash_foreach(repo, list_stash_cb, NULL), + "Unable to list stashes", NULL); + + return 0; +} + +static int cmd_push(git_repository *repo, struct opts *opts) +{ + git_signature *signature; + git_commit *stash; + git_oid stashid; + + if (opts->argc) + usage("push does not accept any parameters"); + + check_lg2(git_signature_default_from_env(&signature, NULL, repo), + "Unable to get signature", NULL); + check_lg2(git_stash_save(&stashid, repo, signature, NULL, GIT_STASH_DEFAULT), + "Unable to save stash", NULL); + check_lg2(git_commit_lookup(&stash, repo, &stashid), + "Unable to lookup stash commit", NULL); + + printf("Saved working directory %s\n", git_commit_summary(stash)); + + git_signature_free(signature); + git_commit_free(stash); + + return 0; +} + +static int cmd_pop(git_repository *repo, struct opts *opts) +{ + if (opts->argc) + usage("pop does not accept any parameters"); + + check_lg2(git_stash_pop(repo, 0, NULL), + "Unable to pop stash", NULL); + + printf("Dropped refs/stash@{0}\n"); + + return 0; +} + +int lg2_stash(git_repository *repo, int argc, char *argv[]) +{ + struct opts opts = { 0 }; + + parse_subcommand(&opts, argc, argv); + + switch (opts.cmd) { + case SUBCMD_APPLY: + return cmd_apply(repo, &opts); + case SUBCMD_LIST: + return cmd_list(repo, &opts); + case SUBCMD_PUSH: + return cmd_push(repo, &opts); + case SUBCMD_POP: + return cmd_pop(repo, &opts); + } + + return -1; +} diff --git a/examples/status.c b/examples/status.c new file mode 100644 index 00000000000..e659efb059b --- /dev/null +++ b/examples/status.c @@ -0,0 +1,498 @@ +/* + * libgit2 "status" example - shows how to use the status APIs + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/** + * This example demonstrates the use of the libgit2 status APIs, + * particularly the `git_status_list` object, to roughly simulate the + * output of running `git status`. It serves as a simple example of + * using those APIs to get basic status information. + * + * This does not have: + * + * - Robust error handling + * - Colorized or paginated output formatting + * + * This does have: + * + * - Examples of translating command line arguments to the status + * options settings to mimic `git status` results. + * - A sample status formatter that matches the default "long" format + * from `git status` + * - A sample status formatter that matches the "short" format + */ + +enum { + FORMAT_DEFAULT = 0, + FORMAT_LONG = 1, + FORMAT_SHORT = 2, + FORMAT_PORCELAIN = 3 +}; + +#define MAX_PATHSPEC 8 + +struct status_opts { + git_status_options statusopt; + char *repodir; + char *pathspec[MAX_PATHSPEC]; + int npaths; + int format; + int zterm; + int showbranch; + int showsubmod; + int repeat; +}; + +static void parse_opts(struct status_opts *o, int argc, char *argv[]); +static void show_branch(git_repository *repo, int format); +static void print_long(git_status_list *status); +static void print_short(git_repository *repo, git_status_list *status); +static int print_submod(git_submodule *sm, const char *name, void *payload); + +int lg2_status(git_repository *repo, int argc, char *argv[]) +{ + git_status_list *status; + struct status_opts o = { GIT_STATUS_OPTIONS_INIT, "." }; + + o.statusopt.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; + o.statusopt.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | + GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX | + GIT_STATUS_OPT_SORT_CASE_SENSITIVELY; + + parse_opts(&o, argc, argv); + + if (git_repository_is_bare(repo)) + fatal("Cannot report status on bare repository", + git_repository_path(repo)); + +show_status: + if (o.repeat) + printf("\033[H\033[2J"); + + /** + * Run status on the repository + * + * We use `git_status_list_new()` to generate a list of status + * information which lets us iterate over it at our + * convenience and extract the data we want to show out of + * each entry. + * + * You can use `git_status_foreach()` or + * `git_status_foreach_ext()` if you'd prefer to execute a + * callback for each entry. The latter gives you more control + * about what results are presented. + */ + check_lg2(git_status_list_new(&status, repo, &o.statusopt), + "Could not get status", NULL); + + if (o.showbranch) + show_branch(repo, o.format); + + if (o.showsubmod) { + int submod_count = 0; + check_lg2(git_submodule_foreach(repo, print_submod, &submod_count), + "Cannot iterate submodules", o.repodir); + } + + if (o.format == FORMAT_LONG) + print_long(status); + else + print_short(repo, status); + + git_status_list_free(status); + + if (o.repeat) { + sleep(o.repeat); + goto show_status; + } + + return 0; +} + +/** + * If the user asked for the branch, let's show the short name of the + * branch. + */ +static void show_branch(git_repository *repo, int format) +{ + int error = 0; + const char *branch = NULL; + git_reference *head = NULL; + + error = git_repository_head(&head, repo); + + if (error == GIT_EUNBORNBRANCH || error == GIT_ENOTFOUND) + branch = NULL; + else if (!error) { + branch = git_reference_shorthand(head); + } else + check_lg2(error, "failed to get current branch", NULL); + + if (format == FORMAT_LONG) + printf("# On branch %s\n", + branch ? branch : "Not currently on any branch."); + else + printf("## %s\n", branch ? branch : "HEAD (no branch)"); + + git_reference_free(head); +} + +/** + * This function print out an output similar to git's status command + * in long form, including the command-line hints. + */ +static void print_long(git_status_list *status) +{ + size_t i, maxi = git_status_list_entrycount(status); + const git_status_entry *s; + int header = 0, changes_in_index = 0; + int changed_in_workdir = 0, rm_in_workdir = 0; + const char *old_path, *new_path; + + /** Print index changes. */ + + for (i = 0; i < maxi; ++i) { + char *istatus = NULL; + + s = git_status_byindex(status, i); + + if (s->status == GIT_STATUS_CURRENT) + continue; + + if (s->status & GIT_STATUS_WT_DELETED) + rm_in_workdir = 1; + + if (s->status & GIT_STATUS_INDEX_NEW) + istatus = "new file: "; + if (s->status & GIT_STATUS_INDEX_MODIFIED) + istatus = "modified: "; + if (s->status & GIT_STATUS_INDEX_DELETED) + istatus = "deleted: "; + if (s->status & GIT_STATUS_INDEX_RENAMED) + istatus = "renamed: "; + if (s->status & GIT_STATUS_INDEX_TYPECHANGE) + istatus = "typechange:"; + + if (istatus == NULL) + continue; + + if (!header) { + printf("# Changes to be committed:\n"); + printf("# (use \"git reset HEAD ...\" to unstage)\n"); + printf("#\n"); + header = 1; + } + + old_path = s->head_to_index->old_file.path; + new_path = s->head_to_index->new_file.path; + + if (old_path && new_path && strcmp(old_path, new_path)) + printf("#\t%s %s -> %s\n", istatus, old_path, new_path); + else + printf("#\t%s %s\n", istatus, old_path ? old_path : new_path); + } + + if (header) { + changes_in_index = 1; + printf("#\n"); + } + header = 0; + + /** Print workdir changes to tracked files. */ + + for (i = 0; i < maxi; ++i) { + char *wstatus = NULL; + + s = git_status_byindex(status, i); + + /** + * With `GIT_STATUS_OPT_INCLUDE_UNMODIFIED` (not used in this example) + * `index_to_workdir` may not be `NULL` even if there are + * no differences, in which case it will be a `GIT_DELTA_UNMODIFIED`. + */ + if (s->status == GIT_STATUS_CURRENT || s->index_to_workdir == NULL) + continue; + + /** Print out the output since we know the file has some changes */ + if (s->status & GIT_STATUS_WT_MODIFIED) + wstatus = "modified: "; + if (s->status & GIT_STATUS_WT_DELETED) + wstatus = "deleted: "; + if (s->status & GIT_STATUS_WT_RENAMED) + wstatus = "renamed: "; + if (s->status & GIT_STATUS_WT_TYPECHANGE) + wstatus = "typechange:"; + + if (wstatus == NULL) + continue; + + if (!header) { + printf("# Changes not staged for commit:\n"); + printf("# (use \"git add%s ...\" to update what will be committed)\n", rm_in_workdir ? "/rm" : ""); + printf("# (use \"git checkout -- ...\" to discard changes in working directory)\n"); + printf("#\n"); + header = 1; + } + + old_path = s->index_to_workdir->old_file.path; + new_path = s->index_to_workdir->new_file.path; + + if (old_path && new_path && strcmp(old_path, new_path)) + printf("#\t%s %s -> %s\n", wstatus, old_path, new_path); + else + printf("#\t%s %s\n", wstatus, old_path ? old_path : new_path); + } + + if (header) { + changed_in_workdir = 1; + printf("#\n"); + } + + /** Print untracked files. */ + + header = 0; + + for (i = 0; i < maxi; ++i) { + s = git_status_byindex(status, i); + + if (s->status == GIT_STATUS_WT_NEW) { + + if (!header) { + printf("# Untracked files:\n"); + printf("# (use \"git add ...\" to include in what will be committed)\n"); + printf("#\n"); + header = 1; + } + + printf("#\t%s\n", s->index_to_workdir->old_file.path); + } + } + + header = 0; + + /** Print ignored files. */ + + for (i = 0; i < maxi; ++i) { + s = git_status_byindex(status, i); + + if (s->status == GIT_STATUS_IGNORED) { + + if (!header) { + printf("# Ignored files:\n"); + printf("# (use \"git add -f ...\" to include in what will be committed)\n"); + printf("#\n"); + header = 1; + } + + printf("#\t%s\n", s->index_to_workdir->old_file.path); + } + } + + if (!changes_in_index && changed_in_workdir) + printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n"); +} + +/** + * This version of the output prefixes each path with two status + * columns and shows submodule status information. + */ +static void print_short(git_repository *repo, git_status_list *status) +{ + size_t i, maxi = git_status_list_entrycount(status); + const git_status_entry *s; + char istatus, wstatus; + const char *extra, *a, *b, *c; + + for (i = 0; i < maxi; ++i) { + s = git_status_byindex(status, i); + + if (s->status == GIT_STATUS_CURRENT) + continue; + + a = b = c = NULL; + istatus = wstatus = ' '; + extra = ""; + + if (s->status & GIT_STATUS_INDEX_NEW) + istatus = 'A'; + if (s->status & GIT_STATUS_INDEX_MODIFIED) + istatus = 'M'; + if (s->status & GIT_STATUS_INDEX_DELETED) + istatus = 'D'; + if (s->status & GIT_STATUS_INDEX_RENAMED) + istatus = 'R'; + if (s->status & GIT_STATUS_INDEX_TYPECHANGE) + istatus = 'T'; + + if (s->status & GIT_STATUS_WT_NEW) { + if (istatus == ' ') + istatus = '?'; + wstatus = '?'; + } + if (s->status & GIT_STATUS_WT_MODIFIED) + wstatus = 'M'; + if (s->status & GIT_STATUS_WT_DELETED) + wstatus = 'D'; + if (s->status & GIT_STATUS_WT_RENAMED) + wstatus = 'R'; + if (s->status & GIT_STATUS_WT_TYPECHANGE) + wstatus = 'T'; + + if (s->status & GIT_STATUS_IGNORED) { + istatus = '!'; + wstatus = '!'; + } + + if (istatus == '?' && wstatus == '?') + continue; + + /** + * A commit in a tree is how submodules are stored, so + * let's go take a look at its status. + */ + if (s->index_to_workdir && + s->index_to_workdir->new_file.mode == GIT_FILEMODE_COMMIT) + { + unsigned int smstatus = 0; + + if (!git_submodule_status(&smstatus, repo, s->index_to_workdir->new_file.path, + GIT_SUBMODULE_IGNORE_UNSPECIFIED)) { + if (smstatus & GIT_SUBMODULE_STATUS_WD_MODIFIED) + extra = " (new commits)"; + else if (smstatus & GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED) + extra = " (modified content)"; + else if (smstatus & GIT_SUBMODULE_STATUS_WD_WD_MODIFIED) + extra = " (modified content)"; + else if (smstatus & GIT_SUBMODULE_STATUS_WD_UNTRACKED) + extra = " (untracked content)"; + } + } + + /** + * Now that we have all the information, format the output. + */ + + if (s->head_to_index) { + a = s->head_to_index->old_file.path; + b = s->head_to_index->new_file.path; + } + if (s->index_to_workdir) { + if (!a) + a = s->index_to_workdir->old_file.path; + if (!b) + b = s->index_to_workdir->old_file.path; + c = s->index_to_workdir->new_file.path; + } + + if (istatus == 'R') { + if (wstatus == 'R') + printf("%c%c %s %s %s%s\n", istatus, wstatus, a, b, c, extra); + else + printf("%c%c %s %s%s\n", istatus, wstatus, a, b, extra); + } else { + if (wstatus == 'R') + printf("%c%c %s %s%s\n", istatus, wstatus, a, c, extra); + else + printf("%c%c %s%s\n", istatus, wstatus, a, extra); + } + } + + for (i = 0; i < maxi; ++i) { + s = git_status_byindex(status, i); + + if (s->status == GIT_STATUS_WT_NEW) + printf("?? %s\n", s->index_to_workdir->old_file.path); + } +} + +static int print_submod(git_submodule *sm, const char *name, void *payload) +{ + int *count = payload; + (void)name; + + if (*count == 0) + printf("# Submodules\n"); + (*count)++; + + printf("# - submodule '%s' at %s\n", + git_submodule_name(sm), git_submodule_path(sm)); + + return 0; +} + +/** + * Parse options that git's status command supports. + */ +static void parse_opts(struct status_opts *o, int argc, char *argv[]) +{ + struct args_info args = ARGS_INFO_INIT; + + for (args.pos = 1; args.pos < argc; ++args.pos) { + char *a = argv[args.pos]; + + if (a[0] != '-') { + if (o->npaths < MAX_PATHSPEC) + o->pathspec[o->npaths++] = a; + else + fatal("Example only supports a limited pathspec", NULL); + } + else if (!strcmp(a, "-s") || !strcmp(a, "--short")) + o->format = FORMAT_SHORT; + else if (!strcmp(a, "--long")) + o->format = FORMAT_LONG; + else if (!strcmp(a, "--porcelain")) + o->format = FORMAT_PORCELAIN; + else if (!strcmp(a, "-b") || !strcmp(a, "--branch")) + o->showbranch = 1; + else if (!strcmp(a, "-z")) { + o->zterm = 1; + if (o->format == FORMAT_DEFAULT) + o->format = FORMAT_PORCELAIN; + } + else if (!strcmp(a, "--ignored")) + o->statusopt.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED; + else if (!strcmp(a, "-uno") || + !strcmp(a, "--untracked-files=no")) + o->statusopt.flags &= ~GIT_STATUS_OPT_INCLUDE_UNTRACKED; + else if (!strcmp(a, "-unormal") || + !strcmp(a, "--untracked-files=normal")) + o->statusopt.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED; + else if (!strcmp(a, "-uall") || + !strcmp(a, "--untracked-files=all")) + o->statusopt.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED | + GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS; + else if (!strcmp(a, "--ignore-submodules=all")) + o->statusopt.flags |= GIT_STATUS_OPT_EXCLUDE_SUBMODULES; + else if (!strncmp(a, "--git-dir=", strlen("--git-dir="))) + o->repodir = a + strlen("--git-dir="); + else if (!strcmp(a, "--repeat")) + o->repeat = 10; + else if (match_int_arg(&o->repeat, &args, "--repeat", 0)) + /* okay */; + else if (!strcmp(a, "--list-submodules")) + o->showsubmod = 1; + else + check_lg2(-1, "Unsupported option", a); + } + + if (o->format == FORMAT_DEFAULT) + o->format = FORMAT_LONG; + if (o->format == FORMAT_LONG) + o->showbranch = 1; + if (o->npaths > 0) { + o->statusopt.pathspec.strings = o->pathspec; + o->statusopt.pathspec.count = o->npaths; + } +} diff --git a/examples/tag.c b/examples/tag.c new file mode 100644 index 00000000000..ebe1a9d7bf0 --- /dev/null +++ b/examples/tag.c @@ -0,0 +1,310 @@ +/* + * libgit2 "tag" example - shows how to list, create and delete tags + * + * Written by the libgit2 contributors + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with this software. If not, see + * . + */ + +#include "common.h" + +/** + * The following example partially reimplements the `git tag` command + * and some of its options. + * + * These commands should work: + + * - Tag name listing (`tag`) + * - Filtered tag listing with messages (`tag -n3 -l "v0.1*"`) + * - Lightweight tag creation (`tag test v0.18.0`) + * - Tag creation (`tag -a -m "Test message" test v0.18.0`) + * - Tag deletion (`tag -d test`) + * + * The command line parsing logic is simplified and doesn't handle + * all of the use cases. + */ + +/** tag_options represents the parsed command line options */ +struct tag_options { + const char *message; + const char *pattern; + const char *tag_name; + const char *target; + int num_lines; + int force; +}; + +/** tag_state represents the current program state for dragging around */ +typedef struct { + git_repository *repo; + struct tag_options *opts; +} tag_state; + +/** An action to execute based on the command line arguments */ +typedef void (*tag_action)(tag_state *state); +typedef struct args_info args_info; + +static void check(int result, const char *message) +{ + if (result) fatal(message, NULL); +} + +/** Tag listing: Print individual message lines */ +static void print_list_lines(const char *message, const tag_state *state) +{ + const char *msg = message; + int num = state->opts->num_lines - 1; + + if (!msg) return; + + /** first line - headline */ + while(*msg && *msg != '\n') printf("%c", *msg++); + + /** skip over new lines */ + while(*msg && *msg == '\n') msg++; + + printf("\n"); + + /** print just headline? */ + if (num == 0) return; + if (*msg && msg[1]) printf("\n"); + + /** print individual commit/tag lines */ + while (*msg && num-- >= 2) { + printf(" "); + + while (*msg && *msg != '\n') printf("%c", *msg++); + + /** handle consecutive new lines */ + if (*msg && *msg == '\n' && msg[1] == '\n') { + num--; + printf("\n"); + } + while(*msg && *msg == '\n') msg++; + + printf("\n"); + } +} + +/** Tag listing: Print an actual tag object */ +static void print_tag(git_tag *tag, const tag_state *state) +{ + printf("%-16s", git_tag_name(tag)); + + if (state->opts->num_lines) { + const char *msg = git_tag_message(tag); + print_list_lines(msg, state); + } else { + printf("\n"); + } +} + +/** Tag listing: Print a commit (target of a lightweight tag) */ +static void print_commit(git_commit *commit, const char *name, + const tag_state *state) +{ + printf("%-16s", name); + + if (state->opts->num_lines) { + const char *msg = git_commit_message(commit); + print_list_lines(msg, state); + } else { + printf("\n"); + } +} + +/** Tag listing: Fallback, should not happen */ +static void print_name(const char *name) +{ + printf("%s\n", name); +} + +/** Tag listing: Lookup tags based on ref name and dispatch to print */ +static int each_tag(const char *name, tag_state *state) +{ + git_repository *repo = state->repo; + git_object *obj; + + check_lg2(git_revparse_single(&obj, repo, name), + "Failed to lookup rev", name); + + switch (git_object_type(obj)) { + case GIT_OBJECT_TAG: + print_tag((git_tag *) obj, state); + break; + case GIT_OBJECT_COMMIT: + print_commit((git_commit *) obj, name, state); + break; + default: + print_name(name); + } + + git_object_free(obj); + return 0; +} + +static void action_list_tags(tag_state *state) +{ + const char *pattern = state->opts->pattern; + git_strarray tag_names = {0}; + size_t i; + + check_lg2(git_tag_list_match(&tag_names, pattern ? pattern : "*", state->repo), + "Unable to get list of tags", NULL); + + for(i = 0; i < tag_names.count; i++) { + each_tag(tag_names.strings[i], state); + } + + git_strarray_dispose(&tag_names); +} + +static void action_delete_tag(tag_state *state) +{ + struct tag_options *opts = state->opts; + git_object *obj; + git_buf abbrev_oid = {0}; + + check(!opts->tag_name, "Name required"); + + check_lg2(git_revparse_single(&obj, state->repo, opts->tag_name), + "Failed to lookup rev", opts->tag_name); + + check_lg2(git_object_short_id(&abbrev_oid, obj), + "Unable to get abbreviated OID", opts->tag_name); + + check_lg2(git_tag_delete(state->repo, opts->tag_name), + "Unable to delete tag", opts->tag_name); + + printf("Deleted tag '%s' (was %s)\n", opts->tag_name, abbrev_oid.ptr); + + git_buf_dispose(&abbrev_oid); + git_object_free(obj); +} + +static void action_create_lightweight_tag(tag_state *state) +{ + git_repository *repo = state->repo; + struct tag_options *opts = state->opts; + git_oid oid; + git_object *target; + + check(!opts->tag_name, "Name required"); + + if (!opts->target) opts->target = "HEAD"; + + check(!opts->target, "Target required"); + + check_lg2(git_revparse_single(&target, repo, opts->target), + "Unable to resolve spec", opts->target); + + check_lg2(git_tag_create_lightweight(&oid, repo, opts->tag_name, + target, opts->force), "Unable to create tag", NULL); + + git_object_free(target); +} + +static void action_create_tag(tag_state *state) +{ + git_repository *repo = state->repo; + struct tag_options *opts = state->opts; + git_signature *tagger; + git_oid oid; + git_object *target; + + check(!opts->tag_name, "Name required"); + check(!opts->message, "Message required"); + + if (!opts->target) opts->target = "HEAD"; + + check_lg2(git_revparse_single(&target, repo, opts->target), + "Unable to resolve spec", opts->target); + + check_lg2(git_signature_default_from_env(&tagger, NULL, repo), + "Unable to create signature", NULL); + + check_lg2(git_tag_create(&oid, repo, opts->tag_name, + target, tagger, opts->message, opts->force), "Unable to create tag", NULL); + + git_object_free(target); + git_signature_free(tagger); +} + +static void print_usage(void) +{ + fprintf(stderr, "usage: see `git help tag`\n"); + exit(1); +} + +/** Parse command line arguments and choose action to run when done */ +static void parse_options(tag_action *action, struct tag_options *opts, int argc, char **argv) +{ + args_info args = ARGS_INFO_INIT; + *action = &action_list_tags; + + for (args.pos = 1; args.pos < argc; ++args.pos) { + const char *curr = argv[args.pos]; + + if (curr[0] != '-') { + if (!opts->tag_name) + opts->tag_name = curr; + else if (!opts->target) + opts->target = curr; + else + print_usage(); + + if (*action != &action_create_tag) + *action = &action_create_lightweight_tag; + } else if (!strcmp(curr, "-n")) { + opts->num_lines = 1; + *action = &action_list_tags; + } else if (!strcmp(curr, "-a")) { + *action = &action_create_tag; + } else if (!strcmp(curr, "-f")) { + opts->force = 1; + } else if (match_int_arg(&opts->num_lines, &args, "-n", 0)) { + *action = &action_list_tags; + } else if (match_str_arg(&opts->pattern, &args, "-l")) { + *action = &action_list_tags; + } else if (match_str_arg(&opts->tag_name, &args, "-d")) { + *action = &action_delete_tag; + } else if (match_str_arg(&opts->message, &args, "-m")) { + *action = &action_create_tag; + } + } +} + +/** Initialize tag_options struct */ +static void tag_options_init(struct tag_options *opts) +{ + memset(opts, 0, sizeof(*opts)); + + opts->message = NULL; + opts->pattern = NULL; + opts->tag_name = NULL; + opts->target = NULL; + opts->num_lines = 0; + opts->force = 0; +} + +int lg2_tag(git_repository *repo, int argc, char **argv) +{ + struct tag_options opts; + tag_action action; + tag_state state; + + tag_options_init(&opts); + parse_options(&action, &opts, argc, argv); + + state.repo = repo; + state.opts = &opts; + action(&state); + + return 0; +} diff --git a/fuzzers/CMakeLists.txt b/fuzzers/CMakeLists.txt new file mode 100644 index 00000000000..38d705f74c8 --- /dev/null +++ b/fuzzers/CMakeLists.txt @@ -0,0 +1,31 @@ +# fuzzers: libFuzzer and standalone fuzzing utilities + +if(BUILD_FUZZERS AND NOT USE_STANDALONE_FUZZERS) + set(CMAKE_REQUIRED_FLAGS "-fsanitize=fuzzer-no-link") + add_c_flag(-fsanitize=fuzzer-no-link) + unset(CMAKE_REQUIRED_FLAGS) +endif() + +file(GLOB SRC_FUZZERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *_fuzzer.c) +foreach(fuzz_target_src ${SRC_FUZZERS}) + string(REPLACE ".c" "" fuzz_target_name ${fuzz_target_src}) + string(REPLACE "_fuzzer" "" fuzz_name ${fuzz_target_name}) + + set(${fuzz_target_name}_SOURCES + ${fuzz_target_src} "fuzzer_utils.c" ${LIBGIT2_OBJECTS}) + + if(USE_STANDALONE_FUZZERS) + list(APPEND ${fuzz_target_name}_SOURCES "standalone_driver.c") + endif() + + add_executable(${fuzz_target_name} ${${fuzz_target_name}_SOURCES}) + target_include_directories(${fuzz_target_name} PRIVATE ${LIBGIT2_INCLUDES} ${LIBGIT2_DEPENDENCY_INCLUDES}) + target_include_directories(${fuzz_target_name} SYSTEM PRIVATE ${LIBGIT2_SYSTEM_INCLUDES}) + + target_link_libraries(${fuzz_target_name} ${LIBGIT2_SYSTEM_LIBS}) + if(NOT USE_STANDALONE_FUZZERS) + target_link_options(${fuzz_target_name} PRIVATE "-fsanitize=fuzzer") + endif() + + add_test(${fuzz_target_name} "${CMAKE_CURRENT_BINARY_DIR}/${fuzz_target_name}" "${CMAKE_CURRENT_SOURCE_DIR}/corpora/${fuzz_name}") +endforeach() diff --git a/fuzzers/commit_graph_fuzzer.c b/fuzzers/commit_graph_fuzzer.c new file mode 100644 index 00000000000..9c1443e52b4 --- /dev/null +++ b/fuzzers/commit_graph_fuzzer.c @@ -0,0 +1,85 @@ +/* + * libgit2 commit-graph fuzzer target. + * + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include + +#include "git2.h" + +#include "common.h" +#include "str.h" +#include "futils.h" +#include "hash.h" +#include "commit_graph.h" + +#include "standalone_driver.h" + +int LLVMFuzzerInitialize(int *argc, char ***argv) +{ + GIT_UNUSED(argc); + GIT_UNUSED(argv); + + if (git_libgit2_init() < 0) { + fprintf(stderr, "Failed to initialize libgit2\n"); + abort(); + } + return 0; +} + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + git_commit_graph_file file = {{0}}; + git_commit_graph_entry e; + git_str commit_graph_buf = GIT_STR_INIT; + unsigned char hash[GIT_HASH_SHA1_SIZE]; + git_oid oid = GIT_OID_NONE; + bool append_hash = false; + + if (size < 4) + return 0; + + /* + * If the first byte in the stream has the high bit set, append the + * SHA1 hash so that the file is somewhat valid. + */ + append_hash = *data & 0x80; + /* Keep a 4-byte alignment to avoid unaligned accesses. */ + data += 4; + size -= 4; + + if (append_hash) { + if (git_str_init(&commit_graph_buf, size + GIT_HASH_SHA1_SIZE) < 0) + goto cleanup; + if (git_hash_buf(hash, data, size, GIT_HASH_ALGORITHM_SHA1) < 0) { + fprintf(stderr, "Failed to compute the SHA1 hash\n"); + abort(); + } + memcpy(commit_graph_buf.ptr, data, size); + memcpy(commit_graph_buf.ptr + size, hash, GIT_HASH_SHA1_SIZE); + + memcpy(oid.id, hash, GIT_OID_SHA1_SIZE); + } else { + git_str_attach_notowned(&commit_graph_buf, (char *)data, size); + } + + if (git_commit_graph_file_parse( + &file, + (const unsigned char *)git_str_cstr(&commit_graph_buf), + git_str_len(&commit_graph_buf)) + < 0) + goto cleanup; + + /* Search for any oid, just to exercise that codepath. */ + if (git_commit_graph_entry_find(&e, &file, &oid, GIT_OID_SHA1_HEXSIZE) < 0) + goto cleanup; + +cleanup: + git_commit_graph_file_close(&file); + git_str_dispose(&commit_graph_buf); + return 0; +} diff --git a/fuzzers/config_file_fuzzer.c b/fuzzers/config_file_fuzzer.c new file mode 100644 index 00000000000..763036960b4 --- /dev/null +++ b/fuzzers/config_file_fuzzer.c @@ -0,0 +1,61 @@ +/* + * libgit2 config file parser fuzz target. + * + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2.h" +#include "config_backend.h" + +#include "standalone_driver.h" + +#define UNUSED(x) (void)(x) + +static int foreach_cb(const git_config_entry *entry, void *payload) +{ + UNUSED(entry); + UNUSED(payload); + + return 0; +} + +int LLVMFuzzerInitialize(int *argc, char ***argv) +{ + UNUSED(argc); + UNUSED(argv); + + if (git_libgit2_init() < 0) + abort(); + + return 0; +} + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + git_config *cfg = NULL; + git_config_backend *backend = NULL; + int err = 0; + + if ((err = git_config_new(&cfg)) != 0) { + goto out; + } + + if ((err = git_config_backend_from_string(&backend, (const char*)data, size, NULL)) != 0) { + goto out; + } + if ((err = git_config_add_backend(cfg, backend, 0, NULL, 0)) != 0) { + goto out; + } + /* Now owned by the config */ + backend = NULL; + + git_config_foreach(cfg, foreach_cb, NULL); + out: + git_config_backend_free(backend); + git_config_free(cfg); + + return 0; +} diff --git a/fuzzers/corpora/commit_graph/005682ce1cb5b20c20fccf4be5dbd47ca399e53e b/fuzzers/corpora/commit_graph/005682ce1cb5b20c20fccf4be5dbd47ca399e53e new file mode 100644 index 00000000000..15d0d281366 Binary files /dev/null and b/fuzzers/corpora/commit_graph/005682ce1cb5b20c20fccf4be5dbd47ca399e53e differ diff --git a/fuzzers/corpora/commit_graph/00574fc29fd1323e93d18d625cde80d3ea20e8cc b/fuzzers/corpora/commit_graph/00574fc29fd1323e93d18d625cde80d3ea20e8cc new file mode 100644 index 00000000000..4eabd00da7a Binary files /dev/null and b/fuzzers/corpora/commit_graph/00574fc29fd1323e93d18d625cde80d3ea20e8cc differ diff --git a/fuzzers/corpora/commit_graph/00916ec21ddbd3c622bde6e4dc824250176b9e88 b/fuzzers/corpora/commit_graph/00916ec21ddbd3c622bde6e4dc824250176b9e88 new file mode 100644 index 00000000000..d069fb5cb33 Binary files /dev/null and b/fuzzers/corpora/commit_graph/00916ec21ddbd3c622bde6e4dc824250176b9e88 differ diff --git a/fuzzers/corpora/commit_graph/00b6dde4b8d5e68a5ec40d88c39134cf2f1f8bc3 b/fuzzers/corpora/commit_graph/00b6dde4b8d5e68a5ec40d88c39134cf2f1f8bc3 new file mode 100644 index 00000000000..98f2d0e9d45 Binary files /dev/null and b/fuzzers/corpora/commit_graph/00b6dde4b8d5e68a5ec40d88c39134cf2f1f8bc3 differ diff --git a/fuzzers/corpora/commit_graph/020f0e77e42d8b3810019050f4c5ceadd205b37c b/fuzzers/corpora/commit_graph/020f0e77e42d8b3810019050f4c5ceadd205b37c new file mode 100644 index 00000000000..d09327d5488 Binary files /dev/null and b/fuzzers/corpora/commit_graph/020f0e77e42d8b3810019050f4c5ceadd205b37c differ diff --git a/fuzzers/corpora/commit_graph/02739c05abc1715fac1ce995b532e482abc8d4dc b/fuzzers/corpora/commit_graph/02739c05abc1715fac1ce995b532e482abc8d4dc new file mode 100644 index 00000000000..b9de08e1d85 Binary files /dev/null and b/fuzzers/corpora/commit_graph/02739c05abc1715fac1ce995b532e482abc8d4dc differ diff --git a/fuzzers/corpora/commit_graph/02a276faa5dc8c7df5b82a57ab6cd195a13e4ae0 b/fuzzers/corpora/commit_graph/02a276faa5dc8c7df5b82a57ab6cd195a13e4ae0 new file mode 100644 index 00000000000..891d3efbe08 Binary files /dev/null and b/fuzzers/corpora/commit_graph/02a276faa5dc8c7df5b82a57ab6cd195a13e4ae0 differ diff --git a/fuzzers/corpora/commit_graph/02de15987d68a97db3d9fd964cfd785bcbd54d3a b/fuzzers/corpora/commit_graph/02de15987d68a97db3d9fd964cfd785bcbd54d3a new file mode 100644 index 00000000000..b8bd4ce97ea Binary files /dev/null and b/fuzzers/corpora/commit_graph/02de15987d68a97db3d9fd964cfd785bcbd54d3a differ diff --git a/fuzzers/corpora/commit_graph/02e106f97a91b1d3aef4dd2d31368ae5077bd42b b/fuzzers/corpora/commit_graph/02e106f97a91b1d3aef4dd2d31368ae5077bd42b new file mode 100644 index 00000000000..f1277396cf5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/02e106f97a91b1d3aef4dd2d31368ae5077bd42b differ diff --git a/fuzzers/corpora/commit_graph/038555bcb4cc2daf764840f79ebce4023bdb7670 b/fuzzers/corpora/commit_graph/038555bcb4cc2daf764840f79ebce4023bdb7670 new file mode 100644 index 00000000000..239f4e6e393 Binary files /dev/null and b/fuzzers/corpora/commit_graph/038555bcb4cc2daf764840f79ebce4023bdb7670 differ diff --git a/fuzzers/corpora/commit_graph/04c159a04b0732e04ac4c59ed3356860af8dffce b/fuzzers/corpora/commit_graph/04c159a04b0732e04ac4c59ed3356860af8dffce new file mode 100644 index 00000000000..856af0f76ae Binary files /dev/null and b/fuzzers/corpora/commit_graph/04c159a04b0732e04ac4c59ed3356860af8dffce differ diff --git a/fuzzers/corpora/commit_graph/0560ec993882ffbd8d46dcab0ed430089c4f2aa1 b/fuzzers/corpora/commit_graph/0560ec993882ffbd8d46dcab0ed430089c4f2aa1 new file mode 100644 index 00000000000..a236d1a2e12 Binary files /dev/null and b/fuzzers/corpora/commit_graph/0560ec993882ffbd8d46dcab0ed430089c4f2aa1 differ diff --git a/fuzzers/corpora/commit_graph/059b3aab3fde6b4c9404aff83fed638596f594bb b/fuzzers/corpora/commit_graph/059b3aab3fde6b4c9404aff83fed638596f594bb new file mode 100644 index 00000000000..4900105ff32 Binary files /dev/null and b/fuzzers/corpora/commit_graph/059b3aab3fde6b4c9404aff83fed638596f594bb differ diff --git a/fuzzers/corpora/commit_graph/06168e726aa0260f520165be4ea0c88244831049 b/fuzzers/corpora/commit_graph/06168e726aa0260f520165be4ea0c88244831049 new file mode 100644 index 00000000000..4c9e9df0324 Binary files /dev/null and b/fuzzers/corpora/commit_graph/06168e726aa0260f520165be4ea0c88244831049 differ diff --git a/fuzzers/corpora/commit_graph/066d1ec700a526b97009cedd0305b6a47242faba b/fuzzers/corpora/commit_graph/066d1ec700a526b97009cedd0305b6a47242faba new file mode 100644 index 00000000000..f5b17612a4a Binary files /dev/null and b/fuzzers/corpora/commit_graph/066d1ec700a526b97009cedd0305b6a47242faba differ diff --git a/fuzzers/corpora/commit_graph/086a5f8cbfa9f058b5c938a6eb724c9e4c5f84f3 b/fuzzers/corpora/commit_graph/086a5f8cbfa9f058b5c938a6eb724c9e4c5f84f3 new file mode 100644 index 00000000000..aa9cdca9769 Binary files /dev/null and b/fuzzers/corpora/commit_graph/086a5f8cbfa9f058b5c938a6eb724c9e4c5f84f3 differ diff --git a/fuzzers/corpora/commit_graph/089313c698f3e351433e9a45af2ace1d85b9673e b/fuzzers/corpora/commit_graph/089313c698f3e351433e9a45af2ace1d85b9673e new file mode 100644 index 00000000000..14fd3bbc336 Binary files /dev/null and b/fuzzers/corpora/commit_graph/089313c698f3e351433e9a45af2ace1d85b9673e differ diff --git a/fuzzers/corpora/commit_graph/092eb973a771fa14cf0b567d65bd2c99130f543e b/fuzzers/corpora/commit_graph/092eb973a771fa14cf0b567d65bd2c99130f543e new file mode 100644 index 00000000000..3092a2bc457 Binary files /dev/null and b/fuzzers/corpora/commit_graph/092eb973a771fa14cf0b567d65bd2c99130f543e differ diff --git a/fuzzers/corpora/commit_graph/094b8cd1aa3e40b1f9ff83680892d52e246df0f8 b/fuzzers/corpora/commit_graph/094b8cd1aa3e40b1f9ff83680892d52e246df0f8 new file mode 100644 index 00000000000..ed62ec9e5a9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/094b8cd1aa3e40b1f9ff83680892d52e246df0f8 differ diff --git a/fuzzers/corpora/commit_graph/0ce990c9c2ec121b8c78ba2bdf84679e04c0bdae b/fuzzers/corpora/commit_graph/0ce990c9c2ec121b8c78ba2bdf84679e04c0bdae new file mode 100644 index 00000000000..d708b683741 Binary files /dev/null and b/fuzzers/corpora/commit_graph/0ce990c9c2ec121b8c78ba2bdf84679e04c0bdae differ diff --git a/fuzzers/corpora/commit_graph/0dd0770c34fcf6b1f13219450190616d344db021 b/fuzzers/corpora/commit_graph/0dd0770c34fcf6b1f13219450190616d344db021 new file mode 100644 index 00000000000..aea94b2c83e Binary files /dev/null and b/fuzzers/corpora/commit_graph/0dd0770c34fcf6b1f13219450190616d344db021 differ diff --git a/fuzzers/corpora/commit_graph/0e2b2e6a32733b8a625bc7e812e2ea508d69a5e4 b/fuzzers/corpora/commit_graph/0e2b2e6a32733b8a625bc7e812e2ea508d69a5e4 new file mode 100644 index 00000000000..58e88014e55 Binary files /dev/null and b/fuzzers/corpora/commit_graph/0e2b2e6a32733b8a625bc7e812e2ea508d69a5e4 differ diff --git a/fuzzers/corpora/commit_graph/0e8d0bd07c543d708aecaca377106492b7a74fa3 b/fuzzers/corpora/commit_graph/0e8d0bd07c543d708aecaca377106492b7a74fa3 new file mode 100644 index 00000000000..98a92307d18 Binary files /dev/null and b/fuzzers/corpora/commit_graph/0e8d0bd07c543d708aecaca377106492b7a74fa3 differ diff --git a/fuzzers/corpora/commit_graph/0f0d16e1b8c8671dbe1074115c1d86aa9b359e7e b/fuzzers/corpora/commit_graph/0f0d16e1b8c8671dbe1074115c1d86aa9b359e7e new file mode 100644 index 00000000000..bc322de2b30 Binary files /dev/null and b/fuzzers/corpora/commit_graph/0f0d16e1b8c8671dbe1074115c1d86aa9b359e7e differ diff --git a/fuzzers/corpora/commit_graph/102ef78036de5a30927e7f751377b05441c41a08 b/fuzzers/corpora/commit_graph/102ef78036de5a30927e7f751377b05441c41a08 new file mode 100644 index 00000000000..3b14c352f50 Binary files /dev/null and b/fuzzers/corpora/commit_graph/102ef78036de5a30927e7f751377b05441c41a08 differ diff --git a/fuzzers/corpora/commit_graph/10494e7cc9cb8dff289c431d7560bcee0d1b14ed b/fuzzers/corpora/commit_graph/10494e7cc9cb8dff289c431d7560bcee0d1b14ed new file mode 100644 index 00000000000..293c49d36fa Binary files /dev/null and b/fuzzers/corpora/commit_graph/10494e7cc9cb8dff289c431d7560bcee0d1b14ed differ diff --git a/fuzzers/corpora/commit_graph/107b11d86381345f50aa19b8485477a870ff399f b/fuzzers/corpora/commit_graph/107b11d86381345f50aa19b8485477a870ff399f new file mode 100644 index 00000000000..53bb34eee1c Binary files /dev/null and b/fuzzers/corpora/commit_graph/107b11d86381345f50aa19b8485477a870ff399f differ diff --git a/fuzzers/corpora/commit_graph/10bb37e18fb3c0897dabacf9c464b4d324007dc3 b/fuzzers/corpora/commit_graph/10bb37e18fb3c0897dabacf9c464b4d324007dc3 new file mode 100644 index 00000000000..e75a9c511aa Binary files /dev/null and b/fuzzers/corpora/commit_graph/10bb37e18fb3c0897dabacf9c464b4d324007dc3 differ diff --git a/fuzzers/corpora/commit_graph/10ee715f64b08549c3e8261204276694728eb841 b/fuzzers/corpora/commit_graph/10ee715f64b08549c3e8261204276694728eb841 new file mode 100644 index 00000000000..104eda6c3cd Binary files /dev/null and b/fuzzers/corpora/commit_graph/10ee715f64b08549c3e8261204276694728eb841 differ diff --git a/fuzzers/corpora/commit_graph/123e4eeb7a731f48d06e336b4d29af717f8b6550 b/fuzzers/corpora/commit_graph/123e4eeb7a731f48d06e336b4d29af717f8b6550 new file mode 100644 index 00000000000..22d14827866 Binary files /dev/null and b/fuzzers/corpora/commit_graph/123e4eeb7a731f48d06e336b4d29af717f8b6550 differ diff --git a/fuzzers/corpora/commit_graph/125a228afb923970e0a6d4412f7257ba998594a1 b/fuzzers/corpora/commit_graph/125a228afb923970e0a6d4412f7257ba998594a1 new file mode 100644 index 00000000000..3de2c87e331 Binary files /dev/null and b/fuzzers/corpora/commit_graph/125a228afb923970e0a6d4412f7257ba998594a1 differ diff --git a/fuzzers/corpora/commit_graph/130d96c16fba06dcbe7e2a661ab959a3274a4bd9 b/fuzzers/corpora/commit_graph/130d96c16fba06dcbe7e2a661ab959a3274a4bd9 new file mode 100644 index 00000000000..66a40979483 Binary files /dev/null and b/fuzzers/corpora/commit_graph/130d96c16fba06dcbe7e2a661ab959a3274a4bd9 differ diff --git a/fuzzers/corpora/commit_graph/131c5a2fec55cb0d63f7dc055d6fad5f3dc3c974 b/fuzzers/corpora/commit_graph/131c5a2fec55cb0d63f7dc055d6fad5f3dc3c974 new file mode 100644 index 00000000000..b54bfadadbc Binary files /dev/null and b/fuzzers/corpora/commit_graph/131c5a2fec55cb0d63f7dc055d6fad5f3dc3c974 differ diff --git a/fuzzers/corpora/commit_graph/13e562d61acb3aa36260a819a00b07ff16450335 b/fuzzers/corpora/commit_graph/13e562d61acb3aa36260a819a00b07ff16450335 new file mode 100644 index 00000000000..6682c84e62c Binary files /dev/null and b/fuzzers/corpora/commit_graph/13e562d61acb3aa36260a819a00b07ff16450335 differ diff --git a/fuzzers/corpora/commit_graph/1414e6e8ab6bad1b5c51fed807c514a9d6575e66 b/fuzzers/corpora/commit_graph/1414e6e8ab6bad1b5c51fed807c514a9d6575e66 new file mode 100644 index 00000000000..c7f2386d0a4 Binary files /dev/null and b/fuzzers/corpora/commit_graph/1414e6e8ab6bad1b5c51fed807c514a9d6575e66 differ diff --git a/fuzzers/corpora/commit_graph/1432d191846ae2d0e381813efcfacff2f1dba0e4 b/fuzzers/corpora/commit_graph/1432d191846ae2d0e381813efcfacff2f1dba0e4 new file mode 100644 index 00000000000..cf5b990a65c Binary files /dev/null and b/fuzzers/corpora/commit_graph/1432d191846ae2d0e381813efcfacff2f1dba0e4 differ diff --git a/fuzzers/corpora/commit_graph/14a84cdc6f8d432be4cd3d3eafce92ae385e472f b/fuzzers/corpora/commit_graph/14a84cdc6f8d432be4cd3d3eafce92ae385e472f new file mode 100644 index 00000000000..8f866511349 Binary files /dev/null and b/fuzzers/corpora/commit_graph/14a84cdc6f8d432be4cd3d3eafce92ae385e472f differ diff --git a/fuzzers/corpora/commit_graph/14e3e735dba88791f2cadd6e0dc5d662a104a6d7 b/fuzzers/corpora/commit_graph/14e3e735dba88791f2cadd6e0dc5d662a104a6d7 new file mode 100644 index 00000000000..32fb993782f Binary files /dev/null and b/fuzzers/corpora/commit_graph/14e3e735dba88791f2cadd6e0dc5d662a104a6d7 differ diff --git a/fuzzers/corpora/commit_graph/1574abb020203103ea629d677edd21c967fc0f4c b/fuzzers/corpora/commit_graph/1574abb020203103ea629d677edd21c967fc0f4c new file mode 100644 index 00000000000..b3da74c1a7f Binary files /dev/null and b/fuzzers/corpora/commit_graph/1574abb020203103ea629d677edd21c967fc0f4c differ diff --git a/fuzzers/corpora/commit_graph/169cc492ba94948a6206765436881a1a0c601780 b/fuzzers/corpora/commit_graph/169cc492ba94948a6206765436881a1a0c601780 new file mode 100644 index 00000000000..2ce25f66b34 Binary files /dev/null and b/fuzzers/corpora/commit_graph/169cc492ba94948a6206765436881a1a0c601780 differ diff --git a/fuzzers/corpora/commit_graph/16a2130c1d75129f3bae3bf8f2c2de41fb3533c0 b/fuzzers/corpora/commit_graph/16a2130c1d75129f3bae3bf8f2c2de41fb3533c0 new file mode 100644 index 00000000000..0b6638518be Binary files /dev/null and b/fuzzers/corpora/commit_graph/16a2130c1d75129f3bae3bf8f2c2de41fb3533c0 differ diff --git a/fuzzers/corpora/commit_graph/16ba602eadfc9a3f74c0845394eda0de42b61571 b/fuzzers/corpora/commit_graph/16ba602eadfc9a3f74c0845394eda0de42b61571 new file mode 100644 index 00000000000..c44bd060e44 Binary files /dev/null and b/fuzzers/corpora/commit_graph/16ba602eadfc9a3f74c0845394eda0de42b61571 differ diff --git a/fuzzers/corpora/commit_graph/17555fb2dfc444d171ba686667d72e388bd6c041 b/fuzzers/corpora/commit_graph/17555fb2dfc444d171ba686667d72e388bd6c041 new file mode 100644 index 00000000000..3d000269393 Binary files /dev/null and b/fuzzers/corpora/commit_graph/17555fb2dfc444d171ba686667d72e388bd6c041 differ diff --git a/fuzzers/corpora/commit_graph/1a10450d99c1e53d9b7f97b8014cb7fc01906ef2 b/fuzzers/corpora/commit_graph/1a10450d99c1e53d9b7f97b8014cb7fc01906ef2 new file mode 100644 index 00000000000..f1fec6037d4 Binary files /dev/null and b/fuzzers/corpora/commit_graph/1a10450d99c1e53d9b7f97b8014cb7fc01906ef2 differ diff --git a/fuzzers/corpora/commit_graph/1af670b5515231fc04b2be9038ee30a7e066b09b b/fuzzers/corpora/commit_graph/1af670b5515231fc04b2be9038ee30a7e066b09b new file mode 100644 index 00000000000..3bf73452f60 Binary files /dev/null and b/fuzzers/corpora/commit_graph/1af670b5515231fc04b2be9038ee30a7e066b09b differ diff --git a/fuzzers/corpora/commit_graph/1b72cfa68259e3f3b3802906902a0a29368f86b5 b/fuzzers/corpora/commit_graph/1b72cfa68259e3f3b3802906902a0a29368f86b5 new file mode 100644 index 00000000000..e6509d1764e Binary files /dev/null and b/fuzzers/corpora/commit_graph/1b72cfa68259e3f3b3802906902a0a29368f86b5 differ diff --git a/fuzzers/corpora/commit_graph/1c62ac5d632aa9e449a4335b675941107d8825ae b/fuzzers/corpora/commit_graph/1c62ac5d632aa9e449a4335b675941107d8825ae new file mode 100644 index 00000000000..10c9e7490b7 Binary files /dev/null and b/fuzzers/corpora/commit_graph/1c62ac5d632aa9e449a4335b675941107d8825ae differ diff --git a/fuzzers/corpora/commit_graph/1d95b5db2f802011b33d10212a66fbe40827dfd4 b/fuzzers/corpora/commit_graph/1d95b5db2f802011b33d10212a66fbe40827dfd4 new file mode 100644 index 00000000000..2353450fe79 Binary files /dev/null and b/fuzzers/corpora/commit_graph/1d95b5db2f802011b33d10212a66fbe40827dfd4 differ diff --git a/fuzzers/corpora/commit_graph/1e068537ce1211a325aab42ae1263a109131c9f9 b/fuzzers/corpora/commit_graph/1e068537ce1211a325aab42ae1263a109131c9f9 new file mode 100644 index 00000000000..035173872bd Binary files /dev/null and b/fuzzers/corpora/commit_graph/1e068537ce1211a325aab42ae1263a109131c9f9 differ diff --git a/fuzzers/corpora/commit_graph/1e9c882c9d33304a5791ef6c98eee65e142bd7fd b/fuzzers/corpora/commit_graph/1e9c882c9d33304a5791ef6c98eee65e142bd7fd new file mode 100644 index 00000000000..d5b9da4e5d7 Binary files /dev/null and b/fuzzers/corpora/commit_graph/1e9c882c9d33304a5791ef6c98eee65e142bd7fd differ diff --git a/fuzzers/corpora/commit_graph/1f54935df929403a29e77591c97f767d94871aea b/fuzzers/corpora/commit_graph/1f54935df929403a29e77591c97f767d94871aea new file mode 100644 index 00000000000..6f9b0a07ba9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/1f54935df929403a29e77591c97f767d94871aea differ diff --git a/fuzzers/corpora/commit_graph/206015659641771bb0d668728c2fdc4209e65dda b/fuzzers/corpora/commit_graph/206015659641771bb0d668728c2fdc4209e65dda new file mode 100644 index 00000000000..086ab64694b Binary files /dev/null and b/fuzzers/corpora/commit_graph/206015659641771bb0d668728c2fdc4209e65dda differ diff --git a/fuzzers/corpora/commit_graph/2096493a2bcc2d15b7ae5bf3112fe49c39976ad8 b/fuzzers/corpora/commit_graph/2096493a2bcc2d15b7ae5bf3112fe49c39976ad8 new file mode 100644 index 00000000000..7b33d38e228 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2096493a2bcc2d15b7ae5bf3112fe49c39976ad8 differ diff --git a/fuzzers/corpora/commit_graph/209b74e08abe8c787b7c5ba81e51cb69c57ecded b/fuzzers/corpora/commit_graph/209b74e08abe8c787b7c5ba81e51cb69c57ecded new file mode 100644 index 00000000000..55dca76e7ef Binary files /dev/null and b/fuzzers/corpora/commit_graph/209b74e08abe8c787b7c5ba81e51cb69c57ecded differ diff --git a/fuzzers/corpora/commit_graph/21137876575fbca357fc0c96db1de73c6737e1ae b/fuzzers/corpora/commit_graph/21137876575fbca357fc0c96db1de73c6737e1ae new file mode 100644 index 00000000000..fea1ac18ca3 Binary files /dev/null and b/fuzzers/corpora/commit_graph/21137876575fbca357fc0c96db1de73c6737e1ae differ diff --git a/fuzzers/corpora/commit_graph/2143d9db9802f076c72a71184cd9d0cb4581e9e7 b/fuzzers/corpora/commit_graph/2143d9db9802f076c72a71184cd9d0cb4581e9e7 new file mode 100644 index 00000000000..bacbe8830ee Binary files /dev/null and b/fuzzers/corpora/commit_graph/2143d9db9802f076c72a71184cd9d0cb4581e9e7 differ diff --git a/fuzzers/corpora/commit_graph/21a52a5282145407d951ac73c2ff27876783899d b/fuzzers/corpora/commit_graph/21a52a5282145407d951ac73c2ff27876783899d new file mode 100644 index 00000000000..1b5d2f0ca5c Binary files /dev/null and b/fuzzers/corpora/commit_graph/21a52a5282145407d951ac73c2ff27876783899d differ diff --git a/fuzzers/corpora/commit_graph/21d5c8c8ac3a09bcba5388c472df32795986a5cb b/fuzzers/corpora/commit_graph/21d5c8c8ac3a09bcba5388c472df32795986a5cb new file mode 100644 index 00000000000..b148c6feb42 --- /dev/null +++ b/fuzzers/corpora/commit_graph/21d5c8c8ac3a09bcba5388c472df32795986a5cb @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/commit_graph/22170d1110a1c18009b7feb21a470681f55e85fb b/fuzzers/corpora/commit_graph/22170d1110a1c18009b7feb21a470681f55e85fb new file mode 100644 index 00000000000..6c16354e8b4 Binary files /dev/null and b/fuzzers/corpora/commit_graph/22170d1110a1c18009b7feb21a470681f55e85fb differ diff --git a/fuzzers/corpora/commit_graph/22f55dff94785f24252d7a070f713840f59b0870 b/fuzzers/corpora/commit_graph/22f55dff94785f24252d7a070f713840f59b0870 new file mode 100644 index 00000000000..b45c99ad215 Binary files /dev/null and b/fuzzers/corpora/commit_graph/22f55dff94785f24252d7a070f713840f59b0870 differ diff --git a/fuzzers/corpora/commit_graph/23d10ee9694e1c66bedc7060990f19a2ac3eaee3 b/fuzzers/corpora/commit_graph/23d10ee9694e1c66bedc7060990f19a2ac3eaee3 new file mode 100644 index 00000000000..8790bfd9fd6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/23d10ee9694e1c66bedc7060990f19a2ac3eaee3 differ diff --git a/fuzzers/corpora/commit_graph/2435430ca19502c3b0ec4987508d4a8fbdbc898c b/fuzzers/corpora/commit_graph/2435430ca19502c3b0ec4987508d4a8fbdbc898c new file mode 100644 index 00000000000..e1d58bd0d2a Binary files /dev/null and b/fuzzers/corpora/commit_graph/2435430ca19502c3b0ec4987508d4a8fbdbc898c differ diff --git a/fuzzers/corpora/commit_graph/244d2ea0c5c3117000b599cfab37680ba8f04513 b/fuzzers/corpora/commit_graph/244d2ea0c5c3117000b599cfab37680ba8f04513 new file mode 100644 index 00000000000..5eedc854bf4 Binary files /dev/null and b/fuzzers/corpora/commit_graph/244d2ea0c5c3117000b599cfab37680ba8f04513 differ diff --git a/fuzzers/corpora/commit_graph/248bf94143d150da2459cfdca099c30c6daff00a b/fuzzers/corpora/commit_graph/248bf94143d150da2459cfdca099c30c6daff00a new file mode 100644 index 00000000000..8b5f81b4683 Binary files /dev/null and b/fuzzers/corpora/commit_graph/248bf94143d150da2459cfdca099c30c6daff00a differ diff --git a/fuzzers/corpora/commit_graph/25bc53498129bb3717671f00c355d2637a91c86a b/fuzzers/corpora/commit_graph/25bc53498129bb3717671f00c355d2637a91c86a new file mode 100644 index 00000000000..d86bb32ef33 Binary files /dev/null and b/fuzzers/corpora/commit_graph/25bc53498129bb3717671f00c355d2637a91c86a differ diff --git a/fuzzers/corpora/commit_graph/2614f60da2d7e291501397238366d27513bff773 b/fuzzers/corpora/commit_graph/2614f60da2d7e291501397238366d27513bff773 new file mode 100644 index 00000000000..57cd70aba09 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2614f60da2d7e291501397238366d27513bff773 differ diff --git a/fuzzers/corpora/commit_graph/2651b3d5a8b4616b1faa81dabe27ab2712a27561 b/fuzzers/corpora/commit_graph/2651b3d5a8b4616b1faa81dabe27ab2712a27561 new file mode 100644 index 00000000000..5f838deaa94 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2651b3d5a8b4616b1faa81dabe27ab2712a27561 differ diff --git a/fuzzers/corpora/commit_graph/270257a2872b33dd13c4fd466cbc1ae67d613f9b b/fuzzers/corpora/commit_graph/270257a2872b33dd13c4fd466cbc1ae67d613f9b new file mode 100644 index 00000000000..30904964ef1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/270257a2872b33dd13c4fd466cbc1ae67d613f9b differ diff --git a/fuzzers/corpora/commit_graph/2830c6244c74656f6c5649c8226953905a582a38 b/fuzzers/corpora/commit_graph/2830c6244c74656f6c5649c8226953905a582a38 new file mode 100644 index 00000000000..895a8efcc2f Binary files /dev/null and b/fuzzers/corpora/commit_graph/2830c6244c74656f6c5649c8226953905a582a38 differ diff --git a/fuzzers/corpora/commit_graph/2889a85c07c20551ff0b97fc640e3c91b33aa4a1 b/fuzzers/corpora/commit_graph/2889a85c07c20551ff0b97fc640e3c91b33aa4a1 new file mode 100644 index 00000000000..349ff154cc5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2889a85c07c20551ff0b97fc640e3c91b33aa4a1 differ diff --git a/fuzzers/corpora/commit_graph/295ce43fdd56def8948d1ba2bfa7fdf0c47b5318 b/fuzzers/corpora/commit_graph/295ce43fdd56def8948d1ba2bfa7fdf0c47b5318 new file mode 100644 index 00000000000..4a3ce801f94 Binary files /dev/null and b/fuzzers/corpora/commit_graph/295ce43fdd56def8948d1ba2bfa7fdf0c47b5318 differ diff --git a/fuzzers/corpora/commit_graph/296cbb94c4e68ab86972a174405308ee34d0c40f b/fuzzers/corpora/commit_graph/296cbb94c4e68ab86972a174405308ee34d0c40f new file mode 100644 index 00000000000..45c218ea3d9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/296cbb94c4e68ab86972a174405308ee34d0c40f differ diff --git a/fuzzers/corpora/commit_graph/2975adf222cad108ec90d8225fd655e30e3bf253 b/fuzzers/corpora/commit_graph/2975adf222cad108ec90d8225fd655e30e3bf253 new file mode 100644 index 00000000000..6a16429c5ed Binary files /dev/null and b/fuzzers/corpora/commit_graph/2975adf222cad108ec90d8225fd655e30e3bf253 differ diff --git a/fuzzers/corpora/commit_graph/29f5d27760c9254ab4db661a6cd0323dd11c34ca b/fuzzers/corpora/commit_graph/29f5d27760c9254ab4db661a6cd0323dd11c34ca new file mode 100644 index 00000000000..a9d81e1d4fa Binary files /dev/null and b/fuzzers/corpora/commit_graph/29f5d27760c9254ab4db661a6cd0323dd11c34ca differ diff --git a/fuzzers/corpora/commit_graph/2a359fb09eaad968e57d353453908027645873d1 b/fuzzers/corpora/commit_graph/2a359fb09eaad968e57d353453908027645873d1 new file mode 100644 index 00000000000..a17910b8ad5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2a359fb09eaad968e57d353453908027645873d1 differ diff --git a/fuzzers/corpora/commit_graph/2a6b65a8d6c28febaa081d220a4433f8366d02bc b/fuzzers/corpora/commit_graph/2a6b65a8d6c28febaa081d220a4433f8366d02bc new file mode 100644 index 00000000000..597f363aef0 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2a6b65a8d6c28febaa081d220a4433f8366d02bc differ diff --git a/fuzzers/corpora/commit_graph/2b14dcade4d0919b0a17830fe353738015f492a6 b/fuzzers/corpora/commit_graph/2b14dcade4d0919b0a17830fe353738015f492a6 new file mode 100644 index 00000000000..33f4e6efbdb Binary files /dev/null and b/fuzzers/corpora/commit_graph/2b14dcade4d0919b0a17830fe353738015f492a6 differ diff --git a/fuzzers/corpora/commit_graph/2b298a13abbd9829e965424a1486baa13d4166c4 b/fuzzers/corpora/commit_graph/2b298a13abbd9829e965424a1486baa13d4166c4 new file mode 100644 index 00000000000..837e45f32a1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2b298a13abbd9829e965424a1486baa13d4166c4 differ diff --git a/fuzzers/corpora/commit_graph/2b44d8cd8e70e25172b4c740ebe38ef411c965b3 b/fuzzers/corpora/commit_graph/2b44d8cd8e70e25172b4c740ebe38ef411c965b3 new file mode 100644 index 00000000000..8059ce413a6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2b44d8cd8e70e25172b4c740ebe38ef411c965b3 differ diff --git a/fuzzers/corpora/commit_graph/2b590c4e61fdfcf21c017b29440747a1894b1534 b/fuzzers/corpora/commit_graph/2b590c4e61fdfcf21c017b29440747a1894b1534 new file mode 100644 index 00000000000..c3ea72932fb Binary files /dev/null and b/fuzzers/corpora/commit_graph/2b590c4e61fdfcf21c017b29440747a1894b1534 differ diff --git a/fuzzers/corpora/commit_graph/2becb18a971ae30e1a8f6680982fd7305708caa0 b/fuzzers/corpora/commit_graph/2becb18a971ae30e1a8f6680982fd7305708caa0 new file mode 100644 index 00000000000..4b133a7f8d5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2becb18a971ae30e1a8f6680982fd7305708caa0 differ diff --git a/fuzzers/corpora/commit_graph/2bf78b02099a1fe4ce50d065254e843ca55e280f b/fuzzers/corpora/commit_graph/2bf78b02099a1fe4ce50d065254e843ca55e280f new file mode 100644 index 00000000000..9b459e2097c Binary files /dev/null and b/fuzzers/corpora/commit_graph/2bf78b02099a1fe4ce50d065254e843ca55e280f differ diff --git a/fuzzers/corpora/commit_graph/2c1541ecd01aa7b9e99bccfe9804198b3e79f118 b/fuzzers/corpora/commit_graph/2c1541ecd01aa7b9e99bccfe9804198b3e79f118 new file mode 100644 index 00000000000..92daa0a2edb Binary files /dev/null and b/fuzzers/corpora/commit_graph/2c1541ecd01aa7b9e99bccfe9804198b3e79f118 differ diff --git a/fuzzers/corpora/commit_graph/2c6798057af5894c27631ff63e845fe1e4bdc9ee b/fuzzers/corpora/commit_graph/2c6798057af5894c27631ff63e845fe1e4bdc9ee new file mode 100644 index 00000000000..06a7d31fa14 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2c6798057af5894c27631ff63e845fe1e4bdc9ee differ diff --git a/fuzzers/corpora/commit_graph/2cf7eb7fe489e5acd64df755e820c871784c2ba1 b/fuzzers/corpora/commit_graph/2cf7eb7fe489e5acd64df755e820c871784c2ba1 new file mode 100644 index 00000000000..5614c74b9f8 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2cf7eb7fe489e5acd64df755e820c871784c2ba1 differ diff --git a/fuzzers/corpora/commit_graph/2d49ba35ca404baa0d593925f36a81ce53943c8d b/fuzzers/corpora/commit_graph/2d49ba35ca404baa0d593925f36a81ce53943c8d new file mode 100644 index 00000000000..36d6b853bdf Binary files /dev/null and b/fuzzers/corpora/commit_graph/2d49ba35ca404baa0d593925f36a81ce53943c8d differ diff --git a/fuzzers/corpora/commit_graph/2d507d42ca43ffc2f3c8892826e1db74144ec096 b/fuzzers/corpora/commit_graph/2d507d42ca43ffc2f3c8892826e1db74144ec096 new file mode 100644 index 00000000000..35c28f488b5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2d507d42ca43ffc2f3c8892826e1db74144ec096 differ diff --git a/fuzzers/corpora/commit_graph/2e4da693e3e336d2b1a40311a7ccf94def035b6b b/fuzzers/corpora/commit_graph/2e4da693e3e336d2b1a40311a7ccf94def035b6b new file mode 100644 index 00000000000..42727e95820 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2e4da693e3e336d2b1a40311a7ccf94def035b6b differ diff --git a/fuzzers/corpora/commit_graph/2e71ff86128b5618f0f067c407a76ff645ae2019 b/fuzzers/corpora/commit_graph/2e71ff86128b5618f0f067c407a76ff645ae2019 new file mode 100644 index 00000000000..32f02fcaf47 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2e71ff86128b5618f0f067c407a76ff645ae2019 differ diff --git a/fuzzers/corpora/commit_graph/2eb777c6d7e6ee9bd7a44e37372595043aad596b b/fuzzers/corpora/commit_graph/2eb777c6d7e6ee9bd7a44e37372595043aad596b new file mode 100644 index 00000000000..7203c91c088 Binary files /dev/null and b/fuzzers/corpora/commit_graph/2eb777c6d7e6ee9bd7a44e37372595043aad596b differ diff --git a/fuzzers/corpora/commit_graph/2ec3ebffba165b9dd49e755a9e77e23aed796628 b/fuzzers/corpora/commit_graph/2ec3ebffba165b9dd49e755a9e77e23aed796628 new file mode 100644 index 00000000000..d4018de500d Binary files /dev/null and b/fuzzers/corpora/commit_graph/2ec3ebffba165b9dd49e755a9e77e23aed796628 differ diff --git a/fuzzers/corpora/commit_graph/302703e3b0d74219868aca39ee7593944c0b2400 b/fuzzers/corpora/commit_graph/302703e3b0d74219868aca39ee7593944c0b2400 new file mode 100644 index 00000000000..6b692e19cc0 Binary files /dev/null and b/fuzzers/corpora/commit_graph/302703e3b0d74219868aca39ee7593944c0b2400 differ diff --git a/fuzzers/corpora/commit_graph/3048c6908dc3176707fa8bcb0196824e3358357a b/fuzzers/corpora/commit_graph/3048c6908dc3176707fa8bcb0196824e3358357a new file mode 100644 index 00000000000..c58805fc4dd Binary files /dev/null and b/fuzzers/corpora/commit_graph/3048c6908dc3176707fa8bcb0196824e3358357a differ diff --git a/fuzzers/corpora/commit_graph/30616cb39d3ad6060324fada03709d611ad28d5c b/fuzzers/corpora/commit_graph/30616cb39d3ad6060324fada03709d611ad28d5c new file mode 100644 index 00000000000..1fd655ed892 Binary files /dev/null and b/fuzzers/corpora/commit_graph/30616cb39d3ad6060324fada03709d611ad28d5c differ diff --git a/fuzzers/corpora/commit_graph/306beadd9b3135a00037323760eb5377c88a403e b/fuzzers/corpora/commit_graph/306beadd9b3135a00037323760eb5377c88a403e new file mode 100644 index 00000000000..86c8672306d Binary files /dev/null and b/fuzzers/corpora/commit_graph/306beadd9b3135a00037323760eb5377c88a403e differ diff --git a/fuzzers/corpora/commit_graph/31464a6fbad023923a7e4700fc11564e811bcbd2 b/fuzzers/corpora/commit_graph/31464a6fbad023923a7e4700fc11564e811bcbd2 new file mode 100644 index 00000000000..102de5c14f8 Binary files /dev/null and b/fuzzers/corpora/commit_graph/31464a6fbad023923a7e4700fc11564e811bcbd2 differ diff --git a/fuzzers/corpora/commit_graph/317f4bcfecf066961ef1982d551cd14e63c9f008 b/fuzzers/corpora/commit_graph/317f4bcfecf066961ef1982d551cd14e63c9f008 new file mode 100644 index 00000000000..5c2a5d70fbb Binary files /dev/null and b/fuzzers/corpora/commit_graph/317f4bcfecf066961ef1982d551cd14e63c9f008 differ diff --git a/fuzzers/corpora/commit_graph/31b2248faaabbec69a06098c8cb0f69c5d0aa208 b/fuzzers/corpora/commit_graph/31b2248faaabbec69a06098c8cb0f69c5d0aa208 new file mode 100644 index 00000000000..555c7b18f16 Binary files /dev/null and b/fuzzers/corpora/commit_graph/31b2248faaabbec69a06098c8cb0f69c5d0aa208 differ diff --git a/fuzzers/corpora/commit_graph/31d1c3d1147385d58dbe6f82898a5523320fbcac b/fuzzers/corpora/commit_graph/31d1c3d1147385d58dbe6f82898a5523320fbcac new file mode 100644 index 00000000000..1c5ef071906 Binary files /dev/null and b/fuzzers/corpora/commit_graph/31d1c3d1147385d58dbe6f82898a5523320fbcac differ diff --git a/fuzzers/corpora/commit_graph/32c9bc1616a78a230a3724abc02150db1cc40aa0 b/fuzzers/corpora/commit_graph/32c9bc1616a78a230a3724abc02150db1cc40aa0 new file mode 100644 index 00000000000..cfc45a1f7a2 Binary files /dev/null and b/fuzzers/corpora/commit_graph/32c9bc1616a78a230a3724abc02150db1cc40aa0 differ diff --git a/fuzzers/corpora/commit_graph/331e2866416b091252f0299e98d32cfb29237029 b/fuzzers/corpora/commit_graph/331e2866416b091252f0299e98d32cfb29237029 new file mode 100644 index 00000000000..241e719a58c Binary files /dev/null and b/fuzzers/corpora/commit_graph/331e2866416b091252f0299e98d32cfb29237029 differ diff --git a/fuzzers/corpora/commit_graph/331eb3876dd2f3f0bd51f380ac431d86d6e3bb5e b/fuzzers/corpora/commit_graph/331eb3876dd2f3f0bd51f380ac431d86d6e3bb5e new file mode 100644 index 00000000000..a52780fe2d0 Binary files /dev/null and b/fuzzers/corpora/commit_graph/331eb3876dd2f3f0bd51f380ac431d86d6e3bb5e differ diff --git a/fuzzers/corpora/commit_graph/346bd6eaeadeafcb840ff9441614b309330db63e b/fuzzers/corpora/commit_graph/346bd6eaeadeafcb840ff9441614b309330db63e new file mode 100644 index 00000000000..50b7f93a8c4 Binary files /dev/null and b/fuzzers/corpora/commit_graph/346bd6eaeadeafcb840ff9441614b309330db63e differ diff --git a/fuzzers/corpora/commit_graph/349931f447981f21476481448576e805c093a25b b/fuzzers/corpora/commit_graph/349931f447981f21476481448576e805c093a25b new file mode 100644 index 00000000000..ae5b758d968 Binary files /dev/null and b/fuzzers/corpora/commit_graph/349931f447981f21476481448576e805c093a25b differ diff --git a/fuzzers/corpora/commit_graph/34a2da1e9adaac1b4be1d40b1ece81fe00643d49 b/fuzzers/corpora/commit_graph/34a2da1e9adaac1b4be1d40b1ece81fe00643d49 new file mode 100644 index 00000000000..f5d660f124c Binary files /dev/null and b/fuzzers/corpora/commit_graph/34a2da1e9adaac1b4be1d40b1ece81fe00643d49 differ diff --git a/fuzzers/corpora/commit_graph/34bb8f475e7384a8a39618fd15fdc5fb1b12c1a1 b/fuzzers/corpora/commit_graph/34bb8f475e7384a8a39618fd15fdc5fb1b12c1a1 new file mode 100644 index 00000000000..f940f66f8df Binary files /dev/null and b/fuzzers/corpora/commit_graph/34bb8f475e7384a8a39618fd15fdc5fb1b12c1a1 differ diff --git a/fuzzers/corpora/commit_graph/351a036c6eb95db9364706b861f7e75ad26194e8 b/fuzzers/corpora/commit_graph/351a036c6eb95db9364706b861f7e75ad26194e8 new file mode 100644 index 00000000000..197bee17a62 Binary files /dev/null and b/fuzzers/corpora/commit_graph/351a036c6eb95db9364706b861f7e75ad26194e8 differ diff --git a/fuzzers/corpora/commit_graph/355452c1da8e7689d816d67cdde040b5df7eabd7 b/fuzzers/corpora/commit_graph/355452c1da8e7689d816d67cdde040b5df7eabd7 new file mode 100644 index 00000000000..f135872e53e Binary files /dev/null and b/fuzzers/corpora/commit_graph/355452c1da8e7689d816d67cdde040b5df7eabd7 differ diff --git a/fuzzers/corpora/commit_graph/35c157ad2b100b4f334cddcf3dea6ef2d85462be b/fuzzers/corpora/commit_graph/35c157ad2b100b4f334cddcf3dea6ef2d85462be new file mode 100644 index 00000000000..7d73ba721bd Binary files /dev/null and b/fuzzers/corpora/commit_graph/35c157ad2b100b4f334cddcf3dea6ef2d85462be differ diff --git a/fuzzers/corpora/commit_graph/36a81a45eabfcf53e1ae0361aa234791e2fdb750 b/fuzzers/corpora/commit_graph/36a81a45eabfcf53e1ae0361aa234791e2fdb750 new file mode 100644 index 00000000000..fc1b8dde6f6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/36a81a45eabfcf53e1ae0361aa234791e2fdb750 differ diff --git a/fuzzers/corpora/commit_graph/36ee20f6dbeb3a34e91eafbbe2e379f9ac6cfa43 b/fuzzers/corpora/commit_graph/36ee20f6dbeb3a34e91eafbbe2e379f9ac6cfa43 new file mode 100644 index 00000000000..769017a8bb1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/36ee20f6dbeb3a34e91eafbbe2e379f9ac6cfa43 differ diff --git a/fuzzers/corpora/commit_graph/377627c19bcac6adc880202048a9eac07b5417d4 b/fuzzers/corpora/commit_graph/377627c19bcac6adc880202048a9eac07b5417d4 new file mode 100644 index 00000000000..d4ef66b69f1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/377627c19bcac6adc880202048a9eac07b5417d4 differ diff --git a/fuzzers/corpora/commit_graph/38747e7c8bec2f9c923739d50ba54ff88ba6503f b/fuzzers/corpora/commit_graph/38747e7c8bec2f9c923739d50ba54ff88ba6503f new file mode 100644 index 00000000000..6d94b5766be Binary files /dev/null and b/fuzzers/corpora/commit_graph/38747e7c8bec2f9c923739d50ba54ff88ba6503f differ diff --git a/fuzzers/corpora/commit_graph/3945843a6fab2ec71030f09b237c125b97cd3ea5 b/fuzzers/corpora/commit_graph/3945843a6fab2ec71030f09b237c125b97cd3ea5 new file mode 100644 index 00000000000..76191cad060 Binary files /dev/null and b/fuzzers/corpora/commit_graph/3945843a6fab2ec71030f09b237c125b97cd3ea5 differ diff --git a/fuzzers/corpora/commit_graph/396321d39b82ffaccbc64115117df7e822b0f515 b/fuzzers/corpora/commit_graph/396321d39b82ffaccbc64115117df7e822b0f515 new file mode 100644 index 00000000000..74715a9203a Binary files /dev/null and b/fuzzers/corpora/commit_graph/396321d39b82ffaccbc64115117df7e822b0f515 differ diff --git a/fuzzers/corpora/commit_graph/396e78eb9b54e2cefb52cd76a22137c8abd6cbcf b/fuzzers/corpora/commit_graph/396e78eb9b54e2cefb52cd76a22137c8abd6cbcf new file mode 100644 index 00000000000..b5648c45390 Binary files /dev/null and b/fuzzers/corpora/commit_graph/396e78eb9b54e2cefb52cd76a22137c8abd6cbcf differ diff --git a/fuzzers/corpora/commit_graph/39c1ab66035adc104cd06a6d98b77668172d21af b/fuzzers/corpora/commit_graph/39c1ab66035adc104cd06a6d98b77668172d21af new file mode 100644 index 00000000000..f9649f7bfae Binary files /dev/null and b/fuzzers/corpora/commit_graph/39c1ab66035adc104cd06a6d98b77668172d21af differ diff --git a/fuzzers/corpora/commit_graph/3a1078c35f5401ce09b5ba921fc348dde37530bb b/fuzzers/corpora/commit_graph/3a1078c35f5401ce09b5ba921fc348dde37530bb new file mode 100644 index 00000000000..7e519f87f64 Binary files /dev/null and b/fuzzers/corpora/commit_graph/3a1078c35f5401ce09b5ba921fc348dde37530bb differ diff --git a/fuzzers/corpora/commit_graph/3aa3d8f40392d1c863d23799b8ec0aedc7191302 b/fuzzers/corpora/commit_graph/3aa3d8f40392d1c863d23799b8ec0aedc7191302 new file mode 100644 index 00000000000..3cbeaaf681d Binary files /dev/null and b/fuzzers/corpora/commit_graph/3aa3d8f40392d1c863d23799b8ec0aedc7191302 differ diff --git a/fuzzers/corpora/commit_graph/3b08c505601271cb92345ec7f0ff0b28daf90a9c b/fuzzers/corpora/commit_graph/3b08c505601271cb92345ec7f0ff0b28daf90a9c new file mode 100644 index 00000000000..69b9baba1aa Binary files /dev/null and b/fuzzers/corpora/commit_graph/3b08c505601271cb92345ec7f0ff0b28daf90a9c differ diff --git a/fuzzers/corpora/commit_graph/3b41702587be45f678b36823ad2f7e5002337dc4 b/fuzzers/corpora/commit_graph/3b41702587be45f678b36823ad2f7e5002337dc4 new file mode 100644 index 00000000000..29069abc7b8 Binary files /dev/null and b/fuzzers/corpora/commit_graph/3b41702587be45f678b36823ad2f7e5002337dc4 differ diff --git a/fuzzers/corpora/commit_graph/3b69108cc919aba0248f9b864d4e71c5f6d1931e b/fuzzers/corpora/commit_graph/3b69108cc919aba0248f9b864d4e71c5f6d1931e new file mode 100644 index 00000000000..207df24697d Binary files /dev/null and b/fuzzers/corpora/commit_graph/3b69108cc919aba0248f9b864d4e71c5f6d1931e differ diff --git a/fuzzers/corpora/commit_graph/3b90507501bb3bcfe0094f9c92cc2869f1a7dda5 b/fuzzers/corpora/commit_graph/3b90507501bb3bcfe0094f9c92cc2869f1a7dda5 new file mode 100644 index 00000000000..57272325b67 Binary files /dev/null and b/fuzzers/corpora/commit_graph/3b90507501bb3bcfe0094f9c92cc2869f1a7dda5 differ diff --git a/fuzzers/corpora/commit_graph/3bc7fe44c3a1464dd35a4d22b482f46cdeda0405 b/fuzzers/corpora/commit_graph/3bc7fe44c3a1464dd35a4d22b482f46cdeda0405 new file mode 100644 index 00000000000..e1a8bd350e8 Binary files /dev/null and b/fuzzers/corpora/commit_graph/3bc7fe44c3a1464dd35a4d22b482f46cdeda0405 differ diff --git a/fuzzers/corpora/commit_graph/3ce99994986efb6df3f3568423e0077b53c7ef78 b/fuzzers/corpora/commit_graph/3ce99994986efb6df3f3568423e0077b53c7ef78 new file mode 100644 index 00000000000..21f9ab81484 Binary files /dev/null and b/fuzzers/corpora/commit_graph/3ce99994986efb6df3f3568423e0077b53c7ef78 differ diff --git a/fuzzers/corpora/commit_graph/3d6cb3ba21181c9f0ab08b2608eab773f36773f2 b/fuzzers/corpora/commit_graph/3d6cb3ba21181c9f0ab08b2608eab773f36773f2 new file mode 100644 index 00000000000..548a1295b23 Binary files /dev/null and b/fuzzers/corpora/commit_graph/3d6cb3ba21181c9f0ab08b2608eab773f36773f2 differ diff --git a/fuzzers/corpora/commit_graph/3d8ec41450b943d5dea73fb1e393960b03d7c3b9 b/fuzzers/corpora/commit_graph/3d8ec41450b943d5dea73fb1e393960b03d7c3b9 new file mode 100644 index 00000000000..150ed2c92ae Binary files /dev/null and b/fuzzers/corpora/commit_graph/3d8ec41450b943d5dea73fb1e393960b03d7c3b9 differ diff --git a/fuzzers/corpora/commit_graph/3e29e8baaac0f6c7e4cf3d5adca2ab3a2c491ac7 b/fuzzers/corpora/commit_graph/3e29e8baaac0f6c7e4cf3d5adca2ab3a2c491ac7 new file mode 100644 index 00000000000..bc9597d420d Binary files /dev/null and b/fuzzers/corpora/commit_graph/3e29e8baaac0f6c7e4cf3d5adca2ab3a2c491ac7 differ diff --git a/fuzzers/corpora/commit_graph/3e9469b3c68ba334671aacda7a7669b0e97b74d6 b/fuzzers/corpora/commit_graph/3e9469b3c68ba334671aacda7a7669b0e97b74d6 new file mode 100644 index 00000000000..cb20df0df54 Binary files /dev/null and b/fuzzers/corpora/commit_graph/3e9469b3c68ba334671aacda7a7669b0e97b74d6 differ diff --git a/fuzzers/corpora/commit_graph/3eeda3bfa7abef69911c94520c009a08c49b9942 b/fuzzers/corpora/commit_graph/3eeda3bfa7abef69911c94520c009a08c49b9942 new file mode 100644 index 00000000000..dbf559f7322 Binary files /dev/null and b/fuzzers/corpora/commit_graph/3eeda3bfa7abef69911c94520c009a08c49b9942 differ diff --git a/fuzzers/corpora/commit_graph/3f0f5021016451b57f673d0603cd9e4830c2198d b/fuzzers/corpora/commit_graph/3f0f5021016451b57f673d0603cd9e4830c2198d new file mode 100644 index 00000000000..2f4c5232681 Binary files /dev/null and b/fuzzers/corpora/commit_graph/3f0f5021016451b57f673d0603cd9e4830c2198d differ diff --git a/fuzzers/corpora/commit_graph/3f46540fbd94bf0337c1d0d7437ec992a3568f09 b/fuzzers/corpora/commit_graph/3f46540fbd94bf0337c1d0d7437ec992a3568f09 new file mode 100644 index 00000000000..7fbf35089a7 Binary files /dev/null and b/fuzzers/corpora/commit_graph/3f46540fbd94bf0337c1d0d7437ec992a3568f09 differ diff --git a/fuzzers/corpora/commit_graph/402d9c25d5833d42630882ab5c57833266bef785 b/fuzzers/corpora/commit_graph/402d9c25d5833d42630882ab5c57833266bef785 new file mode 100644 index 00000000000..66002819626 Binary files /dev/null and b/fuzzers/corpora/commit_graph/402d9c25d5833d42630882ab5c57833266bef785 differ diff --git a/fuzzers/corpora/commit_graph/4048bb3c26d67c345630ff9e86db551a3add6549 b/fuzzers/corpora/commit_graph/4048bb3c26d67c345630ff9e86db551a3add6549 new file mode 100644 index 00000000000..a07e1957b93 Binary files /dev/null and b/fuzzers/corpora/commit_graph/4048bb3c26d67c345630ff9e86db551a3add6549 differ diff --git a/fuzzers/corpora/commit_graph/40792f23c1281842dab671e8b213fc408d1ec39f b/fuzzers/corpora/commit_graph/40792f23c1281842dab671e8b213fc408d1ec39f new file mode 100644 index 00000000000..9a0f9c2988b Binary files /dev/null and b/fuzzers/corpora/commit_graph/40792f23c1281842dab671e8b213fc408d1ec39f differ diff --git a/fuzzers/corpora/commit_graph/41cd0b5d9a9540947b7b1841a55e4c11bd4346a2 b/fuzzers/corpora/commit_graph/41cd0b5d9a9540947b7b1841a55e4c11bd4346a2 new file mode 100644 index 00000000000..a1b3a077a19 Binary files /dev/null and b/fuzzers/corpora/commit_graph/41cd0b5d9a9540947b7b1841a55e4c11bd4346a2 differ diff --git a/fuzzers/corpora/commit_graph/41d86e5ea3df4a0de60d42aeb16e2a5599aedeae b/fuzzers/corpora/commit_graph/41d86e5ea3df4a0de60d42aeb16e2a5599aedeae new file mode 100644 index 00000000000..d749cf6e0bc Binary files /dev/null and b/fuzzers/corpora/commit_graph/41d86e5ea3df4a0de60d42aeb16e2a5599aedeae differ diff --git a/fuzzers/corpora/commit_graph/42b4e5430b2b1b17a361067fb9dd33ab74e52232 b/fuzzers/corpora/commit_graph/42b4e5430b2b1b17a361067fb9dd33ab74e52232 new file mode 100644 index 00000000000..6adf001bd64 Binary files /dev/null and b/fuzzers/corpora/commit_graph/42b4e5430b2b1b17a361067fb9dd33ab74e52232 differ diff --git a/fuzzers/corpora/commit_graph/42ef1c9d234b90acaf1651d930fc52d5f8f158f2 b/fuzzers/corpora/commit_graph/42ef1c9d234b90acaf1651d930fc52d5f8f158f2 new file mode 100644 index 00000000000..0514ae83705 Binary files /dev/null and b/fuzzers/corpora/commit_graph/42ef1c9d234b90acaf1651d930fc52d5f8f158f2 differ diff --git a/fuzzers/corpora/commit_graph/4570c8ff26d7f31afe73b3d9a35a29bc1274d68a b/fuzzers/corpora/commit_graph/4570c8ff26d7f31afe73b3d9a35a29bc1274d68a new file mode 100644 index 00000000000..834d62b346b Binary files /dev/null and b/fuzzers/corpora/commit_graph/4570c8ff26d7f31afe73b3d9a35a29bc1274d68a differ diff --git a/fuzzers/corpora/commit_graph/45cf4751a5929930a7c30ec10134434b9ee13c3d b/fuzzers/corpora/commit_graph/45cf4751a5929930a7c30ec10134434b9ee13c3d new file mode 100644 index 00000000000..b761279db11 Binary files /dev/null and b/fuzzers/corpora/commit_graph/45cf4751a5929930a7c30ec10134434b9ee13c3d differ diff --git a/fuzzers/corpora/commit_graph/46e9d351dd5bb71f7d4d8f15b3fad312c781452e b/fuzzers/corpora/commit_graph/46e9d351dd5bb71f7d4d8f15b3fad312c781452e new file mode 100644 index 00000000000..ce26235334d Binary files /dev/null and b/fuzzers/corpora/commit_graph/46e9d351dd5bb71f7d4d8f15b3fad312c781452e differ diff --git a/fuzzers/corpora/commit_graph/472421633b984556b96bc20f1fcf7a98c25736f3 b/fuzzers/corpora/commit_graph/472421633b984556b96bc20f1fcf7a98c25736f3 new file mode 100644 index 00000000000..4a2faa137f0 Binary files /dev/null and b/fuzzers/corpora/commit_graph/472421633b984556b96bc20f1fcf7a98c25736f3 differ diff --git a/fuzzers/corpora/commit_graph/47f35b91699caee098cacdde0161ffab21bdfc57 b/fuzzers/corpora/commit_graph/47f35b91699caee098cacdde0161ffab21bdfc57 new file mode 100644 index 00000000000..a0f24ef4866 Binary files /dev/null and b/fuzzers/corpora/commit_graph/47f35b91699caee098cacdde0161ffab21bdfc57 differ diff --git a/fuzzers/corpora/commit_graph/48b9da327218f9409287687a43b7eead4789a588 b/fuzzers/corpora/commit_graph/48b9da327218f9409287687a43b7eead4789a588 new file mode 100644 index 00000000000..c6f2b79c795 Binary files /dev/null and b/fuzzers/corpora/commit_graph/48b9da327218f9409287687a43b7eead4789a588 differ diff --git a/fuzzers/corpora/commit_graph/48d14fca326d5d591d18d34c2821a457277819a2 b/fuzzers/corpora/commit_graph/48d14fca326d5d591d18d34c2821a457277819a2 new file mode 100644 index 00000000000..d223341edbc Binary files /dev/null and b/fuzzers/corpora/commit_graph/48d14fca326d5d591d18d34c2821a457277819a2 differ diff --git a/fuzzers/corpora/commit_graph/48f3a33e2a027f5735d0a333ec4acd5a2aa57118 b/fuzzers/corpora/commit_graph/48f3a33e2a027f5735d0a333ec4acd5a2aa57118 new file mode 100644 index 00000000000..0604edbacc8 Binary files /dev/null and b/fuzzers/corpora/commit_graph/48f3a33e2a027f5735d0a333ec4acd5a2aa57118 differ diff --git a/fuzzers/corpora/commit_graph/49e0eee24eab094a9c62f6b37b6ba01f8aece4e4 b/fuzzers/corpora/commit_graph/49e0eee24eab094a9c62f6b37b6ba01f8aece4e4 new file mode 100644 index 00000000000..78f9425794b Binary files /dev/null and b/fuzzers/corpora/commit_graph/49e0eee24eab094a9c62f6b37b6ba01f8aece4e4 differ diff --git a/fuzzers/corpora/commit_graph/4b45bcb707d2a0bc23b415e9bc3d7eb1f7f0e188 b/fuzzers/corpora/commit_graph/4b45bcb707d2a0bc23b415e9bc3d7eb1f7f0e188 new file mode 100644 index 00000000000..3a1fdd7bc6c Binary files /dev/null and b/fuzzers/corpora/commit_graph/4b45bcb707d2a0bc23b415e9bc3d7eb1f7f0e188 differ diff --git a/fuzzers/corpora/commit_graph/4c428300fe4866fe81cff02ad4bc14b6848f7f73 b/fuzzers/corpora/commit_graph/4c428300fe4866fe81cff02ad4bc14b6848f7f73 new file mode 100644 index 00000000000..d7f09e3ba21 Binary files /dev/null and b/fuzzers/corpora/commit_graph/4c428300fe4866fe81cff02ad4bc14b6848f7f73 differ diff --git a/fuzzers/corpora/commit_graph/4d69c567df2e858c5f248b3fc8e4a9c04f02481c b/fuzzers/corpora/commit_graph/4d69c567df2e858c5f248b3fc8e4a9c04f02481c new file mode 100644 index 00000000000..26ba52528c9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/4d69c567df2e858c5f248b3fc8e4a9c04f02481c differ diff --git a/fuzzers/corpora/commit_graph/4d88b6c9b513d5db2e07313a39b43d112d3d4562 b/fuzzers/corpora/commit_graph/4d88b6c9b513d5db2e07313a39b43d112d3d4562 new file mode 100644 index 00000000000..bfe64c948a0 Binary files /dev/null and b/fuzzers/corpora/commit_graph/4d88b6c9b513d5db2e07313a39b43d112d3d4562 differ diff --git a/fuzzers/corpora/commit_graph/4da73370cf854ef8bd08c7f79b92a187cdbff278 b/fuzzers/corpora/commit_graph/4da73370cf854ef8bd08c7f79b92a187cdbff278 new file mode 100644 index 00000000000..da1801e7701 Binary files /dev/null and b/fuzzers/corpora/commit_graph/4da73370cf854ef8bd08c7f79b92a187cdbff278 differ diff --git a/fuzzers/corpora/commit_graph/4e4b2827351bbfd414b718052a8f950a9e3eb7ee b/fuzzers/corpora/commit_graph/4e4b2827351bbfd414b718052a8f950a9e3eb7ee new file mode 100644 index 00000000000..77d2e785e29 Binary files /dev/null and b/fuzzers/corpora/commit_graph/4e4b2827351bbfd414b718052a8f950a9e3eb7ee differ diff --git a/fuzzers/corpora/commit_graph/4ed43f7d3c0305461edcbc86f62e0c6ad56df01e b/fuzzers/corpora/commit_graph/4ed43f7d3c0305461edcbc86f62e0c6ad56df01e new file mode 100644 index 00000000000..cfef7a1fd13 Binary files /dev/null and b/fuzzers/corpora/commit_graph/4ed43f7d3c0305461edcbc86f62e0c6ad56df01e differ diff --git a/fuzzers/corpora/commit_graph/4f011529809e88205421fa8ce39dcc025293bcb8 b/fuzzers/corpora/commit_graph/4f011529809e88205421fa8ce39dcc025293bcb8 new file mode 100644 index 00000000000..2331acfe78b Binary files /dev/null and b/fuzzers/corpora/commit_graph/4f011529809e88205421fa8ce39dcc025293bcb8 differ diff --git a/fuzzers/corpora/commit_graph/4f1928b6376369ab6acf8a282284366cc3bf71ef b/fuzzers/corpora/commit_graph/4f1928b6376369ab6acf8a282284366cc3bf71ef new file mode 100644 index 00000000000..ad3d1739ce9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/4f1928b6376369ab6acf8a282284366cc3bf71ef differ diff --git a/fuzzers/corpora/commit_graph/4f669eca3416c44f0d003ef2720d03e697e2230e b/fuzzers/corpora/commit_graph/4f669eca3416c44f0d003ef2720d03e697e2230e new file mode 100644 index 00000000000..6a143b3a11a Binary files /dev/null and b/fuzzers/corpora/commit_graph/4f669eca3416c44f0d003ef2720d03e697e2230e differ diff --git a/fuzzers/corpora/commit_graph/4f750f24ecb5080bea2845061cfd3ce4529d30ee b/fuzzers/corpora/commit_graph/4f750f24ecb5080bea2845061cfd3ce4529d30ee new file mode 100644 index 00000000000..ea36bdc1e64 Binary files /dev/null and b/fuzzers/corpora/commit_graph/4f750f24ecb5080bea2845061cfd3ce4529d30ee differ diff --git a/fuzzers/corpora/commit_graph/4fab9bb2bacf562e65f4a8681c429e6ea92aaed7 b/fuzzers/corpora/commit_graph/4fab9bb2bacf562e65f4a8681c429e6ea92aaed7 new file mode 100644 index 00000000000..d00a7944d89 Binary files /dev/null and b/fuzzers/corpora/commit_graph/4fab9bb2bacf562e65f4a8681c429e6ea92aaed7 differ diff --git a/fuzzers/corpora/commit_graph/4fd757c7251c17413b3005fb38aee0fd029d89ec b/fuzzers/corpora/commit_graph/4fd757c7251c17413b3005fb38aee0fd029d89ec new file mode 100644 index 00000000000..4f4db7d47d6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/4fd757c7251c17413b3005fb38aee0fd029d89ec differ diff --git a/fuzzers/corpora/commit_graph/506092de91dcf93254cdd5ad9e02a953a38099ea b/fuzzers/corpora/commit_graph/506092de91dcf93254cdd5ad9e02a953a38099ea new file mode 100644 index 00000000000..64c5405f2c2 Binary files /dev/null and b/fuzzers/corpora/commit_graph/506092de91dcf93254cdd5ad9e02a953a38099ea differ diff --git a/fuzzers/corpora/commit_graph/50e934fb52d9bc5cd2a531adced1cad7f102a112 b/fuzzers/corpora/commit_graph/50e934fb52d9bc5cd2a531adced1cad7f102a112 new file mode 100644 index 00000000000..b6301797506 Binary files /dev/null and b/fuzzers/corpora/commit_graph/50e934fb52d9bc5cd2a531adced1cad7f102a112 differ diff --git a/fuzzers/corpora/commit_graph/512e49a9e789656964988950009e6534907e6317 b/fuzzers/corpora/commit_graph/512e49a9e789656964988950009e6534907e6317 new file mode 100644 index 00000000000..7cf8e31811b Binary files /dev/null and b/fuzzers/corpora/commit_graph/512e49a9e789656964988950009e6534907e6317 differ diff --git a/fuzzers/corpora/commit_graph/51404149f1ea30ee6959fafe81a52acabed97e9e b/fuzzers/corpora/commit_graph/51404149f1ea30ee6959fafe81a52acabed97e9e new file mode 100644 index 00000000000..394e4bfaaae Binary files /dev/null and b/fuzzers/corpora/commit_graph/51404149f1ea30ee6959fafe81a52acabed97e9e differ diff --git a/fuzzers/corpora/commit_graph/5150f8a67399ee16178a2b08198cf91a90c0e53e b/fuzzers/corpora/commit_graph/5150f8a67399ee16178a2b08198cf91a90c0e53e new file mode 100644 index 00000000000..c0867a0a7ea Binary files /dev/null and b/fuzzers/corpora/commit_graph/5150f8a67399ee16178a2b08198cf91a90c0e53e differ diff --git a/fuzzers/corpora/commit_graph/51a1fd23dfe5a8062cd4601d235509247f3bc2dc b/fuzzers/corpora/commit_graph/51a1fd23dfe5a8062cd4601d235509247f3bc2dc new file mode 100644 index 00000000000..c448928732a Binary files /dev/null and b/fuzzers/corpora/commit_graph/51a1fd23dfe5a8062cd4601d235509247f3bc2dc differ diff --git a/fuzzers/corpora/commit_graph/51a963486f041a60c422f0dd6da3b69c52f12fb7 b/fuzzers/corpora/commit_graph/51a963486f041a60c422f0dd6da3b69c52f12fb7 new file mode 100644 index 00000000000..f8a1c562ece Binary files /dev/null and b/fuzzers/corpora/commit_graph/51a963486f041a60c422f0dd6da3b69c52f12fb7 differ diff --git a/fuzzers/corpora/commit_graph/51fbf57a2a35ec33164838fa254fe605a3c868e9 b/fuzzers/corpora/commit_graph/51fbf57a2a35ec33164838fa254fe605a3c868e9 new file mode 100644 index 00000000000..d0c77cd132b Binary files /dev/null and b/fuzzers/corpora/commit_graph/51fbf57a2a35ec33164838fa254fe605a3c868e9 differ diff --git a/fuzzers/corpora/commit_graph/53068b9f9cb54bb52d076e9602ccd55f169ef39a b/fuzzers/corpora/commit_graph/53068b9f9cb54bb52d076e9602ccd55f169ef39a new file mode 100644 index 00000000000..bed0af6203c Binary files /dev/null and b/fuzzers/corpora/commit_graph/53068b9f9cb54bb52d076e9602ccd55f169ef39a differ diff --git a/fuzzers/corpora/commit_graph/5314619e15fa5ee67df44481b8213a53786d39c5 b/fuzzers/corpora/commit_graph/5314619e15fa5ee67df44481b8213a53786d39c5 new file mode 100644 index 00000000000..a00e14dac12 Binary files /dev/null and b/fuzzers/corpora/commit_graph/5314619e15fa5ee67df44481b8213a53786d39c5 differ diff --git a/fuzzers/corpora/commit_graph/533f5f00275968129846522fe01e2819746272eb b/fuzzers/corpora/commit_graph/533f5f00275968129846522fe01e2819746272eb new file mode 100644 index 00000000000..0830b01dcd2 Binary files /dev/null and b/fuzzers/corpora/commit_graph/533f5f00275968129846522fe01e2819746272eb differ diff --git a/fuzzers/corpora/commit_graph/53a62799135c282435a17e032deda03eaf9daf0f b/fuzzers/corpora/commit_graph/53a62799135c282435a17e032deda03eaf9daf0f new file mode 100644 index 00000000000..f6bfddbef53 Binary files /dev/null and b/fuzzers/corpora/commit_graph/53a62799135c282435a17e032deda03eaf9daf0f differ diff --git a/fuzzers/corpora/commit_graph/53c9d5cd849977e523d92dd2d639e9b0e721be50 b/fuzzers/corpora/commit_graph/53c9d5cd849977e523d92dd2d639e9b0e721be50 new file mode 100644 index 00000000000..ed30c5e7b51 Binary files /dev/null and b/fuzzers/corpora/commit_graph/53c9d5cd849977e523d92dd2d639e9b0e721be50 differ diff --git a/fuzzers/corpora/commit_graph/54767a0bb3b96d39f5b2004ce3f274465f1a927e b/fuzzers/corpora/commit_graph/54767a0bb3b96d39f5b2004ce3f274465f1a927e new file mode 100644 index 00000000000..fd9a0ac653c Binary files /dev/null and b/fuzzers/corpora/commit_graph/54767a0bb3b96d39f5b2004ce3f274465f1a927e differ diff --git a/fuzzers/corpora/commit_graph/548de37dbe6a3829b73d976996ec9838cf608554 b/fuzzers/corpora/commit_graph/548de37dbe6a3829b73d976996ec9838cf608554 new file mode 100644 index 00000000000..89772e2009a Binary files /dev/null and b/fuzzers/corpora/commit_graph/548de37dbe6a3829b73d976996ec9838cf608554 differ diff --git a/fuzzers/corpora/commit_graph/5522cefa54b798ea4aba8ef2a42ad248a7fb02ee b/fuzzers/corpora/commit_graph/5522cefa54b798ea4aba8ef2a42ad248a7fb02ee new file mode 100644 index 00000000000..6a4da78121c Binary files /dev/null and b/fuzzers/corpora/commit_graph/5522cefa54b798ea4aba8ef2a42ad248a7fb02ee differ diff --git a/fuzzers/corpora/commit_graph/554fab3eef5d8709f06d1d4319efe5c0c437421b b/fuzzers/corpora/commit_graph/554fab3eef5d8709f06d1d4319efe5c0c437421b new file mode 100644 index 00000000000..7a54bd0fe8e Binary files /dev/null and b/fuzzers/corpora/commit_graph/554fab3eef5d8709f06d1d4319efe5c0c437421b differ diff --git a/fuzzers/corpora/commit_graph/567fe73919dae39b0bcb78b03d655643a71714a8 b/fuzzers/corpora/commit_graph/567fe73919dae39b0bcb78b03d655643a71714a8 new file mode 100644 index 00000000000..56b1e141a8b Binary files /dev/null and b/fuzzers/corpora/commit_graph/567fe73919dae39b0bcb78b03d655643a71714a8 differ diff --git a/fuzzers/corpora/commit_graph/5717a281aa722ee4a32dfa1cc72fc5d6081f6755 b/fuzzers/corpora/commit_graph/5717a281aa722ee4a32dfa1cc72fc5d6081f6755 new file mode 100644 index 00000000000..77f0e516eee Binary files /dev/null and b/fuzzers/corpora/commit_graph/5717a281aa722ee4a32dfa1cc72fc5d6081f6755 differ diff --git a/fuzzers/corpora/commit_graph/577d814e0be43df9321c5b27119c398bd00a00c5 b/fuzzers/corpora/commit_graph/577d814e0be43df9321c5b27119c398bd00a00c5 new file mode 100644 index 00000000000..c892728c8ef Binary files /dev/null and b/fuzzers/corpora/commit_graph/577d814e0be43df9321c5b27119c398bd00a00c5 differ diff --git a/fuzzers/corpora/commit_graph/58680611707c6188f9f067f8747b699cd2fe82d3 b/fuzzers/corpora/commit_graph/58680611707c6188f9f067f8747b699cd2fe82d3 new file mode 100644 index 00000000000..81efaf38a34 Binary files /dev/null and b/fuzzers/corpora/commit_graph/58680611707c6188f9f067f8747b699cd2fe82d3 differ diff --git a/fuzzers/corpora/commit_graph/5915b7f91dd43ec37a4718061c90cbec2686b916 b/fuzzers/corpora/commit_graph/5915b7f91dd43ec37a4718061c90cbec2686b916 new file mode 100644 index 00000000000..f22d2aa7f16 Binary files /dev/null and b/fuzzers/corpora/commit_graph/5915b7f91dd43ec37a4718061c90cbec2686b916 differ diff --git a/fuzzers/corpora/commit_graph/599516e368ff621dd06d8450837350f4e9558c38 b/fuzzers/corpora/commit_graph/599516e368ff621dd06d8450837350f4e9558c38 new file mode 100644 index 00000000000..6aff8eef662 Binary files /dev/null and b/fuzzers/corpora/commit_graph/599516e368ff621dd06d8450837350f4e9558c38 differ diff --git a/fuzzers/corpora/commit_graph/5a2d01d141e4d523e718c30e20cb07c3ad98f33d b/fuzzers/corpora/commit_graph/5a2d01d141e4d523e718c30e20cb07c3ad98f33d new file mode 100644 index 00000000000..fb20c583aa8 Binary files /dev/null and b/fuzzers/corpora/commit_graph/5a2d01d141e4d523e718c30e20cb07c3ad98f33d differ diff --git a/fuzzers/corpora/commit_graph/5a9803ef8cd88d1e8f1d6e5920b8afd170cafb11 b/fuzzers/corpora/commit_graph/5a9803ef8cd88d1e8f1d6e5920b8afd170cafb11 new file mode 100644 index 00000000000..38c2c086a26 Binary files /dev/null and b/fuzzers/corpora/commit_graph/5a9803ef8cd88d1e8f1d6e5920b8afd170cafb11 differ diff --git a/fuzzers/corpora/commit_graph/5ba93c9db0cff93f52b521d7420e43f6eda2784f b/fuzzers/corpora/commit_graph/5ba93c9db0cff93f52b521d7420e43f6eda2784f new file mode 100644 index 00000000000..f76dd238ade Binary files /dev/null and b/fuzzers/corpora/commit_graph/5ba93c9db0cff93f52b521d7420e43f6eda2784f differ diff --git a/fuzzers/corpora/commit_graph/5bf0ca772092e6fa34b6822f61a1b1c3d7f2c6e3 b/fuzzers/corpora/commit_graph/5bf0ca772092e6fa34b6822f61a1b1c3d7f2c6e3 new file mode 100644 index 00000000000..06dd1e1a82b Binary files /dev/null and b/fuzzers/corpora/commit_graph/5bf0ca772092e6fa34b6822f61a1b1c3d7f2c6e3 differ diff --git a/fuzzers/corpora/commit_graph/5cfbfb3e12b629dc9f74baf0a8741345ec288795 b/fuzzers/corpora/commit_graph/5cfbfb3e12b629dc9f74baf0a8741345ec288795 new file mode 100644 index 00000000000..73e257d4b98 Binary files /dev/null and b/fuzzers/corpora/commit_graph/5cfbfb3e12b629dc9f74baf0a8741345ec288795 differ diff --git a/fuzzers/corpora/commit_graph/5d8cc97b739c39820b761b6551d34dd647da6816 b/fuzzers/corpora/commit_graph/5d8cc97b739c39820b761b6551d34dd647da6816 new file mode 100644 index 00000000000..7a241f33030 Binary files /dev/null and b/fuzzers/corpora/commit_graph/5d8cc97b739c39820b761b6551d34dd647da6816 differ diff --git a/fuzzers/corpora/commit_graph/5dcbb3e1c2fc9a191dd3f3443b86f6bc38c39e37 b/fuzzers/corpora/commit_graph/5dcbb3e1c2fc9a191dd3f3443b86f6bc38c39e37 new file mode 100644 index 00000000000..04a1639c6ac Binary files /dev/null and b/fuzzers/corpora/commit_graph/5dcbb3e1c2fc9a191dd3f3443b86f6bc38c39e37 differ diff --git a/fuzzers/corpora/commit_graph/5ec17d081aef9872f746e88ad8b03553719f9c36 b/fuzzers/corpora/commit_graph/5ec17d081aef9872f746e88ad8b03553719f9c36 new file mode 100644 index 00000000000..84fa131ecb5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/5ec17d081aef9872f746e88ad8b03553719f9c36 differ diff --git a/fuzzers/corpora/commit_graph/5f88e3ba60c11be25c47a842763d8870d23cc7f2 b/fuzzers/corpora/commit_graph/5f88e3ba60c11be25c47a842763d8870d23cc7f2 new file mode 100644 index 00000000000..36693877dea Binary files /dev/null and b/fuzzers/corpora/commit_graph/5f88e3ba60c11be25c47a842763d8870d23cc7f2 differ diff --git a/fuzzers/corpora/commit_graph/6045e4d2bf85013c78a32e71b014ba3d4a4b7c61 b/fuzzers/corpora/commit_graph/6045e4d2bf85013c78a32e71b014ba3d4a4b7c61 new file mode 100644 index 00000000000..5ebec5aee8e Binary files /dev/null and b/fuzzers/corpora/commit_graph/6045e4d2bf85013c78a32e71b014ba3d4a4b7c61 differ diff --git a/fuzzers/corpora/commit_graph/615c7ba7ffbce955ffd964682e2a0f7ef3c767e4 b/fuzzers/corpora/commit_graph/615c7ba7ffbce955ffd964682e2a0f7ef3c767e4 new file mode 100644 index 00000000000..8360672e47d Binary files /dev/null and b/fuzzers/corpora/commit_graph/615c7ba7ffbce955ffd964682e2a0f7ef3c767e4 differ diff --git a/fuzzers/corpora/commit_graph/6189f29cbbe88ac6cb32fdefecda1bd6194332a6 b/fuzzers/corpora/commit_graph/6189f29cbbe88ac6cb32fdefecda1bd6194332a6 new file mode 100644 index 00000000000..40ec5b1bb2a Binary files /dev/null and b/fuzzers/corpora/commit_graph/6189f29cbbe88ac6cb32fdefecda1bd6194332a6 differ diff --git a/fuzzers/corpora/commit_graph/627224cb8484c62992dcbc4cdebdbfa48a3c021a b/fuzzers/corpora/commit_graph/627224cb8484c62992dcbc4cdebdbfa48a3c021a new file mode 100644 index 00000000000..f73b59d91cd Binary files /dev/null and b/fuzzers/corpora/commit_graph/627224cb8484c62992dcbc4cdebdbfa48a3c021a differ diff --git a/fuzzers/corpora/commit_graph/629fff0962d298a7283a3d1e1d1b940dfef9b315 b/fuzzers/corpora/commit_graph/629fff0962d298a7283a3d1e1d1b940dfef9b315 new file mode 100644 index 00000000000..1c0c2c4822a Binary files /dev/null and b/fuzzers/corpora/commit_graph/629fff0962d298a7283a3d1e1d1b940dfef9b315 differ diff --git a/fuzzers/corpora/commit_graph/6322594cff2a99d0abb1139e6a43b06df76d539a b/fuzzers/corpora/commit_graph/6322594cff2a99d0abb1139e6a43b06df76d539a new file mode 100644 index 00000000000..4dabbce05b0 Binary files /dev/null and b/fuzzers/corpora/commit_graph/6322594cff2a99d0abb1139e6a43b06df76d539a differ diff --git a/fuzzers/corpora/commit_graph/63de5e8e042222d53bf05640c87da376aefb76cc b/fuzzers/corpora/commit_graph/63de5e8e042222d53bf05640c87da376aefb76cc new file mode 100644 index 00000000000..2fd95a3fd50 Binary files /dev/null and b/fuzzers/corpora/commit_graph/63de5e8e042222d53bf05640c87da376aefb76cc differ diff --git a/fuzzers/corpora/commit_graph/647dbb1d05fe0fab685bfe126bd9ac3a12b6bccf b/fuzzers/corpora/commit_graph/647dbb1d05fe0fab685bfe126bd9ac3a12b6bccf new file mode 100644 index 00000000000..245d46b1801 Binary files /dev/null and b/fuzzers/corpora/commit_graph/647dbb1d05fe0fab685bfe126bd9ac3a12b6bccf differ diff --git a/fuzzers/corpora/commit_graph/647e5e265d8d1079784fc2a3da25f7ba58126acd b/fuzzers/corpora/commit_graph/647e5e265d8d1079784fc2a3da25f7ba58126acd new file mode 100644 index 00000000000..633a2a3db7d Binary files /dev/null and b/fuzzers/corpora/commit_graph/647e5e265d8d1079784fc2a3da25f7ba58126acd differ diff --git a/fuzzers/corpora/commit_graph/653bd480dfd1e5f4bdca702aba3dfd8da0c204b7 b/fuzzers/corpora/commit_graph/653bd480dfd1e5f4bdca702aba3dfd8da0c204b7 new file mode 100644 index 00000000000..d12360225a6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/653bd480dfd1e5f4bdca702aba3dfd8da0c204b7 differ diff --git a/fuzzers/corpora/commit_graph/65485740a465377213c80fa68028727f281299fb b/fuzzers/corpora/commit_graph/65485740a465377213c80fa68028727f281299fb new file mode 100644 index 00000000000..c100bdb51b5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/65485740a465377213c80fa68028727f281299fb differ diff --git a/fuzzers/corpora/commit_graph/6551f8c8c3028006d0cc4997943df8a86ee3f598 b/fuzzers/corpora/commit_graph/6551f8c8c3028006d0cc4997943df8a86ee3f598 new file mode 100644 index 00000000000..0ff4ddab708 Binary files /dev/null and b/fuzzers/corpora/commit_graph/6551f8c8c3028006d0cc4997943df8a86ee3f598 differ diff --git a/fuzzers/corpora/commit_graph/67799e79d33883510f85ae9705ab3932862128a2 b/fuzzers/corpora/commit_graph/67799e79d33883510f85ae9705ab3932862128a2 new file mode 100644 index 00000000000..2782b4f37d2 Binary files /dev/null and b/fuzzers/corpora/commit_graph/67799e79d33883510f85ae9705ab3932862128a2 differ diff --git a/fuzzers/corpora/commit_graph/67b475481e5a21351b49789874adbc988aefd64c b/fuzzers/corpora/commit_graph/67b475481e5a21351b49789874adbc988aefd64c new file mode 100644 index 00000000000..263753aaf79 Binary files /dev/null and b/fuzzers/corpora/commit_graph/67b475481e5a21351b49789874adbc988aefd64c differ diff --git a/fuzzers/corpora/commit_graph/67e5a649967dee002d1c181e079748c404e29767 b/fuzzers/corpora/commit_graph/67e5a649967dee002d1c181e079748c404e29767 new file mode 100644 index 00000000000..3bb8714af9a Binary files /dev/null and b/fuzzers/corpora/commit_graph/67e5a649967dee002d1c181e079748c404e29767 differ diff --git a/fuzzers/corpora/commit_graph/687424a4a31a66a78d1637c680c9c10746741007 b/fuzzers/corpora/commit_graph/687424a4a31a66a78d1637c680c9c10746741007 new file mode 100644 index 00000000000..6b26c194ab3 Binary files /dev/null and b/fuzzers/corpora/commit_graph/687424a4a31a66a78d1637c680c9c10746741007 differ diff --git a/fuzzers/corpora/commit_graph/68fa6dd52832657cb8dd7e1485d6fbafd4e93903 b/fuzzers/corpora/commit_graph/68fa6dd52832657cb8dd7e1485d6fbafd4e93903 new file mode 100644 index 00000000000..fb966ffe938 Binary files /dev/null and b/fuzzers/corpora/commit_graph/68fa6dd52832657cb8dd7e1485d6fbafd4e93903 differ diff --git a/fuzzers/corpora/commit_graph/691696af1c042115f4d9f9b8e24f7b8c06ed189b b/fuzzers/corpora/commit_graph/691696af1c042115f4d9f9b8e24f7b8c06ed189b new file mode 100644 index 00000000000..e407bd3c785 Binary files /dev/null and b/fuzzers/corpora/commit_graph/691696af1c042115f4d9f9b8e24f7b8c06ed189b differ diff --git a/fuzzers/corpora/commit_graph/6a80152f9b1afa3a3080bf3f6aa48e84c2e18497 b/fuzzers/corpora/commit_graph/6a80152f9b1afa3a3080bf3f6aa48e84c2e18497 new file mode 100644 index 00000000000..a706cb042ac Binary files /dev/null and b/fuzzers/corpora/commit_graph/6a80152f9b1afa3a3080bf3f6aa48e84c2e18497 differ diff --git a/fuzzers/corpora/commit_graph/6af27e4cf4c7bcce128a5949ee27fc73ab2cc71e b/fuzzers/corpora/commit_graph/6af27e4cf4c7bcce128a5949ee27fc73ab2cc71e new file mode 100644 index 00000000000..d0b2fd2d8d7 Binary files /dev/null and b/fuzzers/corpora/commit_graph/6af27e4cf4c7bcce128a5949ee27fc73ab2cc71e differ diff --git a/fuzzers/corpora/commit_graph/6afd8f82d5639b774de0dfd418ae85322f4168dd b/fuzzers/corpora/commit_graph/6afd8f82d5639b774de0dfd418ae85322f4168dd new file mode 100644 index 00000000000..7840c31b1f6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/6afd8f82d5639b774de0dfd418ae85322f4168dd differ diff --git a/fuzzers/corpora/commit_graph/6c64a9e26e0e1480bb5e60b7044ca6ce17104a80 b/fuzzers/corpora/commit_graph/6c64a9e26e0e1480bb5e60b7044ca6ce17104a80 new file mode 100644 index 00000000000..752046b9a63 Binary files /dev/null and b/fuzzers/corpora/commit_graph/6c64a9e26e0e1480bb5e60b7044ca6ce17104a80 differ diff --git a/fuzzers/corpora/commit_graph/6c850c17db130ca0152f7c75562fa191f7ef89de b/fuzzers/corpora/commit_graph/6c850c17db130ca0152f7c75562fa191f7ef89de new file mode 100644 index 00000000000..a7f693ba444 Binary files /dev/null and b/fuzzers/corpora/commit_graph/6c850c17db130ca0152f7c75562fa191f7ef89de differ diff --git a/fuzzers/corpora/commit_graph/6c9afe4527371a2baf33c5e220e4ca21a3207f94 b/fuzzers/corpora/commit_graph/6c9afe4527371a2baf33c5e220e4ca21a3207f94 new file mode 100644 index 00000000000..d7b8ffca05e Binary files /dev/null and b/fuzzers/corpora/commit_graph/6c9afe4527371a2baf33c5e220e4ca21a3207f94 differ diff --git a/fuzzers/corpora/commit_graph/6ce3d40b0225923a7f4123a919b1c5d70841fad7 b/fuzzers/corpora/commit_graph/6ce3d40b0225923a7f4123a919b1c5d70841fad7 new file mode 100644 index 00000000000..3a2836b2cfc Binary files /dev/null and b/fuzzers/corpora/commit_graph/6ce3d40b0225923a7f4123a919b1c5d70841fad7 differ diff --git a/fuzzers/corpora/commit_graph/6cfd064aa6197813eb18f38df967ae4cdba9c6da b/fuzzers/corpora/commit_graph/6cfd064aa6197813eb18f38df967ae4cdba9c6da new file mode 100644 index 00000000000..51778d55c6a Binary files /dev/null and b/fuzzers/corpora/commit_graph/6cfd064aa6197813eb18f38df967ae4cdba9c6da differ diff --git a/fuzzers/corpora/commit_graph/6e6675676c53bcddc870e06605d2432e3429f224 b/fuzzers/corpora/commit_graph/6e6675676c53bcddc870e06605d2432e3429f224 new file mode 100644 index 00000000000..1ddebeff144 Binary files /dev/null and b/fuzzers/corpora/commit_graph/6e6675676c53bcddc870e06605d2432e3429f224 differ diff --git a/fuzzers/corpora/commit_graph/6e6e82579b7abae2b43d90448d3f2ead4dfcba78 b/fuzzers/corpora/commit_graph/6e6e82579b7abae2b43d90448d3f2ead4dfcba78 new file mode 100644 index 00000000000..a528901ac82 Binary files /dev/null and b/fuzzers/corpora/commit_graph/6e6e82579b7abae2b43d90448d3f2ead4dfcba78 differ diff --git a/fuzzers/corpora/commit_graph/6f13d23c75a562eddefafe85e208e602832294e2 b/fuzzers/corpora/commit_graph/6f13d23c75a562eddefafe85e208e602832294e2 new file mode 100644 index 00000000000..49a53932fee Binary files /dev/null and b/fuzzers/corpora/commit_graph/6f13d23c75a562eddefafe85e208e602832294e2 differ diff --git a/fuzzers/corpora/commit_graph/6fed59b0472927f5d2396d0ee4d7fd13579377ce b/fuzzers/corpora/commit_graph/6fed59b0472927f5d2396d0ee4d7fd13579377ce new file mode 100644 index 00000000000..6b3970fbf6f Binary files /dev/null and b/fuzzers/corpora/commit_graph/6fed59b0472927f5d2396d0ee4d7fd13579377ce differ diff --git a/fuzzers/corpora/commit_graph/71f7724196f9f8fcfe3ee0161a84893bb9c4ab11 b/fuzzers/corpora/commit_graph/71f7724196f9f8fcfe3ee0161a84893bb9c4ab11 new file mode 100644 index 00000000000..9e5c8ddb34e Binary files /dev/null and b/fuzzers/corpora/commit_graph/71f7724196f9f8fcfe3ee0161a84893bb9c4ab11 differ diff --git a/fuzzers/corpora/commit_graph/7335ecb1d41e713bf3909adf5802b90e22bc1581 b/fuzzers/corpora/commit_graph/7335ecb1d41e713bf3909adf5802b90e22bc1581 new file mode 100644 index 00000000000..02e2fa6d16e Binary files /dev/null and b/fuzzers/corpora/commit_graph/7335ecb1d41e713bf3909adf5802b90e22bc1581 differ diff --git a/fuzzers/corpora/commit_graph/73afaa73175f461e1d19d5138e055c1649926dfe b/fuzzers/corpora/commit_graph/73afaa73175f461e1d19d5138e055c1649926dfe new file mode 100644 index 00000000000..0f45e0bdb4f Binary files /dev/null and b/fuzzers/corpora/commit_graph/73afaa73175f461e1d19d5138e055c1649926dfe differ diff --git a/fuzzers/corpora/commit_graph/73e2fcb45c4df90d19091056b235e7a317631a62 b/fuzzers/corpora/commit_graph/73e2fcb45c4df90d19091056b235e7a317631a62 new file mode 100644 index 00000000000..549eeb30619 Binary files /dev/null and b/fuzzers/corpora/commit_graph/73e2fcb45c4df90d19091056b235e7a317631a62 differ diff --git a/fuzzers/corpora/commit_graph/741cb2d5ae11b0a9e0608b58ec7284d75129a1f2 b/fuzzers/corpora/commit_graph/741cb2d5ae11b0a9e0608b58ec7284d75129a1f2 new file mode 100644 index 00000000000..a16738d6fc2 Binary files /dev/null and b/fuzzers/corpora/commit_graph/741cb2d5ae11b0a9e0608b58ec7284d75129a1f2 differ diff --git a/fuzzers/corpora/commit_graph/7431bb0097a9bb52e1ceaaa8674a13cd3486a387 b/fuzzers/corpora/commit_graph/7431bb0097a9bb52e1ceaaa8674a13cd3486a387 new file mode 100644 index 00000000000..d681b26cd47 Binary files /dev/null and b/fuzzers/corpora/commit_graph/7431bb0097a9bb52e1ceaaa8674a13cd3486a387 differ diff --git a/fuzzers/corpora/commit_graph/7455b805995d0c96ac12f8a1c1264caaffcfac1c b/fuzzers/corpora/commit_graph/7455b805995d0c96ac12f8a1c1264caaffcfac1c new file mode 100644 index 00000000000..b3d2620ffe1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/7455b805995d0c96ac12f8a1c1264caaffcfac1c differ diff --git a/fuzzers/corpora/commit_graph/74e39b8a82fc06f9ed8f83ea30545ddf6df66811 b/fuzzers/corpora/commit_graph/74e39b8a82fc06f9ed8f83ea30545ddf6df66811 new file mode 100644 index 00000000000..3cc9debcbba Binary files /dev/null and b/fuzzers/corpora/commit_graph/74e39b8a82fc06f9ed8f83ea30545ddf6df66811 differ diff --git a/fuzzers/corpora/commit_graph/75d51e413d3e916560dc0c2ee5092d2f4972aec1 b/fuzzers/corpora/commit_graph/75d51e413d3e916560dc0c2ee5092d2f4972aec1 new file mode 100644 index 00000000000..68f34e7ccf9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/75d51e413d3e916560dc0c2ee5092d2f4972aec1 differ diff --git a/fuzzers/corpora/commit_graph/75e068964ea6beb7310a154d763de74a70071f48 b/fuzzers/corpora/commit_graph/75e068964ea6beb7310a154d763de74a70071f48 new file mode 100644 index 00000000000..a08ee589084 Binary files /dev/null and b/fuzzers/corpora/commit_graph/75e068964ea6beb7310a154d763de74a70071f48 differ diff --git a/fuzzers/corpora/commit_graph/763bf498dd847bd2b4af7b611199619bd428bea6 b/fuzzers/corpora/commit_graph/763bf498dd847bd2b4af7b611199619bd428bea6 new file mode 100644 index 00000000000..4c2893554c9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/763bf498dd847bd2b4af7b611199619bd428bea6 differ diff --git a/fuzzers/corpora/commit_graph/77064ae04581a3c6d2a77158ef1a0b1e60db414a b/fuzzers/corpora/commit_graph/77064ae04581a3c6d2a77158ef1a0b1e60db414a new file mode 100644 index 00000000000..fbd0ca79e88 Binary files /dev/null and b/fuzzers/corpora/commit_graph/77064ae04581a3c6d2a77158ef1a0b1e60db414a differ diff --git a/fuzzers/corpora/commit_graph/783bb14d68021061f592601607f40fe232ad17c4 b/fuzzers/corpora/commit_graph/783bb14d68021061f592601607f40fe232ad17c4 new file mode 100644 index 00000000000..3cfa562ac4a Binary files /dev/null and b/fuzzers/corpora/commit_graph/783bb14d68021061f592601607f40fe232ad17c4 differ diff --git a/fuzzers/corpora/commit_graph/7862814cb684310b54ef920b35403515efaba13c b/fuzzers/corpora/commit_graph/7862814cb684310b54ef920b35403515efaba13c new file mode 100644 index 00000000000..3066626ba54 Binary files /dev/null and b/fuzzers/corpora/commit_graph/7862814cb684310b54ef920b35403515efaba13c differ diff --git a/fuzzers/corpora/commit_graph/791fd85b6ffb2429e9fa5ba29eebdce214ad88c7 b/fuzzers/corpora/commit_graph/791fd85b6ffb2429e9fa5ba29eebdce214ad88c7 new file mode 100644 index 00000000000..2c4739bb815 Binary files /dev/null and b/fuzzers/corpora/commit_graph/791fd85b6ffb2429e9fa5ba29eebdce214ad88c7 differ diff --git a/fuzzers/corpora/commit_graph/79396d4f6142a53e26e14aa6ccb4afb4fd8fc580 b/fuzzers/corpora/commit_graph/79396d4f6142a53e26e14aa6ccb4afb4fd8fc580 new file mode 100644 index 00000000000..ef543420328 Binary files /dev/null and b/fuzzers/corpora/commit_graph/79396d4f6142a53e26e14aa6ccb4afb4fd8fc580 differ diff --git a/fuzzers/corpora/commit_graph/79661b8e529e2182d5c612faba9f26e32a122b78 b/fuzzers/corpora/commit_graph/79661b8e529e2182d5c612faba9f26e32a122b78 new file mode 100644 index 00000000000..75ebe29ce53 Binary files /dev/null and b/fuzzers/corpora/commit_graph/79661b8e529e2182d5c612faba9f26e32a122b78 differ diff --git a/fuzzers/corpora/commit_graph/7969143acb3334bffac46c6dfd96362c81644191 b/fuzzers/corpora/commit_graph/7969143acb3334bffac46c6dfd96362c81644191 new file mode 100644 index 00000000000..6ea2e668187 Binary files /dev/null and b/fuzzers/corpora/commit_graph/7969143acb3334bffac46c6dfd96362c81644191 differ diff --git a/fuzzers/corpora/commit_graph/79d84866dc8c067508c02516b65c0e48cf689b56 b/fuzzers/corpora/commit_graph/79d84866dc8c067508c02516b65c0e48cf689b56 new file mode 100644 index 00000000000..3080da739b1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/79d84866dc8c067508c02516b65c0e48cf689b56 differ diff --git a/fuzzers/corpora/commit_graph/7b61f8f4a96e309bbe64ed82637fc81492a9652f b/fuzzers/corpora/commit_graph/7b61f8f4a96e309bbe64ed82637fc81492a9652f new file mode 100644 index 00000000000..28d93b7a1e1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/7b61f8f4a96e309bbe64ed82637fc81492a9652f differ diff --git a/fuzzers/corpora/commit_graph/7b8123f973edfb0f3cab027c0cd6b8efc7b11d6b b/fuzzers/corpora/commit_graph/7b8123f973edfb0f3cab027c0cd6b8efc7b11d6b new file mode 100644 index 00000000000..1c6699bc404 Binary files /dev/null and b/fuzzers/corpora/commit_graph/7b8123f973edfb0f3cab027c0cd6b8efc7b11d6b differ diff --git a/fuzzers/corpora/commit_graph/7b8dd3093efba07f7a4d3bab4b90b8f6e4f28bfb b/fuzzers/corpora/commit_graph/7b8dd3093efba07f7a4d3bab4b90b8f6e4f28bfb new file mode 100644 index 00000000000..3e686e9d55c Binary files /dev/null and b/fuzzers/corpora/commit_graph/7b8dd3093efba07f7a4d3bab4b90b8f6e4f28bfb differ diff --git a/fuzzers/corpora/commit_graph/7cc771aab0f3be7730881a46d952ae0a06958201 b/fuzzers/corpora/commit_graph/7cc771aab0f3be7730881a46d952ae0a06958201 new file mode 100644 index 00000000000..ba94bfe7dda Binary files /dev/null and b/fuzzers/corpora/commit_graph/7cc771aab0f3be7730881a46d952ae0a06958201 differ diff --git a/fuzzers/corpora/commit_graph/7d177f4207de78d50df2493a3bc07f2cd578b363 b/fuzzers/corpora/commit_graph/7d177f4207de78d50df2493a3bc07f2cd578b363 new file mode 100644 index 00000000000..a936354f3a2 Binary files /dev/null and b/fuzzers/corpora/commit_graph/7d177f4207de78d50df2493a3bc07f2cd578b363 differ diff --git a/fuzzers/corpora/commit_graph/7d2df075f3e73ea9809c31586c37ece0f568b7fa b/fuzzers/corpora/commit_graph/7d2df075f3e73ea9809c31586c37ece0f568b7fa new file mode 100644 index 00000000000..897276b6763 Binary files /dev/null and b/fuzzers/corpora/commit_graph/7d2df075f3e73ea9809c31586c37ece0f568b7fa differ diff --git a/fuzzers/corpora/commit_graph/7d386e68e4c733a1fb11c0117f379fb4b9955fbb b/fuzzers/corpora/commit_graph/7d386e68e4c733a1fb11c0117f379fb4b9955fbb new file mode 100644 index 00000000000..f4f4d75bd85 --- /dev/null +++ b/fuzzers/corpora/commit_graph/7d386e68e4c733a1fb11c0117f379fb4b9955fbb @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/commit_graph/7e4260830352479d29310bd6e1022e19a68ffe76 b/fuzzers/corpora/commit_graph/7e4260830352479d29310bd6e1022e19a68ffe76 new file mode 100644 index 00000000000..f478389ffaa Binary files /dev/null and b/fuzzers/corpora/commit_graph/7e4260830352479d29310bd6e1022e19a68ffe76 differ diff --git a/fuzzers/corpora/commit_graph/7e4dfdae52be18cf95555c2eb1f54af7f69c6dde b/fuzzers/corpora/commit_graph/7e4dfdae52be18cf95555c2eb1f54af7f69c6dde new file mode 100644 index 00000000000..0f6d9768432 Binary files /dev/null and b/fuzzers/corpora/commit_graph/7e4dfdae52be18cf95555c2eb1f54af7f69c6dde differ diff --git a/fuzzers/corpora/commit_graph/7eafedf7e7f20e86ecdf9ba51febf8492bdbc1f1 b/fuzzers/corpora/commit_graph/7eafedf7e7f20e86ecdf9ba51febf8492bdbc1f1 new file mode 100644 index 00000000000..ae5f7c56cde Binary files /dev/null and b/fuzzers/corpora/commit_graph/7eafedf7e7f20e86ecdf9ba51febf8492bdbc1f1 differ diff --git a/fuzzers/corpora/commit_graph/7ef1829a378d66b1dd70a767729127a0dc5edcae b/fuzzers/corpora/commit_graph/7ef1829a378d66b1dd70a767729127a0dc5edcae new file mode 100644 index 00000000000..e5d73559175 Binary files /dev/null and b/fuzzers/corpora/commit_graph/7ef1829a378d66b1dd70a767729127a0dc5edcae differ diff --git a/fuzzers/corpora/commit_graph/80b7d2b9d7e8c8fd7ae239b8d307b592f97ee000 b/fuzzers/corpora/commit_graph/80b7d2b9d7e8c8fd7ae239b8d307b592f97ee000 new file mode 100644 index 00000000000..58ddc12d3df Binary files /dev/null and b/fuzzers/corpora/commit_graph/80b7d2b9d7e8c8fd7ae239b8d307b592f97ee000 differ diff --git a/fuzzers/corpora/commit_graph/810f577ff5c1af7807a26226af912687558158cd b/fuzzers/corpora/commit_graph/810f577ff5c1af7807a26226af912687558158cd new file mode 100644 index 00000000000..be8be9c089a Binary files /dev/null and b/fuzzers/corpora/commit_graph/810f577ff5c1af7807a26226af912687558158cd differ diff --git a/fuzzers/corpora/commit_graph/81603f1fe8d8e29005418d0fc9a9b33972366038 b/fuzzers/corpora/commit_graph/81603f1fe8d8e29005418d0fc9a9b33972366038 new file mode 100644 index 00000000000..d9cb488e0d9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/81603f1fe8d8e29005418d0fc9a9b33972366038 differ diff --git a/fuzzers/corpora/commit_graph/81c8b4d6884f954935fa4a8e828c4637db04b61a b/fuzzers/corpora/commit_graph/81c8b4d6884f954935fa4a8e828c4637db04b61a new file mode 100644 index 00000000000..47ae17be319 Binary files /dev/null and b/fuzzers/corpora/commit_graph/81c8b4d6884f954935fa4a8e828c4637db04b61a differ diff --git a/fuzzers/corpora/commit_graph/8226846e9b092561f85cc2956ab89d8cc1ae61e0 b/fuzzers/corpora/commit_graph/8226846e9b092561f85cc2956ab89d8cc1ae61e0 new file mode 100644 index 00000000000..80d1cd91c53 Binary files /dev/null and b/fuzzers/corpora/commit_graph/8226846e9b092561f85cc2956ab89d8cc1ae61e0 differ diff --git a/fuzzers/corpora/commit_graph/825cfceea434e2392cce161356e3cb5f81ec2b3a b/fuzzers/corpora/commit_graph/825cfceea434e2392cce161356e3cb5f81ec2b3a new file mode 100644 index 00000000000..01f87eda73b Binary files /dev/null and b/fuzzers/corpora/commit_graph/825cfceea434e2392cce161356e3cb5f81ec2b3a differ diff --git a/fuzzers/corpora/commit_graph/82603febce83d95adf68b85cabf15d43ca0c4ee9 b/fuzzers/corpora/commit_graph/82603febce83d95adf68b85cabf15d43ca0c4ee9 new file mode 100644 index 00000000000..d828b05afca Binary files /dev/null and b/fuzzers/corpora/commit_graph/82603febce83d95adf68b85cabf15d43ca0c4ee9 differ diff --git a/fuzzers/corpora/commit_graph/827f0826cc4156e19b4c4938bec74e38de62fe9c b/fuzzers/corpora/commit_graph/827f0826cc4156e19b4c4938bec74e38de62fe9c new file mode 100644 index 00000000000..a391d62a97f Binary files /dev/null and b/fuzzers/corpora/commit_graph/827f0826cc4156e19b4c4938bec74e38de62fe9c differ diff --git a/fuzzers/corpora/commit_graph/8486397ff8d1156249676c19b419a7758ff53f9a b/fuzzers/corpora/commit_graph/8486397ff8d1156249676c19b419a7758ff53f9a new file mode 100644 index 00000000000..dac9bbe4cee Binary files /dev/null and b/fuzzers/corpora/commit_graph/8486397ff8d1156249676c19b419a7758ff53f9a differ diff --git a/fuzzers/corpora/commit_graph/84d99ee359bec1b8ee0f59e9bd96f1da062030b7 b/fuzzers/corpora/commit_graph/84d99ee359bec1b8ee0f59e9bd96f1da062030b7 new file mode 100644 index 00000000000..a2e7cf305f5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/84d99ee359bec1b8ee0f59e9bd96f1da062030b7 differ diff --git a/fuzzers/corpora/commit_graph/84e629bc7416039f1feb81fa9168d7c1ee3141c2 b/fuzzers/corpora/commit_graph/84e629bc7416039f1feb81fa9168d7c1ee3141c2 new file mode 100644 index 00000000000..8ae4e395cb8 Binary files /dev/null and b/fuzzers/corpora/commit_graph/84e629bc7416039f1feb81fa9168d7c1ee3141c2 differ diff --git a/fuzzers/corpora/commit_graph/84e885752179076fb38739ca7bc4345716bee56a b/fuzzers/corpora/commit_graph/84e885752179076fb38739ca7bc4345716bee56a new file mode 100644 index 00000000000..dd0c42692e1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/84e885752179076fb38739ca7bc4345716bee56a differ diff --git a/fuzzers/corpora/commit_graph/859ef05494c8070057810b5c20df00fc81f81cf5 b/fuzzers/corpora/commit_graph/859ef05494c8070057810b5c20df00fc81f81cf5 new file mode 100644 index 00000000000..2e3f2990047 Binary files /dev/null and b/fuzzers/corpora/commit_graph/859ef05494c8070057810b5c20df00fc81f81cf5 differ diff --git a/fuzzers/corpora/commit_graph/859fe592f33abc1d959c0e73ecd6cd4bffe23a97 b/fuzzers/corpora/commit_graph/859fe592f33abc1d959c0e73ecd6cd4bffe23a97 new file mode 100644 index 00000000000..5289c8e1cf5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/859fe592f33abc1d959c0e73ecd6cd4bffe23a97 differ diff --git a/fuzzers/corpora/commit_graph/860da5e8a468805b76a44b9ac99b4575be16ea15 b/fuzzers/corpora/commit_graph/860da5e8a468805b76a44b9ac99b4575be16ea15 new file mode 100644 index 00000000000..c8b03da24aa Binary files /dev/null and b/fuzzers/corpora/commit_graph/860da5e8a468805b76a44b9ac99b4575be16ea15 differ diff --git a/fuzzers/corpora/commit_graph/865e415745cead02a826f058a5ee49099bdf9562 b/fuzzers/corpora/commit_graph/865e415745cead02a826f058a5ee49099bdf9562 new file mode 100644 index 00000000000..a4ce6588338 Binary files /dev/null and b/fuzzers/corpora/commit_graph/865e415745cead02a826f058a5ee49099bdf9562 differ diff --git a/fuzzers/corpora/commit_graph/878bfce051a9c7462847d4e99b7e926dc821b7b8 b/fuzzers/corpora/commit_graph/878bfce051a9c7462847d4e99b7e926dc821b7b8 new file mode 100644 index 00000000000..efb0bc5514e Binary files /dev/null and b/fuzzers/corpora/commit_graph/878bfce051a9c7462847d4e99b7e926dc821b7b8 differ diff --git a/fuzzers/corpora/commit_graph/880492e4dc7259577c227bb4f075d7165e875c29 b/fuzzers/corpora/commit_graph/880492e4dc7259577c227bb4f075d7165e875c29 new file mode 100644 index 00000000000..c977dffaf90 Binary files /dev/null and b/fuzzers/corpora/commit_graph/880492e4dc7259577c227bb4f075d7165e875c29 differ diff --git a/fuzzers/corpora/commit_graph/88b7de1bd1c96454a1350286d115c0ee368511f9 b/fuzzers/corpora/commit_graph/88b7de1bd1c96454a1350286d115c0ee368511f9 new file mode 100644 index 00000000000..7261eec17cb Binary files /dev/null and b/fuzzers/corpora/commit_graph/88b7de1bd1c96454a1350286d115c0ee368511f9 differ diff --git a/fuzzers/corpora/commit_graph/896268e4a5775b7ce33923ac6daeb0810420c55b b/fuzzers/corpora/commit_graph/896268e4a5775b7ce33923ac6daeb0810420c55b new file mode 100644 index 00000000000..0897bdad026 Binary files /dev/null and b/fuzzers/corpora/commit_graph/896268e4a5775b7ce33923ac6daeb0810420c55b differ diff --git a/fuzzers/corpora/commit_graph/8978f8da89f9652878edabad164f5513ef508f27 b/fuzzers/corpora/commit_graph/8978f8da89f9652878edabad164f5513ef508f27 new file mode 100644 index 00000000000..0dd9ba49276 Binary files /dev/null and b/fuzzers/corpora/commit_graph/8978f8da89f9652878edabad164f5513ef508f27 differ diff --git a/fuzzers/corpora/commit_graph/89a6525b7db0e6ec211a484efd2880abef928d4e b/fuzzers/corpora/commit_graph/89a6525b7db0e6ec211a484efd2880abef928d4e new file mode 100644 index 00000000000..e5dd1cdf809 Binary files /dev/null and b/fuzzers/corpora/commit_graph/89a6525b7db0e6ec211a484efd2880abef928d4e differ diff --git a/fuzzers/corpora/commit_graph/8ae86cba2bba6664fc5eb97be8e9777b8825d823 b/fuzzers/corpora/commit_graph/8ae86cba2bba6664fc5eb97be8e9777b8825d823 new file mode 100644 index 00000000000..04a4aa532dd Binary files /dev/null and b/fuzzers/corpora/commit_graph/8ae86cba2bba6664fc5eb97be8e9777b8825d823 differ diff --git a/fuzzers/corpora/commit_graph/8b845fbd2aa14e4f83c4dbc8b4b0b54d06482acd b/fuzzers/corpora/commit_graph/8b845fbd2aa14e4f83c4dbc8b4b0b54d06482acd new file mode 100644 index 00000000000..c711793c8fc Binary files /dev/null and b/fuzzers/corpora/commit_graph/8b845fbd2aa14e4f83c4dbc8b4b0b54d06482acd differ diff --git a/fuzzers/corpora/commit_graph/8c4121e6ce5956cfa408b980f16d276f456374dc b/fuzzers/corpora/commit_graph/8c4121e6ce5956cfa408b980f16d276f456374dc new file mode 100644 index 00000000000..1ba18917fe6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/8c4121e6ce5956cfa408b980f16d276f456374dc differ diff --git a/fuzzers/corpora/commit_graph/8cb6a5b8ab41e3d27668d5735b5c09ff1f2eab65 b/fuzzers/corpora/commit_graph/8cb6a5b8ab41e3d27668d5735b5c09ff1f2eab65 new file mode 100644 index 00000000000..e23d11256a1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/8cb6a5b8ab41e3d27668d5735b5c09ff1f2eab65 differ diff --git a/fuzzers/corpora/commit_graph/8d80a70ffd362a89b88663e27f11e8ab69b70c1b b/fuzzers/corpora/commit_graph/8d80a70ffd362a89b88663e27f11e8ab69b70c1b new file mode 100644 index 00000000000..02f765ea284 Binary files /dev/null and b/fuzzers/corpora/commit_graph/8d80a70ffd362a89b88663e27f11e8ab69b70c1b differ diff --git a/fuzzers/corpora/commit_graph/8db603c1720b3680047f831f2ea9862567a7cdc4 b/fuzzers/corpora/commit_graph/8db603c1720b3680047f831f2ea9862567a7cdc4 new file mode 100644 index 00000000000..d02cb03541a Binary files /dev/null and b/fuzzers/corpora/commit_graph/8db603c1720b3680047f831f2ea9862567a7cdc4 differ diff --git a/fuzzers/corpora/commit_graph/8dd40b2d27c7dd4b986c35d87f826da287c09c4c b/fuzzers/corpora/commit_graph/8dd40b2d27c7dd4b986c35d87f826da287c09c4c new file mode 100644 index 00000000000..11aa235b17a Binary files /dev/null and b/fuzzers/corpora/commit_graph/8dd40b2d27c7dd4b986c35d87f826da287c09c4c differ diff --git a/fuzzers/corpora/commit_graph/8e9d6e6408e5f708a1924e8370e687e2c202a4c4 b/fuzzers/corpora/commit_graph/8e9d6e6408e5f708a1924e8370e687e2c202a4c4 new file mode 100644 index 00000000000..cbeb46b1750 Binary files /dev/null and b/fuzzers/corpora/commit_graph/8e9d6e6408e5f708a1924e8370e687e2c202a4c4 differ diff --git a/fuzzers/corpora/commit_graph/8f2dff1a30ee28e5985cb9379828aea5658d5849 b/fuzzers/corpora/commit_graph/8f2dff1a30ee28e5985cb9379828aea5658d5849 new file mode 100644 index 00000000000..b7220c3cbca Binary files /dev/null and b/fuzzers/corpora/commit_graph/8f2dff1a30ee28e5985cb9379828aea5658d5849 differ diff --git a/fuzzers/corpora/commit_graph/8f7d18cdd6e605b85784ada14571fd5e5a184f2a b/fuzzers/corpora/commit_graph/8f7d18cdd6e605b85784ada14571fd5e5a184f2a new file mode 100644 index 00000000000..80fe175ba1c Binary files /dev/null and b/fuzzers/corpora/commit_graph/8f7d18cdd6e605b85784ada14571fd5e5a184f2a differ diff --git a/fuzzers/corpora/commit_graph/903ae52f0ac9af8348038b12f9259741b0de42f1 b/fuzzers/corpora/commit_graph/903ae52f0ac9af8348038b12f9259741b0de42f1 new file mode 100644 index 00000000000..da8c2209c62 Binary files /dev/null and b/fuzzers/corpora/commit_graph/903ae52f0ac9af8348038b12f9259741b0de42f1 differ diff --git a/fuzzers/corpora/commit_graph/9119e331f59e9337d419739c324f49d1bd62c8bf b/fuzzers/corpora/commit_graph/9119e331f59e9337d419739c324f49d1bd62c8bf new file mode 100644 index 00000000000..fc3a6963cd2 Binary files /dev/null and b/fuzzers/corpora/commit_graph/9119e331f59e9337d419739c324f49d1bd62c8bf differ diff --git a/fuzzers/corpora/commit_graph/91d54d03b0917314ea1d67a70690df9247dd08d2 b/fuzzers/corpora/commit_graph/91d54d03b0917314ea1d67a70690df9247dd08d2 new file mode 100644 index 00000000000..290da4da4e7 Binary files /dev/null and b/fuzzers/corpora/commit_graph/91d54d03b0917314ea1d67a70690df9247dd08d2 differ diff --git a/fuzzers/corpora/commit_graph/922da3b96725bfd0e3f6ce119f1e2249d53f9086 b/fuzzers/corpora/commit_graph/922da3b96725bfd0e3f6ce119f1e2249d53f9086 new file mode 100644 index 00000000000..c3bb009f399 Binary files /dev/null and b/fuzzers/corpora/commit_graph/922da3b96725bfd0e3f6ce119f1e2249d53f9086 differ diff --git a/fuzzers/corpora/commit_graph/9277561e0524cccba2f851970b0d88ec4f4d3f5e b/fuzzers/corpora/commit_graph/9277561e0524cccba2f851970b0d88ec4f4d3f5e new file mode 100644 index 00000000000..07d1da19be7 Binary files /dev/null and b/fuzzers/corpora/commit_graph/9277561e0524cccba2f851970b0d88ec4f4d3f5e differ diff --git a/fuzzers/corpora/commit_graph/92a4d571804026b7bbe957396185e079e756b894 b/fuzzers/corpora/commit_graph/92a4d571804026b7bbe957396185e079e756b894 new file mode 100644 index 00000000000..63b98b3b43f Binary files /dev/null and b/fuzzers/corpora/commit_graph/92a4d571804026b7bbe957396185e079e756b894 differ diff --git a/fuzzers/corpora/commit_graph/931224cc80168fd362a360d99bab813ed7bbf8ce b/fuzzers/corpora/commit_graph/931224cc80168fd362a360d99bab813ed7bbf8ce new file mode 100644 index 00000000000..df27b369676 Binary files /dev/null and b/fuzzers/corpora/commit_graph/931224cc80168fd362a360d99bab813ed7bbf8ce differ diff --git a/fuzzers/corpora/commit_graph/936ea5dad3bf023c552aa0bbeea8f7f66a11612c b/fuzzers/corpora/commit_graph/936ea5dad3bf023c552aa0bbeea8f7f66a11612c new file mode 100644 index 00000000000..847b537acb1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/936ea5dad3bf023c552aa0bbeea8f7f66a11612c differ diff --git a/fuzzers/corpora/commit_graph/93aa4e0b1864933dce0abc0df69fe3d261f117f2 b/fuzzers/corpora/commit_graph/93aa4e0b1864933dce0abc0df69fe3d261f117f2 new file mode 100644 index 00000000000..ac830ca5fd1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/93aa4e0b1864933dce0abc0df69fe3d261f117f2 differ diff --git a/fuzzers/corpora/commit_graph/93d5b084965cf1b09085c4079a972e25207b3659 b/fuzzers/corpora/commit_graph/93d5b084965cf1b09085c4079a972e25207b3659 new file mode 100644 index 00000000000..02a711a984c Binary files /dev/null and b/fuzzers/corpora/commit_graph/93d5b084965cf1b09085c4079a972e25207b3659 differ diff --git a/fuzzers/corpora/commit_graph/9443fd3468bcc0bc3ff8dfe765225f045ab43d0a b/fuzzers/corpora/commit_graph/9443fd3468bcc0bc3ff8dfe765225f045ab43d0a new file mode 100644 index 00000000000..d1667ca74a9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/9443fd3468bcc0bc3ff8dfe765225f045ab43d0a differ diff --git a/fuzzers/corpora/commit_graph/9624c26cefb5804b7906147d262e81ee4000b6d6 b/fuzzers/corpora/commit_graph/9624c26cefb5804b7906147d262e81ee4000b6d6 new file mode 100644 index 00000000000..4c1883146bd Binary files /dev/null and b/fuzzers/corpora/commit_graph/9624c26cefb5804b7906147d262e81ee4000b6d6 differ diff --git a/fuzzers/corpora/commit_graph/9890933a73f39208627bd36e2fe88a6d54343a74 b/fuzzers/corpora/commit_graph/9890933a73f39208627bd36e2fe88a6d54343a74 new file mode 100644 index 00000000000..dfbb39dde1b Binary files /dev/null and b/fuzzers/corpora/commit_graph/9890933a73f39208627bd36e2fe88a6d54343a74 differ diff --git a/fuzzers/corpora/commit_graph/989dad0448e79af10040d5080f74eba2b8a401ba b/fuzzers/corpora/commit_graph/989dad0448e79af10040d5080f74eba2b8a401ba new file mode 100644 index 00000000000..202bbd65bcd Binary files /dev/null and b/fuzzers/corpora/commit_graph/989dad0448e79af10040d5080f74eba2b8a401ba differ diff --git a/fuzzers/corpora/commit_graph/98ed4808b4a8da66a91fcea1be63be6371a7c7ac b/fuzzers/corpora/commit_graph/98ed4808b4a8da66a91fcea1be63be6371a7c7ac new file mode 100644 index 00000000000..b06a1674e6d Binary files /dev/null and b/fuzzers/corpora/commit_graph/98ed4808b4a8da66a91fcea1be63be6371a7c7ac differ diff --git a/fuzzers/corpora/commit_graph/9928e516b85e22fbad58d562d3b7e814d9ce812d b/fuzzers/corpora/commit_graph/9928e516b85e22fbad58d562d3b7e814d9ce812d new file mode 100644 index 00000000000..d1e5a041222 Binary files /dev/null and b/fuzzers/corpora/commit_graph/9928e516b85e22fbad58d562d3b7e814d9ce812d differ diff --git a/fuzzers/corpora/commit_graph/994c7cc5599252b5628d89cd0ba4b5574d32bf00 b/fuzzers/corpora/commit_graph/994c7cc5599252b5628d89cd0ba4b5574d32bf00 new file mode 100644 index 00000000000..d51d00d0639 Binary files /dev/null and b/fuzzers/corpora/commit_graph/994c7cc5599252b5628d89cd0ba4b5574d32bf00 differ diff --git a/fuzzers/corpora/commit_graph/99c8557c2a02ea030de42869af42c1f7c77114db b/fuzzers/corpora/commit_graph/99c8557c2a02ea030de42869af42c1f7c77114db new file mode 100644 index 00000000000..18f5349b66a Binary files /dev/null and b/fuzzers/corpora/commit_graph/99c8557c2a02ea030de42869af42c1f7c77114db differ diff --git a/fuzzers/corpora/commit_graph/9a14c867272f102b84efdba73662d318c3e51cfe b/fuzzers/corpora/commit_graph/9a14c867272f102b84efdba73662d318c3e51cfe new file mode 100644 index 00000000000..fc473b2b738 Binary files /dev/null and b/fuzzers/corpora/commit_graph/9a14c867272f102b84efdba73662d318c3e51cfe differ diff --git a/fuzzers/corpora/commit_graph/9a6f158c176d4a1982d541be2bc27a8afba4ea57 b/fuzzers/corpora/commit_graph/9a6f158c176d4a1982d541be2bc27a8afba4ea57 new file mode 100644 index 00000000000..b99bc20328c Binary files /dev/null and b/fuzzers/corpora/commit_graph/9a6f158c176d4a1982d541be2bc27a8afba4ea57 differ diff --git a/fuzzers/corpora/commit_graph/9aa4af603192823a2fdc53d95ed36896bc3309b2 b/fuzzers/corpora/commit_graph/9aa4af603192823a2fdc53d95ed36896bc3309b2 new file mode 100644 index 00000000000..c36e298fc1e Binary files /dev/null and b/fuzzers/corpora/commit_graph/9aa4af603192823a2fdc53d95ed36896bc3309b2 differ diff --git a/fuzzers/corpora/commit_graph/9b40c2190123cec66af3b37212f6c567869efda3 b/fuzzers/corpora/commit_graph/9b40c2190123cec66af3b37212f6c567869efda3 new file mode 100644 index 00000000000..16623249243 Binary files /dev/null and b/fuzzers/corpora/commit_graph/9b40c2190123cec66af3b37212f6c567869efda3 differ diff --git a/fuzzers/corpora/commit_graph/9b6268c11d78c35db5164f1346905e602b6a49fe b/fuzzers/corpora/commit_graph/9b6268c11d78c35db5164f1346905e602b6a49fe new file mode 100644 index 00000000000..4106e203752 Binary files /dev/null and b/fuzzers/corpora/commit_graph/9b6268c11d78c35db5164f1346905e602b6a49fe differ diff --git a/fuzzers/corpora/commit_graph/9c6883ba5cedb7d711b12733d66ef1a1156dd0af b/fuzzers/corpora/commit_graph/9c6883ba5cedb7d711b12733d66ef1a1156dd0af new file mode 100644 index 00000000000..84a193c4592 Binary files /dev/null and b/fuzzers/corpora/commit_graph/9c6883ba5cedb7d711b12733d66ef1a1156dd0af differ diff --git a/fuzzers/corpora/commit_graph/9c85c90f44b454ce0d52882c447f5ecb8d303634 b/fuzzers/corpora/commit_graph/9c85c90f44b454ce0d52882c447f5ecb8d303634 new file mode 100644 index 00000000000..e17438bf8f6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/9c85c90f44b454ce0d52882c447f5ecb8d303634 differ diff --git a/fuzzers/corpora/commit_graph/9cb7a2e89ec636da3fd41ecc49ebe25e5344e2c6 b/fuzzers/corpora/commit_graph/9cb7a2e89ec636da3fd41ecc49ebe25e5344e2c6 new file mode 100644 index 00000000000..55f76fd223a Binary files /dev/null and b/fuzzers/corpora/commit_graph/9cb7a2e89ec636da3fd41ecc49ebe25e5344e2c6 differ diff --git a/fuzzers/corpora/commit_graph/9d912dc5a3497e4b5b40b37202fc0ffbf5263666 b/fuzzers/corpora/commit_graph/9d912dc5a3497e4b5b40b37202fc0ffbf5263666 new file mode 100644 index 00000000000..c71c31afcc8 Binary files /dev/null and b/fuzzers/corpora/commit_graph/9d912dc5a3497e4b5b40b37202fc0ffbf5263666 differ diff --git a/fuzzers/corpora/commit_graph/9dcbafe8c5345194ee0ce7eb4f6efaeb55543626 b/fuzzers/corpora/commit_graph/9dcbafe8c5345194ee0ce7eb4f6efaeb55543626 new file mode 100644 index 00000000000..42c5e7d3a41 Binary files /dev/null and b/fuzzers/corpora/commit_graph/9dcbafe8c5345194ee0ce7eb4f6efaeb55543626 differ diff --git a/fuzzers/corpora/commit_graph/9f4b0f3d2d25e6405ba6093f24d0605327711573 b/fuzzers/corpora/commit_graph/9f4b0f3d2d25e6405ba6093f24d0605327711573 new file mode 100644 index 00000000000..0dc94be849f Binary files /dev/null and b/fuzzers/corpora/commit_graph/9f4b0f3d2d25e6405ba6093f24d0605327711573 differ diff --git a/fuzzers/corpora/commit_graph/a047bf683239fa208dbac09424b105820ac23f43 b/fuzzers/corpora/commit_graph/a047bf683239fa208dbac09424b105820ac23f43 new file mode 100644 index 00000000000..925c86c6d57 Binary files /dev/null and b/fuzzers/corpora/commit_graph/a047bf683239fa208dbac09424b105820ac23f43 differ diff --git a/fuzzers/corpora/commit_graph/a1379dcd89ef5e73eabbfcc395113e3636e0ae09 b/fuzzers/corpora/commit_graph/a1379dcd89ef5e73eabbfcc395113e3636e0ae09 new file mode 100644 index 00000000000..43d31b6d230 Binary files /dev/null and b/fuzzers/corpora/commit_graph/a1379dcd89ef5e73eabbfcc395113e3636e0ae09 differ diff --git a/fuzzers/corpora/commit_graph/a38c7ef56adabd0916abac514154b1f362d40434 b/fuzzers/corpora/commit_graph/a38c7ef56adabd0916abac514154b1f362d40434 new file mode 100644 index 00000000000..944b4ea50a0 Binary files /dev/null and b/fuzzers/corpora/commit_graph/a38c7ef56adabd0916abac514154b1f362d40434 differ diff --git a/fuzzers/corpora/commit_graph/a38ec6ad4a8466b4feb88e67b16524e8f3feac64 b/fuzzers/corpora/commit_graph/a38ec6ad4a8466b4feb88e67b16524e8f3feac64 new file mode 100644 index 00000000000..a9d1adbb367 Binary files /dev/null and b/fuzzers/corpora/commit_graph/a38ec6ad4a8466b4feb88e67b16524e8f3feac64 differ diff --git a/fuzzers/corpora/commit_graph/a3fdea21020268b3b2409c1115d50697d9ae8f8c b/fuzzers/corpora/commit_graph/a3fdea21020268b3b2409c1115d50697d9ae8f8c new file mode 100644 index 00000000000..8d4f934fac6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/a3fdea21020268b3b2409c1115d50697d9ae8f8c differ diff --git a/fuzzers/corpora/commit_graph/a45f1987a444b2c27e90fc1477e8b0815f75383f b/fuzzers/corpora/commit_graph/a45f1987a444b2c27e90fc1477e8b0815f75383f new file mode 100644 index 00000000000..d211a8b1f5f Binary files /dev/null and b/fuzzers/corpora/commit_graph/a45f1987a444b2c27e90fc1477e8b0815f75383f differ diff --git a/fuzzers/corpora/commit_graph/a4682958fb7029384c0a01a4a1356ac6f2f44fe1 b/fuzzers/corpora/commit_graph/a4682958fb7029384c0a01a4a1356ac6f2f44fe1 new file mode 100644 index 00000000000..e8f66dc88d1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/a4682958fb7029384c0a01a4a1356ac6f2f44fe1 differ diff --git a/fuzzers/corpora/commit_graph/a4de41561725960d6f48f210a4fb74d527f7b0c2 b/fuzzers/corpora/commit_graph/a4de41561725960d6f48f210a4fb74d527f7b0c2 new file mode 100644 index 00000000000..ac4c41c9cae Binary files /dev/null and b/fuzzers/corpora/commit_graph/a4de41561725960d6f48f210a4fb74d527f7b0c2 differ diff --git a/fuzzers/corpora/commit_graph/a5935f34435ecdd6587ad4f77b20d479d3387dbe b/fuzzers/corpora/commit_graph/a5935f34435ecdd6587ad4f77b20d479d3387dbe new file mode 100644 index 00000000000..2e00f596bfd Binary files /dev/null and b/fuzzers/corpora/commit_graph/a5935f34435ecdd6587ad4f77b20d479d3387dbe differ diff --git a/fuzzers/corpora/commit_graph/a5b394beb2b1d463ad80924a8c8c70584bf5c629 b/fuzzers/corpora/commit_graph/a5b394beb2b1d463ad80924a8c8c70584bf5c629 new file mode 100644 index 00000000000..eb8f700522f Binary files /dev/null and b/fuzzers/corpora/commit_graph/a5b394beb2b1d463ad80924a8c8c70584bf5c629 differ diff --git a/fuzzers/corpora/commit_graph/a62bc806f8c98ba7986243c2185a0548a8dd57ef b/fuzzers/corpora/commit_graph/a62bc806f8c98ba7986243c2185a0548a8dd57ef new file mode 100644 index 00000000000..fb30d9e49ef Binary files /dev/null and b/fuzzers/corpora/commit_graph/a62bc806f8c98ba7986243c2185a0548a8dd57ef differ diff --git a/fuzzers/corpora/commit_graph/a7013e97948893e0118c686c06e332cc611bea7e b/fuzzers/corpora/commit_graph/a7013e97948893e0118c686c06e332cc611bea7e new file mode 100644 index 00000000000..ab501138e40 Binary files /dev/null and b/fuzzers/corpora/commit_graph/a7013e97948893e0118c686c06e332cc611bea7e differ diff --git a/fuzzers/corpora/commit_graph/a74f5df8c7f25c37c15c0f74ed50019d17338225 b/fuzzers/corpora/commit_graph/a74f5df8c7f25c37c15c0f74ed50019d17338225 new file mode 100644 index 00000000000..b234c15833b Binary files /dev/null and b/fuzzers/corpora/commit_graph/a74f5df8c7f25c37c15c0f74ed50019d17338225 differ diff --git a/fuzzers/corpora/commit_graph/a7ab3559fb3da3f027e67091116253f3bdfd7828 b/fuzzers/corpora/commit_graph/a7ab3559fb3da3f027e67091116253f3bdfd7828 new file mode 100644 index 00000000000..838337a5d9b Binary files /dev/null and b/fuzzers/corpora/commit_graph/a7ab3559fb3da3f027e67091116253f3bdfd7828 differ diff --git a/fuzzers/corpora/commit_graph/a845c8258a02022d447ea9249788b345f5504648 b/fuzzers/corpora/commit_graph/a845c8258a02022d447ea9249788b345f5504648 new file mode 100644 index 00000000000..78311d95fbf Binary files /dev/null and b/fuzzers/corpora/commit_graph/a845c8258a02022d447ea9249788b345f5504648 differ diff --git a/fuzzers/corpora/commit_graph/a8d3e026e2393587eb170afb32e94ff0e1f8a8be b/fuzzers/corpora/commit_graph/a8d3e026e2393587eb170afb32e94ff0e1f8a8be new file mode 100644 index 00000000000..54457f3e5e9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/a8d3e026e2393587eb170afb32e94ff0e1f8a8be differ diff --git a/fuzzers/corpora/commit_graph/a8d547e41ee21e163e65cf0a186d469dfa50ec19 b/fuzzers/corpora/commit_graph/a8d547e41ee21e163e65cf0a186d469dfa50ec19 new file mode 100644 index 00000000000..52d265bcdb6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/a8d547e41ee21e163e65cf0a186d469dfa50ec19 differ diff --git a/fuzzers/corpora/commit_graph/a8fa22521dd6813e595cc0a9586ee71fff305fe2 b/fuzzers/corpora/commit_graph/a8fa22521dd6813e595cc0a9586ee71fff305fe2 new file mode 100644 index 00000000000..5f9099585ca Binary files /dev/null and b/fuzzers/corpora/commit_graph/a8fa22521dd6813e595cc0a9586ee71fff305fe2 differ diff --git a/fuzzers/corpora/commit_graph/a9969442d585d9a53259c71c73b095701280eac5 b/fuzzers/corpora/commit_graph/a9969442d585d9a53259c71c73b095701280eac5 new file mode 100644 index 00000000000..f75792824ff Binary files /dev/null and b/fuzzers/corpora/commit_graph/a9969442d585d9a53259c71c73b095701280eac5 differ diff --git a/fuzzers/corpora/commit_graph/a99789d0ce2d7b937aaa8afa3cfc0f4ccd7be95f b/fuzzers/corpora/commit_graph/a99789d0ce2d7b937aaa8afa3cfc0f4ccd7be95f new file mode 100644 index 00000000000..471add50a45 Binary files /dev/null and b/fuzzers/corpora/commit_graph/a99789d0ce2d7b937aaa8afa3cfc0f4ccd7be95f differ diff --git a/fuzzers/corpora/commit_graph/aaca30ee3ab38edfa2b061fcbcbca0c0ea657f15 b/fuzzers/corpora/commit_graph/aaca30ee3ab38edfa2b061fcbcbca0c0ea657f15 new file mode 100644 index 00000000000..cc981c1eb5e Binary files /dev/null and b/fuzzers/corpora/commit_graph/aaca30ee3ab38edfa2b061fcbcbca0c0ea657f15 differ diff --git a/fuzzers/corpora/commit_graph/aacdec3f05e98eb6eedddb9c6edb968e1a63c551 b/fuzzers/corpora/commit_graph/aacdec3f05e98eb6eedddb9c6edb968e1a63c551 new file mode 100644 index 00000000000..622c24795f9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/aacdec3f05e98eb6eedddb9c6edb968e1a63c551 differ diff --git a/fuzzers/corpora/commit_graph/aadd85127241b94a41d02d9e9699e3e9773de1c9 b/fuzzers/corpora/commit_graph/aadd85127241b94a41d02d9e9699e3e9773de1c9 new file mode 100644 index 00000000000..b84e5f17da3 Binary files /dev/null and b/fuzzers/corpora/commit_graph/aadd85127241b94a41d02d9e9699e3e9773de1c9 differ diff --git a/fuzzers/corpora/commit_graph/ab8ad126702803d21dbafc85713bbee7f25f36e5 b/fuzzers/corpora/commit_graph/ab8ad126702803d21dbafc85713bbee7f25f36e5 new file mode 100644 index 00000000000..9a19eda0fb5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/ab8ad126702803d21dbafc85713bbee7f25f36e5 differ diff --git a/fuzzers/corpora/commit_graph/ac26f9afd599ff6f33396c2e02130654f3e2390c b/fuzzers/corpora/commit_graph/ac26f9afd599ff6f33396c2e02130654f3e2390c new file mode 100644 index 00000000000..843eaa25cd5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/ac26f9afd599ff6f33396c2e02130654f3e2390c differ diff --git a/fuzzers/corpora/commit_graph/ac8b129e4756fda0c50c9dd0eb13e34c7b41ce8e b/fuzzers/corpora/commit_graph/ac8b129e4756fda0c50c9dd0eb13e34c7b41ce8e new file mode 100644 index 00000000000..4b41d07c777 --- /dev/null +++ b/fuzzers/corpora/commit_graph/ac8b129e4756fda0c50c9dd0eb13e34c7b41ce8e @@ -0,0 +1 @@ +@ \ No newline at end of file diff --git a/fuzzers/corpora/commit_graph/aceaf3b72c2627dd3dd065974b854150681c093f b/fuzzers/corpora/commit_graph/aceaf3b72c2627dd3dd065974b854150681c093f new file mode 100644 index 00000000000..d490782e19f Binary files /dev/null and b/fuzzers/corpora/commit_graph/aceaf3b72c2627dd3dd065974b854150681c093f differ diff --git a/fuzzers/corpora/commit_graph/ad1fcdc3bf806392e754a902eba9edd3b344c31f b/fuzzers/corpora/commit_graph/ad1fcdc3bf806392e754a902eba9edd3b344c31f new file mode 100644 index 00000000000..1379b75e86f Binary files /dev/null and b/fuzzers/corpora/commit_graph/ad1fcdc3bf806392e754a902eba9edd3b344c31f differ diff --git a/fuzzers/corpora/commit_graph/ad8c80e532482f9dfbfbb7c0d447f1f4e592bf72 b/fuzzers/corpora/commit_graph/ad8c80e532482f9dfbfbb7c0d447f1f4e592bf72 new file mode 100644 index 00000000000..bfe39118aef Binary files /dev/null and b/fuzzers/corpora/commit_graph/ad8c80e532482f9dfbfbb7c0d447f1f4e592bf72 differ diff --git a/fuzzers/corpora/commit_graph/add92b71bf897da2f71f691e6abcb6d02cb8e99f b/fuzzers/corpora/commit_graph/add92b71bf897da2f71f691e6abcb6d02cb8e99f new file mode 100644 index 00000000000..1676dbfaf08 Binary files /dev/null and b/fuzzers/corpora/commit_graph/add92b71bf897da2f71f691e6abcb6d02cb8e99f differ diff --git a/fuzzers/corpora/commit_graph/aeb8ccf6d82be9236c9e689e1580d043bd701eb0 b/fuzzers/corpora/commit_graph/aeb8ccf6d82be9236c9e689e1580d043bd701eb0 new file mode 100644 index 00000000000..10688e3dbbd Binary files /dev/null and b/fuzzers/corpora/commit_graph/aeb8ccf6d82be9236c9e689e1580d043bd701eb0 differ diff --git a/fuzzers/corpora/commit_graph/af1a827aedbf674fff2bdeb5589554eec62787ab b/fuzzers/corpora/commit_graph/af1a827aedbf674fff2bdeb5589554eec62787ab new file mode 100644 index 00000000000..6f08ae5099f Binary files /dev/null and b/fuzzers/corpora/commit_graph/af1a827aedbf674fff2bdeb5589554eec62787ab differ diff --git a/fuzzers/corpora/commit_graph/afaab9a75414d231176ad4582b6f8d81b5dbedb3 b/fuzzers/corpora/commit_graph/afaab9a75414d231176ad4582b6f8d81b5dbedb3 new file mode 100644 index 00000000000..87017801022 Binary files /dev/null and b/fuzzers/corpora/commit_graph/afaab9a75414d231176ad4582b6f8d81b5dbedb3 differ diff --git a/fuzzers/corpora/commit_graph/afc12c4ebed1f3ab962d7dcef110b5328b1e24c3 b/fuzzers/corpora/commit_graph/afc12c4ebed1f3ab962d7dcef110b5328b1e24c3 new file mode 100644 index 00000000000..0e10f77368d Binary files /dev/null and b/fuzzers/corpora/commit_graph/afc12c4ebed1f3ab962d7dcef110b5328b1e24c3 differ diff --git a/fuzzers/corpora/commit_graph/b0044f3744cf019658d668a33f8d1e53ef8bd6ce b/fuzzers/corpora/commit_graph/b0044f3744cf019658d668a33f8d1e53ef8bd6ce new file mode 100644 index 00000000000..ede7bfe90df Binary files /dev/null and b/fuzzers/corpora/commit_graph/b0044f3744cf019658d668a33f8d1e53ef8bd6ce differ diff --git a/fuzzers/corpora/commit_graph/b06adc81a4e1cdcda3786970ca07ed9dee0b6401 b/fuzzers/corpora/commit_graph/b06adc81a4e1cdcda3786970ca07ed9dee0b6401 new file mode 100644 index 00000000000..b5f08b21fb6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b06adc81a4e1cdcda3786970ca07ed9dee0b6401 differ diff --git a/fuzzers/corpora/commit_graph/b139802a1cc90fd5b86cae044c221361892c688d b/fuzzers/corpora/commit_graph/b139802a1cc90fd5b86cae044c221361892c688d new file mode 100644 index 00000000000..c21f39ef8d7 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b139802a1cc90fd5b86cae044c221361892c688d differ diff --git a/fuzzers/corpora/commit_graph/b1b8f251542db01bdb01be3b6d5b117b07db1834 b/fuzzers/corpora/commit_graph/b1b8f251542db01bdb01be3b6d5b117b07db1834 new file mode 100644 index 00000000000..b42f7263bc6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b1b8f251542db01bdb01be3b6d5b117b07db1834 differ diff --git a/fuzzers/corpora/commit_graph/b1b9af93f84ed6861b9c0ade39980e89ef828c8f b/fuzzers/corpora/commit_graph/b1b9af93f84ed6861b9c0ade39980e89ef828c8f new file mode 100644 index 00000000000..b367b87d973 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b1b9af93f84ed6861b9c0ade39980e89ef828c8f differ diff --git a/fuzzers/corpora/commit_graph/b2eae68035cafd4077f6a4c3e4e961fdc1e8122b b/fuzzers/corpora/commit_graph/b2eae68035cafd4077f6a4c3e4e961fdc1e8122b new file mode 100644 index 00000000000..1c62898864c Binary files /dev/null and b/fuzzers/corpora/commit_graph/b2eae68035cafd4077f6a4c3e4e961fdc1e8122b differ diff --git a/fuzzers/corpora/commit_graph/b32897a6aedaa8c5a6e656dd808bafabc4ee5608 b/fuzzers/corpora/commit_graph/b32897a6aedaa8c5a6e656dd808bafabc4ee5608 new file mode 100644 index 00000000000..b3fbb813cc2 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b32897a6aedaa8c5a6e656dd808bafabc4ee5608 differ diff --git a/fuzzers/corpora/commit_graph/b376e4fc517297f92ac1713803ae3b60d5ebbe43 b/fuzzers/corpora/commit_graph/b376e4fc517297f92ac1713803ae3b60d5ebbe43 new file mode 100644 index 00000000000..637eafdebe9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b376e4fc517297f92ac1713803ae3b60d5ebbe43 differ diff --git a/fuzzers/corpora/commit_graph/b3fd100b139cfbffaad68aacf7d462861e9dca35 b/fuzzers/corpora/commit_graph/b3fd100b139cfbffaad68aacf7d462861e9dca35 new file mode 100644 index 00000000000..da105f5fcb5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b3fd100b139cfbffaad68aacf7d462861e9dca35 differ diff --git a/fuzzers/corpora/commit_graph/b40808ca955faab4829811bced1cccb2ab58ea58 b/fuzzers/corpora/commit_graph/b40808ca955faab4829811bced1cccb2ab58ea58 new file mode 100644 index 00000000000..4daa7413844 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b40808ca955faab4829811bced1cccb2ab58ea58 differ diff --git a/fuzzers/corpora/commit_graph/b43daf9f87a514bce74af3e5a39284c69c4e7011 b/fuzzers/corpora/commit_graph/b43daf9f87a514bce74af3e5a39284c69c4e7011 new file mode 100644 index 00000000000..5c8129df182 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b43daf9f87a514bce74af3e5a39284c69c4e7011 differ diff --git a/fuzzers/corpora/commit_graph/b477da07f3e5796ff4a98c8a5bdb0e4a634954bf b/fuzzers/corpora/commit_graph/b477da07f3e5796ff4a98c8a5bdb0e4a634954bf new file mode 100644 index 00000000000..dbe8abee3cb Binary files /dev/null and b/fuzzers/corpora/commit_graph/b477da07f3e5796ff4a98c8a5bdb0e4a634954bf differ diff --git a/fuzzers/corpora/commit_graph/b4a2ef09cf59ca5ccf810a6f001cce710cc02f6b b/fuzzers/corpora/commit_graph/b4a2ef09cf59ca5ccf810a6f001cce710cc02f6b new file mode 100644 index 00000000000..3a5817375ba Binary files /dev/null and b/fuzzers/corpora/commit_graph/b4a2ef09cf59ca5ccf810a6f001cce710cc02f6b differ diff --git a/fuzzers/corpora/commit_graph/b4b75e588cb83430c502a34ec3dcfaf774a00359 b/fuzzers/corpora/commit_graph/b4b75e588cb83430c502a34ec3dcfaf774a00359 new file mode 100644 index 00000000000..4afd452627d Binary files /dev/null and b/fuzzers/corpora/commit_graph/b4b75e588cb83430c502a34ec3dcfaf774a00359 differ diff --git a/fuzzers/corpora/commit_graph/b4ce98acd2b288b6cfc00461e2e15e0f8004030c b/fuzzers/corpora/commit_graph/b4ce98acd2b288b6cfc00461e2e15e0f8004030c new file mode 100644 index 00000000000..a7074267369 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b4ce98acd2b288b6cfc00461e2e15e0f8004030c differ diff --git a/fuzzers/corpora/commit_graph/b75563f30f7e4fb369d2449b723ee8b282d03eff b/fuzzers/corpora/commit_graph/b75563f30f7e4fb369d2449b723ee8b282d03eff new file mode 100644 index 00000000000..a101bca5e12 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b75563f30f7e4fb369d2449b723ee8b282d03eff differ diff --git a/fuzzers/corpora/commit_graph/b7a0a820afa7057081de186728d0d887131d9314 b/fuzzers/corpora/commit_graph/b7a0a820afa7057081de186728d0d887131d9314 new file mode 100644 index 00000000000..915cc26be3b Binary files /dev/null and b/fuzzers/corpora/commit_graph/b7a0a820afa7057081de186728d0d887131d9314 differ diff --git a/fuzzers/corpora/commit_graph/b7e880446146c735a3f820fb93969c8c172c2fb5 b/fuzzers/corpora/commit_graph/b7e880446146c735a3f820fb93969c8c172c2fb5 new file mode 100644 index 00000000000..cd26631d7be Binary files /dev/null and b/fuzzers/corpora/commit_graph/b7e880446146c735a3f820fb93969c8c172c2fb5 differ diff --git a/fuzzers/corpora/commit_graph/b833073d3006e7cbac03c494603a9b75e7b2a723 b/fuzzers/corpora/commit_graph/b833073d3006e7cbac03c494603a9b75e7b2a723 new file mode 100644 index 00000000000..0798037e504 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b833073d3006e7cbac03c494603a9b75e7b2a723 differ diff --git a/fuzzers/corpora/commit_graph/b89459c1fb6fc918db4c81a32a75ee66217f9ab8 b/fuzzers/corpora/commit_graph/b89459c1fb6fc918db4c81a32a75ee66217f9ab8 new file mode 100644 index 00000000000..bdc6de6840f Binary files /dev/null and b/fuzzers/corpora/commit_graph/b89459c1fb6fc918db4c81a32a75ee66217f9ab8 differ diff --git a/fuzzers/corpora/commit_graph/b8aab6c9b2c706f8df0ff695ff94969171f9c807 b/fuzzers/corpora/commit_graph/b8aab6c9b2c706f8df0ff695ff94969171f9c807 new file mode 100644 index 00000000000..810a30544ac Binary files /dev/null and b/fuzzers/corpora/commit_graph/b8aab6c9b2c706f8df0ff695ff94969171f9c807 differ diff --git a/fuzzers/corpora/commit_graph/b9751182a36acb79b77585e1e379857a530e95c8 b/fuzzers/corpora/commit_graph/b9751182a36acb79b77585e1e379857a530e95c8 new file mode 100644 index 00000000000..eeb1ed05d86 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b9751182a36acb79b77585e1e379857a530e95c8 differ diff --git a/fuzzers/corpora/commit_graph/b9ddb239b5a2c1348d972ec70a08507c35ba4432 b/fuzzers/corpora/commit_graph/b9ddb239b5a2c1348d972ec70a08507c35ba4432 new file mode 100644 index 00000000000..0d746e52c47 Binary files /dev/null and b/fuzzers/corpora/commit_graph/b9ddb239b5a2c1348d972ec70a08507c35ba4432 differ diff --git a/fuzzers/corpora/commit_graph/ba8f573256a0fbb95c5626f399ebc3ef50bbd826 b/fuzzers/corpora/commit_graph/ba8f573256a0fbb95c5626f399ebc3ef50bbd826 new file mode 100644 index 00000000000..330229e41a4 Binary files /dev/null and b/fuzzers/corpora/commit_graph/ba8f573256a0fbb95c5626f399ebc3ef50bbd826 differ diff --git a/fuzzers/corpora/commit_graph/bc165749042d5425c5d6d4e29b17769a2315a80d b/fuzzers/corpora/commit_graph/bc165749042d5425c5d6d4e29b17769a2315a80d new file mode 100644 index 00000000000..af0d232ab89 Binary files /dev/null and b/fuzzers/corpora/commit_graph/bc165749042d5425c5d6d4e29b17769a2315a80d differ diff --git a/fuzzers/corpora/commit_graph/bc910bd349319e1ed44d7c7266e3ac99cc29ecc6 b/fuzzers/corpora/commit_graph/bc910bd349319e1ed44d7c7266e3ac99cc29ecc6 new file mode 100644 index 00000000000..a38abe5802b Binary files /dev/null and b/fuzzers/corpora/commit_graph/bc910bd349319e1ed44d7c7266e3ac99cc29ecc6 differ diff --git a/fuzzers/corpora/commit_graph/bc97b1d4f57eb7770bc3983e2d57c8c01b21d29e b/fuzzers/corpora/commit_graph/bc97b1d4f57eb7770bc3983e2d57c8c01b21d29e new file mode 100644 index 00000000000..631754c10e7 Binary files /dev/null and b/fuzzers/corpora/commit_graph/bc97b1d4f57eb7770bc3983e2d57c8c01b21d29e differ diff --git a/fuzzers/corpora/commit_graph/bd06f768e35ded4437cb88e2bc0ddd0bea3fa84c b/fuzzers/corpora/commit_graph/bd06f768e35ded4437cb88e2bc0ddd0bea3fa84c new file mode 100644 index 00000000000..954e73d68fc Binary files /dev/null and b/fuzzers/corpora/commit_graph/bd06f768e35ded4437cb88e2bc0ddd0bea3fa84c differ diff --git a/fuzzers/corpora/commit_graph/bd702faff9725a7a1957fd0f85cc52799f37b682 b/fuzzers/corpora/commit_graph/bd702faff9725a7a1957fd0f85cc52799f37b682 new file mode 100644 index 00000000000..928fe58f75a Binary files /dev/null and b/fuzzers/corpora/commit_graph/bd702faff9725a7a1957fd0f85cc52799f37b682 differ diff --git a/fuzzers/corpora/commit_graph/bee4464861e1cae3cfdd5fbcb340efbf02e8d8ca b/fuzzers/corpora/commit_graph/bee4464861e1cae3cfdd5fbcb340efbf02e8d8ca new file mode 100644 index 00000000000..8f636ce9b0f Binary files /dev/null and b/fuzzers/corpora/commit_graph/bee4464861e1cae3cfdd5fbcb340efbf02e8d8ca differ diff --git a/fuzzers/corpora/commit_graph/bf7ad994b098ec85d62683a16e067635e21a8af5 b/fuzzers/corpora/commit_graph/bf7ad994b098ec85d62683a16e067635e21a8af5 new file mode 100644 index 00000000000..71ca107457d Binary files /dev/null and b/fuzzers/corpora/commit_graph/bf7ad994b098ec85d62683a16e067635e21a8af5 differ diff --git a/fuzzers/corpora/commit_graph/c054fc89ed72101dec861668ff1738ef85b728b9 b/fuzzers/corpora/commit_graph/c054fc89ed72101dec861668ff1738ef85b728b9 new file mode 100644 index 00000000000..988ab6be665 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c054fc89ed72101dec861668ff1738ef85b728b9 differ diff --git a/fuzzers/corpora/commit_graph/c06752415ac037fefe5172dc7245cd7c49ca7fca b/fuzzers/corpora/commit_graph/c06752415ac037fefe5172dc7245cd7c49ca7fca new file mode 100644 index 00000000000..d03fcac6c7f Binary files /dev/null and b/fuzzers/corpora/commit_graph/c06752415ac037fefe5172dc7245cd7c49ca7fca differ diff --git a/fuzzers/corpora/commit_graph/c0c8b54354d172a0be751e3e9b80be961bb15ddb b/fuzzers/corpora/commit_graph/c0c8b54354d172a0be751e3e9b80be961bb15ddb new file mode 100644 index 00000000000..233aa4e0372 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c0c8b54354d172a0be751e3e9b80be961bb15ddb differ diff --git a/fuzzers/corpora/commit_graph/c0e7ca9b5b4d0e72d23d7dc9e9d1f2463a17a20d b/fuzzers/corpora/commit_graph/c0e7ca9b5b4d0e72d23d7dc9e9d1f2463a17a20d new file mode 100644 index 00000000000..c36f7c16f9b Binary files /dev/null and b/fuzzers/corpora/commit_graph/c0e7ca9b5b4d0e72d23d7dc9e9d1f2463a17a20d differ diff --git a/fuzzers/corpora/commit_graph/c13576a29c98bee02aa47f646f5f170f9b7d83f9 b/fuzzers/corpora/commit_graph/c13576a29c98bee02aa47f646f5f170f9b7d83f9 new file mode 100644 index 00000000000..95bdcb90c16 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c13576a29c98bee02aa47f646f5f170f9b7d83f9 differ diff --git a/fuzzers/corpora/commit_graph/c14edf1d34f40b3cc74772c81ebe5d72172cc662 b/fuzzers/corpora/commit_graph/c14edf1d34f40b3cc74772c81ebe5d72172cc662 new file mode 100644 index 00000000000..63b1e3900d2 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c14edf1d34f40b3cc74772c81ebe5d72172cc662 differ diff --git a/fuzzers/corpora/commit_graph/c2789364cb35d111f08f924d0d7550ea9785c61e b/fuzzers/corpora/commit_graph/c2789364cb35d111f08f924d0d7550ea9785c61e new file mode 100644 index 00000000000..0044eb5037b Binary files /dev/null and b/fuzzers/corpora/commit_graph/c2789364cb35d111f08f924d0d7550ea9785c61e differ diff --git a/fuzzers/corpora/commit_graph/c2d8b07acb13e43a89b6c4afb3ecb9817dd4a8e9 b/fuzzers/corpora/commit_graph/c2d8b07acb13e43a89b6c4afb3ecb9817dd4a8e9 new file mode 100644 index 00000000000..8cf6a914dd4 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c2d8b07acb13e43a89b6c4afb3ecb9817dd4a8e9 differ diff --git a/fuzzers/corpora/commit_graph/c36ed796c1bf839668db8fc3475a2ffb32ad8ceb b/fuzzers/corpora/commit_graph/c36ed796c1bf839668db8fc3475a2ffb32ad8ceb new file mode 100644 index 00000000000..77dd76b6b37 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c36ed796c1bf839668db8fc3475a2ffb32ad8ceb differ diff --git a/fuzzers/corpora/commit_graph/c41ec9dd94427423e4704721e7f21eae0c44ef20 b/fuzzers/corpora/commit_graph/c41ec9dd94427423e4704721e7f21eae0c44ef20 new file mode 100644 index 00000000000..9b828b8cfa8 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c41ec9dd94427423e4704721e7f21eae0c44ef20 differ diff --git a/fuzzers/corpora/commit_graph/c42c544fa9dbb1264b39bf920b40985384db1d16 b/fuzzers/corpora/commit_graph/c42c544fa9dbb1264b39bf920b40985384db1d16 new file mode 100644 index 00000000000..60889805d74 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c42c544fa9dbb1264b39bf920b40985384db1d16 differ diff --git a/fuzzers/corpora/commit_graph/c45ec3f594abc15de0a8cc3ad748ba23cb34ec64 b/fuzzers/corpora/commit_graph/c45ec3f594abc15de0a8cc3ad748ba23cb34ec64 new file mode 100644 index 00000000000..6a711bab2a5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c45ec3f594abc15de0a8cc3ad748ba23cb34ec64 differ diff --git a/fuzzers/corpora/commit_graph/c49004d980961f288616a4eb9ebf68123fd68ffa b/fuzzers/corpora/commit_graph/c49004d980961f288616a4eb9ebf68123fd68ffa new file mode 100644 index 00000000000..0b5a6bcfefa Binary files /dev/null and b/fuzzers/corpora/commit_graph/c49004d980961f288616a4eb9ebf68123fd68ffa differ diff --git a/fuzzers/corpora/commit_graph/c4c3c3c8df24adf505127627b3090116de78d9a6 b/fuzzers/corpora/commit_graph/c4c3c3c8df24adf505127627b3090116de78d9a6 new file mode 100644 index 00000000000..528725c9348 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c4c3c3c8df24adf505127627b3090116de78d9a6 differ diff --git a/fuzzers/corpora/commit_graph/c5c1921293af4a5953cb386092694042715fcfb3 b/fuzzers/corpora/commit_graph/c5c1921293af4a5953cb386092694042715fcfb3 new file mode 100644 index 00000000000..2256d19b98c Binary files /dev/null and b/fuzzers/corpora/commit_graph/c5c1921293af4a5953cb386092694042715fcfb3 differ diff --git a/fuzzers/corpora/commit_graph/c615caad21cd8a754fcb2008420234c5511c62b7 b/fuzzers/corpora/commit_graph/c615caad21cd8a754fcb2008420234c5511c62b7 new file mode 100644 index 00000000000..fa4c9cc7313 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c615caad21cd8a754fcb2008420234c5511c62b7 differ diff --git a/fuzzers/corpora/commit_graph/c6a9ee3f8fdc42566c4799db3912a83c8c438d7f b/fuzzers/corpora/commit_graph/c6a9ee3f8fdc42566c4799db3912a83c8c438d7f new file mode 100644 index 00000000000..c3c69fcb5d0 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c6a9ee3f8fdc42566c4799db3912a83c8c438d7f differ diff --git a/fuzzers/corpora/commit_graph/c6b661e976282051285b913b3728383f36103ef8 b/fuzzers/corpora/commit_graph/c6b661e976282051285b913b3728383f36103ef8 new file mode 100644 index 00000000000..6a156d2fdf3 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c6b661e976282051285b913b3728383f36103ef8 differ diff --git a/fuzzers/corpora/commit_graph/c716ba47f810c238fe7bda1fbdc7b1ccc34e9848 b/fuzzers/corpora/commit_graph/c716ba47f810c238fe7bda1fbdc7b1ccc34e9848 new file mode 100644 index 00000000000..271ec76f953 --- /dev/null +++ b/fuzzers/corpora/commit_graph/c716ba47f810c238fe7bda1fbdc7b1ccc34e9848 @@ -0,0 +1 @@ +@ \ No newline at end of file diff --git a/fuzzers/corpora/commit_graph/c85b2fa4421302e2fa333a9e33d59a882aa04f4f b/fuzzers/corpora/commit_graph/c85b2fa4421302e2fa333a9e33d59a882aa04f4f new file mode 100644 index 00000000000..eba46a67ca8 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c85b2fa4421302e2fa333a9e33d59a882aa04f4f differ diff --git a/fuzzers/corpora/commit_graph/c871d135f2d3117b326688355bc0fa6f26d56cd6 b/fuzzers/corpora/commit_graph/c871d135f2d3117b326688355bc0fa6f26d56cd6 new file mode 100644 index 00000000000..6ead612d79e Binary files /dev/null and b/fuzzers/corpora/commit_graph/c871d135f2d3117b326688355bc0fa6f26d56cd6 differ diff --git a/fuzzers/corpora/commit_graph/c915b02265a27d185a8b028305f082ddb3ebd704 b/fuzzers/corpora/commit_graph/c915b02265a27d185a8b028305f082ddb3ebd704 new file mode 100644 index 00000000000..1960dfb4748 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c915b02265a27d185a8b028305f082ddb3ebd704 differ diff --git a/fuzzers/corpora/commit_graph/c952d38b3e642db4795d7f954b85f4f6d2a041aa b/fuzzers/corpora/commit_graph/c952d38b3e642db4795d7f954b85f4f6d2a041aa new file mode 100644 index 00000000000..b8ee305562e Binary files /dev/null and b/fuzzers/corpora/commit_graph/c952d38b3e642db4795d7f954b85f4f6d2a041aa differ diff --git a/fuzzers/corpora/commit_graph/c98ee52065736c4172f6ee0c31977bf1b560d685 b/fuzzers/corpora/commit_graph/c98ee52065736c4172f6ee0c31977bf1b560d685 new file mode 100644 index 00000000000..f8d7a23f1c8 Binary files /dev/null and b/fuzzers/corpora/commit_graph/c98ee52065736c4172f6ee0c31977bf1b560d685 differ diff --git a/fuzzers/corpora/commit_graph/c99b183a2cd0dd8a4c1a141cc6eebb0311501fa5 b/fuzzers/corpora/commit_graph/c99b183a2cd0dd8a4c1a141cc6eebb0311501fa5 new file mode 100644 index 00000000000..9cd1ad4cf77 --- /dev/null +++ b/fuzzers/corpora/commit_graph/c99b183a2cd0dd8a4c1a141cc6eebb0311501fa5 @@ -0,0 +1 @@ +@~ \ No newline at end of file diff --git a/fuzzers/corpora/commit_graph/ca0cd26baff2f2c0759e619800ebbe7314d2bb95 b/fuzzers/corpora/commit_graph/ca0cd26baff2f2c0759e619800ebbe7314d2bb95 new file mode 100644 index 00000000000..78a357b557c Binary files /dev/null and b/fuzzers/corpora/commit_graph/ca0cd26baff2f2c0759e619800ebbe7314d2bb95 differ diff --git a/fuzzers/corpora/commit_graph/ca3e0d745c35d7cceb0f6e3f8a709eb658b7e5a8 b/fuzzers/corpora/commit_graph/ca3e0d745c35d7cceb0f6e3f8a709eb658b7e5a8 new file mode 100644 index 00000000000..174863ddb91 Binary files /dev/null and b/fuzzers/corpora/commit_graph/ca3e0d745c35d7cceb0f6e3f8a709eb658b7e5a8 differ diff --git a/fuzzers/corpora/commit_graph/cac667320e99e93a796bb89842de4675735eb4a4 b/fuzzers/corpora/commit_graph/cac667320e99e93a796bb89842de4675735eb4a4 new file mode 100644 index 00000000000..20e9db998fd Binary files /dev/null and b/fuzzers/corpora/commit_graph/cac667320e99e93a796bb89842de4675735eb4a4 differ diff --git a/fuzzers/corpora/commit_graph/cb41b00e9db33a07e27b3ee05d3bbecaf853b963 b/fuzzers/corpora/commit_graph/cb41b00e9db33a07e27b3ee05d3bbecaf853b963 new file mode 100644 index 00000000000..612843d8aed Binary files /dev/null and b/fuzzers/corpora/commit_graph/cb41b00e9db33a07e27b3ee05d3bbecaf853b963 differ diff --git a/fuzzers/corpora/commit_graph/cbdbd3f320eee627097778f15b9fb2c1dc2bd15f b/fuzzers/corpora/commit_graph/cbdbd3f320eee627097778f15b9fb2c1dc2bd15f new file mode 100644 index 00000000000..a922018ee49 Binary files /dev/null and b/fuzzers/corpora/commit_graph/cbdbd3f320eee627097778f15b9fb2c1dc2bd15f differ diff --git a/fuzzers/corpora/commit_graph/cc7f114000c83abb2ab17f0deab6dcfc2acde7f5 b/fuzzers/corpora/commit_graph/cc7f114000c83abb2ab17f0deab6dcfc2acde7f5 new file mode 100644 index 00000000000..7c9f84c333f Binary files /dev/null and b/fuzzers/corpora/commit_graph/cc7f114000c83abb2ab17f0deab6dcfc2acde7f5 differ diff --git a/fuzzers/corpora/commit_graph/cc9bb93a6b7a1362a15f04898845dbe1447ec382 b/fuzzers/corpora/commit_graph/cc9bb93a6b7a1362a15f04898845dbe1447ec382 new file mode 100644 index 00000000000..5dde22529c2 Binary files /dev/null and b/fuzzers/corpora/commit_graph/cc9bb93a6b7a1362a15f04898845dbe1447ec382 differ diff --git a/fuzzers/corpora/commit_graph/cce7355f826bbcf3955394596d358abc7df6fe6f b/fuzzers/corpora/commit_graph/cce7355f826bbcf3955394596d358abc7df6fe6f new file mode 100644 index 00000000000..9b780380014 Binary files /dev/null and b/fuzzers/corpora/commit_graph/cce7355f826bbcf3955394596d358abc7df6fe6f differ diff --git a/fuzzers/corpora/commit_graph/cceff2878a558166fb5bf2a0354c1be31dcc4e21 b/fuzzers/corpora/commit_graph/cceff2878a558166fb5bf2a0354c1be31dcc4e21 new file mode 100644 index 00000000000..94fec1bbc64 Binary files /dev/null and b/fuzzers/corpora/commit_graph/cceff2878a558166fb5bf2a0354c1be31dcc4e21 differ diff --git a/fuzzers/corpora/commit_graph/cd96909f3ded7aa54bb2ffd2f2f47f8acc6f99e2 b/fuzzers/corpora/commit_graph/cd96909f3ded7aa54bb2ffd2f2f47f8acc6f99e2 new file mode 100644 index 00000000000..22bbade2df5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/cd96909f3ded7aa54bb2ffd2f2f47f8acc6f99e2 differ diff --git a/fuzzers/corpora/commit_graph/cee9f69d7d1a227833fba127a529ea2a10341da3 b/fuzzers/corpora/commit_graph/cee9f69d7d1a227833fba127a529ea2a10341da3 new file mode 100644 index 00000000000..2fff45e837b Binary files /dev/null and b/fuzzers/corpora/commit_graph/cee9f69d7d1a227833fba127a529ea2a10341da3 differ diff --git a/fuzzers/corpora/commit_graph/d064f27a3109afde629165432f78f389da73ff07 b/fuzzers/corpora/commit_graph/d064f27a3109afde629165432f78f389da73ff07 new file mode 100644 index 00000000000..e6d98c0897a Binary files /dev/null and b/fuzzers/corpora/commit_graph/d064f27a3109afde629165432f78f389da73ff07 differ diff --git a/fuzzers/corpora/commit_graph/d07e3094f02b0c0e3bab370684c2d8c5634224d5 b/fuzzers/corpora/commit_graph/d07e3094f02b0c0e3bab370684c2d8c5634224d5 new file mode 100644 index 00000000000..29d86aadba9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/d07e3094f02b0c0e3bab370684c2d8c5634224d5 differ diff --git a/fuzzers/corpora/commit_graph/d0ba3413d5706de17de64824d78233d48c6efbec b/fuzzers/corpora/commit_graph/d0ba3413d5706de17de64824d78233d48c6efbec new file mode 100644 index 00000000000..96c82076020 Binary files /dev/null and b/fuzzers/corpora/commit_graph/d0ba3413d5706de17de64824d78233d48c6efbec differ diff --git a/fuzzers/corpora/commit_graph/d136511364a74973b009f2be9b021d4122f71a6c b/fuzzers/corpora/commit_graph/d136511364a74973b009f2be9b021d4122f71a6c new file mode 100644 index 00000000000..a663ab9152f Binary files /dev/null and b/fuzzers/corpora/commit_graph/d136511364a74973b009f2be9b021d4122f71a6c differ diff --git a/fuzzers/corpora/commit_graph/d1d215c40bcc8dd4ce02b0c0621e90b183b40b3e b/fuzzers/corpora/commit_graph/d1d215c40bcc8dd4ce02b0c0621e90b183b40b3e new file mode 100644 index 00000000000..a328d3e9c3b Binary files /dev/null and b/fuzzers/corpora/commit_graph/d1d215c40bcc8dd4ce02b0c0621e90b183b40b3e differ diff --git a/fuzzers/corpora/commit_graph/d1e35b137b2027b61def408f3f3c8cf9bcab274e b/fuzzers/corpora/commit_graph/d1e35b137b2027b61def408f3f3c8cf9bcab274e new file mode 100644 index 00000000000..79c43fd21ae Binary files /dev/null and b/fuzzers/corpora/commit_graph/d1e35b137b2027b61def408f3f3c8cf9bcab274e differ diff --git a/fuzzers/corpora/commit_graph/d349d137e57fb1a60ab8babd20e2acedc7a9042e b/fuzzers/corpora/commit_graph/d349d137e57fb1a60ab8babd20e2acedc7a9042e new file mode 100644 index 00000000000..981aaf6bea2 Binary files /dev/null and b/fuzzers/corpora/commit_graph/d349d137e57fb1a60ab8babd20e2acedc7a9042e differ diff --git a/fuzzers/corpora/commit_graph/d3714ec4d3acc6262295b0fc99c6ba699f5bfe65 b/fuzzers/corpora/commit_graph/d3714ec4d3acc6262295b0fc99c6ba699f5bfe65 new file mode 100644 index 00000000000..7f6e30995bb Binary files /dev/null and b/fuzzers/corpora/commit_graph/d3714ec4d3acc6262295b0fc99c6ba699f5bfe65 differ diff --git a/fuzzers/corpora/commit_graph/d419df696512216074f1c6b17ea1dfc81c0e6e20 b/fuzzers/corpora/commit_graph/d419df696512216074f1c6b17ea1dfc81c0e6e20 new file mode 100644 index 00000000000..47c701932fc Binary files /dev/null and b/fuzzers/corpora/commit_graph/d419df696512216074f1c6b17ea1dfc81c0e6e20 differ diff --git a/fuzzers/corpora/commit_graph/d49ad4fdafac251ceec32481826228c1698360aa b/fuzzers/corpora/commit_graph/d49ad4fdafac251ceec32481826228c1698360aa new file mode 100644 index 00000000000..9d4756c4bc8 Binary files /dev/null and b/fuzzers/corpora/commit_graph/d49ad4fdafac251ceec32481826228c1698360aa differ diff --git a/fuzzers/corpora/commit_graph/d4f85ba549c87ccaba59971a25da7e07b57c9f4e b/fuzzers/corpora/commit_graph/d4f85ba549c87ccaba59971a25da7e07b57c9f4e new file mode 100644 index 00000000000..e6d72e7bd82 Binary files /dev/null and b/fuzzers/corpora/commit_graph/d4f85ba549c87ccaba59971a25da7e07b57c9f4e differ diff --git a/fuzzers/corpora/commit_graph/d51ade0715bcea7decee2a045934599a10c1b07a b/fuzzers/corpora/commit_graph/d51ade0715bcea7decee2a045934599a10c1b07a new file mode 100644 index 00000000000..2403b35513a Binary files /dev/null and b/fuzzers/corpora/commit_graph/d51ade0715bcea7decee2a045934599a10c1b07a differ diff --git a/fuzzers/corpora/commit_graph/d5447fb72c97462a3f47c8b2d55deb0afaa225f8 b/fuzzers/corpora/commit_graph/d5447fb72c97462a3f47c8b2d55deb0afaa225f8 new file mode 100644 index 00000000000..07e14a4b03e Binary files /dev/null and b/fuzzers/corpora/commit_graph/d5447fb72c97462a3f47c8b2d55deb0afaa225f8 differ diff --git a/fuzzers/corpora/commit_graph/d6611a91c29291872ed2932455cb15ddb3801323 b/fuzzers/corpora/commit_graph/d6611a91c29291872ed2932455cb15ddb3801323 new file mode 100644 index 00000000000..361431c7327 Binary files /dev/null and b/fuzzers/corpora/commit_graph/d6611a91c29291872ed2932455cb15ddb3801323 differ diff --git a/fuzzers/corpora/commit_graph/d676f5e7efd6de6f2e1773231479471d2bba7261 b/fuzzers/corpora/commit_graph/d676f5e7efd6de6f2e1773231479471d2bba7261 new file mode 100644 index 00000000000..f9756ab7e5a Binary files /dev/null and b/fuzzers/corpora/commit_graph/d676f5e7efd6de6f2e1773231479471d2bba7261 differ diff --git a/fuzzers/corpora/commit_graph/d6a21eaa08a957d8f428192e193c2508fca2c218 b/fuzzers/corpora/commit_graph/d6a21eaa08a957d8f428192e193c2508fca2c218 new file mode 100644 index 00000000000..f845fbb6e3a Binary files /dev/null and b/fuzzers/corpora/commit_graph/d6a21eaa08a957d8f428192e193c2508fca2c218 differ diff --git a/fuzzers/corpora/commit_graph/d778052a29539344a9e3144e262e68df9628ebde b/fuzzers/corpora/commit_graph/d778052a29539344a9e3144e262e68df9628ebde new file mode 100644 index 00000000000..beace901be3 Binary files /dev/null and b/fuzzers/corpora/commit_graph/d778052a29539344a9e3144e262e68df9628ebde differ diff --git a/fuzzers/corpora/commit_graph/d884f6944adfff7cb41728062bf91cac5cdacfc9 b/fuzzers/corpora/commit_graph/d884f6944adfff7cb41728062bf91cac5cdacfc9 new file mode 100644 index 00000000000..f6de6519c2d Binary files /dev/null and b/fuzzers/corpora/commit_graph/d884f6944adfff7cb41728062bf91cac5cdacfc9 differ diff --git a/fuzzers/corpora/commit_graph/d89aae18d8e320bbae55eaae6a0514d7e005a883 b/fuzzers/corpora/commit_graph/d89aae18d8e320bbae55eaae6a0514d7e005a883 new file mode 100644 index 00000000000..1f0bbf302e7 Binary files /dev/null and b/fuzzers/corpora/commit_graph/d89aae18d8e320bbae55eaae6a0514d7e005a883 differ diff --git a/fuzzers/corpora/commit_graph/d926fde818c63f7b34f38c9f018bc833bc0bf7e1 b/fuzzers/corpora/commit_graph/d926fde818c63f7b34f38c9f018bc833bc0bf7e1 new file mode 100644 index 00000000000..fe1e2743e52 Binary files /dev/null and b/fuzzers/corpora/commit_graph/d926fde818c63f7b34f38c9f018bc833bc0bf7e1 differ diff --git a/fuzzers/corpora/commit_graph/d9d542d7c56774143cb6362e5a63739055469349 b/fuzzers/corpora/commit_graph/d9d542d7c56774143cb6362e5a63739055469349 new file mode 100644 index 00000000000..946106f79aa Binary files /dev/null and b/fuzzers/corpora/commit_graph/d9d542d7c56774143cb6362e5a63739055469349 differ diff --git a/fuzzers/corpora/commit_graph/da99bc9ce5b831f132dfb2eb11b8537e5cccfcd4 b/fuzzers/corpora/commit_graph/da99bc9ce5b831f132dfb2eb11b8537e5cccfcd4 new file mode 100644 index 00000000000..91ed5b2f781 Binary files /dev/null and b/fuzzers/corpora/commit_graph/da99bc9ce5b831f132dfb2eb11b8537e5cccfcd4 differ diff --git a/fuzzers/corpora/commit_graph/dabff2729fa69ab507fb00b7392aee1262056a29 b/fuzzers/corpora/commit_graph/dabff2729fa69ab507fb00b7392aee1262056a29 new file mode 100644 index 00000000000..9318cec8581 Binary files /dev/null and b/fuzzers/corpora/commit_graph/dabff2729fa69ab507fb00b7392aee1262056a29 differ diff --git a/fuzzers/corpora/commit_graph/dac4f4b91e33847bcedf7c66ef6e4ad0181e8ad8 b/fuzzers/corpora/commit_graph/dac4f4b91e33847bcedf7c66ef6e4ad0181e8ad8 new file mode 100644 index 00000000000..9587c539ba6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/dac4f4b91e33847bcedf7c66ef6e4ad0181e8ad8 differ diff --git a/fuzzers/corpora/commit_graph/db10ff6d01c7a66aa1823b9f99193590ddce99c6 b/fuzzers/corpora/commit_graph/db10ff6d01c7a66aa1823b9f99193590ddce99c6 new file mode 100644 index 00000000000..2d8d099866f Binary files /dev/null and b/fuzzers/corpora/commit_graph/db10ff6d01c7a66aa1823b9f99193590ddce99c6 differ diff --git a/fuzzers/corpora/commit_graph/dbbda2208fa688a5275dda0d304630db01ca081d b/fuzzers/corpora/commit_graph/dbbda2208fa688a5275dda0d304630db01ca081d new file mode 100644 index 00000000000..7edfc3b9733 Binary files /dev/null and b/fuzzers/corpora/commit_graph/dbbda2208fa688a5275dda0d304630db01ca081d differ diff --git a/fuzzers/corpora/commit_graph/dc47c5037be68a2747ff8a9fa450e1078a5ac5a5 b/fuzzers/corpora/commit_graph/dc47c5037be68a2747ff8a9fa450e1078a5ac5a5 new file mode 100644 index 00000000000..e4ac972c2f1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/dc47c5037be68a2747ff8a9fa450e1078a5ac5a5 differ diff --git a/fuzzers/corpora/commit_graph/dc760f136b123e38677aec72853e3365f08010fc b/fuzzers/corpora/commit_graph/dc760f136b123e38677aec72853e3365f08010fc new file mode 100644 index 00000000000..855c7b3a7c3 Binary files /dev/null and b/fuzzers/corpora/commit_graph/dc760f136b123e38677aec72853e3365f08010fc differ diff --git a/fuzzers/corpora/commit_graph/dca41b901bf1612d4197e6a450366a00ac036ec3 b/fuzzers/corpora/commit_graph/dca41b901bf1612d4197e6a450366a00ac036ec3 new file mode 100644 index 00000000000..9eec2738f60 Binary files /dev/null and b/fuzzers/corpora/commit_graph/dca41b901bf1612d4197e6a450366a00ac036ec3 differ diff --git a/fuzzers/corpora/commit_graph/dca62f21fce50d1c8c51b82e0d7eeedc6746e652 b/fuzzers/corpora/commit_graph/dca62f21fce50d1c8c51b82e0d7eeedc6746e652 new file mode 100644 index 00000000000..f8188c6918a Binary files /dev/null and b/fuzzers/corpora/commit_graph/dca62f21fce50d1c8c51b82e0d7eeedc6746e652 differ diff --git a/fuzzers/corpora/commit_graph/dcc7e6c444f95b10d634b1137413824e2cd68f62 b/fuzzers/corpora/commit_graph/dcc7e6c444f95b10d634b1137413824e2cd68f62 new file mode 100644 index 00000000000..247d648abde Binary files /dev/null and b/fuzzers/corpora/commit_graph/dcc7e6c444f95b10d634b1137413824e2cd68f62 differ diff --git a/fuzzers/corpora/commit_graph/dcf4b6addda69040f792c9b860ade2af0b77a14c b/fuzzers/corpora/commit_graph/dcf4b6addda69040f792c9b860ade2af0b77a14c new file mode 100644 index 00000000000..6cbb0b72100 Binary files /dev/null and b/fuzzers/corpora/commit_graph/dcf4b6addda69040f792c9b860ade2af0b77a14c differ diff --git a/fuzzers/corpora/commit_graph/dd6178166ac1eed82d132fea491bcda0d953227c b/fuzzers/corpora/commit_graph/dd6178166ac1eed82d132fea491bcda0d953227c new file mode 100644 index 00000000000..4c13f9985a3 Binary files /dev/null and b/fuzzers/corpora/commit_graph/dd6178166ac1eed82d132fea491bcda0d953227c differ diff --git a/fuzzers/corpora/commit_graph/ddbd5d3074323ccd7cd70bf5de5a2f30de977d99 b/fuzzers/corpora/commit_graph/ddbd5d3074323ccd7cd70bf5de5a2f30de977d99 new file mode 100644 index 00000000000..646febd1f44 Binary files /dev/null and b/fuzzers/corpora/commit_graph/ddbd5d3074323ccd7cd70bf5de5a2f30de977d99 differ diff --git a/fuzzers/corpora/commit_graph/ddd8ec5632bf1b8153d03a4537d3d76517c497d5 b/fuzzers/corpora/commit_graph/ddd8ec5632bf1b8153d03a4537d3d76517c497d5 new file mode 100644 index 00000000000..e948534f626 Binary files /dev/null and b/fuzzers/corpora/commit_graph/ddd8ec5632bf1b8153d03a4537d3d76517c497d5 differ diff --git a/fuzzers/corpora/commit_graph/de7a56f36e10d7b9ff43160b1cea3e76b24386d1 b/fuzzers/corpora/commit_graph/de7a56f36e10d7b9ff43160b1cea3e76b24386d1 new file mode 100644 index 00000000000..6cae1f2c4be Binary files /dev/null and b/fuzzers/corpora/commit_graph/de7a56f36e10d7b9ff43160b1cea3e76b24386d1 differ diff --git a/fuzzers/corpora/commit_graph/defa60aa46ea5a47c09b6962b4e4296ef1bcad92 b/fuzzers/corpora/commit_graph/defa60aa46ea5a47c09b6962b4e4296ef1bcad92 new file mode 100644 index 00000000000..297875550d9 Binary files /dev/null and b/fuzzers/corpora/commit_graph/defa60aa46ea5a47c09b6962b4e4296ef1bcad92 differ diff --git a/fuzzers/corpora/commit_graph/e0ae419425207832518d66c0ef35d11cbdc20361 b/fuzzers/corpora/commit_graph/e0ae419425207832518d66c0ef35d11cbdc20361 new file mode 100644 index 00000000000..89404f4cc1c Binary files /dev/null and b/fuzzers/corpora/commit_graph/e0ae419425207832518d66c0ef35d11cbdc20361 differ diff --git a/fuzzers/corpora/commit_graph/e0f519accbf15bc57a1bf1d7cc46d2a0b07a67f5 b/fuzzers/corpora/commit_graph/e0f519accbf15bc57a1bf1d7cc46d2a0b07a67f5 new file mode 100644 index 00000000000..af59e9d06bc Binary files /dev/null and b/fuzzers/corpora/commit_graph/e0f519accbf15bc57a1bf1d7cc46d2a0b07a67f5 differ diff --git a/fuzzers/corpora/commit_graph/e128eff8ca7572d9bb0bfc84f64d79c52afc2c67 b/fuzzers/corpora/commit_graph/e128eff8ca7572d9bb0bfc84f64d79c52afc2c67 new file mode 100644 index 00000000000..d963f77b5c6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/e128eff8ca7572d9bb0bfc84f64d79c52afc2c67 differ diff --git a/fuzzers/corpora/commit_graph/e17fdc21ae03243bd1d31bb6301b4187cab6fe47 b/fuzzers/corpora/commit_graph/e17fdc21ae03243bd1d31bb6301b4187cab6fe47 new file mode 100644 index 00000000000..381f8e1bdbb Binary files /dev/null and b/fuzzers/corpora/commit_graph/e17fdc21ae03243bd1d31bb6301b4187cab6fe47 differ diff --git a/fuzzers/corpora/commit_graph/e340ace35a2db7f89d6aa21cc1300766a74be4e1 b/fuzzers/corpora/commit_graph/e340ace35a2db7f89d6aa21cc1300766a74be4e1 new file mode 100644 index 00000000000..00cb20b4616 Binary files /dev/null and b/fuzzers/corpora/commit_graph/e340ace35a2db7f89d6aa21cc1300766a74be4e1 differ diff --git a/fuzzers/corpora/commit_graph/e36dfc11bcaab1e42df13924a2d7da024684db2e b/fuzzers/corpora/commit_graph/e36dfc11bcaab1e42df13924a2d7da024684db2e new file mode 100644 index 00000000000..25dd8eacd31 Binary files /dev/null and b/fuzzers/corpora/commit_graph/e36dfc11bcaab1e42df13924a2d7da024684db2e differ diff --git a/fuzzers/corpora/commit_graph/e39e0c87ac5ce0b78c89ae2df84226baba666372 b/fuzzers/corpora/commit_graph/e39e0c87ac5ce0b78c89ae2df84226baba666372 new file mode 100644 index 00000000000..acfc88121d7 Binary files /dev/null and b/fuzzers/corpora/commit_graph/e39e0c87ac5ce0b78c89ae2df84226baba666372 differ diff --git a/fuzzers/corpora/commit_graph/e46b4666c6bfcd6f589ec3617a48cce9c968e833 b/fuzzers/corpora/commit_graph/e46b4666c6bfcd6f589ec3617a48cce9c968e833 new file mode 100644 index 00000000000..92bca774e7e Binary files /dev/null and b/fuzzers/corpora/commit_graph/e46b4666c6bfcd6f589ec3617a48cce9c968e833 differ diff --git a/fuzzers/corpora/commit_graph/e57219555e11f9221d3166d5029ed2ad92300608 b/fuzzers/corpora/commit_graph/e57219555e11f9221d3166d5029ed2ad92300608 new file mode 100644 index 00000000000..6f9d153f90e Binary files /dev/null and b/fuzzers/corpora/commit_graph/e57219555e11f9221d3166d5029ed2ad92300608 differ diff --git a/fuzzers/corpora/commit_graph/e58ce590c2454e7ebe18e0a31a943b0b754fbd13 b/fuzzers/corpora/commit_graph/e58ce590c2454e7ebe18e0a31a943b0b754fbd13 new file mode 100644 index 00000000000..89d479039ce Binary files /dev/null and b/fuzzers/corpora/commit_graph/e58ce590c2454e7ebe18e0a31a943b0b754fbd13 differ diff --git a/fuzzers/corpora/commit_graph/e595f8fef5c8014cb0867978c6580301078ca0d9 b/fuzzers/corpora/commit_graph/e595f8fef5c8014cb0867978c6580301078ca0d9 new file mode 100644 index 00000000000..339b09efdb7 Binary files /dev/null and b/fuzzers/corpora/commit_graph/e595f8fef5c8014cb0867978c6580301078ca0d9 differ diff --git a/fuzzers/corpora/commit_graph/e5b76398f60628e879328d7009b9fa89feea14cb b/fuzzers/corpora/commit_graph/e5b76398f60628e879328d7009b9fa89feea14cb new file mode 100644 index 00000000000..6ed637f6e35 Binary files /dev/null and b/fuzzers/corpora/commit_graph/e5b76398f60628e879328d7009b9fa89feea14cb differ diff --git a/fuzzers/corpora/commit_graph/e5cec0217eea93b18a59d76b0aed6b46b13fa6a9 b/fuzzers/corpora/commit_graph/e5cec0217eea93b18a59d76b0aed6b46b13fa6a9 new file mode 100644 index 00000000000..7ae3eb4183b Binary files /dev/null and b/fuzzers/corpora/commit_graph/e5cec0217eea93b18a59d76b0aed6b46b13fa6a9 differ diff --git a/fuzzers/corpora/commit_graph/e637b4e0b47d0d6cd870502e6a2d6a53bf917f73 b/fuzzers/corpora/commit_graph/e637b4e0b47d0d6cd870502e6a2d6a53bf917f73 new file mode 100644 index 00000000000..f84ee8c61f6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/e637b4e0b47d0d6cd870502e6a2d6a53bf917f73 differ diff --git a/fuzzers/corpora/commit_graph/e7a6cb6e5a1552837fdbee9025fc48a9373f8564 b/fuzzers/corpora/commit_graph/e7a6cb6e5a1552837fdbee9025fc48a9373f8564 new file mode 100644 index 00000000000..a5ae268ec31 Binary files /dev/null and b/fuzzers/corpora/commit_graph/e7a6cb6e5a1552837fdbee9025fc48a9373f8564 differ diff --git a/fuzzers/corpora/commit_graph/e7f57c48016e1180c9af95acd34470881f10bd06 b/fuzzers/corpora/commit_graph/e7f57c48016e1180c9af95acd34470881f10bd06 new file mode 100644 index 00000000000..07bbb9c5b81 Binary files /dev/null and b/fuzzers/corpora/commit_graph/e7f57c48016e1180c9af95acd34470881f10bd06 differ diff --git a/fuzzers/corpora/commit_graph/e8253c668bfe37df5c5ada3226860cee74fb33a2 b/fuzzers/corpora/commit_graph/e8253c668bfe37df5c5ada3226860cee74fb33a2 new file mode 100644 index 00000000000..0cb7581d973 Binary files /dev/null and b/fuzzers/corpora/commit_graph/e8253c668bfe37df5c5ada3226860cee74fb33a2 differ diff --git a/fuzzers/corpora/commit_graph/e8f9981443c34ece02bca3c66130f3429d7b3375 b/fuzzers/corpora/commit_graph/e8f9981443c34ece02bca3c66130f3429d7b3375 new file mode 100644 index 00000000000..09fe9ddc0ac Binary files /dev/null and b/fuzzers/corpora/commit_graph/e8f9981443c34ece02bca3c66130f3429d7b3375 differ diff --git a/fuzzers/corpora/commit_graph/e91ed5416bbcd1b03803197b99c08f42c9869139 b/fuzzers/corpora/commit_graph/e91ed5416bbcd1b03803197b99c08f42c9869139 new file mode 100644 index 00000000000..3267ebb7bf5 Binary files /dev/null and b/fuzzers/corpora/commit_graph/e91ed5416bbcd1b03803197b99c08f42c9869139 differ diff --git a/fuzzers/corpora/commit_graph/e94201cfa88df7b198abd3abae9007a6780b52a7 b/fuzzers/corpora/commit_graph/e94201cfa88df7b198abd3abae9007a6780b52a7 new file mode 100644 index 00000000000..0279a3c3bd6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/e94201cfa88df7b198abd3abae9007a6780b52a7 differ diff --git a/fuzzers/corpora/commit_graph/e967bbd6a0d251ae62c9c38b784271d707f792c0 b/fuzzers/corpora/commit_graph/e967bbd6a0d251ae62c9c38b784271d707f792c0 new file mode 100644 index 00000000000..2baf0508151 Binary files /dev/null and b/fuzzers/corpora/commit_graph/e967bbd6a0d251ae62c9c38b784271d707f792c0 differ diff --git a/fuzzers/corpora/commit_graph/ea01737ceed783b3e0f66d9d0c409cb496c1d526 b/fuzzers/corpora/commit_graph/ea01737ceed783b3e0f66d9d0c409cb496c1d526 new file mode 100644 index 00000000000..75c988e03d7 Binary files /dev/null and b/fuzzers/corpora/commit_graph/ea01737ceed783b3e0f66d9d0c409cb496c1d526 differ diff --git a/fuzzers/corpora/commit_graph/ea40f7879a58d1e52a46404c761f76a949e14a31 b/fuzzers/corpora/commit_graph/ea40f7879a58d1e52a46404c761f76a949e14a31 new file mode 100644 index 00000000000..0fe4b3006cb Binary files /dev/null and b/fuzzers/corpora/commit_graph/ea40f7879a58d1e52a46404c761f76a949e14a31 differ diff --git a/fuzzers/corpora/commit_graph/ea5ad04a54f95963baea1f47845847626e08dd55 b/fuzzers/corpora/commit_graph/ea5ad04a54f95963baea1f47845847626e08dd55 new file mode 100644 index 00000000000..79a1c4f809d Binary files /dev/null and b/fuzzers/corpora/commit_graph/ea5ad04a54f95963baea1f47845847626e08dd55 differ diff --git a/fuzzers/corpora/commit_graph/ea608a401f54b0ca70e42b897f0c8ce6efdbc0ef b/fuzzers/corpora/commit_graph/ea608a401f54b0ca70e42b897f0c8ce6efdbc0ef new file mode 100644 index 00000000000..26c55fe6e38 Binary files /dev/null and b/fuzzers/corpora/commit_graph/ea608a401f54b0ca70e42b897f0c8ce6efdbc0ef differ diff --git a/fuzzers/corpora/commit_graph/eb8700d6b3728e6e70c2a0fe504543771639f2b6 b/fuzzers/corpora/commit_graph/eb8700d6b3728e6e70c2a0fe504543771639f2b6 new file mode 100644 index 00000000000..cf359369b28 Binary files /dev/null and b/fuzzers/corpora/commit_graph/eb8700d6b3728e6e70c2a0fe504543771639f2b6 differ diff --git a/fuzzers/corpora/commit_graph/ec1f271b04c322353865f4819153d46df7def873 b/fuzzers/corpora/commit_graph/ec1f271b04c322353865f4819153d46df7def873 new file mode 100644 index 00000000000..95cfa61ffd0 Binary files /dev/null and b/fuzzers/corpora/commit_graph/ec1f271b04c322353865f4819153d46df7def873 differ diff --git a/fuzzers/corpora/commit_graph/ee215536e7f0cfbd07b53dd65c5af9a604a01830 b/fuzzers/corpora/commit_graph/ee215536e7f0cfbd07b53dd65c5af9a604a01830 new file mode 100644 index 00000000000..82241f0004a Binary files /dev/null and b/fuzzers/corpora/commit_graph/ee215536e7f0cfbd07b53dd65c5af9a604a01830 differ diff --git a/fuzzers/corpora/commit_graph/ee4d4393d7d79b755f85ef5bf8f6e3d743bfa258 b/fuzzers/corpora/commit_graph/ee4d4393d7d79b755f85ef5bf8f6e3d743bfa258 new file mode 100644 index 00000000000..3d0d7d45d9c Binary files /dev/null and b/fuzzers/corpora/commit_graph/ee4d4393d7d79b755f85ef5bf8f6e3d743bfa258 differ diff --git a/fuzzers/corpora/commit_graph/ee8099331b2c392e7e036ffcd4a9b36ec2c2082d b/fuzzers/corpora/commit_graph/ee8099331b2c392e7e036ffcd4a9b36ec2c2082d new file mode 100644 index 00000000000..4e8f26bc199 Binary files /dev/null and b/fuzzers/corpora/commit_graph/ee8099331b2c392e7e036ffcd4a9b36ec2c2082d differ diff --git a/fuzzers/corpora/commit_graph/eede9da76db25513f8347f972e170102831de91a b/fuzzers/corpora/commit_graph/eede9da76db25513f8347f972e170102831de91a new file mode 100644 index 00000000000..d3646058fc6 Binary files /dev/null and b/fuzzers/corpora/commit_graph/eede9da76db25513f8347f972e170102831de91a differ diff --git a/fuzzers/corpora/commit_graph/ef707cdeaa9548b6c820f769c1d8ad607b3c4514 b/fuzzers/corpora/commit_graph/ef707cdeaa9548b6c820f769c1d8ad607b3c4514 new file mode 100644 index 00000000000..31daa3b3108 Binary files /dev/null and b/fuzzers/corpora/commit_graph/ef707cdeaa9548b6c820f769c1d8ad607b3c4514 differ diff --git a/fuzzers/corpora/commit_graph/ef98609d8196dc158365dfcbbc47e3d1699c50c2 b/fuzzers/corpora/commit_graph/ef98609d8196dc158365dfcbbc47e3d1699c50c2 new file mode 100644 index 00000000000..6cac8493c4b Binary files /dev/null and b/fuzzers/corpora/commit_graph/ef98609d8196dc158365dfcbbc47e3d1699c50c2 differ diff --git a/fuzzers/corpora/commit_graph/efa38b4269f978f3714b44b501831bea678244e0 b/fuzzers/corpora/commit_graph/efa38b4269f978f3714b44b501831bea678244e0 new file mode 100644 index 00000000000..923b8c514af Binary files /dev/null and b/fuzzers/corpora/commit_graph/efa38b4269f978f3714b44b501831bea678244e0 differ diff --git a/fuzzers/corpora/commit_graph/efba428e29811d233720ccaaf41966a309312a29 b/fuzzers/corpora/commit_graph/efba428e29811d233720ccaaf41966a309312a29 new file mode 100644 index 00000000000..b803c2ea1b7 Binary files /dev/null and b/fuzzers/corpora/commit_graph/efba428e29811d233720ccaaf41966a309312a29 differ diff --git a/fuzzers/corpora/commit_graph/efd514f056d8d83498b4724249c4623560e0390d b/fuzzers/corpora/commit_graph/efd514f056d8d83498b4724249c4623560e0390d new file mode 100644 index 00000000000..d0e1a33d4f4 Binary files /dev/null and b/fuzzers/corpora/commit_graph/efd514f056d8d83498b4724249c4623560e0390d differ diff --git a/fuzzers/corpora/commit_graph/f00e449ba67ef15e7f29df1e6948c28155d72baa b/fuzzers/corpora/commit_graph/f00e449ba67ef15e7f29df1e6948c28155d72baa new file mode 100644 index 00000000000..32153f74d24 Binary files /dev/null and b/fuzzers/corpora/commit_graph/f00e449ba67ef15e7f29df1e6948c28155d72baa differ diff --git a/fuzzers/corpora/commit_graph/f0a83929d588466051dced6eae0c387db307d646 b/fuzzers/corpora/commit_graph/f0a83929d588466051dced6eae0c387db307d646 new file mode 100644 index 00000000000..fc53a85f4af Binary files /dev/null and b/fuzzers/corpora/commit_graph/f0a83929d588466051dced6eae0c387db307d646 differ diff --git a/fuzzers/corpora/commit_graph/f0e53b72e5d69467e7c014474028ea734f4fcb26 b/fuzzers/corpora/commit_graph/f0e53b72e5d69467e7c014474028ea734f4fcb26 new file mode 100644 index 00000000000..bad38e83bce Binary files /dev/null and b/fuzzers/corpora/commit_graph/f0e53b72e5d69467e7c014474028ea734f4fcb26 differ diff --git a/fuzzers/corpora/commit_graph/f186265b3f10f4383f4174e9fb74f0a0cdfa3fca b/fuzzers/corpora/commit_graph/f186265b3f10f4383f4174e9fb74f0a0cdfa3fca new file mode 100644 index 00000000000..2cc2dd040c1 Binary files /dev/null and b/fuzzers/corpora/commit_graph/f186265b3f10f4383f4174e9fb74f0a0cdfa3fca differ diff --git a/fuzzers/corpora/commit_graph/f18932fcce5a9db5d6c8f59d622eabc25e255e12 b/fuzzers/corpora/commit_graph/f18932fcce5a9db5d6c8f59d622eabc25e255e12 new file mode 100644 index 00000000000..85c6e0eac67 Binary files /dev/null and b/fuzzers/corpora/commit_graph/f18932fcce5a9db5d6c8f59d622eabc25e255e12 differ diff --git a/fuzzers/corpora/commit_graph/f2ea163bddb95d67597e2a747779ebf4651cb2a9 b/fuzzers/corpora/commit_graph/f2ea163bddb95d67597e2a747779ebf4651cb2a9 new file mode 100644 index 00000000000..f974087cb92 Binary files /dev/null and b/fuzzers/corpora/commit_graph/f2ea163bddb95d67597e2a747779ebf4651cb2a9 differ diff --git a/fuzzers/corpora/commit_graph/f2f7d48a6d86143ecb4969808d634163576065b1 b/fuzzers/corpora/commit_graph/f2f7d48a6d86143ecb4969808d634163576065b1 new file mode 100644 index 00000000000..f2ad4a6843b Binary files /dev/null and b/fuzzers/corpora/commit_graph/f2f7d48a6d86143ecb4969808d634163576065b1 differ diff --git a/fuzzers/corpora/commit_graph/f34a833faf2b0dcbae8aaad142c76c7c7e534e99 b/fuzzers/corpora/commit_graph/f34a833faf2b0dcbae8aaad142c76c7c7e534e99 new file mode 100644 index 00000000000..2eaa521d6c2 Binary files /dev/null and b/fuzzers/corpora/commit_graph/f34a833faf2b0dcbae8aaad142c76c7c7e534e99 differ diff --git a/fuzzers/corpora/commit_graph/f5c044ce01645c069334698fb8c4750e44835912 b/fuzzers/corpora/commit_graph/f5c044ce01645c069334698fb8c4750e44835912 new file mode 100644 index 00000000000..a67affa6ccb Binary files /dev/null and b/fuzzers/corpora/commit_graph/f5c044ce01645c069334698fb8c4750e44835912 differ diff --git a/fuzzers/corpora/commit_graph/f680112645c2502f0612e9d017bbb50cb28affbf b/fuzzers/corpora/commit_graph/f680112645c2502f0612e9d017bbb50cb28affbf new file mode 100644 index 00000000000..dec09a84016 Binary files /dev/null and b/fuzzers/corpora/commit_graph/f680112645c2502f0612e9d017bbb50cb28affbf differ diff --git a/fuzzers/corpora/commit_graph/f6b778d1b34415a7715905f54968c8b6eb057912 b/fuzzers/corpora/commit_graph/f6b778d1b34415a7715905f54968c8b6eb057912 new file mode 100644 index 00000000000..a93cb662bda Binary files /dev/null and b/fuzzers/corpora/commit_graph/f6b778d1b34415a7715905f54968c8b6eb057912 differ diff --git a/fuzzers/corpora/commit_graph/f6ca6a62dc885c6b2a4b40c4aa1a7cb8118e30bb b/fuzzers/corpora/commit_graph/f6ca6a62dc885c6b2a4b40c4aa1a7cb8118e30bb new file mode 100644 index 00000000000..29ed04c9c46 Binary files /dev/null and b/fuzzers/corpora/commit_graph/f6ca6a62dc885c6b2a4b40c4aa1a7cb8118e30bb differ diff --git a/fuzzers/corpora/commit_graph/f733a8770c23fde182d2fef7e0d96e67244274d5 b/fuzzers/corpora/commit_graph/f733a8770c23fde182d2fef7e0d96e67244274d5 new file mode 100644 index 00000000000..c6aa7585e78 Binary files /dev/null and b/fuzzers/corpora/commit_graph/f733a8770c23fde182d2fef7e0d96e67244274d5 differ diff --git a/fuzzers/corpora/commit_graph/f8529ddf17d4505c0932c3d40abe33cbfd8c6f22 b/fuzzers/corpora/commit_graph/f8529ddf17d4505c0932c3d40abe33cbfd8c6f22 new file mode 100644 index 00000000000..f004ecbb759 Binary files /dev/null and b/fuzzers/corpora/commit_graph/f8529ddf17d4505c0932c3d40abe33cbfd8c6f22 differ diff --git a/fuzzers/corpora/commit_graph/f96f8419a3fc3719ae86d64e1147e7b7f66a2470 b/fuzzers/corpora/commit_graph/f96f8419a3fc3719ae86d64e1147e7b7f66a2470 new file mode 100644 index 00000000000..5dee3ca7c35 Binary files /dev/null and b/fuzzers/corpora/commit_graph/f96f8419a3fc3719ae86d64e1147e7b7f66a2470 differ diff --git a/fuzzers/corpora/commit_graph/fae241a6c87af37781a3b49e534b7ddb6636eda8 b/fuzzers/corpora/commit_graph/fae241a6c87af37781a3b49e534b7ddb6636eda8 new file mode 100644 index 00000000000..fc4e26bfd21 Binary files /dev/null and b/fuzzers/corpora/commit_graph/fae241a6c87af37781a3b49e534b7ddb6636eda8 differ diff --git a/fuzzers/corpora/commit_graph/faf8817a04b77c6a976ab0a3d1e905f79bb7f799 b/fuzzers/corpora/commit_graph/faf8817a04b77c6a976ab0a3d1e905f79bb7f799 new file mode 100644 index 00000000000..5164ecb6a5b Binary files /dev/null and b/fuzzers/corpora/commit_graph/faf8817a04b77c6a976ab0a3d1e905f79bb7f799 differ diff --git a/fuzzers/corpora/commit_graph/fb3e769019fb25d384d4be9d38e4cbce00a6adbc b/fuzzers/corpora/commit_graph/fb3e769019fb25d384d4be9d38e4cbce00a6adbc new file mode 100644 index 00000000000..008337c8d18 Binary files /dev/null and b/fuzzers/corpora/commit_graph/fb3e769019fb25d384d4be9d38e4cbce00a6adbc differ diff --git a/fuzzers/corpora/commit_graph/fb9b4b2a46f1c65076340a7bd03b076eb101b760 b/fuzzers/corpora/commit_graph/fb9b4b2a46f1c65076340a7bd03b076eb101b760 new file mode 100644 index 00000000000..8d0c735e40e Binary files /dev/null and b/fuzzers/corpora/commit_graph/fb9b4b2a46f1c65076340a7bd03b076eb101b760 differ diff --git a/fuzzers/corpora/commit_graph/fca9b0a398832c9ba02cdc811f625b97d5beb18e b/fuzzers/corpora/commit_graph/fca9b0a398832c9ba02cdc811f625b97d5beb18e new file mode 100644 index 00000000000..b2681f0c400 Binary files /dev/null and b/fuzzers/corpora/commit_graph/fca9b0a398832c9ba02cdc811f625b97d5beb18e differ diff --git a/fuzzers/corpora/commit_graph/fcb1b42c706e61245d5e86f708be777ae63f2772 b/fuzzers/corpora/commit_graph/fcb1b42c706e61245d5e86f708be777ae63f2772 new file mode 100644 index 00000000000..a98b66141ed Binary files /dev/null and b/fuzzers/corpora/commit_graph/fcb1b42c706e61245d5e86f708be777ae63f2772 differ diff --git a/fuzzers/corpora/commit_graph/fd6c463e7c30b0e51198c0d1ebbea25f20145e3f b/fuzzers/corpora/commit_graph/fd6c463e7c30b0e51198c0d1ebbea25f20145e3f new file mode 100644 index 00000000000..b25332139cc Binary files /dev/null and b/fuzzers/corpora/commit_graph/fd6c463e7c30b0e51198c0d1ebbea25f20145e3f differ diff --git a/fuzzers/corpora/commit_graph/fdcbaa49097ad120c6d7709b29d5b65b8cf8e719 b/fuzzers/corpora/commit_graph/fdcbaa49097ad120c6d7709b29d5b65b8cf8e719 new file mode 100644 index 00000000000..2167806d627 Binary files /dev/null and b/fuzzers/corpora/commit_graph/fdcbaa49097ad120c6d7709b29d5b65b8cf8e719 differ diff --git a/fuzzers/corpora/commit_graph/fe46775b28a2923b8770b44381552a8a1560d875 b/fuzzers/corpora/commit_graph/fe46775b28a2923b8770b44381552a8a1560d875 new file mode 100644 index 00000000000..0acef6edc76 Binary files /dev/null and b/fuzzers/corpora/commit_graph/fe46775b28a2923b8770b44381552a8a1560d875 differ diff --git a/fuzzers/corpora/commit_graph/ff04441135ef3308fec2687cf688069c6df8aa31 b/fuzzers/corpora/commit_graph/ff04441135ef3308fec2687cf688069c6df8aa31 new file mode 100644 index 00000000000..33afa05c6fd Binary files /dev/null and b/fuzzers/corpora/commit_graph/ff04441135ef3308fec2687cf688069c6df8aa31 differ diff --git a/fuzzers/corpora/config_file/git2.dat b/fuzzers/corpora/config_file/git2.dat new file mode 100644 index 00000000000..e5561545fd8 --- /dev/null +++ b/fuzzers/corpora/config_file/git2.dat @@ -0,0 +1,11 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true +[remote "origin"] + url = git@github.com:libgit2/libgit2 + fetch = +refs/heads/*:refs/remotes/origin/* +[branch "master"] + remote = origin + merge = refs/heads/master diff --git a/fuzzers/corpora/download_refs/clone.dat b/fuzzers/corpora/download_refs/clone.dat new file mode 100644 index 00000000000..f3e56b06b4b Binary files /dev/null and b/fuzzers/corpora/download_refs/clone.dat differ diff --git a/fuzzers/corpora/midx/037cbbe0dc03807dd9d9e8629f1712d7df34ee18 b/fuzzers/corpora/midx/037cbbe0dc03807dd9d9e8629f1712d7df34ee18 new file mode 100644 index 00000000000..2e5bda8c607 Binary files /dev/null and b/fuzzers/corpora/midx/037cbbe0dc03807dd9d9e8629f1712d7df34ee18 differ diff --git a/fuzzers/corpora/midx/039ee34fef8f323ed618a10abc0109df123d0cb5 b/fuzzers/corpora/midx/039ee34fef8f323ed618a10abc0109df123d0cb5 new file mode 100644 index 00000000000..0c97a38f5f0 Binary files /dev/null and b/fuzzers/corpora/midx/039ee34fef8f323ed618a10abc0109df123d0cb5 differ diff --git a/fuzzers/corpora/midx/054ee2c82bdb6a170106eb5d35f21bde2119d584 b/fuzzers/corpora/midx/054ee2c82bdb6a170106eb5d35f21bde2119d584 new file mode 100644 index 00000000000..f7711e49623 --- /dev/null +++ b/fuzzers/corpora/midx/054ee2c82bdb6a170106eb5d35f21bde2119d584 @@ -0,0 +1 @@ +Ma \ No newline at end of file diff --git a/fuzzers/corpora/midx/055ca4cbc961ebf5fd5c922b4f73880d3fbfe39d b/fuzzers/corpora/midx/055ca4cbc961ebf5fd5c922b4f73880d3fbfe39d new file mode 100644 index 00000000000..adb91f935a7 Binary files /dev/null and b/fuzzers/corpora/midx/055ca4cbc961ebf5fd5c922b4f73880d3fbfe39d differ diff --git a/fuzzers/corpora/midx/05c4e5eb1b97bc9b6973921fcb30d4c5e2eb79e4 b/fuzzers/corpora/midx/05c4e5eb1b97bc9b6973921fcb30d4c5e2eb79e4 new file mode 100644 index 00000000000..1a53734a2a4 Binary files /dev/null and b/fuzzers/corpora/midx/05c4e5eb1b97bc9b6973921fcb30d4c5e2eb79e4 differ diff --git a/fuzzers/corpora/midx/0672eeda541a191cfc68d521a3c7ac0aac4057a6 b/fuzzers/corpora/midx/0672eeda541a191cfc68d521a3c7ac0aac4057a6 new file mode 100644 index 00000000000..5b6495587bb Binary files /dev/null and b/fuzzers/corpora/midx/0672eeda541a191cfc68d521a3c7ac0aac4057a6 differ diff --git a/fuzzers/corpora/midx/06a58d1bd5562a668ebf01ef297fd774e0e587a6 b/fuzzers/corpora/midx/06a58d1bd5562a668ebf01ef297fd774e0e587a6 new file mode 100644 index 00000000000..30e454a42e9 Binary files /dev/null and b/fuzzers/corpora/midx/06a58d1bd5562a668ebf01ef297fd774e0e587a6 differ diff --git a/fuzzers/corpora/midx/06bf7c2461ae1049030f31b83ae76babfcc20c83 b/fuzzers/corpora/midx/06bf7c2461ae1049030f31b83ae76babfcc20c83 new file mode 100644 index 00000000000..10751dc37eb Binary files /dev/null and b/fuzzers/corpora/midx/06bf7c2461ae1049030f31b83ae76babfcc20c83 differ diff --git a/fuzzers/corpora/midx/06c2db67ea65758d971346bfd6beaa61ed12f22c b/fuzzers/corpora/midx/06c2db67ea65758d971346bfd6beaa61ed12f22c new file mode 100644 index 00000000000..5641570e124 Binary files /dev/null and b/fuzzers/corpora/midx/06c2db67ea65758d971346bfd6beaa61ed12f22c differ diff --git a/fuzzers/corpora/midx/07f88eefaf12609b7370fe78b82be2955f1b41fd b/fuzzers/corpora/midx/07f88eefaf12609b7370fe78b82be2955f1b41fd new file mode 100644 index 00000000000..8e09bb462ea Binary files /dev/null and b/fuzzers/corpora/midx/07f88eefaf12609b7370fe78b82be2955f1b41fd differ diff --git a/fuzzers/corpora/midx/08495c5f3828a56c167de870d385c46ffdce03c5 b/fuzzers/corpora/midx/08495c5f3828a56c167de870d385c46ffdce03c5 new file mode 100644 index 00000000000..b4f569772be Binary files /dev/null and b/fuzzers/corpora/midx/08495c5f3828a56c167de870d385c46ffdce03c5 differ diff --git a/fuzzers/corpora/midx/08ec8594e5b35fb9e8e0726584f720154f0b2b5d b/fuzzers/corpora/midx/08ec8594e5b35fb9e8e0726584f720154f0b2b5d new file mode 100644 index 00000000000..772b4fd4fad Binary files /dev/null and b/fuzzers/corpora/midx/08ec8594e5b35fb9e8e0726584f720154f0b2b5d differ diff --git a/fuzzers/corpora/midx/0903e378a493c596298074d6bff8de7f9ac25aa7 b/fuzzers/corpora/midx/0903e378a493c596298074d6bff8de7f9ac25aa7 new file mode 100644 index 00000000000..34f5f3b25b6 --- /dev/null +++ b/fuzzers/corpora/midx/0903e378a493c596298074d6bff8de7f9ac25aa7 @@ -0,0 +1 @@ +7 \ No newline at end of file diff --git a/fuzzers/corpora/midx/09144a846f90f894049ef8a0ed0cc7ab4588dc6c b/fuzzers/corpora/midx/09144a846f90f894049ef8a0ed0cc7ab4588dc6c new file mode 100644 index 00000000000..ce7a43d9cdc --- /dev/null +++ b/fuzzers/corpora/midx/09144a846f90f894049ef8a0ed0cc7ab4588dc6c @@ -0,0 +1 @@ +seed \ No newline at end of file diff --git a/fuzzers/corpora/midx/09b40dd618373bfe4d3f2838f686a70f645e640b b/fuzzers/corpora/midx/09b40dd618373bfe4d3f2838f686a70f645e640b new file mode 100644 index 00000000000..09473b7de2e Binary files /dev/null and b/fuzzers/corpora/midx/09b40dd618373bfe4d3f2838f686a70f645e640b differ diff --git a/fuzzers/corpora/midx/0a00ef44d234c18d365ec41724dbf4f21b09d0c5 b/fuzzers/corpora/midx/0a00ef44d234c18d365ec41724dbf4f21b09d0c5 new file mode 100644 index 00000000000..861a9869a6b Binary files /dev/null and b/fuzzers/corpora/midx/0a00ef44d234c18d365ec41724dbf4f21b09d0c5 differ diff --git a/fuzzers/corpora/midx/0a94e9f4a9b8cf56d52a9e3e7f2fa9a0a5c80d30 b/fuzzers/corpora/midx/0a94e9f4a9b8cf56d52a9e3e7f2fa9a0a5c80d30 new file mode 100644 index 00000000000..11f08c02d9b Binary files /dev/null and b/fuzzers/corpora/midx/0a94e9f4a9b8cf56d52a9e3e7f2fa9a0a5c80d30 differ diff --git a/fuzzers/corpora/midx/0b35a123104b7872a7f15a710a23ef3594ace04d b/fuzzers/corpora/midx/0b35a123104b7872a7f15a710a23ef3594ace04d new file mode 100644 index 00000000000..eac151bd65a Binary files /dev/null and b/fuzzers/corpora/midx/0b35a123104b7872a7f15a710a23ef3594ace04d differ diff --git a/fuzzers/corpora/midx/0c3d7e6be32c014ea873440b0f095961d391af1a b/fuzzers/corpora/midx/0c3d7e6be32c014ea873440b0f095961d391af1a new file mode 100644 index 00000000000..e9c66219d37 Binary files /dev/null and b/fuzzers/corpora/midx/0c3d7e6be32c014ea873440b0f095961d391af1a differ diff --git a/fuzzers/corpora/midx/0c65de477b89afc312a7e89cde06f8a17f65bd54 b/fuzzers/corpora/midx/0c65de477b89afc312a7e89cde06f8a17f65bd54 new file mode 100644 index 00000000000..8f4b25ca54d Binary files /dev/null and b/fuzzers/corpora/midx/0c65de477b89afc312a7e89cde06f8a17f65bd54 differ diff --git a/fuzzers/corpora/midx/0c81d0f368e979d2a0eb4598cbf1c9283936ba0c b/fuzzers/corpora/midx/0c81d0f368e979d2a0eb4598cbf1c9283936ba0c new file mode 100644 index 00000000000..a2dd1636c4b Binary files /dev/null and b/fuzzers/corpora/midx/0c81d0f368e979d2a0eb4598cbf1c9283936ba0c differ diff --git a/fuzzers/corpora/midx/0c95a44ae995070a5279a2991c36de2251081460 b/fuzzers/corpora/midx/0c95a44ae995070a5279a2991c36de2251081460 new file mode 100644 index 00000000000..821b07bc90d Binary files /dev/null and b/fuzzers/corpora/midx/0c95a44ae995070a5279a2991c36de2251081460 differ diff --git a/fuzzers/corpora/midx/0de38e2cb13167df7d5a882570633596f64bc4f4 b/fuzzers/corpora/midx/0de38e2cb13167df7d5a882570633596f64bc4f4 new file mode 100644 index 00000000000..80a27f68208 Binary files /dev/null and b/fuzzers/corpora/midx/0de38e2cb13167df7d5a882570633596f64bc4f4 differ diff --git a/fuzzers/corpora/midx/0de96aa193045315457ade63c2614610c503db9e b/fuzzers/corpora/midx/0de96aa193045315457ade63c2614610c503db9e new file mode 100644 index 00000000000..342c6c9a6c3 Binary files /dev/null and b/fuzzers/corpora/midx/0de96aa193045315457ade63c2614610c503db9e differ diff --git a/fuzzers/corpora/midx/0e02deca2b16d71f8637933bd56dc8592ed9fdff b/fuzzers/corpora/midx/0e02deca2b16d71f8637933bd56dc8592ed9fdff new file mode 100644 index 00000000000..d3b5fe27f63 --- /dev/null +++ b/fuzzers/corpora/midx/0e02deca2b16d71f8637933bd56dc8592ed9fdff @@ -0,0 +1 @@ +H \ No newline at end of file diff --git a/fuzzers/corpora/midx/0e44fc9176fe2c1bae4209369da5bc057f54b2d2 b/fuzzers/corpora/midx/0e44fc9176fe2c1bae4209369da5bc057f54b2d2 new file mode 100644 index 00000000000..74690025f39 Binary files /dev/null and b/fuzzers/corpora/midx/0e44fc9176fe2c1bae4209369da5bc057f54b2d2 differ diff --git a/fuzzers/corpora/midx/0f6c5fc9b6a68835364bbef8937560ee5a481938 b/fuzzers/corpora/midx/0f6c5fc9b6a68835364bbef8937560ee5a481938 new file mode 100644 index 00000000000..309e2d84fad --- /dev/null +++ b/fuzzers/corpora/midx/0f6c5fc9b6a68835364bbef8937560ee5a481938 @@ -0,0 +1 @@ +* \ No newline at end of file diff --git a/fuzzers/corpora/midx/10d542d5c7da060a5f0664e21478a0d598e29844 b/fuzzers/corpora/midx/10d542d5c7da060a5f0664e21478a0d598e29844 new file mode 100644 index 00000000000..777d32aa047 Binary files /dev/null and b/fuzzers/corpora/midx/10d542d5c7da060a5f0664e21478a0d598e29844 differ diff --git a/fuzzers/corpora/midx/118735f7786ae6b4c2f6b36314ab1f2cafe9c3c8 b/fuzzers/corpora/midx/118735f7786ae6b4c2f6b36314ab1f2cafe9c3c8 new file mode 100644 index 00000000000..a91dbc9128c --- /dev/null +++ b/fuzzers/corpora/midx/118735f7786ae6b4c2f6b36314ab1f2cafe9c3c8 @@ -0,0 +1 @@ +ۊ \ No newline at end of file diff --git a/fuzzers/corpora/midx/119b58eb353aa344264005016297fb911510ea0d b/fuzzers/corpora/midx/119b58eb353aa344264005016297fb911510ea0d new file mode 100644 index 00000000000..b4883c29cdf Binary files /dev/null and b/fuzzers/corpora/midx/119b58eb353aa344264005016297fb911510ea0d differ diff --git a/fuzzers/corpora/midx/127626832c30d6d94bb29384c0fde7ac6bca75ec b/fuzzers/corpora/midx/127626832c30d6d94bb29384c0fde7ac6bca75ec new file mode 100644 index 00000000000..3a0fe06c96c Binary files /dev/null and b/fuzzers/corpora/midx/127626832c30d6d94bb29384c0fde7ac6bca75ec differ diff --git a/fuzzers/corpora/midx/1284f1a162588d4de87ca17149474644a0863b27 b/fuzzers/corpora/midx/1284f1a162588d4de87ca17149474644a0863b27 new file mode 100644 index 00000000000..03a9f977912 Binary files /dev/null and b/fuzzers/corpora/midx/1284f1a162588d4de87ca17149474644a0863b27 differ diff --git a/fuzzers/corpora/midx/1458599f19f1a967c787562bf8ec3e67677da9c8 b/fuzzers/corpora/midx/1458599f19f1a967c787562bf8ec3e67677da9c8 new file mode 100644 index 00000000000..e9dc36e9683 Binary files /dev/null and b/fuzzers/corpora/midx/1458599f19f1a967c787562bf8ec3e67677da9c8 differ diff --git a/fuzzers/corpora/midx/14ba6c1ddd05b22c6f2eae5f894721cd3efcbb16 b/fuzzers/corpora/midx/14ba6c1ddd05b22c6f2eae5f894721cd3efcbb16 new file mode 100644 index 00000000000..e0f281c6f38 Binary files /dev/null and b/fuzzers/corpora/midx/14ba6c1ddd05b22c6f2eae5f894721cd3efcbb16 differ diff --git a/fuzzers/corpora/midx/158cdc0a5aa005f167a8588d0beed9eee4aa36f2 b/fuzzers/corpora/midx/158cdc0a5aa005f167a8588d0beed9eee4aa36f2 new file mode 100644 index 00000000000..98de253d87c Binary files /dev/null and b/fuzzers/corpora/midx/158cdc0a5aa005f167a8588d0beed9eee4aa36f2 differ diff --git a/fuzzers/corpora/midx/15dafc6fa800327f694b5eb2fc4ebf007be9c117 b/fuzzers/corpora/midx/15dafc6fa800327f694b5eb2fc4ebf007be9c117 new file mode 100644 index 00000000000..ff07ca2bd24 Binary files /dev/null and b/fuzzers/corpora/midx/15dafc6fa800327f694b5eb2fc4ebf007be9c117 differ diff --git a/fuzzers/corpora/midx/1613ed4b2e909871f8897fd6354ff80a4ac12f87 b/fuzzers/corpora/midx/1613ed4b2e909871f8897fd6354ff80a4ac12f87 new file mode 100644 index 00000000000..1afb32e2802 Binary files /dev/null and b/fuzzers/corpora/midx/1613ed4b2e909871f8897fd6354ff80a4ac12f87 differ diff --git a/fuzzers/corpora/midx/16daf4cb967bb47cf4566e9be7d96d3125bd2e12 b/fuzzers/corpora/midx/16daf4cb967bb47cf4566e9be7d96d3125bd2e12 new file mode 100644 index 00000000000..729b22a63fc Binary files /dev/null and b/fuzzers/corpora/midx/16daf4cb967bb47cf4566e9be7d96d3125bd2e12 differ diff --git a/fuzzers/corpora/midx/177783dce78efee878f6d6020fd87ab107bb11a1 b/fuzzers/corpora/midx/177783dce78efee878f6d6020fd87ab107bb11a1 new file mode 100644 index 00000000000..cc1810e6cad Binary files /dev/null and b/fuzzers/corpora/midx/177783dce78efee878f6d6020fd87ab107bb11a1 differ diff --git a/fuzzers/corpora/midx/17a5090400a1fedc45070e4b530a26f320a89097 b/fuzzers/corpora/midx/17a5090400a1fedc45070e4b530a26f320a89097 new file mode 100644 index 00000000000..7255c857748 Binary files /dev/null and b/fuzzers/corpora/midx/17a5090400a1fedc45070e4b530a26f320a89097 differ diff --git a/fuzzers/corpora/midx/17dea5cfa498f4d54384289a1daed0d15a85e7cc b/fuzzers/corpora/midx/17dea5cfa498f4d54384289a1daed0d15a85e7cc new file mode 100644 index 00000000000..00572bbeaaf Binary files /dev/null and b/fuzzers/corpora/midx/17dea5cfa498f4d54384289a1daed0d15a85e7cc differ diff --git a/fuzzers/corpora/midx/17e76ae5b54316679981113f52c27edc87dbcdea b/fuzzers/corpora/midx/17e76ae5b54316679981113f52c27edc87dbcdea new file mode 100644 index 00000000000..5f0e0d56c71 Binary files /dev/null and b/fuzzers/corpora/midx/17e76ae5b54316679981113f52c27edc87dbcdea differ diff --git a/fuzzers/corpora/midx/191ed5e9334693c53fc843f692dbc3c2c63e8241 b/fuzzers/corpora/midx/191ed5e9334693c53fc843f692dbc3c2c63e8241 new file mode 100644 index 00000000000..17fddd8d131 Binary files /dev/null and b/fuzzers/corpora/midx/191ed5e9334693c53fc843f692dbc3c2c63e8241 differ diff --git a/fuzzers/corpora/midx/196a0ba4edb5bbfd66c1cda669abf0496573cf0e b/fuzzers/corpora/midx/196a0ba4edb5bbfd66c1cda669abf0496573cf0e new file mode 100644 index 00000000000..4d685869bbd Binary files /dev/null and b/fuzzers/corpora/midx/196a0ba4edb5bbfd66c1cda669abf0496573cf0e differ diff --git a/fuzzers/corpora/midx/19742b6cee79fa5bf9b27dcbe367c82d0a399904 b/fuzzers/corpora/midx/19742b6cee79fa5bf9b27dcbe367c82d0a399904 new file mode 100644 index 00000000000..39e705417e1 Binary files /dev/null and b/fuzzers/corpora/midx/19742b6cee79fa5bf9b27dcbe367c82d0a399904 differ diff --git a/fuzzers/corpora/midx/1a21d7581d3b0a8d67934d48e91d45bd818836e8 b/fuzzers/corpora/midx/1a21d7581d3b0a8d67934d48e91d45bd818836e8 new file mode 100644 index 00000000000..616b808d9bb Binary files /dev/null and b/fuzzers/corpora/midx/1a21d7581d3b0a8d67934d48e91d45bd818836e8 differ diff --git a/fuzzers/corpora/midx/1b2f96c5d75c7ca09b1012be4e6c3a7b248ed924 b/fuzzers/corpora/midx/1b2f96c5d75c7ca09b1012be4e6c3a7b248ed924 new file mode 100644 index 00000000000..148aad96809 Binary files /dev/null and b/fuzzers/corpora/midx/1b2f96c5d75c7ca09b1012be4e6c3a7b248ed924 differ diff --git a/fuzzers/corpora/midx/1b604ff0683d0e23dc7945431f6514ba30d6ca0d b/fuzzers/corpora/midx/1b604ff0683d0e23dc7945431f6514ba30d6ca0d new file mode 100644 index 00000000000..1456048aced Binary files /dev/null and b/fuzzers/corpora/midx/1b604ff0683d0e23dc7945431f6514ba30d6ca0d differ diff --git a/fuzzers/corpora/midx/1b771dd5bd3ae2b1c42c4efe6c896c83b88a4f91 b/fuzzers/corpora/midx/1b771dd5bd3ae2b1c42c4efe6c896c83b88a4f91 new file mode 100644 index 00000000000..92f6376627d Binary files /dev/null and b/fuzzers/corpora/midx/1b771dd5bd3ae2b1c42c4efe6c896c83b88a4f91 differ diff --git a/fuzzers/corpora/midx/1b793a4ee73fa8bf423da70fca5f39ef32a8d288 b/fuzzers/corpora/midx/1b793a4ee73fa8bf423da70fca5f39ef32a8d288 new file mode 100644 index 00000000000..8c4478cfcd8 Binary files /dev/null and b/fuzzers/corpora/midx/1b793a4ee73fa8bf423da70fca5f39ef32a8d288 differ diff --git a/fuzzers/corpora/midx/1c9599ce00978780519272be279f508c402e3268 b/fuzzers/corpora/midx/1c9599ce00978780519272be279f508c402e3268 new file mode 100644 index 00000000000..c70f1282866 --- /dev/null +++ b/fuzzers/corpora/midx/1c9599ce00978780519272be279f508c402e3268 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/midx/1cc0068f9f63b12dc8fdd38f9ebfb96c42664e95 b/fuzzers/corpora/midx/1cc0068f9f63b12dc8fdd38f9ebfb96c42664e95 new file mode 100644 index 00000000000..c41e6a879ef Binary files /dev/null and b/fuzzers/corpora/midx/1cc0068f9f63b12dc8fdd38f9ebfb96c42664e95 differ diff --git a/fuzzers/corpora/midx/1de6e1f5579da6e5c40f4ee23ac62e29e4f90541 b/fuzzers/corpora/midx/1de6e1f5579da6e5c40f4ee23ac62e29e4f90541 new file mode 100644 index 00000000000..d8c3fbf0a35 Binary files /dev/null and b/fuzzers/corpora/midx/1de6e1f5579da6e5c40f4ee23ac62e29e4f90541 differ diff --git a/fuzzers/corpora/midx/1eec93083260ebfab5f4c6d13119cf27c374b7e9 b/fuzzers/corpora/midx/1eec93083260ebfab5f4c6d13119cf27c374b7e9 new file mode 100644 index 00000000000..5f9c27fac16 Binary files /dev/null and b/fuzzers/corpora/midx/1eec93083260ebfab5f4c6d13119cf27c374b7e9 differ diff --git a/fuzzers/corpora/midx/1f0f574addd363d1fed131289f301c5c033aaa8f b/fuzzers/corpora/midx/1f0f574addd363d1fed131289f301c5c033aaa8f new file mode 100644 index 00000000000..12cbb60c2da Binary files /dev/null and b/fuzzers/corpora/midx/1f0f574addd363d1fed131289f301c5c033aaa8f differ diff --git a/fuzzers/corpora/midx/1f3e85cffdb545c1ba7c8bbe1ca18ec13e341038 b/fuzzers/corpora/midx/1f3e85cffdb545c1ba7c8bbe1ca18ec13e341038 new file mode 100644 index 00000000000..36b2d5ea2a3 Binary files /dev/null and b/fuzzers/corpora/midx/1f3e85cffdb545c1ba7c8bbe1ca18ec13e341038 differ diff --git a/fuzzers/corpora/midx/1f6a66a92d5f083a73a82280a0a1ae0800e56ae5 b/fuzzers/corpora/midx/1f6a66a92d5f083a73a82280a0a1ae0800e56ae5 new file mode 100644 index 00000000000..ea50ac7be46 Binary files /dev/null and b/fuzzers/corpora/midx/1f6a66a92d5f083a73a82280a0a1ae0800e56ae5 differ diff --git a/fuzzers/corpora/midx/208e422322052efcdaeb1a09bbf06c5f476b8efc b/fuzzers/corpora/midx/208e422322052efcdaeb1a09bbf06c5f476b8efc new file mode 100644 index 00000000000..ce98394b385 Binary files /dev/null and b/fuzzers/corpora/midx/208e422322052efcdaeb1a09bbf06c5f476b8efc differ diff --git a/fuzzers/corpora/midx/22d75b2c3937957b14eded621b638283ce7fe1fe b/fuzzers/corpora/midx/22d75b2c3937957b14eded621b638283ce7fe1fe new file mode 100644 index 00000000000..0aa34c4ef92 Binary files /dev/null and b/fuzzers/corpora/midx/22d75b2c3937957b14eded621b638283ce7fe1fe differ diff --git a/fuzzers/corpora/midx/22f90ff68166a409acf8f89bf60a31ad2c64ab37 b/fuzzers/corpora/midx/22f90ff68166a409acf8f89bf60a31ad2c64ab37 new file mode 100644 index 00000000000..cdecbb60c94 Binary files /dev/null and b/fuzzers/corpora/midx/22f90ff68166a409acf8f89bf60a31ad2c64ab37 differ diff --git a/fuzzers/corpora/midx/236ebad449d432b039d6ace1f250ef1fa2aa364d b/fuzzers/corpora/midx/236ebad449d432b039d6ace1f250ef1fa2aa364d new file mode 100644 index 00000000000..0e213a171de Binary files /dev/null and b/fuzzers/corpora/midx/236ebad449d432b039d6ace1f250ef1fa2aa364d differ diff --git a/fuzzers/corpora/midx/252a4e4bf7fb21792ec2f305fd88fa7c9168505f b/fuzzers/corpora/midx/252a4e4bf7fb21792ec2f305fd88fa7c9168505f new file mode 100644 index 00000000000..b23555c9335 Binary files /dev/null and b/fuzzers/corpora/midx/252a4e4bf7fb21792ec2f305fd88fa7c9168505f differ diff --git a/fuzzers/corpora/midx/259e1faf7b7f12250062d36ded1193a9dbcae0f5 b/fuzzers/corpora/midx/259e1faf7b7f12250062d36ded1193a9dbcae0f5 new file mode 100644 index 00000000000..f2f1de5c695 Binary files /dev/null and b/fuzzers/corpora/midx/259e1faf7b7f12250062d36ded1193a9dbcae0f5 differ diff --git a/fuzzers/corpora/midx/25ad3dfb655ab4c853d0d277872310d9579c8e83 b/fuzzers/corpora/midx/25ad3dfb655ab4c853d0d277872310d9579c8e83 new file mode 100644 index 00000000000..325c385857b Binary files /dev/null and b/fuzzers/corpora/midx/25ad3dfb655ab4c853d0d277872310d9579c8e83 differ diff --git a/fuzzers/corpora/midx/26210f5b8fdbf81b312feea48659ec6e2e083c0b b/fuzzers/corpora/midx/26210f5b8fdbf81b312feea48659ec6e2e083c0b new file mode 100644 index 00000000000..479a291fac8 Binary files /dev/null and b/fuzzers/corpora/midx/26210f5b8fdbf81b312feea48659ec6e2e083c0b differ diff --git a/fuzzers/corpora/midx/263a2a0915be36d8cb2bc30774e37e0344262347 b/fuzzers/corpora/midx/263a2a0915be36d8cb2bc30774e37e0344262347 new file mode 100644 index 00000000000..8a907614046 Binary files /dev/null and b/fuzzers/corpora/midx/263a2a0915be36d8cb2bc30774e37e0344262347 differ diff --git a/fuzzers/corpora/midx/2679bfbc2f4f7c10a304245da4e156e235377b63 b/fuzzers/corpora/midx/2679bfbc2f4f7c10a304245da4e156e235377b63 new file mode 100644 index 00000000000..dbf598d817e Binary files /dev/null and b/fuzzers/corpora/midx/2679bfbc2f4f7c10a304245da4e156e235377b63 differ diff --git a/fuzzers/corpora/midx/270b7b567a63dd94bb2a90448bbbc2e2bbc4a261 b/fuzzers/corpora/midx/270b7b567a63dd94bb2a90448bbbc2e2bbc4a261 new file mode 100644 index 00000000000..0c08b8c13b6 Binary files /dev/null and b/fuzzers/corpora/midx/270b7b567a63dd94bb2a90448bbbc2e2bbc4a261 differ diff --git a/fuzzers/corpora/midx/271cd5c5e254a293d115588ee130040ef26b59e8 b/fuzzers/corpora/midx/271cd5c5e254a293d115588ee130040ef26b59e8 new file mode 100644 index 00000000000..89309dc1ace Binary files /dev/null and b/fuzzers/corpora/midx/271cd5c5e254a293d115588ee130040ef26b59e8 differ diff --git a/fuzzers/corpora/midx/27839a8035b48f8c19ab073808a03a95b6a90cc3 b/fuzzers/corpora/midx/27839a8035b48f8c19ab073808a03a95b6a90cc3 new file mode 100644 index 00000000000..459506950cb Binary files /dev/null and b/fuzzers/corpora/midx/27839a8035b48f8c19ab073808a03a95b6a90cc3 differ diff --git a/fuzzers/corpora/midx/2810c385c9285cbdb65bcdab5175999fe547cbad b/fuzzers/corpora/midx/2810c385c9285cbdb65bcdab5175999fe547cbad new file mode 100644 index 00000000000..0d3fc30b8f5 Binary files /dev/null and b/fuzzers/corpora/midx/2810c385c9285cbdb65bcdab5175999fe547cbad differ diff --git a/fuzzers/corpora/midx/28afaf4ab4b092ccf987661e58009f96126bba63 b/fuzzers/corpora/midx/28afaf4ab4b092ccf987661e58009f96126bba63 new file mode 100644 index 00000000000..6a29fb12c69 Binary files /dev/null and b/fuzzers/corpora/midx/28afaf4ab4b092ccf987661e58009f96126bba63 differ diff --git a/fuzzers/corpora/midx/29f842e86a891cff9f0b44c8aec19f7e23a47000 b/fuzzers/corpora/midx/29f842e86a891cff9f0b44c8aec19f7e23a47000 new file mode 100644 index 00000000000..5644eb8afa8 Binary files /dev/null and b/fuzzers/corpora/midx/29f842e86a891cff9f0b44c8aec19f7e23a47000 differ diff --git a/fuzzers/corpora/midx/2aa2549f617f19402d1feac61d4ca1af3545cc8a b/fuzzers/corpora/midx/2aa2549f617f19402d1feac61d4ca1af3545cc8a new file mode 100644 index 00000000000..77045850a38 Binary files /dev/null and b/fuzzers/corpora/midx/2aa2549f617f19402d1feac61d4ca1af3545cc8a differ diff --git a/fuzzers/corpora/midx/2b73c2902eda6da41321493601003b29c3445713 b/fuzzers/corpora/midx/2b73c2902eda6da41321493601003b29c3445713 new file mode 100644 index 00000000000..402d0664737 Binary files /dev/null and b/fuzzers/corpora/midx/2b73c2902eda6da41321493601003b29c3445713 differ diff --git a/fuzzers/corpora/midx/2bcec1274c5e7b2d7a581d851c016ef5b553fabe b/fuzzers/corpora/midx/2bcec1274c5e7b2d7a581d851c016ef5b553fabe new file mode 100644 index 00000000000..43e18c8cce4 Binary files /dev/null and b/fuzzers/corpora/midx/2bcec1274c5e7b2d7a581d851c016ef5b553fabe differ diff --git a/fuzzers/corpora/midx/2dd9a328b6d4e29e42684347be5c4b7cd7dc1a66 b/fuzzers/corpora/midx/2dd9a328b6d4e29e42684347be5c4b7cd7dc1a66 new file mode 100644 index 00000000000..a3e6da55cbe Binary files /dev/null and b/fuzzers/corpora/midx/2dd9a328b6d4e29e42684347be5c4b7cd7dc1a66 differ diff --git a/fuzzers/corpora/midx/2ddc17ee7ee89bb7dbc673328d5f3e55c76e686e b/fuzzers/corpora/midx/2ddc17ee7ee89bb7dbc673328d5f3e55c76e686e new file mode 100644 index 00000000000..7b789f3049d Binary files /dev/null and b/fuzzers/corpora/midx/2ddc17ee7ee89bb7dbc673328d5f3e55c76e686e differ diff --git a/fuzzers/corpora/midx/2f71d5e99dc93618ed99fdb7c244a8f5e4a7eb4a b/fuzzers/corpora/midx/2f71d5e99dc93618ed99fdb7c244a8f5e4a7eb4a new file mode 100644 index 00000000000..7d2f004ba2e Binary files /dev/null and b/fuzzers/corpora/midx/2f71d5e99dc93618ed99fdb7c244a8f5e4a7eb4a differ diff --git a/fuzzers/corpora/midx/2f7cd0154d71a83e7b104670b2a77fbd285ffde2 b/fuzzers/corpora/midx/2f7cd0154d71a83e7b104670b2a77fbd285ffde2 new file mode 100644 index 00000000000..645a39bff9d Binary files /dev/null and b/fuzzers/corpora/midx/2f7cd0154d71a83e7b104670b2a77fbd285ffde2 differ diff --git a/fuzzers/corpora/midx/2f9d40ef790f5213234e95d123dce942b2d1d389 b/fuzzers/corpora/midx/2f9d40ef790f5213234e95d123dce942b2d1d389 new file mode 100644 index 00000000000..bca234806e6 Binary files /dev/null and b/fuzzers/corpora/midx/2f9d40ef790f5213234e95d123dce942b2d1d389 differ diff --git a/fuzzers/corpora/midx/31577bacbca7017308d2a0c9ebfdd4fce513bbe4 b/fuzzers/corpora/midx/31577bacbca7017308d2a0c9ebfdd4fce513bbe4 new file mode 100644 index 00000000000..72972b16b71 Binary files /dev/null and b/fuzzers/corpora/midx/31577bacbca7017308d2a0c9ebfdd4fce513bbe4 differ diff --git a/fuzzers/corpora/midx/3278f1bab88b80597d0066812d49f8bd3c7b1dcf b/fuzzers/corpora/midx/3278f1bab88b80597d0066812d49f8bd3c7b1dcf new file mode 100644 index 00000000000..4177febbba4 Binary files /dev/null and b/fuzzers/corpora/midx/3278f1bab88b80597d0066812d49f8bd3c7b1dcf differ diff --git a/fuzzers/corpora/midx/328160cae6235605ff70951a2f6ac669ba7bb397 b/fuzzers/corpora/midx/328160cae6235605ff70951a2f6ac669ba7bb397 new file mode 100644 index 00000000000..1585907e012 Binary files /dev/null and b/fuzzers/corpora/midx/328160cae6235605ff70951a2f6ac669ba7bb397 differ diff --git a/fuzzers/corpora/midx/337ed1bf91701a4c8926840259077e55938c6efc b/fuzzers/corpora/midx/337ed1bf91701a4c8926840259077e55938c6efc new file mode 100644 index 00000000000..915128f16a7 Binary files /dev/null and b/fuzzers/corpora/midx/337ed1bf91701a4c8926840259077e55938c6efc differ diff --git a/fuzzers/corpora/midx/33a97d83ff7a774797b1751ea4bffbb4a22c58d9 b/fuzzers/corpora/midx/33a97d83ff7a774797b1751ea4bffbb4a22c58d9 new file mode 100644 index 00000000000..852d8dc19a2 Binary files /dev/null and b/fuzzers/corpora/midx/33a97d83ff7a774797b1751ea4bffbb4a22c58d9 differ diff --git a/fuzzers/corpora/midx/341021da9516401cf364ed2b7dfdda346db04f2f b/fuzzers/corpora/midx/341021da9516401cf364ed2b7dfdda346db04f2f new file mode 100644 index 00000000000..13c21ab3ca6 Binary files /dev/null and b/fuzzers/corpora/midx/341021da9516401cf364ed2b7dfdda346db04f2f differ diff --git a/fuzzers/corpora/midx/341773a439cdecc58f55fb205ac584cd93ffe0f2 b/fuzzers/corpora/midx/341773a439cdecc58f55fb205ac584cd93ffe0f2 new file mode 100644 index 00000000000..0446a8866e8 --- /dev/null +++ b/fuzzers/corpora/midx/341773a439cdecc58f55fb205ac584cd93ffe0f2 @@ -0,0 +1 @@ +yyyyyyyyyyyyy \ No newline at end of file diff --git a/fuzzers/corpora/midx/366091157510e40bca08fc2102b9018ccf4697de b/fuzzers/corpora/midx/366091157510e40bca08fc2102b9018ccf4697de new file mode 100644 index 00000000000..f2148a165e5 Binary files /dev/null and b/fuzzers/corpora/midx/366091157510e40bca08fc2102b9018ccf4697de differ diff --git a/fuzzers/corpora/midx/37096157e2f9f2ec8e0b97b21d335bd653f3edbd b/fuzzers/corpora/midx/37096157e2f9f2ec8e0b97b21d335bd653f3edbd new file mode 100644 index 00000000000..03600aa65c9 Binary files /dev/null and b/fuzzers/corpora/midx/37096157e2f9f2ec8e0b97b21d335bd653f3edbd differ diff --git a/fuzzers/corpora/midx/373a74b8613d09babcb567f91047e7b556a8de90 b/fuzzers/corpora/midx/373a74b8613d09babcb567f91047e7b556a8de90 new file mode 100644 index 00000000000..9427eb0c708 Binary files /dev/null and b/fuzzers/corpora/midx/373a74b8613d09babcb567f91047e7b556a8de90 differ diff --git a/fuzzers/corpora/midx/3748b07ee7bec7bdd202ee14222cefca182417d1 b/fuzzers/corpora/midx/3748b07ee7bec7bdd202ee14222cefca182417d1 new file mode 100644 index 00000000000..9699411c07e Binary files /dev/null and b/fuzzers/corpora/midx/3748b07ee7bec7bdd202ee14222cefca182417d1 differ diff --git a/fuzzers/corpora/midx/38b7906b9f956dca01dc92d0a901388ec1cbc8b1 b/fuzzers/corpora/midx/38b7906b9f956dca01dc92d0a901388ec1cbc8b1 new file mode 100644 index 00000000000..d7b26a33124 Binary files /dev/null and b/fuzzers/corpora/midx/38b7906b9f956dca01dc92d0a901388ec1cbc8b1 differ diff --git a/fuzzers/corpora/midx/38ddf3424559f1a6e7687eff8469a358184b833b b/fuzzers/corpora/midx/38ddf3424559f1a6e7687eff8469a358184b833b new file mode 100644 index 00000000000..972dd750dc1 --- /dev/null +++ b/fuzzers/corpora/midx/38ddf3424559f1a6e7687eff8469a358184b833b @@ -0,0 +1 @@ +D \ No newline at end of file diff --git a/fuzzers/corpora/midx/38e31d0a7dcc3835ce1a4afeeda8446fb3d7ed73 b/fuzzers/corpora/midx/38e31d0a7dcc3835ce1a4afeeda8446fb3d7ed73 new file mode 100644 index 00000000000..2afdc8fb564 Binary files /dev/null and b/fuzzers/corpora/midx/38e31d0a7dcc3835ce1a4afeeda8446fb3d7ed73 differ diff --git a/fuzzers/corpora/midx/3955ec4497b226391ef9eb40f38af6dee4fa26b7 b/fuzzers/corpora/midx/3955ec4497b226391ef9eb40f38af6dee4fa26b7 new file mode 100644 index 00000000000..cff10d8860e Binary files /dev/null and b/fuzzers/corpora/midx/3955ec4497b226391ef9eb40f38af6dee4fa26b7 differ diff --git a/fuzzers/corpora/midx/3b6b424342133feb0f587f22bcd8f21595c004e5 b/fuzzers/corpora/midx/3b6b424342133feb0f587f22bcd8f21595c004e5 new file mode 100644 index 00000000000..9f2bea4f5db Binary files /dev/null and b/fuzzers/corpora/midx/3b6b424342133feb0f587f22bcd8f21595c004e5 differ diff --git a/fuzzers/corpora/midx/3bb71f41200e0ebf8d19532e7d6e384c48aa2d03 b/fuzzers/corpora/midx/3bb71f41200e0ebf8d19532e7d6e384c48aa2d03 new file mode 100644 index 00000000000..28fa1334e44 Binary files /dev/null and b/fuzzers/corpora/midx/3bb71f41200e0ebf8d19532e7d6e384c48aa2d03 differ diff --git a/fuzzers/corpora/midx/3c5a6063797aba9ffe5ea9903bbfcf87193652d3 b/fuzzers/corpora/midx/3c5a6063797aba9ffe5ea9903bbfcf87193652d3 new file mode 100644 index 00000000000..4a5725e8106 Binary files /dev/null and b/fuzzers/corpora/midx/3c5a6063797aba9ffe5ea9903bbfcf87193652d3 differ diff --git a/fuzzers/corpora/midx/3dfb9927d959f2462f6944a32d080b60a265abfe b/fuzzers/corpora/midx/3dfb9927d959f2462f6944a32d080b60a265abfe new file mode 100644 index 00000000000..c234cd14913 Binary files /dev/null and b/fuzzers/corpora/midx/3dfb9927d959f2462f6944a32d080b60a265abfe differ diff --git a/fuzzers/corpora/midx/3e19242a63ec92a0c3f7138ebbc31bfe7cbd40cd b/fuzzers/corpora/midx/3e19242a63ec92a0c3f7138ebbc31bfe7cbd40cd new file mode 100644 index 00000000000..c0e1b1d4a8f Binary files /dev/null and b/fuzzers/corpora/midx/3e19242a63ec92a0c3f7138ebbc31bfe7cbd40cd differ diff --git a/fuzzers/corpora/midx/3ec53ce4ea1f41f040a3c2beed929572af95dd43 b/fuzzers/corpora/midx/3ec53ce4ea1f41f040a3c2beed929572af95dd43 new file mode 100644 index 00000000000..9a7ec6f798d Binary files /dev/null and b/fuzzers/corpora/midx/3ec53ce4ea1f41f040a3c2beed929572af95dd43 differ diff --git a/fuzzers/corpora/midx/3f0762fdf49a58c0d8fd6683964a85caddee391b b/fuzzers/corpora/midx/3f0762fdf49a58c0d8fd6683964a85caddee391b new file mode 100644 index 00000000000..aec9b3d42ad Binary files /dev/null and b/fuzzers/corpora/midx/3f0762fdf49a58c0d8fd6683964a85caddee391b differ diff --git a/fuzzers/corpora/midx/3f71ae863c4e9bac98e49a554b8ec4d78b17492d b/fuzzers/corpora/midx/3f71ae863c4e9bac98e49a554b8ec4d78b17492d new file mode 100644 index 00000000000..a9e06c79ddb --- /dev/null +++ b/fuzzers/corpora/midx/3f71ae863c4e9bac98e49a554b8ec4d78b17492d @@ -0,0 +1 @@ +* \ No newline at end of file diff --git a/fuzzers/corpora/midx/3f9df30bfb66a28fbe6f1951ef7ae4ca9f19fdf2 b/fuzzers/corpora/midx/3f9df30bfb66a28fbe6f1951ef7ae4ca9f19fdf2 new file mode 100644 index 00000000000..ebb1cde8653 Binary files /dev/null and b/fuzzers/corpora/midx/3f9df30bfb66a28fbe6f1951ef7ae4ca9f19fdf2 differ diff --git a/fuzzers/corpora/midx/3fabb14670c008c22094c1d7cd7b1e23b4c48b3d b/fuzzers/corpora/midx/3fabb14670c008c22094c1d7cd7b1e23b4c48b3d new file mode 100644 index 00000000000..1d2d0828c4b Binary files /dev/null and b/fuzzers/corpora/midx/3fabb14670c008c22094c1d7cd7b1e23b4c48b3d differ diff --git a/fuzzers/corpora/midx/408fba9c66c5d1deb31e4c69f1dd0677844dbc1b b/fuzzers/corpora/midx/408fba9c66c5d1deb31e4c69f1dd0677844dbc1b new file mode 100644 index 00000000000..1cded07043f Binary files /dev/null and b/fuzzers/corpora/midx/408fba9c66c5d1deb31e4c69f1dd0677844dbc1b differ diff --git a/fuzzers/corpora/midx/40ca8645081087e950ad61bccf8d43450366356e b/fuzzers/corpora/midx/40ca8645081087e950ad61bccf8d43450366356e new file mode 100644 index 00000000000..834daf8c9e5 Binary files /dev/null and b/fuzzers/corpora/midx/40ca8645081087e950ad61bccf8d43450366356e differ diff --git a/fuzzers/corpora/midx/412faec949b9d04498de939561664ee559a583a7 b/fuzzers/corpora/midx/412faec949b9d04498de939561664ee559a583a7 new file mode 100644 index 00000000000..f15b10e736e --- /dev/null +++ b/fuzzers/corpora/midx/412faec949b9d04498de939561664ee559a583a7 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/midx/4148bd5336f89e73b2a5416c67d491c0ec4a2b7d b/fuzzers/corpora/midx/4148bd5336f89e73b2a5416c67d491c0ec4a2b7d new file mode 100644 index 00000000000..894ac62eabe Binary files /dev/null and b/fuzzers/corpora/midx/4148bd5336f89e73b2a5416c67d491c0ec4a2b7d differ diff --git a/fuzzers/corpora/midx/41933e61fa20fbe2b190f9ae7ceae4a4b1220021 b/fuzzers/corpora/midx/41933e61fa20fbe2b190f9ae7ceae4a4b1220021 new file mode 100644 index 00000000000..727789d6c72 Binary files /dev/null and b/fuzzers/corpora/midx/41933e61fa20fbe2b190f9ae7ceae4a4b1220021 differ diff --git a/fuzzers/corpora/midx/423d90f3fc7ddc146095ec5a4b4f455aa876b69b b/fuzzers/corpora/midx/423d90f3fc7ddc146095ec5a4b4f455aa876b69b new file mode 100644 index 00000000000..fc73bd07f8a Binary files /dev/null and b/fuzzers/corpora/midx/423d90f3fc7ddc146095ec5a4b4f455aa876b69b differ diff --git a/fuzzers/corpora/midx/42a6c52249aa12cfef1db1bf302a483a01c972f3 b/fuzzers/corpora/midx/42a6c52249aa12cfef1db1bf302a483a01c972f3 new file mode 100644 index 00000000000..e35ab98be5e Binary files /dev/null and b/fuzzers/corpora/midx/42a6c52249aa12cfef1db1bf302a483a01c972f3 differ diff --git a/fuzzers/corpora/midx/42a82726f0e70da9b87b6c52bc1b3415576025f2 b/fuzzers/corpora/midx/42a82726f0e70da9b87b6c52bc1b3415576025f2 new file mode 100644 index 00000000000..46117b4a99a Binary files /dev/null and b/fuzzers/corpora/midx/42a82726f0e70da9b87b6c52bc1b3415576025f2 differ diff --git a/fuzzers/corpora/midx/4458e19f99e38c61ad9792b0b3bf8ac79f8236f1 b/fuzzers/corpora/midx/4458e19f99e38c61ad9792b0b3bf8ac79f8236f1 new file mode 100644 index 00000000000..80407ab450b --- /dev/null +++ b/fuzzers/corpora/midx/4458e19f99e38c61ad9792b0b3bf8ac79f8236f1 @@ -0,0 +1 @@ +]]s4 \ No newline at end of file diff --git a/fuzzers/corpora/midx/44a4411a8d6ed67ee3ea61d91d2afafe89295b0f b/fuzzers/corpora/midx/44a4411a8d6ed67ee3ea61d91d2afafe89295b0f new file mode 100644 index 00000000000..e9933b1faee Binary files /dev/null and b/fuzzers/corpora/midx/44a4411a8d6ed67ee3ea61d91d2afafe89295b0f differ diff --git a/fuzzers/corpora/midx/44e04754d1b6c0c045e05509dd7033d19a926b10 b/fuzzers/corpora/midx/44e04754d1b6c0c045e05509dd7033d19a926b10 new file mode 100644 index 00000000000..5fe8e2e841d Binary files /dev/null and b/fuzzers/corpora/midx/44e04754d1b6c0c045e05509dd7033d19a926b10 differ diff --git a/fuzzers/corpora/midx/45259e9f0a2cc7739a94eccaafb37c1570f73884 b/fuzzers/corpora/midx/45259e9f0a2cc7739a94eccaafb37c1570f73884 new file mode 100644 index 00000000000..90134d1daa2 Binary files /dev/null and b/fuzzers/corpora/midx/45259e9f0a2cc7739a94eccaafb37c1570f73884 differ diff --git a/fuzzers/corpora/midx/46c0d7e952200cabc08b9cd776a9f6759f4208c3 b/fuzzers/corpora/midx/46c0d7e952200cabc08b9cd776a9f6759f4208c3 new file mode 100644 index 00000000000..e3a7a837b8c Binary files /dev/null and b/fuzzers/corpora/midx/46c0d7e952200cabc08b9cd776a9f6759f4208c3 differ diff --git a/fuzzers/corpora/midx/46deac8631633ea3c32005124e20a2bc2bbabade b/fuzzers/corpora/midx/46deac8631633ea3c32005124e20a2bc2bbabade new file mode 100644 index 00000000000..5f54eacd69a Binary files /dev/null and b/fuzzers/corpora/midx/46deac8631633ea3c32005124e20a2bc2bbabade differ diff --git a/fuzzers/corpora/midx/46e7edf6e9d6cbcdabde3b48f1c4efd93be40348 b/fuzzers/corpora/midx/46e7edf6e9d6cbcdabde3b48f1c4efd93be40348 new file mode 100644 index 00000000000..0314f2c237a Binary files /dev/null and b/fuzzers/corpora/midx/46e7edf6e9d6cbcdabde3b48f1c4efd93be40348 differ diff --git a/fuzzers/corpora/midx/46fe9556c28c94f7321baa2519a3cbeabbd54d09 b/fuzzers/corpora/midx/46fe9556c28c94f7321baa2519a3cbeabbd54d09 new file mode 100644 index 00000000000..75e8e6fa7a0 Binary files /dev/null and b/fuzzers/corpora/midx/46fe9556c28c94f7321baa2519a3cbeabbd54d09 differ diff --git a/fuzzers/corpora/midx/49223681729e73b48b26a2262e4a66b2ba00e176 b/fuzzers/corpora/midx/49223681729e73b48b26a2262e4a66b2ba00e176 new file mode 100644 index 00000000000..3068c356cd3 Binary files /dev/null and b/fuzzers/corpora/midx/49223681729e73b48b26a2262e4a66b2ba00e176 differ diff --git a/fuzzers/corpora/midx/499e61b689f6cc7e4efb0631684739c2a6f97c7d b/fuzzers/corpora/midx/499e61b689f6cc7e4efb0631684739c2a6f97c7d new file mode 100644 index 00000000000..d3c735b3bcf Binary files /dev/null and b/fuzzers/corpora/midx/499e61b689f6cc7e4efb0631684739c2a6f97c7d differ diff --git a/fuzzers/corpora/midx/4a06ad8c4d717bd048a7a1315a3d609d70f0162d b/fuzzers/corpora/midx/4a06ad8c4d717bd048a7a1315a3d609d70f0162d new file mode 100644 index 00000000000..caef1688b8f Binary files /dev/null and b/fuzzers/corpora/midx/4a06ad8c4d717bd048a7a1315a3d609d70f0162d differ diff --git a/fuzzers/corpora/midx/4adb7d4791a4c6370478dff2eb987d715554bf09 b/fuzzers/corpora/midx/4adb7d4791a4c6370478dff2eb987d715554bf09 new file mode 100644 index 00000000000..ced14774691 Binary files /dev/null and b/fuzzers/corpora/midx/4adb7d4791a4c6370478dff2eb987d715554bf09 differ diff --git a/fuzzers/corpora/midx/4b01c479cdc9b750a31d5e7ac5004309222d218d b/fuzzers/corpora/midx/4b01c479cdc9b750a31d5e7ac5004309222d218d new file mode 100644 index 00000000000..4ea5a880050 Binary files /dev/null and b/fuzzers/corpora/midx/4b01c479cdc9b750a31d5e7ac5004309222d218d differ diff --git a/fuzzers/corpora/midx/4bce7460a6becba6d26984bb438d7d3aa4e4fc56 b/fuzzers/corpora/midx/4bce7460a6becba6d26984bb438d7d3aa4e4fc56 new file mode 100644 index 00000000000..41c7c3acec2 Binary files /dev/null and b/fuzzers/corpora/midx/4bce7460a6becba6d26984bb438d7d3aa4e4fc56 differ diff --git a/fuzzers/corpora/midx/4cc96483b6800dda296f00887b12a35154115090 b/fuzzers/corpora/midx/4cc96483b6800dda296f00887b12a35154115090 new file mode 100644 index 00000000000..4f0179da894 Binary files /dev/null and b/fuzzers/corpora/midx/4cc96483b6800dda296f00887b12a35154115090 differ diff --git a/fuzzers/corpora/midx/4f3aa59bae0619c9a06b631d9cb7767591810ab0 b/fuzzers/corpora/midx/4f3aa59bae0619c9a06b631d9cb7767591810ab0 new file mode 100644 index 00000000000..0a677236c0d Binary files /dev/null and b/fuzzers/corpora/midx/4f3aa59bae0619c9a06b631d9cb7767591810ab0 differ diff --git a/fuzzers/corpora/midx/501840d963cedd2945018de59e0202444d7ebf4b b/fuzzers/corpora/midx/501840d963cedd2945018de59e0202444d7ebf4b new file mode 100644 index 00000000000..cd26169c2d3 Binary files /dev/null and b/fuzzers/corpora/midx/501840d963cedd2945018de59e0202444d7ebf4b differ diff --git a/fuzzers/corpora/midx/50479958c030d1addceb1ca8c27f24447e555e65 b/fuzzers/corpora/midx/50479958c030d1addceb1ca8c27f24447e555e65 new file mode 100644 index 00000000000..22159a49eda Binary files /dev/null and b/fuzzers/corpora/midx/50479958c030d1addceb1ca8c27f24447e555e65 differ diff --git a/fuzzers/corpora/midx/508ba8ef164a809f739834a39d690e700101a7a1 b/fuzzers/corpora/midx/508ba8ef164a809f739834a39d690e700101a7a1 new file mode 100644 index 00000000000..7cf01e116c5 Binary files /dev/null and b/fuzzers/corpora/midx/508ba8ef164a809f739834a39d690e700101a7a1 differ diff --git a/fuzzers/corpora/midx/521d345313812e54bc6c944485e19dbb39a87768 b/fuzzers/corpora/midx/521d345313812e54bc6c944485e19dbb39a87768 new file mode 100644 index 00000000000..6e9550f749f Binary files /dev/null and b/fuzzers/corpora/midx/521d345313812e54bc6c944485e19dbb39a87768 differ diff --git a/fuzzers/corpora/midx/5369d74ac157f85b597c1b28bbd6768105e9327b b/fuzzers/corpora/midx/5369d74ac157f85b597c1b28bbd6768105e9327b new file mode 100644 index 00000000000..bda1f8c5d98 Binary files /dev/null and b/fuzzers/corpora/midx/5369d74ac157f85b597c1b28bbd6768105e9327b differ diff --git a/fuzzers/corpora/midx/53997b0146ff49bfe464be203b130a67ea93fd26 b/fuzzers/corpora/midx/53997b0146ff49bfe464be203b130a67ea93fd26 new file mode 100644 index 00000000000..12ea4cd2816 Binary files /dev/null and b/fuzzers/corpora/midx/53997b0146ff49bfe464be203b130a67ea93fd26 differ diff --git a/fuzzers/corpora/midx/560ea8bd7d11b00e0d21631b6d9ec7e63f0a5286 b/fuzzers/corpora/midx/560ea8bd7d11b00e0d21631b6d9ec7e63f0a5286 new file mode 100644 index 00000000000..0c984e107ef Binary files /dev/null and b/fuzzers/corpora/midx/560ea8bd7d11b00e0d21631b6d9ec7e63f0a5286 differ diff --git a/fuzzers/corpora/midx/5682ebc6878e247ce9bc636d34ada6ad338fcaf0 b/fuzzers/corpora/midx/5682ebc6878e247ce9bc636d34ada6ad338fcaf0 new file mode 100644 index 00000000000..1b881402fa3 Binary files /dev/null and b/fuzzers/corpora/midx/5682ebc6878e247ce9bc636d34ada6ad338fcaf0 differ diff --git a/fuzzers/corpora/midx/5762abb5234edd913754b69e1ab03274c711ee68 b/fuzzers/corpora/midx/5762abb5234edd913754b69e1ab03274c711ee68 new file mode 100644 index 00000000000..668572272ca Binary files /dev/null and b/fuzzers/corpora/midx/5762abb5234edd913754b69e1ab03274c711ee68 differ diff --git a/fuzzers/corpora/midx/579406f055070559bda3c6120107feb3e637c481 b/fuzzers/corpora/midx/579406f055070559bda3c6120107feb3e637c481 new file mode 100644 index 00000000000..be7a59b5c1a --- /dev/null +++ b/fuzzers/corpora/midx/579406f055070559bda3c6120107feb3e637c481 @@ -0,0 +1,2 @@ +& +) \ No newline at end of file diff --git a/fuzzers/corpora/midx/5837d16af4a9c1f2616467cc4aa9ec8836e05c58 b/fuzzers/corpora/midx/5837d16af4a9c1f2616467cc4aa9ec8836e05c58 new file mode 100644 index 00000000000..69bf0eb68c0 Binary files /dev/null and b/fuzzers/corpora/midx/5837d16af4a9c1f2616467cc4aa9ec8836e05c58 differ diff --git a/fuzzers/corpora/midx/58901e865fe20b9fa136cca4b253d3ae73c2b78e b/fuzzers/corpora/midx/58901e865fe20b9fa136cca4b253d3ae73c2b78e new file mode 100644 index 00000000000..c3605201e31 Binary files /dev/null and b/fuzzers/corpora/midx/58901e865fe20b9fa136cca4b253d3ae73c2b78e differ diff --git a/fuzzers/corpora/midx/58a87098a14572e46b53c87340083f999d8fcfc2 b/fuzzers/corpora/midx/58a87098a14572e46b53c87340083f999d8fcfc2 new file mode 100644 index 00000000000..f3711cd08bc Binary files /dev/null and b/fuzzers/corpora/midx/58a87098a14572e46b53c87340083f999d8fcfc2 differ diff --git a/fuzzers/corpora/midx/59ae139a21448e0eb7371ddc6ef57f0c9dfe9c85 b/fuzzers/corpora/midx/59ae139a21448e0eb7371ddc6ef57f0c9dfe9c85 new file mode 100644 index 00000000000..953072ca144 Binary files /dev/null and b/fuzzers/corpora/midx/59ae139a21448e0eb7371ddc6ef57f0c9dfe9c85 differ diff --git a/fuzzers/corpora/midx/5a7e81419f895168c555ac9b4e75a1ad4f04b34a b/fuzzers/corpora/midx/5a7e81419f895168c555ac9b4e75a1ad4f04b34a new file mode 100644 index 00000000000..c6b2c583ff9 Binary files /dev/null and b/fuzzers/corpora/midx/5a7e81419f895168c555ac9b4e75a1ad4f04b34a differ diff --git a/fuzzers/corpora/midx/5b848c1f56a150d64020e9b0bb398a286dca4096 b/fuzzers/corpora/midx/5b848c1f56a150d64020e9b0bb398a286dca4096 new file mode 100644 index 00000000000..17e91c12ea3 Binary files /dev/null and b/fuzzers/corpora/midx/5b848c1f56a150d64020e9b0bb398a286dca4096 differ diff --git a/fuzzers/corpora/midx/5bd311bd846336149b2815666052fdb7e8bf2ea6 b/fuzzers/corpora/midx/5bd311bd846336149b2815666052fdb7e8bf2ea6 new file mode 100644 index 00000000000..ccfa796ec5c Binary files /dev/null and b/fuzzers/corpora/midx/5bd311bd846336149b2815666052fdb7e8bf2ea6 differ diff --git a/fuzzers/corpora/midx/5ce77eb98473a2e01d04909939edf7aabef5762c b/fuzzers/corpora/midx/5ce77eb98473a2e01d04909939edf7aabef5762c new file mode 100644 index 00000000000..b8ed8eea9e6 Binary files /dev/null and b/fuzzers/corpora/midx/5ce77eb98473a2e01d04909939edf7aabef5762c differ diff --git a/fuzzers/corpora/midx/5e5cd5819811507ac69bd8abad27433ccd6b7521 b/fuzzers/corpora/midx/5e5cd5819811507ac69bd8abad27433ccd6b7521 new file mode 100644 index 00000000000..9069e16c138 Binary files /dev/null and b/fuzzers/corpora/midx/5e5cd5819811507ac69bd8abad27433ccd6b7521 differ diff --git a/fuzzers/corpora/midx/5ea114ae3dbb140364000c416152b0f32ce3de23 b/fuzzers/corpora/midx/5ea114ae3dbb140364000c416152b0f32ce3de23 new file mode 100644 index 00000000000..2c0394488bb Binary files /dev/null and b/fuzzers/corpora/midx/5ea114ae3dbb140364000c416152b0f32ce3de23 differ diff --git a/fuzzers/corpora/midx/5f181bb0a79603c84534a9b8e37ecdeb1d2aeeb5 b/fuzzers/corpora/midx/5f181bb0a79603c84534a9b8e37ecdeb1d2aeeb5 new file mode 100644 index 00000000000..c1a826f37c5 --- /dev/null +++ b/fuzzers/corpora/midx/5f181bb0a79603c84534a9b8e37ecdeb1d2aeeb5 @@ -0,0 +1 @@ +) (version 2.8 or newer) on all platforms. + +On most systems you can build the library using the following commands + + $ mkdir build && cd build + $ cmake .. + $ cmake --build . + +Alternatively you can point the CMake GUI tool to the CMakeLists.txt file and generate platform specific build project or IDE workspace. + +Running Tests +------------- + +Once built, you can run the tests from the `build` directory with the command + + $ ctest -V + +Alternatively you can run the test suite directly using, + + $ ./libgit2_tests + +Invoking the test suite directly is useful because it allows you to execute +individual tests, or groups of tests using the `-s` flag. For example, to +run the index tests: + + $ ./libgit2_tests -sindex + +To run a single test named `index::racy::diff`, which corresponds to the test +function (`test_index_racy__diff`)[https://github.com/libgit2/libgit2/blob/master/tests/index/racy.c#L23]: + + $ ./libgit2_tests -sindex::racy::diff + +The test suite will print a `.` for every passing test, and an `F` for any +failing test. An `S` indicates that a test was skipped because it is not +applicable to your platform or is particularly expensive. + +**Note:** There should be _no_ failing tests when you build an unmodified +source tree from a [release](https://github.com/libgit2/libgit2/releases), +or from the [master branch](https://github.com/libgit2/libgit2/tree/master). +Please contact us or [open an issue](https://github.com/libgit2/libgit2/issues) +if you see test failures. + +Installation +------------ + +To install the library you can specify the install prefix by setting: + + $ cmake .. -DCMAKE_INSTALL_PREFIX=/install/prefix + $ cmake --build . --target install + +Advanced Usage +-------------- + +For more advanced use or questions about CMake please read . + +The following CMake variables are declared: + +- `BIN_INSTALL_DIR`: Where to install binaries to. +- `LIB_INSTALL_DIR`: Where to install libraries to. +- `INCLUDE_INSTALL_DIR`: Where to install headers to. +- `BUILD_SHARED_LIBS`: Build libgit2 as a Shared Library (defaults to ON) +- `BUILD_TESTS`: Build [Clar](https://github.com/vmg/clar)-based test suite (defaults to ON) +- `THREADSAFE`: Build libgit2 with threading support (defaults to ON) +- `STDCALL`: Build libgit2 as `stdcall`. Turn off for `cdecl` (Windows; defaults to ON) + +Compiler and linker options +--------------------------- + +CMake lets you specify a few variables to control the behavior of the +compiler and linker. These flags are rarely used but can be useful for +64-bit to 32-bit cross-compilation. + +- `CMAKE_C_FLAGS`: Set your own compiler flags +- `CMAKE_FIND_ROOT_PATH`: Override the search path for libraries +- `ZLIB_LIBRARY`, `OPENSSL_SSL_LIBRARY` AND `OPENSSL_CRYPTO_LIBRARY`: +Tell CMake where to find those specific libraries + +MacOS X +------- + +If you want to build a universal binary for Mac OS X, CMake sets it +all up for you if you use `-DCMAKE_OSX_ARCHITECTURES="i386;x86_64"` +when configuring. + +Android +------- + +Extract toolchain from NDK using, `make-standalone-toolchain.sh` script. +Optionally, crosscompile and install OpenSSL inside of it. Then create CMake +toolchain file that configures paths to your crosscompiler (substitute `{PATH}` +with full path to the toolchain): + + SET(CMAKE_SYSTEM_NAME Linux) + SET(CMAKE_SYSTEM_VERSION Android) + + SET(CMAKE_C_COMPILER {PATH}/bin/arm-linux-androideabi-gcc) + SET(CMAKE_CXX_COMPILER {PATH}/bin/arm-linux-androideabi-g++) + SET(CMAKE_FIND_ROOT_PATH {PATH}/sysroot/) + + SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) + SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) + SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) + +Add `-DCMAKE_TOOLCHAIN_FILE={pathToToolchainFile}` to cmake command +when configuring. + +Language Bindings +================================== + +Here are the bindings to libgit2 that are currently available: + +* C++ + * libqgit2, Qt bindings +* Chicken Scheme + * chicken-git +* D + * dlibgit +* Delphi + * GitForDelphi +* Erlang + * Geef +* Go + * git2go +* GObject + * libgit2-glib +* Guile + * Guile-Git +* Haskell + * hgit2 +* Java + * Jagged +* Julia + * LibGit2.jl +* Lua + * luagit2 +* .NET + * libgit2sharp +* Node.js + * nodegit +* Objective-C + * objective-git +* OCaml + * ocaml-libgit2 +* Parrot Virtual Machine + * parrot-libgit2 +* Perl + * Git-Raw +* PHP + * php-git +* PowerShell + * PSGit +* Python + * pygit2 +* R + * git2r +* Ruby + * Rugged +* Rust + * git2-rs +* Swift + * SwiftGit2 +* Vala + * libgit2.vapi + +If you start another language binding to libgit2, please let us know so +we can add it to the list. + +How Can I Contribute? +================================== + +We welcome new contributors! We have a number of issues marked as +["up for grabs"](https://github.com/libgit2/libgit2/issues?q=is%3Aissue+is%3Aopen+label%3A%22up+for+grabs%22) +and +["easy fix"](https://github.com/libgit2/libgit2/issues?utf8=✓&q=is%3Aissue+is%3Aopen+label%3A%22easy+fix%22) +that are good places to jump in and get started. There's much more detailed +information in our list of [outstanding projects](PROJECTS.md). + +Please be sure to check the [contribution guidelines](CONTRIBUTING.md) to +understand our workflow, and the libgit2 [coding conventions](CONVENTIONS.md). + +License +================================== + +`libgit2` is under GPL2 **with linking exception**. This means you can link to +and use the library from any program, proprietary or open source; paid or +gratis. However, if you modify libgit2 itself, you must distribute the +source to your modified version of libgit2. + +See the [COPYING file](COPYING) for the full license text. diff --git a/fuzzers/corpora/objects/commit b/fuzzers/corpora/objects/commit new file mode 100644 index 00000000000..519495fc7f8 --- /dev/null +++ b/fuzzers/corpora/objects/commit @@ -0,0 +1,20 @@ +tree 3e7ac388cadae960fe7e22175ce0da878afe9d18 +parent 8b89f362a34fcccdf1c6c5f3445895b71d9c6d56 +parent c590b41fe4057a84a9bd31a5605ceef2c309b0f8 +author Patrick Steinhardt 1538760730 +0200 +committer GitHub 1538760730 +0200 +gpgsig -----BEGIN PGP SIGNATURE----- + + wsBcBAABCAAQBQJbt6AaCRBK7hj4Ov3rIwAAdHIIAKZGIpS0dAirVRt5NVFj3ZtC + o2Q3ADC0XpYLKkEsClhG7pVtr7MRZZ8+qaJpbxn9j9WZZ4UtEeDjseos+pMNn9Mf + OQQntNzGAbHSw0apyYT+mTUKaVONPev4fw9Lnc/RJ/iWwHx+4gmgNqLwV3foaCW9 + w1JzCL+BVJyZI80jrEehihhUnpIUOuMBwGjzSt54Zn5JqviC4cIldF2sXFGQqvsq + 3WDNnEUYanU6cLAdb9Pd6bVBI1EJnRLxehSeYiSaRPmLhQyhkH8KZ5lSi8iuH1C4 + bjA6HaEUwCeq0k9Le6BUu93BExEOFcuu8+zEKCrwCdSwdEQ3Iakv8dh7XlT9iUY= + =nGP0 + -----END PGP SIGNATURE----- + + +Merge pull request #4834 from pks-t/pks/v0.27.5 + +Security release v0.27.5 \ No newline at end of file diff --git a/fuzzers/corpora/objects/tag b/fuzzers/corpora/objects/tag new file mode 100644 index 00000000000..f5f1c5ed2fa --- /dev/null +++ b/fuzzers/corpora/objects/tag @@ -0,0 +1,6 @@ +object a8d447f68076d1520f69649bb52629941be7031f +type commit +tag testtag +tagger Patrick Steinhardt 1539253015 +0200 + +Tag message diff --git a/fuzzers/corpora/objects/tree b/fuzzers/corpora/objects/tree new file mode 100644 index 00000000000..d6639d860ef Binary files /dev/null and b/fuzzers/corpora/objects/tree differ diff --git a/fuzzers/corpora/packfile/004bd06c91c0dc8ab7e963f4b5e87be00292911e b/fuzzers/corpora/packfile/004bd06c91c0dc8ab7e963f4b5e87be00292911e new file mode 100644 index 00000000000..53c9642602b --- /dev/null +++ b/fuzzers/corpora/packfile/004bd06c91c0dc8ab7e963f4b5e87be00292911e @@ -0,0 +1 @@ +PACK \ No newline at end of file diff --git a/fuzzers/corpora/packfile/00b67414c7b17916b3bd0a3d02284937fa0c4378 b/fuzzers/corpora/packfile/00b67414c7b17916b3bd0a3d02284937fa0c4378 new file mode 100644 index 00000000000..838a0074745 Binary files /dev/null and b/fuzzers/corpora/packfile/00b67414c7b17916b3bd0a3d02284937fa0c4378 differ diff --git a/fuzzers/corpora/packfile/02eaeb43f0ec7dbfd91bd75e7ddcc7fd590dbc77 b/fuzzers/corpora/packfile/02eaeb43f0ec7dbfd91bd75e7ddcc7fd590dbc77 new file mode 100644 index 00000000000..48fe2b3d68a Binary files /dev/null and b/fuzzers/corpora/packfile/02eaeb43f0ec7dbfd91bd75e7ddcc7fd590dbc77 differ diff --git a/fuzzers/corpora/packfile/02f4286569be24124d8ab209733b7492f7560310 b/fuzzers/corpora/packfile/02f4286569be24124d8ab209733b7492f7560310 new file mode 100644 index 00000000000..3944cd0cbdd Binary files /dev/null and b/fuzzers/corpora/packfile/02f4286569be24124d8ab209733b7492f7560310 differ diff --git a/fuzzers/corpora/packfile/037ba5f9d6d695aa4739810f8bea6e795c1d7614 b/fuzzers/corpora/packfile/037ba5f9d6d695aa4739810f8bea6e795c1d7614 new file mode 100644 index 00000000000..f4ea551b76b Binary files /dev/null and b/fuzzers/corpora/packfile/037ba5f9d6d695aa4739810f8bea6e795c1d7614 differ diff --git a/fuzzers/corpora/packfile/038e06289ac876f109fc12ca4b8284497ca26821 b/fuzzers/corpora/packfile/038e06289ac876f109fc12ca4b8284497ca26821 new file mode 100644 index 00000000000..24923c4ee0b Binary files /dev/null and b/fuzzers/corpora/packfile/038e06289ac876f109fc12ca4b8284497ca26821 differ diff --git a/fuzzers/corpora/packfile/042dc4512fa3d391c5170cf3aa61e6a638f84342 b/fuzzers/corpora/packfile/042dc4512fa3d391c5170cf3aa61e6a638f84342 new file mode 100644 index 00000000000..597a6db294c --- /dev/null +++ b/fuzzers/corpora/packfile/042dc4512fa3d391c5170cf3aa61e6a638f84342 @@ -0,0 +1 @@ +i \ No newline at end of file diff --git a/fuzzers/corpora/packfile/044bf19babf3f9cde07adbfa2a45c7508538cbe8 b/fuzzers/corpora/packfile/044bf19babf3f9cde07adbfa2a45c7508538cbe8 new file mode 100644 index 00000000000..73a40137333 Binary files /dev/null and b/fuzzers/corpora/packfile/044bf19babf3f9cde07adbfa2a45c7508538cbe8 differ diff --git a/fuzzers/corpora/packfile/044e12ea43bee3c4befe27ba4687bee98d505fd7 b/fuzzers/corpora/packfile/044e12ea43bee3c4befe27ba4687bee98d505fd7 new file mode 100644 index 00000000000..1e129661ecf Binary files /dev/null and b/fuzzers/corpora/packfile/044e12ea43bee3c4befe27ba4687bee98d505fd7 differ diff --git a/fuzzers/corpora/packfile/061fb208431db793bbd3645b7a16058a1e2a2412 b/fuzzers/corpora/packfile/061fb208431db793bbd3645b7a16058a1e2a2412 new file mode 100644 index 00000000000..12183abc9e3 --- /dev/null +++ b/fuzzers/corpora/packfile/061fb208431db793bbd3645b7a16058a1e2a2412 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/06576556d1ad802f247cad11ae748be47b70cd9c b/fuzzers/corpora/packfile/06576556d1ad802f247cad11ae748be47b70cd9c new file mode 100644 index 00000000000..ac044e5e464 --- /dev/null +++ b/fuzzers/corpora/packfile/06576556d1ad802f247cad11ae748be47b70cd9c @@ -0,0 +1 @@ +R \ No newline at end of file diff --git a/fuzzers/corpora/packfile/06ceea0c98756d302c302410fffe0dc54a055486 b/fuzzers/corpora/packfile/06ceea0c98756d302c302410fffe0dc54a055486 new file mode 100644 index 00000000000..add37f9a31b Binary files /dev/null and b/fuzzers/corpora/packfile/06ceea0c98756d302c302410fffe0dc54a055486 differ diff --git a/fuzzers/corpora/packfile/071e65ac0bf08f2424a89a4a499004c1bb9f3f6c b/fuzzers/corpora/packfile/071e65ac0bf08f2424a89a4a499004c1bb9f3f6c new file mode 100644 index 00000000000..748750ce61a Binary files /dev/null and b/fuzzers/corpora/packfile/071e65ac0bf08f2424a89a4a499004c1bb9f3f6c differ diff --git a/fuzzers/corpora/packfile/0739ff2f064568a4d775c8061958e66c419dbea0 b/fuzzers/corpora/packfile/0739ff2f064568a4d775c8061958e66c419dbea0 new file mode 100644 index 00000000000..aed5f1686e7 Binary files /dev/null and b/fuzzers/corpora/packfile/0739ff2f064568a4d775c8061958e66c419dbea0 differ diff --git a/fuzzers/corpora/packfile/077760469bf8392342d09329c732b98d24be2c30 b/fuzzers/corpora/packfile/077760469bf8392342d09329c732b98d24be2c30 new file mode 100644 index 00000000000..c9553bc99ae Binary files /dev/null and b/fuzzers/corpora/packfile/077760469bf8392342d09329c732b98d24be2c30 differ diff --git a/fuzzers/corpora/packfile/08534f33c201a45017b502e90a800f1b708ebcb3 b/fuzzers/corpora/packfile/08534f33c201a45017b502e90a800f1b708ebcb3 new file mode 100644 index 00000000000..b7d5379f9e3 --- /dev/null +++ b/fuzzers/corpora/packfile/08534f33c201a45017b502e90a800f1b708ebcb3 @@ -0,0 +1 @@ +\ \ No newline at end of file diff --git a/fuzzers/corpora/packfile/09e9046a7d6125cf2915a326a1504dd75d0543b5 b/fuzzers/corpora/packfile/09e9046a7d6125cf2915a326a1504dd75d0543b5 new file mode 100644 index 00000000000..86d7321ade2 --- /dev/null +++ b/fuzzers/corpora/packfile/09e9046a7d6125cf2915a326a1504dd75d0543b5 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/0ade7c2cf97f75d009975f4d720d1fa6c19f4897 b/fuzzers/corpora/packfile/0ade7c2cf97f75d009975f4d720d1fa6c19f4897 new file mode 100644 index 00000000000..f11c82a4cb6 --- /dev/null +++ b/fuzzers/corpora/packfile/0ade7c2cf97f75d009975f4d720d1fa6c19f4897 @@ -0,0 +1 @@ +9 \ No newline at end of file diff --git a/fuzzers/corpora/packfile/0c316c67c1450aee57ffa3f74c19ea5d32d44622 b/fuzzers/corpora/packfile/0c316c67c1450aee57ffa3f74c19ea5d32d44622 new file mode 100644 index 00000000000..08d7351a31f Binary files /dev/null and b/fuzzers/corpora/packfile/0c316c67c1450aee57ffa3f74c19ea5d32d44622 differ diff --git a/fuzzers/corpora/packfile/0c395c44e4dd5b67caae8a98a66741e17e8af136 b/fuzzers/corpora/packfile/0c395c44e4dd5b67caae8a98a66741e17e8af136 new file mode 100644 index 00000000000..e899a797a7e Binary files /dev/null and b/fuzzers/corpora/packfile/0c395c44e4dd5b67caae8a98a66741e17e8af136 differ diff --git a/fuzzers/corpora/packfile/0cb9120e5ae00b0d660a111ef85fc9194a5f244a b/fuzzers/corpora/packfile/0cb9120e5ae00b0d660a111ef85fc9194a5f244a new file mode 100644 index 00000000000..9fb6a664bdb Binary files /dev/null and b/fuzzers/corpora/packfile/0cb9120e5ae00b0d660a111ef85fc9194a5f244a differ diff --git a/fuzzers/corpora/packfile/0d44e7156d04cd269fd1219c58c3b79dc8c41d60 b/fuzzers/corpora/packfile/0d44e7156d04cd269fd1219c58c3b79dc8c41d60 new file mode 100644 index 00000000000..cf18be050f6 Binary files /dev/null and b/fuzzers/corpora/packfile/0d44e7156d04cd269fd1219c58c3b79dc8c41d60 differ diff --git a/fuzzers/corpora/packfile/0d77a48bea1dde6e5d391a65456dc0aa3d9cc5e3 b/fuzzers/corpora/packfile/0d77a48bea1dde6e5d391a65456dc0aa3d9cc5e3 new file mode 100644 index 00000000000..6d3c9c0c83b Binary files /dev/null and b/fuzzers/corpora/packfile/0d77a48bea1dde6e5d391a65456dc0aa3d9cc5e3 differ diff --git a/fuzzers/corpora/packfile/0db25107ff248616cadc75b7819b21d06394cf25 b/fuzzers/corpora/packfile/0db25107ff248616cadc75b7819b21d06394cf25 new file mode 100644 index 00000000000..c158d57e84a Binary files /dev/null and b/fuzzers/corpora/packfile/0db25107ff248616cadc75b7819b21d06394cf25 differ diff --git a/fuzzers/corpora/packfile/0debae2db7ef2933f386bac21a2d3bebb473070e b/fuzzers/corpora/packfile/0debae2db7ef2933f386bac21a2d3bebb473070e new file mode 100644 index 00000000000..1f9443f9f6d Binary files /dev/null and b/fuzzers/corpora/packfile/0debae2db7ef2933f386bac21a2d3bebb473070e differ diff --git a/fuzzers/corpora/packfile/0e2d48524de33394ca82ea3a43f5f04aac6e86c7 b/fuzzers/corpora/packfile/0e2d48524de33394ca82ea3a43f5f04aac6e86c7 new file mode 100644 index 00000000000..1fd3289a50d Binary files /dev/null and b/fuzzers/corpora/packfile/0e2d48524de33394ca82ea3a43f5f04aac6e86c7 differ diff --git a/fuzzers/corpora/packfile/0e49f6aa78f3b2f6c3fa5d281d5b1052fa9951dc b/fuzzers/corpora/packfile/0e49f6aa78f3b2f6c3fa5d281d5b1052fa9951dc new file mode 100644 index 00000000000..286d703326e Binary files /dev/null and b/fuzzers/corpora/packfile/0e49f6aa78f3b2f6c3fa5d281d5b1052fa9951dc differ diff --git a/fuzzers/corpora/packfile/0f2982027f0b3b05250267b19e3969f8797e389e b/fuzzers/corpora/packfile/0f2982027f0b3b05250267b19e3969f8797e389e new file mode 100644 index 00000000000..57505d27bcf --- /dev/null +++ b/fuzzers/corpora/packfile/0f2982027f0b3b05250267b19e3969f8797e389e @@ -0,0 +1 @@ +PACK \ No newline at end of file diff --git a/fuzzers/corpora/packfile/11f6ad8ec52a2984abaafd7c3b516503785c2072 b/fuzzers/corpora/packfile/11f6ad8ec52a2984abaafd7c3b516503785c2072 new file mode 100644 index 00000000000..c1b0730e013 --- /dev/null +++ b/fuzzers/corpora/packfile/11f6ad8ec52a2984abaafd7c3b516503785c2072 @@ -0,0 +1 @@ +x \ No newline at end of file diff --git a/fuzzers/corpora/packfile/123ca693d81a8cfd99760ff5ca9e152ded878537 b/fuzzers/corpora/packfile/123ca693d81a8cfd99760ff5ca9e152ded878537 new file mode 100644 index 00000000000..e6809148f12 Binary files /dev/null and b/fuzzers/corpora/packfile/123ca693d81a8cfd99760ff5ca9e152ded878537 differ diff --git a/fuzzers/corpora/packfile/12878ca5643ab15a4a847e74ddd84fb365736af2 b/fuzzers/corpora/packfile/12878ca5643ab15a4a847e74ddd84fb365736af2 new file mode 100644 index 00000000000..60294631db7 Binary files /dev/null and b/fuzzers/corpora/packfile/12878ca5643ab15a4a847e74ddd84fb365736af2 differ diff --git a/fuzzers/corpora/packfile/13f292a24a9e79ae911f5d5e1ef7db0112601e64 b/fuzzers/corpora/packfile/13f292a24a9e79ae911f5d5e1ef7db0112601e64 new file mode 100644 index 00000000000..36b85ed32bf Binary files /dev/null and b/fuzzers/corpora/packfile/13f292a24a9e79ae911f5d5e1ef7db0112601e64 differ diff --git a/fuzzers/corpora/packfile/13facd9b4b5b4509fee92c7ccc1c82ed90624172 b/fuzzers/corpora/packfile/13facd9b4b5b4509fee92c7ccc1c82ed90624172 new file mode 100644 index 00000000000..82f4fa05902 Binary files /dev/null and b/fuzzers/corpora/packfile/13facd9b4b5b4509fee92c7ccc1c82ed90624172 differ diff --git a/fuzzers/corpora/packfile/140092a21903fdc56c98de126725fa6bead98ab1 b/fuzzers/corpora/packfile/140092a21903fdc56c98de126725fa6bead98ab1 new file mode 100644 index 00000000000..11006ca79d3 Binary files /dev/null and b/fuzzers/corpora/packfile/140092a21903fdc56c98de126725fa6bead98ab1 differ diff --git a/fuzzers/corpora/packfile/1489f923c4dca729178b3e3233458550d8dddf29 b/fuzzers/corpora/packfile/1489f923c4dca729178b3e3233458550d8dddf29 new file mode 100644 index 00000000000..09f370e38f4 Binary files /dev/null and b/fuzzers/corpora/packfile/1489f923c4dca729178b3e3233458550d8dddf29 differ diff --git a/fuzzers/corpora/packfile/1501a58834f24f95442f190653a50dd67d01e19d b/fuzzers/corpora/packfile/1501a58834f24f95442f190653a50dd67d01e19d new file mode 100644 index 00000000000..3bbe254b70e Binary files /dev/null and b/fuzzers/corpora/packfile/1501a58834f24f95442f190653a50dd67d01e19d differ diff --git a/fuzzers/corpora/packfile/15eddee57cafb11e927810d62230a6e104de1d5c b/fuzzers/corpora/packfile/15eddee57cafb11e927810d62230a6e104de1d5c new file mode 100644 index 00000000000..4c563e6cba4 Binary files /dev/null and b/fuzzers/corpora/packfile/15eddee57cafb11e927810d62230a6e104de1d5c differ diff --git a/fuzzers/corpora/packfile/1632aa4b049f1118306485b11c70c499a0200dd5 b/fuzzers/corpora/packfile/1632aa4b049f1118306485b11c70c499a0200dd5 new file mode 100644 index 00000000000..677f3ab4933 Binary files /dev/null and b/fuzzers/corpora/packfile/1632aa4b049f1118306485b11c70c499a0200dd5 differ diff --git a/fuzzers/corpora/packfile/18e1cf33b179a5cbaaf0baac8279ec4ed1cbdcf3 b/fuzzers/corpora/packfile/18e1cf33b179a5cbaaf0baac8279ec4ed1cbdcf3 new file mode 100644 index 00000000000..8445262300d Binary files /dev/null and b/fuzzers/corpora/packfile/18e1cf33b179a5cbaaf0baac8279ec4ed1cbdcf3 differ diff --git a/fuzzers/corpora/packfile/18e768865207e0b90047487b66532b20bc74b1a2 b/fuzzers/corpora/packfile/18e768865207e0b90047487b66532b20bc74b1a2 new file mode 100644 index 00000000000..2069d5bb96f Binary files /dev/null and b/fuzzers/corpora/packfile/18e768865207e0b90047487b66532b20bc74b1a2 differ diff --git a/fuzzers/corpora/packfile/1940c66b45a3bd5c847330b207fd87aee6e96194 b/fuzzers/corpora/packfile/1940c66b45a3bd5c847330b207fd87aee6e96194 new file mode 100644 index 00000000000..5b8513ee73e Binary files /dev/null and b/fuzzers/corpora/packfile/1940c66b45a3bd5c847330b207fd87aee6e96194 differ diff --git a/fuzzers/corpora/packfile/1966ab31dc80ab75196b0cbf28e3960a0eb3f6c5 b/fuzzers/corpora/packfile/1966ab31dc80ab75196b0cbf28e3960a0eb3f6c5 new file mode 100644 index 00000000000..572d311d205 Binary files /dev/null and b/fuzzers/corpora/packfile/1966ab31dc80ab75196b0cbf28e3960a0eb3f6c5 differ diff --git a/fuzzers/corpora/packfile/19da91f2603889267dfd77786e07a5b8f067d62a b/fuzzers/corpora/packfile/19da91f2603889267dfd77786e07a5b8f067d62a new file mode 100644 index 00000000000..8b43ca9ac41 --- /dev/null +++ b/fuzzers/corpora/packfile/19da91f2603889267dfd77786e07a5b8f067d62a @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/1a72795a3dffdfc999b030d9aab7237dea1e2bc1 b/fuzzers/corpora/packfile/1a72795a3dffdfc999b030d9aab7237dea1e2bc1 new file mode 100644 index 00000000000..5a8a38b900e Binary files /dev/null and b/fuzzers/corpora/packfile/1a72795a3dffdfc999b030d9aab7237dea1e2bc1 differ diff --git a/fuzzers/corpora/packfile/1e29cf67a66f225b338610fbcdf1b8185a8f5b7d b/fuzzers/corpora/packfile/1e29cf67a66f225b338610fbcdf1b8185a8f5b7d new file mode 100644 index 00000000000..52219dec15d Binary files /dev/null and b/fuzzers/corpora/packfile/1e29cf67a66f225b338610fbcdf1b8185a8f5b7d differ diff --git a/fuzzers/corpora/packfile/1eb8977ef8c3be9ee896d785663c762c7e32be28 b/fuzzers/corpora/packfile/1eb8977ef8c3be9ee896d785663c762c7e32be28 new file mode 100644 index 00000000000..d924eb71be5 Binary files /dev/null and b/fuzzers/corpora/packfile/1eb8977ef8c3be9ee896d785663c762c7e32be28 differ diff --git a/fuzzers/corpora/packfile/1f0837530c1c3d122157f2eaa9c2178dcc3580df b/fuzzers/corpora/packfile/1f0837530c1c3d122157f2eaa9c2178dcc3580df new file mode 100644 index 00000000000..26b0c60418f Binary files /dev/null and b/fuzzers/corpora/packfile/1f0837530c1c3d122157f2eaa9c2178dcc3580df differ diff --git a/fuzzers/corpora/packfile/1f3c5fd6dc091faa397bce776aa97b457388fdae b/fuzzers/corpora/packfile/1f3c5fd6dc091faa397bce776aa97b457388fdae new file mode 100644 index 00000000000..5b5232da222 Binary files /dev/null and b/fuzzers/corpora/packfile/1f3c5fd6dc091faa397bce776aa97b457388fdae differ diff --git a/fuzzers/corpora/packfile/20528983163f834108150a7191600ff94ae2c1d2 b/fuzzers/corpora/packfile/20528983163f834108150a7191600ff94ae2c1d2 new file mode 100644 index 00000000000..05189f0f469 Binary files /dev/null and b/fuzzers/corpora/packfile/20528983163f834108150a7191600ff94ae2c1d2 differ diff --git a/fuzzers/corpora/packfile/20a725140a8ffbe11bb71c1b83f19452147e5180 b/fuzzers/corpora/packfile/20a725140a8ffbe11bb71c1b83f19452147e5180 new file mode 100644 index 00000000000..f0337fac29d Binary files /dev/null and b/fuzzers/corpora/packfile/20a725140a8ffbe11bb71c1b83f19452147e5180 differ diff --git a/fuzzers/corpora/packfile/2149aa9e07dda9bbf502e088d8d0a38e8fb94f2e b/fuzzers/corpora/packfile/2149aa9e07dda9bbf502e088d8d0a38e8fb94f2e new file mode 100644 index 00000000000..e7754cae5ad --- /dev/null +++ b/fuzzers/corpora/packfile/2149aa9e07dda9bbf502e088d8d0a38e8fb94f2e @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/21b664aa8c86aaee4007d9bdbc2d63bf82bd5109 b/fuzzers/corpora/packfile/21b664aa8c86aaee4007d9bdbc2d63bf82bd5109 new file mode 100644 index 00000000000..44c66047c91 Binary files /dev/null and b/fuzzers/corpora/packfile/21b664aa8c86aaee4007d9bdbc2d63bf82bd5109 differ diff --git a/fuzzers/corpora/packfile/21b9ec8a7d7ac4d542c9bf7b2e26581cfcfaaab6 b/fuzzers/corpora/packfile/21b9ec8a7d7ac4d542c9bf7b2e26581cfcfaaab6 new file mode 100644 index 00000000000..c2f5439b3d3 Binary files /dev/null and b/fuzzers/corpora/packfile/21b9ec8a7d7ac4d542c9bf7b2e26581cfcfaaab6 differ diff --git a/fuzzers/corpora/packfile/21c07e2affed6b0134d5dc28ea6c4937e691c761 b/fuzzers/corpora/packfile/21c07e2affed6b0134d5dc28ea6c4937e691c761 new file mode 100644 index 00000000000..ee9fc8981c6 Binary files /dev/null and b/fuzzers/corpora/packfile/21c07e2affed6b0134d5dc28ea6c4937e691c761 differ diff --git a/fuzzers/corpora/packfile/23841d4076641ebcb4f58d1fd03047528c9d359b b/fuzzers/corpora/packfile/23841d4076641ebcb4f58d1fd03047528c9d359b new file mode 100644 index 00000000000..76b0b49e5d1 Binary files /dev/null and b/fuzzers/corpora/packfile/23841d4076641ebcb4f58d1fd03047528c9d359b differ diff --git a/fuzzers/corpora/packfile/23b9174c42560de6525b1f103125f599479f95cb b/fuzzers/corpora/packfile/23b9174c42560de6525b1f103125f599479f95cb new file mode 100644 index 00000000000..4bef2f6edda Binary files /dev/null and b/fuzzers/corpora/packfile/23b9174c42560de6525b1f103125f599479f95cb differ diff --git a/fuzzers/corpora/packfile/241cbd6dfb6e53c43c73b62f9384359091dcbf56 b/fuzzers/corpora/packfile/241cbd6dfb6e53c43c73b62f9384359091dcbf56 new file mode 100644 index 00000000000..bd0fd359422 --- /dev/null +++ b/fuzzers/corpora/packfile/241cbd6dfb6e53c43c73b62f9384359091dcbf56 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/245a2ddea41e6531944933c4420b8c9790ac351b b/fuzzers/corpora/packfile/245a2ddea41e6531944933c4420b8c9790ac351b new file mode 100644 index 00000000000..99cbc9beb37 Binary files /dev/null and b/fuzzers/corpora/packfile/245a2ddea41e6531944933c4420b8c9790ac351b differ diff --git a/fuzzers/corpora/packfile/2541e340271ea496a392870bcc20d3510287b9e9 b/fuzzers/corpora/packfile/2541e340271ea496a392870bcc20d3510287b9e9 new file mode 100644 index 00000000000..ff63b2add2a Binary files /dev/null and b/fuzzers/corpora/packfile/2541e340271ea496a392870bcc20d3510287b9e9 differ diff --git a/fuzzers/corpora/packfile/276af22d5bf94344737fb1a5eb5de7d335004493 b/fuzzers/corpora/packfile/276af22d5bf94344737fb1a5eb5de7d335004493 new file mode 100644 index 00000000000..fd2b0774c9d Binary files /dev/null and b/fuzzers/corpora/packfile/276af22d5bf94344737fb1a5eb5de7d335004493 differ diff --git a/fuzzers/corpora/packfile/27d5482eebd075de44389774fce28c69f45c8a75 b/fuzzers/corpora/packfile/27d5482eebd075de44389774fce28c69f45c8a75 new file mode 100644 index 00000000000..be54354a943 --- /dev/null +++ b/fuzzers/corpora/packfile/27d5482eebd075de44389774fce28c69f45c8a75 @@ -0,0 +1 @@ +h \ No newline at end of file diff --git a/fuzzers/corpora/packfile/28334bd612cb539de776370995f60c8032215434 b/fuzzers/corpora/packfile/28334bd612cb539de776370995f60c8032215434 new file mode 100644 index 00000000000..6d67b283dd4 Binary files /dev/null and b/fuzzers/corpora/packfile/28334bd612cb539de776370995f60c8032215434 differ diff --git a/fuzzers/corpora/packfile/2973e2ac092cba077d7350bfffe1cf2e0644a6e1 b/fuzzers/corpora/packfile/2973e2ac092cba077d7350bfffe1cf2e0644a6e1 new file mode 100644 index 00000000000..1066746d385 Binary files /dev/null and b/fuzzers/corpora/packfile/2973e2ac092cba077d7350bfffe1cf2e0644a6e1 differ diff --git a/fuzzers/corpora/packfile/2adcd01e876b12d867c858ffaec38c42c59c36c7 b/fuzzers/corpora/packfile/2adcd01e876b12d867c858ffaec38c42c59c36c7 new file mode 100644 index 00000000000..4323d106b3a Binary files /dev/null and b/fuzzers/corpora/packfile/2adcd01e876b12d867c858ffaec38c42c59c36c7 differ diff --git a/fuzzers/corpora/packfile/2b28470644f5d0323643da99c831d82f20a7a74f b/fuzzers/corpora/packfile/2b28470644f5d0323643da99c831d82f20a7a74f new file mode 100644 index 00000000000..a65ff490e69 Binary files /dev/null and b/fuzzers/corpora/packfile/2b28470644f5d0323643da99c831d82f20a7a74f differ diff --git a/fuzzers/corpora/packfile/2b86229020ba808df84e16f800dc152254f18f64 b/fuzzers/corpora/packfile/2b86229020ba808df84e16f800dc152254f18f64 new file mode 100644 index 00000000000..bd650d245ef Binary files /dev/null and b/fuzzers/corpora/packfile/2b86229020ba808df84e16f800dc152254f18f64 differ diff --git a/fuzzers/corpora/packfile/2cc5bf2f780cd85ad93d232890f418625f4d1274 b/fuzzers/corpora/packfile/2cc5bf2f780cd85ad93d232890f418625f4d1274 new file mode 100644 index 00000000000..f45a38c22f0 --- /dev/null +++ b/fuzzers/corpora/packfile/2cc5bf2f780cd85ad93d232890f418625f4d1274 @@ -0,0 +1 @@ +Pw \ No newline at end of file diff --git a/fuzzers/corpora/packfile/2d6ae8fa82b656879dd3371d0a6899e88ef34e76 b/fuzzers/corpora/packfile/2d6ae8fa82b656879dd3371d0a6899e88ef34e76 new file mode 100644 index 00000000000..cd62a7ba79f Binary files /dev/null and b/fuzzers/corpora/packfile/2d6ae8fa82b656879dd3371d0a6899e88ef34e76 differ diff --git a/fuzzers/corpora/packfile/2e74d24e887678f0681d4c7c010477b8b9697f1a b/fuzzers/corpora/packfile/2e74d24e887678f0681d4c7c010477b8b9697f1a new file mode 100644 index 00000000000..ae9780bc629 --- /dev/null +++ b/fuzzers/corpora/packfile/2e74d24e887678f0681d4c7c010477b8b9697f1a @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/2f436c68a7b0be43aa6d4ad5126ec9401a9f9211 b/fuzzers/corpora/packfile/2f436c68a7b0be43aa6d4ad5126ec9401a9f9211 new file mode 100644 index 00000000000..4841ef55ab9 Binary files /dev/null and b/fuzzers/corpora/packfile/2f436c68a7b0be43aa6d4ad5126ec9401a9f9211 differ diff --git a/fuzzers/corpora/packfile/2fec48b0dcb45b98597bfec12bf0dc650543b3e3 b/fuzzers/corpora/packfile/2fec48b0dcb45b98597bfec12bf0dc650543b3e3 new file mode 100644 index 00000000000..0ded0e30829 Binary files /dev/null and b/fuzzers/corpora/packfile/2fec48b0dcb45b98597bfec12bf0dc650543b3e3 differ diff --git a/fuzzers/corpora/packfile/31bd25636a9807d6024e78b9b3d02fbb1a02835e b/fuzzers/corpora/packfile/31bd25636a9807d6024e78b9b3d02fbb1a02835e new file mode 100644 index 00000000000..15d17515466 --- /dev/null +++ b/fuzzers/corpora/packfile/31bd25636a9807d6024e78b9b3d02fbb1a02835e @@ -0,0 +1 @@ +@ \ No newline at end of file diff --git a/fuzzers/corpora/packfile/323c88be36ecc20ff30b21cf417106554042db04 b/fuzzers/corpora/packfile/323c88be36ecc20ff30b21cf417106554042db04 new file mode 100644 index 00000000000..0e5c2cbf50e Binary files /dev/null and b/fuzzers/corpora/packfile/323c88be36ecc20ff30b21cf417106554042db04 differ diff --git a/fuzzers/corpora/packfile/33b3aa957ca4fb31873033a7f460617f1fe81e32 b/fuzzers/corpora/packfile/33b3aa957ca4fb31873033a7f460617f1fe81e32 new file mode 100644 index 00000000000..cea3b121799 Binary files /dev/null and b/fuzzers/corpora/packfile/33b3aa957ca4fb31873033a7f460617f1fe81e32 differ diff --git a/fuzzers/corpora/packfile/34303d14e37c9ddfb0bad130e006fec927e13785 b/fuzzers/corpora/packfile/34303d14e37c9ddfb0bad130e006fec927e13785 new file mode 100644 index 00000000000..1adb6b18d3b Binary files /dev/null and b/fuzzers/corpora/packfile/34303d14e37c9ddfb0bad130e006fec927e13785 differ diff --git a/fuzzers/corpora/packfile/34dac9466a4a2c15aaeef13a9567f6827ace7748 b/fuzzers/corpora/packfile/34dac9466a4a2c15aaeef13a9567f6827ace7748 new file mode 100644 index 00000000000..2b0708eabd0 Binary files /dev/null and b/fuzzers/corpora/packfile/34dac9466a4a2c15aaeef13a9567f6827ace7748 differ diff --git a/fuzzers/corpora/packfile/356a192b7913b04c54574d18c28d46e6395428ab b/fuzzers/corpora/packfile/356a192b7913b04c54574d18c28d46e6395428ab new file mode 100644 index 00000000000..56a6051ca2b --- /dev/null +++ b/fuzzers/corpora/packfile/356a192b7913b04c54574d18c28d46e6395428ab @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/fuzzers/corpora/packfile/35bf585248be2c6d3940e15b85f72c4146903097 b/fuzzers/corpora/packfile/35bf585248be2c6d3940e15b85f72c4146903097 new file mode 100644 index 00000000000..f17d4fa0714 Binary files /dev/null and b/fuzzers/corpora/packfile/35bf585248be2c6d3940e15b85f72c4146903097 differ diff --git a/fuzzers/corpora/packfile/3725a1c4431714019827277deac8ec2efeed8f1d b/fuzzers/corpora/packfile/3725a1c4431714019827277deac8ec2efeed8f1d new file mode 100644 index 00000000000..f58c2719eac Binary files /dev/null and b/fuzzers/corpora/packfile/3725a1c4431714019827277deac8ec2efeed8f1d differ diff --git a/fuzzers/corpora/packfile/37a2b7de1fadc9eab2d5fabf5dfe7007c245dbee b/fuzzers/corpora/packfile/37a2b7de1fadc9eab2d5fabf5dfe7007c245dbee new file mode 100644 index 00000000000..0c71198c344 Binary files /dev/null and b/fuzzers/corpora/packfile/37a2b7de1fadc9eab2d5fabf5dfe7007c245dbee differ diff --git a/fuzzers/corpora/packfile/37ab8a0ca81fc62279057401761f7730a5a8f1b2 b/fuzzers/corpora/packfile/37ab8a0ca81fc62279057401761f7730a5a8f1b2 new file mode 100644 index 00000000000..d9b74cf6546 Binary files /dev/null and b/fuzzers/corpora/packfile/37ab8a0ca81fc62279057401761f7730a5a8f1b2 differ diff --git a/fuzzers/corpora/packfile/38011be20a664dcd2594e712a26c063c2d50efcd b/fuzzers/corpora/packfile/38011be20a664dcd2594e712a26c063c2d50efcd new file mode 100644 index 00000000000..bd806165340 Binary files /dev/null and b/fuzzers/corpora/packfile/38011be20a664dcd2594e712a26c063c2d50efcd differ diff --git a/fuzzers/corpora/packfile/3838851a5da8239c2ae6cbbe869c81446c720e87 b/fuzzers/corpora/packfile/3838851a5da8239c2ae6cbbe869c81446c720e87 new file mode 100644 index 00000000000..d1282b948c9 Binary files /dev/null and b/fuzzers/corpora/packfile/3838851a5da8239c2ae6cbbe869c81446c720e87 differ diff --git a/fuzzers/corpora/packfile/3921322ac01429b001f88d64c8319088fe49218e b/fuzzers/corpora/packfile/3921322ac01429b001f88d64c8319088fe49218e new file mode 100644 index 00000000000..6ed50ebd0c8 Binary files /dev/null and b/fuzzers/corpora/packfile/3921322ac01429b001f88d64c8319088fe49218e differ diff --git a/fuzzers/corpora/packfile/395df8f7c51f007019cb30201c49e884b46b92fa b/fuzzers/corpora/packfile/395df8f7c51f007019cb30201c49e884b46b92fa new file mode 100644 index 00000000000..fa7af8bf5fd --- /dev/null +++ b/fuzzers/corpora/packfile/395df8f7c51f007019cb30201c49e884b46b92fa @@ -0,0 +1 @@ +z \ No newline at end of file diff --git a/fuzzers/corpora/packfile/3e98eb4fd65d3f2c41fa979db0f5678b310e51fe b/fuzzers/corpora/packfile/3e98eb4fd65d3f2c41fa979db0f5678b310e51fe new file mode 100644 index 00000000000..8f1d1f526ca Binary files /dev/null and b/fuzzers/corpora/packfile/3e98eb4fd65d3f2c41fa979db0f5678b310e51fe differ diff --git a/fuzzers/corpora/packfile/3f9ec359d0cb573cb6d2b2df64c9f4048ea298b8 b/fuzzers/corpora/packfile/3f9ec359d0cb573cb6d2b2df64c9f4048ea298b8 new file mode 100644 index 00000000000..3c1f737c107 Binary files /dev/null and b/fuzzers/corpora/packfile/3f9ec359d0cb573cb6d2b2df64c9f4048ea298b8 differ diff --git a/fuzzers/corpora/packfile/4067250457728bf775aa310ef253b223ae2fe4dc b/fuzzers/corpora/packfile/4067250457728bf775aa310ef253b223ae2fe4dc new file mode 100644 index 00000000000..1e9a88b682c Binary files /dev/null and b/fuzzers/corpora/packfile/4067250457728bf775aa310ef253b223ae2fe4dc differ diff --git a/fuzzers/corpora/packfile/40818db87e110b29cb864f73265586cc054f5bbb b/fuzzers/corpora/packfile/40818db87e110b29cb864f73265586cc054f5bbb new file mode 100644 index 00000000000..e5e52b2bfed Binary files /dev/null and b/fuzzers/corpora/packfile/40818db87e110b29cb864f73265586cc054f5bbb differ diff --git a/fuzzers/corpora/packfile/418f9fb9ce1d4efdf481ca8fff9dadd09caee9fc b/fuzzers/corpora/packfile/418f9fb9ce1d4efdf481ca8fff9dadd09caee9fc new file mode 100644 index 00000000000..93eb9350823 Binary files /dev/null and b/fuzzers/corpora/packfile/418f9fb9ce1d4efdf481ca8fff9dadd09caee9fc differ diff --git a/fuzzers/corpora/packfile/41ca0ae865b686089b8d99e9d661da291ce51019 b/fuzzers/corpora/packfile/41ca0ae865b686089b8d99e9d661da291ce51019 new file mode 100644 index 00000000000..e6f394f020a Binary files /dev/null and b/fuzzers/corpora/packfile/41ca0ae865b686089b8d99e9d661da291ce51019 differ diff --git a/fuzzers/corpora/packfile/42099b4af021e53fd8fd4e056c2568d7c2e3ffa8 b/fuzzers/corpora/packfile/42099b4af021e53fd8fd4e056c2568d7c2e3ffa8 new file mode 100644 index 00000000000..35ec3b9d758 --- /dev/null +++ b/fuzzers/corpora/packfile/42099b4af021e53fd8fd4e056c2568d7c2e3ffa8 @@ -0,0 +1 @@ +/ \ No newline at end of file diff --git a/fuzzers/corpora/packfile/420ce645ce1c93cee59a06da2159cbbb251e4c01 b/fuzzers/corpora/packfile/420ce645ce1c93cee59a06da2159cbbb251e4c01 new file mode 100644 index 00000000000..5e6efc2da76 Binary files /dev/null and b/fuzzers/corpora/packfile/420ce645ce1c93cee59a06da2159cbbb251e4c01 differ diff --git a/fuzzers/corpora/packfile/4345cb1fa27885a8fbfe7c0c830a592cc76a552b b/fuzzers/corpora/packfile/4345cb1fa27885a8fbfe7c0c830a592cc76a552b new file mode 100644 index 00000000000..02691e3522c --- /dev/null +++ b/fuzzers/corpora/packfile/4345cb1fa27885a8fbfe7c0c830a592cc76a552b @@ -0,0 +1 @@ +% \ No newline at end of file diff --git a/fuzzers/corpora/packfile/450718a71a93a1b5ff982595432400b0fa876fb6 b/fuzzers/corpora/packfile/450718a71a93a1b5ff982595432400b0fa876fb6 new file mode 100644 index 00000000000..977e5950cbe Binary files /dev/null and b/fuzzers/corpora/packfile/450718a71a93a1b5ff982595432400b0fa876fb6 differ diff --git a/fuzzers/corpora/packfile/453a312eb77b9d4198ac62faef10ecf3e283120c b/fuzzers/corpora/packfile/453a312eb77b9d4198ac62faef10ecf3e283120c new file mode 100644 index 00000000000..b25fe537670 Binary files /dev/null and b/fuzzers/corpora/packfile/453a312eb77b9d4198ac62faef10ecf3e283120c differ diff --git a/fuzzers/corpora/packfile/45470317334b614ce4d119c05ed2d6250dbc6061 b/fuzzers/corpora/packfile/45470317334b614ce4d119c05ed2d6250dbc6061 new file mode 100644 index 00000000000..4b9a0a91a4d Binary files /dev/null and b/fuzzers/corpora/packfile/45470317334b614ce4d119c05ed2d6250dbc6061 differ diff --git a/fuzzers/corpora/packfile/45a65193e30784b0124f4fed659eb7e46552c2d0 b/fuzzers/corpora/packfile/45a65193e30784b0124f4fed659eb7e46552c2d0 new file mode 100644 index 00000000000..5bd7dea14b4 --- /dev/null +++ b/fuzzers/corpora/packfile/45a65193e30784b0124f4fed659eb7e46552c2d0 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/45cff3494791ded181e1e3dab1c7a0e40130b57b b/fuzzers/corpora/packfile/45cff3494791ded181e1e3dab1c7a0e40130b57b new file mode 100644 index 00000000000..a92e1a51a15 Binary files /dev/null and b/fuzzers/corpora/packfile/45cff3494791ded181e1e3dab1c7a0e40130b57b differ diff --git a/fuzzers/corpora/packfile/481dedc2855981510024d48828af0fe35a1503dd b/fuzzers/corpora/packfile/481dedc2855981510024d48828af0fe35a1503dd new file mode 100644 index 00000000000..3d95cc33cd0 Binary files /dev/null and b/fuzzers/corpora/packfile/481dedc2855981510024d48828af0fe35a1503dd differ diff --git a/fuzzers/corpora/packfile/49a6448a722742b1b392f0471542ee0c572c5a9a b/fuzzers/corpora/packfile/49a6448a722742b1b392f0471542ee0c572c5a9a new file mode 100644 index 00000000000..c767ee576f6 Binary files /dev/null and b/fuzzers/corpora/packfile/49a6448a722742b1b392f0471542ee0c572c5a9a differ diff --git a/fuzzers/corpora/packfile/4a6e6af93dea13a5720be52b95f2948e0cab4602 b/fuzzers/corpora/packfile/4a6e6af93dea13a5720be52b95f2948e0cab4602 new file mode 100644 index 00000000000..013d77bc51f Binary files /dev/null and b/fuzzers/corpora/packfile/4a6e6af93dea13a5720be52b95f2948e0cab4602 differ diff --git a/fuzzers/corpora/packfile/4ac25548f35e06eb9f91b0f15b89db3cb5bcb283 b/fuzzers/corpora/packfile/4ac25548f35e06eb9f91b0f15b89db3cb5bcb283 new file mode 100644 index 00000000000..2ee658030f1 Binary files /dev/null and b/fuzzers/corpora/packfile/4ac25548f35e06eb9f91b0f15b89db3cb5bcb283 differ diff --git a/fuzzers/corpora/packfile/4b586169f192749a0aa023ad6e4edd2e15d67f53 b/fuzzers/corpora/packfile/4b586169f192749a0aa023ad6e4edd2e15d67f53 new file mode 100644 index 00000000000..d517c58a9ca Binary files /dev/null and b/fuzzers/corpora/packfile/4b586169f192749a0aa023ad6e4edd2e15d67f53 differ diff --git a/fuzzers/corpora/packfile/4c3c8ec0e25da9342dc87c2e78d3040c381514ce b/fuzzers/corpora/packfile/4c3c8ec0e25da9342dc87c2e78d3040c381514ce new file mode 100644 index 00000000000..c6a105949a9 Binary files /dev/null and b/fuzzers/corpora/packfile/4c3c8ec0e25da9342dc87c2e78d3040c381514ce differ diff --git a/fuzzers/corpora/packfile/4d5189cd1411daaa579df34591c6a5946204c9a0 b/fuzzers/corpora/packfile/4d5189cd1411daaa579df34591c6a5946204c9a0 new file mode 100644 index 00000000000..ac19046c876 Binary files /dev/null and b/fuzzers/corpora/packfile/4d5189cd1411daaa579df34591c6a5946204c9a0 differ diff --git a/fuzzers/corpora/packfile/4d7f1bfa928c0d3399598d562e346c6e23de6a03 b/fuzzers/corpora/packfile/4d7f1bfa928c0d3399598d562e346c6e23de6a03 new file mode 100644 index 00000000000..712cf004bc8 Binary files /dev/null and b/fuzzers/corpora/packfile/4d7f1bfa928c0d3399598d562e346c6e23de6a03 differ diff --git a/fuzzers/corpora/packfile/4eee38183d6fce3f42224738be58d0e3975300f4 b/fuzzers/corpora/packfile/4eee38183d6fce3f42224738be58d0e3975300f4 new file mode 100644 index 00000000000..4434b932779 Binary files /dev/null and b/fuzzers/corpora/packfile/4eee38183d6fce3f42224738be58d0e3975300f4 differ diff --git a/fuzzers/corpora/packfile/4f2e2af611d6567abcf5b6bfc579487ac417a8d4 b/fuzzers/corpora/packfile/4f2e2af611d6567abcf5b6bfc579487ac417a8d4 new file mode 100644 index 00000000000..d16ad16ac24 Binary files /dev/null and b/fuzzers/corpora/packfile/4f2e2af611d6567abcf5b6bfc579487ac417a8d4 differ diff --git a/fuzzers/corpora/packfile/4fa04b2c3ac839c36c4a3b51bf882eb99b7cd097 b/fuzzers/corpora/packfile/4fa04b2c3ac839c36c4a3b51bf882eb99b7cd097 new file mode 100644 index 00000000000..0102fe32d5b Binary files /dev/null and b/fuzzers/corpora/packfile/4fa04b2c3ac839c36c4a3b51bf882eb99b7cd097 differ diff --git a/fuzzers/corpora/packfile/4fbe10aede9fd9ce2030c6e567a9281e1a5338f4 b/fuzzers/corpora/packfile/4fbe10aede9fd9ce2030c6e567a9281e1a5338f4 new file mode 100644 index 00000000000..83ca0198cba Binary files /dev/null and b/fuzzers/corpora/packfile/4fbe10aede9fd9ce2030c6e567a9281e1a5338f4 differ diff --git a/fuzzers/corpora/packfile/5037f4f74273aed9a09122af5f4acc10f42c033a b/fuzzers/corpora/packfile/5037f4f74273aed9a09122af5f4acc10f42c033a new file mode 100644 index 00000000000..cc94079d3bf Binary files /dev/null and b/fuzzers/corpora/packfile/5037f4f74273aed9a09122af5f4acc10f42c033a differ diff --git a/fuzzers/corpora/packfile/511993d3c99719e38a6779073019dacd7178ddb9 b/fuzzers/corpora/packfile/511993d3c99719e38a6779073019dacd7178ddb9 new file mode 100644 index 00000000000..675f43ab433 --- /dev/null +++ b/fuzzers/corpora/packfile/511993d3c99719e38a6779073019dacd7178ddb9 @@ -0,0 +1 @@ +P \ No newline at end of file diff --git a/fuzzers/corpora/packfile/520aa436eab6343c3729f51f0f8048e6b87f6aeb b/fuzzers/corpora/packfile/520aa436eab6343c3729f51f0f8048e6b87f6aeb new file mode 100644 index 00000000000..b5ec5afb85e --- /dev/null +++ b/fuzzers/corpora/packfile/520aa436eab6343c3729f51f0f8048e6b87f6aeb @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/521e228f3b62dca81d87d2e7d5476657d7b5e0a9 b/fuzzers/corpora/packfile/521e228f3b62dca81d87d2e7d5476657d7b5e0a9 new file mode 100644 index 00000000000..c0ea9e99ffc Binary files /dev/null and b/fuzzers/corpora/packfile/521e228f3b62dca81d87d2e7d5476657d7b5e0a9 differ diff --git a/fuzzers/corpora/packfile/52e37dfd77d56769dc8a96388aa26695a8108dac b/fuzzers/corpora/packfile/52e37dfd77d56769dc8a96388aa26695a8108dac new file mode 100644 index 00000000000..57d12a9fb56 Binary files /dev/null and b/fuzzers/corpora/packfile/52e37dfd77d56769dc8a96388aa26695a8108dac differ diff --git a/fuzzers/corpora/packfile/5374fb6be0a406cf8d0e95771ecb032254d21305 b/fuzzers/corpora/packfile/5374fb6be0a406cf8d0e95771ecb032254d21305 new file mode 100644 index 00000000000..42b16b0eac8 Binary files /dev/null and b/fuzzers/corpora/packfile/5374fb6be0a406cf8d0e95771ecb032254d21305 differ diff --git a/fuzzers/corpora/packfile/53e1d4898c15c8ee3ef5e2fb279d151108725731 b/fuzzers/corpora/packfile/53e1d4898c15c8ee3ef5e2fb279d151108725731 new file mode 100644 index 00000000000..516958e0373 Binary files /dev/null and b/fuzzers/corpora/packfile/53e1d4898c15c8ee3ef5e2fb279d151108725731 differ diff --git a/fuzzers/corpora/packfile/53e61ad37ca92b7f6c0396e5fab1ed8743e73da0 b/fuzzers/corpora/packfile/53e61ad37ca92b7f6c0396e5fab1ed8743e73da0 new file mode 100644 index 00000000000..ded29680cd1 Binary files /dev/null and b/fuzzers/corpora/packfile/53e61ad37ca92b7f6c0396e5fab1ed8743e73da0 differ diff --git a/fuzzers/corpora/packfile/55df2a59ed6a888ee2f0cdfdcc8582696702de7a b/fuzzers/corpora/packfile/55df2a59ed6a888ee2f0cdfdcc8582696702de7a new file mode 100644 index 00000000000..a4a063a1573 --- /dev/null +++ b/fuzzers/corpora/packfile/55df2a59ed6a888ee2f0cdfdcc8582696702de7a @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/56a2020f68c5eb72786ea168cc2a7e8ea34ad9c2 b/fuzzers/corpora/packfile/56a2020f68c5eb72786ea168cc2a7e8ea34ad9c2 new file mode 100644 index 00000000000..be0ff0d1b8a Binary files /dev/null and b/fuzzers/corpora/packfile/56a2020f68c5eb72786ea168cc2a7e8ea34ad9c2 differ diff --git a/fuzzers/corpora/packfile/578678e78b72f8bcb9f414e4129ae5d85a4af914 b/fuzzers/corpora/packfile/578678e78b72f8bcb9f414e4129ae5d85a4af914 new file mode 100644 index 00000000000..74829542dff Binary files /dev/null and b/fuzzers/corpora/packfile/578678e78b72f8bcb9f414e4129ae5d85a4af914 differ diff --git a/fuzzers/corpora/packfile/5a8bc5597fd0b2b44194ffabce46e2fa94c1ffd7 b/fuzzers/corpora/packfile/5a8bc5597fd0b2b44194ffabce46e2fa94c1ffd7 new file mode 100644 index 00000000000..4c88499ed0a Binary files /dev/null and b/fuzzers/corpora/packfile/5a8bc5597fd0b2b44194ffabce46e2fa94c1ffd7 differ diff --git a/fuzzers/corpora/packfile/5bab61eb53176449e25c2c82f172b82cb13ffb9d b/fuzzers/corpora/packfile/5bab61eb53176449e25c2c82f172b82cb13ffb9d new file mode 100644 index 00000000000..0d758c9c7bc --- /dev/null +++ b/fuzzers/corpora/packfile/5bab61eb53176449e25c2c82f172b82cb13ffb9d @@ -0,0 +1 @@ +? \ No newline at end of file diff --git a/fuzzers/corpora/packfile/5c2dd944dde9e08881bef0894fe7b22a5c9c4b06 b/fuzzers/corpora/packfile/5c2dd944dde9e08881bef0894fe7b22a5c9c4b06 new file mode 100644 index 00000000000..0fe2fa50e8e --- /dev/null +++ b/fuzzers/corpora/packfile/5c2dd944dde9e08881bef0894fe7b22a5c9c4b06 @@ -0,0 +1 @@ +j \ No newline at end of file diff --git a/fuzzers/corpora/packfile/5cb4674f4468d39f061d1df3c95b9c2dca529c54 b/fuzzers/corpora/packfile/5cb4674f4468d39f061d1df3c95b9c2dca529c54 new file mode 100644 index 00000000000..9b54d9fa2da Binary files /dev/null and b/fuzzers/corpora/packfile/5cb4674f4468d39f061d1df3c95b9c2dca529c54 differ diff --git a/fuzzers/corpora/packfile/60b6fbfe65dc1796a09a734e054223aba8c90260 b/fuzzers/corpora/packfile/60b6fbfe65dc1796a09a734e054223aba8c90260 new file mode 100644 index 00000000000..a7df9a62329 Binary files /dev/null and b/fuzzers/corpora/packfile/60b6fbfe65dc1796a09a734e054223aba8c90260 differ diff --git a/fuzzers/corpora/packfile/611f5b9368427ef823f7ed89ad23667b02a06435 b/fuzzers/corpora/packfile/611f5b9368427ef823f7ed89ad23667b02a06435 new file mode 100644 index 00000000000..56ee3803743 Binary files /dev/null and b/fuzzers/corpora/packfile/611f5b9368427ef823f7ed89ad23667b02a06435 differ diff --git a/fuzzers/corpora/packfile/6214b4afdbfe63400ce428d47a58a2e29f682738 b/fuzzers/corpora/packfile/6214b4afdbfe63400ce428d47a58a2e29f682738 new file mode 100644 index 00000000000..09e3c40977b Binary files /dev/null and b/fuzzers/corpora/packfile/6214b4afdbfe63400ce428d47a58a2e29f682738 differ diff --git a/fuzzers/corpora/packfile/634b675b80d51b52c3c6fbc73181ed47f61749ba b/fuzzers/corpora/packfile/634b675b80d51b52c3c6fbc73181ed47f61749ba new file mode 100644 index 00000000000..9afac4f1030 Binary files /dev/null and b/fuzzers/corpora/packfile/634b675b80d51b52c3c6fbc73181ed47f61749ba differ diff --git a/fuzzers/corpora/packfile/6431f1b31dc492fad89732b7d3e511fa7361985d b/fuzzers/corpora/packfile/6431f1b31dc492fad89732b7d3e511fa7361985d new file mode 100644 index 00000000000..b68409e30b7 Binary files /dev/null and b/fuzzers/corpora/packfile/6431f1b31dc492fad89732b7d3e511fa7361985d differ diff --git a/fuzzers/corpora/packfile/6442fd4bbb7656f142c92050da17b0e81e79fad1 b/fuzzers/corpora/packfile/6442fd4bbb7656f142c92050da17b0e81e79fad1 new file mode 100644 index 00000000000..2ca2d9605e8 Binary files /dev/null and b/fuzzers/corpora/packfile/6442fd4bbb7656f142c92050da17b0e81e79fad1 differ diff --git a/fuzzers/corpora/packfile/6486c8cf6bcc2fca60502564924f6b266399df3d b/fuzzers/corpora/packfile/6486c8cf6bcc2fca60502564924f6b266399df3d new file mode 100644 index 00000000000..a80e2090c00 Binary files /dev/null and b/fuzzers/corpora/packfile/6486c8cf6bcc2fca60502564924f6b266399df3d differ diff --git a/fuzzers/corpora/packfile/651c573b6fdd393e97536a47f8b9e65226e29c33 b/fuzzers/corpora/packfile/651c573b6fdd393e97536a47f8b9e65226e29c33 new file mode 100644 index 00000000000..ec11120dd08 Binary files /dev/null and b/fuzzers/corpora/packfile/651c573b6fdd393e97536a47f8b9e65226e29c33 differ diff --git a/fuzzers/corpora/packfile/657fc646e93cb999417f43f0ec77fbad694e3e18 b/fuzzers/corpora/packfile/657fc646e93cb999417f43f0ec77fbad694e3e18 new file mode 100644 index 00000000000..acb56a8b7e0 Binary files /dev/null and b/fuzzers/corpora/packfile/657fc646e93cb999417f43f0ec77fbad694e3e18 differ diff --git a/fuzzers/corpora/packfile/65cc90263dec0020ceabc727d33aa587e57fc175 b/fuzzers/corpora/packfile/65cc90263dec0020ceabc727d33aa587e57fc175 new file mode 100644 index 00000000000..d14a6c30dfa --- /dev/null +++ b/fuzzers/corpora/packfile/65cc90263dec0020ceabc727d33aa587e57fc175 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/688934845f22049cb14668832efa33d45013b6b9 b/fuzzers/corpora/packfile/688934845f22049cb14668832efa33d45013b6b9 new file mode 100644 index 00000000000..15294a501aa Binary files /dev/null and b/fuzzers/corpora/packfile/688934845f22049cb14668832efa33d45013b6b9 differ diff --git a/fuzzers/corpora/packfile/6b0d31c0d563223024da45691584643ac78c96e8 b/fuzzers/corpora/packfile/6b0d31c0d563223024da45691584643ac78c96e8 new file mode 100644 index 00000000000..08b9811c98f --- /dev/null +++ b/fuzzers/corpora/packfile/6b0d31c0d563223024da45691584643ac78c96e8 @@ -0,0 +1 @@ +m \ No newline at end of file diff --git a/fuzzers/corpora/packfile/6b4dc6028a3a684a20dbc427b15a37ea2fd12dd1 b/fuzzers/corpora/packfile/6b4dc6028a3a684a20dbc427b15a37ea2fd12dd1 new file mode 100644 index 00000000000..d76aa6f32da Binary files /dev/null and b/fuzzers/corpora/packfile/6b4dc6028a3a684a20dbc427b15a37ea2fd12dd1 differ diff --git a/fuzzers/corpora/packfile/6b7486fc2a47a40eb5a85a5edf53af60d48be7d5 b/fuzzers/corpora/packfile/6b7486fc2a47a40eb5a85a5edf53af60d48be7d5 new file mode 100644 index 00000000000..d1e0eedf925 Binary files /dev/null and b/fuzzers/corpora/packfile/6b7486fc2a47a40eb5a85a5edf53af60d48be7d5 differ diff --git a/fuzzers/corpora/packfile/6bace82ea640ac0a78963c79483faf0faa7fd168 b/fuzzers/corpora/packfile/6bace82ea640ac0a78963c79483faf0faa7fd168 new file mode 100644 index 00000000000..8c53a7f5f36 --- /dev/null +++ b/fuzzers/corpora/packfile/6bace82ea640ac0a78963c79483faf0faa7fd168 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/6ca38da5f096a5847776e4d50cb63121341fd67c b/fuzzers/corpora/packfile/6ca38da5f096a5847776e4d50cb63121341fd67c new file mode 100644 index 00000000000..628bda022c6 Binary files /dev/null and b/fuzzers/corpora/packfile/6ca38da5f096a5847776e4d50cb63121341fd67c differ diff --git a/fuzzers/corpora/packfile/6d344a65b9edced36045f94215b6810799789334 b/fuzzers/corpora/packfile/6d344a65b9edced36045f94215b6810799789334 new file mode 100644 index 00000000000..1c794422fcf Binary files /dev/null and b/fuzzers/corpora/packfile/6d344a65b9edced36045f94215b6810799789334 differ diff --git a/fuzzers/corpora/packfile/6dd655e8ef0573eb1c41151af924974aa1e9c454 b/fuzzers/corpora/packfile/6dd655e8ef0573eb1c41151af924974aa1e9c454 new file mode 100644 index 00000000000..53047563e61 Binary files /dev/null and b/fuzzers/corpora/packfile/6dd655e8ef0573eb1c41151af924974aa1e9c454 differ diff --git a/fuzzers/corpora/packfile/6e118259c2940fafba0a9ceeef6308e12e881ae1 b/fuzzers/corpora/packfile/6e118259c2940fafba0a9ceeef6308e12e881ae1 new file mode 100644 index 00000000000..23597c3e5f3 Binary files /dev/null and b/fuzzers/corpora/packfile/6e118259c2940fafba0a9ceeef6308e12e881ae1 differ diff --git a/fuzzers/corpora/packfile/6e4b5ef83333606a16a63b579f221f6ffb7b48ee b/fuzzers/corpora/packfile/6e4b5ef83333606a16a63b579f221f6ffb7b48ee new file mode 100644 index 00000000000..6d72c9a90f1 --- /dev/null +++ b/fuzzers/corpora/packfile/6e4b5ef83333606a16a63b579f221f6ffb7b48ee @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/6f47ff60d54c012103a0c28851ffa9eab3002511 b/fuzzers/corpora/packfile/6f47ff60d54c012103a0c28851ffa9eab3002511 new file mode 100644 index 00000000000..a7efd537f31 Binary files /dev/null and b/fuzzers/corpora/packfile/6f47ff60d54c012103a0c28851ffa9eab3002511 differ diff --git a/fuzzers/corpora/packfile/701a765befff451207517d56c3fe8609d059867d b/fuzzers/corpora/packfile/701a765befff451207517d56c3fe8609d059867d new file mode 100644 index 00000000000..ee936e331d8 Binary files /dev/null and b/fuzzers/corpora/packfile/701a765befff451207517d56c3fe8609d059867d differ diff --git a/fuzzers/corpora/packfile/7050f56d64b28499c89d5430761f18a8a2a933d4 b/fuzzers/corpora/packfile/7050f56d64b28499c89d5430761f18a8a2a933d4 new file mode 100644 index 00000000000..a3df39612cb Binary files /dev/null and b/fuzzers/corpora/packfile/7050f56d64b28499c89d5430761f18a8a2a933d4 differ diff --git a/fuzzers/corpora/packfile/724fa0194f615e1a0f08184a9f1520123f8e2833 b/fuzzers/corpora/packfile/724fa0194f615e1a0f08184a9f1520123f8e2833 new file mode 100644 index 00000000000..dd6df785081 Binary files /dev/null and b/fuzzers/corpora/packfile/724fa0194f615e1a0f08184a9f1520123f8e2833 differ diff --git a/fuzzers/corpora/packfile/72c52d0d98717e21dfee45418a046a19198b5d5d b/fuzzers/corpora/packfile/72c52d0d98717e21dfee45418a046a19198b5d5d new file mode 100644 index 00000000000..922e3068321 Binary files /dev/null and b/fuzzers/corpora/packfile/72c52d0d98717e21dfee45418a046a19198b5d5d differ diff --git a/fuzzers/corpora/packfile/72cec0949c5743ee1df67b41ece5d6806f9bede6 b/fuzzers/corpora/packfile/72cec0949c5743ee1df67b41ece5d6806f9bede6 new file mode 100644 index 00000000000..366439dbb7f Binary files /dev/null and b/fuzzers/corpora/packfile/72cec0949c5743ee1df67b41ece5d6806f9bede6 differ diff --git a/fuzzers/corpora/packfile/72e6bfb7b881befc0b461334411d70ae227a426a b/fuzzers/corpora/packfile/72e6bfb7b881befc0b461334411d70ae227a426a new file mode 100644 index 00000000000..c7986f68713 Binary files /dev/null and b/fuzzers/corpora/packfile/72e6bfb7b881befc0b461334411d70ae227a426a differ diff --git a/fuzzers/corpora/packfile/73b74736664ad85828ce1be2e29fb4a68d24402b b/fuzzers/corpora/packfile/73b74736664ad85828ce1be2e29fb4a68d24402b new file mode 100644 index 00000000000..009080e8e0b --- /dev/null +++ b/fuzzers/corpora/packfile/73b74736664ad85828ce1be2e29fb4a68d24402b @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/745bedb79413d20844a8b0e96fbec51b4989c65d b/fuzzers/corpora/packfile/745bedb79413d20844a8b0e96fbec51b4989c65d new file mode 100644 index 00000000000..0022a3ee843 --- /dev/null +++ b/fuzzers/corpora/packfile/745bedb79413d20844a8b0e96fbec51b4989c65d @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/748142c17e56d0f0ad9e4d6525b39294d81261d6 b/fuzzers/corpora/packfile/748142c17e56d0f0ad9e4d6525b39294d81261d6 new file mode 100644 index 00000000000..7ac1cad4dd9 Binary files /dev/null and b/fuzzers/corpora/packfile/748142c17e56d0f0ad9e4d6525b39294d81261d6 differ diff --git a/fuzzers/corpora/packfile/74dfea2e26741a8778fb942d1d60a84d0759d7a0 b/fuzzers/corpora/packfile/74dfea2e26741a8778fb942d1d60a84d0759d7a0 new file mode 100644 index 00000000000..fe307f6da29 --- /dev/null +++ b/fuzzers/corpora/packfile/74dfea2e26741a8778fb942d1d60a84d0759d7a0 @@ -0,0 +1 @@ +o \ No newline at end of file diff --git a/fuzzers/corpora/packfile/767b2efbb7502a2f652a448b471693d32c128677 b/fuzzers/corpora/packfile/767b2efbb7502a2f652a448b471693d32c128677 new file mode 100644 index 00000000000..90a8f3a7d53 Binary files /dev/null and b/fuzzers/corpora/packfile/767b2efbb7502a2f652a448b471693d32c128677 differ diff --git a/fuzzers/corpora/packfile/78abe558c4277852128d4b91282edcb68f86bdea b/fuzzers/corpora/packfile/78abe558c4277852128d4b91282edcb68f86bdea new file mode 100644 index 00000000000..30644d14801 Binary files /dev/null and b/fuzzers/corpora/packfile/78abe558c4277852128d4b91282edcb68f86bdea differ diff --git a/fuzzers/corpora/packfile/7960246c2db6d39e68dfe93ded358a3acba8f896 b/fuzzers/corpora/packfile/7960246c2db6d39e68dfe93ded358a3acba8f896 new file mode 100644 index 00000000000..4d3703ac21e Binary files /dev/null and b/fuzzers/corpora/packfile/7960246c2db6d39e68dfe93ded358a3acba8f896 differ diff --git a/fuzzers/corpora/packfile/7a4ff814176b55af008ad9580201d5e659242f05 b/fuzzers/corpora/packfile/7a4ff814176b55af008ad9580201d5e659242f05 new file mode 100644 index 00000000000..029e4fc32ca Binary files /dev/null and b/fuzzers/corpora/packfile/7a4ff814176b55af008ad9580201d5e659242f05 differ diff --git a/fuzzers/corpora/packfile/7a752694fce29ded083fbabbc9ff95f5b92a3d9c b/fuzzers/corpora/packfile/7a752694fce29ded083fbabbc9ff95f5b92a3d9c new file mode 100644 index 00000000000..82b5593f67a Binary files /dev/null and b/fuzzers/corpora/packfile/7a752694fce29ded083fbabbc9ff95f5b92a3d9c differ diff --git a/fuzzers/corpora/packfile/7a81af3e591ac713f81ea1efe93dcf36157d8376 b/fuzzers/corpora/packfile/7a81af3e591ac713f81ea1efe93dcf36157d8376 new file mode 100644 index 00000000000..883ad6e8ef9 --- /dev/null +++ b/fuzzers/corpora/packfile/7a81af3e591ac713f81ea1efe93dcf36157d8376 @@ -0,0 +1 @@ +o \ No newline at end of file diff --git a/fuzzers/corpora/packfile/7c957a1fd650f9ae0eadc112837ea451a692affc b/fuzzers/corpora/packfile/7c957a1fd650f9ae0eadc112837ea451a692affc new file mode 100644 index 00000000000..251bc956ca9 Binary files /dev/null and b/fuzzers/corpora/packfile/7c957a1fd650f9ae0eadc112837ea451a692affc differ diff --git a/fuzzers/corpora/packfile/7cda4ab6a0daf50f675d5225cbc166c86a8ef95f b/fuzzers/corpora/packfile/7cda4ab6a0daf50f675d5225cbc166c86a8ef95f new file mode 100644 index 00000000000..9e58e190e71 Binary files /dev/null and b/fuzzers/corpora/packfile/7cda4ab6a0daf50f675d5225cbc166c86a8ef95f differ diff --git a/fuzzers/corpora/packfile/7cf184f4c67ad58283ecb19349720b0cae756829 b/fuzzers/corpora/packfile/7cf184f4c67ad58283ecb19349720b0cae756829 new file mode 100644 index 00000000000..8ac2eb50895 --- /dev/null +++ b/fuzzers/corpora/packfile/7cf184f4c67ad58283ecb19349720b0cae756829 @@ -0,0 +1 @@ +H \ No newline at end of file diff --git a/fuzzers/corpora/packfile/7df1ea8d86d601c3bd39977ea85e5f74c9db6acb b/fuzzers/corpora/packfile/7df1ea8d86d601c3bd39977ea85e5f74c9db6acb new file mode 100644 index 00000000000..0865a4aad69 Binary files /dev/null and b/fuzzers/corpora/packfile/7df1ea8d86d601c3bd39977ea85e5f74c9db6acb differ diff --git a/fuzzers/corpora/packfile/820ec3e39089d863641a1be6942445db3ff34270 b/fuzzers/corpora/packfile/820ec3e39089d863641a1be6942445db3ff34270 new file mode 100644 index 00000000000..a7a949a73e3 Binary files /dev/null and b/fuzzers/corpora/packfile/820ec3e39089d863641a1be6942445db3ff34270 differ diff --git a/fuzzers/corpora/packfile/827704fd978bd02a46268b7396b202a52ad261ed b/fuzzers/corpora/packfile/827704fd978bd02a46268b7396b202a52ad261ed new file mode 100644 index 00000000000..855e5fcd6b9 Binary files /dev/null and b/fuzzers/corpora/packfile/827704fd978bd02a46268b7396b202a52ad261ed differ diff --git a/fuzzers/corpora/packfile/828acfc1d49a0fdbcd9f238138ff65582c2a9fc8 b/fuzzers/corpora/packfile/828acfc1d49a0fdbcd9f238138ff65582c2a9fc8 new file mode 100644 index 00000000000..cf479adb08b Binary files /dev/null and b/fuzzers/corpora/packfile/828acfc1d49a0fdbcd9f238138ff65582c2a9fc8 differ diff --git a/fuzzers/corpora/packfile/8306a2f04a47fe4c95098675ffa25c074ecd89de b/fuzzers/corpora/packfile/8306a2f04a47fe4c95098675ffa25c074ecd89de new file mode 100644 index 00000000000..44cda73a297 Binary files /dev/null and b/fuzzers/corpora/packfile/8306a2f04a47fe4c95098675ffa25c074ecd89de differ diff --git a/fuzzers/corpora/packfile/8327db1c6a884d8b3e3cefd94cec9eb94bffae0a b/fuzzers/corpora/packfile/8327db1c6a884d8b3e3cefd94cec9eb94bffae0a new file mode 100644 index 00000000000..315e1e666a6 Binary files /dev/null and b/fuzzers/corpora/packfile/8327db1c6a884d8b3e3cefd94cec9eb94bffae0a differ diff --git a/fuzzers/corpora/packfile/847f4e42f8f2730a48d19951d8829621b2e70082 b/fuzzers/corpora/packfile/847f4e42f8f2730a48d19951d8829621b2e70082 new file mode 100644 index 00000000000..22952d50619 Binary files /dev/null and b/fuzzers/corpora/packfile/847f4e42f8f2730a48d19951d8829621b2e70082 differ diff --git a/fuzzers/corpora/packfile/84a516841ba77a5b4648de2cd0dfcb30ea46dbb4 b/fuzzers/corpora/packfile/84a516841ba77a5b4648de2cd0dfcb30ea46dbb4 new file mode 100644 index 00000000000..3410062ba67 --- /dev/null +++ b/fuzzers/corpora/packfile/84a516841ba77a5b4648de2cd0dfcb30ea46dbb4 @@ -0,0 +1 @@ +c \ No newline at end of file diff --git a/fuzzers/corpora/packfile/8552526f5aba95119c0b95b61cd40386e7a3738b b/fuzzers/corpora/packfile/8552526f5aba95119c0b95b61cd40386e7a3738b new file mode 100644 index 00000000000..6a29f0f41b0 Binary files /dev/null and b/fuzzers/corpora/packfile/8552526f5aba95119c0b95b61cd40386e7a3738b differ diff --git a/fuzzers/corpora/packfile/8565db62ac64209ff009ac40e7c2d2ac4ae944eb b/fuzzers/corpora/packfile/8565db62ac64209ff009ac40e7c2d2ac4ae944eb new file mode 100644 index 00000000000..066b1daa9f0 Binary files /dev/null and b/fuzzers/corpora/packfile/8565db62ac64209ff009ac40e7c2d2ac4ae944eb differ diff --git a/fuzzers/corpora/packfile/859b3346967c5c3c136459e565b402f9a936aa0d b/fuzzers/corpora/packfile/859b3346967c5c3c136459e565b402f9a936aa0d new file mode 100644 index 00000000000..9c10c980fcf Binary files /dev/null and b/fuzzers/corpora/packfile/859b3346967c5c3c136459e565b402f9a936aa0d differ diff --git a/fuzzers/corpora/packfile/86a69caf3c5866d78d80da087b1b843cfea5e907 b/fuzzers/corpora/packfile/86a69caf3c5866d78d80da087b1b843cfea5e907 new file mode 100644 index 00000000000..b122e37880d Binary files /dev/null and b/fuzzers/corpora/packfile/86a69caf3c5866d78d80da087b1b843cfea5e907 differ diff --git a/fuzzers/corpora/packfile/86e1fb54a04fc18ee482b794ba3b2b306f6515d4 b/fuzzers/corpora/packfile/86e1fb54a04fc18ee482b794ba3b2b306f6515d4 new file mode 100644 index 00000000000..7d310551793 Binary files /dev/null and b/fuzzers/corpora/packfile/86e1fb54a04fc18ee482b794ba3b2b306f6515d4 differ diff --git a/fuzzers/corpora/packfile/86f217ee467d31ad9ad2a8c502b91279cd7f1c40 b/fuzzers/corpora/packfile/86f217ee467d31ad9ad2a8c502b91279cd7f1c40 new file mode 100644 index 00000000000..9951e266654 Binary files /dev/null and b/fuzzers/corpora/packfile/86f217ee467d31ad9ad2a8c502b91279cd7f1c40 differ diff --git a/fuzzers/corpora/packfile/8768a53e1d4c182907306300f9ca90cfd8018383 b/fuzzers/corpora/packfile/8768a53e1d4c182907306300f9ca90cfd8018383 new file mode 100644 index 00000000000..3ea63c2ccdc --- /dev/null +++ b/fuzzers/corpora/packfile/8768a53e1d4c182907306300f9ca90cfd8018383 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/8834a7aac170c494f45aa4da71b9605a52d82435 b/fuzzers/corpora/packfile/8834a7aac170c494f45aa4da71b9605a52d82435 new file mode 100644 index 00000000000..40d2f60591e Binary files /dev/null and b/fuzzers/corpora/packfile/8834a7aac170c494f45aa4da71b9605a52d82435 differ diff --git a/fuzzers/corpora/packfile/883f023f38a031d8a8e8ce2cba6614b9bff5d41f b/fuzzers/corpora/packfile/883f023f38a031d8a8e8ce2cba6614b9bff5d41f new file mode 100644 index 00000000000..30928b6e073 Binary files /dev/null and b/fuzzers/corpora/packfile/883f023f38a031d8a8e8ce2cba6614b9bff5d41f differ diff --git a/fuzzers/corpora/packfile/88738071086eb04e47b77d1ca28b35ddbfaa0968 b/fuzzers/corpora/packfile/88738071086eb04e47b77d1ca28b35ddbfaa0968 new file mode 100644 index 00000000000..7c1f28e447d Binary files /dev/null and b/fuzzers/corpora/packfile/88738071086eb04e47b77d1ca28b35ddbfaa0968 differ diff --git a/fuzzers/corpora/packfile/892aef744c87c6ee4ba3dd457c7ca02ba3d359bd b/fuzzers/corpora/packfile/892aef744c87c6ee4ba3dd457c7ca02ba3d359bd new file mode 100644 index 00000000000..16e6f0a7b66 Binary files /dev/null and b/fuzzers/corpora/packfile/892aef744c87c6ee4ba3dd457c7ca02ba3d359bd differ diff --git a/fuzzers/corpora/packfile/8afb5c282d23c4055500f88a10b26383c682c900 b/fuzzers/corpora/packfile/8afb5c282d23c4055500f88a10b26383c682c900 new file mode 100644 index 00000000000..cf91507b420 Binary files /dev/null and b/fuzzers/corpora/packfile/8afb5c282d23c4055500f88a10b26383c682c900 differ diff --git a/fuzzers/corpora/packfile/8b3dfce4cd7b8942eedb52af0e9ca4caa5c6de61 b/fuzzers/corpora/packfile/8b3dfce4cd7b8942eedb52af0e9ca4caa5c6de61 new file mode 100644 index 00000000000..4c8cc0c220f Binary files /dev/null and b/fuzzers/corpora/packfile/8b3dfce4cd7b8942eedb52af0e9ca4caa5c6de61 differ diff --git a/fuzzers/corpora/packfile/8c2cccf751bb5844bea8dc63c22e3f8e4489411e b/fuzzers/corpora/packfile/8c2cccf751bb5844bea8dc63c22e3f8e4489411e new file mode 100644 index 00000000000..70e2d300a6d Binary files /dev/null and b/fuzzers/corpora/packfile/8c2cccf751bb5844bea8dc63c22e3f8e4489411e differ diff --git a/fuzzers/corpora/packfile/8e30894298502ba4d43af98f1ec3088f9b8f29d5 b/fuzzers/corpora/packfile/8e30894298502ba4d43af98f1ec3088f9b8f29d5 new file mode 100644 index 00000000000..c4df8023c13 Binary files /dev/null and b/fuzzers/corpora/packfile/8e30894298502ba4d43af98f1ec3088f9b8f29d5 differ diff --git a/fuzzers/corpora/packfile/8eb4d738f7170d2e0594b779f782eb3171c9d421 b/fuzzers/corpora/packfile/8eb4d738f7170d2e0594b779f782eb3171c9d421 new file mode 100644 index 00000000000..410a7d63ae5 Binary files /dev/null and b/fuzzers/corpora/packfile/8eb4d738f7170d2e0594b779f782eb3171c9d421 differ diff --git a/fuzzers/corpora/packfile/8f46a043da3aa5d466ade170e62b0b9f362b4c5b b/fuzzers/corpora/packfile/8f46a043da3aa5d466ade170e62b0b9f362b4c5b new file mode 100644 index 00000000000..172736f8cfe Binary files /dev/null and b/fuzzers/corpora/packfile/8f46a043da3aa5d466ade170e62b0b9f362b4c5b differ diff --git a/fuzzers/corpora/packfile/9295f39686016bf3abb1d6e9924d6725c1263920 b/fuzzers/corpora/packfile/9295f39686016bf3abb1d6e9924d6725c1263920 new file mode 100644 index 00000000000..04be53351b3 Binary files /dev/null and b/fuzzers/corpora/packfile/9295f39686016bf3abb1d6e9924d6725c1263920 differ diff --git a/fuzzers/corpora/packfile/92fa2c2237724e7ba49e9c59adad8d61ce400bbf b/fuzzers/corpora/packfile/92fa2c2237724e7ba49e9c59adad8d61ce400bbf new file mode 100644 index 00000000000..b890df0d3c6 Binary files /dev/null and b/fuzzers/corpora/packfile/92fa2c2237724e7ba49e9c59adad8d61ce400bbf differ diff --git a/fuzzers/corpora/packfile/936548b53e1a1e30cb30747a87203abd4eae78ea b/fuzzers/corpora/packfile/936548b53e1a1e30cb30747a87203abd4eae78ea new file mode 100644 index 00000000000..21717701c22 Binary files /dev/null and b/fuzzers/corpora/packfile/936548b53e1a1e30cb30747a87203abd4eae78ea differ diff --git a/fuzzers/corpora/packfile/9835ad3ff27939bc1315632d6a22980b377c36e4 b/fuzzers/corpora/packfile/9835ad3ff27939bc1315632d6a22980b377c36e4 new file mode 100644 index 00000000000..025ad5085a0 --- /dev/null +++ b/fuzzers/corpora/packfile/9835ad3ff27939bc1315632d6a22980b377c36e4 @@ -0,0 +1 @@ +P \ No newline at end of file diff --git a/fuzzers/corpora/packfile/9857740c36a95415fa3be04cdf184db7b41a8b3e b/fuzzers/corpora/packfile/9857740c36a95415fa3be04cdf184db7b41a8b3e new file mode 100644 index 00000000000..7d3242f1040 Binary files /dev/null and b/fuzzers/corpora/packfile/9857740c36a95415fa3be04cdf184db7b41a8b3e differ diff --git a/fuzzers/corpora/packfile/98c35b9d5e7b430d0d4ef70f642d8e2f3266b6d4 b/fuzzers/corpora/packfile/98c35b9d5e7b430d0d4ef70f642d8e2f3266b6d4 new file mode 100644 index 00000000000..ea7c76eb1b2 Binary files /dev/null and b/fuzzers/corpora/packfile/98c35b9d5e7b430d0d4ef70f642d8e2f3266b6d4 differ diff --git a/fuzzers/corpora/packfile/9929b50ac145c0781a0347be1559764edc668173 b/fuzzers/corpora/packfile/9929b50ac145c0781a0347be1559764edc668173 new file mode 100644 index 00000000000..33f1e92c4d6 --- /dev/null +++ b/fuzzers/corpora/packfile/9929b50ac145c0781a0347be1559764edc668173 @@ -0,0 +1 @@ +PACK \ No newline at end of file diff --git a/fuzzers/corpora/packfile/9bf6a450d87badf2d495c2df37081ea16156915a b/fuzzers/corpora/packfile/9bf6a450d87badf2d495c2df37081ea16156915a new file mode 100644 index 00000000000..ff4c4838943 Binary files /dev/null and b/fuzzers/corpora/packfile/9bf6a450d87badf2d495c2df37081ea16156915a differ diff --git a/fuzzers/corpora/packfile/9bffb3ff7a4429144305b770162074bbffe39ce9 b/fuzzers/corpora/packfile/9bffb3ff7a4429144305b770162074bbffe39ce9 new file mode 100644 index 00000000000..aa9c335f5d5 Binary files /dev/null and b/fuzzers/corpora/packfile/9bffb3ff7a4429144305b770162074bbffe39ce9 differ diff --git a/fuzzers/corpora/packfile/9c040d3207196e3aeee0df389170d6e59733ba0f b/fuzzers/corpora/packfile/9c040d3207196e3aeee0df389170d6e59733ba0f new file mode 100644 index 00000000000..4c4e68adbf4 Binary files /dev/null and b/fuzzers/corpora/packfile/9c040d3207196e3aeee0df389170d6e59733ba0f differ diff --git a/fuzzers/corpora/packfile/9c740d0f3b8875a3b19f1cf1a88d5192a997a68d b/fuzzers/corpora/packfile/9c740d0f3b8875a3b19f1cf1a88d5192a997a68d new file mode 100644 index 00000000000..1f1ea65372c Binary files /dev/null and b/fuzzers/corpora/packfile/9c740d0f3b8875a3b19f1cf1a88d5192a997a68d differ diff --git a/fuzzers/corpora/packfile/9cf72097400efb70d06179e6b00abb4cdec74e66 b/fuzzers/corpora/packfile/9cf72097400efb70d06179e6b00abb4cdec74e66 new file mode 100644 index 00000000000..25cfb391fa6 Binary files /dev/null and b/fuzzers/corpora/packfile/9cf72097400efb70d06179e6b00abb4cdec74e66 differ diff --git a/fuzzers/corpora/packfile/9d36c270ef1f14214742562238dc747242d4756e b/fuzzers/corpora/packfile/9d36c270ef1f14214742562238dc747242d4756e new file mode 100644 index 00000000000..c54dc55fae0 Binary files /dev/null and b/fuzzers/corpora/packfile/9d36c270ef1f14214742562238dc747242d4756e differ diff --git a/fuzzers/corpora/packfile/9fb415ccadc8e7b0f38646ec5782d5895111e259 b/fuzzers/corpora/packfile/9fb415ccadc8e7b0f38646ec5782d5895111e259 new file mode 100644 index 00000000000..473ebb7084f Binary files /dev/null and b/fuzzers/corpora/packfile/9fb415ccadc8e7b0f38646ec5782d5895111e259 differ diff --git a/fuzzers/corpora/packfile/a13b7fbb454fe3bdebd07a51d466484aa41ee142 b/fuzzers/corpora/packfile/a13b7fbb454fe3bdebd07a51d466484aa41ee142 new file mode 100644 index 00000000000..48edf15f510 Binary files /dev/null and b/fuzzers/corpora/packfile/a13b7fbb454fe3bdebd07a51d466484aa41ee142 differ diff --git a/fuzzers/corpora/packfile/a1a7715c7596c77b892dc6d4debb7c108ca4ef97 b/fuzzers/corpora/packfile/a1a7715c7596c77b892dc6d4debb7c108ca4ef97 new file mode 100644 index 00000000000..395831125ca --- /dev/null +++ b/fuzzers/corpora/packfile/a1a7715c7596c77b892dc6d4debb7c108ca4ef97 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/a1ac8b656af02b56aefe6029db36b1af9fb664ef b/fuzzers/corpora/packfile/a1ac8b656af02b56aefe6029db36b1af9fb664ef new file mode 100644 index 00000000000..eeae7697244 Binary files /dev/null and b/fuzzers/corpora/packfile/a1ac8b656af02b56aefe6029db36b1af9fb664ef differ diff --git a/fuzzers/corpora/packfile/a343687e2522222c2d49e8cb18d3feda64cf1d66 b/fuzzers/corpora/packfile/a343687e2522222c2d49e8cb18d3feda64cf1d66 new file mode 100644 index 00000000000..7d24f8cde20 Binary files /dev/null and b/fuzzers/corpora/packfile/a343687e2522222c2d49e8cb18d3feda64cf1d66 differ diff --git a/fuzzers/corpora/packfile/a6f57425137e9aa54537f0b3f5364ce165aedb0a b/fuzzers/corpora/packfile/a6f57425137e9aa54537f0b3f5364ce165aedb0a new file mode 100644 index 00000000000..d50394efdf1 --- /dev/null +++ b/fuzzers/corpora/packfile/a6f57425137e9aa54537f0b3f5364ce165aedb0a @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/a73df4ce29f75cc638a7a2d823fee57d909ab681 b/fuzzers/corpora/packfile/a73df4ce29f75cc638a7a2d823fee57d909ab681 new file mode 100644 index 00000000000..1a443b91064 Binary files /dev/null and b/fuzzers/corpora/packfile/a73df4ce29f75cc638a7a2d823fee57d909ab681 differ diff --git a/fuzzers/corpora/packfile/a7ee38bb7be4fc44198cb2685d9601dcf2b9f569 b/fuzzers/corpora/packfile/a7ee38bb7be4fc44198cb2685d9601dcf2b9f569 new file mode 100644 index 00000000000..449e49efc2a --- /dev/null +++ b/fuzzers/corpora/packfile/a7ee38bb7be4fc44198cb2685d9601dcf2b9f569 @@ -0,0 +1 @@ +K \ No newline at end of file diff --git a/fuzzers/corpora/packfile/a8b9b91157274e617bf4ac5045fc0c6ac97e76f7 b/fuzzers/corpora/packfile/a8b9b91157274e617bf4ac5045fc0c6ac97e76f7 new file mode 100644 index 00000000000..8e4277ba878 Binary files /dev/null and b/fuzzers/corpora/packfile/a8b9b91157274e617bf4ac5045fc0c6ac97e76f7 differ diff --git a/fuzzers/corpora/packfile/a9c697f383f59a3b0642cd55b88190bce6201bae b/fuzzers/corpora/packfile/a9c697f383f59a3b0642cd55b88190bce6201bae new file mode 100644 index 00000000000..990de0a2afe Binary files /dev/null and b/fuzzers/corpora/packfile/a9c697f383f59a3b0642cd55b88190bce6201bae differ diff --git a/fuzzers/corpora/packfile/ab064cd6847c0fa546bbec4241eb9b095e0e73da b/fuzzers/corpora/packfile/ab064cd6847c0fa546bbec4241eb9b095e0e73da new file mode 100644 index 00000000000..b95307b1c42 Binary files /dev/null and b/fuzzers/corpora/packfile/ab064cd6847c0fa546bbec4241eb9b095e0e73da differ diff --git a/fuzzers/corpora/packfile/ab2c64588d3d9dc5c54c48d414e6d46d6a78cfa6 b/fuzzers/corpora/packfile/ab2c64588d3d9dc5c54c48d414e6d46d6a78cfa6 new file mode 100644 index 00000000000..02f5e7953da Binary files /dev/null and b/fuzzers/corpora/packfile/ab2c64588d3d9dc5c54c48d414e6d46d6a78cfa6 differ diff --git a/fuzzers/corpora/packfile/abe729b06750880778312618dcebb43257ec03e0 b/fuzzers/corpora/packfile/abe729b06750880778312618dcebb43257ec03e0 new file mode 100644 index 00000000000..fc1860b0f49 Binary files /dev/null and b/fuzzers/corpora/packfile/abe729b06750880778312618dcebb43257ec03e0 differ diff --git a/fuzzers/corpora/packfile/ac1bf5a5fe61e5784f72b364ef1bcddfb0d13716 b/fuzzers/corpora/packfile/ac1bf5a5fe61e5784f72b364ef1bcddfb0d13716 new file mode 100644 index 00000000000..5b9fb92c3f5 Binary files /dev/null and b/fuzzers/corpora/packfile/ac1bf5a5fe61e5784f72b364ef1bcddfb0d13716 differ diff --git a/fuzzers/corpora/packfile/ac47b6d3f0e479df3292131535f8e0d99c288de9 b/fuzzers/corpora/packfile/ac47b6d3f0e479df3292131535f8e0d99c288de9 new file mode 100644 index 00000000000..def06efe685 Binary files /dev/null and b/fuzzers/corpora/packfile/ac47b6d3f0e479df3292131535f8e0d99c288de9 differ diff --git a/fuzzers/corpora/packfile/ac9231da4082430afe8f4d40127814c613648d8e b/fuzzers/corpora/packfile/ac9231da4082430afe8f4d40127814c613648d8e new file mode 100644 index 00000000000..501a6bbaf1e --- /dev/null +++ b/fuzzers/corpora/packfile/ac9231da4082430afe8f4d40127814c613648d8e @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/ace9ffcaa273c01c0eb60381321e47edf4842332 b/fuzzers/corpora/packfile/ace9ffcaa273c01c0eb60381321e47edf4842332 new file mode 100644 index 00000000000..783b97f1a3d Binary files /dev/null and b/fuzzers/corpora/packfile/ace9ffcaa273c01c0eb60381321e47edf4842332 differ diff --git a/fuzzers/corpora/packfile/ad6ba9b0bc076987efbeb11ce3fc92bc1df69d0a b/fuzzers/corpora/packfile/ad6ba9b0bc076987efbeb11ce3fc92bc1df69d0a new file mode 100644 index 00000000000..d6e99fca80d Binary files /dev/null and b/fuzzers/corpora/packfile/ad6ba9b0bc076987efbeb11ce3fc92bc1df69d0a differ diff --git a/fuzzers/corpora/packfile/ae99dcb9b5e1b09aa5df6bb2fada3a3de61268fe b/fuzzers/corpora/packfile/ae99dcb9b5e1b09aa5df6bb2fada3a3de61268fe new file mode 100644 index 00000000000..09e47fd9b6e Binary files /dev/null and b/fuzzers/corpora/packfile/ae99dcb9b5e1b09aa5df6bb2fada3a3de61268fe differ diff --git a/fuzzers/corpora/packfile/aeeacf0499ace69549fe2c76757d4948ba65a10b b/fuzzers/corpora/packfile/aeeacf0499ace69549fe2c76757d4948ba65a10b new file mode 100644 index 00000000000..4832b6e5f5b Binary files /dev/null and b/fuzzers/corpora/packfile/aeeacf0499ace69549fe2c76757d4948ba65a10b differ diff --git a/fuzzers/corpora/packfile/af6614c37604ee5d3f7b00cddca761a8776283b5 b/fuzzers/corpora/packfile/af6614c37604ee5d3f7b00cddca761a8776283b5 new file mode 100644 index 00000000000..f41a79900c7 Binary files /dev/null and b/fuzzers/corpora/packfile/af6614c37604ee5d3f7b00cddca761a8776283b5 differ diff --git a/fuzzers/corpora/packfile/afd44f8c385a922c8caacc1ea5688d324bad5b39 b/fuzzers/corpora/packfile/afd44f8c385a922c8caacc1ea5688d324bad5b39 new file mode 100644 index 00000000000..eec211030fd Binary files /dev/null and b/fuzzers/corpora/packfile/afd44f8c385a922c8caacc1ea5688d324bad5b39 differ diff --git a/fuzzers/corpora/packfile/aff024fe4ab0fece4091de044c58c9ae4233383a b/fuzzers/corpora/packfile/aff024fe4ab0fece4091de044c58c9ae4233383a new file mode 100644 index 00000000000..6bf0c97a7f8 --- /dev/null +++ b/fuzzers/corpora/packfile/aff024fe4ab0fece4091de044c58c9ae4233383a @@ -0,0 +1 @@ +w \ No newline at end of file diff --git a/fuzzers/corpora/packfile/b1f86f05d4928c8393fe0f138c0714df3978f0bb b/fuzzers/corpora/packfile/b1f86f05d4928c8393fe0f138c0714df3978f0bb new file mode 100644 index 00000000000..a36092c1807 Binary files /dev/null and b/fuzzers/corpora/packfile/b1f86f05d4928c8393fe0f138c0714df3978f0bb differ diff --git a/fuzzers/corpora/packfile/b452cd4b70f2827e3cbd6d5dd20f678b6e55f813 b/fuzzers/corpora/packfile/b452cd4b70f2827e3cbd6d5dd20f678b6e55f813 new file mode 100644 index 00000000000..f763b0d07c1 Binary files /dev/null and b/fuzzers/corpora/packfile/b452cd4b70f2827e3cbd6d5dd20f678b6e55f813 differ diff --git a/fuzzers/corpora/packfile/b491dbad4c3edc87aa5a7f12b2c9a447a712c20d b/fuzzers/corpora/packfile/b491dbad4c3edc87aa5a7f12b2c9a447a712c20d new file mode 100644 index 00000000000..9e7d3e05006 Binary files /dev/null and b/fuzzers/corpora/packfile/b491dbad4c3edc87aa5a7f12b2c9a447a712c20d differ diff --git a/fuzzers/corpora/packfile/b54664965911c6fe91e18cd01b68a75c8183b530 b/fuzzers/corpora/packfile/b54664965911c6fe91e18cd01b68a75c8183b530 new file mode 100644 index 00000000000..39e8d660251 --- /dev/null +++ b/fuzzers/corpora/packfile/b54664965911c6fe91e18cd01b68a75c8183b530 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/b68542373c05c0ed25231d09955b2c699d37c45b b/fuzzers/corpora/packfile/b68542373c05c0ed25231d09955b2c699d37c45b new file mode 100644 index 00000000000..050ac90ecbd --- /dev/null +++ b/fuzzers/corpora/packfile/b68542373c05c0ed25231d09955b2c699d37c45b @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/b706e78cf7110a78dfccce991cd4ce22c6fd898a b/fuzzers/corpora/packfile/b706e78cf7110a78dfccce991cd4ce22c6fd898a new file mode 100644 index 00000000000..e905006d657 Binary files /dev/null and b/fuzzers/corpora/packfile/b706e78cf7110a78dfccce991cd4ce22c6fd898a differ diff --git a/fuzzers/corpora/packfile/b8d3910a75ad8a7058f9c3f202f8eb27419137d7 b/fuzzers/corpora/packfile/b8d3910a75ad8a7058f9c3f202f8eb27419137d7 new file mode 100644 index 00000000000..0a0adc4ea4f Binary files /dev/null and b/fuzzers/corpora/packfile/b8d3910a75ad8a7058f9c3f202f8eb27419137d7 differ diff --git a/fuzzers/corpora/packfile/b93abe6094fb4ebbfa7414fbceb7199ce766075b b/fuzzers/corpora/packfile/b93abe6094fb4ebbfa7414fbceb7199ce766075b new file mode 100644 index 00000000000..fb7b651069e Binary files /dev/null and b/fuzzers/corpora/packfile/b93abe6094fb4ebbfa7414fbceb7199ce766075b differ diff --git a/fuzzers/corpora/packfile/b9a64cc0694f3ac4a3c530c721bbf69026192187 b/fuzzers/corpora/packfile/b9a64cc0694f3ac4a3c530c721bbf69026192187 new file mode 100644 index 00000000000..c775b6fdb9b Binary files /dev/null and b/fuzzers/corpora/packfile/b9a64cc0694f3ac4a3c530c721bbf69026192187 differ diff --git a/fuzzers/corpora/packfile/b9e5319eca8fbc26e5c322e0b151ed8ed60628d1 b/fuzzers/corpora/packfile/b9e5319eca8fbc26e5c322e0b151ed8ed60628d1 new file mode 100644 index 00000000000..54e04316a48 Binary files /dev/null and b/fuzzers/corpora/packfile/b9e5319eca8fbc26e5c322e0b151ed8ed60628d1 differ diff --git a/fuzzers/corpora/packfile/ba390745a04c5394601f7aa73fe795097b814d1a b/fuzzers/corpora/packfile/ba390745a04c5394601f7aa73fe795097b814d1a new file mode 100644 index 00000000000..1738d2d27e6 Binary files /dev/null and b/fuzzers/corpora/packfile/ba390745a04c5394601f7aa73fe795097b814d1a differ diff --git a/fuzzers/corpora/packfile/bb7d065b776833337d3e1a3071de4d5d2759d78b b/fuzzers/corpora/packfile/bb7d065b776833337d3e1a3071de4d5d2759d78b new file mode 100644 index 00000000000..2882b1818ef --- /dev/null +++ b/fuzzers/corpora/packfile/bb7d065b776833337d3e1a3071de4d5d2759d78b @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/bb99cf0bb3e5d75d59300e6ca9cb1f67ce315e3a b/fuzzers/corpora/packfile/bb99cf0bb3e5d75d59300e6ca9cb1f67ce315e3a new file mode 100644 index 00000000000..7450e265474 Binary files /dev/null and b/fuzzers/corpora/packfile/bb99cf0bb3e5d75d59300e6ca9cb1f67ce315e3a differ diff --git a/fuzzers/corpora/packfile/bd9722d91e0615cbdae3cee3476ec6181fbad98d b/fuzzers/corpora/packfile/bd9722d91e0615cbdae3cee3476ec6181fbad98d new file mode 100644 index 00000000000..a8f95b69a5a Binary files /dev/null and b/fuzzers/corpora/packfile/bd9722d91e0615cbdae3cee3476ec6181fbad98d differ diff --git a/fuzzers/corpora/packfile/bf8b4530d8d246dd74ac53a13471bba17941dff7 b/fuzzers/corpora/packfile/bf8b4530d8d246dd74ac53a13471bba17941dff7 new file mode 100644 index 00000000000..6b2aaa76407 --- /dev/null +++ b/fuzzers/corpora/packfile/bf8b4530d8d246dd74ac53a13471bba17941dff7 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/bffc4698ad4aaddd977fe857da20858aa6654263 b/fuzzers/corpora/packfile/bffc4698ad4aaddd977fe857da20858aa6654263 new file mode 100644 index 00000000000..b7cde14f5d8 Binary files /dev/null and b/fuzzers/corpora/packfile/bffc4698ad4aaddd977fe857da20858aa6654263 differ diff --git a/fuzzers/corpora/packfile/c0ea828d8f9c4a2c0fc6253908cd283f6c7994a1 b/fuzzers/corpora/packfile/c0ea828d8f9c4a2c0fc6253908cd283f6c7994a1 new file mode 100644 index 00000000000..ab567632ed6 Binary files /dev/null and b/fuzzers/corpora/packfile/c0ea828d8f9c4a2c0fc6253908cd283f6c7994a1 differ diff --git a/fuzzers/corpora/packfile/c151b760696d665265187501c51f38cd84503634 b/fuzzers/corpora/packfile/c151b760696d665265187501c51f38cd84503634 new file mode 100644 index 00000000000..4be24608898 --- /dev/null +++ b/fuzzers/corpora/packfile/c151b760696d665265187501c51f38cd84503634 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/c16f90096603258174790bc85f076413dad0e228 b/fuzzers/corpora/packfile/c16f90096603258174790bc85f076413dad0e228 new file mode 100644 index 00000000000..d736f273cf7 Binary files /dev/null and b/fuzzers/corpora/packfile/c16f90096603258174790bc85f076413dad0e228 differ diff --git a/fuzzers/corpora/packfile/c20fc8fb8f1d44050c281089191b8eac2dc9444c b/fuzzers/corpora/packfile/c20fc8fb8f1d44050c281089191b8eac2dc9444c new file mode 100644 index 00000000000..221c4826ed2 Binary files /dev/null and b/fuzzers/corpora/packfile/c20fc8fb8f1d44050c281089191b8eac2dc9444c differ diff --git a/fuzzers/corpora/packfile/c2143b1a0db17957bec1b41bb2e5f75aa135981e b/fuzzers/corpora/packfile/c2143b1a0db17957bec1b41bb2e5f75aa135981e new file mode 100644 index 00000000000..52e60b4495a --- /dev/null +++ b/fuzzers/corpora/packfile/c2143b1a0db17957bec1b41bb2e5f75aa135981e @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/c22c3fba53bb2c5579b47852fa9ec54a88c03472 b/fuzzers/corpora/packfile/c22c3fba53bb2c5579b47852fa9ec54a88c03472 new file mode 100644 index 00000000000..45df7781a45 Binary files /dev/null and b/fuzzers/corpora/packfile/c22c3fba53bb2c5579b47852fa9ec54a88c03472 differ diff --git a/fuzzers/corpora/packfile/c297564cff1bb4f7933221050cfcffa36c59f691 b/fuzzers/corpora/packfile/c297564cff1bb4f7933221050cfcffa36c59f691 new file mode 100644 index 00000000000..751e6bb634e Binary files /dev/null and b/fuzzers/corpora/packfile/c297564cff1bb4f7933221050cfcffa36c59f691 differ diff --git a/fuzzers/corpora/packfile/c2c4da76233acd3efe08eaebb7ae8dc9b3036527 b/fuzzers/corpora/packfile/c2c4da76233acd3efe08eaebb7ae8dc9b3036527 new file mode 100644 index 00000000000..5533ee37853 Binary files /dev/null and b/fuzzers/corpora/packfile/c2c4da76233acd3efe08eaebb7ae8dc9b3036527 differ diff --git a/fuzzers/corpora/packfile/c3d47118536d19a8d1a601978510cc24344aa8df b/fuzzers/corpora/packfile/c3d47118536d19a8d1a601978510cc24344aa8df new file mode 100644 index 00000000000..dac425171d6 Binary files /dev/null and b/fuzzers/corpora/packfile/c3d47118536d19a8d1a601978510cc24344aa8df differ diff --git a/fuzzers/corpora/packfile/c4cbb032db94c57061003a85d30bdf4117979b1e b/fuzzers/corpora/packfile/c4cbb032db94c57061003a85d30bdf4117979b1e new file mode 100644 index 00000000000..e46252ee487 Binary files /dev/null and b/fuzzers/corpora/packfile/c4cbb032db94c57061003a85d30bdf4117979b1e differ diff --git a/fuzzers/corpora/packfile/c7090127a03c0e7230c11a649e4f98fcb4ca2b75 b/fuzzers/corpora/packfile/c7090127a03c0e7230c11a649e4f98fcb4ca2b75 new file mode 100644 index 00000000000..7009494e726 Binary files /dev/null and b/fuzzers/corpora/packfile/c7090127a03c0e7230c11a649e4f98fcb4ca2b75 differ diff --git a/fuzzers/corpora/packfile/c8b839665bd381ff7d006b1b08c35f94f1818556 b/fuzzers/corpora/packfile/c8b839665bd381ff7d006b1b08c35f94f1818556 new file mode 100644 index 00000000000..234b26a69ea Binary files /dev/null and b/fuzzers/corpora/packfile/c8b839665bd381ff7d006b1b08c35f94f1818556 differ diff --git a/fuzzers/corpora/packfile/ca8c7c16d1d6b60e951dcfb558cc97e14231c750 b/fuzzers/corpora/packfile/ca8c7c16d1d6b60e951dcfb558cc97e14231c750 new file mode 100644 index 00000000000..e0b7aecf42f Binary files /dev/null and b/fuzzers/corpora/packfile/ca8c7c16d1d6b60e951dcfb558cc97e14231c750 differ diff --git a/fuzzers/corpora/packfile/cb46c744c83541a0900e1e61780c18d43031a08b b/fuzzers/corpora/packfile/cb46c744c83541a0900e1e61780c18d43031a08b new file mode 100644 index 00000000000..825026bf2f9 --- /dev/null +++ b/fuzzers/corpora/packfile/cb46c744c83541a0900e1e61780c18d43031a08b @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/cbf1e454dc7ac878bd23e3dfd0b6a28a50b2155d b/fuzzers/corpora/packfile/cbf1e454dc7ac878bd23e3dfd0b6a28a50b2155d new file mode 100644 index 00000000000..7e60ba4e83c Binary files /dev/null and b/fuzzers/corpora/packfile/cbf1e454dc7ac878bd23e3dfd0b6a28a50b2155d differ diff --git a/fuzzers/corpora/packfile/cbfb8cae82ddd82c04996f474fdb4f1b80dcb6db b/fuzzers/corpora/packfile/cbfb8cae82ddd82c04996f474fdb4f1b80dcb6db new file mode 100644 index 00000000000..7bd0640dc55 Binary files /dev/null and b/fuzzers/corpora/packfile/cbfb8cae82ddd82c04996f474fdb4f1b80dcb6db differ diff --git a/fuzzers/corpora/packfile/cf74f755c004ca634818f8ba44c99fffbaa950a1 b/fuzzers/corpora/packfile/cf74f755c004ca634818f8ba44c99fffbaa950a1 new file mode 100644 index 00000000000..6c8a3e28fa3 Binary files /dev/null and b/fuzzers/corpora/packfile/cf74f755c004ca634818f8ba44c99fffbaa950a1 differ diff --git a/fuzzers/corpora/packfile/d07e4bc786c88b8d2304f84c7db2098666f822c0 b/fuzzers/corpora/packfile/d07e4bc786c88b8d2304f84c7db2098666f822c0 new file mode 100644 index 00000000000..5639b6ddcf6 --- /dev/null +++ b/fuzzers/corpora/packfile/d07e4bc786c88b8d2304f84c7db2098666f822c0 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/d17ab0db9edea68e8f9f51f471decae84b192a1a b/fuzzers/corpora/packfile/d17ab0db9edea68e8f9f51f471decae84b192a1a new file mode 100644 index 00000000000..e3ec67ed438 Binary files /dev/null and b/fuzzers/corpora/packfile/d17ab0db9edea68e8f9f51f471decae84b192a1a differ diff --git a/fuzzers/corpora/packfile/d1854cae891ec7b29161ccaf79a24b00c274bdaa b/fuzzers/corpora/packfile/d1854cae891ec7b29161ccaf79a24b00c274bdaa new file mode 100644 index 00000000000..ef073cc45cc --- /dev/null +++ b/fuzzers/corpora/packfile/d1854cae891ec7b29161ccaf79a24b00c274bdaa @@ -0,0 +1 @@ +n \ No newline at end of file diff --git a/fuzzers/corpora/packfile/d22aac18f8b435fc34566fe0d3f42464aec9458c b/fuzzers/corpora/packfile/d22aac18f8b435fc34566fe0d3f42464aec9458c new file mode 100644 index 00000000000..766a326783a Binary files /dev/null and b/fuzzers/corpora/packfile/d22aac18f8b435fc34566fe0d3f42464aec9458c differ diff --git a/fuzzers/corpora/packfile/d446a50788c32053358495358696f9d595358bcf b/fuzzers/corpora/packfile/d446a50788c32053358495358696f9d595358bcf new file mode 100644 index 00000000000..1015f8ede84 Binary files /dev/null and b/fuzzers/corpora/packfile/d446a50788c32053358495358696f9d595358bcf differ diff --git a/fuzzers/corpora/packfile/d461cbcff85c87b0068f0e9c15d2056197cdfa52 b/fuzzers/corpora/packfile/d461cbcff85c87b0068f0e9c15d2056197cdfa52 new file mode 100644 index 00000000000..e475e616d02 Binary files /dev/null and b/fuzzers/corpora/packfile/d461cbcff85c87b0068f0e9c15d2056197cdfa52 differ diff --git a/fuzzers/corpora/packfile/d54709a1b46002c81f57da533379e57f00afe942 b/fuzzers/corpora/packfile/d54709a1b46002c81f57da533379e57f00afe942 new file mode 100644 index 00000000000..010a294b257 Binary files /dev/null and b/fuzzers/corpora/packfile/d54709a1b46002c81f57da533379e57f00afe942 differ diff --git a/fuzzers/corpora/packfile/d567007f84b83e82df7069838bf8b6c5826b18a8 b/fuzzers/corpora/packfile/d567007f84b83e82df7069838bf8b6c5826b18a8 new file mode 100644 index 00000000000..feb21a2c2a4 Binary files /dev/null and b/fuzzers/corpora/packfile/d567007f84b83e82df7069838bf8b6c5826b18a8 differ diff --git a/fuzzers/corpora/packfile/d5e60b9f94126a9ec865fda83feb6835d294b76b b/fuzzers/corpora/packfile/d5e60b9f94126a9ec865fda83feb6835d294b76b new file mode 100644 index 00000000000..63dd3950439 Binary files /dev/null and b/fuzzers/corpora/packfile/d5e60b9f94126a9ec865fda83feb6835d294b76b differ diff --git a/fuzzers/corpora/packfile/d81092a4f3607ddbba85862facf2285459696078 b/fuzzers/corpora/packfile/d81092a4f3607ddbba85862facf2285459696078 new file mode 100644 index 00000000000..0074e177e33 Binary files /dev/null and b/fuzzers/corpora/packfile/d81092a4f3607ddbba85862facf2285459696078 differ diff --git a/fuzzers/corpora/packfile/d898eb860ceac044950605db88429e029ea278ef b/fuzzers/corpora/packfile/d898eb860ceac044950605db88429e029ea278ef new file mode 100644 index 00000000000..a74a67e5627 Binary files /dev/null and b/fuzzers/corpora/packfile/d898eb860ceac044950605db88429e029ea278ef differ diff --git a/fuzzers/corpora/packfile/d8fc60ccdd8f555c1858b9f0820f263e3d2b58ec b/fuzzers/corpora/packfile/d8fc60ccdd8f555c1858b9f0820f263e3d2b58ec new file mode 100644 index 00000000000..9d3cd688987 --- /dev/null +++ b/fuzzers/corpora/packfile/d8fc60ccdd8f555c1858b9f0820f263e3d2b58ec @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/d9b69c63cdc0c1622f2fab84d1307f9e0c0fa3b9 b/fuzzers/corpora/packfile/d9b69c63cdc0c1622f2fab84d1307f9e0c0fa3b9 new file mode 100644 index 00000000000..1f4966d57cb Binary files /dev/null and b/fuzzers/corpora/packfile/d9b69c63cdc0c1622f2fab84d1307f9e0c0fa3b9 differ diff --git a/fuzzers/corpora/packfile/db1bb4b7348d387623dcaf0a743d0b11fa18409f b/fuzzers/corpora/packfile/db1bb4b7348d387623dcaf0a743d0b11fa18409f new file mode 100644 index 00000000000..bed137b82e9 Binary files /dev/null and b/fuzzers/corpora/packfile/db1bb4b7348d387623dcaf0a743d0b11fa18409f differ diff --git a/fuzzers/corpora/packfile/db291360a195c79ae504a3dfb2cd0f71cbc11902 b/fuzzers/corpora/packfile/db291360a195c79ae504a3dfb2cd0f71cbc11902 new file mode 100644 index 00000000000..aaebf73f894 Binary files /dev/null and b/fuzzers/corpora/packfile/db291360a195c79ae504a3dfb2cd0f71cbc11902 differ diff --git a/fuzzers/corpora/packfile/dc98359b3ef2ced9c3d07636c89d475a564c39d9 b/fuzzers/corpora/packfile/dc98359b3ef2ced9c3d07636c89d475a564c39d9 new file mode 100644 index 00000000000..44b5bbe70c6 Binary files /dev/null and b/fuzzers/corpora/packfile/dc98359b3ef2ced9c3d07636c89d475a564c39d9 differ diff --git a/fuzzers/corpora/packfile/dccc5642917b20b4dd64d3e44b71d08da30445e9 b/fuzzers/corpora/packfile/dccc5642917b20b4dd64d3e44b71d08da30445e9 new file mode 100644 index 00000000000..d6430c257c9 Binary files /dev/null and b/fuzzers/corpora/packfile/dccc5642917b20b4dd64d3e44b71d08da30445e9 differ diff --git a/fuzzers/corpora/packfile/dd79c8cfb8beeacd0460429944b4ecbe95a31561 b/fuzzers/corpora/packfile/dd79c8cfb8beeacd0460429944b4ecbe95a31561 new file mode 100644 index 00000000000..8a908eccd27 --- /dev/null +++ b/fuzzers/corpora/packfile/dd79c8cfb8beeacd0460429944b4ecbe95a31561 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/de9550264c4e2dbef14e1281ff3693f2d19dc1c9 b/fuzzers/corpora/packfile/de9550264c4e2dbef14e1281ff3693f2d19dc1c9 new file mode 100644 index 00000000000..2c096ffd2d3 Binary files /dev/null and b/fuzzers/corpora/packfile/de9550264c4e2dbef14e1281ff3693f2d19dc1c9 differ diff --git a/fuzzers/corpora/packfile/df8b4d163e9ed75634eb56467343bde73b13263e b/fuzzers/corpora/packfile/df8b4d163e9ed75634eb56467343bde73b13263e new file mode 100644 index 00000000000..5c3529e1290 Binary files /dev/null and b/fuzzers/corpora/packfile/df8b4d163e9ed75634eb56467343bde73b13263e differ diff --git a/fuzzers/corpora/packfile/e0184adedf913b076626646d3f52c3b49c39ad6d b/fuzzers/corpora/packfile/e0184adedf913b076626646d3f52c3b49c39ad6d new file mode 100644 index 00000000000..9fb75b8d4f4 --- /dev/null +++ b/fuzzers/corpora/packfile/e0184adedf913b076626646d3f52c3b49c39ad6d @@ -0,0 +1 @@ +E \ No newline at end of file diff --git a/fuzzers/corpora/packfile/e0905bac594c818b9cfa909269114977c4d6d1b2 b/fuzzers/corpora/packfile/e0905bac594c818b9cfa909269114977c4d6d1b2 new file mode 100644 index 00000000000..40f2025d33d Binary files /dev/null and b/fuzzers/corpora/packfile/e0905bac594c818b9cfa909269114977c4d6d1b2 differ diff --git a/fuzzers/corpora/packfile/e0bcb16cd6b42128201e1b6454323175a7e412f0 b/fuzzers/corpora/packfile/e0bcb16cd6b42128201e1b6454323175a7e412f0 new file mode 100644 index 00000000000..7386ac1f854 Binary files /dev/null and b/fuzzers/corpora/packfile/e0bcb16cd6b42128201e1b6454323175a7e412f0 differ diff --git a/fuzzers/corpora/packfile/e1ac9563c33f4f31b3e147b9d2fef80fca550948 b/fuzzers/corpora/packfile/e1ac9563c33f4f31b3e147b9d2fef80fca550948 new file mode 100644 index 00000000000..c87f08b9bc9 Binary files /dev/null and b/fuzzers/corpora/packfile/e1ac9563c33f4f31b3e147b9d2fef80fca550948 differ diff --git a/fuzzers/corpora/packfile/e230c91352f1b07f6f34da803d07e75c06897b30 b/fuzzers/corpora/packfile/e230c91352f1b07f6f34da803d07e75c06897b30 new file mode 100644 index 00000000000..ff1376fe80f Binary files /dev/null and b/fuzzers/corpora/packfile/e230c91352f1b07f6f34da803d07e75c06897b30 differ diff --git a/fuzzers/corpora/packfile/e26b3bacbfd6603d021d4ddadbac94b7b7aa0034 b/fuzzers/corpora/packfile/e26b3bacbfd6603d021d4ddadbac94b7b7aa0034 new file mode 100644 index 00000000000..e6bdb3a7969 --- /dev/null +++ b/fuzzers/corpora/packfile/e26b3bacbfd6603d021d4ddadbac94b7b7aa0034 @@ -0,0 +1 @@ +! \ No newline at end of file diff --git a/fuzzers/corpora/packfile/e2a6f8dc3dc5d6c859f19d6e0fa64745201df0a6 b/fuzzers/corpora/packfile/e2a6f8dc3dc5d6c859f19d6e0fa64745201df0a6 new file mode 100644 index 00000000000..3afd5ca8967 Binary files /dev/null and b/fuzzers/corpora/packfile/e2a6f8dc3dc5d6c859f19d6e0fa64745201df0a6 differ diff --git a/fuzzers/corpora/packfile/e2ba004118345660b379df5147bfa7a39d884dbc b/fuzzers/corpora/packfile/e2ba004118345660b379df5147bfa7a39d884dbc new file mode 100644 index 00000000000..cb1ff9443c0 Binary files /dev/null and b/fuzzers/corpora/packfile/e2ba004118345660b379df5147bfa7a39d884dbc differ diff --git a/fuzzers/corpora/packfile/e45aaf139d726366a18dce9e4854ee6c82901677 b/fuzzers/corpora/packfile/e45aaf139d726366a18dce9e4854ee6c82901677 new file mode 100644 index 00000000000..0ba1db8d6a1 Binary files /dev/null and b/fuzzers/corpora/packfile/e45aaf139d726366a18dce9e4854ee6c82901677 differ diff --git a/fuzzers/corpora/packfile/e4b3ab7e8c18de815fc8bd6ebfd5d52cf1924a8e b/fuzzers/corpora/packfile/e4b3ab7e8c18de815fc8bd6ebfd5d52cf1924a8e new file mode 100644 index 00000000000..786d94bbbd8 Binary files /dev/null and b/fuzzers/corpora/packfile/e4b3ab7e8c18de815fc8bd6ebfd5d52cf1924a8e differ diff --git a/fuzzers/corpora/packfile/e6818b96c50bb749911248959af81a9c412a0223 b/fuzzers/corpora/packfile/e6818b96c50bb749911248959af81a9c412a0223 new file mode 100644 index 00000000000..b8ac98e0839 Binary files /dev/null and b/fuzzers/corpora/packfile/e6818b96c50bb749911248959af81a9c412a0223 differ diff --git a/fuzzers/corpora/packfile/e69f20e9f683920d3fb4329abd951e878b1f9372 b/fuzzers/corpora/packfile/e69f20e9f683920d3fb4329abd951e878b1f9372 new file mode 100644 index 00000000000..c137216fe16 --- /dev/null +++ b/fuzzers/corpora/packfile/e69f20e9f683920d3fb4329abd951e878b1f9372 @@ -0,0 +1 @@ +F \ No newline at end of file diff --git a/fuzzers/corpora/packfile/e6eb439fef7d5461bc3552aa7c064d24e44c5f32 b/fuzzers/corpora/packfile/e6eb439fef7d5461bc3552aa7c064d24e44c5f32 new file mode 100644 index 00000000000..9ab1a4ddb27 Binary files /dev/null and b/fuzzers/corpora/packfile/e6eb439fef7d5461bc3552aa7c064d24e44c5f32 differ diff --git a/fuzzers/corpora/packfile/e9d9930dc3fea44fbc7acb0d1ef4bd867f1c902b b/fuzzers/corpora/packfile/e9d9930dc3fea44fbc7acb0d1ef4bd867f1c902b new file mode 100644 index 00000000000..bd8ffbe202a Binary files /dev/null and b/fuzzers/corpora/packfile/e9d9930dc3fea44fbc7acb0d1ef4bd867f1c902b differ diff --git a/fuzzers/corpora/packfile/eb05b6ad73fb1f69ef750d0b9cb6c606ab9d949f b/fuzzers/corpora/packfile/eb05b6ad73fb1f69ef750d0b9cb6c606ab9d949f new file mode 100644 index 00000000000..aaba356e48e Binary files /dev/null and b/fuzzers/corpora/packfile/eb05b6ad73fb1f69ef750d0b9cb6c606ab9d949f differ diff --git a/fuzzers/corpora/packfile/eb0814ae767e5f28b87c998b0f44dcf80814db1b b/fuzzers/corpora/packfile/eb0814ae767e5f28b87c998b0f44dcf80814db1b new file mode 100644 index 00000000000..f834b0bf0cb Binary files /dev/null and b/fuzzers/corpora/packfile/eb0814ae767e5f28b87c998b0f44dcf80814db1b differ diff --git a/fuzzers/corpora/packfile/ebbd9763912dd557d08abd1373c867a4b56e6a41 b/fuzzers/corpora/packfile/ebbd9763912dd557d08abd1373c867a4b56e6a41 new file mode 100644 index 00000000000..9a5a0eda7fc Binary files /dev/null and b/fuzzers/corpora/packfile/ebbd9763912dd557d08abd1373c867a4b56e6a41 differ diff --git a/fuzzers/corpora/packfile/ebcdcb7effcc3f06e0d503638ac621de877fc554 b/fuzzers/corpora/packfile/ebcdcb7effcc3f06e0d503638ac621de877fc554 new file mode 100644 index 00000000000..ef608090670 --- /dev/null +++ b/fuzzers/corpora/packfile/ebcdcb7effcc3f06e0d503638ac621de877fc554 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/packfile/eddccafb2716adafb9ad48203f0621bb00ebc73f b/fuzzers/corpora/packfile/eddccafb2716adafb9ad48203f0621bb00ebc73f new file mode 100644 index 00000000000..9b1c9fbb332 Binary files /dev/null and b/fuzzers/corpora/packfile/eddccafb2716adafb9ad48203f0621bb00ebc73f differ diff --git a/fuzzers/corpora/packfile/edfbf20c83d3cec45470105581f7dc8b7e0889da b/fuzzers/corpora/packfile/edfbf20c83d3cec45470105581f7dc8b7e0889da new file mode 100644 index 00000000000..bf9c3901dac Binary files /dev/null and b/fuzzers/corpora/packfile/edfbf20c83d3cec45470105581f7dc8b7e0889da differ diff --git a/fuzzers/corpora/packfile/f03218467b1c74e465cebb3b8092e21a5122f31d b/fuzzers/corpora/packfile/f03218467b1c74e465cebb3b8092e21a5122f31d new file mode 100644 index 00000000000..d7539e4c98a Binary files /dev/null and b/fuzzers/corpora/packfile/f03218467b1c74e465cebb3b8092e21a5122f31d differ diff --git a/fuzzers/corpora/packfile/f28600befd899a94bed8e62853e90655d614f439 b/fuzzers/corpora/packfile/f28600befd899a94bed8e62853e90655d614f439 new file mode 100644 index 00000000000..5087f32e818 Binary files /dev/null and b/fuzzers/corpora/packfile/f28600befd899a94bed8e62853e90655d614f439 differ diff --git a/fuzzers/corpora/packfile/f3b15185b7a9a10716752d58434fe656d839092e b/fuzzers/corpora/packfile/f3b15185b7a9a10716752d58434fe656d839092e new file mode 100644 index 00000000000..857524445a0 Binary files /dev/null and b/fuzzers/corpora/packfile/f3b15185b7a9a10716752d58434fe656d839092e differ diff --git a/fuzzers/corpora/packfile/f436ed7933482610e08e18b40e9eec102b63b7d4 b/fuzzers/corpora/packfile/f436ed7933482610e08e18b40e9eec102b63b7d4 new file mode 100644 index 00000000000..4a7dcffb459 Binary files /dev/null and b/fuzzers/corpora/packfile/f436ed7933482610e08e18b40e9eec102b63b7d4 differ diff --git a/fuzzers/corpora/packfile/f55ea5b7c1cf5400aae56d7faf65a42320d0323a b/fuzzers/corpora/packfile/f55ea5b7c1cf5400aae56d7faf65a42320d0323a new file mode 100644 index 00000000000..c5e31373000 Binary files /dev/null and b/fuzzers/corpora/packfile/f55ea5b7c1cf5400aae56d7faf65a42320d0323a differ diff --git a/fuzzers/corpora/packfile/f5eeab2d009aa4984378df6bfdd89366b7ecbb32 b/fuzzers/corpora/packfile/f5eeab2d009aa4984378df6bfdd89366b7ecbb32 new file mode 100644 index 00000000000..15828cfa6d1 Binary files /dev/null and b/fuzzers/corpora/packfile/f5eeab2d009aa4984378df6bfdd89366b7ecbb32 differ diff --git a/fuzzers/corpora/packfile/f6250c8b3cc0510e0f8f621100be83f018e2d234 b/fuzzers/corpora/packfile/f6250c8b3cc0510e0f8f621100be83f018e2d234 new file mode 100644 index 00000000000..ab03a5e31e3 Binary files /dev/null and b/fuzzers/corpora/packfile/f6250c8b3cc0510e0f8f621100be83f018e2d234 differ diff --git a/fuzzers/corpora/packfile/f7168410c7158ff7331698930937f9cdc54f4d8a b/fuzzers/corpora/packfile/f7168410c7158ff7331698930937f9cdc54f4d8a new file mode 100644 index 00000000000..e42053d0c90 Binary files /dev/null and b/fuzzers/corpora/packfile/f7168410c7158ff7331698930937f9cdc54f4d8a differ diff --git a/fuzzers/corpora/packfile/f91847640af285c1b8a6df27f5c50686ed0deb70 b/fuzzers/corpora/packfile/f91847640af285c1b8a6df27f5c50686ed0deb70 new file mode 100644 index 00000000000..615255fd64b Binary files /dev/null and b/fuzzers/corpora/packfile/f91847640af285c1b8a6df27f5c50686ed0deb70 differ diff --git a/fuzzers/corpora/packfile/fa58a6b2d3286a136a43afeeaac589d2dde0a2a6 b/fuzzers/corpora/packfile/fa58a6b2d3286a136a43afeeaac589d2dde0a2a6 new file mode 100644 index 00000000000..860053ee9cb Binary files /dev/null and b/fuzzers/corpora/packfile/fa58a6b2d3286a136a43afeeaac589d2dde0a2a6 differ diff --git a/fuzzers/corpora/packfile/fe3667be5fd2ffdd553ae04a534a2e9ce5445188 b/fuzzers/corpora/packfile/fe3667be5fd2ffdd553ae04a534a2e9ce5445188 new file mode 100644 index 00000000000..d92e5748ed2 Binary files /dev/null and b/fuzzers/corpora/packfile/fe3667be5fd2ffdd553ae04a534a2e9ce5445188 differ diff --git a/fuzzers/corpora/packfile/ff21cad92ddd105224408fa696e91080a8cf98fb b/fuzzers/corpora/packfile/ff21cad92ddd105224408fa696e91080a8cf98fb new file mode 100644 index 00000000000..c1cfc00c2c9 Binary files /dev/null and b/fuzzers/corpora/packfile/ff21cad92ddd105224408fa696e91080a8cf98fb differ diff --git a/fuzzers/corpora/packfile/ff9804ac04790bd58cdd124526c00b920469b812 b/fuzzers/corpora/packfile/ff9804ac04790bd58cdd124526c00b920469b812 new file mode 100644 index 00000000000..4280c829cac Binary files /dev/null and b/fuzzers/corpora/packfile/ff9804ac04790bd58cdd124526c00b920469b812 differ diff --git a/fuzzers/corpora/packfile/ffc54ca808e7666f250133ad0ae2185ad688a826 b/fuzzers/corpora/packfile/ffc54ca808e7666f250133ad0ae2185ad688a826 new file mode 100644 index 00000000000..4489a650035 --- /dev/null +++ b/fuzzers/corpora/packfile/ffc54ca808e7666f250133ad0ae2185ad688a826 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fuzzers/corpora/patch_parse/edit-file.diff b/fuzzers/corpora/patch_parse/edit-file.diff new file mode 100644 index 00000000000..d9e783a7f0b --- /dev/null +++ b/fuzzers/corpora/patch_parse/edit-file.diff @@ -0,0 +1,13 @@ +diff --git a/fuzzers/patch_fuzzer.c b/fuzzers/patch_fuzzer.c +index 76186b6fb..f7ce73ac8 100644 +--- a/fuzzers/patch_fuzzer.c ++++ b/fuzzers/patch_fuzzer.c +@@ -32,7 +32,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) + git_patch* patch; + git_patch_options opts = {(uint32_t)data[0]}; + int status = git_patch_from_buffer(&patch, (const char*)data+1, size-1, &opts); +- if (status == 0 && patch) { ++ if (patch) { + git_patch_free(patch); + } + return 0; diff --git a/fuzzers/corpora/patch_parse/patch_fuzzer-patch.diff b/fuzzers/corpora/patch_parse/patch_fuzzer-patch.diff new file mode 100644 index 00000000000..7c98d8ad493 --- /dev/null +++ b/fuzzers/corpora/patch_parse/patch_fuzzer-patch.diff @@ -0,0 +1,45 @@ +diff --git a/fuzzers/patch_fuzzer.c b/fuzzers/patch_fuzzer.c +new file mode 100644 +index 000000000..76186b6fb +--- /dev/null ++++ b/fuzzers/patch_fuzzer.c +@@ -0,0 +1,39 @@ ++/* ++ * libgit2 patch fuzzer target. ++ * ++ * Copyright (C) the libgit2 contributors. All rights reserved. ++ * ++ * This file is part of libgit2, distributed under the GNU GPL v2 with ++ * a Linking Exception. For full terms see the included COPYING file. ++ */ ++ ++#include "git2.h" ++#include "patch.h" ++#include "patch_parse.h" ++ ++#define UNUSED(x) (void)(x) ++ ++int LLVMFuzzerInitialize(int *argc, char ***argv) ++{ ++ UNUSED(argc); ++ UNUSED(argv); ++ ++ if (git_libgit2_init() < 0) ++ abort(); ++ ++ return 0; ++} ++ ++int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) ++{ ++ if (size < 1) { ++ return 0; ++ } ++ git_patch* patch; ++ git_patch_options opts = {(uint32_t)data[0]}; ++ int status = git_patch_from_buffer(&patch, (const char*)data+1, size-1, &opts); ++ if (status == 0 && patch) { ++ git_patch_free(patch); ++ } ++ return 0; ++} diff --git a/fuzzers/corpora/revparse/head b/fuzzers/corpora/revparse/head new file mode 100644 index 00000000000..e5517e4c5b4 --- /dev/null +++ b/fuzzers/corpora/revparse/head @@ -0,0 +1 @@ +HEAD \ No newline at end of file diff --git a/fuzzers/corpora/revparse/revat b/fuzzers/corpora/revparse/revat new file mode 100644 index 00000000000..382ffc0ba32 --- /dev/null +++ b/fuzzers/corpora/revparse/revat @@ -0,0 +1 @@ +xxxxxxxxxxxxxxxx@ \ No newline at end of file diff --git a/fuzzers/download_refs_fuzzer.c b/fuzzers/download_refs_fuzzer.c new file mode 100644 index 00000000000..c2b80ccb1f2 --- /dev/null +++ b/fuzzers/download_refs_fuzzer.c @@ -0,0 +1,198 @@ +/* + * libgit2 raw packfile fuzz target. + * + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include +#include + +#include "git2.h" +#include "git2/sys/transport.h" +#include "futils.h" + +#include "standalone_driver.h" +#include "fuzzer_utils.h" + +#define UNUSED(x) (void)(x) + +struct fuzzer_buffer { + const unsigned char *data; + size_t size; +}; + +struct fuzzer_stream { + git_smart_subtransport_stream base; + const unsigned char *readp; + const unsigned char *endp; +}; + +struct fuzzer_subtransport { + git_smart_subtransport base; + git_transport *owner; + struct fuzzer_buffer data; +}; + +static git_repository *repo; + +static int fuzzer_stream_read(git_smart_subtransport_stream *stream, + char *buffer, + size_t buf_size, + size_t *bytes_read) +{ + struct fuzzer_stream *fs = (struct fuzzer_stream *) stream; + size_t avail = fs->endp - fs->readp; + + *bytes_read = (buf_size > avail) ? avail : buf_size; + memcpy(buffer, fs->readp, *bytes_read); + fs->readp += *bytes_read; + + return 0; +} + +static int fuzzer_stream_write(git_smart_subtransport_stream *stream, + const char *buffer, size_t len) +{ + UNUSED(stream); + UNUSED(buffer); + UNUSED(len); + return 0; +} + +static void fuzzer_stream_free(git_smart_subtransport_stream *stream) +{ + free(stream); +} + +static int fuzzer_stream_new( + struct fuzzer_stream **out, + const struct fuzzer_buffer *data) +{ + struct fuzzer_stream *stream = malloc(sizeof(*stream)); + if (!stream) + return -1; + + stream->readp = data->data; + stream->endp = data->data + data->size; + stream->base.read = fuzzer_stream_read; + stream->base.write = fuzzer_stream_write; + stream->base.free = fuzzer_stream_free; + + *out = stream; + + return 0; +} + +static int fuzzer_subtransport_action( + git_smart_subtransport_stream **out, + git_smart_subtransport *transport, + const char *url, + git_smart_service_t action) +{ + struct fuzzer_subtransport *ft = (struct fuzzer_subtransport *) transport; + + UNUSED(url); + UNUSED(action); + + return fuzzer_stream_new((struct fuzzer_stream **) out, &ft->data); +} + +static int fuzzer_subtransport_close(git_smart_subtransport *transport) +{ + UNUSED(transport); + return 0; +} + +static void fuzzer_subtransport_free(git_smart_subtransport *transport) +{ + free(transport); +} + +static int fuzzer_subtransport_new( + struct fuzzer_subtransport **out, + git_transport *owner, + const struct fuzzer_buffer *data) +{ + struct fuzzer_subtransport *sub = malloc(sizeof(*sub)); + if (!sub) + return -1; + + sub->owner = owner; + sub->data.data = data->data; + sub->data.size = data->size; + sub->base.action = fuzzer_subtransport_action; + sub->base.close = fuzzer_subtransport_close; + sub->base.free = fuzzer_subtransport_free; + + *out = sub; + + return 0; +} + +static int fuzzer_subtransport_cb( + git_smart_subtransport **out, + git_transport *owner, + void *payload) +{ + struct fuzzer_buffer *buf = (struct fuzzer_buffer *) payload; + struct fuzzer_subtransport *sub; + + if (fuzzer_subtransport_new(&sub, owner, buf) < 0) + return -1; + + *out = &sub->base; + return 0; +} + +static int fuzzer_transport_cb(git_transport **out, git_remote *owner, void *param) +{ + git_smart_subtransport_definition def = { + fuzzer_subtransport_cb, + 1, + param + }; + return git_transport_smart(out, owner, &def); +} + +int LLVMFuzzerInitialize(int *argc, char ***argv) +{ + UNUSED(argc); + UNUSED(argv); + + if (git_libgit2_init() < 0) + abort(); + + if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS, 10000000) < 0) + abort(); + + repo = fuzzer_repo_init(); + return 0; +} + +int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size) +{ + struct fuzzer_buffer buffer = { data, size }; + git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT; + git_remote *remote; + + if (git_remote_create_anonymous(&remote, repo, "fuzzer://remote-url") < 0) + fuzzer_git_abort("git_remote_create"); + + callbacks.transport = fuzzer_transport_cb; + callbacks.payload = &buffer; + + if (git_remote_connect(remote, GIT_DIRECTION_FETCH, + &callbacks, NULL, NULL) < 0) + goto out; + + git_remote_download(remote, NULL, NULL); + + out: + git_remote_free(remote); + + return 0; +} diff --git a/fuzzers/fuzzer_utils.c b/fuzzers/fuzzer_utils.c new file mode 100644 index 00000000000..cde5065da03 --- /dev/null +++ b/fuzzers/fuzzer_utils.c @@ -0,0 +1,51 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include +#include + +#include "git2.h" +#include "futils.h" + +#include "fuzzer_utils.h" + +void fuzzer_git_abort(const char *op) +{ + const git_error *err = git_error_last(); + fprintf(stderr, "unexpected libgit error: %s: %s\n", + op, err ? err->message : ""); + abort(); +} + +git_repository *fuzzer_repo_init(void) +{ + git_repository *repo; + +#if defined(_WIN32) + char tmpdir[MAX_PATH], path[MAX_PATH]; + + if (GetTempPath((DWORD)sizeof(tmpdir), tmpdir) == 0) + abort(); + + if (GetTempFileName(tmpdir, "lg2", 1, path) == 0) + abort(); + + if (git_futils_mkdir(path, 0700, 0) < 0) + abort(); +#else + char path[] = "/tmp/git2.XXXXXX"; + + if (mkdtemp(path) != path) + abort(); +#endif + + if (git_repository_init(&repo, path, 1) < 0) + fuzzer_git_abort("git_repository_init"); + + return repo; +} diff --git a/fuzzers/fuzzer_utils.h b/fuzzers/fuzzer_utils.h new file mode 100644 index 00000000000..6b67c9a611d --- /dev/null +++ b/fuzzers/fuzzer_utils.h @@ -0,0 +1,14 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_fuzzer_utils_h__ +#define INCLUDE_fuzzer_utils_h__ + +extern void fuzzer_git_abort(const char *op); +extern git_repository *fuzzer_repo_init(void); + +#endif diff --git a/fuzzers/midx_fuzzer.c b/fuzzers/midx_fuzzer.c new file mode 100644 index 00000000000..21fb90361ee --- /dev/null +++ b/fuzzers/midx_fuzzer.c @@ -0,0 +1,80 @@ +/* + * libgit2 multi-pack-index fuzzer target. + * + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include + +#include "git2.h" + +#include "common.h" +#include "futils.h" +#include "hash.h" +#include "midx.h" + +#include "standalone_driver.h" + +int LLVMFuzzerInitialize(int *argc, char ***argv) +{ + GIT_UNUSED(argc); + GIT_UNUSED(argv); + + if (git_libgit2_init() < 0) { + fprintf(stderr, "Failed to initialize libgit2\n"); + abort(); + } + return 0; +} + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + git_midx_file idx = {{0}}; + git_midx_entry e; + git_str midx_buf = GIT_STR_INIT; + unsigned char hash[GIT_HASH_SHA1_SIZE]; + git_oid oid = GIT_OID_NONE; + bool append_hash = false; + + if (size < 4) + return 0; + + /* + * If the first byte in the stream has the high bit set, append the + * SHA1 hash so that the packfile is somewhat valid. + */ + append_hash = *data & 0x80; + /* Keep a 4-byte alignment to avoid unaligned accesses. */ + data += 4; + size -= 4; + + if (append_hash) { + if (git_str_init(&midx_buf, size + GIT_HASH_SHA1_SIZE) < 0) + goto cleanup; + if (git_hash_buf(hash, data, size, GIT_HASH_ALGORITHM_SHA1) < 0) { + fprintf(stderr, "Failed to compute the SHA1 hash\n"); + abort(); + } + memcpy(midx_buf.ptr, data, size); + memcpy(midx_buf.ptr + size, hash, GIT_HASH_SHA1_SIZE); + + memcpy(oid.id, hash, GIT_OID_SHA1_SIZE); + } else { + git_str_attach_notowned(&midx_buf, (char *)data, size); + } + + if (git_midx_parse(&idx, (const unsigned char *)git_str_cstr(&midx_buf), git_str_len(&midx_buf)) < 0) + goto cleanup; + + /* Search for any oid, just to exercise that codepath. */ + if (git_midx_entry_find(&e, &idx, &oid, GIT_OID_SHA1_HEXSIZE) < 0) + goto cleanup; + +cleanup: + git_midx_close(&idx); + git_str_dispose(&midx_buf); + return 0; +} diff --git a/fuzzers/objects_fuzzer.c b/fuzzers/objects_fuzzer.c new file mode 100644 index 00000000000..7294e9b35aa --- /dev/null +++ b/fuzzers/objects_fuzzer.c @@ -0,0 +1,49 @@ +/* + * libgit2 packfile fuzzer target. + * + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2.h" +#include "object.h" + +#include "standalone_driver.h" + +#define UNUSED(x) (void)(x) + +int LLVMFuzzerInitialize(int *argc, char ***argv) +{ + UNUSED(argc); + UNUSED(argv); + + if (git_libgit2_init() < 0) + abort(); + + return 0; +} + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + const git_object_t types[] = { + GIT_OBJECT_BLOB, GIT_OBJECT_TREE, GIT_OBJECT_COMMIT, GIT_OBJECT_TAG + }; + git_object *object = NULL; + size_t i; + + /* + * Brute-force parse this as every object type. We want + * to stress the parsing logic anyway, so this is fine + * to do. + */ + for (i = 0; i < ARRAY_SIZE(types); i++) { + if (git_object__from_raw(&object, (const char *) data, size, types[i], GIT_OID_SHA1) < 0) + continue; + git_object_free(object); + object = NULL; + } + + return 0; +} diff --git a/fuzzers/packfile_fuzzer.c b/fuzzers/packfile_fuzzer.c new file mode 100644 index 00000000000..cc4c33ad536 --- /dev/null +++ b/fuzzers/packfile_fuzzer.c @@ -0,0 +1,136 @@ +/* + * libgit2 packfile fuzzer target. + * + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include + +#include "git2.h" +#include "git2/sys/mempack.h" +#include "common.h" +#include "str.h" + +#include "standalone_driver.h" + +static git_odb *odb = NULL; +static git_odb_backend *mempack = NULL; + +/* Arbitrary object to seed the ODB. */ +static const unsigned char base_obj[] = { 07, 076 }; +static const unsigned int base_obj_len = 2; + +int LLVMFuzzerInitialize(int *argc, char ***argv) +{ + GIT_UNUSED(argc); + GIT_UNUSED(argv); + + if (git_libgit2_init() < 0) { + fprintf(stderr, "Failed to initialize libgit2\n"); + abort(); + } + if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS, 10000000) < 0) { + fprintf(stderr, "Failed to limit maximum pack object count\n"); + abort(); + } + + if (git_odb_new(&odb) < 0) { + fprintf(stderr, "Failed to create the odb\n"); + abort(); + } + + if (git_mempack_new(&mempack) < 0) { + fprintf(stderr, "Failed to create the mempack\n"); + abort(); + } + if (git_odb_add_backend(odb, mempack, 999) < 0) { + fprintf(stderr, "Failed to add the mempack\n"); + abort(); + } + return 0; +} + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + git_indexer_progress stats = {0, 0}; + git_indexer *indexer = NULL; + git_str path = GIT_STR_INIT; + git_oid oid; + bool append_hash = false; + int error; + + if (size == 0) + return 0; + + if (!odb || !mempack) { + fprintf(stderr, "Global state not initialized\n"); + abort(); + } + git_mempack_reset(mempack); + + if (git_odb_write(&oid, odb, base_obj, base_obj_len, GIT_OBJECT_BLOB) < 0) { + fprintf(stderr, "Failed to add an object to the odb\n"); + abort(); + } + +#ifdef GIT_EXPERIMENTAL_SHA256 + error = git_indexer_new(&indexer, ".", NULL); +#else + error = git_indexer_new(&indexer, ".", 0, odb, NULL); +#endif + + if (error < 0) { + fprintf(stderr, "Failed to create the indexer: %s\n", + git_error_last()->message); + abort(); + } + + /* + * If the first byte in the stream has the high bit set, append the + * SHA1 hash so that the packfile is somewhat valid. + */ + append_hash = *data & 0x80; + ++data; + --size; + + if (git_indexer_append(indexer, data, size, &stats) < 0) + goto cleanup; + if (append_hash) { +#ifdef GIT_EXPERIMENTAL_SHA256 + if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB, GIT_OID_SHA1) < 0) { + fprintf(stderr, "Failed to compute the SHA1 hash\n"); + abort(); + } +#else + if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB) < 0) { + fprintf(stderr, "Failed to compute the SHA1 hash\n"); + abort(); + } +#endif + + if (git_indexer_append(indexer, &oid.id, GIT_OID_SHA1_SIZE, &stats) < 0) { + goto cleanup; + } + } + if (git_indexer_commit(indexer, &stats) < 0) + goto cleanup; + + if (git_str_printf(&path, "pack-%s.idx", git_indexer_name(indexer)) < 0) + goto cleanup; + p_unlink(git_str_cstr(&path)); + + git_str_clear(&path); + + if (git_str_printf(&path, "pack-%s.pack", git_indexer_name(indexer)) < 0) + goto cleanup; + p_unlink(git_str_cstr(&path)); + +cleanup: + git_mempack_reset(mempack); + git_indexer_free(indexer); + git_str_dispose(&path); + return 0; +} diff --git a/fuzzers/patch_parse_fuzzer.c b/fuzzers/patch_parse_fuzzer.c new file mode 100644 index 00000000000..2e65a01fd88 --- /dev/null +++ b/fuzzers/patch_parse_fuzzer.c @@ -0,0 +1,40 @@ +/* + * libgit2 patch parser fuzzer target. + * + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2.h" +#include "patch.h" +#include "patch_parse.h" + +#include "standalone_driver.h" + +#define UNUSED(x) (void)(x) + +int LLVMFuzzerInitialize(int *argc, char ***argv) +{ + UNUSED(argc); + UNUSED(argv); + + if (git_libgit2_init() < 0) + abort(); + + return 0; +} + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + if (size) { + git_patch *patch = NULL; + git_patch_options opts = GIT_PATCH_OPTIONS_INIT; + opts.prefix_len = (uint32_t)data[0]; + git_patch_from_buffer(&patch, (const char *)data + 1, size - 1, + &opts); + git_patch_free(patch); + } + return 0; +} diff --git a/fuzzers/revparse_fuzzer.c b/fuzzers/revparse_fuzzer.c new file mode 100644 index 00000000000..37c22e2221c --- /dev/null +++ b/fuzzers/revparse_fuzzer.c @@ -0,0 +1,52 @@ +/* + * libgit2 revparse fuzzer target. + * + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include + +#include "git2.h" + +#include "standalone_driver.h" +#include "fuzzer_utils.h" + +#define UNUSED(x) (void)(x) + +static git_repository *repo; + +int LLVMFuzzerInitialize(int *argc, char ***argv) +{ + UNUSED(argc); + UNUSED(argv); + + if (git_libgit2_init() < 0) + abort(); + + if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS, 10000000) < 0) + abort(); + + repo = fuzzer_repo_init(); + return 0; +} + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + git_object *obj = NULL; + char *c; + + if ((c = calloc(1, size + 1)) == NULL) + abort(); + + memcpy(c, data, size); + + git_revparse_single(&obj, repo, c); + git_object_free(obj); + free(c); + + return 0; +} diff --git a/fuzzers/standalone_driver.c b/fuzzers/standalone_driver.c new file mode 100644 index 00000000000..17b54de952e --- /dev/null +++ b/fuzzers/standalone_driver.c @@ -0,0 +1,73 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include + +#include "git2.h" +#include "futils.h" +#include "path.h" + +#include "standalone_driver.h" + +static int run_one_file(const char *filename) +{ + git_str buf = GIT_STR_INIT; + int error = 0; + + if (git_futils_readbuffer(&buf, filename) < 0) { + fprintf(stderr, "Failed to read %s: %s\n", filename, git_error_last()->message); + error = -1; + goto exit; + } + + LLVMFuzzerTestOneInput((const unsigned char *)buf.ptr, buf.size); +exit: + git_str_dispose(&buf); + return error; +} + +int main(int argc, char **argv) +{ + git_vector corpus_files = GIT_VECTOR_INIT; + char *filename = NULL; + unsigned i = 0; + int error = 0; + + if (git_libgit2_init() < 0) { + fprintf(stderr, "Failed to initialize libgit2\n"); + abort(); + } + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + error = -1; + goto exit; + } + + fprintf(stderr, "Running %s against %s\n", argv[0], argv[1]); + LLVMFuzzerInitialize(&argc, &argv); + + if (git_fs_path_dirload(&corpus_files, argv[1], 0, 0x0) < 0) { + fprintf(stderr, "Failed to scan corpus directory '%s': %s\n", + argv[1], git_error_last()->message); + error = -1; + goto exit; + } + git_vector_foreach(&corpus_files, i, filename) { + fprintf(stderr, "\tRunning %s...\n", filename); + if (run_one_file(filename) < 0) { + error = -1; + goto exit; + } + } + fprintf(stderr, "Done %d runs\n", i); + +exit: + git_vector_dispose_deep(&corpus_files); + git_libgit2_shutdown(); + return error; +} diff --git a/fuzzers/standalone_driver.h b/fuzzers/standalone_driver.h new file mode 100644 index 00000000000..507fcb9fdb1 --- /dev/null +++ b/fuzzers/standalone_driver.h @@ -0,0 +1,14 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_standalone_driver_h__ +#define INCLUDE_standalone_driver_h__ + +extern int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size); +extern int LLVMFuzzerInitialize(int *argc, char ***argv); + +#endif diff --git a/git.git-authors b/git.git-authors index 3fcf27ffcc1..d9a911419a6 100644 --- a/git.git-authors +++ b/git.git-authors @@ -25,12 +25,9 @@ # contributed code (possibly with some exceptions) # "no" means the author does not consent # "ask" means that the contributor wants to give/withhold -# his/her consent on a patch-by-patch basis. +# their consent on a patch-by-patch basis. # "???" means the person is a prominent contributor who has -# not yet made his/her standpoint clear. -# "ign" means the authors consent is ignored for the purpose -# of libification. This is because the author has contributed -# to areas that aren't interesting for the library. +# not yet made their standpoint clear. # # Please try to keep the list alphabetically ordered. It will # help in case we get all 600-ish git.git authors on it. @@ -39,32 +36,42 @@ # but has otherwise not contributed to git.) # ok Adam Simpkins (http transport) +ok Adrian Johnson +ok Alexey Shumkin ok Andreas Ericsson +ok Antoine Pelisse ok Boyd Lynn Gerber +ok Brandon Casey ok Brian Downing ok Brian Gernhardt ok Christian Couder ok Daniel Barkalow +ok Elijah Newren ok Florian Forster +ok Gustaf Hendeby ok Holger Weiss ok Jeff King ok Johannes Schindelin ok Johannes Sixt +ask Jonathan Nieder +ok Jonathan Tan ok Junio C Hamano ok Kristian Høgsberg ok Linus Torvalds ok Lukas Sandström ok Matthieu Moy -ign Mike McCormack (imap-send) +ok Michael Haggerty ok Nicolas Pitre ok Paolo Bonzini ok Paul Kocher ok Peter Hagervall +ok Petr Onderka ok Pierre Habouzit ok Pieter de Bie ok René Scharfe -ign Robert Shearman (imap-send) ok Sebastian Schuberth ok Shawn O. Pearce ok Steffen Prohaska ok Sven Verdoolaege +ask Thomas Rast (ok before 6-Oct-2013) +ok Torsten Bögershausen diff --git a/include/git2.h b/include/git2.h index 5f9fc4824b5..3457e5f0476 100644 --- a/include/git2.h +++ b/include/git2.h @@ -8,53 +8,67 @@ #ifndef INCLUDE_git_git_h__ #define INCLUDE_git_git_h__ -#include "git2/version.h" - -#include "git2/common.h" -#include "git2/threads.h" -#include "git2/errors.h" - -#include "git2/types.h" - -#include "git2/oid.h" -#include "git2/signature.h" -#include "git2/odb.h" - -#include "git2/repository.h" -#include "git2/revwalk.h" -#include "git2/merge.h" -#include "git2/graph.h" -#include "git2/refs.h" -#include "git2/reflog.h" -#include "git2/revparse.h" - -#include "git2/object.h" +#include "git2/annotated_commit.h" +#include "git2/apply.h" +#include "git2/attr.h" #include "git2/blob.h" +#include "git2/blame.h" +#include "git2/branch.h" +#include "git2/buffer.h" +#include "git2/cert.h" +#include "git2/checkout.h" +#include "git2/cherrypick.h" +#include "git2/clone.h" #include "git2/commit.h" -#include "git2/tag.h" -#include "git2/tree.h" -#include "git2/diff.h" - -#include "git2/index.h" +#include "git2/common.h" #include "git2/config.h" -#include "git2/transport.h" -#include "git2/remote.h" -#include "git2/clone.h" -#include "git2/checkout.h" -#include "git2/push.h" - -#include "git2/attr.h" +#include "git2/credential.h" +#include "git2/deprecated.h" +#include "git2/describe.h" +#include "git2/diff.h" +#include "git2/email.h" +#include "git2/errors.h" +#include "git2/experimental.h" +#include "git2/filter.h" +#include "git2/global.h" +#include "git2/graph.h" #include "git2/ignore.h" -#include "git2/branch.h" -#include "git2/refspec.h" -#include "git2/net.h" -#include "git2/status.h" +#include "git2/index.h" #include "git2/indexer.h" -#include "git2/submodule.h" -#include "git2/notes.h" -#include "git2/reset.h" +#include "git2/mailmap.h" +#include "git2/merge.h" #include "git2/message.h" +#include "git2/net.h" +#include "git2/notes.h" +#include "git2/object.h" +#include "git2/odb.h" +#include "git2/odb_backend.h" +#include "git2/oid.h" #include "git2/pack.h" +#include "git2/patch.h" +#include "git2/pathspec.h" +#include "git2/proxy.h" +#include "git2/rebase.h" +#include "git2/refdb.h" +#include "git2/reflog.h" +#include "git2/refs.h" +#include "git2/refspec.h" +#include "git2/remote.h" +#include "git2/repository.h" +#include "git2/reset.h" +#include "git2/revert.h" +#include "git2/revparse.h" +#include "git2/revwalk.h" +#include "git2/signature.h" #include "git2/stash.h" +#include "git2/status.h" +#include "git2/submodule.h" +#include "git2/tag.h" +#include "git2/transport.h" +#include "git2/transaction.h" +#include "git2/tree.h" +#include "git2/types.h" +#include "git2/version.h" +#include "git2/worktree.h" #endif diff --git a/include/git2/annotated_commit.h b/include/git2/annotated_commit.h new file mode 100644 index 00000000000..04f3b1c381f --- /dev/null +++ b/include/git2/annotated_commit.h @@ -0,0 +1,132 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_annotated_commit_h__ +#define INCLUDE_git_annotated_commit_h__ + +#include "common.h" +#include "repository.h" +#include "types.h" + +/** + * @file git2/annotated_commit.h + * @brief A commit and information about how it was looked up by the user. + * @defgroup git_annotated_commit Git annotated commit routines + * @ingroup Git + * + * An "annotated commit" is a commit that contains information about + * how the commit was resolved, which can be used for maintaining the + * user's "intent" through commands like merge and rebase. For example, + * if a user wants to "merge HEAD" then an annotated commit is used to + * both contain the HEAD commit _and_ the fact that it was resolved as + * the HEAD ref. + * @{ + */ +GIT_BEGIN_DECL + +/** + * Creates a `git_annotated_commit` from the given reference. + * The resulting git_annotated_commit must be freed with + * `git_annotated_commit_free`. + * + * @param[out] out pointer to store the git_annotated_commit result in + * @param repo repository that contains the given reference + * @param ref reference to use to lookup the git_annotated_commit + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_annotated_commit_from_ref( + git_annotated_commit **out, + git_repository *repo, + const git_reference *ref); + +/** + * Creates a `git_annotated_commit` from the given fetch head data. + * The resulting git_annotated_commit must be freed with + * `git_annotated_commit_free`. + * + * @param[out] out pointer to store the git_annotated_commit result in + * @param repo repository that contains the given commit + * @param branch_name name of the (remote) branch + * @param remote_url url of the remote + * @param id the commit object id of the remote branch + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_annotated_commit_from_fetchhead( + git_annotated_commit **out, + git_repository *repo, + const char *branch_name, + const char *remote_url, + const git_oid *id); + +/** + * Creates a `git_annotated_commit` from the given commit id. + * The resulting git_annotated_commit must be freed with + * `git_annotated_commit_free`. + * + * An annotated commit contains information about how it was + * looked up, which may be useful for functions like merge or + * rebase to provide context to the operation. For example, + * conflict files will include the name of the source or target + * branches being merged. It is therefore preferable to use the + * most specific function (eg `git_annotated_commit_from_ref`) + * instead of this one when that data is known. + * + * @param[out] out pointer to store the git_annotated_commit result in + * @param repo repository that contains the given commit + * @param id the commit object id to lookup + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_annotated_commit_lookup( + git_annotated_commit **out, + git_repository *repo, + const git_oid *id); + +/** + * Creates a `git_annotated_commit` from a revision string. + * + * See `man gitrevisions`, or + * http://git-scm.com/docs/git-rev-parse.html#_specifying_revisions for + * information on the syntax accepted. + * + * @param[out] out pointer to store the git_annotated_commit result in + * @param repo repository that contains the given commit + * @param revspec the extended sha syntax string to use to lookup the commit + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_annotated_commit_from_revspec( + git_annotated_commit **out, + git_repository *repo, + const char *revspec); + +/** + * Gets the commit ID that the given `git_annotated_commit` refers to. + * + * @param commit the given annotated commit + * @return commit id + */ +GIT_EXTERN(const git_oid *) git_annotated_commit_id( + const git_annotated_commit *commit); + +/** + * Get the refname that the given `git_annotated_commit` refers to. + * + * @param commit the given annotated commit + * @return ref name. + */ +GIT_EXTERN(const char *) git_annotated_commit_ref( + const git_annotated_commit *commit); + +/** + * Frees a `git_annotated_commit`. + * + * @param commit annotated commit to free + */ +GIT_EXTERN(void) git_annotated_commit_free( + git_annotated_commit *commit); + +/** @} */ +GIT_END_DECL +#endif diff --git a/include/git2/apply.h b/include/git2/apply.h new file mode 100644 index 00000000000..7ab939d1f2d --- /dev/null +++ b/include/git2/apply.h @@ -0,0 +1,186 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_apply_h__ +#define INCLUDE_git_apply_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" +#include "diff.h" + +/** + * @file git2/apply.h + * @brief Apply patches to the working directory or index + * @defgroup git_apply Git patch application routines + * @ingroup Git + * + * Mechanisms to apply a patch to the index, the working directory, + * or both. + * @{ + */ +GIT_BEGIN_DECL + +/** + * When applying a patch, callback that will be made per delta (file). + * + * When the callback: + * - returns < 0, the apply process will be aborted. + * - returns > 0, the delta will not be applied, but the apply process + * continues + * - returns 0, the delta is applied, and the apply process continues. + * + * @param delta The delta to be applied + * @param payload User-specified payload + * @return 0 if the delta is applied, < 0 if the apply process will be aborted + * or > 0 if the delta will not be applied. + */ +typedef int GIT_CALLBACK(git_apply_delta_cb)( + const git_diff_delta *delta, + void *payload); + +/** + * When applying a patch, callback that will be made per hunk. + * + * When the callback: + * - returns < 0, the apply process will be aborted. + * - returns > 0, the hunk will not be applied, but the apply process + * continues + * - returns 0, the hunk is applied, and the apply process continues. + * + * @param hunk The hunk to be applied + * @param payload User-specified payload + * @return 0 if the hunk is applied, < 0 if the apply process will be aborted + * or > 0 if the hunk will not be applied. + */ +typedef int GIT_CALLBACK(git_apply_hunk_cb)( + const git_diff_hunk *hunk, + void *payload); + +/** + * Flags controlling the behavior of `git_apply`. + * + * When the callback: + * - returns < 0, the apply process will be aborted. + * - returns > 0, the hunk will not be applied, but the apply process + * continues + * - returns 0, the hunk is applied, and the apply process continues. + */ +typedef enum { + /** + * Don't actually make changes, just test that the patch applies. + * This is the equivalent of `git apply --check`. + */ + GIT_APPLY_CHECK = (1 << 0) +} git_apply_flags_t; + +/** + * Apply options structure. + * + * When the callback: + * - returns < 0, the apply process will be aborted. + * - returns > 0, the hunk will not be applied, but the apply process + * continues + * - returns 0, the hunk is applied, and the apply process continues. + * + * Initialize with `GIT_APPLY_OPTIONS_INIT`. Alternatively, you can + * use `git_apply_options_init`. + * + * @see git_apply_to_tree + * @see git_apply + */ +typedef struct { + unsigned int version; /**< The version */ + + /** When applying a patch, callback that will be made per delta (file). */ + git_apply_delta_cb delta_cb; + + /** When applying a patch, callback that will be made per hunk. */ + git_apply_hunk_cb hunk_cb; + + /** Payload passed to both `delta_cb` & `hunk_cb`. */ + void *payload; + + /** Bitmask of `git_apply_flags_t` */ + unsigned int flags; +} git_apply_options; + +/** Current version for the `git_apply_options` structure */ +#define GIT_APPLY_OPTIONS_VERSION 1 + +/** Static constructor for `git_apply_options` */ +#define GIT_APPLY_OPTIONS_INIT {GIT_APPLY_OPTIONS_VERSION} + +/** + * Initialize git_apply_options structure + * + * Initialize a `git_apply_options` with default values. Equivalent to creating + * an instance with GIT_APPLY_OPTIONS_INIT. + * + * @param opts The `git_apply_options` struct to initialize. + * @param version The struct version; pass `GIT_APPLY_OPTIONS_VERSION` + * @return 0 on success or -1 on failure. + */ +GIT_EXTERN(int) git_apply_options_init(git_apply_options *opts, unsigned int version); + +/** + * Apply a `git_diff` to a `git_tree`, and return the resulting image + * as an index. + * + * @param out the postimage of the application + * @param repo the repository to apply + * @param preimage the tree to apply the diff to + * @param diff the diff to apply + * @param options the options for the apply (or null for defaults) + * @return 0 or an error code + */ +GIT_EXTERN(int) git_apply_to_tree( + git_index **out, + git_repository *repo, + git_tree *preimage, + git_diff *diff, + const git_apply_options *options); + +/** Possible application locations for git_apply */ +typedef enum { + /** + * Apply the patch to the workdir, leaving the index untouched. + * This is the equivalent of `git apply` with no location argument. + */ + GIT_APPLY_LOCATION_WORKDIR = 0, + + /** + * Apply the patch to the index, leaving the working directory + * untouched. This is the equivalent of `git apply --cached`. + */ + GIT_APPLY_LOCATION_INDEX = 1, + + /** + * Apply the patch to both the working directory and the index. + * This is the equivalent of `git apply --index`. + */ + GIT_APPLY_LOCATION_BOTH = 2 +} git_apply_location_t; + +/** + * Apply a `git_diff` to the given repository, making changes directly + * in the working directory, the index, or both. + * + * @param repo the repository to apply to + * @param diff the diff to apply + * @param location the location to apply (workdir, index or both) + * @param options the options for the apply (or null for defaults) + * @return 0 or an error code + */ +GIT_EXTERN(int) git_apply( + git_repository *repo, + git_diff *diff, + git_apply_location_t location, + const git_apply_options *options); + +/** @} */ +GIT_END_DECL +#endif diff --git a/include/git2/attr.h b/include/git2/attr.h index dea44f0e318..e5216fef99e 100644 --- a/include/git2/attr.h +++ b/include/git2/attr.h @@ -12,9 +12,13 @@ /** * @file git2/attr.h - * @brief Git attribute management routines + * @brief Attribute management routines * @defgroup git_attr Git attribute management routines * @ingroup Git + * + * Attributes specify additional information about how git should + * handle particular paths - for example, they may indicate whether + * a particular filter is applied, like LFS or line ending conversions. * @{ */ GIT_BEGIN_DECL @@ -30,7 +34,7 @@ GIT_BEGIN_DECL * Then for file `xyz.c` looking up attribute "foo" gives a value for * which `GIT_ATTR_TRUE(value)` is true. */ -#define GIT_ATTR_TRUE(attr) (git_attr_value(attr) == GIT_ATTR_TRUE_T) +#define GIT_ATTR_IS_TRUE(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_TRUE) /** * GIT_ATTR_FALSE checks if an attribute is set off. In core git @@ -44,7 +48,7 @@ GIT_BEGIN_DECL * Then for file `zyx.h` looking up attribute "foo" gives a value for * which `GIT_ATTR_FALSE(value)` is true. */ -#define GIT_ATTR_FALSE(attr) (git_attr_value(attr) == GIT_ATTR_FALSE_T) +#define GIT_ATTR_IS_FALSE(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_FALSE) /** * GIT_ATTR_UNSPECIFIED checks if an attribute is unspecified. This @@ -62,7 +66,7 @@ GIT_BEGIN_DECL * file `onefile.rb` or looking up "bar" on any file will all give * `GIT_ATTR_UNSPECIFIED(value)` of true. */ -#define GIT_ATTR_UNSPECIFIED(attr) (git_attr_value(attr) == GIT_ATTR_UNSPECIFIED_T) +#define GIT_ATTR_IS_UNSPECIFIED(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_UNSPECIFIED) /** * GIT_ATTR_HAS_VALUE checks if an attribute is set to a value (as @@ -74,29 +78,32 @@ GIT_BEGIN_DECL * Given this, looking up "eol" for `onefile.txt` will give back the * string "lf" and `GIT_ATTR_SET_TO_VALUE(attr)` will return true. */ -#define GIT_ATTR_HAS_VALUE(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_T) +#define GIT_ATTR_HAS_VALUE(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_STRING) +/** + * Possible states for an attribute + */ typedef enum { - GIT_ATTR_UNSPECIFIED_T = 0, - GIT_ATTR_TRUE_T, - GIT_ATTR_FALSE_T, - GIT_ATTR_VALUE_T, -} git_attr_t; + GIT_ATTR_VALUE_UNSPECIFIED = 0, /**< The attribute has been left unspecified */ + GIT_ATTR_VALUE_TRUE, /**< The attribute has been set */ + GIT_ATTR_VALUE_FALSE, /**< The attribute has been unset */ + GIT_ATTR_VALUE_STRING /**< This attribute has a value */ +} git_attr_value_t; -/* - * Return the value type for a given attribute. +/** + * Return the value type for a given attribute. * - * This can be either `TRUE`, `FALSE`, `UNSPECIFIED` (if the attribute - * was not set at all), or `VALUE`, if the attribute was set to - * an actual string. + * This can be either `TRUE`, `FALSE`, `UNSPECIFIED` (if the attribute + * was not set at all), or `VALUE`, if the attribute was set to an + * actual string. * - * If the attribute has a `VALUE` string, it can be accessed normally - * as a NULL-terminated C string. + * If the attribute has a `VALUE` string, it can be accessed normally + * as a NULL-terminated C string. * - * @param attr The attribute - * @return the value type for the attribute + * @param attr The attribute + * @return the value type for the attribute */ -GIT_EXTERN(git_attr_t) git_attr_value(const char *attr); +GIT_EXTERN(git_attr_value_t) git_attr_value(const char *attr); /** * Check attribute flags: Reading values from index and working directory. @@ -111,18 +118,63 @@ GIT_EXTERN(git_attr_t) git_attr_value(const char *attr); * use index only for creating archives or for a bare repo (if an * index has been specified for the bare repo). */ + +/** Examine attribute in working directory, then index */ #define GIT_ATTR_CHECK_FILE_THEN_INDEX 0 +/** Examine attribute in index, then working directory */ #define GIT_ATTR_CHECK_INDEX_THEN_FILE 1 -#define GIT_ATTR_CHECK_INDEX_ONLY 2 +/** Examine attributes only in the index */ +#define GIT_ATTR_CHECK_INDEX_ONLY 2 /** - * Check attribute flags: Using the system attributes file. + * Check attribute flags: controlling extended attribute behavior. * * Normally, attribute checks include looking in the /etc (or system - * equivalent) directory for a `gitattributes` file. Passing this - * flag will cause attribute checks to ignore that file. + * equivalent) directory for a `gitattributes` file. Passing the + * `GIT_ATTR_CHECK_NO_SYSTEM` flag will cause attribute checks to + * ignore that file. + * + * Passing the `GIT_ATTR_CHECK_INCLUDE_HEAD` flag will use attributes + * from a `.gitattributes` file in the repository at the HEAD revision. + * + * Passing the `GIT_ATTR_CHECK_INCLUDE_COMMIT` flag will use attributes + * from a `.gitattributes` file in a specific commit. */ -#define GIT_ATTR_CHECK_NO_SYSTEM (1 << 2) + +/** Ignore system attributes */ +#define GIT_ATTR_CHECK_NO_SYSTEM (1 << 2) +/** Honor `.gitattributes` in the HEAD revision */ +#define GIT_ATTR_CHECK_INCLUDE_HEAD (1 << 3) +/** Honor `.gitattributes` in a specific commit */ +#define GIT_ATTR_CHECK_INCLUDE_COMMIT (1 << 4) + +/** +* An options structure for querying attributes. +*/ +typedef struct { + unsigned int version; + + /** A combination of GIT_ATTR_CHECK flags */ + unsigned int flags; + +#ifdef GIT_DEPRECATE_HARD + void *reserved; +#else + git_oid *commit_id; +#endif + + /** + * The commit to load attributes from, when + * `GIT_ATTR_CHECK_INCLUDE_COMMIT` is specified. + */ + git_oid attr_commit_id; +} git_attr_options; + +/** Current version for the `git_attr_options` structure */ +#define GIT_ATTR_OPTIONS_VERSION 1 + +/** Static constructor for `git_attr_options` */ +#define GIT_ATTR_OPTIONS_INIT {GIT_ATTR_OPTIONS_VERSION} /** * Look up the value of one git attribute for path. @@ -138,14 +190,38 @@ GIT_EXTERN(git_attr_t) git_attr_value(const char *attr); * not have to exist, but if it does not, then it will be * treated as a plain file (not a directory). * @param name The name of the attribute to look up. + * @return 0 or an error code. */ GIT_EXTERN(int) git_attr_get( const char **value_out, - git_repository *repo, + git_repository *repo, uint32_t flags, const char *path, const char *name); +/** + * Look up the value of one git attribute for path with extended options. + * + * @param value_out Output of the value of the attribute. Use the GIT_ATTR_... + * macros to test for TRUE, FALSE, UNSPECIFIED, etc. or just + * use the string value for attributes set to a value. You + * should NOT modify or free this value. + * @param repo The repository containing the path. + * @param opts The `git_attr_options` to use when querying these attributes. + * @param path The path to check for attributes. Relative paths are + * interpreted relative to the repo root. The file does + * not have to exist, but if it does not, then it will be + * treated as a plain file (not a directory). + * @param name The name of the attribute to look up. + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_attr_get_ext( + const char **value_out, + git_repository *repo, + git_attr_options *opts, + const char *path, + const char *name); + /** * Look up a list of git attributes for path. * @@ -162,7 +238,7 @@ GIT_EXTERN(int) git_attr_get( * Then you could loop through the 3 values to get the settings for * the three attributes you asked about. * - * @param values An array of num_attr entries that will have string + * @param values_out An array of num_attr entries that will have string * pointers written into it for the values of the attributes. * You should not modify or free the values that are written * into this array (although of course, you should free the @@ -174,6 +250,7 @@ GIT_EXTERN(int) git_attr_get( * it will be treated as a plain file (i.e. not a directory). * @param num_attr The number of attributes being looked up * @param names An array of num_attr strings containing attribute names. + * @return 0 or an error code. */ GIT_EXTERN(int) git_attr_get_many( const char **values_out, @@ -183,7 +260,48 @@ GIT_EXTERN(int) git_attr_get_many( size_t num_attr, const char **names); -typedef int (*git_attr_foreach_cb)(const char *name, const char *value, void *payload); +/** + * Look up a list of git attributes for path with extended options. + * + * @param values_out An array of num_attr entries that will have string + * pointers written into it for the values of the attributes. + * You should not modify or free the values that are written + * into this array (although of course, you should free the + * array itself if you allocated it). + * @param repo The repository containing the path. + * @param opts The `git_attr_options` to use when querying these attributes. + * @param path The path inside the repo to check attributes. This + * does not have to exist, but if it does not, then + * it will be treated as a plain file (i.e. not a directory). + * @param num_attr The number of attributes being looked up + * @param names An array of num_attr strings containing attribute names. + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_attr_get_many_ext( + const char **values_out, + git_repository *repo, + git_attr_options *opts, + const char *path, + size_t num_attr, + const char **names); + +/** + * The callback used with git_attr_foreach. + * + * This callback will be invoked only once per attribute name, even if there + * are multiple rules for a given file. The highest priority rule will be + * used. + * + * @see git_attr_foreach. + * + * @param name The attribute name. + * @param value The attribute value. May be NULL if the attribute is explicitly + * set to UNSPECIFIED using the '!' sign. + * @param payload A user-specified pointer. + * @return 0 to continue looping, non-zero to stop. This value will be returned + * from git_attr_foreach. + */ +typedef int GIT_CALLBACK(git_attr_foreach_cb)(const char *name, const char *value, void *payload); /** * Loop over all the git attributes for a path. @@ -193,14 +311,10 @@ typedef int (*git_attr_foreach_cb)(const char *name, const char *value, void *pa * @param path Path inside the repo to check attributes. This does not have * to exist, but if it does not, then it will be treated as a * plain file (i.e. not a directory). - * @param callback Function to invoke on each attribute name and value. The - * value may be NULL is the attribute is explicitly set to - * UNSPECIFIED using the '!' sign. Callback will be invoked - * only once per attribute name, even if there are multiple - * rules for a given file. The highest priority rule will be - * used. Return a non-zero value from this to stop looping. + * @param callback Function to invoke on each attribute name and value. + * See git_attr_foreach_cb. * @param payload Passed on as extra parameter to callback function. - * @return 0 on success, GIT_EUSER on non-zero callback, or error code + * @return 0 on success, non-zero callback return value, or error code */ GIT_EXTERN(int) git_attr_foreach( git_repository *repo, @@ -209,6 +323,26 @@ GIT_EXTERN(int) git_attr_foreach( git_attr_foreach_cb callback, void *payload); +/** + * Loop over all the git attributes for a path with extended options. + * + * @param repo The repository containing the path. + * @param opts The `git_attr_options` to use when querying these attributes. + * @param path Path inside the repo to check attributes. This does not have + * to exist, but if it does not, then it will be treated as a + * plain file (i.e. not a directory). + * @param callback Function to invoke on each attribute name and value. + * See git_attr_foreach_cb. + * @param payload Passed on as extra parameter to callback function. + * @return 0 on success, non-zero callback return value, or error code + */ +GIT_EXTERN(int) git_attr_foreach_ext( + git_repository *repo, + git_attr_options *opts, + const char *path, + git_attr_foreach_cb callback, + void *payload); + /** * Flush the gitattributes cache. * @@ -216,19 +350,27 @@ GIT_EXTERN(int) git_attr_foreach( * disk no longer match the cached contents of memory. This will cause * the attributes files to be reloaded the next time that an attribute * access function is called. + * + * @param repo The repository containing the gitattributes cache + * @return 0 on success, or an error code */ -GIT_EXTERN(void) git_attr_cache_flush( +GIT_EXTERN(int) git_attr_cache_flush( git_repository *repo); /** * Add a macro definition. * * Macros will automatically be loaded from the top level `.gitattributes` - * file of the repository (plus the build-in "binary" macro). This + * file of the repository (plus the built-in "binary" macro). This * function allows you to add others. For example, to add the default * macro, you would call: * - * git_attr_add_macro(repo, "binary", "-diff -crlf"); + * git_attr_add_macro(repo, "binary", "-diff -crlf"); + * + * @param repo The repository to add the macro in. + * @param name The name of the macro. + * @param values The value for the macro. + * @return 0 or an error code. */ GIT_EXTERN(int) git_attr_add_macro( git_repository *repo, diff --git a/include/git2/blame.h b/include/git2/blame.h new file mode 100644 index 00000000000..f3e66924c89 --- /dev/null +++ b/include/git2/blame.h @@ -0,0 +1,390 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_git_blame_h__ +#define INCLUDE_git_blame_h__ + +#include "common.h" +#include "oid.h" + +/** + * @file git2/blame.h + * @brief Specify a file's most recent changes per-line + * @defgroup git_blame Git blame routines + * @ingroup Git + * + * Producing a "blame" (or "annotated history") decorates individual + * lines in a file with the commit that introduced that particular line + * of changes. This can be useful to indicate when and why a particular + * change was made. + * @{ + */ +GIT_BEGIN_DECL + +/** + * Flags for indicating option behavior for git_blame APIs. + */ +typedef enum { + /** Normal blame, the default */ + GIT_BLAME_NORMAL = 0, + + /** + * Track lines that have moved within a file (like `git blame -M`). + * + * This is not yet implemented and reserved for future use. + */ + GIT_BLAME_TRACK_COPIES_SAME_FILE = (1<<0), + + /** + * Track lines that have moved across files in the same commit + * (like `git blame -C`). + * + * This is not yet implemented and reserved for future use. + */ + GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES = (1<<1), + + /** + * Track lines that have been copied from another file that exists + * in the same commit (like `git blame -CC`). Implies SAME_FILE. + * + * This is not yet implemented and reserved for future use. + */ + GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES = (1<<2), + + /** + * Track lines that have been copied from another file that exists in + * *any* commit (like `git blame -CCC`). Implies SAME_COMMIT_COPIES. + * + * This is not yet implemented and reserved for future use. + */ + GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES = (1<<3), + + /** + * Restrict the search of commits to those reachable following only + * the first parents. + */ + GIT_BLAME_FIRST_PARENT = (1<<4), + + /** + * Use mailmap file to map author and committer names and email + * addresses to canonical real names and email addresses. The + * mailmap will be read from the working directory, or HEAD in a + * bare repository. + */ + GIT_BLAME_USE_MAILMAP = (1<<5), + + /** Ignore whitespace differences */ + GIT_BLAME_IGNORE_WHITESPACE = (1<<6) +} git_blame_flag_t; + +/** + * Blame options structure + * + * Initialize with `GIT_BLAME_OPTIONS_INIT`. Alternatively, you can + * use `git_blame_options_init`. + * + */ +typedef struct git_blame_options { + unsigned int version; + + /** A combination of `git_blame_flag_t` */ + unsigned int flags; + + /** + * The lower bound on the number of alphanumeric characters that + * must be detected as moving/copying within a file for it to + * associate those lines with the parent commit. The default value + * is 20. + * + * This value only takes effect if any of the `GIT_BLAME_TRACK_COPIES_*` + * flags are specified. + */ + uint16_t min_match_characters; + + /** The id of the newest commit to consider. The default is HEAD. */ + git_oid newest_commit; + + /** + * The id of the oldest commit to consider. + * The default is the first commit encountered with a NULL parent. + */ + git_oid oldest_commit; + + /** + * The first line in the file to blame. + * The default is 1 (line numbers start with 1). + */ + size_t min_line; + + /** + * The last line in the file to blame. + * The default is the last line of the file. + */ + size_t max_line; +} git_blame_options; + +/** Current version for the `git_blame_options` structure */ +#define GIT_BLAME_OPTIONS_VERSION 1 + +/** Static constructor for `git_blame_options` */ +#define GIT_BLAME_OPTIONS_INIT {GIT_BLAME_OPTIONS_VERSION} + +/** + * Initialize git_blame_options structure + * + * Initializes a `git_blame_options` with default values. Equivalent to creating + * an instance with GIT_BLAME_OPTIONS_INIT. + * + * @param opts The `git_blame_options` struct to initialize. + * @param version The struct version; pass `GIT_BLAME_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_blame_options_init( + git_blame_options *opts, + unsigned int version); + +/** + * Structure that represents a blame hunk. + */ +typedef struct git_blame_hunk { + /** + * The number of lines in this hunk. + */ + size_t lines_in_hunk; + + /** + * The OID of the commit where this line was last changed. + */ + git_oid final_commit_id; + + /** + * The 1-based line number where this hunk begins, in the final version + * of the file. + */ + size_t final_start_line_number; + + /** + * The author of `final_commit_id`. If `GIT_BLAME_USE_MAILMAP` has been + * specified, it will contain the canonical real name and email address. + */ + git_signature *final_signature; + + /** + * The committer of `final_commit_id`. If `GIT_BLAME_USE_MAILMAP` has + * been specified, it will contain the canonical real name and email + * address. + */ + git_signature *final_committer; + + /** + * The OID of the commit where this hunk was found. + * This will usually be the same as `final_commit_id`, except when + * `GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES` has been specified. + */ + git_oid orig_commit_id; + + /** + * The path to the file where this hunk originated, as of the commit + * specified by `orig_commit_id`. + */ + const char *orig_path; + + /** + * The 1-based line number where this hunk begins in the file named by + * `orig_path` in the commit specified by `orig_commit_id`. + */ + size_t orig_start_line_number; + + /** + * The author of `orig_commit_id`. If `GIT_BLAME_USE_MAILMAP` has been + * specified, it will contain the canonical real name and email address. + */ + git_signature *orig_signature; + + /** + * The committer of `orig_commit_id`. If `GIT_BLAME_USE_MAILMAP` has + * been specified, it will contain the canonical real name and email + * address. + */ + git_signature *orig_committer; + + /* + * The summary of the commit. + */ + const char *summary; + + /** + * The 1 iff the hunk has been tracked to a boundary commit (the root, + * or the commit specified in git_blame_options.oldest_commit) + */ + char boundary; +} git_blame_hunk; + +/** + * Structure that represents a line in a blamed file. + */ +typedef struct git_blame_line { + const char *ptr; + size_t len; +} git_blame_line; + +/** Opaque structure to hold blame results */ +typedef struct git_blame git_blame; + +/** + * Gets the number of lines that exist in the blame structure. + * + * @param blame The blame structure to query. + * @return The number of line. + */ +GIT_EXTERN(size_t) git_blame_linecount(git_blame *blame); + +/** + * Gets the number of hunks that exist in the blame structure. + * + * @param blame The blame structure to query. + * @return The number of hunks. + */ +GIT_EXTERN(size_t) git_blame_hunkcount(git_blame *blame); + +/** + * Gets the blame hunk at the given index. + * + * @param blame the blame structure to query + * @param index index of the hunk to retrieve + * @return the hunk at the given index, or NULL on error + */ +GIT_EXTERN(const git_blame_hunk *) git_blame_hunk_byindex( + git_blame *blame, + size_t index); + +/** + * Gets the hunk that relates to the given line number in the newest + * commit. + * + * @param blame the blame structure to query + * @param lineno the (1-based) line number to find a hunk for + * @return the hunk that contains the given line, or NULL on error + */ +GIT_EXTERN(const git_blame_hunk *) git_blame_hunk_byline( + git_blame *blame, + size_t lineno); + +/** + * Gets the information about the line in the blame. + * + * @param blame the blame structure to query + * @param idx the (1-based) line number + * @return the blamed line, or NULL on error + */ +GIT_EXTERN(const git_blame_line *) git_blame_line_byindex( + git_blame *blame, + size_t idx); + +#ifndef GIT_DEPRECATE_HARD +/** + * Gets the number of hunks that exist in the blame structure. + * + * @param blame The blame structure to query. + * @return The number of hunks. + */ + +GIT_EXTERN(uint32_t) git_blame_get_hunk_count(git_blame *blame); + +/** + * Gets the blame hunk at the given index. + * + * @param blame the blame structure to query + * @param index index of the hunk to retrieve + * @return the hunk at the given index, or NULL on error + */ +GIT_EXTERN(const git_blame_hunk *) git_blame_get_hunk_byindex( + git_blame *blame, + uint32_t index); + +/** + * Gets the hunk that relates to the given line number in the newest commit. + * + * @param blame the blame structure to query + * @param lineno the (1-based) line number to find a hunk for + * @return the hunk that contains the given line, or NULL on error + */ +GIT_EXTERN(const git_blame_hunk *) git_blame_get_hunk_byline( + git_blame *blame, + size_t lineno); +#endif + +/** + * Get the blame for a single file in the repository. + * + * @param out pointer that will receive the blame object + * @param repo repository whose history is to be walked + * @param path path to file to consider + * @param options options for the blame operation or NULL + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_blame_file( + git_blame **out, + git_repository *repo, + const char *path, + git_blame_options *options); + +/** + * Get the blame for a single file in the repository, using the specified + * buffer contents as the uncommitted changes of the file (the working + * directory contents). + * + * @param out pointer that will receive the blame object + * @param repo repository whose history is to be walked + * @param path path to file to consider + * @param contents the uncommitted changes + * @param contents_len the length of the changes buffer + * @param options options for the blame operation or NULL + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_blame_file_from_buffer( + git_blame **out, + git_repository *repo, + const char *path, + const char *contents, + size_t contents_len, + git_blame_options *options); + +/** + * Get blame data for a file that has been modified in memory. The `blame` + * parameter is a pre-calculated blame for the in-odb history of the file. + * This means that once a file blame is completed (which can be expensive), + * updating the buffer blame is very fast. + * + * Lines that differ between the buffer and the committed version are + * marked as having a zero OID for their final_commit_id. + * + * @param out pointer that will receive the resulting blame data + * @param base cached blame from the history of the file (usually the output + * from git_blame_file) + * @param buffer the (possibly) modified contents of the file + * @param buffer_len number of valid bytes in the buffer + * @return 0 on success, or an error code. (use git_error_last for information + * about the error) + */ +GIT_EXTERN(int) git_blame_buffer( + git_blame **out, + git_blame *base, + const char *buffer, + size_t buffer_len); + +/** + * Free memory allocated by git_blame_file or git_blame_buffer. + * + * @param blame the blame structure to free + */ +GIT_EXTERN(void) git_blame_free(git_blame *blame); + +/** @} */ +GIT_END_DECL +#endif + diff --git a/include/git2/blob.h b/include/git2/blob.h index 0c0f3e580f6..0ed168555ec 100644 --- a/include/git2/blob.h +++ b/include/git2/blob.h @@ -11,12 +11,17 @@ #include "types.h" #include "oid.h" #include "object.h" +#include "buffer.h" /** * @file git2/blob.h - * @brief Git blob load and write routines + * @brief A blob represents a file in a git repository. * @defgroup git_blob Git blob load and write routines * @ingroup Git + * + * A blob represents a file in a git repository. This is the raw data + * as it is stored in the repository itself. Blobs may be "filtered" + * to produce the on-disk content. * @{ */ GIT_BEGIN_DECL @@ -24,15 +29,15 @@ GIT_BEGIN_DECL /** * Lookup a blob object from a repository. * - * @param blob pointer to the looked up blob + * @param[out] blob pointer to the looked up blob * @param repo the repo to use when locating the blob. * @param id identity of the blob to locate. * @return 0 or an error code */ -GIT_INLINE(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git_oid *id) -{ - return git_object_lookup((git_object **)blob, repo, id, GIT_OBJ_BLOB); -} +GIT_EXTERN(int) git_blob_lookup( + git_blob **blob, + git_repository *repo, + const git_oid *id); /** * Lookup a blob object from a repository, @@ -40,16 +45,13 @@ GIT_INLINE(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git * * @see git_object_lookup_prefix * - * @param blob pointer to the looked up blob + * @param[out] blob pointer to the looked up blob * @param repo the repo to use when locating the blob. * @param id identity of the blob to locate. * @param len the length of the short identifier * @return 0 or an error code */ -GIT_INLINE(int) git_blob_lookup_prefix(git_blob **blob, git_repository *repo, const git_oid *id, size_t len) -{ - return git_object_lookup_prefix((git_object **)blob, repo, id, len, GIT_OBJ_BLOB); -} +GIT_EXTERN(int) git_blob_lookup_prefix(git_blob **blob, git_repository *repo, const git_oid *id, size_t len); /** * Close an open blob @@ -62,11 +64,7 @@ GIT_INLINE(int) git_blob_lookup_prefix(git_blob **blob, git_repository *repo, co * * @param blob the blob to close */ - -GIT_INLINE(void) git_blob_free(git_blob *blob) -{ - git_object_free((git_object *) blob); -} +GIT_EXTERN(void) git_blob_free(git_blob *blob); /** * Get the id of a blob. @@ -74,11 +72,15 @@ GIT_INLINE(void) git_blob_free(git_blob *blob) * @param blob a previously loaded blob. * @return SHA1 hash for this blob. */ -GIT_INLINE(const git_oid *) git_blob_id(const git_blob *blob) -{ - return git_object_id((const git_object *)blob); -} +GIT_EXTERN(const git_oid *) git_blob_id(const git_blob *blob); +/** + * Get the repository that contains the blob. + * + * @param blob A previously loaded blob. + * @return Repository that contains this blob. + */ +GIT_EXTERN(git_repository *) git_blob_owner(const git_blob *blob); /** * Get a read-only buffer with the raw content of a blob. @@ -89,7 +91,7 @@ GIT_INLINE(const git_oid *) git_blob_id(const git_blob *blob) * time. * * @param blob pointer to the blob - * @return the pointer; NULL if the blob has no contents + * @return @type `unsigned char *` the pointer, or NULL on error */ GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob); @@ -97,104 +99,255 @@ GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob); * Get the size in bytes of the contents of a blob * * @param blob pointer to the blob - * @return size on bytes + * @return size in bytes */ -GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob); +GIT_EXTERN(git_object_size_t) git_blob_rawsize(const git_blob *blob); /** - * Read a file from the working folder of a repository - * and write it to the Object Database as a loose blob + * Flags to control the functionality of `git_blob_filter`. * - * @param id return the id of the written blob - * @param repo repository where the blob will be written. - * this repository cannot be bare - * @param relative_path file from which the blob will be created, - * relative to the repository's working dir - * @return 0 or an error code + * @flags */ -GIT_EXTERN(int) git_blob_create_fromworkdir(git_oid *id, git_repository *repo, const char *relative_path); +typedef enum { + /** When set, filters will not be applied to binary files. */ + GIT_BLOB_FILTER_CHECK_FOR_BINARY = (1 << 0), + + /** + * When set, filters will not load configuration from the + * system-wide `gitattributes` in `/etc` (or system equivalent). + */ + GIT_BLOB_FILTER_NO_SYSTEM_ATTRIBUTES = (1 << 1), + + /** + * When set, filters will be loaded from a `.gitattributes` file + * in the HEAD commit. + */ + GIT_BLOB_FILTER_ATTRIBUTES_FROM_HEAD = (1 << 2), + + /** + * When set, filters will be loaded from a `.gitattributes` file + * in the specified commit. + */ + GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT = (1 << 3) +} git_blob_filter_flag_t; /** - * Read a file from the filesystem and write its content - * to the Object Database as a loose blob + * The options used when applying filter options to a file. * - * @param id return the id of the written blob - * @param repo repository where the blob will be written. - * this repository can be bare or not - * @param path file from which the blob will be created - * @return 0 or an error code + * Initialize with `GIT_BLOB_FILTER_OPTIONS_INIT`. Alternatively, you can + * use `git_blob_filter_options_init`. + * + * @options[version] GIT_BLOB_FILTER_OPTIONS_VERSION + * @options[init_macro] GIT_BLOB_FILTER_OPTIONS_INIT + * @options[init_function] git_blob_filter_options_init */ -GIT_EXTERN(int) git_blob_create_fromdisk(git_oid *id, git_repository *repo, const char *path); +typedef struct { + /** Version number of the options structure. */ + int version; + /** + * Flags to control the filtering process, see `git_blob_filter_flag_t` above. + * + * @type[flags] git_blob_filter_flag_t + */ + uint32_t flags; -typedef int (*git_blob_chunk_cb)(char *content, size_t max_length, void *payload); +#ifdef GIT_DEPRECATE_HARD + /** + * Unused and reserved for ABI compatibility. + * + * @deprecated this value should not be set + */ + void *reserved; +#else + /** + * This value is unused and reserved for API compatibility. + * + * @deprecated this value should not be set + */ + git_oid *commit_id; +#endif + + /** + * The commit to load attributes from, when + * `GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT` is specified. + */ + git_oid attr_commit_id; +} git_blob_filter_options; /** - * Write a loose blob to the Object Database from a - * provider of chunks of data. + * The current version number for the `git_blob_filter_options` structure ABI. + */ +#define GIT_BLOB_FILTER_OPTIONS_VERSION 1 + +/** + * The default values for `git_blob_filter_options`. + */ +#define GIT_BLOB_FILTER_OPTIONS_INIT { \ + GIT_BLOB_FILTER_OPTIONS_VERSION, \ + GIT_BLOB_FILTER_CHECK_FOR_BINARY \ + } + +/** + * Initialize git_blob_filter_options structure * - * Provided the `hintpath` parameter is filled, its value - * will help to determine what git filters should be applied - * to the object before it can be placed to the object database. + * Initializes a `git_blob_filter_options` with default values. Equivalent + * to creating an instance with `GIT_BLOB_FILTER_OPTIONS_INIT`. * + * @param opts The `git_blob_filter_options` struct to initialize. + * @param version The struct version; pass GIT_BLOB_FILTER_OPTIONS_VERSION + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_blob_filter_options_init( + git_blob_filter_options *opts, + unsigned int version); + +/** + * Get a buffer with the filtered content of a blob. * - * The implementation of the callback has to respect the - * following rules: + * This applies filters as if the blob was being checked out to the + * working directory under the specified filename. This may apply + * CRLF filtering or other types of changes depending on the file + * attributes set for the blob and the content detected in it. * - * - `content` will have to be filled by the consumer. The maximum number - * of bytes that the buffer can accept per call is defined by the - * `max_length` parameter. Allocation and freeing of the buffer will be taken - * care of by the function. + * The output is written into a `git_buf` which the caller must dispose + * when done (via `git_buf_dispose`). * - * - The callback is expected to return the number of bytes - * that `content` have been filled with. + * If no filters need to be applied, then the `out` buffer will just + * be populated with a pointer to the raw content of the blob. In + * that case, be careful to *not* free the blob until done with the + * buffer or copy it into memory you own. * - * - When there is no more data to stream, the callback should - * return 0. This will prevent it from being invoked anymore. + * @param out The git_buf to be filled in + * @param blob Pointer to the blob + * @param as_path Path used for file attribute lookups, etc. + * @param opts Options to use for filtering the blob + * @return @type[enum] git_error_code 0 on success or an error code + */ +GIT_EXTERN(int) git_blob_filter( + git_buf *out, + git_blob *blob, + const char *as_path, + git_blob_filter_options *opts); + +/** + * Read a file from the working folder of a repository and write it + * to the object database. * - * - When an error occurs, the callback should return -1. + * @param[out] id return the id of the written blob + * @param repo repository where the blob will be written. + * this repository cannot be bare + * @param relative_path file from which the blob will be created, + * relative to the repository's working dir + * @return 0 or an error code + */ +GIT_EXTERN(int) git_blob_create_from_workdir(git_oid *id, git_repository *repo, const char *relative_path); + +/** + * Read a file from the filesystem (not necessarily inside the + * working folder of the repository) and write it to the object + * database. * + * @param[out] id return the id of the written blob + * @param repo repository where the blob will be written. + * this repository can be bare or not + * @param path file from which the blob will be created + * @return 0 or an error code + */ +GIT_EXTERN(int) git_blob_create_from_disk( + git_oid *id, + git_repository *repo, + const char *path); + +/** + * Create a stream to write a new blob into the object database. * - * @param id Return the id of the written blob + * This function may need to buffer the data on disk and will in + * general not be the right choice if you know the size of the data + * to write. If you have data in memory, use + * `git_blob_create_from_buffer()`. If you do not, but know the size of + * the contents (and don't want/need to perform filtering), use + * `git_odb_open_wstream()`. * - * @param repo repository where the blob will be written. - * This repository can be bare or not. + * Don't close this stream yourself but pass it to + * `git_blob_create_from_stream_commit()` to commit the write to the + * object db and get the object id. * - * @param hintpath if not NULL, will help selecting the filters - * to apply onto the content of the blob to be created. + * If the `hintpath` parameter is filled, it will be used to determine + * what git filters should be applied to the object before it is written + * to the object database. * - * @return GIT_SUCCESS or an error code + * @param[out] out the stream into which to write + * @param repo Repository where the blob will be written. + * This repository can be bare or not. + * @param hintpath If not NULL, will be used to select data filters + * to apply onto the content of the blob to be created. + * @return 0 or error code */ -GIT_EXTERN(int) git_blob_create_fromchunks( - git_oid *id, +GIT_EXTERN(int) git_blob_create_from_stream( + git_writestream **out, git_repository *repo, - const char *hintpath, - git_blob_chunk_cb callback, - void *payload); + const char *hintpath); + +/** + * Close the stream and finalize writing the blob to the object database. + * + * The stream will be closed and freed. + * + * @param[out] out the id of the new blob + * @param stream the stream to close + * @return 0 or an error code + */ +GIT_EXTERN(int) git_blob_create_from_stream_commit( + git_oid *out, + git_writestream *stream); /** - * Write an in-memory buffer to the ODB as a blob + * Write an in-memory buffer to the object database as a blob. * - * @param oid return the oid of the written blob - * @param repo repository where to blob will be written + * @param[out] id return the id of the written blob + * @param repo repository where the blob will be written * @param buffer data to be written into the blob * @param len length of the data * @return 0 or an error code */ -GIT_EXTERN(int) git_blob_create_frombuffer(git_oid *oid, git_repository *repo, const void *buffer, size_t len); +GIT_EXTERN(int) git_blob_create_from_buffer( + git_oid *id, git_repository *repo, const void *buffer, size_t len); /** - * Determine if the blob content is most certainly binary or not. + * Determine if the blob content is most likely binary or not. * * The heuristic used to guess if a file is binary is taken from core git: * Searching for NUL bytes and looking for a reasonable ratio of printable - * to non-printable characters among the first 4000 bytes. + * to non-printable characters among the first 8000 bytes. * * @param blob The blob which content should be analyzed + * @return @type bool 1 if the content of the blob is detected + * as binary; 0 otherwise. + */ +GIT_EXTERN(int) git_blob_is_binary(const git_blob *blob); + +/** + * Determine if the given content is most certainly binary or not; + * this is the same mechanism used by `git_blob_is_binary` but only + * looking at raw data. + * + * @param data The blob data which content should be analyzed + * @param len The length of the data * @return 1 if the content of the blob is detected * as binary; 0 otherwise. */ -GIT_EXTERN(int) git_blob_is_binary(git_blob *blob); +GIT_EXTERN(int) git_blob_data_is_binary(const char *data, size_t len); + +/** + * Create an in-memory copy of a blob. The copy must be explicitly + * free'd or it will leak. + * + * @param[out] out Pointer to store the copy of the object + * @param source Original object to copy + * @return 0. + */ +GIT_EXTERN(int) git_blob_dup(git_blob **out, git_blob *source); /** @} */ GIT_END_DECL diff --git a/include/git2/branch.h b/include/git2/branch.h index 54a1ab1186a..56d737d0fb0 100644 --- a/include/git2/branch.h +++ b/include/git2/branch.h @@ -13,9 +13,15 @@ /** * @file git2/branch.h - * @brief Git branch parsing routines + * @brief Branch creation and handling * @defgroup git_branch Git branch management * @ingroup Git + * + * A branch is a specific type of reference, at any particular time, + * a git working directory typically is said to have a branch "checked out", + * meaning that commits that are created will be made "on" a branch. + * This occurs by updating the branch reference to point to the new + * commit. The checked out branch is indicated by the `HEAD` meta-ref. * @{ */ GIT_BEGIN_DECL @@ -33,66 +39,95 @@ GIT_BEGIN_DECL * See `git_tag_create()` for rules about valid names. * * @param out Pointer where to store the underlying reference. - * + * @param repo the repository to create the branch in. * @param branch_name Name for the branch; this name is - * validated for consistency. It should also not conflict with - * an already existing branch name. - * - * @param target Object to which this branch should point. This object - * must belong to the given `repo` and can either be a git_commit or a - * git_tag. When a git_tag is being passed, it should be dereferencable - * to a git_commit which oid will be used as the target of the branch. - * + * validated for consistency. It should also not conflict with + * an already existing branch name. + * @param target Commit to which this branch should point. This object + * must belong to the given `repo`. * @param force Overwrite existing branch. - * * @return 0, GIT_EINVALIDSPEC or an error code. * A proper reference is written in the refs/heads namespace * pointing to the provided target commit. */ GIT_EXTERN(int) git_branch_create( - git_reference **out, - git_repository *repo, - const char *branch_name, - const git_commit *target, - int force); + git_reference **out, + git_repository *repo, + const char *branch_name, + const git_commit *target, + int force); + +/** + * Create a new branch pointing at a target commit + * + * This behaves like `git_branch_create()` but takes an annotated + * commit, which lets you specify which extended sha syntax string was + * specified by a user, allowing for more exact reflog messages. + * + * @param ref_out Pointer where to store the underlying reference. + * @param repo the repository to create the branch in. + * @param branch_name Name for the branch; this name is + * validated for consistency. It should also not conflict with + * an already existing branch name. + * @param target Annotated commit to which this branch should point. This + * object must belong to the given `repo`. + * @param force Overwrite existing branch. + * @return 0, GIT_EINVALIDSPEC or an error code. + */ +GIT_EXTERN(int) git_branch_create_from_annotated( + git_reference **ref_out, + git_repository *repo, + const char *branch_name, + const git_annotated_commit *target, + int force); /** * Delete an existing branch reference. * - * If the branch is successfully deleted, the passed reference - * object will be freed and invalidated. + * Note that if the deletion succeeds, the reference object will not + * be valid anymore, and should be freed immediately by the user using + * `git_reference_free()`. * * @param branch A valid reference representing a branch * @return 0 on success, or an error code. */ GIT_EXTERN(int) git_branch_delete(git_reference *branch); +/** Iterator type for branches */ +typedef struct git_branch_iterator git_branch_iterator; + /** - * Loop over all the branches and issue a callback for each one. - * - * If the callback returns a non-zero value, this will stop looping. + * Create an iterator which loops over the requested branches. * + * @param out the iterator * @param repo Repository where to find the branches. - * * @param list_flags Filtering flags for the branch * listing. Valid values are GIT_BRANCH_LOCAL, GIT_BRANCH_REMOTE - * or a combination of the two. + * or GIT_BRANCH_ALL. * - * @param branch_cb Callback to invoke per found branch. + * @return 0 on success or an error code + */ +GIT_EXTERN(int) git_branch_iterator_new( + git_branch_iterator **out, + git_repository *repo, + git_branch_t list_flags); + +/** + * Retrieve the next branch from the iterator * - * @param payload Extra parameter to callback function. + * @param out the reference + * @param out_type the type of branch (local or remote-tracking) + * @param iter the branch iterator + * @return 0 on success, GIT_ITEROVER if there are no more branches or an error code. + */ +GIT_EXTERN(int) git_branch_next(git_reference **out, git_branch_t *out_type, git_branch_iterator *iter); + +/** + * Free a branch iterator * - * @return 0 on success, GIT_EUSER on non-zero callback, or error code + * @param iter the iterator to free */ -GIT_EXTERN(int) git_branch_foreach( - git_repository *repo, - unsigned int list_flags, - int (*branch_cb)( - const char *branch_name, - git_branch_t branch_type, - void *payload), - void *payload -); +GIT_EXTERN(void) git_branch_iterator_free(git_branch_iterator *iter); /** * Move/rename an existing local branch reference. @@ -100,6 +135,12 @@ GIT_EXTERN(int) git_branch_foreach( * The new branch name will be checked for validity. * See `git_tag_create()` for rules about valid names. * + * Note that if the move succeeds, the old reference object will not + * be valid anymore, and should be freed immediately by the user using + * `git_reference_free()`. + * + * @param out New reference object for the updated name. + * * @param branch Current underlying reference of the branch. * * @param new_branch_name Target name of the branch once the move @@ -110,25 +151,23 @@ GIT_EXTERN(int) git_branch_foreach( * @return 0 on success, GIT_EINVALIDSPEC or an error code. */ GIT_EXTERN(int) git_branch_move( - git_reference *branch, - const char *new_branch_name, - int force); + git_reference **out, + git_reference *branch, + const char *new_branch_name, + int force); /** * Lookup a branch by its name in a repository. * * The generated reference must be freed by the user. - * * The branch name will be checked for validity. - * See `git_tag_create()` for rules about valid names. * - * @param out pointer to the looked-up branch reference + * @see git_tag_create for rules about valid names. * + * @param out pointer to the looked-up branch reference * @param repo the repository to look up the branch - * * @param branch_name Name of the branch to be looked-up; * this name is validated for consistency. - * * @param branch_type Type of the considered branch. This should * be valued with either GIT_BRANCH_LOCAL or GIT_BRANCH_REMOTE. * @@ -136,79 +175,168 @@ GIT_EXTERN(int) git_branch_move( * exists, GIT_EINVALIDSPEC, otherwise an error code. */ GIT_EXTERN(int) git_branch_lookup( - git_reference **out, - git_repository *repo, - const char *branch_name, - git_branch_t branch_type); + git_reference **out, + git_repository *repo, + const char *branch_name, + git_branch_t branch_type); /** - * Return the name of the given local or remote branch. + * Get the branch name * - * The name of the branch matches the definition of the name - * for git_branch_lookup. That is, if the returned name is given - * to git_branch_lookup() then the reference is returned that - * was given to this function. + * Given a reference object, this will check that it really is a branch (ie. + * it lives under "refs/heads/" or "refs/remotes/"), and return the branch part + * of it. * - * @param out where the pointer of branch name is stored; - * this is valid as long as the ref is not freed. - * @param ref the reference ideally pointing to a branch + * @param out Pointer to the abbreviated reference name. + * Owned by ref, do not free. * - * @return 0 on success; otherwise an error code (e.g., if the - * ref is no local or remote branch). + * @param ref A reference object, ideally pointing to a branch + * + * @return 0 on success; GIT_EINVALID if the reference isn't either a local or + * remote branch, otherwise an error code. */ -GIT_EXTERN(int) git_branch_name(const char **out, - git_reference *ref); +GIT_EXTERN(int) git_branch_name( + const char **out, + const git_reference *ref); /** - * Return the reference supporting the remote tracking branch, - * given a local branch reference. + * Get the upstream of a branch + * + * Given a reference, this will return a new reference object corresponding + * to its remote tracking branch. The reference must be a local branch. * - * @param out Pointer where to store the retrieved - * reference. + * @see git_branch_upstream_name for details on the resolution. * + * @param out Pointer where to store the retrieved reference. * @param branch Current underlying reference of the branch. * * @return 0 on success; GIT_ENOTFOUND when no remote tracking - * reference exists, otherwise an error code. + * reference exists, otherwise an error code. */ -GIT_EXTERN(int) git_branch_tracking( - git_reference **out, - git_reference *branch); +GIT_EXTERN(int) git_branch_upstream( + git_reference **out, + const git_reference *branch); /** - * Return the name of the reference supporting the remote tracking branch, - * given the name of a local branch reference. + * Set a branch's upstream branch * - * @param tracking_branch_name_out The user-allocated buffer which will be - * filled with the name of the reference. Pass NULL if you just want to - * get the needed size of the name of the reference as the output value. + * This will update the configuration to set the branch named `branch_name` as the upstream of `branch`. + * Pass a NULL name to unset the upstream information. * - * @param buffer_size Size of the `out` buffer in bytes. + * @note the actual tracking reference must have been already created for the + * operation to succeed. * - * @param repo the repository where the branches live + * @param branch the branch to configure + * @param branch_name remote-tracking or local branch to set as upstream. * - * @param canonical_branch_name name of the local branch. + * @return @type git_error_t 0 on success; GIT_ENOTFOUND if there's no branch named `branch_name` + * or an error code + */ +GIT_EXTERN(int) git_branch_set_upstream( + git_reference *branch, + const char *branch_name); + +/** + * Get the upstream name of a branch * - * @return number of characters in the reference name - * including the trailing NUL byte; GIT_ENOTFOUND when no remote tracking - * reference exists, otherwise an error code. + * Given a local branch, this will return its remote-tracking branch information, + * as a full reference name, ie. "feature/nice" would become + * "refs/remote/origin/feature/nice", depending on that branch's configuration. + * + * @param out the buffer into which the name will be written. + * @param repo the repository where the branches live. + * @param refname reference name of the local branch. + * + * @return 0 on success, GIT_ENOTFOUND when no remote tracking reference exists, + * or an error code. */ -GIT_EXTERN(int) git_branch_tracking_name( - char *tracking_branch_name_out, - size_t buffer_size, +GIT_EXTERN(int) git_branch_upstream_name( + git_buf *out, git_repository *repo, - const char *canonical_branch_name); + const char *refname); /** - * Determine if the current local branch is pointed at by HEAD. + * Determine if HEAD points to the given branch * - * @param branch Current underlying reference of the branch. + * @param branch A reference to a local branch. * - * @return 1 if HEAD points at the branch, 0 if it isn't, - * error code otherwise. + * @return 1 if HEAD points at the branch, 0 if it isn't, or a negative value + * as an error code. */ GIT_EXTERN(int) git_branch_is_head( - git_reference *branch); + const git_reference *branch); + +/** + * Determine if any HEAD points to the current branch + * + * This will iterate over all known linked repositories (usually in the form of + * worktrees) and report whether any HEAD is pointing at the current branch. + * + * @param branch A reference to a local branch. + * + * @return 1 if branch is checked out, 0 if it isn't, an error code otherwise. + */ +GIT_EXTERN(int) git_branch_is_checked_out( + const git_reference *branch); + +/** + * Find the remote name of a remote-tracking branch + * + * This will return the name of the remote whose fetch refspec is matching + * the given branch. E.g. given a branch "refs/remotes/test/master", it will + * extract the "test" part. If refspecs from multiple remotes match, + * the function will return GIT_EAMBIGUOUS. + * + * @param out The buffer into which the name will be written. + * @param repo The repository where the branch lives. + * @param refname complete name of the remote tracking branch. + * + * @return 0 on success, GIT_ENOTFOUND when no matching remote was found, + * GIT_EAMBIGUOUS when the branch maps to several remotes, + * otherwise an error code. + */ +GIT_EXTERN(int) git_branch_remote_name( + git_buf *out, + git_repository *repo, + const char *refname); + +/** + * Retrieve the upstream remote of a local branch + * + * This will return the currently configured "branch.*.remote" for a given + * branch. This branch must be local. + * + * @param buf the buffer into which to write the name + * @param repo the repository in which to look + * @param refname the full name of the branch + * @return 0 or an error code + */ + GIT_EXTERN(int) git_branch_upstream_remote(git_buf *buf, git_repository *repo, const char *refname); + +/** + * Retrieve the upstream merge of a local branch + * + * This will return the currently configured "branch.*.merge" for a given + * branch. This branch must be local. + * + * @param buf the buffer into which to write the name + * @param repo the repository in which to look + * @param refname the full name of the branch + * @return 0 or an error code + */ + GIT_EXTERN(int) git_branch_upstream_merge(git_buf *buf, git_repository *repo, const char *refname); + +/** + * Determine whether a branch name is valid, meaning that (when prefixed + * with `refs/heads/`) that it is a valid reference name, and that any + * additional branch name restrictions are imposed (eg, it cannot start + * with a `-`). + * + * @param valid output pointer to set with validity of given branch name + * @param name a branch name to test + * @return 0 on success or an error code + */ +GIT_EXTERN(int) git_branch_name_is_valid(int *valid, const char *name); /** @} */ GIT_END_DECL diff --git a/include/git2/buffer.h b/include/git2/buffer.h new file mode 100644 index 00000000000..3fe4f854857 --- /dev/null +++ b/include/git2/buffer.h @@ -0,0 +1,76 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_buf_h__ +#define INCLUDE_git_buf_h__ + +#include "common.h" + +/** + * @file git2/buffer.h + * @brief A data structure to return data to callers + * @ingroup Git + * + * The `git_buf` buffer is used to return arbitrary data - typically + * strings - to callers. Callers are responsible for freeing the memory + * in a buffer with the `git_buf_dispose` function. + * @{ + */ +GIT_BEGIN_DECL + +/** + * A data buffer for exporting data from libgit2 + * + * Sometimes libgit2 wants to return an allocated data buffer to the + * caller and have the caller take responsibility for freeing that memory. + * To make ownership clear in these cases, libgit2 uses `git_buf` to + * return this data. Callers should use `git_buf_dispose()` to release + * the memory when they are done. + * + * A `git_buf` contains a pointer to a NUL-terminated C string, and + * the length of the string (not including the NUL terminator). + */ +typedef struct { + /** + * The buffer contents. `ptr` points to the start of the buffer + * being returned. The buffer's length (in bytes) is specified + * by the `size` member of the structure, and contains a NUL + * terminator at position `(size + 1)`. + */ + char *ptr; + + /** + * This field is reserved and unused. + */ + size_t reserved; + + /** + * The length (in bytes) of the buffer pointed to by `ptr`, + * not including a NUL terminator. + */ + size_t size; +} git_buf; + +/** + * Use to initialize a `git_buf` before passing it to a function that + * will populate it. + */ +#define GIT_BUF_INIT { NULL, 0, 0 } + +/** + * Free the memory referred to by the git_buf. + * + * Note that this does not free the `git_buf` itself, just the memory + * pointed to by `buffer->ptr`. + * + * @param buffer The buffer to deallocate + */ +GIT_EXTERN(void) git_buf_dispose(git_buf *buffer); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/cert.h b/include/git2/cert.h new file mode 100644 index 00000000000..7b91b638d4f --- /dev/null +++ b/include/git2/cert.h @@ -0,0 +1,173 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_cert_h__ +#define INCLUDE_git_cert_h__ + +#include "common.h" +#include "types.h" + +/** + * @file git2/cert.h + * @brief TLS and SSH certificate handling + * @defgroup git_cert Certificate objects + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Type of host certificate structure that is passed to the check callback + */ +typedef enum git_cert_t { + /** + * No information about the certificate is available. This may + * happen when using curl. + */ + GIT_CERT_NONE, + /** + * The `data` argument to the callback will be a pointer to + * the DER-encoded data. + */ + GIT_CERT_X509, + /** + * The `data` argument to the callback will be a pointer to a + * `git_cert_hostkey` structure. + */ + GIT_CERT_HOSTKEY_LIBSSH2, + /** + * The `data` argument to the callback will be a pointer to a + * `git_strarray` with `name:content` strings containing + * information about the certificate. This is used when using + * curl. + */ + GIT_CERT_STRARRAY +} git_cert_t; + +/** + * Parent type for `git_cert_hostkey` and `git_cert_x509`. + */ +struct git_cert { + /** + * Type of certificate. A `GIT_CERT_` value. + */ + git_cert_t cert_type; +}; + +/** + * Callback for the user's custom certificate checks. + * + * @param cert The host certificate + * @param valid Whether the libgit2 checks (OpenSSL or WinHTTP) think + * this certificate is valid + * @param host Hostname of the host libgit2 connected to + * @param payload Payload provided by the caller + * @return 0 to proceed with the connection, < 0 to fail the connection + * or > 0 to indicate that the callback refused to act and that + * the existing validity determination should be honored + */ +typedef int GIT_CALLBACK(git_transport_certificate_check_cb)(git_cert *cert, int valid, const char *host, void *payload); + +/** + * Type of SSH host fingerprint + */ +typedef enum { + /** MD5 is available */ + GIT_CERT_SSH_MD5 = (1 << 0), + /** SHA-1 is available */ + GIT_CERT_SSH_SHA1 = (1 << 1), + /** SHA-256 is available */ + GIT_CERT_SSH_SHA256 = (1 << 2), + /** Raw hostkey is available */ + GIT_CERT_SSH_RAW = (1 << 3) +} git_cert_ssh_t; + +typedef enum { + /** The raw key is of an unknown type. */ + GIT_CERT_SSH_RAW_TYPE_UNKNOWN = 0, + /** The raw key is an RSA key. */ + GIT_CERT_SSH_RAW_TYPE_RSA = 1, + /** The raw key is a DSS key. */ + GIT_CERT_SSH_RAW_TYPE_DSS = 2, + /** The raw key is a ECDSA 256 key. */ + GIT_CERT_SSH_RAW_TYPE_KEY_ECDSA_256 = 3, + /** The raw key is a ECDSA 384 key. */ + GIT_CERT_SSH_RAW_TYPE_KEY_ECDSA_384 = 4, + /** The raw key is a ECDSA 521 key. */ + GIT_CERT_SSH_RAW_TYPE_KEY_ECDSA_521 = 5, + /** The raw key is a ED25519 key. */ + GIT_CERT_SSH_RAW_TYPE_KEY_ED25519 = 6 +} git_cert_ssh_raw_type_t; + +/** + * Hostkey information taken from libssh2 + */ +typedef struct { + git_cert parent; /**< The parent cert */ + + /** + * A bitmask containing the available fields. + */ + git_cert_ssh_t type; + + /** + * Hostkey hash. If `type` has `GIT_CERT_SSH_MD5` set, this will + * have the MD5 hash of the hostkey. + */ + unsigned char hash_md5[16]; + + /** + * Hostkey hash. If `type` has `GIT_CERT_SSH_SHA1` set, this will + * have the SHA-1 hash of the hostkey. + */ + unsigned char hash_sha1[20]; + + /** + * Hostkey hash. If `type` has `GIT_CERT_SSH_SHA256` set, this will + * have the SHA-256 hash of the hostkey. + */ + unsigned char hash_sha256[32]; + + /** + * Raw hostkey type. If `type` has `GIT_CERT_SSH_RAW` set, this will + * have the type of the raw hostkey. + */ + git_cert_ssh_raw_type_t raw_type; + + /** + * Pointer to the raw hostkey. If `type` has `GIT_CERT_SSH_RAW` set, + * this will have the raw contents of the hostkey. + */ + const char *hostkey; + + /** + * Raw hostkey length. If `type` has `GIT_CERT_SSH_RAW` set, this will + * have the length of the raw contents of the hostkey. + */ + size_t hostkey_len; +} git_cert_hostkey; + +/** + * X.509 certificate information + */ +typedef struct { + git_cert parent; /**< The parent cert */ + + /** + * Pointer to the X.509 certificate data + */ + void *data; + + /** + * Length of the memory block pointed to by `data`. + */ + size_t len; +} git_cert_x509; + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/checkout.h b/include/git2/checkout.h index d3e971b434c..bdea928459a 100644 --- a/include/git2/checkout.h +++ b/include/git2/checkout.h @@ -13,9 +13,13 @@ /** * @file git2/checkout.h - * @brief Git checkout routines + * @brief Update the contents of the working directory * @defgroup git_checkout Git checkout routines * @ingroup Git + * + * Update the contents of the working directory, or a subset of the + * files in the working directory, to point to the data in the index + * or a specific commit. * @{ */ GIT_BEGIN_DECL @@ -31,50 +35,44 @@ GIT_BEGIN_DECL * check out, the "baseline" tree of what was checked out previously, the * working directory for actual files, and the index for staged changes. * - * You give checkout one of four strategies for update: - * - * - `GIT_CHECKOUT_NONE` is a dry-run strategy that checks for conflicts, - * etc., but doesn't make any actual changes. - * - * - `GIT_CHECKOUT_FORCE` is at the opposite extreme, taking any action to - * make the working directory match the target (including potentially - * discarding modified files). - * - * In between those are `GIT_CHECKOUT_SAFE` and `GIT_CHECKOUT_SAFE_CREATE` - * both of which only make modifications that will not lose changes. + * You give checkout one of two strategies for update: * - * | target == baseline | target != baseline | - * ---------------------|-----------------------|----------------------| - * workdir == baseline | no action | create, update, or | - * | | delete file | - * ---------------------|-----------------------|----------------------| - * workdir exists and | no action | conflict (notify | - * is != baseline | notify dirty MODIFIED | and cancel checkout) | - * ---------------------|-----------------------|----------------------| - * workdir missing, | create if SAFE_CREATE | create file | - * baseline present | notify dirty DELETED | | - * ---------------------|-----------------------|----------------------| + * - `GIT_CHECKOUT_SAFE` is the default, and similar to git's default, + * which will make modifications that will not lose changes in the + * working directory. * - * The only difference between SAFE and SAFE_CREATE is that SAFE_CREATE - * will cause a file to be checked out if it is missing from the working - * directory even if it is not modified between the target and baseline. + * | target == baseline | target != baseline | + * ---------------------|-----------------------|----------------------| + * workdir == baseline | no action | create, update, or | + * | | delete file | + * ---------------------|-----------------------|----------------------| + * workdir exists and | no action | conflict (notify | + * is != baseline | notify dirty MODIFIED | and cancel checkout) | + * ---------------------|-----------------------|----------------------| + * workdir missing, | notify dirty DELETED | create file | + * baseline present | | | + * ---------------------|-----------------------|----------------------| * + * - `GIT_CHECKOUT_FORCE` will take any action to make the working + * directory match the target (including potentially discarding + * modified files). * * To emulate `git checkout`, use `GIT_CHECKOUT_SAFE` with a checkout * notification callback (see below) that displays information about dirty * files. The default behavior will cancel checkout on conflicts. * - * To emulate `git checkout-index`, use `GIT_CHECKOUT_SAFE_CREATE` with a + * To emulate `git checkout-index`, use `GIT_CHECKOUT_SAFE` with a * notification callback that cancels the operation if a dirty-but-existing * file is found in the working directory. This core git command isn't * quite "force" but is sensitive about some types of changes. * * To emulate `git checkout -f`, use `GIT_CHECKOUT_FORCE`. * - * To emulate `git clone` use `GIT_CHECKOUT_SAFE_CREATE` in the options. * + * There are some additional flags to modify the behavior of checkout: * - * There are some additional flags to modified the behavior of checkout: + * - `GIT_CHECKOUT_DRY_RUN` is a dry-run strategy that checks for conflicts, + * etc., but doesn't make any actual changes. * * - GIT_CHECKOUT_ALLOW_CONFLICTS makes SAFE mode apply safe file updates * even if there are conflicts (instead of cancelling the checkout). @@ -99,19 +97,36 @@ GIT_BEGIN_DECL * files with unmerged index entries instead. GIT_CHECKOUT_USE_OURS and * GIT_CHECKOUT_USE_THEIRS to proceed with the checkout using either the * stage 2 ("ours") or stage 3 ("theirs") version of files in the index. + * + * - GIT_CHECKOUT_DONT_OVERWRITE_IGNORED prevents ignored files from being + * overwritten. Normally, files that are ignored in the working directory + * are not considered "precious" and may be overwritten if the checkout + * target contains that file. + * + * - GIT_CHECKOUT_DONT_REMOVE_EXISTING prevents checkout from removing + * files or folders that fold to the same name on case insensitive + * filesystems. This can cause files to retain their existing names + * and write through existing symbolic links. + * + * @flags */ typedef enum { - GIT_CHECKOUT_NONE = 0, /** default is a dry run, no actual updates */ - - /** Allow safe updates that cannot overwrite uncommitted data */ - GIT_CHECKOUT_SAFE = (1u << 0), - - /** Allow safe updates plus creation of missing files */ - GIT_CHECKOUT_SAFE_CREATE = (1u << 1), + /** + * Allow safe updates that cannot overwrite uncommitted data. + * If the uncommitted changes don't conflict with the checked + * out files, the checkout will still proceed, leaving the + * changes intact. + */ + GIT_CHECKOUT_SAFE = 0, - /** Allow all updates to force working directory to look like index */ - GIT_CHECKOUT_FORCE = (1u << 2), + /** + * Allow all updates to force working directory to look like + * the index, potentially losing data in the process. + */ + GIT_CHECKOUT_FORCE = (1u << 1), + /** Allow checkout to recreate missing files */ + GIT_CHECKOUT_RECREATE_MISSING = (1u << 2), /** Allow checkout to make safe updates even if conflicts are found */ GIT_CHECKOUT_ALLOW_CONFLICTS = (1u << 4), @@ -125,31 +140,69 @@ typedef enum { /** Only update existing files, don't create new ones */ GIT_CHECKOUT_UPDATE_ONLY = (1u << 7), - /** Normally checkout updates index entries as it goes; this stops that */ + /** + * Normally checkout updates index entries as it goes; this stops that. + * Implies `GIT_CHECKOUT_DONT_WRITE_INDEX`. + */ GIT_CHECKOUT_DONT_UPDATE_INDEX = (1u << 8), /** Don't refresh index/config/etc before doing checkout */ GIT_CHECKOUT_NO_REFRESH = (1u << 9), + /** Allow checkout to skip unmerged files */ + GIT_CHECKOUT_SKIP_UNMERGED = (1u << 10), + /** For unmerged files, checkout stage 2 from index */ + GIT_CHECKOUT_USE_OURS = (1u << 11), + /** For unmerged files, checkout stage 3 from index */ + GIT_CHECKOUT_USE_THEIRS = (1u << 12), + /** Treat pathspec as simple list of exact match file paths */ GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH = (1u << 13), + /** Ignore directories in use, they will be left empty */ + GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES = (1u << 18), + + /** Don't overwrite ignored files that exist in the checkout target */ + GIT_CHECKOUT_DONT_OVERWRITE_IGNORED = (1u << 19), + + /** Write normal merge files for conflicts */ + GIT_CHECKOUT_CONFLICT_STYLE_MERGE = (1u << 20), + + /** Include common ancestor data in diff3 format files for conflicts */ + GIT_CHECKOUT_CONFLICT_STYLE_DIFF3 = (1u << 21), + + /** Don't overwrite existing files or folders */ + GIT_CHECKOUT_DONT_REMOVE_EXISTING = (1u << 22), + + /** Normally checkout writes the index upon completion; this prevents that. */ + GIT_CHECKOUT_DONT_WRITE_INDEX = (1u << 23), + /** - * THE FOLLOWING OPTIONS ARE NOT YET IMPLEMENTED + * Perform a "dry run", reporting what _would_ be done but + * without actually making changes in the working directory + * or the index. */ + GIT_CHECKOUT_DRY_RUN = (1u << 24), - /** Allow checkout to skip unmerged files (NOT IMPLEMENTED) */ - GIT_CHECKOUT_SKIP_UNMERGED = (1u << 10), - /** For unmerged files, checkout stage 2 from index (NOT IMPLEMENTED) */ - GIT_CHECKOUT_USE_OURS = (1u << 11), - /** For unmerged files, checkout stage 3 from index (NOT IMPLEMENTED) */ - GIT_CHECKOUT_USE_THEIRS = (1u << 12), + /** Include common ancestor data in zdiff3 format for conflicts */ + GIT_CHECKOUT_CONFLICT_STYLE_ZDIFF3 = (1u << 25), + + /** + * Do not do a checkout and do not fire callbacks; this is primarily + * useful only for internal functions that will perform the + * checkout themselves but need to pass checkout options into + * another function, for example, `git_clone`. + */ + GIT_CHECKOUT_NONE = (1u << 30), + + /* + * THE FOLLOWING OPTIONS ARE NOT YET IMPLEMENTED + */ /** Recursively checkout submodules with same options (NOT IMPLEMENTED) */ GIT_CHECKOUT_UPDATE_SUBMODULES = (1u << 16), /** Recursively checkout submodules if HEAD moved in super repo (NOT IMPLEMENTED) */ - GIT_CHECKOUT_UPDATE_SUBMODULES_IF_CHANGED = (1u << 17), - + GIT_CHECKOUT_UPDATE_SUBMODULES_IF_CHANGED = (1u << 17) } git_checkout_strategy_t; /** @@ -158,32 +211,68 @@ typedef enum { * Checkout will invoke an options notification callback (`notify_cb`) for * certain cases - you pick which ones via `notify_flags`: * - * - GIT_CHECKOUT_NOTIFY_CONFLICT invokes checkout on conflicting paths. - * - * - GIT_CHECKOUT_NOTIFY_DIRTY notifies about "dirty" files, i.e. those that - * do not need an update but no longer match the baseline. Core git - * displays these files when checkout runs, but won't stop the checkout. - * - * - GIT_CHECKOUT_NOTIFY_UPDATED sends notification for any file changed. - * - * - GIT_CHECKOUT_NOTIFY_UNTRACKED notifies about untracked files. + * Returning a non-zero value from this callback will cancel the checkout. + * The non-zero return value will be propagated back and returned by the + * git_checkout_... call. * - * - GIT_CHECKOUT_NOTIFY_IGNORED notifies about ignored files. + * Notification callbacks are made prior to modifying any files on disk, + * so canceling on any notification will still happen prior to any files + * being modified. * - * Returning a non-zero value from this callback will cancel the checkout. - * Notification callbacks are made prior to modifying any files on disk. + * @flags */ typedef enum { GIT_CHECKOUT_NOTIFY_NONE = 0, + + /** + * Invokes checkout on conflicting paths. + */ GIT_CHECKOUT_NOTIFY_CONFLICT = (1u << 0), + + /** + * Notifies about "dirty" files, i.e. those that do not need an update + * but no longer match the baseline. Core git displays these files when + * checkout runs, but won't stop the checkout. + */ GIT_CHECKOUT_NOTIFY_DIRTY = (1u << 1), + + /** + * Sends notification for any file changed. + */ GIT_CHECKOUT_NOTIFY_UPDATED = (1u << 2), + + /** + * Notifies about untracked files. + */ GIT_CHECKOUT_NOTIFY_UNTRACKED = (1u << 3), + + /** + * Notifies about ignored files. + */ GIT_CHECKOUT_NOTIFY_IGNORED = (1u << 4), + + GIT_CHECKOUT_NOTIFY_ALL = 0x0FFFFu } git_checkout_notify_t; -/** Checkout notification callback function */ -typedef int (*git_checkout_notify_cb)( +/** Checkout performance-reporting structure */ +typedef struct { + size_t mkdir_calls; + size_t stat_calls; + size_t chmod_calls; +} git_checkout_perfdata; + +/** + * Checkout notification callback function. + * + * @param why the notification reason + * @param path the path to the file being checked out + * @param baseline the baseline's diff file information + * @param target the checkout target diff file information + * @param workdir the working directory diff file information + * @param payload the user-supplied callback payload + * @return 0 on success, or an error code + */ +typedef int GIT_CALLBACK(git_checkout_notify_cb)( git_checkout_notify_t why, const char *path, const git_diff_file *baseline, @@ -191,64 +280,157 @@ typedef int (*git_checkout_notify_cb)( const git_diff_file *workdir, void *payload); -/** Checkout progress notification function */ -typedef void (*git_checkout_progress_cb)( +/** + * Checkout progress notification function. + * + * @param path the path to the file being checked out + * @param completed_steps number of checkout steps completed + * @param total_steps number of total steps in the checkout process + * @param payload the user-supplied callback payload + */ +typedef void GIT_CALLBACK(git_checkout_progress_cb)( const char *path, size_t completed_steps, size_t total_steps, void *payload); +/** + * Checkout performance data reporting function. + * + * @param perfdata the performance data for the checkout + * @param payload the user-supplied callback payload + */ +typedef void GIT_CALLBACK(git_checkout_perfdata_cb)( + const git_checkout_perfdata *perfdata, + void *payload); + /** * Checkout options structure * - * Zero out for defaults. Initialize with `GIT_CHECKOUT_OPTS_INIT` macro to - * correctly set the `version` field. E.g. + * Initialize with `GIT_CHECKOUT_OPTIONS_INIT`. Alternatively, you can + * use `git_checkout_options_init`. * - * git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; + * @options[version] GIT_CHECKOUT_OPTIONS_VERSION + * @options[init_macro] GIT_CHECKOUT_OPTIONS_INIT + * @options[init_function] git_checkout_options_init */ -typedef struct git_checkout_opts { - unsigned int version; +typedef struct git_checkout_options { + unsigned int version; /**< The version */ + + /** + * Checkout strategy. Default is a safe checkout. + * + * @type[flags] git_checkout_strategy_t + */ + unsigned int checkout_strategy; /**< default will be a safe checkout */ - unsigned int checkout_strategy; /** default will be a dry run */ + int disable_filters; /**< don't apply filters like CRLF conversion */ + unsigned int dir_mode; /**< default is 0755 */ + unsigned int file_mode; /**< default is 0644 or 0755 as dictated by blob */ + int file_open_flags; /**< default is O_CREAT | O_TRUNC | O_WRONLY */ - int disable_filters; /** don't apply filters like CRLF conversion */ - unsigned int dir_mode; /** default is 0755 */ - unsigned int file_mode; /** default is 0644 or 0755 as dictated by blob */ - int file_open_flags; /** default is O_CREAT | O_TRUNC | O_WRONLY */ + /** + * Checkout notification flags specify what operations the notify + * callback is invoked for. + * + * @type[flags] git_checkout_notify_t + */ + unsigned int notify_flags; - unsigned int notify_flags; /** see `git_checkout_notify_t` above */ + /** + * Optional callback to get notifications on specific file states. + * @see git_checkout_notify_t + */ git_checkout_notify_cb notify_cb; + + /** Payload passed to notify_cb */ void *notify_payload; - /* Optional callback to notify the consumer of checkout progress. */ + /** Optional callback to notify the consumer of checkout progress. */ git_checkout_progress_cb progress_cb; + + /** Payload passed to progress_cb */ void *progress_payload; - /** When not zeroed out, array of fnmatch patterns specifying which - * paths should be taken into account, otherwise all files. Use - * GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH to treat as simple list. + /** + * A list of wildmatch patterns or paths. + * + * By default, all paths are processed. If you pass an array of wildmatch + * patterns, those will be used to filter which paths should be taken into + * account. + * + * Use GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH to treat as a simple list. */ git_strarray paths; - git_tree *baseline; /** expected content of workdir, defaults to HEAD */ -} git_checkout_opts; + /** + * The expected content of the working directory; defaults to HEAD. + * + * If the working directory does not match this baseline information, + * that will produce a checkout conflict. + */ + git_tree *baseline; + + /** + * Like `baseline` above, though expressed as an index. This + * option overrides `baseline`. + */ + git_index *baseline_index; + + const char *target_directory; /**< alternative checkout path to workdir */ -#define GIT_CHECKOUT_OPTS_VERSION 1 -#define GIT_CHECKOUT_OPTS_INIT {GIT_CHECKOUT_OPTS_VERSION} + const char *ancestor_label; /**< the name of the common ancestor side of conflicts */ + const char *our_label; /**< the name of the "our" side of conflicts */ + const char *their_label; /**< the name of the "their" side of conflicts */ + + /** Optional callback to notify the consumer of performance data. */ + git_checkout_perfdata_cb perfdata_cb; + + /** Payload passed to perfdata_cb */ + void *perfdata_payload; +} git_checkout_options; + + +/** Current version for the `git_checkout_options` structure */ +#define GIT_CHECKOUT_OPTIONS_VERSION 1 + +/** Static constructor for `git_checkout_options` */ +#define GIT_CHECKOUT_OPTIONS_INIT { GIT_CHECKOUT_OPTIONS_VERSION } + +/** + * Initialize git_checkout_options structure + * + * Initializes a `git_checkout_options` with default values. Equivalent to creating + * an instance with GIT_CHECKOUT_OPTIONS_INIT. + * + * @param opts The `git_checkout_options` struct to initialize. + * @param version The struct version; pass `GIT_CHECKOUT_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_checkout_options_init( + git_checkout_options *opts, + unsigned int version); /** * Updates files in the index and the working tree to match the content of * the commit pointed at by HEAD. * + * Note that this is _not_ the correct mechanism used to switch branches; + * do not change your `HEAD` and then call this method, that would leave + * you with checkout conflicts since your working directory would then + * appear to be dirty. Instead, checkout the target of the branch and + * then update `HEAD` using `git_repository_set_head` to point to the + * branch you checked out. + * * @param repo repository to check out (must be non-bare) * @param opts specifies checkout options (may be NULL) - * @return 0 on success, GIT_EORPHANEDHEAD when HEAD points to a non existing - * branch, GIT_ERROR otherwise (use giterr_last for information - * about the error) + * @return 0 on success, GIT_EUNBORNBRANCH if HEAD points to a non + * existing branch, non-zero value returned by `notify_cb`, or + * other error code < 0 (use git_error_last for error details) */ GIT_EXTERN(int) git_checkout_head( git_repository *repo, - git_checkout_opts *opts); + const git_checkout_options *opts); /** * Updates files in the working tree to match the content of the index. @@ -256,13 +438,13 @@ GIT_EXTERN(int) git_checkout_head( * @param repo repository into which to check out (must be non-bare) * @param index index to be checked out (or NULL to use repository index) * @param opts specifies checkout options (may be NULL) - * @return 0 on success, GIT_ERROR otherwise (use giterr_last for information - * about the error) + * @return 0 on success, non-zero return value from `notify_cb`, or error + * code < 0 (use git_error_last for error details) */ GIT_EXTERN(int) git_checkout_index( git_repository *repo, git_index *index, - git_checkout_opts *opts); + const git_checkout_options *opts); /** * Updates files in the index and working tree to match the content of the @@ -270,16 +452,17 @@ GIT_EXTERN(int) git_checkout_index( * * @param repo repository to check out (must be non-bare) * @param treeish a commit, tag or tree which content will be used to update - * the working directory + * the working directory (or NULL to use HEAD) * @param opts specifies checkout options (may be NULL) - * @return 0 on success, GIT_ERROR otherwise (use giterr_last for information - * about the error) + * @return 0 on success, non-zero return value from `notify_cb`, or error + * code < 0 (use git_error_last for error details) */ GIT_EXTERN(int) git_checkout_tree( git_repository *repo, const git_object *treeish, - git_checkout_opts *opts); + const git_checkout_options *opts); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/cherrypick.h b/include/git2/cherrypick.h new file mode 100644 index 00000000000..e6cf99ea51d --- /dev/null +++ b/include/git2/cherrypick.h @@ -0,0 +1,99 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_cherrypick_h__ +#define INCLUDE_git_cherrypick_h__ + +#include "common.h" +#include "types.h" +#include "merge.h" + +/** + * @file git2/cherrypick.h + * @brief Cherry-pick the contents of an individual commit + * @defgroup git_cherrypick Git cherry-pick routines + * @ingroup Git + * + * "Cherry-pick" will attempts to re-apply the changes in an + * individual commit to the current index and working directory. + * @{ + */ +GIT_BEGIN_DECL + +/** + * Cherry-pick options + */ +typedef struct { + unsigned int version; + + /** For merge commits, the "mainline" is treated as the parent. */ + unsigned int mainline; + + git_merge_options merge_opts; /**< Options for the merging */ + git_checkout_options checkout_opts; /**< Options for the checkout */ +} git_cherrypick_options; + +/** Current version for the `git_cherrypick_options` structure */ +#define GIT_CHERRYPICK_OPTIONS_VERSION 1 + +/** Static constructor for `git_cherrypick_options` */ +#define GIT_CHERRYPICK_OPTIONS_INIT { \ + GIT_CHERRYPICK_OPTIONS_VERSION, 0, \ + GIT_MERGE_OPTIONS_INIT, GIT_CHECKOUT_OPTIONS_INIT } + +/** + * Initialize git_cherrypick_options structure + * + * Initializes a `git_cherrypick_options` with default values. Equivalent to creating + * an instance with GIT_CHERRYPICK_OPTIONS_INIT. + * + * @param opts The `git_cherrypick_options` struct to initialize. + * @param version The struct version; pass `GIT_CHERRYPICK_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_cherrypick_options_init( + git_cherrypick_options *opts, + unsigned int version); + +/** + * Cherry-picks the given commit against the given "our" commit, producing an + * index that reflects the result of the cherry-pick. + * + * The returned index must be freed explicitly with `git_index_free`. + * + * @param out pointer to store the index result in + * @param repo the repository that contains the given commits + * @param cherrypick_commit the commit to cherry-pick + * @param our_commit the commit to cherry-pick against (eg, HEAD) + * @param mainline the parent of the `cherrypick_commit`, if it is a merge + * @param merge_options the merge options (or null for defaults) + * @return zero on success, -1 on failure. + */ +GIT_EXTERN(int) git_cherrypick_commit( + git_index **out, + git_repository *repo, + git_commit *cherrypick_commit, + git_commit *our_commit, + unsigned int mainline, + const git_merge_options *merge_options); + +/** + * Cherry-pick the given commit, producing changes in the index and working directory. + * + * @param repo the repository to cherry-pick + * @param commit the commit to cherry-pick + * @param cherrypick_options the cherry-pick options (or null for defaults) + * @return zero on success, -1 on failure. + */ +GIT_EXTERN(int) git_cherrypick( + git_repository *repo, + git_commit *commit, + const git_cherrypick_options *cherrypick_options); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/clone.h b/include/git2/clone.h index b5467687492..b7a47ab484b 100644 --- a/include/git2/clone.h +++ b/include/git2/clone.h @@ -12,96 +12,214 @@ #include "indexer.h" #include "checkout.h" #include "remote.h" +#include "transport.h" /** * @file git2/clone.h - * @brief Git cloning routines + * @brief Clone a remote repository to the local disk * @defgroup git_clone Git cloning routines * @ingroup Git + * + * Clone will take a remote repository - located on a remote server + * accessible by HTTPS or SSH, or a repository located elsewhere on + * the local disk - and place a copy in the given local path. * @{ */ GIT_BEGIN_DECL /** - * Clone options structure + * Options for bypassing the git-aware transport on clone. Bypassing + * it means that instead of a fetch, libgit2 will copy the object + * database directory instead of figuring out what it needs, which is + * faster. If possible, it will hardlink the files to save space. + */ +typedef enum { + /** + * Auto-detect (default), libgit2 will bypass the git-aware + * transport for local paths, but use a normal fetch for + * `file://` urls. + */ + GIT_CLONE_LOCAL_AUTO, + /** + * Bypass the git-aware transport even for a `file://` url. + */ + GIT_CLONE_LOCAL, + /** + * Do no bypass the git-aware transport + */ + GIT_CLONE_NO_LOCAL, + /** + * Bypass the git-aware transport, but do not try to use + * hardlinks. + */ + GIT_CLONE_LOCAL_NO_LINKS +} git_clone_local_t; + +/** + * The signature of a function matching git_remote_create, with an additional + * void* as a callback payload. * - * Use zeros to indicate default settings. It's easiest to use the - * `GIT_CLONE_OPTIONS_INIT` macro: + * Callers of git_clone may provide a function matching this signature to override + * the remote creation and customization process during a clone operation. * - * git_clone_options opts = GIT_CLONE_OPTIONS_INIT; + * @param[out] out the resulting remote + * @param repo the repository in which to create the remote + * @param name the remote's name + * @param url the remote's url + * @param payload an opaque payload + * @return 0, GIT_EINVALIDSPEC, GIT_EEXISTS or an error code + */ +typedef int GIT_CALLBACK(git_remote_create_cb)( + git_remote **out, + git_repository *repo, + const char *name, + const char *url, + void *payload); + +/** + * The signature of a function matching git_repository_init, with an + * additional void * as callback payload. * - * - `checkout_opts` is options for the checkout step. To disable checkout, - * set the `checkout_strategy` to GIT_CHECKOUT_DEFAULT. - * - `bare` should be set to zero to create a standard repo, non-zero for - * a bare repo - * - `fetch_progress_cb` is optional callback for fetch progress. Be aware that - * this is called inline with network and indexing operations, so performance - * may be affected. - * - `fetch_progress_payload` is payload for fetch_progress_cb + * Callers of git_clone my provide a function matching this signature + * to override the repository creation and customization process + * during a clone operation. * - * ** "origin" remote options: ** - * - `remote_name` is the name given to the "origin" remote. The default is - * "origin". - * - `pushurl` is a URL to be used for pushing. NULL means use the fetch url. - * - `fetch_spec` is the fetch specification to be used for fetching. NULL - * results in the same behavior as GIT_REMOTE_DEFAULT_FETCH. - * - `push_spec` is the fetch specification to be used for pushing. NULL means - * use the same spec as for fetching. - * - `cred_acquire_cb` is a callback to be used if credentials are required - * during the initial fetch. - * - `cred_acquire_payload` is the payload for the above callback. - * - `transport` is a custom transport to be used for the initial fetch. NULL - * means use the transport autodetected from the URL. - * - `remote_callbacks` may be used to specify custom progress callbacks for - * the origin remote before the fetch is initiated. - * - `remote_autotag` may be used to specify the autotag setting before the - * initial fetch. - * - `checkout_branch` gives the name of the branch to checkout. NULL means - * use the remote's HEAD. + * @param[out] out the resulting repository + * @param path path in which to create the repository + * @param bare whether the repository is bare. This is the value from the clone options + * @param payload payload specified by the options + * @return 0, or a negative value to indicate error */ +typedef int GIT_CALLBACK(git_repository_create_cb)( + git_repository **out, + const char *path, + int bare, + void *payload); +/** + * Clone options structure + * + * Initialize with `GIT_CLONE_OPTIONS_INIT`. Alternatively, you can + * use `git_clone_options_init`. + * + * @options[version] GIT_CLONE_OPTIONS_VERSION + * @options[init_macro] GIT_CLONE_OPTIONS_INIT + * @options[init_function] git_clone_options_init + */ typedef struct git_clone_options { unsigned int version; - git_checkout_opts checkout_opts; + /** + * These options are passed to the checkout step. To disable + * checkout, set the `checkout_strategy` to `GIT_CHECKOUT_NONE` + * or `GIT_CHECKOUT_DRY_RUN`. + */ + git_checkout_options checkout_opts; + + /** + * Options which control the fetch, including callbacks. + * + * The callbacks are used for reporting fetch progress, and for acquiring + * credentials in the event they are needed. + */ + git_fetch_options fetch_opts; + + /** + * Set to zero (false) to create a standard repo, or non-zero + * for a bare repo + */ int bare; - git_transfer_progress_callback fetch_progress_cb; - void *fetch_progress_payload; - - const char *remote_name; - const char *pushurl; - const char *fetch_spec; - const char *push_spec; - git_cred_acquire_cb cred_acquire_cb; - void *cred_acquire_payload; - git_transport *transport; - git_remote_callbacks *remote_callbacks; - git_remote_autotag_option_t remote_autotag; - const char* checkout_branch; + + /** + * Whether to use a fetch or copy the object database. + */ + git_clone_local_t local; + + /** + * The name of the branch to checkout. NULL means use the + * remote's default branch. + */ + const char *checkout_branch; + + /** + * A callback used to create the new repository into which to + * clone. If NULL, the 'bare' field will be used to determine + * whether to create a bare repository. + */ + git_repository_create_cb repository_cb; + + /** + * An opaque payload to pass to the git_repository creation callback. + * This parameter is ignored unless repository_cb is non-NULL. + */ + void *repository_cb_payload; + + /** + * A callback used to create the git_remote, prior to its being + * used to perform the clone operation. See the documentation for + * git_remote_create_cb for details. This parameter may be NULL, + * indicating that git_clone should provide default behavior. + */ + git_remote_create_cb remote_cb; + + /** + * An opaque payload to pass to the git_remote creation callback. + * This parameter is ignored unless remote_cb is non-NULL. + */ + void *remote_cb_payload; } git_clone_options; +/** Current version for the `git_clone_options` structure */ #define GIT_CLONE_OPTIONS_VERSION 1 -#define GIT_CLONE_OPTIONS_INIT {GIT_CLONE_OPTIONS_VERSION, {GIT_CHECKOUT_OPTS_VERSION, GIT_CHECKOUT_SAFE_CREATE}} + +/** Static constructor for `git_clone_options` */ +#define GIT_CLONE_OPTIONS_INIT \ + { GIT_CLONE_OPTIONS_VERSION, \ + GIT_CHECKOUT_OPTIONS_INIT, \ + GIT_FETCH_OPTIONS_INIT } /** - * Clone a remote repository, and checkout the branch pointed to by the remote - * HEAD. + * Initialize git_clone_options structure + * + * Initializes a `git_clone_options` with default values. Equivalent to creating + * an instance with GIT_CLONE_OPTIONS_INIT. * - * @param out pointer that will receive the resulting repository object - * @param origin_remote a remote which will act as the initial fetch source + * @param opts The `git_clone_options` struct to initialize. + * @param version The struct version; pass `GIT_CLONE_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_clone_options_init( + git_clone_options *opts, + unsigned int version); + +/** + * Clone a remote repository. + * + * By default this creates its repository and initial remote to match + * git's defaults. You can use the options in the callback to + * customize how these are created. + * + * Note that the libgit2 library _must_ be initialized using + * `git_libgit2_init` before any APIs can be called, including + * this one. + * + * @param[out] out pointer that will receive the resulting repository object + * @param url the remote repository to clone * @param local_path local directory to clone to - * @param options configuration options for the clone. If NULL, the function - * works as though GIT_OPTIONS_INIT were passed. - * @return 0 on success, GIT_ERROR otherwise (use giterr_last for information - * about the error) + * @param options configuration options for the clone. If NULL, the + * function works as though GIT_OPTIONS_INIT were passed. + * @return 0 on success, any non-zero return value from a callback + * function, or a negative value to indicate an error (use + * `git_error_last` for a detailed error message) */ GIT_EXTERN(int) git_clone( - git_repository **out, - const char *url, - const char *local_path, - const git_clone_options *options); + git_repository **out, + const char *url, + const char *local_path, + const git_clone_options *options); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/commit.h b/include/git2/commit.h index 764053eaa38..b998e188974 100644 --- a/include/git2/commit.h +++ b/include/git2/commit.h @@ -14,9 +14,13 @@ /** * @file git2/commit.h - * @brief Git commit parsing, formatting routines + * @brief A representation of a set of changes in the repository * @defgroup git_commit Git commit parsing, formatting routines * @ingroup Git + * + * A commit represents a set of changes made to the files within a + * repository, and metadata about who made the changes, and when the + * changes were made. * @{ */ GIT_BEGIN_DECL @@ -24,20 +28,24 @@ GIT_BEGIN_DECL /** * Lookup a commit object from a repository. * + * The returned object should be released with `git_commit_free` when no + * longer needed. + * * @param commit pointer to the looked up commit * @param repo the repo to use when locating the commit. * @param id identity of the commit to locate. If the object is * an annotated tag it will be peeled back to the commit. * @return 0 or an error code */ -GIT_INLINE(int) git_commit_lookup(git_commit **commit, git_repository *repo, const git_oid *id) -{ - return git_object_lookup((git_object **)commit, repo, id, GIT_OBJ_COMMIT); -} +GIT_EXTERN(int) git_commit_lookup( + git_commit **commit, git_repository *repo, const git_oid *id); /** - * Lookup a commit object from a repository, - * given a prefix of its identifier (short id). + * Lookup a commit object from a repository, given a prefix of its + * identifier (short id). + * + * The returned object should be released with `git_commit_free` when no + * longer needed. * * @see git_object_lookup_prefix * @@ -48,10 +56,8 @@ GIT_INLINE(int) git_commit_lookup(git_commit **commit, git_repository *repo, con * @param len the length of the short identifier * @return 0 or an error code */ -GIT_INLINE(int) git_commit_lookup_prefix(git_commit **commit, git_repository *repo, const git_oid *id, size_t len) -{ - return git_object_lookup_prefix((git_object **)commit, repo, id, len, GIT_OBJ_COMMIT); -} +GIT_EXTERN(int) git_commit_lookup_prefix( + git_commit **commit, git_repository *repo, const git_oid *id, size_t len); /** * Close an open commit @@ -65,10 +71,7 @@ GIT_INLINE(int) git_commit_lookup_prefix(git_commit **commit, git_repository *re * @param commit the commit to close */ -GIT_INLINE(void) git_commit_free(git_commit *commit) -{ - git_object_free((git_object *) commit); -} +GIT_EXTERN(void) git_commit_free(git_commit *commit); /** * Get the id of a commit. @@ -76,10 +79,15 @@ GIT_INLINE(void) git_commit_free(git_commit *commit) * @param commit a previously loaded commit. * @return object identity for the commit. */ -GIT_INLINE(const git_oid *) git_commit_id(const git_commit *commit) -{ - return git_object_id((const git_object *)commit); -} +GIT_EXTERN(const git_oid *) git_commit_id(const git_commit *commit); + +/** + * Get the repository that contains the commit. + * + * @param commit A previously loaded commit. + * @return Repository that contains this commit. + */ +GIT_EXTERN(git_repository *) git_commit_owner(const git_commit *commit); /** * Get the encoding for the message of a commit, @@ -96,11 +104,46 @@ GIT_EXTERN(const char *) git_commit_message_encoding(const git_commit *commit); /** * Get the full message of a commit. * + * The returned message will be slightly prettified by removing any + * potential leading newlines. + * * @param commit a previously loaded commit. * @return the message of a commit */ GIT_EXTERN(const char *) git_commit_message(const git_commit *commit); +/** + * Get the full raw message of a commit. + * + * @param commit a previously loaded commit. + * @return the raw message of a commit + */ +GIT_EXTERN(const char *) git_commit_message_raw(const git_commit *commit); + +/** + * Get the short "summary" of the git commit message. + * + * The returned message is the summary of the commit, comprising the + * first paragraph of the message with whitespace trimmed and squashed. + * + * @param commit a previously loaded commit. + * @return the summary of a commit or NULL on error + */ +GIT_EXTERN(const char *) git_commit_summary(git_commit *commit); + +/** + * Get the long "body" of the git commit message. + * + * The returned message is the body of the commit, comprising + * everything but the first paragraph of the message. Leading and + * trailing whitespaces are trimmed. + * + * @param commit a previously loaded commit. + * @return the body of a commit or NULL when no the message only + * consists of a summary + */ +GIT_EXTERN(const char *) git_commit_body(git_commit *commit); + /** * Get the commit time (i.e. committer time) of a commit. * @@ -133,6 +176,42 @@ GIT_EXTERN(const git_signature *) git_commit_committer(const git_commit *commit) */ GIT_EXTERN(const git_signature *) git_commit_author(const git_commit *commit); +/** + * Get the committer of a commit, using the mailmap to map names and email + * addresses to canonical real names and email addresses. + * + * Call `git_signature_free` to free the signature. + * + * @param out a pointer to store the resolved signature. + * @param commit a previously loaded commit. + * @param mailmap the mailmap to resolve with. (may be NULL) + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_committer_with_mailmap( + git_signature **out, const git_commit *commit, const git_mailmap *mailmap); + +/** + * Get the author of a commit, using the mailmap to map names and email + * addresses to canonical real names and email addresses. + * + * Call `git_signature_free` to free the signature. + * + * @param out a pointer to store the resolved signature. + * @param commit a previously loaded commit. + * @param mailmap the mailmap to resolve with. (may be NULL) + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_author_with_mailmap( + git_signature **out, const git_commit *commit, const git_mailmap *mailmap); + +/** + * Get the full raw text of the commit header. + * + * @param commit a previously loaded commit + * @return the header text of the commit + */ +GIT_EXTERN(const char *) git_commit_raw_header(const git_commit *commit); + /** * Get the tree pointed to by a commit. * @@ -168,7 +247,10 @@ GIT_EXTERN(unsigned int) git_commit_parentcount(const git_commit *commit); * @param n the position of the parent (from 0 to `parentcount`) * @return 0 or an error code */ -GIT_EXTERN(int) git_commit_parent(git_commit **out, git_commit *commit, unsigned int n); +GIT_EXTERN(int) git_commit_parent( + git_commit **out, + const git_commit *commit, + unsigned int n); /** * Get the oid of a specified parent for a commit. This is different from @@ -179,7 +261,9 @@ GIT_EXTERN(int) git_commit_parent(git_commit **out, git_commit *commit, unsigned * @param n the position of the parent (from 0 to `parentcount`) * @return the id of the parent, NULL on error. */ -GIT_EXTERN(const git_oid *) git_commit_parent_id(git_commit *commit, unsigned int n); +GIT_EXTERN(const git_oid *) git_commit_parent_id( + const git_commit *commit, + unsigned int n); /** * Get the commit object that is the th generation ancestor @@ -201,14 +285,44 @@ GIT_EXTERN(int) git_commit_nth_gen_ancestor( unsigned int n); /** - * Create a new commit in the repository using `git_object` - * instances as parameters. + * Get an arbitrary header field * - * The message will not be cleaned up. This can be achieved - * through `git_message_prettify()`. + * @param out the buffer to fill; existing content will be + * overwritten + * @param commit the commit to look in + * @param field the header field to return + * @return 0 on succeess, GIT_ENOTFOUND if the field does not exist, + * or an error code + */ +GIT_EXTERN(int) git_commit_header_field(git_buf *out, const git_commit *commit, const char *field); + +/** + * Extract the signature from a commit + * + * If the id is not for a commit, the error class will be + * `GIT_ERROR_INVALID`. If the commit does not have a signature, the + * error class will be `GIT_ERROR_OBJECT`. + * + * @param signature the signature block; existing content will be + * overwritten + * @param signed_data signed data; this is the commit contents minus the signature block; + * existing content will be overwritten + * @param repo the repository in which the commit exists + * @param commit_id the commit from which to extract the data + * @param field the name of the header field containing the signature + * block; pass `NULL` to extract the default 'gpgsig' + * @return 0 on success, GIT_ENOTFOUND if the id is not for a commit + * or the commit does not have a signature. + */ +GIT_EXTERN(int) git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_repository *repo, git_oid *commit_id, const char *field); + +/** + * Create new commit in the repository from a list of `git_object` pointers * - * @param id Pointer where to store the OID of the - * newly created commit + * The message will **not** be cleaned up automatically. You can do that + * with the `git_message_prettify()` function. + * + * @param id Pointer in which to store the OID of the newly created commit * * @param repo Repository where to store the commit * @@ -217,76 +331,345 @@ GIT_EXTERN(int) git_commit_nth_gen_ancestor( * is not direct, it will be resolved to a direct reference. * Use "HEAD" to update the HEAD of the current branch and * make it point to this commit. If the reference doesn't - * exist yet, it will be created. + * exist yet, it will be created. If it does exist, the first + * parent must be the tip of this branch. * - * @param author Signature representing the author and the authory - * time of this commit + * @param author Signature with author and author time of commit * - * @param committer Signature representing the committer and the - * commit time of this commit + * @param committer Signature with committer and * commit time of commit * * @param message_encoding The encoding for the message in the - * commit, represented with a standard encoding name. - * E.g. "UTF-8". If NULL, no encoding header is written and - * UTF-8 is assumed. + * commit, represented with a standard encoding name. + * E.g. "UTF-8". If NULL, no encoding header is written and + * UTF-8 is assumed. * * @param message Full message for this commit * * @param tree An instance of a `git_tree` object that will - * be used as the tree for the commit. This tree object must - * also be owned by the given `repo`. + * be used as the tree for the commit. This tree object must + * also be owned by the given `repo`. * * @param parent_count Number of parents for this commit * - * @param parents[] Array of `parent_count` pointers to `git_commit` - * objects that will be used as the parents for this commit. This - * array may be NULL if `parent_count` is 0 (root commit). All the - * given commits must be owned by the `repo`. + * @param parents Array of `parent_count` pointers to `git_commit` + * objects that will be used as the parents for this commit. This + * array may be NULL if `parent_count` is 0 (root commit). All the + * given commits must be owned by the `repo`. * * @return 0 or an error code * The created commit will be written to the Object Database and * the given reference will be updated to point to it */ GIT_EXTERN(int) git_commit_create( - git_oid *id, - git_repository *repo, - const char *update_ref, - const git_signature *author, - const git_signature *committer, - const char *message_encoding, - const char *message, - const git_tree *tree, - int parent_count, - const git_commit *parents[]); + git_oid *id, + git_repository *repo, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree, + size_t parent_count, + const git_commit *parents[]); /** - * Create a new commit in the repository using a variable - * argument list. + * Create new commit in the repository using a variable argument list. + * + * The message will **not** be cleaned up automatically. You can do that + * with the `git_message_prettify()` function. + * + * The parents for the commit are specified as a variable list of pointers + * to `const git_commit *`. Note that this is a convenience method which may + * not be safe to export for certain languages or compilers + * + * All other parameters remain the same as `git_commit_create()`. * - * The message will be cleaned up from excess whitespace - * it will be made sure that the last line ends with a '\n'. + * @param id Pointer in which to store the OID of the newly created commit * - * The parents for the commit are specified as a variable - * list of pointers to `const git_commit *`. Note that this - * is a convenience method which may not be safe to export - * for certain languages or compilers + * @param repo Repository where to store the commit + * + * @param update_ref If not NULL, name of the reference that + * will be updated to point to this commit. If the reference + * is not direct, it will be resolved to a direct reference. + * Use "HEAD" to update the HEAD of the current branch and + * make it point to this commit. If the reference doesn't + * exist yet, it will be created. If it does exist, the first + * parent must be the tip of this branch. + * + * @param author Signature with author and author time of commit * - * All other parameters remain the same + * @param committer Signature with committer and * commit time of commit + * + * @param message_encoding The encoding for the message in the + * commit, represented with a standard encoding name. + * E.g. "UTF-8". If NULL, no encoding header is written and + * UTF-8 is assumed. * - * @see git_commit_create + * @param message Full message for this commit + * + * @param tree An instance of a `git_tree` object that will + * be used as the tree for the commit. This tree object must + * also be owned by the given `repo`. + * + * @param parent_count Number of parents for this commit + * + * @return 0 or an error code + * The created commit will be written to the Object Database and + * the given reference will be updated to point to it */ GIT_EXTERN(int) git_commit_create_v( - git_oid *id, - git_repository *repo, - const char *update_ref, - const git_signature *author, - const git_signature *committer, - const char *message_encoding, - const char *message, - const git_tree *tree, - int parent_count, - ...); + git_oid *id, + git_repository *repo, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree, + size_t parent_count, + ...); + +typedef struct { + unsigned int version; + + /** + * Flags for creating the commit. + * + * If `allow_empty_commit` is specified, a commit with no changes + * from the prior commit (and "empty" commit) is allowed. Otherwise, + * commit creation will be stopped. + */ + unsigned int allow_empty_commit : 1; + + /** The commit author, or NULL for the default. */ + const git_signature *author; + + /** The committer, or NULL for the default. */ + const git_signature *committer; + + /** Encoding for the commit message; leave NULL for default. */ + const char *message_encoding; +} git_commit_create_options; + +/** Current version for the `git_commit_create_options` structure */ +#define GIT_COMMIT_CREATE_OPTIONS_VERSION 1 + +/** Static constructor for `git_commit_create_options` */ +#define GIT_COMMIT_CREATE_OPTIONS_INIT { GIT_COMMIT_CREATE_OPTIONS_VERSION } + +/** + * Commits the staged changes in the repository; this is a near analog to + * `git commit -m message`. + * + * By default, empty commits are not allowed. + * + * @param id pointer to store the new commit's object id + * @param repo repository to commit changes in + * @param message the commit message + * @param opts options for creating the commit + * @return 0 on success, GIT_EUNCHANGED if there were no changes to commit, or an error code + */ +GIT_EXTERN(int) git_commit_create_from_stage( + git_oid *id, + git_repository *repo, + const char *message, + const git_commit_create_options *opts); + +/** + * Amend an existing commit by replacing only non-NULL values. + * + * This creates a new commit that is exactly the same as the old commit, + * except that any non-NULL values will be updated. The new commit has + * the same parents as the old commit. + * + * The `update_ref` value works as in the regular `git_commit_create()`, + * updating the ref to point to the newly rewritten commit. If you want + * to amend a commit that is not currently the tip of the branch and then + * rewrite the following commits to reach a ref, pass this as NULL and + * update the rest of the commit chain and ref separately. + * + * Unlike `git_commit_create()`, the `author`, `committer`, `message`, + * `message_encoding`, and `tree` parameters can be NULL in which case this + * will use the values from the original `commit_to_amend`. + * + * All parameters have the same meanings as in `git_commit_create()`. + * + * @param id Pointer in which to store the OID of the newly created commit + * + * @param commit_to_amend The commit to amend + * + * @param update_ref If not NULL, name of the reference that + * will be updated to point to this commit. If the reference + * is not direct, it will be resolved to a direct reference. + * Use "HEAD" to update the HEAD of the current branch and + * make it point to this commit. If the reference doesn't + * exist yet, it will be created. If it does exist, the first + * parent must be the tip of this branch. + * + * @param author Signature with author and author time of commit + * + * @param committer Signature with committer and * commit time of commit + * + * @param message_encoding The encoding for the message in the + * commit, represented with a standard encoding name. + * E.g. "UTF-8". If NULL, no encoding header is written and + * UTF-8 is assumed. + * + * @param message Full message for this commit + * + * @param tree An instance of a `git_tree` object that will + * be used as the tree for the commit. This tree object must + * also be owned by the given `repo`. + * + * @return 0 or an error code + * The created commit will be written to the Object Database and + * the given reference will be updated to point to it + */ +GIT_EXTERN(int) git_commit_amend( + git_oid *id, + const git_commit *commit_to_amend, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree); + +/** + * Create a commit and write it into a buffer + * + * Create a commit as with `git_commit_create()` but instead of + * writing it to the objectdb, write the contents of the object into a + * buffer. + * + * @param out the buffer into which to write the commit object content + * + * @param repo Repository where the referenced tree and parents live + * + * @param author Signature with author and author time of commit + * + * @param committer Signature with committer and * commit time of commit + * + * @param message_encoding The encoding for the message in the + * commit, represented with a standard encoding name. + * E.g. "UTF-8". If NULL, no encoding header is written and + * UTF-8 is assumed. + * + * @param message Full message for this commit + * + * @param tree An instance of a `git_tree` object that will + * be used as the tree for the commit. This tree object must + * also be owned by the given `repo`. + * + * @param parent_count Number of parents for this commit + * + * @param parents Array of `parent_count` pointers to `git_commit` + * objects that will be used as the parents for this commit. This + * array may be NULL if `parent_count` is 0 (root commit). All the + * given commits must be owned by the `repo`. + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_create_buffer( + git_buf *out, + git_repository *repo, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree, + size_t parent_count, + const git_commit *parents[]); + +/** + * Create a commit object from the given buffer and signature + * + * Given the unsigned commit object's contents, its signature and the + * header field in which to store the signature, attach the signature + * to the commit and write it into the given repository. + * + * @param out the resulting commit id + * @param repo the repository to create the commit in. + * @param commit_content the content of the unsigned commit object + * @param signature the signature to add to the commit. Leave `NULL` + * to create a commit without adding a signature field. + * @param signature_field which header field should contain this + * signature. Leave `NULL` for the default of "gpgsig" + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_create_with_signature( + git_oid *out, + git_repository *repo, + const char *commit_content, + const char *signature, + const char *signature_field); + +/** + * Create an in-memory copy of a commit. The copy must be explicitly + * free'd or it will leak. + * + * @param out Pointer to store the copy of the commit + * @param source Original commit to copy + * @return 0 + */ +GIT_EXTERN(int) git_commit_dup(git_commit **out, git_commit *source); + +/** + * Commit creation callback: used when a function is going to create + * commits (for example, in `git_rebase_commit`) to allow callers to + * override the commit creation behavior. For example, users may + * wish to sign commits by providing this information to + * `git_commit_create_buffer`, signing that buffer, then calling + * `git_commit_create_with_signature`. The resultant commit id + * should be set in the `out` object id parameter. + * + * @param out pointer that this callback will populate with the object + * id of the commit that is created + * @param author the author name and time of the commit + * @param committer the committer name and time of the commit + * @param message_encoding the encoding of the given message, or NULL + * to assume UTF8 + * @param message the commit message + * @param tree the tree to be committed + * @param parent_count the number of parents for this commit + * @param parents the commit parents + * @param payload the payload pointer in the rebase options + * @return 0 if this callback has created the commit and populated the out + * parameter, GIT_PASSTHROUGH if the callback has not created a + * commit and wants the calling function to create the commit as + * if no callback had been specified, any other value to stop + * and return a failure + */ +typedef int (*git_commit_create_cb)( + git_oid *out, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree, + size_t parent_count, + const git_commit *parents[], + void *payload); + +/** An array of commits returned from the library */ +typedef struct git_commitarray { + git_commit *const *commits; + size_t count; +} git_commitarray; + +/** + * Free the commits contained in a commit array. This method should + * be called on `git_commitarray` objects that were provided by the + * library. Not doing so will result in a memory leak. + * + * This does not free the `git_commitarray` itself, since the library + * will never allocate that object directly itself. + * + * @param array The git_commitarray that contains commits to free + */ +GIT_EXTERN(void) git_commitarray_dispose(git_commitarray *array); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/common.h b/include/git2/common.h index 7066d5ea3c7..0be84fa77bd 100644 --- a/include/git2/common.h +++ b/include/git2/common.h @@ -10,14 +10,10 @@ #include #include -#ifdef _MSC_VER -# include "inttypes.h" -#else -# include -#endif - #ifdef __cplusplus + /** Start declarations in C mode for C++ compatibility */ # define GIT_BEGIN_DECL extern "C" { + /** End declarations in C mode */ # define GIT_END_DECL } #else /** Start declarations in C mode */ @@ -26,22 +22,48 @@ # define GIT_END_DECL /* empty */ #endif +#if defined(_MSC_VER) && _MSC_VER < 1800 +# include +#elif !defined(__CLANG_INTTYPES_H) +# include +#endif + +#ifdef DOCURIUM +/* + * This is so clang's doc parser acknowledges comments on functions + * with size_t parameters. + */ +typedef size_t size_t; +#endif + /** Declare a public function exported for application use. */ #if __GNUC__ >= 4 # define GIT_EXTERN(type) extern \ __attribute__((visibility("default"))) \ type #elif defined(_MSC_VER) -# define GIT_EXTERN(type) __declspec(dllexport) type +# define GIT_EXTERN(type) __declspec(dllexport) type __cdecl #else # define GIT_EXTERN(type) extern type #endif -/** Declare a function as always inlined. */ +/** Declare a callback function for application use. */ #if defined(_MSC_VER) -# define GIT_INLINE(type) static __inline type +# define GIT_CALLBACK(name) (__cdecl *name) +#else +# define GIT_CALLBACK(name) (*name) +#endif + +/** Declare a function as deprecated. */ +#if defined(__GNUC__) +# define GIT_DEPRECATED(func) \ + __attribute__((deprecated)) \ + __attribute__((used)) \ + func +#elif defined(_MSC_VER) +# define GIT_DEPRECATED(func) __declspec(deprecated) func #else -# define GIT_INLINE(type) static inline type +# define GIT_DEPRECATED(func) func #endif /** Declare a function's takes printf style arguments. */ @@ -51,19 +73,19 @@ # define GIT_FORMAT_PRINTF(a,b) /* empty */ #endif -#if (defined(_WIN32)) && !defined(__CYGWIN__) -#define GIT_WIN32 1 -#endif - #ifdef __amigaos4__ #include #endif /** * @file git2/common.h - * @brief Git common platform definitions + * @brief Base platform functionality * @defgroup git_common Git common platform definitions * @ingroup Git + * + * Common platform functionality including introspecting libgit2 + * itself - information like how it was built, and the current + * running version. * @{ */ @@ -71,13 +93,13 @@ GIT_BEGIN_DECL /** * The separator used in path list strings (ie like in the PATH - * environment variable). A semi-colon ";" is used on Windows, and - * a colon ":" for all other systems. + * environment variable). A semi-colon ";" is used on Windows and + * AmigaOS, and a colon ":" for all other systems. */ -#ifdef GIT_WIN32 -#define GIT_PATH_LIST_SEPARATOR ';' +#if (defined(_WIN32) && !defined(__CYGWIN__)) || defined(AMIGA) +# define GIT_PATH_LIST_SEPARATOR ';' #else -#define GIT_PATH_LIST_SEPARATOR ':' +# define GIT_PATH_LIST_SEPARATOR ':' #endif /** @@ -85,11 +107,6 @@ GIT_BEGIN_DECL */ #define GIT_PATH_MAX 4096 -/** - * The string representation of the null object ID. - */ -#define GIT_OID_HEX_ZERO "0000000000000000000000000000000000000000" - /** * Return the version of the libgit2 library * being currently used. @@ -97,56 +114,482 @@ GIT_BEGIN_DECL * @param major Store the major version number * @param minor Store the minor version number * @param rev Store the revision (patch) number + * @return 0 on success or an error code on failure + */ +GIT_EXTERN(int) git_libgit2_version(int *major, int *minor, int *rev); + +/** + * Return the prerelease state of the libgit2 library currently being + * used. For nightly builds during active development, this will be + * "alpha". Releases may have a "beta" or release candidate ("rc1", + * "rc2", etc) prerelease. For a final release, this function returns + * NULL. + * + * @return the name of the prerelease state or NULL */ -GIT_EXTERN(void) git_libgit2_version(int *major, int *minor, int *rev); +GIT_EXTERN(const char *) git_libgit2_prerelease(void); /** - * Combinations of these values describe the capabilities of libgit2. + * Configurable features of libgit2; either optional settings (like + * threading), or features that can be enabled by one of a number of + * different backend "providers" (like HTTPS, which can be provided by + * OpenSSL, mbedTLS, or system libraries). */ -enum { - GIT_CAP_THREADS = ( 1 << 0 ), - GIT_CAP_HTTPS = ( 1 << 1 ) -}; +typedef enum { + /** + * libgit2 is thread-aware and can be used from multiple threads + * (as described in the documentation). + */ + GIT_FEATURE_THREADS = (1 << 0), + + /** HTTPS remotes */ + GIT_FEATURE_HTTPS = (1 << 1), + + /** SSH remotes */ + GIT_FEATURE_SSH = (1 << 2), + + /** Sub-second resolution in index timestamps */ + GIT_FEATURE_NSEC = (1 << 3), + + /** HTTP parsing; always available */ + GIT_FEATURE_HTTP_PARSER = (1 << 4), + + /** Regular expression support; always available */ + GIT_FEATURE_REGEX = (1 << 5), + + /** Internationalization support for filename translation */ + GIT_FEATURE_I18N = (1 << 6), + + /** NTLM support over HTTPS */ + GIT_FEATURE_AUTH_NTLM = (1 << 7), + + /** Kerberos (SPNEGO) authentication support over HTTPS */ + GIT_FEATURE_AUTH_NEGOTIATE = (1 << 8), + + /** zlib support; always available */ + GIT_FEATURE_COMPRESSION = (1 << 9), + + /** SHA1 object support; always available */ + GIT_FEATURE_SHA1 = (1 << 10), + + /** SHA256 object support */ + GIT_FEATURE_SHA256 = (1 << 11) +} git_feature_t; /** * Query compile time options for libgit2. * - * @return A combination of GIT_CAP_* values. + * @return A combination of GIT_FEATURE_* values. + */ +GIT_EXTERN(int) git_libgit2_features(void); + +/** + * Query the backend details for the compile-time feature in libgit2. + * + * This will return the "backend" for the feature, which is useful for + * things like HTTPS or SSH support, that can have multiple backends + * that could be compiled in. + * + * For example, when libgit2 is compiled with dynamic OpenSSL support, + * the feature backend will be `openssl-dynamic`. The feature backend + * names reflect the compilation options specified to the build system + * (though in all lower case). The backend _may_ be "builtin" for + * features that are provided by libgit2 itself. * - * - GIT_CAP_THREADS - * Libgit2 was compiled with thread support. Note that thread support is still to be seen as a - * 'work in progress'. + * If the feature is not supported by the library, this API returns + * `NULL`. * - * - GIT_CAP_HTTPS - * Libgit2 supports the https:// protocol. This requires the open ssl library to be - * found when compiling libgit2. + * @param feature the feature to query details for + * @return the provider details, or NULL if the feature is not supported */ -GIT_EXTERN(int) git_libgit2_capabilities(void); +GIT_EXTERN(const char *) git_libgit2_feature_backend( + git_feature_t feature); - -enum { +/** + * Global library options + * + * These are used to select which global option to set or get and are + * used in `git_libgit2_opts()`. + */ +typedef enum { GIT_OPT_GET_MWINDOW_SIZE, GIT_OPT_SET_MWINDOW_SIZE, GIT_OPT_GET_MWINDOW_MAPPED_LIMIT, - GIT_OPT_SET_MWINDOW_MAPPED_LIMIT -}; + GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, + GIT_OPT_GET_SEARCH_PATH, + GIT_OPT_SET_SEARCH_PATH, + GIT_OPT_SET_CACHE_OBJECT_LIMIT, + GIT_OPT_SET_CACHE_MAX_SIZE, + GIT_OPT_ENABLE_CACHING, + GIT_OPT_GET_CACHED_MEMORY, + GIT_OPT_GET_TEMPLATE_PATH, + GIT_OPT_SET_TEMPLATE_PATH, + GIT_OPT_SET_SSL_CERT_LOCATIONS, + GIT_OPT_SET_USER_AGENT, + GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, + GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION, + GIT_OPT_SET_SSL_CIPHERS, + GIT_OPT_GET_USER_AGENT, + GIT_OPT_ENABLE_OFS_DELTA, + GIT_OPT_ENABLE_FSYNC_GITDIR, + GIT_OPT_GET_WINDOWS_SHAREMODE, + GIT_OPT_SET_WINDOWS_SHAREMODE, + GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, + GIT_OPT_SET_ALLOCATOR, + GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY, + GIT_OPT_GET_PACK_MAX_OBJECTS, + GIT_OPT_SET_PACK_MAX_OBJECTS, + GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS, + GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE, + GIT_OPT_GET_MWINDOW_FILE_LIMIT, + GIT_OPT_SET_MWINDOW_FILE_LIMIT, + GIT_OPT_SET_ODB_PACKED_PRIORITY, + GIT_OPT_SET_ODB_LOOSE_PRIORITY, + GIT_OPT_GET_EXTENSIONS, + GIT_OPT_SET_EXTENSIONS, + GIT_OPT_GET_OWNER_VALIDATION, + GIT_OPT_SET_OWNER_VALIDATION, + GIT_OPT_GET_HOMEDIR, + GIT_OPT_SET_HOMEDIR, + GIT_OPT_SET_SERVER_CONNECT_TIMEOUT, + GIT_OPT_GET_SERVER_CONNECT_TIMEOUT, + GIT_OPT_SET_SERVER_TIMEOUT, + GIT_OPT_GET_SERVER_TIMEOUT, + GIT_OPT_SET_USER_AGENT_PRODUCT, + GIT_OPT_GET_USER_AGENT_PRODUCT, + GIT_OPT_ADD_SSL_X509_CERT +} git_libgit2_opt_t; /** * Set or query a library global option * * Available options: * - * opts(GIT_OPT_MWINDOW_SIZE, size_t): - * set the maximum mmap window size + * * opts(GIT_OPT_GET_MWINDOW_SIZE, size_t *): + * + * > Get the maximum mmap window size + * + * * opts(GIT_OPT_SET_MWINDOW_SIZE, size_t): + * + * > Set the maximum mmap window size + * + * * opts(GIT_OPT_GET_MWINDOW_MAPPED_LIMIT, size_t *): + * + * > Get the maximum memory that will be mapped in total by the library + * + * * opts(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, size_t): + * + * > Set the maximum amount of memory that can be mapped at any time + * > by the library + * + * * opts(GIT_OPT_GET_MWINDOW_FILE_LIMIT, size_t *): + * + * > Get the maximum number of files that will be mapped at any time by the + * > library + * + * * opts(GIT_OPT_SET_MWINDOW_FILE_LIMIT, size_t): + * + * > Set the maximum number of files that can be mapped at any time + * > by the library. The default (0) is unlimited. + * + * * opts(GIT_OPT_GET_SEARCH_PATH, int level, git_buf *buf) + * + * > Get the search path for a given level of config data. "level" must + * > be one of `GIT_CONFIG_LEVEL_SYSTEM`, `GIT_CONFIG_LEVEL_GLOBAL`, + * > `GIT_CONFIG_LEVEL_XDG`, or `GIT_CONFIG_LEVEL_PROGRAMDATA`. + * > The search path is written to the `out` buffer. + * + * * opts(GIT_OPT_SET_SEARCH_PATH, int level, const char *path) + * + * > Set the search path for a level of config data. The search path + * > applied to shared attributes and ignore files, too. + * > + * > - `path` lists directories delimited by GIT_PATH_LIST_SEPARATOR. + * > Pass NULL to reset to the default (generally based on environment + * > variables). Use magic path `$PATH` to include the old value + * > of the path (if you want to prepend or append, for instance). + * > + * > - `level` must be `GIT_CONFIG_LEVEL_SYSTEM`, + * > `GIT_CONFIG_LEVEL_GLOBAL`, `GIT_CONFIG_LEVEL_XDG`, or + * > `GIT_CONFIG_LEVEL_PROGRAMDATA`. + * + * * opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, git_object_t type, size_t size) + * + * > Set the maximum data size for the given type of object to be + * > considered eligible for caching in memory. Setting to value to + * > zero means that that type of object will not be cached. + * > Defaults to 0 for GIT_OBJECT_BLOB (i.e. won't cache blobs) and 4k + * > for GIT_OBJECT_COMMIT, GIT_OBJECT_TREE, and GIT_OBJECT_TAG. + * + * * opts(GIT_OPT_SET_CACHE_MAX_SIZE, ssize_t max_storage_bytes) + * + * > Set the maximum total data size that will be cached in memory + * > across all repositories before libgit2 starts evicting objects + * > from the cache. This is a soft limit, in that the library might + * > briefly exceed it, but will start aggressively evicting objects + * > from cache when that happens. The default cache size is 256MB. + * + * * opts(GIT_OPT_ENABLE_CACHING, int enabled) * - * opts(GIT_OPT_MWINDOW_MAPPED_LIMIT, size_t): - * set the maximum amount of memory that can be mapped at any time - * by the library + * > Enable or disable caching completely. + * > + * > Because caches are repository-specific, disabling the cache + * > cannot immediately clear all cached objects, but each cache will + * > be cleared on the next attempt to update anything in it. + * + * * opts(GIT_OPT_GET_CACHED_MEMORY, ssize_t *current, ssize_t *allowed) + * + * > Get the current bytes in cache and the maximum that would be + * > allowed in the cache. + * + * * opts(GIT_OPT_GET_TEMPLATE_PATH, git_buf *out) + * + * > Get the default template path. + * > The path is written to the `out` buffer. + * + * * opts(GIT_OPT_SET_TEMPLATE_PATH, const char *path) + * + * > Set the default template path. + * > + * > - `path` directory of template. + * + * * opts(GIT_OPT_SET_SSL_CERT_LOCATIONS, const char *file, const char *path) + * + * > Set the SSL certificate-authority locations. + * > + * > - `file` is the location of a file containing several + * > certificates concatenated together. + * > - `path` is the location of a directory holding several + * > certificates, one per file. + * > + * > Calling `GIT_OPT_ADD_SSL_X509_CERT` may override the + * > data in `path`. + * > + * > Either parameter may be `NULL`, but not both. + * + * * opts(GIT_OPT_ADD_SSL_X509_CERT, const X509 *cert) + * + * > Add a raw X509 certificate into the SSL certs store. + * > This certificate is only used by libgit2 invocations + * > during the application lifetime and is not persisted + * > to disk. This certificate cannot be removed from the + * > application once is has been added. + * > + * > - `cert` is the raw X509 cert will be added to cert store. + * + * * opts(GIT_OPT_SET_USER_AGENT, const char *user_agent) + * + * > Set the value of the comment section of the User-Agent header. + * > This can be information about your product and its version. + * > By default this is "libgit2" followed by the libgit2 version. + * > + * > This value will be appended to User-Agent _product_, which + * > is typically set to "git/2.0". + * > + * > Set to the empty string ("") to not send any information in the + * > comment section, or set to NULL to restore the default. + * + * * opts(GIT_OPT_GET_USER_AGENT, git_buf *out) + * + * > Get the value of the User-Agent header. + * > The User-Agent is written to the `out` buffer. + * + * * opts(GIT_OPT_SET_USER_AGENT_PRODUCT, const char *user_agent_product) + * + * > Set the value of the product portion of the User-Agent header. + * > This defaults to "git/2.0", for compatibility with other git + * > clients. It is recommended to keep this as git/ for + * > compatibility with servers that do user-agent detection. + * > + * > Set to the empty string ("") to not send any user-agent string, + * > or set to NULL to restore the default. + * + * * opts(GIT_OPT_GET_USER_AGENT_PRODUCT, git_buf *out) + * + * > Get the value of the User-Agent product header. + * > The User-Agent product is written to the `out` buffer. + * + * * opts(GIT_OPT_SET_WINDOWS_SHAREMODE, unsigned long value) + * + * > Set the share mode used when opening files on Windows. + * > For more information, see the documentation for CreateFile. + * > The default is: FILE_SHARE_READ | FILE_SHARE_WRITE. This is + * > ignored and unused on non-Windows platforms. + * + * * opts(GIT_OPT_GET_WINDOWS_SHAREMODE, unsigned long *value) + * + * > Get the share mode used when opening files on Windows. + * + * * opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, int enabled) + * + * > Enable strict input validation when creating new objects + * > to ensure that all inputs to the new objects are valid. For + * > example, when this is enabled, the parent(s) and tree inputs + * > will be validated when creating a new commit. This defaults + * > to enabled. + * + * * opts(GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION, int enabled) + * + * > Validate the target of a symbolic ref when creating it. For + * > example, `foobar` is not a valid ref, therefore `foobar` is + * > not a valid target for a symbolic ref by default, whereas + * > `refs/heads/foobar` is. Disabling this bypasses validation + * > so that an arbitrary strings such as `foobar` can be used + * > for a symbolic ref target. This defaults to enabled. + * + * * opts(GIT_OPT_SET_SSL_CIPHERS, const char *ciphers) + * + * > Set the SSL ciphers use for HTTPS connections. + * > + * > - `ciphers` is the list of ciphers that are eanbled. + * + * * opts(GIT_OPT_ENABLE_OFS_DELTA, int enabled) + * + * > Enable or disable the use of "offset deltas" when creating packfiles, + * > and the negotiation of them when talking to a remote server. + * > Offset deltas store a delta base location as an offset into the + * > packfile from the current location, which provides a shorter encoding + * > and thus smaller resultant packfiles. + * > Packfiles containing offset deltas can still be read. + * > This defaults to enabled. + * + * * opts(GIT_OPT_ENABLE_FSYNC_GITDIR, int enabled) + * + * > Enable synchronized writes of files in the gitdir using `fsync` + * > (or the platform equivalent) to ensure that new object data + * > is written to permanent storage, not simply cached. This + * > defaults to disabled. + * + * opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, int enabled) + * + * > Enable strict verification of object hashsums when reading + * > objects from disk. This may impact performance due to an + * > additional checksum calculation on each object. This defaults + * > to enabled. + * + * opts(GIT_OPT_SET_ALLOCATOR, git_allocator *allocator) + * + * > Set the memory allocator to a different memory allocator. This + * > allocator will then be used to make all memory allocations for + * > libgit2 operations. If the given `allocator` is NULL, then the + * > system default will be restored. + * + * opts(GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY, int enabled) + * + * > Ensure that there are no unsaved changes in the index before + * > beginning any operation that reloads the index from disk (eg, + * > checkout). If there are unsaved changes, the instruction will + * > fail. (Using the FORCE flag to checkout will still overwrite + * > these changes.) + * + * opts(GIT_OPT_GET_PACK_MAX_OBJECTS, size_t *out) + * + * > Get the maximum number of objects libgit2 will allow in a pack + * > file when downloading a pack file from a remote. This can be + * > used to limit maximum memory usage when fetching from an untrusted + * > remote. + * + * opts(GIT_OPT_SET_PACK_MAX_OBJECTS, size_t objects) + * + * > Set the maximum number of objects libgit2 will allow in a pack + * > file when downloading a pack file from a remote. + * + * opts(GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS, int enabled) + * > This will cause .keep file existence checks to be skipped when + * > accessing packfiles, which can help performance with remote filesystems. + * + * opts(GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE, int enabled) + * > When connecting to a server using NTLM or Negotiate + * > authentication, use expect/continue when POSTing data. + * > This option is not available on Windows. + * + * opts(GIT_OPT_SET_ODB_PACKED_PRIORITY, int priority) + * > Override the default priority of the packed ODB backend which + * > is added when default backends are assigned to a repository + * + * opts(GIT_OPT_SET_ODB_LOOSE_PRIORITY, int priority) + * > Override the default priority of the loose ODB backend which + * > is added when default backends are assigned to a repository + * + * opts(GIT_OPT_GET_EXTENSIONS, git_strarray *out) + * > Returns the list of git extensions that are supported. This + * > is the list of built-in extensions supported by libgit2 and + * > custom extensions that have been added with + * > `GIT_OPT_SET_EXTENSIONS`. Extensions that have been negated + * > will not be returned. The returned list should be released + * > with `git_strarray_dispose`. + * + * opts(GIT_OPT_SET_EXTENSIONS, const char **extensions, size_t len) + * > Set that the given git extensions are supported by the caller. + * > Extensions supported by libgit2 may be negated by prefixing + * > them with a `!`. For example: setting extensions to + * > { "!noop", "newext" } indicates that the caller does not want + * > to support repositories with the `noop` extension but does want + * > to support repositories with the `newext` extension. + * + * opts(GIT_OPT_GET_OWNER_VALIDATION, int *enabled) + * > Gets the owner validation setting for repository + * > directories. + * + * opts(GIT_OPT_SET_OWNER_VALIDATION, int enabled) + * > Set that repository directories should be owned by the current + * > user. The default is to validate ownership. + * + * opts(GIT_OPT_GET_HOMEDIR, git_buf *out) + * > Gets the current user's home directory, as it will be used + * > for file lookups. The path is written to the `out` buffer. + * + * opts(GIT_OPT_SET_HOMEDIR, const char *path) + * > Sets the directory used as the current user's home directory, + * > for file lookups. + * > + * > - `path` directory of home directory. + * + * opts(GIT_OPT_GET_SERVER_CONNECT_TIMEOUT, int *timeout) + * > Gets the timeout (in milliseconds) to attempt connections to + * > a remote server. + * + * opts(GIT_OPT_SET_SERVER_CONNECT_TIMEOUT, int timeout) + * > Sets the timeout (in milliseconds) to attempt connections to + * > a remote server. Set to 0 to use the system default. Note that + * > this may not be able to be configured longer than the system + * > default, typically 75 seconds. + * + * opts(GIT_OPT_GET_SERVER_TIMEOUT, int *timeout) + * > Gets the timeout (in milliseconds) for reading from and writing + * > to a remote server. + * + * opts(GIT_OPT_SET_SERVER_TIMEOUT, int timeout) + * > Sets the timeout (in milliseconds) for reading from and writing + * > to a remote server. Set to 0 to use the system default. + * + * @param option Option key + * @return 0 on success, <0 on failure + */ +GIT_EXTERN(int) git_libgit2_opts(int option, ...); + +/** + * Build information items to query. This is the information type + * passed to `git_libgit2_buildinfo` to get particular information + * about the libgit2 build. + */ +typedef enum { + /** The CPU type that libgit2 was built for. */ + GIT_BUILDINFO_CPU = 1, + + /** The commit that libgit2 was built from. */ + GIT_BUILDINFO_COMMIT +} git_buildinfo_t; + +/** + * Query information about the compile-time information about + * libgit2. * - * @param option Option key - * @param ... value to set the option + * @param info the build information to query + * @return the requested information, or `NULL` on error */ -GIT_EXTERN(void) git_libgit2_opts(int option, ...); +GIT_EXTERN(const char *) git_libgit2_buildinfo( + git_buildinfo_t info); /** @} */ GIT_END_DECL diff --git a/include/git2/config.h b/include/git2/config.h index 19d4cb78db1..f9c26675403 100644 --- a/include/git2/config.h +++ b/include/git2/config.h @@ -9,75 +9,159 @@ #include "common.h" #include "types.h" +#include "buffer.h" /** * @file git2/config.h - * @brief Git config management routines + * @brief Per-repository, per-user or per-system configuration * @defgroup git_config Git config management routines * @ingroup Git + * + * Git configuration affects the operation of the version control + * system, and can be specified on a per-repository basis, in user + * settings, or at the system level. * @{ */ GIT_BEGIN_DECL /** * Priority level of a config file. + * * These priority levels correspond to the natural escalation logic - * (from higher to lower) when searching for config entries in git.git. + * (from higher to lower) when reading or searching for config entries + * in git.git. Meaning that for the same key, the configuration in + * the local configuration is preferred over the configuration in + * the system configuration file. + * + * Callers can add their own custom configuration, beginning at the + * `GIT_CONFIG_LEVEL_APP` level. + * + * Writes, by default, occur in the highest priority level backend + * that is writable. This ordering can be overridden with + * `git_config_set_writeorder`. * * git_config_open_default() and git_repository_config() honor those * priority levels as well. + * + * @see git_config_open_default + * @see git_repository_config */ -enum { - GIT_CONFIG_LEVEL_SYSTEM = 1, /**< System-wide configuration file. */ - GIT_CONFIG_LEVEL_XDG = 2, /**< XDG compatible configuration file (.config/git/config). */ - GIT_CONFIG_LEVEL_GLOBAL = 3, /**< User-specific configuration file, also called Global configuration file. */ - GIT_CONFIG_LEVEL_LOCAL = 4, /**< Repository specific configuration file. */ - GIT_CONFIG_HIGHEST_LEVEL = -1, /**< Represents the highest level of a config file. */ -}; - -typedef struct { +typedef enum { + /** + * System-wide on Windows, for compatibility with "Portable Git". + */ + GIT_CONFIG_LEVEL_PROGRAMDATA = 1, + + /** + * System-wide configuration file; `/etc/gitconfig` on Linux. + */ + GIT_CONFIG_LEVEL_SYSTEM = 2, + + /** + * XDG compatible configuration file; typically + * `~/.config/git/config`. + */ + GIT_CONFIG_LEVEL_XDG = 3, + + /** + * Global configuration file is the user-specific configuration; + * typically `~/.gitconfig`. + */ + GIT_CONFIG_LEVEL_GLOBAL = 4, + + /** + * Local configuration, the repository-specific configuration file; + * typically `$GIT_DIR/config`. + */ + GIT_CONFIG_LEVEL_LOCAL = 5, + + /** + * Worktree-specific configuration; typically + * `$GIT_DIR/config.worktree`. + */ + GIT_CONFIG_LEVEL_WORKTREE = 6, + + /** + * Application-specific configuration file. Callers into libgit2 + * can add their own configuration beginning at this level. + */ + GIT_CONFIG_LEVEL_APP = 7, + + /** + * Not a configuration level; callers can use this value when + * querying configuration levels to specify that they want to + * have data from the highest-level currently configuration. + * This can be used to indicate that callers want the most + * specific config file available that actually is loaded. + */ + GIT_CONFIG_HIGHEST_LEVEL = -1 +} git_config_level_t; + +/** + * An entry in a configuration file + */ +typedef struct git_config_entry { + /** Name of the configuration entry (normalized). */ const char *name; + + /** Literal (string) value of the entry. */ const char *value; - unsigned int level; -} git_config_entry; -typedef int (*git_config_foreach_cb)(const git_config_entry *, void *); + /** The type of backend that this entry exists in (eg, "file"). */ + const char *backend_type; + /** + * The path to the origin of this entry. For config files, this is + * the path to the file. + */ + const char *origin_path; + + /** Depth of includes where this variable was found. */ + unsigned int include_depth; + + /** Configuration level for the file this was found in. */ + git_config_level_t level; +} git_config_entry; + +/** + * Free a config entry. + * + * @param entry The entry to free. + */ +GIT_EXTERN(void) git_config_entry_free(git_config_entry *entry); /** - * Generic backend that implements the interface to - * access a configuration file + * A config enumeration callback. + * + * @param entry the entry currently being enumerated + * @param payload a user-specified pointer + * @return non-zero to terminate the iteration. */ -struct git_config_backend { - unsigned int version; - struct git_config *cfg; +typedef int GIT_CALLBACK(git_config_foreach_cb)(const git_config_entry *entry, void *payload); - /* Open means open the file/database and parse if necessary */ - int (*open)(struct git_config_backend *, unsigned int level); - int (*get)(const struct git_config_backend *, const char *key, const git_config_entry **entry); - int (*get_multivar)(struct git_config_backend *, const char *key, const char *regexp, git_config_foreach_cb callback, void *payload); - int (*set)(struct git_config_backend *, const char *key, const char *value); - int (*set_multivar)(git_config_backend *cfg, const char *name, const char *regexp, const char *value); - int (*del)(struct git_config_backend *, const char *key); - int (*foreach)(struct git_config_backend *, const char *, git_config_foreach_cb callback, void *payload); - int (*refresh)(struct git_config_backend *); - void (*free)(struct git_config_backend *); -}; -#define GIT_CONFIG_BACKEND_VERSION 1 -#define GIT_CONFIG_BACKEND_INIT {GIT_CONFIG_BACKEND_VERSION} +/** + * An opaque structure for a configuration iterator. + */ +typedef struct git_config_iterator git_config_iterator; +/** + * Config var type + */ typedef enum { - GIT_CVAR_FALSE = 0, - GIT_CVAR_TRUE = 1, - GIT_CVAR_INT32, - GIT_CVAR_STRING -} git_cvar_t; + GIT_CONFIGMAP_FALSE = 0, + GIT_CONFIGMAP_TRUE = 1, + GIT_CONFIGMAP_INT32, + GIT_CONFIGMAP_STRING +} git_configmap_t; +/** + * Mapping from config variables to values. + */ typedef struct { - git_cvar_t cvar_type; + git_configmap_t type; const char *str_match; int map_value; -} git_cvar_map; +} git_configmap; /** * Locate the path to the global configuration file @@ -91,13 +175,12 @@ typedef struct { * global configuration file. * * This method will not guess the path to the xdg compatible - * config file (.config/git/config). + * config file (`.config/git/config`). * - * @param out Buffer to store the path in - * @param length size of the buffer in bytes - * @return 0 if a global configuration file has been found. Its path will be stored in `buffer`. + * @param out Pointer to a user-allocated git_buf in which to store the path + * @return 0 if a global configuration file has been found. Its path will be stored in `out`. */ -GIT_EXTERN(int) git_config_find_global(char *out, size_t length); +GIT_EXTERN(int) git_config_find_global(git_buf *out); /** * Locate the path to the global xdg compatible configuration file @@ -110,25 +193,34 @@ GIT_EXTERN(int) git_config_find_global(char *out, size_t length); * may be used on any `git_config` call to load the * xdg compatible configuration file. * - * @param out Buffer to store the path in - * @param length size of the buffer in bytes + * @param out Pointer to a user-allocated git_buf in which to store the path * @return 0 if a xdg compatible configuration file has been - * found. Its path will be stored in `buffer`. + * found. Its path will be stored in `out`. */ -GIT_EXTERN(int) git_config_find_xdg(char *out, size_t length); +GIT_EXTERN(int) git_config_find_xdg(git_buf *out); /** * Locate the path to the system configuration file * - * If /etc/gitconfig doesn't exist, it will look for - * %PROGRAMFILES%\Git\etc\gitconfig. - - * @param global_config_path Buffer to store the path in - * @param length size of the buffer in bytes + * If `/etc/gitconfig` doesn't exist, it will look for + * `%PROGRAMFILES%\Git\etc\gitconfig`. + * + * @param out Pointer to a user-allocated git_buf in which to store the path * @return 0 if a system configuration file has been - * found. Its path will be stored in `buffer`. + * found. Its path will be stored in `out`. + */ +GIT_EXTERN(int) git_config_find_system(git_buf *out); + +/** + * Locate the path to the configuration file in ProgramData + * + * Look for the file in `%PROGRAMDATA%\Git\config` used by portable git. + * + * @param out Pointer to a user-allocated git_buf in which to store the path + * @return 0 if a ProgramData configuration file has been + * found. Its path will be stored in `out`. */ -GIT_EXTERN(int) git_config_find_system(char *out, size_t length); +GIT_EXTERN(int) git_config_find_programdata(git_buf *out); /** * Open the global, XDG and system configuration files @@ -153,30 +245,6 @@ GIT_EXTERN(int) git_config_open_default(git_config **out); */ GIT_EXTERN(int) git_config_new(git_config **out); -/** - * Add a generic config file instance to an existing config - * - * Note that the configuration object will free the file - * automatically. - * - * Further queries on this config object will access each - * of the config file instances in order (instances with - * a higher priority level will be accessed first). - * - * @param cfg the configuration to add the file to - * @param file the configuration file (backend) to add - * @param level the priority level of the backend - * @param force if a config file already exists for the given - * priority level, replace it - * @return 0 on success, GIT_EEXISTS when adding more than one file - * for a given priority level (and force_replace set to 0), or error code - */ -GIT_EXTERN(int) git_config_add_backend( - git_config *cfg, - git_config_backend *file, - unsigned int level, - int force); - /** * Add an on-disk config file instance to an existing config * @@ -184,6 +252,9 @@ GIT_EXTERN(int) git_config_add_backend( * parsed; it's expected to be a native Git config file following * the default Git config syntax (see man git-config). * + * If the file does not exist, the file will still be added and it + * will be created the first time we write to it. + * * Note that the configuration object will free the file * automatically. * @@ -192,10 +263,11 @@ GIT_EXTERN(int) git_config_add_backend( * a higher priority level will be accessed first). * * @param cfg the configuration to add the file to - * @param path path to the configuration file (backend) to add + * @param path path to the configuration file to add * @param level the priority level of the backend - * @param force if a config file already exists for the given - * priority level, replace it + * @param repo optional repository to allow parsing of + * conditional includes + * @param force replace config file at the given priority level * @return 0 on success, GIT_EEXISTS when adding more than one file * for a given priority level (and force_replace set to 0), * GIT_ENOTFOUND when the file doesn't exist or error code @@ -203,7 +275,8 @@ GIT_EXTERN(int) git_config_add_backend( GIT_EXTERN(int) git_config_add_file_ondisk( git_config *cfg, const char *path, - unsigned int level, + git_config_level_t level, + const git_repository *repo, int force); /** @@ -216,8 +289,7 @@ GIT_EXTERN(int) git_config_add_file_ondisk( * * @param out The configuration instance to create * @param path Path to the on-disk file to open - * @return 0 on success, GIT_ENOTFOUND when the file doesn't exist - * or an error code + * @return 0 on success, or an error code */ GIT_EXTERN(int) git_config_open_ondisk(git_config **out, const char *path); @@ -238,22 +310,56 @@ GIT_EXTERN(int) git_config_open_ondisk(git_config **out, const char *path); * multi-level parent config, or an error code */ GIT_EXTERN(int) git_config_open_level( - git_config **out, - const git_config *parent, - unsigned int level); + git_config **out, + const git_config *parent, + git_config_level_t level); + +/** + * Open the global/XDG configuration file according to git's rules + * + * Git allows you to store your global configuration at + * `$HOME/.gitconfig` or `$XDG_CONFIG_HOME/git/config`. For backwards + * compatibility, the XDG file shouldn't be used unless the use has + * created it explicitly. With this function you'll open the correct + * one to write to. + * + * @param out pointer in which to store the config object + * @param config the config object in which to look + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_config_open_global(git_config **out, git_config *config); + +/** + * Set the write order for configuration backends. By default, the + * write ordering does not match the read ordering; for example, the + * worktree configuration is a high-priority for reading, but is not + * written to unless explicitly chosen. + * + * @param cfg the configuration to change write order of + * @param levels the ordering of levels for writing + * @param len the length of the levels array + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_set_writeorder( + git_config *cfg, + git_config_level_t *levels, + size_t len); /** - * Reload changed config files + * Create a snapshot of the configuration + * + * Create a snapshot of the current state of a configuration, which + * allows you to look into a consistent view of the configuration for + * looking up complex values (e.g. a remote, submodule). * - * A config file may be changed on disk out from under the in-memory - * config object. This function causes us to look for files that have - * been modified since we last loaded them and refresh the config with - * the latest information. + * The string returned when querying such a config object is valid + * until it is freed. * - * @param cfg The configuration to refresh + * @param out pointer in which to store the snapshot config object + * @param config configuration to snapshot * @return 0 or an error code */ -GIT_EXTERN(int) git_config_refresh(git_config *cfg); +GIT_EXTERN(int) git_config_snapshot(git_config **out, git_config *config); /** * Free the configuration and its associated memory and files @@ -265,16 +371,15 @@ GIT_EXTERN(void) git_config_free(git_config *cfg); /** * Get the git_config_entry of a config variable. * - * The git_config_entry is owned by the config and should not be freed by the - * user. - + * Free the git_config_entry after use with `git_config_entry_free()`. + * * @param out pointer to the variable git_config_entry * @param cfg where to look for the variable * @param name the variable's name * @return 0 or an error code */ GIT_EXTERN(int) git_config_get_entry( - const git_config_entry **out, + git_config_entry **out, const git_config *cfg, const char *name); @@ -283,7 +388,7 @@ GIT_EXTERN(int) git_config_get_entry( * * All config files will be looked into, in the order of their * defined level. A higher level means a higher priority. The - * first occurence of the variable will be returned here. + * first occurrence of the variable will be returned here. * * @param out pointer to the variable where the value should be stored * @param cfg where to look for the variable @@ -323,17 +428,36 @@ GIT_EXTERN(int) git_config_get_int64(int64_t *out, const git_config *cfg, const */ GIT_EXTERN(int) git_config_get_bool(int *out, const git_config *cfg, const char *name); +/** + * Get the value of a path config variable. + * + * A leading '~' will be expanded to the global search path (which + * defaults to the user's home directory but can be overridden via + * `git_libgit2_opts()`. + * + * All config files will be looked into, in the order of their + * defined level. A higher level means a higher priority. The + * first occurrence of the variable will be returned here. + * + * @param out the buffer in which to store the result + * @param cfg where to look for the variable + * @param name the variable's name + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_get_path(git_buf *out, const git_config *cfg, const char *name); + /** * Get the value of a string config variable. * - * The string is owned by the variable and should not be freed by the - * user. + * This function can only be used on snapshot config objects. The + * string is owned by the config and should not be freed by the + * user. The pointer will be valid until the config is freed. * * All config files will be looked into, in the order of their * defined level. A higher level means a higher priority. The * first occurrence of the variable will be returned here. * - * @param out pointer to the variable's value + * @param out pointer to the string * @param cfg where to look for the variable * @param name the variable's name * @return 0 or an error code @@ -341,18 +465,74 @@ GIT_EXTERN(int) git_config_get_bool(int *out, const git_config *cfg, const char GIT_EXTERN(int) git_config_get_string(const char **out, const git_config *cfg, const char *name); /** - * Get each value of a multivar. + * Get the value of a string config variable. + * + * The value of the config will be copied into the buffer. + * + * All config files will be looked into, in the order of their + * defined level. A higher level means a higher priority. The + * first occurrence of the variable will be returned here. + * + * @param out buffer in which to store the string + * @param cfg where to look for the variable + * @param name the variable's name + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_get_string_buf(git_buf *out, const git_config *cfg, const char *name); + +/** + * Get each value of a multivar in a foreach callback * * The callback will be called on each variable found * + * The regular expression is applied case-sensitively on the normalized form of + * the variable name: the section and variable parts are lower-cased. The + * subsection is left unchanged. + * + * @param cfg where to look for the variable + * @param name the variable's name + * @param regexp regular expression to filter which variables we're + * interested in. Use NULL to indicate all + * @param callback the function to be called on each value of the variable + * @param payload opaque pointer to pass to the callback + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_config_get_multivar_foreach(const git_config *cfg, const char *name, const char *regexp, git_config_foreach_cb callback, void *payload); + +/** + * Get each value of a multivar + * + * The regular expression is applied case-sensitively on the normalized form of + * the variable name: the section and variable parts are lower-cased. The + * subsection is left unchanged. + * + * @param out pointer to store the iterator * @param cfg where to look for the variable * @param name the variable's name * @param regexp regular expression to filter which variables we're * interested in. Use NULL to indicate all - * @param fn the function to be called on each value of the variable - * @param data opaque pointer to pass to the callback + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_config_multivar_iterator_new(git_config_iterator **out, const git_config *cfg, const char *name, const char *regexp); + +/** + * Return the current entry and advance the iterator + * + * The pointers returned by this function are valid until the next call + * to `git_config_next` or until the iterator is freed. + * + * @param entry pointer to store the entry + * @param iter the iterator + * @return 0 or an error code. GIT_ITEROVER if the iteration has completed + */ +GIT_EXTERN(int) git_config_next(git_config_entry **entry, git_config_iterator *iter); + +/** + * Free a config iterator + * + * @param iter the iterator to free */ -GIT_EXTERN(int) git_config_get_multivar(const git_config *cfg, const char *name, const char *regexp, git_config_foreach_cb callback, void *payload); +GIT_EXTERN(void) git_config_iterator_free(git_config_iterator *iter); /** * Set the value of an integer config variable in the config file @@ -404,10 +584,13 @@ GIT_EXTERN(int) git_config_set_string(git_config *cfg, const char *name, const c /** * Set a multivar in the local config file. * + * The regular expression is applied case-sensitively on the value. + * * @param cfg where to look for the variable * @param name the variable's name * @param regexp a regular expression to indicate which values to replace * @param value the new value. + * @return 0 or an error code. */ GIT_EXTERN(int) git_config_set_multivar(git_config *cfg, const char *name, const char *regexp, const char *value); @@ -417,34 +600,87 @@ GIT_EXTERN(int) git_config_set_multivar(git_config *cfg, const char *name, const * * @param cfg the configuration * @param name the variable to delete + * @return 0 or an error code. */ GIT_EXTERN(int) git_config_delete_entry(git_config *cfg, const char *name); +/** + * Deletes one or several entries from a multivar in the local config file. + * + * The regular expression is applied case-sensitively on the value. + * + * @param cfg where to look for the variables + * @param name the variable's name + * @param regexp a regular expression to indicate which values to delete + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_delete_multivar(git_config *cfg, const char *name, const char *regexp); + /** * Perform an operation on each config variable. * * The callback receives the normalized name and value of each variable * in the config backend, and the data pointer passed to this function. - * As soon as one of the callback functions returns something other than 0, - * this function stops iterating and returns `GIT_EUSER`. + * If the callback returns a non-zero value, the function stops iterating + * and returns that value to the caller. + * + * The pointers passed to the callback are only valid as long as the + * iteration is ongoing. * * @param cfg where to get the variables from * @param callback the function to call on each variable * @param payload the data to pass to the callback - * @return 0 on success, GIT_EUSER on non-zero callback, or error code + * @return 0 on success, non-zero callback return value, or error code */ GIT_EXTERN(int) git_config_foreach( const git_config *cfg, git_config_foreach_cb callback, void *payload); +/** + * Iterate over all the config variables + * + * Use `git_config_next` to advance the iteration and + * `git_config_iterator_free` when done. + * + * @param out pointer to store the iterator + * @param cfg where to get the variables from + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_config_iterator_new(git_config_iterator **out, const git_config *cfg); + +/** + * Iterate over all the config variables whose name matches a pattern + * + * Use `git_config_next` to advance the iteration and + * `git_config_iterator_free` when done. + * + * The regular expression is applied case-sensitively on the normalized form of + * the variable name: the section and variable parts are lower-cased. The + * subsection is left unchanged. + * + * @param out pointer to store the iterator + * @param cfg where to ge the variables from + * @param regexp regular expression to match the names + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_config_iterator_glob_new(git_config_iterator **out, const git_config *cfg, const char *regexp); + /** * Perform an operation on each config variable matching a regular expression. * - * This behaviors like `git_config_foreach` with an additional filter of a + * This behaves like `git_config_foreach` with an additional filter of a * regular expression that filters which config keys are passed to the * callback. * + * The regular expression is applied case-sensitively on the normalized form of + * the variable name: the section and variable parts are lower-cased. The + * subsection is left unchanged. + * + * The regular expression is applied case-sensitively on the normalized form of + * the variable name: the case-insensitive parts are lower-case. + * * @param cfg where to get the variables from * @param regexp regular expression to match against config names * @param callback the function to call on each variable @@ -466,7 +702,7 @@ GIT_EXTERN(int) git_config_foreach_match( * * A mapping array looks as follows: * - * git_cvar_map autocrlf_mapping[] = { + * git_configmap autocrlf_mapping[] = { * {GIT_CVAR_FALSE, NULL, GIT_AUTO_CRLF_FALSE}, * {GIT_CVAR_TRUE, NULL, GIT_AUTO_CRLF_TRUE}, * {GIT_CVAR_STRING, "input", GIT_AUTO_CRLF_INPUT}, @@ -487,28 +723,29 @@ GIT_EXTERN(int) git_config_foreach_match( * @param out place to store the result of the mapping * @param cfg config file to get the variables from * @param name name of the config variable to lookup - * @param maps array of `git_cvar_map` objects specifying the possible mappings + * @param maps array of `git_configmap` objects specifying the possible mappings * @param map_n number of mapping objects in `maps` * @return 0 on success, error code otherwise */ GIT_EXTERN(int) git_config_get_mapped( - int *out, - const git_config *cfg, - const char *name, - const git_cvar_map *maps, - size_t map_n); + int *out, + const git_config *cfg, + const char *name, + const git_configmap *maps, + size_t map_n); /** * Maps a string value to an integer constant * * @param out place to store the result of the parsing - * @param maps array of `git_cvar_map` objects specifying the possible mappings + * @param maps array of `git_configmap` objects specifying the possible mappings * @param map_n number of mapping objects in `maps` * @param value value to parse + * @return 0 or an error code. */ GIT_EXTERN(int) git_config_lookup_map_value( int *out, - const git_cvar_map *maps, + const git_configmap *maps, size_t map_n, const char *value); @@ -521,6 +758,7 @@ GIT_EXTERN(int) git_config_lookup_map_value( * * @param out place to store the result of the parsing * @param value value to parse + * @return 0 or an error code. */ GIT_EXTERN(int) git_config_parse_bool(int *out, const char *value); @@ -533,6 +771,7 @@ GIT_EXTERN(int) git_config_parse_bool(int *out, const char *value); * * @param out place to store the result of the parsing * @param value value to parse + * @return 0 or an error code. */ GIT_EXTERN(int) git_config_parse_int32(int32_t *out, const char *value); @@ -545,10 +784,69 @@ GIT_EXTERN(int) git_config_parse_int32(int32_t *out, const char *value); * * @param out place to store the result of the parsing * @param value value to parse + * @return 0 or an error code. */ GIT_EXTERN(int) git_config_parse_int64(int64_t *out, const char *value); +/** + * Parse a string value as a path. + * + * A leading '~' will be expanded to the global search path (which + * defaults to the user's home directory but can be overridden via + * `git_libgit2_opts()`. + * + * If the value does not begin with a tilde, the input will be + * returned. + * + * @param out placae to store the result of parsing + * @param value the path to evaluate + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_config_parse_path(git_buf *out, const char *value); + +/** + * Perform an operation on each config variable in a given config backend, + * matching a regular expression. + * + * This behaves like `git_config_foreach_match` except that only config + * entries from the given backend entry are enumerated. + * + * The regular expression is applied case-sensitively on the normalized form of + * the variable name: the section and variable parts are lower-cased. The + * subsection is left unchanged. + * + * @param backend where to get the variables from + * @param regexp regular expression to match against config names (can be NULL) + * @param callback the function to call on each variable + * @param payload the data to pass to the callback + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_config_backend_foreach_match( + git_config_backend *backend, + const char *regexp, + git_config_foreach_cb callback, + void *payload); + + +/** + * Lock the backend with the highest priority + * + * Locking disallows anybody else from writing to that backend. Any + * updates made after locking will not be visible to a reader until + * the file is unlocked. + * + * You can apply the changes by calling `git_transaction_commit()` + * before freeing the transaction. Either of these actions will unlock + * the config. + * + * @param tx the resulting transaction, use this to commit or undo the + * changes + * @param cfg the configuration in which to lock + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_lock(git_transaction **tx, git_config *cfg); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/cred_helpers.h b/include/git2/cred_helpers.h index 7c213c8ddf9..3721b6d8a7d 100644 --- a/include/git2/cred_helpers.h +++ b/include/git2/cred_helpers.h @@ -7,44 +7,9 @@ #ifndef INCLUDE_git_cred_helpers_h__ #define INCLUDE_git_cred_helpers_h__ -#include "git2/transport.h" - -/** - * @file git2/cred_helpers.h - * @brief Utility functions for credential management - * @defgroup git_cred_helpers credential management helpers - * @ingroup Git - * @{ - */ -GIT_BEGIN_DECL - -/** - * Payload for git_cred_stock_userpass_plaintext. - */ -typedef struct git_cred_userpass_payload { - char *username; - char *password; -} git_cred_userpass_payload; - - -/** - * Stock callback usable as a git_cred_acquire_cb. This calls - * git_cred_userpass_plaintext_new unless the protocol has not specified - * GIT_CREDTYPE_USERPASS_PLAINTEXT as an allowed type. - * - * @param cred The newly created credential object. - * @param url The resource for which we are demanding a credential. - * @param allowed_types A bitmask stating which cred types are OK to return. - * @param payload The payload provided when specifying this callback. (This is - * interpreted as a `git_cred_userpass_payload*`.) - */ -GIT_EXTERN(int) git_cred_userpass( - git_cred **cred, - const char *url, - unsigned int allowed_types, - void *payload); - +/* These declarations have moved. */ +#ifndef GIT_DEPRECATE_HARD +# include "git2/credential_helpers.h" +#endif -/** @} */ -GIT_END_DECL #endif diff --git a/include/git2/credential.h b/include/git2/credential.h new file mode 100644 index 00000000000..33755ca916d --- /dev/null +++ b/include/git2/credential.h @@ -0,0 +1,343 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_credential_h__ +#define INCLUDE_git_credential_h__ + +#include "common.h" + +/** + * @file git2/credential.h + * @brief Authentication and credential management + * @defgroup git_credential Authentication & credential management + * @ingroup Git + * + * Credentials specify how to authenticate to a remote system + * over HTTPS or SSH. + * @{ + */ +GIT_BEGIN_DECL + +/** + * Supported credential types + * + * This represents the various types of authentication methods supported by + * the library. + */ +typedef enum { + /** + * A vanilla user/password request + * @see git_credential_userpass_plaintext_new + */ + GIT_CREDENTIAL_USERPASS_PLAINTEXT = (1u << 0), + + /** + * An SSH key-based authentication request + * @see git_credential_ssh_key_new + */ + GIT_CREDENTIAL_SSH_KEY = (1u << 1), + + /** + * An SSH key-based authentication request, with a custom signature + * @see git_credential_ssh_custom_new + */ + GIT_CREDENTIAL_SSH_CUSTOM = (1u << 2), + + /** + * An NTLM/Negotiate-based authentication request. + * @see git_credential_default + */ + GIT_CREDENTIAL_DEFAULT = (1u << 3), + + /** + * An SSH interactive authentication request + * @see git_credential_ssh_interactive_new + */ + GIT_CREDENTIAL_SSH_INTERACTIVE = (1u << 4), + + /** + * Username-only authentication request + * + * Used as a pre-authentication step if the underlying transport + * (eg. SSH, with no username in its URL) does not know which username + * to use. + * + * @see git_credential_username_new + */ + GIT_CREDENTIAL_USERNAME = (1u << 5), + + /** + * An SSH key-based authentication request + * + * Allows credentials to be read from memory instead of files. + * Note that because of differences in crypto backend support, it might + * not be functional. + * + * @see git_credential_ssh_key_memory_new + */ + GIT_CREDENTIAL_SSH_MEMORY = (1u << 6) +} git_credential_t; + +/** + * The base structure for all credential types + */ +typedef struct git_credential git_credential; + +typedef struct git_credential_userpass_plaintext git_credential_userpass_plaintext; + +/** Username-only credential information */ +typedef struct git_credential_username git_credential_username; + +/** A key for NTLM/Kerberos "default" credentials */ +typedef struct git_credential git_credential_default; + +/** + * A ssh key from disk + */ +typedef struct git_credential_ssh_key git_credential_ssh_key; + +/** + * Keyboard-interactive based ssh authentication + */ +typedef struct git_credential_ssh_interactive git_credential_ssh_interactive; + +/** + * A key with a custom signature function + */ +typedef struct git_credential_ssh_custom git_credential_ssh_custom; + +/** + * Credential acquisition callback. + * + * This callback is usually involved any time another system might need + * authentication. As such, you are expected to provide a valid + * git_credential object back, depending on allowed_types (a + * git_credential_t bitmask). + * + * Note that most authentication details are your responsibility - this + * callback will be called until the authentication succeeds, or you report + * an error. As such, it's easy to get in a loop if you fail to stop providing + * the same incorrect credentials. + * + * @param[out] out The newly created credential object. + * @param url The resource for which we are demanding a credential. + * @param username_from_url The username that was embedded in a "user\@host" + * remote url, or NULL if not included. + * @param allowed_types A bitmask stating which credential types are OK to return. + * @param payload The payload provided when specifying this callback. + * @return 0 for success, < 0 to indicate an error, > 0 to indicate + * no credential was acquired + */ +typedef int GIT_CALLBACK(git_credential_acquire_cb)( + git_credential **out, + const char *url, + const char *username_from_url, + unsigned int allowed_types, + void *payload); + +/** + * Free a credential. + * + * This is only necessary if you own the object; that is, if you are a + * transport. + * + * @param cred the object to free + */ +GIT_EXTERN(void) git_credential_free(git_credential *cred); + +/** + * Check whether a credential object contains username information. + * + * @param cred object to check + * @return 1 if the credential object has non-NULL username, 0 otherwise + */ +GIT_EXTERN(int) git_credential_has_username(git_credential *cred); + +/** + * Return the username associated with a credential object. + * + * @param cred object to check + * @return the credential username, or NULL if not applicable + */ +GIT_EXTERN(const char *) git_credential_get_username(git_credential *cred); + +/** + * Create a new plain-text username and password credential object. + * The supplied credential parameter will be internally duplicated. + * + * @param out The newly created credential object. + * @param username The username of the credential. + * @param password The password of the credential. + * @return 0 for success or an error code for failure + */ +GIT_EXTERN(int) git_credential_userpass_plaintext_new( + git_credential **out, + const char *username, + const char *password); + +/** + * Create a "default" credential usable for Negotiate mechanisms like NTLM + * or Kerberos authentication. + * + * @param out The newly created credential object. + * @return 0 for success or an error code for failure + */ +GIT_EXTERN(int) git_credential_default_new(git_credential **out); + +/** + * Create a credential to specify a username. + * + * This is used with ssh authentication to query for the username if + * none is specified in the url. + * + * @param out The newly created credential object. + * @param username The username to authenticate with + * @return 0 for success or an error code for failure + */ +GIT_EXTERN(int) git_credential_username_new(git_credential **out, const char *username); + +/** + * Create a new passphrase-protected ssh key credential object. + * The supplied credential parameter will be internally duplicated. + * + * @param out The newly created credential object. + * @param username username to use to authenticate + * @param publickey The path to the public key of the credential. + * @param privatekey The path to the private key of the credential. + * @param passphrase The passphrase of the credential. + * @return 0 for success or an error code for failure + */ +GIT_EXTERN(int) git_credential_ssh_key_new( + git_credential **out, + const char *username, + const char *publickey, + const char *privatekey, + const char *passphrase); + +/** + * Create a new ssh key credential object reading the keys from memory. + * + * @param out The newly created credential object. + * @param username username to use to authenticate. + * @param publickey The public key of the credential. + * @param privatekey The private key of the credential. + * @param passphrase The passphrase of the credential. + * @return 0 for success or an error code for failure + */ +GIT_EXTERN(int) git_credential_ssh_key_memory_new( + git_credential **out, + const char *username, + const char *publickey, + const char *privatekey, + const char *passphrase); + +/* + * If the user hasn't included libssh2.h before git2.h, we need to + * define a few types for the callback signatures. + */ +#ifndef LIBSSH2_VERSION +typedef struct _LIBSSH2_SESSION LIBSSH2_SESSION; +typedef struct _LIBSSH2_USERAUTH_KBDINT_PROMPT LIBSSH2_USERAUTH_KBDINT_PROMPT; +typedef struct _LIBSSH2_USERAUTH_KBDINT_RESPONSE LIBSSH2_USERAUTH_KBDINT_RESPONSE; +#endif + +/** + * Callback for interactive SSH credentials. + * + * @param name the name + * @param name_len the length of the name + * @param instruction the authentication instruction + * @param instruction_len the length of the instruction + * @param num_prompts the number of prompts + * @param prompts the prompts + * @param responses the responses + * @param abstract the abstract + */ +typedef void GIT_CALLBACK(git_credential_ssh_interactive_cb)( + const char *name, + int name_len, + const char *instruction, int instruction_len, + int num_prompts, const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, + LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, + void **abstract); + + +/** + * Create a new ssh keyboard-interactive based credential object. + * The supplied credential parameter will be internally duplicated. + * + * @param out The newly created credential object. + * @param username Username to use to authenticate. + * @param prompt_callback The callback method used for prompts. + * @param payload Additional data to pass to the callback. + * @return 0 for success or an error code for failure. + */ +GIT_EXTERN(int) git_credential_ssh_interactive_new( + git_credential **out, + const char *username, + git_credential_ssh_interactive_cb prompt_callback, + void *payload); + +/** + * Create a new ssh key credential object used for querying an ssh-agent. + * The supplied credential parameter will be internally duplicated. + * + * @param out The newly created credential object. + * @param username username to use to authenticate + * @return 0 for success or an error code for failure + */ +GIT_EXTERN(int) git_credential_ssh_key_from_agent( + git_credential **out, + const char *username); + +/** + * Callback for credential signing. + * + * @param session the libssh2 session + * @param sig the signature + * @param sig_len the length of the signature + * @param data the data + * @param data_len the length of the data + * @param abstract the abstract + * @return 0 for success, < 0 to indicate an error, > 0 to indicate + * no credential was acquired + */ +typedef int GIT_CALLBACK(git_credential_sign_cb)( + LIBSSH2_SESSION *session, + unsigned char **sig, size_t *sig_len, + const unsigned char *data, size_t data_len, + void **abstract); + +/** + * Create an ssh key credential with a custom signing function. + * + * This lets you use your own function to sign the challenge. + * + * This function and its credential type is provided for completeness + * and wraps `libssh2_userauth_publickey()`, which is undocumented. + * + * The supplied credential parameter will be internally duplicated. + * + * @param out The newly created credential object. + * @param username username to use to authenticate + * @param publickey The bytes of the public key. + * @param publickey_len The length of the public key in bytes. + * @param sign_callback The callback method to sign the data during the challenge. + * @param payload Additional data to pass to the callback. + * @return 0 for success or an error code for failure + */ +GIT_EXTERN(int) git_credential_ssh_custom_new( + git_credential **out, + const char *username, + const char *publickey, + size_t publickey_len, + git_credential_sign_cb sign_callback, + void *payload); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/credential_helpers.h b/include/git2/credential_helpers.h new file mode 100644 index 00000000000..706558d5bd0 --- /dev/null +++ b/include/git2/credential_helpers.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_credential_helpers_h__ +#define INCLUDE_git_credential_helpers_h__ + +#include "transport.h" + +/** + * @file git2/credential_helpers.h + * @brief Utility functions for credential management + * @defgroup git_credential_helpers credential management helpers + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Payload for git_credential_userpass_plaintext. + */ +typedef struct git_credential_userpass_payload { + const char *username; + const char *password; +} git_credential_userpass_payload; + + +/** + * Stock callback usable as a git_credential_acquire_cb. This calls + * git_cred_userpass_plaintext_new unless the protocol has not specified + * `GIT_CREDENTIAL_USERPASS_PLAINTEXT` as an allowed type. + * + * @param out The newly created credential object. + * @param url The resource for which we are demanding a credential. + * @param user_from_url The username that was embedded in a "user\@host" + * remote url, or NULL if not included. + * @param allowed_types A bitmask stating which credential types are OK to return. + * @param payload The payload provided when specifying this callback. (This is + * interpreted as a `git_credential_userpass_payload*`.) + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_credential_userpass( + git_credential **out, + const char *url, + const char *user_from_url, + unsigned int allowed_types, + void *payload); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/deprecated.h b/include/git2/deprecated.h new file mode 100644 index 00000000000..930d4f7d6f0 --- /dev/null +++ b/include/git2/deprecated.h @@ -0,0 +1,1090 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_deprecated_h__ +#define INCLUDE_git_deprecated_h__ + +#include "attr.h" +#include "config.h" +#include "common.h" +#include "blame.h" +#include "buffer.h" +#include "checkout.h" +#include "cherrypick.h" +#include "clone.h" +#include "describe.h" +#include "diff.h" +#include "errors.h" +#include "filter.h" +#include "index.h" +#include "indexer.h" +#include "merge.h" +#include "object.h" +#include "proxy.h" +#include "refs.h" +#include "rebase.h" +#include "remote.h" +#include "trace.h" +#include "repository.h" +#include "revert.h" +#include "revparse.h" +#include "stash.h" +#include "status.h" +#include "submodule.h" +#include "worktree.h" +#include "credential.h" +#include "credential_helpers.h" + +/* + * Users can avoid deprecated functions by defining `GIT_DEPRECATE_HARD`. + */ +#ifndef GIT_DEPRECATE_HARD + +/* + * The credential structures are now opaque by default, and their + * definition has moved into the `sys/credential.h` header; include + * them here for backward compatibility. + */ +#include "sys/credential.h" + +/** + * @file git2/deprecated.h + * @brief Deprecated functions and values + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** @name Deprecated Attribute Constants + * + * These enumeration values are retained for backward compatibility. + * The newer versions of these functions should be preferred in all + * new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +/** @deprecated use GIT_ATTR_VALUE_UNSPECIFIED */ +#define GIT_ATTR_UNSPECIFIED_T GIT_ATTR_VALUE_UNSPECIFIED +/** @deprecated use GIT_ATTR_VALUE_TRUE */ +#define GIT_ATTR_TRUE_T GIT_ATTR_VALUE_TRUE +/** @deprecated use GIT_ATTR_VALUE_FALSE */ +#define GIT_ATTR_FALSE_T GIT_ATTR_VALUE_FALSE +/** @deprecated use GIT_ATTR_VALUE_STRING */ +#define GIT_ATTR_VALUE_T GIT_ATTR_VALUE_STRING + +/** @deprecated use GIT_ATTR_IS_TRUE */ +#define GIT_ATTR_TRUE(attr) GIT_ATTR_IS_TRUE(attr) +/** @deprecated use GIT_ATTR_IS_FALSE */ +#define GIT_ATTR_FALSE(attr) GIT_ATTR_IS_FALSE(attr) +/** @deprecated use GIT_ATTR_IS_UNSPECIFIED */ +#define GIT_ATTR_UNSPECIFIED(attr) GIT_ATTR_IS_UNSPECIFIED(attr) + +/** @deprecated use git_attr_value_t */ +typedef git_attr_value_t git_attr_t; + +/**@}*/ + +/** @name Deprecated Blob Functions and Constants + * + * These functions and enumeration values are retained for backward + * compatibility. The newer versions of these functions and values + * should be preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +/** @deprecated use GIT_BLOB_FILTER_ATTRIBUTES_FROM_HEAD */ +#define GIT_BLOB_FILTER_ATTTRIBUTES_FROM_HEAD GIT_BLOB_FILTER_ATTRIBUTES_FROM_HEAD + +GIT_EXTERN(int) git_blob_create_fromworkdir(git_oid *id, git_repository *repo, const char *relative_path); +GIT_EXTERN(int) git_blob_create_fromdisk(git_oid *id, git_repository *repo, const char *path); +GIT_EXTERN(int) git_blob_create_fromstream( + git_writestream **out, + git_repository *repo, + const char *hintpath); +GIT_EXTERN(int) git_blob_create_fromstream_commit( + git_oid *out, + git_writestream *stream); +GIT_EXTERN(int) git_blob_create_frombuffer( + git_oid *id, git_repository *repo, const void *buffer, size_t len); + +/** Deprecated in favor of `git_blob_filter`. + * + * @deprecated Use git_blob_filter + * @see git_blob_filter + */ +GIT_EXTERN(int) git_blob_filtered_content( + git_buf *out, + git_blob *blob, + const char *as_path, + int check_for_binary_data); + +/**@}*/ + +/** @name Deprecated Filter Functions + * + * These functions are retained for backward compatibility. The + * newer versions of these functions should be preferred in all + * new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +/** Deprecated in favor of `git_filter_list_stream_buffer`. + * + * @deprecated Use git_filter_list_stream_buffer + * @see Use git_filter_list_stream_buffer + */ +GIT_EXTERN(int) git_filter_list_stream_data( + git_filter_list *filters, + git_buf *data, + git_writestream *target); + +/** Deprecated in favor of `git_filter_list_apply_to_buffer`. + * + * @deprecated Use git_filter_list_apply_to_buffer + * @see Use git_filter_list_apply_to_buffer + */ +GIT_EXTERN(int) git_filter_list_apply_to_data( + git_buf *out, + git_filter_list *filters, + git_buf *in); + +/**@}*/ + +/** @name Deprecated Tree Functions + * + * These functions are retained for backward compatibility. The + * newer versions of these functions and values should be preferred + * in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +/** + * Write the contents of the tree builder as a tree object. + * This is an alias of `git_treebuilder_write` and is preserved + * for backward compatibility. + * + * This function is deprecated, but there is no plan to remove this + * function at this time. + * + * @deprecated Use git_treebuilder_write + * @see git_treebuilder_write + */ +GIT_EXTERN(int) git_treebuilder_write_with_buffer( + git_oid *oid, git_treebuilder *bld, git_buf *tree); + +/**@}*/ + +/** @name Deprecated Buffer Functions + * + * These functions and enumeration values are retained for backward + * compatibility. The newer versions of these functions should be + * preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +/** + * Static initializer for git_buf from static buffer + */ +#define GIT_BUF_INIT_CONST(STR,LEN) { (char *)(STR), 0, (size_t)(LEN) } + +/** + * Resize the buffer allocation to make more space. + * + * This will attempt to grow the buffer to accommodate the target size. + * + * If the buffer refers to memory that was not allocated by libgit2 (i.e. + * the `asize` field is zero), then `ptr` will be replaced with a newly + * allocated block of data. Be careful so that memory allocated by the + * caller is not lost. As a special variant, if you pass `target_size` as + * 0 and the memory is not allocated by libgit2, this will allocate a new + * buffer of size `size` and copy the external data into it. + * + * Currently, this will never shrink a buffer, only expand it. + * + * If the allocation fails, this will return an error and the buffer will be + * marked as invalid for future operations, invaliding the contents. + * + * @param buffer The buffer to be resized; may or may not be allocated yet + * @param target_size The desired available size + * @return 0 on success, -1 on allocation failure + */ +GIT_EXTERN(int) git_buf_grow(git_buf *buffer, size_t target_size); + +/** + * Set buffer to a copy of some raw data. + * + * @param buffer The buffer to set + * @param data The data to copy into the buffer + * @param datalen The length of the data to copy into the buffer + * @return 0 on success, -1 on allocation failure + */ +GIT_EXTERN(int) git_buf_set( + git_buf *buffer, const void *data, size_t datalen); + +/** +* Check quickly if buffer looks like it contains binary data +* +* @param buf Buffer to check +* @return 1 if buffer looks like non-text data +*/ +GIT_EXTERN(int) git_buf_is_binary(const git_buf *buf); + +/** +* Check quickly if buffer contains a NUL byte +* +* @param buf Buffer to check +* @return 1 if buffer contains a NUL byte +*/ +GIT_EXTERN(int) git_buf_contains_nul(const git_buf *buf); + +/** + * Free the memory referred to by the git_buf. This is an alias of + * `git_buf_dispose` and is preserved for backward compatibility. + * + * This function is deprecated, but there is no plan to remove this + * function at this time. + * + * @deprecated Use git_buf_dispose + * @see git_buf_dispose + */ +GIT_EXTERN(void) git_buf_free(git_buf *buffer); + +/**@}*/ + +/** @name Deprecated Commit Definitions + */ +/**@{*/ + +/** + * Provide a commit signature during commit creation. + * + * Callers should instead define a `git_commit_create_cb` that + * generates a commit buffer using `git_commit_create_buffer`, sign + * that buffer and call `git_commit_create_with_signature`. + * + * @deprecated use a `git_commit_create_cb` instead + */ +typedef int (*git_commit_signing_cb)( + git_buf *signature, + git_buf *signature_field, + const char *commit_content, + void *payload); + +/**@}*/ + +/** @name Deprecated Config Functions and Constants + */ +/**@{*/ + +/** @deprecated use GIT_CONFIGMAP_FALSE */ +#define GIT_CVAR_FALSE GIT_CONFIGMAP_FALSE +/** @deprecated use GIT_CONFIGMAP_TRUE */ +#define GIT_CVAR_TRUE GIT_CONFIGMAP_TRUE +/** @deprecated use GIT_CONFIGMAP_INT32 */ +#define GIT_CVAR_INT32 GIT_CONFIGMAP_INT32 +/** @deprecated use GIT_CONFIGMAP_STRING */ +#define GIT_CVAR_STRING GIT_CONFIGMAP_STRING + +/** @deprecated use git_cvar_map */ +typedef git_configmap git_cvar_map; + +/**@}*/ + +/** @name Deprecated Diff Functions and Constants + * + * These functions and enumeration values are retained for backward + * compatibility. The newer versions of these functions and values + * should be preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +/** + * Formatting options for diff e-mail generation + */ +typedef enum { + /** Normal patch, the default */ + GIT_DIFF_FORMAT_EMAIL_NONE = 0, + + /** Don't insert "[PATCH]" in the subject header*/ + GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER = (1 << 0) +} git_diff_format_email_flags_t; + +/** + * Options for controlling the formatting of the generated e-mail. + * + * @deprecated use `git_email_create_options` + */ +typedef struct { + unsigned int version; + + /** see `git_diff_format_email_flags_t` above */ + uint32_t flags; + + /** This patch number */ + size_t patch_no; + + /** Total number of patches in this series */ + size_t total_patches; + + /** id to use for the commit */ + const git_oid *id; + + /** Summary of the change */ + const char *summary; + + /** Commit message's body */ + const char *body; + + /** Author of the change */ + const git_signature *author; +} git_diff_format_email_options; + +/** @deprecated use `git_email_create_options` */ +#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION 1 +/** @deprecated use `git_email_create_options` */ +#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT {GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, 0, 1, 1, NULL, NULL, NULL, NULL} + +/** + * Create an e-mail ready patch from a diff. + * + * @deprecated git_email_create_from_diff + * @see git_email_create_from_diff + */ +GIT_EXTERN(int) git_diff_format_email( + git_buf *out, + git_diff *diff, + const git_diff_format_email_options *opts); + +/** + * Create an e-mail ready patch for a commit. + * + * @deprecated git_email_create_from_commit + * @see git_email_create_from_commit + */ +GIT_EXTERN(int) git_diff_commit_as_email( + git_buf *out, + git_repository *repo, + git_commit *commit, + size_t patch_no, + size_t total_patches, + uint32_t flags, + const git_diff_options *diff_opts); + +/** + * Initialize git_diff_format_email_options structure + * + * Initializes a `git_diff_format_email_options` with default values. Equivalent + * to creating an instance with GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT. + * + * @param opts The `git_blame_options` struct to initialize. + * @param version The struct version; pass `GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_diff_format_email_options_init( + git_diff_format_email_options *opts, + unsigned int version); + +/**@}*/ + +/** @name Deprecated Error Functions and Constants + * + * These functions and enumeration values are retained for backward + * compatibility. The newer versions of these functions and values + * should be preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +/** @deprecated use `GIT_ERROR_NONE` */ +#define GITERR_NONE GIT_ERROR_NONE +/** @deprecated use `GIT_ERROR_NOMEMORY` */ +#define GITERR_NOMEMORY GIT_ERROR_NOMEMORY +/** @deprecated use `GIT_ERROR_OS` */ +#define GITERR_OS GIT_ERROR_OS +/** @deprecated use `GIT_ERROR_INVALID` */ +#define GITERR_INVALID GIT_ERROR_INVALID +/** @deprecated use `GIT_ERROR_REFERENCE` */ +#define GITERR_REFERENCE GIT_ERROR_REFERENCE +/** @deprecated use `GIT_ERROR_ZLIB` */ +#define GITERR_ZLIB GIT_ERROR_ZLIB +/** @deprecated use `GIT_ERROR_REPOSITORY` */ +#define GITERR_REPOSITORY GIT_ERROR_REPOSITORY +/** @deprecated use `GIT_ERROR_CONFIG` */ +#define GITERR_CONFIG GIT_ERROR_CONFIG +/** @deprecated use `GIT_ERROR_REGEX` */ +#define GITERR_REGEX GIT_ERROR_REGEX +/** @deprecated use `GIT_ERROR_ODB` */ +#define GITERR_ODB GIT_ERROR_ODB +/** @deprecated use `GIT_ERROR_INDEX` */ +#define GITERR_INDEX GIT_ERROR_INDEX +/** @deprecated use `GIT_ERROR_OBJECT` */ +#define GITERR_OBJECT GIT_ERROR_OBJECT +/** @deprecated use `GIT_ERROR_NET` */ +#define GITERR_NET GIT_ERROR_NET +/** @deprecated use `GIT_ERROR_TAG` */ +#define GITERR_TAG GIT_ERROR_TAG +/** @deprecated use `GIT_ERROR_TREE` */ +#define GITERR_TREE GIT_ERROR_TREE +/** @deprecated use `GIT_ERROR_INDEXER` */ +#define GITERR_INDEXER GIT_ERROR_INDEXER +/** @deprecated use `GIT_ERROR_SSL` */ +#define GITERR_SSL GIT_ERROR_SSL +/** @deprecated use `GIT_ERROR_SUBMODULE` */ +#define GITERR_SUBMODULE GIT_ERROR_SUBMODULE +/** @deprecated use `GIT_ERROR_THREAD` */ +#define GITERR_THREAD GIT_ERROR_THREAD +/** @deprecated use `GIT_ERROR_STASH` */ +#define GITERR_STASH GIT_ERROR_STASH +/** @deprecated use `GIT_ERROR_CHECKOUT` */ +#define GITERR_CHECKOUT GIT_ERROR_CHECKOUT +/** @deprecated use `GIT_ERROR_FETCHHEAD` */ +#define GITERR_FETCHHEAD GIT_ERROR_FETCHHEAD +/** @deprecated use `GIT_ERROR_MERGE` */ +#define GITERR_MERGE GIT_ERROR_MERGE +/** @deprecated use `GIT_ERROR_SSH` */ +#define GITERR_SSH GIT_ERROR_SSH +/** @deprecated use `GIT_ERROR_FILTER` */ +#define GITERR_FILTER GIT_ERROR_FILTER +/** @deprecated use `GIT_ERROR_REVERT` */ +#define GITERR_REVERT GIT_ERROR_REVERT +/** @deprecated use `GIT_ERROR_CALLBACK` */ +#define GITERR_CALLBACK GIT_ERROR_CALLBACK +/** @deprecated use `GIT_ERROR_CHERRYPICK` */ +#define GITERR_CHERRYPICK GIT_ERROR_CHERRYPICK +/** @deprecated use `GIT_ERROR_DESCRIBE` */ +#define GITERR_DESCRIBE GIT_ERROR_DESCRIBE +/** @deprecated use `GIT_ERROR_REBASE` */ +#define GITERR_REBASE GIT_ERROR_REBASE +/** @deprecated use `GIT_ERROR_FILESYSTEM` */ +#define GITERR_FILESYSTEM GIT_ERROR_FILESYSTEM +/** @deprecated use `GIT_ERROR_PATCH` */ +#define GITERR_PATCH GIT_ERROR_PATCH +/** @deprecated use `GIT_ERROR_WORKTREE` */ +#define GITERR_WORKTREE GIT_ERROR_WORKTREE +/** @deprecated use `GIT_ERROR_SHA1` */ +#define GITERR_SHA1 GIT_ERROR_SHA1 +/** @deprecated use `GIT_ERROR_SHA` */ +#define GIT_ERROR_SHA1 GIT_ERROR_SHA + +/** + * Return the last `git_error` object that was generated for the + * current thread. This is an alias of `git_error_last` and is + * preserved for backward compatibility. + * + * This function is deprecated, but there is no plan to remove this + * function at this time. + * + * @deprecated Use git_error_last + * @see git_error_last + */ +GIT_EXTERN(const git_error *) giterr_last(void); + +/** + * Clear the last error. This is an alias of `git_error_clear` and is + * preserved for backward compatibility. + * + * This function is deprecated, but there is no plan to remove this + * function at this time. + * + * @deprecated Use git_error_clear + * @see git_error_clear + */ +GIT_EXTERN(void) giterr_clear(void); + +/** + * Sets the error message to the given string. This is an alias of + * `git_error_set_str` and is preserved for backward compatibility. + * + * This function is deprecated, but there is no plan to remove this + * function at this time. + * + * @deprecated Use git_error_set_str + * @see git_error_set_str + */ +GIT_EXTERN(void) giterr_set_str(int error_class, const char *string); + +/** + * Indicates that an out-of-memory situation occurred. This is an alias + * of `git_error_set_oom` and is preserved for backward compatibility. + * + * This function is deprecated, but there is no plan to remove this + * function at this time. + * + * @deprecated Use git_error_set_oom + * @see git_error_set_oom + */ +GIT_EXTERN(void) giterr_set_oom(void); + +/**@}*/ + +/** @name Deprecated Index Functions and Constants + * + * These functions and enumeration values are retained for backward + * compatibility. The newer versions of these values should be + * preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +/* The git_idxentry_extended_flag_t enum */ +/** @deprecated use `GIT_INDEX_ENTRY_NAMEMASK` */ +#define GIT_IDXENTRY_NAMEMASK GIT_INDEX_ENTRY_NAMEMASK +/** @deprecated use `GIT_INDEX_ENTRY_STAGEMASK` */ +#define GIT_IDXENTRY_STAGEMASK GIT_INDEX_ENTRY_STAGEMASK +/** @deprecated use `GIT_INDEX_ENTRY_STAGESHIFT` */ +#define GIT_IDXENTRY_STAGESHIFT GIT_INDEX_ENTRY_STAGESHIFT + +/* The git_indxentry_flag_t enum */ +/** @deprecated use `GIT_INDEX_ENTRY_EXTENDED` */ +#define GIT_IDXENTRY_EXTENDED GIT_INDEX_ENTRY_EXTENDED +/** @deprecated use `GIT_INDEX_ENTRY_VALID` */ +#define GIT_IDXENTRY_VALID GIT_INDEX_ENTRY_VALID + +/** @deprecated use `GIT_INDEX_ENTRY_STAGE` */ +#define GIT_IDXENTRY_STAGE(E) GIT_INDEX_ENTRY_STAGE(E) +/** @deprecated use `GIT_INDEX_ENTRY_STAGE_SET` */ +#define GIT_IDXENTRY_STAGE_SET(E,S) GIT_INDEX_ENTRY_STAGE_SET(E,S) + +/* The git_idxentry_extended_flag_t enum */ +/** @deprecated use `GIT_INDEX_ENTRY_INTENT_TO_ADD` */ +#define GIT_IDXENTRY_INTENT_TO_ADD GIT_INDEX_ENTRY_INTENT_TO_ADD +/** @deprecated use `GIT_INDEX_ENTRY_SKIP_WORKTREE` */ +#define GIT_IDXENTRY_SKIP_WORKTREE GIT_INDEX_ENTRY_SKIP_WORKTREE +/** @deprecated use `GIT_INDEX_ENTRY_INTENT_TO_ADD | GIT_INDEX_ENTRY_SKIP_WORKTREE` */ +#define GIT_IDXENTRY_EXTENDED_FLAGS (GIT_INDEX_ENTRY_INTENT_TO_ADD | GIT_INDEX_ENTRY_SKIP_WORKTREE) +/** @deprecated this value is not public */ +#define GIT_IDXENTRY_EXTENDED2 (1 << 15) +/** @deprecated this value is not public */ +#define GIT_IDXENTRY_UPDATE (1 << 0) +/** @deprecated this value is not public */ +#define GIT_IDXENTRY_REMOVE (1 << 1) +/** @deprecated this value is not public */ +#define GIT_IDXENTRY_UPTODATE (1 << 2) +/** @deprecated this value is not public */ +#define GIT_IDXENTRY_ADDED (1 << 3) +/** @deprecated this value is not public */ +#define GIT_IDXENTRY_HASHED (1 << 4) +/** @deprecated this value is not public */ +#define GIT_IDXENTRY_UNHASHED (1 << 5) +/** @deprecated this value is not public */ +#define GIT_IDXENTRY_WT_REMOVE (1 << 6) +/** @deprecated this value is not public */ +#define GIT_IDXENTRY_CONFLICTED (1 << 7) +/** @deprecated this value is not public */ +#define GIT_IDXENTRY_UNPACKED (1 << 8) +/** @deprecated this value is not public */ +#define GIT_IDXENTRY_NEW_SKIP_WORKTREE (1 << 9) + +/* The git_index_capability_t enum */ +/** @deprecated use `GIT_INDEX_CAPABILITY_IGNORE_CASE` */ +#define GIT_INDEXCAP_IGNORE_CASE GIT_INDEX_CAPABILITY_IGNORE_CASE +/** @deprecated use `GIT_INDEX_CAPABILITY_NO_FILEMODE` */ +#define GIT_INDEXCAP_NO_FILEMODE GIT_INDEX_CAPABILITY_NO_FILEMODE +/** @deprecated use `GIT_INDEX_CAPABILITY_NO_SYMLINKS` */ +#define GIT_INDEXCAP_NO_SYMLINKS GIT_INDEX_CAPABILITY_NO_SYMLINKS +/** @deprecated use `GIT_INDEX_CAPABILITY_FROM_OWNER` */ +#define GIT_INDEXCAP_FROM_OWNER GIT_INDEX_CAPABILITY_FROM_OWNER + +GIT_EXTERN(int) git_index_add_frombuffer( + git_index *index, + const git_index_entry *entry, + const void *buffer, size_t len); + +/**@}*/ + +/** @name Deprecated Object Constants + * + * These enumeration values are retained for backward compatibility. The + * newer versions of these values should be preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +/** @deprecate use `git_object_t` */ +#define git_otype git_object_t + +/** @deprecate use `GIT_OBJECT_ANY` */ +#define GIT_OBJ_ANY GIT_OBJECT_ANY +/** @deprecate use `GIT_OBJECT_INVALID` */ +#define GIT_OBJ_BAD GIT_OBJECT_INVALID +/** @deprecated this value is not public */ +#define GIT_OBJ__EXT1 0 +/** @deprecate use `GIT_OBJECT_COMMIT` */ +#define GIT_OBJ_COMMIT GIT_OBJECT_COMMIT +/** @deprecate use `GIT_OBJECT_TREE` */ +#define GIT_OBJ_TREE GIT_OBJECT_TREE +/** @deprecate use `GIT_OBJECT_BLOB` */ +#define GIT_OBJ_BLOB GIT_OBJECT_BLOB +/** @deprecate use `GIT_OBJECT_TAG` */ +#define GIT_OBJ_TAG GIT_OBJECT_TAG +/** @deprecated this value is not public */ +#define GIT_OBJ__EXT2 5 +/** @deprecate use `GIT_OBJECT_OFS_DELTA` */ +#define GIT_OBJ_OFS_DELTA GIT_OBJECT_OFS_DELTA +/** @deprecate use `GIT_OBJECT_REF_DELTA` */ +#define GIT_OBJ_REF_DELTA GIT_OBJECT_REF_DELTA + +/** + * Get the size in bytes for the structure which + * acts as an in-memory representation of any given + * object type. + * + * For all the core types, this would the equivalent + * of calling `sizeof(git_commit)` if the core types + * were not opaque on the external API. + * + * @param type object type to get its size + * @return size in bytes of the object + */ +GIT_EXTERN(size_t) git_object__size(git_object_t type); + +/** + * Determine if the given git_object_t is a valid object type. + * + * @deprecated use `git_object_type_is_valid` + * + * @param type object type to test. + * @return 1 if the type represents a valid object type, 0 otherwise + */ +GIT_EXTERN(int) git_object_typeisloose(git_object_t type); + +/**@}*/ + +/** @name Deprecated Remote Functions + * + * These functions are retained for backward compatibility. The newer + * versions of these functions should be preferred in all new code. + * + * There is no plan to remove these backward compatibility functions at + * this time. + */ +/**@{*/ + +/** + * Ensure the remote name is well-formed. + * + * @deprecated Use git_remote_name_is_valid + * @param remote_name name to be checked. + * @return 1 if the reference name is acceptable; 0 if it isn't + */ +GIT_EXTERN(int) git_remote_is_valid_name(const char *remote_name); + +/**@}*/ + +/** @name Deprecated Reference Functions and Constants + * + * These functions and enumeration values are retained for backward + * compatibility. The newer versions of these values should be + * preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + + /** Basic type of any Git reference. */ +/** @deprecate use `git_reference_t` */ +#define git_ref_t git_reference_t +/** @deprecate use `git_reference_format_t` */ +#define git_reference_normalize_t git_reference_format_t + +/** @deprecate use `GIT_REFERENCE_INVALID` */ +#define GIT_REF_INVALID GIT_REFERENCE_INVALID +/** @deprecate use `GIT_REFERENCE_DIRECT` */ +#define GIT_REF_OID GIT_REFERENCE_DIRECT +/** @deprecate use `GIT_REFERENCE_SYMBOLIC` */ +#define GIT_REF_SYMBOLIC GIT_REFERENCE_SYMBOLIC +/** @deprecate use `GIT_REFERENCE_ALL` */ +#define GIT_REF_LISTALL GIT_REFERENCE_ALL + +/** @deprecate use `GIT_REFERENCE_FORMAT_NORMAL` */ +#define GIT_REF_FORMAT_NORMAL GIT_REFERENCE_FORMAT_NORMAL +/** @deprecate use `GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL` */ +#define GIT_REF_FORMAT_ALLOW_ONELEVEL GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL +/** @deprecate use `GIT_REFERENCE_FORMAT_REFSPEC_PATTERN` */ +#define GIT_REF_FORMAT_REFSPEC_PATTERN GIT_REFERENCE_FORMAT_REFSPEC_PATTERN +/** @deprecate use `GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND` */ +#define GIT_REF_FORMAT_REFSPEC_SHORTHAND GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND + +/** + * Ensure the reference name is well-formed. + * + * Valid reference names must follow one of two patterns: + * + * 1. Top-level names must contain only capital letters and underscores, + * and must begin and end with a letter. (e.g. "HEAD", "ORIG_HEAD"). + * 2. Names prefixed with "refs/" can be almost anything. You must avoid + * the characters '~', '^', ':', '\\', '?', '[', and '*', and the + * sequences ".." and "@{" which have special meaning to revparse. + * + * @deprecated Use git_reference_name_is_valid + * @param refname name to be checked. + * @return 1 if the reference name is acceptable; 0 if it isn't + */ +GIT_EXTERN(int) git_reference_is_valid_name(const char *refname); + +GIT_EXTERN(int) git_tag_create_frombuffer( + git_oid *oid, + git_repository *repo, + const char *buffer, + int force); + +/**@}*/ + +/** @name Deprecated Repository Constants + * + * These enumeration values are retained for backward compatibility. + */ + +/** + * @deprecated This option is deprecated; it is now implied when + * a separate working directory is specified to `git_repository_init`. + */ +#define GIT_REPOSITORY_INIT_NO_DOTGIT_DIR 0 + +/** @name Deprecated Revspec Constants + * + * These enumeration values are retained for backward compatibility. + * The newer versions of these values should be preferred in all new + * code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +typedef git_revspec_t git_revparse_mode_t; + +/** @deprecated use `GIT_REVSPEC_SINGLE` */ +#define GIT_REVPARSE_SINGLE GIT_REVSPEC_SINGLE +/** @deprecated use `GIT_REVSPEC_RANGE` */ +#define GIT_REVPARSE_RANGE GIT_REVSPEC_RANGE +/** @deprecated use `GIT_REVSPEC_MERGE_BASE` */ +#define GIT_REVPARSE_MERGE_BASE GIT_REVSPEC_MERGE_BASE + +/**@}*/ + +/** @name Deprecated Credential Types + * + * These types are retained for backward compatibility. The newer + * versions of these values should be preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +typedef git_credential git_cred; +typedef git_credential_userpass_plaintext git_cred_userpass_plaintext; +typedef git_credential_username git_cred_username; +typedef git_credential_default git_cred_default; +typedef git_credential_ssh_key git_cred_ssh_key; +typedef git_credential_ssh_interactive git_cred_ssh_interactive; +typedef git_credential_ssh_custom git_cred_ssh_custom; + +typedef git_credential_acquire_cb git_cred_acquire_cb; +typedef git_credential_sign_cb git_cred_sign_callback; +typedef git_credential_sign_cb git_cred_sign_cb; +typedef git_credential_ssh_interactive_cb git_cred_ssh_interactive_callback; +typedef git_credential_ssh_interactive_cb git_cred_ssh_interactive_cb; + +/** @deprecated use `git_credential_t` */ +#define git_credtype_t git_credential_t + +/** @deprecated use `GIT_CREDENTIAL_USERPASS_PLAINTEXT` */ +#define GIT_CREDTYPE_USERPASS_PLAINTEXT GIT_CREDENTIAL_USERPASS_PLAINTEXT +/** @deprecated use `GIT_CREDENTIAL_SSH_KEY` */ +#define GIT_CREDTYPE_SSH_KEY GIT_CREDENTIAL_SSH_KEY +/** @deprecated use `GIT_CREDENTIAL_SSH_CUSTOM` */ +#define GIT_CREDTYPE_SSH_CUSTOM GIT_CREDENTIAL_SSH_CUSTOM +/** @deprecated use `GIT_CREDENTIAL_DEFAULT` */ +#define GIT_CREDTYPE_DEFAULT GIT_CREDENTIAL_DEFAULT +/** @deprecated use `GIT_CREDENTIAL_SSH_INTERACTIVE` */ +#define GIT_CREDTYPE_SSH_INTERACTIVE GIT_CREDENTIAL_SSH_INTERACTIVE +/** @deprecated use `GIT_CREDENTIAL_USERNAME` */ +#define GIT_CREDTYPE_USERNAME GIT_CREDENTIAL_USERNAME +/** @deprecated use `GIT_CREDENTIAL_SSH_MEMORY` */ +#define GIT_CREDTYPE_SSH_MEMORY GIT_CREDENTIAL_SSH_MEMORY + +GIT_EXTERN(void) git_cred_free(git_credential *cred); +GIT_EXTERN(int) git_cred_has_username(git_credential *cred); +GIT_EXTERN(const char *) git_cred_get_username(git_credential *cred); +GIT_EXTERN(int) git_cred_userpass_plaintext_new( + git_credential **out, + const char *username, + const char *password); +GIT_EXTERN(int) git_cred_default_new(git_credential **out); +GIT_EXTERN(int) git_cred_username_new(git_credential **out, const char *username); +GIT_EXTERN(int) git_cred_ssh_key_new( + git_credential **out, + const char *username, + const char *publickey, + const char *privatekey, + const char *passphrase); +GIT_EXTERN(int) git_cred_ssh_key_memory_new( + git_credential **out, + const char *username, + const char *publickey, + const char *privatekey, + const char *passphrase); +GIT_EXTERN(int) git_cred_ssh_interactive_new( + git_credential **out, + const char *username, + git_credential_ssh_interactive_cb prompt_callback, + void *payload); +GIT_EXTERN(int) git_cred_ssh_key_from_agent( + git_credential **out, + const char *username); +GIT_EXTERN(int) git_cred_ssh_custom_new( + git_credential **out, + const char *username, + const char *publickey, + size_t publickey_len, + git_credential_sign_cb sign_callback, + void *payload); + +/* Deprecated Credential Helper Types */ + +typedef git_credential_userpass_payload git_cred_userpass_payload; + +GIT_EXTERN(int) git_cred_userpass( + git_credential **out, + const char *url, + const char *user_from_url, + unsigned int allowed_types, + void *payload); + +/**@}*/ + +/** @name Deprecated Trace Callback Types + * + * These types are retained for backward compatibility. The newer + * versions of these values should be preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +typedef git_trace_cb git_trace_callback; + +/**@}*/ + +/** @name Deprecated Object ID Types + * + * These types are retained for backward compatibility. The newer + * versions of these values should be preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +#ifndef GIT_EXPERIMENTAL_SHA256 +/** Deprecated OID "raw size" definition */ +# define GIT_OID_RAWSZ GIT_OID_SHA1_SIZE +/** Deprecated OID "hex size" definition */ +# define GIT_OID_HEXSZ GIT_OID_SHA1_HEXSIZE +/** Deprecated OID "hex zero" definition */ +# define GIT_OID_HEX_ZERO GIT_OID_SHA1_HEXZERO +#endif + +GIT_EXTERN(int) git_oid_iszero(const git_oid *id); + +/**@}*/ + +/** @name Deprecated OID Array Functions + * + * These types are retained for backward compatibility. The newer + * versions of these values should be preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +/** + * Free the memory referred to by the git_oidarray. This is an alias of + * `git_oidarray_dispose` and is preserved for backward compatibility. + * + * This function is deprecated, but there is no plan to remove this + * function at this time. + * + * @deprecated Use git_oidarray_dispose + * @see git_oidarray_dispose + */ +GIT_EXTERN(void) git_oidarray_free(git_oidarray *array); + +/**@}*/ + +/** @name Deprecated Transfer Progress Types + * + * These types are retained for backward compatibility. The newer + * versions of these values should be preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +/** + * This structure is used to provide callers information about the + * progress of indexing a packfile. + * + * This type is deprecated, but there is no plan to remove this + * type definition at this time. + */ +typedef git_indexer_progress git_transfer_progress; + +/** + * Type definition for progress callbacks during indexing. + * + * This type is deprecated, but there is no plan to remove this + * type definition at this time. + */ +typedef git_indexer_progress_cb git_transfer_progress_cb; + +/** + * Type definition for push transfer progress callbacks. + * + * This type is deprecated, but there is no plan to remove this + * type definition at this time. + */ +typedef git_push_transfer_progress_cb git_push_transfer_progress; + + /** The type of a remote completion event */ +#define git_remote_completion_type git_remote_completion_t + +/** + * Callback for listing the remote heads + */ +typedef int GIT_CALLBACK(git_headlist_cb)(git_remote_head *rhead, void *payload); + +/**@}*/ + +/** @name Deprecated String Array Functions + * + * These types are retained for backward compatibility. The newer + * versions of these values should be preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +/** + * Copy a string array object from source to target. + * + * This function is deprecated, but there is no plan to remove this + * function at this time. + * + * @param tgt target + * @param src source + * @return 0 on success, < 0 on allocation failure + */ +GIT_EXTERN(int) git_strarray_copy(git_strarray *tgt, const git_strarray *src); + +/** + * Free the memory referred to by the git_strarray. This is an alias of + * `git_strarray_dispose` and is preserved for backward compatibility. + * + * This function is deprecated, but there is no plan to remove this + * function at this time. + * + * @deprecated Use git_strarray_dispose + * @see git_strarray_dispose + */ +GIT_EXTERN(void) git_strarray_free(git_strarray *array); + +/**@}*/ + +/** @name Deprecated Version Constants + * + * These constants are retained for backward compatibility. The newer + * versions of these constants should be preferred in all new code. + * + * There is no plan to remove these backward compatibility constants at + * this time. + */ +/**@{*/ + +#define LIBGIT2_VER_MAJOR LIBGIT2_VERSION_MAJOR +#define LIBGIT2_VER_MINOR LIBGIT2_VERSION_MINOR +#define LIBGIT2_VER_REVISION LIBGIT2_VERSION_REVISION +#define LIBGIT2_VER_PATCH LIBGIT2_VERSION_PATCH +#define LIBGIT2_VER_PRERELEASE LIBGIT2_VERSION_PRERELEASE + +/**@}*/ + +/** @name Deprecated Options Initialization Functions + * + * These functions are retained for backward compatibility. The newer + * versions of these functions should be preferred in all new code. + * + * There is no plan to remove these backward compatibility functions at + * this time. + */ +/**@{*/ + +GIT_EXTERN(int) git_blame_init_options(git_blame_options *opts, unsigned int version); +GIT_EXTERN(int) git_checkout_init_options(git_checkout_options *opts, unsigned int version); +GIT_EXTERN(int) git_cherrypick_init_options(git_cherrypick_options *opts, unsigned int version); +GIT_EXTERN(int) git_clone_init_options(git_clone_options *opts, unsigned int version); +GIT_EXTERN(int) git_describe_init_options(git_describe_options *opts, unsigned int version); +GIT_EXTERN(int) git_describe_init_format_options(git_describe_format_options *opts, unsigned int version); +GIT_EXTERN(int) git_diff_init_options(git_diff_options *opts, unsigned int version); +GIT_EXTERN(int) git_diff_find_init_options(git_diff_find_options *opts, unsigned int version); +GIT_EXTERN(int) git_diff_format_email_init_options(git_diff_format_email_options *opts, unsigned int version); +GIT_EXTERN(int) git_diff_patchid_init_options(git_diff_patchid_options *opts, unsigned int version); +GIT_EXTERN(int) git_fetch_init_options(git_fetch_options *opts, unsigned int version); +GIT_EXTERN(int) git_indexer_init_options(git_indexer_options *opts, unsigned int version); +GIT_EXTERN(int) git_merge_init_options(git_merge_options *opts, unsigned int version); +GIT_EXTERN(int) git_merge_file_init_input(git_merge_file_input *input, unsigned int version); +GIT_EXTERN(int) git_merge_file_init_options(git_merge_file_options *opts, unsigned int version); +GIT_EXTERN(int) git_proxy_init_options(git_proxy_options *opts, unsigned int version); +GIT_EXTERN(int) git_push_init_options(git_push_options *opts, unsigned int version); +GIT_EXTERN(int) git_rebase_init_options(git_rebase_options *opts, unsigned int version); +GIT_EXTERN(int) git_remote_create_init_options(git_remote_create_options *opts, unsigned int version); +GIT_EXTERN(int) git_repository_init_init_options(git_repository_init_options *opts, unsigned int version); +GIT_EXTERN(int) git_revert_init_options(git_revert_options *opts, unsigned int version); +GIT_EXTERN(int) git_stash_apply_init_options(git_stash_apply_options *opts, unsigned int version); +GIT_EXTERN(int) git_status_init_options(git_status_options *opts, unsigned int version); +GIT_EXTERN(int) git_submodule_update_init_options(git_submodule_update_options *opts, unsigned int version); +GIT_EXTERN(int) git_worktree_add_init_options(git_worktree_add_options *opts, unsigned int version); +GIT_EXTERN(int) git_worktree_prune_init_options(git_worktree_prune_options *opts, unsigned int version); + +/**@}*/ + +/** @} */ +GIT_END_DECL + +#endif + +#endif diff --git a/include/git2/describe.h b/include/git2/describe.h new file mode 100644 index 00000000000..938c470d272 --- /dev/null +++ b/include/git2/describe.h @@ -0,0 +1,206 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_describe_h__ +#define INCLUDE_git_describe_h__ + +#include "common.h" +#include "types.h" +#include "buffer.h" + +/** + * @file git2/describe.h + * @brief Describe a commit in reference to tags + * @defgroup git_describe Git describing routines + * @ingroup Git + * @{ + * + * Describe a commit, showing information about how the current commit + * relates to the tags. This can be useful for showing how the current + * commit has changed from a particular tagged version of the repository. + */ +GIT_BEGIN_DECL + +/** + * Reference lookup strategy + * + * These behave like the --tags and --all options to git-describe, + * namely they say to look for any reference in either refs/tags/ or + * refs/ respectively. + */ +typedef enum { + GIT_DESCRIBE_DEFAULT, + GIT_DESCRIBE_TAGS, + GIT_DESCRIBE_ALL +} git_describe_strategy_t; + +/** + * Describe options structure + * + * Initialize with `GIT_DESCRIBE_OPTIONS_INIT`. Alternatively, you can + * use `git_describe_options_init`. + * + */ +typedef struct git_describe_options { + unsigned int version; + + unsigned int max_candidates_tags; /**< default: 10 */ + unsigned int describe_strategy; /**< default: GIT_DESCRIBE_DEFAULT */ + const char *pattern; + /** + * When calculating the distance from the matching tag or + * reference, only walk down the first-parent ancestry. + */ + int only_follow_first_parent; + /** + * If no matching tag or reference is found, the describe + * operation would normally fail. If this option is set, it + * will instead fall back to showing the full id of the + * commit. + */ + int show_commit_oid_as_fallback; +} git_describe_options; + +/** Default maximum candidate tags */ +#define GIT_DESCRIBE_DEFAULT_MAX_CANDIDATES_TAGS 10 +/** Default abbreviated size */ +#define GIT_DESCRIBE_DEFAULT_ABBREVIATED_SIZE 7 + +/** Current version for the `git_describe_options` structure */ +#define GIT_DESCRIBE_OPTIONS_VERSION 1 + +/** Static constructor for `git_describe_options` */ +#define GIT_DESCRIBE_OPTIONS_INIT { \ + GIT_DESCRIBE_OPTIONS_VERSION, \ + GIT_DESCRIBE_DEFAULT_MAX_CANDIDATES_TAGS, \ +} + +/** + * Initialize git_describe_options structure + * + * Initializes a `git_describe_options` with default values. Equivalent to creating + * an instance with GIT_DESCRIBE_OPTIONS_INIT. + * + * @param opts The `git_describe_options` struct to initialize. + * @param version The struct version; pass `GIT_DESCRIBE_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_describe_options_init(git_describe_options *opts, unsigned int version); + +/** + * Describe format options structure + * + * Initialize with `GIT_DESCRIBE_FORMAT_OPTIONS_INIT`. Alternatively, you can + * use `git_describe_format_options_init`. + * + */ +typedef struct { + unsigned int version; + + /** + * Size of the abbreviated commit id to use. This value is the + * lower bound for the length of the abbreviated string. The + * default is 7. + */ + unsigned int abbreviated_size; + + /** + * Set to use the long format even when a shorter name could be used. + */ + int always_use_long_format; + + /** + * If the workdir is dirty and this is set, this string will + * be appended to the description string. + */ + const char *dirty_suffix; +} git_describe_format_options; + +/** Current version for the `git_describe_format_options` structure */ +#define GIT_DESCRIBE_FORMAT_OPTIONS_VERSION 1 + +/** Static constructor for `git_describe_format_options` */ +#define GIT_DESCRIBE_FORMAT_OPTIONS_INIT { \ + GIT_DESCRIBE_FORMAT_OPTIONS_VERSION, \ + GIT_DESCRIBE_DEFAULT_ABBREVIATED_SIZE, \ + } + +/** + * Initialize git_describe_format_options structure + * + * Initializes a `git_describe_format_options` with default values. Equivalent to creating + * an instance with GIT_DESCRIBE_FORMAT_OPTIONS_INIT. + * + * @param opts The `git_describe_format_options` struct to initialize. + * @param version The struct version; pass `GIT_DESCRIBE_FORMAT_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_describe_format_options_init(git_describe_format_options *opts, unsigned int version); + +/** + * A struct that stores the result of a describe operation. + */ +typedef struct git_describe_result git_describe_result; + +/** + * Describe a commit + * + * Perform the describe operation on the given committish object. + * + * @param result pointer to store the result. You must free this once + * you're done with it. + * @param committish a committish to describe + * @param opts the lookup options (or NULL for defaults) + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_describe_commit( + git_describe_result **result, + git_object *committish, + git_describe_options *opts); + +/** + * Describe a commit + * + * Perform the describe operation on the current commit and the + * worktree. After performing describe on HEAD, a status is run and the + * description is considered to be dirty if there are. + * + * @param out pointer to store the result. You must free this once + * you're done with it. + * @param repo the repository in which to perform the describe + * @param opts the lookup options (or NULL for defaults) + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_describe_workdir( + git_describe_result **out, + git_repository *repo, + git_describe_options *opts); + +/** + * Print the describe result to a buffer + * + * @param out The buffer to store the result + * @param result the result from `git_describe_commit()` or + * `git_describe_workdir()`. + * @param opts the formatting options (or NULL for defaults) + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_describe_format( + git_buf *out, + const git_describe_result *result, + const git_describe_format_options *opts); + +/** + * Free the describe result. + * + * @param result The result to free. + */ +GIT_EXTERN(void) git_describe_result_free(git_describe_result *result); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/diff.h b/include/git2/diff.h index 81c41df04f2..856a28f7943 100644 --- a/include/git2/diff.h +++ b/include/git2/diff.h @@ -15,47 +15,7 @@ /** * @file git2/diff.h - * @brief Git tree and file differencing routines. - * - * Overview - * -------- - * - * Calculating diffs is generally done in two phases: building a diff list - * then traversing the diff list. This makes is easier to share logic - * across the various types of diffs (tree vs tree, workdir vs index, etc.), - * and also allows you to insert optional diff list post-processing phases, - * such as rename detected, in between the steps. When you are done with a - * diff list object, it must be freed. - * - * Terminology - * ----------- - * - * To understand the diff APIs, you should know the following terms: - * - * - A `diff` or `diff list` represents the cumulative list of differences - * between two snapshots of a repository (possibly filtered by a set of - * file name patterns). This is the `git_diff_list` object. - * - A `delta` is a file pair with an old and new revision. The old version - * may be absent if the file was just created and the new version may be - * absent if the file was deleted. A diff is mostly just a list of deltas. - * - A `binary` file / delta is a file (or pair) for which no text diffs - * should be generated. A diff list can contain delta entries that are - * binary, but no diff content will be output for those files. There is - * a base heuristic for binary detection and you can further tune the - * behavior with git attributes or diff flags and option settings. - * - A `hunk` is a span of modified lines in a delta along with some stable - * surrounding context. You can configure the amount of context and other - * properties of how hunks are generated. Each hunk also comes with a - * header that described where it starts and ends in both the old and new - * versions in the delta. - * - A `line` is a range of characters inside a hunk. It could be a context - * line (i.e. in both old and new versions), an added line (i.e. only in - * the new version), or a removed line (i.e. only in the old version). - * Unfortunately, we don't know anything about the encoding of data in the - * file being diffed, so we cannot tell you much about the line content. - * Line data will not be NUL-byte terminated, however, because it will be - * just a span of bytes inside the larger file. - * + * @brief Indicate the differences between two versions of the repository * @ingroup Git * @{ */ @@ -68,178 +28,268 @@ GIT_BEGIN_DECL typedef enum { /** Normal diff, the default */ GIT_DIFF_NORMAL = 0, + + /* + * Options controlling which files will be in the diff + */ + /** Reverse the sides of the diff */ - GIT_DIFF_REVERSE = (1 << 0), - /** Treat all files as text, disabling binary attributes & detection */ - GIT_DIFF_FORCE_TEXT = (1 << 1), - /** Ignore all whitespace */ - GIT_DIFF_IGNORE_WHITESPACE = (1 << 2), - /** Ignore changes in amount of whitespace */ - GIT_DIFF_IGNORE_WHITESPACE_CHANGE = (1 << 3), - /** Ignore whitespace at end of line */ - GIT_DIFF_IGNORE_WHITESPACE_EOL = (1 << 4), - /** Exclude submodules from the diff completely */ - GIT_DIFF_IGNORE_SUBMODULES = (1 << 5), - /** Use the "patience diff" algorithm (currently unimplemented) */ - GIT_DIFF_PATIENCE = (1 << 6), - /** Include ignored files in the diff list */ - GIT_DIFF_INCLUDE_IGNORED = (1 << 7), - /** Include untracked files in the diff list */ - GIT_DIFF_INCLUDE_UNTRACKED = (1 << 8), - /** Include unmodified files in the diff list */ - GIT_DIFF_INCLUDE_UNMODIFIED = (1 << 9), - /** Even with the GIT_DIFF_INCLUDE_UNTRACKED flag, when an untracked - * directory is found, only a single entry for the directory is added - * to the diff list; with this flag, all files under the directory will - * be included, too. - */ - GIT_DIFF_RECURSE_UNTRACKED_DIRS = (1 << 10), - /** If the pathspec is set in the diff options, this flags means to - * apply it as an exact match instead of as an fnmatch pattern. - */ - GIT_DIFF_DISABLE_PATHSPEC_MATCH = (1 << 11), - /** Use case insensitive filename comparisons */ - GIT_DIFF_DELTAS_ARE_ICASE = (1 << 12), - /** When generating patch text, include the content of untracked files */ - GIT_DIFF_INCLUDE_UNTRACKED_CONTENT = (1 << 13), - /** Disable updating of the `binary` flag in delta records. This is - * useful when iterating over a diff if you don't need hunk and data - * callbacks and want to avoid having to load file completely. + GIT_DIFF_REVERSE = (1u << 0), + + /** Include ignored files in the diff */ + GIT_DIFF_INCLUDE_IGNORED = (1u << 1), + + /** Even with GIT_DIFF_INCLUDE_IGNORED, an entire ignored directory + * will be marked with only a single entry in the diff; this flag + * adds all files under the directory as IGNORED entries, too. + */ + GIT_DIFF_RECURSE_IGNORED_DIRS = (1u << 2), + + /** Include untracked files in the diff */ + GIT_DIFF_INCLUDE_UNTRACKED = (1u << 3), + + /** Even with GIT_DIFF_INCLUDE_UNTRACKED, an entire untracked + * directory will be marked with only a single entry in the diff + * (a la what core Git does in `git status`); this flag adds *all* + * files under untracked directories as UNTRACKED entries, too. */ - GIT_DIFF_SKIP_BINARY_CHECK = (1 << 14), + GIT_DIFF_RECURSE_UNTRACKED_DIRS = (1u << 4), + + /** Include unmodified files in the diff */ + GIT_DIFF_INCLUDE_UNMODIFIED = (1u << 5), + /** Normally, a type change between files will be converted into a * DELETED record for the old and an ADDED record for the new; this * options enabled the generation of TYPECHANGE delta records. */ - GIT_DIFF_INCLUDE_TYPECHANGE = (1 << 15), + GIT_DIFF_INCLUDE_TYPECHANGE = (1u << 6), + /** Even with GIT_DIFF_INCLUDE_TYPECHANGE, blob->tree changes still * generally show as a DELETED blob. This flag tries to correctly * label blob->tree transitions as TYPECHANGE records with new_file's * mode set to tree. Note: the tree SHA will not be available. */ - GIT_DIFF_INCLUDE_TYPECHANGE_TREES = (1 << 16), + GIT_DIFF_INCLUDE_TYPECHANGE_TREES = (1u << 7), + /** Ignore file mode changes */ - GIT_DIFF_IGNORE_FILEMODE = (1 << 17), + GIT_DIFF_IGNORE_FILEMODE = (1u << 8), + + /** Treat all submodules as unmodified */ + GIT_DIFF_IGNORE_SUBMODULES = (1u << 9), + + /** Use case insensitive filename comparisons */ + GIT_DIFF_IGNORE_CASE = (1u << 10), + + /** May be combined with `GIT_DIFF_IGNORE_CASE` to specify that a file + * that has changed case will be returned as an add/delete pair. + */ + GIT_DIFF_INCLUDE_CASECHANGE = (1u << 11), + + /** If the pathspec is set in the diff options, this flags indicates + * that the paths will be treated as literal paths instead of + * fnmatch patterns. Each path in the list must either be a full + * path to a file or a directory. (A trailing slash indicates that + * the path will _only_ match a directory). If a directory is + * specified, all children will be included. + */ + GIT_DIFF_DISABLE_PATHSPEC_MATCH = (1u << 12), + + /** Disable updating of the `binary` flag in delta records. This is + * useful when iterating over a diff if you don't need hunk and data + * callbacks and want to avoid having to load file completely. + */ + GIT_DIFF_SKIP_BINARY_CHECK = (1u << 13), + + /** When diff finds an untracked directory, to match the behavior of + * core Git, it scans the contents for IGNORED and UNTRACKED files. + * If *all* contents are IGNORED, then the directory is IGNORED; if + * any contents are not IGNORED, then the directory is UNTRACKED. + * This is extra work that may not matter in many cases. This flag + * turns off that scan and immediately labels an untracked directory + * as UNTRACKED (changing the behavior to not match core Git). + */ + GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS = (1u << 14), + + /** When diff finds a file in the working directory with stat + * information different from the index, but the OID ends up being the + * same, write the correct stat information into the index. Note: + * without this flag, diff will always leave the index untouched. + */ + GIT_DIFF_UPDATE_INDEX = (1u << 15), + + /** Include unreadable files in the diff */ + GIT_DIFF_INCLUDE_UNREADABLE = (1u << 16), + + /** Include unreadable files in the diff */ + GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED = (1u << 17), + + /* + * Options controlling how output will be generated + */ + + /** Use a heuristic that takes indentation and whitespace into account + * which generally can produce better diffs when dealing with ambiguous + * diff hunks. + */ + GIT_DIFF_INDENT_HEURISTIC = (1u << 18), + + /** Ignore blank lines */ + GIT_DIFF_IGNORE_BLANK_LINES = (1u << 19), + + /** Treat all files as text, disabling binary attributes & detection */ + GIT_DIFF_FORCE_TEXT = (1u << 20), + /** Treat all files as binary, disabling text diffs */ + GIT_DIFF_FORCE_BINARY = (1u << 21), + + /** Ignore all whitespace */ + GIT_DIFF_IGNORE_WHITESPACE = (1u << 22), + /** Ignore changes in amount of whitespace */ + GIT_DIFF_IGNORE_WHITESPACE_CHANGE = (1u << 23), + /** Ignore whitespace at end of line */ + GIT_DIFF_IGNORE_WHITESPACE_EOL = (1u << 24), + + /** When generating patch text, include the content of untracked + * files. This automatically turns on GIT_DIFF_INCLUDE_UNTRACKED but + * it does not turn on GIT_DIFF_RECURSE_UNTRACKED_DIRS. Add that + * flag if you want the content of every single UNTRACKED file. + */ + GIT_DIFF_SHOW_UNTRACKED_CONTENT = (1u << 25), + + /** When generating output, include the names of unmodified files if + * they are included in the git_diff. Normally these are skipped in + * the formats that list files (e.g. name-only, name-status, raw). + * Even with this, these will not be included in patch format. + */ + GIT_DIFF_SHOW_UNMODIFIED = (1u << 26), + + /** Use the "patience diff" algorithm */ + GIT_DIFF_PATIENCE = (1u << 28), + /** Take extra time to find minimal diff */ + GIT_DIFF_MINIMAL = (1u << 29), + + /** Include the necessary deflate / delta information so that `git-apply` + * can apply given diff information to binary files. + */ + GIT_DIFF_SHOW_BINARY = (1u << 30) } git_diff_option_t; /** - * Structure describing options about how the diff should be executed. + * The diff object that contains all individual file deltas. * - * Setting all values of the structure to zero will yield the default - * values. Similarly, passing NULL for the options structure will - * give the defaults. The default values are marked below. + * A `diff` represents the cumulative list of differences between two + * snapshots of a repository (possibly filtered by a set of file name + * patterns). * - * - `flags` is a combination of the `git_diff_option_t` values above - * - `context_lines` is the number of unchanged lines that define the - * boundary of a hunk (and to display before and after) - * - `interhunk_lines` is the maximum number of unchanged lines between - * hunk boundaries before the hunks will be merged into a one. - * - `old_prefix` is the virtual "directory" to prefix to old file names - * in hunk headers (default "a") - * - `new_prefix` is the virtual "directory" to prefix to new file names - * in hunk headers (default "b") - * - `pathspec` is an array of paths / fnmatch patterns to constrain diff - * - `max_size` is a file size (in bytes) above which a blob will be marked - * as binary automatically; pass a negative value to disable. - */ -typedef struct { - unsigned int version; /**< version for the struct */ - uint32_t flags; /**< defaults to GIT_DIFF_NORMAL */ - uint16_t context_lines; /**< defaults to 3 */ - uint16_t interhunk_lines; /**< defaults to 0 */ - const char *old_prefix; /**< defaults to "a" */ - const char *new_prefix; /**< defaults to "b" */ - git_strarray pathspec; /**< defaults to include all paths */ - git_off_t max_size; /**< defaults to 512MB */ -} git_diff_options; - -#define GIT_DIFF_OPTIONS_VERSION 1 -#define GIT_DIFF_OPTIONS_INIT {GIT_DIFF_OPTIONS_VERSION} - -/** - * The diff list object that contains all individual file deltas. + * Calculating diffs is generally done in two phases: building a list of + * diffs then traversing it. This makes is easier to share logic across + * the various types of diffs (tree vs tree, workdir vs index, etc.), and + * also allows you to insert optional diff post-processing phases, + * such as rename detection, in between the steps. When you are done with + * a diff object, it must be freed. * * This is an opaque structure which will be allocated by one of the diff - * generator functions below (such as `git_diff_tree_to_tree`). You are + * generator functions below (such as `git_diff_tree_to_tree`). You are * responsible for releasing the object memory when done, using the - * `git_diff_list_free()` function. + * `git_diff_free()` function. + * */ -typedef struct git_diff_list git_diff_list; +typedef struct git_diff git_diff; /** - * Flags for the file object on each side of a diff. + * Flags for the delta object and the file objects on each side. * - * Note: most of these flags are just for **internal** consumption by - * libgit2, but some of them may be interesting to external users. + * These flags are used for both the `flags` value of the `git_diff_delta` + * and the flags for the `git_diff_file` objects representing the old and + * new sides of the delta. Values outside of this public range should be + * considered reserved for internal or future use. */ typedef enum { - GIT_DIFF_FILE_VALID_OID = (1 << 0), /** `oid` value is known correct */ - GIT_DIFF_FILE_FREE_PATH = (1 << 1), /** `path` is allocated memory */ - GIT_DIFF_FILE_BINARY = (1 << 2), /** should be considered binary data */ - GIT_DIFF_FILE_NOT_BINARY = (1 << 3), /** should be considered text data */ - GIT_DIFF_FILE_FREE_DATA = (1 << 4), /** internal file data is allocated */ - GIT_DIFF_FILE_UNMAP_DATA = (1 << 5), /** internal file data is mmap'ed */ - GIT_DIFF_FILE_NO_DATA = (1 << 6), /** file data should not be loaded */ -} git_diff_file_flag_t; + GIT_DIFF_FLAG_BINARY = (1u << 0), /**< file(s) treated as binary data */ + GIT_DIFF_FLAG_NOT_BINARY = (1u << 1), /**< file(s) treated as text data */ + GIT_DIFF_FLAG_VALID_ID = (1u << 2), /**< `id` value is known correct */ + GIT_DIFF_FLAG_EXISTS = (1u << 3), /**< file exists at this side of the delta */ + GIT_DIFF_FLAG_VALID_SIZE = (1u << 4) /**< file size value is known correct */ +} git_diff_flag_t; /** * What type of change is described by a git_diff_delta? * * `GIT_DELTA_RENAMED` and `GIT_DELTA_COPIED` will only show up if you run - * `git_diff_find_similar()` on the diff list object. + * `git_diff_find_similar()` on the diff object. * * `GIT_DELTA_TYPECHANGE` only shows up given `GIT_DIFF_INCLUDE_TYPECHANGE` * in the option flags (otherwise type changes will be split into ADDED / * DELETED pairs). */ typedef enum { - GIT_DELTA_UNMODIFIED = 0, /** no changes */ - GIT_DELTA_ADDED = 1, /** entry does not exist in old version */ - GIT_DELTA_DELETED = 2, /** entry does not exist in new version */ - GIT_DELTA_MODIFIED = 3, /** entry content changed between old and new */ - GIT_DELTA_RENAMED = 4, /** entry was renamed between old and new */ - GIT_DELTA_COPIED = 5, /** entry was copied from another old entry */ - GIT_DELTA_IGNORED = 6, /** entry is ignored item in workdir */ - GIT_DELTA_UNTRACKED = 7, /** entry is untracked item in workdir */ - GIT_DELTA_TYPECHANGE = 8, /** type of entry changed between old and new */ + GIT_DELTA_UNMODIFIED = 0, /**< no changes */ + GIT_DELTA_ADDED = 1, /**< entry does not exist in old version */ + GIT_DELTA_DELETED = 2, /**< entry does not exist in new version */ + GIT_DELTA_MODIFIED = 3, /**< entry content changed between old and new */ + GIT_DELTA_RENAMED = 4, /**< entry was renamed between old and new */ + GIT_DELTA_COPIED = 5, /**< entry was copied from another old entry */ + GIT_DELTA_IGNORED = 6, /**< entry is ignored item in workdir */ + GIT_DELTA_UNTRACKED = 7, /**< entry is untracked item in workdir */ + GIT_DELTA_TYPECHANGE = 8, /**< type of entry changed between old and new */ + GIT_DELTA_UNREADABLE = 9, /**< entry is unreadable */ + GIT_DELTA_CONFLICTED = 10 /**< entry in the index is conflicted */ } git_delta_t; /** - * Description of one side of a diff entry. - * - * Although this is called a "file", it may actually represent a file, a - * symbolic link, a submodule commit id, or even a tree (although that only - * if you are tracking type changes or ignored/untracked directories). - * - * The `oid` is the `git_oid` of the item. If the entry represents an - * absent side of a diff (e.g. the `old_file` of a `GIT_DELTA_ADDED` delta), - * then the oid will be zeroes. - * - * `path` is the NUL-terminated path to the entry relative to the working - * directory of the repository. - * - * `size` is the size of the entry in bytes. - * - * `flags` is a combination of the `git_diff_file_flag_t` types, but those - * are largely internal values. + * Description of one side of a delta. * - * `mode` is, roughly, the stat() `st_mode` value for the item. This will - * be restricted to one of the `git_filemode_t` values. + * Although this is called a "file", it could represent a file, a symbolic + * link, a submodule commit id, or even a tree (although that only if you + * are tracking type changes or ignored/untracked directories). */ typedef struct { - git_oid oid; - const char *path; - git_off_t size; - unsigned int flags; - uint16_t mode; + /** + * The `git_oid` of the item. If the entry represents an + * absent side of a diff (e.g. the `old_file` of a `GIT_DELTA_ADDED` delta), + * then the oid will be zeroes. + */ + git_oid id; + + /** + * The NUL-terminated path to the entry relative to the working + * directory of the repository. + */ + const char *path; + + /** + * The size of the entry in bytes. + */ + git_object_size_t size; + + /** + * A combination of the `git_diff_flag_t` types + */ + uint32_t flags; + + /** + * Roughly, the stat() `st_mode` value for the item. This will + * be restricted to one of the `git_filemode_t` values. + */ + uint16_t mode; + + /** + * Represents the known length of the `id` field, when + * converted to a hex string. It is generally `GIT_OID_SHA1_HEXSIZE`, unless this + * delta was created from reading a patch file, in which case it may be + * abbreviated to something reasonable, like 7 characters. + */ + uint16_t id_abbrev; } git_diff_file; /** * Description of changes to one entry. * - * When iterating over a diff list object, this will be passed to most - * callback functions and you can use the contents to understand exactly - * what has changed. + * A `delta` is a file pair with an old and new revision. The old version + * may be absent if the file was just created and the new version may be + * absent if the file was deleted. A diff is mostly just a list of deltas. + * + * When iterating over a diff, this will be passed to most callbacks and + * you can use the contents to understand exactly what has changed. * * The `old_file` represents the "from" side of the diff and the `new_file` * represents to "to" side of the diff. What those means depend on the @@ -253,76 +303,360 @@ typedef struct { * * Under some circumstances, in the name of efficiency, not all fields will * be filled in, but we generally try to fill in as much as possible. One - * example is that the "binary" field will not examine file contents if you - * do not pass in hunk and/or line callbacks to the diff foreach iteration - * function. It will just use the git attributes for those files. + * example is that the "flags" field may not have either the `BINARY` or the + * `NOT_BINARY` flag set to avoid examining file contents if you do not pass + * in hunk and/or line callbacks to the diff foreach iteration function. It + * will just use the git attributes for those files. + * + * The similarity score is zero unless you call `git_diff_find_similar()` + * which does a similarity analysis of files in the diff. Use that + * function to do rename and copy detection, and to split heavily modified + * files in add/delete pairs. After that call, deltas with a status of + * GIT_DELTA_RENAMED or GIT_DELTA_COPIED will have a similarity score + * between 0 and 100 indicating how similar the old and new sides are. + * + * If you ask `git_diff_find_similar` to find heavily modified files to + * break, but to not *actually* break the records, then GIT_DELTA_MODIFIED + * records may have a non-zero similarity score if the self-similarity is + * below the split threshold. To display this value like core Git, invert + * the score (a la `printf("M%03d", 100 - delta->similarity)`). */ typedef struct { + git_delta_t status; + uint32_t flags; /**< git_diff_flag_t values */ + uint16_t similarity; /**< for RENAMED and COPIED, value 0-100 */ + uint16_t nfiles; /**< number of files in this delta */ git_diff_file old_file; git_diff_file new_file; - git_delta_t status; - unsigned int similarity; /**< for RENAMED and COPIED, value 0-100 */ - int binary; } git_diff_delta; +/** + * Diff notification callback function. + * + * The callback will be called for each file, just before the `git_diff_delta` + * gets inserted into the diff. + * + * When the callback: + * - returns < 0, the diff process will be aborted. + * - returns > 0, the delta will not be inserted into the diff, but the + * diff process continues. + * - returns 0, the delta is inserted into the diff, and the diff process + * continues. + * + * @param diff_so_far the diff structure as it currently exists + * @param delta_to_add the delta that is to be added + * @param matched_pathspec the pathspec + * @param payload the user-specified callback payload + * @return 0 on success, 1 to skip this delta, or an error code + */ +typedef int GIT_CALLBACK(git_diff_notify_cb)( + const git_diff *diff_so_far, + const git_diff_delta *delta_to_add, + const char *matched_pathspec, + void *payload); + +/** + * Diff progress callback. + * + * Called before each file comparison. + * + * @param diff_so_far The diff being generated. + * @param old_path The path to the old file or NULL. + * @param new_path The path to the new file or NULL. + * @param payload the user-specified callback payload + * @return 0 or an error code + */ +typedef int GIT_CALLBACK(git_diff_progress_cb)( + const git_diff *diff_so_far, + const char *old_path, + const char *new_path, + void *payload); + +/** + * Structure describing options about how the diff should be executed. + * + * Setting all values of the structure to zero will yield the default + * values. Similarly, passing NULL for the options structure will + * give the defaults. The default values are marked below. + * + */ +typedef struct { + unsigned int version; /**< version for the struct */ + + /** + * A combination of `git_diff_option_t` values above. + * Defaults to GIT_DIFF_NORMAL + */ + uint32_t flags; + + /* options controlling which files are in the diff */ + + /** Overrides the submodule ignore setting for all submodules in the diff. */ + git_submodule_ignore_t ignore_submodules; + + /** + * An array of paths / fnmatch patterns to constrain diff. + * All paths are included by default. + */ + git_strarray pathspec; + + /** + * An optional callback function, notifying the consumer of changes to + * the diff as new deltas are added. + */ + git_diff_notify_cb notify_cb; + + /** + * An optional callback function, notifying the consumer of which files + * are being examined as the diff is generated. + */ + git_diff_progress_cb progress_cb; + + /** The payload to pass to the callback functions. */ + void *payload; + + /* options controlling how to diff text is generated */ + + /** + * The number of unchanged lines that define the boundary of a hunk + * (and to display before and after). Defaults to 3. + */ + uint32_t context_lines; + /** + * The maximum number of unchanged lines between hunk boundaries before + * the hunks will be merged into one. Defaults to 0. + */ + uint32_t interhunk_lines; + + /** + * The object ID type to emit in diffs; this is used by functions + * that operate without a repository - namely `git_diff_buffers`, + * or `git_diff_blobs` and `git_diff_blob_to_buffer` when one blob + * is `NULL`. + * + * This may be omitted (set to `0`). If a repository is available, + * the object ID format of the repository will be used. If no + * repository is available then the default is `GIT_OID_SHA`. + * + * If this is specified and a repository is available, then the + * specified `oid_type` must match the repository's object ID + * format. + */ + git_oid_t oid_type; + + /** + * The abbreviation length to use when formatting object ids. + * Defaults to the value of 'core.abbrev' from the config, or 7 if unset. + */ + uint16_t id_abbrev; + + /** + * A size (in bytes) above which a blob will be marked as binary + * automatically; pass a negative value to disable. + * Defaults to 512MB. + */ + git_off_t max_size; + + /** + * The virtual "directory" prefix for old file names in hunk headers. + * Default is "a". + */ + const char *old_prefix; + + /** + * The virtual "directory" prefix for new file names in hunk headers. + * Defaults to "b". + */ + const char *new_prefix; +} git_diff_options; + +/** The current version of the diff options structure */ +#define GIT_DIFF_OPTIONS_VERSION 1 + +/** Stack initializer for diff options. Alternatively use + * `git_diff_options_init` programmatic initialization. + */ +#define GIT_DIFF_OPTIONS_INIT \ + {GIT_DIFF_OPTIONS_VERSION, 0, GIT_SUBMODULE_IGNORE_UNSPECIFIED, {NULL,0}, NULL, NULL, NULL, 3} + +/** + * Initialize git_diff_options structure + * + * Initializes a `git_diff_options` with default values. Equivalent to creating + * an instance with GIT_DIFF_OPTIONS_INIT. + * + * @param opts The `git_diff_options` struct to initialize. + * @param version The struct version; pass `GIT_DIFF_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_diff_options_init( + git_diff_options *opts, + unsigned int version); + /** * When iterating over a diff, callback that will be made per file. * * @param delta A pointer to the delta data for the file - * @param progress Goes from 0 to 1 over the diff list + * @param progress Goes from 0 to 1 over the diff * @param payload User-specified pointer from foreach function + * @return 0 or an error code */ -typedef int (*git_diff_file_cb)( +typedef int GIT_CALLBACK(git_diff_file_cb)( const git_diff_delta *delta, float progress, void *payload); +/** Maximum size of the hunk header */ +#define GIT_DIFF_HUNK_HEADER_SIZE 128 + +/** + * When producing a binary diff, the binary data returned will be + * either the deflated full ("literal") contents of the file, or + * the deflated binary delta between the two sides (whichever is + * smaller). + */ +typedef enum { + /** There is no binary delta. */ + GIT_DIFF_BINARY_NONE, + + /** The binary data is the literal contents of the file. */ + GIT_DIFF_BINARY_LITERAL, + + /** The binary data is the delta from one side to the other. */ + GIT_DIFF_BINARY_DELTA +} git_diff_binary_t; + +/** The contents of one of the files in a binary diff. */ +typedef struct { + /** The type of binary data for this file. */ + git_diff_binary_t type; + + /** The binary data, deflated. */ + const char *data; + + /** The length of the binary data. */ + size_t datalen; + + /** The length of the binary data after inflation. */ + size_t inflatedlen; +} git_diff_binary_file; + +/** + * Structure describing the binary contents of a diff. + * + * A `binary` file / delta is a file (or pair) for which no text diffs + * should be generated. A diff can contain delta entries that are + * binary, but no diff content will be output for those files. There is + * a base heuristic for binary detection and you can further tune the + * behavior with git attributes or diff flags and option settings. + */ +typedef struct { + /** + * Whether there is data in this binary structure or not. + * + * If this is `1`, then this was produced and included binary content. + * If this is `0` then this was generated knowing only that a binary + * file changed but without providing the data, probably from a patch + * that said `Binary files a/file.txt and b/file.txt differ`. + */ + unsigned int contains_data; + git_diff_binary_file old_file; /**< The contents of the old file. */ + git_diff_binary_file new_file; /**< The contents of the new file. */ +} git_diff_binary; + +/** + * When iterating over a diff, callback that will be made for + * binary content within the diff. + * + * @param delta the delta + * @param binary the binary content + * @param payload the user-specified callback payload + * @return 0 or an error code + */ +typedef int GIT_CALLBACK(git_diff_binary_cb)( + const git_diff_delta *delta, + const git_diff_binary *binary, + void *payload); + /** * Structure describing a hunk of a diff. + * + * A `hunk` is a span of modified lines in a delta along with some stable + * surrounding context. You can configure the amount of context and other + * properties of how hunks are generated. Each hunk also comes with a + * header that described where it starts and ends in both the old and new + * versions in the delta. */ typedef struct { - int old_start; /** Starting line number in old_file */ - int old_lines; /** Number of lines in old_file */ - int new_start; /** Starting line number in new_file */ - int new_lines; /** Number of lines in new_file */ -} git_diff_range; + int old_start; /**< Starting line number in old_file */ + int old_lines; /**< Number of lines in old_file */ + int new_start; /**< Starting line number in new_file */ + int new_lines; /**< Number of lines in new_file */ + size_t header_len; /**< Number of bytes in header text */ + char header[GIT_DIFF_HUNK_HEADER_SIZE]; /**< Header text, NUL-byte terminated */ +} git_diff_hunk; /** * When iterating over a diff, callback that will be made per hunk. + * + * @param delta the delta + * @param hunk the hunk + * @param payload the user-specified callback payload + * @return 0 or an error code */ -typedef int (*git_diff_hunk_cb)( +typedef int GIT_CALLBACK(git_diff_hunk_cb)( const git_diff_delta *delta, - const git_diff_range *range, - const char *header, - size_t header_len, + const git_diff_hunk *hunk, void *payload); /** * Line origin constants. * * These values describe where a line came from and will be passed to - * the git_diff_data_cb when iterating over a diff. There are some + * the git_diff_line_cb when iterating over a diff. There are some * special origin constants at the end that are used for the text * output callbacks to demarcate lines that are actually part of * the file or hunk headers. */ typedef enum { - /* These values will be sent to `git_diff_data_cb` along with the line */ + /* These values will be sent to `git_diff_line_cb` along with the line */ GIT_DIFF_LINE_CONTEXT = ' ', GIT_DIFF_LINE_ADDITION = '+', GIT_DIFF_LINE_DELETION = '-', - GIT_DIFF_LINE_ADD_EOFNL = '\n', /**< Removed line w/o LF & added one with */ - GIT_DIFF_LINE_DEL_EOFNL = '\0', /**< LF was removed at end of file */ - /* The following values will only be sent to a `git_diff_data_cb` when - * the content of a diff is being formatted (eg. through - * git_diff_print_patch() or git_diff_print_compact(), for instance). + GIT_DIFF_LINE_CONTEXT_EOFNL = '=', /**< Both files have no LF at end */ + GIT_DIFF_LINE_ADD_EOFNL = '>', /**< Old has no LF at end, new does */ + GIT_DIFF_LINE_DEL_EOFNL = '<', /**< Old has LF at end, new does not */ + + /* The following values will only be sent to a `git_diff_line_cb` when + * the content of a diff is being formatted through `git_diff_print`. */ GIT_DIFF_LINE_FILE_HDR = 'F', GIT_DIFF_LINE_HUNK_HDR = 'H', - GIT_DIFF_LINE_BINARY = 'B' + GIT_DIFF_LINE_BINARY = 'B' /**< For "Binary files x and y differ" */ } git_diff_line_t; +/** + * Structure describing a line (or data span) of a diff. + * + * A `line` is a range of characters inside a hunk. It could be a context + * line (i.e. in both old and new versions), an added line (i.e. only in + * the new version), or a removed line (i.e. only in the old version). + * Unfortunately, we don't know anything about the encoding of data in the + * file being diffed, so we cannot tell you much about the line content. + * Line data will not be NUL-byte terminated, however, because it will be + * just a span of bytes inside the larger file. + */ +typedef struct { + char origin; /**< A git_diff_line_t value */ + int old_lineno; /**< Line number in old file or -1 for added line */ + int new_lineno; /**< Line number in new file or -1 for deleted line */ + int num_lines; /**< Number of newline characters in content */ + size_t content_len; /**< Number of bytes of data */ + git_off_t content_offset; /**< Offset in the original file to the content */ + const char *content; /**< Pointer to diff text, not NUL-byte terminated */ +} git_diff_line; + /** * When iterating over a diff, callback that will be made per text diff * line. In this context, the provided range will be NULL. @@ -330,103 +664,229 @@ typedef enum { * When printing a diff, callback that will be made to output each line * of text. This uses some extra GIT_DIFF_LINE_... constants for output * of lines of file and hunk headers. - */ -typedef int (*git_diff_data_cb)( - const git_diff_delta *delta, /** delta that contains this data */ - const git_diff_range *range, /** range of lines containing this data */ - char line_origin, /** git_diff_list_t value from above */ - const char *content, /** diff data - not NUL terminated */ - size_t content_len, /** number of bytes of diff data */ - void *payload); /** user reference data */ - -/** - * The diff patch is used to store all the text diffs for a delta. * - * You can easily loop over the content of patches and get information about - * them. + * @param delta the delta that contains the line + * @param hunk the hunk that contains the line + * @param line the line in the diff + * @param payload the user-specified callback payload + * @return 0 or an error code */ -typedef struct git_diff_patch git_diff_patch; +typedef int GIT_CALLBACK(git_diff_line_cb)( + const git_diff_delta *delta, /**< delta that contains this data */ + const git_diff_hunk *hunk, /**< hunk containing this data */ + const git_diff_line *line, /**< line data */ + void *payload); /**< user reference data */ /** * Flags to control the behavior of diff rename/copy detection. */ typedef enum { - /** look for renames? (`--find-renames`) */ - GIT_DIFF_FIND_RENAMES = (1 << 0), - /** consider old size of modified for renames? (`--break-rewrites=N`) */ - GIT_DIFF_FIND_RENAMES_FROM_REWRITES = (1 << 1), - - /** look for copies? (a la `--find-copies`) */ - GIT_DIFF_FIND_COPIES = (1 << 2), - /** consider unmodified as copy sources? (`--find-copies-harder`) */ - GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED = (1 << 3), - - /** split large rewrites into delete/add pairs (`--break-rewrites=/M`) */ - GIT_DIFF_FIND_AND_BREAK_REWRITES = (1 << 4), + /** Obey `diff.renames`. Overridden by any other GIT_DIFF_FIND_... flag. */ + GIT_DIFF_FIND_BY_CONFIG = 0, + + /** Look for renames? (`--find-renames`) */ + GIT_DIFF_FIND_RENAMES = (1u << 0), + + /** Consider old side of MODIFIED for renames? (`--break-rewrites=N`) */ + GIT_DIFF_FIND_RENAMES_FROM_REWRITES = (1u << 1), + + /** Look for copies? (a la `--find-copies`). */ + GIT_DIFF_FIND_COPIES = (1u << 2), + + /** Consider UNMODIFIED as copy sources? (`--find-copies-harder`). + * + * For this to work correctly, use GIT_DIFF_INCLUDE_UNMODIFIED when + * the initial `git_diff` is being generated. + */ + GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED = (1u << 3), + + /** Mark significant rewrites for split (`--break-rewrites=/M`) */ + GIT_DIFF_FIND_REWRITES = (1u << 4), + /** Actually split large rewrites into delete/add pairs */ + GIT_DIFF_BREAK_REWRITES = (1u << 5), + /** Mark rewrites for split and break into delete/add pairs */ + GIT_DIFF_FIND_AND_BREAK_REWRITES = + (GIT_DIFF_FIND_REWRITES | GIT_DIFF_BREAK_REWRITES), + + /** Find renames/copies for UNTRACKED items in working directory. + * + * For this to work correctly, use GIT_DIFF_INCLUDE_UNTRACKED when the + * initial `git_diff` is being generated (and obviously the diff must + * be against the working directory for this to make sense). + */ + GIT_DIFF_FIND_FOR_UNTRACKED = (1u << 6), + + /** Turn on all finding features. */ + GIT_DIFF_FIND_ALL = (0x0ff), + + /** Measure similarity ignoring leading whitespace (default) */ + GIT_DIFF_FIND_IGNORE_LEADING_WHITESPACE = 0, + /** Measure similarity ignoring all whitespace */ + GIT_DIFF_FIND_IGNORE_WHITESPACE = (1u << 12), + /** Measure similarity including all data */ + GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE = (1u << 13), + /** Measure similarity only by comparing SHAs (fast and cheap) */ + GIT_DIFF_FIND_EXACT_MATCH_ONLY = (1u << 14), + + /** Do not break rewrites unless they contribute to a rename. + * + * Normally, GIT_DIFF_FIND_AND_BREAK_REWRITES will measure the self- + * similarity of modified files and split the ones that have changed a + * lot into a DELETE / ADD pair. Then the sides of that pair will be + * considered candidates for rename and copy detection. + * + * If you add this flag in and the split pair is *not* used for an + * actual rename or copy, then the modified record will be restored to + * a regular MODIFIED record instead of being split. + */ + GIT_DIFF_BREAK_REWRITES_FOR_RENAMES_ONLY = (1u << 15), + + /** Remove any UNMODIFIED deltas after find_similar is done. + * + * Using GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED to emulate the + * --find-copies-harder behavior requires building a diff with the + * GIT_DIFF_INCLUDE_UNMODIFIED flag. If you do not want UNMODIFIED + * records in the final result, pass this flag to have them removed. + */ + GIT_DIFF_FIND_REMOVE_UNMODIFIED = (1u << 16) } git_diff_find_t; +/** + * Pluggable similarity metric + */ +typedef struct { + int GIT_CALLBACK(file_signature)( + void **out, const git_diff_file *file, + const char *fullpath, void *payload); + int GIT_CALLBACK(buffer_signature)( + void **out, const git_diff_file *file, + const char *buf, size_t buflen, void *payload); + void GIT_CALLBACK(free_signature)(void *sig, void *payload); + int GIT_CALLBACK(similarity)(int *score, void *siga, void *sigb, void *payload); + void *payload; +} git_diff_similarity_metric; + /** * Control behavior of rename and copy detection + * + * These options mostly mimic parameters that can be passed to git-diff. */ typedef struct { unsigned int version; - /** Combination of git_diff_find_t values (default FIND_RENAMES) */ - unsigned int flags; + /** + * Combination of git_diff_find_t values (default GIT_DIFF_FIND_BY_CONFIG). + * NOTE: if you don't explicitly set this, `diff.renames` could be set + * to false, resulting in `git_diff_find_similar` doing nothing. + */ + uint32_t flags; + + /** + * Threshold above which similar files will be considered renames. + * This is equivalent to the -M option. Defaults to 50. + */ + uint16_t rename_threshold; - /** Similarity to consider a file renamed (default 50) */ - unsigned int rename_threshold; - /** Similarity of modified to be eligible rename source (default 50) */ - unsigned int rename_from_rewrite_threshold; - /** Similarity to consider a file a copy (default 50) */ - unsigned int copy_threshold; - /** Similarity to split modify into delete/add pair (default 60) */ - unsigned int break_rewrite_threshold; + /** + * Threshold below which similar files will be eligible to be a rename source. + * This is equivalent to the first part of the -B option. Defaults to 50. + */ + uint16_t rename_from_rewrite_threshold; + + /** + * Threshold above which similar files will be considered copies. + * This is equivalent to the -C option. Defaults to 50. + */ + uint16_t copy_threshold; - /** Maximum similarity sources to examine (a la diff's `-l` option or - * the `diff.renameLimit` config) (default 200) + /** + * Threshold below which similar files will be split into a delete/add pair. + * This is equivalent to the last part of the -B option. Defaults to 60. */ - unsigned int target_limit; + uint16_t break_rewrite_threshold; + + /** + * Maximum number of matches to consider for a particular file. + * + * This is a little different from the `-l` option from Git because we + * will still process up to this many matches before abandoning the search. + * Defaults to 1000. + */ + size_t rename_limit; + + /** + * The `metric` option allows you to plug in a custom similarity metric. + * + * Set it to NULL to use the default internal metric. + * + * The default metric is based on sampling hashes of ranges of data in + * the file, which is a pretty good similarity approximation that should + * work fairly well for both text and binary data while still being + * pretty fast with a fixed memory overhead. + */ + git_diff_similarity_metric *metric; } git_diff_find_options; +/** Current version for the `git_diff_find_options` structure */ #define GIT_DIFF_FIND_OPTIONS_VERSION 1 + +/** Static constructor for `git_diff_find_options` */ #define GIT_DIFF_FIND_OPTIONS_INIT {GIT_DIFF_FIND_OPTIONS_VERSION} -/** @name Diff List Generator Functions +/** + * Initialize git_diff_find_options structure + * + * Initializes a `git_diff_find_options` with default values. Equivalent to creating + * an instance with GIT_DIFF_FIND_OPTIONS_INIT. + * + * @param opts The `git_diff_find_options` struct to initialize. + * @param version The struct version; pass `GIT_DIFF_FIND_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_diff_find_options_init( + git_diff_find_options *opts, + unsigned int version); + +/** @name Diff Generator Functions * * These are the functions you would use to create (or destroy) a - * git_diff_list from various objects in a repository. + * git_diff from various objects in a repository. */ /**@{*/ /** - * Deallocate a diff list. + * Deallocate a diff. + * + * @param diff The previously created diff; cannot be used after free. */ -GIT_EXTERN(void) git_diff_list_free(git_diff_list *diff); +GIT_EXTERN(void) git_diff_free(git_diff *diff); /** - * Create a diff list with the difference between two tree objects. + * Create a diff with the difference between two tree objects. * * This is equivalent to `git diff ` * * The first tree will be used for the "old_file" side of the delta and the - * second tree will be used for the "new_file" side of the delta. + * second tree will be used for the "new_file" side of the delta. You can + * pass NULL to indicate an empty tree, although it is an error to pass + * NULL for both the `old_tree` and `new_tree`. * - * @param diff Output pointer to a git_diff_list pointer to be allocated. + * @param diff Output pointer to a git_diff pointer to be allocated. * @param repo The repository containing the trees. - * @param old_tree A git_tree object to diff from. - * @param new_tree A git_tree object to diff to. + * @param old_tree A git_tree object to diff from, or NULL for empty tree. + * @param new_tree A git_tree object to diff to, or NULL for empty tree. * @param opts Structure with options to influence diff or NULL for defaults. + * @return 0 or an error code. */ GIT_EXTERN(int) git_diff_tree_to_tree( - git_diff_list **diff, + git_diff **diff, git_repository *repo, git_tree *old_tree, git_tree *new_tree, - const git_diff_options *opts); /**< can be NULL for defaults */ + const git_diff_options *opts); /** - * Create a diff list between a tree and repository index. + * Create a diff between a tree and repository index. * * This is equivalent to `git diff --cached ` or if you pass * the HEAD tree, then like `git diff --cached`. @@ -434,21 +894,26 @@ GIT_EXTERN(int) git_diff_tree_to_tree( * The tree you pass will be used for the "old_file" side of the delta, and * the index will be used for the "new_file" side of the delta. * - * @param diff Output pointer to a git_diff_list pointer to be allocated. + * If you pass NULL for the index, then the existing index of the `repo` + * will be used. In this case, the index will be refreshed from disk + * (if it has changed) before the diff is generated. + * + * @param diff Output pointer to a git_diff pointer to be allocated. * @param repo The repository containing the tree and index. - * @param old_tree A git_tree object to diff from. + * @param old_tree A git_tree object to diff from, or NULL for empty tree. * @param index The index to diff with; repo index used if NULL. * @param opts Structure with options to influence diff or NULL for defaults. + * @return 0 or an error code. */ GIT_EXTERN(int) git_diff_tree_to_index( - git_diff_list **diff, + git_diff **diff, git_repository *repo, git_tree *old_tree, git_index *index, - const git_diff_options *opts); /**< can be NULL for defaults */ + const git_diff_options *opts); /** - * Create a diff list between the repository index and the workdir directory. + * Create a diff between the repository index and the workdir directory. * * This matches the `git diff` command. See the note below on * `git_diff_tree_to_workdir` for a discussion of the difference between @@ -458,57 +923,97 @@ GIT_EXTERN(int) git_diff_tree_to_index( * The index will be used for the "old_file" side of the delta, and the * working directory will be used for the "new_file" side of the delta. * - * @param diff Output pointer to a git_diff_list pointer to be allocated. + * If you pass NULL for the index, then the existing index of the `repo` + * will be used. In this case, the index will be refreshed from disk + * (if it has changed) before the diff is generated. + * + * @param diff Output pointer to a git_diff pointer to be allocated. * @param repo The repository. * @param index The index to diff from; repo index used if NULL. * @param opts Structure with options to influence diff or NULL for defaults. + * @return 0 or an error code. */ GIT_EXTERN(int) git_diff_index_to_workdir( - git_diff_list **diff, + git_diff **diff, git_repository *repo, git_index *index, - const git_diff_options *opts); /**< can be NULL for defaults */ + const git_diff_options *opts); /** - * Create a diff list between a tree and the working directory. + * Create a diff between a tree and the working directory. * * The tree you provide will be used for the "old_file" side of the delta, * and the working directory will be used for the "new_file" side. * - * Please note: this is *NOT* the same as `git diff `. Running - * `git diff HEAD` or the like actually uses information from the index, - * along with the tree and working directory info. + * This is not the same as `git diff ` or `git diff-index + * `. Those commands use information from the index, whereas this + * function strictly returns the differences between the tree and the files + * in the working directory, regardless of the state of the index. Use + * `git_diff_tree_to_workdir_with_index` to emulate those commands. * - * This function returns strictly the differences between the tree and the - * files contained in the working directory, regardless of the state of - * files in the index. It may come as a surprise, but there is no direct - * equivalent in core git. + * To see difference between this and `git_diff_tree_to_workdir_with_index`, + * consider the example of a staged file deletion where the file has then + * been put back into the working dir and further modified. The + * tree-to-workdir diff for that file is 'modified', but `git diff` would + * show status 'deleted' since there is a staged delete. * - * To emulate `git diff `, call both `git_diff_tree_to_index` and - * `git_diff_index_to_workdir`, then call `git_diff_merge` on the results. - * That will yield a `git_diff_list` that matches the git output. + * @param diff A pointer to a git_diff pointer that will be allocated. + * @param repo The repository containing the tree. + * @param old_tree A git_tree object to diff from, or NULL for empty tree. + * @param opts Structure with options to influence diff or NULL for defaults. + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_diff_tree_to_workdir( + git_diff **diff, + git_repository *repo, + git_tree *old_tree, + const git_diff_options *opts); + +/** + * Create a diff between a tree and the working directory using index data + * to account for staged deletes, tracked files, etc. * - * If this seems confusing, take the case of a file with a staged deletion - * where the file has then been put back into the working dir and modified. - * The tree-to-workdir diff for that file is 'modified', but core git would - * show status 'deleted' since there is a pending deletion in the index. + * This emulates `git diff ` by diffing the tree to the index and + * the index to the working directory and blending the results into a + * single diff that includes staged deleted, etc. * - * @param diff A pointer to a git_diff_list pointer that will be allocated. + * @param diff A pointer to a git_diff pointer that will be allocated. * @param repo The repository containing the tree. - * @param old_tree A git_tree object to diff from. + * @param old_tree A git_tree object to diff from, or NULL for empty tree. * @param opts Structure with options to influence diff or NULL for defaults. + * @return 0 or an error code. */ -GIT_EXTERN(int) git_diff_tree_to_workdir( - git_diff_list **diff, +GIT_EXTERN(int) git_diff_tree_to_workdir_with_index( + git_diff **diff, git_repository *repo, git_tree *old_tree, - const git_diff_options *opts); /**< can be NULL for defaults */ + const git_diff_options *opts); + +/** + * Create a diff with the difference between two index objects. + * + * The first index will be used for the "old_file" side of the delta and the + * second index will be used for the "new_file" side of the delta. + * + * @param diff Output pointer to a git_diff pointer to be allocated. + * @param repo The repository containing the indexes. + * @param old_index A git_index object to diff from. + * @param new_index A git_index object to diff to. + * @param opts Structure with options to influence diff or NULL for defaults. + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_diff_index_to_index( + git_diff **diff, + git_repository *repo, + git_index *old_index, + git_index *new_index, + const git_diff_options *opts); /** - * Merge one diff list into another. + * Merge one diff into another. * * This merges items from the "from" list into the "onto" list. The - * resulting diff list will have all items that appear in either list. + * resulting diff will have all items that appear in either list. * If an item appears in both lists, then it will be "merged" to appear * as if the old version was from the "onto" list and the new version * is from the "from" list (with the exception that if the item has a @@ -516,53 +1021,105 @@ GIT_EXTERN(int) git_diff_tree_to_workdir( * * @param onto Diff to merge into. * @param from Diff to merge. + * @return 0 or an error code. */ GIT_EXTERN(int) git_diff_merge( - git_diff_list *onto, - const git_diff_list *from); + git_diff *onto, + const git_diff *from); /** - * Transform a diff list marking file renames, copies, etc. + * Transform a diff marking file renames, copies, etc. * - * This modifies a diff list in place, replacing old entries that look + * This modifies a diff in place, replacing old entries that look * like renames or copies with new entries reflecting those changes. * This also will, if requested, break modified files into add/remove * pairs if the amount of change is above a threshold. * - * @param diff Diff list to run detection algorithms on + * @param diff diff to run detection algorithms on * @param options Control how detection should be run, NULL for defaults * @return 0 on success, -1 on failure */ GIT_EXTERN(int) git_diff_find_similar( - git_diff_list *diff, - git_diff_find_options *options); + git_diff *diff, + const git_diff_find_options *options); /**@}*/ -/** @name Diff List Processor Functions +/** @name Diff Processor Functions * - * These are the functions you apply to a diff list to process it + * These are the functions you apply to a diff to process it * or read it in some way. */ /**@{*/ /** - * Loop over all deltas in a diff list issuing callbacks. + * Query how many diff records are there in a diff. + * + * @param diff A git_diff generated by one of the above functions + * @return Count of number of deltas in the list + */ +GIT_EXTERN(size_t) git_diff_num_deltas(const git_diff *diff); + +/** + * Query how many diff deltas are there in a diff filtered by type. + * + * This works just like `git_diff_num_deltas()` with an extra parameter + * that is a `git_delta_t` and returns just the count of how many deltas + * match that particular type. + * + * @param diff A git_diff generated by one of the above functions + * @param type A git_delta_t value to filter the count + * @return Count of number of deltas matching delta_t type + */ +GIT_EXTERN(size_t) git_diff_num_deltas_of_type( + const git_diff *diff, git_delta_t type); + +/** + * Return the diff delta for an entry in the diff list. + * + * The `git_diff_delta` pointer points to internal data and you do not + * have to release it when you are done with it. It will go away when + * the * `git_diff` (or any associated `git_patch`) goes away. + * + * Note that the flags on the delta related to whether it has binary + * content or not may not be set if there are no attributes set for the + * file and there has been no reason to load the file data at this point. + * For now, if you need those flags to be up to date, your only option is + * to either use `git_diff_foreach` or create a `git_patch`. + * + * @param diff Diff list object + * @param idx Index into diff list + * @return Pointer to git_diff_delta (or NULL if `idx` out of range) + */ +GIT_EXTERN(const git_diff_delta *) git_diff_get_delta( + const git_diff *diff, size_t idx); + +/** + * Check if deltas are sorted case sensitively or insensitively. + * + * @param diff diff to check + * @return 0 if case sensitive, 1 if case is ignored + */ +GIT_EXTERN(int) git_diff_is_sorted_icase(const git_diff *diff); + +/** + * Loop over all deltas in a diff issuing callbacks. * * This will iterate through all of the files described in a diff. You * should provide a file callback to learn about each file. * * The "hunk" and "line" callbacks are optional, and the text diff of the * files will only be calculated if they are not NULL. Of course, these - * callbacks will not be invoked for binary files on the diff list or for + * callbacks will not be invoked for binary files on the diff or for * files whose only changed is a file mode change. * * Returning a non-zero value from any of the callbacks will terminate - * the iteration and cause this return `GIT_EUSER`. + * the iteration and return the value to the user. * - * @param diff A git_diff_list generated by one of the above functions. + * @param diff A git_diff generated by one of the above functions. * @param file_cb Callback function to make per file in the diff. + * @param binary_cb Optional callback to make for binary files. * @param hunk_cb Optional callback to make per hunk of text diff. This * callback is called to describe a range of lines in the * diff. It will not be issued for binary files. @@ -570,306 +1127,411 @@ GIT_EXTERN(int) git_diff_find_similar( * same callback will be made for context lines, added, and * removed lines, and even for a deleted trailing newline. * @param payload Reference pointer that will be passed to your callbacks. - * @return 0 on success, GIT_EUSER on non-zero callback, or error code + * @return 0 on success, non-zero callback return value, or error code */ GIT_EXTERN(int) git_diff_foreach( - git_diff_list *diff, + git_diff *diff, git_diff_file_cb file_cb, + git_diff_binary_cb binary_cb, git_diff_hunk_cb hunk_cb, - git_diff_data_cb line_cb, - void *payload); - -/** - * Iterate over a diff generating text output like "git diff --name-status". - * - * Returning a non-zero value from the callbacks will terminate the - * iteration and cause this return `GIT_EUSER`. - * - * @param diff A git_diff_list generated by one of the above functions. - * @param print_cb Callback to make per line of diff text. - * @param payload Reference pointer that will be passed to your callback. - * @return 0 on success, GIT_EUSER on non-zero callback, or error code - */ -GIT_EXTERN(int) git_diff_print_compact( - git_diff_list *diff, - git_diff_data_cb print_cb, + git_diff_line_cb line_cb, void *payload); /** * Look up the single character abbreviation for a delta status code. * - * When you call `git_diff_print_compact` it prints single letter codes into - * the output such as 'A' for added, 'D' for deleted, 'M' for modified, etc. - * It is sometimes convenient to convert a git_delta_t value into these - * letters for your own purposes. This function does just that. By the - * way, unmodified will return a space (i.e. ' '). + * When you run `git diff --name-status` it uses single letter codes in + * the output such as 'A' for added, 'D' for deleted, 'M' for modified, + * etc. This function converts a git_delta_t value into these letters for + * your own purposes. GIT_DELTA_UNTRACKED will return a space (i.e. ' '). * - * @param delta_t The git_delta_t value to look up + * @param status The git_delta_t value to look up * @return The single character label for that code */ GIT_EXTERN(char) git_diff_status_char(git_delta_t status); /** - * Iterate over a diff generating text output like "git diff". - * - * This is a super easy way to generate a patch from a diff. + * Possible output formats for diff data + */ +typedef enum { + GIT_DIFF_FORMAT_PATCH = 1u, /**< full git diff */ + GIT_DIFF_FORMAT_PATCH_HEADER = 2u, /**< just the file headers of patch */ + GIT_DIFF_FORMAT_RAW = 3u, /**< like git diff --raw */ + GIT_DIFF_FORMAT_NAME_ONLY = 4u, /**< like git diff --name-only */ + GIT_DIFF_FORMAT_NAME_STATUS = 5u, /**< like git diff --name-status */ + GIT_DIFF_FORMAT_PATCH_ID = 6u /**< git diff as used by git patch-id */ +} git_diff_format_t; + +/** + * Iterate over a diff generating formatted text output. * * Returning a non-zero value from the callbacks will terminate the - * iteration and cause this return `GIT_EUSER`. + * iteration and return the non-zero value to the caller. * - * @param diff A git_diff_list generated by one of the above functions. - * @param payload Reference pointer that will be passed to your callbacks. - * @param print_cb Callback function to output lines of the diff. This - * same function will be called for file headers, hunk - * headers, and diff lines. Fortunately, you can probably - * use various GIT_DIFF_LINE constants to determine what - * text you are given. - * @return 0 on success, GIT_EUSER on non-zero callback, or error code - */ -GIT_EXTERN(int) git_diff_print_patch( - git_diff_list *diff, - git_diff_data_cb print_cb, + * @param diff A git_diff generated by one of the above functions. + * @param format A git_diff_format_t value to pick the text format. + * @param print_cb Callback to make per line of diff text. + * @param payload Reference pointer that will be passed to your callback. + * @return 0 on success, non-zero callback return value, or error code + */ +GIT_EXTERN(int) git_diff_print( + git_diff *diff, + git_diff_format_t format, + git_diff_line_cb print_cb, void *payload); /** - * Query how many diff records are there in a diff list. - * - * @param diff A git_diff_list generated by one of the above functions - * @return Count of number of deltas in the list + * Produce the complete formatted text output from a diff into a + * buffer. + * + * @param out A pointer to a user-allocated git_buf that will + * contain the diff text + * @param diff A git_diff generated by one of the above functions. + * @param format A git_diff_format_t value to pick the text format. + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_diff_to_buf( + git_buf *out, + git_diff *diff, + git_diff_format_t format); + +/**@}*/ + +/* + * Low-level file comparison, invoking callbacks per difference. */ -GIT_EXTERN(size_t) git_diff_num_deltas(git_diff_list *diff); /** - * Query how many diff deltas are there in a diff list filtered by type. + * Directly run a diff on two blobs. * - * This works just like `git_diff_entrycount()` with an extra parameter - * that is a `git_delta_t` and returns just the count of how many deltas - * match that particular type. + * Compared to a file, a blob lacks some contextual information. As such, + * the `git_diff_file` given to the callback will have some fake data; i.e. + * `mode` will be 0 and `path` will be NULL. * - * @param diff A git_diff_list generated by one of the above functions - * @param type A git_delta_t value to filter the count - * @return Count of number of deltas matching delta_t type + * NULL is allowed for either `old_blob` or `new_blob` and will be treated + * as an empty blob, with the `oid` set to NULL in the `git_diff_file` data. + * Passing NULL for both blobs is a noop; no callbacks will be made at all. + * + * We do run a binary content check on the blob content and if either blob + * looks like binary data, the `git_diff_delta` binary attribute will be set + * to 1 and no call to the hunk_cb nor line_cb will be made (unless you pass + * `GIT_DIFF_FORCE_TEXT` of course). + * + * @param old_blob Blob for old side of diff, or NULL for empty blob + * @param old_as_path Treat old blob as if it had this filename; can be NULL + * @param new_blob Blob for new side of diff, or NULL for empty blob + * @param new_as_path Treat new blob as if it had this filename; can be NULL + * @param options Options for diff, or NULL for default options + * @param file_cb Callback for "file"; made once if there is a diff; can be NULL + * @param binary_cb Callback for binary files; can be NULL + * @param hunk_cb Callback for each hunk in diff; can be NULL + * @param line_cb Callback for each line in diff; can be NULL + * @param payload Payload passed to each callback function + * @return 0 on success, non-zero callback return value, or error code */ -GIT_EXTERN(size_t) git_diff_num_deltas_of_type( - git_diff_list *diff, - git_delta_t type); +GIT_EXTERN(int) git_diff_blobs( + const git_blob *old_blob, + const char *old_as_path, + const git_blob *new_blob, + const char *new_as_path, + const git_diff_options *options, + git_diff_file_cb file_cb, + git_diff_binary_cb binary_cb, + git_diff_hunk_cb hunk_cb, + git_diff_line_cb line_cb, + void *payload); /** - * Return the diff delta and patch for an entry in the diff list. - * - * The `git_diff_patch` is a newly created object contains the text diffs - * for the delta. You have to call `git_diff_patch_free()` when you are - * done with it. You can use the patch object to loop over all the hunks - * and lines in the diff of the one delta. - * - * For an unchanged file or a binary file, no `git_diff_patch` will be - * created, the output will be set to NULL, and the `binary` flag will be - * set true in the `git_diff_delta` structure. - * - * The `git_diff_delta` pointer points to internal data and you do not have - * to release it when you are done with it. It will go away when the - * `git_diff_list` and `git_diff_patch` go away. + * Directly run a diff between a blob and a buffer. * - * It is okay to pass NULL for either of the output parameters; if you pass - * NULL for the `git_diff_patch`, then the text diff will not be calculated. + * As with `git_diff_blobs`, comparing a blob and buffer lacks some context, + * so the `git_diff_file` parameters to the callbacks will be faked a la the + * rules for `git_diff_blobs()`. * - * @param patch_out Output parameter for the delta patch object - * @param delta_out Output parameter for the delta object - * @param diff Diff list object - * @param idx Index into diff list - * @return 0 on success, other value < 0 on error + * Passing NULL for `old_blob` will be treated as an empty blob (i.e. the + * `file_cb` will be invoked with GIT_DELTA_ADDED and the diff will be the + * entire content of the buffer added). Passing NULL to the buffer will do + * the reverse, with GIT_DELTA_REMOVED and blob content removed. + * + * @param old_blob Blob for old side of diff, or NULL for empty blob + * @param old_as_path Treat old blob as if it had this filename; can be NULL + * @param buffer Raw data for new side of diff, or NULL for empty + * @param buffer_len Length of raw data for new side of diff + * @param buffer_as_path Treat buffer as if it had this filename; can be NULL + * @param options Options for diff, or NULL for default options + * @param file_cb Callback for "file"; made once if there is a diff; can be NULL + * @param binary_cb Callback for binary files; can be NULL + * @param hunk_cb Callback for each hunk in diff; can be NULL + * @param line_cb Callback for each line in diff; can be NULL + * @param payload Payload passed to each callback function + * @return 0 on success, non-zero callback return value, or error code */ -GIT_EXTERN(int) git_diff_get_patch( - git_diff_patch **patch_out, - const git_diff_delta **delta_out, - git_diff_list *diff, - size_t idx); +GIT_EXTERN(int) git_diff_blob_to_buffer( + const git_blob *old_blob, + const char *old_as_path, + const char *buffer, + size_t buffer_len, + const char *buffer_as_path, + const git_diff_options *options, + git_diff_file_cb file_cb, + git_diff_binary_cb binary_cb, + git_diff_hunk_cb hunk_cb, + git_diff_line_cb line_cb, + void *payload); /** - * Free a git_diff_patch object. + * Directly run a diff between two buffers. + * + * Even more than with `git_diff_blobs`, comparing two buffer lacks + * context, so the `git_diff_file` parameters to the callbacks will be + * faked a la the rules for `git_diff_blobs()`. + * + * @param old_buffer Raw data for old side of diff, or NULL for empty + * @param old_len Length of the raw data for old side of the diff + * @param old_as_path Treat old buffer as if it had this filename; can be NULL + * @param new_buffer Raw data for new side of diff, or NULL for empty + * @param new_len Length of raw data for new side of diff + * @param new_as_path Treat buffer as if it had this filename; can be NULL + * @param options Options for diff, or NULL for default options + * @param file_cb Callback for "file"; made once if there is a diff; can be NULL + * @param binary_cb Callback for binary files; can be NULL + * @param hunk_cb Callback for each hunk in diff; can be NULL + * @param line_cb Callback for each line in diff; can be NULL + * @param payload Payload passed to each callback function + * @return 0 on success, non-zero callback return value, or error code */ -GIT_EXTERN(void) git_diff_patch_free( - git_diff_patch *patch); +GIT_EXTERN(int) git_diff_buffers( + const void *old_buffer, + size_t old_len, + const char *old_as_path, + const void *new_buffer, + size_t new_len, + const char *new_as_path, + const git_diff_options *options, + git_diff_file_cb file_cb, + git_diff_binary_cb binary_cb, + git_diff_hunk_cb hunk_cb, + git_diff_line_cb line_cb, + void *payload); + +/* Patch file parsing. */ /** - * Get the delta associated with a patch + * Options for parsing a diff / patch file. + */ +typedef struct { + unsigned int version; + + /** Object ID type used in the patch file. */ + git_oid_t oid_type; +} git_diff_parse_options; + +/** The current version of the diff parse options structure */ +#define GIT_DIFF_PARSE_OPTIONS_VERSION 1 + +/** Stack initializer for diff parse options. Alternatively use + * `git_diff_parse_options_init` programmatic initialization. */ -GIT_EXTERN(const git_diff_delta *) git_diff_patch_delta( - git_diff_patch *patch); +#define GIT_DIFF_PARSE_OPTIONS_INIT { GIT_DIFF_PARSE_OPTIONS_VERSION } /** - * Get the number of hunks in a patch + * Read the contents of a git patch file into a `git_diff` object. + * + * The diff object produced is similar to the one that would be + * produced if you actually produced it computationally by comparing + * two trees, however there may be subtle differences. For example, + * a patch file likely contains abbreviated object IDs, so the + * object IDs in a `git_diff_delta` produced by this function will + * also be abbreviated. + * + * This function will only read patch files created by a git + * implementation, it will not read unified diffs produced by + * the `diff` program, nor any other types of patch files. + * + * @note This API only supports SHA1 patch files + * @see git_diff_from_buffer_ext + * + * @param out A pointer to a git_diff pointer that will be allocated. + * @param content The contents of a patch file + * @param content_len The length of the patch file contents + * @return 0 or an error code */ -GIT_EXTERN(size_t) git_diff_patch_num_hunks( - git_diff_patch *patch); +GIT_EXTERN(int) git_diff_from_buffer( + git_diff **out, + const char *content, + size_t content_len); + +#ifdef GIT_EXPERIMENTAL_SHA256 /** - * Get line counts of each type in a patch. - * - * This helps imitate a diff --numstat type of output. For that purpose, - * you only need the `total_additions` and `total_deletions` values, but we - * include the `total_context` line count in case you want the total number - * of lines of diff output that will be generated. - * - * All outputs are optional. Pass NULL if you don't need a particular count. - * - * @param total_context Count of context lines in output, can be NULL. - * @param total_additions Count of addition lines in output, can be NULL. - * @param total_deletions Count of deletion lines in output, can be NULL. - * @param patch The git_diff_patch object - * @return Number of lines in hunk or -1 if invalid hunk index + * Read the contents of a git patch file into a `git_diff` object. + * + * The diff object produced is similar to the one that would be + * produced if you actually produced it computationally by comparing + * two trees, however there may be subtle differences. For example, + * a patch file likely contains abbreviated object IDs, so the + * object IDs in a `git_diff_delta` produced by this function will + * also be abbreviated. + * + * This function will only read patch files created by a git + * implementation, it will not read unified diffs produced by + * the `diff` program, nor any other types of patch files. + * + * @param out A pointer to a git_diff pointer that will be allocated. + * @param content The contents of a patch file + * @param content_len The length of the patch file contents + * @param opts Options controlling diff parsing + * @return 0 or an error code */ -GIT_EXTERN(int) git_diff_patch_line_stats( - size_t *total_context, - size_t *total_additions, - size_t *total_deletions, - const git_diff_patch *patch); +GIT_EXTERN(int) git_diff_from_buffer_ext( + git_diff **out, + const char *content, + size_t content_len, + git_diff_parse_options *opts); + +#endif /** - * Get the information about a hunk in a patch - * - * Given a patch and a hunk index into the patch, this returns detailed - * information about that hunk. Any of the output pointers can be passed - * as NULL if you don't care about that particular piece of information. - * - * @param range Output pointer to git_diff_range of hunk - * @param header Output pointer to header string for hunk. Unlike the - * content pointer for each line, this will be NUL-terminated - * @param header_len Output value of characters in header string - * @param lines_in_hunk Output count of total lines in this hunk - * @param patch Input pointer to patch object - * @param hunk_idx Input index of hunk to get information about - * @return 0 on success, GIT_ENOTFOUND if hunk_idx out of range, <0 on error + * This is an opaque structure which is allocated by `git_diff_get_stats`. + * You are responsible for releasing the object memory when done, using the + * `git_diff_stats_free()` function. */ -GIT_EXTERN(int) git_diff_patch_get_hunk( - const git_diff_range **range, - const char **header, - size_t *header_len, - size_t *lines_in_hunk, - git_diff_patch *patch, - size_t hunk_idx); +typedef struct git_diff_stats git_diff_stats; /** - * Get the number of lines in a hunk. - * - * @param patch The git_diff_patch object - * @param hunk_idx Index of the hunk - * @return Number of lines in hunk or -1 if invalid hunk index + * Formatting options for diff stats */ -GIT_EXTERN(int) git_diff_patch_num_lines_in_hunk( - git_diff_patch *patch, - size_t hunk_idx); +typedef enum { + /** No stats*/ + GIT_DIFF_STATS_NONE = 0, + + /** Full statistics, equivalent of `--stat` */ + GIT_DIFF_STATS_FULL = (1u << 0), + + /** Short statistics, equivalent of `--shortstat` */ + GIT_DIFF_STATS_SHORT = (1u << 1), + + /** Number statistics, equivalent of `--numstat` */ + GIT_DIFF_STATS_NUMBER = (1u << 2), + + /** Extended header information such as creations, renames and mode changes, equivalent of `--summary` */ + GIT_DIFF_STATS_INCLUDE_SUMMARY = (1u << 3) +} git_diff_stats_format_t; /** - * Get data about a line in a hunk of a patch. - * - * Given a patch, a hunk index, and a line index in the hunk, this - * will return a lot of details about that line. If you pass a hunk - * index larger than the number of hunks or a line index larger than - * the number of lines in the hunk, this will return -1. + * Accumulate diff statistics for all patches. * - * @param line_origin A GIT_DIFF_LINE constant from above - * @param content Pointer to content of diff line, not NUL-terminated - * @param content_len Number of characters in content - * @param old_lineno Line number in old file or -1 if line is added - * @param new_lineno Line number in new file or -1 if line is deleted - * @param patch The patch to look in - * @param hunk_idx The index of the hunk - * @param line_of_index The index of the line in the hunk - * @return 0 on success, <0 on failure + * @param out Structure containing the diff statistics. + * @param diff A git_diff generated by one of the above functions. + * @return 0 on success; non-zero on error */ -GIT_EXTERN(int) git_diff_patch_get_line_in_hunk( - char *line_origin, - const char **content, - size_t *content_len, - int *old_lineno, - int *new_lineno, - git_diff_patch *patch, - size_t hunk_idx, - size_t line_of_hunk); +GIT_EXTERN(int) git_diff_get_stats( + git_diff_stats **out, + git_diff *diff); /** - * Serialize the patch to text via callback. + * Get the total number of files changed in a diff * - * Returning a non-zero value from the callback will terminate the iteration - * and cause this return `GIT_EUSER`. - * - * @param patch A git_diff_patch representing changes to one file - * @param print_cb Callback function to output lines of the patch. Will be - * called for file headers, hunk headers, and diff lines. - * @param payload Reference pointer that will be passed to your callbacks. - * @return 0 on success, GIT_EUSER on non-zero callback, or error code + * @param stats A `git_diff_stats` generated by one of the above functions. + * @return total number of files changed in the diff */ -GIT_EXTERN(int) git_diff_patch_print( - git_diff_patch *patch, - git_diff_data_cb print_cb, - void *payload); +GIT_EXTERN(size_t) git_diff_stats_files_changed( + const git_diff_stats *stats); /** - * Get the content of a patch as a single diff text. + * Get the total number of insertions in a diff * - * @param string Allocated string; caller must free. - * @param patch A git_diff_patch representing changes to one file - * @return 0 on success, <0 on failure. + * @param stats A `git_diff_stats` generated by one of the above functions. + * @return total number of insertions in the diff */ -GIT_EXTERN(int) git_diff_patch_to_str( - char **string, - git_diff_patch *patch); - -/**@}*/ +GIT_EXTERN(size_t) git_diff_stats_insertions( + const git_diff_stats *stats); - -/* - * Misc +/** + * Get the total number of deletions in a diff + * + * @param stats A `git_diff_stats` generated by one of the above functions. + * @return total number of deletions in the diff */ +GIT_EXTERN(size_t) git_diff_stats_deletions( + const git_diff_stats *stats); /** - * Directly run a diff on two blobs. + * Print diff statistics to a `git_buf`. * - * Compared to a file, a blob lacks some contextual information. As such, - * the `git_diff_file` given to the callback will have some fake data; i.e. - * `mode` will be 0 and `path` will be NULL. + * @param out buffer to store the formatted diff statistics in. + * @param stats A `git_diff_stats` generated by one of the above functions. + * @param format Formatting option. + * @param width Target width for output (only affects GIT_DIFF_STATS_FULL) + * @return 0 on success; non-zero on error + */ +GIT_EXTERN(int) git_diff_stats_to_buf( + git_buf *out, + const git_diff_stats *stats, + git_diff_stats_format_t format, + size_t width); + +/** + * Deallocate a `git_diff_stats`. * - * NULL is allowed for either `old_blob` or `new_blob` and will be treated - * as an empty blob, with the `oid` set to NULL in the `git_diff_file` data. + * @param stats The previously created statistics object; + * cannot be used after free. + */ +GIT_EXTERN(void) git_diff_stats_free(git_diff_stats *stats); + +/** + * Patch ID options structure * - * We do run a binary content check on the two blobs and if either of the - * blobs looks like binary data, the `git_diff_delta` binary attribute will - * be set to 1 and no call to the hunk_cb nor line_cb will be made (unless - * you pass `GIT_DIFF_FORCE_TEXT` of course). + * Initialize with `GIT_PATCHID_OPTIONS_INIT`. Alternatively, you can + * use `git_diff_patchid_options_init`. * - * @return 0 on success, GIT_EUSER on non-zero callback, or error code */ -GIT_EXTERN(int) git_diff_blobs( - const git_blob *old_blob, - const git_blob *new_blob, - const git_diff_options *options, - git_diff_file_cb file_cb, - git_diff_hunk_cb hunk_cb, - git_diff_data_cb line_cb, - void *payload); +typedef struct git_diff_patchid_options { + unsigned int version; +} git_diff_patchid_options; + +/** Current version for the `git_diff_patchid_options` structure */ +#define GIT_DIFF_PATCHID_OPTIONS_VERSION 1 + +/** Static constructor for `git_diff_patchid_options` */ +#define GIT_DIFF_PATCHID_OPTIONS_INIT { GIT_DIFF_PATCHID_OPTIONS_VERSION } /** - * Directly run a diff between a blob and a buffer. + * Initialize git_diff_patchid_options structure * - * As with `git_diff_blobs`, comparing a blob and buffer lacks some context, - * so the `git_diff_file` parameters to the callbacks will be faked a la the - * rules for `git_diff_blobs()`. + * Initializes a `git_diff_patchid_options` with default values. Equivalent to + * creating an instance with `GIT_DIFF_PATCHID_OPTIONS_INIT`. * - * @return 0 on success, GIT_EUSER on non-zero callback, or error code + * @param opts The `git_diff_patchid_options` struct to initialize. + * @param version The struct version; pass `GIT_DIFF_PATCHID_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. */ -GIT_EXTERN(int) git_diff_blob_to_buffer( - const git_blob *old_blob, - const char *buffer, - size_t buffer_len, - const git_diff_options *options, - git_diff_file_cb file_cb, - git_diff_hunk_cb hunk_cb, - git_diff_data_cb data_cb, - void *payload); +GIT_EXTERN(int) git_diff_patchid_options_init( + git_diff_patchid_options *opts, + unsigned int version); -GIT_END_DECL +/** + * Calculate the patch ID for the given patch. + * + * Calculate a stable patch ID for the given patch by summing the + * hash of the file diffs, ignoring whitespace and line numbers. + * This can be used to derive whether two diffs are the same with + * a high probability. + * + * Currently, this function only calculates stable patch IDs, as + * defined in git-patch-id(1), and should in fact generate the + * same IDs as the upstream git project does. + * + * @param out Pointer where the calculated patch ID should be stored + * @param diff The diff to calculate the ID for + * @param opts Options for how to calculate the patch ID. This is + * intended for future changes, as currently no options are + * available. + * @return 0 on success, an error code otherwise. + */ +GIT_EXTERN(int) git_diff_patchid(git_oid *out, git_diff *diff, git_diff_patchid_options *opts); /** @} */ +GIT_END_DECL #endif diff --git a/include/git2/email.h b/include/git2/email.h new file mode 100644 index 00000000000..ad37e424985 --- /dev/null +++ b/include/git2/email.h @@ -0,0 +1,107 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_email_h__ +#define INCLUDE_git_email_h__ + +#include "common.h" +#include "diff.h" + +/** + * @file git2/email.h + * @brief Produce email-ready patches + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Formatting options for diff e-mail generation + */ +typedef enum { + /** Normal patch, the default */ + GIT_EMAIL_CREATE_DEFAULT = 0, + + /** Do not include patch numbers in the subject prefix. */ + GIT_EMAIL_CREATE_OMIT_NUMBERS = (1u << 0), + + /** + * Include numbers in the subject prefix even when the + * patch is for a single commit (1/1). + */ + GIT_EMAIL_CREATE_ALWAYS_NUMBER = (1u << 1), + + /** Do not perform rename or similarity detection. */ + GIT_EMAIL_CREATE_NO_RENAMES = (1u << 2) +} git_email_create_flags_t; + +/** + * Options for controlling the formatting of the generated e-mail. + */ +typedef struct { + unsigned int version; + + /** see `git_email_create_flags_t` above */ + uint32_t flags; + + /** Options to use when creating diffs */ + git_diff_options diff_opts; + + /** Options for finding similarities within diffs */ + git_diff_find_options diff_find_opts; + + /** + * The subject prefix, by default "PATCH". If set to an empty + * string ("") then only the patch numbers will be shown in the + * prefix. If the subject_prefix is empty and patch numbers + * are not being shown, the prefix will be omitted entirely. + */ + const char *subject_prefix; + + /** + * The starting patch number; this cannot be 0. By default, + * this is 1. + */ + size_t start_number; + + /** The "re-roll" number. By default, there is no re-roll. */ + size_t reroll_number; +} git_email_create_options; + +/** Current version for the `git_email_create_options` structure */ +#define GIT_EMAIL_CREATE_OPTIONS_VERSION 1 + +/** Static constructor for `git_email_create_options` + * + * By default, our options include rename detection and binary + * diffs to match `git format-patch`. + */ +#define GIT_EMAIL_CREATE_OPTIONS_INIT \ +{ \ + GIT_EMAIL_CREATE_OPTIONS_VERSION, \ + GIT_EMAIL_CREATE_DEFAULT, \ + { GIT_DIFF_OPTIONS_VERSION, GIT_DIFF_SHOW_BINARY, GIT_SUBMODULE_IGNORE_UNSPECIFIED, {NULL,0}, NULL, NULL, NULL, 3 }, \ + GIT_DIFF_FIND_OPTIONS_INIT \ +} + +/** + * Create a diff for a commit in mbox format for sending via email. + * The commit must not be a merge commit. + * + * @param out buffer to store the e-mail patch in + * @param commit commit to create a patch for + * @param opts email creation options + * @return 0 or an error code + */ +GIT_EXTERN(int) git_email_create_from_commit( + git_buf *out, + git_commit *commit, + const git_email_create_options *opts); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/errors.h b/include/git2/errors.h index 917f0699cd5..11413907e7c 100644 --- a/include/git2/errors.h +++ b/include/git2/errors.h @@ -11,110 +11,144 @@ /** * @file git2/errors.h - * @brief Git error handling routines and variables + * @brief Error handling routines and variables * @ingroup Git * @{ */ GIT_BEGIN_DECL /** Generic return codes */ -enum { - GIT_OK = 0, - GIT_ERROR = -1, - GIT_ENOTFOUND = -3, - GIT_EEXISTS = -4, - GIT_EAMBIGUOUS = -5, - GIT_EBUFS = -6, - GIT_EUSER = -7, - GIT_EBAREREPO = -8, - GIT_EORPHANEDHEAD = -9, - GIT_EUNMERGED = -10, - GIT_ENONFASTFORWARD = -11, - GIT_EINVALIDSPEC = -12, - GIT_EMERGECONFLICT = -13, +typedef enum { + /** + * No error occurred; the call was successful. + */ + GIT_OK = 0, - GIT_PASSTHROUGH = -30, - GIT_ITEROVER = -31, -}; + /** + * An error occurred; call `git_error_last` for more information. + */ + GIT_ERROR = -1, -typedef struct { - char *message; - int klass; -} git_error; + GIT_ENOTFOUND = -3, /**< Requested object could not be found. */ + GIT_EEXISTS = -4, /**< Object exists preventing operation. */ + GIT_EAMBIGUOUS = -5, /**< More than one object matches. */ + GIT_EBUFS = -6, /**< Output buffer too short to hold data. */ -/** Error classes */ -typedef enum { - GITERR_NOMEMORY, - GITERR_OS, - GITERR_INVALID, - GITERR_REFERENCE, - GITERR_ZLIB, - GITERR_REPOSITORY, - GITERR_CONFIG, - GITERR_REGEX, - GITERR_ODB, - GITERR_INDEX, - GITERR_OBJECT, - GITERR_NET, - GITERR_TAG, - GITERR_TREE, - GITERR_INDEXER, - GITERR_SSL, - GITERR_SUBMODULE, - GITERR_THREAD, - GITERR_STASH, - GITERR_CHECKOUT, - GITERR_FETCHHEAD, - GITERR_MERGE, -} git_error_t; + /** + * GIT_EUSER is a special error that is never generated by libgit2 + * code. You can return it from a callback (e.g to stop an iteration) + * to know that it was generated by the callback and not by libgit2. + */ + GIT_EUSER = -7, + + GIT_EBAREREPO = -8, /**< Operation not allowed on bare repository. */ + GIT_EUNBORNBRANCH = -9, /**< HEAD refers to branch with no commits. */ + GIT_EUNMERGED = -10, /**< Merge in progress prevented operation */ + GIT_ENONFASTFORWARD = -11, /**< Reference was not fast-forwardable */ + GIT_EINVALIDSPEC = -12, /**< Name/ref spec was not in a valid format */ + GIT_ECONFLICT = -13, /**< Checkout conflicts prevented operation */ + GIT_ELOCKED = -14, /**< Lock file prevented operation */ + GIT_EMODIFIED = -15, /**< Reference value does not match expected */ + GIT_EAUTH = -16, /**< Authentication error */ + GIT_ECERTIFICATE = -17, /**< Server certificate is invalid */ + GIT_EAPPLIED = -18, /**< Patch/merge has already been applied */ + GIT_EPEEL = -19, /**< The requested peel operation is not possible */ + GIT_EEOF = -20, /**< Unexpected EOF */ + GIT_EINVALID = -21, /**< Invalid operation or input */ + GIT_EUNCOMMITTED = -22, /**< Uncommitted changes in index prevented operation */ + GIT_EDIRECTORY = -23, /**< The operation is not valid for a directory */ + GIT_EMERGECONFLICT = -24, /**< A merge conflict exists and cannot continue */ + + GIT_PASSTHROUGH = -30, /**< A user-configured callback refused to act */ + GIT_ITEROVER = -31, /**< Signals end of iteration with iterator */ + GIT_RETRY = -32, /**< Internal only */ + GIT_EMISMATCH = -33, /**< Hashsum mismatch in object */ + GIT_EINDEXDIRTY = -34, /**< Unsaved changes in the index would be overwritten */ + GIT_EAPPLYFAIL = -35, /**< Patch application failed */ + GIT_EOWNER = -36, /**< The object is not owned by the current user */ + GIT_TIMEOUT = -37, /**< The operation timed out */ + GIT_EUNCHANGED = -38, /**< There were no changes */ + GIT_ENOTSUPPORTED = -39, /**< An option is not supported */ + GIT_EREADONLY = -40 /**< The subject is read-only */ +} git_error_code; /** - * Return the last `git_error` object that was generated for the - * current thread or NULL if no error has occurred. - * - * @return A git_error object. + * Error classes are the category of error. They reflect the area of the + * code where an error occurred. */ -GIT_EXTERN(const git_error *) giterr_last(void); +typedef enum { + GIT_ERROR_NONE = 0, + GIT_ERROR_NOMEMORY, + GIT_ERROR_OS, + GIT_ERROR_INVALID, + GIT_ERROR_REFERENCE, + GIT_ERROR_ZLIB, + GIT_ERROR_REPOSITORY, + GIT_ERROR_CONFIG, + GIT_ERROR_REGEX, + GIT_ERROR_ODB, + GIT_ERROR_INDEX, + GIT_ERROR_OBJECT, + GIT_ERROR_NET, + GIT_ERROR_TAG, + GIT_ERROR_TREE, + GIT_ERROR_INDEXER, + GIT_ERROR_SSL, + GIT_ERROR_SUBMODULE, + GIT_ERROR_THREAD, + GIT_ERROR_STASH, + GIT_ERROR_CHECKOUT, + GIT_ERROR_FETCHHEAD, + GIT_ERROR_MERGE, + GIT_ERROR_SSH, + GIT_ERROR_FILTER, + GIT_ERROR_REVERT, + GIT_ERROR_CALLBACK, + GIT_ERROR_CHERRYPICK, + GIT_ERROR_DESCRIBE, + GIT_ERROR_REBASE, + GIT_ERROR_FILESYSTEM, + GIT_ERROR_PATCH, + GIT_ERROR_WORKTREE, + GIT_ERROR_SHA, + GIT_ERROR_HTTP, + GIT_ERROR_INTERNAL, + GIT_ERROR_GRAFTS +} git_error_t; /** - * Clear the last library error that occurred for this thread. + * Structure to store extra details of the last error that occurred. + * + * This is kept on a per-thread basis if GIT_THREADS was defined when the + * library was build, otherwise one is kept globally for the library */ -GIT_EXTERN(void) giterr_clear(void); +typedef struct { + char *message; /**< The error message for the last error. */ + int klass; /**< The category of the last error. @type git_error_t */ +} git_error; /** - * Set the error message string for this thread. + * Return the last `git_error` object that was generated for the + * current thread. * - * This function is public so that custom ODB backends and the like can - * relay an error message through libgit2. Most regular users of libgit2 - * will never need to call this function -- actually, calling it in most - * circumstances (for example, calling from within a callback function) - * will just end up having the value overwritten by libgit2 internals. + * This function will never return NULL. * - * This error message is stored in thread-local storage and only applies - * to the particular thread that this libgit2 call is made from. + * Callers should not rely on this to determine whether an error has + * occurred. For error checking, callers should examine the return + * codes of libgit2 functions. * - * NOTE: Passing the `error_class` as GITERR_OS has a special behavior: we - * attempt to append the system default error message for the last OS error - * that occurred and then clear the last error. The specific implementation - * of looking up and clearing this last OS error will vary by platform. + * This call can only reliably report error messages when an error + * has occurred. (It may contain stale information if it is called + * after a different function that succeeds.) * - * @param error_class One of the `git_error_t` enum above describing the - * general subsystem that is responsible for the error. - * @param message The formatted error message to keep - */ -GIT_EXTERN(void) giterr_set_str(int error_class, const char *string); - -/** - * Set the error message to a special value for memory allocation failure. + * The memory for this object is managed by libgit2. It should not + * be freed. * - * The normal `giterr_set_str()` function attempts to `strdup()` the string - * that is passed in. This is not a good idea when the error in question - * is a memory allocation failure. That circumstance has a special setter - * function that sets the error string to a known and statically allocated - * internal value. + * @return A pointer to a `git_error` object that describes the error. */ -GIT_EXTERN(void) giterr_set_oom(void); +GIT_EXTERN(const git_error *) git_error_last(void); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/experimental.h b/include/git2/experimental.h new file mode 100644 index 00000000000..06435f9a76a --- /dev/null +++ b/include/git2/experimental.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_experimental_h__ +#define INCLUDE_experimental_h__ + +/* + * This file exists to support users who build libgit2 with a bespoke + * build system and do not use our cmake configuration. Normally, cmake + * will create `experimental.h` from the `experimental.h.in` file and + * will include the generated file instead of this one. For non-cmake + * users, we bundle this `experimental.h` file which will be used + * instead. + */ + +#endif diff --git a/include/git2/filter.h b/include/git2/filter.h new file mode 100644 index 00000000000..cf6c5f59d97 --- /dev/null +++ b/include/git2/filter.h @@ -0,0 +1,283 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_filter_h__ +#define INCLUDE_git_filter_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" +#include "buffer.h" + +/** + * @file git2/filter.h + * @brief Filters modify files during checkout or commit + * @ingroup Git + * + * During checkout, filters update a file from a "canonical" state to + * a format appropriate for the local filesystem; during commit, filters + * produce the canonical state. For example, on Windows, the line ending + * filters _may_ take a canonical state (with Unix-style newlines) in + * the repository, and place the contents on-disk with Windows-style + * `\r\n` line endings. + * @{ + */ +GIT_BEGIN_DECL + +/** + * Filters are applied in one of two directions: smudging - which is + * exporting a file from the Git object database to the working directory, + * and cleaning - which is importing a file from the working directory to + * the Git object database. These values control which direction of + * change is being applied. + */ +typedef enum { + GIT_FILTER_TO_WORKTREE = 0, + GIT_FILTER_SMUDGE = GIT_FILTER_TO_WORKTREE, + GIT_FILTER_TO_ODB = 1, + GIT_FILTER_CLEAN = GIT_FILTER_TO_ODB +} git_filter_mode_t; + +/** + * Filter option flags. + */ +typedef enum { + GIT_FILTER_DEFAULT = 0u, + + /** Don't error for `safecrlf` violations, allow them to continue. */ + GIT_FILTER_ALLOW_UNSAFE = (1u << 0), + + /** Don't load `/etc/gitattributes` (or the system equivalent) */ + GIT_FILTER_NO_SYSTEM_ATTRIBUTES = (1u << 1), + + /** Load attributes from `.gitattributes` in the root of HEAD */ + GIT_FILTER_ATTRIBUTES_FROM_HEAD = (1u << 2), + + /** + * Load attributes from `.gitattributes` in a given commit. + * This can only be specified in a `git_filter_options`. + */ + GIT_FILTER_ATTRIBUTES_FROM_COMMIT = (1u << 3) +} git_filter_flag_t; + +/** + * Filtering options + */ +typedef struct { + unsigned int version; + + /** See `git_filter_flag_t` above */ + uint32_t flags; + +#ifdef GIT_DEPRECATE_HARD + void *reserved; +#else + git_oid *commit_id; +#endif + + /** + * The commit to load attributes from, when + * `GIT_FILTER_ATTRIBUTES_FROM_COMMIT` is specified. + */ + git_oid attr_commit_id; +} git_filter_options; + +/** Current version for the `git_filter_options` structure */ +#define GIT_FILTER_OPTIONS_VERSION 1 + +/** Static constructor for `git_filter_options` */ +#define GIT_FILTER_OPTIONS_INIT {GIT_FILTER_OPTIONS_VERSION} + +/** + * A filter that can transform file data + * + * This represents a filter that can be used to transform or even replace + * file data. Libgit2 includes one built in filter and it is possible to + * write your own (see git2/sys/filter.h for information on that). + * + * The two builtin filters are: + * + * * "crlf" which uses the complex rules with the "text", "eol", and + * "crlf" file attributes to decide how to convert between LF and CRLF + * line endings + * * "ident" which replaces "$Id$" in a blob with "$Id: $" upon + * checkout and replaced "$Id: $" with "$Id$" on checkin. + */ +typedef struct git_filter git_filter; + +/** + * List of filters to be applied + * + * This represents a list of filters to be applied to a file / blob. You + * can build the list with one call, apply it with another, and dispose it + * with a third. In typical usage, there are not many occasions where a + * git_filter_list is needed directly since the library will generally + * handle conversions for you, but it can be convenient to be able to + * build and apply the list sometimes. + */ +typedef struct git_filter_list git_filter_list; + +/** + * Load the filter list for a given path. + * + * This will return 0 (success) but set the output git_filter_list to NULL + * if no filters are requested for the given file. + * + * @param filters Output newly created git_filter_list (or NULL) + * @param repo Repository object that contains `path` + * @param blob The blob to which the filter will be applied (if known) + * @param path Relative path of the file to be filtered + * @param mode Filtering direction (WT->ODB or ODB->WT) + * @param flags Combination of `git_filter_flag_t` flags + * @return 0 on success (which could still return NULL if no filters are + * needed for the requested file), <0 on error + */ +GIT_EXTERN(int) git_filter_list_load( + git_filter_list **filters, + git_repository *repo, + git_blob *blob, /* can be NULL */ + const char *path, + git_filter_mode_t mode, + uint32_t flags); + +/** + * Load the filter list for a given path. + * + * This will return 0 (success) but set the output git_filter_list to NULL + * if no filters are requested for the given file. + * + * @param filters Output newly created git_filter_list (or NULL) + * @param repo Repository object that contains `path` + * @param blob The blob to which the filter will be applied (if known) + * @param path Relative path of the file to be filtered + * @param mode Filtering direction (WT->ODB or ODB->WT) + * @param opts The `git_filter_options` to use when loading filters + * @return 0 on success (which could still return NULL if no filters are + * needed for the requested file), <0 on error + */ +GIT_EXTERN(int) git_filter_list_load_ext( + git_filter_list **filters, + git_repository *repo, + git_blob *blob, + const char *path, + git_filter_mode_t mode, + git_filter_options *opts); + +/** + * Query the filter list to see if a given filter (by name) will run. + * The built-in filters "crlf" and "ident" can be queried, otherwise this + * is the name of the filter specified by the filter attribute. + * + * This will return 0 if the given filter is not in the list, or 1 if + * the filter will be applied. + * + * @param filters A loaded git_filter_list (or NULL) + * @param name The name of the filter to query + * @return 1 if the filter is in the list, 0 otherwise + */ +GIT_EXTERN(int) git_filter_list_contains( + git_filter_list *filters, + const char *name); + +/** + * Apply filter list to a data buffer. + * + * @param out Buffer to store the result of the filtering + * @param filters A loaded git_filter_list (or NULL) + * @param in Buffer containing the data to filter + * @param in_len The length of the input buffer + * @return 0 on success, an error code otherwise + */ +GIT_EXTERN(int) git_filter_list_apply_to_buffer( + git_buf *out, + git_filter_list *filters, + const char *in, + size_t in_len); + +/** + * Apply a filter list to the contents of a file on disk + * + * @param out buffer into which to store the filtered file + * @param filters the list of filters to apply + * @param repo the repository in which to perform the filtering + * @param path the path of the file to filter, a relative path will be + * taken as relative to the workdir + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_filter_list_apply_to_file( + git_buf *out, + git_filter_list *filters, + git_repository *repo, + const char *path); + +/** + * Apply a filter list to the contents of a blob + * + * @param out buffer into which to store the filtered file + * @param filters the list of filters to apply + * @param blob the blob to filter + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_filter_list_apply_to_blob( + git_buf *out, + git_filter_list *filters, + git_blob *blob); + +/** + * Apply a filter list to an arbitrary buffer as a stream + * + * @param filters the list of filters to apply + * @param buffer the buffer to filter + * @param len the size of the buffer + * @param target the stream into which the data will be written + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_filter_list_stream_buffer( + git_filter_list *filters, + const char *buffer, + size_t len, + git_writestream *target); + +/** + * Apply a filter list to a file as a stream + * + * @param filters the list of filters to apply + * @param repo the repository in which to perform the filtering + * @param path the path of the file to filter, a relative path will be + * taken as relative to the workdir + * @param target the stream into which the data will be written + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_filter_list_stream_file( + git_filter_list *filters, + git_repository *repo, + const char *path, + git_writestream *target); + +/** + * Apply a filter list to a blob as a stream + * + * @param filters the list of filters to apply + * @param blob the blob to filter + * @param target the stream into which the data will be written + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_filter_list_stream_blob( + git_filter_list *filters, + git_blob *blob, + git_writestream *target); + +/** + * Free a git_filter_list + * + * @param filters A git_filter_list created by `git_filter_list_load` + */ +GIT_EXTERN(void) git_filter_list_free(git_filter_list *filters); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/global.h b/include/git2/global.h new file mode 100644 index 00000000000..f15eb2d2880 --- /dev/null +++ b/include/git2/global.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_global_h__ +#define INCLUDE_git_global_h__ + +#include "common.h" + +/** + * @file git2/global.h + * @brief libgit2 library initializer and shutdown functionality + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Init the global state + * + * This function must be called before any other libgit2 function in + * order to set up global state and threading. + * + * This function may be called multiple times - it will return the number + * of times the initialization has been called (including this one) that have + * not subsequently been shutdown. + * + * @return the number of initializations of the library, or an error code. + */ +GIT_EXTERN(int) git_libgit2_init(void); + +/** + * Shutdown the global state + * + * Clean up the global state and threading context after calling it as + * many times as `git_libgit2_init()` was called - it will return the + * number of remainining initializations that have not been shutdown + * (after this one). + * + * @return the number of remaining initializations of the library, or an + * error code. + */ +GIT_EXTERN(int) git_libgit2_shutdown(void); + +/** @} */ +GIT_END_DECL + +#endif + diff --git a/include/git2/graph.h b/include/git2/graph.h index 5850aa6e28d..1792020a4be 100644 --- a/include/git2/graph.h +++ b/include/git2/graph.h @@ -13,7 +13,7 @@ /** * @file git2/graph.h - * @brief Git graph traversal routines + * @brief Graph traversal routines * @defgroup git_revwalk Git graph traversal routines * @ingroup Git * @{ @@ -23,14 +23,56 @@ GIT_BEGIN_DECL /** * Count the number of unique commits between two commit objects * - * @param ahead number of commits, starting at `one`, unique from commits in `two` - * @param behind number of commits, starting at `two`, unique from commits in `one` + * There is no need for branches containing the commits to have any + * upstream relationship, but it helps to think of one as a branch and + * the other as its upstream, the `ahead` and `behind` values will be + * what git would report for the branches. + * + * @param ahead number of unique from commits in `upstream` + * @param behind number of unique from commits in `local` + * @param repo the repository where the commits exist + * @param local the commit for local + * @param upstream the commit for upstream + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_graph_ahead_behind(size_t *ahead, size_t *behind, git_repository *repo, const git_oid *local, const git_oid *upstream); + + +/** + * Determine if a commit is the descendant of another commit. + * + * Note that a commit is not considered a descendant of itself, in contrast + * to `git merge-base --is-ancestor`. + * * @param repo the repository where the commits exist - * @param one one of the commits - * @param two the other commit + * @param commit a previously loaded commit + * @param ancestor a potential ancestor commit + * @return 1 if the given commit is a descendant of the potential ancestor, + * 0 if not, error code otherwise. */ -GIT_EXTERN(int) git_graph_ahead_behind(size_t *ahead, size_t *behind, git_repository *repo, const git_oid *one, const git_oid *two); +GIT_EXTERN(int) git_graph_descendant_of( + git_repository *repo, + const git_oid *commit, + const git_oid *ancestor); + +/** + * Determine if a commit is reachable from any of a list of commits by + * following parent edges. + * + * @param repo the repository where the commits exist + * @param commit a previously loaded commit + * @param descendant_array oids of the commits + * @param length the number of commits in the provided `descendant_array` + * @return 1 if the given commit is an ancestor of any of the given potential + * descendants, 0 if not, error code otherwise. + */ +GIT_EXTERN(int) git_graph_reachable_from_any( + git_repository *repo, + const git_oid *commit, + const git_oid descendant_array[], + size_t length); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/ignore.h b/include/git2/ignore.h index d0c1877a8b9..730f2214ba5 100644 --- a/include/git2/ignore.h +++ b/include/git2/ignore.h @@ -10,6 +10,15 @@ #include "common.h" #include "types.h" +/** + * @file git2/ignore.h + * @brief Ignore particular untracked files + * @ingroup Git + * @{ + * + * When examining the repository status, git can optionally ignore + * specified untracked files. + */ GIT_BEGIN_DECL /** @@ -29,7 +38,7 @@ GIT_BEGIN_DECL * This would add three rules to the ignores. * * @param repo The repository to add ignore rules to. - * @param rules Text of rules, a la the contents of a .gitignore file. + * @param rules Text of rules, the contents to add on a .gitignore file. * It is okay to have multiple rules in the text; if so, * each rule should be terminated with a newline. * @return 0 on success @@ -59,8 +68,8 @@ GIT_EXTERN(int) git_ignore_clear_internal_rules( * given file. This indicates if the file would be ignored regardless of * whether the file is already in the index or committed to the repository. * - * One way to think of this is if you were to do "git add ." on the - * directory containing the file, would it be added or not? + * One way to think of this is if you were to do "git check-ignore --no-index" + * on the given file, would it be shown or not? * * @param ignored boolean returning 0 if the file is not ignored, 1 if it is * @param repo a repository object @@ -73,6 +82,7 @@ GIT_EXTERN(int) git_ignore_path_is_ignored( git_repository *repo, const char *path); +/** @} */ GIT_END_DECL #endif diff --git a/include/git2/index.h b/include/git2/index.h index 9f9d144cf02..a6ad1d88bcd 100644 --- a/include/git2/index.h +++ b/include/git2/index.h @@ -11,99 +11,224 @@ #include "indexer.h" #include "types.h" #include "oid.h" +#include "strarray.h" /** * @file git2/index.h - * @brief Git index parsing and manipulation routines + * @brief Index (aka "cache" aka "staging area") * @defgroup git_index Git index parsing and manipulation routines * @ingroup Git + * + * The index (or "cache", or "staging area") is the contents of the + * next commit. In addition, the index stores other data, such as + * conflicts that occurred during the last merge operation, and + * the "treecache" to speed up various on-disk operations. * @{ */ GIT_BEGIN_DECL -#define GIT_IDXENTRY_NAMEMASK (0x0fff) -#define GIT_IDXENTRY_STAGEMASK (0x3000) -#define GIT_IDXENTRY_EXTENDED (0x4000) -#define GIT_IDXENTRY_VALID (0x8000) -#define GIT_IDXENTRY_STAGESHIFT 12 +/** Time structure used in a git index entry */ +typedef struct { + int32_t seconds; + /* nsec should not be stored as time_t compatible */ + uint32_t nanoseconds; +} git_index_time; -/* - * Flags are divided into two parts: in-memory flags and - * on-disk ones. Flags in GIT_IDXENTRY_EXTENDED_FLAGS - * will get saved on-disk. +/** + * In-memory representation of a file entry in the index. + * + * This is a public structure that represents a file entry in the index. + * The meaning of the fields corresponds to core Git's documentation (in + * "Documentation/gitformat-index.adoc"). * - * In-memory only flags: + * The `flags` field consists of a number of bit fields which can be + * accessed via the first set of `GIT_INDEX_ENTRY_...` bitmasks below. + * These flags are all read from and persisted to disk. + * + * The `flags_extended` field also has a number of bit fields which can be + * accessed via the later `GIT_INDEX_ENTRY_...` bitmasks below. Some of + * these flags are read from and written to disk, but some are set aside + * for in-memory only reference. + * + * Note that the time and size fields are truncated to 32 bits. This + * is enough to detect changes, which is enough for the index to + * function as a cache, but it should not be taken as an authoritative + * source for that data. */ -#define GIT_IDXENTRY_UPDATE (1 << 0) -#define GIT_IDXENTRY_REMOVE (1 << 1) -#define GIT_IDXENTRY_UPTODATE (1 << 2) -#define GIT_IDXENTRY_ADDED (1 << 3) +typedef struct git_index_entry { + git_index_time ctime; + git_index_time mtime; -#define GIT_IDXENTRY_HASHED (1 << 4) -#define GIT_IDXENTRY_UNHASHED (1 << 5) -#define GIT_IDXENTRY_WT_REMOVE (1 << 6) /* remove in work directory */ -#define GIT_IDXENTRY_CONFLICTED (1 << 7) + uint32_t dev; + uint32_t ino; + uint32_t mode; + uint32_t uid; + uint32_t gid; + uint32_t file_size; -#define GIT_IDXENTRY_UNPACKED (1 << 8) -#define GIT_IDXENTRY_NEW_SKIP_WORKTREE (1 << 9) + git_oid id; -/* - * Extended on-disk flags: - */ -#define GIT_IDXENTRY_INTENT_TO_ADD (1 << 13) -#define GIT_IDXENTRY_SKIP_WORKTREE (1 << 14) -/* GIT_IDXENTRY_EXTENDED2 is for future extension */ -#define GIT_IDXENTRY_EXTENDED2 (1 << 15) + uint16_t flags; + uint16_t flags_extended; -#define GIT_IDXENTRY_EXTENDED_FLAGS (GIT_IDXENTRY_INTENT_TO_ADD | GIT_IDXENTRY_SKIP_WORKTREE) + const char *path; +} git_index_entry; -/** Time used in a git index entry */ -typedef struct { - git_time_t seconds; - /* nsec should not be stored as time_t compatible */ - unsigned int nanoseconds; -} git_index_time; +/** + * Bitmasks for on-disk fields of `git_index_entry`'s `flags` + * + * These bitmasks match the four fields in the `git_index_entry` `flags` + * value both in memory and on disk. You can use them to interpret the + * data in the `flags`. + */ -/** Memory representation of a file entry in the index. */ -typedef struct git_index_entry { - git_index_time ctime; - git_index_time mtime; +/** Mask for name length */ +#define GIT_INDEX_ENTRY_NAMEMASK (0x0fff) +/** Mask for index entry stage */ +#define GIT_INDEX_ENTRY_STAGEMASK (0x3000) +/** Shift bits for index entry */ +#define GIT_INDEX_ENTRY_STAGESHIFT 12 - unsigned int dev; - unsigned int ino; - unsigned int mode; - unsigned int uid; - unsigned int gid; - git_off_t file_size; +/** + * Flags for index entries + */ +typedef enum { + GIT_INDEX_ENTRY_EXTENDED = (0x4000), + GIT_INDEX_ENTRY_VALID = (0x8000) +} git_index_entry_flag_t; + +/** + * Macro to get the stage value (0 for the "main index", or a conflict + * value) from an index entry. + */ +#define GIT_INDEX_ENTRY_STAGE(E) \ + (((E)->flags & GIT_INDEX_ENTRY_STAGEMASK) >> GIT_INDEX_ENTRY_STAGESHIFT) - git_oid oid; +/** + * Macro to set the stage value (0 for the "main index", or a conflict + * value) for an index entry. + */ +#define GIT_INDEX_ENTRY_STAGE_SET(E,S) do { \ + (E)->flags = ((E)->flags & ~GIT_INDEX_ENTRY_STAGEMASK) | \ + (((S) & 0x03) << GIT_INDEX_ENTRY_STAGESHIFT); } while (0) - unsigned short flags; - unsigned short flags_extended; +/** + * Bitmasks for on-disk fields of `git_index_entry`'s `flags_extended` + * + * In memory, the `flags_extended` fields are divided into two parts: the + * fields that are read from and written to disk, and other fields that + * in-memory only and used by libgit2. Only the flags in + * `GIT_INDEX_ENTRY_EXTENDED_FLAGS` will get saved on-disk. + * + * Thee first three bitmasks match the three fields in the + * `git_index_entry` `flags_extended` value that belong on disk. You + * can use them to interpret the data in the `flags_extended`. + * + * The rest of the bitmasks match the other fields in the `git_index_entry` + * `flags_extended` value that are only used in-memory by libgit2. + * You can use them to interpret the data in the `flags_extended`. + * + */ +typedef enum { + GIT_INDEX_ENTRY_INTENT_TO_ADD = (1 << 13), + GIT_INDEX_ENTRY_SKIP_WORKTREE = (1 << 14), - char *path; -} git_index_entry; + GIT_INDEX_ENTRY_EXTENDED_FLAGS = (GIT_INDEX_ENTRY_INTENT_TO_ADD | GIT_INDEX_ENTRY_SKIP_WORKTREE), -/** Representation of a resolve undo entry in the index. */ -typedef struct git_index_reuc_entry { - unsigned int mode[3]; - git_oid oid[3]; - char *path; -} git_index_reuc_entry; + GIT_INDEX_ENTRY_UPTODATE = (1 << 2) +} git_index_entry_extended_flag_t; /** Capabilities of system that affect index actions. */ -enum { - GIT_INDEXCAP_IGNORE_CASE = 1, - GIT_INDEXCAP_NO_FILEMODE = 2, - GIT_INDEXCAP_NO_SYMLINKS = 4, - GIT_INDEXCAP_FROM_OWNER = ~0u -}; +typedef enum { + GIT_INDEX_CAPABILITY_IGNORE_CASE = 1, + GIT_INDEX_CAPABILITY_NO_FILEMODE = 2, + GIT_INDEX_CAPABILITY_NO_SYMLINKS = 4, + GIT_INDEX_CAPABILITY_FROM_OWNER = -1 +} git_index_capability_t; + -/** @name Index File Functions +/** + * Callback for APIs that add/remove/update files matching pathspec * - * These functions work on the index file itself. + * @param path the path + * @param matched_pathspec the given pathspec + * @param payload the user-specified payload + * @return 0 to continue with the index operation, positive number to skip this file for the index operation, negative number on failure */ -/**@{*/ +typedef int GIT_CALLBACK(git_index_matched_path_cb)( + const char *path, const char *matched_pathspec, void *payload); + +/** Flags for APIs that add files matching pathspec */ +typedef enum { + GIT_INDEX_ADD_DEFAULT = 0, + GIT_INDEX_ADD_FORCE = (1u << 0), + GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH = (1u << 1), + GIT_INDEX_ADD_CHECK_PATHSPEC = (1u << 2) +} git_index_add_option_t; + +/** Git index stage states */ +typedef enum { + /** + * Match any index stage. + * + * Some index APIs take a stage to match; pass this value to match + * any entry matching the path regardless of stage. + */ + GIT_INDEX_STAGE_ANY = -1, + + /** A normal staged file in the index. */ + GIT_INDEX_STAGE_NORMAL = 0, + + /** The ancestor side of a conflict. */ + GIT_INDEX_STAGE_ANCESTOR = 1, + + /** The "ours" side of a conflict. */ + GIT_INDEX_STAGE_OURS = 2, + + /** The "theirs" side of a conflict. */ + GIT_INDEX_STAGE_THEIRS = 3 +} git_index_stage_t; + +/** + * The options for opening or creating an index. + * + * Initialize with `GIT_INDEX_OPTIONS_INIT`. Alternatively, you can + * use `git_index_options_init`. + * + * @options[version] GIT_INDEX_OPTIONS_VERSION + * @options[init_macro] GIT_INDEX_OPTIONS_INIT + * @options[init_function] git_index_options_init + */ +typedef struct git_index_options { + unsigned int version; /**< The version */ + + /** + * The object ID type for the object IDs that exist in the index. + * + * If this is not specified, this defaults to `GIT_OID_SHA1`. + */ + git_oid_t oid_type; +} git_index_options; + +/** Current version for the `git_index_options` structure */ +#define GIT_INDEX_OPTIONS_VERSION 1 + +/** Static constructor for `git_index_options` */ +#define GIT_INDEX_OPTIONS_INIT { GIT_INDEX_OPTIONS_VERSION } + +/** + * Initialize git_index_options structure + * + * Initializes a `git_index_options` with default values. Equivalent to creating + * an instance with GIT_INDEX_OPTIONS_INIT. + * + * @param opts The `git_index_options` struct to initialize. + * @param version The struct version; pass `GIT_INDEX_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_index_options_init( + git_index_options *opts, + unsigned int version); /** * Create a new bare Git index object as a memory representation @@ -111,19 +236,42 @@ enum { * to back it. * * Since there is no ODB or working directory behind this index, - * any Index methods which rely on these (e.g. index_add) will - * fail with the GIT_EBAREINDEX error code. + * any Index methods which rely on these (e.g. index_add_bypath) + * will fail with the GIT_ERROR error code. * * If you need to access the index of an actual repository, * use the `git_repository_index` wrapper. * * The index must be freed once it's no longer in use. * - * @param out the pointer for the new index + * @note This API only supports SHA1 indexes + * @see git_index_open_ext + * + * @param index_out the pointer for the new index + * @param index_path the path to the index file in disk + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_open( + git_index **index_out, + const char *index_path); + +#ifdef GIT_EXPERIMENTAL_SHA256 + +/** + * Creates a new bare Git index object, without a repository to back + * it. This index object is capable of storing SHA256 objects. + * + * @param index_out the pointer for the new index * @param index_path the path to the index file in disk + * @param opts the options for opening the index, or NULL * @return 0 or an error code */ -GIT_EXTERN(int) git_index_open(git_index **out, const char *index_path); +GIT_EXTERN(int) git_index_open_ext( + git_index **index_out, + const char *index_path, + const git_index_options *opts); + +#endif /** * Create an in-memory index object. @@ -133,10 +281,28 @@ GIT_EXTERN(int) git_index_open(git_index **out, const char *index_path); * * The index must be freed once it's no longer in use. * - * @param out the pointer for the new index + * @note This API only supports SHA1 indexes + * @see git_index_new_ext + * + * @param index_out the pointer for the new index + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_new(git_index **index_out); + +#ifdef GIT_EXPERIMENTAL_SHA256 + +/** + * Create an in-memory index object. + * + * @param index_out the pointer for the new index + * @param opts the options for opening the index, or NULL * @return 0 or an error code */ -GIT_EXTERN(int) git_index_new(git_index **out); +GIT_EXTERN(int) git_index_new_ext( + git_index **index_out, + const git_index_options *opts); + +#endif /** * Free an existing index object. @@ -157,31 +323,66 @@ GIT_EXTERN(git_repository *) git_index_owner(const git_index *index); * Read index capabilities flags. * * @param index An existing index object - * @return A combination of GIT_INDEXCAP values + * @return A combination of GIT_INDEX_CAPABILITY values */ -GIT_EXTERN(unsigned int) git_index_caps(const git_index *index); +GIT_EXTERN(int) git_index_caps(const git_index *index); /** * Set index capabilities flags. * - * If you pass `GIT_INDEXCAP_FROM_OWNER` for the caps, then the + * If you pass `GIT_INDEX_CAPABILITY_FROM_OWNER` for the caps, then * capabilities will be read from the config of the owner object, * looking at `core.ignorecase`, `core.filemode`, `core.symlinks`. * * @param index An existing index object - * @param caps A combination of GIT_INDEXCAP values + * @param caps A combination of GIT_INDEX_CAPABILITY values * @return 0 on success, -1 on failure */ -GIT_EXTERN(int) git_index_set_caps(git_index *index, unsigned int caps); +GIT_EXTERN(int) git_index_set_caps(git_index *index, int caps); /** - * Update the contents of an existing index object in memory - * by reading from the hard disk. + * Get index on-disk version. + * + * Valid return values are 2, 3, or 4. If 3 is returned, an index + * with version 2 may be written instead, if the extension data in + * version 3 is not necessary. + * + * @param index An existing index object + * @return the index version + */ +GIT_EXTERN(unsigned int) git_index_version(git_index *index); + +/** + * Set index on-disk version. + * + * Valid values are 2, 3, or 4. If 2 is given, git_index_write may + * write an index with version 3 instead, if necessary to accurately + * represent the index. + * + * @param index An existing index object + * @param version The new version number + * @return 0 on success, -1 on failure + */ +GIT_EXTERN(int) git_index_set_version(git_index *index, unsigned int version); + +/** + * Update the contents of an existing index object in memory by reading + * from the hard disk. + * + * If `force` is true, this performs a "hard" read that discards in-memory + * changes and always reloads the on-disk index data. If there is no + * on-disk version, the index will be cleared. + * + * If `force` is false, this does a "soft" read that reloads the index + * data from disk only if it has changed since the last time it was + * loaded. Purely in-memory index data will be untouched. Be aware: if + * there are changes on disk, unwritten in-memory changes are discarded. * * @param index an existing index object + * @param force if true, always reload, vs. only read if file has changed * @return 0 or an error code */ -GIT_EXTERN(int) git_index_read(git_index *index); +GIT_EXTERN(int) git_index_read(git_index *index, int force); /** * Write an existing index object from memory back to disk @@ -192,6 +393,29 @@ GIT_EXTERN(int) git_index_read(git_index *index); */ GIT_EXTERN(int) git_index_write(git_index *index); +/** + * Get the full path to the index file on disk. + * + * @param index an existing index object + * @return path to index file or NULL for in-memory index + */ +GIT_EXTERN(const char *) git_index_path(const git_index *index); + +#ifndef GIT_DEPRECATE_HARD +/** + * Get the checksum of the index + * + * This checksum is the SHA-1 hash over the index file (except the + * last 20 bytes which are the checksum itself). In cases where the + * index does not exist on-disk, it will be zeroed out. + * + * @deprecated this function is deprecated with no replacement + * @param index an existing index object + * @return a pointer to the checksum of the index + */ +GIT_EXTERN(const git_oid *) git_index_checksum(git_index *index); +#endif + /** * Read a tree into the index file with stats * @@ -233,7 +457,7 @@ GIT_EXTERN(int) git_index_write_tree(git_oid *out, git_index *index); * * The index must not contain any file in conflict. * - * @param out Pointer where to store OID of the the written tree + * @param out Pointer where to store OID of the written tree * @param index Index to write * @param repo Repository where to write the tree * @return 0 on success, GIT_EUNMERGED when the index is not clean @@ -262,21 +486,21 @@ GIT_EXTERN(size_t) git_index_entrycount(const git_index *index); /** * Clear the contents (all the entries) of an index object. - * This clears the index object in memory; changes must be manually - * written to disk for them to take effect. + * + * This clears the index object in memory; changes must be explicitly + * written to disk for them to take effect persistently. * * @param index an existing index object + * @return 0 on success, error code < 0 on failure */ -GIT_EXTERN(void) git_index_clear(git_index *index); +GIT_EXTERN(int) git_index_clear(git_index *index); /** * Get a pointer to one of the entries in the index * - * The values of this entry can be modified (except the path) - * and the changes will be written back to disk on the next - * write() call. - * - * The entry should not be freed by the caller. + * The entry is not modifiable and should not be freed. Because the + * `git_index_entry` struct is a publicly defined struct, you should + * be able to make your own permanent copy of the data if necessary. * * @param index an existing index object * @param n the position of the entry @@ -288,11 +512,9 @@ GIT_EXTERN(const git_index_entry *) git_index_get_byindex( /** * Get a pointer to one of the entries in the index * - * The values of this entry can be modified (except the path) - * and the changes will be written back to disk on the next - * write() call. - * - * The entry should not be freed by the caller. + * The entry is not modifiable and should not be freed. Because the + * `git_index_entry` struct is a publicly defined struct, you should + * be able to make your own permanent copy of the data if necessary. * * @param index an existing index object * @param path path to search @@ -342,16 +564,65 @@ GIT_EXTERN(int) git_index_add(git_index *index, const git_index_entry *source_en /** * Return the stage number from a git index entry * - * This entry is calculated from the entry's flag - * attribute like this: + * This entry is calculated from the entry's flag attribute like this: * - * (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT + * (entry->flags & GIT_INDEX_ENTRY_STAGEMASK) >> GIT_INDEX_ENTRY_STAGESHIFT * * @param entry The entry - * @returns the stage number + * @return the stage number */ GIT_EXTERN(int) git_index_entry_stage(const git_index_entry *entry); +/** + * Return whether the given index entry is a conflict (has a high stage + * entry). This is simply shorthand for `git_index_entry_stage > 0`. + * + * @param entry The entry + * @return 1 if the entry is a conflict entry, 0 otherwise + */ +GIT_EXTERN(int) git_index_entry_is_conflict(const git_index_entry *entry); + +/**@}*/ + +/** @name Index Entry Iteration Functions + * + * These functions provide an iterator for index entries. + */ +/**@{*/ + +/** + * Create an iterator that will return every entry contained in the + * index at the time of creation. Entries are returned in order, + * sorted by path. This iterator is backed by a snapshot that allows + * callers to modify the index while iterating without affecting the + * iterator. + * + * @param iterator_out The newly created iterator + * @param index The index to iterate + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_index_iterator_new( + git_index_iterator **iterator_out, + git_index *index); + +/** + * Return the next index entry in-order from the iterator. + * + * @param out Pointer to store the index entry in + * @param iterator The iterator + * @return 0, GIT_ITEROVER on iteration completion or an error code + */ +GIT_EXTERN(int) git_index_iterator_next( + const git_index_entry **out, + git_index_iterator *iterator); + +/** + * Free the index iterator + * + * @param iterator The iterator to free + */ +GIT_EXTERN(void) git_index_iterator_free(git_index_iterator *iterator); + /**@}*/ /** @name Workdir Index Entry Functions @@ -383,6 +654,37 @@ GIT_EXTERN(int) git_index_entry_stage(const git_index_entry *entry); */ GIT_EXTERN(int) git_index_add_bypath(git_index *index, const char *path); +/** + * Add or update an index entry from a buffer in memory + * + * This method will create a blob in the repository that owns the + * index and then add the index entry to the index. The `path` of the + * entry represents the position of the blob relative to the + * repository's root folder. + * + * If a previous index entry exists that has the same path as the + * given 'entry', it will be replaced. Otherwise, the 'entry' will be + * added. + * + * This forces the file to be added to the index, not looking + * at gitignore rules. Those rules can be evaluated through + * the git_status APIs (in status.h) before calling this. + * + * If this file currently is the result of a merge conflict, this + * file will no longer be marked as conflicting. The data about + * the conflict will be moved to the "resolve undo" (REUC) section. + * + * @param index an existing index object + * @param entry filename to add + * @param buffer data to be written into the blob + * @param len length of the data + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_add_from_buffer( + git_index *index, + const git_index_entry *entry, + const void *buffer, size_t len); + /** * Remove an index entry corresponding to a file on disk * @@ -400,16 +702,130 @@ GIT_EXTERN(int) git_index_add_bypath(git_index *index, const char *path); GIT_EXTERN(int) git_index_remove_bypath(git_index *index, const char *path); /** - * Find the first index of any entries which point to given + * Add or update index entries matching files in the working directory. + * + * This method will fail in bare index instances. + * + * The `pathspec` is a list of file names or shell glob patterns that will + * be matched against files in the repository's working directory. Each + * file that matches will be added to the index (either updating an + * existing entry or adding a new entry). You can disable glob expansion + * and force exact matching with the `GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH` + * flag. + * + * Files that are ignored will be skipped (unlike `git_index_add_bypath`). + * If a file is already tracked in the index, then it *will* be updated + * even if it is ignored. Pass the `GIT_INDEX_ADD_FORCE` flag to skip + * the checking of ignore rules. + * + * To emulate `git add -A` and generate an error if the pathspec contains + * the exact path of an ignored file (when not using FORCE), add the + * `GIT_INDEX_ADD_CHECK_PATHSPEC` flag. This checks that each entry + * in the `pathspec` that is an exact match to a filename on disk is + * either not ignored or already in the index. If this check fails, the + * function will return GIT_EINVALIDSPEC. + * + * To emulate `git add -A` with the "dry-run" option, just use a callback + * function that always returns a positive value. See below for details. + * + * If any files are currently the result of a merge conflict, those files + * will no longer be marked as conflicting. The data about the conflicts + * will be moved to the "resolve undo" (REUC) section. + * + * If you provide a callback function, it will be invoked on each matching + * item in the working directory immediately *before* it is added to / + * updated in the index. Returning zero will add the item to the index, + * greater than zero will skip the item, and less than zero will abort the + * scan and return that value to the caller. + * + * @param index an existing index object + * @param pathspec array of path patterns + * @param flags combination of git_index_add_option_t flags + * @param callback notification callback for each added/updated path (also + * gets index of matching pathspec entry); can be NULL; + * return 0 to add, >0 to skip, <0 to abort scan. + * @param payload payload passed through to callback function + * @return 0 on success, negative callback return value, or error code + */ +GIT_EXTERN(int) git_index_add_all( + git_index *index, + const git_strarray *pathspec, + unsigned int flags, + git_index_matched_path_cb callback, + void *payload); + +/** + * Remove all matching index entries. + * + * If you provide a callback function, it will be invoked on each matching + * item in the index immediately *before* it is removed. Return 0 to + * remove the item, > 0 to skip the item, and < 0 to abort the scan. + * + * @param index An existing index object + * @param pathspec array of path patterns + * @param callback notification callback for each removed path (also + * gets index of matching pathspec entry); can be NULL; + * return 0 to add, >0 to skip, <0 to abort scan. + * @param payload payload passed through to callback function + * @return 0 on success, negative callback return value, or error code + */ +GIT_EXTERN(int) git_index_remove_all( + git_index *index, + const git_strarray *pathspec, + git_index_matched_path_cb callback, + void *payload); + +/** + * Update all index entries to match the working directory + * + * This method will fail in bare index instances. + * + * This scans the existing index entries and synchronizes them with the + * working directory, deleting them if the corresponding working directory + * file no longer exists otherwise updating the information (including + * adding the latest version of file to the ODB if needed). + * + * If you provide a callback function, it will be invoked on each matching + * item in the index immediately *before* it is updated (either refreshed + * or removed depending on working directory state). Return 0 to proceed + * with updating the item, > 0 to skip the item, and < 0 to abort the scan. + * + * @param index An existing index object + * @param pathspec array of path patterns + * @param callback notification callback for each updated path (also + * gets index of matching pathspec entry); can be NULL; + * return 0 to add, >0 to skip, <0 to abort scan. + * @param payload payload passed through to callback function + * @return 0 on success, negative callback return value, or error code + */ +GIT_EXTERN(int) git_index_update_all( + git_index *index, + const git_strarray *pathspec, + git_index_matched_path_cb callback, + void *payload); + +/** + * Find the first position of any entries which point to given * path in the Git index. * * @param at_pos the address to which the position of the index entry is written (optional) * @param index an existing index object * @param path path to search - * @return 0 if found, < 0 otherwise (GIT_ENOTFOUND) + * @return 0 or an error code */ GIT_EXTERN(int) git_index_find(size_t *at_pos, git_index *index, const char *path); +/** + * Find the first position of any entries matching a prefix. To find the first position + * of a path inside a given folder, suffix the prefix with a '/'. + * + * @param at_pos the address to which the position of the index entry is written (optional) + * @param index an existing index object + * @param prefix the prefix to search for + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_find_prefix(size_t *at_pos, git_index *index, const char *prefix); + /**@}*/ /** @name Conflict Index Entry Functions @@ -419,7 +835,8 @@ GIT_EXTERN(int) git_index_find(size_t *at_pos, git_index *index, const char *pat /**@{*/ /** - * Add or update index entries to represent a conflict + * Add or update index entries to represent a conflict. Any staged + * entries that exist at the given paths will be removed. * * The entries are the entries from the tree included in the merge. Any * entry may be null to indicate that that file was not present in the @@ -433,7 +850,7 @@ GIT_EXTERN(int) git_index_find(size_t *at_pos, git_index *index, const char *pat * @return 0 or an error code */ GIT_EXTERN(int) git_index_conflict_add( - git_index *index, + git_index *index, const git_index_entry *ancestor_entry, const git_index_entry *our_entry, const git_index_entry *their_entry); @@ -441,130 +858,88 @@ GIT_EXTERN(int) git_index_conflict_add( /** * Get the index entries that represent a conflict of a single file. * - * The values of this entry can be modified (except the paths) - * and the changes will be written back to disk on the next - * write() call. + * The entries are not modifiable and should not be freed. Because the + * `git_index_entry` struct is a publicly defined struct, you should + * be able to make your own permanent copy of the data if necessary. * * @param ancestor_out Pointer to store the ancestor entry * @param our_out Pointer to store the our entry * @param their_out Pointer to store the their entry * @param index an existing index object * @param path path to search + * @return 0 or an error code */ -GIT_EXTERN(int) git_index_conflict_get(git_index_entry **ancestor_out, git_index_entry **our_out, git_index_entry **their_out, git_index *index, const char *path); +GIT_EXTERN(int) git_index_conflict_get( + const git_index_entry **ancestor_out, + const git_index_entry **our_out, + const git_index_entry **their_out, + git_index *index, + const char *path); /** * Removes the index entries that represent a conflict of a single file. * * @param index an existing index object - * @param path to search + * @param path path to remove conflicts for + * @return 0 or an error code */ GIT_EXTERN(int) git_index_conflict_remove(git_index *index, const char *path); /** - * Remove all conflicts in the index (entries with a stage greater than 0.) + * Remove all conflicts in the index (entries with a stage greater than 0). * * @param index an existing index object + * @return 0 or an error code */ -GIT_EXTERN(void) git_index_conflict_cleanup(git_index *index); +GIT_EXTERN(int) git_index_conflict_cleanup(git_index *index); /** * Determine if the index contains entries representing file conflicts. * + * @param index An existing index object. * @return 1 if at least one conflict is found, 0 otherwise. */ GIT_EXTERN(int) git_index_has_conflicts(const git_index *index); -/**@}*/ - -/** @name Resolve Undo (REUC) index entry manipulation. - * - * These functions work on the Resolve Undo index extension and contains - * data about the original files that led to a merge conflict. - */ -/**@{*/ - /** - * Get the count of resolve undo entries currently in the index. + * Create an iterator for the conflicts in the index. * - * @param index an existing index object - * @return integer of count of current resolve undo entries - */ -GIT_EXTERN(unsigned int) git_index_reuc_entrycount(git_index *index); - -/** - * Finds the resolve undo entry that points to the given path in the Git - * index. + * The index must not be modified while iterating; the results are undefined. * - * @param at_pos the address to which the position of the reuc entry is written (optional) - * @param index an existing index object - * @param path path to search - * @return 0 if found, < 0 otherwise (GIT_ENOTFOUND) - */ -GIT_EXTERN(int) git_index_reuc_find(size_t *at_pos, git_index *index, const char *path); - -/** - * Get a resolve undo entry from the index. - * - * The returned entry is read-only and should not be modified - * or freed by the caller. - * - * @param index an existing index object - * @param path path to search - * @return the resolve undo entry; NULL if not found - */ -GIT_EXTERN(const git_index_reuc_entry *) git_index_reuc_get_bypath(git_index *index, const char *path); - -/** - * Get a resolve undo entry from the index. - * - * The returned entry is read-only and should not be modified - * or freed by the caller. - * - * @param index an existing index object - * @param n the position of the entry - * @return a pointer to the resolve undo entry; NULL if out of bounds + * @param iterator_out The newly created conflict iterator + * @param index The index to scan + * @return 0 or an error code */ -GIT_EXTERN(const git_index_reuc_entry *) git_index_reuc_get_byindex(git_index *index, size_t n); +GIT_EXTERN(int) git_index_conflict_iterator_new( + git_index_conflict_iterator **iterator_out, + git_index *index); /** - * Adds a resolve undo entry for a file based on the given parameters. - * - * The resolve undo entry contains the OIDs of files that were involved - * in a merge conflict after the conflict has been resolved. This allows - * conflicts to be re-resolved later. + * Returns the current conflict (ancestor, ours and theirs entry) and + * advance the iterator internally to the next value. * - * If there exists a resolve undo entry for the given path in the index, - * it will be removed. - * - * This method will fail in bare index instances. - * - * @param index an existing index object - * @param path filename to add - * @param ancestor_mode mode of the ancestor file - * @param ancestor_id oid of the ancestor file - * @param our_mode mode of our file - * @param our_id oid of our file - * @param their_mode mode of their file - * @param their_id oid of their file - * @return 0 or an error code + * @param ancestor_out Pointer to store the ancestor side of the conflict + * @param our_out Pointer to store our side of the conflict + * @param their_out Pointer to store their side of the conflict + * @param iterator The conflict iterator. + * @return 0 (no error), GIT_ITEROVER (iteration is done) or an error code + * (negative value) */ -GIT_EXTERN(int) git_index_reuc_add(git_index *index, const char *path, - int ancestor_mode, git_oid *ancestor_id, - int our_mode, git_oid *our_id, - int their_mode, git_oid *their_id); +GIT_EXTERN(int) git_index_conflict_next( + const git_index_entry **ancestor_out, + const git_index_entry **our_out, + const git_index_entry **their_out, + git_index_conflict_iterator *iterator); /** - * Remove an resolve undo entry from the index + * Frees a `git_index_conflict_iterator`. * - * @param index an existing index object - * @param n position of the resolve undo entry to remove - * @return 0 or an error code + * @param iterator pointer to the iterator */ -GIT_EXTERN(int) git_index_reuc_remove(git_index *index, size_t n); - -/**@}*/ +GIT_EXTERN(void) git_index_conflict_iterator_free( + git_index_conflict_iterator *iterator); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/indexer.h b/include/git2/indexer.h index c428d43a85f..9aaedc3c43f 100644 --- a/include/git2/indexer.h +++ b/include/git2/indexer.h @@ -4,47 +4,153 @@ * This file is part of libgit2, distributed under the GNU GPL v2 with * a Linking Exception. For full terms see the included COPYING file. */ -#ifndef _INCLUDE_git_indexer_h__ -#define _INCLUDE_git_indexer_h__ +#ifndef INCLUDE_git_indexer_h__ +#define INCLUDE_git_indexer_h__ #include "common.h" +#include "types.h" #include "oid.h" +/** + * @file git2/indexer.h + * @brief Packfile indexing + * @ingroup Git + * @{ + * + * Indexing is the operation of taking a packfile - which is simply a + * collection of unordered commits - and producing an "index" so that + * one can lookup a commit in the packfile by object ID. + */ GIT_BEGIN_DECL +/** A git indexer object */ +typedef struct git_indexer git_indexer; + /** - * This is passed as the first argument to the callback to allow the - * user to see the progress. + * This structure is used to provide callers information about the + * progress of indexing a packfile, either directly or part of a + * fetch or clone that downloads a packfile. */ -typedef struct git_transfer_progress { +typedef struct git_indexer_progress { + /** number of objects in the packfile being indexed */ unsigned int total_objects; + + /** received objects that have been hashed */ unsigned int indexed_objects; + + /** received_objects: objects which have been downloaded */ unsigned int received_objects; + + /** + * locally-available objects that have been injected in order + * to fix a thin pack + */ + unsigned int local_objects; + + /** number of deltas in the packfile being indexed */ + unsigned int total_deltas; + + /** received deltas that have been indexed */ + unsigned int indexed_deltas; + + /** size of the packfile received up to now */ size_t received_bytes; -} git_transfer_progress; +} git_indexer_progress; +/** + * Type for progress callbacks during indexing. Return a value less + * than zero to cancel the indexing or download. + * + * @param stats Structure containing information about the state of the transfer + * @param payload Payload provided by caller + * @return 0 on success or an error code + */ +typedef int GIT_CALLBACK(git_indexer_progress_cb)(const git_indexer_progress *stats, void *payload); /** - * Type for progress callbacks during indexing + * Options for indexer configuration */ -typedef void (*git_transfer_progress_callback)(const git_transfer_progress *stats, void *payload); +typedef struct git_indexer_options { + unsigned int version; + +#ifdef GIT_EXPERIMENTAL_SHA256 + /** permissions to use creating packfile or 0 for defaults */ + unsigned int mode; + + /** the type of object ids in the packfile or 0 for SHA1 */ + git_oid_t oid_type; + + /** + * object database from which to read base objects when + * fixing thin packs. This can be NULL if there are no thin + * packs; if a thin pack is encountered, an error will be + * returned if there are bases missing. + */ + git_odb *odb; +#endif -typedef struct git_indexer git_indexer; -typedef struct git_indexer_stream git_indexer_stream; + /** progress_cb function to call with progress information */ + git_indexer_progress_cb progress_cb; + + /** progress_cb_payload payload for the progress callback */ + void *progress_cb_payload; + /** Do connectivity checks for the received pack */ + unsigned char verify; +} git_indexer_options; + +/** Current version for the `git_indexer_options` structure */ +#define GIT_INDEXER_OPTIONS_VERSION 1 + +/** Static constructor for `git_indexer_options` */ +#define GIT_INDEXER_OPTIONS_INIT { GIT_INDEXER_OPTIONS_VERSION } + +/** + * Initializes a `git_indexer_options` with default values. Equivalent to + * creating an instance with GIT_INDEXER_OPTIONS_INIT. + * + * @param opts the `git_indexer_options` struct to initialize. + * @param version Version of struct; pass `GIT_INDEXER_OPTIONS_VERSION` + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_indexer_options_init( + git_indexer_options *opts, + unsigned int version); + +#ifdef GIT_EXPERIMENTAL_SHA256 /** - * Create a new streaming indexer instance + * Create a new indexer instance + * + * @param out where to store the indexer instance + * @param path to the directory where the packfile should be stored + * @param opts the options to create the indexer with + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_indexer_new( + git_indexer **out, + const char *path, + git_indexer_options *opts); +#else +/** + * Create a new indexer instance * * @param out where to store the indexer instance * @param path to the directory where the packfile should be stored - * @param progress_cb function to call with progress information - * @param progress_payload payload for the progress callback + * @param mode permissions to use creating packfile or 0 for defaults + * @param odb object database from which to read base objects when + * fixing thin packs. Pass NULL if no thin pack is expected (an error + * will be returned if there are bases missing) + * @param opts Optional structure containing additional options. See + * `git_indexer_options` above. + * @return 0 or an error code. */ -GIT_EXTERN(int) git_indexer_stream_new( - git_indexer_stream **out, +GIT_EXTERN(int) git_indexer_new( + git_indexer **out, const char *path, - git_transfer_progress_callback progress_cb, - void *progress_cb_payload); + unsigned int mode, + git_odb *odb, + git_indexer_options *opts); +#endif /** * Add data to the indexer @@ -53,8 +159,9 @@ GIT_EXTERN(int) git_indexer_stream_new( * @param data the data to add * @param size the size of the data in bytes * @param stats stat storage + * @return 0 or an error code. */ -GIT_EXTERN(int) git_indexer_stream_add(git_indexer_stream *idx, const void *data, size_t size, git_transfer_progress *stats); +GIT_EXTERN(int) git_indexer_append(git_indexer *idx, const void *data, size_t size, git_indexer_progress *stats); /** * Finalize the pack and index @@ -62,65 +169,35 @@ GIT_EXTERN(int) git_indexer_stream_add(git_indexer_stream *idx, const void *data * Resolve any pending deltas and write out the index file * * @param idx the indexer + * @param stats Stat storage. + * @return 0 or an error code. */ -GIT_EXTERN(int) git_indexer_stream_finalize(git_indexer_stream *idx, git_transfer_progress *stats); +GIT_EXTERN(int) git_indexer_commit(git_indexer *idx, git_indexer_progress *stats); +#ifndef GIT_DEPRECATE_HARD /** * Get the packfile's hash * * A packfile's name is derived from the sorted hashing of all object * names. This is only correct after the index has been finalized. * + * @deprecated use git_indexer_name * @param idx the indexer instance + * @return the packfile's hash */ -GIT_EXTERN(const git_oid *) git_indexer_stream_hash(const git_indexer_stream *idx); - -/** - * Free the indexer and its resources - * - * @param idx the indexer to free - */ -GIT_EXTERN(void) git_indexer_stream_free(git_indexer_stream *idx); - -/** - * Create a new indexer instance - * - * @param out where to store the indexer instance - * @param packname the absolute filename of the packfile to index - */ -GIT_EXTERN(int) git_indexer_new(git_indexer **out, const char *packname); - -/** - * Iterate over the objects in the packfile and extract the information - * - * Indexing a packfile can be very expensive so this function is - * expected to be run in a worker thread and the stats used to provide - * feedback the user. - * - * @param idx the indexer instance - * @param stats storage for the running state - */ -GIT_EXTERN(int) git_indexer_run(git_indexer *idx, git_transfer_progress *stats); - -/** - * Write the index file to disk. - * - * The file will be stored as pack-$hash.idx in the same directory as - * the packfile. - * - * @param idx the indexer instance - */ -GIT_EXTERN(int) git_indexer_write(git_indexer *idx); +GIT_EXTERN(const git_oid *) git_indexer_hash(const git_indexer *idx); +#endif /** - * Get the packfile's hash + * Get the unique name for the resulting packfile. * - * A packfile's name is derived from the sorted hashing of all object - * names. This is only correct after the index has been written to disk. + * The packfile's name is derived from the packfile's content. + * This is only correct after the index has been finalized. * * @param idx the indexer instance + * @return a NUL terminated string for the packfile name */ -GIT_EXTERN(const git_oid *) git_indexer_hash(const git_indexer *idx); +GIT_EXTERN(const char *) git_indexer_name(const git_indexer *idx); /** * Free the indexer and its resources @@ -129,6 +206,7 @@ GIT_EXTERN(const git_oid *) git_indexer_hash(const git_indexer *idx); */ GIT_EXTERN(void) git_indexer_free(git_indexer *idx); +/** @} */ GIT_END_DECL #endif diff --git a/include/git2/inttypes.h b/include/git2/inttypes.h deleted file mode 100644 index 71608421934..00000000000 --- a/include/git2/inttypes.h +++ /dev/null @@ -1,309 +0,0 @@ -// ISO C9x compliant inttypes.h for Microsoft Visual Studio -// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 -// -// Copyright (c) 2006 Alexander Chemeris -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in the -// documentation and/or other materials provided with the distribution. -// -// 3. The name of the author may be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED -// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO -// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -/////////////////////////////////////////////////////////////////////////////// - -#ifndef _MSC_VER // [ -#error "Use this header only with Microsoft Visual C++ compilers!" -#endif // _MSC_VER ] - -#ifndef _MSC_INTTYPES_H_ // [ -#define _MSC_INTTYPES_H_ - -#if _MSC_VER > 1000 -#pragma once -#endif - -#if _MSC_VER >= 1600 -#include -#else -#include "stdint.h" -#endif - -// 7.8 Format conversion of integer types - -typedef struct { - intmax_t quot; - intmax_t rem; -} imaxdiv_t; - -// 7.8.1 Macros for format specifiers - -#if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS) // [ See footnote 185 at page 198 - -// The fprintf macros for signed integers are: -#define PRId8 "d" -#define PRIi8 "i" -#define PRIdLEAST8 "d" -#define PRIiLEAST8 "i" -#define PRIdFAST8 "d" -#define PRIiFAST8 "i" - -#define PRId16 "hd" -#define PRIi16 "hi" -#define PRIdLEAST16 "hd" -#define PRIiLEAST16 "hi" -#define PRIdFAST16 "hd" -#define PRIiFAST16 "hi" - -#define PRId32 "I32d" -#define PRIi32 "I32i" -#define PRIdLEAST32 "I32d" -#define PRIiLEAST32 "I32i" -#define PRIdFAST32 "I32d" -#define PRIiFAST32 "I32i" - -#define PRId64 "I64d" -#define PRIi64 "I64i" -#define PRIdLEAST64 "I64d" -#define PRIiLEAST64 "I64i" -#define PRIdFAST64 "I64d" -#define PRIiFAST64 "I64i" - -#define PRIdMAX "I64d" -#define PRIiMAX "I64i" - -#define PRIdPTR "Id" -#define PRIiPTR "Ii" - -// The fprintf macros for unsigned integers are: -#define PRIo8 "o" -#define PRIu8 "u" -#define PRIx8 "x" -#define PRIX8 "X" -#define PRIoLEAST8 "o" -#define PRIuLEAST8 "u" -#define PRIxLEAST8 "x" -#define PRIXLEAST8 "X" -#define PRIoFAST8 "o" -#define PRIuFAST8 "u" -#define PRIxFAST8 "x" -#define PRIXFAST8 "X" - -#define PRIo16 "ho" -#define PRIu16 "hu" -#define PRIx16 "hx" -#define PRIX16 "hX" -#define PRIoLEAST16 "ho" -#define PRIuLEAST16 "hu" -#define PRIxLEAST16 "hx" -#define PRIXLEAST16 "hX" -#define PRIoFAST16 "ho" -#define PRIuFAST16 "hu" -#define PRIxFAST16 "hx" -#define PRIXFAST16 "hX" - -#define PRIo32 "I32o" -#define PRIu32 "I32u" -#define PRIx32 "I32x" -#define PRIX32 "I32X" -#define PRIoLEAST32 "I32o" -#define PRIuLEAST32 "I32u" -#define PRIxLEAST32 "I32x" -#define PRIXLEAST32 "I32X" -#define PRIoFAST32 "I32o" -#define PRIuFAST32 "I32u" -#define PRIxFAST32 "I32x" -#define PRIXFAST32 "I32X" - -#define PRIo64 "I64o" -#define PRIu64 "I64u" -#define PRIx64 "I64x" -#define PRIX64 "I64X" -#define PRIoLEAST64 "I64o" -#define PRIuLEAST64 "I64u" -#define PRIxLEAST64 "I64x" -#define PRIXLEAST64 "I64X" -#define PRIoFAST64 "I64o" -#define PRIuFAST64 "I64u" -#define PRIxFAST64 "I64x" -#define PRIXFAST64 "I64X" - -#define PRIoMAX "I64o" -#define PRIuMAX "I64u" -#define PRIxMAX "I64x" -#define PRIXMAX "I64X" - -#define PRIoPTR "Io" -#define PRIuPTR "Iu" -#define PRIxPTR "Ix" -#define PRIXPTR "IX" - -// The fscanf macros for signed integers are: -#define SCNd8 "d" -#define SCNi8 "i" -#define SCNdLEAST8 "d" -#define SCNiLEAST8 "i" -#define SCNdFAST8 "d" -#define SCNiFAST8 "i" - -#define SCNd16 "hd" -#define SCNi16 "hi" -#define SCNdLEAST16 "hd" -#define SCNiLEAST16 "hi" -#define SCNdFAST16 "hd" -#define SCNiFAST16 "hi" - -#define SCNd32 "ld" -#define SCNi32 "li" -#define SCNdLEAST32 "ld" -#define SCNiLEAST32 "li" -#define SCNdFAST32 "ld" -#define SCNiFAST32 "li" - -#define SCNd64 "I64d" -#define SCNi64 "I64i" -#define SCNdLEAST64 "I64d" -#define SCNiLEAST64 "I64i" -#define SCNdFAST64 "I64d" -#define SCNiFAST64 "I64i" - -#define SCNdMAX "I64d" -#define SCNiMAX "I64i" - -#ifdef _WIN64 // [ -# define SCNdPTR "I64d" -# define SCNiPTR "I64i" -#else // _WIN64 ][ -# define SCNdPTR "ld" -# define SCNiPTR "li" -#endif // _WIN64 ] - -// The fscanf macros for unsigned integers are: -#define SCNo8 "o" -#define SCNu8 "u" -#define SCNx8 "x" -#define SCNX8 "X" -#define SCNoLEAST8 "o" -#define SCNuLEAST8 "u" -#define SCNxLEAST8 "x" -#define SCNXLEAST8 "X" -#define SCNoFAST8 "o" -#define SCNuFAST8 "u" -#define SCNxFAST8 "x" -#define SCNXFAST8 "X" - -#define SCNo16 "ho" -#define SCNu16 "hu" -#define SCNx16 "hx" -#define SCNX16 "hX" -#define SCNoLEAST16 "ho" -#define SCNuLEAST16 "hu" -#define SCNxLEAST16 "hx" -#define SCNXLEAST16 "hX" -#define SCNoFAST16 "ho" -#define SCNuFAST16 "hu" -#define SCNxFAST16 "hx" -#define SCNXFAST16 "hX" - -#define SCNo32 "lo" -#define SCNu32 "lu" -#define SCNx32 "lx" -#define SCNX32 "lX" -#define SCNoLEAST32 "lo" -#define SCNuLEAST32 "lu" -#define SCNxLEAST32 "lx" -#define SCNXLEAST32 "lX" -#define SCNoFAST32 "lo" -#define SCNuFAST32 "lu" -#define SCNxFAST32 "lx" -#define SCNXFAST32 "lX" - -#define SCNo64 "I64o" -#define SCNu64 "I64u" -#define SCNx64 "I64x" -#define SCNX64 "I64X" -#define SCNoLEAST64 "I64o" -#define SCNuLEAST64 "I64u" -#define SCNxLEAST64 "I64x" -#define SCNXLEAST64 "I64X" -#define SCNoFAST64 "I64o" -#define SCNuFAST64 "I64u" -#define SCNxFAST64 "I64x" -#define SCNXFAST64 "I64X" - -#define SCNoMAX "I64o" -#define SCNuMAX "I64u" -#define SCNxMAX "I64x" -#define SCNXMAX "I64X" - -#ifdef _WIN64 // [ -# define SCNoPTR "I64o" -# define SCNuPTR "I64u" -# define SCNxPTR "I64x" -# define SCNXPTR "I64X" -#else // _WIN64 ][ -# define SCNoPTR "lo" -# define SCNuPTR "lu" -# define SCNxPTR "lx" -# define SCNXPTR "lX" -#endif // _WIN64 ] - -#endif // __STDC_FORMAT_MACROS ] - -// 7.8.2 Functions for greatest-width integer types - -// 7.8.2.1 The imaxabs function -#define imaxabs _abs64 - -// 7.8.2.2 The imaxdiv function - -// This is modified version of div() function from Microsoft's div.c found -// in %MSVC.NET%\crt\src\div.c -#ifdef STATIC_IMAXDIV // [ -static -#else // STATIC_IMAXDIV ][ -_inline -#endif // STATIC_IMAXDIV ] -imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom) -{ - imaxdiv_t result; - - result.quot = numer / denom; - result.rem = numer % denom; - - if (numer < 0 && result.rem > 0) { - // did division wrong; must fix up - ++result.quot; - result.rem -= denom; - } - - return result; -} - -// 7.8.2.3 The strtoimax and strtoumax functions -#define strtoimax _strtoi64 -#define strtoumax _strtoui64 - -// 7.8.2.4 The wcstoimax and wcstoumax functions -#define wcstoimax _wcstoi64 -#define wcstoumax _wcstoui64 - - -#endif // _MSC_INTTYPES_H_ ] diff --git a/include/git2/mailmap.h b/include/git2/mailmap.h new file mode 100644 index 00000000000..fd6ae7170c2 --- /dev/null +++ b/include/git2/mailmap.h @@ -0,0 +1,121 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_mailmap_h__ +#define INCLUDE_git_mailmap_h__ + +#include "common.h" +#include "types.h" +#include "buffer.h" + +/** + * @file git2/mailmap.h + * @brief Mailmaps provide alternate email addresses for users + * @defgroup git_mailmap Git mailmap routines + * @ingroup Git + * @{ + * + * A mailmap can be used to specify alternate email addresses for + * repository committers or authors. This allows systems to map + * commits made using different email addresses to the same logical + * person. + */ +GIT_BEGIN_DECL + +/** + * Allocate a new mailmap object. + * + * This object is empty, so you'll have to add a mailmap file before you can do + * anything with it. The mailmap must be freed with 'git_mailmap_free'. + * + * @param out pointer to store the new mailmap + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_mailmap_new(git_mailmap **out); + +/** + * Free the mailmap and its associated memory. + * + * @param mm the mailmap to free + */ +GIT_EXTERN(void) git_mailmap_free(git_mailmap *mm); + +/** + * Add a single entry to the given mailmap object. If the entry already exists, + * it will be replaced with the new entry. + * + * @param mm mailmap to add the entry to + * @param real_name the real name to use, or NULL + * @param real_email the real email to use, or NULL + * @param replace_name the name to replace, or NULL + * @param replace_email the email to replace + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_mailmap_add_entry( + git_mailmap *mm, const char *real_name, const char *real_email, + const char *replace_name, const char *replace_email); + +/** + * Create a new mailmap instance containing a single mailmap file + * + * @param out pointer to store the new mailmap + * @param buf buffer to parse the mailmap from + * @param len the length of the input buffer + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_mailmap_from_buffer( + git_mailmap **out, const char *buf, size_t len); + +/** + * Create a new mailmap instance from a repository, loading mailmap files based + * on the repository's configuration. + * + * Mailmaps are loaded in the following order: + * 1. '.mailmap' in the root of the repository's working directory, if present. + * 2. The blob object identified by the 'mailmap.blob' config entry, if set. + * [NOTE: 'mailmap.blob' defaults to 'HEAD:.mailmap' in bare repositories] + * 3. The path in the 'mailmap.file' config entry, if set. + * + * @param out pointer to store the new mailmap + * @param repo repository to load mailmap information from + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_mailmap_from_repository( + git_mailmap **out, git_repository *repo); + +/** + * Resolve a name and email to the corresponding real name and email. + * + * The lifetime of the strings are tied to `mm`, `name`, and `email` parameters. + * + * @param real_name pointer to store the real name + * @param real_email pointer to store the real email + * @param mm the mailmap to perform a lookup with (may be NULL) + * @param name the name to look up + * @param email the email to look up + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_mailmap_resolve( + const char **real_name, const char **real_email, + const git_mailmap *mm, const char *name, const char *email); + +/** + * Resolve a signature to use real names and emails with a mailmap. + * + * Call `git_signature_free()` to free the data. + * + * @param out new signature + * @param mm mailmap to resolve with + * @param sig signature to resolve + * @return 0 or an error code + */ +GIT_EXTERN(int) git_mailmap_resolve_signature( + git_signature **out, const git_mailmap *mm, const git_signature *sig); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/merge.h b/include/git2/merge.h index f4c5d988117..be3b065b8a2 100644 --- a/include/git2/merge.h +++ b/include/git2/merge.h @@ -10,16 +10,432 @@ #include "common.h" #include "types.h" #include "oid.h" +#include "oidarray.h" +#include "checkout.h" +#include "index.h" +#include "annotated_commit.h" /** * @file git2/merge.h - * @brief Git merge-base routines - * @defgroup git_revwalk Git merge-base routines + * @brief Merge re-joins diverging branches of history + * @defgroup git_merge Git merge routines * @ingroup Git + * + * Merge will take two commits and attempt to produce a commit that + * includes the changes that were made in both branches. * @{ */ GIT_BEGIN_DECL +/** + * The file inputs to `git_merge_file`. Callers should populate the + * `git_merge_file_input` structure with descriptions of the files in + * each side of the conflict for use in producing the merge file. + */ +typedef struct { + unsigned int version; + + /** Pointer to the contents of the file. */ + const char *ptr; + + /** Size of the contents pointed to in `ptr`. */ + size_t size; + + /** File name of the conflicted file, or `NULL` to not merge the path. */ + const char *path; + + /** File mode of the conflicted file, or `0` to not merge the mode. */ + unsigned int mode; +} git_merge_file_input; + +/** Current version for the `git_merge_file_input_options` structure */ +#define GIT_MERGE_FILE_INPUT_VERSION 1 + +/** Static constructor for `git_merge_file_input_options` */ +#define GIT_MERGE_FILE_INPUT_INIT {GIT_MERGE_FILE_INPUT_VERSION} + +/** + * Initializes a `git_merge_file_input` with default values. Equivalent to + * creating an instance with GIT_MERGE_FILE_INPUT_INIT. + * + * @param opts the `git_merge_file_input` instance to initialize. + * @param version the version of the struct; you should pass + * `GIT_MERGE_FILE_INPUT_VERSION` here. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_merge_file_input_init( + git_merge_file_input *opts, + unsigned int version); + +/** + * Flags for `git_merge` options. A combination of these flags can be + * passed in via the `flags` value in the `git_merge_options`. + */ +typedef enum { + /** + * Detect renames that occur between the common ancestor and the "ours" + * side or the common ancestor and the "theirs" side. This will enable + * the ability to merge between a modified and renamed file. + */ + GIT_MERGE_FIND_RENAMES = (1 << 0), + + /** + * If a conflict occurs, exit immediately instead of attempting to + * continue resolving conflicts. The merge operation will fail with + * GIT_EMERGECONFLICT and no index will be returned. + */ + GIT_MERGE_FAIL_ON_CONFLICT = (1 << 1), + + /** + * Do not write the REUC extension on the generated index + */ + GIT_MERGE_SKIP_REUC = (1 << 2), + + /** + * If the commits being merged have multiple merge bases, do not build + * a recursive merge base (by merging the multiple merge bases), + * instead simply use the first base. This flag provides a similar + * merge base to `git-merge-resolve`. + */ + GIT_MERGE_NO_RECURSIVE = (1 << 3), + + /** + * Treat this merge as if it is to produce the virtual base + * of a recursive merge. This will ensure that there are + * no conflicts, any conflicting regions will keep conflict + * markers in the merge result. + */ + GIT_MERGE_VIRTUAL_BASE = (1 << 4) +} git_merge_flag_t; + +/** + * Merge file favor options for `git_merge_options` instruct the file-level + * merging functionality how to deal with conflicting regions of the files. + */ +typedef enum { + /** + * When a region of a file is changed in both branches, a conflict + * will be recorded in the index so that `git_checkout` can produce + * a merge file with conflict markers in the working directory. + * This is the default. + */ + GIT_MERGE_FILE_FAVOR_NORMAL = 0, + + /** + * When a region of a file is changed in both branches, the file + * created in the index will contain the "ours" side of any conflicting + * region. The index will not record a conflict. + */ + GIT_MERGE_FILE_FAVOR_OURS = 1, + + /** + * When a region of a file is changed in both branches, the file + * created in the index will contain the "theirs" side of any conflicting + * region. The index will not record a conflict. + */ + GIT_MERGE_FILE_FAVOR_THEIRS = 2, + + /** + * When a region of a file is changed in both branches, the file + * created in the index will contain each unique line from each side, + * which has the result of combining both files. The index will not + * record a conflict. + */ + GIT_MERGE_FILE_FAVOR_UNION = 3 +} git_merge_file_favor_t; + +/** + * File merging flags + */ +typedef enum { + /** Defaults */ + GIT_MERGE_FILE_DEFAULT = 0, + + /** Create standard conflicted merge files */ + GIT_MERGE_FILE_STYLE_MERGE = (1 << 0), + + /** Create diff3-style files */ + GIT_MERGE_FILE_STYLE_DIFF3 = (1 << 1), + + /** Condense non-alphanumeric regions for simplified diff file */ + GIT_MERGE_FILE_SIMPLIFY_ALNUM = (1 << 2), + + /** Ignore all whitespace */ + GIT_MERGE_FILE_IGNORE_WHITESPACE = (1 << 3), + + /** Ignore changes in amount of whitespace */ + GIT_MERGE_FILE_IGNORE_WHITESPACE_CHANGE = (1 << 4), + + /** Ignore whitespace at end of line */ + GIT_MERGE_FILE_IGNORE_WHITESPACE_EOL = (1 << 5), + + /** Use the "patience diff" algorithm */ + GIT_MERGE_FILE_DIFF_PATIENCE = (1 << 6), + + /** Take extra time to find minimal diff */ + GIT_MERGE_FILE_DIFF_MINIMAL = (1 << 7), + + /** Create zdiff3 ("zealous diff3")-style files */ + GIT_MERGE_FILE_STYLE_ZDIFF3 = (1 << 8), + + /** + * Do not produce file conflicts when common regions have + * changed; keep the conflict markers in the file and accept + * that as the merge result. + */ + GIT_MERGE_FILE_ACCEPT_CONFLICTS = (1 << 9) +} git_merge_file_flag_t; + +/** Default size for conflict markers */ +#define GIT_MERGE_CONFLICT_MARKER_SIZE 7 + +/** + * Options for merging a file + */ +typedef struct { + unsigned int version; + + /** + * Label for the ancestor file side of the conflict which will be prepended + * to labels in diff3-format merge files. + */ + const char *ancestor_label; + + /** + * Label for our file side of the conflict which will be prepended + * to labels in merge files. + */ + const char *our_label; + + /** + * Label for their file side of the conflict which will be prepended + * to labels in merge files. + */ + const char *their_label; + + /** The file to favor in region conflicts. */ + git_merge_file_favor_t favor; + + /** see `git_merge_file_flag_t` above */ + uint32_t flags; + + /** The size of conflict markers (eg, "<<<<<<<"). Default is + * GIT_MERGE_CONFLICT_MARKER_SIZE. */ + unsigned short marker_size; +} git_merge_file_options; + +/** Current version for the `git_merge_file_options` structure */ +#define GIT_MERGE_FILE_OPTIONS_VERSION 1 + +/** Static constructor for `git_merge_file_options` */ +#define GIT_MERGE_FILE_OPTIONS_INIT {GIT_MERGE_FILE_OPTIONS_VERSION} + +/** + * Initialize git_merge_file_options structure + * + * Initializes a `git_merge_file_options` with default values. Equivalent to + * creating an instance with `GIT_MERGE_FILE_OPTIONS_INIT`. + * + * @param opts The `git_merge_file_options` struct to initialize. + * @param version The struct version; pass `GIT_MERGE_FILE_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_merge_file_options_init(git_merge_file_options *opts, unsigned int version); + +/** + * Information about file-level merging + */ +typedef struct { + /** + * True if the output was automerged, false if the output contains + * conflict markers. + */ + unsigned int automergeable; + + /** + * The path that the resultant merge file should use, or NULL if a + * filename conflict would occur. + */ + const char *path; + + /** The mode that the resultant merge file should use. */ + unsigned int mode; + + /** The contents of the merge. */ + const char *ptr; + + /** The length of the merge contents. */ + size_t len; +} git_merge_file_result; + +/** + * Merging options + */ +typedef struct { + unsigned int version; + + /** See `git_merge_flag_t` above */ + uint32_t flags; + + /** + * Similarity to consider a file renamed (default 50). If + * `GIT_MERGE_FIND_RENAMES` is enabled, added files will be compared + * with deleted files to determine their similarity. Files that are + * more similar than the rename threshold (percentage-wise) will be + * treated as a rename. + */ + unsigned int rename_threshold; + + /** + * Maximum similarity sources to examine for renames (default 200). + * If the number of rename candidates (add / delete pairs) is greater + * than this value, inexact rename detection is aborted. + * + * This setting overrides the `merge.renameLimit` configuration value. + */ + unsigned int target_limit; + + /** Pluggable similarity metric; pass NULL to use internal metric */ + git_diff_similarity_metric *metric; + + /** + * Maximum number of times to merge common ancestors to build a + * virtual merge base when faced with criss-cross merges. When this + * limit is reached, the next ancestor will simply be used instead of + * attempting to merge it. The default is unlimited. + */ + unsigned int recursion_limit; + + /** + * Default merge driver to be used when both sides of a merge have + * changed. The default is the `text` driver. + */ + const char *default_driver; + + /** + * Flags for handling conflicting content, to be used with the standard + * (`text`) merge driver. + */ + git_merge_file_favor_t file_favor; + + /** see `git_merge_file_flag_t` above */ + uint32_t file_flags; +} git_merge_options; + +/** Current version for the `git_merge_options` structure */ +#define GIT_MERGE_OPTIONS_VERSION 1 + +/** Static constructor for `git_merge_options` */ +#define GIT_MERGE_OPTIONS_INIT { \ + GIT_MERGE_OPTIONS_VERSION, GIT_MERGE_FIND_RENAMES } + +/** + * Initialize git_merge_options structure + * + * Initializes a `git_merge_options` with default values. Equivalent to + * creating an instance with `GIT_MERGE_OPTIONS_INIT`. + * + * @param opts The `git_merge_options` struct to initialize. + * @param version The struct version; pass `GIT_MERGE_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_merge_options_init(git_merge_options *opts, unsigned int version); + +/** + * The results of `git_merge_analysis` indicate the merge opportunities. + */ +typedef enum { + /** No merge is possible. (Unused.) */ + GIT_MERGE_ANALYSIS_NONE = 0, + + /** + * A "normal" merge; both HEAD and the given merge input have diverged + * from their common ancestor. The divergent commits must be merged. + */ + GIT_MERGE_ANALYSIS_NORMAL = (1 << 0), + + /** + * All given merge inputs are reachable from HEAD, meaning the + * repository is up-to-date and no merge needs to be performed. + */ + GIT_MERGE_ANALYSIS_UP_TO_DATE = (1 << 1), + + /** + * The given merge input is a fast-forward from HEAD and no merge + * needs to be performed. Instead, the client can check out the + * given merge input. + */ + GIT_MERGE_ANALYSIS_FASTFORWARD = (1 << 2), + + /** + * The HEAD of the current repository is "unborn" and does not point to + * a valid commit. No merge can be performed, but the caller may wish + * to simply set HEAD to the target commit(s). + */ + GIT_MERGE_ANALYSIS_UNBORN = (1 << 3) +} git_merge_analysis_t; + +/** + * The user's stated preference for merges. + */ +typedef enum { + /** + * No configuration was found that suggests a preferred behavior for + * merge. + */ + GIT_MERGE_PREFERENCE_NONE = 0, + + /** + * There is a `merge.ff=false` configuration setting, suggesting that + * the user does not want to allow a fast-forward merge. + */ + GIT_MERGE_PREFERENCE_NO_FASTFORWARD = (1 << 0), + + /** + * There is a `merge.ff=only` configuration setting, suggesting that + * the user only wants fast-forward merges. + */ + GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY = (1 << 1) +} git_merge_preference_t; + +/** + * Analyzes the given branch(es) and determines the opportunities for + * merging them into the HEAD of the repository. + * + * @param analysis_out analysis enumeration that the result is written into + * @param preference_out One of the `git_merge_preference_t` flag. + * @param repo the repository to merge + * @param their_heads the heads to merge into + * @param their_heads_len the number of heads to merge + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_merge_analysis( + git_merge_analysis_t *analysis_out, + git_merge_preference_t *preference_out, + git_repository *repo, + const git_annotated_commit **their_heads, + size_t their_heads_len); + +/** + * Analyzes the given branch(es) and determines the opportunities for + * merging them into a reference. + * + * @param analysis_out analysis enumeration that the result is written into + * @param preference_out One of the `git_merge_preference_t` flag. + * @param repo the repository to merge + * @param our_ref the reference to perform the analysis from + * @param their_heads the heads to merge into + * @param their_heads_len the number of heads to merge + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_merge_analysis_for_ref( + git_merge_analysis_t *analysis_out, + git_merge_preference_t *preference_out, + git_repository *repo, + git_reference *our_ref, + const git_annotated_commit **their_heads, + size_t their_heads_len); + /** * Find a merge base between two commits * @@ -27,7 +443,7 @@ GIT_BEGIN_DECL * @param repo the repository where the commits exist * @param one one of the commits * @param two the other commit - * @return Zero on success; GIT_ENOTFOUND or -1 on failure. + * @return 0 on success, GIT_ENOTFOUND if not found or error code */ GIT_EXTERN(int) git_merge_base( git_oid *out, @@ -35,21 +451,221 @@ GIT_EXTERN(int) git_merge_base( const git_oid *one, const git_oid *two); +/** + * Find merge bases between two commits + * + * @param out array in which to store the resulting ids + * @param repo the repository where the commits exist + * @param one one of the commits + * @param two the other commit + * @return 0 on success, GIT_ENOTFOUND if not found or error code + */ +GIT_EXTERN(int) git_merge_bases( + git_oidarray *out, + git_repository *repo, + const git_oid *one, + const git_oid *two); + /** * Find a merge base given a list of commits * * @param out the OID of a merge base considering all the commits * @param repo the repository where the commits exist - * @param input_array oids of the commits * @param length The number of commits in the provided `input_array` + * @param input_array oids of the commits * @return Zero on success; GIT_ENOTFOUND or -1 on failure. */ GIT_EXTERN(int) git_merge_base_many( git_oid *out, git_repository *repo, - const git_oid input_array[], - size_t length); + size_t length, + const git_oid input_array[]); + +/** + * Find all merge bases given a list of commits + * + * This behaves similar to [`git merge-base`](https://git-scm.com/docs/git-merge-base#_discussion). + * + * Given three commits `a`, `b`, and `c`, `merge_base_many` + * will compute a hypothetical commit `m`, which is a merge between `b` + * and `c`. + + * For example, with the following topology: + * ```text + * o---o---o---o---C + * / + * / o---o---o---B + * / / + * ---2---1---o---o---o---A + * ``` + * + * the result of `merge_base_many` given `a`, `b`, and `c` is 1. This is + * because the equivalent topology with the imaginary merge commit `m` + * between `b` and `c` is: + * ```text + * o---o---o---o---o + * / \ + * / o---o---o---o---M + * / / + * ---2---1---o---o---o---A + * ``` + * + * and the result of `merge_base_many` given `a` and `m` is 1. + * + * If you're looking to recieve the common ancestor between all the + * given commits, use `merge_base_octopus`. + * + * @param out array in which to store the resulting ids + * @param repo the repository where the commits exist + * @param length The number of commits in the provided `input_array` + * @param input_array oids of the commits + * @return Zero on success; GIT_ENOTFOUND or -1 on failure. + */ +GIT_EXTERN(int) git_merge_bases_many( + git_oidarray *out, + git_repository *repo, + size_t length, + const git_oid input_array[]); + +/** + * Find a merge base in preparation for an octopus merge + * + * @param out the OID of a merge base considering all the commits + * @param repo the repository where the commits exist + * @param length The number of commits in the provided `input_array` + * @param input_array oids of the commits + * @return Zero on success; GIT_ENOTFOUND or -1 on failure. + */ +GIT_EXTERN(int) git_merge_base_octopus( + git_oid *out, + git_repository *repo, + size_t length, + const git_oid input_array[]); + +/** + * Merge two files as they exist in the in-memory data structures, using + * the given common ancestor as the baseline, producing a + * `git_merge_file_result` that reflects the merge result. The + * `git_merge_file_result` must be freed with `git_merge_file_result_free`. + * + * Note that this function does not reference a repository and any + * configuration must be passed as `git_merge_file_options`. + * + * @param out The git_merge_file_result to be filled in + * @param ancestor The contents of the ancestor file + * @param ours The contents of the file in "our" side + * @param theirs The contents of the file in "their" side + * @param opts The merge file options or `NULL` for defaults + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_merge_file( + git_merge_file_result *out, + const git_merge_file_input *ancestor, + const git_merge_file_input *ours, + const git_merge_file_input *theirs, + const git_merge_file_options *opts); + +/** + * Merge two files as they exist in the index, using the given common + * ancestor as the baseline, producing a `git_merge_file_result` that + * reflects the merge result. The `git_merge_file_result` must be freed with + * `git_merge_file_result_free`. + * + * @param out The git_merge_file_result to be filled in + * @param repo The repository + * @param ancestor The index entry for the ancestor file (stage level 1) + * @param ours The index entry for our file (stage level 2) + * @param theirs The index entry for their file (stage level 3) + * @param opts The merge file options or NULL + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_merge_file_from_index( + git_merge_file_result *out, + git_repository *repo, + const git_index_entry *ancestor, + const git_index_entry *ours, + const git_index_entry *theirs, + const git_merge_file_options *opts); + +/** + * Frees a `git_merge_file_result`. + * + * @param result The result to free or `NULL` + */ +GIT_EXTERN(void) git_merge_file_result_free(git_merge_file_result *result); + +/** + * Merge two trees, producing a `git_index` that reflects the result of + * the merge. The index may be written as-is to the working directory + * or checked out. If the index is to be converted to a tree, the caller + * should resolve any conflicts that arose as part of the merge. + * + * The returned index must be freed explicitly with `git_index_free`. + * + * @param out pointer to store the index result in + * @param repo repository that contains the given trees + * @param ancestor_tree the common ancestor between the trees (or null if none) + * @param our_tree the tree that reflects the destination tree + * @param their_tree the tree to merge in to `our_tree` + * @param opts the merge tree options (or null for defaults) + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_merge_trees( + git_index **out, + git_repository *repo, + const git_tree *ancestor_tree, + const git_tree *our_tree, + const git_tree *their_tree, + const git_merge_options *opts); + +/** + * Merge two commits, producing a `git_index` that reflects the result of + * the merge. The index may be written as-is to the working directory + * or checked out. If the index is to be converted to a tree, the caller + * should resolve any conflicts that arose as part of the merge. + * + * The returned index must be freed explicitly with `git_index_free`. + * + * @param out pointer to store the index result in + * @param repo repository that contains the given trees + * @param our_commit the commit that reflects the destination tree + * @param their_commit the commit to merge in to `our_commit` + * @param opts the merge tree options (or null for defaults) + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_merge_commits( + git_index **out, + git_repository *repo, + const git_commit *our_commit, + const git_commit *their_commit, + const git_merge_options *opts); + +/** + * Merges the given commit(s) into HEAD, writing the results into the working + * directory. Any changes are staged for commit and any conflicts are written + * to the index. Callers should inspect the repository's index after this + * completes, resolve any conflicts and prepare a commit. + * + * For compatibility with git, the repository is put into a merging + * state. Once the commit is done (or if the user wishes to abort), + * you should clear this state by calling + * `git_repository_state_cleanup()`. + * + * @param repo the repository to merge + * @param their_heads the heads to merge into + * @param their_heads_len the number of heads to merge + * @param merge_opts merge options + * @param checkout_opts checkout options + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_merge( + git_repository *repo, + const git_annotated_commit **their_heads, + size_t their_heads_len, + const git_merge_options *merge_opts, + const git_checkout_options *checkout_opts); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/message.h b/include/git2/message.h index 395c886903c..874d027f23f 100644 --- a/include/git2/message.h +++ b/include/git2/message.h @@ -8,42 +8,79 @@ #define INCLUDE_git_message_h__ #include "common.h" +#include "buffer.h" /** * @file git2/message.h - * @brief Git message management routines + * @brief Commit messages * @ingroup Git * @{ */ GIT_BEGIN_DECL /** - * Clean up message from excess whitespace and make sure that the last line - * ends with a '\n'. + * Clean up excess whitespace and make sure there is a trailing newline in the message. * - * Optionally, can remove lines starting with a "#". + * Optionally, it can remove lines which start with the comment character. * - * @param out The user-allocated buffer which will be filled with the - * cleaned up message. Pass NULL if you just want to get the needed - * size of the prettified message as the output value. - * - * @param out_size Size of the `out` buffer in bytes. + * @param out The user-allocated git_buf which will be filled with the + * cleaned up message. * * @param message The message to be prettified. * - * @param strip_comments Non-zero to remove lines starting with "#", 0 to - * leave them in. + * @param strip_comments Non-zero to remove comment lines, 0 to leave them in. + * + * @param comment_char Comment character. Lines starting with this character + * are considered to be comments and removed if `strip_comments` is non-zero. + * + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_message_prettify(git_buf *out, const char *message, int strip_comments, char comment_char); + +/** + * Represents a single git message trailer. + */ +typedef struct { + const char *key; + const char *value; +} git_message_trailer; + +/** + * Represents an array of git message trailers. + * + * Struct members under the private comment are private, subject to change + * and should not be used by callers. + */ +typedef struct { + git_message_trailer *trailers; + size_t count; + + /* private */ + char *_trailer_block; +} git_message_trailer_array; + +/** + * Parse trailers out of a message, filling the array pointed to by +arr+. + * + * Trailers are key/value pairs in the last paragraph of a message, not + * including any patches or conflicts that may be present. + * + * @param arr A pre-allocated git_message_trailer_array struct to be filled in + * with any trailers found during parsing. + * @param message The message to be parsed + * @return 0 on success, or non-zero on error. + */ +GIT_EXTERN(int) git_message_trailers(git_message_trailer_array *arr, const char *message); + +/** + * Clean's up any allocated memory in the git_message_trailer_array filled by + * a call to git_message_trailers. * - * @return -1 on error, else number of characters in prettified message - * including the trailing NUL byte + * @param arr The trailer to free. */ -GIT_EXTERN(int) git_message_prettify( - char *out, - size_t out_size, - const char *message, - int strip_comments); +GIT_EXTERN(void) git_message_trailer_array_free(git_message_trailer_array *arr); /** @} */ GIT_END_DECL -#endif /* INCLUDE_git_message_h__ */ +#endif diff --git a/include/git2/net.h b/include/git2/net.h index 6e3525f5d0c..93bdac4995f 100644 --- a/include/git2/net.h +++ b/include/git2/net.h @@ -13,41 +13,44 @@ /** * @file git2/net.h - * @brief Git networking declarations + * @brief Low-level networking functionality * @ingroup Git * @{ */ GIT_BEGIN_DECL +/** Default git protocol port number */ #define GIT_DEFAULT_PORT "9418" -/* +/** + * Direction of the connection. + * * We need this because we need to know whether we should call * git-upload-pack or git-receive-pack on the remote end when get_refs * gets called. */ - typedef enum { GIT_DIRECTION_FETCH = 0, GIT_DIRECTION_PUSH = 1 } git_direction; - /** - * Remote head description, given out on `ls` calls. + * Description of a reference advertised by a remote server, given out + * on `ls` calls. */ struct git_remote_head { - int local:1; /* available locally */ + int local; /* available locally */ git_oid oid; git_oid loid; char *name; + /** + * If the server send a symref mapping for this ref, this will + * point to the target. + */ + char *symref_target; }; -/** - * Callback for listing the remote heads - */ -typedef int (*git_headlist_cb)(git_remote_head *rhead, void *payload); - /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/notes.h b/include/git2/notes.h index c51d3732ac0..3784d5f5222 100644 --- a/include/git2/notes.h +++ b/include/git2/notes.h @@ -11,7 +11,7 @@ /** * @file git2/notes.h - * @brief Git notes management routines + * @brief Notes are metadata attached to an object * @defgroup git_note Git notes management routines * @ingroup Git * @{ @@ -21,13 +21,75 @@ GIT_BEGIN_DECL /** * Callback for git_note_foreach. * - * Receives: - * - blob_id: Oid of the blob containing the message - * - annotated_object_id: Oid of the git object being annotated - * - payload: Payload data passed to `git_note_foreach` + * @param blob_id object id of the blob containing the message + * @param annotated_object_id the id of the object being annotated + * @param payload user-specified data to the foreach function + * @return 0 on success, or a negative number on failure */ -typedef int (*git_note_foreach_cb)( - const git_oid *blob_id, const git_oid *annotated_object_id, void *payload); +typedef int GIT_CALLBACK(git_note_foreach_cb)( + const git_oid *blob_id, + const git_oid *annotated_object_id, + void *payload); + +/** + * note iterator + */ +typedef struct git_iterator git_note_iterator; + +/** + * Creates a new iterator for notes + * + * The iterator must be freed manually by the user. + * + * @param out pointer to the iterator + * @param repo repository where to look up the note + * @param notes_ref canonical name of the reference to use (optional); defaults to + * "refs/notes/commits" + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_note_iterator_new( + git_note_iterator **out, + git_repository *repo, + const char *notes_ref); + +/** + * Creates a new iterator for notes from a commit + * + * The iterator must be freed manually by the user. + * + * @param out pointer to the iterator + * @param notes_commit a pointer to the notes commit object + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_note_commit_iterator_new( + git_note_iterator **out, + git_commit *notes_commit); + +/** + * Frees an git_note_iterator + * + * @param it pointer to the iterator + */ +GIT_EXTERN(void) git_note_iterator_free(git_note_iterator *it); + +/** + * Return the current item (note_id and annotated_id) and advance the iterator + * internally to the next value + * + * @param note_id id of blob containing the message + * @param annotated_id id of the git object being annotated + * @param it pointer to the iterator + * + * @return 0 (no error), GIT_ITEROVER (iteration is done) or an error code + * (negative value) + */ +GIT_EXTERN(int) git_note_next( + git_oid *note_id, + git_oid *annotated_id, + git_note_iterator *it); + /** * Read the note for an object @@ -48,32 +110,68 @@ GIT_EXTERN(int) git_note_read( const char *notes_ref, const git_oid *oid); + +/** + * Read the note for an object from a note commit + * + * The note must be freed manually by the user. + * + * @param out pointer to the read note; NULL in case of error + * @param repo repository where to look up the note + * @param notes_commit a pointer to the notes commit object + * @param oid OID of the git object to read the note from + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_note_commit_read( + git_note **out, + git_repository *repo, + git_commit *notes_commit, + const git_oid *oid); + +/** + * Get the note author + * + * @param note the note + * @return the author + */ +GIT_EXTERN(const git_signature *) git_note_author(const git_note *note); + +/** + * Get the note committer + * + * @param note the note + * @return the committer + */ +GIT_EXTERN(const git_signature *) git_note_committer(const git_note *note); + + /** * Get the note message * - * @param note + * @param note the note * @return the note message */ GIT_EXTERN(const char *) git_note_message(const git_note *note); /** - * Get the note object OID + * Get the note object's id * - * @param note - * @return the note object OID + * @param note the note + * @return the note object's id */ -GIT_EXTERN(const git_oid *) git_note_oid(const git_note *note); +GIT_EXTERN(const git_oid *) git_note_id(const git_note *note); /** * Add a note for an object * * @param out pointer to store the OID (optional); NULL in case of error * @param repo repository where to store the note - * @param author signature of the notes commit author - * @param committer signature of the notes commit committer * @param notes_ref canonical name of the reference to use (optional); * defaults to "refs/notes/commits" + * @param author signature of the notes commit author + * @param committer signature of the notes commit committer * @param oid OID of the git object to decorate * @param note Content of the note to add for object oid * @param force Overwrite existing note @@ -83,13 +181,43 @@ GIT_EXTERN(const git_oid *) git_note_oid(const git_note *note); GIT_EXTERN(int) git_note_create( git_oid *out, git_repository *repo, + const char *notes_ref, const git_signature *author, const git_signature *committer, - const char *notes_ref, const git_oid *oid, const char *note, int force); +/** + * Add a note for an object from a commit + * + * This function will create a notes commit for a given object, + * the commit is a dangling commit, no reference is created. + * + * @param notes_commit_out pointer to store the commit (optional); + * NULL in case of error + * @param notes_blob_out a point to the id of a note blob (optional) + * @param repo repository where the note will live + * @param parent Pointer to parent note + * or NULL if this shall start a new notes tree + * @param author signature of the notes commit author + * @param committer signature of the notes commit committer + * @param oid OID of the git object to decorate + * @param note Content of the note to add for object oid + * @param allow_note_overwrite Overwrite existing note + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_note_commit_create( + git_oid *notes_commit_out, + git_oid *notes_blob_out, + git_repository *repo, + git_commit *parent, + const git_signature *author, + const git_signature *committer, + const git_oid *oid, + const char *note, + int allow_note_overwrite); /** * Remove the note for an object @@ -110,6 +238,32 @@ GIT_EXTERN(int) git_note_remove( const git_signature *committer, const git_oid *oid); +/** + * Remove the note for an object + * + * @param notes_commit_out pointer to store the new notes commit (optional); + * NULL in case of error. + * When removing a note a new tree containing all notes + * sans the note to be removed is created and a new commit + * pointing to that tree is also created. + * In the case where the resulting tree is an empty tree + * a new commit pointing to this empty tree will be returned. + * @param repo repository where the note lives + * @param notes_commit a pointer to the notes commit object + * @param author signature of the notes commit author + * @param committer signature of the notes commit committer + * @param oid OID of the git object to remove the note from + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_note_commit_remove( + git_oid *notes_commit_out, + git_repository *repo, + git_commit *notes_commit, + const git_signature *author, + const git_signature *committer, + const git_oid *oid); + /** * Free a git_note object * @@ -120,12 +274,12 @@ GIT_EXTERN(void) git_note_free(git_note *note); /** * Get the default notes reference for a repository * - * @param out Pointer to the default notes reference + * @param out buffer in which to store the name of the default notes reference * @param repo The Git repository * * @return 0 or an error code */ -GIT_EXTERN(int) git_note_default_ref(const char **out, git_repository *repo); +GIT_EXTERN(int) git_note_default_ref(git_buf *out, git_repository *repo); /** * Loop over all the notes within a specified namespace @@ -141,7 +295,7 @@ GIT_EXTERN(int) git_note_default_ref(const char **out, git_repository *repo); * * @param payload Extra parameter to callback function. * - * @return 0 on success, GIT_EUSER on non-zero callback, or error code + * @return 0 on success, non-zero callback return value, or error code */ GIT_EXTERN(int) git_note_foreach( git_repository *repo, @@ -151,4 +305,5 @@ GIT_EXTERN(int) git_note_foreach( /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/object.h b/include/git2/object.h index e029f01254b..f923f81edfb 100644 --- a/include/git2/object.h +++ b/include/git2/object.h @@ -10,16 +10,20 @@ #include "common.h" #include "types.h" #include "oid.h" +#include "buffer.h" /** * @file git2/object.h - * @brief Git revision object management routines + * @brief Objects are blobs (files), trees (directories), commits, and annotated tags * @defgroup git_object Git revision object management routines * @ingroup Git * @{ */ GIT_BEGIN_DECL +/** Maximum size of a git object */ +#define GIT_OBJECT_SIZE_MAX UINT64_MAX + /** * Lookup a reference to one of the objects in a repository. * @@ -29,20 +33,20 @@ GIT_BEGIN_DECL * * The 'type' parameter must match the type of the object * in the odb; the method will fail otherwise. - * The special value 'GIT_OBJ_ANY' may be passed to let + * The special value 'GIT_OBJECT_ANY' may be passed to let * the method guess the object's type. * * @param object pointer to the looked-up object * @param repo the repository to look up the object * @param id the unique identifier for the object * @param type the type of the object - * @return a reference to the object + * @return 0 or an error code */ GIT_EXTERN(int) git_object_lookup( git_object **object, git_repository *repo, const git_oid *id, - git_otype type); + git_object_t type); /** * Lookup a reference to one of the objects in a repository, @@ -50,18 +54,18 @@ GIT_EXTERN(int) git_object_lookup( * * The object obtained will be so that its identifier * matches the first 'len' hexadecimal characters - * (packets of 4 bits) of the given 'id'. - * 'len' must be at least GIT_OID_MINPREFIXLEN, and - * long enough to identify a unique object matching - * the prefix; otherwise the method will fail. + * (packets of 4 bits) of the given `id`. `len` must be + * at least `GIT_OID_MINPREFIXLEN`, and long enough to + * identify a unique object matching the prefix; otherwise + * the method will fail. * * The generated reference is owned by the repository and * should be closed with the `git_object_free` method * instead of free'd manually. * - * The 'type' parameter must match the type of the object + * The `type` parameter must match the type of the object * in the odb; the method will fail otherwise. - * The special value 'GIT_OBJ_ANY' may be passed to let + * The special value `GIT_OBJECT_ANY` may be passed to let * the method guess the object's type. * * @param object_out pointer where to store the looked-up object @@ -76,7 +80,24 @@ GIT_EXTERN(int) git_object_lookup_prefix( git_repository *repo, const git_oid *id, size_t len, - git_otype type); + git_object_t type); + + +/** + * Lookup an object that represents a tree entry. + * + * @param out buffer that receives a pointer to the object (which must be freed + * by the caller) + * @param treeish root object that can be peeled to a tree + * @param path relative path from the root object to the desired object + * @param type type of object desired + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_object_lookup_bypath( + git_object **out, + const git_object *treeish, + const char *path, + git_object_t type); /** * Get the id (SHA1) of a repository object @@ -86,13 +107,27 @@ GIT_EXTERN(int) git_object_lookup_prefix( */ GIT_EXTERN(const git_oid *) git_object_id(const git_object *obj); +/** + * Get a short abbreviated OID string for the object + * + * This starts at the "core.abbrev" length (default 7 characters) and + * iteratively extends to a longer string if that length is ambiguous. + * The result will be unambiguous (at least until new objects are added to + * the repository). + * + * @param out Buffer to write string into + * @param obj The object to get an ID for + * @return 0 on success, <0 for error + */ +GIT_EXTERN(int) git_object_short_id(git_buf *out, const git_object *obj); + /** * Get the object type of an object * * @param obj the repository object * @return the object's type */ -GIT_EXTERN(git_otype) git_object_type(const git_object *obj); +GIT_EXTERN(git_object_t) git_object_type(const git_object *obj); /** * Get the repository that owns this object @@ -126,7 +161,7 @@ GIT_EXTERN(git_repository *) git_object_owner(const git_object *obj); GIT_EXTERN(void) git_object_free(git_object *object); /** - * Convert an object type to it's string representation. + * Convert an object type to its string representation. * * The result is a pointer to a string in static memory and * should not be free()'ed. @@ -134,59 +169,109 @@ GIT_EXTERN(void) git_object_free(git_object *object); * @param type object type to convert. * @return the corresponding string representation. */ -GIT_EXTERN(const char *) git_object_type2string(git_otype type); +GIT_EXTERN(const char *) git_object_type2string(git_object_t type); /** - * Convert a string object type representation to it's git_otype. + * Convert a string object type representation to it's git_object_t. * * @param str the string to convert. - * @return the corresponding git_otype. + * @return the corresponding git_object_t. */ -GIT_EXTERN(git_otype) git_object_string2type(const char *str); +GIT_EXTERN(git_object_t) git_object_string2type(const char *str); /** - * Determine if the given git_otype is a valid loose object type. + * Determine if the given git_object_t is a valid object type. * * @param type object type to test. - * @return true if the type represents a valid loose object type, - * false otherwise. + * @return 1 if the type represents a valid loose object type, 0 otherwise */ -GIT_EXTERN(int) git_object_typeisloose(git_otype type); +GIT_EXTERN(int) git_object_type_is_valid(git_object_t type); /** - * Get the size in bytes for the structure which - * acts as an in-memory representation of any given - * object type. + * Recursively peel an object until an object of the specified type is met. * - * For all the core types, this would the equivalent - * of calling `sizeof(git_commit)` if the core types - * were not opaque on the external API. + * If the query cannot be satisfied due to the object model, + * GIT_EINVALIDSPEC will be returned (e.g. trying to peel a blob to a + * tree). * - * @param type object type to get its size - * @return size in bytes of the object - */ -GIT_EXTERN(size_t) git_object__size(git_otype type); - -/** - * Recursively peel an object until an object of the specified type is met. + * If you pass `GIT_OBJECT_ANY` as the target type, then the object will + * be peeled until the type changes. A tag will be peeled until the + * referenced object is no longer a tag, and a commit will be peeled + * to a tree. Any other object type will return GIT_EINVALIDSPEC. * - * The retrieved `peeled` object is owned by the repository and should be - * closed with the `git_object_free` method. + * If peeling a tag we discover an object which cannot be peeled to + * the target type due to the object model, GIT_EPEEL will be + * returned. * - * If you pass `GIT_OBJ_ANY` as the target type, then the object will be - * peeled until the type changes (e.g. a tag will be chased until the - * referenced object is no longer a tag). + * You must free the returned object. * * @param peeled Pointer to the peeled git_object * @param object The object to be processed - * @param target_type The type of the requested object (GIT_OBJ_COMMIT, - * GIT_OBJ_TAG, GIT_OBJ_TREE, GIT_OBJ_BLOB or GIT_OBJ_ANY). - * @return 0 on success, GIT_EAMBIGUOUS, GIT_ENOTFOUND or an error code + * @param target_type The type of the requested object (a GIT_OBJECT_ value) + * @return 0 on success, GIT_EINVALIDSPEC, GIT_EPEEL, or an error code */ GIT_EXTERN(int) git_object_peel( git_object **peeled, const git_object *object, - git_otype target_type); + git_object_t target_type); + +/** + * Create an in-memory copy of a Git object. The copy must be + * explicitly free'd or it will leak. + * + * @param dest Pointer to store the copy of the object + * @param source Original object to copy + * @return 0 or an error code + */ +GIT_EXTERN(int) git_object_dup(git_object **dest, git_object *source); + +#ifdef GIT_EXPERIMENTAL_SHA256 +/** + * Analyzes a buffer of raw object content and determines its validity. + * Tree, commit, and tag objects will be parsed and ensured that they + * are valid, parseable content. (Blobs are always valid by definition.) + * An error message will be set with an informative message if the object + * is not valid. + * + * @warning This function is experimental and its signature may change in + * the future. + * + * @param valid Output pointer to set with validity of the object content + * @param buf The contents to validate + * @param len The length of the buffer + * @param object_type The type of the object in the buffer + * @param oid_type The object ID type for the OIDs in the given buffer + * @return 0 on success or an error code + */ +GIT_EXTERN(int) git_object_rawcontent_is_valid( + int *valid, + const char *buf, + size_t len, + git_object_t object_type, + git_oid_t oid_type); +#else +/** + * Analyzes a buffer of raw object content and determines its validity. + * Tree, commit, and tag objects will be parsed and ensured that they + * are valid, parseable content. (Blobs are always valid by definition.) + * An error message will be set with an informative message if the object + * is not valid. + * + * @warning This function is experimental and its signature may change in + * the future. + * + * @param[out] valid Output pointer to set with validity of the object content + * @param buf The contents to validate + * @param len The length of the buffer + * @param object_type The type of the object in the buffer + * @return 0 on success or an error code + */ +GIT_EXTERN(int) git_object_rawcontent_is_valid( + int *valid, + const char *buf, + size_t len, + git_object_t object_type); +#endif /** @} */ GIT_END_DECL diff --git a/include/git2/odb.h b/include/git2/odb.h index 8fd1a95be57..e81e41c910a 100644 --- a/include/git2/odb.h +++ b/include/git2/odb.h @@ -10,29 +10,86 @@ #include "common.h" #include "types.h" #include "oid.h" -#include "odb_backend.h" +#include "oidarray.h" #include "indexer.h" /** * @file git2/odb.h - * @brief Git object database routines + * @brief An object database manages the storage of git objects * @defgroup git_odb Git object database routines * @ingroup Git * @{ */ GIT_BEGIN_DECL +/** Flags controlling the behavior of ODB lookup operations */ +typedef enum { + /** + * Don't call `git_odb_refresh` if the lookup fails. Useful when doing + * a batch of lookup operations for objects that may legitimately not + * exist. When using this flag, you may wish to manually call + * `git_odb_refresh` before processing a batch of objects. + */ + GIT_ODB_LOOKUP_NO_REFRESH = (1 << 0) +} git_odb_lookup_flags_t; + +/** + * Function type for callbacks from git_odb_foreach. + * + * @param id an id of an object in the object database + * @param payload the payload from the initial call to git_odb_foreach + * @return 0 on success, or an error code + */ +typedef int GIT_CALLBACK(git_odb_foreach_cb)(const git_oid *id, void *payload); + +/** Options for configuring a loose object backend. */ +typedef struct { + unsigned int version; /**< version for the struct */ + + /** + * Type of object IDs to use for this object database, or + * 0 for default (currently SHA1). + */ + git_oid_t oid_type; +} git_odb_options; + +/** The current version of the diff options structure */ +#define GIT_ODB_OPTIONS_VERSION 1 + +/** + * Stack initializer for odb options. Alternatively use + * `git_odb_options_init` programmatic initialization. + */ +#define GIT_ODB_OPTIONS_INIT { GIT_ODB_OPTIONS_VERSION } + /** * Create a new object database with no backends. * * Before the ODB can be used for read/writing, a custom database * backend must be manually added using `git_odb_add_backend()` * - * @param out location to store the database pointer, if opened. - * Set to NULL if the open failed. + * @note This API only supports SHA1 object databases + * @see git_odb_new_ext + * + * @param[out] odb location to store the database pointer, if opened. * @return 0 or an error code */ -GIT_EXTERN(int) git_odb_new(git_odb **out); +GIT_EXTERN(int) git_odb_new(git_odb **odb); + +#ifdef GIT_EXPERIMENTAL_SHA256 + +/** + * Create a new object database with no backends. + * + * @param[out] odb location to store the database pointer, if opened. + * @param opts the options for this object database or NULL for defaults + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_new_ext( + git_odb **odb, + const git_odb_options *opts); + +#endif /** * Create a new object database and automatically add @@ -45,48 +102,34 @@ GIT_EXTERN(int) git_odb_new(git_odb **out); * assuming `objects_dir` as the Objects folder which * contains a 'pack/' folder with the corresponding data * - * @param out location to store the database pointer, if opened. + * @note This API only supports SHA1 object databases + * @see git_odb_open_ext + * + * @param[out] odb_out location to store the database pointer, if opened. * Set to NULL if the open failed. * @param objects_dir path of the backends' "objects" directory. * @return 0 or an error code */ -GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir); +GIT_EXTERN(int) git_odb_open(git_odb **odb_out, const char *objects_dir); -/** - * Add a custom backend to an existing Object DB - * - * The backends are checked in relative ordering, based on the - * value of the `priority` parameter. - * - * Read for more information. - * - * @param odb database to add the backend to - * @param backend pointer to a git_odb_backend instance - * @param priority Value for ordering the backends queue - * @return 0 on success; error code otherwise - */ -GIT_EXTERN(int) git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority); +#ifdef GIT_EXPERIMENTAL_SHA256 /** - * Add a custom backend to an existing Object DB; this - * backend will work as an alternate. - * - * Alternate backends are always checked for objects *after* - * all the main backends have been exhausted. - * - * The backends are checked in relative ordering, based on the - * value of the `priority` parameter. - * - * Writing is disabled on alternate backends. + * Create a new object database and automatically add loose and packed + * backends. * - * Read for more information. - * - * @param odb database to add the backend to - * @param backend pointer to a git_odb_backend instance - * @param priority Value for ordering the backends queue - * @return 0 on success; error code otherwise + * @param[out] odb_out location to store the database pointer, if opened. + * Set to NULL if the open failed. + * @param objects_dir path of the backends' "objects" directory. + * @param opts the options for this object database or NULL for defaults + * @return 0 or an error code */ -GIT_EXTERN(int) git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority); +GIT_EXTERN(int) git_odb_open_ext( + git_odb **odb_out, + const char *objects_dir, + const git_odb_options *opts); + +#endif /** * Add an on-disk alternate to an existing Object DB. @@ -101,7 +144,7 @@ GIT_EXTERN(int) git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, in * * @param odb database to add the backend to * @param path path to the objects folder for the alternate - * @return 0 on success; error code otherwise + * @return 0 on success, error code otherwise */ GIT_EXTERN(int) git_odb_add_disk_alternate(git_odb *odb, const char *path); @@ -122,14 +165,13 @@ GIT_EXTERN(void) git_odb_free(git_odb *db); * internally cached, so it should be closed * by the user once it's no longer in use. * - * @param out pointer where to store the read object + * @param[out] obj pointer where to store the read object * @param db database to search for the object in. * @param id identity of the object to read. - * @return - * - 0 if the object was read; - * - GIT_ENOTFOUND if the object is not in the database. + * @return 0 if the object was read, GIT_ENOTFOUND if the object is + * not in the database. */ -GIT_EXTERN(int) git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id); +GIT_EXTERN(int) git_odb_read(git_odb_object **obj, git_odb *db, const git_oid *id); /** * Read an object from the database, given a prefix @@ -138,7 +180,7 @@ GIT_EXTERN(int) git_odb_read(git_odb_object **out, git_odb *db, const git_oid *i * This method queries all available ODB backends * trying to match the 'len' first hexadecimal * characters of the 'short_id'. - * The remaining (GIT_OID_HEXSZ-len)*4 bits of + * The remaining (GIT_OID_SHA1_HEXSIZE-len)*4 bits of * 'short_id' must be 0s. * 'len' must be at least GIT_OID_MINPREFIXLEN, * and the prefix must be long enough to identify @@ -149,16 +191,15 @@ GIT_EXTERN(int) git_odb_read(git_odb_object **out, git_odb *db, const git_oid *i * internally cached, so it should be closed * by the user once it's no longer in use. * - * @param out pointer where to store the read object + * @param[out] obj pointer where to store the read object * @param db database to search for the object in. * @param short_id a prefix of the id of the object to read. * @param len the length of the prefix - * @return - * - 0 if the object was read; - * - GIT_ENOTFOUND if the object is not in the database. - * - GIT_EAMBIGUOUS if the prefix is ambiguous (several objects match the prefix) + * @return 0 if the object was read, GIT_ENOTFOUND if the object is not in the + * database. GIT_EAMBIGUOUS if the prefix is ambiguous + * (several objects match the prefix) */ -GIT_EXTERN(int) git_odb_read_prefix(git_odb_object **out, git_odb *db, const git_oid *short_id, size_t len); +GIT_EXTERN(int) git_odb_read_prefix(git_odb_object **obj, git_odb *db, const git_oid *short_id, size_t len); /** * Read the header of an object from the database, without @@ -170,27 +211,94 @@ GIT_EXTERN(int) git_odb_read_prefix(git_odb_object **out, git_odb *db, const git * of an object, so the whole object will be read and then the * header will be returned. * - * @param len_out pointer where to store the length - * @param type_out pointer where to store the type + * @param[out] len_out pointer where to store the length + * @param[out] type_out pointer where to store the type * @param db database to search for the object in. * @param id identity of the object to read. - * @return - * - 0 if the object was read; - * - GIT_ENOTFOUND if the object is not in the database. + * @return 0 if the object was read, GIT_ENOTFOUND if the object is not + * in the database. */ -GIT_EXTERN(int) git_odb_read_header(size_t *len_out, git_otype *type_out, git_odb *db, const git_oid *id); +GIT_EXTERN(int) git_odb_read_header(size_t *len_out, git_object_t *type_out, git_odb *db, const git_oid *id); /** * Determine if the given object can be found in the object database. * * @param db database to be searched for the given object. * @param id the object to search for. - * @return - * - 1, if the object was found - * - 0, otherwise + * @return 1 if the object was found, 0 otherwise */ GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id); +/** + * Determine if the given object can be found in the object database, with + * extended options. + * + * @param db database to be searched for the given object. + * @param id the object to search for. + * @param flags flags affecting the lookup (see `git_odb_lookup_flags_t`) + * @return 1 if the object was found, 0 otherwise + */ +GIT_EXTERN(int) git_odb_exists_ext(git_odb *db, const git_oid *id, unsigned int flags); + +/** + * Determine if an object can be found in the object database by an + * abbreviated object ID. + * + * @param out The full OID of the found object if just one is found. + * @param db The database to be searched for the given object. + * @param short_id A prefix of the id of the object to read. + * @param len The length of the prefix. + * @return 0 if found, GIT_ENOTFOUND if not found, GIT_EAMBIGUOUS if multiple + * matches were found, other value < 0 if there was a read error. + */ +GIT_EXTERN(int) git_odb_exists_prefix( + git_oid *out, git_odb *db, const git_oid *short_id, size_t len); + +/** + * The information about object IDs to query in `git_odb_expand_ids`, + * which will be populated upon return. + */ +typedef struct git_odb_expand_id { + /** The object ID to expand */ + git_oid id; + + /** + * The length of the object ID (in nibbles, or packets of 4 bits; the + * number of hex characters) + * */ + unsigned short length; + + /** + * The (optional) type of the object to search for; leave as `0` or set + * to `GIT_OBJECT_ANY` to query for any object matching the ID. + */ + git_object_t type; +} git_odb_expand_id; + +/** + * Determine if one or more objects can be found in the object database + * by their abbreviated object ID and type. + * + * The given array will be updated in place: for each abbreviated ID that is + * unique in the database, and of the given type (if specified), + * the full object ID, object ID length (`GIT_OID_SHA1_HEXSIZE`) and type will be + * written back to the array. For IDs that are not found (or are ambiguous), + * the array entry will be zeroed. + * + * Note that since this function operates on multiple objects, the + * underlying database will not be asked to be reloaded if an object is + * not found (which is unlike other object database operations.) + * + * @param db The database to be searched for the given objects. + * @param ids An array of short object IDs to search for + * @param count The length of the `ids` array + * @return 0 on success or an error code on failure + */ +GIT_EXTERN(int) git_odb_expand_ids( + git_odb *db, + git_odb_expand_id *ids, + size_t count); + /** * Refresh the object database to load newly added files. * @@ -209,7 +317,7 @@ GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id); * @param db database to refresh * @return 0 on success, error code otherwise */ -GIT_EXTERN(int) git_odb_refresh(struct git_odb *db); +GIT_EXTERN(int) git_odb_refresh(git_odb *db); /** * List all objects available in the database @@ -222,9 +330,12 @@ GIT_EXTERN(int) git_odb_refresh(struct git_odb *db); * @param db database to use * @param cb the callback to call for each object * @param payload data to pass to the callback - * @return 0 on success, GIT_EUSER on non-zero callback, or error code + * @return 0 on success, non-zero callback return value, or error code */ -GIT_EXTERN(int) git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload); +GIT_EXTERN(int) git_odb_foreach( + git_odb *db, + git_odb_foreach_cb cb, + void *payload); /** * Write an object directly into the ODB @@ -239,12 +350,12 @@ GIT_EXTERN(int) git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payloa * * @param out pointer to store the OID result of the write * @param odb object database where to store the object - * @param data buffer with the data to store + * @param data @type `const unsigned char *` buffer with the data to store * @param len size of the buffer * @param type type of the data to store * @return 0 or an error code */ -GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size_t len, git_otype type); +GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size_t len, git_object_t type); /** * Open a stream to write an object into the ODB @@ -252,18 +363,12 @@ GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size * The type and final length of the object must be specified * when opening the stream. * - * The returned stream will be of type `GIT_STREAM_WRONLY` and - * will have the following methods: - * - * - stream->write: write `n` bytes into the stream - * - stream->finalize_write: close the stream and store the object in - * the odb - * - stream->free: free the stream - * - * The streaming write won't be effective until `stream->finalize_write` - * is called and returns without an error + * The returned stream will be of type `GIT_STREAM_WRONLY`, and it + * won't be effective until `git_odb_stream_finalize_write` is called + * and returns without an error * - * The stream must always be free'd or will leak memory. + * The stream must always be freed when done with `git_odb_stream_free` or + * will leak memory. * * @see git_odb_stream * @@ -273,7 +378,54 @@ GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size * @param type type of the object that will be written * @return 0 if the stream was created; error code otherwise */ -GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, size_t size, git_otype type); +GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, git_object_size_t size, git_object_t type); + +/** + * Write to an odb stream + * + * This method will fail if the total number of received bytes exceeds the + * size declared with `git_odb_open_wstream()` + * + * @param stream the stream + * @param buffer the data to write + * @param len the buffer's length + * @return 0 if the write succeeded, error code otherwise + */ +GIT_EXTERN(int) git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len); + +/** + * Finish writing to an odb stream + * + * The object will take its final name and will be available to the + * odb. + * + * This method will fail if the total number of received bytes + * differs from the size declared with `git_odb_open_wstream()` + * + * @param out pointer to store the resulting object's id + * @param stream the stream + * @return 0 on success, an error code otherwise + */ +GIT_EXTERN(int) git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream); + +/** + * Read from an odb stream + * + * Most backends don't implement streaming reads + * + * @param stream the stream + * @param buffer a user-allocated buffer to store the data in. + * @param len the buffer's length + * @return the number of bytes read if succeeded, error code otherwise + */ +GIT_EXTERN(int) git_odb_stream_read(git_odb_stream *stream, char *buffer, size_t len); + +/** + * Free an odb stream + * + * @param stream the stream to free + */ +GIT_EXTERN(void) git_odb_stream_free(git_odb_stream *stream); /** * Open a stream to read an object from the ODB @@ -295,11 +447,18 @@ GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, size_t s * @see git_odb_stream * * @param out pointer where to store the stream + * @param len pointer where to store the length of the object + * @param type pointer where to store the type of the object * @param db object database where the stream will read from * @param oid oid of the object the stream will read from - * @return 0 if the stream was created; error code otherwise + * @return 0 if the stream was created, error code otherwise */ -GIT_EXTERN(int) git_odb_open_rstream(git_odb_stream **out, git_odb *db, const git_oid *oid); +GIT_EXTERN(int) git_odb_open_rstream( + git_odb_stream **out, + size_t *len, + git_object_t *type, + git_odb *db, + const git_oid *oid); /** * Open a stream for writing a pack file to the ODB. @@ -318,26 +477,77 @@ GIT_EXTERN(int) git_odb_open_rstream(git_odb_stream **out, git_odb *db, const gi * Be aware that this is called inline with network and indexing operations, * so performance may be affected. * @param progress_payload payload for the progress callback + * @return 0 or an error code. */ GIT_EXTERN(int) git_odb_write_pack( git_odb_writepack **out, git_odb *db, - git_transfer_progress_callback progress_cb, + git_indexer_progress_cb progress_cb, void *progress_payload); /** - * Determine the object-ID (sha1 hash) of a data buffer + * Write a `multi-pack-index` file from all the `.pack` files in the ODB. + * + * If the ODB layer understands pack files, then this will create a file called + * `multi-pack-index` next to the `.pack` and `.idx` files, which will contain + * an index of all objects stored in `.pack` files. This will allow for + * O(log n) lookup for n objects (regardless of how many packfiles there + * exist). + * + * @param db object database where the `multi-pack-index` file will be written. + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_odb_write_multi_pack_index( + git_odb *db); + +#ifdef GIT_EXPERIMENTAL_SHA256 + +/** + * Generate the object ID (in SHA1 or SHA256 format) for a given data buffer. + * + * @param[out] oid the resulting object ID. + * @param data data to hash + * @param len size of the data + * @param object_type of the data to hash + * @param oid_type the oid type to hash to + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_hash( + git_oid *oid, + const void *data, + size_t len, + git_object_t object_type, + git_oid_t oid_type); + +/** + * Determine the object ID of a file on disk. + * + * @param[out] oid oid structure the result is written into. + * @param path file to read and determine object id for + * @param object_type of the data to hash + * @param oid_type the oid type to hash to + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_hashfile( + git_oid *oid, + const char *path, + git_object_t object_type, + git_oid_t oid_type); +#else + +/** + * Determine the object-ID (sha1 or sha256 hash) of a data buffer * - * The resulting SHA-1 OID will be the identifier for the data - * buffer as if the data buffer it were to written to the ODB. + * The resulting OID will be the identifier for the data buffer as if + * the data buffer it were to written to the ODB. * - * @param out the resulting object-ID. + * @param[out] oid the resulting object-ID. * @param data data to hash * @param len size of the data - * @param type of the data to hash + * @param object_type of the data to hash * @return 0 or an error code */ -GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_otype type); +GIT_EXTERN(int) git_odb_hash(git_oid *oid, const void *data, size_t len, git_object_t object_type); /** * Read a file from disk and fill a git_oid with the object id @@ -347,12 +557,28 @@ GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_oty * the `-w` flag, however, with the --no-filters flag. * If you need filters, see git_repository_hashfile. * - * @param out oid structure the result is written into. + * @param[out] oid oid structure the result is written into. * @param path file to read and determine object id for - * @param type the type of the object that will be hashed + * @param object_type of the data to hash * @return 0 or an error code */ -GIT_EXTERN(int) git_odb_hashfile(git_oid *out, const char *path, git_otype type); +GIT_EXTERN(int) git_odb_hashfile(git_oid *oid, const char *path, git_object_t object_type); + +#endif + +/** + * Create a copy of an odb_object + * + * The returned copy must be manually freed with `git_odb_object_free`. + * Note that because of an implementation detail, the returned copy will be + * the same pointer as `source`: the object is internally refcounted, so the + * copy still needs to be freed twice. + * + * @param dest pointer where to store the copy + * @param source object to copy + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_object_dup(git_odb_object **dest, git_odb_object *source); /** * Close an ODB object @@ -383,7 +609,7 @@ GIT_EXTERN(const git_oid *) git_odb_object_id(git_odb_object *object); * This pointer is owned by the object and shall not be free'd. * * @param object the object - * @return a pointer to the data + * @return @type `const unsigned char *` a pointer to the data */ GIT_EXTERN(const void *) git_odb_object_data(git_odb_object *object); @@ -404,8 +630,78 @@ GIT_EXTERN(size_t) git_odb_object_size(git_odb_object *object); * @param object the object * @return the type */ -GIT_EXTERN(git_otype) git_odb_object_type(git_odb_object *object); +GIT_EXTERN(git_object_t) git_odb_object_type(git_odb_object *object); + +/** + * Add a custom backend to an existing Object DB + * + * The backends are checked in relative ordering, based on the + * value of the `priority` parameter. + * + * Read for more information. + * + * @param odb database to add the backend to + * @param backend pointer to a git_odb_backend instance + * @param priority Value for ordering the backends queue + * @return 0 on success, error code otherwise + */ +GIT_EXTERN(int) git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority); + +/** + * Add a custom backend to an existing Object DB; this + * backend will work as an alternate. + * + * Alternate backends are always checked for objects *after* + * all the main backends have been exhausted. + * + * The backends are checked in relative ordering, based on the + * value of the `priority` parameter. + * + * Writing is disabled on alternate backends. + * + * Read for more information. + * + * @param odb database to add the backend to + * @param backend pointer to a git_odb_backend instance + * @param priority Value for ordering the backends queue + * @return 0 on success, error code otherwise + */ +GIT_EXTERN(int) git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority); + +/** + * Get the number of ODB backend objects + * + * @param odb object database + * @return number of backends in the ODB + */ +GIT_EXTERN(size_t) git_odb_num_backends(git_odb *odb); + +/** + * Lookup an ODB backend object by index + * + * @param out output pointer to ODB backend at pos + * @param odb object database + * @param pos index into object database backend list + * @return 0 on success, GIT_ENOTFOUND if pos is invalid, other errors < 0 + */ +GIT_EXTERN(int) git_odb_get_backend(git_odb_backend **out, git_odb *odb, size_t pos); + +/** + * Set the git commit-graph for the ODB. + * + * After a successful call, the ownership of the cgraph parameter will be + * transferred to libgit2, and the caller should not free it. + * + * The commit-graph can also be unset by explicitly passing NULL as the cgraph + * parameter. + * + * @param odb object database + * @param cgraph the git commit-graph + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_odb_set_commit_graph(git_odb *odb, git_commit_graph *cgraph); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/odb_backend.h b/include/git2/odb_backend.h index dbc3981f69e..88ca29fb9f8 100644 --- a/include/git2/odb_backend.h +++ b/include/git2/odb_backend.h @@ -9,141 +9,243 @@ #include "common.h" #include "types.h" -#include "oid.h" #include "indexer.h" /** * @file git2/backend.h - * @brief Git custom backend functions - * @defgroup git_backend Git custom backend API + * @brief Object database backends manage the storage of git objects + * @defgroup git_odb Git object database routines * @ingroup Git * @{ */ GIT_BEGIN_DECL -struct git_odb_stream; -struct git_odb_writepack; +/** Options for configuring a packfile object backend. */ +typedef struct { + unsigned int version; /**< version for the struct */ -/** - * Function type for callbacks from git_odb_foreach. - */ -typedef int (*git_odb_foreach_cb)(const git_oid *id, void *payload); + /** + * Type of object IDs to use for this object database, or + * 0 for default (currently SHA1). + */ + git_oid_t oid_type; +} git_odb_backend_pack_options; + +/** The current version of the diff options structure */ +#define GIT_ODB_BACKEND_PACK_OPTIONS_VERSION 1 /** - * An instance for a custom backend + * Stack initializer for odb pack backend options. Alternatively use + * `git_odb_backend_pack_options_init` programmatic initialization. */ -struct git_odb_backend { - unsigned int version; - git_odb *odb; - - /* read and read_prefix each return to libgit2 a buffer which - * will be freed later. The buffer should be allocated using - * the function git_odb_backend_malloc to ensure that it can - * be safely freed later. */ - int (* read)( - void **, size_t *, git_otype *, - struct git_odb_backend *, - const git_oid *); - - /* To find a unique object given a prefix - * of its oid. - * The oid given must be so that the - * remaining (GIT_OID_HEXSZ - len)*4 bits - * are 0s. +#define GIT_ODB_BACKEND_PACK_OPTIONS_INIT \ + { GIT_ODB_BACKEND_PACK_OPTIONS_VERSION } + +typedef enum { + GIT_ODB_BACKEND_LOOSE_FSYNC = (1 << 0) +} git_odb_backend_loose_flag_t; + +/** Options for configuring a loose object backend. */ +typedef struct { + unsigned int version; /**< version for the struct */ + + /** A combination of the `git_odb_backend_loose_flag_t` types. */ + uint32_t flags; + + /** + * zlib compression level to use (0-9), where 1 is the fastest + * at the expense of larger files, and 9 produces the best + * compression at the expense of speed. 0 indicates that no + * compression should be performed. -1 is the default (currently + * optimizing for speed). */ - int (* read_prefix)( - git_oid *, - void **, size_t *, git_otype *, - struct git_odb_backend *, - const git_oid *, - size_t); - - int (* read_header)( - size_t *, git_otype *, - struct git_odb_backend *, - const git_oid *); - - /* The writer may assume that the object - * has already been hashed and is passed - * in the first parameter. + int compression_level; + + /** Permissions to use creating a directory or 0 for defaults */ + unsigned int dir_mode; + + /** Permissions to use creating a file or 0 for defaults */ + unsigned int file_mode; + + /** + * Type of object IDs to use for this object database, or + * 0 for default (currently SHA1). */ - int (* write)( - git_oid *, - struct git_odb_backend *, - const void *, - size_t, - git_otype); - - int (* writestream)( - struct git_odb_stream **, - struct git_odb_backend *, - size_t, - git_otype); - - int (* readstream)( - struct git_odb_stream **, - struct git_odb_backend *, - const git_oid *); - - int (* exists)( - struct git_odb_backend *, - const git_oid *); - - int (* refresh)(struct git_odb_backend *); - - int (* foreach)( - struct git_odb_backend *, - git_odb_foreach_cb cb, - void *payload); - - int (* writepack)( - struct git_odb_writepack **, - struct git_odb_backend *, - git_transfer_progress_callback progress_cb, - void *progress_payload); - - void (* free)(struct git_odb_backend *); -}; + git_oid_t oid_type; +} git_odb_backend_loose_options; + +/** The current version of the diff options structure */ +#define GIT_ODB_BACKEND_LOOSE_OPTIONS_VERSION 1 + +/** + * Stack initializer for odb loose backend options. Alternatively use + * `git_odb_backend_loose_options_init` programmatic initialization. + */ +#define GIT_ODB_BACKEND_LOOSE_OPTIONS_INIT \ + { GIT_ODB_BACKEND_LOOSE_OPTIONS_VERSION, 0, -1 } + +/* + * Constructors for in-box ODB backends. + */ -#define GIT_ODB_BACKEND_VERSION 1 -#define GIT_ODB_BACKEND_INIT {GIT_ODB_BACKEND_VERSION} +#ifdef GIT_EXPERIMENTAL_SHA256 + +/** + * Create a backend for a directory containing packfiles. + * + * @param[out] out location to store the odb backend pointer + * @param objects_dir the Git repository's objects directory + * @param opts the options to use when creating the pack backend + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_backend_pack( + git_odb_backend **out, + const char *objects_dir, + const git_odb_backend_pack_options *opts); + +/** + * Create a backend for a single packfile. + * + * @param[out] out location to store the odb backend pointer + * @param index_file path to the packfile's .idx file + * @param opts the options to use when creating the pack backend + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_backend_one_pack( + git_odb_backend **out, + const char *index_file, + const git_odb_backend_pack_options *opts); + +/** + * Create a backend for loose objects + * + * @param[out] out location to store the odb backend pointer + * @param objects_dir the Git repository's objects directory + * @param opts options for the loose object backend or NULL + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_backend_loose( + git_odb_backend **out, + const char *objects_dir, + git_odb_backend_loose_options *opts); + +#else + +/** + * Create a backend for a directory containing packfiles. + * + * @param[out] out location to store the odb backend pointer + * @param objects_dir the Git repository's objects directory + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_backend_pack( + git_odb_backend **out, + const char *objects_dir); + +/** + * Create a backend out of a single packfile + * + * This can be useful for inspecting the contents of a single + * packfile. + * + * @param[out] out location to store the odb backend pointer + * @param index_file path to the packfile's .idx file + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_backend_one_pack( + git_odb_backend **out, + const char *index_file); + +/** + * Create a backend for loose objects + * + * @param[out] out location to store the odb backend pointer + * @param objects_dir the Git repository's objects directory + * @param compression_level zlib compression level (0-9), or -1 for the default + * @param do_fsync if non-zero, perform an fsync on write + * @param dir_mode permission to use when creating directories, or 0 for default + * @param file_mode permission to use when creating directories, or 0 for default + * @return 0 or an error code + */ +GIT_EXTERN(int) git_odb_backend_loose( + git_odb_backend **out, + const char *objects_dir, + int compression_level, + int do_fsync, + unsigned int dir_mode, + unsigned int file_mode); + +#endif /** Streaming mode */ -enum { +typedef enum { GIT_STREAM_RDONLY = (1 << 1), GIT_STREAM_WRONLY = (1 << 2), - GIT_STREAM_RW = (GIT_STREAM_RDONLY | GIT_STREAM_WRONLY), -}; + GIT_STREAM_RW = (GIT_STREAM_RDONLY | GIT_STREAM_WRONLY) +} git_odb_stream_t; -/** A stream to read/write from a backend */ +/** + * A stream to read/write from a backend. + * + * This represents a stream of data being written to or read from a + * backend. When writing, the frontend functions take care of + * calculating the object's id and all `finalize_write` needs to do is + * store the object with the id it is passed. + */ struct git_odb_stream { - struct git_odb_backend *backend; + git_odb_backend *backend; unsigned int mode; + void *hash_ctx; + +#ifdef GIT_EXPERIMENTAL_SHA256 + git_oid_t oid_type; +#endif + + git_object_size_t declared_size; + git_object_size_t received_bytes; - int (*read)(struct git_odb_stream *stream, char *buffer, size_t len); - int (*write)(struct git_odb_stream *stream, const char *buffer, size_t len); - int (*finalize_write)(git_oid *oid_p, struct git_odb_stream *stream); - void (*free)(struct git_odb_stream *stream); + /** + * Write at most `len` bytes into `buffer` and advance the stream. + */ + int GIT_CALLBACK(read)(git_odb_stream *stream, char *buffer, size_t len); + + /** + * Write `len` bytes from `buffer` into the stream. + */ + int GIT_CALLBACK(write)(git_odb_stream *stream, const char *buffer, size_t len); + + /** + * Store the contents of the stream as an object with the id + * specified in `oid`. + * + * This method might not be invoked if: + * - an error occurs earlier with the `write` callback, + * - the object referred to by `oid` already exists in any backend, or + * - the final number of received bytes differs from the size declared + * with `git_odb_open_wstream()` + */ + int GIT_CALLBACK(finalize_write)(git_odb_stream *stream, const git_oid *oid); + + /** + * Free the stream's memory. + * + * This method might be called without a call to `finalize_write` if + * an error occurs or if the object is already present in the ODB. + */ + void GIT_CALLBACK(free)(git_odb_stream *stream); }; /** A stream to write a pack file to the ODB */ struct git_odb_writepack { - struct git_odb_backend *backend; + git_odb_backend *backend; - int (*add)(struct git_odb_writepack *writepack, const void *data, size_t size, git_transfer_progress *stats); - int (*commit)(struct git_odb_writepack *writepack, git_transfer_progress *stats); - void (*free)(struct git_odb_writepack *writepack); + int GIT_CALLBACK(append)(git_odb_writepack *writepack, const void *data, size_t size, git_indexer_progress *stats); + int GIT_CALLBACK(commit)(git_odb_writepack *writepack, git_indexer_progress *stats); + void GIT_CALLBACK(free)(git_odb_writepack *writepack); }; -GIT_EXTERN(void *) git_odb_backend_malloc(git_odb_backend *backend, size_t len); - -/** - * Constructors for in-box ODB backends. - */ -GIT_EXTERN(int) git_odb_backend_pack(git_odb_backend **out, const char *objects_dir); -GIT_EXTERN(int) git_odb_backend_loose(git_odb_backend **out, const char *objects_dir, int compression_level, int do_fsync); -GIT_EXTERN(int) git_odb_backend_one_pack(git_odb_backend **out, const char *index_file); - +/** @} */ GIT_END_DECL #endif diff --git a/include/git2/oid.h b/include/git2/oid.h index 6be02da6e71..f1920648bcf 100644 --- a/include/git2/oid.h +++ b/include/git2/oid.h @@ -8,22 +8,92 @@ #define INCLUDE_git_oid_h__ #include "common.h" -#include "types.h" +#include "experimental.h" /** * @file git2/oid.h - * @brief Git object id routines + * @brief Object IDs * @defgroup git_oid Git object id routines * @ingroup Git * @{ */ GIT_BEGIN_DECL -/** Size (in bytes) of a raw/binary oid */ -#define GIT_OID_RAWSZ 20 +/** The type of object id. */ +typedef enum { -/** Size (in bytes) of a hex formatted oid */ -#define GIT_OID_HEXSZ (GIT_OID_RAWSZ * 2) +#ifdef GIT_EXPERIMENTAL_SHA256 + GIT_OID_SHA1 = 1, /**< SHA1 */ + GIT_OID_SHA256 = 2 /**< SHA256 */ +#else + GIT_OID_SHA1 = 1 /**< SHA1 */ +#endif + +} git_oid_t; + +/* + * SHA1 is currently the only supported object ID type. + */ + +/** SHA1 is currently libgit2's default oid type. */ +#define GIT_OID_DEFAULT GIT_OID_SHA1 + +/** Size (in bytes) of a raw/binary sha1 oid */ +#define GIT_OID_SHA1_SIZE 20 +/** Size (in bytes) of a hex formatted sha1 oid */ +#define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2) + +/** + * The binary representation of the null sha1 object ID. + */ +#ifndef GIT_EXPERIMENTAL_SHA256 +# define GIT_OID_SHA1_ZERO { { 0 } } +#else +# define GIT_OID_SHA1_ZERO { GIT_OID_SHA1, { 0 } } +#endif + +/** + * The string representation of the null sha1 object ID. + */ +#define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000" + +/* + * Experimental SHA256 support is a breaking change to the API. + * This exists for application compatibility testing. + */ + +#ifdef GIT_EXPERIMENTAL_SHA256 + +/** Size (in bytes) of a raw/binary sha256 oid */ +# define GIT_OID_SHA256_SIZE 32 +/** Size (in bytes) of a hex formatted sha256 oid */ +# define GIT_OID_SHA256_HEXSIZE (GIT_OID_SHA256_SIZE * 2) + +/** + * The binary representation of the null sha256 object ID. + */ +# define GIT_OID_SHA256_ZERO { GIT_OID_SHA256, { 0 } } + +/** + * The string representation of the null sha256 object ID. + */ +# define GIT_OID_SHA256_HEXZERO "0000000000000000000000000000000000000000000000000000000000000000" + +#endif + +/** Maximum possible object ID size in raw format */ +#ifdef GIT_EXPERIMENTAL_SHA256 +# define GIT_OID_MAX_SIZE GIT_OID_SHA256_SIZE +#else +# define GIT_OID_MAX_SIZE GIT_OID_SHA1_SIZE +#endif + +/** Maximum possible object ID size in hex format */ +#ifdef GIT_EXPERIMENTAL_SHA256 +# define GIT_OID_MAX_HEXSIZE GIT_OID_SHA256_HEXSIZE +#else +# define GIT_OID_MAX_HEXSIZE GIT_OID_SHA1_HEXSIZE +#endif /** Minimum length (in number of hex characters, * i.e. packets of 4 bits) of an oid prefix */ @@ -31,26 +101,105 @@ GIT_BEGIN_DECL /** Unique identity of any object (commit, tree, blob, tag). */ typedef struct git_oid { + +#ifdef GIT_EXPERIMENTAL_SHA256 + /** type of object id */ + unsigned char type; +#endif + /** raw binary formatted id */ - unsigned char id[GIT_OID_RAWSZ]; + unsigned char id[GIT_OID_MAX_SIZE]; } git_oid; +#ifdef GIT_EXPERIMENTAL_SHA256 + +/** + * Parse a NUL terminated hex formatted object id string into a `git_oid`. + * + * The given string must be NUL terminated, and must be the hex size of + * the given object ID type - 40 characters for SHA1, 64 characters for + * SHA256. + * + * To parse an incomplete object ID (an object ID prefix), or a sequence + * of characters that is not NUL terminated, use `git_oid_from_prefix`. + * + * @param out oid structure the result is written into. + * @param str input hex string + * @param type object id type + * @return 0 or an error code + */ +GIT_EXTERN(int) git_oid_from_string( + git_oid *out, + const char *str, + git_oid_t type); + +/** + * Parse the given number of characters out of a hex formatted object id + * string into a `git_oid`. + * + * The given length can be between 0 and the hex size for the given object ID + * type - 40 characters for SHA1, 64 characters for SHA256. The remainder of + * the `git_oid` will be set to zeros. + * + * @param out oid structure the result is written into. + * @param str input hex prefix + * @param type object id type + * @return 0 or an error code + */ +GIT_EXTERN(int) git_oid_from_prefix( + git_oid *out, + const char *str, + size_t len, + git_oid_t type); + +/** + * Parse a raw object id into a `git_oid`. + * + * The appropriate number of bytes for the given object ID type will + * be read from the byte array - 20 bytes for SHA1, 32 bytes for SHA256. + * + * @param out oid structure the result is written into. + * @param raw raw object ID bytes + * @param type object id type + * @return 0 or an error code + */ +GIT_EXTERN(int) git_oid_from_raw( + git_oid *out, + const unsigned char *raw, + git_oid_t type); + +#endif + /** * Parse a hex formatted object id into a git_oid. * + * The appropriate number of bytes for the given object ID type will + * be read from the string - 40 bytes for SHA1, 64 bytes for SHA256. + * The given string need not be NUL terminated. + * * @param out oid structure the result is written into. * @param str input hex string; must be pointing at the start of * the hex sequence and have at least the number of bytes - * needed for an oid encoded in hex (40 bytes). + * needed for an oid encoded in hex (40 bytes for sha1, + * 256 bytes for sha256). * @return 0 or an error code */ GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str); /** - * Parse N characters of a hex formatted object id into a git_oid + * Parse a hex formatted NUL-terminated string into a git_oid. * - * If N is odd, N-1 characters will be parsed instead. - * The remaining space in the git_oid will be set to zero. + * @param out oid structure the result is written into. + * @param str input hex string; must be null-terminated. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str); + +/** + * Parse N characters of a hex formatted object id into a git_oid. + * + * If N is odd, the last byte's high nibble will be read in and the + * low nibble set to zero. * * @param out oid structure the result is written into. * @param str input hex string of at least size `length` @@ -64,20 +213,35 @@ GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length); * * @param out oid structure the result is written into. * @param raw the raw input bytes to be copied. + * @return 0 on success or error code */ -GIT_EXTERN(void) git_oid_fromraw(git_oid *out, const unsigned char *raw); +GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw); /** * Format a git_oid into a hex string. * * @param out output hex string; must be pointing at the start of * the hex sequence and have at least the number of bytes - * needed for an oid encoded in hex (40 bytes). Only the - * oid digits are written; a '\\0' terminator must be added - * by the caller if it is required. - * @param oid oid structure to format. + * needed for an oid encoded in hex (40 bytes for SHA1, + * 64 bytes for SHA256). Only the oid digits are written; + * a '\\0' terminator must be added by the caller if it is + * required. + * @param id oid structure to format. + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_oid_fmt(char *out, const git_oid *id); + +/** + * Format a git_oid into a partial hex string. + * + * @param out output hex string; you say how many bytes to write. + * If the number of bytes is > GIT_OID_SHA1_HEXSIZE, extra bytes + * will be zeroed; if not, a '\0' terminator is NOT added. + * @param n number of characters to write into out string + * @param id oid structure to format. + * @return 0 on success or error code */ -GIT_EXTERN(void) git_oid_fmt(char *out, const git_oid *id); +GIT_EXTERN(int) git_oid_nfmt(char *out, size_t n, const git_oid *id); /** * Format a git_oid into a loose-object path string. @@ -87,30 +251,40 @@ GIT_EXTERN(void) git_oid_fmt(char *out, const git_oid *id); * * @param out output hex string; must be pointing at the start of * the hex sequence and have at least the number of bytes - * needed for an oid encoded in hex (41 bytes). Only the - * oid digits are written; a '\\0' terminator must be added - * by the caller if it is required. + * needed for an oid encoded in hex (41 bytes for SHA1, + * 65 bytes for SHA256). Only the oid digits are written; + * a '\\0' terminator must be added by the caller if it + * is required. * @param id oid structure to format. + * @return 0 on success, non-zero callback return value, or error code */ -GIT_EXTERN(void) git_oid_pathfmt(char *out, const git_oid *id); +GIT_EXTERN(int) git_oid_pathfmt(char *out, const git_oid *id); /** - * Format a git_oid into a newly allocated c-string. + * Format a git_oid into a statically allocated c-string. + * + * The c-string is owned by the library and should not be freed + * by the user. If libgit2 is built with thread support, the string + * will be stored in TLS (i.e. one buffer per thread) to allow for + * concurrent calls of the function. * - * @param oid the oid structure to format - * @return the c-string; NULL if memory is exhausted. Caller must - * deallocate the string with git__free(). + * @param oid The oid structure to format + * @return the c-string or NULL on failure */ -GIT_EXTERN(char *) git_oid_allocfmt(const git_oid *id); +GIT_EXTERN(char *) git_oid_tostr_s(const git_oid *oid); /** * Format a git_oid into a buffer as a hex format c-string. * - * If the buffer is smaller than GIT_OID_HEXSZ+1, then the resulting - * oid c-string will be truncated to n-1 characters. If there are - * any input parameter errors (out == NULL, n == 0, oid == NULL), - * then a pointer to an empty string is returned, so that the return - * value can always be printed. + * If the buffer is smaller than the size of a hex-formatted oid string + * plus an additional byte (GIT_OID_SHA_HEXSIZE + 1 for SHA1 or + * GIT_OID_SHA256_HEXSIZE + 1 for SHA256), then the resulting + * oid c-string will be truncated to n-1 characters (but will still be + * NUL-byte terminated). + * + * If there are any input parameter errors (out == NULL, n == 0, oid == + * NULL), then a pointer to an empty string is returned, so that the + * return value can always be printed. * * @param out the buffer into which the oid string is output. * @param n the size of the out buffer. @@ -125,8 +299,9 @@ GIT_EXTERN(char *) git_oid_tostr(char *out, size_t n, const git_oid *id); * * @param out oid structure the result is written into. * @param src oid structure to copy from. + * @return 0 on success or error code */ -GIT_EXTERN(void) git_oid_cpy(git_oid *out, const git_oid *src); +GIT_EXTERN(int) git_oid_cpy(git_oid *out, const git_oid *src); /** * Compare two oid structures. @@ -135,19 +310,7 @@ GIT_EXTERN(void) git_oid_cpy(git_oid *out, const git_oid *src); * @param b second oid structure. * @return <0, 0, >0 if a < b, a == b, a > b. */ -GIT_INLINE(int) git_oid_cmp(const git_oid *a, const git_oid *b) -{ - const unsigned char *sha1 = a->id; - const unsigned char *sha2 = b->id; - int i; - - for (i = 0; i < GIT_OID_RAWSZ; i++, sha1++, sha2++) { - if (*sha1 != *sha2) - return *sha1 - *sha2; - } - - return 0; -} +GIT_EXTERN(int) git_oid_cmp(const git_oid *a, const git_oid *b); /** * Compare two oid structures for equality @@ -156,10 +319,7 @@ GIT_INLINE(int) git_oid_cmp(const git_oid *a, const git_oid *b) * @param b second oid structure. * @return true if equal, false otherwise */ -GIT_INLINE(int) git_oid_equal(const git_oid *a, const git_oid *b) -{ - return !git_oid_cmp(a, b); -} +GIT_EXTERN(int) git_oid_equal(const git_oid *a, const git_oid *b); /** * Compare the first 'len' hexadecimal characters (packets of 4 bits) @@ -177,17 +337,27 @@ GIT_EXTERN(int) git_oid_ncmp(const git_oid *a, const git_oid *b, size_t len); * * @param id oid structure. * @param str input hex string of an object id. - * @return GIT_ENOTOID if str is not a valid hex string, - * 0 in case of a match, GIT_ERROR otherwise. + * @return 0 in case of a match, -1 otherwise. */ GIT_EXTERN(int) git_oid_streq(const git_oid *id, const char *str); +/** + * Compare an oid to an hex formatted object id. + * + * @param id oid structure. + * @param str input hex string of an object id. + * @return -1 if str is not valid, <0 if id sorts before str, + * 0 if id matches str, >0 if id sorts after str. + */ +GIT_EXTERN(int) git_oid_strcmp(const git_oid *id, const char *str); + /** * Check is an oid is all zeros. * + * @param id the object ID to check * @return 1 if all zeros, 0 otherwise. */ -GIT_EXTERN(int) git_oid_iszero(const git_oid *id); +GIT_EXTERN(int) git_oid_is_zero(const git_oid *id); /** * OID Shortener object @@ -220,13 +390,13 @@ GIT_EXTERN(git_oid_shorten *) git_oid_shorten_new(size_t min_length); * or freed. * * For performance reasons, there is a hard-limit of how many - * OIDs can be added to a single set (around ~22000, assuming + * OIDs can be added to a single set (around ~32000, assuming * a mostly randomized distribution), which should be enough * for any kind of program, and keeps the algorithm fast and * memory-efficient. * * Attempting to add more than those OIDs will result in a - * GIT_ENOMEM error + * GIT_ERROR_INVALID error * * @param os a `git_oid_shorten` instance * @param text_id an OID in text form @@ -245,4 +415,5 @@ GIT_EXTERN(void) git_oid_shorten_free(git_oid_shorten *os); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/oidarray.h b/include/git2/oidarray.h new file mode 100644 index 00000000000..e79a55953df --- /dev/null +++ b/include/git2/oidarray.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_oidarray_h__ +#define INCLUDE_git_oidarray_h__ + +#include "common.h" +#include "oid.h" + +/** + * @file git2/oidarray.h + * @brief An array of object IDs + * @defgroup git_oidarray Arrays of object IDs + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** Array of object ids */ +typedef struct git_oidarray { + git_oid *ids; + size_t count; +} git_oidarray; + +/** + * Free the object IDs contained in an oid_array. This method should + * be called on `git_oidarray` objects that were provided by the + * library. Not doing so will result in a memory leak. + * + * This does not free the `git_oidarray` itself, since the library will + * never allocate that object directly itself. + * + * @param array git_oidarray from which to free oid data + */ +GIT_EXTERN(void) git_oidarray_dispose(git_oidarray *array); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/pack.h b/include/git2/pack.h index bc628c56a94..3837e04468d 100644 --- a/include/git2/pack.h +++ b/include/git2/pack.h @@ -9,16 +9,51 @@ #include "common.h" #include "oid.h" +#include "indexer.h" /** * @file git2/pack.h * @brief Git pack management routines - * @defgroup git_pack Git pack management routines + * + * Packing objects + * --------------- + * + * Creation of packfiles requires two steps: + * + * - First, insert all the objects you want to put into the packfile + * using `git_packbuilder_insert` and `git_packbuilder_insert_tree`. + * It's important to add the objects in recency order ("in the order + * that they are 'reachable' from head"). + * + * "ANY order will give you a working pack, ... [but it is] the thing + * that gives packs good locality. It keeps the objects close to the + * head (whether they are old or new, but they are _reachable_ from the + * head) at the head of the pack. So packs actually have absolutely + * _wonderful_ IO patterns." - Linus Torvalds + * git.git/Documentation/technical/pack-heuristics.txt + * + * - Second, use `git_packbuilder_write` or `git_packbuilder_foreach` to + * write the resulting packfile. + * + * libgit2 will take care of the delta ordering and generation. + * `git_packbuilder_set_threads` can be used to adjust the number of + * threads used for the process. + * + * See tests/pack/packbuilder.c for an example. + * * @ingroup Git * @{ */ GIT_BEGIN_DECL +/** + * Stages that are reported by the packbuilder progress callback. + */ +typedef enum { + GIT_PACKBUILDER_ADDING_OBJECTS = 0, + GIT_PACKBUILDER_DELTAFICATION = 1 +} git_packbuilder_stage_t; + /** * Initialize a new packbuilder * @@ -69,14 +104,108 @@ GIT_EXTERN(int) git_packbuilder_insert(git_packbuilder *pb, const git_oid *id, c GIT_EXTERN(int) git_packbuilder_insert_tree(git_packbuilder *pb, const git_oid *id); /** - * Write the new pack and the corresponding index to path + * Insert a commit object + * + * This will add a commit as well as the completed referenced tree. * * @param pb The packbuilder - * @param path Directory to store the new pack and index + * @param id The oid of the commit + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_packbuilder_insert_commit(git_packbuilder *pb, const git_oid *id); + +/** + * Insert objects as given by the walk * + * Those commits and all objects they reference will be inserted into + * the packbuilder. + * + * @param pb the packbuilder + * @param walk the revwalk to use to fill the packbuilder + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_packbuilder_insert_walk(git_packbuilder *pb, git_revwalk *walk); + +/** + * Recursively insert an object and its referenced objects + * + * Insert the object as well as any object it references. + * + * @param pb the packbuilder + * @param id the id of the root object to insert + * @param name optional name for the object + * @return 0 or an error code + */ +GIT_EXTERN(int) git_packbuilder_insert_recur(git_packbuilder *pb, const git_oid *id, const char *name); + +/** + * Write the contents of the packfile to an in-memory buffer + * + * The contents of the buffer will become a valid packfile, even though there + * will be no attached index + * + * @param buf Buffer where to write the packfile + * @param pb The packbuilder * @return 0 or an error code */ -GIT_EXTERN(int) git_packbuilder_write(git_packbuilder *pb, const char *file); +GIT_EXTERN(int) git_packbuilder_write_buf(git_buf *buf, git_packbuilder *pb); + +/** + * Write the new pack and corresponding index file to path. + * + * @param pb The packbuilder + * @param path Path to the directory where the packfile and index should be stored, or NULL for default location + * @param mode permissions to use creating a packfile or 0 for defaults + * @param progress_cb function to call with progress information from the indexer (optional) + * @param progress_cb_payload payload for the progress callback (optional) + * + * @return 0 or an error code + */ +GIT_EXTERN(int) git_packbuilder_write( + git_packbuilder *pb, + const char *path, + unsigned int mode, + git_indexer_progress_cb progress_cb, + void *progress_cb_payload); + +#ifndef GIT_DEPRECATE_HARD +/** + * Get the packfile's hash + * + * A packfile's name is derived from the sorted hashing of all object + * names. This is only correct after the packfile has been written. + * + * @deprecated use git_packbuilder_name + * @param pb The packbuilder object + * @return 0 or an error code + */ +GIT_EXTERN(const git_oid *) git_packbuilder_hash(git_packbuilder *pb); +#endif + +/** + * Get the unique name for the resulting packfile. + * + * The packfile's name is derived from the packfile's content. + * This is only correct after the packfile has been written. + * + * @param pb the packbuilder instance + * @return a NUL terminated string for the packfile name + */ +GIT_EXTERN(const char *) git_packbuilder_name(git_packbuilder *pb); + +/** + * Callback used to iterate over packed objects + * + * @see git_packbuilder_foreach + * + * @param buf A pointer to the object's data + * @param size The size of the underlying object + * @param payload Payload passed to git_packbuilder_foreach + * @return non-zero to terminate the iteration + */ +typedef int GIT_CALLBACK(git_packbuilder_foreach_cb)(void *buf, size_t size, void *payload); /** * Create the new pack and pass each object to the callback @@ -86,24 +215,56 @@ GIT_EXTERN(int) git_packbuilder_write(git_packbuilder *pb, const char *file); * @param payload the callback's data * @return 0 or an error code */ -typedef int (*git_packbuilder_foreach_cb)(void *buf, size_t size, void *payload); GIT_EXTERN(int) git_packbuilder_foreach(git_packbuilder *pb, git_packbuilder_foreach_cb cb, void *payload); /** * Get the total number of objects the packbuilder will write out * * @param pb the packbuilder - * @return + * @return the number of objects in the packfile */ -GIT_EXTERN(uint32_t) git_packbuilder_object_count(git_packbuilder *pb); +GIT_EXTERN(size_t) git_packbuilder_object_count(git_packbuilder *pb); /** * Get the number of objects the packbuilder has already written out * * @param pb the packbuilder - * @return + * @return the number of objects which have already been written */ -GIT_EXTERN(uint32_t) git_packbuilder_written(git_packbuilder *pb); +GIT_EXTERN(size_t) git_packbuilder_written(git_packbuilder *pb); + +/** + * Packbuilder progress notification function. + * + * @param stage the stage of the packbuilder + * @param current the current object + * @param total the total number of objects + * @param payload the callback payload + * @return 0 on success or an error code + */ +typedef int GIT_CALLBACK(git_packbuilder_progress)( + int stage, + uint32_t current, + uint32_t total, + void *payload); + +/** + * Set the callbacks for a packbuilder + * + * @param pb The packbuilder object + * @param progress_cb Function to call with progress information during + * pack building. Be aware that this is called inline with pack building + * operations, so performance may be affected. + * When progress_cb returns an error, the pack building process will be + * aborted and the error will be returned from the invoked function. + * `pb` must then be freed. + * @param progress_cb_payload Payload for progress callback. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_packbuilder_set_callbacks( + git_packbuilder *pb, + git_packbuilder_progress progress_cb, + void *progress_cb_payload); /** * Free the packbuilder and all associated data @@ -114,4 +275,5 @@ GIT_EXTERN(void) git_packbuilder_free(git_packbuilder *pb); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/patch.h b/include/git2/patch.h new file mode 100644 index 00000000000..782482158be --- /dev/null +++ b/include/git2/patch.h @@ -0,0 +1,289 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_patch_h__ +#define INCLUDE_git_patch_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" +#include "diff.h" + +/** + * @file git2/patch.h + * @brief Patches store the textual diffs in a delta + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * The diff patch is used to store all the text diffs for a delta. + * + * You can easily loop over the content of patches and get information about + * them. + */ +typedef struct git_patch git_patch; + +/** + * Get the repository associated with this patch. May be NULL. + * + * @param patch the patch + * @return a pointer to the repository + */ +GIT_EXTERN(git_repository *) git_patch_owner(const git_patch *patch); + +/** + * Return a patch for an entry in the diff list. + * + * The `git_patch` is a newly created object contains the text diffs + * for the delta. You have to call `git_patch_free()` when you are + * done with it. You can use the patch object to loop over all the hunks + * and lines in the diff of the one delta. + * + * For an unchanged file or a binary file, no `git_patch` will be + * created, the output will be set to NULL, and the `binary` flag will be + * set true in the `git_diff_delta` structure. + * + * It is okay to pass NULL for either of the output parameters; if you pass + * NULL for the `git_patch`, then the text diff will not be calculated. + * + * @param out Output parameter for the delta patch object + * @param diff Diff list object + * @param idx Index into diff list + * @return 0 on success, other value < 0 on error + */ +GIT_EXTERN(int) git_patch_from_diff( + git_patch **out, git_diff *diff, size_t idx); + +/** + * Directly generate a patch from the difference between two blobs. + * + * This is just like `git_diff_blobs()` except it generates a patch object + * for the difference instead of directly making callbacks. You can use the + * standard `git_patch` accessor functions to read the patch data, and + * you must call `git_patch_free()` on the patch when done. + * + * @param out The generated patch; NULL on error + * @param old_blob Blob for old side of diff, or NULL for empty blob + * @param old_as_path Treat old blob as if it had this filename; can be NULL + * @param new_blob Blob for new side of diff, or NULL for empty blob + * @param new_as_path Treat new blob as if it had this filename; can be NULL + * @param opts Options for diff, or NULL for default options + * @return 0 on success or error code < 0 + */ +GIT_EXTERN(int) git_patch_from_blobs( + git_patch **out, + const git_blob *old_blob, + const char *old_as_path, + const git_blob *new_blob, + const char *new_as_path, + const git_diff_options *opts); + +/** + * Directly generate a patch from the difference between a blob and a buffer. + * + * This is just like `git_diff_blob_to_buffer()` except it generates a patch + * object for the difference instead of directly making callbacks. You can + * use the standard `git_patch` accessor functions to read the patch + * data, and you must call `git_patch_free()` on the patch when done. + * + * @param out The generated patch; NULL on error + * @param old_blob Blob for old side of diff, or NULL for empty blob + * @param old_as_path Treat old blob as if it had this filename; can be NULL + * @param buffer Raw data for new side of diff, or NULL for empty + * @param buffer_len Length of raw data for new side of diff + * @param buffer_as_path Treat buffer as if it had this filename; can be NULL + * @param opts Options for diff, or NULL for default options + * @return 0 on success or error code < 0 + */ +GIT_EXTERN(int) git_patch_from_blob_and_buffer( + git_patch **out, + const git_blob *old_blob, + const char *old_as_path, + const void *buffer, + size_t buffer_len, + const char *buffer_as_path, + const git_diff_options *opts); + +/** + * Directly generate a patch from the difference between two buffers. + * + * This is just like `git_diff_buffers()` except it generates a patch + * object for the difference instead of directly making callbacks. You can + * use the standard `git_patch` accessor functions to read the patch + * data, and you must call `git_patch_free()` on the patch when done. + * + * @param out The generated patch; NULL on error + * @param old_buffer Raw data for old side of diff, or NULL for empty + * @param old_len Length of the raw data for old side of the diff + * @param old_as_path Treat old buffer as if it had this filename; can be NULL + * @param new_buffer Raw data for new side of diff, or NULL for empty + * @param new_len Length of raw data for new side of diff + * @param new_as_path Treat buffer as if it had this filename; can be NULL + * @param opts Options for diff, or NULL for default options + * @return 0 on success or error code < 0 + */ +GIT_EXTERN(int) git_patch_from_buffers( + git_patch **out, + const void *old_buffer, + size_t old_len, + const char *old_as_path, + const void *new_buffer, + size_t new_len, + const char *new_as_path, + const git_diff_options *opts); + +/** + * Free a git_patch object. + * + * @param patch The patch to free. + */ +GIT_EXTERN(void) git_patch_free(git_patch *patch); + +/** + * Get the delta associated with a patch. This delta points to internal + * data and you do not have to release it when you are done with it. + * + * @param patch The patch in which to get the delta. + * @return The delta associated with the patch. + */ +GIT_EXTERN(const git_diff_delta *) git_patch_get_delta(const git_patch *patch); + +/** + * Get the number of hunks in a patch + * + * @param patch The patch in which to get the number of hunks. + * @return The number of hunks of the patch. + */ +GIT_EXTERN(size_t) git_patch_num_hunks(const git_patch *patch); + +/** + * Get line counts of each type in a patch. + * + * This helps imitate a diff --numstat type of output. For that purpose, + * you only need the `total_additions` and `total_deletions` values, but we + * include the `total_context` line count in case you want the total number + * of lines of diff output that will be generated. + * + * All outputs are optional. Pass NULL if you don't need a particular count. + * + * @param total_context Count of context lines in output, can be NULL. + * @param total_additions Count of addition lines in output, can be NULL. + * @param total_deletions Count of deletion lines in output, can be NULL. + * @param patch The git_patch object + * @return 0 on success, <0 on error + */ +GIT_EXTERN(int) git_patch_line_stats( + size_t *total_context, + size_t *total_additions, + size_t *total_deletions, + const git_patch *patch); + +/** + * Get the information about a hunk in a patch + * + * Given a patch and a hunk index into the patch, this returns detailed + * information about that hunk. Any of the output pointers can be passed + * as NULL if you don't care about that particular piece of information. + * + * @param out Output pointer to git_diff_hunk of hunk + * @param lines_in_hunk Output count of total lines in this hunk + * @param patch Input pointer to patch object + * @param hunk_idx Input index of hunk to get information about + * @return 0 on success, GIT_ENOTFOUND if hunk_idx out of range, <0 on error + */ +GIT_EXTERN(int) git_patch_get_hunk( + const git_diff_hunk **out, + size_t *lines_in_hunk, + git_patch *patch, + size_t hunk_idx); + +/** + * Get the number of lines in a hunk. + * + * @param patch The git_patch object + * @param hunk_idx Index of the hunk + * @return Number of lines in hunk or GIT_ENOTFOUND if invalid hunk index + */ +GIT_EXTERN(int) git_patch_num_lines_in_hunk( + const git_patch *patch, + size_t hunk_idx); + +/** + * Get data about a line in a hunk of a patch. + * + * Given a patch, a hunk index, and a line index in the hunk, this + * will return a lot of details about that line. If you pass a hunk + * index larger than the number of hunks or a line index larger than + * the number of lines in the hunk, this will return -1. + * + * @param out The git_diff_line data for this line + * @param patch The patch to look in + * @param hunk_idx The index of the hunk + * @param line_of_hunk The index of the line in the hunk + * @return 0 on success, <0 on failure + */ +GIT_EXTERN(int) git_patch_get_line_in_hunk( + const git_diff_line **out, + git_patch *patch, + size_t hunk_idx, + size_t line_of_hunk); + +/** + * Look up size of patch diff data in bytes + * + * This returns the raw size of the patch data. This only includes the + * actual data from the lines of the diff, not the file or hunk headers. + * + * If you pass `include_context` as true (non-zero), this will be the size + * of all of the diff output; if you pass it as false (zero), this will + * only include the actual changed lines (as if `context_lines` was 0). + * + * @param patch A git_patch representing changes to one file + * @param include_context Include context lines in size if non-zero + * @param include_hunk_headers Include hunk header lines if non-zero + * @param include_file_headers Include file header lines if non-zero + * @return The number of bytes of data + */ +GIT_EXTERN(size_t) git_patch_size( + git_patch *patch, + int include_context, + int include_hunk_headers, + int include_file_headers); + +/** + * Serialize the patch to text via callback. + * + * Returning a non-zero value from the callback will terminate the iteration + * and return that value to the caller. + * + * @param patch A git_patch representing changes to one file + * @param print_cb Callback function to output lines of the patch. Will be + * called for file headers, hunk headers, and diff lines. + * @param payload Reference pointer that will be passed to your callbacks. + * @return 0 on success, non-zero callback return value, or error code + */ +GIT_EXTERN(int) git_patch_print( + git_patch *patch, + git_diff_line_cb print_cb, + void *payload); + +/** + * Get the content of a patch as a single diff text. + * + * @param out The git_buf to be filled in + * @param patch A git_patch representing changes to one file + * @return 0 on success, <0 on failure. + */ +GIT_EXTERN(int) git_patch_to_buf( + git_buf *out, + git_patch *patch); + +/**@}*/ +GIT_END_DECL + +#endif diff --git a/include/git2/pathspec.h b/include/git2/pathspec.h new file mode 100644 index 00000000000..6f6918cdb9d --- /dev/null +++ b/include/git2/pathspec.h @@ -0,0 +1,289 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_pathspec_h__ +#define INCLUDE_git_pathspec_h__ + +#include "common.h" +#include "types.h" +#include "strarray.h" +#include "diff.h" + +/** + * @file git2/pathspec.h + * @brief Specifiers for path matching + * @defgroup git_pathspec Specifiers for path matching + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Compiled pathspec + */ +typedef struct git_pathspec git_pathspec; + +/** + * List of filenames matching a pathspec + */ +typedef struct git_pathspec_match_list git_pathspec_match_list; + +/** + * Options controlling how pathspec match should be executed + */ +typedef enum { + GIT_PATHSPEC_DEFAULT = 0, + + /** + * GIT_PATHSPEC_IGNORE_CASE forces match to ignore case; otherwise + * match will use native case sensitivity of platform filesystem + */ + GIT_PATHSPEC_IGNORE_CASE = (1u << 0), + + /** + * GIT_PATHSPEC_USE_CASE forces case sensitive match; otherwise + * match will use native case sensitivity of platform filesystem + */ + GIT_PATHSPEC_USE_CASE = (1u << 1), + + /** + * GIT_PATHSPEC_NO_GLOB disables glob patterns and just uses simple + * string comparison for matching + */ + GIT_PATHSPEC_NO_GLOB = (1u << 2), + + /** + * GIT_PATHSPEC_NO_MATCH_ERROR means the match functions return error + * code GIT_ENOTFOUND if no matches are found; otherwise no matches is + * still success (return 0) but `git_pathspec_match_list_entrycount` + * will indicate 0 matches. + */ + GIT_PATHSPEC_NO_MATCH_ERROR = (1u << 3), + + /** + * GIT_PATHSPEC_FIND_FAILURES means that the `git_pathspec_match_list` + * should track which patterns matched which files so that at the end of + * the match we can identify patterns that did not match any files. + */ + GIT_PATHSPEC_FIND_FAILURES = (1u << 4), + + /** + * GIT_PATHSPEC_FAILURES_ONLY means that the `git_pathspec_match_list` + * does not need to keep the actual matching filenames. Use this to + * just test if there were any matches at all or in combination with + * GIT_PATHSPEC_FIND_FAILURES to validate a pathspec. + */ + GIT_PATHSPEC_FAILURES_ONLY = (1u << 5) +} git_pathspec_flag_t; + +/** + * Compile a pathspec + * + * @param out Output of the compiled pathspec + * @param pathspec A git_strarray of the paths to match + * @return 0 on success, <0 on failure + */ +GIT_EXTERN(int) git_pathspec_new( + git_pathspec **out, const git_strarray *pathspec); + +/** + * Free a pathspec + * + * @param ps The compiled pathspec + */ +GIT_EXTERN(void) git_pathspec_free(git_pathspec *ps); + +/** + * Try to match a path against a pathspec + * + * Unlike most of the other pathspec matching functions, this will not + * fall back on the native case-sensitivity for your platform. You must + * explicitly pass flags to control case sensitivity or else this will + * fall back on being case sensitive. + * + * @param ps The compiled pathspec + * @param flags Combination of git_pathspec_flag_t options to control match + * @param path The pathname to attempt to match + * @return 1 is path matches spec, 0 if it does not + */ +GIT_EXTERN(int) git_pathspec_matches_path( + const git_pathspec *ps, uint32_t flags, const char *path); + +/** + * Match a pathspec against the working directory of a repository. + * + * This matches the pathspec against the current files in the working + * directory of the repository. It is an error to invoke this on a bare + * repo. This handles git ignores (i.e. ignored files will not be + * considered to match the `pathspec` unless the file is tracked in the + * index). + * + * If `out` is not NULL, this returns a `git_patchspec_match_list`. That + * contains the list of all matched filenames (unless you pass the + * `GIT_PATHSPEC_FAILURES_ONLY` flag) and may also contain the list of + * pathspecs with no match (if you used the `GIT_PATHSPEC_FIND_FAILURES` + * flag). You must call `git_pathspec_match_list_free()` on this object. + * + * @param out Output list of matches; pass NULL to just get return value + * @param repo The repository in which to match; bare repo is an error + * @param flags Combination of git_pathspec_flag_t options to control match + * @param ps Pathspec to be matched + * @return 0 on success, -1 on error, GIT_ENOTFOUND if no matches and + * the GIT_PATHSPEC_NO_MATCH_ERROR flag was given + */ +GIT_EXTERN(int) git_pathspec_match_workdir( + git_pathspec_match_list **out, + git_repository *repo, + uint32_t flags, + git_pathspec *ps); + +/** + * Match a pathspec against entries in an index. + * + * This matches the pathspec against the files in the repository index. + * + * NOTE: At the moment, the case sensitivity of this match is controlled + * by the current case-sensitivity of the index object itself and the + * USE_CASE and IGNORE_CASE flags will have no effect. This behavior will + * be corrected in a future release. + * + * If `out` is not NULL, this returns a `git_patchspec_match_list`. That + * contains the list of all matched filenames (unless you pass the + * `GIT_PATHSPEC_FAILURES_ONLY` flag) and may also contain the list of + * pathspecs with no match (if you used the `GIT_PATHSPEC_FIND_FAILURES` + * flag). You must call `git_pathspec_match_list_free()` on this object. + * + * @param out Output list of matches; pass NULL to just get return value + * @param index The index to match against + * @param flags Combination of git_pathspec_flag_t options to control match + * @param ps Pathspec to be matched + * @return 0 on success, -1 on error, GIT_ENOTFOUND if no matches and + * the GIT_PATHSPEC_NO_MATCH_ERROR flag is used + */ +GIT_EXTERN(int) git_pathspec_match_index( + git_pathspec_match_list **out, + git_index *index, + uint32_t flags, + git_pathspec *ps); + +/** + * Match a pathspec against files in a tree. + * + * This matches the pathspec against the files in the given tree. + * + * If `out` is not NULL, this returns a `git_patchspec_match_list`. That + * contains the list of all matched filenames (unless you pass the + * `GIT_PATHSPEC_FAILURES_ONLY` flag) and may also contain the list of + * pathspecs with no match (if you used the `GIT_PATHSPEC_FIND_FAILURES` + * flag). You must call `git_pathspec_match_list_free()` on this object. + * + * @param out Output list of matches; pass NULL to just get return value + * @param tree The root-level tree to match against + * @param flags Combination of git_pathspec_flag_t options to control match + * @param ps Pathspec to be matched + * @return 0 on success, -1 on error, GIT_ENOTFOUND if no matches and + * the GIT_PATHSPEC_NO_MATCH_ERROR flag is used + */ +GIT_EXTERN(int) git_pathspec_match_tree( + git_pathspec_match_list **out, + git_tree *tree, + uint32_t flags, + git_pathspec *ps); + +/** + * Match a pathspec against files in a diff list. + * + * This matches the pathspec against the files in the given diff list. + * + * If `out` is not NULL, this returns a `git_patchspec_match_list`. That + * contains the list of all matched filenames (unless you pass the + * `GIT_PATHSPEC_FAILURES_ONLY` flag) and may also contain the list of + * pathspecs with no match (if you used the `GIT_PATHSPEC_FIND_FAILURES` + * flag). You must call `git_pathspec_match_list_free()` on this object. + * + * @param out Output list of matches; pass NULL to just get return value + * @param diff A generated diff list + * @param flags Combination of git_pathspec_flag_t options to control match + * @param ps Pathspec to be matched + * @return 0 on success, -1 on error, GIT_ENOTFOUND if no matches and + * the GIT_PATHSPEC_NO_MATCH_ERROR flag is used + */ +GIT_EXTERN(int) git_pathspec_match_diff( + git_pathspec_match_list **out, + git_diff *diff, + uint32_t flags, + git_pathspec *ps); + +/** + * Free memory associates with a git_pathspec_match_list + * + * @param m The git_pathspec_match_list to be freed + */ +GIT_EXTERN(void) git_pathspec_match_list_free(git_pathspec_match_list *m); + +/** + * Get the number of items in a match list. + * + * @param m The git_pathspec_match_list object + * @return Number of items in match list + */ +GIT_EXTERN(size_t) git_pathspec_match_list_entrycount( + const git_pathspec_match_list *m); + +/** + * Get a matching filename by position. + * + * This routine cannot be used if the match list was generated by + * `git_pathspec_match_diff`. If so, it will always return NULL. + * + * @param m The git_pathspec_match_list object + * @param pos The index into the list + * @return The filename of the match + */ +GIT_EXTERN(const char *) git_pathspec_match_list_entry( + const git_pathspec_match_list *m, size_t pos); + +/** + * Get a matching diff delta by position. + * + * This routine can only be used if the match list was generated by + * `git_pathspec_match_diff`. Otherwise it will always return NULL. + * + * @param m The git_pathspec_match_list object + * @param pos The index into the list + * @return The filename of the match + */ +GIT_EXTERN(const git_diff_delta *) git_pathspec_match_list_diff_entry( + const git_pathspec_match_list *m, size_t pos); + +/** + * Get the number of pathspec items that did not match. + * + * This will be zero unless you passed GIT_PATHSPEC_FIND_FAILURES when + * generating the git_pathspec_match_list. + * + * @param m The git_pathspec_match_list object + * @return Number of items in original pathspec that had no matches + */ +GIT_EXTERN(size_t) git_pathspec_match_list_failed_entrycount( + const git_pathspec_match_list *m); + +/** + * Get an original pathspec string that had no matches. + * + * This will be return NULL for positions out of range. + * + * @param m The git_pathspec_match_list object + * @param pos The index into the failed items + * @return The pathspec pattern that didn't match anything + */ +GIT_EXTERN(const char *) git_pathspec_match_list_failed_entry( + const git_pathspec_match_list *m, size_t pos); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/proxy.h b/include/git2/proxy.h new file mode 100644 index 00000000000..fc8ffaaa8ee --- /dev/null +++ b/include/git2/proxy.h @@ -0,0 +1,107 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_proxy_h__ +#define INCLUDE_git_proxy_h__ + +#include "common.h" + +#include "cert.h" +#include "credential.h" + +/** + * @file git2/proxy.h + * @brief TLS proxies + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * The type of proxy to use. + */ +typedef enum { + /** + * Do not attempt to connect through a proxy + * + * If built against libcurl, it itself may attempt to connect + * to a proxy if the environment variables specify it. + */ + GIT_PROXY_NONE, + /** + * Try to auto-detect the proxy from the git configuration. + */ + GIT_PROXY_AUTO, + /** + * Connect via the URL given in the options + */ + GIT_PROXY_SPECIFIED +} git_proxy_t; + +/** + * Options for connecting through a proxy + * + * Note that not all types may be supported, depending on the platform + * and compilation options. + */ +typedef struct { + unsigned int version; + + /** + * The type of proxy to use, by URL, auto-detect. + */ + git_proxy_t type; + + /** + * The URL of the proxy. + */ + const char *url; + + /** + * This will be called if the remote host requires + * authentication in order to connect to it. + * + * Returning GIT_PASSTHROUGH will make libgit2 behave as + * though this field isn't set. + */ + git_credential_acquire_cb credentials; + + /** + * This will be called to let the user make the final decision of whether + * to allow the connection to proceed. Returns 0 to allow the connection + * or a negative value to indicate an error. + */ + git_transport_certificate_check_cb certificate_check; + + /** + * Payload to be provided to the credentials and certificate + * check callbacks. + */ + void *payload; +} git_proxy_options; + +/** Current version for the `git_proxy_options` structure */ +#define GIT_PROXY_OPTIONS_VERSION 1 + +/** Static constructor for `git_proxy_options` */ +#define GIT_PROXY_OPTIONS_INIT {GIT_PROXY_OPTIONS_VERSION} + +/** + * Initialize git_proxy_options structure + * + * Initializes a `git_proxy_options` with default values. Equivalent to + * creating an instance with `GIT_PROXY_OPTIONS_INIT`. + * + * @param opts The `git_proxy_options` struct to initialize. + * @param version The struct version; pass `GIT_PROXY_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_proxy_options_init(git_proxy_options *opts, unsigned int version); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/push.h b/include/git2/push.h deleted file mode 100644 index 6e07f368ee5..00000000000 --- a/include/git2/push.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_git_push_h__ -#define INCLUDE_git_push_h__ - -#include "common.h" - -/** - * @file git2/push.h - * @brief Git push management functions - * @defgroup git_push push management functions - * @ingroup Git - * @{ - */ -GIT_BEGIN_DECL - -/** - * Create a new push object - * - * @param out New push object - * @param remote Remote instance - * - * @return 0 or an error code - */ -GIT_EXTERN(int) git_push_new(git_push **out, git_remote *remote); - -/** - * Add a refspec to be pushed - * - * @param push The push object - * @param refspec Refspec string - * - * @return 0 or an error code - */ -GIT_EXTERN(int) git_push_add_refspec(git_push *push, const char *refspec); - -/** - * Update remote tips after a push - * - * @param push The push object - * - * @return 0 or an error code - */ -GIT_EXTERN(int) git_push_update_tips(git_push *push); - -/** - * Actually push all given refspecs - * - * @param push The push object - * - * @return 0 or an error code - */ -GIT_EXTERN(int) git_push_finish(git_push *push); - -/** - * Check if remote side successfully unpacked - * - * @param push The push object - * - * @return true if equal, false otherwise - */ -GIT_EXTERN(int) git_push_unpack_ok(git_push *push); - -/** - * Call callback `cb' on each status - * - * @param push The push object - * @param cb The callback to call on each object - * - * @return 0 on success, GIT_EUSER on non-zero callback, or error code - */ -GIT_EXTERN(int) git_push_status_foreach(git_push *push, - int (*cb)(const char *ref, const char *msg, void *data), - void *data); - -/** - * Free the given push object - * - * @param push The push object - */ -GIT_EXTERN(void) git_push_free(git_push *push); - -/** @} */ -GIT_END_DECL -#endif diff --git a/include/git2/rebase.h b/include/git2/rebase.h new file mode 100644 index 00000000000..3fb3e5733a2 --- /dev/null +++ b/include/git2/rebase.h @@ -0,0 +1,402 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_rebase_h__ +#define INCLUDE_git_rebase_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" +#include "annotated_commit.h" +#include "merge.h" +#include "checkout.h" +#include "commit.h" + +/** + * @file git2/rebase.h + * @brief Rebase manipulates commits, placing them on a new parent + * @defgroup git_rebase Rebase manipulates commits, placing them on a new parent + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Rebase options + * + * Use to tell the rebase machinery how to operate. + */ +typedef struct { + unsigned int version; + + /** + * Used by `git_rebase_init`, this will instruct other clients working + * on this rebase that you want a quiet rebase experience, which they + * may choose to provide in an application-specific manner. This has no + * effect upon libgit2 directly, but is provided for interoperability + * between Git tools. + */ + int quiet; + + /** + * Used by `git_rebase_init`, this will begin an in-memory rebase, + * which will allow callers to step through the rebase operations and + * commit the rebased changes, but will not rewind HEAD or update the + * repository to be in a rebasing state. This will not interfere with + * the working directory (if there is one). + */ + int inmemory; + + /** + * Used by `git_rebase_finish`, this is the name of the notes reference + * used to rewrite notes for rebased commits when finishing the rebase; + * if NULL, the contents of the configuration option `notes.rewriteRef` + * is examined, unless the configuration option `notes.rewrite.rebase` + * is set to false. If `notes.rewriteRef` is also NULL, notes will + * not be rewritten. + */ + const char *rewrite_notes_ref; + + /** + * Options to control how trees are merged during `git_rebase_next`. + */ + git_merge_options merge_options; + + /** + * Options to control how files are written during `git_rebase_init`, + * `git_rebase_next` and `git_rebase_abort`. Note that during + * `abort`, these options will add an implied `GIT_CHECKOUT_FORCE` + * to match git semantics. + */ + git_checkout_options checkout_options; + + /** + * Optional callback that allows users to override commit + * creation in `git_rebase_commit`. If specified, users can + * create their own commit and provide the commit ID, which + * may be useful for signing commits or otherwise customizing + * the commit creation. + * + * If this callback returns `GIT_PASSTHROUGH`, then + * `git_rebase_commit` will continue to create the commit. + */ + git_commit_create_cb commit_create_cb; + +#ifdef GIT_DEPRECATE_HARD + void *reserved; +#else + /** + * If provided, this will be called with the commit content, allowing + * a signature to be added to the rebase commit. Can be skipped with + * GIT_PASSTHROUGH. If GIT_PASSTHROUGH is returned, a commit will be made + * without a signature. + * + * This field is only used when performing git_rebase_commit. + * + * This callback is not invoked if a `git_commit_create_cb` is + * specified. + * + * This callback is deprecated; users should provide a + * creation callback as `commit_create_cb` that produces a + * commit buffer, signs it, and commits it. + */ + int (*signing_cb)(git_buf *, git_buf *, const char *, void *); +#endif + + /** + * This will be passed to each of the callbacks in this struct + * as the last parameter. + */ + void *payload; +} git_rebase_options; + +/** + * Type of rebase operation in-progress after calling `git_rebase_next`. + */ +typedef enum { + /** + * The given commit is to be cherry-picked. The client should commit + * the changes and continue if there are no conflicts. + */ + GIT_REBASE_OPERATION_PICK = 0, + + /** + * The given commit is to be cherry-picked, but the client should prompt + * the user to provide an updated commit message. + */ + GIT_REBASE_OPERATION_REWORD, + + /** + * The given commit is to be cherry-picked, but the client should stop + * to allow the user to edit the changes before committing them. + */ + GIT_REBASE_OPERATION_EDIT, + + /** + * The given commit is to be squashed into the previous commit. The + * commit message will be merged with the previous message. + */ + GIT_REBASE_OPERATION_SQUASH, + + /** + * The given commit is to be squashed into the previous commit. The + * commit message from this commit will be discarded. + */ + GIT_REBASE_OPERATION_FIXUP, + + /** + * No commit will be cherry-picked. The client should run the given + * command and (if successful) continue. + */ + GIT_REBASE_OPERATION_EXEC +} git_rebase_operation_t; + +/** Current version for the `git_rebase_options` structure */ +#define GIT_REBASE_OPTIONS_VERSION 1 + +/** Static constructor for `git_rebase_options` */ +#define GIT_REBASE_OPTIONS_INIT \ + { GIT_REBASE_OPTIONS_VERSION, 0, 0, NULL, GIT_MERGE_OPTIONS_INIT, \ + GIT_CHECKOUT_OPTIONS_INIT, NULL, NULL } + +/** Indicates that a rebase operation is not (yet) in progress. */ +#define GIT_REBASE_NO_OPERATION SIZE_MAX + +/** + * A rebase operation + * + * Describes a single instruction/operation to be performed during the + * rebase. + */ +typedef struct { + /** The type of rebase operation. */ + git_rebase_operation_t type; + + /** + * The commit ID being cherry-picked. This will be populated for + * all operations except those of type `GIT_REBASE_OPERATION_EXEC`. + */ + const git_oid id; + + /** + * The executable the user has requested be run. This will only + * be populated for operations of type `GIT_REBASE_OPERATION_EXEC`. + */ + const char *exec; +} git_rebase_operation; + +/** + * Initialize git_rebase_options structure + * + * Initializes a `git_rebase_options` with default values. Equivalent to + * creating an instance with `GIT_REBASE_OPTIONS_INIT`. + * + * @param opts The `git_rebase_options` struct to initialize. + * @param version The struct version; pass `GIT_REBASE_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_rebase_options_init( + git_rebase_options *opts, + unsigned int version); + +/** + * Initializes a rebase operation to rebase the changes in `branch` + * relative to `upstream` onto another branch. To begin the rebase + * process, call `git_rebase_next`. When you have finished with this + * object, call `git_rebase_free`. + * + * @param out Pointer to store the rebase object + * @param repo The repository to perform the rebase + * @param branch The terminal commit to rebase, or NULL to rebase the + * current branch + * @param upstream The commit to begin rebasing from, or NULL to rebase all + * reachable commits + * @param onto The branch to rebase onto, or NULL to rebase onto the given + * upstream + * @param opts Options to specify how rebase is performed, or NULL + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_rebase_init( + git_rebase **out, + git_repository *repo, + const git_annotated_commit *branch, + const git_annotated_commit *upstream, + const git_annotated_commit *onto, + const git_rebase_options *opts); + +/** + * Opens an existing rebase that was previously started by either an + * invocation of `git_rebase_init` or by another client. + * + * @param out Pointer to store the rebase object + * @param repo The repository that has a rebase in-progress + * @param opts Options to specify how rebase is performed + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_rebase_open( + git_rebase **out, + git_repository *repo, + const git_rebase_options *opts); + +/** + * Gets the original `HEAD` ref name for merge rebases. + * + * @param rebase The in-progress rebase. + * @return The original `HEAD` ref name + */ +GIT_EXTERN(const char *) git_rebase_orig_head_name(git_rebase *rebase); + +/** + * Gets the original `HEAD` id for merge rebases. + * + * @param rebase The in-progress rebase. + * @return The original `HEAD` id + */ +GIT_EXTERN(const git_oid *) git_rebase_orig_head_id(git_rebase *rebase); + +/** + * Gets the `onto` ref name for merge rebases. + * + * @param rebase The in-progress rebase. + * @return The `onto` ref name + */ +GIT_EXTERN(const char *) git_rebase_onto_name(git_rebase *rebase); + +/** + * Gets the `onto` id for merge rebases. + * + * @param rebase The in-progress rebase. + * @return The `onto` id + */ +GIT_EXTERN(const git_oid *) git_rebase_onto_id(git_rebase *rebase); + +/** + * Gets the count of rebase operations that are to be applied. + * + * @param rebase The in-progress rebase + * @return The number of rebase operations in total + */ +GIT_EXTERN(size_t) git_rebase_operation_entrycount(git_rebase *rebase); + +/** + * Gets the index of the rebase operation that is currently being applied. + * If the first operation has not yet been applied (because you have + * called `init` but not yet `next`) then this returns + * `GIT_REBASE_NO_OPERATION`. + * + * @param rebase The in-progress rebase + * @return The index of the rebase operation currently being applied. + */ +GIT_EXTERN(size_t) git_rebase_operation_current(git_rebase *rebase); + +/** + * Gets the rebase operation specified by the given index. + * + * @param rebase The in-progress rebase + * @param idx The index of the rebase operation to retrieve + * @return The rebase operation or NULL if `idx` was out of bounds + */ +GIT_EXTERN(git_rebase_operation *) git_rebase_operation_byindex( + git_rebase *rebase, + size_t idx); + +/** + * Performs the next rebase operation and returns the information about it. + * If the operation is one that applies a patch (which is any operation except + * GIT_REBASE_OPERATION_EXEC) then the patch will be applied and the index and + * working directory will be updated with the changes. If there are conflicts, + * you will need to address those before committing the changes. + * + * @param operation Pointer to store the rebase operation that is to be performed next + * @param rebase The rebase in progress + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_rebase_next( + git_rebase_operation **operation, + git_rebase *rebase); + +/** + * Gets the index produced by the last operation, which is the result + * of `git_rebase_next` and which will be committed by the next + * invocation of `git_rebase_commit`. This is useful for resolving + * conflicts in an in-memory rebase before committing them. You must + * call `git_index_free` when you are finished with this. + * + * This is only applicable for in-memory rebases; for rebases within + * a working directory, the changes were applied to the repository's + * index. + * + * @param index The result index of the last operation. + * @param rebase The in-progress rebase. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_rebase_inmemory_index( + git_index **index, + git_rebase *rebase); + +/** + * Commits the current patch. You must have resolved any conflicts that + * were introduced during the patch application from the `git_rebase_next` + * invocation. + * + * @param id Pointer in which to store the OID of the newly created commit + * @param rebase The rebase that is in-progress + * @param author The author of the updated commit, or NULL to keep the + * author from the original commit + * @param committer The committer of the rebase + * @param message_encoding The encoding for the message in the commit, + * represented with a standard encoding name. If message is NULL, + * this should also be NULL, and the encoding from the original + * commit will be maintained. If message is specified, this may be + * NULL to indicate that "UTF-8" is to be used. + * @param message The message for this commit, or NULL to use the message + * from the original commit. + * @return Zero on success, GIT_EUNMERGED if there are unmerged changes in + * the index, GIT_EAPPLIED if the current commit has already + * been applied to the upstream and there is nothing to commit, + * -1 on failure. + */ +GIT_EXTERN(int) git_rebase_commit( + git_oid *id, + git_rebase *rebase, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message); + +/** + * Aborts a rebase that is currently in progress, resetting the repository + * and working directory to their state before rebase began. + * + * @param rebase The rebase that is in-progress + * @return Zero on success; GIT_ENOTFOUND if a rebase is not in progress, + * -1 on other errors. + */ +GIT_EXTERN(int) git_rebase_abort(git_rebase *rebase); + +/** + * Finishes a rebase that is currently in progress once all patches have + * been applied. + * + * @param rebase The rebase that is in-progress + * @param signature The identity that is finishing the rebase (optional) + * @return Zero on success; -1 on error + */ +GIT_EXTERN(int) git_rebase_finish( + git_rebase *rebase, + const git_signature *signature); + +/** + * Frees the `git_rebase` object. + * + * @param rebase The rebase object + */ +GIT_EXTERN(void) git_rebase_free(git_rebase *rebase); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/refdb.h b/include/git2/refdb.h new file mode 100644 index 00000000000..a896bdd079c --- /dev/null +++ b/include/git2/refdb.h @@ -0,0 +1,76 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_refdb_h__ +#define INCLUDE_git_refdb_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" +#include "refs.h" + +/** + * @file git2/refdb.h + * @brief A database for references (branches and tags) + * @defgroup git_refdb A database for references (branches and tags) + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** The type of the refdb as determined by "extensions.refStorage". */ +typedef enum { + GIT_REFDB_FILES = 1 /**< Files backend using loose and packed refs. */ +} git_refdb_t; + +/** + * Create a new reference database with no backends. + * + * Before the Ref DB can be used for read/writing, a custom database + * backend must be manually set using `git_refdb_set_backend()` + * + * @param out location to store the database pointer, if opened. + * Set to NULL if the open failed. + * @param repo the repository + * @return 0 or an error code + */ +GIT_EXTERN(int) git_refdb_new(git_refdb **out, git_repository *repo); + +/** + * Create a new reference database and automatically add + * the default backends: + * + * - git_refdb_dir: read and write loose and packed refs + * from disk, assuming the repository dir as the folder + * + * @param out location to store the database pointer, if opened. + * Set to NULL if the open failed. + * @param repo the repository + * @return 0 or an error code + */ +GIT_EXTERN(int) git_refdb_open(git_refdb **out, git_repository *repo); + +/** + * Suggests that the given refdb compress or optimize its references. + * This mechanism is implementation specific. For on-disk reference + * databases, for example, this may pack all loose references. + * + * @param refdb The reference database to optimize. + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_refdb_compress(git_refdb *refdb); + +/** + * Close an open reference database. + * + * @param refdb reference database pointer or NULL + */ +GIT_EXTERN(void) git_refdb_free(git_refdb *refdb); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/reflog.h b/include/git2/reflog.h index 4944530af34..a0956c63a6a 100644 --- a/include/git2/reflog.h +++ b/include/git2/reflog.h @@ -13,8 +13,8 @@ /** * @file git2/reflog.h - * @brief Git reflog management routines - * @defgroup git_reflog Git reflog management routines + * @brief Reference logs store how references change + * @defgroup git_reflog Reference logs store how references change * @ingroup Git * @{ */ @@ -31,10 +31,11 @@ GIT_BEGIN_DECL * git_reflog_free(). * * @param out pointer to reflog - * @param ref reference to read the reflog for + * @param repo the repository + * @param name reference to look up * @return 0 or an error code */ -GIT_EXTERN(int) git_reflog_read(git_reflog **out, const git_reference *ref); +GIT_EXTERN(int) git_reflog_read(git_reflog **out, git_repository *repo, const char *name); /** * Write an existing in-memory reflog object back to disk @@ -46,7 +47,7 @@ GIT_EXTERN(int) git_reflog_read(git_reflog **out, const git_reference *ref); GIT_EXTERN(int) git_reflog_write(git_reflog *reflog); /** - * Add a new entry to the reflog. + * Add a new entry to the in-memory reflog. * * `msg` is optional and can be NULL. * @@ -59,26 +60,28 @@ GIT_EXTERN(int) git_reflog_write(git_reflog *reflog); GIT_EXTERN(int) git_reflog_append(git_reflog *reflog, const git_oid *id, const git_signature *committer, const char *msg); /** - * Rename the reflog for the given reference + * Rename a reflog * * The reflog to be renamed is expected to already exist * * The new name will be checked for validity. * See `git_reference_create_symbolic()` for rules about valid names. * - * @param ref the reference + * @param repo the repository + * @param old_name the old name of the reference * @param name the new name of the reference * @return 0 on success, GIT_EINVALIDSPEC or an error code */ -GIT_EXTERN(int) git_reflog_rename(git_reference *ref, const char *name); +GIT_EXTERN(int) git_reflog_rename(git_repository *repo, const char *old_name, const char *name); /** * Delete the reflog for the given reference * - * @param ref the reference + * @param repo the repository + * @param name the reflog to delete * @return 0 or an error code */ -GIT_EXTERN(int) git_reflog_delete(git_reference *ref); +GIT_EXTERN(int) git_reflog_delete(git_repository *repo, const char *name); /** * Get the number of log entries in a reflog @@ -99,7 +102,7 @@ GIT_EXTERN(size_t) git_reflog_entrycount(git_reflog *reflog); * equal to 0 (zero) and less than `git_reflog_entrycount()`. * @return the entry; NULL if not found */ -GIT_EXTERN(const git_reflog_entry *) git_reflog_entry_byindex(git_reflog *reflog, size_t idx); +GIT_EXTERN(const git_reflog_entry *) git_reflog_entry_byindex(const git_reflog *reflog, size_t idx); /** * Remove an entry from the reflog by its index @@ -164,4 +167,5 @@ GIT_EXTERN(void) git_reflog_free(git_reflog *reflog); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/refs.h b/include/git2/refs.h index d586917c2cd..d820f2a1867 100644 --- a/include/git2/refs.h +++ b/include/git2/refs.h @@ -14,8 +14,8 @@ /** * @file git2/refs.h - * @brief Git reference management routines - * @defgroup git_reference Git reference management routines + * @brief References point to a commit; generally these are branches and tags + * @defgroup git_reference References point to a commit; generally these are branches and tags * @ingroup Git * @{ */ @@ -27,12 +27,12 @@ GIT_BEGIN_DECL * The returned reference must be freed by the user. * * The name will be checked for validity. - * See `git_reference_create_symbolic()` for rules about valid names. + * See `git_reference_symbolic_create()` for rules about valid names. * - * @param out pointer to the looked-up reference + * @param[out] out pointer to the looked-up reference * @param repo the repository to look up the reference * @param name the long name for the reference (e.g. HEAD, refs/heads/master, refs/tags/v0.1.0, ...) - * @return 0 on success, ENOTFOUND, EINVALIDSPEC or an error code. + * @return 0 on success, GIT_ENOTFOUND, GIT_EINVALIDSPEC or an error code. */ GIT_EXTERN(int) git_reference_lookup(git_reference **out, git_repository *repo, const char *name); @@ -48,12 +48,69 @@ GIT_EXTERN(int) git_reference_lookup(git_reference **out, git_repository *repo, * * @param out Pointer to oid to be filled in * @param repo The repository in which to look up the reference - * @param name The long name for the reference - * @return 0 on success, ENOTFOUND, EINVALIDSPEC or an error code. + * @param name The long name for the reference (e.g. HEAD, refs/heads/master, refs/tags/v0.1.0, ...) + * @return 0 on success, GIT_ENOTFOUND, GIT_EINVALIDSPEC or an error code. */ GIT_EXTERN(int) git_reference_name_to_id( git_oid *out, git_repository *repo, const char *name); +/** + * Lookup a reference by DWIMing its short name + * + * Apply the git precedence rules to the given shorthand to determine + * which reference the user is referring to. + * + * @param out pointer in which to store the reference + * @param repo the repository in which to look + * @param shorthand the short name for the reference + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reference_dwim(git_reference **out, git_repository *repo, const char *shorthand); + +/** + * Conditionally create a new symbolic reference. + * + * A symbolic reference is a reference name that refers to another + * reference name. If the other name moves, the symbolic name will move, + * too. As a simple example, the "HEAD" reference might refer to + * "refs/heads/master" while on the "master" branch of a repository. + * + * The symbolic reference will be created in the repository and written to + * the disk. The generated reference object must be freed by the user. + * + * Valid reference names must follow one of two patterns: + * + * 1. Top-level names must contain only capital letters and underscores, + * and must begin and end with a letter. (e.g. "HEAD", "ORIG_HEAD"). + * 2. Names prefixed with "refs/" can be almost anything. You must avoid + * the characters '~', '^', ':', '\\', '?', '[', and '*', and the + * sequences ".." and "@{" which have special meaning to revparse. + * + * This function will return an error if a reference already exists with the + * given name unless `force` is true, in which case it will be overwritten. + * + * The message for the reflog will be ignored if the reference does + * not belong in the standard set (HEAD, branches and remote-tracking + * branches) and it does not have a reflog. + * + * It will return GIT_EMODIFIED if the reference's value at the time + * of updating does not match the one passed through `current_value` + * (i.e. if the ref has changed since the user read it). + * + * If `current_value` is all zeros, this function will return GIT_EMODIFIED + * if the ref already exists. + * + * @param out Pointer to the newly created reference + * @param repo Repository where that reference will live + * @param name The name of the reference + * @param target The target of the reference + * @param force Overwrite existing references + * @param current_value The expected value of the reference when updating + * @param log_message The one line long message to be appended to the reflog + * @return 0 on success, GIT_EEXISTS, GIT_EINVALIDSPEC, GIT_EMODIFIED or an error code + */ +GIT_EXTERN(int) git_reference_symbolic_create_matching(git_reference **out, git_repository *repo, const char *name, const char *target, int force, const char *current_value, const char *log_message); + /** * Create a new symbolic reference. * @@ -76,14 +133,19 @@ GIT_EXTERN(int) git_reference_name_to_id( * This function will return an error if a reference already exists with the * given name unless `force` is true, in which case it will be overwritten. * + * The message for the reflog will be ignored if the reference does + * not belong in the standard set (HEAD, branches and remote-tracking + * branches) and it does not have a reflog. + * * @param out Pointer to the newly created reference * @param repo Repository where that reference will live * @param name The name of the reference * @param target The target of the reference * @param force Overwrite existing references - * @return 0 on success, EEXISTS, EINVALIDSPEC or an error code + * @param log_message The one line long message to be appended to the reflog + * @return 0 on success, GIT_EEXISTS, GIT_EINVALIDSPEC or an error code */ -GIT_EXTERN(int) git_reference_symbolic_create(git_reference **out, git_repository *repo, const char *name, const char *target, int force); +GIT_EXTERN(int) git_reference_symbolic_create(git_reference **out, git_repository *repo, const char *name, const char *target, int force, const char *log_message); /** * Create a new direct reference. @@ -108,14 +170,62 @@ GIT_EXTERN(int) git_reference_symbolic_create(git_reference **out, git_repositor * This function will return an error if a reference already exists with the * given name unless `force` is true, in which case it will be overwritten. * + * The message for the reflog will be ignored if the reference does + * not belong in the standard set (HEAD, branches and remote-tracking + * branches) and it does not have a reflog. + * + * @param out Pointer to the newly created reference + * @param repo Repository where that reference will live + * @param name The name of the reference + * @param id The object id pointed to by the reference. + * @param force Overwrite existing references + * @param log_message The one line long message to be appended to the reflog + * @return 0 on success, GIT_EEXISTS, GIT_EINVALIDSPEC or an error code + */ +GIT_EXTERN(int) git_reference_create(git_reference **out, git_repository *repo, const char *name, const git_oid *id, int force, const char *log_message); + +/** + * Conditionally create new direct reference + * + * A direct reference (also called an object id reference) refers directly + * to a specific object id (a.k.a. OID or SHA) in the repository. The id + * permanently refers to the object (although the reference itself can be + * moved). For example, in libgit2 the direct ref "refs/tags/v0.17.0" + * refers to OID 5b9fac39d8a76b9139667c26a63e6b3f204b3977. + * + * The direct reference will be created in the repository and written to + * the disk. The generated reference object must be freed by the user. + * + * Valid reference names must follow one of two patterns: + * + * 1. Top-level names must contain only capital letters and underscores, + * and must begin and end with a letter. (e.g. "HEAD", "ORIG_HEAD"). + * 2. Names prefixed with "refs/" can be almost anything. You must avoid + * the characters '~', '^', ':', '\\', '?', '[', and '*', and the + * sequences ".." and "@{" which have special meaning to revparse. + * + * This function will return an error if a reference already exists with the + * given name unless `force` is true, in which case it will be overwritten. + * + * The message for the reflog will be ignored if the reference does + * not belong in the standard set (HEAD, branches and remote-tracking + * branches) and it does not have a reflog. + * + * It will return GIT_EMODIFIED if the reference's value at the time + * of updating does not match the one passed through `current_id` + * (i.e. if the ref has changed since the user read it). + * * @param out Pointer to the newly created reference * @param repo Repository where that reference will live * @param name The name of the reference * @param id The object id pointed to by the reference. * @param force Overwrite existing references - * @return 0 on success, EEXISTS, EINVALIDSPEC or an error code + * @param current_id The expected value of the reference at the time of update + * @param log_message The one line long message to be appended to the reflog + * @return 0 on success, GIT_EMODIFIED if the value of the reference + * has changed, GIT_EEXISTS, GIT_EINVALIDSPEC or an error code */ -GIT_EXTERN(int) git_reference_create(git_reference **out, git_repository *repo, const char *name, const git_oid *id, int force); +GIT_EXTERN(int) git_reference_create_matching(git_reference **out, git_repository *repo, const char *name, const git_oid *id, int force, const git_oid *current_id, const char *log_message); /** * Get the OID pointed to by a direct reference. @@ -132,6 +242,17 @@ GIT_EXTERN(int) git_reference_create(git_reference **out, git_repository *repo, */ GIT_EXTERN(const git_oid *) git_reference_target(const git_reference *ref); +/** + * Return the peeled OID target of this reference. + * + * This peeled OID only applies to direct references that point to + * a hard Tag object: it is the result of peeling such Tag. + * + * @param ref The reference + * @return a pointer to the oid if available, NULL otherwise + */ +GIT_EXTERN(const git_oid *) git_reference_target_peel(const git_reference *ref); + /** * Get full name to the reference pointed to by a symbolic reference. * @@ -145,17 +266,17 @@ GIT_EXTERN(const char *) git_reference_symbolic_target(const git_reference *ref) /** * Get the type of a reference. * - * Either direct (GIT_REF_OID) or symbolic (GIT_REF_SYMBOLIC) + * Either direct (GIT_REFERENCE_DIRECT) or symbolic (GIT_REFERENCE_SYMBOLIC) * * @param ref The reference * @return the type */ -GIT_EXTERN(git_ref_t) git_reference_type(const git_reference *ref); +GIT_EXTERN(git_reference_t) git_reference_type(const git_reference *ref); /** * Get the full name of a reference. * - * See `git_reference_create_symbolic()` for rules about valid names. + * See `git_reference_symbolic_create()` for rules about valid names. * * @param ref The reference * @return the full name for the ref @@ -174,7 +295,7 @@ GIT_EXTERN(const char *) git_reference_name(const git_reference *ref); * If a direct reference is passed as an argument, a copy of that * reference is returned. This copy must be manually freed too. * - * @param resolved_ref Pointer to the peeled reference + * @param out Pointer to the peeled reference * @param ref The reference * @return 0 or an error code */ @@ -189,33 +310,50 @@ GIT_EXTERN(int) git_reference_resolve(git_reference **out, const git_reference * GIT_EXTERN(git_repository *) git_reference_owner(const git_reference *ref); /** - * Set the symbolic target of a reference. - * - * The reference must be a symbolic reference, otherwise this will fail. + * Create a new reference with the same name as the given reference but a + * different symbolic target. The reference must be a symbolic reference, + * otherwise this will fail. * - * The reference will be automatically updated in memory and on disk. + * The new reference will be written to disk, overwriting the given reference. * * The target name will be checked for validity. - * See `git_reference_create_symbolic()` for rules about valid names. + * See `git_reference_symbolic_create()` for rules about valid names. * + * The message for the reflog will be ignored if the reference does + * not belong in the standard set (HEAD, branches and remote-tracking + * branches) and it does not have a reflog. + * + * @param out Pointer to the newly created reference * @param ref The reference * @param target The new target for the reference - * @return 0 on success, EINVALIDSPEC or an error code + * @param log_message The one line long message to be appended to the reflog + * @return 0 on success, GIT_EINVALIDSPEC or an error code */ -GIT_EXTERN(int) git_reference_symbolic_set_target(git_reference *ref, const char *target); +GIT_EXTERN(int) git_reference_symbolic_set_target( + git_reference **out, + git_reference *ref, + const char *target, + const char *log_message); /** - * Set the OID target of a reference. + * Conditionally create a new reference with the same name as the given reference but a + * different OID target. The reference must be a direct reference, otherwise + * this will fail. * - * The reference must be a direct reference, otherwise this will fail. - * - * The reference will be automatically updated in memory and on disk. + * The new reference will be written to disk, overwriting the given reference. * + * @param out Pointer to the newly created reference * @param ref The reference * @param id The new target OID for the reference - * @return 0 or an error code + * @param log_message The one line long message to be appended to the reflog + * @return 0 on success, GIT_EMODIFIED if the value of the reference + * has changed since it was read, or an error code */ -GIT_EXTERN(int) git_reference_set_target(git_reference *ref, const git_oid *id); +GIT_EXTERN(int) git_reference_set_target( + git_reference **out, + git_reference *ref, + const git_oid *id, + const char *log_message); /** * Rename an existing reference. @@ -223,11 +361,7 @@ GIT_EXTERN(int) git_reference_set_target(git_reference *ref, const git_oid *id); * This method works for both direct and symbolic references. * * The new name will be checked for validity. - * See `git_reference_create_symbolic()` for rules about valid names. - * - * The given git_reference will be updated in place. - * - * The reference will be immediately renamed in-memory and on disk. + * See `git_reference_symbolic_create()` for rules about valid names. * * If the `force` flag is not enabled, and there's already * a reference with the given name, the renaming will fail. @@ -237,51 +371,51 @@ GIT_EXTERN(int) git_reference_set_target(git_reference *ref, const git_oid *id); * reflog is enabled for the repository. We only rename * the reflog if it exists. * + * @param[out] new_ref The new reference * @param ref The reference to rename - * @param name The new name for the reference + * @param new_name The new name for the reference * @param force Overwrite an existing reference - * @return 0 on success, EINVALIDSPEC, EEXISTS or an error code + * @param log_message The one line long message to be appended to the reflog + * @return 0 on success, GIT_EINVALIDSPEC, GIT_EEXISTS or an error code * */ -GIT_EXTERN(int) git_reference_rename(git_reference *ref, const char *name, int force); +GIT_EXTERN(int) git_reference_rename( + git_reference **new_ref, + git_reference *ref, + const char *new_name, + int force, + const char *log_message); /** * Delete an existing reference. * - * This method works for both direct and symbolic references. + * This method works for both direct and symbolic references. The reference + * will be immediately removed on disk but the memory will not be freed. + * Callers must call `git_reference_free`. * - * The reference will be immediately removed on disk and from memory - * (i.e. freed). The given reference pointer will no longer be valid. + * This function will return an error if the reference has changed + * from the time it was looked up. * * @param ref The reference to remove - * @return 0 or an error code + * @return 0, GIT_EMODIFIED or an error code */ GIT_EXTERN(int) git_reference_delete(git_reference *ref); /** - * Pack all the loose references in the repository. - * - * This method will load into the cache all the loose - * references on the repository and update the - * `packed-refs` file with them. + * Delete an existing reference by name * - * Once the `packed-refs` file has been written properly, - * the loose references will be removed from disk. + * This method removes the named reference from the repository without + * looking at its old value. * - * @param repo Repository where the loose refs will be packed + * @param repo The repository to remove the reference from + * @param name The reference to remove * @return 0 or an error code */ -GIT_EXTERN(int) git_reference_packall(git_repository *repo); +GIT_EXTERN(int) git_reference_remove(git_repository *repo, const char *name); /** * Fill a list with all the references that can be found in a repository. * - * Using the `list_flags` parameter, the listed references may be filtered - * by type (`GIT_REF_OID` or `GIT_REF_SYMBOLIC`) or using a bitwise OR of - * `git_ref_t` values. To include packed refs, include `GIT_REF_PACKED`. - * For convenience, use the value `GIT_REF_LISTALL` to obtain all - * references, including packed ones. - * * The string array will be filled with the names of all references; these * values are owned by the user and should be free'd manually when no * longer needed, using `git_strarray_free()`. @@ -289,64 +423,81 @@ GIT_EXTERN(int) git_reference_packall(git_repository *repo); * @param array Pointer to a git_strarray structure where * the reference names will be stored * @param repo Repository where to find the refs - * @param list_flags Filtering flags for the reference listing * @return 0 or an error code */ -GIT_EXTERN(int) git_reference_list(git_strarray *array, git_repository *repo, unsigned int list_flags); +GIT_EXTERN(int) git_reference_list(git_strarray *array, git_repository *repo); -typedef int (*git_reference_foreach_cb)(const char *refname, void *payload); +/** + * Callback used to iterate over references + * + * @see git_reference_foreach + * + * @param reference The reference object + * @param payload Payload passed to git_reference_foreach + * @return non-zero to terminate the iteration + */ +typedef int GIT_CALLBACK(git_reference_foreach_cb)(git_reference *reference, void *payload); /** - * Perform a callback on each reference in the repository. + * Callback used to iterate over reference names * - * Using the `list_flags` parameter, the references may be filtered by - * type (`GIT_REF_OID` or `GIT_REF_SYMBOLIC`) or using a bitwise OR of - * `git_ref_t` values. To include packed refs, include `GIT_REF_PACKED`. - * For convenience, use the value `GIT_REF_LISTALL` to obtain all - * references, including packed ones. + * @see git_reference_foreach_name + * + * @param name The reference name + * @param payload Payload passed to git_reference_foreach_name + * @return non-zero to terminate the iteration + */ +typedef int GIT_CALLBACK(git_reference_foreach_name_cb)(const char *name, void *payload); + +/** + * Perform a callback on each reference in the repository. * * The `callback` function will be called for each reference in the - * repository, receiving the name of the reference and the `payload` value + * repository, receiving the reference object and the `payload` value * passed to this method. Returning a non-zero value from the callback * will terminate the iteration. * + * Note that the callback function is responsible to call `git_reference_free` + * on each reference passed to it. + * * @param repo Repository where to find the refs - * @param list_flags Filtering flags for the reference listing. * @param callback Function which will be called for every listed ref * @param payload Additional data to pass to the callback - * @return 0 on success, GIT_EUSER on non-zero callback, or error code + * @return 0 on success, non-zero callback return value, or error code */ GIT_EXTERN(int) git_reference_foreach( git_repository *repo, - unsigned int list_flags, git_reference_foreach_cb callback, void *payload); /** - * Check if a reference has been loaded from a packfile. + * Perform a callback on the fully-qualified name of each reference. * - * @param ref A git reference - * @return 0 in case it's not packed; 1 otherwise + * The `callback` function will be called for each reference in the + * repository, receiving the name of the reference and the `payload` value + * passed to this method. Returning a non-zero value from the callback + * will terminate the iteration. + * + * @param repo Repository where to find the refs + * @param callback Function which will be called for every listed ref name + * @param payload Additional data to pass to the callback + * @return 0 on success, non-zero callback return value, or error code */ -GIT_EXTERN(int) git_reference_is_packed(git_reference *ref); +GIT_EXTERN(int) git_reference_foreach_name( + git_repository *repo, + git_reference_foreach_name_cb callback, + void *payload); /** - * Reload a reference from disk. - * - * Reference pointers can become outdated if the Git repository is - * accessed simultaneously by other clients while the library is open. - * - * This method forces a reload of the reference from disk, to ensure that - * the provided information is still reliable. + * Create a copy of an existing reference. * - * If the reload fails (e.g. the reference no longer exists on disk, or - * has become corrupted), an error code will be returned and the reference - * pointer will be invalidated and freed. + * Call `git_reference_free` to free the data. * - * @param ref The reference to reload - * @return 0 on success, or an error code + * @param dest pointer where to store the copy + * @param source object to copy + * @return 0 or an error code */ -GIT_EXTERN(int) git_reference_reload(git_reference *ref); +GIT_EXTERN(int) git_reference_dup(git_reference **dest, git_reference *source); /** * Free the given reference. @@ -362,7 +513,63 @@ GIT_EXTERN(void) git_reference_free(git_reference *ref); * @param ref2 The second git_reference * @return 0 if the same, else a stable but meaningless ordering. */ -GIT_EXTERN(int) git_reference_cmp(git_reference *ref1, git_reference *ref2); +GIT_EXTERN(int) git_reference_cmp( + const git_reference *ref1, + const git_reference *ref2); + +/** + * Create an iterator for the repo's references + * + * @param[out] out pointer in which to store the iterator + * @param repo the repository + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reference_iterator_new( + git_reference_iterator **out, + git_repository *repo); + +/** + * Create an iterator for the repo's references that match the + * specified glob + * + * @param out pointer in which to store the iterator + * @param repo the repository + * @param glob the glob to match against the reference names + * @return 0 or an error code + */ +GIT_EXTERN(int) git_reference_iterator_glob_new( + git_reference_iterator **out, + git_repository *repo, + const char *glob); + +/** + * Get the next reference + * + * @param[out] out pointer in which to store the reference + * @param iter the iterator + * @return 0, GIT_ITEROVER if there are no more; or an error code + */ +GIT_EXTERN(int) git_reference_next(git_reference **out, git_reference_iterator *iter); + +/** + * Get the next reference's name + * + * This function is provided for convenience in case only the names + * are interesting as it avoids the allocation of the `git_reference` + * object which `git_reference_next()` needs. + * + * @param out pointer in which to store the string + * @param iter the iterator + * @return 0, GIT_ITEROVER if there are no more; or an error code + */ +GIT_EXTERN(int) git_reference_next_name(const char **out, git_reference_iterator *iter); + +/** + * Free the iterator and its associated resources + * + * @param iter the iterator to free + */ +GIT_EXTERN(void) git_reference_iterator_free(git_reference_iterator *iter); /** * Perform a callback on each reference in the repository whose name @@ -378,7 +585,6 @@ GIT_EXTERN(int) git_reference_cmp(git_reference *ref1, git_reference *ref2); * * @param repo Repository where to find the refs * @param glob Pattern to match (fnmatch-style) against reference name. - * @param list_flags Filtering flags for the reference listing. * @param callback Function which will be called for every listed ref * @param payload Additional data to pass to the callback * @return 0 on success, GIT_EUSER on non-zero callback, or error code @@ -386,19 +592,30 @@ GIT_EXTERN(int) git_reference_cmp(git_reference *ref1, git_reference *ref2); GIT_EXTERN(int) git_reference_foreach_glob( git_repository *repo, const char *glob, - unsigned int list_flags, - git_reference_foreach_cb callback, + git_reference_foreach_name_cb callback, void *payload); /** * Check if a reflog exists for the specified reference. * - * @param ref A git reference - * + * @param repo the repository + * @param refname the reference's name * @return 0 when no reflog can be found, 1 when it exists; * otherwise an error code. */ -GIT_EXTERN(int) git_reference_has_log(git_reference *ref); +GIT_EXTERN(int) git_reference_has_log(git_repository *repo, const char *refname); + +/** + * Ensure there is a reflog for a particular reference. + * + * Make sure that successive updates to the reference will append to + * its log. + * + * @param repo the repository + * @param refname the reference's name + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_reference_ensure_log(git_repository *repo, const char *refname); /** * Check if a reference is a local branch. @@ -408,7 +625,7 @@ GIT_EXTERN(int) git_reference_has_log(git_reference *ref); * @return 1 when the reference lives in the refs/heads * namespace; 0 otherwise. */ -GIT_EXTERN(int) git_reference_is_branch(git_reference *ref); +GIT_EXTERN(int) git_reference_is_branch(const git_reference *ref); /** * Check if a reference is a remote tracking branch @@ -418,11 +635,36 @@ GIT_EXTERN(int) git_reference_is_branch(git_reference *ref); * @return 1 when the reference lives in the refs/remotes * namespace; 0 otherwise. */ -GIT_EXTERN(int) git_reference_is_remote(git_reference *ref); +GIT_EXTERN(int) git_reference_is_remote(const git_reference *ref); +/** + * Check if a reference is a tag + * + * @param ref A git reference + * + * @return 1 when the reference lives in the refs/tags + * namespace; 0 otherwise. + */ +GIT_EXTERN(int) git_reference_is_tag(const git_reference *ref); +/** + * Check if a reference is a note + * + * @param ref A git reference + * + * @return 1 when the reference lives in the refs/notes + * namespace; 0 otherwise. + */ +GIT_EXTERN(int) git_reference_is_note(const git_reference *ref); + +/** + * Normalization options for reference lookup + */ typedef enum { - GIT_REF_FORMAT_NORMAL = 0, + /** + * No particular normalization. + */ + GIT_REFERENCE_FORMAT_NORMAL = 0u, /** * Control whether one-level refnames are accepted @@ -430,7 +672,7 @@ typedef enum { * components). Those are expected to be written only using * uppercase letters and underscore (FETCH_HEAD, ...) */ - GIT_REF_FORMAT_ALLOW_ONELEVEL = (1 << 0), + GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL = (1u << 0), /** * Interpret the provided name as a reference pattern for a @@ -439,8 +681,15 @@ typedef enum { * in place of a one full pathname component * (e.g., foo//bar but not foo/bar). */ - GIT_REF_FORMAT_REFSPEC_PATTERN = (1 << 1), -} git_reference_normalize_t; + GIT_REFERENCE_FORMAT_REFSPEC_PATTERN = (1u << 1), + + /** + * Interpret the name as part of a refspec in shorthand form + * so the `ONELEVEL` naming rules aren't enforced and 'master' + * becomes a valid name. + */ + GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND = (1u << 2) +} git_reference_format_t; /** * Normalize reference name and check validity. @@ -452,14 +701,14 @@ typedef enum { * Once normalized, if the reference name is valid, it will be returned in * the user allocated buffer. * - * See `git_reference_create_symbolic()` for rules about valid names. + * See `git_reference_symbolic_create()` for rules about valid names. * * @param buffer_out User allocated buffer to store normalized name * @param buffer_size Size of buffer_out * @param name Reference name to be checked. * @param flags Flags to constrain name validation rules - see the - * GIT_REF_FORMAT constants above. - * @return 0 on success, GIT_EBUFS if buffer is too small, EINVALIDSPEC + * GIT_REFERENCE_FORMAT constants above. + * @return 0 on success, GIT_EBUFS if buffer is too small, GIT_EINVALIDSPEC * or an error code. */ GIT_EXTERN(int) git_reference_normalize_name( @@ -474,19 +723,19 @@ GIT_EXTERN(int) git_reference_normalize_name( * The retrieved `peeled` object is owned by the repository * and should be closed with the `git_object_free` method. * - * If you pass `GIT_OBJ_ANY` as the target type, then the object + * If you pass `GIT_OBJECT_ANY` as the target type, then the object * will be peeled until a non-tag object is met. * - * @param peeled Pointer to the peeled git_object + * @param[out] out Pointer to the peeled git_object * @param ref The reference to be processed - * @param target_type The type of the requested object (GIT_OBJ_COMMIT, - * GIT_OBJ_TAG, GIT_OBJ_TREE, GIT_OBJ_BLOB or GIT_OBJ_ANY). + * @param type The type of the requested object (GIT_OBJECT_COMMIT, + * GIT_OBJECT_TAG, GIT_OBJECT_TREE, GIT_OBJECT_BLOB or GIT_OBJECT_ANY). * @return 0 on success, GIT_EAMBIGUOUS, GIT_ENOTFOUND or an error code */ GIT_EXTERN(int) git_reference_peel( git_object **out, - git_reference *ref, - git_otype type); + const git_reference *ref, + git_object_t type); /** * Ensure the reference name is well-formed. @@ -499,11 +748,27 @@ GIT_EXTERN(int) git_reference_peel( * the characters '~', '^', ':', '\\', '?', '[', and '*', and the * sequences ".." and "@{" which have special meaning to revparse. * + * @param valid output pointer to set with validity of given reference name * @param refname name to be checked. - * @return 1 if the reference name is acceptable; 0 if it isn't + * @return 0 on success or an error code */ -GIT_EXTERN(int) git_reference_is_valid_name(const char *refname); +GIT_EXTERN(int) git_reference_name_is_valid(int *valid, const char *refname); + +/** + * Get the reference's short name + * + * This will transform the reference name into a name "human-readable" + * version. If no shortname is appropriate, it will return the full + * name. + * + * The memory is owned by the reference and must not be freed. + * + * @param ref a reference + * @return the human-readable version of the name + */ +GIT_EXTERN(const char *) git_reference_shorthand(const git_reference *ref); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/refspec.h b/include/git2/refspec.h index ee06f8eca33..49d5f89f7e6 100644 --- a/include/git2/refspec.h +++ b/include/git2/refspec.h @@ -9,16 +9,35 @@ #include "common.h" #include "types.h" +#include "net.h" +#include "buffer.h" /** * @file git2/refspec.h - * @brief Git refspec attributes - * @defgroup git_refspec Git refspec attributes + * @brief Refspecs map local references to remote references + * @defgroup git_refspec Refspecs map local references to remote references * @ingroup Git * @{ */ GIT_BEGIN_DECL +/** + * Parse a given refspec string + * + * @param refspec a pointer to hold the refspec handle + * @param input the refspec string + * @param is_fetch is this a refspec for a fetch + * @return 0 if the refspec string could be parsed, -1 otherwise + */ +GIT_EXTERN(int) git_refspec_parse(git_refspec **refspec, const char *input, int is_fetch); + +/** + * Free a refspec object which has been created by git_refspec_parse + * + * @param refspec the refspec object + */ +GIT_EXTERN(void) git_refspec_free(git_refspec *refspec); + /** * Get the source specifier * @@ -35,6 +54,14 @@ GIT_EXTERN(const char *) git_refspec_src(const git_refspec *refspec); */ GIT_EXTERN(const char *) git_refspec_dst(const git_refspec *refspec); +/** + * Get the refspec's string + * + * @param refspec the refspec + * @return the refspec's original string + */ +GIT_EXTERN(const char *) git_refspec_string(const git_refspec *refspec); + /** * Get the force update setting * @@ -44,7 +71,24 @@ GIT_EXTERN(const char *) git_refspec_dst(const git_refspec *refspec); GIT_EXTERN(int) git_refspec_force(const git_refspec *refspec); /** - * Check if a refspec's source descriptor matches a reference + * Get the refspec's direction. + * + * @param spec refspec + * @return GIT_DIRECTION_FETCH or GIT_DIRECTION_PUSH + */ +GIT_EXTERN(git_direction) git_refspec_direction(const git_refspec *spec); + +/** + * Check if a refspec's source descriptor matches a negative reference + * + * @param refspec the refspec + * @param refname the name of the reference to check + * @return 1 if the refspec matches, 0 otherwise + */ +GIT_EXTERN(int) git_refspec_src_matches_negative(const git_refspec *refspec, const char *refname); + +/** + * Check if a refspec's source descriptor matches a reference * * @param refspec the refspec * @param refname the name of the reference to check @@ -52,17 +96,36 @@ GIT_EXTERN(int) git_refspec_force(const git_refspec *refspec); */ GIT_EXTERN(int) git_refspec_src_matches(const git_refspec *refspec, const char *refname); +/** + * Check if a refspec's destination descriptor matches a reference + * + * @param refspec the refspec + * @param refname the name of the reference to check + * @return 1 if the refspec matches, 0 otherwise + */ +GIT_EXTERN(int) git_refspec_dst_matches(const git_refspec *refspec, const char *refname); + /** * Transform a reference to its target following the refspec's rules * * @param out where to store the target name - * @param outlen the size of the `out` buffer * @param spec the refspec * @param name the name of the reference to transform * @return 0, GIT_EBUFS or another error */ -GIT_EXTERN(int) git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, const char *name); +GIT_EXTERN(int) git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name); + +/** + * Transform a target reference to its source reference following the refspec's rules + * + * @param out where to store the source reference name + * @param spec the refspec + * @param name the name of the reference to transform + * @return 0, GIT_EBUFS or another error + */ +GIT_EXTERN(int) git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name); +/** @} */ GIT_END_DECL #endif diff --git a/include/git2/remote.h b/include/git2/remote.h index b92a0cd0490..b5e83ecc8f0 100644 --- a/include/git2/remote.h +++ b/include/git2/remote.h @@ -14,28 +14,20 @@ #include "indexer.h" #include "strarray.h" #include "transport.h" +#include "pack.h" +#include "proxy.h" /** * @file git2/remote.h - * @brief Git remote management functions - * @defgroup git_remote remote management functions + * @brief Remotes are where local repositories fetch from and push to + * @defgroup git_remote Remotes are where local repositories fetch from and push to * @ingroup Git * @{ */ GIT_BEGIN_DECL -typedef int (*git_remote_rename_problem_cb)(const char *problematic_refspec, void *payload); -/* - * TODO: This functions still need to be implemented: - * - _listcb/_foreach - * - _add - * - _rename - * - _del (needs support from config) - */ - /** - * Add a remote with the default fetch refspec to the repository's configuration. This - * calls git_remote_save before returning. + * Add a remote with the default fetch refspec to the repository's configuration. * * @param out the resulting remote * @param repo the repository in which to create the remote @@ -50,25 +42,167 @@ GIT_EXTERN(int) git_remote_create( const char *url); /** - * Create a remote in memory + * Remote redirection settings; whether redirects to another host + * are permitted. By default, git will follow a redirect on the + * initial request (`/info/refs`), but not subsequent requests. + */ +typedef enum { + /** + * Do not follow any off-site redirects at any stage of + * the fetch or push. + */ + GIT_REMOTE_REDIRECT_NONE = (1 << 0), + + /** + * Allow off-site redirects only upon the initial request. + * This is the default. + */ + GIT_REMOTE_REDIRECT_INITIAL = (1 << 1), + + /** + * Allow redirects at any stage in the fetch or push. + */ + GIT_REMOTE_REDIRECT_ALL = (1 << 2) +} git_remote_redirect_t; + +/** + * Remote creation options flags + */ +typedef enum { + /** Ignore the repository apply.insteadOf configuration */ + GIT_REMOTE_CREATE_SKIP_INSTEADOF = (1 << 0), + + /** Don't build a fetchspec from the name if none is set */ + GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC = (1 << 1) +} git_remote_create_flags; + +/** + * How to handle reference updates. + */ +typedef enum { + /* Write the fetch results to FETCH_HEAD. */ + GIT_REMOTE_UPDATE_FETCHHEAD = (1 << 0), + + /* Report unchanged tips in the update_refs callback. */ + GIT_REMOTE_UPDATE_REPORT_UNCHANGED = (1 << 1) +} git_remote_update_flags; + +/** + * Remote creation options structure * - * Create a remote with the given refspec in memory. You can use - * this when you have a URL instead of a remote's name. Note that in-memory - * remotes cannot be converted to persisted remotes. + * Initialize with `GIT_REMOTE_CREATE_OPTIONS_INIT`. Alternatively, you can + * use `git_remote_create_options_init`. * - * The name, when provided, will be checked for validity. - * See `git_tag_create()` for rules about valid names. + */ +typedef struct git_remote_create_options { + unsigned int version; + + /** + * The repository that should own the remote. + * Setting this to NULL results in a detached remote. + */ + git_repository *repository; + + /** + * The remote's name. + * Setting this to NULL results in an in-memory/anonymous remote. + */ + const char *name; + + /** The fetchspec the remote should use. */ + const char *fetchspec; + + /** Additional flags for the remote. See git_remote_create_flags. */ + unsigned int flags; +} git_remote_create_options; + +/** Current version for the `git_remote_create_options` structure */ +#define GIT_REMOTE_CREATE_OPTIONS_VERSION 1 + +/** Static constructor for `git_remote_create_options` */ +#define GIT_REMOTE_CREATE_OPTIONS_INIT {GIT_REMOTE_CREATE_OPTIONS_VERSION} + +/** + * Initialize git_remote_create_options structure * - * @param out pointer to the new remote object + * Initializes a `git_remote_create_options` with default values. Equivalent to + * creating an instance with `GIT_REMOTE_CREATE_OPTIONS_INIT`. + * + * @param opts The `git_remote_create_options` struct to initialize. + * @param version The struct version; pass `GIT_REMOTE_CREATE_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_remote_create_options_init( + git_remote_create_options *opts, + unsigned int version); + +/** + * Create a remote, with options. + * + * This function allows more fine-grained control over the remote creation. + * + * Passing NULL as the opts argument will result in a detached remote. + * + * @param out the resulting remote + * @param url the remote's url + * @param opts the remote creation options + * @return 0, GIT_EINVALIDSPEC, GIT_EEXISTS or an error code + */ +GIT_EXTERN(int) git_remote_create_with_opts( + git_remote **out, + const char *url, + const git_remote_create_options *opts); + +/** + * Add a remote with the provided fetch refspec (or default if NULL) to the repository's + * configuration. + * + * @param out the resulting remote + * @param repo the repository in which to create the remote + * @param name the remote's name + * @param url the remote's url + * @param fetch the remote fetch value + * @return 0, GIT_EINVALIDSPEC, GIT_EEXISTS or an error code + */ +GIT_EXTERN(int) git_remote_create_with_fetchspec( + git_remote **out, + git_repository *repo, + const char *name, + const char *url, + const char *fetch); + +/** + * Create an anonymous remote + * + * Create a remote with the given url in-memory. You can use this when + * you have a URL instead of a remote's name. + * + * @param out pointer to the new remote objects * @param repo the associated repository - * @param fetch the fetch refspec to use for this remote. May be NULL for defaults. * @param url the remote repository's URL * @return 0 or an error code */ -GIT_EXTERN(int) git_remote_create_inmemory( +GIT_EXTERN(int) git_remote_create_anonymous( git_remote **out, git_repository *repo, - const char *fetch, + const char *url); + +/** + * Create a remote without a connected local repo + * + * Create a remote with the given url in-memory. You can use this when + * you have a URL instead of a remote's name. + * + * Contrasted with git_remote_create_anonymous, a detached remote + * will not consider any repo configuration values (such as insteadof url + * substitutions). + * + * @param out pointer to the new remote objects + * @param url the remote repository's URL + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_create_detached( + git_remote **out, const char *url); /** @@ -82,18 +216,27 @@ GIT_EXTERN(int) git_remote_create_inmemory( * @param name the remote's name * @return 0, GIT_ENOTFOUND, GIT_EINVALIDSPEC or an error code */ -GIT_EXTERN(int) git_remote_load(git_remote **out, git_repository *repo, const char *name); +GIT_EXTERN(int) git_remote_lookup(git_remote **out, git_repository *repo, const char *name); /** - * Save a remote to its repository's configuration + * Create a copy of an existing remote. All internal strings are also + * duplicated. Callbacks are not duplicated. + * + * Call `git_remote_free` to free the data. * - * One can't save a in-memory remote. Doing so will - * result in a GIT_EINVALIDSPEC being returned. + * @param dest pointer where to store the copy + * @param source object to copy + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_dup(git_remote **dest, git_remote *source); + +/** + * Get the remote's repository * - * @param remote the remote to save to config - * @return 0, GIT_EINVALIDSPEC or an error code + * @param remote the remote + * @return a pointer to the repository */ -GIT_EXTERN(int) git_remote_save(const git_remote *remote); +GIT_EXTERN(git_repository *) git_remote_owner(const git_remote *remote); /** * Get the remote's name @@ -106,13 +249,21 @@ GIT_EXTERN(const char *) git_remote_name(const git_remote *remote); /** * Get the remote's url * + * If url.*.insteadOf has been configured for this URL, it will return + * the modified URL. This function does not consider if a push url has + * been configured for this remote (use `git_remote_pushurl` if needed). + * * @param remote the remote * @return a pointer to the url */ GIT_EXTERN(const char *) git_remote_url(const git_remote *remote); /** - * Get the remote's url for pushing + * Get the remote's url for pushing. + * + * If url.*.pushInsteadOf has been configured for this URL, it + * will return the modified URL. If `git_remote_set_instance_pushurl` + * has been called for this remote, then that URL will be returned. * * @param remote the remote * @return a pointer to the url or NULL if no special url for pushing is set @@ -120,111 +271,140 @@ GIT_EXTERN(const char *) git_remote_url(const git_remote *remote); GIT_EXTERN(const char *) git_remote_pushurl(const git_remote *remote); /** - * Set the remote's url + * Set the remote's url in the configuration * - * Existing connections will not be updated. + * Remote objects already in memory will not be affected. This assumes + * the common case of a single-url remote and will otherwise return an error. * - * @param remote the remote + * @param repo the repository in which to perform the change + * @param remote the remote's name * @param url the url to set * @return 0 or an error value */ -GIT_EXTERN(int) git_remote_set_url(git_remote *remote, const char* url); +GIT_EXTERN(int) git_remote_set_url(git_repository *repo, const char *remote, const char *url); /** - * Set the remote's url for pushing + * Set the remote's url for pushing in the configuration. * - * Existing connections will not be updated. + * Remote objects already in memory will not be affected. This assumes + * the common case of a single-url remote and will otherwise return an error. * - * @param remote the remote - * @param url the url to set or NULL to clear the pushurl - * @return 0 or an error value + * + * @param repo the repository in which to perform the change + * @param remote the remote's name + * @param url the url to set + * @return 0, or an error code */ -GIT_EXTERN(int) git_remote_set_pushurl(git_remote *remote, const char* url); +GIT_EXTERN(int) git_remote_set_pushurl(git_repository *repo, const char *remote, const char *url); /** - * Set the remote's fetch refspec + * Set the url for this particular url instance. The URL in the + * configuration will be ignored, and will not be changed. * - * @param remote the remote - * @apram spec the new fetch refspec + * @param remote the remote's name + * @param url the url to set * @return 0 or an error value */ -GIT_EXTERN(int) git_remote_set_fetchspec(git_remote *remote, const char *spec); +GIT_EXTERN(int) git_remote_set_instance_url(git_remote *remote, const char *url); /** - * Get the fetch refspec + * Set the push url for this particular url instance. The URL in the + * configuration will be ignored, and will not be changed. * - * @param remote the remote - * @return a pointer to the fetch refspec or NULL if it doesn't exist + * @param remote the remote's name + * @param url the url to set + * @return 0 or an error value */ -GIT_EXTERN(const git_refspec *) git_remote_fetchspec(const git_remote *remote); +GIT_EXTERN(int) git_remote_set_instance_pushurl(git_remote *remote, const char *url); /** - * Set the remote's push refspec + * Add a fetch refspec to the remote's configuration * - * @param remote the remote - * @param spec the new push refspec - * @return 0 or an error value + * Add the given refspec to the fetch list in the configuration. No + * loaded remote instances will be affected. + * + * @param repo the repository in which to change the configuration + * @param remote the name of the remote to change + * @param refspec the new fetch refspec + * @return 0, GIT_EINVALIDSPEC if refspec is invalid or an error value */ -GIT_EXTERN(int) git_remote_set_pushspec(git_remote *remote, const char *spec); +GIT_EXTERN(int) git_remote_add_fetch(git_repository *repo, const char *remote, const char *refspec); /** - * Get the push refspec + * Get the remote's list of fetch refspecs * - * @param remote the remote - * @return a pointer to the push refspec or NULL if it doesn't exist + * The memory is owned by the user and should be freed with + * `git_strarray_free`. + * + * @param array pointer to the array in which to store the strings + * @param remote the remote to query + * @return 0 or an error code. */ - -GIT_EXTERN(const git_refspec *) git_remote_pushspec(const git_remote *remote); +GIT_EXTERN(int) git_remote_get_fetch_refspecs(git_strarray *array, const git_remote *remote); /** - * Open a connection to a remote + * Add a push refspec to the remote's configuration * - * The transport is selected based on the URL. The direction argument - * is due to a limitation of the git protocol (over TCP or SSH) which - * starts up a specific binary which can only do the one or the other. + * Add the given refspec to the push list in the configuration. No + * loaded remote instances will be affected. * - * @param remote the remote to connect to - * @param direction whether you want to receive or send data - * @return 0 or an error code + * @param repo the repository in which to change the configuration + * @param remote the name of the remote to change + * @param refspec the new push refspec + * @return 0, GIT_EINVALIDSPEC if refspec is invalid or an error value */ -GIT_EXTERN(int) git_remote_connect(git_remote *remote, git_direction direction); +GIT_EXTERN(int) git_remote_add_push(git_repository *repo, const char *remote, const char *refspec); /** - * Get a list of refs at the remote + * Get the remote's list of push refspecs * - * The remote (or more exactly its transport) must be connected. The - * memory belongs to the remote. + * The memory is owned by the user and should be freed with + * `git_strarray_free`. * - * If you a return a non-zero value from the callback, this will stop - * looping over the refs. + * @param array pointer to the array in which to store the strings + * @param remote the remote to query + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_remote_get_push_refspecs(git_strarray *array, const git_remote *remote); + +/** + * Get the number of refspecs for a remote * * @param remote the remote - * @param list_cb function to call with each ref discovered at the remote - * @param payload additional data to pass to the callback - * @return 0 on success, GIT_EUSER on non-zero callback, or error code + * @return the amount of refspecs configured in this remote */ -GIT_EXTERN(int) git_remote_ls(git_remote *remote, git_headlist_cb list_cb, void *payload); +GIT_EXTERN(size_t) git_remote_refspec_count(const git_remote *remote); /** - * Download the packfile + * Get a refspec from the remote * - * Negotiate what objects should be downloaded and download the - * packfile with those objects. The packfile is downloaded with a - * temporary filename, as it's final name is not known yet. If there - * was no packfile needed (all the objects were available locally), - * filename will be NULL and the function will return success. + * @param remote the remote to query + * @param n the refspec to get + * @return the nth refspec + */ +GIT_EXTERN(const git_refspec *)git_remote_get_refspec(const git_remote *remote, size_t n); + +/** + * Get the remote repository's reference advertisement list * - * @param remote the remote to download from - * @param progress_cb function to call with progress information. Be aware that - * this is called inline with network and indexing operations, so performance - * may be affected. - * @param progress_payload payload for the progress callback - * @return 0 or an error code + * Get the list of references with which the server responds to a new + * connection. + * + * The remote (or more exactly its transport) must have connected to + * the remote repository. This list is available as soon as the + * connection to the remote is initiated and it remains available + * after disconnecting. + * + * The memory belongs to the remote. The pointer will be valid as long + * as a new connection is not initiated, but it is recommended that + * you make a copy in order to make use of the data. + * + * @param out pointer to the array + * @param size the number of remote heads + * @param remote the remote + * @return 0 on success, or an error code */ -GIT_EXTERN(int) git_remote_download( - git_remote *remote, - git_transfer_progress_callback progress_cb, - void *payload); +GIT_EXTERN(int) git_remote_ls(const git_remote_head ***out, size_t *size, git_remote *remote); /** * Check whether the remote is connected @@ -235,7 +415,20 @@ GIT_EXTERN(int) git_remote_download( * @param remote the remote * @return 1 if it's connected, 0 otherwise. */ -GIT_EXTERN(int) git_remote_connected(git_remote *remote); +GIT_EXTERN(int) git_remote_connected(const git_remote *remote); + +/** + * Get the remote repository's object format. + * + * The remote (or more exactly its transport) must have connected to + * the remote repository. This format is available as soon as the + * connection to the remote is initiated and stays connected. + * + * @param out the resulting object format type + * @param remote the remote + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_remote_oid_type(git_oid_t *out, git_remote *remote); /** * Cancel the operation @@ -244,18 +437,19 @@ GIT_EXTERN(int) git_remote_connected(git_remote *remote); * the operation has been cancelled and if so stops the operation. * * @param remote the remote + * @return 0 on success, or an error code */ -GIT_EXTERN(void) git_remote_stop(git_remote *remote); +GIT_EXTERN(int) git_remote_stop(git_remote *remote); /** * Disconnect from the remote * - * Close the connection to the remote and free the underlying - * transport. + * Close the connection to the remote. * * @param remote the remote to disconnect from + * @return 0 on success, or an error code */ -GIT_EXTERN(void) git_remote_disconnect(git_remote *remote); +GIT_EXTERN(int) git_remote_disconnect(git_remote *remote); /** * Free the memory associated with a remote @@ -268,146 +462,733 @@ GIT_EXTERN(void) git_remote_disconnect(git_remote *remote); GIT_EXTERN(void) git_remote_free(git_remote *remote); /** - * Update the tips to the new state + * Get a list of the configured remotes for a repo * - * @param remote the remote to update + * The string array must be freed by the user. + * + * @param out a string array which receives the names of the remotes + * @param repo the repository to query * @return 0 or an error code */ -GIT_EXTERN(int) git_remote_update_tips(git_remote *remote); +GIT_EXTERN(int) git_remote_list(git_strarray *out, git_repository *repo); /** - * Return whether a string is a valid remote URL - * - * @param url the url to check - * @param 1 if the url is valid, 0 otherwise + * Argument to the completion callback which tells it which operation + * finished. */ -GIT_EXTERN(int) git_remote_valid_url(const char *url); +typedef enum git_remote_completion_t { + GIT_REMOTE_COMPLETION_DOWNLOAD, + GIT_REMOTE_COMPLETION_INDEXING, + GIT_REMOTE_COMPLETION_ERROR +} git_remote_completion_t; /** - * Return whether the passed URL is supported by this version of the library. + * Push network progress notification callback. * - * @param url the url to check - * @return 1 if the url is supported, 0 otherwise -*/ -GIT_EXTERN(int) git_remote_supported_url(const char* url); + * @param current The number of objects pushed so far + * @param total The total number of objects to push + * @param bytes The number of bytes pushed + * @param payload The user-specified payload callback + * @return 0 or an error code to stop the transfer + */ +typedef int GIT_CALLBACK(git_push_transfer_progress_cb)( + unsigned int current, + unsigned int total, + size_t bytes, + void *payload); /** - * Get a list of the configured remotes for a repo - * - * The string array must be freed by the user. - * - * @param out a string array which receives the names of the remotes - * @param repo the repository to query - * @return 0 or an error code + * Represents an update which will be performed on the remote during push */ -GIT_EXTERN(int) git_remote_list(git_strarray *out, git_repository *repo); +typedef struct { + /** + * The source name of the reference + */ + char *src_refname; + /** + * The name of the reference to update on the server + */ + char *dst_refname; + /** + * The current target of the reference + */ + git_oid src; + /** + * The new target for the reference + */ + git_oid dst; +} git_push_update; /** - * Choose whether to check the server's certificate (applies to HTTPS only) + * Callback used to inform of upcoming updates. * - * @param remote the remote to configure - * @param check whether to check the server's certificate (defaults to yes) + * @param updates an array containing the updates which will be sent + * as commands to the destination. + * @param len number of elements in `updates` + * @param payload Payload provided by the caller + * @return 0 or an error code to stop the push */ -GIT_EXTERN(void) git_remote_check_cert(git_remote *remote, int check); +typedef int GIT_CALLBACK(git_push_negotiation)( + const git_push_update **updates, + size_t len, + void *payload); /** - * Set a credentials acquisition callback for this remote. If the remote is - * not available for anonymous access, then you must set this callback in order - * to provide credentials to the transport at the time of authentication - * failure so that retry can be performed. + * Callback used to inform of the update status from the remote. + * + * Called for each updated reference on push. If `status` is + * not `NULL`, the update was rejected by the remote server + * and `status` contains the reason given. * - * @param remote the remote to configure - * @param cred_acquire_cb The credentials acquisition callback to use (defaults - * to NULL) + * @param refname refname specifying to the remote ref + * @param status status message sent from the remote + * @param data data provided by the caller + * @return 0 on success, otherwise an error */ -GIT_EXTERN(void) git_remote_set_cred_acquire_cb( - git_remote *remote, - git_cred_acquire_cb cred_acquire_cb, - void *payload); +typedef int GIT_CALLBACK(git_push_update_reference_cb)(const char *refname, const char *status, void *data); +#ifndef GIT_DEPRECATE_HARD /** - * Sets a custom transport for the remote. The caller can use this function - * to bypass the automatic discovery of a transport by URL scheme (i.e. - * http://, https://, git://) and supply their own transport to be used - * instead. After providing the transport to a remote using this function, - * the transport object belongs exclusively to that remote, and the remote will - * free it when it is freed with git_remote_free. + * Callback to resolve URLs before connecting to remote * - * @param remote the remote to configure - * @param transport the transport object for the remote to use - * @return 0 or an error code + * If you return GIT_PASSTHROUGH, you don't need to write anything to + * url_resolved. + * + * @param url_resolved The buffer to write the resolved URL to + * @param url The URL to resolve + * @param direction GIT_DIRECTION_FETCH or GIT_DIRECTION_PUSH + * @param payload Payload provided by the caller + * @return 0 on success, GIT_PASSTHROUGH or an error + * @deprecated Use `git_remote_set_instance_url` */ -GIT_EXTERN(int) git_remote_set_transport( - git_remote *remote, - git_transport *transport); +typedef int GIT_CALLBACK(git_url_resolve_cb)(git_buf *url_resolved, const char *url, int direction, void *payload); +#endif /** - * Argument to the completion callback which tells it which operation - * finished. + * Callback invoked immediately before we attempt to connect to the + * given url. Callers may change the URL before the connection by + * calling `git_remote_set_instance_url` in the callback. + * + * @param remote The remote to be connected + * @param direction GIT_DIRECTION_FETCH or GIT_DIRECTION_PUSH + * @param payload Payload provided by the caller + * @return 0 on success, or an error */ -typedef enum git_remote_completion_type { - GIT_REMOTE_COMPLETION_DOWNLOAD, - GIT_REMOTE_COMPLETION_INDEXING, - GIT_REMOTE_COMPLETION_ERROR, -} git_remote_completion_type; +typedef int GIT_CALLBACK(git_remote_ready_cb)(git_remote *remote, int direction, void *payload); /** * The callback settings structure * - * Set the calbacks to be called by the remote. + * Set the callbacks to be called by the remote when informing the user + * about the progress of the network operations. */ struct git_remote_callbacks { - unsigned int version; - void (*progress)(const char *str, int len, void *data); - int (*completion)(git_remote_completion_type type, void *data); - int (*update_tips)(const char *refname, const git_oid *a, const git_oid *b, void *data); + unsigned int version; /**< The version */ + + /** + * Textual progress from the remote. Text send over the + * progress side-band will be passed to this function (this is + * the 'counting objects' output). + */ + git_transport_message_cb sideband_progress; + + /** + * Completion is called when different parts of the download + * process are done (currently unused). + */ + int GIT_CALLBACK(completion)(git_remote_completion_t type, + void *data); + + /** + * This will be called if the remote host requires + * authentication in order to connect to it. + * + * Returning GIT_PASSTHROUGH will make libgit2 behave as + * though this field isn't set. + */ + git_credential_acquire_cb credentials; + + /** + * This will be called to let the user make the final decision of whether + * to allow the connection to proceed. Returns 0 to allow the connection + * or a negative value to indicate an error. + */ + git_transport_certificate_check_cb certificate_check; + + /** + * During the download of new data, this will be regularly + * called with the current count of progress done by the + * indexer. + */ + git_indexer_progress_cb transfer_progress; + +#ifdef GIT_DEPRECATE_HARD + void *reserved_update_tips; +#else + /** + * Deprecated callback for reference updates, callers should + * set `update_refs` instead. This is retained for backward + * compatibility; if you specify both `update_refs` and + * `update_tips`, then only the `update_refs` function will + * be called. + * + * @deprecated the `update_refs` callback in this structure + * should be preferred + */ + int GIT_CALLBACK(update_tips)(const char *refname, + const git_oid *a, const git_oid *b, void *data); +#endif + + /** + * Function to call with progress information during pack + * building. Be aware that this is called inline with pack + * building operations, so performance may be affected. + */ + git_packbuilder_progress pack_progress; + + /** + * Function to call with progress information during the + * upload portion of a push. Be aware that this is called + * inline with pack building operations, so performance may be + * affected. + */ + git_push_transfer_progress_cb push_transfer_progress; + + /** + * See documentation of git_push_update_reference_cb + */ + git_push_update_reference_cb push_update_reference; + + /** + * Called once between the negotiation step and the upload. It + * provides information about what updates will be performed. + */ + git_push_negotiation push_negotiation; + + /** + * Create the transport to use for this operation. Leave NULL + * to auto-detect. + */ + git_transport_cb transport; + + /** + * Callback when the remote is ready to connect. + */ + git_remote_ready_cb remote_ready; + + /** + * This will be passed to each of the callbacks in this struct + * as the last parameter. + */ void *payload; + +#ifdef GIT_DEPRECATE_HARD + void *reserved; +#else + /** + * Resolve URL before connecting to remote. + * The returned URL will be used to connect to the remote instead. + * + * This callback is deprecated; users should use + * git_remote_ready_cb and configure the instance URL instead. + */ + git_url_resolve_cb resolve_url; +#endif + + /** + * Each time a reference is updated locally, this function + * will be called with information about it. This should be + * preferred over the `update_tips` callback in this + * structure. + */ + int GIT_CALLBACK(update_refs)( + const char *refname, + const git_oid *a, + const git_oid *b, + git_refspec *spec, + void *data); }; +/** Current version for the `git_remote_callbacks_options` structure */ #define GIT_REMOTE_CALLBACKS_VERSION 1 + +/** Static constructor for `git_remote_callbacks_options` */ #define GIT_REMOTE_CALLBACKS_INIT {GIT_REMOTE_CALLBACKS_VERSION} /** - * Set the callbacks for a remote - * - * Note that the remote keeps its own copy of the data and you need to - * call this function again if you want to change the callbacks. + * Initializes a `git_remote_callbacks` with default values. Equivalent to + * creating an instance with GIT_REMOTE_CALLBACKS_INIT. * - * @param remote the remote to configure - * @param callbacks a pointer to the user's callback settings - * @return 0 or an error code + * @param opts the `git_remote_callbacks` struct to initialize + * @param version Version of struct; pass `GIT_REMOTE_CALLBACKS_VERSION` + * @return Zero on success; -1 on failure. */ -GIT_EXTERN(int) git_remote_set_callbacks(git_remote *remote, git_remote_callbacks *callbacks); +GIT_EXTERN(int) git_remote_init_callbacks( + git_remote_callbacks *opts, + unsigned int version); + +/** Acceptable prune settings when fetching */ +typedef enum { + /** + * Use the setting from the configuration + */ + GIT_FETCH_PRUNE_UNSPECIFIED, + /** + * Force pruning on + */ + GIT_FETCH_PRUNE, + /** + * Force pruning off + */ + GIT_FETCH_NO_PRUNE +} git_fetch_prune_t; /** - * Get the statistics structure that is filled in by the fetch operation. + * Automatic tag following option + * + * Lets us select the --tags option to use. */ -GIT_EXTERN(const git_transfer_progress *) git_remote_stats(git_remote *remote); - typedef enum { - GIT_REMOTE_DOWNLOAD_TAGS_UNSET, - GIT_REMOTE_DOWNLOAD_TAGS_NONE, + /** + * Use the setting from the configuration. + */ + GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED = 0, + /** + * Ask the server for tags pointing to objects we're already + * downloading. + */ GIT_REMOTE_DOWNLOAD_TAGS_AUTO, + /** + * Don't ask for any tags beyond the refspecs. + */ + GIT_REMOTE_DOWNLOAD_TAGS_NONE, + /** + * Ask for the all the tags. + */ GIT_REMOTE_DOWNLOAD_TAGS_ALL } git_remote_autotag_option_t; +/** Constants for fetch depth (shallowness of fetch). */ +typedef enum { + /** The fetch is "full" (not shallow). This is the default. */ + GIT_FETCH_DEPTH_FULL = 0, + + /** The fetch should "unshallow" and fetch missing data. */ + GIT_FETCH_DEPTH_UNSHALLOW = 2147483647 +} git_fetch_depth_t; + +/** + * Fetch options structure. + * + * Zero out for defaults. Initialize with `GIT_FETCH_OPTIONS_INIT` macro to + * correctly set the `version` field. E.g. + * + * git_fetch_options opts = GIT_FETCH_OPTIONS_INIT; + */ +typedef struct { + int version; + + /** + * Callbacks to use for this fetch operation + */ + git_remote_callbacks callbacks; + + /** + * Whether to perform a prune after the fetch + */ + git_fetch_prune_t prune; + + /** + * How to handle reference updates; see `git_remote_update_flags`. + */ + unsigned int update_fetchhead; + + /** + * Determines how to behave regarding tags on the remote, such + * as auto-downloading tags for objects we're downloading or + * downloading all of them. + * + * The default is to auto-follow tags. + */ + git_remote_autotag_option_t download_tags; + + /** + * Proxy options to use, by default no proxy is used. + */ + git_proxy_options proxy_opts; + + /** + * Depth of the fetch to perform, or `GIT_FETCH_DEPTH_FULL` + * (or `0`) for full history, or `GIT_FETCH_DEPTH_UNSHALLOW` + * to "unshallow" a shallow repository. + * + * The default is full (`GIT_FETCH_DEPTH_FULL` or `0`). + */ + int depth; + + /** + * Whether to allow off-site redirects. If this is not + * specified, the `http.followRedirects` configuration setting + * will be consulted. + */ + git_remote_redirect_t follow_redirects; + + /** + * Extra headers for this fetch operation + */ + git_strarray custom_headers; +} git_fetch_options; + +/** Current version for the `git_fetch_options` structure */ +#define GIT_FETCH_OPTIONS_VERSION 1 + +/** Static constructor for `git_fetch_options` */ +#define GIT_FETCH_OPTIONS_INIT { \ + GIT_FETCH_OPTIONS_VERSION, \ + GIT_REMOTE_CALLBACKS_INIT, \ + GIT_FETCH_PRUNE_UNSPECIFIED, \ + GIT_REMOTE_UPDATE_FETCHHEAD, \ + GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED, \ + GIT_PROXY_OPTIONS_INIT } + +/** + * Initialize git_fetch_options structure + * + * Initializes a `git_fetch_options` with default values. Equivalent to + * creating an instance with `GIT_FETCH_OPTIONS_INIT`. + * + * @param opts The `git_fetch_options` struct to initialize. + * @param version The struct version; pass `GIT_FETCH_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_fetch_options_init( + git_fetch_options *opts, + unsigned int version); + + +/** + * Controls the behavior of a git_push object. + */ +typedef struct { + unsigned int version; + + /** + * If the transport being used to push to the remote requires the creation + * of a pack file, this controls the number of worker threads used by + * the packbuilder when creating that pack file to be sent to the remote. + * + * If set to 0, the packbuilder will auto-detect the number of threads + * to create. The default value is 1. + */ + unsigned int pb_parallelism; + + /** + * Callbacks to use for this push operation + */ + git_remote_callbacks callbacks; + + /** + * Proxy options to use, by default no proxy is used. + */ + git_proxy_options proxy_opts; + + /** + * Whether to allow off-site redirects. If this is not + * specified, the `http.followRedirects` configuration setting + * will be consulted. + */ + git_remote_redirect_t follow_redirects; + + /** + * Extra headers for this push operation + */ + git_strarray custom_headers; + + /** + * "Push options" to deliver to the remote. + */ + git_strarray remote_push_options; +} git_push_options; + +/** Current version for the `git_push_options` structure */ +#define GIT_PUSH_OPTIONS_VERSION 1 + +/** Static constructor for `git_push_options` */ +#define GIT_PUSH_OPTIONS_INIT { GIT_PUSH_OPTIONS_VERSION, 1, GIT_REMOTE_CALLBACKS_INIT, GIT_PROXY_OPTIONS_INIT } + +/** + * Initialize git_push_options structure + * + * Initializes a `git_push_options` with default values. Equivalent to + * creating an instance with `GIT_PUSH_OPTIONS_INIT`. + * + * @param opts The `git_push_options` struct to initialize. + * @param version The struct version; pass `GIT_PUSH_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_push_options_init( + git_push_options *opts, + unsigned int version); + +/** + * Remote creation options structure + * + * Initialize with `GIT_REMOTE_CREATE_OPTIONS_INIT`. Alternatively, you can + * use `git_remote_create_options_init`. + * + */ +typedef struct { + unsigned int version; + + /** Callbacks to use for this connection */ + git_remote_callbacks callbacks; + + /** HTTP Proxy settings */ + git_proxy_options proxy_opts; + + /** + * Whether to allow off-site redirects. If this is not + * specified, the `http.followRedirects` configuration setting + * will be consulted. + */ + git_remote_redirect_t follow_redirects; + + /** Extra HTTP headers to use in this connection */ + git_strarray custom_headers; +} git_remote_connect_options; + +/** Current version for the `git_remote_connect_options` structure */ +#define GIT_REMOTE_CONNECT_OPTIONS_VERSION 1 + +/** Static constructor for `git_remote_connect_options` */ +#define GIT_REMOTE_CONNECT_OPTIONS_INIT { \ + GIT_REMOTE_CONNECT_OPTIONS_VERSION, \ + GIT_REMOTE_CALLBACKS_INIT, \ + GIT_PROXY_OPTIONS_INIT } + +/** + * Initialize git_remote_connect_options structure. + * + * Initializes a `git_remote_connect_options` with default values. + * Equivalent to creating an instance with + * `GIT_REMOTE_CONNECT_OPTIONS_INIT`. + * + * @param opts The `git_remote_connect_options` struct to initialize. + * @param version The struct version; pass `GIT_REMOTE_CONNECT_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_remote_connect_options_init( + git_remote_connect_options *opts, + unsigned int version); + +/** + * Open a connection to a remote. + * + * The transport is selected based on the URL; the direction argument + * is due to a limitation of the git protocol which starts up a + * specific binary which can only do the one or the other. + * + * @param remote the remote to connect to + * @param direction GIT_DIRECTION_FETCH if you want to fetch or + * GIT_DIRECTION_PUSH if you want to push + * @param callbacks the callbacks to use for this connection + * @param proxy_opts proxy settings + * @param custom_headers extra HTTP headers to use in this connection + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_connect( + git_remote *remote, + git_direction direction, + const git_remote_callbacks *callbacks, + const git_proxy_options *proxy_opts, + const git_strarray *custom_headers); + +/** + * Open a connection to a remote with extended options. + * + * The transport is selected based on the URL; the direction argument + * is due to a limitation of the git protocol which starts up a + * specific binary which can only do the one or the other. + * + * The given options structure will form the defaults for connection + * options and callback setup. Callers may override these defaults + * by specifying `git_fetch_options` or `git_push_options` in + * subsequent calls. + * + * @param remote the remote to connect to + * @param direction GIT_DIRECTION_FETCH if you want to fetch or + * GIT_DIRECTION_PUSH if you want to push + * @param opts the remote connection options + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_connect_ext( + git_remote *remote, + git_direction direction, + const git_remote_connect_options *opts); + +/** + * Download and index the packfile. + * + * Connect to the remote if it hasn't been done yet, negotiate with + * the remote git which objects are missing, download and index the + * packfile. + * + * The .idx file will be created and both it and the packfile with be + * renamed to their final name. + * + * If options are specified and this remote is already connected then + * the existing remote connection options will be discarded and the + * remote will now use the new options. + * + * @param remote the remote + * @param refspecs the refspecs to use for this negotiation and + * download. Use NULL or an empty array to use the base refspecs + * @param opts the options to use for this fetch or NULL + * @return 0 or an error code + */ + GIT_EXTERN(int) git_remote_download( + git_remote *remote, + const git_strarray *refspecs, + const git_fetch_options *opts); + +/** + * Create a packfile and send it to the server + * + * Connect to the remote if it hasn't been done yet, negotiate with + * the remote git which objects are missing, create a packfile with + * the missing objects and send it. + * + * If options are specified and this remote is already connected then + * the existing remote connection options will be discarded and the + * remote will now use the new options. + * + * @param remote the remote + * @param refspecs the refspecs to use for this negotiation and + * upload. Use NULL or an empty array to use the base refspecs + * @param opts the options to use for this push + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_upload( + git_remote *remote, + const git_strarray *refspecs, + const git_push_options *opts); + +/** + * Update the tips to the new state. + * + * If callbacks are not specified then the callbacks specified to + * `git_remote_connect` will be used (if it was called). + * + * @param remote the remote to update + * @param callbacks pointer to the callback structure to use or NULL + * @param update_flags the git_remote_update_flags for these tips. + * @param download_tags what the behaviour for downloading tags is for this fetch. This is + * ignored for push. This must be the same value passed to `git_remote_download()`. + * @param reflog_message The message to insert into the reflogs. If + * NULL and fetching, the default is "fetch ", where is + * the name of the remote (or its url, for in-memory remotes). This + * parameter is ignored when pushing. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_update_tips( + git_remote *remote, + const git_remote_callbacks *callbacks, + unsigned int update_flags, + git_remote_autotag_option_t download_tags, + const char *reflog_message); + +/** + * Download new data and update tips. + * + * Convenience function to connect to a remote, download the data, + * disconnect and update the remote-tracking branches. + * + * If options are specified and this remote is already connected then + * the existing remote connection options will be discarded and the + * remote will now use the new options. + * + * @param remote the remote to fetch from + * @param refspecs the refspecs to use for this fetch. Pass NULL or an + * empty array to use the base refspecs. + * @param opts options to use for this fetch or NULL + * @param reflog_message The message to insert into the reflogs. If NULL, the + * default is "fetch" + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_fetch( + git_remote *remote, + const git_strarray *refspecs, + const git_fetch_options *opts, + const char *reflog_message); + +/** + * Prune tracking refs that are no longer present on remote. + * + * If callbacks are not specified then the callbacks specified to + * `git_remote_connect` will be used (if it was called). + * + * @param remote the remote to prune + * @param callbacks callbacks to use for this prune + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_prune( + git_remote *remote, + const git_remote_callbacks *callbacks); + +/** + * Perform a push. + * + * If options are specified and this remote is already connected then + * the existing remote connection options will be discarded and the + * remote will now use the new options. + * + * @param remote the remote to push to + * @param refspecs the refspecs to use for pushing. If NULL or an empty + * array, the configured refspecs will be used + * @param opts options to use for this push + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_remote_push( + git_remote *remote, + const git_strarray *refspecs, + const git_push_options *opts); + +/** + * Get the statistics structure that is filled in by the fetch operation. + * + * @param remote the remote to get statistics for + * @return the git_indexer_progress for the remote + */ +GIT_EXTERN(const git_indexer_progress *) git_remote_stats(git_remote *remote); + /** * Retrieve the tag auto-follow setting * * @param remote the remote to query * @return the auto-follow setting */ -GIT_EXTERN(git_remote_autotag_option_t) git_remote_autotag(git_remote *remote); +GIT_EXTERN(git_remote_autotag_option_t) git_remote_autotag(const git_remote *remote); /** - * Set the tag auto-follow setting + * Set the remote's tag following setting. * - * @param remote the remote to configure - * @param value a GIT_REMOTE_DOWNLOAD_TAGS value + * The change will be made in the configuration. No loaded remotes + * will be affected. + * + * @param repo the repository in which to make the change + * @param remote the name of the remote + * @param value the new value to take. + * @return 0, or an error code. */ -GIT_EXTERN(void) git_remote_set_autotag( - git_remote *remote, - git_remote_autotag_option_t value); +GIT_EXTERN(int) git_remote_set_autotag(git_repository *repo, const char *remote, git_remote_autotag_option_t value); + +/** + * Retrieve the ref-prune setting + * + * @param remote the remote to query + * @return the ref-prune setting + */ +GIT_EXTERN(int) git_remote_prune_refs(const git_remote *remote); /** * Give the remote a new name @@ -418,38 +1199,63 @@ GIT_EXTERN(void) git_remote_set_autotag( * The new name will be checked for validity. * See `git_tag_create()` for rules about valid names. * - * A temporary in-memory remote cannot be given a name with this method. + * No loaded instances of a the remote with the old name will change + * their name or their list of refspecs. * - * @param remote the remote to rename + * @param problems non-default refspecs cannot be renamed and will be + * stored here for further processing by the caller. Always free this + * strarray on successful return. + * @param repo the repository in which to rename + * @param name the current name of the remote * @param new_name the new name the remote should bear - * @param callback Optional callback to notify the consumer of fetch refspecs - * that haven't been automatically updated and need potential manual tweaking. - * @param payload Additional data to pass to the callback * @return 0, GIT_EINVALIDSPEC, GIT_EEXISTS or an error code */ GIT_EXTERN(int) git_remote_rename( - git_remote *remote, - const char *new_name, - git_remote_rename_problem_cb callback, - void *payload); + git_strarray *problems, + git_repository *repo, + const char *name, + const char *new_name); /** - * Retrieve the update FETCH_HEAD setting. + * Ensure the remote name is well-formed. * - * @param remote the remote to query - * @return the update FETCH_HEAD setting + * @param valid output pointer to set with validity of given remote name + * @param remote_name name to be checked. + * @return 0 on success or an error code */ -GIT_EXTERN(int) git_remote_update_fetchhead(git_remote *remote); +GIT_EXTERN(int) git_remote_name_is_valid(int *valid, const char *remote_name); + +/** +* Delete an existing persisted remote. +* +* All remote-tracking branches and configuration settings +* for the remote will be removed. +* +* @param repo the repository in which to act +* @param name the name of the remote to delete +* @return 0 on success, or an error code. +*/ +GIT_EXTERN(int) git_remote_delete(git_repository *repo, const char *name); /** - * Sets the update FETCH_HEAD setting. By default, FETCH_HEAD will be - * updated on every fetch. Set to 0 to disable. + * Retrieve the name of the remote's default branch + * + * The default branch of a repository is the branch which HEAD points + * to. If the remote does not support reporting this information + * directly, it performs the guess as git does; that is, if there are + * multiple branches which point to the same commit, the first one is + * chosen. If the master branch is a candidate, it wins. + * + * This function must only be called after connecting. * - * @param remote the remote to configure - * @param value 0 to disable updating FETCH_HEAD + * @param out the buffer in which to store the reference name + * @param remote the remote + * @return 0, GIT_ENOTFOUND if the remote does not have any references + * or none of them point to HEAD's commit, or an error message. */ -GIT_EXTERN(void) git_remote_set_update_fetchhead(git_remote *remote, int value); +GIT_EXTERN(int) git_remote_default_branch(git_buf *out, git_remote *remote); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/repository.h b/include/git2/repository.h index e207e5bb5c9..62faefa621f 100644 --- a/include/git2/repository.h +++ b/include/git2/repository.h @@ -10,11 +10,15 @@ #include "common.h" #include "types.h" #include "oid.h" +#include "odb.h" +#include "refdb.h" +#include "buffer.h" +#include "commit.h" /** * @file git2/repository.h - * @brief Git repository management routines - * @defgroup git_repository Git repository management routines + * @brief The repository stores revisions for a source tree + * @defgroup git_repository The repository stores revisions for a source tree * @ingroup Git * @{ */ @@ -29,11 +33,26 @@ GIT_BEGIN_DECL * The method will automatically detect if 'path' is a normal * or bare repository or fail is 'path' is neither. * - * @param out pointer to the repo which will be opened + * Note that the libgit2 library _must_ be initialized using + * `git_libgit2_init` before any APIs can be called, including + * this one. + * + * @param[out] out pointer to the repo which will be opened * @param path the path to the repository * @return 0 or an error code */ GIT_EXTERN(int) git_repository_open(git_repository **out, const char *path); +/** + * Open working tree as a repository + * + * Open the working directory of the working tree as a normal + * repository that can then be worked on. + * + * @param out Output pointer containing opened repository + * @param wt Working tree to open + * @return 0 or an error code + */ +GIT_EXTERN(int) git_repository_open_from_worktree(git_repository **out, git_worktree *wt); /** * Create a "fake" repository to wrap an object database @@ -46,7 +65,9 @@ GIT_EXTERN(int) git_repository_open(git_repository **out, const char *path); * @param odb the object database to wrap * @return 0 or an error code */ -GIT_EXTERN(int) git_repository_wrap_odb(git_repository **out, git_odb *odb); +GIT_EXTERN(int) git_repository_wrap_odb( + git_repository **out, + git_odb *odb); /** * Look for a git repository and copy its path in the given buffer. @@ -58,10 +79,12 @@ GIT_EXTERN(int) git_repository_wrap_odb(git_repository **out, git_odb *odb); * The method will automatically detect if the repository is bare * (if there is a repository). * - * @param path_out The user allocated buffer which will - * contain the found path. + * Note that the libgit2 library _must_ be initialized using + * `git_libgit2_init` before any APIs can be called, including + * this one. * - * @param path_size repository_path size + * @param out A pointer to a user-allocated git_buf which will contain + * the found path. * * @param start_path The base path where the lookup starts. * @@ -77,38 +100,77 @@ GIT_EXTERN(int) git_repository_wrap_odb(git_repository **out, git_odb *odb); * @return 0 or an error code */ GIT_EXTERN(int) git_repository_discover( - char *path_out, - size_t path_size, + git_buf *out, const char *start_path, int across_fs, const char *ceiling_dirs); /** * Option flags for `git_repository_open_ext`. - * - * * GIT_REPOSITORY_OPEN_NO_SEARCH - Only open the repository if it can be - * immediately found in the start_path. Do not walk up from the - * start_path looking at parent directories. - * * GIT_REPOSITORY_OPEN_CROSS_FS - Unless this flag is set, open will not - * continue searching across filesystem boundaries (i.e. when `st_dev` - * changes from the `stat` system call). (E.g. Searching in a user's home - * directory "/home/user/source/" will not return "/.git/" as the found - * repo if "/" is a different filesystem than "/home".) */ typedef enum { + /** + * Only open the repository if it can be immediately found in the + * start_path. Do not walk up from the start_path looking at parent + * directories. + */ GIT_REPOSITORY_OPEN_NO_SEARCH = (1 << 0), + + /** + * Unless this flag is set, open will not continue searching across + * filesystem boundaries (i.e. when `st_dev` changes from the `stat` + * system call). For example, searching in a user's home directory at + * "/home/user/source/" will not return "/.git/" as the found repo if + * "/" is a different filesystem than "/home". + */ GIT_REPOSITORY_OPEN_CROSS_FS = (1 << 1), + + /** + * Open repository as a bare repo regardless of core.bare config, and + * defer loading config file for faster setup. + * Unlike `git_repository_open_bare`, this can follow gitlinks. + */ + GIT_REPOSITORY_OPEN_BARE = (1 << 2), + + /** + * Do not check for a repository by appending /.git to the start_path; + * only open the repository if start_path itself points to the git + * directory. + */ + GIT_REPOSITORY_OPEN_NO_DOTGIT = (1 << 3), + + /** + * Find and open a git repository, respecting the environment variables + * used by the git command-line tools. + * If set, `git_repository_open_ext` will ignore the other flags and + * the `ceiling_dirs` argument, and will allow a NULL `path` to use + * `GIT_DIR` or search from the current directory. + * The search for a repository will respect $GIT_CEILING_DIRECTORIES and + * $GIT_DISCOVERY_ACROSS_FILESYSTEM. The opened repository will + * respect $GIT_INDEX_FILE, $GIT_NAMESPACE, $GIT_OBJECT_DIRECTORY, and + * $GIT_ALTERNATE_OBJECT_DIRECTORIES. + * In the future, this flag will also cause `git_repository_open_ext` + * to respect $GIT_WORK_TREE and $GIT_COMMON_DIR; currently, + * `git_repository_open_ext` with this flag will error out if either + * $GIT_WORK_TREE or $GIT_COMMON_DIR is set. + */ + GIT_REPOSITORY_OPEN_FROM_ENV = (1 << 4) } git_repository_open_flag_t; /** * Find and open a repository with extended controls. * - * @param out Pointer to the repo which will be opened. This can + * Note that the libgit2 library _must_ be initialized using + * `git_libgit2_init` before any APIs can be called, including + * this one. + * + * @param[out] out Pointer to the repo which will be opened. This can * actually be NULL if you only want to use the error code to * see if a repo at this path could be opened. * @param path Path to open as git repository. If the flags * permit "searching", then this can be a path to a subdirectory - * inside the working directory of the repository. + * inside the working directory of the repository. May be NULL if + * flags is GIT_REPOSITORY_OPEN_FROM_ENV. * @param flags A combination of the GIT_REPOSITORY_OPEN flags above. * @param ceiling_dirs A GIT_PATH_LIST_SEPARATOR delimited list of path * prefixes at which the search for a containing repository should @@ -123,6 +185,23 @@ GIT_EXTERN(int) git_repository_open_ext( unsigned int flags, const char *ceiling_dirs); +/** + * Open a bare repository on the serverside. + * + * This is a fast open for bare repositories that will come in handy + * if you're e.g. hosting git repositories and need to access them + * efficiently + * + * Note that the libgit2 library _must_ be initialized using + * `git_libgit2_init` before any APIs can be called, including + * this one. + * + * @param[out] out Pointer to the repo which will be opened. + * @param bare_path Direct path to the bare repository + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_repository_open_bare(git_repository **out, const char *bare_path); + /** * Free a previously allocated repository * @@ -142,7 +221,11 @@ GIT_EXTERN(void) git_repository_free(git_repository *repo); * TODO: * - Reinit the repository * - * @param out pointer to the repo which will be created or reinitialized + * Note that the libgit2 library _must_ be initialized using + * `git_libgit2_init` before any APIs can be called, including + * this one. + * + * @param[out] out pointer to the repo which will be created or reinitialized * @param path the path to the repository * @param is_bare if true, a Git repository without a working directory is * created at the pointed path. If false, provided path will be @@ -161,34 +244,48 @@ GIT_EXTERN(int) git_repository_init( * * These flags configure extra behaviors to `git_repository_init_ext`. * In every case, the default behavior is the zero value (i.e. flag is - * not set). Just OR the flag values together for the `flags` parameter - * when initializing a new repo. Details of individual values are: - * - * * BARE - Create a bare repository with no working directory. - * * NO_REINIT - Return an EEXISTS error if the repo_path appears to - * already be an git repository. - * * NO_DOTGIT_DIR - Normally a "/.git/" will be appended to the repo - * path for non-bare repos (if it is not already there), but - * passing this flag prevents that behavior. - * * MKDIR - Make the repo_path (and workdir_path) as needed. Init is - * always willing to create the ".git" directory even without this - * flag. This flag tells init to create the trailing component of - * the repo and workdir paths as needed. - * * MKPATH - Recursively make all components of the repo and workdir - * paths as necessary. - * * EXTERNAL_TEMPLATE - libgit2 normally uses internal templates to - * initialize a new repo. This flags enables external templates, - * looking the "template_path" from the options if set, or the - * `init.templatedir` global config if not, or falling back on - * "/usr/share/git-core/templates" if it exists. + * not set). Just OR the flag values together for the `flags` parameter + * when initializing a new repo. */ typedef enum { + /** + * Create a bare repository with no working directory. + */ GIT_REPOSITORY_INIT_BARE = (1u << 0), + + /** + * Return an GIT_EEXISTS error if the repo_path appears to already be + * an git repository. + */ GIT_REPOSITORY_INIT_NO_REINIT = (1u << 1), - GIT_REPOSITORY_INIT_NO_DOTGIT_DIR = (1u << 2), + + /** + * Make the repo_path (and workdir_path) as needed. Init is always willing + * to create the ".git" directory even without this flag. This flag tells + * init to create the trailing component of the repo and workdir paths + * as needed. + */ GIT_REPOSITORY_INIT_MKDIR = (1u << 3), + + /** + * Recursively make all components of the repo and workdir paths as + * necessary. + */ GIT_REPOSITORY_INIT_MKPATH = (1u << 4), + + /** + * libgit2 normally uses internal templates to initialize a new repo. + * This flags enables external templates, looking the "template_path" from + * the options if set, or the `init.templatedir` global config if not, + * or falling back on "/usr/share/git-core/templates" if it exists. + */ GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE = (1u << 5), + + /** + * If an alternate workdir is specified, use relative paths for the gitdir + * and core.worktree. + */ + GIT_REPOSITORY_INIT_RELATIVE_GITLINK = (1u << 6) } git_repository_init_flag_t; /** @@ -196,62 +293,119 @@ typedef enum { * * Set the mode field of the `git_repository_init_options` structure * either to the custom mode that you would like, or to one of the - * following modes: - * - * * SHARED_UMASK - Use permissions configured by umask - the default. - * * SHARED_GROUP - Use "--shared=group" behavior, chmod'ing the new repo - * to be group writable and "g+sx" for sticky group assignment. - * * SHARED_ALL - Use "--shared=all" behavior, adding world readability. - * * Anything else - Set to custom value. + * defined modes. */ typedef enum { + /** + * Use permissions configured by umask - the default. + */ GIT_REPOSITORY_INIT_SHARED_UMASK = 0, + + /** + * Use "--shared=group" behavior, chmod'ing the new repo to be group + * writable and "g+sx" for sticky group assignment. + */ GIT_REPOSITORY_INIT_SHARED_GROUP = 0002775, - GIT_REPOSITORY_INIT_SHARED_ALL = 0002777, + + /** + * Use "--shared=all" behavior, adding world readability. + */ + GIT_REPOSITORY_INIT_SHARED_ALL = 0002777 } git_repository_init_mode_t; /** * Extended options structure for `git_repository_init_ext`. * * This contains extra options for `git_repository_init_ext` that enable - * additional initialization features. The fields are: - * - * * flags - Combination of GIT_REPOSITORY_INIT flags above. - * * mode - Set to one of the standard GIT_REPOSITORY_INIT_SHARED_... - * constants above, or to a custom value that you would like. - * * workdir_path - The path to the working dir or NULL for default (i.e. - * repo_path parent on non-bare repos). IF THIS IS RELATIVE PATH, - * IT WILL BE EVALUATED RELATIVE TO THE REPO_PATH. If this is not - * the "natural" working directory, a .git gitlink file will be - * created here linking to the repo_path. - * * description - If set, this will be used to initialize the "description" - * file in the repository, instead of using the template content. - * * template_path - When GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE is set, - * this contains the path to use for the template directory. If - * this is NULL, the config or default directory options will be - * used instead. - * * initial_head - The name of the head to point HEAD at. If NULL, then - * this will be treated as "master" and the HEAD ref will be set - * to "refs/heads/master". If this begins with "refs/" it will be - * used verbatim; otherwise "refs/heads/" will be prefixed. - * * origin_url - If this is non-NULL, then after the rest of the - * repository initialization is completed, an "origin" remote - * will be added pointing to this URL. + * additional initialization features. */ typedef struct { unsigned int version; + + /** + * Combination of GIT_REPOSITORY_INIT flags above. + */ uint32_t flags; + + /** + * Set to one of the standard GIT_REPOSITORY_INIT_SHARED_... constants + * above, or to a custom value that you would like. + */ uint32_t mode; + + /** + * The path to the working dir or NULL for default (i.e. repo_path parent + * on non-bare repos). IF THIS IS RELATIVE PATH, IT WILL BE EVALUATED + * RELATIVE TO THE REPO_PATH. If this is not the "natural" working + * directory, a .git gitlink file will be created here linking to the + * repo_path. + */ const char *workdir_path; + + /** + * If set, this will be used to initialize the "description" file in the + * repository, instead of using the template content. + */ const char *description; + + /** + * When GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE is set, this contains + * the path to use for the template directory. If this is NULL, the config + * or default directory options will be used instead. + */ const char *template_path; + + /** + * The name of the head to point HEAD at. If NULL, then this will be + * treated as "master" and the HEAD ref will be set to "refs/heads/master". + * If this begins with "refs/" it will be used verbatim; + * otherwise "refs/heads/" will be prefixed. + */ const char *initial_head; + + /** + * If this is non-NULL, then after the rest of the repository + * initialization is completed, an "origin" remote will be added + * pointing to this URL. + */ const char *origin_url; + +#ifdef GIT_EXPERIMENTAL_SHA256 + /** + * + * Type of object IDs to use for this repository, or 0 for + * default (currently SHA1). + */ + git_oid_t oid_type; +#endif + + /** + * Type of the reference database to use for this repository, or 0 for + * default (currently "files"). + */ + git_refdb_t refdb_type; } git_repository_init_options; +/** Current version for the `git_repository_init_options` structure */ #define GIT_REPOSITORY_INIT_OPTIONS_VERSION 1 + +/** Static constructor for `git_repository_init_options` */ #define GIT_REPOSITORY_INIT_OPTIONS_INIT {GIT_REPOSITORY_INIT_OPTIONS_VERSION} +/** + * Initialize git_repository_init_options structure + * + * Initializes a `git_repository_init_options` with default values. Equivalent to + * creating an instance with `GIT_REPOSITORY_INIT_OPTIONS_INIT`. + * + * @param opts The `git_repository_init_options` struct to initialize. + * @param version The struct version; pass `GIT_REPOSITORY_INIT_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_repository_init_options_init( + git_repository_init_options *opts, + unsigned int version); + /** * Create a new Git repository in the given folder with extended controls. * @@ -260,6 +414,10 @@ typedef struct { * auto-detect the case sensitivity of the file system and if the * file system supports file mode bits correctly. * + * Note that the libgit2 library _must_ be initialized using + * `git_libgit2_init` before any APIs can be called, including + * this one. + * * @param out Pointer to the repo which will be created or reinitialized. * @param repo_path The path to the repository. * @param opts Pointer to git_repository_init_options struct. @@ -277,14 +435,25 @@ GIT_EXTERN(int) git_repository_init_ext( * `git_reference_free()` must be called when done with it to release the * allocated memory and prevent a leak. * - * @param out pointer to the reference which will be retrieved + * @param[out] out pointer to the reference which will be retrieved * @param repo a repository object * - * @return 0 on success, GIT_EORPHANEDHEAD when HEAD points to a non existing + * @return 0 on success, GIT_EUNBORNBRANCH when HEAD points to a non existing * branch, GIT_ENOTFOUND when HEAD is missing; an error code otherwise */ GIT_EXTERN(int) git_repository_head(git_reference **out, git_repository *repo); +/** + * Retrieve the referenced HEAD for the worktree + * + * @param out pointer to the reference which will be retrieved + * @param repo a repository object + * @param name name of the worktree to retrieve HEAD for + * @return 0 when successful, error-code otherwise + */ +GIT_EXTERN(int) git_repository_head_for_worktree(git_reference **out, git_repository *repo, + const char *name); + /** * Check if a repository's HEAD is detached * @@ -298,22 +467,38 @@ GIT_EXTERN(int) git_repository_head(git_reference **out, git_repository *repo); GIT_EXTERN(int) git_repository_head_detached(git_repository *repo); /** - * Check if the current branch is an orphan + * Check if a worktree's HEAD is detached + * + * A worktree's HEAD is detached when it points directly to a + * commit instead of a branch. * - * An orphan branch is one named from HEAD but which doesn't exist in + * @param repo a repository object + * @param name name of the worktree to retrieve HEAD for + * @return 1 if HEAD is detached, 0 if its not; error code if + * there was an error + */ +GIT_EXTERN(int) git_repository_head_detached_for_worktree(git_repository *repo, + const char *name); + +/** + * Check if the current branch is unborn + * + * An unborn branch is one named from HEAD but which doesn't exist in * the refs namespace, because it doesn't have any commit to point to. * * @param repo Repo to test - * @return 1 if the current branch is an orphan, 0 if it's not; error + * @return 1 if the current branch is unborn, 0 if it's not; error * code if there was an error */ -GIT_EXTERN(int) git_repository_head_orphan(git_repository *repo); +GIT_EXTERN(int) git_repository_head_unborn(git_repository *repo); /** * Check if a repository is empty * - * An empty repository has just been initialized and contains - * no references. + * An empty repository has just been initialized and contains no references + * apart from HEAD, which must be pointing to the unborn master branch, + * or the branch specified for the repository in the `init.defaultBranch` + * configuration variable. * * @param repo Repo to test * @return 1 if the repository is empty, 0 if it isn't, error code @@ -321,6 +506,44 @@ GIT_EXTERN(int) git_repository_head_orphan(git_repository *repo); */ GIT_EXTERN(int) git_repository_is_empty(git_repository *repo); +/** + * List of items which belong to the git repository layout + */ +typedef enum { + GIT_REPOSITORY_ITEM_GITDIR, + GIT_REPOSITORY_ITEM_WORKDIR, + GIT_REPOSITORY_ITEM_COMMONDIR, + GIT_REPOSITORY_ITEM_INDEX, + GIT_REPOSITORY_ITEM_OBJECTS, + GIT_REPOSITORY_ITEM_REFS, + GIT_REPOSITORY_ITEM_PACKED_REFS, + GIT_REPOSITORY_ITEM_REMOTES, + GIT_REPOSITORY_ITEM_CONFIG, + GIT_REPOSITORY_ITEM_INFO, + GIT_REPOSITORY_ITEM_HOOKS, + GIT_REPOSITORY_ITEM_LOGS, + GIT_REPOSITORY_ITEM_MODULES, + GIT_REPOSITORY_ITEM_WORKTREES, + GIT_REPOSITORY_ITEM_WORKTREE_CONFIG, + GIT_REPOSITORY_ITEM__LAST +} git_repository_item_t; + +/** + * Get the location of a specific repository file or directory + * + * This function will retrieve the path of a specific repository + * item. It will thereby honor things like the repository's + * common directory, gitdir, etc. In case a file path cannot + * exist for a given item (e.g. the working directory of a bare + * repository), GIT_ENOTFOUND is returned. + * + * @param out Buffer to store the path at + * @param repo Repository to get path for + * @param item The repository item for which to retrieve the path + * @return 0, GIT_ENOTFOUND if the path cannot exist or an error code + */ +GIT_EXTERN(int) git_repository_item_path(git_buf *out, const git_repository *repo, git_repository_item_t item); + /** * Get the path of this repository * @@ -330,7 +553,7 @@ GIT_EXTERN(int) git_repository_is_empty(git_repository *repo); * @param repo A repository object * @return the path to the repository */ -GIT_EXTERN(const char *) git_repository_path(git_repository *repo); +GIT_EXTERN(const char *) git_repository_path(const git_repository *repo); /** * Get the path of the working directory for this repository @@ -341,7 +564,19 @@ GIT_EXTERN(const char *) git_repository_path(git_repository *repo); * @param repo A repository object * @return the path to the working dir, if it exists */ -GIT_EXTERN(const char *) git_repository_workdir(git_repository *repo); +GIT_EXTERN(const char *) git_repository_workdir(const git_repository *repo); + +/** + * Get the path of the shared common directory for this repository. + * + * If the repository is bare, it is the root directory for the repository. + * If the repository is a worktree, it is the parent repo's gitdir. + * Otherwise, it is the gitdir. + * + * @param repo A repository object + * @return the path to the common dir + */ +GIT_EXTERN(const char *) git_repository_commondir(const git_repository *repo); /** * Set the path to the working directory for this repository @@ -369,7 +604,15 @@ GIT_EXTERN(int) git_repository_set_workdir( * @param repo Repo to test * @return 1 if the repository is bare, 0 otherwise. */ -GIT_EXTERN(int) git_repository_is_bare(git_repository *repo); +GIT_EXTERN(int) git_repository_is_bare(const git_repository *repo); + +/** + * Check if a repository is a linked work tree + * + * @param repo Repo to test + * @return 1 if the repository is a linked work tree, 0 otherwise. + */ +GIT_EXTERN(int) git_repository_is_worktree(const git_repository *repo); /** * Get the configuration file for this repository. @@ -381,26 +624,27 @@ GIT_EXTERN(int) git_repository_is_bare(git_repository *repo); * The configuration file must be freed once it's no longer * being used by the user. * - * @param out Pointer to store the loaded config file + * @param out Pointer to store the loaded configuration * @param repo A repository object * @return 0, or an error code */ GIT_EXTERN(int) git_repository_config(git_config **out, git_repository *repo); /** - * Set the configuration file for this repository + * Get a snapshot of the repository's configuration * - * This configuration file will be used for all configuration - * queries involving this repository. + * Convenience function to take a snapshot from the repository's + * configuration. The contents of this snapshot will not change, + * even if the underlying config files are modified. * - * The repository will keep a reference to the config file; - * the user must still free the config after setting it - * to the repository, or it will leak. + * The configuration file must be freed once it's no longer + * being used by the user. * - * @param repo A repository object - * @param config A Config object + * @param out Pointer to store the loaded configuration + * @param repo the repository + * @return 0, or an error code */ -GIT_EXTERN(void) git_repository_set_config(git_repository *repo, git_config *config); +GIT_EXTERN(int) git_repository_config_snapshot(git_config **out, git_repository *repo); /** * Get the Object Database for this repository. @@ -412,26 +656,27 @@ GIT_EXTERN(void) git_repository_set_config(git_repository *repo, git_config *con * The ODB must be freed once it's no longer being used by * the user. * - * @param out Pointer to store the loaded ODB + * @param[out] out Pointer to store the loaded ODB * @param repo A repository object * @return 0, or an error code */ GIT_EXTERN(int) git_repository_odb(git_odb **out, git_repository *repo); /** - * Set the Object Database for this repository + * Get the Reference Database Backend for this repository. * - * The ODB will be used for all object-related operations - * involving this repository. + * If a custom refsdb has not been set, the default database for + * the repository will be returned (the one that manipulates loose + * and packed references in the `.git` directory). * - * The repository will keep a reference to the ODB; the user - * must still free the ODB object after setting it to the - * repository, or it will leak. + * The refdb must be freed once it's no longer being used by + * the user. * + * @param[out] out Pointer to store the loaded refdb * @param repo A repository object - * @param odb An ODB object + * @return 0, or an error code */ -GIT_EXTERN(void) git_repository_set_odb(git_repository *repo, git_odb *odb); +GIT_EXTERN(int) git_repository_refdb(git_refdb **out, git_repository *repo); /** * Get the Index file for this repository. @@ -443,27 +688,12 @@ GIT_EXTERN(void) git_repository_set_odb(git_repository *repo, git_odb *odb); * The index must be freed once it's no longer being used by * the user. * - * @param out Pointer to store the loaded index + * @param[out] out Pointer to store the loaded index * @param repo A repository object * @return 0, or an error code */ GIT_EXTERN(int) git_repository_index(git_index **out, git_repository *repo); -/** - * Set the index file for this repository - * - * This index will be used for all index-related operations - * involving this repository. - * - * The repository will keep a reference to the index file; - * the user must still free the index after setting it - * to the repository, or it will leak. - * - * @param repo A repository object - * @param index An index object - */ -GIT_EXTERN(void) git_repository_set_index(git_repository *repo, git_index *index); - /** * Retrieve git's prepared message * @@ -476,60 +706,91 @@ GIT_EXTERN(void) git_repository_set_index(git_repository *repo, git_index *index * Use this function to get the contents of this file. Don't forget to * remove the file after you create the commit. * - * @param out Buffer to write data into or NULL to just read required size - * @param len Length of buffer in bytes + * @param out git_buf to write data into * @param repo Repository to read prepared message from - * @return Bytes written to buffer, GIT_ENOTFOUND if no message, or -1 on error + * @return 0, GIT_ENOTFOUND if no message exists or an error code */ -GIT_EXTERN(int) git_repository_message(char *out, size_t len, git_repository *repo); +GIT_EXTERN(int) git_repository_message(git_buf *out, git_repository *repo); /** * Remove git's prepared message. * * Remove the message that `git_repository_message` retrieves. + * + * @param repo Repository to remove prepared message from. + * @return 0 or an error code. */ GIT_EXTERN(int) git_repository_message_remove(git_repository *repo); /** - * Remove all the metadata associated with an ongoing git merge, including - * MERGE_HEAD, MERGE_MSG, etc. + * Remove all the metadata associated with an ongoing command like merge, + * revert, cherry-pick, etc. For example: MERGE_HEAD, MERGE_MSG, etc. * * @param repo A repository object * @return 0 on success, or error */ -GIT_EXTERN(int) git_repository_merge_cleanup(git_repository *repo); +GIT_EXTERN(int) git_repository_state_cleanup(git_repository *repo); -typedef int (*git_repository_fetchhead_foreach_cb)(const char *ref_name, +/** + * Callback used to iterate over each FETCH_HEAD entry + * + * @see git_repository_fetchhead_foreach + * + * @param ref_name The reference name + * @param remote_url The remote URL + * @param oid The reference target OID + * @param is_merge Was the reference the result of a merge + * @param payload Payload passed to git_repository_fetchhead_foreach + * @return non-zero to terminate the iteration + */ +typedef int GIT_CALLBACK(git_repository_fetchhead_foreach_cb)(const char *ref_name, const char *remote_url, const git_oid *oid, unsigned int is_merge, void *payload); /** - * Call callback 'callback' for each entry in the given FETCH_HEAD file. + * Invoke 'callback' for each entry in the given FETCH_HEAD file. + * + * Return a non-zero value from the callback to stop the loop. * * @param repo A repository object * @param callback Callback function * @param payload Pointer to callback data (optional) - * @return 0 on success, GIT_ENOTFOUND, GIT_EUSER or error + * @return 0 on success, non-zero callback return value, GIT_ENOTFOUND if + * there is no FETCH_HEAD file, or other error code. */ -GIT_EXTERN(int) git_repository_fetchhead_foreach(git_repository *repo, +GIT_EXTERN(int) git_repository_fetchhead_foreach( + git_repository *repo, git_repository_fetchhead_foreach_cb callback, void *payload); -typedef int (*git_repository_mergehead_foreach_cb)(const git_oid *oid, +/** + * Callback used to iterate over each MERGE_HEAD entry + * + * @see git_repository_mergehead_foreach + * + * @param oid The merge OID + * @param payload Payload passed to git_repository_mergehead_foreach + * @return non-zero to terminate the iteration + */ +typedef int GIT_CALLBACK(git_repository_mergehead_foreach_cb)(const git_oid *oid, void *payload); /** - * If a merge is in progress, call callback 'cb' for each commit ID in the + * If a merge is in progress, invoke 'callback' for each commit ID in the * MERGE_HEAD file. * + * Return a non-zero value from the callback to stop the loop. + * * @param repo A repository object * @param callback Callback function - * @param apyload Pointer to callback data (optional) - * @return 0 on success, GIT_ENOTFOUND, GIT_EUSER or error + * @param payload Pointer to callback data (optional) + * @return 0 on success, non-zero callback return value, GIT_ENOTFOUND if + * there is no MERGE_HEAD file, or other error code. */ -GIT_EXTERN(int) git_repository_mergehead_foreach(git_repository *repo, +GIT_EXTERN(int) git_repository_mergehead_foreach( + git_repository *repo, git_repository_mergehead_foreach_cb callback, void *payload); @@ -541,22 +802,29 @@ GIT_EXTERN(int) git_repository_mergehead_foreach(git_repository *repo, * hash a file in the repository and you want to apply filtering rules (e.g. * crlf filters) before generating the SHA, then use this function. * + * Note: if the repository has `core.safecrlf` set to fail and the + * filtering triggers that failure, then this function will return an + * error and not calculate the hash of the file. + * * @param out Output value of calculated SHA * @param repo Repository pointer - * @param path Path to file on disk whose contents should be hashed. If the - * repository is not NULL, this can be a relative path. - * @param type The object type to hash as (e.g. GIT_OBJ_BLOB) + * @param path Path to file on disk whose contents should be hashed. This + * may be an absolute path or a relative path, in which case it + * will be treated as a path within the working directory. + * @param type The object type to hash as (e.g. GIT_OBJECT_BLOB) * @param as_path The path to use to look up filtering rules. If this is - * NULL, then the `path` parameter will be used instead. If - * this is passed as the empty string, then no filters will be - * applied when calculating the hash. + * an empty string then no filters will be applied when + * calculating the hash. If this is `NULL` and the `path` + * parameter is a file within the repository's working + * directory, then the `path` will be used. + * @return 0 on success, or an error code */ GIT_EXTERN(int) git_repository_hashfile( - git_oid *out, - git_repository *repo, - const char *path, - git_otype type, - const char *as_path); + git_oid *out, + git_repository *repo, + const char *path, + git_object_t type, + const char *as_path); /** * Make the repository HEAD point to the specified reference. @@ -577,8 +845,8 @@ GIT_EXTERN(int) git_repository_hashfile( * @return 0 on success, or an error code */ GIT_EXTERN(int) git_repository_set_head( - git_repository* repo, - const char* refname); + git_repository *repo, + const char *refname); /** * Make the repository HEAD directly point to the Commit. @@ -586,19 +854,37 @@ GIT_EXTERN(int) git_repository_set_head( * If the provided committish cannot be found in the repository, the HEAD * is unaltered and GIT_ENOTFOUND is returned. * - * If the provided commitish cannot be peeled into a commit, the HEAD + * If the provided committish cannot be peeled into a commit, the HEAD * is unaltered and -1 is returned. * * Otherwise, the HEAD will eventually be detached and will directly point to * the peeled Commit. * * @param repo Repository pointer - * @param commitish Object id of the Commit the HEAD should point to + * @param committish Object id of the Commit the HEAD should point to * @return 0 on success, or an error code */ GIT_EXTERN(int) git_repository_set_head_detached( - git_repository* repo, - const git_oid* commitish); + git_repository *repo, + const git_oid *committish); + +/** + * Make the repository HEAD directly point to the Commit. + * + * This behaves like `git_repository_set_head_detached()` but takes an + * annotated commit, which lets you specify which extended sha syntax + * string was specified by a user, allowing for more exact reflog + * messages. + * + * See the documentation for `git_repository_set_head_detached()`. + * + * @param repo Repository pointer + * @param committish annotated commit to point HEAD to + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_repository_set_head_detached_from_annotated( + git_repository *repo, + const git_annotated_commit *committish); /** * Detach the HEAD. @@ -608,29 +894,37 @@ GIT_EXTERN(int) git_repository_set_head_detached( * If the HEAD is already detached and points to a Tag, the HEAD is * updated into making it point to the peeled Commit, and 0 is returned. * - * If the HEAD is already detached and points to a non commitish, the HEAD is + * If the HEAD is already detached and points to a non committish, the HEAD is * unaltered, and -1 is returned. * * Otherwise, the HEAD will be detached and point to the peeled Commit. * * @param repo Repository pointer - * @return 0 on success, GIT_EORPHANEDHEAD when HEAD points to a non existing + * @return 0 on success, GIT_EUNBORNBRANCH when HEAD points to a non existing * branch or an error code */ GIT_EXTERN(int) git_repository_detach_head( - git_repository* repo); + git_repository *repo); +/** + * Repository state + * + * These values represent possible states for the repository to be in, + * based on the current operation which is ongoing. + */ typedef enum { GIT_REPOSITORY_STATE_NONE, GIT_REPOSITORY_STATE_MERGE, GIT_REPOSITORY_STATE_REVERT, - GIT_REPOSITORY_STATE_CHERRY_PICK, + GIT_REPOSITORY_STATE_REVERT_SEQUENCE, + GIT_REPOSITORY_STATE_CHERRYPICK, + GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE, GIT_REPOSITORY_STATE_BISECT, GIT_REPOSITORY_STATE_REBASE, GIT_REPOSITORY_STATE_REBASE_INTERACTIVE, GIT_REPOSITORY_STATE_REBASE_MERGE, GIT_REPOSITORY_STATE_APPLY_MAILBOX, - GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE, + GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE } git_repository_state_t; /** @@ -642,6 +936,84 @@ typedef enum { */ GIT_EXTERN(int) git_repository_state(git_repository *repo); +/** + * Sets the active namespace for this Git Repository + * + * This namespace affects all reference operations for the repo. + * See `man gitnamespaces` + * + * @param repo The repo + * @param nmspace The namespace. This should not include the refs + * folder, e.g. to namespace all references under `refs/namespaces/foo/`, + * use `foo` as the namespace. + * @return 0 on success, -1 on error + */ +GIT_EXTERN(int) git_repository_set_namespace(git_repository *repo, const char *nmspace); + +/** + * Get the currently active namespace for this repository + * + * @param repo The repo + * @return the active namespace, or NULL if there isn't one + */ +GIT_EXTERN(const char *) git_repository_get_namespace(git_repository *repo); + + +/** + * Determine if the repository was a shallow clone + * + * @param repo The repository + * @return 1 if shallow, zero if not + */ +GIT_EXTERN(int) git_repository_is_shallow(git_repository *repo); + +/** + * Retrieve the configured identity to use for reflogs + * + * The memory is owned by the repository and must not be freed by the + * user. + * + * @param[out] name where to store the pointer to the name + * @param[out] email where to store the pointer to the email + * @param repo the repository + * @return 0 or an error code + */ +GIT_EXTERN(int) git_repository_ident(const char **name, const char **email, const git_repository *repo); + +/** + * Set the identity to be used for writing reflogs + * + * If both are set, this name and email will be used to write to the + * reflog. Pass NULL to unset. When unset, the identity will be taken + * from the repository's configuration. + * + * @param repo the repository to configure + * @param name the name to use for the reflog entries + * @param email the email to use for the reflog entries + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_repository_set_ident(git_repository *repo, const char *name, const char *email); + +/** + * Gets the object type used by this repository. + * + * @param repo the repository + * @return the object id type + */ +GIT_EXTERN(git_oid_t) git_repository_oid_type(git_repository *repo); + +/** + * Gets the parents of the next commit, given the current repository state. + * Generally, this is the HEAD commit, except when performing a merge, in + * which case it is two or more commits. + * + * @param commits a `git_commitarray` that will contain the commit parents + * @param repo the repository + * @return 0 or an error code + */ +GIT_EXTERN(int) git_repository_commit_parents(git_commitarray *commits, git_repository *repo); + /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/reset.h b/include/git2/reset.h index 00d25dff01a..0123f7c7600 100644 --- a/include/git2/reset.h +++ b/include/git2/reset.h @@ -7,9 +7,14 @@ #ifndef INCLUDE_git_reset_h__ #define INCLUDE_git_reset_h__ +#include "common.h" +#include "types.h" +#include "strarray.h" +#include "checkout.h" + /** * @file git2/reset.h - * @brief Git reset management routines + * @brief Reset will update the local repository to a prior state * @ingroup Git * @{ */ @@ -19,16 +24,16 @@ GIT_BEGIN_DECL * Kinds of reset operation */ typedef enum { - GIT_RESET_SOFT = 1, /** Move the head to the given commit */ - GIT_RESET_MIXED = 2, /** SOFT plus reset index to the commit */ - GIT_RESET_HARD = 3, /** MIXED plus changes in working tree discarded */ + GIT_RESET_SOFT = 1, /**< Move the head to the given commit */ + GIT_RESET_MIXED = 2, /**< SOFT plus reset index to the commit */ + GIT_RESET_HARD = 3 /**< MIXED plus changes in working tree discarded */ } git_reset_t; /** * Sets the current head to the specified commit oid and optionally * resets the index and working tree to match. * - * SOFT reset means the head will be moved to the commit. + * SOFT reset means the Head will be moved to the commit. * * MIXED reset will trigger a SOFT reset, plus the index will be replaced * with the content of the commit tree. @@ -41,18 +46,79 @@ typedef enum { * * @param repo Repository where to perform the reset operation. * - * @param target Object to which the Head should be moved to. This object + * @param target Committish to which the Head should be moved to. This object * must belong to the given `repo` and can either be a git_commit or a - * git_tag. When a git_tag is being passed, it should be dereferencable + * git_tag. When a git_tag is being passed, it should be dereferenceable * to a git_commit which oid will be used as the target of the branch. * * @param reset_type Kind of reset operation to perform. * - * @return 0 on success or an error code < 0 + * @param checkout_opts Optional checkout options to be used for a HARD reset. + * The checkout_strategy field will be overridden (based on reset_type). + * This parameter can be used to propagate notify and progress callbacks. + * + * @return 0 on success or an error code */ GIT_EXTERN(int) git_reset( - git_repository *repo, git_object *target, git_reset_t reset_type); + git_repository *repo, + const git_object *target, + git_reset_t reset_type, + const git_checkout_options *checkout_opts); + +/** + * Sets the current head to the specified commit oid and optionally + * resets the index and working tree to match. + * + * This behaves like `git_reset()` but takes an annotated commit, + * which lets you specify which extended sha syntax string was + * specified by a user, allowing for more exact reflog messages. + * + * See the documentation for `git_reset()`. + * + * @param repo Repository where to perform the reset operation. + * + * @param target Annotated commit to which the Head should be moved to. + * This object must belong to the given `repo`, it will be dereferenced + * to a git_commit which oid will be used as the target of the branch. + * + * @param reset_type Kind of reset operation to perform. + * + * @param checkout_opts Optional checkout options to be used for a HARD reset. + * The checkout_strategy field will be overridden (based on reset_type). + * This parameter can be used to propagate notify and progress callbacks. + * + * @return 0 on success or an error code + */ +GIT_EXTERN(int) git_reset_from_annotated( + git_repository *repo, + const git_annotated_commit *target, + git_reset_t reset_type, + const git_checkout_options *checkout_opts); + +/** + * Updates some entries in the index from the target commit tree. + * + * The scope of the updated entries is determined by the paths + * being passed in the `pathspec` parameters. + * + * Passing a NULL `target` will result in removing + * entries in the index matching the provided pathspecs. + * + * @param repo Repository where to perform the reset operation. + * + * @param target The committish which content will be used to reset the content + * of the index. + * + * @param pathspecs List of pathspecs to operate on. + * + * @return 0 on success or an error code < 0 + */ +GIT_EXTERN(int) git_reset_default( + git_repository *repo, + const git_object *target, + const git_strarray* pathspecs); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/revert.h b/include/git2/revert.h new file mode 100644 index 00000000000..ec51eca2dd8 --- /dev/null +++ b/include/git2/revert.h @@ -0,0 +1,96 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_revert_h__ +#define INCLUDE_git_revert_h__ + +#include "common.h" +#include "types.h" +#include "merge.h" + +/** + * @file git2/revert.h + * @brief Cherry-pick the inverse of a change to "undo" its effects + * @defgroup git_revert Cherry-pick the inverse of a change to "undo" its effects + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Options for revert + */ +typedef struct { + unsigned int version; + + /** For merge commits, the "mainline" is treated as the parent. */ + unsigned int mainline; + + git_merge_options merge_opts; /**< Options for the merging */ + git_checkout_options checkout_opts; /**< Options for the checkout */ +} git_revert_options; + +/** Current version for the `git_revert_options` structure */ +#define GIT_REVERT_OPTIONS_VERSION 1 + +/** Static constructor for `git_revert_options` */ +#define GIT_REVERT_OPTIONS_INIT { \ + GIT_REVERT_OPTIONS_VERSION, 0, \ + GIT_MERGE_OPTIONS_INIT, GIT_CHECKOUT_OPTIONS_INIT } + +/** + * Initialize git_revert_options structure + * + * Initializes a `git_revert_options` with default values. Equivalent to + * creating an instance with `GIT_REVERT_OPTIONS_INIT`. + * + * @param opts The `git_revert_options` struct to initialize. + * @param version The struct version; pass `GIT_REVERT_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_revert_options_init( + git_revert_options *opts, + unsigned int version); + +/** + * Reverts the given commit against the given "our" commit, producing an + * index that reflects the result of the revert. + * + * The returned index must be freed explicitly with `git_index_free`. + * + * @param out pointer to store the index result in + * @param repo the repository that contains the given commits + * @param revert_commit the commit to revert + * @param our_commit the commit to revert against (eg, HEAD) + * @param mainline the parent of the revert commit, if it is a merge + * @param merge_options the merge options (or null for defaults) + * @return zero on success, -1 on failure. + */ +GIT_EXTERN(int) git_revert_commit( + git_index **out, + git_repository *repo, + git_commit *revert_commit, + git_commit *our_commit, + unsigned int mainline, + const git_merge_options *merge_options); + +/** + * Reverts the given commit, producing changes in the index and working directory. + * + * @param repo the repository to revert + * @param commit the commit to revert + * @param given_opts the revert options (or null for defaults) + * @return zero on success, -1 on failure. + */ +GIT_EXTERN(int) git_revert( + git_repository *repo, + git_commit *commit, + const git_revert_options *given_opts); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/revparse.h b/include/git2/revparse.h index 6edb7767c9a..c14fe3dc874 100644 --- a/include/git2/revparse.h +++ b/include/git2/revparse.h @@ -10,28 +10,104 @@ #include "common.h" #include "types.h" - /** * @file git2/revparse.h - * @brief Git revision parsing routines - * @defgroup git_revparse Git revision parsing routines + * @brief Parse the textual revision information + * @defgroup git_revparse Parse the textual revision information * @ingroup Git * @{ */ GIT_BEGIN_DECL /** - * Find an object, as specified by a revision string. See `man gitrevisions`, or the documentation - * for `git rev-parse` for information on the syntax accepted. + * Find a single object, as specified by a revision string. + * + * See `man gitrevisions`, or + * http://git-scm.com/docs/git-rev-parse.html#_specifying_revisions for + * information on the syntax accepted. + * + * The returned object should be released with `git_object_free` when no + * longer needed. * * @param out pointer to output object * @param repo the repository to search in * @param spec the textual specification for an object - * @return 0 on success, GIT_ENOTFOUND, GIT_EAMBIGUOUS, - * GIT_EINVALIDSPEC or an error code + * @return 0 on success, GIT_ENOTFOUND, GIT_EAMBIGUOUS, GIT_EINVALIDSPEC or an error code */ -GIT_EXTERN(int) git_revparse_single(git_object **out, git_repository *repo, const char *spec); +GIT_EXTERN(int) git_revparse_single( + git_object **out, git_repository *repo, const char *spec); + +/** + * Find a single object and intermediate reference by a revision string. + * + * See `man gitrevisions`, or + * http://git-scm.com/docs/git-rev-parse.html#_specifying_revisions for + * information on the syntax accepted. + * + * In some cases (`@{<-n>}` or `@{upstream}`), the expression may + * point to an intermediate reference. When such expressions are being passed + * in, `reference_out` will be valued as well. + * + * The returned object should be released with `git_object_free` and the + * returned reference with `git_reference_free` when no longer needed. + * + * @param object_out pointer to output object + * @param reference_out pointer to output reference or NULL + * @param repo the repository to search in + * @param spec the textual specification for an object + * @return 0 on success, GIT_ENOTFOUND, GIT_EAMBIGUOUS, GIT_EINVALIDSPEC + * or an error code + */ +GIT_EXTERN(int) git_revparse_ext( + git_object **object_out, + git_reference **reference_out, + git_repository *repo, + const char *spec); + +/** + * Revparse flags. These indicate the intended behavior of the spec passed to + * git_revparse. + */ +typedef enum { + /** The spec targeted a single object. */ + GIT_REVSPEC_SINGLE = 1 << 0, + /** The spec targeted a range of commits. */ + GIT_REVSPEC_RANGE = 1 << 1, + /** The spec used the '...' operator, which invokes special semantics. */ + GIT_REVSPEC_MERGE_BASE = 1 << 2 +} git_revspec_t; + +/** + * Git Revision Spec: output of a `git_revparse` operation + */ +typedef struct { + /** The left element of the revspec; must be freed by the user */ + git_object *from; + /** The right element of the revspec; must be freed by the user */ + git_object *to; + /** The intent of the revspec (i.e. `git_revspec_mode_t` flags) */ + unsigned int flags; +} git_revspec; + +/** + * Parse a revision string for `from`, `to`, and intent. + * + * See `man gitrevisions` or + * http://git-scm.com/docs/git-rev-parse.html#_specifying_revisions for + * information on the syntax accepted. + * + * @param revspec Pointer to an user-allocated git_revspec struct where + * the result of the rev-parse will be stored + * @param repo the repository to search in + * @param spec the rev-parse spec to parse + * @return 0 on success, GIT_INVALIDSPEC, GIT_ENOTFOUND, GIT_EAMBIGUOUS or an error code + */ +GIT_EXTERN(int) git_revparse( + git_revspec *revspec, + git_repository *repo, + const char *spec); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/revwalk.h b/include/git2/revwalk.h index ad57b622ee1..7c4ac5465d9 100644 --- a/include/git2/revwalk.h +++ b/include/git2/revwalk.h @@ -13,41 +13,44 @@ /** * @file git2/revwalk.h - * @brief Git revision traversal routines - * @defgroup git_revwalk Git revision traversal routines + * @brief Traverse (walk) the commit graph (revision history) + * @defgroup git_revwalk Traverse (walk) the commit graph (revision history) * @ingroup Git * @{ */ GIT_BEGIN_DECL /** - * Sort the repository contents in no particular ordering; - * this sorting is arbitrary, implementation-specific - * and subject to change at any time. - * This is the default sorting for new walkers. + * Flags to specify the sorting which a revwalk should perform. */ -#define GIT_SORT_NONE (0) +typedef enum { + /** + * Sort the output with the same default method from `git`: reverse + * chronological order. This is the default sorting for new walkers. + */ + GIT_SORT_NONE = 0, -/** - * Sort the repository contents in topological order - * (parents before children); this sorting mode - * can be combined with time sorting. - */ -#define GIT_SORT_TOPOLOGICAL (1 << 0) + /** + * Sort the repository contents in topological order (no parents before + * all of its children are shown); this sorting mode can be combined + * with time sorting to produce `git`'s `--date-order``. + */ + GIT_SORT_TOPOLOGICAL = 1 << 0, -/** - * Sort the repository contents by commit time; - * this sorting mode can be combined with - * topological sorting. - */ -#define GIT_SORT_TIME (1 << 1) + /** + * Sort the repository contents by commit time; + * this sorting mode can be combined with + * topological sorting. + */ + GIT_SORT_TIME = 1 << 1, -/** - * Iterate through the repository contents in reverse - * order; this sorting mode can be combined with - * any of the above. - */ -#define GIT_SORT_REVERSE (1 << 2) + /** + * Iterate through the repository contents in reverse + * order; this sorting mode can be combined with + * any of the above. + */ + GIT_SORT_REVERSE = 1 << 2 +} git_sort_t; /** * Allocate a new revision walker to iterate through a repo. @@ -81,19 +84,22 @@ GIT_EXTERN(int) git_revwalk_new(git_revwalk **out, git_repository *repo); * is over. * * @param walker handle to reset. + * @return 0 or an error code */ -GIT_EXTERN(void) git_revwalk_reset(git_revwalk *walker); +GIT_EXTERN(int) git_revwalk_reset(git_revwalk *walker); /** - * Mark a commit to start traversal from. + * Add a new root for the traversal * - * The given OID must belong to a commit on the walked - * repository. + * The pushed commit will be marked as one of the roots from which to + * start the walk. This commit may not be walked if it or a child is + * hidden. * - * The given commit will be used as one of the roots - * when starting the revision walk. At least one commit - * must be pushed the repository before a walk can - * be started. + * At least one commit must be pushed onto the walker before a walk + * can be started. + * + * The given id must belong to a committish on the walked + * repository. * * @param walk the walker being used for the traversal. * @param id the oid of the commit to start from. @@ -108,7 +114,10 @@ GIT_EXTERN(int) git_revwalk_push(git_revwalk *walk, const git_oid *id); * pattern will be pushed to the revision walker. * * A leading 'refs/' is implied if not present as well as a trailing - * '/ *' if the glob lacks '?', '*' or '['. + * '/\*' if the glob lacks '?', '\*' or '['. + * + * Any references matching this glob which do not point to a + * committish will be ignored. * * @param walk the walker being used for the traversal * @param glob the glob pattern references should match @@ -127,7 +136,7 @@ GIT_EXTERN(int) git_revwalk_push_head(git_revwalk *walk); /** * Mark a commit (and its ancestors) uninteresting for the output. * - * The given OID must belong to a commit on the walked + * The given id must belong to a committish on the walked * repository. * * The resolved commit and all its parents will be hidden from the @@ -147,7 +156,10 @@ GIT_EXTERN(int) git_revwalk_hide(git_revwalk *walk, const git_oid *commit_id); * revision walk. * * A leading 'refs/' is implied if not present as well as a trailing - * '/ *' if the glob lacks '?', '*' or '['. + * '/\*' if the glob lacks '?', '\*' or '['. + * + * Any references matching this glob which do not point to a + * committish will be ignored. * * @param walk the walker being used for the traversal * @param glob the glob pattern references should match @@ -166,7 +178,7 @@ GIT_EXTERN(int) git_revwalk_hide_head(git_revwalk *walk); /** * Push the OID pointed to by a reference * - * The reference must point to a commit. + * The reference must point to a committish. * * @param walk the walker being used for the traversal * @param refname the reference to push @@ -177,7 +189,7 @@ GIT_EXTERN(int) git_revwalk_push_ref(git_revwalk *walk, const char *refname); /** * Hide the OID pointed to by a reference * - * The reference must point to a commit. + * The reference must point to a committish. * * @param walk the walker being used for the traversal * @param refname the reference to hide @@ -213,8 +225,35 @@ GIT_EXTERN(int) git_revwalk_next(git_oid *out, git_revwalk *walk); * * @param walk the walker being used for the traversal. * @param sort_mode combination of GIT_SORT_XXX flags + * @return 0 or an error code + */ +GIT_EXTERN(int) git_revwalk_sorting(git_revwalk *walk, unsigned int sort_mode); + +/** + * Push and hide the respective endpoints of the given range. + * + * The range should be of the form + * .. + * where each is in the form accepted by 'git_revparse_single'. + * The left-hand commit will be hidden and the right-hand commit pushed. + * + * @param walk the walker being used for the traversal + * @param range the range + * @return 0 or an error code + * + */ +GIT_EXTERN(int) git_revwalk_push_range(git_revwalk *walk, const char *range); + +/** + * Simplify the history by first-parent + * + * No parents other than the first for each commit will be enqueued. + * + * @param walk The revision walker. + * @return 0 or an error code */ -GIT_EXTERN(void) git_revwalk_sorting(git_revwalk *walk, unsigned int sort_mode); +GIT_EXTERN(int) git_revwalk_simplify_first_parent(git_revwalk *walk); + /** * Free a revision walker previously allocated. @@ -232,6 +271,33 @@ GIT_EXTERN(void) git_revwalk_free(git_revwalk *walk); */ GIT_EXTERN(git_repository *) git_revwalk_repository(git_revwalk *walk); +/** + * This is a callback function that user can provide to hide a + * commit and its parents. If the callback function returns non-zero value, + * then this commit and its parents will be hidden. + * + * @param commit_id oid of Commit + * @param payload User-specified pointer to data to be passed as data payload + * @return non-zero to hide the commmit and it parent. + */ +typedef int GIT_CALLBACK(git_revwalk_hide_cb)( + const git_oid *commit_id, + void *payload); + +/** + * Adds, changes or removes a callback function to hide a commit and its parents + * + * @param walk the revision walker + * @param hide_cb callback function to hide a commit and its parents + * @param payload data payload to be passed to callback function + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_revwalk_add_hide_cb( + git_revwalk *walk, + git_revwalk_hide_cb hide_cb, + void *payload); + /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/signature.h b/include/git2/signature.h index 00d19de66f7..20ec24b340a 100644 --- a/include/git2/signature.h +++ b/include/git2/signature.h @@ -12,9 +12,13 @@ /** * @file git2/signature.h - * @brief Git signature creation + * @brief Signatures are the actor in a repository and when they acted * @defgroup git_signature Git signature creation * @ingroup Git + * + * Signatures contain the information about the actor (committer or + * author) in a repository, and the time that they performed the + * commit, or authoring. * @{ */ GIT_BEGIN_DECL @@ -30,8 +34,8 @@ GIT_BEGIN_DECL * @param out new signature, in case of error NULL * @param name name of the person * @param email email of the person - * @param time time when the action happened - * @param offset timezone offset in minutes for the time + * @param time time (in seconds from epoch) when the action happened + * @param offset timezone offset (in minutes) for the time * @return 0 or an error code */ GIT_EXTERN(int) git_signature_new(git_signature **out, const char *name, const char *email, git_time_t time, int offset); @@ -48,6 +52,72 @@ GIT_EXTERN(int) git_signature_new(git_signature **out, const char *name, const c */ GIT_EXTERN(int) git_signature_now(git_signature **out, const char *name, const char *email); +/** + * Create a new author and/or committer signatures with default + * information based on the configuration and environment variables. + * + * If `author_out` is set, it will be populated with the author + * information. The `GIT_AUTHOR_NAME` and `GIT_AUTHOR_EMAIL` + * environment variables will be honored, and `user.name` and + * `user.email` configuration options will be honored if the + * environment variables are unset. For timestamps, `GIT_AUTHOR_DATE` + * will be used, otherwise the current time will be used. + * + * If `committer_out` is set, it will be populated with the + * committer information. The `GIT_COMMITTER_NAME` and + * `GIT_COMMITTER_EMAIL` environment variables will be honored, + * and `user.name` and `user.email` configuration options will + * be honored if the environment variables are unset. For timestamps, + * `GIT_COMMITTER_DATE` will be used, otherwise the current time will + * be used. + * + * If neither `GIT_AUTHOR_DATE` nor `GIT_COMMITTER_DATE` are set, + * both timestamps will be set to the same time. + * + * It will return `GIT_ENOTFOUND` if either the `user.name` or + * `user.email` are not set and there is no fallback from an environment + * variable. One of `author_out` or `committer_out` must be set. + * + * @param author_out pointer to set the author signature, or NULL + * @param committer_out pointer to set the committer signature, or NULL + * @param repo repository pointer + * @return 0 on success, GIT_ENOTFOUND if config is missing, or error code + */ +GIT_EXTERN(int) git_signature_default_from_env( + git_signature **author_out, + git_signature **committer_out, + git_repository *repo); + +/** + * Create a new action signature with default user and now timestamp. + * + * This looks up the user.name and user.email from the configuration and + * uses the current time as the timestamp, and creates a new signature + * based on that information. It will return GIT_ENOTFOUND if either the + * user.name or user.email are not set. + * + * Note that these do not examine environment variables, only the + * configuration files. Use `git_signature_default_from_env` to + * consider the environment variables. + * + * @param out new signature + * @param repo repository pointer + * @return 0 on success, GIT_ENOTFOUND if config is missing, or error code + */ +GIT_EXTERN(int) git_signature_default(git_signature **out, git_repository *repo); + +/** + * Create a new signature by parsing the given buffer, which is + * expected to be in the format "Real Name timestamp tzoffset", + * where `timestamp` is the number of seconds since the Unix epoch and + * `tzoffset` is the timezone offset in `hhmm` format (note the lack + * of a colon separator). + * + * @param out new signature + * @param buf signature string + * @return 0 on success, GIT_EINVALID if the signature is not parseable, or an error code + */ +GIT_EXTERN(int) git_signature_from_buffer(git_signature **out, const char *buf); /** * Create a copy of an existing signature. All internal strings are also @@ -55,10 +125,11 @@ GIT_EXTERN(int) git_signature_now(git_signature **out, const char *name, const c * * Call `git_signature_free()` to free the data. * - * @param sig signature to duplicated - * @return a copy of sig, NULL on out of memory + * @param dest pointer where to store the copy + * @param sig signature to duplicate + * @return 0 or an error code */ -GIT_EXTERN(git_signature *) git_signature_dup(const git_signature *sig); +GIT_EXTERN(int) git_signature_dup(git_signature **dest, const git_signature *sig); /** * Free an existing signature. @@ -73,4 +144,5 @@ GIT_EXTERN(void) git_signature_free(git_signature *sig); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/stash.h b/include/git2/stash.h index cf8bc9d4cfe..ad28c32639a 100644 --- a/include/git2/stash.h +++ b/include/git2/stash.h @@ -9,32 +9,52 @@ #include "common.h" #include "types.h" +#include "checkout.h" /** * @file git2/stash.h - * @brief Git stash management routines + * @brief Stashes stores some uncommitted state in the repository * @ingroup Git + * + * Stashes stores some uncommitted state in the repository; generally + * this allows a user to stash some changes so that they can restore + * the working directory to an unmodified state. This can allow a + * developer to work on two different changes in parallel. * @{ */ GIT_BEGIN_DECL +/** + * Stash flags + */ typedef enum { + /** + * No option, default + */ GIT_STASH_DEFAULT = 0, - /* All changes already added to the index - * are left intact in the working directory + /** + * All changes already added to the index are left intact in + * the working directory */ GIT_STASH_KEEP_INDEX = (1 << 0), - /* All untracked files are also stashed and then - * cleaned up from the working directory + /** + * All untracked files are also stashed and then cleaned up + * from the working directory */ GIT_STASH_INCLUDE_UNTRACKED = (1 << 1), - /* All ignored files are also stashed and then - * cleaned up from the working directory + /** + * All ignored files are also stashed and then cleaned up from + * the working directory */ GIT_STASH_INCLUDE_IGNORED = (1 << 2), + + /** + * All changes in the index and working directory are left intact + */ + GIT_STASH_KEEP_ALL = (1 << 3) } git_stash_flags; /** @@ -42,43 +62,212 @@ typedef enum { * * @param out Object id of the commit containing the stashed state. * This commit is also the target of the direct reference refs/stash. - * * @param repo The owning repository. - * * @param stasher The identity of the person performing the stashing. - * * @param message Optional description along with the stashed state. - * * @param flags Flags to control the stashing process. (see GIT_STASH_* above) - * * @return 0 on success, GIT_ENOTFOUND where there's nothing to stash, * or error code. */ GIT_EXTERN(int) git_stash_save( git_oid *out, git_repository *repo, - git_signature *stasher, + const git_signature *stasher, const char *message, - unsigned int flags); + uint32_t flags); + +/** + * Stash save options structure + * + * Initialize with `GIT_STASH_SAVE_OPTIONS_INIT`. Alternatively, you can + * use `git_stash_save_options_init`. + * + */ +typedef struct git_stash_save_options { + unsigned int version; + + /** Flags to control the stashing process. (see GIT_STASH_* above) */ + uint32_t flags; + + /** The identity of the person performing the stashing. */ + const git_signature *stasher; + + /** Optional description along with the stashed state. */ + const char *message; + + /** Optional paths that control which files are stashed. */ + git_strarray paths; +} git_stash_save_options; + +/** Current version for the `git_stash_save_options` structure */ +#define GIT_STASH_SAVE_OPTIONS_VERSION 1 + +/** Static constructor for `git_stash_save_options` */ +#define GIT_STASH_SAVE_OPTIONS_INIT { GIT_STASH_SAVE_OPTIONS_VERSION } + +/** + * Initialize git_stash_save_options structure + * + * Initializes a `git_stash_save_options` with default values. Equivalent to + * creating an instance with `GIT_STASH_SAVE_OPTIONS_INIT`. + * + * @param opts The `git_stash_save_options` struct to initialize. + * @param version The struct version; pass `GIT_STASH_SAVE_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_stash_save_options_init( + git_stash_save_options *opts, unsigned int version); + +/** + * Save the local modifications to a new stash, with options. + * + * @param out Object id of the commit containing the stashed state. + * This commit is also the target of the direct reference refs/stash. + * @param repo The owning repository. + * @param opts The stash options. + * @return 0 on success, GIT_ENOTFOUND where there's nothing to stash, + * or error code. + */ +GIT_EXTERN(int) git_stash_save_with_opts( + git_oid *out, + git_repository *repo, + const git_stash_save_options *opts); + +/** Stash application flags. */ +typedef enum { + GIT_STASH_APPLY_DEFAULT = 0, + + /* Try to reinstate not only the working tree's changes, + * but also the index's changes. + */ + GIT_STASH_APPLY_REINSTATE_INDEX = (1 << 0) +} git_stash_apply_flags; + +/** Stash apply progression states */ +typedef enum { + GIT_STASH_APPLY_PROGRESS_NONE = 0, + + /** Loading the stashed data from the object database. */ + GIT_STASH_APPLY_PROGRESS_LOADING_STASH, + + /** The stored index is being analyzed. */ + GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX, + + /** The modified files are being analyzed. */ + GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED, + + /** The untracked and ignored files are being analyzed. */ + GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED, + + /** The untracked files are being written to disk. */ + GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED, + + /** The modified files are being written to disk. */ + GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED, + + /** The stash was applied successfully. */ + GIT_STASH_APPLY_PROGRESS_DONE +} git_stash_apply_progress_t; + +/** + * Stash application progress notification function. + * Return 0 to continue processing, or a negative value to + * abort the stash application. + * + * @param progress the progress information + * @param payload the user-specified payload to the apply function + * @return 0 on success, -1 on error + */ +typedef int GIT_CALLBACK(git_stash_apply_progress_cb)( + git_stash_apply_progress_t progress, + void *payload); + +/** + * Stash application options structure + * + * Initialize with `GIT_STASH_APPLY_OPTIONS_INIT`. Alternatively, you can + * use `git_stash_apply_options_init`. + * + */ +typedef struct git_stash_apply_options { + unsigned int version; + + /** See `git_stash_apply_flags`, above. */ + uint32_t flags; + + /** Options to use when writing files to the working directory. */ + git_checkout_options checkout_options; + + /** Optional callback to notify the consumer of application progress. */ + git_stash_apply_progress_cb progress_cb; + void *progress_payload; +} git_stash_apply_options; + +/** Current version for the `git_stash_apply_options` structure */ +#define GIT_STASH_APPLY_OPTIONS_VERSION 1 + +/** Static constructor for `git_stash_apply_options` */ +#define GIT_STASH_APPLY_OPTIONS_INIT { \ + GIT_STASH_APPLY_OPTIONS_VERSION, \ + GIT_STASH_APPLY_DEFAULT, \ + GIT_CHECKOUT_OPTIONS_INIT } /** - * When iterating over all the stashed states, callback that will be - * issued per entry. + * Initialize git_stash_apply_options structure + * + * Initializes a `git_stash_apply_options` with default values. Equivalent to + * creating an instance with `GIT_STASH_APPLY_OPTIONS_INIT`. + * + * @param opts The `git_stash_apply_options` struct to initialize. + * @param version The struct version; pass `GIT_STASH_APPLY_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_stash_apply_options_init( + git_stash_apply_options *opts, unsigned int version); + +/** + * Apply a single stashed state from the stash list. + * + * If local changes in the working directory conflict with changes in the + * stash then GIT_EMERGECONFLICT will be returned. In this case, the index + * will always remain unmodified and all files in the working directory will + * remain unmodified. However, if you are restoring untracked files or + * ignored files and there is a conflict when applying the modified files, + * then those files will remain in the working directory. * + * If passing the GIT_STASH_APPLY_REINSTATE_INDEX flag and there would be + * conflicts when reinstating the index, the function will return + * GIT_EMERGECONFLICT and both the working directory and index will be left + * unmodified. + * + * @param repo The owning repository. * @param index The position within the stash list. 0 points to the - * most recent stashed state. + * most recent stashed state. + * @param options Optional options to control how stashes are applied. * - * @param message The stash message. + * @return 0 on success, GIT_ENOTFOUND if there's no stashed state for the + * given index, GIT_EMERGECONFLICT if changes exist in the working + * directory, or an error code + */ +GIT_EXTERN(int) git_stash_apply( + git_repository *repo, + size_t index, + const git_stash_apply_options *options); + +/** + * This is a callback function you can provide to iterate over all the + * stashed states that will be invoked per entry. * + * @param index The position within the stash list. 0 points to the + * most recent stashed state. + * @param message The stash message. * @param stash_id The commit oid of the stashed state. - * * @param payload Extra parameter to callback function. - * - * @return 0 on success, GIT_EUSER on non-zero callback, or error code + * @return 0 to continue iterating or non-zero to stop. */ -typedef int (*git_stash_cb)( +typedef int GIT_CALLBACK(git_stash_cb)( size_t index, - const char* message, + const char *message, const git_oid *stash_id, void *payload); @@ -89,12 +278,12 @@ typedef int (*git_stash_cb)( * * @param repo Repository where to find the stash. * - * @param callabck Callback to invoke per found stashed state. The most recent - * stash state will be enumerated first. + * @param callback Callback to invoke per found stashed state. The most + * recent stash state will be enumerated first. * * @param payload Extra parameter to callback function. * - * @return 0 on success, GIT_EUSER on non-zero callback, or error code + * @return 0 on success, non-zero callback return value, or error code. */ GIT_EXTERN(int) git_stash_foreach( git_repository *repo, @@ -109,13 +298,31 @@ GIT_EXTERN(int) git_stash_foreach( * @param index The position within the stash list. 0 points to the * most recent stashed state. * - * @return 0 on success, or error code + * @return 0 on success, GIT_ENOTFOUND if there's no stashed state for the given + * index, or error code. */ - GIT_EXTERN(int) git_stash_drop( git_repository *repo, size_t index); +/** + * Apply a single stashed state from the stash list and remove it from the list + * if successful. + * + * @param repo The owning repository. + * @param index The position within the stash list. 0 points to the + * most recent stashed state. + * @param options Optional options to control how stashes are applied. + * + * @return 0 on success, GIT_ENOTFOUND if there's no stashed state for the given + * index, or error code. (see git_stash_apply() above for details) +*/ +GIT_EXTERN(int) git_stash_pop( + git_repository *repo, + size_t index, + const git_stash_apply_options *options); + /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/status.h b/include/git2/status.h index d0c4a496d0e..e13783b6746 100644 --- a/include/git2/status.h +++ b/include/git2/status.h @@ -9,10 +9,12 @@ #include "common.h" #include "types.h" +#include "strarray.h" +#include "diff.h" /** * @file git2/status.h - * @brief Git file status routines + * @brief Status indicates how a user has changed the working directory and index * @defgroup git_status Git file status routines * @ingroup Git * @{ @@ -42,131 +44,284 @@ typedef enum { GIT_STATUS_WT_MODIFIED = (1u << 8), GIT_STATUS_WT_DELETED = (1u << 9), GIT_STATUS_WT_TYPECHANGE = (1u << 10), + GIT_STATUS_WT_RENAMED = (1u << 11), + GIT_STATUS_WT_UNREADABLE = (1u << 12), GIT_STATUS_IGNORED = (1u << 14), + GIT_STATUS_CONFLICTED = (1u << 15) } git_status_t; /** * Function pointer to receive status on individual files * - * `path` is the relative path to the file from the root of the repository. - * - * `status_flags` is a combination of `git_status_t` values that apply. - * - * `payload` is the value you passed to the foreach function as payload. + * @param path is the path to the file + * @param status_flags the `git_status_t` values for file's status + * @param payload the user-specified payload to the foreach function + * @return 0 on success, or a negative number on failure */ -typedef int (*git_status_cb)( +typedef int GIT_CALLBACK(git_status_cb)( const char *path, unsigned int status_flags, void *payload); /** - * Gather file statuses and run a callback for each one. - * - * The callback is passed the path of the file, the status (a combination of - * the `git_status_t` values above) and the `payload` data pointer passed - * into this function. + * Select the files on which to report status. * - * If the callback returns a non-zero value, this function will stop looping - * and return GIT_EUSER. - * - * @param repo A repository object - * @param callback The function to call on each file - * @param payload Pointer to pass through to callback function - * @return 0 on success, GIT_EUSER on non-zero callback, or error code - */ -GIT_EXTERN(int) git_status_foreach( - git_repository *repo, - git_status_cb callback, - void *payload); - -/** - * For extended status, select the files on which to report status. - * - * - GIT_STATUS_SHOW_INDEX_AND_WORKDIR is the default. This is the - * rough equivalent of `git status --porcelain` where each file - * will receive a callback indicating its status in the index and - * in the workdir. - * - GIT_STATUS_SHOW_INDEX_ONLY will only make callbacks for index - * side of status. The status of the index contents relative to - * the HEAD will be given. - * - GIT_STATUS_SHOW_WORKDIR_ONLY will only make callbacks for the - * workdir side of status, reporting the status of workdir content - * relative to the index. - * - GIT_STATUS_SHOW_INDEX_THEN_WORKDIR behaves like index-only - * followed by workdir-only, causing two callbacks to be issued - * per file (first index then workdir). This is slightly more - * efficient than making separate calls. This makes it easier to - * emulate the output of a plain `git status`. + * With `git_status_foreach_ext`, this will control which changes get + * callbacks. With `git_status_list_new`, these will control which + * changes are included in the list. */ typedef enum { + /** + * The default. This roughly matches `git status --porcelain` regarding + * which files are included and in what order. + */ GIT_STATUS_SHOW_INDEX_AND_WORKDIR = 0, + + /** + * Only gives status based on HEAD to index comparison, not looking at + * working directory changes. + */ GIT_STATUS_SHOW_INDEX_ONLY = 1, - GIT_STATUS_SHOW_WORKDIR_ONLY = 2, - GIT_STATUS_SHOW_INDEX_THEN_WORKDIR = 3, + + /** + * Only gives status based on index to working directory comparison, + * not comparing the index to the HEAD. + */ + GIT_STATUS_SHOW_WORKDIR_ONLY = 2 } git_status_show_t; /** * Flags to control status callbacks * - * - GIT_STATUS_OPT_INCLUDE_UNTRACKED says that callbacks should be made - * on untracked files. These will only be made if the workdir files are - * included in the status "show" option. - * - GIT_STATUS_OPT_INCLUDE_IGNORED says that ignored files should get - * callbacks. Again, these callbacks will only be made if the workdir - * files are included in the status "show" option. Right now, there is - * no option to include all files in directories that are ignored - * completely. - * - GIT_STATUS_OPT_INCLUDE_UNMODIFIED indicates that callback should be - * made even on unmodified files. - * - GIT_STATUS_OPT_EXCLUDE_SUBMODULES indicates that directories which - * appear to be submodules should just be skipped over. - * - GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS indicates that the contents of - * untracked directories should be included in the status. Normally if - * an entire directory is new, then just the top-level directory will be - * included (with a trailing slash on the entry name). Given this flag, - * the directory itself will not be included, but all the files in it - * will. - * - GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH indicates that the given path - * will be treated as a literal path, and not as a pathspec. - * * Calling `git_status_foreach()` is like calling the extended version * with: GIT_STATUS_OPT_INCLUDE_IGNORED, GIT_STATUS_OPT_INCLUDE_UNTRACKED, - * and GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS. + * and GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS. Those options are bundled + * together as `GIT_STATUS_OPT_DEFAULTS` if you want them as a baseline. */ typedef enum { - GIT_STATUS_OPT_INCLUDE_UNTRACKED = (1 << 0), - GIT_STATUS_OPT_INCLUDE_IGNORED = (1 << 1), - GIT_STATUS_OPT_INCLUDE_UNMODIFIED = (1 << 2), - GIT_STATUS_OPT_EXCLUDE_SUBMODULES = (1 << 3), - GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS = (1 << 4), - GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH = (1 << 5), + /** + * Says that callbacks should be made on untracked files. + * These will only be made if the workdir files are included in the status + * "show" option. + */ + GIT_STATUS_OPT_INCLUDE_UNTRACKED = (1u << 0), + + /** + * Says that ignored files get callbacks. + * Again, these callbacks will only be made if the workdir files are + * included in the status "show" option. + */ + GIT_STATUS_OPT_INCLUDE_IGNORED = (1u << 1), + + /** + * Indicates that callback should be made even on unmodified files. + */ + GIT_STATUS_OPT_INCLUDE_UNMODIFIED = (1u << 2), + + /** + * Indicates that submodules should be skipped. + * This only applies if there are no pending typechanges to the submodule + * (either from or to another type). + */ + GIT_STATUS_OPT_EXCLUDE_SUBMODULES = (1u << 3), + + /** + * Indicates that all files in untracked directories should be included. + * Normally if an entire directory is new, then just the top-level + * directory is included (with a trailing slash on the entry name). + * This flag says to include all of the individual files in the directory + * instead. + */ + GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS = (1u << 4), + + /** + * Indicates that the given path should be treated as a literal path, + * and not as a pathspec pattern. + */ + GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH = (1u << 5), + + /** + * Indicates that the contents of ignored directories should be included + * in the status. This is like doing `git ls-files -o -i --exclude-standard` + * with core git. + */ + GIT_STATUS_OPT_RECURSE_IGNORED_DIRS = (1u << 6), + + /** + * Indicates that rename detection should be processed between the head and + * the index and enables the GIT_STATUS_INDEX_RENAMED as a possible status + * flag. + */ + GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX = (1u << 7), + + /** + * Indicates that rename detection should be run between the index and the + * working directory and enabled GIT_STATUS_WT_RENAMED as a possible status + * flag. + */ + GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR = (1u << 8), + + /** + * Overrides the native case sensitivity for the file system and forces + * the output to be in case-sensitive order. + */ + GIT_STATUS_OPT_SORT_CASE_SENSITIVELY = (1u << 9), + + /** + * Overrides the native case sensitivity for the file system and forces + * the output to be in case-insensitive order. + */ + GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY = (1u << 10), + + /** + * Iindicates that rename detection should include rewritten files. + */ + GIT_STATUS_OPT_RENAMES_FROM_REWRITES = (1u << 11), + + /** + * Bypasses the default status behavior of doing a "soft" index reload + * (i.e. reloading the index data if the file on disk has been modified + * outside libgit2). + */ + GIT_STATUS_OPT_NO_REFRESH = (1u << 12), + + /** + * Tells libgit2 to refresh the stat cache in the index for files that are + * unchanged but have out of date stat einformation in the index. + * It will result in less work being done on subsequent calls to get status. + * This is mutually exclusive with the NO_REFRESH option. + */ + GIT_STATUS_OPT_UPDATE_INDEX = (1u << 13), + + /** + * Normally files that cannot be opened or read are ignored as + * these are often transient files; this option will return + * unreadable files as `GIT_STATUS_WT_UNREADABLE`. + */ + GIT_STATUS_OPT_INCLUDE_UNREADABLE = (1u << 14), + + /** + * Unreadable files will be detected and given the status + * untracked instead of unreadable. + */ + GIT_STATUS_OPT_INCLUDE_UNREADABLE_AS_UNTRACKED = (1u << 15) } git_status_opt_t; +/** Default `git_status_opt_t` values */ +#define GIT_STATUS_OPT_DEFAULTS \ + (GIT_STATUS_OPT_INCLUDE_IGNORED | \ + GIT_STATUS_OPT_INCLUDE_UNTRACKED | \ + GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS) + /** * Options to control how `git_status_foreach_ext()` will issue callbacks. * - * This structure is set so that zeroing it out will give you relatively - * sane defaults. - * - * The `show` value is one of the `git_status_show_t` constants that - * control which files to scan and in what order. + * Initialize with `GIT_STATUS_OPTIONS_INIT`. Alternatively, you can + * use `git_status_options_init`. * - * The `flags` value is an OR'ed combination of the `git_status_opt_t` - * values above. - * - * The `pathspec` is an array of path patterns to match (using - * fnmatch-style matching), or just an array of paths to match exactly if - * `GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH` is specified in the flags. */ typedef struct { - unsigned int version; + /** + * The struct version; pass `GIT_STATUS_OPTIONS_VERSION`. + */ + unsigned int version; + + /** + * The `show` value is one of the `git_status_show_t` constants that + * control which files to scan and in what order. The default is + * `GIT_STATUS_SHOW_INDEX_AND_WORKDIR`. + */ git_status_show_t show; + + /** + * The `flags` value is an OR'ed combination of the + * `git_status_opt_t` values above. The default is + * `GIT_STATUS_OPT_DEFAULTS`, which matches git's default + * behavior. + */ unsigned int flags; + + /** + * The `pathspec` is an array of path patterns to match (using + * fnmatch-style matching), or just an array of paths to match + * exactly if `GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH` is specified + * in the flags. + */ git_strarray pathspec; + + /** + * The `baseline` is the tree to be used for comparison to the + * working directory and index; defaults to HEAD. + */ + git_tree *baseline; + + /** + * Threshold above which similar files will be considered renames. + * This is equivalent to the -M option. Defaults to 50. + */ + uint16_t rename_threshold; } git_status_options; +/** Current version for the `git_status_options` structure */ #define GIT_STATUS_OPTIONS_VERSION 1 + +/** Static constructor for `git_status_options` */ #define GIT_STATUS_OPTIONS_INIT {GIT_STATUS_OPTIONS_VERSION} +/** + * Initialize git_status_options structure + * + * Initializes a `git_status_options` with default values. Equivalent to + * creating an instance with `GIT_STATUS_OPTIONS_INIT`. + * + * @param opts The `git_status_options` struct to initialize. + * @param version The struct version; pass `GIT_STATUS_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_status_options_init( + git_status_options *opts, + unsigned int version); + +/** + * A status entry, providing the differences between the file as it exists + * in HEAD and the index, and providing the differences between the index + * and the working directory. + * + * The `status` value provides the status flags for this file. + * + * The `head_to_index` value provides detailed information about the + * differences between the file in HEAD and the file in the index. + * + * The `index_to_workdir` value provides detailed information about the + * differences between the file in the index and the file in the + * working directory. + */ +typedef struct { + git_status_t status; + git_diff_delta *head_to_index; + git_diff_delta *index_to_workdir; +} git_status_entry; + + +/** + * Gather file statuses and run a callback for each one. + * + * The callback is passed the path of the file, the status (a combination of + * the `git_status_t` values above) and the `payload` data pointer passed + * into this function. + * + * If the callback returns a non-zero value, this function will stop looping + * and return that value to caller. + * + * @param repo A repository object + * @param callback The function to call on each file + * @param payload Pointer to pass through to callback function + * @return 0 on success, non-zero callback return value, or error code + */ +GIT_EXTERN(int) git_status_foreach( + git_repository *repo, + git_status_cb callback, + void *payload); + /** * Gather file status information and run callbacks as requested. * @@ -175,11 +330,16 @@ typedef struct { * in what order. See the `git_status_options` structure for details * about the additional controls that this makes available. * + * Note that if a `pathspec` is given in the `git_status_options` to filter + * the status, then the results from rename detection (if you enable it) may + * not be accurate. To do rename detection properly, this must be called + * with no `pathspec` so that all files can be considered. + * * @param repo Repository object * @param opts Status options structure * @param callback The function to call on each file * @param payload Pointer to pass through to callback function - * @return 0 on success, GIT_EUSER on non-zero callback, or error code + * @return 0 on success, non-zero callback return value, or error code */ GIT_EXTERN(int) git_status_foreach_ext( git_repository *repo, @@ -190,21 +350,85 @@ GIT_EXTERN(int) git_status_foreach_ext( /** * Get file status for a single file. * - * This is not quite the same as calling `git_status_foreach_ext()` with - * the pathspec set to the specified path. + * This tries to get status for the filename that you give. If no files + * match that name (in either the HEAD, index, or working directory), this + * returns GIT_ENOTFOUND. * - * @param status_flags The status value for the file + * If the name matches multiple files (for example, if the `path` names a + * directory or if running on a case- insensitive filesystem and yet the + * HEAD has two entries that both match the path), then this returns + * GIT_EAMBIGUOUS because it cannot give correct results. + * + * This does not do any sort of rename detection. Renames require a set of + * targets and because of the path filtering, there is not enough + * information to check renames correctly. To check file status with rename + * detection, there is no choice but to do a full `git_status_list_new` and + * scan through looking for the path that you are interested in. + * + * @param status_flags Output combination of git_status_t values for file * @param repo A repository object - * @param path The file to retrieve status for, rooted at the repo's workdir + * @param path The exact path to retrieve status for relative to the + * repository working directory * @return 0 on success, GIT_ENOTFOUND if the file is not found in the HEAD, - * index, and work tree, GIT_EINVALIDPATH if `path` points at a folder, - * GIT_EAMBIGUOUS if "path" matches multiple files, -1 on other error. + * index, and work tree, GIT_EAMBIGUOUS if `path` matches multiple files + * or if it refers to a folder, and -1 on other errors. */ GIT_EXTERN(int) git_status_file( unsigned int *status_flags, git_repository *repo, const char *path); +/** + * Gather file status information and populate the `git_status_list`. + * + * Note that if a `pathspec` is given in the `git_status_options` to filter + * the status, then the results from rename detection (if you enable it) may + * not be accurate. To do rename detection properly, this must be called + * with no `pathspec` so that all files can be considered. + * + * @param out Pointer to store the status results in + * @param repo Repository object + * @param opts Status options structure + * @return 0 on success or error code + */ +GIT_EXTERN(int) git_status_list_new( + git_status_list **out, + git_repository *repo, + const git_status_options *opts); + +/** + * Gets the count of status entries in this list. + * + * If there are no changes in status (at least according the options given + * when the status list was created), this can return 0. + * + * @param statuslist Existing status list object + * @return the number of status entries + */ +GIT_EXTERN(size_t) git_status_list_entrycount( + git_status_list *statuslist); + +/** + * Get a pointer to one of the entries in the status list. + * + * The entry is not modifiable and should not be freed. + * + * @param statuslist Existing status list object + * @param idx Position of the entry + * @return Pointer to the entry; NULL if out of bounds + */ +GIT_EXTERN(const git_status_entry *) git_status_byindex( + git_status_list *statuslist, + size_t idx); + +/** + * Free an existing status list + * + * @param statuslist Existing status list object + */ +GIT_EXTERN(void) git_status_list_free( + git_status_list *statuslist); + /** * Test if the ignore rules apply to a given file. * @@ -228,4 +452,5 @@ GIT_EXTERN(int) git_status_should_ignore( /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/stdint.h b/include/git2/stdint.h index c66fbb817c0..4f532e13c85 100644 --- a/include/git2/stdint.h +++ b/include/git2/stdint.h @@ -1,39 +1,37 @@ -// ISO C9x compliant stdint.h for Microsoft Visual Studio -// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 -// -// Copyright (c) 2006-2008 Alexander Chemeris -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in the -// documentation and/or other materials provided with the distribution. -// -// 3. The name of the author may be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED -// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO -// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -/////////////////////////////////////////////////////////////////////////////// - -#ifndef _MSC_VER // [ -#error "Use this header only with Microsoft Visual C++ compilers!" -#endif // _MSC_VER ] - -#ifndef _MSC_STDINT_H_ // [ +/* ISO C9x compliant stdint.h for Microsoft Visual Studio + * Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 + * + * Copyright (c) 2006-2008 Alexander Chemeris + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. The name of the author may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *******************************************************************************/ + +#ifdef _MSC_VER /* [ */ + +#ifndef _MSC_STDINT_H_ /* [ */ #define _MSC_STDINT_H_ #if _MSC_VER > 1000 @@ -42,10 +40,11 @@ #include -// For Visual Studio 6 in C++ mode and for many Visual Studio versions when -// compiling for ARM we should wrap include with 'extern "C++" {}' -// or compiler give many errors like this: -// error C2733: second C linkage of overloaded function 'wmemchr' not allowed +/* For Visual Studio 6 in C++ mode and for many Visual Studio versions when + * compiling for ARM we should wrap include with 'extern "C++" {}' + * or compiler give many errors like this: + * error C2733: second C linkage of overloaded function 'wmemchr' not allowed +*/ #ifdef __cplusplus extern "C" { #endif @@ -54,7 +53,7 @@ extern "C" { } #endif -// Define _W64 macros to mark types changing their size, like intptr_t. +/* Define _W64 macros to mark types changing their size, like intptr_t. */ #ifndef _W64 # if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300 # define _W64 __w64 @@ -64,13 +63,14 @@ extern "C" { #endif -// 7.18.1 Integer types - -// 7.18.1.1 Exact-width integer types - -// Visual Studio 6 and Embedded Visual C++ 4 doesn't -// realize that, e.g. char has the same size as __int8 -// so we give up on __intX for them. +/* 7.18.1 Integer types + * + * 7.18.1.1 Exact-width integer types + * + * Visual Studio 6 and Embedded Visual C++ 4 doesn't + * realize that, e.g. char has the same size as __int8 + * so we give up on __intX for them. + */ #if (_MSC_VER < 1300) typedef signed char int8_t; typedef signed short int16_t; @@ -90,7 +90,7 @@ typedef signed __int64 int64_t; typedef unsigned __int64 uint64_t; -// 7.18.1.2 Minimum-width integer types +/* 7.18.1.2 Minimum-width integer types */ typedef int8_t int_least8_t; typedef int16_t int_least16_t; typedef int32_t int_least32_t; @@ -100,7 +100,7 @@ typedef uint16_t uint_least16_t; typedef uint32_t uint_least32_t; typedef uint64_t uint_least64_t; -// 7.18.1.3 Fastest minimum-width integer types +/* 7.18.1.3 Fastest minimum-width integer types */ typedef int8_t int_fast8_t; typedef int16_t int_fast16_t; typedef int32_t int_fast32_t; @@ -110,25 +110,25 @@ typedef uint16_t uint_fast16_t; typedef uint32_t uint_fast32_t; typedef uint64_t uint_fast64_t; -// 7.18.1.4 Integer types capable of holding object pointers -#ifdef _WIN64 // [ +/* 7.18.1.4 Integer types capable of holding object pointers */ +#ifdef _WIN64 /* [ */ typedef signed __int64 intptr_t; typedef unsigned __int64 uintptr_t; -#else // _WIN64 ][ +#else /* _WIN64 ][ */ typedef _W64 signed int intptr_t; typedef _W64 unsigned int uintptr_t; -#endif // _WIN64 ] +#endif /* _WIN64 ] */ -// 7.18.1.5 Greatest-width integer types +/* 7.18.1.5 Greatest-width integer types */ typedef int64_t intmax_t; typedef uint64_t uintmax_t; -// 7.18.2 Limits of specified-width integer types +/* 7.18.2 Limits of specified-width integer types */ -#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [ See footnote 220 at page 257 and footnote 221 at page 259 +#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) /* [ See footnote 220 at page 257 and footnote 221 at page 259 */ -// 7.18.2.1 Limits of exact-width integer types +/* 7.18.2.1 Limits of exact-width integer types */ #define INT8_MIN ((int8_t)_I8_MIN) #define INT8_MAX _I8_MAX #define INT16_MIN ((int16_t)_I16_MIN) @@ -142,7 +142,7 @@ typedef uint64_t uintmax_t; #define UINT32_MAX _UI32_MAX #define UINT64_MAX _UI64_MAX -// 7.18.2.2 Limits of minimum-width integer types +/* 7.18.2.2 Limits of minimum-width integer types */ #define INT_LEAST8_MIN INT8_MIN #define INT_LEAST8_MAX INT8_MAX #define INT_LEAST16_MIN INT16_MIN @@ -156,7 +156,7 @@ typedef uint64_t uintmax_t; #define UINT_LEAST32_MAX UINT32_MAX #define UINT_LEAST64_MAX UINT64_MAX -// 7.18.2.3 Limits of fastest minimum-width integer types +/* 7.18.2.3 Limits of fastest minimum-width integer types */ #define INT_FAST8_MIN INT8_MIN #define INT_FAST8_MAX INT8_MAX #define INT_FAST16_MIN INT16_MIN @@ -170,62 +170,62 @@ typedef uint64_t uintmax_t; #define UINT_FAST32_MAX UINT32_MAX #define UINT_FAST64_MAX UINT64_MAX -// 7.18.2.4 Limits of integer types capable of holding object pointers -#ifdef _WIN64 // [ +/* 7.18.2.4 Limits of integer types capable of holding object pointers */ +#ifdef _WIN64 /* [ */ # define INTPTR_MIN INT64_MIN # define INTPTR_MAX INT64_MAX # define UINTPTR_MAX UINT64_MAX -#else // _WIN64 ][ +#else /* _WIN64 ][ */ # define INTPTR_MIN INT32_MIN # define INTPTR_MAX INT32_MAX # define UINTPTR_MAX UINT32_MAX -#endif // _WIN64 ] +#endif /* _WIN64 ] */ -// 7.18.2.5 Limits of greatest-width integer types +/* 7.18.2.5 Limits of greatest-width integer types */ #define INTMAX_MIN INT64_MIN #define INTMAX_MAX INT64_MAX #define UINTMAX_MAX UINT64_MAX -// 7.18.3 Limits of other integer types +/* 7.18.3 Limits of other integer types */ -#ifdef _WIN64 // [ +#ifdef _WIN64 /* [ */ # define PTRDIFF_MIN _I64_MIN # define PTRDIFF_MAX _I64_MAX -#else // _WIN64 ][ +#else /* _WIN64 ][ */ # define PTRDIFF_MIN _I32_MIN # define PTRDIFF_MAX _I32_MAX -#endif // _WIN64 ] +#endif /* _WIN64 ] */ #define SIG_ATOMIC_MIN INT_MIN #define SIG_ATOMIC_MAX INT_MAX -#ifndef SIZE_MAX // [ -# ifdef _WIN64 // [ +#ifndef SIZE_MAX /* [ */ +# ifdef _WIN64 /* [ */ # define SIZE_MAX _UI64_MAX -# else // _WIN64 ][ +# else /* _WIN64 ][ */ # define SIZE_MAX _UI32_MAX -# endif // _WIN64 ] -#endif // SIZE_MAX ] +# endif /* _WIN64 ] */ +#endif /* SIZE_MAX ] */ -// WCHAR_MIN and WCHAR_MAX are also defined in -#ifndef WCHAR_MIN // [ +/* WCHAR_MIN and WCHAR_MAX are also defined in */ +#ifndef WCHAR_MIN /* [ */ # define WCHAR_MIN 0 -#endif // WCHAR_MIN ] -#ifndef WCHAR_MAX // [ +#endif /* WCHAR_MIN ] */ +#ifndef WCHAR_MAX /* [ */ # define WCHAR_MAX _UI16_MAX -#endif // WCHAR_MAX ] +#endif /* WCHAR_MAX ] */ #define WINT_MIN 0 #define WINT_MAX _UI16_MAX -#endif // __STDC_LIMIT_MACROS ] +#endif /* __STDC_LIMIT_MACROS ] */ -// 7.18.4 Limits of other integer types +/* 7.18.4 Limits of other integer types */ -#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260 +#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) /* [ See footnote 224 at page 260 */ -// 7.18.4.1 Macros for minimum-width integer constants +/* 7.18.4.1 Macros for minimum-width integer constants */ #define INT8_C(val) val##i8 #define INT16_C(val) val##i16 @@ -237,11 +237,13 @@ typedef uint64_t uintmax_t; #define UINT32_C(val) val##ui32 #define UINT64_C(val) val##ui64 -// 7.18.4.2 Macros for greatest-width integer constants +/* 7.18.4.2 Macros for greatest-width integer constants */ #define INTMAX_C INT64_C #define UINTMAX_C UINT64_C -#endif // __STDC_CONSTANT_MACROS ] +#endif /* __STDC_CONSTANT_MACROS ] */ + +#endif /* _MSC_STDINT_H_ ] */ -#endif // _MSC_STDINT_H_ ] +#endif /* _MSC_VER ] */ diff --git a/include/git2/strarray.h b/include/git2/strarray.h index 6ea570c148e..dcb628a1846 100644 --- a/include/git2/strarray.h +++ b/include/git2/strarray.h @@ -11,8 +11,8 @@ /** * @file git2/strarray.h - * @brief Git string array routines - * @defgroup git_strarray Git string array routines + * @brief An array of strings for the user to free + * @defgroup git_strarray An array of strings for the user to free * @ingroup Git * @{ */ @@ -20,41 +20,23 @@ GIT_BEGIN_DECL /** Array of strings */ typedef struct git_strarray { - char **strings; - size_t count; + char **strings; + size_t count; } git_strarray; /** - * Close a string array object - * - * This method should be called on `git_strarray` objects where the strings - * array is allocated and contains allocated strings, such as what you - * would get from `git_strarray_copy()`. Not doing so, will result in a - * memory leak. + * Free the strings contained in a string array. This method should + * be called on `git_strarray` objects that were provided by the + * library. Not doing so, will result in a memory leak. * * This does not free the `git_strarray` itself, since the library will - * never allocate that object directly itself (it is more commonly embedded - * inside another struct or created on the stack). - * - * @param array git_strarray from which to free string data - */ -GIT_EXTERN(void) git_strarray_free(git_strarray *array); - -/** - * Copy a string array object from source to target. - * - * Note: target is overwritten and hence should be empty, - * otherwise its contents are leaked. + * never allocate that object directly itself. * - * @param tgt target - * @param src source - * @return 0 on success, < 0 on allocation failure + * @param array The git_strarray that contains strings to free */ -GIT_EXTERN(int) git_strarray_copy(git_strarray *tgt, const git_strarray *src); - +GIT_EXTERN(void) git_strarray_dispose(git_strarray *array); /** @} */ GIT_END_DECL #endif - diff --git a/include/git2/submodule.h b/include/git2/submodule.h index 1abd33e79f0..911b3cee39c 100644 --- a/include/git2/submodule.h +++ b/include/git2/submodule.h @@ -10,55 +10,24 @@ #include "common.h" #include "types.h" #include "oid.h" +#include "remote.h" +#include "checkout.h" /** * @file git2/submodule.h - * @brief Git submodule management utilities - * @defgroup git_submodule Git submodule management routines - * @ingroup Git - * @{ - */ -GIT_BEGIN_DECL - -/** - * Opaque structure representing a submodule. + * @brief Submodules place another repository's contents within this one * * Submodule support in libgit2 builds a list of known submodules and keeps * it in the repository. The list is built from the .gitmodules file, the * .git/config file, the index, and the HEAD tree. Items in the working * directory that look like submodules (i.e. a git repo) but are not * mentioned in those places won't be tracked. - */ -typedef struct git_submodule git_submodule; - -/** - * Values that could be specified for the update rule of a submodule. - * - * Use the DEFAULT value if you have altered the update value via - * `git_submodule_set_update()` and wish to reset to the original default. - */ -typedef enum { - GIT_SUBMODULE_UPDATE_DEFAULT = -1, - GIT_SUBMODULE_UPDATE_CHECKOUT = 0, - GIT_SUBMODULE_UPDATE_REBASE = 1, - GIT_SUBMODULE_UPDATE_MERGE = 2, - GIT_SUBMODULE_UPDATE_NONE = 3 -} git_submodule_update_t; - -/** - * Values that could be specified for how closely to examine the - * working directory when getting submodule status. * - * Use the DEFUALT value if you have altered the ignore value via - * `git_submodule_set_ignore()` and wish to reset to the original value. + * @defgroup git_submodule Git submodule management routines + * @ingroup Git + * @{ */ -typedef enum { - GIT_SUBMODULE_IGNORE_DEFAULT = -1, /* reset to default */ - GIT_SUBMODULE_IGNORE_NONE = 0, /* any change or untracked == dirty */ - GIT_SUBMODULE_IGNORE_UNTRACKED = 1, /* dirty if tracked files change */ - GIT_SUBMODULE_IGNORE_DIRTY = 2, /* only dirty if HEAD moved */ - GIT_SUBMODULE_IGNORE_ALL = 3 /* never dirty */ -} git_submodule_ignore_t; +GIT_BEGIN_DECL /** * Return codes for submodule status. @@ -103,33 +72,134 @@ typedef enum { * * WD_UNTRACKED - wd contains untracked files */ typedef enum { - GIT_SUBMODULE_STATUS_IN_HEAD = (1u << 0), - GIT_SUBMODULE_STATUS_IN_INDEX = (1u << 1), - GIT_SUBMODULE_STATUS_IN_CONFIG = (1u << 2), - GIT_SUBMODULE_STATUS_IN_WD = (1u << 3), - GIT_SUBMODULE_STATUS_INDEX_ADDED = (1u << 4), - GIT_SUBMODULE_STATUS_INDEX_DELETED = (1u << 5), - GIT_SUBMODULE_STATUS_INDEX_MODIFIED = (1u << 6), - GIT_SUBMODULE_STATUS_WD_UNINITIALIZED = (1u << 7), - GIT_SUBMODULE_STATUS_WD_ADDED = (1u << 8), - GIT_SUBMODULE_STATUS_WD_DELETED = (1u << 9), - GIT_SUBMODULE_STATUS_WD_MODIFIED = (1u << 10), - GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED = (1u << 11), - GIT_SUBMODULE_STATUS_WD_WD_MODIFIED = (1u << 12), - GIT_SUBMODULE_STATUS_WD_UNTRACKED = (1u << 13), + GIT_SUBMODULE_STATUS_IN_HEAD = (1u << 0), + GIT_SUBMODULE_STATUS_IN_INDEX = (1u << 1), + GIT_SUBMODULE_STATUS_IN_CONFIG = (1u << 2), + GIT_SUBMODULE_STATUS_IN_WD = (1u << 3), + GIT_SUBMODULE_STATUS_INDEX_ADDED = (1u << 4), + GIT_SUBMODULE_STATUS_INDEX_DELETED = (1u << 5), + GIT_SUBMODULE_STATUS_INDEX_MODIFIED = (1u << 6), + GIT_SUBMODULE_STATUS_WD_UNINITIALIZED = (1u << 7), + GIT_SUBMODULE_STATUS_WD_ADDED = (1u << 8), + GIT_SUBMODULE_STATUS_WD_DELETED = (1u << 9), + GIT_SUBMODULE_STATUS_WD_MODIFIED = (1u << 10), + GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED = (1u << 11), + GIT_SUBMODULE_STATUS_WD_WD_MODIFIED = (1u << 12), + GIT_SUBMODULE_STATUS_WD_UNTRACKED = (1u << 13) } git_submodule_status_t; +/** Submodule source bits */ +#define GIT_SUBMODULE_STATUS__IN_FLAGS 0x000Fu +/** Submodule index status */ +#define GIT_SUBMODULE_STATUS__INDEX_FLAGS 0x0070u +/** Submodule working directory status */ +#define GIT_SUBMODULE_STATUS__WD_FLAGS 0x3F80u + +/** Whether the submodule is modified */ #define GIT_SUBMODULE_STATUS_IS_UNMODIFIED(S) \ - (((S) & ~(GIT_SUBMODULE_STATUS_IN_HEAD | \ - GIT_SUBMODULE_STATUS_IN_INDEX | \ - GIT_SUBMODULE_STATUS_IN_CONFIG | \ - GIT_SUBMODULE_STATUS_IN_WD)) == 0) + (((S) & ~GIT_SUBMODULE_STATUS__IN_FLAGS) == 0) +/** Whether the submodule is modified (in the index) */ +#define GIT_SUBMODULE_STATUS_IS_INDEX_UNMODIFIED(S) \ + (((S) & GIT_SUBMODULE_STATUS__INDEX_FLAGS) == 0) + +/** Whether the submodule is modified (in the working directory) */ +#define GIT_SUBMODULE_STATUS_IS_WD_UNMODIFIED(S) \ + (((S) & (GIT_SUBMODULE_STATUS__WD_FLAGS & \ + ~GIT_SUBMODULE_STATUS_WD_UNINITIALIZED)) == 0) + +/** Whether the submodule working directory is dirty */ #define GIT_SUBMODULE_STATUS_IS_WD_DIRTY(S) \ (((S) & (GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED | \ GIT_SUBMODULE_STATUS_WD_WD_MODIFIED | \ GIT_SUBMODULE_STATUS_WD_UNTRACKED)) != 0) +/** + * Function pointer to receive each submodule + * + * @param sm git_submodule currently being visited + * @param name name of the submodule + * @param payload value you passed to the foreach function as payload + * @return 0 on success or error code + */ +typedef int GIT_CALLBACK(git_submodule_cb)( + git_submodule *sm, const char *name, void *payload); + +/** + * Submodule update options structure + * + * Initialize with `GIT_SUBMODULE_UPDATE_OPTIONS_INIT`. Alternatively, you can + * use `git_submodule_update_options_init`. + * + */ +typedef struct git_submodule_update_options { + unsigned int version; + + /** + * These options are passed to the checkout step. To disable + * checkout, set the `checkout_strategy` to `GIT_CHECKOUT_NONE` + * or `GIT_CHECKOUT_DRY_RUN`. + */ + git_checkout_options checkout_opts; + + /** + * Options which control the fetch, including callbacks. + * + * The callbacks to use for reporting fetch progress, and for acquiring + * credentials in the event they are needed. + */ + git_fetch_options fetch_opts; + + /** + * Allow fetching from the submodule's default remote if the target + * commit isn't found. Enabled by default. + */ + int allow_fetch; +} git_submodule_update_options; + +/** Current version for the `git_submodule_update_options` structure */ +#define GIT_SUBMODULE_UPDATE_OPTIONS_VERSION 1 + +/** Static constructor for `git_submodule_update_options` */ +#define GIT_SUBMODULE_UPDATE_OPTIONS_INIT \ + { GIT_SUBMODULE_UPDATE_OPTIONS_VERSION, \ + GIT_CHECKOUT_OPTIONS_INIT, \ + GIT_FETCH_OPTIONS_INIT, \ + 1 } + +/** + * Initialize git_submodule_update_options structure + * + * Initializes a `git_submodule_update_options` with default values. Equivalent to + * creating an instance with `GIT_SUBMODULE_UPDATE_OPTIONS_INIT`. + * + * @param opts The `git_submodule_update_options` struct to initialize. + * @param version The struct version; pass `GIT_SUBMODULE_UPDATE_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_submodule_update_options_init( + git_submodule_update_options *opts, unsigned int version); + +/** + * Update a submodule. This will clone a missing submodule and + * checkout the subrepository to the commit specified in the index of + * the containing repository. If the submodule repository doesn't contain + * the target commit (e.g. because fetchRecurseSubmodules isn't set), then + * the submodule is fetched using the fetch options supplied in options. + * + * @param submodule Submodule object + * @param init If the submodule is not initialized, setting this flag to true + * will initialize the submodule before updating. Otherwise, this will + * return an error if attempting to update an uninitialized repository. + * but setting this to true forces them to be updated. + * @param options configuration options for the update. If NULL, the + * function works as though GIT_SUBMODULE_UPDATE_OPTIONS_INIT was passed. + * @return 0 on success, any non-zero return value from a callback + * function, or a negative value to indicate an error (use + * `git_error_last` for a detailed error message). + */ +GIT_EXTERN(int) git_submodule_update(git_submodule *submodule, int init, git_submodule_update_options *options); + /** * Lookup submodule information by name or path. * @@ -140,34 +210,50 @@ typedef enum { * * - The submodule is not mentioned in the HEAD, the index, and the config, * but does "exist" in the working directory (i.e. there is a subdirectory - * that is a valid self-contained git repo). In this case, this function - * returns GIT_EEXISTS to indicate the the submodule exists but not in a + * that appears to be a Git repository). In this case, this function + * returns GIT_EEXISTS to indicate a sub-repository exists but not in a * state where a git_submodule can be instantiated. * - The submodule is not mentioned in the HEAD, index, or config and the * working directory doesn't contain a value git repo at that path. * There may or may not be anything else at that path, but nothing that * looks like a submodule. In this case, this returns GIT_ENOTFOUND. * - * The submodule object is owned by the containing repo and will be freed - * when the repo is freed. The caller need not free the submodule. + * You must call `git_submodule_free` when done with the submodule. * - * @param submodule Pointer to submodule description object pointer.. - * @param repo The repository. - * @param name The name of the submodule. Trailing slashes will be ignored. + * @param out Output ptr to submodule; pass NULL to just get return code + * @param repo The parent repository + * @param name The name of or path to the submodule; trailing slashes okay * @return 0 on success, GIT_ENOTFOUND if submodule does not exist, - * GIT_EEXISTS if submodule exists in working directory only, -1 on - * other errors. + * GIT_EEXISTS if a repository is found in working directory only, + * -1 on other errors. */ GIT_EXTERN(int) git_submodule_lookup( - git_submodule **submodule, + git_submodule **out, git_repository *repo, const char *name); +/** + * Create an in-memory copy of a submodule. The copy must be explicitly + * free'd or it will leak. + * + * @param out Pointer to store the copy of the submodule. + * @param source Original submodule to copy. + * @return 0 + */ +GIT_EXTERN(int) git_submodule_dup(git_submodule **out, git_submodule *source); + +/** + * Release a submodule + * + * @param submodule Submodule object + */ +GIT_EXTERN(void) git_submodule_free(git_submodule *submodule); + /** * Iterate over all tracked submodules of a repository. * * See the note on `git_submodule` above. This iterates over the tracked - * submodules as decribed therein. + * submodules as described therein. * * If you are concerned about items in the working directory that look like * submodules but are not tracked, the diff API will generate a diff record @@ -183,7 +269,7 @@ GIT_EXTERN(int) git_submodule_lookup( */ GIT_EXTERN(int) git_submodule_foreach( git_repository *repo, - int (*callback)(git_submodule *sm, const char *name, void *payload), + git_submodule_cb callback, void *payload); /** @@ -196,13 +282,16 @@ GIT_EXTERN(int) git_submodule_foreach( * from the working directory to the new repo. * * To fully emulate "git submodule add" call this function, then open the - * submodule repo and perform the clone step as needed. Lastly, call + * submodule repo and perform the clone step as needed (if you don't need + * anything custom see `git_submodule_add_clone()`). Lastly, call * `git_submodule_add_finalize()` to wrap up adding the new submodule and * .gitmodules to the index to be ready to commit. * - * @param submodule The newly created submodule ready to open for clone - * @param repo Superproject repository to contain the new submodule - * @param url URL for the submodules remote + * You must call `git_submodule_free` on the submodule object when done. + * + * @param out The newly created submodule ready to open for clone + * @param repo The repository in which you want to create the submodule + * @param url URL for the submodule's remote * @param path Path at which the submodule should be created * @param use_gitlink Should workdir contain a gitlink to the repo in * .git/modules vs. repo directly in workdir. @@ -210,12 +299,28 @@ GIT_EXTERN(int) git_submodule_foreach( * -1 on other errors. */ GIT_EXTERN(int) git_submodule_add_setup( - git_submodule **submodule, + git_submodule **out, git_repository *repo, const char *url, const char *path, int use_gitlink); +/** + * Perform the clone step for a newly created submodule. + * + * This performs the necessary `git_clone` to setup a newly-created submodule. + * + * @param out The newly created repository object. Optional. + * @param submodule The submodule currently waiting for its clone. + * @param opts The options to use. + * + * @return 0 on success, -1 on other errors (see git_clone). + */ +GIT_EXTERN(int) git_submodule_clone( + git_repository **out, + git_submodule *submodule, + const git_submodule_update_options *opts); + /** * Resolve the setup of a new git submodule. * @@ -225,6 +330,7 @@ GIT_EXTERN(int) git_submodule_add_setup( * (but doesn't actually do the commit). * * @param submodule The submodule to finish adding. + * @return 0 or an error code. */ GIT_EXTERN(int) git_submodule_add_finalize(git_submodule *submodule); @@ -242,20 +348,6 @@ GIT_EXTERN(int) git_submodule_add_to_index( git_submodule *submodule, int write_index); -/** - * Write submodule settings to .gitmodules file. - * - * This commits any in-memory changes to the submodule to the gitmodules - * file on disk. You may also be interested in `git_submodule_init()` which - * writes submodule info to ".git/config" (which is better for local changes - * to submodule settings) and/or `git_submodule_sync()` which writes - * settings about remotes to the actual submodule repository. - * - * @param submodule The submodule to write. - * @return 0 on success, <0 on failure. - */ -GIT_EXTERN(int) git_submodule_save(git_submodule *submodule); - /** * Get the containing repository for a submodule. * @@ -297,20 +389,49 @@ GIT_EXTERN(const char *) git_submodule_path(git_submodule *submodule); GIT_EXTERN(const char *) git_submodule_url(git_submodule *submodule); /** - * Set the URL for the submodule. + * Resolve a submodule url relative to the given repository. * - * This sets the URL in memory for the submodule. This will be used for - * any following submodule actions while this submodule data is in memory. + * @param out buffer to store the absolute submodule url in + * @param repo Pointer to repository object + * @param url Relative url + * @return 0 or an error code + */ +GIT_EXTERN(int) git_submodule_resolve_url(git_buf *out, git_repository *repo, const char *url); + +/** +* Get the branch for the submodule. +* +* @param submodule Pointer to submodule object +* @return Pointer to the submodule branch +*/ +GIT_EXTERN(const char *) git_submodule_branch(git_submodule *submodule); + +/** + * Set the branch for the submodule in the configuration + * + * After calling this, you may wish to call `git_submodule_sync()` to + * write the changes to the checked out submodule repository. * - * After calling this, you may wish to call `git_submodule_save()` to write - * the changes back to the ".gitmodules" file and `git_submodule_sync()` to + * @param repo the repository to affect + * @param name the name of the submodule to configure + * @param branch Branch that should be used for the submodule + * @return 0 on success, <0 on failure + */ +GIT_EXTERN(int) git_submodule_set_branch(git_repository *repo, const char *name, const char *branch); + +/** + * Set the URL for the submodule in the configuration + * + * + * After calling this, you may wish to call `git_submodule_sync()` to * write the changes to the checked out submodule repository. * - * @param submodule Pointer to the submodule object + * @param repo the repository to affect + * @param name the name of the submodule to configure * @param url URL that should be used for the submodule * @return 0 on success, <0 on failure */ -GIT_EXTERN(int) git_submodule_set_url(git_submodule *submodule, const char *url); +GIT_EXTERN(int) git_submodule_set_url(git_repository *repo, const char *name, const char *url); /** * Get the OID for the submodule in the index. @@ -342,9 +463,10 @@ GIT_EXTERN(const git_oid *) git_submodule_head_id(git_submodule *submodule); GIT_EXTERN(const git_oid *) git_submodule_wd_id(git_submodule *submodule); /** - * Get the ignore rule for the submodule. + * Get the ignore rule that will be used for the submodule. * - * There are four ignore values: + * These values control the behavior of `git_submodule_status()` for this + * submodule. There are four ignore values: * * - **GIT_SUBMODULE_IGNORE_NONE** will consider any change to the contents * of the submodule from a clean checkout to be dirty, including the @@ -358,48 +480,55 @@ GIT_EXTERN(const git_oid *) git_submodule_wd_id(git_submodule *submodule); * - **GIT_SUBMODULE_IGNORE_ALL** means not to open the submodule repo. * The working directory will be consider clean so long as there is a * checked out version present. + * + * @param submodule The submodule to check + * @return The current git_submodule_ignore_t valyue what will be used for + * this submodule. */ GIT_EXTERN(git_submodule_ignore_t) git_submodule_ignore( git_submodule *submodule); /** - * Set the ignore rule for the submodule. + * Set the ignore rule for the submodule in the configuration * - * This sets the ignore rule in memory for the submodule. This will be used - * for any following actions (such as `git_submodule_status()`) while the - * submodule is in memory. You should call `git_submodule_save()` if you - * want to persist the new ignore role. + * This does not affect any currently-loaded instances. * - * Calling this again with GIT_SUBMODULE_IGNORE_DEFAULT or calling - * `git_submodule_reload()` will revert the rule to the value that was in the - * original config. - * - * @return old value for ignore + * @param repo the repository to affect + * @param name the name of the submdule + * @param ignore The new value for the ignore rule + * @return 0 or an error code */ -GIT_EXTERN(git_submodule_ignore_t) git_submodule_set_ignore( - git_submodule *submodule, +GIT_EXTERN(int) git_submodule_set_ignore( + git_repository *repo, + const char *name, git_submodule_ignore_t ignore); /** - * Get the update rule for the submodule. + * Get the update rule that will be used for the submodule. + * + * This value controls the behavior of the `git submodule update` command. + * There are four useful values documented with `git_submodule_update_t`. + * + * @param submodule The submodule to check + * @return The current git_submodule_update_t value that will be used + * for this submodule. */ -GIT_EXTERN(git_submodule_update_t) git_submodule_update( +GIT_EXTERN(git_submodule_update_t) git_submodule_update_strategy( git_submodule *submodule); /** - * Set the update rule for the submodule. - * - * This sets the update rule in memory for the submodule. You should call - * `git_submodule_save()` if you want to persist the new update rule. + * Set the update rule for the submodule in the configuration * - * Calling this again with GIT_SUBMODULE_UPDATE_DEFAULT or calling - * `git_submodule_reload()` will revert the rule to the value that was in the - * original config. + * This setting won't affect any existing instances. * - * @return old value for update + * @param repo the repository to affect + * @param name the name of the submodule to configure + * @param update The new value to use + * @return 0 or an error code */ -GIT_EXTERN(git_submodule_update_t) git_submodule_set_update( - git_submodule *submodule, +GIT_EXTERN(int) git_submodule_set_update( + git_repository *repo, + const char *name, git_submodule_update_t update); /** @@ -411,25 +540,26 @@ GIT_EXTERN(git_submodule_update_t) git_submodule_set_update( * Note that at this time, libgit2 does not honor this setting and the * fetch functionality current ignores submodules. * - * @return 0 if fetchRecurseSubmodules is false, 1 if true + * @param submodule the submodule to examine + * @return the submodule recursion configuration */ -GIT_EXTERN(int) git_submodule_fetch_recurse_submodules( +GIT_EXTERN(git_submodule_recurse_t) git_submodule_fetch_recurse_submodules( git_submodule *submodule); /** - * Set the fetchRecurseSubmodules rule for a submodule. + * Set the fetchRecurseSubmodules rule for a submodule in the configuration * - * This sets the submodule..fetchRecurseSubmodules value for - * the submodule. You should call `git_submodule_save()` if you want - * to persist the new value. + * This setting won't affect any existing instances. * - * @param submodule The submodule to modify - * @param fetch_recurse_submodules Boolean value + * @param repo the repository to affect + * @param name the submodule to configure + * @param fetch_recurse_submodules the submodule recursion configuration * @return old value for fetchRecurseSubmodules */ GIT_EXTERN(int) git_submodule_set_fetch_recurse_submodules( - git_submodule *submodule, - int fetch_recurse_submodules); + git_repository *repo, + const char *name, + git_submodule_recurse_t fetch_recurse_submodules); /** * Copy submodule info into ".git/config" file. @@ -446,6 +576,24 @@ GIT_EXTERN(int) git_submodule_set_fetch_recurse_submodules( */ GIT_EXTERN(int) git_submodule_init(git_submodule *submodule, int overwrite); +/** + * Set up the subrepository for a submodule in preparation for clone. + * + * This function can be called to init and set up a submodule + * repository from a submodule in preparation to clone it from + * its remote. + * + * @param out Output pointer to the created git repository. + * @param sm The submodule to create a new subrepository from. + * @param use_gitlink Should the workdir contain a gitlink to + * the repo in .git/modules vs. repo directly in workdir. + * @return 0 on success, <0 on failure. + */ +GIT_EXTERN(int) git_submodule_repo_init( + git_repository **out, + const git_submodule *sm, + int use_gitlink); + /** * Copy submodule remote info into submodule repo. * @@ -453,6 +601,9 @@ GIT_EXTERN(int) git_submodule_init(git_submodule *submodule, int overwrite); * submodule config, acting like "git submodule sync". This is useful if * you have altered the URL for the submodule (or it has been altered by a * fetch of upstream changes) and you need to update your local repo. + * + * @param submodule The submodule to copy. + * @return 0 or an error code. */ GIT_EXTERN(int) git_submodule_sync(git_submodule *submodule); @@ -464,7 +615,7 @@ GIT_EXTERN(int) git_submodule_sync(git_submodule *submodule); * function will return distinct `git_repository` objects. This will only * work if the submodule is checked out into the working directory. * - * @param subrepo Pointer to the submodule repo which was opened + * @param repo Pointer to the submodule repo which was opened * @param submodule Submodule to be opened * @return 0 on success, <0 if submodule repo could not be opened. */ @@ -477,15 +628,12 @@ GIT_EXTERN(int) git_submodule_open( * * Call this to reread cached submodule information for this submodule if * you have reason to believe that it has changed. - */ -GIT_EXTERN(int) git_submodule_reload(git_submodule *submodule); - -/** - * Reread all submodule info. * - * Call this to reload all cached submodule information for the repo. + * @param submodule The submodule to reload + * @param force Force reload even if the data doesn't seem out of date + * @return 0 on success, <0 on error */ -GIT_EXTERN(int) git_submodule_reload_all(git_repository *repo); +GIT_EXTERN(int) git_submodule_reload(git_submodule *submodule, int force); /** * Get the status for a submodule. @@ -493,16 +641,19 @@ GIT_EXTERN(int) git_submodule_reload_all(git_repository *repo); * This looks at a submodule and tries to determine the status. It * will return a combination of the `GIT_SUBMODULE_STATUS` values above. * How deeply it examines the working directory to do this will depend - * on the `git_submodule_ignore_t` value for the submodule - which can be - * set either temporarily or permanently with `git_submodule_set_ignore()`. + * on the `git_submodule_ignore_t` value for the submodule. * * @param status Combination of `GIT_SUBMODULE_STATUS` flags - * @param submodule Submodule for which to get status + * @param repo the repository in which to look + * @param name name of the submodule + * @param ignore the ignore rules to follow * @return 0 on success, <0 on error */ GIT_EXTERN(int) git_submodule_status( unsigned int *status, - git_submodule *submodule); + git_repository *repo, + const char *name, + git_submodule_ignore_t ignore); /** * Get the locations of submodule information. @@ -514,7 +665,7 @@ GIT_EXTERN(int) git_submodule_status( * This can be useful if you want to know if the submodule is present in the * working directory at this point in time, etc. * - * @param status Combination of first four `GIT_SUBMODULE_STATUS` flags + * @param location_status Combination of first four `GIT_SUBMODULE_STATUS` flags * @param submodule Submodule for which to get status * @return 0 on success, <0 on error */ @@ -524,4 +675,5 @@ GIT_EXTERN(int) git_submodule_location( /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/sys/alloc.h b/include/git2/sys/alloc.h new file mode 100644 index 00000000000..67506f2b17e --- /dev/null +++ b/include/git2/sys/alloc.h @@ -0,0 +1,79 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_sys_git_alloc_h__ +#define INCLUDE_sys_git_alloc_h__ + +#include "git2/common.h" + +/** + * @file git2/sys/alloc.h + * @brief Custom memory allocators + * @defgroup git_merge Git merge routines + * @ingroup Git + * + * Users can configure custom allocators; this is particularly + * interesting when running in constrained environments, when calling + * from another language, or during testing. + * @{ + */ +GIT_BEGIN_DECL + +/** + * An instance for a custom memory allocator + * + * Setting the pointers of this structure allows the developer to implement + * custom memory allocators. The global memory allocator can be set by using + * "GIT_OPT_SET_ALLOCATOR" with the `git_libgit2_opts` function. Keep in mind + * that all fields need to be set to a proper function. + */ +typedef struct { + /** Allocate `n` bytes of memory */ + void * GIT_CALLBACK(gmalloc)(size_t n, const char *file, int line); + + /** + * This function shall deallocate the old object `ptr` and return a + * pointer to a new object that has the size specified by `size`. In + * case `ptr` is `NULL`, a new array shall be allocated. + */ + void * GIT_CALLBACK(grealloc)(void *ptr, size_t size, const char *file, int line); + + /** + * This function shall free the memory pointed to by `ptr`. In case + * `ptr` is `NULL`, this shall be a no-op. + */ + void GIT_CALLBACK(gfree)(void *ptr); +} git_allocator; + +/** + * Initialize the allocator structure to use the `stdalloc` pointer. + * + * Set up the structure so that all of its members are using the standard + * "stdalloc" allocator functions. The structure can then be used with + * `git_allocator_setup`. + * + * @param allocator The allocator that is to be initialized. + * @return An error code or 0. + */ +int git_stdalloc_init_allocator(git_allocator *allocator); + +/** + * Initialize the allocator structure to use the `crtdbg` pointer. + * + * Set up the structure so that all of its members are using the "crtdbg" + * allocator functions. Note that this allocator is only available on Windows + * platforms and only if libgit2 is being compiled with "-DMSVC_CRTDBG". + * + * @param allocator The allocator that is to be initialized. + * @return An error code or 0. + */ +int git_win32_crtdbg_init_allocator(git_allocator *allocator); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/commit.h b/include/git2/sys/commit.h new file mode 100644 index 00000000000..a8253c06743 --- /dev/null +++ b/include/git2/sys/commit.h @@ -0,0 +1,154 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_commit_h__ +#define INCLUDE_sys_git_commit_h__ + +#include "git2/common.h" +#include "git2/types.h" +#include "git2/oid.h" + +/** + * @file git2/sys/commit.h + * @brief Low-level Git commit creation + * @defgroup git_commit Low-level Git commit creation + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Create new commit in the repository from a list of `git_oid` values. + * + * See documentation for `git_commit_create()` for information about the + * parameters, as the meaning is identical excepting that `tree` and + * `parents` now take `git_oid`. This is a dangerous API in that nor + * the `tree`, neither the `parents` list of `git_oid`s are checked for + * validity. + * + * @param id Pointer in which to store the OID of the newly created commit + * + * @param repo Repository where to store the commit + * + * @param update_ref If not NULL, name of the reference that + * will be updated to point to this commit. If the reference + * is not direct, it will be resolved to a direct reference. + * Use "HEAD" to update the HEAD of the current branch and + * make it point to this commit. If the reference doesn't + * exist yet, it will be created. If it does exist, the first + * parent must be the tip of this branch. + * + * @param author Signature with author and author time of commit + * + * @param committer Signature with committer and * commit time of commit + * + * @param message_encoding The encoding for the message in the + * commit, represented with a standard encoding name. + * E.g. "UTF-8". If NULL, no encoding header is written and + * UTF-8 is assumed. + * + * @param message Full message for this commit + * + * @param tree An instance of a `git_tree` object that will + * be used as the tree for the commit. This tree object must + * also be owned by the given `repo`. + * + * @param parent_count Number of parents for this commit + * + * @param parents Array of `parent_count` pointers to `git_commit` + * objects that will be used as the parents for this commit. This + * array may be NULL if `parent_count` is 0 (root commit). All the + * given commits must be owned by the `repo`. + * + * @return 0 or an error code + * The created commit will be written to the Object Database and + * the given reference will be updated to point to it + */ +GIT_EXTERN(int) git_commit_create_from_ids( + git_oid *id, + git_repository *repo, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_oid *tree, + size_t parent_count, + const git_oid *parents[]); + +/** + * Callback function to return parents for commit. + * + * This is invoked with the count of the number of parents processed so far + * along with the user supplied payload. This should return a git_oid of + * the next parent or NULL if all parents have been provided. + * + * @param idx the index of the parent + * @param payload the user-specified payload + * @return the object id of the parent, or NULL if there are no further parents + */ +typedef const git_oid * GIT_CALLBACK(git_commit_parent_callback)(size_t idx, void *payload); + +/** + * Create a new commit in the repository with an callback to supply parents. + * + * See documentation for `git_commit_create()` for information about the + * parameters, as the meaning is identical excepting that `tree` takes a + * `git_oid` and doesn't check for validity, and `parent_cb` is invoked + * with `parent_payload` and should return `git_oid` values or NULL to + * indicate that all parents are accounted for. + * + * @param id Pointer in which to store the OID of the newly created commit + * + * @param repo Repository where to store the commit + * + * @param update_ref If not NULL, name of the reference that + * will be updated to point to this commit. If the reference + * is not direct, it will be resolved to a direct reference. + * Use "HEAD" to update the HEAD of the current branch and + * make it point to this commit. If the reference doesn't + * exist yet, it will be created. If it does exist, the first + * parent must be the tip of this branch. + * + * @param author Signature with author and author time of commit + * + * @param committer Signature with committer and * commit time of commit + * + * @param message_encoding The encoding for the message in the + * commit, represented with a standard encoding name. + * E.g. "UTF-8". If NULL, no encoding header is written and + * UTF-8 is assumed. + * + * @param message Full message for this commit + * + * @param tree An instance of a `git_tree` object that will + * be used as the tree for the commit. This tree object must + * also be owned by the given `repo`. + * + * @param parent_cb Callback to invoke to obtain parent information + * + * @param parent_payload User-specified payload to provide to the callback + * + * @return 0 or an error code + * The created commit will be written to the Object Database and + * the given reference will be updated to point to it + */ +GIT_EXTERN(int) git_commit_create_from_callback( + git_oid *id, + git_repository *repo, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_oid *tree, + git_commit_parent_callback parent_cb, + void *parent_payload); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/commit_graph.h b/include/git2/sys/commit_graph.h new file mode 100644 index 00000000000..ff547ef0c1f --- /dev/null +++ b/include/git2/sys/commit_graph.h @@ -0,0 +1,225 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_commit_graph_h__ +#define INCLUDE_sys_git_commit_graph_h__ + +#include "git2/common.h" +#include "git2/types.h" + +/** + * @file git2/sys/commit_graph.h + * @brief Commit graphs store information about commit relationships + * @defgroup git_commit_graph Commit graphs + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Options structure for `git_commit_graph_open_new`. + * + * Initialize with `GIT_COMMIT_GRAPH_OPEN_OPTIONS_INIT`. Alternatively, + * you can use `git_commit_graph_open_options_init`. + */ +typedef struct { + unsigned int version; + +#ifdef GIT_EXPERIMENTAL_SHA256 + /** The object ID type that this commit graph contains. */ + git_oid_t oid_type; +#endif +} git_commit_graph_open_options; + +/** Current version for the `git_commit_graph_open_options` structure */ +#define GIT_COMMIT_GRAPH_OPEN_OPTIONS_VERSION 1 + +/** Static constructor for `git_commit_graph_open_options` */ +#define GIT_COMMIT_GRAPH_OPEN_OPTIONS_INIT { \ + GIT_COMMIT_GRAPH_OPEN_OPTIONS_VERSION \ + } + +/** + * Initialize git_commit_graph_open_options structure + * + * Initializes a `git_commit_graph_open_options` with default values. + * Equivalent to creating an instance with + * `GIT_COMMIT_GRAPH_OPEN_OPTIONS_INIT`. + * + * @param opts The `git_commit_graph_open_options` struct to initialize. + * @param version The struct version; pass `GIT_COMMIT_GRAPH_OPEN_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_commit_graph_open_options_init( + git_commit_graph_open_options *opts, + unsigned int version); + + +/** + * Opens a `git_commit_graph` from a path to an objects directory. + * + * This finds, opens, and validates the `commit-graph` file. + * + * @param cgraph_out the `git_commit_graph` struct to initialize. + * @param objects_dir the path to a git objects directory. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_commit_graph_open( + git_commit_graph **cgraph_out, + const char *objects_dir +#ifdef GIT_EXPERIMENTAL_SHA256 + , const git_commit_graph_open_options *options +#endif + ); + +/** + * Frees commit-graph data. This should only be called when memory allocated + * using `git_commit_graph_open` is not returned to libgit2 because it was not + * associated with the ODB through a successful call to + * `git_odb_set_commit_graph`. + * + * @param cgraph the commit-graph object to free. If NULL, no action is taken. + */ +GIT_EXTERN(void) git_commit_graph_free(git_commit_graph *cgraph); + + +/** + * The strategy to use when adding a new set of commits to a pre-existing + * commit-graph chain. + */ +typedef enum { + /** + * Do not split commit-graph files. The other split strategy-related option + * fields are ignored. + */ + GIT_COMMIT_GRAPH_SPLIT_STRATEGY_SINGLE_FILE = 0 +} git_commit_graph_split_strategy_t; + +/** + * Options structure for `git_commit_graph_writer_new`. + * + * Initialize with `GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT`. Alternatively, + * you can use `git_commit_graph_writer_options_init`. + */ +typedef struct { + unsigned int version; + +#ifdef GIT_EXPERIMENTAL_SHA256 + /** The object ID type that this commit graph contains. */ + git_oid_t oid_type; +#endif + + /** + * The strategy to use when adding new commits to a pre-existing commit-graph + * chain. + */ + git_commit_graph_split_strategy_t split_strategy; + + /** + * The number of commits in level N is less than X times the number of + * commits in level N + 1. Default is 2. + */ + float size_multiple; + + /** + * The number of commits in level N + 1 is more than C commits. + * Default is 64000. + */ + size_t max_commits; +} git_commit_graph_writer_options; + +/** Current version for the `git_commit_graph_writer_options` structure */ +#define GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION 1 + +/** Static constructor for `git_commit_graph_writer_options` */ +#define GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT { \ + GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION \ + } + +/** + * Initialize git_commit_graph_writer_options structure + * + * Initializes a `git_commit_graph_writer_options` with default values. Equivalent to + * creating an instance with `GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT`. + * + * @param opts The `git_commit_graph_writer_options` struct to initialize. + * @param version The struct version; pass `GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_commit_graph_writer_options_init( + git_commit_graph_writer_options *opts, + unsigned int version); + +/** + * Create a new writer for `commit-graph` files. + * + * @param out Location to store the writer pointer. + * @param objects_info_dir The `objects/info` directory. + * The `commit-graph` file will be written in this directory. + * @param options The options for the commit graph writer. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_graph_writer_new( + git_commit_graph_writer **out, + const char *objects_info_dir, + const git_commit_graph_writer_options *options); + +/** + * Free the commit-graph writer and its resources. + * + * @param w The writer to free. If NULL no action is taken. + */ +GIT_EXTERN(void) git_commit_graph_writer_free(git_commit_graph_writer *w); + +/** + * Add an `.idx` file (associated to a packfile) to the writer. + * + * @param w The writer. + * @param repo The repository that owns the `.idx` file. + * @param idx_path The path of an `.idx` file. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_graph_writer_add_index_file( + git_commit_graph_writer *w, + git_repository *repo, + const char *idx_path); + +/** + * Add a revwalk to the writer. This will add all the commits from the revwalk + * to the commit-graph. + * + * @param w The writer. + * @param walk The git_revwalk. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_graph_writer_add_revwalk( + git_commit_graph_writer *w, + git_revwalk *walk); + +/** + * Write a `commit-graph` file to a file. + * + * @param w The writer + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_graph_writer_commit( + git_commit_graph_writer *w); + +/** + * Dump the contents of the `commit-graph` to an in-memory buffer. + * + * @param[out] buffer Buffer where to store the contents of the `commit-graph`. + * @param w The writer. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_graph_writer_dump( + git_buf *buffer, + git_commit_graph_writer *w); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/config.h b/include/git2/sys/config.h new file mode 100644 index 00000000000..cc4a3991ddc --- /dev/null +++ b/include/git2/sys/config.h @@ -0,0 +1,206 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_config_backend_h__ +#define INCLUDE_sys_git_config_backend_h__ + +#include "git2/common.h" +#include "git2/types.h" +#include "git2/config.h" + +/** + * @file git2/sys/config.h + * @brief Custom configuration database backends + * @defgroup git_backend Custom configuration database backends + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * An entry in a configuration backend. This is provided so that + * backend implementors can have a mechanism to free their data. + */ +typedef struct git_config_backend_entry { + /** The base configuration entry */ + struct git_config_entry entry; + + /** + * Free function for this entry; for internal purposes. Callers + * should call `git_config_entry_free` to free data. + */ + void GIT_CALLBACK(free)(struct git_config_backend_entry *entry); +} git_config_backend_entry; + +/** + * Every iterator must have this struct as its first element, so the + * API can talk to it. You'd define your iterator as + * + * struct my_iterator { + * git_config_iterator parent; + * ... + * } + * + * and assign `iter->parent.backend` to your `git_config_backend`. + */ +struct git_config_iterator { + git_config_backend *backend; + unsigned int flags; + + /** + * Return the current entry and advance the iterator. The + * memory belongs to the library. + */ + int GIT_CALLBACK(next)(git_config_backend_entry **entry, git_config_iterator *iter); + + /** + * Free the iterator + */ + void GIT_CALLBACK(free)(git_config_iterator *iter); +}; + +/** + * Generic backend that implements the interface to + * access a configuration file + */ +struct git_config_backend { + unsigned int version; + /** True if this backend is for a snapshot */ + int readonly; + struct git_config *cfg; + + /* Open means open the file/database and parse if necessary */ + int GIT_CALLBACK(open)(struct git_config_backend *, git_config_level_t level, const git_repository *repo); + int GIT_CALLBACK(get)(struct git_config_backend *, const char *key, git_config_backend_entry **entry); + int GIT_CALLBACK(set)(struct git_config_backend *, const char *key, const char *value); + int GIT_CALLBACK(set_multivar)(git_config_backend *cfg, const char *name, const char *regexp, const char *value); + int GIT_CALLBACK(del)(struct git_config_backend *, const char *key); + int GIT_CALLBACK(del_multivar)(struct git_config_backend *, const char *key, const char *regexp); + int GIT_CALLBACK(iterator)(git_config_iterator **, struct git_config_backend *); + /** Produce a read-only version of this backend */ + int GIT_CALLBACK(snapshot)(struct git_config_backend **, struct git_config_backend *); + /** + * Lock this backend. + * + * Prevent any writes to the data store backing this + * backend. Any updates must not be visible to any other + * readers. + */ + int GIT_CALLBACK(lock)(struct git_config_backend *); + /** + * Unlock the data store backing this backend. If success is + * true, the changes should be committed, otherwise rolled + * back. + */ + int GIT_CALLBACK(unlock)(struct git_config_backend *, int success); + void GIT_CALLBACK(free)(struct git_config_backend *); +}; + +/** Current version for the `git_config_backend_options` structure */ +#define GIT_CONFIG_BACKEND_VERSION 1 + +/** Static constructor for `git_config_backend_options` */ +#define GIT_CONFIG_BACKEND_INIT {GIT_CONFIG_BACKEND_VERSION} + +/** + * Initializes a `git_config_backend` with default values. Equivalent to + * creating an instance with GIT_CONFIG_BACKEND_INIT. + * + * @param backend the `git_config_backend` struct to initialize. + * @param version Version of struct; pass `GIT_CONFIG_BACKEND_VERSION` + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_config_init_backend( + git_config_backend *backend, + unsigned int version); + +/** + * Add a generic config file instance to an existing config + * + * Note that the configuration object will free the file + * automatically. + * + * Further queries on this config object will access each + * of the config file instances in order (instances with + * a higher priority level will be accessed first). + * + * @param cfg the configuration to add the file to + * @param file the configuration file (backend) to add + * @param level the priority level of the backend + * @param repo optional repository to allow parsing of + * conditional includes + * @param force if a config file already exists for the given + * priority level, replace it + * @return 0 on success, GIT_EEXISTS when adding more than one file + * for a given priority level (and force_replace set to 0), or error code + */ +GIT_EXTERN(int) git_config_add_backend( + git_config *cfg, + git_config_backend *file, + git_config_level_t level, + const git_repository *repo, + int force); + +/** Options for in-memory configuration backends. */ +typedef struct { + unsigned int version; + + /** + * The type of this backend (eg, "command line"). If this is + * NULL, then this will be "in-memory". + */ + const char *backend_type; + + /** + * The path to the origin; if this is NULL then it will be + * left unset in the resulting configuration entries. + */ + const char *origin_path; +} git_config_backend_memory_options; + +/** Current version for the `git_config_backend_memory_options` structure */ +#define GIT_CONFIG_BACKEND_MEMORY_OPTIONS_VERSION 1 + +/** Static constructor for `git_config_backend_memory_options` */ +#define GIT_CONFIG_BACKEND_MEMORY_OPTIONS_INIT { GIT_CONFIG_BACKEND_MEMORY_OPTIONS_VERSION } + + +/** + * Create an in-memory configuration backend from a string in standard + * git configuration file format. + * + * @param out the new backend + * @param cfg the configuration that is to be parsed + * @param len the length of the string pointed to by `cfg` + * @param opts the options to initialize this backend with, or NULL + * @return 0 on success or an error code + */ +extern int git_config_backend_from_string( + git_config_backend **out, + const char *cfg, + size_t len, + git_config_backend_memory_options *opts); + +/** + * Create an in-memory configuration backend from a list of name/value + * pairs. + * + * @param out the new backend + * @param values the configuration values to set (in "key=value" format) + * @param len the length of the values array + * @param opts the options to initialize this backend with, or NULL + * @return 0 on success or an error code + */ +extern int git_config_backend_from_values( + git_config_backend **out, + const char **values, + size_t len, + git_config_backend_memory_options *opts); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/cred.h b/include/git2/sys/cred.h new file mode 100644 index 00000000000..4d2a59af751 --- /dev/null +++ b/include/git2/sys/cred.h @@ -0,0 +1,15 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_cred_h__ +#define INCLUDE_sys_git_cred_h__ + +/* These declarations have moved. */ +#ifndef GIT_DEPRECATE_HARD +# include "git2/sys/credential.h" +#endif + +#endif diff --git a/include/git2/sys/credential.h b/include/git2/sys/credential.h new file mode 100644 index 00000000000..0d573a3231f --- /dev/null +++ b/include/git2/sys/credential.h @@ -0,0 +1,91 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_credential_h__ +#define INCLUDE_sys_git_credential_h__ + +#include "git2/common.h" +#include "git2/credential.h" + +/** + * @file git2/sys/credential.h + * @brief Low-level credentials implementation + * @defgroup git_credential Low-level credentials implementation + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * The base structure for all credential types + */ +struct git_credential { + git_credential_t credtype; /**< A type of credential */ + + /** The deallocator for this type of credentials */ + void GIT_CALLBACK(free)(git_credential *cred); +}; + +/** A plaintext username and password */ +struct git_credential_userpass_plaintext { + git_credential parent; /**< The parent credential */ + char *username; /**< The username to authenticate as */ + char *password; /**< The password to use */ +}; + +/** Username-only credential information */ +struct git_credential_username { + git_credential parent; /**< The parent credential */ + char username[1]; /**< The username to authenticate as */ +}; + +/** + * A ssh key from disk + */ +struct git_credential_ssh_key { + git_credential parent; /**< The parent credential */ + char *username; /**< The username to authenticate as */ + char *publickey; /**< The path to a public key */ + char *privatekey; /**< The path to a private key */ + char *passphrase; /**< Passphrase to decrypt the private key */ +}; + +/** + * Keyboard-interactive based ssh authentication + */ +struct git_credential_ssh_interactive { + git_credential parent; /**< The parent credential */ + char *username; /**< The username to authenticate as */ + + /** + * Callback used for authentication. + */ + git_credential_ssh_interactive_cb prompt_callback; + + void *payload; /**< Payload passed to prompt_callback */ +}; + +/** + * A key with a custom signature function + */ +struct git_credential_ssh_custom { + git_credential parent; /**< The parent credential */ + char *username; /**< The username to authenticate as */ + char *publickey; /**< The public key data */ + size_t publickey_len; /**< Length of the public key */ + + /** + * Callback used to sign the data. + */ + git_credential_sign_cb sign_callback; + + void *payload; /**< Payload passed to prompt_callback */ +}; + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/diff.h b/include/git2/sys/diff.h new file mode 100644 index 00000000000..a398f5490fb --- /dev/null +++ b/include/git2/sys/diff.h @@ -0,0 +1,114 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_diff_h__ +#define INCLUDE_sys_git_diff_h__ + +#include "git2/common.h" +#include "git2/types.h" +#include "git2/oid.h" +#include "git2/diff.h" +#include "git2/status.h" + +/** + * @file git2/sys/diff.h + * @brief Low-level diff utilities + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Diff print callback that writes to a git_buf. + * + * This function is provided not for you to call it directly, but instead + * so you can use it as a function pointer to the `git_diff_print` or + * `git_patch_print` APIs. When using those APIs, you specify a callback + * to actually handle the diff and/or patch data. + * + * Use this callback to easily write that data to a `git_buf` buffer. You + * must pass a `git_buf *` value as the payload to the `git_diff_print` + * and/or `git_patch_print` function. The data will be appended to the + * buffer (after any existing content). + * + * @param delta the delta being processed + * @param hunk the hunk being processed + * @param line the line being processed + * @param payload the payload provided by the diff generator + * @return 0 on success or an error code + */ +GIT_EXTERN(int) git_diff_print_callback__to_buf( + const git_diff_delta *delta, + const git_diff_hunk *hunk, + const git_diff_line *line, + void *payload); /**< payload must be a `git_buf *` */ + +/** + * Diff print callback that writes to stdio FILE handle. + * + * This function is provided not for you to call it directly, but instead + * so you can use it as a function pointer to the `git_diff_print` or + * `git_patch_print` APIs. When using those APIs, you specify a callback + * to actually handle the diff and/or patch data. + * + * Use this callback to easily write that data to a stdio FILE handle. You + * must pass a `FILE *` value (such as `stdout` or `stderr` or the return + * value from `fopen()`) as the payload to the `git_diff_print` + * and/or `git_patch_print` function. If you pass NULL, this will write + * data to `stdout`. + * + * @param delta the delta being processed + * @param hunk the hunk being processed + * @param line the line being processed + * @param payload the payload provided by the diff generator + * @return 0 on success or an error code + */ +GIT_EXTERN(int) git_diff_print_callback__to_file_handle( + const git_diff_delta *delta, + const git_diff_hunk *hunk, + const git_diff_line *line, + void *payload); /**< payload must be a `FILE *` */ + + +/** + * Performance data from diffing + */ +typedef struct { + unsigned int version; + size_t stat_calls; /**< Number of stat() calls performed */ + size_t oid_calculations; /**< Number of ID calculations */ +} git_diff_perfdata; + +/** Current version for the `git_diff_perfdata_options` structure */ +#define GIT_DIFF_PERFDATA_VERSION 1 + +/** Static constructor for `git_diff_perfdata_options` */ +#define GIT_DIFF_PERFDATA_INIT {GIT_DIFF_PERFDATA_VERSION,0,0} + +/** + * Get performance data for a diff object. + * + * @param out Structure to be filled with diff performance data + * @param diff Diff to read performance data from + * @return 0 for success, <0 for error + */ +GIT_EXTERN(int) git_diff_get_perfdata( + git_diff_perfdata *out, const git_diff *diff); + +/** + * Get performance data for diffs from a git_status_list + * + * @param out Structure to be filled with diff performance data + * @param status Diff to read performance data from + * @return 0 for success, <0 for error + */ +GIT_EXTERN(int) git_status_list_get_perfdata( + git_diff_perfdata *out, const git_status_list *status); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/email.h b/include/git2/sys/email.h new file mode 100644 index 00000000000..26e792abf89 --- /dev/null +++ b/include/git2/sys/email.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_email_h__ +#define INCLUDE_sys_git_email_h__ + +#include "git2/common.h" +#include "git2/diff.h" +#include "git2/email.h" +#include "git2/types.h" + +/** + * @file git2/sys/email.h + * @brief Advanced git email creation routines + * @defgroup git_email Advanced git email creation routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Create a diff for a commit in mbox format for sending via email. + * + * @param out buffer to store the e-mail patch in + * @param diff the changes to include in the email + * @param patch_idx the patch index + * @param patch_count the total number of patches that will be included + * @param commit_id the commit id for this change + * @param summary the commit message for this change + * @param body optional text to include above the diffstat + * @param author the person who authored this commit + * @param opts email creation options + * @return 0 on success or an error code + */ +GIT_EXTERN(int) git_email_create_from_diff( + git_buf *out, + git_diff *diff, + size_t patch_idx, + size_t patch_count, + const git_oid *commit_id, + const char *summary, + const char *body, + const git_signature *author, + const git_email_create_options *opts); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/errors.h b/include/git2/sys/errors.h new file mode 100644 index 00000000000..44e8ecba84f --- /dev/null +++ b/include/git2/sys/errors.h @@ -0,0 +1,76 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_sys_git_errors_h__ +#define INCLUDE_sys_git_errors_h__ + +#include "git2/common.h" + +/** + * @file git2/sys/errors.h + * @brief Advanced error handling + * @ingroup Git + * + * Error handling for advanced consumers; those who use callbacks + * or those who create custom databases. + * @{ + */ +GIT_BEGIN_DECL + +/** + * Clear the last library error that occurred for this thread. + */ +GIT_EXTERN(void) git_error_clear(void); + +/** + * Set the error message string for this thread, using `printf`-style + * formatting. + * + * This function is public so that custom ODB backends and the like can + * relay an error message through libgit2. Most regular users of libgit2 + * will never need to call this function -- actually, calling it in most + * circumstances (for example, calling from within a callback function) + * will just end up having the value overwritten by libgit2 internals. + * + * This error message is stored in thread-local storage and only applies + * to the particular thread that this libgit2 call is made from. + * + * @param error_class One of the `git_error_t` enum above describing the + * general subsystem that is responsible for the error. + * @param fmt The `printf`-style format string; subsequent arguments must + * be the arguments for the format string. + */ +GIT_EXTERN(void) git_error_set(int error_class, const char *fmt, ...) + GIT_FORMAT_PRINTF(2, 3); + +/** + * Set the error message string for this thread. This function is like + * `git_error_set` but takes a static string instead of a `printf`-style + * format. + * + * @param error_class One of the `git_error_t` enum above describing the + * general subsystem that is responsible for the error. + * @param string The error message to keep + * @return 0 on success or -1 on failure + */ +GIT_EXTERN(int) git_error_set_str(int error_class, const char *string); + +/** + * Set the error message to a special value for memory allocation failure. + * + * The normal `git_error_set_str()` function attempts to `strdup()` the + * string that is passed in. This is not a good idea when the error in + * question is a memory allocation failure. That circumstance has a + * special setter function that sets the error string to a known and + * statically allocated internal value. + */ +GIT_EXTERN(void) git_error_set_oom(void); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/filter.h b/include/git2/sys/filter.h new file mode 100644 index 00000000000..60466d173fb --- /dev/null +++ b/include/git2/sys/filter.h @@ -0,0 +1,416 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_filter_h__ +#define INCLUDE_sys_git_filter_h__ + +#include "git2/filter.h" + +/** + * @file git2/sys/filter.h + * @brief Custom filter backends and plugins + * @defgroup git_backend Custom filter backends and plugins + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Look up a filter by name + * + * @param name The name of the filter + * @return Pointer to the filter object or NULL if not found + */ +GIT_EXTERN(git_filter *) git_filter_lookup(const char *name); + +/** The "crlf" filter */ +#define GIT_FILTER_CRLF "crlf" + +/** The "ident" filter */ +#define GIT_FILTER_IDENT "ident" + +/** + * This is priority that the internal CRLF filter will be registered with + */ +#define GIT_FILTER_CRLF_PRIORITY 0 + +/** + * This is priority that the internal ident filter will be registered with + */ +#define GIT_FILTER_IDENT_PRIORITY 100 + +/** + * This is priority to use with a custom filter to imitate a core Git + * filter driver, so that it will be run last on checkout and first on + * checkin. You do not have to use this, but it helps compatibility. + */ +#define GIT_FILTER_DRIVER_PRIORITY 200 + +/** + * Create a new empty filter list + * + * Normally you won't use this because `git_filter_list_load` will create + * the filter list for you, but you can use this in combination with the + * `git_filter_lookup` and `git_filter_list_push` functions to assemble + * your own chains of filters. + * + * @param out the filter list + * @param repo the repository to use for configuration + * @param mode the filter mode (direction) + * @param options the options + * @return 0 on success or an error code + */ +GIT_EXTERN(int) git_filter_list_new( + git_filter_list **out, + git_repository *repo, + git_filter_mode_t mode, + uint32_t options); + +/** + * Add a filter to a filter list with the given payload. + * + * Normally you won't have to do this because the filter list is created + * by calling the "check" function on registered filters when the filter + * attributes are set, but this does allow more direct manipulation of + * filter lists when desired. + * + * Note that normally the "check" function can set up a payload for the + * filter. Using this function, you can either pass in a payload if you + * know the expected payload format, or you can pass NULL. Some filters + * may fail with a NULL payload. Good luck! + * + * @param fl the filter list + * @param filter the filter to push + * @param payload the payload for the filter + * @return 0 on success or an error code + */ +GIT_EXTERN(int) git_filter_list_push( + git_filter_list *fl, git_filter *filter, void *payload); + +/** + * Look up how many filters are in the list + * + * We will attempt to apply all of these filters to any data passed in, + * but note that the filter apply action still has the option of skipping + * data that is passed in (for example, the CRLF filter will skip data + * that appears to be binary). + * + * @param fl A filter list + * @return The number of filters in the list + */ +GIT_EXTERN(size_t) git_filter_list_length(const git_filter_list *fl); + +/** + * A filter source represents a file/blob to be processed + */ +typedef struct git_filter_source git_filter_source; + +/** + * Get the repository that the source data is coming from. + * + * @param src the filter source + * @return the repository for the filter information + */ +GIT_EXTERN(git_repository *) git_filter_source_repo(const git_filter_source *src); + +/** + * Get the path that the source data is coming from. + * + * @param src the filter source + * @return the path that is being filtered + */ +GIT_EXTERN(const char *) git_filter_source_path(const git_filter_source *src); + +/** + * Get the file mode of the source file + * If the mode is unknown, this will return 0 + * + * @param src the filter source + * @return the file mode for the file being filtered + */ +GIT_EXTERN(uint16_t) git_filter_source_filemode(const git_filter_source *src); + +/** + * Get the OID of the source + * If the OID is unknown (often the case with GIT_FILTER_CLEAN) then + * this will return NULL. + * + * @param src the filter source + * @return the object id of the file being filtered + */ +GIT_EXTERN(const git_oid *) git_filter_source_id(const git_filter_source *src); + +/** + * Get the git_filter_mode_t to be used + * + * @param src the filter source + * @return the mode (direction) of the filter + */ +GIT_EXTERN(git_filter_mode_t) git_filter_source_mode(const git_filter_source *src); + +/** + * Get the combination git_filter_flag_t options to be applied + * + * @param src the filter source + * @return the flags of the filter + */ +GIT_EXTERN(uint32_t) git_filter_source_flags(const git_filter_source *src); + +/** + * Initialize callback on filter + * + * Specified as `filter.initialize`, this is an optional callback invoked + * before a filter is first used. It will be called once at most. + * + * If non-NULL, the filter's `initialize` callback will be invoked right + * before the first use of the filter, so you can defer expensive + * initialization operations (in case libgit2 is being used in a way that + * doesn't need the filter). + * + * @param self the filter to initialize + * @return 0 on success, negative number on failure + */ +typedef int GIT_CALLBACK(git_filter_init_fn)(git_filter *self); + +/** + * Shutdown callback on filter + * + * Specified as `filter.shutdown`, this is an optional callback invoked + * when the filter is unregistered or when libgit2 is shutting down. It + * will be called once at most and should release resources as needed. + * This may be called even if the `initialize` callback was not made. + * + * Typically this function will free the `git_filter` object itself. + * + * @param self the filter to shutdown + */ +typedef void GIT_CALLBACK(git_filter_shutdown_fn)(git_filter *self); + +/** + * Callback to decide if a given source needs this filter + * + * Specified as `filter.check`, this is an optional callback that checks + * if filtering is needed for a given source. + * + * It should return 0 if the filter should be applied (i.e. success), + * GIT_PASSTHROUGH if the filter should not be applied, or an error code + * to fail out of the filter processing pipeline and return to the caller. + * + * The `attr_values` will be set to the values of any attributes given in + * the filter definition. See `git_filter` below for more detail. + * + * The `payload` will be a pointer to a reference payload for the filter. + * This will start as NULL, but `check` can assign to this pointer for + * later use by the `stream` callback. Note that the value should be heap + * allocated (not stack), so that it doesn't go away before the `stream` + * callback can use it. If a filter allocates and assigns a value to the + * `payload`, it will need a `cleanup` callback to free the payload. + * + * @param self the filter check + * @param payload a data for future filter functions + * @param src the filter source + * @param attr_values the attribute values + * @return 0 on success or a negative value on error + */ +typedef int GIT_CALLBACK(git_filter_check_fn)( + git_filter *self, + void **payload, /* NULL on entry, may be set */ + const git_filter_source *src, + const char **attr_values); + +#ifndef GIT_DEPRECATE_HARD +/** + * Callback to actually perform the data filtering + * + * Specified as `filter.apply`, this is the callback that actually filters + * data. If it successfully writes the output, it should return 0. Like + * `check`, it can return GIT_PASSTHROUGH to indicate that the filter + * doesn't want to run. Other error codes will stop filter processing and + * return to the caller. + * + * The `payload` value will refer to any payload that was set by the + * `check` callback. It may be read from or written to as needed. + * + * @param self the filter check + * @param payload a data for future filter functions + * @param to the input buffer + * @param from the output buffer + * @param src the filter source + * @return 0 on success or a negative value on error + * @deprecated use git_filter_stream_fn + */ +typedef int GIT_CALLBACK(git_filter_apply_fn)( + git_filter *self, + void **payload, /* may be read and/or set */ + git_buf *to, + const git_buf *from, + const git_filter_source *src); +#endif + +/** + * Callback to perform the data filtering. + * + * Specified as `filter.stream`, this is a callback that filters data + * in a streaming manner. This function will provide a + * `git_writestream` that will the original data will be written to; + * with that data, the `git_writestream` will then perform the filter + * translation and stream the filtered data out to the `next` location. + * + * @param out the write stream + * @param self the filter + * @param payload a data for future filter functions + * @param src the filter source + * @param next the output stream + * @return 0 on success or a negative value on error + */ +typedef int GIT_CALLBACK(git_filter_stream_fn)( + git_writestream **out, + git_filter *self, + void **payload, + const git_filter_source *src, + git_writestream *next); + +/** + * Callback to clean up after filtering has been applied + * + * Specified as `filter.cleanup`, this is an optional callback invoked + * after the filter has been applied. If the `check`, `apply`, or + * `stream` callbacks allocated a `payload` to keep per-source filter + * state, use this callback to free that payload and release resources + * as required. + * + * @param self the filter + * @param payload a data for future filter functions + */ +typedef void GIT_CALLBACK(git_filter_cleanup_fn)( + git_filter *self, + void *payload); + +/** + * Filter structure used to register custom filters. + * + * To associate extra data with a filter, allocate extra data and put the + * `git_filter` struct at the start of your data buffer, then cast the + * `self` pointer to your larger structure when your callback is invoked. + */ +struct git_filter { + /** The `version` field should be set to `GIT_FILTER_VERSION`. */ + unsigned int version; + + /** + * A whitespace-separated list of attribute names to check for this + * filter (e.g. "eol crlf text"). If the attribute name is bare, it + * will be simply loaded and passed to the `check` callback. If it + * has a value (i.e. "name=value"), the attribute must match that + * value for the filter to be applied. The value may be a wildcard + * (eg, "name=*"), in which case the filter will be invoked for any + * value for the given attribute name. See the attribute parameter + * of the `check` callback for the attribute value that was specified. + */ + const char *attributes; + + /** Called when the filter is first used for any file. */ + git_filter_init_fn initialize; + + /** Called when the filter is removed or unregistered from the system. */ + git_filter_shutdown_fn shutdown; + + /** + * Called to determine whether the filter should be invoked for a + * given file. If this function returns `GIT_PASSTHROUGH` then the + * `stream` or `apply` functions will not be invoked and the + * contents will be passed through unmodified. + */ + git_filter_check_fn check; + +#ifdef GIT_DEPRECATE_HARD + void *reserved; +#else + /** + * Provided for backward compatibility; this will apply the + * filter to the given contents in a `git_buf`. Callers should + * provide a `stream` function instead. + */ + git_filter_apply_fn apply; +#endif + + /** + * Called to apply the filter, this function will provide a + * `git_writestream` that will the original data will be + * written to; with that data, the `git_writestream` will then + * perform the filter translation and stream the filtered data + * out to the `next` location. + */ + git_filter_stream_fn stream; + + /** Called when the system is done filtering for a file. */ + git_filter_cleanup_fn cleanup; +}; + +/** Current version for the `git_filter_options` structure */ +#define GIT_FILTER_VERSION 1 + +/** Static constructor for `git_filter_options` */ +#define GIT_FILTER_INIT {GIT_FILTER_VERSION} + +/** + * Initializes a `git_filter` with default values. Equivalent to + * creating an instance with GIT_FILTER_INIT. + * + * @param filter the `git_filter` struct to initialize. + * @param version Version the struct; pass `GIT_FILTER_VERSION` + * @return 0 on success; -1 on failure. + */ +GIT_EXTERN(int) git_filter_init(git_filter *filter, unsigned int version); + +/** + * Register a filter under a given name with a given priority. + * + * As mentioned elsewhere, the initialize callback will not be invoked + * immediately. It is deferred until the filter is used in some way. + * + * A filter's attribute checks and `check` and `stream` (or `apply`) + * callbacks will be issued in order of `priority` on smudge (to + * workdir), and in reverse order of `priority` on clean (to odb). + * + * Two filters are preregistered with libgit2: + * - GIT_FILTER_CRLF with priority 0 + * - GIT_FILTER_IDENT with priority 100 + * + * Currently the filter registry is not thread safe, so any registering or + * deregistering of filters must be done outside of any possible usage of + * the filters (i.e. during application setup or shutdown). + * + * @param name A name by which the filter can be referenced. Attempting + * to register with an in-use name will return GIT_EEXISTS. + * @param filter The filter definition. This pointer will be stored as is + * by libgit2 so it must be a durable allocation (either static + * or on the heap). + * @param priority The priority for filter application + * @return 0 on successful registry, error code <0 on failure + */ +GIT_EXTERN(int) git_filter_register( + const char *name, git_filter *filter, int priority); + +/** + * Remove the filter with the given name + * + * Attempting to remove the builtin libgit2 filters is not permitted and + * will return an error. + * + * Currently the filter registry is not thread safe, so any registering or + * deregistering of filters must be done outside of any possible usage of + * the filters (i.e. during application setup or shutdown). + * + * @param name The name under which the filter was registered + * @return 0 on success, error code <0 on failure + */ +GIT_EXTERN(int) git_filter_unregister(const char *name); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/hashsig.h b/include/git2/sys/hashsig.h new file mode 100644 index 00000000000..0d7be535ce3 --- /dev/null +++ b/include/git2/sys/hashsig.h @@ -0,0 +1,117 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_hashsig_h__ +#define INCLUDE_sys_hashsig_h__ + +#include "git2/common.h" + +/** + * @file git2/sys/hashsig.h + * @brief Signatures for file similarity comparison + * @defgroup git_hashsig Git merge routines + * @ingroup Git + * + * Hash signatures are used for file similary comparison; this is + * used for git's rename handling. + * @{ + */ +GIT_BEGIN_DECL + +/** + * Similarity signature of arbitrary text content based on line hashes + */ +typedef struct git_hashsig git_hashsig; + +/** + * Options for hashsig computation + * + * The options GIT_HASHSIG_NORMAL, GIT_HASHSIG_IGNORE_WHITESPACE, + * GIT_HASHSIG_SMART_WHITESPACE are exclusive and should not be combined. + */ +typedef enum { + /** + * Use all data + */ + GIT_HASHSIG_NORMAL = 0, + + /** + * Ignore whitespace + */ + GIT_HASHSIG_IGNORE_WHITESPACE = (1 << 0), + + /** + * Ignore \r and all space after \n + */ + GIT_HASHSIG_SMART_WHITESPACE = (1 << 1), + + /** + * Allow hashing of small files + */ + GIT_HASHSIG_ALLOW_SMALL_FILES = (1 << 2) +} git_hashsig_option_t; + +/** + * Compute a similarity signature for a text buffer + * + * If you have passed the option GIT_HASHSIG_IGNORE_WHITESPACE, then the + * whitespace will be removed from the buffer while it is being processed, + * modifying the buffer in place. Sorry about that! + * + * @param out The computed similarity signature. + * @param buf The input buffer. + * @param buflen The input buffer size. + * @param opts The signature computation options (see above). + * @return 0 on success, GIT_EBUFS if the buffer doesn't contain enough data to + * compute a valid signature (unless GIT_HASHSIG_ALLOW_SMALL_FILES is set), or + * error code. + */ +GIT_EXTERN(int) git_hashsig_create( + git_hashsig **out, + const char *buf, + size_t buflen, + git_hashsig_option_t opts); + +/** + * Compute a similarity signature for a text file + * + * This walks through the file, only loading a maximum of 4K of file data at + * a time. Otherwise, it acts just like `git_hashsig_create`. + * + * @param out The computed similarity signature. + * @param path The path to the input file. + * @param opts The signature computation options (see above). + * @return 0 on success, GIT_EBUFS if the buffer doesn't contain enough data to + * compute a valid signature (unless GIT_HASHSIG_ALLOW_SMALL_FILES is set), or + * error code. + */ +GIT_EXTERN(int) git_hashsig_create_fromfile( + git_hashsig **out, + const char *path, + git_hashsig_option_t opts); + +/** + * Release memory for a content similarity signature + * + * @param sig The similarity signature to free. + */ +GIT_EXTERN(void) git_hashsig_free(git_hashsig *sig); + +/** + * Measure similarity score between two similarity signatures + * + * @param a The first similarity signature to compare. + * @param b The second similarity signature to compare. + * @return [0 to 100] on success as the similarity score, or error code. + */ +GIT_EXTERN(int) git_hashsig_compare( + const git_hashsig *a, + const git_hashsig *b); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/index.h b/include/git2/sys/index.h new file mode 100644 index 00000000000..b3b86a04598 --- /dev/null +++ b/include/git2/sys/index.h @@ -0,0 +1,183 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_index_h__ +#define INCLUDE_sys_git_index_h__ + +#include "git2/common.h" +#include "git2/types.h" + +/** + * @file git2/sys/index.h + * @brief Low-level index manipulation routines + * @defgroup git_index Low-level index manipulation routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** Representation of a rename conflict entry in the index. */ +typedef struct git_index_name_entry { + char *ancestor; + char *ours; + char *theirs; +} git_index_name_entry; + +/** Representation of a resolve undo entry in the index. */ +typedef struct git_index_reuc_entry { + uint32_t mode[3]; + git_oid oid[3]; + char *path; +} git_index_reuc_entry; + +/** @name Conflict Name entry functions + * + * These functions work on rename conflict entries. + */ +/**@{*/ + +/** + * Get the count of filename conflict entries currently in the index. + * + * @param index an existing index object + * @return integer of count of current filename conflict entries + */ +GIT_EXTERN(size_t) git_index_name_entrycount(git_index *index); + +/** + * Get a filename conflict entry from the index. + * + * The returned entry is read-only and should not be modified + * or freed by the caller. + * + * @param index an existing index object + * @param n the position of the entry + * @return a pointer to the filename conflict entry; NULL if out of bounds + */ +GIT_EXTERN(const git_index_name_entry *) git_index_name_get_byindex( + git_index *index, size_t n); + +/** + * Record the filenames involved in a rename conflict. + * + * @param index an existing index object + * @param ancestor the path of the file as it existed in the ancestor + * @param ours the path of the file as it existed in our tree + * @param theirs the path of the file as it existed in their tree + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_index_name_add(git_index *index, + const char *ancestor, const char *ours, const char *theirs); + +/** + * Remove all filename conflict entries. + * + * @param index an existing index object + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_name_clear(git_index *index); + +/**@}*/ + +/** @name Resolve Undo (REUC) index entry manipulation. + * + * These functions work on the Resolve Undo index extension and contains + * data about the original files that led to a merge conflict. + */ +/**@{*/ + +/** + * Get the count of resolve undo entries currently in the index. + * + * @param index an existing index object + * @return integer of count of current resolve undo entries + */ +GIT_EXTERN(size_t) git_index_reuc_entrycount(git_index *index); + +/** + * Finds the resolve undo entry that points to the given path in the Git + * index. + * + * @param at_pos the address to which the position of the reuc entry is written (optional) + * @param index an existing index object + * @param path path to search + * @return 0 if found, < 0 otherwise (GIT_ENOTFOUND) + */ +GIT_EXTERN(int) git_index_reuc_find(size_t *at_pos, git_index *index, const char *path); + +/** + * Get a resolve undo entry from the index. + * + * The returned entry is read-only and should not be modified + * or freed by the caller. + * + * @param index an existing index object + * @param path path to search + * @return the resolve undo entry; NULL if not found + */ +GIT_EXTERN(const git_index_reuc_entry *) git_index_reuc_get_bypath(git_index *index, const char *path); + +/** + * Get a resolve undo entry from the index. + * + * The returned entry is read-only and should not be modified + * or freed by the caller. + * + * @param index an existing index object + * @param n the position of the entry + * @return a pointer to the resolve undo entry; NULL if out of bounds + */ +GIT_EXTERN(const git_index_reuc_entry *) git_index_reuc_get_byindex(git_index *index, size_t n); + +/** + * Adds a resolve undo entry for a file based on the given parameters. + * + * The resolve undo entry contains the OIDs of files that were involved + * in a merge conflict after the conflict has been resolved. This allows + * conflicts to be re-resolved later. + * + * If there exists a resolve undo entry for the given path in the index, + * it will be removed. + * + * This method will fail in bare index instances. + * + * @param index an existing index object + * @param path filename to add + * @param ancestor_mode mode of the ancestor file + * @param ancestor_id oid of the ancestor file + * @param our_mode mode of our file + * @param our_id oid of our file + * @param their_mode mode of their file + * @param their_id oid of their file + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_reuc_add(git_index *index, const char *path, + int ancestor_mode, const git_oid *ancestor_id, + int our_mode, const git_oid *our_id, + int their_mode, const git_oid *their_id); + +/** + * Remove an resolve undo entry from the index + * + * @param index an existing index object + * @param n position of the resolve undo entry to remove + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_reuc_remove(git_index *index, size_t n); + +/** + * Remove all resolve undo entries from the index + * + * @param index an existing index object + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_reuc_clear(git_index *index); + +/**@}*/ + +/** @} */ +GIT_END_DECL +#endif diff --git a/include/git2/sys/mempack.h b/include/git2/sys/mempack.h new file mode 100644 index 00000000000..be902be254f --- /dev/null +++ b/include/git2/sys/mempack.h @@ -0,0 +1,117 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_odb_mempack_h__ +#define INCLUDE_sys_git_odb_mempack_h__ + +#include "git2/common.h" +#include "git2/types.h" +#include "git2/oid.h" +#include "git2/odb.h" +#include "git2/buffer.h" + +/** + * @file git2/sys/mempack.h + * @brief A custom object database backend for storing objects in-memory + * @defgroup git_mempack A custom object database backend for storing objects in-memory + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Instantiate a new mempack backend. + * + * The backend must be added to an existing ODB with the highest + * priority. + * + * git_mempack_new(&mempacker); + * git_repository_odb(&odb, repository); + * git_odb_add_backend(odb, mempacker, 999); + * + * Once the backend has been loaded, all writes to the ODB will + * instead be queued in memory, and can be finalized with + * `git_mempack_dump`. + * + * Subsequent reads will also be served from the in-memory store + * to ensure consistency, until the memory store is dumped. + * + * @param out Pointer where to store the ODB backend + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_mempack_new(git_odb_backend **out); + +/** + * Write a thin packfile with the objects in the memory store. + * + * A thin packfile is a packfile that does not contain its transitive closure of + * references. This is useful for efficiently distributing additions to a + * repository over the network, but also finds use in the efficient bulk + * addition of objects to a repository, locally. + * + * This operation performs the (shallow) insert operations into the + * `git_packbuilder`, but does not write the packfile to disk; + * see `git_packbuilder_write_buf`. + * + * It also does not reset the in-memory object database; see `git_mempack_reset`. + * + * @param backend The mempack backend + * @param pb The packbuilder to use to write the packfile + * @return 0 on success or an error code + */ +GIT_EXTERN(int) git_mempack_write_thin_pack(git_odb_backend *backend, git_packbuilder *pb); + +/** + * Dump all the queued in-memory writes to a packfile. + * + * The contents of the packfile will be stored in the given buffer. + * It is the caller's responsibility to ensure that the generated + * packfile is available to the repository (e.g. by writing it + * to disk, or doing something crazy like distributing it across + * several copies of the repository over a network). + * + * Once the generated packfile is available to the repository, + * call `git_mempack_reset` to cleanup the memory store. + * + * Calling `git_mempack_reset` before the packfile has been + * written to disk will result in an inconsistent repository + * (the objects in the memory store won't be accessible). + * + * @param pack Buffer where to store the raw packfile + * @param repo The active repository where the backend is loaded + * @param backend The mempack backend + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_mempack_dump(git_buf *pack, git_repository *repo, git_odb_backend *backend); + +/** + * Reset the memory packer by clearing all the queued objects. + * + * This assumes that `git_mempack_dump` has been called before to + * store all the queued objects into a single packfile. + * + * Alternatively, call `reset` without a previous dump to "undo" + * all the recently written objects, giving transaction-like + * semantics to the Git repository. + * + * @param backend The mempack backend + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_mempack_reset(git_odb_backend *backend); + +/** + * Get the total number of objects in mempack + * + * @param count The count of objects in the mempack + * @param backend The mempack backend + * @return 0 on success, or -1 on error + */ +GIT_EXTERN(int) git_mempack_object_count(size_t *count, git_odb_backend *backend); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/merge.h b/include/git2/sys/merge.h new file mode 100644 index 00000000000..a9f522054ba --- /dev/null +++ b/include/git2/sys/merge.h @@ -0,0 +1,230 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_merge_h__ +#define INCLUDE_sys_git_merge_h__ + +#include "git2/common.h" +#include "git2/types.h" +#include "git2/index.h" +#include "git2/merge.h" + +/** + * @file git2/sys/merge.h + * @brief Custom merge drivers + * @defgroup git_merge Custom merge drivers + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * A "merge driver" is a mechanism that can be configured to handle + * conflict resolution for files changed in both the "ours" and "theirs" + * side of a merge. + */ +typedef struct git_merge_driver git_merge_driver; + +/** + * Look up a merge driver by name + * + * @param name The name of the merge driver + * @return Pointer to the merge driver object or NULL if not found + */ +GIT_EXTERN(git_merge_driver *) git_merge_driver_lookup(const char *name); + +/** The "text" merge driver */ +#define GIT_MERGE_DRIVER_TEXT "text" +/** The "binary" merge driver */ +#define GIT_MERGE_DRIVER_BINARY "binary" +/** The "union" merge driver */ +#define GIT_MERGE_DRIVER_UNION "union" + +/** + * A merge driver source represents the file to be merged + */ +typedef struct git_merge_driver_source git_merge_driver_source; + +/** + * Get the repository that the source data is coming from. + * + * @param src the merge driver source + * @return the repository + */ +GIT_EXTERN(git_repository *) git_merge_driver_source_repo( + const git_merge_driver_source *src); + +/** + * Gets the ancestor of the file to merge. + * + * @param src the merge driver source + * @return the ancestor or NULL if there was no ancestor + */ +GIT_EXTERN(const git_index_entry *) git_merge_driver_source_ancestor( + const git_merge_driver_source *src); + +/** + * Gets the ours side of the file to merge. + * + * @param src the merge driver source + * @return the ours side or NULL if there was no ours side + */ +GIT_EXTERN(const git_index_entry *) git_merge_driver_source_ours( + const git_merge_driver_source *src); + +/** + * Gets the theirs side of the file to merge. + * + * @param src the merge driver source + * @return the theirs side or NULL if there was no theirs side + */ +GIT_EXTERN(const git_index_entry *) git_merge_driver_source_theirs( + const git_merge_driver_source *src); + +/** + * Gets the merge file options that the merge was invoked with. + * + * @param src the merge driver source + * @return the options + */ +GIT_EXTERN(const git_merge_file_options *) git_merge_driver_source_file_options( + const git_merge_driver_source *src); + + +/** + * Initialize callback on merge driver + * + * Specified as `driver.initialize`, this is an optional callback invoked + * before a merge driver is first used. It will be called once at most + * per library lifetime. + * + * If non-NULL, the merge driver's `initialize` callback will be invoked + * right before the first use of the driver, so you can defer expensive + * initialization operations (in case libgit2 is being used in a way that + * doesn't need the merge driver). + * + * @param self the merge driver to initialize + * @return 0 on success, or a negative number on failure + */ +typedef int GIT_CALLBACK(git_merge_driver_init_fn)(git_merge_driver *self); + +/** + * Shutdown callback on merge driver + * + * Specified as `driver.shutdown`, this is an optional callback invoked + * when the merge driver is unregistered or when libgit2 is shutting down. + * It will be called once at most and should release resources as needed. + * This may be called even if the `initialize` callback was not made. + * + * Typically this function will free the `git_merge_driver` object itself. + * + * @param self the merge driver to shutdown + */ +typedef void GIT_CALLBACK(git_merge_driver_shutdown_fn)(git_merge_driver *self); + +/** + * Callback to perform the merge. + * + * Specified as `driver.apply`, this is the callback that actually does the + * merge. If it can successfully perform a merge, it should populate + * `path_out` with a pointer to the filename to accept, `mode_out` with + * the resultant mode, and `merged_out` with the buffer of the merged file + * and then return 0. If the driver returns `GIT_PASSTHROUGH`, then the + * default merge driver should instead be run. It can also return + * `GIT_EMERGECONFLICT` if the driver is not able to produce a merge result, + * and the file will remain conflicted. Any other errors will fail and + * return to the caller. + * + * The `filter_name` contains the name of the filter that was invoked, as + * specified by the file's attributes. + * + * The `src` contains the data about the file to be merged. + * + * @param self the merge driver + * @param path_out the resolved path + * @param mode_out the resolved mode + * @param merged_out the merged output contents + * @param filter_name the filter that was invoked + * @param src the data about the unmerged file + * @return 0 on success, or an error code + */ +typedef int GIT_CALLBACK(git_merge_driver_apply_fn)( + git_merge_driver *self, + const char **path_out, + uint32_t *mode_out, + git_buf *merged_out, + const char *filter_name, + const git_merge_driver_source *src); + +/** + * Merge driver structure used to register custom merge drivers. + * + * To associate extra data with a driver, allocate extra data and put the + * `git_merge_driver` struct at the start of your data buffer, then cast + * the `self` pointer to your larger structure when your callback is invoked. + */ +struct git_merge_driver { + /** The `version` should be set to `GIT_MERGE_DRIVER_VERSION`. */ + unsigned int version; + + /** Called when the merge driver is first used for any file. */ + git_merge_driver_init_fn initialize; + + /** Called when the merge driver is unregistered from the system. */ + git_merge_driver_shutdown_fn shutdown; + + /** + * Called to merge the contents of a conflict. If this function + * returns `GIT_PASSTHROUGH` then the default (`text`) merge driver + * will instead be invoked. If this function returns + * `GIT_EMERGECONFLICT` then the file will remain conflicted. + */ + git_merge_driver_apply_fn apply; +}; + +/** The version for the `git_merge_driver` */ +#define GIT_MERGE_DRIVER_VERSION 1 + +/** + * Register a merge driver under a given name. + * + * As mentioned elsewhere, the initialize callback will not be invoked + * immediately. It is deferred until the driver is used in some way. + * + * Currently the merge driver registry is not thread safe, so any + * registering or deregistering of merge drivers must be done outside of + * any possible usage of the drivers (i.e. during application setup or + * shutdown). + * + * @param name The name of this driver to match an attribute. Attempting + * to register with an in-use name will return GIT_EEXISTS. + * @param driver The merge driver definition. This pointer will be stored + * as is by libgit2 so it must be a durable allocation (either + * static or on the heap). + * @return 0 on successful registry, error code <0 on failure + */ +GIT_EXTERN(int) git_merge_driver_register( + const char *name, git_merge_driver *driver); + +/** + * Remove the merge driver with the given name. + * + * Attempting to remove the builtin libgit2 merge drivers is not permitted + * and will return an error. + * + * Currently the merge driver registry is not thread safe, so any + * registering or deregistering of drivers must be done outside of any + * possible usage of the drivers (i.e. during application setup or shutdown). + * + * @param name The name under which the merge driver was registered + * @return 0 on success, error code <0 on failure + */ +GIT_EXTERN(int) git_merge_driver_unregister(const char *name); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/midx.h b/include/git2/sys/midx.h new file mode 100644 index 00000000000..b3a68afbfc5 --- /dev/null +++ b/include/git2/sys/midx.h @@ -0,0 +1,117 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_midx_h__ +#define INCLUDE_sys_git_midx_h__ + +#include "git2/common.h" +#include "git2/types.h" + +/** + * @file git2/sys/midx.h + * @brief Incremental multi-pack indexes + * @defgroup git_midx Incremental multi-pack indexes + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Options structure for `git_midx_writer_options`. + * + * Initialize with `GIT_MIDX_WRITER_OPTIONS_INIT`. Alternatively, + * you can use `git_midx_writer_options_init`. + */ +typedef struct { + unsigned int version; + +#ifdef GIT_EXPERIMENTAL_SHA256 + /** The object ID type that this commit graph contains. */ + git_oid_t oid_type; +#endif +} git_midx_writer_options; + +/** Current version for the `git_midx_writer_options` structure */ +#define GIT_MIDX_WRITER_OPTIONS_VERSION 1 + +/** Static constructor for `git_midx_writer_options` */ +#define GIT_MIDX_WRITER_OPTIONS_INIT { \ + GIT_MIDX_WRITER_OPTIONS_VERSION \ + } + +/** + * Initialize git_midx_writer_options structure + * + * Initializes a `git_midx_writer_options` with default values. + * Equivalent to creating an instance with + * `GIT_MIDX_WRITER_OPTIONS_INIT`. + * + * @param opts The `git_midx_writer_options` struct to initialize. + * @param version The struct version; pass `GIT_MIDX_WRITER_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_midx_writer_options_init( + git_midx_writer_options *opts, + unsigned int version); + +/** + * Create a new writer for `multi-pack-index` files. + * + * @param out location to store the writer pointer. + * @param pack_dir the directory where the `.pack` and `.idx` files are. The + * `multi-pack-index` file will be written in this directory, too. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_midx_writer_new( + git_midx_writer **out, + const char *pack_dir +#ifdef GIT_EXPERIMENTAL_SHA256 + , git_midx_writer_options *options +#endif + ); + +/** + * Free the multi-pack-index writer and its resources. + * + * @param w the writer to free. If NULL no action is taken. + */ +GIT_EXTERN(void) git_midx_writer_free(git_midx_writer *w); + +/** + * Add an `.idx` file to the writer. + * + * @param w the writer + * @param idx_path the path of an `.idx` file. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_midx_writer_add( + git_midx_writer *w, + const char *idx_path); + +/** + * Write a `multi-pack-index` file to a file. + * + * @param w the writer + * @return 0 or an error code + */ +GIT_EXTERN(int) git_midx_writer_commit( + git_midx_writer *w); + +/** + * Dump the contents of the `multi-pack-index` to an in-memory buffer. + * + * @param midx Buffer where to store the contents of the `multi-pack-index`. + * @param w the writer + * @return 0 or an error code + */ +GIT_EXTERN(int) git_midx_writer_dump( + git_buf *midx, + git_midx_writer *w); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/odb_backend.h b/include/git2/sys/odb_backend.h new file mode 100644 index 00000000000..53d8d060eac --- /dev/null +++ b/include/git2/sys/odb_backend.h @@ -0,0 +1,176 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_odb_backend_h__ +#define INCLUDE_sys_git_odb_backend_h__ + +#include "git2/common.h" +#include "git2/types.h" +#include "git2/oid.h" +#include "git2/odb.h" + +/** + * @file git2/sys/odb_backend.h + * @brief Object database backends for custom object storage + * @defgroup git_backend Object database backends for custom object storage + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * An instance for a custom backend + */ +struct git_odb_backend { + unsigned int version; + git_odb *odb; + + /* read and read_prefix each return to libgit2 a buffer which + * will be freed later. The buffer should be allocated using + * the function git_odb_backend_data_alloc to ensure that libgit2 + * can safely free it later. */ + int GIT_CALLBACK(read)( + void **, size_t *, git_object_t *, git_odb_backend *, const git_oid *); + + /* To find a unique object given a prefix of its oid. The oid given + * must be so that the remaining (GIT_OID_SHA1_HEXSIZE - len)*4 bits are 0s. + */ + int GIT_CALLBACK(read_prefix)( + git_oid *, void **, size_t *, git_object_t *, + git_odb_backend *, const git_oid *, size_t); + + int GIT_CALLBACK(read_header)( + size_t *, git_object_t *, git_odb_backend *, const git_oid *); + + /** + * Write an object into the backend. The id of the object has + * already been calculated and is passed in. + */ + int GIT_CALLBACK(write)( + git_odb_backend *, const git_oid *, const void *, size_t, git_object_t); + + int GIT_CALLBACK(writestream)( + git_odb_stream **, git_odb_backend *, git_object_size_t, git_object_t); + + int GIT_CALLBACK(readstream)( + git_odb_stream **, size_t *, git_object_t *, + git_odb_backend *, const git_oid *); + + int GIT_CALLBACK(exists)( + git_odb_backend *, const git_oid *); + + int GIT_CALLBACK(exists_prefix)( + git_oid *, git_odb_backend *, const git_oid *, size_t); + + /** + * If the backend implements a refreshing mechanism, it should be exposed + * through this endpoint. Each call to `git_odb_refresh()` will invoke it. + * + * The odb layer will automatically call this when needed on failed + * lookups (ie. `exists()`, `read()`, `read_header()`). + */ + int GIT_CALLBACK(refresh)(git_odb_backend *); + + int GIT_CALLBACK(foreach)( + git_odb_backend *, git_odb_foreach_cb cb, void *payload); + + int GIT_CALLBACK(writepack)( + git_odb_writepack **, git_odb_backend *, git_odb *odb, + git_indexer_progress_cb progress_cb, void *progress_payload); + + /** + * If the backend supports pack files, this will create a + * `multi-pack-index` file which will contain an index of all objects + * across all the `.pack` files. + */ + int GIT_CALLBACK(writemidx)(git_odb_backend *); + + /** + * "Freshens" an already existing object, updating its last-used + * time. This occurs when `git_odb_write` was called, but the + * object already existed (and will not be re-written). The + * underlying implementation may want to update last-used timestamps. + * + * If callers implement this, they should return `0` if the object + * exists and was freshened, and non-zero otherwise. + */ + int GIT_CALLBACK(freshen)(git_odb_backend *, const git_oid *); + + /** + * Frees any resources held by the odb (including the `git_odb_backend` + * itself). An odb backend implementation must provide this function. + */ + void GIT_CALLBACK(free)(git_odb_backend *); +}; + +/** Current version for the `git_odb_backend_options` structure */ +#define GIT_ODB_BACKEND_VERSION 1 + +/** Static constructor for `git_odb_backend_options` */ +#define GIT_ODB_BACKEND_INIT {GIT_ODB_BACKEND_VERSION} + +/** + * Initializes a `git_odb_backend` with default values. Equivalent to + * creating an instance with GIT_ODB_BACKEND_INIT. + * + * @param backend the `git_odb_backend` struct to initialize. + * @param version Version the struct; pass `GIT_ODB_BACKEND_VERSION` + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_odb_init_backend( + git_odb_backend *backend, + unsigned int version); + +/** + * Allocate data for an ODB object. Custom ODB backends may use this + * to provide data back to the ODB from their read function. This + * memory should not be freed once it is returned to libgit2. If a + * custom ODB uses this function but encounters an error and does not + * return this data to libgit2, then they should use the corresponding + * git_odb_backend_data_free function. + * + * @param backend the ODB backend that is allocating this memory + * @param len the number of bytes to allocate + * @return the allocated buffer on success or NULL if out of memory + */ +GIT_EXTERN(void *) git_odb_backend_data_alloc(git_odb_backend *backend, size_t len); + +/** + * Frees custom allocated ODB data. This should only be called when + * memory allocated using git_odb_backend_data_alloc is not returned + * to libgit2 because the backend encountered an error in the read + * function after allocation and did not return this data to libgit2. + * + * @param backend the ODB backend that is freeing this memory + * @param data the buffer to free + */ +GIT_EXTERN(void) git_odb_backend_data_free(git_odb_backend *backend, void *data); + + +/* + * Users can avoid deprecated functions by defining `GIT_DEPRECATE_HARD`. + */ +#ifndef GIT_DEPRECATE_HARD + +/** + * Allocate memory for an ODB object from a custom backend. This is + * an alias of `git_odb_backend_data_alloc` and is preserved for + * backward compatibility. + * + * This function is deprecated, but there is no plan to remove this + * function at this time. + * + * @deprecated git_odb_backend_data_alloc + * @see git_odb_backend_data_alloc + */ +GIT_EXTERN(void *) git_odb_backend_malloc(git_odb_backend *backend, size_t len); + +#endif + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/openssl.h b/include/git2/sys/openssl.h new file mode 100644 index 00000000000..8b74a98cd3d --- /dev/null +++ b/include/git2/sys/openssl.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_openssl_h__ +#define INCLUDE_git_openssl_h__ + +#include "git2/common.h" + +/** + * @file git2/sys/openssl.h + * @brief Custom OpenSSL functionality + * @defgroup git_openssl Custom OpenSSL functionality + * @{ + */ +GIT_BEGIN_DECL + +/** + * Initialize the OpenSSL locks + * + * OpenSSL requires the application to determine how it performs + * locking. + * + * This is a last-resort convenience function which libgit2 provides for + * allocating and initializing the locks as well as setting the + * locking function to use the system's native locking functions. + * + * The locking function will be cleared and the memory will be freed + * when you call git_threads_sutdown(). + * + * If your programming language has an OpenSSL package/bindings, it + * likely sets up locking. You should very strongly prefer that over + * this function. + * + * @return 0 on success, -1 if there are errors or if libgit2 was not + * built with OpenSSL and threading support. + */ +GIT_EXTERN(int) git_openssl_set_locking(void); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/path.h b/include/git2/sys/path.h new file mode 100644 index 00000000000..2963bca3f7f --- /dev/null +++ b/include/git2/sys/path.h @@ -0,0 +1,75 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_sys_git_path_h__ +#define INCLUDE_sys_git_path_h__ + +#include "git2/common.h" + +/** + * @file git2/sys/path.h + * @brief Custom path handling + * @defgroup git_path Custom path handling + * @ingroup Git + * + * Merge will take two commits and attempt to produce a commit that + * includes the changes that were made in both branches. + * @{ + */ +GIT_BEGIN_DECL + +/** + * The kinds of git-specific files we know about. + * + * The order needs to stay the same to not break the `gitfiles` + * array in path.c + */ +typedef enum { + /** Check for the .gitignore file */ + GIT_PATH_GITFILE_GITIGNORE, + /** Check for the .gitmodules file */ + GIT_PATH_GITFILE_GITMODULES, + /** Check for the .gitattributes file */ + GIT_PATH_GITFILE_GITATTRIBUTES +} git_path_gitfile; + +/** + * The kinds of checks to perform according to which filesystem we are trying to + * protect. + */ +typedef enum { + /** Do both NTFS- and HFS-specific checks */ + GIT_PATH_FS_GENERIC, + /** Do NTFS-specific checks only */ + GIT_PATH_FS_NTFS, + /** Do HFS-specific checks only */ + GIT_PATH_FS_HFS +} git_path_fs; + +/** + * Check whether a path component corresponds to a .git$SUFFIX + * file. + * + * As some filesystems do special things to filenames when + * writing files to disk, you cannot always do a plain string + * comparison to verify whether a file name matches an expected + * path or not. This function can do the comparison for you, + * depending on the filesystem you're on. + * + * @param path the path component to check + * @param pathlen the length of `path` that is to be checked + * @param gitfile which file to check against + * @param fs which filesystem-specific checks to use + * @return 0 in case the file does not match, a positive value if + * it does; -1 in case of an error + */ +GIT_EXTERN(int) git_path_is_gitfile(const char *path, size_t pathlen, git_path_gitfile gitfile, git_path_fs fs); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/refdb_backend.h b/include/git2/sys/refdb_backend.h new file mode 100644 index 00000000000..386c6ca2685 --- /dev/null +++ b/include/git2/sys/refdb_backend.h @@ -0,0 +1,403 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_refdb_backend_h__ +#define INCLUDE_sys_git_refdb_backend_h__ + +#include "git2/common.h" +#include "git2/types.h" +#include "git2/oid.h" + +/** + * @file git2/sys/refdb_backend.h + * @brief Custom reference database backends for refs storage + * @defgroup git_refdb_backend Custom reference database backends for refs storage + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + + +/** + * Every backend's iterator must have a pointer to itself as the first + * element, so the API can talk to it. You'd define your iterator as + * + * struct my_iterator { + * git_reference_iterator parent; + * ... + * } + * + * and assign `iter->parent.backend` to your `git_refdb_backend`. + */ +struct git_reference_iterator { + git_refdb *db; + + /** + * Return the current reference and advance the iterator. + */ + int GIT_CALLBACK(next)( + git_reference **ref, + git_reference_iterator *iter); + + /** + * Return the name of the current reference and advance the iterator + */ + int GIT_CALLBACK(next_name)( + const char **ref_name, + git_reference_iterator *iter); + + /** + * Free the iterator + */ + void GIT_CALLBACK(free)( + git_reference_iterator *iter); +}; + +typedef enum { + /** + * The refdb that is to be initialized is for a worktree. + */ + GIT_REFDB_BACKEND_INIT_IS_WORKTREE = (1u << 0), + + /* + * Force-overwrite HEAD in case the refdb is already (partially) + * initialized. + */ + GIT_REFDB_BACKEND_INIT_FORCE_HEAD = (1u << 1) +} git_refdb_backend_init_flag_t; + +/** An instance for a custom backend */ +struct git_refdb_backend { + unsigned int version; /**< The backend API version */ + + /** + * Create a new refdb and initialize its structures. + * + * A refdb implementation may provide this function; if it is not + * provided, no data structures will be initialized for the refdb when + * a new repository is created. + * + * @param path The path of the Git directory that shall be initialized. + * @param initial_head The target that HEAD should point to. This value + * should only be applied when there isn't yet a HEAD + * reference, or when `GIT_REFDB_BACKEND_INIT_FORCE_HEAD` + * is passed. Optional, if unset the backend should not set + * up HEAD, either. + * @param mode The mode that shall be used to create files and + * directories. May be one of `git_repository_init_mode_t` + * or normal Unix permission bits. + * @param flags A combination of `git_refdb_backend_init_flag_t` flags. + * @return `0` on success, a negative error value code. + */ + int GIT_CALLBACK(init)( + git_refdb_backend *backend, + const char *head_target, + mode_t mode, + uint32_t flags); + + /** + * Queries the refdb backend for the existence of a reference. + * + * A refdb implementation must provide this function. + * + * @param exists The implementation shall set this to `0` if a ref does + * not exist, otherwise to `1`. + * @param ref_name The reference's name that should be checked for + * existence. + * @return `0` on success, a negative error value code. + */ + int GIT_CALLBACK(exists)( + int *exists, + git_refdb_backend *backend, + const char *ref_name); + + /** + * Queries the refdb backend for a given reference. + * + * A refdb implementation must provide this function. + * + * @param out The implementation shall set this to the allocated + * reference, if it could be found, otherwise to `NULL`. + * @param ref_name The reference's name that should be checked for + * existence. + * @return `0` on success, `GIT_ENOTFOUND` if the reference does + * exist, otherwise a negative error code. + */ + int GIT_CALLBACK(lookup)( + git_reference **out, + git_refdb_backend *backend, + const char *ref_name); + + /** + * Allocate an iterator object for the backend. + * + * A refdb implementation must provide this function. + * + * @param out The implementation shall set this to the allocated + * reference iterator. A custom structure may be used with an + * embedded `git_reference_iterator` structure. Both `next` + * and `next_name` functions of `git_reference_iterator` need + * to be populated. + * @param glob A pattern to filter references by. If given, the iterator + * shall only return references that match the glob when + * passed to `wildmatch`. + * @return `0` on success, otherwise a negative error code. + */ + int GIT_CALLBACK(iterator)( + git_reference_iterator **iter, + struct git_refdb_backend *backend, + const char *glob); + + /** + * Writes the given reference to the refdb. + * + * A refdb implementation must provide this function. + * + * @param ref The reference to persist. May either be a symbolic or + * direct reference. + * @param force Whether to write the reference if a reference with the + * same name already exists. + * @param who The person updating the reference. Shall be used to create + * a reflog entry. + * @param message The message detailing what kind of reference update is + * performed. Shall be used to create a reflog entry. + * @param old If not `NULL` and `force` is not set, then the + * implementation needs to ensure that the reference is currently at + * the given OID before writing the new value. If both `old` + * and `old_target` are `NULL`, then the reference should not + * exist at the point of writing. + * @param old_target If not `NULL` and `force` is not set, then the + * implementation needs to ensure that the symbolic + * reference is currently at the given target before + * writing the new value. If both `old` and + * `old_target` are `NULL`, then the reference should + * not exist at the point of writing. + * @return `0` on success, otherwise a negative error code. + */ + int GIT_CALLBACK(write)(git_refdb_backend *backend, + const git_reference *ref, int force, + const git_signature *who, const char *message, + const git_oid *old, const char *old_target); + + /** + * Rename a reference in the refdb. + * + * A refdb implementation must provide this function. + * + * @param out The implementation shall set this to the newly created + * reference or `NULL` on error. + * @param old_name The current name of the reference that is to be renamed. + * @param new_name The new name that the old reference shall be renamed to. + * @param force Whether to write the reference if a reference with the + * target name already exists. + * @param who The person updating the reference. Shall be used to create + * a reflog entry. + * @param message The message detailing what kind of reference update is + * performed. Shall be used to create a reflog entry. + * @return `0` on success, otherwise a negative error code. + */ + int GIT_CALLBACK(rename)( + git_reference **out, git_refdb_backend *backend, + const char *old_name, const char *new_name, int force, + const git_signature *who, const char *message); + + /** + * Deletes the given reference from the refdb. + * + * If it exists, its reflog should be deleted as well. + * + * A refdb implementation must provide this function. + * + * @param ref_name The name of the reference name that shall be deleted. + * @param old_id If not `NULL` and `force` is not set, then the + * implementation needs to ensure that the reference is currently at + * the given OID before writing the new value. + * @param old_target If not `NULL` and `force` is not set, then the + * implementation needs to ensure that the symbolic + * reference is currently at the given target before + * writing the new value. + * @return `0` on success, otherwise a negative error code. + */ + int GIT_CALLBACK(del)(git_refdb_backend *backend, const char *ref_name, const git_oid *old_id, const char *old_target); + + /** + * Suggests that the given refdb compress or optimize its references. + * + * This mechanism is implementation specific. For on-disk reference + * databases, this may pack all loose references. + * + * A refdb implementation may provide this function; if it is not + * provided, nothing will be done. + * + * @return `0` on success a negative error code otherwise + */ + int GIT_CALLBACK(compress)(git_refdb_backend *backend); + + /** + * Query whether a particular reference has a log (may be empty) + * + * Shall return 1 if it has a reflog, 0 it it doesn't and negative in + * case an error occurred. + * + * A refdb implementation must provide this function. + * + * @return `0` on success, `1` if the reflog for the given reference + * exists, a negative error code otherwise + */ + int GIT_CALLBACK(has_log)(git_refdb_backend *backend, const char *refname); + + /** + * Make sure a particular reference will have a reflog which + * will be appended to on writes. + * + * A refdb implementation must provide this function. + * + * @return `0` on success, a negative error code otherwise + */ + int GIT_CALLBACK(ensure_log)(git_refdb_backend *backend, const char *refname); + + /** + * Frees any resources held by the refdb (including the `git_refdb_backend` + * itself). + * + * A refdb backend implementation must provide this function. + */ + void GIT_CALLBACK(free)(git_refdb_backend *backend); + + /** + * Read the reflog for the given reference name. + * + * A refdb implementation must provide this function. + * + * @return `0` on success, a negative error code otherwise + */ + int GIT_CALLBACK(reflog_read)(git_reflog **out, git_refdb_backend *backend, const char *name); + + /** + * Write a reflog to disk. + * + * A refdb implementation must provide this function. + * + * @param reflog The complete reference log for a given reference. Note + * that this may contain entries that have already been + * written to disk. + * @return `0` on success, a negative error code otherwise + */ + int GIT_CALLBACK(reflog_write)(git_refdb_backend *backend, git_reflog *reflog); + + /** + * Rename a reflog. + * + * A refdb implementation must provide this function. + * + * @param old_name The name of old reference whose reflog shall be renamed from. + * @param new_name The name of new reference whose reflog shall be renamed to. + * @return `0` on success, a negative error code otherwise + */ + int GIT_CALLBACK(reflog_rename)(git_refdb_backend *_backend, const char *old_name, const char *new_name); + + /** + * Remove a reflog. + * + * A refdb implementation must provide this function. + * + * @param name The name of the reference whose reflog shall be deleted. + * @return `0` on success, a negative error code otherwise + */ + int GIT_CALLBACK(reflog_delete)(git_refdb_backend *backend, const char *name); + + /** + * Lock a reference. + * + * A refdb implementation may provide this function; if it is not + * provided, the transaction API will fail to work. + * + * @param payload_out Opaque parameter that will be passed verbosely to + * `unlock`. + * @param refname Reference that shall be locked. + * @return `0` on success, a negative error code otherwise + */ + int GIT_CALLBACK(lock)(void **payload_out, git_refdb_backend *backend, const char *refname); + + /** + * Unlock a reference. + * + * Only one of target or symbolic_target will be set. + * `success` will be true if the reference should be update, false if + * the lock must be discarded. + * + * A refdb implementation must provide this function if a `lock` + * implementation is provided. + * + * @param payload The payload returned by `lock`. + * @param success `1` if a reference should be updated, `2` if + * a reference should be deleted, `0` if the lock must be + * discarded. + * @param update_reflog `1` in case the reflog should be updated, `0` + * otherwise. + * @param ref The reference which should be unlocked. + * @param who The person updating the reference. Shall be used to create + * a reflog entry in case `update_reflog` is set. + * @param message The message detailing what kind of reference update is + * performed. Shall be used to create a reflog entry in + * case `update_reflog` is set. + * @return `0` on success, a negative error code otherwise + */ + int GIT_CALLBACK(unlock)(git_refdb_backend *backend, void *payload, int success, int update_reflog, + const git_reference *ref, const git_signature *sig, const char *message); +}; + +/** Current version for the `git_refdb_backend_options` structure */ +#define GIT_REFDB_BACKEND_VERSION 1 + +/** Static constructor for `git_refdb_backend_options` */ +#define GIT_REFDB_BACKEND_INIT {GIT_REFDB_BACKEND_VERSION} + +/** + * Initializes a `git_refdb_backend` with default values. Equivalent to + * creating an instance with GIT_REFDB_BACKEND_INIT. + * + * @param backend the `git_refdb_backend` struct to initialize + * @param version Version of struct; pass `GIT_REFDB_BACKEND_VERSION` + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_refdb_init_backend( + git_refdb_backend *backend, + unsigned int version); + +/** + * Constructors for default filesystem-based refdb backend + * + * Under normal usage, this is called for you when the repository is + * opened / created, but you can use this to explicitly construct a + * filesystem refdb backend for a repository. + * + * @param backend_out Output pointer to the git_refdb_backend object + * @param repo Git repository to access + * @return 0 on success, <0 error code on failure + */ +GIT_EXTERN(int) git_refdb_backend_fs( + git_refdb_backend **backend_out, + git_repository *repo); + +/** + * Sets the custom backend to an existing reference DB + * + * The `git_refdb` will take ownership of the `git_refdb_backend` so you + * should NOT free it after calling this function. + * + * @param refdb database to add the backend to + * @param backend pointer to a git_refdb_backend instance + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_refdb_set_backend( + git_refdb *refdb, + git_refdb_backend *backend); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/refs.h b/include/git2/sys/refs.h new file mode 100644 index 00000000000..e434e67c34d --- /dev/null +++ b/include/git2/sys/refs.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_refdb_h__ +#define INCLUDE_sys_git_refdb_h__ + +#include "git2/common.h" +#include "git2/types.h" +#include "git2/oid.h" + +/** + * @file git2/sys/refs.h + * @brief Low-level git reference creation + * @defgroup git_backend Low-level git reference creation + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Create a new direct reference from an OID. + * + * @param name the reference name + * @param oid the object id for a direct reference + * @param peel the first non-tag object's OID, or NULL + * @return the created git_reference or NULL on error + */ +GIT_EXTERN(git_reference *) git_reference__alloc( + const char *name, + const git_oid *oid, + const git_oid *peel); + +/** + * Create a new symbolic reference. + * + * @param name the reference name + * @param target the target for a symbolic reference + * @return the created git_reference or NULL on error + */ +GIT_EXTERN(git_reference *) git_reference__alloc_symbolic( + const char *name, + const char *target); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/remote.h b/include/git2/sys/remote.h new file mode 100644 index 00000000000..476965daa72 --- /dev/null +++ b/include/git2/sys/remote.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_sys_git_remote_h +#define INCLUDE_sys_git_remote_h + +#include "git2/remote.h" + +/** + * @file git2/sys/remote.h + * @brief Low-level remote functionality for custom transports + * @defgroup git_remote Low-level remote functionality for custom transports + * @ingroup Git + * @{ +*/ + +GIT_BEGIN_DECL + +/** + * A remote's capabilities. + */ +typedef enum { + /** Remote supports fetching an advertised object by ID. */ + GIT_REMOTE_CAPABILITY_TIP_OID = (1 << 0), + + /** Remote supports fetching an individual reachable object. */ + GIT_REMOTE_CAPABILITY_REACHABLE_OID = (1 << 1), + + /** Remote supports push options. */ + GIT_REMOTE_CAPABILITY_PUSH_OPTIONS = (1 << 2), +} git_remote_capability_t; + +/** + * Disposes libgit2-initialized fields from a git_remote_connect_options. + * This should only be used for git_remote_connect_options returned by + * git_transport_remote_connect_options. + * + * Note that this does not free the `git_remote_connect_options` itself, just + * the memory pointed to by it. + * + * @param opts The `git_remote_connect_options` struct to dispose. + */ +GIT_EXTERN(void) git_remote_connect_options_dispose( + git_remote_connect_options *opts); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/repository.h b/include/git2/sys/repository.h new file mode 100644 index 00000000000..cdd1a4bd2d6 --- /dev/null +++ b/include/git2/sys/repository.h @@ -0,0 +1,244 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_repository_h__ +#define INCLUDE_sys_git_repository_h__ + +#include "git2/common.h" +#include "git2/types.h" +#include "git2/oid.h" + +/** + * @file git2/sys/repository.h + * @brief Custom repository handling + * @defgroup git_repository Custom repository handling + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * The options for creating an repository from scratch. + * + * Initialize with `GIT_REPOSITORY_NEW_OPTIONS_INIT`. Alternatively, + * you can use `git_repository_new_options_init`. + * + * @options[version] GIT_REPOSITORY_NEW_OPTIONS_VERSION + * @options[init_macro] GIT_REPOSITORY_NEW_OPTIONS_INIT + * @options[init_function] git_repository_new_options_init + */ +typedef struct git_repository_new_options { + unsigned int version; /**< The version */ + + /** + * The object ID type for the object IDs that exist in the index. + * + * If this is not specified, this defaults to `GIT_OID_SHA1`. + */ + git_oid_t oid_type; +} git_repository_new_options; + +/** Current version for the `git_repository_new_options` structure */ +#define GIT_REPOSITORY_NEW_OPTIONS_VERSION 1 + +/** Static constructor for `git_repository_new_options` */ +#define GIT_REPOSITORY_NEW_OPTIONS_INIT { GIT_REPOSITORY_NEW_OPTIONS_VERSION } + +/** + * Initialize git_repository_new_options structure + * + * Initializes a `git_repository_new_options` with default values. + * Equivalent to creating an instance with + * `GIT_REPOSITORY_NEW_OPTIONS_INIT`. + * + * @param opts The `git_repository_new_options` struct to initialize. + * @param version The struct version; pass `GIT_REPOSITORY_NEW_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_repository_new_options_init( + git_repository_new_options *opts, + unsigned int version); + +/** + * Create a new repository with neither backends nor config object + * + * Note that this is only useful if you wish to associate the repository + * with a non-filesystem-backed object database and config store. + * + * Caveats: since this repository has no physical location, some systems + * can fail to function properly: locations under $GIT_DIR, $GIT_COMMON_DIR, + * or $GIT_INFO_DIR are impacted. + * + * @note This API only creates SHA1 repositories + * @see git_repository_new_ext + * + * @param[out] out The blank repository + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_repository_new(git_repository **out); + +#ifdef GIT_EXPERIMENTAL_SHA256 + +/** + * Create a new repository with no backends. + * + * @param[out] out The blank repository + * @param opts the options for repository creation, or NULL for defaults + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_repository_new_ext( + git_repository **out, + git_repository_new_options *opts); + +#endif + +/** + * Reset all the internal state in a repository. + * + * This will free all the mapped memory and internal objects + * of the repository and leave it in a "blank" state. + * + * There's no need to call this function directly unless you're + * trying to aggressively cleanup the repo before its + * deallocation. `git_repository_free` already performs this operation + * before deallocating the repo. + * + * @param repo The repository to clean up + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_repository__cleanup(git_repository *repo); + +/** + * Update the filesystem config settings for an open repository + * + * When a repository is initialized, config values are set based on the + * properties of the filesystem that the repository is on, such as + * "core.ignorecase", "core.filemode", "core.symlinks", etc. If the + * repository is moved to a new filesystem, these properties may no + * longer be correct and API calls may not behave as expected. This + * call reruns the phase of repository initialization that sets those + * properties to compensate for the current filesystem of the repo. + * + * @param repo A repository object + * @param recurse_submodules Should submodules be updated recursively + * @return 0 on success, < 0 on error + */ +GIT_EXTERN(int) git_repository_reinit_filesystem( + git_repository *repo, + int recurse_submodules); + +/** + * Set the configuration file for this repository + * + * This configuration file will be used for all configuration + * queries involving this repository. + * + * The repository will keep a reference to the config file; + * the user must still free the config after setting it + * to the repository, or it will leak. + * + * @param repo A repository object + * @param config A Config object + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_repository_set_config(git_repository *repo, git_config *config); + +/** + * Set the Object Database for this repository + * + * The ODB will be used for all object-related operations + * involving this repository. + * + * The repository will keep a reference to the ODB; the user + * must still free the ODB object after setting it to the + * repository, or it will leak. + * + * @param repo A repository object + * @param odb An ODB object + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_repository_set_odb(git_repository *repo, git_odb *odb); + +/** + * Set the Reference Database Backend for this repository + * + * The refdb will be used for all reference related operations + * involving this repository. + * + * The repository will keep a reference to the refdb; the user + * must still free the refdb object after setting it to the + * repository, or it will leak. + * + * @param repo A repository object + * @param refdb An refdb object + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_repository_set_refdb(git_repository *repo, git_refdb *refdb); + +/** + * Set the index file for this repository + * + * This index will be used for all index-related operations + * involving this repository. + * + * The repository will keep a reference to the index file; + * the user must still free the index after setting it + * to the repository, or it will leak. + * + * @param repo A repository object + * @param index An index object + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_repository_set_index(git_repository *repo, git_index *index); + +/** + * Set a repository to be bare. + * + * Clear the working directory and set core.bare to true. You may also + * want to call `git_repository_set_index(repo, NULL)` since a bare repo + * typically does not have an index, but this function will not do that + * for you. + * + * @param repo Repo to make bare + * @return 0 on success, <0 on failure + */ +GIT_EXTERN(int) git_repository_set_bare(git_repository *repo); + +/** + * Load and cache all submodules. + * + * Because the `.gitmodules` file is unstructured, loading submodules is an + * O(N) operation. Any operation (such as `git_rebase_init`) that requires + * accessing all submodules is O(N^2) in the number of submodules, if it + * has to look each one up individually. This function loads all submodules + * and caches them so that subsequent calls to `git_submodule_lookup` are O(1). + * + * @param repo the repository whose submodules will be cached. + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_repository_submodule_cache_all( + git_repository *repo); + +/** + * Clear the submodule cache. + * + * Clear the submodule cache populated by `git_repository_submodule_cache_all`. + * If there is no cache, do nothing. + * + * The cache incorporates data from the repository's configuration, as well + * as the state of the working tree, the index, and HEAD. So any time any + * of these has changed, the cache might become invalid. + * + * @param repo the repository whose submodule cache will be cleared + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_repository_submodule_cache_clear( + git_repository *repo); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/stream.h b/include/git2/sys/stream.h new file mode 100644 index 00000000000..eabff68643f --- /dev/null +++ b/include/git2/sys/stream.h @@ -0,0 +1,161 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sys_git_stream_h__ +#define INCLUDE_sys_git_stream_h__ + +#include "git2/common.h" +#include "git2/types.h" +#include "git2/proxy.h" + +/** + * @file git2/sys/stream.h + * @brief Streaming file I/O functionality + * @defgroup git_stream Streaming file I/O functionality + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** Current version for the `git_stream` structures */ +#define GIT_STREAM_VERSION 1 + +/** + * Every stream must have this struct as its first element, so the + * API can talk to it. You'd define your stream as + * + * struct my_stream { + * git_stream parent; + * ... + * } + * + * and fill the functions + */ +typedef struct git_stream { + int version; + + unsigned int encrypted : 1, + proxy_support : 1; + + /** + * Timeout for read and write operations; can be set to `0` to + * block indefinitely. + */ + int timeout; + + /** + * Timeout to connect to the remote server; can be set to `0` + * to use the system defaults. This can be shorter than the + * system default - often 75 seconds - but cannot be longer. + */ + int connect_timeout; + + int GIT_CALLBACK(connect)(struct git_stream *); + int GIT_CALLBACK(certificate)(git_cert **, struct git_stream *); + int GIT_CALLBACK(set_proxy)(struct git_stream *, const git_proxy_options *proxy_opts); + ssize_t GIT_CALLBACK(read)(struct git_stream *, void *, size_t); + ssize_t GIT_CALLBACK(write)(struct git_stream *, const char *, size_t, int); + int GIT_CALLBACK(close)(struct git_stream *); + void GIT_CALLBACK(free)(struct git_stream *); +} git_stream; + +typedef struct { + /** The `version` field should be set to `GIT_STREAM_VERSION`. */ + int version; + + /** + * Called to create a new connection to a given host. + * + * @param out The created stream + * @param host The hostname to connect to; may be a hostname or + * IP address + * @param port The port to connect to; may be a port number or + * service name + * @return 0 or an error code + */ + int GIT_CALLBACK(init)(git_stream **out, const char *host, const char *port); + + /** + * Called to create a new connection on top of the given stream. If + * this is a TLS stream, then this function may be used to proxy a + * TLS stream over an HTTP CONNECT session. If this is unset, then + * HTTP CONNECT proxies will not be supported. + * + * @param out The created stream + * @param in An existing stream to add TLS to + * @param host The hostname that the stream is connected to, + * for certificate validation + * @return 0 or an error code + */ + int GIT_CALLBACK(wrap)(git_stream **out, git_stream *in, const char *host); +} git_stream_registration; + +/** + * The type of stream to register. + */ +typedef enum { + /** A standard (non-TLS) socket. */ + GIT_STREAM_STANDARD = 1, + + /** A TLS-encrypted socket. */ + GIT_STREAM_TLS = 2 +} git_stream_t; + +/** + * Register stream constructors for the library to use + * + * If a registration structure is already set, it will be overwritten. + * Pass `NULL` in order to deregister the current constructor and return + * to the system defaults. + * + * The type parameter may be a bitwise AND of types. + * + * @param type the type or types of stream to register + * @param registration the registration data + * @return 0 or an error code + */ +GIT_EXTERN(int) git_stream_register( + git_stream_t type, git_stream_registration *registration); + +#ifndef GIT_DEPRECATE_HARD + +/** @name Deprecated TLS Stream Registration Functions + * + * These functions are retained for backward compatibility. The newer + * versions of these values should be preferred in all new code. + * + * There is no plan to remove these backward compatibility values at + * this time. + */ +/**@{*/ + +/** + * @deprecated Provide a git_stream_registration to git_stream_register + * @see git_stream_registration + */ +typedef int GIT_CALLBACK(git_stream_cb)(git_stream **out, const char *host, const char *port); + +/** + * Register a TLS stream constructor for the library to use. This stream + * will not support HTTP CONNECT proxies. This internally calls + * `git_stream_register` and is preserved for backward compatibility. + * + * This function is deprecated, but there is no plan to remove this + * function at this time. + * + * @deprecated Provide a git_stream_registration to git_stream_register + * @see git_stream_register + */ +GIT_EXTERN(int) git_stream_register_tls(git_stream_cb ctor); + +/**@}*/ + +#endif + +/**@}*/ +GIT_END_DECL + +#endif diff --git a/include/git2/sys/transport.h b/include/git2/sys/transport.h new file mode 100644 index 00000000000..6a190242cdb --- /dev/null +++ b/include/git2/sys/transport.h @@ -0,0 +1,486 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_sys_git_transport_h +#define INCLUDE_sys_git_transport_h + +#include "git2/net.h" +#include "git2/oidarray.h" +#include "git2/proxy.h" +#include "git2/remote.h" +#include "git2/strarray.h" +#include "git2/transport.h" +#include "git2/types.h" + +/** + * @file git2/sys/transport.h + * @brief Custom transport registration interfaces and functions + * @defgroup git_transport Custom transport registration + * @ingroup Git + * + * Callers can override the default HTTPS or SSH implementation by + * specifying a custom transport. + * @{ + */ + +GIT_BEGIN_DECL + +/** + * The negotiation state during a fetch smart transport negotiation. + */ +typedef struct { + const git_remote_head * const *refs; + size_t refs_len; + git_oid *shallow_roots; + size_t shallow_roots_len; + int depth; +} git_fetch_negotiation; + +struct git_transport { + unsigned int version; /**< The struct version */ + + /** + * Connect the transport to the remote repository, using the given + * direction. + */ + int GIT_CALLBACK(connect)( + git_transport *transport, + const char *url, + int direction, + const git_remote_connect_options *connect_opts); + + /** + * Resets the connect options for the given transport. This + * is useful for updating settings or callbacks for an already + * connected transport. + */ + int GIT_CALLBACK(set_connect_opts)( + git_transport *transport, + const git_remote_connect_options *connect_opts); + + /** + * Gets the capabilities for this remote repository. + * + * This function may be called after a successful call to + * `connect()`. + */ + int GIT_CALLBACK(capabilities)( + unsigned int *capabilities, + git_transport *transport); + +#ifdef GIT_EXPERIMENTAL_SHA256 + /** + * Gets the object type for the remote repository. + * + * This function may be called after a successful call to + * `connect()`. + */ + int GIT_CALLBACK(oid_type)( + git_oid_t *object_type, + git_transport *transport); +#endif + + /** + * Get the list of available references in the remote repository. + * + * This function may be called after a successful call to + * `connect()`. The array returned is owned by the transport and + * must be kept valid until the next call to one of its functions. + */ + int GIT_CALLBACK(ls)( + const git_remote_head ***out, + size_t *size, + git_transport *transport); + + /** Executes the push whose context is in the git_push object. */ + int GIT_CALLBACK(push)( + git_transport *transport, + git_push *push); + + /** + * Negotiate a fetch with the remote repository. + * + * This function may be called after a successful call to `connect()`, + * when the direction is GIT_DIRECTION_FETCH. The function performs a + * negotiation to calculate the `wants` list for the fetch. + */ + int GIT_CALLBACK(negotiate_fetch)( + git_transport *transport, + git_repository *repo, + const git_fetch_negotiation *fetch_data); + + /** + * Return the shallow roots of the remote. + * + * This function may be called after a successful call to + * `negotiate_fetch`. + */ + int GIT_CALLBACK(shallow_roots)( + git_oidarray *out, + git_transport *transport); + + /** + * Start downloading the packfile from the remote repository. + * + * This function may be called after a successful call to + * negotiate_fetch(), when the direction is GIT_DIRECTION_FETCH. + */ + int GIT_CALLBACK(download_pack)( + git_transport *transport, + git_repository *repo, + git_indexer_progress *stats); + + /** Checks to see if the transport is connected */ + int GIT_CALLBACK(is_connected)(git_transport *transport); + + /** Cancels any outstanding transport operation */ + void GIT_CALLBACK(cancel)(git_transport *transport); + + /** + * Close the connection to the remote repository. + * + * This function is the reverse of connect() -- it terminates the + * connection to the remote end. + */ + int GIT_CALLBACK(close)(git_transport *transport); + + /** Frees/destructs the git_transport object. */ + void GIT_CALLBACK(free)(git_transport *transport); +}; + +/** Current version for the `git_transport` structure */ +#define GIT_TRANSPORT_VERSION 1 + +/** Static constructor for `git_transport` */ +#define GIT_TRANSPORT_INIT {GIT_TRANSPORT_VERSION} + +/** + * Initializes a `git_transport` with default values. Equivalent to + * creating an instance with GIT_TRANSPORT_INIT. + * + * @param opts the `git_transport` struct to initialize + * @param version Version of struct; pass `GIT_TRANSPORT_VERSION` + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_transport_init( + git_transport *opts, + unsigned int version); + +/** + * Function to use to create a transport from a URL. The transport database + * is scanned to find a transport that implements the scheme of the URI (i.e. + * git:// or http://) and a transport object is returned to the caller. + * + * @param out The newly created transport (out) + * @param owner The git_remote which will own this transport + * @param url The URL to connect to + * @return 0 or an error code + */ +GIT_EXTERN(int) git_transport_new(git_transport **out, git_remote *owner, const char *url); + +/** + * Create an ssh transport with custom git command paths + * + * This is a factory function suitable for setting as the transport + * callback in a remote (or for a clone in the options). + * + * The payload argument must be a strarray pointer with the paths for + * the `git-upload-pack` and `git-receive-pack` at index 0 and 1. + * + * @param out the resulting transport + * @param owner the owning remote + * @param payload a strarray with the paths + * @return 0 or an error code + */ +GIT_EXTERN(int) git_transport_ssh_with_paths(git_transport **out, git_remote *owner, void *payload); + +/** + * Add a custom transport definition, to be used in addition to the built-in + * set of transports that come with libgit2. + * + * The caller is responsible for synchronizing calls to git_transport_register + * and git_transport_unregister with other calls to the library that + * instantiate transports. + * + * @param prefix The scheme to match, eg "git" or "https" + * @param cb The callback used to create an instance of the transport + * @param param A fixed parameter to pass to cb at creation time + * @return 0 or an error code + */ +GIT_EXTERN(int) git_transport_register( + const char *prefix, + git_transport_cb cb, + void *param); + +/** + * Unregister a custom transport definition which was previously registered + * with git_transport_register. + * + * The caller is responsible for synchronizing calls to git_transport_register + * and git_transport_unregister with other calls to the library that + * instantiate transports. + * + * @param prefix From the previous call to git_transport_register + * @return 0 or an error code + */ +GIT_EXTERN(int) git_transport_unregister( + const char *prefix); + +/* Transports which come with libgit2 (match git_transport_cb). The expected + * value for "param" is listed in-line below. */ + +/** + * Create an instance of the dummy transport. + * + * @param out The newly created transport (out) + * @param owner The git_remote which will own this transport + * @param payload You must pass NULL for this parameter. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_transport_dummy( + git_transport **out, + git_remote *owner, + /* NULL */ void *payload); + +/** + * Create an instance of the local transport. + * + * @param out The newly created transport (out) + * @param owner The git_remote which will own this transport + * @param payload You must pass NULL for this parameter. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_transport_local( + git_transport **out, + git_remote *owner, + /* NULL */ void *payload); + +/** + * Create an instance of the smart transport. + * + * @param out The newly created transport (out) + * @param owner The git_remote which will own this transport + * @param payload A pointer to a git_smart_subtransport_definition + * @return 0 or an error code + */ +GIT_EXTERN(int) git_transport_smart( + git_transport **out, + git_remote *owner, + /* (git_smart_subtransport_definition *) */ void *payload); + +/** + * Call the certificate check for this transport. + * + * @param transport a smart transport + * @param cert the certificate to pass to the caller + * @param valid whether we believe the certificate is valid + * @param hostname the hostname we connected to + * @return the return value of the callback: 0 for no error, GIT_PASSTHROUGH + * to indicate that there is no callback registered (or the callback + * refused to validate the certificate and callers should behave as + * if no callback was set), or < 0 for an error + */ +GIT_EXTERN(int) git_transport_smart_certificate_check(git_transport *transport, git_cert *cert, int valid, const char *hostname); + +/** + * Call the credentials callback for this transport + * + * @param out the pointer where the creds are to be stored + * @param transport a smart transport + * @param user the user we saw on the url (if any) + * @param methods available methods for authentication + * @return the return value of the callback: 0 for no error, GIT_PASSTHROUGH + * to indicate that there is no callback registered (or the callback + * refused to provide credentials and callers should behave as if no + * callback was set), or < 0 for an error + */ +GIT_EXTERN(int) git_transport_smart_credentials(git_credential **out, git_transport *transport, const char *user, int methods); + +/** + * Get a copy of the remote connect options + * + * All data is copied and must be freed by the caller by calling + * `git_remote_connect_options_dispose`. + * + * @param out options struct to fill + * @param transport the transport to extract the data from. + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_transport_remote_connect_options( + git_remote_connect_options *out, + git_transport *transport); + +/* + *** End of base transport interface *** + *** Begin interface for subtransports for the smart transport *** + */ + +/** Actions that the smart transport can ask a subtransport to perform */ +typedef enum { + GIT_SERVICE_UPLOADPACK_LS = 1, + GIT_SERVICE_UPLOADPACK = 2, + GIT_SERVICE_RECEIVEPACK_LS = 3, + GIT_SERVICE_RECEIVEPACK = 4 +} git_smart_service_t; + +typedef struct git_smart_subtransport git_smart_subtransport; +typedef struct git_smart_subtransport_stream git_smart_subtransport_stream; + +/** + * A stream used by the smart transport to read and write data + * from a subtransport. + * + * This provides a customization point in case you need to + * support some other communication method. + */ +struct git_smart_subtransport_stream { + git_smart_subtransport *subtransport; /**< The owning subtransport */ + + /** + * Read available data from the stream. + * + * The implementation may read less than requested. + */ + int GIT_CALLBACK(read)( + git_smart_subtransport_stream *stream, + char *buffer, + size_t buf_size, + size_t *bytes_read); + + /** + * Write data to the stream + * + * The implementation must write all data or return an error. + */ + int GIT_CALLBACK(write)( + git_smart_subtransport_stream *stream, + const char *buffer, + size_t len); + + /** Free the stream */ + void GIT_CALLBACK(free)( + git_smart_subtransport_stream *stream); +}; + +/** + * An implementation of a subtransport which carries data for the + * smart transport + */ +struct git_smart_subtransport { + /** + * Setup a subtransport stream for the requested action. + */ + int GIT_CALLBACK(action)( + git_smart_subtransport_stream **out, + git_smart_subtransport *transport, + const char *url, + git_smart_service_t action); + + /** + * Close the subtransport. + * + * Subtransports are guaranteed a call to close() between + * calls to action(), except for the following two "natural" progressions + * of actions against a constant URL: + * + * - UPLOADPACK_LS -> UPLOADPACK + * - RECEIVEPACK_LS -> RECEIVEPACK + */ + int GIT_CALLBACK(close)(git_smart_subtransport *transport); + + /** Free the subtransport */ + void GIT_CALLBACK(free)(git_smart_subtransport *transport); +}; + +/** + * A function that creates a new subtransport for the smart transport + * + * @param out the smart subtransport + * @param owner the transport owner + * @param param the input parameter + * @return 0 on success, or an error code + */ +typedef int GIT_CALLBACK(git_smart_subtransport_cb)( + git_smart_subtransport **out, + git_transport *owner, + void *param); + +/** + * Definition for a "subtransport" + * + * The smart transport knows how to speak the git protocol, but it has no + * knowledge of how to establish a connection between it and another endpoint, + * or how to move data back and forth. For this, a subtransport interface is + * declared, and the smart transport delegates this work to the subtransports. + * + * Three subtransports are provided by libgit2: ssh, git, http(s). + * + * Subtransports can either be RPC = 0 (persistent connection) or RPC = 1 + * (request/response). The smart transport handles the differences in its own + * logic. The git subtransport is RPC = 0, while http is RPC = 1. + */ +typedef struct git_smart_subtransport_definition { + /** The function to use to create the git_smart_subtransport */ + git_smart_subtransport_cb callback; + + /** + * True if the protocol is stateless; false otherwise. For example, + * http:// is stateless, but git:// is not. + */ + unsigned rpc; + + /** User-specified parameter passed to the callback */ + void *param; +} git_smart_subtransport_definition; + +/* Smart transport subtransports that come with libgit2 */ + +/** + * Create an instance of the http subtransport. + * + * This subtransport also supports https. + * + * @param out The newly created subtransport + * @param owner The smart transport to own this subtransport + * @param param custom parameters for the subtransport + * @return 0 or an error code + */ +GIT_EXTERN(int) git_smart_subtransport_http( + git_smart_subtransport **out, + git_transport *owner, + void *param); + +/** + * Create an instance of the git subtransport. + * + * @param out The newly created subtransport + * @param owner The smart transport to own this subtransport + * @param param custom parameters for the subtransport + * @return 0 or an error code + */ +GIT_EXTERN(int) git_smart_subtransport_git( + git_smart_subtransport **out, + git_transport *owner, + void *param); + +/** + * Create an instance of the ssh subtransport. + * + * @param out The newly created subtransport + * @param owner The smart transport to own this subtransport + * @param param custom parameters for the subtransport + * @return 0 or an error code + */ +GIT_EXTERN(int) git_smart_subtransport_ssh( + git_smart_subtransport **out, + git_transport *owner, + void *param); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/tag.h b/include/git2/tag.h index 1ffeb0b4a58..3b0c12ebcb8 100644 --- a/include/git2/tag.h +++ b/include/git2/tag.h @@ -15,7 +15,7 @@ /** * @file git2/tag.h - * @brief Git tag parsing routines + * @brief A (nearly) immutable pointer to a commit; useful for versioning * @defgroup git_tag Git tag management * @ingroup Git * @{ @@ -30,12 +30,8 @@ GIT_BEGIN_DECL * @param id identity of the tag to locate. * @return 0 or an error code */ -GIT_INLINE(int) git_tag_lookup( - git_tag **out, git_repository *repo, const git_oid *id) -{ - return git_object_lookup( - (git_object **)out, repo, id, (git_otype)GIT_OBJ_TAG); -} +GIT_EXTERN(int) git_tag_lookup( + git_tag **out, git_repository *repo, const git_oid *id); /** * Lookup a tag object from the repository, @@ -49,12 +45,8 @@ GIT_INLINE(int) git_tag_lookup( * @param len the length of the short identifier * @return 0 or an error code */ -GIT_INLINE(int) git_tag_lookup_prefix( - git_tag **out, git_repository *repo, const git_oid *id, size_t len) -{ - return git_object_lookup_prefix( - (git_object **)out, repo, id, len, (git_otype)GIT_OBJ_TAG); -} +GIT_EXTERN(int) git_tag_lookup_prefix( + git_tag **out, git_repository *repo, const git_oid *id, size_t len); /** * Close an open tag @@ -66,12 +58,7 @@ GIT_INLINE(int) git_tag_lookup_prefix( * * @param tag the tag to close */ - -GIT_INLINE(void) git_tag_free(git_tag *tag) -{ - git_object_free((git_object *)tag); -} - +GIT_EXTERN(void) git_tag_free(git_tag *tag); /** * Get the id of a tag. @@ -81,6 +68,14 @@ GIT_INLINE(void) git_tag_free(git_tag *tag) */ GIT_EXTERN(const git_oid *) git_tag_id(const git_tag *tag); +/** + * Get the repository that contains the tag. + * + * @param tag A previously loaded tag. + * @return Repository that contains this tag. + */ +GIT_EXTERN(git_repository *) git_tag_owner(const git_tag *tag); + /** * Get the tagged object of a tag * @@ -107,7 +102,7 @@ GIT_EXTERN(const git_oid *) git_tag_target_id(const git_tag *tag); * @param tag a previously loaded tag. * @return type of the tagged object */ -GIT_EXTERN(git_otype) git_tag_target_type(const git_tag *tag); +GIT_EXTERN(git_object_t) git_tag_target_type(const git_tag *tag); /** * Get the name of a tag @@ -121,7 +116,7 @@ GIT_EXTERN(const char *) git_tag_name(const git_tag *tag); * Get the tagger (author) of a tag * * @param tag a previously loaded tag. - * @return reference to the tag's author + * @return reference to the tag's author or NULL when unspecified */ GIT_EXTERN(const git_signature *) git_tag_tagger(const git_tag *tag); @@ -129,7 +124,7 @@ GIT_EXTERN(const git_signature *) git_tag_tagger(const git_tag *tag); * Get the message of a tag * * @param tag a previously loaded tag. - * @return message of the tag + * @return message of the tag or NULL when unspecified */ GIT_EXTERN(const char *) git_tag_message(const git_tag *tag); @@ -182,6 +177,37 @@ GIT_EXTERN(int) git_tag_create( const char *message, int force); +/** + * Create a new tag in the object database pointing to a git_object + * + * The message will not be cleaned up. This can be achieved + * through `git_message_prettify()`. + * + * @param oid Pointer where to store the OID of the + * newly created tag + * + * @param repo Repository where to store the tag + * + * @param tag_name Name for the tag + * + * @param target Object to which this tag points. This object + * must belong to the given `repo`. + * + * @param tagger Signature of the tagger for this tag, and + * of the tagging time + * + * @param message Full message for this tag + * + * @return 0 on success or an error code + */ +GIT_EXTERN(int) git_tag_annotation_create( + git_oid *oid, + git_repository *repo, + const char *tag_name, + const git_object *target, + const git_signature *tagger, + const char *message); + /** * Create a new tag in the repository from a buffer * @@ -191,7 +217,7 @@ GIT_EXTERN(int) git_tag_create( * @param force Overwrite existing tags * @return 0 on success; error code otherwise */ -GIT_EXTERN(int) git_tag_create_frombuffer( +GIT_EXTERN(int) git_tag_create_from_buffer( git_oid *oid, git_repository *repo, const char *buffer, @@ -291,8 +317,17 @@ GIT_EXTERN(int) git_tag_list_match( const char *pattern, git_repository *repo); - -typedef int (*git_tag_foreach_cb)(const char *name, git_oid *oid, void *payload); +/** + * Callback used to iterate over tag names + * + * @see git_tag_foreach + * + * @param name The tag name + * @param oid The tag's OID + * @param payload Payload passed to git_tag_foreach + * @return non-zero to terminate the iteration + */ +typedef int GIT_CALLBACK(git_tag_foreach_cb)(const char *name, git_oid *oid, void *payload); /** * Call callback `cb' for each tag in the repository @@ -300,6 +335,7 @@ typedef int (*git_tag_foreach_cb)(const char *name, git_oid *oid, void *payload) * @param repo Repository * @param callback Callback function * @param payload Pointer to callback data (optional) + * @return 0 on success or an error code */ GIT_EXTERN(int) git_tag_foreach( git_repository *repo, @@ -321,6 +357,29 @@ GIT_EXTERN(int) git_tag_peel( git_object **tag_target_out, const git_tag *tag); +/** + * Create an in-memory copy of a tag. The copy must be explicitly + * free'd or it will leak. + * + * @param out Pointer to store the copy of the tag + * @param source Original tag to copy + * @return 0 + */ +GIT_EXTERN(int) git_tag_dup(git_tag **out, git_tag *source); + +/** + * Determine whether a tag name is valid, meaning that (when prefixed + * with `refs/tags/`) that it is a valid reference name, and that any + * additional tag name restrictions are imposed (eg, it cannot start + * with a `-`). + * + * @param valid output pointer to set with validity of given tag name + * @param name a tag name to test + * @return 0 on success or an error code + */ +GIT_EXTERN(int) git_tag_name_is_valid(int *valid, const char *name); + /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/threads.h b/include/git2/threads.h deleted file mode 100644 index 11f89729a04..00000000000 --- a/include/git2/threads.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_git_threads_h__ -#define INCLUDE_git_threads_h__ - -#include "common.h" - -/** - * @file git2/threads.h - * @brief Library level thread functions - * @defgroup git_thread Threading functions - * @ingroup Git - * @{ - */ -GIT_BEGIN_DECL - -/** - * Init the threading system. - * - * If libgit2 has been built with GIT_THREADS - * on, this function must be called once before - * any other library functions. - * - * If libgit2 has been built without GIT_THREADS - * support, this function is a no-op. - * - * @return 0 or an error code - */ -GIT_EXTERN(int) git_threads_init(void); - -/** - * Shutdown the threading system. - * - * If libgit2 has been built with GIT_THREADS - * on, this function must be called before shutting - * down the library. - * - * If libgit2 has been built without GIT_THREADS - * support, this function is a no-op. - */ -GIT_EXTERN(void) git_threads_shutdown(void); - -/** @} */ -GIT_END_DECL -#endif - diff --git a/include/git2/trace.h b/include/git2/trace.h new file mode 100644 index 00000000000..62cb87c012c --- /dev/null +++ b/include/git2/trace.h @@ -0,0 +1,73 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_trace_h__ +#define INCLUDE_git_trace_h__ + +#include "common.h" +#include "types.h" + +/** + * @file git2/trace.h + * @brief Tracing functionality to introspect libgit2 in your application + * @defgroup git_trace Tracing functionality to introspect libgit2 in your application + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Available tracing levels. When tracing is set to a particular level, + * callers will be provided tracing at the given level and all lower levels. + */ +typedef enum { + /** No tracing will be performed. */ + GIT_TRACE_NONE = 0, + + /** Severe errors that may impact the program's execution */ + GIT_TRACE_FATAL = 1, + + /** Errors that do not impact the program's execution */ + GIT_TRACE_ERROR = 2, + + /** Warnings that suggest abnormal data */ + GIT_TRACE_WARN = 3, + + /** Informational messages about program execution */ + GIT_TRACE_INFO = 4, + + /** Detailed data that allows for debugging */ + GIT_TRACE_DEBUG = 5, + + /** Exceptionally detailed debugging data */ + GIT_TRACE_TRACE = 6 +} git_trace_level_t; + +/** + * An instance for a tracing function + * + * @param level the trace level + * @param msg the trace message + */ +typedef void GIT_CALLBACK(git_trace_cb)( + git_trace_level_t level, + const char *msg); + +/** + * Sets the system tracing configuration to the specified level with the + * specified callback. When system events occur at a level equal to, or + * lower than, the given level they will be reported to the given callback. + * + * @param level Level to set tracing to + * @param cb Function to call with trace data + * @return 0 or an error code + */ +GIT_EXTERN(int) git_trace_set(git_trace_level_t level, git_trace_cb cb); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/transaction.h b/include/git2/transaction.h new file mode 100644 index 00000000000..212d32919a7 --- /dev/null +++ b/include/git2/transaction.h @@ -0,0 +1,122 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_transaction_h__ +#define INCLUDE_git_transaction_h__ + +#include "common.h" +#include "types.h" + +/** + * @file git2/transaction.h + * @brief Transactional reference handling + * @defgroup git_transaction Transactional reference handling + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Create a new transaction object + * + * This does not lock anything, but sets up the transaction object to + * know from which repository to lock. + * + * @param out the resulting transaction + * @param repo the repository in which to lock + * @return 0 or an error code + */ +GIT_EXTERN(int) git_transaction_new(git_transaction **out, git_repository *repo); + +/** + * Lock a reference + * + * Lock the specified reference. This is the first step to updating a + * reference. + * + * @param tx the transaction + * @param refname the reference to lock + * @return 0 or an error message + */ +GIT_EXTERN(int) git_transaction_lock_ref(git_transaction *tx, const char *refname); + +/** + * Set the target of a reference + * + * Set the target of the specified reference. This reference must be + * locked. + * + * @param tx the transaction + * @param refname reference to update + * @param target target to set the reference to + * @param sig signature to use in the reflog; pass NULL to read the identity from the config + * @param msg message to use in the reflog + * @return 0, GIT_ENOTFOUND if the reference is not among the locked ones, or an error code + */ +GIT_EXTERN(int) git_transaction_set_target(git_transaction *tx, const char *refname, const git_oid *target, const git_signature *sig, const char *msg); + +/** + * Set the target of a reference + * + * Set the target of the specified reference. This reference must be + * locked. + * + * @param tx the transaction + * @param refname reference to update + * @param target target to set the reference to + * @param sig signature to use in the reflog; pass NULL to read the identity from the config + * @param msg message to use in the reflog + * @return 0, GIT_ENOTFOUND if the reference is not among the locked ones, or an error code + */ +GIT_EXTERN(int) git_transaction_set_symbolic_target(git_transaction *tx, const char *refname, const char *target, const git_signature *sig, const char *msg); + +/** + * Set the reflog of a reference + * + * Set the specified reference's reflog. If this is combined with + * setting the target, that update won't be written to the reflog. + * + * @param tx the transaction + * @param refname the reference whose reflog to set + * @param reflog the reflog as it should be written out + * @return 0, GIT_ENOTFOUND if the reference is not among the locked ones, or an error code + */ +GIT_EXTERN(int) git_transaction_set_reflog(git_transaction *tx, const char *refname, const git_reflog *reflog); + +/** + * Remove a reference + * + * @param tx the transaction + * @param refname the reference to remove + * @return 0, GIT_ENOTFOUND if the reference is not among the locked ones, or an error code + */ +GIT_EXTERN(int) git_transaction_remove(git_transaction *tx, const char *refname); + +/** + * Commit the changes from the transaction + * + * Perform the changes that have been queued. The updates will be made + * one by one, and the first failure will stop the processing. + * + * @param tx the transaction + * @return 0 or an error code + */ +GIT_EXTERN(int) git_transaction_commit(git_transaction *tx); + +/** + * Free the resources allocated by this transaction + * + * If any references remain locked, they will be unlocked without any + * changes made to them. + * + * @param tx the transaction + */ +GIT_EXTERN(void) git_transaction_free(git_transaction *tx); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/include/git2/transport.h b/include/git2/transport.h index 4945ff15106..04a7390b10f 100644 --- a/include/git2/transport.h +++ b/include/git2/transport.h @@ -10,326 +10,41 @@ #include "indexer.h" #include "net.h" #include "types.h" +#include "cert.h" +#include "credential.h" /** * @file git2/transport.h - * @brief Git transport interfaces and functions - * @defgroup git_transport interfaces and functions + * @brief Transports are the low-level mechanism to connect to a remote server + * @defgroup git_transport Transports are the low-level mechanism to connect to a remote server * @ingroup Git * @{ */ GIT_BEGIN_DECL -/* - *** Begin interface for credentials acquisition *** - */ - -typedef enum { - /* git_cred_userpass_plaintext */ - GIT_CREDTYPE_USERPASS_PLAINTEXT = 1, -} git_credtype_t; - -/* The base structure for all credential types */ -typedef struct git_cred { - git_credtype_t credtype; - void (*free)( - struct git_cred *cred); -} git_cred; - -/* A plaintext username and password */ -typedef struct git_cred_userpass_plaintext { - git_cred parent; - char *username; - char *password; -} git_cred_userpass_plaintext; - -/** - * Creates a new plain-text username and password credential object. - * The supplied credential parameter will be internally duplicated. - * - * @param out The newly created credential object. - * @param username The username of the credential. - * @param password The password of the credential. - * @return 0 for success or an error code for failure - */ -GIT_EXTERN(int) git_cred_userpass_plaintext_new( - git_cred **out, - const char *username, - const char *password); - -/** - * Signature of a function which acquires a credential object. - * - * @param cred The newly created credential object. - * @param url The resource for which we are demanding a credential. - * @param allowed_types A bitmask stating which cred types are OK to return. - * @param payload The payload provided when specifying this callback. - * @return 0 for success or an error code for failure - */ -typedef int (*git_cred_acquire_cb)( - git_cred **cred, - const char *url, - unsigned int allowed_types, - void *payload); - -/* - *** End interface for credentials acquisition *** - *** Begin base transport interface *** - */ - -typedef enum { - GIT_TRANSPORTFLAGS_NONE = 0, - /* If the connection is secured with SSL/TLS, the authenticity - * of the server certificate should not be verified. */ - GIT_TRANSPORTFLAGS_NO_CHECK_CERT = 1 -} git_transport_flags_t; - -typedef void (*git_transport_message_cb)(const char *str, int len, void *data); - -typedef struct git_transport { - unsigned int version; - /* Set progress and error callbacks */ - int (*set_callbacks)(struct git_transport *transport, - git_transport_message_cb progress_cb, - git_transport_message_cb error_cb, - void *payload); - - /* Connect the transport to the remote repository, using the given - * direction. */ - int (*connect)(struct git_transport *transport, - const char *url, - git_cred_acquire_cb cred_acquire_cb, - void *cred_acquire_payload, - int direction, - int flags); - - /* This function may be called after a successful call to connect(). The - * provided callback is invoked for each ref discovered on the remote - * end. */ - int (*ls)(struct git_transport *transport, - git_headlist_cb list_cb, - void *payload); - - /* Executes the push whose context is in the git_push object. */ - int (*push)(struct git_transport *transport, git_push *push); - - /* This function may be called after a successful call to connect(), when - * the direction is FETCH. The function performs a negotiation to calculate - * the wants list for the fetch. */ - int (*negotiate_fetch)(struct git_transport *transport, - git_repository *repo, - const git_remote_head * const *refs, - size_t count); - - /* This function may be called after a successful call to negotiate_fetch(), - * when the direction is FETCH. This function retrieves the pack file for - * the fetch from the remote end. */ - int (*download_pack)(struct git_transport *transport, - git_repository *repo, - git_transfer_progress *stats, - git_transfer_progress_callback progress_cb, - void *progress_payload); - - /* Checks to see if the transport is connected */ - int (*is_connected)(struct git_transport *transport); - - /* Reads the flags value previously passed into connect() */ - int (*read_flags)(struct git_transport *transport, int *flags); - - /* Cancels any outstanding transport operation */ - void (*cancel)(struct git_transport *transport); - - /* This function is the reverse of connect() -- it terminates the - * connection to the remote end. */ - int (*close)(struct git_transport *transport); - - /* Frees/destructs the git_transport object. */ - void (*free)(struct git_transport *transport); -} git_transport; - -#define GIT_TRANSPORT_VERSION 1 -#define GIT_TRANSPORT_INIT {GIT_TRANSPORT_VERSION} - -/** - * Function to use to create a transport from a URL. The transport database - * is scanned to find a transport that implements the scheme of the URI (i.e. - * git:// or http://) and a transport object is returned to the caller. - * - * @param out The newly created transport (out) - * @param owner The git_remote which will own this transport - * @param url The URL to connect to - * @return 0 or an error code - */ -GIT_EXTERN(int) git_transport_new(git_transport **out, git_remote *owner, const char *url); - -/** - * Function which checks to see if a transport could be created for the - * given URL (i.e. checks to see if libgit2 has a transport that supports - * the given URL's scheme) - * - * @param url The URL to check - * @return Zero if the URL is not valid; nonzero otherwise - */ -GIT_EXTERN(int) git_transport_valid_url(const char *url); - -/* Signature of a function which creates a transport */ -typedef int (*git_transport_cb)(git_transport **out, git_remote *owner, void *param); - -/* Transports which come with libgit2 (match git_transport_cb). The expected - * value for "param" is listed in-line below. */ - /** - * Create an instance of the dummy transport. + * Callback for messages received by the transport. * - * @param out The newly created transport (out) - * @param owner The git_remote which will own this transport - * @param payload You must pass NULL for this parameter. - * @return 0 or an error code - */ -GIT_EXTERN(int) git_transport_dummy( - git_transport **out, - git_remote *owner, - /* NULL */ void *payload); - -/** - * Create an instance of the local transport. + * Return a negative value to cancel the network operation. * - * @param out The newly created transport (out) - * @param owner The git_remote which will own this transport - * @param payload You must pass NULL for this parameter. - * @return 0 or an error code + * @param str The message from the transport + * @param len The length of the message + * @param payload Payload provided by the caller + * @return 0 on success or an error code */ -GIT_EXTERN(int) git_transport_local( - git_transport **out, - git_remote *owner, - /* NULL */ void *payload); +typedef int GIT_CALLBACK(git_transport_message_cb)(const char *str, int len, void *payload); /** - * Create an instance of the smart transport. + * Signature of a function which creates a transport. * - * @param out The newly created transport (out) - * @param owner The git_remote which will own this transport - * @param payload A pointer to a git_smart_subtransport_definition - * @return 0 or an error code - */ -GIT_EXTERN(int) git_transport_smart( - git_transport **out, - git_remote *owner, - /* (git_smart_subtransport_definition *) */ void *payload); - -/* - *** End of base transport interface *** - *** Begin interface for subtransports for the smart transport *** - */ - -/* The smart transport knows how to speak the git protocol, but it has no - * knowledge of how to establish a connection between it and another endpoint, - * or how to move data back and forth. For this, a subtransport interface is - * declared, and the smart transport delegates this work to the subtransports. - * Three subtransports are implemented: git, http, and winhttp. (The http and - * winhttp transports each implement both http and https.) */ - -/* Subtransports can either be RPC = 0 (persistent connection) or RPC = 1 - * (request/response). The smart transport handles the differences in its own - * logic. The git subtransport is RPC = 0, while http and winhttp are both - * RPC = 1. */ - -/* Actions that the smart transport can ask - * a subtransport to perform */ -typedef enum { - GIT_SERVICE_UPLOADPACK_LS = 1, - GIT_SERVICE_UPLOADPACK = 2, - GIT_SERVICE_RECEIVEPACK_LS = 3, - GIT_SERVICE_RECEIVEPACK = 4, -} git_smart_service_t; - -struct git_smart_subtransport; - -/* A stream used by the smart transport to read and write data - * from a subtransport */ -typedef struct git_smart_subtransport_stream { - /* The owning subtransport */ - struct git_smart_subtransport *subtransport; - - int (*read)( - struct git_smart_subtransport_stream *stream, - char *buffer, - size_t buf_size, - size_t *bytes_read); - - int (*write)( - struct git_smart_subtransport_stream *stream, - const char *buffer, - size_t len); - - void (*free)( - struct git_smart_subtransport_stream *stream); -} git_smart_subtransport_stream; - -/* An implementation of a subtransport which carries data for the - * smart transport */ -typedef struct git_smart_subtransport { - int (* action)( - git_smart_subtransport_stream **out, - struct git_smart_subtransport *transport, - const char *url, - git_smart_service_t action); - - /* Subtransports are guaranteed a call to close() between - * calls to action(), except for the following two "natural" progressions - * of actions against a constant URL. - * - * 1. UPLOADPACK_LS -> UPLOADPACK - * 2. RECEIVEPACK_LS -> RECEIVEPACK */ - int (* close)(struct git_smart_subtransport *transport); - - void (* free)(struct git_smart_subtransport *transport); -} git_smart_subtransport; - -/* A function which creates a new subtransport for the smart transport */ -typedef int (*git_smart_subtransport_cb)( - git_smart_subtransport **out, - git_transport* owner); - -typedef struct git_smart_subtransport_definition { - /* The function to use to create the git_smart_subtransport */ - git_smart_subtransport_cb callback; - - /* True if the protocol is stateless; false otherwise. For example, - * http:// is stateless, but git:// is not. */ - unsigned rpc : 1; -} git_smart_subtransport_definition; - -/* Smart transport subtransports that come with libgit2 */ - -/** - * Create an instance of the http subtransport. This subtransport - * also supports https. On Win32, this subtransport may be implemented - * using the WinHTTP library. - * - * @param out The newly created subtransport - * @param owner The smart transport to own this subtransport - * @return 0 or an error code - */ -GIT_EXTERN(int) git_smart_subtransport_http( - git_smart_subtransport **out, - git_transport* owner); - -/** - * Create an instance of the git subtransport. - * - * @param out The newly created subtransport - * @param owner The smart transport to own this subtransport - * @return 0 or an error code - */ -GIT_EXTERN(int) git_smart_subtransport_git( - git_smart_subtransport **out, - git_transport* owner); - -/* - *** End interface for subtransports for the smart transport *** + * @param out the transport generate + * @param owner the owner for the transport + * @param param the param to the transport creation + * @return 0 on success or an error code */ +typedef int GIT_CALLBACK(git_transport_cb)(git_transport **out, git_remote *owner, void *param); /** @} */ GIT_END_DECL + #endif diff --git a/include/git2/tree.h b/include/git2/tree.h index 73bfc86f42e..b8e2de217ed 100644 --- a/include/git2/tree.h +++ b/include/git2/tree.h @@ -14,8 +14,8 @@ /** * @file git2/tree.h - * @brief Git tree parsing, loading routines - * @defgroup git_tree Git tree parsing, loading routines + * @brief Trees are collections of files and folders to make up the repository hierarchy + * @defgroup git_tree Trees are collections of files and folders to make up the repository hierarchy * @ingroup Git * @{ */ @@ -24,16 +24,13 @@ GIT_BEGIN_DECL /** * Lookup a tree object from the repository. * - * @param out Pointer to the looked up tree + * @param[out] out Pointer to the looked up tree * @param repo The repo to use when locating the tree. * @param id Identity of the tree to locate. * @return 0 or an error code */ -GIT_INLINE(int) git_tree_lookup( - git_tree **out, git_repository *repo, const git_oid *id) -{ - return git_object_lookup((git_object **)out, repo, id, GIT_OBJ_TREE); -} +GIT_EXTERN(int) git_tree_lookup( + git_tree **out, git_repository *repo, const git_oid *id); /** * Lookup a tree object from the repository, @@ -41,21 +38,17 @@ GIT_INLINE(int) git_tree_lookup( * * @see git_object_lookup_prefix * - * @param tree pointer to the looked up tree + * @param out pointer to the looked up tree * @param repo the repo to use when locating the tree. * @param id identity of the tree to locate. * @param len the length of the short identifier * @return 0 or an error code */ -GIT_INLINE(int) git_tree_lookup_prefix( +GIT_EXTERN(int) git_tree_lookup_prefix( git_tree **out, git_repository *repo, const git_oid *id, - size_t len) -{ - return git_object_lookup_prefix( - (git_object **)out, repo, id, len, GIT_OBJ_TREE); -} + size_t len); /** * Close an open tree @@ -67,10 +60,7 @@ GIT_INLINE(int) git_tree_lookup_prefix( * * @param tree The tree to close */ -GIT_INLINE(void) git_tree_free(git_tree *tree) -{ - git_object_free((git_object *)tree); -} +GIT_EXTERN(void) git_tree_free(git_tree *tree); /** * Get the id of a tree. @@ -107,7 +97,7 @@ GIT_EXTERN(size_t) git_tree_entrycount(const git_tree *tree); * @return the tree entry; NULL if not found */ GIT_EXTERN(const git_tree_entry *) git_tree_entry_byname( - git_tree *tree, const char *filename); + const git_tree *tree, const char *filename); /** * Lookup a tree entry by its position in the tree @@ -120,7 +110,7 @@ GIT_EXTERN(const git_tree_entry *) git_tree_entry_byname( * @return the tree entry; NULL if not found */ GIT_EXTERN(const git_tree_entry *) git_tree_entry_byindex( - git_tree *tree, size_t idx); + const git_tree *tree, size_t idx); /** * Lookup a tree entry by SHA value. @@ -131,11 +121,11 @@ GIT_EXTERN(const git_tree_entry *) git_tree_entry_byindex( * Warning: this must examine every entry in the tree, so it is not fast. * * @param tree a previously loaded tree. - * @param oid the sha being looked for + * @param id the sha being looked for * @return the tree entry; NULL if not found */ -GIT_EXTERN(const git_tree_entry *) git_tree_entry_byoid( - const git_tree *tree, const git_oid *oid); +GIT_EXTERN(const git_tree_entry *) git_tree_entry_byid( + const git_tree *tree, const git_oid *id); /** * Retrieve a tree entry contained in a tree or in any of its subtrees, @@ -146,12 +136,12 @@ GIT_EXTERN(const git_tree_entry *) git_tree_entry_byoid( * * @param out Pointer where to store the tree entry * @param root Previously loaded tree which is the root of the relative path - * @param subtree_path Path to the contained entry + * @param path Path to the contained entry * @return 0 on success; GIT_ENOTFOUND if the path does not exist */ GIT_EXTERN(int) git_tree_entry_bypath( git_tree_entry **out, - git_tree *root, + const git_tree *root, const char *path); /** @@ -160,10 +150,11 @@ GIT_EXTERN(int) git_tree_entry_bypath( * Create a copy of a tree entry. The returned copy is owned by the user, * and must be freed explicitly with `git_tree_entry_free()`. * - * @param entry A tree entry to duplicate - * @return a copy of the original entry or NULL on error (alloc failure) + * @param dest pointer where to store the copy + * @param source tree entry to duplicate + * @return 0 or an error code */ -GIT_EXTERN(git_tree_entry *) git_tree_entry_dup(const git_tree_entry *entry); +GIT_EXTERN(int) git_tree_entry_dup(git_tree_entry **dest, const git_tree_entry *source); /** * Free a user-owned tree entry @@ -198,7 +189,7 @@ GIT_EXTERN(const git_oid *) git_tree_entry_id(const git_tree_entry *entry); * @param entry a tree entry * @return the type of the pointed object */ -GIT_EXTERN(git_otype) git_tree_entry_type(const git_tree_entry *entry); +GIT_EXTERN(git_object_t) git_tree_entry_type(const git_tree_entry *entry); /** * Get the UNIX file attributes of a tree entry @@ -208,6 +199,17 @@ GIT_EXTERN(git_otype) git_tree_entry_type(const git_tree_entry *entry); */ GIT_EXTERN(git_filemode_t) git_tree_entry_filemode(const git_tree_entry *entry); +/** + * Get the raw UNIX file attributes of a tree entry + * + * This function does not perform any normalization and is only useful + * if you need to be able to recreate the original tree object. + * + * @param entry a tree entry + * @return filemode as an integer + */ + +GIT_EXTERN(git_filemode_t) git_tree_entry_filemode_raw(const git_tree_entry *entry); /** * Compare two tree entries * @@ -218,11 +220,11 @@ GIT_EXTERN(git_filemode_t) git_tree_entry_filemode(const git_tree_entry *entry); GIT_EXTERN(int) git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2); /** - * Convert a tree entry to the git_object it points too. + * Convert a tree entry to the git_object it points to. * * You must call `git_object_free()` on the object when you are done with it. * - * @param object pointer to the converted object + * @param object_out pointer to the converted object * @param repo repository where to lookup the pointed object * @param entry a tree entry * @return 0 or an error code @@ -245,26 +247,28 @@ GIT_EXTERN(int) git_tree_entry_to_object( * entries and will have to be filled manually. * * @param out Pointer where to store the tree builder + * @param repo Repository in which to store the object * @param source Source tree to initialize the builder (optional) * @return 0 on success; error code otherwise */ -GIT_EXTERN(int) git_treebuilder_create( - git_treebuilder **out, const git_tree *source); +GIT_EXTERN(int) git_treebuilder_new( + git_treebuilder **out, git_repository *repo, const git_tree *source); /** - * Clear all the entires in the builder + * Clear all the entries in the builder * * @param bld Builder to clear + * @return 0 on success; error code otherwise */ -GIT_EXTERN(void) git_treebuilder_clear(git_treebuilder *bld); +GIT_EXTERN(int) git_treebuilder_clear(git_treebuilder *bld); /** * Get the number of entries listed in a treebuilder * - * @param tree a previously loaded treebuilder. + * @param bld a previously loaded treebuilder. * @return the number of entries in the treebuilder */ -GIT_EXTERN(unsigned int) git_treebuilder_entrycount(git_treebuilder *bld); +GIT_EXTERN(size_t) git_treebuilder_entrycount(git_treebuilder *bld); /** * Free a tree builder @@ -299,12 +303,15 @@ GIT_EXTERN(const git_tree_entry *) git_treebuilder_get( * If an entry named `filename` already exists, its attributes * will be updated with the given ones. * - * The optional pointer `out` can be used to retrieve a pointer to - * the newly created/updated entry. Pass NULL if you do not need it. + * The optional pointer `out` can be used to retrieve a pointer to the + * newly created/updated entry. Pass NULL if you do not need it. The + * pointer may not be valid past the next operation in this + * builder. Duplicate the entry if you want to keep it. * - * No attempt is being made to ensure that the provided oid points - * to an existing git object in the object database, nor that the - * attributes make sense regarding the type of the pointed at object. + * By default the entry that you are inserting will be checked for + * validity; that it exists in the object database and is of the + * correct type. If you do not want this behavior, set the + * `GIT_OPT_ENABLE_STRICT_OBJECT_CREATION` library option to false. * * @param out Pointer to store the entry (optional) * @param bld Tree builder @@ -327,15 +334,27 @@ GIT_EXTERN(int) git_treebuilder_insert( * * @param bld Tree builder * @param filename Filename of the entry to remove + * @return 0 or an error code */ GIT_EXTERN(int) git_treebuilder_remove( git_treebuilder *bld, const char *filename); -typedef int (*git_treebuilder_filter_cb)( +/** + * Callback for git_treebuilder_filter + * + * The return value is treated as a boolean, with zero indicating that the + * entry should be left alone and any non-zero value meaning that the + * entry should be removed from the treebuilder list (i.e. filtered out). + * + * @param entry the tree entry for the callback to examine + * @param payload the payload from the caller + * @return 0 to do nothing, non-zero to remove the entry + */ +typedef int GIT_CALLBACK(git_treebuilder_filter_cb)( const git_tree_entry *entry, void *payload); /** - * Filter the entries in the tree + * Selectively remove entries in the tree * * The `filter` callback will be called for each entry in the tree with a * pointer to the entry and the provided `payload`; if the callback returns @@ -343,9 +362,10 @@ typedef int (*git_treebuilder_filter_cb)( * * @param bld Tree builder * @param filter Callback to filter entries - * @param payload Extra data to pass to filter + * @param payload Extra data to pass to filter callback + * @return 0 on success, non-zero callback return value, or error code */ -GIT_EXTERN(void) git_treebuilder_filter( +GIT_EXTERN(int) git_treebuilder_filter( git_treebuilder *bld, git_treebuilder_filter_cb filter, void *payload); @@ -357,22 +377,27 @@ GIT_EXTERN(void) git_treebuilder_filter( * identifying SHA1 hash will be stored in the `id` pointer. * * @param id Pointer to store the OID of the newly written tree - * @param repo Repository in which to store the object * @param bld Tree builder to write * @return 0 or an error code */ GIT_EXTERN(int) git_treebuilder_write( - git_oid *id, git_repository *repo, git_treebuilder *bld); + git_oid *id, git_treebuilder *bld); - -/** Callback for the tree traversal method */ -typedef int (*git_treewalk_cb)( +/** + * Callback for the tree traversal method. + * + * @param root the current (relative) root to the entry + * @param entry the tree entry + * @param payload the caller-provided callback payload + * @return a positive value to skip the entry, a negative value to stop the walk + */ +typedef int GIT_CALLBACK(git_treewalk_cb)( const char *root, const git_tree_entry *entry, void *payload); /** Tree traversal modes */ typedef enum { GIT_TREEWALK_PRE = 0, /* Pre-order */ - GIT_TREEWALK_POST = 1, /* Post-order */ + GIT_TREEWALK_POST = 1 /* Post-order */ } git_treewalk_mode; /** @@ -398,7 +423,64 @@ GIT_EXTERN(int) git_tree_walk( git_treewalk_cb callback, void *payload); -/** @} */ +/** + * Create an in-memory copy of a tree. The copy must be explicitly + * free'd or it will leak. + * + * @param out Pointer to store the copy of the tree + * @param source Original tree to copy + * @return 0 + */ +GIT_EXTERN(int) git_tree_dup(git_tree **out, git_tree *source); + +/** + * The kind of update to perform + */ +typedef enum { + /** Update or insert an entry at the specified path */ + GIT_TREE_UPDATE_UPSERT, + /** Remove an entry from the specified path */ + GIT_TREE_UPDATE_REMOVE +} git_tree_update_t; + +/** + * An action to perform during the update of a tree + */ +typedef struct { + /** Update action. If it's an removal, only the path is looked at */ + git_tree_update_t action; + /** The entry's id */ + git_oid id; + /** The filemode/kind of object */ + git_filemode_t filemode; + /** The full path from the root tree */ + const char *path; +} git_tree_update; +/** + * Create a tree based on another one with the specified modifications + * + * Given the `baseline` perform the changes described in the list of + * `updates` and create a new tree. + * + * This function is optimized for common file/directory addition, removal and + * replacement in trees. It is much more efficient than reading the tree into a + * `git_index` and modifying that, but in exchange it is not as flexible. + * + * Deleting and adding the same entry is undefined behaviour, changing + * a tree to a blob or viceversa is not supported. + * + * @param out id of the new tree + * @param repo the repository in which to create the tree, must be the + * same as for `baseline` + * @param baseline the tree to base these changes on + * @param nupdates the number of elements in the update list + * @param updates the list of updates to perform + * @return 0 or an error code + */ +GIT_EXTERN(int) git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseline, size_t nupdates, const git_tree_update *updates); + +/** @} */ GIT_END_DECL + #endif diff --git a/include/git2/types.h b/include/git2/types.h index c16bb8872ea..fdccd9646d7 100644 --- a/include/git2/types.h +++ b/include/git2/types.h @@ -59,31 +59,38 @@ typedef __haiku_std_int64 git_time_t; * app, even though /we/ define _FILE_OFFSET_BITS=64. */ typedef int64_t git_off_t; -typedef int64_t git_time_t; +typedef int64_t git_time_t; /**< time in seconds from epoch */ #endif +/** The maximum size of an object */ +typedef uint64_t git_object_size_t; + +#include "buffer.h" +#include "oid.h" + /** Basic type (loose or packed) of any Git object. */ typedef enum { - GIT_OBJ_ANY = -2, /**< Object can be any of the following */ - GIT_OBJ_BAD = -1, /**< Object is invalid. */ - GIT_OBJ__EXT1 = 0, /**< Reserved for future use. */ - GIT_OBJ_COMMIT = 1, /**< A commit object. */ - GIT_OBJ_TREE = 2, /**< A tree (directory listing) object. */ - GIT_OBJ_BLOB = 3, /**< A file revision object. */ - GIT_OBJ_TAG = 4, /**< An annotated tag object. */ - GIT_OBJ__EXT2 = 5, /**< Reserved for future use. */ - GIT_OBJ_OFS_DELTA = 6, /**< A delta, base is given by an offset. */ - GIT_OBJ_REF_DELTA = 7, /**< A delta, base is given by object id. */ -} git_otype; - -/** An open object database handle. */ + GIT_OBJECT_ANY = -2, /**< Object can be any of the following */ + GIT_OBJECT_INVALID = -1, /**< Object is invalid. */ + GIT_OBJECT_COMMIT = 1, /**< A commit object. */ + GIT_OBJECT_TREE = 2, /**< A tree (directory listing) object. */ + GIT_OBJECT_BLOB = 3, /**< A file revision object. */ + GIT_OBJECT_TAG = 4 /**< An annotated tag object. */ +} git_object_t; + +/** + * An object database stores the objects (commit, trees, blobs, tags, + * etc) for a repository. + */ typedef struct git_odb git_odb; /** A custom backend in an ODB */ typedef struct git_odb_backend git_odb_backend; -/** An object read from the ODB */ +/** + * A "raw" object read from the object database. + */ typedef struct git_odb_object git_odb_object; /** A stream to read/write from the ODB */ @@ -92,12 +99,30 @@ typedef struct git_odb_stream git_odb_stream; /** A stream to write a packfile to the ODB */ typedef struct git_odb_writepack git_odb_writepack; +/** a writer for multi-pack-index files. */ +typedef struct git_midx_writer git_midx_writer; + +/** An open refs database handle. */ +typedef struct git_refdb git_refdb; + +/** A custom backend for refs */ +typedef struct git_refdb_backend git_refdb_backend; + +/** A git commit-graph */ +typedef struct git_commit_graph git_commit_graph; + +/** a writer for commit-graph files. */ +typedef struct git_commit_graph_writer git_commit_graph_writer; + /** * Representation of an existing git repository, * including all its object contents */ typedef struct git_repository git_repository; +/** Representation of a working tree */ +typedef struct git_worktree git_worktree; + /** Representation of a generic object in a repository */ typedef struct git_object git_object; @@ -125,6 +150,12 @@ typedef struct git_treebuilder git_treebuilder; /** Memory representation of an index file. */ typedef struct git_index git_index; +/** An iterator for entries in the index. */ +typedef struct git_index_iterator git_index_iterator; + +/** An iterator for conflicts in the index. */ +typedef struct git_index_conflict_iterator git_index_conflict_iterator; + /** Memory representation of a set of config files */ typedef struct git_config git_config; @@ -145,53 +176,209 @@ typedef struct git_packbuilder git_packbuilder; /** Time in a signature */ typedef struct git_time { - git_time_t time; /** time in seconds from epoch */ - int offset; /** timezone offset, in minutes */ + git_time_t time; /**< time in seconds from epoch */ + int offset; /**< timezone offset, in minutes */ + char sign; /**< indicator for questionable '-0000' offsets in signature */ } git_time; /** An action signature (e.g. for committers, taggers, etc) */ typedef struct git_signature { - char *name; /** full name of the author */ - char *email; /** email of the author */ - git_time when; /** time when the action happened */ + char *name; /**< full name of the author */ + char *email; /**< email of the author */ + git_time when; /**< time when the action happened */ } git_signature; /** In-memory representation of a reference. */ typedef struct git_reference git_reference; +/** Iterator for references */ +typedef struct git_reference_iterator git_reference_iterator; + +/** Transactional interface to references */ +typedef struct git_transaction git_transaction; + +/** + * Annotated commits are commits with additional metadata about how the + * commit was resolved, which can be used for maintaining the user's + * "intent" through commands like merge and rebase. + * + * For example, if a user wants to conceptually "merge `HEAD`", then the + * commit portion of an annotated commit will point to the `HEAD` commit, + * but the _annotation_ will denote the ref `HEAD`. This allows git to + * perform the internal bookkeeping so that the system knows both the + * content of what is being merged but also how the content was looked up + * so that it can be recorded in the reflog appropriately. + */ +typedef struct git_annotated_commit git_annotated_commit; + +/** Representation of a status collection */ +typedef struct git_status_list git_status_list; + +/** Representation of a rebase */ +typedef struct git_rebase git_rebase; + /** Basic type of any Git reference. */ typedef enum { - GIT_REF_INVALID = 0, /** Invalid reference */ - GIT_REF_OID = 1, /** A reference which points at an object id */ - GIT_REF_SYMBOLIC = 2, /** A reference which points at another reference */ - GIT_REF_PACKED = 4, - GIT_REF_HAS_PEEL = 8, - GIT_REF_LISTALL = GIT_REF_OID|GIT_REF_SYMBOLIC|GIT_REF_PACKED, -} git_ref_t; + GIT_REFERENCE_INVALID = 0, /**< Invalid reference */ + GIT_REFERENCE_DIRECT = 1, /**< A reference that points at an object id */ + GIT_REFERENCE_SYMBOLIC = 2, /**< A reference that points at another reference */ + GIT_REFERENCE_ALL = GIT_REFERENCE_DIRECT | GIT_REFERENCE_SYMBOLIC +} git_reference_t; /** Basic type of any Git branch. */ typedef enum { GIT_BRANCH_LOCAL = 1, GIT_BRANCH_REMOTE = 2, + GIT_BRANCH_ALL = GIT_BRANCH_LOCAL|GIT_BRANCH_REMOTE } git_branch_t; /** Valid modes for index and tree entries. */ typedef enum { - GIT_FILEMODE_NEW = 0000000, - GIT_FILEMODE_TREE = 0040000, - GIT_FILEMODE_BLOB = 0100644, - GIT_FILEMODE_BLOB_EXECUTABLE = 0100755, - GIT_FILEMODE_LINK = 0120000, - GIT_FILEMODE_COMMIT = 0160000, + GIT_FILEMODE_UNREADABLE = 0000000, + GIT_FILEMODE_TREE = 0040000, + GIT_FILEMODE_BLOB = 0100644, + GIT_FILEMODE_BLOB_EXECUTABLE = 0100755, + GIT_FILEMODE_LINK = 0120000, + GIT_FILEMODE_COMMIT = 0160000 } git_filemode_t; +/** + * A refspec specifies the mapping between remote and local reference + * names when fetch or pushing. + */ typedef struct git_refspec git_refspec; + +/** + * Git's idea of a remote repository. A remote can be anonymous (in + * which case it does not have backing configuration entries). + */ typedef struct git_remote git_remote; + +/** + * Interface which represents a transport to communicate with a + * remote. + */ +typedef struct git_transport git_transport; + +/** + * Preparation for a push operation. Can be used to configure what to + * push and the level of parallelism of the packfile builder. + */ typedef struct git_push git_push; +/* documentation in the definition */ typedef struct git_remote_head git_remote_head; typedef struct git_remote_callbacks git_remote_callbacks; +/** + * Parent type for `git_cert_hostkey` and `git_cert_x509`. + */ +typedef struct git_cert git_cert; + +/** + * Opaque structure representing a submodule. + */ +typedef struct git_submodule git_submodule; + +/** + * Submodule update values + * + * These values represent settings for the `submodule.$name.update` + * configuration value which says how to handle `git submodule update` for + * this submodule. The value is usually set in the ".gitmodules" file and + * copied to ".git/config" when the submodule is initialized. + * + * You can override this setting on a per-submodule basis with + * `git_submodule_set_update()` and write the changed value to disk using + * `git_submodule_save()`. If you have overwritten the value, you can + * revert it by passing `GIT_SUBMODULE_UPDATE_RESET` to the set function. + * + * The values are: + * + * - GIT_SUBMODULE_UPDATE_CHECKOUT: the default; when a submodule is + * updated, checkout the new detached HEAD to the submodule directory. + * - GIT_SUBMODULE_UPDATE_REBASE: update by rebasing the current checked + * out branch onto the commit from the superproject. + * - GIT_SUBMODULE_UPDATE_MERGE: update by merging the commit in the + * superproject into the current checkout out branch of the submodule. + * - GIT_SUBMODULE_UPDATE_NONE: do not update this submodule even when + * the commit in the superproject is updated. + * - GIT_SUBMODULE_UPDATE_DEFAULT: not used except as static initializer + * when we don't want any particular update rule to be specified. + */ +typedef enum { + GIT_SUBMODULE_UPDATE_CHECKOUT = 1, + GIT_SUBMODULE_UPDATE_REBASE = 2, + GIT_SUBMODULE_UPDATE_MERGE = 3, + GIT_SUBMODULE_UPDATE_NONE = 4, + + GIT_SUBMODULE_UPDATE_DEFAULT = 0 +} git_submodule_update_t; + +/** + * Submodule ignore values + * + * These values represent settings for the `submodule.$name.ignore` + * configuration value which says how deeply to look at the working + * directory when getting submodule status. + * + * You can override this value in memory on a per-submodule basis with + * `git_submodule_set_ignore()` and can write the changed value to disk + * with `git_submodule_save()`. If you have overwritten the value, you + * can revert to the on disk value by using `GIT_SUBMODULE_IGNORE_RESET`. + * + * The values are: + * + * - GIT_SUBMODULE_IGNORE_UNSPECIFIED: use the submodule's configuration + * - GIT_SUBMODULE_IGNORE_NONE: don't ignore any change - i.e. even an + * untracked file, will mark the submodule as dirty. Ignored files are + * still ignored, of course. + * - GIT_SUBMODULE_IGNORE_UNTRACKED: ignore untracked files; only changes + * to tracked files, or the index or the HEAD commit will matter. + * - GIT_SUBMODULE_IGNORE_DIRTY: ignore changes in the working directory, + * only considering changes if the HEAD of submodule has moved from the + * value in the superproject. + * - GIT_SUBMODULE_IGNORE_ALL: never check if the submodule is dirty + * - GIT_SUBMODULE_IGNORE_DEFAULT: not used except as static initializer + * when we don't want any particular ignore rule to be specified. + */ +typedef enum { + GIT_SUBMODULE_IGNORE_UNSPECIFIED = -1, /**< use the submodule's configuration */ + + GIT_SUBMODULE_IGNORE_NONE = 1, /**< any change or untracked == dirty */ + GIT_SUBMODULE_IGNORE_UNTRACKED = 2, /**< dirty if tracked files change */ + GIT_SUBMODULE_IGNORE_DIRTY = 3, /**< only dirty if HEAD moved */ + GIT_SUBMODULE_IGNORE_ALL = 4 /**< never dirty */ +} git_submodule_ignore_t; + +/** + * Options for submodule recurse. + * + * Represent the value of `submodule.$name.fetchRecurseSubmodules` + * + * * GIT_SUBMODULE_RECURSE_NO - do no recurse into submodules + * * GIT_SUBMODULE_RECURSE_YES - recurse into submodules + * * GIT_SUBMODULE_RECURSE_ONDEMAND - recurse into submodules only when + * commit not already in local clone + */ +typedef enum { + GIT_SUBMODULE_RECURSE_NO = 0, + GIT_SUBMODULE_RECURSE_YES = 1, + GIT_SUBMODULE_RECURSE_ONDEMAND = 2 +} git_submodule_recurse_t; + +typedef struct git_writestream git_writestream; + +/** A type to write in a streaming fashion, for example, for filters. */ +struct git_writestream { + int GIT_CALLBACK(write)(git_writestream *stream, const char *buffer, size_t len); + int GIT_CALLBACK(close)(git_writestream *stream); + void GIT_CALLBACK(free)(git_writestream *stream); +}; + +/** Representation of .mailmap file state. */ +typedef struct git_mailmap git_mailmap; + /** @} */ GIT_END_DECL diff --git a/include/git2/version.h b/include/git2/version.h index bc03e85d6c8..e543179981a 100644 --- a/include/git2/version.h +++ b/include/git2/version.h @@ -7,9 +7,71 @@ #ifndef INCLUDE_git_version_h__ #define INCLUDE_git_version_h__ -#define LIBGIT2_VERSION "0.17.0" -#define LIBGIT2_VER_MAJOR 0 -#define LIBGIT2_VER_MINOR 17 -#define LIBGIT2_VER_REVISION 0 +#include "common.h" + +/** + * @file git2/version.h + * @brief The version of libgit2 + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * The version string for libgit2. This string follows semantic + * versioning (v2) guidelines. + */ +#define LIBGIT2_VERSION "1.9.0" + +/** The major version number for this version of libgit2. */ +#define LIBGIT2_VERSION_MAJOR 1 + +/** The minor version number for this version of libgit2. */ +#define LIBGIT2_VERSION_MINOR 9 + +/** The revision ("teeny") version number for this version of libgit2. */ +#define LIBGIT2_VERSION_REVISION 0 + +/** The Windows DLL patch number for this version of libgit2. */ +#define LIBGIT2_VERSION_PATCH 0 + +/** + * The prerelease string for this version of libgit2. For development + * (nightly) builds, this will be "alpha". For prereleases, this will be + * a prerelease name like "beta" or "rc1". For final releases, this will + * be `NULL`. + */ +#define LIBGIT2_VERSION_PRERELEASE NULL + +/** + * The library ABI soversion for this version of libgit2. This should + * only be changed when the library has a breaking ABI change, and so + * may not reflect the library's API version number. + */ +#define LIBGIT2_SOVERSION "1.9" + +/** + * An integer value representing the libgit2 version number. For example, + * libgit2 1.6.3 is 1060300. + */ +#define LIBGIT2_VERSION_NUMBER ( \ + (LIBGIT2_VERSION_MAJOR * 1000000) + \ + (LIBGIT2_VERSION_MINOR * 10000) + \ + (LIBGIT2_VERSION_REVISION * 100)) + +/** + * Compare the libgit2 version against a given version. Evaluates to true + * if the given major, minor, and revision values are greater than or equal + * to the currently running libgit2 version. For example: + * + * #if LIBGIT2_VERSION_CHECK(1, 6, 3) + * # error libgit2 version is >= 1.6.3 + * #endif + */ +#define LIBGIT2_VERSION_CHECK(major, minor, revision) \ + (LIBGIT2_VERSION_NUMBER >= ((major)*1000000)+((minor)*10000)+((revision)*100)) + +/** @} */ +GIT_END_DECL #endif diff --git a/include/git2/worktree.h b/include/git2/worktree.h new file mode 100644 index 00000000000..fd3751753b4 --- /dev/null +++ b/include/git2/worktree.h @@ -0,0 +1,278 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_worktree_h__ +#define INCLUDE_git_worktree_h__ + +#include "common.h" +#include "buffer.h" +#include "types.h" +#include "strarray.h" +#include "checkout.h" + +/** + * @file git2/worktree.h + * @brief Additional working directories for a repository + * @defgroup git_commit Additional working directories for a repository + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * List names of linked working trees + * + * The returned list should be released with `git_strarray_free` + * when no longer needed. + * + * @param out pointer to the array of working tree names + * @param repo the repo to use when listing working trees + * @return 0 or an error code + */ +GIT_EXTERN(int) git_worktree_list(git_strarray *out, git_repository *repo); + +/** + * Lookup a working tree by its name for a given repository + * + * @param out Output pointer to looked up worktree or `NULL` + * @param repo The repository containing worktrees + * @param name Name of the working tree to look up + * @return 0 or an error code + */ +GIT_EXTERN(int) git_worktree_lookup(git_worktree **out, git_repository *repo, const char *name); + +/** + * Open a worktree of a given repository + * + * If a repository is not the main tree but a worktree, this + * function will look up the worktree inside the parent + * repository and create a new `git_worktree` structure. + * + * @param out Out-pointer for the newly allocated worktree + * @param repo Repository to look up worktree for + * @return 0 or an error code + */ +GIT_EXTERN(int) git_worktree_open_from_repository(git_worktree **out, git_repository *repo); + +/** + * Free a previously allocated worktree + * + * @param wt worktree handle to close. If NULL nothing occurs. + */ +GIT_EXTERN(void) git_worktree_free(git_worktree *wt); + +/** + * Check if worktree is valid + * + * A valid worktree requires both the git data structures inside + * the linked parent repository and the linked working copy to be + * present. + * + * @param wt Worktree to check + * @return 0 when worktree is valid, error-code otherwise + */ +GIT_EXTERN(int) git_worktree_validate(const git_worktree *wt); + +/** + * Worktree add options structure + * + * Initialize with `GIT_WORKTREE_ADD_OPTIONS_INIT`. Alternatively, you can + * use `git_worktree_add_options_init`. + * + */ +typedef struct git_worktree_add_options { + unsigned int version; + + int lock; /**< lock newly created worktree */ + int checkout_existing; /**< allow checkout of existing branch matching worktree name */ + git_reference *ref; /**< reference to use for the new worktree HEAD */ + + /** + * Options for the checkout. + */ + git_checkout_options checkout_options; +} git_worktree_add_options; + +/** Current version for the `git_worktree_add_options` structure */ +#define GIT_WORKTREE_ADD_OPTIONS_VERSION 1 + +/** Static constructor for `git_worktree_add_options` */ +#define GIT_WORKTREE_ADD_OPTIONS_INIT { GIT_WORKTREE_ADD_OPTIONS_VERSION, \ + 0, 0, NULL, GIT_CHECKOUT_OPTIONS_INIT } + +/** + * Initialize git_worktree_add_options structure + * + * Initializes a `git_worktree_add_options` with default values. Equivalent to + * creating an instance with `GIT_WORKTREE_ADD_OPTIONS_INIT`. + * + * @param opts The `git_worktree_add_options` struct to initialize. + * @param version The struct version; pass `GIT_WORKTREE_ADD_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_worktree_add_options_init(git_worktree_add_options *opts, + unsigned int version); + +/** + * Add a new working tree + * + * Add a new working tree for the repository, that is create the + * required data structures inside the repository and check out + * the current HEAD at `path` + * + * @param out Output pointer containing new working tree + * @param repo Repository to create working tree for + * @param name Name of the working tree + * @param path Path to create working tree at + * @param opts Options to modify default behavior. May be NULL + * @return 0 or an error code + */ +GIT_EXTERN(int) git_worktree_add(git_worktree **out, git_repository *repo, + const char *name, const char *path, + const git_worktree_add_options *opts); + +/** + * Lock worktree if not already locked + * + * Lock a worktree, optionally specifying a reason why the linked + * working tree is being locked. + * + * @param wt Worktree to lock + * @param reason Reason why the working tree is being locked + * @return 0 on success, non-zero otherwise + */ +GIT_EXTERN(int) git_worktree_lock(git_worktree *wt, const char *reason); + +/** + * Unlock a locked worktree + * + * @param wt Worktree to unlock + * @return 0 on success, 1 if worktree was not locked, error-code + * otherwise + */ +GIT_EXTERN(int) git_worktree_unlock(git_worktree *wt); + +/** + * Check if worktree is locked + * + * A worktree may be locked if the linked working tree is stored + * on a portable device which is not available. + * + * @param reason Buffer to store reason in. If NULL no reason is stored. + * @param wt Worktree to check + * @return 0 when the working tree not locked, a value greater + * than zero if it is locked, less than zero if there was an + * error + */ +GIT_EXTERN(int) git_worktree_is_locked(git_buf *reason, const git_worktree *wt); + +/** + * Retrieve the name of the worktree + * + * @param wt Worktree to get the name for + * @return The worktree's name. The pointer returned is valid for the + * lifetime of the git_worktree + */ +GIT_EXTERN(const char *) git_worktree_name(const git_worktree *wt); + +/** + * Retrieve the filesystem path for the worktree + * + * @param wt Worktree to get the path for + * @return The worktree's filesystem path. The pointer returned + * is valid for the lifetime of the git_worktree. + */ +GIT_EXTERN(const char *) git_worktree_path(const git_worktree *wt); + +/** + * Flags which can be passed to git_worktree_prune to alter its + * behavior. + */ +typedef enum { + /* Prune working tree even if working tree is valid */ + GIT_WORKTREE_PRUNE_VALID = 1u << 0, + /* Prune working tree even if it is locked */ + GIT_WORKTREE_PRUNE_LOCKED = 1u << 1, + /* Prune checked out working tree */ + GIT_WORKTREE_PRUNE_WORKING_TREE = 1u << 2 +} git_worktree_prune_t; + +/** + * Worktree prune options structure + * + * Initialize with `GIT_WORKTREE_PRUNE_OPTIONS_INIT`. Alternatively, you can + * use `git_worktree_prune_options_init`. + * + */ +typedef struct git_worktree_prune_options { + unsigned int version; + + /** A combination of `git_worktree_prune_t` */ + uint32_t flags; +} git_worktree_prune_options; + +/** Current version for the `git_worktree_prune_options` structure */ +#define GIT_WORKTREE_PRUNE_OPTIONS_VERSION 1 + +/** Static constructor for `git_worktree_prune_options` */ +#define GIT_WORKTREE_PRUNE_OPTIONS_INIT {GIT_WORKTREE_PRUNE_OPTIONS_VERSION,0} + +/** + * Initialize git_worktree_prune_options structure + * + * Initializes a `git_worktree_prune_options` with default values. Equivalent to + * creating an instance with `GIT_WORKTREE_PRUNE_OPTIONS_INIT`. + * + * @param opts The `git_worktree_prune_options` struct to initialize. + * @param version The struct version; pass `GIT_WORKTREE_PRUNE_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_worktree_prune_options_init( + git_worktree_prune_options *opts, + unsigned int version); + +/** + * Is the worktree prunable with the given options? + * + * A worktree is not prunable in the following scenarios: + * + * - the worktree is linking to a valid on-disk worktree. The + * `valid` member will cause this check to be ignored. + * - the worktree is locked. The `locked` flag will cause this + * check to be ignored. + * + * If the worktree is not valid and not locked or if the above + * flags have been passed in, this function will return a + * positive value. If the worktree is not prunable, an error + * message will be set (visible in `giterr_last`) with details about + * why. + * + * @param wt Worktree to check. + * @param opts The prunable options. + * @return 1 if the worktree is prunable, 0 otherwise, or an error code. + */ +GIT_EXTERN(int) git_worktree_is_prunable(git_worktree *wt, + git_worktree_prune_options *opts); + +/** + * Prune working tree + * + * Prune the working tree, that is remove the git data + * structures on disk. The repository will only be pruned of + * `git_worktree_is_prunable` succeeds. + * + * @param wt Worktree to prune + * @param opts Specifies which checks to override. See + * `git_worktree_is_prunable`. May be NULL + * @return 0 or an error code + */ +GIT_EXTERN(int) git_worktree_prune(git_worktree *wt, + git_worktree_prune_options *opts); + +/** @} */ +GIT_END_DECL + +#endif diff --git a/libgit2.pc.in b/libgit2.pc.in deleted file mode 100644 index 52ad901f7ef..00000000000 --- a/libgit2.pc.in +++ /dev/null @@ -1,9 +0,0 @@ -libdir=@CMAKE_INSTALL_PREFIX@/@LIB_INSTALL_DIR@ -includedir=@CMAKE_INSTALL_PREFIX@/@INCLUDE_INSTALL_DIR@ - -Name: libgit2 -Description: The git library, take 2 -Version: @LIBGIT2_VERSION_STRING@ -Requires: libcrypto -Libs: -L${libdir} -lgit2 -lz -lcrypto -Cflags: -I${includedir} diff --git a/libgit2_clar.supp b/libgit2_clar.supp deleted file mode 100644 index 5d20928afdd..00000000000 --- a/libgit2_clar.supp +++ /dev/null @@ -1,42 +0,0 @@ -{ - ignore-zlib-errors-cond - Memcheck:Cond - obj:*libz.so* -} - -{ - ignore-giterr-set-leak - Memcheck:Leak - ... - fun:giterr_set -} - -{ - ignore-git-global-state-leak - Memcheck:Leak - ... - fun:git__global_state -} - -{ - ignore-openssl-ssl-leak - Memcheck:Leak - ... - obj:*libssl.so* - ... -} - -{ - ignore-openssl-crypto-leak - Memcheck:Leak - ... - obj:*libcrypto.so* - ... -} - -{ - ignore-openssl-crypto-cond - Memcheck:Cond - obj:*libcrypto.so* - ... -} diff --git a/package.json b/package.json new file mode 100644 index 00000000000..2bea37ef30b --- /dev/null +++ b/package.json @@ -0,0 +1,7 @@ +{ + "name": "libgit2", + "version": "1.9.0", + "repo": "https://github.com/libgit2/libgit2", + "description": " A cross-platform, linkable library implementation of Git that you can use in your application.", + "install": "mkdir build && cd build && cmake .. && cmake --build ." +} diff --git a/packaging/rpm/README b/packaging/rpm/README deleted file mode 100644 index 1a6410b16c7..00000000000 --- a/packaging/rpm/README +++ /dev/null @@ -1,6 +0,0 @@ -To build RPM pakcages for Fedora, follow these steps: - cp packaging/rpm/libgit2.spec ~/rpmbuild/SPECS - cd ~/rpmbuild/SOURCES - wget https://github.com/downloads/libgit2/libgit2/libgit2-0.16.0.tar.gz - cd ~/rpmbuild/SPECS - rpmbuild -ba libgit2.spec diff --git a/packaging/rpm/libgit2.spec b/packaging/rpm/libgit2.spec deleted file mode 100644 index 80e70c1641d..00000000000 --- a/packaging/rpm/libgit2.spec +++ /dev/null @@ -1,106 +0,0 @@ -# -# spec file for package libgit2 -# -# Copyright (c) 2012 Saleem Ansari -# Copyright (c) 2012 SUSE LINUX Products GmbH, Nuernberg, Germany. -# Copyright (c) 2011, Sascha Peilicke -# -# All modifications and additions to the file contributed by third parties -# remain the property of their copyright owners, unless otherwise agreed -# upon. The license for this file, and modifications and additions to the -# file, is the same license as for the pristine package itself (unless the -# license for the pristine package is not an Open Source License, in which -# case the license is the MIT License). An "Open Source License" is a -# license that conforms to the Open Source Definition (Version 1.9) -# published by the Open Source Initiative. - -# Please submit bugfixes or comments via http://bugs.opensuse.org/ -# -Name: libgit2 -Version: 0.16.0 -Release: 1 -Summary: C git library -License: GPL-2.0 with linking -Group: Development/Libraries/C and C++ -Url: http://libgit2.github.com/ -Source0: https://github.com/downloads/libgit2/libgit2/libgit2-0.16.0.tar.gz -BuildRequires: cmake -BuildRequires: pkgconfig -BuildRoot: %{_tmppath}/%{name}-%{version}-build -%if 0%{?fedora} || 0%{?rhel_version} || 0%{?centos_version} -BuildRequires: openssl-devel -%else -BuildRequires: libopenssl-devel -%endif - -%description -libgit2 is a portable, pure C implementation of the Git core methods -provided as a re-entrant linkable library with a solid API, allowing -you to write native speed custom Git applications in any language -with bindings. - -%package -n %{name}-0 -Summary: C git library -Group: System/Libraries - -%description -n %{name}-0 -libgit2 is a portable, pure C implementation of the Git core methods -provided as a re-entrant linkable library with a solid API, allowing -you to write native speed custom Git applications in any language -with bindings. - -%package devel -Summary: C git library -Group: Development/Libraries/C and C++ -Requires: %{name}-0 >= %{version} - -%description devel -This package contains all necessary include files and libraries needed -to compile and develop applications that use libgit2. - -%prep -%setup -q - -%build -cmake . \ - -DCMAKE_C_FLAGS:STRING="%{optflags}" \ - -DCMAKE_INSTALL_PREFIX:PATH=%{_prefix} \ - -DLIB_INSTALL_DIR:PATH=%{_libdir}S -make %{?_smp_mflags} - -%install -%make_install - -%post -n %{name}-0 -p /sbin/ldconfig -%postun -n %{name}-0 -p /sbin/ldconfig - -%files -n %{name}-0 -%defattr (-,root,root) -%doc AUTHORS COPYING README.md -%{_libdir}/%{name}.so.* - -%files devel -%defattr (-,root,root) -%doc CONVENTIONS examples -%{_libdir}/%{name}.so -%{_includedir}/git2* -%{_libdir}/pkgconfig/libgit2.pc - -%changelog -* Tue Mar 04 2012 tuxdna@gmail.com -- Update to version 0.16.0 -* Tue Jan 31 2012 jengelh@medozas.de -- Provide pkgconfig symbols -* Thu Oct 27 2011 saschpe@suse.de -- Change license to 'GPL-2.0 with linking', fixes bnc#726789 -* Wed Oct 26 2011 saschpe@suse.de -- Update to version 0.15.0: - * Upstream doesn't provide changes -- Removed outdated %%clean section -* Tue Jan 18 2011 saschpe@gmx.de -- Proper Requires for devel package -* Tue Jan 18 2011 saschpe@gmx.de -- Set BuildRequires to "openssl-devel" also for RHEL and CentOS -* Tue Jan 18 2011 saschpe@gmx.de -- Initial commit (0.0.1) -- Added patch to fix shared library soname diff --git a/script/api-docs/.gitignore b/script/api-docs/.gitignore new file mode 100644 index 00000000000..c2658d7d1b3 --- /dev/null +++ b/script/api-docs/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/script/api-docs/README.md b/script/api-docs/README.md new file mode 100644 index 00000000000..fb329e2faa1 --- /dev/null +++ b/script/api-docs/README.md @@ -0,0 +1,13 @@ +# API Documentation Generator + +These scripts generate the "raw API" specs and reference documentation +for [www.libgit2.org](https://libgit2.org/docs/reference). + +The "raw API" specs consists of JSON documents, on per +released version or branch, that describes the APIs. This is +suitable for creating documentation from, or may be useful for +language bindings as well. + +The reference documentation is documentation fragments for each +API in each version, ready to be included in the libgit2 documentation +website. diff --git a/script/api-docs/api-generator.js b/script/api-docs/api-generator.js new file mode 100755 index 00000000000..47c928acf01 --- /dev/null +++ b/script/api-docs/api-generator.js @@ -0,0 +1,1558 @@ +#!/usr/bin/env node + +const path = require('node:path'); +const child_process = require('node:child_process'); +const fs = require('node:fs').promises; +const util = require('node:util'); +const process = require('node:process'); + +const { program } = require('commander'); + +const includePath = (p) => `${p}/include`; +const ancientIncludePath = (p) => `${p}/src/git`; +const legacyIncludePath = (p) => `${p}/src/git2`; +const standardIncludePath = (p) => `${includePath(p)}/git2`; +const systemIncludePath = (p) => `${includePath(p)}/git2/sys`; + +const fileIgnoreList = [ 'stdint.h', 'inttypes.h' ]; +const apiIgnoreList = [ 'GIT_BEGIN_DECL', 'GIT_END_DECL', 'GIT_WIN32' ]; + +// Some older versions of libgit2 need some help with includes +const defaultIncludes = [ + 'checkout.h', 'common.h', 'diff.h', 'email.h', 'oidarray.h', 'merge.h', 'remote.h', 'types.h' +]; + +// We're unable to fully map `types.h` defined types into groups; +// provide some help. +const groupMap = { + 'filemode': 'tree', + 'treebuilder': 'tree', + 'note': 'notes', + 'packbuilder': 'pack', + 'reference': 'refs', + 'push': 'remote' }; + +async function headerPaths(p) { + const possibleIncludePaths = [ + ancientIncludePath(p), + legacyIncludePath(p), + standardIncludePath(p), + systemIncludePath(p) + ]; + + const includePaths = [ ]; + const paths = [ ]; + + for (const possibleIncludePath of possibleIncludePaths) { + try { + await fs.stat(possibleIncludePath); + includePaths.push(possibleIncludePath); + } + catch (e) { + if (e?.code !== 'ENOENT') { + throw e; + } + } + } + + if (!includePaths.length) { + throw new Error(`no include paths for ${p}`); + } + + for (const fullPath of includePaths) { + paths.push(...(await fs.readdir(fullPath)). + filter((filename) => filename.endsWith('.h')). + filter((filename) => !fileIgnoreList.includes(filename)). + filter((filename) => filename !== 'deprecated.h' || !options.deprecateHard). + map((filename) => `${fullPath}/${filename}`)); + } + + return paths; +} + +function trimPath(basePath, headerPath) { + const possibleIncludePaths = [ + ancientIncludePath(basePath), + legacyIncludePath(basePath), + standardIncludePath(basePath), + systemIncludePath(basePath) + ]; + + for (const possibleIncludePath of possibleIncludePaths) { + if (headerPath.startsWith(possibleIncludePath + '/')) { + return headerPath.substr(possibleIncludePath.length + 1); + } + } + + throw new Error("header path is not beneath include root"); +} + +function parseFileAst(path, ast) { + let currentFile = undefined; + const fileData = [ ]; + + for (const node of ast.inner) { + if (node.loc?.file && currentFile != node.loc.file) { + currentFile = node.loc.file; + } else if (node.loc?.spellingLoc?.file && currentFile != node.loc.spellingLoc.file) { + currentFile = node.loc.spellingLoc.file; + } + + if (currentFile != path) { + continue; + } + + fileData.push(node); + } + + return fileData; +} + +function includeBase(path) { + const segments = path.split('/'); + + while (segments.length > 1) { + if (segments[segments.length - 1] === 'git2' || + segments[segments.length - 1] === 'git') { + segments.pop(); + return segments.join('/'); + } + + segments.pop(); + } + + throw new Error(`could not resolve include base for ${path}`); +} + +function readAst(path, options) { + return new Promise((resolve, reject) => { + let errorMessage = ''; + const chunks = [ ]; + + const processArgs = [ path, '-Xclang', '-ast-dump=json', `-I${includeBase(path)}` ]; + + if (options?.deprecateHard) { + processArgs.push(`-DGIT_DEPRECATE_HARD`); + } + + if (options?.includeFiles) { + for (const file of options.includeFiles) { + processArgs.push(`-include`); + processArgs.push(file) + } + } + + const process = child_process.spawn('clang', processArgs); + + process.stderr.on('data', (message) => { + errorMessage += message; + }); + process.stdout.on('data', (chunk) => { + chunks.push(chunk); + }); + process.on('close', (code) => { + if (code != 0 && options.strict) { + reject(new Error(`clang exit code ${code}: ${errorMessage}`)); + } + else if (code != 0) { + resolve([ ]); + } + else { + const ast = JSON.parse(Buffer.concat(chunks).toString()); + resolve(parseFileAst(path, ast)); + } + }); + process.on('error', function (err) { + reject(err); + }); + }); +} + +async function readFile(path) { + const buf = await fs.readFile(path); + return buf.toString(); +} + +function ensure(message, test) { + if (!test) { + throw new Error(message); + } +} + +function ensureDefined(name, value) { + if (!value) { + throw new Error(`could not find ${name} for declaration`); + } + + return value; +} + +function groupifyId(location, id) { + if (!id) { + throw new Error(`could not find id in declaration`); + } + + if (!location || !location.file) { + throw new Error(`unspecified location`); + } + + return `${location.file}-${id}`; +} + +function blockCommentText(block) { + ensure('block does not have a single paragraph element', block.inner.length === 1 && block.inner[0].kind === 'ParagraphComment'); + return commentText(block.inner[0]); +} + +function richBlockCommentText(block) { + ensure('block does not have a single paragraph element', block.inner.length === 1 && block.inner[0].kind === 'ParagraphComment'); + return richCommentText(block.inner[0]); +} + +function paramCommentText(param) { + ensure('param does not have a single paragraph element', param.inner.length === 1 && param.inner[0].kind === 'ParagraphComment'); + return richCommentText(param.inner[0]); +} + +function appendCommentText(chunk) { + return chunk.startsWith(' ') ? "\n" + chunk : chunk; +} + +function commentText(para) { + let text = ''; + + for (const comment of para.inner) { + // docbook allows backslash escaped text, and reports it differently. + // we restore the literal `\`. + if (comment.kind === 'InlineCommandComment') { + text += `\\${comment.name}`; + } + else if (comment.kind === 'TextComment') { + text += text ? "\n" + comment.text : comment.text; + } else { + throw new Error(`unknown paragraph comment element: ${comment.kind}`); + } + } + + return text.trim(); +} + +function nextText(para, idx) { + if (!para.inner[idx + 1] || para.inner[idx + 1].kind !== 'TextComment') { + throw new Error("expected text comment"); + } + + return para.inner[idx + 1].text; +} + +function inlineCommandData(data, command) { + ensure(`${command} information does not follow @${command}`, data?.kind === 'TextComment'); + + const result = data.text.match(/^(?:\[([^\]]+)\])? ((?:[a-zA-Z0-9\_]+)|`[a-zA-Z0-9\_\* ]+`)(.*)/); + ensure(`${command} data does not follow @${command}`, result); + + const [ , attr, spec, remain ] = result; + return [ attr, spec.replace(/^`(.*)`$/, "$1"), remain ] +} + +function richCommentText(para) { + let text = ''; + let extendedType = undefined; + let subkind = undefined; + let versionMacro = undefined; + let initMacro = undefined; + let initFunction = undefined; + let lastComment = undefined; + + for (let i = 0; i < para.inner?.length; i++) { + const comment = para.inner[i]; + + if (comment.kind === 'InlineCommandComment' && + comment.name === 'type') { + const [ attr, data, remain ] = inlineCommandData(para.inner[++i], "type"); + + extendedType = { kind: attr, type: data }; + text += remain; + } + else if (comment.kind === 'InlineCommandComment' && + comment.name === 'flags') { + subkind = 'flags'; + } + else if (comment.kind === 'InlineCommandComment' && + comment.name === 'options') { + const [ attr, data, remain ] = inlineCommandData(para.inner[++i], "options"); + + if (attr === 'version') { + versionMacro = data; + } + else if (attr === 'init_macro') { + initMacro = data; + } + else if (attr === 'init_function') { + initFunction = data; + } + + subkind = 'options'; + text += remain; + } + // docbook allows backslash escaped text, and reports it differently. + // we restore the literal `\`. + else if (comment.kind === 'InlineCommandComment') { + text += `\\${comment.name}`; + } + else if (comment.kind === 'TextComment') { + // clang oddity: it breaks into two + // comment blocks, assuming that the trailing > should be a + // blockquote newline sort of thing. unbreak them. + if (comment.text.startsWith('>') && + lastComment && + lastComment.loc.offset + lastComment.text.length === comment.loc.offset) { + + text += comment.text; + } else { + text += text ? "\n" + comment.text : comment.text; + } + } + else if (comment.kind === 'HTMLStartTagComment' && comment.name === 'p') { + text += "\n"; + } + else { + throw new Error(`unknown paragraph comment element: ${comment.kind}`); + } + + lastComment = comment; + } + + return { + text: text.trim(), + extendedType: extendedType, + subkind: subkind, + versionMacro: versionMacro, + initMacro: initMacro, + initFunction: initFunction + } +} + +function join(arr, elem) { + if (arr) { + return [ ...arr, elem ]; + } + + return [ elem ]; +} + +function joinIfNotEmpty(arr, elem) { + if (!elem || elem === '') { + return arr; + } + + if (arr) { + return [ ...arr, elem ]; + } + + return [ elem ]; +} + +function pushIfNotEmpty(arr, elem) { + if (elem && elem !== '') { + arr.push(elem); + } +} + +function single(arr, fn, message) { + let result = undefined; + + if (!arr) { + return undefined; + } + + for (const match of arr.filter(fn)) { + if (result) { + throw new Error(`multiple matches in array for ${fn}${message ? ' (' + message + ')': ''}`); + } + + result = match; + } + + return result; +} + +function updateLocation(location, decl) { + location.file = trimBase(decl.loc?.spellingLoc?.file || decl.loc?.file) || location.file; + location.line = decl.loc?.spellingLoc?.line || decl.loc?.line || location.line; + location.column = decl.loc?.spellingLoc?.col || decl.loc?.col || location.column; + + return location; +} + +async function readFileLocation(startLocation, endLocation) { + if (startLocation.file != endLocation.file) { + throw new Error("cannot read across files"); + } + + const data = await fs.readFile(startLocation.file, "utf8"); + const lines = data.split(/\r?\n/).slice(startLocation.line - 1, endLocation.line); + + lines[lines.length - 1] = lines[lines.length - 1].slice(0, endLocation.column); + lines[0] = lines[0].slice(startLocation.column - 1); + + return lines +} + +function formatLines(lines) { + let result = ""; + let continuation = false; + + for (const i in lines) { + if (!continuation) { + lines[i] = lines[i].trimStart(); + } + + continuation = lines[i].endsWith("\\"); + + if (continuation) { + lines[i] = lines[i].slice(0, -1); + } else { + lines[i] = lines[i].trimEnd(); + } + + result += lines[i]; + } + + if (continuation) { + throw new Error("unterminated literal continuation"); + } + + return result; +} + +async function parseExternalRange(location, range) { + const startLocation = {...location}; + startLocation.file = trimBase(range.begin.spellingLoc.file || startLocation.file); + startLocation.line = range.begin.spellingLoc.line || startLocation.line; + startLocation.column = range.begin.spellingLoc.col || startLocation.column; + + const endLocation = {...startLocation}; + endLocation.file = trimBase(range.end.spellingLoc.file || endLocation.file); + endLocation.line = range.end.spellingLoc.line || endLocation.line; + endLocation.column = range.end.spellingLoc.col || endLocation.column; + + const lines = await readFileLocation(startLocation, endLocation); + + return formatLines(lines); +} + +async function parseLiteralRange(location, range) { + const startLocation = updateLocation({...location}, { loc: range.begin }); + const endLocation = updateLocation({...location}, { loc: range.end }); + + const lines = await readFileLocation(startLocation, endLocation); + + return formatLines(lines); +} + +async function parseRange(location, range) { + return range.begin.spellingLoc ? parseExternalRange(location, range) : parseLiteralRange(location, range); +} + +class ParserError extends Error { + constructor(message, location) { + if (!location) { + super(`${message} at (unknown)`); + } + else { + super(`${message} at ${location.file}:${location.line}`); + } + this.name = 'ParserError'; + } +} + +function validateParsing(test, message, location) { + if (!test) { + throw new ParserError(message, location); + } +} + +function parseComment(spec, location, comment, options) { + let result = { }; + let last = undefined; + + for (const c of comment.inner.filter(c => c.kind === 'ParagraphComment' || c.kind === 'VerbatimLineComment')) { + if (c.kind === 'ParagraphComment') { + const commentData = richCommentText(c); + + result.comment = joinIfNotEmpty(result.comment, commentData.text); + delete commentData.text; + + result = { ...result, ...commentData }; + } + else if (c.kind === 'VerbatimLineComment') { + result.comment = joinIfNotEmpty(result.comment, c.text.trim()); + } + else { + throw new Error(`unknown comment ${c.kind}`); + } + } + + for (const c of comment.inner.filter(c => c.kind !== 'ParagraphComment' && c.kind !== 'VerbatimLineComment')) { + if (c.kind === 'BlockCommandComment' && c.name === 'see') { + result.see = joinIfNotEmpty(result.see, blockCommentText(c)); + } + else if (c.kind === 'BlockCommandComment' && c.name === 'note') { + result.notes = joinIfNotEmpty(result.notes, blockCommentText(c)); + } + else if (c.kind === 'BlockCommandComment' && c.name === 'deprecated') { + result.deprecations = joinIfNotEmpty(result.deprecations, blockCommentText(c)); + } + else if (c.kind === 'BlockCommandComment' && c.name === 'warning') { + result.warnings = joinIfNotEmpty(result.warnings, blockCommentText(c)); + } + else if (c.kind === 'BlockCommandComment' && + (c.name === 'return' || (c.name === 'returns' && !options.strict))) { + const returnData = richBlockCommentText(c); + + result.returns = { + extendedType: returnData.extendedType, + comment: returnData.text + }; + } + else if (c.kind === 'ParamCommandComment') { + ensure('param has a name', c.param); + + const paramDetails = paramCommentText(c); + + result.params = join(result.params, { + name: c.param, + direction: c.direction, + values: paramDetails.type, + extendedType: paramDetails.extendedType, + comment: paramDetails.text + }); + } + else if (options.strict) { + if (c.kind === 'BlockCommandComment') { + throw new ParserError(`unknown block command comment ${c.name}`, location); + } + else if (c.kind === 'VerbatimBlockComment') { + throw new Error(`unknown verbatim command comment ${c.name}`, location); + } + else { + throw new Error(`unknown comment ${c.kind} in ${kind}`); + } + } + } + + return result; +} + +async function parseFunction(location, decl, options) { + let result = { + kind: 'function', + id: groupifyId(location, decl.id), + name: ensureDefined('name', decl.name), + location: {...location} + }; + + // prototype + const [ , returnType, ] = decl.type.qualType.match(/(.*?)(?: )?\((.*)\)$/) || [ ]; + ensureDefined('return type declaration', returnType); + result.returns = { type: returnType }; + + for (const paramDecl of decl.inner.filter(attr => attr.kind === 'ParmVarDecl')) { + updateLocation(location, paramDecl); + + const inner = paramDecl.inner || []; + const innerLocation = {...location}; + let paramAnnotations = undefined; + + for (const annotateDecl of inner.filter(attr => attr.kind === 'AnnotateAttr')) { + updateLocation(innerLocation, annotateDecl); + + paramAnnotations = join(paramAnnotations, await parseRange(innerLocation, annotateDecl.range)); + } + + result.params = join(result.params, { + name: paramDecl.name, + type: paramDecl.type.qualType, + annotations: paramAnnotations + }); + } + + // doc comment + const commentText = single(decl.inner, (attr => attr.kind === 'FullComment')); + + if (commentText) { + const commentData = parseComment(`function:${decl.name}`, location, commentText, options); + + if (result.params) { + if (options.strict && (!commentData.params || result.params.length > commentData.params.length)) { + throw new ParserError(`not all params are documented`, location); + } + + if (options.strict && result.params.length < commentData.params.length) { + throw new ParserError(`additional params are documented`, location); + } + } + + if (commentData.params) { + for (const i in result.params) { + let match; + + for (const j in commentData.params) { + if (result.params[i].name === commentData.params[j].name) { + match = j; + break; + } + } + + if (options.strict && (!match || match != i)) { + throw new ParserError( + `param documentation does not match param name '${result.params[i].name}'`, + location); + } + + if (match) { + result.params[i] = { ...result.params[i], ...commentData.params[match] }; + } + } + } else if (options.strict && result.params) { + throw new ParserError(`no params documented for ${decl.name}`, location); + } + + if (options.strict && !commentData.returns && result.returns.type != 'void') { + throw new ParserError(`return information is not documented for ${decl.name}`, location); + } + + result.returns = { ...result.returns, ...commentData.returns }; + + delete commentData.params; + delete commentData.returns; + + result = { ...result, ...commentData }; + } + else if (options.strict) { + throw new ParserError(`no documentation for function ${decl.name}`, location); + } + + return result; +} + +function parseEnum(location, decl, options) { + let result = { + kind: 'enum', + id: groupifyId(location, decl.id), + name: decl.name, + referenceName: decl.name ? `enum ${decl.name}` : undefined, + members: [ ], + comment: undefined, + location: {...location} + }; + + for (const member of decl.inner.filter(attr => attr.kind === 'EnumConstantDecl')) { + ensure('enum constant has a name', member.name); + + const explicitValue = single(member.inner, (attr => attr.kind === 'ConstantExpr')); + const implicitValue = single(member.inner, (attr => attr.kind === 'ImplicitCastExpr')); + const commentText = single(member.inner, (attr => attr.kind === 'FullComment')); + const commentData = commentText ? parseComment(`enum:${decl.name}:member:${member.name}`, location, commentText, options) : undefined; + + let value = undefined; + + if (explicitValue && explicitValue.value) { + value = explicitValue.value; + } else if (implicitValue) { + const innerExplicit = single(implicitValue.inner, (attr => attr.kind === 'ConstantExpr')); + + value = innerExplicit?.value; + } + + result.members.push({ + name: member.name, + value: value, + ...commentData + }); + } + + const commentText = single(decl.inner, (attr => attr.kind === 'FullComment')); + + if (commentText) { + result = { ...result, ...parseComment(`enum:${decl.name}`, location, commentText, options) }; + } + + return result; +} + +function resolveFunctionPointerTypedef(location, typedef) { + const signature = typedef.type.match(/^((?:const )?[^\s]+(?:\s+\*+)?)\s*\(\*\)\((.*)\)$/); + const [ , returnType, paramData ] = signature; + const params = paramData.split(/,\s+/); + + if (options.strict && (!typedef.params || params.length != typedef.params.length)) { + throw new ParserError(`not all params are documented for function pointer typedef ${typedef.name}`, typedef.location); + } + + if (!typedef.params) { + typedef.params = [ ]; + } + + for (const i in params) { + if (!typedef.params[i]) { + typedef.params[i] = { }; + } + + typedef.params[i].type = params[i]; + } + + if (typedef.returns === undefined && returnType === 'void') { + typedef.returns = { type: 'void' }; + } + else if (typedef.returns !== undefined) { + typedef.returns.type = returnType; + } + else if (options.strict) { + throw new ParserError(`return type is not documented for function pointer typedef ${typedef.name}`, typedef.location); + } +} + +function parseTypedef(location, decl, options) { + updateLocation(location, decl); + + let result = { + kind: 'typedef', + id: groupifyId(location, decl.id), + name: ensureDefined('name', decl.name), + type: ensureDefined('type.qualType', decl.type.qualType), + targetId: undefined, + comment: undefined, + location: {...location} + }; + + const elaborated = single(decl.inner, (attr => attr.kind === 'ElaboratedType')); + if (elaborated !== undefined && elaborated.ownedTagDecl?.id) { + result.targetId = groupifyId(location, elaborated.ownedTagDecl?.id); + } + + const commentText = single(decl.inner, (attr => attr.kind === 'FullComment')); + + if (commentText) { + const commentData = parseComment(`typedef:${decl.name}`, location, commentText, options); + result = { ...result, ...commentData }; + } + + if (isFunctionPointer(result.type)) { + resolveFunctionPointerTypedef(location, result); + } + + return result; +} + +function parseStruct(location, decl, options) { + let result = { + kind: 'struct', + id: groupifyId(location, decl.id), + name: decl.name, + referenceName: decl.name ? `struct ${decl.name}` : undefined, + comment: undefined, + members: [ ], + location: {...location} + }; + + for (const member of decl.inner.filter(attr => attr.kind === 'FieldDecl')) { + let memberData = { + 'name': member.name, + 'type': member.type.qualType + }; + + const commentText = single(member.inner, (attr => attr.kind === 'FullComment')); + + if (commentText) { + memberData = {...memberData, ...parseComment(`struct:${decl.name}:member:${member.name}`, location, commentText, options)}; + } + + result.members.push(memberData); + } + + const commentText = single(decl.inner, (attr => attr.kind === 'FullComment')); + + if (commentText) { + const commentData = parseComment(`struct:${decl.name}`, location, commentText, options); + result = { ...result, ...commentData }; + } + + return result; +} + +function newResults() { + return { + all: [ ], + functions: [ ], + enums: [ ], + typedefs: [ ], + structs: [ ], + macros: [ ] + }; +}; + +const returnMap = { }; +const paramMap = { }; + +function simplifyType(givenType) { + let type = givenType; + + if (type.startsWith('const ')) { + type = type.substring(6); + } + + while (type.endsWith('*') && type !== 'void *' && type !== 'char *') { + type = type.substring(0, type.length - 1).trim(); + } + + if (!type.length) { + throw new Error(`invalid type: ${result.returns.extendedType || result.returns.type}`); + } + + return type; +} + +function createAndPush(arr, name, value) { + if (!arr[name]) { + arr[name] = [ ]; + } + + if (arr[name].length && arr[name][arr[name].length - 1] === value) { + return; + } + + arr[name].push(value); +} + +function addReturn(result) { + if (!result.returns) { + return; + } + + let type = simplifyType(result.returns.extendedType?.type || result.returns.type); + + createAndPush(returnMap, type, result.name); +} + +function addParameters(result) { + if (!result.params) { + return; + } + + for (const param of result.params) { + let type = param.extendedType?.type || param.type; + + if (!type && options.strict) { + throw new Error(`parameter ${result.name} erroneously documented when not specified`); + } else if (!type) { + continue; + } + + type = simplifyType(type); + + if (param.direction === 'out') { + createAndPush(returnMap, type, result.name); + } + else { + createAndPush(paramMap, type, result.name); + } + } +} + +function addResult(results, result) { + results[`${result.kind}s`].push(result); + results.all.push(result); + + addReturn(result); + addParameters(result); +} + +function mergeResults(one, two) { + const results = newResults(); + + for (const inst of Object.keys(results)) { + results[inst].push(...one[inst]); + results[inst].push(...two[inst]); + } + + return results; +} + +function getById(results, id) { + ensure("id is set", id !== undefined); + return single(results.all.all, (item => item.id === id), id); +} + +function getByKindAndName(results, kind, name) { + ensure("kind is set", kind !== undefined); + ensure("name is set", name !== undefined); + return single(results.all[`${kind}s`], (item => item.name === name), name); +} + +function getByName(results, name) { + ensure("name is set", name !== undefined); + return single(results.all.all, (item => item.name === name), name); +} + +function isFunctionPointer(type) { + return type.match(/^(?:const )?[A-Za-z0-9_]+\s+\**\(\*/); +} + +function resolveCallbacks(results) { + // expand callback types + for (const fn of results.all.functions) { + for (const param of fn.params || [ ]) { + const typedef = getByName(results, param.type); + + if (typedef === undefined) { + continue; + } + + param.referenceType = typedef.type; + } + } + + for (const struct of results.all.structs) { + for (const member of struct.members) { + const typedef = getByKindAndName(results, 'typedef', member.type); + + if (typedef === undefined) { + continue; + } + + member.referenceType = typedef.type; + } + } +} + +function trimBase(path) { + if (!path) { + return path; + } + + for (const segment of [ 'git2', 'git' ]) { + const base = [ includeBase(path), segment ].join('/'); + + if (path.startsWith(base + '/')) { + return path.substr(base.length + 1); + } + } + + throw new Error(`header path ${path} is not beneath standard root`); +} + +function resolveTypedefs(results) { + for (const typedef of results.all.typedefs) { + let target = typedef.targetId ? getById(results, typedef.targetId) : undefined; + + if (target) { + // update the target's preferred name with the short name + target.referenceName = typedef.name; + + if (target.name === undefined) { + target.name = typedef.name; + } + } + else if (typedef.type.startsWith('struct ')) { + const path = typedef.location.file; + + /* + * See if this is actually a typedef to a declared struct, + * then it is not actually opaque. + */ + if (results.all.structs.filter(fn => fn.name === typedef.name).length > 0) { + typedef.opaque = false; + continue; + } + + opaque = { + kind: 'struct', + id: groupifyId(typedef.location, typedef.id), + name: typedef.name, + referenceName: typedef.type, + opaque: true, + comment: typedef.comment, + location: typedef.location, + group: typedef.group + }; + + addResult(results.files[path], opaque); + addResult(results.all, opaque); + } + else if (isFunctionPointer(typedef.type) || + typedef.type === 'int64_t' || + typedef.type === 'uint64_t') { + // standard types + // TODO : make these a list + } + else { + typedef.kind = 'alias'; + typedef.typedef = true; + } + } +} + +function lastCommentIsGroupDelimiter(decls) { + if (decls[decls.length - 1].inner && + decls[decls.length - 1].inner.length > 0) { + return lastCommentIsGroupDelimiter(decls[decls.length - 1].inner); + } + + if (decls.length >= 2 && + decls[decls.length - 1].kind.endsWith('Comment') && + decls[decls.length - 2].kind.endsWith('Comment') && + decls[decls.length - 2].text === '@' && + decls[decls.length - 1].text === '{') { + return true; + } + + return false; +} + +async function parseAst(decls, options) { + const location = { + file: undefined, + line: undefined, + column: undefined + }; + + const results = newResults(); + + /* The first decl might have picked up the javadoc _for the file + * itself_ based on the file's structure. Remove it. + */ + if (decls.length && decls[0].inner && + decls[0].inner.length > 0 && + decls[0].inner[0].kind === 'FullComment' && + lastCommentIsGroupDelimiter(decls[0].inner[0].inner)) { + updateLocation(location, decls[0]); + delete decls[0].inner[0]; + } + + for (const decl of decls) { + updateLocation(location, decl); + + ensureDefined('kind', decl.kind); + + if (decl.kind === 'FunctionDecl') { + addResult(results, await parseFunction({...location}, decl, options)); + } + else if (decl.kind === 'EnumDecl') { + addResult(results, parseEnum({...location}, decl, options)); + } + else if (decl.kind === 'TypedefDecl') { + addResult(results, parseTypedef({...location}, decl, options)); + } + else if (decl.kind === 'RecordDecl' && decl.tagUsed === 'struct') { + if (decl.completeDefinition) { + addResult(results, parseStruct({...location}, decl, options)); + } + } + else if (decl.kind === 'VarDecl') { + if (options.strict) { + throw new Error(`unsupported variable declaration ${decl.kind}`); + } + } + else { + throw new Error(`unknown declaration type ${decl.kind}`); + } + } + + return results; +} + +function parseCommentForMacro(lines, macroIdx, name) { + let startIdx = -1, endIdx = 0; + const commentLines = [ ]; + + while (macroIdx > 0 && + (line = lines[macroIdx - 1].trim()) && + (line.trim() === '' || + line.trim().endsWith('\\') || + line.trim().match(/^#\s*if\s+/) || + line.trim().startsWith('#ifdef ') || + line.trim().startsWith('#ifndef ') || + line.trim().startsWith('#elif ') || + line.trim().startsWith('#else ') || + line.trim().match(/^#\s*define\s+${name}\s+/))) { + macroIdx--; + } + + if (macroIdx > 0 && lines[macroIdx - 1].trim().endsWith('*/')) { + endIdx = macroIdx - 1; + } else { + return ''; + } + + for (let i = endIdx; i >= 0; i--) { + if (lines[i].trim().startsWith('/**')) { + startIdx = i; + break; + } + else if (lines[i].trim().startsWith('/*')) { + break; + } + } + + if (startIdx < 0) { + return ''; + } + + for (let i = startIdx; i <= endIdx; i++) { + let line = lines[i].trim(); + + if (i == startIdx) { + line = line.replace(/^\s*\/\*\*\s*/, ''); + } + + if (i === endIdx) { + line = line.replace(/\s*\*\/\s*$/, ''); + } + + if (i != startIdx) { + line = line.replace(/^\s*\*\s*/, ''); + } + + if (i == startIdx && (line === '@{' || line.startsWith("@{ "))) { + return ''; + } + + if (line === '') { + continue; + } + + commentLines.push(line); + } + + return commentLines.join(' '); +} + +async function parseInfo(data) { + const fileHeader = data.match(/(.*)\n+GIT_BEGIN_DECL.*/s); + const headerLines = fileHeader ? fileHeader[1].split(/\n/) : [ ]; + + let lines = [ ]; + const detailsLines = [ ]; + + let summary = undefined; + let endIdx = headerLines.length - 1; + + for (let i = headerLines.length - 1; i >= 0; i--) { + let line = headerLines[i].trim(); + + if (line.match(/^\s*\*\/\s*$/)) { + endIdx = i; + } + + if (line.match(/^\/\*\*(\s+.*)?$/)) { + lines = headerLines.slice(i + 1, endIdx); + break; + } + else if (line.match(/^\/\*(\s+.*)?$/)) { + break; + } + } + + for (let line of lines) { + line = line.replace(/^\s\*/, ''); + line = line.trim(); + + const comment = line.match(/^\@(\w+|{)\s*(.*)/); + + if (comment) { + if (comment[1] === 'brief') { + summary = comment[2]; + } + } + else if (line != '') { + detailsLines.push(line); + } + } + + const details = detailsLines.length > 0 ? detailsLines.join("\n") : undefined; + + return { + 'summary': summary, + 'details': details + }; +} + +async function parseMacros(path, data, options) { + const results = newResults(); + const lines = data.split(/\r?\n/); + + const macros = { }; + + for (let i = 0; i < lines.length; i++) { + const macro = lines[i].match(/^(\s*#\s*define\s+)([^\s\(]+)(\([^\)]+\))?\s*(.*)/); + let more = false; + + if (!macro) { + continue; + } + + let [ , prefix, name, args, value ] = macro; + + if (name.startsWith('INCLUDE_') || name.startsWith('_INCLUDE_')) { + continue; + } + + if (args) { + name = name + args; + } + + if (macros[name]) { + continue; + } + + macros[name] = true; + + value = value.trim(); + + if (value.endsWith('\\')) { + value = value.substring(0, value.length - 1).trim(); + more = true; + } + + while (more) { + more = false; + + let line = lines[++i]; + + if (line.endsWith('\\')) { + line = line.substring(0, line.length - 1); + more = true; + } + + value += ' ' + line.trim(); + } + + const comment = parseCommentForMacro(lines, i, name); + const location = { + file: path, + line: i + 1, + column: prefix.length + 1, + }; + + if (options.strict && !comment) { + throw new ParserError(`no comment for ${name}`, location); + } + + addResult(results, { + kind: 'macro', + name: name, + location: location, + value: value, + comment: comment, + }); + } + + return results; +} + +function resolveUngroupedTypes(results) { + const groups = { }; + + for (const result of results.all.all) { + result.group = result.location.file; + + if (result.group.endsWith('.h')) { + result.group = result.group.substring(0, result.group.length - 2); + groups[result.group] = true; + } + } + + for (const result of results.all.all) { + if (result.location.file === 'types.h' && + result.name.startsWith('git_')) { + let possibleGroup = result.name.substring(4); + + do { + if (groupMap[possibleGroup]) { + result.group = groupMap[possibleGroup]; + break; + } + else if (groups[possibleGroup]) { + result.group = possibleGroup; + break; + } + else if (groups[`sys/${possibleGroup}`]) { + result.group = `sys/${possibleGroup}`; + break; + } + + let match = possibleGroup.match(/^(.*)_[^_]+$/); + + if (!match) { + break; + } + + possibleGroup = match[1]; + } while (true); + } + } +} + +function resolveReturns(results) { + for (const result of results.all.all) { + result.returnedBy = returnMap[result.name]; + } +} + +function resolveParameters(results) { + for (const result of results.all.all) { + result.parameterTo = paramMap[result.name]; + } +} + +async function parseHeaders(sourcePath, options) { + const results = { all: newResults(), files: { } }; + + for (const fullPath of await headerPaths(sourcePath)) { + const path = trimPath(sourcePath, fullPath); + const fileContents = await readFile(fullPath); + + const ast = await parseAst(await readAst(fullPath, options), options); + const macros = await parseMacros(path, fileContents, options); + const info = await parseInfo(fileContents); + + const filedata = mergeResults(ast, macros); + + filedata['info'] = info; + + results.files[path] = filedata; + results.all = mergeResults(results.all, filedata); + } + + resolveCallbacks(results); + resolveTypedefs(results); + + resolveUngroupedTypes(results); + + resolveReturns(results); + resolveParameters(results); + + return results; +} + +function isFunctionPointer(type) { + return type.match(/^(const\s+)?[A-Za-z0-9_]+\s+\*?\(\*/); +} +function isEnum(type) { + return type.match(/^enum\s+/); +} +function isStruct(type) { + return type.match(/^struct\s+/); +} + +/* + * We keep the `all` arrays around so that we can lookup; drop them + * for the end result. + */ +function simplify(results) { + const simplified = { + 'info': { }, + 'groups': { } + }; + + results.all.all.sort((a, b) => { + if (!a.group) { + throw new Error(`missing group for api ${a.name}`); + } + + if (!b.group) { + throw new Error(`missing group for api ${b.name}`); + } + + const aSystem = a.group.startsWith('sys/'); + const aName = aSystem ? a.group.substr(4) : a.group; + + const bSystem = b.group.startsWith('sys/'); + const bName = bSystem ? b.group.substr(4) : b.group; + + if (aName !== bName) { + return aName.localeCompare(bName); + } + + if (aSystem !== bSystem) { + return aSystem ? 1 : -1; + } + + if (a.location.file !== b.location.file) { + return a.location.file.localeCompare(b.location.file); + } + + if (a.location.line !== b.location.line) { + return a.location.line - b.location.line; + } + + return a.location.column - b.location.column; + }); + + for (const api of results.all.all) { + delete api.id; + delete api.targetId; + + const type = api.referenceType || api.type; + + if (api.kind === 'typedef' && isFunctionPointer(type)) { + api.kind = 'callback'; + api.typedef = true; + } + else if (api.kind === 'typedef' && (!isEnum(type) && !isStruct(type))) { + api.kind = 'alias'; + api.typedef = true; + } + else if (api.kind === 'typedef') { + continue; + } + + if (apiIgnoreList.includes(api.name)) { + continue; + } + + // TODO: do a warning where there's a redefinition of a symbol + // There are occasions where we redefine a symbol. First, our + // parser is not smart enough to know #ifdef's around #define's. + // But also we declared `git_email_create_from_diff` twice (in + // email.h and sys/email.h) for several releases. + + if (!simplified['groups'][api.group]) { + simplified['groups'][api.group] = { }; + simplified['groups'][api.group].apis = { }; + simplified['groups'][api.group].info = results.files[`${api.group}.h`].info; + } + + simplified['groups'][api.group].apis[api.name] = api; + } + + return simplified; +} + +function joinArguments(next, previous) { + if (previous) { + return [...previous, next]; + } + return [next]; +} + +async function findIncludes() { + const includes = [ ]; + + for (const possible of defaultIncludes) { + const includeFile = `${docsPath}/include/git2/${possible}`; + + try { + await fs.stat(includeFile); + includes.push(`git2/${possible}`); + } + catch (e) { + if (e?.code !== 'ENOENT') { + throw e; + } + } + } + + return includes; +} + +async function execGit(path, command) { + const process = child_process.spawn('git', command, { cwd: path }); + const chunks = [ ]; + + return new Promise((resolve, reject) => { + process.stdout.on('data', (chunk) => { + chunks.push(chunk); + }); + process.on('close', (code) => { + resolve(code == 0 ? Buffer.concat(chunks).toString() : undefined); + }); + process.on('error', function (err) { + reject(err); + }); + }); +} + +async function readMetadata(path) { + let commit = await execGit(path, [ 'rev-parse', 'HEAD' ]); + + if (commit) { + commit = commit.trimEnd(); + } + + let version = await execGit(path, [ 'describe', '--tags', '--exact' ]); + + if (!version) { + const ref = await execGit(path, [ 'describe', '--all', '--exact' ]); + + if (ref && ref.startsWith('heads/')) { + version = ref.substr(6); + } + } + + if (version) { + version = version.trimEnd(); + } + + return { + 'version': version, + 'commit': commit + }; +} + +program.option('--output ') + .option('--include ', undefined, joinArguments) + .option('--no-includes') + .option('--deprecate-hard') + .option('--validate-only') + .option('--strict'); +program.parse(); + +const options = program.opts(); + +if (program.args.length != 1) { + console.error(`usage: ${path.basename(process.argv[1])} docs`); + process.exit(1); +} + +const docsPath = program.args[0]; + +if (options['include'] && !options['includes']) { + console.error(`usage: cannot combined --include with --no-include`); + process.exit(1); +} + +(async () => { + try { + if (options['include']) { + includes = options['include']; + } + else if (!options['includes']) { + includes = [ ]; + } + else { + includes = await findIncludes(); + } + + const parseOptions = { + deprecateHard: options.deprecateHard || false, + includeFiles: includes, + strict: options.strict || false + }; + + const results = await parseHeaders(docsPath, parseOptions); + const metadata = await readMetadata(docsPath); + + const simplified = simplify(results); + simplified['info'] = metadata; + + if (!options.validateOnly) { + console.log(JSON.stringify(simplified, null, 2)); + } + } catch (e) { + console.error(e); + process.exit(1); + } +})(); diff --git a/script/api-docs/docs-generator.js b/script/api-docs/docs-generator.js new file mode 100755 index 00000000000..dcf25e57311 --- /dev/null +++ b/script/api-docs/docs-generator.js @@ -0,0 +1,1417 @@ +#!/usr/bin/env node + +const markdownit = require('markdown-it'); +const { program } = require('commander'); + +const path = require('node:path'); +const fs = require('node:fs/promises'); +const process = require('node:process'); + +const githubPath = 'https://github.com/libgit2/libgit2'; + +const linkPrefix = '/docs/reference'; + +const projectTitle = 'libgit2'; +const includePath = 'include/git2'; + +const fileDenylist = [ 'stdint.h' ]; +const showVersions = true; + +const defaultBranch = 'main'; + +const markdown = markdownit(); +const markdownDefaults = { + code_inline: markdown.renderer.rules.code_inline +}; +markdown.renderer.rules.code_inline = (tokens, idx, options, env, self) => { + const version = env.__version || defaultBranch; + + const code = tokens[idx].content; + const text = `${nowrap(sanitize(tokens[idx].content))}`; + const link = linkForCode(version, code, text); + + return link ? link : text; +}; + +// globals +const apiData = { }; +const versions = [ ]; +const versionDeltas = { }; + +function produceVersionPicker(version, classes, cb) { + let content = ""; + + if (!showVersions) { + return content; + } + + content += `
\n`; + content += ` Version:\n`; + content += ` \n`; + + content += `
\n`; + + return content; +} + +function produceBreadcrumb(version, api, type) { + let content = ""; + let group = api.group; + let sys = false; + + if (group.endsWith('.h')) { + group = group.substr(0, group.length - 2); + } + + let groupTitle = group; + + if (groupTitle.startsWith('sys/')) { + groupTitle = groupTitle.substr(4); + groupTitle += ' (advanced)'; + } + + content += `
\n`; + content += ` \n`; + content += `
\n`; + + return content; +} + +function produceHeader(version, api, type) { + let content = ""; + + content += `
\n`; + content += `

${api.name}

\n`; + + content += produceAttributes(version, api, type); + content += produceSearchArea(version, type); + + content += produceVersionPicker(version, + `apiHeaderVersionSelect ${type}HeaderVersionSelect`, + (v) => { + const versionedApi = selectApi(v, (i => i.name === api.name)); + return versionedApi ? linkFor(v, versionedApi) : undefined; + }); + + content += `
\n`; + content += `\n`; + + return content; +} + +function produceAttributes(version, api, type) { + let content = ""; + + if (api.deprecations) { + content += ` Deprecated\n`; + } + + return content; +} + +function produceDescription(version, desc, type) { + let content = ""; + + if (! desc.comment) { + return content; + } + + content += `\n`; + content += `
\n`; + + for (const para of Array.isArray(desc.comment) ? desc.comment : [ desc.comment ]) { + content += ` ${markdown.render(para, { __version: version })}\n`; + } + + content += `
\n`; + + return content; +} + +function produceList(version, api, type, listType) { + let content = ""; + + if (!api[listType]) { + return content; + } + + const listTypeUpper = listType.charAt(0).toUpperCase() + listType.slice(1); + const listTypeTitle = listTypeUpper.replaceAll(/(.)([A-Z])/g, (match, one, two) => { return one + ' ' + two; }); + + content += `\n`; + content += `

${listTypeTitle}

\n`; + + content += `
\n`; + content += `
    \n`; + + for (const item of api[listType]) { + content += `
  • \n`; + content += ` ${linkText(version, item)}\n`; + content += `
  • \n`; + } + + content += `
\n`; + content += `
\n`; + + return content; +} + +function produceNotes(version, api, type) { + return produceList(version, api, type, 'notes'); +} + +function produceSeeAlso(version, api, type) { + return produceList(version, api, type, 'deprecated'); +} + +function produceSeeAlso(version, api, type) { + return produceList(version, api, type, 'see'); +} + +function produceWarnings(version, api, type) { + return produceList(version, api, type, 'warnings'); +} + +function produceDeprecations(version, api, type) { + return produceList(version, api, type, 'deprecations'); +} + +function produceGitHubLink(version, api, type) { + if (!api || !api.location || !api.location.file) { + return undefined; + } + + let file = api.location.file; + + let link = githubPath + '/blob/' + version + '/' + includePath + '/' + file; + + if (api.location.line) { + link += '#L' + api.location.line; + } + + return link; +} + +function produceSignatureForFunction(version, api, type) { + let content = ""; + let paramCount = 0; + + let prefix = type === 'callback' ? 'typedef' : ''; + const returnType = api.returns?.type || 'int'; + + const githubLink = produceGitHubLink(version, api, type); + + content += `\n`; + + content += `

Signature

\n`; + + if (githubLink) { + content += ` \n`; + } + + content += `
\n`; + + content += ` ${prefix ? prefix + ' ' : ''}${returnType}`; + content += returnType.endsWith('*') ? '' : ' '; + content += `${api.name}(`; + + for (const param of api.params || [ ]) { + content += (paramCount++ > 0) ? ', ' : ''; + + if (!param.type && options.strict) { + throw new Error(`param ${param.name} has no type for function ${api.name}`); + } + else if (!param.type) { + continue; + } + + content += ``; + content += `${param.type}`; + content += param.type.endsWith('*') ? '' : ' '; + + if (param.name) { + content += `${param.name}`; + } + + content += ``; + } + + content += `);\n`; + content += `
\n`; + + return content; +} + +function produceFunctionParameters(version, api, type) { + let content = ""; + + if (!api.params || api.params.length == 0) { + return content; + } + + content += `\n`; + + content += `

Parameters

\n`; + content += `
\n`; + + for (const param of api.params) { + let direction = param.direction || 'in'; + direction = direction.charAt(0).toUpperCase() + direction.slice(1); + + if (!param.type && options.strict) { + throw new Error(`param ${param.name} has no type for function ${api.name}`); + } + else if (!param.type) { + continue; + } + + content += `
\n`; + content += `
\n`; + content += ` ${linkType(version, param.type)}\n`; + content += `
\n`; + + if (param.extendedType) { + content += `
\n`; + content += ` ${linkType(version, param.extendedType.type)}\n`; + content += `
\n`; + } + + content += `
\n`; + + content += ` ${direction}\n`; + content += `
\n`; + + if (param.name) { + content += `
\n`; + content += ` ${param.name}\n`; + content += `
\n`; + } + + content += `
\n`; + content += ` ${render(version, param.comment)}\n`; + content += `
\n`; + content += `
\n`; + } + + content += `
\n`; + + return content; +} + +function produceFunctionReturn(version, api, type) { + let content = ""; + + if (api.returns && api.returns.type && api.returns.type !== 'void') { + content += `\n`; + content += `

Returns

\n`; + content += `
\n`; + content += `
\n`; + content += ` ${linkType(version, api.returns.type)}\n`; + content += `
\n`; + content += `
\n`; + content += ` ${render(version, api.returns.comment)}\n`; + content += `
\n`; + content += `
\n`; + } + + return content; +} + +function produceSignatureForObject(version, api, type) { + let content = ""; + + const githubLink = produceGitHubLink(version, api, type); + + content += `\n`; + + content += `

Signature

\n`; + + if (githubLink) { + content += ` \n`; + } + + content += `
\n`; + content += ` typedef ${api.referenceName} ${api.name}\n`; + content += `
\n`; + + return content; +} + +function produceSignatureForStruct(version, api, type) { + let content = ""; + + const githubLink = produceGitHubLink(version, api, type); + + content += `\n`; + + content += `

Signature

\n`; + + if (githubLink) { + content += ` \n`; + } + + const typedef = api.name.startsWith('struct') ? '' : 'typedef '; + + content += `
\n`; + content += ` ${typedef}struct ${api.name} {\n`; + + for (const member of api.members || [ ]) { + content += ``; + content += `${member.type}`; + content += member.type.endsWith('*') ? '' : ' '; + + if (member.name) { + content += `${member.name}`; + } + + content += `\n`; + } + + content += ` };\n`; + content += `
\n`; + + return content; +} + +function isOctalEnum(version, api, type) { + return api.name === 'git_filemode_t'; +} + +function isFlagsEnum(version, api, type) { + // TODO: also handle the flags metadata instead of always just guessing + if (type !== 'enum') { + return false; + } + + let largest = 0; + + for (const member of api.members) { + if (member.value === undefined) { + return false; + } + + if (member.value && (member.value & (member.value - 1))) { + return false; + } + + largest = member.value; + } + + return (largest > 1); +} + +function flagsOctal(v) { + const n = parseInt(v); + return n ? `0${n.toString(8)}` : 0; +} + +function flagsValue(v) { + if (v === '0') { + return '0'; + } + + return `(1 << ${Math.log2(v)})`; +} + +function produceMembers(version, api, type) { + let content = ""; + let value = 0; + + if (!api.members || api.members.length == 0) { + return ""; + } + + let title = type === 'enum' ? 'Values' : 'Members'; + const isOctal = isOctalEnum(version, api, type); + const isFlags = isFlagsEnum(version, api, type); + + content += `\n`; + + content += `

${title}

\n`; + + const githubLink = api.kind === 'struct' ? undefined : produceGitHubLink(version, api, type); + + if (githubLink) { + content += ` \n`; + } + + content += `
\n`; + + for (const member of api.members) { + value = member.value ? member.value : value; + + content += `
\n`; + + if (type === 'struct') { + content += `
\n`; + content += ` ${linkType(version, member.type)}\n`; + content += `
\n`; + } + + content += `
\n`; + content += ` ${member.name}\n`; + content += `
\n`; + + if (type === 'enum') { + const enumValue = isOctal ? flagsOctal(value) : (isFlags ? flagsValue(value) : value); + + content += `
\n`; + content += ` ${enumValue}\n`; + content += `
\n`; + } + + content += `
\n`; + content += ` ${render(version, member.comment)}\n`; + content += `
\n`; + content += `
\n`; + + value++; + } + + content += `
\n`; + + return content; +} + +function produceReturnedBy(version, api, type) { + return produceList(version, api, type, 'returnedBy'); +} + +function produceParameterTo(version, api, type) { + return produceList(version, api, type, 'parameterTo'); +} + +function produceVersionDeltas(version, api, type) { + let content = ''; + + if (!showVersions) { + return content; + } + + const deltas = versionDeltas[api.name]; + if (!deltas) { + throw new Error(`no version information for ${api.kind} ${api.name}`); + } + + content += `

Versions

\n`; + content += `
\n`; + content += `
    \n`; + + for (const idx in deltas) { + const item = deltas[idx]; + + if (idx == deltas.length - 1) { + content += `
  • \n`; + } else if (item.changed) { + content += `
  • \n`; + } else { + content += `
  • \n`; + } + + content += ` ${item.version}\n`; + content += `
  • \n`; + } + + content += `
\n`; + content += `
\n`; + + return content; +} + +async function layout(data) { + let layout; + + if (options.layout) { + layout = await fs.readFile(options.layout); + } + else if (options.jekyllLayout) { + layout = `---\ntitle: {{title}}\nlayout: ${options.jekyllLayout}\n---\n\n{{content}}`; + } + else { + return data.content; + } + + return layout.toString().replaceAll(/{{([a-z]+)}}/g, (match, p1) => data[p1] || ""); +} + +function produceSearchArea(version, type) { + let content = ""; + + content += `\n`; + content += ` \n`; + content += ` \n`; + + content += `
\n`; + content += ` \n`; + content += `
\n`; + content += `
\n`; + content += `
\n`; + content += `\n`; + + return content; +} + +async function produceDocumentationForApi(version, api, type) { + let content = ""; + + content += `
\n`; + + content += produceBreadcrumb(version, api, type); + content += produceHeader(version, api, type); + content += produceDescription(version, api, type); + content += produceNotes(version, api, type); + content += produceDeprecations(version, api, type); + content += produceSeeAlso(version, api, type); + content += produceWarnings(version, api, type); + content += produceSignature(version, api, type); + content += produceMembers(version, api, type); + content += produceFunctionParameters(version, api, type); + content += produceFunctionReturn(version, api, type); + content += produceReturnedBy(version, api, type); + content += produceParameterTo(version, api, type); + content += produceVersionDeltas(version, api, type); + + content += `
\n`; + + + const name = (type === 'macro' && api.name.includes('(')) ? + api.name.replace(/\(.*/, '') : api.name; + + const groupDir = `${outputPath}/${version}/${api.group}`; + const filename = `${groupDir}/${name}.html`; + + await fs.mkdir(groupDir, { recursive: true }); + await fs.writeFile(filename, await layout({ + title: `${api.name} (${projectTitle} ${version})`, + content: content + })); +} + +function selectApi(version, cb) { + const allApis = allApisForVersion(version, apiData[version]['groups']); + + for (const name in allApis) { + const api = allApis[name]; + + if (cb(api)) { + return api; + } + } + + return undefined; +} + +function apiFor(version, type) { + return selectApi(version, ((api) => api.name === type)); +} + +function linkFor(version, api) { + const name = (api.kind === 'macro' && api.name.includes('(')) ? + api.name.replace(/\(.*/, '') : api.name; + + return `${linkPrefix}/${version}/${api.group}/${name}.html`; +} + +function linkForCode(version, code, text) { + let api = selectApi(version, ((api) => api.name === code)); + let valueDecl = undefined; + + const apisForVersion = allApisForVersion(version, apiData[version]['groups']); + + if (!api) { + for (const enumDecl of Object.values(apisForVersion).filter(api => api.kind === 'enum')) { + const member = enumDecl.members.filter((m) => m.name === code); + + if (member && member[0]) { + api = enumDecl; + valueDecl = member[0]; + break; + } + } + } + + if (!api) { + return undefined; + } + + const kind = internalKind(version, api); + let link = linkFor(version, api); + + if (valueDecl) { + link += `#${valueDecl.name}`; + } + + if (!text) { + text = `${sanitize(code)}`; + } + + return `${text}`; +} + +function linkType(version, given) { + let type = given; + + if ((content = given.match(/^(?:const\s+)?([A-Za-z0-9_]+)(?:\s+\*+)?/))) { + type = content[1]; + } + + const api = apiFor(version, type); + + if (api) { + return `${given}`; + } + + return given; +} + +function linkText(version, str) { + const api = apiFor(version, str); + + if (api) { + return `${str}`; + } + + return sanitize(str); +} + +function render(version, str) { + let content = [ ]; + + if (!str) { + return ''; + } + + for (const s of Array.isArray(str) ? str : [ str ] ) { + content.push(markdown.render(s, { __version: version }).replaceAll(/\s+/g, ' ')); + } + + return content.join(' '); +} + +function nowrap(text) { + text = text.replaceAll(' ', ' '); + text = `${text}`; + return text; +} + +function sanitize(str) { + let content = [ ]; + + if (!str) { + return ''; + } + + for (const s of Array.isArray(str) ? str : [ str ] ) { + content.push(s.replaceAll('&', '&') + .replaceAll('<', '<') + .replaceAll('>', '>') + .replaceAll('{', '{') + .replaceAll('}', '}')); + } + + return content.join(' '); +} + +function produceSignatureForAlias(version, api, type) { + let content = ""; + + const githubLink = produceGitHubLink(version, api, type); + + content += `

Signature

\n`; + + if (githubLink) { + content += ` \n`; + } + + content += `
\n`; + content += ` typedef ${api.name} ${api.type};`; + content += `
\n`; + + return content; +} + +function produceSignatureForMacro(version, api, type) { + let content = ""; + + const githubLink = produceGitHubLink(version, api, type); + + content += `

Signature

\n`; + + if (githubLink) { + content += ` \n`; + } + + content += `
\n`; + content += ` #define ${api.name} ${sanitize(api.value)}`; + content += `
\n`; + + return content; +} + +function produceSignature(version, api, type) { + if (type === 'macro') { + return produceSignatureForMacro(version, api, type); + } + else if (type === 'alias') { + return produceSignatureForAlias(version, api, type); + } + else if (type === 'function' || type === 'callback') { + return produceSignatureForFunction(version, api, type); + } + else if (type === 'object') { + return produceSignatureForObject(version, api, type); + } + else if (type === 'struct') { + return produceSignatureForStruct(version, api, type); + } + else if (type === 'struct' || type === 'enum') { + return ""; + } + else { + throw new Error(`unknown type: ${api.kind}`); + } +} + +function isFunctionPointer(type) { + return type.match(/^(const\s+)?[A-Za-z0-9_]+\s+\*?\(\*/); +} + +function isEnum(type) { + return type.match(/^enum\s+/); +} + +function isStruct(type) { + return type.match(/^struct\s+/); +} + +function internalKind(version, api) { + if (api.kind === 'struct' && api.opaque) { + return 'object'; + } + + return api.kind; +} + +function externalKind(kind) { + if (kind === 'object') { + return 'struct'; + } + + return kind; +} + +async function produceIndexForGroup(version, group, versionApis) { + let content = ""; + + if (versionApis['groups'][group].apis.length == 0) { + return; + } + + const apis = Object.values(versionApis['groups'][group].apis); + + let fileName = group; + if (fileName.endsWith('.h')) { + fileName = fileName.substr(0, fileName.length - 2); + } + + const system = fileName.startsWith('sys/'); + let groupName = system ? fileName.substr(4) : fileName; + + content += `
\n`; + + content += `
\n`; + content += ` \n`; + content += `
\n`; + + content += `
\n`; + content += `

${groupName}

\n`; + + content += produceSearchArea(version, 'group'); + content += produceVersionPicker(version, "groupHeaderVersionSelect", (v) => { + if (apiData[v]['groups'][group]) { + return `${linkPrefix}/${v}/${groupName}/index.html`; + } + return undefined; + }); + + content += `
\n`; + + let details = undefined; + + if (versionApis['groups'][group].info?.details) { + details = markdown.render(versionApis['groups'][group].info.details, { __version: version }); + } else if (versionApis['groups'][group].info?.summary) { + details = versionApis['groups'][group].info.summary; + } + + if (details) { + content += `
\n`; + content += ` ${details}\n`; + content += `
\n`; + } + + for (const kind of [ 'object', 'struct', 'macro', 'enum', 'callback', 'alias', 'function' ]) { + content += produceIndexForApiKind(version, apis.filter(api => { + if (kind === 'object') { + return api.kind === 'struct' && api.opaque; + } + else if (kind === 'struct') { + return api.kind === 'struct' && !api.opaque; + } + else { + return api.kind === kind; + } + }), kind); + } + + content += `
\n`; + + const groupsDir = `${outputPath}/${version}/${fileName}`; + const filename = `${groupsDir}/index.html`; + + await fs.mkdir(groupsDir, { recursive: true }); + await fs.writeFile(filename, await layout({ + title: `${groupName} APIs (${projectTitle} ${version})`, + content: content + })); +} + +async function produceDocumentationForApis(version, apiData) { + const apis = allApisForVersion(version, apiData['groups']); + + for (const func of Object.values(apis).filter(api => api.kind === 'function')) { + await produceDocumentationForApi(version, func, 'function'); + } + + for (const struct of Object.values(apis).filter(api => api.kind === 'struct')) { + await produceDocumentationForApi(version, struct, internalKind(version, struct)); + } + + for (const e of Object.values(apis).filter(api => api.kind === 'enum')) { + await produceDocumentationForApi(version, e, 'enum'); + } + + for (const callback of Object.values(apis).filter(api => api.kind === 'callback')) { + await produceDocumentationForApi(version, callback, 'callback'); + } + + for (const alias of Object.values(apis).filter(api => api.kind === 'alias')) { + await produceDocumentationForApi(version, alias, 'alias'); + } + + for (const macro of Object.values(apis).filter(api => api.kind === 'macro')) { + await produceDocumentationForApi(version, macro, 'macro'); + } +} + +function produceIndexForApiKind(version, apis, kind) { + let content = ""; + + if (!apis || !apis.length) { + return content; + } + + let kindUpper = kind.charAt(0).toUpperCase() + kind.slice(1); + kindUpper += (kind === 'alias') ? 'es' : 's'; + + content += `\n`; + content += `

${kindUpper}

\n`; + + content += `
\n`; + + for (const item of apis) { + if (item.changed) { + content += `
\n`; + } else { + content += `
\n`; + } + + content += `
\n`; + content += ` \n`; + content += ` ${item.name}\n`; + content += ` \n`; + content += `
\n`; + + let shortComment = Array.isArray(item.comment) ? item.comment[0] : item.comment; + shortComment = shortComment || ''; + + shortComment = shortComment.replace(/\..*/, ''); + + content += `
\n`; + content += ` ${render(version, shortComment)}\n`; + content += `
\n`; + content += `
\n`; + } + + content += `
\n`; + + return content; +} + +function versionIndexContent(version, apiData) { + let content = ""; + let hasSystem = false; + + content += `
\n`; + content += `
\n`; + content += `

${projectTitle} ${version}

\n`; + + content += produceSearchArea(version, 'version'); + content += produceVersionPicker(version, "versionHeaderVersionSelect", + (v) => `${linkPrefix}/${v}/index.html`); + + content += `
\n`; + + content += `\n`; + content += `

Groups

\n`; + content += `
    \n`; + + for (const group of Object.keys(apiData['groups']).sort((a, b) => { + if (a.startsWith('sys/')) { return 1; } + if (b.startsWith('sys/')) { return -1; } + return a.localeCompare(b); + }).map(fn => { + let n = fn; + let sys = false; + + if (n.endsWith('.h')) { + n = n.substr(0, n.length - 2); + } + + if (n.startsWith('sys/')) { + n = n.substr(4); + sys = true; + } + + return { + name: n, filename: fn, system: sys, info: apiData['groups'][fn].info, apis: apiData['groups'][fn] + }; + }).filter(filedata => { + return Object.keys(filedata.apis).length > 0 && !fileDenylist.includes(filedata.filename); + })) { + if (group.system && !hasSystem) { + hasSystem = true; + + content += `
\n`; + content += `\n`; + content += `

System Groups (Advanced)

\n`; + content += `
    \n`; + } + + let link = `${linkPrefix}/${version}/`; + link += group.system ? `sys/` : ''; + link += group.name; + link += `/index.html`; + + content += `
  • \n`; + content += `
    \n`; + content += ` \n`; + content += ` ${group.name}\n`; + content += ` \n`; + content += `
    \n`; + + if (group.info?.summary) { + content += `
    \n`; + content += ` ${group.info.summary}`; + content += `
    \n`; + } + + content += `
  • \n`; + } + + content += `
\n`; + + content += `
\n`; + + return content; +} + +async function produceDocumentationIndex(version, apiData) { + const content = versionIndexContent(version, apiData); + + const versionDir = `${outputPath}/${version}`; + const filename = `${versionDir}/index.html`; + + await fs.mkdir(versionDir, { recursive: true }); + await fs.writeFile(filename, await layout({ + title: `APIs (${projectTitle} ${version})`, + content: content + })); +} + +async function documentationIsUpToDateForVersion(version, apiData) { + try { + const existingMetadata = JSON.parse(await fs.readFile(`${outputPath}/${version}/.metadata`)); + return existingMetadata?.commit === apiData.info.commit; + } + catch (e) { + } + + return false; +} + +async function produceDocumentationMetadata(version, apiData) { + const versionDir = `${outputPath}/${version}`; + const filename = `${versionDir}/.metadata`; + + await fs.mkdir(versionDir, { recursive: true }); + await fs.writeFile(filename, JSON.stringify(apiData.info, null, 2) + "\n"); +} + +async function cleanupOldDocumentation(version) { + const versionDir = `${outputPath}/${version}`; + + for (const fn of await fs.readdir(versionDir)) { + if (fn === '.metadata') { + continue; + } + + const path = `${versionDir}/${fn}`; + await fs.rm(path, { recursive: true }); + } +} + +async function produceDocumentationForVersion(version, apiData) { + if (!options.force && await documentationIsUpToDateForVersion(version, apiData)) { + if (options.verbose) { + console.log(`Documentation exists for ${version} at version ${apiData.info.commit.substr(0, 7)}; skipping...`); + } + + return; + } + + if (options.verbose) { + console.log(`Producing documentation for ${version}...`); + } + + await cleanupOldDocumentation(version); + + await produceDocumentationForApis(version, apiData); + + for (const group in apiData['groups']) { + await produceIndexForGroup(version, group, apiData); + } + + await produceDocumentationIndex(version, apiData); + + await produceDocumentationMetadata(version, apiData); +} + +function versionDeltaData(version, api) { + const base = { version: version, api: api }; + + if (api.kind === 'function') { + return { + ...base, + returns: api.returns?.type || 'int', + params: api.params?.map((p) => p.type) || [ 'void' ] + }; + } + else if (api.kind === 'enum') { + return { + ...base, + members: api.members?.map((m) => { return { 'name': m.name, 'value': m.value } }) + }; + } + else if (api.kind === 'callback') { + return { ...base, }; + } + else if (api.kind === 'alias') { + return { ...base, }; + } + else if (api.kind === 'struct') { + return { + ...base, + members: api.members?.map((m) => { return { 'name': m.name, 'type': m.type } }) + }; + } + else if (api.kind === 'macro') { + return { + ...base, + name: api.name, + value: api.value + }; + } + else { + throw new Error(`unknown api kind: '${api.kind}'`); + } +} + +function deltasEqual(a, b) { + const unversionedA = { ...a }; + const unversionedB = { ...b }; + + delete unversionedA.version; + delete unversionedA.api; + delete unversionedA.changed; + delete unversionedB.version; + delete unversionedB.api; + delete unversionedB.changed; + + return JSON.stringify(unversionedA) === JSON.stringify(unversionedB); +} + +const apiForVersionCache = { }; +function allApisForVersion(version, apiData) { + if (apiForVersionCache[version]) { + return apiForVersionCache[version]; + } + + let result = { }; + for (const file in apiData['groups']) { + result = { ...result, ...apiData['groups'][file].apis }; + } + + apiForVersionCache[version] = result; + return result; +} + +function seedVersionApis(apiData) { + for (const version in apiData) { + allApisForVersion(version, apiData[version]); + } +} + +function calculateVersionDeltas(apiData) { + for (const version in apiData) { + const apisForVersion = allApisForVersion(version, apiData[version]); + + for (const api in apisForVersion) { + if (!versionDeltas[api]) { + versionDeltas[api] = [ ]; + } + + versionDeltas[api].push(versionDeltaData(version, apisForVersion[api])); + } + } + + for (const api in versionDeltas) { + const count = versionDeltas[api].length; + + versionDeltas[api][count - 1].changed = true; + + for (let i = count - 2; i >= 0; i--) { + versionDeltas[api][i].changed = !deltasEqual(versionDeltas[api][i], versionDeltas[api][i + 1]); + } + } +} + +async function produceSearch(versions) { + if (options.verbose) { + console.log(`Producing search page...`); + } + + let content = ""; + + content += `\n`; + content += `\n`; + content += `\n`; + content += `\n`; + + content += `\n`; + + content += ` \n`; + + const filename = `${outputPath}/search.html`; + + await fs.mkdir(outputPath, { recursive: true }); + await fs.writeFile(filename, await layout({ + title: `API search (${projectTitle})`, + content: content + })); +} + +async function produceMainIndex(versions) { + const versionDefault = versions[0]; + + if (options.verbose) { + console.log(`Producing documentation index...`); + } + + let content = ""; + + content += `\n`; + content += `\n`; + + content += versionIndexContent(versionDefault, apiData[versionDefault]); + + const filename = `${outputPath}/index.html`; + + await fs.mkdir(outputPath, { recursive: true }); + await fs.writeFile(filename, await layout({ + title: `APIs (${projectTitle} ${versionDefault})`, + content: content + })); +} + +function versionSort(a, b) { + if (a === b) { + return 0; + } + + const aVersion = a.match(/^v(\d+)(?:\.(\d+)(?:\.(\d+)(?:\.(\d+))?)?)?(?:-(.*))?$/); + const bVersion = b.match(/^v(\d+)(?:\.(\d+)(?:\.(\d+)(?:\.(\d+))?)?)?(?:-(.*))?$/); + + if (!aVersion && !bVersion) { + return a.localeCompare(b); + } + else if (aVersion && !bVersion) { + return -1; + } + else if (!aVersion && bVersion) { + return 1; + } + + for (let i = 1; i < 5; i++) { + if (!aVersion[i] && !bVersion[i]) { + break; + } + else if (aVersion[i] && !bVersion[i]) { + return 1; + } + else if (!aVersion[i] && bVersion[i]) { + return -1; + } + else if (aVersion[i] !== bVersion[i]) { + return aVersion[i] - bVersion[i]; + } + } + + if (aVersion[5] && !bVersion[5]) { + return -1; + } + else if (!aVersion[5] && bVersion[5]) { + return 1; + } + else if (aVersion[5] && bVersion[5]) { + return aVersion[5].localeCompare(bVersion[5]); + } + + return 0; +} + +program.option('--output ') + .option('--layout ') + .option('--jekyll-layout ') + .option('--version ') + .option('--verbose') + .option('--force') + .option('--strict'); +program.parse(); + +const options = program.opts(); + +if (program.args.length != 2) { + console.error(`usage: ${path.basename(process.argv[1])} raw_api_dir output_dir`); + process.exit(1); +} + +const docsPath = program.args[0]; +const outputPath = program.args[1]; + +(async () => { + try { + const v = options.version ? options.version : + (await fs.readdir(docsPath)) + .filter(a => a.endsWith('.json')) + .map(a => a.replace(/\.json$/, '')); + + versions.push(...v.sort(versionSort).reverse()); + + for (const version of versions) { + if (options.verbose) { + console.log(`Reading documentation data for ${version}...`); + } + + apiData[version] = JSON.parse(await fs.readFile(`${docsPath}/${version}.json`)); + } + + if (showVersions) { + if (options.verbose) { + console.log(`Calculating version deltas...`); + } + + calculateVersionDeltas(apiData); + } + + for (const version of versions) { + await produceDocumentationForVersion(version, apiData[version]); + } + + await produceSearch(versions); + await produceMainIndex(versions); + } catch (e) { + console.error(e); + process.exit(1); + } +})(); diff --git a/script/api-docs/generate b/script/api-docs/generate new file mode 100755 index 00000000000..76f3d8ce6a8 --- /dev/null +++ b/script/api-docs/generate @@ -0,0 +1,115 @@ +#!/usr/bin/env bash +# +# Usage: generate repo_path output_path +# +# Example: generate https://github.com/libgit2/libgit2 path_to_output +# to clone the repository from GitHub and produce documentation; +# the repo_path can also be a local path + +set -eo pipefail + +source_path=$(mktemp -d) +verbose= +force= + +for var in "$@"; do + if [ "${var}" == "--verbose" ]; then + verbose=true + elif [ "${var}" == "--force" ]; then + force=true + elif [ "${repo_path}" == "" ]; then + repo_path="${var}" + elif [ "${output_path}" == "" ]; then + output_path="${var}" + else + repo_path="" + output_path="" + fi +done + +if [ "${repo_path}" = "" -o "${output_path}" = "" ]; then + echo "usage: $0 [--verbose] [--force] repo_path output_path" 1>&2 + exit 1 +fi + +function do_checkout { + if [ "$1" = "" ]; then + echo "usage: $0 source_path" 1>&2 + exit 1 + fi + + if [ "${verbose}" ]; then + echo ":: Checking out source trees..." + echo "" + fi + + source_path=$1 + + mkdir -p "${source_path}" + git clone "${repo_path}" "${source_path}/main" --no-checkout + ( cd "${source_path}/main" && git sparse-checkout set --no-cone 'include/*' ) + ( cd "${source_path}/main" && git read-tree origin/main ) + ( cd "${source_path}/main" && git checkout -- include ) + + for tag in $(git --git-dir="${source_path}/main/.git" tag -l); do + git --git-dir="${source_path}/main/.git" worktree add -f "${source_path}/${tag}" "${tag}" --no-checkout + ( cd "${source_path}/${tag}" && git sparse-checkout set --no-cone 'include/*' ) + ( cd "${source_path}/${tag}" && git read-tree HEAD ) + + if [ "${tag}" == "v0.1.0" ]; then + ( cd "${source_path}/${tag}" && git checkout -- src/git ) + elif [ "${tag}" == "v0.2.0" -o "${tag}" == "v0.3.0" ]; then + ( cd "${source_path}/${tag}" && git checkout -- src/git2 ) + else + ( cd "${source_path}/${tag}" && git checkout -- include ) + fi + done +} + +do_checkout ${source_path} + +if [ "${verbose}" ]; then + echo "" + echo ":: Generating raw API documentation..." + echo "" +fi + +for version in ${source_path}/*; do + version=$(echo "${version}" | sed -e "s/.*\///") + commit=$( cd "${source_path}/${version}" && git rev-parse HEAD ) + + if [ -f "${output_path}/api/${version}.json" ]; then + existing_commit=$(jq -r .info.commit < "${output_path}/api/${version}.json") + + if [ "${existing_commit}" == "${commit}" -a ! "${force}" ]; then + if [ "${verbose}" ]; then + echo "Raw API documentation for ${version} exists; skipping..." + fi + + continue + fi + fi + + echo "Generating raw API documentation for ${version}..." + mkdir -p "${output_path}/api" + node ./api-generator.js "${source_path}/${version}" > "${output_path}/api/${version}.json" +done + +if [ "${verbose}" ]; then + echo "" + echo ":: Generating HTML documentation..." + echo "" +fi + +search_options="" +docs_options="" +if [ "${verbose}" ]; then + search_options="${search_options} --verbose" + docs_options="${docs_options} --verbose" +fi +if [ "${force}" ]; then + docs_options="${docs_options} --force" +fi + +node ./search-generator.js ${search_options} "${output_path}/api" "${output_path}/search-index" +node ./docs-generator.js ${docs_options} --jekyll-layout default "${output_path}/api" "${output_path}/reference" diff --git a/script/api-docs/package-lock.json b/script/api-docs/package-lock.json new file mode 100644 index 00000000000..3ba22260f12 --- /dev/null +++ b/script/api-docs/package-lock.json @@ -0,0 +1,85 @@ +{ + "name": "api-docs", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "commander": "^12.1.0", + "markdown-it": "^14.1.0", + "minisearch": "^7.1.1" + } + }, + "node_modules/argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" + }, + "node_modules/commander": { + "version": "12.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz", + "integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==", + "engines": { + "node": ">=18" + } + }, + "node_modules/entities": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz", + "integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==", + "engines": { + "node": ">=0.12" + }, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, + "node_modules/linkify-it": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-5.0.0.tgz", + "integrity": "sha512-5aHCbzQRADcdP+ATqnDuhhJ/MRIqDkZX5pyjFHRRysS8vZ5AbqGEoFIb6pYHPZ+L/OC2Lc+xT8uHVVR5CAK/wQ==", + "dependencies": { + "uc.micro": "^2.0.0" + } + }, + "node_modules/markdown-it": { + "version": "14.1.0", + "resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-14.1.0.tgz", + "integrity": "sha512-a54IwgWPaeBCAAsv13YgmALOF1elABB08FxO9i+r4VFk5Vl4pKokRPeX8u5TCgSsPi6ec1otfLjdOpVcgbpshg==", + "dependencies": { + "argparse": "^2.0.1", + "entities": "^4.4.0", + "linkify-it": "^5.0.0", + "mdurl": "^2.0.0", + "punycode.js": "^2.3.1", + "uc.micro": "^2.1.0" + }, + "bin": { + "markdown-it": "bin/markdown-it.mjs" + } + }, + "node_modules/mdurl": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/mdurl/-/mdurl-2.0.0.tgz", + "integrity": "sha512-Lf+9+2r+Tdp5wXDXC4PcIBjTDtq4UKjCPMQhKIuzpJNW0b96kVqSwW0bT7FhRSfmAiFYgP+SCRvdrDozfh0U5w==" + }, + "node_modules/minisearch": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/minisearch/-/minisearch-7.1.1.tgz", + "integrity": "sha512-b3YZEYCEH4EdCAtYP7OlDyx7FdPwNzuNwLQ34SfJpM9dlbBZzeXndGavTrC+VCiRWomL21SWfMc6SCKO/U2ZNw==" + }, + "node_modules/punycode.js": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode.js/-/punycode.js-2.3.1.tgz", + "integrity": "sha512-uxFIHU0YlHYhDQtV4R9J6a52SLx28BCjT+4ieh7IGbgwVJWO+km431c4yRlREUAsAmt/uMjQUyQHNEPf0M39CA==", + "engines": { + "node": ">=6" + } + }, + "node_modules/uc.micro": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-2.1.0.tgz", + "integrity": "sha512-ARDJmphmdvUk6Glw7y9DQ2bFkKBHwQHLi2lsaH6PPmz/Ka9sFOBsBluozhDltWmnv9u/cF6Rt87znRTPV+yp/A==" + } + } +} diff --git a/script/api-docs/package.json b/script/api-docs/package.json new file mode 100644 index 00000000000..67a6fe7aa2a --- /dev/null +++ b/script/api-docs/package.json @@ -0,0 +1,7 @@ +{ + "dependencies": { + "commander": "^12.1.0", + "markdown-it": "^14.1.0", + "minisearch": "^7.1.1" + } +} diff --git a/script/api-docs/search-generator.js b/script/api-docs/search-generator.js new file mode 100755 index 00000000000..cad73f947ae --- /dev/null +++ b/script/api-docs/search-generator.js @@ -0,0 +1,212 @@ +#!/usr/bin/env node + +const markdownit = require('markdown-it'); +const { program } = require('commander'); +const minisearch = require('minisearch'); + +const path = require('node:path'); +const fs = require('node:fs/promises'); + +const linkPrefix = '/docs/reference'; + +const defaultBranch = 'main'; + +function uniqueifyId(api, nodes) { + let suffix = "", i = 1; + + while (true) { + const possibleId = `${api.kind}-${api.name}${suffix}`; + let collision = false; + + for (const item of nodes) { + if (item.id === possibleId) { + collision = true; + break; + } + } + + if (!collision) { + return possibleId; + } + + suffix = `-${++i}`; + } +} + +async function produceSearchIndex(version, apiData) { + const nodes = [ ]; + + for (const group in apiData['groups']) { + for (const name in apiData['groups'][group]['apis']) { + const api = apiData['groups'][group]['apis'][name]; + + let displayName = name; + + if (api.kind === 'macro') { + displayName = displayName.replace(/\(.*/, ''); + } + + const apiSearchData = { + id: uniqueifyId(api, nodes), + name: displayName, + group: group, + kind: api.kind + }; + + apiSearchData.description = Array.isArray(api.comment) ? + api.comment[0] : api.comment; + + let detail = ""; + + if (api.kind === 'macro') { + detail = api.value; + } + else if (api.kind === 'alias') { + detail = api.type; + } + else { + let details = undefined; + + if (api.kind === 'struct' || api.kind === 'enum') { + details = api.members; + } + else if (api.kind === 'function' || api.kind === 'callback') { + details = api.params; + } + else { + throw new Error(`unknown api type '${api.kind}'`); + } + + for (const item of details || [ ]) { + if (detail.length > 0) { + detail += ' '; + } + + detail += item.name; + + if (item.comment) { + detail += ' '; + detail += item.comment; + } + } + + if (api.kind === 'function' || api.kind === 'callback') { + if (detail.length > 0 && api.returns?.type) { + detail += ' ' + api.returns.type; + } + + if (detail.length > 0 && api.returns?.comment) { + detail += ' ' + api.returns.comment; + } + } + } + + detail = detail.replaceAll(/\s+/g, ' ') + .replaceAll(/[\"\'\`]/g, ''); + + apiSearchData.detail = detail; + + nodes.push(apiSearchData); + } + } + + const index = new minisearch({ + fields: [ 'name', 'description', 'detail' ], + storeFields: [ 'name', 'group', 'kind', 'description' ], + searchOptions: { boost: { name: 5, description: 2 } } + }); + + index.addAll(nodes); + + const filename = `${outputPath}/${version}.json`; + await fs.mkdir(outputPath, { recursive: true }); + await fs.writeFile(filename, JSON.stringify(index, null, 2)); +} + +function versionSort(a, b) { + if (a === b) { + return 0; + } + + const aVersion = a.match(/^v(\d+)(?:\.(\d+)(?:\.(\d+)(?:\.(\d+))?)?)?(?:-(.*))?$/); + const bVersion = b.match(/^v(\d+)(?:\.(\d+)(?:\.(\d+)(?:\.(\d+))?)?)?(?:-(.*))?$/); + + if (!aVersion && !bVersion) { + return a.localeCompare(b); + } + else if (aVersion && !bVersion) { + return -1; + } + else if (!aVersion && bVersion) { + return 1; + } + + for (let i = 1; i < 5; i++) { + if (!aVersion[i] && !bVersion[i]) { + break; + } + else if (aVersion[i] && !bVersion[i]) { + return 1; + } + else if (!aVersion[i] && bVersion[i]) { + return -1; + } + else if (aVersion[i] !== bVersion[i]) { + return aVersion[i] - bVersion[i]; + } + } + + if (aVersion[5] && !bVersion[5]) { + return -1; + } + else if (!aVersion[5] && bVersion[5]) { + return 1; + } + else if (aVersion[5] && bVersion[5]) { + return aVersion[5].localeCompare(bVersion[5]); + } + + return 0; +} + +program.option('--verbose') + .option('--version '); +program.parse(); + +const options = program.opts(); + +if (program.args.length != 2) { + console.error(`usage: ${path.basename(process.argv[1])} raw_api_dir output_dir`); + process.exit(1); +} + +const docsPath = program.args[0]; +const outputPath = program.args[1]; + +(async () => { + try { + const v = options.version ? options.version : + (await fs.readdir(docsPath)) + .filter(a => a.endsWith('.json')) + .map(a => a.replace(/\.json$/, '')); + + const versions = v.sort(versionSort).reverse(); + + for (const version of versions) { + if (options.verbose) { + console.log(`Reading documentation data for ${version}...`); + } + + const apiData = JSON.parse(await fs.readFile(`${docsPath}/${version}.json`)); + + if (options.verbose) { + console.log(`Creating minisearch index for ${version}...`); + } + + await produceSearchIndex(version, apiData); + } + } catch (e) { + console.error(e); + process.exit(1); + } +})(); diff --git a/script/backport.sh b/script/backport.sh new file mode 100755 index 00000000000..3c2f899d6d6 --- /dev/null +++ b/script/backport.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +if test $# -eq 0 +then + echo "USAGE: $0 <#PR> [<#PR>...]" + exit +fi + +commits= + +for pr in $* +do + mergecommit=$(git rev-parse ":/Merge pull request #$pr" || exit 1) + mergebase=$(git merge-base "$mergecommit"^1 "$mergecommit"^2 || exit 1) + + commits="$commits $(git rev-list --reverse "$mergecommit"^2 ^"$mergebase")" +done + +echo "Cherry-picking the following commits:" +git rev-list --no-walk --oneline $commits +echo + +git cherry-pick $commits diff --git a/script/leaks.sh b/script/leaks.sh new file mode 100755 index 00000000000..4ae748ccc52 --- /dev/null +++ b/script/leaks.sh @@ -0,0 +1,8 @@ +#!/bin/sh +export MallocStackLogging=1 +export MallocScribble=1 +export MallocLogFile=/dev/null +# Exclude known Apple Security framework leak in CryptKit::FEEKeyInfoProvider +# which occurs during SSL/TLS handshakes and is not in libgit2's control +export CLAR_AT_EXIT="leaks -quiet -exclude=SSLHandshake \$PPID" +exec "$@" diff --git a/script/release.py b/script/release.py new file mode 100755 index 00000000000..1a240decab6 --- /dev/null +++ b/script/release.py @@ -0,0 +1,191 @@ +#!/usr/bin/env python + +from collections import namedtuple + +import argparse +import base64 +import copy +import json +import subprocess +import sys +import urllib.parse +import urllib.request +import urllib.error + +class Error(Exception): + pass + +class Version(object): + def __init__(self, version): + versions = version.split(sep='.') + if len(versions) < 2 or len(versions) > 3: + raise Error("Invalid version string '{}'".format(version)) + self.major = int(versions[0]) + self.minor = int(versions[1]) + self.revision = int(versions[2]) if len(versions) == 3 else 0 + + def __str__(self): + return '{}.{}.{}'.format(self.major, self.minor, self.revision) + + def __eq__(self, other): + return self.major == other.major and self.minor == other.minor and self.revision == other.revision + +def verify_version(version): + expected = { + 'VERSION': [ '"{}"'.format(version), None ], + 'VER_MAJOR': [ str(version.major), None ], + 'VER_MINOR': [ str(version.minor), None ], + 'VER_REVISION': [ str(version.revision), None ], + 'VER_PATCH': [ '0', None ], + 'SOVERSION': [ '"{}.{}"'.format(version.major, version.minor), None ], + } + + # Parse CMakeLists + with open('CMakeLists.txt') as f: + for line in f.readlines(): + if line.startswith('project(libgit2 VERSION "{}"'.format(version)): + break + else: + raise Error("cmake: invalid project definition") + + # Parse version.h + with open('include/git2/version.h') as f: + lines = f.readlines() + + for key in expected.keys(): + define = '#define LIBGIT2_{} '.format(key) + for line in lines: + if line.startswith(define): + expected[key][1] = line[len(define):].strip() + break + else: + raise Error("version.h: missing define for '{}'".format(key)) + + for k, v in expected.items(): + if v[0] != v[1]: + raise Error("version.h: define '{}' does not match (got '{}', expected '{}')".format(k, v[0], v[1])) + + with open('package.json') as f: + pkg = json.load(f) + + try: + pkg_version = Version(pkg["version"]) + except KeyError as err: + raise Error("package.json: missing the field {}".format(err)) + + if pkg_version != version: + raise Error("package.json: version does not match (got '{}', expected '{}')".format(pkg_version, version)) + +def generate_relnotes(tree, version): + with open('docs/changelog.md') as f: + lines = f.readlines() + + if not lines[0].startswith('v'): + raise Error("changelog.md: missing section for v{}".format(version)) + try: + v = Version(lines[0][1:].strip()) + except: + raise Error("changelog.md: invalid version string {}".format(lines[0].strip())) + if v != version: + raise Error("changelog.md: changelog version doesn't match (got {}, expected {})".format(v, version)) + if not lines[1].startswith('----'): + raise Error("changelog.md: missing version header") + if lines[2] != '\n': + raise Error("changelog.md: missing newline after version header") + + for i, line in enumerate(lines[3:]): + if not line.startswith('v'): + continue + try: + Version(line[1:].strip()) + break + except: + continue + else: + raise Error("changelog.md: cannot find section header of preceding release") + + return ''.join(lines[3:i + 3]).strip() + +def git(*args): + process = subprocess.run([ 'git', *args ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if process.returncode != 0: + raise Error('Failed executing git {}: {}'.format(' '.join(args), process.stderr.decode())) + return process.stdout + +def post(url, data, contenttype, user, password): + request = urllib.request.Request(url, data=data) + request.add_header('Accept', 'application/json') + request.add_header('Content-Type', contenttype) + request.add_header('Content-Length', len(data)) + request.add_header('Authorization', 'Basic ' + base64.b64encode('{}:{}'.format(user, password).encode()).decode()) + + try: + response = urllib.request.urlopen(request) + if response.getcode() != 201: + raise Error("POST to '{}' failed: {}".format(url, response.reason)) + except urllib.error.URLError as e: + raise Error("POST to '{}' failed: {}".format(url, e)) + data = json.load(response) + + return data + +def generate_asset(version, tree, archive_format): + Asset = namedtuple('Asset', ['name', 'label', 'mimetype', 'data']) + mimetype = 'application/{}'.format('gzip' if archive_format == 'tar.gz' else 'zip') + return Asset( + "libgit2-{}.{}".format(version, archive_format), "Release sources: libgit2-{}.{}".format(version, archive_format), mimetype, + git('archive', '--format', archive_format, '--prefix', 'libgit2-{}/'.format(version), tree) + ) + +def release(args): + params = { + "tag_name": 'v' + str(args.version), + "name": 'libgit2 v' + str(args.version), + "target_commitish": git('rev-parse', args.tree).decode().strip(), + "body": generate_relnotes(args.tree, args.version), + } + assets = [ + generate_asset(args.version, args.tree, 'tar.gz'), + generate_asset(args.version, args.tree, 'zip'), + ] + + if args.dryrun: + for k, v in params.items(): + print('{}: {}'.format(k, v)) + for asset in assets: + print('asset: name={}, label={}, mimetype={}, bytes={}'.format(asset.name, asset.label, asset.mimetype, len(asset.data))) + return + + try: + url = 'https://api.github.com/repos/{}/releases'.format(args.repository) + response = post(url, json.dumps(params).encode(), 'application/json', args.user, args.password) + except Error as e: + raise Error('Could not create release: ' + str(e)) + + for asset in assets: + try: + url = list(urllib.parse.urlparse(response['upload_url'].split('{?')[0])) + url[4] = urllib.parse.urlencode({ 'name': asset.name, 'label': asset.label }) + post(urllib.parse.urlunparse(url), asset.data, asset.mimetype, args.user, args.password) + except Error as e: + raise Error('Could not upload asset: ' + str(e)) + +def main(): + parser = argparse.ArgumentParser(description='Create a libgit2 release') + parser.add_argument('--tree', default='HEAD', help='tree to create release for (default: HEAD)') + parser.add_argument('--dryrun', action='store_true', help='generate release, but do not post it') + parser.add_argument('--repository', default='libgit2/libgit2', help='GitHub repository to create repository in') + parser.add_argument('--user', help='user to authenticate as') + parser.add_argument('--password', help='password to authenticate with') + parser.add_argument('version', type=Version, help='version of the new release') + args = parser.parse_args() + + verify_version(args.version) + release(args) + +if __name__ == '__main__': + try: + main() + except Error as e: + print(e) + sys.exit(1) diff --git a/script/sanitizers.supp b/script/sanitizers.supp new file mode 100644 index 00000000000..4e0e9be8332 --- /dev/null +++ b/script/sanitizers.supp @@ -0,0 +1,4 @@ +[undefined] +# This library allows unaligned access on Intel-like processors. Prevent UBSan +# from complaining about that. +fun:sha1_compression_states diff --git a/script/thread-sanitizer.supp b/script/thread-sanitizer.supp new file mode 100644 index 00000000000..97d23046ff2 --- /dev/null +++ b/script/thread-sanitizer.supp @@ -0,0 +1,26 @@ +# In attr_file_free, the locks are acquired in the opposite order in which they +# are normally acquired. This is probably something worth fixing to have a +# consistent lock hierarchy that is easy to understand. +deadlock:attr_cache_lock + +# git_mwindow_file_register has the possibility of evicting some files from the +# global cache. In order to avoid races and closing files that are currently +# being accessed, before evicting any file it will attempt to acquire that +# file's lock. Finally, git_mwindow_file_register is typically called with a +# file lock held, because the caller will use the fd in the mwf immediately +# after registering it. This causes ThreadSanitizer to observe different orders +# of acquisition of the mutex (which implies a possibility of a deadlock), +# _but_ since the files are added to the cache after other files have been +# evicted, there cannot be a case where mwf A is trying to be registered while +# evicting mwf B concurrently and viceversa: at most one of them can be present +# in the cache. +deadlock:git_mwindow_file_register + +# When invoking the time/timezone functions from git_signature_now(), they +# access libc methods that need to be instrumented to correctly analyze the +# data races. +called_from_lib:libc.so.6 + +# TODO(#5592): Investigate and fix this. It can be triggered by the `thread` +# test suite. +race:git_filter_list__load_ext diff --git a/script/user_model.c b/script/user_model.c new file mode 100644 index 00000000000..49425272e4d --- /dev/null +++ b/script/user_model.c @@ -0,0 +1,98 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +void *realloc(void *ptr, size_t size); +void *memmove(void *dest, const void *src, size_t n); +size_t strlen(const char *s); + +typedef struct va_list_str *va_list; + +typedef struct git_vector { + void **contents; + size_t length; +} git_vector; + +typedef struct git_buf { + char *ptr; + size_t asize, size; +} git_buf; + +int git_vector_insert(git_vector *v, void *element) +{ + if (!v) + __coverity_panic__(); + + v->contents = realloc(v->contents, ++v->length); + if (!v->contents) + __coverity_panic__(); + v->contents[v->length] = element; + + return 0; +} + +int git_buf_len(const struct git_buf *buf) +{ + return strlen(buf->ptr); +} + +int git_buf_vprintf(git_buf *buf, const char *format, va_list ap) +{ + char ch, *s; + size_t len; + + __coverity_string_null_sink__(format); + __coverity_string_size_sink__(format); + + ch = *format; + ch = *(char *)ap; + + buf->ptr = __coverity_alloc__(len); + __coverity_writeall__(buf->ptr); + buf->size = len; + + return 0; +} + +int git_buf_put(git_buf *buf, const char *data, size_t len) +{ + buf->ptr = __coverity_alloc__(buf->size + len + 1); + memmove(buf->ptr + buf->size, data, len); + buf->size += len; + buf->ptr[buf->size + len] = 0; + return 0; +} + +int git_buf_set(git_buf *buf, const void *data, size_t len) +{ + buf->ptr = __coverity_alloc__(len + 1); + memmove(buf->ptr, data, len); + buf->size = len + 1; + return 0; +} + +void clar__fail( + const char *file, + int line, + const char *error, + const char *description, + int should_abort) +{ + if (should_abort) + __coverity_panic__(); +} + +void clar__assert( + int condition, + const char *file, + int line, + const char *error, + const char *description, + int should_abort) +{ + if (!condition && should_abort) + __coverity_panic__(); +} diff --git a/script/user_nodefs.h b/script/user_nodefs.h new file mode 100644 index 00000000000..b6e2df312f7 --- /dev/null +++ b/script/user_nodefs.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#nodef GIT_ERROR_CHECK_ALLOC(ptr) if (ptr == NULL) { __coverity_panic__(); } +#nodef GIT_ERROR_CHECK_ALLOC_BUF(buf) if (buf == NULL || git_buf_oom(buf)) { __coverity_panic__(); } + +#nodef GITERR_CHECK_ALLOC_ADD(out, one, two) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two)) { __coverity_panic__(); } + +#nodef GITERR_CHECK_ALLOC_ADD3(out, one, two, three) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), three)) { __coverity_panic__(); } + +#nodef GITERR_CHECK_ALLOC_ADD4(out, one, two, three, four) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), four)) { __coverity_panic__(); } + +#nodef GITERR_CHECK_ALLOC_MULTIPLY(out, nelem, elsize) \ + if (GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize)) { __coverity_panic__(); } + +#nodef GIT_ERROR_CHECK_VERSION(S,V,N) if (git_error__check_version(S,V,N) < 0) { __coverity_panic__(); } + +#nodef LOOKS_LIKE_DRIVE_PREFIX(S) (strlen(S) >= 2 && git__isalpha((S)[0]) && (S)[1] == ':') + +#nodef git_vector_foreach(v, iter, elem) \ + for ((iter) = 0; (v)->contents != NULL && (iter) < (v)->length && ((elem) = (v)->contents[(iter)], 1); (iter)++ ) + +#nodef git_vector_rforeach(v, iter, elem) \ + for ((iter) = (v)->length - 1; (v)->contents != NULL && (iter) < SIZE_MAX && ((elem) = (v)->contents[(iter)], 1); (iter)-- ) diff --git a/script/valgrind.sh b/script/valgrind.sh new file mode 100755 index 00000000000..aacd767a7c8 --- /dev/null +++ b/script/valgrind.sh @@ -0,0 +1,2 @@ +#!/bin/bash +exec valgrind --leak-check=full --show-reachable=yes --child-silent-after-fork=yes --error-exitcode=125 --num-callers=50 --suppressions="$(dirname "${BASH_SOURCE[0]}")/valgrind.supp" "$@" diff --git a/script/valgrind.supp b/script/valgrind.supp new file mode 100644 index 00000000000..79e8378f07e --- /dev/null +++ b/script/valgrind.supp @@ -0,0 +1,261 @@ +{ + ignore-zlib-errors-cond + Memcheck:Cond + obj:*libz.so* +} + +{ + ignore-giterror-set-leak + Memcheck:Leak + ... + fun:giterror_set +} + +{ + ignore-git-global-state-leak + Memcheck:Leak + ... + fun:git__global_state +} + +{ + ignore-openssl-ssl-leak + Memcheck:Leak + ... + obj:*libssl.so* + ... +} + +{ + ignore-openssl-crypto-leak + Memcheck:Leak + ... + obj:*libcrypto.so* + ... +} + +{ + ignore-openssl-crypto-cond + Memcheck:Cond + obj:*libcrypto.so* + ... +} + +{ + ignore-openssl-init-leak + Memcheck:Leak + ... + fun:git_openssl_stream_global_init + ... +} + +{ + ignore-openssl-legacy-init-leak + Memcheck:Leak + ... + fun:OPENSSL_init_ssl__legacy + ... +} + +{ + ignore-openssl-malloc-leak + Memcheck:Leak + ... + fun:git_openssl_malloc + ... +} + +{ + ignore-openssl-realloc-leak + Memcheck:Leak + ... + fun:git_openssl_realloc + ... +} + +{ + ignore-glibc-getaddrinfo-cache + Memcheck:Leak + ... + fun:__check_pf +} + +{ + ignore-glibc-getaddrinfo-fn + Memcheck:Leak + ... + fun:getaddrinfo +} + +{ + ignore-curl-global-init + Memcheck:Leak + ... + fun:curl_global_init +} + +{ + ignore-libssh2-init + Memcheck:Leak + ... + fun:gcry_control + fun:libssh2_init + ... +} + +{ + ignore-libssh2-session-create + Memcheck:Leak + ... + fun:_git_ssh_session_create + ... +} + +{ + ignore-libssh2-setup-conn + Memcheck:Leak + ... + fun:_git_ssh_setup_conn + ... +} + +{ + ignore-libssh2-gcrypt-control-leak + Memcheck:Leak + ... + fun:gcry_control + obj:*libssh2.so* +} + +{ + ignore-libssh2-gcrypt-mpinew-leak + Memcheck:Leak + ... + fun:gcry_mpi_new + obj:*libssh2.so* +} + +{ + ignore-libssh2-gcrypt-mpiscan-leak + Memcheck:Leak + ... + fun:gcry_mpi_scan + obj:*libssh2.so* + ... +} + +{ + ignore-libssh2-gcrypt-randomize-leak + Memcheck:Leak + ... + fun:gcry_randomize + obj:*libssh2.so* +} + +{ + ignore-libssh2-gcrypt-sexpfindtoken-leak + Memcheck:Leak + ... + fun:gcry_sexp_find_token + obj:*libssh2.so* +} + +{ + ignore-libssh2-gcrypt-pksign-leak + Memcheck:Leak + ... + fun:gcry_pk_sign + obj:*libssh2.so* +} + +{ + ignore-libssh2-gcrypt-session-handshake + Memcheck:Leak + ... + obj:*libssh2.so* + obj:*libssh2.so* + fun:libssh2_session_handshake + ... +} + +{ + ignore-openssl-undefined-in-read + Memcheck:Cond + ... + obj:*libssl.so* + ... + fun:openssl_read + ... +} + +{ + ignore-openssl-undefined-in-connect + Memcheck:Cond + ... + obj:*libssl.so* + ... + fun:openssl_connect + ... +} + +{ + ignore-openssl-undefined-in-connect + Memcheck:Cond + ... + obj:*libcrypto.so* + ... + fun:openssl_connect + ... +} + +{ + ignore-libssh2-rsa-sha1-sign + Memcheck:Leak + ... + obj:*libgcrypt.so* + fun:_libssh2_rsa_sha1_sign + ... +} + +{ + ignore-libssh2-kexinit + Memcheck:Leak + ... + obj:*libssh2.so* + fun:kexinit + ... +} + +{ + ignore-noai6ai_cached-double-free + Memcheck:Free + fun:free + fun:__libc_freeres + ... + fun:exit + ... +} + +{ + ignore-libcrypto-uninitialized-read-for-entropy + Memcheck:Value8 + ... + obj:*libcrypto.so* + ... +} + +{ + ignore-dlopen-leak + Memcheck:Leak + ... + fun:dlopen + ... +} + +{ + ignore-dlopen-leak + Memcheck:Leak + ... + fun:_dlerror_run + ... +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 00000000000..f76bbecc138 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,170 @@ +# The main libgit2 source tree: this CMakeLists.txt identifies platform +# support and includes the subprojects that make up core libgit2 support. + +# +# Optional build configuration settings +# + +if(DEPRECATE_HARD) + add_definitions(-DGIT_DEPRECATE_HARD) +endif() + +if(DEBUG_LEAK_CHECKER STREQUAL "valgrind") + add_definitions(-DVALGRIND) +endif() + +# +# Optional debugging functionality +# + +if(DEBUG_POOL) + set(GIT_DEBUG_POOL 1) +endif() +add_feature_info("Debug pool" GIT_DEBUG_POOL "debug-mode struct pool allocators") + +if(DEBUG_STRICT_ALLOC) + set(GIT_DEBUG_STRICT_ALLOC 1) +endif() +add_feature_info("Debug alloc" GIT_DEBUG_STRICT_ALLOC "debug-mode strict allocators") + +if(DEBUG_STRICT_OPEN) + set(GIT_DEBUG_STRICT_OPEN 1) +endif() +add_feature_info("Debug open" GIT_DEBUG_STRICT_OPEN "strict path validation in open") + +# +# Optional feature enablement +# + +include(SelectThreads) +include(SelectNsec) +include(SelectHTTPSBackend) +include(SelectHashes) +include(SelectHTTPParser) +include(SelectRegex) +include(SelectXdiff) +include(SelectSSH) +include(SelectCompression) +include(SelectI18n) +include(SelectAuthNTLM) +include(SelectAuthNegotiate) + +# +# Platform support +# + +# futimes/futimens + +if(HAVE_FUTIMENS) + set(GIT_FUTIMENS 1) +endif() + +# qsort + +# old-style FreeBSD qsort_r() has the 'context' parameter as the first argument +# of the comparison function: +check_prototype_definition_safe(qsort_r + "void (qsort_r)(void *base, size_t nmemb, size_t size, void *context, int (*compar)(void *, const void *, const void *))" + "" "stdlib.h" GIT_QSORT_BSD) + +# GNU or POSIX qsort_r() has the 'context' parameter as the last argument of the +# comparison function: +check_prototype_definition_safe(qsort_r + "void (qsort_r)(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *context)" + "" "stdlib.h" GIT_QSORT_GNU) + +# C11 qsort_s() has the 'context' parameter as the last argument of the +# comparison function, and returns an error status: +check_prototype_definition_safe(qsort_s + "errno_t (qsort_s)(void *base, rsize_t nmemb, rsize_t size, int (*compar)(const void *, const void *, void *), void *context)" + "0" "stdlib.h" GIT_QSORT_C11) + +# MSC qsort_s() has the 'context' parameter as the first argument of the +# comparison function, and as the last argument of qsort_s(): +check_prototype_definition_safe(qsort_s + "void (qsort_s)(void *base, size_t num, size_t width, int (*compare )(void *, const void *, const void *), void *context)" + "" "stdlib.h" GIT_QSORT_MSC) + +# random / entropy data + +check_symbol_exists(getentropy unistd.h GIT_RAND_GETENTROPY) +check_symbol_exists(getloadavg stdlib.h GIT_RAND_GETLOADAVG) + +# poll + +if(WIN32) + set(GIT_IO_WSAPOLL 1) +else() + check_symbol_exists(poll poll.h GIT_IO_POLL) + check_symbol_exists(select sys/select.h GIT_IO_SELECT) +endif() + +# determine architecture of the machine + +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(GIT_ARCH_64 1) +elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) + set(GIT_ARCH_32 1) +elseif(CMAKE_SIZEOF_VOID_P) + message(FATAL_ERROR "Unsupported architecture (pointer size is ${CMAKE_SIZEOF_VOID_P} bytes)") +else() + message(FATAL_ERROR "Unsupported architecture (CMAKE_SIZEOF_VOID_P is unset)") +endif() + +# realtime support + +check_library_exists(rt clock_gettime "time.h" NEED_LIBRT) + +if(NEED_LIBRT AND NOT CMAKE_SYSTEM_NAME MATCHES "iOS") + list(APPEND LIBGIT2_SYSTEM_LIBS rt) + list(APPEND LIBGIT2_PC_LIBS "-lrt") +endif() + +# platform libraries + +if(WIN32) + list(APPEND LIBGIT2_SYSTEM_LIBS "ws2_32" "secur32") + list(APPEND LIBGIT2_PC_LIBS "-lws2_32" "-lsecur32") +endif() + +if(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") + list(APPEND LIBGIT2_SYSTEM_LIBS socket nsl) + list(APPEND LIBGIT2_PC_LIBS "-lsocket" "-lnsl") +endif() + +if(CMAKE_SYSTEM_NAME MATCHES "Haiku") + list(APPEND LIBGIT2_SYSTEM_LIBS gnu network) + list(APPEND LIBGIT2_PC_LIBS "-lgnu -lnetwork") +endif() + +if(AMIGA) + add_definitions(-DNO_ADDRINFO -DNO_READDIR_R -DNO_MMAP) +endif() + +# +# Set build time information +# + +set(GIT_BUILD_CPU "${CMAKE_SYSTEM_PROCESSOR}") +execute_process(COMMAND git rev-parse HEAD + OUTPUT_VARIABLE GIT_BUILD_COMMIT OUTPUT_STRIP_TRAILING_WHITESPACE) + +# +# Include child projects +# + +add_subdirectory(libgit2) +add_subdirectory(util) + +if(BUILD_CLI AND NOT CMAKE_SYSTEM_NAME MATCHES "iOS") + add_subdirectory(cli) +endif() + +# re-export these to the root so that peer projects (tests, fuzzers, +# examples) can use them +set(LIBGIT2_INCLUDES ${LIBGIT2_INCLUDES} PARENT_SCOPE) +set(LIBGIT2_OBJECTS ${LIBGIT2_OBJECTS} PARENT_SCOPE) +set(LIBGIT2_DEPENDENCY_INCLUDES ${LIBGIT2_DEPENDENCY_INCLUDES} PARENT_SCOPE) +set(LIBGIT2_DEPENDENCY_OBJECTS ${LIBGIT2_DEPENDENCY_OBJECTS} PARENT_SCOPE) +set(LIBGIT2_SYSTEM_INCLUDES ${LIBGIT2_SYSTEM_INCLUDES} PARENT_SCOPE) +set(LIBGIT2_SYSTEM_LIBS ${LIBGIT2_SYSTEM_LIBS} PARENT_SCOPE) diff --git a/src/README.md b/src/README.md new file mode 100644 index 00000000000..10b86c1dcf0 --- /dev/null +++ b/src/README.md @@ -0,0 +1,12 @@ +# libgit2 sources + +This is the source that makes up the core of libgit2 and its related +projects. + +* `cli` + A git-compatible command-line interface that uses libgit2. +* `libgit2` + This is the libgit2 project, a cross-platform, linkable library + implementation of Git that you can use in your application. +* `util` + A shared utility library for these projects. diff --git a/src/amiga/map.c b/src/amiga/map.c deleted file mode 100644 index 0ba7995c6c8..00000000000 --- a/src/amiga/map.c +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include - -#ifndef GIT_WIN32 - -#include "posix.h" -#include "map.h" -#include - -int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset) -{ - GIT_MMAP_VALIDATE(out, len, prot, flags); - - out->data = NULL; - out->len = 0; - - if ((prot & GIT_PROT_WRITE) && ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)) { - giterr_set(GITERR_OS, "Trying to map shared-writeable"); - return -1; - } - - out->data = malloc(len); - GITERR_CHECK_ALLOC(out->data); - - if ((p_lseek(fd, offset, SEEK_SET) < 0) || ((size_t)p_read(fd, out->data, len) != len)) { - giterr_set(GITERR_OS, "mmap emulation failed"); - return -1; - } - - out->len = len; - return 0; -} - -int p_munmap(git_map *map) -{ - assert(map != NULL); - free(map->data); - - return 0; -} - -#endif - diff --git a/src/attr.c b/src/attr.c deleted file mode 100644 index a1d9932e907..00000000000 --- a/src/attr.c +++ /dev/null @@ -1,713 +0,0 @@ -#include "repository.h" -#include "fileops.h" -#include "config.h" -#include "git2/oid.h" -#include - -GIT__USE_STRMAP; - -const char *git_attr__true = "[internal]__TRUE__"; -const char *git_attr__false = "[internal]__FALSE__"; -const char *git_attr__unset = "[internal]__UNSET__"; - -git_attr_t git_attr_value(const char *attr) -{ - if (attr == NULL || attr == git_attr__unset) - return GIT_ATTR_UNSPECIFIED_T; - - if (attr == git_attr__true) - return GIT_ATTR_TRUE_T; - - if (attr == git_attr__false) - return GIT_ATTR_FALSE_T; - - return GIT_ATTR_VALUE_T; -} - - -static int collect_attr_files( - git_repository *repo, - uint32_t flags, - const char *path, - git_vector *files); - - -int git_attr_get( - const char **value, - git_repository *repo, - uint32_t flags, - const char *pathname, - const char *name) -{ - int error; - git_attr_path path; - git_vector files = GIT_VECTOR_INIT; - size_t i, j; - git_attr_file *file; - git_attr_name attr; - git_attr_rule *rule; - - *value = NULL; - - if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0) - return -1; - - if ((error = collect_attr_files(repo, flags, pathname, &files)) < 0) - goto cleanup; - - attr.name = name; - attr.name_hash = git_attr_file__name_hash(name); - - git_vector_foreach(&files, i, file) { - - git_attr_file__foreach_matching_rule(file, &path, j, rule) { - size_t pos; - - if (!git_vector_bsearch(&pos, &rule->assigns, &attr)) { - *value = ((git_attr_assignment *)git_vector_get( - &rule->assigns, pos))->value; - goto cleanup; - } - } - } - -cleanup: - git_vector_free(&files); - git_attr_path__free(&path); - - return error; -} - - -typedef struct { - git_attr_name name; - git_attr_assignment *found; -} attr_get_many_info; - -int git_attr_get_many( - const char **values, - git_repository *repo, - uint32_t flags, - const char *pathname, - size_t num_attr, - const char **names) -{ - int error; - git_attr_path path; - git_vector files = GIT_VECTOR_INIT; - size_t i, j, k; - git_attr_file *file; - git_attr_rule *rule; - attr_get_many_info *info = NULL; - size_t num_found = 0; - - memset((void *)values, 0, sizeof(const char *) * num_attr); - - if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0) - return -1; - - if ((error = collect_attr_files(repo, flags, pathname, &files)) < 0) - goto cleanup; - - info = git__calloc(num_attr, sizeof(attr_get_many_info)); - GITERR_CHECK_ALLOC(info); - - git_vector_foreach(&files, i, file) { - - git_attr_file__foreach_matching_rule(file, &path, j, rule) { - - for (k = 0; k < num_attr; k++) { - size_t pos; - - if (info[k].found != NULL) /* already found assignment */ - continue; - - if (!info[k].name.name) { - info[k].name.name = names[k]; - info[k].name.name_hash = git_attr_file__name_hash(names[k]); - } - - if (!git_vector_bsearch(&pos, &rule->assigns, &info[k].name)) { - info[k].found = (git_attr_assignment *) - git_vector_get(&rule->assigns, pos); - values[k] = info[k].found->value; - - if (++num_found == num_attr) - goto cleanup; - } - } - } - } - -cleanup: - git_vector_free(&files); - git_attr_path__free(&path); - git__free(info); - - return error; -} - - -int git_attr_foreach( - git_repository *repo, - uint32_t flags, - const char *pathname, - int (*callback)(const char *name, const char *value, void *payload), - void *payload) -{ - int error; - git_attr_path path; - git_vector files = GIT_VECTOR_INIT; - size_t i, j, k; - git_attr_file *file; - git_attr_rule *rule; - git_attr_assignment *assign; - git_strmap *seen = NULL; - - if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0) - return -1; - - if ((error = collect_attr_files(repo, flags, pathname, &files)) < 0) - goto cleanup; - - seen = git_strmap_alloc(); - GITERR_CHECK_ALLOC(seen); - - git_vector_foreach(&files, i, file) { - - git_attr_file__foreach_matching_rule(file, &path, j, rule) { - - git_vector_foreach(&rule->assigns, k, assign) { - /* skip if higher priority assignment was already seen */ - if (git_strmap_exists(seen, assign->name)) - continue; - - git_strmap_insert(seen, assign->name, assign, error); - if (error < 0) - goto cleanup; - - error = callback(assign->name, assign->value, payload); - if (error) { - giterr_clear(); - error = GIT_EUSER; - goto cleanup; - } - } - } - } - -cleanup: - git_strmap_free(seen); - git_vector_free(&files); - git_attr_path__free(&path); - - return error; -} - - -int git_attr_add_macro( - git_repository *repo, - const char *name, - const char *values) -{ - int error; - git_attr_rule *macro = NULL; - git_pool *pool; - - if (git_attr_cache__init(repo) < 0) - return -1; - - macro = git__calloc(1, sizeof(git_attr_rule)); - GITERR_CHECK_ALLOC(macro); - - pool = &git_repository_attr_cache(repo)->pool; - - macro->match.pattern = git_pool_strdup(pool, name); - GITERR_CHECK_ALLOC(macro->match.pattern); - - macro->match.length = strlen(macro->match.pattern); - macro->match.flags = GIT_ATTR_FNMATCH_MACRO; - - error = git_attr_assignment__parse(repo, pool, ¯o->assigns, &values); - - if (!error) - error = git_attr_cache__insert_macro(repo, macro); - - if (error < 0) - git_attr_rule__free(macro); - - return error; -} - -bool git_attr_cache__is_cached( - git_repository *repo, git_attr_file_source source, const char *path) -{ - git_buf cache_key = GIT_BUF_INIT; - git_strmap *files = git_repository_attr_cache(repo)->files; - const char *workdir = git_repository_workdir(repo); - bool rval; - - if (workdir && git__prefixcmp(path, workdir) == 0) - path += strlen(workdir); - if (git_buf_printf(&cache_key, "%d#%s", (int)source, path) < 0) - return false; - - rval = git_strmap_exists(files, git_buf_cstr(&cache_key)); - - git_buf_free(&cache_key); - - return rval; -} - -static int load_attr_file( - const char **data, - git_futils_filestamp *stamp, - const char *filename) -{ - int error; - git_buf content = GIT_BUF_INIT; - - error = git_futils_filestamp_check(stamp, filename); - if (error < 0) - return error; - - /* if error == 0, then file is up to date. By returning GIT_ENOTFOUND, - * we tell the caller not to reparse this file... - */ - if (!error) - return GIT_ENOTFOUND; - - error = git_futils_readbuffer(&content, filename); - if (error < 0) - return error; - - *data = git_buf_detach(&content); - - return 0; -} - -static int load_attr_blob_from_index( - const char **content, - git_blob **blob, - git_repository *repo, - const git_oid *old_oid, - const char *relfile) -{ - int error; - size_t pos; - git_index *index; - const git_index_entry *entry; - - if ((error = git_repository_index__weakptr(&index, repo)) < 0 || - (error = git_index_find(&pos, index, relfile)) < 0) - return error; - - entry = git_index_get_byindex(index, pos); - - if (old_oid && git_oid_cmp(old_oid, &entry->oid) == 0) - return GIT_ENOTFOUND; - - if ((error = git_blob_lookup(blob, repo, &entry->oid)) < 0) - return error; - - *content = git_blob_rawcontent(*blob); - return 0; -} - -static int load_attr_from_cache( - git_attr_file **file, - git_attr_cache *cache, - git_attr_file_source source, - const char *relative_path) -{ - git_buf cache_key = GIT_BUF_INIT; - khiter_t cache_pos; - - *file = NULL; - - if (!cache || !cache->files) - return 0; - - if (git_buf_printf(&cache_key, "%d#%s", (int)source, relative_path) < 0) - return -1; - - cache_pos = git_strmap_lookup_index(cache->files, cache_key.ptr); - - git_buf_free(&cache_key); - - if (git_strmap_valid_index(cache->files, cache_pos)) - *file = git_strmap_value_at(cache->files, cache_pos); - - return 0; -} - -int git_attr_cache__internal_file( - git_repository *repo, - const char *filename, - git_attr_file **file) -{ - int error = 0; - git_attr_cache *cache = git_repository_attr_cache(repo); - khiter_t cache_pos = git_strmap_lookup_index(cache->files, filename); - - if (git_strmap_valid_index(cache->files, cache_pos)) { - *file = git_strmap_value_at(cache->files, cache_pos); - return 0; - } - - if (git_attr_file__new(file, 0, filename, &cache->pool) < 0) - return -1; - - git_strmap_insert(cache->files, (*file)->key + 2, *file, error); - if (error > 0) - error = 0; - - return error; -} - -int git_attr_cache__push_file( - git_repository *repo, - const char *base, - const char *filename, - git_attr_file_source source, - git_attr_file_parser parse, - void* parsedata, - git_vector *stack) -{ - int error = 0; - git_buf path = GIT_BUF_INIT; - const char *workdir = git_repository_workdir(repo); - const char *relfile, *content = NULL; - git_attr_cache *cache = git_repository_attr_cache(repo); - git_attr_file *file = NULL; - git_blob *blob = NULL; - git_futils_filestamp stamp; - - assert(filename && stack); - - /* join base and path as needed */ - if (base != NULL && git_path_root(filename) < 0) { - if (git_buf_joinpath(&path, base, filename) < 0) - return -1; - filename = path.ptr; - } - - relfile = filename; - if (workdir && git__prefixcmp(relfile, workdir) == 0) - relfile += strlen(workdir); - - /* check cache */ - if (load_attr_from_cache(&file, cache, source, relfile) < 0) - return -1; - - /* if not in cache, load data, parse, and cache */ - - if (source == GIT_ATTR_FILE_FROM_FILE) { - git_futils_filestamp_set( - &stamp, file ? &file->cache_data.stamp : NULL); - - error = load_attr_file(&content, &stamp, filename); - } else { - error = load_attr_blob_from_index(&content, &blob, - repo, file ? &file->cache_data.oid : NULL, relfile); - } - - if (error) { - /* not finding a file is not an error for this function */ - if (error == GIT_ENOTFOUND) { - giterr_clear(); - error = 0; - } - goto finish; - } - - /* if we got here, we have to parse and/or reparse the file */ - if (file) - git_attr_file__clear_rules(file); - else { - error = git_attr_file__new(&file, source, relfile, &cache->pool); - if (error < 0) - goto finish; - } - - if (parse && (error = parse(repo, parsedata, content, file)) < 0) - goto finish; - - git_strmap_insert(cache->files, file->key, file, error); //-V595 - if (error > 0) - error = 0; - - /* remember "cache buster" file signature */ - if (blob) - git_oid_cpy(&file->cache_data.oid, git_object_id((git_object *)blob)); - else - git_futils_filestamp_set(&file->cache_data.stamp, &stamp); - -finish: - /* push file onto vector if we found one*/ - if (!error && file != NULL) - error = git_vector_insert(stack, file); - - if (error != 0) - git_attr_file__free(file); - - if (blob) - git_blob_free(blob); - else - git__free((void *)content); - - git_buf_free(&path); - - return error; -} - -#define push_attr_file(R,S,B,F) \ - git_attr_cache__push_file((R),(B),(F),GIT_ATTR_FILE_FROM_FILE,git_attr_file__parse_buffer,NULL,(S)) - -typedef struct { - git_repository *repo; - uint32_t flags; - const char *workdir; - git_index *index; - git_vector *files; -} attr_walk_up_info; - -int git_attr_cache__decide_sources( - uint32_t flags, bool has_wd, bool has_index, git_attr_file_source *srcs) -{ - int count = 0; - - switch (flags & 0x03) { - case GIT_ATTR_CHECK_FILE_THEN_INDEX: - if (has_wd) - srcs[count++] = GIT_ATTR_FILE_FROM_FILE; - if (has_index) - srcs[count++] = GIT_ATTR_FILE_FROM_INDEX; - break; - case GIT_ATTR_CHECK_INDEX_THEN_FILE: - if (has_index) - srcs[count++] = GIT_ATTR_FILE_FROM_INDEX; - if (has_wd) - srcs[count++] = GIT_ATTR_FILE_FROM_FILE; - break; - case GIT_ATTR_CHECK_INDEX_ONLY: - if (has_index) - srcs[count++] = GIT_ATTR_FILE_FROM_INDEX; - break; - } - - return count; -} - -static int push_one_attr(void *ref, git_buf *path) -{ - int error = 0, n_src, i; - attr_walk_up_info *info = (attr_walk_up_info *)ref; - git_attr_file_source src[2]; - - n_src = git_attr_cache__decide_sources( - info->flags, info->workdir != NULL, info->index != NULL, src); - - for (i = 0; !error && i < n_src; ++i) - error = git_attr_cache__push_file( - info->repo, path->ptr, GIT_ATTR_FILE, src[i], - git_attr_file__parse_buffer, NULL, info->files); - - return error; -} - -static int collect_attr_files( - git_repository *repo, - uint32_t flags, - const char *path, - git_vector *files) -{ - int error; - git_buf dir = GIT_BUF_INIT; - const char *workdir = git_repository_workdir(repo); - attr_walk_up_info info; - - if (git_attr_cache__init(repo) < 0 || - git_vector_init(files, 4, NULL) < 0) - return -1; - - /* Resolve path in a non-bare repo */ - if (workdir != NULL) - error = git_path_find_dir(&dir, path, workdir); - else - error = git_path_dirname_r(&dir, path); - if (error < 0) - goto cleanup; - - /* in precendence order highest to lowest: - * - $GIT_DIR/info/attributes - * - path components with .gitattributes - * - config core.attributesfile - * - $GIT_PREFIX/etc/gitattributes - */ - - error = push_attr_file( - repo, files, git_repository_path(repo), GIT_ATTR_FILE_INREPO); - if (error < 0) - goto cleanup; - - info.repo = repo; - info.flags = flags; - info.workdir = workdir; - if (git_repository_index__weakptr(&info.index, repo) < 0) - giterr_clear(); /* no error even if there is no index */ - info.files = files; - - error = git_path_walk_up(&dir, workdir, push_one_attr, &info); - if (error < 0) - goto cleanup; - - if (git_repository_attr_cache(repo)->cfg_attr_file != NULL) { - error = push_attr_file( - repo, files, NULL, git_repository_attr_cache(repo)->cfg_attr_file); - if (error < 0) - goto cleanup; - } - - if ((flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) { - error = git_futils_find_system_file(&dir, GIT_ATTR_FILE_SYSTEM); - if (!error) - error = push_attr_file(repo, files, NULL, dir.ptr); - else if (error == GIT_ENOTFOUND) { - giterr_clear(); - error = 0; - } - } - - cleanup: - if (error < 0) - git_vector_free(files); - git_buf_free(&dir); - - return error; -} - -static char *try_global_default(const char *relpath) -{ - git_buf dflt = GIT_BUF_INIT; - char *rval = NULL; - - if (!git_futils_find_global_file(&dflt, relpath)) - rval = git_buf_detach(&dflt); - - git_buf_free(&dflt); - - return rval; -} - -int git_attr_cache__init(git_repository *repo) -{ - int ret; - git_attr_cache *cache = git_repository_attr_cache(repo); - git_config *cfg; - - if (cache->initialized) - return 0; - - /* cache config settings for attributes and ignores */ - if (git_repository_config__weakptr(&cfg, repo) < 0) - return -1; - - ret = git_config_get_string(&cache->cfg_attr_file, cfg, GIT_ATTR_CONFIG); - if (ret < 0 && ret != GIT_ENOTFOUND) - return ret; - if (ret == GIT_ENOTFOUND) - cache->cfg_attr_file = try_global_default(GIT_ATTR_CONFIG_DEFAULT); - - ret = git_config_get_string(&cache->cfg_excl_file, cfg, GIT_IGNORE_CONFIG); - if (ret < 0 && ret != GIT_ENOTFOUND) - return ret; - if (ret == GIT_ENOTFOUND) - cache->cfg_excl_file = try_global_default(GIT_IGNORE_CONFIG_DEFAULT); - - giterr_clear(); - - /* allocate hashtable for attribute and ignore file contents */ - if (cache->files == NULL) { - cache->files = git_strmap_alloc(); - GITERR_CHECK_ALLOC(cache->files); - } - - /* allocate hashtable for attribute macros */ - if (cache->macros == NULL) { - cache->macros = git_strmap_alloc(); - GITERR_CHECK_ALLOC(cache->macros); - } - - /* allocate string pool */ - if (git_pool_init(&cache->pool, 1, 0) < 0) - return -1; - - cache->initialized = 1; - - /* insert default macros */ - return git_attr_add_macro(repo, "binary", "-diff -crlf -text"); -} - -void git_attr_cache_flush( - git_repository *repo) -{ - git_attr_cache *cache; - - if (!repo) - return; - - cache = git_repository_attr_cache(repo); - - if (cache->files != NULL) { - git_attr_file *file; - - git_strmap_foreach_value(cache->files, file, { - git_attr_file__free(file); - }); - - git_strmap_free(cache->files); - } - - if (cache->macros != NULL) { - git_attr_rule *rule; - - git_strmap_foreach_value(cache->macros, rule, { - git_attr_rule__free(rule); - }); - - git_strmap_free(cache->macros); - } - - git_pool_clear(&cache->pool); - - cache->initialized = 0; -} - -int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro) -{ - git_strmap *macros = git_repository_attr_cache(repo)->macros; - int error; - - /* TODO: generate warning log if (macro->assigns.length == 0) */ - if (macro->assigns.length == 0) - return 0; - - git_strmap_insert(macros, macro->match.pattern, macro, error); - return (error < 0) ? -1 : 0; -} - -git_attr_rule *git_attr_cache__lookup_macro( - git_repository *repo, const char *name) -{ - git_strmap *macros = git_repository_attr_cache(repo)->macros; - khiter_t pos; - - pos = git_strmap_lookup_index(macros, name); - - if (!git_strmap_valid_index(macros, pos)) - return NULL; - - return (git_attr_rule *)git_strmap_value_at(macros, pos); -} - diff --git a/src/attr.h b/src/attr.h deleted file mode 100644 index 0fc33089b89..00000000000 --- a/src/attr.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_attr_h__ -#define INCLUDE_attr_h__ - -#include "attr_file.h" -#include "strmap.h" - -#define GIT_ATTR_CONFIG "core.attributesfile" -#define GIT_ATTR_CONFIG_DEFAULT ".config/git/attributes" -#define GIT_IGNORE_CONFIG "core.excludesfile" -#define GIT_IGNORE_CONFIG_DEFAULT ".config/git/ignore" - -typedef struct { - int initialized; - git_pool pool; - git_strmap *files; /* hash path to git_attr_file of rules */ - git_strmap *macros; /* hash name to vector */ - const char *cfg_attr_file; /* cached value of core.attributesfile */ - const char *cfg_excl_file; /* cached value of core.excludesfile */ -} git_attr_cache; - -typedef int (*git_attr_file_parser)( - git_repository *, void *, const char *, git_attr_file *); - -extern int git_attr_cache__init(git_repository *repo); - -extern int git_attr_cache__insert_macro( - git_repository *repo, git_attr_rule *macro); - -extern git_attr_rule *git_attr_cache__lookup_macro( - git_repository *repo, const char *name); - -extern int git_attr_cache__push_file( - git_repository *repo, - const char *base, - const char *filename, - git_attr_file_source source, - git_attr_file_parser parse, - void *parsedata, /* passed through to parse function */ - git_vector *stack); - -extern int git_attr_cache__internal_file( - git_repository *repo, - const char *key, - git_attr_file **file_ptr); - -/* returns true if path is in cache */ -extern bool git_attr_cache__is_cached( - git_repository *repo, git_attr_file_source source, const char *path); - -extern int git_attr_cache__decide_sources( - uint32_t flags, bool has_wd, bool has_index, git_attr_file_source *srcs); - -#endif diff --git a/src/attr_file.c b/src/attr_file.c deleted file mode 100644 index 628cb1544b9..00000000000 --- a/src/attr_file.c +++ /dev/null @@ -1,596 +0,0 @@ -#include "common.h" -#include "repository.h" -#include "filebuf.h" -#include "git2/blob.h" -#include "git2/tree.h" -#include - -static int sort_by_hash_and_name(const void *a_raw, const void *b_raw); -static void git_attr_rule__clear(git_attr_rule *rule); - -int git_attr_file__new( - git_attr_file **attrs_ptr, - git_attr_file_source from, - const char *path, - git_pool *pool) -{ - git_attr_file *attrs = NULL; - - attrs = git__calloc(1, sizeof(git_attr_file)); - GITERR_CHECK_ALLOC(attrs); - - if (pool) - attrs->pool = pool; - else { - attrs->pool = git__calloc(1, sizeof(git_pool)); - if (!attrs->pool || git_pool_init(attrs->pool, 1, 0) < 0) - goto fail; - attrs->pool_is_allocated = true; - } - - if (path) { - size_t len = strlen(path); - - attrs->key = git_pool_malloc(attrs->pool, (uint32_t)len + 3); - GITERR_CHECK_ALLOC(attrs->key); - - attrs->key[0] = '0' + from; - attrs->key[1] = '#'; - memcpy(&attrs->key[2], path, len); - attrs->key[len + 2] = '\0'; - } - - if (git_vector_init(&attrs->rules, 4, NULL) < 0) - goto fail; - - *attrs_ptr = attrs; - return 0; - -fail: - git_attr_file__free(attrs); - attrs_ptr = NULL; - return -1; -} - -int git_attr_file__parse_buffer( - git_repository *repo, void *parsedata, const char *buffer, git_attr_file *attrs) -{ - int error = 0; - const char *scan = NULL; - char *context = NULL; - git_attr_rule *rule = NULL; - - GIT_UNUSED(parsedata); - - assert(buffer && attrs); - - scan = buffer; - - /* if subdir file path, convert context for file paths */ - if (attrs->key && git__suffixcmp(attrs->key, "/" GIT_ATTR_FILE) == 0) { - context = attrs->key + 2; - context[strlen(context) - strlen(GIT_ATTR_FILE)] = '\0'; - } - - while (!error && *scan) { - /* allocate rule if needed */ - if (!rule && !(rule = git__calloc(1, sizeof(git_attr_rule)))) { - error = -1; - break; - } - - /* parse the next "pattern attr attr attr" line */ - if (!(error = git_attr_fnmatch__parse( - &rule->match, attrs->pool, context, &scan)) && - !(error = git_attr_assignment__parse( - repo, attrs->pool, &rule->assigns, &scan))) - { - if (rule->match.flags & GIT_ATTR_FNMATCH_MACRO) - /* should generate error/warning if this is coming from any - * file other than .gitattributes at repo root. - */ - error = git_attr_cache__insert_macro(repo, rule); - else - error = git_vector_insert(&attrs->rules, rule); - } - - /* if the rule wasn't a pattern, on to the next */ - if (error < 0) { - git_attr_rule__clear(rule); /* reset rule contents */ - if (error == GIT_ENOTFOUND) - error = 0; - } else { - rule = NULL; /* vector now "owns" the rule */ - } - } - - git_attr_rule__free(rule); - - /* restore file path used for context */ - if (context) - context[strlen(context)] = '.'; /* first char of GIT_ATTR_FILE */ - - return error; -} - -int git_attr_file__new_and_load( - git_attr_file **attrs_ptr, - const char *path) -{ - int error; - git_buf content = GIT_BUF_INIT; - - if ((error = git_attr_file__new(attrs_ptr, 0, path, NULL)) < 0) - return error; - - if (!(error = git_futils_readbuffer(&content, path))) - error = git_attr_file__parse_buffer( - NULL, NULL, git_buf_cstr(&content), *attrs_ptr); - - git_buf_free(&content); - - if (error) { - git_attr_file__free(*attrs_ptr); - *attrs_ptr = NULL; - } - - return error; -} - -void git_attr_file__clear_rules(git_attr_file *file) -{ - unsigned int i; - git_attr_rule *rule; - - git_vector_foreach(&file->rules, i, rule) - git_attr_rule__free(rule); - - git_vector_free(&file->rules); -} - -void git_attr_file__free(git_attr_file *file) -{ - if (!file) - return; - - git_attr_file__clear_rules(file); - - if (file->pool_is_allocated) { - git_pool_clear(file->pool); - git__free(file->pool); - } - file->pool = NULL; - - git__free(file); -} - -uint32_t git_attr_file__name_hash(const char *name) -{ - uint32_t h = 5381; - int c; - assert(name); - while ((c = (int)*name++) != 0) - h = ((h << 5) + h) + c; - return h; -} - - -int git_attr_file__lookup_one( - git_attr_file *file, - const git_attr_path *path, - const char *attr, - const char **value) -{ - size_t i; - git_attr_name name; - git_attr_rule *rule; - - *value = NULL; - - name.name = attr; - name.name_hash = git_attr_file__name_hash(attr); - - git_attr_file__foreach_matching_rule(file, path, i, rule) { - size_t pos; - - if (!git_vector_bsearch(&pos, &rule->assigns, &name)) { - *value = ((git_attr_assignment *) - git_vector_get(&rule->assigns, pos))->value; - break; - } - } - - return 0; -} - - -bool git_attr_fnmatch__match( - git_attr_fnmatch *match, - const git_attr_path *path) -{ - int fnm; - int icase_flags = (match->flags & GIT_ATTR_FNMATCH_ICASE) ? FNM_CASEFOLD : 0; - - if (match->flags & GIT_ATTR_FNMATCH_DIRECTORY && !path->is_dir) - return false; - - if (match->flags & GIT_ATTR_FNMATCH_FULLPATH) - fnm = p_fnmatch(match->pattern, path->path, FNM_PATHNAME | icase_flags); - else if (path->is_dir) - fnm = p_fnmatch(match->pattern, path->basename, FNM_LEADING_DIR | icase_flags); - else - fnm = p_fnmatch(match->pattern, path->basename, icase_flags); - - return (fnm == FNM_NOMATCH) ? false : true; -} - -bool git_attr_rule__match( - git_attr_rule *rule, - const git_attr_path *path) -{ - bool matched = git_attr_fnmatch__match(&rule->match, path); - - if (rule->match.flags & GIT_ATTR_FNMATCH_NEGATIVE) - matched = !matched; - - return matched; -} - - -git_attr_assignment *git_attr_rule__lookup_assignment( - git_attr_rule *rule, const char *name) -{ - size_t pos; - git_attr_name key; - key.name = name; - key.name_hash = git_attr_file__name_hash(name); - - if (git_vector_bsearch(&pos, &rule->assigns, &key)) - return NULL; - - return git_vector_get(&rule->assigns, pos); -} - -int git_attr_path__init( - git_attr_path *info, const char *path, const char *base) -{ - ssize_t root; - - /* build full path as best we can */ - git_buf_init(&info->full, 0); - - if (git_path_join_unrooted(&info->full, path, base, &root) < 0) - return -1; - - info->path = info->full.ptr + root; - - /* remove trailing slashes */ - while (info->full.size > 0) { - if (info->full.ptr[info->full.size - 1] != '/') - break; - info->full.size--; - } - info->full.ptr[info->full.size] = '\0'; - - /* skip leading slashes in path */ - while (*info->path == '/') - info->path++; - - /* find trailing basename component */ - info->basename = strrchr(info->path, '/'); - if (info->basename) - info->basename++; - if (!info->basename || !*info->basename) - info->basename = info->path; - - info->is_dir = (int)git_path_isdir(info->full.ptr); - - return 0; -} - -void git_attr_path__free(git_attr_path *info) -{ - git_buf_free(&info->full); - info->path = NULL; - info->basename = NULL; -} - - -/* - * From gitattributes(5): - * - * Patterns have the following format: - * - * - A blank line matches no files, so it can serve as a separator for - * readability. - * - * - A line starting with # serves as a comment. - * - * - An optional prefix ! which negates the pattern; any matching file - * excluded by a previous pattern will become included again. If a negated - * pattern matches, this will override lower precedence patterns sources. - * - * - If the pattern ends with a slash, it is removed for the purpose of the - * following description, but it would only find a match with a directory. In - * other words, foo/ will match a directory foo and paths underneath it, but - * will not match a regular file or a symbolic link foo (this is consistent - * with the way how pathspec works in general in git). - * - * - If the pattern does not contain a slash /, git treats it as a shell glob - * pattern and checks for a match against the pathname without leading - * directories. - * - * - Otherwise, git treats the pattern as a shell glob suitable for consumption - * by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will - * not match a / in the pathname. For example, "Documentation/\*.html" matches - * "Documentation/git.html" but not "Documentation/ppc/ppc.html". A leading - * slash matches the beginning of the pathname; for example, "/\*.c" matches - * "cat-file.c" but not "mozilla-sha1/sha1.c". - */ - -/* - * This will return 0 if the spec was filled out, - * GIT_ENOTFOUND if the fnmatch does not require matching, or - * another error code there was an actual problem. - */ -int git_attr_fnmatch__parse( - git_attr_fnmatch *spec, - git_pool *pool, - const char *source, - const char **base) -{ - const char *pattern, *scan; - int slash_count, allow_space; - - assert(spec && base && *base); - - spec->flags = (spec->flags & GIT_ATTR_FNMATCH_ALLOWSPACE); - allow_space = (spec->flags != 0); - - pattern = *base; - - while (git__isspace(*pattern)) pattern++; - if (!*pattern || *pattern == '#') { - *base = git__next_line(pattern); - return GIT_ENOTFOUND; - } - - if (*pattern == '[') { - if (strncmp(pattern, "[attr]", 6) == 0) { - spec->flags = spec->flags | GIT_ATTR_FNMATCH_MACRO; - pattern += 6; - } - /* else a character range like [a-e]* which is accepted */ - } - - if (*pattern == '!') { - spec->flags = spec->flags | GIT_ATTR_FNMATCH_NEGATIVE; - pattern++; - } - - slash_count = 0; - for (scan = pattern; *scan != '\0'; ++scan) { - /* scan until (non-escaped) white space */ - if (git__isspace(*scan) && *(scan - 1) != '\\') { - if (!allow_space || (*scan != ' ' && *scan != '\t')) - break; - } - - if (*scan == '/') { - spec->flags = spec->flags | GIT_ATTR_FNMATCH_FULLPATH; - slash_count++; - if (pattern == scan) - pattern++; - } - /* remember if we see an unescaped wildcard in pattern */ - else if (git__iswildcard(*scan) && - (scan == pattern || (*(scan - 1) != '\\'))) - spec->flags = spec->flags | GIT_ATTR_FNMATCH_HASWILD; - } - - *base = scan; - - spec->length = scan - pattern; - - if (pattern[spec->length - 1] == '/') { - spec->length--; - spec->flags = spec->flags | GIT_ATTR_FNMATCH_DIRECTORY; - if (--slash_count <= 0) - spec->flags = spec->flags & ~GIT_ATTR_FNMATCH_FULLPATH; - } - - if ((spec->flags & GIT_ATTR_FNMATCH_FULLPATH) != 0 && - source != NULL && git_path_root(pattern) < 0) - { - size_t sourcelen = strlen(source); - /* given an unrooted fullpath match from a file inside a repo, - * prefix the pattern with the relative directory of the source file - */ - spec->pattern = git_pool_malloc( - pool, (uint32_t)(sourcelen + spec->length + 1)); - if (spec->pattern) { - memcpy(spec->pattern, source, sourcelen); - memcpy(spec->pattern + sourcelen, pattern, spec->length); - spec->length += sourcelen; - spec->pattern[spec->length] = '\0'; - } - } else { - spec->pattern = git_pool_strndup(pool, pattern, spec->length); - } - - if (!spec->pattern) { - *base = git__next_line(pattern); - return -1; - } else { - /* strip '\' that might have be used for internal whitespace */ - spec->length = git__unescape(spec->pattern); - } - - return 0; -} - -static int sort_by_hash_and_name(const void *a_raw, const void *b_raw) -{ - const git_attr_name *a = a_raw; - const git_attr_name *b = b_raw; - - if (b->name_hash < a->name_hash) - return 1; - else if (b->name_hash > a->name_hash) - return -1; - else - return strcmp(b->name, a->name); -} - -static void git_attr_assignment__free(git_attr_assignment *assign) -{ - /* name and value are stored in a git_pool associated with the - * git_attr_file, so they do not need to be freed here - */ - assign->name = NULL; - assign->value = NULL; - git__free(assign); -} - -static int merge_assignments(void **old_raw, void *new_raw) -{ - git_attr_assignment **old = (git_attr_assignment **)old_raw; - git_attr_assignment *new = (git_attr_assignment *)new_raw; - - GIT_REFCOUNT_DEC(*old, git_attr_assignment__free); - *old = new; - return GIT_EEXISTS; -} - -int git_attr_assignment__parse( - git_repository *repo, - git_pool *pool, - git_vector *assigns, - const char **base) -{ - int error; - const char *scan = *base; - git_attr_assignment *assign = NULL; - - assert(assigns && !assigns->length); - - assigns->_cmp = sort_by_hash_and_name; - - while (*scan && *scan != '\n') { - const char *name_start, *value_start; - - /* skip leading blanks */ - while (git__isspace(*scan) && *scan != '\n') scan++; - - /* allocate assign if needed */ - if (!assign) { - assign = git__calloc(1, sizeof(git_attr_assignment)); - GITERR_CHECK_ALLOC(assign); - GIT_REFCOUNT_INC(assign); - } - - assign->name_hash = 5381; - assign->value = git_attr__true; - - /* look for magic name prefixes */ - if (*scan == '-') { - assign->value = git_attr__false; - scan++; - } else if (*scan == '!') { - assign->value = git_attr__unset; /* explicit unspecified state */ - scan++; - } else if (*scan == '#') /* comment rest of line */ - break; - - /* find the name */ - name_start = scan; - while (*scan && !git__isspace(*scan) && *scan != '=') { - assign->name_hash = - ((assign->name_hash << 5) + assign->name_hash) + *scan; - scan++; - } - if (scan == name_start) { - /* must have found lone prefix (" - ") or leading = ("=foo") - * or end of buffer -- advance until whitespace and continue - */ - while (*scan && !git__isspace(*scan)) scan++; - continue; - } - - /* allocate permanent storage for name */ - assign->name = git_pool_strndup(pool, name_start, scan - name_start); - GITERR_CHECK_ALLOC(assign->name); - - /* if there is an equals sign, find the value */ - if (*scan == '=') { - for (value_start = ++scan; *scan && !git__isspace(*scan); ++scan); - - /* if we found a value, allocate permanent storage for it */ - if (scan > value_start) { - assign->value = git_pool_strndup(pool, value_start, scan - value_start); - GITERR_CHECK_ALLOC(assign->value); - } - } - - /* expand macros (if given a repo with a macro cache) */ - if (repo != NULL && assign->value == git_attr__true) { - git_attr_rule *macro = - git_attr_cache__lookup_macro(repo, assign->name); - - if (macro != NULL) { - unsigned int i; - git_attr_assignment *massign; - - git_vector_foreach(¯o->assigns, i, massign) { - GIT_REFCOUNT_INC(massign); - - error = git_vector_insert_sorted( - assigns, massign, &merge_assignments); - if (error < 0 && error != GIT_EEXISTS) - return error; - } - } - } - - /* insert allocated assign into vector */ - error = git_vector_insert_sorted(assigns, assign, &merge_assignments); - if (error < 0 && error != GIT_EEXISTS) - return error; - - /* clear assign since it is now "owned" by the vector */ - assign = NULL; - } - - if (assign != NULL) - git_attr_assignment__free(assign); - - *base = git__next_line(scan); - - return (assigns->length == 0) ? GIT_ENOTFOUND : 0; -} - -static void git_attr_rule__clear(git_attr_rule *rule) -{ - unsigned int i; - git_attr_assignment *assign; - - if (!rule) - return; - - if (!(rule->match.flags & GIT_ATTR_FNMATCH_IGNORE)) { - git_vector_foreach(&rule->assigns, i, assign) - GIT_REFCOUNT_DEC(assign, git_attr_assignment__free); - git_vector_free(&rule->assigns); - } - - /* match.pattern is stored in a git_pool, so no need to free */ - rule->match.pattern = NULL; - rule->match.length = 0; -} - -void git_attr_rule__free(git_attr_rule *rule) -{ - git_attr_rule__clear(rule); - git__free(rule); -} - diff --git a/src/attr_file.h b/src/attr_file.h deleted file mode 100644 index 8dc8303f70a..00000000000 --- a/src/attr_file.h +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_attr_file_h__ -#define INCLUDE_attr_file_h__ - -#include "git2/oid.h" -#include "git2/attr.h" -#include "vector.h" -#include "pool.h" -#include "buffer.h" -#include "fileops.h" - -#define GIT_ATTR_FILE ".gitattributes" -#define GIT_ATTR_FILE_INREPO "info/attributes" -#define GIT_ATTR_FILE_SYSTEM "gitattributes" - -#define GIT_ATTR_FNMATCH_NEGATIVE (1U << 0) -#define GIT_ATTR_FNMATCH_DIRECTORY (1U << 1) -#define GIT_ATTR_FNMATCH_FULLPATH (1U << 2) -#define GIT_ATTR_FNMATCH_MACRO (1U << 3) -#define GIT_ATTR_FNMATCH_IGNORE (1U << 4) -#define GIT_ATTR_FNMATCH_HASWILD (1U << 5) -#define GIT_ATTR_FNMATCH_ALLOWSPACE (1U << 6) -#define GIT_ATTR_FNMATCH_ICASE (1U << 7) - -extern const char *git_attr__true; -extern const char *git_attr__false; -extern const char *git_attr__unset; - -typedef struct { - char *pattern; - size_t length; - unsigned int flags; -} git_attr_fnmatch; - -typedef struct { - git_attr_fnmatch match; - git_vector assigns; /* vector of */ -} git_attr_rule; - -typedef struct { - git_refcount unused; - const char *name; - uint32_t name_hash; -} git_attr_name; - -typedef struct { - git_refcount rc; /* for macros */ - char *name; - uint32_t name_hash; - const char *value; -} git_attr_assignment; - -typedef struct { - char *key; /* cache "source#path" this was loaded from */ - git_vector rules; /* vector of or */ - git_pool *pool; - bool pool_is_allocated; - union { - git_oid oid; - git_futils_filestamp stamp; - } cache_data; -} git_attr_file; - -typedef struct { - git_buf full; - char *path; - char *basename; - int is_dir; -} git_attr_path; - -typedef enum { - GIT_ATTR_FILE_FROM_FILE = 0, - GIT_ATTR_FILE_FROM_INDEX = 1 -} git_attr_file_source; - -/* - * git_attr_file API - */ - -extern int git_attr_file__new( - git_attr_file **attrs_ptr, git_attr_file_source src, const char *path, git_pool *pool); - -extern int git_attr_file__new_and_load( - git_attr_file **attrs_ptr, const char *path); - -extern void git_attr_file__free(git_attr_file *file); - -extern void git_attr_file__clear_rules(git_attr_file *file); - -extern int git_attr_file__parse_buffer( - git_repository *repo, void *parsedata, const char *buf, git_attr_file *file); - -extern int git_attr_file__lookup_one( - git_attr_file *file, - const git_attr_path *path, - const char *attr, - const char **value); - -/* loop over rules in file from bottom to top */ -#define git_attr_file__foreach_matching_rule(file, path, iter, rule) \ - git_vector_rforeach(&(file)->rules, (iter), (rule)) \ - if (git_attr_rule__match((rule), (path))) - -extern uint32_t git_attr_file__name_hash(const char *name); - - -/* - * other utilities - */ - -extern int git_attr_fnmatch__parse( - git_attr_fnmatch *spec, - git_pool *pool, - const char *source, - const char **base); - -extern bool git_attr_fnmatch__match( - git_attr_fnmatch *rule, - const git_attr_path *path); - -extern void git_attr_rule__free(git_attr_rule *rule); - -extern bool git_attr_rule__match( - git_attr_rule *rule, - const git_attr_path *path); - -extern git_attr_assignment *git_attr_rule__lookup_assignment( - git_attr_rule *rule, const char *name); - -extern int git_attr_path__init( - git_attr_path *info, const char *path, const char *base); - -extern void git_attr_path__free(git_attr_path *info); - -extern int git_attr_assignment__parse( - git_repository *repo, /* needed to expand macros */ - git_pool *pool, - git_vector *assigns, - const char **scan); - -#endif diff --git a/src/blob.c b/src/blob.c deleted file mode 100644 index bcb6ac96bf0..00000000000 --- a/src/blob.c +++ /dev/null @@ -1,310 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "git2/common.h" -#include "git2/object.h" -#include "git2/repository.h" - -#include "common.h" -#include "blob.h" -#include "filter.h" - -const void *git_blob_rawcontent(const git_blob *blob) -{ - assert(blob); - return blob->odb_object->raw.data; -} - -git_off_t git_blob_rawsize(const git_blob *blob) -{ - assert(blob); - return (git_off_t)blob->odb_object->raw.len; -} - -int git_blob__getbuf(git_buf *buffer, git_blob *blob) -{ - return git_buf_set( - buffer, blob->odb_object->raw.data, blob->odb_object->raw.len); -} - -void git_blob__free(git_blob *blob) -{ - git_odb_object_free(blob->odb_object); - git__free(blob); -} - -int git_blob__parse(git_blob *blob, git_odb_object *odb_obj) -{ - assert(blob); - git_cached_obj_incref((git_cached_obj *)odb_obj); - blob->odb_object = odb_obj; - return 0; -} - -int git_blob_create_frombuffer(git_oid *oid, git_repository *repo, const void *buffer, size_t len) -{ - int error; - git_odb *odb; - git_odb_stream *stream; - - if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 || - (error = git_odb_open_wstream(&stream, odb, len, GIT_OBJ_BLOB)) < 0) - return error; - - if ((error = stream->write(stream, buffer, len)) == 0) - error = stream->finalize_write(oid, stream); - - stream->free(stream); - return error; -} - -static int write_file_stream( - git_oid *oid, git_odb *odb, const char *path, git_off_t file_size) -{ - int fd, error; - char buffer[4096]; - git_odb_stream *stream = NULL; - ssize_t read_len = -1, written = 0; - - if ((error = git_odb_open_wstream( - &stream, odb, (size_t)file_size, GIT_OBJ_BLOB)) < 0) - return error; - - if ((fd = git_futils_open_ro(path)) < 0) { - stream->free(stream); - return -1; - } - - while (!error && (read_len = p_read(fd, buffer, sizeof(buffer))) > 0) { - error = stream->write(stream, buffer, read_len); - written += read_len; - } - - p_close(fd); - - if (written != file_size || read_len < 0) { - giterr_set(GITERR_OS, "Failed to read file into stream"); - error = -1; - } - - if (!error) - error = stream->finalize_write(oid, stream); - - stream->free(stream); - return error; -} - -static int write_file_filtered( - git_oid *oid, - git_odb *odb, - const char *full_path, - git_vector *filters) -{ - int error; - git_buf source = GIT_BUF_INIT; - git_buf dest = GIT_BUF_INIT; - - if ((error = git_futils_readbuffer(&source, full_path)) < 0) - return error; - - error = git_filters_apply(&dest, &source, filters); - - /* Free the source as soon as possible. This can be big in memory, - * and we don't want to ODB write to choke */ - git_buf_free(&source); - - /* Write the file to disk if it was properly filtered */ - if (!error) - error = git_odb_write(oid, odb, dest.ptr, dest.size, GIT_OBJ_BLOB); - - git_buf_free(&dest); - return error; -} - -static int write_symlink( - git_oid *oid, git_odb *odb, const char *path, size_t link_size) -{ - char *link_data; - ssize_t read_len; - int error; - - link_data = git__malloc(link_size); - GITERR_CHECK_ALLOC(link_data); - - read_len = p_readlink(path, link_data, link_size); - if (read_len != (ssize_t)link_size) { - giterr_set(GITERR_OS, "Failed to create blob. Can't read symlink '%s'", path); - git__free(link_data); - return -1; - } - - error = git_odb_write(oid, odb, (void *)link_data, link_size, GIT_OBJ_BLOB); - git__free(link_data); - return error; -} - -static int blob_create_internal(git_oid *oid, git_repository *repo, const char *content_path, const char *hint_path, bool try_load_filters) -{ - int error; - struct stat st; - git_odb *odb = NULL; - git_off_t size; - - assert(hint_path || !try_load_filters); - - if ((error = git_path_lstat(content_path, &st)) < 0 || (error = git_repository_odb__weakptr(&odb, repo)) < 0) - return error; - - size = st.st_size; - - if (S_ISLNK(st.st_mode)) { - error = write_symlink(oid, odb, content_path, (size_t)size); - } else { - git_vector write_filters = GIT_VECTOR_INIT; - int filter_count = 0; - - if (try_load_filters) { - /* Load the filters for writing this file to the ODB */ - filter_count = git_filters_load( - &write_filters, repo, hint_path, GIT_FILTER_TO_ODB); - } - - if (filter_count < 0) { - /* Negative value means there was a critical error */ - error = filter_count; - } else if (filter_count == 0) { - /* No filters need to be applied to the document: we can stream - * directly from disk */ - error = write_file_stream(oid, odb, content_path, size); - } else { - /* We need to apply one or more filters */ - error = write_file_filtered(oid, odb, content_path, &write_filters); - } - - git_filters_free(&write_filters); - - /* - * TODO: eventually support streaming filtered files, for files - * which are bigger than a given threshold. This is not a priority - * because applying a filter in streaming mode changes the final - * size of the blob, and without knowing its final size, the blob - * cannot be written in stream mode to the ODB. - * - * The plan is to do streaming writes to a tempfile on disk and then - * opening streaming that file to the ODB, using - * `write_file_stream`. - * - * CAREFULLY DESIGNED APIS YO - */ - } - - return error; -} - -int git_blob_create_fromworkdir(git_oid *oid, git_repository *repo, const char *path) -{ - git_buf full_path = GIT_BUF_INIT; - const char *workdir; - int error; - - if ((error = git_repository__ensure_not_bare(repo, "create blob from file")) < 0) - return error; - - workdir = git_repository_workdir(repo); - - if (git_buf_joinpath(&full_path, workdir, path) < 0) { - git_buf_free(&full_path); - return -1; - } - - error = blob_create_internal(oid, repo, git_buf_cstr(&full_path), git_buf_cstr(&full_path), true); - - git_buf_free(&full_path); - return error; -} - -int git_blob_create_fromdisk(git_oid *oid, git_repository *repo, const char *path) -{ - int error; - git_buf full_path = GIT_BUF_INIT; - - if ((error = git_path_prettify(&full_path, path, NULL)) < 0) { - git_buf_free(&full_path); - return error; - } - - error = blob_create_internal(oid, repo, git_buf_cstr(&full_path), git_buf_cstr(&full_path), true); - - git_buf_free(&full_path); - return error; -} - -#define BUFFER_SIZE 4096 - -int git_blob_create_fromchunks( - git_oid *oid, - git_repository *repo, - const char *hintpath, - int (*source_cb)(char *content, size_t max_length, void *payload), - void *payload) -{ - int error = -1, read_bytes; - char *content = NULL; - git_filebuf file = GIT_FILEBUF_INIT; - git_buf path = GIT_BUF_INIT; - - if (git_buf_join_n( - &path, '/', 3, - git_repository_path(repo), - GIT_OBJECTS_DIR, - "streamed") < 0) - goto cleanup; - - content = git__malloc(BUFFER_SIZE); - GITERR_CHECK_ALLOC(content); - - if (git_filebuf_open(&file, git_buf_cstr(&path), GIT_FILEBUF_TEMPORARY) < 0) - goto cleanup; - - while (1) { - read_bytes = source_cb(content, BUFFER_SIZE, payload); - - assert(read_bytes <= BUFFER_SIZE); - - if (read_bytes <= 0) - break; - - if (git_filebuf_write(&file, content, read_bytes) < 0) - goto cleanup; - } - - if (read_bytes < 0) - goto cleanup; - - if (git_filebuf_flush(&file) < 0) - goto cleanup; - - error = blob_create_internal(oid, repo, file.path_lock, hintpath, hintpath != NULL); - -cleanup: - git_buf_free(&path); - git_filebuf_cleanup(&file); - git__free(content); - return error; -} - -int git_blob_is_binary(git_blob *blob) -{ - git_buf content; - - assert(blob); - - content.ptr = blob->odb_object->raw.data; - content.size = min(blob->odb_object->raw.len, 4000); - - return git_buf_text_is_binary(&content); -} diff --git a/src/blob.h b/src/blob.h deleted file mode 100644 index 524734b1f69..00000000000 --- a/src/blob.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_blob_h__ -#define INCLUDE_blob_h__ - -#include "git2/blob.h" -#include "repository.h" -#include "odb.h" -#include "fileops.h" - -struct git_blob { - git_object object; - git_odb_object *odb_object; -}; - -void git_blob__free(git_blob *blob); -int git_blob__parse(git_blob *blob, git_odb_object *obj); -int git_blob__getbuf(git_buf *buffer, git_blob *blob); - -#endif diff --git a/src/branch.c b/src/branch.c deleted file mode 100644 index 3959409c5e5..00000000000 --- a/src/branch.c +++ /dev/null @@ -1,405 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "commit.h" -#include "tag.h" -#include "config.h" -#include "refspec.h" -#include "refs.h" - -#include "git2/branch.h" - -static int retrieve_branch_reference( - git_reference **branch_reference_out, - git_repository *repo, - const char *branch_name, - int is_remote) -{ - git_reference *branch; - int error = -1; - char *prefix; - git_buf ref_name = GIT_BUF_INIT; - - *branch_reference_out = NULL; - - prefix = is_remote ? GIT_REFS_REMOTES_DIR : GIT_REFS_HEADS_DIR; - - if (git_buf_joinpath(&ref_name, prefix, branch_name) < 0) - goto cleanup; - - if ((error = git_reference_lookup(&branch, repo, ref_name.ptr)) < 0) { - giterr_set(GITERR_REFERENCE, - "Cannot locate %s branch '%s'.", is_remote ? "remote-tracking" : "local", branch_name); - goto cleanup; - } - - *branch_reference_out = branch; - -cleanup: - git_buf_free(&ref_name); - return error; -} - -static int not_a_local_branch(const char *reference_name) -{ - giterr_set( - GITERR_INVALID, - "Reference '%s' is not a local branch.", reference_name); - return -1; -} - -int git_branch_create( - git_reference **ref_out, - git_repository *repository, - const char *branch_name, - const git_commit *commit, - int force) -{ - git_reference *branch = NULL; - git_buf canonical_branch_name = GIT_BUF_INIT; - int error = -1; - - assert(branch_name && commit && ref_out); - assert(git_object_owner((const git_object *)commit) == repository); - - if (git_buf_joinpath(&canonical_branch_name, GIT_REFS_HEADS_DIR, branch_name) < 0) - goto cleanup; - - error = git_reference_create(&branch, repository, - git_buf_cstr(&canonical_branch_name), git_commit_id(commit), force); - - if (!error) - *ref_out = branch; - -cleanup: - git_buf_free(&canonical_branch_name); - return error; -} - -int git_branch_delete(git_reference *branch) -{ - int is_head; - git_buf config_section = GIT_BUF_INIT; - int error = -1; - - assert(branch); - - if (!git_reference_is_branch(branch) && - !git_reference_is_remote(branch)) { - giterr_set(GITERR_INVALID, "Reference '%s' is not a valid branch.", git_reference_name(branch)); - return -1; - } - - if ((is_head = git_branch_is_head(branch)) < 0) - return is_head; - - if (is_head) { - giterr_set(GITERR_REFERENCE, - "Cannot delete branch '%s' as it is the current HEAD of the repository.", git_reference_name(branch)); - return -1; - } - - if (git_buf_printf(&config_section, "branch.%s", git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0) - goto on_error; - - if (git_config_rename_section( - git_reference_owner(branch), - git_buf_cstr(&config_section), - NULL) < 0) - goto on_error; - - if (git_reference_delete(branch) < 0) - goto on_error; - - error = 0; - -on_error: - git_buf_free(&config_section); - return error; -} - -typedef struct { - int (*branch_cb)( - const char *branch_name, - git_branch_t branch_type, - void *payload); - void *callback_payload; - unsigned int branch_type; -} branch_foreach_filter; - -static int branch_foreach_cb(const char *branch_name, void *payload) -{ - branch_foreach_filter *filter = (branch_foreach_filter *)payload; - - if (filter->branch_type & GIT_BRANCH_LOCAL && - git__prefixcmp(branch_name, GIT_REFS_HEADS_DIR) == 0) - return filter->branch_cb(branch_name + strlen(GIT_REFS_HEADS_DIR), GIT_BRANCH_LOCAL, filter->callback_payload); - - if (filter->branch_type & GIT_BRANCH_REMOTE && - git__prefixcmp(branch_name, GIT_REFS_REMOTES_DIR) == 0) - return filter->branch_cb(branch_name + strlen(GIT_REFS_REMOTES_DIR), GIT_BRANCH_REMOTE, filter->callback_payload); - - return 0; -} - -int git_branch_foreach( - git_repository *repo, - unsigned int list_flags, - int (*branch_cb)( - const char *branch_name, - git_branch_t branch_type, - void *payload), - void *payload -) -{ - branch_foreach_filter filter; - - filter.branch_cb = branch_cb; - filter.branch_type = list_flags; - filter.callback_payload = payload; - - return git_reference_foreach(repo, GIT_REF_LISTALL, &branch_foreach_cb, (void *)&filter); -} - -int git_branch_move( - git_reference *branch, - const char *new_branch_name, - int force) -{ - git_buf new_reference_name = GIT_BUF_INIT, - old_config_section = GIT_BUF_INIT, - new_config_section = GIT_BUF_INIT; - int error; - - assert(branch && new_branch_name); - - if (!git_reference_is_branch(branch)) - return not_a_local_branch(git_reference_name(branch)); - - if ((error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0) - goto cleanup; - - if (git_buf_printf( - &old_config_section, - "branch.%s", - git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0) - goto cleanup; - - if ((error = git_reference_rename(branch, git_buf_cstr(&new_reference_name), force)) < 0) - goto cleanup; - - if (git_buf_printf(&new_config_section, "branch.%s", new_branch_name) < 0) - goto cleanup; - - if ((error = git_config_rename_section( - git_reference_owner(branch), - git_buf_cstr(&old_config_section), - git_buf_cstr(&new_config_section))) < 0) - goto cleanup; - -cleanup: - git_buf_free(&new_reference_name); - git_buf_free(&old_config_section); - git_buf_free(&new_config_section); - - return error; -} - -int git_branch_lookup( - git_reference **ref_out, - git_repository *repo, - const char *branch_name, - git_branch_t branch_type) -{ - assert(ref_out && repo && branch_name); - - return retrieve_branch_reference(ref_out, repo, branch_name, branch_type == GIT_BRANCH_REMOTE); -} - -int git_branch_name(const char **out, git_reference *ref) -{ - const char *branch_name; - - assert(out && ref); - - branch_name = ref->name; - - if (git_reference_is_branch(ref)) { - branch_name += strlen(GIT_REFS_HEADS_DIR); - } else if (git_reference_is_remote(ref)) { - branch_name += strlen(GIT_REFS_REMOTES_DIR); - } else { - giterr_set(GITERR_INVALID, - "Reference '%s' is neither a local nor a remote branch.", ref->name); - return -1; - } - *out = branch_name; - return 0; -} - -static int retrieve_tracking_configuration( - const char **out, - git_repository *repo, - const char *canonical_branch_name, - const char *format) -{ - git_config *config; - git_buf buf = GIT_BUF_INIT; - int error; - - if (git_repository_config__weakptr(&config, repo) < 0) - return -1; - - if (git_buf_printf(&buf, format, - canonical_branch_name + strlen(GIT_REFS_HEADS_DIR)) < 0) - return -1; - - error = git_config_get_string(out, config, git_buf_cstr(&buf)); - git_buf_free(&buf); - return error; -} - -int git_branch_tracking__name( - git_buf *tracking_name, - git_repository *repo, - const char *canonical_branch_name) -{ - const char *remote_name, *merge_name; - git_buf buf = GIT_BUF_INIT; - int error = -1; - git_remote *remote = NULL; - const git_refspec *refspec; - - assert(tracking_name && canonical_branch_name); - - if (!git_reference__is_branch(canonical_branch_name)) - return not_a_local_branch(canonical_branch_name); - - if ((error = retrieve_tracking_configuration( - &remote_name, repo, canonical_branch_name, "branch.%s.remote")) < 0) - goto cleanup; - - if ((error = retrieve_tracking_configuration( - &merge_name, repo, canonical_branch_name, "branch.%s.merge")) < 0) - goto cleanup; - - if (!*remote_name || !*merge_name) { - error = GIT_ENOTFOUND; - goto cleanup; - } - - if (strcmp(".", remote_name) != 0) { - if ((error = git_remote_load(&remote, repo, remote_name)) < 0) - goto cleanup; - - refspec = git_remote_fetchspec(remote); - if (refspec == NULL - || refspec->src == NULL - || refspec->dst == NULL) { - error = GIT_ENOTFOUND; - goto cleanup; - } - - if (git_refspec_transform_r(&buf, refspec, merge_name) < 0) - goto cleanup; - } else - if (git_buf_sets(&buf, merge_name) < 0) - goto cleanup; - - error = git_buf_set(tracking_name, git_buf_cstr(&buf), git_buf_len(&buf)); - -cleanup: - git_remote_free(remote); - git_buf_free(&buf); - return error; -} - -int git_branch_tracking_name( - char *tracking_branch_name_out, - size_t buffer_size, - git_repository *repo, - const char *canonical_branch_name) -{ - git_buf buf = GIT_BUF_INIT; - int error; - - assert(canonical_branch_name); - - if (tracking_branch_name_out && buffer_size) - *tracking_branch_name_out = '\0'; - - if ((error = git_branch_tracking__name( - &buf, repo, canonical_branch_name)) < 0) - goto cleanup; - - if (tracking_branch_name_out && buf.size + 1 > buffer_size) { /* +1 for NUL byte */ - giterr_set( - GITERR_INVALID, - "Buffer too short to hold the tracked reference name."); - error = -1; - goto cleanup; - } - - if (tracking_branch_name_out) - git_buf_copy_cstr(tracking_branch_name_out, buffer_size, &buf); - - error = buf.size + 1; - -cleanup: - git_buf_free(&buf); - return (int)error; -} - -int git_branch_tracking( - git_reference **tracking_out, - git_reference *branch) -{ - int error; - git_buf tracking_name = GIT_BUF_INIT; - - if ((error = git_branch_tracking__name(&tracking_name, - git_reference_owner(branch), git_reference_name(branch))) < 0) - return error; - - error = git_reference_lookup( - tracking_out, - git_reference_owner(branch), - git_buf_cstr(&tracking_name)); - - git_buf_free(&tracking_name); - return error; -} - -int git_branch_is_head( - git_reference *branch) -{ - git_reference *head; - bool is_same = false; - int error; - - assert(branch); - - if (!git_reference_is_branch(branch)) - return false; - - error = git_repository_head(&head, git_reference_owner(branch)); - - if (error == GIT_EORPHANEDHEAD || error == GIT_ENOTFOUND) - return false; - - if (error < 0) - return -1; - - is_same = strcmp( - git_reference_name(branch), - git_reference_name(head)) == 0; - - git_reference_free(head); - - return is_same; -} diff --git a/src/branch.h b/src/branch.h deleted file mode 100644 index 8a26c4fea79..00000000000 --- a/src/branch.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_branch_h__ -#define INCLUDE_branch_h__ - -#include "buffer.h" - -int git_branch_tracking__name( - git_buf *tracking_name, - git_repository *repo, - const char *canonical_branch_name); - -#endif diff --git a/src/bswap.h b/src/bswap.h deleted file mode 100644 index 486df82f4ce..00000000000 --- a/src/bswap.h +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" - -/* - * Default version that the compiler ought to optimize properly with - * constant values. - */ -GIT_INLINE(uint32_t) default_swab32(uint32_t val) -{ - return (((val & 0xff000000) >> 24) | - ((val & 0x00ff0000) >> 8) | - ((val & 0x0000ff00) << 8) | - ((val & 0x000000ff) << 24)); -} - -#undef bswap32 - -GIT_INLINE(uint16_t) default_swab16(uint16_t val) -{ - return (((val & 0xff00) >> 8) | - ((val & 0x00ff) << 8)); -} - -#undef bswap16 - -#if defined(__GNUC__) && defined(__i386__) - -#define bswap32(x) ({ \ - uint32_t __res; \ - if (__builtin_constant_p(x)) { \ - __res = default_swab32(x); \ - } else { \ - __asm__("bswap %0" : "=r" (__res) : "0" ((uint32_t)(x))); \ - } \ - __res; }) - -#define bswap16(x) ({ \ - uint16_t __res; \ - if (__builtin_constant_p(x)) { \ - __res = default_swab16(x); \ - } else { \ - __asm__("xchgb %b0,%h0" : "=q" (__res) : "0" ((uint16_t)(x))); \ - } \ - __res; }) - -#elif defined(__GNUC__) && defined(__x86_64__) - -#define bswap32(x) ({ \ - uint32_t __res; \ - if (__builtin_constant_p(x)) { \ - __res = default_swab32(x); \ - } else { \ - __asm__("bswapl %0" : "=r" (__res) : "0" ((uint32_t)(x))); \ - } \ - __res; }) - -#define bswap16(x) ({ \ - uint16_t __res; \ - if (__builtin_constant_p(x)) { \ - __res = default_swab16(x); \ - } else { \ - __asm__("xchgb %b0,%h0" : "=Q" (__res) : "0" ((uint16_t)(x))); \ - } \ - __res; }) - -#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) - -#include - -#define bswap32(x) _byteswap_ulong(x) -#define bswap16(x) _byteswap_ushort(x) - -#endif - -#ifdef bswap32 - -#undef ntohl -#undef htonl -#define ntohl(x) bswap32(x) -#define htonl(x) bswap32(x) - -#endif - -#ifdef bswap16 - -#undef ntohs -#undef htons -#define ntohs(x) bswap16(x) -#define htons(x) bswap16(x) - -#endif diff --git a/src/buf_text.c b/src/buf_text.c deleted file mode 100644 index 3a8f442b4c1..00000000000 --- a/src/buf_text.c +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "buf_text.h" - -int git_buf_text_puts_escaped( - git_buf *buf, - const char *string, - const char *esc_chars, - const char *esc_with) -{ - const char *scan; - size_t total = 0, esc_len = strlen(esc_with), count; - - if (!string) - return 0; - - for (scan = string; *scan; ) { - /* count run of non-escaped characters */ - count = strcspn(scan, esc_chars); - total += count; - scan += count; - /* count run of escaped characters */ - count = strspn(scan, esc_chars); - total += count * (esc_len + 1); - scan += count; - } - - if (git_buf_grow(buf, buf->size + total + 1) < 0) - return -1; - - for (scan = string; *scan; ) { - count = strcspn(scan, esc_chars); - - memmove(buf->ptr + buf->size, scan, count); - scan += count; - buf->size += count; - - for (count = strspn(scan, esc_chars); count > 0; --count) { - /* copy escape sequence */ - memmove(buf->ptr + buf->size, esc_with, esc_len); - buf->size += esc_len; - /* copy character to be escaped */ - buf->ptr[buf->size] = *scan; - buf->size++; - scan++; - } - } - - buf->ptr[buf->size] = '\0'; - - return 0; -} - -void git_buf_text_unescape(git_buf *buf) -{ - buf->size = git__unescape(buf->ptr); -} - -int git_buf_text_common_prefix(git_buf *buf, const git_strarray *strings) -{ - size_t i; - const char *str, *pfx; - - git_buf_clear(buf); - - if (!strings || !strings->count) - return 0; - - /* initialize common prefix to first string */ - if (git_buf_sets(buf, strings->strings[0]) < 0) - return -1; - - /* go through the rest of the strings, truncating to shared prefix */ - for (i = 1; i < strings->count; ++i) { - - for (str = strings->strings[i], pfx = buf->ptr; - *str && *str == *pfx; str++, pfx++) - /* scanning */; - - git_buf_truncate(buf, pfx - buf->ptr); - - if (!buf->size) - break; - } - - return 0; -} - -bool git_buf_text_is_binary(const git_buf *buf) -{ - const char *scan = buf->ptr, *end = buf->ptr + buf->size; - int printable = 0, nonprintable = 0; - - while (scan < end) { - unsigned char c = *scan++; - - if (c > 0x1F && c < 0x7F) - printable++; - else if (c == '\0') - return true; - else if (!git__isspace(c)) - nonprintable++; - } - - return ((printable >> 7) < nonprintable); -} - -bool git_buf_text_contains_nul(const git_buf *buf) -{ - return (memchr(buf->ptr, '\0', buf->size) != NULL); -} - -int git_buf_text_detect_bom(git_bom_t *bom, const git_buf *buf, size_t offset) -{ - const char *ptr; - size_t len; - - *bom = GIT_BOM_NONE; - /* need at least 2 bytes after offset to look for any BOM */ - if (buf->size < offset + 2) - return 0; - - ptr = buf->ptr + offset; - len = buf->size - offset; - - switch (*ptr++) { - case 0: - if (len >= 4 && ptr[0] == 0 && ptr[1] == '\xFE' && ptr[2] == '\xFF') { - *bom = GIT_BOM_UTF32_BE; - return 4; - } - break; - case '\xEF': - if (len >= 3 && ptr[0] == '\xBB' && ptr[1] == '\xBF') { - *bom = GIT_BOM_UTF8; - return 3; - } - break; - case '\xFE': - if (*ptr == '\xFF') { - *bom = GIT_BOM_UTF16_BE; - return 2; - } - break; - case '\xFF': - if (*ptr != '\xFE') - break; - if (len >= 4 && ptr[1] == 0 && ptr[2] == 0) { - *bom = GIT_BOM_UTF32_LE; - return 4; - } else { - *bom = GIT_BOM_UTF16_LE; - return 2; - } - break; - default: - break; - } - - return 0; -} - -bool git_buf_text_gather_stats( - git_buf_text_stats *stats, const git_buf *buf, bool skip_bom) -{ - const char *scan = buf->ptr, *end = buf->ptr + buf->size; - int skip; - - memset(stats, 0, sizeof(*stats)); - - /* BOM detection */ - skip = git_buf_text_detect_bom(&stats->bom, buf, 0); - if (skip_bom) - scan += skip; - - /* Ignore EOF character */ - if (buf->size > 0 && end[-1] == '\032') - end--; - - /* Counting loop */ - while (scan < end) { - unsigned char c = *scan++; - - if ((c > 0x1F && c < 0x7F) || c > 0x9f) - stats->printable++; - else switch (c) { - case '\0': - stats->nul++; - stats->nonprintable++; - break; - case '\n': - stats->lf++; - break; - case '\r': - stats->cr++; - if (scan < end && *scan == '\n') - stats->crlf++; - break; - case '\t': case '\f': case '\v': case '\b': case 0x1b: /*ESC*/ - stats->printable++; - break; - default: - stats->nonprintable++; - break; - } - } - - return (stats->nul > 0 || - ((stats->printable >> 7) < stats->nonprintable)); -} diff --git a/src/buf_text.h b/src/buf_text.h deleted file mode 100644 index 458ee33c98c..00000000000 --- a/src/buf_text.h +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_buf_text_h__ -#define INCLUDE_buf_text_h__ - -#include "buffer.h" - -typedef enum { - GIT_BOM_NONE = 0, - GIT_BOM_UTF8 = 1, - GIT_BOM_UTF16_LE = 2, - GIT_BOM_UTF16_BE = 3, - GIT_BOM_UTF32_LE = 4, - GIT_BOM_UTF32_BE = 5 -} git_bom_t; - -typedef struct { - git_bom_t bom; /* BOM found at head of text */ - unsigned int nul, cr, lf, crlf; /* NUL, CR, LF and CRLF counts */ - unsigned int printable, nonprintable; /* These are just approximations! */ -} git_buf_text_stats; - -/** - * Append string to buffer, prefixing each character from `esc_chars` with - * `esc_with` string. - * - * @param buf Buffer to append data to - * @param string String to escape and append - * @param esc_chars Characters to be escaped - * @param esc_with String to insert in from of each found character - * @return 0 on success, <0 on failure (probably allocation problem) - */ -extern int git_buf_text_puts_escaped( - git_buf *buf, - const char *string, - const char *esc_chars, - const char *esc_with); - -/** - * Append string escaping characters that are regex special - */ -GIT_INLINE(int) git_buf_text_puts_escape_regex(git_buf *buf, const char *string) -{ - return git_buf_text_puts_escaped(buf, string, "^.[]$()|*+?{}\\", "\\"); -} - -/** - * Unescape all characters in a buffer in place - * - * I.e. remove backslashes - */ -extern void git_buf_text_unescape(git_buf *buf); - -/** - * Fill buffer with the common prefix of a array of strings - * - * Buffer will be set to empty if there is no common prefix - */ -extern int git_buf_text_common_prefix(git_buf *buf, const git_strarray *strs); - -/** - * Check quickly if buffer looks like it contains binary data - * - * @param buf Buffer to check - * @return true if buffer looks like non-text data - */ -extern bool git_buf_text_is_binary(const git_buf *buf); - -/** - * Check quickly if buffer contains a NUL byte - * - * @param buf Buffer to check - * @return true if buffer contains a NUL byte - */ -extern bool git_buf_text_contains_nul(const git_buf *buf); - -/** - * Check if a buffer begins with a UTF BOM - * - * @param bom Set to the type of BOM detected or GIT_BOM_NONE - * @param buf Buffer in which to check the first bytes for a BOM - * @param offset Offset into buffer to look for BOM - * @return Number of bytes of BOM data (or 0 if no BOM found) - */ -extern int git_buf_text_detect_bom( - git_bom_t *bom, const git_buf *buf, size_t offset); - -/** - * Gather stats for a piece of text - * - * Fill the `stats` structure with counts of unreadable characters, carriage - * returns, etc, so it can be used in heuristics. This automatically skips - * a trailing EOF (\032 character). Also it will look for a BOM at the - * start of the text and can be told to skip that as well. - * - * @param stats Structure to be filled in - * @param buf Text to process - * @param skip_bom Exclude leading BOM from stats if true - * @return Does the buffer heuristically look like binary data - */ -extern bool git_buf_text_gather_stats( - git_buf_text_stats *stats, const git_buf *buf, bool skip_bom); - -#endif diff --git a/src/buffer.c b/src/buffer.c deleted file mode 100644 index 6e3ffe56017..00000000000 --- a/src/buffer.c +++ /dev/null @@ -1,477 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "buffer.h" -#include "posix.h" -#include -#include - -/* Used as default value for git_buf->ptr so that people can always - * assume ptr is non-NULL and zero terminated even for new git_bufs. - */ -char git_buf__initbuf[1]; - -char git_buf__oom[1]; - -#define ENSURE_SIZE(b, d) \ - if ((d) > buf->asize && git_buf_grow(b, (d)) < 0)\ - return -1; - - -void git_buf_init(git_buf *buf, size_t initial_size) -{ - buf->asize = 0; - buf->size = 0; - buf->ptr = git_buf__initbuf; - - if (initial_size) - git_buf_grow(buf, initial_size); -} - -int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom) -{ - char *new_ptr; - size_t new_size; - - if (buf->ptr == git_buf__oom) - return -1; - - if (target_size <= buf->asize) - return 0; - - if (buf->asize == 0) { - new_size = target_size; - new_ptr = NULL; - } else { - new_size = buf->asize; - new_ptr = buf->ptr; - } - - /* grow the buffer size by 1.5, until it's big enough - * to fit our target size */ - while (new_size < target_size) - new_size = (new_size << 1) - (new_size >> 1); - - /* round allocation up to multiple of 8 */ - new_size = (new_size + 7) & ~7; - - new_ptr = git__realloc(new_ptr, new_size); - - if (!new_ptr) { - if (mark_oom) - buf->ptr = git_buf__oom; - return -1; - } - - buf->asize = new_size; - buf->ptr = new_ptr; - - /* truncate the existing buffer size if necessary */ - if (buf->size >= buf->asize) - buf->size = buf->asize - 1; - buf->ptr[buf->size] = '\0'; - - return 0; -} - -void git_buf_free(git_buf *buf) -{ - if (!buf) return; - - if (buf->ptr != git_buf__initbuf && buf->ptr != git_buf__oom) - git__free(buf->ptr); - - git_buf_init(buf, 0); -} - -void git_buf_clear(git_buf *buf) -{ - buf->size = 0; - if (buf->asize > 0) - buf->ptr[0] = '\0'; -} - -int git_buf_set(git_buf *buf, const char *data, size_t len) -{ - if (len == 0 || data == NULL) { - git_buf_clear(buf); - } else { - if (data != buf->ptr) { - ENSURE_SIZE(buf, len + 1); - memmove(buf->ptr, data, len); - } - buf->size = len; - buf->ptr[buf->size] = '\0'; - } - return 0; -} - -int git_buf_sets(git_buf *buf, const char *string) -{ - return git_buf_set(buf, string, string ? strlen(string) : 0); -} - -int git_buf_putc(git_buf *buf, char c) -{ - ENSURE_SIZE(buf, buf->size + 2); - buf->ptr[buf->size++] = c; - buf->ptr[buf->size] = '\0'; - return 0; -} - -int git_buf_put(git_buf *buf, const char *data, size_t len) -{ - ENSURE_SIZE(buf, buf->size + len + 1); - memmove(buf->ptr + buf->size, data, len); - buf->size += len; - buf->ptr[buf->size] = '\0'; - return 0; -} - -int git_buf_puts(git_buf *buf, const char *string) -{ - assert(string); - return git_buf_put(buf, string, strlen(string)); -} - -static const char b64str[64] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -int git_buf_put_base64(git_buf *buf, const char *data, size_t len) -{ - size_t extra = len % 3; - uint8_t *write, a, b, c; - const uint8_t *read = (const uint8_t *)data; - - ENSURE_SIZE(buf, buf->size + 4 * ((len / 3) + !!extra) + 1); - write = (uint8_t *)&buf->ptr[buf->size]; - - /* convert each run of 3 bytes into 4 output bytes */ - for (len -= extra; len > 0; len -= 3) { - a = *read++; - b = *read++; - c = *read++; - - *write++ = b64str[a >> 2]; - *write++ = b64str[(a & 0x03) << 4 | b >> 4]; - *write++ = b64str[(b & 0x0f) << 2 | c >> 6]; - *write++ = b64str[c & 0x3f]; - } - - if (extra > 0) { - a = *read++; - b = (extra > 1) ? *read++ : 0; - - *write++ = b64str[a >> 2]; - *write++ = b64str[(a & 0x03) << 4 | b >> 4]; - *write++ = (extra > 1) ? b64str[(b & 0x0f) << 2] : '='; - *write++ = '='; - } - - buf->size = ((char *)write) - buf->ptr; - buf->ptr[buf->size] = '\0'; - - return 0; -} - -int git_buf_vprintf(git_buf *buf, const char *format, va_list ap) -{ - int len; - const size_t expected_size = buf->size + (strlen(format) * 2); - - ENSURE_SIZE(buf, expected_size); - - while (1) { - va_list args; - va_copy(args, ap); - - len = p_vsnprintf( - buf->ptr + buf->size, - buf->asize - buf->size, - format, args - ); - - if (len < 0) { - git__free(buf->ptr); - buf->ptr = git_buf__oom; - return -1; - } - - if ((size_t)len + 1 <= buf->asize - buf->size) { - buf->size += len; - break; - } - - ENSURE_SIZE(buf, buf->size + len + 1); - } - - return 0; -} - -int git_buf_printf(git_buf *buf, const char *format, ...) -{ - int r; - va_list ap; - - va_start(ap, format); - r = git_buf_vprintf(buf, format, ap); - va_end(ap); - - return r; -} - -void git_buf_copy_cstr(char *data, size_t datasize, const git_buf *buf) -{ - size_t copylen; - - assert(data && datasize && buf); - - data[0] = '\0'; - - if (buf->size == 0 || buf->asize <= 0) - return; - - copylen = buf->size; - if (copylen > datasize - 1) - copylen = datasize - 1; - memmove(data, buf->ptr, copylen); - data[copylen] = '\0'; -} - -void git_buf_consume(git_buf *buf, const char *end) -{ - if (end > buf->ptr && end <= buf->ptr + buf->size) { - size_t consumed = end - buf->ptr; - memmove(buf->ptr, end, buf->size - consumed); - buf->size -= consumed; - buf->ptr[buf->size] = '\0'; - } -} - -void git_buf_truncate(git_buf *buf, size_t len) -{ - if (len < buf->size) { - buf->size = len; - buf->ptr[buf->size] = '\0'; - } -} - -void git_buf_rtruncate_at_char(git_buf *buf, char separator) -{ - ssize_t idx = git_buf_rfind_next(buf, separator); - git_buf_truncate(buf, idx < 0 ? 0 : (size_t)idx); -} - -void git_buf_swap(git_buf *buf_a, git_buf *buf_b) -{ - git_buf t = *buf_a; - *buf_a = *buf_b; - *buf_b = t; -} - -char *git_buf_detach(git_buf *buf) -{ - char *data = buf->ptr; - - if (buf->asize == 0 || buf->ptr == git_buf__oom) - return NULL; - - git_buf_init(buf, 0); - - return data; -} - -void git_buf_attach(git_buf *buf, char *ptr, size_t asize) -{ - git_buf_free(buf); - - if (ptr) { - buf->ptr = ptr; - buf->size = strlen(ptr); - if (asize) - buf->asize = (asize < buf->size) ? buf->size + 1 : asize; - else /* pass 0 to fall back on strlen + 1 */ - buf->asize = buf->size + 1; - } else { - git_buf_grow(buf, asize); - } -} - -int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...) -{ - va_list ap; - int i; - size_t total_size = 0, original_size = buf->size; - char *out, *original = buf->ptr; - - if (buf->size > 0 && buf->ptr[buf->size - 1] != separator) - ++total_size; /* space for initial separator */ - - /* Make two passes to avoid multiple reallocation */ - - va_start(ap, nbuf); - for (i = 0; i < nbuf; ++i) { - const char* segment; - size_t segment_len; - - segment = va_arg(ap, const char *); - if (!segment) - continue; - - segment_len = strlen(segment); - total_size += segment_len; - if (segment_len == 0 || segment[segment_len - 1] != separator) - ++total_size; /* space for separator */ - } - va_end(ap); - - /* expand buffer if needed */ - if (total_size == 0) - return 0; - if (git_buf_grow(buf, buf->size + total_size + 1) < 0) - return -1; - - out = buf->ptr + buf->size; - - /* append separator to existing buf if needed */ - if (buf->size > 0 && out[-1] != separator) - *out++ = separator; - - va_start(ap, nbuf); - for (i = 0; i < nbuf; ++i) { - const char* segment; - size_t segment_len; - - segment = va_arg(ap, const char *); - if (!segment) - continue; - - /* deal with join that references buffer's original content */ - if (segment >= original && segment < original + original_size) { - size_t offset = (segment - original); - segment = buf->ptr + offset; - segment_len = original_size - offset; - } else { - segment_len = strlen(segment); - } - - /* skip leading separators */ - if (out > buf->ptr && out[-1] == separator) - while (segment_len > 0 && *segment == separator) { - segment++; - segment_len--; - } - - /* copy over next buffer */ - if (segment_len > 0) { - memmove(out, segment, segment_len); - out += segment_len; - } - - /* append trailing separator (except for last item) */ - if (i < nbuf - 1 && out > buf->ptr && out[-1] != separator) - *out++ = separator; - } - va_end(ap); - - /* set size based on num characters actually written */ - buf->size = out - buf->ptr; - buf->ptr[buf->size] = '\0'; - - return 0; -} - -int git_buf_join( - git_buf *buf, - char separator, - const char *str_a, - const char *str_b) -{ - size_t strlen_a = str_a ? strlen(str_a) : 0; - size_t strlen_b = strlen(str_b); - int need_sep = 0; - ssize_t offset_a = -1; - - /* not safe to have str_b point internally to the buffer */ - assert(str_b < buf->ptr || str_b > buf->ptr + buf->size); - - /* figure out if we need to insert a separator */ - if (separator && strlen_a) { - while (*str_b == separator) { str_b++; strlen_b--; } - if (str_a[strlen_a - 1] != separator) - need_sep = 1; - } - - /* str_a could be part of the buffer */ - if (str_a >= buf->ptr && str_a < buf->ptr + buf->size) - offset_a = str_a - buf->ptr; - - if (git_buf_grow(buf, strlen_a + strlen_b + need_sep + 1) < 0) - return -1; - - /* fix up internal pointers */ - if (offset_a >= 0) - str_a = buf->ptr + offset_a; - - /* do the actual copying */ - if (offset_a != 0) - memmove(buf->ptr, str_a, strlen_a); - if (need_sep) - buf->ptr[strlen_a] = separator; - memcpy(buf->ptr + strlen_a + need_sep, str_b, strlen_b); - - buf->size = strlen_a + strlen_b + need_sep; - buf->ptr[buf->size] = '\0'; - - return 0; -} - -void git_buf_rtrim(git_buf *buf) -{ - while (buf->size > 0) { - if (!git__isspace(buf->ptr[buf->size - 1])) - break; - - buf->size--; - } - - buf->ptr[buf->size] = '\0'; -} - -int git_buf_cmp(const git_buf *a, const git_buf *b) -{ - int result = memcmp(a->ptr, b->ptr, min(a->size, b->size)); - return (result != 0) ? result : - (a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0; -} - -int git_buf_splice( - git_buf *buf, - size_t where, - size_t nb_to_remove, - const char *data, - size_t nb_to_insert) -{ - assert(buf && - where <= git_buf_len(buf) && - where + nb_to_remove <= git_buf_len(buf)); - - /* Ported from git.git - * https://github.com/git/git/blob/16eed7c/strbuf.c#L159-176 - */ - if (git_buf_grow(buf, git_buf_len(buf) + nb_to_insert - nb_to_remove) < 0) - return -1; - - memmove(buf->ptr + where + nb_to_insert, - buf->ptr + where + nb_to_remove, - buf->size - where - nb_to_remove); - - memcpy(buf->ptr + where, data, nb_to_insert); - - buf->size = buf->size + nb_to_insert - nb_to_remove; - buf->ptr[buf->size] = '\0'; - return 0; -} diff --git a/src/buffer.h b/src/buffer.h deleted file mode 100644 index 6e73895b4c9..00000000000 --- a/src/buffer.h +++ /dev/null @@ -1,177 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_buffer_h__ -#define INCLUDE_buffer_h__ - -#include "common.h" -#include "git2/strarray.h" -#include - -typedef struct { - char *ptr; - size_t asize, size; -} git_buf; - -extern char git_buf__initbuf[]; -extern char git_buf__oom[]; - -#define GIT_BUF_INIT { git_buf__initbuf, 0, 0 } - -/** - * Initialize a git_buf structure. - * - * For the cases where GIT_BUF_INIT cannot be used to do static - * initialization. - */ -extern void git_buf_init(git_buf *buf, size_t initial_size); - -/** - * Attempt to grow the buffer to hold at least `target_size` bytes. - * - * If the allocation fails, this will return an error. If mark_oom is true, - * this will mark the buffer as invalid for future operations; if false, - * existing buffer content will be preserved, but calling code must handle - * that buffer was not expanded. - */ -extern int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom); - -/** - * Grow the buffer to hold at least `target_size` bytes. - * - * If the allocation fails, this will return an error and the buffer will be - * marked as invalid for future operations, invaliding contents. - * - * @return 0 on success or -1 on failure - */ -GIT_INLINE(int) git_buf_grow(git_buf *buf, size_t target_size) -{ - return git_buf_try_grow(buf, target_size, true); -} - -extern void git_buf_free(git_buf *buf); -extern void git_buf_swap(git_buf *buf_a, git_buf *buf_b); -extern char *git_buf_detach(git_buf *buf); -extern void git_buf_attach(git_buf *buf, char *ptr, size_t asize); - -/** - * Test if there have been any reallocation failures with this git_buf. - * - * Any function that writes to a git_buf can fail due to memory allocation - * issues. If one fails, the git_buf will be marked with an OOM error and - * further calls to modify the buffer will fail. Check git_buf_oom() at the - * end of your sequence and it will be true if you ran out of memory at any - * point with that buffer. - * - * @return false if no error, true if allocation error - */ -GIT_INLINE(bool) git_buf_oom(const git_buf *buf) -{ - return (buf->ptr == git_buf__oom); -} - -/* - * Functions below that return int value error codes will return 0 on - * success or -1 on failure (which generally means an allocation failed). - * Using a git_buf where the allocation has failed with result in -1 from - * all further calls using that buffer. As a result, you can ignore the - * return code of these functions and call them in a series then just call - * git_buf_oom at the end. - */ -int git_buf_set(git_buf *buf, const char *data, size_t len); -int git_buf_sets(git_buf *buf, const char *string); -int git_buf_putc(git_buf *buf, char c); -int git_buf_put(git_buf *buf, const char *data, size_t len); -int git_buf_puts(git_buf *buf, const char *string); -int git_buf_printf(git_buf *buf, const char *format, ...) GIT_FORMAT_PRINTF(2, 3); -int git_buf_vprintf(git_buf *buf, const char *format, va_list ap); -void git_buf_clear(git_buf *buf); -void git_buf_consume(git_buf *buf, const char *end); -void git_buf_truncate(git_buf *buf, size_t len); -void git_buf_rtruncate_at_char(git_buf *path, char separator); - -int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...); -int git_buf_join(git_buf *buf, char separator, const char *str_a, const char *str_b); - -/** - * Join two strings as paths, inserting a slash between as needed. - * @return 0 on success, -1 on failure - */ -GIT_INLINE(int) git_buf_joinpath(git_buf *buf, const char *a, const char *b) -{ - return git_buf_join(buf, '/', a, b); -} - -GIT_INLINE(const char *) git_buf_cstr(const git_buf *buf) -{ - return buf->ptr; -} - -GIT_INLINE(size_t) git_buf_len(const git_buf *buf) -{ - return buf->size; -} - -void git_buf_copy_cstr(char *data, size_t datasize, const git_buf *buf); - -#define git_buf_PUTS(buf, str) git_buf_put(buf, str, sizeof(str) - 1) - -GIT_INLINE(ssize_t) git_buf_rfind_next(git_buf *buf, char ch) -{ - ssize_t idx = (ssize_t)buf->size - 1; - while (idx >= 0 && buf->ptr[idx] == ch) idx--; - while (idx >= 0 && buf->ptr[idx] != ch) idx--; - return idx; -} - -GIT_INLINE(ssize_t) git_buf_rfind(git_buf *buf, char ch) -{ - ssize_t idx = (ssize_t)buf->size - 1; - while (idx >= 0 && buf->ptr[idx] != ch) idx--; - return idx; -} - -GIT_INLINE(ssize_t) git_buf_find(git_buf *buf, char ch) -{ - size_t idx = 0; - while (idx < buf->size && buf->ptr[idx] != ch) idx++; - return (idx == buf->size) ? -1 : (ssize_t)idx; -} - -/* Remove whitespace from the end of the buffer */ -void git_buf_rtrim(git_buf *buf); - -int git_buf_cmp(const git_buf *a, const git_buf *b); - -/* Write data as base64 encoded in buffer */ -int git_buf_put_base64(git_buf *buf, const char *data, size_t len); - -/* - * Insert, remove or replace a portion of the buffer. - * - * @param buf The buffer to work with - * - * @param where The location in the buffer where the transformation - * should be applied. - * - * @param nb_to_remove The number of chars to be removed. 0 to not - * remove any character in the buffer. - * - * @param data A pointer to the data which should be inserted. - * - * @param nb_to_insert The number of chars to be inserted. 0 to not - * insert any character from the buffer. - * - * @return 0 or an error code. - */ -int git_buf_splice( - git_buf *buf, - size_t where, - size_t nb_to_remove, - const char *data, - size_t nb_to_insert); - -#endif diff --git a/src/cache.c b/src/cache.c deleted file mode 100644 index e7f33357722..00000000000 --- a/src/cache.c +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "repository.h" -#include "commit.h" -#include "thread-utils.h" -#include "util.h" -#include "cache.h" -#include "git2/oid.h" - -int git_cache_init(git_cache *cache, size_t size, git_cached_obj_freeptr free_ptr) -{ - if (size < 8) - size = 8; - size = git__size_t_powerof2(size); - - cache->size_mask = size - 1; - cache->lru_count = 0; - cache->free_obj = free_ptr; - - git_mutex_init(&cache->lock); - - cache->nodes = git__malloc(size * sizeof(git_cached_obj *)); - GITERR_CHECK_ALLOC(cache->nodes); - - memset(cache->nodes, 0x0, size * sizeof(git_cached_obj *)); - return 0; -} - -void git_cache_free(git_cache *cache) -{ - size_t i; - - for (i = 0; i < (cache->size_mask + 1); ++i) { - if (cache->nodes[i] != NULL) - git_cached_obj_decref(cache->nodes[i], cache->free_obj); - } - - git_mutex_free(&cache->lock); - git__free(cache->nodes); -} - -void *git_cache_get(git_cache *cache, const git_oid *oid) -{ - uint32_t hash; - git_cached_obj *node = NULL, *result = NULL; - - memcpy(&hash, oid->id, sizeof(hash)); - - if (git_mutex_lock(&cache->lock)) { - giterr_set(GITERR_THREAD, "unable to lock cache mutex"); - return NULL; - } - - { - node = cache->nodes[hash & cache->size_mask]; - - if (node != NULL && git_oid_cmp(&node->oid, oid) == 0) { - git_cached_obj_incref(node); - result = node; - } - } - git_mutex_unlock(&cache->lock); - - return result; -} - -void *git_cache_try_store(git_cache *cache, void *_entry) -{ - git_cached_obj *entry = _entry; - uint32_t hash; - - memcpy(&hash, &entry->oid, sizeof(uint32_t)); - - if (git_mutex_lock(&cache->lock)) { - giterr_set(GITERR_THREAD, "unable to lock cache mutex"); - return NULL; - } - - { - git_cached_obj *node = cache->nodes[hash & cache->size_mask]; - - /* increase the refcount on this object, because - * the cache now owns it */ - git_cached_obj_incref(entry); - - if (node == NULL) { - cache->nodes[hash & cache->size_mask] = entry; - } else if (git_oid_cmp(&node->oid, &entry->oid) == 0) { - git_cached_obj_decref(entry, cache->free_obj); - entry = node; - } else { - git_cached_obj_decref(node, cache->free_obj); - cache->nodes[hash & cache->size_mask] = entry; - } - - /* increase the refcount again, because we are - * returning it to the user */ - git_cached_obj_incref(entry); - - } - git_mutex_unlock(&cache->lock); - - return entry; -} diff --git a/src/cache.h b/src/cache.h deleted file mode 100644 index 7034ec26819..00000000000 --- a/src/cache.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_cache_h__ -#define INCLUDE_cache_h__ - -#include "git2/common.h" -#include "git2/oid.h" -#include "git2/odb.h" - -#include "thread-utils.h" - -#define GIT_DEFAULT_CACHE_SIZE 128 - -typedef void (*git_cached_obj_freeptr)(void *); - -typedef struct { - git_oid oid; - git_atomic refcount; -} git_cached_obj; - -typedef struct { - git_cached_obj **nodes; - git_mutex lock; - - unsigned int lru_count; - size_t size_mask; - git_cached_obj_freeptr free_obj; -} git_cache; - -int git_cache_init(git_cache *cache, size_t size, git_cached_obj_freeptr free_ptr); -void git_cache_free(git_cache *cache); - -void *git_cache_try_store(git_cache *cache, void *entry); -void *git_cache_get(git_cache *cache, const git_oid *oid); - -GIT_INLINE(void) git_cached_obj_incref(void *_obj) -{ - git_cached_obj *obj = _obj; - git_atomic_inc(&obj->refcount); -} - -GIT_INLINE(void) git_cached_obj_decref(void *_obj, git_cached_obj_freeptr free_obj) -{ - git_cached_obj *obj = _obj; - - if (git_atomic_dec(&obj->refcount) == 0) - free_obj(obj); -} - -#endif diff --git a/src/cc-compat.h b/src/cc-compat.h deleted file mode 100644 index a5e4ce17e8f..00000000000 --- a/src/cc-compat.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_compat_h__ -#define INCLUDE_compat_h__ - -/* - * See if our compiler is known to support flexible array members. - */ -#ifndef GIT_FLEX_ARRAY -# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) -# define GIT_FLEX_ARRAY /* empty */ -# elif defined(__GNUC__) -# if (__GNUC__ >= 3) -# define GIT_FLEX_ARRAY /* empty */ -# else -# define GIT_FLEX_ARRAY 0 /* older GNU extension */ -# endif -# endif - -/* Default to safer but a bit wasteful traditional style */ -# ifndef GIT_FLEX_ARRAY -# define GIT_FLEX_ARRAY 1 -# endif -#endif - -#ifdef __GNUC__ -# define GIT_TYPEOF(x) (__typeof__(x)) -#else -# define GIT_TYPEOF(x) -#endif - -#define GIT_UNUSED(x) ((void)(x)) - -/* Define the printf format specifer to use for size_t output */ -#if defined(_MSC_VER) || defined(__MINGW32__) -# define PRIuZ "Iu" -# define PRIxZ "Ix" -#else -# define PRIuZ "zu" -# define PRIxZ "zx" -#endif - -/* Micosoft Visual C/C++ */ -#if defined(_MSC_VER) -/* disable "deprecated function" warnings */ -# pragma warning ( disable : 4996 ) -/* disable "conditional expression is constant" level 4 warnings */ -# pragma warning ( disable : 4127 ) -#endif - -#if defined (_MSC_VER) - typedef unsigned char bool; -# define true 1 -# define false 0 -#else -# include -#endif - -#ifndef va_copy -# ifdef __va_copy -# define va_copy(dst, src) __va_copy(dst, src) -# else -# define va_copy(dst, src) ((dst) = (src)) -# endif -#endif - -#endif /* INCLUDE_compat_h__ */ diff --git a/src/checkout.c b/src/checkout.c deleted file mode 100644 index 40f5732ed94..00000000000 --- a/src/checkout.c +++ /dev/null @@ -1,1370 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include - -#include "checkout.h" - -#include "git2/repository.h" -#include "git2/refs.h" -#include "git2/tree.h" -#include "git2/blob.h" -#include "git2/config.h" -#include "git2/diff.h" -#include "git2/submodule.h" - -#include "refs.h" -#include "repository.h" -#include "filter.h" -#include "blob.h" -#include "diff.h" -#include "pathspec.h" - -/* See docs/checkout-internals.md for more information */ - -enum { - CHECKOUT_ACTION__NONE = 0, - CHECKOUT_ACTION__REMOVE = 1, - CHECKOUT_ACTION__UPDATE_BLOB = 2, - CHECKOUT_ACTION__UPDATE_SUBMODULE = 4, - CHECKOUT_ACTION__CONFLICT = 8, - CHECKOUT_ACTION__MAX = 8, - CHECKOUT_ACTION__DEFER_REMOVE = 16, - CHECKOUT_ACTION__REMOVE_AND_UPDATE = - (CHECKOUT_ACTION__UPDATE_BLOB | CHECKOUT_ACTION__REMOVE), -}; - -typedef struct { - git_repository *repo; - git_diff_list *diff; - git_checkout_opts opts; - bool opts_free_baseline; - char *pfx; - git_index *index; - git_pool pool; - git_vector removes; - git_buf path; - size_t workdir_len; - unsigned int strategy; - int can_symlink; - bool reload_submodules; - size_t total_steps; - size_t completed_steps; -} checkout_data; - -static int checkout_notify( - checkout_data *data, - git_checkout_notify_t why, - const git_diff_delta *delta, - const git_index_entry *wditem) -{ - git_diff_file wdfile; - const git_diff_file *baseline = NULL, *target = NULL, *workdir = NULL; - - if (!data->opts.notify_cb) - return 0; - - if ((why & data->opts.notify_flags) == 0) - return 0; - - if (wditem) { - memset(&wdfile, 0, sizeof(wdfile)); - - git_oid_cpy(&wdfile.oid, &wditem->oid); - wdfile.path = wditem->path; - wdfile.size = wditem->file_size; - wdfile.flags = GIT_DIFF_FILE_VALID_OID; - wdfile.mode = wditem->mode; - - workdir = &wdfile; - } - - if (delta) { - switch (delta->status) { - case GIT_DELTA_UNMODIFIED: - case GIT_DELTA_MODIFIED: - case GIT_DELTA_TYPECHANGE: - default: - baseline = &delta->old_file; - target = &delta->new_file; - break; - case GIT_DELTA_ADDED: - case GIT_DELTA_IGNORED: - case GIT_DELTA_UNTRACKED: - target = &delta->new_file; - break; - case GIT_DELTA_DELETED: - baseline = &delta->old_file; - break; - } - } - - return data->opts.notify_cb( - why, delta ? delta->old_file.path : wditem->path, - baseline, target, workdir, data->opts.notify_payload); -} - -static bool checkout_is_workdir_modified( - checkout_data *data, - const git_diff_file *baseitem, - const git_index_entry *wditem) -{ - git_oid oid; - - /* handle "modified" submodule */ - if (wditem->mode == GIT_FILEMODE_COMMIT) { - git_submodule *sm; - unsigned int sm_status = 0; - const git_oid *sm_oid = NULL; - - if (git_submodule_lookup(&sm, data->repo, wditem->path) < 0 || - git_submodule_status(&sm_status, sm) < 0) - return true; - - if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status)) - return true; - - sm_oid = git_submodule_wd_id(sm); - if (!sm_oid) - return false; - - return (git_oid_cmp(&baseitem->oid, sm_oid) != 0); - } - - /* depending on where base is coming from, we may or may not know - * the actual size of the data, so we can't rely on this shortcut. - */ - if (baseitem->size && wditem->file_size != baseitem->size) - return true; - - if (git_diff__oid_for_file( - data->repo, wditem->path, wditem->mode, - wditem->file_size, &oid) < 0) - return false; - - return (git_oid_cmp(&baseitem->oid, &oid) != 0); -} - -#define CHECKOUT_ACTION_IF(FLAG,YES,NO) \ - ((data->strategy & GIT_CHECKOUT_##FLAG) ? CHECKOUT_ACTION__##YES : CHECKOUT_ACTION__##NO) - -static int checkout_action_common( - checkout_data *data, - int action, - const git_diff_delta *delta, - const git_index_entry *wd) -{ - git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE; - - if (action <= 0) - return action; - - if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) - action = (action & ~CHECKOUT_ACTION__REMOVE); - - if ((action & CHECKOUT_ACTION__UPDATE_BLOB) != 0) { - if (S_ISGITLINK(delta->new_file.mode)) - action = (action & ~CHECKOUT_ACTION__UPDATE_BLOB) | - CHECKOUT_ACTION__UPDATE_SUBMODULE; - - notify = GIT_CHECKOUT_NOTIFY_UPDATED; - } - - if ((action & CHECKOUT_ACTION__CONFLICT) != 0) - notify = GIT_CHECKOUT_NOTIFY_CONFLICT; - - if (notify != GIT_CHECKOUT_NOTIFY_NONE && - checkout_notify(data, notify, delta, wd) != 0) - return GIT_EUSER; - - return action; -} - -static int checkout_action_no_wd( - checkout_data *data, - const git_diff_delta *delta) -{ - int action = CHECKOUT_ACTION__NONE; - - switch (delta->status) { - case GIT_DELTA_UNMODIFIED: /* case 12 */ - if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL)) - return GIT_EUSER; - action = CHECKOUT_ACTION_IF(SAFE_CREATE, UPDATE_BLOB, NONE); - break; - case GIT_DELTA_ADDED: /* case 2 or 28 (and 5 but not really) */ - case GIT_DELTA_MODIFIED: /* case 13 (and 35 but not really) */ - action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE); - break; - case GIT_DELTA_TYPECHANGE: /* case 21 (B->T) and 28 (T->B)*/ - if (delta->new_file.mode == GIT_FILEMODE_TREE) - action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE); - break; - case GIT_DELTA_DELETED: /* case 8 or 25 */ - default: /* impossible */ - break; - } - - return checkout_action_common(data, action, delta, NULL); -} - -static int checkout_action_wd_only( - checkout_data *data, - git_iterator *workdir, - const git_index_entry *wd, - git_vector *pathspec) -{ - bool remove = false; - git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE; - - if (!git_pathspec_match_path( - pathspec, wd->path, - (data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0, - git_iterator_ignore_case(workdir))) - return 0; - - /* check if item is tracked in the index but not in the checkout diff */ - if (data->index != NULL) { - if (wd->mode != GIT_FILEMODE_TREE) { - if (git_index_get_bypath(data->index, wd->path, 0) != NULL) { - notify = GIT_CHECKOUT_NOTIFY_DIRTY; - remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0); - } - } else { - /* for tree entries, we have to see if there are any index - * entries that are contained inside that tree - */ - size_t pos = git_index__prefix_position(data->index, wd->path); - const git_index_entry *e = git_index_get_byindex(data->index, pos); - - if (e != NULL && data->diff->pfxcomp(e->path, wd->path) == 0) { - notify = GIT_CHECKOUT_NOTIFY_DIRTY; - remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0); - } - } - } - - if (notify != GIT_CHECKOUT_NOTIFY_NONE) - /* found in index */; - else if (git_iterator_current_is_ignored(workdir)) { - notify = GIT_CHECKOUT_NOTIFY_IGNORED; - remove = ((data->strategy & GIT_CHECKOUT_REMOVE_IGNORED) != 0); - } - else { - notify = GIT_CHECKOUT_NOTIFY_UNTRACKED; - remove = ((data->strategy & GIT_CHECKOUT_REMOVE_UNTRACKED) != 0); - } - - if (checkout_notify(data, notify, NULL, wd)) - return GIT_EUSER; - - if (remove) { - char *path = git_pool_strdup(&data->pool, wd->path); - GITERR_CHECK_ALLOC(path); - - if (git_vector_insert(&data->removes, path) < 0) - return -1; - } - - return 0; -} - -static bool submodule_is_config_only( - checkout_data *data, - const char *path) -{ - git_submodule *sm = NULL; - unsigned int sm_loc = 0; - - if (git_submodule_lookup(&sm, data->repo, path) < 0 || - git_submodule_location(&sm_loc, sm) < 0 || - sm_loc == GIT_SUBMODULE_STATUS_IN_CONFIG) - return true; - - return false; -} - -static int checkout_action_with_wd( - checkout_data *data, - const git_diff_delta *delta, - const git_index_entry *wd) -{ - int action = CHECKOUT_ACTION__NONE; - - switch (delta->status) { - case GIT_DELTA_UNMODIFIED: /* case 14/15 or 33 */ - if (checkout_is_workdir_modified(data, &delta->old_file, wd)) { - if (checkout_notify( - data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd)) - return GIT_EUSER; - action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, NONE); - } - break; - case GIT_DELTA_ADDED: /* case 3, 4 or 6 */ - action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT); - break; - case GIT_DELTA_DELETED: /* case 9 or 10 (or 26 but not really) */ - if (checkout_is_workdir_modified(data, &delta->old_file, wd)) - action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT); - else - action = CHECKOUT_ACTION_IF(SAFE, REMOVE, NONE); - break; - case GIT_DELTA_MODIFIED: /* case 16, 17, 18 (or 36 but not really) */ - if (checkout_is_workdir_modified(data, &delta->old_file, wd)) - action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT); - else - action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE); - break; - case GIT_DELTA_TYPECHANGE: /* case 22, 23, 29, 30 */ - if (delta->old_file.mode == GIT_FILEMODE_TREE) { - if (wd->mode == GIT_FILEMODE_TREE) - /* either deleting items in old tree will delete the wd dir, - * or we'll get a conflict when we attempt blob update... - */ - action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE); - else if (wd->mode == GIT_FILEMODE_COMMIT) { - /* workdir is possibly a "phantom" submodule - treat as a - * tree if the only submodule info came from the config - */ - if (submodule_is_config_only(data, wd->path)) - action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE); - else - action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); - } else - action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT); - } - else if (checkout_is_workdir_modified(data, &delta->old_file, wd)) - action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); - else - action = CHECKOUT_ACTION_IF(SAFE, REMOVE_AND_UPDATE, NONE); - - /* don't update if the typechange is to a tree */ - if (delta->new_file.mode == GIT_FILEMODE_TREE) - action = (action & ~CHECKOUT_ACTION__UPDATE_BLOB); - break; - default: /* impossible */ - break; - } - - return checkout_action_common(data, action, delta, wd); -} - -static int checkout_action_with_wd_blocker( - checkout_data *data, - const git_diff_delta *delta, - const git_index_entry *wd) -{ - int action = CHECKOUT_ACTION__NONE; - - switch (delta->status) { - case GIT_DELTA_UNMODIFIED: - /* should show delta as dirty / deleted */ - if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd)) - return GIT_EUSER; - action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, NONE); - break; - case GIT_DELTA_ADDED: - case GIT_DELTA_MODIFIED: - action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); - break; - case GIT_DELTA_DELETED: - action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT); - break; - case GIT_DELTA_TYPECHANGE: - /* not 100% certain about this... */ - action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); - break; - default: /* impossible */ - break; - } - - return checkout_action_common(data, action, delta, wd); -} - -static int checkout_action_with_wd_dir( - checkout_data *data, - const git_diff_delta *delta, - const git_index_entry *wd) -{ - int action = CHECKOUT_ACTION__NONE; - - switch (delta->status) { - case GIT_DELTA_UNMODIFIED: /* case 19 or 24 (or 34 but not really) */ - if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL) || - checkout_notify( - data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd)) - return GIT_EUSER; - break; - case GIT_DELTA_ADDED:/* case 4 (and 7 for dir) */ - case GIT_DELTA_MODIFIED: /* case 20 (or 37 but not really) */ - if (delta->old_file.mode == GIT_FILEMODE_COMMIT) - /* expected submodule (and maybe found one) */; - else if (delta->new_file.mode != GIT_FILEMODE_TREE) - action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); - break; - case GIT_DELTA_DELETED: /* case 11 (and 27 for dir) */ - if (delta->old_file.mode != GIT_FILEMODE_TREE && - checkout_notify( - data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd)) - return GIT_EUSER; - break; - case GIT_DELTA_TYPECHANGE: /* case 24 or 31 */ - if (delta->old_file.mode == GIT_FILEMODE_TREE) { - /* For typechange from dir, remove dir and add blob, but it is - * not safe to remove dir if it contains modified files. - * However, safely removing child files will remove the parent - * directory if is it left empty, so we can defer removing the - * dir and it will succeed if no children are left. - */ - action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE); - if (action != CHECKOUT_ACTION__NONE) - action |= CHECKOUT_ACTION__DEFER_REMOVE; - } - else if (delta->new_file.mode != GIT_FILEMODE_TREE) - /* For typechange to dir, dir is already created so no action */ - action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); - break; - default: /* impossible */ - break; - } - - return checkout_action_common(data, action, delta, wd); -} - -static int checkout_action( - checkout_data *data, - git_diff_delta *delta, - git_iterator *workdir, - const git_index_entry **wditem_ptr, - git_vector *pathspec) -{ - const git_index_entry *wd = *wditem_ptr; - int cmp = -1, act; - int (*strcomp)(const char *, const char *) = data->diff->strcomp; - int (*pfxcomp)(const char *str, const char *pfx) = data->diff->pfxcomp; - - /* move workdir iterator to follow along with deltas */ - - while (1) { - if (!wd) - return checkout_action_no_wd(data, delta); - - cmp = strcomp(wd->path, delta->old_file.path); - - /* 1. wd before delta ("a/a" before "a/b") - * 2. wd prefixes delta & should expand ("a/" before "a/b") - * 3. wd prefixes delta & cannot expand ("a/b" before "a/b/c") - * 4. wd equals delta ("a/b" and "a/b") - * 5. wd after delta & delta prefixes wd ("a/b/c" after "a/b/" or "a/b") - * 6. wd after delta ("a/c" after "a/b") - */ - - if (cmp < 0) { - cmp = pfxcomp(delta->old_file.path, wd->path); - - if (cmp == 0) { - if (wd->mode == GIT_FILEMODE_TREE) { - /* case 2 - entry prefixed by workdir tree */ - if (git_iterator_advance_into_directory(workdir, &wd) < 0) - goto fail; - continue; - } - - /* case 3 maybe - wd contains non-dir where dir expected */ - if (delta->old_file.path[strlen(wd->path)] == '/') { - act = checkout_action_with_wd_blocker(data, delta, wd); - *wditem_ptr = - git_iterator_advance(workdir, &wd) ? NULL : wd; - return act; - } - } - - /* case 1 - handle wd item (if it matches pathspec) */ - if (checkout_action_wd_only(data, workdir, wd, pathspec) < 0 || - git_iterator_advance(workdir, &wd) < 0) - goto fail; - - *wditem_ptr = wd; - continue; - } - - if (cmp == 0) { - /* case 4 */ - act = checkout_action_with_wd(data, delta, wd); - *wditem_ptr = git_iterator_advance(workdir, &wd) ? NULL : wd; - return act; - } - - cmp = pfxcomp(wd->path, delta->old_file.path); - - if (cmp == 0) { /* case 5 */ - if (wd->path[strlen(delta->old_file.path)] != '/') - return checkout_action_no_wd(data, delta); - - if (delta->status == GIT_DELTA_TYPECHANGE) { - if (delta->old_file.mode == GIT_FILEMODE_TREE) { - act = checkout_action_with_wd(data, delta, wd); - if (git_iterator_advance_into_directory(workdir, &wd) < 0) - wd = NULL; - *wditem_ptr = wd; - return act; - } - - if (delta->new_file.mode == GIT_FILEMODE_TREE || - delta->new_file.mode == GIT_FILEMODE_COMMIT || - delta->old_file.mode == GIT_FILEMODE_COMMIT) - { - act = checkout_action_with_wd(data, delta, wd); - if (git_iterator_advance(workdir, &wd) < 0) - wd = NULL; - *wditem_ptr = wd; - return act; - } - } - - return checkout_action_with_wd_dir(data, delta, wd); - } - - /* case 6 - wd is after delta */ - return checkout_action_no_wd(data, delta); - } - -fail: - *wditem_ptr = NULL; - return -1; -} - -static int checkout_remaining_wd_items( - checkout_data *data, - git_iterator *workdir, - const git_index_entry *wd, - git_vector *spec) -{ - int error = 0; - - while (wd && !error) { - if (!(error = checkout_action_wd_only(data, workdir, wd, spec))) - error = git_iterator_advance(workdir, &wd); - } - - return error; -} - -static int checkout_get_actions( - uint32_t **actions_ptr, - size_t **counts_ptr, - checkout_data *data, - git_iterator *workdir) -{ - int error = 0; - const git_index_entry *wditem; - git_vector pathspec = GIT_VECTOR_INIT, *deltas; - git_pool pathpool = GIT_POOL_INIT_STRINGPOOL; - git_diff_delta *delta; - size_t i, *counts = NULL; - uint32_t *actions = NULL; - - if (data->opts.paths.count > 0 && - git_pathspec_init(&pathspec, &data->opts.paths, &pathpool) < 0) - return -1; - - if ((error = git_iterator_current(workdir, &wditem)) < 0) - goto fail; - - deltas = &data->diff->deltas; - - *counts_ptr = counts = git__calloc(CHECKOUT_ACTION__MAX+1, sizeof(size_t)); - *actions_ptr = actions = git__calloc( - deltas->length ? deltas->length : 1, sizeof(uint32_t)); - if (!counts || !actions) { - error = -1; - goto fail; - } - - git_vector_foreach(deltas, i, delta) { - int act = checkout_action(data, delta, workdir, &wditem, &pathspec); - - if (act < 0) { - error = act; - goto fail; - } - - actions[i] = act; - - if (act & CHECKOUT_ACTION__REMOVE) - counts[CHECKOUT_ACTION__REMOVE]++; - if (act & CHECKOUT_ACTION__UPDATE_BLOB) - counts[CHECKOUT_ACTION__UPDATE_BLOB]++; - if (act & CHECKOUT_ACTION__UPDATE_SUBMODULE) - counts[CHECKOUT_ACTION__UPDATE_SUBMODULE]++; - if (act & CHECKOUT_ACTION__CONFLICT) - counts[CHECKOUT_ACTION__CONFLICT]++; - } - - error = checkout_remaining_wd_items(data, workdir, wditem, &pathspec); - if (error < 0) - goto fail; - - counts[CHECKOUT_ACTION__REMOVE] += data->removes.length; - - if (counts[CHECKOUT_ACTION__CONFLICT] > 0 && - (data->strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) == 0) - { - giterr_set(GITERR_CHECKOUT, "%d conflicts prevent checkout", - (int)counts[CHECKOUT_ACTION__CONFLICT]); - error = GIT_EMERGECONFLICT; - goto fail; - } - - git_pathspec_free(&pathspec); - git_pool_clear(&pathpool); - - return 0; - -fail: - *counts_ptr = NULL; - git__free(counts); - *actions_ptr = NULL; - git__free(actions); - - git_pathspec_free(&pathspec); - git_pool_clear(&pathpool); - - return error; -} - -static int buffer_to_file( - struct stat *st, - git_buf *buffer, - const char *path, - mode_t dir_mode, - int file_open_flags, - mode_t file_mode) -{ - int fd, error; - - if ((error = git_futils_mkpath2file(path, dir_mode)) < 0) - return error; - - if ((fd = p_open(path, file_open_flags, file_mode)) < 0) { - giterr_set(GITERR_OS, "Could not open '%s' for writing", path); - return fd; - } - - if ((error = p_write(fd, git_buf_cstr(buffer), git_buf_len(buffer))) < 0) { - giterr_set(GITERR_OS, "Could not write to '%s'", path); - (void)p_close(fd); - } else { - if ((error = p_fstat(fd, st)) < 0) - giterr_set(GITERR_OS, "Error while statting '%s'", path); - - if ((error = p_close(fd)) < 0) - giterr_set(GITERR_OS, "Error while closing '%s'", path); - } - - if (!error && - (file_mode & 0100) != 0 && - (error = p_chmod(path, file_mode)) < 0) - giterr_set(GITERR_OS, "Failed to set permissions on '%s'", path); - - return error; -} - -static int blob_content_to_file( - struct stat *st, - git_blob *blob, - const char *path, - mode_t entry_filemode, - git_checkout_opts *opts) -{ - int error = -1, nb_filters = 0; - mode_t file_mode = opts->file_mode; - bool dont_free_filtered = false; - git_buf unfiltered = GIT_BUF_INIT, filtered = GIT_BUF_INIT; - git_vector filters = GIT_VECTOR_INIT; - - /* Create a fake git_buf from the blob raw data... */ - filtered.ptr = blob->odb_object->raw.data; - filtered.size = blob->odb_object->raw.len; - /* ... and make sure it doesn't get unexpectedly freed */ - dont_free_filtered = true; - - if (!opts->disable_filters && - !git_buf_text_is_binary(&filtered) && - (nb_filters = git_filters_load( - &filters, - git_object_owner((git_object *)blob), - path, - GIT_FILTER_TO_WORKTREE)) > 0) - { - /* reset 'filtered' so it can be a filter target */ - git_buf_init(&filtered, 0); - dont_free_filtered = false; - } - - if (nb_filters < 0) - return nb_filters; - - if (nb_filters > 0) { - if ((error = git_blob__getbuf(&unfiltered, blob)) < 0) - goto cleanup; - - if ((error = git_filters_apply(&filtered, &unfiltered, &filters)) < 0) - goto cleanup; - } - - /* Allow overriding of file mode */ - if (!file_mode) - file_mode = entry_filemode; - - error = buffer_to_file( - st, &filtered, path, opts->dir_mode, opts->file_open_flags, file_mode); - - if (!error) - st->st_mode = entry_filemode; - -cleanup: - git_filters_free(&filters); - git_buf_free(&unfiltered); - if (!dont_free_filtered) - git_buf_free(&filtered); - - return error; -} - -static int blob_content_to_link( - struct stat *st, git_blob *blob, const char *path, int can_symlink) -{ - git_buf linktarget = GIT_BUF_INIT; - int error; - - if ((error = git_blob__getbuf(&linktarget, blob)) < 0) - return error; - - if (can_symlink) { - if ((error = p_symlink(git_buf_cstr(&linktarget), path)) < 0) - giterr_set(GITERR_CHECKOUT, "Could not create symlink %s\n", path); - } else { - error = git_futils_fake_symlink(git_buf_cstr(&linktarget), path); - } - - if (!error) { - if ((error = p_lstat(path, st)) < 0) - giterr_set(GITERR_CHECKOUT, "Could not stat symlink %s", path); - - st->st_mode = GIT_FILEMODE_LINK; - } - - git_buf_free(&linktarget); - - return error; -} - -static int checkout_update_index( - checkout_data *data, - const git_diff_file *file, - struct stat *st) -{ - git_index_entry entry; - - if (!data->index) - return 0; - - memset(&entry, 0, sizeof(entry)); - entry.path = (char *)file->path; /* cast to prevent warning */ - git_index_entry__init_from_stat(&entry, st); - git_oid_cpy(&entry.oid, &file->oid); - - return git_index_add(data->index, &entry); -} - -static int checkout_submodule( - checkout_data *data, - const git_diff_file *file) -{ - int error = 0; - git_submodule *sm; - - /* Until submodules are supported, UPDATE_ONLY means do nothing here */ - if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) - return 0; - - if ((error = git_futils_mkdir( - file->path, git_repository_workdir(data->repo), - data->opts.dir_mode, GIT_MKDIR_PATH)) < 0) - return error; - - if ((error = git_submodule_lookup(&sm, data->repo, file->path)) < 0) - return error; - - /* TODO: Support checkout_strategy options. Two circumstances: - * 1 - submodule already checked out, but we need to move the HEAD - * to the new OID, or - * 2 - submodule not checked out and we should recursively check it out - * - * Checkout will not execute a pull on the submodule, but a clone - * command should probably be able to. Do we need a submodule callback? - */ - - /* update the index unless prevented */ - if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0) { - struct stat st; - - git_buf_truncate(&data->path, data->workdir_len); - if (git_buf_puts(&data->path, file->path) < 0) - return -1; - - if ((error = p_stat(git_buf_cstr(&data->path), &st)) < 0) { - giterr_set( - GITERR_CHECKOUT, "Could not stat submodule %s\n", file->path); - return error; - } - - st.st_mode = GIT_FILEMODE_COMMIT; - - error = checkout_update_index(data, file, &st); - } - - return error; -} - -static void report_progress( - checkout_data *data, - const char *path) -{ - if (data->opts.progress_cb) - data->opts.progress_cb( - path, data->completed_steps, data->total_steps, - data->opts.progress_payload); -} - -static int checkout_safe_for_update_only(const char *path, mode_t expected_mode) -{ - struct stat st; - - if (p_lstat(path, &st) < 0) { - /* if doesn't exist, then no error and no update */ - if (errno == ENOENT || errno == ENOTDIR) - return 0; - - /* otherwise, stat error and no update */ - giterr_set(GITERR_OS, "Failed to stat file '%s'", path); - return -1; - } - - /* only safe for update if this is the same type of file */ - if ((st.st_mode & ~0777) == (expected_mode & ~0777)) - return 1; - - return 0; -} - -static int checkout_blob( - checkout_data *data, - const git_diff_file *file) -{ - int error = 0; - git_blob *blob; - struct stat st; - - git_buf_truncate(&data->path, data->workdir_len); - if (git_buf_puts(&data->path, file->path) < 0) - return -1; - - if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) { - int rval = checkout_safe_for_update_only( - git_buf_cstr(&data->path), file->mode); - if (rval <= 0) - return rval; - } - - if ((error = git_blob_lookup(&blob, data->repo, &file->oid)) < 0) - return error; - - if (S_ISLNK(file->mode)) - error = blob_content_to_link( - &st, blob, git_buf_cstr(&data->path), data->can_symlink); - else - error = blob_content_to_file( - &st, blob, git_buf_cstr(&data->path), file->mode, &data->opts); - - git_blob_free(blob); - - /* if we try to create the blob and an existing directory blocks it from - * being written, then there must have been a typechange conflict in a - * parent directory - suppress the error and try to continue. - */ - if ((data->strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) != 0 && - (error == GIT_ENOTFOUND || error == GIT_EEXISTS)) - { - giterr_clear(); - error = 0; - } - - /* update the index unless prevented */ - if (!error && (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0) - error = checkout_update_index(data, file, &st); - - /* update the submodule data if this was a new .gitmodules file */ - if (!error && strcmp(file->path, ".gitmodules") == 0) - data->reload_submodules = true; - - return error; -} - -static int checkout_remove_the_old( - unsigned int *actions, - checkout_data *data) -{ - int error = 0; - git_diff_delta *delta; - const char *str; - size_t i; - const char *workdir = git_buf_cstr(&data->path); - uint32_t flg = GIT_RMDIR_EMPTY_PARENTS | - GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS; - - git_buf_truncate(&data->path, data->workdir_len); - - git_vector_foreach(&data->diff->deltas, i, delta) { - if (actions[i] & CHECKOUT_ACTION__REMOVE) { - error = git_futils_rmdir_r(delta->old_file.path, workdir, flg); - if (error < 0) - return error; - - data->completed_steps++; - report_progress(data, delta->old_file.path); - - if ((actions[i] & CHECKOUT_ACTION__UPDATE_BLOB) == 0 && - (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0 && - data->index != NULL) - { - (void)git_index_remove(data->index, delta->old_file.path, 0); - } - } - } - - git_vector_foreach(&data->removes, i, str) { - error = git_futils_rmdir_r(str, workdir, flg); - if (error < 0) - return error; - - data->completed_steps++; - report_progress(data, str); - - if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0 && - data->index != NULL) - { - if (str[strlen(str) - 1] == '/') - (void)git_index_remove_directory(data->index, str, 0); - else - (void)git_index_remove(data->index, str, 0); - } - } - - return 0; -} - -static int checkout_deferred_remove(git_repository *repo, const char *path) -{ -#if 0 - int error = git_futils_rmdir_r( - path, git_repository_workdir(repo), GIT_RMDIR_EMPTY_PARENTS); - - if (error == GIT_ENOTFOUND) { - error = 0; - giterr_clear(); - } - - return error; -#else - GIT_UNUSED(repo); - GIT_UNUSED(path); - assert(false); - return 0; -#endif -} - -static int checkout_create_the_new( - unsigned int *actions, - checkout_data *data) -{ - int error = 0; - git_diff_delta *delta; - size_t i; - - git_vector_foreach(&data->diff->deltas, i, delta) { - if (actions[i] & CHECKOUT_ACTION__DEFER_REMOVE) { - /* this had a blocker directory that should only be removed iff - * all of the contents of the directory were safely removed - */ - if ((error = checkout_deferred_remove( - data->repo, delta->old_file.path)) < 0) - return error; - } - - if (actions[i] & CHECKOUT_ACTION__UPDATE_BLOB) { - error = checkout_blob(data, &delta->new_file); - if (error < 0) - return error; - - data->completed_steps++; - report_progress(data, delta->new_file.path); - } - } - - return 0; -} - -static int checkout_create_submodules( - unsigned int *actions, - checkout_data *data) -{ - int error = 0; - git_diff_delta *delta; - size_t i; - - /* initial reload of submodules if .gitmodules was changed */ - if (data->reload_submodules && - (error = git_submodule_reload_all(data->repo)) < 0) - return error; - - git_vector_foreach(&data->diff->deltas, i, delta) { - if (actions[i] & CHECKOUT_ACTION__DEFER_REMOVE) { - /* this has a blocker directory that should only be removed iff - * all of the contents of the directory were safely removed - */ - if ((error = checkout_deferred_remove( - data->repo, delta->old_file.path)) < 0) - return error; - } - - if (actions[i] & CHECKOUT_ACTION__UPDATE_SUBMODULE) { - int error = checkout_submodule(data, &delta->new_file); - if (error < 0) - return error; - - data->completed_steps++; - report_progress(data, delta->new_file.path); - } - } - - /* final reload once submodules have been updated */ - return git_submodule_reload_all(data->repo); -} - -static int checkout_lookup_head_tree(git_tree **out, git_repository *repo) -{ - int error = 0; - git_reference *ref = NULL; - git_object *head; - - if (!(error = git_repository_head(&ref, repo)) && - !(error = git_reference_peel(&head, ref, GIT_OBJ_TREE))) - *out = (git_tree *)head; - - git_reference_free(ref); - - return error; -} - -static void checkout_data_clear(checkout_data *data) -{ - if (data->opts_free_baseline) { - git_tree_free(data->opts.baseline); - data->opts.baseline = NULL; - } - - git_vector_free(&data->removes); - git_pool_clear(&data->pool); - - git__free(data->pfx); - data->pfx = NULL; - - git_buf_free(&data->path); - - git_index_free(data->index); - data->index = NULL; -} - -static int checkout_data_init( - checkout_data *data, - git_iterator *target, - git_checkout_opts *proposed) -{ - int error = 0; - git_config *cfg; - git_repository *repo = git_iterator_owner(target); - - memset(data, 0, sizeof(*data)); - - if (!repo) { - giterr_set(GITERR_CHECKOUT, "Cannot checkout nothing"); - return -1; - } - - if ((error = git_repository__ensure_not_bare(repo, "checkout")) < 0) - return error; - - if ((error = git_repository_config__weakptr(&cfg, repo)) < 0) - return error; - - data->repo = repo; - - GITERR_CHECK_VERSION( - proposed, GIT_CHECKOUT_OPTS_VERSION, "git_checkout_opts"); - - if (!proposed) - GIT_INIT_STRUCTURE(&data->opts, GIT_CHECKOUT_OPTS_VERSION); - else - memmove(&data->opts, proposed, sizeof(git_checkout_opts)); - - /* refresh config and index content unless NO_REFRESH is given */ - if ((data->opts.checkout_strategy & GIT_CHECKOUT_NO_REFRESH) == 0) { - if ((error = git_config_refresh(cfg)) < 0) - goto cleanup; - - if (git_iterator_inner_type(target) == GIT_ITERATOR_TYPE_INDEX) { - /* if we are iterating over the index, don't reload */ - data->index = git_iterator_index_get_index(target); - GIT_REFCOUNT_INC(data->index); - } else { - /* otherwise, grab and reload the index */ - if ((error = git_repository_index(&data->index, data->repo)) < 0 || - (error = git_index_read(data->index)) < 0) - goto cleanup; - } - } - - /* if you are forcing, definitely allow safe updates */ - if ((data->opts.checkout_strategy & GIT_CHECKOUT_FORCE) != 0) - data->opts.checkout_strategy |= GIT_CHECKOUT_SAFE_CREATE; - if ((data->opts.checkout_strategy & GIT_CHECKOUT_SAFE_CREATE) != 0) - data->opts.checkout_strategy |= GIT_CHECKOUT_SAFE; - - data->strategy = data->opts.checkout_strategy; - - /* opts->disable_filters is false by default */ - - if (!data->opts.dir_mode) - data->opts.dir_mode = GIT_DIR_MODE; - - if (!data->opts.file_open_flags) - data->opts.file_open_flags = O_CREAT | O_TRUNC | O_WRONLY; - - data->pfx = git_pathspec_prefix(&data->opts.paths); - - error = git_config_get_bool(&data->can_symlink, cfg, "core.symlinks"); - if (error < 0) { - if (error != GIT_ENOTFOUND) - goto cleanup; - - /* If "core.symlinks" is not found anywhere, default to true. */ - data->can_symlink = true; - giterr_clear(); - error = 0; - } - - if (!data->opts.baseline) { - data->opts_free_baseline = true; - error = checkout_lookup_head_tree(&data->opts.baseline, repo); - - if (error == GIT_EORPHANEDHEAD) { - error = 0; - giterr_clear(); - } - - if (error < 0) - goto cleanup; - } - - if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 || - (error = git_pool_init(&data->pool, 1, 0)) < 0 || - (error = git_buf_puts(&data->path, git_repository_workdir(repo))) < 0) - goto cleanup; - - data->workdir_len = git_buf_len(&data->path); - -cleanup: - if (error < 0) - checkout_data_clear(data); - - return error; -} - -int git_checkout_iterator( - git_iterator *target, - git_checkout_opts *opts) -{ - int error = 0; - git_iterator *baseline = NULL, *workdir = NULL; - checkout_data data = {0}; - git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; - uint32_t *actions = NULL; - size_t *counts = NULL; - git_iterator_flag_t iterflags = 0; - - /* initialize structures and options */ - error = checkout_data_init(&data, target, opts); - if (error < 0) - return error; - - diff_opts.flags = - GIT_DIFF_INCLUDE_UNMODIFIED | - GIT_DIFF_INCLUDE_UNTRACKED | - GIT_DIFF_RECURSE_UNTRACKED_DIRS | /* needed to match baseline */ - GIT_DIFF_INCLUDE_IGNORED | - GIT_DIFF_INCLUDE_TYPECHANGE | - GIT_DIFF_INCLUDE_TYPECHANGE_TREES | - GIT_DIFF_SKIP_BINARY_CHECK; - if (data.opts.checkout_strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) - diff_opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH; - if (data.opts.paths.count > 0) - diff_opts.pathspec = data.opts.paths; - - /* set up iterators */ - - iterflags = git_iterator_ignore_case(target) ? - GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE; - - if ((error = git_iterator_reset(target, data.pfx, data.pfx)) < 0 || - (error = git_iterator_for_workdir_range( - &workdir, data.repo, iterflags, data.pfx, data.pfx)) < 0 || - (error = git_iterator_for_tree_range( - &baseline, data.opts.baseline, iterflags, data.pfx, data.pfx)) < 0) - goto cleanup; - - /* Handle case insensitivity for baseline if necessary */ - if (git_iterator_ignore_case(workdir) != git_iterator_ignore_case(baseline)) - if ((error = git_iterator_spoolandsort_push(baseline, true)) < 0) - goto cleanup; - - /* Generate baseline-to-target diff which will include an entry for - * every possible update that might need to be made. - */ - if ((error = git_diff__from_iterators( - &data.diff, data.repo, baseline, target, &diff_opts)) < 0) - goto cleanup; - - /* Loop through diff (and working directory iterator) building a list of - * actions to be taken, plus look for conflicts and send notifications. - */ - if ((error = checkout_get_actions(&actions, &counts, &data, workdir)) < 0) - goto cleanup; - - data.total_steps = counts[CHECKOUT_ACTION__REMOVE] + - counts[CHECKOUT_ACTION__UPDATE_BLOB] + - counts[CHECKOUT_ACTION__UPDATE_SUBMODULE]; - - report_progress(&data, NULL); /* establish 0 baseline */ - - /* To deal with some order dependencies, perform remaining checkout - * in three passes: removes, then update blobs, then update submodules. - */ - if (counts[CHECKOUT_ACTION__REMOVE] > 0 && - (error = checkout_remove_the_old(actions, &data)) < 0) - goto cleanup; - - if (counts[CHECKOUT_ACTION__UPDATE_BLOB] > 0 && - (error = checkout_create_the_new(actions, &data)) < 0) - goto cleanup; - - if (counts[CHECKOUT_ACTION__UPDATE_SUBMODULE] > 0 && - (error = checkout_create_submodules(actions, &data)) < 0) - goto cleanup; - - assert(data.completed_steps == data.total_steps); - -cleanup: - if (error == GIT_EUSER) - giterr_clear(); - - if (!error && data.index != NULL && - (data.strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0) - error = git_index_write(data.index); - - git_diff_list_free(data.diff); - git_iterator_free(workdir); - git_iterator_free(baseline); - git__free(actions); - git__free(counts); - checkout_data_clear(&data); - - return error; -} - -int git_checkout_index( - git_repository *repo, - git_index *index, - git_checkout_opts *opts) -{ - int error; - git_iterator *index_i; - - if ((error = git_repository__ensure_not_bare(repo, "checkout index")) < 0) - return error; - - if (!index && (error = git_repository_index__weakptr(&index, repo)) < 0) - return error; - GIT_REFCOUNT_INC(index); - - if (!(error = git_iterator_for_index(&index_i, index))) - error = git_checkout_iterator(index_i, opts); - - git_iterator_free(index_i); - git_index_free(index); - - return error; -} - -int git_checkout_tree( - git_repository *repo, - const git_object *treeish, - git_checkout_opts *opts) -{ - int error; - git_tree *tree = NULL; - git_iterator *tree_i = NULL; - - if ((error = git_repository__ensure_not_bare(repo, "checkout tree")) < 0) - return error; - - if (git_object_peel((git_object **)&tree, treeish, GIT_OBJ_TREE) < 0) { - giterr_set( - GITERR_CHECKOUT, "Provided object cannot be peeled to a tree"); - return -1; - } - - if (!(error = git_iterator_for_tree(&tree_i, tree))) - error = git_checkout_iterator(tree_i, opts); - - git_iterator_free(tree_i); - git_tree_free(tree); - - return error; -} - -int git_checkout_head( - git_repository *repo, - git_checkout_opts *opts) -{ - int error; - git_tree *head = NULL; - git_iterator *head_i = NULL; - - if ((error = git_repository__ensure_not_bare(repo, "checkout head")) < 0) - return error; - - if (!(error = checkout_lookup_head_tree(&head, repo)) && - !(error = git_iterator_for_tree(&head_i, head))) - error = git_checkout_iterator(head_i, opts); - - git_iterator_free(head_i); - git_tree_free(head); - - return error; -} diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt new file mode 100644 index 00000000000..d121c588a6c --- /dev/null +++ b/src/cli/CMakeLists.txt @@ -0,0 +1,56 @@ +set(CLI_INCLUDES + "${libgit2_BINARY_DIR}/src/util" + "${libgit2_BINARY_DIR}/include" + "${libgit2_SOURCE_DIR}/src/util" + "${libgit2_SOURCE_DIR}/src/cli" + "${libgit2_SOURCE_DIR}/include" + "${LIBGIT2_DEPENDENCY_INCLUDES}" + "${LIBGIT2_SYSTEM_INCLUDES}") + +if(WIN32 AND NOT CYGWIN) + file(GLOB CLI_SRC_OS win32/*.c) + list(SORT CLI_SRC_OS) +else() + file(GLOB CLI_SRC_OS unix/*.c) + list(SORT CLI_SRC_OS) +endif() + +file(GLOB CLI_SRC_C *.c *.h) +list(SORT CLI_SRC_C) + +# +# The CLI currently needs to be statically linked against libgit2 because +# the utility library uses libgit2's thread-local error buffers. TODO: +# remove this dependency and allow us to dynamically link against libgit2. +# + +if(BUILD_CLI STREQUAL "dynamic") + set(CLI_LIBGIT2_LIBRARY libgit2package) +else() + set(CLI_LIBGIT2_OBJECTS $) +endif() + +# +# Compile and link the CLI +# + +add_executable(git2_cli ${CLI_SRC_C} ${CLI_SRC_OS} ${CLI_OBJECTS} + $ + ${CLI_LIBGIT2_OBJECTS} + ${LIBGIT2_DEPENDENCY_OBJECTS}) +target_link_libraries(git2_cli ${CLI_LIBGIT2_LIBRARY} ${LIBGIT2_SYSTEM_LIBS}) + +set_target_properties(git2_cli PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${libgit2_BINARY_DIR}) +set_target_properties(git2_cli PROPERTIES OUTPUT_NAME ${LIBGIT2_FILENAME}) + +ide_split_sources(git2_cli) + +target_include_directories(git2_cli PRIVATE ${CLI_INCLUDES}) + +if(MSVC_IDE) + # Precompiled headers + set_target_properties(git2_cli PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") + set_source_files_properties(win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h") +endif() + +install(TARGETS git2_cli RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/src/cli/README.md b/src/cli/README.md new file mode 100644 index 00000000000..3087c39c425 --- /dev/null +++ b/src/cli/README.md @@ -0,0 +1,26 @@ +# cli + +A git-compatible command-line interface that uses libgit2. + +## Adding commands + +1. Individual commands have a `main`-like top-level entrypoint. For example: + + ```c + int cmd_help(int argc, char **argv) + ``` + + Although this is the same signature as `main`, commands are not built as + individual standalone executables, they'll be linked into the main cli. + (Though there may be an option for command executables to be built as + standalone executables in the future.) + +2. Commands are prototyped in `cmd.h` and added to `main.c`'s list of + commands (`cli_cmds[]`). Commands should be specified with their name, + entrypoint and a brief description that can be printed in `git help`. + This is done because commands are linked into the main cli. + +3. Commands should accept a `--help` option that displays their help + information. This will be shown when a user runs ` --help` and + when a user runs `help `. + diff --git a/src/cli/cmd.c b/src/cli/cmd.c new file mode 100644 index 00000000000..0b1fafb4423 --- /dev/null +++ b/src/cli/cmd.c @@ -0,0 +1,21 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" +#include "cmd.h" + +const cli_cmd_spec *cli_cmd_spec_byname(const char *name) +{ + const cli_cmd_spec *cmd; + + for (cmd = cli_cmds; cmd->name; cmd++) { + if (!strcmp(cmd->name, name)) + return cmd; + } + + return NULL; +} diff --git a/src/cli/cmd.h b/src/cli/cmd.h new file mode 100644 index 00000000000..bce4709fb7a --- /dev/null +++ b/src/cli/cmd.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef CLI_cmd_h__ +#define CLI_cmd_h__ + +/* Command definitions */ +typedef struct { + const char *name; + int (*fn)(int argc, char **argv); + const char *desc; +} cli_cmd_spec; + +/* Options that are common to all commands (eg --help, --git-dir) */ +extern const cli_opt_spec cli_common_opts[]; + +/* All the commands supported by the CLI */ +extern const cli_cmd_spec cli_cmds[]; + +/* Find a command by name */ +extern const cli_cmd_spec *cli_cmd_spec_byname(const char *name); + +/* Commands */ +extern int cmd_blame(int argc, char **argv); +extern int cmd_cat_file(int argc, char **argv); +extern int cmd_clone(int argc, char **argv); +extern int cmd_config(int argc, char **argv); +extern int cmd_hash_object(int argc, char **argv); +extern int cmd_help(int argc, char **argv); +extern int cmd_index_pack(int argc, char **argv); +extern int cmd_init(int argc, char **argv); +extern int cmd_version(int argc, char **argv); + +#endif /* CLI_cmd_h__ */ diff --git a/src/cli/cmd_blame.c b/src/cli/cmd_blame.c new file mode 100644 index 00000000000..180a948f987 --- /dev/null +++ b/src/cli/cmd_blame.c @@ -0,0 +1,287 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include +#include "common.h" +#include "cmd.h" +#include "error.h" +#include "sighandler.h" +#include "progress.h" + +#include "fs_path.h" +#include "futils.h" +#include "date.h" +#include "hashmap.h" + +#define COMMAND_NAME "blame" + +static char *file; +static int porcelain, line_porcelain; + +static const cli_opt_spec opts[] = { + CLI_COMMON_OPT, + + { CLI_OPT_TYPE_SWITCH, "porcelain", 'p', &porcelain, 1, + CLI_OPT_USAGE_DEFAULT, NULL, "show machine readable output" }, + { CLI_OPT_TYPE_SWITCH, "line-porcelain", 0, &line_porcelain, 1, + CLI_OPT_USAGE_DEFAULT, NULL, "show individual lines in machine readable output" }, + { CLI_OPT_TYPE_LITERAL }, + { CLI_OPT_TYPE_ARG, "file", 0, &file, 0, + CLI_OPT_USAGE_REQUIRED, "file", "file to blame" }, + + { 0 } +}; + +static void print_help(void) +{ + cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts, 0); + printf("\n"); + + printf("Show the origin of each line of a file.\n"); + printf("\n"); + + printf("Options:\n"); + + cli_opt_help_fprint(stdout, opts); +} + +static int strintlen(size_t n) +{ + int len = 1; + + while (n > 10) { + n /= 10; + len++; + + if (len == INT_MAX) + break; + } + + return len; +} + +static int fmt_date(git_str *out, git_time_t time, int offset) +{ + time_t t; + struct tm gmt; + + GIT_ASSERT_ARG(out); + + t = (time_t)(time + offset * 60); + + if (p_gmtime_r(&t, &gmt) == NULL) + return -1; + + return git_str_printf(out, "%.4u-%02u-%02u %02u:%02u:%02u %+03d%02d", + gmt.tm_year + 1900, gmt.tm_mon + 1, gmt.tm_mday, + gmt.tm_hour, gmt.tm_min, gmt.tm_sec, + offset / 60, offset % 60); +} + +static int print_standard(git_blame *blame) +{ + size_t max_line_number = 0; + int max_lineno_len, max_line_len, max_author_len = 0, max_path_len = 0; + const char *last_path = NULL; + const git_blame_line *line; + bool paths_differ = false; + git_str date_str = GIT_STR_INIT; + size_t i; + int ret = 0; + + /* Compute the maximum size of things */ + for (i = 0; i < git_blame_hunkcount(blame); i++) { + const git_blame_hunk *hunk = git_blame_hunk_byindex(blame, i); + size_t hunk_author_len = strlen(hunk->orig_signature->name); + size_t hunk_path_len = strlen(hunk->orig_path); + size_t hunk_max_line_number = + hunk->orig_start_line_number + hunk->lines_in_hunk; + + if (hunk_max_line_number > max_line_number) + max_line_number = hunk_max_line_number; + + if (hunk_author_len > INT_MAX) + max_author_len = INT_MAX; + else if ((int)hunk_author_len > max_author_len) + max_author_len = (int)hunk_author_len; + + if (hunk_path_len > INT_MAX) + hunk_path_len = INT_MAX; + else if ((int)hunk_path_len > max_path_len) + max_path_len = (int)hunk_path_len; + + if (!paths_differ && last_path != NULL && + strcmp(last_path, hunk->orig_path) != 0) { + paths_differ = true; + } + + last_path = hunk->orig_path; + } + + max_lineno_len = strintlen(max_line_number); + + max_author_len--; + + for (i = 1; i < git_blame_linecount(blame); i++) { + const git_blame_hunk *hunk = git_blame_hunk_byline(blame, i); + int oid_abbrev; + + if (!hunk) + break; + + oid_abbrev = hunk->boundary ? 7 : 8; + printf("%s%.*s ", hunk->boundary ? "^" : "", + oid_abbrev, git_oid_tostr_s(&hunk->orig_commit_id)); + + if (paths_differ) + printf("%-*.*s ", max_path_len, max_path_len, hunk->orig_path); + + git_str_clear(&date_str); + if (fmt_date(&date_str, + hunk->orig_signature->when.time, + hunk->orig_signature->when.offset) < 0) { + ret = cli_error_git(); + goto done; + } + + if ((line = git_blame_line_byindex(blame, i)) == NULL) { + ret = cli_error_git(); + goto done; + } + + max_line_len = (int)min(line->len, INT_MAX); + + printf("(%-*.*s %s %*" PRIuZ ") %.*s" , + max_author_len, max_author_len, hunk->orig_signature->name, + date_str.ptr, + max_lineno_len, i, + max_line_len, line->ptr); + printf("\n"); + } + +done: + git_str_dispose(&date_str); + return ret; +} + +GIT_INLINE(uint32_t) oid_hashcode(const git_oid *oid) +{ + uint32_t hash; + memcpy(&hash, oid->id, sizeof(uint32_t)); + return hash; +} + +GIT_HASHSET_SETUP(git_blame_commitmap, const git_oid *, oid_hashcode, git_oid_equal); + +static int print_porcelain(git_blame *blame) +{ + git_blame_commitmap seen_ids = GIT_HASHSET_INIT; + size_t i, j; + + for (i = 0; i < git_blame_hunkcount(blame); i++) { + const git_blame_line *line; + const git_blame_hunk *hunk = git_blame_hunk_byindex(blame, i); + + for (j = 0; j < hunk->lines_in_hunk; j++) { + size_t line_number = hunk->final_start_line_number + j; + bool seen = git_blame_commitmap_contains(&seen_ids, &hunk->orig_commit_id); + + printf("%s %" PRIuZ " %" PRIuZ, + git_oid_tostr_s(&hunk->orig_commit_id), + hunk->orig_start_line_number + j, + hunk->final_start_line_number + j); + + if (!j) + printf(" %" PRIuZ, hunk->lines_in_hunk); + + printf("\n"); + + if ((!j && !seen) || line_porcelain) { + printf("author %s\n", hunk->orig_signature->name); + printf("author-mail <%s>\n", hunk->orig_signature->email); + printf("author-time %" PRId64 "\n", hunk->orig_signature->when.time); + printf("author-tz %+03d%02d\n", + hunk->orig_signature->when.offset / 60, + hunk->orig_signature->when.offset % 60); + + printf("committer %s\n", hunk->orig_committer->name); + printf("committer-mail <%s>\n", hunk->orig_committer->email); + printf("committer-time %" PRId64 "\n", hunk->orig_committer->when.time); + printf("committer-tz %+03d%02d\n", + hunk->orig_committer->when.offset / 60, + hunk->orig_committer->when.offset % 60); + + printf("summary %s\n", hunk->summary); + + /* TODO: previous */ + + printf("filename %s\n", hunk->orig_path); + } + + if ((line = git_blame_line_byindex(blame, line_number)) == NULL) + return cli_error_git(); + + printf("\t%.*s\n", (int)min(line->len, INT_MAX), + line->ptr); + + if (!seen) + git_blame_commitmap_add(&seen_ids, &hunk->orig_commit_id); + } + } + + git_blame_commitmap_dispose(&seen_ids); + return 0; +} + +int cmd_blame(int argc, char **argv) +{ + cli_repository_open_options open_opts = { argv + 1, argc - 1 }; + git_blame_options blame_opts = GIT_BLAME_OPTIONS_INIT; + git_repository *repo = NULL; + git_str workdir_path = GIT_STR_INIT; + git_blame *blame = NULL; + cli_opt invalid_opt; + int ret = 0; + + blame_opts.flags |= GIT_BLAME_USE_MAILMAP; + + if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU)) + return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt); + + if (cli_opt__show_help) { + print_help(); + return 0; + } + + if (!file) { + ret = cli_error_usage("you must specify a file to blame"); + goto done; + } + + if (cli_repository_open(&repo, &open_opts) < 0) + return cli_error_git(); + + if ((ret = cli_resolve_path(&workdir_path, repo, file)) != 0) + goto done; + + if (git_blame_file(&blame, repo, workdir_path.ptr, &blame_opts) < 0) { + ret = cli_error_git(); + goto done; + } + + if (porcelain || line_porcelain) + ret = print_porcelain(blame); + else + ret = print_standard(blame); + +done: + git_str_dispose(&workdir_path); + git_blame_free(blame); + git_repository_free(repo); + return ret; +} diff --git a/src/cli/cmd_cat_file.c b/src/cli/cmd_cat_file.c new file mode 100644 index 00000000000..95a0240cae8 --- /dev/null +++ b/src/cli/cmd_cat_file.c @@ -0,0 +1,202 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include "common.h" +#include "cmd.h" + +#define COMMAND_NAME "cat-file" + +typedef enum { + DISPLAY_CONTENT = 0, + DISPLAY_EXISTS, + DISPLAY_PRETTY, + DISPLAY_SIZE, + DISPLAY_TYPE +} display_t; + +static int display = DISPLAY_CONTENT; +static char *type_name, *object_spec; + +static const cli_opt_spec opts[] = { + CLI_COMMON_OPT, + + { CLI_OPT_TYPE_SWITCH, NULL, 't', &display, DISPLAY_TYPE, + CLI_OPT_USAGE_REQUIRED, NULL, "display the type of the object" }, + { CLI_OPT_TYPE_SWITCH, NULL, 's', &display, DISPLAY_SIZE, + CLI_OPT_USAGE_CHOICE, NULL, "display the size of the object" }, + { CLI_OPT_TYPE_SWITCH, NULL, 'e', &display, DISPLAY_EXISTS, + CLI_OPT_USAGE_CHOICE, NULL, "displays nothing unless the object is corrupt" }, + { CLI_OPT_TYPE_SWITCH, NULL, 'p', &display, DISPLAY_PRETTY, + CLI_OPT_USAGE_CHOICE, NULL, "pretty-print the object" }, + { CLI_OPT_TYPE_ARG, "type", 0, &type_name, 0, + CLI_OPT_USAGE_CHOICE, "type", "the type of object to display" }, + { CLI_OPT_TYPE_ARG, "object", 0, &object_spec, 0, + CLI_OPT_USAGE_REQUIRED, "object", "the object to display" }, + { 0 }, +}; + +static void print_help(void) +{ + cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts, 0); + printf("\n"); + + printf("Display the content for the given object in the repository.\n"); + printf("\n"); + + printf("Options:\n"); + + cli_opt_help_fprint(stdout, opts); +} + +static int print_odb(git_object *object, display_t display) +{ + git_odb *odb = NULL; + git_odb_object *odb_object = NULL; + const unsigned char *content; + git_object_size_t size; + int ret = 0; + + /* + * Our parsed blobs retain the raw content; all other objects are + * parsed into a working representation. To get the raw content, + * we need to do an ODB lookup. (Thankfully, this should be cached + * in-memory from our last call.) + */ + if (git_object_type(object) == GIT_OBJECT_BLOB) { + content = git_blob_rawcontent((git_blob *)object); + size = git_blob_rawsize((git_blob *)object); + } else { + if (git_repository_odb(&odb, git_object_owner(object)) < 0 || + git_odb_read(&odb_object, odb, git_object_id(object)) < 0) { + ret = cli_error_git(); + goto done; + } + + content = git_odb_object_data(odb_object); + size = git_odb_object_size(odb_object); + } + + switch (display) { + case DISPLAY_SIZE: + if (printf("%" PRIu64 "\n", size) < 0) + ret = cli_error_os(); + break; + case DISPLAY_CONTENT: + if (p_write(fileno(stdout), content, (size_t)size) < 0) + ret = cli_error_os(); + break; + default: + GIT_ASSERT(0); + } + +done: + git_odb_object_free(odb_object); + git_odb_free(odb); + return ret; +} + +static int print_type(git_object *object) +{ + if (printf("%s\n", git_object_type2string(git_object_type(object))) < 0) + return cli_error_os(); + + return 0; +} + +static int print_pretty(git_object *object) +{ + const git_tree_entry *entry; + size_t i, count; + + /* + * Only trees are stored in an unreadable format and benefit from + * pretty-printing. + */ + if (git_object_type(object) != GIT_OBJECT_TREE) + return print_odb(object, DISPLAY_CONTENT); + + for (i = 0, count = git_tree_entrycount((git_tree *)object); i < count; i++) { + entry = git_tree_entry_byindex((git_tree *)object, i); + + if (printf("%06o %s %s\t%s\n", + git_tree_entry_filemode_raw(entry), + git_object_type2string(git_tree_entry_type(entry)), + git_oid_tostr_s(git_tree_entry_id(entry)), + git_tree_entry_name(entry)) < 0) + return cli_error_os(); + } + + return 0; +} + +int cmd_cat_file(int argc, char **argv) +{ + cli_repository_open_options open_opts = { argv + 1, argc - 1}; + git_repository *repo = NULL; + git_object *object = NULL; + git_object_t type; + cli_opt invalid_opt; + int giterr, ret = 0; + + if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU)) + return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt); + + if (cli_opt__show_help) { + print_help(); + return 0; + } + + if (cli_repository_open(&repo, &open_opts) < 0) + return cli_error_git(); + + if ((giterr = git_revparse_single(&object, repo, object_spec)) < 0) { + if (display == DISPLAY_EXISTS && giterr == GIT_ENOTFOUND) + ret = 1; + else + ret = cli_error_git(); + + goto done; + } + + if (type_name) { + git_object *peeled; + + if ((type = git_object_string2type(type_name)) == GIT_OBJECT_INVALID) { + ret = cli_error_usage("invalid object type '%s'", type_name); + goto done; + } + + if (git_object_peel(&peeled, object, type) < 0) { + ret = cli_error_git(); + goto done; + } + + git_object_free(object); + object = peeled; + } + + switch (display) { + case DISPLAY_EXISTS: + ret = 0; + break; + case DISPLAY_TYPE: + ret = print_type(object); + break; + case DISPLAY_PRETTY: + ret = print_pretty(object); + break; + default: + ret = print_odb(object, display); + break; + } + +done: + git_object_free(object); + git_repository_free(repo); + return ret; +} diff --git a/src/cli/cmd_clone.c b/src/cli/cmd_clone.c new file mode 100644 index 00000000000..c18cb28d4e0 --- /dev/null +++ b/src/cli/cmd_clone.c @@ -0,0 +1,190 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include +#include "common.h" +#include "cmd.h" +#include "error.h" +#include "sighandler.h" +#include "progress.h" + +#include "fs_path.h" +#include "futils.h" + +#define COMMAND_NAME "clone" + +static char *branch, *remote_path, *local_path, *depth; +static int quiet, checkout = 1, bare; +static bool local_path_exists; +static cli_progress progress = CLI_PROGRESS_INIT; + +static const cli_opt_spec opts[] = { + CLI_COMMON_OPT, + + { CLI_OPT_TYPE_SWITCH, "quiet", 'q', &quiet, 1, + CLI_OPT_USAGE_DEFAULT, NULL, "display the type of the object" }, + { CLI_OPT_TYPE_SWITCH, "no-checkout", 'n', &checkout, 0, + CLI_OPT_USAGE_DEFAULT, NULL, "don't checkout HEAD" }, + { CLI_OPT_TYPE_SWITCH, "bare", 0, &bare, 1, + CLI_OPT_USAGE_DEFAULT, NULL, "don't create a working directory" }, + { CLI_OPT_TYPE_VALUE, "branch", 'b', &branch, 0, + CLI_OPT_USAGE_DEFAULT, "name", "branch to check out" }, + { CLI_OPT_TYPE_VALUE, "depth", 0, &depth, 0, + CLI_OPT_USAGE_DEFAULT, "depth", "commit depth to check out " }, + { CLI_OPT_TYPE_LITERAL }, + { CLI_OPT_TYPE_ARG, "repository", 0, &remote_path, 0, + CLI_OPT_USAGE_REQUIRED, "repository", "repository path" }, + { CLI_OPT_TYPE_ARG, "directory", 0, &local_path, 0, + CLI_OPT_USAGE_DEFAULT, "directory", "directory to clone into" }, + { 0 } +}; + +static void print_help(void) +{ + cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts, 0); + printf("\n"); + + printf("Clone a repository into a new directory.\n"); + printf("\n"); + + printf("Options:\n"); + + cli_opt_help_fprint(stdout, opts); +} + +static char *compute_local_path(const char *orig_path) +{ + const char *slash; + char *local_path; + + if ((slash = strrchr(orig_path, '/')) == NULL && + (slash = strrchr(orig_path, '\\')) == NULL) + local_path = git__strdup(orig_path); + else + local_path = git__strdup(slash + 1); + + return local_path; +} + +static int compute_depth(const char *depth) +{ + int64_t i; + const char *endptr; + + if (!depth) + return 0; + + if (git__strntol64(&i, depth, strlen(depth), &endptr, 10) < 0 || i < 0 || i > INT_MAX || *endptr) { + fprintf(stderr, "fatal: depth '%s' is not valid.\n", depth); + exit(128); + } + + return (int)i; +} + +static bool validate_local_path(const char *path) +{ + if (!git_fs_path_exists(path)) + return false; + + if (!git_fs_path_isdir(path) || !git_fs_path_is_empty_dir(path)) { + fprintf(stderr, "fatal: destination path '%s' already exists and is not an empty directory.\n", + path); + exit(128); + } + + return true; +} + +static void cleanup(void) +{ + int rmdir_flags = GIT_RMDIR_REMOVE_FILES; + + cli_progress_abort(&progress); + + if (local_path_exists) + rmdir_flags |= GIT_RMDIR_SKIP_ROOT; + + if (!git_fs_path_isdir(local_path)) + return; + + git_futils_rmdir_r(local_path, NULL, rmdir_flags); +} + +static void interrupt_cleanup(void) +{ + cleanup(); + exit(130); +} + +int cmd_clone(int argc, char **argv) +{ + git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; + git_repository *repo = NULL; + cli_opt invalid_opt; + char *computed_path = NULL; + int ret = 0; + + if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU)) + return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt); + + if (cli_opt__show_help) { + print_help(); + return 0; + } + + if (!remote_path) { + ret = cli_error_usage("you must specify a repository to clone"); + goto done; + } + + clone_opts.bare = !!bare; + clone_opts.checkout_branch = branch; + clone_opts.fetch_opts.depth = compute_depth(depth); + + if (!checkout) + clone_opts.checkout_opts.checkout_strategy = GIT_CHECKOUT_NONE; + + if (!local_path) + local_path = computed_path = compute_local_path(remote_path); + + local_path_exists = validate_local_path(local_path); + + cli_sighandler_set_interrupt(interrupt_cleanup); + + if (!local_path_exists && + git_futils_mkdir(local_path, 0777, 0) < 0) { + ret = cli_error_git(); + goto done; + } + + if (!quiet) { + clone_opts.fetch_opts.callbacks.sideband_progress = cli_progress_fetch_sideband; + clone_opts.fetch_opts.callbacks.transfer_progress = cli_progress_fetch_transfer; + clone_opts.fetch_opts.callbacks.payload = &progress; + + clone_opts.checkout_opts.progress_cb = cli_progress_checkout; + clone_opts.checkout_opts.progress_payload = &progress; + + printf("Cloning into '%s'...\n", local_path); + } + + if (git_clone(&repo, remote_path, local_path, &clone_opts) < 0) { + cleanup(); + ret = cli_error_git(); + goto done; + } + + cli_progress_finish(&progress); + +done: + cli_progress_dispose(&progress); + git__free(computed_path); + git_repository_free(repo); + return ret; +} diff --git a/src/cli/cmd_config.c b/src/cli/cmd_config.c new file mode 100644 index 00000000000..9056e81f0b7 --- /dev/null +++ b/src/cli/cmd_config.c @@ -0,0 +1,241 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include + +#include "common.h" +#include "cmd.h" + +#define COMMAND_NAME "config" + +typedef enum { + ACTION_NONE = 0, + ACTION_GET, + ACTION_ADD, + ACTION_REPLACE_ALL, + ACTION_LIST +} action_t; + +static action_t action = ACTION_NONE; +static int show_origin; +static int show_scope; +static int null_separator; +static int config_level; +static char *config_filename; +static char *name, *value, *value_pattern; + +static const cli_opt_spec opts[] = { + CLI_COMMON_OPT, \ + + { CLI_OPT_TYPE_SWITCH, "null", 'z', &null_separator, 1, + 0, NULL, "use NUL as a separator" }, + + { CLI_OPT_TYPE_SWITCH, "system", 0, &config_level, GIT_CONFIG_LEVEL_SYSTEM, + 0, NULL, "read/write to system configuration" }, + { CLI_OPT_TYPE_SWITCH, "global", 0, &config_level, GIT_CONFIG_LEVEL_GLOBAL, + CLI_OPT_USAGE_CHOICE, NULL, "read/write to global configuration" }, + { CLI_OPT_TYPE_SWITCH, "local", 0, &config_level, GIT_CONFIG_LEVEL_LOCAL, + CLI_OPT_USAGE_CHOICE, NULL, "read/write to local configuration" }, + { CLI_OPT_TYPE_VALUE, "file", 0, &config_filename, 0, + CLI_OPT_USAGE_CHOICE, "filename", "read/write to specified configuration file" }, + + { CLI_OPT_TYPE_SWITCH, "get", 0, &action, ACTION_GET, + CLI_OPT_USAGE_REQUIRED, NULL, "get a configuration value" }, + { CLI_OPT_TYPE_SWITCH, "add", 0, &action, ACTION_ADD, + CLI_OPT_USAGE_CHOICE, NULL, "add a configuration value" }, + { CLI_OPT_TYPE_SWITCH, "replace-all", 0, &action, ACTION_REPLACE_ALL, + CLI_OPT_USAGE_CHOICE, NULL, "add a configuration value, replacing any old values" }, + { CLI_OPT_TYPE_SWITCH, "list", 'l', &action, ACTION_LIST, + CLI_OPT_USAGE_CHOICE | CLI_OPT_USAGE_SHOW_LONG, + NULL, "list all configuration entries" }, + { CLI_OPT_TYPE_SWITCH, "show-origin", 0, &show_origin, 1, + 0, NULL, "show origin of configuration" }, + { CLI_OPT_TYPE_SWITCH, "show-scope", 0, &show_scope, 1, + 0, NULL, "show scope of configuration" }, + { CLI_OPT_TYPE_ARG, "name", 0, &name, 0, + 0, "name", "name of configuration entry" }, + { CLI_OPT_TYPE_ARG, "value", 0, &value, 0, + 0, "value", "value of configuration entry" }, + { CLI_OPT_TYPE_ARG, "regexp", 0, &value_pattern, 0, + 0, "regexp", "regular expression of values to replace" }, + { 0 }, +}; + +static void print_help(void) +{ + cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts, 0); + printf("\n"); + + printf("Query and set configuration options.\n"); + printf("\n"); + + printf("Options:\n"); + + cli_opt_help_fprint(stdout, opts); +} + +static int get_config(git_config *config) +{ + git_buf value = GIT_BUF_INIT; + char sep = null_separator ? '\0' : '\n'; + int error; + + error = git_config_get_string_buf(&value, config, name); + + if (error && error != GIT_ENOTFOUND) + return cli_error_git(); + + else if (error == GIT_ENOTFOUND) + return 1; + + printf("%s%c", value.ptr, sep); + return 0; +} + +static int add_config(git_config *config) +{ + if (git_config_set_multivar(config, name, "$^", value) < 0) + return cli_error_git(); + + return 0; +} + +static int replace_all_config(git_config *config) +{ + if (git_config_set_multivar(config, name, value_pattern ? value_pattern : ".*", value) < 0) + return cli_error_git(); + + return 0; +} + +static const char *level_name(git_config_level_t level) +{ + switch (level) { + case GIT_CONFIG_LEVEL_PROGRAMDATA: + return "programdata"; + case GIT_CONFIG_LEVEL_SYSTEM: + return "system"; + case GIT_CONFIG_LEVEL_XDG: + return "global"; + case GIT_CONFIG_LEVEL_GLOBAL: + return "global"; + case GIT_CONFIG_LEVEL_LOCAL: + return "local"; + case GIT_CONFIG_LEVEL_APP: + return "command"; + default: + return "unknown"; + } +} + +static int list_config(git_config *config) +{ + git_config_iterator *iterator; + git_config_entry *entry; + char data_separator = null_separator ? '\0' : '\t'; + char kv_separator = null_separator ? '\n' : '='; + char entry_separator = null_separator ? '\0' : '\n'; + int error; + + if (git_config_iterator_new(&iterator, config) < 0) + return cli_error_git(); + + while ((error = git_config_next(&entry, iterator)) == 0) { + if (show_scope) + printf("%s%c", + level_name(entry->level), + data_separator); + + if (show_origin) + printf("%s%s%s%c", + entry->backend_type ? entry->backend_type : "", + entry->backend_type && entry->origin_path ? ":" : "", + entry->origin_path ? entry->origin_path : "", + data_separator); + + printf("%s%c%s%c", entry->name, kv_separator, entry->value, + entry_separator); + } + + if (error != GIT_ITEROVER) + return cli_error_git(); + + git_config_iterator_free(iterator); + return 0; +} + +int cmd_config(int argc, char **argv) +{ + git_repository *repo = NULL; + git_config *config = NULL; + cli_repository_open_options open_opts = { argv + 1, argc - 1}; + cli_opt invalid_opt; + int ret = 0; + + if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU)) + return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt); + + if (cli_opt__show_help) { + print_help(); + return 0; + } + + if (config_filename) { + if (git_config_new(&config) < 0 || + git_config_add_file_ondisk(config, config_filename, + GIT_CONFIG_LEVEL_APP, NULL, 0) < 0) { + ret = cli_error_git(); + goto done; + } + } else { + if (cli_repository_open(&repo, &open_opts) < 0 || + git_repository_config(&config, repo) < 0) { + ret = cli_error_git(); + goto done; + } + + if (config_level && + git_config_open_level(&config, config, config_level) < 0) { + ret = cli_error_git(); + goto done; + } + } + + switch (action) { + case ACTION_ADD: + if (!name || !value || value_pattern) + ret = cli_error_usage("%s --add requires two arguments", COMMAND_NAME); + else + ret = add_config(config); + break; + case ACTION_REPLACE_ALL: + if (!name || !value) + ret = cli_error_usage("%s --replace-all requires two or three arguments", COMMAND_NAME); + else + ret = replace_all_config(config); + break; + case ACTION_GET: + if (!name) + ret = cli_error_usage("%s --get requires an argument", COMMAND_NAME); + else + ret = get_config(config); + break; + case ACTION_LIST: + if (name) + ret = cli_error_usage("%s --list does not take an argument", COMMAND_NAME); + else + ret = list_config(config); + break; + default: + ret = cli_error_usage("unknown action"); + } + +done: + git_config_free(config); + git_repository_free(repo); + return ret; +} diff --git a/src/cli/cmd_hash_object.c b/src/cli/cmd_hash_object.c new file mode 100644 index 00000000000..94e7eb91f0d --- /dev/null +++ b/src/cli/cmd_hash_object.c @@ -0,0 +1,152 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include "common.h" +#include "cmd.h" + +#include "futils.h" + +#define COMMAND_NAME "hash-object" + +static char *type_name; +static int write_object, read_stdin, literally; +static char **filenames; + +static const cli_opt_spec opts[] = { + CLI_COMMON_OPT, + + { CLI_OPT_TYPE_VALUE, NULL, 't', &type_name, 0, + CLI_OPT_USAGE_DEFAULT, "type", "the type of object to hash (default: \"blob\")" }, + { CLI_OPT_TYPE_SWITCH, NULL, 'w', &write_object, 1, + CLI_OPT_USAGE_DEFAULT, NULL, "write the object to the object database" }, + { CLI_OPT_TYPE_SWITCH, "literally", 0, &literally, 1, + CLI_OPT_USAGE_DEFAULT, NULL, "do not validate the object contents" }, + { CLI_OPT_TYPE_SWITCH, "stdin", 0, &read_stdin, 1, + CLI_OPT_USAGE_REQUIRED, NULL, "read content from stdin" }, + { CLI_OPT_TYPE_ARGS, "file", 0, &filenames, 0, + CLI_OPT_USAGE_CHOICE, "file", "the file (or files) to read and hash" }, + { 0 }, +}; + +static void print_help(void) +{ + cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts, 0); + printf("\n"); + + printf("Compute the object ID for a given file and optionally write that file\nto the object database.\n"); + printf("\n"); + + printf("Options:\n"); + + cli_opt_help_fprint(stdout, opts); +} + +static int hash_buf( + git_odb *odb, + git_str *buf, + git_object_t object_type, + git_oid_t oid_type) +{ + git_oid oid; + + if (!literally) { + int valid = 0; + +#ifdef GIT_EXPERIMENTAL_SHA256 + if (git_object_rawcontent_is_valid(&valid, buf->ptr, buf->size, object_type, oid_type) < 0 || !valid) + return cli_error_git(); +#else + GIT_UNUSED(oid_type); + + if (git_object_rawcontent_is_valid(&valid, buf->ptr, buf->size, object_type) < 0 || !valid) + return cli_error_git(); +#endif + } + + if (write_object) { + if (git_odb_write(&oid, odb, buf->ptr, buf->size, object_type) < 0) + return cli_error_git(); + } else { +#ifdef GIT_EXPERIMENTAL_SHA256 + if (git_odb_hash(&oid, buf->ptr, buf->size, object_type, GIT_OID_SHA1) < 0) + return cli_error_git(); +#else + if (git_odb_hash(&oid, buf->ptr, buf->size, object_type) < 0) + return cli_error_git(); +#endif + } + + if (printf("%s\n", git_oid_tostr_s(&oid)) < 0) + return cli_error_os(); + + return 0; +} + +int cmd_hash_object(int argc, char **argv) +{ + cli_repository_open_options open_opts = { argv + 1, argc - 1}; + git_repository *repo = NULL; + git_odb *odb = NULL; + git_oid_t oid_type; + git_str buf = GIT_STR_INIT; + cli_opt invalid_opt; + git_object_t object_type = GIT_OBJECT_BLOB; + char **filename; + int ret = 0; + + if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU)) + return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt); + + if (cli_opt__show_help) { + print_help(); + return 0; + } + + if (type_name && (object_type = git_object_string2type(type_name)) == GIT_OBJECT_INVALID) + return cli_error_usage("invalid object type '%s'", type_name); + + if (write_object && + (cli_repository_open(&repo, &open_opts) < 0 || + git_repository_odb(&odb, repo) < 0)) { + ret = cli_error_git(); + goto done; + } + + oid_type = git_repository_oid_type(repo); + + /* + * TODO: we're reading blobs, we shouldn't pull them all into main + * memory, we should just stream them into the odb instead. + * (Or create a `git_odb_writefile` API.) + */ + if (read_stdin) { + if (git_futils_readbuffer_fd_full(&buf, fileno(stdin)) < 0) { + ret = cli_error_git(); + goto done; + } + + if ((ret = hash_buf(odb, &buf, object_type, oid_type)) != 0) + goto done; + } else { + for (filename = filenames; *filename; filename++) { + if (git_futils_readbuffer(&buf, *filename) < 0) { + ret = cli_error_git(); + goto done; + } + + if ((ret = hash_buf(odb, &buf, object_type, oid_type)) != 0) + goto done; + } + } + +done: + git_str_dispose(&buf); + git_odb_free(odb); + git_repository_free(repo); + return ret; +} diff --git a/src/cli/cmd_help.c b/src/cli/cmd_help.c new file mode 100644 index 00000000000..c01163d3c89 --- /dev/null +++ b/src/cli/cmd_help.c @@ -0,0 +1,85 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include +#include "common.h" +#include "cmd.h" + +#define COMMAND_NAME "help" + +static char *command; + +static const cli_opt_spec opts[] = { + CLI_COMMON_OPT, + + { CLI_OPT_TYPE_ARG, "command", 0, &command, 0, + CLI_OPT_USAGE_DEFAULT, "command", "the command to show help for" }, + { 0 }, +}; + +static int print_help(void) +{ + cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts, CLI_OPT_USAGE_SHOW_HIDDEN); + printf("\n"); + + printf("Display help information about %s. If a command is specified, help\n", PROGRAM_NAME); + printf("about that command will be shown. Otherwise, general information about\n"); + printf("%s will be shown, including the commands available.\n", PROGRAM_NAME); + + return 0; +} + +static int print_commands(void) +{ + const cli_cmd_spec *cmd; + + cli_opt_usage_fprint(stdout, PROGRAM_NAME, NULL, cli_common_opts, CLI_OPT_USAGE_SHOW_HIDDEN); + printf("\n"); + + printf("These are the %s commands available:\n\n", PROGRAM_NAME); + + for (cmd = cli_cmds; cmd->name; cmd++) + printf(" %-11s %s\n", cmd->name, cmd->desc); + + printf("\nSee '%s help ' for more information on a specific command.\n", PROGRAM_NAME); + + return 0; +} + +int cmd_help(int argc, char **argv) +{ + char *fake_args[2]; + const cli_cmd_spec *cmd; + cli_opt invalid_opt; + + if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU)) + return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt); + + /* Show the meta-help */ + if (cli_opt__show_help) + return print_help(); + + /* We were not asked to show help for a specific command. */ + if (!command) + return print_commands(); + + /* + * If we were asked for help for a command (eg, `help `), + * delegate back to that command's `--help` option. This lets + * commands own their help. Emulate the command-line arguments + * that would invoke ` --help` and invoke that command. + */ + fake_args[0] = command; + fake_args[1] = "--help"; + + if ((cmd = cli_cmd_spec_byname(command)) == NULL) + return cli_error("'%s' is not a %s command. See '%s help'.", + command, PROGRAM_NAME, PROGRAM_NAME); + + return cmd->fn(2, fake_args); +} diff --git a/src/cli/cmd_index_pack.c b/src/cli/cmd_index_pack.c new file mode 100644 index 00000000000..6a67990b47d --- /dev/null +++ b/src/cli/cmd_index_pack.c @@ -0,0 +1,114 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include "common.h" +#include "cmd.h" +#include "progress.h" + +#define COMMAND_NAME "index-pack" + +#define BUFFER_SIZE (1024 * 1024) + +static int verbose, read_stdin; +static char *filename; +static cli_progress progress = CLI_PROGRESS_INIT; + +static const cli_opt_spec opts[] = { + CLI_COMMON_OPT, + + { CLI_OPT_TYPE_SWITCH, "verbose", 'v', &verbose, 1, + CLI_OPT_USAGE_DEFAULT, NULL, "display progress output" }, + + { CLI_OPT_TYPE_LITERAL }, + + { CLI_OPT_TYPE_SWITCH, "stdin", 0, &read_stdin, 1, + CLI_OPT_USAGE_REQUIRED, NULL, "read from stdin" }, + { CLI_OPT_TYPE_ARG, "pack-file", 0, &filename, 0, + CLI_OPT_USAGE_CHOICE, "pack-file", "packfile path" }, + + { 0 }, +}; + +static void print_help(void) +{ + cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts, 0); + printf("\n"); + + printf("Indexes a packfile and writes the index to disk.\n"); + printf("\n"); + + printf("Options:\n"); + + cli_opt_help_fprint(stdout, opts); +} + +int cmd_index_pack(int argc, char **argv) +{ + cli_opt invalid_opt; + git_indexer *idx = NULL; + git_indexer_options idx_opts = GIT_INDEXER_OPTIONS_INIT; + git_indexer_progress stats = {0}; + char buf[BUFFER_SIZE]; + ssize_t read_len; + int fd, ret; + + if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU)) + return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt); + + if (cli_opt__show_help) { + print_help(); + return 0; + } + + if (verbose) { + idx_opts.progress_cb = cli_progress_indexer; + idx_opts.progress_cb_payload = &progress; + } + + if (read_stdin) { + fd = fileno(stdin); + } else if ((fd = p_open(filename, O_RDONLY)) < 0) { + ret = cli_error_git(); + goto done; + } + +#ifdef GIT_EXPERIMENTAL_SHA256 + idx_opts.oid_type = GIT_OID_SHA1; + + ret = git_indexer_new(&idx, ".", &idx_opts); +#else + ret = git_indexer_new(&idx, ".", 0, NULL, &idx_opts); +#endif + + if (ret < 0) { + ret = cli_error_git(); + goto done; + } + + while ((read_len = p_read(fd, buf, sizeof(buf))) > 0) { + if (git_indexer_append(idx, buf, (size_t)read_len, &stats) < 0) { + ret = cli_error_git(); + goto done; + } + } + + if (git_indexer_commit(idx, &stats) < 0) { + ret = cli_error_git(); + goto done; + } + + cli_progress_finish(&progress); + +done: + if (!read_stdin && fd >= 0) + p_close(fd); + + cli_progress_dispose(&progress); + git_indexer_free(idx); + return ret; +} diff --git a/src/cli/cmd_init.c b/src/cli/cmd_init.c new file mode 100644 index 00000000000..40037d6d6b4 --- /dev/null +++ b/src/cli/cmd_init.c @@ -0,0 +1,100 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include +#include "common.h" +#include "cmd.h" +#include "error.h" +#include "sighandler.h" +#include "progress.h" + +#include "fs_path.h" +#include "futils.h" + +#define COMMAND_NAME "init" + +static char *branch, *git_dir, *template_dir, *path; +static int quiet, bare; + +static const cli_opt_spec opts[] = { + CLI_COMMON_OPT, + + { CLI_OPT_TYPE_SWITCH, "quiet", 'q', &quiet, 1, + CLI_OPT_USAGE_DEFAULT, NULL, "quiet mode; don't display informational messages" }, + { CLI_OPT_TYPE_SWITCH, "bare", 0, &bare, 1, + CLI_OPT_USAGE_DEFAULT, NULL, "don't create a working directory" }, + { CLI_OPT_TYPE_VALUE, "initial-branch", 'b', &branch, 0, + CLI_OPT_USAGE_DEFAULT, "name", "initial branch name" }, + { CLI_OPT_TYPE_VALUE, "separate-git-dir", 0, &git_dir, 0, + CLI_OPT_USAGE_DEFAULT, "git-dir", "path to separate git directory" }, + { CLI_OPT_TYPE_VALUE, "template", 0, &template_dir, 0, + CLI_OPT_USAGE_DEFAULT, "template-dir", "path to git directory templates" }, + { CLI_OPT_TYPE_LITERAL }, + { CLI_OPT_TYPE_ARG, "directory", 0, &path, 0, + CLI_OPT_USAGE_DEFAULT, "directory", "directory to create repository in" }, + { 0 } +}; + +static void print_help(void) +{ + cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts, 0); + printf("\n"); + + printf("Create a new git repository.\n"); + printf("\n"); + + printf("Options:\n"); + + cli_opt_help_fprint(stdout, opts); +} + +int cmd_init(int argc, char **argv) +{ + git_repository *repo = NULL; + git_repository_init_options init_opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; + cli_opt invalid_opt; + const char *repo_path; + int ret = 0; + + if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU)) + return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt); + + if (cli_opt__show_help) { + print_help(); + return 0; + } + + init_opts.flags |= GIT_REPOSITORY_INIT_MKPATH | + GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE; + + if (bare && git_dir) + return cli_error_usage("the '--bare' and '--separate-git-dir' options cannot be used together"); + + if (bare) + init_opts.flags |= GIT_REPOSITORY_INIT_BARE; + + init_opts.template_path = template_dir; + init_opts.initial_head = branch; + + if (git_dir) { + init_opts.workdir_path = path; + repo_path = git_dir; + } else { + repo_path = path; + } + + if (git_repository_init_ext(&repo, repo_path, &init_opts) < 0) { + ret = cli_error_git(); + } else if (!quiet) { + printf("Initialized empty Git repository in %s\n", + git_repository_path(repo)); + } + + git_repository_free(repo); + return ret; +} diff --git a/src/cli/cmd_version.c b/src/cli/cmd_version.c new file mode 100644 index 00000000000..9c84068b6fb --- /dev/null +++ b/src/cli/cmd_version.c @@ -0,0 +1,107 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include +#include "common.h" +#include "cmd.h" + +#define COMMAND_NAME "version" + +static int build_options; + +struct info_names { + int key; + const char *name; +}; + +static const struct info_names buildinfo_names[] = { + { GIT_BUILDINFO_CPU, "cpu" }, + { GIT_BUILDINFO_COMMIT, "built from commit" }, + { 0, NULL } +}; + +static const struct info_names feature_names[] = { + { GIT_FEATURE_SHA1, "sha1" }, + { GIT_FEATURE_SHA256, "sha256" }, + { GIT_FEATURE_THREADS, "threads" }, + { GIT_FEATURE_NSEC, "nsec" }, + { GIT_FEATURE_COMPRESSION, "compression" }, + { GIT_FEATURE_I18N, "i18n" }, + { GIT_FEATURE_REGEX, "regex" }, + { GIT_FEATURE_SSH, "ssh" }, + { GIT_FEATURE_HTTPS, "https" }, + { GIT_FEATURE_HTTP_PARSER, "http_parser" }, + { GIT_FEATURE_AUTH_NTLM, "auth_ntlm" }, + { GIT_FEATURE_AUTH_NEGOTIATE, "auth_negotiate" }, + { 0, NULL } +}; + +static const cli_opt_spec opts[] = { + CLI_COMMON_OPT, + + { CLI_OPT_TYPE_SWITCH, "build-options", 0, &build_options, 1, + CLI_OPT_USAGE_DEFAULT, NULL, "show compile-time options" }, + { 0 }, +}; + +static int print_help(void) +{ + cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts, 0); + printf("\n"); + + printf("Display version information for %s.\n", PROGRAM_NAME); + printf("\n"); + + printf("Options:\n"); + + cli_opt_help_fprint(stdout, opts); + + return 0; +} + +int cmd_version(int argc, char **argv) +{ + cli_opt invalid_opt; + const struct info_names *i; + const char *backend; + int supported_features; + + if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU)) + return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt); + + if (cli_opt__show_help) { + print_help(); + return 0; + } + + printf("%s version %s\n", PROGRAM_NAME, LIBGIT2_VERSION); + + if (build_options) { + supported_features = git_libgit2_features(); + + for (i = buildinfo_names; i->key; i++) { + const char *value = git_libgit2_buildinfo(i->key); + + if (value && *value) + printf("%s: %s\n", i->name, value); + } + + printf("sizeof-long: %" PRIuZ "\n", sizeof(long)); + printf("sizeof-size_t: %" PRIuZ "\n", sizeof(size_t)); + + for (i = feature_names; i->key; i++) { + if (!(supported_features & i->key)) + continue; + + backend = git_libgit2_feature_backend(i->key); + printf("backend-%s: %s\n", i->name, backend); + } + } + + return 0; +} diff --git a/src/cli/common.c b/src/cli/common.c new file mode 100644 index 00000000000..dbeefea48ed --- /dev/null +++ b/src/cli/common.c @@ -0,0 +1,168 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include + +#include "git2_util.h" +#include "vector.h" +#include "fs_path.h" + +#include "common.h" +#include "error.h" + +static int parse_option(cli_opt *opt, void *data) +{ + git_str kv = GIT_STR_INIT, env = GIT_STR_INIT; + git_vector *cmdline_config = data; + int error = 0; + + if (opt->spec && opt->spec->alias == 'c') { + if (git_str_puts(&kv, opt->value) < 0) { + error = cli_error_git(); + goto done; + } + } + + else if (opt->spec && !strcmp(opt->spec->name, "config-env")) { + char *val = strchr(opt->value, '='); + + if (val == NULL || *(val + 1) == '\0') { + error = cli_error("invalid config format: '%s'", opt->value); + goto done; + } + + if (git_str_put(&kv, opt->value, (val - opt->value)) < 0) { + error = cli_error_git(); + goto done; + } + + val++; + + if ((error = git__getenv(&env, val)) == GIT_ENOTFOUND) { + error = cli_error("missing environment variable '%s' for configuration '%s'", val, kv.ptr); + goto done; + } else if (error) { + error = cli_error_git(); + goto done; + } + + if (git_str_putc(&kv, '=') < 0 || + git_str_puts(&kv, env.ptr) < 0) { + error = cli_error_git(); + goto done; + } + } + + if (kv.size > 0 && + git_vector_insert(cmdline_config, git_str_detach(&kv)) < 0) + error = cli_error_git(); + +done: + git_str_dispose(&env); + git_str_dispose(&kv); + return error; +} + +static int parse_common_options( + git_repository *repo, + cli_repository_open_options *opts) +{ + cli_opt_spec common_opts[] = { + { CLI_COMMON_OPT_CONFIG }, + { CLI_COMMON_OPT_CONFIG_ENV }, + { 0 } + }; + git_config_backend_memory_options config_opts = + GIT_CONFIG_BACKEND_MEMORY_OPTIONS_INIT; + git_vector cmdline = GIT_VECTOR_INIT; + git_config *config = NULL; + git_config_backend *backend = NULL; + int error = 0; + + config_opts.backend_type = "command line"; + + if ((error = cli_opt_foreach(common_opts, opts->args, + opts->args_len, CLI_OPT_PARSE_GNU, parse_option, + &cmdline)) < 0) + goto done; + + if (git_vector_length(&cmdline) == 0) + goto done; + + if (git_repository_config(&config, repo) < 0 || + git_config_backend_from_values(&backend, + (const char **)cmdline.contents, cmdline.length, + &config_opts) < 0 || + git_config_add_backend(config, backend, GIT_CONFIG_LEVEL_APP, + repo, 0) < 0) + error = cli_error_git(); + +done: + if (error && backend) + backend->free(backend); + git_config_free(config); + git_vector_dispose_deep(&cmdline); + return error; +} + +int cli_repository_open( + git_repository **out, + cli_repository_open_options *opts) +{ + git_repository *repo; + + if (git_repository_open_ext(&repo, ".", GIT_REPOSITORY_OPEN_FROM_ENV, NULL) < 0) + return -1; + + if (opts && parse_common_options(repo, opts) < 0) + return -1; + + *out = repo; + return 0; +} + +/* + * This resolves paths - not _pathspecs_ like git - it accepts an absolute + * path (to a path within the repository working directory) or a path + * relative to the current directory. + */ +int cli_resolve_path(git_str *out, git_repository *repo, const char *given_path) +{ + git_str path = GIT_STR_INIT; + int error = 0; + + if (!git_fs_path_is_absolute(given_path)) { + char cwd[GIT_PATH_MAX]; + + if (p_getcwd(cwd, GIT_PATH_MAX) < 0) + error = cli_error_os(); + else if (git_str_puts(&path, cwd) < 0 || + git_fs_path_apply_relative(&path, given_path) < 0) + error = cli_error_git(); + + if (error) + goto done; + } else if (git_str_puts(&path, given_path) < 0) { + error = cli_error_git(); + goto done; + } + + error = git_fs_path_make_relative(&path, git_repository_workdir(repo)); + + if (error == GIT_ENOTFOUND) + error = cli_error("path '%s' is not inside the git repository '%s'", + given_path, git_repository_workdir(repo)); + else if (error < 0) + error = cli_error_git(); + else + git_str_swap(out, &path); + +done: + git_str_dispose(&path); + return error; +} diff --git a/src/cli/common.h b/src/cli/common.h new file mode 100644 index 00000000000..330f776c91e --- /dev/null +++ b/src/cli/common.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef CLI_common_h__ +#define CLI_common_h__ + +#define PROGRAM_NAME "git2" + +#include "git2_util.h" + +#include "error.h" +#include "opt.h" +#include "opt_usage.h" + +/* + * Common command arguments. + */ + +extern int cli_opt__show_help; +extern int cli_opt__use_pager; + +#define CLI_COMMON_OPT_HELP \ + CLI_OPT_TYPE_SWITCH, "help", 0, &cli_opt__show_help, 1, \ + CLI_OPT_USAGE_HIDDEN | CLI_OPT_USAGE_STOP_PARSING, \ + NULL, "display help information" +#define CLI_COMMON_OPT_CONFIG \ + CLI_OPT_TYPE_VALUE, NULL, 'c', NULL, 0, \ + CLI_OPT_USAGE_HIDDEN, \ + "key=value", "add configuration value" +#define CLI_COMMON_OPT_CONFIG_ENV \ + CLI_OPT_TYPE_VALUE, "config-env", 0, NULL, 0, \ + CLI_OPT_USAGE_HIDDEN, \ + "key=value", "set configuration value to environment variable" +#define CLI_COMMON_OPT_NO_PAGER \ + CLI_OPT_TYPE_SWITCH, "no-pager", 0, &cli_opt__use_pager, 0, \ + CLI_OPT_USAGE_HIDDEN, \ + NULL, "don't paginate multi-page output" + +#define CLI_COMMON_OPT \ + { CLI_COMMON_OPT_HELP }, \ + { CLI_COMMON_OPT_CONFIG }, \ + { CLI_COMMON_OPT_CONFIG_ENV }, \ + { CLI_COMMON_OPT_NO_PAGER } + +typedef struct { + char **args; + int args_len; +} cli_repository_open_options; + +extern int cli_repository_open( + git_repository **out, + cli_repository_open_options *opts); + +extern int cli_resolve_path( + git_str *out, + git_repository *repo, + const char *given_path); + +#endif /* CLI_common_h__ */ diff --git a/src/cli/error.h b/src/cli/error.h new file mode 100644 index 00000000000..abf8a5160d1 --- /dev/null +++ b/src/cli/error.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef CLI_error_h__ +#define CLI_error_h__ + +#include "common.h" +#include + +#define CLI_EXIT_OK 0 +#define CLI_EXIT_ERROR 1 +#define CLI_EXIT_OS 128 +#define CLI_EXIT_GIT 128 +#define CLI_EXIT_USAGE 129 + +#define cli_error__print(fmt) do { \ + va_list ap; \ + va_start(ap, fmt); \ + fprintf(stderr, "%s: ", PROGRAM_NAME); \ + vfprintf(stderr, fmt, ap); \ + fprintf(stderr, "\n"); \ + va_end(ap); \ + } while(0) + +GIT_INLINE(int) cli_error(const char *fmt, ...) +{ + cli_error__print(fmt); + return CLI_EXIT_ERROR; +} + +GIT_INLINE(int) cli_error_usage(const char *fmt, ...) +{ + cli_error__print(fmt); + return CLI_EXIT_USAGE; +} + +GIT_INLINE(int) cli_error_git(void) +{ + const git_error *err = git_error_last(); + fprintf(stderr, "%s: %s\n", PROGRAM_NAME, + err ? err->message : "unknown error"); + return CLI_EXIT_GIT; +} + +#define cli_error_os() (perror(PROGRAM_NAME), CLI_EXIT_OS) + +#endif /* CLI_error_h__ */ diff --git a/src/cli/main.c b/src/cli/main.c new file mode 100644 index 00000000000..4716d6ddee9 --- /dev/null +++ b/src/cli/main.c @@ -0,0 +1,142 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include +#include "common.h" +#include "cmd.h" + +int cli_opt__show_help = 0; +int cli_opt__use_pager = 1; + +static int show_version = 0; +static char *command = NULL; +static char **args = NULL; + +const cli_opt_spec cli_common_opts[] = { + CLI_COMMON_OPT, + + { CLI_OPT_TYPE_SWITCH, "version", 0, &show_version, 1, + CLI_OPT_USAGE_DEFAULT, NULL, "display the version" }, + { CLI_OPT_TYPE_ARG, "command", 0, &command, 0, + CLI_OPT_USAGE_REQUIRED, "command", "the command to run" }, + { CLI_OPT_TYPE_ARGS, "args", 0, &args, 0, + CLI_OPT_USAGE_DEFAULT, "args", "arguments for the command" }, + { 0 } +}; + +const cli_cmd_spec cli_cmds[] = { + { "blame", cmd_blame, "Show the origin of each line of a file" }, + { "cat-file", cmd_cat_file, "Display an object in the repository" }, + { "clone", cmd_clone, "Clone a repository into a new directory" }, + { "config", cmd_config, "View or set configuration values " }, + { "hash-object", cmd_hash_object, "Hash a raw object and product its object ID" }, + { "help", cmd_help, "Display help information" }, + { "index-pack", cmd_index_pack, "Create an index for a packfile" }, + { "init", cmd_init, "Create a new git repository" }, + { "version", cmd_version, "Show application version information" }, + { NULL } +}; + +/* + * Reorder the argv as it was given, since git has the notion of global + * options (like `--help` or `-c key=val`) that we want to pass to the + * subcommand, and that can appear early in the arguments, before the + * command name. Put the command-name in argv[1] to allow easier parsing. + */ +static void reorder_args(char **argv, size_t first) +{ + char *tmp; + size_t i; + + if (first == 1) + return; + + tmp = argv[first]; + + for (i = first; i > 1; i--) + argv[i] = argv[i - 1]; + + argv[1] = tmp; +} + +/* + * When invoked without a command, or just with `--help`, we invoke + * the help command; but we want to preserve only arguments that would + * be useful for that. + */ +static void help_args(int *argc, char **argv) +{ + cli_opt__show_help = 0; + + argv[0] = "help"; + *argc = 1; +} + +static void version_args(int *argc, char **argv) +{ + argv[0] = "version"; + *argc = 1; +} + +int main(int argc, char **argv) +{ + const cli_cmd_spec *cmd; + cli_opt_parser optparser; + cli_opt opt; + int ret = 0; + + if (git_libgit2_init() < 0) { + cli_error("failed to initialize libgit2"); + exit(CLI_EXIT_GIT); + } + + cli_opt_parser_init(&optparser, cli_common_opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU); + + /* Parse the top-level (common) options and command information */ + while (cli_opt_parser_next(&opt, &optparser)) { + if (!opt.spec) { + cli_opt_status_fprint(stderr, PROGRAM_NAME, &opt); + cli_opt_usage_fprint(stderr, PROGRAM_NAME, NULL, cli_common_opts, CLI_OPT_USAGE_SHOW_HIDDEN); + ret = CLI_EXIT_USAGE; + goto done; + } + + /* + * When we see a command, stop parsing and capture the + * remaining arguments as args for the command itself. + */ + if (command) { + reorder_args(argv, optparser.idx); + break; + } + } + + if (show_version) { + version_args(&argc, argv); + ret = cmd_version(argc, argv); + goto done; + } + + if (!command) { + help_args(&argc, argv); + ret = cmd_help(argc, argv); + goto done; + } + + if ((cmd = cli_cmd_spec_byname(command)) == NULL) { + ret = cli_error("'%s' is not a %s command. See '%s help'.", + command, PROGRAM_NAME, PROGRAM_NAME); + goto done; + } + + ret = cmd->fn(argc - 1, &argv[1]); + +done: + git_libgit2_shutdown(); + return ret; +} diff --git a/src/cli/opt.c b/src/cli/opt.c new file mode 100644 index 00000000000..c0880248e97 --- /dev/null +++ b/src/cli/opt.c @@ -0,0 +1,695 @@ +/* + * Copyright (c), Edward Thomson + * All rights reserved. + * + * This file is part of adopt, distributed under the MIT license. + * For full terms and conditions, see the included LICENSE file. + * + * THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT. + * + * This file was produced by using the `rename.pl` script included with + * adopt. The command-line specified was: + * + * ./rename.pl cli_opt --filename=opt --include=common.h --inline=GIT_INLINE --header-guard=CLI_opt_h__ --lowercase-status --without-usage + */ + +#include +#include +#include +#include +#include + +#if defined(__sun) || defined(__illumos__) || defined(__CYGWIN__) +# include +#endif + +#include "common.h" +#include "opt.h" + +#ifdef _WIN32 +# include +#else +# include +# include +#endif + +#ifdef _MSC_VER +# define alloca _alloca +#endif + +#define spec_is_option_type(x) \ + ((x)->type == CLI_OPT_TYPE_BOOL || \ + (x)->type == CLI_OPT_TYPE_SWITCH || \ + (x)->type == CLI_OPT_TYPE_VALUE) + +GIT_INLINE(const cli_opt_spec *) spec_for_long( + int *is_negated, + int *has_value, + const char **value, + const cli_opt_parser *parser, + const char *arg) +{ + const cli_opt_spec *spec; + const char *eql; + size_t eql_pos; + + eql = strchr(arg, '='); + eql_pos = (eql = strchr(arg, '=')) ? (size_t)(eql - arg) : strlen(arg); + + for (spec = parser->specs; spec->type; ++spec) { + /* Handle -- (everything after this is literal) */ + if (spec->type == CLI_OPT_TYPE_LITERAL && arg[0] == '\0') + return spec; + + /* Handle --no-option arguments for bool types */ + if (spec->type == CLI_OPT_TYPE_BOOL && + strncmp(arg, "no-", 3) == 0 && + strcmp(arg + 3, spec->name) == 0) { + *is_negated = 1; + return spec; + } + + /* Handle the typical --option arguments */ + if (spec_is_option_type(spec) && + spec->name && + strcmp(arg, spec->name) == 0) + return spec; + + /* Handle --option=value arguments */ + if (spec->type == CLI_OPT_TYPE_VALUE && + spec->name && eql && + strncmp(arg, spec->name, eql_pos) == 0 && + spec->name[eql_pos] == '\0') { + *has_value = 1; + *value = arg[eql_pos + 1] ? &arg[eql_pos + 1] : NULL; + return spec; + } + } + + return NULL; +} + +GIT_INLINE(const cli_opt_spec *) spec_for_short( + const char **value, + const cli_opt_parser *parser, + const char *arg) +{ + const cli_opt_spec *spec; + + for (spec = parser->specs; spec->type; ++spec) { + /* Handle -svalue short options with a value */ + if (spec->type == CLI_OPT_TYPE_VALUE && + arg[0] == spec->alias && + arg[1] != '\0') { + *value = &arg[1]; + return spec; + } + + /* Handle typical -s short options */ + if (arg[0] == spec->alias) { + *value = NULL; + return spec; + } + } + + return NULL; +} + +GIT_INLINE(const cli_opt_spec *) spec_for_arg(cli_opt_parser *parser) +{ + const cli_opt_spec *spec; + size_t args = 0; + + for (spec = parser->specs; spec->type; ++spec) { + if (spec->type == CLI_OPT_TYPE_ARG) { + if (args == parser->arg_idx) { + parser->arg_idx++; + return spec; + } + + args++; + } + + if (spec->type == CLI_OPT_TYPE_ARGS && args == parser->arg_idx) + return spec; + } + + return NULL; +} + +GIT_INLINE(int) spec_is_choice(const cli_opt_spec *spec) +{ + return ((spec + 1)->type && + ((spec + 1)->usage & CLI_OPT_USAGE_CHOICE)); +} + +/* + * If we have a choice with switches and bare arguments, and we see + * the switch, then we no longer expect the bare argument. + */ +GIT_INLINE(void) consume_choices(const cli_opt_spec *spec, cli_opt_parser *parser) +{ + /* back up to the beginning of the choices */ + while (spec->type && (spec->usage & CLI_OPT_USAGE_CHOICE)) + --spec; + + if (!spec_is_choice(spec)) + return; + + do { + if (spec->type == CLI_OPT_TYPE_ARG) + parser->arg_idx++; + ++spec; + } while(spec->type && (spec->usage & CLI_OPT_USAGE_CHOICE)); +} + +static cli_opt_status_t parse_long(cli_opt *opt, cli_opt_parser *parser) +{ + const cli_opt_spec *spec; + char *arg = parser->args[parser->idx++]; + const char *value = NULL; + int is_negated = 0, has_value = 0; + + opt->arg = arg; + + if ((spec = spec_for_long(&is_negated, &has_value, &value, parser, &arg[2])) == NULL) { + opt->spec = NULL; + opt->status = CLI_OPT_STATUS_UNKNOWN_OPTION; + goto done; + } + + opt->spec = spec; + + /* Future options parsed as literal */ + if (spec->type == CLI_OPT_TYPE_LITERAL) + parser->in_literal = 1; + + /* --bool or --no-bool */ + else if (spec->type == CLI_OPT_TYPE_BOOL && spec->value) + *((int *)spec->value) = !is_negated; + + /* --accumulate */ + else if (spec->type == CLI_OPT_TYPE_ACCUMULATOR && spec->value) + *((int *)spec->value) += spec->switch_value ? spec->switch_value : 1; + + /* --switch */ + else if (spec->type == CLI_OPT_TYPE_SWITCH && spec->value) + *((int *)spec->value) = spec->switch_value; + + /* Parse values as "--foo=bar" or "--foo bar" */ + else if (spec->type == CLI_OPT_TYPE_VALUE) { + if (has_value) + opt->value = (char *)value; + else if ((parser->idx + 1) <= parser->args_len) + opt->value = parser->args[parser->idx++]; + + if (spec->value) + *((char **)spec->value) = opt->value; + } + + /* Required argument was not provided */ + if (spec->type == CLI_OPT_TYPE_VALUE && + !opt->value && + !(spec->usage & CLI_OPT_USAGE_VALUE_OPTIONAL)) + opt->status = CLI_OPT_STATUS_MISSING_VALUE; + else + opt->status = CLI_OPT_STATUS_OK; + + consume_choices(opt->spec, parser); + +done: + return opt->status; +} + +static cli_opt_status_t parse_short(cli_opt *opt, cli_opt_parser *parser) +{ + const cli_opt_spec *spec; + char *arg = parser->args[parser->idx++]; + const char *value; + + opt->arg = arg; + + if ((spec = spec_for_short(&value, parser, &arg[1 + parser->in_short])) == NULL) { + opt->spec = NULL; + opt->status = CLI_OPT_STATUS_UNKNOWN_OPTION; + goto done; + } + + opt->spec = spec; + + if (spec->type == CLI_OPT_TYPE_BOOL && spec->value) + *((int *)spec->value) = 1; + + else if (spec->type == CLI_OPT_TYPE_ACCUMULATOR && spec->value) + *((int *)spec->value) += spec->switch_value ? spec->switch_value : 1; + + else if (spec->type == CLI_OPT_TYPE_SWITCH && spec->value) + *((int *)spec->value) = spec->switch_value; + + /* Parse values as "-ifoo" or "-i foo" */ + else if (spec->type == CLI_OPT_TYPE_VALUE) { + if (value) + opt->value = (char *)value; + else if ((parser->idx + 1) <= parser->args_len) + opt->value = parser->args[parser->idx++]; + + if (spec->value) + *((char **)spec->value) = opt->value; + } + + /* + * Handle compressed short arguments, like "-fbcd"; see if there's + * another character after the one we processed. If not, advance + * the parser index. + */ + if (spec->type != CLI_OPT_TYPE_VALUE && arg[2 + parser->in_short] != '\0') { + parser->in_short++; + parser->idx--; + } else { + parser->in_short = 0; + } + + /* Required argument was not provided */ + if (spec->type == CLI_OPT_TYPE_VALUE && !opt->value) + opt->status = CLI_OPT_STATUS_MISSING_VALUE; + else + opt->status = CLI_OPT_STATUS_OK; + + consume_choices(opt->spec, parser); + +done: + return opt->status; +} + +static cli_opt_status_t parse_arg(cli_opt *opt, cli_opt_parser *parser) +{ + const cli_opt_spec *spec = spec_for_arg(parser); + + opt->spec = spec; + opt->arg = parser->args[parser->idx]; + + if (!spec) { + parser->idx++; + opt->status = CLI_OPT_STATUS_UNKNOWN_OPTION; + } else if (spec->type == CLI_OPT_TYPE_ARGS) { + if (spec->value) + *((char ***)spec->value) = &parser->args[parser->idx]; + + /* + * We have started a list of arguments; the remainder of + * given arguments need not be examined. + */ + parser->in_args = (parser->args_len - parser->idx); + parser->idx = parser->args_len; + opt->args_len = parser->in_args; + opt->status = CLI_OPT_STATUS_OK; + } else { + if (spec->value) + *((char **)spec->value) = parser->args[parser->idx]; + + parser->idx++; + opt->status = CLI_OPT_STATUS_OK; + } + + return opt->status; +} + +static int support_gnu_style(unsigned int flags) +{ + if ((flags & CLI_OPT_PARSE_FORCE_GNU) != 0) + return 1; + + if ((flags & CLI_OPT_PARSE_GNU) == 0) + return 0; + + /* TODO: Windows */ +#if defined(_WIN32) && defined(UNICODE) + if (_wgetenv(L"POSIXLY_CORRECT") != NULL) + return 0; +#else + if (getenv("POSIXLY_CORRECT") != NULL) + return 0; +#endif + + return 1; +} + +void cli_opt_parser_init( + cli_opt_parser *parser, + const cli_opt_spec specs[], + char **args, + size_t args_len, + unsigned int flags) +{ + assert(parser); + + memset(parser, 0x0, sizeof(cli_opt_parser)); + + parser->specs = specs; + parser->args = args; + parser->args_len = args_len; + parser->flags = flags; + + parser->needs_sort = support_gnu_style(flags); +} + +GIT_INLINE(const cli_opt_spec *) spec_for_sort( + int *needs_value, + const cli_opt_parser *parser, + const char *arg) +{ + int is_negated, has_value = 0; + const char *value; + const cli_opt_spec *spec = NULL; + size_t idx = 0; + + *needs_value = 0; + + if (strncmp(arg, "--", 2) == 0) { + spec = spec_for_long(&is_negated, &has_value, &value, parser, &arg[2]); + *needs_value = !has_value; + } + + else if (strncmp(arg, "-", 1) == 0) { + spec = spec_for_short(&value, parser, &arg[1]); + + /* + * Advance through compressed short arguments to see if + * the last one has a value, eg "-xvffilename". + */ + while (spec && !value && arg[1 + ++idx] != '\0') + spec = spec_for_short(&value, parser, &arg[1 + idx]); + + *needs_value = (value == NULL); + } + + return spec; +} + +/* + * Some parsers allow for handling arguments like "file1 --help file2"; + * this is done by re-sorting the arguments in-place; emulate that. + */ +static int sort_gnu_style(cli_opt_parser *parser) +{ + size_t i, j, insert_idx = parser->idx, offset; + const cli_opt_spec *spec; + char *option, *value; + int needs_value, changed = 0; + + parser->needs_sort = 0; + + for (i = parser->idx; i < parser->args_len; i++) { + spec = spec_for_sort(&needs_value, parser, parser->args[i]); + + /* Not a "-" or "--" prefixed option. No change. */ + if (!spec) + continue; + + /* A "--" alone means remaining args are literal. */ + if (spec->type == CLI_OPT_TYPE_LITERAL) + break; + + option = parser->args[i]; + + /* + * If the argument is a value type and doesn't already + * have a value (eg "--foo=bar" or "-fbar") then we need + * to copy the next argument as its value. + */ + if (spec->type == CLI_OPT_TYPE_VALUE && needs_value) { + /* + * A required value is not provided; set parser + * index to this value so that we fail on it. + */ + if (i + 1 >= parser->args_len) { + parser->idx = i; + return 1; + } + + value = parser->args[i + 1]; + offset = 1; + } else { + value = NULL; + offset = 0; + } + + /* Caller error if args[0] is an option. */ + if (i == 0) + return 0; + + /* Shift args up one (or two) and insert the option */ + for (j = i; j > insert_idx; j--) + parser->args[j + offset] = parser->args[j - 1]; + + parser->args[insert_idx] = option; + + if (value) + parser->args[insert_idx + 1] = value; + + insert_idx += (1 + offset); + i += offset; + + changed = 1; + } + + return changed; +} + +cli_opt_status_t cli_opt_parser_next(cli_opt *opt, cli_opt_parser *parser) +{ + assert(opt && parser); + + memset(opt, 0x0, sizeof(cli_opt)); + + if (parser->idx >= parser->args_len) { + opt->args_len = parser->in_args; + return CLI_OPT_STATUS_DONE; + } + + /* Handle options in long form, those beginning with "--" */ + if (strncmp(parser->args[parser->idx], "--", 2) == 0 && + !parser->in_short && + !parser->in_literal) + return parse_long(opt, parser); + + /* Handle options in short form, those beginning with "-" */ + else if (parser->in_short || + (strncmp(parser->args[parser->idx], "-", 1) == 0 && + !parser->in_literal)) + return parse_short(opt, parser); + + /* + * We've reached the first "bare" argument. In POSIX mode, all + * remaining items on the command line are arguments. In GNU + * mode, there may be long or short options after this. Sort any + * options up to this position then re-parse the current position. + */ + if (parser->needs_sort && sort_gnu_style(parser)) + return cli_opt_parser_next(opt, parser); + + return parse_arg(opt, parser); +} + +GIT_INLINE(int) spec_included(const cli_opt_spec **specs, const cli_opt_spec *spec) +{ + const cli_opt_spec **i; + + for (i = specs; *i; ++i) { + if (spec == *i) + return 1; + } + + return 0; +} + +static cli_opt_status_t validate_required( + cli_opt *opt, + const cli_opt_spec specs[], + const cli_opt_spec **given_specs) +{ + const cli_opt_spec *spec, *required; + int given; + + /* + * Iterate over the possible specs to identify requirements and + * ensure that those have been given on the command-line. + * Note that we can have required *choices*, where one in a + * list of choices must be specified. + */ + for (spec = specs, required = NULL, given = 0; spec->type; ++spec) { + if (!required && (spec->usage & CLI_OPT_USAGE_REQUIRED)) { + required = spec; + given = 0; + } else if (!required) { + continue; + } + + if (!given) + given = spec_included(given_specs, spec); + + /* + * Validate the requirement unless we're in a required + * choice. In that case, keep the required state and + * validate at the end of the choice list. + */ + if (!spec_is_choice(spec)) { + if (!given) { + opt->spec = required; + opt->status = CLI_OPT_STATUS_MISSING_ARGUMENT; + break; + } + + required = NULL; + given = 0; + } + } + + return opt->status; +} + +cli_opt_status_t cli_opt_parse( + cli_opt *opt, + const cli_opt_spec specs[], + char **args, + size_t args_len, + unsigned int flags) +{ + cli_opt_parser parser; + const cli_opt_spec **given_specs; + size_t given_idx = 0; + + cli_opt_parser_init(&parser, specs, args, args_len, flags); + + given_specs = alloca(sizeof(const cli_opt_spec *) * (args_len + 1)); + + while (cli_opt_parser_next(opt, &parser)) { + if (opt->status != CLI_OPT_STATUS_OK && + opt->status != CLI_OPT_STATUS_DONE) + return opt->status; + + if ((opt->spec->usage & CLI_OPT_USAGE_STOP_PARSING)) + return (opt->status = CLI_OPT_STATUS_DONE); + + given_specs[given_idx++] = opt->spec; + } + + given_specs[given_idx] = NULL; + + return validate_required(opt, specs, given_specs); +} + +int cli_opt_foreach( + const cli_opt_spec specs[], + char **args, + size_t args_len, + unsigned int flags, + int (*callback)(cli_opt *, void *), + void *callback_data) +{ + cli_opt_parser parser; + cli_opt opt; + int ret; + + cli_opt_parser_init(&parser, specs, args, args_len, flags); + + while (cli_opt_parser_next(&opt, &parser)) { + if ((ret = callback(&opt, callback_data)) != 0) + return ret; + } + + return 0; +} + +static int spec_name_fprint(FILE *file, const cli_opt_spec *spec) +{ + int error; + + if (spec->type == CLI_OPT_TYPE_ARG) + error = fprintf(file, "%s", spec->value_name); + else if (spec->type == CLI_OPT_TYPE_ARGS) + error = fprintf(file, "%s", spec->value_name); + else if (spec->alias && !(spec->usage & CLI_OPT_USAGE_SHOW_LONG)) + error = fprintf(file, "-%c", spec->alias); + else + error = fprintf(file, "--%s", spec->name); + + return error; +} + +int cli_opt_status_fprint( + FILE *file, + const char *command, + const cli_opt *opt) +{ + const cli_opt_spec *choice; + int error; + + if (command && (error = fprintf(file, "%s: ", command)) < 0) + return error; + + switch (opt->status) { + case CLI_OPT_STATUS_DONE: + error = fprintf(file, "finished processing arguments (no error)\n"); + break; + case CLI_OPT_STATUS_OK: + error = fprintf(file, "no error\n"); + break; + case CLI_OPT_STATUS_UNKNOWN_OPTION: + error = fprintf(file, "unknown option: %s\n", opt->arg); + break; + case CLI_OPT_STATUS_MISSING_VALUE: + if ((error = fprintf(file, "argument '")) < 0 || + (error = spec_name_fprint(file, opt->spec)) < 0 || + (error = fprintf(file, "' requires a value.\n")) < 0) + break; + break; + case CLI_OPT_STATUS_MISSING_ARGUMENT: + if (spec_is_choice(opt->spec)) { + int is_choice = 1; + + if (spec_is_choice((opt->spec)+1)) + error = fprintf(file, "one of"); + else + error = fprintf(file, "either"); + + if (error < 0) + break; + + for (choice = opt->spec; is_choice; ++choice) { + is_choice = spec_is_choice(choice); + + if (!is_choice) + error = fprintf(file, " or"); + else if (choice != opt->spec) + error = fprintf(file, ","); + + if ((error < 0) || + (error = fprintf(file, " '")) < 0 || + (error = spec_name_fprint(file, choice)) < 0 || + (error = fprintf(file, "'")) < 0) + break; + + if (!spec_is_choice(choice)) + break; + } + + if ((error < 0) || + (error = fprintf(file, " is required.\n")) < 0) + break; + } else { + if ((error = fprintf(file, "argument '")) < 0 || + (error = spec_name_fprint(file, opt->spec)) < 0 || + (error = fprintf(file, "' is required.\n")) < 0) + break; + } + + break; + default: + error = fprintf(file, "unknown status: %d\n", opt->status); + break; + } + + return error; +} + diff --git a/src/cli/opt.h b/src/cli/opt.h new file mode 100644 index 00000000000..226f74db8bc --- /dev/null +++ b/src/cli/opt.h @@ -0,0 +1,367 @@ +/* + * Copyright (c), Edward Thomson + * All rights reserved. + * + * This file is part of adopt, distributed under the MIT license. + * For full terms and conditions, see the included LICENSE file. + * + * THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT. + * + * This file was produced by using the `rename.pl` script included with + * adopt. The command-line specified was: + * + * ./rename.pl cli_opt --filename=opt --include=common.h --inline=GIT_INLINE --header-guard=CLI_opt_h__ --lowercase-status --without-usage + */ + +#ifndef CLI_opt_h__ +#define CLI_opt_h__ + +#include +#include + +/** + * The type of argument to be parsed. + */ +typedef enum { + CLI_OPT_TYPE_NONE = 0, + + /** + * An option that, when specified, sets a given value to true. + * This is useful for options like "--debug". A negation + * option (beginning with "no-") is implicitly specified; for + * example "--no-debug". The `value` pointer in the returned + * option will be set to `1` when this is specified, and set to + * `0` when the negation "no-" option is specified. + */ + CLI_OPT_TYPE_BOOL, + + /** + * An option that, when specified, sets the given `value` pointer + * to the specified `switch_value`. This is useful for booleans + * where you do not want the implicit negation that comes with an + * `CLI_OPT_TYPE_BOOL`, or for switches that multiplex a value, like + * setting a mode. For example, `--read` may set the `value` to + * `MODE_READ` and `--write` may set the `value` to `MODE_WRITE`. + */ + CLI_OPT_TYPE_SWITCH, + + /** + * An option that, when specified, increments the given + * `value` by the given `switch_value`. This can be specified + * multiple times to continue to increment the `value`. + * (For example, "-vvv" to set verbosity to 3.) + */ + CLI_OPT_TYPE_ACCUMULATOR, + + /** + * An option that takes a value, for example `-n value`, + * `-nvalue`, `--name value` or `--name=value`. + */ + CLI_OPT_TYPE_VALUE, + + /** + * A bare "--" that indicates that arguments following this are + * literal. This allows callers to specify things that might + * otherwise look like options, for example to operate on a file + * named "-rf" then you can invoke "program -- -rf" to treat + * "-rf" as an argument not an option. + */ + CLI_OPT_TYPE_LITERAL, + + /** + * A single argument, not an option. When options are exhausted, + * arguments will be matches in the order that they're specified + * in the spec list. For example, if two `CLI_OPT_TYPE_ARGS` are + * specified, `input_file` and `output_file`, then the first bare + * argument on the command line will be `input_file` and the + * second will be `output_file`. + */ + CLI_OPT_TYPE_ARG, + + /** + * A collection of arguments. This is useful when you want to take + * a list of arguments, for example, multiple paths. When specified, + * the value will be set to the first argument in the list. + */ + CLI_OPT_TYPE_ARGS, +} cli_opt_type_t; + +/** + * Additional information about an option, including parsing + * restrictions and usage information to be displayed to the end-user. + */ +typedef enum { + /** Defaults for the argument. */ + CLI_OPT_USAGE_DEFAULT = 0, + + /** This argument is required. */ + CLI_OPT_USAGE_REQUIRED = (1u << 0), + + /** + * This is a multiple choice argument, combined with the previous + * argument. For example, when the previous argument is `-f` and + * this optional is applied to an argument of type `-b` then one + * of `-f` or `-b` may be specified. + */ + CLI_OPT_USAGE_CHOICE = (1u << 1), + + /** + * This argument short-circuits the remainder of parsing. + * Useful for arguments like `--help`. + */ + CLI_OPT_USAGE_STOP_PARSING = (1u << 2), + + /** The argument's value is optional ("-n" or "-n foo") */ + CLI_OPT_USAGE_VALUE_OPTIONAL = (1u << 3), + + /** This argument should not be displayed in usage. */ + CLI_OPT_USAGE_HIDDEN = (1u << 4), + + /** In usage, show the long format instead of the abbreviated format. */ + CLI_OPT_USAGE_SHOW_LONG = (1u << 5), +} cli_opt_usage_t; + +typedef enum { + /** Default parsing behavior. */ + CLI_OPT_PARSE_DEFAULT = 0, + + /** + * Parse with GNU `getopt_long` style behavior, where options can + * be intermixed with arguments at any position (for example, + * "file1 --help file2".) Like `getopt_long`, this can mutate the + * arguments given. + */ + CLI_OPT_PARSE_GNU = (1u << 0), + + /** + * Force GNU `getopt_long` style behavior; the `POSIXLY_CORRECT` + * environment variable is ignored. + */ + CLI_OPT_PARSE_FORCE_GNU = (1u << 1), +} cli_opt_flag_t; + +/** Specification for an available option. */ +typedef struct cli_opt_spec { + /** Type of option expected. */ + cli_opt_type_t type; + + /** Name of the long option. */ + const char *name; + + /** The alias is the short (one-character) option alias. */ + const char alias; + + /** + * If this spec is of type `CLI_OPT_TYPE_BOOL`, this is a pointer + * to an `int` that will be set to `1` if the option is specified. + * + * If this spec is of type `CLI_OPT_TYPE_SWITCH`, this is a pointer + * to an `int` that will be set to the opt's `switch_value` (below) + * when this option is specified. + * + * If this spec is of type `CLI_OPT_TYPE_ACCUMULATOR`, this is a + * pointer to an `int` that will be incremented by the opt's + * `switch_value` (below). If no `switch_value` is provided then + * the value will be incremented by 1. + * + * If this spec is of type `CLI_OPT_TYPE_VALUE`, + * `CLI_OPT_TYPE_VALUE_OPTIONAL`, or `CLI_OPT_TYPE_ARG`, this is + * a pointer to a `char *` that will be set to the value + * specified on the command line. + * + * If this spec is of type `CLI_OPT_TYPE_ARGS`, this is a pointer + * to a `char **` that will be set to the remaining values + * specified on the command line. + */ + void *value; + + /** + * If this spec is of type `CLI_OPT_TYPE_SWITCH`, this is the value + * to set in the option's `value` pointer when it is specified. If + * this spec is of type `CLI_OPT_TYPE_ACCUMULATOR`, this is the value + * to increment in the option's `value` pointer when it is + * specified. This is ignored for other opt types. + */ + int switch_value; + + /** + * Optional usage flags that change parsing behavior and how + * usage information is shown to the end-user. + */ + uint32_t usage; + + /** + * The name of the value, provided when creating usage information. + * This is required only for the functions that display usage + * information and only when a spec is of type `CLI_OPT_TYPE_VALUE, + * `CLI_OPT_TYPE_ARG` or `CLI_OPT_TYPE_ARGS``. + */ + const char *value_name; + + /** + * Optional short description of the option to display to the + * end-user. This is only used when creating usage information. + */ + const char *help; +} cli_opt_spec; + +/** Return value for `cli_opt_parser_next`. */ +typedef enum { + /** Parsing is complete; there are no more arguments. */ + CLI_OPT_STATUS_DONE = 0, + + /** + * This argument was parsed correctly; the `opt` structure is + * populated and the value pointer has been set. + */ + CLI_OPT_STATUS_OK = 1, + + /** + * The argument could not be parsed correctly, it does not match + * any of the specifications provided. + */ + CLI_OPT_STATUS_UNKNOWN_OPTION = 2, + + /** + * The argument matched a spec of type `CLI_OPT_VALUE`, but no value + * was provided. + */ + CLI_OPT_STATUS_MISSING_VALUE = 3, + + /** A required argument was not provided. */ + CLI_OPT_STATUS_MISSING_ARGUMENT = 4, +} cli_opt_status_t; + +/** An option provided on the command-line. */ +typedef struct cli_opt { + /** The status of parsing the most recent argument. */ + cli_opt_status_t status; + + /** + * The specification that was provided on the command-line, or + * `NULL` if the argument did not match an `cli_opt_spec`. + */ + const cli_opt_spec *spec; + + /** + * The argument as it was specified on the command-line, including + * dashes, eg, `-f` or `--foo`. + */ + char *arg; + + /** + * If the spec is of type `CLI_OPT_VALUE` or `CLI_OPT_VALUE_OPTIONAL`, + * this is the value provided to the argument. + */ + char *value; + + /** + * If the argument is of type `CLI_OPT_ARGS`, this is the number of + * arguments remaining. This value is persisted even when parsing + * is complete and `status` == `CLI_OPT_STATUS_DONE`. + */ + size_t args_len; +} cli_opt; + +/* The internal parser state. Callers should not modify this structure. */ +typedef struct cli_opt_parser { + const cli_opt_spec *specs; + char **args; + size_t args_len; + unsigned int flags; + + /* Parser state */ + size_t idx; + size_t arg_idx; + size_t in_args; + size_t in_short; + unsigned int needs_sort : 1, + in_literal : 1; +} cli_opt_parser; + +/** + * Parses all the command-line arguments and updates all the options using + * the pointers provided. Parsing stops on any invalid argument and + * information about the failure will be provided in the opt argument. + * + * This is the simplest way to parse options; it handles the initialization + * (`parser_init`) and looping (`parser_next`). + * + * @param opt The The `cli_opt` information that failed parsing + * @param specs A NULL-terminated array of `cli_opt_spec`s that can be parsed + * @param args The arguments that will be parsed + * @param args_len The length of arguments to be parsed + * @param flags The `cli_opt_flag_t flags for parsing + */ +cli_opt_status_t cli_opt_parse( + cli_opt *opt, + const cli_opt_spec specs[], + char **args, + size_t args_len, + unsigned int flags); + +/** + * Quickly executes the given callback for each argument. + * + * @param specs A NULL-terminated array of `cli_opt_spec`s that can be parsed + * @param args The arguments that will be parsed + * @param args_len The length of arguments to be parsed + * @param flags The `cli_opt_flag_t flags for parsing + * @param callback The callback to invoke for each specified option + * @param callback_data Data to be provided to the callback + */ +int cli_opt_foreach( + const cli_opt_spec specs[], + char **args, + size_t args_len, + unsigned int flags, + int (*callback)(cli_opt *, void *), + void *callback_data); + +/** + * Initializes a parser that parses the given arguments according to the + * given specifications. + * + * @param parser The `cli_opt_parser` that will be initialized + * @param specs A NULL-terminated array of `cli_opt_spec`s that can be parsed + * @param args The arguments that will be parsed + * @param args_len The length of arguments to be parsed + * @param flags The `cli_opt_flag_t flags for parsing + */ +void cli_opt_parser_init( + cli_opt_parser *parser, + const cli_opt_spec specs[], + char **args, + size_t args_len, + unsigned int flags); + +/** + * Parses the next command-line argument and places the information about + * the argument into the given `opt` data. + * + * @param opt The `cli_opt` information parsed from the argument + * @param parser An `cli_opt_parser` that has been initialized with + * `cli_opt_parser_init` + * @return true if the caller should continue iterating, or 0 if there are + * no arguments left to process. + */ +cli_opt_status_t cli_opt_parser_next( + cli_opt *opt, + cli_opt_parser *parser); + +/** + * Prints the status after parsing the most recent argument. This is + * useful for printing an error message when an unknown argument was + * specified, or when an argument was specified without a value. + * + * @param file The file to print information to + * @param command The name of the command to use when printing (optional) + * @param opt The option that failed to parse + * @return 0 on success, -1 on failure + */ +int cli_opt_status_fprint( + FILE *file, + const char *command, + const cli_opt *opt); + +#endif /* CLI_opt_h__ */ diff --git a/src/cli/opt_usage.c b/src/cli/opt_usage.c new file mode 100644 index 00000000000..5b6aee921f3 --- /dev/null +++ b/src/cli/opt_usage.c @@ -0,0 +1,263 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" +#include "str.h" + +#define is_switch_or_value(spec) \ + ((spec)->type == CLI_OPT_TYPE_SWITCH || \ + (spec)->type == CLI_OPT_TYPE_VALUE) + +static int print_spec_args(git_str *out, const cli_opt_spec *spec) +{ + GIT_ASSERT(!is_switch_or_value(spec)); + + if (spec->type == CLI_OPT_TYPE_ARG) + return git_str_printf(out, "<%s>", spec->value_name); + if (spec->type == CLI_OPT_TYPE_ARGS) + return git_str_printf(out, "<%s>...", spec->value_name); + if (spec->type == CLI_OPT_TYPE_LITERAL) + return git_str_printf(out, "--"); + + GIT_ASSERT(!"unknown option spec type"); + return -1; +} + +GIT_INLINE(int) print_spec_alias(git_str *out, const cli_opt_spec *spec) +{ + GIT_ASSERT(is_switch_or_value(spec) && spec->alias); + + if (spec->type == CLI_OPT_TYPE_VALUE && + !(spec->usage & CLI_OPT_USAGE_VALUE_OPTIONAL)) + return git_str_printf(out, "-%c <%s>", spec->alias, spec->value_name); + else if (spec->type == CLI_OPT_TYPE_VALUE) + return git_str_printf(out, "-%c [<%s>]", spec->alias, spec->value_name); + else + return git_str_printf(out, "-%c", spec->alias); +} + +GIT_INLINE(int) print_spec_name(git_str *out, const cli_opt_spec *spec) +{ + GIT_ASSERT(is_switch_or_value(spec) && spec->name); + + if (spec->type == CLI_OPT_TYPE_VALUE && + !(spec->usage & CLI_OPT_USAGE_VALUE_OPTIONAL)) + return git_str_printf(out, "--%s=<%s>", spec->name, spec->value_name); + else if (spec->type == CLI_OPT_TYPE_VALUE) + return git_str_printf(out, "--%s[=<%s>]", spec->name, spec->value_name); + else + return git_str_printf(out, "--%s", spec->name); +} + +GIT_INLINE(int) print_spec_full(git_str *out, const cli_opt_spec *spec) +{ + int error = 0; + + if (is_switch_or_value(spec)) { + if (spec->alias) + error |= print_spec_alias(out, spec); + + if (spec->alias && spec->name) + error |= git_str_printf(out, ", "); + + if (spec->name) + error |= print_spec_name(out, spec); + } else { + error |= print_spec_args(out, spec); + } + + return error; +} + +GIT_INLINE(int) print_spec(git_str *out, const cli_opt_spec *spec) +{ + if (is_switch_or_value(spec)) { + if (spec->alias && !(spec->usage & CLI_OPT_USAGE_SHOW_LONG)) + return print_spec_alias(out, spec); + else + return print_spec_name(out, spec); + } + + return print_spec_args(out, spec); +} + +/* + * This is similar to adopt's function, but modified to understand + * that we have a command ("git") and a "subcommand" ("checkout"). + * It also understands a terminal's line length and wrap appropriately, + * using a `git_str` for storage. + */ +int cli_opt_usage_fprint( + FILE *file, + const char *command, + const char *subcommand, + const cli_opt_spec specs[], + unsigned int print_flags) +{ + git_str usage = GIT_BUF_INIT, opt = GIT_BUF_INIT; + const cli_opt_spec *spec; + size_t i, prefixlen, linelen; + bool choice = false, next_choice = false, optional = false; + int error; + + /* TODO: query actual console width. */ + int console_width = 78; + + if ((error = git_str_printf(&usage, "usage: %s", command)) < 0) + goto done; + + if (subcommand && + (error = git_str_printf(&usage, " %s", subcommand)) < 0) + goto done; + + linelen = git_str_len(&usage); + prefixlen = linelen + 1; + + for (spec = specs; spec->type; ++spec) { + if (!choice) + optional = !(spec->usage & CLI_OPT_USAGE_REQUIRED); + + next_choice = !!((spec + 1)->usage & CLI_OPT_USAGE_CHOICE); + + if ((spec->usage & CLI_OPT_USAGE_HIDDEN) && + !(print_flags & CLI_OPT_USAGE_SHOW_HIDDEN)) + continue; + + if (choice) + git_str_putc(&opt, '|'); + else + git_str_clear(&opt); + + if (optional && !choice) + git_str_putc(&opt, '['); + if (!optional && !choice && next_choice) + git_str_putc(&opt, '('); + + if ((error = print_spec(&opt, spec)) < 0) + goto done; + + if (!optional && choice && !next_choice) + git_str_putc(&opt, ')'); + else if (optional && !next_choice) + git_str_putc(&opt, ']'); + + if ((choice = next_choice)) + continue; + + if (git_str_oom(&opt)) { + error = -1; + goto done; + } + + if (linelen > prefixlen && + console_width > 0 && + linelen + git_str_len(&opt) + 1 > (size_t)console_width) { + git_str_putc(&usage, '\n'); + + for (i = 0; i < prefixlen; i++) + git_str_putc(&usage, ' '); + + linelen = prefixlen; + } + + git_str_putc(&usage, ' '); + linelen += git_str_len(&opt) + 1; + + git_str_puts(&usage, git_str_cstr(&opt)); + + if (git_str_oom(&usage)) { + error = -1; + goto done; + } + } + + error = fprintf(file, "%s\n", git_str_cstr(&usage)); + +done: + error = (error < 0) ? -1 : 0; + + git_str_dispose(&usage); + git_str_dispose(&opt); + return error; +} + +int cli_opt_usage_error( + const char *subcommand, + const cli_opt_spec specs[], + const cli_opt *invalid_opt) +{ + cli_opt_status_fprint(stderr, PROGRAM_NAME, invalid_opt); + cli_opt_usage_fprint(stderr, PROGRAM_NAME, subcommand, specs, 0); + return CLI_EXIT_USAGE; +} + +int cli_opt_help_fprint( + FILE *file, + const cli_opt_spec specs[]) +{ + git_str help = GIT_BUF_INIT; + const cli_opt_spec *spec; + bool required = true; + int error = 0; + + /* Display required arguments first */ + for (spec = specs; spec->type; ++spec) { + if ((spec->usage & CLI_OPT_USAGE_HIDDEN) || + (spec->type == CLI_OPT_TYPE_LITERAL)) + continue; + + required = ((spec->usage & CLI_OPT_USAGE_REQUIRED) || + ((spec->usage & CLI_OPT_USAGE_CHOICE) && required)); + + if (!required) + continue; + + git_str_printf(&help, " "); + + if ((error = print_spec_full(&help, spec)) < 0) + goto done; + + git_str_printf(&help, "\n"); + + if (spec->help) + git_str_printf(&help, " %s\n", spec->help); + } + + /* Display the remaining arguments */ + for (spec = specs; spec->type; ++spec) { + if ((spec->usage & CLI_OPT_USAGE_HIDDEN) || + (spec->type == CLI_OPT_TYPE_LITERAL)) + continue; + + required = ((spec->usage & CLI_OPT_USAGE_REQUIRED) || + ((spec->usage & CLI_OPT_USAGE_CHOICE) && required)); + + if (required) + continue; + + git_str_printf(&help, " "); + + if ((error = print_spec_full(&help, spec)) < 0) + goto done; + + git_str_printf(&help, "\n"); + + if (spec->help) + git_str_printf(&help, " %s\n", spec->help); + } + + if (git_str_oom(&help) || + p_write(fileno(file), help.ptr, help.size) < 0) + error = -1; + +done: + error = (error < 0) ? -1 : 0; + + git_str_dispose(&help); + return error; +} + diff --git a/src/cli/opt_usage.h b/src/cli/opt_usage.h new file mode 100644 index 00000000000..b9b75b84b35 --- /dev/null +++ b/src/cli/opt_usage.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef CLI_opt_usage_h__ +#define CLI_opt_usage_h__ + +typedef enum { + CLI_OPT_USAGE_SHOW_HIDDEN = (1 << 0), +} cli_opt_usage_flags; + +/** + * Prints usage information to the given file handle. + * + * @param file The file to print information to + * @param command The name of the command to use when printing + * @param subcommand The name of the subcommand (eg "checkout") to use when printing, or NULL to skip + * @param specs The specifications allowed by the command + * @return 0 on success, -1 on failure + */ +int cli_opt_usage_fprint( + FILE *file, + const char *command, + const char *subcommand, + const cli_opt_spec specs[], + unsigned int print_flags); + +int cli_opt_usage_error( + const char *subcommand, + const cli_opt_spec specs[], + const cli_opt *invalid_opt); + +int cli_opt_help_fprint( + FILE *file, + const cli_opt_spec specs[]); + +#endif /* CLI_opt_usage_h__ */ diff --git a/src/cli/progress.c b/src/cli/progress.c new file mode 100644 index 00000000000..d975b0954ac --- /dev/null +++ b/src/cli/progress.c @@ -0,0 +1,395 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include +#include + +#include "progress.h" +#include "error.h" + +/* + * Show updates to the percentage and number of objects received + * separately from the throughput to give an accurate progress while + * avoiding too much noise on the screen. (In milliseconds.) + */ +#define PROGRESS_UPDATE_TIME 60 +#define THROUGHPUT_UPDATE_TIME 500 + +#define is_nl(c) ((c) == '\r' || (c) == '\n') + +#define return_os_error(msg) do { \ + git_error_set(GIT_ERROR_OS, "%s", msg); return -1; } while(0) + +GIT_INLINE(size_t) no_nl_len(const char *str, size_t len) +{ + size_t i = 0; + + while (i < len && !is_nl(str[i])) + i++; + + return i; +} + +GIT_INLINE(size_t) nl_len(bool *has_nl, const char *str, size_t len) +{ + size_t i = no_nl_len(str, len); + + *has_nl = false; + + while (i < len && is_nl(str[i])) { + *has_nl = true; + i++; + } + + return i; +} + +static int progress_write(cli_progress *progress, bool force, git_str *line) +{ + bool has_nl; + size_t no_nl = no_nl_len(line->ptr, line->size); + size_t nl = nl_len(&has_nl, line->ptr + no_nl, line->size - no_nl); + uint64_t now = git_time_monotonic(); + size_t i; + + /* Avoid spamming the console with progress updates */ + if (!force && line->ptr[line->size - 1] != '\n' && progress->last_update) { + if (now - progress->last_update < PROGRESS_UPDATE_TIME) { + git_str_clear(&progress->deferred); + git_str_put(&progress->deferred, line->ptr, line->size); + return git_str_oom(&progress->deferred) ? -1 : 0; + } + } + + /* + * If there's something on this line already (eg, a progress line + * with only a trailing `\r` that we'll print over) then we need + * to really print over it in case we're writing a shorter line. + */ + if (printf("%.*s", (int)no_nl, line->ptr) < 0) + return_os_error("could not print status"); + + if (progress->onscreen.size) { + for (i = no_nl; i < progress->onscreen.size; i++) { + if (printf(" ") < 0) + return_os_error("could not print status"); + } + } + + if (printf("%.*s", (int)nl, line->ptr + no_nl) < 0 || + fflush(stdout) != 0) + return_os_error("could not print status"); + + git_str_clear(&progress->onscreen); + + if (line->ptr[line->size - 1] == '\n') { + progress->last_update = 0; + } else { + git_str_put(&progress->onscreen, line->ptr, line->size); + progress->last_update = now; + } + + git_str_clear(&progress->deferred); + return git_str_oom(&progress->onscreen) ? -1 : 0; +} + +static int progress_printf(cli_progress *progress, bool force, const char *fmt, ...) + GIT_FORMAT_PRINTF(3, 4); + +int progress_printf(cli_progress *progress, bool force, const char *fmt, ...) +{ + git_str buf = GIT_BUF_INIT; + va_list ap; + int error; + + va_start(ap, fmt); + error = git_str_vprintf(&buf, fmt, ap); + va_end(ap); + + if (error < 0) + return error; + + error = progress_write(progress, force, &buf); + + git_str_dispose(&buf); + return error; +} + +static int progress_complete(cli_progress *progress) +{ + if (progress->deferred.size) + progress_write(progress, true, &progress->deferred); + + if (progress->onscreen.size) + if (printf("\n") < 0) + return_os_error("could not print status"); + + git_str_clear(&progress->deferred); + git_str_clear(&progress->onscreen); + progress->last_update = 0; + progress->action_start = 0; + progress->action_finish = 0; + + return 0; +} + +GIT_INLINE(int) percent(size_t completed, size_t total) +{ + if (total == 0) + return (completed == 0) ? 100 : 0; + + return (int)(((double)completed / (double)total) * 100); +} + +int cli_progress_fetch_sideband(const char *str, int len, void *payload) +{ + cli_progress *progress = (cli_progress *)payload; + size_t remain; + + if (len <= 0) + return 0; + + /* Accumulate the sideband data, then print it line-at-a-time. */ + if (git_str_put(&progress->sideband, str, len) < 0) + return -1; + + str = progress->sideband.ptr; + remain = progress->sideband.size; + + while (remain) { + bool has_nl; + size_t line_len = nl_len(&has_nl, str, remain); + + if (!has_nl) + break; + + if (line_len < INT_MAX) { + int error = progress_printf(progress, true, + "remote: %.*s", (int)line_len, str); + + if (error < 0) + return error; + } + + str += line_len; + remain -= line_len; + } + + git_str_consume_bytes(&progress->sideband, (progress->sideband.size - remain)); + + return 0; +} + +static int fetch_receiving( + cli_progress *progress, + const git_indexer_progress *stats) +{ + char *recv_units[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL }; + char *rate_units[] = { "B/s", "KiB/s", "MiB/s", "GiB/s", "TiB/s", NULL }; + uint64_t now, elapsed; + + double recv_len, rate; + size_t recv_unit_idx = 0, rate_unit_idx = 0; + bool done = (stats->received_objects == stats->total_objects); + + if (!progress->action_start) + progress->action_start = git_time_monotonic(); + + if (done && progress->action_finish) + now = progress->action_finish; + else if (done) + progress->action_finish = now = git_time_monotonic(); + else + now = git_time_monotonic(); + + if (progress->throughput_update && + now - progress->throughput_update < THROUGHPUT_UPDATE_TIME) { + elapsed = progress->throughput_update - + progress->action_start; + recv_len = progress->throughput_bytes; + } else { + elapsed = now - progress->action_start; + recv_len = (double)stats->received_bytes; + + progress->throughput_update = now; + progress->throughput_bytes = recv_len; + } + + rate = elapsed ? recv_len / elapsed : 0; + + while (recv_len > 1024 && recv_units[recv_unit_idx+1]) { + recv_len /= 1024; + recv_unit_idx++; + } + + while (rate > 1024 && rate_units[rate_unit_idx+1]) { + rate /= 1024; + rate_unit_idx++; + } + + return progress_printf(progress, false, + "Receiving objects: %3d%% (%d/%d), %.2f %s | %.2f %s%s\r", + percent(stats->received_objects, stats->total_objects), + stats->received_objects, + stats->total_objects, + recv_len, recv_units[recv_unit_idx], + rate, rate_units[rate_unit_idx], + done ? ", done." : ""); +} + +static int indexer_indexing( + cli_progress *progress, + const git_indexer_progress *stats) +{ + bool done = (stats->received_objects == stats->total_objects); + + return progress_printf(progress, false, + "Indexing objects: %3d%% (%d/%d)%s\r", + percent(stats->received_objects, stats->total_objects), + stats->received_objects, + stats->total_objects, + done ? ", done." : ""); +} + +static int indexer_resolving( + cli_progress *progress, + const git_indexer_progress *stats) +{ + bool done = (stats->indexed_deltas == stats->total_deltas); + + return progress_printf(progress, false, + "Resolving deltas: %3d%% (%d/%d)%s\r", + percent(stats->indexed_deltas, stats->total_deltas), + stats->indexed_deltas, stats->total_deltas, + done ? ", done." : ""); +} + +int cli_progress_fetch_transfer(const git_indexer_progress *stats, void *payload) +{ + cli_progress *progress = (cli_progress *)payload; + int error = 0; + + switch (progress->action) { + case CLI_PROGRESS_NONE: + progress->action = CLI_PROGRESS_RECEIVING; + /* fall through */ + + case CLI_PROGRESS_RECEIVING: + if ((error = fetch_receiving(progress, stats)) < 0) + break; + + /* + * Upgrade from receiving to resolving; do this after the + * final call to cli_progress_fetch_receiving (above) to + * ensure that we've printed a final "done" string after + * any sideband data. + */ + if (!stats->indexed_deltas) + break; + + progress_complete(progress); + progress->action = CLI_PROGRESS_RESOLVING; + /* fall through */ + + case CLI_PROGRESS_RESOLVING: + error = indexer_resolving(progress, stats); + break; + + default: + /* should not be reached */ + GIT_ASSERT(!"unexpected progress state"); + } + + return error; +} + +int cli_progress_indexer( + const git_indexer_progress *stats, + void *payload) +{ + cli_progress *progress = (cli_progress *)payload; + int error = 0; + + switch (progress->action) { + case CLI_PROGRESS_NONE: + progress->action = CLI_PROGRESS_INDEXING; + /* fall through */ + + case CLI_PROGRESS_INDEXING: + if ((error = indexer_indexing(progress, stats)) < 0) + break; + + if (stats->indexed_deltas == stats->total_deltas) + break; + + progress_complete(progress); + progress->action = CLI_PROGRESS_RESOLVING; + /* fall through */ + + case CLI_PROGRESS_RESOLVING: + error = indexer_resolving(progress, stats); + break; + + default: + /* should not be reached */ + GIT_ASSERT(!"unexpected progress state"); + } + + return error; +} + +void cli_progress_checkout( + const char *path, + size_t completed_steps, + size_t total_steps, + void *payload) +{ + cli_progress *progress = (cli_progress *)payload; + bool done = (completed_steps == total_steps); + + GIT_UNUSED(path); + + if (progress->action != CLI_PROGRESS_CHECKING_OUT) { + progress_complete(progress); + progress->action = CLI_PROGRESS_CHECKING_OUT; + } + + progress_printf(progress, false, + "Checking out files: %3d%% (%" PRIuZ "/%" PRIuZ ")%s\r", + percent(completed_steps, total_steps), + completed_steps, total_steps, + done ? ", done." : ""); +} + +int cli_progress_abort(cli_progress *progress) +{ + if (progress->onscreen.size > 0 && printf("\n") < 0) + return_os_error("could not print status"); + + return 0; +} + +int cli_progress_finish(cli_progress *progress) +{ + int error = progress->action ? progress_complete(progress) : 0; + + progress->action = 0; + return error; +} + +void cli_progress_dispose(cli_progress *progress) +{ + if (progress == NULL) + return; + + git_str_dispose(&progress->sideband); + git_str_dispose(&progress->onscreen); + git_str_dispose(&progress->deferred); + + memset(progress, 0, sizeof(cli_progress)); +} diff --git a/src/cli/progress.h b/src/cli/progress.h new file mode 100644 index 00000000000..f08d68f19e4 --- /dev/null +++ b/src/cli/progress.h @@ -0,0 +1,129 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef CLI_progress_h__ +#define CLI_progress_h__ + +#include +#include "str.h" + +/* + * A general purpose set of progress printing functions. An individual + * `cli_progress` object is capable of displaying progress for a single + * function, even if that function displays multiple pieces of progress + * (like `git_clone`). `cli_progress_finish` should be called after + * any function invocation to re-set state. + */ + +typedef enum { + CLI_PROGRESS_NONE, + CLI_PROGRESS_RECEIVING, + CLI_PROGRESS_INDEXING, + CLI_PROGRESS_RESOLVING, + CLI_PROGRESS_CHECKING_OUT +} cli_progress_t; + +typedef struct { + cli_progress_t action; + + /* Actions may time themselves (eg fetch) but are not required to */ + uint64_t action_start; + uint64_t action_finish; + + /* Last console update, avoid too frequent updates. */ + uint64_t last_update; + + /* Accumulators for partial output and deferred updates. */ + git_str sideband; + git_str onscreen; + git_str deferred; + + /* Last update about throughput */ + uint64_t throughput_update; + double throughput_bytes; +} cli_progress; + +#define CLI_PROGRESS_INIT { 0 } + +/** + * Prints sideband data from fetch to the console. Suitable for a + * `sideband_progress` callback for `git_fetch_options`. + * + * @param str The sideband string + * @param len The length of the sideband string + * @param payload A pointer to the cli_progress + * @return 0 on success, -1 on failure + */ +extern int cli_progress_fetch_sideband( + const char *str, + int len, + void *payload); + +/** + * Prints fetch transfer statistics to the console. Suitable for a + * `transfer_progress` callback for `git_fetch_options`. + * + * @param stats The indexer stats + * @param payload A pointer to the cli_progress + * @return 0 on success, -1 on failure + */ +extern int cli_progress_fetch_transfer( + const git_indexer_progress *stats, + void *payload); + +/** + * Prints indexer progress to the console. Suitable for a + * `progress_cb` callback for `git_indexer_options`. + * + * @param stats The indexer stats + * @param payload A pointer to the cli_progress + */ +extern int cli_progress_indexer( + const git_indexer_progress *stats, + void *payload); + +/** + * Prints checkout progress to the console. Suitable for a + * `progress_cb` callback for `git_checkout_options`. + * + * @param path The path being written + * @param completed_steps The completed checkout steps + * @param total_steps The total number of checkout steps + * @param payload A pointer to the cli_progress + */ +extern void cli_progress_checkout( + const char *path, + size_t completed_steps, + size_t total_steps, + void *payload); + +/** + * Stop displaying progress quickly; suitable for stopping an application + * quickly. Does not display any lines that were buffered, just gets the + * console back to a sensible place. + * + * @param progress The progress information + * @return 0 on success, -1 on failure + */ +extern int cli_progress_abort(cli_progress *progress); + +/** + * Finishes displaying progress; flushes any buffered output. + * + * @param progress The progress information + * @return 0 on success, -1 on failure + */ +extern int cli_progress_finish(cli_progress *progress); + +/** + * Disposes the progress information. + * + * @param progress The progress information + */ +extern void cli_progress_dispose(cli_progress *progress); + +#endif /* CLI_progress_h__ */ diff --git a/src/cli/sighandler.h b/src/cli/sighandler.h new file mode 100644 index 00000000000..877223e02a6 --- /dev/null +++ b/src/cli/sighandler.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef CLI_sighandler_h__ +#define CLI_sighandler_h__ + +/** + * Sets up a signal handler that will run when the process is interrupted + * (via SIGINT on POSIX or Control-C or Control-Break on Windows). + * + * @param handler The function to run on interrupt + * @return 0 on success, -1 on failure + */ +int cli_sighandler_set_interrupt(void (*handler)(void)); + +#endif /* CLI_sighandler_h__ */ diff --git a/src/cli/unix/sighandler.c b/src/cli/unix/sighandler.c new file mode 100644 index 00000000000..05ac8672cad --- /dev/null +++ b/src/cli/unix/sighandler.c @@ -0,0 +1,37 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include +#include "git2_util.h" +#include "common.h" +#include "sighandler.h" + +static void (*interrupt_handler)(void) = NULL; + +static void interrupt_proxy(int signal) +{ + GIT_UNUSED(signal); + interrupt_handler(); +} + +int cli_sighandler_set_interrupt(void (*handler)(void)) +{ + void (*result)(int); + + if ((interrupt_handler = handler) != NULL) + result = signal(SIGINT, interrupt_proxy); + else + result = signal(SIGINT, SIG_DFL); + + if (result == SIG_ERR) { + git_error_set(GIT_ERROR_OS, "could not set signal handler"); + return -1; + } + + return 0; +} diff --git a/src/win32/precompiled.c b/src/cli/win32/precompiled.c similarity index 100% rename from src/win32/precompiled.c rename to src/cli/win32/precompiled.c diff --git a/src/cli/win32/precompiled.h b/src/cli/win32/precompiled.h new file mode 100644 index 00000000000..031370e87e7 --- /dev/null +++ b/src/cli/win32/precompiled.h @@ -0,0 +1,3 @@ +#include + +#include "common.h" diff --git a/src/cli/win32/sighandler.c b/src/cli/win32/sighandler.c new file mode 100644 index 00000000000..05a67fb14fe --- /dev/null +++ b/src/cli/win32/sighandler.c @@ -0,0 +1,37 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2_util.h" +#include + +#include "sighandler.h" + +static void (*interrupt_handler)(void) = NULL; + +static BOOL WINAPI interrupt_proxy(DWORD signal) +{ + GIT_UNUSED(signal); + interrupt_handler(); + return TRUE; +} + +int cli_sighandler_set_interrupt(void (*handler)(void)) +{ + BOOL result; + + if ((interrupt_handler = handler) != NULL) + result = SetConsoleCtrlHandler(interrupt_proxy, FALSE); + else + result = SetConsoleCtrlHandler(NULL, FALSE); + + if (!result) { + git_error_set(GIT_ERROR_OS, "could not set control control handler"); + return -1; + } + + return 0; +} diff --git a/src/clone.c b/src/clone.c deleted file mode 100644 index 333bf21486a..00000000000 --- a/src/clone.c +++ /dev/null @@ -1,456 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include - -#include "git2/clone.h" -#include "git2/remote.h" -#include "git2/revparse.h" -#include "git2/branch.h" -#include "git2/config.h" -#include "git2/checkout.h" -#include "git2/commit.h" -#include "git2/tree.h" - -#include "common.h" -#include "remote.h" -#include "fileops.h" -#include "refs.h" -#include "path.h" - -static int create_branch( - git_reference **branch, - git_repository *repo, - const git_oid *target, - const char *name) -{ - git_commit *head_obj = NULL; - git_reference *branch_ref = NULL; - int error; - - /* Find the target commit */ - if ((error = git_commit_lookup(&head_obj, repo, target)) < 0) - return error; - - /* Create the new branch */ - error = git_branch_create(&branch_ref, repo, name, head_obj, 0); - - git_commit_free(head_obj); - - if (!error) - *branch = branch_ref; - else - git_reference_free(branch_ref); - - return error; -} - -static int setup_tracking_config( - git_repository *repo, - const char *branch_name, - const char *remote_name, - const char *merge_target) -{ - git_config *cfg; - git_buf remote_key = GIT_BUF_INIT, merge_key = GIT_BUF_INIT; - int error = -1; - - if (git_repository_config__weakptr(&cfg, repo) < 0) - return -1; - - if (git_buf_printf(&remote_key, "branch.%s.remote", branch_name) < 0) - goto cleanup; - - if (git_buf_printf(&merge_key, "branch.%s.merge", branch_name) < 0) - goto cleanup; - - if (git_config_set_string(cfg, git_buf_cstr(&remote_key), remote_name) < 0) - goto cleanup; - - if (git_config_set_string(cfg, git_buf_cstr(&merge_key), merge_target) < 0) - goto cleanup; - - error = 0; - -cleanup: - git_buf_free(&remote_key); - git_buf_free(&merge_key); - return error; -} - -static int create_tracking_branch( - git_reference **branch, - git_repository *repo, - const git_oid *target, - const char *branch_name) -{ - int error; - - if ((error = create_branch(branch, repo, target, branch_name)) < 0) - return error; - - return setup_tracking_config( - repo, - branch_name, - GIT_REMOTE_ORIGIN, - git_reference_name(*branch)); -} - -struct head_info { - git_repository *repo; - git_oid remote_head_oid; - git_buf branchname; - const git_refspec *refspec; - bool found; -}; - -static int reference_matches_remote_head( - const char *reference_name, - void *payload) -{ - struct head_info *head_info = (struct head_info *)payload; - git_oid oid; - - /* TODO: Should we guard against references - * which name doesn't start with refs/heads/ ? - */ - - /* Stop looking if we've already found a match */ - if (head_info->found) - return 0; - - if (git_reference_name_to_id( - &oid, - head_info->repo, - reference_name) < 0) { - /* If the reference doesn't exists, it obviously cannot match the expected oid. */ - giterr_clear(); - return 0; - } - - if (git_oid_cmp(&head_info->remote_head_oid, &oid) == 0) { - /* Determine the local reference name from the remote tracking one */ - if (git_refspec_transform_l( - &head_info->branchname, - head_info->refspec, - reference_name) < 0) - return -1; - - if (git_buf_len(&head_info->branchname) > 0) { - if (git_buf_sets( - &head_info->branchname, - git_buf_cstr(&head_info->branchname) + strlen(GIT_REFS_HEADS_DIR)) < 0) - return -1; - - head_info->found = 1; - } - } - - return 0; -} - -static int update_head_to_new_branch( - git_repository *repo, - const git_oid *target, - const char *name) -{ - git_reference *tracking_branch = NULL; - int error; - - if ((error = create_tracking_branch( - &tracking_branch, - repo, - target, - name)) < 0) - return error; - - error = git_repository_set_head(repo, git_reference_name(tracking_branch)); - - git_reference_free(tracking_branch); - - return error; -} - -static int get_head_callback(git_remote_head *head, void *payload) -{ - git_remote_head **destination = (git_remote_head **)payload; - - /* Save the first entry, and terminate the enumeration */ - *destination = head; - return 1; -} - -static int update_head_to_remote(git_repository *repo, git_remote *remote) -{ - int retcode = -1; - git_remote_head *remote_head; - struct head_info head_info; - git_buf remote_master_name = GIT_BUF_INIT; - - /* Did we just clone an empty repository? */ - if (remote->refs.length == 0) { - return setup_tracking_config( - repo, - "master", - GIT_REMOTE_ORIGIN, - GIT_REFS_HEADS_MASTER_FILE); - } - - /* Get the remote's HEAD. This is always the first ref in remote->refs. */ - remote_head = NULL; - - if (!remote->transport->ls(remote->transport, get_head_callback, &remote_head)) - return -1; - - assert(remote_head); - - git_oid_cpy(&head_info.remote_head_oid, &remote_head->oid); - git_buf_init(&head_info.branchname, 16); - head_info.repo = repo; - head_info.refspec = git_remote_fetchspec(remote); - head_info.found = 0; - - /* Determine the remote tracking reference name from the local master */ - if (git_refspec_transform_r( - &remote_master_name, - head_info.refspec, - GIT_REFS_HEADS_MASTER_FILE) < 0) - return -1; - - /* Check to see if the remote HEAD points to the remote master */ - if (reference_matches_remote_head(git_buf_cstr(&remote_master_name), &head_info) < 0) - goto cleanup; - - if (head_info.found) { - retcode = update_head_to_new_branch( - repo, - &head_info.remote_head_oid, - git_buf_cstr(&head_info.branchname)); - - goto cleanup; - } - - /* Not master. Check all the other refs. */ - if (git_reference_foreach( - repo, - GIT_REF_LISTALL, - reference_matches_remote_head, - &head_info) < 0) - goto cleanup; - - if (head_info.found) { - retcode = update_head_to_new_branch( - repo, - &head_info.remote_head_oid, - git_buf_cstr(&head_info.branchname)); - - goto cleanup; - } else { - retcode = git_repository_set_head_detached( - repo, - &head_info.remote_head_oid); - goto cleanup; - } - -cleanup: - git_buf_free(&remote_master_name); - git_buf_free(&head_info.branchname); - return retcode; -} - -static int update_head_to_branch( - git_repository *repo, - const git_clone_options *options) -{ - int retcode; - git_buf remote_branch_name = GIT_BUF_INIT; - git_reference* remote_ref = NULL; - - assert(options->checkout_branch); - - if ((retcode = git_buf_printf(&remote_branch_name, GIT_REFS_REMOTES_DIR "%s/%s", - options->remote_name, options->checkout_branch)) < 0 ) - goto cleanup; - - if ((retcode = git_reference_lookup(&remote_ref, repo, git_buf_cstr(&remote_branch_name))) < 0) - goto cleanup; - - retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), - options->checkout_branch); - -cleanup: - git_reference_free(remote_ref); - git_buf_free(&remote_branch_name); - return retcode; -} - -/* - * submodules? - */ - -static int create_and_configure_origin( - git_remote **out, - git_repository *repo, - const char *url, - const git_clone_options *options) -{ - int error; - git_remote *origin = NULL; - - if ((error = git_remote_create(&origin, repo, options->remote_name, url)) < 0) - goto on_error; - - git_remote_set_cred_acquire_cb(origin, options->cred_acquire_cb, - options->cred_acquire_payload); - git_remote_set_autotag(origin, options->remote_autotag); - /* - * Don't write FETCH_HEAD, we'll check out the remote tracking - * branch ourselves based on the server's default. - */ - git_remote_set_update_fetchhead(origin, 0); - - if (options->remote_callbacks && - (error = git_remote_set_callbacks(origin, options->remote_callbacks)) < 0) - goto on_error; - - if (options->fetch_spec && - (error = git_remote_set_fetchspec(origin, options->fetch_spec)) < 0) - goto on_error; - - if (options->push_spec && - (error = git_remote_set_pushspec(origin, options->push_spec)) < 0) - goto on_error; - - if (options->pushurl && - (error = git_remote_set_pushurl(origin, options->pushurl)) < 0) - goto on_error; - - if ((error = git_remote_save(origin)) < 0) - goto on_error; - - *out = origin; - return 0; - -on_error: - git_remote_free(origin); - return error; -} - - -static int setup_remotes_and_fetch( - git_repository *repo, - const char *url, - const git_clone_options *options) -{ - int retcode = GIT_ERROR; - git_remote *origin; - - /* Construct an origin remote */ - if (!create_and_configure_origin(&origin, repo, url, options)) { - git_remote_set_update_fetchhead(origin, 0); - - /* Connect and download everything */ - if (!git_remote_connect(origin, GIT_DIRECTION_FETCH)) { - if (!git_remote_download(origin, options->fetch_progress_cb, - options->fetch_progress_payload)) { - /* Create "origin/foo" branches for all remote branches */ - if (!git_remote_update_tips(origin)) { - /* Point HEAD to the requested branch */ - if (options->checkout_branch) { - if (!update_head_to_branch(repo, options)) - retcode = 0; - } - /* Point HEAD to the same ref as the remote's head */ - else if (!update_head_to_remote(repo, origin)) { - retcode = 0; - } - } - } - git_remote_disconnect(origin); - } - git_remote_free(origin); - } - - return retcode; -} - - -static bool path_is_okay(const char *path) -{ - /* The path must either not exist, or be an empty directory */ - if (!git_path_exists(path)) return true; - if (!git_path_is_empty_dir(path)) { - giterr_set(GITERR_INVALID, - "'%s' exists and is not an empty directory", path); - return false; - } - return true; -} - -static bool should_checkout( - git_repository *repo, - bool is_bare, - git_checkout_opts *opts) -{ - if (is_bare) - return false; - - if (!opts) - return false; - - if (opts->checkout_strategy == GIT_CHECKOUT_NONE) - return false; - - return !git_repository_head_orphan(repo); -} - -static void normalize_options(git_clone_options *dst, const git_clone_options *src) -{ - git_clone_options default_options = GIT_CLONE_OPTIONS_INIT; - if (!src) src = &default_options; - - *dst = *src; - - /* Provide defaults for null pointers */ - if (!dst->remote_name) dst->remote_name = "origin"; -} - -int git_clone( - git_repository **out, - const char *url, - const char *local_path, - const git_clone_options *options) -{ - int retcode = GIT_ERROR; - git_repository *repo = NULL; - git_clone_options normOptions; - - assert(out && url && local_path); - - normalize_options(&normOptions, options); - GITERR_CHECK_VERSION(&normOptions, GIT_CLONE_OPTIONS_VERSION, "git_clone_options"); - - if (!path_is_okay(local_path)) { - return GIT_ERROR; - } - - if (!(retcode = git_repository_init(&repo, local_path, normOptions.bare))) { - if ((retcode = setup_remotes_and_fetch(repo, url, &normOptions)) < 0) { - /* Failed to fetch; clean up */ - git_repository_free(repo); - git_futils_rmdir_r(local_path, NULL, GIT_RMDIR_REMOVE_FILES); - } else { - *out = repo; - retcode = 0; - } - } - - if (!retcode && should_checkout(repo, normOptions.bare, &normOptions.checkout_opts)) - retcode = git_checkout_head(*out, &normOptions.checkout_opts); - - return retcode; -} diff --git a/src/commit.c b/src/commit.c deleted file mode 100644 index 29ce3910746..00000000000 --- a/src/commit.c +++ /dev/null @@ -1,290 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "git2/common.h" -#include "git2/object.h" -#include "git2/repository.h" -#include "git2/signature.h" - -#include "common.h" -#include "odb.h" -#include "commit.h" -#include "signature.h" -#include "message.h" - -#include - -static void clear_parents(git_commit *commit) -{ - unsigned int i; - - for (i = 0; i < commit->parent_ids.length; ++i) { - git_oid *parent = git_vector_get(&commit->parent_ids, i); - git__free(parent); - } - - git_vector_clear(&commit->parent_ids); -} - -void git_commit__free(git_commit *commit) -{ - clear_parents(commit); - git_vector_free(&commit->parent_ids); - - git_signature_free(commit->author); - git_signature_free(commit->committer); - - git__free(commit->message); - git__free(commit->message_encoding); - git__free(commit); -} - -int git_commit_create_v( - git_oid *oid, - git_repository *repo, - const char *update_ref, - const git_signature *author, - const git_signature *committer, - const char *message_encoding, - const char *message, - const git_tree *tree, - int parent_count, - ...) -{ - va_list ap; - int i, res; - const git_commit **parents; - - parents = git__malloc(parent_count * sizeof(git_commit *)); - GITERR_CHECK_ALLOC(parents); - - va_start(ap, parent_count); - for (i = 0; i < parent_count; ++i) - parents[i] = va_arg(ap, const git_commit *); - va_end(ap); - - res = git_commit_create( - oid, repo, update_ref, author, committer, - message_encoding, message, - tree, parent_count, parents); - - git__free((void *)parents); - return res; -} - -int git_commit_create( - git_oid *oid, - git_repository *repo, - const char *update_ref, - const git_signature *author, - const git_signature *committer, - const char *message_encoding, - const char *message, - const git_tree *tree, - int parent_count, - const git_commit *parents[]) -{ - git_buf commit = GIT_BUF_INIT; - int i; - git_odb *odb; - - assert(git_object_owner((const git_object *)tree) == repo); - - git_oid__writebuf(&commit, "tree ", git_object_id((const git_object *)tree)); - - for (i = 0; i < parent_count; ++i) { - assert(git_object_owner((const git_object *)parents[i]) == repo); - git_oid__writebuf(&commit, "parent ", git_object_id((const git_object *)parents[i])); - } - - git_signature__writebuf(&commit, "author ", author); - git_signature__writebuf(&commit, "committer ", committer); - - if (message_encoding != NULL) - git_buf_printf(&commit, "encoding %s\n", message_encoding); - - git_buf_putc(&commit, '\n'); - - if (git_buf_puts(&commit, message) < 0) - goto on_error; - - if (git_repository_odb__weakptr(&odb, repo) < 0) - goto on_error; - - if (git_odb_write(oid, odb, commit.ptr, commit.size, GIT_OBJ_COMMIT) < 0) - goto on_error; - - git_buf_free(&commit); - - if (update_ref != NULL) - return git_reference__update(repo, oid, update_ref); - - return 0; - -on_error: - git_buf_free(&commit); - giterr_set(GITERR_OBJECT, "Failed to create commit."); - return -1; -} - -int git_commit__parse_buffer(git_commit *commit, const void *data, size_t len) -{ - const char *buffer = data; - const char *buffer_end = (const char *)data + len; - git_oid parent_id; - - if (git_vector_init(&commit->parent_ids, 4, NULL) < 0) - return -1; - - if (git_oid__parse(&commit->tree_id, &buffer, buffer_end, "tree ") < 0) - goto bad_buffer; - - /* - * TODO: commit grafts! - */ - - while (git_oid__parse(&parent_id, &buffer, buffer_end, "parent ") == 0) { - git_oid *new_id = git__malloc(sizeof(git_oid)); - GITERR_CHECK_ALLOC(new_id); - - git_oid_cpy(new_id, &parent_id); - - if (git_vector_insert(&commit->parent_ids, new_id) < 0) - return -1; - } - - commit->author = git__malloc(sizeof(git_signature)); - GITERR_CHECK_ALLOC(commit->author); - - if (git_signature__parse(commit->author, &buffer, buffer_end, "author ", '\n') < 0) - return -1; - - /* Always parse the committer; we need the commit time */ - commit->committer = git__malloc(sizeof(git_signature)); - GITERR_CHECK_ALLOC(commit->committer); - - if (git_signature__parse(commit->committer, &buffer, buffer_end, "committer ", '\n') < 0) - return -1; - - /* Parse add'l header entries until blank line found */ - while (buffer < buffer_end && *buffer != '\n') { - const char *eoln = buffer; - while (eoln < buffer_end && *eoln != '\n') - ++eoln; - - if (git__prefixcmp(buffer, "encoding ") == 0) { - buffer += strlen("encoding "); - - commit->message_encoding = git__strndup(buffer, eoln - buffer); - GITERR_CHECK_ALLOC(commit->message_encoding); - } - - if (eoln < buffer_end && *eoln == '\n') - ++eoln; - - buffer = eoln; - } - - /* skip blank lines */ - while (buffer < buffer_end - 1 && *buffer == '\n') - buffer++; - - /* parse commit message */ - if (buffer <= buffer_end) { - commit->message = git__strndup(buffer, buffer_end - buffer); - GITERR_CHECK_ALLOC(commit->message); - } - - return 0; - -bad_buffer: - giterr_set(GITERR_OBJECT, "Failed to parse bad commit object"); - return -1; -} - -int git_commit__parse(git_commit *commit, git_odb_object *obj) -{ - assert(commit); - return git_commit__parse_buffer(commit, obj->raw.data, obj->raw.len); -} - -#define GIT_COMMIT_GETTER(_rvalue, _name, _return) \ - _rvalue git_commit_##_name(const git_commit *commit) \ - {\ - assert(commit); \ - return _return; \ - } - -GIT_COMMIT_GETTER(const git_signature *, author, commit->author) -GIT_COMMIT_GETTER(const git_signature *, committer, commit->committer) -GIT_COMMIT_GETTER(const char *, message, commit->message) -GIT_COMMIT_GETTER(const char *, message_encoding, commit->message_encoding) -GIT_COMMIT_GETTER(git_time_t, time, commit->committer->when.time) -GIT_COMMIT_GETTER(int, time_offset, commit->committer->when.offset) -GIT_COMMIT_GETTER(unsigned int, parentcount, (unsigned int)commit->parent_ids.length) -GIT_COMMIT_GETTER(const git_oid *, tree_id, &commit->tree_id); - -int git_commit_tree(git_tree **tree_out, const git_commit *commit) -{ - assert(commit); - return git_tree_lookup(tree_out, commit->object.repo, &commit->tree_id); -} - -const git_oid *git_commit_parent_id(git_commit *commit, unsigned int n) -{ - assert(commit); - - return git_vector_get(&commit->parent_ids, n); -} - -int git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n) -{ - const git_oid *parent_id; - assert(commit); - - parent_id = git_commit_parent_id(commit, n); - if (parent_id == NULL) { - giterr_set(GITERR_INVALID, "Parent %u does not exist", n); - return GIT_ENOTFOUND; - } - - return git_commit_lookup(parent, commit->object.repo, parent_id); -} - -int git_commit_nth_gen_ancestor( - git_commit **ancestor, - const git_commit *commit, - unsigned int n) -{ - git_commit *current, *parent; - int error; - - assert(ancestor && commit); - - current = (git_commit *)commit; - - if (n == 0) - return git_commit_lookup( - ancestor, - commit->object.repo, - git_object_id((const git_object *)commit)); - - while (n--) { - error = git_commit_parent(&parent, (git_commit *)current, 0); - - if (current != commit) - git_commit_free(current); - - if (error < 0) - return error; - - current = parent; - } - - *ancestor = parent; - return 0; -} diff --git a/src/commit.h b/src/commit.h deleted file mode 100644 index 1ab164c0b65..00000000000 --- a/src/commit.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_commit_h__ -#define INCLUDE_commit_h__ - -#include "git2/commit.h" -#include "tree.h" -#include "repository.h" -#include "vector.h" - -#include - -struct git_commit { - git_object object; - - git_vector parent_ids; - git_oid tree_id; - - git_signature *author; - git_signature *committer; - - char *message_encoding; - char *message; -}; - -void git_commit__free(git_commit *c); -int git_commit__parse(git_commit *commit, git_odb_object *obj); - -int git_commit__parse_buffer(git_commit *commit, const void *data, size_t len); -#endif diff --git a/src/commit_list.c b/src/commit_list.c deleted file mode 100644 index 603dd754a35..00000000000 --- a/src/commit_list.c +++ /dev/null @@ -1,194 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "commit_list.h" -#include "common.h" -#include "revwalk.h" -#include "pool.h" -#include "odb.h" - -int git_commit_list_time_cmp(void *a, void *b) -{ - git_commit_list_node *commit_a = (git_commit_list_node *)a; - git_commit_list_node *commit_b = (git_commit_list_node *)b; - - return (commit_a->time < commit_b->time); -} - -git_commit_list *git_commit_list_insert(git_commit_list_node *item, git_commit_list **list_p) -{ - git_commit_list *new_list = git__malloc(sizeof(git_commit_list)); - if (new_list != NULL) { - new_list->item = item; - new_list->next = *list_p; - } - *list_p = new_list; - return new_list; -} - -git_commit_list *git_commit_list_insert_by_date(git_commit_list_node *item, git_commit_list **list_p) -{ - git_commit_list **pp = list_p; - git_commit_list *p; - - while ((p = *pp) != NULL) { - if (git_commit_list_time_cmp(p->item, item) < 0) - break; - - pp = &p->next; - } - - return git_commit_list_insert(item, pp); -} - -git_commit_list_node *git_commit_list_alloc_node(git_revwalk *walk) -{ - return (git_commit_list_node *)git_pool_malloc(&walk->commit_pool, COMMIT_ALLOC); -} - -static int commit_error(git_commit_list_node *commit, const char *msg) -{ - char commit_oid[GIT_OID_HEXSZ + 1]; - git_oid_fmt(commit_oid, &commit->oid); - commit_oid[GIT_OID_HEXSZ] = '\0'; - - giterr_set(GITERR_ODB, "Failed to parse commit %s - %s", commit_oid, msg); - - return -1; -} - -static git_commit_list_node **alloc_parents( - git_revwalk *walk, git_commit_list_node *commit, size_t n_parents) -{ - if (n_parents <= PARENTS_PER_COMMIT) - return (git_commit_list_node **)((char *)commit + sizeof(git_commit_list_node)); - - return (git_commit_list_node **)git_pool_malloc( - &walk->commit_pool, (uint32_t)(n_parents * sizeof(git_commit_list_node *))); -} - - -void git_commit_list_free(git_commit_list **list_p) -{ - git_commit_list *list = *list_p; - - if (list == NULL) - return; - - while (list) { - git_commit_list *temp = list; - list = temp->next; - git__free(temp); - } - - *list_p = NULL; -} - -git_commit_list_node *git_commit_list_pop(git_commit_list **stack) -{ - git_commit_list *top = *stack; - git_commit_list_node *item = top ? top->item : NULL; - - if (top) { - *stack = top->next; - git__free(top); - } - return item; -} - -static int commit_quick_parse(git_revwalk *walk, git_commit_list_node *commit, git_rawobj *raw) -{ - const size_t parent_len = strlen("parent ") + GIT_OID_HEXSZ + 1; - unsigned char *buffer = raw->data; - unsigned char *buffer_end = buffer + raw->len; - unsigned char *parents_start, *committer_start; - int i, parents = 0; - int commit_time; - - buffer += strlen("tree ") + GIT_OID_HEXSZ + 1; - - parents_start = buffer; - while (buffer + parent_len < buffer_end && memcmp(buffer, "parent ", strlen("parent ")) == 0) { - parents++; - buffer += parent_len; - } - - commit->parents = alloc_parents(walk, commit, parents); - GITERR_CHECK_ALLOC(commit->parents); - - buffer = parents_start; - for (i = 0; i < parents; ++i) { - git_oid oid; - - if (git_oid_fromstr(&oid, (char *)buffer + strlen("parent ")) < 0) - return -1; - - commit->parents[i] = git_revwalk__commit_lookup(walk, &oid); - if (commit->parents[i] == NULL) - return -1; - - buffer += parent_len; - } - - commit->out_degree = (unsigned short)parents; - - if ((committer_start = buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) - return commit_error(commit, "object is corrupted"); - - buffer++; - - if ((buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) - return commit_error(commit, "object is corrupted"); - - /* Skip trailing spaces */ - while (buffer > committer_start && git__isspace(*buffer)) - buffer--; - - /* Seek for the begining of the pack of digits */ - while (buffer > committer_start && git__isdigit(*buffer)) - buffer--; - - /* Skip potential timezone offset */ - if ((buffer > committer_start) && (*buffer == '+' || *buffer == '-')) { - buffer--; - - while (buffer > committer_start && git__isspace(*buffer)) - buffer--; - - while (buffer > committer_start && git__isdigit(*buffer)) - buffer--; - } - - if ((buffer == committer_start) || (git__strtol32(&commit_time, (char *)(buffer + 1), NULL, 10) < 0)) - return commit_error(commit, "cannot parse commit time"); - - commit->time = (time_t)commit_time; - commit->parsed = 1; - return 0; -} - -int git_commit_list_parse(git_revwalk *walk, git_commit_list_node *commit) -{ - git_odb_object *obj; - int error; - - if (commit->parsed) - return 0; - - if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < 0) - return error; - - if (obj->raw.type != GIT_OBJ_COMMIT) { - giterr_set(GITERR_INVALID, "Object is no commit object"); - error = -1; - } else - error = commit_quick_parse(walk, commit, &obj->raw); - - git_odb_object_free(obj); - return error; -} - diff --git a/src/common.h b/src/common.h deleted file mode 100644 index ca203ee5c6e..00000000000 --- a/src/common.h +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_common_h__ -#define INCLUDE_common_h__ - -#include "git2/common.h" -#include "cc-compat.h" - -#include -#include -#include -#include -#include -#include - -#include -#include - -#ifdef GIT_WIN32 - -# include -# include -# include -# include -# include "win32/msvc-compat.h" -# include "win32/mingw-compat.h" -# include "win32/error.h" -# ifdef GIT_THREADS -# include "win32/pthread.h" -#endif - -# define snprintf _snprintf - -#else -# include - -# ifdef GIT_THREADS -# include -# endif -#endif - -#include "git2/types.h" -#include "git2/errors.h" -#include "thread-utils.h" -#include "bswap.h" - -#include - -/** - * Check a pointer allocation result, returning -1 if it failed. - */ -#define GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; } - -/** - * Set the error message for this thread, formatting as needed. - */ -void giterr_set(int error_class, const char *string, ...); - -/** - * Set the error message for a regex failure, using the internal regex - * error code lookup and return a libgit error code. - */ -int giterr_set_regex(const regex_t *regex, int error_code); - -/** - * Check a versioned structure for validity - */ -GIT_INLINE(int) giterr__check_version(const void *structure, unsigned int expected_max, const char *name) -{ - unsigned int actual; - - if (!structure) - return 0; - - actual = *(const unsigned int*)structure; - if (actual > 0 && actual <= expected_max) - return 0; - - giterr_set(GITERR_INVALID, "Invalid version %d on %s", actual, name); - return -1; -} -#define GITERR_CHECK_VERSION(S,V,N) if (giterr__check_version(S,V,N) < 0) return -1 - -/** - * Initialize a structure with a version. - */ -GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int version) -{ - memset(structure, 0, len); - *((int*)structure) = version; -} -#define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V) - -/* NOTE: other giterr functions are in the public errors.h header file */ - -#include "util.h" - -#endif /* INCLUDE_common_h__ */ diff --git a/src/compress.c b/src/compress.c deleted file mode 100644 index 14b79404c85..00000000000 --- a/src/compress.c +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "compress.h" - -#include - -#define BUFFER_SIZE (1024 * 1024) - -int git__compress(git_buf *buf, const void *buff, size_t len) -{ - z_stream zs; - char *zb; - size_t have; - - memset(&zs, 0, sizeof(zs)); - if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK) - return -1; - - zb = git__malloc(BUFFER_SIZE); - GITERR_CHECK_ALLOC(zb); - - zs.next_in = (void *)buff; - zs.avail_in = (uInt)len; - - do { - zs.next_out = (unsigned char *)zb; - zs.avail_out = BUFFER_SIZE; - - if (deflate(&zs, Z_FINISH) == Z_STREAM_ERROR) { - git__free(zb); - return -1; - } - - have = BUFFER_SIZE - (size_t)zs.avail_out; - - if (git_buf_put(buf, zb, have) < 0) { - git__free(zb); - return -1; - } - - } while (zs.avail_out == 0); - - assert(zs.avail_in == 0); - - deflateEnd(&zs); - git__free(zb); - return 0; -} diff --git a/src/compress.h b/src/compress.h deleted file mode 100644 index 49e6f4749c4..00000000000 --- a/src/compress.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_compress_h__ -#define INCLUDE_compress_h__ - -#include "common.h" - -#include "buffer.h" - -int git__compress(git_buf *buf, const void *buff, size_t len); - -#endif /* INCLUDE_compress_h__ */ diff --git a/src/config.c b/src/config.c deleted file mode 100644 index ce105089e58..00000000000 --- a/src/config.c +++ /dev/null @@ -1,840 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "fileops.h" -#include "config.h" -#include "git2/config.h" -#include "vector.h" -#include "buf_text.h" -#include "config_file.h" -#if GIT_WIN32 -# include -#endif - -#include - -typedef struct { - git_refcount rc; - - git_config_backend *file; - unsigned int level; -} file_internal; - -static void file_internal_free(file_internal *internal) -{ - git_config_backend *file; - - file = internal->file; - file->free(file); - git__free(internal); -} - -static void config_free(git_config *cfg) -{ - unsigned int i; - file_internal *internal; - - for(i = 0; i < cfg->files.length; ++i){ - internal = git_vector_get(&cfg->files, i); - GIT_REFCOUNT_DEC(internal, file_internal_free); - } - - git_vector_free(&cfg->files); - git__free(cfg); -} - -void git_config_free(git_config *cfg) -{ - if (cfg == NULL) - return; - - GIT_REFCOUNT_DEC(cfg, config_free); -} - -static int config_backend_cmp(const void *a, const void *b) -{ - const file_internal *bk_a = (const file_internal *)(a); - const file_internal *bk_b = (const file_internal *)(b); - - return bk_b->level - bk_a->level; -} - -int git_config_new(git_config **out) -{ - git_config *cfg; - - cfg = git__malloc(sizeof(git_config)); - GITERR_CHECK_ALLOC(cfg); - - memset(cfg, 0x0, sizeof(git_config)); - - if (git_vector_init(&cfg->files, 3, config_backend_cmp) < 0) { - git__free(cfg); - return -1; - } - - *out = cfg; - GIT_REFCOUNT_INC(cfg); - return 0; -} - -int git_config_add_file_ondisk( - git_config *cfg, - const char *path, - unsigned int level, - int force) -{ - git_config_backend *file = NULL; - int res; - - assert(cfg && path); - - if (!git_path_isfile(path)) { - giterr_set(GITERR_CONFIG, "Cannot find config file '%s'", path); - return GIT_ENOTFOUND; - } - - if (git_config_file__ondisk(&file, path) < 0) - return -1; - - if ((res = git_config_add_backend(cfg, file, level, force)) < 0) { - /* - * free manually; the file is not owned by the config - * instance yet and will not be freed on cleanup - */ - file->free(file); - return res; - } - - return 0; -} - -int git_config_open_ondisk(git_config **out, const char *path) -{ - int error; - git_config *config; - - *out = NULL; - - if (git_config_new(&config) < 0) - return -1; - - if ((error = git_config_add_file_ondisk(config, path, GIT_CONFIG_LEVEL_LOCAL, 0)) < 0) - git_config_free(config); - else - *out = config; - - return error; -} - -static int find_internal_file_by_level( - file_internal **internal_out, - const git_config *cfg, - int level) -{ - int pos = -1; - file_internal *internal; - unsigned int i; - - /* when passing GIT_CONFIG_HIGHEST_LEVEL, the idea is to get the config file - * which has the highest level. As config files are stored in a vector - * sorted by decreasing order of level, getting the file at position 0 - * will do the job. - */ - if (level == GIT_CONFIG_HIGHEST_LEVEL) { - pos = 0; - } else { - git_vector_foreach(&cfg->files, i, internal) { - if (internal->level == (unsigned int)level) - pos = i; - } - } - - if (pos == -1) { - giterr_set(GITERR_CONFIG, - "No config file exists for the given level '%i'", level); - return GIT_ENOTFOUND; - } - - *internal_out = git_vector_get(&cfg->files, pos); - - return 0; -} - -static int duplicate_level(void **old_raw, void *new_raw) -{ - file_internal **old = (file_internal **)old_raw; - - GIT_UNUSED(new_raw); - - giterr_set(GITERR_CONFIG, "A file with the same level (%i) has already been added to the config", (*old)->level); - return GIT_EEXISTS; -} - -static void try_remove_existing_file_internal( - git_config *cfg, - unsigned int level) -{ - int pos = -1; - file_internal *internal; - unsigned int i; - - git_vector_foreach(&cfg->files, i, internal) { - if (internal->level == level) - pos = i; - } - - if (pos == -1) - return; - - internal = git_vector_get(&cfg->files, pos); - - if (git_vector_remove(&cfg->files, pos) < 0) - return; - - GIT_REFCOUNT_DEC(internal, file_internal_free); -} - -static int git_config__add_internal( - git_config *cfg, - file_internal *internal, - unsigned int level, - int force) -{ - int result; - - /* delete existing config file for level if it exists */ - if (force) - try_remove_existing_file_internal(cfg, level); - - if ((result = git_vector_insert_sorted(&cfg->files, - internal, &duplicate_level)) < 0) - return result; - - git_vector_sort(&cfg->files); - internal->file->cfg = cfg; - - GIT_REFCOUNT_INC(internal); - - return 0; -} - -int git_config_open_level( - git_config **cfg_out, - const git_config *cfg_parent, - unsigned int level) -{ - git_config *cfg; - file_internal *internal; - int res; - - if ((res = find_internal_file_by_level(&internal, cfg_parent, level)) < 0) - return res; - - if ((res = git_config_new(&cfg)) < 0) - return res; - - if ((res = git_config__add_internal(cfg, internal, level, true)) < 0) { - git_config_free(cfg); - return res; - } - - *cfg_out = cfg; - - return 0; -} - -int git_config_add_backend( - git_config *cfg, - git_config_backend *file, - unsigned int level, - int force) -{ - file_internal *internal; - int result; - - assert(cfg && file); - - GITERR_CHECK_VERSION(file, GIT_CONFIG_BACKEND_VERSION, "git_config_backend"); - - if ((result = file->open(file, level)) < 0) - return result; - - internal = git__malloc(sizeof(file_internal)); - GITERR_CHECK_ALLOC(internal); - - memset(internal, 0x0, sizeof(file_internal)); - - internal->file = file; - internal->level = level; - - if ((result = git_config__add_internal(cfg, internal, level, force)) < 0) { - git__free(internal); - return result; - } - - return 0; -} - -int git_config_refresh(git_config *cfg) -{ - int error = 0; - unsigned int i; - - for (i = 0; i < cfg->files.length && !error; ++i) { - file_internal *internal = git_vector_get(&cfg->files, i); - git_config_backend *file = internal->file; - error = file->refresh(file); - } - - return error; -} - -/* - * Loop over all the variables - */ - -int git_config_foreach( - const git_config *cfg, git_config_foreach_cb cb, void *payload) -{ - return git_config_foreach_match(cfg, NULL, cb, payload); -} - -int git_config_foreach_match( - const git_config *cfg, - const char *regexp, - git_config_foreach_cb cb, - void *payload) -{ - int ret = 0; - unsigned int i; - file_internal *internal; - git_config_backend *file; - - for (i = 0; i < cfg->files.length && ret == 0; ++i) { - internal = git_vector_get(&cfg->files, i); - file = internal->file; - ret = file->foreach(file, regexp, cb, payload); - } - - return ret; -} - -int git_config_delete_entry(git_config *cfg, const char *name) -{ - git_config_backend *file; - file_internal *internal; - - internal = git_vector_get(&cfg->files, 0); - file = internal->file; - - return file->del(file, name); -} - -/************** - * Setters - **************/ - -int git_config_set_int64(git_config *cfg, const char *name, int64_t value) -{ - char str_value[32]; /* All numbers should fit in here */ - p_snprintf(str_value, sizeof(str_value), "%" PRId64, value); - return git_config_set_string(cfg, name, str_value); -} - -int git_config_set_int32(git_config *cfg, const char *name, int32_t value) -{ - return git_config_set_int64(cfg, name, (int64_t)value); -} - -int git_config_set_bool(git_config *cfg, const char *name, int value) -{ - return git_config_set_string(cfg, name, value ? "true" : "false"); -} - -int git_config_set_string(git_config *cfg, const char *name, const char *value) -{ - git_config_backend *file; - file_internal *internal; - - internal = git_vector_get(&cfg->files, 0); - file = internal->file; - - return file->set(file, name, value); -} - -/*********** - * Getters - ***********/ -int git_config_get_mapped( - int *out, - const git_config *cfg, - const char *name, - const git_cvar_map *maps, - size_t map_n) -{ - const char *value; - int ret; - - if ((ret = git_config_get_string(&value, cfg, name)) < 0) - return ret; - - return git_config_lookup_map_value(out, maps, map_n, value); -} - -int git_config_get_int64(int64_t *out, const git_config *cfg, const char *name) -{ - const char *value; - int ret; - - if ((ret = git_config_get_string(&value, cfg, name)) < 0) - return ret; - - return git_config_parse_int64(out, value); -} - -int git_config_get_int32(int32_t *out, const git_config *cfg, const char *name) -{ - const char *value; - int ret; - - if ((ret = git_config_get_string(&value, cfg, name)) < 0) - return ret; - - return git_config_parse_int32(out, value); -} - -static int get_string_at_file(const char **out, const git_config_backend *file, const char *name) -{ - const git_config_entry *entry; - int res; - - res = file->get(file, name, &entry); - if (!res) - *out = entry->value; - - return res; -} - -static int get_string(const char **out, const git_config *cfg, const char *name) -{ - file_internal *internal; - unsigned int i; - - assert(cfg->files.length); - - git_vector_foreach(&cfg->files, i, internal) { - int res = get_string_at_file(out, internal->file, name); - - if (res != GIT_ENOTFOUND) - return res; - } - - return GIT_ENOTFOUND; -} - -int git_config_get_bool(int *out, const git_config *cfg, const char *name) -{ - const char *value = NULL; - int ret; - - if ((ret = get_string(&value, cfg, name)) < 0) - return ret; - - return git_config_parse_bool(out, value); -} - -int git_config_get_string(const char **out, const git_config *cfg, const char *name) -{ - int ret; - const char *str = NULL; - - if ((ret = get_string(&str, cfg, name)) < 0) - return ret; - - *out = str == NULL ? "" : str; - return 0; -} - -int git_config_get_entry(const git_config_entry **out, const git_config *cfg, const char *name) -{ - file_internal *internal; - unsigned int i; - - assert(cfg->files.length); - - *out = NULL; - - git_vector_foreach(&cfg->files, i, internal) { - git_config_backend *file = internal->file; - int ret = file->get(file, name, out); - if (ret != GIT_ENOTFOUND) - return ret; - } - - return GIT_ENOTFOUND; -} - -int git_config_get_multivar(const git_config *cfg, const char *name, const char *regexp, - git_config_foreach_cb cb, void *payload) -{ - file_internal *internal; - git_config_backend *file; - int ret = GIT_ENOTFOUND; - size_t i; - - assert(cfg->files.length); - - /* - * This loop runs the "wrong" way 'round because we need to - * look at every value from the most general to most specific - */ - for (i = cfg->files.length; i > 0; --i) { - internal = git_vector_get(&cfg->files, i - 1); - file = internal->file; - ret = file->get_multivar(file, name, regexp, cb, payload); - if (ret < 0 && ret != GIT_ENOTFOUND) - return ret; - } - - return 0; -} - -int git_config_set_multivar(git_config *cfg, const char *name, const char *regexp, const char *value) -{ - git_config_backend *file; - file_internal *internal; - - internal = git_vector_get(&cfg->files, 0); - file = internal->file; - - return file->set_multivar(file, name, regexp, value); -} - -int git_config_find_global_r(git_buf *path) -{ - int error = git_futils_find_global_file(path, GIT_CONFIG_FILENAME); - - return error; -} - -int git_config_find_xdg_r(git_buf *path) -{ - int error = git_futils_find_global_file(path, GIT_CONFIG_FILENAME_ALT); - - return error; -} - -int git_config_find_global(char *global_config_path, size_t length) -{ - git_buf path = GIT_BUF_INIT; - int ret = git_config_find_global_r(&path); - - if (ret < 0) { - git_buf_free(&path); - return ret; - } - - if (path.size >= length) { - git_buf_free(&path); - giterr_set(GITERR_NOMEMORY, - "Path is to long to fit on the given buffer"); - return -1; - } - - git_buf_copy_cstr(global_config_path, length, &path); - git_buf_free(&path); - return 0; -} - -int git_config_find_xdg(char *xdg_config_path, size_t length) -{ - git_buf path = GIT_BUF_INIT; - int ret = git_config_find_xdg_r(&path); - - if (ret < 0) { - git_buf_free(&path); - return ret; - } - - if (path.size >= length) { - git_buf_free(&path); - giterr_set(GITERR_NOMEMORY, - "Path is to long to fit on the given buffer"); - return -1; - } - - git_buf_copy_cstr(xdg_config_path, length, &path); - git_buf_free(&path); - return 0; -} - -int git_config_find_system_r(git_buf *path) -{ - return git_futils_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM); -} - -int git_config_find_system(char *system_config_path, size_t length) -{ - git_buf path = GIT_BUF_INIT; - int ret = git_config_find_system_r(&path); - - if (ret < 0) { - git_buf_free(&path); - return ret; - } - - if (path.size >= length) { - git_buf_free(&path); - giterr_set(GITERR_NOMEMORY, - "Path is to long to fit on the given buffer"); - return -1; - } - - git_buf_copy_cstr(system_config_path, length, &path); - git_buf_free(&path); - return 0; -} - -int git_config_open_default(git_config **out) -{ - int error; - git_config *cfg = NULL; - git_buf buf = GIT_BUF_INIT; - - error = git_config_new(&cfg); - - if (!error && !git_config_find_global_r(&buf)) - error = git_config_add_file_ondisk(cfg, buf.ptr, - GIT_CONFIG_LEVEL_GLOBAL, 0); - - if (!error && !git_config_find_xdg_r(&buf)) - error = git_config_add_file_ondisk(cfg, buf.ptr, - GIT_CONFIG_LEVEL_XDG, 0); - - if (!error && !git_config_find_system_r(&buf)) - error = git_config_add_file_ondisk(cfg, buf.ptr, - GIT_CONFIG_LEVEL_SYSTEM, 0); - - git_buf_free(&buf); - - if (error && cfg) { - git_config_free(cfg); - cfg = NULL; - } - - *out = cfg; - - return error; -} - -/*********** - * Parsers - ***********/ -int git_config_lookup_map_value( - int *out, - const git_cvar_map *maps, - size_t map_n, - const char *value) -{ - size_t i; - - if (!value) - goto fail_parse; - - for (i = 0; i < map_n; ++i) { - const git_cvar_map *m = maps + i; - - switch (m->cvar_type) { - case GIT_CVAR_FALSE: - case GIT_CVAR_TRUE: { - int bool_val; - - if (git__parse_bool(&bool_val, value) == 0 && - bool_val == (int)m->cvar_type) { - *out = m->map_value; - return 0; - } - break; - } - - case GIT_CVAR_INT32: - if (git_config_parse_int32(out, value) == 0) - return 0; - break; - - case GIT_CVAR_STRING: - if (strcasecmp(value, m->str_match) == 0) { - *out = m->map_value; - return 0; - } - break; - } - } - -fail_parse: - giterr_set(GITERR_CONFIG, "Failed to map '%s'", value); - return -1; -} - -int git_config_parse_bool(int *out, const char *value) -{ - if (git__parse_bool(out, value) == 0) - return 0; - - if (git_config_parse_int32(out, value) == 0) { - *out = !!(*out); - return 0; - } - - giterr_set(GITERR_CONFIG, "Failed to parse '%s' as a boolean value", value); - return -1; -} - -int git_config_parse_int64(int64_t *out, const char *value) -{ - const char *num_end; - int64_t num; - - if (git__strtol64(&num, value, &num_end, 0) < 0) - goto fail_parse; - - switch (*num_end) { - case 'g': - case 'G': - num *= 1024; - /* fallthrough */ - - case 'm': - case 'M': - num *= 1024; - /* fallthrough */ - - case 'k': - case 'K': - num *= 1024; - - /* check that that there are no more characters after the - * given modifier suffix */ - if (num_end[1] != '\0') - return -1; - - /* fallthrough */ - - case '\0': - *out = num; - return 0; - - default: - goto fail_parse; - } - -fail_parse: - giterr_set(GITERR_CONFIG, "Failed to parse '%s' as an integer", value); - return -1; -} - -int git_config_parse_int32(int32_t *out, const char *value) -{ - int64_t tmp; - int32_t truncate; - - if (git_config_parse_int64(&tmp, value) < 0) - goto fail_parse; - - truncate = tmp & 0xFFFFFFFF; - if (truncate != tmp) - goto fail_parse; - - *out = truncate; - return 0; - -fail_parse: - giterr_set(GITERR_CONFIG, "Failed to parse '%s' as a 32-bit integer", value); - return -1; -} - -struct rename_data { - git_config *config; - git_buf *name; - size_t old_len; - int actual_error; -}; - -static int rename_config_entries_cb( - const git_config_entry *entry, - void *payload) -{ - int error = 0; - struct rename_data *data = (struct rename_data *)payload; - size_t base_len = git_buf_len(data->name); - - if (base_len > 0 && - !(error = git_buf_puts(data->name, entry->name + data->old_len))) - { - error = git_config_set_string( - data->config, git_buf_cstr(data->name), entry->value); - - git_buf_truncate(data->name, base_len); - } - - if (!error) - error = git_config_delete_entry(data->config, entry->name); - - data->actual_error = error; /* preserve actual error code */ - - return error; -} - -int git_config_rename_section( - git_repository *repo, - const char *old_section_name, - const char *new_section_name) -{ - git_config *config; - git_buf pattern = GIT_BUF_INIT, replace = GIT_BUF_INIT; - int error = 0; - struct rename_data data; - - git_buf_text_puts_escape_regex(&pattern, old_section_name); - - if ((error = git_buf_puts(&pattern, "\\..+")) < 0) - goto cleanup; - - if ((error = git_repository_config__weakptr(&config, repo)) < 0) - goto cleanup; - - data.config = config; - data.name = &replace; - data.old_len = strlen(old_section_name) + 1; - data.actual_error = 0; - - if ((error = git_buf_join(&replace, '.', new_section_name, "")) < 0) - goto cleanup; - - if (new_section_name != NULL && - (error = git_config_file_normalize_section( - replace.ptr, strchr(replace.ptr, '.'))) < 0) - { - giterr_set( - GITERR_CONFIG, "Invalid config section '%s'", new_section_name); - goto cleanup; - } - - error = git_config_foreach_match( - config, git_buf_cstr(&pattern), rename_config_entries_cb, &data); - - if (error == GIT_EUSER) - error = data.actual_error; - -cleanup: - git_buf_free(&pattern); - git_buf_free(&replace); - - return error; -} diff --git a/src/config.h b/src/config.h deleted file mode 100644 index db5ebb3b77a..00000000000 --- a/src/config.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_config_h__ -#define INCLUDE_config_h__ - -#include "git2.h" -#include "git2/config.h" -#include "vector.h" -#include "repository.h" - -#define GIT_CONFIG_FILENAME ".gitconfig" -#define GIT_CONFIG_FILENAME_ALT ".config/git/config" -#define GIT_CONFIG_FILENAME_INREPO "config" -#define GIT_CONFIG_FILENAME_SYSTEM "gitconfig" -#define GIT_CONFIG_FILE_MODE 0666 - -struct git_config { - git_refcount rc; - git_vector files; -}; - -extern int git_config_find_global_r(git_buf *global_config_path); -extern int git_config_find_xdg_r(git_buf *system_config_path); -extern int git_config_find_system_r(git_buf *system_config_path); - -extern int git_config_rename_section( - git_repository *repo, - const char *old_section_name, /* eg "branch.dummy" */ - const char *new_section_name); /* NULL to drop the old section */ - -/** - * Create a configuration file backend for ondisk files - * - * These are the normal `.gitconfig` files that Core Git - * processes. Note that you first have to add this file to a - * configuration object before you can query it for configuration - * variables. - * - * @param out the new backend - * @param path where the config file is located - */ -extern int git_config_file__ondisk(struct git_config_backend **out, const char *path); - -#endif diff --git a/src/config_cache.c b/src/config_cache.c deleted file mode 100644 index 2f36df7d168..00000000000 --- a/src/config_cache.c +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "fileops.h" -#include "config.h" -#include "git2/config.h" -#include "vector.h" -#include "filter.h" -#include "repository.h" - -struct map_data { - const char *cvar_name; - git_cvar_map *maps; - size_t map_count; - int default_value; -}; - -/* - * core.eol - * Sets the line ending type to use in the working directory for - * files that have the text property set. Alternatives are lf, crlf - * and native, which uses the platform's native line ending. The default - * value is native. See gitattributes(5) for more information on - * end-of-line conversion. - */ -static git_cvar_map _cvar_map_eol[] = { - {GIT_CVAR_FALSE, NULL, GIT_EOL_UNSET}, - {GIT_CVAR_STRING, "lf", GIT_EOL_LF}, - {GIT_CVAR_STRING, "crlf", GIT_EOL_CRLF}, - {GIT_CVAR_STRING, "native", GIT_EOL_NATIVE} -}; - -/* - * core.autocrlf - * Setting this variable to "true" is almost the same as setting - * the text attribute to "auto" on all files except that text files are - * not guaranteed to be normalized: files that contain CRLF in the - * repository will not be touched. Use this setting if you want to have - * CRLF line endings in your working directory even though the repository - * does not have normalized line endings. This variable can be set to input, - * in which case no output conversion is performed. - */ -static git_cvar_map _cvar_map_autocrlf[] = { - {GIT_CVAR_FALSE, NULL, GIT_AUTO_CRLF_FALSE}, - {GIT_CVAR_TRUE, NULL, GIT_AUTO_CRLF_TRUE}, - {GIT_CVAR_STRING, "input", GIT_AUTO_CRLF_INPUT} -}; - -static struct map_data _cvar_maps[] = { - {"core.autocrlf", _cvar_map_autocrlf, ARRAY_SIZE(_cvar_map_autocrlf), GIT_AUTO_CRLF_DEFAULT}, - {"core.eol", _cvar_map_eol, ARRAY_SIZE(_cvar_map_eol), GIT_EOL_DEFAULT} -}; - -int git_repository__cvar(int *out, git_repository *repo, git_cvar_cached cvar) -{ - *out = repo->cvar_cache[(int)cvar]; - - if (*out == GIT_CVAR_NOT_CACHED) { - struct map_data *data = &_cvar_maps[(int)cvar]; - git_config *config; - int error; - - error = git_repository_config__weakptr(&config, repo); - if (error < 0) - return error; - - error = git_config_get_mapped(out, - config, data->cvar_name, data->maps, data->map_count); - - if (error == GIT_ENOTFOUND) - *out = data->default_value; - - else if (error < 0) - return error; - - repo->cvar_cache[(int)cvar] = *out; - } - - return 0; -} - -void git_repository__cvar_cache_clear(git_repository *repo) -{ - int i; - - for (i = 0; i < GIT_CVAR_CACHE_MAX; ++i) - repo->cvar_cache[i] = GIT_CVAR_NOT_CACHED; -} - diff --git a/src/config_file.c b/src/config_file.c deleted file mode 100644 index 8b51ab21bec..00000000000 --- a/src/config_file.c +++ /dev/null @@ -1,1480 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "config.h" -#include "fileops.h" -#include "filebuf.h" -#include "buffer.h" -#include "buf_text.h" -#include "git2/config.h" -#include "git2/types.h" -#include "strmap.h" - -#include -#include -#include - -GIT__USE_STRMAP; - -typedef struct cvar_t { - struct cvar_t *next; - git_config_entry *entry; -} cvar_t; - -#define CVAR_LIST_HEAD(list) ((list)->head) - -#define CVAR_LIST_TAIL(list) ((list)->tail) - -#define CVAR_LIST_NEXT(var) ((var)->next) - -#define CVAR_LIST_EMPTY(list) ((list)->head == NULL) - -#define CVAR_LIST_APPEND(list, var) do {\ - if (CVAR_LIST_EMPTY(list)) {\ - CVAR_LIST_HEAD(list) = CVAR_LIST_TAIL(list) = var;\ - } else {\ - CVAR_LIST_NEXT(CVAR_LIST_TAIL(list)) = var;\ - CVAR_LIST_TAIL(list) = var;\ - }\ -} while(0) - -#define CVAR_LIST_REMOVE_HEAD(list) do {\ - CVAR_LIST_HEAD(list) = CVAR_LIST_NEXT(CVAR_LIST_HEAD(list));\ -} while(0) - -#define CVAR_LIST_REMOVE_AFTER(var) do {\ - CVAR_LIST_NEXT(var) = CVAR_LIST_NEXT(CVAR_LIST_NEXT(var));\ -} while(0) - -#define CVAR_LIST_FOREACH(list, iter)\ - for ((iter) = CVAR_LIST_HEAD(list);\ - (iter) != NULL;\ - (iter) = CVAR_LIST_NEXT(iter)) - -/* - * Inspired by the FreeBSD functions - */ -#define CVAR_LIST_FOREACH_SAFE(start, iter, tmp)\ - for ((iter) = CVAR_LIST_HEAD(vars);\ - (iter) && (((tmp) = CVAR_LIST_NEXT(iter) || 1));\ - (iter) = (tmp)) - -typedef struct { - git_config_backend parent; - - git_strmap *values; - - struct { - git_buf buffer; - char *read_ptr; - int line_number; - int eof; - } reader; - - char *file_path; - time_t file_mtime; - size_t file_size; - - unsigned int level; -} diskfile_backend; - -static int config_parse(diskfile_backend *cfg_file, unsigned int level); -static int parse_variable(diskfile_backend *cfg, char **var_name, char **var_value); -static int config_write(diskfile_backend *cfg, const char *key, const regex_t *preg, const char *value); -static char *escape_value(const char *ptr); - -static void set_parse_error(diskfile_backend *backend, int col, const char *error_str) -{ - giterr_set(GITERR_CONFIG, "Failed to parse config file: %s (in %s:%d, column %d)", - error_str, backend->file_path, backend->reader.line_number, col); -} - -static void cvar_free(cvar_t *var) -{ - if (var == NULL) - return; - - git__free((char*)var->entry->name); - git__free((char *)var->entry->value); - git__free(var->entry); - git__free(var); -} - -int git_config_file_normalize_section(char *start, char *end) -{ - char *scan; - - if (start == end) - return GIT_EINVALIDSPEC; - - /* Validate and downcase range */ - for (scan = start; *scan; ++scan) { - if (end && scan >= end) - break; - if (isalnum(*scan)) - *scan = tolower(*scan); - else if (*scan != '-' || scan == start) - return GIT_EINVALIDSPEC; - } - - if (scan == start) - return GIT_EINVALIDSPEC; - - return 0; -} - -/* Take something the user gave us and make it nice for our hash function */ -static int normalize_name(const char *in, char **out) -{ - char *name, *fdot, *ldot; - - assert(in && out); - - name = git__strdup(in); - GITERR_CHECK_ALLOC(name); - - fdot = strchr(name, '.'); - ldot = strrchr(name, '.'); - - if (fdot == NULL || fdot == name || ldot == NULL || !ldot[1]) - goto invalid; - - /* Validate and downcase up to first dot and after last dot */ - if (git_config_file_normalize_section(name, fdot) < 0 || - git_config_file_normalize_section(ldot + 1, NULL) < 0) - goto invalid; - - /* If there is a middle range, make sure it doesn't have newlines */ - while (fdot < ldot) - if (*fdot++ == '\n') - goto invalid; - - *out = name; - return 0; - -invalid: - git__free(name); - giterr_set(GITERR_CONFIG, "Invalid config item name '%s'", in); - return GIT_EINVALIDSPEC; -} - -static void free_vars(git_strmap *values) -{ - cvar_t *var = NULL; - - if (values == NULL) - return; - - git_strmap_foreach_value(values, var, - while (var != NULL) { - cvar_t *next = CVAR_LIST_NEXT(var); - cvar_free(var); - var = next; - }); - - git_strmap_free(values); -} - -static int config_open(git_config_backend *cfg, unsigned int level) -{ - int res; - diskfile_backend *b = (diskfile_backend *)cfg; - - b->level = level; - - b->values = git_strmap_alloc(); - GITERR_CHECK_ALLOC(b->values); - - git_buf_init(&b->reader.buffer, 0); - res = git_futils_readbuffer_updated( - &b->reader.buffer, b->file_path, &b->file_mtime, &b->file_size, NULL); - - /* It's fine if the file doesn't exist */ - if (res == GIT_ENOTFOUND) - return 0; - - if (res < 0 || (res = config_parse(b, level)) < 0) { - free_vars(b->values); - b->values = NULL; - } - - git_buf_free(&b->reader.buffer); - return res; -} - -static int config_refresh(git_config_backend *cfg) -{ - int res, updated = 0; - diskfile_backend *b = (diskfile_backend *)cfg; - git_strmap *old_values; - - res = git_futils_readbuffer_updated( - &b->reader.buffer, b->file_path, &b->file_mtime, &b->file_size, &updated); - if (res < 0 || !updated) - return (res == GIT_ENOTFOUND) ? 0 : res; - - /* need to reload - store old values and prep for reload */ - old_values = b->values; - b->values = git_strmap_alloc(); - GITERR_CHECK_ALLOC(b->values); - - if ((res = config_parse(b, b->level)) < 0) { - free_vars(b->values); - b->values = old_values; - } else { - free_vars(old_values); - } - - git_buf_free(&b->reader.buffer); - return res; -} - -static void backend_free(git_config_backend *_backend) -{ - diskfile_backend *backend = (diskfile_backend *)_backend; - - if (backend == NULL) - return; - - git__free(backend->file_path); - free_vars(backend->values); - git__free(backend); -} - -static int file_foreach( - git_config_backend *backend, - const char *regexp, - int (*fn)(const git_config_entry *, void *), - void *data) -{ - diskfile_backend *b = (diskfile_backend *)backend; - cvar_t *var, *next_var; - const char *key; - regex_t regex; - int result = 0; - - if (!b->values) - return 0; - - if (regexp != NULL) { - if ((result = regcomp(®ex, regexp, REG_EXTENDED)) < 0) { - giterr_set_regex(®ex, result); - regfree(®ex); - return -1; - } - } - - git_strmap_foreach(b->values, key, var, - for (; var != NULL; var = next_var) { - next_var = CVAR_LIST_NEXT(var); - - /* skip non-matching keys if regexp was provided */ - if (regexp && regexec(®ex, key, 0, NULL, 0) != 0) - continue; - - /* abort iterator on non-zero return value */ - if (fn(var->entry, data)) { - giterr_clear(); - result = GIT_EUSER; - goto cleanup; - } - } - ); - -cleanup: - if (regexp != NULL) - regfree(®ex); - - return result; -} - -static int config_set(git_config_backend *cfg, const char *name, const char *value) -{ - cvar_t *var = NULL, *old_var; - diskfile_backend *b = (diskfile_backend *)cfg; - char *key, *esc_value = NULL; - khiter_t pos; - int rval, ret; - - if ((rval = normalize_name(name, &key)) < 0) - return rval; - - /* - * Try to find it in the existing values and update it if it - * only has one value. - */ - pos = git_strmap_lookup_index(b->values, key); - if (git_strmap_valid_index(b->values, pos)) { - cvar_t *existing = git_strmap_value_at(b->values, pos); - char *tmp = NULL; - - git__free(key); - - if (existing->next != NULL) { - giterr_set(GITERR_CONFIG, "Multivar incompatible with simple set"); - return -1; - } - - /* don't update if old and new values already match */ - if ((!existing->entry->value && !value) || - (existing->entry->value && value && !strcmp(existing->entry->value, value))) - return 0; - - if (value) { - tmp = git__strdup(value); - GITERR_CHECK_ALLOC(tmp); - esc_value = escape_value(value); - GITERR_CHECK_ALLOC(esc_value); - } - - git__free((void *)existing->entry->value); - existing->entry->value = tmp; - - ret = config_write(b, existing->entry->name, NULL, esc_value); - - git__free(esc_value); - return ret; - } - - var = git__malloc(sizeof(cvar_t)); - GITERR_CHECK_ALLOC(var); - memset(var, 0x0, sizeof(cvar_t)); - var->entry = git__malloc(sizeof(git_config_entry)); - GITERR_CHECK_ALLOC(var->entry); - memset(var->entry, 0x0, sizeof(git_config_entry)); - - var->entry->name = key; - var->entry->value = NULL; - - if (value) { - var->entry->value = git__strdup(value); - GITERR_CHECK_ALLOC(var->entry->value); - esc_value = escape_value(value); - GITERR_CHECK_ALLOC(esc_value); - } - - if (config_write(b, key, NULL, esc_value) < 0) { - git__free(esc_value); - cvar_free(var); - return -1; - } - - git__free(esc_value); - git_strmap_insert2(b->values, key, var, old_var, rval); - if (rval < 0) - return -1; - if (old_var != NULL) - cvar_free(old_var); - - return 0; -} - -/* - * Internal function that actually gets the value in string form - */ -static int config_get(const git_config_backend *cfg, const char *name, const git_config_entry **out) -{ - diskfile_backend *b = (diskfile_backend *)cfg; - char *key; - khiter_t pos; - int error; - - if ((error = normalize_name(name, &key)) < 0) - return error; - - pos = git_strmap_lookup_index(b->values, key); - git__free(key); - - /* no error message; the config system will write one */ - if (!git_strmap_valid_index(b->values, pos)) - return GIT_ENOTFOUND; - - *out = ((cvar_t *)git_strmap_value_at(b->values, pos))->entry; - - return 0; -} - -static int config_get_multivar( - git_config_backend *cfg, - const char *name, - const char *regex_str, - int (*fn)(const git_config_entry *, void *), - void *data) -{ - cvar_t *var; - diskfile_backend *b = (diskfile_backend *)cfg; - char *key; - khiter_t pos; - int error; - - if ((error = normalize_name(name, &key)) < 0) - return error; - - pos = git_strmap_lookup_index(b->values, key); - git__free(key); - - if (!git_strmap_valid_index(b->values, pos)) - return GIT_ENOTFOUND; - - var = git_strmap_value_at(b->values, pos); - - if (regex_str != NULL) { - regex_t regex; - int result; - - /* regex matching; build the regex */ - result = regcomp(®ex, regex_str, REG_EXTENDED); - if (result < 0) { - giterr_set_regex(®ex, result); - regfree(®ex); - return -1; - } - - /* and throw the callback only on the variables that - * match the regex */ - do { - if (regexec(®ex, var->entry->value, 0, NULL, 0) == 0) { - /* early termination by the user is not an error; - * just break and return successfully */ - if (fn(var->entry, data) < 0) - break; - } - - var = var->next; - } while (var != NULL); - regfree(®ex); - } else { - /* no regex; go through all the variables */ - do { - /* early termination by the user is not an error; - * just break and return successfully */ - if (fn(var->entry, data) < 0) - break; - - var = var->next; - } while (var != NULL); - } - - return 0; -} - -static int config_set_multivar( - git_config_backend *cfg, const char *name, const char *regexp, const char *value) -{ - int replaced = 0; - cvar_t *var, *newvar; - diskfile_backend *b = (diskfile_backend *)cfg; - char *key; - regex_t preg; - int result; - khiter_t pos; - - assert(regexp); - - if ((result = normalize_name(name, &key)) < 0) - return result; - - pos = git_strmap_lookup_index(b->values, key); - if (!git_strmap_valid_index(b->values, pos)) { - git__free(key); - return GIT_ENOTFOUND; - } - - var = git_strmap_value_at(b->values, pos); - - result = regcomp(&preg, regexp, REG_EXTENDED); - if (result < 0) { - git__free(key); - giterr_set_regex(&preg, result); - regfree(&preg); - return -1; - } - - for (;;) { - if (regexec(&preg, var->entry->value, 0, NULL, 0) == 0) { - char *tmp = git__strdup(value); - GITERR_CHECK_ALLOC(tmp); - - git__free((void *)var->entry->value); - var->entry->value = tmp; - replaced = 1; - } - - if (var->next == NULL) - break; - - var = var->next; - } - - /* If we've reached the end of the variables and we haven't found it yet, we need to append it */ - if (!replaced) { - newvar = git__malloc(sizeof(cvar_t)); - GITERR_CHECK_ALLOC(newvar); - memset(newvar, 0x0, sizeof(cvar_t)); - newvar->entry = git__malloc(sizeof(git_config_entry)); - GITERR_CHECK_ALLOC(newvar->entry); - memset(newvar->entry, 0x0, sizeof(git_config_entry)); - - newvar->entry->name = git__strdup(var->entry->name); - GITERR_CHECK_ALLOC(newvar->entry->name); - - newvar->entry->value = git__strdup(value); - GITERR_CHECK_ALLOC(newvar->entry->value); - - newvar->entry->level = var->entry->level; - - var->next = newvar; - } - - result = config_write(b, key, &preg, value); - - git__free(key); - regfree(&preg); - - return result; -} - -static int config_delete(git_config_backend *cfg, const char *name) -{ - cvar_t *var; - diskfile_backend *b = (diskfile_backend *)cfg; - char *key; - int result; - khiter_t pos; - - if ((result = normalize_name(name, &key)) < 0) - return result; - - pos = git_strmap_lookup_index(b->values, key); - git__free(key); - - if (!git_strmap_valid_index(b->values, pos)) { - giterr_set(GITERR_CONFIG, "Could not find key '%s' to delete", name); - return GIT_ENOTFOUND; - } - - var = git_strmap_value_at(b->values, pos); - - if (var->next != NULL) { - giterr_set(GITERR_CONFIG, "Cannot delete multivar with a single delete"); - return -1; - } - - git_strmap_delete_at(b->values, pos); - - result = config_write(b, var->entry->name, NULL, NULL); - - cvar_free(var); - return result; -} - -int git_config_file__ondisk(git_config_backend **out, const char *path) -{ - diskfile_backend *backend; - - backend = git__calloc(1, sizeof(diskfile_backend)); - GITERR_CHECK_ALLOC(backend); - - backend->parent.version = GIT_CONFIG_BACKEND_VERSION; - - backend->file_path = git__strdup(path); - GITERR_CHECK_ALLOC(backend->file_path); - - backend->parent.open = config_open; - backend->parent.get = config_get; - backend->parent.get_multivar = config_get_multivar; - backend->parent.set = config_set; - backend->parent.set_multivar = config_set_multivar; - backend->parent.del = config_delete; - backend->parent.foreach = file_foreach; - backend->parent.refresh = config_refresh; - backend->parent.free = backend_free; - - *out = (git_config_backend *)backend; - - return 0; -} - -static int cfg_getchar_raw(diskfile_backend *cfg) -{ - int c; - - c = *cfg->reader.read_ptr++; - - /* - Win 32 line breaks: if we find a \r\n sequence, - return only the \n as a newline - */ - if (c == '\r' && *cfg->reader.read_ptr == '\n') { - cfg->reader.read_ptr++; - c = '\n'; - } - - if (c == '\n') - cfg->reader.line_number++; - - if (c == 0) { - cfg->reader.eof = 1; - c = '\n'; - } - - return c; -} - -#define SKIP_WHITESPACE (1 << 1) -#define SKIP_COMMENTS (1 << 2) - -static int cfg_getchar(diskfile_backend *cfg_file, int flags) -{ - const int skip_whitespace = (flags & SKIP_WHITESPACE); - const int skip_comments = (flags & SKIP_COMMENTS); - int c; - - assert(cfg_file->reader.read_ptr); - - do c = cfg_getchar_raw(cfg_file); - while (skip_whitespace && git__isspace(c) && - !cfg_file->reader.eof); - - if (skip_comments && (c == '#' || c == ';')) { - do c = cfg_getchar_raw(cfg_file); - while (c != '\n'); - } - - return c; -} - -/* - * Read the next char, but don't move the reading pointer. - */ -static int cfg_peek(diskfile_backend *cfg, int flags) -{ - void *old_read_ptr; - int old_lineno, old_eof; - int ret; - - assert(cfg->reader.read_ptr); - - old_read_ptr = cfg->reader.read_ptr; - old_lineno = cfg->reader.line_number; - old_eof = cfg->reader.eof; - - ret = cfg_getchar(cfg, flags); - - cfg->reader.read_ptr = old_read_ptr; - cfg->reader.line_number = old_lineno; - cfg->reader.eof = old_eof; - - return ret; -} - -/* - * Read and consume a line, returning it in newly-allocated memory. - */ -static char *cfg_readline(diskfile_backend *cfg, bool skip_whitespace) -{ - char *line = NULL; - char *line_src, *line_end; - size_t line_len; - - line_src = cfg->reader.read_ptr; - - if (skip_whitespace) { - /* Skip empty empty lines */ - while (git__isspace(*line_src)) - ++line_src; - } - - line_end = strchr(line_src, '\n'); - - /* no newline at EOF */ - if (line_end == NULL) - line_end = strchr(line_src, 0); - - line_len = line_end - line_src; - - line = git__malloc(line_len + 1); - if (line == NULL) - return NULL; - - memcpy(line, line_src, line_len); - - do line[line_len] = '\0'; - while (line_len-- > 0 && git__isspace(line[line_len])); - - if (*line_end == '\n') - line_end++; - - if (*line_end == '\0') - cfg->reader.eof = 1; - - cfg->reader.line_number++; - cfg->reader.read_ptr = line_end; - - return line; -} - -/* - * Consume a line, without storing it anywhere - */ -static void cfg_consume_line(diskfile_backend *cfg) -{ - char *line_start, *line_end; - - line_start = cfg->reader.read_ptr; - line_end = strchr(line_start, '\n'); - /* No newline at EOF */ - if(line_end == NULL){ - line_end = strchr(line_start, '\0'); - } - - if (*line_end == '\n') - line_end++; - - if (*line_end == '\0') - cfg->reader.eof = 1; - - cfg->reader.line_number++; - cfg->reader.read_ptr = line_end; -} - -GIT_INLINE(int) config_keychar(int c) -{ - return isalnum(c) || c == '-'; -} - -static int parse_section_header_ext(diskfile_backend *cfg, const char *line, const char *base_name, char **section_name) -{ - int c, rpos; - char *first_quote, *last_quote; - git_buf buf = GIT_BUF_INIT; - int quote_marks; - /* - * base_name is what came before the space. We should be at the - * first quotation mark, except for now, line isn't being kept in - * sync so we only really use it to calculate the length. - */ - - first_quote = strchr(line, '"'); - last_quote = strrchr(line, '"'); - - if (last_quote - first_quote == 0) { - set_parse_error(cfg, 0, "Missing closing quotation mark in section header"); - return -1; - } - - git_buf_grow(&buf, strlen(base_name) + last_quote - first_quote + 2); - git_buf_printf(&buf, "%s.", base_name); - - rpos = 0; - quote_marks = 0; - - line = first_quote; - c = line[rpos++]; - - /* - * At the end of each iteration, whatever is stored in c will be - * added to the string. In case of error, jump to out - */ - do { - if (quote_marks == 2) { - set_parse_error(cfg, rpos, "Unexpected text after closing quotes"); - git_buf_free(&buf); - return -1; - } - - switch (c) { - case '"': - ++quote_marks; - continue; - - case '\\': - c = line[rpos++]; - - switch (c) { - case '"': - case '\\': - break; - - default: - set_parse_error(cfg, rpos, "Unsupported escape sequence"); - git_buf_free(&buf); - return -1; - } - - default: - break; - } - - git_buf_putc(&buf, c); - } while ((c = line[rpos++]) != ']'); - - *section_name = git_buf_detach(&buf); - return 0; -} - -static int parse_section_header(diskfile_backend *cfg, char **section_out) -{ - char *name, *name_end; - int name_length, c, pos; - int result; - char *line; - - line = cfg_readline(cfg, true); - if (line == NULL) - return -1; - - /* find the end of the variable's name */ - name_end = strchr(line, ']'); - if (name_end == NULL) { - git__free(line); - set_parse_error(cfg, 0, "Missing ']' in section header"); - return -1; - } - - name = (char *)git__malloc((size_t)(name_end - line) + 1); - GITERR_CHECK_ALLOC(name); - - name_length = 0; - pos = 0; - - /* Make sure we were given a section header */ - c = line[pos++]; - assert(c == '['); - - c = line[pos++]; - - do { - if (git__isspace(c)){ - name[name_length] = '\0'; - result = parse_section_header_ext(cfg, line, name, section_out); - git__free(line); - git__free(name); - return result; - } - - if (!config_keychar(c) && c != '.') { - set_parse_error(cfg, pos, "Unexpected character in header"); - goto fail_parse; - } - - name[name_length++] = (char) tolower(c); - - } while ((c = line[pos++]) != ']'); - - if (line[pos - 1] != ']') { - set_parse_error(cfg, pos, "Unexpected end of file"); - goto fail_parse; - } - - git__free(line); - - name[name_length] = 0; - *section_out = name; - - return 0; - -fail_parse: - git__free(line); - git__free(name); - return -1; -} - -static int skip_bom(diskfile_backend *cfg) -{ - git_bom_t bom; - int bom_offset = git_buf_text_detect_bom(&bom, - &cfg->reader.buffer, cfg->reader.read_ptr - cfg->reader.buffer.ptr); - - if (bom == GIT_BOM_UTF8) - cfg->reader.read_ptr += bom_offset; - - /* TODO: reference implementation is pretty stupid with BoM */ - - return 0; -} - -/* - (* basic types *) - digit = "0".."9" - integer = digit { digit } - alphabet = "a".."z" + "A" .. "Z" - - section_char = alphabet | "." | "-" - extension_char = (* any character except newline *) - any_char = (* any character *) - variable_char = "alphabet" | "-" - - - (* actual grammar *) - config = { section } - - section = header { definition } - - header = "[" section [subsection | subsection_ext] "]" - - subsection = "." section - subsection_ext = "\"" extension "\"" - - section = section_char { section_char } - extension = extension_char { extension_char } - - definition = variable_name ["=" variable_value] "\n" - - variable_name = variable_char { variable_char } - variable_value = string | boolean | integer - - string = quoted_string | plain_string - quoted_string = "\"" plain_string "\"" - plain_string = { any_char } - - boolean = boolean_true | boolean_false - boolean_true = "yes" | "1" | "true" | "on" - boolean_false = "no" | "0" | "false" | "off" -*/ - -static int strip_comments(char *line, int in_quotes) -{ - int quote_count = in_quotes; - char *ptr; - - for (ptr = line; *ptr; ++ptr) { - if (ptr[0] == '"' && ptr > line && ptr[-1] != '\\') - quote_count++; - - if ((ptr[0] == ';' || ptr[0] == '#') && (quote_count % 2) == 0) { - ptr[0] = '\0'; - break; - } - } - - /* skip any space at the end */ - if (ptr > line && git__isspace(ptr[-1])) { - ptr--; - } - ptr[0] = '\0'; - - return quote_count; -} - -static int config_parse(diskfile_backend *cfg_file, unsigned int level) -{ - int c; - char *current_section = NULL; - char *var_name; - char *var_value; - cvar_t *var, *existing; - git_buf buf = GIT_BUF_INIT; - int result = 0; - khiter_t pos; - - /* Initialize the reading position */ - cfg_file->reader.read_ptr = cfg_file->reader.buffer.ptr; - cfg_file->reader.eof = 0; - - /* If the file is empty, there's nothing for us to do */ - if (*cfg_file->reader.read_ptr == '\0') - return 0; - - skip_bom(cfg_file); - - while (result == 0 && !cfg_file->reader.eof) { - - c = cfg_peek(cfg_file, SKIP_WHITESPACE); - - switch (c) { - case '\n': /* EOF when peeking, set EOF in the reader to exit the loop */ - cfg_file->reader.eof = 1; - break; - - case '[': /* section header, new section begins */ - git__free(current_section); - current_section = NULL; - result = parse_section_header(cfg_file, ¤t_section); - break; - - case ';': - case '#': - cfg_consume_line(cfg_file); - break; - - default: /* assume variable declaration */ - result = parse_variable(cfg_file, &var_name, &var_value); - if (result < 0) - break; - - var = git__malloc(sizeof(cvar_t)); - GITERR_CHECK_ALLOC(var); - memset(var, 0x0, sizeof(cvar_t)); - var->entry = git__malloc(sizeof(git_config_entry)); - GITERR_CHECK_ALLOC(var->entry); - memset(var->entry, 0x0, sizeof(git_config_entry)); - - git__strtolower(var_name); - git_buf_printf(&buf, "%s.%s", current_section, var_name); - git__free(var_name); - - if (git_buf_oom(&buf)) - return -1; - - var->entry->name = git_buf_detach(&buf); - var->entry->value = var_value; - var->entry->level = level; - - /* Add or append the new config option */ - pos = git_strmap_lookup_index(cfg_file->values, var->entry->name); - if (!git_strmap_valid_index(cfg_file->values, pos)) { - git_strmap_insert(cfg_file->values, var->entry->name, var, result); - if (result < 0) - break; - result = 0; - } else { - existing = git_strmap_value_at(cfg_file->values, pos); - while (existing->next != NULL) { - existing = existing->next; - } - existing->next = var; - } - - break; - } - } - - git__free(current_section); - return result; -} - -static int write_section(git_filebuf *file, const char *key) -{ - int result; - const char *dot; - git_buf buf = GIT_BUF_INIT; - - /* All of this just for [section "subsection"] */ - dot = strchr(key, '.'); - git_buf_putc(&buf, '['); - if (dot == NULL) { - git_buf_puts(&buf, key); - } else { - char *escaped; - git_buf_put(&buf, key, dot - key); - escaped = escape_value(dot + 1); - GITERR_CHECK_ALLOC(escaped); - git_buf_printf(&buf, " \"%s\"", escaped); - git__free(escaped); - } - git_buf_puts(&buf, "]\n"); - - if (git_buf_oom(&buf)) - return -1; - - result = git_filebuf_write(file, git_buf_cstr(&buf), buf.size); - git_buf_free(&buf); - - return result; -} - -/* - * This is pretty much the parsing, except we write out anything we don't have - */ -static int config_write(diskfile_backend *cfg, const char *key, const regex_t *preg, const char* value) -{ - int result, c; - int section_matches = 0, last_section_matched = 0, preg_replaced = 0, write_trailer = 0; - const char *pre_end = NULL, *post_start = NULL, *data_start; - char *current_section = NULL, *section, *name, *ldot; - git_filebuf file = GIT_FILEBUF_INIT; - - /* We need to read in our own config file */ - result = git_futils_readbuffer(&cfg->reader.buffer, cfg->file_path); - - /* Initialise the reading position */ - if (result == GIT_ENOTFOUND) { - cfg->reader.read_ptr = NULL; - cfg->reader.eof = 1; - data_start = NULL; - git_buf_clear(&cfg->reader.buffer); - } else if (result == 0) { - cfg->reader.read_ptr = cfg->reader.buffer.ptr; - cfg->reader.eof = 0; - data_start = cfg->reader.read_ptr; - } else { - return -1; /* OS error when reading the file */ - } - - /* Lock the file */ - if (git_filebuf_open(&file, cfg->file_path, 0) < 0) - return -1; - - skip_bom(cfg); - ldot = strrchr(key, '.'); - name = ldot + 1; - section = git__strndup(key, ldot - key); - - while (!cfg->reader.eof) { - c = cfg_peek(cfg, SKIP_WHITESPACE); - - if (c == '\0') { /* We've arrived at the end of the file */ - break; - - } else if (c == '[') { /* section header, new section begins */ - /* - * We set both positions to the current one in case we - * need to add a variable to the end of a section. In that - * case, we want both variables to point just before the - * new section. If we actually want to replace it, the - * default case will take care of updating them. - */ - pre_end = post_start = cfg->reader.read_ptr; - - git__free(current_section); - current_section = NULL; - if (parse_section_header(cfg, ¤t_section) < 0) - goto rewrite_fail; - - /* Keep track of when it stops matching */ - last_section_matched = section_matches; - section_matches = !strcmp(current_section, section); - } - - else if (c == ';' || c == '#') { - cfg_consume_line(cfg); - } - - else { - /* - * If the section doesn't match, but the last section did, - * it means we need to add a variable (so skip the line - * otherwise). If both the section and name match, we need - * to overwrite the variable (so skip the line - * otherwise). pre_end needs to be updated each time so we - * don't loose that information, but we only need to - * update post_start if we're going to use it in this - * iteration. - */ - if (!section_matches) { - if (!last_section_matched) { - cfg_consume_line(cfg); - continue; - } - } else { - int has_matched = 0; - char *var_name, *var_value; - - pre_end = cfg->reader.read_ptr; - if (parse_variable(cfg, &var_name, &var_value) < 0) - goto rewrite_fail; - - /* First try to match the name of the variable */ - if (strcasecmp(name, var_name) == 0) - has_matched = 1; - - /* If the name matches, and we have a regex to match the - * value, try to match it */ - if (has_matched && preg != NULL) - has_matched = (regexec(preg, var_value, 0, NULL, 0) == 0); - - git__free(var_name); - git__free(var_value); - - /* if there is no match, keep going */ - if (!has_matched) - continue; - - post_start = cfg->reader.read_ptr; - } - - /* We've found the variable we wanted to change, so - * write anything up to it */ - git_filebuf_write(&file, data_start, pre_end - data_start); - preg_replaced = 1; - - /* Then replace the variable. If the value is NULL, it - * means we want to delete it, so don't write anything. */ - if (value != NULL) { - git_filebuf_printf(&file, "\t%s = %s\n", name, value); - } - - /* multiline variable? we need to keep reading lines to match */ - if (preg != NULL) { - data_start = post_start; - continue; - } - - write_trailer = 1; - break; /* break from the loop */ - } - } - - /* - * Being here can mean that - * - * 1) our section is the last one in the file and we're - * adding a variable - * - * 2) we didn't find a section for us so we need to create it - * ourselves. - * - * 3) we're setting a multivar with a regex, which means we - * continue to search for matching values - * - * In the last case, if we've already replaced a value, we - * want to write the rest of the file. Otherwise we need to write - * out the whole file and then the new variable. - */ - if (write_trailer) { - /* Write out rest of the file */ - git_filebuf_write(&file, post_start, cfg->reader.buffer.size - (post_start - data_start)); - } else { - if (preg_replaced) { - git_filebuf_printf(&file, "\n%s", data_start); - } else { - git_filebuf_write(&file, cfg->reader.buffer.ptr, cfg->reader.buffer.size); - - /* And now if we just need to add a variable */ - if (!section_matches && write_section(&file, section) < 0) - goto rewrite_fail; - - /* Sanity check: if we are here, and value is NULL, that means that somebody - * touched the config file after our intial read. We should probably assert() - * this, but instead we'll handle it gracefully with an error. */ - if (value == NULL) { - giterr_set(GITERR_CONFIG, - "Race condition when writing a config file (a cvar has been removed)"); - goto rewrite_fail; - } - - /* If we are here, there is at least a section line */ - if (cfg->reader.buffer.size > 0 && *(cfg->reader.buffer.ptr + cfg->reader.buffer.size - 1) != '\n') - git_filebuf_write(&file, "\n", 1); - - git_filebuf_printf(&file, "\t%s = %s\n", name, value); - } - } - - git__free(section); - git__free(current_section); - - /* refresh stats - if this errors, then commit will error too */ - (void)git_filebuf_stats(&cfg->file_mtime, &cfg->file_size, &file); - - result = git_filebuf_commit(&file, GIT_CONFIG_FILE_MODE); - git_buf_free(&cfg->reader.buffer); - - return result; - -rewrite_fail: - git__free(section); - git__free(current_section); - - git_filebuf_cleanup(&file); - git_buf_free(&cfg->reader.buffer); - return -1; -} - -static const char *escapes = "ntb\"\\"; -static const char *escaped = "\n\t\b\"\\"; - -/* Escape the values to write them to the file */ -static char *escape_value(const char *ptr) -{ - git_buf buf = GIT_BUF_INIT; - size_t len; - const char *esc; - - assert(ptr); - - len = strlen(ptr); - git_buf_grow(&buf, len); - - while (*ptr != '\0') { - if ((esc = strchr(escaped, *ptr)) != NULL) { - git_buf_putc(&buf, '\\'); - git_buf_putc(&buf, escapes[esc - escaped]); - } else { - git_buf_putc(&buf, *ptr); - } - ptr++; - } - - if (git_buf_oom(&buf)) { - git_buf_free(&buf); - return NULL; - } - - return git_buf_detach(&buf); -} - -/* '\"' -> '"' etc */ -static char *fixup_line(const char *ptr, int quote_count) -{ - char *str = git__malloc(strlen(ptr) + 1); - char *out = str, *esc; - - if (str == NULL) - return NULL; - - while (*ptr != '\0') { - if (*ptr == '"') { - quote_count++; - } else if (*ptr != '\\') { - *out++ = *ptr; - } else { - /* backslash, check the next char */ - ptr++; - /* if we're at the end, it's a multiline, so keep the backslash */ - if (*ptr == '\0') { - *out++ = '\\'; - goto out; - } - if ((esc = strchr(escapes, *ptr)) != NULL) { - *out++ = escaped[esc - escapes]; - } else { - git__free(str); - giterr_set(GITERR_CONFIG, "Invalid escape at %s", ptr); - return NULL; - } - } - ptr++; - } - -out: - *out = '\0'; - - return str; -} - -static int is_multiline_var(const char *str) -{ - int count = 0; - const char *end = str + strlen(str); - while (end > str && end[-1] == '\\') { - count++; - end--; - } - - /* An odd number means last backslash wasn't escaped, so it's multiline */ - return (end > str) && (count & 1); -} - -static int parse_multiline_variable(diskfile_backend *cfg, git_buf *value, int in_quotes) -{ - char *line = NULL, *proc_line = NULL; - int quote_count; - - /* Check that the next line exists */ - line = cfg_readline(cfg, false); - if (line == NULL) - return -1; - - /* We've reached the end of the file, there is input missing */ - if (line[0] == '\0') { - set_parse_error(cfg, 0, "Unexpected end of file while parsing multine var"); - git__free(line); - return -1; - } - - quote_count = strip_comments(line, !!in_quotes); - - /* If it was just a comment, pretend it didn't exist */ - if (line[0] == '\0') { - git__free(line); - return parse_multiline_variable(cfg, value, quote_count); - /* TODO: unbounded recursion. This **could** be exploitable */ - } - - /* Drop the continuation character '\': to closely follow the UNIX - * standard, this character **has** to be last one in the buf, with - * no whitespace after it */ - assert(is_multiline_var(value->ptr)); - git_buf_truncate(value, git_buf_len(value) - 1); - - proc_line = fixup_line(line, in_quotes); - if (proc_line == NULL) { - git__free(line); - return -1; - } - /* add this line to the multiline var */ - git_buf_puts(value, proc_line); - git__free(line); - git__free(proc_line); - - /* - * If we need to continue reading the next line, let's just - * keep putting stuff in the buffer - */ - if (is_multiline_var(value->ptr)) - return parse_multiline_variable(cfg, value, quote_count); - - return 0; -} - -static int parse_variable(diskfile_backend *cfg, char **var_name, char **var_value) -{ - const char *var_end = NULL; - const char *value_start = NULL; - char *line; - int quote_count; - - line = cfg_readline(cfg, true); - if (line == NULL) - return -1; - - quote_count = strip_comments(line, 0); - - var_end = strchr(line, '='); - - if (var_end == NULL) - var_end = strchr(line, '\0'); - else - value_start = var_end + 1; - - do var_end--; - while (var_end>line && git__isspace(*var_end)); - - *var_name = git__strndup(line, var_end - line + 1); - GITERR_CHECK_ALLOC(*var_name); - - /* If there is no value, boolean true is assumed */ - *var_value = NULL; - - /* - * Now, let's try to parse the value - */ - if (value_start != NULL) { - while (git__isspace(value_start[0])) - value_start++; - - if (is_multiline_var(value_start)) { - git_buf multi_value = GIT_BUF_INIT; - char *proc_line = fixup_line(value_start, 0); - GITERR_CHECK_ALLOC(proc_line); - git_buf_puts(&multi_value, proc_line); - git__free(proc_line); - if (parse_multiline_variable(cfg, &multi_value, quote_count) < 0 || git_buf_oom(&multi_value)) { - git__free(*var_name); - git__free(line); - git_buf_free(&multi_value); - return -1; - } - - *var_value = git_buf_detach(&multi_value); - - } - else if (value_start[0] != '\0') { - *var_value = fixup_line(value_start, 0); - GITERR_CHECK_ALLOC(*var_value); - } else { /* equals sign but missing rhs */ - *var_value = git__strdup(""); - GITERR_CHECK_ALLOC(*var_value); - } - } - - git__free(line); - return 0; -} diff --git a/src/config_file.h b/src/config_file.h deleted file mode 100644 index 7445859c46a..00000000000 --- a/src/config_file.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_config_file_h__ -#define INCLUDE_config_file_h__ - -#include "git2/config.h" - -GIT_INLINE(int) git_config_file_open(git_config_backend *cfg, unsigned int level) -{ - return cfg->open(cfg, level); -} - -GIT_INLINE(void) git_config_file_free(git_config_backend *cfg) -{ - cfg->free(cfg); -} - -GIT_INLINE(int) git_config_file_get_string( - const git_config_entry **out, git_config_backend *cfg, const char *name) -{ - return cfg->get(cfg, name, out); -} - -GIT_INLINE(int) git_config_file_set_string( - git_config_backend *cfg, const char *name, const char *value) -{ - return cfg->set(cfg, name, value); -} - -GIT_INLINE(int) git_config_file_delete( - git_config_backend *cfg, const char *name) -{ - return cfg->del(cfg, name); -} - -GIT_INLINE(int) git_config_file_foreach( - git_config_backend *cfg, - int (*fn)(const git_config_entry *entry, void *data), - void *data) -{ - return cfg->foreach(cfg, NULL, fn, data); -} - -GIT_INLINE(int) git_config_file_foreach_match( - git_config_backend *cfg, - const char *regexp, - int (*fn)(const git_config_entry *entry, void *data), - void *data) -{ - return cfg->foreach(cfg, regexp, fn, data); -} - -extern int git_config_file_normalize_section(char *start, char *end); - -#endif - diff --git a/src/crlf.c b/src/crlf.c deleted file mode 100644 index 060d39d37cb..00000000000 --- a/src/crlf.c +++ /dev/null @@ -1,314 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "fileops.h" -#include "hash.h" -#include "filter.h" -#include "repository.h" - -#include "git2/attr.h" - -struct crlf_attrs { - int crlf_action; - int eol; -}; - -struct crlf_filter { - git_filter f; - struct crlf_attrs attrs; -}; - -static int check_crlf(const char *value) -{ - if (GIT_ATTR_TRUE(value)) - return GIT_CRLF_TEXT; - - if (GIT_ATTR_FALSE(value)) - return GIT_CRLF_BINARY; - - if (GIT_ATTR_UNSPECIFIED(value)) - return GIT_CRLF_GUESS; - - if (strcmp(value, "input") == 0) - return GIT_CRLF_INPUT; - - if (strcmp(value, "auto") == 0) - return GIT_CRLF_AUTO; - - return GIT_CRLF_GUESS; -} - -static int check_eol(const char *value) -{ - if (GIT_ATTR_UNSPECIFIED(value)) - return GIT_EOL_UNSET; - - if (strcmp(value, "lf") == 0) - return GIT_EOL_LF; - - if (strcmp(value, "crlf") == 0) - return GIT_EOL_CRLF; - - return GIT_EOL_UNSET; -} - -static int crlf_input_action(struct crlf_attrs *ca) -{ - if (ca->crlf_action == GIT_CRLF_BINARY) - return GIT_CRLF_BINARY; - - if (ca->eol == GIT_EOL_LF) - return GIT_CRLF_INPUT; - - if (ca->eol == GIT_EOL_CRLF) - return GIT_CRLF_CRLF; - - return ca->crlf_action; -} - -static int crlf_load_attributes(struct crlf_attrs *ca, git_repository *repo, const char *path) -{ -#define NUM_CONV_ATTRS 3 - - static const char *attr_names[NUM_CONV_ATTRS] = { - "crlf", "eol", "text", - }; - - const char *attr_vals[NUM_CONV_ATTRS]; - int error; - - error = git_attr_get_many(attr_vals, - repo, 0, path, NUM_CONV_ATTRS, attr_names); - - if (error == GIT_ENOTFOUND) { - ca->crlf_action = GIT_CRLF_GUESS; - ca->eol = GIT_EOL_UNSET; - return 0; - } - - if (error == 0) { - ca->crlf_action = check_crlf(attr_vals[2]); /* text */ - if (ca->crlf_action == GIT_CRLF_GUESS) - ca->crlf_action = check_crlf(attr_vals[0]); /* clrf */ - - ca->eol = check_eol(attr_vals[1]); /* eol */ - return 0; - } - - return -1; -} - -static int drop_crlf(git_buf *dest, const git_buf *source) -{ - const char *scan = source->ptr, *next; - const char *scan_end = git_buf_cstr(source) + git_buf_len(source); - - /* Main scan loop. Find the next carriage return and copy the - * whole chunk up to that point to the destination buffer. - */ - while ((next = memchr(scan, '\r', scan_end - scan)) != NULL) { - /* copy input up to \r */ - if (next > scan) - git_buf_put(dest, scan, next - scan); - - /* Do not drop \r unless it is followed by \n */ - if (*(next + 1) != '\n') - git_buf_putc(dest, '\r'); - - scan = next + 1; - } - - /* If there was no \r, then tell the library to skip this filter */ - if (scan == source->ptr) - return -1; - - /* Copy remaining input into dest */ - git_buf_put(dest, scan, scan_end - scan); - return 0; -} - -static int crlf_apply_to_odb(git_filter *self, git_buf *dest, const git_buf *source) -{ - struct crlf_filter *filter = (struct crlf_filter *)self; - - assert(self && dest && source); - - /* Empty file? Nothing to do */ - if (git_buf_len(source) == 0) - return 0; - - /* Heuristics to see if we can skip the conversion. - * Straight from Core Git. - */ - if (filter->attrs.crlf_action == GIT_CRLF_AUTO || - filter->attrs.crlf_action == GIT_CRLF_GUESS) { - - git_buf_text_stats stats; - - /* Check heuristics for binary vs text... */ - if (git_buf_text_gather_stats(&stats, source, false)) - return -1; - - /* - * We're currently not going to even try to convert stuff - * that has bare CR characters. Does anybody do that crazy - * stuff? - */ - if (stats.cr != stats.crlf) - return -1; - -#if 0 - if (crlf_action == CRLF_GUESS) { - /* - * If the file in the index has any CR in it, do not convert. - * This is the new safer autocrlf handling. - */ - if (has_cr_in_index(path)) - return 0; - } -#endif - - if (!stats.cr) - return -1; - } - - /* Actually drop the carriage returns */ - return drop_crlf(dest, source); -} - -static int convert_line_endings(git_buf *dest, const git_buf *source, const char *ending) -{ - const char *scan = git_buf_cstr(source), - *next, - *scan_end = git_buf_cstr(source) + git_buf_len(source); - - while ((next = memchr(scan, '\n', scan_end - scan)) != NULL) { - if (next > scan) - git_buf_put(dest, scan, next-scan); - git_buf_puts(dest, ending); - scan = next + 1; - } - - git_buf_put(dest, scan, scan_end - scan); - return 0; -} - -static const char *line_ending(struct crlf_filter *filter) -{ - switch (filter->attrs.crlf_action) { - case GIT_CRLF_BINARY: - case GIT_CRLF_INPUT: - return "\n"; - - case GIT_CRLF_CRLF: - return "\r\n"; - - case GIT_CRLF_AUTO: - case GIT_CRLF_TEXT: - case GIT_CRLF_GUESS: - break; - - default: - goto line_ending_error; - } - - switch (filter->attrs.eol) { - case GIT_EOL_UNSET: - return GIT_EOL_NATIVE == GIT_EOL_CRLF - ? "\r\n" - : "\n"; - - case GIT_EOL_CRLF: - return "\r\n"; - - case GIT_EOL_LF: - return "\n"; - - default: - goto line_ending_error; - } - -line_ending_error: - giterr_set(GITERR_INVALID, "Invalid input to line ending filter"); - return NULL; -} - -static int crlf_apply_to_workdir(git_filter *self, git_buf *dest, const git_buf *source) -{ - struct crlf_filter *filter = (struct crlf_filter *)self; - const char *workdir_ending = NULL; - - assert (self && dest && source); - - /* Empty file? Nothing to do. */ - if (git_buf_len(source) == 0) - return 0; - - /* Determine proper line ending */ - workdir_ending = line_ending(filter); - if (!workdir_ending) return -1; - - /* If the line ending is '\n', just copy the input */ - if (!strcmp(workdir_ending, "\n")) - return git_buf_puts(dest, git_buf_cstr(source)); - - return convert_line_endings(dest, source, workdir_ending); -} - -static int find_and_add_filter( - git_vector *filters, git_repository *repo, const char *path, - int (*apply)(struct git_filter *self, git_buf *dest, const git_buf *source)) -{ - struct crlf_attrs ca; - struct crlf_filter *filter; - int error; - - /* Load gitattributes for the path */ - if ((error = crlf_load_attributes(&ca, repo, path)) < 0) - return error; - - /* - * Use the core Git logic to see if we should perform CRLF for this file - * based on its attributes & the value of `core.autocrlf` - */ - ca.crlf_action = crlf_input_action(&ca); - - if (ca.crlf_action == GIT_CRLF_BINARY) - return 0; - - if (ca.crlf_action == GIT_CRLF_GUESS) { - int auto_crlf; - - if ((error = git_repository__cvar(&auto_crlf, repo, GIT_CVAR_AUTO_CRLF)) < 0) - return error; - - if (auto_crlf == GIT_AUTO_CRLF_FALSE) - return 0; - } - - /* If we're good, we create a new filter object and push it - * into the filters array */ - filter = git__malloc(sizeof(struct crlf_filter)); - GITERR_CHECK_ALLOC(filter); - - filter->f.apply = apply; - filter->f.do_free = NULL; - memcpy(&filter->attrs, &ca, sizeof(struct crlf_attrs)); - - return git_vector_insert(filters, filter); -} - -int git_filter_add__crlf_to_odb(git_vector *filters, git_repository *repo, const char *path) -{ - return find_and_add_filter(filters, repo, path, &crlf_apply_to_odb); -} - -int git_filter_add__crlf_to_workdir(git_vector *filters, git_repository *repo, const char *path) -{ - return find_and_add_filter(filters, repo, path, &crlf_apply_to_workdir); -} diff --git a/src/date.c b/src/date.c deleted file mode 100644 index bbf88eb44bb..00000000000 --- a/src/date.c +++ /dev/null @@ -1,876 +0,0 @@ -/* - * GIT - The information manager from hell - * - * Copyright (C) Linus Torvalds, 2005 - */ - -#include "common.h" - -#ifndef GIT_WIN32 -#include -#endif - -#include "util.h" -#include "cache.h" -#include "posix.h" - -#include -#include - -typedef enum { - DATE_NORMAL = 0, - DATE_RELATIVE, - DATE_SHORT, - DATE_LOCAL, - DATE_ISO8601, - DATE_RFC2822, - DATE_RAW -} date_mode; - -/* - * This is like mktime, but without normalization of tm_wday and tm_yday. - */ -static git_time_t tm_to_time_t(const struct tm *tm) -{ - static const int mdays[] = { - 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 - }; - int year = tm->tm_year - 70; - int month = tm->tm_mon; - int day = tm->tm_mday; - - if (year < 0 || year > 129) /* algo only works for 1970-2099 */ - return -1; - if (month < 0 || month > 11) /* array bounds */ - return -1; - if (month < 2 || (year + 2) % 4) - day--; - if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0) - return -1; - return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + - tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; -} - -static const char *month_names[] = { - "January", "February", "March", "April", "May", "June", - "July", "August", "September", "October", "November", "December" -}; - -static const char *weekday_names[] = { - "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays" -}; - - - -/* - * Check these. And note how it doesn't do the summer-time conversion. - * - * In my world, it's always summer, and things are probably a bit off - * in other ways too. - */ -static const struct { - const char *name; - int offset; - int dst; -} timezone_names[] = { - { "IDLW", -12, 0, }, /* International Date Line West */ - { "NT", -11, 0, }, /* Nome */ - { "CAT", -10, 0, }, /* Central Alaska */ - { "HST", -10, 0, }, /* Hawaii Standard */ - { "HDT", -10, 1, }, /* Hawaii Daylight */ - { "YST", -9, 0, }, /* Yukon Standard */ - { "YDT", -9, 1, }, /* Yukon Daylight */ - { "PST", -8, 0, }, /* Pacific Standard */ - { "PDT", -8, 1, }, /* Pacific Daylight */ - { "MST", -7, 0, }, /* Mountain Standard */ - { "MDT", -7, 1, }, /* Mountain Daylight */ - { "CST", -6, 0, }, /* Central Standard */ - { "CDT", -6, 1, }, /* Central Daylight */ - { "EST", -5, 0, }, /* Eastern Standard */ - { "EDT", -5, 1, }, /* Eastern Daylight */ - { "AST", -3, 0, }, /* Atlantic Standard */ - { "ADT", -3, 1, }, /* Atlantic Daylight */ - { "WAT", -1, 0, }, /* West Africa */ - - { "GMT", 0, 0, }, /* Greenwich Mean */ - { "UTC", 0, 0, }, /* Universal (Coordinated) */ - { "Z", 0, 0, }, /* Zulu, alias for UTC */ - - { "WET", 0, 0, }, /* Western European */ - { "BST", 0, 1, }, /* British Summer */ - { "CET", +1, 0, }, /* Central European */ - { "MET", +1, 0, }, /* Middle European */ - { "MEWT", +1, 0, }, /* Middle European Winter */ - { "MEST", +1, 1, }, /* Middle European Summer */ - { "CEST", +1, 1, }, /* Central European Summer */ - { "MESZ", +1, 1, }, /* Middle European Summer */ - { "FWT", +1, 0, }, /* French Winter */ - { "FST", +1, 1, }, /* French Summer */ - { "EET", +2, 0, }, /* Eastern Europe */ - { "EEST", +2, 1, }, /* Eastern European Daylight */ - { "WAST", +7, 0, }, /* West Australian Standard */ - { "WADT", +7, 1, }, /* West Australian Daylight */ - { "CCT", +8, 0, }, /* China Coast */ - { "JST", +9, 0, }, /* Japan Standard */ - { "EAST", +10, 0, }, /* Eastern Australian Standard */ - { "EADT", +10, 1, }, /* Eastern Australian Daylight */ - { "GST", +10, 0, }, /* Guam Standard */ - { "NZT", +12, 0, }, /* New Zealand */ - { "NZST", +12, 0, }, /* New Zealand Standard */ - { "NZDT", +12, 1, }, /* New Zealand Daylight */ - { "IDLE", +12, 0, }, /* International Date Line East */ -}; - -static size_t match_string(const char *date, const char *str) -{ - size_t i = 0; - - for (i = 0; *date; date++, str++, i++) { - if (*date == *str) - continue; - if (toupper(*date) == toupper(*str)) - continue; - if (!isalnum(*date)) - break; - return 0; - } - return i; -} - -static int skip_alpha(const char *date) -{ - int i = 0; - do { - i++; - } while (isalpha(date[i])); - return i; -} - -/* -* Parse month, weekday, or timezone name -*/ -static size_t match_alpha(const char *date, struct tm *tm, int *offset) -{ - unsigned int i; - - for (i = 0; i < 12; i++) { - size_t match = match_string(date, month_names[i]); - if (match >= 3) { - tm->tm_mon = i; - return match; - } - } - - for (i = 0; i < 7; i++) { - size_t match = match_string(date, weekday_names[i]); - if (match >= 3) { - tm->tm_wday = i; - return match; - } - } - - for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { - size_t match = match_string(date, timezone_names[i].name); - if (match >= 3 || match == strlen(timezone_names[i].name)) { - int off = timezone_names[i].offset; - - /* This is bogus, but we like summer */ - off += timezone_names[i].dst; - - /* Only use the tz name offset if we don't have anything better */ - if (*offset == -1) - *offset = 60*off; - - return match; - } - } - - if (match_string(date, "PM") == 2) { - tm->tm_hour = (tm->tm_hour % 12) + 12; - return 2; - } - - if (match_string(date, "AM") == 2) { - tm->tm_hour = (tm->tm_hour % 12) + 0; - return 2; - } - - /* BAD */ - return skip_alpha(date); -} - -static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) -{ - if (month > 0 && month < 13 && day > 0 && day < 32) { - struct tm check = *tm; - struct tm *r = (now_tm ? &check : tm); - time_t specified; - - r->tm_mon = month - 1; - r->tm_mday = day; - if (year == -1) { - if (!now_tm) - return 1; - r->tm_year = now_tm->tm_year; - } - else if (year >= 1970 && year < 2100) - r->tm_year = year - 1900; - else if (year > 70 && year < 100) - r->tm_year = year; - else if (year < 38) - r->tm_year = year + 100; - else - return 0; - if (!now_tm) - return 1; - - specified = tm_to_time_t(r); - - /* Be it commit time or author time, it does not make - * sense to specify timestamp way into the future. Make - * sure it is not later than ten days from now... - */ - if (now + 10*24*3600 < specified) - return 0; - tm->tm_mon = r->tm_mon; - tm->tm_mday = r->tm_mday; - if (year != -1) - tm->tm_year = r->tm_year; - return 1; - } - return 0; -} - -static size_t match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm) -{ - time_t now; - struct tm now_tm; - struct tm *refuse_future; - long num2, num3; - - num2 = strtol(end+1, &end, 10); - num3 = -1; - if (*end == c && isdigit(end[1])) - num3 = strtol(end+1, &end, 10); - - /* Time? Date? */ - switch (c) { - case ':': - if (num3 < 0) - num3 = 0; - if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { - tm->tm_hour = num; - tm->tm_min = num2; - tm->tm_sec = num3; - break; - } - return 0; - - case '-': - case '/': - case '.': - now = time(NULL); - refuse_future = NULL; - if (p_gmtime_r(&now, &now_tm)) - refuse_future = &now_tm; - - if (num > 70) { - /* yyyy-mm-dd? */ - if (is_date(num, num2, num3, refuse_future, now, tm)) - break; - /* yyyy-dd-mm? */ - if (is_date(num, num3, num2, refuse_future, now, tm)) - break; - } - /* Our eastern European friends say dd.mm.yy[yy] - * is the norm there, so giving precedence to - * mm/dd/yy[yy] form only when separator is not '.' - */ - if (c != '.' && - is_date(num3, num, num2, refuse_future, now, tm)) - break; - /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ - if (is_date(num3, num2, num, refuse_future, now, tm)) - break; - /* Funny European mm.dd.yy */ - if (c == '.' && - is_date(num3, num, num2, refuse_future, now, tm)) - break; - return 0; - } - return end - date; -} - -/* - * Have we filled in any part of the time/date yet? - * We just do a binary 'and' to see if the sign bit - * is set in all the values. - */ -static int nodate(struct tm *tm) -{ - return (tm->tm_year & - tm->tm_mon & - tm->tm_mday & - tm->tm_hour & - tm->tm_min & - tm->tm_sec) < 0; -} - -/* - * We've seen a digit. Time? Year? Date? - */ -static size_t match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt) -{ - size_t n; - char *end; - unsigned long num; - - num = strtoul(date, &end, 10); - - /* - * Seconds since 1970? We trigger on that for any numbers with - * more than 8 digits. This is because we don't want to rule out - * numbers like 20070606 as a YYYYMMDD date. - */ - if (num >= 100000000 && nodate(tm)) { - time_t time = num; - if (p_gmtime_r(&time, tm)) { - *tm_gmt = 1; - return end - date; - } - } - - /* - * Check for special formats: num[-.:/]num[same]num - */ - switch (*end) { - case ':': - case '.': - case '/': - case '-': - if (isdigit(end[1])) { - size_t match = match_multi_number(num, *end, date, end, tm); - if (match) - return match; - } - } - - /* - * None of the special formats? Try to guess what - * the number meant. We use the number of digits - * to make a more educated guess.. - */ - n = 0; - do { - n++; - } while (isdigit(date[n])); - - /* Four-digit year or a timezone? */ - if (n == 4) { - if (num <= 1400 && *offset == -1) { - unsigned int minutes = num % 100; - unsigned int hours = num / 100; - *offset = hours*60 + minutes; - } else if (num > 1900 && num < 2100) - tm->tm_year = num - 1900; - return n; - } - - /* - * Ignore lots of numerals. We took care of 4-digit years above. - * Days or months must be one or two digits. - */ - if (n > 2) - return n; - - /* - * NOTE! We will give precedence to day-of-month over month or - * year numbers in the 1-12 range. So 05 is always "mday 5", - * unless we already have a mday.. - * - * IOW, 01 Apr 05 parses as "April 1st, 2005". - */ - if (num > 0 && num < 32 && tm->tm_mday < 0) { - tm->tm_mday = num; - return n; - } - - /* Two-digit year? */ - if (n == 2 && tm->tm_year < 0) { - if (num < 10 && tm->tm_mday >= 0) { - tm->tm_year = num + 100; - return n; - } - if (num >= 70) { - tm->tm_year = num; - return n; - } - } - - if (num > 0 && num < 13 && tm->tm_mon < 0) - tm->tm_mon = num-1; - - return n; -} - -static size_t match_tz(const char *date, int *offp) -{ - char *end; - int hour = strtoul(date + 1, &end, 10); - size_t n = end - (date + 1); - int min = 0; - - if (n == 4) { - /* hhmm */ - min = hour % 100; - hour = hour / 100; - } else if (n != 2) { - min = 99; /* random stuff */ - } else if (*end == ':') { - /* hh:mm? */ - min = strtoul(end + 1, &end, 10); - if (end - (date + 1) != 5) - min = 99; /* random stuff */ - } /* otherwise we parsed "hh" */ - - /* - * Don't accept any random stuff. Even though some places have - * offset larger than 12 hours (e.g. Pacific/Kiritimati is at - * UTC+14), there is something wrong if hour part is much - * larger than that. We might also want to check that the - * minutes are divisible by 15 or something too. (Offset of - * Kathmandu, Nepal is UTC+5:45) - */ - if (min < 60 && hour < 24) { - int offset = hour * 60 + min; - if (*date == '-') - offset = -offset; - *offp = offset; - } - return end - date; -} - -/* - * Parse a string like "0 +0000" as ancient timestamp near epoch, but - * only when it appears not as part of any other string. - */ -static int match_object_header_date(const char *date, git_time_t *timestamp, int *offset) -{ - char *end; - unsigned long stamp; - int ofs; - - if (*date < '0' || '9' <= *date) - return -1; - stamp = strtoul(date, &end, 10); - if (*end != ' ' || stamp == ULONG_MAX || (end[1] != '+' && end[1] != '-')) - return -1; - date = end + 2; - ofs = strtol(date, &end, 10); - if ((*end != '\0' && (*end != '\n')) || end != date + 4) - return -1; - ofs = (ofs / 100) * 60 + (ofs % 100); - if (date[-1] == '-') - ofs = -ofs; - *timestamp = stamp; - *offset = ofs; - return 0; -} - -/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 - (i.e. English) day/month names, and it doesn't work correctly with %z. */ -static int parse_date_basic(const char *date, git_time_t *timestamp, int *offset) -{ - struct tm tm; - int tm_gmt; - git_time_t dummy_timestamp; - int dummy_offset; - - if (!timestamp) - timestamp = &dummy_timestamp; - if (!offset) - offset = &dummy_offset; - - memset(&tm, 0, sizeof(tm)); - tm.tm_year = -1; - tm.tm_mon = -1; - tm.tm_mday = -1; - tm.tm_isdst = -1; - tm.tm_hour = -1; - tm.tm_min = -1; - tm.tm_sec = -1; - *offset = -1; - tm_gmt = 0; - - if (*date == '@' && - !match_object_header_date(date + 1, timestamp, offset)) - return 0; /* success */ - for (;;) { - size_t match = 0; - unsigned char c = *date; - - /* Stop at end of string or newline */ - if (!c || c == '\n') - break; - - if (isalpha(c)) - match = match_alpha(date, &tm, offset); - else if (isdigit(c)) - match = match_digit(date, &tm, offset, &tm_gmt); - else if ((c == '-' || c == '+') && isdigit(date[1])) - match = match_tz(date, offset); - - if (!match) { - /* BAD */ - match = 1; - } - - date += match; - } - - /* mktime uses local timezone */ - *timestamp = tm_to_time_t(&tm); - if (*offset == -1) - *offset = (int)((time_t)*timestamp - mktime(&tm)) / 60; - - if (*timestamp == (git_time_t)-1) - return -1; - - if (!tm_gmt) - *timestamp -= *offset * 60; - return 0; /* success */ -} - - -/* - * Relative time update (eg "2 days ago"). If we haven't set the time - * yet, we need to set it from current time. - */ -static git_time_t update_tm(struct tm *tm, struct tm *now, unsigned long sec) -{ - time_t n; - - if (tm->tm_mday < 0) - tm->tm_mday = now->tm_mday; - if (tm->tm_mon < 0) - tm->tm_mon = now->tm_mon; - if (tm->tm_year < 0) { - tm->tm_year = now->tm_year; - if (tm->tm_mon > now->tm_mon) - tm->tm_year--; - } - - n = mktime(tm) - sec; - p_localtime_r(&n, tm); - return n; -} - -static void date_now(struct tm *tm, struct tm *now, int *num) -{ - GIT_UNUSED(num); - update_tm(tm, now, 0); -} - -static void date_yesterday(struct tm *tm, struct tm *now, int *num) -{ - GIT_UNUSED(num); - update_tm(tm, now, 24*60*60); -} - -static void date_time(struct tm *tm, struct tm *now, int hour) -{ - if (tm->tm_hour < hour) - date_yesterday(tm, now, NULL); - tm->tm_hour = hour; - tm->tm_min = 0; - tm->tm_sec = 0; -} - -static void date_midnight(struct tm *tm, struct tm *now, int *num) -{ - GIT_UNUSED(num); - date_time(tm, now, 0); -} - -static void date_noon(struct tm *tm, struct tm *now, int *num) -{ - GIT_UNUSED(num); - date_time(tm, now, 12); -} - -static void date_tea(struct tm *tm, struct tm *now, int *num) -{ - GIT_UNUSED(num); - date_time(tm, now, 17); -} - -static void date_pm(struct tm *tm, struct tm *now, int *num) -{ - int hour, n = *num; - *num = 0; - GIT_UNUSED(now); - - hour = tm->tm_hour; - if (n) { - hour = n; - tm->tm_min = 0; - tm->tm_sec = 0; - } - tm->tm_hour = (hour % 12) + 12; -} - -static void date_am(struct tm *tm, struct tm *now, int *num) -{ - int hour, n = *num; - *num = 0; - GIT_UNUSED(now); - - hour = tm->tm_hour; - if (n) { - hour = n; - tm->tm_min = 0; - tm->tm_sec = 0; - } - tm->tm_hour = (hour % 12); -} - -static void date_never(struct tm *tm, struct tm *now, int *num) -{ - time_t n = 0; - GIT_UNUSED(now); - GIT_UNUSED(num); - p_localtime_r(&n, tm); -} - -static const struct special { - const char *name; - void (*fn)(struct tm *, struct tm *, int *); -} special[] = { - { "yesterday", date_yesterday }, - { "noon", date_noon }, - { "midnight", date_midnight }, - { "tea", date_tea }, - { "PM", date_pm }, - { "AM", date_am }, - { "never", date_never }, - { "now", date_now }, - { NULL } -}; - -static const char *number_name[] = { - "zero", "one", "two", "three", "four", - "five", "six", "seven", "eight", "nine", "ten", -}; - -static const struct typelen { - const char *type; - int length; -} typelen[] = { - { "seconds", 1 }, - { "minutes", 60 }, - { "hours", 60*60 }, - { "days", 24*60*60 }, - { "weeks", 7*24*60*60 }, - { NULL } -}; - -static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched) -{ - const struct typelen *tl; - const struct special *s; - const char *end = date; - int i; - - while (isalpha(*++end)); - ; - - for (i = 0; i < 12; i++) { - size_t match = match_string(date, month_names[i]); - if (match >= 3) { - tm->tm_mon = i; - *touched = 1; - return end; - } - } - - for (s = special; s->name; s++) { - size_t len = strlen(s->name); - if (match_string(date, s->name) == len) { - s->fn(tm, now, num); - *touched = 1; - return end; - } - } - - if (!*num) { - for (i = 1; i < 11; i++) { - size_t len = strlen(number_name[i]); - if (match_string(date, number_name[i]) == len) { - *num = i; - *touched = 1; - return end; - } - } - if (match_string(date, "last") == 4) { - *num = 1; - *touched = 1; - } - return end; - } - - tl = typelen; - while (tl->type) { - size_t len = strlen(tl->type); - if (match_string(date, tl->type) >= len-1) { - update_tm(tm, now, tl->length * *num); - *num = 0; - *touched = 1; - return end; - } - tl++; - } - - for (i = 0; i < 7; i++) { - size_t match = match_string(date, weekday_names[i]); - if (match >= 3) { - int diff, n = *num -1; - *num = 0; - - diff = tm->tm_wday - i; - if (diff <= 0) - n++; - diff += 7*n; - - update_tm(tm, now, diff * 24 * 60 * 60); - *touched = 1; - return end; - } - } - - if (match_string(date, "months") >= 5) { - int n; - update_tm(tm, now, 0); /* fill in date fields if needed */ - n = tm->tm_mon - *num; - *num = 0; - while (n < 0) { - n += 12; - tm->tm_year--; - } - tm->tm_mon = n; - *touched = 1; - return end; - } - - if (match_string(date, "years") >= 4) { - update_tm(tm, now, 0); /* fill in date fields if needed */ - tm->tm_year -= *num; - *num = 0; - *touched = 1; - return end; - } - - return end; -} - -static const char *approxidate_digit(const char *date, struct tm *tm, int *num) -{ - char *end; - unsigned long number = strtoul(date, &end, 10); - - switch (*end) { - case ':': - case '.': - case '/': - case '-': - if (isdigit(end[1])) { - size_t match = match_multi_number(number, *end, date, end, tm); - if (match) - return date + match; - } - } - - /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */ - if (date[0] != '0' || end - date <= 2) - *num = number; - return end; -} - -/* - * Do we have a pending number at the end, or when - * we see a new one? Let's assume it's a month day, - * as in "Dec 6, 1992" - */ -static void pending_number(struct tm *tm, int *num) -{ - int number = *num; - - if (number) { - *num = 0; - if (tm->tm_mday < 0 && number < 32) - tm->tm_mday = number; - else if (tm->tm_mon < 0 && number < 13) - tm->tm_mon = number-1; - else if (tm->tm_year < 0) { - if (number > 1969 && number < 2100) - tm->tm_year = number - 1900; - else if (number > 69 && number < 100) - tm->tm_year = number; - else if (number < 38) - tm->tm_year = 100 + number; - /* We mess up for number = 00 ? */ - } - } -} - -static git_time_t approxidate_str(const char *date, - const struct timeval *tv, - int *error_ret) -{ - int number = 0; - int touched = 0; - struct tm tm = {0}, now; - time_t time_sec; - - time_sec = tv->tv_sec; - p_localtime_r(&time_sec, &tm); - now = tm; - - tm.tm_year = -1; - tm.tm_mon = -1; - tm.tm_mday = -1; - - for (;;) { - unsigned char c = *date; - if (!c) - break; - date++; - if (isdigit(c)) { - pending_number(&tm, &number); - date = approxidate_digit(date-1, &tm, &number); - touched = 1; - continue; - } - if (isalpha(c)) - date = approxidate_alpha(date-1, &tm, &now, &number, &touched); - } - pending_number(&tm, &number); - if (!touched) - *error_ret = 1; - return update_tm(&tm, &now, 0); -} - -int git__date_parse(git_time_t *out, const char *date) -{ - struct timeval tv; - git_time_t timestamp; - int offset, error_ret=0; - - if (!parse_date_basic(date, ×tamp, &offset)) { - *out = timestamp; - return 0; - } - - p_gettimeofday(&tv, NULL); - *out = approxidate_str(date, &tv, &error_ret); - return error_ret; -} diff --git a/src/delta-apply.c b/src/delta-apply.c deleted file mode 100644 index a39c7af5cf0..00000000000 --- a/src/delta-apply.c +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "common.h" -#include "git2/odb.h" -#include "delta-apply.h" - -/* - * This file was heavily cribbed from BinaryDelta.java in JGit, which - * itself was heavily cribbed from patch-delta.c in the - * GIT project. The original delta patching code was written by - * Nicolas Pitre . - */ - -static int hdr_sz( - size_t *size, - const unsigned char **delta, - const unsigned char *end) -{ - const unsigned char *d = *delta; - size_t r = 0; - unsigned int c, shift = 0; - - do { - if (d == end) - return -1; - c = *d++; - r |= (c & 0x7f) << shift; - shift += 7; - } while (c & 0x80); - *delta = d; - *size = r; - return 0; -} - -int git__delta_read_header( - const unsigned char *delta, - size_t delta_len, - size_t *base_sz, - size_t *res_sz) -{ - const unsigned char *delta_end = delta + delta_len; - if ((hdr_sz(base_sz, &delta, delta_end) < 0) || - (hdr_sz(res_sz, &delta, delta_end) < 0)) - return -1; - return 0; -} - -int git__delta_apply( - git_rawobj *out, - const unsigned char *base, - size_t base_len, - const unsigned char *delta, - size_t delta_len) -{ - const unsigned char *delta_end = delta + delta_len; - size_t base_sz, res_sz; - unsigned char *res_dp; - - /* Check that the base size matches the data we were given; - * if not we would underflow while accessing data from the - * base object, resulting in data corruption or segfault. - */ - if ((hdr_sz(&base_sz, &delta, delta_end) < 0) || (base_sz != base_len)) { - giterr_set(GITERR_INVALID, "Failed to apply delta. Base size does not match given data"); - return -1; - } - - if (hdr_sz(&res_sz, &delta, delta_end) < 0) { - giterr_set(GITERR_INVALID, "Failed to apply delta. Base size does not match given data"); - return -1; - } - - res_dp = git__malloc(res_sz + 1); - GITERR_CHECK_ALLOC(res_dp); - - res_dp[res_sz] = '\0'; - out->data = res_dp; - out->len = res_sz; - - while (delta < delta_end) { - unsigned char cmd = *delta++; - if (cmd & 0x80) { - /* cmd is a copy instruction; copy from the base. - */ - size_t off = 0, len = 0; - - if (cmd & 0x01) off = *delta++; - if (cmd & 0x02) off |= *delta++ << 8; - if (cmd & 0x04) off |= *delta++ << 16; - if (cmd & 0x08) off |= *delta++ << 24; - - if (cmd & 0x10) len = *delta++; - if (cmd & 0x20) len |= *delta++ << 8; - if (cmd & 0x40) len |= *delta++ << 16; - if (!len) len = 0x10000; - - if (base_len < off + len || res_sz < len) - goto fail; - memcpy(res_dp, base + off, len); - res_dp += len; - res_sz -= len; - - } else if (cmd) { - /* cmd is a literal insert instruction; copy from - * the delta stream itself. - */ - if (delta_end - delta < cmd || res_sz < cmd) - goto fail; - memcpy(res_dp, delta, cmd); - delta += cmd; - res_dp += cmd; - res_sz -= cmd; - - } else { - /* cmd == 0 is reserved for future encodings. - */ - goto fail; - } - } - - if (delta != delta_end || res_sz) - goto fail; - return 0; - -fail: - git__free(out->data); - out->data = NULL; - giterr_set(GITERR_INVALID, "Failed to apply delta"); - return -1; -} diff --git a/src/delta-apply.h b/src/delta-apply.h deleted file mode 100644 index d7d99d04c2c..00000000000 --- a/src/delta-apply.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_delta_apply_h__ -#define INCLUDE_delta_apply_h__ - -#include "odb.h" - -/** - * Apply a git binary delta to recover the original content. - * - * @param out the output buffer to receive the original data. - * Only out->data and out->len are populated, as this is - * the only information available in the delta. - * @param base the base to copy from during copy instructions. - * @param base_len number of bytes available at base. - * @param delta the delta to execute copy/insert instructions from. - * @param delta_len total number of bytes in the delta. - * @return - * - 0 on a successful delta unpack. - * - GIT_ERROR if the delta is corrupt or doesn't match the base. - */ -extern int git__delta_apply( - git_rawobj *out, - const unsigned char *base, - size_t base_len, - const unsigned char *delta, - size_t delta_len); - -/** - * Read the header of a git binary delta. - * - * @param delta the delta to execute copy/insert instructions from. - * @param delta_len total number of bytes in the delta. - * @param base_sz pointer to store the base size field. - * @param res_sz pointer to store the result size field. - * @return - * - 0 on a successful decoding the header. - * - GIT_ERROR if the delta is corrupt. - */ -extern int git__delta_read_header( - const unsigned char *delta, - size_t delta_len, - size_t *base_sz, - size_t *res_sz); - -#endif diff --git a/src/delta.c b/src/delta.c deleted file mode 100644 index 3252dbf149b..00000000000 --- a/src/delta.c +++ /dev/null @@ -1,424 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "delta.h" - -/* maximum hash entry list for the same hash bucket */ -#define HASH_LIMIT 64 - -#define RABIN_SHIFT 23 -#define RABIN_WINDOW 16 - -static const unsigned int T[256] = { - 0x00000000, 0xab59b4d1, 0x56b369a2, 0xfdeadd73, 0x063f6795, 0xad66d344, - 0x508c0e37, 0xfbd5bae6, 0x0c7ecf2a, 0xa7277bfb, 0x5acda688, 0xf1941259, - 0x0a41a8bf, 0xa1181c6e, 0x5cf2c11d, 0xf7ab75cc, 0x18fd9e54, 0xb3a42a85, - 0x4e4ef7f6, 0xe5174327, 0x1ec2f9c1, 0xb59b4d10, 0x48719063, 0xe32824b2, - 0x1483517e, 0xbfdae5af, 0x423038dc, 0xe9698c0d, 0x12bc36eb, 0xb9e5823a, - 0x440f5f49, 0xef56eb98, 0x31fb3ca8, 0x9aa28879, 0x6748550a, 0xcc11e1db, - 0x37c45b3d, 0x9c9defec, 0x6177329f, 0xca2e864e, 0x3d85f382, 0x96dc4753, - 0x6b369a20, 0xc06f2ef1, 0x3bba9417, 0x90e320c6, 0x6d09fdb5, 0xc6504964, - 0x2906a2fc, 0x825f162d, 0x7fb5cb5e, 0xd4ec7f8f, 0x2f39c569, 0x846071b8, - 0x798aaccb, 0xd2d3181a, 0x25786dd6, 0x8e21d907, 0x73cb0474, 0xd892b0a5, - 0x23470a43, 0x881ebe92, 0x75f463e1, 0xdeadd730, 0x63f67950, 0xc8afcd81, - 0x354510f2, 0x9e1ca423, 0x65c91ec5, 0xce90aa14, 0x337a7767, 0x9823c3b6, - 0x6f88b67a, 0xc4d102ab, 0x393bdfd8, 0x92626b09, 0x69b7d1ef, 0xc2ee653e, - 0x3f04b84d, 0x945d0c9c, 0x7b0be704, 0xd05253d5, 0x2db88ea6, 0x86e13a77, - 0x7d348091, 0xd66d3440, 0x2b87e933, 0x80de5de2, 0x7775282e, 0xdc2c9cff, - 0x21c6418c, 0x8a9ff55d, 0x714a4fbb, 0xda13fb6a, 0x27f92619, 0x8ca092c8, - 0x520d45f8, 0xf954f129, 0x04be2c5a, 0xafe7988b, 0x5432226d, 0xff6b96bc, - 0x02814bcf, 0xa9d8ff1e, 0x5e738ad2, 0xf52a3e03, 0x08c0e370, 0xa39957a1, - 0x584ced47, 0xf3155996, 0x0eff84e5, 0xa5a63034, 0x4af0dbac, 0xe1a96f7d, - 0x1c43b20e, 0xb71a06df, 0x4ccfbc39, 0xe79608e8, 0x1a7cd59b, 0xb125614a, - 0x468e1486, 0xedd7a057, 0x103d7d24, 0xbb64c9f5, 0x40b17313, 0xebe8c7c2, - 0x16021ab1, 0xbd5bae60, 0x6cb54671, 0xc7ecf2a0, 0x3a062fd3, 0x915f9b02, - 0x6a8a21e4, 0xc1d39535, 0x3c394846, 0x9760fc97, 0x60cb895b, 0xcb923d8a, - 0x3678e0f9, 0x9d215428, 0x66f4eece, 0xcdad5a1f, 0x3047876c, 0x9b1e33bd, - 0x7448d825, 0xdf116cf4, 0x22fbb187, 0x89a20556, 0x7277bfb0, 0xd92e0b61, - 0x24c4d612, 0x8f9d62c3, 0x7836170f, 0xd36fa3de, 0x2e857ead, 0x85dcca7c, - 0x7e09709a, 0xd550c44b, 0x28ba1938, 0x83e3ade9, 0x5d4e7ad9, 0xf617ce08, - 0x0bfd137b, 0xa0a4a7aa, 0x5b711d4c, 0xf028a99d, 0x0dc274ee, 0xa69bc03f, - 0x5130b5f3, 0xfa690122, 0x0783dc51, 0xacda6880, 0x570fd266, 0xfc5666b7, - 0x01bcbbc4, 0xaae50f15, 0x45b3e48d, 0xeeea505c, 0x13008d2f, 0xb85939fe, - 0x438c8318, 0xe8d537c9, 0x153feaba, 0xbe665e6b, 0x49cd2ba7, 0xe2949f76, - 0x1f7e4205, 0xb427f6d4, 0x4ff24c32, 0xe4abf8e3, 0x19412590, 0xb2189141, - 0x0f433f21, 0xa41a8bf0, 0x59f05683, 0xf2a9e252, 0x097c58b4, 0xa225ec65, - 0x5fcf3116, 0xf49685c7, 0x033df00b, 0xa86444da, 0x558e99a9, 0xfed72d78, - 0x0502979e, 0xae5b234f, 0x53b1fe3c, 0xf8e84aed, 0x17bea175, 0xbce715a4, - 0x410dc8d7, 0xea547c06, 0x1181c6e0, 0xbad87231, 0x4732af42, 0xec6b1b93, - 0x1bc06e5f, 0xb099da8e, 0x4d7307fd, 0xe62ab32c, 0x1dff09ca, 0xb6a6bd1b, - 0x4b4c6068, 0xe015d4b9, 0x3eb80389, 0x95e1b758, 0x680b6a2b, 0xc352defa, - 0x3887641c, 0x93ded0cd, 0x6e340dbe, 0xc56db96f, 0x32c6cca3, 0x999f7872, - 0x6475a501, 0xcf2c11d0, 0x34f9ab36, 0x9fa01fe7, 0x624ac294, 0xc9137645, - 0x26459ddd, 0x8d1c290c, 0x70f6f47f, 0xdbaf40ae, 0x207afa48, 0x8b234e99, - 0x76c993ea, 0xdd90273b, 0x2a3b52f7, 0x8162e626, 0x7c883b55, 0xd7d18f84, - 0x2c043562, 0x875d81b3, 0x7ab75cc0, 0xd1eee811 -}; - -static const unsigned int U[256] = { - 0x00000000, 0x7eb5200d, 0x5633f4cb, 0x2886d4c6, 0x073e5d47, 0x798b7d4a, - 0x510da98c, 0x2fb88981, 0x0e7cba8e, 0x70c99a83, 0x584f4e45, 0x26fa6e48, - 0x0942e7c9, 0x77f7c7c4, 0x5f711302, 0x21c4330f, 0x1cf9751c, 0x624c5511, - 0x4aca81d7, 0x347fa1da, 0x1bc7285b, 0x65720856, 0x4df4dc90, 0x3341fc9d, - 0x1285cf92, 0x6c30ef9f, 0x44b63b59, 0x3a031b54, 0x15bb92d5, 0x6b0eb2d8, - 0x4388661e, 0x3d3d4613, 0x39f2ea38, 0x4747ca35, 0x6fc11ef3, 0x11743efe, - 0x3eccb77f, 0x40799772, 0x68ff43b4, 0x164a63b9, 0x378e50b6, 0x493b70bb, - 0x61bda47d, 0x1f088470, 0x30b00df1, 0x4e052dfc, 0x6683f93a, 0x1836d937, - 0x250b9f24, 0x5bbebf29, 0x73386bef, 0x0d8d4be2, 0x2235c263, 0x5c80e26e, - 0x740636a8, 0x0ab316a5, 0x2b7725aa, 0x55c205a7, 0x7d44d161, 0x03f1f16c, - 0x2c4978ed, 0x52fc58e0, 0x7a7a8c26, 0x04cfac2b, 0x73e5d470, 0x0d50f47d, - 0x25d620bb, 0x5b6300b6, 0x74db8937, 0x0a6ea93a, 0x22e87dfc, 0x5c5d5df1, - 0x7d996efe, 0x032c4ef3, 0x2baa9a35, 0x551fba38, 0x7aa733b9, 0x041213b4, - 0x2c94c772, 0x5221e77f, 0x6f1ca16c, 0x11a98161, 0x392f55a7, 0x479a75aa, - 0x6822fc2b, 0x1697dc26, 0x3e1108e0, 0x40a428ed, 0x61601be2, 0x1fd53bef, - 0x3753ef29, 0x49e6cf24, 0x665e46a5, 0x18eb66a8, 0x306db26e, 0x4ed89263, - 0x4a173e48, 0x34a21e45, 0x1c24ca83, 0x6291ea8e, 0x4d29630f, 0x339c4302, - 0x1b1a97c4, 0x65afb7c9, 0x446b84c6, 0x3adea4cb, 0x1258700d, 0x6ced5000, - 0x4355d981, 0x3de0f98c, 0x15662d4a, 0x6bd30d47, 0x56ee4b54, 0x285b6b59, - 0x00ddbf9f, 0x7e689f92, 0x51d01613, 0x2f65361e, 0x07e3e2d8, 0x7956c2d5, - 0x5892f1da, 0x2627d1d7, 0x0ea10511, 0x7014251c, 0x5facac9d, 0x21198c90, - 0x099f5856, 0x772a785b, 0x4c921c31, 0x32273c3c, 0x1aa1e8fa, 0x6414c8f7, - 0x4bac4176, 0x3519617b, 0x1d9fb5bd, 0x632a95b0, 0x42eea6bf, 0x3c5b86b2, - 0x14dd5274, 0x6a687279, 0x45d0fbf8, 0x3b65dbf5, 0x13e30f33, 0x6d562f3e, - 0x506b692d, 0x2ede4920, 0x06589de6, 0x78edbdeb, 0x5755346a, 0x29e01467, - 0x0166c0a1, 0x7fd3e0ac, 0x5e17d3a3, 0x20a2f3ae, 0x08242768, 0x76910765, - 0x59298ee4, 0x279caee9, 0x0f1a7a2f, 0x71af5a22, 0x7560f609, 0x0bd5d604, - 0x235302c2, 0x5de622cf, 0x725eab4e, 0x0ceb8b43, 0x246d5f85, 0x5ad87f88, - 0x7b1c4c87, 0x05a96c8a, 0x2d2fb84c, 0x539a9841, 0x7c2211c0, 0x029731cd, - 0x2a11e50b, 0x54a4c506, 0x69998315, 0x172ca318, 0x3faa77de, 0x411f57d3, - 0x6ea7de52, 0x1012fe5f, 0x38942a99, 0x46210a94, 0x67e5399b, 0x19501996, - 0x31d6cd50, 0x4f63ed5d, 0x60db64dc, 0x1e6e44d1, 0x36e89017, 0x485db01a, - 0x3f77c841, 0x41c2e84c, 0x69443c8a, 0x17f11c87, 0x38499506, 0x46fcb50b, - 0x6e7a61cd, 0x10cf41c0, 0x310b72cf, 0x4fbe52c2, 0x67388604, 0x198da609, - 0x36352f88, 0x48800f85, 0x6006db43, 0x1eb3fb4e, 0x238ebd5d, 0x5d3b9d50, - 0x75bd4996, 0x0b08699b, 0x24b0e01a, 0x5a05c017, 0x728314d1, 0x0c3634dc, - 0x2df207d3, 0x534727de, 0x7bc1f318, 0x0574d315, 0x2acc5a94, 0x54797a99, - 0x7cffae5f, 0x024a8e52, 0x06852279, 0x78300274, 0x50b6d6b2, 0x2e03f6bf, - 0x01bb7f3e, 0x7f0e5f33, 0x57888bf5, 0x293dabf8, 0x08f998f7, 0x764cb8fa, - 0x5eca6c3c, 0x207f4c31, 0x0fc7c5b0, 0x7172e5bd, 0x59f4317b, 0x27411176, - 0x1a7c5765, 0x64c97768, 0x4c4fa3ae, 0x32fa83a3, 0x1d420a22, 0x63f72a2f, - 0x4b71fee9, 0x35c4dee4, 0x1400edeb, 0x6ab5cde6, 0x42331920, 0x3c86392d, - 0x133eb0ac, 0x6d8b90a1, 0x450d4467, 0x3bb8646a -}; - -struct index_entry { - const unsigned char *ptr; - unsigned int val; - struct index_entry *next; -}; - -struct git_delta_index { - unsigned long memsize; - const void *src_buf; - unsigned long src_size; - unsigned int hash_mask; - struct index_entry *hash[GIT_FLEX_ARRAY]; -}; - -struct git_delta_index * -git_delta_create_index(const void *buf, unsigned long bufsize) -{ - unsigned int i, hsize, hmask, entries, prev_val, *hash_count; - const unsigned char *data, *buffer = buf; - struct git_delta_index *index; - struct index_entry *entry, **hash; - void *mem; - unsigned long memsize; - - if (!buf || !bufsize) - return NULL; - - /* Determine index hash size. Note that indexing skips the - first byte to allow for optimizing the rabin polynomial - initialization in create_delta(). */ - entries = (unsigned int)(bufsize - 1) / RABIN_WINDOW; - if (bufsize >= 0xffffffffUL) { - /* - * Current delta format can't encode offsets into - * reference buffer with more than 32 bits. - */ - entries = 0xfffffffeU / RABIN_WINDOW; - } - hsize = entries / 4; - for (i = 4; (1u << i) < hsize && i < 31; i++); - hsize = 1 << i; - hmask = hsize - 1; - - /* allocate lookup index */ - memsize = sizeof(*index) + - sizeof(*hash) * hsize + - sizeof(*entry) * entries; - mem = git__malloc(memsize); - if (!mem) - return NULL; - index = mem; - mem = index->hash; - hash = mem; - mem = hash + hsize; - entry = mem; - - index->memsize = memsize; - index->src_buf = buf; - index->src_size = bufsize; - index->hash_mask = hmask; - memset(hash, 0, hsize * sizeof(*hash)); - - /* allocate an array to count hash entries */ - hash_count = calloc(hsize, sizeof(*hash_count)); - if (!hash_count) { - git__free(index); - return NULL; - } - - /* then populate the index */ - prev_val = ~0; - for (data = buffer + entries * RABIN_WINDOW - RABIN_WINDOW; - data >= buffer; - data -= RABIN_WINDOW) { - unsigned int val = 0; - for (i = 1; i <= RABIN_WINDOW; i++) - val = ((val << 8) | data[i]) ^ T[val >> RABIN_SHIFT]; - if (val == prev_val) { - /* keep the lowest of consecutive identical blocks */ - entry[-1].ptr = data + RABIN_WINDOW; - } else { - prev_val = val; - i = val & hmask; - entry->ptr = data + RABIN_WINDOW; - entry->val = val; - entry->next = hash[i]; - hash[i] = entry++; - hash_count[i]++; - } - } - - /* - * Determine a limit on the number of entries in the same hash - * bucket. This guard us against patological data sets causing - * really bad hash distribution with most entries in the same hash - * bucket that would bring us to O(m*n) computing costs (m and n - * corresponding to reference and target buffer sizes). - * - * Make sure none of the hash buckets has more entries than - * we're willing to test. Otherwise we cull the entry list - * uniformly to still preserve a good repartition across - * the reference buffer. - */ - for (i = 0; i < hsize; i++) { - if (hash_count[i] < HASH_LIMIT) - continue; - - entry = hash[i]; - do { - struct index_entry *keep = entry; - int skip = hash_count[i] / HASH_LIMIT / 2; - do { - entry = entry->next; - } while(--skip && entry); - keep->next = entry; - } while (entry); - } - git__free(hash_count); - - return index; -} - -void git_delta_free_index(struct git_delta_index *index) -{ - git__free(index); -} - -unsigned long git_delta_sizeof_index(struct git_delta_index *index) -{ - if (index) - return index->memsize; - else - return 0; -} - -/* - * The maximum size for any opcode sequence, including the initial header - * plus rabin window plus biggest copy. - */ -#define MAX_OP_SIZE (5 + 5 + 1 + RABIN_WINDOW + 7) - -void * -git_delta_create( - const struct git_delta_index *index, - const void *trg_buf, - unsigned long trg_size, - unsigned long *delta_size, - unsigned long max_size) -{ - unsigned int i, outpos, outsize, moff, msize, val; - int inscnt; - const unsigned char *ref_data, *ref_top, *data, *top; - unsigned char *out; - - if (!trg_buf || !trg_size) - return NULL; - - outpos = 0; - outsize = 8192; - if (max_size && outsize >= max_size) - outsize = (unsigned int)(max_size + MAX_OP_SIZE + 1); - out = git__malloc(outsize); - if (!out) - return NULL; - - /* store reference buffer size */ - i = index->src_size; - while (i >= 0x80) { - out[outpos++] = i | 0x80; - i >>= 7; - } - out[outpos++] = i; - - /* store target buffer size */ - i = trg_size; - while (i >= 0x80) { - out[outpos++] = i | 0x80; - i >>= 7; - } - out[outpos++] = i; - - ref_data = index->src_buf; - ref_top = ref_data + index->src_size; - data = trg_buf; - top = (const unsigned char *) trg_buf + trg_size; - - outpos++; - val = 0; - for (i = 0; i < RABIN_WINDOW && data < top; i++, data++) { - out[outpos++] = *data; - val = ((val << 8) | *data) ^ T[val >> RABIN_SHIFT]; - } - inscnt = i; - - moff = 0; - msize = 0; - while (data < top) { - if (msize < 4096) { - struct index_entry *entry; - val ^= U[data[-RABIN_WINDOW]]; - val = ((val << 8) | *data) ^ T[val >> RABIN_SHIFT]; - i = val & index->hash_mask; - for (entry = index->hash[i]; entry; entry = entry->next) { - const unsigned char *ref = entry->ptr; - const unsigned char *src = data; - unsigned int ref_size = (unsigned int)(ref_top - ref); - if (entry->val != val) - continue; - if (ref_size > (unsigned int)(top - src)) - ref_size = (unsigned int)(top - src); - if (ref_size <= msize) - break; - while (ref_size-- && *src++ == *ref) - ref++; - if (msize < (unsigned int)(ref - entry->ptr)) { - /* this is our best match so far */ - msize = (unsigned int)(ref - entry->ptr); - moff = (unsigned int)(entry->ptr - ref_data); - if (msize >= 4096) /* good enough */ - break; - } - } - } - - if (msize < 4) { - if (!inscnt) - outpos++; - out[outpos++] = *data++; - inscnt++; - if (inscnt == 0x7f) { - out[outpos - inscnt - 1] = inscnt; - inscnt = 0; - } - msize = 0; - } else { - unsigned int left; - unsigned char *op; - - if (inscnt) { - while (moff && ref_data[moff-1] == data[-1]) { - /* we can match one byte back */ - msize++; - moff--; - data--; - outpos--; - if (--inscnt) - continue; - outpos--; /* remove count slot */ - inscnt--; /* make it -1 */ - break; - } - out[outpos - inscnt - 1] = inscnt; - inscnt = 0; - } - - /* A copy op is currently limited to 64KB (pack v2) */ - left = (msize < 0x10000) ? 0 : (msize - 0x10000); - msize -= left; - - op = out + outpos++; - i = 0x80; - - if (moff & 0x000000ff) - out[outpos++] = moff >> 0, i |= 0x01; - if (moff & 0x0000ff00) - out[outpos++] = moff >> 8, i |= 0x02; - if (moff & 0x00ff0000) - out[outpos++] = moff >> 16, i |= 0x04; - if (moff & 0xff000000) - out[outpos++] = moff >> 24, i |= 0x08; - - if (msize & 0x00ff) - out[outpos++] = msize >> 0, i |= 0x10; - if (msize & 0xff00) - out[outpos++] = msize >> 8, i |= 0x20; - - *op = i; - - data += msize; - moff += msize; - msize = left; - - if (msize < 4096) { - int j; - val = 0; - for (j = -RABIN_WINDOW; j < 0; j++) - val = ((val << 8) | data[j]) - ^ T[val >> RABIN_SHIFT]; - } - } - - if (outpos >= outsize - MAX_OP_SIZE) { - void *tmp = out; - outsize = outsize * 3 / 2; - if (max_size && outsize >= max_size) - outsize = max_size + MAX_OP_SIZE + 1; - if (max_size && outpos > max_size) - break; - out = git__realloc(out, outsize); - if (!out) { - git__free(tmp); - return NULL; - } - } - } - - if (inscnt) - out[outpos - inscnt - 1] = inscnt; - - if (max_size && outpos > max_size) { - git__free(out); - return NULL; - } - - *delta_size = outpos; - return out; -} diff --git a/src/delta.h b/src/delta.h deleted file mode 100644 index 4ca32799217..00000000000 --- a/src/delta.h +++ /dev/null @@ -1,114 +0,0 @@ -/* - * diff-delta code taken from git.git. See diff-delta.c for details. - * - */ -#ifndef INCLUDE_git_delta_h__ -#define INCLUDE_git_delta_h__ - -#include "common.h" - -/* opaque object for delta index */ -struct git_delta_index; - -/* - * create_delta_index: compute index data from given buffer - * - * This returns a pointer to a struct delta_index that should be passed to - * subsequent create_delta() calls, or to free_delta_index(). A NULL pointer - * is returned on failure. The given buffer must not be freed nor altered - * before free_delta_index() is called. The returned pointer must be freed - * using free_delta_index(). - */ -extern struct git_delta_index * -git_delta_create_index(const void *buf, unsigned long bufsize); - -/* - * free_delta_index: free the index created by create_delta_index() - * - * Given pointer must be what create_delta_index() returned, or NULL. - */ -extern void git_delta_free_index(struct git_delta_index *index); - -/* - * sizeof_delta_index: returns memory usage of delta index - * - * Given pointer must be what create_delta_index() returned, or NULL. - */ -extern unsigned long git_delta_sizeof_index(struct git_delta_index *index); - -/* - * create_delta: create a delta from given index for the given buffer - * - * This function may be called multiple times with different buffers using - * the same delta_index pointer. If max_delta_size is non-zero and the - * resulting delta is to be larger than max_delta_size then NULL is returned. - * On success, a non-NULL pointer to the buffer with the delta data is - * returned and *delta_size is updated with its size. The returned buffer - * must be freed by the caller. - */ -extern void *git_delta_create( - const struct git_delta_index *index, - const void *buf, - unsigned long bufsize, - unsigned long *delta_size, - unsigned long max_delta_size); - -/* - * diff_delta: create a delta from source buffer to target buffer - * - * If max_delta_size is non-zero and the resulting delta is to be larger - * than max_delta_size then NULL is returned. On success, a non-NULL - * pointer to the buffer with the delta data is returned and *delta_size is - * updated with its size. The returned buffer must be freed by the caller. - */ -GIT_INLINE(void *) git_delta( - const void *src_buf, unsigned long src_bufsize, - const void *trg_buf, unsigned long trg_bufsize, - unsigned long *delta_size, - unsigned long max_delta_size) -{ - struct git_delta_index *index = git_delta_create_index(src_buf, src_bufsize); - if (index) { - void *delta = git_delta_create( - index, trg_buf, trg_bufsize, delta_size, max_delta_size); - git_delta_free_index(index); - return delta; - } - return NULL; -} - -/* - * patch_delta: recreate target buffer given source buffer and delta data - * - * On success, a non-NULL pointer to the target buffer is returned and - * *trg_bufsize is updated with its size. On failure a NULL pointer is - * returned. The returned buffer must be freed by the caller. - */ -extern void *git_delta_patch( - const void *src_buf, unsigned long src_size, - const void *delta_buf, unsigned long delta_size, - unsigned long *dst_size); - -/* the smallest possible delta size is 4 bytes */ -#define GIT_DELTA_SIZE_MIN 4 - -/* - * This must be called twice on the delta data buffer, first to get the - * expected source buffer size, and again to get the target buffer size. - */ -GIT_INLINE(unsigned long) git_delta_get_hdr_size( - const unsigned char **datap, const unsigned char *top) -{ - const unsigned char *data = *datap; - unsigned long cmd, size = 0; - int i = 0; - do { - cmd = *data++; - size |= (cmd & 0x7f) << i; - i += 7; - } while (cmd & 0x80 && data < top); - *datap = data; - return size; -} - -#endif diff --git a/src/diff.c b/src/diff.c deleted file mode 100644 index 4b60935f067..00000000000 --- a/src/diff.c +++ /dev/null @@ -1,858 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "common.h" -#include "diff.h" -#include "fileops.h" -#include "config.h" -#include "attr_file.h" -#include "filter.h" -#include "pathspec.h" - -static git_diff_delta *diff_delta__alloc( - git_diff_list *diff, - git_delta_t status, - const char *path) -{ - git_diff_delta *delta = git__calloc(1, sizeof(git_diff_delta)); - if (!delta) - return NULL; - - delta->old_file.path = git_pool_strdup(&diff->pool, path); - if (delta->old_file.path == NULL) { - git__free(delta); - return NULL; - } - - delta->new_file.path = delta->old_file.path; - - if (diff->opts.flags & GIT_DIFF_REVERSE) { - switch (status) { - case GIT_DELTA_ADDED: status = GIT_DELTA_DELETED; break; - case GIT_DELTA_DELETED: status = GIT_DELTA_ADDED; break; - default: break; /* leave other status values alone */ - } - } - delta->status = status; - - return delta; -} - -static int diff_delta__from_one( - git_diff_list *diff, - git_delta_t status, - const git_index_entry *entry) -{ - git_diff_delta *delta; - - if (status == GIT_DELTA_IGNORED && - (diff->opts.flags & GIT_DIFF_INCLUDE_IGNORED) == 0) - return 0; - - if (status == GIT_DELTA_UNTRACKED && - (diff->opts.flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0) - return 0; - - if (!git_pathspec_match_path( - &diff->pathspec, entry->path, - (diff->opts.flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH) != 0, - (diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0)) - return 0; - - delta = diff_delta__alloc(diff, status, entry->path); - GITERR_CHECK_ALLOC(delta); - - /* This fn is just for single-sided diffs */ - assert(status != GIT_DELTA_MODIFIED); - - if (delta->status == GIT_DELTA_DELETED) { - delta->old_file.mode = entry->mode; - delta->old_file.size = entry->file_size; - git_oid_cpy(&delta->old_file.oid, &entry->oid); - } else /* ADDED, IGNORED, UNTRACKED */ { - delta->new_file.mode = entry->mode; - delta->new_file.size = entry->file_size; - git_oid_cpy(&delta->new_file.oid, &entry->oid); - } - - delta->old_file.flags |= GIT_DIFF_FILE_VALID_OID; - - if (delta->status == GIT_DELTA_DELETED || - !git_oid_iszero(&delta->new_file.oid)) - delta->new_file.flags |= GIT_DIFF_FILE_VALID_OID; - - if (git_vector_insert(&diff->deltas, delta) < 0) { - git__free(delta); - return -1; - } - - return 0; -} - -static int diff_delta__from_two( - git_diff_list *diff, - git_delta_t status, - const git_index_entry *old_entry, - uint32_t old_mode, - const git_index_entry *new_entry, - uint32_t new_mode, - git_oid *new_oid) -{ - git_diff_delta *delta; - - if (status == GIT_DELTA_UNMODIFIED && - (diff->opts.flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0) - return 0; - - if ((diff->opts.flags & GIT_DIFF_REVERSE) != 0) { - uint32_t temp_mode = old_mode; - const git_index_entry *temp_entry = old_entry; - old_entry = new_entry; - new_entry = temp_entry; - old_mode = new_mode; - new_mode = temp_mode; - } - - delta = diff_delta__alloc(diff, status, old_entry->path); - GITERR_CHECK_ALLOC(delta); - - git_oid_cpy(&delta->old_file.oid, &old_entry->oid); - delta->old_file.size = old_entry->file_size; - delta->old_file.mode = old_mode; - delta->old_file.flags |= GIT_DIFF_FILE_VALID_OID; - - git_oid_cpy(&delta->new_file.oid, &new_entry->oid); - delta->new_file.size = new_entry->file_size; - delta->new_file.mode = new_mode; - - if (new_oid) { - if ((diff->opts.flags & GIT_DIFF_REVERSE) != 0) - git_oid_cpy(&delta->old_file.oid, new_oid); - else - git_oid_cpy(&delta->new_file.oid, new_oid); - } - - if (new_oid || !git_oid_iszero(&new_entry->oid)) - delta->new_file.flags |= GIT_DIFF_FILE_VALID_OID; - - if (git_vector_insert(&diff->deltas, delta) < 0) { - git__free(delta); - return -1; - } - - return 0; -} - -static git_diff_delta *diff_delta__last_for_item( - git_diff_list *diff, - const git_index_entry *item) -{ - git_diff_delta *delta = git_vector_last(&diff->deltas); - if (!delta) - return NULL; - - switch (delta->status) { - case GIT_DELTA_UNMODIFIED: - case GIT_DELTA_DELETED: - if (git_oid_cmp(&delta->old_file.oid, &item->oid) == 0) - return delta; - break; - case GIT_DELTA_ADDED: - if (git_oid_cmp(&delta->new_file.oid, &item->oid) == 0) - return delta; - break; - case GIT_DELTA_UNTRACKED: - if (diff->strcomp(delta->new_file.path, item->path) == 0 && - git_oid_cmp(&delta->new_file.oid, &item->oid) == 0) - return delta; - break; - case GIT_DELTA_MODIFIED: - if (git_oid_cmp(&delta->old_file.oid, &item->oid) == 0 || - git_oid_cmp(&delta->new_file.oid, &item->oid) == 0) - return delta; - break; - default: - break; - } - - return NULL; -} - -static char *diff_strdup_prefix(git_pool *pool, const char *prefix) -{ - size_t len = strlen(prefix); - - /* append '/' at end if needed */ - if (len > 0 && prefix[len - 1] != '/') - return git_pool_strcat(pool, prefix, "/"); - else - return git_pool_strndup(pool, prefix, len + 1); -} - -int git_diff_delta__cmp(const void *a, const void *b) -{ - const git_diff_delta *da = a, *db = b; - int val = strcmp(da->old_file.path, db->old_file.path); - return val ? val : ((int)da->status - (int)db->status); -} - -bool git_diff_delta__should_skip( - const git_diff_options *opts, const git_diff_delta *delta) -{ - uint32_t flags = opts ? opts->flags : 0; - - if (delta->status == GIT_DELTA_UNMODIFIED && - (flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0) - return true; - - if (delta->status == GIT_DELTA_IGNORED && - (flags & GIT_DIFF_INCLUDE_IGNORED) == 0) - return true; - - if (delta->status == GIT_DELTA_UNTRACKED && - (flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0) - return true; - - return false; -} - - -static int config_bool(git_config *cfg, const char *name, int defvalue) -{ - int val = defvalue; - - if (git_config_get_bool(&val, cfg, name) < 0) - giterr_clear(); - - return val; -} - -static git_diff_list *git_diff_list_alloc( - git_repository *repo, const git_diff_options *opts) -{ - git_config *cfg; - git_diff_list *diff = git__calloc(1, sizeof(git_diff_list)); - if (diff == NULL) - return NULL; - - GIT_REFCOUNT_INC(diff); - diff->repo = repo; - - if (git_vector_init(&diff->deltas, 0, git_diff_delta__cmp) < 0 || - git_pool_init(&diff->pool, 1, 0) < 0) - goto fail; - - /* load config values that affect diff behavior */ - if (git_repository_config__weakptr(&cfg, repo) < 0) - goto fail; - if (config_bool(cfg, "core.symlinks", 1)) - diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_HAS_SYMLINKS; - if (config_bool(cfg, "core.ignorestat", 0)) - diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_ASSUME_UNCHANGED; - if (config_bool(cfg, "core.filemode", 1)) - diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_TRUST_MODE_BITS; - if (config_bool(cfg, "core.trustctime", 1)) - diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_TRUST_CTIME; - /* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */ - - /* TODO: there are certain config settings where even if we were - * not given an options structure, we need the diff list to have one - * so that we can store the altered default values. - * - * - diff.ignoreSubmodules - * - diff.mnemonicprefix - * - diff.noprefix - */ - - if (opts == NULL) - return diff; - - memcpy(&diff->opts, opts, sizeof(git_diff_options)); - - if(opts->flags & GIT_DIFF_IGNORE_FILEMODE) - diff->diffcaps = diff->diffcaps & ~GIT_DIFFCAPS_TRUST_MODE_BITS; - - /* pathspec init will do nothing for empty pathspec */ - if (git_pathspec_init(&diff->pathspec, &opts->pathspec, &diff->pool) < 0) - goto fail; - - /* TODO: handle config diff.mnemonicprefix, diff.noprefix */ - - diff->opts.old_prefix = diff_strdup_prefix(&diff->pool, - opts->old_prefix ? opts->old_prefix : DIFF_OLD_PREFIX_DEFAULT); - diff->opts.new_prefix = diff_strdup_prefix(&diff->pool, - opts->new_prefix ? opts->new_prefix : DIFF_NEW_PREFIX_DEFAULT); - - if (!diff->opts.old_prefix || !diff->opts.new_prefix) - goto fail; - - if (diff->opts.flags & GIT_DIFF_REVERSE) { - const char *swap = diff->opts.old_prefix; - diff->opts.old_prefix = diff->opts.new_prefix; - diff->opts.new_prefix = swap; - } - - /* INCLUDE_TYPECHANGE_TREES implies INCLUDE_TYPECHANGE */ - if (diff->opts.flags & GIT_DIFF_INCLUDE_TYPECHANGE_TREES) - diff->opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE; - - return diff; - -fail: - git_diff_list_free(diff); - return NULL; -} - -static void diff_list_free(git_diff_list *diff) -{ - git_diff_delta *delta; - unsigned int i; - - git_vector_foreach(&diff->deltas, i, delta) { - git__free(delta); - diff->deltas.contents[i] = NULL; - } - git_vector_free(&diff->deltas); - - git_pathspec_free(&diff->pathspec); - git_pool_clear(&diff->pool); - git__free(diff); -} - -void git_diff_list_free(git_diff_list *diff) -{ - if (!diff) - return; - - GIT_REFCOUNT_DEC(diff, diff_list_free); -} - -void git_diff_list_addref(git_diff_list *diff) -{ - GIT_REFCOUNT_INC(diff); -} - -int git_diff__oid_for_file( - git_repository *repo, - const char *path, - uint16_t mode, - git_off_t size, - git_oid *oid) -{ - int result = 0; - git_buf full_path = GIT_BUF_INIT; - - if (git_buf_joinpath( - &full_path, git_repository_workdir(repo), path) < 0) - return -1; - - if (!mode) { - struct stat st; - - if (p_stat(path, &st) < 0) { - giterr_set(GITERR_OS, "Could not stat '%s'", path); - result = -1; - goto cleanup; - } - - mode = st.st_mode; - size = st.st_size; - } - - /* calculate OID for file if possible */ - if (S_ISGITLINK(mode)) { - git_submodule *sm; - const git_oid *sm_oid; - - if (!git_submodule_lookup(&sm, repo, path) && - (sm_oid = git_submodule_wd_id(sm)) != NULL) - git_oid_cpy(oid, sm_oid); - else { - /* if submodule lookup failed probably just in an intermediate - * state where some init hasn't happened, so ignore the error - */ - giterr_clear(); - memset(oid, 0, sizeof(*oid)); - } - } else if (S_ISLNK(mode)) { - result = git_odb__hashlink(oid, full_path.ptr); - } else if (!git__is_sizet(size)) { - giterr_set(GITERR_OS, "File size overflow (for 32-bits) on '%s'", path); - result = -1; - } else { - git_vector filters = GIT_VECTOR_INIT; - - result = git_filters_load(&filters, repo, path, GIT_FILTER_TO_ODB); - if (result >= 0) { - int fd = git_futils_open_ro(full_path.ptr); - if (fd < 0) - result = fd; - else { - result = git_odb__hashfd_filtered( - oid, fd, (size_t)size, GIT_OBJ_BLOB, &filters); - p_close(fd); - } - } - - git_filters_free(&filters); - } - -cleanup: - git_buf_free(&full_path); - return result; -} - -#define MODE_BITS_MASK 0000777 - -static int maybe_modified( - git_iterator *old_iter, - const git_index_entry *oitem, - git_iterator *new_iter, - const git_index_entry *nitem, - git_diff_list *diff) -{ - git_oid noid, *use_noid = NULL; - git_delta_t status = GIT_DELTA_MODIFIED; - unsigned int omode = oitem->mode; - unsigned int nmode = nitem->mode; - bool new_is_workdir = (new_iter->type == GIT_ITERATOR_TYPE_WORKDIR); - - GIT_UNUSED(old_iter); - - if (!git_pathspec_match_path( - &diff->pathspec, oitem->path, - (diff->opts.flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH) != 0, - (diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0)) - return 0; - - /* on platforms with no symlinks, preserve mode of existing symlinks */ - if (S_ISLNK(omode) && S_ISREG(nmode) && new_is_workdir && - !(diff->diffcaps & GIT_DIFFCAPS_HAS_SYMLINKS)) - nmode = omode; - - /* on platforms with no execmode, just preserve old mode */ - if (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) && - (nmode & MODE_BITS_MASK) != (omode & MODE_BITS_MASK) && - new_is_workdir) - nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK); - - /* support "assume unchanged" (poorly, b/c we still stat everything) */ - if ((diff->diffcaps & GIT_DIFFCAPS_ASSUME_UNCHANGED) != 0) - status = (oitem->flags_extended & GIT_IDXENTRY_INTENT_TO_ADD) ? - GIT_DELTA_MODIFIED : GIT_DELTA_UNMODIFIED; - - /* support "skip worktree" index bit */ - else if ((oitem->flags_extended & GIT_IDXENTRY_SKIP_WORKTREE) != 0) - status = GIT_DELTA_UNMODIFIED; - - /* if basic type of file changed, then split into delete and add */ - else if (GIT_MODE_TYPE(omode) != GIT_MODE_TYPE(nmode)) { - if ((diff->opts.flags & GIT_DIFF_INCLUDE_TYPECHANGE) != 0) - status = GIT_DELTA_TYPECHANGE; - else { - if (diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem) < 0 || - diff_delta__from_one(diff, GIT_DELTA_ADDED, nitem) < 0) - return -1; - return 0; - } - } - - /* if oids and modes match, then file is unmodified */ - else if (git_oid_equal(&oitem->oid, &nitem->oid) && omode == nmode) - status = GIT_DELTA_UNMODIFIED; - - /* if we have an unknown OID and a workdir iterator, then check some - * circumstances that can accelerate things or need special handling - */ - else if (git_oid_iszero(&nitem->oid) && new_is_workdir) { - /* TODO: add check against index file st_mtime to avoid racy-git */ - - /* if the stat data looks exactly alike, then assume the same */ - if (omode == nmode && - oitem->file_size == nitem->file_size && - (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) || - (oitem->ctime.seconds == nitem->ctime.seconds)) && - oitem->mtime.seconds == nitem->mtime.seconds && - (!(diff->diffcaps & GIT_DIFFCAPS_USE_DEV) || - (oitem->dev == nitem->dev)) && - oitem->ino == nitem->ino && - oitem->uid == nitem->uid && - oitem->gid == nitem->gid) - status = GIT_DELTA_UNMODIFIED; - - else if (S_ISGITLINK(nmode)) { - git_submodule *sub; - - if ((diff->opts.flags & GIT_DIFF_IGNORE_SUBMODULES) != 0) - status = GIT_DELTA_UNMODIFIED; - else if (git_submodule_lookup(&sub, diff->repo, nitem->path) < 0) - return -1; - else if (git_submodule_ignore(sub) == GIT_SUBMODULE_IGNORE_ALL) - status = GIT_DELTA_UNMODIFIED; - else { - unsigned int sm_status = 0; - if (git_submodule_status(&sm_status, sub) < 0) - return -1; - status = GIT_SUBMODULE_STATUS_IS_UNMODIFIED(sm_status) - ? GIT_DELTA_UNMODIFIED : GIT_DELTA_MODIFIED; - - /* grab OID while we are here */ - if (git_oid_iszero(&nitem->oid)) { - const git_oid *sm_oid = git_submodule_wd_id(sub); - if (sm_oid != NULL) { - git_oid_cpy(&noid, sm_oid); - use_noid = &noid; - } - } - } - } - } - - /* if we got here and decided that the files are modified, but we - * haven't calculated the OID of the new item, then calculate it now - */ - if (status != GIT_DELTA_UNMODIFIED && git_oid_iszero(&nitem->oid)) { - if (!use_noid) { - if (git_diff__oid_for_file(diff->repo, - nitem->path, nitem->mode, nitem->file_size, &noid) < 0) - return -1; - use_noid = &noid; - } - if (omode == nmode && git_oid_equal(&oitem->oid, use_noid)) - status = GIT_DELTA_UNMODIFIED; - } - - return diff_delta__from_two( - diff, status, oitem, omode, nitem, nmode, use_noid); -} - -static bool entry_is_prefixed( - git_diff_list *diff, - const git_index_entry *item, - const git_index_entry *prefix_item) -{ - size_t pathlen; - - if (!item || diff->pfxcomp(item->path, prefix_item->path) != 0) - return false; - - pathlen = strlen(prefix_item->path); - - return (prefix_item->path[pathlen - 1] == '/' || - item->path[pathlen] == '\0' || - item->path[pathlen] == '/'); -} - -static int diff_list_init_from_iterators( - git_diff_list *diff, - git_iterator *old_iter, - git_iterator *new_iter) -{ - diff->old_src = old_iter->type; - diff->new_src = new_iter->type; - - /* Use case-insensitive compare if either iterator has - * the ignore_case bit set */ - if (!git_iterator_ignore_case(old_iter) && - !git_iterator_ignore_case(new_iter)) - { - diff->opts.flags &= ~GIT_DIFF_DELTAS_ARE_ICASE; - - diff->strcomp = git__strcmp; - diff->strncomp = git__strncmp; - diff->pfxcomp = git__prefixcmp; - diff->entrycomp = git_index_entry__cmp; - } else { - diff->opts.flags |= GIT_DIFF_DELTAS_ARE_ICASE; - - diff->strcomp = git__strcasecmp; - diff->strncomp = git__strncasecmp; - diff->pfxcomp = git__prefixcmp_icase; - diff->entrycomp = git_index_entry__cmp_icase; - } - - return 0; -} - -int git_diff__from_iterators( - git_diff_list **diff_ptr, - git_repository *repo, - git_iterator *old_iter, - git_iterator *new_iter, - const git_diff_options *opts) -{ - int error = 0; - const git_index_entry *oitem, *nitem; - git_buf ignore_prefix = GIT_BUF_INIT; - git_diff_list *diff = git_diff_list_alloc(repo, opts); - - *diff_ptr = NULL; - - if (!diff || diff_list_init_from_iterators(diff, old_iter, new_iter) < 0) - goto fail; - - if (diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) { - /* If either iterator does not have ignore_case set, then we will - * spool its data, sort it icase, and use that for the merge join - * with the other iterator which was icase sorted. This call is - * a no-op on an iterator that already matches "ignore_case". - */ - if (git_iterator_spoolandsort_push(old_iter, true) < 0 || - git_iterator_spoolandsort_push(new_iter, true) < 0) - goto fail; - } - - if (git_iterator_current(old_iter, &oitem) < 0 || - git_iterator_current(new_iter, &nitem) < 0) - goto fail; - - /* run iterators building diffs */ - while (oitem || nitem) { - int cmp = oitem ? (nitem ? diff->entrycomp(oitem, nitem) : -1) : 1; - - /* create DELETED records for old items not matched in new */ - if (cmp < 0) { - if (diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem) < 0) - goto fail; - - /* if we are generating TYPECHANGE records then check for that - * instead of just generating a DELETE record - */ - if ((diff->opts.flags & GIT_DIFF_INCLUDE_TYPECHANGE_TREES) != 0 && - entry_is_prefixed(diff, nitem, oitem)) - { - /* this entry has become a tree! convert to TYPECHANGE */ - git_diff_delta *last = diff_delta__last_for_item(diff, oitem); - if (last) { - last->status = GIT_DELTA_TYPECHANGE; - last->new_file.mode = GIT_FILEMODE_TREE; - } - - /* If new_iter is a workdir iterator, then this situation - * will certainly be followed by a series of untracked items. - * Unless RECURSE_UNTRACKED_DIRS is set, skip over them... - */ - if (S_ISDIR(nitem->mode) && - !(diff->opts.flags & GIT_DIFF_RECURSE_UNTRACKED_DIRS)) - { - if (git_iterator_advance(new_iter, &nitem) < 0) - goto fail; - } - } - - if (git_iterator_advance(old_iter, &oitem) < 0) - goto fail; - } - - /* create ADDED, TRACKED, or IGNORED records for new items not - * matched in old (and/or descend into directories as needed) - */ - else if (cmp > 0) { - git_delta_t delta_type = GIT_DELTA_UNTRACKED; - bool contains_oitem = entry_is_prefixed(diff, oitem, nitem); - - /* check if contained in ignored parent directory */ - if (git_buf_len(&ignore_prefix) && - diff->pfxcomp(nitem->path, git_buf_cstr(&ignore_prefix)) == 0) - delta_type = GIT_DELTA_IGNORED; - - if (S_ISDIR(nitem->mode)) { - /* recurse into directory only if there are tracked items in - * it or if the user requested the contents of untracked - * directories and it is not under an ignored directory. - */ - bool recurse_untracked = - (delta_type == GIT_DELTA_UNTRACKED && - (diff->opts.flags & GIT_DIFF_RECURSE_UNTRACKED_DIRS) != 0); - - /* do not advance into directories that contain a .git file */ - if (!contains_oitem && recurse_untracked) { - git_buf *full = NULL; - if (git_iterator_current_workdir_path(new_iter, &full) < 0) - goto fail; - if (git_path_contains_dir(full, DOT_GIT)) - recurse_untracked = false; - } - - if (contains_oitem || recurse_untracked) { - /* if this directory is ignored, remember it as the - * "ignore_prefix" for processing contained items - */ - if (delta_type == GIT_DELTA_UNTRACKED && - git_iterator_current_is_ignored(new_iter)) - git_buf_sets(&ignore_prefix, nitem->path); - - if (git_iterator_advance_into_directory(new_iter, &nitem) < 0) - goto fail; - - continue; - } - } - - /* In core git, the next two "else if" clauses are effectively - * reversed -- i.e. when an untracked file contained in an - * ignored directory is individually ignored, it shows up as an - * ignored file in the diff list, even though other untracked - * files in the same directory are skipped completely. - * - * To me, this is odd. If the directory is ignored and the file - * is untracked, we should skip it consistently, regardless of - * whether it happens to match a pattern in the ignore file. - * - * To match the core git behavior, just reverse the following - * two "else if" cases so that individual file ignores are - * checked before container directory exclusions are used to - * skip the file. - */ - else if (delta_type == GIT_DELTA_IGNORED) { - if (git_iterator_advance(new_iter, &nitem) < 0) - goto fail; - continue; /* ignored parent directory, so skip completely */ - } - - else if (git_iterator_current_is_ignored(new_iter)) - delta_type = GIT_DELTA_IGNORED; - - else if (new_iter->type != GIT_ITERATOR_TYPE_WORKDIR) - delta_type = GIT_DELTA_ADDED; - - if (diff_delta__from_one(diff, delta_type, nitem) < 0) - goto fail; - - /* if we are generating TYPECHANGE records then check for that - * instead of just generating an ADDED/UNTRACKED record - */ - if (delta_type != GIT_DELTA_IGNORED && - (diff->opts.flags & GIT_DIFF_INCLUDE_TYPECHANGE_TREES) != 0 && - contains_oitem) - { - /* this entry was prefixed with a tree - make TYPECHANGE */ - git_diff_delta *last = diff_delta__last_for_item(diff, nitem); - if (last) { - last->status = GIT_DELTA_TYPECHANGE; - last->old_file.mode = GIT_FILEMODE_TREE; - } - } - - if (git_iterator_advance(new_iter, &nitem) < 0) - goto fail; - } - - /* otherwise item paths match, so create MODIFIED record - * (or ADDED and DELETED pair if type changed) - */ - else { - assert(oitem && nitem && cmp == 0); - - if (maybe_modified(old_iter, oitem, new_iter, nitem, diff) < 0 || - git_iterator_advance(old_iter, &oitem) < 0 || - git_iterator_advance(new_iter, &nitem) < 0) - goto fail; - } - } - - *diff_ptr = diff; - -fail: - if (!*diff_ptr) { - git_diff_list_free(diff); - error = -1; - } - - git_buf_free(&ignore_prefix); - - return error; -} - -#define DIFF_FROM_ITERATORS(MAKE_FIRST, MAKE_SECOND) do { \ - git_iterator *a = NULL, *b = NULL; \ - char *pfx = opts ? git_pathspec_prefix(&opts->pathspec) : NULL; \ - GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \ - if (!(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \ - error = git_diff__from_iterators(diff, repo, a, b, opts); \ - git__free(pfx); git_iterator_free(a); git_iterator_free(b); \ -} while (0) - -int git_diff_tree_to_tree( - git_diff_list **diff, - git_repository *repo, - git_tree *old_tree, - git_tree *new_tree, - const git_diff_options *opts) -{ - int error = 0; - - assert(diff && repo); - - DIFF_FROM_ITERATORS( - git_iterator_for_tree_range(&a, old_tree, 0, pfx, pfx), - git_iterator_for_tree_range(&b, new_tree, 0, pfx, pfx) - ); - - return error; -} - -int git_diff_tree_to_index( - git_diff_list **diff, - git_repository *repo, - git_tree *old_tree, - git_index *index, - const git_diff_options *opts) -{ - int error = 0; - - assert(diff && repo); - - if (!index && (error = git_repository_index__weakptr(&index, repo)) < 0) - return error; - - DIFF_FROM_ITERATORS( - git_iterator_for_tree_range(&a, old_tree, 0, pfx, pfx), - git_iterator_for_index_range(&b, index, 0, pfx, pfx) - ); - - return error; -} - -int git_diff_index_to_workdir( - git_diff_list **diff, - git_repository *repo, - git_index *index, - const git_diff_options *opts) -{ - int error = 0; - - assert(diff && repo); - - if (!index && (error = git_repository_index__weakptr(&index, repo)) < 0) - return error; - - DIFF_FROM_ITERATORS( - git_iterator_for_index_range(&a, index, 0, pfx, pfx), - git_iterator_for_workdir_range(&b, repo, 0, pfx, pfx) - ); - - return error; -} - - -int git_diff_tree_to_workdir( - git_diff_list **diff, - git_repository *repo, - git_tree *old_tree, - const git_diff_options *opts) -{ - int error = 0; - - assert(diff && repo); - - DIFF_FROM_ITERATORS( - git_iterator_for_tree_range(&a, old_tree, 0, pfx, pfx), - git_iterator_for_workdir_range(&b, repo, 0, pfx, pfx) - ); - - return error; -} diff --git a/src/diff.h b/src/diff.h deleted file mode 100644 index 16fbf71e6c4..00000000000 --- a/src/diff.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_diff_h__ -#define INCLUDE_diff_h__ - -#include "git2/diff.h" -#include "git2/oid.h" - -#include -#include "vector.h" -#include "buffer.h" -#include "iterator.h" -#include "repository.h" -#include "pool.h" - -#define DIFF_OLD_PREFIX_DEFAULT "a/" -#define DIFF_NEW_PREFIX_DEFAULT "b/" - -enum { - GIT_DIFFCAPS_HAS_SYMLINKS = (1 << 0), /* symlinks on platform? */ - GIT_DIFFCAPS_ASSUME_UNCHANGED = (1 << 1), /* use stat? */ - GIT_DIFFCAPS_TRUST_MODE_BITS = (1 << 2), /* use st_mode? */ - GIT_DIFFCAPS_TRUST_CTIME = (1 << 3), /* use st_ctime? */ - GIT_DIFFCAPS_USE_DEV = (1 << 4), /* use st_dev? */ -}; - -#define GIT_DELTA__TO_DELETE 10 -#define GIT_DELTA__TO_SPLIT 11 - -struct git_diff_list { - git_refcount rc; - git_repository *repo; - git_diff_options opts; - git_vector pathspec; - git_vector deltas; /* vector of git_diff_delta */ - git_pool pool; - git_iterator_type_t old_src; - git_iterator_type_t new_src; - uint32_t diffcaps; - - int (*strcomp)(const char *, const char *); - int (*strncomp)(const char *, const char *, size_t); - int (*pfxcomp)(const char *str, const char *pfx); - int (*entrycomp)(const void *a, const void *b); -}; - -extern void git_diff__cleanup_modes( - uint32_t diffcaps, uint32_t *omode, uint32_t *nmode); - -extern void git_diff_list_addref(git_diff_list *diff); - -extern int git_diff_delta__cmp(const void *a, const void *b); - -extern bool git_diff_delta__should_skip( - const git_diff_options *opts, const git_diff_delta *delta); - -extern int git_diff__oid_for_file( - git_repository *, const char *, uint16_t, git_off_t, git_oid *); - -extern int git_diff__from_iterators( - git_diff_list **diff_ptr, - git_repository *repo, - git_iterator *old_iter, - git_iterator *new_iter, - const git_diff_options *opts); - -#endif - diff --git a/src/diff_output.c b/src/diff_output.c deleted file mode 100644 index 4f1064b7f75..00000000000 --- a/src/diff_output.c +++ /dev/null @@ -1,1741 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "common.h" -#include "git2/attr.h" -#include "git2/oid.h" -#include "git2/submodule.h" -#include "diff_output.h" -#include -#include "fileops.h" -#include "filter.h" - -static int read_next_int(const char **str, int *value) -{ - const char *scan = *str; - int v = 0, digits = 0; - /* find next digit */ - for (scan = *str; *scan && !isdigit(*scan); scan++); - /* parse next number */ - for (; isdigit(*scan); scan++, digits++) - v = (v * 10) + (*scan - '0'); - *str = scan; - *value = v; - return (digits > 0) ? 0 : -1; -} - -static int parse_hunk_header(git_diff_range *range, const char *header) -{ - /* expect something of the form "@@ -%d[,%d] +%d[,%d] @@" */ - if (*header != '@') - return -1; - if (read_next_int(&header, &range->old_start) < 0) - return -1; - if (*header == ',') { - if (read_next_int(&header, &range->old_lines) < 0) - return -1; - } else - range->old_lines = 1; - if (read_next_int(&header, &range->new_start) < 0) - return -1; - if (*header == ',') { - if (read_next_int(&header, &range->new_lines) < 0) - return -1; - } else - range->new_lines = 1; - if (range->old_start < 0 || range->new_start < 0) - return -1; - - return 0; -} - -#define KNOWN_BINARY_FLAGS (GIT_DIFF_FILE_BINARY|GIT_DIFF_FILE_NOT_BINARY) -#define NOT_BINARY_FLAGS (GIT_DIFF_FILE_NOT_BINARY|GIT_DIFF_FILE_NO_DATA) - -static int update_file_is_binary_by_attr( - git_repository *repo, git_diff_file *file) -{ - const char *value; - - /* because of blob diffs, cannot assume path is set */ - if (!file->path || !strlen(file->path)) - return 0; - - if (git_attr_get(&value, repo, 0, file->path, "diff") < 0) - return -1; - - if (GIT_ATTR_FALSE(value)) - file->flags |= GIT_DIFF_FILE_BINARY; - else if (GIT_ATTR_TRUE(value)) - file->flags |= GIT_DIFF_FILE_NOT_BINARY; - /* otherwise leave file->flags alone */ - - return 0; -} - -static void update_delta_is_binary(git_diff_delta *delta) -{ - if ((delta->old_file.flags & GIT_DIFF_FILE_BINARY) != 0 || - (delta->new_file.flags & GIT_DIFF_FILE_BINARY) != 0) - delta->binary = 1; - - else if ((delta->old_file.flags & NOT_BINARY_FLAGS) != 0 && - (delta->new_file.flags & NOT_BINARY_FLAGS) != 0) - delta->binary = 0; - - /* otherwise leave delta->binary value untouched */ -} - -static int diff_delta_is_binary_by_attr( - diff_context *ctxt, git_diff_patch *patch) -{ - int error = 0, mirror_new; - git_diff_delta *delta = patch->delta; - - delta->binary = -1; - - /* make sure files are conceivably mmap-able */ - if ((git_off_t)((size_t)delta->old_file.size) != delta->old_file.size || - (git_off_t)((size_t)delta->new_file.size) != delta->new_file.size) - { - delta->old_file.flags |= GIT_DIFF_FILE_BINARY; - delta->new_file.flags |= GIT_DIFF_FILE_BINARY; - delta->binary = 1; - return 0; - } - - /* check if user is forcing us to text diff these files */ - if (ctxt->opts && (ctxt->opts->flags & GIT_DIFF_FORCE_TEXT) != 0) { - delta->old_file.flags |= GIT_DIFF_FILE_NOT_BINARY; - delta->new_file.flags |= GIT_DIFF_FILE_NOT_BINARY; - delta->binary = 0; - return 0; - } - - /* check diff attribute +, -, or 0 */ - if (update_file_is_binary_by_attr(ctxt->repo, &delta->old_file) < 0) - return -1; - - mirror_new = (delta->new_file.path == delta->old_file.path || - ctxt->diff->strcomp(delta->new_file.path, delta->old_file.path) == 0); - if (mirror_new) - delta->new_file.flags |= (delta->old_file.flags & KNOWN_BINARY_FLAGS); - else - error = update_file_is_binary_by_attr(ctxt->repo, &delta->new_file); - - update_delta_is_binary(delta); - - return error; -} - -static int diff_delta_is_binary_by_content( - diff_context *ctxt, - git_diff_delta *delta, - git_diff_file *file, - const git_map *map) -{ - const git_buf search = { map->data, 0, min(map->len, 4000) }; - - GIT_UNUSED(ctxt); - - if ((file->flags & KNOWN_BINARY_FLAGS) == 0) { - /* TODO: provide encoding / binary detection callbacks that can - * be UTF-8 aware, etc. For now, instead of trying to be smart, - * let's just use the simple NUL-byte detection that core git uses. - */ - /* previously was: if (git_buf_text_is_binary(&search)) */ - if (git_buf_text_contains_nul(&search)) - file->flags |= GIT_DIFF_FILE_BINARY; - else - file->flags |= GIT_DIFF_FILE_NOT_BINARY; - } - - update_delta_is_binary(delta); - - return 0; -} - -static int diff_delta_is_binary_by_size( - diff_context *ctxt, git_diff_delta *delta, git_diff_file *file) -{ - git_off_t threshold = MAX_DIFF_FILESIZE; - - if ((file->flags & KNOWN_BINARY_FLAGS) != 0) - return 0; - - if (ctxt && ctxt->opts) { - if (ctxt->opts->max_size < 0) - return 0; - - if (ctxt->opts->max_size > 0) - threshold = ctxt->opts->max_size; - } - - if (file->size > threshold) - file->flags |= GIT_DIFF_FILE_BINARY; - - update_delta_is_binary(delta); - - return 0; -} - -static void setup_xdiff_options( - const git_diff_options *opts, xdemitconf_t *cfg, xpparam_t *param) -{ - memset(cfg, 0, sizeof(xdemitconf_t)); - memset(param, 0, sizeof(xpparam_t)); - - cfg->ctxlen = - (!opts || !opts->context_lines) ? 3 : opts->context_lines; - cfg->interhunkctxlen = - (!opts) ? 0 : opts->interhunk_lines; - - if (!opts) - return; - - if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE) - param->flags |= XDF_WHITESPACE_FLAGS; - if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE_CHANGE) - param->flags |= XDF_IGNORE_WHITESPACE_CHANGE; - if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE_EOL) - param->flags |= XDF_IGNORE_WHITESPACE_AT_EOL; -} - - -static int get_blob_content( - diff_context *ctxt, - git_diff_delta *delta, - git_diff_file *file, - git_map *map, - git_blob **blob) -{ - int error; - git_odb_object *odb_obj = NULL; - - if (git_oid_iszero(&file->oid)) - return 0; - - if (file->mode == GIT_FILEMODE_COMMIT) - { - char oidstr[GIT_OID_HEXSZ+1]; - git_buf content = GIT_BUF_INIT; - - git_oid_fmt(oidstr, &file->oid); - oidstr[GIT_OID_HEXSZ] = 0; - git_buf_printf(&content, "Subproject commit %s\n", oidstr ); - - map->data = git_buf_detach(&content); - map->len = strlen(map->data); - - file->flags |= GIT_DIFF_FILE_FREE_DATA; - return 0; - } - - if (!file->size) { - git_odb *odb; - size_t len; - git_otype type; - - /* peek at object header to avoid loading if too large */ - if ((error = git_repository_odb__weakptr(&odb, ctxt->repo)) < 0 || - (error = git_odb__read_header_or_object( - &odb_obj, &len, &type, odb, &file->oid)) < 0) - return error; - - assert(type == GIT_OBJ_BLOB); - - file->size = len; - } - - /* if blob is too large to diff, mark as binary */ - if ((error = diff_delta_is_binary_by_size(ctxt, delta, file)) < 0) - return error; - if (delta->binary == 1) - return 0; - - if (odb_obj != NULL) { - error = git_object__from_odb_object( - (git_object **)blob, ctxt->repo, odb_obj, GIT_OBJ_BLOB); - git_odb_object_free(odb_obj); - } else - error = git_blob_lookup(blob, ctxt->repo, &file->oid); - - if (error) - return error; - - map->data = (void *)git_blob_rawcontent(*blob); - map->len = (size_t)git_blob_rawsize(*blob); - - return diff_delta_is_binary_by_content(ctxt, delta, file, map); -} - -static int get_workdir_sm_content( - diff_context *ctxt, - git_diff_file *file, - git_map *map) -{ - int error = 0; - git_buf content = GIT_BUF_INIT; - git_submodule* sm = NULL; - unsigned int sm_status = 0; - const char* sm_status_text = ""; - char oidstr[GIT_OID_HEXSZ+1]; - - if ((error = git_submodule_lookup(&sm, ctxt->repo, file->path)) < 0 || - (error = git_submodule_status(&sm_status, sm)) < 0) - return error; - - /* update OID if we didn't have it previously */ - if ((file->flags & GIT_DIFF_FILE_VALID_OID) == 0) { - const git_oid* sm_head; - - if ((sm_head = git_submodule_wd_id(sm)) != NULL || - (sm_head = git_submodule_head_id(sm)) != NULL) - { - git_oid_cpy(&file->oid, sm_head); - file->flags |= GIT_DIFF_FILE_VALID_OID; - } - } - - git_oid_fmt(oidstr, &file->oid); - oidstr[GIT_OID_HEXSZ] = '\0'; - - if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status)) - sm_status_text = "-dirty"; - - git_buf_printf(&content, "Subproject commit %s%s\n", - oidstr, sm_status_text); - - map->data = git_buf_detach(&content); - map->len = strlen(map->data); - - file->flags |= GIT_DIFF_FILE_FREE_DATA; - - return 0; -} - -static int get_workdir_content( - diff_context *ctxt, - git_diff_delta *delta, - git_diff_file *file, - git_map *map) -{ - int error = 0; - git_buf path = GIT_BUF_INIT; - const char *wd = git_repository_workdir(ctxt->repo); - - if (S_ISGITLINK(file->mode)) - return get_workdir_sm_content(ctxt, file, map); - - if (S_ISDIR(file->mode)) - return 0; - - if (git_buf_joinpath(&path, wd, file->path) < 0) - return -1; - - if (S_ISLNK(file->mode)) { - ssize_t alloc_len, read_len; - - file->flags |= GIT_DIFF_FILE_FREE_DATA; - file->flags |= GIT_DIFF_FILE_BINARY; - - /* link path on disk could be UTF-16, so prepare a buffer that is - * big enough to handle some UTF-8 data expansion - */ - alloc_len = (ssize_t)(file->size * 2) + 1; - - map->data = git__malloc(alloc_len); - GITERR_CHECK_ALLOC(map->data); - - read_len = p_readlink(path.ptr, map->data, (int)alloc_len); - if (read_len < 0) { - giterr_set(GITERR_OS, "Failed to read symlink '%s'", file->path); - error = -1; - goto cleanup; - } - - map->len = read_len; - } - else { - git_file fd = git_futils_open_ro(path.ptr); - git_vector filters = GIT_VECTOR_INIT; - - if (fd < 0) { - error = fd; - goto cleanup; - } - - if (!file->size) - file->size = git_futils_filesize(fd); - - if ((error = diff_delta_is_binary_by_size(ctxt, delta, file)) < 0 || - delta->binary == 1) - goto close_and_cleanup; - - error = git_filters_load( - &filters, ctxt->repo, file->path, GIT_FILTER_TO_ODB); - if (error < 0) - goto close_and_cleanup; - - if (error == 0) { /* note: git_filters_load returns filter count */ - if (!file->size) - goto close_and_cleanup; - - error = git_futils_mmap_ro(map, fd, 0, (size_t)file->size); - file->flags |= GIT_DIFF_FILE_UNMAP_DATA; - } else { - git_buf raw = GIT_BUF_INIT, filtered = GIT_BUF_INIT; - - if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)file->size)) && - !(error = git_filters_apply(&filtered, &raw, &filters))) - { - map->len = git_buf_len(&filtered); - map->data = git_buf_detach(&filtered); - - file->flags |= GIT_DIFF_FILE_FREE_DATA; - } - - git_buf_free(&raw); - git_buf_free(&filtered); - } - -close_and_cleanup: - git_filters_free(&filters); - p_close(fd); - } - - /* once data is loaded, update OID if we didn't have it previously */ - if (!error && (file->flags & GIT_DIFF_FILE_VALID_OID) == 0) { - error = git_odb_hash( - &file->oid, map->data, map->len, GIT_OBJ_BLOB); - if (!error) - file->flags |= GIT_DIFF_FILE_VALID_OID; - } - - if (!error) - error = diff_delta_is_binary_by_content(ctxt, delta, file, map); - -cleanup: - git_buf_free(&path); - return error; -} - -static void release_content(git_diff_file *file, git_map *map, git_blob *blob) -{ - if (blob != NULL) - git_blob_free(blob); - - if (file->flags & GIT_DIFF_FILE_FREE_DATA) { - git__free(map->data); - map->data = ""; - map->len = 0; - file->flags &= ~GIT_DIFF_FILE_FREE_DATA; - } - else if (file->flags & GIT_DIFF_FILE_UNMAP_DATA) { - git_futils_mmap_free(map); - map->data = ""; - map->len = 0; - file->flags &= ~GIT_DIFF_FILE_UNMAP_DATA; - } -} - - -static void diff_context_init( - diff_context *ctxt, - git_diff_list *diff, - git_repository *repo, - const git_diff_options *opts, - git_diff_file_cb file_cb, - git_diff_hunk_cb hunk_cb, - git_diff_data_cb data_cb, - void *payload) -{ - memset(ctxt, 0, sizeof(diff_context)); - - ctxt->repo = repo; - ctxt->diff = diff; - ctxt->opts = opts; - ctxt->file_cb = file_cb; - ctxt->hunk_cb = hunk_cb; - ctxt->data_cb = data_cb; - ctxt->payload = payload; - ctxt->error = 0; - - setup_xdiff_options(ctxt->opts, &ctxt->xdiff_config, &ctxt->xdiff_params); -} - -static int diff_delta_file_callback( - diff_context *ctxt, git_diff_delta *delta, size_t idx) -{ - float progress; - - if (!ctxt->file_cb) - return 0; - - progress = ctxt->diff ? ((float)idx / ctxt->diff->deltas.length) : 1.0f; - - if (ctxt->file_cb(delta, progress, ctxt->payload) != 0) - ctxt->error = GIT_EUSER; - - return ctxt->error; -} - -static void diff_patch_init( - diff_context *ctxt, git_diff_patch *patch) -{ - memset(patch, 0, sizeof(git_diff_patch)); - - patch->diff = ctxt->diff; - patch->ctxt = ctxt; - - if (patch->diff) { - patch->old_src = patch->diff->old_src; - patch->new_src = patch->diff->new_src; - } else { - patch->old_src = patch->new_src = GIT_ITERATOR_TYPE_TREE; - } -} - -static git_diff_patch *diff_patch_alloc( - diff_context *ctxt, git_diff_delta *delta) -{ - git_diff_patch *patch = git__malloc(sizeof(git_diff_patch)); - if (!patch) - return NULL; - - diff_patch_init(ctxt, patch); - - git_diff_list_addref(patch->diff); - - GIT_REFCOUNT_INC(patch); - - patch->delta = delta; - patch->flags = GIT_DIFF_PATCH_ALLOCATED; - - return patch; -} - -static int diff_patch_load( - diff_context *ctxt, git_diff_patch *patch) -{ - int error = 0; - git_diff_delta *delta = patch->delta; - bool check_if_unmodified = false; - - if ((patch->flags & GIT_DIFF_PATCH_LOADED) != 0) - return 0; - - error = diff_delta_is_binary_by_attr(ctxt, patch); - - patch->old_data.data = ""; - patch->old_data.len = 0; - patch->old_blob = NULL; - - patch->new_data.data = ""; - patch->new_data.len = 0; - patch->new_blob = NULL; - - if (delta->binary == 1) - goto cleanup; - - if (!ctxt->hunk_cb && - !ctxt->data_cb && - (ctxt->opts->flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0) - goto cleanup; - - switch (delta->status) { - case GIT_DELTA_ADDED: - delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA; - break; - case GIT_DELTA_DELETED: - delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA; - break; - case GIT_DELTA_MODIFIED: - break; - case GIT_DELTA_UNTRACKED: - delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA; - if ((ctxt->opts->flags & GIT_DIFF_INCLUDE_UNTRACKED_CONTENT) == 0) - delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA; - break; - default: - delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA; - delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA; - break; - } - -#define CHECK_UNMODIFIED (GIT_DIFF_FILE_NO_DATA | GIT_DIFF_FILE_VALID_OID) - - check_if_unmodified = - (delta->old_file.flags & CHECK_UNMODIFIED) == 0 && - (delta->new_file.flags & CHECK_UNMODIFIED) == 0; - - /* Always try to load workdir content first, since it may need to be - * filtered (and hence use 2x memory) and we want to minimize the max - * memory footprint during diff. - */ - - if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 && - patch->old_src == GIT_ITERATOR_TYPE_WORKDIR) { - if ((error = get_workdir_content( - ctxt, delta, &delta->old_file, &patch->old_data)) < 0) - goto cleanup; - if (delta->binary == 1) - goto cleanup; - } - - if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 && - patch->new_src == GIT_ITERATOR_TYPE_WORKDIR) { - if ((error = get_workdir_content( - ctxt, delta, &delta->new_file, &patch->new_data)) < 0) - goto cleanup; - if (delta->binary == 1) - goto cleanup; - } - - if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 && - patch->old_src != GIT_ITERATOR_TYPE_WORKDIR) { - if ((error = get_blob_content( - ctxt, delta, &delta->old_file, - &patch->old_data, &patch->old_blob)) < 0) - goto cleanup; - if (delta->binary == 1) - goto cleanup; - } - - if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 && - patch->new_src != GIT_ITERATOR_TYPE_WORKDIR) { - if ((error = get_blob_content( - ctxt, delta, &delta->new_file, - &patch->new_data, &patch->new_blob)) < 0) - goto cleanup; - if (delta->binary == 1) - goto cleanup; - } - - /* if we did not previously have the definitive oid, we may have - * incorrect status and need to switch this to UNMODIFIED. - */ - if (check_if_unmodified && - delta->old_file.mode == delta->new_file.mode && - !git_oid_cmp(&delta->old_file.oid, &delta->new_file.oid)) - { - delta->status = GIT_DELTA_UNMODIFIED; - - if ((ctxt->opts->flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0) - goto cleanup; - } - -cleanup: - if (delta->binary == -1) - update_delta_is_binary(delta); - - if (!error) { - patch->flags |= GIT_DIFF_PATCH_LOADED; - - if (delta->binary != 1 && - delta->status != GIT_DELTA_UNMODIFIED && - (patch->old_data.len || patch->new_data.len) && - !git_oid_equal(&delta->old_file.oid, &delta->new_file.oid)) - patch->flags |= GIT_DIFF_PATCH_DIFFABLE; - } - - return error; -} - -static int diff_patch_cb(void *priv, mmbuffer_t *bufs, int len) -{ - git_diff_patch *patch = priv; - diff_context *ctxt = patch->ctxt; - - if (len == 1) { - ctxt->error = parse_hunk_header(&ctxt->range, bufs[0].ptr); - if (ctxt->error < 0) - return ctxt->error; - - if (ctxt->hunk_cb != NULL && - ctxt->hunk_cb(patch->delta, &ctxt->range, - bufs[0].ptr, bufs[0].size, ctxt->payload)) - ctxt->error = GIT_EUSER; - } - - if (len == 2 || len == 3) { - /* expect " "/"-"/"+", then data */ - char origin = - (*bufs[0].ptr == '+') ? GIT_DIFF_LINE_ADDITION : - (*bufs[0].ptr == '-') ? GIT_DIFF_LINE_DELETION : - GIT_DIFF_LINE_CONTEXT; - - if (ctxt->data_cb != NULL && - ctxt->data_cb(patch->delta, &ctxt->range, - origin, bufs[1].ptr, bufs[1].size, ctxt->payload)) - ctxt->error = GIT_EUSER; - } - - if (len == 3 && !ctxt->error) { - /* If we have a '+' and a third buf, then we have added a line - * without a newline and the old code had one, so DEL_EOFNL. - * If we have a '-' and a third buf, then we have removed a line - * with out a newline but added a blank line, so ADD_EOFNL. - */ - char origin = - (*bufs[0].ptr == '+') ? GIT_DIFF_LINE_DEL_EOFNL : - (*bufs[0].ptr == '-') ? GIT_DIFF_LINE_ADD_EOFNL : - GIT_DIFF_LINE_CONTEXT; - - if (ctxt->data_cb != NULL && - ctxt->data_cb(patch->delta, &ctxt->range, - origin, bufs[2].ptr, bufs[2].size, ctxt->payload)) - ctxt->error = GIT_EUSER; - } - - return ctxt->error; -} - -static int diff_patch_generate( - diff_context *ctxt, git_diff_patch *patch) -{ - int error = 0; - xdemitcb_t xdiff_callback; - mmfile_t old_xdiff_data, new_xdiff_data; - - if ((patch->flags & GIT_DIFF_PATCH_DIFFED) != 0) - return 0; - - if ((patch->flags & GIT_DIFF_PATCH_LOADED) == 0) - if ((error = diff_patch_load(ctxt, patch)) < 0) - return error; - - if ((patch->flags & GIT_DIFF_PATCH_DIFFABLE) == 0) - return 0; - - if (!ctxt->file_cb && !ctxt->hunk_cb) - return 0; - - patch->ctxt = ctxt; - - memset(&xdiff_callback, 0, sizeof(xdiff_callback)); - xdiff_callback.outf = diff_patch_cb; - xdiff_callback.priv = patch; - - old_xdiff_data.ptr = patch->old_data.data; - old_xdiff_data.size = patch->old_data.len; - new_xdiff_data.ptr = patch->new_data.data; - new_xdiff_data.size = patch->new_data.len; - - xdl_diff(&old_xdiff_data, &new_xdiff_data, - &ctxt->xdiff_params, &ctxt->xdiff_config, &xdiff_callback); - - error = ctxt->error; - - if (!error) - patch->flags |= GIT_DIFF_PATCH_DIFFED; - - return error; -} - -static void diff_patch_unload(git_diff_patch *patch) -{ - if ((patch->flags & GIT_DIFF_PATCH_DIFFED) != 0) { - patch->flags = (patch->flags & ~GIT_DIFF_PATCH_DIFFED); - - patch->hunks_size = 0; - patch->lines_size = 0; - } - - if ((patch->flags & GIT_DIFF_PATCH_LOADED) != 0) { - patch->flags = (patch->flags & ~GIT_DIFF_PATCH_LOADED); - - release_content( - &patch->delta->old_file, &patch->old_data, patch->old_blob); - release_content( - &patch->delta->new_file, &patch->new_data, patch->new_blob); - } -} - -static void diff_patch_free(git_diff_patch *patch) -{ - diff_patch_unload(patch); - - git__free(patch->lines); - patch->lines = NULL; - patch->lines_asize = 0; - - git__free(patch->hunks); - patch->hunks = NULL; - patch->hunks_asize = 0; - - if (!(patch->flags & GIT_DIFF_PATCH_ALLOCATED)) - return; - - patch->flags = 0; - - git_diff_list_free(patch->diff); /* decrements refcount */ - - git__free(patch); -} - -#define MAX_HUNK_STEP 128 -#define MIN_HUNK_STEP 8 -#define MAX_LINE_STEP 256 -#define MIN_LINE_STEP 8 - -static int diff_patch_hunk_cb( - const git_diff_delta *delta, - const git_diff_range *range, - const char *header, - size_t header_len, - void *payload) -{ - git_diff_patch *patch = payload; - diff_patch_hunk *hunk; - - GIT_UNUSED(delta); - - if (patch->hunks_size >= patch->hunks_asize) { - size_t new_size; - diff_patch_hunk *new_hunks; - - if (patch->hunks_asize > MAX_HUNK_STEP) - new_size = patch->hunks_asize + MAX_HUNK_STEP; - else - new_size = patch->hunks_asize * 2; - if (new_size < MIN_HUNK_STEP) - new_size = MIN_HUNK_STEP; - - new_hunks = git__realloc( - patch->hunks, new_size * sizeof(diff_patch_hunk)); - if (!new_hunks) - return -1; - - patch->hunks = new_hunks; - patch->hunks_asize = new_size; - } - - hunk = &patch->hunks[patch->hunks_size++]; - - memcpy(&hunk->range, range, sizeof(hunk->range)); - - assert(header_len + 1 < sizeof(hunk->header)); - memcpy(&hunk->header, header, header_len); - hunk->header[header_len] = '\0'; - hunk->header_len = header_len; - - hunk->line_start = patch->lines_size; - hunk->line_count = 0; - - patch->oldno = range->old_start; - patch->newno = range->new_start; - - return 0; -} - -static int diff_patch_line_cb( - const git_diff_delta *delta, - const git_diff_range *range, - char line_origin, - const char *content, - size_t content_len, - void *payload) -{ - git_diff_patch *patch = payload; - diff_patch_hunk *hunk; - diff_patch_line *line; - - GIT_UNUSED(delta); - GIT_UNUSED(range); - - assert(patch->hunks_size > 0); - assert(patch->hunks != NULL); - - hunk = &patch->hunks[patch->hunks_size - 1]; - - if (patch->lines_size >= patch->lines_asize) { - size_t new_size; - diff_patch_line *new_lines; - - if (patch->lines_asize > MAX_LINE_STEP) - new_size = patch->lines_asize + MAX_LINE_STEP; - else - new_size = patch->lines_asize * 2; - if (new_size < MIN_LINE_STEP) - new_size = MIN_LINE_STEP; - - new_lines = git__realloc( - patch->lines, new_size * sizeof(diff_patch_line)); - if (!new_lines) - return -1; - - patch->lines = new_lines; - patch->lines_asize = new_size; - } - - line = &patch->lines[patch->lines_size++]; - - line->ptr = content; - line->len = content_len; - line->origin = line_origin; - - /* do some bookkeeping so we can provide old/new line numbers */ - - for (line->lines = 0; content_len > 0; --content_len) { - if (*content++ == '\n') - ++line->lines; - } - - switch (line_origin) { - case GIT_DIFF_LINE_ADDITION: - line->oldno = -1; - line->newno = patch->newno; - patch->newno += line->lines; - break; - case GIT_DIFF_LINE_DELETION: - line->oldno = patch->oldno; - line->newno = -1; - patch->oldno += line->lines; - break; - default: - line->oldno = patch->oldno; - line->newno = patch->newno; - patch->oldno += line->lines; - patch->newno += line->lines; - break; - } - - hunk->line_count++; - - return 0; -} - - -int git_diff_foreach( - git_diff_list *diff, - git_diff_file_cb file_cb, - git_diff_hunk_cb hunk_cb, - git_diff_data_cb data_cb, - void *payload) -{ - int error = 0; - diff_context ctxt; - size_t idx; - git_diff_patch patch; - - diff_context_init( - &ctxt, diff, diff->repo, &diff->opts, - file_cb, hunk_cb, data_cb, payload); - - diff_patch_init(&ctxt, &patch); - - git_vector_foreach(&diff->deltas, idx, patch.delta) { - - /* check flags against patch status */ - if (git_diff_delta__should_skip(ctxt.opts, patch.delta)) - continue; - - if (!(error = diff_patch_load(&ctxt, &patch))) { - - /* invoke file callback */ - error = diff_delta_file_callback(&ctxt, patch.delta, idx); - - /* generate diffs and invoke hunk and line callbacks */ - if (!error) - error = diff_patch_generate(&ctxt, &patch); - - diff_patch_unload(&patch); - } - - if (error < 0) - break; - } - - if (error == GIT_EUSER) - giterr_clear(); /* don't let error message leak */ - - return error; -} - - -typedef struct { - git_diff_list *diff; - git_diff_data_cb print_cb; - void *payload; - git_buf *buf; -} diff_print_info; - -static char pick_suffix(int mode) -{ - if (S_ISDIR(mode)) - return '/'; - else if (mode & 0100) //-V536 - /* in git, modes are very regular, so we must have 0100755 mode */ - return '*'; - else - return ' '; -} - -char git_diff_status_char(git_delta_t status) -{ - char code; - - switch (status) { - case GIT_DELTA_ADDED: code = 'A'; break; - case GIT_DELTA_DELETED: code = 'D'; break; - case GIT_DELTA_MODIFIED: code = 'M'; break; - case GIT_DELTA_RENAMED: code = 'R'; break; - case GIT_DELTA_COPIED: code = 'C'; break; - case GIT_DELTA_IGNORED: code = 'I'; break; - case GIT_DELTA_UNTRACKED: code = '?'; break; - default: code = ' '; break; - } - - return code; -} - -static int print_compact( - const git_diff_delta *delta, float progress, void *data) -{ - diff_print_info *pi = data; - char old_suffix, new_suffix, code = git_diff_status_char(delta->status); - - GIT_UNUSED(progress); - - if (code == ' ') - return 0; - - old_suffix = pick_suffix(delta->old_file.mode); - new_suffix = pick_suffix(delta->new_file.mode); - - git_buf_clear(pi->buf); - - if (delta->old_file.path != delta->new_file.path && - pi->diff->strcomp(delta->old_file.path,delta->new_file.path) != 0) - git_buf_printf(pi->buf, "%c\t%s%c -> %s%c\n", code, - delta->old_file.path, old_suffix, delta->new_file.path, new_suffix); - else if (delta->old_file.mode != delta->new_file.mode && - delta->old_file.mode != 0 && delta->new_file.mode != 0) - git_buf_printf(pi->buf, "%c\t%s%c (%o -> %o)\n", code, - delta->old_file.path, new_suffix, delta->old_file.mode, delta->new_file.mode); - else if (old_suffix != ' ') - git_buf_printf(pi->buf, "%c\t%s%c\n", code, delta->old_file.path, old_suffix); - else - git_buf_printf(pi->buf, "%c\t%s\n", code, delta->old_file.path); - - if (git_buf_oom(pi->buf)) - return -1; - - if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR, - git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload)) - { - giterr_clear(); - return GIT_EUSER; - } - - return 0; -} - -int git_diff_print_compact( - git_diff_list *diff, - git_diff_data_cb print_cb, - void *payload) -{ - int error; - git_buf buf = GIT_BUF_INIT; - diff_print_info pi; - - pi.diff = diff; - pi.print_cb = print_cb; - pi.payload = payload; - pi.buf = &buf; - - error = git_diff_foreach(diff, print_compact, NULL, NULL, &pi); - - git_buf_free(&buf); - - return error; -} - -static int print_oid_range(diff_print_info *pi, const git_diff_delta *delta) -{ - char start_oid[8], end_oid[8]; - - /* TODO: Determine a good actual OID range to print */ - git_oid_tostr(start_oid, sizeof(start_oid), &delta->old_file.oid); - git_oid_tostr(end_oid, sizeof(end_oid), &delta->new_file.oid); - - /* TODO: Match git diff more closely */ - if (delta->old_file.mode == delta->new_file.mode) { - git_buf_printf(pi->buf, "index %s..%s %o\n", - start_oid, end_oid, delta->old_file.mode); - } else { - if (delta->old_file.mode == 0) { - git_buf_printf(pi->buf, "new file mode %o\n", delta->new_file.mode); - } else if (delta->new_file.mode == 0) { - git_buf_printf(pi->buf, "deleted file mode %o\n", delta->old_file.mode); - } else { - git_buf_printf(pi->buf, "old mode %o\n", delta->old_file.mode); - git_buf_printf(pi->buf, "new mode %o\n", delta->new_file.mode); - } - git_buf_printf(pi->buf, "index %s..%s\n", start_oid, end_oid); - } - - if (git_buf_oom(pi->buf)) - return -1; - - return 0; -} - -static int print_patch_file( - const git_diff_delta *delta, float progress, void *data) -{ - diff_print_info *pi = data; - const char *oldpfx = pi->diff->opts.old_prefix; - const char *oldpath = delta->old_file.path; - const char *newpfx = pi->diff->opts.new_prefix; - const char *newpath = delta->new_file.path; - - GIT_UNUSED(progress); - - if (S_ISDIR(delta->new_file.mode)) - return 0; - - if (!oldpfx) - oldpfx = DIFF_OLD_PREFIX_DEFAULT; - - if (!newpfx) - newpfx = DIFF_NEW_PREFIX_DEFAULT; - - git_buf_clear(pi->buf); - git_buf_printf(pi->buf, "diff --git %s%s %s%s\n", oldpfx, delta->old_file.path, newpfx, delta->new_file.path); - - if (print_oid_range(pi, delta) < 0) - return -1; - - if (git_oid_iszero(&delta->old_file.oid)) { - oldpfx = ""; - oldpath = "/dev/null"; - } - if (git_oid_iszero(&delta->new_file.oid)) { - newpfx = ""; - newpath = "/dev/null"; - } - - if (delta->binary != 1) { - git_buf_printf(pi->buf, "--- %s%s\n", oldpfx, oldpath); - git_buf_printf(pi->buf, "+++ %s%s\n", newpfx, newpath); - } - - if (git_buf_oom(pi->buf)) - return -1; - - if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR, - git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload)) - { - giterr_clear(); - return GIT_EUSER; - } - - if (delta->binary != 1) - return 0; - - git_buf_clear(pi->buf); - git_buf_printf( - pi->buf, "Binary files %s%s and %s%s differ\n", - oldpfx, oldpath, newpfx, newpath); - if (git_buf_oom(pi->buf)) - return -1; - - if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_BINARY, - git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload)) - { - giterr_clear(); - return GIT_EUSER; - } - - return 0; -} - -static int print_patch_hunk( - const git_diff_delta *d, - const git_diff_range *r, - const char *header, - size_t header_len, - void *data) -{ - diff_print_info *pi = data; - - if (S_ISDIR(d->new_file.mode)) - return 0; - - git_buf_clear(pi->buf); - if (git_buf_printf(pi->buf, "%.*s", (int)header_len, header) < 0) - return -1; - - if (pi->print_cb(d, r, GIT_DIFF_LINE_HUNK_HDR, - git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload)) - { - giterr_clear(); - return GIT_EUSER; - } - - return 0; -} - -static int print_patch_line( - const git_diff_delta *delta, - const git_diff_range *range, - char line_origin, /* GIT_DIFF_LINE value from above */ - const char *content, - size_t content_len, - void *data) -{ - diff_print_info *pi = data; - - if (S_ISDIR(delta->new_file.mode)) - return 0; - - git_buf_clear(pi->buf); - - if (line_origin == GIT_DIFF_LINE_ADDITION || - line_origin == GIT_DIFF_LINE_DELETION || - line_origin == GIT_DIFF_LINE_CONTEXT) - git_buf_printf(pi->buf, "%c%.*s", line_origin, (int)content_len, content); - else if (content_len > 0) - git_buf_printf(pi->buf, "%.*s", (int)content_len, content); - - if (git_buf_oom(pi->buf)) - return -1; - - if (pi->print_cb(delta, range, line_origin, - git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload)) - { - giterr_clear(); - return GIT_EUSER; - } - - return 0; -} - -int git_diff_print_patch( - git_diff_list *diff, - git_diff_data_cb print_cb, - void *payload) -{ - int error; - git_buf buf = GIT_BUF_INIT; - diff_print_info pi; - - pi.diff = diff; - pi.print_cb = print_cb; - pi.payload = payload; - pi.buf = &buf; - - error = git_diff_foreach( - diff, print_patch_file, print_patch_hunk, print_patch_line, &pi); - - git_buf_free(&buf); - - return error; -} - -static void set_data_from_blob( - const git_blob *blob, git_map *map, git_diff_file *file) -{ - if (blob) { - file->size = git_blob_rawsize(blob); - git_oid_cpy(&file->oid, git_object_id((const git_object *)blob)); - file->mode = 0644; - - map->len = (size_t)file->size; - map->data = (char *)git_blob_rawcontent(blob); - } else { - file->size = 0; - file->flags |= GIT_DIFF_FILE_NO_DATA; - - map->len = 0; - map->data = ""; - } -} - -static void set_data_from_buffer( - const char *buffer, size_t buffer_len, git_map *map, git_diff_file *file) -{ - file->size = (git_off_t)buffer_len; - file->mode = 0644; - - if (!buffer) - file->flags |= GIT_DIFF_FILE_NO_DATA; - else - git_odb_hash(&file->oid, buffer, buffer_len, GIT_OBJ_BLOB); - - map->len = buffer_len; - map->data = (char *)buffer; -} - -typedef struct { - diff_context ctxt; - git_diff_delta delta; - git_diff_patch patch; -} diff_single_data; - -static int diff_single_init( - diff_single_data *data, - git_repository *repo, - const git_diff_options *opts, - git_diff_file_cb file_cb, - git_diff_hunk_cb hunk_cb, - git_diff_data_cb data_cb, - void *payload) -{ - GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); - - memset(data, 0, sizeof(*data)); - - diff_context_init( - &data->ctxt, NULL, repo, opts, file_cb, hunk_cb, data_cb, payload); - - diff_patch_init(&data->ctxt, &data->patch); - - return 0; -} - -static int diff_single_apply(diff_single_data *data) -{ - int error; - git_diff_delta *delta = &data->delta; - bool has_old = ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0); - bool has_new = ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0); - - /* finish setting up fake git_diff_delta record and loaded data */ - - data->patch.delta = delta; - delta->binary = -1; - - delta->status = has_new ? - (has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) : - (has_old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED); - - if (git_oid_cmp(&delta->new_file.oid, &delta->old_file.oid) == 0) - delta->status = GIT_DELTA_UNMODIFIED; - - if ((error = diff_delta_is_binary_by_content( - &data->ctxt, delta, &delta->old_file, &data->patch.old_data)) < 0 || - (error = diff_delta_is_binary_by_content( - &data->ctxt, delta, &delta->new_file, &data->patch.new_data)) < 0) - goto cleanup; - - data->patch.flags |= GIT_DIFF_PATCH_LOADED; - - if (delta->binary != 1 && delta->status != GIT_DELTA_UNMODIFIED) - data->patch.flags |= GIT_DIFF_PATCH_DIFFABLE; - - /* do diffs */ - - if (!(error = diff_delta_file_callback(&data->ctxt, delta, 1))) - error = diff_patch_generate(&data->ctxt, &data->patch); - -cleanup: - if (error == GIT_EUSER) - giterr_clear(); - - diff_patch_unload(&data->patch); - - return error; -} - -int git_diff_blobs( - const git_blob *old_blob, - const git_blob *new_blob, - const git_diff_options *options, - git_diff_file_cb file_cb, - git_diff_hunk_cb hunk_cb, - git_diff_data_cb data_cb, - void *payload) -{ - int error; - diff_single_data d; - git_repository *repo = - new_blob ? git_object_owner((const git_object *)new_blob) : - old_blob ? git_object_owner((const git_object *)old_blob) : NULL; - - if ((error = diff_single_init( - &d, repo, options, file_cb, hunk_cb, data_cb, payload)) < 0) - return error; - - if (options && (options->flags & GIT_DIFF_REVERSE) != 0) { - const git_blob *swap = old_blob; - old_blob = new_blob; - new_blob = swap; - } - - set_data_from_blob(old_blob, &d.patch.old_data, &d.delta.old_file); - set_data_from_blob(new_blob, &d.patch.new_data, &d.delta.new_file); - - return diff_single_apply(&d); -} - -int git_diff_blob_to_buffer( - const git_blob *old_blob, - const char *buf, - size_t buflen, - const git_diff_options *options, - git_diff_file_cb file_cb, - git_diff_hunk_cb hunk_cb, - git_diff_data_cb data_cb, - void *payload) -{ - int error; - diff_single_data d; - git_repository *repo = - old_blob ? git_object_owner((const git_object *)old_blob) : NULL; - - if ((error = diff_single_init( - &d, repo, options, file_cb, hunk_cb, data_cb, payload)) < 0) - return error; - - if (options && (options->flags & GIT_DIFF_REVERSE) != 0) { - set_data_from_buffer(buf, buflen, &d.patch.old_data, &d.delta.old_file); - set_data_from_blob(old_blob, &d.patch.new_data, &d.delta.new_file); - } else { - set_data_from_blob(old_blob, &d.patch.old_data, &d.delta.old_file); - set_data_from_buffer(buf, buflen, &d.patch.new_data, &d.delta.new_file); - } - - return diff_single_apply(&d); -} - -size_t git_diff_num_deltas(git_diff_list *diff) -{ - assert(diff); - return (size_t)diff->deltas.length; -} - -size_t git_diff_num_deltas_of_type(git_diff_list *diff, git_delta_t type) -{ - size_t i, count = 0; - git_diff_delta *delta; - - assert(diff); - - git_vector_foreach(&diff->deltas, i, delta) { - count += (delta->status == type); - } - - return count; -} - -int git_diff_get_patch( - git_diff_patch **patch_ptr, - const git_diff_delta **delta_ptr, - git_diff_list *diff, - size_t idx) -{ - int error; - diff_context ctxt; - git_diff_delta *delta; - git_diff_patch *patch; - - if (patch_ptr) - *patch_ptr = NULL; - - delta = git_vector_get(&diff->deltas, idx); - if (!delta) { - if (delta_ptr) - *delta_ptr = NULL; - giterr_set(GITERR_INVALID, "Index out of range for delta in diff"); - return GIT_ENOTFOUND; - } - - if (delta_ptr) - *delta_ptr = delta; - - if (!patch_ptr && - (delta->binary != -1 || - (diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0)) - return 0; - - diff_context_init( - &ctxt, diff, diff->repo, &diff->opts, - NULL, diff_patch_hunk_cb, diff_patch_line_cb, NULL); - - if (git_diff_delta__should_skip(ctxt.opts, delta)) - return 0; - - patch = diff_patch_alloc(&ctxt, delta); - if (!patch) - return -1; - - if (!(error = diff_patch_load(&ctxt, patch))) { - ctxt.payload = patch; - - error = diff_patch_generate(&ctxt, patch); - - if (error == GIT_EUSER) - error = ctxt.error; - } - - if (error) - git_diff_patch_free(patch); - else if (patch_ptr) - *patch_ptr = patch; - - return error; -} - -void git_diff_patch_free(git_diff_patch *patch) -{ - if (patch) - GIT_REFCOUNT_DEC(patch, diff_patch_free); -} - -const git_diff_delta *git_diff_patch_delta(git_diff_patch *patch) -{ - assert(patch); - return patch->delta; -} - -size_t git_diff_patch_num_hunks(git_diff_patch *patch) -{ - assert(patch); - return patch->hunks_size; -} - -int git_diff_patch_line_stats( - size_t *total_ctxt, - size_t *total_adds, - size_t *total_dels, - const git_diff_patch *patch) -{ - size_t totals[3], idx; - - memset(totals, 0, sizeof(totals)); - - for (idx = 0; idx < patch->lines_size; ++idx) { - switch (patch->lines[idx].origin) { - case GIT_DIFF_LINE_CONTEXT: totals[0]++; break; - case GIT_DIFF_LINE_ADDITION: totals[1]++; break; - case GIT_DIFF_LINE_DELETION: totals[2]++; break; - default: - /* diff --stat and --numstat don't count EOFNL marks because - * they will always be paired with a ADDITION or DELETION line. - */ - break; - } - } - - if (total_ctxt) - *total_ctxt = totals[0]; - if (total_adds) - *total_adds = totals[1]; - if (total_dels) - *total_dels = totals[2]; - - return 0; -} - -int git_diff_patch_get_hunk( - const git_diff_range **range, - const char **header, - size_t *header_len, - size_t *lines_in_hunk, - git_diff_patch *patch, - size_t hunk_idx) -{ - diff_patch_hunk *hunk; - - assert(patch); - - if (hunk_idx >= patch->hunks_size) { - if (range) *range = NULL; - if (header) *header = NULL; - if (header_len) *header_len = 0; - if (lines_in_hunk) *lines_in_hunk = 0; - return GIT_ENOTFOUND; - } - - hunk = &patch->hunks[hunk_idx]; - - if (range) *range = &hunk->range; - if (header) *header = hunk->header; - if (header_len) *header_len = hunk->header_len; - if (lines_in_hunk) *lines_in_hunk = hunk->line_count; - - return 0; -} - -int git_diff_patch_num_lines_in_hunk( - git_diff_patch *patch, - size_t hunk_idx) -{ - assert(patch); - - if (hunk_idx >= patch->hunks_size) - return GIT_ENOTFOUND; - else - return (int)patch->hunks[hunk_idx].line_count; -} - -int git_diff_patch_get_line_in_hunk( - char *line_origin, - const char **content, - size_t *content_len, - int *old_lineno, - int *new_lineno, - git_diff_patch *patch, - size_t hunk_idx, - size_t line_of_hunk) -{ - diff_patch_hunk *hunk; - diff_patch_line *line; - - assert(patch); - - if (hunk_idx >= patch->hunks_size) - goto notfound; - hunk = &patch->hunks[hunk_idx]; - - if (line_of_hunk >= hunk->line_count) - goto notfound; - - line = &patch->lines[hunk->line_start + line_of_hunk]; - - if (line_origin) *line_origin = line->origin; - if (content) *content = line->ptr; - if (content_len) *content_len = line->len; - if (old_lineno) *old_lineno = line->oldno; - if (new_lineno) *new_lineno = line->newno; - - return 0; - -notfound: - if (line_origin) *line_origin = GIT_DIFF_LINE_CONTEXT; - if (content) *content = NULL; - if (content_len) *content_len = 0; - if (old_lineno) *old_lineno = -1; - if (new_lineno) *new_lineno = -1; - - return GIT_ENOTFOUND; -} - -static int print_to_buffer_cb( - const git_diff_delta *delta, - const git_diff_range *range, - char line_origin, - const char *content, - size_t content_len, - void *payload) -{ - git_buf *output = payload; - GIT_UNUSED(delta); GIT_UNUSED(range); GIT_UNUSED(line_origin); - return git_buf_put(output, content, content_len); -} - -int git_diff_patch_print( - git_diff_patch *patch, - git_diff_data_cb print_cb, - void *payload) -{ - int error; - git_buf temp = GIT_BUF_INIT; - diff_print_info pi; - size_t h, l; - - assert(patch && print_cb); - - pi.diff = patch->diff; - pi.print_cb = print_cb; - pi.payload = payload; - pi.buf = &temp; - - error = print_patch_file(patch->delta, 0, &pi); - - for (h = 0; h < patch->hunks_size && !error; ++h) { - diff_patch_hunk *hunk = &patch->hunks[h]; - - error = print_patch_hunk( - patch->delta, &hunk->range, hunk->header, hunk->header_len, &pi); - - for (l = 0; l < hunk->line_count && !error; ++l) { - diff_patch_line *line = &patch->lines[hunk->line_start + l]; - - error = print_patch_line( - patch->delta, &hunk->range, - line->origin, line->ptr, line->len, &pi); - } - } - - git_buf_free(&temp); - - return error; -} - -int git_diff_patch_to_str( - char **string, - git_diff_patch *patch) -{ - int error; - git_buf output = GIT_BUF_INIT; - - error = git_diff_patch_print(patch, print_to_buffer_cb, &output); - - /* GIT_EUSER means git_buf_put in print_to_buffer_cb returned -1, - * meaning a memory allocation failure, so just map to -1... - */ - if (error == GIT_EUSER) - error = -1; - - *string = git_buf_detach(&output); - - return error; -} - -int git_diff__paired_foreach( - git_diff_list *idx2head, - git_diff_list *wd2idx, - int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload), - void *payload) -{ - int cmp; - git_diff_delta *i2h, *w2i; - size_t i, j, i_max, j_max; - int (*strcomp)(const char *, const char *); - - i_max = idx2head ? idx2head->deltas.length : 0; - j_max = wd2idx ? wd2idx->deltas.length : 0; - - /* Get appropriate strcmp function */ - strcomp = idx2head ? idx2head->strcomp : wd2idx ? wd2idx->strcomp : NULL; - - /* Assert both iterators use matching ignore-case. If this function ever - * supports merging diffs that are not sorted by the same function, then - * it will need to spool and sort on one of the results before merging - */ - if (idx2head && wd2idx) { - assert(idx2head->strcomp == wd2idx->strcomp); - } - - for (i = 0, j = 0; i < i_max || j < j_max; ) { - i2h = idx2head ? GIT_VECTOR_GET(&idx2head->deltas,i) : NULL; - w2i = wd2idx ? GIT_VECTOR_GET(&wd2idx->deltas,j) : NULL; - - cmp = !w2i ? -1 : !i2h ? 1 : - strcomp(i2h->old_file.path, w2i->old_file.path); - - if (cmp < 0) { - if (cb(i2h, NULL, payload)) - return GIT_EUSER; - i++; - } else if (cmp > 0) { - if (cb(NULL, w2i, payload)) - return GIT_EUSER; - j++; - } else { - if (cb(i2h, w2i, payload)) - return GIT_EUSER; - i++; j++; - } - } - - return 0; -} diff --git a/src/diff_output.h b/src/diff_output.h deleted file mode 100644 index ae1a491ec24..00000000000 --- a/src/diff_output.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_diff_output_h__ -#define INCLUDE_diff_output_h__ - -#include "git2/blob.h" -#include "diff.h" -#include "map.h" -#include "xdiff/xdiff.h" - -#define MAX_DIFF_FILESIZE 0x20000000 - -enum { - GIT_DIFF_PATCH_ALLOCATED = (1 << 0), - GIT_DIFF_PATCH_PREPPED = (1 << 1), - GIT_DIFF_PATCH_LOADED = (1 << 2), - GIT_DIFF_PATCH_DIFFABLE = (1 << 3), - GIT_DIFF_PATCH_DIFFED = (1 << 4), -}; - -/* context for performing diffs */ -typedef struct { - git_repository *repo; - git_diff_list *diff; - const git_diff_options *opts; - git_diff_file_cb file_cb; - git_diff_hunk_cb hunk_cb; - git_diff_data_cb data_cb; - void *payload; - int error; - git_diff_range range; - xdemitconf_t xdiff_config; - xpparam_t xdiff_params; -} diff_context; - -/* cached information about a single span in a diff */ -typedef struct diff_patch_line diff_patch_line; -struct diff_patch_line { - const char *ptr; - size_t len; - int lines, oldno, newno; - char origin; -}; - -/* cached information about a hunk in a diff */ -typedef struct diff_patch_hunk diff_patch_hunk; -struct diff_patch_hunk { - git_diff_range range; - char header[128]; - size_t header_len; - size_t line_start; - size_t line_count; -}; - -struct git_diff_patch { - git_refcount rc; - git_diff_list *diff; /* for refcount purposes, maybe NULL for blob diffs */ - git_diff_delta *delta; - diff_context *ctxt; /* only valid while generating patch */ - git_iterator_type_t old_src; - git_iterator_type_t new_src; - git_blob *old_blob; - git_blob *new_blob; - git_map old_data; - git_map new_data; - uint32_t flags; - diff_patch_hunk *hunks; - size_t hunks_asize, hunks_size; - diff_patch_line *lines; - size_t lines_asize, lines_size; - size_t oldno, newno; -}; - -/* context for performing diff on a single delta */ -typedef struct { - git_diff_patch *patch; - uint32_t prepped : 1; - uint32_t loaded : 1; - uint32_t diffable : 1; - uint32_t diffed : 1; -} diff_delta_context; - -extern int git_diff__paired_foreach( - git_diff_list *idx2head, - git_diff_list *wd2idx, - int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload), - void *payload); - -#endif diff --git a/src/diff_tform.c b/src/diff_tform.c deleted file mode 100644 index 2c2e1fb19fd..00000000000 --- a/src/diff_tform.c +++ /dev/null @@ -1,482 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "common.h" -#include "diff.h" -#include "git2/config.h" - -static git_diff_delta *diff_delta__dup( - const git_diff_delta *d, git_pool *pool) -{ - git_diff_delta *delta = git__malloc(sizeof(git_diff_delta)); - if (!delta) - return NULL; - - memcpy(delta, d, sizeof(git_diff_delta)); - - delta->old_file.path = git_pool_strdup(pool, d->old_file.path); - if (delta->old_file.path == NULL) - goto fail; - - if (d->new_file.path != d->old_file.path) { - delta->new_file.path = git_pool_strdup(pool, d->new_file.path); - if (delta->new_file.path == NULL) - goto fail; - } else { - delta->new_file.path = delta->old_file.path; - } - - return delta; - -fail: - git__free(delta); - return NULL; -} - -static git_diff_delta *diff_delta__merge_like_cgit( - const git_diff_delta *a, const git_diff_delta *b, git_pool *pool) -{ - git_diff_delta *dup; - - /* Emulate C git for merging two diffs (a la 'git diff '). - * - * When C git does a diff between the work dir and a tree, it actually - * diffs with the index but uses the workdir contents. This emulates - * those choices so we can emulate the type of diff. - * - * We have three file descriptions here, let's call them: - * f1 = a->old_file - * f2 = a->new_file AND b->old_file - * f3 = b->new_file - */ - - /* if f2 == f3 or f2 is deleted, then just dup the 'a' diff */ - if (b->status == GIT_DELTA_UNMODIFIED || a->status == GIT_DELTA_DELETED) - return diff_delta__dup(a, pool); - - /* otherwise, base this diff on the 'b' diff */ - if ((dup = diff_delta__dup(b, pool)) == NULL) - return NULL; - - /* If 'a' status is uninteresting, then we're done */ - if (a->status == GIT_DELTA_UNMODIFIED) - return dup; - - assert(a->status != GIT_DELTA_UNMODIFIED); - assert(b->status != GIT_DELTA_UNMODIFIED); - - /* A cgit exception is that the diff of a file that is only in the - * index (i.e. not in HEAD nor workdir) is given as empty. - */ - if (dup->status == GIT_DELTA_DELETED) { - if (a->status == GIT_DELTA_ADDED) - dup->status = GIT_DELTA_UNMODIFIED; - /* else don't overwrite DELETE status */ - } else { - dup->status = a->status; - } - - git_oid_cpy(&dup->old_file.oid, &a->old_file.oid); - dup->old_file.mode = a->old_file.mode; - dup->old_file.size = a->old_file.size; - dup->old_file.flags = a->old_file.flags; - - return dup; -} - -int git_diff_merge( - git_diff_list *onto, - const git_diff_list *from) -{ - int error = 0; - git_pool onto_pool; - git_vector onto_new; - git_diff_delta *delta; - bool ignore_case = false; - unsigned int i, j; - - assert(onto && from); - - if (!from->deltas.length) - return 0; - - if (git_vector_init( - &onto_new, onto->deltas.length, git_diff_delta__cmp) < 0 || - git_pool_init(&onto_pool, 1, 0) < 0) - return -1; - - if ((onto->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0 || - (from->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0) - { - ignore_case = true; - - /* This function currently only supports merging diff lists that - * are sorted identically. */ - assert((onto->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0 && - (from->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0); - } - - for (i = 0, j = 0; i < onto->deltas.length || j < from->deltas.length; ) { - git_diff_delta *o = GIT_VECTOR_GET(&onto->deltas, i); - const git_diff_delta *f = GIT_VECTOR_GET(&from->deltas, j); - int cmp = !f ? -1 : !o ? 1 : STRCMP_CASESELECT(ignore_case, o->old_file.path, f->old_file.path); - - if (cmp < 0) { - delta = diff_delta__dup(o, &onto_pool); - i++; - } else if (cmp > 0) { - delta = diff_delta__dup(f, &onto_pool); - j++; - } else { - delta = diff_delta__merge_like_cgit(o, f, &onto_pool); - i++; - j++; - } - - /* the ignore rules for the target may not match the source - * or the result of a merged delta could be skippable... - */ - if (git_diff_delta__should_skip(&onto->opts, delta)) { - git__free(delta); - continue; - } - - if ((error = !delta ? -1 : git_vector_insert(&onto_new, delta)) < 0) - break; - } - - if (!error) { - git_vector_swap(&onto->deltas, &onto_new); - git_pool_swap(&onto->pool, &onto_pool); - onto->new_src = from->new_src; - - /* prefix strings also come from old pool, so recreate those.*/ - onto->opts.old_prefix = - git_pool_strdup_safe(&onto->pool, onto->opts.old_prefix); - onto->opts.new_prefix = - git_pool_strdup_safe(&onto->pool, onto->opts.new_prefix); - } - - git_vector_foreach(&onto_new, i, delta) - git__free(delta); - git_vector_free(&onto_new); - git_pool_clear(&onto_pool); - - return error; -} - -#define DEFAULT_THRESHOLD 50 -#define DEFAULT_BREAK_REWRITE_THRESHOLD 60 -#define DEFAULT_TARGET_LIMIT 200 - -static int normalize_find_opts( - git_diff_list *diff, - git_diff_find_options *opts, - git_diff_find_options *given) -{ - git_config *cfg = NULL; - const char *val; - - if (diff->repo != NULL && - git_repository_config__weakptr(&cfg, diff->repo) < 0) - return -1; - - if (given != NULL) - memcpy(opts, given, sizeof(*opts)); - else { - git_diff_find_options init = GIT_DIFF_FIND_OPTIONS_INIT; - memmove(opts, &init, sizeof(init)); - - opts->flags = GIT_DIFF_FIND_RENAMES; - - if (git_config_get_string(&val, cfg, "diff.renames") < 0) - giterr_clear(); - else if (val && - (!strcasecmp(val, "copies") || !strcasecmp(val, "copy"))) - opts->flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES; - } - - GITERR_CHECK_VERSION(opts, GIT_DIFF_FIND_OPTIONS_VERSION, "git_diff_find_options"); - - /* some flags imply others */ - - if (opts->flags & GIT_DIFF_FIND_RENAMES_FROM_REWRITES) - opts->flags |= GIT_DIFF_FIND_RENAMES; - - if (opts->flags & GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED) - opts->flags |= GIT_DIFF_FIND_COPIES; - -#define USE_DEFAULT(X) ((X) == 0 || (X) > 100) - - if (USE_DEFAULT(opts->rename_threshold)) - opts->rename_threshold = DEFAULT_THRESHOLD; - - if (USE_DEFAULT(opts->rename_from_rewrite_threshold)) - opts->rename_from_rewrite_threshold = DEFAULT_THRESHOLD; - - if (USE_DEFAULT(opts->copy_threshold)) - opts->copy_threshold = DEFAULT_THRESHOLD; - - if (USE_DEFAULT(opts->break_rewrite_threshold)) - opts->break_rewrite_threshold = DEFAULT_BREAK_REWRITE_THRESHOLD; - -#undef USE_DEFAULT - - if (!opts->target_limit) { - int32_t limit = 0; - - opts->target_limit = DEFAULT_TARGET_LIMIT; - - if (git_config_get_int32(&limit, cfg, "diff.renameLimit") < 0) - giterr_clear(); - else if (limit > 0) - opts->target_limit = limit; - } - - return 0; -} - -static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size) -{ - git_vector onto = GIT_VECTOR_INIT; - size_t i; - git_diff_delta *delta; - - if (git_vector_init(&onto, expected_size, git_diff_delta__cmp) < 0) - return -1; - - /* build new delta list without TO_DELETE and splitting TO_SPLIT */ - git_vector_foreach(&diff->deltas, i, delta) { - if (delta->status == GIT_DELTA__TO_DELETE) - continue; - - if (delta->status == GIT_DELTA__TO_SPLIT) { - git_diff_delta *deleted = diff_delta__dup(delta, &diff->pool); - if (!deleted) - goto on_error; - - deleted->status = GIT_DELTA_DELETED; - memset(&deleted->new_file, 0, sizeof(deleted->new_file)); - deleted->new_file.path = deleted->old_file.path; - deleted->new_file.flags |= GIT_DIFF_FILE_VALID_OID; - - if (git_vector_insert(&onto, deleted) < 0) - goto on_error; - - delta->status = GIT_DELTA_ADDED; - memset(&delta->old_file, 0, sizeof(delta->old_file)); - delta->old_file.path = delta->new_file.path; - delta->old_file.flags |= GIT_DIFF_FILE_VALID_OID; - } - - if (git_vector_insert(&onto, delta) < 0) - goto on_error; - } - - /* cannot return an error past this point */ - git_vector_foreach(&diff->deltas, i, delta) - if (delta->status == GIT_DELTA__TO_DELETE) - git__free(delta); - - /* swap new delta list into place */ - git_vector_sort(&onto); - git_vector_swap(&diff->deltas, &onto); - git_vector_free(&onto); - - return 0; - -on_error: - git_vector_foreach(&onto, i, delta) - git__free(delta); - - git_vector_free(&onto); - - return -1; -} - -static unsigned int calc_similarity( - void *cache, git_diff_file *old_file, git_diff_file *new_file) -{ - GIT_UNUSED(cache); - - if (git_oid_cmp(&old_file->oid, &new_file->oid) == 0) - return 100; - - /* TODO: insert actual similarity algo here */ - - return 0; -} - -#define FLAG_SET(opts,flag_name) ((opts.flags & flag_name) != 0) - -int git_diff_find_similar( - git_diff_list *diff, - git_diff_find_options *given_opts) -{ - unsigned int i, j, similarity; - git_diff_delta *from, *to; - git_diff_find_options opts; - unsigned int tried_targets, num_changes = 0; - git_vector matches = GIT_VECTOR_INIT; - - if (normalize_find_opts(diff, &opts, given_opts) < 0) - return -1; - - /* first do splits if requested */ - - if (FLAG_SET(opts, GIT_DIFF_FIND_AND_BREAK_REWRITES)) { - git_vector_foreach(&diff->deltas, i, from) { - if (from->status != GIT_DELTA_MODIFIED) - continue; - - /* Right now, this doesn't work right because the similarity - * algorithm isn't actually implemented... - */ - similarity = 100; - /* calc_similarity(NULL, &from->old_file, from->new_file); */ - - if (similarity < opts.break_rewrite_threshold) { - from->status = GIT_DELTA__TO_SPLIT; - num_changes++; - } - } - - /* apply splits as needed */ - if (num_changes > 0 && - apply_splits_and_deletes( - diff, diff->deltas.length + num_changes) < 0) - return -1; - } - - /* next find the most similar delta for each rename / copy candidate */ - - if (git_vector_init(&matches, diff->deltas.length, git_diff_delta__cmp) < 0) - return -1; - - git_vector_foreach(&diff->deltas, i, from) { - tried_targets = 0; - - git_vector_foreach(&diff->deltas, j, to) { - if (i == j) - continue; - - switch (to->status) { - case GIT_DELTA_ADDED: - case GIT_DELTA_UNTRACKED: - case GIT_DELTA_RENAMED: - case GIT_DELTA_COPIED: - break; - default: - /* only the above status values should be checked */ - continue; - } - - /* skip all but DELETED files unless copy detection is on */ - if (from->status != GIT_DELTA_DELETED && - !FLAG_SET(opts, GIT_DIFF_FIND_COPIES)) - continue; - - /* don't check UNMODIFIED files as source unless given option */ - if (from->status == GIT_DELTA_UNMODIFIED && - !FLAG_SET(opts, GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED)) - continue; - - /* cap on maximum files we'll examine */ - if (++tried_targets > opts.target_limit) - break; - - /* calculate similarity and see if this pair beats the - * similarity score of the current best pair. - */ - similarity = calc_similarity(NULL, &from->old_file, &to->new_file); - - if (to->similarity < similarity) { - to->similarity = similarity; - if (git_vector_set(NULL, &matches, j, from) < 0) - return -1; - } - } - } - - /* next rewrite the diffs with renames / copies */ - - num_changes = 0; - - git_vector_foreach(&diff->deltas, j, to) { - from = GIT_VECTOR_GET(&matches, j); - if (!from) { - assert(to->similarity == 0); - continue; - } - - /* three possible outcomes here: - * 1. old DELETED and if over rename threshold, - * new becomes RENAMED and old goes away - * 2. old was MODIFIED but FIND_RENAMES_FROM_REWRITES is on and - * old is more similar to new than it is to itself, in which - * case, new becomes RENAMED and old becomed ADDED - * 3. otherwise if over copy threshold, new becomes COPIED - */ - - if (from->status == GIT_DELTA_DELETED) { - if (to->similarity < opts.rename_threshold) { - to->similarity = 0; - continue; - } - - to->status = GIT_DELTA_RENAMED; - memcpy(&to->old_file, &from->old_file, sizeof(to->old_file)); - - from->status = GIT_DELTA__TO_DELETE; - num_changes++; - - continue; - } - - if (from->status == GIT_DELTA_MODIFIED && - FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES) && - to->similarity > opts.rename_threshold) - { - similarity = 100; - /* calc_similarity(NULL, &from->old_file, from->new_file); */ - - if (similarity < opts.rename_from_rewrite_threshold) { - to->status = GIT_DELTA_RENAMED; - memcpy(&to->old_file, &from->old_file, sizeof(to->old_file)); - - from->status = GIT_DELTA_ADDED; - memset(&from->old_file, 0, sizeof(from->old_file)); - from->old_file.path = to->old_file.path; - from->old_file.flags |= GIT_DIFF_FILE_VALID_OID; - - continue; - } - } - - if (to->similarity < opts.copy_threshold) { - to->similarity = 0; - continue; - } - - /* convert "to" to a COPIED record */ - to->status = GIT_DELTA_COPIED; - memcpy(&to->old_file, &from->old_file, sizeof(to->old_file)); - } - - git_vector_free(&matches); - - if (num_changes > 0) { - assert(num_changes < diff->deltas.length); - - if (apply_splits_and_deletes( - diff, diff->deltas.length - num_changes) < 0) - return -1; - } - - return 0; -} - -#undef FLAG_SET diff --git a/src/errors.c b/src/errors.c deleted file mode 100644 index 3aa1757b208..00000000000 --- a/src/errors.c +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "common.h" -#include "global.h" -#include "posix.h" -#include "buffer.h" -#include - -/******************************************** - * New error handling - ********************************************/ - -static git_error g_git_oom_error = { - "Out of memory", - GITERR_NOMEMORY -}; - -static void set_error(int error_class, char *string) -{ - git_error *error = &GIT_GLOBAL->error_t; - - git__free(error->message); - - error->message = string; - error->klass = error_class; - - GIT_GLOBAL->last_error = error; -} - -void giterr_set_oom(void) -{ - GIT_GLOBAL->last_error = &g_git_oom_error; -} - -void giterr_set(int error_class, const char *string, ...) -{ - git_buf buf = GIT_BUF_INIT; - va_list arglist; -#ifdef GIT_WIN32 - DWORD win32_error_code = (error_class == GITERR_OS) ? GetLastError() : 0; -#endif - int error_code = (error_class == GITERR_OS) ? errno : 0; - - va_start(arglist, string); - git_buf_vprintf(&buf, string, arglist); - va_end(arglist); - - if (error_class == GITERR_OS) { -#ifdef GIT_WIN32 - char * win32_error = git_win32_get_error_message(win32_error_code); - if (win32_error) { - git_buf_PUTS(&buf, ": "); - git_buf_puts(&buf, win32_error); - git__free(win32_error); - - SetLastError(0); - } - else -#endif - if (error_code) { - git_buf_PUTS(&buf, ": "); - git_buf_puts(&buf, strerror(error_code)); - } - - if (error_code) - errno = 0; - } - - if (!git_buf_oom(&buf)) - set_error(error_class, git_buf_detach(&buf)); -} - -void giterr_set_str(int error_class, const char *string) -{ - char *message; - - assert(string); - - message = git__strdup(string); - - if (message) - set_error(error_class, message); -} - -int giterr_set_regex(const regex_t *regex, int error_code) -{ - char error_buf[1024]; - regerror(error_code, regex, error_buf, sizeof(error_buf)); - giterr_set_str(GITERR_REGEX, error_buf); - - if (error_code == REG_NOMATCH) - return GIT_ENOTFOUND; - else if (error_code > REG_BADPAT) - return GIT_EINVALIDSPEC; - else - return -1; -} - -void giterr_clear(void) -{ - GIT_GLOBAL->last_error = NULL; - - errno = 0; -#ifdef GIT_WIN32 - SetLastError(0); -#endif -} - -const git_error *giterr_last(void) -{ - return GIT_GLOBAL->last_error; -} - diff --git a/src/fetch.c b/src/fetch.c deleted file mode 100644 index b60a95232ef..00000000000 --- a/src/fetch.c +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "git2/oid.h" -#include "git2/refs.h" -#include "git2/revwalk.h" -#include "git2/transport.h" - -#include "common.h" -#include "remote.h" -#include "refspec.h" -#include "pack.h" -#include "fetch.h" -#include "netops.h" - -struct filter_payload { - git_remote *remote; - const git_refspec *spec, *tagspec; - git_odb *odb; - int found_head; -}; - -static int filter_ref__cb(git_remote_head *head, void *payload) -{ - struct filter_payload *p = payload; - int match = 0; - - if (!git_reference_is_valid_name(head->name)) - return 0; - - if (!p->found_head && strcmp(head->name, GIT_HEAD_FILE) == 0) - p->found_head = 1; - else if (git_refspec_src_matches(p->spec, head->name)) - match = 1; - else if (p->remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL && - git_refspec_src_matches(p->tagspec, head->name)) - match = 1; - - if (!match) - return 0; - - /* If we have the object, mark it so we don't ask for it */ - if (git_odb_exists(p->odb, &head->oid)) - head->local = 1; - else - p->remote->need_pack = 1; - - return git_vector_insert(&p->remote->refs, head); -} - -static int filter_wants(git_remote *remote) -{ - struct filter_payload p; - git_refspec tagspec; - int error = -1; - - git_vector_clear(&remote->refs); - if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0) - return error; - - /* - * The fetch refspec can be NULL, and what this means is that the - * user didn't specify one. This is fine, as it means that we're - * not interested in any particular branch but just the remote's - * HEAD, which will be stored in FETCH_HEAD after the fetch. - */ - p.spec = git_remote_fetchspec(remote); - p.tagspec = &tagspec; - p.found_head = 0; - p.remote = remote; - - if (git_repository_odb__weakptr(&p.odb, remote->repo) < 0) - goto cleanup; - - error = git_remote_ls(remote, filter_ref__cb, &p); - -cleanup: - git_refspec__free(&tagspec); - - return error; -} - -/* - * In this first version, we push all our refs in and start sending - * them out. When we get an ACK we hide that commit and continue - * traversing until we're done - */ -int git_fetch_negotiate(git_remote *remote) -{ - git_transport *t = remote->transport; - - if (filter_wants(remote) < 0) { - giterr_set(GITERR_NET, "Failed to filter the reference list for wants"); - return -1; - } - - /* Don't try to negotiate when we don't want anything */ - if (remote->refs.length == 0 || !remote->need_pack) - return 0; - - /* - * Now we have everything set up so we can start tell the - * server what we want and what we have. - */ - return t->negotiate_fetch(t, - remote->repo, - (const git_remote_head * const *)remote->refs.contents, - remote->refs.length); -} - -int git_fetch_download_pack( - git_remote *remote, - git_transfer_progress_callback progress_cb, - void *progress_payload) -{ - git_transport *t = remote->transport; - - if(!remote->need_pack) - return 0; - - return t->download_pack(t, remote->repo, &remote->stats, progress_cb, progress_payload); -} diff --git a/src/fetch.h b/src/fetch.h deleted file mode 100644 index 059251d04ae..00000000000 --- a/src/fetch.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_fetch_h__ -#define INCLUDE_fetch_h__ - -#include "netops.h" - -int git_fetch_negotiate(git_remote *remote); - -int git_fetch_download_pack( - git_remote *remote, - git_transfer_progress_callback progress_cb, - void *progress_payload); - -int git_fetch__download_pack( - git_transport *t, - git_repository *repo, - git_transfer_progress *stats, - git_transfer_progress_callback progress_cb, - void *progress_payload); - -int git_fetch_setup_walk(git_revwalk **out, git_repository *repo); - -#endif diff --git a/src/fetchhead.c b/src/fetchhead.c deleted file mode 100644 index 6e8fb9faccd..00000000000 --- a/src/fetchhead.c +++ /dev/null @@ -1,296 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "git2/types.h" -#include "git2/oid.h" - -#include "fetchhead.h" -#include "common.h" -#include "buffer.h" -#include "fileops.h" -#include "filebuf.h" -#include "refs.h" -#include "repository.h" - - -int git_fetchhead_ref_cmp(const void *a, const void *b) -{ - const git_fetchhead_ref *one = (const git_fetchhead_ref *)a; - const git_fetchhead_ref *two = (const git_fetchhead_ref *)b; - - if (one->is_merge && !two->is_merge) - return -1; - if (two->is_merge && !one->is_merge) - return 1; - - if (one->ref_name && two->ref_name) - return strcmp(one->ref_name, two->ref_name); - else if (one->ref_name) - return -1; - else if (two->ref_name) - return 1; - - return 0; -} - -int git_fetchhead_ref_create( - git_fetchhead_ref **out, - git_oid *oid, - unsigned int is_merge, - const char *ref_name, - const char *remote_url) -{ - git_fetchhead_ref *fetchhead_ref; - - assert(out && oid); - - *out = NULL; - - fetchhead_ref = git__malloc(sizeof(git_fetchhead_ref)); - GITERR_CHECK_ALLOC(fetchhead_ref); - - memset(fetchhead_ref, 0x0, sizeof(git_fetchhead_ref)); - - git_oid_cpy(&fetchhead_ref->oid, oid); - fetchhead_ref->is_merge = is_merge; - - if (ref_name) - fetchhead_ref->ref_name = git__strdup(ref_name); - - if (remote_url) - fetchhead_ref->remote_url = git__strdup(remote_url); - - *out = fetchhead_ref; - - return 0; -} - -static int fetchhead_ref_write( - git_filebuf *file, - git_fetchhead_ref *fetchhead_ref) -{ - char oid[GIT_OID_HEXSZ + 1]; - const char *type, *name; - - assert(file && fetchhead_ref); - - git_oid_fmt(oid, &fetchhead_ref->oid); - oid[GIT_OID_HEXSZ] = '\0'; - - if (git__prefixcmp(fetchhead_ref->ref_name, GIT_REFS_HEADS_DIR) == 0) { - type = "branch "; - name = fetchhead_ref->ref_name + strlen(GIT_REFS_HEADS_DIR); - } else if(git__prefixcmp(fetchhead_ref->ref_name, - GIT_REFS_TAGS_DIR) == 0) { - type = "tag "; - name = fetchhead_ref->ref_name + strlen(GIT_REFS_TAGS_DIR); - } else { - type = ""; - name = fetchhead_ref->ref_name; - } - - return git_filebuf_printf(file, "%s\t%s\t%s'%s' of %s\n", - oid, - (fetchhead_ref->is_merge) ? "" : "not-for-merge", - type, - name, - fetchhead_ref->remote_url); -} - -int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs) -{ - git_filebuf file = GIT_FILEBUF_INIT; - git_buf path = GIT_BUF_INIT; - unsigned int i; - git_fetchhead_ref *fetchhead_ref; - - assert(repo && fetchhead_refs); - - if (git_buf_joinpath(&path, repo->path_repository, GIT_FETCH_HEAD_FILE) < 0) - return -1; - - if (git_filebuf_open(&file, path.ptr, GIT_FILEBUF_FORCE) < 0) { - git_buf_free(&path); - return -1; - } - - git_buf_free(&path); - - git_vector_sort(fetchhead_refs); - - git_vector_foreach(fetchhead_refs, i, fetchhead_ref) - fetchhead_ref_write(&file, fetchhead_ref); - - return git_filebuf_commit(&file, GIT_REFS_FILE_MODE); -} - -static int fetchhead_ref_parse( - git_oid *oid, - unsigned int *is_merge, - git_buf *ref_name, - const char **remote_url, - char *line, - size_t line_num) -{ - char *oid_str, *is_merge_str, *desc, *name = NULL; - const char *type = NULL; - int error = 0; - - *remote_url = NULL; - - if (!*line) { - giterr_set(GITERR_FETCHHEAD, - "Empty line in FETCH_HEAD line %d", line_num); - return -1; - } - - /* Compat with old git clients that wrote FETCH_HEAD like a loose ref. */ - if ((oid_str = git__strsep(&line, "\t")) == NULL) { - oid_str = line; - line += strlen(line); - - *is_merge = 1; - } - - if (strlen(oid_str) != GIT_OID_HEXSZ) { - giterr_set(GITERR_FETCHHEAD, - "Invalid object ID in FETCH_HEAD line %d", line_num); - return -1; - } - - if (git_oid_fromstr(oid, oid_str) < 0) { - const git_error *oid_err = giterr_last(); - const char *err_msg = oid_err ? oid_err->message : "Invalid object ID"; - - giterr_set(GITERR_FETCHHEAD, "%s in FETCH_HEAD line %d", - err_msg, line_num); - return -1; - } - - /* Parse new data from newer git clients */ - if (*line) { - if ((is_merge_str = git__strsep(&line, "\t")) == NULL) { - giterr_set(GITERR_FETCHHEAD, - "Invalid description data in FETCH_HEAD line %d", line_num); - return -1; - } - - if (*is_merge_str == '\0') - *is_merge = 1; - else if (strcmp(is_merge_str, "not-for-merge") == 0) - *is_merge = 0; - else { - giterr_set(GITERR_FETCHHEAD, - "Invalid for-merge entry in FETCH_HEAD line %d", line_num); - return -1; - } - - if ((desc = line) == NULL) { - giterr_set(GITERR_FETCHHEAD, - "Invalid description in FETCH_HEAD line %d", line_num); - return -1; - } - - if (git__prefixcmp(desc, "branch '") == 0) { - type = GIT_REFS_HEADS_DIR; - name = desc + 8; - } else if (git__prefixcmp(desc, "tag '") == 0) { - type = GIT_REFS_TAGS_DIR; - name = desc + 5; - } else if (git__prefixcmp(desc, "'") == 0) - name = desc + 1; - - if (name) { - if ((desc = strchr(name, '\'')) == NULL || - git__prefixcmp(desc, "' of ") != 0) { - giterr_set(GITERR_FETCHHEAD, - "Invalid description in FETCH_HEAD line %d", line_num); - return -1; - } - - *desc = '\0'; - desc += 5; - } - - *remote_url = desc; - } - - git_buf_clear(ref_name); - - if (type) - git_buf_join(ref_name, '/', type, name); - else if(name) - git_buf_puts(ref_name, name); - - return error; -} - -int git_repository_fetchhead_foreach(git_repository *repo, - git_repository_fetchhead_foreach_cb cb, - void *payload) -{ - git_buf path = GIT_BUF_INIT, file = GIT_BUF_INIT, name = GIT_BUF_INIT; - const char *ref_name; - git_oid oid; - const char *remote_url; - unsigned int is_merge = 0; - char *buffer, *line; - size_t line_num = 0; - int error = 0; - - assert(repo && cb); - - if (git_buf_joinpath(&path, repo->path_repository, GIT_FETCH_HEAD_FILE) < 0) - return -1; - - if ((error = git_futils_readbuffer(&file, git_buf_cstr(&path))) < 0) - goto done; - - buffer = file.ptr; - - while ((line = git__strsep(&buffer, "\n")) != NULL) { - ++line_num; - - if ((error = fetchhead_ref_parse(&oid, &is_merge, &name, &remote_url, - line, line_num)) < 0) - goto done; - - if (git_buf_len(&name) > 0) - ref_name = git_buf_cstr(&name); - else - ref_name = NULL; - - if ((cb(ref_name, remote_url, &oid, is_merge, payload)) != 0) { - error = GIT_EUSER; - goto done; - } - } - - if (*buffer) { - giterr_set(GITERR_FETCHHEAD, "No EOL at line %d", line_num+1); - error = -1; - goto done; - } - -done: - git_buf_free(&file); - git_buf_free(&path); - git_buf_free(&name); - - return error; -} - -void git_fetchhead_ref_free(git_fetchhead_ref *fetchhead_ref) -{ - if (fetchhead_ref == NULL) - return; - - git__free(fetchhead_ref->remote_url); - git__free(fetchhead_ref->ref_name); - git__free(fetchhead_ref); -} - diff --git a/src/filebuf.c b/src/filebuf.c deleted file mode 100644 index 246ae34e785..00000000000 --- a/src/filebuf.c +++ /dev/null @@ -1,495 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include - -#include "common.h" -#include "filebuf.h" -#include "fileops.h" - -#define GIT_LOCK_FILE_MODE 0644 - -static const size_t WRITE_BUFFER_SIZE = (4096 * 2); - -enum buferr_t { - BUFERR_OK = 0, - BUFERR_WRITE, - BUFERR_ZLIB, - BUFERR_MEM -}; - -#define ENSURE_BUF_OK(buf) if ((buf)->last_error != BUFERR_OK) { return -1; } - -static int verify_last_error(git_filebuf *file) -{ - switch (file->last_error) { - case BUFERR_WRITE: - giterr_set(GITERR_OS, "Failed to write out file"); - return -1; - - case BUFERR_MEM: - giterr_set_oom(); - return -1; - - case BUFERR_ZLIB: - giterr_set(GITERR_ZLIB, - "Buffer error when writing out ZLib data"); - return -1; - - default: - return 0; - } -} - -static int lock_file(git_filebuf *file, int flags) -{ - if (git_path_exists(file->path_lock) == true) { - if (flags & GIT_FILEBUF_FORCE) - p_unlink(file->path_lock); - else { - giterr_clear(); /* actual OS error code just confuses */ - giterr_set(GITERR_OS, - "Failed to lock file '%s' for writing", file->path_lock); - return -1; - } - } - - /* create path to the file buffer is required */ - if (flags & GIT_FILEBUF_FORCE) { - /* XXX: Should dirmode here be configurable? Or is 0777 always fine? */ - file->fd = git_futils_creat_locked_withpath(file->path_lock, 0777, GIT_LOCK_FILE_MODE); - } else { - file->fd = git_futils_creat_locked(file->path_lock, GIT_LOCK_FILE_MODE); - } - - if (file->fd < 0) - return -1; - - file->fd_is_open = true; - - if ((flags & GIT_FILEBUF_APPEND) && git_path_exists(file->path_original) == true) { - git_file source; - char buffer[2048]; - ssize_t read_bytes; - - source = p_open(file->path_original, O_RDONLY); - if (source < 0) { - giterr_set(GITERR_OS, - "Failed to open file '%s' for reading", - file->path_original); - return -1; - } - - while ((read_bytes = p_read(source, buffer, sizeof(buffer))) > 0) { - p_write(file->fd, buffer, read_bytes); - if (file->compute_digest) - git_hash_update(&file->digest, buffer, read_bytes); - } - - p_close(source); - - if (read_bytes < 0) { - giterr_set(GITERR_OS, "Failed to read file '%s'", file->path_original); - return -1; - } - } - - return 0; -} - -void git_filebuf_cleanup(git_filebuf *file) -{ - if (file->fd_is_open && file->fd >= 0) - p_close(file->fd); - - if (file->fd_is_open && file->path_lock && git_path_exists(file->path_lock)) - p_unlink(file->path_lock); - - if (file->compute_digest) { - git_hash_ctx_cleanup(&file->digest); - file->compute_digest = 0; - } - - if (file->buffer) - git__free(file->buffer); - - /* use the presence of z_buf to decide if we need to deflateEnd */ - if (file->z_buf) { - git__free(file->z_buf); - deflateEnd(&file->zs); - } - - if (file->path_original) - git__free(file->path_original); - if (file->path_lock) - git__free(file->path_lock); - - memset(file, 0x0, sizeof(git_filebuf)); - file->fd = -1; -} - -GIT_INLINE(int) flush_buffer(git_filebuf *file) -{ - int result = file->write(file, file->buffer, file->buf_pos); - file->buf_pos = 0; - return result; -} - -int git_filebuf_flush(git_filebuf *file) -{ - return flush_buffer(file); -} - -static int write_normal(git_filebuf *file, void *source, size_t len) -{ - if (len > 0) { - if (p_write(file->fd, (void *)source, len) < 0) { - file->last_error = BUFERR_WRITE; - return -1; - } - - if (file->compute_digest) - git_hash_update(&file->digest, source, len); - } - - return 0; -} - -static int write_deflate(git_filebuf *file, void *source, size_t len) -{ - z_stream *zs = &file->zs; - - if (len > 0 || file->flush_mode == Z_FINISH) { - zs->next_in = source; - zs->avail_in = (uInt)len; - - do { - size_t have; - - zs->next_out = file->z_buf; - zs->avail_out = (uInt)file->buf_size; - - if (deflate(zs, file->flush_mode) == Z_STREAM_ERROR) { - file->last_error = BUFERR_ZLIB; - return -1; - } - - have = file->buf_size - (size_t)zs->avail_out; - - if (p_write(file->fd, file->z_buf, have) < 0) { - file->last_error = BUFERR_WRITE; - return -1; - } - - } while (zs->avail_out == 0); - - assert(zs->avail_in == 0); - - if (file->compute_digest) - git_hash_update(&file->digest, source, len); - } - - return 0; -} - -int git_filebuf_open(git_filebuf *file, const char *path, int flags) -{ - int compression; - size_t path_len; - - /* opening an already open buffer is a programming error; - * assert that this never happens instead of returning - * an error code */ - assert(file && path && file->buffer == NULL); - - memset(file, 0x0, sizeof(git_filebuf)); - - if (flags & GIT_FILEBUF_DO_NOT_BUFFER) - file->do_not_buffer = true; - - file->buf_size = WRITE_BUFFER_SIZE; - file->buf_pos = 0; - file->fd = -1; - file->last_error = BUFERR_OK; - - /* Allocate the main cache buffer */ - if (!file->do_not_buffer) { - file->buffer = git__malloc(file->buf_size); - GITERR_CHECK_ALLOC(file->buffer); - } - - /* If we are hashing on-write, allocate a new hash context */ - if (flags & GIT_FILEBUF_HASH_CONTENTS) { - file->compute_digest = 1; - - if (git_hash_ctx_init(&file->digest) < 0) - goto cleanup; - } - - compression = flags >> GIT_FILEBUF_DEFLATE_SHIFT; - - /* If we are deflating on-write, */ - if (compression != 0) { - /* Initialize the ZLib stream */ - if (deflateInit(&file->zs, compression) != Z_OK) { - giterr_set(GITERR_ZLIB, "Failed to initialize zlib"); - goto cleanup; - } - - /* Allocate the Zlib cache buffer */ - file->z_buf = git__malloc(file->buf_size); - GITERR_CHECK_ALLOC(file->z_buf); - - /* Never flush */ - file->flush_mode = Z_NO_FLUSH; - file->write = &write_deflate; - } else { - file->write = &write_normal; - } - - /* If we are writing to a temp file */ - if (flags & GIT_FILEBUF_TEMPORARY) { - git_buf tmp_path = GIT_BUF_INIT; - - /* Open the file as temporary for locking */ - file->fd = git_futils_mktmp(&tmp_path, path); - - if (file->fd < 0) { - git_buf_free(&tmp_path); - goto cleanup; - } - file->fd_is_open = true; - - /* No original path */ - file->path_original = NULL; - file->path_lock = git_buf_detach(&tmp_path); - GITERR_CHECK_ALLOC(file->path_lock); - } else { - path_len = strlen(path); - - /* Save the original path of the file */ - file->path_original = git__strdup(path); - GITERR_CHECK_ALLOC(file->path_original); - - /* create the locking path by appending ".lock" to the original */ - file->path_lock = git__malloc(path_len + GIT_FILELOCK_EXTLENGTH); - GITERR_CHECK_ALLOC(file->path_lock); - - memcpy(file->path_lock, file->path_original, path_len); - memcpy(file->path_lock + path_len, GIT_FILELOCK_EXTENSION, GIT_FILELOCK_EXTLENGTH); - - /* open the file for locking */ - if (lock_file(file, flags) < 0) - goto cleanup; - } - - return 0; - -cleanup: - git_filebuf_cleanup(file); - return -1; -} - -int git_filebuf_hash(git_oid *oid, git_filebuf *file) -{ - assert(oid && file && file->compute_digest); - - flush_buffer(file); - - if (verify_last_error(file) < 0) - return -1; - - git_hash_final(oid, &file->digest); - git_hash_ctx_cleanup(&file->digest); - file->compute_digest = 0; - - return 0; -} - -int git_filebuf_commit_at(git_filebuf *file, const char *path, mode_t mode) -{ - git__free(file->path_original); - file->path_original = git__strdup(path); - GITERR_CHECK_ALLOC(file->path_original); - - return git_filebuf_commit(file, mode); -} - -int git_filebuf_commit(git_filebuf *file, mode_t mode) -{ - /* temporary files cannot be committed */ - assert(file && file->path_original); - - file->flush_mode = Z_FINISH; - flush_buffer(file); - - if (verify_last_error(file) < 0) - goto on_error; - - file->fd_is_open = false; - - if (p_close(file->fd) < 0) { - giterr_set(GITERR_OS, "Failed to close file at '%s'", file->path_lock); - goto on_error; - } - - file->fd = -1; - - if (p_chmod(file->path_lock, mode)) { - giterr_set(GITERR_OS, "Failed to set attributes for file at '%s'", file->path_lock); - goto on_error; - } - - p_unlink(file->path_original); - - if (p_rename(file->path_lock, file->path_original) < 0) { - giterr_set(GITERR_OS, "Failed to rename lockfile to '%s'", file->path_original); - goto on_error; - } - - git_filebuf_cleanup(file); - return 0; - -on_error: - git_filebuf_cleanup(file); - return -1; -} - -GIT_INLINE(void) add_to_cache(git_filebuf *file, const void *buf, size_t len) -{ - memcpy(file->buffer + file->buf_pos, buf, len); - file->buf_pos += len; -} - -int git_filebuf_write(git_filebuf *file, const void *buff, size_t len) -{ - const unsigned char *buf = buff; - - ENSURE_BUF_OK(file); - - if (file->do_not_buffer) - return file->write(file, (void *)buff, len); - - for (;;) { - size_t space_left = file->buf_size - file->buf_pos; - - /* cache if it's small */ - if (space_left > len) { - add_to_cache(file, buf, len); - return 0; - } - - add_to_cache(file, buf, space_left); - if (flush_buffer(file) < 0) - return -1; - - len -= space_left; - buf += space_left; - } -} - -int git_filebuf_reserve(git_filebuf *file, void **buffer, size_t len) -{ - size_t space_left = file->buf_size - file->buf_pos; - - *buffer = NULL; - - ENSURE_BUF_OK(file); - - if (len > file->buf_size) { - file->last_error = BUFERR_MEM; - return -1; - } - - if (space_left <= len) { - if (flush_buffer(file) < 0) - return -1; - } - - *buffer = (file->buffer + file->buf_pos); - file->buf_pos += len; - - return 0; -} - -int git_filebuf_printf(git_filebuf *file, const char *format, ...) -{ - va_list arglist; - size_t space_left; - int len, res; - char *tmp_buffer; - - ENSURE_BUF_OK(file); - - space_left = file->buf_size - file->buf_pos; - - do { - va_start(arglist, format); - len = p_vsnprintf((char *)file->buffer + file->buf_pos, space_left, format, arglist); - va_end(arglist); - - if (len < 0) { - file->last_error = BUFERR_MEM; - return -1; - } - - if ((size_t)len + 1 <= space_left) { - file->buf_pos += len; - return 0; - } - - if (flush_buffer(file) < 0) - return -1; - - space_left = file->buf_size - file->buf_pos; - - } while ((size_t)len + 1 <= space_left); - - tmp_buffer = git__malloc(len + 1); - if (!tmp_buffer) { - file->last_error = BUFERR_MEM; - return -1; - } - - va_start(arglist, format); - len = p_vsnprintf(tmp_buffer, len + 1, format, arglist); - va_end(arglist); - - if (len < 0) { - git__free(tmp_buffer); - file->last_error = BUFERR_MEM; - return -1; - } - - res = git_filebuf_write(file, tmp_buffer, len); - git__free(tmp_buffer); - - return res; -} - -int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file) -{ - int res; - struct stat st; - - if (file->fd_is_open) - res = p_fstat(file->fd, &st); - else - res = p_stat(file->path_original, &st); - - if (res < 0) { - giterr_set(GITERR_OS, "Could not get stat info for '%s'", - file->path_original); - return res; - } - - if (mtime) - *mtime = st.st_mtime; - if (size) - *size = (size_t)st.st_size; - - return 0; -} diff --git a/src/filebuf.h b/src/filebuf.h deleted file mode 100644 index 823af81bf16..00000000000 --- a/src/filebuf.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_filebuf_h__ -#define INCLUDE_filebuf_h__ - -#include "fileops.h" -#include "hash.h" -#include - -#ifdef GIT_THREADS -# define GIT_FILEBUF_THREADS -#endif - -#define GIT_FILEBUF_HASH_CONTENTS (1 << 0) -#define GIT_FILEBUF_APPEND (1 << 2) -#define GIT_FILEBUF_FORCE (1 << 3) -#define GIT_FILEBUF_TEMPORARY (1 << 4) -#define GIT_FILEBUF_DO_NOT_BUFFER (1 << 5) -#define GIT_FILEBUF_DEFLATE_SHIFT (6) - -#define GIT_FILELOCK_EXTENSION ".lock\0" -#define GIT_FILELOCK_EXTLENGTH 6 - -struct git_filebuf { - char *path_original; - char *path_lock; - - int (*write)(struct git_filebuf *file, void *source, size_t len); - - bool compute_digest; - git_hash_ctx digest; - - unsigned char *buffer; - unsigned char *z_buf; - - z_stream zs; - int flush_mode; - - size_t buf_size, buf_pos; - git_file fd; - bool fd_is_open; - bool do_not_buffer; - int last_error; -}; - -typedef struct git_filebuf git_filebuf; - -#define GIT_FILEBUF_INIT {0} - -/* - * The git_filebuf object lifecycle is: - * - Allocate git_filebuf, preferably using GIT_FILEBUF_INIT. - * - * - Call git_filebuf_open() to initialize the filebuf for use. - * - * - Make as many calls to git_filebuf_write(), git_filebuf_printf(), - * git_filebuf_reserve() as you like. The error codes for these - * functions don't need to be checked. They are stored internally - * by the file buffer. - * - * - While you are writing, you may call git_filebuf_hash() to get - * the hash of all you have written so far. This function will - * fail if any of the previous writes to the buffer failed. - * - * - To close the git_filebuf, you may call git_filebuf_commit() or - * git_filebuf_commit_at() to save the file, or - * git_filebuf_cleanup() to abandon the file. All of these will - * free the git_filebuf object. Likewise, all of these will fail - * if any of the previous writes to the buffer failed, and set - * an error code accordingly. - */ -int git_filebuf_write(git_filebuf *lock, const void *buff, size_t len); -int git_filebuf_reserve(git_filebuf *file, void **buff, size_t len); -int git_filebuf_printf(git_filebuf *file, const char *format, ...) GIT_FORMAT_PRINTF(2, 3); - -int git_filebuf_open(git_filebuf *lock, const char *path, int flags); -int git_filebuf_commit(git_filebuf *lock, mode_t mode); -int git_filebuf_commit_at(git_filebuf *lock, const char *path, mode_t mode); -void git_filebuf_cleanup(git_filebuf *lock); -int git_filebuf_hash(git_oid *oid, git_filebuf *file); -int git_filebuf_flush(git_filebuf *file); -int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file); - -#endif diff --git a/src/fileops.c b/src/fileops.c deleted file mode 100644 index 4ae9e3ab1a1..00000000000 --- a/src/fileops.c +++ /dev/null @@ -1,813 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "common.h" -#include "fileops.h" -#include -#if GIT_WIN32 -#include "win32/findfile.h" -#endif - -int git_futils_mkpath2file(const char *file_path, const mode_t mode) -{ - return git_futils_mkdir( - file_path, NULL, mode, - GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR); -} - -int git_futils_mktmp(git_buf *path_out, const char *filename) -{ - int fd; - - git_buf_sets(path_out, filename); - git_buf_puts(path_out, "_git2_XXXXXX"); - - if (git_buf_oom(path_out)) - return -1; - - if ((fd = p_mkstemp(path_out->ptr)) < 0) { - giterr_set(GITERR_OS, - "Failed to create temporary file '%s'", path_out->ptr); - return -1; - } - - return fd; -} - -int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode) -{ - int fd; - - if (git_futils_mkpath2file(path, dirmode) < 0) - return -1; - - fd = p_creat(path, mode); - if (fd < 0) { - giterr_set(GITERR_OS, "Failed to create file '%s'", path); - return -1; - } - - return fd; -} - -int git_futils_creat_locked(const char *path, const mode_t mode) -{ - int fd; - -#ifdef GIT_WIN32 - wchar_t buf[GIT_WIN_PATH]; - - git__utf8_to_16(buf, GIT_WIN_PATH, path); - fd = _wopen(buf, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode); -#else - fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode); -#endif - - if (fd < 0) { - giterr_set(GITERR_OS, "Failed to create locked file '%s'", path); - return -1; - } - - return fd; -} - -int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode) -{ - if (git_futils_mkpath2file(path, dirmode) < 0) - return -1; - - return git_futils_creat_locked(path, mode); -} - -int git_futils_open_ro(const char *path) -{ - int fd = p_open(path, O_RDONLY); - if (fd < 0) { - if (errno == ENOENT || errno == ENOTDIR) - fd = GIT_ENOTFOUND; - giterr_set(GITERR_OS, "Failed to open '%s'", path); - } - return fd; -} - -git_off_t git_futils_filesize(git_file fd) -{ - struct stat sb; - - if (p_fstat(fd, &sb)) { - giterr_set(GITERR_OS, "Failed to stat file descriptor"); - return -1; - } - - return sb.st_size; -} - -mode_t git_futils_canonical_mode(mode_t raw_mode) -{ - if (S_ISREG(raw_mode)) - return S_IFREG | GIT_CANONICAL_PERMS(raw_mode); - else if (S_ISLNK(raw_mode)) - return S_IFLNK; - else if (S_ISGITLINK(raw_mode)) - return S_IFGITLINK; - else if (S_ISDIR(raw_mode)) - return S_IFDIR; - else - return 0; -} - -int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len) -{ - ssize_t read_size = 0; - - git_buf_clear(buf); - - if (git_buf_grow(buf, len + 1) < 0) - return -1; - - /* p_read loops internally to read len bytes */ - read_size = p_read(fd, buf->ptr, len); - - if (read_size != (ssize_t)len) { - giterr_set(GITERR_OS, "Failed to read descriptor"); - return -1; - } - - buf->ptr[read_size] = '\0'; - buf->size = read_size; - - return 0; -} - -int git_futils_readbuffer_updated( - git_buf *buf, const char *path, time_t *mtime, size_t *size, int *updated) -{ - git_file fd; - struct stat st; - bool changed = false; - - assert(buf && path && *path); - - if (updated != NULL) - *updated = 0; - - if ((fd = git_futils_open_ro(path)) < 0) - return fd; - - if (p_fstat(fd, &st) < 0 || S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) { - p_close(fd); - giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path); - return -1; - } - - /* - * If we were given a time and/or a size, we only want to read the file - * if it has been modified. - */ - if (size && *size != (size_t)st.st_size) - changed = true; - if (mtime && *mtime != st.st_mtime) - changed = true; - if (!size && !mtime) - changed = true; - - if (!changed) { - p_close(fd); - return 0; - } - - if (mtime != NULL) - *mtime = st.st_mtime; - if (size != NULL) - *size = (size_t)st.st_size; - - if (git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size) < 0) { - p_close(fd); - return -1; - } - - p_close(fd); - - if (updated != NULL) - *updated = 1; - - return 0; -} - -int git_futils_readbuffer(git_buf *buf, const char *path) -{ - return git_futils_readbuffer_updated(buf, path, NULL, NULL, NULL); -} - -int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode) -{ - if (git_futils_mkpath2file(to, dirmode) < 0) - return -1; - - if (p_rename(from, to) < 0) { - giterr_set(GITERR_OS, "Failed to rename '%s' to '%s'", from, to); - return -1; - } - - return 0; -} - -int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len) -{ - return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin); -} - -int git_futils_mmap_ro_file(git_map *out, const char *path) -{ - git_file fd = git_futils_open_ro(path); - git_off_t len; - int result; - - if (fd < 0) - return fd; - - len = git_futils_filesize(fd); - if (!git__is_sizet(len)) { - giterr_set(GITERR_OS, "File `%s` too large to mmap", path); - return -1; - } - - result = git_futils_mmap_ro(out, fd, 0, (size_t)len); - p_close(fd); - return result; -} - -void git_futils_mmap_free(git_map *out) -{ - p_munmap(out); -} - -int git_futils_mkdir( - const char *path, - const char *base, - mode_t mode, - uint32_t flags) -{ - int error = -1; - git_buf make_path = GIT_BUF_INIT; - ssize_t root = 0; - char lastch, *tail; - - /* build path and find "root" where we should start calling mkdir */ - if (git_path_join_unrooted(&make_path, path, base, &root) < 0) - return -1; - - if (make_path.size == 0) { - giterr_set(GITERR_OS, "Attempt to create empty path"); - goto fail; - } - - /* remove trailing slashes on path */ - while (make_path.ptr[make_path.size - 1] == '/') { - make_path.size--; - make_path.ptr[make_path.size] = '\0'; - } - - /* if we are not supposed to made the last element, truncate it */ - if ((flags & GIT_MKDIR_SKIP_LAST) != 0) - git_buf_rtruncate_at_char(&make_path, '/'); - - /* if we are not supposed to make the whole path, reset root */ - if ((flags & GIT_MKDIR_PATH) == 0) - root = git_buf_rfind(&make_path, '/'); - - /* clip root to make_path length */ - if (root >= (ssize_t)make_path.size) - root = (ssize_t)make_path.size - 1; - if (root < 0) - root = 0; - - tail = & make_path.ptr[root]; - - while (*tail) { - /* advance tail to include next path component */ - while (*tail == '/') - tail++; - while (*tail && *tail != '/') - tail++; - - /* truncate path at next component */ - lastch = *tail; - *tail = '\0'; - - /* make directory */ - if (p_mkdir(make_path.ptr, mode) < 0) { - if (errno == EEXIST) { - if (!lastch && (flags & GIT_MKDIR_VERIFY_DIR) != 0) { - if (!git_path_isdir(make_path.ptr)) { - giterr_set( - GITERR_OS, "Existing path is not a directory '%s'", - make_path.ptr); - error = GIT_ENOTFOUND; - goto fail; - } - } - if ((flags & GIT_MKDIR_EXCL) != 0) { - giterr_set(GITERR_OS, "Directory already exists '%s'", - make_path.ptr); - error = GIT_EEXISTS; - goto fail; - } - } else { - giterr_set(GITERR_OS, "Failed to make directory '%s'", - make_path.ptr); - goto fail; - } - } - - /* chmod if requested */ - if ((flags & GIT_MKDIR_CHMOD_PATH) != 0 || - ((flags & GIT_MKDIR_CHMOD) != 0 && lastch == '\0')) - { - if (p_chmod(make_path.ptr, mode) < 0) { - giterr_set(GITERR_OS, "Failed to set permissions on '%s'", - make_path.ptr); - goto fail; - } - } - - *tail = lastch; - } - - git_buf_free(&make_path); - return 0; - -fail: - git_buf_free(&make_path); - return error; -} - -int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode) -{ - return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH); -} - -typedef struct { - const char *base; - size_t baselen; - uint32_t flags; - int error; -} futils__rmdir_data; - -static int futils__error_cannot_rmdir(const char *path, const char *filemsg) -{ - if (filemsg) - giterr_set(GITERR_OS, "Could not remove directory. File '%s' %s", - path, filemsg); - else - giterr_set(GITERR_OS, "Could not remove directory '%s'", path); - - return -1; -} - -static int futils__rm_first_parent(git_buf *path, const char *ceiling) -{ - int error = GIT_ENOTFOUND; - struct stat st; - - while (error == GIT_ENOTFOUND) { - git_buf_rtruncate_at_char(path, '/'); - - if (!path->size || git__prefixcmp(path->ptr, ceiling) != 0) - error = 0; - else if (p_lstat_posixly(path->ptr, &st) == 0) { - if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) - error = p_unlink(path->ptr); - else if (!S_ISDIR(st.st_mode)) - error = -1; /* fail to remove non-regular file */ - } else if (errno != ENOTDIR) - error = -1; - } - - if (error) - futils__error_cannot_rmdir(path->ptr, "cannot remove parent"); - - return error; -} - -static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path) -{ - struct stat st; - futils__rmdir_data *data = opaque; - - if ((data->error = p_lstat_posixly(path->ptr, &st)) < 0) { - if (errno == ENOENT) - data->error = 0; - else if (errno == ENOTDIR) { - /* asked to remove a/b/c/d/e and a/b is a normal file */ - if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0) - data->error = futils__rm_first_parent(path, data->base); - else - futils__error_cannot_rmdir( - path->ptr, "parent is not directory"); - } - else - futils__error_cannot_rmdir(path->ptr, "cannot access"); - } - - else if (S_ISDIR(st.st_mode)) { - int error = git_path_direach(path, futils__rmdir_recurs_foreach, data); - if (error < 0) - return (error == GIT_EUSER) ? data->error : error; - - data->error = p_rmdir(path->ptr); - - if (data->error < 0) { - if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 && - (errno == ENOTEMPTY || errno == EEXIST)) - data->error = 0; - else - futils__error_cannot_rmdir(path->ptr, NULL); - } - } - - else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) { - data->error = p_unlink(path->ptr); - - if (data->error < 0) - futils__error_cannot_rmdir(path->ptr, "cannot be removed"); - } - - else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0) - data->error = futils__error_cannot_rmdir(path->ptr, "still present"); - - return data->error; -} - -static int futils__rmdir_empty_parent(void *opaque, git_buf *path) -{ - futils__rmdir_data *data = opaque; - int error; - - if (git_buf_len(path) <= data->baselen) - return GIT_ITEROVER; - - error = p_rmdir(git_buf_cstr(path)); - - if (error) { - int en = errno; - - if (en == ENOENT || en == ENOTDIR) { - giterr_clear(); - error = 0; - } else if (en == ENOTEMPTY || en == EEXIST) { - giterr_clear(); - error = GIT_ITEROVER; - } else { - futils__error_cannot_rmdir(git_buf_cstr(path), NULL); - } - } - - return error; -} - -int git_futils_rmdir_r( - const char *path, const char *base, uint32_t flags) -{ - int error; - git_buf fullpath = GIT_BUF_INIT; - futils__rmdir_data data; - - /* build path and find "root" where we should start calling mkdir */ - if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0) - return -1; - - data.base = base ? base : ""; - data.baselen = base ? strlen(base) : 0; - data.flags = flags; - data.error = 0; - - error = futils__rmdir_recurs_foreach(&data, &fullpath); - - /* remove now-empty parents if requested */ - if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0) { - error = git_path_walk_up( - &fullpath, base, futils__rmdir_empty_parent, &data); - - if (error == GIT_ITEROVER) - error = 0; - } - - git_buf_free(&fullpath); - - return error; -} - -int git_futils_find_system_file(git_buf *path, const char *filename) -{ -#ifdef GIT_WIN32 - // try to find git.exe/git.cmd on path - if (!win32_find_system_file_using_path(path, filename)) - return 0; - - // try to find msysgit installation path using registry - if (!win32_find_system_file_using_registry(path, filename)) - return 0; -#else - if (git_buf_joinpath(path, "/etc", filename) < 0) - return -1; - - if (git_path_exists(path->ptr) == true) - return 0; -#endif - - git_buf_clear(path); - giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename); - return GIT_ENOTFOUND; -} - -int git_futils_find_global_file(git_buf *path, const char *filename) -{ -#ifdef GIT_WIN32 - struct win32_path root; - static const wchar_t *tmpls[4] = { - L"%HOME%\\", - L"%HOMEDRIVE%%HOMEPATH%\\", - L"%USERPROFILE%\\", - NULL, - }; - const wchar_t **tmpl; - - for (tmpl = tmpls; *tmpl != NULL; tmpl++) { - /* try to expand environment variable, skipping if not set */ - if (win32_expand_path(&root, *tmpl) != 0 || root.path[0] == L'%') - continue; - - /* try to look up file under path */ - if (!win32_find_file(path, &root, filename)) - return 0; - } - - giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename); - git_buf_clear(path); - - return GIT_ENOTFOUND; -#else - const char *home = getenv("HOME"); - - if (home == NULL) { - giterr_set(GITERR_OS, "Global file lookup failed. " - "Cannot locate the user's home directory"); - return GIT_ENOTFOUND; - } - - if (git_buf_joinpath(path, home, filename) < 0) - return -1; - - if (git_path_exists(path->ptr) == false) { - giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename); - git_buf_clear(path); - return GIT_ENOTFOUND; - } - - return 0; -#endif -} - -int git_futils_fake_symlink(const char *old, const char *new) -{ - int retcode = GIT_ERROR; - int fd = git_futils_creat_withpath(new, 0755, 0644); - if (fd >= 0) { - retcode = p_write(fd, old, strlen(old)); - p_close(fd); - } - return retcode; -} - -static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done) -{ - int error = 0; - char buffer[4096]; - ssize_t len = 0; - - while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0) - /* p_write() does not have the same semantics as write(). It loops - * internally and will return 0 when it has completed writing. - */ - error = p_write(ofd, buffer, len); - - if (len < 0) { - giterr_set(GITERR_OS, "Read error while copying file"); - error = (int)len; - } - - if (close_fd_when_done) { - p_close(ifd); - p_close(ofd); - } - - return error; -} - -int git_futils_cp(const char *from, const char *to, mode_t filemode) -{ - int ifd, ofd; - - if ((ifd = git_futils_open_ro(from)) < 0) - return ifd; - - if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) { - if (errno == ENOENT || errno == ENOTDIR) - ofd = GIT_ENOTFOUND; - giterr_set(GITERR_OS, "Failed to open '%s' for writing", to); - p_close(ifd); - return ofd; - } - - return cp_by_fd(ifd, ofd, true); -} - -static int cp_link(const char *from, const char *to, size_t link_size) -{ - int error = 0; - ssize_t read_len; - char *link_data = git__malloc(link_size + 1); - GITERR_CHECK_ALLOC(link_data); - - read_len = p_readlink(from, link_data, link_size); - if (read_len != (ssize_t)link_size) { - giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from); - error = -1; - } - else { - link_data[read_len] = '\0'; - - if (p_symlink(link_data, to) < 0) { - giterr_set(GITERR_OS, "Could not symlink '%s' as '%s'", - link_data, to); - error = -1; - } - } - - git__free(link_data); - return error; -} - -typedef struct { - const char *to_root; - git_buf to; - ssize_t from_prefix; - uint32_t flags; - uint32_t mkdir_flags; - mode_t dirmode; -} cp_r_info; - -static int _cp_r_callback(void *ref, git_buf *from) -{ - cp_r_info *info = ref; - struct stat from_st, to_st; - bool exists = false; - - if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 && - from->ptr[git_path_basename_offset(from)] == '.') - return 0; - - if (git_buf_joinpath( - &info->to, info->to_root, from->ptr + info->from_prefix) < 0) - return -1; - - if (p_lstat(info->to.ptr, &to_st) < 0) { - if (errno != ENOENT && errno != ENOTDIR) { - giterr_set(GITERR_OS, - "Could not access %s while copying files", info->to.ptr); - return -1; - } - } else - exists = true; - - if (git_path_lstat(from->ptr, &from_st) < 0) - return -1; - - if (S_ISDIR(from_st.st_mode)) { - int error = 0; - mode_t oldmode = info->dirmode; - - /* if we are not chmod'ing, then overwrite dirmode */ - if ((info->flags & GIT_CPDIR_CHMOD) == 0) - info->dirmode = from_st.st_mode; - - /* make directory now if CREATE_EMPTY_DIRS is requested and needed */ - if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0) - error = git_futils_mkdir( - info->to.ptr, NULL, info->dirmode, info->mkdir_flags); - - /* recurse onto target directory */ - if (!exists || S_ISDIR(to_st.st_mode)) - error = git_path_direach(from, _cp_r_callback, info); - - if (oldmode != 0) - info->dirmode = oldmode; - - return error; - } - - if (exists) { - if ((info->flags & GIT_CPDIR_OVERWRITE) == 0) - return 0; - - if (p_unlink(info->to.ptr) < 0) { - giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'", - info->to.ptr); - return -1; - } - } - - /* Done if this isn't a regular file or a symlink */ - if (!S_ISREG(from_st.st_mode) && - (!S_ISLNK(from_st.st_mode) || - (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0)) - return 0; - - /* Make container directory on demand if needed */ - if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 && - git_futils_mkdir( - info->to.ptr, NULL, info->dirmode, info->mkdir_flags) < 0) - return -1; - - /* make symlink or regular file */ - if (S_ISLNK(from_st.st_mode)) - return cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size); - else - return git_futils_cp(from->ptr, info->to.ptr, from_st.st_mode); -} - -int git_futils_cp_r( - const char *from, - const char *to, - uint32_t flags, - mode_t dirmode) -{ - int error; - git_buf path = GIT_BUF_INIT; - cp_r_info info; - - if (git_buf_sets(&path, from) < 0) - return -1; - - info.to_root = to; - info.flags = flags; - info.dirmode = dirmode; - info.from_prefix = path.size; - git_buf_init(&info.to, 0); - - /* precalculate mkdir flags */ - if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) { - info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST; - if ((flags & GIT_CPDIR_CHMOD) != 0) - info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH; - } else { - info.mkdir_flags = - ((flags & GIT_CPDIR_CHMOD) != 0) ? GIT_MKDIR_CHMOD : 0; - } - - error = _cp_r_callback(&info, &path); - - git_buf_free(&path); - git_buf_free(&info.to); - - return error; -} - -int git_futils_filestamp_check( - git_futils_filestamp *stamp, const char *path) -{ - struct stat st; - - /* if the stamp is NULL, then always reload */ - if (stamp == NULL) - return 1; - - if (p_stat(path, &st) < 0) - return GIT_ENOTFOUND; - - if (stamp->mtime == (git_time_t)st.st_mtime && - stamp->size == (git_off_t)st.st_size && - stamp->ino == (unsigned int)st.st_ino) - return 0; - - stamp->mtime = (git_time_t)st.st_mtime; - stamp->size = (git_off_t)st.st_size; - stamp->ino = (unsigned int)st.st_ino; - - return 1; -} - -void git_futils_filestamp_set( - git_futils_filestamp *target, const git_futils_filestamp *source) -{ - assert(target); - - if (source) - memcpy(target, source, sizeof(*target)); - else - memset(target, 0, sizeof(*target)); -} diff --git a/src/fileops.h b/src/fileops.h deleted file mode 100644 index 5495b12cd80..00000000000 --- a/src/fileops.h +++ /dev/null @@ -1,325 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_fileops_h__ -#define INCLUDE_fileops_h__ - -#include "common.h" -#include "map.h" -#include "posix.h" -#include "path.h" - -/** - * Filebuffer methods - * - * Read whole files into an in-memory buffer for processing - */ -extern int git_futils_readbuffer(git_buf *obj, const char *path); -extern int git_futils_readbuffer_updated( - git_buf *obj, const char *path, time_t *mtime, size_t *size, int *updated); -extern int git_futils_readbuffer_fd(git_buf *obj, git_file fd, size_t len); - -/** - * File utils - * - * These are custom filesystem-related helper methods. They are - * rather high level, and wrap the underlying POSIX methods - * - * All these methods return 0 on success, - * or an error code on failure and an error message is set. - */ - -/** - * Create and open a file, while also - * creating all the folders in its path - */ -extern int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode); - -/** - * Create an open a process-locked file - */ -extern int git_futils_creat_locked(const char *path, const mode_t mode); - -/** - * Create an open a process-locked file, while - * also creating all the folders in its path - */ -extern int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode); - -/** - * Create a path recursively - * - * If a base parameter is being passed, it's expected to be valued with a - * path pointing to an already existing directory. - */ -extern int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode); - -/** - * Flags to pass to `git_futils_mkdir`. - * - * * GIT_MKDIR_EXCL is "exclusive" - i.e. generate an error if dir exists. - * * GIT_MKDIR_PATH says to make all components in the path. - * * GIT_MKDIR_CHMOD says to chmod the final directory entry after creation - * * GIT_MKDIR_CHMOD_PATH says to chmod each directory component in the path - * * GIT_MKDIR_SKIP_LAST says to leave off the last element of the path - * * GIT_MKDIR_VERIFY_DIR says confirm final item is a dir, not just EEXIST - * - * Note that the chmod options will be executed even if the directory already - * exists, unless GIT_MKDIR_EXCL is given. - */ -typedef enum { - GIT_MKDIR_EXCL = 1, - GIT_MKDIR_PATH = 2, - GIT_MKDIR_CHMOD = 4, - GIT_MKDIR_CHMOD_PATH = 8, - GIT_MKDIR_SKIP_LAST = 16, - GIT_MKDIR_VERIFY_DIR = 32, -} git_futils_mkdir_flags; - -/** - * Create a directory or entire path. - * - * This makes a directory (and the entire path leading up to it if requested), - * and optionally chmods the directory immediately after (or each part of the - * path if requested). - * - * @param path The path to create. - * @param base Root for relative path. These directories will never be made. - * @param mode The mode to use for created directories. - * @param flags Combination of the mkdir flags above. - * @return 0 on success, else error code - */ -extern int git_futils_mkdir(const char *path, const char *base, mode_t mode, uint32_t flags); - -/** - * Create all the folders required to contain - * the full path of a file - */ -extern int git_futils_mkpath2file(const char *path, const mode_t mode); - -/** - * Flags to pass to `git_futils_rmdir_r`. - * - * * GIT_RMDIR_EMPTY_HIERARCHY - the default; remove hierarchy of empty - * dirs and generate error if any files are found. - * * GIT_RMDIR_REMOVE_FILES - attempt to remove files in the hierarchy. - * * GIT_RMDIR_SKIP_NONEMPTY - skip non-empty directories with no error. - * * GIT_RMDIR_EMPTY_PARENTS - remove containing directories up to base - * if removing this item leaves them empty - * * GIT_RMDIR_REMOVE_BLOCKERS - remove blocking file that causes ENOTDIR - * - * The old values translate into the new as follows: - * - * * GIT_DIRREMOVAL_EMPTY_HIERARCHY == GIT_RMDIR_EMPTY_HIERARCHY - * * GIT_DIRREMOVAL_FILES_AND_DIRS ~= GIT_RMDIR_REMOVE_FILES - * * GIT_DIRREMOVAL_ONLY_EMPTY_DIRS == GIT_RMDIR_SKIP_NONEMPTY - */ -typedef enum { - GIT_RMDIR_EMPTY_HIERARCHY = 0, - GIT_RMDIR_REMOVE_FILES = (1 << 0), - GIT_RMDIR_SKIP_NONEMPTY = (1 << 1), - GIT_RMDIR_EMPTY_PARENTS = (1 << 2), - GIT_RMDIR_REMOVE_BLOCKERS = (1 << 3), -} git_futils_rmdir_flags; - -/** - * Remove path and any files and directories beneath it. - * - * @param path Path to to top level directory to process. - * @param base Root for relative path. - * @param flags Combination of git_futils_rmdir_flags values - * @return 0 on success; -1 on error. - */ -extern int git_futils_rmdir_r(const char *path, const char *base, uint32_t flags); - -/** - * Create and open a temporary file with a `_git2_` suffix. - * Writes the filename into path_out. - * @return On success, an open file descriptor, else an error code < 0. - */ -extern int git_futils_mktmp(git_buf *path_out, const char *filename); - -/** - * Move a file on the filesystem, create the - * destination path if it doesn't exist - */ -extern int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode); - -/** - * Copy a file - * - * The filemode will be used for the newly created file. - */ -extern int git_futils_cp( - const char *from, - const char *to, - mode_t filemode); - -/** - * Flags that can be passed to `git_futils_cp_r`. - */ -typedef enum { - GIT_CPDIR_CREATE_EMPTY_DIRS = 1, - GIT_CPDIR_COPY_SYMLINKS = 2, - GIT_CPDIR_COPY_DOTFILES = 4, - GIT_CPDIR_OVERWRITE = 8, - GIT_CPDIR_CHMOD = 16 -} git_futils_cpdir_flags; - -/** - * Copy a directory tree. - * - * This copies directories and files from one root to another. You can - * pass a combinationof GIT_CPDIR flags as defined above. - * - * If you pass the CHMOD flag, then the dirmode will be applied to all - * directories that are created during the copy, overiding the natural - * permissions. If you do not pass the CHMOD flag, then the dirmode - * will actually be copied from the source files and the `dirmode` arg - * will be ignored. - */ -extern int git_futils_cp_r( - const char *from, - const char *to, - uint32_t flags, - mode_t dirmode); - -/** - * Open a file readonly and set error if needed. - */ -extern int git_futils_open_ro(const char *path); - -/** - * Get the filesize in bytes of a file - */ -extern git_off_t git_futils_filesize(git_file fd); - -#define GIT_MODE_PERMS_MASK 0777 -#define GIT_CANONICAL_PERMS(MODE) (((MODE) & 0100) ? 0755 : 0644) -#define GIT_MODE_TYPE(MODE) ((MODE) & ~GIT_MODE_PERMS_MASK) - -/** - * Convert a mode_t from the OS to a legal git mode_t value. - */ -extern mode_t git_futils_canonical_mode(mode_t raw_mode); - - -/** - * Read-only map all or part of a file into memory. - * When possible this function should favor a virtual memory - * style mapping over some form of malloc()+read(), as the - * data access will be random and is not likely to touch the - * majority of the region requested. - * - * @param out buffer to populate with the mapping information. - * @param fd open descriptor to configure the mapping from. - * @param begin first byte to map, this should be page aligned. - * @param end number of bytes to map. - * @return - * - 0 on success; - * - -1 on error. - */ -extern int git_futils_mmap_ro( - git_map *out, - git_file fd, - git_off_t begin, - size_t len); - -/** - * Read-only map an entire file. - * - * @param out buffer to populate with the mapping information. - * @param path path to file to be opened. - * @return - * - 0 on success; - * - GIT_ENOTFOUND if not found; - * - -1 on an unspecified OS related error. - */ -extern int git_futils_mmap_ro_file( - git_map *out, - const char *path); - -/** - * Release the memory associated with a previous memory mapping. - * @param map the mapping description previously configured. - */ -extern void git_futils_mmap_free(git_map *map); - -/** - * Find a "global" file (i.e. one in a user's home directory). - * - * @param pathbuf buffer to write the full path into - * @param filename name of file to find in the home directory - * @return - * - 0 if found; - * - GIT_ENOTFOUND if not found; - * - -1 on an unspecified OS related error. - */ -extern int git_futils_find_global_file(git_buf *path, const char *filename); - -/** - * Find a "system" file (i.e. one shared for all users of the system). - * - * @param pathbuf buffer to write the full path into - * @param filename name of file to find in the home directory - * @return - * - 0 if found; - * - GIT_ENOTFOUND if not found; - * - -1 on an unspecified OS related error. - */ -extern int git_futils_find_system_file(git_buf *path, const char *filename); - - -/** - * Create a "fake" symlink (text file containing the target path). - * - * @param new symlink file to be created - * @param old original symlink target - * @return 0 on success, -1 on error - */ -extern int git_futils_fake_symlink(const char *new, const char *old); - -/** - * A file stamp represents a snapshot of information about a file that can - * be used to test if the file changes. This portable implementation is - * based on stat data about that file, but it is possible that OS specific - * versions could be implemented in the future. - */ -typedef struct { - git_time_t mtime; - git_off_t size; - unsigned int ino; -} git_futils_filestamp; - -/** - * Compare stat information for file with reference info. - * - * This function updates the file stamp to current data for the given path - * and returns 0 if the file is up-to-date relative to the prior setting or - * 1 if the file has been changed. (This also may return GIT_ENOTFOUND if - * the file doesn't exist.) - * - * @param stamp File stamp to be checked - * @param path Path to stat and check if changed - * @return 0 if up-to-date, 1 if out-of-date, <0 on error - */ -extern int git_futils_filestamp_check( - git_futils_filestamp *stamp, const char *path); - -/** - * Set or reset file stamp data - * - * This writes the target file stamp. If the source is NULL, this will set - * the target stamp to values that will definitely be out of date. If the - * source is not NULL, this copies the source values to the target. - * - * @param tgt File stamp to write to - * @param src File stamp to copy from or NULL to clear the target - */ -extern void git_futils_filestamp_set( - git_futils_filestamp *tgt, const git_futils_filestamp *src); - -#endif /* INCLUDE_fileops_h__ */ diff --git a/src/filter.c b/src/filter.c deleted file mode 100644 index f0bfb7980c9..00000000000 --- a/src/filter.c +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "fileops.h" -#include "hash.h" -#include "filter.h" -#include "repository.h" -#include "git2/config.h" -#include "blob.h" - -int git_filters_load(git_vector *filters, git_repository *repo, const char *path, int mode) -{ - int error; - - if (mode == GIT_FILTER_TO_ODB) { - /* Load the CRLF cleanup filter when writing to the ODB */ - error = git_filter_add__crlf_to_odb(filters, repo, path); - if (error < 0) - return error; - } else { - error = git_filter_add__crlf_to_workdir(filters, repo, path); - if (error < 0) - return error; - } - - return (int)filters->length; -} - -void git_filters_free(git_vector *filters) -{ - size_t i; - git_filter *filter; - - git_vector_foreach(filters, i, filter) { - if (filter->do_free != NULL) - filter->do_free(filter); - else - git__free(filter); - } - - git_vector_free(filters); -} - -int git_filters_apply(git_buf *dest, git_buf *source, git_vector *filters) -{ - unsigned int i, src; - git_buf *dbuffer[2]; - - dbuffer[0] = source; - dbuffer[1] = dest; - - src = 0; - - if (git_buf_len(source) == 0) { - git_buf_clear(dest); - return 0; - } - - /* Pre-grow the destination buffer to more or less the size - * we expect it to have */ - if (git_buf_grow(dest, git_buf_len(source)) < 0) - return -1; - - for (i = 0; i < filters->length; ++i) { - git_filter *filter = git_vector_get(filters, i); - unsigned int dst = 1 - src; - - git_buf_clear(dbuffer[dst]); - - /* Apply the filter from dbuffer[src] to the other buffer; - * if the filtering is canceled by the user mid-filter, - * we skip to the next filter without changing the source - * of the double buffering (so that the text goes through - * cleanly). - */ - if (filter->apply(filter, dbuffer[dst], dbuffer[src]) == 0) - src = dst; - - if (git_buf_oom(dbuffer[dst])) - return -1; - } - - /* Ensure that the output ends up in dbuffer[1] (i.e. the dest) */ - if (src != 1) - git_buf_swap(dest, source); - - return 0; -} diff --git a/src/filter.h b/src/filter.h deleted file mode 100644 index 0ca71656bff..00000000000 --- a/src/filter.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_filter_h__ -#define INCLUDE_filter_h__ - -#include "common.h" -#include "buffer.h" -#include "buf_text.h" -#include "git2/odb.h" -#include "git2/repository.h" - -typedef struct git_filter { - int (*apply)(struct git_filter *self, git_buf *dest, const git_buf *source); - void (*do_free)(struct git_filter *self); -} git_filter; - -typedef enum { - GIT_FILTER_TO_WORKTREE, - GIT_FILTER_TO_ODB -} git_filter_mode; - -typedef enum { - GIT_CRLF_GUESS = -1, - GIT_CRLF_BINARY = 0, - GIT_CRLF_TEXT, - GIT_CRLF_INPUT, - GIT_CRLF_CRLF, - GIT_CRLF_AUTO, -} git_crlf_t; - -/* - * FILTER API - */ - -/* - * For any given path in the working directory, fill the `filters` - * array with the relevant filters that need to be applied. - * - * Mode is either `GIT_FILTER_TO_WORKTREE` if you need to load the - * filters that will be used when checking out a file to the working - * directory, or `GIT_FILTER_TO_ODB` for the filters used when writing - * a file to the ODB. - * - * @param filters Vector where to store all the loaded filters - * @param repo Repository object that contains `path` - * @param path Relative path of the file to be filtered - * @param mode Filtering direction (WT->ODB or ODB->WT) - * @return the number of filters loaded for the file (0 if the file - * doesn't need filtering), or a negative error code - */ -extern int git_filters_load(git_vector *filters, git_repository *repo, const char *path, int mode); - -/* - * Apply one or more filters to a file. - * - * The file must have been loaded as a `git_buf` object. Both the `source` - * and `dest` buffers are owned by the caller and must be freed once - * they are no longer needed. - * - * NOTE: Because of the double-buffering schema, the `source` buffer that contains - * the original file may be tampered once the filtering is complete. Regardless, - * the `dest` buffer will always contain the final result of the filtering - * - * @param dest Buffer to store the result of the filtering - * @param source Buffer containing the document to filter - * @param filters A non-empty vector of filters as supplied by `git_filters_load` - * @return 0 on success, an error code otherwise - */ -extern int git_filters_apply(git_buf *dest, git_buf *source, git_vector *filters); - -/* - * Free the `filters` array generated by `git_filters_load`. - * - * Note that this frees both the array and its contents. The array will - * be clean/reusable after this call. - * - * @param filters A filters array as supplied by `git_filters_load` - */ -extern void git_filters_free(git_vector *filters); - -/* - * Available filters - */ - -/* Strip CRLF, from Worktree to ODB */ -extern int git_filter_add__crlf_to_odb(git_vector *filters, git_repository *repo, const char *path); - -/* Add CRLF, from ODB to worktree */ -extern int git_filter_add__crlf_to_workdir(git_vector *filters, git_repository *repo, const char *path); - -#endif diff --git a/src/fnmatch.c b/src/fnmatch.c deleted file mode 100644 index e3e47f37b96..00000000000 --- a/src/fnmatch.c +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -/* - * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6. - * Compares a filename or pathname to a pattern. - */ - -#include -#include -#include - -#include "fnmatch.h" - -#define EOS '\0' - -#define RANGE_MATCH 1 -#define RANGE_NOMATCH 0 -#define RANGE_ERROR (-1) - -static int rangematch(const char *, char, int, char **); - -static int -p_fnmatchx(const char *pattern, const char *string, int flags, size_t recurs) -{ - const char *stringstart; - char *newp; - char c, test; - - if (recurs-- == 0) - return FNM_NORES; - - for (stringstart = string;;) - switch (c = *pattern++) { - case EOS: - if ((flags & FNM_LEADING_DIR) && *string == '/') - return (0); - return (*string == EOS ? 0 : FNM_NOMATCH); - case '?': - if (*string == EOS) - return (FNM_NOMATCH); - if (*string == '/' && (flags & FNM_PATHNAME)) - return (FNM_NOMATCH); - if (*string == '.' && (flags & FNM_PERIOD) && - (string == stringstart || - ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) - return (FNM_NOMATCH); - ++string; - break; - case '*': - c = *pattern; - /* Collapse multiple stars. */ - while (c == '*') - c = *++pattern; - - if (*string == '.' && (flags & FNM_PERIOD) && - (string == stringstart || - ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) - return (FNM_NOMATCH); - - /* Optimize for pattern with * at end or before /. */ - if (c == EOS) { - if (flags & FNM_PATHNAME) - return ((flags & FNM_LEADING_DIR) || - strchr(string, '/') == NULL ? - 0 : FNM_NOMATCH); - else - return (0); - } else if (c == '/' && (flags & FNM_PATHNAME)) { - if ((string = strchr(string, '/')) == NULL) - return (FNM_NOMATCH); - break; - } - - /* General case, use recursion. */ - while ((test = *string) != EOS) { - int e; - - e = p_fnmatchx(pattern, string, flags & ~FNM_PERIOD, recurs); - if (e != FNM_NOMATCH) - return e; - if (test == '/' && (flags & FNM_PATHNAME)) - break; - ++string; - } - return (FNM_NOMATCH); - case '[': - if (*string == EOS) - return (FNM_NOMATCH); - if (*string == '/' && (flags & FNM_PATHNAME)) - return (FNM_NOMATCH); - if (*string == '.' && (flags & FNM_PERIOD) && - (string == stringstart || - ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) - return (FNM_NOMATCH); - - switch (rangematch(pattern, *string, flags, &newp)) { - case RANGE_ERROR: - /* not a good range, treat as normal text */ - goto normal; - case RANGE_MATCH: - pattern = newp; - break; - case RANGE_NOMATCH: - return (FNM_NOMATCH); - } - ++string; - break; - case '\\': - if (!(flags & FNM_NOESCAPE)) { - if ((c = *pattern++) == EOS) { - c = '\\'; - --pattern; - } - } - /* FALLTHROUGH */ - default: - normal: - if (c != *string && !((flags & FNM_CASEFOLD) && - (tolower((unsigned char)c) == - tolower((unsigned char)*string)))) - return (FNM_NOMATCH); - ++string; - break; - } - /* NOTREACHED */ -} - -static int -rangematch(const char *pattern, char test, int flags, char **newp) -{ - int negate, ok; - char c, c2; - - /* - * A bracket expression starting with an unquoted circumflex - * character produces unspecified results (IEEE 1003.2-1992, - * 3.13.2). This implementation treats it like '!', for - * consistency with the regular expression syntax. - * J.T. Conklin (conklin@ngai.kaleida.com) - */ - if ((negate = (*pattern == '!' || *pattern == '^')) != 0) - ++pattern; - - if (flags & FNM_CASEFOLD) - test = (char)tolower((unsigned char)test); - - /* - * A right bracket shall lose its special meaning and represent - * itself in a bracket expression if it occurs first in the list. - * -- POSIX.2 2.8.3.2 - */ - ok = 0; - c = *pattern++; - do { - if (c == '\\' && !(flags & FNM_NOESCAPE)) - c = *pattern++; - if (c == EOS) - return (RANGE_ERROR); - if (c == '/' && (flags & FNM_PATHNAME)) - return (RANGE_NOMATCH); - if ((flags & FNM_CASEFOLD)) - c = (char)tolower((unsigned char)c); - if (*pattern == '-' - && (c2 = *(pattern+1)) != EOS && c2 != ']') { - pattern += 2; - if (c2 == '\\' && !(flags & FNM_NOESCAPE)) - c2 = *pattern++; - if (c2 == EOS) - return (RANGE_ERROR); - if (flags & FNM_CASEFOLD) - c2 = (char)tolower((unsigned char)c2); - if (c <= test && test <= c2) - ok = 1; - } else if (c == test) - ok = 1; - } while ((c = *pattern++) != ']'); - - *newp = (char *)pattern; - return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH); -} - -int -p_fnmatch(const char *pattern, const char *string, int flags) -{ - return p_fnmatchx(pattern, string, flags, 64); -} - diff --git a/src/fnmatch.h b/src/fnmatch.h deleted file mode 100644 index 920e7de4d88..00000000000 --- a/src/fnmatch.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_fnmatch__compat_h__ -#define INCLUDE_fnmatch__compat_h__ - -#include "common.h" - -#define FNM_NOMATCH 1 /* Match failed. */ -#define FNM_NOSYS 2 /* Function not supported (unused). */ -#define FNM_NORES 3 /* Out of resources */ - -#define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */ -#define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */ -#define FNM_PERIOD 0x04 /* Period must be matched by period. */ -#define FNM_LEADING_DIR 0x08 /* Ignore / after Imatch. */ -#define FNM_CASEFOLD 0x10 /* Case insensitive search. */ - -#define FNM_IGNORECASE FNM_CASEFOLD -#define FNM_FILE_NAME FNM_PATHNAME - -extern int p_fnmatch(const char *pattern, const char *string, int flags); - -#endif /* _FNMATCH_H */ - diff --git a/src/global.c b/src/global.c deleted file mode 100644 index 4d37fa1d2ee..00000000000 --- a/src/global.c +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "common.h" -#include "global.h" -#include "hash.h" -#include "git2/threads.h" -#include "thread-utils.h" - - -git_mutex git__mwindow_mutex; - -/** - * Handle the global state with TLS - * - * If libgit2 is built with GIT_THREADS enabled, - * the `git_threads_init()` function must be called - * before calling any other function of the library. - * - * This function allocates a TLS index (using pthreads - * or the native Win32 API) to store the global state - * on a per-thread basis. - * - * Any internal method that requires global state will - * then call `git__global_state()` which returns a pointer - * to the global state structure; this pointer is lazily - * allocated on each thread. - * - * Before shutting down the library, the - * `git_threads_shutdown` method must be called to free - * the previously reserved TLS index. - * - * If libgit2 is built without threading support, the - * `git__global_statestate()` call returns a pointer to a single, - * statically allocated global state. The `git_thread_` - * functions are not available in that case. - */ - -/* - * `git_threads_init()` allows subsystems to perform global setup, - * which may take place in the global scope. An explicit memory - * fence exists at the exit of `git_threads_init()`. Without this, - * CPU cores are free to reorder cache invalidation of `_tls_init` - * before cache invalidation of the subsystems' newly written global - * state. - */ -#if defined(GIT_THREADS) && defined(GIT_WIN32) - -static DWORD _tls_index; -static int _tls_init = 0; - -int git_threads_init(void) -{ - int error; - - if (_tls_init) - return 0; - - _tls_index = TlsAlloc(); - git_mutex_init(&git__mwindow_mutex); - - /* Initialize any other subsystems that have global state */ - if ((error = git_hash_global_init()) >= 0) - _tls_init = 1; - - if (error == 0) - _tls_init = 1; - - GIT_MEMORY_BARRIER; - - return error; -} - -void git_threads_shutdown(void) -{ - TlsFree(_tls_index); - _tls_init = 0; - git_mutex_free(&git__mwindow_mutex); - - /* Shut down any subsystems that have global state */ - git_hash_global_shutdown(); -} - -git_global_st *git__global_state(void) -{ - void *ptr; - - assert(_tls_init); - - if ((ptr = TlsGetValue(_tls_index)) != NULL) - return ptr; - - ptr = git__malloc(sizeof(git_global_st)); - if (!ptr) - return NULL; - - memset(ptr, 0x0, sizeof(git_global_st)); - TlsSetValue(_tls_index, ptr); - return ptr; -} - -#elif defined(GIT_THREADS) && defined(_POSIX_THREADS) - -static pthread_key_t _tls_key; -static int _tls_init = 0; - -static void cb__free_status(void *st) -{ - git__free(st); -} - -int git_threads_init(void) -{ - int error = 0; - - if (_tls_init) - return 0; - - git_mutex_init(&git__mwindow_mutex); - pthread_key_create(&_tls_key, &cb__free_status); - - /* Initialize any other subsystems that have global state */ - if ((error = git_hash_global_init()) >= 0) - _tls_init = 1; - - GIT_MEMORY_BARRIER; - - return error; -} - -void git_threads_shutdown(void) -{ - pthread_key_delete(_tls_key); - _tls_init = 0; - git_mutex_free(&git__mwindow_mutex); - - /* Shut down any subsystems that have global state */ - git_hash_global_shutdown(); -} - -git_global_st *git__global_state(void) -{ - void *ptr; - - assert(_tls_init); - - if ((ptr = pthread_getspecific(_tls_key)) != NULL) - return ptr; - - ptr = git__malloc(sizeof(git_global_st)); - if (!ptr) - return NULL; - - memset(ptr, 0x0, sizeof(git_global_st)); - pthread_setspecific(_tls_key, ptr); - return ptr; -} - -#else - -static git_global_st __state; - -int git_threads_init(void) -{ - /* noop */ - return 0; -} - -void git_threads_shutdown(void) -{ - /* noop */ -} - -git_global_st *git__global_state(void) -{ - return &__state; -} - -#endif /* GIT_THREADS */ diff --git a/src/global.h b/src/global.h deleted file mode 100644 index f0ad1df29d7..00000000000 --- a/src/global.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_global_h__ -#define INCLUDE_global_h__ - -#include "mwindow.h" -#include "hash.h" - -#if defined(GIT_THREADS) && defined(_MSC_VER) -# define GIT_MEMORY_BARRIER MemoryBarrier() -#elif defined(GIT_THREADS) -# define GIT_MEMORY_BARRIER __sync_synchronize() -#else -# define GIT_MEMORY_BARRIER /* noop */ -#endif - -typedef struct { - git_error *last_error; - git_error error_t; -} git_global_st; - -git_global_st *git__global_state(void); - -extern git_mutex git__mwindow_mutex; - -#define GIT_GLOBAL (git__global_state()) - -#endif diff --git a/src/graph.c b/src/graph.c deleted file mode 100644 index cb17279242a..00000000000 --- a/src/graph.c +++ /dev/null @@ -1,178 +0,0 @@ - -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "revwalk.h" -#include "merge.h" -#include "git2/graph.h" - -static int interesting(git_pqueue *list, git_commit_list *roots) -{ - unsigned int i; - /* element 0 isn't used - we need to start at 1 */ - for (i = 1; i < list->size; i++) { - git_commit_list_node *commit = list->d[i]; - if ((commit->flags & STALE) == 0) - return 1; - } - - while(roots) { - if ((roots->item->flags & STALE) == 0) - return 1; - roots = roots->next; - } - - return 0; -} - -static int mark_parents(git_revwalk *walk, git_commit_list_node *one, - git_commit_list_node *two) -{ - unsigned int i; - git_commit_list *roots = NULL; - git_pqueue list; - - /* if the commit is repeated, we have a our merge base already */ - if (one == two) { - one->flags |= PARENT1 | PARENT2 | RESULT; - return 0; - } - - if (git_pqueue_init(&list, 2, git_commit_list_time_cmp) < 0) - return -1; - - if (git_commit_list_parse(walk, one) < 0) - goto on_error; - one->flags |= PARENT1; - if (git_pqueue_insert(&list, one) < 0) - goto on_error; - - if (git_commit_list_parse(walk, two) < 0) - goto on_error; - two->flags |= PARENT2; - if (git_pqueue_insert(&list, two) < 0) - goto on_error; - - /* as long as there are non-STALE commits */ - while (interesting(&list, roots)) { - git_commit_list_node *commit; - int flags; - - commit = git_pqueue_pop(&list); - if (commit == NULL) - break; - - flags = commit->flags & (PARENT1 | PARENT2 | STALE); - if (flags == (PARENT1 | PARENT2)) { - if (!(commit->flags & RESULT)) - commit->flags |= RESULT; - /* we mark the parents of a merge stale */ - flags |= STALE; - } - - for (i = 0; i < commit->out_degree; i++) { - git_commit_list_node *p = commit->parents[i]; - if ((p->flags & flags) == flags) - continue; - - if (git_commit_list_parse(walk, p) < 0) - goto on_error; - - p->flags |= flags; - if (git_pqueue_insert(&list, p) < 0) - goto on_error; - } - - /* Keep track of root commits, to make sure the path gets marked */ - if (commit->out_degree == 0) { - if (git_commit_list_insert(commit, &roots) == NULL) - goto on_error; - } - } - - git_commit_list_free(&roots); - git_pqueue_free(&list); - return 0; - -on_error: - git_commit_list_free(&roots); - git_pqueue_free(&list); - return -1; -} - - -static int ahead_behind(git_commit_list_node *one, git_commit_list_node *two, - size_t *ahead, size_t *behind) -{ - git_commit_list_node *commit; - git_pqueue pq; - int i; - *ahead = 0; - *behind = 0; - - if (git_pqueue_init(&pq, 2, git_commit_list_time_cmp) < 0) - return -1; - if (git_pqueue_insert(&pq, one) < 0) - goto on_error; - if (git_pqueue_insert(&pq, two) < 0) - goto on_error; - - while ((commit = git_pqueue_pop(&pq)) != NULL) { - if (commit->flags & RESULT || - (commit->flags & (PARENT1 | PARENT2)) == (PARENT1 | PARENT2)) - continue; - else if (commit->flags & PARENT1) - (*behind)++; - else if (commit->flags & PARENT2) - (*ahead)++; - - for (i = 0; i < commit->out_degree; i++) { - git_commit_list_node *p = commit->parents[i]; - if (git_pqueue_insert(&pq, p) < 0) - return -1; - } - commit->flags |= RESULT; - } - - git_pqueue_free(&pq); - return 0; - -on_error: - git_pqueue_free(&pq); - return -1; -} - -int git_graph_ahead_behind(size_t *ahead, size_t *behind, git_repository *repo, - const git_oid *one, const git_oid *two) -{ - git_revwalk *walk; - git_commit_list_node *commit1, *commit2; - - if (git_revwalk_new(&walk, repo) < 0) - return -1; - - commit2 = git_revwalk__commit_lookup(walk, two); - if (commit2 == NULL) - goto on_error; - - commit1 = git_revwalk__commit_lookup(walk, one); - if (commit1 == NULL) - goto on_error; - - if (mark_parents(walk, commit1, commit2) < 0) - goto on_error; - if (ahead_behind(commit1, commit2, ahead, behind) < 0) - goto on_error; - - git_revwalk_free(walk); - - return 0; - -on_error: - git_revwalk_free(walk); - return -1; -} diff --git a/src/hash.c b/src/hash.c deleted file mode 100644 index f3645a91393..00000000000 --- a/src/hash.c +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "hash.h" - -int git_hash_buf(git_oid *out, const void *data, size_t len) -{ - git_hash_ctx ctx; - int error = 0; - - if (git_hash_ctx_init(&ctx) < 0) - return -1; - - if ((error = git_hash_update(&ctx, data, len)) >= 0) - error = git_hash_final(out, &ctx); - - git_hash_ctx_cleanup(&ctx); - - return error; -} - -int git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n) -{ - git_hash_ctx ctx; - size_t i; - int error = 0; - - if (git_hash_ctx_init(&ctx) < 0) - return -1; - - for (i = 0; i < n; i++) { - if ((error = git_hash_update(&ctx, vec[i].data, vec[i].len)) < 0) - goto done; - } - - error = git_hash_final(out, &ctx); - -done: - git_hash_ctx_cleanup(&ctx); - - return error; -} diff --git a/src/hash.h b/src/hash.h deleted file mode 100644 index 5b848981f4b..00000000000 --- a/src/hash.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_hash_h__ -#define INCLUDE_hash_h__ - -#include "git2/oid.h" - -typedef struct git_hash_prov git_hash_prov; -typedef struct git_hash_ctx git_hash_ctx; - -int git_hash_global_init(void); -void git_hash_global_shutdown(void); - -int git_hash_ctx_init(git_hash_ctx *ctx); -void git_hash_ctx_cleanup(git_hash_ctx *ctx); - -#if defined(OPENSSL_SHA1) -# include "hash/hash_openssl.h" -#elif defined(WIN32_SHA1) -# include "hash/hash_win32.h" -#else -# include "hash/hash_generic.h" -#endif - -typedef struct { - void *data; - size_t len; -} git_buf_vec; - -int git_hash_init(git_hash_ctx *c); -int git_hash_update(git_hash_ctx *c, const void *data, size_t len); -int git_hash_final(git_oid *out, git_hash_ctx *c); - -int git_hash_buf(git_oid *out, const void *data, size_t len); -int git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n); - -#endif /* INCLUDE_hash_h__ */ diff --git a/src/hash/hash_generic.c b/src/hash/hash_generic.c deleted file mode 100644 index 0723bfaf96e..00000000000 --- a/src/hash/hash_generic.c +++ /dev/null @@ -1,288 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "hash.h" -#include "hash/hash_generic.h" - -#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) - -/* - * Force usage of rol or ror by selecting the one with the smaller constant. - * It _can_ generate slightly smaller code (a constant of 1 is special), but - * perhaps more importantly it's possibly faster on any uarch that does a - * rotate with a loop. - */ - -#define SHA_ASM(op, x, n) ({ unsigned int __res; __asm__(op " %1,%0":"=r" (__res):"i" (n), "0" (x)); __res; }) -#define SHA_ROL(x,n) SHA_ASM("rol", x, n) -#define SHA_ROR(x,n) SHA_ASM("ror", x, n) - -#else - -#define SHA_ROT(X,l,r) (((X) << (l)) | ((X) >> (r))) -#define SHA_ROL(X,n) SHA_ROT(X,n,32-(n)) -#define SHA_ROR(X,n) SHA_ROT(X,32-(n),n) - -#endif - -/* - * If you have 32 registers or more, the compiler can (and should) - * try to change the array[] accesses into registers. However, on - * machines with less than ~25 registers, that won't really work, - * and at least gcc will make an unholy mess of it. - * - * So to avoid that mess which just slows things down, we force - * the stores to memory to actually happen (we might be better off - * with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as - * suggested by Artur Skawina - that will also make gcc unable to - * try to do the silly "optimize away loads" part because it won't - * see what the value will be). - * - * Ben Herrenschmidt reports that on PPC, the C version comes close - * to the optimized asm with this (ie on PPC you don't want that - * 'volatile', since there are lots of registers). - * - * On ARM we get the best code generation by forcing a full memory barrier - * between each SHA_ROUND, otherwise gcc happily get wild with spilling and - * the stack frame size simply explode and performance goes down the drain. - */ - -#if defined(__i386__) || defined(__x86_64__) - #define setW(x, val) (*(volatile unsigned int *)&W(x) = (val)) -#elif defined(__GNUC__) && defined(__arm__) - #define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0) -#else - #define setW(x, val) (W(x) = (val)) -#endif - -/* - * Performance might be improved if the CPU architecture is OK with - * unaligned 32-bit loads and a fast ntohl() is available. - * Otherwise fall back to byte loads and shifts which is portable, - * and is faster on architectures with memory alignment issues. - */ - -#if defined(__i386__) || defined(__x86_64__) || \ - defined(_M_IX86) || defined(_M_X64) || \ - defined(__ppc__) || defined(__ppc64__) || \ - defined(__powerpc__) || defined(__powerpc64__) || \ - defined(__s390__) || defined(__s390x__) - -#define get_be32(p) ntohl(*(const unsigned int *)(p)) -#define put_be32(p, v) do { *(unsigned int *)(p) = htonl(v); } while (0) - -#else - -#define get_be32(p) ( \ - (*((const unsigned char *)(p) + 0) << 24) | \ - (*((const unsigned char *)(p) + 1) << 16) | \ - (*((const unsigned char *)(p) + 2) << 8) | \ - (*((const unsigned char *)(p) + 3) << 0) ) -#define put_be32(p, v) do { \ - unsigned int __v = (v); \ - *((unsigned char *)(p) + 0) = __v >> 24; \ - *((unsigned char *)(p) + 1) = __v >> 16; \ - *((unsigned char *)(p) + 2) = __v >> 8; \ - *((unsigned char *)(p) + 3) = __v >> 0; } while (0) - -#endif - -/* This "rolls" over the 512-bit array */ -#define W(x) (array[(x)&15]) - -/* - * Where do we get the source from? The first 16 iterations get it from - * the input data, the next mix it from the 512-bit array. - */ -#define SHA_SRC(t) get_be32(data + t) -#define SHA_MIX(t) SHA_ROL(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1) - -#define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \ - unsigned int TEMP = input(t); setW(t, TEMP); \ - E += TEMP + SHA_ROL(A,5) + (fn) + (constant); \ - B = SHA_ROR(B, 2); } while (0) - -#define T_0_15(t, A, B, C, D, E) SHA_ROUND(t, SHA_SRC, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E ) -#define T_16_19(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E ) -#define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E ) -#define T_40_59(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, ((B&C)+(D&(B^C))) , 0x8f1bbcdc, A, B, C, D, E ) -#define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E ) - -static void hash__block(git_hash_ctx *ctx, const unsigned int *data) -{ - unsigned int A,B,C,D,E; - unsigned int array[16]; - - A = ctx->H[0]; - B = ctx->H[1]; - C = ctx->H[2]; - D = ctx->H[3]; - E = ctx->H[4]; - - /* Round 1 - iterations 0-16 take their input from 'data' */ - T_0_15( 0, A, B, C, D, E); - T_0_15( 1, E, A, B, C, D); - T_0_15( 2, D, E, A, B, C); - T_0_15( 3, C, D, E, A, B); - T_0_15( 4, B, C, D, E, A); - T_0_15( 5, A, B, C, D, E); - T_0_15( 6, E, A, B, C, D); - T_0_15( 7, D, E, A, B, C); - T_0_15( 8, C, D, E, A, B); - T_0_15( 9, B, C, D, E, A); - T_0_15(10, A, B, C, D, E); - T_0_15(11, E, A, B, C, D); - T_0_15(12, D, E, A, B, C); - T_0_15(13, C, D, E, A, B); - T_0_15(14, B, C, D, E, A); - T_0_15(15, A, B, C, D, E); - - /* Round 1 - tail. Input from 512-bit mixing array */ - T_16_19(16, E, A, B, C, D); - T_16_19(17, D, E, A, B, C); - T_16_19(18, C, D, E, A, B); - T_16_19(19, B, C, D, E, A); - - /* Round 2 */ - T_20_39(20, A, B, C, D, E); - T_20_39(21, E, A, B, C, D); - T_20_39(22, D, E, A, B, C); - T_20_39(23, C, D, E, A, B); - T_20_39(24, B, C, D, E, A); - T_20_39(25, A, B, C, D, E); - T_20_39(26, E, A, B, C, D); - T_20_39(27, D, E, A, B, C); - T_20_39(28, C, D, E, A, B); - T_20_39(29, B, C, D, E, A); - T_20_39(30, A, B, C, D, E); - T_20_39(31, E, A, B, C, D); - T_20_39(32, D, E, A, B, C); - T_20_39(33, C, D, E, A, B); - T_20_39(34, B, C, D, E, A); - T_20_39(35, A, B, C, D, E); - T_20_39(36, E, A, B, C, D); - T_20_39(37, D, E, A, B, C); - T_20_39(38, C, D, E, A, B); - T_20_39(39, B, C, D, E, A); - - /* Round 3 */ - T_40_59(40, A, B, C, D, E); - T_40_59(41, E, A, B, C, D); - T_40_59(42, D, E, A, B, C); - T_40_59(43, C, D, E, A, B); - T_40_59(44, B, C, D, E, A); - T_40_59(45, A, B, C, D, E); - T_40_59(46, E, A, B, C, D); - T_40_59(47, D, E, A, B, C); - T_40_59(48, C, D, E, A, B); - T_40_59(49, B, C, D, E, A); - T_40_59(50, A, B, C, D, E); - T_40_59(51, E, A, B, C, D); - T_40_59(52, D, E, A, B, C); - T_40_59(53, C, D, E, A, B); - T_40_59(54, B, C, D, E, A); - T_40_59(55, A, B, C, D, E); - T_40_59(56, E, A, B, C, D); - T_40_59(57, D, E, A, B, C); - T_40_59(58, C, D, E, A, B); - T_40_59(59, B, C, D, E, A); - - /* Round 4 */ - T_60_79(60, A, B, C, D, E); - T_60_79(61, E, A, B, C, D); - T_60_79(62, D, E, A, B, C); - T_60_79(63, C, D, E, A, B); - T_60_79(64, B, C, D, E, A); - T_60_79(65, A, B, C, D, E); - T_60_79(66, E, A, B, C, D); - T_60_79(67, D, E, A, B, C); - T_60_79(68, C, D, E, A, B); - T_60_79(69, B, C, D, E, A); - T_60_79(70, A, B, C, D, E); - T_60_79(71, E, A, B, C, D); - T_60_79(72, D, E, A, B, C); - T_60_79(73, C, D, E, A, B); - T_60_79(74, B, C, D, E, A); - T_60_79(75, A, B, C, D, E); - T_60_79(76, E, A, B, C, D); - T_60_79(77, D, E, A, B, C); - T_60_79(78, C, D, E, A, B); - T_60_79(79, B, C, D, E, A); - - ctx->H[0] += A; - ctx->H[1] += B; - ctx->H[2] += C; - ctx->H[3] += D; - ctx->H[4] += E; -} - -int git_hash_init(git_hash_ctx *ctx) -{ - ctx->size = 0; - - /* Initialize H with the magic constants (see FIPS180 for constants) */ - ctx->H[0] = 0x67452301; - ctx->H[1] = 0xefcdab89; - ctx->H[2] = 0x98badcfe; - ctx->H[3] = 0x10325476; - ctx->H[4] = 0xc3d2e1f0; - - return 0; -} - -int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len) -{ - unsigned int lenW = ctx->size & 63; - - ctx->size += len; - - /* Read the data into W and process blocks as they get full */ - if (lenW) { - unsigned int left = 64 - lenW; - if (len < left) - left = (unsigned int)len; - memcpy(lenW + (char *)ctx->W, data, left); - lenW = (lenW + left) & 63; - len -= left; - data = ((const char *)data + left); - if (lenW) - return 0; - hash__block(ctx, ctx->W); - } - while (len >= 64) { - hash__block(ctx, data); - data = ((const char *)data + 64); - len -= 64; - } - if (len) - memcpy(ctx->W, data, len); - - return 0; -} - -int git_hash_final(git_oid *out, git_hash_ctx *ctx) -{ - static const unsigned char pad[64] = { 0x80 }; - unsigned int padlen[2]; - int i; - - /* Pad with a binary 1 (ie 0x80), then zeroes, then length */ - padlen[0] = htonl((uint32_t)(ctx->size >> 29)); - padlen[1] = htonl((uint32_t)(ctx->size << 3)); - - i = ctx->size & 63; - git_hash_update(ctx, pad, 1+ (63 & (55 - i))); - git_hash_update(ctx, padlen, 8); - - /* Output hash */ - for (i = 0; i < 5; i++) - put_be32(out->id + i*4, ctx->H[i]); - - return 0; -} - diff --git a/src/hash/hash_generic.h b/src/hash/hash_generic.h deleted file mode 100644 index b731de8b309..00000000000 --- a/src/hash/hash_generic.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#ifndef INCLUDE_hash_generic_h__ -#define INCLUDE_hash_generic_h__ - -#include "hash.h" - -struct git_hash_ctx { - unsigned long long size; - unsigned int H[5]; - unsigned int W[16]; -}; - -#define git_hash_global_init() 0 -#define git_hash_global_shutdown() /* noop */ -#define git_hash_ctx_init(ctx) git_hash_init(ctx) -#define git_hash_ctx_cleanup(ctx) - -#endif /* INCLUDE_hash_generic_h__ */ diff --git a/src/hash/hash_openssl.h b/src/hash/hash_openssl.h deleted file mode 100644 index f83279a5a1b..00000000000 --- a/src/hash/hash_openssl.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#ifndef INCLUDE_hash_openssl_h__ -#define INCLUDE_hash_openssl_h__ - -#include "hash.h" - -#include - -struct git_hash_ctx { - SHA_CTX c; -}; - -#define git_hash_global_init() 0 -#define git_hash_global_shutdown() /* noop */ -#define git_hash_ctx_init(ctx) git_hash_init(ctx) -#define git_hash_ctx_cleanup(ctx) - -GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx) -{ - assert(ctx); - SHA1_Init(&ctx->c); - return 0; -} - -GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *data, size_t len) -{ - assert(ctx); - SHA1_Update(&ctx->c, data, len); - return 0; -} - -GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx) -{ - assert(ctx); - SHA1_Final(out->id, &ctx->c); - return 0; -} - -#endif /* INCLUDE_hash_openssl_h__ */ diff --git a/src/hash/hash_win32.c b/src/hash/hash_win32.c deleted file mode 100644 index 43d54ca6d14..00000000000 --- a/src/hash/hash_win32.c +++ /dev/null @@ -1,291 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "global.h" -#include "hash.h" -#include "hash/hash_win32.h" - -#include -#include - -static struct git_hash_prov hash_prov = {0}; - -/* Hash initialization */ - -/* Initialize CNG, if available */ -GIT_INLINE(int) hash_cng_prov_init(void) -{ - OSVERSIONINFOEX version_test = {0}; - DWORD version_test_mask; - DWORDLONG version_condition_mask = 0; - char dll_path[MAX_PATH]; - DWORD dll_path_len, size_len; - - return -1; - - /* Only use CNG on Windows 2008 / Vista SP1 or better (Windows 6.0 SP1) */ - version_test.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); - version_test.dwMajorVersion = 6; - version_test.dwMinorVersion = 0; - version_test.wServicePackMajor = 1; - version_test.wServicePackMinor = 0; - - version_test_mask = (VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR); - - VER_SET_CONDITION(version_condition_mask, VER_MAJORVERSION, VER_GREATER_EQUAL); - VER_SET_CONDITION(version_condition_mask, VER_MINORVERSION, VER_GREATER_EQUAL); - VER_SET_CONDITION(version_condition_mask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL); - VER_SET_CONDITION(version_condition_mask, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL); - - if (!VerifyVersionInfo(&version_test, version_test_mask, version_condition_mask)) - return -1; - - /* Load bcrypt.dll explicitly from the system directory */ - if ((dll_path_len = GetSystemDirectory(dll_path, MAX_PATH)) == 0 || dll_path_len > MAX_PATH || - StringCchCat(dll_path, MAX_PATH, "\\") < 0 || - StringCchCat(dll_path, MAX_PATH, GIT_HASH_CNG_DLL_NAME) < 0 || - (hash_prov.prov.cng.dll = LoadLibrary(dll_path)) == NULL) - return -1; - - /* Load the function addresses */ - if ((hash_prov.prov.cng.open_algorithm_provider = (hash_win32_cng_open_algorithm_provider_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptOpenAlgorithmProvider")) == NULL || - (hash_prov.prov.cng.get_property = (hash_win32_cng_get_property_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptGetProperty")) == NULL || - (hash_prov.prov.cng.create_hash = (hash_win32_cng_create_hash_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptCreateHash")) == NULL || - (hash_prov.prov.cng.finish_hash = (hash_win32_cng_finish_hash_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptFinishHash")) == NULL || - (hash_prov.prov.cng.hash_data = (hash_win32_cng_hash_data_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptHashData")) == NULL || - (hash_prov.prov.cng.destroy_hash = (hash_win32_cng_destroy_hash_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptDestroyHash")) == NULL || - (hash_prov.prov.cng.close_algorithm_provider = (hash_win32_cng_close_algorithm_provider_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptCloseAlgorithmProvider")) == NULL) { - FreeLibrary(hash_prov.prov.cng.dll); - return -1; - } - - /* Load the SHA1 algorithm */ - if (hash_prov.prov.cng.open_algorithm_provider(&hash_prov.prov.cng.handle, GIT_HASH_CNG_HASH_TYPE, NULL, GIT_HASH_CNG_HASH_REUSABLE) < 0) { - FreeLibrary(hash_prov.prov.cng.dll); - return -1; - } - - /* Get storage space for the hash object */ - if (hash_prov.prov.cng.get_property(hash_prov.prov.cng.handle, GIT_HASH_CNG_HASH_OBJECT_LEN, (PBYTE)&hash_prov.prov.cng.hash_object_size, sizeof(DWORD), &size_len, 0) < 0) { - hash_prov.prov.cng.close_algorithm_provider(hash_prov.prov.cng.handle, 0); - FreeLibrary(hash_prov.prov.cng.dll); - return -1; - } - - hash_prov.type = CNG; - return 0; -} - -GIT_INLINE(void) hash_cng_prov_shutdown(void) -{ - hash_prov.prov.cng.close_algorithm_provider(hash_prov.prov.cng.handle, 0); - FreeLibrary(hash_prov.prov.cng.dll); - - hash_prov.type = INVALID; -} - -/* Initialize CryptoAPI */ -GIT_INLINE(int) hash_cryptoapi_prov_init() -{ - if (!CryptAcquireContext(&hash_prov.prov.cryptoapi.handle, NULL, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) - return -1; - - hash_prov.type = CRYPTOAPI; - return 0; -} - -GIT_INLINE(void) hash_cryptoapi_prov_shutdown(void) -{ - CryptReleaseContext(hash_prov.prov.cryptoapi.handle, 0); - - hash_prov.type = INVALID; -} - -int git_hash_global_init() -{ - int error = 0; - - if (hash_prov.type != INVALID) - return 0; - - if ((error = hash_cng_prov_init()) < 0) - error = hash_cryptoapi_prov_init(); - - return error; -} - -void git_hash_global_shutdown() -{ - if (hash_prov.type == CNG) - hash_cng_prov_shutdown(); - else if(hash_prov.type == CRYPTOAPI) - hash_cryptoapi_prov_shutdown(); -} - -/* CryptoAPI: available in Windows XP and newer */ - -GIT_INLINE(int) hash_ctx_cryptoapi_init(git_hash_ctx *ctx) -{ - ctx->type = CRYPTOAPI; - ctx->prov = &hash_prov; - - return git_hash_init(ctx); -} - -GIT_INLINE(int) hash_cryptoapi_init(git_hash_ctx *ctx) -{ - if (ctx->ctx.cryptoapi.valid) - CryptDestroyHash(ctx->ctx.cryptoapi.hash_handle); - - if (!CryptCreateHash(ctx->prov->prov.cryptoapi.handle, CALG_SHA1, 0, 0, &ctx->ctx.cryptoapi.hash_handle)) { - ctx->ctx.cryptoapi.valid = 0; - return -1; - } - - ctx->ctx.cryptoapi.valid = 1; - return 0; -} - -GIT_INLINE(int) hash_cryptoapi_update(git_hash_ctx *ctx, const void *data, size_t len) -{ - assert(ctx->ctx.cryptoapi.valid); - - if (!CryptHashData(ctx->ctx.cryptoapi.hash_handle, (const BYTE *)data, (DWORD)len, 0)) - return -1; - - return 0; -} - -GIT_INLINE(int) hash_cryptoapi_final(git_oid *out, git_hash_ctx *ctx) -{ - DWORD len = 20; - int error = 0; - - assert(ctx->ctx.cryptoapi.valid); - - if (!CryptGetHashParam(ctx->ctx.cryptoapi.hash_handle, HP_HASHVAL, out->id, &len, 0)) - error = -1; - - CryptDestroyHash(ctx->ctx.cryptoapi.hash_handle); - ctx->ctx.cryptoapi.valid = 0; - - return error; -} - -GIT_INLINE(void) hash_ctx_cryptoapi_cleanup(git_hash_ctx *ctx) -{ - if (ctx->ctx.cryptoapi.valid) - CryptDestroyHash(ctx->ctx.cryptoapi.hash_handle); -} - -/* CNG: Available in Windows Server 2008 and newer */ - -GIT_INLINE(int) hash_ctx_cng_init(git_hash_ctx *ctx) -{ - if ((ctx->ctx.cng.hash_object = git__malloc(hash_prov.prov.cng.hash_object_size)) == NULL) - return -1; - - if (hash_prov.prov.cng.create_hash(hash_prov.prov.cng.handle, &ctx->ctx.cng.hash_handle, ctx->ctx.cng.hash_object, hash_prov.prov.cng.hash_object_size, NULL, 0, 0) < 0) { - git__free(ctx->ctx.cng.hash_object); - return -1; - } - - ctx->type = CNG; - ctx->prov = &hash_prov; - - return 0; -} - -GIT_INLINE(int) hash_cng_init(git_hash_ctx *ctx) -{ - BYTE hash[GIT_OID_RAWSZ]; - - if (!ctx->ctx.cng.updated) - return 0; - - /* CNG needs to be finished to restart */ - if (ctx->prov->prov.cng.finish_hash(ctx->ctx.cng.hash_handle, hash, GIT_OID_RAWSZ, 0) < 0) - return -1; - - ctx->ctx.cng.updated = 0; - - return 0; -} - -GIT_INLINE(int) hash_cng_update(git_hash_ctx *ctx, const void *data, size_t len) -{ - if (ctx->prov->prov.cng.hash_data(ctx->ctx.cng.hash_handle, (PBYTE)data, (ULONG)len, 0) < 0) - return -1; - - return 0; -} - -GIT_INLINE(int) hash_cng_final(git_oid *out, git_hash_ctx *ctx) -{ - if (ctx->prov->prov.cng.finish_hash(ctx->ctx.cng.hash_handle, out->id, GIT_OID_RAWSZ, 0) < 0) - return -1; - - ctx->ctx.cng.updated = 0; - - return 0; -} - -GIT_INLINE(void) hash_ctx_cng_cleanup(git_hash_ctx *ctx) -{ - ctx->prov->prov.cng.destroy_hash(ctx->ctx.cng.hash_handle); - git__free(ctx->ctx.cng.hash_object); -} - -/* Indirection between CryptoAPI and CNG */ - -int git_hash_ctx_init(git_hash_ctx *ctx) -{ - int error = 0; - - assert(ctx); - - /* - * When compiled with GIT_THREADS, the global hash_prov data is - * initialized with git_threads_init. Otherwise, it must be initialized - * at first use. - */ - if (hash_prov.type == INVALID && (error = git_hash_global_init()) < 0) - return error; - - memset(ctx, 0x0, sizeof(git_hash_ctx)); - - return (hash_prov.type == CNG) ? hash_ctx_cng_init(ctx) : hash_ctx_cryptoapi_init(ctx); -} - -int git_hash_init(git_hash_ctx *ctx) -{ - assert(ctx && ctx->type); - return (ctx->type == CNG) ? hash_cng_init(ctx) : hash_cryptoapi_init(ctx); -} - -int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len) -{ - assert(ctx && ctx->type); - return (ctx->type == CNG) ? hash_cng_update(ctx, data, len) : hash_cryptoapi_update(ctx, data, len); -} - -int git_hash_final(git_oid *out, git_hash_ctx *ctx) -{ - assert(ctx && ctx->type); - return (ctx->type == CNG) ? hash_cng_final(out, ctx) : hash_cryptoapi_final(out, ctx); -} - -void git_hash_ctx_cleanup(git_hash_ctx *ctx) -{ - assert(ctx); - - if (ctx->type == CNG) - hash_ctx_cng_cleanup(ctx); - else if(ctx->type == CRYPTOAPI) - hash_ctx_cryptoapi_cleanup(ctx); -} diff --git a/src/hash/hash_win32.h b/src/hash/hash_win32.h deleted file mode 100644 index daa769b5988..00000000000 --- a/src/hash/hash_win32.h +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#ifndef INCLUDE_hash_win32_h__ -#define INCLUDE_hash_win32_h__ - -#include "common.h" -#include "hash.h" - -#include -#include - -enum hash_win32_prov_type { - INVALID = 0, - CRYPTOAPI, - CNG -}; - -/* - * CryptoAPI is available for hashing on Windows XP and newer. - */ - -struct hash_cryptoapi_prov { - HCRYPTPROV handle; -}; - -/* - * CNG (bcrypt.dll) is significantly more performant than CryptoAPI and is - * preferred, however it is only available on Windows 2008 and newer and - * must therefore be dynamically loaded, and we must inline constants that - * would not exist when building in pre-Windows 2008 environments. - */ - -#define GIT_HASH_CNG_DLL_NAME "bcrypt.dll" - -/* BCRYPT_SHA1_ALGORITHM */ -#define GIT_HASH_CNG_HASH_TYPE L"SHA1" - -/* BCRYPT_OBJECT_LENGTH */ -#define GIT_HASH_CNG_HASH_OBJECT_LEN L"ObjectLength" - -/* BCRYPT_HASH_REUSEABLE_FLAGS */ -#define GIT_HASH_CNG_HASH_REUSABLE 0x00000020 - -/* Function declarations for CNG */ -typedef NTSTATUS (WINAPI *hash_win32_cng_open_algorithm_provider_fn)( - HANDLE /* BCRYPT_ALG_HANDLE */ *phAlgorithm, - LPCWSTR pszAlgId, - LPCWSTR pszImplementation, - DWORD dwFlags); - -typedef NTSTATUS (WINAPI *hash_win32_cng_get_property_fn)( - HANDLE /* BCRYPT_HANDLE */ hObject, - LPCWSTR pszProperty, - PUCHAR pbOutput, - ULONG cbOutput, - ULONG *pcbResult, - ULONG dwFlags); - -typedef NTSTATUS (WINAPI *hash_win32_cng_create_hash_fn)( - HANDLE /* BCRYPT_ALG_HANDLE */ hAlgorithm, - HANDLE /* BCRYPT_HASH_HANDLE */ *phHash, - PUCHAR pbHashObject, ULONG cbHashObject, - PUCHAR pbSecret, - ULONG cbSecret, - ULONG dwFlags); - -typedef NTSTATUS (WINAPI *hash_win32_cng_finish_hash_fn)( - HANDLE /* BCRYPT_HASH_HANDLE */ hHash, - PUCHAR pbOutput, - ULONG cbOutput, - ULONG dwFlags); - -typedef NTSTATUS (WINAPI *hash_win32_cng_hash_data_fn)( - HANDLE /* BCRYPT_HASH_HANDLE */ hHash, - PUCHAR pbInput, - ULONG cbInput, - ULONG dwFlags); - -typedef NTSTATUS (WINAPI *hash_win32_cng_destroy_hash_fn)( - HANDLE /* BCRYPT_HASH_HANDLE */ hHash); - -typedef NTSTATUS (WINAPI *hash_win32_cng_close_algorithm_provider_fn)( - HANDLE /* BCRYPT_ALG_HANDLE */ hAlgorithm, - ULONG dwFlags); - -struct hash_cng_prov { - /* DLL for CNG */ - HINSTANCE dll; - - /* Function pointers for CNG */ - hash_win32_cng_open_algorithm_provider_fn open_algorithm_provider; - hash_win32_cng_get_property_fn get_property; - hash_win32_cng_create_hash_fn create_hash; - hash_win32_cng_finish_hash_fn finish_hash; - hash_win32_cng_hash_data_fn hash_data; - hash_win32_cng_destroy_hash_fn destroy_hash; - hash_win32_cng_close_algorithm_provider_fn close_algorithm_provider; - - HANDLE /* BCRYPT_ALG_HANDLE */ handle; - DWORD hash_object_size; -}; - -struct git_hash_prov { - enum hash_win32_prov_type type; - - union { - struct hash_cryptoapi_prov cryptoapi; - struct hash_cng_prov cng; - } prov; -}; - -/* Hash contexts */ - -struct hash_cryptoapi_ctx { - bool valid; - HCRYPTHASH hash_handle; -}; - -struct hash_cng_ctx { - bool updated; - HANDLE /* BCRYPT_HASH_HANDLE */ hash_handle; - PBYTE hash_object; -}; - -struct git_hash_ctx { - enum hash_win32_prov_type type; - git_hash_prov *prov; - - union { - struct hash_cryptoapi_ctx cryptoapi; - struct hash_cng_ctx cng; - } ctx; -}; - -#endif /* INCLUDE_hash_openssl_h__ */ diff --git a/src/ignore.c b/src/ignore.c deleted file mode 100644 index 5edc5b65b2e..00000000000 --- a/src/ignore.c +++ /dev/null @@ -1,361 +0,0 @@ -#include "git2/ignore.h" -#include "ignore.h" -#include "path.h" -#include "config.h" - -#define GIT_IGNORE_INTERNAL "[internal]exclude" -#define GIT_IGNORE_FILE_INREPO "info/exclude" -#define GIT_IGNORE_FILE ".gitignore" - -#define GIT_IGNORE_DEFAULT_RULES ".\n..\n.git\n" - -static int parse_ignore_file( - git_repository *repo, void *parsedata, const char *buffer, git_attr_file *ignores) -{ - int error = 0; - git_attr_fnmatch *match = NULL; - const char *scan = NULL; - char *context = NULL; - bool ignore_case = false; - git_config *cfg = NULL; - int val; - - /* Prefer to have the caller pass in a git_ignores as the parsedata object. - * If they did not, then we can (much more slowly) find the value of - * ignore_case by using the repository object. */ - if (parsedata != NULL) { - ignore_case = ((git_ignores *)parsedata)->ignore_case; - } else { - if ((error = git_repository_config(&cfg, repo)) < 0) - return error; - - if (git_config_get_bool(&val, cfg, "core.ignorecase") == 0) - ignore_case = (val != 0); - - git_config_free(cfg); - } - - if (ignores->key && git__suffixcmp(ignores->key, "/" GIT_IGNORE_FILE) == 0) { - context = ignores->key + 2; - context[strlen(context) - strlen(GIT_IGNORE_FILE)] = '\0'; - } - - scan = buffer; - - while (!error && *scan) { - if (!match) { - match = git__calloc(1, sizeof(*match)); - GITERR_CHECK_ALLOC(match); - } - - match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE; - - if (!(error = git_attr_fnmatch__parse( - match, ignores->pool, context, &scan))) - { - match->flags |= GIT_ATTR_FNMATCH_IGNORE; - - if (ignore_case) - match->flags |= GIT_ATTR_FNMATCH_ICASE; - - scan = git__next_line(scan); - error = git_vector_insert(&ignores->rules, match); - } - - if (error != 0) { - git__free(match->pattern); - match->pattern = NULL; - - if (error == GIT_ENOTFOUND) - error = 0; - } else { - match = NULL; /* vector now "owns" the match */ - } - } - - git__free(match); - /* restore file path used for context */ - if (context) - context[strlen(context)] = '.'; /* first char of GIT_IGNORE_FILE */ - - return error; -} - -#define push_ignore_file(R,IGN,S,B,F) \ - git_attr_cache__push_file((R),(B),(F),GIT_ATTR_FILE_FROM_FILE,parse_ignore_file,(IGN),(S)) - -static int push_one_ignore(void *ref, git_buf *path) -{ - git_ignores *ign = (git_ignores *)ref; - return push_ignore_file(ign->repo, ign, &ign->ign_path, path->ptr, GIT_IGNORE_FILE); -} - -static int get_internal_ignores(git_attr_file **ign, git_repository *repo) -{ - int error; - - if (!(error = git_attr_cache__init(repo))) - error = git_attr_cache__internal_file(repo, GIT_IGNORE_INTERNAL, ign); - - if (!error && !(*ign)->rules.length) - error = parse_ignore_file(repo, NULL, GIT_IGNORE_DEFAULT_RULES, *ign); - - return error; -} - -int git_ignore__for_path( - git_repository *repo, - const char *path, - git_ignores *ignores) -{ - int error = 0; - const char *workdir = git_repository_workdir(repo); - git_config *cfg = NULL; - int val; - - assert(ignores); - - ignores->repo = repo; - git_buf_init(&ignores->dir, 0); - ignores->ign_internal = NULL; - - /* Set the ignore_case flag appropriately */ - if ((error = git_repository_config(&cfg, repo)) < 0) - goto cleanup; - - if (git_config_get_bool(&val, cfg, "core.ignorecase") == 0) - ignores->ignore_case = (val != 0); - else - ignores->ignore_case = 0; - - git_config_free(cfg); - - if ((error = git_vector_init(&ignores->ign_path, 8, NULL)) < 0 || - (error = git_vector_init(&ignores->ign_global, 2, NULL)) < 0 || - (error = git_attr_cache__init(repo)) < 0) - goto cleanup; - - /* given a unrooted path in a non-bare repo, resolve it */ - if (workdir && git_path_root(path) < 0) - error = git_path_find_dir(&ignores->dir, path, workdir); - else - error = git_buf_sets(&ignores->dir, path); - if (error < 0) - goto cleanup; - - /* set up internals */ - error = get_internal_ignores(&ignores->ign_internal, repo); - if (error < 0) - goto cleanup; - - /* load .gitignore up the path */ - if (workdir != NULL) { - error = git_path_walk_up( - &ignores->dir, workdir, push_one_ignore, ignores); - if (error < 0) - goto cleanup; - } - - /* load .git/info/exclude */ - error = push_ignore_file(repo, ignores, &ignores->ign_global, - git_repository_path(repo), GIT_IGNORE_FILE_INREPO); - if (error < 0) - goto cleanup; - - /* load core.excludesfile */ - if (git_repository_attr_cache(repo)->cfg_excl_file != NULL) - error = push_ignore_file(repo, ignores, &ignores->ign_global, NULL, - git_repository_attr_cache(repo)->cfg_excl_file); - -cleanup: - if (error < 0) - git_ignore__free(ignores); - - return error; -} - -int git_ignore__push_dir(git_ignores *ign, const char *dir) -{ - if (git_buf_joinpath(&ign->dir, ign->dir.ptr, dir) < 0) - return -1; - else - return push_ignore_file( - ign->repo, ign, &ign->ign_path, ign->dir.ptr, GIT_IGNORE_FILE); -} - -int git_ignore__pop_dir(git_ignores *ign) -{ - if (ign->ign_path.length > 0) { - git_attr_file *file = git_vector_last(&ign->ign_path); - if (git__suffixcmp(ign->dir.ptr, file->key + 2) == 0) - git_vector_pop(&ign->ign_path); - git_buf_rtruncate_at_char(&ign->dir, '/'); - } - return 0; -} - -void git_ignore__free(git_ignores *ignores) -{ - /* don't need to free ignores->ign_internal since it is in cache */ - git_vector_free(&ignores->ign_path); - git_vector_free(&ignores->ign_global); - git_buf_free(&ignores->dir); -} - -static bool ignore_lookup_in_rules( - git_vector *rules, git_attr_path *path, int *ignored) -{ - size_t j; - git_attr_fnmatch *match; - - git_vector_rforeach(rules, j, match) { - if (git_attr_fnmatch__match(match, path)) { - *ignored = ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0); - return true; - } - } - - return false; -} - -int git_ignore__lookup( - git_ignores *ignores, const char *pathname, int *ignored) -{ - unsigned int i; - git_attr_file *file; - git_attr_path path; - - if (git_attr_path__init( - &path, pathname, git_repository_workdir(ignores->repo)) < 0) - return -1; - - /* first process builtins - success means path was found */ - if (ignore_lookup_in_rules( - &ignores->ign_internal->rules, &path, ignored)) - goto cleanup; - - /* next process files in the path */ - git_vector_foreach(&ignores->ign_path, i, file) { - if (ignore_lookup_in_rules(&file->rules, &path, ignored)) - goto cleanup; - } - - /* last process global ignores */ - git_vector_foreach(&ignores->ign_global, i, file) { - if (ignore_lookup_in_rules(&file->rules, &path, ignored)) - goto cleanup; - } - - *ignored = 0; - -cleanup: - git_attr_path__free(&path); - return 0; -} - -int git_ignore_add_rule( - git_repository *repo, - const char *rules) -{ - int error; - git_attr_file *ign_internal; - - if (!(error = get_internal_ignores(&ign_internal, repo))) - error = parse_ignore_file(repo, NULL, rules, ign_internal); - - return error; -} - -int git_ignore_clear_internal_rules( - git_repository *repo) -{ - int error; - git_attr_file *ign_internal; - - if (!(error = get_internal_ignores(&ign_internal, repo))) { - git_attr_file__clear_rules(ign_internal); - - return parse_ignore_file( - repo, NULL, GIT_IGNORE_DEFAULT_RULES, ign_internal); - } - - return error; -} - -int git_ignore_path_is_ignored( - int *ignored, - git_repository *repo, - const char *pathname) -{ - int error; - const char *workdir; - git_attr_path path; - char *tail, *end; - bool full_is_dir; - git_ignores ignores; - unsigned int i; - git_attr_file *file; - - assert(ignored && pathname); - - workdir = repo ? git_repository_workdir(repo) : NULL; - - if ((error = git_attr_path__init(&path, pathname, workdir)) < 0) - return error; - - tail = path.path; - end = &path.full.ptr[path.full.size]; - full_is_dir = path.is_dir; - - while (1) { - /* advance to next component of path */ - path.basename = tail; - - while (tail < end && *tail != '/') tail++; - *tail = '\0'; - - path.full.size = (tail - path.full.ptr); - path.is_dir = (tail == end) ? full_is_dir : true; - - /* update ignores for new path fragment */ - if (path.basename == path.path) - error = git_ignore__for_path(repo, path.path, &ignores); - else - error = git_ignore__push_dir(&ignores, path.basename); - if (error < 0) - break; - - /* first process builtins - success means path was found */ - if (ignore_lookup_in_rules( - &ignores.ign_internal->rules, &path, ignored)) - goto cleanup; - - /* next process files in the path */ - git_vector_foreach(&ignores.ign_path, i, file) { - if (ignore_lookup_in_rules(&file->rules, &path, ignored)) - goto cleanup; - } - - /* last process global ignores */ - git_vector_foreach(&ignores.ign_global, i, file) { - if (ignore_lookup_in_rules(&file->rules, &path, ignored)) - goto cleanup; - } - - /* if we found no rules before reaching the end, we're done */ - if (tail == end) - break; - - /* reinstate divider in path */ - *tail = '/'; - while (*tail == '/') tail++; - } - - *ignored = 0; - -cleanup: - git_attr_path__free(&path); - git_ignore__free(&ignores); - return error; -} - diff --git a/src/ignore.h b/src/ignore.h deleted file mode 100644 index 5a15afcca90..00000000000 --- a/src/ignore.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_ignore_h__ -#define INCLUDE_ignore_h__ - -#include "repository.h" -#include "vector.h" - -/* The git_ignores structure maintains three sets of ignores: - * - internal ignores - * - per directory ignores - * - global ignores (at lower priority than the others) - * As you traverse from one directory to another, you can push and pop - * directories onto git_ignores list efficiently. - */ -typedef struct { - git_repository *repo; - git_buf dir; - git_attr_file *ign_internal; - git_vector ign_path; - git_vector ign_global; - unsigned int ignore_case:1; -} git_ignores; - -extern int git_ignore__for_path(git_repository *repo, const char *path, git_ignores *ign); - -extern int git_ignore__push_dir(git_ignores *ign, const char *dir); - -extern int git_ignore__pop_dir(git_ignores *ign); - -extern void git_ignore__free(git_ignores *ign); - -extern int git_ignore__lookup(git_ignores *ign, const char *path, int *ignored); - -#endif diff --git a/src/index.c b/src/index.c deleted file mode 100644 index 1e00dd3fab4..00000000000 --- a/src/index.c +++ /dev/null @@ -1,1729 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include - -#include "common.h" -#include "repository.h" -#include "index.h" -#include "tree.h" -#include "tree-cache.h" -#include "hash.h" -#include "iterator.h" -#include "pathspec.h" -#include "git2/odb.h" -#include "git2/oid.h" -#include "git2/blob.h" -#include "git2/config.h" - -#define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7) -#define short_entry_size(len) entry_size(struct entry_short, len) -#define long_entry_size(len) entry_size(struct entry_long, len) - -#define minimal_entry_size (offsetof(struct entry_short, path)) - -static const size_t INDEX_FOOTER_SIZE = GIT_OID_RAWSZ; -static const size_t INDEX_HEADER_SIZE = 12; - -static const unsigned int INDEX_VERSION_NUMBER = 2; -static const unsigned int INDEX_VERSION_NUMBER_EXT = 3; - -static const unsigned int INDEX_HEADER_SIG = 0x44495243; -static const char INDEX_EXT_TREECACHE_SIG[] = {'T', 'R', 'E', 'E'}; -static const char INDEX_EXT_UNMERGED_SIG[] = {'R', 'E', 'U', 'C'}; - -#define INDEX_OWNER(idx) ((git_repository *)(GIT_REFCOUNT_OWNER(idx))) - -struct index_header { - uint32_t signature; - uint32_t version; - uint32_t entry_count; -}; - -struct index_extension { - char signature[4]; - uint32_t extension_size; -}; - -struct entry_time { - uint32_t seconds; - uint32_t nanoseconds; -}; - -struct entry_short { - struct entry_time ctime; - struct entry_time mtime; - uint32_t dev; - uint32_t ino; - uint32_t mode; - uint32_t uid; - uint32_t gid; - uint32_t file_size; - git_oid oid; - uint16_t flags; - char path[1]; /* arbitrary length */ -}; - -struct entry_long { - struct entry_time ctime; - struct entry_time mtime; - uint32_t dev; - uint32_t ino; - uint32_t mode; - uint32_t uid; - uint32_t gid; - uint32_t file_size; - git_oid oid; - uint16_t flags; - uint16_t flags_extended; - char path[1]; /* arbitrary length */ -}; - -struct entry_srch_key { - const char *path; - int stage; -}; - -/* local declarations */ -static size_t read_extension(git_index *index, const char *buffer, size_t buffer_size); -static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffer_size); -static int read_header(struct index_header *dest, const void *buffer); - -static int parse_index(git_index *index, const char *buffer, size_t buffer_size); -static bool is_index_extended(git_index *index); -static int write_index(git_index *index, git_filebuf *file); - -static int index_find(size_t *at_pos, git_index *index, const char *path, int stage); - -static void index_entry_free(git_index_entry *entry); -static void index_entry_reuc_free(git_index_reuc_entry *reuc); - -GIT_INLINE(int) index_entry_stage(const git_index_entry *entry) -{ - return (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT; -} - -static int index_srch(const void *key, const void *array_member) -{ - const struct entry_srch_key *srch_key = key; - const git_index_entry *entry = array_member; - int ret; - - ret = strcmp(srch_key->path, entry->path); - - if (ret == 0) - ret = srch_key->stage - index_entry_stage(entry); - - return ret; -} - -static int index_isrch(const void *key, const void *array_member) -{ - const struct entry_srch_key *srch_key = key; - const git_index_entry *entry = array_member; - int ret; - - ret = strcasecmp(srch_key->path, entry->path); - - if (ret == 0) - ret = srch_key->stage - index_entry_stage(entry); - - return ret; -} - -static int index_cmp_path(const void *a, const void *b) -{ - return strcmp((const char *)a, (const char *)b); -} - -static int index_icmp_path(const void *a, const void *b) -{ - return strcasecmp((const char *)a, (const char *)b); -} - -static int index_srch_path(const void *path, const void *array_member) -{ - const git_index_entry *entry = array_member; - - return strcmp((const char *)path, entry->path); -} - -static int index_isrch_path(const void *path, const void *array_member) -{ - const git_index_entry *entry = array_member; - - return strcasecmp((const char *)path, entry->path); -} - -static int index_cmp(const void *a, const void *b) -{ - int diff; - const git_index_entry *entry_a = a; - const git_index_entry *entry_b = b; - - diff = strcmp(entry_a->path, entry_b->path); - - if (diff == 0) - diff = (index_entry_stage(entry_a) - index_entry_stage(entry_b)); - - return diff; -} - -static int index_icmp(const void *a, const void *b) -{ - int diff; - const git_index_entry *entry_a = a; - const git_index_entry *entry_b = b; - - diff = strcasecmp(entry_a->path, entry_b->path); - - if (diff == 0) - diff = (index_entry_stage(entry_a) - index_entry_stage(entry_b)); - - return diff; -} - -static int reuc_srch(const void *key, const void *array_member) -{ - const git_index_reuc_entry *reuc = array_member; - - return strcmp(key, reuc->path); -} - -static int reuc_isrch(const void *key, const void *array_member) -{ - const git_index_reuc_entry *reuc = array_member; - - return strcasecmp(key, reuc->path); -} - -static int reuc_cmp(const void *a, const void *b) -{ - const git_index_reuc_entry *info_a = a; - const git_index_reuc_entry *info_b = b; - - return strcmp(info_a->path, info_b->path); -} - -static int reuc_icmp(const void *a, const void *b) -{ - const git_index_reuc_entry *info_a = a; - const git_index_reuc_entry *info_b = b; - - return strcasecmp(info_a->path, info_b->path); -} - -static unsigned int index_create_mode(unsigned int mode) -{ - if (S_ISLNK(mode)) - return S_IFLNK; - - if (S_ISDIR(mode) || (mode & S_IFMT) == (S_IFLNK | S_IFDIR)) - return (S_IFLNK | S_IFDIR); - - return S_IFREG | ((mode & 0100) ? 0755 : 0644); -} - -static unsigned int index_merge_mode( - git_index *index, git_index_entry *existing, unsigned int mode) -{ - if (index->no_symlinks && S_ISREG(mode) && - existing && S_ISLNK(existing->mode)) - return existing->mode; - - if (index->distrust_filemode && S_ISREG(mode)) - return (existing && S_ISREG(existing->mode)) ? - existing->mode : index_create_mode(0666); - - return index_create_mode(mode); -} - -static void index_set_ignore_case(git_index *index, bool ignore_case) -{ - index->entries._cmp = ignore_case ? index_icmp : index_cmp; - index->entries_cmp_path = ignore_case ? index_icmp_path : index_cmp_path; - index->entries_search = ignore_case ? index_isrch : index_srch; - index->entries_search_path = ignore_case ? index_isrch_path : index_srch_path; - index->entries.sorted = 0; - git_vector_sort(&index->entries); - - index->reuc._cmp = ignore_case ? reuc_icmp : reuc_cmp; - index->reuc_search = ignore_case ? reuc_isrch : reuc_srch; - index->reuc.sorted = 0; - git_vector_sort(&index->reuc); -} - -int git_index_open(git_index **index_out, const char *index_path) -{ - git_index *index; - - assert(index_out); - - index = git__calloc(1, sizeof(git_index)); - GITERR_CHECK_ALLOC(index); - - if (index_path != NULL) { - index->index_file_path = git__strdup(index_path); - GITERR_CHECK_ALLOC(index->index_file_path); - - /* Check if index file is stored on disk already */ - if (git_path_exists(index->index_file_path) == true) - index->on_disk = 1; - } - - if (git_vector_init(&index->entries, 32, index_cmp) < 0) - return -1; - - index->entries_cmp_path = index_cmp_path; - index->entries_search = index_srch; - index->entries_search_path = index_srch_path; - index->reuc_search = reuc_srch; - - *index_out = index; - GIT_REFCOUNT_INC(index); - - return (index_path != NULL) ? git_index_read(index) : 0; -} - -int git_index_new(git_index **out) -{ - return git_index_open(out, NULL); -} - -static void index_free(git_index *index) -{ - git_index_entry *e; - git_index_reuc_entry *reuc; - size_t i; - - git_index_clear(index); - git_vector_foreach(&index->entries, i, e) { - index_entry_free(e); - } - git_vector_free(&index->entries); - git_vector_foreach(&index->reuc, i, reuc) { - index_entry_reuc_free(reuc); - } - git_vector_free(&index->reuc); - - git__free(index->index_file_path); - git__free(index); -} - -void git_index_free(git_index *index) -{ - if (index == NULL) - return; - - GIT_REFCOUNT_DEC(index, index_free); -} - -void git_index_clear(git_index *index) -{ - unsigned int i; - - assert(index); - - for (i = 0; i < index->entries.length; ++i) { - git_index_entry *e; - e = git_vector_get(&index->entries, i); - git__free(e->path); - git__free(e); - } - - for (i = 0; i < index->reuc.length; ++i) { - git_index_reuc_entry *e; - e = git_vector_get(&index->reuc, i); - git__free(e->path); - git__free(e); - } - - git_vector_clear(&index->entries); - git_vector_clear(&index->reuc); - git_futils_filestamp_set(&index->stamp, NULL); - - git_tree_cache_free(index->tree); - index->tree = NULL; -} - -static int create_index_error(int error, const char *msg) -{ - giterr_set(GITERR_INDEX, msg); - return error; -} - -int git_index_set_caps(git_index *index, unsigned int caps) -{ - int old_ignore_case; - - assert(index); - - old_ignore_case = index->ignore_case; - - if (caps == GIT_INDEXCAP_FROM_OWNER) { - git_config *cfg; - int val; - - if (INDEX_OWNER(index) == NULL || - git_repository_config__weakptr(&cfg, INDEX_OWNER(index)) < 0) - return create_index_error(-1, - "Cannot get repository config to set index caps"); - - if (git_config_get_bool(&val, cfg, "core.ignorecase") == 0) - index->ignore_case = (val != 0); - if (git_config_get_bool(&val, cfg, "core.filemode") == 0) - index->distrust_filemode = (val == 0); - if (git_config_get_bool(&val, cfg, "core.symlinks") == 0) - index->no_symlinks = (val == 0); - } - else { - index->ignore_case = ((caps & GIT_INDEXCAP_IGNORE_CASE) != 0); - index->distrust_filemode = ((caps & GIT_INDEXCAP_NO_FILEMODE) != 0); - index->no_symlinks = ((caps & GIT_INDEXCAP_NO_SYMLINKS) != 0); - } - - if (old_ignore_case != index->ignore_case) { - index_set_ignore_case(index, index->ignore_case); - } - - return 0; -} - -unsigned int git_index_caps(const git_index *index) -{ - return ((index->ignore_case ? GIT_INDEXCAP_IGNORE_CASE : 0) | - (index->distrust_filemode ? GIT_INDEXCAP_NO_FILEMODE : 0) | - (index->no_symlinks ? GIT_INDEXCAP_NO_SYMLINKS : 0)); -} - -int git_index_read(git_index *index) -{ - int error = 0, updated; - git_buf buffer = GIT_BUF_INIT; - git_futils_filestamp stamp = {0}; - - if (!index->index_file_path) - return create_index_error(-1, - "Failed to read index: The index is in-memory only"); - - if (!index->on_disk || git_path_exists(index->index_file_path) == false) { - git_index_clear(index); - index->on_disk = 0; - return 0; - } - - updated = git_futils_filestamp_check(&stamp, index->index_file_path); - if (updated <= 0) - return updated; - - error = git_futils_readbuffer(&buffer, index->index_file_path); - if (error < 0) - return error; - - git_index_clear(index); - error = parse_index(index, buffer.ptr, buffer.size); - - if (!error) - git_futils_filestamp_set(&index->stamp, &stamp); - - git_buf_free(&buffer); - return error; -} - -int git_index_write(git_index *index) -{ - git_filebuf file = GIT_FILEBUF_INIT; - int error; - - if (!index->index_file_path) - return create_index_error(-1, - "Failed to read index: The index is in-memory only"); - - git_vector_sort(&index->entries); - git_vector_sort(&index->reuc); - - if ((error = git_filebuf_open( - &file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS)) < 0) - return error; - - if ((error = write_index(index, &file)) < 0) { - git_filebuf_cleanup(&file); - return error; - } - - if ((error = git_filebuf_commit(&file, GIT_INDEX_FILE_MODE)) < 0) - return error; - - error = git_futils_filestamp_check(&index->stamp, index->index_file_path); - if (error < 0) - return error; - - index->on_disk = 1; - return 0; -} - -int git_index_write_tree(git_oid *oid, git_index *index) -{ - git_repository *repo; - - assert(oid && index); - - repo = INDEX_OWNER(index); - - if (repo == NULL) - return create_index_error(-1, "Failed to write tree. " - "The index file is not backed up by an existing repository"); - - return git_tree__write_index(oid, index, repo); -} - -int git_index_write_tree_to(git_oid *oid, git_index *index, git_repository *repo) -{ - assert(oid && index && repo); - return git_tree__write_index(oid, index, repo); -} - -size_t git_index_entrycount(const git_index *index) -{ - assert(index); - return index->entries.length; -} - -const git_index_entry *git_index_get_byindex( - git_index *index, size_t n) -{ - assert(index); - git_vector_sort(&index->entries); - return git_vector_get(&index->entries, n); -} - -const git_index_entry *git_index_get_bypath( - git_index *index, const char *path, int stage) -{ - size_t pos; - - assert(index); - - git_vector_sort(&index->entries); - - if (index_find(&pos, index, path, stage) < 0) - return NULL; - - return git_index_get_byindex(index, pos); -} - -void git_index_entry__init_from_stat(git_index_entry *entry, struct stat *st) -{ - entry->ctime.seconds = (git_time_t)st->st_ctime; - entry->mtime.seconds = (git_time_t)st->st_mtime; - /* entry->mtime.nanoseconds = st->st_mtimensec; */ - /* entry->ctime.nanoseconds = st->st_ctimensec; */ - entry->dev = st->st_rdev; - entry->ino = st->st_ino; - entry->mode = index_create_mode(st->st_mode); - entry->uid = st->st_uid; - entry->gid = st->st_gid; - entry->file_size = st->st_size; -} - -int git_index_entry__cmp(const void *a, const void *b) -{ - const git_index_entry *entry_a = a; - const git_index_entry *entry_b = b; - - return strcmp(entry_a->path, entry_b->path); -} - -int git_index_entry__cmp_icase(const void *a, const void *b) -{ - const git_index_entry *entry_a = a; - const git_index_entry *entry_b = b; - - return strcasecmp(entry_a->path, entry_b->path); -} - -static int index_entry_init(git_index_entry **entry_out, git_index *index, const char *rel_path) -{ - git_index_entry *entry = NULL; - struct stat st; - git_oid oid; - const char *workdir; - git_buf full_path = GIT_BUF_INIT; - int error; - - if (INDEX_OWNER(index) == NULL) - return create_index_error(-1, - "Could not initialize index entry. " - "Index is not backed up by an existing repository."); - - workdir = git_repository_workdir(INDEX_OWNER(index)); - - if (!workdir) - return create_index_error(GIT_EBAREREPO, - "Could not initialize index entry. Repository is bare"); - - if ((error = git_buf_joinpath(&full_path, workdir, rel_path)) < 0) - return error; - - if ((error = git_path_lstat(full_path.ptr, &st)) < 0) { - git_buf_free(&full_path); - return error; - } - - git_buf_free(&full_path); /* done with full path */ - - /* There is no need to validate the rel_path here, since it will be - * immediately validated by the call to git_blob_create_fromfile. - */ - - /* write the blob to disk and get the oid */ - if ((error = git_blob_create_fromworkdir(&oid, INDEX_OWNER(index), rel_path)) < 0) - return error; - - entry = git__calloc(1, sizeof(git_index_entry)); - GITERR_CHECK_ALLOC(entry); - - git_index_entry__init_from_stat(entry, &st); - - entry->oid = oid; - entry->path = git__strdup(rel_path); - GITERR_CHECK_ALLOC(entry->path); - - *entry_out = entry; - return 0; -} - -static int index_entry_reuc_init(git_index_reuc_entry **reuc_out, - const char *path, - int ancestor_mode, git_oid *ancestor_oid, - int our_mode, git_oid *our_oid, int their_mode, git_oid *their_oid) -{ - git_index_reuc_entry *reuc = NULL; - - assert(reuc_out && path); - - *reuc_out = NULL; - - reuc = git__calloc(1, sizeof(git_index_reuc_entry)); - GITERR_CHECK_ALLOC(reuc); - - reuc->path = git__strdup(path); - if (reuc->path == NULL) - return -1; - - if ((reuc->mode[0] = ancestor_mode) > 0) - git_oid_cpy(&reuc->oid[0], ancestor_oid); - - if ((reuc->mode[1] = our_mode) > 0) - git_oid_cpy(&reuc->oid[1], our_oid); - - if ((reuc->mode[2] = their_mode) > 0) - git_oid_cpy(&reuc->oid[2], their_oid); - - *reuc_out = reuc; - return 0; -} - -static void index_entry_reuc_free(git_index_reuc_entry *reuc) -{ - if (!reuc) - return; - - git__free(reuc->path); - git__free(reuc); -} - -static git_index_entry *index_entry_dup(const git_index_entry *source_entry) -{ - git_index_entry *entry; - - entry = git__malloc(sizeof(git_index_entry)); - if (!entry) - return NULL; - - memcpy(entry, source_entry, sizeof(git_index_entry)); - - /* duplicate the path string so we own it */ - entry->path = git__strdup(entry->path); - if (!entry->path) - return NULL; - - return entry; -} - -static void index_entry_free(git_index_entry *entry) -{ - if (!entry) - return; - git__free(entry->path); - git__free(entry); -} - -static int index_insert(git_index *index, git_index_entry *entry, int replace) -{ - size_t path_length, position; - git_index_entry **existing = NULL; - - assert(index && entry && entry->path != NULL); - - /* make sure that the path length flag is correct */ - path_length = strlen(entry->path); - - entry->flags &= ~GIT_IDXENTRY_NAMEMASK; - - if (path_length < GIT_IDXENTRY_NAMEMASK) - entry->flags |= path_length & GIT_IDXENTRY_NAMEMASK; - else - entry->flags |= GIT_IDXENTRY_NAMEMASK; - - /* look if an entry with this path already exists */ - if (!index_find(&position, index, entry->path, index_entry_stage(entry))) { - existing = (git_index_entry **)&index->entries.contents[position]; - - /* update filemode to existing values if stat is not trusted */ - entry->mode = index_merge_mode(index, *existing, entry->mode); - } - - /* if replacing is not requested or no existing entry exists, just - * insert entry at the end; the index is no longer sorted - */ - if (!replace || !existing) - return git_vector_insert(&index->entries, entry); - - /* exists, replace it */ - git__free((*existing)->path); - git__free(*existing); - *existing = entry; - - return 0; -} - -static int index_conflict_to_reuc(git_index *index, const char *path) -{ - git_index_entry *conflict_entries[3]; - int ancestor_mode, our_mode, their_mode; - git_oid *ancestor_oid, *our_oid, *their_oid; - int ret; - - if ((ret = git_index_conflict_get(&conflict_entries[0], - &conflict_entries[1], &conflict_entries[2], index, path)) < 0) - return ret; - - ancestor_mode = conflict_entries[0] == NULL ? 0 : conflict_entries[0]->mode; - our_mode = conflict_entries[1] == NULL ? 0 : conflict_entries[1]->mode; - their_mode = conflict_entries[2] == NULL ? 0 : conflict_entries[2]->mode; - - ancestor_oid = conflict_entries[0] == NULL ? NULL : &conflict_entries[0]->oid; - our_oid = conflict_entries[1] == NULL ? NULL : &conflict_entries[1]->oid; - their_oid = conflict_entries[2] == NULL ? NULL : &conflict_entries[2]->oid; - - if ((ret = git_index_reuc_add(index, path, ancestor_mode, ancestor_oid, - our_mode, our_oid, their_mode, their_oid)) >= 0) - ret = git_index_conflict_remove(index, path); - - return ret; -} - -int git_index_add_bypath(git_index *index, const char *path) -{ - git_index_entry *entry = NULL; - int ret; - - assert(index && path); - - if ((ret = index_entry_init(&entry, index, path)) < 0 || - (ret = index_insert(index, entry, 1)) < 0) - goto on_error; - - /* Adding implies conflict was resolved, move conflict entries to REUC */ - if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND) - goto on_error; - - git_tree_cache_invalidate_path(index->tree, entry->path); - return 0; - -on_error: - index_entry_free(entry); - return ret; -} - -int git_index_remove_bypath(git_index *index, const char *path) -{ - int ret; - - assert(index && path); - - if (((ret = git_index_remove(index, path, 0)) < 0 && - ret != GIT_ENOTFOUND) || - ((ret = index_conflict_to_reuc(index, path)) < 0 && - ret != GIT_ENOTFOUND)) - return ret; - - return 0; -} - -int git_index_add(git_index *index, const git_index_entry *source_entry) -{ - git_index_entry *entry = NULL; - int ret; - - entry = index_entry_dup(source_entry); - if (entry == NULL) - return -1; - - if ((ret = index_insert(index, entry, 1)) < 0) { - index_entry_free(entry); - return ret; - } - - git_tree_cache_invalidate_path(index->tree, entry->path); - return 0; -} - -int git_index_remove(git_index *index, const char *path, int stage) -{ - size_t position; - int error; - git_index_entry *entry; - - git_vector_sort(&index->entries); - - if (index_find(&position, index, path, stage) < 0) - return GIT_ENOTFOUND; - - entry = git_vector_get(&index->entries, position); - if (entry != NULL) - git_tree_cache_invalidate_path(index->tree, entry->path); - - error = git_vector_remove(&index->entries, (unsigned int)position); - - if (!error) - index_entry_free(entry); - - return error; -} - -int git_index_remove_directory(git_index *index, const char *dir, int stage) -{ - git_buf pfx = GIT_BUF_INIT; - int error = 0; - size_t pos; - git_index_entry *entry; - - if (git_buf_sets(&pfx, dir) < 0 || git_path_to_dir(&pfx) < 0) - return -1; - - git_vector_sort(&index->entries); - - pos = git_index__prefix_position(index, pfx.ptr); - - while (1) { - entry = git_vector_get(&index->entries, pos); - if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0) - break; - - if (index_entry_stage(entry) != stage) { - ++pos; - continue; - } - - git_tree_cache_invalidate_path(index->tree, entry->path); - - if ((error = git_vector_remove(&index->entries, pos)) < 0) - break; - index_entry_free(entry); - - /* removed entry at 'pos' so we don't need to increment it */ - } - - git_buf_free(&pfx); - - return error; -} - -static int index_find(size_t *at_pos, git_index *index, const char *path, int stage) -{ - struct entry_srch_key srch_key; - - assert(path); - - srch_key.path = path; - srch_key.stage = stage; - - return git_vector_bsearch2(at_pos, &index->entries, index->entries_search, &srch_key); -} - -int git_index_find(size_t *at_pos, git_index *index, const char *path) -{ - size_t pos; - - assert(index && path); - - if (git_vector_bsearch2(&pos, &index->entries, index->entries_search_path, path) < 0) { - giterr_set(GITERR_INDEX, "Index does not contain %s", path); - return GIT_ENOTFOUND; - } - - /* Since our binary search only looked at path, we may be in the - * middle of a list of stages. - */ - while (pos > 0) { - const git_index_entry *prev = git_vector_get(&index->entries, pos-1); - - if (index->entries_cmp_path(prev->path, path) != 0) - break; - - --pos; - } - - if (at_pos) - *at_pos = pos; - - return 0; -} - -size_t git_index__prefix_position(git_index *index, const char *path) -{ - struct entry_srch_key srch_key; - size_t pos; - - srch_key.path = path; - srch_key.stage = 0; - - git_vector_sort(&index->entries); - git_vector_bsearch2( - &pos, &index->entries, index->entries_search, &srch_key); - - return pos; -} - -int git_index_conflict_add(git_index *index, - const git_index_entry *ancestor_entry, - const git_index_entry *our_entry, - const git_index_entry *their_entry) -{ - git_index_entry *entries[3] = { 0 }; - unsigned short i; - int ret = 0; - - assert (index); - - if ((ancestor_entry != NULL && (entries[0] = index_entry_dup(ancestor_entry)) == NULL) || - (our_entry != NULL && (entries[1] = index_entry_dup(our_entry)) == NULL) || - (their_entry != NULL && (entries[2] = index_entry_dup(their_entry)) == NULL)) - return -1; - - for (i = 0; i < 3; i++) { - if (entries[i] == NULL) - continue; - - /* Make sure stage is correct */ - entries[i]->flags = (entries[i]->flags & ~GIT_IDXENTRY_STAGEMASK) | - ((i+1) << GIT_IDXENTRY_STAGESHIFT); - - if ((ret = index_insert(index, entries[i], 1)) < 0) - goto on_error; - } - - return 0; - -on_error: - for (i = 0; i < 3; i++) { - if (entries[i] != NULL) - index_entry_free(entries[i]); - } - - return ret; -} - -int git_index_conflict_get(git_index_entry **ancestor_out, - git_index_entry **our_out, - git_index_entry **their_out, - git_index *index, const char *path) -{ - size_t pos, posmax; - int stage; - git_index_entry *conflict_entry; - int error = GIT_ENOTFOUND; - - assert(ancestor_out && our_out && their_out && index && path); - - *ancestor_out = NULL; - *our_out = NULL; - *their_out = NULL; - - if (git_index_find(&pos, index, path) < 0) - return GIT_ENOTFOUND; - - for (posmax = git_index_entrycount(index); pos < posmax; ++pos) { - - conflict_entry = git_vector_get(&index->entries, pos); - - if (index->entries_cmp_path(conflict_entry->path, path) != 0) - break; - - stage = index_entry_stage(conflict_entry); - - switch (stage) { - case 3: - *their_out = conflict_entry; - error = 0; - break; - case 2: - *our_out = conflict_entry; - error = 0; - break; - case 1: - *ancestor_out = conflict_entry; - error = 0; - break; - default: - break; - }; - } - - return error; -} - -int git_index_conflict_remove(git_index *index, const char *path) -{ - size_t pos, posmax; - git_index_entry *conflict_entry; - int error = 0; - - assert(index && path); - - if (git_index_find(&pos, index, path) < 0) - return GIT_ENOTFOUND; - - posmax = git_index_entrycount(index); - - while (pos < posmax) { - conflict_entry = git_vector_get(&index->entries, pos); - - if (index->entries_cmp_path(conflict_entry->path, path) != 0) - break; - - if (index_entry_stage(conflict_entry) == 0) { - pos++; - continue; - } - - if ((error = git_vector_remove(&index->entries, pos)) < 0) - return error; - - index_entry_free(conflict_entry); - posmax--; - } - - return 0; -} - -static int index_conflicts_match(const git_vector *v, size_t idx) -{ - git_index_entry *entry = git_vector_get(v, idx); - - if (index_entry_stage(entry) > 0) { - index_entry_free(entry); - return 1; - } - - return 0; -} - -void git_index_conflict_cleanup(git_index *index) -{ - assert(index); - git_vector_remove_matching(&index->entries, index_conflicts_match); -} - -int git_index_has_conflicts(const git_index *index) -{ - size_t i; - git_index_entry *entry; - - assert(index); - - git_vector_foreach(&index->entries, i, entry) { - if (index_entry_stage(entry) > 0) - return 1; - } - - return 0; -} - -unsigned int git_index_reuc_entrycount(git_index *index) -{ - assert(index); - return (unsigned int)index->reuc.length; -} - -static int index_reuc_insert(git_index *index, git_index_reuc_entry *reuc, int replace) -{ - git_index_reuc_entry **existing = NULL; - size_t position; - - assert(index && reuc && reuc->path != NULL); - - if (!git_index_reuc_find(&position, index, reuc->path)) - existing = (git_index_reuc_entry **)&index->reuc.contents[position]; - - if (!replace || !existing) - return git_vector_insert(&index->reuc, reuc); - - /* exists, replace it */ - git__free((*existing)->path); - git__free(*existing); - *existing = reuc; - - return 0; -} - -int git_index_reuc_add(git_index *index, const char *path, - int ancestor_mode, git_oid *ancestor_oid, - int our_mode, git_oid *our_oid, - int their_mode, git_oid *their_oid) -{ - git_index_reuc_entry *reuc = NULL; - int error = 0; - - assert(index && path); - - if ((error = index_entry_reuc_init(&reuc, path, ancestor_mode, ancestor_oid, our_mode, our_oid, their_mode, their_oid)) < 0 || - (error = index_reuc_insert(index, reuc, 1)) < 0) - { - index_entry_reuc_free(reuc); - return error; - } - - return error; -} - -int git_index_reuc_find(size_t *at_pos, git_index *index, const char *path) -{ - return git_vector_bsearch2(at_pos, &index->reuc, index->reuc_search, path); -} - -const git_index_reuc_entry *git_index_reuc_get_bypath( - git_index *index, const char *path) -{ - size_t pos; - assert(index && path); - - if (!index->reuc.length) - return NULL; - - git_vector_sort(&index->reuc); - - if (git_index_reuc_find(&pos, index, path) < 0) - return NULL; - - return git_vector_get(&index->reuc, pos); -} - -const git_index_reuc_entry *git_index_reuc_get_byindex( - git_index *index, size_t n) -{ - assert(index); - - git_vector_sort(&index->reuc); - return git_vector_get(&index->reuc, n); -} - -int git_index_reuc_remove(git_index *index, size_t position) -{ - int error; - git_index_reuc_entry *reuc; - - git_vector_sort(&index->reuc); - - reuc = git_vector_get(&index->reuc, position); - error = git_vector_remove(&index->reuc, (unsigned int)position); - - if (!error) - index_entry_reuc_free(reuc); - - return error; -} - -static int index_error_invalid(const char *message) -{ - giterr_set(GITERR_INDEX, "Invalid data in index - %s", message); - return -1; -} - -static int read_reuc(git_index *index, const char *buffer, size_t size) -{ - const char *endptr; - size_t len; - int i; - - /* This gets called multiple times, the vector might already be initialized */ - if (index->reuc._alloc_size == 0 && git_vector_init(&index->reuc, 16, reuc_cmp) < 0) - return -1; - - while (size) { - git_index_reuc_entry *lost; - - len = strlen(buffer) + 1; - if (size <= len) - return index_error_invalid("reading reuc entries"); - - lost = git__malloc(sizeof(git_index_reuc_entry)); - GITERR_CHECK_ALLOC(lost); - - if (git_vector_insert(&index->reuc, lost) < 0) - return -1; - - /* read NUL-terminated pathname for entry */ - lost->path = git__strdup(buffer); - GITERR_CHECK_ALLOC(lost->path); - - size -= len; - buffer += len; - - /* read 3 ASCII octal numbers for stage entries */ - for (i = 0; i < 3; i++) { - int tmp; - - if (git__strtol32(&tmp, buffer, &endptr, 8) < 0 || - !endptr || endptr == buffer || *endptr || - (unsigned)tmp > UINT_MAX) - return index_error_invalid("reading reuc entry stage"); - - lost->mode[i] = tmp; - - len = (endptr + 1) - buffer; - if (size <= len) - return index_error_invalid("reading reuc entry stage"); - - size -= len; - buffer += len; - } - - /* read up to 3 OIDs for stage entries */ - for (i = 0; i < 3; i++) { - if (!lost->mode[i]) - continue; - if (size < 20) - return index_error_invalid("reading reuc entry oid"); - - git_oid_fromraw(&lost->oid[i], (const unsigned char *) buffer); - size -= 20; - buffer += 20; - } - } - - /* entries are guaranteed to be sorted on-disk */ - index->reuc.sorted = 1; - - return 0; -} - -static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffer_size) -{ - size_t path_length, entry_size; - uint16_t flags_raw; - const char *path_ptr; - const struct entry_short *source = buffer; - - if (INDEX_FOOTER_SIZE + minimal_entry_size > buffer_size) - return 0; - - memset(dest, 0x0, sizeof(git_index_entry)); - - dest->ctime.seconds = (git_time_t)ntohl(source->ctime.seconds); - dest->ctime.nanoseconds = ntohl(source->ctime.nanoseconds); - dest->mtime.seconds = (git_time_t)ntohl(source->mtime.seconds); - dest->mtime.nanoseconds = ntohl(source->mtime.nanoseconds); - dest->dev = ntohl(source->dev); - dest->ino = ntohl(source->ino); - dest->mode = ntohl(source->mode); - dest->uid = ntohl(source->uid); - dest->gid = ntohl(source->gid); - dest->file_size = ntohl(source->file_size); - git_oid_cpy(&dest->oid, &source->oid); - dest->flags = ntohs(source->flags); - - if (dest->flags & GIT_IDXENTRY_EXTENDED) { - const struct entry_long *source_l = (const struct entry_long *)source; - path_ptr = source_l->path; - - flags_raw = ntohs(source_l->flags_extended); - memcpy(&dest->flags_extended, &flags_raw, 2); - } else - path_ptr = source->path; - - path_length = dest->flags & GIT_IDXENTRY_NAMEMASK; - - /* if this is a very long string, we must find its - * real length without overflowing */ - if (path_length == 0xFFF) { - const char *path_end; - - path_end = memchr(path_ptr, '\0', buffer_size); - if (path_end == NULL) - return 0; - - path_length = path_end - path_ptr; - } - - if (dest->flags & GIT_IDXENTRY_EXTENDED) - entry_size = long_entry_size(path_length); - else - entry_size = short_entry_size(path_length); - - if (INDEX_FOOTER_SIZE + entry_size > buffer_size) - return 0; - - dest->path = git__strdup(path_ptr); - assert(dest->path); - - return entry_size; -} - -static int read_header(struct index_header *dest, const void *buffer) -{ - const struct index_header *source = buffer; - - dest->signature = ntohl(source->signature); - if (dest->signature != INDEX_HEADER_SIG) - return index_error_invalid("incorrect header signature"); - - dest->version = ntohl(source->version); - if (dest->version != INDEX_VERSION_NUMBER_EXT && - dest->version != INDEX_VERSION_NUMBER) - return index_error_invalid("incorrect header version"); - - dest->entry_count = ntohl(source->entry_count); - return 0; -} - -static size_t read_extension(git_index *index, const char *buffer, size_t buffer_size) -{ - const struct index_extension *source; - struct index_extension dest; - size_t total_size; - - source = (const struct index_extension *)(buffer); - - memcpy(dest.signature, source->signature, 4); - dest.extension_size = ntohl(source->extension_size); - - total_size = dest.extension_size + sizeof(struct index_extension); - - if (buffer_size - total_size < INDEX_FOOTER_SIZE) - return 0; - - /* optional extension */ - if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') { - /* tree cache */ - if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) { - if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size) < 0) - return 0; - } else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) { - if (read_reuc(index, buffer + 8, dest.extension_size) < 0) - return 0; - } - /* else, unsupported extension. We cannot parse this, but we can skip - * it by returning `total_size */ - } else { - /* we cannot handle non-ignorable extensions; - * in fact they aren't even defined in the standard */ - return 0; - } - - return total_size; -} - -static int parse_index(git_index *index, const char *buffer, size_t buffer_size) -{ - unsigned int i; - struct index_header header; - git_oid checksum_calculated, checksum_expected; - -#define seek_forward(_increase) { \ - if (_increase >= buffer_size) \ - return index_error_invalid("ran out of data while parsing"); \ - buffer += _increase; \ - buffer_size -= _increase;\ -} - - if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE) - return index_error_invalid("insufficient buffer space"); - - /* Precalculate the SHA1 of the files's contents -- we'll match it to - * the provided SHA1 in the footer */ - git_hash_buf(&checksum_calculated, buffer, buffer_size - INDEX_FOOTER_SIZE); - - /* Parse header */ - if (read_header(&header, buffer) < 0) - return -1; - - seek_forward(INDEX_HEADER_SIZE); - - git_vector_clear(&index->entries); - - /* Parse all the entries */ - for (i = 0; i < header.entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) { - size_t entry_size; - git_index_entry *entry; - - entry = git__malloc(sizeof(git_index_entry)); - GITERR_CHECK_ALLOC(entry); - - entry_size = read_entry(entry, buffer, buffer_size); - - /* 0 bytes read means an object corruption */ - if (entry_size == 0) - return index_error_invalid("invalid entry"); - - if (git_vector_insert(&index->entries, entry) < 0) - return -1; - - seek_forward(entry_size); - } - - if (i != header.entry_count) - return index_error_invalid("header entries changed while parsing"); - - /* There's still space for some extensions! */ - while (buffer_size > INDEX_FOOTER_SIZE) { - size_t extension_size; - - extension_size = read_extension(index, buffer, buffer_size); - - /* see if we have read any bytes from the extension */ - if (extension_size == 0) - return index_error_invalid("extension size is zero"); - - seek_forward(extension_size); - } - - if (buffer_size != INDEX_FOOTER_SIZE) - return index_error_invalid("buffer size does not match index footer size"); - - /* 160-bit SHA-1 over the content of the index file before this checksum. */ - git_oid_fromraw(&checksum_expected, (const unsigned char *)buffer); - - if (git_oid_cmp(&checksum_calculated, &checksum_expected) != 0) - return index_error_invalid("calculated checksum does not match expected"); - -#undef seek_forward - - /* Entries are stored case-sensitively on disk. */ - index->entries.sorted = !index->ignore_case; - git_vector_sort(&index->entries); - - return 0; -} - -static bool is_index_extended(git_index *index) -{ - size_t i, extended; - git_index_entry *entry; - - extended = 0; - - git_vector_foreach(&index->entries, i, entry) { - entry->flags &= ~GIT_IDXENTRY_EXTENDED; - if (entry->flags_extended & GIT_IDXENTRY_EXTENDED_FLAGS) { - extended++; - entry->flags |= GIT_IDXENTRY_EXTENDED; - } - } - - return (extended > 0); -} - -static int write_disk_entry(git_filebuf *file, git_index_entry *entry) -{ - void *mem = NULL; - struct entry_short *ondisk; - size_t path_len, disk_size; - char *path; - - path_len = strlen(entry->path); - - if (entry->flags & GIT_IDXENTRY_EXTENDED) - disk_size = long_entry_size(path_len); - else - disk_size = short_entry_size(path_len); - - if (git_filebuf_reserve(file, &mem, disk_size) < 0) - return -1; - - ondisk = (struct entry_short *)mem; - - memset(ondisk, 0x0, disk_size); - - /** - * Yes, we have to truncate. - * - * The on-disk format for Index entries clearly defines - * the time and size fields to be 4 bytes each -- so even if - * we store these values with 8 bytes on-memory, they must - * be truncated to 4 bytes before writing to disk. - * - * In 2038 I will be either too dead or too rich to care about this - */ - ondisk->ctime.seconds = htonl((uint32_t)entry->ctime.seconds); - ondisk->mtime.seconds = htonl((uint32_t)entry->mtime.seconds); - ondisk->ctime.nanoseconds = htonl(entry->ctime.nanoseconds); - ondisk->mtime.nanoseconds = htonl(entry->mtime.nanoseconds); - ondisk->dev = htonl(entry->dev); - ondisk->ino = htonl(entry->ino); - ondisk->mode = htonl(entry->mode); - ondisk->uid = htonl(entry->uid); - ondisk->gid = htonl(entry->gid); - ondisk->file_size = htonl((uint32_t)entry->file_size); - - git_oid_cpy(&ondisk->oid, &entry->oid); - - ondisk->flags = htons(entry->flags); - - if (entry->flags & GIT_IDXENTRY_EXTENDED) { - struct entry_long *ondisk_ext; - ondisk_ext = (struct entry_long *)ondisk; - ondisk_ext->flags_extended = htons(entry->flags_extended); - path = ondisk_ext->path; - } - else - path = ondisk->path; - - memcpy(path, entry->path, path_len); - - return 0; -} - -static int write_entries(git_index *index, git_filebuf *file) -{ - int error = 0; - size_t i; - git_vector case_sorted; - git_index_entry *entry; - git_vector *out = &index->entries; - - /* If index->entries is sorted case-insensitively, then we need - * to re-sort it case-sensitively before writing */ - if (index->ignore_case) { - git_vector_dup(&case_sorted, &index->entries, index_cmp); - git_vector_sort(&case_sorted); - out = &case_sorted; - } - - git_vector_foreach(out, i, entry) - if ((error = write_disk_entry(file, entry)) < 0) - break; - - if (index->ignore_case) - git_vector_free(&case_sorted); - - return error; -} - -static int write_extension(git_filebuf *file, struct index_extension *header, git_buf *data) -{ - struct index_extension ondisk; - int error = 0; - - memset(&ondisk, 0x0, sizeof(struct index_extension)); - memcpy(&ondisk, header, 4); - ondisk.extension_size = htonl(header->extension_size); - - if ((error = git_filebuf_write(file, &ondisk, sizeof(struct index_extension))) == 0) - error = git_filebuf_write(file, data->ptr, data->size); - - return error; -} - -static int create_reuc_extension_data(git_buf *reuc_buf, git_index_reuc_entry *reuc) -{ - int i; - int error = 0; - - if ((error = git_buf_put(reuc_buf, reuc->path, strlen(reuc->path) + 1)) < 0) - return error; - - for (i = 0; i < 3; i++) { - if ((error = git_buf_printf(reuc_buf, "%o", reuc->mode[i])) < 0 || - (error = git_buf_put(reuc_buf, "\0", 1)) < 0) - return error; - } - - for (i = 0; i < 3; i++) { - if (reuc->mode[i] && (error = git_buf_put(reuc_buf, (char *)&reuc->oid[i].id, GIT_OID_RAWSZ)) < 0) - return error; - } - - return 0; -} - -static int write_reuc_extension(git_index *index, git_filebuf *file) -{ - git_buf reuc_buf = GIT_BUF_INIT; - git_vector *out = &index->reuc; - git_index_reuc_entry *reuc; - struct index_extension extension; - size_t i; - int error = 0; - - git_vector_foreach(out, i, reuc) { - if ((error = create_reuc_extension_data(&reuc_buf, reuc)) < 0) - goto done; - } - - memset(&extension, 0x0, sizeof(struct index_extension)); - memcpy(&extension.signature, INDEX_EXT_UNMERGED_SIG, 4); - extension.extension_size = (uint32_t)reuc_buf.size; - - error = write_extension(file, &extension, &reuc_buf); - - git_buf_free(&reuc_buf); - -done: - return error; -} - -static int write_index(git_index *index, git_filebuf *file) -{ - git_oid hash_final; - struct index_header header; - bool is_extended; - - assert(index && file); - - is_extended = is_index_extended(index); - - header.signature = htonl(INDEX_HEADER_SIG); - header.version = htonl(is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER); - header.entry_count = htonl((uint32_t)index->entries.length); - - if (git_filebuf_write(file, &header, sizeof(struct index_header)) < 0) - return -1; - - if (write_entries(index, file) < 0) - return -1; - - /* TODO: write tree cache extension */ - - /* write the reuc extension */ - if (index->reuc.length > 0 && write_reuc_extension(index, file) < 0) - return -1; - - /* get out the hash for all the contents we've appended to the file */ - git_filebuf_hash(&hash_final, file); - - /* write it at the end of the file */ - return git_filebuf_write(file, hash_final.id, GIT_OID_RAWSZ); -} - -int git_index_entry_stage(const git_index_entry *entry) -{ - return index_entry_stage(entry); -} - -typedef struct read_tree_data { - git_index *index; - git_transfer_progress *stats; -} read_tree_data; - -static int read_tree_cb(const char *root, const git_tree_entry *tentry, void *data) -{ - git_index *index = (git_index *)data; - git_index_entry *entry = NULL; - git_buf path = GIT_BUF_INIT; - - if (git_tree_entry__is_tree(tentry)) - return 0; - - if (git_buf_joinpath(&path, root, tentry->filename) < 0) - return -1; - - entry = git__calloc(1, sizeof(git_index_entry)); - GITERR_CHECK_ALLOC(entry); - - entry->mode = tentry->attr; - entry->oid = tentry->oid; - - if (path.size < GIT_IDXENTRY_NAMEMASK) - entry->flags = path.size & GIT_IDXENTRY_NAMEMASK; - else - entry->flags = GIT_IDXENTRY_NAMEMASK; - - entry->path = git_buf_detach(&path); - git_buf_free(&path); - - if (git_vector_insert(&index->entries, entry) < 0) { - index_entry_free(entry); - return -1; - } - - return 0; -} - -int git_index_read_tree(git_index *index, const git_tree *tree) -{ - git_index_clear(index); - - return git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, index); -} - -git_repository *git_index_owner(const git_index *index) -{ - return INDEX_OWNER(index); -} - -int git_index_read_tree_match( - git_index *index, git_tree *tree, git_strarray *strspec) -{ -#if 0 - git_iterator *iter = NULL; - const git_index_entry *entry; - char *pfx = NULL; - git_vector pathspec = GIT_VECTOR_INIT; - git_pool pathpool = GIT_POOL_INIT_STRINGPOOL; -#endif - - if (!git_pathspec_is_interesting(strspec)) - return git_index_read_tree(index, tree); - - return git_index_read_tree(index, tree); - -#if 0 - /* The following loads the matches into the index, but doesn't - * erase obsoleted entries (e.g. you load a blob at "a/b" which - * should obsolete a blob at "a/b/c/d" since b is no longer a tree) - */ - - if (git_pathspec_init(&pathspec, strspec, &pathpool) < 0) - return -1; - - pfx = git_pathspec_prefix(strspec); - - if ((error = git_iterator_for_tree_range(&iter, tree, pfx, pfx)) < 0 || - (error = git_iterator_current(iter, &entry)) < 0) - goto cleanup; - - while (entry != NULL) { - if (git_pathspec_match_path(&pathspec, entry->path, false, false) && - (error = git_index_add(index, entry)) < 0) - goto cleanup; - - if ((error = git_iterator_advance(iter, &entry)) < 0) - goto cleanup; - } - -cleanup: - git_iterator_free(iter); - git_pathspec_free(&pathspec); - git_pool_clear(&pathpool); - git__free(pfx); - - return error; -#endif -} diff --git a/src/index.h b/src/index.h deleted file mode 100644 index 9304b5539a8..00000000000 --- a/src/index.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_index_h__ -#define INCLUDE_index_h__ - -#include "fileops.h" -#include "filebuf.h" -#include "vector.h" -#include "tree-cache.h" -#include "git2/odb.h" -#include "git2/index.h" - -#define GIT_INDEX_FILE "index" -#define GIT_INDEX_FILE_MODE 0666 - -struct git_index { - git_refcount rc; - - char *index_file_path; - - git_futils_filestamp stamp; - git_vector entries; - - unsigned int on_disk:1; - - unsigned int ignore_case:1; - unsigned int distrust_filemode:1; - unsigned int no_symlinks:1; - - git_tree_cache *tree; - - git_vector reuc; - - git_vector_cmp entries_cmp_path; - git_vector_cmp entries_search; - git_vector_cmp entries_search_path; - git_vector_cmp reuc_search; -}; - -extern void git_index_entry__init_from_stat(git_index_entry *entry, struct stat *st); - -extern size_t git_index__prefix_position(git_index *index, const char *path); - -extern int git_index_entry__cmp(const void *a, const void *b); -extern int git_index_entry__cmp_icase(const void *a, const void *b); - -extern int git_index_read_tree_match( - git_index *index, git_tree *tree, git_strarray *strspec); - -#endif diff --git a/src/indexer.c b/src/indexer.c deleted file mode 100644 index 3f6b1076e26..00000000000 --- a/src/indexer.c +++ /dev/null @@ -1,1065 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include - -#include "git2/indexer.h" -#include "git2/object.h" -#include "git2/oid.h" - -#include "common.h" -#include "pack.h" -#include "mwindow.h" -#include "posix.h" -#include "pack.h" -#include "filebuf.h" - -#define UINT31_MAX (0x7FFFFFFF) - -struct entry { - git_oid oid; - uint32_t crc; - uint32_t offset; - uint64_t offset_long; -}; - -struct git_indexer { - struct git_pack_file *pack; - size_t nr_objects; - git_vector objects; - git_filebuf file; - unsigned int fanout[256]; - git_oid hash; -}; - -struct git_indexer_stream { - unsigned int parsed_header :1, - opened_pack :1, - have_stream :1, - have_delta :1; - struct git_pack_file *pack; - git_filebuf pack_file; - git_filebuf index_file; - git_off_t off; - git_off_t entry_start; - git_packfile_stream stream; - size_t nr_objects; - git_vector objects; - git_vector deltas; - unsigned int fanout[256]; - git_hash_ctx hash_ctx; - git_oid hash; - git_transfer_progress_callback progress_cb; - void *progress_payload; - char objbuf[8*1024]; -}; - -struct delta_info { - git_off_t delta_off; -}; - -const git_oid *git_indexer_hash(const git_indexer *idx) -{ - return &idx->hash; -} - -const git_oid *git_indexer_stream_hash(const git_indexer_stream *idx) -{ - return &idx->hash; -} - -static int open_pack(struct git_pack_file **out, const char *filename) -{ - size_t namelen; - struct git_pack_file *pack; - struct stat st; - int fd; - - namelen = strlen(filename); - pack = git__calloc(1, sizeof(struct git_pack_file) + namelen + 1); - GITERR_CHECK_ALLOC(pack); - - memcpy(pack->pack_name, filename, namelen + 1); - - if (p_stat(filename, &st) < 0) { - giterr_set(GITERR_OS, "Failed to stat packfile."); - goto cleanup; - } - - if ((fd = p_open(pack->pack_name, O_RDONLY)) < 0) { - giterr_set(GITERR_OS, "Failed to open packfile."); - goto cleanup; - } - - pack->mwf.fd = fd; - pack->mwf.size = (git_off_t)st.st_size; - - *out = pack; - return 0; - -cleanup: - git__free(pack); - return -1; -} - -static int parse_header(struct git_pack_header *hdr, struct git_pack_file *pack) -{ - int error; - - /* Verify we recognize this pack file format. */ - if ((error = p_read(pack->mwf.fd, hdr, sizeof(*hdr))) < 0) { - giterr_set(GITERR_OS, "Failed to read in pack header"); - return error; - } - - if (hdr->hdr_signature != ntohl(PACK_SIGNATURE)) { - giterr_set(GITERR_INDEXER, "Wrong pack signature"); - return -1; - } - - if (!pack_version_ok(hdr->hdr_version)) { - giterr_set(GITERR_INDEXER, "Wrong pack version"); - return -1; - } - - return 0; -} - -static int objects_cmp(const void *a, const void *b) -{ - const struct entry *entrya = a; - const struct entry *entryb = b; - - return git_oid_cmp(&entrya->oid, &entryb->oid); -} - -static int cache_cmp(const void *a, const void *b) -{ - const struct git_pack_entry *ea = a; - const struct git_pack_entry *eb = b; - - return git_oid_cmp(&ea->sha1, &eb->sha1); -} - -int git_indexer_stream_new( - git_indexer_stream **out, - const char *prefix, - git_transfer_progress_callback progress_cb, - void *progress_payload) -{ - git_indexer_stream *idx; - git_buf path = GIT_BUF_INIT; - static const char suff[] = "/pack"; - int error; - - idx = git__calloc(1, sizeof(git_indexer_stream)); - GITERR_CHECK_ALLOC(idx); - idx->progress_cb = progress_cb; - idx->progress_payload = progress_payload; - - error = git_buf_joinpath(&path, prefix, suff); - if (error < 0) - goto cleanup; - - error = git_filebuf_open(&idx->pack_file, path.ptr, - GIT_FILEBUF_TEMPORARY | GIT_FILEBUF_DO_NOT_BUFFER); - git_buf_free(&path); - if (error < 0) - goto cleanup; - - *out = idx; - return 0; - -cleanup: - git_buf_free(&path); - git_filebuf_cleanup(&idx->pack_file); - git__free(idx); - return -1; -} - -/* Try to store the delta so we can try to resolve it later */ -static int store_delta(git_indexer_stream *idx) -{ - struct delta_info *delta; - - delta = git__calloc(1, sizeof(struct delta_info)); - GITERR_CHECK_ALLOC(delta); - delta->delta_off = idx->entry_start; - - if (git_vector_insert(&idx->deltas, delta) < 0) - return -1; - - return 0; -} - -static void hash_header(git_hash_ctx *ctx, git_off_t len, git_otype type) -{ - char buffer[64]; - size_t hdrlen; - - hdrlen = git_odb__format_object_header(buffer, sizeof(buffer), (size_t)len, type); - git_hash_update(ctx, buffer, hdrlen); -} - -static int hash_object_stream(git_indexer_stream *idx, git_packfile_stream *stream) -{ - ssize_t read; - - assert(idx && stream); - - do { - if ((read = git_packfile_stream_read(stream, idx->objbuf, sizeof(idx->objbuf))) < 0) - break; - - git_hash_update(&idx->hash_ctx, idx->objbuf, read); - } while (read > 0); - - if (read < 0) - return (int)read; - - return 0; -} - -/* In order to create the packfile stream, we need to skip over the delta base description */ -static int advance_delta_offset(git_indexer_stream *idx, git_otype type) -{ - git_mwindow *w = NULL; - - assert(type == GIT_OBJ_REF_DELTA || type == GIT_OBJ_OFS_DELTA); - - if (type == GIT_OBJ_REF_DELTA) { - idx->off += GIT_OID_RAWSZ; - } else { - git_off_t base_off = get_delta_base(idx->pack, &w, &idx->off, type, idx->entry_start); - git_mwindow_close(&w); - if (base_off < 0) - return (int)base_off; - } - - return 0; -} - -/* Read from the stream and discard any output */ -static int read_object_stream(git_indexer_stream *idx, git_packfile_stream *stream) -{ - ssize_t read; - - assert(stream); - - do { - read = git_packfile_stream_read(stream, idx->objbuf, sizeof(idx->objbuf)); - } while (read > 0); - - if (read < 0) - return (int)read; - - return 0; -} - -static int crc_object(uint32_t *crc_out, git_mwindow_file *mwf, git_off_t start, git_off_t size) -{ - void *ptr; - uint32_t crc; - unsigned int left, len; - git_mwindow *w = NULL; - - crc = crc32(0L, Z_NULL, 0); - while (size) { - ptr = git_mwindow_open(mwf, &w, start, (size_t)size, &left); - if (ptr == NULL) - return -1; - - len = min(left, (unsigned int)size); - crc = crc32(crc, ptr, len); - size -= len; - start += len; - git_mwindow_close(&w); - } - - *crc_out = htonl(crc); - return 0; -} - -static int store_object(git_indexer_stream *idx) -{ - int i; - git_oid oid; - struct entry *entry; - git_off_t entry_size; - struct git_pack_entry *pentry; - git_hash_ctx *ctx = &idx->hash_ctx; - git_off_t entry_start = idx->entry_start; - - entry = git__calloc(1, sizeof(*entry)); - GITERR_CHECK_ALLOC(entry); - - pentry = git__malloc(sizeof(struct git_pack_entry)); - GITERR_CHECK_ALLOC(pentry); - - git_hash_final(&oid, ctx); - entry_size = idx->off - entry_start; - if (entry_start > UINT31_MAX) { - entry->offset = UINT32_MAX; - entry->offset_long = entry_start; - } else { - entry->offset = (uint32_t)entry_start; - } - - git_oid_cpy(&pentry->sha1, &oid); - pentry->offset = entry_start; - if (git_vector_insert(&idx->pack->cache, pentry) < 0) { - git__free(pentry); - goto on_error; - } - - git_oid_cpy(&entry->oid, &oid); - - if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0) - goto on_error; - - /* Add the object to the list */ - if (git_vector_insert(&idx->objects, entry) < 0) - goto on_error; - - for (i = oid.id[0]; i < 256; ++i) { - idx->fanout[i]++; - } - - return 0; - -on_error: - git__free(entry); - - return -1; -} - -static int hash_and_save(git_indexer_stream *idx, git_rawobj *obj, git_off_t entry_start) -{ - int i; - git_oid oid; - size_t entry_size; - struct entry *entry; - struct git_pack_entry *pentry; - - entry = git__calloc(1, sizeof(*entry)); - GITERR_CHECK_ALLOC(entry); - - if (entry_start > UINT31_MAX) { - entry->offset = UINT32_MAX; - entry->offset_long = entry_start; - } else { - entry->offset = (uint32_t)entry_start; - } - - /* FIXME: Parse the object instead of hashing it */ - if (git_odb__hashobj(&oid, obj) < 0) { - giterr_set(GITERR_INDEXER, "Failed to hash object"); - return -1; - } - - pentry = git__malloc(sizeof(struct git_pack_entry)); - GITERR_CHECK_ALLOC(pentry); - - git_oid_cpy(&pentry->sha1, &oid); - pentry->offset = entry_start; - if (git_vector_insert(&idx->pack->cache, pentry) < 0) { - git__free(pentry); - goto on_error; - } - - git_oid_cpy(&entry->oid, &oid); - entry->crc = crc32(0L, Z_NULL, 0); - - entry_size = (size_t)(idx->off - entry_start); - if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0) - goto on_error; - - /* Add the object to the list */ - if (git_vector_insert(&idx->objects, entry) < 0) - goto on_error; - - for (i = oid.id[0]; i < 256; ++i) { - idx->fanout[i]++; - } - - return 0; - -on_error: - git__free(entry); - git__free(obj->data); - return -1; -} - -static void do_progress_callback(git_indexer_stream *idx, git_transfer_progress *stats) -{ - if (!idx->progress_cb) return; - idx->progress_cb(stats, idx->progress_payload); -} - -int git_indexer_stream_add(git_indexer_stream *idx, const void *data, size_t size, git_transfer_progress *stats) -{ - int error; - struct git_pack_header hdr; - size_t processed; - git_mwindow_file *mwf = &idx->pack->mwf; - - assert(idx && data && stats); - - processed = stats->indexed_objects; - - if (git_filebuf_write(&idx->pack_file, data, size) < 0) - return -1; - - /* Make sure we set the new size of the pack */ - if (idx->opened_pack) { - idx->pack->mwf.size += size; - //printf("\nadding %zu for %zu\n", size, idx->pack->mwf.size); - } else { - if (open_pack(&idx->pack, idx->pack_file.path_lock) < 0) - return -1; - idx->opened_pack = 1; - mwf = &idx->pack->mwf; - if (git_mwindow_file_register(&idx->pack->mwf) < 0) - return -1; - } - - if (!idx->parsed_header) { - if ((unsigned)idx->pack->mwf.size < sizeof(hdr)) - return 0; - - if (parse_header(&hdr, idx->pack) < 0) - return -1; - - idx->parsed_header = 1; - idx->nr_objects = ntohl(hdr.hdr_entries); - idx->off = sizeof(struct git_pack_header); - - /* for now, limit to 2^32 objects */ - assert(idx->nr_objects == (size_t)((unsigned int)idx->nr_objects)); - - if (git_vector_init(&idx->pack->cache, (unsigned int)idx->nr_objects, cache_cmp) < 0) - return -1; - - idx->pack->has_cache = 1; - if (git_vector_init(&idx->objects, (unsigned int)idx->nr_objects, objects_cmp) < 0) - return -1; - - if (git_vector_init(&idx->deltas, (unsigned int)(idx->nr_objects / 2), NULL) < 0) - return -1; - - stats->received_objects = 0; - stats->indexed_objects = 0; - stats->total_objects = (unsigned int)idx->nr_objects; - do_progress_callback(idx, stats); - } - - /* Now that we have data in the pack, let's try to parse it */ - - /* As the file grows any windows we try to use will be out of date */ - git_mwindow_free_all(mwf); - while (processed < idx->nr_objects) { - git_packfile_stream *stream = &idx->stream; - git_off_t entry_start = idx->off; - size_t entry_size; - git_otype type; - git_mwindow *w = NULL; - - if (idx->pack->mwf.size <= idx->off + 20) - return 0; - - if (!idx->have_stream) { - error = git_packfile_unpack_header(&entry_size, &type, mwf, &w, &idx->off); - if (error == GIT_EBUFS) { - idx->off = entry_start; - return 0; - } - if (error < 0) - return -1; - - git_mwindow_close(&w); - idx->entry_start = entry_start; - git_hash_ctx_init(&idx->hash_ctx); - - if (type == GIT_OBJ_REF_DELTA || type == GIT_OBJ_OFS_DELTA) { - error = advance_delta_offset(idx, type); - if (error == GIT_EBUFS) { - idx->off = entry_start; - return 0; - } - if (error < 0) - return -1; - - idx->have_delta = 1; - } else { - idx->have_delta = 0; - hash_header(&idx->hash_ctx, entry_size, type); - } - - idx->have_stream = 1; - if (git_packfile_stream_open(stream, idx->pack, idx->off) < 0) - goto on_error; - - } - - if (idx->have_delta) { - error = read_object_stream(idx, stream); - } else { - error = hash_object_stream(idx, stream); - } - - idx->off = stream->curpos; - if (error == GIT_EBUFS) - return 0; - - /* We want to free the stream reasorces no matter what here */ - idx->have_stream = 0; - git_packfile_stream_free(stream); - - if (error < 0) - goto on_error; - - if (idx->have_delta) { - error = store_delta(idx); - } else { - error = store_object(idx); - } - - if (error < 0) - goto on_error; - - if (!idx->have_delta) { - stats->indexed_objects = (unsigned int)++processed; - } - stats->received_objects++; - - do_progress_callback(idx, stats); - } - - return 0; - -on_error: - git_mwindow_free_all(mwf); - return -1; -} - -static int index_path_stream(git_buf *path, git_indexer_stream *idx, const char *suffix) -{ - const char prefix[] = "pack-"; - size_t slash = (size_t)path->size; - - /* search backwards for '/' */ - while (slash > 0 && path->ptr[slash - 1] != '/') - slash--; - - if (git_buf_grow(path, slash + 1 + strlen(prefix) + - GIT_OID_HEXSZ + strlen(suffix) + 1) < 0) - return -1; - - git_buf_truncate(path, slash); - git_buf_puts(path, prefix); - git_oid_fmt(path->ptr + git_buf_len(path), &idx->hash); - path->size += GIT_OID_HEXSZ; - git_buf_puts(path, suffix); - - return git_buf_oom(path) ? -1 : 0; -} - -static int resolve_deltas(git_indexer_stream *idx, git_transfer_progress *stats) -{ - unsigned int i; - struct delta_info *delta; - - git_vector_foreach(&idx->deltas, i, delta) { - git_rawobj obj; - - idx->off = delta->delta_off; - if (git_packfile_unpack(&obj, idx->pack, &idx->off) < 0) - return -1; - - if (hash_and_save(idx, &obj, delta->delta_off) < 0) - return -1; - - git__free(obj.data); - stats->indexed_objects++; - do_progress_callback(idx, stats); - } - - return 0; -} - -int git_indexer_stream_finalize(git_indexer_stream *idx, git_transfer_progress *stats) -{ - git_mwindow *w = NULL; - unsigned int i, long_offsets = 0, left; - struct git_pack_idx_header hdr; - git_buf filename = GIT_BUF_INIT; - struct entry *entry; - void *packfile_hash; - git_oid file_hash; - git_hash_ctx ctx; - - if (git_hash_ctx_init(&ctx) < 0) - return -1; - - /* Test for this before resolve_deltas(), as it plays with idx->off */ - if (idx->off < idx->pack->mwf.size - GIT_OID_RAWSZ) { - giterr_set(GITERR_INDEXER, "Indexing error: unexpected data at the end of the pack"); - return -1; - } - - if (idx->deltas.length > 0) - if (resolve_deltas(idx, stats) < 0) - return -1; - - if (stats->indexed_objects != stats->total_objects) { - giterr_set(GITERR_INDEXER, "Indexing error: early EOF"); - return -1; - } - - git_vector_sort(&idx->objects); - - git_buf_sets(&filename, idx->pack->pack_name); - git_buf_truncate(&filename, filename.size - strlen("pack")); - git_buf_puts(&filename, "idx"); - if (git_buf_oom(&filename)) - return -1; - - if (git_filebuf_open(&idx->index_file, filename.ptr, GIT_FILEBUF_HASH_CONTENTS) < 0) - goto on_error; - - /* Write out the header */ - hdr.idx_signature = htonl(PACK_IDX_SIGNATURE); - hdr.idx_version = htonl(2); - git_filebuf_write(&idx->index_file, &hdr, sizeof(hdr)); - - /* Write out the fanout table */ - for (i = 0; i < 256; ++i) { - uint32_t n = htonl(idx->fanout[i]); - git_filebuf_write(&idx->index_file, &n, sizeof(n)); - } - - /* Write out the object names (SHA-1 hashes) */ - git_vector_foreach(&idx->objects, i, entry) { - git_filebuf_write(&idx->index_file, &entry->oid, sizeof(git_oid)); - git_hash_update(&ctx, &entry->oid, GIT_OID_RAWSZ); - } - git_hash_final(&idx->hash, &ctx); - - /* Write out the CRC32 values */ - git_vector_foreach(&idx->objects, i, entry) { - git_filebuf_write(&idx->index_file, &entry->crc, sizeof(uint32_t)); - } - - /* Write out the offsets */ - git_vector_foreach(&idx->objects, i, entry) { - uint32_t n; - - if (entry->offset == UINT32_MAX) - n = htonl(0x80000000 | long_offsets++); - else - n = htonl(entry->offset); - - git_filebuf_write(&idx->index_file, &n, sizeof(uint32_t)); - } - - /* Write out the long offsets */ - git_vector_foreach(&idx->objects, i, entry) { - uint32_t split[2]; - - if (entry->offset != UINT32_MAX) - continue; - - split[0] = htonl(entry->offset_long >> 32); - split[1] = htonl(entry->offset_long & 0xffffffff); - - git_filebuf_write(&idx->index_file, &split, sizeof(uint32_t) * 2); - } - - /* Write out the packfile trailer */ - packfile_hash = git_mwindow_open(&idx->pack->mwf, &w, idx->pack->mwf.size - GIT_OID_RAWSZ, GIT_OID_RAWSZ, &left); - if (packfile_hash == NULL) { - git_mwindow_close(&w); - goto on_error; - } - - memcpy(&file_hash, packfile_hash, GIT_OID_RAWSZ); - git_mwindow_close(&w); - - git_filebuf_write(&idx->index_file, &file_hash, sizeof(git_oid)); - - /* Write out the packfile trailer to the idx file as well */ - if (git_filebuf_hash(&file_hash, &idx->index_file) < 0) - goto on_error; - - git_filebuf_write(&idx->index_file, &file_hash, sizeof(git_oid)); - - /* Figure out what the final name should be */ - if (index_path_stream(&filename, idx, ".idx") < 0) - goto on_error; - - /* Commit file */ - if (git_filebuf_commit_at(&idx->index_file, filename.ptr, GIT_PACK_FILE_MODE) < 0) - goto on_error; - - git_mwindow_free_all(&idx->pack->mwf); - /* We need to close the descriptor here so Windows doesn't choke on commit_at */ - p_close(idx->pack->mwf.fd); - idx->pack->mwf.fd = -1; - - if (index_path_stream(&filename, idx, ".pack") < 0) - goto on_error; - /* And don't forget to rename the packfile to its new place. */ - if (git_filebuf_commit_at(&idx->pack_file, filename.ptr, GIT_PACK_FILE_MODE) < 0) - return -1; - - git_buf_free(&filename); - return 0; - -on_error: - git_mwindow_free_all(&idx->pack->mwf); - git_filebuf_cleanup(&idx->index_file); - git_buf_free(&filename); - git_hash_ctx_cleanup(&ctx); - return -1; -} - -void git_indexer_stream_free(git_indexer_stream *idx) -{ - unsigned int i; - struct entry *e; - struct git_pack_entry *pe; - struct delta_info *delta; - - if (idx == NULL) - return; - - git_vector_foreach(&idx->objects, i, e) - git__free(e); - git_vector_free(&idx->objects); - if (idx->pack) { - git_vector_foreach(&idx->pack->cache, i, pe) - git__free(pe); - git_vector_free(&idx->pack->cache); - } - git_vector_foreach(&idx->deltas, i, delta) - git__free(delta); - git_vector_free(&idx->deltas); - git_packfile_free(idx->pack); - git__free(idx); -} - -int git_indexer_new(git_indexer **out, const char *packname) -{ - git_indexer *idx; - struct git_pack_header hdr; - int error; - - assert(out && packname); - - idx = git__calloc(1, sizeof(git_indexer)); - GITERR_CHECK_ALLOC(idx); - - open_pack(&idx->pack, packname); - - if ((error = parse_header(&hdr, idx->pack)) < 0) - goto cleanup; - - idx->nr_objects = ntohl(hdr.hdr_entries); - - /* for now, limit to 2^32 objects */ - assert(idx->nr_objects == (size_t)((unsigned int)idx->nr_objects)); - - error = git_vector_init(&idx->pack->cache, (unsigned int)idx->nr_objects, cache_cmp); - if (error < 0) - goto cleanup; - - idx->pack->has_cache = 1; - error = git_vector_init(&idx->objects, (unsigned int)idx->nr_objects, objects_cmp); - if (error < 0) - goto cleanup; - - *out = idx; - - return 0; - -cleanup: - git_indexer_free(idx); - - return -1; -} - -static int index_path(git_buf *path, git_indexer *idx) -{ - const char prefix[] = "pack-", suffix[] = ".idx"; - size_t slash = (size_t)path->size; - - /* search backwards for '/' */ - while (slash > 0 && path->ptr[slash - 1] != '/') - slash--; - - if (git_buf_grow(path, slash + 1 + strlen(prefix) + - GIT_OID_HEXSZ + strlen(suffix) + 1) < 0) - return -1; - - git_buf_truncate(path, slash); - git_buf_puts(path, prefix); - git_oid_fmt(path->ptr + git_buf_len(path), &idx->hash); - path->size += GIT_OID_HEXSZ; - git_buf_puts(path, suffix); - - return git_buf_oom(path) ? -1 : 0; -} - -int git_indexer_write(git_indexer *idx) -{ - git_mwindow *w = NULL; - int error; - unsigned int i, long_offsets = 0, left; - struct git_pack_idx_header hdr; - git_buf filename = GIT_BUF_INIT; - struct entry *entry; - void *packfile_hash; - git_oid file_hash; - git_hash_ctx ctx; - - if (git_hash_ctx_init(&ctx) < 0) - return -1; - - git_vector_sort(&idx->objects); - - git_buf_sets(&filename, idx->pack->pack_name); - git_buf_truncate(&filename, filename.size - strlen("pack")); - git_buf_puts(&filename, "idx"); - if (git_buf_oom(&filename)) - return -1; - - error = git_filebuf_open(&idx->file, filename.ptr, GIT_FILEBUF_HASH_CONTENTS); - if (error < 0) - goto cleanup; - - /* Write out the header */ - hdr.idx_signature = htonl(PACK_IDX_SIGNATURE); - hdr.idx_version = htonl(2); - error = git_filebuf_write(&idx->file, &hdr, sizeof(hdr)); - if (error < 0) - goto cleanup; - - /* Write out the fanout table */ - for (i = 0; i < 256; ++i) { - uint32_t n = htonl(idx->fanout[i]); - error = git_filebuf_write(&idx->file, &n, sizeof(n)); - if (error < 0) - goto cleanup; - } - - /* Write out the object names (SHA-1 hashes) */ - git_vector_foreach(&idx->objects, i, entry) { - if ((error = git_filebuf_write(&idx->file, &entry->oid, sizeof(git_oid))) < 0 || - (error = git_hash_update(&ctx, &entry->oid, GIT_OID_RAWSZ)) < 0) - goto cleanup; - } - - if ((error = git_hash_final(&idx->hash, &ctx)) < 0) - goto cleanup; - - /* Write out the CRC32 values */ - git_vector_foreach(&idx->objects, i, entry) { - error = git_filebuf_write(&idx->file, &entry->crc, sizeof(uint32_t)); - if (error < 0) - goto cleanup; - } - - /* Write out the offsets */ - git_vector_foreach(&idx->objects, i, entry) { - uint32_t n; - - if (entry->offset == UINT32_MAX) - n = htonl(0x80000000 | long_offsets++); - else - n = htonl(entry->offset); - - error = git_filebuf_write(&idx->file, &n, sizeof(uint32_t)); - if (error < 0) - goto cleanup; - } - - /* Write out the long offsets */ - git_vector_foreach(&idx->objects, i, entry) { - uint32_t split[2]; - - if (entry->offset != UINT32_MAX) - continue; - - split[0] = htonl(entry->offset_long >> 32); - split[1] = htonl(entry->offset_long & 0xffffffff); - - error = git_filebuf_write(&idx->file, &split, sizeof(uint32_t) * 2); - if (error < 0) - goto cleanup; - } - - /* Write out the packfile trailer */ - - packfile_hash = git_mwindow_open(&idx->pack->mwf, &w, idx->pack->mwf.size - GIT_OID_RAWSZ, GIT_OID_RAWSZ, &left); - git_mwindow_close(&w); - if (packfile_hash == NULL) { - error = -1; - goto cleanup; - } - - memcpy(&file_hash, packfile_hash, GIT_OID_RAWSZ); - - git_mwindow_close(&w); - - error = git_filebuf_write(&idx->file, &file_hash, sizeof(git_oid)); - if (error < 0) - goto cleanup; - - /* Write out the index sha */ - error = git_filebuf_hash(&file_hash, &idx->file); - if (error < 0) - goto cleanup; - - error = git_filebuf_write(&idx->file, &file_hash, sizeof(git_oid)); - if (error < 0) - goto cleanup; - - /* Figure out what the final name should be */ - error = index_path(&filename, idx); - if (error < 0) - goto cleanup; - - /* Commit file */ - error = git_filebuf_commit_at(&idx->file, filename.ptr, GIT_PACK_FILE_MODE); - -cleanup: - git_mwindow_free_all(&idx->pack->mwf); - git_mwindow_file_deregister(&idx->pack->mwf); - if (error < 0) - git_filebuf_cleanup(&idx->file); - git_buf_free(&filename); - git_hash_ctx_cleanup(&ctx); - - return error; -} - -int git_indexer_run(git_indexer *idx, git_transfer_progress *stats) -{ - git_mwindow_file *mwf; - git_off_t off = sizeof(struct git_pack_header); - int error; - struct entry *entry; - unsigned int left, processed; - - assert(idx && stats); - - mwf = &idx->pack->mwf; - error = git_mwindow_file_register(mwf); - if (error < 0) - return error; - - stats->total_objects = (unsigned int)idx->nr_objects; - stats->indexed_objects = processed = 0; - - while (processed < idx->nr_objects) { - git_rawobj obj; - git_oid oid; - struct git_pack_entry *pentry; - git_mwindow *w = NULL; - int i; - git_off_t entry_start = off; - void *packed; - size_t entry_size; - char fmt[GIT_OID_HEXSZ] = {0}; - - entry = git__calloc(1, sizeof(*entry)); - GITERR_CHECK_ALLOC(entry); - - if (off > UINT31_MAX) { - entry->offset = UINT32_MAX; - entry->offset_long = off; - } else { - entry->offset = (uint32_t)off; - } - - error = git_packfile_unpack(&obj, idx->pack, &off); - if (error < 0) - goto cleanup; - - /* FIXME: Parse the object instead of hashing it */ - error = git_odb__hashobj(&oid, &obj); - if (error < 0) { - giterr_set(GITERR_INDEXER, "Failed to hash object"); - goto cleanup; - } - - pentry = git__malloc(sizeof(struct git_pack_entry)); - if (pentry == NULL) { - error = -1; - goto cleanup; - } - - git_oid_cpy(&pentry->sha1, &oid); - pentry->offset = entry_start; - git_oid_fmt(fmt, &oid); - error = git_vector_insert(&idx->pack->cache, pentry); - if (error < 0) - goto cleanup; - - git_oid_cpy(&entry->oid, &oid); - entry->crc = crc32(0L, Z_NULL, 0); - - entry_size = (size_t)(off - entry_start); - packed = git_mwindow_open(mwf, &w, entry_start, entry_size, &left); - if (packed == NULL) { - error = -1; - goto cleanup; - } - entry->crc = htonl(crc32(entry->crc, packed, (uInt)entry_size)); - git_mwindow_close(&w); - - /* Add the object to the list */ - error = git_vector_insert(&idx->objects, entry); - if (error < 0) - goto cleanup; - - for (i = oid.id[0]; i < 256; ++i) { - idx->fanout[i]++; - } - - git__free(obj.data); - - stats->indexed_objects = ++processed; - } - -cleanup: - git_mwindow_free_all(mwf); - - return error; - -} - -void git_indexer_free(git_indexer *idx) -{ - unsigned int i; - struct entry *e; - struct git_pack_entry *pe; - - if (idx == NULL) - return; - - git_mwindow_file_deregister(&idx->pack->mwf); - git_vector_foreach(&idx->objects, i, e) - git__free(e); - git_vector_free(&idx->objects); - git_vector_foreach(&idx->pack->cache, i, pe) - git__free(pe); - git_vector_free(&idx->pack->cache); - git_packfile_free(idx->pack); - git__free(idx); -} - diff --git a/src/iterator.c b/src/iterator.c deleted file mode 100644 index 8ad639d6bdf..00000000000 --- a/src/iterator.c +++ /dev/null @@ -1,1241 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "iterator.h" -#include "tree.h" -#include "ignore.h" -#include "buffer.h" -#include "git2/submodule.h" -#include - -#define ITERATOR_SET_CB(P,NAME_LC) do { \ - (P)->cb.current = NAME_LC ## _iterator__current; \ - (P)->cb.at_end = NAME_LC ## _iterator__at_end; \ - (P)->cb.advance = NAME_LC ## _iterator__advance; \ - (P)->cb.seek = NAME_LC ## _iterator__seek; \ - (P)->cb.reset = NAME_LC ## _iterator__reset; \ - (P)->cb.free = NAME_LC ## _iterator__free; \ - } while (0) - -#define ITERATOR_BASE_INIT(P,NAME_LC,NAME_UC) do { \ - (P) = git__calloc(1, sizeof(NAME_LC ## _iterator)); \ - GITERR_CHECK_ALLOC(P); \ - (P)->base.type = GIT_ITERATOR_TYPE_ ## NAME_UC; \ - (P)->base.cb = &(P)->cb; \ - ITERATOR_SET_CB(P,NAME_LC); \ - (P)->base.start = start ? git__strdup(start) : NULL; \ - (P)->base.end = end ? git__strdup(end) : NULL; \ - if ((start && !(P)->base.start) || (end && !(P)->base.end)) { \ - git__free(P); return -1; } \ - (P)->base.prefixcomp = git__prefixcmp; \ - } while (0) - -static int iterator__reset_range( - git_iterator *iter, const char *start, const char *end) -{ - if (start) { - if (iter->start) - git__free(iter->start); - iter->start = git__strdup(start); - GITERR_CHECK_ALLOC(iter->start); - } - - if (end) { - if (iter->end) - git__free(iter->end); - iter->end = git__strdup(end); - GITERR_CHECK_ALLOC(iter->end); - } - - return 0; -} - -static int iterator_update_ignore_case( - git_iterator *iter, - git_iterator_flag_t flags) -{ - int error = 0, ignore_case = -1; - - if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0) - ignore_case = true; - else if ((flags & GIT_ITERATOR_DONT_IGNORE_CASE) != 0) - ignore_case = false; - else { - git_index *index; - - if (!(error = git_repository_index__weakptr(&index, iter->repo))) - ignore_case = (index->ignore_case != false); - } - - if (ignore_case > 0) - iter->flags = (iter->flags | GIT_ITERATOR_IGNORE_CASE); - else if (ignore_case == 0) - iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE); - - iter->prefixcomp = ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) ? - git__prefixcmp_icase : git__prefixcmp; - - return error; -} - -static int empty_iterator__no_item( - git_iterator *iter, const git_index_entry **entry) -{ - GIT_UNUSED(iter); - *entry = NULL; - return 0; -} - -static int empty_iterator__at_end(git_iterator *iter) -{ - GIT_UNUSED(iter); - return 1; -} - -static int empty_iterator__reset( - git_iterator *iter, const char *start, const char *end) -{ - GIT_UNUSED(iter); GIT_UNUSED(start); GIT_UNUSED(end); - return 0; -} - -static int empty_iterator__seek(git_iterator *iter, const char *prefix) -{ - GIT_UNUSED(iter); GIT_UNUSED(prefix); - return -1; -} - -static void empty_iterator__free(git_iterator *iter) -{ - GIT_UNUSED(iter); -} - -typedef struct { - git_iterator base; - git_iterator_callbacks cb; -} empty_iterator; - -int git_iterator_for_nothing(git_iterator **iter, git_iterator_flag_t flags) -{ - empty_iterator *i = git__calloc(1, sizeof(empty_iterator)); - GITERR_CHECK_ALLOC(i); - - i->base.type = GIT_ITERATOR_TYPE_EMPTY; - i->base.cb = &i->cb; - i->base.flags = flags; - i->cb.current = empty_iterator__no_item; - i->cb.at_end = empty_iterator__at_end; - i->cb.advance = empty_iterator__no_item; - i->cb.seek = empty_iterator__seek; - i->cb.reset = empty_iterator__reset; - i->cb.free = empty_iterator__free; - - *iter = (git_iterator *)i; - - return 0; -} - - -typedef struct tree_iterator_frame tree_iterator_frame; -struct tree_iterator_frame { - tree_iterator_frame *next, *prev; - git_tree *tree; - char *start; - size_t startlen; - size_t index; - void **icase_map; - void *icase_data[GIT_FLEX_ARRAY]; -}; - -typedef struct { - git_iterator base; - git_iterator_callbacks cb; - tree_iterator_frame *stack, *tail; - git_index_entry entry; - git_buf path; - bool path_has_filename; -} tree_iterator; - -GIT_INLINE(const git_tree_entry *)tree_iterator__tree_entry(tree_iterator *ti) -{ - tree_iterator_frame *tf = ti->stack; - - if (tf->index >= git_tree_entrycount(tf->tree)) - return NULL; - - return git_tree_entry_byindex( - tf->tree, tf->icase_map ? (size_t)tf->icase_map[tf->index] : tf->index); -} - -static char *tree_iterator__current_filename( - tree_iterator *ti, const git_tree_entry *te) -{ - if (!ti->path_has_filename) { - if (git_buf_joinpath(&ti->path, ti->path.ptr, te->filename) < 0) - return NULL; - ti->path_has_filename = true; - } - - return ti->path.ptr; -} - -static void tree_iterator__free_frame(tree_iterator_frame *tf) -{ - if (!tf) - return; - - git_tree_free(tf->tree); - tf->tree = NULL; - - git__free(tf); -} - -static bool tree_iterator__pop_frame(tree_iterator *ti) -{ - tree_iterator_frame *tf = ti->stack; - - /* don't free the initial tree/frame */ - if (!tf->next) - return false; - - ti->stack = tf->next; - ti->stack->prev = NULL; - - tree_iterator__free_frame(tf); - - return true; -} - -static int tree_iterator__to_end(tree_iterator *ti) -{ - while (tree_iterator__pop_frame(ti)) /* pop all */; - ti->stack->index = git_tree_entrycount(ti->stack->tree); - return 0; -} - -static int tree_iterator__current( - git_iterator *self, const git_index_entry **entry) -{ - tree_iterator *ti = (tree_iterator *)self; - const git_tree_entry *te = tree_iterator__tree_entry(ti); - - if (entry) - *entry = NULL; - - if (te == NULL) - return 0; - - ti->entry.mode = te->attr; - git_oid_cpy(&ti->entry.oid, &te->oid); - - ti->entry.path = tree_iterator__current_filename(ti, te); - if (ti->entry.path == NULL) - return -1; - - if (ti->base.end && ti->base.prefixcomp(ti->entry.path, ti->base.end) > 0) - return tree_iterator__to_end(ti); - - if (entry) - *entry = &ti->entry; - - return 0; -} - -static int tree_iterator__at_end(git_iterator *self) -{ - return (tree_iterator__tree_entry((tree_iterator *)self) == NULL); -} - -static int tree_iterator__icase_map_cmp(const void *a, const void *b, void *data) -{ - git_tree *tree = data; - const git_tree_entry *te1 = git_tree_entry_byindex(tree, (size_t)a); - const git_tree_entry *te2 = git_tree_entry_byindex(tree, (size_t)b); - - return te1 ? (te2 ? git_tree_entry_icmp(te1, te2) : 1) : -1; -} - -static int tree_iterator__frame_start_icmp(const void *key, const void *el) -{ - const tree_iterator_frame *tf = (const tree_iterator_frame *)key; - const git_tree_entry *te = git_tree_entry_byindex(tf->tree, (size_t)el); - size_t minlen = min(tf->startlen, te->filename_len); - - return git__strncasecmp(tf->start, te->filename, minlen); -} - -static void tree_iterator__frame_seek_start(tree_iterator_frame *tf) -{ - if (!tf->start) - tf->index = 0; - else if (!tf->icase_map) - tf->index = git_tree__prefix_position(tf->tree, tf->start); - else { - if (!git__bsearch( - tf->icase_map, git_tree_entrycount(tf->tree), - tf, tree_iterator__frame_start_icmp, &tf->index)) - { - while (tf->index > 0) { - /* move back while previous entry is still prefixed */ - if (tree_iterator__frame_start_icmp( - tf, (const void *)(tf->index - 1))) - break; - tf->index--; - } - } - } -} - -static tree_iterator_frame *tree_iterator__alloc_frame( - tree_iterator *ti, git_tree *tree, char *start) -{ - size_t i, max_i = git_tree_entrycount(tree); - tree_iterator_frame *tf = - git__calloc(1, sizeof(tree_iterator_frame) + max_i * sizeof(void *)); - if (!tf) - return NULL; - - tf->tree = tree; - - if (start && *start) { - tf->start = start; - tf->startlen = strlen(start); - } - - if (!max_i) - return tf; - - if ((ti->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0) { - tf->icase_map = tf->icase_data; - - for (i = 0; i < max_i; ++i) - tf->icase_map[i] = (void *)i; - - git__tsort_r( - tf->icase_map, max_i, tree_iterator__icase_map_cmp, tf->tree); - } - - tree_iterator__frame_seek_start(tf); - - return tf; -} - -static int tree_iterator__expand_tree(tree_iterator *ti) -{ - int error; - git_tree *subtree; - const git_tree_entry *te = tree_iterator__tree_entry(ti); - tree_iterator_frame *tf; - char *relpath; - - while (te != NULL && git_tree_entry__is_tree(te)) { - if (git_buf_joinpath(&ti->path, ti->path.ptr, te->filename) < 0) - return -1; - - /* check that we have not passed the range end */ - if (ti->base.end != NULL && - ti->base.prefixcomp(ti->path.ptr, ti->base.end) > 0) - return tree_iterator__to_end(ti); - - if ((error = git_tree_lookup(&subtree, ti->base.repo, &te->oid)) < 0) - return error; - - relpath = NULL; - - /* apply range start to new frame if relevant */ - if (ti->stack->start && - ti->base.prefixcomp(ti->stack->start, te->filename) == 0) - { - if (ti->stack->start[te->filename_len] == '/') - relpath = ti->stack->start + te->filename_len + 1; - } - - if ((tf = tree_iterator__alloc_frame(ti, subtree, relpath)) == NULL) - return -1; - - tf->next = ti->stack; - ti->stack = tf; - tf->next->prev = tf; - - te = tree_iterator__tree_entry(ti); - } - - return 0; -} - -static int tree_iterator__advance( - git_iterator *self, const git_index_entry **entry) -{ - int error = 0; - tree_iterator *ti = (tree_iterator *)self; - const git_tree_entry *te = NULL; - - if (entry != NULL) - *entry = NULL; - - if (ti->path_has_filename) { - git_buf_rtruncate_at_char(&ti->path, '/'); - ti->path_has_filename = false; - } - - while (1) { - ++ti->stack->index; - - if ((te = tree_iterator__tree_entry(ti)) != NULL) - break; - - if (!tree_iterator__pop_frame(ti)) - break; /* no frames left to pop */ - - git_buf_rtruncate_at_char(&ti->path, '/'); - } - - if (te && git_tree_entry__is_tree(te)) - error = tree_iterator__expand_tree(ti); - - if (!error) - error = tree_iterator__current(self, entry); - - return error; -} - -static int tree_iterator__seek(git_iterator *self, const char *prefix) -{ - GIT_UNUSED(self); - GIT_UNUSED(prefix); - /* pop stack until matches prefix */ - /* seek item in current frame matching prefix */ - /* push stack which matches prefix */ - return -1; -} - -static void tree_iterator__free(git_iterator *self) -{ - tree_iterator *ti = (tree_iterator *)self; - - while (tree_iterator__pop_frame(ti)) /* pop all */; - - tree_iterator__free_frame(ti->stack); - ti->stack = ti->tail = NULL; - - git_buf_free(&ti->path); -} - -static int tree_iterator__reset( - git_iterator *self, const char *start, const char *end) -{ - tree_iterator *ti = (tree_iterator *)self; - - while (tree_iterator__pop_frame(ti)) /* pop all */; - - if (iterator__reset_range(self, start, end) < 0) - return -1; - - /* reset start position */ - tree_iterator__frame_seek_start(ti->stack); - - git_buf_clear(&ti->path); - ti->path_has_filename = false; - - return tree_iterator__expand_tree(ti); -} - -int git_iterator_for_tree_range( - git_iterator **iter, - git_tree *tree, - git_iterator_flag_t flags, - const char *start, - const char *end) -{ - int error; - tree_iterator *ti; - - if (tree == NULL) - return git_iterator_for_nothing(iter, flags); - - if ((error = git_tree__dup(&tree, tree)) < 0) - return error; - - ITERATOR_BASE_INIT(ti, tree, TREE); - - ti->base.repo = git_tree_owner(tree); - - if ((error = iterator_update_ignore_case((git_iterator *)ti, flags)) < 0) - goto fail; - - ti->stack = ti->tail = tree_iterator__alloc_frame(ti, tree, ti->base.start); - - if ((error = tree_iterator__expand_tree(ti)) < 0) - goto fail; - - *iter = (git_iterator *)ti; - return 0; - -fail: - git_iterator_free((git_iterator *)ti); - return error; -} - - -typedef struct { - git_iterator base; - git_iterator_callbacks cb; - git_index *index; - size_t current; -} index_iterator; - -static int index_iterator__current( - git_iterator *self, const git_index_entry **entry) -{ - index_iterator *ii = (index_iterator *)self; - const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current); - - if (entry) - *entry = ie; - - return 0; -} - -static int index_iterator__at_end(git_iterator *self) -{ - index_iterator *ii = (index_iterator *)self; - return (ii->current >= git_index_entrycount(ii->index)); -} - -static void index_iterator__skip_conflicts( - index_iterator *ii) -{ - size_t entrycount = git_index_entrycount(ii->index); - const git_index_entry *ie; - - while (ii->current < entrycount) { - ie = git_index_get_byindex(ii->index, ii->current); - - if (ie == NULL || - (ii->base.end != NULL && - ii->base.prefixcomp(ie->path, ii->base.end) > 0)) { - ii->current = entrycount; - break; - } - - if (git_index_entry_stage(ie) == 0) - break; - - ii->current++; - } -} - -static int index_iterator__advance( - git_iterator *self, const git_index_entry **entry) -{ - index_iterator *ii = (index_iterator *)self; - - if (ii->current < git_index_entrycount(ii->index)) - ii->current++; - - index_iterator__skip_conflicts(ii); - - return index_iterator__current(self, entry); -} - -static int index_iterator__seek(git_iterator *self, const char *prefix) -{ - GIT_UNUSED(self); - GIT_UNUSED(prefix); - /* find last item before prefix */ - return -1; -} - -static int index_iterator__reset( - git_iterator *self, const char *start, const char *end) -{ - index_iterator *ii = (index_iterator *)self; - if (iterator__reset_range(self, start, end) < 0) - return -1; - ii->current = ii->base.start ? - git_index__prefix_position(ii->index, ii->base.start) : 0; - index_iterator__skip_conflicts(ii); - return 0; -} - -static void index_iterator__free(git_iterator *self) -{ - index_iterator *ii = (index_iterator *)self; - git_index_free(ii->index); - ii->index = NULL; -} - -int git_iterator_for_index_range( - git_iterator **iter, - git_index *index, - git_iterator_flag_t flags, - const char *start, - const char *end) -{ - index_iterator *ii; - - GIT_UNUSED(flags); - - ITERATOR_BASE_INIT(ii, index, INDEX); - - ii->base.repo = git_index_owner(index); - if (index->ignore_case) { - ii->base.flags |= GIT_ITERATOR_IGNORE_CASE; - ii->base.prefixcomp = git__prefixcmp_icase; - } - ii->index = index; - GIT_REFCOUNT_INC(index); - - index_iterator__reset((git_iterator *)ii, NULL, NULL); - - *iter = (git_iterator *)ii; - - return 0; -} - - -typedef struct workdir_iterator_frame workdir_iterator_frame; -struct workdir_iterator_frame { - workdir_iterator_frame *next; - git_vector entries; - size_t index; -}; - -typedef struct { - git_iterator base; - git_iterator_callbacks cb; - workdir_iterator_frame *stack; - int (*entrycmp)(const void *pfx, const void *item); - git_ignores ignores; - git_index_entry entry; - git_buf path; - size_t root_len; - int is_ignored; -} workdir_iterator; - -GIT_INLINE(bool) path_is_dotgit(const git_path_with_stat *ps) -{ - if (!ps) - return false; - else { - const char *path = ps->path; - size_t len = ps->path_len; - - if (len < 4) - return false; - if (path[len - 1] == '/') - len--; - if (tolower(path[len - 1]) != 't' || - tolower(path[len - 2]) != 'i' || - tolower(path[len - 3]) != 'g' || - tolower(path[len - 4]) != '.') - return false; - return (len == 4 || path[len - 5] == '/'); - } -} - -static workdir_iterator_frame *workdir_iterator__alloc_frame( - workdir_iterator *wi) -{ - workdir_iterator_frame *wf = git__calloc(1, sizeof(workdir_iterator_frame)); - git_vector_cmp entry_compare = CASESELECT( - (wi->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0, - git_path_with_stat_cmp_icase, git_path_with_stat_cmp); - - if (wf == NULL) - return NULL; - - if (git_vector_init(&wf->entries, 0, entry_compare) != 0) { - git__free(wf); - return NULL; - } - - return wf; -} - -static void workdir_iterator__free_frame(workdir_iterator_frame *wf) -{ - unsigned int i; - git_path_with_stat *path; - - git_vector_foreach(&wf->entries, i, path) - git__free(path); - git_vector_free(&wf->entries); - git__free(wf); -} - -static int workdir_iterator__update_entry(workdir_iterator *wi); - -static int workdir_iterator__entry_cmp_case(const void *pfx, const void *item) -{ - const git_path_with_stat *ps = item; - return git__prefixcmp((const char *)pfx, ps->path); -} - -static int workdir_iterator__entry_cmp_icase(const void *pfx, const void *item) -{ - const git_path_with_stat *ps = item; - return git__prefixcmp_icase((const char *)pfx, ps->path); -} - -static void workdir_iterator__seek_frame_start( - workdir_iterator *wi, workdir_iterator_frame *wf) -{ - if (!wf) - return; - - if (wi->base.start) - git_vector_bsearch2( - &wf->index, &wf->entries, wi->entrycmp, wi->base.start); - else - wf->index = 0; - - if (path_is_dotgit(git_vector_get(&wf->entries, wf->index))) - wf->index++; -} - -static int workdir_iterator__expand_dir(workdir_iterator *wi) -{ - int error; - workdir_iterator_frame *wf = workdir_iterator__alloc_frame(wi); - GITERR_CHECK_ALLOC(wf); - - error = git_path_dirload_with_stat( - wi->path.ptr, wi->root_len, - (wi->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0, - wi->base.start, wi->base.end, &wf->entries); - - if (error < 0 || wf->entries.length == 0) { - workdir_iterator__free_frame(wf); - return GIT_ENOTFOUND; - } - - workdir_iterator__seek_frame_start(wi, wf); - - /* only push new ignores if this is not top level directory */ - if (wi->stack != NULL) { - ssize_t slash_pos = git_buf_rfind_next(&wi->path, '/'); - (void)git_ignore__push_dir(&wi->ignores, &wi->path.ptr[slash_pos + 1]); - } - - wf->next = wi->stack; - wi->stack = wf; - - return workdir_iterator__update_entry(wi); -} - -static int workdir_iterator__current( - git_iterator *self, const git_index_entry **entry) -{ - workdir_iterator *wi = (workdir_iterator *)self; - *entry = (wi->entry.path == NULL) ? NULL : &wi->entry; - return 0; -} - -static int workdir_iterator__at_end(git_iterator *self) -{ - return (((workdir_iterator *)self)->entry.path == NULL); -} - -static int workdir_iterator__advance( - git_iterator *self, const git_index_entry **entry) -{ - int error; - workdir_iterator *wi = (workdir_iterator *)self; - workdir_iterator_frame *wf; - git_path_with_stat *next; - - if (entry != NULL) - *entry = NULL; - - if (wi->entry.path == NULL) - return 0; - - while (1) { - wf = wi->stack; - next = git_vector_get(&wf->entries, ++wf->index); - - if (next != NULL) { - /* match git's behavior of ignoring anything named ".git" */ - if (path_is_dotgit(next)) - continue; - /* else found a good entry */ - break; - } - - /* pop stack if anything is left to pop */ - if (!wf->next) { - memset(&wi->entry, 0, sizeof(wi->entry)); - return 0; - } - - wi->stack = wf->next; - workdir_iterator__free_frame(wf); - git_ignore__pop_dir(&wi->ignores); - } - - error = workdir_iterator__update_entry(wi); - - if (!error && entry != NULL) - error = workdir_iterator__current(self, entry); - - return error; -} - -static int workdir_iterator__seek(git_iterator *self, const char *prefix) -{ - GIT_UNUSED(self); - GIT_UNUSED(prefix); - /* pop stack until matching prefix */ - /* find prefix item in current frame */ - /* push subdirectories as deep as possible while matching */ - return 0; -} - -static int workdir_iterator__reset( - git_iterator *self, const char *start, const char *end) -{ - workdir_iterator *wi = (workdir_iterator *)self; - - while (wi->stack != NULL && wi->stack->next != NULL) { - workdir_iterator_frame *wf = wi->stack; - wi->stack = wf->next; - workdir_iterator__free_frame(wf); - git_ignore__pop_dir(&wi->ignores); - } - - if (iterator__reset_range(self, start, end) < 0) - return -1; - - workdir_iterator__seek_frame_start(wi, wi->stack); - - return workdir_iterator__update_entry(wi); -} - -static void workdir_iterator__free(git_iterator *self) -{ - workdir_iterator *wi = (workdir_iterator *)self; - - while (wi->stack != NULL) { - workdir_iterator_frame *wf = wi->stack; - wi->stack = wf->next; - workdir_iterator__free_frame(wf); - } - - git_ignore__free(&wi->ignores); - git_buf_free(&wi->path); -} - -static int workdir_iterator__update_entry(workdir_iterator *wi) -{ - git_path_with_stat *ps = - git_vector_get(&wi->stack->entries, wi->stack->index); - - git_buf_truncate(&wi->path, wi->root_len); - memset(&wi->entry, 0, sizeof(wi->entry)); - - if (!ps) - return 0; - - if (git_buf_put(&wi->path, ps->path, ps->path_len) < 0) - return -1; - - if (wi->base.end && - wi->base.prefixcomp(wi->path.ptr + wi->root_len, wi->base.end) > 0) - return 0; - - wi->entry.path = ps->path; - - /* skip over .git entries */ - if (path_is_dotgit(ps)) - return workdir_iterator__advance((git_iterator *)wi, NULL); - - wi->is_ignored = -1; - - git_index_entry__init_from_stat(&wi->entry, &ps->st); - - /* need different mode here to keep directories during iteration */ - wi->entry.mode = git_futils_canonical_mode(ps->st.st_mode); - - /* if this is a file type we don't handle, treat as ignored */ - if (wi->entry.mode == 0) { - wi->is_ignored = 1; - return 0; - } - - /* detect submodules */ - if (S_ISDIR(wi->entry.mode)) { - int res = git_submodule_lookup(NULL, wi->base.repo, wi->entry.path); - bool is_submodule = (res == 0); - if (res == GIT_ENOTFOUND) - giterr_clear(); - - /* if submodule, mark as GITLINK and remove trailing slash */ - if (is_submodule) { - size_t len = strlen(wi->entry.path); - assert(wi->entry.path[len - 1] == '/'); - wi->entry.path[len - 1] = '\0'; - wi->entry.mode = S_IFGITLINK; - } - } - - return 0; -} - -int git_iterator_for_workdir_range( - git_iterator **iter, - git_repository *repo, - git_iterator_flag_t flags, - const char *start, - const char *end) -{ - int error; - workdir_iterator *wi; - - assert(iter && repo); - - if ((error = git_repository__ensure_not_bare( - repo, "scan working directory")) < 0) - return error; - - ITERATOR_BASE_INIT(wi, workdir, WORKDIR); - wi->base.repo = repo; - - if ((error = iterator_update_ignore_case((git_iterator *)wi, flags)) < 0) - goto fail; - - if (git_buf_sets(&wi->path, git_repository_workdir(repo)) < 0 || - git_path_to_dir(&wi->path) < 0 || - git_ignore__for_path(repo, "", &wi->ignores) < 0) - { - git__free(wi); - return -1; - } - - wi->root_len = wi->path.size; - wi->entrycmp = (wi->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0 ? - workdir_iterator__entry_cmp_icase : workdir_iterator__entry_cmp_case; - - if ((error = workdir_iterator__expand_dir(wi)) < 0) { - if (error != GIT_ENOTFOUND) - goto fail; - giterr_clear(); - } - - *iter = (git_iterator *)wi; - return 0; - -fail: - git_iterator_free((git_iterator *)wi); - return error; -} - - -typedef struct { - /* replacement callbacks */ - git_iterator_callbacks cb; - /* original iterator values */ - git_iterator_callbacks *orig; - git_iterator_type_t orig_type; - /* spoolandsort data */ - git_vector entries; - git_pool entry_pool; - git_pool string_pool; - size_t position; -} spoolandsort_callbacks; - -static int spoolandsort_iterator__current( - git_iterator *self, const git_index_entry **entry) -{ - spoolandsort_callbacks *scb = (spoolandsort_callbacks *)self->cb; - - *entry = (const git_index_entry *) - git_vector_get(&scb->entries, scb->position); - - return 0; -} - -static int spoolandsort_iterator__at_end(git_iterator *self) -{ - spoolandsort_callbacks *scb = (spoolandsort_callbacks *)self->cb; - - return 0 == scb->entries.length || scb->entries.length - 1 <= scb->position; -} - -static int spoolandsort_iterator__advance( - git_iterator *self, const git_index_entry **entry) -{ - spoolandsort_callbacks *scb = (spoolandsort_callbacks *)self->cb; - - *entry = (const git_index_entry *) - git_vector_get(&scb->entries, ++scb->position); - - return 0; -} - -static int spoolandsort_iterator__seek(git_iterator *self, const char *prefix) -{ - GIT_UNUSED(self); - GIT_UNUSED(prefix); - - return -1; -} - -static int spoolandsort_iterator__reset( - git_iterator *self, const char *start, const char *end) -{ - spoolandsort_callbacks *scb = (spoolandsort_callbacks *)self->cb; - - GIT_UNUSED(start); GIT_UNUSED(end); - - scb->position = 0; - - return 0; -} - -static void spoolandsort_iterator__free_callbacks(spoolandsort_callbacks *scb) -{ - git_pool_clear(&scb->string_pool); - git_pool_clear(&scb->entry_pool); - git_vector_free(&scb->entries); - git__free(scb); -} - -void git_iterator_spoolandsort_pop(git_iterator *self) -{ - spoolandsort_callbacks *scb = (spoolandsort_callbacks *)self->cb; - - if (self->type != GIT_ITERATOR_TYPE_SPOOLANDSORT) - return; - - self->cb = scb->orig; - self->type = scb->orig_type; - self->flags ^= GIT_ITERATOR_IGNORE_CASE; - - spoolandsort_iterator__free_callbacks(scb); -} - -static void spoolandsort_iterator__free(git_iterator *self) -{ - git_iterator_spoolandsort_pop(self); - self->cb->free(self); -} - -int git_iterator_spoolandsort_push(git_iterator *iter, bool ignore_case) -{ - const git_index_entry *item; - spoolandsort_callbacks *scb; - int (*entrycomp)(const void *a, const void *b); - - if (((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) == (ignore_case != 0)) - return 0; - - if (iter->type == GIT_ITERATOR_TYPE_EMPTY) { - iter->flags = (iter->flags ^ GIT_ITERATOR_IGNORE_CASE); - return 0; - } - - scb = git__calloc(1, sizeof(spoolandsort_callbacks)); - GITERR_CHECK_ALLOC(scb); - - ITERATOR_SET_CB(scb,spoolandsort); - - scb->orig = iter->cb; - scb->orig_type = iter->type; - scb->position = 0; - - entrycomp = ignore_case ? git_index_entry__cmp_icase : git_index_entry__cmp; - - if (git_vector_init(&scb->entries, 16, entrycomp) < 0 || - git_pool_init(&scb->entry_pool, sizeof(git_index_entry), 0) < 0 || - git_pool_init(&scb->string_pool, 1, 0) < 0 || - git_iterator_current(iter, &item) < 0) - goto fail; - - while (item) { - git_index_entry *clone = git_pool_malloc(&scb->entry_pool, 1); - if (!clone) - goto fail; - - memcpy(clone, item, sizeof(git_index_entry)); - - if (item->path) { - clone->path = git_pool_strdup(&scb->string_pool, item->path); - if (!clone->path) - goto fail; - } - - if (git_vector_insert(&scb->entries, clone) < 0) - goto fail; - - if (git_iterator_advance(iter, &item) < 0) - goto fail; - } - - git_vector_sort(&scb->entries); - - iter->cb = (git_iterator_callbacks *)scb; - iter->type = GIT_ITERATOR_TYPE_SPOOLANDSORT; - iter->flags ^= GIT_ITERATOR_IGNORE_CASE; - - return 0; - -fail: - spoolandsort_iterator__free_callbacks(scb); - return -1; -} - - -void git_iterator_free(git_iterator *iter) -{ - if (iter == NULL) - return; - - iter->cb->free(iter); - - git__free(iter->start); - git__free(iter->end); - - memset(iter, 0, sizeof(*iter)); - - git__free(iter); -} - -git_index *git_iterator_index_get_index(git_iterator *iter) -{ - if (iter->type == GIT_ITERATOR_TYPE_INDEX) - return ((index_iterator *)iter)->index; - - if (iter->type == GIT_ITERATOR_TYPE_SPOOLANDSORT && - ((spoolandsort_callbacks *)iter->cb)->orig_type == GIT_ITERATOR_TYPE_INDEX) - return ((index_iterator *)iter)->index; - - return NULL; -} - -git_iterator_type_t git_iterator_inner_type(git_iterator *iter) -{ - if (iter->type == GIT_ITERATOR_TYPE_SPOOLANDSORT) - return ((spoolandsort_callbacks *)iter->cb)->orig_type; - - return iter->type; -} - -int git_iterator_current_tree_entry( - git_iterator *iter, const git_tree_entry **tree_entry) -{ - *tree_entry = (iter->type != GIT_ITERATOR_TYPE_TREE) ? NULL : - tree_iterator__tree_entry((tree_iterator *)iter); - return 0; -} - -int git_iterator_current_parent_tree( - git_iterator *iter, - const char *parent_path, - const git_tree **tree_ptr) -{ - tree_iterator *ti = (tree_iterator *)iter; - tree_iterator_frame *tf; - const char *scan = parent_path; - int (*strncomp)(const char *a, const char *b, size_t sz); - - if (iter->type != GIT_ITERATOR_TYPE_TREE || ti->stack == NULL) - goto notfound; - - strncomp = ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) ? - git__strncasecmp : git__strncmp; - - for (tf = ti->tail; tf != NULL; tf = tf->prev) { - const git_tree_entry *te; - - if (!*scan) { - *tree_ptr = tf->tree; - return 0; - } - - te = git_tree_entry_byindex(tf->tree, - tf->icase_map ? (size_t)tf->icase_map[tf->index] : tf->index); - - if (strncomp(scan, te->filename, te->filename_len) != 0) - goto notfound; - - scan += te->filename_len; - - if (*scan) { - if (*scan != '/') - goto notfound; - scan++; - } - } - -notfound: - *tree_ptr = NULL; - return 0; -} - -int git_iterator_current_is_ignored(git_iterator *iter) -{ - workdir_iterator *wi = (workdir_iterator *)iter; - - if (iter->type != GIT_ITERATOR_TYPE_WORKDIR) - return 0; - - if (wi->is_ignored != -1) - return wi->is_ignored; - - if (git_ignore__lookup(&wi->ignores, wi->entry.path, &wi->is_ignored) < 0) - wi->is_ignored = 1; - - return wi->is_ignored; -} - -int git_iterator_advance_into_directory( - git_iterator *iter, const git_index_entry **entry) -{ - workdir_iterator *wi = (workdir_iterator *)iter; - - if (iter->type == GIT_ITERATOR_TYPE_WORKDIR && - wi->entry.path && - (wi->entry.mode == GIT_FILEMODE_TREE || - wi->entry.mode == GIT_FILEMODE_COMMIT)) - { - if (workdir_iterator__expand_dir(wi) < 0) - /* if error loading or if empty, skip the directory. */ - return workdir_iterator__advance(iter, entry); - } - - return entry ? git_iterator_current(iter, entry) : 0; -} - -int git_iterator_cmp(git_iterator *iter, const char *path_prefix) -{ - const git_index_entry *entry; - - /* a "done" iterator is after every prefix */ - if (git_iterator_current(iter, &entry) < 0 || - entry == NULL) - return 1; - - /* a NULL prefix is after any valid iterator */ - if (!path_prefix) - return -1; - - return iter->prefixcomp(entry->path, path_prefix); -} - -int git_iterator_current_workdir_path(git_iterator *iter, git_buf **path) -{ - workdir_iterator *wi = (workdir_iterator *)iter; - - if (iter->type != GIT_ITERATOR_TYPE_WORKDIR || !wi->entry.path) - *path = NULL; - else - *path = &wi->path; - - return 0; -} - diff --git a/src/iterator.h b/src/iterator.h deleted file mode 100644 index a9bccfca820..00000000000 --- a/src/iterator.h +++ /dev/null @@ -1,212 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_iterator_h__ -#define INCLUDE_iterator_h__ - -#include "common.h" -#include "git2/index.h" -#include "vector.h" -#include "buffer.h" - -#define ITERATOR_PREFIXCMP(ITER, STR, PREFIX) \ - (((ITER).flags & GIT_ITERATOR_IGNORE_CASE) != 0 ? \ - git__prefixcmp_icase((STR), (PREFIX)) : \ - git__prefixcmp((STR), (PREFIX))) - -typedef struct git_iterator git_iterator; - -typedef enum { - GIT_ITERATOR_TYPE_EMPTY = 0, - GIT_ITERATOR_TYPE_TREE = 1, - GIT_ITERATOR_TYPE_INDEX = 2, - GIT_ITERATOR_TYPE_WORKDIR = 3, - GIT_ITERATOR_TYPE_SPOOLANDSORT = 4 -} git_iterator_type_t; - -typedef enum { - GIT_ITERATOR_IGNORE_CASE = (1 << 0), /* ignore_case */ - GIT_ITERATOR_DONT_IGNORE_CASE = (1 << 1), /* force ignore_case off */ -} git_iterator_flag_t; - -typedef struct { - int (*current)(git_iterator *, const git_index_entry **); - int (*at_end)(git_iterator *); - int (*advance)(git_iterator *, const git_index_entry **); - int (*seek)(git_iterator *, const char *prefix); - int (*reset)(git_iterator *, const char *start, const char *end); - void (*free)(git_iterator *); -} git_iterator_callbacks; - -struct git_iterator { - git_iterator_type_t type; - git_iterator_callbacks *cb; - git_repository *repo; - char *start; - char *end; - int (*prefixcomp)(const char *str, const char *prefix); - unsigned int flags; -}; - -extern int git_iterator_for_nothing( - git_iterator **out, git_iterator_flag_t flags); - -/* tree iterators will match the ignore_case value from the index of the - * repository, unless you override with a non-zero flag value - */ -extern int git_iterator_for_tree_range( - git_iterator **out, - git_tree *tree, - git_iterator_flag_t flags, - const char *start, - const char *end); - -GIT_INLINE(int) git_iterator_for_tree(git_iterator **out, git_tree *tree) -{ - return git_iterator_for_tree_range(out, tree, 0, NULL, NULL); -} - -/* index iterators will take the ignore_case value from the index; the - * ignore_case flags are not used - */ -extern int git_iterator_for_index_range( - git_iterator **out, - git_index *index, - git_iterator_flag_t flags, - const char *start, - const char *end); - -GIT_INLINE(int) git_iterator_for_index(git_iterator **out, git_index *index) -{ - return git_iterator_for_index_range(out, index, 0, NULL, NULL); -} - -/* workdir iterators will match the ignore_case value from the index of the - * repository, unless you override with a non-zero flag value - */ -extern int git_iterator_for_workdir_range( - git_iterator **out, - git_repository *repo, - git_iterator_flag_t flags, - const char *start, - const char *end); - -GIT_INLINE(int) git_iterator_for_workdir(git_iterator **out, git_repository *repo) -{ - return git_iterator_for_workdir_range(out, repo, 0, NULL, NULL); -} - -extern void git_iterator_free(git_iterator *iter); - -/* Spool all iterator values, resort with alternative ignore_case value - * and replace callbacks with spoolandsort alternates. - */ -extern int git_iterator_spoolandsort_push(git_iterator *iter, bool ignore_case); - -/* Restore original callbacks - not required in most circumstances */ -extern void git_iterator_spoolandsort_pop(git_iterator *iter); - -/* Entry is not guaranteed to be fully populated. For a tree iterator, - * we will only populate the mode, oid and path, for example. For a workdir - * iterator, we will not populate the oid. - * - * You do not need to free the entry. It is still "owned" by the iterator. - * Once you call `git_iterator_advance`, then content of the old entry is - * no longer guaranteed to be valid. - */ -GIT_INLINE(int) git_iterator_current( - git_iterator *iter, const git_index_entry **entry) -{ - return iter->cb->current(iter, entry); -} - -GIT_INLINE(int) git_iterator_at_end(git_iterator *iter) -{ - return iter->cb->at_end(iter); -} - -GIT_INLINE(int) git_iterator_advance( - git_iterator *iter, const git_index_entry **entry) -{ - return iter->cb->advance(iter, entry); -} - -GIT_INLINE(int) git_iterator_seek( - git_iterator *iter, const char *prefix) -{ - return iter->cb->seek(iter, prefix); -} - -GIT_INLINE(int) git_iterator_reset( - git_iterator *iter, const char *start, const char *end) -{ - return iter->cb->reset(iter, start, end); -} - -GIT_INLINE(git_iterator_type_t) git_iterator_type(git_iterator *iter) -{ - return iter->type; -} - -GIT_INLINE(git_repository *) git_iterator_owner(git_iterator *iter) -{ - return iter->repo; -} - -GIT_INLINE(git_iterator_flag_t) git_iterator_flags(git_iterator *iter) -{ - return iter->flags; -} - -GIT_INLINE(bool) git_iterator_ignore_case(git_iterator *iter) -{ - return ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0); -} - -extern int git_iterator_current_tree_entry( - git_iterator *iter, const git_tree_entry **tree_entry); - -extern int git_iterator_current_parent_tree( - git_iterator *iter, const char *parent_path, const git_tree **tree_ptr); - -extern int git_iterator_current_is_ignored(git_iterator *iter); - -/** - * Iterate into a workdir directory. - * - * Workdir iterators do not automatically descend into directories (so that - * when comparing two iterator entries you can detect a newly created - * directory in the workdir). As a result, you may get S_ISDIR items from - * a workdir iterator. If you wish to iterate over the contents of the - * directories you encounter, then call this function when you encounter - * a directory. - * - * If there are no files in the directory, this will end up acting like a - * regular advance and will skip past the directory, so you should be - * prepared for that case. - * - * On non-workdir iterators or if not pointing at a directory, this is a - * no-op and will not advance the iterator. - */ -extern int git_iterator_advance_into_directory( - git_iterator *iter, const git_index_entry **entry); - -extern int git_iterator_cmp( - git_iterator *iter, const char *path_prefix); - -/** - * Get the full path of the current item from a workdir iterator. - * This will return NULL for a non-workdir iterator. - */ -extern int git_iterator_current_workdir_path( - git_iterator *iter, git_buf **path); - - -extern git_index *git_iterator_index_get_index(git_iterator *iter); - -extern git_iterator_type_t git_iterator_inner_type(git_iterator *iter); - -#endif diff --git a/src/khash.h b/src/khash.h deleted file mode 100644 index 242204464aa..00000000000 --- a/src/khash.h +++ /dev/null @@ -1,610 +0,0 @@ -/* The MIT License - - Copyright (c) 2008, 2009, 2011 by Attractive Chaos - - Permission is hereby granted, free of charge, to any person obtaining - a copy of this software and associated documentation files (the - "Software"), to deal in the Software without restriction, including - without limitation the rights to use, copy, modify, merge, publish, - distribute, sublicense, and/or sell copies of the Software, and to - permit persons to whom the Software is furnished to do so, subject to - the following conditions: - - The above copyright notice and this permission notice shall be - included in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ - -/* - An example: - -#include "khash.h" -KHASH_MAP_INIT_INT(32, char) -int main() { - int ret, is_missing; - khiter_t k; - khash_t(32) *h = kh_init(32); - k = kh_put(32, h, 5, &ret); - kh_value(h, k) = 10; - k = kh_get(32, h, 10); - is_missing = (k == kh_end(h)); - k = kh_get(32, h, 5); - kh_del(32, h, k); - for (k = kh_begin(h); k != kh_end(h); ++k) - if (kh_exist(h, k)) kh_value(h, k) = 1; - kh_destroy(32, h); - return 0; -} -*/ - -/* - 2011-12-29 (0.2.7): - - * Minor code clean up; no actual effect. - - 2011-09-16 (0.2.6): - - * The capacity is a power of 2. This seems to dramatically improve the - speed for simple keys. Thank Zilong Tan for the suggestion. Reference: - - - http://code.google.com/p/ulib/ - - http://nothings.org/computer/judy/ - - * Allow to optionally use linear probing which usually has better - performance for random input. Double hashing is still the default as it - is more robust to certain non-random input. - - * Added Wang's integer hash function (not used by default). This hash - function is more robust to certain non-random input. - - 2011-02-14 (0.2.5): - - * Allow to declare global functions. - - 2009-09-26 (0.2.4): - - * Improve portability - - 2008-09-19 (0.2.3): - - * Corrected the example - * Improved interfaces - - 2008-09-11 (0.2.2): - - * Improved speed a little in kh_put() - - 2008-09-10 (0.2.1): - - * Added kh_clear() - * Fixed a compiling error - - 2008-09-02 (0.2.0): - - * Changed to token concatenation which increases flexibility. - - 2008-08-31 (0.1.2): - - * Fixed a bug in kh_get(), which has not been tested previously. - - 2008-08-31 (0.1.1): - - * Added destructor -*/ - - -#ifndef __AC_KHASH_H -#define __AC_KHASH_H - -/*! - @header - - Generic hash table library. - */ - -#define AC_VERSION_KHASH_H "0.2.6" - -#include -#include -#include - -/* compipler specific configuration */ - -#if UINT_MAX == 0xffffffffu -typedef unsigned int khint32_t; -#elif ULONG_MAX == 0xffffffffu -typedef unsigned long khint32_t; -#endif - -#if ULONG_MAX == ULLONG_MAX -typedef unsigned long khint64_t; -#else -typedef unsigned long long khint64_t; -#endif - -#ifdef _MSC_VER -#define kh_inline __inline -#else -#define kh_inline inline -#endif - -typedef khint32_t khint_t; -typedef khint_t khiter_t; - -#define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2) -#define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1) -#define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3) -#define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1))) -#define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1))) -#define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1))) -#define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1)) - -#ifdef KHASH_LINEAR -#define __ac_inc(k, m) 1 -#else -#define __ac_inc(k, m) (((k)>>3 ^ (k)<<3) | 1) & (m) -#endif - -#define __ac_fsize(m) ((m) < 16? 1 : (m)>>4) - -#ifndef kroundup32 -#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) -#endif - -#ifndef kcalloc -#define kcalloc(N,Z) calloc(N,Z) -#endif -#ifndef kmalloc -#define kmalloc(Z) malloc(Z) -#endif -#ifndef krealloc -#define krealloc(P,Z) realloc(P,Z) -#endif -#ifndef kfree -#define kfree(P) free(P) -#endif - -static const double __ac_HASH_UPPER = 0.77; - -#define __KHASH_TYPE(name, khkey_t, khval_t) \ - typedef struct { \ - khint_t n_buckets, size, n_occupied, upper_bound; \ - khint32_t *flags; \ - khkey_t *keys; \ - khval_t *vals; \ - } kh_##name##_t; - -#define __KHASH_PROTOTYPES(name, khkey_t, khval_t) \ - extern kh_##name##_t *kh_init_##name(void); \ - extern void kh_destroy_##name(kh_##name##_t *h); \ - extern void kh_clear_##name(kh_##name##_t *h); \ - extern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \ - extern int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \ - extern khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \ - extern void kh_del_##name(kh_##name##_t *h, khint_t x); - -#define __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \ - SCOPE kh_##name##_t *kh_init_##name(void) { \ - return (kh_##name##_t*)kcalloc(1, sizeof(kh_##name##_t)); \ - } \ - SCOPE void kh_destroy_##name(kh_##name##_t *h) \ - { \ - if (h) { \ - kfree((void *)h->keys); kfree(h->flags); \ - kfree((void *)h->vals); \ - kfree(h); \ - } \ - } \ - SCOPE void kh_clear_##name(kh_##name##_t *h) \ - { \ - if (h && h->flags) { \ - memset(h->flags, 0xaa, __ac_fsize(h->n_buckets) * sizeof(khint32_t)); \ - h->size = h->n_occupied = 0; \ - } \ - } \ - SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \ - { \ - if (h->n_buckets) { \ - khint_t inc, k, i, last, mask; \ - mask = h->n_buckets - 1; \ - k = __hash_func(key); i = k & mask; \ - inc = __ac_inc(k, mask); last = i; /* inc==1 for linear probing */ \ - while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \ - i = (i + inc) & mask; \ - if (i == last) return h->n_buckets; \ - } \ - return __ac_iseither(h->flags, i)? h->n_buckets : i; \ - } else return 0; \ - } \ - SCOPE int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \ - { /* This function uses 0.25*n_bucktes bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \ - khint32_t *new_flags = 0; \ - khint_t j = 1; \ - { \ - kroundup32(new_n_buckets); \ - if (new_n_buckets < 4) new_n_buckets = 4; \ - if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \ - else { /* hash table size to be changed (shrink or expand); rehash */ \ - new_flags = (khint32_t*)kmalloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t)); \ - if (!new_flags) return -1; \ - memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \ - if (h->n_buckets < new_n_buckets) { /* expand */ \ - khkey_t *new_keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \ - if (!new_keys) return -1; \ - h->keys = new_keys; \ - if (kh_is_map) { \ - khval_t *new_vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \ - if (!new_vals) return -1; \ - h->vals = new_vals; \ - } \ - } /* otherwise shrink */ \ - } \ - } \ - if (j) { /* rehashing is needed */ \ - for (j = 0; j != h->n_buckets; ++j) { \ - if (__ac_iseither(h->flags, j) == 0) { \ - khkey_t key = h->keys[j]; \ - khval_t val; \ - khint_t new_mask; \ - new_mask = new_n_buckets - 1; \ - if (kh_is_map) val = h->vals[j]; \ - __ac_set_isdel_true(h->flags, j); \ - while (1) { /* kick-out process; sort of like in Cuckoo hashing */ \ - khint_t inc, k, i; \ - k = __hash_func(key); \ - i = k & new_mask; \ - inc = __ac_inc(k, new_mask); \ - while (!__ac_isempty(new_flags, i)) i = (i + inc) & new_mask; \ - __ac_set_isempty_false(new_flags, i); \ - if (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { /* kick out the existing element */ \ - { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \ - if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \ - __ac_set_isdel_true(h->flags, i); /* mark it as deleted in the old hash table */ \ - } else { /* write the element and jump out of the loop */ \ - h->keys[i] = key; \ - if (kh_is_map) h->vals[i] = val; \ - break; \ - } \ - } \ - } \ - } \ - if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \ - h->keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \ - if (kh_is_map) h->vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \ - } \ - kfree(h->flags); /* free the working space */ \ - h->flags = new_flags; \ - h->n_buckets = new_n_buckets; \ - h->n_occupied = h->size; \ - h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \ - } \ - return 0; \ - } \ - SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \ - { \ - khint_t x; \ - if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \ - if (h->n_buckets > (h->size<<1)) { \ - if (kh_resize_##name(h, h->n_buckets - 1) < 0) { /* clear "deleted" elements */ \ - *ret = -1; return h->n_buckets; \ - } \ - } else if (kh_resize_##name(h, h->n_buckets + 1) < 0) { /* expand the hash table */ \ - *ret = -1; return h->n_buckets; \ - } \ - } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \ - { \ - khint_t inc, k, i, site, last, mask = h->n_buckets - 1; \ - x = site = h->n_buckets; k = __hash_func(key); i = k & mask; \ - if (__ac_isempty(h->flags, i)) x = i; /* for speed up */ \ - else { \ - inc = __ac_inc(k, mask); last = i; \ - while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \ - if (__ac_isdel(h->flags, i)) site = i; \ - i = (i + inc) & mask; \ - if (i == last) { x = site; break; } \ - } \ - if (x == h->n_buckets) { \ - if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \ - else x = i; \ - } \ - } \ - } \ - if (__ac_isempty(h->flags, x)) { /* not present at all */ \ - h->keys[x] = key; \ - __ac_set_isboth_false(h->flags, x); \ - ++h->size; ++h->n_occupied; \ - *ret = 1; \ - } else if (__ac_isdel(h->flags, x)) { /* deleted */ \ - h->keys[x] = key; \ - __ac_set_isboth_false(h->flags, x); \ - ++h->size; \ - *ret = 2; \ - } else *ret = 0; /* Don't touch h->keys[x] if present and not deleted */ \ - return x; \ - } \ - SCOPE void kh_del_##name(kh_##name##_t *h, khint_t x) \ - { \ - if (x != h->n_buckets && !__ac_iseither(h->flags, x)) { \ - __ac_set_isdel_true(h->flags, x); \ - --h->size; \ - } \ - } - -#define KHASH_DECLARE(name, khkey_t, khval_t) \ - __KHASH_TYPE(name, khkey_t, khval_t) \ - __KHASH_PROTOTYPES(name, khkey_t, khval_t) - -#define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \ - __KHASH_TYPE(name, khkey_t, khval_t) \ - __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) - -#define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \ - KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) - -/* --- BEGIN OF HASH FUNCTIONS --- */ - -/*! @function - @abstract Integer hash function - @param key The integer [khint32_t] - @return The hash value [khint_t] - */ -#define kh_int_hash_func(key) (khint32_t)(key) -/*! @function - @abstract Integer comparison function - */ -#define kh_int_hash_equal(a, b) ((a) == (b)) -/*! @function - @abstract 64-bit integer hash function - @param key The integer [khint64_t] - @return The hash value [khint_t] - */ -#define kh_int64_hash_func(key) (khint32_t)((key)>>33^(key)^(key)<<11) -/*! @function - @abstract 64-bit integer comparison function - */ -#define kh_int64_hash_equal(a, b) ((a) == (b)) -/*! @function - @abstract const char* hash function - @param s Pointer to a null terminated string - @return The hash value - */ -static kh_inline khint_t __ac_X31_hash_string(const char *s) -{ - khint_t h = (khint_t)*s; - if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s; - return h; -} -/*! @function - @abstract Another interface to const char* hash function - @param key Pointer to a null terminated string [const char*] - @return The hash value [khint_t] - */ -#define kh_str_hash_func(key) __ac_X31_hash_string(key) -/*! @function - @abstract Const char* comparison function - */ -#define kh_str_hash_equal(a, b) (strcmp(a, b) == 0) - -static kh_inline khint_t __ac_Wang_hash(khint_t key) -{ - key += ~(key << 15); - key ^= (key >> 10); - key += (key << 3); - key ^= (key >> 6); - key += ~(key << 11); - key ^= (key >> 16); - return key; -} -#define kh_int_hash_func2(k) __ac_Wang_hash((khint_t)key) - -/* --- END OF HASH FUNCTIONS --- */ - -/* Other convenient macros... */ - -/*! - @abstract Type of the hash table. - @param name Name of the hash table [symbol] - */ -#define khash_t(name) kh_##name##_t - -/*! @function - @abstract Initiate a hash table. - @param name Name of the hash table [symbol] - @return Pointer to the hash table [khash_t(name)*] - */ -#define kh_init(name) kh_init_##name() - -/*! @function - @abstract Destroy a hash table. - @param name Name of the hash table [symbol] - @param h Pointer to the hash table [khash_t(name)*] - */ -#define kh_destroy(name, h) kh_destroy_##name(h) - -/*! @function - @abstract Reset a hash table without deallocating memory. - @param name Name of the hash table [symbol] - @param h Pointer to the hash table [khash_t(name)*] - */ -#define kh_clear(name, h) kh_clear_##name(h) - -/*! @function - @abstract Resize a hash table. - @param name Name of the hash table [symbol] - @param h Pointer to the hash table [khash_t(name)*] - @param s New size [khint_t] - */ -#define kh_resize(name, h, s) kh_resize_##name(h, s) - -/*! @function - @abstract Insert a key to the hash table. - @param name Name of the hash table [symbol] - @param h Pointer to the hash table [khash_t(name)*] - @param k Key [type of keys] - @param r Extra return code: 0 if the key is present in the hash table; - 1 if the bucket is empty (never used); 2 if the element in - the bucket has been deleted [int*] - @return Iterator to the inserted element [khint_t] - */ -#define kh_put(name, h, k, r) kh_put_##name(h, k, r) - -/*! @function - @abstract Retrieve a key from the hash table. - @param name Name of the hash table [symbol] - @param h Pointer to the hash table [khash_t(name)*] - @param k Key [type of keys] - @return Iterator to the found element, or kh_end(h) is the element is absent [khint_t] - */ -#define kh_get(name, h, k) kh_get_##name(h, k) - -/*! @function - @abstract Remove a key from the hash table. - @param name Name of the hash table [symbol] - @param h Pointer to the hash table [khash_t(name)*] - @param k Iterator to the element to be deleted [khint_t] - */ -#define kh_del(name, h, k) kh_del_##name(h, k) - -/*! @function - @abstract Test whether a bucket contains data. - @param h Pointer to the hash table [khash_t(name)*] - @param x Iterator to the bucket [khint_t] - @return 1 if containing data; 0 otherwise [int] - */ -#define kh_exist(h, x) (!__ac_iseither((h)->flags, (x))) - -/*! @function - @abstract Get key given an iterator - @param h Pointer to the hash table [khash_t(name)*] - @param x Iterator to the bucket [khint_t] - @return Key [type of keys] - */ -#define kh_key(h, x) ((h)->keys[x]) - -/*! @function - @abstract Get value given an iterator - @param h Pointer to the hash table [khash_t(name)*] - @param x Iterator to the bucket [khint_t] - @return Value [type of values] - @discussion For hash sets, calling this results in segfault. - */ -#define kh_val(h, x) ((h)->vals[x]) - -/*! @function - @abstract Alias of kh_val() - */ -#define kh_value(h, x) ((h)->vals[x]) - -/*! @function - @abstract Get the start iterator - @param h Pointer to the hash table [khash_t(name)*] - @return The start iterator [khint_t] - */ -#define kh_begin(h) (khint_t)(0) - -/*! @function - @abstract Get the end iterator - @param h Pointer to the hash table [khash_t(name)*] - @return The end iterator [khint_t] - */ -#define kh_end(h) ((h)->n_buckets) - -/*! @function - @abstract Get the number of elements in the hash table - @param h Pointer to the hash table [khash_t(name)*] - @return Number of elements in the hash table [khint_t] - */ -#define kh_size(h) ((h)->size) - -/*! @function - @abstract Get the number of buckets in the hash table - @param h Pointer to the hash table [khash_t(name)*] - @return Number of buckets in the hash table [khint_t] - */ -#define kh_n_buckets(h) ((h)->n_buckets) - -/*! @function - @abstract Iterate over the entries in the hash table - @param h Pointer to the hash table [khash_t(name)*] - @param kvar Variable to which key will be assigned - @param vvar Variable to which value will be assigned - @param code Block of code to execute - */ -#define kh_foreach(h, kvar, vvar, code) { khint_t __i; \ - for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \ - if (!kh_exist(h,__i)) continue; \ - (kvar) = kh_key(h,__i); \ - (vvar) = kh_val(h,__i); \ - code; \ - } } - -/*! @function - @abstract Iterate over the values in the hash table - @param h Pointer to the hash table [khash_t(name)*] - @param vvar Variable to which value will be assigned - @param code Block of code to execute - */ -#define kh_foreach_value(h, vvar, code) { khint_t __i; \ - for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \ - if (!kh_exist(h,__i)) continue; \ - (vvar) = kh_val(h,__i); \ - code; \ - } } - -/* More conenient interfaces */ - -/*! @function - @abstract Instantiate a hash set containing integer keys - @param name Name of the hash table [symbol] - */ -#define KHASH_SET_INIT_INT(name) \ - KHASH_INIT(name, khint32_t, char, 0, kh_int_hash_func, kh_int_hash_equal) - -/*! @function - @abstract Instantiate a hash map containing integer keys - @param name Name of the hash table [symbol] - @param khval_t Type of values [type] - */ -#define KHASH_MAP_INIT_INT(name, khval_t) \ - KHASH_INIT(name, khint32_t, khval_t, 1, kh_int_hash_func, kh_int_hash_equal) - -/*! @function - @abstract Instantiate a hash map containing 64-bit integer keys - @param name Name of the hash table [symbol] - */ -#define KHASH_SET_INIT_INT64(name) \ - KHASH_INIT(name, khint64_t, char, 0, kh_int64_hash_func, kh_int64_hash_equal) - -/*! @function - @abstract Instantiate a hash map containing 64-bit integer keys - @param name Name of the hash table [symbol] - @param khval_t Type of values [type] - */ -#define KHASH_MAP_INIT_INT64(name, khval_t) \ - KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal) - -typedef const char *kh_cstr_t; -/*! @function - @abstract Instantiate a hash map containing const char* keys - @param name Name of the hash table [symbol] - */ -#define KHASH_SET_INIT_STR(name) \ - KHASH_INIT(name, kh_cstr_t, char, 0, kh_str_hash_func, kh_str_hash_equal) - -/*! @function - @abstract Instantiate a hash map containing const char* keys - @param name Name of the hash table [symbol] - @param khval_t Type of values [type] - */ -#define KHASH_MAP_INIT_STR(name, khval_t) \ - KHASH_INIT(name, kh_cstr_t, khval_t, 1, kh_str_hash_func, kh_str_hash_equal) - -#endif /* __AC_KHASH_H */ diff --git a/src/libgit2/CMakeLists.txt b/src/libgit2/CMakeLists.txt new file mode 100644 index 00000000000..195be80022e --- /dev/null +++ b/src/libgit2/CMakeLists.txt @@ -0,0 +1,140 @@ +# libgit2: the shared library: this CMakeLists.txt compiles the core +# git library functionality. + +add_library(libgit2 OBJECT) + +include(PkgBuildConfig) +include(CMakePackageConfigHelpers) + +set(LIBGIT2_INCLUDES + "${PROJECT_BINARY_DIR}/src/util" + "${PROJECT_BINARY_DIR}/include" + "${PROJECT_SOURCE_DIR}/src/libgit2" + "${PROJECT_SOURCE_DIR}/src/util" + "${PROJECT_SOURCE_DIR}/include") + +# Collect sourcefiles +file(GLOB SRC_H + "${PROJECT_SOURCE_DIR}/include/git2.h" + "${PROJECT_SOURCE_DIR}/include/git2/*.h" + "${PROJECT_SOURCE_DIR}/include/git2/sys/*.h") +list(SORT SRC_H) +target_sources(libgit2 PRIVATE ${SRC_H}) + +file(GLOB SRC_GIT2 *.c *.h + streams/*.c streams/*.h + transports/*.c transports/*.h) +list(SORT SRC_GIT2) +target_sources(libgit2 PRIVATE ${SRC_GIT2}) + +if(WIN32 AND NOT CYGWIN) + # Add resource information on Windows + set(SRC_RC "git2.rc") +endif() + +if(APPLE) + # The old Secure Transport API has been deprecated in macOS 10.15. + set_source_files_properties(streams/stransport.c PROPERTIES COMPILE_FLAGS -Wno-deprecated) +endif() + +ide_split_sources(libgit2) +list(APPEND LIBGIT2_OBJECTS $ $ ${LIBGIT2_DEPENDENCY_OBJECTS}) +list(APPEND LIBGIT2_INCLUDES ${LIBGIT2_DEPENDENCY_INCLUDES}) + +target_include_directories(libgit2 PRIVATE ${LIBGIT2_INCLUDES} ${LIBGIT2_DEPENDENCY_INCLUDES} PUBLIC ${PROJECT_SOURCE_DIR}/include) +target_include_directories(libgit2 SYSTEM PRIVATE ${LIBGIT2_SYSTEM_INCLUDES}) + +set(LIBGIT2_INCLUDES ${LIBGIT2_INCLUDES} PARENT_SCOPE) +set(LIBGIT2_OBJECTS ${LIBGIT2_OBJECTS} PARENT_SCOPE) +set(LIBGIT2_DEPENDENCY_INCLUDES ${LIBGIT2_DEPENDENCY_INCLUDES} PARENT_SCOPE) +set(LIBGIT2_DEPENDENCY_OBJECTS ${LIBGIT2_DEPENDENCY_OBJECTS} PARENT_SCOPE) +set(LIBGIT2_SYSTEM_INCLUDES ${LIBGIT2_SYSTEM_INCLUDES} PARENT_SCOPE) +set(LIBGIT2_SYSTEM_LIBS ${LIBGIT2_SYSTEM_LIBS} PARENT_SCOPE) + +# +# Compile and link libgit2 +# + +add_library(libgit2package ${SRC_RC} ${LIBGIT2_OBJECTS}) +target_link_libraries(libgit2package ${LIBGIT2_SYSTEM_LIBS}) +target_include_directories(libgit2package SYSTEM PRIVATE ${LIBGIT2_INCLUDES}) +target_include_directories(libgit2package INTERFACE $) + +set_target_properties(libgit2package PROPERTIES C_STANDARD 90) +set_target_properties(libgit2package PROPERTIES C_EXTENSIONS OFF) +set_target_properties(libgit2package PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) +set_target_properties(libgit2package PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) +set_target_properties(libgit2package PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) + +ide_split_sources(libgit2package) + +if(SONAME) + set_target_properties(libgit2package PROPERTIES VERSION ${libgit2_VERSION}) + set_target_properties(libgit2package PROPERTIES SOVERSION "${libgit2_VERSION_MAJOR}.${libgit2_VERSION_MINOR}") + if(LIBGIT2_FILENAME) + target_compile_definitions(libgit2package PRIVATE LIBGIT2_FILENAME=\"${LIBGIT2_FILENAME}\") + set_target_properties(libgit2package PROPERTIES OUTPUT_NAME ${LIBGIT2_FILENAME}) + elseif(DEFINED LIBGIT2_PREFIX) + set_target_properties(libgit2package PROPERTIES PREFIX "${LIBGIT2_PREFIX}") + endif() +endif() + +pkg_build_config(NAME "lib${LIBGIT2_FILENAME}" + VERSION ${libgit2_VERSION} + DESCRIPTION "The git library, take 2" + LIBS_SELF ${LIBGIT2_FILENAME} + PRIVATE_LIBS ${LIBGIT2_PC_LIBS} + REQUIRES ${LIBGIT2_PC_REQUIRES}) + +if(MSVC_IDE) + # Precompiled headers + set_target_properties(libgit2package PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") + set_source_files_properties(win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h") +endif() + +# support experimental features and functionality + +configure_file(experimental.h.in "${PROJECT_BINARY_DIR}/include/git2/experimental.h") + +# translate filenames in the git2.h so that they match the install directory +# (allows for side-by-side installs of libgit2 and libgit2-experimental.) + +FILE(READ "${PROJECT_SOURCE_DIR}/include/git2.h" LIBGIT2_INCLUDE) +STRING(REGEX REPLACE "#include \"git2\/" "#include \"${LIBGIT2_FILENAME}/" LIBGIT2_INCLUDE "${LIBGIT2_INCLUDE}") +FILE(WRITE "${PROJECT_BINARY_DIR}/include/${LIBGIT2_FILENAME}.h" ${LIBGIT2_INCLUDE}) + +# cmake package targets + +set(LIBGIT2_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets") + +write_basic_package_version_file( + "${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}ConfigVersion.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMajorVersion) + +configure_file(config.cmake.in + "${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}Config.cmake" + @ONLY) + +install(FILES + "${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}Config.cmake" + "${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}ConfigVersion.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") +install( + EXPORT ${LIBGIT2_TARGETS_EXPORT_NAME} + NAMESPACE "${PROJECT_NAME}::" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") + +# Install + +install(TARGETS libgit2package + EXPORT ${LIBGIT2_TARGETS_EXPORT_NAME} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) +install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/git2/ + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${LIBGIT2_FILENAME}") +install(FILES ${PROJECT_BINARY_DIR}/include/git2/experimental.h + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${LIBGIT2_FILENAME}") +install(FILES "${PROJECT_BINARY_DIR}/include/${LIBGIT2_FILENAME}.h" + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) diff --git a/src/libgit2/annotated_commit.c b/src/libgit2/annotated_commit.c new file mode 100644 index 00000000000..c5c8ace789d --- /dev/null +++ b/src/libgit2/annotated_commit.c @@ -0,0 +1,240 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "annotated_commit.h" + +#include "refs.h" +#include "cache.h" + +#include "git2/commit.h" +#include "git2/refs.h" +#include "git2/repository.h" +#include "git2/annotated_commit.h" +#include "git2/revparse.h" +#include "git2/tree.h" +#include "git2/index.h" + +static int annotated_commit_init( + git_annotated_commit **out, + git_commit *commit, + const char *description) +{ + git_annotated_commit *annotated_commit; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(commit); + + *out = NULL; + + annotated_commit = git__calloc(1, sizeof(git_annotated_commit)); + GIT_ERROR_CHECK_ALLOC(annotated_commit); + + annotated_commit->type = GIT_ANNOTATED_COMMIT_REAL; + + if ((error = git_commit_dup(&annotated_commit->commit, commit)) < 0) + goto done; + + git_oid_tostr(annotated_commit->id_str, GIT_OID_MAX_HEXSIZE + 1, + git_commit_id(commit)); + + if (!description) + description = annotated_commit->id_str; + + annotated_commit->description = git__strdup(description); + GIT_ERROR_CHECK_ALLOC(annotated_commit->description); + +done: + if (!error) + *out = annotated_commit; + + return error; +} + +static int annotated_commit_init_from_id( + git_annotated_commit **out, + git_repository *repo, + const git_oid *id, + const char *description) +{ + git_commit *commit = NULL; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(id); + + *out = NULL; + + if ((error = git_commit_lookup(&commit, repo, id)) < 0) + goto done; + + error = annotated_commit_init(out, commit, description); + +done: + git_commit_free(commit); + return error; +} + +int git_annotated_commit_lookup( + git_annotated_commit **out, + git_repository *repo, + const git_oid *id) +{ + return annotated_commit_init_from_id(out, repo, id, NULL); +} + +int git_annotated_commit_from_commit( + git_annotated_commit **out, + git_commit *commit) +{ + return annotated_commit_init(out, commit, NULL); +} + +int git_annotated_commit_from_revspec( + git_annotated_commit **out, + git_repository *repo, + const char *revspec) +{ + git_object *obj, *commit; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(revspec); + + if ((error = git_revparse_single(&obj, repo, revspec)) < 0) + return error; + + if ((error = git_object_peel(&commit, obj, GIT_OBJECT_COMMIT))) { + git_object_free(obj); + return error; + } + + error = annotated_commit_init(out, (git_commit *)commit, revspec); + + git_object_free(obj); + git_object_free(commit); + + return error; +} + +int git_annotated_commit_from_ref( + git_annotated_commit **out, + git_repository *repo, + const git_reference *ref) +{ + git_object *peeled; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(ref); + + *out = NULL; + + if ((error = git_reference_peel(&peeled, ref, GIT_OBJECT_COMMIT)) < 0) + return error; + + error = annotated_commit_init_from_id(out, + repo, + git_object_id(peeled), + git_reference_name(ref)); + + if (!error) { + (*out)->ref_name = git__strdup(git_reference_name(ref)); + GIT_ERROR_CHECK_ALLOC((*out)->ref_name); + } + + git_object_free(peeled); + return error; +} + +int git_annotated_commit_from_head( + git_annotated_commit **out, + git_repository *repo) +{ + git_reference *head; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + *out = NULL; + + if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0) + return -1; + + error = git_annotated_commit_from_ref(out, repo, head); + + git_reference_free(head); + return error; +} + +int git_annotated_commit_from_fetchhead( + git_annotated_commit **out, + git_repository *repo, + const char *branch_name, + const char *remote_url, + const git_oid *id) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(branch_name); + GIT_ASSERT_ARG(remote_url); + GIT_ASSERT_ARG(id); + + if (annotated_commit_init_from_id(out, repo, id, branch_name) < 0) + return -1; + + (*out)->ref_name = git__strdup(branch_name); + GIT_ERROR_CHECK_ALLOC((*out)->ref_name); + + (*out)->remote_url = git__strdup(remote_url); + GIT_ERROR_CHECK_ALLOC((*out)->remote_url); + + return 0; +} + + +const git_oid *git_annotated_commit_id( + const git_annotated_commit *annotated_commit) +{ + GIT_ASSERT_ARG_WITH_RETVAL(annotated_commit, NULL); + return git_commit_id(annotated_commit->commit); +} + +const char *git_annotated_commit_ref( + const git_annotated_commit *annotated_commit) +{ + GIT_ASSERT_ARG_WITH_RETVAL(annotated_commit, NULL); + return annotated_commit->ref_name; +} + +void git_annotated_commit_free(git_annotated_commit *annotated_commit) +{ + if (annotated_commit == NULL) + return; + + switch (annotated_commit->type) { + case GIT_ANNOTATED_COMMIT_REAL: + git_commit_free(annotated_commit->commit); + git_tree_free(annotated_commit->tree); + git__free((char *)annotated_commit->description); + git__free((char *)annotated_commit->ref_name); + git__free((char *)annotated_commit->remote_url); + break; + case GIT_ANNOTATED_COMMIT_VIRTUAL: + git_index_free(annotated_commit->index); + git_array_clear(annotated_commit->parents); + break; + default: + abort(); + } + + git__free(annotated_commit); +} diff --git a/src/libgit2/annotated_commit.h b/src/libgit2/annotated_commit.h new file mode 100644 index 00000000000..1f805fe9bda --- /dev/null +++ b/src/libgit2/annotated_commit.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_annotated_commit_h__ +#define INCLUDE_annotated_commit_h__ + +#include "common.h" + +#include "oidarray.h" + +#include "git2/oid.h" + +typedef enum { + GIT_ANNOTATED_COMMIT_REAL = 1, + GIT_ANNOTATED_COMMIT_VIRTUAL = 2 +} git_annotated_commit_t; + +/** + * Internal structure for merge inputs. An annotated commit is generally + * "real" and backed by an actual commit in the repository, but merge will + * internally create "virtual" commits that are in-memory intermediate + * commits backed by an index. + */ +struct git_annotated_commit { + git_annotated_commit_t type; + + /* real commit */ + git_commit *commit; + git_tree *tree; + + /* virtual commit structure */ + git_index *index; + git_array_oid_t parents; + + /* how this commit was looked up */ + const char *description; + + const char *ref_name; + const char *remote_url; + + char id_str[GIT_OID_MAX_HEXSIZE + 1]; +}; + +extern int git_annotated_commit_from_head(git_annotated_commit **out, + git_repository *repo); +extern int git_annotated_commit_from_commit(git_annotated_commit **out, + git_commit *commit); + +#endif diff --git a/src/libgit2/apply.c b/src/libgit2/apply.c new file mode 100644 index 00000000000..24922cfa550 --- /dev/null +++ b/src/libgit2/apply.c @@ -0,0 +1,898 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2/apply.h" +#include "git2/patch.h" +#include "git2/filter.h" +#include "git2/blob.h" +#include "git2/index.h" +#include "git2/checkout.h" +#include "git2/repository.h" +#include "array.h" +#include "patch.h" +#include "futils.h" +#include "delta.h" +#include "zstream.h" +#include "reader.h" +#include "index.h" +#include "repository.h" +#include "hashmap_str.h" +#include "apply.h" + +typedef struct { + /* The lines that we allocate ourself are allocated out of the pool. + * (Lines may have been allocated out of the diff.) + */ + git_pool pool; + git_vector lines; +} patch_image; + +static int apply_err(const char *fmt, ...) GIT_FORMAT_PRINTF(1, 2); +static int apply_err(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + git_error_vset(GIT_ERROR_PATCH, fmt, ap); + va_end(ap); + + return GIT_EAPPLYFAIL; +} + +static void patch_line_init( + git_diff_line *out, + const char *in, + size_t in_len, + size_t in_offset) +{ + out->content = in; + out->content_len = in_len; + out->content_offset = in_offset; +} + +#define PATCH_IMAGE_INIT { GIT_POOL_INIT, GIT_VECTOR_INIT } + +static int patch_image_init_fromstr( + patch_image *out, const char *in, size_t in_len) +{ + git_diff_line *line; + const char *start, *end; + + memset(out, 0x0, sizeof(patch_image)); + + if (git_pool_init(&out->pool, sizeof(git_diff_line)) < 0) + return -1; + + if (!in_len) + return 0; + + for (start = in; start < in + in_len; start = end) { + end = memchr(start, '\n', in_len - (start - in)); + + if (end == NULL) + end = in + in_len; + + else if (end < in + in_len) + end++; + + line = git_pool_mallocz(&out->pool, 1); + GIT_ERROR_CHECK_ALLOC(line); + + if (git_vector_insert(&out->lines, line) < 0) + return -1; + + patch_line_init(line, start, (end - start), (start - in)); + } + + return 0; +} + +static void patch_image_free(patch_image *image) +{ + if (image == NULL) + return; + + git_pool_clear(&image->pool); + git_vector_dispose(&image->lines); +} + +static bool match_hunk( + patch_image *image, + patch_image *preimage, + size_t linenum) +{ + bool match = 0; + size_t i; + + /* Ensure this hunk is within the image boundaries. */ + if (git_vector_length(&preimage->lines) + linenum > + git_vector_length(&image->lines)) + return 0; + + match = 1; + + /* Check exact match. */ + for (i = 0; i < git_vector_length(&preimage->lines); i++) { + git_diff_line *preimage_line = git_vector_get(&preimage->lines, i); + git_diff_line *image_line = git_vector_get(&image->lines, linenum + i); + + if (preimage_line->content_len != image_line->content_len || + memcmp(preimage_line->content, image_line->content, image_line->content_len) != 0) { + match = 0; + break; + } + } + + return match; +} + +static bool find_hunk_linenum( + size_t *out, + patch_image *image, + patch_image *preimage, + size_t linenum) +{ + size_t max = git_vector_length(&image->lines); + bool match; + + if (linenum > max) + linenum = max; + + match = match_hunk(image, preimage, linenum); + + *out = linenum; + return match; +} + +static int update_hunk( + patch_image *image, + size_t linenum, + patch_image *preimage, + patch_image *postimage) +{ + size_t postlen = git_vector_length(&postimage->lines); + size_t prelen = git_vector_length(&preimage->lines); + size_t i; + int error = 0; + + if (postlen > prelen) + error = git_vector_insert_null( + &image->lines, linenum, (postlen - prelen)); + else if (prelen > postlen) + error = git_vector_remove_range( + &image->lines, linenum, (prelen - postlen)); + + if (error) { + git_error_set_oom(); + return -1; + } + + for (i = 0; i < git_vector_length(&postimage->lines); i++) { + image->lines.contents[linenum + i] = + git_vector_get(&postimage->lines, i); + } + + return 0; +} + +typedef struct { + git_apply_options opts; + size_t skipped_new_lines; + size_t skipped_old_lines; +} apply_hunks_ctx; + +static int apply_hunk( + patch_image *image, + git_patch *patch, + git_patch_hunk *hunk, + apply_hunks_ctx *ctx) +{ + patch_image preimage = PATCH_IMAGE_INIT, postimage = PATCH_IMAGE_INIT; + size_t line_num, i; + int error = 0; + + if (ctx->opts.hunk_cb) { + error = ctx->opts.hunk_cb(&hunk->hunk, ctx->opts.payload); + + if (error) { + if (error > 0) { + ctx->skipped_new_lines += hunk->hunk.new_lines; + ctx->skipped_old_lines += hunk->hunk.old_lines; + error = 0; + } + + goto done; + } + } + + for (i = 0; i < hunk->line_count; i++) { + size_t linenum = hunk->line_start + i; + git_diff_line *line = git_array_get(patch->lines, linenum), *prev; + + if (!line) { + error = apply_err("preimage does not contain line %"PRIuZ, linenum); + goto done; + } + + switch (line->origin) { + case GIT_DIFF_LINE_CONTEXT_EOFNL: + case GIT_DIFF_LINE_DEL_EOFNL: + case GIT_DIFF_LINE_ADD_EOFNL: + prev = i ? git_array_get(patch->lines, linenum - 1) : NULL; + if (prev && prev->content[prev->content_len - 1] == '\n') + prev->content_len -= 1; + break; + case GIT_DIFF_LINE_CONTEXT: + if ((error = git_vector_insert(&preimage.lines, line)) < 0 || + (error = git_vector_insert(&postimage.lines, line)) < 0) + goto done; + break; + case GIT_DIFF_LINE_DELETION: + if ((error = git_vector_insert(&preimage.lines, line)) < 0) + goto done; + break; + case GIT_DIFF_LINE_ADDITION: + if ((error = git_vector_insert(&postimage.lines, line)) < 0) + goto done; + break; + } + } + + if (hunk->hunk.new_start) { + line_num = hunk->hunk.new_start - + ctx->skipped_new_lines + + ctx->skipped_old_lines - + 1; + } else { + line_num = 0; + } + + if (!find_hunk_linenum(&line_num, image, &preimage, line_num)) { + error = apply_err("hunk at line %d did not apply", + hunk->hunk.new_start); + goto done; + } + + error = update_hunk(image, line_num, &preimage, &postimage); + +done: + patch_image_free(&preimage); + patch_image_free(&postimage); + + return error; +} + +static int apply_hunks( + git_str *out, + const char *source, + size_t source_len, + git_patch *patch, + apply_hunks_ctx *ctx) +{ + git_patch_hunk *hunk; + git_diff_line *line; + patch_image image; + size_t i; + int error = 0; + + if ((error = patch_image_init_fromstr(&image, source, source_len)) < 0) + goto done; + + git_array_foreach(patch->hunks, i, hunk) { + if ((error = apply_hunk(&image, patch, hunk, ctx)) < 0) + goto done; + } + + git_vector_foreach(&image.lines, i, line) + git_str_put(out, line->content, line->content_len); + +done: + patch_image_free(&image); + + return error; +} + +static int apply_binary_delta( + git_str *out, + const char *source, + size_t source_len, + git_diff_binary_file *binary_file) +{ + git_str inflated = GIT_STR_INIT; + int error = 0; + + /* no diff means identical contents */ + if (binary_file->datalen == 0) + return git_str_put(out, source, source_len); + + error = git_zstream_inflatebuf(&inflated, + binary_file->data, binary_file->datalen); + + if (!error && inflated.size != binary_file->inflatedlen) { + error = apply_err("inflated delta does not match expected length"); + git_str_dispose(out); + } + + if (error < 0) + goto done; + + if (binary_file->type == GIT_DIFF_BINARY_DELTA) { + void *data; + size_t data_len; + + error = git_delta_apply(&data, &data_len, (void *)source, source_len, + (void *)inflated.ptr, inflated.size); + + out->ptr = data; + out->size = data_len; + out->asize = data_len; + } + else if (binary_file->type == GIT_DIFF_BINARY_LITERAL) { + git_str_swap(out, &inflated); + } + else { + error = apply_err("unknown binary delta type"); + goto done; + } + +done: + git_str_dispose(&inflated); + return error; +} + +static int apply_binary( + git_str *out, + const char *source, + size_t source_len, + git_patch *patch) +{ + git_str reverse = GIT_STR_INIT; + int error = 0; + + if (!patch->binary.contains_data) { + error = apply_err("patch does not contain binary data"); + goto done; + } + + if (!patch->binary.old_file.datalen && !patch->binary.new_file.datalen) + goto done; + + /* first, apply the new_file delta to the given source */ + if ((error = apply_binary_delta(out, source, source_len, + &patch->binary.new_file)) < 0) + goto done; + + /* second, apply the old_file delta to sanity check the result */ + if ((error = apply_binary_delta(&reverse, out->ptr, out->size, + &patch->binary.old_file)) < 0) + goto done; + + /* Verify that the resulting file with the reverse patch applied matches the source file */ + if (source_len != reverse.size || + (source_len && memcmp(source, reverse.ptr, source_len) != 0)) { + error = apply_err("binary patch did not apply cleanly"); + goto done; + } + +done: + if (error < 0) + git_str_dispose(out); + + git_str_dispose(&reverse); + return error; +} + +int git_apply__patch( + git_str *contents_out, + char **filename_out, + unsigned int *mode_out, + const char *source, + size_t source_len, + git_patch *patch, + const git_apply_options *given_opts) +{ + apply_hunks_ctx ctx = { GIT_APPLY_OPTIONS_INIT }; + char *filename = NULL; + unsigned int mode = 0; + int error = 0; + + GIT_ASSERT_ARG(contents_out); + GIT_ASSERT_ARG(filename_out); + GIT_ASSERT_ARG(mode_out); + GIT_ASSERT_ARG(source || !source_len); + GIT_ASSERT_ARG(patch); + + if (given_opts) + memcpy(&ctx.opts, given_opts, sizeof(git_apply_options)); + + *filename_out = NULL; + *mode_out = 0; + + if (patch->delta->status != GIT_DELTA_DELETED) { + const git_diff_file *newfile = &patch->delta->new_file; + + filename = git__strdup(newfile->path); + mode = newfile->mode ? + newfile->mode : GIT_FILEMODE_BLOB; + } + + if (patch->delta->flags & GIT_DIFF_FLAG_BINARY) + error = apply_binary(contents_out, source, source_len, patch); + else if (patch->hunks.size) + error = apply_hunks(contents_out, source, source_len, patch, &ctx); + else + error = git_str_put(contents_out, source, source_len); + + if (error) + goto done; + + if (patch->delta->status == GIT_DELTA_DELETED && + git_str_len(contents_out) > 0) { + error = apply_err("removal patch leaves file contents"); + goto done; + } + + *filename_out = filename; + *mode_out = mode; + +done: + if (error < 0) + git__free(filename); + + return error; +} + +static int apply_one( + git_repository *repo, + git_reader *preimage_reader, + git_index *preimage, + git_reader *postimage_reader, + git_index *postimage, + git_diff *diff, + git_hashset_str *removed_paths, + size_t i, + const git_apply_options *opts) +{ + git_patch *patch = NULL; + git_str pre_contents = GIT_STR_INIT, post_contents = GIT_STR_INIT; + const git_diff_delta *delta; + char *filename = NULL; + unsigned int mode; + git_oid pre_id, post_id; + git_filemode_t pre_filemode; + git_index_entry pre_entry, post_entry; + bool skip_preimage = false; + int error; + + if ((error = git_patch_from_diff(&patch, diff, i)) < 0) + goto done; + + delta = git_patch_get_delta(patch); + + if (opts->delta_cb) { + error = opts->delta_cb(delta, opts->payload); + + if (error) { + if (error > 0) + error = 0; + + goto done; + } + } + + /* + * Ensure that the file has not been deleted or renamed if we're + * applying a modification delta. + */ + if (delta->status != GIT_DELTA_RENAMED && + delta->status != GIT_DELTA_ADDED) { + if (git_hashset_str_contains(removed_paths, delta->old_file.path)) { + error = apply_err("path '%s' has been renamed or deleted", delta->old_file.path); + goto done; + } + } + + /* + * We may be applying a second delta to an already seen file. If so, + * use the already modified data in the postimage instead of the + * content from the index or working directory. (Don't do this in + * the case of a rename, which must be specified before additional + * deltas since we apply deltas to the target filename.) + */ + if (delta->status != GIT_DELTA_RENAMED) { + if ((error = git_reader_read(&pre_contents, &pre_id, &pre_filemode, + postimage_reader, delta->old_file.path)) == 0) { + skip_preimage = true; + } else if (error == GIT_ENOTFOUND) { + git_error_clear(); + error = 0; + } else { + goto done; + } + } + + if (!skip_preimage && delta->status != GIT_DELTA_ADDED) { + error = git_reader_read(&pre_contents, &pre_id, &pre_filemode, + preimage_reader, delta->old_file.path); + + /* ENOTFOUND means the preimage was not found; apply failed. */ + if (error == GIT_ENOTFOUND) + error = GIT_EAPPLYFAIL; + + /* When applying to BOTH, the index did not match the workdir. */ + if (error == GIT_READER_MISMATCH) + error = apply_err("%s: does not match index", delta->old_file.path); + + if (error < 0) + goto done; + + /* + * We need to populate the preimage data structure with the + * contents that we are using as the preimage for this file. + * This allows us to apply patches to files that have been + * modified in the working directory. During checkout, + * we will use this expected preimage as the baseline, and + * limit checkout to only the paths affected by patch + * application. (Without this, we would fail to write the + * postimage contents to any file that had been modified + * from HEAD on-disk, even if the patch application succeeded.) + * Use the contents from the delta where available - some + * fields may not be available, like the old file mode (eg in + * an exact rename situation) so trust the patch parsing to + * validate and use the preimage data in that case. + */ + if (preimage) { + memset(&pre_entry, 0, sizeof(git_index_entry)); + pre_entry.path = delta->old_file.path; + pre_entry.mode = delta->old_file.mode ? delta->old_file.mode : pre_filemode; + git_oid_cpy(&pre_entry.id, &pre_id); + + if ((error = git_index_add(preimage, &pre_entry)) < 0) + goto done; + } + } + + if (delta->status != GIT_DELTA_DELETED) { + if ((error = git_apply__patch(&post_contents, &filename, &mode, + pre_contents.ptr, pre_contents.size, patch, opts)) < 0 || + (error = git_blob_create_from_buffer(&post_id, repo, + post_contents.ptr, post_contents.size)) < 0) + goto done; + + memset(&post_entry, 0, sizeof(git_index_entry)); + post_entry.path = filename; + post_entry.mode = mode; + git_oid_cpy(&post_entry.id, &post_id); + + if ((error = git_index_add(postimage, &post_entry)) < 0) + goto done; + } + + if (delta->status == GIT_DELTA_RENAMED || + delta->status == GIT_DELTA_DELETED) + error = git_hashset_str_add(removed_paths, delta->old_file.path); + + if (delta->status == GIT_DELTA_RENAMED || + delta->status == GIT_DELTA_ADDED) + git_hashset_str_remove(removed_paths, delta->new_file.path); + +done: + git_str_dispose(&pre_contents); + git_str_dispose(&post_contents); + git__free(filename); + git_patch_free(patch); + + return error; +} + +static int apply_deltas( + git_repository *repo, + git_reader *pre_reader, + git_index *preimage, + git_reader *post_reader, + git_index *postimage, + git_diff *diff, + const git_apply_options *opts) +{ + git_hashset_str removed_paths = GIT_HASHSET_INIT; + size_t i; + int error = 0; + + for (i = 0; i < git_diff_num_deltas(diff); i++) { + if ((error = apply_one(repo, pre_reader, preimage, post_reader, postimage, diff, &removed_paths, i, opts)) < 0) + goto done; + } + +done: + git_hashset_str_dispose(&removed_paths); + return error; +} + +int git_apply_to_tree( + git_index **out, + git_repository *repo, + git_tree *preimage, + git_diff *diff, + const git_apply_options *given_opts) +{ + git_index *postimage = NULL; + git_reader *pre_reader = NULL, *post_reader = NULL; + git_index_options index_opts = GIT_INDEX_OPTIONS_FOR_REPO(repo); + git_apply_options opts = GIT_APPLY_OPTIONS_INIT; + const git_diff_delta *delta; + size_t i; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(preimage); + GIT_ASSERT_ARG(diff); + + *out = NULL; + + if (given_opts) + memcpy(&opts, given_opts, sizeof(git_apply_options)); + + if ((error = git_reader_for_tree(&pre_reader, preimage)) < 0) + goto done; + + /* + * put the current tree into the postimage as-is - the diff will + * replace any entries contained therein + */ + if ((error = git_index_new_ext(&postimage, &index_opts)) < 0 || + (error = git_index_read_tree(postimage, preimage)) < 0 || + (error = git_reader_for_index(&post_reader, repo, postimage)) < 0) + goto done; + + /* + * Remove the old paths from the index before applying diffs - + * we need to do a full pass to remove them before adding deltas, + * in order to handle rename situations. + */ + for (i = 0; i < git_diff_num_deltas(diff); i++) { + delta = git_diff_get_delta(diff, i); + + if (delta->status == GIT_DELTA_DELETED || + delta->status == GIT_DELTA_RENAMED) { + if ((error = git_index_remove(postimage, + delta->old_file.path, 0)) < 0) + goto done; + } + } + + if ((error = apply_deltas(repo, pre_reader, NULL, post_reader, postimage, diff, &opts)) < 0) + goto done; + + *out = postimage; + +done: + if (error < 0) + git_index_free(postimage); + + git_reader_free(pre_reader); + git_reader_free(post_reader); + + return error; +} + +static int git_apply__to_workdir( + git_repository *repo, + git_diff *diff, + git_index *preimage, + git_index *postimage, + git_apply_location_t location, + git_apply_options *opts) +{ + git_vector paths = GIT_VECTOR_INIT; + git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; + const git_diff_delta *delta; + size_t i; + int error; + + GIT_UNUSED(opts); + + /* + * Limit checkout to the paths affected by the diff; this ensures + * that other modifications in the working directory are unaffected. + */ + if ((error = git_vector_init(&paths, git_diff_num_deltas(diff), NULL)) < 0) + goto done; + + for (i = 0; i < git_diff_num_deltas(diff); i++) { + delta = git_diff_get_delta(diff, i); + + if ((error = git_vector_insert(&paths, (void *)delta->old_file.path)) < 0) + goto done; + + if (strcmp(delta->old_file.path, delta->new_file.path) && + (error = git_vector_insert(&paths, (void *)delta->new_file.path)) < 0) + goto done; + } + + checkout_opts.checkout_strategy |= GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH; + checkout_opts.checkout_strategy |= GIT_CHECKOUT_DONT_WRITE_INDEX; + + if (location == GIT_APPLY_LOCATION_WORKDIR) + checkout_opts.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX; + + checkout_opts.paths.strings = (char **)paths.contents; + checkout_opts.paths.count = paths.length; + + checkout_opts.baseline_index = preimage; + + error = git_checkout_index(repo, postimage, &checkout_opts); + +done: + git_vector_dispose(&paths); + return error; +} + +static int git_apply__to_index( + git_repository *repo, + git_diff *diff, + git_index *preimage, + git_index *postimage, + git_apply_options *opts) +{ + git_index *index = NULL; + const git_diff_delta *delta; + const git_index_entry *entry; + size_t i; + int error; + + GIT_UNUSED(preimage); + GIT_UNUSED(opts); + + if ((error = git_repository_index(&index, repo)) < 0) + goto done; + + /* Remove deleted (or renamed) paths from the index. */ + for (i = 0; i < git_diff_num_deltas(diff); i++) { + delta = git_diff_get_delta(diff, i); + + if (delta->status == GIT_DELTA_DELETED || + delta->status == GIT_DELTA_RENAMED) { + if ((error = git_index_remove(index, delta->old_file.path, 0)) < 0) + goto done; + } + } + + /* Then add the changes back to the index. */ + for (i = 0; i < git_index_entrycount(postimage); i++) { + entry = git_index_get_byindex(postimage, i); + + if ((error = git_index_add(index, entry)) < 0) + goto done; + } + +done: + git_index_free(index); + return error; +} + +int git_apply_options_init(git_apply_options *opts, unsigned int version) +{ + GIT_ASSERT_ARG(opts); + + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_apply_options, GIT_APPLY_OPTIONS_INIT); + return 0; +} + +/* + * Handle the three application options ("locations"): + * + * GIT_APPLY_LOCATION_WORKDIR: the default, emulates `git apply`. + * Applies the diff only to the workdir items and ignores the index + * entirely. + * + * GIT_APPLY_LOCATION_INDEX: emulates `git apply --cached`. + * Applies the diff only to the index items and ignores the workdir + * completely. + * + * GIT_APPLY_LOCATION_BOTH: emulates `git apply --index`. + * Applies the diff to both the index items and the working directory + * items. + */ + +int git_apply( + git_repository *repo, + git_diff *diff, + git_apply_location_t location, + const git_apply_options *given_opts) +{ + git_indexwriter indexwriter = GIT_INDEXWRITER_INIT; + git_index *index = NULL, *preimage = NULL, *postimage = NULL; + git_reader *pre_reader = NULL, *post_reader = NULL; + git_apply_options opts = GIT_APPLY_OPTIONS_INIT; + git_index_options index_opts = GIT_INDEX_OPTIONS_FOR_REPO(repo); + int error = GIT_EINVALID; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(diff); + + GIT_ERROR_CHECK_VERSION( + given_opts, GIT_APPLY_OPTIONS_VERSION, "git_apply_options"); + + if (given_opts) + memcpy(&opts, given_opts, sizeof(git_apply_options)); + + /* + * by default, we apply a patch directly to the working directory; + * in `--cached` or `--index` mode, we apply to the contents already + * in the index. + */ + switch (location) { + case GIT_APPLY_LOCATION_BOTH: + error = git_reader_for_workdir(&pre_reader, repo, true); + break; + case GIT_APPLY_LOCATION_INDEX: + error = git_reader_for_index(&pre_reader, repo, NULL); + break; + case GIT_APPLY_LOCATION_WORKDIR: + error = git_reader_for_workdir(&pre_reader, repo, false); + break; + default: + GIT_ASSERT(false); + } + + if (error < 0) + goto done; + + /* + * Build the preimage and postimage (differences). Note that + * this is not the complete preimage or postimage, it only + * contains the files affected by the patch. We want to avoid + * having the full repo index, so we will limit our checkout + * to only write these files that were affected by the diff. + */ + if ((error = git_index_new_ext(&preimage, &index_opts)) < 0 || + (error = git_index_new_ext(&postimage, &index_opts)) < 0 || + (error = git_reader_for_index(&post_reader, repo, postimage)) < 0) + goto done; + + if (!(opts.flags & GIT_APPLY_CHECK)) + if ((error = git_repository_index(&index, repo)) < 0 || + (error = git_indexwriter_init(&indexwriter, index)) < 0) + goto done; + + if ((error = apply_deltas(repo, pre_reader, preimage, post_reader, postimage, diff, &opts)) < 0) + goto done; + + if ((opts.flags & GIT_APPLY_CHECK)) + goto done; + + switch (location) { + case GIT_APPLY_LOCATION_BOTH: + error = git_apply__to_workdir(repo, diff, preimage, postimage, location, &opts); + break; + case GIT_APPLY_LOCATION_INDEX: + error = git_apply__to_index(repo, diff, preimage, postimage, &opts); + break; + case GIT_APPLY_LOCATION_WORKDIR: + error = git_apply__to_workdir(repo, diff, preimage, postimage, location, &opts); + break; + default: + GIT_ASSERT(false); + } + + if (error < 0) + goto done; + + error = git_indexwriter_commit(&indexwriter); + +done: + git_indexwriter_cleanup(&indexwriter); + git_index_free(postimage); + git_index_free(preimage); + git_index_free(index); + git_reader_free(pre_reader); + git_reader_free(post_reader); + + return error; +} diff --git a/src/libgit2/apply.h b/src/libgit2/apply.h new file mode 100644 index 00000000000..e990a71071c --- /dev/null +++ b/src/libgit2/apply.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_apply_h__ +#define INCLUDE_apply_h__ + +#include "common.h" + +#include "git2/patch.h" +#include "git2/apply.h" +#include "str.h" + +extern int git_apply__patch( + git_str *out, + char **filename, + unsigned int *mode, + const char *source, + size_t source_len, + git_patch *patch, + const git_apply_options *opts); + +#endif diff --git a/src/libgit2/attr.c b/src/libgit2/attr.c new file mode 100644 index 00000000000..8d096d7d85c --- /dev/null +++ b/src/libgit2/attr.c @@ -0,0 +1,717 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "attr.h" + +#include "repository.h" +#include "sysdir.h" +#include "config.h" +#include "attr_file.h" +#include "ignore.h" +#include "git2/oid.h" +#include "hashmap_str.h" +#include + +const char *git_attr__true = "[internal]__TRUE__"; +const char *git_attr__false = "[internal]__FALSE__"; +const char *git_attr__unset = "[internal]__UNSET__"; + +git_attr_value_t git_attr_value(const char *attr) +{ + if (attr == NULL || attr == git_attr__unset) + return GIT_ATTR_VALUE_UNSPECIFIED; + + if (attr == git_attr__true) + return GIT_ATTR_VALUE_TRUE; + + if (attr == git_attr__false) + return GIT_ATTR_VALUE_FALSE; + + return GIT_ATTR_VALUE_STRING; +} + +static int collect_attr_files( + git_repository *repo, + git_attr_session *attr_session, + git_attr_options *opts, + const char *path, + git_vector *files); + +static void release_attr_files(git_vector *files); + +int git_attr_get_ext( + const char **value, + git_repository *repo, + git_attr_options *opts, + const char *pathname, + const char *name) +{ + int error; + git_attr_path path; + git_vector files = GIT_VECTOR_INIT; + size_t i, j; + git_attr_file *file; + git_attr_name attr; + git_attr_rule *rule; + git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN; + + GIT_ASSERT_ARG(value); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + GIT_ERROR_CHECK_VERSION(opts, GIT_ATTR_OPTIONS_VERSION, "git_attr_options"); + + *value = NULL; + + if (git_repository_is_bare(repo)) + dir_flag = GIT_DIR_FLAG_FALSE; + + if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0) + return -1; + + if ((error = collect_attr_files(repo, NULL, opts, pathname, &files)) < 0) + goto cleanup; + + memset(&attr, 0, sizeof(attr)); + attr.name = name; + attr.name_hash = git_attr_file__name_hash(name); + + git_vector_foreach(&files, i, file) { + + git_attr_file__foreach_matching_rule(file, &path, j, rule) { + size_t pos; + + if (!git_vector_bsearch(&pos, &rule->assigns, &attr)) { + *value = ((git_attr_assignment *)git_vector_get( + &rule->assigns, pos))->value; + goto cleanup; + } + } + } + +cleanup: + release_attr_files(&files); + git_attr_path__free(&path); + + return error; +} + +int git_attr_get( + const char **value, + git_repository *repo, + uint32_t flags, + const char *pathname, + const char *name) +{ + git_attr_options opts = GIT_ATTR_OPTIONS_INIT; + + opts.flags = flags; + + return git_attr_get_ext(value, repo, &opts, pathname, name); +} + + +typedef struct { + git_attr_name name; + git_attr_assignment *found; +} attr_get_many_info; + +int git_attr_get_many_with_session( + const char **values, + git_repository *repo, + git_attr_session *attr_session, + git_attr_options *opts, + const char *pathname, + size_t num_attr, + const char **names) +{ + int error; + git_attr_path path; + git_vector files = GIT_VECTOR_INIT; + size_t i, j, k; + git_attr_file *file; + git_attr_rule *rule; + attr_get_many_info *info = NULL; + size_t num_found = 0; + git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN; + + if (!num_attr) + return 0; + + GIT_ASSERT_ARG(values); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(pathname); + GIT_ASSERT_ARG(names); + GIT_ERROR_CHECK_VERSION(opts, GIT_ATTR_OPTIONS_VERSION, "git_attr_options"); + + if (git_repository_is_bare(repo)) + dir_flag = GIT_DIR_FLAG_FALSE; + + if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0) + return -1; + + if ((error = collect_attr_files(repo, attr_session, opts, pathname, &files)) < 0) + goto cleanup; + + info = git__calloc(num_attr, sizeof(attr_get_many_info)); + GIT_ERROR_CHECK_ALLOC(info); + + git_vector_foreach(&files, i, file) { + + git_attr_file__foreach_matching_rule(file, &path, j, rule) { + + for (k = 0; k < num_attr; k++) { + size_t pos; + + if (info[k].found != NULL) /* already found assignment */ + continue; + + if (!info[k].name.name) { + info[k].name.name = names[k]; + info[k].name.name_hash = git_attr_file__name_hash(names[k]); + } + + if (!git_vector_bsearch(&pos, &rule->assigns, &info[k].name)) { + info[k].found = (git_attr_assignment *) + git_vector_get(&rule->assigns, pos); + values[k] = info[k].found->value; + + if (++num_found == num_attr) + goto cleanup; + } + } + } + } + + for (k = 0; k < num_attr; k++) { + if (!info[k].found) + values[k] = NULL; + } + +cleanup: + release_attr_files(&files); + git_attr_path__free(&path); + git__free(info); + + return error; +} + +int git_attr_get_many( + const char **values, + git_repository *repo, + uint32_t flags, + const char *pathname, + size_t num_attr, + const char **names) +{ + git_attr_options opts = GIT_ATTR_OPTIONS_INIT; + + opts.flags = flags; + + return git_attr_get_many_with_session( + values, repo, NULL, &opts, pathname, num_attr, names); +} + +int git_attr_get_many_ext( + const char **values, + git_repository *repo, + git_attr_options *opts, + const char *pathname, + size_t num_attr, + const char **names) +{ + return git_attr_get_many_with_session( + values, repo, NULL, opts, pathname, num_attr, names); +} + +int git_attr_foreach( + git_repository *repo, + uint32_t flags, + const char *pathname, + int (*callback)(const char *name, const char *value, void *payload), + void *payload) +{ + git_attr_options opts = GIT_ATTR_OPTIONS_INIT; + + opts.flags = flags; + + return git_attr_foreach_ext(repo, &opts, pathname, callback, payload); +} + +int git_attr_foreach_ext( + git_repository *repo, + git_attr_options *opts, + const char *pathname, + int (*callback)(const char *name, const char *value, void *payload), + void *payload) +{ + int error; + git_attr_path path; + git_vector files = GIT_VECTOR_INIT; + size_t i, j, k; + git_attr_file *file; + git_attr_rule *rule; + git_attr_assignment *assign; + git_hashset_str seen = GIT_HASHSET_INIT; + git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(callback); + GIT_ERROR_CHECK_VERSION(opts, GIT_ATTR_OPTIONS_VERSION, "git_attr_options"); + + if (git_repository_is_bare(repo)) + dir_flag = GIT_DIR_FLAG_FALSE; + + if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0) + return -1; + + if ((error = collect_attr_files(repo, NULL, opts, pathname, &files)) < 0) + goto cleanup; + + git_vector_foreach(&files, i, file) { + + git_attr_file__foreach_matching_rule(file, &path, j, rule) { + + git_vector_foreach(&rule->assigns, k, assign) { + /* skip if higher priority assignment was already seen */ + if (git_hashset_str_contains(&seen, assign->name)) + continue; + + if ((error = git_hashset_str_add(&seen, assign->name)) < 0) + goto cleanup; + + error = callback(assign->name, assign->value, payload); + if (error) { + git_error_set_after_callback(error); + goto cleanup; + } + } + } + } + +cleanup: + git_hashset_str_dispose(&seen); + release_attr_files(&files); + git_attr_path__free(&path); + + return error; +} + +static int preload_attr_source( + git_repository *repo, + git_attr_session *attr_session, + git_attr_file_source *source) +{ + int error; + git_attr_file *preload = NULL; + + if (!source) + return 0; + + error = git_attr_cache__get(&preload, repo, attr_session, source, + git_attr_file__parse_buffer, true); + + if (!error) + git_attr_file__free(preload); + + return error; +} + +GIT_INLINE(int) preload_attr_file( + git_repository *repo, + git_attr_session *attr_session, + const char *base, + const char *filename) +{ + git_attr_file_source source = { GIT_ATTR_FILE_SOURCE_FILE }; + + if (!filename) + return 0; + + source.base = base; + source.filename = filename; + + return preload_attr_source(repo, attr_session, &source); +} + +static int system_attr_file( + git_str *out, + git_attr_session *attr_session) +{ + int error; + + if (!attr_session) { + error = git_sysdir_find_system_file(out, GIT_ATTR_FILE_SYSTEM); + + if (error == GIT_ENOTFOUND) + git_error_clear(); + + return error; + } + + if (!attr_session->init_sysdir) { + error = git_sysdir_find_system_file(&attr_session->sysdir, GIT_ATTR_FILE_SYSTEM); + + if (error == GIT_ENOTFOUND) + git_error_clear(); + else if (error) + return error; + + attr_session->init_sysdir = 1; + } + + if (attr_session->sysdir.size == 0) + return GIT_ENOTFOUND; + + /* We can safely provide a git_str with no allocation (asize == 0) to + * a consumer. This allows them to treat this as a regular `git_str`, + * but their call to `git_str_dispose` will not attempt to free it. + */ + git_str_attach_notowned( + out, attr_session->sysdir.ptr, attr_session->sysdir.size); + return 0; +} + +static int attr_setup( + git_repository *repo, + git_attr_session *attr_session, + git_attr_options *opts) +{ + git_str system = GIT_STR_INIT, info = GIT_STR_INIT; + git_attr_file_source index_source = { GIT_ATTR_FILE_SOURCE_INDEX, NULL, GIT_ATTR_FILE, NULL }; + git_attr_file_source head_source = { GIT_ATTR_FILE_SOURCE_HEAD, NULL, GIT_ATTR_FILE, NULL }; + git_attr_file_source commit_source = { GIT_ATTR_FILE_SOURCE_COMMIT, NULL, GIT_ATTR_FILE, NULL }; + git_attr_cache *attrcache; + const char *attr_cfg_file = NULL; + git_index *idx = NULL; + const char *workdir; + int error = 0; + + if (attr_session && attr_session->init_setup) + return 0; + + if ((error = git_attr_cache__init(repo)) < 0) + return error; + + /* + * Preload attribute files that could contain macros so the + * definitions will be available for later file parsing. + */ + + if ((error = system_attr_file(&system, attr_session)) < 0 || + (error = preload_attr_file(repo, attr_session, NULL, system.ptr)) < 0) { + if (error != GIT_ENOTFOUND) + goto out; + + error = 0; + } + + if ((attrcache = git_repository_attr_cache(repo)) != NULL) + attr_cfg_file = git_attr_cache_attributesfile(attrcache); + + if ((error = preload_attr_file(repo, attr_session, NULL, attr_cfg_file)) < 0) + goto out; + + if ((error = git_repository__item_path(&info, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 || + (error = preload_attr_file(repo, attr_session, info.ptr, GIT_ATTR_FILE_INREPO)) < 0) { + if (error != GIT_ENOTFOUND) + goto out; + + error = 0; + } + + if ((workdir = git_repository_workdir(repo)) != NULL && + (error = preload_attr_file(repo, attr_session, workdir, GIT_ATTR_FILE)) < 0) + goto out; + + if ((error = git_repository_index__weakptr(&idx, repo)) < 0 || + (error = preload_attr_source(repo, attr_session, &index_source)) < 0) { + if (error != GIT_ENOTFOUND) + goto out; + + error = 0; + } + + if ((opts && (opts->flags & GIT_ATTR_CHECK_INCLUDE_HEAD) != 0) && + (error = preload_attr_source(repo, attr_session, &head_source)) < 0) + goto out; + + if ((opts && (opts->flags & GIT_ATTR_CHECK_INCLUDE_COMMIT) != 0)) { +#ifndef GIT_DEPRECATE_HARD + if (opts->commit_id) + commit_source.commit_id = opts->commit_id; + else +#endif + commit_source.commit_id = &opts->attr_commit_id; + + if ((error = preload_attr_source(repo, attr_session, &commit_source)) < 0) + goto out; + } + + if (attr_session) + attr_session->init_setup = 1; + +out: + git_str_dispose(&system); + git_str_dispose(&info); + + return error; +} + +int git_attr_add_macro( + git_repository *repo, + const char *name, + const char *values) +{ + int error; + git_attr_rule *macro = NULL; + git_attr_cache *attrcache; + git_pool *pool; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + if ((error = git_attr_cache__init(repo)) < 0) + return error; + + macro = git__calloc(1, sizeof(git_attr_rule)); + GIT_ERROR_CHECK_ALLOC(macro); + + attrcache = git_repository_attr_cache(repo); + pool = git_attr_cache_pool(attrcache); + + macro->match.pattern = git_pool_strdup(pool, name); + GIT_ERROR_CHECK_ALLOC(macro->match.pattern); + + macro->match.length = strlen(macro->match.pattern); + macro->match.flags = GIT_ATTR_FNMATCH_MACRO; + + error = git_attr_assignment__parse(repo, pool, ¯o->assigns, &values); + + if (!error) + error = git_attr_cache__insert_macro(repo, macro); + + if (error < 0) + git_attr_rule__free(macro); + + return error; +} + +typedef struct { + git_repository *repo; + git_attr_session *attr_session; + git_attr_options *opts; + const char *workdir; + git_index *index; + git_vector *files; +} attr_walk_up_info; + +static int attr_decide_sources( + uint32_t flags, + bool has_wd, + bool has_index, + git_attr_file_source_t *srcs) +{ + int count = 0; + + switch (flags & 0x03) { + case GIT_ATTR_CHECK_FILE_THEN_INDEX: + if (has_wd) + srcs[count++] = GIT_ATTR_FILE_SOURCE_FILE; + if (has_index) + srcs[count++] = GIT_ATTR_FILE_SOURCE_INDEX; + break; + case GIT_ATTR_CHECK_INDEX_THEN_FILE: + if (has_index) + srcs[count++] = GIT_ATTR_FILE_SOURCE_INDEX; + if (has_wd) + srcs[count++] = GIT_ATTR_FILE_SOURCE_FILE; + break; + case GIT_ATTR_CHECK_INDEX_ONLY: + if (has_index) + srcs[count++] = GIT_ATTR_FILE_SOURCE_INDEX; + break; + } + + if ((flags & GIT_ATTR_CHECK_INCLUDE_HEAD) != 0) + srcs[count++] = GIT_ATTR_FILE_SOURCE_HEAD; + + if ((flags & GIT_ATTR_CHECK_INCLUDE_COMMIT) != 0) + srcs[count++] = GIT_ATTR_FILE_SOURCE_COMMIT; + + return count; +} + +static int push_attr_source( + git_repository *repo, + git_attr_session *attr_session, + git_vector *list, + git_attr_file_source *source, + bool allow_macros) +{ + int error = 0; + git_attr_file *file = NULL; + + error = git_attr_cache__get(&file, repo, attr_session, + source, + git_attr_file__parse_buffer, + allow_macros); + + if (error < 0) + return error; + + if (file != NULL) { + if ((error = git_vector_insert(list, file)) < 0) + git_attr_file__free(file); + } + + return error; +} + +GIT_INLINE(int) push_attr_file( + git_repository *repo, + git_attr_session *attr_session, + git_vector *list, + const char *base, + const char *filename) +{ + git_attr_file_source source = { GIT_ATTR_FILE_SOURCE_FILE, base, filename }; + return push_attr_source(repo, attr_session, list, &source, true); +} + +static int push_one_attr(void *ref, const char *path) +{ + attr_walk_up_info *info = (attr_walk_up_info *)ref; + git_attr_file_source_t src[GIT_ATTR_FILE_NUM_SOURCES]; + int error = 0, n_src, i; + bool allow_macros; + + n_src = attr_decide_sources(info->opts ? info->opts->flags : 0, + info->workdir != NULL, + info->index != NULL, + src); + + allow_macros = info->workdir ? !strcmp(info->workdir, path) : false; + + for (i = 0; !error && i < n_src; ++i) { + git_attr_file_source source = { src[i], path, GIT_ATTR_FILE }; + + if (src[i] == GIT_ATTR_FILE_SOURCE_COMMIT && info->opts) { +#ifndef GIT_DEPRECATE_HARD + if (info->opts->commit_id) + source.commit_id = info->opts->commit_id; + else +#endif + source.commit_id = &info->opts->attr_commit_id; + } + + error = push_attr_source(info->repo, info->attr_session, info->files, + &source, allow_macros); + } + + return error; +} + +static void release_attr_files(git_vector *files) +{ + size_t i; + git_attr_file *file; + + git_vector_foreach(files, i, file) { + git_attr_file__free(file); + files->contents[i] = NULL; + } + git_vector_dispose(files); +} + +static int collect_attr_files( + git_repository *repo, + git_attr_session *attr_session, + git_attr_options *opts, + const char *path, + git_vector *files) +{ + int error = 0; + git_str dir = GIT_STR_INIT, attrfile = GIT_STR_INIT; + const char *workdir = git_repository_workdir(repo); + git_attr_cache *attrcache; + const char *attr_cfg_file = NULL; + attr_walk_up_info info = { NULL }; + + GIT_ASSERT(!git_fs_path_is_absolute(path)); + + if ((error = attr_setup(repo, attr_session, opts)) < 0) + return error; + + /* Resolve path in a non-bare repo */ + if (workdir != NULL) { + if (!(error = git_repository_workdir_path(&dir, repo, path))) + error = git_fs_path_find_dir(&dir); + } + else { + error = git_fs_path_dirname_r(&dir, path); + } + + if (error < 0) + goto cleanup; + + /* in precedence order highest to lowest: + * - $GIT_DIR/info/attributes + * - path components with .gitattributes + * - config core.attributesfile + * - $GIT_PREFIX/etc/gitattributes + */ + + if ((error = git_repository__item_path(&attrfile, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 || + (error = push_attr_file(repo, attr_session, files, attrfile.ptr, GIT_ATTR_FILE_INREPO)) < 0) { + if (error != GIT_ENOTFOUND) + goto cleanup; + } + + info.repo = repo; + info.attr_session = attr_session; + info.opts = opts; + info.workdir = workdir; + if (git_repository_index__weakptr(&info.index, repo) < 0) + git_error_clear(); /* no error even if there is no index */ + info.files = files; + + if (!strcmp(dir.ptr, ".")) + error = push_one_attr(&info, ""); + else + error = git_fs_path_walk_up(&dir, workdir, push_one_attr, &info); + + if (error < 0) + goto cleanup; + + if ((attrcache = git_repository_attr_cache(repo)) != NULL) + attr_cfg_file = git_attr_cache_attributesfile(attrcache); + + + if (attr_cfg_file) { + error = push_attr_file(repo, attr_session, files, NULL, attr_cfg_file); + + if (error < 0) + goto cleanup; + } + + if (!opts || (opts->flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) { + error = system_attr_file(&dir, attr_session); + + if (!error) + error = push_attr_file(repo, attr_session, files, NULL, dir.ptr); + else if (error == GIT_ENOTFOUND) + error = 0; + } + + cleanup: + if (error < 0) + release_attr_files(files); + git_str_dispose(&attrfile); + git_str_dispose(&dir); + + return error; +} diff --git a/src/libgit2/attr.h b/src/libgit2/attr.h new file mode 100644 index 00000000000..9775652057f --- /dev/null +++ b/src/libgit2/attr.h @@ -0,0 +1,15 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_attr_h__ +#define INCLUDE_attr_h__ + +#include "common.h" + +#include "attr_file.h" +#include "attrcache.h" + +#endif diff --git a/src/libgit2/attr_file.c b/src/libgit2/attr_file.c new file mode 100644 index 00000000000..d52a11590e1 --- /dev/null +++ b/src/libgit2/attr_file.c @@ -0,0 +1,1039 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "attr_file.h" + +#include "repository.h" +#include "filebuf.h" +#include "attrcache.h" +#include "git2/blob.h" +#include "git2/tree.h" +#include "blob.h" +#include "index.h" +#include "wildmatch.h" +#include + +static void attr_file_free(git_attr_file *file) +{ + bool unlock = !git_mutex_lock(&file->lock); + git_attr_file__clear_rules(file, false); + git_pool_clear(&file->pool); + if (unlock) + git_mutex_unlock(&file->lock); + git_mutex_free(&file->lock); + + git__memzero(file, sizeof(*file)); + git__free(file); +} + +int git_attr_file__new( + git_attr_file **out, + git_attr_file_entry *entry, + git_attr_file_source *source) +{ + git_attr_file *attrs = git__calloc(1, sizeof(git_attr_file)); + GIT_ERROR_CHECK_ALLOC(attrs); + + if (git_mutex_init(&attrs->lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to initialize lock"); + goto on_error; + } + + if (git_pool_init(&attrs->pool, 1) < 0) + goto on_error; + + GIT_REFCOUNT_INC(attrs); + attrs->entry = entry; + memcpy(&attrs->source, source, sizeof(git_attr_file_source)); + *out = attrs; + return 0; + +on_error: + git__free(attrs); + return -1; +} + +int git_attr_file__clear_rules(git_attr_file *file, bool need_lock) +{ + unsigned int i; + git_attr_rule *rule; + + if (need_lock && git_mutex_lock(&file->lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock attribute file"); + return -1; + } + + git_vector_foreach(&file->rules, i, rule) + git_attr_rule__free(rule); + git_vector_dispose(&file->rules); + + if (need_lock) + git_mutex_unlock(&file->lock); + + return 0; +} + +void git_attr_file__free(git_attr_file *file) +{ + if (!file) + return; + GIT_REFCOUNT_DEC(file, attr_file_free); +} + +static int attr_file_oid_from_index( + git_oid *oid, git_repository *repo, const char *path) +{ + int error; + git_index *idx; + size_t pos; + const git_index_entry *entry; + + if ((error = git_repository_index__weakptr(&idx, repo)) < 0 || + (error = git_index__find_pos(&pos, idx, path, 0, 0)) < 0) + return error; + + if (!(entry = git_index_get_byindex(idx, pos))) + return GIT_ENOTFOUND; + + *oid = entry->id; + return 0; +} + +int git_attr_file__load( + git_attr_file **out, + git_repository *repo, + git_attr_session *attr_session, + git_attr_file_entry *entry, + git_attr_file_source *source, + git_attr_file_parser parser, + bool allow_macros) +{ + int error = 0; + git_commit *commit = NULL; + git_tree *tree = NULL; + git_tree_entry *tree_entry = NULL; + git_blob *blob = NULL; + git_str content = GIT_STR_INIT; + const char *content_str; + git_attr_file *file; + struct stat st; + bool nonexistent = false; + int bom_offset; + git_str_bom_t bom; + git_oid id; + git_object_size_t blobsize; + + *out = NULL; + + switch (source->type) { + case GIT_ATTR_FILE_SOURCE_MEMORY: + /* in-memory attribute file doesn't need data */ + break; + case GIT_ATTR_FILE_SOURCE_INDEX: { + if ((error = attr_file_oid_from_index(&id, repo, entry->path)) < 0 || + (error = git_blob_lookup(&blob, repo, &id)) < 0) + return error; + + /* Do not assume that data straight from the ODB is NULL-terminated; + * copy the contents of a file to a buffer to work on */ + blobsize = git_blob_rawsize(blob); + + GIT_ERROR_CHECK_BLOBSIZE(blobsize); + if (blobsize > GIT_ATTR_MAX_FILE_SIZE) /* TODO: issue warning when warning API is available */ + goto cleanup; + git_str_put(&content, git_blob_rawcontent(blob), (size_t)blobsize); + break; + } + case GIT_ATTR_FILE_SOURCE_FILE: { + int fd = -1; + + /* For open or read errors, pretend that we got ENOTFOUND. */ + /* TODO: issue warning when warning API is available */ + + if (p_stat(entry->fullpath, &st) < 0 || + S_ISDIR(st.st_mode) || + (fd = git_futils_open_ro(entry->fullpath)) < 0 || + (st.st_size > GIT_ATTR_MAX_FILE_SIZE) || + (error = git_futils_readbuffer_fd(&content, fd, (size_t)st.st_size)) < 0) + nonexistent = true; + + if (fd >= 0) + p_close(fd); + + break; + } + case GIT_ATTR_FILE_SOURCE_HEAD: + case GIT_ATTR_FILE_SOURCE_COMMIT: { + if (source->type == GIT_ATTR_FILE_SOURCE_COMMIT) { + if ((error = git_commit_lookup(&commit, repo, source->commit_id)) < 0 || + (error = git_commit_tree(&tree, commit)) < 0) + goto cleanup; + } else { + if ((error = git_repository_head_tree(&tree, repo)) < 0) + goto cleanup; + } + + if ((error = git_tree_entry_bypath(&tree_entry, tree, entry->path)) < 0) { + /* + * If the attributes file does not exist, we can + * cache an empty file for this commit to prevent + * needless future lookups. + */ + if (error == GIT_ENOTFOUND) { + error = 0; + break; + } + + goto cleanup; + } + + if ((error = git_blob_lookup(&blob, repo, git_tree_entry_id(tree_entry))) < 0) + goto cleanup; + + /* + * Do not assume that data straight from the ODB is NULL-terminated; + * copy the contents of a file to a buffer to work on. + */ + blobsize = git_blob_rawsize(blob); + + GIT_ERROR_CHECK_BLOBSIZE(blobsize); + if (blobsize > GIT_ATTR_MAX_FILE_SIZE) /* TODO: issue warning when warning API is available */ + goto cleanup; + if ((error = git_str_put(&content, + git_blob_rawcontent(blob), (size_t)blobsize)) < 0) + goto cleanup; + + break; + } + default: + git_error_set(GIT_ERROR_INVALID, "unknown file source %d", source->type); + return -1; + } + + if ((error = git_attr_file__new(&file, entry, source)) < 0) + goto cleanup; + + /* advance over a UTF8 BOM */ + content_str = git_str_cstr(&content); + bom_offset = git_str_detect_bom(&bom, &content); + + if (bom == GIT_STR_BOM_UTF8) + content_str += bom_offset; + + /* store the key of the attr_reader; don't bother with cache + * invalidation during the same attr reader session. + */ + if (attr_session) + file->session_key = attr_session->key; + + if (parser && (error = parser(repo, file, content_str, allow_macros)) < 0) { + git_attr_file__free(file); + goto cleanup; + } + + /* write cache breakers */ + if (nonexistent) + file->nonexistent = 1; + else if (source->type == GIT_ATTR_FILE_SOURCE_INDEX) + git_oid_cpy(&file->cache_data.oid, git_blob_id(blob)); + else if (source->type == GIT_ATTR_FILE_SOURCE_HEAD) + git_oid_cpy(&file->cache_data.oid, git_tree_id(tree)); + else if (source->type == GIT_ATTR_FILE_SOURCE_COMMIT) + git_oid_cpy(&file->cache_data.oid, git_tree_id(tree)); + else if (source->type == GIT_ATTR_FILE_SOURCE_FILE) + git_futils_filestamp_set_from_stat(&file->cache_data.stamp, &st); + /* else always cacheable */ + + *out = file; + +cleanup: + git_blob_free(blob); + git_tree_entry_free(tree_entry); + git_tree_free(tree); + git_commit_free(commit); + git_str_dispose(&content); + + return error; +} + +int git_attr_file__out_of_date( + git_repository *repo, + git_attr_session *attr_session, + git_attr_file *file, + git_attr_file_source *source) +{ + if (!file) + return 1; + + /* we are never out of date if we just created this data in the same + * attr_session; otherwise, nonexistent files must be invalidated + */ + if (attr_session && attr_session->key == file->session_key) + return 0; + else if (file->nonexistent) + return 1; + + switch (file->source.type) { + case GIT_ATTR_FILE_SOURCE_MEMORY: + return 0; + + case GIT_ATTR_FILE_SOURCE_FILE: + return git_futils_filestamp_check( + &file->cache_data.stamp, file->entry->fullpath); + + case GIT_ATTR_FILE_SOURCE_INDEX: { + int error; + git_oid id; + + if ((error = attr_file_oid_from_index( + &id, repo, file->entry->path)) < 0) + return error; + + return (git_oid__cmp(&file->cache_data.oid, &id) != 0); + } + + case GIT_ATTR_FILE_SOURCE_HEAD: { + git_tree *tree = NULL; + int error = git_repository_head_tree(&tree, repo); + + if (error < 0) + return error; + + error = (git_oid__cmp(&file->cache_data.oid, git_tree_id(tree)) != 0); + + git_tree_free(tree); + return error; + } + + case GIT_ATTR_FILE_SOURCE_COMMIT: { + git_commit *commit = NULL; + git_tree *tree = NULL; + int error; + + if ((error = git_commit_lookup(&commit, repo, source->commit_id)) < 0) + return error; + + error = git_commit_tree(&tree, commit); + git_commit_free(commit); + + if (error < 0) + return error; + + error = (git_oid__cmp(&file->cache_data.oid, git_tree_id(tree)) != 0); + + git_tree_free(tree); + return error; + } + + default: + git_error_set(GIT_ERROR_INVALID, "invalid file type %d", file->source.type); + return -1; + } +} + +static int sort_by_hash_and_name(const void *a_raw, const void *b_raw); +static void git_attr_rule__clear(git_attr_rule *rule); +static bool parse_optimized_patterns( + git_attr_fnmatch *spec, + git_pool *pool, + const char *pattern); + +int git_attr_file__parse_buffer( + git_repository *repo, git_attr_file *attrs, const char *data, bool allow_macros) +{ + const char *scan = data, *context = NULL; + git_attr_rule *rule = NULL; + int ignorecase = 0, error = 0; + + /* If subdir file path, convert context for file paths */ + if (attrs->entry && git_fs_path_root(attrs->entry->path) < 0 && + !git__suffixcmp(attrs->entry->path, "/" GIT_ATTR_FILE)) + context = attrs->entry->path; + + if (git_mutex_lock(&attrs->lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock attribute file"); + return -1; + } + + while (!error && *scan) { + /* Allocate rule if needed, otherwise re-use previous rule */ + if (!rule) { + rule = git__calloc(1, sizeof(*rule)); + GIT_ERROR_CHECK_ALLOC(rule); + } else + git_attr_rule__clear(rule); + + rule->match.flags = GIT_ATTR_FNMATCH_ALLOWNEG | GIT_ATTR_FNMATCH_ALLOWMACRO; + + /* Parse the next "pattern attr attr attr" line */ + if ((error = git_attr_fnmatch__parse(&rule->match, &attrs->pool, context, &scan)) < 0 || + (error = git_attr_assignment__parse(repo, &attrs->pool, &rule->assigns, &scan)) < 0) + { + if (error != GIT_ENOTFOUND) + goto out; + error = 0; + continue; + } + + if (repo && + (error = git_repository__configmap_lookup(&ignorecase, repo, GIT_CONFIGMAP_IGNORECASE)) < 0) + goto out; + + if (ignorecase) + rule->match.flags |= GIT_ATTR_FNMATCH_ICASE; + + if (rule->match.flags & GIT_ATTR_FNMATCH_MACRO) { + /* TODO: warning if macro found in file below repo root */ + if (!allow_macros) + continue; + if ((error = git_attr_cache__insert_macro(repo, rule)) < 0) + goto out; + } else if ((error = git_vector_insert(&attrs->rules, rule)) < 0) + goto out; + + rule = NULL; + } + +out: + git_mutex_unlock(&attrs->lock); + git_attr_rule__free(rule); + + return error; +} + +uint32_t git_attr_file__name_hash(const char *name) +{ + uint32_t h = 5381; + int c; + + GIT_ASSERT_ARG(name); + + while ((c = (int)*name++) != 0) + h = ((h << 5) + h) + c; + return h; +} + +int git_attr_file__lookup_one( + git_attr_file *file, + git_attr_path *path, + const char *attr, + const char **value) +{ + size_t i; + git_attr_name name; + git_attr_rule *rule; + + *value = NULL; + + name.name = attr; + name.name_hash = git_attr_file__name_hash(attr); + + git_attr_file__foreach_matching_rule(file, path, i, rule) { + size_t pos; + + if (!git_vector_bsearch(&pos, &rule->assigns, &name)) { + *value = ((git_attr_assignment *) + git_vector_get(&rule->assigns, pos))->value; + break; + } + } + + return 0; +} + +int git_attr_file__load_standalone(git_attr_file **out, const char *path) +{ + git_str content = GIT_STR_INIT; + git_attr_file_source source = { GIT_ATTR_FILE_SOURCE_FILE }; + git_attr_file *file = NULL; + int error; + + if ((error = git_futils_readbuffer(&content, path)) < 0) + goto out; + + /* + * Because the cache entry is allocated from the file's own pool, we + * don't have to free it - freeing file+pool will free cache entry, too. + */ + + if ((error = git_attr_file__new(&file, NULL, &source)) < 0 || + (error = git_attr_file__parse_buffer(NULL, file, content.ptr, true)) < 0 || + (error = git_attr_cache__alloc_file_entry(&file->entry, NULL, NULL, path, &file->pool)) < 0) + goto out; + + *out = file; +out: + if (error < 0) + git_attr_file__free(file); + git_str_dispose(&content); + + return error; +} + +bool git_attr_fnmatch__match( + git_attr_fnmatch *match, + git_attr_path *path) +{ + const char *relpath = path->path; + const char *filename; + int flags = 0; + + /* + * If the rule was generated in a subdirectory, we must only + * use it for paths inside that directory. We can thus return + * a non-match if the prefixes don't match. + */ + if (match->containing_dir) { + if (match->flags & GIT_ATTR_FNMATCH_ICASE) { + if (git__prefixcmp_icase(path->path, match->containing_dir)) + return 0; + } else { + if (git__prefixcmp(path->path, match->containing_dir)) + return 0; + } + + relpath += match->containing_dir_length; + } + + if (match->flags & GIT_ATTR_FNMATCH_ICASE) + flags |= WM_CASEFOLD; + + if (match->flags & GIT_ATTR_FNMATCH_FULLPATH) { + filename = relpath; + flags |= WM_PATHNAME; + } else { + filename = path->basename; + } + + if ((match->flags & GIT_ATTR_FNMATCH_DIRECTORY) && !path->is_dir) { + bool samename; + + /* + * for attribute checks or checks at the root of this match's + * containing_dir (or root of the repository if no containing_dir), + * do not match. + */ + if (!(match->flags & GIT_ATTR_FNMATCH_IGNORE) || + path->basename == relpath) + return false; + + /* fail match if this is a file with same name as ignored folder */ + samename = (match->flags & GIT_ATTR_FNMATCH_ICASE) ? + !strcasecmp(match->pattern, relpath) : + !strcmp(match->pattern, relpath); + + if (samename) + return false; + + return (wildmatch(match->pattern, relpath, flags) == WM_MATCH); + } + + return (wildmatch(match->pattern, filename, flags) == WM_MATCH); +} + +bool git_attr_rule__match( + git_attr_rule *rule, + git_attr_path *path) +{ + bool matched = git_attr_fnmatch__match(&rule->match, path); + + if (rule->match.flags & GIT_ATTR_FNMATCH_NEGATIVE) + matched = !matched; + + return matched; +} + +git_attr_assignment *git_attr_rule__lookup_assignment( + git_attr_rule *rule, const char *name) +{ + size_t pos; + git_attr_name key; + key.name = name; + key.name_hash = git_attr_file__name_hash(name); + + if (git_vector_bsearch(&pos, &rule->assigns, &key)) + return NULL; + + return git_vector_get(&rule->assigns, pos); +} + +int git_attr_path__init( + git_attr_path *info, + const char *path, + const char *base, + git_dir_flag dir_flag) +{ + ssize_t root; + + /* build full path as best we can */ + git_str_init(&info->full, 0); + + if (git_fs_path_join_unrooted(&info->full, path, base, &root) < 0) + return -1; + + info->path = info->full.ptr + root; + + /* remove trailing slashes */ + while (info->full.size > 0) { + if (info->full.ptr[info->full.size - 1] != '/') + break; + info->full.size--; + } + info->full.ptr[info->full.size] = '\0'; + + /* skip leading slashes in path */ + while (*info->path == '/') + info->path++; + + /* find trailing basename component */ + info->basename = strrchr(info->path, '/'); + if (info->basename) + info->basename++; + if (!info->basename || !*info->basename) + info->basename = info->path; + + switch (dir_flag) + { + case GIT_DIR_FLAG_FALSE: + info->is_dir = 0; + break; + + case GIT_DIR_FLAG_TRUE: + info->is_dir = 1; + break; + + case GIT_DIR_FLAG_UNKNOWN: + default: + info->is_dir = (int)git_fs_path_isdir(info->full.ptr); + break; + } + + return 0; +} + +void git_attr_path__free(git_attr_path *info) +{ + git_str_dispose(&info->full); + info->path = NULL; + info->basename = NULL; +} + +/* + * From gitattributes(5): + * + * Patterns have the following format: + * + * - A blank line matches no files, so it can serve as a separator for + * readability. + * + * - A line starting with # serves as a comment. + * + * - An optional prefix ! which negates the pattern; any matching file + * excluded by a previous pattern will become included again. If a negated + * pattern matches, this will override lower precedence patterns sources. + * + * - If the pattern ends with a slash, it is removed for the purpose of the + * following description, but it would only find a match with a directory. In + * other words, foo/ will match a directory foo and paths underneath it, but + * will not match a regular file or a symbolic link foo (this is consistent + * with the way how pathspec works in general in git). + * + * - If the pattern does not contain a slash /, git treats it as a shell glob + * pattern and checks for a match against the pathname without leading + * directories. + * + * - Otherwise, git treats the pattern as a shell glob suitable for consumption + * by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will + * not match a / in the pathname. For example, "Documentation/\*.html" matches + * "Documentation/git.html" but not "Documentation/ppc/ppc.html". A leading + * slash matches the beginning of the pathname; for example, "/\*.c" matches + * "cat-file.c" but not "mozilla-sha1/sha1.c". + */ + +/* + * Determine the length of trailing spaces. Escaped spaces do not count as + * trailing whitespace. + */ +static size_t trailing_space_length(const char *p, size_t len) +{ + size_t n, i; + for (n = len; n; n--) { + if (p[n-1] != ' ' && p[n-1] != '\t') + break; + + /* + * Count escape-characters before space. In case where it's an + * even number of escape characters, then the escape char itself + * is escaped and the whitespace is an unescaped whitespace. + * Otherwise, the last escape char is not escaped and the + * whitespace in an escaped whitespace. + */ + i = n; + while (i > 1 && p[i-2] == '\\') + i--; + if ((n - i) % 2) + break; + } + return len - n; +} + +static size_t unescape_spaces(char *str) +{ + char *scan, *pos = str; + bool escaped = false; + + if (!str) + return 0; + + for (scan = str; *scan; scan++) { + if (!escaped && *scan == '\\') { + escaped = true; + continue; + } + + /* Only insert the escape character for escaped non-spaces */ + if (escaped && !git__isspace(*scan)) + *pos++ = '\\'; + + *pos++ = *scan; + escaped = false; + } + + if (pos != scan) + *pos = '\0'; + + return (pos - str); +} + +/* + * This will return 0 if the spec was filled out, + * GIT_ENOTFOUND if the fnmatch does not require matching, or + * another error code there was an actual problem. + */ +int git_attr_fnmatch__parse( + git_attr_fnmatch *spec, + git_pool *pool, + const char *context, + const char **base) +{ + const char *pattern, *scan; + int slash_count, allow_space; + bool escaped; + + GIT_ASSERT_ARG(spec); + GIT_ASSERT_ARG(base && *base); + + if (parse_optimized_patterns(spec, pool, *base)) + return 0; + + spec->flags = (spec->flags & GIT_ATTR_FNMATCH__INCOMING); + allow_space = ((spec->flags & GIT_ATTR_FNMATCH_ALLOWSPACE) != 0); + + pattern = *base; + + while (!allow_space && git__isspace(*pattern)) + pattern++; + + if (!*pattern || *pattern == '#' || *pattern == '\n' || + (*pattern == '\r' && *(pattern + 1) == '\n')) { + *base = git__next_line(pattern); + return GIT_ENOTFOUND; + } + + if (*pattern == '[' && (spec->flags & GIT_ATTR_FNMATCH_ALLOWMACRO) != 0) { + if (strncmp(pattern, "[attr]", 6) == 0) { + spec->flags = spec->flags | GIT_ATTR_FNMATCH_MACRO; + pattern += 6; + } + /* else a character range like [a-e]* which is accepted */ + } + + if (*pattern == '!' && (spec->flags & GIT_ATTR_FNMATCH_ALLOWNEG) != 0) { + spec->flags = spec->flags | GIT_ATTR_FNMATCH_NEGATIVE; + pattern++; + } + + slash_count = 0; + escaped = false; + /* Scan until a non-escaped whitespace. */ + for (scan = pattern; *scan != '\0'; ++scan) { + char c = *scan; + + if (c == '\\' && !escaped) { + escaped = true; + continue; + } else if (git__isspace(c) && !escaped) { + if (!allow_space || (c != ' ' && c != '\t' && c != '\r')) + break; + } else if (c == '/') { + spec->flags = spec->flags | GIT_ATTR_FNMATCH_FULLPATH; + slash_count++; + + if (slash_count == 1 && pattern == scan) + pattern++; + } else if (git__iswildcard(c) && !escaped) { + /* remember if we see an unescaped wildcard in pattern */ + spec->flags = spec->flags | GIT_ATTR_FNMATCH_HASWILD; + } + + escaped = false; + } + + *base = scan; + + if ((spec->length = scan - pattern) == 0) + return GIT_ENOTFOUND; + + /* + * Remove one trailing \r in case this is a CRLF delimited + * file, in the case of Icon\r\r\n, we still leave the first + * \r there to match against. + */ + if (pattern[spec->length - 1] == '\r') + if (--spec->length == 0) + return GIT_ENOTFOUND; + + /* Remove trailing spaces. */ + spec->length -= trailing_space_length(pattern, spec->length); + + if (spec->length == 0) + return GIT_ENOTFOUND; + + if (pattern[spec->length - 1] == '/') { + spec->length--; + spec->flags = spec->flags | GIT_ATTR_FNMATCH_DIRECTORY; + if (--slash_count <= 0) + spec->flags = spec->flags & ~GIT_ATTR_FNMATCH_FULLPATH; + } + + if (context) { + const char *slash = strrchr(context, '/'); + size_t len; + if (slash) { + /* include the slash for easier matching */ + len = slash - context + 1; + spec->containing_dir = git_pool_strndup(pool, context, len); + spec->containing_dir_length = len; + } + } + + spec->pattern = git_pool_strndup(pool, pattern, spec->length); + + if (!spec->pattern) { + *base = git__next_line(pattern); + return -1; + } else { + /* strip '\' that might have been used for internal whitespace */ + spec->length = unescape_spaces(spec->pattern); + } + + return 0; +} + +static bool parse_optimized_patterns( + git_attr_fnmatch *spec, + git_pool *pool, + const char *pattern) +{ + if (!pattern[1] && (pattern[0] == '*' || pattern[0] == '.')) { + spec->flags = GIT_ATTR_FNMATCH_MATCH_ALL; + spec->pattern = git_pool_strndup(pool, pattern, 1); + spec->length = 1; + + return true; + } + + return false; +} + +static int sort_by_hash_and_name(const void *a_raw, const void *b_raw) +{ + const git_attr_name *a = a_raw; + const git_attr_name *b = b_raw; + + if (b->name_hash < a->name_hash) + return 1; + else if (b->name_hash > a->name_hash) + return -1; + else + return strcmp(b->name, a->name); +} + +static void git_attr_assignment__free(git_attr_assignment *assign) +{ + /* name and value are stored in a git_pool associated with the + * git_attr_file, so they do not need to be freed here + */ + assign->name = NULL; + assign->value = NULL; + git__free(assign); +} + +static int merge_assignments(void **old_raw, void *new_raw) +{ + git_attr_assignment **old = (git_attr_assignment **)old_raw; + git_attr_assignment *new = (git_attr_assignment *)new_raw; + + GIT_REFCOUNT_DEC(*old, git_attr_assignment__free); + *old = new; + return GIT_EEXISTS; +} + +int git_attr_assignment__parse( + git_repository *repo, + git_pool *pool, + git_vector *assigns, + const char **base) +{ + int error; + const char *scan = *base; + git_attr_assignment *assign = NULL; + + GIT_ASSERT_ARG(assigns && !assigns->length); + + git_vector_set_cmp(assigns, sort_by_hash_and_name); + + while (*scan && *scan != '\n') { + const char *name_start, *value_start; + + /* skip leading blanks */ + while (git__isspace(*scan) && *scan != '\n') scan++; + + /* allocate assign if needed */ + if (!assign) { + assign = git__calloc(1, sizeof(git_attr_assignment)); + GIT_ERROR_CHECK_ALLOC(assign); + GIT_REFCOUNT_INC(assign); + } + + assign->name_hash = 5381; + assign->value = git_attr__true; + + /* look for magic name prefixes */ + if (*scan == '-') { + assign->value = git_attr__false; + scan++; + } else if (*scan == '!') { + assign->value = git_attr__unset; /* explicit unspecified state */ + scan++; + } else if (*scan == '#') /* comment rest of line */ + break; + + /* find the name */ + name_start = scan; + while (*scan && !git__isspace(*scan) && *scan != '=') { + assign->name_hash = + ((assign->name_hash << 5) + assign->name_hash) + *scan; + scan++; + } + if (scan == name_start) { + /* must have found lone prefix (" - ") or leading = ("=foo") + * or end of buffer -- advance until whitespace and continue + */ + while (*scan && !git__isspace(*scan)) scan++; + continue; + } + + /* allocate permanent storage for name */ + assign->name = git_pool_strndup(pool, name_start, scan - name_start); + GIT_ERROR_CHECK_ALLOC(assign->name); + + /* if there is an equals sign, find the value */ + if (*scan == '=') { + for (value_start = ++scan; *scan && !git__isspace(*scan); ++scan); + + /* if we found a value, allocate permanent storage for it */ + if (scan > value_start) { + assign->value = git_pool_strndup(pool, value_start, scan - value_start); + GIT_ERROR_CHECK_ALLOC(assign->value); + } + } + + /* expand macros (if given a repo with a macro cache) */ + if (repo != NULL && assign->value == git_attr__true) { + git_attr_rule *macro = + git_attr_cache__lookup_macro(repo, assign->name); + + if (macro != NULL) { + unsigned int i; + git_attr_assignment *massign; + + git_vector_foreach(¯o->assigns, i, massign) { + GIT_REFCOUNT_INC(massign); + + error = git_vector_insert_sorted( + assigns, massign, &merge_assignments); + if (error < 0 && error != GIT_EEXISTS) { + git_attr_assignment__free(assign); + return error; + } + } + } + } + + /* insert allocated assign into vector */ + error = git_vector_insert_sorted(assigns, assign, &merge_assignments); + if (error < 0 && error != GIT_EEXISTS) + return error; + + /* clear assign since it is now "owned" by the vector */ + assign = NULL; + } + + if (assign != NULL) + git_attr_assignment__free(assign); + + *base = git__next_line(scan); + + return (assigns->length == 0) ? GIT_ENOTFOUND : 0; +} + +static void git_attr_rule__clear(git_attr_rule *rule) +{ + unsigned int i; + git_attr_assignment *assign; + + if (!rule) + return; + + if (!(rule->match.flags & GIT_ATTR_FNMATCH_IGNORE)) { + git_vector_foreach(&rule->assigns, i, assign) + GIT_REFCOUNT_DEC(assign, git_attr_assignment__free); + git_vector_dispose(&rule->assigns); + } + + /* match.pattern is stored in a git_pool, so no need to free */ + rule->match.pattern = NULL; + rule->match.length = 0; +} + +void git_attr_rule__free(git_attr_rule *rule) +{ + git_attr_rule__clear(rule); + git__free(rule); +} + +int git_attr_session__init(git_attr_session *session, git_repository *repo) +{ + GIT_ASSERT_ARG(repo); + + memset(session, 0, sizeof(*session)); + session->key = git_atomic32_inc(&repo->attr_session_key); + + return 0; +} + +void git_attr_session__free(git_attr_session *session) +{ + if (!session) + return; + + git_str_dispose(&session->sysdir); + git_str_dispose(&session->tmp); + + memset(session, 0, sizeof(git_attr_session)); +} diff --git a/src/libgit2/attr_file.h b/src/libgit2/attr_file.h new file mode 100644 index 00000000000..8025359ba9b --- /dev/null +++ b/src/libgit2/attr_file.h @@ -0,0 +1,243 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_attr_file_h__ +#define INCLUDE_attr_file_h__ + +#include "common.h" + +#include "git2/oid.h" +#include "git2/attr.h" +#include "vector.h" +#include "pool.h" +#include "str.h" +#include "futils.h" + +#define GIT_ATTR_FILE ".gitattributes" +#define GIT_ATTR_FILE_INREPO "attributes" +#define GIT_ATTR_FILE_SYSTEM "gitattributes" +#define GIT_ATTR_FILE_XDG "attributes" + +#define GIT_ATTR_MAX_FILE_SIZE 100 * 1024 * 1024 + +#define GIT_ATTR_FNMATCH_NEGATIVE (1U << 0) +#define GIT_ATTR_FNMATCH_DIRECTORY (1U << 1) +#define GIT_ATTR_FNMATCH_FULLPATH (1U << 2) +#define GIT_ATTR_FNMATCH_MACRO (1U << 3) +#define GIT_ATTR_FNMATCH_IGNORE (1U << 4) +#define GIT_ATTR_FNMATCH_HASWILD (1U << 5) +#define GIT_ATTR_FNMATCH_ALLOWSPACE (1U << 6) +#define GIT_ATTR_FNMATCH_ICASE (1U << 7) +#define GIT_ATTR_FNMATCH_MATCH_ALL (1U << 8) +#define GIT_ATTR_FNMATCH_ALLOWNEG (1U << 9) +#define GIT_ATTR_FNMATCH_ALLOWMACRO (1U << 10) + +#define GIT_ATTR_FNMATCH__INCOMING \ + (GIT_ATTR_FNMATCH_ALLOWSPACE | GIT_ATTR_FNMATCH_ALLOWNEG | GIT_ATTR_FNMATCH_ALLOWMACRO) + +typedef enum { + GIT_ATTR_FILE_SOURCE_MEMORY = 0, + GIT_ATTR_FILE_SOURCE_FILE = 1, + GIT_ATTR_FILE_SOURCE_INDEX = 2, + GIT_ATTR_FILE_SOURCE_HEAD = 3, + GIT_ATTR_FILE_SOURCE_COMMIT = 4, + + GIT_ATTR_FILE_NUM_SOURCES = 5 +} git_attr_file_source_t; + +typedef struct { + /* The source location for the attribute file. */ + git_attr_file_source_t type; + + /* + * The filename of the attribute file to read (relative to the + * given base path). + */ + const char *base; + const char *filename; + + /* + * The commit ID when the given source type is a commit (or NULL + * for the repository's HEAD commit.) + */ + git_oid *commit_id; +} git_attr_file_source; + +extern const char *git_attr__true; +extern const char *git_attr__false; +extern const char *git_attr__unset; + +typedef struct { + char *pattern; + size_t length; + char *containing_dir; + size_t containing_dir_length; + unsigned int flags; +} git_attr_fnmatch; + +typedef struct { + git_attr_fnmatch match; + git_vector assigns; /* vector of */ +} git_attr_rule; + +typedef struct { + git_refcount unused; + const char *name; + uint32_t name_hash; +} git_attr_name; + +typedef struct { + git_refcount rc; /* for macros */ + char *name; + uint32_t name_hash; + const char *value; +} git_attr_assignment; + +typedef struct git_attr_file_entry git_attr_file_entry; + +typedef struct { + git_refcount rc; + git_mutex lock; + git_attr_file_entry *entry; + git_attr_file_source source; + git_vector rules; /* vector of or */ + git_pool pool; + unsigned int nonexistent:1; + int session_key; + union { + git_oid oid; + git_futils_filestamp stamp; + } cache_data; +} git_attr_file; + +struct git_attr_file_entry { + git_attr_file *file[GIT_ATTR_FILE_NUM_SOURCES]; + const char *path; /* points into fullpath */ + char fullpath[GIT_FLEX_ARRAY]; +}; + +typedef struct { + git_str full; + char *path; + char *basename; + int is_dir; +} git_attr_path; + +/* A git_attr_session can provide an "instance" of reading, to prevent cache + * invalidation during a single operation instance (like checkout). + */ + +typedef struct { + int key; + unsigned int init_setup:1, + init_sysdir:1; + git_str sysdir; + git_str tmp; +} git_attr_session; + +extern int git_attr_session__init(git_attr_session *attr_session, git_repository *repo); +extern void git_attr_session__free(git_attr_session *session); + +extern int git_attr_get_many_with_session( + const char **values_out, + git_repository *repo, + git_attr_session *attr_session, + git_attr_options *opts, + const char *path, + size_t num_attr, + const char **names); + +typedef int (*git_attr_file_parser)( + git_repository *repo, + git_attr_file *file, + const char *data, + bool allow_macros); + +/* + * git_attr_file API + */ + +int git_attr_file__new( + git_attr_file **out, + git_attr_file_entry *entry, + git_attr_file_source *source); + +void git_attr_file__free(git_attr_file *file); + +int git_attr_file__load( + git_attr_file **out, + git_repository *repo, + git_attr_session *attr_session, + git_attr_file_entry *ce, + git_attr_file_source *source, + git_attr_file_parser parser, + bool allow_macros); + +int git_attr_file__load_standalone( + git_attr_file **out, const char *path); + +int git_attr_file__out_of_date( + git_repository *repo, git_attr_session *session, git_attr_file *file, git_attr_file_source *source); + +int git_attr_file__parse_buffer( + git_repository *repo, git_attr_file *attrs, const char *data, bool allow_macros); + +int git_attr_file__clear_rules( + git_attr_file *file, bool need_lock); + +int git_attr_file__lookup_one( + git_attr_file *file, + git_attr_path *path, + const char *attr, + const char **value); + +/* loop over rules in file from bottom to top */ +#define git_attr_file__foreach_matching_rule(file, path, iter, rule) \ + git_vector_rforeach(&(file)->rules, (iter), (rule)) \ + if (git_attr_rule__match((rule), (path))) + +uint32_t git_attr_file__name_hash(const char *name); + + +/* + * other utilities + */ + +extern int git_attr_fnmatch__parse( + git_attr_fnmatch *spec, + git_pool *pool, + const char *source, + const char **base); + +extern bool git_attr_fnmatch__match( + git_attr_fnmatch *rule, + git_attr_path *path); + +extern void git_attr_rule__free(git_attr_rule *rule); + +extern bool git_attr_rule__match( + git_attr_rule *rule, + git_attr_path *path); + +extern git_attr_assignment *git_attr_rule__lookup_assignment( + git_attr_rule *rule, const char *name); + +typedef enum { GIT_DIR_FLAG_TRUE = 1, GIT_DIR_FLAG_FALSE = 0, GIT_DIR_FLAG_UNKNOWN = -1 } git_dir_flag; + +extern int git_attr_path__init( + git_attr_path *out, + const char *path, + const char *base, + git_dir_flag is_dir); +extern void git_attr_path__free(git_attr_path *info); + +extern int git_attr_assignment__parse( + git_repository *repo, /* needed to expand macros */ + git_pool *pool, + git_vector *assigns, + const char **scan); + +#endif diff --git a/src/libgit2/attrcache.c b/src/libgit2/attrcache.c new file mode 100644 index 00000000000..2a822147a65 --- /dev/null +++ b/src/libgit2/attrcache.c @@ -0,0 +1,514 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "attrcache.h" + +#include "repository.h" +#include "attr_file.h" +#include "config.h" +#include "sysdir.h" +#include "ignore.h" +#include "path.h" +#include "hashmap_str.h" + +GIT_HASHMAP_STR_SETUP(git_attr_cache_filemap, git_attr_file_entry *); +GIT_HASHMAP_STR_SETUP(git_attr_cache_macromap, git_attr_rule *); + +struct git_attr_cache { + char *cfg_attr_file; /* cached value of core.attributesfile */ + char *cfg_excl_file; /* cached value of core.excludesfile */ + + /* hash path to git_attr_file_entry records */ + git_attr_cache_filemap files; + /* hash name to git_attr_rule */ + git_attr_cache_macromap macros; + + git_mutex lock; + git_pool pool; +}; + +const char *git_attr_cache_attributesfile(git_attr_cache *cache) +{ + return cache->cfg_attr_file; +} + +const char *git_attr_cache_excludesfile(git_attr_cache *cache) +{ + return cache->cfg_excl_file; +} + +git_pool *git_attr_cache_pool(git_attr_cache *cache) +{ + return &cache->pool; +} + +GIT_INLINE(int) attr_cache_lock(git_attr_cache *cache) +{ + GIT_UNUSED(cache); /* avoid warning if threading is off */ + + if (git_mutex_lock(&cache->lock) < 0) { + git_error_set(GIT_ERROR_OS, "unable to get attr cache lock"); + return -1; + } + return 0; +} + +GIT_INLINE(void) attr_cache_unlock(git_attr_cache *cache) +{ + GIT_UNUSED(cache); /* avoid warning if threading is off */ + git_mutex_unlock(&cache->lock); +} + +GIT_INLINE(git_attr_file_entry *) attr_cache_lookup_entry( + git_attr_cache *cache, const char *path) +{ + git_attr_file_entry *result; + + if (git_attr_cache_filemap_get(&result, &cache->files, path) == 0) + return result; + + return NULL; +} + +int git_attr_cache__alloc_file_entry( + git_attr_file_entry **out, + git_repository *repo, + const char *base, + const char *path, + git_pool *pool) +{ + git_str fullpath_str = GIT_STR_INIT; + size_t baselen = 0, pathlen = strlen(path); + size_t cachesize = sizeof(git_attr_file_entry) + pathlen + 1; + git_attr_file_entry *ce; + + if (base != NULL && git_fs_path_root(path) < 0) { + baselen = strlen(base); + cachesize += baselen; + + if (baselen && base[baselen - 1] != '/') + cachesize++; + } + + ce = git_pool_mallocz(pool, cachesize); + GIT_ERROR_CHECK_ALLOC(ce); + + if (baselen) { + memcpy(ce->fullpath, base, baselen); + + if (base[baselen - 1] != '/') + ce->fullpath[baselen++] = '/'; + } + memcpy(&ce->fullpath[baselen], path, pathlen); + + fullpath_str.ptr = ce->fullpath; + fullpath_str.size = pathlen + baselen; + + if (git_path_validate_str_length(repo, &fullpath_str) < 0) + return -1; + + ce->path = &ce->fullpath[baselen]; + *out = ce; + + return 0; +} + +/* call with attrcache locked */ +static int attr_cache_make_entry( + git_attr_file_entry **out, git_repository *repo, const char *path) +{ + git_attr_cache *cache = git_repository_attr_cache(repo); + git_attr_file_entry *entry = NULL; + int error; + + if ((error = git_attr_cache__alloc_file_entry(&entry, repo, + git_repository_workdir(repo), path, &cache->pool)) < 0) + return error; + + if ((error = git_attr_cache_filemap_put(&cache->files, entry->path, entry)) < 0) + return error; + + *out = entry; + return error; +} + +/* insert entry or replace existing if we raced with another thread */ +static int attr_cache_upsert(git_attr_cache *cache, git_attr_file *file) +{ + git_attr_file_entry *entry; + git_attr_file *old; + + if (attr_cache_lock(cache) < 0) + return -1; + + entry = attr_cache_lookup_entry(cache, file->entry->path); + + GIT_REFCOUNT_OWN(file, entry); + GIT_REFCOUNT_INC(file); + + /* + * Replace the existing value if another thread has + * created it in the meantime. + */ + old = git_atomic_swap(entry->file[file->source.type], file); + + if (old) { + GIT_REFCOUNT_OWN(old, NULL); + git_attr_file__free(old); + } + + attr_cache_unlock(cache); + return 0; +} + +static int attr_cache_remove(git_attr_cache *cache, git_attr_file *file) +{ + int error = 0; + git_attr_file_entry *entry; + git_attr_file *oldfile = NULL; + + if (!file) + return 0; + + if ((error = attr_cache_lock(cache)) < 0) + return error; + + if ((entry = attr_cache_lookup_entry(cache, file->entry->path)) != NULL) + oldfile = git_atomic_compare_and_swap(&entry->file[file->source.type], file, NULL); + + attr_cache_unlock(cache); + + if (oldfile == file) { + GIT_REFCOUNT_OWN(file, NULL); + git_attr_file__free(file); + } + + return error; +} + +/* Look up cache entry and file. + * - If entry is not present, create it while the cache is locked. + * - If file is present, increment refcount before returning it, so the + * cache can be unlocked and it won't go away. + */ +static int attr_cache_lookup( + git_attr_file **out_file, + git_attr_file_entry **out_entry, + git_repository *repo, + git_attr_session *attr_session, + git_attr_file_source *source) +{ + int error = 0; + git_str path = GIT_STR_INIT; + const char *wd = git_repository_workdir(repo); + const char *filename; + git_attr_cache *cache = git_repository_attr_cache(repo); + git_attr_file_entry *entry = NULL; + git_attr_file *file = NULL; + + /* join base and path as needed */ + if (source->base != NULL && git_fs_path_root(source->filename) < 0) { + git_str *p = attr_session ? &attr_session->tmp : &path; + + if (git_str_joinpath(p, source->base, source->filename) < 0 || + git_path_validate_str_length(repo, p) < 0) + return -1; + + filename = p->ptr; + } else { + filename = source->filename; + } + + if (wd && !git__prefixcmp(filename, wd)) + filename += strlen(wd); + + /* check cache for existing entry */ + if ((error = attr_cache_lock(cache)) < 0) + goto cleanup; + + entry = attr_cache_lookup_entry(cache, filename); + + if (!entry) { + error = attr_cache_make_entry(&entry, repo, filename); + } else if (entry->file[source->type] != NULL) { + file = entry->file[source->type]; + GIT_REFCOUNT_INC(file); + } + + attr_cache_unlock(cache); + +cleanup: + *out_file = file; + *out_entry = entry; + + git_str_dispose(&path); + return error; +} + +int git_attr_cache__get( + git_attr_file **out, + git_repository *repo, + git_attr_session *attr_session, + git_attr_file_source *source, + git_attr_file_parser parser, + bool allow_macros) +{ + int error = 0; + git_attr_cache *cache = git_repository_attr_cache(repo); + git_attr_file_entry *entry = NULL; + git_attr_file *file = NULL, *updated = NULL; + + if ((error = attr_cache_lookup(&file, &entry, repo, attr_session, source)) < 0) + return error; + + /* load file if we don't have one or if existing one is out of date */ + if (!file || + (error = git_attr_file__out_of_date(repo, attr_session, file, source)) > 0) + error = git_attr_file__load(&updated, repo, attr_session, + entry, source, parser, + allow_macros); + + /* if we loaded the file, insert into and/or update cache */ + if (updated) { + if ((error = attr_cache_upsert(cache, updated)) < 0) { + git_attr_file__free(updated); + } else { + git_attr_file__free(file); /* offset incref from lookup */ + file = updated; + } + } + + /* if file could not be loaded */ + if (error < 0) { + /* remove existing entry */ + if (file) { + attr_cache_remove(cache, file); + git_attr_file__free(file); /* offset incref from lookup */ + file = NULL; + } + /* no error if file simply doesn't exist */ + if (error == GIT_ENOTFOUND) { + git_error_clear(); + error = 0; + } + } + + *out = file; + return error; +} + +bool git_attr_cache__is_cached( + git_repository *repo, + git_attr_file_source_t source_type, + const char *filename) +{ + git_attr_cache *cache = git_repository_attr_cache(repo); + git_attr_file_entry *entry; + + if (!cache) + return false; + + if (git_attr_cache_filemap_get(&entry, &cache->files, filename) != 0) + return false; + + return entry && (entry->file[source_type] != NULL); +} + + +static int attr_cache__lookup_path( + char **out, git_config *cfg, const char *key, const char *fallback) +{ + git_str buf = GIT_STR_INIT; + int error; + git_config_entry *entry = NULL; + + *out = NULL; + + if ((error = git_config__lookup_entry(&entry, cfg, key, false)) < 0) + return error; + + if (entry) { + const char *cfgval = entry->value; + + /* expand leading ~/ as needed */ + if (cfgval && cfgval[0] == '~' && cfgval[1] == '/') { + if (! (error = git_sysdir_expand_homedir_file(&buf, &cfgval[2]))) + *out = git_str_detach(&buf); + } else if (cfgval) { + *out = git__strdup(cfgval); + } + } + else if (!git_sysdir_find_xdg_file(&buf, fallback)) { + *out = git_str_detach(&buf); + } + + git_config_entry_free(entry); + git_str_dispose(&buf); + + return error; +} + +static void attr_cache__free(git_attr_cache *cache) +{ + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + git_attr_rule *rule; + git_attr_file_entry *entry; + bool unlock; + + if (!cache) + return; + + unlock = (attr_cache_lock(cache) == 0); + + while (git_attr_cache_filemap_iterate(&iter, NULL, &entry, &cache->files) == 0) { + git_attr_file *file; + size_t i; + + for (i = 0; i < GIT_ATTR_FILE_NUM_SOURCES; i++) { + if ((file = git_atomic_swap(entry->file[i], NULL)) != NULL) { + GIT_REFCOUNT_OWN(file, NULL); + git_attr_file__free(file); + } + } + } + + iter = GIT_HASHMAP_ITER_INIT; + while (git_attr_cache_macromap_iterate(&iter, NULL, &rule, &cache->macros) == 0) + git_attr_rule__free(rule); + + git_attr_cache_filemap_dispose(&cache->files); + git_attr_cache_macromap_dispose(&cache->macros); + + git_pool_clear(&cache->pool); + + git__free(cache->cfg_attr_file); + cache->cfg_attr_file = NULL; + + git__free(cache->cfg_excl_file); + cache->cfg_excl_file = NULL; + + if (unlock) + attr_cache_unlock(cache); + git_mutex_free(&cache->lock); + + git__free(cache); +} + +int git_attr_cache__init(git_repository *repo) +{ + int ret = 0; + git_attr_cache *cache = git_repository_attr_cache(repo); + git_config *cfg = NULL; + + if (cache) + return 0; + + cache = git__calloc(1, sizeof(git_attr_cache)); + GIT_ERROR_CHECK_ALLOC(cache); + + /* set up lock */ + if (git_mutex_init(&cache->lock) < 0) { + git_error_set(GIT_ERROR_OS, "unable to initialize lock for attr cache"); + git__free(cache); + return -1; + } + + if ((ret = git_repository_config_snapshot(&cfg, repo)) < 0) + goto cancel; + + /* cache config settings for attributes and ignores */ + ret = attr_cache__lookup_path( + &cache->cfg_attr_file, cfg, GIT_ATTR_CONFIG, GIT_ATTR_FILE_XDG); + if (ret < 0) + goto cancel; + + ret = attr_cache__lookup_path( + &cache->cfg_excl_file, cfg, GIT_IGNORE_CONFIG, GIT_IGNORE_FILE_XDG); + if (ret < 0) + goto cancel; + + /* allocate hashtable for attribute and ignore file contents, + * hashtable for attribute macros, and string pool + */ + if ((ret = git_pool_init(&cache->pool, 1)) < 0) + goto cancel; + + if (git_atomic_compare_and_swap(&repo->attrcache, NULL, cache) != NULL) + goto cancel; /* raced with another thread, free this but no error */ + + git_config_free(cfg); + + /* insert default macros */ + return git_attr_add_macro(repo, "binary", "-diff -merge -text -crlf"); + +cancel: + attr_cache__free(cache); + git_config_free(cfg); + return ret; +} + +int git_attr_cache_flush(git_repository *repo) +{ + git_attr_cache *cache; + + /* this could be done less expensively, but for now, we'll just free + * the entire attrcache and let the next use reinitialize it... + */ + if (repo && (cache = git_atomic_swap(repo->attrcache, NULL)) != NULL) + attr_cache__free(cache); + + return 0; +} + +int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro) +{ + git_attr_cache *cache = git_repository_attr_cache(repo); + git_attr_rule *preexisting; + bool locked = false; + int error = 0; + + /* + * Callers assume that if we return success, that the + * macro will have been adopted by the attributes cache. + * Thus, we have to free the macro here if it's not being + * added to the cache. + * + * TODO: generate warning log if (macro->assigns.length == 0) + */ + if (macro->assigns.length == 0) { + git_attr_rule__free(macro); + goto out; + } + + if ((error = attr_cache_lock(cache)) < 0) + goto out; + locked = true; + + if (git_attr_cache_macromap_get(&preexisting, &cache->macros, macro->match.pattern) == 0) + git_attr_rule__free(preexisting); + + if ((error = git_attr_cache_macromap_put(&cache->macros, macro->match.pattern, macro)) < 0) + goto out; + +out: + if (locked) + attr_cache_unlock(cache); + return error; +} + +git_attr_rule *git_attr_cache__lookup_macro( + git_repository *repo, const char *name) +{ + git_attr_cache *cache = git_repository_attr_cache(repo); + git_attr_rule *rule; + + if (!cache || + git_attr_cache_macromap_get(&rule, &cache->macros, name) != 0) + return NULL; + + return rule; +} diff --git a/src/libgit2/attrcache.h b/src/libgit2/attrcache.h new file mode 100644 index 00000000000..2693278d4b8 --- /dev/null +++ b/src/libgit2/attrcache.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_attrcache_h__ +#define INCLUDE_attrcache_h__ + +#include "common.h" + +#include "attr_file.h" + +#define GIT_ATTR_CONFIG "core.attributesfile" +#define GIT_IGNORE_CONFIG "core.excludesfile" + +typedef struct git_attr_cache git_attr_cache; + +extern int git_attr_cache__init(git_repository *repo); + +extern const char *git_attr_cache_attributesfile(git_attr_cache *ac); +extern const char *git_attr_cache_excludesfile(git_attr_cache *ac); +extern git_pool *git_attr_cache_pool(git_attr_cache *ac); + +/* get file - loading and reload as needed */ +extern int git_attr_cache__get( + git_attr_file **file, + git_repository *repo, + git_attr_session *attr_session, + git_attr_file_source *source, + git_attr_file_parser parser, + bool allow_macros); + +extern bool git_attr_cache__is_cached( + git_repository *repo, + git_attr_file_source_t source_type, + const char *filename); + +extern int git_attr_cache__alloc_file_entry( + git_attr_file_entry **out, + git_repository *repo, + const char *base, + const char *path, + git_pool *pool); + +extern int git_attr_cache__insert_macro( + git_repository *repo, git_attr_rule *macro); + +extern git_attr_rule *git_attr_cache__lookup_macro( + git_repository *repo, const char *name); + +#endif diff --git a/src/libgit2/blame.c b/src/libgit2/blame.c new file mode 100644 index 00000000000..4f99de69bd1 --- /dev/null +++ b/src/libgit2/blame.c @@ -0,0 +1,652 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "blame.h" + +#include "git2/commit.h" +#include "git2/revparse.h" +#include "git2/revwalk.h" +#include "git2/tree.h" +#include "git2/diff.h" +#include "git2/blob.h" +#include "git2/signature.h" +#include "git2/mailmap.h" +#include "util.h" +#include "repository.h" +#include "blame_git.h" + +static int hunk_byfinalline_search_cmp(const void *key, const void *entry) +{ + git_blame_hunk *hunk = (git_blame_hunk*)entry; + + size_t lineno = *(size_t*)key; + size_t lines_in_hunk = hunk->lines_in_hunk; + size_t final_start_line_number = hunk->final_start_line_number; + + if (lineno < final_start_line_number) + return -1; + if (lineno >= final_start_line_number + lines_in_hunk) + return 1; + return 0; +} + +static int paths_cmp(const void *a, const void *b) { return git__strcmp((char*)a, (char*)b); } +static int hunk_cmp(const void *_a, const void *_b) +{ + git_blame_hunk *a = (git_blame_hunk*)_a, + *b = (git_blame_hunk*)_b; + + if (a->final_start_line_number > b->final_start_line_number) + return 1; + else if (a->final_start_line_number < b->final_start_line_number) + return -1; + else + return 0; +} + +static bool hunk_ends_at_or_before_line(git_blame_hunk *hunk, size_t line) +{ + return line >= (hunk->final_start_line_number + hunk->lines_in_hunk - 1); +} + +static bool hunk_starts_at_or_after_line(git_blame_hunk *hunk, size_t line) +{ + return line <= hunk->final_start_line_number; +} + +static git_blame_hunk *new_hunk( + size_t start, + size_t lines, + size_t orig_start, + const char *path, + git_blame *blame) +{ + git_blame_hunk *hunk = git__calloc(1, sizeof(git_blame_hunk)); + if (!hunk) return NULL; + + hunk->lines_in_hunk = lines; + hunk->final_start_line_number = start; + hunk->orig_start_line_number = orig_start; + hunk->orig_path = path ? git__strdup(path) : NULL; + git_oid_clear(&hunk->orig_commit_id, blame->repository->oid_type); + git_oid_clear(&hunk->final_commit_id, blame->repository->oid_type); + + return hunk; +} + +static void free_hunk(git_blame_hunk *hunk) +{ + git__free((char *)hunk->orig_path); + git__free((char *)hunk->summary); + git_signature_free(hunk->final_signature); + git_signature_free(hunk->final_committer); + git_signature_free(hunk->orig_signature); + git_signature_free(hunk->orig_committer); + git__free(hunk); +} + +static git_blame_hunk *dup_hunk(git_blame_hunk *hunk, git_blame *blame) +{ + git_blame_hunk *newhunk = new_hunk( + hunk->final_start_line_number, + hunk->lines_in_hunk, + hunk->orig_start_line_number, + hunk->orig_path, + blame); + + if (!newhunk) + return NULL; + + git_oid_cpy(&newhunk->orig_commit_id, &hunk->orig_commit_id); + git_oid_cpy(&newhunk->final_commit_id, &hunk->final_commit_id); + newhunk->boundary = hunk->boundary; + + if (git_signature_dup(&newhunk->final_signature, hunk->final_signature) < 0 || + git_signature_dup(&newhunk->final_committer, hunk->final_committer) < 0 || + git_signature_dup(&newhunk->orig_signature, hunk->orig_signature) < 0 || + git_signature_dup(&newhunk->orig_committer, hunk->orig_committer) < 0 || + (newhunk->summary = git__strdup(hunk->summary)) == NULL) { + free_hunk(newhunk); + return NULL; + } + + return newhunk; +} + +/* Starting with the hunk that includes start_line, shift all following hunks' + * final_start_line by shift_by lines */ +static void shift_hunks_by(git_vector *v, size_t start_line, int shift_by) +{ + size_t i; + for (i = 0; i < v->length; i++) { + git_blame_hunk *hunk = (git_blame_hunk*)v->contents[i]; + if(hunk->final_start_line_number < start_line){ + continue; + } + hunk->final_start_line_number += shift_by; + } +} + +git_blame *git_blame__alloc( + git_repository *repo, + git_blame_options opts, + const char *path) +{ + git_blame *gbr = git__calloc(1, sizeof(git_blame)); + if (!gbr) + return NULL; + + gbr->repository = repo; + gbr->options = opts; + + if (git_vector_init(&gbr->hunks, 8, hunk_cmp) < 0 || + git_vector_init(&gbr->paths, 8, paths_cmp) < 0 || + (gbr->path = git__strdup(path)) == NULL || + git_vector_insert(&gbr->paths, git__strdup(path)) < 0) { + git_blame_free(gbr); + return NULL; + } + + if (opts.flags & GIT_BLAME_USE_MAILMAP && + git_mailmap_from_repository(&gbr->mailmap, repo) < 0) { + git_blame_free(gbr); + return NULL; + } + + return gbr; +} + +void git_blame_free(git_blame *blame) +{ + size_t i; + git_blame_hunk *hunk; + + if (!blame) return; + + git_vector_foreach(&blame->hunks, i, hunk) + free_hunk(hunk); + + git_vector_dispose(&blame->hunks); + git_array_clear(blame->lines); + + git_vector_dispose_deep(&blame->paths); + + git_array_clear(blame->line_index); + + git_mailmap_free(blame->mailmap); + + git__free(blame->path); + git_blob_free(blame->final_blob); + git__free(blame); +} + +size_t git_blame_hunkcount(git_blame *blame) +{ + GIT_ASSERT_ARG(blame); + + return blame->hunks.length; +} + +size_t git_blame_linecount(git_blame *blame) +{ + GIT_ASSERT_ARG(blame); + + return git_array_size(blame->line_index); +} + +const git_blame_line *git_blame_line_byindex( + git_blame *blame, + size_t idx) +{ + GIT_ASSERT_ARG_WITH_RETVAL(blame, NULL); + GIT_ASSERT_WITH_RETVAL(idx > 0 && idx <= git_array_size(blame->line_index), NULL); + + return git_array_get(blame->lines, idx - 1); +} + +const git_blame_hunk *git_blame_hunk_byindex( + git_blame *blame, + size_t index) +{ + GIT_ASSERT_ARG_WITH_RETVAL(blame, NULL); + return git_vector_get(&blame->hunks, index); +} + +const git_blame_hunk *git_blame_hunk_byline( + git_blame *blame, + size_t lineno) +{ + size_t i, new_lineno = lineno; + + GIT_ASSERT_ARG_WITH_RETVAL(blame, NULL); + + if (git_vector_bsearch2(&i, &blame->hunks, + hunk_byfinalline_search_cmp, &new_lineno) != 0) + return NULL; + + return git_blame_hunk_byindex(blame, i); +} + +#ifndef GIT_DEPRECATE_HARD +uint32_t git_blame_get_hunk_count(git_blame *blame) +{ + size_t count = git_blame_hunkcount(blame); + GIT_ASSERT(count < UINT32_MAX); + return (uint32_t)count; +} + +const git_blame_hunk *git_blame_get_hunk_byindex( + git_blame *blame, + uint32_t index) +{ + return git_blame_hunk_byindex(blame, index); +} + +const git_blame_hunk *git_blame_get_hunk_byline( + git_blame *blame, + size_t lineno) +{ + return git_blame_hunk_byline(blame, lineno); +} +#endif + +static int normalize_options( + git_blame_options *out, + const git_blame_options *in, + git_repository *repo) +{ + git_blame_options dummy = GIT_BLAME_OPTIONS_INIT; + if (!in) in = &dummy; + + memcpy(out, in, sizeof(git_blame_options)); + + /* No newest_commit => HEAD */ + if (git_oid_is_zero(&out->newest_commit)) { + if (git_reference_name_to_id(&out->newest_commit, repo, "HEAD") < 0) { + return -1; + } + } + + /* min_line 0 really means 1 */ + if (!out->min_line) out->min_line = 1; + /* max_line 0 really means N, but we don't know N yet */ + + /* Fix up option implications */ + if (out->flags & GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES) + out->flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES; + if (out->flags & GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES) + out->flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES; + if (out->flags & GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES) + out->flags |= GIT_BLAME_TRACK_COPIES_SAME_FILE; + + return 0; +} + +static git_blame_hunk *split_hunk_in_vector( + git_vector *vec, + git_blame_hunk *hunk, + size_t rel_line, + bool return_new, + git_blame *blame) +{ + size_t new_line_count; + git_blame_hunk *nh; + + /* Don't split if already at a boundary */ + if (rel_line <= 0 || + rel_line >= hunk->lines_in_hunk) + { + return hunk; + } + + new_line_count = hunk->lines_in_hunk - rel_line; + nh = new_hunk(hunk->final_start_line_number + rel_line, + new_line_count, hunk->orig_start_line_number + rel_line, + hunk->orig_path, blame); + + if (!nh) + return NULL; + + git_oid_cpy(&nh->final_commit_id, &hunk->final_commit_id); + git_oid_cpy(&nh->orig_commit_id, &hunk->orig_commit_id); + + /* Adjust hunk that was split */ + hunk->lines_in_hunk -= new_line_count; + git_vector_insert_sorted(vec, nh, NULL); + { + git_blame_hunk *ret = return_new ? nh : hunk; + return ret; + } +} + +/* + * Construct a list of char indices for where lines begin + * Adapted from core git: + * https://github.com/gitster/git/blob/be5c9fb9049ed470e7005f159bb923a5f4de1309/builtin/blame.c#L1760-L1789 + */ +static int index_blob_lines(git_blame *blame) +{ + const char *buf = blame->final_buf; + size_t len = blame->final_buf_size; + int num = 0, incomplete = 0, bol = 1; + git_blame_line *line = NULL; + size_t *i; + + if (len && buf[len-1] != '\n') + incomplete++; /* incomplete line at the end */ + + while (len--) { + if (bol) { + i = git_array_alloc(blame->line_index); + GIT_ERROR_CHECK_ALLOC(i); + *i = buf - blame->final_buf; + + GIT_ASSERT(line == NULL); + line = git_array_alloc(blame->lines); + GIT_ERROR_CHECK_ALLOC(line); + + line->ptr = buf; + bol = 0; + } + + if (*buf++ == '\n') { + GIT_ASSERT(line); + line->len = (buf - line->ptr) - 1; + line = NULL; + + num++; + bol = 1; + } + } + + i = git_array_alloc(blame->line_index); + GIT_ERROR_CHECK_ALLOC(i); + *i = buf - blame->final_buf; + + if (!bol) { + GIT_ASSERT(line); + line->len = buf - line->ptr; + line = NULL; + } + + GIT_ASSERT(!line); + + blame->num_lines = num + incomplete; + return blame->num_lines; +} + +static git_blame_hunk *hunk_from_entry(git_blame__entry *e, git_blame *blame) +{ + const char *summary; + git_blame_hunk *h = new_hunk( + e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path, + blame); + + if (!h) + return NULL; + + git_oid_cpy(&h->final_commit_id, git_commit_id(e->suspect->commit)); + git_oid_cpy(&h->orig_commit_id, git_commit_id(e->suspect->commit)); + + if (git_commit_author_with_mailmap( + &h->final_signature, e->suspect->commit, blame->mailmap) < 0 || + git_commit_committer_with_mailmap( + &h->final_committer, e->suspect->commit, blame->mailmap) < 0 || + git_signature_dup(&h->orig_signature, h->final_signature) < 0 || + git_signature_dup(&h->orig_committer, h->final_committer) < 0 || + (summary = git_commit_summary(e->suspect->commit)) == NULL || + (h->summary = git__strdup(summary)) == NULL) { + free_hunk(h); + return NULL; + } + + h->boundary = e->is_boundary ? 1 : 0; + return h; +} + +static int load_blob(git_blame *blame) +{ + int error; + + if (blame->final_blob) return 0; + + error = git_commit_lookup(&blame->final, blame->repository, &blame->options.newest_commit); + if (error < 0) + goto cleanup; + error = git_object_lookup_bypath((git_object**)&blame->final_blob, + (git_object*)blame->final, blame->path, GIT_OBJECT_BLOB); + +cleanup: + return error; +} + +static int blame_internal(git_blame *blame) +{ + int error; + git_blame__entry *ent = NULL; + git_blame__origin *o; + + if ((error = load_blob(blame)) < 0 || + (error = git_blame__get_origin(&o, blame, blame->final, blame->path)) < 0) + goto on_error; + + if (git_blob_rawsize(blame->final_blob) > SIZE_MAX) { + git_error_set(GIT_ERROR_NOMEMORY, "blob is too large to blame"); + error = -1; + goto on_error; + } + + blame->final_buf = git_blob_rawcontent(blame->final_blob); + blame->final_buf_size = (size_t)git_blob_rawsize(blame->final_blob); + + ent = git__calloc(1, sizeof(git_blame__entry)); + GIT_ERROR_CHECK_ALLOC(ent); + + ent->num_lines = index_blob_lines(blame); + ent->lno = blame->options.min_line - 1; + ent->num_lines = ent->num_lines - blame->options.min_line + 1; + if (blame->options.max_line > 0) + ent->num_lines = blame->options.max_line - blame->options.min_line + 1; + ent->s_lno = ent->lno; + ent->suspect = o; + + blame->ent = ent; + + if ((error = git_blame__like_git(blame, blame->options.flags)) < 0) + goto on_error; + + for (ent = blame->ent; ent; ent = ent->next) { + git_blame_hunk *h = hunk_from_entry(ent, blame); + git_vector_insert(&blame->hunks, h); + } + +on_error: + for (ent = blame->ent; ent; ) { + git_blame__entry *next = ent->next; + git_blame__free_entry(ent); + ent = next; + } + + return error; +} + +/******************************************************************************* + * File blaming + ******************************************************************************/ + +int git_blame_file( + git_blame **out, + git_repository *repo, + const char *path, + git_blame_options *options) +{ + int error = -1; + git_blame_options normOptions = GIT_BLAME_OPTIONS_INIT; + git_blame *blame = NULL; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(path); + + if ((error = normalize_options(&normOptions, options, repo)) < 0) + goto on_error; + + blame = git_blame__alloc(repo, normOptions, path); + GIT_ERROR_CHECK_ALLOC(blame); + + if ((error = load_blob(blame)) < 0) + goto on_error; + + if ((error = blame_internal(blame)) < 0) + goto on_error; + + *out = blame; + return 0; + +on_error: + git_blame_free(blame); + return error; +} + +/******************************************************************************* + * Buffer blaming + *******************************************************************************/ + +static bool hunk_is_bufferblame(git_blame_hunk *hunk) +{ + return hunk && git_oid_is_zero(&hunk->final_commit_id); +} + +static int buffer_hunk_cb( + const git_diff_delta *delta, + const git_diff_hunk *hunk, + void *payload) +{ + git_blame *blame = (git_blame*)payload; + uint32_t wedge_line; + + GIT_UNUSED(delta); + + wedge_line = (hunk->new_start >= hunk->old_start || hunk->old_lines==0) ? hunk->new_start : hunk->old_start; + blame->current_diff_line = wedge_line; + blame->current_hunk = (git_blame_hunk*)git_blame_hunk_byline(blame, wedge_line); + if (!blame->current_hunk) { + /* Line added at the end of the file */ + blame->current_hunk = new_hunk(wedge_line, 0, wedge_line, + blame->path, blame); + blame->current_diff_line++; + GIT_ERROR_CHECK_ALLOC(blame->current_hunk); + git_vector_insert(&blame->hunks, blame->current_hunk); + } else if (!hunk_starts_at_or_after_line(blame->current_hunk, wedge_line)){ + /* If this hunk doesn't start between existing hunks, split a hunk up so it does */ + blame->current_hunk = split_hunk_in_vector(&blame->hunks, blame->current_hunk, + wedge_line - blame->current_hunk->final_start_line_number, true, + blame); + GIT_ERROR_CHECK_ALLOC(blame->current_hunk); + } + + return 0; +} + +static int ptrs_equal_cmp(const void *a, const void *b) { return ab ? 1 : 0; } +static int buffer_line_cb( + const git_diff_delta *delta, + const git_diff_hunk *hunk, + const git_diff_line *line, + void *payload) +{ + git_blame *blame = (git_blame*)payload; + + GIT_UNUSED(delta); + GIT_UNUSED(hunk); + GIT_UNUSED(line); + + if (line->origin == GIT_DIFF_LINE_ADDITION) { + if (hunk_is_bufferblame(blame->current_hunk) && + hunk_ends_at_or_before_line(blame->current_hunk, blame->current_diff_line)) { + /* Append to the current buffer-blame hunk */ + blame->current_hunk->lines_in_hunk++; + shift_hunks_by(&blame->hunks, blame->current_diff_line, 1); + } else { + /* Create a new buffer-blame hunk with this line */ + shift_hunks_by(&blame->hunks, blame->current_diff_line, 1); + blame->current_hunk = new_hunk(blame->current_diff_line, 1, 0, blame->path, blame); + GIT_ERROR_CHECK_ALLOC(blame->current_hunk); + git_vector_insert_sorted(&blame->hunks, blame->current_hunk, NULL); + } + blame->current_diff_line++; + } + + if (line->origin == GIT_DIFF_LINE_DELETION) { + /* Trim the line from the current hunk; remove it if it's now empty */ + size_t shift_base = blame->current_diff_line + blame->current_hunk->lines_in_hunk; + + if (--(blame->current_hunk->lines_in_hunk) == 0) { + size_t i; + size_t i_next; + if (!git_vector_search2(&i, &blame->hunks, ptrs_equal_cmp, blame->current_hunk)) { + git_vector_remove(&blame->hunks, i); + free_hunk(blame->current_hunk); + i_next = min( i , blame->hunks.length -1); + blame->current_hunk = (git_blame_hunk*)git_blame_hunk_byindex(blame, (uint32_t)i_next); + } + } + shift_hunks_by(&blame->hunks, shift_base, -1); + } + return 0; +} + +int git_blame_buffer( + git_blame **out, + git_blame *reference, + const char *buffer, + size_t buffer_len) +{ + git_blame *blame; + git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; + size_t i; + git_blame_hunk *hunk; + + diffopts.context_lines = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(reference); + GIT_ASSERT_ARG(buffer && buffer_len); + + blame = git_blame__alloc(reference->repository, reference->options, reference->path); + GIT_ERROR_CHECK_ALLOC(blame); + + /* Duplicate all of the hunk structures in the reference blame */ + git_vector_foreach(&reference->hunks, i, hunk) { + git_blame_hunk *h = dup_hunk(hunk, blame); + GIT_ERROR_CHECK_ALLOC(h); + + git_vector_insert(&blame->hunks, h); + } + + /* Diff to the reference blob */ + git_diff_blob_to_buffer(reference->final_blob, blame->path, + buffer, buffer_len, blame->path, &diffopts, + NULL, NULL, buffer_hunk_cb, buffer_line_cb, blame); + + *out = blame; + return 0; +} + +int git_blame_options_init(git_blame_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_blame_options, GIT_BLAME_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_blame_init_options(git_blame_options *opts, unsigned int version) +{ + return git_blame_options_init(opts, version); +} +#endif diff --git a/src/libgit2/blame.h b/src/libgit2/blame.h new file mode 100644 index 00000000000..152834ebba7 --- /dev/null +++ b/src/libgit2/blame.h @@ -0,0 +1,96 @@ +#ifndef INCLUDE_blame_h__ +#define INCLUDE_blame_h__ + +#include "common.h" + +#include "git2/blame.h" +#include "vector.h" +#include "diff.h" +#include "array.h" +#include "git2/oid.h" + +/* + * One blob in a commit that is being suspected + */ +typedef struct git_blame__origin { + int refcnt; + struct git_blame__origin *previous; + git_commit *commit; + git_blob *blob; + char path[GIT_FLEX_ARRAY]; +} git_blame__origin; + +/* + * Each group of lines is described by a git_blame__entry; it can be split + * as we pass blame to the parents. They form a linked list in the + * scoreboard structure, sorted by the target line number. + */ +typedef struct git_blame__entry { + struct git_blame__entry *prev; + struct git_blame__entry *next; + + /* the first line of this group in the final image; + * internally all line numbers are 0 based. + */ + size_t lno; + + /* how many lines this group has */ + size_t num_lines; + + /* the commit that introduced this group into the final image */ + git_blame__origin *suspect; + + /* true if the suspect is truly guilty; false while we have not + * checked if the group came from one of its parents. + */ + bool guilty; + + /* true if the entry has been scanned for copies in the current parent + */ + bool scanned; + + /* the line number of the first line of this group in the + * suspect's file; internally all line numbers are 0 based. + */ + size_t s_lno; + + /* how significant this entry is -- cached to avoid + * scanning the lines over and over. + */ + unsigned score; + + /* Whether this entry has been tracked to a boundary commit. + */ + bool is_boundary; +} git_blame__entry; + +struct git_blame { + char *path; + git_repository *repository; + git_mailmap *mailmap; + git_blame_options options; + + git_vector hunks; + git_array_t(git_blame_line) lines; + git_vector paths; + + git_blob *final_blob; + git_array_t(size_t) line_index; + + size_t current_diff_line; + git_blame_hunk *current_hunk; + + /* Scoreboard fields */ + git_commit *final; + git_blame__entry *ent; + int num_lines; + const char *final_buf; + size_t final_buf_size; +}; + +git_blame *git_blame__alloc( + git_repository *repo, + git_blame_options opts, + const char *path); + +#endif diff --git a/src/libgit2/blame_git.c b/src/libgit2/blame_git.c new file mode 100644 index 00000000000..69897b3860c --- /dev/null +++ b/src/libgit2/blame_git.c @@ -0,0 +1,684 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "blame_git.h" + +#include "commit.h" +#include "blob.h" +#include "diff_xdiff.h" + +/* + * Origin is refcounted and usually we keep the blob contents to be + * reused. + */ +static git_blame__origin *origin_incref(git_blame__origin *o) +{ + if (o) + o->refcnt++; + return o; +} + +static void origin_decref(git_blame__origin *o) +{ + if (o && --o->refcnt <= 0) { + if (o->previous) + origin_decref(o->previous); + git_blob_free(o->blob); + git_commit_free(o->commit); + git__free(o); + } +} + +/* Given a commit and a path in it, create a new origin structure. */ +static int make_origin(git_blame__origin **out, git_commit *commit, const char *path) +{ + git_blame__origin *o; + git_object *blob; + size_t path_len = strlen(path), alloc_len; + int error = 0; + + if ((error = git_object_lookup_bypath(&blob, (git_object*)commit, + path, GIT_OBJECT_BLOB)) < 0) + return error; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, sizeof(*o), path_len); + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1); + o = git__calloc(1, alloc_len); + GIT_ERROR_CHECK_ALLOC(o); + + o->commit = commit; + o->blob = (git_blob *) blob; + o->refcnt = 1; + strcpy(o->path, path); + + *out = o; + + return 0; +} + +/* Locate an existing origin or create a new one. */ +int git_blame__get_origin( + git_blame__origin **out, + git_blame *blame, + git_commit *commit, + const char *path) +{ + git_blame__entry *e; + + for (e = blame->ent; e; e = e->next) { + if (e->suspect->commit == commit && !strcmp(e->suspect->path, path)) { + *out = origin_incref(e->suspect); + } + } + return make_origin(out, commit, path); +} + +typedef struct blame_chunk_cb_data { + git_blame *blame; + git_blame__origin *target; + git_blame__origin *parent; + long tlno; + long plno; +}blame_chunk_cb_data; + +static bool same_suspect(git_blame__origin *a, git_blame__origin *b) +{ + if (a == b) + return true; + if (git_oid_cmp(git_commit_id(a->commit), git_commit_id(b->commit))) + return false; + return 0 == strcmp(a->path, b->path); +} + +/* find the line number of the last line the target is suspected for */ +static bool find_last_in_target(size_t *out, git_blame *blame, git_blame__origin *target) +{ + git_blame__entry *e; + size_t last_in_target = 0; + bool found = false; + + *out = 0; + + for (e=blame->ent; e; e=e->next) { + if (e->guilty || !same_suspect(e->suspect, target)) + continue; + if (last_in_target < e->s_lno + e->num_lines) { + found = true; + last_in_target = e->s_lno + e->num_lines; + } + } + + *out = last_in_target; + return found; +} + +/* + * It is known that lines between tlno to same came from parent, and e + * has an overlap with that range. it also is known that parent's + * line plno corresponds to e's line tlno. + * + * <---- e -----> + * <------> (entirely within) + * <------------> (extends past) + * <------------> (starts before) + * <------------------> (entirely encloses) + * + * Split e into potentially three parts; before this chunk, the chunk + * to be blamed for the parent, and after that portion. + */ +static void split_overlap(git_blame__entry *split, git_blame__entry *e, + size_t tlno, size_t plno, size_t same, git_blame__origin *parent) +{ + size_t chunk_end_lno; + + if (e->s_lno < tlno) { + /* there is a pre-chunk part not blamed on the parent */ + split[0].suspect = origin_incref(e->suspect); + split[0].lno = e->lno; + split[0].s_lno = e->s_lno; + split[0].num_lines = tlno - e->s_lno; + split[1].lno = e->lno + tlno - e->s_lno; + split[1].s_lno = plno; + } else { + split[1].lno = e->lno; + split[1].s_lno = plno + (e->s_lno - tlno); + } + + if (same < e->s_lno + e->num_lines) { + /* there is a post-chunk part not blamed on parent */ + split[2].suspect = origin_incref(e->suspect); + split[2].lno = e->lno + (same - e->s_lno); + split[2].s_lno = e->s_lno + (same - e->s_lno); + split[2].num_lines = e->s_lno + e->num_lines - same; + chunk_end_lno = split[2].lno; + } else { + chunk_end_lno = e->lno + e->num_lines; + } + split[1].num_lines = chunk_end_lno - split[1].lno; + + /* + * if it turns out there is nothing to blame the parent for, forget about + * the splitting. !split[1].suspect signals this. + */ + if (split[1].num_lines < 1) + return; + split[1].suspect = origin_incref(parent); +} + +/* + * Link in a new blame entry to the scoreboard. Entries that cover the same + * line range have been removed from the scoreboard previously. + */ +static void add_blame_entry(git_blame *blame, git_blame__entry *e) +{ + git_blame__entry *ent, *prev = NULL; + + origin_incref(e->suspect); + + for (ent = blame->ent; ent && ent->lno < e->lno; ent = ent->next) + prev = ent; + + /* prev, if not NULL, is the last one that is below e */ + e->prev = prev; + if (prev) { + e->next = prev->next; + prev->next = e; + } else { + e->next = blame->ent; + blame->ent = e; + } + if (e->next) + e->next->prev = e; +} + +/* + * src typically is on-stack; we want to copy the information in it to + * a malloced blame_entry that is already on the linked list of the scoreboard. + * The origin of dst loses a refcnt while the origin of src gains one. + */ +static void dup_entry(git_blame__entry *dst, git_blame__entry *src) +{ + git_blame__entry *p, *n; + + p = dst->prev; + n = dst->next; + origin_incref(src->suspect); + origin_decref(dst->suspect); + memcpy(dst, src, sizeof(*src)); + dst->prev = p; + dst->next = n; + dst->score = 0; +} + +/* + * split_overlap() divided an existing blame e into up to three parts in split. + * Adjust the linked list of blames in the scoreboard to reflect the split. + */ +static int split_blame(git_blame *blame, git_blame__entry *split, git_blame__entry *e) +{ + git_blame__entry *new_entry; + + if (split[0].suspect && split[2].suspect) { + /* The first part (reuse storage for the existing entry e */ + dup_entry(e, &split[0]); + + /* The last part -- me */ + new_entry = git__malloc(sizeof(*new_entry)); + GIT_ERROR_CHECK_ALLOC(new_entry); + memcpy(new_entry, &(split[2]), sizeof(git_blame__entry)); + add_blame_entry(blame, new_entry); + + /* ... and the middle part -- parent */ + new_entry = git__malloc(sizeof(*new_entry)); + GIT_ERROR_CHECK_ALLOC(new_entry); + memcpy(new_entry, &(split[1]), sizeof(git_blame__entry)); + add_blame_entry(blame, new_entry); + } else if (!split[0].suspect && !split[2].suspect) { + /* + * The parent covers the entire area; reuse storage for e and replace it + * with the parent + */ + dup_entry(e, &split[1]); + } else if (split[0].suspect) { + /* me and then parent */ + dup_entry(e, &split[0]); + new_entry = git__malloc(sizeof(*new_entry)); + GIT_ERROR_CHECK_ALLOC(new_entry); + memcpy(new_entry, &(split[1]), sizeof(git_blame__entry)); + add_blame_entry(blame, new_entry); + } else { + /* parent and then me */ + dup_entry(e, &split[1]); + new_entry = git__malloc(sizeof(*new_entry)); + GIT_ERROR_CHECK_ALLOC(new_entry); + memcpy(new_entry, &(split[2]), sizeof(git_blame__entry)); + add_blame_entry(blame, new_entry); + } + + return 0; +} + +/* + * After splitting the blame, the origins used by the on-stack blame_entry + * should lose one refcnt each. + */ +static void decref_split(git_blame__entry *split) +{ + int i; + for (i=0; i<3; i++) + origin_decref(split[i].suspect); +} + +/* + * Helper for blame_chunk(). blame_entry e is known to overlap with the patch + * hunk; split it and pass blame to the parent. + */ +static int blame_overlap( + git_blame *blame, + git_blame__entry *e, + size_t tlno, + size_t plno, + size_t same, + git_blame__origin *parent) +{ + git_blame__entry split[3] = {{0}}; + + split_overlap(split, e, tlno, plno, same, parent); + if (split[1].suspect) + if (split_blame(blame, split, e) < 0) + return -1; + decref_split(split); + + return 0; +} + +/* + * Process one hunk from the patch between the current suspect for blame_entry + * e and its parent. Find and split the overlap, and pass blame to the + * overlapping part to the parent. + */ +static int blame_chunk( + git_blame *blame, + size_t tlno, + size_t plno, + size_t same, + git_blame__origin *target, + git_blame__origin *parent) +{ + git_blame__entry *e; + + for (e = blame->ent; e; e = e->next) { + if (e->guilty || !same_suspect(e->suspect, target)) + continue; + if (same <= e->s_lno) + continue; + if (tlno < e->s_lno + e->num_lines) { + if (blame_overlap(blame, e, tlno, plno, same, parent) < 0) + return -1; + } + } + + return 0; +} + +static int my_emit( + long start_a, long count_a, + long start_b, long count_b, + void *cb_data) +{ + blame_chunk_cb_data *d = (blame_chunk_cb_data *)cb_data; + + if (blame_chunk(d->blame, d->tlno, d->plno, start_b, d->target, d->parent) < 0) + return -1; + d->plno = start_a + count_a; + d->tlno = start_b + count_b; + + return 0; +} + +static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx) +{ + const int blk = 1024; + long trimmed = 0, recovered = 0; + char *ap = a->ptr + a->size; + char *bp = b->ptr + b->size; + long smaller = (long)((a->size < b->size) ? a->size : b->size); + + if (ctx) + return; + + while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) { + trimmed += blk; + ap -= blk; + bp -= blk; + } + + while (recovered < trimmed) + if (ap[recovered++] == '\n') + break; + a->size -= trimmed - recovered; + b->size -= trimmed - recovered; +} + +static int diff_hunks(mmfile_t file_a, mmfile_t file_b, void *cb_data, git_blame_options *options) +{ + xdemitconf_t xecfg = {0}; + xdemitcb_t ecb = {0}; + xpparam_t xpp = {0}; + + if (options->flags & GIT_BLAME_IGNORE_WHITESPACE) + xpp.flags |= XDF_IGNORE_WHITESPACE; + + xecfg.hunk_func = my_emit; + ecb.priv = cb_data; + + trim_common_tail(&file_a, &file_b, 0); + + if (file_a.size > GIT_XDIFF_MAX_SIZE || + file_b.size > GIT_XDIFF_MAX_SIZE) { + git_error_set(GIT_ERROR_INVALID, "file too large to blame"); + return -1; + } + + return xdl_diff(&file_a, &file_b, &xpp, &xecfg, &ecb); +} + +static void fill_origin_blob(git_blame__origin *o, mmfile_t *file) +{ + memset(file, 0, sizeof(*file)); + if (o->blob) { + file->ptr = (char*)git_blob_rawcontent(o->blob); + file->size = (long)git_blob_rawsize(o->blob); + } +} + +static int pass_blame_to_parent( + git_blame *blame, + git_blame__origin *target, + git_blame__origin *parent) +{ + size_t last_in_target; + mmfile_t file_p, file_o; + blame_chunk_cb_data d = { blame, target, parent, 0, 0 }; + + if (!find_last_in_target(&last_in_target, blame, target)) + return 1; /* nothing remains for this target */ + + fill_origin_blob(parent, &file_p); + fill_origin_blob(target, &file_o); + + if (diff_hunks(file_p, file_o, &d, &blame->options) < 0) + return -1; + + /* The reset (i.e. anything after tlno) are the same as the parent */ + if (blame_chunk(blame, d.tlno, d.plno, last_in_target, target, parent) < 0) + return -1; + + return 0; +} + +static int paths_on_dup(void **old, void *new) +{ + GIT_UNUSED(old); + git__free(new); + return -1; +} + +static git_blame__origin *find_origin( + git_blame *blame, + git_commit *parent, + git_blame__origin *origin) +{ + git_blame__origin *porigin = NULL; + git_diff *difflist = NULL; + git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; + git_tree *otree=NULL, *ptree=NULL; + + /* Get the trees from this commit and its parent */ + if (0 != git_commit_tree(&otree, origin->commit) || + 0 != git_commit_tree(&ptree, parent)) + goto cleanup; + + /* Configure the diff */ + diffopts.context_lines = 0; + diffopts.flags = GIT_DIFF_SKIP_BINARY_CHECK; + + /* Check to see if files we're interested have changed */ + diffopts.pathspec.count = blame->paths.length; + diffopts.pathspec.strings = (char**)blame->paths.contents; + if (0 != git_diff_tree_to_tree(&difflist, blame->repository, ptree, otree, &diffopts)) + goto cleanup; + + if (!git_diff_num_deltas(difflist)) { + /* No changes; copy data */ + git_blame__get_origin(&porigin, blame, parent, origin->path); + } else { + git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; + int i; + + /* Generate a full diff between the two trees */ + git_diff_free(difflist); + diffopts.pathspec.count = 0; + if (0 != git_diff_tree_to_tree(&difflist, blame->repository, ptree, otree, &diffopts)) + goto cleanup; + + /* Let diff find renames */ + findopts.flags = GIT_DIFF_FIND_RENAMES; + if (0 != git_diff_find_similar(difflist, &findopts)) + goto cleanup; + + /* Find one that matches */ + for (i=0; i<(int)git_diff_num_deltas(difflist); i++) { + const git_diff_delta *delta = git_diff_get_delta(difflist, i); + + if (!git_vector_bsearch(NULL, &blame->paths, delta->new_file.path)) + { + git_vector_insert_sorted(&blame->paths, (void*)git__strdup(delta->old_file.path), + paths_on_dup); + make_origin(&porigin, parent, delta->old_file.path); + } + } + } + +cleanup: + git_diff_free(difflist); + git_tree_free(otree); + git_tree_free(ptree); + return porigin; +} + +/* + * The blobs of origin and porigin exactly match, so everything origin is + * suspected for can be blamed on the parent. + */ +static int pass_whole_blame(git_blame *blame, + git_blame__origin *origin, git_blame__origin *porigin) +{ + git_blame__entry *e; + + if (!porigin->blob && + git_object_lookup((git_object**)&porigin->blob, blame->repository, + git_blob_id(origin->blob), GIT_OBJECT_BLOB) < 0) + return -1; + for (e=blame->ent; e; e=e->next) { + if (!same_suspect(e->suspect, origin)) + continue; + origin_incref(porigin); + origin_decref(e->suspect); + e->suspect = porigin; + } + + return 0; +} + +static int pass_blame(git_blame *blame, git_blame__origin *origin, uint32_t opt) +{ + git_commit *commit = origin->commit; + int i, num_parents; + git_blame__origin *sg_buf[16]; + git_blame__origin *porigin, **sg_origin = sg_buf; + int ret, error = 0; + + num_parents = git_commit_parentcount(commit); + if (!git_oid_cmp(git_commit_id(commit), &blame->options.oldest_commit)) + /* Stop at oldest specified commit */ + num_parents = 0; + else if (opt & GIT_BLAME_FIRST_PARENT && num_parents > 1) + /* Limit search to the first parent */ + num_parents = 1; + + if (!num_parents) { + git_oid_cpy(&blame->options.oldest_commit, git_commit_id(commit)); + goto finish; + } else if (num_parents < (int)ARRAY_SIZE(sg_buf)) + memset(sg_buf, 0, sizeof(sg_buf)); + else { + sg_origin = git__calloc(num_parents, sizeof(*sg_origin)); + GIT_ERROR_CHECK_ALLOC(sg_origin); + } + + for (i=0; icommit, i)) < 0) + goto finish; + porigin = find_origin(blame, p, origin); + + if (!porigin) { + /* + * We only have to decrement the parent's + * reference count when no porigin has + * been created, as otherwise the commit + * is assigned to the created object. + */ + git_commit_free(p); + continue; + } + if (porigin->blob && origin->blob && + !git_oid_cmp(git_blob_id(porigin->blob), git_blob_id(origin->blob))) { + error = pass_whole_blame(blame, origin, porigin); + origin_decref(porigin); + goto finish; + } + for (j = same = 0; jblob), git_blob_id(porigin->blob))) { + same = 1; + break; + } + if (!same) + sg_origin[i] = porigin; + else + origin_decref(porigin); + } + + /* Standard blame */ + for (i=0; iprevious) { + origin_incref(porigin); + origin->previous = porigin; + } + + if ((ret = pass_blame_to_parent(blame, origin, porigin)) != 0) { + if (ret < 0) + error = -1; + + goto finish; + } + } + + /* TODO: optionally find moves in parents' files */ + + /* TODO: optionally find copies in parents' files */ + +finish: + for (i=0; i pair), + * merge them together. + */ +static void coalesce(git_blame *blame) +{ + git_blame__entry *ent, *next; + + for (ent=blame->ent; ent && (next = ent->next); ent = next) { + if (same_suspect(ent->suspect, next->suspect) && + ent->guilty == next->guilty && + ent->s_lno + ent->num_lines == next->s_lno) + { + ent->num_lines += next->num_lines; + ent->next = next->next; + if (ent->next) + ent->next->prev = ent; + origin_decref(next->suspect); + git__free(next); + ent->score = 0; + next = ent; /* again */ + } + } +} + +int git_blame__like_git(git_blame *blame, uint32_t opt) +{ + int error = 0; + + while (true) { + git_blame__entry *ent; + git_blame__origin *suspect = NULL; + + /* Find a suspect to break down */ + for (ent = blame->ent; !suspect && ent; ent = ent->next) + if (!ent->guilty) + suspect = ent->suspect; + if (!suspect) + break; + + /* We'll use this suspect later in the loop, so hold on to it for now. */ + origin_incref(suspect); + + if ((error = pass_blame(blame, suspect, opt)) < 0) + break; + + /* Take responsibility for the remaining entries */ + for (ent = blame->ent; ent; ent = ent->next) { + if (same_suspect(ent->suspect, suspect)) { + ent->guilty = true; + ent->is_boundary = !git_oid_cmp( + git_commit_id(suspect->commit), + &blame->options.oldest_commit); + } + } + origin_decref(suspect); + } + + if (!error) + coalesce(blame); + + return error; +} + +void git_blame__free_entry(git_blame__entry *ent) +{ + if (!ent) return; + origin_decref(ent->suspect); + git__free(ent); +} diff --git a/src/libgit2/blame_git.h b/src/libgit2/blame_git.h new file mode 100644 index 00000000000..48b85a20d8c --- /dev/null +++ b/src/libgit2/blame_git.h @@ -0,0 +1,22 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_blame_git__ +#define INCLUDE_blame_git__ + +#include "common.h" + +#include "blame.h" + +int git_blame__get_origin( + git_blame__origin **out, + git_blame *sb, + git_commit *commit, + const char *path); +void git_blame__free_entry(git_blame__entry *ent); +int git_blame__like_git(git_blame *sb, uint32_t flags); + +#endif diff --git a/src/libgit2/blob.c b/src/libgit2/blob.c new file mode 100644 index 00000000000..5cfd7474b71 --- /dev/null +++ b/src/libgit2/blob.c @@ -0,0 +1,530 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "blob.h" + +#include "git2/common.h" +#include "git2/object.h" +#include "git2/repository.h" +#include "git2/odb_backend.h" + +#include "buf.h" +#include "filebuf.h" +#include "filter.h" + +const void *git_blob_rawcontent(const git_blob *blob) +{ + GIT_ASSERT_ARG_WITH_RETVAL(blob, NULL); + + if (blob->raw) + return blob->data.raw.data; + else + return git_odb_object_data(blob->data.odb); +} + +git_object_size_t git_blob_rawsize(const git_blob *blob) +{ + GIT_ASSERT_ARG(blob); + + if (blob->raw) + return blob->data.raw.size; + else + return (git_object_size_t)git_odb_object_size(blob->data.odb); +} + +int git_blob__getbuf(git_str *buffer, git_blob *blob) +{ + git_object_size_t size = git_blob_rawsize(blob); + + GIT_ERROR_CHECK_BLOBSIZE(size); + return git_str_set(buffer, git_blob_rawcontent(blob), (size_t)size); +} + +void git_blob__free(void *_blob) +{ + git_blob *blob = (git_blob *) _blob; + if (!blob->raw) + git_odb_object_free(blob->data.odb); + git__free(blob); +} + +int git_blob__parse_raw(void *_blob, const char *data, size_t size, git_oid_t oid_type) +{ + git_blob *blob = (git_blob *) _blob; + + GIT_ASSERT_ARG(blob); + GIT_UNUSED(oid_type); + + blob->raw = 1; + blob->data.raw.data = data; + blob->data.raw.size = size; + return 0; +} + +int git_blob__parse(void *_blob, git_odb_object *odb_obj, git_oid_t oid_type) +{ + git_blob *blob = (git_blob *) _blob; + + GIT_ASSERT_ARG(blob); + GIT_UNUSED(oid_type); + + git_cached_obj_incref((git_cached_obj *)odb_obj); + blob->raw = 0; + blob->data.odb = odb_obj; + return 0; +} + +int git_blob_create_from_buffer( + git_oid *id, git_repository *repo, const void *buffer, size_t len) +{ + int error; + git_odb *odb; + git_odb_stream *stream; + + GIT_ASSERT_ARG(id); + GIT_ASSERT_ARG(repo); + + if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 || + (error = git_odb_open_wstream(&stream, odb, len, GIT_OBJECT_BLOB)) < 0) + return error; + + if ((error = git_odb_stream_write(stream, buffer, len)) == 0) + error = git_odb_stream_finalize_write(id, stream); + + git_odb_stream_free(stream); + return error; +} + +static int write_file_stream( + git_oid *id, git_odb *odb, const char *path, git_object_size_t file_size) +{ + int fd, error; + char buffer[GIT_BUFSIZE_FILEIO]; + git_odb_stream *stream = NULL; + ssize_t read_len = -1; + git_object_size_t written = 0; + + if ((error = git_odb_open_wstream( + &stream, odb, file_size, GIT_OBJECT_BLOB)) < 0) + return error; + + if ((fd = git_futils_open_ro(path)) < 0) { + git_odb_stream_free(stream); + return -1; + } + + while (!error && (read_len = p_read(fd, buffer, sizeof(buffer))) > 0) { + error = git_odb_stream_write(stream, buffer, read_len); + written += read_len; + } + + p_close(fd); + + if (written != file_size || read_len < 0) { + git_error_set(GIT_ERROR_OS, "failed to read file into stream"); + error = -1; + } + + if (!error) + error = git_odb_stream_finalize_write(id, stream); + + git_odb_stream_free(stream); + return error; +} + +static int write_file_filtered( + git_oid *id, + git_object_size_t *size, + git_odb *odb, + const char *full_path, + git_filter_list *fl, + git_repository* repo) +{ + int error; + git_str tgt = GIT_STR_INIT; + + error = git_filter_list__apply_to_file(&tgt, fl, repo, full_path); + + /* Write the file to disk if it was properly filtered */ + if (!error) { + *size = tgt.size; + + error = git_odb_write(id, odb, tgt.ptr, tgt.size, GIT_OBJECT_BLOB); + } + + git_str_dispose(&tgt); + return error; +} + +static int write_symlink( + git_oid *id, git_odb *odb, const char *path, size_t link_size) +{ + char *link_data; + ssize_t read_len; + int error; + + link_data = git__malloc(link_size); + GIT_ERROR_CHECK_ALLOC(link_data); + + read_len = p_readlink(path, link_data, link_size); + if (read_len != (ssize_t)link_size) { + git_error_set(GIT_ERROR_OS, "failed to create blob: cannot read symlink '%s'", path); + git__free(link_data); + return -1; + } + + error = git_odb_write(id, odb, (void *)link_data, link_size, GIT_OBJECT_BLOB); + git__free(link_data); + return error; +} + +int git_blob__create_from_paths( + git_oid *id, + struct stat *out_st, + git_repository *repo, + const char *content_path, + const char *hint_path, + mode_t hint_mode, + bool try_load_filters) +{ + int error; + struct stat st; + git_odb *odb = NULL; + git_object_size_t size; + mode_t mode; + git_str path = GIT_STR_INIT; + + GIT_ASSERT_ARG(hint_path || !try_load_filters); + + if (!content_path) { + if (git_repository_workdir_path(&path, repo, hint_path) < 0) + return -1; + + content_path = path.ptr; + } + + if ((error = git_fs_path_lstat(content_path, &st)) < 0 || + (error = git_repository_odb(&odb, repo)) < 0) + goto done; + + if (S_ISDIR(st.st_mode)) { + git_error_set(GIT_ERROR_ODB, "cannot create blob from '%s': it is a directory", content_path); + error = GIT_EDIRECTORY; + goto done; + } + + if (out_st) + memcpy(out_st, &st, sizeof(st)); + + size = st.st_size; + mode = hint_mode ? hint_mode : st.st_mode; + + if (S_ISLNK(mode)) { + error = write_symlink(id, odb, content_path, (size_t)size); + } else { + git_filter_list *fl = NULL; + + if (try_load_filters) + /* Load the filters for writing this file to the ODB */ + error = git_filter_list_load( + &fl, repo, NULL, hint_path, + GIT_FILTER_TO_ODB, GIT_FILTER_DEFAULT); + + if (error < 0) + /* well, that didn't work */; + else if (fl == NULL) + /* No filters need to be applied to the document: we can stream + * directly from disk */ + error = write_file_stream(id, odb, content_path, size); + else { + /* We need to apply one or more filters */ + error = write_file_filtered(id, &size, odb, content_path, fl, repo); + + git_filter_list_free(fl); + } + + /* + * TODO: eventually support streaming filtered files, for files + * which are bigger than a given threshold. This is not a priority + * because applying a filter in streaming mode changes the final + * size of the blob, and without knowing its final size, the blob + * cannot be written in stream mode to the ODB. + * + * The plan is to do streaming writes to a tempfile on disk and then + * opening streaming that file to the ODB, using + * `write_file_stream`. + * + * CAREFULLY DESIGNED APIS YO + */ + } + +done: + git_odb_free(odb); + git_str_dispose(&path); + + return error; +} + +int git_blob_create_from_workdir( + git_oid *id, git_repository *repo, const char *path) +{ + return git_blob__create_from_paths(id, NULL, repo, NULL, path, 0, true); +} + +int git_blob_create_from_disk( + git_oid *id, git_repository *repo, const char *path) +{ + int error; + git_str full_path = GIT_STR_INIT; + const char *workdir, *hintpath = NULL; + + if ((error = git_fs_path_prettify(&full_path, path, NULL)) < 0) { + git_str_dispose(&full_path); + return error; + } + + workdir = git_repository_workdir(repo); + + if (workdir && !git__prefixcmp(full_path.ptr, workdir)) + hintpath = full_path.ptr + strlen(workdir); + + error = git_blob__create_from_paths( + id, NULL, repo, git_str_cstr(&full_path), hintpath, 0, !!hintpath); + + git_str_dispose(&full_path); + return error; +} + +typedef struct { + git_writestream parent; + git_filebuf fbuf; + git_repository *repo; + char *hintpath; +} blob_writestream; + +static int blob_writestream_close(git_writestream *_stream) +{ + blob_writestream *stream = (blob_writestream *) _stream; + + git_filebuf_cleanup(&stream->fbuf); + return 0; +} + +static void blob_writestream_free(git_writestream *_stream) +{ + blob_writestream *stream = (blob_writestream *) _stream; + + git_filebuf_cleanup(&stream->fbuf); + git__free(stream->hintpath); + git__free(stream); +} + +static int blob_writestream_write(git_writestream *_stream, const char *buffer, size_t len) +{ + blob_writestream *stream = (blob_writestream *) _stream; + + return git_filebuf_write(&stream->fbuf, buffer, len); +} + +int git_blob_create_from_stream(git_writestream **out, git_repository *repo, const char *hintpath) +{ + int error; + git_str path = GIT_STR_INIT; + blob_writestream *stream; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + stream = git__calloc(1, sizeof(blob_writestream)); + GIT_ERROR_CHECK_ALLOC(stream); + + if (hintpath) { + stream->hintpath = git__strdup(hintpath); + GIT_ERROR_CHECK_ALLOC(stream->hintpath); + } + + stream->repo = repo; + stream->parent.write = blob_writestream_write; + stream->parent.close = blob_writestream_close; + stream->parent.free = blob_writestream_free; + + if ((error = git_repository__item_path(&path, repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0 + || (error = git_str_joinpath(&path, path.ptr, "streamed")) < 0) + goto cleanup; + + if ((error = git_filebuf_open_withsize(&stream->fbuf, git_str_cstr(&path), GIT_FILEBUF_TEMPORARY, + 0666, 2 * 1024 * 1024)) < 0) + goto cleanup; + + *out = (git_writestream *) stream; + +cleanup: + if (error < 0) + blob_writestream_free((git_writestream *) stream); + + git_str_dispose(&path); + return error; +} + +int git_blob_create_from_stream_commit(git_oid *out, git_writestream *_stream) +{ + int error; + blob_writestream *stream = (blob_writestream *) _stream; + + /* + * We can make this more officient by avoiding writing to + * disk, but for now let's re-use the helper functions we + * have. + */ + if ((error = git_filebuf_flush(&stream->fbuf)) < 0) + goto cleanup; + + error = git_blob__create_from_paths(out, NULL, stream->repo, stream->fbuf.path_lock, + stream->hintpath, 0, !!stream->hintpath); + +cleanup: + blob_writestream_free(_stream); + return error; + +} + +int git_blob_is_binary(const git_blob *blob) +{ + git_str content = GIT_STR_INIT; + git_object_size_t size; + + GIT_ASSERT_ARG(blob); + + size = git_blob_rawsize(blob); + + git_str_attach_notowned(&content, git_blob_rawcontent(blob), + (size_t)min(size, GIT_FILTER_BYTES_TO_CHECK_NUL)); + return git_str_is_binary(&content); +} + +int git_blob_data_is_binary(const char *str, size_t len) +{ + git_str content = GIT_STR_INIT; + + git_str_attach_notowned(&content, str, len); + + return git_str_is_binary(&content); +} + +int git_blob_filter_options_init( + git_blob_filter_options *opts, + unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE(opts, version, + git_blob_filter_options, GIT_BLOB_FILTER_OPTIONS_INIT); + return 0; +} + +int git_blob_filter( + git_buf *out, + git_blob *blob, + const char *path, + git_blob_filter_options *given_opts) +{ + git_blob_filter_options opts = GIT_BLOB_FILTER_OPTIONS_INIT; + git_filter_options filter_opts = GIT_FILTER_OPTIONS_INIT; + git_filter_list *fl = NULL; + int error = 0; + + GIT_ASSERT_ARG(blob); + GIT_ASSERT_ARG(path); + GIT_ASSERT_ARG(out); + + GIT_ERROR_CHECK_VERSION( + given_opts, GIT_BLOB_FILTER_OPTIONS_VERSION, "git_blob_filter_options"); + + if (given_opts != NULL) + memcpy(&opts, given_opts, sizeof(git_blob_filter_options)); + + if ((opts.flags & GIT_BLOB_FILTER_CHECK_FOR_BINARY) != 0 && + git_blob_is_binary(blob)) + return 0; + + if ((opts.flags & GIT_BLOB_FILTER_NO_SYSTEM_ATTRIBUTES) != 0) + filter_opts.flags |= GIT_FILTER_NO_SYSTEM_ATTRIBUTES; + + if ((opts.flags & GIT_BLOB_FILTER_ATTRIBUTES_FROM_HEAD) != 0) + filter_opts.flags |= GIT_FILTER_ATTRIBUTES_FROM_HEAD; + + if ((opts.flags & GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT) != 0) { + filter_opts.flags |= GIT_FILTER_ATTRIBUTES_FROM_COMMIT; + +#ifndef GIT_DEPRECATE_HARD + if (opts.commit_id) + git_oid_cpy(&filter_opts.attr_commit_id, opts.commit_id); + else +#endif + git_oid_cpy(&filter_opts.attr_commit_id, &opts.attr_commit_id); + } + + if (!(error = git_filter_list_load_ext( + &fl, git_blob_owner(blob), blob, path, + GIT_FILTER_TO_WORKTREE, &filter_opts))) { + + error = git_filter_list_apply_to_blob(out, fl, blob); + + git_filter_list_free(fl); + } + + return error; +} + +/* Deprecated functions */ + +#ifndef GIT_DEPRECATE_HARD +int git_blob_create_frombuffer( + git_oid *id, git_repository *repo, const void *buffer, size_t len) +{ + return git_blob_create_from_buffer(id, repo, buffer, len); +} + +int git_blob_create_fromworkdir(git_oid *id, git_repository *repo, const char *relative_path) +{ + return git_blob_create_from_workdir(id, repo, relative_path); +} + +int git_blob_create_fromdisk(git_oid *id, git_repository *repo, const char *path) +{ + return git_blob_create_from_disk(id, repo, path); +} + +int git_blob_create_fromstream( + git_writestream **out, + git_repository *repo, + const char *hintpath) +{ + return git_blob_create_from_stream(out, repo, hintpath); +} + +int git_blob_create_fromstream_commit( + git_oid *out, + git_writestream *stream) +{ + return git_blob_create_from_stream_commit(out, stream); +} + +int git_blob_filtered_content( + git_buf *out, + git_blob *blob, + const char *path, + int check_for_binary_data) +{ + git_blob_filter_options opts = GIT_BLOB_FILTER_OPTIONS_INIT; + + if (check_for_binary_data) + opts.flags |= GIT_BLOB_FILTER_CHECK_FOR_BINARY; + else + opts.flags &= ~GIT_BLOB_FILTER_CHECK_FOR_BINARY; + + return git_blob_filter(out, blob, path, &opts); +} +#endif diff --git a/src/libgit2/blob.h b/src/libgit2/blob.h new file mode 100644 index 00000000000..d6c9dd99b07 --- /dev/null +++ b/src/libgit2/blob.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_blob_h__ +#define INCLUDE_blob_h__ + +#include "common.h" + +#include "git2/blob.h" +#include "repository.h" +#include "odb.h" +#include "futils.h" + +struct git_blob { + git_object object; + + union { + git_odb_object *odb; + struct { + const char *data; + git_object_size_t size; + } raw; + } data; + unsigned int raw:1; +}; + +#define GIT_ERROR_CHECK_BLOBSIZE(n) \ + do { \ + if (!git__is_sizet(n)) { \ + git_error_set(GIT_ERROR_NOMEMORY, "blob contents too large to fit in memory"); \ + return -1; \ + } \ + } while(0) + +void git_blob__free(void *blob); +int git_blob__parse(void *blob, git_odb_object *obj, git_oid_t oid_type); +int git_blob__parse_raw(void *blob, const char *data, size_t size, git_oid_t oid_type); +int git_blob__getbuf(git_str *buffer, git_blob *blob); + +extern int git_blob__create_from_paths( + git_oid *out_oid, + struct stat *out_st, + git_repository *repo, + const char *full_path, + const char *hint_path, + mode_t hint_mode, + bool apply_filters); + +#endif diff --git a/src/libgit2/branch.c b/src/libgit2/branch.c new file mode 100644 index 00000000000..9a31c9c6ffc --- /dev/null +++ b/src/libgit2/branch.c @@ -0,0 +1,823 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "branch.h" + +#include "buf.h" +#include "commit.h" +#include "tag.h" +#include "config.h" +#include "refspec.h" +#include "refs.h" +#include "remote.h" +#include "annotated_commit.h" +#include "worktree.h" + +#include "git2/branch.h" + +static int retrieve_branch_reference( + git_reference **branch_reference_out, + git_repository *repo, + const char *branch_name, + bool is_remote) +{ + git_reference *branch = NULL; + int error = 0; + char *prefix; + git_str ref_name = GIT_STR_INIT; + + prefix = is_remote ? GIT_REFS_REMOTES_DIR : GIT_REFS_HEADS_DIR; + + if ((error = git_str_joinpath(&ref_name, prefix, branch_name)) < 0) + /* OOM */; + else if ((error = git_reference_lookup(&branch, repo, ref_name.ptr)) < 0) + git_error_set( + GIT_ERROR_REFERENCE, "cannot locate %s branch '%s'", + is_remote ? "remote-tracking" : "local", branch_name); + + *branch_reference_out = branch; /* will be NULL on error */ + + git_str_dispose(&ref_name); + return error; +} + +static int not_a_local_branch(const char *reference_name) +{ + git_error_set( + GIT_ERROR_INVALID, + "reference '%s' is not a local branch.", reference_name); + return -1; +} + +static bool branch_name_is_valid(const char *branch_name) +{ + /* + * Discourage branch name starting with dash, + * https://github.com/git/git/commit/6348624010888b + * and discourage HEAD as branch name, + * https://github.com/git/git/commit/a625b092cc5994 + */ + return branch_name[0] != '-' && git__strcmp(branch_name, "HEAD"); +} + +static int create_branch( + git_reference **ref_out, + git_repository *repository, + const char *branch_name, + const git_commit *commit, + const char *from, + int force) +{ + int is_unmovable_head = 0; + git_reference *branch = NULL; + git_str canonical_branch_name = GIT_STR_INIT, + log_message = GIT_STR_INIT; + int error = -1; + int bare = git_repository_is_bare(repository); + + GIT_ASSERT_ARG(branch_name); + GIT_ASSERT_ARG(commit); + GIT_ASSERT_ARG(ref_out); + GIT_ASSERT_ARG(git_commit_owner(commit) == repository); + + if (!branch_name_is_valid(branch_name)) { + git_error_set(GIT_ERROR_REFERENCE, "'%s' is not a valid branch name", branch_name); + error = -1; + goto cleanup; + } + + if (force && !bare && git_branch_lookup(&branch, repository, branch_name, GIT_BRANCH_LOCAL) == 0) { + error = git_branch_is_head(branch); + git_reference_free(branch); + branch = NULL; + + if (error < 0) + goto cleanup; + + is_unmovable_head = error; + } + + if (is_unmovable_head && force) { + git_error_set(GIT_ERROR_REFERENCE, "cannot force update branch '%s' as it is " + "the current HEAD of the repository.", branch_name); + error = -1; + goto cleanup; + } + + if (git_str_joinpath(&canonical_branch_name, GIT_REFS_HEADS_DIR, branch_name) < 0) + goto cleanup; + + if (git_str_printf(&log_message, "branch: Created from %s", from) < 0) + goto cleanup; + + error = git_reference_create(&branch, repository, + git_str_cstr(&canonical_branch_name), git_commit_id(commit), force, + git_str_cstr(&log_message)); + + if (!error) + *ref_out = branch; + +cleanup: + git_str_dispose(&canonical_branch_name); + git_str_dispose(&log_message); + return error; +} + +int git_branch_create( + git_reference **ref_out, + git_repository *repository, + const char *branch_name, + const git_commit *commit, + int force) +{ + char commit_id[GIT_OID_MAX_HEXSIZE + 1]; + + git_oid_tostr(commit_id, GIT_OID_MAX_HEXSIZE + 1, git_commit_id(commit)); + return create_branch(ref_out, repository, branch_name, commit, commit_id, force); +} + +int git_branch_create_from_annotated( + git_reference **ref_out, + git_repository *repository, + const char *branch_name, + const git_annotated_commit *commit, + int force) +{ + return create_branch(ref_out, + repository, branch_name, commit->commit, commit->description, force); +} + +static int branch_is_checked_out(git_repository *worktree, void *payload) +{ + git_reference *branch = (git_reference *) payload; + git_reference *head = NULL; + int error; + + if (git_repository_is_bare(worktree)) + return 0; + + if ((error = git_reference_lookup(&head, worktree, GIT_HEAD_FILE)) < 0) { + if (error == GIT_ENOTFOUND) + error = 0; + goto out; + } + + if (git_reference_type(head) != GIT_REFERENCE_SYMBOLIC) + goto out; + + error = !git__strcmp(head->target.symbolic, branch->name); + +out: + git_reference_free(head); + return error; +} + +int git_branch_is_checked_out(const git_reference *branch) +{ + GIT_ASSERT_ARG(branch); + + if (!git_reference_is_branch(branch)) + return 0; + return git_repository_foreach_worktree(git_reference_owner(branch), + branch_is_checked_out, (void *)branch) == 1; +} + +int git_branch_delete(git_reference *branch) +{ + int is_head; + git_str config_section = GIT_STR_INIT; + int error = -1; + + GIT_ASSERT_ARG(branch); + + if (!git_reference_is_branch(branch) && !git_reference_is_remote(branch)) { + git_error_set(GIT_ERROR_INVALID, "reference '%s' is not a valid branch.", + git_reference_name(branch)); + return GIT_ENOTFOUND; + } + + if ((is_head = git_branch_is_head(branch)) < 0) + return is_head; + + if (is_head) { + git_error_set(GIT_ERROR_REFERENCE, "cannot delete branch '%s' as it is " + "the current HEAD of the repository.", git_reference_name(branch)); + return -1; + } + + if (git_reference_is_branch(branch) && git_branch_is_checked_out(branch)) { + git_error_set(GIT_ERROR_REFERENCE, "Cannot delete branch '%s' as it is " + "the current HEAD of a linked repository.", git_reference_name(branch)); + return -1; + } + + if (git_str_join(&config_section, '.', "branch", + git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0) + goto on_error; + + if (git_config_rename_section( + git_reference_owner(branch), git_str_cstr(&config_section), NULL) < 0) + goto on_error; + + error = git_reference_delete(branch); + +on_error: + git_str_dispose(&config_section); + return error; +} + +typedef struct { + git_reference_iterator *iter; + unsigned int flags; +} branch_iter; + +int git_branch_next(git_reference **out, git_branch_t *out_type, git_branch_iterator *_iter) +{ + branch_iter *iter = (branch_iter *) _iter; + git_reference *ref; + int error; + + while ((error = git_reference_next(&ref, iter->iter)) == 0) { + if ((iter->flags & GIT_BRANCH_LOCAL) && + !git__prefixcmp(ref->name, GIT_REFS_HEADS_DIR)) { + *out = ref; + *out_type = GIT_BRANCH_LOCAL; + + return 0; + } else if ((iter->flags & GIT_BRANCH_REMOTE) && + !git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR)) { + *out = ref; + *out_type = GIT_BRANCH_REMOTE; + + return 0; + } else { + git_reference_free(ref); + } + } + + return error; +} + +int git_branch_iterator_new( + git_branch_iterator **out, + git_repository *repo, + git_branch_t list_flags) +{ + branch_iter *iter; + + iter = git__calloc(1, sizeof(branch_iter)); + GIT_ERROR_CHECK_ALLOC(iter); + + iter->flags = list_flags; + + if (git_reference_iterator_new(&iter->iter, repo) < 0) { + git__free(iter); + return -1; + } + + *out = (git_branch_iterator *) iter; + + return 0; +} + +void git_branch_iterator_free(git_branch_iterator *_iter) +{ + branch_iter *iter = (branch_iter *) _iter; + + if (iter == NULL) + return; + + git_reference_iterator_free(iter->iter); + git__free(iter); +} + +int git_branch_move( + git_reference **out, + git_reference *branch, + const char *new_branch_name, + int force) +{ + git_str new_reference_name = GIT_STR_INIT, + old_config_section = GIT_STR_INIT, + new_config_section = GIT_STR_INIT, + log_message = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(branch); + GIT_ASSERT_ARG(new_branch_name); + + if (!git_reference_is_branch(branch)) + return not_a_local_branch(git_reference_name(branch)); + + if ((error = git_str_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0) + goto done; + + if ((error = git_str_printf(&log_message, "branch: renamed %s to %s", + git_reference_name(branch), git_str_cstr(&new_reference_name))) < 0) + goto done; + + /* first update ref then config so failure won't trash config */ + + error = git_reference_rename( + out, branch, git_str_cstr(&new_reference_name), force, + git_str_cstr(&log_message)); + if (error < 0) + goto done; + + git_str_join(&old_config_section, '.', "branch", + git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)); + git_str_join(&new_config_section, '.', "branch", new_branch_name); + + error = git_config_rename_section( + git_reference_owner(branch), + git_str_cstr(&old_config_section), + git_str_cstr(&new_config_section)); + +done: + git_str_dispose(&new_reference_name); + git_str_dispose(&old_config_section); + git_str_dispose(&new_config_section); + git_str_dispose(&log_message); + + return error; +} + +int git_branch_lookup( + git_reference **ref_out, + git_repository *repo, + const char *branch_name, + git_branch_t branch_type) +{ + int error = -1; + + GIT_ASSERT_ARG(ref_out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(branch_name); + + switch (branch_type) { + case GIT_BRANCH_LOCAL: + case GIT_BRANCH_REMOTE: + error = retrieve_branch_reference(ref_out, repo, branch_name, branch_type == GIT_BRANCH_REMOTE); + break; + case GIT_BRANCH_ALL: + error = retrieve_branch_reference(ref_out, repo, branch_name, false); + if (error == GIT_ENOTFOUND) + error = retrieve_branch_reference(ref_out, repo, branch_name, true); + break; + default: + GIT_ASSERT(false); + } + return error; +} + +int git_branch_name( + const char **out, + const git_reference *ref) +{ + const char *branch_name; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(ref); + + branch_name = ref->name; + + if (git_reference_is_branch(ref)) { + branch_name += strlen(GIT_REFS_HEADS_DIR); + } else if (git_reference_is_remote(ref)) { + branch_name += strlen(GIT_REFS_REMOTES_DIR); + } else { + git_error_set(GIT_ERROR_INVALID, + "reference '%s' is neither a local nor a remote branch.", ref->name); + return -1; + } + *out = branch_name; + return 0; +} + +static int retrieve_upstream_configuration( + git_str *out, + const git_config *config, + const char *canonical_branch_name, + const char *format) +{ + git_str buf = GIT_STR_INIT; + int error; + + if (git_str_printf(&buf, format, + canonical_branch_name + strlen(GIT_REFS_HEADS_DIR)) < 0) + return -1; + + error = git_config__get_string_buf(out, config, git_str_cstr(&buf)); + git_str_dispose(&buf); + return error; +} + +int git_branch_upstream_name( + git_buf *out, + git_repository *repo, + const char *refname) +{ + GIT_BUF_WRAP_PRIVATE(out, git_branch__upstream_name, repo, refname); +} + +int git_branch__upstream_name( + git_str *out, + git_repository *repo, + const char *refname) +{ + git_str remote_name = GIT_STR_INIT; + git_str merge_name = GIT_STR_INIT; + git_str buf = GIT_STR_INIT; + int error = -1; + git_remote *remote = NULL; + const git_refspec *refspec; + git_config *config; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(refname); + + if (!git_reference__is_branch(refname)) + return not_a_local_branch(refname); + + if ((error = git_repository_config_snapshot(&config, repo)) < 0) + return error; + + if ((error = retrieve_upstream_configuration( + &remote_name, config, refname, "branch.%s.remote")) < 0) + goto cleanup; + + if ((error = retrieve_upstream_configuration( + &merge_name, config, refname, "branch.%s.merge")) < 0) + goto cleanup; + + if (git_str_len(&remote_name) == 0 || git_str_len(&merge_name) == 0) { + git_error_set(GIT_ERROR_REFERENCE, + "branch '%s' does not have an upstream", refname); + error = GIT_ENOTFOUND; + goto cleanup; + } + + if (strcmp(".", git_str_cstr(&remote_name)) != 0) { + if ((error = git_remote_lookup(&remote, repo, git_str_cstr(&remote_name))) < 0) + goto cleanup; + + refspec = git_remote__matching_refspec(remote, git_str_cstr(&merge_name)); + if (!refspec) { + error = GIT_ENOTFOUND; + goto cleanup; + } + + if (git_refspec__transform(&buf, refspec, git_str_cstr(&merge_name)) < 0) + goto cleanup; + } else + if (git_str_set(&buf, git_str_cstr(&merge_name), git_str_len(&merge_name)) < 0) + goto cleanup; + + git_str_swap(out, &buf); + +cleanup: + git_config_free(config); + git_remote_free(remote); + git_str_dispose(&remote_name); + git_str_dispose(&merge_name); + git_str_dispose(&buf); + return error; +} + +static int git_branch_upstream_with_format( + git_str *out, + git_repository *repo, + const char *refname, + const char *format, + const char *format_name) +{ + git_config *cfg; + int error; + + if (!git_reference__is_branch(refname)) + return not_a_local_branch(refname); + + if ((error = git_repository_config__weakptr(&cfg, repo)) < 0 || + (error = retrieve_upstream_configuration(out, cfg, refname, format)) < 0) + return error; + + if (git_str_len(out) == 0) { + git_error_set(GIT_ERROR_REFERENCE, "branch '%s' does not have an upstream %s", refname, format_name); + error = GIT_ENOTFOUND; + } + + return error; +} + +int git_branch_upstream_remote( + git_buf *out, + git_repository *repo, + const char *refname) +{ + GIT_BUF_WRAP_PRIVATE(out, git_branch__upstream_remote, repo, refname); +} + +int git_branch__upstream_remote( + git_str *out, + git_repository *repo, + const char *refname) +{ + return git_branch_upstream_with_format(out, repo, refname, "branch.%s.remote", "remote"); +} + +int git_branch_upstream_merge( + git_buf *out, + git_repository *repo, + const char *refname) +{ + GIT_BUF_WRAP_PRIVATE(out, git_branch__upstream_merge, repo, refname); +} + +int git_branch__upstream_merge( + git_str *out, + git_repository *repo, + const char *refname) +{ + return git_branch_upstream_with_format(out, repo, refname, "branch.%s.merge", "merge"); +} + +int git_branch_remote_name( + git_buf *out, + git_repository *repo, + const char *refname) +{ + GIT_BUF_WRAP_PRIVATE(out, git_branch__remote_name, repo, refname); +} + +int git_branch__remote_name( + git_str *out, + git_repository *repo, + const char *refname) +{ + git_strarray remote_list = {0}; + size_t i; + git_remote *remote; + const git_refspec *fetchspec; + int error = 0; + char *remote_name = NULL; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(refname); + + /* Verify that this is a remote branch */ + if (!git_reference__is_remote(refname)) { + git_error_set(GIT_ERROR_INVALID, "reference '%s' is not a remote branch.", + refname); + error = GIT_ERROR; + goto cleanup; + } + + /* Get the remotes */ + if ((error = git_remote_list(&remote_list, repo)) < 0) + goto cleanup; + + /* Find matching remotes */ + for (i = 0; i < remote_list.count; i++) { + if ((error = git_remote_lookup(&remote, repo, remote_list.strings[i])) < 0) + continue; + + fetchspec = git_remote__matching_dst_refspec(remote, refname); + if (fetchspec) { + /* If we have not already set out yet, then set + * it to the matching remote name. Otherwise + * multiple remotes match this reference, and it + * is ambiguous. */ + if (!remote_name) { + remote_name = remote_list.strings[i]; + } else { + git_remote_free(remote); + + git_error_set(GIT_ERROR_REFERENCE, + "reference '%s' is ambiguous", refname); + error = GIT_EAMBIGUOUS; + goto cleanup; + } + } + + git_remote_free(remote); + } + + if (remote_name) { + git_str_clear(out); + error = git_str_puts(out, remote_name); + } else { + git_error_set(GIT_ERROR_REFERENCE, + "could not determine remote for '%s'", refname); + error = GIT_ENOTFOUND; + } + +cleanup: + if (error < 0) + git_str_dispose(out); + + git_strarray_dispose(&remote_list); + return error; +} + +int git_branch_upstream( + git_reference **tracking_out, + const git_reference *branch) +{ + int error; + git_str tracking_name = GIT_STR_INIT; + + if ((error = git_branch__upstream_name(&tracking_name, + git_reference_owner(branch), git_reference_name(branch))) < 0) + return error; + + error = git_reference_lookup( + tracking_out, + git_reference_owner(branch), + git_str_cstr(&tracking_name)); + + git_str_dispose(&tracking_name); + return error; +} + +static int unset_upstream(git_config *config, const char *shortname) +{ + git_str buf = GIT_STR_INIT; + + if (git_str_printf(&buf, "branch.%s.remote", shortname) < 0) + return -1; + + if (git_config_delete_entry(config, git_str_cstr(&buf)) < 0) + goto on_error; + + git_str_clear(&buf); + if (git_str_printf(&buf, "branch.%s.merge", shortname) < 0) + goto on_error; + + if (git_config_delete_entry(config, git_str_cstr(&buf)) < 0) + goto on_error; + + git_str_dispose(&buf); + return 0; + +on_error: + git_str_dispose(&buf); + return -1; +} + +int git_branch_set_upstream(git_reference *branch, const char *branch_name) +{ + git_str key = GIT_STR_INIT, remote_name = GIT_STR_INIT, merge_refspec = GIT_STR_INIT; + git_reference *upstream; + git_repository *repo; + git_remote *remote = NULL; + git_config *config; + const char *refname, *shortname; + int local, error; + const git_refspec *fetchspec; + + refname = git_reference_name(branch); + if (!git_reference__is_branch(refname)) + return not_a_local_branch(refname); + + if (git_repository_config__weakptr(&config, git_reference_owner(branch)) < 0) + return -1; + + shortname = refname + strlen(GIT_REFS_HEADS_DIR); + + /* We're unsetting, delegate and bail-out */ + if (branch_name == NULL) + return unset_upstream(config, shortname); + + repo = git_reference_owner(branch); + + /* First we need to resolve name to a branch */ + if (git_branch_lookup(&upstream, repo, branch_name, GIT_BRANCH_LOCAL) == 0) + local = 1; + else if (git_branch_lookup(&upstream, repo, branch_name, GIT_BRANCH_REMOTE) == 0) + local = 0; + else { + git_error_set(GIT_ERROR_REFERENCE, + "cannot set upstream for branch '%s'", shortname); + return GIT_ENOTFOUND; + } + + /* + * If it's a local-tracking branch, its remote is "." (as "the local + * repository"), and the branch name is simply the refname. + * Otherwise we need to figure out what the remote-tracking branch's + * name on the remote is and use that. + */ + if (local) + error = git_str_puts(&remote_name, "."); + else + error = git_branch__remote_name(&remote_name, repo, git_reference_name(upstream)); + + if (error < 0) + goto on_error; + + /* Update the upstream branch config with the new name */ + if (git_str_printf(&key, "branch.%s.remote", shortname) < 0) + goto on_error; + + if (git_config_set_string(config, git_str_cstr(&key), git_str_cstr(&remote_name)) < 0) + goto on_error; + + if (local) { + /* A local branch uses the upstream refname directly */ + if (git_str_puts(&merge_refspec, git_reference_name(upstream)) < 0) + goto on_error; + } else { + /* We transform the upstream branch name according to the remote's refspecs */ + if (git_remote_lookup(&remote, repo, git_str_cstr(&remote_name)) < 0) + goto on_error; + + fetchspec = git_remote__matching_dst_refspec(remote, git_reference_name(upstream)); + if (!fetchspec || git_refspec__rtransform(&merge_refspec, fetchspec, git_reference_name(upstream)) < 0) + goto on_error; + + git_remote_free(remote); + remote = NULL; + } + + /* Update the merge branch config with the refspec */ + git_str_clear(&key); + if (git_str_printf(&key, "branch.%s.merge", shortname) < 0) + goto on_error; + + if (git_config_set_string(config, git_str_cstr(&key), git_str_cstr(&merge_refspec)) < 0) + goto on_error; + + git_reference_free(upstream); + git_str_dispose(&key); + git_str_dispose(&remote_name); + git_str_dispose(&merge_refspec); + + return 0; + +on_error: + git_reference_free(upstream); + git_str_dispose(&key); + git_str_dispose(&remote_name); + git_str_dispose(&merge_refspec); + git_remote_free(remote); + + return -1; +} + +int git_branch_is_head( + const git_reference *branch) +{ + git_reference *head; + bool is_same = false; + int error; + + GIT_ASSERT_ARG(branch); + + if (!git_reference_is_branch(branch)) + return false; + + error = git_repository_head(&head, git_reference_owner(branch)); + + if (error == GIT_EUNBORNBRANCH || error == GIT_ENOTFOUND) + return false; + + if (error < 0) + return -1; + + is_same = strcmp( + git_reference_name(branch), + git_reference_name(head)) == 0; + + git_reference_free(head); + + return is_same; +} + +int git_branch_name_is_valid(int *valid, const char *name) +{ + git_str ref_name = GIT_STR_INIT; + int error = 0; + + GIT_ASSERT(valid); + + *valid = 0; + + if (!name || !branch_name_is_valid(name)) + goto done; + + if ((error = git_str_puts(&ref_name, GIT_REFS_HEADS_DIR)) < 0 || + (error = git_str_puts(&ref_name, name)) < 0) + goto done; + + error = git_reference_name_is_valid(valid, ref_name.ptr); + +done: + git_str_dispose(&ref_name); + return error; +} diff --git a/src/libgit2/branch.h b/src/libgit2/branch.h new file mode 100644 index 00000000000..b4db42a0137 --- /dev/null +++ b/src/libgit2/branch.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_branch_h__ +#define INCLUDE_branch_h__ + +#include "common.h" + +#include "str.h" + +int git_branch__remote_name( + git_str *out, + git_repository *repo, + const char *refname); +int git_branch__upstream_remote( + git_str *out, + git_repository *repo, + const char *refname); +int git_branch__upstream_merge( + git_str *out, + git_repository *repo, + const char *refname); +int git_branch__upstream_name( + git_str *tracking_name, + git_repository *repo, + const char *canonical_branch_name); + +#endif diff --git a/src/libgit2/buf.c b/src/libgit2/buf.c new file mode 100644 index 00000000000..652f5dd52c7 --- /dev/null +++ b/src/libgit2/buf.c @@ -0,0 +1,126 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "buf.h" +#include "common.h" + +int git_buf_sanitize(git_buf *buf) +{ + GIT_ASSERT_ARG(buf); + + if (buf->reserved > 0) + buf->ptr[0] = '\0'; + else + buf->ptr = git_str__initstr; + + buf->size = 0; + return 0; +} + +int git_buf_tostr(git_str *out, git_buf *buf) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(buf); + + if (git_buf_sanitize(buf) < 0) + return -1; + + out->ptr = buf->ptr; + out->asize = buf->reserved; + out->size = buf->size; + + buf->ptr = git_str__initstr; + buf->reserved = 0; + buf->size = 0; + + return 0; +} + +int git_buf_fromstr(git_buf *out, git_str *str) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(str); + + out->ptr = str->ptr; + out->reserved = str->asize; + out->size = str->size; + + str->ptr = git_str__initstr; + str->asize = 0; + str->size = 0; + + return 0; +} + +void git_buf_dispose(git_buf *buf) +{ + if (!buf) + return; + + if (buf->ptr != git_str__initstr) + git__free(buf->ptr); + + buf->ptr = git_str__initstr; + buf->reserved = 0; + buf->size = 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_buf_grow(git_buf *buffer, size_t target_size) +{ + char *newptr; + + if (buffer->reserved >= target_size) + return 0; + + if (buffer->ptr == git_str__initstr) + newptr = git__malloc(target_size); + else + newptr = git__realloc(buffer->ptr, target_size); + + if (!newptr) + return -1; + + buffer->ptr = newptr; + buffer->reserved = target_size; + return 0; +} + +int git_buf_set(git_buf *buffer, const void *data, size_t datalen) +{ + size_t alloclen; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, datalen, 1); + + if (git_buf_grow(buffer, alloclen) < 0) + return -1; + + memmove(buffer->ptr, data, datalen); + buffer->size = datalen; + buffer->ptr[buffer->size] = '\0'; + + return 0; +} + +int git_buf_is_binary(const git_buf *buf) +{ + git_str str = GIT_STR_INIT_CONST(buf->ptr, buf->size); + return git_str_is_binary(&str); +} + +int git_buf_contains_nul(const git_buf *buf) +{ + git_str str = GIT_STR_INIT_CONST(buf->ptr, buf->size); + return git_str_contains_nul(&str); +} + +void git_buf_free(git_buf *buffer) +{ + git_buf_dispose(buffer); +} + +#endif diff --git a/src/libgit2/buf.h b/src/libgit2/buf.h new file mode 100644 index 00000000000..4bc7f270957 --- /dev/null +++ b/src/libgit2/buf.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_buf_h__ +#define INCLUDE_buf_h__ + +#include "git2/buffer.h" +#include "common.h" + +/* + * Adapts a private API that takes a `git_str` into a public API that + * takes a `git_buf`. + */ + +#define GIT_BUF_WRAP_PRIVATE(buf, fn, ...) \ + { \ + git_str str = GIT_STR_INIT; \ + int error; \ + if ((error = git_buf_tostr(&str, buf)) == 0 && \ + (error = fn(&str, __VA_ARGS__)) == 0) \ + error = git_buf_fromstr(buf, &str); \ + git_str_dispose(&str); \ + return error; \ +} + +/** + * "Sanitizes" a buffer from user input. This simply ensures that the + * `git_buf` has nice defaults if the user didn't set the members to + * anything, so that if we return early we don't leave it populated + * with nonsense. + */ +extern int git_buf_sanitize(git_buf *from_user); + +/** + * Populate a `git_str` from a `git_buf` for passing to libgit2 internal + * functions. Sanitizes the given `git_buf` before proceeding. The + * `git_buf` will no longer point to this memory. + */ +extern int git_buf_tostr(git_str *out, git_buf *buf); + +/** + * Populate a `git_buf` from a `git_str` for returning to a user. + * The `git_str` will no longer point to this memory. + */ +extern int git_buf_fromstr(git_buf *out, git_str *str); + +#endif diff --git a/src/libgit2/cache.c b/src/libgit2/cache.c new file mode 100644 index 00000000000..629c56387e0 --- /dev/null +++ b/src/libgit2/cache.c @@ -0,0 +1,258 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "cache.h" + +#include "repository.h" +#include "commit.h" +#include "thread.h" +#include "util.h" +#include "odb.h" +#include "object.h" +#include "git2/oid.h" +#include "hashmap_oid.h" + +GIT_HASHMAP_OID_FUNCTIONS(git_cache_oidmap, GIT_HASHMAP_INLINE, git_cached_obj *); + +bool git_cache__enabled = true; +ssize_t git_cache__max_storage = (256 * 1024 * 1024); +git_atomic_ssize git_cache__current_storage = {0}; + +static size_t git_cache__max_object_size[8] = { + 0, /* GIT_OBJECT__EXT1 */ + 4096, /* GIT_OBJECT_COMMIT */ + 4096, /* GIT_OBJECT_TREE */ + 0, /* GIT_OBJECT_BLOB */ + 4096, /* GIT_OBJECT_TAG */ + 0, /* GIT_OBJECT__EXT2 */ + 0, /* GIT_OBJECT_OFS_DELTA */ + 0 /* GIT_OBJECT_REF_DELTA */ +}; + +int git_cache_set_max_object_size(git_object_t type, size_t size) +{ + if (type < 0 || (size_t)type >= ARRAY_SIZE(git_cache__max_object_size)) { + git_error_set(GIT_ERROR_INVALID, "type out of range"); + return -1; + } + + git_cache__max_object_size[type] = size; + return 0; +} + +int git_cache_init(git_cache *cache) +{ + memset(cache, 0, sizeof(*cache)); + + if (git_rwlock_init(&cache->lock)) { + git_error_set(GIT_ERROR_OS, "failed to initialize cache rwlock"); + return -1; + } + + return 0; +} + +/* called with lock */ +static void clear_cache(git_cache *cache) +{ + git_cached_obj *evict = NULL; + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + + if (git_cache_size(cache) == 0) + return; + + while (git_cache_oidmap_iterate(&iter, NULL, &evict, &cache->map) == 0) + git_cached_obj_decref(evict); + + git_cache_oidmap_clear(&cache->map); + git_atomic_ssize_add(&git_cache__current_storage, -cache->used_memory); + cache->used_memory = 0; +} + +void git_cache_clear(git_cache *cache) +{ + if (git_rwlock_wrlock(&cache->lock) < 0) + return; + + clear_cache(cache); + + git_rwlock_wrunlock(&cache->lock); +} + +size_t git_cache_size(git_cache *cache) +{ + return git_cache_oidmap_size(&cache->map); +} + +void git_cache_dispose(git_cache *cache) +{ + git_cache_clear(cache); + git_cache_oidmap_dispose(&cache->map); + git_rwlock_free(&cache->lock); + git__memzero(cache, sizeof(*cache)); +} + +/* Called with lock */ +static void cache_evict_entries(git_cache *cache) +{ + size_t evict_count = git_cache_size(cache) / 2048; + ssize_t evicted_memory = 0; + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + + if (evict_count < 8) + evict_count = 8; + + /* do not infinite loop if there's not enough entries to evict */ + if (evict_count > git_cache_size(cache)) { + clear_cache(cache); + return; + } + + while (evict_count > 0) { + const git_oid *key; + git_cached_obj *evict; + + if (git_cache_oidmap_iterate(&iter, &key, &evict, &cache->map) != 0) + break; + + evict_count--; + evicted_memory += evict->size; + git_cache_oidmap_remove(&cache->map, key); + git_cached_obj_decref(evict); + } + + cache->used_memory -= evicted_memory; + git_atomic_ssize_add(&git_cache__current_storage, -evicted_memory); +} + +static bool cache_should_store(git_object_t object_type, size_t object_size) +{ + size_t max_size = git_cache__max_object_size[object_type]; + return git_cache__enabled && object_size < max_size; +} + +static void *cache_get(git_cache *cache, const git_oid *oid, unsigned int flags) +{ + git_cached_obj *entry = NULL; + + if (!git_cache__enabled || git_rwlock_rdlock(&cache->lock) < 0) + return NULL; + + if (git_cache_oidmap_get(&entry, &cache->map, oid) == 0) { + if (flags && entry->flags != flags) { + entry = NULL; + } else { + git_cached_obj_incref(entry); + } + } + + git_rwlock_rdunlock(&cache->lock); + + return entry; +} + +static void *cache_store(git_cache *cache, git_cached_obj *entry) +{ + git_cached_obj *stored_entry; + + git_cached_obj_incref(entry); + + if (!git_cache__enabled && cache->used_memory > 0) { + git_cache_clear(cache); + return entry; + } + + if (!cache_should_store(entry->type, entry->size)) + return entry; + + if (git_rwlock_wrlock(&cache->lock) < 0) + return entry; + + /* soften the load on the cache */ + if (git_atomic_ssize_get(&git_cache__current_storage) > git_cache__max_storage) + cache_evict_entries(cache); + + /* not found */ + if (git_cache_oidmap_get(&stored_entry, &cache->map, &entry->oid) != 0) { + if (git_cache_oidmap_put(&cache->map, &entry->oid, entry) == 0) { + git_cached_obj_incref(entry); + cache->used_memory += entry->size; + git_atomic_ssize_add(&git_cache__current_storage, (ssize_t)entry->size); + } + } + /* found */ + else { + if (stored_entry->flags == entry->flags) { + git_cached_obj_decref(entry); + git_cached_obj_incref(stored_entry); + entry = stored_entry; + } else if (stored_entry->flags == GIT_CACHE_STORE_RAW && + entry->flags == GIT_CACHE_STORE_PARSED) { + if (git_cache_oidmap_put(&cache->map, &entry->oid, entry) == 0) { + git_cached_obj_decref(stored_entry); + git_cached_obj_incref(entry); + } else { + git_cached_obj_decref(entry); + git_cached_obj_incref(stored_entry); + entry = stored_entry; + } + } else { + /* NO OP */ + } + } + + git_rwlock_wrunlock(&cache->lock); + return entry; +} + +void *git_cache_store_raw(git_cache *cache, git_odb_object *entry) +{ + entry->cached.flags = GIT_CACHE_STORE_RAW; + return cache_store(cache, (git_cached_obj *)entry); +} + +void *git_cache_store_parsed(git_cache *cache, git_object *entry) +{ + entry->cached.flags = GIT_CACHE_STORE_PARSED; + return cache_store(cache, (git_cached_obj *)entry); +} + +git_odb_object *git_cache_get_raw(git_cache *cache, const git_oid *oid) +{ + return cache_get(cache, oid, GIT_CACHE_STORE_RAW); +} + +git_object *git_cache_get_parsed(git_cache *cache, const git_oid *oid) +{ + return cache_get(cache, oid, GIT_CACHE_STORE_PARSED); +} + +void *git_cache_get_any(git_cache *cache, const git_oid *oid) +{ + return cache_get(cache, oid, GIT_CACHE_STORE_ANY); +} + +void git_cached_obj_decref(void *_obj) +{ + git_cached_obj *obj = _obj; + + if (git_atomic32_dec(&obj->refcount) == 0) { + switch (obj->flags) { + case GIT_CACHE_STORE_RAW: + git_odb_object__free(_obj); + break; + + case GIT_CACHE_STORE_PARSED: + git_object__free(_obj); + break; + + default: + git__free(_obj); + break; + } + } +} diff --git a/src/libgit2/cache.h b/src/libgit2/cache.h new file mode 100644 index 00000000000..4c14013a300 --- /dev/null +++ b/src/libgit2/cache.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_cache_h__ +#define INCLUDE_cache_h__ + +#include "common.h" + +#include "git2/common.h" +#include "git2/oid.h" +#include "git2/odb.h" + +#include "thread.h" +#include "hashmap_oid.h" + +enum { + GIT_CACHE_STORE_ANY = 0, + GIT_CACHE_STORE_RAW = 1, + GIT_CACHE_STORE_PARSED = 2 +}; + +typedef struct { + git_oid oid; + int16_t type; /* git_object_t value */ + uint16_t flags; /* GIT_CACHE_STORE value */ + size_t size; + git_atomic32 refcount; +} git_cached_obj; + +GIT_HASHMAP_OID_STRUCT(git_cache_oidmap, git_cached_obj *); + +typedef struct { + git_cache_oidmap map; + git_rwlock lock; + ssize_t used_memory; +} git_cache; + +extern bool git_cache__enabled; +extern ssize_t git_cache__max_storage; +extern git_atomic_ssize git_cache__current_storage; + +int git_cache_set_max_object_size(git_object_t type, size_t size); + +int git_cache_init(git_cache *cache); +void git_cache_dispose(git_cache *cache); +void git_cache_clear(git_cache *cache); +size_t git_cache_size(git_cache *cache); + +void *git_cache_store_raw(git_cache *cache, git_odb_object *entry); +void *git_cache_store_parsed(git_cache *cache, git_object *entry); + +git_odb_object *git_cache_get_raw(git_cache *cache, const git_oid *oid); +git_object *git_cache_get_parsed(git_cache *cache, const git_oid *oid); +void *git_cache_get_any(git_cache *cache, const git_oid *oid); + +GIT_INLINE(void) git_cached_obj_incref(void *_obj) +{ + git_cached_obj *obj = _obj; + git_atomic32_inc(&obj->refcount); +} + +void git_cached_obj_decref(void *_obj); + +#endif diff --git a/src/libgit2/checkout.c b/src/libgit2/checkout.c new file mode 100644 index 00000000000..f4b1ea96f84 --- /dev/null +++ b/src/libgit2/checkout.c @@ -0,0 +1,2823 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "checkout.h" + +#include "git2/repository.h" +#include "git2/refs.h" +#include "git2/tree.h" +#include "git2/blob.h" +#include "git2/config.h" +#include "git2/diff.h" +#include "git2/submodule.h" +#include "git2/sys/index.h" +#include "git2/sys/filter.h" +#include "git2/merge.h" + +#include "refs.h" +#include "repository.h" +#include "index.h" +#include "filter.h" +#include "blob.h" +#include "diff.h" +#include "diff_generate.h" +#include "pathspec.h" +#include "diff_xdiff.h" +#include "fs_path.h" +#include "attr.h" +#include "pool.h" +#include "path.h" +#include "hashmap_str.h" + +/* See docs/checkout-internals.md for more information */ + +enum { + CHECKOUT_ACTION__NONE = 0, + CHECKOUT_ACTION__REMOVE = 1, + CHECKOUT_ACTION__UPDATE_BLOB = 2, + CHECKOUT_ACTION__UPDATE_SUBMODULE = 4, + CHECKOUT_ACTION__CONFLICT = 8, + CHECKOUT_ACTION__REMOVE_CONFLICT = 16, + CHECKOUT_ACTION__UPDATE_CONFLICT = 32, + CHECKOUT_ACTION__MAX = 32, + CHECKOUT_ACTION__REMOVE_AND_UPDATE = + (CHECKOUT_ACTION__UPDATE_BLOB | CHECKOUT_ACTION__REMOVE) +}; + +typedef struct { + git_repository *repo; + git_iterator *target; + git_diff *diff; + git_checkout_options opts; + bool opts_free_baseline; + char *pfx; + git_index *index; + git_pool pool; + git_vector removes; + git_vector remove_conflicts; + git_vector update_conflicts; + git_vector *update_reuc; + git_vector *update_names; + git_str target_path; + size_t target_len; + git_str tmp; + unsigned int strategy; + int can_symlink; + int respect_filemode; + bool reload_submodules; + size_t total_steps; + size_t completed_steps; + git_checkout_perfdata perfdata; + git_hashset_str mkdir_pathcache; + git_attr_session attr_session; +} checkout_data; + +typedef struct { + const git_index_entry *ancestor; + const git_index_entry *ours; + const git_index_entry *theirs; + + unsigned int name_collision:1, + directoryfile:1, + one_to_two:1, + binary:1, + submodule:1; +} checkout_conflictdata; + +static int checkout_notify( + checkout_data *data, + git_checkout_notify_t why, + const git_diff_delta *delta, + const git_index_entry *wditem) +{ + git_diff_file wdfile; + const git_diff_file *baseline = NULL, *target = NULL, *workdir = NULL; + const char *path = NULL; + + if (!data->opts.notify_cb || + (why & data->opts.notify_flags) == 0) + return 0; + + if (wditem) { + memset(&wdfile, 0, sizeof(wdfile)); + + git_oid_cpy(&wdfile.id, &wditem->id); + wdfile.path = wditem->path; + wdfile.size = wditem->file_size; + wdfile.flags = GIT_DIFF_FLAG_VALID_ID; + wdfile.mode = wditem->mode; + + workdir = &wdfile; + + path = wditem->path; + } + + if (delta) { + switch (delta->status) { + case GIT_DELTA_UNMODIFIED: + case GIT_DELTA_MODIFIED: + case GIT_DELTA_TYPECHANGE: + default: + baseline = &delta->old_file; + target = &delta->new_file; + break; + case GIT_DELTA_ADDED: + case GIT_DELTA_IGNORED: + case GIT_DELTA_UNTRACKED: + case GIT_DELTA_UNREADABLE: + target = &delta->new_file; + break; + case GIT_DELTA_DELETED: + baseline = &delta->old_file; + break; + } + + path = delta->old_file.path; + } + + { + int error = data->opts.notify_cb( + why, path, baseline, target, workdir, data->opts.notify_payload); + + return git_error_set_after_callback_function( + error, "git_checkout notification"); + } +} + +GIT_INLINE(bool) is_workdir_base_or_new( + const git_oid *workdir_id, + const git_diff_file *baseitem, + const git_diff_file *newitem) +{ + return (git_oid__cmp(&baseitem->id, workdir_id) == 0 || + git_oid__cmp(&newitem->id, workdir_id) == 0); +} + +GIT_INLINE(bool) is_filemode_changed(git_filemode_t a, git_filemode_t b, int respect_filemode) +{ + /* If core.filemode = false, ignore links in the repository and executable bit changes */ + if (!respect_filemode) { + if (a == S_IFLNK) + a = GIT_FILEMODE_BLOB; + if (b == S_IFLNK) + b = GIT_FILEMODE_BLOB; + + a &= ~0111; + b &= ~0111; + } + + return (a != b); +} + +static bool checkout_is_workdir_modified( + checkout_data *data, + const git_diff_file *baseitem, + const git_diff_file *newitem, + const git_index_entry *wditem) +{ + git_oid oid; + const git_index_entry *ie; + + /* handle "modified" submodule */ + if (wditem->mode == GIT_FILEMODE_COMMIT) { + git_submodule *sm; + unsigned int sm_status = 0; + const git_oid *sm_oid = NULL; + bool rval = false; + + if (git_submodule_lookup(&sm, data->repo, wditem->path) < 0) { + git_error_clear(); + return true; + } + + if (git_submodule_status(&sm_status, data->repo, wditem->path, GIT_SUBMODULE_IGNORE_UNSPECIFIED) < 0 || + GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status)) + rval = true; + else if ((sm_oid = git_submodule_wd_id(sm)) == NULL) + rval = false; + else + rval = (git_oid__cmp(&baseitem->id, sm_oid) != 0); + + git_submodule_free(sm); + return rval; + } + + /* + * Look at the cache to decide if the workdir is modified: if the + * cache contents match the workdir contents, then we do not need + * to examine the working directory directly, instead we can + * examine the cache to see if _it_ has been modified. This allows + * us to avoid touching the disk. + */ + ie = git_index_get_bypath(data->index, wditem->path, 0); + + if (ie != NULL && + !git_index_entry_newer_than_index(ie, data->index) && + git_index_time_eq(&wditem->mtime, &ie->mtime) && + wditem->file_size == ie->file_size && + !is_filemode_changed(wditem->mode, ie->mode, data->respect_filemode)) { + + /* The workdir is modified iff the index entry is modified */ + return !is_workdir_base_or_new(&ie->id, baseitem, newitem) || + is_filemode_changed(baseitem->mode, ie->mode, data->respect_filemode); + } + + /* depending on where base is coming from, we may or may not know + * the actual size of the data, so we can't rely on this shortcut. + */ + if (baseitem->size && wditem->file_size != baseitem->size) + return true; + + /* if the workdir item is a directory, it cannot be a modified file */ + if (S_ISDIR(wditem->mode)) + return false; + + if (is_filemode_changed(baseitem->mode, wditem->mode, data->respect_filemode)) + return true; + + if (git_diff__oid_for_entry(&oid, data->diff, wditem, wditem->mode, NULL) < 0) + return false; + + /* Allow the checkout if the workdir is not modified *or* if the checkout + * target's contents are already in the working directory. + */ + return !is_workdir_base_or_new(&oid, baseitem, newitem); +} + +#define CHECKOUT_ACTION_IF(FLAG,YES,NO) \ + ((data->strategy & GIT_CHECKOUT_##FLAG) ? CHECKOUT_ACTION__##YES : CHECKOUT_ACTION__##NO) + +static int checkout_action_common( + int *action, + checkout_data *data, + const git_diff_delta *delta, + const git_index_entry *wd) +{ + git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE; + + if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) + *action = (*action & ~CHECKOUT_ACTION__REMOVE); + + if ((*action & CHECKOUT_ACTION__UPDATE_BLOB) != 0) { + if (S_ISGITLINK(delta->new_file.mode)) + *action = (*action & ~CHECKOUT_ACTION__UPDATE_BLOB) | + CHECKOUT_ACTION__UPDATE_SUBMODULE; + + /* to "update" a symlink, we must remove the old one first */ + if (delta->new_file.mode == GIT_FILEMODE_LINK && wd != NULL) + *action |= CHECKOUT_ACTION__REMOVE; + + /* if the file is on disk and doesn't match our mode, force update */ + if (wd && + GIT_PERMS_IS_EXEC(wd->mode) != GIT_PERMS_IS_EXEC(delta->new_file.mode)) + *action |= CHECKOUT_ACTION__REMOVE; + + notify = GIT_CHECKOUT_NOTIFY_UPDATED; + } + + if ((*action & CHECKOUT_ACTION__CONFLICT) != 0) + notify = GIT_CHECKOUT_NOTIFY_CONFLICT; + + return checkout_notify(data, notify, delta, wd); +} + +static int checkout_action_no_wd( + int *action, + checkout_data *data, + const git_diff_delta *delta) +{ + int error = 0; + + *action = CHECKOUT_ACTION__NONE; + + if ((data->strategy & GIT_CHECKOUT_NONE)) + return 0; + + switch (delta->status) { + case GIT_DELTA_UNMODIFIED: /* case 12 */ + error = checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL); + if (error) + return error; + *action = CHECKOUT_ACTION_IF(RECREATE_MISSING, UPDATE_BLOB, NONE); + break; + case GIT_DELTA_ADDED: /* case 2 or 28 (and 5 but not really) */ + *action = CHECKOUT_ACTION__UPDATE_BLOB; + break; + case GIT_DELTA_MODIFIED: /* case 13 (and 35 but not really) */ + *action = CHECKOUT_ACTION_IF(RECREATE_MISSING, UPDATE_BLOB, CONFLICT); + break; + case GIT_DELTA_TYPECHANGE: /* case 21 (B->T) and 28 (T->B)*/ + if (delta->new_file.mode == GIT_FILEMODE_TREE) + *action = CHECKOUT_ACTION__UPDATE_BLOB; + break; + case GIT_DELTA_DELETED: /* case 8 or 25 */ + *action = CHECKOUT_ACTION__REMOVE; + break; + default: /* impossible */ + break; + } + + return checkout_action_common(action, data, delta, NULL); +} + +static int checkout_target_fullpath( + git_str **out, checkout_data *data, const char *path) +{ + git_str_truncate(&data->target_path, data->target_len); + + if (path && git_str_puts(&data->target_path, path) < 0) + return -1; + + if (git_path_validate_str_length(data->repo, &data->target_path) < 0) + return -1; + + *out = &data->target_path; + + return 0; +} + +static bool wd_item_is_removable( + checkout_data *data, const git_index_entry *wd) +{ + git_str *full; + + if (wd->mode != GIT_FILEMODE_TREE) + return true; + + if (checkout_target_fullpath(&full, data, wd->path) < 0) + return false; + + return !full || !git_fs_path_contains(full, DOT_GIT); +} + +static int checkout_queue_remove(checkout_data *data, const char *path) +{ + char *copy = git_pool_strdup(&data->pool, path); + GIT_ERROR_CHECK_ALLOC(copy); + return git_vector_insert(&data->removes, copy); +} + +/* note that this advances the iterator over the wd item */ +static int checkout_action_wd_only( + checkout_data *data, + git_iterator *workdir, + const git_index_entry **wditem, + git_vector *pathspec) +{ + int error = 0; + bool remove = false; + git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE; + const git_index_entry *wd = *wditem; + + if (!git_pathspec__match( + pathspec, wd->path, + (data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0, + git_iterator_ignore_case(workdir), NULL, NULL)) { + + if (wd->mode == GIT_FILEMODE_TREE) + return git_iterator_advance_into(wditem, workdir); + else + return git_iterator_advance(wditem, workdir); + } + + /* check if item is tracked in the index but not in the checkout diff */ + if (data->index != NULL) { + size_t pos; + + error = git_index__find_pos( + &pos, data->index, wd->path, 0, GIT_INDEX_STAGE_ANY); + + if (wd->mode != GIT_FILEMODE_TREE) { + if (!error) { /* found by git_index__find_pos call */ + notify = GIT_CHECKOUT_NOTIFY_DIRTY; + remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0); + } else if (error != GIT_ENOTFOUND) + return error; + else + error = 0; /* git_index__find_pos does not set error msg */ + } else { + /* for tree entries, we have to see if there are any index + * entries that are contained inside that tree + */ + const git_index_entry *e = git_index_get_byindex(data->index, pos); + + if (e != NULL && data->diff->pfxcomp(e->path, wd->path) == 0) + return git_iterator_advance_into(wditem, workdir); + } + } + + if (notify != GIT_CHECKOUT_NOTIFY_NONE) { + /* if we found something in the index, notify and advance */ + if ((error = checkout_notify(data, notify, NULL, wd)) != 0) + return error; + + if (remove && wd_item_is_removable(data, wd)) + error = checkout_queue_remove(data, wd->path); + + if (!error) + error = git_iterator_advance(wditem, workdir); + } else { + /* untracked or ignored - can't know which until we advance through */ + bool over = false, removable = wd_item_is_removable(data, wd); + git_iterator_status_t untracked_state; + + /* copy the entry for issuing notification callback later */ + git_index_entry saved_wd = *wd; + git_str_sets(&data->tmp, wd->path); + saved_wd.path = data->tmp.ptr; + + error = git_iterator_advance_over( + wditem, &untracked_state, workdir); + if (error == GIT_ITEROVER) + over = true; + else if (error < 0) + return error; + + if (untracked_state == GIT_ITERATOR_STATUS_IGNORED) { + notify = GIT_CHECKOUT_NOTIFY_IGNORED; + remove = ((data->strategy & GIT_CHECKOUT_REMOVE_IGNORED) != 0); + } else { + notify = GIT_CHECKOUT_NOTIFY_UNTRACKED; + remove = ((data->strategy & GIT_CHECKOUT_REMOVE_UNTRACKED) != 0); + } + + if ((error = checkout_notify(data, notify, NULL, &saved_wd)) != 0) + return error; + + if (remove && removable) + error = checkout_queue_remove(data, saved_wd.path); + + if (!error && over) /* restore ITEROVER if needed */ + error = GIT_ITEROVER; + } + + return error; +} + +static bool submodule_is_config_only( + checkout_data *data, + const char *path) +{ + git_submodule *sm = NULL; + unsigned int sm_loc = 0; + bool rval = false; + + if (git_submodule_lookup(&sm, data->repo, path) < 0) + return true; + + if (git_submodule_location(&sm_loc, sm) < 0 || + sm_loc == GIT_SUBMODULE_STATUS_IN_CONFIG) + rval = true; + + git_submodule_free(sm); + + return rval; +} + +static bool checkout_is_empty_dir(checkout_data *data, const char *path) +{ + git_str *fullpath; + + if (checkout_target_fullpath(&fullpath, data, path) < 0) + return false; + + return git_fs_path_is_empty_dir(fullpath->ptr); +} + +static int checkout_action_with_wd( + int *action, + checkout_data *data, + const git_diff_delta *delta, + git_iterator *workdir, + const git_index_entry *wd) +{ + *action = CHECKOUT_ACTION__NONE; + + if ((data->strategy & GIT_CHECKOUT_NONE)) + return 0; + + switch (delta->status) { + case GIT_DELTA_UNMODIFIED: /* case 14/15 or 33 */ + if (checkout_is_workdir_modified(data, &delta->old_file, &delta->new_file, wd)) { + GIT_ERROR_CHECK_ERROR( + checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd) ); + *action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, NONE); + } + break; + case GIT_DELTA_ADDED: /* case 3, 4 or 6 */ + if (git_iterator_current_is_ignored(workdir)) + *action = CHECKOUT_ACTION_IF(DONT_OVERWRITE_IGNORED, CONFLICT, UPDATE_BLOB); + else + *action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT); + break; + case GIT_DELTA_DELETED: /* case 9 or 10 (or 26 but not really) */ + if (checkout_is_workdir_modified(data, &delta->old_file, &delta->new_file, wd)) + *action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT); + else + *action = CHECKOUT_ACTION__REMOVE; + break; + case GIT_DELTA_MODIFIED: /* case 16, 17, 18 (or 36 but not really) */ + if (wd->mode != GIT_FILEMODE_COMMIT && + checkout_is_workdir_modified(data, &delta->old_file, &delta->new_file, wd)) + *action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT); + else + *action = CHECKOUT_ACTION__UPDATE_BLOB; + break; + case GIT_DELTA_TYPECHANGE: /* case 22, 23, 29, 30 */ + if (delta->old_file.mode == GIT_FILEMODE_TREE) { + if (wd->mode == GIT_FILEMODE_TREE) + /* either deleting items in old tree will delete the wd dir, + * or we'll get a conflict when we attempt blob update... + */ + *action = CHECKOUT_ACTION__UPDATE_BLOB; + else if (wd->mode == GIT_FILEMODE_COMMIT) { + /* workdir is possibly a "phantom" submodule - treat as a + * tree if the only submodule info came from the config + */ + if (submodule_is_config_only(data, wd->path)) + *action = CHECKOUT_ACTION__UPDATE_BLOB; + else + *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); + } else + *action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT); + } + else if (checkout_is_workdir_modified(data, &delta->old_file, &delta->new_file, wd)) + *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); + else + *action = CHECKOUT_ACTION__REMOVE_AND_UPDATE; + + /* don't update if the typechange is to a tree */ + if (delta->new_file.mode == GIT_FILEMODE_TREE) + *action = (*action & ~CHECKOUT_ACTION__UPDATE_BLOB); + break; + default: /* impossible */ + break; + } + + return checkout_action_common(action, data, delta, wd); +} + +static int checkout_action_with_wd_blocker( + int *action, + checkout_data *data, + const git_diff_delta *delta, + const git_index_entry *wd) +{ + *action = CHECKOUT_ACTION__NONE; + + if ((data->strategy & GIT_CHECKOUT_NONE)) + return 0; + + switch (delta->status) { + case GIT_DELTA_UNMODIFIED: + /* should show delta as dirty / deleted */ + GIT_ERROR_CHECK_ERROR( + checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd) ); + *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, NONE); + break; + case GIT_DELTA_ADDED: + case GIT_DELTA_MODIFIED: + *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); + break; + case GIT_DELTA_DELETED: + *action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT); + break; + case GIT_DELTA_TYPECHANGE: + /* not 100% certain about this... */ + *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); + break; + default: /* impossible */ + break; + } + + return checkout_action_common(action, data, delta, wd); +} + +static int checkout_action_with_wd_dir( + int *action, + checkout_data *data, + const git_diff_delta *delta, + git_iterator *workdir, + const git_index_entry *wd) +{ + *action = CHECKOUT_ACTION__NONE; + + if ((data->strategy & GIT_CHECKOUT_NONE)) + return 0; + + switch (delta->status) { + case GIT_DELTA_UNMODIFIED: /* case 19 or 24 (or 34 but not really) */ + GIT_ERROR_CHECK_ERROR( + checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL)); + GIT_ERROR_CHECK_ERROR( + checkout_notify(data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd)); + *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, NONE); + break; + case GIT_DELTA_ADDED:/* case 4 (and 7 for dir) */ + case GIT_DELTA_MODIFIED: /* case 20 (or 37 but not really) */ + if (delta->old_file.mode == GIT_FILEMODE_COMMIT) + /* expected submodule (and maybe found one) */; + else if (delta->new_file.mode != GIT_FILEMODE_TREE) + *action = git_iterator_current_is_ignored(workdir) ? + CHECKOUT_ACTION_IF(DONT_OVERWRITE_IGNORED, CONFLICT, REMOVE_AND_UPDATE) : + CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); + break; + case GIT_DELTA_DELETED: /* case 11 (and 27 for dir) */ + if (delta->old_file.mode != GIT_FILEMODE_TREE) + GIT_ERROR_CHECK_ERROR( + checkout_notify(data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd)); + break; + case GIT_DELTA_TYPECHANGE: /* case 24 or 31 */ + if (delta->old_file.mode == GIT_FILEMODE_TREE) { + /* For typechange from dir, remove dir and add blob, but it is + * not safe to remove dir if it contains modified files. + * However, safely removing child files will remove the parent + * directory if is it left empty, so we can defer removing the + * dir and it will succeed if no children are left. + */ + *action = CHECKOUT_ACTION__UPDATE_BLOB; + } + else if (delta->new_file.mode != GIT_FILEMODE_TREE) + /* For typechange to dir, dir is already created so no action */ + *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); + break; + default: /* impossible */ + break; + } + + return checkout_action_common(action, data, delta, wd); +} + +static int checkout_action_with_wd_dir_empty( + int *action, + checkout_data *data, + const git_diff_delta *delta) +{ + int error = checkout_action_no_wd(action, data, delta); + + /* We can always safely remove an empty directory. */ + if (error == 0 && *action != CHECKOUT_ACTION__NONE) + *action |= CHECKOUT_ACTION__REMOVE; + + return error; +} + +static int checkout_action( + int *action, + checkout_data *data, + git_diff_delta *delta, + git_iterator *workdir, + const git_index_entry **wditem, + git_vector *pathspec) +{ + int cmp = -1, error; + int (*strcomp)(const char *, const char *) = data->diff->strcomp; + int (*pfxcomp)(const char *str, const char *pfx) = data->diff->pfxcomp; + int (*advance)(const git_index_entry **, git_iterator *) = NULL; + + /* move workdir iterator to follow along with deltas */ + + while (1) { + const git_index_entry *wd = *wditem; + + if (!wd) + return checkout_action_no_wd(action, data, delta); + + cmp = strcomp(wd->path, delta->old_file.path); + + /* 1. wd before delta ("a/a" before "a/b") + * 2. wd prefixes delta & should expand ("a/" before "a/b") + * 3. wd prefixes delta & cannot expand ("a/b" before "a/b/c") + * 4. wd equals delta ("a/b" and "a/b") + * 5. wd after delta & delta prefixes wd ("a/b/c" after "a/b/" or "a/b") + * 6. wd after delta ("a/c" after "a/b") + */ + + if (cmp < 0) { + cmp = pfxcomp(delta->old_file.path, wd->path); + + if (cmp == 0) { + if (wd->mode == GIT_FILEMODE_TREE) { + /* case 2 - entry prefixed by workdir tree */ + error = git_iterator_advance_into(wditem, workdir); + if (error < 0 && error != GIT_ITEROVER) + goto done; + continue; + } + + /* case 3 maybe - wd contains non-dir where dir expected */ + if (delta->old_file.path[strlen(wd->path)] == '/') { + error = checkout_action_with_wd_blocker( + action, data, delta, wd); + advance = git_iterator_advance; + goto done; + } + } + + /* case 1 - handle wd item (if it matches pathspec) */ + error = checkout_action_wd_only(data, workdir, wditem, pathspec); + if (error && error != GIT_ITEROVER) + goto done; + continue; + } + + if (cmp == 0) { + /* case 4 */ + error = checkout_action_with_wd(action, data, delta, workdir, wd); + advance = git_iterator_advance; + goto done; + } + + cmp = pfxcomp(wd->path, delta->old_file.path); + + if (cmp == 0) { /* case 5 */ + if (wd->path[strlen(delta->old_file.path)] != '/') + return checkout_action_no_wd(action, data, delta); + + if (delta->status == GIT_DELTA_TYPECHANGE) { + if (delta->old_file.mode == GIT_FILEMODE_TREE) { + error = checkout_action_with_wd(action, data, delta, workdir, wd); + advance = git_iterator_advance_into; + goto done; + } + + if (delta->new_file.mode == GIT_FILEMODE_TREE || + delta->new_file.mode == GIT_FILEMODE_COMMIT || + delta->old_file.mode == GIT_FILEMODE_COMMIT) + { + error = checkout_action_with_wd(action, data, delta, workdir, wd); + advance = git_iterator_advance; + goto done; + } + } + + return checkout_is_empty_dir(data, wd->path) ? + checkout_action_with_wd_dir_empty(action, data, delta) : + checkout_action_with_wd_dir(action, data, delta, workdir, wd); + } + + /* case 6 - wd is after delta */ + return checkout_action_no_wd(action, data, delta); + } + +done: + if (!error && advance != NULL && + (error = advance(wditem, workdir)) < 0) { + *wditem = NULL; + if (error == GIT_ITEROVER) + error = 0; + } + + return error; +} + +static int checkout_remaining_wd_items( + checkout_data *data, + git_iterator *workdir, + const git_index_entry *wd, + git_vector *spec) +{ + int error = 0; + + while (wd && !error) + error = checkout_action_wd_only(data, workdir, &wd, spec); + + if (error == GIT_ITEROVER) + error = 0; + + return error; +} + +GIT_INLINE(int) checkout_idxentry_cmp( + const git_index_entry *a, + const git_index_entry *b) +{ + if (!a && !b) + return 0; + else if (!a && b) + return -1; + else if(a && !b) + return 1; + else + return strcmp(a->path, b->path); +} + +static int checkout_conflictdata_cmp(const void *a, const void *b) +{ + const checkout_conflictdata *ca = a; + const checkout_conflictdata *cb = b; + int diff; + + if ((diff = checkout_idxentry_cmp(ca->ancestor, cb->ancestor)) == 0 && + (diff = checkout_idxentry_cmp(ca->ours, cb->theirs)) == 0) + diff = checkout_idxentry_cmp(ca->theirs, cb->theirs); + + return diff; +} + +static int checkout_conflictdata_empty( + const git_vector *conflicts, size_t idx, void *payload) +{ + checkout_conflictdata *conflict; + + GIT_UNUSED(payload); + + if ((conflict = git_vector_get(conflicts, idx)) == NULL) + return -1; + + if (conflict->ancestor || conflict->ours || conflict->theirs) + return 0; + + git__free(conflict); + return 1; +} + +GIT_INLINE(bool) conflict_pathspec_match( + checkout_data *data, + git_iterator *workdir, + git_vector *pathspec, + const git_index_entry *ancestor, + const git_index_entry *ours, + const git_index_entry *theirs) +{ + /* if the pathspec matches ours *or* theirs, proceed */ + if (ours && git_pathspec__match(pathspec, ours->path, + (data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0, + git_iterator_ignore_case(workdir), NULL, NULL)) + return true; + + if (theirs && git_pathspec__match(pathspec, theirs->path, + (data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0, + git_iterator_ignore_case(workdir), NULL, NULL)) + return true; + + if (ancestor && git_pathspec__match(pathspec, ancestor->path, + (data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0, + git_iterator_ignore_case(workdir), NULL, NULL)) + return true; + + return false; +} + +GIT_INLINE(int) checkout_conflict_detect_submodule(checkout_conflictdata *conflict) +{ + conflict->submodule = ((conflict->ancestor && S_ISGITLINK(conflict->ancestor->mode)) || + (conflict->ours && S_ISGITLINK(conflict->ours->mode)) || + (conflict->theirs && S_ISGITLINK(conflict->theirs->mode))); + return 0; +} + +GIT_INLINE(int) checkout_conflict_detect_binary(git_repository *repo, checkout_conflictdata *conflict) +{ + git_blob *ancestor_blob = NULL, *our_blob = NULL, *their_blob = NULL; + int error = 0; + + if (conflict->submodule) + return 0; + + if (conflict->ancestor) { + if ((error = git_blob_lookup(&ancestor_blob, repo, &conflict->ancestor->id)) < 0) + goto done; + + conflict->binary = git_blob_is_binary(ancestor_blob); + } + + if (!conflict->binary && conflict->ours) { + if ((error = git_blob_lookup(&our_blob, repo, &conflict->ours->id)) < 0) + goto done; + + conflict->binary = git_blob_is_binary(our_blob); + } + + if (!conflict->binary && conflict->theirs) { + if ((error = git_blob_lookup(&their_blob, repo, &conflict->theirs->id)) < 0) + goto done; + + conflict->binary = git_blob_is_binary(their_blob); + } + +done: + git_blob_free(ancestor_blob); + git_blob_free(our_blob); + git_blob_free(their_blob); + + return error; +} + +static int checkout_conflict_append_update( + const git_index_entry *ancestor, + const git_index_entry *ours, + const git_index_entry *theirs, + void *payload) +{ + checkout_data *data = payload; + checkout_conflictdata *conflict; + int error; + + conflict = git__calloc(1, sizeof(checkout_conflictdata)); + GIT_ERROR_CHECK_ALLOC(conflict); + + conflict->ancestor = ancestor; + conflict->ours = ours; + conflict->theirs = theirs; + + if ((error = checkout_conflict_detect_submodule(conflict)) < 0 || + (error = checkout_conflict_detect_binary(data->repo, conflict)) < 0) + { + git__free(conflict); + return error; + } + + if (git_vector_insert(&data->update_conflicts, conflict)) + return -1; + + return 0; +} + +static int checkout_conflicts_foreach( + checkout_data *data, + git_index *index, + git_iterator *workdir, + git_vector *pathspec, + int (*cb)(const git_index_entry *, const git_index_entry *, const git_index_entry *, void *), + void *payload) +{ + git_index_conflict_iterator *iterator = NULL; + const git_index_entry *ancestor, *ours, *theirs; + int error = 0; + + if ((error = git_index_conflict_iterator_new(&iterator, index)) < 0) + goto done; + + /* Collect the conflicts */ + while ((error = git_index_conflict_next(&ancestor, &ours, &theirs, iterator)) == 0) { + if (!conflict_pathspec_match(data, workdir, pathspec, ancestor, ours, theirs)) + continue; + + if ((error = cb(ancestor, ours, theirs, payload)) < 0) + goto done; + } + + if (error == GIT_ITEROVER) + error = 0; + +done: + git_index_conflict_iterator_free(iterator); + + return error; +} + +static int checkout_conflicts_load(checkout_data *data, git_iterator *workdir, git_vector *pathspec) +{ + git_index *index; + + /* Only write conflicts from sources that have them: indexes. */ + if ((index = git_iterator_index(data->target)) == NULL) + return 0; + + data->update_conflicts._cmp = checkout_conflictdata_cmp; + + if (checkout_conflicts_foreach(data, index, workdir, pathspec, checkout_conflict_append_update, data) < 0) + return -1; + + /* Collect the REUC and NAME entries */ + data->update_reuc = &index->reuc; + data->update_names = &index->names; + + return 0; +} + +GIT_INLINE(int) checkout_conflicts_cmp_entry( + const char *path, + const git_index_entry *entry) +{ + return strcmp((const char *)path, entry->path); +} + +static int checkout_conflicts_cmp_ancestor(const void *p, const void *c) +{ + const char *path = p; + const checkout_conflictdata *conflict = c; + + if (!conflict->ancestor) + return 1; + + return checkout_conflicts_cmp_entry(path, conflict->ancestor); +} + +static checkout_conflictdata *checkout_conflicts_search_ancestor( + checkout_data *data, + const char *path) +{ + size_t pos; + + if (git_vector_bsearch2(&pos, &data->update_conflicts, checkout_conflicts_cmp_ancestor, path) < 0) + return NULL; + + return git_vector_get(&data->update_conflicts, pos); +} + +static checkout_conflictdata *checkout_conflicts_search_branch( + checkout_data *data, + const char *path) +{ + checkout_conflictdata *conflict; + size_t i; + + git_vector_foreach(&data->update_conflicts, i, conflict) { + int cmp = -1; + + if (conflict->ancestor) + break; + + if (conflict->ours) + cmp = checkout_conflicts_cmp_entry(path, conflict->ours); + else if (conflict->theirs) + cmp = checkout_conflicts_cmp_entry(path, conflict->theirs); + + if (cmp == 0) + return conflict; + } + + return NULL; +} + +static int checkout_conflicts_load_byname_entry( + checkout_conflictdata **ancestor_out, + checkout_conflictdata **ours_out, + checkout_conflictdata **theirs_out, + checkout_data *data, + const git_index_name_entry *name_entry) +{ + checkout_conflictdata *ancestor, *ours = NULL, *theirs = NULL; + int error = 0; + + *ancestor_out = NULL; + *ours_out = NULL; + *theirs_out = NULL; + + if (!name_entry->ancestor) { + git_error_set(GIT_ERROR_INDEX, "a NAME entry exists without an ancestor"); + error = -1; + goto done; + } + + if (!name_entry->ours && !name_entry->theirs) { + git_error_set(GIT_ERROR_INDEX, "a NAME entry exists without an ours or theirs"); + error = -1; + goto done; + } + + if ((ancestor = checkout_conflicts_search_ancestor(data, + name_entry->ancestor)) == NULL) { + git_error_set(GIT_ERROR_INDEX, + "a NAME entry referenced ancestor entry '%s' which does not exist in the main index", + name_entry->ancestor); + error = -1; + goto done; + } + + if (name_entry->ours) { + if (strcmp(name_entry->ancestor, name_entry->ours) == 0) + ours = ancestor; + else if ((ours = checkout_conflicts_search_branch(data, name_entry->ours)) == NULL || + ours->ours == NULL) { + git_error_set(GIT_ERROR_INDEX, + "a NAME entry referenced our entry '%s' which does not exist in the main index", + name_entry->ours); + error = -1; + goto done; + } + } + + if (name_entry->theirs) { + if (strcmp(name_entry->ancestor, name_entry->theirs) == 0) + theirs = ancestor; + else if (name_entry->ours && strcmp(name_entry->ours, name_entry->theirs) == 0) + theirs = ours; + else if ((theirs = checkout_conflicts_search_branch(data, name_entry->theirs)) == NULL || + theirs->theirs == NULL) { + git_error_set(GIT_ERROR_INDEX, + "a NAME entry referenced their entry '%s' which does not exist in the main index", + name_entry->theirs); + error = -1; + goto done; + } + } + + *ancestor_out = ancestor; + *ours_out = ours; + *theirs_out = theirs; + +done: + return error; +} + +static int checkout_conflicts_coalesce_renames( + checkout_data *data) +{ + git_index *index; + const git_index_name_entry *name_entry; + checkout_conflictdata *ancestor_conflict, *our_conflict, *their_conflict; + size_t i, names; + int error = 0; + + if ((index = git_iterator_index(data->target)) == NULL) + return 0; + + /* Juggle entries based on renames */ + names = git_index_name_entrycount(index); + + for (i = 0; i < names; i++) { + name_entry = git_index_name_get_byindex(index, i); + + if ((error = checkout_conflicts_load_byname_entry( + &ancestor_conflict, &our_conflict, &their_conflict, + data, name_entry)) < 0) + goto done; + + if (our_conflict && our_conflict != ancestor_conflict) { + ancestor_conflict->ours = our_conflict->ours; + our_conflict->ours = NULL; + + if (our_conflict->theirs) + our_conflict->name_collision = 1; + + if (our_conflict->name_collision) + ancestor_conflict->name_collision = 1; + } + + if (their_conflict && their_conflict != ancestor_conflict) { + ancestor_conflict->theirs = their_conflict->theirs; + their_conflict->theirs = NULL; + + if (their_conflict->ours) + their_conflict->name_collision = 1; + + if (their_conflict->name_collision) + ancestor_conflict->name_collision = 1; + } + + if (our_conflict && our_conflict != ancestor_conflict && + their_conflict && their_conflict != ancestor_conflict) + ancestor_conflict->one_to_two = 1; + } + + git_vector_remove_matching( + &data->update_conflicts, checkout_conflictdata_empty, NULL); + +done: + return error; +} + +static int checkout_conflicts_mark_directoryfile( + checkout_data *data) +{ + git_index *index; + checkout_conflictdata *conflict; + const git_index_entry *entry; + size_t i, j, len; + const char *path; + int prefixed, error = 0; + + if ((index = git_iterator_index(data->target)) == NULL) + return 0; + + len = git_index_entrycount(index); + + /* Find d/f conflicts */ + git_vector_foreach(&data->update_conflicts, i, conflict) { + if ((conflict->ours && conflict->theirs) || + (!conflict->ours && !conflict->theirs)) + continue; + + path = conflict->ours ? + conflict->ours->path : conflict->theirs->path; + + if ((error = git_index_find(&j, index, path)) < 0) { + if (error == GIT_ENOTFOUND) + git_error_set(GIT_ERROR_INDEX, + "index inconsistency, could not find entry for expected conflict '%s'", path); + + goto done; + } + + for (; j < len; j++) { + if ((entry = git_index_get_byindex(index, j)) == NULL) { + git_error_set(GIT_ERROR_INDEX, + "index inconsistency, truncated index while loading expected conflict '%s'", path); + error = -1; + goto done; + } + + prefixed = git_fs_path_equal_or_prefixed(path, entry->path, NULL); + + if (prefixed == GIT_FS_PATH_EQUAL) + continue; + + if (prefixed == GIT_FS_PATH_PREFIX) + conflict->directoryfile = 1; + + break; + } + } + +done: + return error; +} + +static int checkout_get_update_conflicts( + checkout_data *data, + git_iterator *workdir, + git_vector *pathspec) +{ + int error = 0; + + if (data->strategy & GIT_CHECKOUT_SKIP_UNMERGED) + return 0; + + if ((error = checkout_conflicts_load(data, workdir, pathspec)) < 0 || + (error = checkout_conflicts_coalesce_renames(data)) < 0 || + (error = checkout_conflicts_mark_directoryfile(data)) < 0) + goto done; + +done: + return error; +} + +static int checkout_conflict_append_remove( + const git_index_entry *ancestor, + const git_index_entry *ours, + const git_index_entry *theirs, + void *payload) +{ + checkout_data *data = payload; + const char *name; + + GIT_ASSERT_ARG(ancestor || ours || theirs); + + if (ancestor) + name = git__strdup(ancestor->path); + else if (ours) + name = git__strdup(ours->path); + else if (theirs) + name = git__strdup(theirs->path); + else + abort(); + + GIT_ERROR_CHECK_ALLOC(name); + + return git_vector_insert(&data->remove_conflicts, (char *)name); +} + +static int checkout_get_remove_conflicts( + checkout_data *data, + git_iterator *workdir, + git_vector *pathspec) +{ + if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) != 0) + return 0; + + return checkout_conflicts_foreach(data, data->index, workdir, pathspec, checkout_conflict_append_remove, data); +} + +static int checkout_verify_paths( + git_repository *repo, + int action, + git_diff_delta *delta) +{ + unsigned int flags = GIT_PATH_REJECT_WORKDIR_DEFAULTS; + + if (action & CHECKOUT_ACTION__REMOVE) { + if (!git_path_is_valid(repo, delta->old_file.path, delta->old_file.mode, flags)) { + git_error_set(GIT_ERROR_CHECKOUT, "cannot remove invalid path '%s'", delta->old_file.path); + return -1; + } + } + + if (action & ~CHECKOUT_ACTION__REMOVE) { + if (!git_path_is_valid(repo, delta->new_file.path, delta->new_file.mode, flags)) { + git_error_set(GIT_ERROR_CHECKOUT, "cannot checkout to invalid path '%s'", delta->new_file.path); + return -1; + } + } + + return 0; +} + +static int checkout_get_actions( + uint32_t **actions_ptr, + size_t **counts_ptr, + checkout_data *data, + git_iterator *workdir) +{ + int error = 0, act; + const git_index_entry *wditem; + git_vector pathspec = GIT_VECTOR_INIT, *deltas; + git_pool pathpool; + git_diff_delta *delta; + size_t i, *counts = NULL; + uint32_t *actions = NULL; + + if (git_pool_init(&pathpool, 1) < 0) + return -1; + + if (data->opts.paths.count > 0 && + git_pathspec__vinit(&pathspec, &data->opts.paths, &pathpool) < 0) + return -1; + + if ((error = git_iterator_current(&wditem, workdir)) < 0 && + error != GIT_ITEROVER) + goto fail; + + deltas = &data->diff->deltas; + + *counts_ptr = counts = git__calloc(CHECKOUT_ACTION__MAX+1, sizeof(size_t)); + *actions_ptr = actions = git__calloc( + deltas->length ? deltas->length : 1, sizeof(uint32_t)); + if (!counts || !actions) { + error = -1; + goto fail; + } + + git_vector_foreach(deltas, i, delta) { + if ((error = checkout_action(&act, data, delta, workdir, &wditem, &pathspec)) == 0) + error = checkout_verify_paths(data->repo, act, delta); + + if (error != 0) + goto fail; + + actions[i] = act; + + if (act & CHECKOUT_ACTION__REMOVE) + counts[CHECKOUT_ACTION__REMOVE]++; + if (act & CHECKOUT_ACTION__UPDATE_BLOB) + counts[CHECKOUT_ACTION__UPDATE_BLOB]++; + if (act & CHECKOUT_ACTION__UPDATE_SUBMODULE) + counts[CHECKOUT_ACTION__UPDATE_SUBMODULE]++; + if (act & CHECKOUT_ACTION__CONFLICT) + counts[CHECKOUT_ACTION__CONFLICT]++; + } + + error = checkout_remaining_wd_items(data, workdir, wditem, &pathspec); + if (error) + goto fail; + + counts[CHECKOUT_ACTION__REMOVE] += data->removes.length; + + if (counts[CHECKOUT_ACTION__CONFLICT] > 0 && + (data->strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) == 0) { + git_error_set(GIT_ERROR_CHECKOUT, "%"PRIuZ" %s checkout", + counts[CHECKOUT_ACTION__CONFLICT], + counts[CHECKOUT_ACTION__CONFLICT] == 1 ? + "conflict prevents" : "conflicts prevent"); + error = GIT_ECONFLICT; + goto fail; + } + + + if ((error = checkout_get_remove_conflicts(data, workdir, &pathspec)) < 0 || + (error = checkout_get_update_conflicts(data, workdir, &pathspec)) < 0) + goto fail; + + counts[CHECKOUT_ACTION__REMOVE_CONFLICT] = git_vector_length(&data->remove_conflicts); + counts[CHECKOUT_ACTION__UPDATE_CONFLICT] = git_vector_length(&data->update_conflicts); + + git_pathspec__vfree(&pathspec); + git_pool_clear(&pathpool); + + return 0; + +fail: + *counts_ptr = NULL; + git__free(counts); + *actions_ptr = NULL; + git__free(actions); + + git_pathspec__vfree(&pathspec); + git_pool_clear(&pathpool); + + return error; +} + +static bool should_remove_existing(checkout_data *data) +{ + int ignorecase; + + if (git_repository__configmap_lookup(&ignorecase, data->repo, GIT_CONFIGMAP_IGNORECASE) < 0) { + ignorecase = 0; + } + + return (ignorecase && + (data->strategy & GIT_CHECKOUT_DONT_REMOVE_EXISTING) == 0); +} + +#define MKDIR_NORMAL \ + GIT_MKDIR_PATH | GIT_MKDIR_VERIFY_DIR +#define MKDIR_REMOVE_EXISTING \ + MKDIR_NORMAL | GIT_MKDIR_REMOVE_FILES | GIT_MKDIR_REMOVE_SYMLINKS + +static int checkout_mkdir( + checkout_data *data, + const char *path, + const char *base, + mode_t mode, + unsigned int flags) +{ + struct git_futils_mkdir_options mkdir_opts = {0}; + int error; + + if (git_pool_is_initialized(&data->pool)) { + mkdir_opts.cache_pool = &data->pool; + mkdir_opts.cache_pathset = &data->mkdir_pathcache; + } + + error = git_futils_mkdir_relative( + path, base, mode, flags, &mkdir_opts); + + data->perfdata.mkdir_calls += mkdir_opts.perfdata.mkdir_calls; + data->perfdata.stat_calls += mkdir_opts.perfdata.stat_calls; + data->perfdata.chmod_calls += mkdir_opts.perfdata.chmod_calls; + + return error; +} + +static int mkpath2file( + checkout_data *data, const char *path, unsigned int mode) +{ + struct stat st; + bool remove_existing = should_remove_existing(data); + unsigned int flags = + (remove_existing ? MKDIR_REMOVE_EXISTING : MKDIR_NORMAL) | + GIT_MKDIR_SKIP_LAST; + int error; + + if ((error = checkout_mkdir( + data, path, data->opts.target_directory, mode, flags)) < 0) + return error; + + if (remove_existing) { + data->perfdata.stat_calls++; + + if (p_lstat(path, &st) == 0) { + + /* Some file, symlink or folder already exists at this name. + * We would have removed it in remove_the_old unless we're on + * a case inensitive filesystem (or the user has asked us not + * to). Remove the similarly named file to write the new. + */ + error = git_futils_rmdir_r(path, NULL, GIT_RMDIR_REMOVE_FILES); + } else if (errno != ENOENT) { + git_error_set(GIT_ERROR_OS, "failed to stat '%s'", path); + return GIT_EEXISTS; + } else { + git_error_clear(); + } + } + + return error; +} + +struct checkout_stream { + git_writestream base; + const char *path; + int fd; + int open; +}; + +static int checkout_stream_write( + git_writestream *s, const char *buffer, size_t len) +{ + struct checkout_stream *stream = (struct checkout_stream *)s; + int ret; + + if ((ret = p_write(stream->fd, buffer, len)) < 0) + git_error_set(GIT_ERROR_OS, "could not write to '%s'", stream->path); + + return ret; +} + +static int checkout_stream_close(git_writestream *s) +{ + struct checkout_stream *stream = (struct checkout_stream *)s; + + GIT_ASSERT_ARG(stream); + GIT_ASSERT_ARG(stream->open); + + stream->open = 0; + return p_close(stream->fd); +} + +static void checkout_stream_free(git_writestream *s) +{ + GIT_UNUSED(s); +} + +static int blob_content_to_file( + checkout_data *data, + struct stat *st, + git_blob *blob, + const char *path, + const char *hint_path, + mode_t entry_filemode) +{ + int flags = data->opts.file_open_flags; + mode_t file_mode = data->opts.file_mode ? + data->opts.file_mode : entry_filemode; + git_filter_session filter_session = GIT_FILTER_SESSION_INIT; + struct checkout_stream writer; + mode_t mode; + git_filter_list *fl = NULL; + int fd; + int error = 0; + + GIT_ASSERT(hint_path != NULL); + + if ((error = mkpath2file(data, path, data->opts.dir_mode)) < 0) + return error; + + if (flags <= 0) + flags = O_CREAT | O_TRUNC | O_WRONLY; + if (!(mode = file_mode)) + mode = GIT_FILEMODE_BLOB; + + if ((fd = p_open(path, flags, mode)) < 0) { + git_error_set(GIT_ERROR_OS, "could not open '%s' for writing", path); + return fd; + } + + filter_session.attr_session = &data->attr_session; + filter_session.temp_buf = &data->tmp; + + if (!data->opts.disable_filters && + (error = git_filter_list__load( + &fl, data->repo, blob, hint_path, + GIT_FILTER_TO_WORKTREE, &filter_session))) { + p_close(fd); + return error; + } + + /* setup the writer */ + memset(&writer, 0, sizeof(struct checkout_stream)); + writer.base.write = checkout_stream_write; + writer.base.close = checkout_stream_close; + writer.base.free = checkout_stream_free; + writer.path = path; + writer.fd = fd; + writer.open = 1; + + error = git_filter_list_stream_blob(fl, blob, &writer.base); + + GIT_ASSERT(writer.open == 0); + + git_filter_list_free(fl); + + if (error < 0) + return error; + + if (st) { + data->perfdata.stat_calls++; + + if ((error = p_stat(path, st)) < 0) { + git_error_set(GIT_ERROR_OS, "failed to stat '%s'", path); + return error; + } + + st->st_mode = entry_filemode; + } + + return 0; +} + +static int blob_content_to_link( + checkout_data *data, + struct stat *st, + git_blob *blob, + const char *path) +{ + git_str linktarget = GIT_STR_INIT; + int error; + + if ((error = mkpath2file(data, path, data->opts.dir_mode)) < 0) + return error; + + if ((error = git_blob__getbuf(&linktarget, blob)) < 0) + return error; + + if (data->can_symlink) { + if ((error = p_symlink(git_str_cstr(&linktarget), path)) < 0) + git_error_set(GIT_ERROR_OS, "could not create symlink %s", path); + } else { + error = git_futils_fake_symlink(git_str_cstr(&linktarget), path); + } + + if (!error) { + data->perfdata.stat_calls++; + + if ((error = p_lstat(path, st)) < 0) + git_error_set(GIT_ERROR_CHECKOUT, "could not stat symlink %s", path); + + st->st_mode = GIT_FILEMODE_LINK; + } + + git_str_dispose(&linktarget); + + return error; +} + +static int checkout_update_index( + checkout_data *data, + const git_diff_file *file, + struct stat *st) +{ + git_index_entry entry; + + if (!data->index) + return 0; + + memset(&entry, 0, sizeof(entry)); + entry.path = (char *)file->path; /* cast to prevent warning */ + git_index_entry__init_from_stat(&entry, st, true); + git_oid_cpy(&entry.id, &file->id); + + return git_index_add(data->index, &entry); +} + +static int checkout_submodule_update_index( + checkout_data *data, + const git_diff_file *file) +{ + git_str *fullpath; + struct stat st; + + /* update the index unless prevented */ + if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) != 0) + return 0; + + if (checkout_target_fullpath(&fullpath, data, file->path) < 0) + return -1; + + data->perfdata.stat_calls++; + if (p_stat(fullpath->ptr, &st) < 0) { + git_error_set( + GIT_ERROR_CHECKOUT, "could not stat submodule %s\n", file->path); + return GIT_ENOTFOUND; + } + + st.st_mode = GIT_FILEMODE_COMMIT; + + return checkout_update_index(data, file, &st); +} + +static int checkout_submodule( + checkout_data *data, + const git_diff_file *file) +{ + bool remove_existing = should_remove_existing(data); + int error = 0; + + /* Until submodules are supported, UPDATE_ONLY means do nothing here */ + if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) + return 0; + + if ((error = checkout_mkdir( + data, + file->path, data->opts.target_directory, data->opts.dir_mode, + remove_existing ? MKDIR_REMOVE_EXISTING : MKDIR_NORMAL)) < 0) + return error; + + if ((error = git_submodule_lookup(NULL, data->repo, file->path)) < 0) { + /* I've observed repos with submodules in the tree that do not + * have a .gitmodules - core Git just makes an empty directory + */ + if (error == GIT_ENOTFOUND) { + git_error_clear(); + return checkout_submodule_update_index(data, file); + } + + return error; + } + + /* TODO: Support checkout_strategy options. Two circumstances: + * 1 - submodule already checked out, but we need to move the HEAD + * to the new OID, or + * 2 - submodule not checked out and we should recursively check it out + * + * Checkout will not execute a pull on the submodule, but a clone + * command should probably be able to. Do we need a submodule callback? + */ + + return checkout_submodule_update_index(data, file); +} + +static void report_progress( + checkout_data *data, + const char *path) +{ + if (data->opts.progress_cb) + data->opts.progress_cb( + path, data->completed_steps, data->total_steps, + data->opts.progress_payload); +} + +static int checkout_safe_for_update_only( + checkout_data *data, const char *path, mode_t expected_mode) +{ + struct stat st; + + data->perfdata.stat_calls++; + + if (p_lstat(path, &st) < 0) { + /* if doesn't exist, then no error and no update */ + if (errno == ENOENT || errno == ENOTDIR) + return 0; + + /* otherwise, stat error and no update */ + git_error_set(GIT_ERROR_OS, "failed to stat '%s'", path); + return -1; + } + + /* only safe for update if this is the same type of file */ + if ((st.st_mode & ~0777) == (expected_mode & ~0777)) + return 1; + + return 0; +} + +static int checkout_write_content( + checkout_data *data, + const git_oid *oid, + const char *full_path, + const char *hint_path, + unsigned int mode, + struct stat *st) +{ + int error = 0; + git_blob *blob; + + if ((error = git_blob_lookup(&blob, data->repo, oid)) < 0) + return error; + + if (S_ISLNK(mode)) + error = blob_content_to_link(data, st, blob, full_path); + else + error = blob_content_to_file(data, st, blob, full_path, hint_path, mode); + + git_blob_free(blob); + + /* if we try to create the blob and an existing directory blocks it from + * being written, then there must have been a typechange conflict in a + * parent directory - suppress the error and try to continue. + */ + if ((data->strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) != 0 && + (error == GIT_ENOTFOUND || error == GIT_EEXISTS)) + { + git_error_clear(); + error = 0; + } + + return error; +} + +static int checkout_blob( + checkout_data *data, + const git_diff_file *file) +{ + git_str *fullpath; + struct stat st; + int error = 0; + + if (checkout_target_fullpath(&fullpath, data, file->path) < 0) + return -1; + + if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) { + int rval = checkout_safe_for_update_only( + data, fullpath->ptr, file->mode); + + if (rval <= 0) + return rval; + } + + error = checkout_write_content( + data, &file->id, fullpath->ptr, file->path, file->mode, &st); + + /* update the index unless prevented */ + if (!error && (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0) + error = checkout_update_index(data, file, &st); + + /* update the submodule data if this was a new .gitmodules file */ + if (!error && strcmp(file->path, ".gitmodules") == 0) + data->reload_submodules = true; + + return error; +} + +static int checkout_remove_the_old( + unsigned int *actions, + checkout_data *data) +{ + int error = 0; + git_diff_delta *delta; + const char *str; + size_t i; + git_str *fullpath; + uint32_t flg = GIT_RMDIR_EMPTY_PARENTS | + GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS; + + if (data->opts.checkout_strategy & GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES) + flg |= GIT_RMDIR_SKIP_NONEMPTY; + + if (checkout_target_fullpath(&fullpath, data, NULL) < 0) + return -1; + + git_vector_foreach(&data->diff->deltas, i, delta) { + if (actions[i] & CHECKOUT_ACTION__REMOVE) { + error = git_futils_rmdir_r( + delta->old_file.path, fullpath->ptr, flg); + + if (error < 0) + return error; + + data->completed_steps++; + report_progress(data, delta->old_file.path); + + if ((actions[i] & CHECKOUT_ACTION__UPDATE_BLOB) == 0 && + (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0 && + data->index != NULL) + { + (void)git_index_remove(data->index, delta->old_file.path, 0); + } + } + } + + git_vector_foreach(&data->removes, i, str) { + error = git_futils_rmdir_r(str, fullpath->ptr, flg); + if (error < 0) + return error; + + data->completed_steps++; + report_progress(data, str); + + if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0 && + data->index != NULL) + { + if (str[strlen(str) - 1] == '/') + (void)git_index_remove_directory(data->index, str, 0); + else + (void)git_index_remove(data->index, str, 0); + } + } + + return 0; +} + +static int checkout_create_the_new( + unsigned int *actions, + checkout_data *data) +{ + int error = 0; + git_diff_delta *delta; + size_t i; + + git_vector_foreach(&data->diff->deltas, i, delta) { + if (actions[i] & CHECKOUT_ACTION__UPDATE_BLOB && !S_ISLNK(delta->new_file.mode)) { + if ((error = checkout_blob(data, &delta->new_file)) < 0) + return error; + data->completed_steps++; + report_progress(data, delta->new_file.path); + } + } + + git_vector_foreach(&data->diff->deltas, i, delta) { + if (actions[i] & CHECKOUT_ACTION__UPDATE_BLOB && S_ISLNK(delta->new_file.mode)) { + if ((error = checkout_blob(data, &delta->new_file)) < 0) + return error; + data->completed_steps++; + report_progress(data, delta->new_file.path); + } + } + + return 0; +} + +static int checkout_create_submodules( + unsigned int *actions, + checkout_data *data) +{ + git_diff_delta *delta; + size_t i; + + git_vector_foreach(&data->diff->deltas, i, delta) { + if (actions[i] & CHECKOUT_ACTION__UPDATE_SUBMODULE) { + int error = checkout_submodule(data, &delta->new_file); + if (error < 0) + return error; + + data->completed_steps++; + report_progress(data, delta->new_file.path); + } + } + + return 0; +} + +static int checkout_lookup_head_tree(git_tree **out, git_repository *repo) +{ + int error = 0; + git_reference *ref = NULL; + git_object *head; + + if (!(error = git_repository_head(&ref, repo)) && + !(error = git_reference_peel(&head, ref, GIT_OBJECT_TREE))) + *out = (git_tree *)head; + + git_reference_free(ref); + + return error; +} + + +static int conflict_entry_name( + git_str *out, + const char *side_name, + const char *filename) +{ + if (git_str_puts(out, side_name) < 0 || + git_str_putc(out, ':') < 0 || + git_str_puts(out, filename) < 0) + return -1; + + return 0; +} + +static int checkout_path_suffixed(git_str *path, const char *suffix) +{ + size_t path_len; + int i = 0, error = 0; + + if ((error = git_str_putc(path, '~')) < 0 || (error = git_str_puts(path, suffix)) < 0) + return -1; + + path_len = git_str_len(path); + + while (git_fs_path_exists(git_str_cstr(path)) && i < INT_MAX) { + git_str_truncate(path, path_len); + + if ((error = git_str_putc(path, '_')) < 0 || + (error = git_str_printf(path, "%d", i)) < 0) + return error; + + i++; + } + + if (i == INT_MAX) { + git_str_truncate(path, path_len); + + git_error_set(GIT_ERROR_CHECKOUT, "could not write '%s': working directory file exists", path->ptr); + return GIT_EEXISTS; + } + + return 0; +} + +static int checkout_write_entry( + checkout_data *data, + checkout_conflictdata *conflict, + const git_index_entry *side) +{ + const char *hint_path = NULL, *suffix; + git_str *fullpath; + struct stat st; + int error; + + GIT_ASSERT(side == conflict->ours || side == conflict->theirs); + + if (checkout_target_fullpath(&fullpath, data, side->path) < 0) + return -1; + + if ((conflict->name_collision || conflict->directoryfile) && + (data->strategy & GIT_CHECKOUT_USE_OURS) == 0 && + (data->strategy & GIT_CHECKOUT_USE_THEIRS) == 0) { + + if (side == conflict->ours) + suffix = data->opts.our_label ? data->opts.our_label : + "ours"; + else + suffix = data->opts.their_label ? data->opts.their_label : + "theirs"; + + if (checkout_path_suffixed(fullpath, suffix) < 0) + return -1; + } + + hint_path = side->path; + + if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0 && + (error = checkout_safe_for_update_only(data, fullpath->ptr, side->mode)) <= 0) + return error; + + if (!S_ISGITLINK(side->mode)) + return checkout_write_content(data, + &side->id, fullpath->ptr, hint_path, side->mode, &st); + + return 0; +} + +static int checkout_write_entries( + checkout_data *data, + checkout_conflictdata *conflict) +{ + int error = 0; + + if ((error = checkout_write_entry(data, conflict, conflict->ours)) >= 0) + error = checkout_write_entry(data, conflict, conflict->theirs); + + return error; +} + +static int checkout_merge_path( + git_str *out, + checkout_data *data, + checkout_conflictdata *conflict, + git_merge_file_result *result) +{ + const char *our_label_raw, *their_label_raw, *suffix; + int error = 0; + + if ((error = git_str_joinpath(out, data->opts.target_directory, result->path)) < 0 || + (error = git_path_validate_str_length(data->repo, out)) < 0) + return error; + + /* Most conflicts simply use the filename in the index */ + if (!conflict->name_collision) + return 0; + + /* Rename 2->1 conflicts need the branch name appended */ + our_label_raw = data->opts.our_label ? data->opts.our_label : "ours"; + their_label_raw = data->opts.their_label ? data->opts.their_label : "theirs"; + suffix = strcmp(result->path, conflict->ours->path) == 0 ? our_label_raw : their_label_raw; + + if ((error = checkout_path_suffixed(out, suffix)) < 0) + return error; + + return 0; +} + +static int checkout_write_merge( + checkout_data *data, + checkout_conflictdata *conflict) +{ + git_str our_label = GIT_STR_INIT, their_label = GIT_STR_INIT, + path_suffixed = GIT_STR_INIT, path_workdir = GIT_STR_INIT, + in_data = GIT_STR_INIT, out_data = GIT_STR_INIT; + git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT; + git_merge_file_result result = {0}; + git_filebuf output = GIT_FILEBUF_INIT; + git_filter_list *fl = NULL; + git_filter_session filter_session = GIT_FILTER_SESSION_INIT; + int error = 0; + + if (data->opts.checkout_strategy & GIT_CHECKOUT_CONFLICT_STYLE_DIFF3) + opts.flags |= GIT_MERGE_FILE_STYLE_DIFF3; + + if (data->opts.checkout_strategy & GIT_CHECKOUT_CONFLICT_STYLE_ZDIFF3) + opts.flags |= GIT_MERGE_FILE_STYLE_ZDIFF3; + + opts.ancestor_label = data->opts.ancestor_label ? + data->opts.ancestor_label : "ancestor"; + opts.our_label = data->opts.our_label ? + data->opts.our_label : "ours"; + opts.their_label = data->opts.their_label ? + data->opts.their_label : "theirs"; + + /* If all the paths are identical, decorate the diff3 file with the branch + * names. Otherwise, append branch_name:path. + */ + if (conflict->ours && conflict->theirs && + strcmp(conflict->ours->path, conflict->theirs->path) != 0) { + + if ((error = conflict_entry_name( + &our_label, opts.our_label, conflict->ours->path)) < 0 || + (error = conflict_entry_name( + &their_label, opts.their_label, conflict->theirs->path)) < 0) + goto done; + + opts.our_label = git_str_cstr(&our_label); + opts.their_label = git_str_cstr(&their_label); + } + + if ((error = git_merge_file_from_index(&result, data->repo, + conflict->ancestor, conflict->ours, conflict->theirs, &opts)) < 0) + goto done; + + if (result.path == NULL || result.mode == 0) { + git_error_set(GIT_ERROR_CHECKOUT, "could not merge contents of file"); + error = GIT_ECONFLICT; + goto done; + } + + if ((error = checkout_merge_path(&path_workdir, data, conflict, &result)) < 0) + goto done; + + if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0 && + (error = checkout_safe_for_update_only(data, git_str_cstr(&path_workdir), result.mode)) <= 0) + goto done; + + if (!data->opts.disable_filters) { + in_data.ptr = (char *)result.ptr; + in_data.size = result.len; + + filter_session.attr_session = &data->attr_session; + filter_session.temp_buf = &data->tmp; + + if ((error = git_filter_list__load( + &fl, data->repo, NULL, result.path, + GIT_FILTER_TO_WORKTREE, &filter_session)) < 0 || + (error = git_filter_list__convert_buf(&out_data, fl, &in_data)) < 0) + goto done; + } else { + out_data.ptr = (char *)result.ptr; + out_data.size = result.len; + } + + if ((error = mkpath2file(data, path_workdir.ptr, data->opts.dir_mode)) < 0 || + (error = git_filebuf_open(&output, git_str_cstr(&path_workdir), GIT_FILEBUF_DO_NOT_BUFFER, result.mode)) < 0 || + (error = git_filebuf_write(&output, out_data.ptr, out_data.size)) < 0 || + (error = git_filebuf_commit(&output)) < 0) + goto done; + +done: + git_filter_list_free(fl); + + git_str_dispose(&out_data); + git_str_dispose(&our_label); + git_str_dispose(&their_label); + + git_merge_file_result_free(&result); + git_str_dispose(&path_workdir); + git_str_dispose(&path_suffixed); + + return error; +} + +static int checkout_conflict_add( + checkout_data *data, + const git_index_entry *conflict) +{ + int error = git_index_remove(data->index, conflict->path, 0); + + if (error == GIT_ENOTFOUND) + git_error_clear(); + else if (error < 0) + return error; + + return git_index_add(data->index, conflict); +} + +static int checkout_conflict_update_index( + checkout_data *data, + checkout_conflictdata *conflict) +{ + int error = 0; + + if (conflict->ancestor) + error = checkout_conflict_add(data, conflict->ancestor); + + if (!error && conflict->ours) + error = checkout_conflict_add(data, conflict->ours); + + if (!error && conflict->theirs) + error = checkout_conflict_add(data, conflict->theirs); + + return error; +} + +static int checkout_create_conflicts(checkout_data *data) +{ + checkout_conflictdata *conflict; + size_t i; + int error = 0; + + git_vector_foreach(&data->update_conflicts, i, conflict) { + + /* Both deleted: nothing to do */ + if (conflict->ours == NULL && conflict->theirs == NULL) + error = 0; + + else if ((data->strategy & GIT_CHECKOUT_USE_OURS) && + conflict->ours) + error = checkout_write_entry(data, conflict, conflict->ours); + else if ((data->strategy & GIT_CHECKOUT_USE_THEIRS) && + conflict->theirs) + error = checkout_write_entry(data, conflict, conflict->theirs); + + /* Ignore the other side of name collisions. */ + else if ((data->strategy & GIT_CHECKOUT_USE_OURS) && + !conflict->ours && conflict->name_collision) + error = 0; + else if ((data->strategy & GIT_CHECKOUT_USE_THEIRS) && + !conflict->theirs && conflict->name_collision) + error = 0; + + /* For modify/delete, name collisions and d/f conflicts, write + * the file (potentially with the name mangled. + */ + else if (conflict->ours != NULL && conflict->theirs == NULL) + error = checkout_write_entry(data, conflict, conflict->ours); + else if (conflict->ours == NULL && conflict->theirs != NULL) + error = checkout_write_entry(data, conflict, conflict->theirs); + + /* Add/add conflicts and rename 1->2 conflicts, write the + * ours/theirs sides (potentially name mangled). + */ + else if (conflict->one_to_two) + error = checkout_write_entries(data, conflict); + + /* If all sides are links, write the ours side */ + else if (S_ISLNK(conflict->ours->mode) && + S_ISLNK(conflict->theirs->mode)) + error = checkout_write_entry(data, conflict, conflict->ours); + /* Link/file conflicts, write the file side */ + else if (S_ISLNK(conflict->ours->mode)) + error = checkout_write_entry(data, conflict, conflict->theirs); + else if (S_ISLNK(conflict->theirs->mode)) + error = checkout_write_entry(data, conflict, conflict->ours); + + /* If any side is a gitlink, do nothing. */ + else if (conflict->submodule) + error = 0; + + /* If any side is binary, write the ours side */ + else if (conflict->binary) + error = checkout_write_entry(data, conflict, conflict->ours); + + else if (!error) + error = checkout_write_merge(data, conflict); + + /* Update the index extensions (REUC and NAME) if we're checking + * out a different index. (Otherwise just leave them there.) + */ + if (!error && (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0) + error = checkout_conflict_update_index(data, conflict); + + if (error) + break; + + data->completed_steps++; + report_progress(data, + conflict->ours ? conflict->ours->path : + (conflict->theirs ? conflict->theirs->path : conflict->ancestor->path)); + } + + return error; +} + +static int checkout_remove_conflicts(checkout_data *data) +{ + const char *conflict; + size_t i; + + git_vector_foreach(&data->remove_conflicts, i, conflict) { + if (git_index_conflict_remove(data->index, conflict) < 0) + return -1; + + data->completed_steps++; + } + + return 0; +} + +static int checkout_extensions_update_index(checkout_data *data) +{ + const git_index_reuc_entry *reuc_entry; + const git_index_name_entry *name_entry; + size_t i; + int error = 0; + + if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) + return 0; + + if (data->update_reuc) { + git_vector_foreach(data->update_reuc, i, reuc_entry) { + if ((error = git_index_reuc_add(data->index, reuc_entry->path, + reuc_entry->mode[0], &reuc_entry->oid[0], + reuc_entry->mode[1], &reuc_entry->oid[1], + reuc_entry->mode[2], &reuc_entry->oid[2])) < 0) + goto done; + } + } + + if (data->update_names) { + git_vector_foreach(data->update_names, i, name_entry) { + if ((error = git_index_name_add(data->index, name_entry->ancestor, + name_entry->ours, name_entry->theirs)) < 0) + goto done; + } + } + +done: + return error; +} + +static void checkout_data_clear(checkout_data *data) +{ + if (data->opts_free_baseline) { + git_tree_free(data->opts.baseline); + data->opts.baseline = NULL; + } + + git_vector_dispose(&data->removes); + git_pool_clear(&data->pool); + + git_vector_dispose_deep(&data->remove_conflicts); + git_vector_dispose_deep(&data->update_conflicts); + + git__free(data->pfx); + data->pfx = NULL; + + git_str_dispose(&data->target_path); + git_str_dispose(&data->tmp); + + git_index_free(data->index); + data->index = NULL; + + git_hashset_str_dispose(&data->mkdir_pathcache); + + git_attr_session__free(&data->attr_session); +} + +static int validate_target_directory(checkout_data *data) +{ + int error; + + if ((error = git_path_validate_length(data->repo, data->opts.target_directory)) < 0) + return error; + + if (git_fs_path_isdir(data->opts.target_directory)) + return 0; + + error = checkout_mkdir(data, data->opts.target_directory, NULL, + GIT_DIR_MODE, GIT_MKDIR_VERIFY_DIR); + + return error; +} + +static int checkout_data_init( + checkout_data *data, + git_iterator *target, + const git_checkout_options *proposed) +{ + int error = 0; + git_repository *repo = git_iterator_owner(target); + + memset(data, 0, sizeof(*data)); + + if (!repo) { + git_error_set(GIT_ERROR_CHECKOUT, "cannot checkout nothing"); + return -1; + } + + if ((!proposed || !proposed->target_directory) && + (error = git_repository__ensure_not_bare(repo, "checkout")) < 0) + return error; + + data->repo = repo; + data->target = target; + + GIT_ERROR_CHECK_VERSION( + proposed, GIT_CHECKOUT_OPTIONS_VERSION, "git_checkout_options"); + + if (!proposed) + GIT_INIT_STRUCTURE(&data->opts, GIT_CHECKOUT_OPTIONS_VERSION); + else + memmove(&data->opts, proposed, sizeof(git_checkout_options)); + + if (!data->opts.target_directory) + data->opts.target_directory = git_repository_workdir(repo); + else if ((error = validate_target_directory(data)) < 0) + goto cleanup; + + if ((error = git_repository_index(&data->index, data->repo)) < 0) + goto cleanup; + + /* refresh config and index content unless NO_REFRESH is given */ + if ((data->opts.checkout_strategy & GIT_CHECKOUT_NO_REFRESH) == 0) { + git_config *cfg; + + if ((error = git_repository_config__weakptr(&cfg, repo)) < 0) + goto cleanup; + + /* Reload the repository index (unless we're checking out the + * index; then it has the changes we're trying to check out + * and those should not be overwritten.) + */ + if (data->index != git_iterator_index(target)) { + if (data->opts.checkout_strategy & GIT_CHECKOUT_FORCE) { + /* When forcing, we can blindly re-read the index */ + if ((error = git_index_read(data->index, false)) < 0) + goto cleanup; + } else { + /* + * When not being forced, we need to check for unresolved + * conflicts and unsaved changes in the index before + * proceeding. + */ + if (git_index_has_conflicts(data->index)) { + error = GIT_ECONFLICT; + git_error_set(GIT_ERROR_CHECKOUT, + "unresolved conflicts exist in the index"); + goto cleanup; + } + + if ((error = git_index_read_safely(data->index)) < 0) + goto cleanup; + } + + /* clean conflict data in the current index */ + git_index_name_clear(data->index); + git_index_reuc_clear(data->index); + } + } + + /* if you are forcing, allow all safe updates, plus recreate missing */ + if ((data->opts.checkout_strategy & GIT_CHECKOUT_FORCE) != 0) + data->opts.checkout_strategy |= GIT_CHECKOUT_RECREATE_MISSING; + + /* if the repository does not actually have an index file, then this + * is an initial checkout (perhaps from clone), so we allow safe updates + */ + if (!data->index->on_disk) + data->opts.checkout_strategy |= GIT_CHECKOUT_RECREATE_MISSING; + + data->strategy = data->opts.checkout_strategy; + + /* opts->disable_filters is false by default */ + + if (!data->opts.dir_mode) + data->opts.dir_mode = GIT_DIR_MODE; + + if (!data->opts.file_open_flags) + data->opts.file_open_flags = O_CREAT | O_TRUNC | O_WRONLY; + + data->pfx = git_pathspec_prefix(&data->opts.paths); + + if ((error = git_repository__configmap_lookup( + &data->can_symlink, repo, GIT_CONFIGMAP_SYMLINKS)) < 0) + goto cleanup; + + if ((error = git_repository__configmap_lookup( + &data->respect_filemode, repo, GIT_CONFIGMAP_FILEMODE)) < 0) + goto cleanup; + + if (!data->opts.baseline && !data->opts.baseline_index) { + data->opts_free_baseline = true; + error = 0; + + /* if we don't have an index, this is an initial checkout and + * should be against an empty baseline + */ + if (data->index->on_disk) + error = checkout_lookup_head_tree(&data->opts.baseline, repo); + + if (error == GIT_EUNBORNBRANCH) { + error = 0; + git_error_clear(); + } + + if (error < 0) + goto cleanup; + } + + if ((data->opts.checkout_strategy & + (GIT_CHECKOUT_CONFLICT_STYLE_MERGE | GIT_CHECKOUT_CONFLICT_STYLE_DIFF3)) == 0) { + git_config_entry *conflict_style = NULL; + git_config *cfg = NULL; + + if ((error = git_repository_config__weakptr(&cfg, repo)) < 0 || + (error = git_config_get_entry(&conflict_style, cfg, "merge.conflictstyle")) < 0 || + error == GIT_ENOTFOUND) + ; + else if (error) + goto cleanup; + else if (strcmp(conflict_style->value, "merge") == 0) + data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_MERGE; + else if (strcmp(conflict_style->value, "diff3") == 0) + data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_DIFF3; + else if (strcmp(conflict_style->value, "zdiff3") == 0) + data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_ZDIFF3; + else { + git_error_set(GIT_ERROR_CHECKOUT, "unknown style '%s' given for 'merge.conflictstyle'", + conflict_style->value); + error = -1; + git_config_entry_free(conflict_style); + goto cleanup; + } + git_config_entry_free(conflict_style); + } + + if ((error = git_pool_init(&data->pool, 1)) < 0 || + (error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 || + (error = git_vector_init(&data->remove_conflicts, 0, NULL)) < 0 || + (error = git_vector_init(&data->update_conflicts, 0, NULL)) < 0 || + (error = git_str_puts(&data->target_path, data->opts.target_directory)) < 0 || + (error = git_fs_path_to_dir(&data->target_path)) < 0) + goto cleanup; + + data->target_len = git_str_len(&data->target_path); + + git_attr_session__init(&data->attr_session, data->repo); + +cleanup: + if (error < 0) + checkout_data_clear(data); + + return error; +} + +#define CHECKOUT_INDEX_DONT_WRITE_MASK \ + (GIT_CHECKOUT_DONT_UPDATE_INDEX | GIT_CHECKOUT_DONT_WRITE_INDEX) + +GIT_INLINE(void) setup_pathspecs( + git_iterator_options *iter_opts, + const git_checkout_options *checkout_opts) +{ + if (checkout_opts && + (checkout_opts->checkout_strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH)) { + iter_opts->pathlist.count = checkout_opts->paths.count; + iter_opts->pathlist.strings = checkout_opts->paths.strings; + } +} + +int git_checkout_iterator( + git_iterator *target, + git_index *index, + const git_checkout_options *opts) +{ + int error = 0; + git_iterator *baseline = NULL, *workdir = NULL; + git_iterator_options baseline_opts = GIT_ITERATOR_OPTIONS_INIT, + workdir_opts = GIT_ITERATOR_OPTIONS_INIT; + checkout_data data = {0}; + git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; + uint32_t *actions = NULL; + size_t *counts = NULL; + + /* initialize structures and options */ + error = checkout_data_init(&data, target, opts); + if (error < 0) + return error; + + diff_opts.flags = + GIT_DIFF_INCLUDE_UNMODIFIED | + GIT_DIFF_INCLUDE_UNREADABLE | + GIT_DIFF_INCLUDE_UNTRACKED | + GIT_DIFF_RECURSE_UNTRACKED_DIRS | /* needed to match baseline */ + GIT_DIFF_INCLUDE_IGNORED | + GIT_DIFF_INCLUDE_TYPECHANGE | + GIT_DIFF_INCLUDE_TYPECHANGE_TREES | + GIT_DIFF_SKIP_BINARY_CHECK | + GIT_DIFF_INCLUDE_CASECHANGE; + if (data.opts.checkout_strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) + diff_opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH; + if (data.opts.paths.count > 0) + diff_opts.pathspec = data.opts.paths; + + /* set up iterators */ + + workdir_opts.flags = git_iterator_ignore_case(target) ? + GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE; + workdir_opts.flags |= GIT_ITERATOR_DONT_AUTOEXPAND; + workdir_opts.start = data.pfx; + workdir_opts.end = data.pfx; + + setup_pathspecs(&workdir_opts, opts); + + if ((error = git_iterator_reset_range(target, data.pfx, data.pfx)) < 0 || + (error = git_iterator_for_workdir_ext( + &workdir, data.repo, data.opts.target_directory, index, NULL, + &workdir_opts)) < 0) + goto cleanup; + + baseline_opts.flags = git_iterator_ignore_case(target) ? + GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE; + baseline_opts.start = data.pfx; + baseline_opts.end = data.pfx; + + setup_pathspecs(&baseline_opts, opts); + + if (data.opts.baseline_index) { + if ((error = git_iterator_for_index( + &baseline, git_index_owner(data.opts.baseline_index), + data.opts.baseline_index, &baseline_opts)) < 0) + goto cleanup; + } else { + if ((error = git_iterator_for_tree( + &baseline, data.opts.baseline, &baseline_opts)) < 0) + goto cleanup; + } + + /* Should not have case insensitivity mismatch */ + GIT_ASSERT(git_iterator_ignore_case(workdir) == git_iterator_ignore_case(baseline)); + + /* Generate baseline-to-target diff which will include an entry for + * every possible update that might need to be made. + */ + if ((error = git_diff__from_iterators( + &data.diff, data.repo, baseline, target, &diff_opts)) < 0) + goto cleanup; + + /* Loop through diff (and working directory iterator) building a list of + * actions to be taken, plus look for conflicts and send notifications, + * then loop through conflicts. + */ + if ((error = checkout_get_actions(&actions, &counts, &data, workdir)) != 0) + goto cleanup; + + if (data.strategy & GIT_CHECKOUT_DRY_RUN) + goto cleanup; + + data.total_steps = counts[CHECKOUT_ACTION__REMOVE] + + counts[CHECKOUT_ACTION__REMOVE_CONFLICT] + + counts[CHECKOUT_ACTION__UPDATE_BLOB] + + counts[CHECKOUT_ACTION__UPDATE_SUBMODULE] + + counts[CHECKOUT_ACTION__UPDATE_CONFLICT]; + + report_progress(&data, NULL); /* establish 0 baseline */ + + /* To deal with some order dependencies, perform remaining checkout + * in three passes: removes, then update blobs, then update submodules. + */ + if (counts[CHECKOUT_ACTION__REMOVE] > 0 && + (error = checkout_remove_the_old(actions, &data)) < 0) + goto cleanup; + + if (counts[CHECKOUT_ACTION__REMOVE_CONFLICT] > 0 && + (error = checkout_remove_conflicts(&data)) < 0) + goto cleanup; + + if (counts[CHECKOUT_ACTION__UPDATE_BLOB] > 0 && + (error = checkout_create_the_new(actions, &data)) < 0) + goto cleanup; + + if (counts[CHECKOUT_ACTION__UPDATE_SUBMODULE] > 0 && + (error = checkout_create_submodules(actions, &data)) < 0) + goto cleanup; + + if (counts[CHECKOUT_ACTION__UPDATE_CONFLICT] > 0 && + (error = checkout_create_conflicts(&data)) < 0) + goto cleanup; + + if (data.index != git_iterator_index(target) && + (error = checkout_extensions_update_index(&data)) < 0) + goto cleanup; + + GIT_ASSERT(data.completed_steps == data.total_steps); + + if (data.opts.perfdata_cb) + data.opts.perfdata_cb(&data.perfdata, data.opts.perfdata_payload); + +cleanup: + if (!error && data.index != NULL && + (data.strategy & CHECKOUT_INDEX_DONT_WRITE_MASK) == 0) + error = git_index_write(data.index); + + git_diff_free(data.diff); + git_iterator_free(workdir); + git_iterator_free(baseline); + git__free(actions); + git__free(counts); + checkout_data_clear(&data); + + return error; +} + +int git_checkout_index( + git_repository *repo, + git_index *index, + const git_checkout_options *opts) +{ + git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; + int error, owned = 0; + git_iterator *index_i; + + if (!index && !repo) { + git_error_set(GIT_ERROR_CHECKOUT, + "must provide either repository or index to checkout"); + return -1; + } + + if (index && repo && + git_index_owner(index) && + git_index_owner(index) != repo) { + git_error_set(GIT_ERROR_CHECKOUT, + "index to checkout does not match repository"); + return -1; + } else if(index && repo && !git_index_owner(index)) { + GIT_REFCOUNT_OWN(index, repo); + owned = 1; + } + + if (!repo) + repo = git_index_owner(index); + + if (!index && (error = git_repository_index__weakptr(&index, repo)) < 0) + return error; + GIT_REFCOUNT_INC(index); + + setup_pathspecs(&iter_opts, opts); + + if (!(error = git_iterator_for_index(&index_i, repo, index, &iter_opts))) + error = git_checkout_iterator(index_i, index, opts); + + if (owned) + GIT_REFCOUNT_OWN(index, NULL); + + git_iterator_free(index_i); + git_index_free(index); + + return error; +} + +int git_checkout_tree( + git_repository *repo, + const git_object *treeish, + const git_checkout_options *opts) +{ + int error; + git_index *index; + git_tree *tree = NULL; + git_iterator *tree_i = NULL; + git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; + + if (!treeish && !repo) { + git_error_set(GIT_ERROR_CHECKOUT, + "must provide either repository or tree to checkout"); + return -1; + } + if (treeish && repo && git_object_owner(treeish) != repo) { + git_error_set(GIT_ERROR_CHECKOUT, + "object to checkout does not match repository"); + return -1; + } + + if (!repo) + repo = git_object_owner(treeish); + + if (treeish) { + if (git_object_peel((git_object **)&tree, treeish, GIT_OBJECT_TREE) < 0) { + git_error_set( + GIT_ERROR_CHECKOUT, "provided object cannot be peeled to a tree"); + return -1; + } + } + else { + if ((error = checkout_lookup_head_tree(&tree, repo)) < 0) { + if (error != GIT_EUNBORNBRANCH) + git_error_set( + GIT_ERROR_CHECKOUT, + "HEAD could not be peeled to a tree and no treeish given"); + return error; + } + } + + if ((error = git_repository_index(&index, repo)) < 0) + return error; + + setup_pathspecs(&iter_opts, opts); + + if (!(error = git_iterator_for_tree(&tree_i, tree, &iter_opts))) + error = git_checkout_iterator(tree_i, index, opts); + + git_iterator_free(tree_i); + git_index_free(index); + git_tree_free(tree); + + return error; +} + +int git_checkout_head( + git_repository *repo, + const git_checkout_options *opts) +{ + GIT_ASSERT_ARG(repo); + + return git_checkout_tree(repo, NULL, opts); +} + +int git_checkout_options_init(git_checkout_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_checkout_options, GIT_CHECKOUT_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_checkout_init_options(git_checkout_options *opts, unsigned int version) +{ + return git_checkout_options_init(opts, version); +} +#endif diff --git a/src/checkout.h b/src/libgit2/checkout.h similarity index 87% rename from src/checkout.h rename to src/libgit2/checkout.h index b1dc80c3866..e613325c7e8 100644 --- a/src/checkout.h +++ b/src/libgit2/checkout.h @@ -7,11 +7,11 @@ #ifndef INCLUDE_checkout_h__ #define INCLUDE_checkout_h__ +#include "common.h" + #include "git2/checkout.h" #include "iterator.h" -#define GIT_CHECKOUT__NOTIFY_CONFLICT_TREE (1u << 12) - /** * Update the working directory to match the target iterator. The * expected baseline value can be passed in via the checkout options @@ -19,6 +19,7 @@ */ extern int git_checkout_iterator( git_iterator *target, - git_checkout_opts *opts); + git_index *index, + const git_checkout_options *opts); #endif diff --git a/src/libgit2/cherrypick.c b/src/libgit2/cherrypick.c new file mode 100644 index 00000000000..561370169fc --- /dev/null +++ b/src/libgit2/cherrypick.c @@ -0,0 +1,241 @@ +/* +* Copyright (C) the libgit2 contributors. All rights reserved. +* +* This file is part of libgit2, distributed under the GNU GPL v2 with +* a Linking Exception. For full terms see the included COPYING file. +*/ + +#include "common.h" + +#include "repository.h" +#include "filebuf.h" +#include "merge.h" +#include "vector.h" +#include "index.h" + +#include "git2/types.h" +#include "git2/merge.h" +#include "git2/cherrypick.h" +#include "git2/commit.h" +#include "git2/sys/commit.h" + +#define GIT_CHERRYPICK_FILE_MODE 0666 + +static int write_cherrypick_head( + git_repository *repo, + const char *commit_oidstr) +{ + git_filebuf file = GIT_FILEBUF_INIT; + git_str file_path = GIT_STR_INIT; + int error = 0; + + if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_CHERRYPICK_HEAD_FILE)) >= 0 && + (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_CHERRYPICK_FILE_MODE)) >= 0 && + (error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0) + error = git_filebuf_commit(&file); + + if (error < 0) + git_filebuf_cleanup(&file); + + git_str_dispose(&file_path); + + return error; +} + +static int write_merge_msg( + git_repository *repo, + const char *commit_msg) +{ + git_filebuf file = GIT_FILEBUF_INIT; + git_str file_path = GIT_STR_INIT; + int error = 0; + + if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 || + (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_CHERRYPICK_FILE_MODE)) < 0 || + (error = git_filebuf_printf(&file, "%s", commit_msg)) < 0) + goto cleanup; + + error = git_filebuf_commit(&file); + +cleanup: + if (error < 0) + git_filebuf_cleanup(&file); + + git_str_dispose(&file_path); + + return error; +} + +static int cherrypick_normalize_opts( + git_repository *repo, + git_cherrypick_options *opts, + const git_cherrypick_options *given, + const char *their_label) +{ + int error = 0; + unsigned int default_checkout_strategy = GIT_CHECKOUT_ALLOW_CONFLICTS; + + GIT_UNUSED(repo); + + if (given != NULL) + memcpy(opts, given, sizeof(git_cherrypick_options)); + else { + git_cherrypick_options default_opts = GIT_CHERRYPICK_OPTIONS_INIT; + memcpy(opts, &default_opts, sizeof(git_cherrypick_options)); + } + + if (!opts->checkout_opts.checkout_strategy) + opts->checkout_opts.checkout_strategy = default_checkout_strategy; + + if (!opts->checkout_opts.our_label) + opts->checkout_opts.our_label = "HEAD"; + + if (!opts->checkout_opts.their_label) + opts->checkout_opts.their_label = their_label; + + return error; +} + +static int cherrypick_state_cleanup(git_repository *repo) +{ + const char *state_files[] = { GIT_CHERRYPICK_HEAD_FILE, GIT_MERGE_MSG_FILE }; + + return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files)); +} + +static int cherrypick_seterr(git_commit *commit, const char *fmt) +{ + char commit_oidstr[GIT_OID_MAX_HEXSIZE + 1]; + + git_error_set(GIT_ERROR_CHERRYPICK, fmt, + git_oid_tostr(commit_oidstr, GIT_OID_MAX_HEXSIZE + 1, git_commit_id(commit))); + + return -1; +} + +int git_cherrypick_commit( + git_index **out, + git_repository *repo, + git_commit *cherrypick_commit, + git_commit *our_commit, + unsigned int mainline, + const git_merge_options *merge_opts) +{ + git_commit *parent_commit = NULL; + git_tree *parent_tree = NULL, *our_tree = NULL, *cherrypick_tree = NULL; + int parent = 0, error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(cherrypick_commit); + GIT_ASSERT_ARG(our_commit); + + if (git_commit_parentcount(cherrypick_commit) > 1) { + if (!mainline) + return cherrypick_seterr(cherrypick_commit, + "mainline branch is not specified but %s is a merge commit"); + + parent = mainline; + } else { + if (mainline) + return cherrypick_seterr(cherrypick_commit, + "mainline branch specified but %s is not a merge commit"); + + parent = git_commit_parentcount(cherrypick_commit); + } + + if (parent && + ((error = git_commit_parent(&parent_commit, cherrypick_commit, (parent - 1))) < 0 || + (error = git_commit_tree(&parent_tree, parent_commit)) < 0)) + goto done; + + if ((error = git_commit_tree(&cherrypick_tree, cherrypick_commit)) < 0 || + (error = git_commit_tree(&our_tree, our_commit)) < 0) + goto done; + + error = git_merge_trees(out, repo, parent_tree, our_tree, cherrypick_tree, merge_opts); + +done: + git_tree_free(parent_tree); + git_tree_free(our_tree); + git_tree_free(cherrypick_tree); + git_commit_free(parent_commit); + + return error; +} + +int git_cherrypick( + git_repository *repo, + git_commit *commit, + const git_cherrypick_options *given_opts) +{ + git_cherrypick_options opts; + git_reference *our_ref = NULL; + git_commit *our_commit = NULL; + char commit_oidstr[GIT_OID_MAX_HEXSIZE + 1]; + const char *commit_msg, *commit_summary; + git_str their_label = GIT_STR_INIT; + git_index *index = NULL; + git_indexwriter indexwriter = GIT_INDEXWRITER_INIT; + int error = 0; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(commit); + + GIT_ERROR_CHECK_VERSION(given_opts, GIT_CHERRYPICK_OPTIONS_VERSION, "git_cherrypick_options"); + + if ((error = git_repository__ensure_not_bare(repo, "cherry-pick")) < 0) + return error; + + if ((commit_msg = git_commit_message(commit)) == NULL || + (commit_summary = git_commit_summary(commit)) == NULL) { + error = -1; + goto on_error; + } + + git_oid_nfmt(commit_oidstr, sizeof(commit_oidstr), git_commit_id(commit)); + + if ((error = write_merge_msg(repo, commit_msg)) < 0 || + (error = git_str_printf(&their_label, "%.7s... %s", commit_oidstr, commit_summary)) < 0 || + (error = cherrypick_normalize_opts(repo, &opts, given_opts, git_str_cstr(&their_label))) < 0 || + (error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 || + (error = write_cherrypick_head(repo, commit_oidstr)) < 0 || + (error = git_repository_head(&our_ref, repo)) < 0 || + (error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJECT_COMMIT)) < 0 || + (error = git_cherrypick_commit(&index, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 || + (error = git_merge__check_result(repo, index)) < 0 || + (error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0 || + (error = git_checkout_index(repo, index, &opts.checkout_opts)) < 0 || + (error = git_indexwriter_commit(&indexwriter)) < 0) + goto on_error; + + goto done; + +on_error: + cherrypick_state_cleanup(repo); + +done: + git_indexwriter_cleanup(&indexwriter); + git_index_free(index); + git_commit_free(our_commit); + git_reference_free(our_ref); + git_str_dispose(&their_label); + + return error; +} + +int git_cherrypick_options_init( + git_cherrypick_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_cherrypick_options, GIT_CHERRYPICK_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_cherrypick_init_options( + git_cherrypick_options *opts, unsigned int version) +{ + return git_cherrypick_options_init(opts, version); +} +#endif diff --git a/src/libgit2/clone.c b/src/libgit2/clone.c new file mode 100644 index 00000000000..237efc0ba7d --- /dev/null +++ b/src/libgit2/clone.c @@ -0,0 +1,706 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "clone.h" + +#include "git2/clone.h" +#include "git2/remote.h" +#include "git2/revparse.h" +#include "git2/branch.h" +#include "git2/config.h" +#include "git2/checkout.h" +#include "git2/commit.h" +#include "git2/tree.h" + +#include "checkout.h" +#include "remote.h" +#include "futils.h" +#include "refs.h" +#include "fs_path.h" +#include "repository.h" +#include "odb.h" +#include "net.h" + +static int create_branch( + git_reference **branch, + git_repository *repo, + const git_oid *target, + const char *name, + const char *log_message) +{ + git_commit *head_obj = NULL; + git_reference *branch_ref = NULL; + git_str refname = GIT_STR_INIT; + int error; + + /* Find the target commit */ + if ((error = git_commit_lookup(&head_obj, repo, target)) < 0) + return error; + + /* Create the new branch */ + if ((error = git_str_printf(&refname, GIT_REFS_HEADS_DIR "%s", name)) < 0) + return error; + + error = git_reference_create(&branch_ref, repo, git_str_cstr(&refname), target, 0, log_message); + git_str_dispose(&refname); + git_commit_free(head_obj); + + if (!error) + *branch = branch_ref; + else + git_reference_free(branch_ref); + + return error; +} + +static int setup_tracking_config( + git_repository *repo, + const char *branch_name, + const char *remote_name, + const char *merge_target) +{ + git_config *cfg; + git_str remote_key = GIT_STR_INIT, merge_key = GIT_STR_INIT; + int error = -1; + + if (git_repository_config__weakptr(&cfg, repo) < 0) + return -1; + + if (git_str_printf(&remote_key, "branch.%s.remote", branch_name) < 0) + goto cleanup; + + if (git_str_printf(&merge_key, "branch.%s.merge", branch_name) < 0) + goto cleanup; + + if (git_config_set_string(cfg, git_str_cstr(&remote_key), remote_name) < 0) + goto cleanup; + + if (git_config_set_string(cfg, git_str_cstr(&merge_key), merge_target) < 0) + goto cleanup; + + error = 0; + +cleanup: + git_str_dispose(&remote_key); + git_str_dispose(&merge_key); + return error; +} + +static int create_tracking_branch( + git_reference **branch, + git_repository *repo, + const git_oid *target, + const char *branch_name, + const char *log_message) +{ + int error; + + if ((error = create_branch(branch, repo, target, branch_name, log_message)) < 0) + return error; + + return setup_tracking_config( + repo, + branch_name, + GIT_REMOTE_ORIGIN, + git_reference_name(*branch)); +} + +static int update_head_to_new_branch( + git_repository *repo, + const git_oid *target, + const char *name, + const char *reflog_message) +{ + git_reference *tracking_branch = NULL; + int error; + + if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR)) + name += strlen(GIT_REFS_HEADS_DIR); + + error = create_tracking_branch(&tracking_branch, repo, target, name, + reflog_message); + + if (!error) + error = git_repository_set_head( + repo, git_reference_name(tracking_branch)); + + git_reference_free(tracking_branch); + + /* if it already existed, then the user's refspec created it for us, ignore it' */ + if (error == GIT_EEXISTS) + error = 0; + + return error; +} + +static int update_head_to_default(git_repository *repo) +{ + git_str initialbranch = GIT_STR_INIT; + const char *branch_name; + int error = 0; + + if ((error = git_repository_initialbranch(&initialbranch, repo)) < 0) + goto done; + + if (git__prefixcmp(initialbranch.ptr, GIT_REFS_HEADS_DIR) != 0) { + git_error_set(GIT_ERROR_INVALID, "invalid initial branch '%s'", initialbranch.ptr); + error = -1; + goto done; + } + + branch_name = initialbranch.ptr + strlen(GIT_REFS_HEADS_DIR); + + error = setup_tracking_config(repo, branch_name, GIT_REMOTE_ORIGIN, + initialbranch.ptr); + +done: + git_str_dispose(&initialbranch); + return error; +} + +static int update_remote_head( + git_repository *repo, + git_remote *remote, + git_str *target, + const char *reflog_message) +{ + git_refspec *refspec; + git_reference *remote_head = NULL; + git_str remote_head_name = GIT_STR_INIT; + git_str remote_branch_name = GIT_STR_INIT; + int error; + + /* Determine the remote tracking ref name from the local branch */ + refspec = git_remote__matching_refspec(remote, git_str_cstr(target)); + + if (refspec == NULL) { + git_error_set(GIT_ERROR_NET, "the remote's default branch does not fit the refspec configuration"); + error = GIT_EINVALIDSPEC; + goto cleanup; + } + + if ((error = git_refspec__transform( + &remote_branch_name, + refspec, + git_str_cstr(target))) < 0) + goto cleanup; + + if ((error = git_str_printf(&remote_head_name, + "%s%s/%s", + GIT_REFS_REMOTES_DIR, + git_remote_name(remote), + GIT_HEAD_FILE)) < 0) + goto cleanup; + + error = git_reference_symbolic_create( + &remote_head, + repo, + git_str_cstr(&remote_head_name), + git_str_cstr(&remote_branch_name), + true, + reflog_message); + +cleanup: + git_reference_free(remote_head); + git_str_dispose(&remote_branch_name); + git_str_dispose(&remote_head_name); + return error; +} + +static int update_head_to_remote( + git_repository *repo, + git_remote *remote, + const char *reflog_message) +{ + int error = 0; + size_t refs_len; + const git_remote_head *remote_head, **refs; + const git_oid *remote_head_id; + git_str branch = GIT_STR_INIT; + + if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0) + return error; + + /* We cloned an empty repository or one with an unborn HEAD */ + if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE)) + return update_head_to_default(repo); + + /* We know we have HEAD, let's see where it points */ + remote_head = refs[0]; + GIT_ASSERT(remote_head); + + remote_head_id = &remote_head->oid; + + error = git_remote__default_branch(&branch, remote); + if (error == GIT_ENOTFOUND) { + error = git_repository_set_head_detached( + repo, remote_head_id); + goto cleanup; + } + + if ((error = update_remote_head(repo, remote, &branch, reflog_message)) < 0) + goto cleanup; + + error = update_head_to_new_branch( + repo, + remote_head_id, + git_str_cstr(&branch), + reflog_message); + +cleanup: + git_str_dispose(&branch); + + return error; +} + +static int update_head_to_branch( + git_repository *repo, + git_remote *remote, + const char *branch, + const char *reflog_message) +{ + int retcode; + git_str remote_branch_name = GIT_STR_INIT; + git_reference *remote_ref = NULL; + git_str default_branch = GIT_STR_INIT; + + GIT_ASSERT_ARG(remote); + GIT_ASSERT_ARG(branch); + + if ((retcode = git_str_printf(&remote_branch_name, GIT_REFS_REMOTES_DIR "%s/%s", + git_remote_name(remote), branch)) < 0 ) + goto cleanup; + + if ((retcode = git_reference_lookup(&remote_ref, repo, git_str_cstr(&remote_branch_name))) < 0) + goto cleanup; + + if ((retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch, + reflog_message)) < 0) + goto cleanup; + + retcode = git_remote__default_branch(&default_branch, remote); + + if (retcode == GIT_ENOTFOUND) + retcode = 0; + else if (retcode) + goto cleanup; + + if (!git_remote__matching_refspec(remote, git_str_cstr(&default_branch))) + goto cleanup; + + retcode = update_remote_head(repo, remote, &default_branch, reflog_message); + +cleanup: + git_reference_free(remote_ref); + git_str_dispose(&remote_branch_name); + git_str_dispose(&default_branch); + return retcode; +} + +static int default_repository_create(git_repository **out, const char *path, int bare, void *payload) +{ + GIT_UNUSED(payload); + + return git_repository_init(out, path, bare); +} + +static int default_remote_create( + git_remote **out, + git_repository *repo, + const char *name, + const char *url, + void *payload) +{ + GIT_UNUSED(payload); + + return git_remote_create(out, repo, name, url); +} + +/* + * submodules? + */ + +static int create_and_configure_origin( + git_remote **out, + git_repository *repo, + const char *url, + const git_clone_options *options) +{ + int error; + git_remote *origin = NULL; + char buf[GIT_PATH_MAX]; + git_remote_create_cb remote_create = options->remote_cb; + void *payload = options->remote_cb_payload; + + /* If the path is local and exists it should be the absolute path. */ + if (!git_net_str_is_url(url) && git_fs_path_root(url) < 0 && + git_fs_path_exists(url)) { + if (p_realpath(url, buf) == NULL) + return -1; + + url = buf; + } + + if (!remote_create) { + remote_create = default_remote_create; + payload = NULL; + } + + if ((error = remote_create(&origin, repo, "origin", url, payload)) < 0) + goto on_error; + + *out = origin; + return 0; + +on_error: + git_remote_free(origin); + return error; +} + +static int should_checkout( + bool *out, + git_repository *repo, + bool is_bare, + const git_clone_options *opts) +{ + int error; + + if (!opts || is_bare || + opts->checkout_opts.checkout_strategy == GIT_CHECKOUT_NONE) { + *out = false; + return 0; + } + + if ((error = git_repository_head_unborn(repo)) < 0) + return error; + + *out = !error; + return 0; +} + +static int checkout_branch( + git_repository *repo, + git_remote *remote, + const git_clone_options *opts, + const char *reflog_message) +{ + bool checkout; + int error; + + if (opts->checkout_branch) + error = update_head_to_branch(repo, remote, opts->checkout_branch, reflog_message); + /* Point HEAD to the same ref as the remote's head */ + else + error = update_head_to_remote(repo, remote, reflog_message); + + if (error < 0) + return error; + + if ((error = should_checkout(&checkout, repo, git_repository_is_bare(repo), opts)) < 0) + return error; + + if (checkout) + error = git_checkout_head(repo, &opts->checkout_opts); + + return error; +} + +static int clone_into( + git_repository *repo, + git_remote *_remote, + const git_clone_options *opts) +{ + git_str reflog_message = GIT_STR_INIT; + git_remote_connect_options connect_opts = GIT_REMOTE_CONNECT_OPTIONS_INIT; + git_remote *remote; + git_oid_t oid_type; + int error; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(_remote); + + if (!git_repository_is_empty(repo)) { + git_error_set(GIT_ERROR_INVALID, "the repository is not empty"); + return -1; + } + + if ((error = git_remote_dup(&remote, _remote)) < 0) + return error; + + if ((error = git_remote_connect_options__from_fetch_opts(&connect_opts, remote, &opts->fetch_opts)) < 0) + goto cleanup; + + git_str_printf(&reflog_message, "clone: from %s", git_remote_url(remote)); + + /* + * Connect to the server so that we can identify the remote + * object format. + */ + + if ((error = git_remote_connect_ext(remote, GIT_DIRECTION_FETCH, + &connect_opts)) < 0) + goto cleanup; + + if ((error = git_remote_oid_type(&oid_type, remote)) < 0 || + (error = git_repository__set_objectformat(repo, oid_type)) < 0) + goto cleanup; + + if ((error = git_remote_fetch(remote, NULL, &opts->fetch_opts, git_str_cstr(&reflog_message))) != 0) + goto cleanup; + + error = checkout_branch(repo, remote, opts, git_str_cstr(&reflog_message)); + +cleanup: + git_remote_free(remote); + git_remote_connect_options_dispose(&connect_opts); + git_str_dispose(&reflog_message); + + return error; +} + +static bool can_link(const char *src, const char *dst, int link) +{ +#ifdef GIT_WIN32 + GIT_UNUSED(src); + GIT_UNUSED(dst); + GIT_UNUSED(link); + return false; +#else + + struct stat st_src, st_dst; + + if (!link) + return false; + + if (p_stat(src, &st_src) < 0) + return false; + + if (p_stat(dst, &st_dst) < 0) + return false; + + return st_src.st_dev == st_dst.st_dev; +#endif +} + +static int clone_local_into( + git_repository *repo, + git_remote *remote, + const git_clone_options *opts) +{ + int error, flags; + git_repository *src; + git_str src_odb = GIT_STR_INIT, dst_odb = GIT_STR_INIT, src_path = GIT_STR_INIT; + git_str reflog_message = GIT_STR_INIT; + bool link = (opts && opts->local != GIT_CLONE_LOCAL_NO_LINKS); + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(remote); + + if (!git_repository_is_empty(repo)) { + git_error_set(GIT_ERROR_INVALID, "the repository is not empty"); + return -1; + } + + /* + * Let's figure out what path we should use for the source + * repo, if it's not rooted, the path should be relative to + * the repository's worktree/gitdir. + */ + if ((error = git_fs_path_from_url_or_path(&src_path, git_remote_url(remote))) < 0) + return error; + + /* Copy .git/objects/ from the source to the target */ + if ((error = git_repository_open(&src, git_str_cstr(&src_path))) < 0) { + git_str_dispose(&src_path); + return error; + } + + if (git_repository__item_path(&src_odb, src, GIT_REPOSITORY_ITEM_OBJECTS) < 0 || + git_repository__item_path(&dst_odb, repo, GIT_REPOSITORY_ITEM_OBJECTS) < 0) { + error = -1; + goto cleanup; + } + + flags = 0; + if (can_link(git_repository_path(src), git_repository_path(repo), link)) + flags |= GIT_CPDIR_LINK_FILES; + + error = git_futils_cp_r(git_str_cstr(&src_odb), git_str_cstr(&dst_odb), + flags, GIT_OBJECT_DIR_MODE); + + /* + * can_link() doesn't catch all variations, so if we hit an + * error and did want to link, let's try again without trying + * to link. + */ + if (error < 0 && link) { + flags &= ~GIT_CPDIR_LINK_FILES; + error = git_futils_cp_r(git_str_cstr(&src_odb), git_str_cstr(&dst_odb), + flags, GIT_OBJECT_DIR_MODE); + } + + if (error < 0) + goto cleanup; + + git_str_printf(&reflog_message, "clone: from %s", git_remote_url(remote)); + + if ((error = git_remote_fetch(remote, NULL, &opts->fetch_opts, git_str_cstr(&reflog_message))) != 0) + goto cleanup; + + error = checkout_branch(repo, remote, opts, git_str_cstr(&reflog_message)); + +cleanup: + git_str_dispose(&reflog_message); + git_str_dispose(&src_path); + git_str_dispose(&src_odb); + git_str_dispose(&dst_odb); + git_repository_free(src); + return error; +} + +int git_clone__should_clone_local( + bool *out, + const char *url_or_path, + git_clone_local_t local) +{ + git_str fromurl = GIT_STR_INIT; + + *out = false; + + if (local == GIT_CLONE_NO_LOCAL) + return 0; + + if (git_net_str_is_url(url_or_path)) { + /* If GIT_CLONE_LOCAL_AUTO is specified, any url should + * be treated as remote */ + if (local == GIT_CLONE_LOCAL_AUTO || + !git_fs_path_is_local_file_url(url_or_path)) + return 0; + + if (git_fs_path_fromurl(&fromurl, url_or_path) < 0) + return -1; + + *out = git_fs_path_isdir(git_str_cstr(&fromurl)); + git_str_dispose(&fromurl); + } else { + *out = git_fs_path_isdir(url_or_path); + } + + return 0; +} + +static int clone_repo( + git_repository **out, + const char *url, + const char *local_path, + const git_clone_options *given_opts, + int use_existing) +{ + int error = 0; + git_repository *repo = NULL; + git_remote *origin; + git_clone_options options = GIT_CLONE_OPTIONS_INIT; + uint32_t rmdir_flags = GIT_RMDIR_REMOVE_FILES; + git_repository_create_cb repository_cb; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(url); + GIT_ASSERT_ARG(local_path); + + if (given_opts) + memcpy(&options, given_opts, sizeof(git_clone_options)); + + GIT_ERROR_CHECK_VERSION(&options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options"); + + /* enforce some behavior on fetch */ + options.fetch_opts.update_fetchhead = 0; + + if (!options.fetch_opts.depth) + options.fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL; + + /* Only clone to a new directory or an empty directory */ + if (git_fs_path_exists(local_path) && !use_existing && !git_fs_path_is_empty_dir(local_path)) { + git_error_set(GIT_ERROR_INVALID, + "'%s' exists and is not an empty directory", local_path); + return GIT_EEXISTS; + } + + /* Only remove the root directory on failure if we create it */ + if (git_fs_path_exists(local_path)) + rmdir_flags |= GIT_RMDIR_SKIP_ROOT; + + if (options.repository_cb) + repository_cb = options.repository_cb; + else + repository_cb = default_repository_create; + + if ((error = repository_cb(&repo, local_path, options.bare, options.repository_cb_payload)) < 0) + return error; + + if (!(error = create_and_configure_origin(&origin, repo, url, &options))) { + bool clone_local; + + if ((error = git_clone__should_clone_local(&clone_local, url, options.local)) < 0) { + git_remote_free(origin); + return error; + } + + if (clone_local) + error = clone_local_into(repo, origin, &options); + else + error = clone_into(repo, origin, &options); + + git_remote_free(origin); + } + + if (error != 0) { + git_error *last_error; + git_error_save(&last_error); + + git_repository_free(repo); + repo = NULL; + + (void)git_futils_rmdir_r(local_path, NULL, rmdir_flags); + + git_error_restore(last_error); + } + + *out = repo; + return error; +} + +int git_clone( + git_repository **out, + const char *url, + const char *local_path, + const git_clone_options *options) +{ + return clone_repo(out, url, local_path, options, 0); +} + +int git_clone__submodule( + git_repository **out, + const char *url, + const char *local_path, + const git_clone_options *options) +{ + return clone_repo(out, url, local_path, options, 1); +} + +int git_clone_options_init(git_clone_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_clone_options, GIT_CLONE_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_clone_init_options(git_clone_options *opts, unsigned int version) +{ + return git_clone_options_init(opts, version); +} +#endif diff --git a/src/libgit2/clone.h b/src/libgit2/clone.h new file mode 100644 index 00000000000..fde796f91bd --- /dev/null +++ b/src/libgit2/clone.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_clone_h__ +#define INCLUDE_clone_h__ + +#include "common.h" + +#include "git2/clone.h" + +extern int git_clone__submodule(git_repository **out, + const char *url, const char *local_path, + const git_clone_options *_options); + +extern int git_clone__should_clone_local( + bool *out, + const char *url, + git_clone_local_t local); + +#endif diff --git a/src/libgit2/commit.c b/src/libgit2/commit.c new file mode 100644 index 00000000000..633279227fc --- /dev/null +++ b/src/libgit2/commit.c @@ -0,0 +1,1191 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "commit.h" + +#include "git2/common.h" +#include "git2/object.h" +#include "git2/repository.h" +#include "git2/signature.h" +#include "git2/mailmap.h" +#include "git2/sys/commit.h" + +#include "buf.h" +#include "odb.h" +#include "commit.h" +#include "signature.h" +#include "refs.h" +#include "object.h" +#include "array.h" +#include "oidarray.h" +#include "grafts.h" + +void git_commit__free(void *_commit) +{ + git_commit *commit = _commit; + + git_array_clear(commit->parent_ids); + + git_signature_free(commit->author); + git_signature_free(commit->committer); + + git__free(commit->raw_header); + git__free(commit->raw_message); + git__free(commit->message_encoding); + git__free(commit->summary); + git__free(commit->body); + + git__free(commit); +} + +static int git_commit__create_buffer_internal( + git_str *out, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_oid *tree, + git_array_oid_t *parents) +{ + size_t i = 0; + const git_oid *parent; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(tree); + + if (git_object__write_oid_header(out, "tree ", tree) < 0) + goto on_error; + + for (i = 0; i < git_array_size(*parents); i++) { + parent = git_array_get(*parents, i); + if (git_object__write_oid_header(out, "parent ", parent) < 0) + goto on_error; + } + + git_signature__writebuf(out, "author ", author); + git_signature__writebuf(out, "committer ", committer); + + if (message_encoding != NULL) + git_str_printf(out, "encoding %s\n", message_encoding); + + git_str_putc(out, '\n'); + + if (git_str_puts(out, message) < 0) + goto on_error; + + return 0; + +on_error: + git_str_dispose(out); + return -1; +} + +static int validate_tree_and_parents(git_array_oid_t *parents, git_repository *repo, const git_oid *tree, + git_commit_parent_callback parent_cb, void *parent_payload, + const git_oid *current_id, bool validate) +{ + size_t i; + int error; + git_oid *parent_cpy; + const git_oid *parent; + + if (validate && !git_object__is_valid(repo, tree, GIT_OBJECT_TREE)) + return -1; + + i = 0; + while ((parent = parent_cb(i, parent_payload)) != NULL) { + if (validate && !git_object__is_valid(repo, parent, GIT_OBJECT_COMMIT)) { + error = -1; + goto on_error; + } + + parent_cpy = git_array_alloc(*parents); + GIT_ERROR_CHECK_ALLOC(parent_cpy); + + git_oid_cpy(parent_cpy, parent); + i++; + } + + if (current_id && (parents->size == 0 || git_oid_cmp(current_id, git_array_get(*parents, 0)))) { + git_error_set(GIT_ERROR_OBJECT, "failed to create commit: current tip is not the first parent"); + error = GIT_EMODIFIED; + goto on_error; + } + + return 0; + +on_error: + git_array_clear(*parents); + return error; +} + +static int git_commit__create_internal( + git_oid *id, + git_repository *repo, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_oid *tree, + git_commit_parent_callback parent_cb, + void *parent_payload, + bool validate) +{ + int error; + git_odb *odb; + git_reference *ref = NULL; + git_str buf = GIT_STR_INIT; + const git_oid *current_id = NULL; + git_array_oid_t parents = GIT_ARRAY_INIT; + + if (update_ref) { + error = git_reference_lookup_resolved(&ref, repo, update_ref, 10); + if (error < 0 && error != GIT_ENOTFOUND) + return error; + } + git_error_clear(); + + if (ref) + current_id = git_reference_target(ref); + + if ((error = validate_tree_and_parents(&parents, repo, tree, parent_cb, parent_payload, current_id, validate)) < 0) + goto cleanup; + + error = git_commit__create_buffer_internal(&buf, author, committer, + message_encoding, message, tree, + &parents); + + if (error < 0) + goto cleanup; + + if (git_repository_odb__weakptr(&odb, repo) < 0) + goto cleanup; + + if (git_odb__freshen(odb, tree) < 0) + goto cleanup; + + if (git_odb_write(id, odb, buf.ptr, buf.size, GIT_OBJECT_COMMIT) < 0) + goto cleanup; + + + if (update_ref != NULL) { + error = git_reference__update_for_commit( + repo, ref, update_ref, id, "commit"); + goto cleanup; + } + +cleanup: + git_array_clear(parents); + git_reference_free(ref); + git_str_dispose(&buf); + return error; +} + +int git_commit_create_from_callback( + git_oid *id, + git_repository *repo, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_oid *tree, + git_commit_parent_callback parent_cb, + void *parent_payload) +{ + return git_commit__create_internal( + id, repo, update_ref, author, committer, message_encoding, message, + tree, parent_cb, parent_payload, true); +} + +typedef struct { + size_t total; + va_list args; +} commit_parent_varargs; + +static const git_oid *commit_parent_from_varargs(size_t curr, void *payload) +{ + commit_parent_varargs *data = payload; + const git_commit *commit; + if (curr >= data->total) + return NULL; + commit = va_arg(data->args, const git_commit *); + return commit ? git_commit_id(commit) : NULL; +} + +int git_commit_create_v( + git_oid *id, + git_repository *repo, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree, + size_t parent_count, + ...) +{ + int error = 0; + commit_parent_varargs data; + + GIT_ASSERT_ARG(tree); + GIT_ASSERT_ARG(git_tree_owner(tree) == repo); + + data.total = parent_count; + va_start(data.args, parent_count); + + error = git_commit__create_internal( + id, repo, update_ref, author, committer, + message_encoding, message, git_tree_id(tree), + commit_parent_from_varargs, &data, false); + + va_end(data.args); + return error; +} + +typedef struct { + size_t total; + const git_oid **parents; +} commit_parent_oids; + +static const git_oid *commit_parent_from_ids(size_t curr, void *payload) +{ + commit_parent_oids *data = payload; + return (curr < data->total) ? data->parents[curr] : NULL; +} + +int git_commit_create_from_ids( + git_oid *id, + git_repository *repo, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_oid *tree, + size_t parent_count, + const git_oid *parents[]) +{ + commit_parent_oids data = { parent_count, parents }; + + return git_commit__create_internal( + id, repo, update_ref, author, committer, + message_encoding, message, tree, + commit_parent_from_ids, &data, true); +} + +typedef struct { + size_t total; + const git_commit **parents; + git_repository *repo; +} commit_parent_data; + +static const git_oid *commit_parent_from_array(size_t curr, void *payload) +{ + commit_parent_data *data = payload; + const git_commit *commit; + if (curr >= data->total) + return NULL; + commit = data->parents[curr]; + if (git_commit_owner(commit) != data->repo) + return NULL; + return git_commit_id(commit); +} + +int git_commit_create( + git_oid *id, + git_repository *repo, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree, + size_t parent_count, + const git_commit *parents[]) +{ + commit_parent_data data = { parent_count, parents, repo }; + + GIT_ASSERT_ARG(tree); + GIT_ASSERT_ARG(git_tree_owner(tree) == repo); + + return git_commit__create_internal( + id, repo, update_ref, author, committer, + message_encoding, message, git_tree_id(tree), + commit_parent_from_array, &data, false); +} + +static const git_oid *commit_parent_for_amend(size_t curr, void *payload) +{ + const git_commit *commit_to_amend = payload; + if (curr >= git_array_size(commit_to_amend->parent_ids)) + return NULL; + return git_array_get(commit_to_amend->parent_ids, curr); +} + +int git_commit_amend( + git_oid *id, + const git_commit *commit_to_amend, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree) +{ + git_repository *repo; + git_oid tree_id; + git_reference *ref = NULL; + int error; + + GIT_ASSERT_ARG(id); + GIT_ASSERT_ARG(commit_to_amend); + + repo = git_commit_owner(commit_to_amend); + + if (!author) + author = git_commit_author(commit_to_amend); + if (!committer) + committer = git_commit_committer(commit_to_amend); + if (!message_encoding) + message_encoding = git_commit_message_encoding(commit_to_amend); + if (!message) + message = git_commit_message(commit_to_amend); + + if (!tree) { + git_tree *old_tree; + GIT_ERROR_CHECK_ERROR( git_commit_tree(&old_tree, commit_to_amend) ); + git_oid_cpy(&tree_id, git_tree_id(old_tree)); + git_tree_free(old_tree); + } else { + GIT_ASSERT_ARG(git_tree_owner(tree) == repo); + git_oid_cpy(&tree_id, git_tree_id(tree)); + } + + if (update_ref) { + if ((error = git_reference_lookup_resolved(&ref, repo, update_ref, 5)) < 0) + return error; + + if (git_oid_cmp(git_commit_id(commit_to_amend), git_reference_target(ref))) { + git_reference_free(ref); + git_error_set(GIT_ERROR_REFERENCE, "commit to amend is not the tip of the given branch"); + return -1; + } + } + + error = git_commit__create_internal( + id, repo, NULL, author, committer, message_encoding, message, + &tree_id, commit_parent_for_amend, (void *)commit_to_amend, false); + + if (!error && update_ref) { + error = git_reference__update_for_commit( + repo, ref, NULL, id, "commit"); + git_reference_free(ref); + } + + return error; +} + +static int commit_parse( + git_commit *commit, + const char *data, + size_t size, + git_commit__parse_options *opts) +{ + const char *buffer_start = data, *buffer; + const char *buffer_end = buffer_start + size; + git_oid parent_id; + size_t header_len; + git_signature dummy_sig; + int error; + + GIT_ASSERT_ARG(commit); + GIT_ASSERT_ARG(data); + GIT_ASSERT_ARG(opts); + + buffer = buffer_start; + + /* Allocate for one, which will allow not to realloc 90% of the time */ + git_array_init_to_size(commit->parent_ids, 1); + GIT_ERROR_CHECK_ARRAY(commit->parent_ids); + + /* The tree is always the first field */ + if (!(opts->flags & GIT_COMMIT_PARSE_QUICK)) { + if (git_object__parse_oid_header(&commit->tree_id, + &buffer, buffer_end, "tree ", + opts->oid_type) < 0) + goto bad_buffer; + } else { + size_t tree_len = strlen("tree ") + git_oid_hexsize(opts->oid_type) + 1; + + if (buffer + tree_len > buffer_end) + goto bad_buffer; + buffer += tree_len; + } + + while (git_object__parse_oid_header(&parent_id, + &buffer, buffer_end, "parent ", + opts->oid_type) == 0) { + git_oid *new_id = git_array_alloc(commit->parent_ids); + GIT_ERROR_CHECK_ALLOC(new_id); + + git_oid_cpy(new_id, &parent_id); + } + + if (!opts || !(opts->flags & GIT_COMMIT_PARSE_QUICK)) { + commit->author = git__malloc(sizeof(git_signature)); + GIT_ERROR_CHECK_ALLOC(commit->author); + + if ((error = git_signature__parse(commit->author, &buffer, buffer_end, "author ", '\n')) < 0) + return error; + } + + /* Some tools create multiple author fields, ignore the extra ones */ + while (!git__prefixncmp(buffer, buffer_end - buffer, "author ")) { + if ((error = git_signature__parse(&dummy_sig, &buffer, buffer_end, "author ", '\n')) < 0) + return error; + + git__free(dummy_sig.name); + git__free(dummy_sig.email); + } + + /* Always parse the committer; we need the commit time */ + commit->committer = git__malloc(sizeof(git_signature)); + GIT_ERROR_CHECK_ALLOC(commit->committer); + + if ((error = git_signature__parse(commit->committer, &buffer, buffer_end, "committer ", '\n')) < 0) + return error; + + if (opts && opts->flags & GIT_COMMIT_PARSE_QUICK) + return 0; + + /* Parse add'l header entries */ + while (buffer < buffer_end) { + const char *eoln = buffer; + if (buffer[-1] == '\n' && buffer[0] == '\n') + break; + + while (eoln < buffer_end && *eoln != '\n') + ++eoln; + + if (git__prefixncmp(buffer, buffer_end - buffer, "encoding ") == 0) { + buffer += strlen("encoding "); + + commit->message_encoding = git__strndup(buffer, eoln - buffer); + GIT_ERROR_CHECK_ALLOC(commit->message_encoding); + } + + if (eoln < buffer_end && *eoln == '\n') + ++eoln; + buffer = eoln; + } + + header_len = buffer - buffer_start; + commit->raw_header = git__strndup(buffer_start, header_len); + GIT_ERROR_CHECK_ALLOC(commit->raw_header); + + /* point "buffer" to data after header, +1 for the final LF */ + buffer = buffer_start + header_len + 1; + + /* extract commit message */ + if (buffer <= buffer_end) + commit->raw_message = git__strndup(buffer, buffer_end - buffer); + else + commit->raw_message = git__strdup(""); + GIT_ERROR_CHECK_ALLOC(commit->raw_message); + + return 0; + +bad_buffer: + git_error_set(GIT_ERROR_OBJECT, "failed to parse bad commit object"); + return GIT_EINVALID; +} + +int git_commit__parse( + void *commit, + git_odb_object *odb_obj, + git_oid_t oid_type) +{ + git_commit__parse_options parse_options = {0}; + parse_options.oid_type = oid_type; + + return git_commit__parse_ext(commit, odb_obj, &parse_options); +} + +int git_commit__parse_raw( + void *commit, + const char *data, + size_t size, + git_oid_t oid_type) +{ + git_commit__parse_options parse_options = {0}; + parse_options.oid_type = oid_type; + + return commit_parse(commit, data, size, &parse_options); +} + +static int assign_commit_parents_from_graft(git_commit *commit, git_commit_graft *graft) { + size_t idx; + git_oid *oid; + + git_array_clear(commit->parent_ids); + git_array_init_to_size(commit->parent_ids, git_array_size(graft->parents)); + git_array_foreach(graft->parents, idx, oid) { + git_oid *id = git_array_alloc(commit->parent_ids); + GIT_ERROR_CHECK_ALLOC(id); + + git_oid_cpy(id, oid); + } + + return 0; +} + +int git_commit__parse_ext( + git_commit *commit, + git_odb_object *odb_obj, + git_commit__parse_options *parse_opts) +{ + git_repository *repo = git_object_owner((git_object *)commit); + git_commit_graft *graft; + int error; + + if ((error = commit_parse(commit, git_odb_object_data(odb_obj), + git_odb_object_size(odb_obj), parse_opts)) < 0) + return error; + + /* Perform necessary grafts */ + if (git_grafts_get(&graft, repo->grafts, git_odb_object_id(odb_obj)) != 0 && + git_grafts_get(&graft, repo->shallow_grafts, git_odb_object_id(odb_obj)) != 0) + return 0; + + return assign_commit_parents_from_graft(commit, graft); +} + +#define GIT_COMMIT_GETTER(_rvalue, _name, _return, _invalid) \ + _rvalue git_commit_##_name(const git_commit *commit) \ + {\ + GIT_ASSERT_ARG_WITH_RETVAL(commit, _invalid); \ + return _return; \ + } + +GIT_COMMIT_GETTER(const git_signature *, author, commit->author, NULL) +GIT_COMMIT_GETTER(const git_signature *, committer, commit->committer, NULL) +GIT_COMMIT_GETTER(const char *, message_raw, commit->raw_message, NULL) +GIT_COMMIT_GETTER(const char *, message_encoding, commit->message_encoding, NULL) +GIT_COMMIT_GETTER(const char *, raw_header, commit->raw_header, NULL) +GIT_COMMIT_GETTER(git_time_t, time, commit->committer->when.time, INT64_MIN) +GIT_COMMIT_GETTER(int, time_offset, commit->committer->when.offset, -1) +GIT_COMMIT_GETTER(unsigned int, parentcount, (unsigned int)git_array_size(commit->parent_ids), 0) +GIT_COMMIT_GETTER(const git_oid *, tree_id, &commit->tree_id, NULL) + +const char *git_commit_message(const git_commit *commit) +{ + const char *message; + + GIT_ASSERT_ARG_WITH_RETVAL(commit, NULL); + + message = commit->raw_message; + + /* trim leading newlines from raw message */ + while (*message && *message == '\n') + ++message; + + return message; +} + +const char *git_commit_summary(git_commit *commit) +{ + git_str summary = GIT_STR_INIT; + const char *msg, *space, *next; + bool space_contains_newline = false; + + GIT_ASSERT_ARG_WITH_RETVAL(commit, NULL); + + if (!commit->summary) { + for (msg = git_commit_message(commit), space = NULL; *msg; ++msg) { + char next_character = msg[0]; + /* stop processing at the end of the first paragraph */ + if (next_character == '\n') { + if (!msg[1]) + break; + if (msg[1] == '\n') + break; + /* stop processing if next line contains only whitespace */ + next = msg + 1; + while (*next && git__isspace_nonlf(*next)) { + ++next; + } + if (!*next || *next == '\n') + break; + } + /* record the beginning of contiguous whitespace runs */ + if (git__isspace(next_character)) { + if(space == NULL) { + space = msg; + space_contains_newline = false; + } + space_contains_newline |= next_character == '\n'; + } + /* the next character is non-space */ + else { + /* process any recorded whitespace */ + if (space) { + if(space_contains_newline) + git_str_putc(&summary, ' '); /* if the space contains a newline, collapse to ' ' */ + else + git_str_put(&summary, space, (msg - space)); /* otherwise copy it */ + space = NULL; + } + /* copy the next character */ + git_str_putc(&summary, next_character); + } + } + + commit->summary = git_str_detach(&summary); + if (!commit->summary) + commit->summary = git__strdup(""); + } + + return commit->summary; +} + +const char *git_commit_body(git_commit *commit) +{ + const char *msg, *end; + + GIT_ASSERT_ARG_WITH_RETVAL(commit, NULL); + + if (!commit->body) { + /* search for end of summary */ + for (msg = git_commit_message(commit); *msg; ++msg) + if (msg[0] == '\n' && (!msg[1] || msg[1] == '\n')) + break; + + /* trim leading and trailing whitespace */ + for (; *msg; ++msg) + if (!git__isspace(*msg)) + break; + for (end = msg + strlen(msg) - 1; msg <= end; --end) + if (!git__isspace(*end)) + break; + + if (*msg) + commit->body = git__strndup(msg, end - msg + 1); + } + + return commit->body; +} + +int git_commit_tree(git_tree **tree_out, const git_commit *commit) +{ + GIT_ASSERT_ARG(commit); + return git_tree_lookup(tree_out, commit->object.repo, &commit->tree_id); +} + +const git_oid *git_commit_parent_id( + const git_commit *commit, unsigned int n) +{ + GIT_ASSERT_ARG_WITH_RETVAL(commit, NULL); + + return git_array_get(commit->parent_ids, n); +} + +int git_commit_parent( + git_commit **parent, const git_commit *commit, unsigned int n) +{ + const git_oid *parent_id; + GIT_ASSERT_ARG(commit); + + parent_id = git_commit_parent_id(commit, n); + if (parent_id == NULL) { + git_error_set(GIT_ERROR_INVALID, "parent %u does not exist", n); + return GIT_ENOTFOUND; + } + + return git_commit_lookup(parent, commit->object.repo, parent_id); +} + +int git_commit_nth_gen_ancestor( + git_commit **ancestor, + const git_commit *commit, + unsigned int n) +{ + git_commit *current, *parent = NULL; + int error; + + GIT_ASSERT_ARG(ancestor); + GIT_ASSERT_ARG(commit); + + if (git_commit_dup(¤t, (git_commit *)commit) < 0) + return -1; + + if (n == 0) { + *ancestor = current; + return 0; + } + + while (n--) { + error = git_commit_parent(&parent, current, 0); + + git_commit_free(current); + + if (error < 0) + return error; + + current = parent; + } + + *ancestor = parent; + return 0; +} + +int git_commit_header_field( + git_buf *out, + const git_commit *commit, + const char *field) +{ + GIT_BUF_WRAP_PRIVATE(out, git_commit__header_field, commit, field); +} + +int git_commit__header_field( + git_str *out, + const git_commit *commit, + const char *field) +{ + const char *eol, *buf = commit->raw_header; + + git_str_clear(out); + + while ((eol = strchr(buf, '\n'))) { + /* We can skip continuations here */ + if (buf[0] == ' ') { + buf = eol + 1; + continue; + } + + /* Skip until we find the field we're after */ + if (git__prefixcmp(buf, field)) { + buf = eol + 1; + continue; + } + + buf += strlen(field); + /* Check that we're not matching a prefix but the field itself */ + if (buf[0] != ' ') { + buf = eol + 1; + continue; + } + + buf++; /* skip the SP */ + + git_str_put(out, buf, eol - buf); + if (git_str_oom(out)) + goto oom; + + /* If the next line starts with SP, it's multi-line, we must continue */ + while (eol[1] == ' ') { + git_str_putc(out, '\n'); + buf = eol + 2; + eol = strchr(buf, '\n'); + if (!eol) + goto malformed; + + git_str_put(out, buf, eol - buf); + } + + if (git_str_oom(out)) + goto oom; + + return 0; + } + + git_error_set(GIT_ERROR_OBJECT, "no such field '%s'", field); + return GIT_ENOTFOUND; + +malformed: + git_error_set(GIT_ERROR_OBJECT, "malformed header"); + return -1; +oom: + git_error_set_oom(); + return -1; +} + +int git_commit_extract_signature( + git_buf *signature_out, + git_buf *signed_data_out, + git_repository *repo, + git_oid *commit_id, + const char *field) +{ + git_str signature = GIT_STR_INIT, signed_data = GIT_STR_INIT; + int error; + + if ((error = git_buf_tostr(&signature, signature_out)) < 0 || + (error = git_buf_tostr(&signed_data, signed_data_out)) < 0 || + (error = git_commit__extract_signature(&signature, &signed_data, repo, commit_id, field)) < 0 || + (error = git_buf_fromstr(signature_out, &signature)) < 0 || + (error = git_buf_fromstr(signed_data_out, &signed_data)) < 0) + goto done; + +done: + git_str_dispose(&signature); + git_str_dispose(&signed_data); + return error; +} + +int git_commit__extract_signature( + git_str *signature, + git_str *signed_data, + git_repository *repo, + git_oid *commit_id, + const char *field) +{ + git_odb_object *obj; + git_odb *odb; + const char *buf; + const char *h, *eol; + int error; + + git_str_clear(signature); + git_str_clear(signed_data); + + if (!field) + field = "gpgsig"; + + if ((error = git_repository_odb__weakptr(&odb, repo)) < 0) + return error; + + if ((error = git_odb_read(&obj, odb, commit_id)) < 0) + return error; + + if (obj->cached.type != GIT_OBJECT_COMMIT) { + git_error_set(GIT_ERROR_INVALID, "the requested type does not match the type in the ODB"); + error = GIT_ENOTFOUND; + goto cleanup; + } + + buf = git_odb_object_data(obj); + + while ((h = strchr(buf, '\n')) && h[1] != '\0') { + h++; + if (git__prefixcmp(buf, field)) { + if (git_str_put(signed_data, buf, h - buf) < 0) + return -1; + + buf = h; + continue; + } + + h = buf; + h += strlen(field); + eol = strchr(h, '\n'); + if (h[0] != ' ') { + buf = h; + continue; + } + if (!eol) + goto malformed; + + h++; /* skip the SP */ + + git_str_put(signature, h, eol - h); + if (git_str_oom(signature)) + goto oom; + + /* If the next line starts with SP, it's multi-line, we must continue */ + while (eol[1] == ' ') { + git_str_putc(signature, '\n'); + h = eol + 2; + eol = strchr(h, '\n'); + if (!eol) + goto malformed; + + git_str_put(signature, h, eol - h); + } + + if (git_str_oom(signature)) + goto oom; + + error = git_str_puts(signed_data, eol+1); + git_odb_object_free(obj); + return error; + } + + git_error_set(GIT_ERROR_OBJECT, "this commit is not signed"); + error = GIT_ENOTFOUND; + goto cleanup; + +malformed: + git_error_set(GIT_ERROR_OBJECT, "malformed header"); + error = -1; + goto cleanup; +oom: + git_error_set_oom(); + error = -1; + goto cleanup; + +cleanup: + git_odb_object_free(obj); + git_str_clear(signature); + git_str_clear(signed_data); + return error; +} + +int git_commit_create_buffer( + git_buf *out, + git_repository *repo, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree, + size_t parent_count, + const git_commit *parents[]) +{ + GIT_BUF_WRAP_PRIVATE(out, git_commit__create_buffer, repo, + author, committer, message_encoding, message, + tree, parent_count, parents); +} + +int git_commit__create_buffer( + git_str *out, + git_repository *repo, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree, + size_t parent_count, + const git_commit *parents[]) +{ + int error; + commit_parent_data data = { parent_count, parents, repo }; + git_array_oid_t parents_arr = GIT_ARRAY_INIT; + const git_oid *tree_id; + + GIT_ASSERT_ARG(tree); + GIT_ASSERT_ARG(git_tree_owner(tree) == repo); + + tree_id = git_tree_id(tree); + + if ((error = validate_tree_and_parents(&parents_arr, repo, tree_id, commit_parent_from_array, &data, NULL, true)) < 0) + return error; + + error = git_commit__create_buffer_internal( + out, author, committer, + message_encoding, message, tree_id, + &parents_arr); + + git_array_clear(parents_arr); + return error; +} + +/** + * Append to 'out' properly marking continuations when there's a newline in 'content' + */ +static int format_header_field(git_str *out, const char *field, const char *content) +{ + const char *lf; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(field); + GIT_ASSERT_ARG(content); + + git_str_puts(out, field); + git_str_putc(out, ' '); + + while ((lf = strchr(content, '\n')) != NULL) { + git_str_put(out, content, lf - content); + git_str_puts(out, "\n "); + content = lf + 1; + } + + git_str_puts(out, content); + git_str_putc(out, '\n'); + + return git_str_oom(out) ? -1 : 0; +} + +static const git_oid *commit_parent_from_commit(size_t n, void *payload) +{ + const git_commit *commit = (const git_commit *) payload; + + return git_array_get(commit->parent_ids, n); + +} + +int git_commit_create_with_signature( + git_oid *out, + git_repository *repo, + const char *commit_content, + const char *signature, + const char *signature_field) +{ + git_odb *odb; + int error = 0; + const char *field; + const char *header_end; + git_str commit = GIT_STR_INIT; + git_commit *parsed; + git_array_oid_t parents = GIT_ARRAY_INIT; + git_commit__parse_options parse_opts = {0}; + + parse_opts.oid_type = repo->oid_type; + + /* The first step is to verify that all the tree and parents exist */ + parsed = git__calloc(1, sizeof(git_commit)); + GIT_ERROR_CHECK_ALLOC(parsed); + if (commit_parse(parsed, commit_content, strlen(commit_content), &parse_opts) < 0) { + error = -1; + goto cleanup; + } + + if ((error = validate_tree_and_parents(&parents, repo, &parsed->tree_id, commit_parent_from_commit, parsed, NULL, true)) < 0) + goto cleanup; + + git_array_clear(parents); + + /* Then we start appending by identifying the end of the commit header */ + header_end = strstr(commit_content, "\n\n"); + if (!header_end) { + git_error_set(GIT_ERROR_INVALID, "malformed commit contents"); + error = -1; + goto cleanup; + } + + /* The header ends after the first LF */ + header_end++; + git_str_put(&commit, commit_content, header_end - commit_content); + + if (signature != NULL) { + field = signature_field ? signature_field : "gpgsig"; + + if ((error = format_header_field(&commit, field, signature)) < 0) + goto cleanup; + } + + git_str_puts(&commit, header_end); + + if (git_str_oom(&commit)) + return -1; + + if ((error = git_repository_odb__weakptr(&odb, repo)) < 0) + goto cleanup; + + if ((error = git_odb_write(out, odb, commit.ptr, commit.size, GIT_OBJECT_COMMIT)) < 0) + goto cleanup; + +cleanup: + git_commit__free(parsed); + git_str_dispose(&commit); + return error; +} + +int git_commit_create_from_stage( + git_oid *out, + git_repository *repo, + const char *message, + const git_commit_create_options *given_opts) +{ + git_commit_create_options opts = GIT_COMMIT_CREATE_OPTIONS_INIT; + git_signature *default_signature = NULL; + const git_signature *author, *committer; + git_index *index = NULL; + git_diff *diff = NULL; + git_oid tree_id; + git_tree *head_tree = NULL, *tree = NULL; + git_commitarray parents = { 0 }; + int error = -1; + + GIT_ASSERT_ARG(out && repo); + + if (given_opts) + memcpy(&opts, given_opts, sizeof(git_commit_create_options)); + + author = opts.author; + committer = opts.committer; + + if (!author || !committer) { + if (git_signature_default(&default_signature, repo) < 0) + goto done; + + if (!author) + author = default_signature; + + if (!committer) + committer = default_signature; + } + + if (git_repository_index(&index, repo) < 0) + goto done; + + if (!opts.allow_empty_commit) { + error = git_repository_head_tree(&head_tree, repo); + + if (error && error != GIT_EUNBORNBRANCH) + goto done; + + error = -1; + + if (git_diff_tree_to_index(&diff, repo, head_tree, index, NULL) < 0) + goto done; + + if (git_diff_num_deltas(diff) == 0) { + git_error_set(GIT_ERROR_REPOSITORY, + "no changes are staged for commit"); + error = GIT_EUNCHANGED; + goto done; + } + } + + if (git_index_write_tree(&tree_id, index) < 0 || + git_tree_lookup(&tree, repo, &tree_id) < 0 || + git_repository_commit_parents(&parents, repo) < 0) + goto done; + + error = git_commit_create(out, repo, "HEAD", author, committer, + opts.message_encoding, message, + tree, parents.count, + (const git_commit **)parents.commits); + +done: + git_commitarray_dispose(&parents); + git_signature_free(default_signature); + git_tree_free(tree); + git_tree_free(head_tree); + git_diff_free(diff); + git_index_free(index); + return error; +} + +int git_commit_committer_with_mailmap( + git_signature **out, const git_commit *commit, const git_mailmap *mailmap) +{ + return git_mailmap_resolve_signature(out, mailmap, commit->committer); +} + +int git_commit_author_with_mailmap( + git_signature **out, const git_commit *commit, const git_mailmap *mailmap) +{ + return git_mailmap_resolve_signature(out, mailmap, commit->author); +} + +void git_commitarray_dispose(git_commitarray *array) +{ + size_t i; + + if (array == NULL) + return; + + for (i = 0; i < array->count; i++) + git_commit_free(array->commits[i]); + + git__free((git_commit **)array->commits); + + memset(array, 0, sizeof(*array)); +} diff --git a/src/libgit2/commit.h b/src/libgit2/commit.h new file mode 100644 index 00000000000..c25fee327b8 --- /dev/null +++ b/src/libgit2/commit.h @@ -0,0 +1,87 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_commit_h__ +#define INCLUDE_commit_h__ + +#include "common.h" + +#include "git2/commit.h" +#include "tree.h" +#include "repository.h" +#include "array.h" + +#include + +struct git_commit { + git_object object; + + git_array_t(git_oid) parent_ids; + git_oid tree_id; + + git_signature *author; + git_signature *committer; + + char *message_encoding; + char *raw_message; + char *raw_header; + + char *summary; + char *body; +}; + +typedef struct { + git_oid_t oid_type; + unsigned int flags; +} git_commit__parse_options; + +typedef enum { + /** Only parse parents and committer info */ + GIT_COMMIT_PARSE_QUICK = (1 << 0) +} git_commit__parse_flags; + +int git_commit__header_field( + git_str *out, + const git_commit *commit, + const char *field); + +int git_commit__extract_signature( + git_str *signature, + git_str *signed_data, + git_repository *repo, + git_oid *commit_id, + const char *field); + +int git_commit__create_buffer( + git_str *out, + git_repository *repo, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree, + size_t parent_count, + const git_commit *parents[]); + +int git_commit__parse( + void *commit, + git_odb_object *obj, + git_oid_t oid_type); + +int git_commit__parse_raw( + void *commit, + const char *data, + size_t size, + git_oid_t oid_type); + +int git_commit__parse_ext( + git_commit *commit, + git_odb_object *odb_obj, + git_commit__parse_options *parse_opts); + +void git_commit__free(void *commit); + +#endif diff --git a/src/libgit2/commit_graph.c b/src/libgit2/commit_graph.c new file mode 100644 index 00000000000..a841985da01 --- /dev/null +++ b/src/libgit2/commit_graph.c @@ -0,0 +1,1319 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "commit_graph.h" + +#include "array.h" +#include "buf.h" +#include "filebuf.h" +#include "futils.h" +#include "hash.h" +#include "oidarray.h" +#include "pack.h" +#include "repository.h" +#include "revwalk.h" + +#define GIT_COMMIT_GRAPH_MISSING_PARENT 0x70000000 +#define GIT_COMMIT_GRAPH_GENERATION_NUMBER_MAX 0x3FFFFFFF +#define GIT_COMMIT_GRAPH_GENERATION_NUMBER_INFINITY 0xFFFFFFFF + +#define COMMIT_GRAPH_SIGNATURE 0x43475048 /* "CGPH" */ +#define COMMIT_GRAPH_VERSION 1 +#define COMMIT_GRAPH_OBJECT_ID_VERSION 1 + +struct git_commit_graph_header { + uint32_t signature; + uint8_t version; + uint8_t object_id_version; + uint8_t chunks; + uint8_t base_graph_files; +}; + +#define COMMIT_GRAPH_OID_FANOUT_ID 0x4f494446 /* "OIDF" */ +#define COMMIT_GRAPH_OID_LOOKUP_ID 0x4f49444c /* "OIDL" */ +#define COMMIT_GRAPH_COMMIT_DATA_ID 0x43444154 /* "CDAT" */ +#define COMMIT_GRAPH_EXTRA_EDGE_LIST_ID 0x45444745 /* "EDGE" */ +#define COMMIT_GRAPH_BLOOM_FILTER_INDEX_ID 0x42494458 /* "BIDX" */ +#define COMMIT_GRAPH_BLOOM_FILTER_DATA_ID 0x42444154 /* "BDAT" */ + +struct git_commit_graph_chunk { + off64_t offset; + size_t length; +}; + +typedef git_array_t(size_t) parent_index_array_t; + +struct packed_commit { + size_t index; + git_oid sha1; + git_oid tree_oid; + uint32_t generation; + git_time_t commit_time; + git_array_oid_t parents; + parent_index_array_t parent_indices; +}; + +static void packed_commit_free(struct packed_commit *p) +{ + if (!p) + return; + + git_array_clear(p->parents); + git_array_clear(p->parent_indices); + git__free(p); +} + +static struct packed_commit *packed_commit_new(git_commit *commit) +{ + unsigned int i, parentcount = git_commit_parentcount(commit); + struct packed_commit *p = git__calloc(1, sizeof(struct packed_commit)); + if (!p) + goto cleanup; + + git_array_init_to_size(p->parents, parentcount); + if (parentcount && !p->parents.ptr) + goto cleanup; + + if (git_oid_cpy(&p->sha1, git_commit_id(commit)) < 0) + goto cleanup; + if (git_oid_cpy(&p->tree_oid, git_commit_tree_id(commit)) < 0) + goto cleanup; + p->commit_time = git_commit_time(commit); + + for (i = 0; i < parentcount; ++i) { + git_oid *parent_id = git_array_alloc(p->parents); + if (!parent_id) + goto cleanup; + if (git_oid_cpy(parent_id, git_commit_parent_id(commit, i)) < 0) + goto cleanup; + } + + return p; + +cleanup: + packed_commit_free(p); + return NULL; +} + +typedef int (*commit_graph_write_cb)(const char *buf, size_t size, void *cb_data); + +static int commit_graph_error(const char *message) +{ + git_error_set(GIT_ERROR_ODB, "invalid commit-graph file - %s", message); + return -1; +} + +static int commit_graph_parse_oid_fanout( + git_commit_graph_file *file, + const unsigned char *data, + struct git_commit_graph_chunk *chunk_oid_fanout) +{ + uint32_t i, nr; + if (chunk_oid_fanout->offset == 0) + return commit_graph_error("missing OID Fanout chunk"); + if (chunk_oid_fanout->length == 0) + return commit_graph_error("empty OID Fanout chunk"); + if (chunk_oid_fanout->length != 256 * 4) + return commit_graph_error("OID Fanout chunk has wrong length"); + + file->oid_fanout = (const uint32_t *)(data + chunk_oid_fanout->offset); + nr = 0; + for (i = 0; i < 256; ++i) { + uint32_t n = ntohl(file->oid_fanout[i]); + if (n < nr) + return commit_graph_error("index is non-monotonic"); + nr = n; + } + file->num_commits = nr; + return 0; +} + +static int commit_graph_parse_oid_lookup( + git_commit_graph_file *file, + const unsigned char *data, + struct git_commit_graph_chunk *chunk_oid_lookup) +{ + uint32_t i; + unsigned char *oid, *prev_oid, zero_oid[GIT_OID_MAX_SIZE] = {0}; + size_t oid_size; + + oid_size = git_oid_size(file->oid_type); + + if (chunk_oid_lookup->offset == 0) + return commit_graph_error("missing OID Lookup chunk"); + if (chunk_oid_lookup->length == 0) + return commit_graph_error("empty OID Lookup chunk"); + if (chunk_oid_lookup->length != file->num_commits * oid_size) + return commit_graph_error("OID Lookup chunk has wrong length"); + + file->oid_lookup = oid = (unsigned char *)(data + chunk_oid_lookup->offset); + prev_oid = zero_oid; + for (i = 0; i < file->num_commits; ++i, oid += oid_size) { + if (git_oid_raw_cmp(prev_oid, oid, oid_size) >= 0) + return commit_graph_error("OID Lookup index is non-monotonic"); + prev_oid = oid; + } + + return 0; +} + +static int commit_graph_parse_commit_data( + git_commit_graph_file *file, + const unsigned char *data, + struct git_commit_graph_chunk *chunk_commit_data) +{ + size_t oid_size = git_oid_size(file->oid_type); + + if (chunk_commit_data->offset == 0) + return commit_graph_error("missing Commit Data chunk"); + if (chunk_commit_data->length == 0) + return commit_graph_error("empty Commit Data chunk"); + if (chunk_commit_data->length != file->num_commits * (oid_size + 16)) + return commit_graph_error("Commit Data chunk has wrong length"); + + file->commit_data = data + chunk_commit_data->offset; + + return 0; +} + +static int commit_graph_parse_extra_edge_list( + git_commit_graph_file *file, + const unsigned char *data, + struct git_commit_graph_chunk *chunk_extra_edge_list) +{ + if (chunk_extra_edge_list->length == 0) + return 0; + if (chunk_extra_edge_list->length % 4 != 0) + return commit_graph_error("malformed Extra Edge List chunk"); + + file->extra_edge_list = data + chunk_extra_edge_list->offset; + file->num_extra_edge_list = chunk_extra_edge_list->length / 4; + + return 0; +} + +int git_commit_graph_file_parse( + git_commit_graph_file *file, + const unsigned char *data, + size_t size) +{ + struct git_commit_graph_header *hdr; + const unsigned char *chunk_hdr; + struct git_commit_graph_chunk *last_chunk; + uint32_t i; + uint64_t last_chunk_offset, chunk_offset, trailer_offset; + size_t checksum_size; + int error; + struct git_commit_graph_chunk chunk_oid_fanout = {0}, chunk_oid_lookup = {0}, + chunk_commit_data = {0}, chunk_extra_edge_list = {0}, + chunk_unsupported = {0}; + + GIT_ASSERT_ARG(file); + + checksum_size = git_oid_size(file->oid_type); + + if (size < sizeof(struct git_commit_graph_header) + checksum_size) + return commit_graph_error("commit-graph is too short"); + + hdr = ((struct git_commit_graph_header *)data); + + if (hdr->signature != htonl(COMMIT_GRAPH_SIGNATURE) || hdr->version != COMMIT_GRAPH_VERSION + || hdr->object_id_version != COMMIT_GRAPH_OBJECT_ID_VERSION) { + return commit_graph_error("unsupported commit-graph version"); + } + if (hdr->chunks == 0) + return commit_graph_error("no chunks in commit-graph"); + + /* + * The very first chunk's offset should be after the header, all the chunk + * headers, and a special zero chunk. + */ + last_chunk_offset = sizeof(struct git_commit_graph_header) + (1 + hdr->chunks) * 12; + trailer_offset = size - checksum_size; + + if (trailer_offset < last_chunk_offset) + return commit_graph_error("wrong commit-graph size"); + memcpy(file->checksum, (data + trailer_offset), checksum_size); + + chunk_hdr = data + sizeof(struct git_commit_graph_header); + last_chunk = NULL; + for (i = 0; i < hdr->chunks; ++i, chunk_hdr += 12) { + chunk_offset = ((uint64_t)ntohl(*((uint32_t *)(chunk_hdr + 4)))) << 32 + | ((uint64_t)ntohl(*((uint32_t *)(chunk_hdr + 8)))); + if (chunk_offset < last_chunk_offset) + return commit_graph_error("chunks are non-monotonic"); + if (chunk_offset >= trailer_offset) + return commit_graph_error("chunks extend beyond the trailer"); + if (last_chunk != NULL) + last_chunk->length = (size_t)(chunk_offset - last_chunk_offset); + last_chunk_offset = chunk_offset; + + switch (ntohl(*((uint32_t *)(chunk_hdr + 0)))) { + case COMMIT_GRAPH_OID_FANOUT_ID: + chunk_oid_fanout.offset = last_chunk_offset; + last_chunk = &chunk_oid_fanout; + break; + + case COMMIT_GRAPH_OID_LOOKUP_ID: + chunk_oid_lookup.offset = last_chunk_offset; + last_chunk = &chunk_oid_lookup; + break; + + case COMMIT_GRAPH_COMMIT_DATA_ID: + chunk_commit_data.offset = last_chunk_offset; + last_chunk = &chunk_commit_data; + break; + + case COMMIT_GRAPH_EXTRA_EDGE_LIST_ID: + chunk_extra_edge_list.offset = last_chunk_offset; + last_chunk = &chunk_extra_edge_list; + break; + + case COMMIT_GRAPH_BLOOM_FILTER_INDEX_ID: + case COMMIT_GRAPH_BLOOM_FILTER_DATA_ID: + chunk_unsupported.offset = last_chunk_offset; + last_chunk = &chunk_unsupported; + break; + + default: + return commit_graph_error("unrecognized chunk ID"); + } + } + last_chunk->length = (size_t)(trailer_offset - last_chunk_offset); + + error = commit_graph_parse_oid_fanout(file, data, &chunk_oid_fanout); + if (error < 0) + return error; + error = commit_graph_parse_oid_lookup(file, data, &chunk_oid_lookup); + if (error < 0) + return error; + error = commit_graph_parse_commit_data(file, data, &chunk_commit_data); + if (error < 0) + return error; + error = commit_graph_parse_extra_edge_list(file, data, &chunk_extra_edge_list); + if (error < 0) + return error; + + return 0; +} + +int git_commit_graph_new( + git_commit_graph **cgraph_out, + const char *objects_dir, + bool open_file, + git_oid_t oid_type) +{ + git_commit_graph *cgraph = NULL; + int error = 0; + + GIT_ASSERT_ARG(cgraph_out); + GIT_ASSERT_ARG(objects_dir); + GIT_ASSERT_ARG(oid_type); + + cgraph = git__calloc(1, sizeof(git_commit_graph)); + GIT_ERROR_CHECK_ALLOC(cgraph); + + cgraph->oid_type = oid_type; + + error = git_str_joinpath(&cgraph->filename, objects_dir, "info/commit-graph"); + if (error < 0) + goto error; + + if (open_file) { + error = git_commit_graph_file_open(&cgraph->file, + git_str_cstr(&cgraph->filename), oid_type); + + if (error < 0) + goto error; + + cgraph->checked = 1; + } + + *cgraph_out = cgraph; + return 0; + +error: + git_commit_graph_free(cgraph); + return error; +} + +int git_commit_graph_validate(git_commit_graph *cgraph) { + unsigned char checksum[GIT_HASH_MAX_SIZE]; + git_hash_algorithm_t checksum_type; + size_t checksum_size, trailer_offset; + + checksum_type = git_oid_algorithm(cgraph->oid_type); + checksum_size = git_hash_size(checksum_type); + trailer_offset = cgraph->file->graph_map.len - checksum_size; + + if (cgraph->file->graph_map.len < checksum_size) + return commit_graph_error("map length too small"); + + if (git_hash_buf(checksum, cgraph->file->graph_map.data, trailer_offset, checksum_type) < 0) + return commit_graph_error("could not calculate signature"); + if (memcmp(checksum, cgraph->file->checksum, checksum_size) != 0) + return commit_graph_error("index signature mismatch"); + + return 0; +} + +int git_commit_graph_open( + git_commit_graph **cgraph_out, + const char *objects_dir +#ifdef GIT_EXPERIMENTAL_SHA256 + , const git_commit_graph_open_options *opts +#endif + ) +{ + git_oid_t oid_type; + int error; + +#ifdef GIT_EXPERIMENTAL_SHA256 + GIT_ERROR_CHECK_VERSION(opts, + GIT_COMMIT_GRAPH_OPEN_OPTIONS_VERSION, + "git_commit_graph_open_options"); + + oid_type = opts && opts->oid_type ? opts->oid_type : GIT_OID_DEFAULT; + GIT_ASSERT_ARG(git_oid_type_is_valid(oid_type)); +#else + oid_type = GIT_OID_SHA1; +#endif + + error = git_commit_graph_new(cgraph_out, objects_dir, true, + oid_type); + + if (!error) + return git_commit_graph_validate(*cgraph_out); + + return error; +} + +int git_commit_graph_file_open( + git_commit_graph_file **file_out, + const char *path, + git_oid_t oid_type) +{ + git_commit_graph_file *file; + git_file fd = -1; + size_t cgraph_size; + struct stat st; + int error; + + /* TODO: properly open the file without access time using O_NOATIME */ + fd = git_futils_open_ro(path); + if (fd < 0) + return fd; + + if (p_fstat(fd, &st) < 0) { + p_close(fd); + git_error_set(GIT_ERROR_ODB, "commit-graph file not found - '%s'", path); + return GIT_ENOTFOUND; + } + + if (!S_ISREG(st.st_mode) || !git__is_sizet(st.st_size)) { + p_close(fd); + git_error_set(GIT_ERROR_ODB, "invalid pack index '%s'", path); + return GIT_ENOTFOUND; + } + cgraph_size = (size_t)st.st_size; + + file = git__calloc(1, sizeof(git_commit_graph_file)); + GIT_ERROR_CHECK_ALLOC(file); + + file->oid_type = oid_type; + + error = git_futils_mmap_ro(&file->graph_map, fd, 0, cgraph_size); + p_close(fd); + if (error < 0) { + git_commit_graph_file_free(file); + return error; + } + + if ((error = git_commit_graph_file_parse(file, file->graph_map.data, cgraph_size)) < 0) { + git_commit_graph_file_free(file); + return error; + } + + *file_out = file; + return 0; +} + +int git_commit_graph_get_file( + git_commit_graph_file **file_out, + git_commit_graph *cgraph) +{ + if (!cgraph->checked) { + int error = 0; + git_commit_graph_file *result = NULL; + + /* We only check once, no matter the result. */ + cgraph->checked = 1; + + /* Best effort */ + error = git_commit_graph_file_open(&result, + git_str_cstr(&cgraph->filename), cgraph->oid_type); + + if (error < 0) + return error; + + cgraph->file = result; + } + if (!cgraph->file) + return GIT_ENOTFOUND; + + *file_out = cgraph->file; + return 0; +} + +void git_commit_graph_refresh(git_commit_graph *cgraph) +{ + if (!cgraph->checked) + return; + + if (cgraph->file + && git_commit_graph_file_needs_refresh(cgraph->file, git_str_cstr(&cgraph->filename))) { + /* We just free the commit graph. The next time it is requested, it will be + * re-loaded. */ + git_commit_graph_file_free(cgraph->file); + cgraph->file = NULL; + } + /* Force a lazy re-check next time it is needed. */ + cgraph->checked = 0; +} + +static int git_commit_graph_entry_get_byindex( + git_commit_graph_entry *e, + const git_commit_graph_file *file, + size_t pos) +{ + const unsigned char *commit_data; + size_t oid_size = git_oid_size(file->oid_type); + + GIT_ASSERT_ARG(e); + GIT_ASSERT_ARG(file); + + if (pos >= file->num_commits) { + git_error_set(GIT_ERROR_INVALID, "commit index %zu does not exist", pos); + return GIT_ENOTFOUND; + } + + commit_data = file->commit_data + pos * (oid_size + 4 * sizeof(uint32_t)); + git_oid_from_raw(&e->tree_oid, commit_data, file->oid_type); + e->parent_indices[0] = ntohl(*((uint32_t *)(commit_data + oid_size))); + e->parent_indices[1] = ntohl( + *((uint32_t *)(commit_data + oid_size + sizeof(uint32_t)))); + e->parent_count = (e->parent_indices[0] != GIT_COMMIT_GRAPH_MISSING_PARENT) + + (e->parent_indices[1] != GIT_COMMIT_GRAPH_MISSING_PARENT); + e->generation = ntohl(*((uint32_t *)(commit_data + oid_size + 2 * sizeof(uint32_t)))); + e->commit_time = ntohl(*((uint32_t *)(commit_data + oid_size + 3 * sizeof(uint32_t)))); + + e->commit_time |= (e->generation & UINT64_C(0x3)) << UINT64_C(32); + e->generation >>= 2u; + if (e->parent_indices[1] & 0x80000000u) { + uint32_t extra_edge_list_pos = e->parent_indices[1] & 0x7fffffff; + + /* Make sure we're not being sent out of bounds */ + if (extra_edge_list_pos >= file->num_extra_edge_list) { + git_error_set(GIT_ERROR_INVALID, + "commit %u does not exist", + extra_edge_list_pos); + return GIT_ENOTFOUND; + } + + e->extra_parents_index = extra_edge_list_pos; + while (extra_edge_list_pos < file->num_extra_edge_list + && (ntohl(*( + (uint32_t *)(file->extra_edge_list + + extra_edge_list_pos * sizeof(uint32_t)))) + & 0x80000000u) + == 0) { + extra_edge_list_pos++; + e->parent_count++; + } + } + + git_oid_from_raw(&e->sha1, &file->oid_lookup[pos * oid_size], file->oid_type); + return 0; +} + +bool git_commit_graph_file_needs_refresh(const git_commit_graph_file *file, const char *path) +{ + git_file fd = -1; + struct stat st; + ssize_t bytes_read; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + size_t checksum_size = git_oid_size(file->oid_type); + + /* TODO: properly open the file without access time using O_NOATIME */ + fd = git_futils_open_ro(path); + if (fd < 0) + return true; + + if (p_fstat(fd, &st) < 0) { + p_close(fd); + return true; + } + + if (!S_ISREG(st.st_mode) || !git__is_sizet(st.st_size) + || (size_t)st.st_size != file->graph_map.len) { + p_close(fd); + return true; + } + + bytes_read = p_pread(fd, checksum, checksum_size, st.st_size - checksum_size); + p_close(fd); + if (bytes_read != (ssize_t)checksum_size) + return true; + + return (memcmp(checksum, file->checksum, checksum_size) != 0); +} + +int git_commit_graph_entry_find( + git_commit_graph_entry *e, + const git_commit_graph_file *file, + const git_oid *short_oid, + size_t len) +{ + int pos, found = 0; + uint32_t hi, lo; + const unsigned char *current = NULL; + size_t oid_size, oid_hexsize; + + GIT_ASSERT_ARG(e); + GIT_ASSERT_ARG(file); + GIT_ASSERT_ARG(short_oid); + + oid_size = git_oid_size(file->oid_type); + oid_hexsize = git_oid_hexsize(file->oid_type); + + hi = ntohl(file->oid_fanout[(int)short_oid->id[0]]); + lo = ((short_oid->id[0] == 0x0) ? 0 : ntohl(file->oid_fanout[(int)short_oid->id[0] - 1])); + + pos = git_pack__lookup_id(file->oid_lookup, oid_size, lo, hi, + short_oid->id, file->oid_type); + + if (pos >= 0) { + /* An object matching exactly the oid was found */ + found = 1; + current = file->oid_lookup + (pos * oid_size); + } else { + /* No object was found */ + /* pos refers to the object with the "closest" oid to short_oid */ + pos = -1 - pos; + if (pos < (int)file->num_commits) { + current = file->oid_lookup + (pos * oid_size); + + if (!git_oid_raw_ncmp(short_oid->id, current, len)) + found = 1; + } + } + + if (found && len != oid_hexsize && pos + 1 < (int)file->num_commits) { + /* Check for ambiguousity */ + const unsigned char *next = current + oid_size; + + if (!git_oid_raw_ncmp(short_oid->id, next, len)) + found = 2; + } + + if (!found) + return git_odb__error_notfound( + "failed to find offset for commit-graph index entry", short_oid, len); + if (found > 1) + return git_odb__error_ambiguous( + "found multiple offsets for commit-graph index entry"); + + return git_commit_graph_entry_get_byindex(e, file, pos); +} + +int git_commit_graph_entry_parent( + git_commit_graph_entry *parent, + const git_commit_graph_file *file, + const git_commit_graph_entry *entry, + size_t n) +{ + GIT_ASSERT_ARG(parent); + GIT_ASSERT_ARG(file); + + if (n >= entry->parent_count) { + git_error_set(GIT_ERROR_INVALID, "parent index %zu does not exist", n); + return GIT_ENOTFOUND; + } + + if (n == 0 || (n == 1 && entry->parent_count == 2)) + return git_commit_graph_entry_get_byindex(parent, file, entry->parent_indices[n]); + + return git_commit_graph_entry_get_byindex( + parent, + file, + ntohl( + *(uint32_t *)(file->extra_edge_list + + (entry->extra_parents_index + n - 1) + * sizeof(uint32_t))) + & 0x7fffffff); +} + +int git_commit_graph_file_close(git_commit_graph_file *file) +{ + GIT_ASSERT_ARG(file); + + if (file->graph_map.data) + git_futils_mmap_free(&file->graph_map); + + return 0; +} + +void git_commit_graph_free(git_commit_graph *cgraph) +{ + if (!cgraph) + return; + + git_str_dispose(&cgraph->filename); + git_commit_graph_file_free(cgraph->file); + git__free(cgraph); +} + +void git_commit_graph_file_free(git_commit_graph_file *file) +{ + if (!file) + return; + + git_commit_graph_file_close(file); + git__free(file); +} + +static int packed_commit__cmp(const void *a_, const void *b_) +{ + const struct packed_commit *a = a_; + const struct packed_commit *b = b_; + return git_oid_cmp(&a->sha1, &b->sha1); +} + +int git_commit_graph_writer_options_init( + git_commit_graph_writer_options *opts, + unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, + version, + git_commit_graph_writer_options, + GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT); + return 0; +} + +int git_commit_graph_writer_new( + git_commit_graph_writer **out, + const char *objects_info_dir, + const git_commit_graph_writer_options *opts + ) +{ + git_commit_graph_writer *w; + git_oid_t oid_type; + +#ifdef GIT_EXPERIMENTAL_SHA256 + GIT_ERROR_CHECK_VERSION(opts, + GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION, + "git_commit_graph_writer_options"); + + oid_type = opts && opts->oid_type ? opts->oid_type : GIT_OID_DEFAULT; + GIT_ASSERT_ARG(git_oid_type_is_valid(oid_type)); +#else + GIT_UNUSED(opts); + oid_type = GIT_OID_SHA1; +#endif + + GIT_ASSERT_ARG(out && objects_info_dir); + + w = git__calloc(1, sizeof(git_commit_graph_writer)); + GIT_ERROR_CHECK_ALLOC(w); + + w->oid_type = oid_type; + + if (git_str_sets(&w->objects_info_dir, objects_info_dir) < 0) { + git__free(w); + return -1; + } + + if (git_vector_init(&w->commits, 0, packed_commit__cmp) < 0) { + git_str_dispose(&w->objects_info_dir); + git__free(w); + return -1; + } + + *out = w; + return 0; +} + +void git_commit_graph_writer_free(git_commit_graph_writer *w) +{ + struct packed_commit *packed_commit; + size_t i; + + if (!w) + return; + + git_vector_foreach (&w->commits, i, packed_commit) + packed_commit_free(packed_commit); + git_vector_dispose(&w->commits); + git_str_dispose(&w->objects_info_dir); + git__free(w); +} + +struct object_entry_cb_state { + git_repository *repo; + git_odb *db; + git_vector *commits; +}; + +static int object_entry__cb(const git_oid *id, void *data) +{ + struct object_entry_cb_state *state = (struct object_entry_cb_state *)data; + git_commit *commit = NULL; + struct packed_commit *packed_commit = NULL; + size_t header_len; + git_object_t header_type; + int error = 0; + + error = git_odb_read_header(&header_len, &header_type, state->db, id); + if (error < 0) + return error; + + if (header_type != GIT_OBJECT_COMMIT) + return 0; + + error = git_commit_lookup(&commit, state->repo, id); + if (error < 0) + return error; + + packed_commit = packed_commit_new(commit); + git_commit_free(commit); + GIT_ERROR_CHECK_ALLOC(packed_commit); + + error = git_vector_insert(state->commits, packed_commit); + if (error < 0) { + packed_commit_free(packed_commit); + return error; + } + + return 0; +} + +int git_commit_graph_writer_add_index_file( + git_commit_graph_writer *w, + git_repository *repo, + const char *idx_path) +{ + int error; + struct git_pack_file *p = NULL; + struct object_entry_cb_state state = {0}; + state.repo = repo; + state.commits = &w->commits; + + error = git_repository_odb(&state.db, repo); + if (error < 0) + goto cleanup; + + error = git_mwindow_get_pack(&p, idx_path, repo->oid_type); + if (error < 0) + goto cleanup; + + error = git_pack_foreach_entry(p, object_entry__cb, &state); + if (error < 0) + goto cleanup; + +cleanup: + if (p) + git_mwindow_put_pack(p); + git_odb_free(state.db); + return error; +} + +int git_commit_graph_writer_add_revwalk(git_commit_graph_writer *w, git_revwalk *walk) +{ + int error; + git_oid id; + git_repository *repo = git_revwalk_repository(walk); + git_commit *commit; + struct packed_commit *packed_commit; + + while ((git_revwalk_next(&id, walk)) == 0) { + error = git_commit_lookup(&commit, repo, &id); + if (error < 0) + return error; + + packed_commit = packed_commit_new(commit); + git_commit_free(commit); + GIT_ERROR_CHECK_ALLOC(packed_commit); + + error = git_vector_insert(&w->commits, packed_commit); + if (error < 0) { + packed_commit_free(packed_commit); + return error; + } + } + + return 0; +} + +enum generation_number_commit_state { + GENERATION_NUMBER_COMMIT_STATE_UNVISITED = 0, + GENERATION_NUMBER_COMMIT_STATE_ADDED = 1, + GENERATION_NUMBER_COMMIT_STATE_EXPANDED = 2, + GENERATION_NUMBER_COMMIT_STATE_VISITED = 3 +}; + +GIT_HASHMAP_OID_SETUP(git_commit_graph_oidmap, struct packed_commit *); + +static int compute_generation_numbers(git_vector *commits) +{ + git_array_t(size_t) index_stack = GIT_ARRAY_INIT; + size_t i, j; + size_t *parent_idx; + enum generation_number_commit_state *commit_states = NULL; + struct packed_commit *child_packed_commit; + git_commit_graph_oidmap packed_commit_map = GIT_HASHMAP_INIT; + int error = 0; + + /* First populate the parent indices fields */ + git_vector_foreach (commits, i, child_packed_commit) { + child_packed_commit->index = i; + error = git_commit_graph_oidmap_put(&packed_commit_map, + &child_packed_commit->sha1, child_packed_commit); + if (error < 0) + goto cleanup; + } + + git_vector_foreach (commits, i, child_packed_commit) { + size_t parent_i, *parent_idx_ptr; + struct packed_commit *parent_packed_commit; + git_oid *parent_id; + git_array_init_to_size( + child_packed_commit->parent_indices, + git_array_size(child_packed_commit->parents)); + if (git_array_size(child_packed_commit->parents) + && !child_packed_commit->parent_indices.ptr) { + error = -1; + goto cleanup; + } + git_array_foreach (child_packed_commit->parents, parent_i, parent_id) { + if (git_commit_graph_oidmap_get(&parent_packed_commit, &packed_commit_map, parent_id) != 0) { + git_error_set(GIT_ERROR_ODB, + "parent commit %s not found in commit graph", + git_oid_tostr_s(parent_id)); + error = GIT_ENOTFOUND; + goto cleanup; + } + parent_idx_ptr = git_array_alloc(child_packed_commit->parent_indices); + if (!parent_idx_ptr) { + error = -1; + goto cleanup; + } + *parent_idx_ptr = parent_packed_commit->index; + } + } + + /* + * We copy all the commits to the stack and then during visitation, + * each node can be added up to two times to the stack. + */ + git_array_init_to_size(index_stack, 3 * git_vector_length(commits)); + if (!index_stack.ptr) { + error = -1; + goto cleanup; + } + + commit_states = (enum generation_number_commit_state *)git__calloc( + git_vector_length(commits), sizeof(enum generation_number_commit_state)); + if (!commit_states) { + error = -1; + goto cleanup; + } + + /* + * Perform a Post-Order traversal so that all parent nodes are fully + * visited before the child node. + */ + git_vector_foreach (commits, i, child_packed_commit) + *(size_t *)git_array_alloc(index_stack) = i; + + while (git_array_size(index_stack)) { + size_t *index_ptr = git_array_pop(index_stack); + i = *index_ptr; + child_packed_commit = git_vector_get(commits, i); + + if (commit_states[i] == GENERATION_NUMBER_COMMIT_STATE_VISITED) { + /* This commit has already been fully visited. */ + continue; + } + if (commit_states[i] == GENERATION_NUMBER_COMMIT_STATE_EXPANDED) { + /* All of the commits parents have been visited. */ + child_packed_commit->generation = 0; + git_array_foreach (child_packed_commit->parent_indices, j, parent_idx) { + struct packed_commit *parent = git_vector_get(commits, *parent_idx); + if (child_packed_commit->generation < parent->generation) + child_packed_commit->generation = parent->generation; + } + if (child_packed_commit->generation + < GIT_COMMIT_GRAPH_GENERATION_NUMBER_MAX) { + ++child_packed_commit->generation; + } + commit_states[i] = GENERATION_NUMBER_COMMIT_STATE_VISITED; + continue; + } + + /* + * This is the first time we see this commit. We need + * to visit all its parents before we can fully visit + * it. + */ + if (git_array_size(child_packed_commit->parent_indices) == 0) { + /* + * Special case: if the commit has no parents, there's + * no need to add it to the stack just to immediately + * remove it. + */ + commit_states[i] = GENERATION_NUMBER_COMMIT_STATE_VISITED; + child_packed_commit->generation = 1; + continue; + } + + /* + * Add this current commit again so that it is visited + * again once all its children have been visited. + */ + *(size_t *)git_array_alloc(index_stack) = i; + git_array_foreach (child_packed_commit->parent_indices, j, parent_idx) { + if (commit_states[*parent_idx] + != GENERATION_NUMBER_COMMIT_STATE_UNVISITED) { + /* This commit has already been considered. */ + continue; + } + + commit_states[*parent_idx] = GENERATION_NUMBER_COMMIT_STATE_ADDED; + *(size_t *)git_array_alloc(index_stack) = *parent_idx; + } + commit_states[i] = GENERATION_NUMBER_COMMIT_STATE_EXPANDED; + } + +cleanup: + git_commit_graph_oidmap_dispose(&packed_commit_map); + git__free(commit_states); + git_array_clear(index_stack); + + return error; +} + +static int write_offset(off64_t offset, commit_graph_write_cb write_cb, void *cb_data) +{ + int error; + uint32_t word; + + word = htonl((uint32_t)((offset >> 32) & 0xffffffffu)); + error = write_cb((const char *)&word, sizeof(word), cb_data); + if (error < 0) + return error; + word = htonl((uint32_t)((offset >> 0) & 0xffffffffu)); + error = write_cb((const char *)&word, sizeof(word), cb_data); + if (error < 0) + return error; + + return 0; +} + +static int write_chunk_header( + int chunk_id, + off64_t offset, + commit_graph_write_cb write_cb, + void *cb_data) +{ + uint32_t word = htonl(chunk_id); + int error = write_cb((const char *)&word, sizeof(word), cb_data); + if (error < 0) + return error; + return write_offset(offset, write_cb, cb_data); +} + +static int commit_graph_write_buf(const char *buf, size_t size, void *data) +{ + git_str *b = (git_str *)data; + return git_str_put(b, buf, size); +} + +struct commit_graph_write_hash_context { + commit_graph_write_cb write_cb; + void *cb_data; + git_hash_ctx *ctx; +}; + +static int commit_graph_write_hash(const char *buf, size_t size, void *data) +{ + struct commit_graph_write_hash_context *ctx = data; + int error; + + if (ctx->ctx) { + error = git_hash_update(ctx->ctx, buf, size); + + if (error < 0) + return error; + } + + return ctx->write_cb(buf, size, ctx->cb_data); +} + +static void packed_commit_free_dup(void *packed_commit) +{ + packed_commit_free(packed_commit); +} + +static int commit_graph_write( + git_commit_graph_writer *w, + commit_graph_write_cb write_cb, + void *cb_data) +{ + int error = 0; + size_t i; + struct packed_commit *packed_commit; + struct git_commit_graph_header hdr = {0}; + uint32_t oid_fanout_count; + uint32_t extra_edge_list_count; + uint32_t oid_fanout[256]; + off64_t offset; + git_str oid_lookup = GIT_STR_INIT, commit_data = GIT_STR_INIT, + extra_edge_list = GIT_STR_INIT; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + git_hash_algorithm_t checksum_type; + size_t checksum_size, oid_size; + git_hash_ctx ctx; + struct commit_graph_write_hash_context hash_cb_data = {0}; + + hdr.signature = htonl(COMMIT_GRAPH_SIGNATURE); + hdr.version = COMMIT_GRAPH_VERSION; + hdr.object_id_version = COMMIT_GRAPH_OBJECT_ID_VERSION; + hdr.chunks = 0; + hdr.base_graph_files = 0; + hash_cb_data.write_cb = write_cb; + hash_cb_data.cb_data = cb_data; + hash_cb_data.ctx = &ctx; + + oid_size = git_oid_size(w->oid_type); + checksum_type = git_oid_algorithm(w->oid_type); + checksum_size = git_hash_size(checksum_type); + + error = git_hash_ctx_init(&ctx, checksum_type); + if (error < 0) + return error; + cb_data = &hash_cb_data; + write_cb = commit_graph_write_hash; + + /* Sort the commits. */ + git_vector_sort(&w->commits); + git_vector_uniq(&w->commits, packed_commit_free_dup); + error = compute_generation_numbers(&w->commits); + if (error < 0) + goto cleanup; + + /* Fill the OID Fanout table. */ + oid_fanout_count = 0; + for (i = 0; i < 256; i++) { + while (oid_fanout_count < git_vector_length(&w->commits) && + (packed_commit = (struct packed_commit *)git_vector_get(&w->commits, oid_fanout_count)) && + packed_commit->sha1.id[0] <= i) + ++oid_fanout_count; + oid_fanout[i] = htonl(oid_fanout_count); + } + + /* Fill the OID Lookup table. */ + git_vector_foreach (&w->commits, i, packed_commit) { + error = git_str_put(&oid_lookup, + (const char *)&packed_commit->sha1.id, + oid_size); + + if (error < 0) + goto cleanup; + } + + /* Fill the Commit Data and Extra Edge List tables. */ + extra_edge_list_count = 0; + git_vector_foreach (&w->commits, i, packed_commit) { + uint64_t commit_time; + uint32_t generation; + uint32_t word; + size_t *packed_index; + unsigned int parentcount = (unsigned int)git_array_size(packed_commit->parents); + + error = git_str_put(&commit_data, + (const char *)&packed_commit->tree_oid.id, + oid_size); + + if (error < 0) + goto cleanup; + + if (parentcount == 0) { + word = htonl(GIT_COMMIT_GRAPH_MISSING_PARENT); + } else { + packed_index = git_array_get(packed_commit->parent_indices, 0); + word = htonl((uint32_t)*packed_index); + } + error = git_str_put(&commit_data, (const char *)&word, sizeof(word)); + if (error < 0) + goto cleanup; + + if (parentcount < 2) { + word = htonl(GIT_COMMIT_GRAPH_MISSING_PARENT); + } else if (parentcount == 2) { + packed_index = git_array_get(packed_commit->parent_indices, 1); + word = htonl((uint32_t)*packed_index); + } else { + word = htonl(0x80000000u | extra_edge_list_count); + } + error = git_str_put(&commit_data, (const char *)&word, sizeof(word)); + if (error < 0) + goto cleanup; + + if (parentcount > 2) { + unsigned int parent_i; + for (parent_i = 1; parent_i < parentcount; ++parent_i) { + packed_index = git_array_get( + packed_commit->parent_indices, parent_i); + word = htonl((uint32_t)(*packed_index | (parent_i + 1 == parentcount ? 0x80000000u : 0))); + + error = git_str_put(&extra_edge_list, + (const char *)&word, + sizeof(word)); + if (error < 0) + goto cleanup; + } + extra_edge_list_count += parentcount - 1; + } + + generation = packed_commit->generation; + commit_time = (uint64_t)packed_commit->commit_time; + if (generation > GIT_COMMIT_GRAPH_GENERATION_NUMBER_MAX) + generation = GIT_COMMIT_GRAPH_GENERATION_NUMBER_MAX; + word = ntohl((uint32_t)((generation << 2) | (((uint32_t)(commit_time >> 32)) & 0x3) )); + error = git_str_put(&commit_data, (const char *)&word, sizeof(word)); + if (error < 0) + goto cleanup; + word = ntohl((uint32_t)(commit_time & 0xfffffffful)); + error = git_str_put(&commit_data, (const char *)&word, sizeof(word)); + if (error < 0) + goto cleanup; + } + + /* Write the header. */ + hdr.chunks = 3; + if (git_str_len(&extra_edge_list) > 0) + hdr.chunks++; + error = write_cb((const char *)&hdr, sizeof(hdr), cb_data); + if (error < 0) + goto cleanup; + + /* Write the chunk headers. */ + offset = sizeof(hdr) + (hdr.chunks + 1) * 12; + error = write_chunk_header(COMMIT_GRAPH_OID_FANOUT_ID, offset, write_cb, cb_data); + if (error < 0) + goto cleanup; + offset += sizeof(oid_fanout); + error = write_chunk_header(COMMIT_GRAPH_OID_LOOKUP_ID, offset, write_cb, cb_data); + if (error < 0) + goto cleanup; + offset += git_str_len(&oid_lookup); + error = write_chunk_header(COMMIT_GRAPH_COMMIT_DATA_ID, offset, write_cb, cb_data); + if (error < 0) + goto cleanup; + offset += git_str_len(&commit_data); + if (git_str_len(&extra_edge_list) > 0) { + error = write_chunk_header( + COMMIT_GRAPH_EXTRA_EDGE_LIST_ID, offset, write_cb, cb_data); + if (error < 0) + goto cleanup; + offset += git_str_len(&extra_edge_list); + } + error = write_chunk_header(0, offset, write_cb, cb_data); + if (error < 0) + goto cleanup; + + /* Write all the chunks. */ + error = write_cb((const char *)oid_fanout, sizeof(oid_fanout), cb_data); + if (error < 0) + goto cleanup; + error = write_cb(git_str_cstr(&oid_lookup), git_str_len(&oid_lookup), cb_data); + if (error < 0) + goto cleanup; + error = write_cb(git_str_cstr(&commit_data), git_str_len(&commit_data), cb_data); + if (error < 0) + goto cleanup; + error = write_cb(git_str_cstr(&extra_edge_list), git_str_len(&extra_edge_list), cb_data); + if (error < 0) + goto cleanup; + + /* Finalize the checksum and write the trailer. */ + error = git_hash_final(checksum, &ctx); + if (error < 0) + goto cleanup; + + hash_cb_data.ctx = NULL; + + error = write_cb((char *)checksum, checksum_size, cb_data); + if (error < 0) + goto cleanup; + +cleanup: + git_str_dispose(&oid_lookup); + git_str_dispose(&commit_data); + git_str_dispose(&extra_edge_list); + git_hash_ctx_cleanup(&ctx); + return error; +} + +static int commit_graph_write_filebuf(const char *buf, size_t size, void *data) +{ + git_filebuf *f = (git_filebuf *)data; + return git_filebuf_write(f, buf, size); +} + +int git_commit_graph_writer_commit(git_commit_graph_writer *w) +{ + int error; + int filebuf_flags = GIT_FILEBUF_DO_NOT_BUFFER; + git_str commit_graph_path = GIT_STR_INIT; + git_filebuf output = GIT_FILEBUF_INIT; + + error = git_str_joinpath( + &commit_graph_path, git_str_cstr(&w->objects_info_dir), "commit-graph"); + if (error < 0) + return error; + + if (git_repository__fsync_gitdir) + filebuf_flags |= GIT_FILEBUF_FSYNC; + error = git_filebuf_open(&output, git_str_cstr(&commit_graph_path), filebuf_flags, 0644); + git_str_dispose(&commit_graph_path); + if (error < 0) + return error; + + error = commit_graph_write(w, commit_graph_write_filebuf, &output); + if (error < 0) { + git_filebuf_cleanup(&output); + return error; + } + + return git_filebuf_commit(&output); +} + +int git_commit_graph_writer_dump( + git_buf *cgraph, + git_commit_graph_writer *w) +{ + GIT_BUF_WRAP_PRIVATE(cgraph, git_commit_graph__writer_dump, w); +} + +int git_commit_graph__writer_dump( + git_str *cgraph, + git_commit_graph_writer *w) +{ + return commit_graph_write(w, commit_graph_write_buf, cgraph); +} diff --git a/src/libgit2/commit_graph.h b/src/libgit2/commit_graph.h new file mode 100644 index 00000000000..a06f3f86219 --- /dev/null +++ b/src/libgit2/commit_graph.h @@ -0,0 +1,187 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_commit_graph_h__ +#define INCLUDE_commit_graph_h__ + +#include "common.h" + +#include "git2/types.h" +#include "git2/sys/commit_graph.h" + +#include "map.h" +#include "vector.h" +#include "oid.h" +#include "hash.h" + +/** + * A commit-graph file. + * + * This file contains metadata about commits, particularly the generation + * number for each one. This can help speed up graph operations without + * requiring a full graph traversal. + * + * Support for this feature was added in git 2.19. + */ +typedef struct git_commit_graph_file { + git_map graph_map; + + /* The type of object IDs in the commit graph file. */ + git_oid_t oid_type; + + /* The OID Fanout table. */ + const uint32_t *oid_fanout; + /* The total number of commits in the graph. */ + uint32_t num_commits; + + /* The OID Lookup table. */ + unsigned char *oid_lookup; + + /* + * The Commit Data table. Each entry contains the OID of the commit followed + * by two 8-byte fields in network byte order: + * - The indices of the first two parents (32 bits each). + * - The generation number (first 30 bits) and commit time in seconds since + * UNIX epoch (34 bits). + */ + const unsigned char *commit_data; + + /* + * The Extra Edge List table. Each 4-byte entry is a network byte order index + * of one of the i-th (i > 0) parents of commits in the `commit_data` table, + * when the commit has more than 2 parents. + */ + const unsigned char *extra_edge_list; + /* The number of entries in the Extra Edge List table. Each entry is 4 bytes wide. */ + size_t num_extra_edge_list; + + /* The trailer of the file. Contains the SHA1-checksum of the whole file. */ + unsigned char checksum[GIT_HASH_SHA1_SIZE]; +} git_commit_graph_file; + +/** + * An entry in the commit-graph file. Provides a subset of the information that + * can be obtained from the commit header. + */ +typedef struct git_commit_graph_entry { + /* The generation number of the commit within the graph */ + size_t generation; + + /* Time in seconds from UNIX epoch. */ + git_time_t commit_time; + + /* The number of parents of the commit. */ + size_t parent_count; + + /* + * The indices of the parent commits within the Commit Data table. The value + * of `GIT_COMMIT_GRAPH_MISSING_PARENT` indicates that no parent is in that + * position. + */ + size_t parent_indices[2]; + + /* The index within the Extra Edge List of any parent after the first two. */ + size_t extra_parents_index; + + /* The object ID of the root tree of the commit. */ + git_oid tree_oid; + + /* The object ID hash of the requested commit. */ + git_oid sha1; +} git_commit_graph_entry; + +/* A wrapper for git_commit_graph_file to enable lazy loading in the ODB. */ +struct git_commit_graph { + /* The path to the commit-graph file. Something like ".git/objects/info/commit-graph". */ + git_str filename; + + /* The underlying commit-graph file. */ + git_commit_graph_file *file; + + /* The object ID types in the commit graph. */ + git_oid_t oid_type; + + /* Whether the commit-graph file was already checked for validity. */ + bool checked; +}; + +/** Create a new commit-graph, optionally opening the underlying file. */ +int git_commit_graph_new( + git_commit_graph **cgraph_out, + const char *objects_dir, + bool open_file, + git_oid_t oid_type); + +/** Validate the checksum of a commit graph */ +int git_commit_graph_validate(git_commit_graph *cgraph); + +/** Open and validate a commit-graph file. */ +int git_commit_graph_file_open( + git_commit_graph_file **file_out, + const char *path, + git_oid_t oid_type); + +/* + * Attempt to get the git_commit_graph's commit-graph file. This object is + * still owned by the git_commit_graph. If the repository does not contain a commit graph, + * it will return GIT_ENOTFOUND. + * + * This function is not thread-safe. + */ +int git_commit_graph_get_file(git_commit_graph_file **file_out, git_commit_graph *cgraph); + +/* Marks the commit-graph file as needing a refresh. */ +void git_commit_graph_refresh(git_commit_graph *cgraph); + +/* + * A writer for `commit-graph` files. + */ +struct git_commit_graph_writer { + /* + * The path of the `objects/info` directory where the `commit-graph` will be + * stored. + */ + git_str objects_info_dir; + + /* The object ID type of the commit graph. */ + git_oid_t oid_type; + + /* The list of packed commits. */ + git_vector commits; +}; + +int git_commit_graph__writer_dump( + git_str *cgraph, + git_commit_graph_writer *w); + +/* + * Returns whether the git_commit_graph_file needs to be reloaded since the + * contents of the commit-graph file have changed on disk. + */ +bool git_commit_graph_file_needs_refresh( + const git_commit_graph_file *file, const char *path); + +int git_commit_graph_entry_find( + git_commit_graph_entry *e, + const git_commit_graph_file *file, + const git_oid *short_oid, + size_t len); +int git_commit_graph_entry_parent( + git_commit_graph_entry *parent, + const git_commit_graph_file *file, + const git_commit_graph_entry *entry, + size_t n); +int git_commit_graph_file_close(git_commit_graph_file *cgraph); +void git_commit_graph_file_free(git_commit_graph_file *cgraph); + +/* This is exposed for use in the fuzzers. */ +int git_commit_graph_file_parse( + git_commit_graph_file *file, + const unsigned char *data, + size_t size); + +#endif diff --git a/src/libgit2/commit_list.c b/src/libgit2/commit_list.c new file mode 100644 index 00000000000..7f00c483fac --- /dev/null +++ b/src/libgit2/commit_list.c @@ -0,0 +1,219 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "commit_list.h" + +#include "revwalk.h" +#include "pool.h" +#include "odb.h" +#include "commit.h" + +int git_commit_list_generation_cmp(const void *a, const void *b) +{ + uint32_t generation_a = ((git_commit_list_node *) a)->generation; + uint32_t generation_b = ((git_commit_list_node *) b)->generation; + + if (!generation_a || !generation_b) { + /* Fall back to comparing by timestamps if at least one commit lacks a generation. */ + return git_commit_list_time_cmp(a, b); + } + + if (generation_a < generation_b) + return 1; + if (generation_a > generation_b) + return -1; + + return 0; +} + +int git_commit_list_time_cmp(const void *a, const void *b) +{ + int64_t time_a = ((git_commit_list_node *) a)->time; + int64_t time_b = ((git_commit_list_node *) b)->time; + + if (time_a < time_b) + return 1; + if (time_a > time_b) + return -1; + + return 0; +} + +git_commit_list *git_commit_list_create(git_commit_list_node *item, git_commit_list *next) { + git_commit_list *new_list = git__malloc(sizeof(git_commit_list)); + if (new_list != NULL) { + new_list->item = item; + new_list->next = next; + } + return new_list; +} + +git_commit_list *git_commit_list_insert(git_commit_list_node *item, git_commit_list **list_p) +{ + git_commit_list *new_list = git_commit_list_create(item, *list_p); + *list_p = new_list; + return new_list; +} + +git_commit_list *git_commit_list_insert_by_date(git_commit_list_node *item, git_commit_list **list_p) +{ + git_commit_list **pp = list_p; + git_commit_list *p; + + while ((p = *pp) != NULL) { + if (git_commit_list_time_cmp(p->item, item) > 0) + break; + + pp = &p->next; + } + + return git_commit_list_insert(item, pp); +} + +git_commit_list_node *git_commit_list_alloc_node(git_revwalk *walk) +{ + return (git_commit_list_node *)git_pool_mallocz(&walk->commit_pool, 1); +} + +static git_commit_list_node **alloc_parents( + git_revwalk *walk, git_commit_list_node *commit, size_t n_parents) +{ + size_t bytes; + + if (n_parents <= PARENTS_PER_COMMIT) + return (git_commit_list_node **)((char *)commit + sizeof(git_commit_list_node)); + + if (git__multiply_sizet_overflow(&bytes, n_parents, sizeof(git_commit_list_node *))) + return NULL; + + return (git_commit_list_node **)git_pool_malloc(&walk->commit_pool, bytes); +} + + +void git_commit_list_free(git_commit_list **list_p) +{ + git_commit_list *list = *list_p; + + if (list == NULL) + return; + + while (list) { + git_commit_list *temp = list; + list = temp->next; + git__free(temp); + } + + *list_p = NULL; +} + +git_commit_list_node *git_commit_list_pop(git_commit_list **stack) +{ + git_commit_list *top = *stack; + git_commit_list_node *item = top ? top->item : NULL; + + if (top) { + *stack = top->next; + git__free(top); + } + return item; +} + +static int commit_quick_parse( + git_revwalk *walk, + git_commit_list_node *node, + git_odb_object *obj) +{ + git_oid *parent_oid; + git_commit *commit; + git_commit__parse_options parse_opts = { + walk->repo->oid_type, + GIT_COMMIT_PARSE_QUICK + }; + size_t i; + + commit = git__calloc(1, sizeof(*commit)); + GIT_ERROR_CHECK_ALLOC(commit); + commit->object.repo = walk->repo; + + if (git_commit__parse_ext(commit, obj, &parse_opts) < 0) { + git__free(commit); + return -1; + } + + if (!git__is_uint16(git_array_size(commit->parent_ids))) { + git__free(commit); + git_error_set(GIT_ERROR_INVALID, "commit has more than 2^16 parents"); + return -1; + } + + node->generation = 0; + node->time = commit->committer->when.time; + node->out_degree = (uint16_t) git_array_size(commit->parent_ids); + node->parents = alloc_parents(walk, node, node->out_degree); + GIT_ERROR_CHECK_ALLOC(node->parents); + + git_array_foreach(commit->parent_ids, i, parent_oid) { + node->parents[i] = git_revwalk__commit_lookup(walk, parent_oid); + } + + git_commit__free(commit); + + node->parsed = 1; + + return 0; +} + +int git_commit_list_parse(git_revwalk *walk, git_commit_list_node *commit) +{ + git_odb_object *obj; + git_commit_graph_file *cgraph_file = NULL; + int error; + + if (commit->parsed) + return 0; + + /* Let's try to use the commit graph first. */ + git_odb__get_commit_graph_file(&cgraph_file, walk->odb); + if (cgraph_file) { + git_commit_graph_entry e; + + error = git_commit_graph_entry_find(&e, cgraph_file, + &commit->oid, git_oid_size(walk->repo->oid_type)); + + if (error == 0 && git__is_uint16(e.parent_count)) { + size_t i; + commit->generation = (uint32_t)e.generation; + commit->time = e.commit_time; + commit->out_degree = (uint16_t)e.parent_count; + commit->parents = alloc_parents(walk, commit, commit->out_degree); + GIT_ERROR_CHECK_ALLOC(commit->parents); + + for (i = 0; i < commit->out_degree; ++i) { + git_commit_graph_entry parent; + error = git_commit_graph_entry_parent(&parent, cgraph_file, &e, i); + if (error < 0) + return error; + commit->parents[i] = git_revwalk__commit_lookup(walk, &parent.sha1); + } + commit->parsed = 1; + return 0; + } + } + + if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < 0) + return error; + + if (obj->cached.type != GIT_OBJECT_COMMIT) { + git_error_set(GIT_ERROR_INVALID, "object is no commit object"); + error = -1; + } else + error = commit_quick_parse(walk, commit, obj); + + git_odb_object_free(obj); + return error; +} + diff --git a/src/commit_list.h b/src/libgit2/commit_list.h similarity index 75% rename from src/commit_list.h rename to src/libgit2/commit_list.h index d2f54b3ca65..e2dbd2aaed3 100644 --- a/src/commit_list.h +++ b/src/libgit2/commit_list.h @@ -7,28 +7,35 @@ #ifndef INCLUDE_commit_list_h__ #define INCLUDE_commit_list_h__ +#include "common.h" + #include "git2/oid.h" #define PARENT1 (1 << 0) #define PARENT2 (1 << 1) #define RESULT (1 << 2) #define STALE (1 << 3) +#define ALL_FLAGS (PARENT1 | PARENT2 | STALE | RESULT) #define PARENTS_PER_COMMIT 2 #define COMMIT_ALLOC \ (sizeof(git_commit_list_node) + PARENTS_PER_COMMIT * sizeof(git_commit_list_node *)) +#define FLAG_BITS 4 + typedef struct git_commit_list_node { git_oid oid; - uint32_t time; + int64_t time; + uint32_t generation; unsigned int seen:1, uninteresting:1, topo_delay:1, parsed:1, - flags : 4; + added:1, + flags : FLAG_BITS; - unsigned short in_degree; - unsigned short out_degree; + uint16_t in_degree; + uint16_t out_degree; struct git_commit_list_node **parents; } git_commit_list_node; @@ -39,8 +46,10 @@ typedef struct git_commit_list { } git_commit_list; git_commit_list_node *git_commit_list_alloc_node(git_revwalk *walk); -int git_commit_list_time_cmp(void *a, void *b); +int git_commit_list_generation_cmp(const void *a, const void *b); +int git_commit_list_time_cmp(const void *a, const void *b); void git_commit_list_free(git_commit_list **list_p); +git_commit_list *git_commit_list_create(git_commit_list_node *item, git_commit_list *next); git_commit_list *git_commit_list_insert(git_commit_list_node *item, git_commit_list **list_p); git_commit_list *git_commit_list_insert_by_date(git_commit_list_node *item, git_commit_list **list_p); int git_commit_list_parse(git_revwalk *walk, git_commit_list_node *commit); diff --git a/src/libgit2/common.h b/src/libgit2/common.h new file mode 100644 index 00000000000..bb9ec5ac1fe --- /dev/null +++ b/src/libgit2/common.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_common_h__ +#define INCLUDE_common_h__ + +#include "git2_util.h" +#include "errors.h" + +/* +* Include the declarations for deprecated functions; this ensures +* that they're decorated with the proper extern/visibility attributes. +*/ +#include "git2/deprecated.h" + +#include "posix.h" + +/** + * Initialize a structure with a version. + */ +GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int version) +{ + memset(structure, 0, len); + *((int*)structure) = version; +} +#define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V) + +#define GIT_INIT_STRUCTURE_FROM_TEMPLATE(PTR,VERSION,TYPE,TPL) do { \ + TYPE _tmpl = TPL; \ + GIT_ERROR_CHECK_VERSION(&(VERSION), _tmpl.version, #TYPE); \ + memcpy((PTR), &_tmpl, sizeof(_tmpl)); } while (0) + +/** + * Check a versioned structure for validity + */ +GIT_INLINE(int) git_error__check_version(const void *structure, unsigned int expected_max, const char *name) +{ + unsigned int actual; + + if (!structure) + return 0; + + actual = *(const unsigned int*)structure; + if (actual > 0 && actual <= expected_max) + return 0; + + git_error_set(GIT_ERROR_INVALID, "invalid version %d on %s", actual, name); + return -1; +} +#define GIT_ERROR_CHECK_VERSION(S,V,N) if (git_error__check_version(S,V,N) < 0) return -1 + +#endif diff --git a/src/libgit2/config.c b/src/libgit2/config.c new file mode 100644 index 00000000000..653142ce4e1 --- /dev/null +++ b/src/libgit2/config.c @@ -0,0 +1,1667 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "config.h" + +#include "git2/config.h" +#include "git2/sys/config.h" + +#include "buf.h" +#include "config_backend.h" +#include "regexp.h" +#include "sysdir.h" +#include "transaction.h" +#include "vector.h" +#if GIT_WIN32 +# include +#endif + +#include + +/* + * A refcounted instance of a config_backend that can be shared across + * a configuration instance, any snapshots, and individual configuration + * levels (from `git_config_open_level`). + */ +typedef struct { + git_refcount rc; + git_config_backend *backend; +} backend_instance; + +/* + * An entry in the readers or writers vector in the configuration. + * This is kept separate from the refcounted instance so that different + * views of the configuration can have different notions of levels or + * write orders. + * + * (eg, a standard configuration has a priority ordering of writers, a + * snapshot has *no* writers, and an individual level has a single + * writer.) + */ +typedef struct { + backend_instance *instance; + git_config_level_t level; + int write_order; +} backend_entry; + +void git_config_entry_free(git_config_entry *entry) +{ + git_config_backend_entry *be; + + if (!entry) + return; + + be = (git_config_backend_entry *)entry; + be->free(be); +} + +static void backend_instance_free(backend_instance *instance) +{ + git_config_backend *backend; + + backend = instance->backend; + backend->free(backend); + git__free(instance); +} + +static void config_free(git_config *config) +{ + size_t i; + backend_entry *entry; + + git_vector_foreach(&config->readers, i, entry) { + GIT_REFCOUNT_DEC(entry->instance, backend_instance_free); + git__free(entry); + } + + git_vector_dispose(&config->readers); + git_vector_dispose(&config->writers); + git__free(config); +} + +void git_config_free(git_config *config) +{ + if (config == NULL) + return; + + GIT_REFCOUNT_DEC(config, config_free); +} + +static int reader_cmp(const void *_a, const void *_b) +{ + const backend_entry *a = _a; + const backend_entry *b = _b; + + return b->level - a->level; +} + +static int writer_cmp(const void *_a, const void *_b) +{ + const backend_entry *a = _a; + const backend_entry *b = _b; + + return b->write_order - a->write_order; +} + +int git_config_new(git_config **out) +{ + git_config *config; + + config = git__calloc(1, sizeof(git_config)); + GIT_ERROR_CHECK_ALLOC(config); + + if (git_vector_init(&config->readers, 8, reader_cmp) < 0 || + git_vector_init(&config->writers, 8, writer_cmp) < 0) { + config_free(config); + return -1; + } + + GIT_REFCOUNT_INC(config); + + *out = config; + return 0; +} + +int git_config_add_file_ondisk( + git_config *config, + const char *path, + git_config_level_t level, + const git_repository *repo, + int force) +{ + git_config_backend *file = NULL; + struct stat st; + int res; + + GIT_ASSERT_ARG(config); + GIT_ASSERT_ARG(path); + + res = p_stat(path, &st); + if (res < 0 && errno != ENOENT && errno != ENOTDIR) { + git_error_set(GIT_ERROR_CONFIG, "failed to stat '%s'", path); + return -1; + } + + if (git_config_backend_from_file(&file, path) < 0) + return -1; + + if ((res = git_config_add_backend(config, file, level, repo, force)) < 0) { + /* + * free manually; the file is not owned by the config + * instance yet and will not be freed on cleanup + */ + file->free(file); + return res; + } + + return 0; +} + +int git_config_open_ondisk(git_config **out, const char *path) +{ + int error; + git_config *config; + + *out = NULL; + + if (git_config_new(&config) < 0) + return -1; + + if ((error = git_config_add_file_ondisk(config, path, GIT_CONFIG_LEVEL_LOCAL, NULL, 0)) < 0) + git_config_free(config); + else + *out = config; + + return error; +} + +int git_config_snapshot(git_config **out, git_config *in) +{ + int error = 0; + size_t i; + backend_entry *entry; + git_config *config; + + *out = NULL; + + if (git_config_new(&config) < 0) + return -1; + + git_vector_foreach(&in->readers, i, entry) { + git_config_backend *b; + + if ((error = entry->instance->backend->snapshot(&b, entry->instance->backend)) < 0) + break; + + if ((error = git_config_add_backend(config, b, entry->level, NULL, 0)) < 0) { + b->free(b); + break; + } + } + + git_config_set_writeorder(config, NULL, 0); + + if (error < 0) + git_config_free(config); + else + *out = config; + + return error; +} + +static int find_backend_by_level( + backend_instance **out, + const git_config *config, + git_config_level_t level) +{ + backend_entry *entry, *found = NULL; + size_t i; + + /* + * when passing GIT_CONFIG_HIGHEST_LEVEL, the idea is to get the + * config backend which has the highest level. As config backends + * are stored in a vector sorted by decreasing order of level, + * getting the backend at position 0 will do the job. + */ + if (level == GIT_CONFIG_HIGHEST_LEVEL) { + found = git_vector_get(&config->readers, 0); + } else { + git_vector_foreach(&config->readers, i, entry) { + if (entry->level == level) { + found = entry; + break; + } + } + } + + if (!found) { + git_error_set(GIT_ERROR_CONFIG, + "no configuration exists for the given level '%d'", level); + return GIT_ENOTFOUND; + } + + *out = found->instance; + return 0; +} + +static int duplicate_level(void **_old, void *_new) +{ + backend_entry **old = (backend_entry **)_old; + + GIT_UNUSED(_new); + + git_error_set(GIT_ERROR_CONFIG, "configuration at level %d already exists", (*old)->level); + return GIT_EEXISTS; +} + +static void try_remove_existing_backend( + git_config *config, + git_config_level_t level) +{ + backend_entry *entry, *found = NULL; + size_t i; + + git_vector_foreach(&config->readers, i, entry) { + if (entry->level == level) { + git_vector_remove(&config->readers, i); + found = entry; + break; + } + } + + if (!found) + return; + + git_vector_foreach(&config->writers, i, entry) { + if (entry->level == level) { + git_vector_remove(&config->writers, i); + break; + } + } + + GIT_REFCOUNT_DEC(found->instance, backend_instance_free); + git__free(found); +} + +static int git_config__add_instance( + git_config *config, + backend_instance *instance, + git_config_level_t level, + int force) +{ + backend_entry *entry; + int result; + + /* delete existing config backend for level if it exists */ + if (force) + try_remove_existing_backend(config, level); + + entry = git__malloc(sizeof(backend_entry)); + GIT_ERROR_CHECK_ALLOC(entry); + + entry->instance = instance; + entry->level = level; + entry->write_order = level; + + if ((result = git_vector_insert_sorted(&config->readers, + entry, &duplicate_level)) < 0 || + (result = git_vector_insert_sorted(&config->writers, + entry, NULL)) < 0) { + git__free(entry); + return result; + } + + GIT_REFCOUNT_INC(entry->instance); + + return 0; +} + +int git_config_open_global(git_config **out, git_config *config) +{ + int error; + + error = git_config_open_level(out, config, GIT_CONFIG_LEVEL_XDG); + + if (error == 0) + return 0; + else if (error != GIT_ENOTFOUND) + return error; + + return git_config_open_level(out, config, GIT_CONFIG_LEVEL_GLOBAL); +} + +int git_config_open_level( + git_config **out, + const git_config *parent, + git_config_level_t level) +{ + git_config *config; + backend_instance *instance; + int res; + + if ((res = find_backend_by_level(&instance, parent, level)) < 0) + return res; + + if ((res = git_config_new(&config)) < 0) + return res; + + if ((res = git_config__add_instance(config, instance, level, true)) < 0) { + git_config_free(config); + return res; + } + + *out = config; + + return 0; +} + +int git_config_add_backend( + git_config *config, + git_config_backend *backend, + git_config_level_t level, + const git_repository *repo, + int force) +{ + backend_instance *instance; + int result; + + GIT_ASSERT_ARG(config); + GIT_ASSERT_ARG(backend); + + GIT_ERROR_CHECK_VERSION(backend, GIT_CONFIG_BACKEND_VERSION, "git_config_backend"); + + if ((result = backend->open(backend, level, repo)) < 0) + return result; + + instance = git__calloc(1, sizeof(backend_instance)); + GIT_ERROR_CHECK_ALLOC(instance); + + instance->backend = backend; + instance->backend->cfg = config; + + if ((result = git_config__add_instance(config, instance, level, force)) < 0) { + git__free(instance); + return result; + } + + return 0; +} + +int git_config_set_writeorder( + git_config *config, + git_config_level_t *levels, + size_t len) +{ + backend_entry *entry; + size_t i, j; + + GIT_ASSERT(len < INT_MAX); + + git_vector_foreach(&config->readers, i, entry) { + bool found = false; + + for (j = 0; j < len; j++) { + if (levels[j] == entry->level) { + entry->write_order = (int)j; + found = true; + break; + } + } + + if (!found) + entry->write_order = -1; + } + + git_vector_sort(&config->writers); + + return 0; +} + +/* + * Loop over all the variables + */ + +typedef struct { + git_config_iterator parent; + git_config_iterator *current; + const git_config *config; + git_regexp regex; + size_t i; +} all_iter; + +static int all_iter_next( + git_config_backend_entry **out, + git_config_iterator *_iter) +{ + all_iter *iter = (all_iter *) _iter; + backend_entry *entry; + git_config_backend *backend; + git_config_backend_entry *be; + int error = 0; + + if (iter->current != NULL && + (error = iter->current->next(&be, iter->current)) == 0) { + *out = be; + return 0; + } + + if (error < 0 && error != GIT_ITEROVER) + return error; + + do { + if (iter->i == 0) + return GIT_ITEROVER; + + entry = git_vector_get(&iter->config->readers, iter->i - 1); + GIT_ASSERT(entry && entry->instance && entry->instance->backend); + + backend = entry->instance->backend; + iter->i--; + + if (iter->current) + iter->current->free(iter->current); + + iter->current = NULL; + error = backend->iterator(&iter->current, backend); + + if (error == GIT_ENOTFOUND) + continue; + + if (error < 0) + return error; + + if ((error = iter->current->next(&be, iter->current)) == 0) { + *out = be; + return 0; + } + + /* If this backend is empty, then keep going */ + if (error == GIT_ITEROVER) + continue; + + return error; + + } while(1); + + return GIT_ITEROVER; +} + +static int all_iter_glob_next( + git_config_backend_entry **entry, + git_config_iterator *_iter) +{ + int error; + all_iter *iter = (all_iter *) _iter; + + /* + * We use the "normal" function to grab the next one across + * readers and then apply the regex + */ + while ((error = all_iter_next(entry, _iter)) == 0) { + /* skip non-matching keys if regexp was provided */ + if (git_regexp_match(&iter->regex, (*entry)->entry.name) != 0) + continue; + + /* and simply return if we like the entry's name */ + return 0; + } + + return error; +} + +static void all_iter_free(git_config_iterator *_iter) +{ + all_iter *iter = (all_iter *) _iter; + + if (iter->current) + iter->current->free(iter->current); + + git__free(iter); +} + +static void all_iter_glob_free(git_config_iterator *_iter) +{ + all_iter *iter = (all_iter *) _iter; + + git_regexp_dispose(&iter->regex); + all_iter_free(_iter); +} + +int git_config_iterator_new(git_config_iterator **out, const git_config *config) +{ + all_iter *iter; + + iter = git__calloc(1, sizeof(all_iter)); + GIT_ERROR_CHECK_ALLOC(iter); + + iter->parent.free = all_iter_free; + iter->parent.next = all_iter_next; + + iter->i = config->readers.length; + iter->config = config; + + *out = (git_config_iterator *) iter; + + return 0; +} + +int git_config_iterator_glob_new(git_config_iterator **out, const git_config *config, const char *regexp) +{ + all_iter *iter; + int result; + + if (regexp == NULL) + return git_config_iterator_new(out, config); + + iter = git__calloc(1, sizeof(all_iter)); + GIT_ERROR_CHECK_ALLOC(iter); + + if ((result = git_regexp_compile(&iter->regex, regexp, 0)) < 0) { + git__free(iter); + return -1; + } + + iter->parent.next = all_iter_glob_next; + iter->parent.free = all_iter_glob_free; + iter->i = config->readers.length; + iter->config = config; + + *out = (git_config_iterator *) iter; + + return 0; +} + +int git_config_foreach( + const git_config *config, git_config_foreach_cb cb, void *payload) +{ + return git_config_foreach_match(config, NULL, cb, payload); +} + +int git_config_backend_foreach_match( + git_config_backend *backend, + const char *regexp, + git_config_foreach_cb cb, + void *payload) +{ + git_config_backend_entry *entry; + git_config_iterator *iter; + git_regexp regex; + int error = 0; + + GIT_ASSERT_ARG(backend); + GIT_ASSERT_ARG(cb); + + if (regexp && git_regexp_compile(®ex, regexp, 0) < 0) + return -1; + + if ((error = backend->iterator(&iter, backend)) < 0) { + iter = NULL; + return -1; + } + + while (!(iter->next(&entry, iter) < 0)) { + /* skip non-matching keys if regexp was provided */ + if (regexp && git_regexp_match(®ex, entry->entry.name) != 0) + continue; + + /* abort iterator on non-zero return value */ + if ((error = cb(&entry->entry, payload)) != 0) { + git_error_set_after_callback(error); + break; + } + } + + if (regexp != NULL) + git_regexp_dispose(®ex); + + iter->free(iter); + + return error; +} + +int git_config_foreach_match( + const git_config *config, + const char *regexp, + git_config_foreach_cb cb, + void *payload) +{ + int error; + git_config_iterator *iter; + git_config_entry *entry; + + if ((error = git_config_iterator_glob_new(&iter, config, regexp)) < 0) + return error; + + while (!(error = git_config_next(&entry, iter))) { + if ((error = cb(entry, payload)) != 0) { + git_error_set_after_callback(error); + break; + } + } + + git_config_iterator_free(iter); + + if (error == GIT_ITEROVER) + error = 0; + + return error; +} + +/************** + * Setters + **************/ + + static backend_instance *get_writer_instance(git_config *config) + { + backend_entry *entry; + size_t i; + + git_vector_foreach(&config->writers, i, entry) { + if (entry->instance->backend->readonly) + continue; + + if (entry->write_order < 0) + continue; + + return entry->instance; + } + + return NULL; + } + +static git_config_backend *get_writer(git_config *config) +{ + backend_instance *instance = get_writer_instance(config); + + return instance ? instance->backend : NULL; +} + +int git_config_delete_entry(git_config *config, const char *name) +{ + git_config_backend *backend; + + if ((backend = get_writer(config)) == NULL) + return GIT_EREADONLY; + + return backend->del(backend, name); +} + +int git_config_set_int64(git_config *config, const char *name, int64_t value) +{ + char str_value[32]; /* All numbers should fit in here */ + p_snprintf(str_value, sizeof(str_value), "%" PRId64, value); + return git_config_set_string(config, name, str_value); +} + +int git_config_set_int32(git_config *config, const char *name, int32_t value) +{ + return git_config_set_int64(config, name, (int64_t)value); +} + +int git_config_set_bool(git_config *config, const char *name, int value) +{ + return git_config_set_string(config, name, value ? "true" : "false"); +} + +int git_config_set_string(git_config *config, const char *name, const char *value) +{ + int error; + git_config_backend *backend; + + if (!value) { + git_error_set(GIT_ERROR_CONFIG, "the value to set cannot be NULL"); + return -1; + } + + if ((backend = get_writer(config)) == NULL) { + git_error_set(GIT_ERROR_CONFIG, "cannot set '%s': the configuration is read-only", name); + return GIT_EREADONLY; + } + + error = backend->set(backend, name, value); + + if (!error && GIT_REFCOUNT_OWNER(config) != NULL) + git_repository__configmap_lookup_cache_clear(GIT_REFCOUNT_OWNER(config)); + + return error; +} + +int git_config__update_entry( + git_config *config, + const char *key, + const char *value, + bool overwrite_existing, + bool only_if_existing) +{ + int error = 0; + git_config_entry *ce = NULL; + + if ((error = git_config__lookup_entry(&ce, config, key, false)) < 0) + return error; + + if (!ce && only_if_existing) /* entry doesn't exist */ + return 0; + if (ce && !overwrite_existing) /* entry would be overwritten */ + return 0; + if (value && ce && ce->value && !strcmp(ce->value, value)) /* no change */ + return 0; + if (!value && (!ce || !ce->value)) /* asked to delete absent entry */ + return 0; + + if (!value) + error = git_config_delete_entry(config, key); + else + error = git_config_set_string(config, key, value); + + git_config_entry_free(ce); + return error; +} + +/*********** + * Getters + ***********/ + +static int config_error_notfound(const char *name) +{ + git_error_set(GIT_ERROR_CONFIG, "config value '%s' was not found", name); + return GIT_ENOTFOUND; +} + +enum { + GET_ALL_ERRORS = 0, + GET_NO_MISSING = 1, + GET_NO_ERRORS = 2 +}; + +static int get_entry( + git_config_entry **out, + const git_config *config, + const char *name, + bool normalize_name, + int want_errors) +{ + backend_entry *entry; + git_config_backend *backend; + git_config_backend_entry *be; + int res = GIT_ENOTFOUND; + const char *key = name; + char *normalized = NULL; + size_t i; + + *out = NULL; + + if (normalize_name) { + if ((res = git_config__normalize_name(name, &normalized)) < 0) + goto cleanup; + key = normalized; + } + + res = GIT_ENOTFOUND; + git_vector_foreach(&config->readers, i, entry) { + GIT_ASSERT(entry->instance && entry->instance->backend); + + backend = entry->instance->backend; + res = backend->get(backend, key, &be); + + if (res != GIT_ENOTFOUND) { + *out = &be->entry; + break; + } + } + + git__free(normalized); + +cleanup: + if (res == GIT_ENOTFOUND) { + res = (want_errors > GET_ALL_ERRORS) ? 0 : config_error_notfound(name); + } else if (res && (want_errors == GET_NO_ERRORS)) { + git_error_clear(); + res = 0; + } + + return res; +} + +int git_config_get_entry( + git_config_entry **out, const git_config *config, const char *name) +{ + return get_entry(out, config, name, true, GET_ALL_ERRORS); +} + +int git_config__lookup_entry( + git_config_entry **out, + const git_config *config, + const char *key, + bool no_errors) +{ + return get_entry( + out, config, key, false, no_errors ? GET_NO_ERRORS : GET_NO_MISSING); +} + +int git_config_get_mapped( + int *out, + const git_config *config, + const char *name, + const git_configmap *maps, + size_t map_n) +{ + git_config_entry *entry; + int ret; + + if ((ret = get_entry(&entry, config, name, true, GET_ALL_ERRORS)) < 0) + return ret; + + ret = git_config_lookup_map_value(out, maps, map_n, entry->value); + git_config_entry_free(entry); + + return ret; +} + +int git_config_get_int64(int64_t *out, const git_config *config, const char *name) +{ + git_config_entry *entry; + int ret; + + if ((ret = get_entry(&entry, config, name, true, GET_ALL_ERRORS)) < 0) + return ret; + + ret = git_config_parse_int64(out, entry->value); + git_config_entry_free(entry); + + return ret; +} + +int git_config_get_int32(int32_t *out, const git_config *config, const char *name) +{ + git_config_entry *entry; + int ret; + + if ((ret = get_entry(&entry, config, name, true, GET_ALL_ERRORS)) < 0) + return ret; + + ret = git_config_parse_int32(out, entry->value); + git_config_entry_free(entry); + + return ret; +} + +int git_config_get_bool(int *out, const git_config *config, const char *name) +{ + git_config_entry *entry; + int ret; + + if ((ret = get_entry(&entry, config, name, true, GET_ALL_ERRORS)) < 0) + return ret; + + ret = git_config_parse_bool(out, entry->value); + git_config_entry_free(entry); + + return ret; +} + +static int is_readonly(const git_config *config) +{ + backend_entry *entry; + size_t i; + + git_vector_foreach(&config->writers, i, entry) { + GIT_ASSERT(entry->instance && entry->instance->backend); + + if (!entry->instance->backend->readonly) + return 0; + } + + return 1; +} + +static int git_config__parse_path(git_str *out, const char *value) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(value); + + if (value[0] == '~') { + if (value[1] != '\0' && value[1] != '/') { + git_error_set(GIT_ERROR_CONFIG, "retrieving a homedir by name is not supported"); + return -1; + } + + return git_sysdir_expand_homedir_file(out, value[1] ? &value[2] : NULL); + } + + return git_str_sets(out, value); +} + +int git_config_parse_path(git_buf *out, const char *value) +{ + GIT_BUF_WRAP_PRIVATE(out, git_config__parse_path, value); +} + +int git_config_get_path( + git_buf *out, + const git_config *config, + const char *name) +{ + GIT_BUF_WRAP_PRIVATE(out, git_config__get_path, config, name); +} + +int git_config__get_path( + git_str *out, + const git_config *config, + const char *name) +{ + git_config_entry *entry; + int error; + + if ((error = get_entry(&entry, config, name, true, GET_ALL_ERRORS)) < 0) + return error; + + error = git_config__parse_path(out, entry->value); + git_config_entry_free(entry); + + return error; +} + +int git_config_get_string( + const char **out, const git_config *config, const char *name) +{ + git_config_entry *entry; + int ret; + + if (!is_readonly(config)) { + git_error_set(GIT_ERROR_CONFIG, "get_string called on a live config object"); + return -1; + } + + ret = get_entry(&entry, config, name, true, GET_ALL_ERRORS); + *out = !ret ? (entry->value ? entry->value : "") : NULL; + + git_config_entry_free(entry); + + return ret; +} + +int git_config_get_string_buf( + git_buf *out, const git_config *config, const char *name) +{ + GIT_BUF_WRAP_PRIVATE(out, git_config__get_string_buf, config, name); +} + +int git_config__get_string_buf( + git_str *out, const git_config *config, const char *name) +{ + git_config_entry *entry; + int ret; + const char *str; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(config); + + ret = get_entry(&entry, config, name, true, GET_ALL_ERRORS); + str = !ret ? (entry->value ? entry->value : "") : NULL; + + if (str) + ret = git_str_puts(out, str); + + git_config_entry_free(entry); + + return ret; +} + +char *git_config__get_string_force( + const git_config *config, const char *key, const char *fallback_value) +{ + git_config_entry *entry; + char *ret; + + get_entry(&entry, config, key, false, GET_NO_ERRORS); + ret = (entry && entry->value) ? git__strdup(entry->value) : fallback_value ? git__strdup(fallback_value) : NULL; + git_config_entry_free(entry); + + return ret; +} + +int git_config__get_bool_force( + const git_config *config, const char *key, int fallback_value) +{ + int val = fallback_value; + git_config_entry *entry; + + get_entry(&entry, config, key, false, GET_NO_ERRORS); + + if (entry && git_config_parse_bool(&val, entry->value) < 0) + git_error_clear(); + + git_config_entry_free(entry); + return val; +} + +int git_config__get_int_force( + const git_config *config, const char *key, int fallback_value) +{ + int32_t val = (int32_t)fallback_value; + git_config_entry *entry; + + get_entry(&entry, config, key, false, GET_NO_ERRORS); + + if (entry && git_config_parse_int32(&val, entry->value) < 0) + git_error_clear(); + + git_config_entry_free(entry); + return (int)val; +} + +int git_config_get_multivar_foreach( + const git_config *config, const char *name, const char *regexp, + git_config_foreach_cb cb, void *payload) +{ + int err, found; + git_config_iterator *iter; + git_config_backend_entry *be; + + if ((err = git_config_multivar_iterator_new(&iter, config, name, regexp)) < 0) + return err; + + found = 0; + while ((err = iter->next(&be, iter)) == 0) { + found = 1; + + if ((err = cb(&be->entry, payload)) != 0) { + git_error_set_after_callback(err); + break; + } + } + + iter->free(iter); + if (err == GIT_ITEROVER) + err = 0; + + if (found == 0 && err == 0) + err = config_error_notfound(name); + + return err; +} + +typedef struct { + git_config_iterator parent; + git_config_iterator *iter; + char *name; + git_regexp regex; + int have_regex; +} multivar_iter; + +static int multivar_iter_next( + git_config_backend_entry **entry, + git_config_iterator *_iter) +{ + multivar_iter *iter = (multivar_iter *) _iter; + int error = 0; + + while ((error = iter->iter->next(entry, iter->iter)) == 0) { + if (git__strcmp(iter->name, (*entry)->entry.name)) + continue; + + if (!iter->have_regex) + return 0; + + if (git_regexp_match(&iter->regex, (*entry)->entry.value) == 0) + return 0; + } + + return error; +} + +static void multivar_iter_free(git_config_iterator *_iter) +{ + multivar_iter *iter = (multivar_iter *) _iter; + + iter->iter->free(iter->iter); + + git__free(iter->name); + if (iter->have_regex) + git_regexp_dispose(&iter->regex); + git__free(iter); +} + +int git_config_multivar_iterator_new(git_config_iterator **out, const git_config *config, const char *name, const char *regexp) +{ + multivar_iter *iter = NULL; + git_config_iterator *inner = NULL; + int error; + + if ((error = git_config_iterator_new(&inner, config)) < 0) + return error; + + iter = git__calloc(1, sizeof(multivar_iter)); + GIT_ERROR_CHECK_ALLOC(iter); + + if ((error = git_config__normalize_name(name, &iter->name)) < 0) + goto on_error; + + if (regexp != NULL) { + if ((error = git_regexp_compile(&iter->regex, regexp, 0)) < 0) + goto on_error; + + iter->have_regex = 1; + } + + iter->iter = inner; + iter->parent.free = multivar_iter_free; + iter->parent.next = multivar_iter_next; + + *out = (git_config_iterator *) iter; + + return 0; + +on_error: + + inner->free(inner); + git__free(iter); + return error; +} + +int git_config_set_multivar(git_config *config, const char *name, const char *regexp, const char *value) +{ + git_config_backend *backend; + + if ((backend = get_writer(config)) == NULL) { + git_error_set(GIT_ERROR_CONFIG, "cannot set '%s': the configuration is read-only", name); + return GIT_EREADONLY; + } + + return backend->set_multivar(backend, name, regexp, value); +} + +int git_config_delete_multivar(git_config *config, const char *name, const char *regexp) +{ + git_config_backend *backend; + + if ((backend = get_writer(config)) == NULL) + return GIT_EREADONLY; + + return backend->del_multivar(backend, name, regexp); +} + +int git_config_next(git_config_entry **entry, git_config_iterator *iter) +{ + git_config_backend_entry *be; + int error; + + if ((error = iter->next(&be, iter)) != 0) + return error; + + *entry = &be->entry; + return 0; +} + +void git_config_iterator_free(git_config_iterator *iter) +{ + if (iter == NULL) + return; + + iter->free(iter); +} + +int git_config_find_global(git_buf *path) +{ + GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_global_file, GIT_CONFIG_FILENAME_GLOBAL); +} + +int git_config__find_global(git_str *path) +{ + return git_sysdir_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL); +} + +int git_config_find_xdg(git_buf *path) +{ + GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_xdg_file, GIT_CONFIG_FILENAME_XDG); +} + +int git_config__find_xdg(git_str *path) +{ + return git_sysdir_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG); +} + +int git_config_find_system(git_buf *path) +{ + GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_system_file, GIT_CONFIG_FILENAME_SYSTEM); +} + +int git_config__find_system(git_str *path) +{ + return git_sysdir_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM); +} + +int git_config_find_programdata(git_buf *path) +{ + git_str str = GIT_STR_INIT; + int error; + + if ((error = git_buf_tostr(&str, path)) == 0 && + (error = git_config__find_programdata(&str)) == 0) + error = git_buf_fromstr(path, &str); + + git_str_dispose(&str); + return error; +} + +int git_config__find_programdata(git_str *path) +{ + git_fs_path_owner_t owner_level = + GIT_FS_PATH_OWNER_CURRENT_USER | + GIT_FS_PATH_OWNER_ADMINISTRATOR; + bool is_safe; + int error; + + if ((error = git_sysdir_find_programdata_file(path, GIT_CONFIG_FILENAME_PROGRAMDATA)) < 0) + return error; + + if (git_fs_path_owner_is(&is_safe, path->ptr, owner_level) < 0) + return -1; + + if (!is_safe) { + git_error_set(GIT_ERROR_CONFIG, "programdata path has invalid ownership"); + return -1; + } + + return 0; +} + +int git_config__global_location(git_str *buf) +{ + const git_str *paths; + const char *sep, *start; + + if (git_sysdir_get(&paths, GIT_SYSDIR_GLOBAL) < 0) + return -1; + + /* no paths, so give up */ + if (!paths || !git_str_len(paths)) + return -1; + + /* find unescaped separator or end of string */ + for (sep = start = git_str_cstr(paths); *sep; ++sep) { + if (*sep == GIT_PATH_LIST_SEPARATOR && + (sep <= start || sep[-1] != '\\')) + break; + } + + if (git_str_set(buf, start, (size_t)(sep - start)) < 0) + return -1; + + return git_str_joinpath(buf, buf->ptr, GIT_CONFIG_FILENAME_GLOBAL); +} + +int git_config_open_default(git_config **out) +{ + int error; + git_config *config = NULL; + git_str buf = GIT_STR_INIT; + + if ((error = git_config_new(&config)) < 0) + return error; + + if (!git_config__find_global(&buf) || + !git_config__global_location(&buf)) { + error = git_config_add_file_ondisk(config, buf.ptr, + GIT_CONFIG_LEVEL_GLOBAL, NULL, 0); + } + + if (!error && !git_config__find_xdg(&buf)) + error = git_config_add_file_ondisk(config, buf.ptr, + GIT_CONFIG_LEVEL_XDG, NULL, 0); + + if (!error && !git_config__find_system(&buf)) + error = git_config_add_file_ondisk(config, buf.ptr, + GIT_CONFIG_LEVEL_SYSTEM, NULL, 0); + + if (!error && !git_config__find_programdata(&buf)) + error = git_config_add_file_ondisk(config, buf.ptr, + GIT_CONFIG_LEVEL_PROGRAMDATA, NULL, 0); + + git_str_dispose(&buf); + + if (error) { + git_config_free(config); + config = NULL; + } + + *out = config; + + return error; +} + +int git_config_lock(git_transaction **out, git_config *config) +{ + backend_instance *instance; + int error; + + GIT_ASSERT_ARG(config); + + if ((instance = get_writer_instance(config)) == NULL) { + git_error_set(GIT_ERROR_CONFIG, "cannot lock: the configuration is read-only"); + return GIT_EREADONLY; + } + + if ((error = instance->backend->lock(instance->backend)) < 0 || + (error = git_transaction_config_new(out, config, instance)) < 0) + return error; + + GIT_REFCOUNT_INC(instance); + return 0; +} + +int git_config_unlock( + git_config *config, + void *data, + int commit) +{ + backend_instance *instance = data; + int error; + + GIT_ASSERT_ARG(config && data); + GIT_UNUSED(config); + + error = instance->backend->unlock(instance->backend, commit); + GIT_REFCOUNT_DEC(instance, backend_instance_free); + + return error; +} + +/*********** + * Parsers + ***********/ + +int git_config_lookup_map_value( + int *out, + const git_configmap *maps, + size_t map_n, + const char *value) +{ + size_t i; + + for (i = 0; i < map_n; ++i) { + const git_configmap *m = maps + i; + + switch (m->type) { + case GIT_CONFIGMAP_FALSE: + case GIT_CONFIGMAP_TRUE: { + int bool_val; + + if (git_config_parse_bool(&bool_val, value) == 0 && + bool_val == (int)m->type) { + *out = m->map_value; + return 0; + } + break; + } + + case GIT_CONFIGMAP_INT32: + if (git_config_parse_int32(out, value) == 0) + return 0; + break; + + case GIT_CONFIGMAP_STRING: + if (value && strcasecmp(value, m->str_match) == 0) { + *out = m->map_value; + return 0; + } + break; + } + } + + git_error_set(GIT_ERROR_CONFIG, "failed to map '%s'", value); + return -1; +} + +int git_config_lookup_map_enum(git_configmap_t *type_out, const char **str_out, + const git_configmap *maps, size_t map_n, int enum_val) +{ + size_t i; + + for (i = 0; i < map_n; i++) { + const git_configmap *m = &maps[i]; + + if (m->map_value != enum_val) + continue; + + *type_out = m->type; + *str_out = m->str_match; + return 0; + } + + git_error_set(GIT_ERROR_CONFIG, "invalid enum value"); + return GIT_ENOTFOUND; +} + +int git_config_parse_bool(int *out, const char *value) +{ + if (git__parse_bool(out, value) == 0) + return 0; + + if (git_config_parse_int32(out, value) == 0) { + *out = !!(*out); + return 0; + } + + git_error_set(GIT_ERROR_CONFIG, "failed to parse '%s' as a boolean", value ? value : "(null)"); + return -1; +} + +int git_config_parse_int64(int64_t *out, const char *value) +{ + const char *num_end; + int64_t num; + + if (!value || git__strntol64(&num, value, strlen(value), &num_end, 0) < 0) + goto fail_parse; + + switch (*num_end) { + case 'g': + case 'G': + num *= 1024; + /* fallthrough */ + + case 'm': + case 'M': + num *= 1024; + /* fallthrough */ + + case 'k': + case 'K': + num *= 1024; + + /* check that that there are no more characters after the + * given modifier suffix */ + if (num_end[1] != '\0') + return -1; + + /* fallthrough */ + + case '\0': + *out = num; + return 0; + + default: + goto fail_parse; + } + +fail_parse: + git_error_set(GIT_ERROR_CONFIG, "failed to parse '%s' as an integer", value ? value : "(null)"); + return -1; +} + +int git_config_parse_int32(int32_t *out, const char *value) +{ + int64_t tmp; + int32_t truncate; + + if (git_config_parse_int64(&tmp, value) < 0) + goto fail_parse; + + truncate = tmp & 0xFFFFFFFF; + if (truncate != tmp) + goto fail_parse; + + *out = truncate; + return 0; + +fail_parse: + git_error_set(GIT_ERROR_CONFIG, "failed to parse '%s' as a 32-bit integer", value ? value : "(null)"); + return -1; +} + +static int normalize_section(char *start, char *end) +{ + char *scan; + + if (start == end) + return GIT_EINVALIDSPEC; + + /* Validate and downcase range */ + for (scan = start; *scan; ++scan) { + if (end && scan >= end) + break; + if (git__isalnum(*scan)) + *scan = (char)git__tolower(*scan); + else if (*scan != '-' || scan == start) + return GIT_EINVALIDSPEC; + } + + if (scan == start) + return GIT_EINVALIDSPEC; + + return 0; +} + + +/* Take something the user gave us and make it nice for our hash function */ +int git_config__normalize_name(const char *in, char **out) +{ + char *name, *fdot, *ldot; + + GIT_ASSERT_ARG(in); + GIT_ASSERT_ARG(out); + + name = git__strdup(in); + GIT_ERROR_CHECK_ALLOC(name); + + fdot = strchr(name, '.'); + ldot = strrchr(name, '.'); + + if (fdot == NULL || fdot == name || ldot == NULL || !ldot[1]) + goto invalid; + + /* Validate and downcase up to first dot and after last dot */ + if (normalize_section(name, fdot) < 0 || + normalize_section(ldot + 1, NULL) < 0) + goto invalid; + + /* If there is a middle range, make sure it doesn't have newlines */ + while (fdot < ldot) + if (*fdot++ == '\n') + goto invalid; + + *out = name; + return 0; + +invalid: + git__free(name); + git_error_set(GIT_ERROR_CONFIG, "invalid config item name '%s'", in); + return GIT_EINVALIDSPEC; +} + +struct rename_data { + git_config *config; + git_str *name; + size_t old_len; +}; + +static int rename_config_entries_cb( + const git_config_entry *entry, + void *payload) +{ + int error = 0; + struct rename_data *data = (struct rename_data *)payload; + size_t base_len = git_str_len(data->name); + git_str value = GIT_STR_INIT; + + if (base_len > 0) { + if ((error = git_str_puts(data->name, + entry->name + data->old_len)) < 0 || + (error = git_config_set_multivar( + data->config, git_str_cstr(data->name), "^$", + entry->value)) < 0) + goto cleanup; + } + + git_str_putc(&value, '^'); + git_str_puts_escape_regex(&value, entry->value); + git_str_putc(&value, '$'); + + if (git_str_oom(&value)) { + error = -1; + goto cleanup; + } + + error = git_config_delete_multivar( + data->config, entry->name, git_str_cstr(&value)); + + cleanup: + git_str_truncate(data->name, base_len); + git_str_dispose(&value); + return error; +} + +int git_config_rename_section( + git_repository *repo, + const char *old_section_name, + const char *new_section_name) +{ + git_config *config; + git_str pattern = GIT_STR_INIT, replace = GIT_STR_INIT; + int error = 0; + struct rename_data data; + + git_str_puts_escape_regex(&pattern, old_section_name); + + if ((error = git_str_puts(&pattern, "\\..+")) < 0) + goto cleanup; + + if ((error = git_repository_config__weakptr(&config, repo)) < 0) + goto cleanup; + + data.config = config; + data.name = &replace; + data.old_len = strlen(old_section_name) + 1; + + if ((error = git_str_join(&replace, '.', new_section_name, "")) < 0) + goto cleanup; + + if (new_section_name != NULL && + (error = normalize_section(replace.ptr, strchr(replace.ptr, '.'))) < 0) + { + git_error_set( + GIT_ERROR_CONFIG, "invalid config section '%s'", new_section_name); + goto cleanup; + } + + error = git_config_foreach_match( + config, git_str_cstr(&pattern), rename_config_entries_cb, &data); + +cleanup: + git_str_dispose(&pattern); + git_str_dispose(&replace); + + return error; +} + +int git_config_init_backend(git_config_backend *backend, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + backend, version, git_config_backend, GIT_CONFIG_BACKEND_INIT); + return 0; +} diff --git a/src/libgit2/config.cmake.in b/src/libgit2/config.cmake.in new file mode 100644 index 00000000000..6d15e05882f --- /dev/null +++ b/src/libgit2/config.cmake.in @@ -0,0 +1,3 @@ +include(CMakeFindDependencyMacro) + +include("${CMAKE_CURRENT_LIST_DIR}/@LIBGIT2_TARGETS_EXPORT_NAME@.cmake") \ No newline at end of file diff --git a/src/libgit2/config.h b/src/libgit2/config.h new file mode 100644 index 00000000000..5003cbf9c1f --- /dev/null +++ b/src/libgit2/config.h @@ -0,0 +1,115 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_config_h__ +#define INCLUDE_config_h__ + +#include "common.h" + +#include "git2.h" +#include "git2/config.h" +#include "vector.h" +#include "repository.h" + +#define GIT_CONFIG_FILENAME_PROGRAMDATA "config" +#define GIT_CONFIG_FILENAME_SYSTEM "gitconfig" +#define GIT_CONFIG_FILENAME_GLOBAL ".gitconfig" +#define GIT_CONFIG_FILENAME_XDG "config" + +#define GIT_CONFIG_FILENAME_INREPO "config" +#define GIT_CONFIG_FILE_MODE 0666 + +struct git_config { + git_refcount rc; + git_vector readers; + git_vector writers; +}; + +extern int git_config__global_location(git_str *buf); + +extern int git_config__find_global(git_str *path); +extern int git_config__find_xdg(git_str *path); +extern int git_config__find_system(git_str *path); +extern int git_config__find_programdata(git_str *path); + +extern int git_config_rename_section( + git_repository *repo, + const char *old_section_name, /* eg "branch.dummy" */ + const char *new_section_name); /* NULL to drop the old section */ + +extern int git_config__normalize_name(const char *in, char **out); + +/* internal only: does not normalize key and sets out to NULL if not found */ +extern int git_config__lookup_entry( + git_config_entry **out, + const git_config *cfg, + const char *key, + bool no_errors); + +/* internal only: update and/or delete entry string with constraints */ +extern int git_config__update_entry( + git_config *cfg, + const char *key, + const char *value, + bool overwrite_existing, + bool only_if_existing); + +int git_config__get_path( + git_str *out, + const git_config *cfg, + const char *name); + +int git_config__get_string_buf( + git_str *out, const git_config *cfg, const char *name); + +/* + * Lookup functions that cannot fail. These functions look up a config + * value and return a fallback value if the value is missing or if any + * failures occur while trying to access the value. + */ + +extern char *git_config__get_string_force( + const git_config *cfg, const char *key, const char *fallback_value); + +extern int git_config__get_bool_force( + const git_config *cfg, const char *key, int fallback_value); + +extern int git_config__get_int_force( + const git_config *cfg, const char *key, int fallback_value); + +/* API for repository configmap-style lookups from config - not cached, but + * uses configmap value maps and fallbacks + */ +extern int git_config__configmap_lookup( + int *out, git_config *config, git_configmap_item item); + +/** + * The opposite of git_config_lookup_map_value, we take an enum value + * and map it to the string or bool value on the config. + */ +int git_config_lookup_map_enum(git_configmap_t *type_out, + const char **str_out, const git_configmap *maps, + size_t map_n, int enum_val); + +/** + * Unlock the given backend that was previously locked. + * + * Unlocking will allow other writers to update the configuration + * file. Optionally, any changes performed since the lock will be + * applied to the configuration. + * + * @param config the config instance + * @param data the config data passed to git_transaction_new + * @param commit boolean which indicates whether to commit any changes + * done since locking + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_unlock( + git_config *config, + void *data, + int commit); + +#endif diff --git a/src/libgit2/config_backend.h b/src/libgit2/config_backend.h new file mode 100644 index 00000000000..786c5de1a75 --- /dev/null +++ b/src/libgit2/config_backend.h @@ -0,0 +1,94 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_config_file_h__ +#define INCLUDE_config_file_h__ + +#include "common.h" + +#include "git2/sys/config.h" +#include "git2/config.h" + +/** + * Create a configuration file backend for ondisk files + * + * These are the normal `.gitconfig` files that Core Git + * processes. Note that you first have to add this file to a + * configuration object before you can query it for configuration + * variables. + * + * @param out the new backend + * @param path where the config file is located + */ +extern int git_config_backend_from_file(git_config_backend **out, const char *path); + +/** + * Create a readonly configuration file backend from another backend + * + * This copies the complete contents of the source backend to the + * new backend. The new backend will be completely read-only and + * cannot be modified. + * + * @param out the new snapshotted backend + * @param source the backend to copy + */ +extern int git_config_backend_snapshot(git_config_backend **out, git_config_backend *source); + +GIT_INLINE(int) git_config_backend_open(git_config_backend *cfg, unsigned int level, const git_repository *repo) +{ + return cfg->open(cfg, level, repo); +} + +GIT_INLINE(void) git_config_backend_free(git_config_backend *cfg) +{ + if (cfg) + cfg->free(cfg); +} + +GIT_INLINE(int) git_config_backend_get_string( + git_config_entry **out, git_config_backend *cfg, const char *name) +{ + git_config_backend_entry *be; + int error; + + if ((error = cfg->get(cfg, name, &be)) < 0) + return error; + + *out = &be->entry; + return 0; +} + +GIT_INLINE(int) git_config_backend_set_string( + git_config_backend *cfg, const char *name, const char *value) +{ + return cfg->set(cfg, name, value); +} + +GIT_INLINE(int) git_config_backend_delete( + git_config_backend *cfg, const char *name) +{ + return cfg->del(cfg, name); +} + +GIT_INLINE(int) git_config_backend_foreach( + git_config_backend *cfg, + int (*fn)(const git_config_entry *entry, void *data), + void *data) +{ + return git_config_backend_foreach_match(cfg, NULL, fn, data); +} + +GIT_INLINE(int) git_config_backend_lock(git_config_backend *cfg) +{ + return cfg->lock(cfg); +} + +GIT_INLINE(int) git_config_backend_unlock(git_config_backend *cfg, int success) +{ + return cfg->unlock(cfg, success); +} + +#endif diff --git a/src/libgit2/config_cache.c b/src/libgit2/config_cache.c new file mode 100644 index 00000000000..05d9d5828e0 --- /dev/null +++ b/src/libgit2/config_cache.c @@ -0,0 +1,141 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "futils.h" +#include "repository.h" +#include "config.h" +#include "git2/config.h" +#include "vector.h" +#include "filter.h" + +struct map_data { + const char *name; + git_configmap *maps; + size_t map_count; + int default_value; +}; + +/* + * core.eol + * Sets the line ending type to use in the working directory for + * files that have the text property set. Alternatives are lf, crlf + * and native, which uses the platform's native line ending. The default + * value is native. See gitattributes(5) for more information on + * end-of-line conversion. + */ +static git_configmap _configmap_eol[] = { + {GIT_CONFIGMAP_FALSE, NULL, GIT_EOL_UNSET}, + {GIT_CONFIGMAP_STRING, "lf", GIT_EOL_LF}, + {GIT_CONFIGMAP_STRING, "crlf", GIT_EOL_CRLF}, + {GIT_CONFIGMAP_STRING, "native", GIT_EOL_NATIVE} +}; + +/* + * core.autocrlf + * Setting this variable to "true" is almost the same as setting + * the text attribute to "auto" on all files except that text files are + * not guaranteed to be normalized: files that contain CRLF in the + * repository will not be touched. Use this setting if you want to have + * CRLF line endings in your working directory even though the repository + * does not have normalized line endings. This variable can be set to input, + * in which case no output conversion is performed. + */ +static git_configmap _configmap_autocrlf[] = { + {GIT_CONFIGMAP_FALSE, NULL, GIT_AUTO_CRLF_FALSE}, + {GIT_CONFIGMAP_TRUE, NULL, GIT_AUTO_CRLF_TRUE}, + {GIT_CONFIGMAP_STRING, "input", GIT_AUTO_CRLF_INPUT} +}; + +static git_configmap _configmap_safecrlf[] = { + {GIT_CONFIGMAP_FALSE, NULL, GIT_SAFE_CRLF_FALSE}, + {GIT_CONFIGMAP_TRUE, NULL, GIT_SAFE_CRLF_FAIL}, + {GIT_CONFIGMAP_STRING, "warn", GIT_SAFE_CRLF_WARN} +}; + +static git_configmap _configmap_logallrefupdates[] = { + {GIT_CONFIGMAP_FALSE, NULL, GIT_LOGALLREFUPDATES_FALSE}, + {GIT_CONFIGMAP_TRUE, NULL, GIT_LOGALLREFUPDATES_TRUE}, + {GIT_CONFIGMAP_STRING, "always", GIT_LOGALLREFUPDATES_ALWAYS}, +}; + +static git_configmap _configmap_abbrev[] = { + {GIT_CONFIGMAP_INT32, NULL, 0}, + {GIT_CONFIGMAP_FALSE, NULL, GIT_ABBREV_FALSE}, + {GIT_CONFIGMAP_STRING, "auto", GIT_ABBREV_DEFAULT} +}; + +static struct map_data _configmaps[] = { + {"core.autocrlf", _configmap_autocrlf, ARRAY_SIZE(_configmap_autocrlf), GIT_AUTO_CRLF_DEFAULT}, + {"core.eol", _configmap_eol, ARRAY_SIZE(_configmap_eol), GIT_EOL_DEFAULT}, + {"core.symlinks", NULL, 0, GIT_SYMLINKS_DEFAULT }, + {"core.ignorecase", NULL, 0, GIT_IGNORECASE_DEFAULT }, + {"core.filemode", NULL, 0, GIT_FILEMODE_DEFAULT }, + {"core.ignorestat", NULL, 0, GIT_IGNORESTAT_DEFAULT }, + {"core.trustctime", NULL, 0, GIT_TRUSTCTIME_DEFAULT }, + {"core.abbrev", _configmap_abbrev, ARRAY_SIZE(_configmap_abbrev), GIT_ABBREV_DEFAULT }, + {"core.precomposeunicode", NULL, 0, GIT_PRECOMPOSE_DEFAULT }, + {"core.safecrlf", _configmap_safecrlf, ARRAY_SIZE(_configmap_safecrlf), GIT_SAFE_CRLF_DEFAULT}, + {"core.logallrefupdates", _configmap_logallrefupdates, ARRAY_SIZE(_configmap_logallrefupdates), GIT_LOGALLREFUPDATES_DEFAULT}, + {"core.protecthfs", NULL, 0, GIT_PROTECTHFS_DEFAULT }, + {"core.protectntfs", NULL, 0, GIT_PROTECTNTFS_DEFAULT }, + {"core.fsyncobjectfiles", NULL, 0, GIT_FSYNCOBJECTFILES_DEFAULT }, + {"core.longpaths", NULL, 0, GIT_LONGPATHS_DEFAULT }, +}; + +int git_config__configmap_lookup(int *out, git_config *config, git_configmap_item item) +{ + int error = 0; + struct map_data *data = &_configmaps[(int)item]; + git_config_entry *entry; + + if ((error = git_config__lookup_entry(&entry, config, data->name, false)) < 0) + return error; + + if (!entry) + *out = data->default_value; + else if (data->maps) + error = git_config_lookup_map_value( + out, data->maps, data->map_count, entry->value); + else + error = git_config_parse_bool(out, entry->value); + + git_config_entry_free(entry); + return error; +} + +int git_repository__configmap_lookup(int *out, git_repository *repo, git_configmap_item item) +{ + intptr_t value = (intptr_t)git_atomic_load(repo->configmap_cache[(int)item]); + + *out = (int)value; + + if (value == GIT_CONFIGMAP_NOT_CACHED) { + git_config *config; + intptr_t oldval = value; + int error; + + if ((error = git_repository_config__weakptr(&config, repo)) < 0 || + (error = git_config__configmap_lookup(out, config, item)) < 0) + return error; + + value = *out; + git_atomic_compare_and_swap(&repo->configmap_cache[(int)item], (void *)oldval, (void *)value); + } + + return 0; +} + +void git_repository__configmap_lookup_cache_clear(git_repository *repo) +{ + int i; + + for (i = 0; i < GIT_CONFIGMAP_CACHE_MAX; ++i) + repo->configmap_cache[i] = GIT_CONFIGMAP_NOT_CACHED; +} + diff --git a/src/libgit2/config_file.c b/src/libgit2/config_file.c new file mode 100644 index 00000000000..870b137046a --- /dev/null +++ b/src/libgit2/config_file.c @@ -0,0 +1,1212 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "config.h" + +#include "git2/config.h" +#include "git2/sys/config.h" + +#include "array.h" +#include "str.h" +#include "config_backend.h" +#include "config_list.h" +#include "config_parse.h" +#include "filebuf.h" +#include "regexp.h" +#include "sysdir.h" +#include "wildmatch.h" +#include "hash.h" + +/* Max depth for [include] directives */ +#define MAX_INCLUDE_DEPTH 10 + +#define CONFIG_FILE_TYPE "file" + +typedef struct config_file { + git_futils_filestamp stamp; + unsigned char checksum[GIT_HASH_SHA256_SIZE]; + char *path; + git_array_t(struct config_file) includes; +} config_file; + +typedef struct { + git_config_backend parent; + git_mutex values_mutex; + git_config_list *config_list; + const git_repository *repo; + git_config_level_t level; + + git_array_t(git_config_parser) readers; + + bool locked; + git_filebuf locked_buf; + git_str locked_content; + + config_file file; +} config_file_backend; + +typedef struct { + const git_repository *repo; + config_file *file; + git_config_list *config_list; + git_config_level_t level; + unsigned int depth; +} config_file_parse_data; + +static int config_file_read(git_config_list *config_list, const git_repository *repo, config_file *file, git_config_level_t level, int depth); +static int config_file_read_buffer(git_config_list *config_list, const git_repository *repo, config_file *file, git_config_level_t level, int depth, const char *buf, size_t buflen); +static int config_file_write(config_file_backend *cfg, const char *orig_key, const char *key, const git_regexp *preg, const char *value); +static char *escape_value(const char *ptr); + +/** + * Take the current values map from the backend and increase its + * refcount. This is its own function to make sure we use the mutex to + * avoid the map pointer from changing under us. + */ +static int config_file_take_list(git_config_list **out, config_file_backend *b) +{ + int error; + + if ((error = git_mutex_lock(&b->values_mutex)) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock config backend"); + return error; + } + + git_config_list_incref(b->config_list); + *out = b->config_list; + + git_mutex_unlock(&b->values_mutex); + + return 0; +} + +static void config_file_clear(config_file *file) +{ + config_file *include; + uint32_t i; + + if (file == NULL) + return; + + git_array_foreach(file->includes, i, include) { + config_file_clear(include); + } + git_array_clear(file->includes); + + git__free(file->path); +} + +static int config_file_open(git_config_backend *cfg, git_config_level_t level, const git_repository *repo) +{ + config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); + int res; + + b->level = level; + b->repo = repo; + + if ((res = git_config_list_new(&b->config_list)) < 0) + return res; + + if (!git_fs_path_exists(b->file.path)) + return 0; + + /* + * git silently ignores configuration files that are not + * readable. We emulate that behavior. This is particularly + * important for sandboxed applications on macOS where the + * git configuration files may not be readable. + */ + if (p_access(b->file.path, R_OK) < 0) + return GIT_ENOTFOUND; + + if (res < 0 || (res = config_file_read(b->config_list, repo, &b->file, level, 0)) < 0) { + git_config_list_free(b->config_list); + b->config_list = NULL; + } + + return res; +} + +static int config_file_is_modified(int *modified, config_file *file) +{ + config_file *include; + git_str buf = GIT_STR_INIT; + unsigned char checksum[GIT_HASH_SHA256_SIZE]; + uint32_t i; + int error = 0; + + *modified = 0; + + if (!git_futils_filestamp_check(&file->stamp, file->path)) + goto check_includes; + + if ((error = git_futils_readbuffer(&buf, file->path)) < 0) + goto out; + + if ((error = git_hash_buf(checksum, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA256)) < 0) + goto out; + + if (memcmp(checksum, file->checksum, GIT_HASH_SHA256_SIZE) != 0) { + *modified = 1; + goto out; + } + +check_includes: + git_array_foreach(file->includes, i, include) { + if ((error = config_file_is_modified(modified, include)) < 0 || *modified) + goto out; + } + +out: + git_str_dispose(&buf); + + return error; +} + +static void config_file_clear_includes(config_file_backend *cfg) +{ + config_file *include; + uint32_t i; + + git_array_foreach(cfg->file.includes, i, include) + config_file_clear(include); + git_array_clear(cfg->file.includes); +} + +static int config_file_set_entries(git_config_backend *cfg, git_config_list *config_list) +{ + config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); + git_config_list *old = NULL; + int error; + + if (b->parent.readonly) { + git_error_set(GIT_ERROR_CONFIG, "this backend is read-only"); + return -1; + } + + if ((error = git_mutex_lock(&b->values_mutex)) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock config backend"); + goto out; + } + + old = b->config_list; + b->config_list = config_list; + + git_mutex_unlock(&b->values_mutex); + +out: + git_config_list_free(old); + return error; +} + +static int config_file_refresh_from_buffer(git_config_backend *cfg, const char *buf, size_t buflen) +{ + config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); + git_config_list *config_list = NULL; + int error; + + config_file_clear_includes(b); + + if ((error = git_config_list_new(&config_list)) < 0 || + (error = config_file_read_buffer(config_list, b->repo, &b->file, + b->level, 0, buf, buflen)) < 0 || + (error = config_file_set_entries(cfg, config_list)) < 0) + goto out; + + config_list = NULL; +out: + git_config_list_free(config_list); + return error; +} + +static int config_file_refresh(git_config_backend *cfg) +{ + config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); + git_config_list *config_list = NULL; + int error, modified; + + if (cfg->readonly) + return 0; + + if ((error = config_file_is_modified(&modified, &b->file)) < 0 && error != GIT_ENOTFOUND) + goto out; + + if (!modified) + return 0; + + config_file_clear_includes(b); + + if ((error = git_config_list_new(&config_list)) < 0 || + (error = config_file_read(config_list, b->repo, &b->file, b->level, 0)) < 0 || + (error = config_file_set_entries(cfg, config_list)) < 0) + goto out; + + config_list = NULL; +out: + git_config_list_free(config_list); + + return (error == GIT_ENOTFOUND) ? 0 : error; +} + +static void config_file_free(git_config_backend *_backend) +{ + config_file_backend *backend = GIT_CONTAINER_OF(_backend, config_file_backend, parent); + + if (backend == NULL) + return; + + config_file_clear(&backend->file); + git_config_list_free(backend->config_list); + git_mutex_free(&backend->values_mutex); + git__free(backend); +} + +static int config_file_iterator( + git_config_iterator **iter, + struct git_config_backend *backend) +{ + config_file_backend *b = GIT_CONTAINER_OF(backend, config_file_backend, parent); + git_config_list *dupped = NULL, *config_list = NULL; + int error; + + if ((error = config_file_refresh(backend)) < 0 || + (error = config_file_take_list(&config_list, b)) < 0 || + (error = git_config_list_dup(&dupped, config_list)) < 0 || + (error = git_config_list_iterator_new(iter, dupped)) < 0) + goto out; + +out: + /* Let iterator delete duplicated config_list when it's done */ + git_config_list_free(config_list); + git_config_list_free(dupped); + return error; +} + +static int config_file_snapshot(git_config_backend **out, git_config_backend *backend) +{ + return git_config_backend_snapshot(out, backend); +} + +static int config_file_set(git_config_backend *cfg, const char *name, const char *value) +{ + config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); + git_config_list *config_list; + git_config_list_entry *existing; + char *key, *esc_value = NULL; + int error; + + if ((error = git_config__normalize_name(name, &key)) < 0) + return error; + + if ((error = config_file_take_list(&config_list, b)) < 0) + return error; + + /* Check whether we'd be modifying an included or multivar key */ + if ((error = git_config_list_get_unique(&existing, config_list, key)) < 0) { + if (error != GIT_ENOTFOUND) + goto out; + error = 0; + } else if ((!existing->base.entry.value && !value) || + (existing->base.entry.value && value && !strcmp(existing->base.entry.value, value))) { + /* don't update if old and new values already match */ + error = 0; + goto out; + } + + /* No early returns due to sanity checks, let's write it out and refresh */ + if (value) { + esc_value = escape_value(value); + GIT_ERROR_CHECK_ALLOC(esc_value); + } + + if ((error = config_file_write(b, name, key, NULL, esc_value)) < 0) + goto out; + +out: + git_config_list_free(config_list); + git__free(esc_value); + git__free(key); + return error; +} + +/* + * Internal function that actually gets the value in string form + */ +static int config_file_get( + git_config_backend *cfg, + const char *key, + git_config_backend_entry **out) +{ + config_file_backend *h = GIT_CONTAINER_OF(cfg, config_file_backend, parent); + git_config_list *config_list = NULL; + git_config_list_entry *entry; + int error = 0; + + if (!h->parent.readonly && ((error = config_file_refresh(cfg)) < 0)) + return error; + + if ((error = config_file_take_list(&config_list, h)) < 0) + return error; + + if ((error = (git_config_list_get(&entry, config_list, key))) < 0) { + git_config_list_free(config_list); + return error; + } + + *out = &entry->base; + + return 0; +} + +static int config_file_set_multivar( + git_config_backend *cfg, const char *name, const char *regexp, const char *value) +{ + config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); + git_regexp preg; + int result; + char *key; + + GIT_ASSERT_ARG(regexp); + + if ((result = git_config__normalize_name(name, &key)) < 0) + return result; + + if ((result = git_regexp_compile(&preg, regexp, 0)) < 0) + goto out; + + /* If we do have it, set call config_file_write() and reload */ + if ((result = config_file_write(b, name, key, &preg, value)) < 0) + goto out; + +out: + git__free(key); + git_regexp_dispose(&preg); + + return result; +} + +static int config_file_delete(git_config_backend *cfg, const char *name) +{ + config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); + git_config_list *config_list = NULL; + git_config_list_entry *entry; + char *key = NULL; + int error; + + if ((error = git_config__normalize_name(name, &key)) < 0) + goto out; + + if ((error = config_file_take_list(&config_list, b)) < 0) + goto out; + + /* Check whether we'd be modifying an included or multivar key */ + if ((error = git_config_list_get_unique(&entry, config_list, key)) < 0) { + if (error == GIT_ENOTFOUND) + git_error_set(GIT_ERROR_CONFIG, "could not find key '%s' to delete", name); + goto out; + } + + if ((error = config_file_write(b, name, entry->base.entry.name, NULL, NULL)) < 0) + goto out; + +out: + git_config_list_free(config_list); + git__free(key); + return error; +} + +static int config_file_delete_multivar(git_config_backend *cfg, const char *name, const char *regexp) +{ + config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); + git_config_list *config_list = NULL; + git_config_list_entry *entry = NULL; + git_regexp preg = GIT_REGEX_INIT; + char *key = NULL; + int result; + + if ((result = git_config__normalize_name(name, &key)) < 0) + goto out; + + if ((result = config_file_take_list(&config_list, b)) < 0) + goto out; + + if ((result = git_config_list_get(&entry, config_list, key)) < 0) { + if (result == GIT_ENOTFOUND) + git_error_set(GIT_ERROR_CONFIG, "could not find key '%s' to delete", name); + goto out; + } + + if ((result = git_regexp_compile(&preg, regexp, 0)) < 0) + goto out; + + if ((result = config_file_write(b, name, key, &preg, NULL)) < 0) + goto out; + +out: + git_config_list_free(config_list); + git__free(key); + git_regexp_dispose(&preg); + return result; +} + +static int config_file_lock(git_config_backend *_cfg) +{ + config_file_backend *cfg = GIT_CONTAINER_OF(_cfg, config_file_backend, parent); + int error; + + if ((error = git_filebuf_open(&cfg->locked_buf, cfg->file.path, 0, GIT_CONFIG_FILE_MODE)) < 0) + return error; + + error = git_futils_readbuffer(&cfg->locked_content, cfg->file.path); + if (error < 0 && error != GIT_ENOTFOUND) { + git_filebuf_cleanup(&cfg->locked_buf); + return error; + } + + cfg->locked = true; + return 0; + +} + +static int config_file_unlock(git_config_backend *_cfg, int success) +{ + config_file_backend *cfg = GIT_CONTAINER_OF(_cfg, config_file_backend, parent); + int error = 0; + + if (success) { + git_filebuf_write(&cfg->locked_buf, cfg->locked_content.ptr, cfg->locked_content.size); + error = git_filebuf_commit(&cfg->locked_buf); + } + + git_filebuf_cleanup(&cfg->locked_buf); + git_str_dispose(&cfg->locked_content); + cfg->locked = false; + + return error; +} + +int git_config_backend_from_file(git_config_backend **out, const char *path) +{ + config_file_backend *backend; + + backend = git__calloc(1, sizeof(config_file_backend)); + GIT_ERROR_CHECK_ALLOC(backend); + + backend->parent.version = GIT_CONFIG_BACKEND_VERSION; + git_mutex_init(&backend->values_mutex); + + backend->file.path = git__strdup(path); + GIT_ERROR_CHECK_ALLOC(backend->file.path); + git_array_init(backend->file.includes); + + backend->parent.open = config_file_open; + backend->parent.get = config_file_get; + backend->parent.set = config_file_set; + backend->parent.set_multivar = config_file_set_multivar; + backend->parent.del = config_file_delete; + backend->parent.del_multivar = config_file_delete_multivar; + backend->parent.iterator = config_file_iterator; + backend->parent.snapshot = config_file_snapshot; + backend->parent.lock = config_file_lock; + backend->parent.unlock = config_file_unlock; + backend->parent.free = config_file_free; + + *out = (git_config_backend *)backend; + + return 0; +} + +static int included_path(git_str *out, const char *dir, const char *path) +{ + /* From the user's home */ + if (path[0] == '~' && path[1] == '/') + return git_sysdir_expand_homedir_file(out, &path[1]); + + return git_fs_path_join_unrooted(out, path, dir, NULL); +} + +/* Escape the values to write them to the file */ +static char *escape_value(const char *ptr) +{ + git_str buf; + size_t len; + const char *esc; + + GIT_ASSERT_ARG_WITH_RETVAL(ptr, NULL); + + len = strlen(ptr); + if (!len) + return git__calloc(1, sizeof(char)); + + if (git_str_init(&buf, len) < 0) + return NULL; + + while (*ptr != '\0') { + if ((esc = strchr(git_config_escaped, *ptr)) != NULL) { + git_str_putc(&buf, '\\'); + git_str_putc(&buf, git_config_escapes[esc - git_config_escaped]); + } else { + git_str_putc(&buf, *ptr); + } + ptr++; + } + + if (git_str_oom(&buf)) + return NULL; + + return git_str_detach(&buf); +} + +static int parse_include(config_file_parse_data *parse_data, const char *file) +{ + config_file *include; + git_str path = GIT_STR_INIT; + char *dir; + int result; + + if (!file) + return 0; + + if ((result = git_fs_path_dirname_r(&path, parse_data->file->path)) < 0) + return result; + + dir = git_str_detach(&path); + result = included_path(&path, dir, file); + git__free(dir); + + if (result < 0) + return result; + + include = git_array_alloc(parse_data->file->includes); + GIT_ERROR_CHECK_ALLOC(include); + memset(include, 0, sizeof(*include)); + git_array_init(include->includes); + include->path = git_str_detach(&path); + + result = config_file_read(parse_data->config_list, parse_data->repo, include, + parse_data->level, parse_data->depth+1); + + if (result == GIT_ENOTFOUND) { + git_error_clear(); + result = 0; + } + + return result; +} + +static int do_match_gitdir( + int *matches, + const git_repository *repo, + const char *cfg_file, + const char *condition, + bool case_insensitive) +{ + git_str pattern = GIT_STR_INIT, gitdir = GIT_STR_INIT; + int error; + + if (condition[0] == '.' && git_fs_path_is_dirsep(condition[1])) { + git_fs_path_dirname_r(&pattern, cfg_file); + git_str_joinpath(&pattern, pattern.ptr, condition + 2); + } else if (condition[0] == '~' && git_fs_path_is_dirsep(condition[1])) + git_sysdir_expand_homedir_file(&pattern, condition + 1); + else if (!git_fs_path_is_absolute(condition)) + git_str_joinpath(&pattern, "**", condition); + else + git_str_sets(&pattern, condition); + + if (git_fs_path_is_dirsep(condition[strlen(condition) - 1])) + git_str_puts(&pattern, "**"); + + if (git_str_oom(&pattern)) { + error = -1; + goto out; + } + + if ((error = git_repository__item_path(&gitdir, repo, GIT_REPOSITORY_ITEM_GITDIR)) < 0) + goto out; + + if (git_fs_path_is_dirsep(gitdir.ptr[gitdir.size - 1])) + git_str_truncate(&gitdir, gitdir.size - 1); + + *matches = wildmatch(pattern.ptr, gitdir.ptr, + WM_PATHNAME | (case_insensitive ? WM_CASEFOLD : 0)) == WM_MATCH; +out: + git_str_dispose(&pattern); + git_str_dispose(&gitdir); + return error; +} + +static int conditional_match_gitdir( + int *matches, + const git_repository *repo, + const char *cfg_file, + const char *value) +{ + return do_match_gitdir(matches, repo, cfg_file, value, false); +} + +static int conditional_match_gitdir_i( + int *matches, + const git_repository *repo, + const char *cfg_file, + const char *value) +{ + return do_match_gitdir(matches, repo, cfg_file, value, true); +} + +static int conditional_match_onbranch( + int *matches, + const git_repository *repo, + const char *cfg_file, + const char *condition) +{ + git_str reference = GIT_STR_INIT, buf = GIT_STR_INIT; + int error; + + GIT_UNUSED(cfg_file); + + /* + * NOTE: you cannot use `git_repository_head` here. Looking up the + * HEAD reference will create the ODB, which causes us to read the + * repo's config for keys like core.precomposeUnicode. As we're + * just parsing the config right now, though, this would result in + * an endless recursion. + */ + + if ((error = git_str_joinpath(&buf, git_repository_path(repo), GIT_HEAD_FILE)) < 0 || + (error = git_futils_readbuffer(&reference, buf.ptr)) < 0) + goto out; + git_str_rtrim(&reference); + + if (git__strncmp(reference.ptr, GIT_SYMREF, strlen(GIT_SYMREF))) + goto out; + git_str_consume(&reference, reference.ptr + strlen(GIT_SYMREF)); + + if (git__strncmp(reference.ptr, GIT_REFS_HEADS_DIR, strlen(GIT_REFS_HEADS_DIR))) + goto out; + git_str_consume(&reference, reference.ptr + strlen(GIT_REFS_HEADS_DIR)); + + /* + * If the condition ends with a '/', then we should treat it as if + * it had '**' appended. + */ + if ((error = git_str_sets(&buf, condition)) < 0) + goto out; + if (git_fs_path_is_dirsep(condition[strlen(condition) - 1]) && + (error = git_str_puts(&buf, "**")) < 0) + goto out; + + *matches = wildmatch(buf.ptr, reference.ptr, WM_PATHNAME) == WM_MATCH; +out: + git_str_dispose(&reference); + git_str_dispose(&buf); + + return error; + +} + +static const struct { + const char *prefix; + int (*matches)(int *matches, const git_repository *repo, const char *cfg, const char *value); +} conditions[] = { + { "gitdir:", conditional_match_gitdir }, + { "gitdir/i:", conditional_match_gitdir_i }, + { "onbranch:", conditional_match_onbranch } +}; + +static int parse_conditional_include(config_file_parse_data *parse_data, const char *section, const char *file) +{ + char *condition; + size_t section_len, i; + int error = 0, matches; + + if (!parse_data->repo || !file) + return 0; + + section_len = strlen(section); + + /* + * We checked that the string starts with `includeIf.` and ends + * in `.path` to get here. Make sure it consists of more. + */ + if (section_len < CONST_STRLEN("includeIf.") + CONST_STRLEN(".path")) + return 0; + + condition = git__substrdup(section + CONST_STRLEN("includeIf."), + section_len - CONST_STRLEN("includeIf.") - CONST_STRLEN(".path")); + + GIT_ERROR_CHECK_ALLOC(condition); + + for (i = 0; i < ARRAY_SIZE(conditions); i++) { + if (git__prefixcmp(condition, conditions[i].prefix)) + continue; + + if ((error = conditions[i].matches(&matches, + parse_data->repo, + parse_data->file->path, + condition + strlen(conditions[i].prefix))) < 0) + break; + + if (matches) + error = parse_include(parse_data, file); + + break; + } + + git__free(condition); + return error; +} + +static int read_on_variable( + git_config_parser *reader, + const char *current_section, + const char *var_name, + const char *var_value, + const char *line, + size_t line_len, + void *data) +{ + config_file_parse_data *parse_data = (config_file_parse_data *)data; + git_str buf = GIT_STR_INIT; + git_config_list_entry *entry; + const char *c; + int result = 0; + + GIT_UNUSED(reader); + GIT_UNUSED(line); + GIT_UNUSED(line_len); + + if (current_section) { + /* TODO: Once warnings lang, we should likely warn + * here. Git appears to warn in most cases if it sees + * un-namespaced config options. + */ + git_str_puts(&buf, current_section); + git_str_putc(&buf, '.'); + } + + for (c = var_name; *c; c++) + git_str_putc(&buf, git__tolower(*c)); + + if (git_str_oom(&buf)) + return -1; + + entry = git__calloc(1, sizeof(git_config_list_entry)); + GIT_ERROR_CHECK_ALLOC(entry); + + entry->base.entry.name = git_str_detach(&buf); + GIT_ERROR_CHECK_ALLOC(entry->base.entry.name); + + if (var_value) { + entry->base.entry.value = git__strdup(var_value); + GIT_ERROR_CHECK_ALLOC(entry->base.entry.value); + } + + entry->base.entry.backend_type = git_config_list_add_string(parse_data->config_list, CONFIG_FILE_TYPE); + GIT_ERROR_CHECK_ALLOC(entry->base.entry.backend_type); + + entry->base.entry.origin_path = git_config_list_add_string(parse_data->config_list, parse_data->file->path); + GIT_ERROR_CHECK_ALLOC(entry->base.entry.origin_path); + + entry->base.entry.level = parse_data->level; + entry->base.entry.include_depth = parse_data->depth; + entry->base.free = git_config_list_entry_free; + entry->config_list = parse_data->config_list; + + if ((result = git_config_list_append(parse_data->config_list, entry)) < 0) + return result; + + result = 0; + + /* Add or append the new config option */ + if (!git__strcmp(entry->base.entry.name, "include.path")) + result = parse_include(parse_data, entry->base.entry.value); + else if (!git__prefixcmp(entry->base.entry.name, "includeif.") && + !git__suffixcmp(entry->base.entry.name, ".path")) + result = parse_conditional_include(parse_data, entry->base.entry.name, entry->base.entry.value); + + return result; +} + +static int config_file_read_buffer( + git_config_list *config_list, + const git_repository *repo, + config_file *file, + git_config_level_t level, + int depth, + const char *buf, + size_t buflen) +{ + config_file_parse_data parse_data; + git_config_parser reader; + int error; + + if (depth >= MAX_INCLUDE_DEPTH) { + git_error_set(GIT_ERROR_CONFIG, "maximum config include depth reached"); + return -1; + } + + /* Initialize the reading position */ + reader.path = file->path; + git_parse_ctx_init(&reader.ctx, buf, buflen); + + /* If the file is empty, there's nothing for us to do */ + if (!reader.ctx.content || *reader.ctx.content == '\0') { + error = 0; + goto out; + } + + parse_data.repo = repo; + parse_data.file = file; + parse_data.config_list = config_list; + parse_data.level = level; + parse_data.depth = depth; + + error = git_config_parse(&reader, NULL, read_on_variable, NULL, NULL, &parse_data); + +out: + return error; +} + +static int config_file_read( + git_config_list *config_list, + const git_repository *repo, + config_file *file, + git_config_level_t level, + int depth) +{ + git_str contents = GIT_STR_INIT; + struct stat st; + int error; + + if (p_stat(file->path, &st) < 0) { + error = git_fs_path_set_error(errno, file->path, "stat"); + goto out; + } + + if ((error = git_futils_readbuffer(&contents, file->path)) < 0) + goto out; + + git_futils_filestamp_set_from_stat(&file->stamp, &st); + if ((error = git_hash_buf(file->checksum, contents.ptr, contents.size, GIT_HASH_ALGORITHM_SHA256)) < 0) + goto out; + + if ((error = config_file_read_buffer(config_list, repo, file, level, depth, + contents.ptr, contents.size)) < 0) + goto out; + +out: + git_str_dispose(&contents); + return error; +} + +static int write_section(git_str *fbuf, const char *key) +{ + int result; + const char *dot; + git_str buf = GIT_STR_INIT; + + /* All of this just for [section "subsection"] */ + dot = strchr(key, '.'); + git_str_putc(&buf, '['); + if (dot == NULL) { + git_str_puts(&buf, key); + } else { + char *escaped; + git_str_put(&buf, key, dot - key); + escaped = escape_value(dot + 1); + GIT_ERROR_CHECK_ALLOC(escaped); + git_str_printf(&buf, " \"%s\"", escaped); + git__free(escaped); + } + git_str_puts(&buf, "]\n"); + + if (git_str_oom(&buf)) + return -1; + + result = git_str_put(fbuf, git_str_cstr(&buf), buf.size); + git_str_dispose(&buf); + + return result; +} + +static const char *quotes_for_value(const char *value) +{ + const char *ptr; + + if (value[0] == ' ' || value[0] == '\0') + return "\""; + + for (ptr = value; *ptr; ++ptr) { + if (*ptr == ';' || *ptr == '#') + return "\""; + } + + if (ptr[-1] == ' ') + return "\""; + + return ""; +} + +struct write_data { + git_str *buf; + git_str buffered_comment; + unsigned int in_section : 1, + preg_replaced : 1; + const char *orig_section; + const char *section; + const char *orig_name; + const char *name; + const git_regexp *preg; + const char *value; +}; + +static int write_line_to(git_str *buf, const char *line, size_t line_len) +{ + int result = git_str_put(buf, line, line_len); + + if (!result && line_len && line[line_len-1] != '\n') + result = git_str_printf(buf, "\n"); + + return result; +} + +static int write_line(struct write_data *write_data, const char *line, size_t line_len) +{ + return write_line_to(write_data->buf, line, line_len); +} + +static int write_value(struct write_data *write_data) +{ + const char *q; + int result; + + q = quotes_for_value(write_data->value); + result = git_str_printf(write_data->buf, + "\t%s = %s%s%s\n", write_data->orig_name, q, write_data->value, q); + + /* If we are updating a single name/value, we're done. Setting `value` + * to `NULL` will prevent us from trying to write it again later (in + * `write_on_section`) if we see the same section repeated. + */ + if (!write_data->preg) + write_data->value = NULL; + + return result; +} + +static int write_on_section( + git_config_parser *reader, + const char *current_section, + const char *line, + size_t line_len, + void *data) +{ + struct write_data *write_data = (struct write_data *)data; + int result = 0; + + GIT_UNUSED(reader); + + /* If we were previously in the correct section (but aren't anymore) + * and haven't written our value (for a simple name/value set, not + * a multivar), then append it to the end of the section before writing + * the new one. + */ + if (write_data->in_section && !write_data->preg && write_data->value) + result = write_value(write_data); + + write_data->in_section = strcmp(current_section, write_data->section) == 0; + + /* + * If there were comments just before this section, dump them as well. + */ + if (!result) { + result = git_str_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size); + git_str_clear(&write_data->buffered_comment); + } + + if (!result) + result = write_line(write_data, line, line_len); + + return result; +} + +static int write_on_variable( + git_config_parser *reader, + const char *current_section, + const char *var_name, + const char *var_value, + const char *line, + size_t line_len, + void *data) +{ + struct write_data *write_data = (struct write_data *)data; + bool has_matched = false; + int error; + + GIT_UNUSED(reader); + GIT_UNUSED(current_section); + + /* + * If there were comments just before this variable, let's dump them as well. + */ + if ((error = git_str_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size)) < 0) + return error; + + git_str_clear(&write_data->buffered_comment); + + /* See if we are to update this name/value pair; first examine name */ + if (write_data->in_section && + strcasecmp(write_data->name, var_name) == 0) + has_matched = true; + + /* If we have a regex to match the value, see if it matches */ + if (has_matched && write_data->preg != NULL) + has_matched = (git_regexp_match(write_data->preg, var_value) == 0); + + /* If this isn't the name/value we're looking for, simply dump the + * existing data back out and continue on. + */ + if (!has_matched) + return write_line(write_data, line, line_len); + + write_data->preg_replaced = 1; + + /* If value is NULL, we are deleting this value; write nothing. */ + if (!write_data->value) + return 0; + + return write_value(write_data); +} + +static int write_on_comment(git_config_parser *reader, const char *line, size_t line_len, void *data) +{ + struct write_data *write_data; + + GIT_UNUSED(reader); + + write_data = (struct write_data *)data; + return write_line_to(&write_data->buffered_comment, line, line_len); +} + +static int write_on_eof( + git_config_parser *reader, const char *current_section, void *data) +{ + struct write_data *write_data = (struct write_data *)data; + int result = 0; + + GIT_UNUSED(reader); + + /* + * If we've buffered comments when reaching EOF, make sure to dump them. + */ + if ((result = git_str_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size)) < 0) + return result; + + /* If we are at the EOF and have not written our value (again, for a + * simple name/value set, not a multivar) then we have never seen the + * section in question and should create a new section and write the + * value. + */ + if ((!write_data->preg || !write_data->preg_replaced) && write_data->value) { + /* write the section header unless we're already in it */ + if (!current_section || strcmp(current_section, write_data->section)) + result = write_section(write_data->buf, write_data->orig_section); + + if (!result) + result = write_value(write_data); + } + + return result; +} + +/* + * This is pretty much the parsing, except we write out anything we don't have + */ +static int config_file_write( + config_file_backend *cfg, + const char *orig_key, + const char *key, + const git_regexp *preg, + const char *value) + +{ + char *orig_section = NULL, *section = NULL; + const char *orig_name, *name, *ldot; + git_str buf = GIT_STR_INIT, contents = GIT_STR_INIT; + git_config_parser parser = GIT_CONFIG_PARSER_INIT; + git_filebuf file = GIT_FILEBUF_INIT; + struct write_data write_data; + int error; + + memset(&write_data, 0, sizeof(write_data)); + + if (cfg->locked) { + error = git_str_puts(&contents, git_str_cstr(&cfg->locked_content) == NULL ? "" : git_str_cstr(&cfg->locked_content)); + } else { + if ((error = git_filebuf_open(&file, cfg->file.path, + GIT_FILEBUF_HASH_SHA256, + GIT_CONFIG_FILE_MODE)) < 0) + goto done; + + /* We need to read in our own config file */ + error = git_futils_readbuffer(&contents, cfg->file.path); + } + if (error < 0 && error != GIT_ENOTFOUND) + goto done; + + if ((git_config_parser_init(&parser, cfg->file.path, contents.ptr, contents.size)) < 0) + goto done; + + ldot = strrchr(key, '.'); + name = ldot + 1; + section = git__strndup(key, ldot - key); + GIT_ERROR_CHECK_ALLOC(section); + + ldot = strrchr(orig_key, '.'); + orig_name = ldot + 1; + orig_section = git__strndup(orig_key, ldot - orig_key); + GIT_ERROR_CHECK_ALLOC(orig_section); + + write_data.buf = &buf; + write_data.orig_section = orig_section; + write_data.section = section; + write_data.orig_name = orig_name; + write_data.name = name; + write_data.preg = preg; + write_data.value = value; + + if ((error = git_config_parse(&parser, write_on_section, write_on_variable, + write_on_comment, write_on_eof, &write_data)) < 0) + goto done; + + if (cfg->locked) { + size_t len = buf.asize; + /* Update our copy with the modified contents */ + git_str_dispose(&cfg->locked_content); + git_str_attach(&cfg->locked_content, git_str_detach(&buf), len); + } else { + git_filebuf_write(&file, git_str_cstr(&buf), git_str_len(&buf)); + + if ((error = git_filebuf_commit(&file)) < 0) + goto done; + + if ((error = config_file_refresh_from_buffer(&cfg->parent, buf.ptr, buf.size)) < 0) + goto done; + } + +done: + git__free(section); + git__free(orig_section); + git_str_dispose(&write_data.buffered_comment); + git_str_dispose(&buf); + git_str_dispose(&contents); + git_filebuf_cleanup(&file); + git_config_parser_dispose(&parser); + + return error; +} diff --git a/src/libgit2/config_list.c b/src/libgit2/config_list.c new file mode 100644 index 00000000000..0e6559795c2 --- /dev/null +++ b/src/libgit2/config_list.c @@ -0,0 +1,285 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "config_list.h" +#include "hashmap_str.h" + +typedef struct config_entry_list { + struct config_entry_list *next; + struct config_entry_list *last; + git_config_list_entry *entry; +} config_entry_list; + +typedef struct { + git_config_list_entry *entry; + bool multivar; +} config_entry_map_head; + +typedef struct config_list_iterator { + git_config_iterator parent; + git_config_list *list; + config_entry_list *head; +} config_list_iterator; + +GIT_HASHMAP_STR_SETUP(git_config_list_pathmap, char *); +GIT_HASHMAP_STR_SETUP(git_config_list_headmap, config_entry_map_head *); + +struct git_config_list { + git_refcount rc; + + /* Interned strings - paths to config files or backend types */ + git_config_list_pathmap strings; + + /* Config entries */ + git_config_list_headmap map; + config_entry_list *entries; +}; + +int git_config_list_new(git_config_list **out) +{ + git_config_list *config_list; + + config_list = git__calloc(1, sizeof(git_config_list)); + GIT_ERROR_CHECK_ALLOC(config_list); + GIT_REFCOUNT_INC(config_list); + + *out = config_list; + return 0; +} + +int git_config_list_dup_entry(git_config_list *config_list, const git_config_entry *entry) +{ + git_config_list_entry *duplicated; + int error; + + duplicated = git__calloc(1, sizeof(git_config_list_entry)); + GIT_ERROR_CHECK_ALLOC(duplicated); + + duplicated->base.entry.name = git__strdup(entry->name); + GIT_ERROR_CHECK_ALLOC(duplicated->base.entry.name); + + if (entry->value) { + duplicated->base.entry.value = git__strdup(entry->value); + GIT_ERROR_CHECK_ALLOC(duplicated->base.entry.value); + } + + duplicated->base.entry.backend_type = git_config_list_add_string(config_list, entry->backend_type); + GIT_ERROR_CHECK_ALLOC(duplicated->base.entry.backend_type); + + if (entry->origin_path) { + duplicated->base.entry.origin_path = git_config_list_add_string(config_list, entry->origin_path); + GIT_ERROR_CHECK_ALLOC(duplicated->base.entry.origin_path); + } + + duplicated->base.entry.level = entry->level; + duplicated->base.entry.include_depth = entry->include_depth; + duplicated->base.free = git_config_list_entry_free; + duplicated->config_list = config_list; + + if ((error = git_config_list_append(config_list, duplicated)) < 0) + goto out; + +out: + if (error && duplicated) { + git__free((char *) duplicated->base.entry.name); + git__free((char *) duplicated->base.entry.value); + git__free(duplicated); + } + return error; +} + +int git_config_list_dup(git_config_list **out, git_config_list *config_list) +{ + git_config_list *result = NULL; + config_entry_list *head; + int error; + + if ((error = git_config_list_new(&result)) < 0) + goto out; + + for (head = config_list->entries; head; head = head->next) + if ((git_config_list_dup_entry(result, &head->entry->base.entry)) < 0) + goto out; + + *out = result; + result = NULL; + +out: + git_config_list_free(result); + return error; +} + +void git_config_list_incref(git_config_list *config_list) +{ + GIT_REFCOUNT_INC(config_list); +} + +static void config_list_free(git_config_list *config_list) +{ + config_entry_list *entry_list = NULL, *next; + config_entry_map_head *head; + char *str; + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + + while (git_config_list_pathmap_iterate(&iter, NULL, &str, &config_list->strings) == 0) + git__free(str); + + git_config_list_pathmap_dispose(&config_list->strings); + + iter = GIT_HASHMAP_ITER_INIT; + while (git_config_list_headmap_iterate(&iter, NULL, &head, &config_list->map) == 0) { + git__free((char *) head->entry->base.entry.name); + git__free(head); + } + git_config_list_headmap_dispose(&config_list->map); + + entry_list = config_list->entries; + while (entry_list != NULL) { + next = entry_list->next; + git__free((char *) entry_list->entry->base.entry.value); + git__free(entry_list->entry); + git__free(entry_list); + entry_list = next; + } + + git__free(config_list); +} + +void git_config_list_free(git_config_list *config_list) +{ + if (config_list) + GIT_REFCOUNT_DEC(config_list, config_list_free); +} + +int git_config_list_append(git_config_list *config_list, git_config_list_entry *entry) +{ + config_entry_list *list_head; + config_entry_map_head *map_head; + + if (git_config_list_headmap_get(&map_head, &config_list->map, entry->base.entry.name) == 0) { + map_head->multivar = true; + /* + * This is a micro-optimization for configuration files + * with a lot of same keys. As for multivars the entry's + * key will be the same for all list, we can just free + * all except the first entry's name and just re-use it. + */ + git__free((char *) entry->base.entry.name); + entry->base.entry.name = map_head->entry->base.entry.name; + } else { + map_head = git__calloc(1, sizeof(*map_head)); + if ((git_config_list_headmap_put(&config_list->map, entry->base.entry.name, map_head)) < 0) + return -1; + } + map_head->entry = entry; + + list_head = git__calloc(1, sizeof(config_entry_list)); + GIT_ERROR_CHECK_ALLOC(list_head); + list_head->entry = entry; + + if (config_list->entries) + config_list->entries->last->next = list_head; + else + config_list->entries = list_head; + config_list->entries->last = list_head; + + return 0; +} + +int git_config_list_get(git_config_list_entry **out, git_config_list *config_list, const char *key) +{ + config_entry_map_head *entry; + + if (git_config_list_headmap_get(&entry, &config_list->map, key) != 0) + return GIT_ENOTFOUND; + + *out = entry->entry; + return 0; +} + +int git_config_list_get_unique(git_config_list_entry **out, git_config_list *config_list, const char *key) +{ + config_entry_map_head *entry; + + if (git_config_list_headmap_get(&entry, &config_list->map, key) != 0) + return GIT_ENOTFOUND; + + if (entry->multivar) { + git_error_set(GIT_ERROR_CONFIG, "entry is not unique due to being a multivar"); + return -1; + } + + if (entry->entry->base.entry.include_depth) { + git_error_set(GIT_ERROR_CONFIG, "entry is not unique due to being included"); + return -1; + } + + *out = entry->entry; + return 0; +} + +static void config_iterator_free(git_config_iterator *iter) +{ + config_list_iterator *it = (config_list_iterator *) iter; + git_config_list_free(it->list); + git__free(it); +} + +static int config_iterator_next( + git_config_backend_entry **entry, + git_config_iterator *iter) +{ + config_list_iterator *it = (config_list_iterator *) iter; + + if (!it->head) + return GIT_ITEROVER; + + *entry = &it->head->entry->base; + it->head = it->head->next; + + return 0; +} + +int git_config_list_iterator_new(git_config_iterator **out, git_config_list *config_list) +{ + config_list_iterator *it; + + it = git__calloc(1, sizeof(config_list_iterator)); + GIT_ERROR_CHECK_ALLOC(it); + it->parent.next = config_iterator_next; + it->parent.free = config_iterator_free; + it->head = config_list->entries; + it->list = config_list; + + git_config_list_incref(config_list); + *out = &it->parent; + + return 0; +} + +/* release the map containing the entry as an equivalent to freeing it */ +void git_config_list_entry_free(git_config_backend_entry *e) +{ + git_config_list_entry *entry = (git_config_list_entry *)e; + git_config_list_free(entry->config_list); +} + +const char *git_config_list_add_string( + git_config_list *config_list, + const char *str) +{ + char *s; + + if (git_config_list_pathmap_get(&s, &config_list->strings, str) == 0) + return s; + + if ((s = git__strdup(str)) == NULL || + git_config_list_pathmap_put(&config_list->strings, s, s) < 0) + return NULL; + + return s; +} diff --git a/src/libgit2/config_list.h b/src/libgit2/config_list.h new file mode 100644 index 00000000000..091a59b9079 --- /dev/null +++ b/src/libgit2/config_list.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "git2/sys/config.h" +#include "config.h" + +typedef struct git_config_list git_config_list; + +typedef struct { + git_config_backend_entry base; + git_config_list *config_list; +} git_config_list_entry; + +int git_config_list_new(git_config_list **out); +int git_config_list_dup(git_config_list **out, git_config_list *list); +int git_config_list_dup_entry(git_config_list *list, const git_config_entry *entry); +void git_config_list_incref(git_config_list *list); +void git_config_list_free(git_config_list *list); +/* Add or append the new config option */ +int git_config_list_append(git_config_list *list, git_config_list_entry *entry); +int git_config_list_get(git_config_list_entry **out, git_config_list *list, const char *key); +int git_config_list_get_unique(git_config_list_entry **out, git_config_list *list, const char *key); +int git_config_list_iterator_new(git_config_iterator **out, git_config_list *list); +const char *git_config_list_add_string(git_config_list *list, const char *str); + +void git_config_list_entry_free(git_config_backend_entry *entry); diff --git a/src/libgit2/config_mem.c b/src/libgit2/config_mem.c new file mode 100644 index 00000000000..3c159073f2d --- /dev/null +++ b/src/libgit2/config_mem.c @@ -0,0 +1,374 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "config.h" + +#include "config_backend.h" +#include "config_parse.h" +#include "config_list.h" +#include "strlist.h" + +typedef struct { + git_config_backend parent; + + char *backend_type; + char *origin_path; + + git_config_list *config_list; + + /* Configuration data in the config file format */ + git_str cfg; + + /* Array of key=value pairs */ + char **values; + size_t values_len; +} config_memory_backend; + +typedef struct { + const char *backend_type; + const char *origin_path; + git_config_list *config_list; + git_config_level_t level; +} config_memory_parse_data; + +static int config_error_readonly(void) +{ + git_error_set(GIT_ERROR_CONFIG, "this backend is read-only"); + return -1; +} + +static int read_variable_cb( + git_config_parser *reader, + const char *current_section, + const char *var_name, + const char *var_value, + const char *line, + size_t line_len, + void *payload) +{ + config_memory_parse_data *parse_data = (config_memory_parse_data *) payload; + git_str buf = GIT_STR_INIT; + git_config_list_entry *entry; + const char *c; + int result; + + GIT_UNUSED(reader); + GIT_UNUSED(line); + GIT_UNUSED(line_len); + + if (current_section) { + /* TODO: Once warnings land, we should likely warn + * here. Git appears to warn in most cases if it sees + * un-namespaced config options. + */ + git_str_puts(&buf, current_section); + git_str_putc(&buf, '.'); + } + + for (c = var_name; *c; c++) + git_str_putc(&buf, git__tolower(*c)); + + if (git_str_oom(&buf)) + return -1; + + entry = git__calloc(1, sizeof(git_config_list_entry)); + GIT_ERROR_CHECK_ALLOC(entry); + entry->base.entry.name = git_str_detach(&buf); + entry->base.entry.value = var_value ? git__strdup(var_value) : NULL; + entry->base.entry.level = parse_data->level; + entry->base.entry.include_depth = 0; + entry->base.entry.backend_type = parse_data->backend_type; + entry->base.entry.origin_path = parse_data->origin_path; + entry->base.free = git_config_list_entry_free; + entry->config_list = parse_data->config_list; + + if ((result = git_config_list_append(parse_data->config_list, entry)) < 0) + return result; + + return result; +} + +static int parse_config( + config_memory_backend *memory_backend, + git_config_level_t level) +{ + git_config_parser parser = GIT_PARSE_CTX_INIT; + config_memory_parse_data parse_data; + int error; + + if ((error = git_config_parser_init(&parser, "in-memory", + memory_backend->cfg.ptr, memory_backend->cfg.size)) < 0) + goto out; + + parse_data.backend_type = git_config_list_add_string( + memory_backend->config_list, memory_backend->backend_type); + parse_data.origin_path = memory_backend->origin_path ? + git_config_list_add_string(memory_backend->config_list, + memory_backend->origin_path) : + NULL; + parse_data.config_list = memory_backend->config_list; + parse_data.level = level; + + if ((error = git_config_parse(&parser, NULL, read_variable_cb, + NULL, NULL, &parse_data)) < 0) + goto out; + +out: + git_config_parser_dispose(&parser); + return error; +} + +static int parse_values( + config_memory_backend *memory_backend, + git_config_level_t level) +{ + git_config_list_entry *entry; + const char *eql, *backend_type, *origin_path; + size_t name_len, i; + + backend_type = git_config_list_add_string( + memory_backend->config_list, memory_backend->backend_type); + GIT_ERROR_CHECK_ALLOC(backend_type); + + origin_path = memory_backend->origin_path ? + git_config_list_add_string(memory_backend->config_list, + memory_backend->origin_path) : + NULL; + + for (i = 0; i < memory_backend->values_len; i++) { + eql = strchr(memory_backend->values[i], '='); + name_len = eql - memory_backend->values[i]; + + if (name_len == 0) { + git_error_set(GIT_ERROR_CONFIG, "empty config key"); + return -1; + } + + entry = git__calloc(1, sizeof(git_config_list_entry)); + GIT_ERROR_CHECK_ALLOC(entry); + + entry->base.entry.name = git__strndup(memory_backend->values[i], name_len); + GIT_ERROR_CHECK_ALLOC(entry->base.entry.name); + + if (eql) { + entry->base.entry.value = git__strdup(eql + 1); + GIT_ERROR_CHECK_ALLOC(entry->base.entry.value); + } + + entry->base.entry.level = level; + entry->base.entry.include_depth = 0; + entry->base.entry.backend_type = backend_type; + entry->base.entry.origin_path = origin_path; + entry->base.free = git_config_list_entry_free; + entry->config_list = memory_backend->config_list; + + if (git_config_list_append(memory_backend->config_list, entry) < 0) + return -1; + } + + return 0; +} + +static int config_memory_open(git_config_backend *backend, git_config_level_t level, const git_repository *repo) +{ + config_memory_backend *memory_backend = (config_memory_backend *) backend; + + GIT_UNUSED(repo); + + if (memory_backend->cfg.size > 0 && + parse_config(memory_backend, level) < 0) + return -1; + + if (memory_backend->values_len > 0 && + parse_values(memory_backend, level) < 0) + return -1; + + return 0; +} + +static int config_memory_get(git_config_backend *backend, const char *key, git_config_backend_entry **out) +{ + config_memory_backend *memory_backend = (config_memory_backend *) backend; + git_config_list_entry *entry; + int error; + + if ((error = git_config_list_get(&entry, memory_backend->config_list, key)) != 0) + return error; + + *out = &entry->base; + return 0; +} + +static int config_memory_iterator( + git_config_iterator **iter, + git_config_backend *backend) +{ + config_memory_backend *memory_backend = (config_memory_backend *) backend; + git_config_list *config_list; + int error; + + if ((error = git_config_list_dup(&config_list, memory_backend->config_list)) < 0) + goto out; + + if ((error = git_config_list_iterator_new(iter, config_list)) < 0) + goto out; + +out: + /* Let iterator delete duplicated config_list when it's done */ + git_config_list_free(config_list); + return error; +} + +static int config_memory_set(git_config_backend *backend, const char *name, const char *value) +{ + GIT_UNUSED(backend); + GIT_UNUSED(name); + GIT_UNUSED(value); + return config_error_readonly(); +} + +static int config_memory_set_multivar( + git_config_backend *backend, const char *name, const char *regexp, const char *value) +{ + GIT_UNUSED(backend); + GIT_UNUSED(name); + GIT_UNUSED(regexp); + GIT_UNUSED(value); + return config_error_readonly(); +} + +static int config_memory_delete(git_config_backend *backend, const char *name) +{ + GIT_UNUSED(backend); + GIT_UNUSED(name); + return config_error_readonly(); +} + +static int config_memory_delete_multivar(git_config_backend *backend, const char *name, const char *regexp) +{ + GIT_UNUSED(backend); + GIT_UNUSED(name); + GIT_UNUSED(regexp); + return config_error_readonly(); +} + +static int config_memory_lock(git_config_backend *backend) +{ + GIT_UNUSED(backend); + return config_error_readonly(); +} + +static int config_memory_unlock(git_config_backend *backend, int success) +{ + GIT_UNUSED(backend); + GIT_UNUSED(success); + return config_error_readonly(); +} + +static void config_memory_free(git_config_backend *_backend) +{ + config_memory_backend *backend = (config_memory_backend *)_backend; + + if (backend == NULL) + return; + + git__free(backend->origin_path); + git__free(backend->backend_type); + git_config_list_free(backend->config_list); + git_strlist_free(backend->values, backend->values_len); + git_str_dispose(&backend->cfg); + git__free(backend); +} + +static config_memory_backend *config_backend_new( + git_config_backend_memory_options *opts) +{ + config_memory_backend *backend; + + if ((backend = git__calloc(1, sizeof(config_memory_backend))) == NULL) + return NULL; + + if (git_config_list_new(&backend->config_list) < 0) + goto on_error; + + backend->parent.version = GIT_CONFIG_BACKEND_VERSION; + backend->parent.readonly = 1; + backend->parent.open = config_memory_open; + backend->parent.get = config_memory_get; + backend->parent.set = config_memory_set; + backend->parent.set_multivar = config_memory_set_multivar; + backend->parent.del = config_memory_delete; + backend->parent.del_multivar = config_memory_delete_multivar; + backend->parent.iterator = config_memory_iterator; + backend->parent.lock = config_memory_lock; + backend->parent.unlock = config_memory_unlock; + backend->parent.snapshot = git_config_backend_snapshot; + backend->parent.free = config_memory_free; + + backend->backend_type = git__strdup(opts && opts->backend_type ? + opts->backend_type : "in-memory"); + + if (backend->backend_type == NULL) + goto on_error; + + if (opts && opts->origin_path && + (backend->origin_path = git__strdup(opts->origin_path)) == NULL) + goto on_error; + + return backend; + +on_error: + git_config_list_free(backend->config_list); + git__free(backend->origin_path); + git__free(backend->backend_type); + git__free(backend); + return NULL; +} + +int git_config_backend_from_string( + git_config_backend **out, + const char *cfg, + size_t len, + git_config_backend_memory_options *opts) +{ + config_memory_backend *backend; + + if ((backend = config_backend_new(opts)) == NULL) + return -1; + + if (git_str_set(&backend->cfg, cfg, len) < 0) { + git_config_list_free(backend->config_list); + git__free(backend); + return -1; + } + + *out = (git_config_backend *)backend; + return 0; +} + +int git_config_backend_from_values( + git_config_backend **out, + const char **values, + size_t len, + git_config_backend_memory_options *opts) +{ + config_memory_backend *backend; + + if ((backend = config_backend_new(opts)) == NULL) + return -1; + + if (git_strlist_copy(&backend->values, values, len) < 0) { + git_config_list_free(backend->config_list); + git__free(backend); + return -1; + } + + backend->values_len = len; + + *out = (git_config_backend *)backend; + return 0; +} diff --git a/src/libgit2/config_parse.c b/src/libgit2/config_parse.c new file mode 100644 index 00000000000..390fea3bfb0 --- /dev/null +++ b/src/libgit2/config_parse.c @@ -0,0 +1,582 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "config_parse.h" + +#include + +const char *git_config_escapes = "ntb\"\\"; +const char *git_config_escaped = "\n\t\b\"\\"; + +static void set_parse_error(git_config_parser *reader, int col, const char *error_str) +{ + if (col) + git_error_set(GIT_ERROR_CONFIG, + "failed to parse config file: %s (in %s:%"PRIuZ", column %d)", + error_str, reader->path, reader->ctx.line_num, col); + else + git_error_set(GIT_ERROR_CONFIG, + "failed to parse config file: %s (in %s:%"PRIuZ")", + error_str, reader->path, reader->ctx.line_num); +} + + +GIT_INLINE(int) config_keychar(char c) +{ + return git__isalnum(c) || c == '-'; +} + +static int strip_comments(char *line, int in_quotes) +{ + int quote_count = in_quotes, backslash_count = 0; + char *ptr; + + for (ptr = line; *ptr; ++ptr) { + if (ptr[0] == '"' && ((ptr > line && ptr[-1] != '\\') || ptr == line)) + quote_count++; + + if ((ptr[0] == ';' || ptr[0] == '#') && + (quote_count % 2) == 0 && + (backslash_count % 2) == 0) { + ptr[0] = '\0'; + break; + } + + if (ptr[0] == '\\') + backslash_count++; + else + backslash_count = 0; + } + + /* skip any space at the end */ + while (ptr > line && git__isspace(ptr[-1])) { + ptr--; + } + ptr[0] = '\0'; + + return quote_count; +} + + +static int parse_subsection_header(git_config_parser *reader, const char *line, size_t pos, const char *base_name, char **section_name) +{ + int c, rpos; + const char *first_quote, *last_quote; + const char *line_start = line; + git_str buf = GIT_STR_INIT; + size_t quoted_len, alloc_len, base_name_len = strlen(base_name); + + /* Skip any additional whitespace before our section name */ + while (git__isspace(line[pos])) + pos++; + + /* We should be at the first quotation mark. */ + if (line[pos] != '"') { + set_parse_error(reader, 0, "missing quotation marks in section header"); + goto end_error; + } + + first_quote = &line[pos]; + last_quote = strrchr(line, '"'); + quoted_len = last_quote - first_quote; + + if ((last_quote - line) > INT_MAX) { + set_parse_error(reader, 0, "invalid section header, line too long"); + goto end_error; + } + + if (quoted_len == 0) { + set_parse_error(reader, 0, "missing closing quotation mark in section header"); + goto end_error; + } + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, base_name_len, quoted_len); + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2); + + if (git_str_grow(&buf, alloc_len) < 0 || + git_str_printf(&buf, "%s.", base_name) < 0) + goto end_error; + + rpos = 0; + + line = first_quote; + c = line[++rpos]; + + /* + * At the end of each iteration, whatever is stored in c will be + * added to the string. In case of error, jump to out + */ + do { + + switch (c) { + case 0: + set_parse_error(reader, 0, "unexpected end-of-line in section header"); + goto end_error; + + case '"': + goto end_parse; + + case '\\': + c = line[++rpos]; + + if (c == 0) { + set_parse_error(reader, rpos, "unexpected end-of-line in section header"); + goto end_error; + } + + default: + break; + } + + git_str_putc(&buf, (char)c); + c = line[++rpos]; + } while (line + rpos < last_quote); + +end_parse: + if (git_str_oom(&buf)) + goto end_error; + + if (line[rpos] != '"' || line[rpos + 1] != ']') { + set_parse_error(reader, rpos, "unexpected text after closing quotes"); + git_str_dispose(&buf); + return -1; + } + + *section_name = git_str_detach(&buf); + return (int)(&line[rpos + 2] - line_start); /* rpos is at the closing quote */ + +end_error: + git_str_dispose(&buf); + + return -1; +} + +static int parse_section_header(git_config_parser *reader, char **section_out) +{ + char *name, *name_end; + int name_length, pos; + int result; + char *line; + char c; + size_t line_len; + + git_parse_advance_ws(&reader->ctx); + line = git__strndup(reader->ctx.line, reader->ctx.line_len); + if (line == NULL) + return -1; + + /* find the end of the variable's name */ + name_end = strrchr(line, ']'); + if (name_end == NULL) { + git__free(line); + set_parse_error(reader, 0, "missing ']' in section header"); + return -1; + } + + GIT_ERROR_CHECK_ALLOC_ADD(&line_len, (size_t)(name_end - line), 1); + name = git__malloc(line_len); + GIT_ERROR_CHECK_ALLOC(name); + + name_length = 0; + pos = 0; + + /* Make sure we were given a section header */ + c = line[pos++]; + GIT_ASSERT(c == '['); + + c = line[pos++]; + + do { + if (git__isspace(c)){ + name[name_length] = '\0'; + result = parse_subsection_header(reader, line, pos, name, section_out); + git__free(line); + git__free(name); + return result; + } + + if (!config_keychar(c) && c != '.') { + set_parse_error(reader, pos, "unexpected character in header"); + goto fail_parse; + } + + name[name_length++] = (char)git__tolower(c); + + } while ((c = line[pos++]) != ']'); + + if (line[pos - 1] != ']') { + set_parse_error(reader, pos, "unexpected end of file"); + goto fail_parse; + } + + git__free(line); + + name[name_length] = 0; + *section_out = name; + + return pos; + +fail_parse: + git__free(line); + git__free(name); + return -1; +} + +static int skip_bom(git_parse_ctx *parser) +{ + git_str buf = GIT_STR_INIT_CONST(parser->content, parser->content_len); + git_str_bom_t bom; + int bom_offset = git_str_detect_bom(&bom, &buf); + + if (bom == GIT_STR_BOM_UTF8) + git_parse_advance_chars(parser, bom_offset); + + /* TODO: reference implementation is pretty stupid with BoM */ + + return 0; +} + +/* + (* basic types *) + digit = "0".."9" + integer = digit { digit } + alphabet = "a".."z" + "A" .. "Z" + + section_char = alphabet | "." | "-" + extension_char = (* any character except newline *) + any_char = (* any character *) + variable_char = "alphabet" | "-" + + + (* actual grammar *) + config = { section } + + section = header { definition } + + header = "[" section [subsection | subsection_ext] "]" + + subsection = "." section + subsection_ext = "\"" extension "\"" + + section = section_char { section_char } + extension = extension_char { extension_char } + + definition = variable_name ["=" variable_value] "\n" + + variable_name = variable_char { variable_char } + variable_value = string | boolean | integer + + string = quoted_string | plain_string + quoted_string = "\"" plain_string "\"" + plain_string = { any_char } + + boolean = boolean_true | boolean_false + boolean_true = "yes" | "1" | "true" | "on" + boolean_false = "no" | "0" | "false" | "off" +*/ + +/* '\"' -> '"' etc */ +static int unescape_line(char **out, bool *is_multi, const char *ptr, int *quote_count) +{ + char *str, *fixed; + const char *esc; + size_t ptr_len = strlen(ptr), alloc_len; + + *is_multi = false; + + if (GIT_ADD_SIZET_OVERFLOW(&alloc_len, ptr_len, 1) || + (str = git__malloc(alloc_len)) == NULL) { + return -1; + } + + fixed = str; + + while (*ptr != '\0') { + if (*ptr == '"') { + if (quote_count) + (*quote_count)++; + } else if (*ptr != '\\') { + *fixed++ = *ptr; + } else { + /* backslash, check the next char */ + ptr++; + /* if we're at the end, it's a multiline, so keep the backslash */ + if (*ptr == '\0') { + *is_multi = true; + goto done; + } + if ((esc = strchr(git_config_escapes, *ptr)) != NULL) { + *fixed++ = git_config_escaped[esc - git_config_escapes]; + } else { + git__free(str); + git_error_set(GIT_ERROR_CONFIG, "invalid escape at %s", ptr); + return -1; + } + } + ptr++; + } + +done: + *fixed = '\0'; + *out = str; + + return 0; +} + +static int parse_multiline_variable(git_config_parser *reader, git_str *value, int in_quotes, size_t *line_len) +{ + int quote_count; + bool multiline = true; + + while (multiline) { + char *line = NULL, *proc_line = NULL; + int error; + + /* Check that the next line exists */ + git_parse_advance_line(&reader->ctx); + line = git__strndup(reader->ctx.line, reader->ctx.line_len); + GIT_ERROR_CHECK_ALLOC(line); + if (GIT_ADD_SIZET_OVERFLOW(line_len, *line_len, reader->ctx.line_len)) { + error = -1; + goto out; + } + + /* + * We've reached the end of the file, there is no continuation. + * (this is not an error). + */ + if (line[0] == '\0') { + error = 0; + goto out; + } + + /* If it was just a comment, pretend it didn't exist */ + quote_count = strip_comments(line, in_quotes); + if (line[0] == '\0') + goto next; + + if ((error = unescape_line(&proc_line, &multiline, + line, &in_quotes)) < 0) + goto out; + + /* Add this line to the multiline var */ + if ((error = git_str_puts(value, proc_line)) < 0) + goto out; + +next: + git__free(line); + git__free(proc_line); + in_quotes = quote_count; + continue; + +out: + git__free(line); + git__free(proc_line); + return error; + } + + return 0; +} + +GIT_INLINE(bool) is_namechar(char c) +{ + return git__isalnum(c) || c == '-'; +} + +static int parse_name( + char **name, const char **value, git_config_parser *reader, const char *line) +{ + const char *name_end = line, *value_start; + + *name = NULL; + *value = NULL; + + while (*name_end && is_namechar(*name_end)) + name_end++; + + if (line == name_end) { + set_parse_error(reader, 0, "invalid configuration key"); + return -1; + } + + value_start = name_end; + + while (*value_start && git__isspace(*value_start)) + value_start++; + + if (*value_start == '=') { + *value = value_start + 1; + } else if (*value_start) { + set_parse_error(reader, 0, "invalid configuration key"); + return -1; + } + + if ((*name = git__strndup(line, name_end - line)) == NULL) + return -1; + + return 0; +} + +static int parse_variable(git_config_parser *reader, char **var_name, char **var_value, size_t *line_len) +{ + const char *value_start = NULL; + char *line = NULL, *name = NULL, *value = NULL; + int quote_count, error; + bool multiline; + + *var_name = NULL; + *var_value = NULL; + + git_parse_advance_ws(&reader->ctx); + line = git__strndup(reader->ctx.line, reader->ctx.line_len); + GIT_ERROR_CHECK_ALLOC(line); + + quote_count = strip_comments(line, 0); + + if ((error = parse_name(&name, &value_start, reader, line)) < 0) + goto out; + + /* + * Now, let's try to parse the value + */ + if (value_start != NULL) { + while (git__isspace(value_start[0])) + value_start++; + + if ((error = unescape_line(&value, &multiline, value_start, NULL)) < 0) + goto out; + + if (multiline) { + git_str multi_value = GIT_STR_INIT; + git_str_attach(&multi_value, value, 0); + value = NULL; + + if (parse_multiline_variable(reader, &multi_value, quote_count % 2, line_len) < 0 || + git_str_oom(&multi_value)) { + error = -1; + git_str_dispose(&multi_value); + goto out; + } + + value = git_str_detach(&multi_value); + } + } + + *var_name = name; + *var_value = value; + name = NULL; + value = NULL; + +out: + git__free(name); + git__free(value); + git__free(line); + return error; +} + +int git_config_parser_init(git_config_parser *out, const char *path, const char *data, size_t datalen) +{ + out->path = path; + return git_parse_ctx_init(&out->ctx, data, datalen); +} + +void git_config_parser_dispose(git_config_parser *parser) +{ + git_parse_ctx_clear(&parser->ctx); +} + +int git_config_parse( + git_config_parser *parser, + git_config_parser_section_cb on_section, + git_config_parser_variable_cb on_variable, + git_config_parser_comment_cb on_comment, + git_config_parser_eof_cb on_eof, + void *payload) +{ + git_parse_ctx *ctx; + char *current_section = NULL, *var_name = NULL, *var_value = NULL; + int result = 0; + + ctx = &parser->ctx; + + skip_bom(ctx); + + for (; ctx->remain_len > 0; git_parse_advance_line(ctx)) { + const char *line_start; + size_t line_len; + char c; + + restart: + line_start = ctx->line; + line_len = ctx->line_len; + + /* + * Get either first non-whitespace character or, if that does + * not exist, the first whitespace character. This is required + * to preserve whitespaces when writing back the file. + */ + if (git_parse_peek(&c, ctx, GIT_PARSE_PEEK_SKIP_WHITESPACE) < 0 && + git_parse_peek(&c, ctx, 0) < 0) + continue; + + switch (c) { + case '[': /* section header, new section begins */ + git__free(current_section); + current_section = NULL; + + result = parse_section_header(parser, ¤t_section); + if (result < 0) + break; + + git_parse_advance_chars(ctx, result); + + if (on_section) + result = on_section(parser, current_section, line_start, line_len, payload); + /* + * After we've parsed the section header we may not be + * done with the line. If there's still data in there, + * run the next loop with the rest of the current line + * instead of moving forward. + */ + + if (!git_parse_peek(&c, ctx, GIT_PARSE_PEEK_SKIP_WHITESPACE)) + goto restart; + + break; + + case '\n': /* comment or whitespace-only */ + case '\r': + case ' ': + case '\t': + case ';': + case '#': + if (on_comment) { + result = on_comment(parser, line_start, line_len, payload); + } + break; + + default: /* assume variable declaration */ + if ((result = parse_variable(parser, &var_name, &var_value, &line_len)) == 0 && on_variable) { + result = on_variable(parser, current_section, var_name, var_value, line_start, line_len, payload); + git__free(var_name); + git__free(var_value); + } + + break; + } + + if (result < 0) + goto out; + } + + if (on_eof) + result = on_eof(parser, current_section, payload); + +out: + git__free(current_section); + return result; +} diff --git a/src/libgit2/config_parse.h b/src/libgit2/config_parse.h new file mode 100644 index 00000000000..b791d324512 --- /dev/null +++ b/src/libgit2/config_parse.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_config_parse_h__ +#define INCLUDE_config_parse_h__ + +#include "common.h" + +#include "array.h" +#include "futils.h" +#include "oid.h" +#include "parse.h" + +extern const char *git_config_escapes; +extern const char *git_config_escaped; + +typedef struct { + const char *path; + git_parse_ctx ctx; +} git_config_parser; + +#define GIT_CONFIG_PARSER_INIT { NULL, GIT_PARSE_CTX_INIT } + +typedef int (*git_config_parser_section_cb)( + git_config_parser *parser, + const char *current_section, + const char *line, + size_t line_len, + void *payload); + +typedef int (*git_config_parser_variable_cb)( + git_config_parser *parser, + const char *current_section, + const char *var_name, + const char *var_value, + const char *line, + size_t line_len, + void *payload); + +typedef int (*git_config_parser_comment_cb)( + git_config_parser *parser, + const char *line, + size_t line_len, + void *payload); + +typedef int (*git_config_parser_eof_cb)( + git_config_parser *parser, + const char *current_section, + void *payload); + +int git_config_parser_init(git_config_parser *out, const char *path, const char *data, size_t datalen); +void git_config_parser_dispose(git_config_parser *parser); + +int git_config_parse( + git_config_parser *parser, + git_config_parser_section_cb on_section, + git_config_parser_variable_cb on_variable, + git_config_parser_comment_cb on_comment, + git_config_parser_eof_cb on_eof, + void *payload); + +#endif diff --git a/src/libgit2/config_snapshot.c b/src/libgit2/config_snapshot.c new file mode 100644 index 00000000000..d20984f512d --- /dev/null +++ b/src/libgit2/config_snapshot.c @@ -0,0 +1,200 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "config_backend.h" + +#include "config.h" +#include "config_list.h" + +typedef struct { + git_config_backend parent; + git_mutex values_mutex; + git_config_list *config_list; + git_config_backend *source; +} config_snapshot_backend; + +static int config_error_readonly(void) +{ + git_error_set(GIT_ERROR_CONFIG, "this backend is read-only"); + return -1; +} + +static int config_snapshot_iterator( + git_config_iterator **iter, + struct git_config_backend *backend) +{ + config_snapshot_backend *b = GIT_CONTAINER_OF(backend, config_snapshot_backend, parent); + git_config_list *config_list = NULL; + int error; + + if ((error = git_config_list_dup(&config_list, b->config_list)) < 0 || + (error = git_config_list_iterator_new(iter, config_list)) < 0) + goto out; + +out: + /* Let iterator delete duplicated config_list when it's done */ + git_config_list_free(config_list); + return error; +} + +static int config_snapshot_get( + git_config_backend *cfg, + const char *key, + git_config_backend_entry **out) +{ + config_snapshot_backend *b = GIT_CONTAINER_OF(cfg, config_snapshot_backend, parent); + git_config_list *config_list = NULL; + git_config_list_entry *entry; + int error = 0; + + if (git_mutex_lock(&b->values_mutex) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock config backend"); + return -1; + } + + config_list = b->config_list; + git_config_list_incref(config_list); + git_mutex_unlock(&b->values_mutex); + + if ((error = (git_config_list_get(&entry, config_list, key))) < 0) { + git_config_list_free(config_list); + return error; + } + + *out = &entry->base; + return 0; +} + +static int config_snapshot_set(git_config_backend *cfg, const char *name, const char *value) +{ + GIT_UNUSED(cfg); + GIT_UNUSED(name); + GIT_UNUSED(value); + + return config_error_readonly(); +} + +static int config_snapshot_set_multivar( + git_config_backend *cfg, const char *name, const char *regexp, const char *value) +{ + GIT_UNUSED(cfg); + GIT_UNUSED(name); + GIT_UNUSED(regexp); + GIT_UNUSED(value); + + return config_error_readonly(); +} + +static int config_snapshot_delete_multivar(git_config_backend *cfg, const char *name, const char *regexp) +{ + GIT_UNUSED(cfg); + GIT_UNUSED(name); + GIT_UNUSED(regexp); + + return config_error_readonly(); +} + +static int config_snapshot_delete(git_config_backend *cfg, const char *name) +{ + GIT_UNUSED(cfg); + GIT_UNUSED(name); + + return config_error_readonly(); +} + +static int config_snapshot_lock(git_config_backend *_cfg) +{ + GIT_UNUSED(_cfg); + + return config_error_readonly(); +} + +static int config_snapshot_unlock(git_config_backend *_cfg, int success) +{ + GIT_UNUSED(_cfg); + GIT_UNUSED(success); + + return config_error_readonly(); +} + +static void config_snapshot_free(git_config_backend *_backend) +{ + config_snapshot_backend *backend = GIT_CONTAINER_OF(_backend, config_snapshot_backend, parent); + + if (backend == NULL) + return; + + git_config_list_free(backend->config_list); + git_mutex_free(&backend->values_mutex); + git__free(backend); +} + +static int config_snapshot_open(git_config_backend *cfg, git_config_level_t level, const git_repository *repo) +{ + config_snapshot_backend *b = GIT_CONTAINER_OF(cfg, config_snapshot_backend, parent); + git_config_list *config_list = NULL; + git_config_iterator *it = NULL; + git_config_entry *entry; + int error; + + /* We're just copying data, don't care about the level or repo*/ + GIT_UNUSED(level); + GIT_UNUSED(repo); + + if ((error = git_config_list_new(&config_list)) < 0 || + (error = b->source->iterator(&it, b->source)) < 0) + goto out; + + while ((error = git_config_next(&entry, it)) == 0) + if ((error = git_config_list_dup_entry(config_list, entry)) < 0) + goto out; + + if (error < 0) { + if (error != GIT_ITEROVER) + goto out; + error = 0; + } + + b->config_list = config_list; + +out: + git_config_iterator_free(it); + if (error) + git_config_list_free(config_list); + return error; +} + +int git_config_backend_snapshot(git_config_backend **out, git_config_backend *source) +{ + config_snapshot_backend *backend; + + backend = git__calloc(1, sizeof(config_snapshot_backend)); + GIT_ERROR_CHECK_ALLOC(backend); + + backend->parent.version = GIT_CONFIG_BACKEND_VERSION; + git_mutex_init(&backend->values_mutex); + + backend->source = source; + + backend->parent.readonly = 1; + backend->parent.version = GIT_CONFIG_BACKEND_VERSION; + backend->parent.open = config_snapshot_open; + backend->parent.get = config_snapshot_get; + backend->parent.set = config_snapshot_set; + backend->parent.set_multivar = config_snapshot_set_multivar; + backend->parent.snapshot = git_config_backend_snapshot; + backend->parent.del = config_snapshot_delete; + backend->parent.del_multivar = config_snapshot_delete_multivar; + backend->parent.iterator = config_snapshot_iterator; + backend->parent.lock = config_snapshot_lock; + backend->parent.unlock = config_snapshot_unlock; + backend->parent.free = config_snapshot_free; + + *out = &backend->parent; + + return 0; +} diff --git a/src/libgit2/crlf.c b/src/libgit2/crlf.c new file mode 100644 index 00000000000..1e1f1e84558 --- /dev/null +++ b/src/libgit2/crlf.c @@ -0,0 +1,426 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "git2/attr.h" +#include "git2/blob.h" +#include "git2/index.h" +#include "git2/sys/filter.h" + +#include "buf.h" +#include "futils.h" +#include "hash.h" +#include "filter.h" +#include "repository.h" + +typedef enum { + GIT_CRLF_UNDEFINED, + GIT_CRLF_BINARY, + GIT_CRLF_TEXT, + GIT_CRLF_TEXT_INPUT, + GIT_CRLF_TEXT_CRLF, + GIT_CRLF_AUTO, + GIT_CRLF_AUTO_INPUT, + GIT_CRLF_AUTO_CRLF +} git_crlf_t; + +struct crlf_attrs { + int attr_action; /* the .gitattributes setting */ + int crlf_action; /* the core.autocrlf setting */ + + int auto_crlf; + int safe_crlf; + int core_eol; +}; + +struct crlf_filter { + git_filter f; +}; + +static git_crlf_t check_crlf(const char *value) +{ + if (GIT_ATTR_IS_TRUE(value)) + return GIT_CRLF_TEXT; + else if (GIT_ATTR_IS_FALSE(value)) + return GIT_CRLF_BINARY; + else if (GIT_ATTR_IS_UNSPECIFIED(value)) + ; + else if (strcmp(value, "input") == 0) + return GIT_CRLF_TEXT_INPUT; + else if (strcmp(value, "auto") == 0) + return GIT_CRLF_AUTO; + + return GIT_CRLF_UNDEFINED; +} + +static git_configmap_value check_eol(const char *value) +{ + if (GIT_ATTR_IS_UNSPECIFIED(value)) + ; + else if (strcmp(value, "lf") == 0) + return GIT_EOL_LF; + else if (strcmp(value, "crlf") == 0) + return GIT_EOL_CRLF; + + return GIT_EOL_UNSET; +} + +static int has_cr_in_index(const git_filter_source *src) +{ + git_repository *repo = git_filter_source_repo(src); + const char *path = git_filter_source_path(src); + git_index *index; + const git_index_entry *entry; + git_blob *blob; + const void *blobcontent; + git_object_size_t blobsize; + bool found_cr; + + if (!path) + return false; + + if (git_repository_index__weakptr(&index, repo) < 0) { + git_error_clear(); + return false; + } + + if (!(entry = git_index_get_bypath(index, path, 0)) && + !(entry = git_index_get_bypath(index, path, 1))) + return false; + + if (!S_ISREG(entry->mode)) /* don't crlf filter non-blobs */ + return true; + + if (git_blob_lookup(&blob, repo, &entry->id) < 0) + return false; + + blobcontent = git_blob_rawcontent(blob); + blobsize = git_blob_rawsize(blob); + if (!git__is_sizet(blobsize)) + blobsize = (size_t)-1; + + found_cr = (blobcontent != NULL && + blobsize > 0 && + memchr(blobcontent, '\r', (size_t)blobsize) != NULL); + + git_blob_free(blob); + return found_cr; +} + +static int text_eol_is_crlf(struct crlf_attrs *ca) +{ + if (ca->auto_crlf == GIT_AUTO_CRLF_TRUE) + return 1; + else if (ca->auto_crlf == GIT_AUTO_CRLF_INPUT) + return 0; + + if (ca->core_eol == GIT_EOL_CRLF) + return 1; + if (ca->core_eol == GIT_EOL_UNSET && GIT_EOL_NATIVE == GIT_EOL_CRLF) + return 1; + + return 0; +} + +static git_configmap_value output_eol(struct crlf_attrs *ca) +{ + switch (ca->crlf_action) { + case GIT_CRLF_BINARY: + return GIT_EOL_UNSET; + case GIT_CRLF_TEXT_CRLF: + return GIT_EOL_CRLF; + case GIT_CRLF_TEXT_INPUT: + return GIT_EOL_LF; + case GIT_CRLF_UNDEFINED: + case GIT_CRLF_AUTO_CRLF: + return GIT_EOL_CRLF; + case GIT_CRLF_AUTO_INPUT: + return GIT_EOL_LF; + case GIT_CRLF_TEXT: + case GIT_CRLF_AUTO: + return text_eol_is_crlf(ca) ? GIT_EOL_CRLF : GIT_EOL_LF; + } + + /* TODO: warn when available */ + return ca->core_eol; +} + +GIT_INLINE(int) check_safecrlf( + struct crlf_attrs *ca, + const git_filter_source *src, + git_str_text_stats *stats) +{ + const char *filename = git_filter_source_path(src); + + if (!ca->safe_crlf) + return 0; + + if (output_eol(ca) == GIT_EOL_LF) { + /* + * CRLFs would not be restored by checkout: + * check if we'd remove CRLFs + */ + if (stats->crlf) { + if (ca->safe_crlf == GIT_SAFE_CRLF_WARN) { + /* TODO: issue a warning when available */ + } else { + if (filename && *filename) + git_error_set( + GIT_ERROR_FILTER, "CRLF would be replaced by LF in '%s'", + filename); + else + git_error_set( + GIT_ERROR_FILTER, "CRLF would be replaced by LF"); + + return -1; + } + } + } else if (output_eol(ca) == GIT_EOL_CRLF) { + /* + * CRLFs would be added by checkout: + * check if we have "naked" LFs + */ + if (stats->crlf != stats->lf) { + if (ca->safe_crlf == GIT_SAFE_CRLF_WARN) { + /* TODO: issue a warning when available */ + } else { + if (filename && *filename) + git_error_set( + GIT_ERROR_FILTER, "LF would be replaced by CRLF in '%s'", + filename); + else + git_error_set( + GIT_ERROR_FILTER, "LF would be replaced by CRLF"); + + return -1; + } + } + } + + return 0; +} + +static int crlf_apply_to_odb( + struct crlf_attrs *ca, + git_str *to, + const git_str *from, + const git_filter_source *src) +{ + git_str_text_stats stats; + bool is_binary; + int error; + + /* Binary attribute? Empty file? Nothing to do */ + if (ca->crlf_action == GIT_CRLF_BINARY || from->size == 0) + return GIT_PASSTHROUGH; + + is_binary = git_str_gather_text_stats(&stats, from, false); + + /* Heuristics to see if we can skip the conversion. + * Straight from Core Git. + */ + if (ca->crlf_action == GIT_CRLF_AUTO || + ca->crlf_action == GIT_CRLF_AUTO_INPUT || + ca->crlf_action == GIT_CRLF_AUTO_CRLF) { + + if (is_binary) + return GIT_PASSTHROUGH; + + /* + * If the file in the index has any CR in it, do not convert. + * This is the new safer autocrlf handling. + */ + if (has_cr_in_index(src)) + return GIT_PASSTHROUGH; + } + + if ((error = check_safecrlf(ca, src, &stats)) < 0) + return error; + + /* If there are no CR characters to filter out, then just pass */ + if (!stats.crlf) + return GIT_PASSTHROUGH; + + /* Actually drop the carriage returns */ + return git_str_crlf_to_lf(to, from); +} + +static int crlf_apply_to_workdir( + struct crlf_attrs *ca, + git_str *to, + const git_str *from) +{ + git_str_text_stats stats; + bool is_binary; + + /* Empty file? Nothing to do. */ + if (git_str_len(from) == 0 || output_eol(ca) != GIT_EOL_CRLF) + return GIT_PASSTHROUGH; + + is_binary = git_str_gather_text_stats(&stats, from, false); + + /* If there are no LFs, or all LFs are part of a CRLF, nothing to do */ + if (stats.lf == 0 || stats.lf == stats.crlf) + return GIT_PASSTHROUGH; + + if (ca->crlf_action == GIT_CRLF_AUTO || + ca->crlf_action == GIT_CRLF_AUTO_INPUT || + ca->crlf_action == GIT_CRLF_AUTO_CRLF) { + + /* If we have any existing CR or CRLF line endings, do nothing */ + if (stats.cr > 0) + return GIT_PASSTHROUGH; + + /* Don't filter binary files */ + if (is_binary) + return GIT_PASSTHROUGH; + } + + return git_str_lf_to_crlf(to, from); +} + +static int convert_attrs( + struct crlf_attrs *ca, + const char **attr_values, + const git_filter_source *src) +{ + int error; + + memset(ca, 0, sizeof(struct crlf_attrs)); + + if ((error = git_repository__configmap_lookup(&ca->auto_crlf, + git_filter_source_repo(src), GIT_CONFIGMAP_AUTO_CRLF)) < 0 || + (error = git_repository__configmap_lookup(&ca->safe_crlf, + git_filter_source_repo(src), GIT_CONFIGMAP_SAFE_CRLF)) < 0 || + (error = git_repository__configmap_lookup(&ca->core_eol, + git_filter_source_repo(src), GIT_CONFIGMAP_EOL)) < 0) + return error; + + /* downgrade FAIL to WARN if ALLOW_UNSAFE option is used */ + if ((git_filter_source_flags(src) & GIT_FILTER_ALLOW_UNSAFE) && + ca->safe_crlf == GIT_SAFE_CRLF_FAIL) + ca->safe_crlf = GIT_SAFE_CRLF_WARN; + + if (attr_values) { + /* load the text attribute */ + ca->crlf_action = check_crlf(attr_values[2]); /* text */ + + if (ca->crlf_action == GIT_CRLF_UNDEFINED) + ca->crlf_action = check_crlf(attr_values[0]); /* crlf */ + + if (ca->crlf_action != GIT_CRLF_BINARY) { + /* load the eol attribute */ + int eol_attr = check_eol(attr_values[1]); + + if (ca->crlf_action == GIT_CRLF_AUTO && eol_attr == GIT_EOL_LF) + ca->crlf_action = GIT_CRLF_AUTO_INPUT; + else if (ca->crlf_action == GIT_CRLF_AUTO && eol_attr == GIT_EOL_CRLF) + ca->crlf_action = GIT_CRLF_AUTO_CRLF; + else if (eol_attr == GIT_EOL_LF) + ca->crlf_action = GIT_CRLF_TEXT_INPUT; + else if (eol_attr == GIT_EOL_CRLF) + ca->crlf_action = GIT_CRLF_TEXT_CRLF; + } + + ca->attr_action = ca->crlf_action; + } else { + ca->crlf_action = GIT_CRLF_UNDEFINED; + } + + if (ca->crlf_action == GIT_CRLF_TEXT) + ca->crlf_action = text_eol_is_crlf(ca) ? GIT_CRLF_TEXT_CRLF : GIT_CRLF_TEXT_INPUT; + if (ca->crlf_action == GIT_CRLF_UNDEFINED && ca->auto_crlf == GIT_AUTO_CRLF_FALSE) + ca->crlf_action = GIT_CRLF_BINARY; + if (ca->crlf_action == GIT_CRLF_UNDEFINED && ca->auto_crlf == GIT_AUTO_CRLF_TRUE) + ca->crlf_action = GIT_CRLF_AUTO_CRLF; + if (ca->crlf_action == GIT_CRLF_UNDEFINED && ca->auto_crlf == GIT_AUTO_CRLF_INPUT) + ca->crlf_action = GIT_CRLF_AUTO_INPUT; + + return 0; +} + +static int crlf_check( + git_filter *self, + void **payload, /* points to NULL ptr on entry, may be set */ + const git_filter_source *src, + const char **attr_values) +{ + struct crlf_attrs ca; + + GIT_UNUSED(self); + + convert_attrs(&ca, attr_values, src); + + if (ca.crlf_action == GIT_CRLF_BINARY) + return GIT_PASSTHROUGH; + + *payload = git__malloc(sizeof(ca)); + GIT_ERROR_CHECK_ALLOC(*payload); + memcpy(*payload, &ca, sizeof(ca)); + + return 0; +} + +static int crlf_apply( + git_filter *self, + void **payload, /* may be read and/or set */ + git_str *to, + const git_str *from, + const git_filter_source *src) +{ + int error = 0; + + /* initialize payload in case `check` was bypassed */ + if (!*payload) { + if ((error = crlf_check(self, payload, src, NULL)) < 0) + return error; + } + + if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE) + error = crlf_apply_to_workdir(*payload, to, from); + else + error = crlf_apply_to_odb(*payload, to, from, src); + + return error; +} + +static int crlf_stream( + git_writestream **out, + git_filter *self, + void **payload, + const git_filter_source *src, + git_writestream *next) +{ + return git_filter_buffered_stream_new(out, + self, crlf_apply, NULL, payload, src, next); +} + +static void crlf_cleanup( + git_filter *self, + void *payload) +{ + GIT_UNUSED(self); + git__free(payload); +} + +git_filter *git_crlf_filter_new(void) +{ + struct crlf_filter *f = git__calloc(1, sizeof(struct crlf_filter)); + if (f == NULL) + return NULL; + + f->f.version = GIT_FILTER_VERSION; + f->f.attributes = "crlf eol text"; + f->f.initialize = NULL; + f->f.shutdown = git_filter_free; + f->f.check = crlf_check; + f->f.stream = crlf_stream; + f->f.cleanup = crlf_cleanup; + + return (git_filter *)f; +} diff --git a/src/libgit2/delta.c b/src/libgit2/delta.c new file mode 100644 index 00000000000..1fd2278c824 --- /dev/null +++ b/src/libgit2/delta.c @@ -0,0 +1,632 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "delta.h" + +/* maximum hash entry list for the same hash bucket */ +#define HASH_LIMIT 64 + +#define RABIN_SHIFT 23 +#define RABIN_WINDOW 16 + +static const unsigned int T[256] = { + 0x00000000, 0xab59b4d1, 0x56b369a2, 0xfdeadd73, 0x063f6795, 0xad66d344, + 0x508c0e37, 0xfbd5bae6, 0x0c7ecf2a, 0xa7277bfb, 0x5acda688, 0xf1941259, + 0x0a41a8bf, 0xa1181c6e, 0x5cf2c11d, 0xf7ab75cc, 0x18fd9e54, 0xb3a42a85, + 0x4e4ef7f6, 0xe5174327, 0x1ec2f9c1, 0xb59b4d10, 0x48719063, 0xe32824b2, + 0x1483517e, 0xbfdae5af, 0x423038dc, 0xe9698c0d, 0x12bc36eb, 0xb9e5823a, + 0x440f5f49, 0xef56eb98, 0x31fb3ca8, 0x9aa28879, 0x6748550a, 0xcc11e1db, + 0x37c45b3d, 0x9c9defec, 0x6177329f, 0xca2e864e, 0x3d85f382, 0x96dc4753, + 0x6b369a20, 0xc06f2ef1, 0x3bba9417, 0x90e320c6, 0x6d09fdb5, 0xc6504964, + 0x2906a2fc, 0x825f162d, 0x7fb5cb5e, 0xd4ec7f8f, 0x2f39c569, 0x846071b8, + 0x798aaccb, 0xd2d3181a, 0x25786dd6, 0x8e21d907, 0x73cb0474, 0xd892b0a5, + 0x23470a43, 0x881ebe92, 0x75f463e1, 0xdeadd730, 0x63f67950, 0xc8afcd81, + 0x354510f2, 0x9e1ca423, 0x65c91ec5, 0xce90aa14, 0x337a7767, 0x9823c3b6, + 0x6f88b67a, 0xc4d102ab, 0x393bdfd8, 0x92626b09, 0x69b7d1ef, 0xc2ee653e, + 0x3f04b84d, 0x945d0c9c, 0x7b0be704, 0xd05253d5, 0x2db88ea6, 0x86e13a77, + 0x7d348091, 0xd66d3440, 0x2b87e933, 0x80de5de2, 0x7775282e, 0xdc2c9cff, + 0x21c6418c, 0x8a9ff55d, 0x714a4fbb, 0xda13fb6a, 0x27f92619, 0x8ca092c8, + 0x520d45f8, 0xf954f129, 0x04be2c5a, 0xafe7988b, 0x5432226d, 0xff6b96bc, + 0x02814bcf, 0xa9d8ff1e, 0x5e738ad2, 0xf52a3e03, 0x08c0e370, 0xa39957a1, + 0x584ced47, 0xf3155996, 0x0eff84e5, 0xa5a63034, 0x4af0dbac, 0xe1a96f7d, + 0x1c43b20e, 0xb71a06df, 0x4ccfbc39, 0xe79608e8, 0x1a7cd59b, 0xb125614a, + 0x468e1486, 0xedd7a057, 0x103d7d24, 0xbb64c9f5, 0x40b17313, 0xebe8c7c2, + 0x16021ab1, 0xbd5bae60, 0x6cb54671, 0xc7ecf2a0, 0x3a062fd3, 0x915f9b02, + 0x6a8a21e4, 0xc1d39535, 0x3c394846, 0x9760fc97, 0x60cb895b, 0xcb923d8a, + 0x3678e0f9, 0x9d215428, 0x66f4eece, 0xcdad5a1f, 0x3047876c, 0x9b1e33bd, + 0x7448d825, 0xdf116cf4, 0x22fbb187, 0x89a20556, 0x7277bfb0, 0xd92e0b61, + 0x24c4d612, 0x8f9d62c3, 0x7836170f, 0xd36fa3de, 0x2e857ead, 0x85dcca7c, + 0x7e09709a, 0xd550c44b, 0x28ba1938, 0x83e3ade9, 0x5d4e7ad9, 0xf617ce08, + 0x0bfd137b, 0xa0a4a7aa, 0x5b711d4c, 0xf028a99d, 0x0dc274ee, 0xa69bc03f, + 0x5130b5f3, 0xfa690122, 0x0783dc51, 0xacda6880, 0x570fd266, 0xfc5666b7, + 0x01bcbbc4, 0xaae50f15, 0x45b3e48d, 0xeeea505c, 0x13008d2f, 0xb85939fe, + 0x438c8318, 0xe8d537c9, 0x153feaba, 0xbe665e6b, 0x49cd2ba7, 0xe2949f76, + 0x1f7e4205, 0xb427f6d4, 0x4ff24c32, 0xe4abf8e3, 0x19412590, 0xb2189141, + 0x0f433f21, 0xa41a8bf0, 0x59f05683, 0xf2a9e252, 0x097c58b4, 0xa225ec65, + 0x5fcf3116, 0xf49685c7, 0x033df00b, 0xa86444da, 0x558e99a9, 0xfed72d78, + 0x0502979e, 0xae5b234f, 0x53b1fe3c, 0xf8e84aed, 0x17bea175, 0xbce715a4, + 0x410dc8d7, 0xea547c06, 0x1181c6e0, 0xbad87231, 0x4732af42, 0xec6b1b93, + 0x1bc06e5f, 0xb099da8e, 0x4d7307fd, 0xe62ab32c, 0x1dff09ca, 0xb6a6bd1b, + 0x4b4c6068, 0xe015d4b9, 0x3eb80389, 0x95e1b758, 0x680b6a2b, 0xc352defa, + 0x3887641c, 0x93ded0cd, 0x6e340dbe, 0xc56db96f, 0x32c6cca3, 0x999f7872, + 0x6475a501, 0xcf2c11d0, 0x34f9ab36, 0x9fa01fe7, 0x624ac294, 0xc9137645, + 0x26459ddd, 0x8d1c290c, 0x70f6f47f, 0xdbaf40ae, 0x207afa48, 0x8b234e99, + 0x76c993ea, 0xdd90273b, 0x2a3b52f7, 0x8162e626, 0x7c883b55, 0xd7d18f84, + 0x2c043562, 0x875d81b3, 0x7ab75cc0, 0xd1eee811 +}; + +static const unsigned int U[256] = { + 0x00000000, 0x7eb5200d, 0x5633f4cb, 0x2886d4c6, 0x073e5d47, 0x798b7d4a, + 0x510da98c, 0x2fb88981, 0x0e7cba8e, 0x70c99a83, 0x584f4e45, 0x26fa6e48, + 0x0942e7c9, 0x77f7c7c4, 0x5f711302, 0x21c4330f, 0x1cf9751c, 0x624c5511, + 0x4aca81d7, 0x347fa1da, 0x1bc7285b, 0x65720856, 0x4df4dc90, 0x3341fc9d, + 0x1285cf92, 0x6c30ef9f, 0x44b63b59, 0x3a031b54, 0x15bb92d5, 0x6b0eb2d8, + 0x4388661e, 0x3d3d4613, 0x39f2ea38, 0x4747ca35, 0x6fc11ef3, 0x11743efe, + 0x3eccb77f, 0x40799772, 0x68ff43b4, 0x164a63b9, 0x378e50b6, 0x493b70bb, + 0x61bda47d, 0x1f088470, 0x30b00df1, 0x4e052dfc, 0x6683f93a, 0x1836d937, + 0x250b9f24, 0x5bbebf29, 0x73386bef, 0x0d8d4be2, 0x2235c263, 0x5c80e26e, + 0x740636a8, 0x0ab316a5, 0x2b7725aa, 0x55c205a7, 0x7d44d161, 0x03f1f16c, + 0x2c4978ed, 0x52fc58e0, 0x7a7a8c26, 0x04cfac2b, 0x73e5d470, 0x0d50f47d, + 0x25d620bb, 0x5b6300b6, 0x74db8937, 0x0a6ea93a, 0x22e87dfc, 0x5c5d5df1, + 0x7d996efe, 0x032c4ef3, 0x2baa9a35, 0x551fba38, 0x7aa733b9, 0x041213b4, + 0x2c94c772, 0x5221e77f, 0x6f1ca16c, 0x11a98161, 0x392f55a7, 0x479a75aa, + 0x6822fc2b, 0x1697dc26, 0x3e1108e0, 0x40a428ed, 0x61601be2, 0x1fd53bef, + 0x3753ef29, 0x49e6cf24, 0x665e46a5, 0x18eb66a8, 0x306db26e, 0x4ed89263, + 0x4a173e48, 0x34a21e45, 0x1c24ca83, 0x6291ea8e, 0x4d29630f, 0x339c4302, + 0x1b1a97c4, 0x65afb7c9, 0x446b84c6, 0x3adea4cb, 0x1258700d, 0x6ced5000, + 0x4355d981, 0x3de0f98c, 0x15662d4a, 0x6bd30d47, 0x56ee4b54, 0x285b6b59, + 0x00ddbf9f, 0x7e689f92, 0x51d01613, 0x2f65361e, 0x07e3e2d8, 0x7956c2d5, + 0x5892f1da, 0x2627d1d7, 0x0ea10511, 0x7014251c, 0x5facac9d, 0x21198c90, + 0x099f5856, 0x772a785b, 0x4c921c31, 0x32273c3c, 0x1aa1e8fa, 0x6414c8f7, + 0x4bac4176, 0x3519617b, 0x1d9fb5bd, 0x632a95b0, 0x42eea6bf, 0x3c5b86b2, + 0x14dd5274, 0x6a687279, 0x45d0fbf8, 0x3b65dbf5, 0x13e30f33, 0x6d562f3e, + 0x506b692d, 0x2ede4920, 0x06589de6, 0x78edbdeb, 0x5755346a, 0x29e01467, + 0x0166c0a1, 0x7fd3e0ac, 0x5e17d3a3, 0x20a2f3ae, 0x08242768, 0x76910765, + 0x59298ee4, 0x279caee9, 0x0f1a7a2f, 0x71af5a22, 0x7560f609, 0x0bd5d604, + 0x235302c2, 0x5de622cf, 0x725eab4e, 0x0ceb8b43, 0x246d5f85, 0x5ad87f88, + 0x7b1c4c87, 0x05a96c8a, 0x2d2fb84c, 0x539a9841, 0x7c2211c0, 0x029731cd, + 0x2a11e50b, 0x54a4c506, 0x69998315, 0x172ca318, 0x3faa77de, 0x411f57d3, + 0x6ea7de52, 0x1012fe5f, 0x38942a99, 0x46210a94, 0x67e5399b, 0x19501996, + 0x31d6cd50, 0x4f63ed5d, 0x60db64dc, 0x1e6e44d1, 0x36e89017, 0x485db01a, + 0x3f77c841, 0x41c2e84c, 0x69443c8a, 0x17f11c87, 0x38499506, 0x46fcb50b, + 0x6e7a61cd, 0x10cf41c0, 0x310b72cf, 0x4fbe52c2, 0x67388604, 0x198da609, + 0x36352f88, 0x48800f85, 0x6006db43, 0x1eb3fb4e, 0x238ebd5d, 0x5d3b9d50, + 0x75bd4996, 0x0b08699b, 0x24b0e01a, 0x5a05c017, 0x728314d1, 0x0c3634dc, + 0x2df207d3, 0x534727de, 0x7bc1f318, 0x0574d315, 0x2acc5a94, 0x54797a99, + 0x7cffae5f, 0x024a8e52, 0x06852279, 0x78300274, 0x50b6d6b2, 0x2e03f6bf, + 0x01bb7f3e, 0x7f0e5f33, 0x57888bf5, 0x293dabf8, 0x08f998f7, 0x764cb8fa, + 0x5eca6c3c, 0x207f4c31, 0x0fc7c5b0, 0x7172e5bd, 0x59f4317b, 0x27411176, + 0x1a7c5765, 0x64c97768, 0x4c4fa3ae, 0x32fa83a3, 0x1d420a22, 0x63f72a2f, + 0x4b71fee9, 0x35c4dee4, 0x1400edeb, 0x6ab5cde6, 0x42331920, 0x3c86392d, + 0x133eb0ac, 0x6d8b90a1, 0x450d4467, 0x3bb8646a +}; + +struct index_entry { + const unsigned char *ptr; + unsigned int val; + struct index_entry *next; +}; + +struct git_delta_index { + unsigned long memsize; + const void *src_buf; + size_t src_size; + unsigned int hash_mask; + struct index_entry *hash[GIT_FLEX_ARRAY]; +}; + +static int lookup_index_alloc( + void **out, unsigned long *out_len, size_t entries, size_t hash_count) +{ + size_t entries_len, hash_len, index_len; + + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&entries_len, entries, sizeof(struct index_entry)); + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&hash_len, hash_count, sizeof(struct index_entry *)); + + GIT_ERROR_CHECK_ALLOC_ADD(&index_len, sizeof(struct git_delta_index), entries_len); + GIT_ERROR_CHECK_ALLOC_ADD(&index_len, index_len, hash_len); + + if (!git__is_ulong(index_len)) { + git_error_set(GIT_ERROR_NOMEMORY, "overly large delta"); + return -1; + } + + *out = git__malloc(index_len); + GIT_ERROR_CHECK_ALLOC(*out); + + *out_len = (unsigned long)index_len; + return 0; +} + +int git_delta_index_init( + git_delta_index **out, const void *buf, size_t bufsize) +{ + unsigned int i, hsize, hmask, entries, prev_val, *hash_count; + const unsigned char *data, *buffer = buf; + struct git_delta_index *index; + struct index_entry *entry, **hash; + void *mem; + unsigned long memsize; + + *out = NULL; + + if (!buf || !bufsize) + return 0; + + /* Determine index hash size. Note that indexing skips the + first byte to allow for optimizing the rabin polynomial + initialization in create_delta(). */ + entries = (unsigned int)(bufsize - 1) / RABIN_WINDOW; + if (bufsize >= 0xffffffffUL) { + /* + * Current delta format can't encode offsets into + * reference buffer with more than 32 bits. + */ + entries = 0xfffffffeU / RABIN_WINDOW; + } + hsize = entries / 4; + for (i = 4; i < 31 && (1u << i) < hsize; i++); + hsize = 1 << i; + hmask = hsize - 1; + + if (lookup_index_alloc(&mem, &memsize, entries, hsize) < 0) + return -1; + + index = mem; + mem = index->hash; + hash = mem; + mem = hash + hsize; + entry = mem; + + index->memsize = memsize; + index->src_buf = buf; + index->src_size = bufsize; + index->hash_mask = hmask; + memset(hash, 0, hsize * sizeof(*hash)); + + /* allocate an array to count hash entries */ + hash_count = git__calloc(hsize, sizeof(*hash_count)); + if (!hash_count) { + git__free(index); + return -1; + } + + /* then populate the index */ + prev_val = ~0; + for (data = buffer + entries * RABIN_WINDOW - RABIN_WINDOW; + data >= buffer; + data -= RABIN_WINDOW) { + unsigned int val = 0; + for (i = 1; i <= RABIN_WINDOW; i++) + val = ((val << 8) | data[i]) ^ T[val >> RABIN_SHIFT]; + if (val == prev_val) { + /* keep the lowest of consecutive identical blocks */ + entry[-1].ptr = data + RABIN_WINDOW; + } else { + prev_val = val; + i = val & hmask; + entry->ptr = data + RABIN_WINDOW; + entry->val = val; + entry->next = hash[i]; + hash[i] = entry++; + hash_count[i]++; + } + } + + /* + * Determine a limit on the number of entries in the same hash + * bucket. This guard us against patological data sets causing + * really bad hash distribution with most entries in the same hash + * bucket that would bring us to O(m*n) computing costs (m and n + * corresponding to reference and target buffer sizes). + * + * Make sure none of the hash buckets has more entries than + * we're willing to test. Otherwise we cull the entry list + * uniformly to still preserve a good repartition across + * the reference buffer. + */ + for (i = 0; i < hsize; i++) { + if (hash_count[i] < HASH_LIMIT) + continue; + + entry = hash[i]; + do { + struct index_entry *keep = entry; + int skip = hash_count[i] / HASH_LIMIT / 2; + do { + entry = entry->next; + } while(--skip && entry); + keep->next = entry; + } while (entry); + } + git__free(hash_count); + + *out = index; + return 0; +} + +void git_delta_index_free(git_delta_index *index) +{ + git__free(index); +} + +size_t git_delta_index_size(git_delta_index *index) +{ + GIT_ASSERT_ARG(index); + + return index->memsize; +} + +/* + * The maximum size for any opcode sequence, including the initial header + * plus rabin window plus biggest copy. + */ +#define MAX_OP_SIZE (5 + 5 + 1 + RABIN_WINDOW + 7) + +int git_delta_create_from_index( + void **out, + size_t *out_len, + const struct git_delta_index *index, + const void *trg_buf, + size_t trg_size, + size_t max_size) +{ + unsigned int i, bufpos, bufsize, moff, msize, val; + int inscnt; + const unsigned char *ref_data, *ref_top, *data, *top; + unsigned char *buf; + + *out = NULL; + *out_len = 0; + + if (!trg_buf || !trg_size) + return 0; + + if (index->src_size > UINT_MAX || + trg_size > UINT_MAX || + max_size > (UINT_MAX - MAX_OP_SIZE - 1)) { + git_error_set(GIT_ERROR_INVALID, "buffer sizes too large for delta processing"); + return -1; + } + + bufpos = 0; + bufsize = 8192; + if (max_size && bufsize >= max_size) + bufsize = (unsigned int)(max_size + MAX_OP_SIZE + 1); + buf = git__malloc(bufsize); + GIT_ERROR_CHECK_ALLOC(buf); + + /* store reference buffer size */ + i = (unsigned int)index->src_size; + while (i >= 0x80) { + buf[bufpos++] = i | 0x80; + i >>= 7; + } + buf[bufpos++] = i; + + /* store target buffer size */ + i = (unsigned int)trg_size; + while (i >= 0x80) { + buf[bufpos++] = i | 0x80; + i >>= 7; + } + buf[bufpos++] = i; + + ref_data = index->src_buf; + ref_top = ref_data + index->src_size; + data = trg_buf; + top = (const unsigned char *) trg_buf + trg_size; + + bufpos++; + val = 0; + for (i = 0; i < RABIN_WINDOW && data < top; i++, data++) { + buf[bufpos++] = *data; + val = ((val << 8) | *data) ^ T[val >> RABIN_SHIFT]; + } + inscnt = i; + + moff = 0; + msize = 0; + while (data < top) { + if (msize < 4096) { + struct index_entry *entry; + val ^= U[data[-RABIN_WINDOW]]; + val = ((val << 8) | *data) ^ T[val >> RABIN_SHIFT]; + i = val & index->hash_mask; + for (entry = index->hash[i]; entry; entry = entry->next) { + const unsigned char *ref = entry->ptr; + const unsigned char *src = data; + unsigned int ref_size = (unsigned int)(ref_top - ref); + if (entry->val != val) + continue; + if (ref_size > (unsigned int)(top - src)) + ref_size = (unsigned int)(top - src); + if (ref_size <= msize) + break; + while (ref_size-- && *src++ == *ref) + ref++; + if (msize < (unsigned int)(ref - entry->ptr)) { + /* this is our best match so far */ + msize = (unsigned int)(ref - entry->ptr); + moff = (unsigned int)(entry->ptr - ref_data); + if (msize >= 4096) /* good enough */ + break; + } + } + } + + if (msize < 4) { + if (!inscnt) + bufpos++; + buf[bufpos++] = *data++; + inscnt++; + if (inscnt == 0x7f) { + buf[bufpos - inscnt - 1] = inscnt; + inscnt = 0; + } + msize = 0; + } else { + unsigned int left; + unsigned char *op; + + if (inscnt) { + while (moff && ref_data[moff-1] == data[-1]) { + /* we can match one byte back */ + msize++; + moff--; + data--; + bufpos--; + if (--inscnt) + continue; + bufpos--; /* remove count slot */ + inscnt--; /* make it -1 */ + break; + } + buf[bufpos - inscnt - 1] = inscnt; + inscnt = 0; + } + + /* A copy op is currently limited to 64KB (pack v2) */ + left = (msize < 0x10000) ? 0 : (msize - 0x10000); + msize -= left; + + op = buf + bufpos++; + i = 0x80; + + if (moff & 0x000000ff) + buf[bufpos++] = moff >> 0, i |= 0x01; + if (moff & 0x0000ff00) + buf[bufpos++] = moff >> 8, i |= 0x02; + if (moff & 0x00ff0000) + buf[bufpos++] = moff >> 16, i |= 0x04; + if (moff & 0xff000000) + buf[bufpos++] = moff >> 24, i |= 0x08; + + if (msize & 0x00ff) + buf[bufpos++] = msize >> 0, i |= 0x10; + if (msize & 0xff00) + buf[bufpos++] = msize >> 8, i |= 0x20; + + *op = i; + + data += msize; + moff += msize; + msize = left; + + if (msize < 4096) { + int j; + val = 0; + for (j = -RABIN_WINDOW; j < 0; j++) + val = ((val << 8) | data[j]) + ^ T[val >> RABIN_SHIFT]; + } + } + + if (bufpos >= bufsize - MAX_OP_SIZE) { + void *tmp = buf; + bufsize = bufsize * 3 / 2; + if (max_size && bufsize >= max_size) + bufsize = (unsigned int)(max_size + MAX_OP_SIZE + 1); + if (max_size && bufpos > max_size) + break; + buf = git__realloc(buf, bufsize); + if (!buf) { + git__free(tmp); + return -1; + } + } + } + + if (inscnt) + buf[bufpos - inscnt - 1] = inscnt; + + if (max_size && bufpos > max_size) { + git_error_set(GIT_ERROR_NOMEMORY, "delta would be larger than maximum size"); + git__free(buf); + return GIT_EBUFS; + } + + *out_len = bufpos; + *out = buf; + return 0; +} + +/* +* Delta application was heavily cribbed from BinaryDelta.java in JGit, which +* itself was heavily cribbed from patch-delta.c in the +* GIT project. The original delta patching code was written by +* Nicolas Pitre . +*/ + +static int hdr_sz( + size_t *size, + const unsigned char **delta, + const unsigned char *end) +{ + const unsigned char *d = *delta; + size_t r = 0; + unsigned int c, shift = 0; + + do { + if (d == end) { + git_error_set(GIT_ERROR_INVALID, "truncated delta"); + return -1; + } + + if (shift >= (sizeof(size_t) * 8)) { + git_error_set(GIT_ERROR_INVALID, "delta header overflow"); + return -1; + } + c = *d++; + r |= ((size_t)(c & 0x7f)) << shift; + shift += 7; + } while (c & 0x80); + *delta = d; + *size = r; + return 0; +} + +int git_delta_read_header( + size_t *base_out, + size_t *result_out, + const unsigned char *delta, + size_t delta_len) +{ + const unsigned char *delta_end = delta + delta_len; + if ((hdr_sz(base_out, &delta, delta_end) < 0) || + (hdr_sz(result_out, &delta, delta_end) < 0)) + return -1; + return 0; +} + +#define DELTA_HEADER_BUFFER_LEN 16 +int git_delta_read_header_fromstream( + size_t *base_sz, size_t *res_sz, git_packfile_stream *stream) +{ + static const size_t buffer_len = DELTA_HEADER_BUFFER_LEN; + unsigned char buffer[DELTA_HEADER_BUFFER_LEN]; + const unsigned char *delta, *delta_end; + size_t len; + ssize_t read; + + len = read = 0; + while (len < buffer_len) { + read = git_packfile_stream_read(stream, &buffer[len], buffer_len - len); + + if (read == 0) + break; + + if (read == GIT_EBUFS) + continue; + + len += read; + } + + delta = buffer; + delta_end = delta + len; + if ((hdr_sz(base_sz, &delta, delta_end) < 0) || + (hdr_sz(res_sz, &delta, delta_end) < 0)) + return -1; + + return 0; +} + +int git_delta_apply( + void **out, + size_t *out_len, + const unsigned char *base, + size_t base_len, + const unsigned char *delta, + size_t delta_len) +{ + const unsigned char *delta_end = delta + delta_len; + size_t base_sz, res_sz, alloc_sz; + unsigned char *res_dp; + + *out = NULL; + *out_len = 0; + + /* + * Check that the base size matches the data we were given; + * if not we would underflow while accessing data from the + * base object, resulting in data corruption or segfault. + */ + if ((hdr_sz(&base_sz, &delta, delta_end) < 0) || (base_sz != base_len)) { + git_error_set(GIT_ERROR_INVALID, "failed to apply delta: base size does not match given data"); + return -1; + } + + if (hdr_sz(&res_sz, &delta, delta_end) < 0) { + git_error_set(GIT_ERROR_INVALID, "failed to apply delta: base size does not match given data"); + return -1; + } + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_sz, res_sz, 1); + res_dp = git__malloc(alloc_sz); + GIT_ERROR_CHECK_ALLOC(res_dp); + + res_dp[res_sz] = '\0'; + *out = res_dp; + *out_len = res_sz; + + while (delta < delta_end) { + unsigned char cmd = *delta++; + if (cmd & 0x80) { + /* cmd is a copy instruction; copy from the base. */ + size_t off = 0, len = 0, end; + +#define ADD_DELTA(o, shift) { if (delta < delta_end) (o) |= ((unsigned) *delta++ << shift); else goto fail; } + if (cmd & 0x01) ADD_DELTA(off, 0UL); + if (cmd & 0x02) ADD_DELTA(off, 8UL); + if (cmd & 0x04) ADD_DELTA(off, 16UL); + if (cmd & 0x08) ADD_DELTA(off, 24UL); + + if (cmd & 0x10) ADD_DELTA(len, 0UL); + if (cmd & 0x20) ADD_DELTA(len, 8UL); + if (cmd & 0x40) ADD_DELTA(len, 16UL); + if (!len) len = 0x10000; +#undef ADD_DELTA + + if (GIT_ADD_SIZET_OVERFLOW(&end, off, len) || + base_len < end || res_sz < len) + goto fail; + + memcpy(res_dp, base + off, len); + res_dp += len; + res_sz -= len; + + } else if (cmd) { + /* + * cmd is a literal insert instruction; copy from + * the delta stream itself. + */ + if (delta_end - delta < cmd || res_sz < cmd) + goto fail; + memcpy(res_dp, delta, cmd); + delta += cmd; + res_dp += cmd; + res_sz -= cmd; + + } else { + /* cmd == 0 is reserved for future encodings. */ + goto fail; + } + } + + if (delta != delta_end || res_sz) + goto fail; + return 0; + +fail: + git__free(*out); + + *out = NULL; + *out_len = 0; + + git_error_set(GIT_ERROR_INVALID, "failed to apply delta"); + return -1; +} diff --git a/src/libgit2/delta.h b/src/libgit2/delta.h new file mode 100644 index 00000000000..f61987304b3 --- /dev/null +++ b/src/libgit2/delta.h @@ -0,0 +1,136 @@ +/* + * diff-delta code taken from git.git. See diff-delta.c for details. + * + */ +#ifndef INCLUDE_git_delta_h__ +#define INCLUDE_git_delta_h__ + +#include "common.h" + +#include "pack.h" + +typedef struct git_delta_index git_delta_index; + +/* + * git_delta_index_init: compute index data from given buffer + * + * This returns a pointer to a struct delta_index that should be passed to + * subsequent create_delta() calls, or to free_delta_index(). A NULL pointer + * is returned on failure. The given buffer must not be freed nor altered + * before free_delta_index() is called. The returned pointer must be freed + * using free_delta_index(). + */ +extern int git_delta_index_init( + git_delta_index **out, const void *buf, size_t bufsize); + +/* + * Free the index created by git_delta_index_init() + */ +extern void git_delta_index_free(git_delta_index *index); + +/* + * Returns memory usage of delta index. + */ +extern size_t git_delta_index_size(git_delta_index *index); + +/* + * create_delta: create a delta from given index for the given buffer + * + * This function may be called multiple times with different buffers using + * the same delta_index pointer. If max_delta_size is non-zero and the + * resulting delta is to be larger than max_delta_size then NULL is returned. + * On success, a non-NULL pointer to the buffer with the delta data is + * returned and *delta_size is updated with its size. The returned buffer + * must be freed by the caller. + */ +extern int git_delta_create_from_index( + void **out, + size_t *out_size, + const struct git_delta_index *index, + const void *buf, + size_t bufsize, + size_t max_delta_size); + +/* + * diff_delta: create a delta from source buffer to target buffer + * + * If max_delta_size is non-zero and the resulting delta is to be larger + * than max_delta_size then GIT_EBUFS is returned. On success, a non-NULL + * pointer to the buffer with the delta data is returned and *delta_size is + * updated with its size. The returned buffer must be freed by the caller. + */ +GIT_INLINE(int) git_delta( + void **out, size_t *out_len, + const void *src_buf, size_t src_bufsize, + const void *trg_buf, size_t trg_bufsize, + size_t max_delta_size) +{ + git_delta_index *index; + int error = 0; + + *out = NULL; + *out_len = 0; + + if ((error = git_delta_index_init(&index, src_buf, src_bufsize)) < 0) + return error; + + if (index) { + error = git_delta_create_from_index(out, out_len, + index, trg_buf, trg_bufsize, max_delta_size); + + git_delta_index_free(index); + } + + return error; +} + +/* the smallest possible delta size is 4 bytes */ +#define GIT_DELTA_SIZE_MIN 4 + +/** +* Apply a git binary delta to recover the original content. +* The caller is responsible for freeing the returned buffer. +* +* @param out the output buffer +* @param out_len the length of the output buffer +* @param base the base to copy from during copy instructions. +* @param base_len number of bytes available at base. +* @param delta the delta to execute copy/insert instructions from. +* @param delta_len total number of bytes in the delta. +* @return 0 on success or an error code +*/ +extern int git_delta_apply( + void **out, + size_t *out_len, + const unsigned char *base, + size_t base_len, + const unsigned char *delta, + size_t delta_len); + +/** +* Read the header of a git binary delta. +* +* @param base_out pointer to store the base size field. +* @param result_out pointer to store the result size field. +* @param delta the delta to execute copy/insert instructions from. +* @param delta_len total number of bytes in the delta. +* @return 0 on success or an error code +*/ +extern int git_delta_read_header( + size_t *base_out, + size_t *result_out, + const unsigned char *delta, + size_t delta_len); + +/** + * Read the header of a git binary delta + * + * This variant reads just enough from the packfile stream to read the + * delta header. + */ +extern int git_delta_read_header_fromstream( + size_t *base_out, + size_t *result_out, + git_packfile_stream *stream); + +#endif diff --git a/src/libgit2/describe.c b/src/libgit2/describe.c new file mode 100644 index 00000000000..dfbe7b4ab0b --- /dev/null +++ b/src/libgit2/describe.c @@ -0,0 +1,912 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "git2/describe.h" +#include "git2/diff.h" +#include "git2/status.h" + +#include "buf.h" +#include "commit.h" +#include "commit_list.h" +#include "refs.h" +#include "repository.h" +#include "revwalk.h" +#include "strarray.h" +#include "tag.h" +#include "vector.h" +#include "wildmatch.h" +#include "hashmap_oid.h" + +/* Ported from https://github.com/git/git/blob/89dde7882f71f846ccd0359756d27bebc31108de/builtin/describe.c */ + +struct commit_name { + git_tag *tag; + unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */ + unsigned name_checked:1; + git_oid sha1; + char *path; + + /* The original key for the hashmap */ + git_oid peeled; +}; + +GIT_HASHMAP_OID_SETUP(git_describe_oidmap, struct commit_name *); + +static struct commit_name *find_commit_name( + git_describe_oidmap *names, + const git_oid *peeled) +{ + struct commit_name *result; + + if (git_describe_oidmap_get(&result, names, peeled) == 0) + return result; + + return NULL; +} + +static int replace_name( + git_tag **tag, + git_repository *repo, + struct commit_name *e, + unsigned int prio, + const git_oid *sha1) +{ + git_time_t e_time = 0, t_time = 0; + + if (!e || e->prio < prio) + return 1; + + if (e->prio == 2 && prio == 2) { + /* Multiple annotated tags point to the same commit. + * Select one to keep based upon their tagger date. + */ + git_tag *t = NULL; + + if (!e->tag) { + if (git_tag_lookup(&t, repo, &e->sha1) < 0) + return 1; + e->tag = t; + } + + if (git_tag_lookup(&t, repo, sha1) < 0) + return 0; + + *tag = t; + + if (e->tag->tagger) + e_time = e->tag->tagger->when.time; + + if (t->tagger) + t_time = t->tagger->when.time; + + if (e_time < t_time) + return 1; + } + + return 0; +} + +static int add_to_known_names( + git_repository *repo, + git_describe_oidmap *names, + const char *path, + const git_oid *peeled, + unsigned int prio, + const git_oid *sha1) +{ + struct commit_name *e = find_commit_name(names, peeled); + bool found = (e != NULL); + + git_tag *tag = NULL; + if (replace_name(&tag, repo, e, prio, sha1)) { + if (!found) { + e = git__malloc(sizeof(struct commit_name)); + GIT_ERROR_CHECK_ALLOC(e); + + e->path = NULL; + e->tag = NULL; + } + + if (e->tag) + git_tag_free(e->tag); + e->tag = tag; + e->prio = prio; + e->name_checked = 0; + git_oid_cpy(&e->sha1, sha1); + git__free(e->path); + e->path = git__strdup(path); + git_oid_cpy(&e->peeled, peeled); + + if (!found && git_describe_oidmap_put(names, &e->peeled, e) < 0) + return -1; + } + else + git_tag_free(tag); + + return 0; +} + +static int retrieve_peeled_tag_or_object_oid( + git_oid *peeled_out, + git_oid *ref_target_out, + git_repository *repo, + const char *refname) +{ + git_reference *ref; + git_object *peeled = NULL; + int error; + + if ((error = git_reference_lookup_resolved(&ref, repo, refname, -1)) < 0) + return error; + + if ((error = git_reference_peel(&peeled, ref, GIT_OBJECT_ANY)) < 0) + goto cleanup; + + git_oid_cpy(ref_target_out, git_reference_target(ref)); + git_oid_cpy(peeled_out, git_object_id(peeled)); + + if (git_oid_cmp(ref_target_out, peeled_out) != 0) + error = 1; /* The reference was pointing to a annotated tag */ + else + error = 0; /* Any other object */ + +cleanup: + git_reference_free(ref); + git_object_free(peeled); + return error; +} + +struct git_describe_result { + int dirty; + int exact_match; + int fallback_to_id; + git_oid commit_id; + git_repository *repo; + struct commit_name *name; + struct possible_tag *tag; +}; + +struct get_name_data +{ + git_describe_options *opts; + git_repository *repo; + git_describe_oidmap names; + git_describe_result *result; +}; + +static int commit_name_dup(struct commit_name **out, struct commit_name *in) +{ + struct commit_name *name; + + name = git__malloc(sizeof(struct commit_name)); + GIT_ERROR_CHECK_ALLOC(name); + + memcpy(name, in, sizeof(struct commit_name)); + name->tag = NULL; + name->path = NULL; + + if (in->tag && git_tag_dup(&name->tag, in->tag) < 0) + return -1; + + name->path = git__strdup(in->path); + GIT_ERROR_CHECK_ALLOC(name->path); + + *out = name; + return 0; +} + +static int get_name(const char *refname, void *payload) +{ + struct get_name_data *data; + bool is_tag, is_annotated, all; + git_oid peeled, sha1; + unsigned int prio; + int error = 0; + + data = (struct get_name_data *)payload; + is_tag = !git__prefixcmp(refname, GIT_REFS_TAGS_DIR); + all = data->opts->describe_strategy == GIT_DESCRIBE_ALL; + + /* Reject anything outside refs/tags/ unless --all */ + if (!all && !is_tag) + return 0; + + /* Accept only tags that match the pattern, if given */ + if (data->opts->pattern && (!is_tag || wildmatch(data->opts->pattern, + refname + strlen(GIT_REFS_TAGS_DIR), 0))) + return 0; + + /* Is it annotated? */ + if ((error = retrieve_peeled_tag_or_object_oid( + &peeled, &sha1, data->repo, refname)) < 0) + return error; + + is_annotated = error; + + /* + * By default, we only use annotated tags, but with --tags + * we fall back to lightweight ones (even without --tags, + * we still remember lightweight ones, only to give hints + * in an error message). --all allows any refs to be used. + */ + if (is_annotated) + prio = 2; + else if (is_tag) + prio = 1; + else + prio = 0; + + add_to_known_names(data->repo, &data->names, + all ? refname + strlen(GIT_REFS_DIR) : refname + strlen(GIT_REFS_TAGS_DIR), + &peeled, prio, &sha1); + return 0; +} + +struct possible_tag { + struct commit_name *name; + int depth; + int found_order; + unsigned flag_within; +}; + +static int possible_tag_dup(struct possible_tag **out, struct possible_tag *in) +{ + struct possible_tag *tag; + int error; + + tag = git__malloc(sizeof(struct possible_tag)); + GIT_ERROR_CHECK_ALLOC(tag); + + memcpy(tag, in, sizeof(struct possible_tag)); + tag->name = NULL; + + if ((error = commit_name_dup(&tag->name, in->name)) < 0) { + git__free(tag); + *out = NULL; + return error; + } + + *out = tag; + return 0; +} + +static int compare_pt(const void *a_, const void *b_) +{ + struct possible_tag *a = (struct possible_tag *)a_; + struct possible_tag *b = (struct possible_tag *)b_; + if (a->depth != b->depth) + return a->depth - b->depth; + if (a->found_order != b->found_order) + return a->found_order - b->found_order; + return 0; +} + +#define SEEN (1u << 0) + +static unsigned long finish_depth_computation( + git_pqueue *list, + git_revwalk *walk, + struct possible_tag *best) +{ + unsigned long seen_commits = 0; + int error, i; + + while (git_pqueue_size(list) > 0) { + git_commit_list_node *c = git_pqueue_pop(list); + seen_commits++; + if (c->flags & best->flag_within) { + size_t index = 0; + while (git_pqueue_size(list) > index) { + git_commit_list_node *i = git_pqueue_get(list, index); + if (!(i->flags & best->flag_within)) + break; + index++; + } + if (index > git_pqueue_size(list)) + break; + } else + best->depth++; + for (i = 0; i < c->out_degree; i++) { + git_commit_list_node *p = c->parents[i]; + if ((error = git_commit_list_parse(walk, p)) < 0) + return error; + if (!(p->flags & SEEN)) + if ((error = git_pqueue_insert(list, p)) < 0) + return error; + p->flags |= c->flags; + } + } + return seen_commits; +} + +static int display_name(git_str *buf, git_repository *repo, struct commit_name *n) +{ + if (n->prio == 2 && !n->tag) { + if (git_tag_lookup(&n->tag, repo, &n->sha1) < 0) { + git_error_set(GIT_ERROR_TAG, "annotated tag '%s' not available", n->path); + return -1; + } + } + + if (n->tag && !n->name_checked) { + if (!git_tag_name(n->tag)) { + git_error_set(GIT_ERROR_TAG, "annotated tag '%s' has no embedded name", n->path); + return -1; + } + + /* TODO: Cope with warnings + if (strcmp(n->tag->tag, all ? n->path + 5 : n->path)) + warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path); + */ + + n->name_checked = 1; + } + + if (n->tag) + git_str_printf(buf, "%s", git_tag_name(n->tag)); + else + git_str_printf(buf, "%s", n->path); + + return 0; +} + +static int find_unique_abbrev_size( + int *out, + git_repository *repo, + const git_oid *oid_in, + unsigned int abbreviated_size) +{ + size_t size = abbreviated_size; + git_odb *odb; + git_oid dummy; + size_t hexsize; + int error; + + if ((error = git_repository_odb__weakptr(&odb, repo)) < 0) + return error; + + hexsize = git_oid_hexsize(repo->oid_type); + + while (size < hexsize) { + if ((error = git_odb_exists_prefix(&dummy, odb, oid_in, size)) == 0) { + *out = (int) size; + return 0; + } + + /* If the error wasn't that it's not unique, then it's a proper error */ + if (error != GIT_EAMBIGUOUS) + return error; + + /* Try again with a larger size */ + size++; + } + + /* If we didn't find any shorter prefix, we have to do the whole thing */ + *out = (int)hexsize; + + return 0; +} + +static int show_suffix( + git_str *buf, + int depth, + git_repository *repo, + const git_oid *id, + unsigned int abbrev_size) +{ + int error, size = 0; + + char hex_oid[GIT_OID_MAX_HEXSIZE]; + + if ((error = find_unique_abbrev_size(&size, repo, id, abbrev_size)) < 0) + return error; + + git_oid_fmt(hex_oid, id); + + git_str_printf(buf, "-%d-g", depth); + + git_str_put(buf, hex_oid, size); + + return git_str_oom(buf) ? -1 : 0; +} + +#define MAX_CANDIDATES_TAGS FLAG_BITS - 1 + +static int describe_not_found(const git_oid *oid, const char *message_format) { + char oid_str[GIT_OID_MAX_HEXSIZE + 1]; + git_oid_tostr(oid_str, sizeof(oid_str), oid); + + git_error_set(GIT_ERROR_DESCRIBE, message_format, oid_str); + return GIT_ENOTFOUND; +} + +static int describe( + struct get_name_data *data, + git_commit *commit) +{ + struct commit_name *n; + struct possible_tag *best; + bool all, tags; + git_revwalk *walk = NULL; + git_pqueue list; + git_commit_list_node *cmit, *gave_up_on = NULL; + git_vector all_matches = GIT_VECTOR_INIT; + unsigned int match_cnt = 0, annotated_cnt = 0, cur_match; + unsigned long seen_commits = 0; /* TODO: Check long */ + unsigned int unannotated_cnt = 0; + int error; + + if (git_vector_init(&all_matches, MAX_CANDIDATES_TAGS, compare_pt) < 0) + return -1; + + if ((error = git_pqueue_init(&list, 0, 2, git_commit_list_time_cmp)) < 0) + goto cleanup; + + all = data->opts->describe_strategy == GIT_DESCRIBE_ALL; + tags = data->opts->describe_strategy == GIT_DESCRIBE_TAGS; + + git_oid_cpy(&data->result->commit_id, git_commit_id(commit)); + + n = find_commit_name(&data->names, git_commit_id(commit)); + if (n && (tags || all || n->prio == 2)) { + /* + * Exact match to an existing ref. + */ + data->result->exact_match = 1; + if ((error = commit_name_dup(&data->result->name, n)) < 0) + goto cleanup; + + goto cleanup; + } + + if (!data->opts->max_candidates_tags) { + error = describe_not_found( + git_commit_id(commit), + "cannot describe - no tag exactly matches '%s'"); + + goto cleanup; + } + + if ((error = git_revwalk_new(&walk, git_commit_owner(commit))) < 0) + goto cleanup; + + if ((cmit = git_revwalk__commit_lookup(walk, git_commit_id(commit))) == NULL) + goto cleanup; + + if ((error = git_commit_list_parse(walk, cmit)) < 0) + goto cleanup; + + cmit->flags = SEEN; + + if ((error = git_pqueue_insert(&list, cmit)) < 0) + goto cleanup; + + while (git_pqueue_size(&list) > 0) + { + int i; + + git_commit_list_node *c = (git_commit_list_node *)git_pqueue_pop(&list); + seen_commits++; + + n = find_commit_name(&data->names, &c->oid); + + if (n) { + if (!tags && !all && n->prio < 2) { + unannotated_cnt++; + } else if (match_cnt < data->opts->max_candidates_tags) { + struct possible_tag *t = git__malloc(sizeof(struct commit_name)); + GIT_ERROR_CHECK_ALLOC(t); + if ((error = git_vector_insert(&all_matches, t)) < 0) + goto cleanup; + + match_cnt++; + + t->name = n; + t->depth = seen_commits - 1; + t->flag_within = 1u << match_cnt; + t->found_order = match_cnt; + c->flags |= t->flag_within; + if (n->prio == 2) + annotated_cnt++; + } + else { + gave_up_on = c; + break; + } + } + + for (cur_match = 0; cur_match < match_cnt; cur_match++) { + struct possible_tag *t = git_vector_get(&all_matches, cur_match); + if (!(c->flags & t->flag_within)) + t->depth++; + } + + if (annotated_cnt && (git_pqueue_size(&list) == 0)) { + /* + if (debug) { + char oid_str[GIT_OID_MAX_HEXSIZE + 1]; + git_oid_tostr(oid_str, sizeof(oid_str), &c->oid); + + fprintf(stderr, "finished search at %s\n", oid_str); + } + */ + break; + } + for (i = 0; i < c->out_degree; i++) { + git_commit_list_node *p = c->parents[i]; + if ((error = git_commit_list_parse(walk, p)) < 0) + goto cleanup; + if (!(p->flags & SEEN)) + if ((error = git_pqueue_insert(&list, p)) < 0) + goto cleanup; + p->flags |= c->flags; + + if (data->opts->only_follow_first_parent) + break; + } + } + + if (!match_cnt) { + if (data->opts->show_commit_oid_as_fallback) { + data->result->fallback_to_id = 1; + git_oid_cpy(&data->result->commit_id, &cmit->oid); + + goto cleanup; + } + if (unannotated_cnt) { + error = describe_not_found(git_commit_id(commit), + "cannot describe - " + "no annotated tags can describe '%s'; " + "however, there were unannotated tags."); + goto cleanup; + } + else { + error = describe_not_found(git_commit_id(commit), + "cannot describe - " + "no tags can describe '%s'."); + goto cleanup; + } + } + + git_vector_sort(&all_matches); + + best = (struct possible_tag *)git_vector_get(&all_matches, 0); + + if (gave_up_on) { + if ((error = git_pqueue_insert(&list, gave_up_on)) < 0) + goto cleanup; + seen_commits--; + } + if ((error = finish_depth_computation( + &list, walk, best)) < 0) + goto cleanup; + + seen_commits += error; + if ((error = possible_tag_dup(&data->result->tag, best)) < 0) + goto cleanup; + + /* + { + static const char *prio_names[] = { + "head", "lightweight", "annotated", + }; + + char oid_str[GIT_OID_MAX_HEXSIZE + 1]; + + if (debug) { + for (cur_match = 0; cur_match < match_cnt; cur_match++) { + struct possible_tag *t = (struct possible_tag *)git_vector_get(&all_matches, cur_match); + fprintf(stderr, " %-11s %8d %s\n", + prio_names[t->name->prio], + t->depth, t->name->path); + } + fprintf(stderr, "traversed %lu commits\n", seen_commits); + if (gave_up_on) { + git_oid_tostr(oid_str, sizeof(oid_str), &gave_up_on->oid); + fprintf(stderr, + "more than %i tags found; listed %i most recent\n" + "gave up search at %s\n", + data->opts->max_candidates_tags, data->opts->max_candidates_tags, + oid_str); + } + } + } + */ + + git_oid_cpy(&data->result->commit_id, &cmit->oid); + +cleanup: + { + size_t i; + struct possible_tag *match; + git_vector_foreach(&all_matches, i, match) { + git__free(match); + } + } + git_vector_dispose(&all_matches); + git_pqueue_free(&list); + git_revwalk_free(walk); + return error; +} + +static int normalize_options( + git_describe_options *dst, + const git_describe_options *src) +{ + git_describe_options default_options = GIT_DESCRIBE_OPTIONS_INIT; + if (!src) src = &default_options; + + *dst = *src; + + if (dst->max_candidates_tags > GIT_DESCRIBE_DEFAULT_MAX_CANDIDATES_TAGS) + dst->max_candidates_tags = GIT_DESCRIBE_DEFAULT_MAX_CANDIDATES_TAGS; + + return 0; +} + +int git_describe_commit( + git_describe_result **result, + git_object *committish, + git_describe_options *opts) +{ + struct get_name_data data = {0}; + struct commit_name *name; + git_commit *commit; + git_describe_options normalized; + git_hashmap_iter_t iter = GIT_HASHMAP_INIT; + int error = -1; + + GIT_ASSERT_ARG(result); + GIT_ASSERT_ARG(committish); + + data.result = git__calloc(1, sizeof(git_describe_result)); + GIT_ERROR_CHECK_ALLOC(data.result); + data.result->repo = git_object_owner(committish); + + data.repo = git_object_owner(committish); + + if ((error = normalize_options(&normalized, opts)) < 0) + return error; + + GIT_ERROR_CHECK_VERSION( + &normalized, + GIT_DESCRIBE_OPTIONS_VERSION, + "git_describe_options"); + data.opts = &normalized; + + /** TODO: contains to be implemented */ + + if ((error = git_object_peel((git_object **)(&commit), committish, GIT_OBJECT_COMMIT)) < 0) + goto cleanup; + + if ((error = git_reference_foreach_name( + git_object_owner(committish), + get_name, &data)) < 0) + goto cleanup; + + if (git_describe_oidmap_size(&data.names) == 0 && !normalized.show_commit_oid_as_fallback) { + git_error_set(GIT_ERROR_DESCRIBE, "cannot describe - " + "no reference found, cannot describe anything."); + error = -1; + goto cleanup; + } + + if ((error = describe(&data, commit)) < 0) + goto cleanup; + +cleanup: + git_commit_free(commit); + + while (git_describe_oidmap_iterate(&iter, NULL, &name, &data.names) == 0) { + git_tag_free(name->tag); + git__free(name->path); + git__free(name); + } + + git_describe_oidmap_dispose(&data.names); + + if (error < 0) + git_describe_result_free(data.result); + else + *result = data.result; + + return error; +} + +int git_describe_workdir( + git_describe_result **out, + git_repository *repo, + git_describe_options *opts) +{ + int error; + git_oid current_id; + git_status_list *status = NULL; + git_status_options status_opts = GIT_STATUS_OPTIONS_INIT; + git_describe_result *result = NULL; + git_object *commit; + + if ((error = git_reference_name_to_id(¤t_id, repo, GIT_HEAD_FILE)) < 0) + return error; + + if ((error = git_object_lookup(&commit, repo, ¤t_id, GIT_OBJECT_COMMIT)) < 0) + return error; + + /* The first step is to perform a describe of HEAD, so we can leverage this */ + if ((error = git_describe_commit(&result, commit, opts)) < 0) + goto out; + + if ((error = git_status_list_new(&status, repo, &status_opts)) < 0) + goto out; + + + if (git_status_list_entrycount(status) > 0) + result->dirty = 1; + +out: + git_object_free(commit); + git_status_list_free(status); + + if (error < 0) + git_describe_result_free(result); + else + *out = result; + + return error; +} + +static int normalize_format_options( + git_describe_format_options *dst, + const git_describe_format_options *src) +{ + if (!src) { + git_describe_format_options_init(dst, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION); + return 0; + } + + memcpy(dst, src, sizeof(git_describe_format_options)); + return 0; +} + +static int git_describe__format( + git_str *out, + const git_describe_result *result, + const git_describe_format_options *given) +{ + int error; + git_repository *repo; + struct commit_name *name; + git_describe_format_options opts; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(result); + + GIT_ERROR_CHECK_VERSION(given, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION, "git_describe_format_options"); + normalize_format_options(&opts, given); + + if (opts.always_use_long_format && opts.abbreviated_size == 0) { + git_error_set(GIT_ERROR_DESCRIBE, "cannot describe - " + "'always_use_long_format' is incompatible with a zero" + "'abbreviated_size'"); + return -1; + } + + + repo = result->repo; + + /* If we did find an exact match, then it's the easier method */ + if (result->exact_match) { + name = result->name; + if ((error = display_name(out, repo, name)) < 0) + return error; + + if (opts.always_use_long_format) { + const git_oid *id = name->tag ? git_tag_target_id(name->tag) : &result->commit_id; + if ((error = show_suffix(out, 0, repo, id, opts.abbreviated_size)) < 0) + return error; + } + + if (result->dirty && opts.dirty_suffix) + git_str_puts(out, opts.dirty_suffix); + + return git_str_oom(out) ? -1 : 0; + } + + /* If we didn't find *any* tags, we fall back to the commit's id */ + if (result->fallback_to_id) { + char hex_oid[GIT_OID_MAX_HEXSIZE + 1] = {0}; + int size = 0; + + if ((error = find_unique_abbrev_size( + &size, repo, &result->commit_id, opts.abbreviated_size)) < 0) + return -1; + + git_oid_fmt(hex_oid, &result->commit_id); + git_str_put(out, hex_oid, size); + + if (result->dirty && opts.dirty_suffix) + git_str_puts(out, opts.dirty_suffix); + + return git_str_oom(out) ? -1 : 0; + } + + /* Lastly, if we found a matching tag, we show that */ + name = result->tag->name; + + if ((error = display_name(out, repo, name)) < 0) + return error; + + if (opts.abbreviated_size) { + if ((error = show_suffix(out, result->tag->depth, repo, + &result->commit_id, opts.abbreviated_size)) < 0) + return error; + } + + if (result->dirty && opts.dirty_suffix) { + git_str_puts(out, opts.dirty_suffix); + } + + return git_str_oom(out) ? -1 : 0; +} + +int git_describe_format( + git_buf *out, + const git_describe_result *result, + const git_describe_format_options *given) +{ + GIT_BUF_WRAP_PRIVATE(out, git_describe__format, result, given); +} + +void git_describe_result_free(git_describe_result *result) +{ + if (result == NULL) + return; + + if (result->name) { + git_tag_free(result->name->tag); + git__free(result->name->path); + git__free(result->name); + } + + if (result->tag) { + git_tag_free(result->tag->name->tag); + git__free(result->tag->name->path); + git__free(result->tag->name); + git__free(result->tag); + } + + git__free(result); +} + +int git_describe_options_init(git_describe_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_describe_options, GIT_DESCRIBE_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_describe_init_options(git_describe_options *opts, unsigned int version) +{ + return git_describe_options_init(opts, version); +} +#endif + +int git_describe_format_options_init(git_describe_format_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_describe_format_options, GIT_DESCRIBE_FORMAT_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_describe_init_format_options(git_describe_format_options *opts, unsigned int version) +{ + return git_describe_format_options_init(opts, version); +} +#endif diff --git a/src/libgit2/diff.c b/src/libgit2/diff.c new file mode 100644 index 00000000000..80027ba30a0 --- /dev/null +++ b/src/libgit2/diff.c @@ -0,0 +1,402 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "diff.h" + +#include "common.h" +#include "buf.h" +#include "patch.h" +#include "email.h" +#include "commit.h" +#include "index.h" +#include "diff_generate.h" + +#include "git2/version.h" +#include "git2/sys/email.h" + +struct patch_id_args { + git_diff *diff; + git_hash_ctx ctx; + git_oid result; + git_oid_t oid_type; + int first_file; +}; + +GIT_INLINE(const char *) diff_delta__path(const git_diff_delta *delta) +{ + const char *str = delta->old_file.path; + + if (!str || + delta->status == GIT_DELTA_ADDED || + delta->status == GIT_DELTA_RENAMED || + delta->status == GIT_DELTA_COPIED) + str = delta->new_file.path; + + return str; +} + +int git_diff_delta__cmp(const void *a, const void *b) +{ + const git_diff_delta *da = a, *db = b; + int val = strcmp(diff_delta__path(da), diff_delta__path(db)); + return val ? val : ((int)da->status - (int)db->status); +} + +int git_diff_delta__casecmp(const void *a, const void *b) +{ + const git_diff_delta *da = a, *db = b; + int val = strcasecmp(diff_delta__path(da), diff_delta__path(db)); + return val ? val : ((int)da->status - (int)db->status); +} + +int git_diff__entry_cmp(const void *a, const void *b) +{ + const git_index_entry *entry_a = a; + const git_index_entry *entry_b = b; + + return strcmp(entry_a->path, entry_b->path); +} + +int git_diff__entry_icmp(const void *a, const void *b) +{ + const git_index_entry *entry_a = a; + const git_index_entry *entry_b = b; + + return strcasecmp(entry_a->path, entry_b->path); +} + +void git_diff_free(git_diff *diff) +{ + if (!diff) + return; + + GIT_REFCOUNT_DEC(diff, diff->free_fn); +} + +void git_diff_addref(git_diff *diff) +{ + GIT_REFCOUNT_INC(diff); +} + +size_t git_diff_num_deltas(const git_diff *diff) +{ + GIT_ASSERT_ARG(diff); + return diff->deltas.length; +} + +size_t git_diff_num_deltas_of_type(const git_diff *diff, git_delta_t type) +{ + size_t i, count = 0; + const git_diff_delta *delta; + + GIT_ASSERT_ARG(diff); + + git_vector_foreach(&diff->deltas, i, delta) { + count += (delta->status == type); + } + + return count; +} + +const git_diff_delta *git_diff_get_delta(const git_diff *diff, size_t idx) +{ + GIT_ASSERT_ARG_WITH_RETVAL(diff, NULL); + return git_vector_get(&diff->deltas, idx); +} + +int git_diff_is_sorted_icase(const git_diff *diff) +{ + return (diff->opts.flags & GIT_DIFF_IGNORE_CASE) != 0; +} + +int git_diff_get_perfdata(git_diff_perfdata *out, const git_diff *diff) +{ + GIT_ASSERT_ARG(out); + GIT_ERROR_CHECK_VERSION(out, GIT_DIFF_PERFDATA_VERSION, "git_diff_perfdata"); + out->stat_calls = diff->perf.stat_calls; + out->oid_calculations = diff->perf.oid_calculations; + return 0; +} + +int git_diff_foreach( + git_diff *diff, + git_diff_file_cb file_cb, + git_diff_binary_cb binary_cb, + git_diff_hunk_cb hunk_cb, + git_diff_line_cb data_cb, + void *payload) +{ + int error = 0; + git_diff_delta *delta; + size_t idx; + + GIT_ASSERT_ARG(diff); + + git_vector_foreach(&diff->deltas, idx, delta) { + git_patch *patch; + + /* check flags against patch status */ + if (git_diff_delta__should_skip(&diff->opts, delta)) + continue; + + if ((error = git_patch_from_diff(&patch, diff, idx)) != 0) + break; + + error = git_patch__invoke_callbacks(patch, file_cb, binary_cb, + hunk_cb, data_cb, payload); + git_patch_free(patch); + + if (error) + break; + } + + return error; +} + +#ifndef GIT_DEPRECATE_HARD + +int git_diff_format_email( + git_buf *out, + git_diff *diff, + const git_diff_format_email_options *opts) +{ + git_email_create_options email_create_opts = GIT_EMAIL_CREATE_OPTIONS_INIT; + git_str email = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(diff); + GIT_ASSERT_ARG(opts && opts->summary && opts->id && opts->author); + + GIT_ERROR_CHECK_VERSION(opts, + GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, + "git_format_email_options"); + + /* This is a `git_buf` special case; subsequent calls append. */ + email.ptr = out->ptr; + email.asize = out->reserved; + email.size = out->size; + + out->ptr = git_str__initstr; + out->reserved = 0; + out->size = 0; + + if ((opts->flags & GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) != 0) + email_create_opts.subject_prefix = ""; + + error = git_email__append_from_diff(&email, diff, opts->patch_no, + opts->total_patches, opts->id, opts->summary, opts->body, + opts->author, &email_create_opts); + + if (error < 0) + goto done; + + error = git_buf_fromstr(out, &email); + +done: + git_str_dispose(&email); + return error; +} + +int git_diff_commit_as_email( + git_buf *out, + git_repository *repo, + git_commit *commit, + size_t patch_no, + size_t total_patches, + uint32_t flags, + const git_diff_options *diff_opts) +{ + git_diff *diff = NULL; + git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT; + const git_oid *commit_id; + const char *summary, *body; + const git_signature *author; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(commit); + + commit_id = git_commit_id(commit); + summary = git_commit_summary(commit); + body = git_commit_body(commit); + author = git_commit_author(commit); + + if ((flags & GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) != 0) + opts.subject_prefix = ""; + + if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0) + return error; + + error = git_email_create_from_diff(out, diff, patch_no, total_patches, commit_id, summary, body, author, &opts); + + git_diff_free(diff); + return error; +} + +int git_diff_init_options(git_diff_options *opts, unsigned int version) +{ + return git_diff_options_init(opts, version); +} + +int git_diff_find_init_options( + git_diff_find_options *opts, unsigned int version) +{ + return git_diff_find_options_init(opts, version); +} + +int git_diff_format_email_options_init( + git_diff_format_email_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_diff_format_email_options, + GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT); + return 0; +} + +int git_diff_format_email_init_options( + git_diff_format_email_options *opts, unsigned int version) +{ + return git_diff_format_email_options_init(opts, version); +} + +#endif + +int git_diff_options_init(git_diff_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_diff_options, GIT_DIFF_OPTIONS_INIT); + return 0; +} + +int git_diff_find_options_init( + git_diff_find_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_diff_find_options, GIT_DIFF_FIND_OPTIONS_INIT); + return 0; +} + +static int flush_hunk(git_oid *result, struct patch_id_args *args) +{ + git_hash_ctx *ctx = &args->ctx; + git_oid hash; + unsigned short carry = 0; + size_t i; + int error; + + if ((error = git_hash_final(hash.id, ctx)) < 0 || + (error = git_hash_init(ctx)) < 0) + return error; + + for (i = 0; i < git_oid_size(args->oid_type); i++) { + carry += result->id[i] + hash.id[i]; + result->id[i] = (unsigned char)carry; + carry >>= 8; + } + + return 0; +} + +static void strip_spaces(git_str *buf) +{ + char *src = buf->ptr, *dst = buf->ptr; + char c; + size_t len = 0; + + while ((c = *src++) != '\0') { + if (!git__isspace(c)) { + *dst++ = c; + len++; + } + } + + git_str_truncate(buf, len); +} + +static int diff_patchid_print_callback_to_buf( + const git_diff_delta *delta, + const git_diff_hunk *hunk, + const git_diff_line *line, + void *payload) +{ + struct patch_id_args *args = (struct patch_id_args *) payload; + git_str buf = GIT_STR_INIT; + int error = 0; + + if (line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL || + line->origin == GIT_DIFF_LINE_ADD_EOFNL || + line->origin == GIT_DIFF_LINE_DEL_EOFNL) + goto out; + + if ((error = git_diff_print_callback__to_buf(delta, hunk, + line, &buf)) < 0) + goto out; + + strip_spaces(&buf); + + if (line->origin == GIT_DIFF_LINE_FILE_HDR && + !args->first_file && + (error = flush_hunk(&args->result, args) < 0)) + goto out; + + if ((error = git_hash_update(&args->ctx, buf.ptr, buf.size)) < 0) + goto out; + + if (line->origin == GIT_DIFF_LINE_FILE_HDR && args->first_file) + args->first_file = 0; + +out: + git_str_dispose(&buf); + return error; +} + +int git_diff_patchid_options_init(git_diff_patchid_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_diff_patchid_options, GIT_DIFF_PATCHID_OPTIONS_INIT); + return 0; +} + +int git_diff_patchid(git_oid *out, git_diff *diff, git_diff_patchid_options *opts) +{ + struct patch_id_args args; + git_hash_algorithm_t algorithm; + int error; + + GIT_ERROR_CHECK_VERSION( + opts, GIT_DIFF_PATCHID_OPTIONS_VERSION, "git_diff_patchid_options"); + + algorithm = git_oid_algorithm(diff->opts.oid_type); + + memset(&args, 0, sizeof(args)); + args.diff = diff; + args.first_file = 1; + args.oid_type = diff->opts.oid_type; + if ((error = git_hash_ctx_init(&args.ctx, algorithm)) < 0) + goto out; + + if ((error = git_diff_print(diff, + GIT_DIFF_FORMAT_PATCH_ID, + diff_patchid_print_callback_to_buf, + &args)) < 0) + goto out; + + if ((error = (flush_hunk(&args.result, &args))) < 0) + goto out; + +#ifdef GIT_EXPERIMENTAL_SHA256 + args.result.type = diff->opts.oid_type; +#endif + + git_oid_cpy(out, &args.result); + +out: + git_hash_ctx_cleanup(&args.ctx); + return error; +} diff --git a/src/libgit2/diff.h b/src/libgit2/diff.h new file mode 100644 index 00000000000..c79a279e379 --- /dev/null +++ b/src/libgit2/diff.h @@ -0,0 +1,77 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_diff_h__ +#define INCLUDE_diff_h__ + +#include "common.h" + +#include "git2/diff.h" +#include "git2/patch.h" +#include "git2/sys/diff.h" +#include "git2/oid.h" + +#include "vector.h" +#include "iterator.h" +#include "repository.h" +#include "pool.h" +#include "odb.h" + +#define DIFF_OLD_PREFIX_DEFAULT "a/" +#define DIFF_NEW_PREFIX_DEFAULT "b/" + +typedef enum { + GIT_DIFF_TYPE_UNKNOWN = 0, + GIT_DIFF_TYPE_GENERATED = 1, + GIT_DIFF_TYPE_PARSED = 2 +} git_diff_origin_t; + +struct git_diff { + git_refcount rc; + git_repository *repo; + git_attr_session attrsession; + git_diff_origin_t type; + git_diff_options opts; + git_vector deltas; /* vector of git_diff_delta */ + git_pool pool; + git_iterator_t old_src; + git_iterator_t new_src; + git_diff_perfdata perf; + + int (*strcomp)(const char *, const char *); + int (*strncomp)(const char *, const char *, size_t); + int (*pfxcomp)(const char *str, const char *pfx); + int (*entrycomp)(const void *a, const void *b); + + int (*patch_fn)(git_patch **out, git_diff *diff, size_t idx); + void (*free_fn)(git_diff *diff); +}; + +extern int git_diff_delta__format_file_header( + git_str *out, + const git_diff_delta *delta, + const char *oldpfx, + const char *newpfx, + int oid_strlen, + bool print_index); + +extern int git_diff_delta__cmp(const void *a, const void *b); +extern int git_diff_delta__casecmp(const void *a, const void *b); + +extern int git_diff__entry_cmp(const void *a, const void *b); +extern int git_diff__entry_icmp(const void *a, const void *b); + +#ifndef GIT_EXPERIMENTAL_SHA256 + +int git_diff_from_buffer_ext( + git_diff **out, + const char *content, + size_t content_len, + git_diff_parse_options *opts); + +#endif + +#endif diff --git a/src/libgit2/diff_driver.c b/src/libgit2/diff_driver.c new file mode 100644 index 00000000000..c2c421ca8c8 --- /dev/null +++ b/src/libgit2/diff_driver.c @@ -0,0 +1,515 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "diff_driver.h" + +#include "git2/attr.h" + +#include "common.h" +#include "diff.h" +#include "map.h" +#include "config.h" +#include "regexp.h" +#include "repository.h" +#include "userdiff.h" + +typedef enum { + DIFF_DRIVER_AUTO = 0, + DIFF_DRIVER_BINARY = 1, + DIFF_DRIVER_TEXT = 2, + DIFF_DRIVER_PATTERNLIST = 3 +} git_diff_driver_t; + +typedef struct { + git_regexp re; + int flags; +} git_diff_driver_pattern; + +enum { + REG_NEGATE = (1 << 15) /* get out of the way of existing flags */ +}; + +/* data for finding function context for a given file type */ +struct git_diff_driver { + git_diff_driver_t type; + uint32_t binary_flags; + uint32_t other_flags; + git_array_t(git_diff_driver_pattern) fn_patterns; + git_regexp word_pattern; + char name[GIT_FLEX_ARRAY]; +}; + +GIT_HASHMAP_STR_SETUP(git_diff_driver_map, git_diff_driver *); + +struct git_diff_driver_registry { + git_diff_driver_map map; +}; + +#define FORCE_DIFFABLE (GIT_DIFF_FORCE_TEXT | GIT_DIFF_FORCE_BINARY) + +static git_diff_driver diff_driver_auto = { DIFF_DRIVER_AUTO, 0, 0 }; +static git_diff_driver diff_driver_binary = { DIFF_DRIVER_BINARY, GIT_DIFF_FORCE_BINARY, 0 }; +static git_diff_driver diff_driver_text = { DIFF_DRIVER_TEXT, GIT_DIFF_FORCE_TEXT, 0 }; + +git_diff_driver_registry *git_diff_driver_registry_new(void) +{ + return git__calloc(1, sizeof(git_diff_driver_registry)); +} + +void git_diff_driver_registry_free(git_diff_driver_registry *reg) +{ + git_diff_driver *drv; + git_hashmap_iter_t iter = 0; + + if (!reg) + return; + + while (git_diff_driver_map_iterate(&iter, NULL, &drv, ®->map) == 0) + git_diff_driver_free(drv); + + git_diff_driver_map_dispose(®->map); + git__free(reg); +} + +static int diff_driver_add_patterns( + git_diff_driver *drv, const char *regex_str, int regex_flags) +{ + int error = 0; + const char *scan, *end; + git_diff_driver_pattern *pat = NULL; + git_str buf = GIT_STR_INIT; + + for (scan = regex_str; scan; scan = end) { + /* get pattern to fill in */ + if ((pat = git_array_alloc(drv->fn_patterns)) == NULL) { + return -1; + } + + pat->flags = regex_flags; + if (*scan == '!') { + pat->flags |= REG_NEGATE; + ++scan; + } + + if ((end = strchr(scan, '\n')) != NULL) { + error = git_str_set(&buf, scan, end - scan); + end++; + } else { + error = git_str_sets(&buf, scan); + } + if (error < 0) + break; + + if ((error = git_regexp_compile(&pat->re, buf.ptr, regex_flags)) != 0) { + /* + * TODO: issue a warning + */ + } + } + + if (error && pat != NULL) + (void)git_array_pop(drv->fn_patterns); /* release last item */ + git_str_dispose(&buf); + + /* We want to ignore bad patterns, so return success regardless */ + return 0; +} + +static int diff_driver_xfuncname(const git_config_entry *entry, void *payload) +{ + return diff_driver_add_patterns(payload, entry->value, 0); +} + +static int diff_driver_funcname(const git_config_entry *entry, void *payload) +{ + return diff_driver_add_patterns(payload, entry->value, 0); +} + +static git_diff_driver_registry *git_repository_driver_registry( + git_repository *repo) +{ + git_diff_driver_registry *reg = git_atomic_load(repo->diff_drivers), *newreg; + if (reg) + return reg; + + newreg = git_diff_driver_registry_new(); + if (!newreg) { + git_error_set(GIT_ERROR_REPOSITORY, "unable to create diff driver registry"); + return newreg; + } + reg = git_atomic_compare_and_swap(&repo->diff_drivers, NULL, newreg); + if (!reg) { + reg = newreg; + } else { + /* if we race, free losing allocation */ + git_diff_driver_registry_free(newreg); + } + return reg; +} + +static int diff_driver_alloc( + git_diff_driver **out, size_t *namelen_out, const char *name) +{ + git_diff_driver *driver; + size_t driverlen = sizeof(git_diff_driver), + namelen = strlen(name), + alloclen; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, driverlen, namelen); + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); + + driver = git__calloc(1, alloclen); + GIT_ERROR_CHECK_ALLOC(driver); + + memcpy(driver->name, name, namelen); + + *out = driver; + + if (namelen_out) + *namelen_out = namelen; + + return 0; +} + +static int git_diff_driver_builtin( + git_diff_driver **out, + git_diff_driver_registry *reg, + const char *driver_name) +{ + git_diff_driver_definition *ddef = NULL; + git_diff_driver *drv = NULL; + int error = 0; + size_t idx; + + for (idx = 0; idx < ARRAY_SIZE(builtin_defs); ++idx) { + if (!strcasecmp(driver_name, builtin_defs[idx].name)) { + ddef = &builtin_defs[idx]; + break; + } + } + if (!ddef) + goto done; + + if ((error = diff_driver_alloc(&drv, NULL, ddef->name)) < 0) + goto done; + + drv->type = DIFF_DRIVER_PATTERNLIST; + + if (ddef->fns && + (error = diff_driver_add_patterns( + drv, ddef->fns, ddef->flags)) < 0) + goto done; + + if (ddef->words && + (error = git_regexp_compile(&drv->word_pattern, ddef->words, ddef->flags)) < 0) + goto done; + + if ((error = git_diff_driver_map_put(®->map, drv->name, drv)) < 0) + goto done; + +done: + if (error && drv) + git_diff_driver_free(drv); + else + *out = drv; + + return error; +} + +static int git_diff_driver_load( + git_diff_driver **out, git_repository *repo, const char *driver_name) +{ + int error = 0; + git_diff_driver_registry *reg; + git_diff_driver *drv = NULL; + size_t namelen; + git_config *cfg = NULL; + git_str name = GIT_STR_INIT; + git_config_entry *ce = NULL; + bool found_driver = false; + + if ((reg = git_repository_driver_registry(repo)) == NULL) + return -1; + + if (git_diff_driver_map_get(&drv, ®->map, driver_name) == 0) { + *out = drv; + return 0; + } + + if ((error = diff_driver_alloc(&drv, &namelen, driver_name)) < 0) + goto done; + + drv->type = DIFF_DRIVER_AUTO; + + /* if you can't read config for repo, just use default driver */ + if (git_repository_config_snapshot(&cfg, repo) < 0) { + git_error_clear(); + goto done; + } + + if ((error = git_str_printf(&name, "diff.%s.binary", driver_name)) < 0) + goto done; + + switch (git_config__get_bool_force(cfg, name.ptr, -1)) { + case true: + /* if diff..binary is true, just return the binary driver */ + *out = &diff_driver_binary; + goto done; + case false: + /* if diff..binary is false, force binary checks off */ + /* but still may have custom function context patterns, etc. */ + drv->binary_flags = GIT_DIFF_FORCE_TEXT; + found_driver = true; + break; + default: + /* diff..binary unspecified or "auto", so just continue */ + break; + } + + /* TODO: warn if diff..command or diff..textconv are set */ + + git_str_truncate(&name, namelen + strlen("diff..")); + if ((error = git_str_PUTS(&name, "xfuncname")) < 0) + goto done; + + if ((error = git_config_get_multivar_foreach( + cfg, name.ptr, NULL, diff_driver_xfuncname, drv)) < 0) { + if (error != GIT_ENOTFOUND) + goto done; + git_error_clear(); /* no diff..xfuncname, so just continue */ + } + + git_str_truncate(&name, namelen + strlen("diff..")); + if ((error = git_str_PUTS(&name, "funcname")) < 0) + goto done; + + if ((error = git_config_get_multivar_foreach( + cfg, name.ptr, NULL, diff_driver_funcname, drv)) < 0) { + if (error != GIT_ENOTFOUND) + goto done; + git_error_clear(); /* no diff..funcname, so just continue */ + } + + /* if we found any patterns, set driver type to use correct callback */ + if (git_array_size(drv->fn_patterns) > 0) { + drv->type = DIFF_DRIVER_PATTERNLIST; + found_driver = true; + } + + git_str_truncate(&name, namelen + strlen("diff..")); + if ((error = git_str_PUTS(&name, "wordregex")) < 0) + goto done; + + if ((error = git_config__lookup_entry(&ce, cfg, name.ptr, false)) < 0) + goto done; + if (!ce || !ce->value) + /* no diff..wordregex, so just continue */; + else if (!(error = git_regexp_compile(&drv->word_pattern, ce->value, 0))) + found_driver = true; + else { + /* TODO: warn about bad regex instead of failure */ + goto done; + } + + /* TODO: look up diff..algorithm to turn on minimal / patience + * diff in drv->other_flags + */ + + /* if no driver config found at all, fall back on AUTO driver */ + if (!found_driver) + goto done; + + /* store driver in registry */ + if ((error = git_diff_driver_map_put(®->map, drv->name, drv)) < 0) + goto done; + + *out = drv; + +done: + git_config_entry_free(ce); + git_str_dispose(&name); + git_config_free(cfg); + + if (!*out) { + int error2 = git_diff_driver_builtin(out, reg, driver_name); + if (!error) + error = error2; + } + + if (drv && drv != *out) + git_diff_driver_free(drv); + + return error; +} + +int git_diff_driver_lookup( + git_diff_driver **out, git_repository *repo, + git_attr_session *attrsession, const char *path) +{ + int error = 0; + const char *values[1], *attrs[] = { "diff" }; + + GIT_ASSERT_ARG(out); + *out = NULL; + + if (!repo || !path || !strlen(path)) + /* just use the auto value */; + else if ((error = git_attr_get_many_with_session(values, repo, + attrsession, 0, path, 1, attrs)) < 0) + /* return error below */; + + else if (GIT_ATTR_IS_UNSPECIFIED(values[0])) + /* just use the auto value */; + else if (GIT_ATTR_IS_FALSE(values[0])) + *out = &diff_driver_binary; + else if (GIT_ATTR_IS_TRUE(values[0])) + *out = &diff_driver_text; + + /* otherwise look for driver information in config and build driver */ + else if ((error = git_diff_driver_load(out, repo, values[0])) < 0) { + if (error == GIT_ENOTFOUND) { + error = 0; + git_error_clear(); + } + } + + if (!*out) + *out = &diff_driver_auto; + + return error; +} + +void git_diff_driver_free(git_diff_driver *driver) +{ + git_diff_driver_pattern *pat; + + if (!driver) + return; + + while ((pat = git_array_pop(driver->fn_patterns)) != NULL) + git_regexp_dispose(&pat->re); + git_array_clear(driver->fn_patterns); + + git_regexp_dispose(&driver->word_pattern); + + git__free(driver); +} + +void git_diff_driver_update_options( + uint32_t *option_flags, git_diff_driver *driver) +{ + if ((*option_flags & FORCE_DIFFABLE) == 0) + *option_flags |= driver->binary_flags; + + *option_flags |= driver->other_flags; +} + +int git_diff_driver_content_is_binary( + git_diff_driver *driver, const char *content, size_t content_len) +{ + git_str search = GIT_STR_INIT; + + GIT_UNUSED(driver); + + git_str_attach_notowned(&search, content, + min(content_len, GIT_FILTER_BYTES_TO_CHECK_NUL)); + + /* TODO: provide encoding / binary detection callbacks that can + * be UTF-8 aware, etc. For now, instead of trying to be smart, + * let's just use the simple NUL-byte detection that core git uses. + */ + + /* previously was: if (git_str_is_binary(&search)) */ + if (git_str_contains_nul(&search)) + return 1; + + return 0; +} + +static int diff_context_line__simple( + git_diff_driver *driver, git_str *line) +{ + char firstch = line->ptr[0]; + GIT_UNUSED(driver); + return (git__isalpha(firstch) || firstch == '_' || firstch == '$'); +} + +static int diff_context_line__pattern_match( + git_diff_driver *driver, git_str *line) +{ + size_t i, maxi = git_array_size(driver->fn_patterns); + git_regmatch pmatch[2]; + + for (i = 0; i < maxi; ++i) { + git_diff_driver_pattern *pat = git_array_get(driver->fn_patterns, i); + + if (!git_regexp_search(&pat->re, line->ptr, 2, pmatch)) { + if (pat->flags & REG_NEGATE) + return false; + + /* use pmatch data to trim line data */ + i = (pmatch[1].start >= 0) ? 1 : 0; + git_str_consume(line, git_str_cstr(line) + pmatch[i].start); + git_str_truncate(line, pmatch[i].end - pmatch[i].start); + git_str_rtrim(line); + + return true; + } + } + + return false; +} + +static long diff_context_find( + const char *line, + long line_len, + char *out, + long out_size, + void *payload) +{ + git_diff_find_context_payload *ctxt = payload; + + if (git_str_set(&ctxt->line, line, (size_t)line_len) < 0) + return -1; + git_str_rtrim(&ctxt->line); + + if (!ctxt->line.size) + return -1; + + if (!ctxt->match_line || !ctxt->match_line(ctxt->driver, &ctxt->line)) + return -1; + + if (out_size > (long)ctxt->line.size) + out_size = (long)ctxt->line.size; + memcpy(out, ctxt->line.ptr, (size_t)out_size); + + return out_size; +} + +void git_diff_find_context_init( + git_diff_find_context_fn *findfn_out, + git_diff_find_context_payload *payload_out, + git_diff_driver *driver) +{ + *findfn_out = driver ? diff_context_find : NULL; + + memset(payload_out, 0, sizeof(*payload_out)); + if (driver) { + payload_out->driver = driver; + payload_out->match_line = (driver->type == DIFF_DRIVER_PATTERNLIST) ? + diff_context_line__pattern_match : diff_context_line__simple; + git_str_init(&payload_out->line, 0); + } +} + +void git_diff_find_context_clear(git_diff_find_context_payload *payload) +{ + if (payload) { + git_str_dispose(&payload->line); + payload->driver = NULL; + } +} diff --git a/src/libgit2/diff_driver.h b/src/libgit2/diff_driver.h new file mode 100644 index 00000000000..ca0a7ae1e84 --- /dev/null +++ b/src/libgit2/diff_driver.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_diff_driver_h__ +#define INCLUDE_diff_driver_h__ + +#include "common.h" + +#include "attr_file.h" +#include "str.h" +#include "hashmap.h" + +typedef struct git_diff_driver git_diff_driver; +typedef struct git_diff_driver_registry git_diff_driver_registry; + +git_diff_driver_registry *git_diff_driver_registry_new(void); +void git_diff_driver_registry_free(git_diff_driver_registry *); + +int git_diff_driver_lookup(git_diff_driver **, git_repository *, + git_attr_session *attrsession, const char *); +void git_diff_driver_free(git_diff_driver *); + +/* diff option flags to force off and on for this driver */ +void git_diff_driver_update_options(uint32_t *option_flags, git_diff_driver *); + +/* returns -1 meaning "unknown", 0 meaning not binary, 1 meaning binary */ +int git_diff_driver_content_is_binary( + git_diff_driver *, const char *content, size_t content_len); + +typedef long (*git_diff_find_context_fn)( + const char *, long, char *, long, void *); + +typedef int (*git_diff_find_context_line)( + git_diff_driver *, git_str *); + +typedef struct { + git_diff_driver *driver; + git_diff_find_context_line match_line; + git_str line; +} git_diff_find_context_payload; + +void git_diff_find_context_init( + git_diff_find_context_fn *findfn_out, + git_diff_find_context_payload *payload_out, + git_diff_driver *driver); + +void git_diff_find_context_clear(git_diff_find_context_payload *); + +#endif diff --git a/src/libgit2/diff_file.c b/src/libgit2/diff_file.c new file mode 100644 index 00000000000..a792834caca --- /dev/null +++ b/src/libgit2/diff_file.c @@ -0,0 +1,490 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "diff_file.h" + +#include "git2/blob.h" +#include "git2/submodule.h" +#include "diff.h" +#include "diff_generate.h" +#include "odb.h" +#include "futils.h" +#include "filter.h" + +#define DIFF_MAX_FILESIZE 0x20000000 + +static bool diff_file_content_binary_by_size(git_diff_file_content *fc) +{ + /* if we have diff opts, check max_size vs file size */ + if ((fc->file->flags & DIFF_FLAGS_KNOWN_BINARY) == 0 && + fc->opts_max_size > 0 && + fc->file->size > fc->opts_max_size) + fc->file->flags |= GIT_DIFF_FLAG_BINARY; + + return ((fc->file->flags & GIT_DIFF_FLAG_BINARY) != 0); +} + +static void diff_file_content_binary_by_content(git_diff_file_content *fc) +{ + if ((fc->file->flags & DIFF_FLAGS_KNOWN_BINARY) != 0) + return; + + switch (git_diff_driver_content_is_binary( + fc->driver, fc->map.data, fc->map.len)) { + case 0: fc->file->flags |= GIT_DIFF_FLAG_NOT_BINARY; break; + case 1: fc->file->flags |= GIT_DIFF_FLAG_BINARY; break; + default: break; + } +} + +static int diff_file_content_init_common( + git_diff_file_content *fc, const git_diff_options *opts) +{ + fc->opts_flags = opts ? opts->flags : GIT_DIFF_NORMAL; + + if (opts && opts->max_size >= 0) + fc->opts_max_size = opts->max_size ? + opts->max_size : DIFF_MAX_FILESIZE; + + if (fc->src == GIT_ITERATOR_EMPTY) + fc->src = GIT_ITERATOR_TREE; + + if (!fc->driver && + git_diff_driver_lookup(&fc->driver, fc->repo, + NULL, fc->file->path) < 0) + return -1; + + /* give driver a chance to modify options */ + git_diff_driver_update_options(&fc->opts_flags, fc->driver); + + /* make sure file is conceivable mmap-able */ + if ((size_t)fc->file->size != fc->file->size) + fc->file->flags |= GIT_DIFF_FLAG_BINARY; + /* check if user is forcing text diff the file */ + else if (fc->opts_flags & GIT_DIFF_FORCE_TEXT) { + fc->file->flags &= ~GIT_DIFF_FLAG_BINARY; + fc->file->flags |= GIT_DIFF_FLAG_NOT_BINARY; + } + /* check if user is forcing binary diff the file */ + else if (fc->opts_flags & GIT_DIFF_FORCE_BINARY) { + fc->file->flags &= ~GIT_DIFF_FLAG_NOT_BINARY; + fc->file->flags |= GIT_DIFF_FLAG_BINARY; + } + + diff_file_content_binary_by_size(fc); + + if ((fc->flags & GIT_DIFF_FLAG__NO_DATA) != 0) { + fc->flags |= GIT_DIFF_FLAG__LOADED; + fc->map.len = 0; + fc->map.data = ""; + } + + if ((fc->flags & GIT_DIFF_FLAG__LOADED) != 0) + diff_file_content_binary_by_content(fc); + + return 0; +} + +int git_diff_file_content__init_from_diff( + git_diff_file_content *fc, + git_diff *diff, + git_diff_delta *delta, + bool use_old) +{ + bool has_data = true; + + memset(fc, 0, sizeof(*fc)); + fc->repo = diff->repo; + fc->file = use_old ? &delta->old_file : &delta->new_file; + fc->src = use_old ? diff->old_src : diff->new_src; + + if (git_diff_driver_lookup(&fc->driver, fc->repo, + &diff->attrsession, fc->file->path) < 0) + return -1; + + switch (delta->status) { + case GIT_DELTA_ADDED: + has_data = !use_old; break; + case GIT_DELTA_DELETED: + has_data = use_old; break; + case GIT_DELTA_UNTRACKED: + has_data = (use_old == (diff->opts.flags & GIT_DIFF_REVERSE)) && + (diff->opts.flags & GIT_DIFF_SHOW_UNTRACKED_CONTENT) != 0; + break; + case GIT_DELTA_UNREADABLE: + case GIT_DELTA_MODIFIED: + case GIT_DELTA_COPIED: + case GIT_DELTA_RENAMED: + break; + default: + has_data = false; + break; + } + + if (!has_data) + fc->flags |= GIT_DIFF_FLAG__NO_DATA; + + return diff_file_content_init_common(fc, &diff->opts); +} + +int git_diff_file_content__init_from_src( + git_diff_file_content *fc, + git_repository *repo, + const git_diff_options *opts, + const git_diff_file_content_src *src, + git_diff_file *as_file) +{ + memset(fc, 0, sizeof(*fc)); + fc->repo = repo; + fc->file = as_file; + + if (!src->blob && !src->buf) { + fc->flags |= GIT_DIFF_FLAG__NO_DATA; + git_oid_clear(&fc->file->id, opts->oid_type); + } else { + fc->flags |= GIT_DIFF_FLAG__LOADED; + fc->file->flags |= GIT_DIFF_FLAG_VALID_ID; + fc->file->mode = GIT_FILEMODE_BLOB; + + if (src->blob) { + git_blob_dup((git_blob **)&fc->blob, (git_blob *) src->blob); + fc->file->size = git_blob_rawsize(src->blob); + git_oid_cpy(&fc->file->id, git_blob_id(src->blob)); + fc->file->id_abbrev = (uint16_t)git_oid_hexsize(repo->oid_type); + + fc->map.len = (size_t)fc->file->size; + fc->map.data = (char *)git_blob_rawcontent(src->blob); + + fc->flags |= GIT_DIFF_FLAG__FREE_BLOB; + } else { + int error; + if ((error = git_odb__hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB, opts->oid_type)) < 0) + return error; + fc->file->size = src->buflen; + fc->file->id_abbrev = (uint16_t)git_oid_hexsize(opts->oid_type); + + fc->map.len = src->buflen; + fc->map.data = (char *)src->buf; + } + } + + return diff_file_content_init_common(fc, opts); +} + +static int diff_file_content_commit_to_str( + git_diff_file_content *fc, bool check_status) +{ + char oid[GIT_OID_MAX_HEXSIZE+1]; + git_str content = GIT_STR_INIT; + const char *status = ""; + + if (check_status) { + int error = 0; + git_submodule *sm = NULL; + unsigned int sm_status = 0; + const git_oid *sm_head; + + if ((error = git_submodule_lookup(&sm, fc->repo, fc->file->path)) < 0) { + /* GIT_EEXISTS means a "submodule" that has not been git added */ + if (error == GIT_EEXISTS) { + git_error_clear(); + error = 0; + } + return error; + } + + if ((error = git_submodule_status(&sm_status, fc->repo, fc->file->path, GIT_SUBMODULE_IGNORE_UNSPECIFIED)) < 0) { + git_submodule_free(sm); + return error; + } + + /* update OID if we didn't have it previously */ + if ((fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0 && + ((sm_head = git_submodule_wd_id(sm)) != NULL || + (sm_head = git_submodule_head_id(sm)) != NULL)) + { + git_oid_cpy(&fc->file->id, sm_head); + fc->file->flags |= GIT_DIFF_FLAG_VALID_ID; + } + + if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status)) + status = "-dirty"; + + git_submodule_free(sm); + } + + git_oid_tostr(oid, sizeof(oid), &fc->file->id); + if (git_str_printf(&content, "Subproject commit %s%s\n", oid, status) < 0) + return -1; + + fc->map.len = git_str_len(&content); + fc->map.data = git_str_detach(&content); + fc->flags |= GIT_DIFF_FLAG__FREE_DATA; + + return 0; +} + +static int diff_file_content_load_blob( + git_diff_file_content *fc, + git_diff_options *opts) +{ + int error = 0; + git_odb_object *odb_obj = NULL; + + if (git_oid_is_zero(&fc->file->id)) + return 0; + + if (fc->file->mode == GIT_FILEMODE_COMMIT) + return diff_file_content_commit_to_str(fc, false); + + /* if we don't know size, try to peek at object header first */ + if (!fc->file->size) { + if ((error = git_diff_file__resolve_zero_size( + fc->file, &odb_obj, fc->repo)) < 0) + return error; + } + + if ((opts->flags & GIT_DIFF_SHOW_BINARY) == 0 && + diff_file_content_binary_by_size(fc)) + return 0; + + if (odb_obj != NULL) { + error = git_object__from_odb_object( + (git_object **)&fc->blob, fc->repo, odb_obj, GIT_OBJECT_BLOB); + git_odb_object_free(odb_obj); + } else { + error = git_blob_lookup( + (git_blob **)&fc->blob, fc->repo, &fc->file->id); + } + + if (!error) { + fc->flags |= GIT_DIFF_FLAG__FREE_BLOB; + fc->map.data = (void *)git_blob_rawcontent(fc->blob); + fc->map.len = (size_t)git_blob_rawsize(fc->blob); + } + + return error; +} + +static int diff_file_content_load_workdir_symlink_fake( + git_diff_file_content *fc, git_str *path) +{ + git_str target = GIT_STR_INIT; + int error; + + if ((error = git_futils_readbuffer(&target, path->ptr)) < 0) + return error; + + fc->map.len = git_str_len(&target); + fc->map.data = git_str_detach(&target); + fc->flags |= GIT_DIFF_FLAG__FREE_DATA; + + git_str_dispose(&target); + return error; +} + +static int diff_file_content_load_workdir_symlink( + git_diff_file_content *fc, git_str *path) +{ + ssize_t alloc_len, read_len; + int symlink_supported, error; + + if ((error = git_repository__configmap_lookup( + &symlink_supported, fc->repo, GIT_CONFIGMAP_SYMLINKS)) < 0) + return -1; + + if (!symlink_supported) + return diff_file_content_load_workdir_symlink_fake(fc, path); + + /* link path on disk could be UTF-16, so prepare a buffer that is + * big enough to handle some UTF-8 data expansion + */ + alloc_len = (ssize_t)(fc->file->size * 2) + 1; + + fc->map.data = git__calloc(alloc_len, sizeof(char)); + GIT_ERROR_CHECK_ALLOC(fc->map.data); + + fc->flags |= GIT_DIFF_FLAG__FREE_DATA; + + read_len = p_readlink(git_str_cstr(path), fc->map.data, alloc_len); + if (read_len < 0) { + git_error_set(GIT_ERROR_OS, "failed to read symlink '%s'", fc->file->path); + return -1; + } + + fc->map.len = read_len; + return 0; +} + +static int diff_file_content_load_workdir_file( + git_diff_file_content *fc, + git_str *path, + git_diff_options *diff_opts) +{ + int error = 0; + git_filter_list *fl = NULL; + git_file fd = git_futils_open_ro(git_str_cstr(path)); + git_str raw = GIT_STR_INIT; + git_object_size_t new_file_size = 0; + + if (fd < 0) + return fd; + + error = git_futils_filesize(&new_file_size, fd); + + if (error < 0) + goto cleanup; + + if (!(fc->file->flags & GIT_DIFF_FLAG_VALID_SIZE)) { + fc->file->size = new_file_size; + fc->file->flags |= GIT_DIFF_FLAG_VALID_SIZE; + } else if (fc->file->size != new_file_size) { + git_error_set(GIT_ERROR_FILESYSTEM, "file changed before we could read it"); + error = -1; + goto cleanup; + } + + /* if file is empty, don't attempt to mmap or readbuffer */ + if (fc->file->size == 0) { + fc->map.len = 0; + fc->map.data = git_str__initstr; + goto cleanup; + } + + if ((diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0 && + diff_file_content_binary_by_size(fc)) + goto cleanup; + + if ((error = git_filter_list_load( + &fl, fc->repo, NULL, fc->file->path, + GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)) < 0) + goto cleanup; + + /* if there are no filters, try to mmap the file */ + if (fl == NULL) { + if (!(error = git_futils_mmap_ro( + &fc->map, fd, 0, (size_t)fc->file->size))) { + fc->flags |= GIT_DIFF_FLAG__UNMAP_DATA; + goto cleanup; + } + + /* if mmap failed, fall through to try readbuffer below */ + git_error_clear(); + } + + if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size))) { + git_str out = GIT_STR_INIT; + + error = git_filter_list__convert_buf(&out, fl, &raw); + + if (!error) { + fc->map.len = out.size; + fc->map.data = out.ptr; + fc->flags |= GIT_DIFF_FLAG__FREE_DATA; + } + } + +cleanup: + git_filter_list_free(fl); + p_close(fd); + + return error; +} + +static int diff_file_content_load_workdir( + git_diff_file_content *fc, + git_diff_options *diff_opts) +{ + int error = 0; + git_str path = GIT_STR_INIT; + + if (fc->file->mode == GIT_FILEMODE_COMMIT) + return diff_file_content_commit_to_str(fc, true); + + if (fc->file->mode == GIT_FILEMODE_TREE) + return 0; + + if (git_repository_workdir_path(&path, fc->repo, fc->file->path) < 0) + return -1; + + if (S_ISLNK(fc->file->mode)) + error = diff_file_content_load_workdir_symlink(fc, &path); + else + error = diff_file_content_load_workdir_file(fc, &path, diff_opts); + + /* once data is loaded, update OID if we didn't have it previously */ + if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0) { + error = git_odb__hash( + &fc->file->id, fc->map.data, fc->map.len, + GIT_OBJECT_BLOB, diff_opts->oid_type); + fc->file->flags |= GIT_DIFF_FLAG_VALID_ID; + } + + git_str_dispose(&path); + return error; +} + +int git_diff_file_content__load( + git_diff_file_content *fc, + git_diff_options *diff_opts) +{ + int error = 0; + + if ((fc->flags & GIT_DIFF_FLAG__LOADED) != 0) + return 0; + + if ((fc->file->flags & GIT_DIFF_FLAG_BINARY) != 0 && + (diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0) + return 0; + + if (fc->src == GIT_ITERATOR_WORKDIR) + error = diff_file_content_load_workdir(fc, diff_opts); + else + error = diff_file_content_load_blob(fc, diff_opts); + if (error) + return error; + + fc->flags |= GIT_DIFF_FLAG__LOADED; + + diff_file_content_binary_by_content(fc); + + return 0; +} + +void git_diff_file_content__unload(git_diff_file_content *fc) +{ + if ((fc->flags & GIT_DIFF_FLAG__LOADED) == 0) + return; + + if (fc->flags & GIT_DIFF_FLAG__FREE_DATA) { + git__free(fc->map.data); + fc->map.data = ""; + fc->map.len = 0; + fc->flags &= ~GIT_DIFF_FLAG__FREE_DATA; + } + else if (fc->flags & GIT_DIFF_FLAG__UNMAP_DATA) { + git_futils_mmap_free(&fc->map); + fc->map.data = ""; + fc->map.len = 0; + fc->flags &= ~GIT_DIFF_FLAG__UNMAP_DATA; + } + + if (fc->flags & GIT_DIFF_FLAG__FREE_BLOB) { + git_blob_free((git_blob *)fc->blob); + fc->blob = NULL; + fc->flags &= ~GIT_DIFF_FLAG__FREE_BLOB; + } + + fc->flags &= ~GIT_DIFF_FLAG__LOADED; +} + +void git_diff_file_content__clear(git_diff_file_content *fc) +{ + git_diff_file_content__unload(fc); + + /* for now, nothing else to do */ +} diff --git a/src/libgit2/diff_file.h b/src/libgit2/diff_file.h new file mode 100644 index 00000000000..8d743e821ff --- /dev/null +++ b/src/libgit2/diff_file.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_diff_file_h__ +#define INCLUDE_diff_file_h__ + +#include "common.h" + +#include "diff.h" +#include "diff_driver.h" +#include "map.h" + +/* expanded information for one side of a delta */ +typedef struct { + git_repository *repo; + git_diff_file *file; + git_diff_driver *driver; + uint32_t flags; + uint32_t opts_flags; + git_object_size_t opts_max_size; + git_iterator_t src; + const git_blob *blob; + git_map map; +} git_diff_file_content; + +extern int git_diff_file_content__init_from_diff( + git_diff_file_content *fc, + git_diff *diff, + git_diff_delta *delta, + bool use_old); + +typedef struct { + const git_blob *blob; + const void *buf; + size_t buflen; + const char *as_path; +} git_diff_file_content_src; + +#define GIT_DIFF_FILE_CONTENT_SRC__BLOB(BLOB,PATH) { (BLOB),NULL,0,(PATH) } +#define GIT_DIFF_FILE_CONTENT_SRC__BUF(BUF,LEN,PATH) { NULL,(BUF),(LEN),(PATH) } + +extern int git_diff_file_content__init_from_src( + git_diff_file_content *fc, + git_repository *repo, + const git_diff_options *opts, + const git_diff_file_content_src *src, + git_diff_file *as_file); + +/* this loads the blob/file-on-disk as needed */ +extern int git_diff_file_content__load( + git_diff_file_content *fc, + git_diff_options *diff_opts); + +/* this releases the blob/file-in-memory */ +extern void git_diff_file_content__unload(git_diff_file_content *fc); + +/* this unloads and also releases any other resources */ +extern void git_diff_file_content__clear(git_diff_file_content *fc); + +#endif diff --git a/src/libgit2/diff_generate.c b/src/libgit2/diff_generate.c new file mode 100644 index 00000000000..654145e34a6 --- /dev/null +++ b/src/libgit2/diff_generate.c @@ -0,0 +1,1750 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "diff_generate.h" + +#include "diff.h" +#include "patch_generate.h" +#include "futils.h" +#include "config.h" +#include "attr_file.h" +#include "filter.h" +#include "pathspec.h" +#include "index.h" +#include "odb.h" +#include "submodule.h" + +#define DIFF_FLAG_IS_SET(DIFF,FLAG) \ + (((DIFF)->base.opts.flags & (FLAG)) != 0) +#define DIFF_FLAG_ISNT_SET(DIFF,FLAG) \ + (((DIFF)->base.opts.flags & (FLAG)) == 0) +#define DIFF_FLAG_SET(DIFF,FLAG,VAL) (DIFF)->base.opts.flags = \ + (VAL) ? ((DIFF)->base.opts.flags | (FLAG)) : \ + ((DIFF)->base.opts.flags & ~(FLAG)) + +typedef struct { + struct git_diff base; + + git_vector pathspec; + + uint32_t diffcaps; + bool index_updated; +} git_diff_generated; + +static git_diff_delta *diff_delta__alloc( + git_diff_generated *diff, + git_delta_t status, + const char *path) +{ + git_diff_delta *delta = git__calloc(1, sizeof(git_diff_delta)); + if (!delta) + return NULL; + + delta->old_file.path = git_pool_strdup(&diff->base.pool, path); + if (delta->old_file.path == NULL) { + git__free(delta); + return NULL; + } + + delta->new_file.path = delta->old_file.path; + + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) { + switch (status) { + case GIT_DELTA_ADDED: status = GIT_DELTA_DELETED; break; + case GIT_DELTA_DELETED: status = GIT_DELTA_ADDED; break; + default: break; /* leave other status values alone */ + } + } + delta->status = status; + + git_oid_clear(&delta->old_file.id, diff->base.opts.oid_type); + git_oid_clear(&delta->new_file.id, diff->base.opts.oid_type); + + return delta; +} + +static int diff_insert_delta( + git_diff_generated *diff, + git_diff_delta *delta, + const char *matched_pathspec) +{ + int error = 0; + + if (diff->base.opts.notify_cb) { + error = diff->base.opts.notify_cb( + &diff->base, delta, matched_pathspec, diff->base.opts.payload); + + if (error) { + git__free(delta); + + if (error > 0) /* positive value means to skip this delta */ + return 0; + else /* negative value means to cancel diff */ + return git_error_set_after_callback_function(error, "git_diff"); + } + } + + if ((error = git_vector_insert(&diff->base.deltas, delta)) < 0) + git__free(delta); + + return error; +} + +static bool diff_pathspec_match( + const char **matched_pathspec, + git_diff_generated *diff, + const git_index_entry *entry) +{ + bool disable_pathspec_match = + DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH); + + /* If we're disabling fnmatch, then the iterator has already applied + * the filters to the files for us and we don't have to do anything. + * However, this only applies to *files* - the iterator will include + * directories that we need to recurse into when not autoexpanding, + * so we still need to apply the pathspec match to directories. + */ + if ((S_ISLNK(entry->mode) || S_ISREG(entry->mode)) && + disable_pathspec_match) { + *matched_pathspec = entry->path; + return true; + } + + return git_pathspec__match( + &diff->pathspec, entry->path, disable_pathspec_match, + DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE), + matched_pathspec, NULL); +} + +static void diff_delta__flag_known_size(git_diff_file *file) +{ + /* + * If we don't know the ID, that can only come from the workdir + * iterator, which means we *do* know the file size. This is a + * leaky abstraction, but alas. Otherwise, we test against the + * empty blob id. + */ + if (file->size || + !(file->flags & GIT_DIFF_FLAG_VALID_ID) || + git_oid_equal(&file->id, &git_oid__empty_blob_sha1)) + file->flags |= GIT_DIFF_FLAG_VALID_SIZE; +} + +static void diff_delta__flag_known_sizes(git_diff_delta *delta) +{ + diff_delta__flag_known_size(&delta->old_file); + diff_delta__flag_known_size(&delta->new_file); +} + +static int diff_delta__from_one( + git_diff_generated *diff, + git_delta_t status, + const git_index_entry *oitem, + const git_index_entry *nitem) +{ + const git_index_entry *entry = nitem; + bool has_old = false; + git_diff_delta *delta; + git_oid_t oid_type; + const char *matched_pathspec; + + GIT_ASSERT_ARG((oitem != NULL) ^ (nitem != NULL)); + + oid_type = diff->base.opts.oid_type; + + if (oitem) { + entry = oitem; + has_old = true; + } + + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) + has_old = !has_old; + + if ((entry->flags & GIT_INDEX_ENTRY_VALID) != 0) + return 0; + + if (status == GIT_DELTA_IGNORED && + DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED)) + return 0; + + if (status == GIT_DELTA_UNTRACKED && + DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED)) + return 0; + + if (status == GIT_DELTA_UNREADABLE && + DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE)) + return 0; + + if (!diff_pathspec_match(&matched_pathspec, diff, entry)) + return 0; + + delta = diff_delta__alloc(diff, status, entry->path); + GIT_ERROR_CHECK_ALLOC(delta); + + /* This fn is just for single-sided diffs */ + GIT_ASSERT(status != GIT_DELTA_MODIFIED); + delta->nfiles = 1; + + git_oid_clear(&delta->old_file.id, diff->base.opts.oid_type); + git_oid_clear(&delta->new_file.id, diff->base.opts.oid_type); + + if (has_old) { + delta->old_file.mode = entry->mode; + delta->old_file.size = entry->file_size; + delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS; + git_oid_cpy(&delta->old_file.id, &entry->id); + git_oid_clear(&delta->new_file.id, oid_type); + delta->old_file.id_abbrev = (uint16_t)git_oid_hexsize(oid_type); + } else /* ADDED, IGNORED, UNTRACKED */ { + delta->new_file.mode = entry->mode; + delta->new_file.size = entry->file_size; + delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS; + git_oid_clear(&delta->old_file.id, oid_type); + git_oid_cpy(&delta->new_file.id, &entry->id); + delta->new_file.id_abbrev = (uint16_t)git_oid_hexsize(oid_type); + } + + delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID; + + if (has_old || !git_oid_is_zero(&delta->new_file.id)) + delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID; + + diff_delta__flag_known_sizes(delta); + + return diff_insert_delta(diff, delta, matched_pathspec); +} + +static int diff_delta__from_two( + git_diff_generated *diff, + git_delta_t status, + const git_index_entry *old_entry, + uint32_t old_mode, + const git_index_entry *new_entry, + uint32_t new_mode, + const git_oid *new_id, + const char *matched_pathspec) +{ + const git_oid *old_id = &old_entry->id; + git_diff_delta *delta; + const char *canonical_path = old_entry->path; + git_oid_t oid_type; + + oid_type = diff->base.opts.oid_type; + + if (status == GIT_DELTA_UNMODIFIED && + DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED)) + return 0; + + if (!new_id) + new_id = &new_entry->id; + + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) { + uint32_t temp_mode = old_mode; + const git_index_entry *temp_entry = old_entry; + const git_oid *temp_id = old_id; + + old_entry = new_entry; + new_entry = temp_entry; + old_mode = new_mode; + new_mode = temp_mode; + old_id = new_id; + new_id = temp_id; + } + + delta = diff_delta__alloc(diff, status, canonical_path); + GIT_ERROR_CHECK_ALLOC(delta); + delta->nfiles = 2; + + if (!git_index_entry_is_conflict(old_entry)) { + delta->old_file.size = old_entry->file_size; + delta->old_file.mode = old_mode; + git_oid_cpy(&delta->old_file.id, old_id); + delta->old_file.id_abbrev = (uint16_t)git_oid_hexsize(oid_type); + delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID | + GIT_DIFF_FLAG_EXISTS; + } + + if (!git_index_entry_is_conflict(new_entry)) { + git_oid_cpy(&delta->new_file.id, new_id); + delta->new_file.id_abbrev = (uint16_t)git_oid_hexsize(oid_type); + delta->new_file.size = new_entry->file_size; + delta->new_file.mode = new_mode; + delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS; + delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS; + + if (!git_oid_is_zero(&new_entry->id)) + delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID; + } + + diff_delta__flag_known_sizes(delta); + + return diff_insert_delta(diff, delta, matched_pathspec); +} + +static git_diff_delta *diff_delta__last_for_item( + git_diff_generated *diff, + const git_index_entry *item) +{ + git_diff_delta *delta = git_vector_last(&diff->base.deltas); + if (!delta) + return NULL; + + switch (delta->status) { + case GIT_DELTA_UNMODIFIED: + case GIT_DELTA_DELETED: + if (git_oid__cmp(&delta->old_file.id, &item->id) == 0) + return delta; + break; + case GIT_DELTA_ADDED: + if (git_oid__cmp(&delta->new_file.id, &item->id) == 0) + return delta; + break; + case GIT_DELTA_UNREADABLE: + case GIT_DELTA_UNTRACKED: + if (diff->base.strcomp(delta->new_file.path, item->path) == 0 && + git_oid__cmp(&delta->new_file.id, &item->id) == 0) + return delta; + break; + case GIT_DELTA_MODIFIED: + if (git_oid__cmp(&delta->old_file.id, &item->id) == 0 || + (delta->new_file.mode == item->mode && + git_oid__cmp(&delta->new_file.id, &item->id) == 0)) + return delta; + break; + default: + break; + } + + return NULL; +} + +static char *diff_strdup_prefix(git_pool *pool, const char *prefix) +{ + size_t len = strlen(prefix); + + /* append '/' at end if needed */ + if (len > 0 && prefix[len - 1] != '/') + return git_pool_strcat(pool, prefix, "/"); + else + return git_pool_strndup(pool, prefix, len + 1); +} + +GIT_INLINE(const char *) diff_delta__i2w_path(const git_diff_delta *delta) +{ + return delta->old_file.path ? + delta->old_file.path : delta->new_file.path; +} + +static int diff_delta_i2w_cmp(const void *a, const void *b) +{ + const git_diff_delta *da = a, *db = b; + int val = strcmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db)); + return val ? val : ((int)da->status - (int)db->status); +} + +static int diff_delta_i2w_casecmp(const void *a, const void *b) +{ + const git_diff_delta *da = a, *db = b; + int val = strcasecmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db)); + return val ? val : ((int)da->status - (int)db->status); +} + +bool git_diff_delta__should_skip( + const git_diff_options *opts, const git_diff_delta *delta) +{ + uint32_t flags = opts ? opts->flags : 0; + + if (delta->status == GIT_DELTA_UNMODIFIED && + (flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0) + return true; + + if (delta->status == GIT_DELTA_IGNORED && + (flags & GIT_DIFF_INCLUDE_IGNORED) == 0) + return true; + + if (delta->status == GIT_DELTA_UNTRACKED && + (flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0) + return true; + + if (delta->status == GIT_DELTA_UNREADABLE && + (flags & GIT_DIFF_INCLUDE_UNREADABLE) == 0) + return true; + + return false; +} + + +static const char *diff_mnemonic_prefix( + git_iterator_t type, bool left_side) +{ + const char *pfx = ""; + + switch (type) { + case GIT_ITERATOR_EMPTY: pfx = "c"; break; + case GIT_ITERATOR_TREE: pfx = "c"; break; + case GIT_ITERATOR_INDEX: pfx = "i"; break; + case GIT_ITERATOR_WORKDIR: pfx = "w"; break; + case GIT_ITERATOR_FS: pfx = left_side ? "1" : "2"; break; + default: break; + } + + /* note: without a deeper look at pathspecs, there is no easy way + * to get the (o)bject / (w)ork tree mnemonics working... + */ + + return pfx; +} + +static void diff_set_ignore_case(git_diff *diff, bool ignore_case) +{ + if (!ignore_case) { + diff->opts.flags &= ~GIT_DIFF_IGNORE_CASE; + + diff->strcomp = git__strcmp; + diff->strncomp = git__strncmp; + diff->pfxcomp = git__prefixcmp; + diff->entrycomp = git_diff__entry_cmp; + + git_vector_set_cmp(&diff->deltas, git_diff_delta__cmp); + } else { + diff->opts.flags |= GIT_DIFF_IGNORE_CASE; + + diff->strcomp = git__strcasecmp; + diff->strncomp = git__strncasecmp; + diff->pfxcomp = git__prefixcmp_icase; + diff->entrycomp = git_diff__entry_icmp; + + git_vector_set_cmp(&diff->deltas, git_diff_delta__casecmp); + } + + git_vector_sort(&diff->deltas); +} + +static void diff_generated_free(git_diff *d) +{ + git_diff_generated *diff = (git_diff_generated *)d; + + git_attr_session__free(&diff->base.attrsession); + git_vector_dispose_deep(&diff->base.deltas); + + git_pathspec__vfree(&diff->pathspec); + git_pool_clear(&diff->base.pool); + + git__memzero(diff, sizeof(*diff)); + git__free(diff); +} + +static git_diff_generated *diff_generated_alloc( + git_repository *repo, + git_iterator *old_iter, + git_iterator *new_iter) +{ + git_diff_generated *diff; + git_diff_options dflt = GIT_DIFF_OPTIONS_INIT; + + GIT_ASSERT_ARG_WITH_RETVAL(repo, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(old_iter, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(new_iter, NULL); + + if ((diff = git__calloc(1, sizeof(git_diff_generated))) == NULL) + return NULL; + + GIT_REFCOUNT_INC(&diff->base); + diff->base.type = GIT_DIFF_TYPE_GENERATED; + diff->base.repo = repo; + diff->base.old_src = old_iter->type; + diff->base.new_src = new_iter->type; + diff->base.patch_fn = git_patch_generated_from_diff; + diff->base.free_fn = diff_generated_free; + git_attr_session__init(&diff->base.attrsession, repo); + memcpy(&diff->base.opts, &dflt, sizeof(git_diff_options)); + + if (git_pool_init(&diff->base.pool, 1) < 0 || + git_vector_init(&diff->base.deltas, 0, git_diff_delta__cmp) < 0) { + git_diff_free(&diff->base); + return NULL; + } + + /* Use case-insensitive compare if either iterator has + * the ignore_case bit set */ + diff_set_ignore_case( + &diff->base, + git_iterator_ignore_case(old_iter) || + git_iterator_ignore_case(new_iter)); + + return diff; +} + +static int diff_generated_apply_options( + git_diff_generated *diff, + const git_diff_options *opts) +{ + git_config *cfg = NULL; + git_repository *repo = diff->base.repo; + git_pool *pool = &diff->base.pool; + int val; + + if (opts) { + /* copy user options (except case sensitivity info from iterators) */ + bool icase = DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE); + memcpy(&diff->base.opts, opts, sizeof(diff->base.opts)); + DIFF_FLAG_SET(diff, GIT_DIFF_IGNORE_CASE, icase); + + /* initialize pathspec from options */ + if (git_pathspec__vinit(&diff->pathspec, &opts->pathspec, pool) < 0) + return -1; + } + + if (!diff->base.opts.oid_type) { + diff->base.opts.oid_type = repo->oid_type; + } else if (diff->base.opts.oid_type != repo->oid_type) { + git_error_set(GIT_ERROR_INVALID, + "specified object ID type does not match repository object ID type"); + return -1; + } + + /* flag INCLUDE_TYPECHANGE_TREES implies INCLUDE_TYPECHANGE */ + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES)) + diff->base.opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE; + + /* flag INCLUDE_UNTRACKED_CONTENT implies INCLUDE_UNTRACKED */ + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_SHOW_UNTRACKED_CONTENT)) + diff->base.opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED; + + /* load config values that affect diff behavior */ + if ((val = git_repository_config_snapshot(&cfg, repo)) < 0) + return val; + + if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_SYMLINKS) && val) + diff->diffcaps |= GIT_DIFFCAPS_HAS_SYMLINKS; + + if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_IGNORESTAT) && val) + diff->diffcaps |= GIT_DIFFCAPS_IGNORE_STAT; + + if ((diff->base.opts.flags & GIT_DIFF_IGNORE_FILEMODE) == 0 && + !git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_FILEMODE) && val) + diff->diffcaps |= GIT_DIFFCAPS_TRUST_MODE_BITS; + + if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_TRUSTCTIME) && val) + diff->diffcaps |= GIT_DIFFCAPS_TRUST_CTIME; + + /* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */ + + /* If not given explicit `opts`, check `diff.xyz` configs */ + if (!opts) { + int context = git_config__get_int_force(cfg, "diff.context", 3); + diff->base.opts.context_lines = context >= 0 ? (uint32_t)context : 3; + + /* add other defaults here */ + } + + /* Reverse src info if diff is reversed */ + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) { + git_iterator_t tmp_src = diff->base.old_src; + diff->base.old_src = diff->base.new_src; + diff->base.new_src = tmp_src; + } + + /* Unset UPDATE_INDEX unless diffing workdir and index */ + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && + (!(diff->base.old_src == GIT_ITERATOR_WORKDIR || + diff->base.new_src == GIT_ITERATOR_WORKDIR) || + !(diff->base.old_src == GIT_ITERATOR_INDEX || + diff->base.new_src == GIT_ITERATOR_INDEX))) + diff->base.opts.flags &= ~GIT_DIFF_UPDATE_INDEX; + + /* if ignore_submodules not explicitly set, check diff config */ + if (diff->base.opts.ignore_submodules <= 0) { + git_config_entry *entry; + git_config__lookup_entry(&entry, cfg, "diff.ignoresubmodules", true); + + if (entry && git_submodule_parse_ignore( + &diff->base.opts.ignore_submodules, entry->value) < 0) + git_error_clear(); + git_config_entry_free(entry); + } + + /* if either prefix is not set, figure out appropriate value */ + if (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) { + const char *use_old = DIFF_OLD_PREFIX_DEFAULT; + const char *use_new = DIFF_NEW_PREFIX_DEFAULT; + + if (git_config__get_bool_force(cfg, "diff.noprefix", 0)) + use_old = use_new = ""; + else if (git_config__get_bool_force(cfg, "diff.mnemonicprefix", 0)) { + use_old = diff_mnemonic_prefix(diff->base.old_src, true); + use_new = diff_mnemonic_prefix(diff->base.new_src, false); + } + + if (!diff->base.opts.old_prefix) + diff->base.opts.old_prefix = use_old; + if (!diff->base.opts.new_prefix) + diff->base.opts.new_prefix = use_new; + } + + /* strdup prefix from pool so we're not dependent on external data */ + diff->base.opts.old_prefix = diff_strdup_prefix(pool, diff->base.opts.old_prefix); + diff->base.opts.new_prefix = diff_strdup_prefix(pool, diff->base.opts.new_prefix); + + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) { + const char *tmp_prefix = diff->base.opts.old_prefix; + diff->base.opts.old_prefix = diff->base.opts.new_prefix; + diff->base.opts.new_prefix = tmp_prefix; + } + + git_config_free(cfg); + + /* check strdup results for error */ + return (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) ? -1 : 0; +} + +int git_diff__oid_for_file( + git_oid *out, + git_diff *diff, + const char *path, + uint16_t mode, + git_object_size_t size) +{ + git_index_entry entry; + + if (size > UINT32_MAX) { + git_error_set(GIT_ERROR_NOMEMORY, "file size overflow (for 32-bits) on '%s'", path); + return -1; + } + + memset(&entry, 0, sizeof(entry)); + entry.mode = mode; + entry.file_size = (uint32_t)size; + entry.path = (char *)path; + git_oid_clear(&entry.id, diff->opts.oid_type); + + return git_diff__oid_for_entry(out, diff, &entry, mode, NULL); +} + +int git_diff__oid_for_entry( + git_oid *out, + git_diff *d, + const git_index_entry *src, + uint16_t mode, + const git_oid *update_match) +{ + git_diff_generated *diff; + git_str full_path = GIT_STR_INIT; + git_index_entry entry = *src; + git_filter_list *fl = NULL; + int error = 0; + + GIT_ASSERT(d->type == GIT_DIFF_TYPE_GENERATED); + diff = (git_diff_generated *)d; + + git_oid_clear(out, diff->base.opts.oid_type); + + if (git_repository_workdir_path(&full_path, diff->base.repo, entry.path) < 0) + return -1; + + if (!mode) { + struct stat st; + + diff->base.perf.stat_calls++; + + if (p_stat(full_path.ptr, &st) < 0) { + error = git_fs_path_set_error(errno, entry.path, "stat"); + git_str_dispose(&full_path); + return error; + } + + git_index_entry__init_from_stat(&entry, + &st, (diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) != 0); + } + + /* calculate OID for file if possible */ + if (S_ISGITLINK(mode)) { + git_submodule *sm; + + if (!git_submodule_lookup(&sm, diff->base.repo, entry.path)) { + const git_oid *sm_oid = git_submodule_wd_id(sm); + if (sm_oid) + git_oid_cpy(out, sm_oid); + git_submodule_free(sm); + } else { + /* if submodule lookup failed probably just in an intermediate + * state where some init hasn't happened, so ignore the error + */ + git_error_clear(); + } + } else if (S_ISLNK(mode)) { + error = git_odb__hashlink(out, full_path.ptr, + diff->base.opts.oid_type); + diff->base.perf.oid_calculations++; + } else if (!git__is_sizet(entry.file_size)) { + git_error_set(GIT_ERROR_NOMEMORY, "file size overflow (for 32-bits) on '%s'", + entry.path); + error = -1; + } else if (!(error = git_filter_list_load(&fl, + diff->base.repo, NULL, entry.path, + GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE))) + { + int fd = git_futils_open_ro(full_path.ptr); + if (fd < 0) + error = fd; + else { + error = git_odb__hashfd_filtered( + out, fd, (size_t)entry.file_size, + GIT_OBJECT_BLOB, diff->base.opts.oid_type, + fl); + p_close(fd); + diff->base.perf.oid_calculations++; + } + + git_filter_list_free(fl); + } + + /* update index for entry if requested */ + if (!error && update_match && git_oid_equal(out, update_match)) { + git_index *idx; + git_index_entry updated_entry; + + memcpy(&updated_entry, &entry, sizeof(git_index_entry)); + updated_entry.mode = mode; + git_oid_cpy(&updated_entry.id, out); + + if (!(error = git_repository_index__weakptr(&idx, + diff->base.repo))) { + error = git_index_add(idx, &updated_entry); + diff->index_updated = true; + } + } + + git_str_dispose(&full_path); + return error; +} + +typedef struct { + git_repository *repo; + git_iterator *old_iter; + git_iterator *new_iter; + const git_index_entry *oitem; + const git_index_entry *nitem; + git_submodule_cache *submodule_cache; + bool submodule_cache_initialized; +} diff_in_progress; + +#define MODE_BITS_MASK 0000777 + +static int maybe_modified_submodule( + git_delta_t *status, + git_oid *found_oid, + git_diff_generated *diff, + diff_in_progress *info) +{ + int error = 0; + git_submodule *sub; + unsigned int sm_status = 0; + git_submodule_ignore_t ign = diff->base.opts.ignore_submodules; + git_submodule_cache *submodule_cache = NULL; + + *status = GIT_DELTA_UNMODIFIED; + + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES) || + ign == GIT_SUBMODULE_IGNORE_ALL) + return 0; + + if (diff->base.repo->submodule_cache != NULL) { + submodule_cache = diff->base.repo->submodule_cache; + } else { + if (!info->submodule_cache_initialized) { + info->submodule_cache_initialized = true; + /* + * Try to cache the submodule information to avoid having to parse it for + * every submodule. It is okay if it fails, the cache will still be NULL + * and the submodules will be attempted to be looked up individually. + */ + git_submodule_cache_init(&info->submodule_cache, diff->base.repo); + } + submodule_cache = info->submodule_cache; + } + + if ((error = git_submodule__lookup_with_cache( + &sub, diff->base.repo, info->nitem->path, submodule_cache)) < 0) { + + /* GIT_EEXISTS means dir with .git in it was found - ignore it */ + if (error == GIT_EEXISTS) { + git_error_clear(); + error = 0; + } + return error; + } + + if (ign <= 0 && git_submodule_ignore(sub) == GIT_SUBMODULE_IGNORE_ALL) + /* ignore it */; + else if ((error = git_submodule__status( + &sm_status, NULL, NULL, found_oid, sub, ign)) < 0) + /* return error below */; + + /* check IS_WD_UNMODIFIED because this case is only used + * when the new side of the diff is the working directory + */ + else if (!GIT_SUBMODULE_STATUS_IS_WD_UNMODIFIED(sm_status)) + *status = GIT_DELTA_MODIFIED; + + /* now that we have a HEAD OID, check if HEAD moved */ + else if ((sm_status & GIT_SUBMODULE_STATUS_IN_WD) != 0 && + !git_oid_equal(&info->oitem->id, found_oid)) + *status = GIT_DELTA_MODIFIED; + + git_submodule_free(sub); + return error; +} + +static int maybe_modified( + git_diff_generated *diff, + diff_in_progress *info) +{ + git_oid noid; + git_delta_t status = GIT_DELTA_MODIFIED; + const git_index_entry *oitem = info->oitem; + const git_index_entry *nitem = info->nitem; + unsigned int omode = oitem->mode; + unsigned int nmode = nitem->mode; + bool new_is_workdir = (info->new_iter->type == GIT_ITERATOR_WORKDIR); + bool modified_uncertain = false; + const char *matched_pathspec; + int error = 0; + + git_oid_clear(&noid, diff->base.opts.oid_type); + + if (!diff_pathspec_match(&matched_pathspec, diff, oitem)) + return 0; + + /* on platforms with no symlinks, preserve mode of existing symlinks */ + if (S_ISLNK(omode) && S_ISREG(nmode) && new_is_workdir && + !(diff->diffcaps & GIT_DIFFCAPS_HAS_SYMLINKS)) + nmode = omode; + + /* on platforms with no execmode, just preserve old mode */ + if (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) && + (nmode & MODE_BITS_MASK) != (omode & MODE_BITS_MASK) && + new_is_workdir) + nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK); + + /* if one side is a conflict, mark the whole delta as conflicted */ + if (git_index_entry_is_conflict(oitem) || + git_index_entry_is_conflict(nitem)) { + status = GIT_DELTA_CONFLICTED; + + /* support "assume unchanged" (poorly, b/c we still stat everything) */ + } else if ((oitem->flags & GIT_INDEX_ENTRY_VALID) != 0) { + status = GIT_DELTA_UNMODIFIED; + + /* support "skip worktree" index bit */ + } else if ((oitem->flags_extended & GIT_INDEX_ENTRY_SKIP_WORKTREE) != 0) { + status = GIT_DELTA_UNMODIFIED; + + /* if basic type of file changed, then split into delete and add */ + } else if (GIT_MODE_TYPE(omode) != GIT_MODE_TYPE(nmode)) { + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE)) { + status = GIT_DELTA_TYPECHANGE; + } + + else if (nmode == GIT_FILEMODE_UNREADABLE) { + if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL))) + error = diff_delta__from_one(diff, GIT_DELTA_UNREADABLE, NULL, nitem); + return error; + } + + else { + if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL))) + error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem); + return error; + } + + /* if oids and modes match (and are valid), then file is unmodified */ + } else if (git_oid_equal(&oitem->id, &nitem->id) && + omode == nmode && + !git_oid_is_zero(&oitem->id)) { + status = GIT_DELTA_UNMODIFIED; + + /* if we have an unknown OID and a workdir iterator, then check some + * circumstances that can accelerate things or need special handling + */ + } else if (git_oid_is_zero(&nitem->id) && new_is_workdir) { + bool use_ctime = + ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0); + git_index *index = git_iterator_index(info->new_iter); + + status = GIT_DELTA_UNMODIFIED; + + if (S_ISGITLINK(nmode)) { + if ((error = maybe_modified_submodule(&status, &noid, diff, info)) < 0) + return error; + } + + /* if the stat data looks different, then mark modified - this just + * means that the OID will be recalculated below to confirm change + */ + else if (omode != nmode || oitem->file_size != nitem->file_size) { + status = GIT_DELTA_MODIFIED; + modified_uncertain = + (oitem->file_size <= 0 && nitem->file_size > 0); + } + else if (!git_index_time_eq(&oitem->mtime, &nitem->mtime) || + (use_ctime && !git_index_time_eq(&oitem->ctime, &nitem->ctime)) || + oitem->ino != nitem->ino || + oitem->uid != nitem->uid || + oitem->gid != nitem->gid || + git_index_entry_newer_than_index(nitem, index)) + { + status = GIT_DELTA_MODIFIED; + modified_uncertain = true; + } + + /* if mode is GITLINK and submodules are ignored, then skip */ + } else if (S_ISGITLINK(nmode) && + DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES)) { + status = GIT_DELTA_UNMODIFIED; + } + + /* if we got here and decided that the files are modified, but we + * haven't calculated the OID of the new item, then calculate it now + */ + if (modified_uncertain && git_oid_is_zero(&nitem->id)) { + const git_oid *update_check = + DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && omode == nmode ? + &oitem->id : NULL; + + if ((error = git_diff__oid_for_entry( + &noid, &diff->base, nitem, nmode, update_check)) < 0) + return error; + + /* if oid matches, then mark unmodified (except submodules, where + * the filesystem content may be modified even if the oid still + * matches between the index and the workdir HEAD) + */ + if (omode == nmode && !S_ISGITLINK(omode) && + git_oid_equal(&oitem->id, &noid)) + status = GIT_DELTA_UNMODIFIED; + } + + /* If we want case changes, then break this into a delete of the old + * and an add of the new so that consumers can act accordingly (eg, + * checkout will update the case on disk.) + */ + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE) && + DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_CASECHANGE) && + strcmp(oitem->path, nitem->path) != 0) { + + if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL))) + error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem); + + return error; + } + + return diff_delta__from_two( + diff, status, oitem, omode, nitem, nmode, + git_oid_is_zero(&noid) ? NULL : &noid, matched_pathspec); +} + +static bool entry_is_prefixed( + git_diff_generated *diff, + const git_index_entry *item, + const git_index_entry *prefix_item) +{ + size_t pathlen; + + if (!item || diff->base.pfxcomp(item->path, prefix_item->path) != 0) + return false; + + pathlen = strlen(prefix_item->path); + + return (prefix_item->path[pathlen - 1] == '/' || + item->path[pathlen] == '\0' || + item->path[pathlen] == '/'); +} + +static int iterator_current( + const git_index_entry **entry, + git_iterator *iterator) +{ + int error; + + if ((error = git_iterator_current(entry, iterator)) == GIT_ITEROVER) { + *entry = NULL; + error = 0; + } + + return error; +} + +static int iterator_advance( + const git_index_entry **entry, + git_iterator *iterator) +{ + const git_index_entry *prev_entry = *entry; + int cmp, error; + + /* if we're looking for conflicts, we only want to report + * one conflict for each file, instead of all three sides. + * so if this entry is a conflict for this file, and the + * previous one was a conflict for the same file, skip it. + */ + while ((error = git_iterator_advance(entry, iterator)) == 0) { + if (!(iterator->flags & GIT_ITERATOR_INCLUDE_CONFLICTS) || + !git_index_entry_is_conflict(prev_entry) || + !git_index_entry_is_conflict(*entry)) + break; + + cmp = (iterator->flags & GIT_ITERATOR_IGNORE_CASE) ? + strcasecmp(prev_entry->path, (*entry)->path) : + strcmp(prev_entry->path, (*entry)->path); + + if (cmp) + break; + } + + if (error == GIT_ITEROVER) { + *entry = NULL; + error = 0; + } + + return error; +} + +static int iterator_advance_into( + const git_index_entry **entry, + git_iterator *iterator) +{ + int error; + + if ((error = git_iterator_advance_into(entry, iterator)) == GIT_ITEROVER) { + *entry = NULL; + error = 0; + } + + return error; +} + +static int iterator_advance_over( + const git_index_entry **entry, + git_iterator_status_t *status, + git_iterator *iterator) +{ + int error = git_iterator_advance_over(entry, status, iterator); + + if (error == GIT_ITEROVER) { + *entry = NULL; + error = 0; + } + + return error; +} + +static int handle_unmatched_new_item( + git_diff_generated *diff, diff_in_progress *info) +{ + int error = 0; + const git_index_entry *nitem = info->nitem; + git_delta_t delta_type = GIT_DELTA_UNTRACKED; + bool contains_oitem; + + /* check if this is a prefix of the other side */ + contains_oitem = entry_is_prefixed(diff, info->oitem, nitem); + + /* update delta_type if this item is conflicted */ + if (git_index_entry_is_conflict(nitem)) + delta_type = GIT_DELTA_CONFLICTED; + + /* update delta_type if this item is ignored */ + else if (git_iterator_current_is_ignored(info->new_iter)) + delta_type = GIT_DELTA_IGNORED; + + if (nitem->mode == GIT_FILEMODE_TREE) { + bool recurse_into_dir = contains_oitem; + + /* check if user requests recursion into this type of dir */ + recurse_into_dir = contains_oitem || + (delta_type == GIT_DELTA_UNTRACKED && + DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) || + (delta_type == GIT_DELTA_IGNORED && + DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS)); + + /* do not advance into directories that contain a .git file */ + if (recurse_into_dir && !contains_oitem) { + git_str *full = NULL; + if (git_iterator_current_workdir_path(&full, info->new_iter) < 0) + return -1; + if (full && git_fs_path_contains(full, DOT_GIT)) { + /* TODO: warning if not a valid git repository */ + recurse_into_dir = false; + } + } + + /* still have to look into untracked directories to match core git - + * with no untracked files, directory is treated as ignored + */ + if (!recurse_into_dir && + delta_type == GIT_DELTA_UNTRACKED && + DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS)) + { + git_diff_delta *last; + git_iterator_status_t untracked_state; + + /* attempt to insert record for this directory */ + if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0) + return error; + + /* if delta wasn't created (because of rules), just skip ahead */ + last = diff_delta__last_for_item(diff, nitem); + if (!last) + return iterator_advance(&info->nitem, info->new_iter); + + /* iterate into dir looking for an actual untracked file */ + if ((error = iterator_advance_over( + &info->nitem, &untracked_state, info->new_iter)) < 0) + return error; + + /* if we found nothing that matched our pathlist filter, exclude */ + if (untracked_state == GIT_ITERATOR_STATUS_FILTERED) { + git_vector_pop(&diff->base.deltas); + git__free(last); + } + + /* if we found nothing or just ignored items, update the record */ + if (untracked_state == GIT_ITERATOR_STATUS_IGNORED || + untracked_state == GIT_ITERATOR_STATUS_EMPTY) { + last->status = GIT_DELTA_IGNORED; + + /* remove the record if we don't want ignored records */ + if (DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED)) { + git_vector_pop(&diff->base.deltas); + git__free(last); + } + } + + return 0; + } + + /* try to advance into directory if necessary */ + if (recurse_into_dir) { + error = iterator_advance_into(&info->nitem, info->new_iter); + + /* if directory is empty, can't advance into it, so skip it */ + if (error == GIT_ENOTFOUND) { + git_error_clear(); + error = iterator_advance(&info->nitem, info->new_iter); + } + + return error; + } + } + + else if (delta_type == GIT_DELTA_IGNORED && + DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS) && + git_iterator_current_tree_is_ignored(info->new_iter)) + /* item contained in ignored directory, so skip over it */ + return iterator_advance(&info->nitem, info->new_iter); + + else if (info->new_iter->type != GIT_ITERATOR_WORKDIR) { + if (delta_type != GIT_DELTA_CONFLICTED) + delta_type = GIT_DELTA_ADDED; + } + + else if (nitem->mode == GIT_FILEMODE_COMMIT) { + /* ignore things that are not actual submodules */ + if (git_submodule_lookup(NULL, info->repo, nitem->path) != 0) { + git_error_clear(); + delta_type = GIT_DELTA_IGNORED; + + /* if this contains a tracked item, treat as normal TREE */ + if (contains_oitem) { + error = iterator_advance_into(&info->nitem, info->new_iter); + if (error != GIT_ENOTFOUND) + return error; + + git_error_clear(); + return iterator_advance(&info->nitem, info->new_iter); + } + } + } + + else if (nitem->mode == GIT_FILEMODE_UNREADABLE) { + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED)) + delta_type = GIT_DELTA_UNTRACKED; + else + delta_type = GIT_DELTA_UNREADABLE; + } + + /* Actually create the record for this item if necessary */ + if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0) + return error; + + /* If user requested TYPECHANGE records, then check for that instead of + * just generating an ADDED/UNTRACKED record + */ + if (delta_type != GIT_DELTA_IGNORED && + DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) && + contains_oitem) + { + /* this entry was prefixed with a tree - make TYPECHANGE */ + git_diff_delta *last = diff_delta__last_for_item(diff, nitem); + if (last) { + last->status = GIT_DELTA_TYPECHANGE; + last->old_file.mode = GIT_FILEMODE_TREE; + } + } + + return iterator_advance(&info->nitem, info->new_iter); +} + +static int handle_unmatched_old_item( + git_diff_generated *diff, diff_in_progress *info) +{ + git_delta_t delta_type = GIT_DELTA_DELETED; + int error; + + /* update delta_type if this item is conflicted */ + if (git_index_entry_is_conflict(info->oitem)) + delta_type = GIT_DELTA_CONFLICTED; + + if ((error = diff_delta__from_one(diff, delta_type, info->oitem, NULL)) < 0) + return error; + + /* if we are generating TYPECHANGE records then check for that + * instead of just generating a DELETE record + */ + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) && + entry_is_prefixed(diff, info->nitem, info->oitem)) + { + /* this entry has become a tree! convert to TYPECHANGE */ + git_diff_delta *last = diff_delta__last_for_item(diff, info->oitem); + if (last) { + last->status = GIT_DELTA_TYPECHANGE; + last->new_file.mode = GIT_FILEMODE_TREE; + } + + /* If new_iter is a workdir iterator, then this situation + * will certainly be followed by a series of untracked items. + * Unless RECURSE_UNTRACKED_DIRS is set, skip over them... + */ + if (S_ISDIR(info->nitem->mode) && + DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) + return iterator_advance(&info->nitem, info->new_iter); + } + + return iterator_advance(&info->oitem, info->old_iter); +} + +static int handle_matched_item( + git_diff_generated *diff, diff_in_progress *info) +{ + int error = 0; + + if ((error = maybe_modified(diff, info)) < 0) + return error; + + if (!(error = iterator_advance(&info->oitem, info->old_iter))) + error = iterator_advance(&info->nitem, info->new_iter); + + return error; +} + +int git_diff__from_iterators( + git_diff **out, + git_repository *repo, + git_iterator *old_iter, + git_iterator *new_iter, + const git_diff_options *opts) +{ + git_diff_generated *diff; + diff_in_progress info = {0}; + int error = 0; + + *out = NULL; + + diff = diff_generated_alloc(repo, old_iter, new_iter); + GIT_ERROR_CHECK_ALLOC(diff); + + info.repo = repo; + info.old_iter = old_iter; + info.new_iter = new_iter; + + /* make iterators have matching icase behavior */ + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE)) { + if ((error = git_iterator_set_ignore_case(old_iter, true)) < 0 || + (error = git_iterator_set_ignore_case(new_iter, true)) < 0) + goto cleanup; + } + + /* finish initialization */ + if ((error = diff_generated_apply_options(diff, opts)) < 0) + goto cleanup; + + if ((error = iterator_current(&info.oitem, old_iter)) < 0 || + (error = iterator_current(&info.nitem, new_iter)) < 0) + goto cleanup; + + /* run iterators building diffs */ + while (!error && (info.oitem || info.nitem)) { + int cmp; + + /* report progress */ + if (opts && opts->progress_cb) { + if ((error = opts->progress_cb(&diff->base, + info.oitem ? info.oitem->path : NULL, + info.nitem ? info.nitem->path : NULL, + opts->payload))) + break; + } + + cmp = info.oitem ? + (info.nitem ? diff->base.entrycomp(info.oitem, info.nitem) : -1) : 1; + + /* create DELETED records for old items not matched in new */ + if (cmp < 0) + error = handle_unmatched_old_item(diff, &info); + + /* create ADDED, TRACKED, or IGNORED records for new items not + * matched in old (and/or descend into directories as needed) + */ + else if (cmp > 0) + error = handle_unmatched_new_item(diff, &info); + + /* otherwise item paths match, so create MODIFIED record + * (or ADDED and DELETED pair if type changed) + */ + else + error = handle_matched_item(diff, &info); + } + + diff->base.perf.stat_calls += + old_iter->stat_calls + new_iter->stat_calls; + +cleanup: + if (!error) + *out = &diff->base; + else + git_diff_free(&diff->base); + if (info.submodule_cache) + git_submodule_cache_free(info.submodule_cache); + + return error; +} + +static int diff_prepare_iterator_opts(char **prefix, git_iterator_options *a, int aflags, + git_iterator_options *b, int bflags, + const git_diff_options *opts) +{ + GIT_ERROR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); + + *prefix = NULL; + + if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) { + a->pathlist.strings = opts->pathspec.strings; + a->pathlist.count = opts->pathspec.count; + b->pathlist.strings = opts->pathspec.strings; + b->pathlist.count = opts->pathspec.count; + } else if (opts) { + *prefix = git_pathspec_prefix(&opts->pathspec); + GIT_ERROR_CHECK_ALLOC(prefix); + } + + a->flags = aflags; + b->flags = bflags; + a->start = b->start = *prefix; + a->end = b->end = *prefix; + + return 0; +} + +int git_diff_tree_to_tree( + git_diff **out, + git_repository *repo, + git_tree *old_tree, + git_tree *new_tree, + const git_diff_options *opts) +{ + git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE; + git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, + b_opts = GIT_ITERATOR_OPTIONS_INIT; + git_iterator *a = NULL, *b = NULL; + git_diff *diff = NULL; + char *prefix = NULL; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + *out = NULL; + + /* for tree to tree diff, be case sensitive even if the index is + * currently case insensitive, unless the user explicitly asked + * for case insensitivity + */ + if (opts && (opts->flags & GIT_DIFF_IGNORE_CASE) != 0) + iflag = GIT_ITERATOR_IGNORE_CASE; + + if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, iflag, &b_opts, iflag, opts)) < 0 || + (error = git_iterator_for_tree(&a, old_tree, &a_opts)) < 0 || + (error = git_iterator_for_tree(&b, new_tree, &b_opts)) < 0 || + (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0) + goto out; + + *out = diff; + diff = NULL; +out: + git_iterator_free(a); + git_iterator_free(b); + git_diff_free(diff); + git__free(prefix); + + return error; +} + +static int diff_load_index(git_index **index, git_repository *repo) +{ + int error = git_repository_index__weakptr(index, repo); + + /* reload the repository index when user did not pass one in */ + if (!error && git_index_read(*index, false) < 0) + git_error_clear(); + + return error; +} + +int git_diff_tree_to_index( + git_diff **out, + git_repository *repo, + git_tree *old_tree, + git_index *index, + const git_diff_options *opts) +{ + git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_INCLUDE_CONFLICTS; + git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, + b_opts = GIT_ITERATOR_OPTIONS_INIT; + git_iterator *a = NULL, *b = NULL; + git_diff *diff = NULL; + char *prefix = NULL; + bool index_ignore_case = false; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + *out = NULL; + + if (!index && (error = diff_load_index(&index, repo)) < 0) + return error; + + index_ignore_case = index->ignore_case; + + if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, iflag, &b_opts, iflag, opts)) < 0 || + (error = git_iterator_for_tree(&a, old_tree, &a_opts)) < 0 || + (error = git_iterator_for_index(&b, repo, index, &b_opts)) < 0 || + (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0) + goto out; + + /* if index is in case-insensitive order, re-sort deltas to match */ + if (index_ignore_case) + diff_set_ignore_case(diff, true); + + *out = diff; + diff = NULL; +out: + git_iterator_free(a); + git_iterator_free(b); + git_diff_free(diff); + git__free(prefix); + + return error; +} + +int git_diff_index_to_workdir( + git_diff **out, + git_repository *repo, + git_index *index, + const git_diff_options *opts) +{ + git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, + b_opts = GIT_ITERATOR_OPTIONS_INIT; + git_iterator *a = NULL, *b = NULL; + git_diff *diff = NULL; + char *prefix = NULL; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + *out = NULL; + + if (!index && (error = diff_load_index(&index, repo)) < 0) + return error; + + if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, GIT_ITERATOR_INCLUDE_CONFLICTS, + &b_opts, GIT_ITERATOR_DONT_AUTOEXPAND, opts)) < 0 || + (error = git_iterator_for_index(&a, repo, index, &a_opts)) < 0 || + (error = git_iterator_for_workdir(&b, repo, index, NULL, &b_opts)) < 0 || + (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0) + goto out; + + if ((diff->opts.flags & GIT_DIFF_UPDATE_INDEX) && ((git_diff_generated *)diff)->index_updated) + if ((error = git_index_write(index)) < 0) + goto out; + + *out = diff; + diff = NULL; +out: + git_iterator_free(a); + git_iterator_free(b); + git_diff_free(diff); + git__free(prefix); + + return error; +} + +int git_diff_tree_to_workdir( + git_diff **out, + git_repository *repo, + git_tree *old_tree, + const git_diff_options *opts) +{ + git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, + b_opts = GIT_ITERATOR_OPTIONS_INIT; + git_iterator *a = NULL, *b = NULL; + git_diff *diff = NULL; + char *prefix = NULL; + git_index *index; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + *out = NULL; + + if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, 0, + &b_opts, GIT_ITERATOR_DONT_AUTOEXPAND, opts) < 0) || + (error = git_repository_index__weakptr(&index, repo)) < 0 || + (error = git_iterator_for_tree(&a, old_tree, &a_opts)) < 0 || + (error = git_iterator_for_workdir(&b, repo, index, old_tree, &b_opts)) < 0 || + (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0) + goto out; + + *out = diff; + diff = NULL; +out: + git_iterator_free(a); + git_iterator_free(b); + git_diff_free(diff); + git__free(prefix); + + return error; +} + +int git_diff_tree_to_workdir_with_index( + git_diff **out, + git_repository *repo, + git_tree *tree, + const git_diff_options *opts) +{ + git_diff *d1 = NULL, *d2 = NULL; + git_index *index = NULL; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + *out = NULL; + + if ((error = diff_load_index(&index, repo)) < 0) + return error; + + if (!(error = git_diff_tree_to_index(&d1, repo, tree, index, opts)) && + !(error = git_diff_index_to_workdir(&d2, repo, index, opts))) + error = git_diff_merge(d1, d2); + + git_diff_free(d2); + + if (error) { + git_diff_free(d1); + d1 = NULL; + } + + *out = d1; + return error; +} + +int git_diff_index_to_index( + git_diff **out, + git_repository *repo, + git_index *old_index, + git_index *new_index, + const git_diff_options *opts) +{ + git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, + b_opts = GIT_ITERATOR_OPTIONS_INIT; + git_iterator *a = NULL, *b = NULL; + git_diff *diff = NULL; + char *prefix = NULL; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(old_index); + GIT_ASSERT_ARG(new_index); + + *out = NULL; + + if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, GIT_ITERATOR_DONT_IGNORE_CASE, + &b_opts, GIT_ITERATOR_DONT_IGNORE_CASE, opts) < 0) || + (error = git_iterator_for_index(&a, repo, old_index, &a_opts)) < 0 || + (error = git_iterator_for_index(&b, repo, new_index, &b_opts)) < 0 || + (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0) + goto out; + + /* if index is in case-insensitive order, re-sort deltas to match */ + if (old_index->ignore_case || new_index->ignore_case) + diff_set_ignore_case(diff, true); + + *out = diff; + diff = NULL; +out: + git_iterator_free(a); + git_iterator_free(b); + git_diff_free(diff); + git__free(prefix); + + return error; +} + +int git_diff__paired_foreach( + git_diff *head2idx, + git_diff *idx2wd, + int (*cb)(git_diff_delta *h2i, git_diff_delta *i2w, void *payload), + void *payload) +{ + int cmp, error = 0; + git_diff_delta *h2i, *i2w; + size_t i, j, i_max, j_max; + int (*strcomp)(const char *, const char *) = git__strcmp; + bool h2i_icase, i2w_icase, icase_mismatch; + + i_max = head2idx ? head2idx->deltas.length : 0; + j_max = idx2wd ? idx2wd->deltas.length : 0; + if (!i_max && !j_max) + return 0; + + /* At some point, tree-to-index diffs will probably never ignore case, + * even if that isn't true now. Index-to-workdir diffs may or may not + * ignore case, but the index filename for the idx2wd diff should + * still be using the canonical case-preserving name. + * + * Therefore the main thing we need to do here is make sure the diffs + * are traversed in a compatible order. To do this, we temporarily + * resort a mismatched diff to get the order correct. + * + * In order to traverse renames in the index->workdir, we need to + * ensure that we compare the index name on both sides, so we + * always sort by the old name in the i2w list. + */ + h2i_icase = head2idx != NULL && git_diff_is_sorted_icase(head2idx); + i2w_icase = idx2wd != NULL && git_diff_is_sorted_icase(idx2wd); + + icase_mismatch = + (head2idx != NULL && idx2wd != NULL && h2i_icase != i2w_icase); + + if (icase_mismatch && h2i_icase) { + git_vector_set_cmp(&head2idx->deltas, git_diff_delta__cmp); + git_vector_sort(&head2idx->deltas); + } + + if (i2w_icase && !icase_mismatch) { + strcomp = git__strcasecmp; + + git_vector_set_cmp(&idx2wd->deltas, diff_delta_i2w_casecmp); + git_vector_sort(&idx2wd->deltas); + } else if (idx2wd != NULL) { + git_vector_set_cmp(&idx2wd->deltas, diff_delta_i2w_cmp); + git_vector_sort(&idx2wd->deltas); + } + + for (i = 0, j = 0; i < i_max || j < j_max; ) { + h2i = head2idx ? GIT_VECTOR_GET(&head2idx->deltas, i) : NULL; + i2w = idx2wd ? GIT_VECTOR_GET(&idx2wd->deltas, j) : NULL; + + cmp = !i2w ? -1 : !h2i ? 1 : + strcomp(h2i->new_file.path, i2w->old_file.path); + + if (cmp < 0) { + i++; i2w = NULL; + } else if (cmp > 0) { + j++; h2i = NULL; + } else { + i++; j++; + } + + if ((error = cb(h2i, i2w, payload)) != 0) { + git_error_set_after_callback(error); + break; + } + } + + /* restore case-insensitive delta sort */ + if (icase_mismatch && h2i_icase) { + git_vector_set_cmp(&head2idx->deltas, git_diff_delta__casecmp); + git_vector_sort(&head2idx->deltas); + } + + /* restore idx2wd sort by new path */ + if (idx2wd != NULL) { + git_vector_set_cmp(&idx2wd->deltas, + i2w_icase ? git_diff_delta__casecmp : git_diff_delta__cmp); + git_vector_sort(&idx2wd->deltas); + } + + return error; +} + +int git_diff__commit( + git_diff **out, + git_repository *repo, + const git_commit *commit, + const git_diff_options *opts) +{ + git_commit *parent = NULL; + git_diff *commit_diff = NULL; + git_tree *old_tree = NULL, *new_tree = NULL; + size_t parents; + int error = 0; + + *out = NULL; + + if ((parents = git_commit_parentcount(commit)) > 1) { + char commit_oidstr[GIT_OID_MAX_HEXSIZE + 1]; + + error = -1; + git_error_set(GIT_ERROR_INVALID, "commit %s is a merge commit", + git_oid_tostr(commit_oidstr, GIT_OID_MAX_HEXSIZE + 1, git_commit_id(commit))); + goto on_error; + } + + if (parents > 0) + if ((error = git_commit_parent(&parent, commit, 0)) < 0 || + (error = git_commit_tree(&old_tree, parent)) < 0) + goto on_error; + + if ((error = git_commit_tree(&new_tree, commit)) < 0 || + (error = git_diff_tree_to_tree(&commit_diff, repo, old_tree, new_tree, opts)) < 0) + goto on_error; + + *out = commit_diff; + +on_error: + git_tree_free(new_tree); + git_tree_free(old_tree); + git_commit_free(parent); + + return error; +} + diff --git a/src/libgit2/diff_generate.h b/src/libgit2/diff_generate.h new file mode 100644 index 00000000000..b782f29c6e4 --- /dev/null +++ b/src/libgit2/diff_generate.h @@ -0,0 +1,130 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_diff_generate_h__ +#define INCLUDE_diff_generate_h__ + +#include "common.h" + +#include "diff.h" +#include "pool.h" +#include "index.h" + +enum { + GIT_DIFFCAPS_HAS_SYMLINKS = (1 << 0), /* symlinks on platform? */ + GIT_DIFFCAPS_IGNORE_STAT = (1 << 1), /* use stat? */ + GIT_DIFFCAPS_TRUST_MODE_BITS = (1 << 2), /* use st_mode? */ + GIT_DIFFCAPS_TRUST_CTIME = (1 << 3), /* use st_ctime? */ + GIT_DIFFCAPS_USE_DEV = (1 << 4) /* use st_dev? */ +}; + +#define DIFF_FLAGS_KNOWN_BINARY (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY) +#define DIFF_FLAGS_NOT_BINARY (GIT_DIFF_FLAG_NOT_BINARY|GIT_DIFF_FLAG__NO_DATA) + +enum { + GIT_DIFF_FLAG__FREE_PATH = (1 << 7), /* `path` is allocated memory */ + GIT_DIFF_FLAG__FREE_DATA = (1 << 8), /* internal file data is allocated */ + GIT_DIFF_FLAG__UNMAP_DATA = (1 << 9), /* internal file data is mmap'ed */ + GIT_DIFF_FLAG__NO_DATA = (1 << 10), /* file data should not be loaded */ + GIT_DIFF_FLAG__FREE_BLOB = (1 << 11), /* release the blob when done */ + GIT_DIFF_FLAG__LOADED = (1 << 12), /* file data has been loaded */ + + GIT_DIFF_FLAG__TO_DELETE = (1 << 16), /* delete entry during rename det. */ + GIT_DIFF_FLAG__TO_SPLIT = (1 << 17), /* split entry during rename det. */ + GIT_DIFF_FLAG__IS_RENAME_TARGET = (1 << 18), + GIT_DIFF_FLAG__IS_RENAME_SOURCE = (1 << 19), + GIT_DIFF_FLAG__HAS_SELF_SIMILARITY = (1 << 20) +}; + +#define GIT_DIFF_FLAG__CLEAR_INTERNAL(F) (F) = ((F) & 0x00FFFF) + +#define GIT_DIFF__VERBOSE (1 << 30) + +extern void git_diff_addref(git_diff *diff); + +extern bool git_diff_delta__should_skip( + const git_diff_options *opts, const git_diff_delta *delta); + +extern int git_diff__from_iterators( + git_diff **diff_ptr, + git_repository *repo, + git_iterator *old_iter, + git_iterator *new_iter, + const git_diff_options *opts); + +extern int git_diff__commit( + git_diff **diff, git_repository *repo, const git_commit *commit, const git_diff_options *opts); + +extern int git_diff__paired_foreach( + git_diff *idx2head, + git_diff *wd2idx, + int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload), + void *payload); + +/* Merge two `git_diff`s according to the callback given by `cb`. */ + +typedef git_diff_delta *(*git_diff__merge_cb)( + const git_diff_delta *left, + const git_diff_delta *right, + git_pool *pool); + +extern int git_diff__merge( + git_diff *onto, const git_diff *from, git_diff__merge_cb cb); + +extern git_diff_delta *git_diff__merge_like_cgit( + const git_diff_delta *a, + const git_diff_delta *b, + git_pool *pool); + +/* Duplicate a `git_diff_delta` out of the `git_pool` */ +extern git_diff_delta *git_diff__delta_dup( + const git_diff_delta *d, git_pool *pool); + +extern int git_diff__oid_for_file( + git_oid *out, + git_diff *diff, + const char *path, + uint16_t mode, + git_object_size_t size); + +extern int git_diff__oid_for_entry( + git_oid *out, + git_diff *diff, + const git_index_entry *src, + uint16_t mode, + const git_oid *update_match); + +/* + * Sometimes a git_diff_file will have a zero size; this attempts to + * fill in the size without loading the blob if possible. If that is + * not possible, then it will return the git_odb_object that had to be + * loaded and the caller can use it or dispose of it as needed. + */ +GIT_INLINE(int) git_diff_file__resolve_zero_size( + git_diff_file *file, git_odb_object **odb_obj, git_repository *repo) +{ + int error; + git_odb *odb; + size_t len; + git_object_t type; + + if ((error = git_repository_odb(&odb, repo)) < 0) + return error; + + error = git_odb__read_header_or_object( + odb_obj, &len, &type, odb, &file->id); + + git_odb_free(odb); + + if (!error) { + file->size = (git_object_size_t)len; + file->flags |= GIT_DIFF_FLAG_VALID_SIZE; + } + + return error; +} + +#endif diff --git a/src/libgit2/diff_parse.c b/src/libgit2/diff_parse.c new file mode 100644 index 00000000000..25dcd8e1100 --- /dev/null +++ b/src/libgit2/diff_parse.c @@ -0,0 +1,124 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "diff_parse.h" + +#include "diff.h" +#include "patch.h" +#include "patch_parse.h" + +static void diff_parsed_free(git_diff *d) +{ + git_diff_parsed *diff = (git_diff_parsed *)d; + git_patch *patch; + size_t i; + + git_vector_foreach(&diff->patches, i, patch) + git_patch_free(patch); + + git_vector_dispose(&diff->patches); + + git_vector_dispose(&diff->base.deltas); + git_pool_clear(&diff->base.pool); + + git__memzero(diff, sizeof(*diff)); + git__free(diff); +} + +static git_diff_parsed *diff_parsed_alloc(git_oid_t oid_type) +{ + git_diff_parsed *diff; + + if ((diff = git__calloc(1, sizeof(git_diff_parsed))) == NULL) + return NULL; + + GIT_REFCOUNT_INC(&diff->base); + diff->base.type = GIT_DIFF_TYPE_PARSED; + diff->base.strcomp = git__strcmp; + diff->base.strncomp = git__strncmp; + diff->base.pfxcomp = git__prefixcmp; + diff->base.entrycomp = git_diff__entry_cmp; + diff->base.patch_fn = git_patch_parsed_from_diff; + diff->base.free_fn = diff_parsed_free; + + if (git_diff_options_init(&diff->base.opts, GIT_DIFF_OPTIONS_VERSION) < 0) { + git__free(diff); + return NULL; + } + + diff->base.opts.flags &= ~GIT_DIFF_IGNORE_CASE; + diff->base.opts.oid_type = oid_type; + + if (git_pool_init(&diff->base.pool, 1) < 0 || + git_vector_init(&diff->patches, 0, NULL) < 0 || + git_vector_init(&diff->base.deltas, 0, git_diff_delta__cmp) < 0) { + git_diff_free(&diff->base); + return NULL; + } + + git_vector_set_cmp(&diff->base.deltas, git_diff_delta__cmp); + + return diff; +} + +int git_diff_from_buffer( + git_diff **out, + const char *content, + size_t content_len) +{ + return git_diff_from_buffer_ext(out, content, content_len, NULL); +} + +int git_diff_from_buffer_ext( + git_diff **out, + const char *content, + size_t content_len, + git_diff_parse_options *opts) +{ + git_diff_parsed *diff; + git_patch *patch; + git_patch_parse_ctx *ctx = NULL; + git_patch_options patch_opts = GIT_PATCH_OPTIONS_INIT; + git_oid_t oid_type; + int error = 0; + + *out = NULL; + + oid_type = (opts && opts->oid_type) ? opts->oid_type : + GIT_OID_DEFAULT; + + patch_opts.oid_type = oid_type; + + diff = diff_parsed_alloc(oid_type); + GIT_ERROR_CHECK_ALLOC(diff); + + ctx = git_patch_parse_ctx_init(content, content_len, &patch_opts); + GIT_ERROR_CHECK_ALLOC(ctx); + + while (ctx->parse_ctx.remain_len) { + if ((error = git_patch_parse(&patch, ctx)) < 0) + break; + + git_vector_insert(&diff->patches, patch); + git_vector_insert(&diff->base.deltas, patch->delta); + } + + if (error == GIT_ENOTFOUND && git_vector_length(&diff->patches) > 0) { + git_error_clear(); + error = 0; + } + + git_patch_parse_ctx_free(ctx); + + if (error < 0) + git_diff_free(&diff->base); + else + *out = &diff->base; + + return error; +} + diff --git a/src/libgit2/diff_parse.h b/src/libgit2/diff_parse.h new file mode 100644 index 00000000000..87678212845 --- /dev/null +++ b/src/libgit2/diff_parse.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_diff_parse_h__ +#define INCLUDE_diff_parse_h__ + +#include "common.h" + +#include "diff.h" + +typedef struct { + struct git_diff base; + + git_vector patches; +} git_diff_parsed; + +#endif diff --git a/src/libgit2/diff_print.c b/src/libgit2/diff_print.c new file mode 100644 index 00000000000..0ffba0d55d8 --- /dev/null +++ b/src/libgit2/diff_print.c @@ -0,0 +1,905 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "buf.h" +#include "diff.h" +#include "diff_file.h" +#include "patch_generate.h" +#include "futils.h" +#include "zstream.h" +#include "blob.h" +#include "delta.h" +#include "repository.h" +#include "git2/sys/diff.h" + +typedef struct { + git_diff_format_t format; + git_diff_line_cb print_cb; + void *payload; + + git_str *buf; + git_diff_line line; + + const char *old_prefix; + const char *new_prefix; + uint32_t flags; + int id_strlen; + unsigned int sent_file_header; + git_oid_t oid_type; + + int (*strcomp)(const char *, const char *); +} diff_print_info; + +static int diff_print_info_init__common( + diff_print_info *pi, + git_str *out, + git_repository *repo, + git_diff_format_t format, + git_diff_line_cb cb, + void *payload) +{ + pi->format = format; + pi->print_cb = cb; + pi->payload = payload; + pi->buf = out; + + GIT_ASSERT(pi->oid_type); + + if (!pi->id_strlen) { + if (!repo) + pi->id_strlen = GIT_ABBREV_DEFAULT; + else if (git_repository__abbrev_length(&pi->id_strlen, repo) < 0) + return -1; + } + + memset(&pi->line, 0, sizeof(pi->line)); + pi->line.old_lineno = -1; + pi->line.new_lineno = -1; + pi->line.num_lines = 1; + + return 0; +} + +static int diff_print_info_init_fromdiff( + diff_print_info *pi, + git_str *out, + git_diff *diff, + git_diff_format_t format, + git_diff_line_cb cb, + void *payload) +{ + git_repository *repo = diff ? diff->repo : NULL; + + memset(pi, 0, sizeof(diff_print_info)); + + if (diff) { + pi->flags = diff->opts.flags; + pi->oid_type = diff->opts.oid_type; + pi->id_strlen = diff->opts.id_abbrev; + pi->old_prefix = diff->opts.old_prefix; + pi->new_prefix = diff->opts.new_prefix; + + pi->strcomp = diff->strcomp; + } + + return diff_print_info_init__common(pi, out, repo, format, cb, payload); +} + +static int diff_print_info_init_frompatch( + diff_print_info *pi, + git_str *out, + git_patch *patch, + git_diff_format_t format, + git_diff_line_cb cb, + void *payload) +{ + GIT_ASSERT_ARG(patch); + + memset(pi, 0, sizeof(diff_print_info)); + + pi->flags = patch->diff_opts.flags; + pi->oid_type = patch->diff_opts.oid_type; + pi->id_strlen = patch->diff_opts.id_abbrev; + pi->old_prefix = patch->diff_opts.old_prefix; + pi->new_prefix = patch->diff_opts.new_prefix; + + return diff_print_info_init__common(pi, out, patch->repo, format, cb, payload); +} + +static char diff_pick_suffix(int mode) +{ + if (S_ISDIR(mode)) + return '/'; + else if (GIT_PERMS_IS_EXEC(mode)) /* -V536 */ + /* in git, modes are very regular, so we must have 0100755 mode */ + return '*'; + else + return ' '; +} + +char git_diff_status_char(git_delta_t status) +{ + char code; + + switch (status) { + case GIT_DELTA_ADDED: code = 'A'; break; + case GIT_DELTA_DELETED: code = 'D'; break; + case GIT_DELTA_MODIFIED: code = 'M'; break; + case GIT_DELTA_RENAMED: code = 'R'; break; + case GIT_DELTA_COPIED: code = 'C'; break; + case GIT_DELTA_IGNORED: code = 'I'; break; + case GIT_DELTA_UNTRACKED: code = '?'; break; + case GIT_DELTA_TYPECHANGE: code = 'T'; break; + case GIT_DELTA_UNREADABLE: code = 'X'; break; + default: code = ' '; break; + } + + return code; +} + +static int diff_print_one_name_only( + const git_diff_delta *delta, float progress, void *data) +{ + diff_print_info *pi = data; + git_str *out = pi->buf; + + GIT_UNUSED(progress); + + if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && + delta->status == GIT_DELTA_UNMODIFIED) + return 0; + + git_str_clear(out); + git_str_puts(out, delta->new_file.path); + git_str_putc(out, '\n'); + if (git_str_oom(out)) + return -1; + + pi->line.origin = GIT_DIFF_LINE_FILE_HDR; + pi->line.content = git_str_cstr(out); + pi->line.content_len = git_str_len(out); + + return pi->print_cb(delta, NULL, &pi->line, pi->payload); +} + +static int diff_print_one_name_status( + const git_diff_delta *delta, float progress, void *data) +{ + diff_print_info *pi = data; + git_str *out = pi->buf; + char old_suffix, new_suffix, code = git_diff_status_char(delta->status); + int(*strcomp)(const char *, const char *) = pi->strcomp ? + pi->strcomp : git__strcmp; + + GIT_UNUSED(progress); + + if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && code == ' ') + return 0; + + old_suffix = diff_pick_suffix(delta->old_file.mode); + new_suffix = diff_pick_suffix(delta->new_file.mode); + + git_str_clear(out); + + if (delta->old_file.path != delta->new_file.path && + strcomp(delta->old_file.path,delta->new_file.path) != 0) + git_str_printf(out, "%c\t%s%c %s%c\n", code, + delta->old_file.path, old_suffix, delta->new_file.path, new_suffix); + else if (delta->old_file.mode != delta->new_file.mode && + delta->old_file.mode != 0 && delta->new_file.mode != 0) + git_str_printf(out, "%c\t%s%c %s%c\n", code, + delta->old_file.path, old_suffix, delta->new_file.path, new_suffix); + else if (old_suffix != ' ') + git_str_printf(out, "%c\t%s%c\n", code, delta->old_file.path, old_suffix); + else + git_str_printf(out, "%c\t%s\n", code, delta->old_file.path); + if (git_str_oom(out)) + return -1; + + pi->line.origin = GIT_DIFF_LINE_FILE_HDR; + pi->line.content = git_str_cstr(out); + pi->line.content_len = git_str_len(out); + + return pi->print_cb(delta, NULL, &pi->line, pi->payload); +} + +static int diff_print_one_raw( + const git_diff_delta *delta, float progress, void *data) +{ + diff_print_info *pi = data; + git_str *out = pi->buf; + int id_abbrev; + char code = git_diff_status_char(delta->status); + char start_oid[GIT_OID_MAX_HEXSIZE + 1], + end_oid[GIT_OID_MAX_HEXSIZE + 1]; + size_t oid_hexsize; + bool id_is_abbrev; + + GIT_UNUSED(progress); + + if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && code == ' ') + return 0; + + git_str_clear(out); + + id_abbrev = delta->old_file.mode ? delta->old_file.id_abbrev : + delta->new_file.id_abbrev; + + if (pi->id_strlen > id_abbrev) { + git_error_set(GIT_ERROR_PATCH, + "the patch input contains %d id characters (cannot print %d)", + id_abbrev, pi->id_strlen); + return -1; + } + +#ifdef GIT_EXPERIMENTAL_SHA256 + GIT_ASSERT(delta->old_file.id.type == delta->new_file.id.type); + oid_hexsize = git_oid_hexsize(delta->old_file.id.type); +#else + oid_hexsize = GIT_OID_SHA1_HEXSIZE; +#endif + + id_is_abbrev = (pi->id_strlen > 0 && + (size_t)pi->id_strlen <= oid_hexsize); + + git_oid_tostr(start_oid, pi->id_strlen + 1, &delta->old_file.id); + git_oid_tostr(end_oid, pi->id_strlen + 1, &delta->new_file.id); + + git_str_printf(out, + id_is_abbrev ? ":%06o %06o %s... %s... %c" : ":%06o %06o %s %s %c", + delta->old_file.mode, delta->new_file.mode, start_oid, end_oid, code); + + if (delta->similarity > 0) + git_str_printf(out, "%03u", delta->similarity); + + if (delta->old_file.path != delta->new_file.path) + git_str_printf( + out, "\t%s %s\n", delta->old_file.path, delta->new_file.path); + else + git_str_printf( + out, "\t%s\n", delta->old_file.path ? + delta->old_file.path : delta->new_file.path); + + if (git_str_oom(out)) + return -1; + + pi->line.origin = GIT_DIFF_LINE_FILE_HDR; + pi->line.content = git_str_cstr(out); + pi->line.content_len = git_str_len(out); + + return pi->print_cb(delta, NULL, &pi->line, pi->payload); +} + +static int diff_print_modes( + git_str *out, const git_diff_delta *delta) +{ + git_str_printf(out, "old mode %o\n", delta->old_file.mode); + git_str_printf(out, "new mode %o\n", delta->new_file.mode); + + return git_str_oom(out) ? -1 : 0; +} + +static int diff_print_oid_range( + git_str *out, const git_diff_delta *delta, int id_strlen, + bool print_index) +{ + char start_oid[GIT_OID_MAX_HEXSIZE + 1], + end_oid[GIT_OID_MAX_HEXSIZE + 1]; + + if (delta->old_file.mode && + id_strlen > delta->old_file.id_abbrev) { + git_error_set(GIT_ERROR_PATCH, + "the patch input contains %d id characters (cannot print %d)", + delta->old_file.id_abbrev, id_strlen); + return -1; + } + + if ((delta->new_file.mode && + id_strlen > delta->new_file.id_abbrev)) { + git_error_set(GIT_ERROR_PATCH, + "the patch input contains %d id characters (cannot print %d)", + delta->new_file.id_abbrev, id_strlen); + return -1; + } + + git_oid_tostr(start_oid, id_strlen + 1, &delta->old_file.id); + git_oid_tostr(end_oid, id_strlen + 1, &delta->new_file.id); + + if (delta->old_file.mode == delta->new_file.mode) { + if (print_index) + git_str_printf(out, "index %s..%s %o\n", + start_oid, end_oid, delta->old_file.mode); + } else { + if (delta->old_file.mode == 0) + git_str_printf(out, "new file mode %o\n", delta->new_file.mode); + else if (delta->new_file.mode == 0) + git_str_printf(out, "deleted file mode %o\n", delta->old_file.mode); + else + diff_print_modes(out, delta); + + if (print_index) + git_str_printf(out, "index %s..%s\n", start_oid, end_oid); + } + + return git_str_oom(out) ? -1 : 0; +} + +static int diff_delta_format_path( + git_str *out, const char *prefix, const char *filename) +{ + if (!filename) { + /* don't prefix "/dev/null" */ + return git_str_puts(out, "/dev/null"); + } + + if (git_str_joinpath(out, prefix, filename) < 0) + return -1; + + return git_str_quote(out); +} + +static int diff_delta_format_with_paths( + git_str *out, + const git_diff_delta *delta, + const char *template, + const char *oldpath, + const char *newpath) +{ + if (git_oid_is_zero(&delta->old_file.id)) + oldpath = "/dev/null"; + + if (git_oid_is_zero(&delta->new_file.id)) + newpath = "/dev/null"; + + return git_str_printf(out, template, oldpath, newpath); +} + +static int diff_delta_format_similarity_header( + git_str *out, + const git_diff_delta *delta) +{ + git_str old_path = GIT_STR_INIT, new_path = GIT_STR_INIT; + const char *type; + int error = 0; + + if (delta->similarity > 100) { + git_error_set(GIT_ERROR_PATCH, "invalid similarity %d", delta->similarity); + error = -1; + goto done; + } + + GIT_ASSERT(delta->status == GIT_DELTA_RENAMED || delta->status == GIT_DELTA_COPIED); + if (delta->status == GIT_DELTA_RENAMED) + type = "rename"; + else + type = "copy"; + + if ((error = git_str_puts(&old_path, delta->old_file.path)) < 0 || + (error = git_str_puts(&new_path, delta->new_file.path)) < 0 || + (error = git_str_quote(&old_path)) < 0 || + (error = git_str_quote(&new_path)) < 0) + goto done; + + git_str_printf(out, + "similarity index %d%%\n" + "%s from %s\n" + "%s to %s\n", + delta->similarity, + type, old_path.ptr, + type, new_path.ptr); + + if (git_str_oom(out)) + error = -1; + +done: + git_str_dispose(&old_path); + git_str_dispose(&new_path); + + return error; +} + +static bool delta_is_unchanged(const git_diff_delta *delta) +{ + if (git_oid_is_zero(&delta->old_file.id) && + git_oid_is_zero(&delta->new_file.id)) + return true; + + if (delta->old_file.mode == GIT_FILEMODE_COMMIT || + delta->new_file.mode == GIT_FILEMODE_COMMIT) + return false; + + if (git_oid_equal(&delta->old_file.id, &delta->new_file.id)) + return true; + + return false; +} + +int git_diff_delta__format_file_header( + git_str *out, + const git_diff_delta *delta, + const char *oldpfx, + const char *newpfx, + int id_strlen, + bool print_index) +{ + git_str old_path = GIT_STR_INIT, new_path = GIT_STR_INIT; + bool unchanged = delta_is_unchanged(delta); + int error = 0; + + if (!oldpfx) + oldpfx = DIFF_OLD_PREFIX_DEFAULT; + if (!newpfx) + newpfx = DIFF_NEW_PREFIX_DEFAULT; + if (!id_strlen) + id_strlen = GIT_ABBREV_DEFAULT; + + if ((error = diff_delta_format_path( + &old_path, oldpfx, delta->old_file.path)) < 0 || + (error = diff_delta_format_path( + &new_path, newpfx, delta->new_file.path)) < 0) + goto done; + + git_str_clear(out); + + git_str_printf(out, "diff --git %s %s\n", + old_path.ptr, new_path.ptr); + + if (unchanged && delta->old_file.mode != delta->new_file.mode) + diff_print_modes(out, delta); + + if (delta->status == GIT_DELTA_RENAMED || + (delta->status == GIT_DELTA_COPIED && unchanged)) { + if ((error = diff_delta_format_similarity_header(out, delta)) < 0) + goto done; + } + + if (!unchanged) { + if ((error = diff_print_oid_range(out, delta, + id_strlen, print_index)) < 0) + goto done; + + if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0) + diff_delta_format_with_paths(out, delta, + "--- %s\n+++ %s\n", old_path.ptr, new_path.ptr); + } + + if (git_str_oom(out)) + error = -1; + +done: + git_str_dispose(&old_path); + git_str_dispose(&new_path); + + return error; +} + +static int format_binary( + diff_print_info *pi, + git_diff_binary_t type, + const char *data, + size_t datalen, + size_t inflatedlen) +{ + const char *typename = type == GIT_DIFF_BINARY_DELTA ? + "delta" : "literal"; + const char *scan, *end; + + git_str_printf(pi->buf, "%s %" PRIuZ "\n", typename, inflatedlen); + pi->line.num_lines++; + + for (scan = data, end = data + datalen; scan < end; ) { + size_t chunk_len = end - scan; + if (chunk_len > 52) + chunk_len = 52; + + if (chunk_len <= 26) + git_str_putc(pi->buf, (char)chunk_len + 'A' - 1); + else + git_str_putc(pi->buf, (char)chunk_len - 26 + 'a' - 1); + + git_str_encode_base85(pi->buf, scan, chunk_len); + git_str_putc(pi->buf, '\n'); + + if (git_str_oom(pi->buf)) + return -1; + + scan += chunk_len; + pi->line.num_lines++; + } + git_str_putc(pi->buf, '\n'); + + if (git_str_oom(pi->buf)) + return -1; + + return 0; +} + +static int diff_print_patch_file_binary_noshow( + diff_print_info *pi, git_diff_delta *delta, + const char *old_pfx, const char *new_pfx) +{ + git_str old_path = GIT_STR_INIT, new_path = GIT_STR_INIT; + int error; + + if ((error = diff_delta_format_path(&old_path, old_pfx, delta->old_file.path)) < 0 || + (error = diff_delta_format_path(&new_path, new_pfx, delta->new_file.path)) < 0 || + (error = diff_delta_format_with_paths(pi->buf, delta, "Binary files %s and %s differ\n", + old_path.ptr, new_path.ptr)) < 0) + goto done; + + pi->line.num_lines = 1; + +done: + git_str_dispose(&old_path); + git_str_dispose(&new_path); + return error; +} + +static int diff_print_patch_file_binary( + diff_print_info *pi, git_diff_delta *delta, + const char *old_pfx, const char *new_pfx, + const git_diff_binary *binary) +{ + size_t pre_binary_size; + int error; + + if (delta->status == GIT_DELTA_UNMODIFIED) + return 0; + + if ((pi->flags & GIT_DIFF_SHOW_BINARY) == 0 || !binary->contains_data) + return diff_print_patch_file_binary_noshow( + pi, delta, old_pfx, new_pfx); + + pre_binary_size = pi->buf->size; + git_str_printf(pi->buf, "GIT binary patch\n"); + pi->line.num_lines++; + + if ((error = format_binary(pi, binary->new_file.type, binary->new_file.data, + binary->new_file.datalen, binary->new_file.inflatedlen)) < 0 || + (error = format_binary(pi, binary->old_file.type, binary->old_file.data, + binary->old_file.datalen, binary->old_file.inflatedlen)) < 0) { + if (error == GIT_EBUFS) { + git_error_clear(); + git_str_truncate(pi->buf, pre_binary_size); + + return diff_print_patch_file_binary_noshow( + pi, delta, old_pfx, new_pfx); + } + } + + pi->line.num_lines++; + return error; +} + +GIT_INLINE(int) should_force_header(const git_diff_delta *delta) +{ + if (delta->old_file.mode != delta->new_file.mode) + return 1; + + if (delta->status == GIT_DELTA_RENAMED || delta->status == GIT_DELTA_COPIED) + return 1; + + return 0; +} + +GIT_INLINE(int) flush_file_header(const git_diff_delta *delta, diff_print_info *pi) +{ + if (pi->sent_file_header) + return 0; + + pi->line.origin = GIT_DIFF_LINE_FILE_HDR; + pi->line.content = git_str_cstr(pi->buf); + pi->line.content_len = git_str_len(pi->buf); + pi->sent_file_header = 1; + + return pi->print_cb(delta, NULL, &pi->line, pi->payload); +} + +static int diff_print_patch_file( + const git_diff_delta *delta, float progress, void *data) +{ + int error; + diff_print_info *pi = data; + const char *oldpfx = + pi->old_prefix ? pi->old_prefix : DIFF_OLD_PREFIX_DEFAULT; + const char *newpfx = + pi->new_prefix ? pi->new_prefix : DIFF_NEW_PREFIX_DEFAULT; + + bool binary = (delta->flags & GIT_DIFF_FLAG_BINARY) || + (pi->flags & GIT_DIFF_FORCE_BINARY); + bool show_binary = !!(pi->flags & GIT_DIFF_SHOW_BINARY); + int id_strlen = pi->id_strlen; + bool print_index = (pi->format != GIT_DIFF_FORMAT_PATCH_ID); + + if (binary && show_binary) + id_strlen = delta->old_file.id_abbrev ? delta->old_file.id_abbrev : + delta->new_file.id_abbrev; + + GIT_UNUSED(progress); + + if (S_ISDIR(delta->new_file.mode) || + delta->status == GIT_DELTA_UNMODIFIED || + delta->status == GIT_DELTA_IGNORED || + delta->status == GIT_DELTA_UNREADABLE || + (delta->status == GIT_DELTA_UNTRACKED && + (pi->flags & GIT_DIFF_SHOW_UNTRACKED_CONTENT) == 0)) + return 0; + + pi->sent_file_header = 0; + + if ((error = git_diff_delta__format_file_header(pi->buf, delta, oldpfx, newpfx, + id_strlen, print_index)) < 0) + return error; + + /* + * pi->buf now contains the file header data. Go ahead and send it + * if there's useful data in there, like similarity. Otherwise, we + * should queue it to send when we see the first hunk. This prevents + * us from sending a header when all hunks were ignored. + */ + if (should_force_header(delta) && (error = flush_file_header(delta, pi)) < 0) + return error; + + return 0; +} + +static int diff_print_patch_binary( + const git_diff_delta *delta, + const git_diff_binary *binary, + void *data) +{ + diff_print_info *pi = data; + const char *old_pfx = + pi->old_prefix ? pi->old_prefix : DIFF_OLD_PREFIX_DEFAULT; + const char *new_pfx = + pi->new_prefix ? pi->new_prefix : DIFF_NEW_PREFIX_DEFAULT; + int error; + + if ((error = flush_file_header(delta, pi)) < 0) + return error; + + /* + * If the caller only wants the header, we just needed to make sure to + * call flush_file_header + */ + if (pi->format == GIT_DIFF_FORMAT_PATCH_HEADER) + return 0; + + git_str_clear(pi->buf); + + if ((error = diff_print_patch_file_binary( + pi, (git_diff_delta *)delta, old_pfx, new_pfx, binary)) < 0) + return error; + + pi->line.origin = GIT_DIFF_LINE_BINARY; + pi->line.content = git_str_cstr(pi->buf); + pi->line.content_len = git_str_len(pi->buf); + + return pi->print_cb(delta, NULL, &pi->line, pi->payload); +} + +static int diff_print_patch_hunk( + const git_diff_delta *d, + const git_diff_hunk *h, + void *data) +{ + diff_print_info *pi = data; + int error; + + if (S_ISDIR(d->new_file.mode)) + return 0; + + if ((error = flush_file_header(d, pi)) < 0) + return error; + + /* + * If the caller only wants the header, we just needed to make sure to + * call flush_file_header + */ + if (pi->format == GIT_DIFF_FORMAT_PATCH_HEADER) + return 0; + + pi->line.origin = GIT_DIFF_LINE_HUNK_HDR; + pi->line.content = h->header; + pi->line.content_len = h->header_len; + + return pi->print_cb(d, h, &pi->line, pi->payload); +} + +static int diff_print_patch_line( + const git_diff_delta *delta, + const git_diff_hunk *hunk, + const git_diff_line *line, + void *data) +{ + diff_print_info *pi = data; + int error; + + if (S_ISDIR(delta->new_file.mode)) + return 0; + + if ((error = flush_file_header(delta, pi)) < 0) + return error; + + return pi->print_cb(delta, hunk, line, pi->payload); +} + +/* print a git_diff to an output callback */ +int git_diff_print( + git_diff *diff, + git_diff_format_t format, + git_diff_line_cb print_cb, + void *payload) +{ + int error; + git_str buf = GIT_STR_INIT; + diff_print_info pi; + git_diff_file_cb print_file = NULL; + git_diff_binary_cb print_binary = NULL; + git_diff_hunk_cb print_hunk = NULL; + git_diff_line_cb print_line = NULL; + + switch (format) { + case GIT_DIFF_FORMAT_PATCH: + print_file = diff_print_patch_file; + print_binary = diff_print_patch_binary; + print_hunk = diff_print_patch_hunk; + print_line = diff_print_patch_line; + break; + case GIT_DIFF_FORMAT_PATCH_ID: + print_file = diff_print_patch_file; + print_binary = diff_print_patch_binary; + print_line = diff_print_patch_line; + break; + case GIT_DIFF_FORMAT_PATCH_HEADER: + print_file = diff_print_patch_file; + print_binary = diff_print_patch_binary; + print_hunk = diff_print_patch_hunk; + break; + case GIT_DIFF_FORMAT_RAW: + print_file = diff_print_one_raw; + break; + case GIT_DIFF_FORMAT_NAME_ONLY: + print_file = diff_print_one_name_only; + break; + case GIT_DIFF_FORMAT_NAME_STATUS: + print_file = diff_print_one_name_status; + break; + default: + git_error_set(GIT_ERROR_INVALID, "unknown diff output format (%d)", format); + return -1; + } + + if ((error = diff_print_info_init_fromdiff(&pi, &buf, diff, format, print_cb, payload)) < 0) + goto out; + + if ((error = git_diff_foreach(diff, print_file, print_binary, print_hunk, print_line, &pi)) != 0) { + git_error_set_after_callback_function(error, "git_diff_print"); + goto out; + } + +out: + git_str_dispose(&buf); + return error; +} + +int git_diff_print_callback__to_buf( + const git_diff_delta *delta, + const git_diff_hunk *hunk, + const git_diff_line *line, + void *payload) +{ + git_str *output = payload; + GIT_UNUSED(delta); GIT_UNUSED(hunk); + + if (!output) { + git_error_set(GIT_ERROR_INVALID, "buffer pointer must be provided"); + return -1; + } + + if (line->origin == GIT_DIFF_LINE_ADDITION || + line->origin == GIT_DIFF_LINE_DELETION || + line->origin == GIT_DIFF_LINE_CONTEXT) + git_str_putc(output, line->origin); + + return git_str_put(output, line->content, line->content_len); +} + +int git_diff_print_callback__to_file_handle( + const git_diff_delta *delta, + const git_diff_hunk *hunk, + const git_diff_line *line, + void *payload) +{ + FILE *fp = payload ? payload : stdout; + int error; + + GIT_UNUSED(delta); + GIT_UNUSED(hunk); + + if (line->origin == GIT_DIFF_LINE_CONTEXT || + line->origin == GIT_DIFF_LINE_ADDITION || + line->origin == GIT_DIFF_LINE_DELETION) { + while ((error = fputc(line->origin, fp)) == EINTR) + continue; + if (error) { + git_error_set(GIT_ERROR_OS, "could not write status"); + return -1; + } + } + + if (fwrite(line->content, line->content_len, 1, fp) != 1) { + git_error_set(GIT_ERROR_OS, "could not write line"); + return -1; + } + + return 0; +} + +/* print a git_diff to a git_str */ +int git_diff_to_buf(git_buf *out, git_diff *diff, git_diff_format_t format) +{ + git_str str = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(diff); + + if ((error = git_buf_tostr(&str, out)) < 0 || + (error = git_diff_print(diff, format, git_diff_print_callback__to_buf, &str)) < 0) + goto done; + + error = git_buf_fromstr(out, &str); + +done: + git_str_dispose(&str); + return error; +} + +/* print a git_patch to an output callback */ +int git_patch_print( + git_patch *patch, + git_diff_line_cb print_cb, + void *payload) +{ + git_str temp = GIT_STR_INIT; + diff_print_info pi; + int error; + + GIT_ASSERT_ARG(patch); + GIT_ASSERT_ARG(print_cb); + + if ((error = diff_print_info_init_frompatch(&pi, &temp, patch, + GIT_DIFF_FORMAT_PATCH, print_cb, payload)) < 0) + goto out; + + if ((error = git_patch__invoke_callbacks(patch, diff_print_patch_file, diff_print_patch_binary, + diff_print_patch_hunk, diff_print_patch_line, &pi)) < 0) { + git_error_set_after_callback_function(error, "git_patch_print"); + goto out; + } + +out: + git_str_dispose(&temp); + return error; +} + +/* print a git_patch to a git_str */ +int git_patch_to_buf(git_buf *out, git_patch *patch) +{ + GIT_BUF_WRAP_PRIVATE(out, git_patch__to_buf, patch); +} + +int git_patch__to_buf(git_str *out, git_patch *patch) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(patch); + + return git_patch_print(patch, git_diff_print_callback__to_buf, out); +} diff --git a/src/libgit2/diff_stats.c b/src/libgit2/diff_stats.c new file mode 100644 index 00000000000..b67aa48dd6b --- /dev/null +++ b/src/libgit2/diff_stats.c @@ -0,0 +1,383 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "diff_stats.h" + +#include "buf.h" +#include "common.h" +#include "vector.h" +#include "diff.h" +#include "patch_generate.h" + +#define DIFF_RENAME_FILE_SEPARATOR " => " +#define STATS_FULL_MIN_SCALE 7 + +typedef struct { + size_t insertions; + size_t deletions; +} diff_file_stats; + +struct git_diff_stats { + git_diff *diff; + diff_file_stats *filestats; + + size_t files_changed; + size_t insertions; + size_t deletions; + + size_t max_name; + size_t max_filestat; + int max_digits; +}; + +static int digits_for_value(size_t val) +{ + int count = 1; + size_t placevalue = 10; + + while (val >= placevalue) { + ++count; + placevalue *= 10; + } + + return count; +} + +static int diff_file_stats_full_to_buf( + git_str *out, + const git_diff_delta *delta, + const diff_file_stats *filestat, + const git_diff_stats *stats, + size_t width) +{ + const char *old_path = NULL, *new_path = NULL, *adddel_path = NULL; + size_t padding; + git_object_size_t old_size, new_size; + + old_path = delta->old_file.path; + new_path = delta->new_file.path; + old_size = delta->old_file.size; + new_size = delta->new_file.size; + + if (old_path && new_path && strcmp(old_path, new_path) != 0) { + size_t common_dirlen; + int error; + + if ((common_dirlen = git_fs_path_common_dirlen(old_path, new_path)) && + common_dirlen <= INT_MAX) { + error = git_str_printf(out, " %.*s{%s"DIFF_RENAME_FILE_SEPARATOR"%s}", + (int) common_dirlen, old_path, + old_path + common_dirlen, + new_path + common_dirlen); + padding = stats->max_name + common_dirlen - strlen(old_path) + - strlen(new_path) - 2 - strlen(DIFF_RENAME_FILE_SEPARATOR); + } else { + error = git_str_printf(out, " %s" DIFF_RENAME_FILE_SEPARATOR "%s", + old_path, new_path); + padding = stats->max_name - strlen(old_path) + - strlen(new_path) - strlen(DIFF_RENAME_FILE_SEPARATOR); + } + + if (error < 0) + goto on_error; + } else { + adddel_path = new_path ? new_path : old_path; + if (git_str_printf(out, " %s", adddel_path) < 0) + goto on_error; + + padding = stats->max_name - strlen(adddel_path); + } + + if (git_str_putcn(out, ' ', padding) < 0 || + git_str_puts(out, " | ") < 0) + goto on_error; + + if (delta->flags & GIT_DIFF_FLAG_BINARY) { + if (git_str_printf(out, + "Bin %" PRId64 " -> %" PRId64 " bytes", old_size, new_size) < 0) + goto on_error; + } + else { + if (git_str_printf(out, + "%*" PRIuZ, stats->max_digits, + filestat->insertions + filestat->deletions) < 0) + goto on_error; + + if (filestat->insertions || filestat->deletions) { + if (git_str_putc(out, ' ') < 0) + goto on_error; + + if (!width) { + if (git_str_putcn(out, '+', filestat->insertions) < 0 || + git_str_putcn(out, '-', filestat->deletions) < 0) + goto on_error; + } else { + size_t total = filestat->insertions + filestat->deletions; + size_t full = (total * width + stats->max_filestat / 2) / + stats->max_filestat; + size_t plus = full * filestat->insertions / total; + size_t minus = full - plus; + + if (git_str_putcn(out, '+', max(plus, 1)) < 0 || + git_str_putcn(out, '-', max(minus, 1)) < 0) + goto on_error; + } + } + } + + git_str_putc(out, '\n'); + +on_error: + return (git_str_oom(out) ? -1 : 0); +} + +static int diff_file_stats_number_to_buf( + git_str *out, + const git_diff_delta *delta, + const diff_file_stats *filestats) +{ + int error; + const char *path = delta->new_file.path; + + if (delta->flags & GIT_DIFF_FLAG_BINARY) + error = git_str_printf(out, "%-8c" "%-8c" "%s\n", '-', '-', path); + else + error = git_str_printf(out, "%-8" PRIuZ "%-8" PRIuZ "%s\n", + filestats->insertions, filestats->deletions, path); + + return error; +} + +static int diff_file_stats_summary_to_buf( + git_str *out, + const git_diff_delta *delta) +{ + if (delta->old_file.mode != delta->new_file.mode) { + if (delta->old_file.mode == 0) { + git_str_printf(out, " create mode %06o %s\n", + delta->new_file.mode, delta->new_file.path); + } + else if (delta->new_file.mode == 0) { + git_str_printf(out, " delete mode %06o %s\n", + delta->old_file.mode, delta->old_file.path); + } + else { + git_str_printf(out, " mode change %06o => %06o %s\n", + delta->old_file.mode, delta->new_file.mode, delta->new_file.path); + } + } + + return 0; +} + +int git_diff_get_stats( + git_diff_stats **out, + git_diff *diff) +{ + size_t i, deltas; + size_t total_insertions = 0, total_deletions = 0; + git_diff_stats *stats = NULL; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(diff); + + stats = git__calloc(1, sizeof(git_diff_stats)); + GIT_ERROR_CHECK_ALLOC(stats); + + deltas = git_diff_num_deltas(diff); + + stats->filestats = git__calloc(deltas, sizeof(diff_file_stats)); + if (!stats->filestats) { + git__free(stats); + return -1; + } + + stats->diff = diff; + GIT_REFCOUNT_INC(diff); + + for (i = 0; i < deltas && !error; ++i) { + git_patch *patch = NULL; + size_t add = 0, remove = 0, namelen; + const git_diff_delta *delta; + + if ((error = git_patch_from_diff(&patch, diff, i)) < 0) + break; + + /* Length calculation for renames mirrors the actual presentation format + * generated in diff_file_stats_full_to_buf; namelen is the full length of + * what will be printed, taking into account renames and common prefixes. + */ + delta = patch->delta; + namelen = strlen(delta->new_file.path); + if (delta->old_file.path && + strcmp(delta->old_file.path, delta->new_file.path) != 0) { + size_t common_dirlen; + if ((common_dirlen = git_fs_path_common_dirlen(delta->old_file.path, delta->new_file.path)) && + common_dirlen <= INT_MAX) { + namelen += strlen(delta->old_file.path) + 2 + + strlen(DIFF_RENAME_FILE_SEPARATOR) - common_dirlen; + } else { + namelen += strlen(delta->old_file.path) + + strlen(DIFF_RENAME_FILE_SEPARATOR); + } + } + + /* and, of course, count the line stats */ + error = git_patch_line_stats(NULL, &add, &remove, patch); + + git_patch_free(patch); + + stats->filestats[i].insertions = add; + stats->filestats[i].deletions = remove; + + total_insertions += add; + total_deletions += remove; + + if (stats->max_name < namelen) + stats->max_name = namelen; + if (stats->max_filestat < add + remove) + stats->max_filestat = add + remove; + } + + stats->files_changed = deltas; + stats->insertions = total_insertions; + stats->deletions = total_deletions; + stats->max_digits = digits_for_value(stats->max_filestat + 1); + + if (error < 0) { + git_diff_stats_free(stats); + stats = NULL; + } + + *out = stats; + return error; +} + +size_t git_diff_stats_files_changed( + const git_diff_stats *stats) +{ + GIT_ASSERT_ARG(stats); + + return stats->files_changed; +} + +size_t git_diff_stats_insertions( + const git_diff_stats *stats) +{ + GIT_ASSERT_ARG(stats); + + return stats->insertions; +} + +size_t git_diff_stats_deletions( + const git_diff_stats *stats) +{ + GIT_ASSERT_ARG(stats); + + return stats->deletions; +} + +int git_diff_stats_to_buf( + git_buf *out, + const git_diff_stats *stats, + git_diff_stats_format_t format, + size_t width) +{ + GIT_BUF_WRAP_PRIVATE(out, git_diff__stats_to_buf, stats, format, width); +} + +int git_diff__stats_to_buf( + git_str *out, + const git_diff_stats *stats, + git_diff_stats_format_t format, + size_t width) +{ + int error = 0; + size_t i; + const git_diff_delta *delta; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(stats); + + if (format & GIT_DIFF_STATS_NUMBER) { + for (i = 0; i < stats->files_changed; ++i) { + if ((delta = git_diff_get_delta(stats->diff, i)) == NULL) + continue; + + error = diff_file_stats_number_to_buf( + out, delta, &stats->filestats[i]); + if (error < 0) + return error; + } + } + + if (format & GIT_DIFF_STATS_FULL) { + if (width > 0) { + if (width > stats->max_name + stats->max_digits + 5) + width -= (stats->max_name + stats->max_digits + 5); + if (width < STATS_FULL_MIN_SCALE) + width = STATS_FULL_MIN_SCALE; + } + if (width > stats->max_filestat) + width = 0; + + for (i = 0; i < stats->files_changed; ++i) { + if ((delta = git_diff_get_delta(stats->diff, i)) == NULL) + continue; + + error = diff_file_stats_full_to_buf( + out, delta, &stats->filestats[i], stats, width); + if (error < 0) + return error; + } + } + + if (format & GIT_DIFF_STATS_FULL || format & GIT_DIFF_STATS_SHORT) { + git_str_printf( + out, " %" PRIuZ " file%s changed", + stats->files_changed, stats->files_changed != 1 ? "s" : ""); + + if (stats->insertions || stats->deletions == 0) + git_str_printf( + out, ", %" PRIuZ " insertion%s(+)", + stats->insertions, stats->insertions != 1 ? "s" : ""); + + if (stats->deletions || stats->insertions == 0) + git_str_printf( + out, ", %" PRIuZ " deletion%s(-)", + stats->deletions, stats->deletions != 1 ? "s" : ""); + + git_str_putc(out, '\n'); + + if (git_str_oom(out)) + return -1; + } + + if (format & GIT_DIFF_STATS_INCLUDE_SUMMARY) { + for (i = 0; i < stats->files_changed; ++i) { + if ((delta = git_diff_get_delta(stats->diff, i)) == NULL) + continue; + + error = diff_file_stats_summary_to_buf(out, delta); + if (error < 0) + return error; + } + } + + return error; +} + +void git_diff_stats_free(git_diff_stats *stats) +{ + if (stats == NULL) + return; + + git_diff_free(stats->diff); /* bumped refcount in constructor */ + git__free(stats->filestats); + git__free(stats); +} diff --git a/src/libgit2/diff_stats.h b/src/libgit2/diff_stats.h new file mode 100644 index 00000000000..c71862b4e6d --- /dev/null +++ b/src/libgit2/diff_stats.h @@ -0,0 +1,18 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_diff_stats_h__ +#define INCLUDE_diff_stats_h__ + +#include "common.h" + +int git_diff__stats_to_buf( + git_str *out, + const git_diff_stats *stats, + git_diff_stats_format_t format, + size_t width); + +#endif diff --git a/src/libgit2/diff_tform.c b/src/libgit2/diff_tform.c new file mode 100644 index 00000000000..33ed2d11cda --- /dev/null +++ b/src/libgit2/diff_tform.c @@ -0,0 +1,1153 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "diff_tform.h" + +#include "git2/config.h" +#include "git2/blob.h" +#include "git2/sys/hashsig.h" + +#include "diff.h" +#include "diff_generate.h" +#include "fs_path.h" +#include "futils.h" +#include "config.h" + +git_diff_delta *git_diff__delta_dup( + const git_diff_delta *d, git_pool *pool) +{ + git_diff_delta *delta = git__malloc(sizeof(git_diff_delta)); + if (!delta) + return NULL; + + memcpy(delta, d, sizeof(git_diff_delta)); + GIT_DIFF_FLAG__CLEAR_INTERNAL(delta->flags); + + if (d->old_file.path != NULL) { + delta->old_file.path = git_pool_strdup(pool, d->old_file.path); + if (delta->old_file.path == NULL) + goto fail; + } + + if (d->new_file.path != d->old_file.path && d->new_file.path != NULL) { + delta->new_file.path = git_pool_strdup(pool, d->new_file.path); + if (delta->new_file.path == NULL) + goto fail; + } else { + delta->new_file.path = delta->old_file.path; + } + + return delta; + +fail: + git__free(delta); + return NULL; +} + +git_diff_delta *git_diff__merge_like_cgit( + const git_diff_delta *a, + const git_diff_delta *b, + git_pool *pool) +{ + git_diff_delta *dup; + + /* Emulate C git for merging two diffs (a la 'git diff '). + * + * When C git does a diff between the work dir and a tree, it actually + * diffs with the index but uses the workdir contents. This emulates + * those choices so we can emulate the type of diff. + * + * We have three file descriptions here, let's call them: + * f1 = a->old_file + * f2 = a->new_file AND b->old_file + * f3 = b->new_file + */ + + /* If one of the diffs is a conflict, just dup it */ + if (b->status == GIT_DELTA_CONFLICTED) + return git_diff__delta_dup(b, pool); + if (a->status == GIT_DELTA_CONFLICTED) + return git_diff__delta_dup(a, pool); + + /* if f2 == f3 or f2 is deleted, then just dup the 'a' diff */ + if (b->status == GIT_DELTA_UNMODIFIED || a->status == GIT_DELTA_DELETED) + return git_diff__delta_dup(a, pool); + + /* otherwise, base this diff on the 'b' diff */ + if ((dup = git_diff__delta_dup(b, pool)) == NULL) + return NULL; + + /* If 'a' status is uninteresting, then we're done */ + if (a->status == GIT_DELTA_UNMODIFIED || + a->status == GIT_DELTA_UNTRACKED || + a->status == GIT_DELTA_UNREADABLE) + return dup; + + GIT_ASSERT_WITH_RETVAL(b->status != GIT_DELTA_UNMODIFIED, NULL); + + /* A cgit exception is that the diff of a file that is only in the + * index (i.e. not in HEAD nor workdir) is given as empty. + */ + if (dup->status == GIT_DELTA_DELETED) { + if (a->status == GIT_DELTA_ADDED) { + dup->status = GIT_DELTA_UNMODIFIED; + dup->nfiles = 2; + } + /* else don't overwrite DELETE status */ + } else { + dup->status = a->status; + dup->nfiles = a->nfiles; + } + + git_oid_cpy(&dup->old_file.id, &a->old_file.id); + dup->old_file.mode = a->old_file.mode; + dup->old_file.size = a->old_file.size; + dup->old_file.flags = a->old_file.flags; + + return dup; +} + +int git_diff__merge( + git_diff *onto, const git_diff *from, git_diff__merge_cb cb) +{ + int error = 0; + git_pool onto_pool; + git_vector onto_new; + git_diff_delta *delta; + bool ignore_case, reversed; + unsigned int i, j; + + GIT_ASSERT_ARG(onto); + GIT_ASSERT_ARG(from); + + if (!from->deltas.length) + return 0; + + ignore_case = ((onto->opts.flags & GIT_DIFF_IGNORE_CASE) != 0); + reversed = ((onto->opts.flags & GIT_DIFF_REVERSE) != 0); + + if (ignore_case != ((from->opts.flags & GIT_DIFF_IGNORE_CASE) != 0) || + reversed != ((from->opts.flags & GIT_DIFF_REVERSE) != 0)) { + git_error_set(GIT_ERROR_INVALID, + "attempt to merge diffs created with conflicting options"); + return -1; + } + + if (git_vector_init(&onto_new, onto->deltas.length, git_diff_delta__cmp) < 0 || + git_pool_init(&onto_pool, 1) < 0) + return -1; + + for (i = 0, j = 0; i < onto->deltas.length || j < from->deltas.length; ) { + git_diff_delta *o = GIT_VECTOR_GET(&onto->deltas, i); + const git_diff_delta *f = GIT_VECTOR_GET(&from->deltas, j); + int cmp = !f ? -1 : !o ? 1 : + STRCMP_CASESELECT(ignore_case, o->old_file.path, f->old_file.path); + + if (cmp < 0) { + delta = git_diff__delta_dup(o, &onto_pool); + i++; + } else if (cmp > 0) { + delta = git_diff__delta_dup(f, &onto_pool); + j++; + } else { + const git_diff_delta *left = reversed ? f : o; + const git_diff_delta *right = reversed ? o : f; + + delta = cb(left, right, &onto_pool); + i++; + j++; + } + + /* the ignore rules for the target may not match the source + * or the result of a merged delta could be skippable... + */ + if (delta && git_diff_delta__should_skip(&onto->opts, delta)) { + git__free(delta); + continue; + } + + if ((error = !delta ? -1 : git_vector_insert(&onto_new, delta)) < 0) + break; + } + + if (!error) { + git_vector_swap(&onto->deltas, &onto_new); + git_pool_swap(&onto->pool, &onto_pool); + + if ((onto->opts.flags & GIT_DIFF_REVERSE) != 0) + onto->old_src = from->old_src; + else + onto->new_src = from->new_src; + + /* prefix strings also come from old pool, so recreate those.*/ + onto->opts.old_prefix = + git_pool_strdup_safe(&onto->pool, onto->opts.old_prefix); + onto->opts.new_prefix = + git_pool_strdup_safe(&onto->pool, onto->opts.new_prefix); + } + + git_vector_dispose_deep(&onto_new); + git_pool_clear(&onto_pool); + + return error; +} + +int git_diff_merge(git_diff *onto, const git_diff *from) +{ + return git_diff__merge(onto, from, git_diff__merge_like_cgit); +} + +int git_diff_find_similar__hashsig_for_file( + void **out, const git_diff_file *f, const char *path, void *p) +{ + git_hashsig_option_t opt = (git_hashsig_option_t)(intptr_t)p; + + GIT_UNUSED(f); + return git_hashsig_create_fromfile((git_hashsig **)out, path, opt); +} + +int git_diff_find_similar__hashsig_for_buf( + void **out, const git_diff_file *f, const char *buf, size_t len, void *p) +{ + git_hashsig_option_t opt = (git_hashsig_option_t)(intptr_t)p; + + GIT_UNUSED(f); + return git_hashsig_create((git_hashsig **)out, buf, len, opt); +} + +void git_diff_find_similar__hashsig_free(void *sig, void *payload) +{ + GIT_UNUSED(payload); + git_hashsig_free(sig); +} + +int git_diff_find_similar__calc_similarity( + int *score, void *siga, void *sigb, void *payload) +{ + int error; + + GIT_UNUSED(payload); + error = git_hashsig_compare(siga, sigb); + if (error < 0) + return error; + + *score = error; + return 0; +} + +#define DEFAULT_THRESHOLD 50 +#define DEFAULT_BREAK_REWRITE_THRESHOLD 60 +#define DEFAULT_RENAME_LIMIT 1000 + +static int normalize_find_opts( + git_diff *diff, + git_diff_find_options *opts, + const git_diff_find_options *given) +{ + git_config *cfg = NULL; + git_hashsig_option_t hashsig_opts; + + GIT_ERROR_CHECK_VERSION(given, GIT_DIFF_FIND_OPTIONS_VERSION, "git_diff_find_options"); + + if (diff->repo != NULL && + git_repository_config__weakptr(&cfg, diff->repo) < 0) + return -1; + + if (given) + memcpy(opts, given, sizeof(*opts)); + + if (!given || + (given->flags & GIT_DIFF_FIND_ALL) == GIT_DIFF_FIND_BY_CONFIG) + { + if (cfg) { + char *rule = + git_config__get_string_force(cfg, "diff.renames", "true"); + int boolval; + + if (!git__parse_bool(&boolval, rule) && !boolval) + /* don't set FIND_RENAMES if bool value is false */; + else if (!strcasecmp(rule, "copies") || !strcasecmp(rule, "copy")) + opts->flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES; + else + opts->flags |= GIT_DIFF_FIND_RENAMES; + + git__free(rule); + } else { + /* set default flag */ + opts->flags |= GIT_DIFF_FIND_RENAMES; + } + } + + /* some flags imply others */ + + if (opts->flags & GIT_DIFF_FIND_EXACT_MATCH_ONLY) { + /* if we are only looking for exact matches, then don't turn + * MODIFIED items into ADD/DELETE pairs because it's too picky + */ + opts->flags &= ~(GIT_DIFF_FIND_REWRITES | GIT_DIFF_BREAK_REWRITES); + + /* similarly, don't look for self-rewrites to split */ + opts->flags &= ~GIT_DIFF_FIND_RENAMES_FROM_REWRITES; + } + + if (opts->flags & GIT_DIFF_FIND_RENAMES_FROM_REWRITES) + opts->flags |= GIT_DIFF_FIND_RENAMES; + + if (opts->flags & GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED) + opts->flags |= GIT_DIFF_FIND_COPIES; + + if (opts->flags & GIT_DIFF_BREAK_REWRITES) + opts->flags |= GIT_DIFF_FIND_REWRITES; + +#define USE_DEFAULT(X) ((X) == 0 || (X) > 100) + + if (USE_DEFAULT(opts->rename_threshold)) + opts->rename_threshold = DEFAULT_THRESHOLD; + + if (USE_DEFAULT(opts->rename_from_rewrite_threshold)) + opts->rename_from_rewrite_threshold = DEFAULT_THRESHOLD; + + if (USE_DEFAULT(opts->copy_threshold)) + opts->copy_threshold = DEFAULT_THRESHOLD; + + if (USE_DEFAULT(opts->break_rewrite_threshold)) + opts->break_rewrite_threshold = DEFAULT_BREAK_REWRITE_THRESHOLD; + +#undef USE_DEFAULT + + if (!opts->rename_limit) { + if (cfg) { + opts->rename_limit = git_config__get_int_force( + cfg, "diff.renamelimit", DEFAULT_RENAME_LIMIT); + } + + if (opts->rename_limit <= 0) + opts->rename_limit = DEFAULT_RENAME_LIMIT; + } + + /* assign the internal metric with whitespace flag as payload */ + if (!opts->metric) { + opts->metric = git__malloc(sizeof(git_diff_similarity_metric)); + GIT_ERROR_CHECK_ALLOC(opts->metric); + + opts->metric->file_signature = git_diff_find_similar__hashsig_for_file; + opts->metric->buffer_signature = git_diff_find_similar__hashsig_for_buf; + opts->metric->free_signature = git_diff_find_similar__hashsig_free; + opts->metric->similarity = git_diff_find_similar__calc_similarity; + + if (opts->flags & GIT_DIFF_FIND_IGNORE_WHITESPACE) + hashsig_opts = GIT_HASHSIG_IGNORE_WHITESPACE; + else if (opts->flags & GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE) + hashsig_opts = GIT_HASHSIG_NORMAL; + else + hashsig_opts = GIT_HASHSIG_SMART_WHITESPACE; + hashsig_opts |= GIT_HASHSIG_ALLOW_SMALL_FILES; + opts->metric->payload = (void *)hashsig_opts; + } + + return 0; +} + +static int insert_delete_side_of_split( + git_diff *diff, git_vector *onto, const git_diff_delta *delta) +{ + /* make new record for DELETED side of split */ + git_diff_delta *deleted = git_diff__delta_dup(delta, &diff->pool); + GIT_ERROR_CHECK_ALLOC(deleted); + + deleted->status = GIT_DELTA_DELETED; + deleted->nfiles = 1; + memset(&deleted->new_file, 0, sizeof(deleted->new_file)); + deleted->new_file.path = deleted->old_file.path; + deleted->new_file.flags |= GIT_DIFF_FLAG_VALID_ID; + git_oid_clear(&deleted->new_file.id, diff->opts.oid_type); + + return git_vector_insert(onto, deleted); +} + +static int apply_splits_and_deletes( + git_diff *diff, size_t expected_size, bool actually_split) +{ + git_vector onto = GIT_VECTOR_INIT; + size_t i; + git_diff_delta *delta; + + if (git_vector_init(&onto, expected_size, diff->deltas._cmp) < 0) + return -1; + + /* build new delta list without TO_DELETE and splitting TO_SPLIT */ + git_vector_foreach(&diff->deltas, i, delta) { + if ((delta->flags & GIT_DIFF_FLAG__TO_DELETE) != 0) + continue; + + if ((delta->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0 && actually_split) { + delta->similarity = 0; + + if (insert_delete_side_of_split(diff, &onto, delta) < 0) + goto on_error; + + if (diff->new_src == GIT_ITERATOR_WORKDIR) + delta->status = GIT_DELTA_UNTRACKED; + else + delta->status = GIT_DELTA_ADDED; + delta->nfiles = 1; + memset(&delta->old_file, 0, sizeof(delta->old_file)); + delta->old_file.path = delta->new_file.path; + delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID; + git_oid_clear(&delta->old_file.id, diff->opts.oid_type); + } + + /* clean up delta before inserting into new list */ + GIT_DIFF_FLAG__CLEAR_INTERNAL(delta->flags); + + if (delta->status != GIT_DELTA_COPIED && + delta->status != GIT_DELTA_RENAMED && + (delta->status != GIT_DELTA_MODIFIED || actually_split)) + delta->similarity = 0; + + /* insert into new list */ + if (git_vector_insert(&onto, delta) < 0) + goto on_error; + } + + /* cannot return an error past this point */ + + /* free deltas from old list that didn't make it to the new one */ + git_vector_foreach(&diff->deltas, i, delta) { + if ((delta->flags & GIT_DIFF_FLAG__TO_DELETE) != 0) + git__free(delta); + } + + /* swap new delta list into place */ + git_vector_swap(&diff->deltas, &onto); + git_vector_dispose(&onto); + git_vector_sort(&diff->deltas); + + return 0; + +on_error: + git_vector_dispose_deep(&onto); + + return -1; +} + +GIT_INLINE(git_diff_file *) similarity_get_file(git_diff *diff, size_t idx) +{ + git_diff_delta *delta = git_vector_get(&diff->deltas, idx / 2); + return (idx & 1) ? &delta->new_file : &delta->old_file; +} + +typedef struct { + size_t idx; + git_iterator_t src; + git_repository *repo; + git_diff_file *file; + git_str data; + git_odb_object *odb_obj; + git_blob *blob; +} similarity_info; + +static int similarity_init( + similarity_info *info, git_diff *diff, size_t file_idx) +{ + info->idx = file_idx; + info->src = (file_idx & 1) ? diff->new_src : diff->old_src; + info->repo = diff->repo; + info->file = similarity_get_file(diff, file_idx); + info->odb_obj = NULL; + info->blob = NULL; + git_str_init(&info->data, 0); + + if ((info->file->flags & GIT_DIFF_FLAG_VALID_SIZE) || + info->src == GIT_ITERATOR_WORKDIR) + return 0; + + return git_diff_file__resolve_zero_size( + info->file, &info->odb_obj, info->repo); +} + +static int similarity_sig( + similarity_info *info, + const git_diff_find_options *opts, + void **cache) +{ + int error = 0; + git_diff_file *file = info->file; + + if (info->src == GIT_ITERATOR_WORKDIR) { + if ((error = git_repository_workdir_path( + &info->data, info->repo, file->path)) < 0) + return error; + + /* if path is not a regular file, just skip this item */ + if (!git_fs_path_isfile(info->data.ptr)) + return 0; + + /* TODO: apply wd-to-odb filters to file data if necessary */ + + error = opts->metric->file_signature( + &cache[info->idx], info->file, + info->data.ptr, opts->metric->payload); + } else { + /* if we didn't initially know the size, we might have an odb_obj + * around from earlier, so convert that, otherwise load the blob now + */ + if (info->odb_obj != NULL) + error = git_object__from_odb_object( + (git_object **)&info->blob, info->repo, + info->odb_obj, GIT_OBJECT_BLOB); + else + error = git_blob_lookup(&info->blob, info->repo, &file->id); + + if (error < 0) { + /* if lookup fails, just skip this item in similarity calc */ + git_error_clear(); + } else { + size_t sz; + + /* index size may not be actual blob size if filtered */ + if (file->size != git_blob_rawsize(info->blob)) + file->size = git_blob_rawsize(info->blob); + + sz = git__is_sizet(file->size) ? (size_t)file->size : (size_t)-1; + + error = opts->metric->buffer_signature( + &cache[info->idx], info->file, + git_blob_rawcontent(info->blob), sz, opts->metric->payload); + } + } + + return error; +} + +static void similarity_unload(similarity_info *info) +{ + if (info->odb_obj) + git_odb_object_free(info->odb_obj); + + if (info->blob) + git_blob_free(info->blob); + else + git_str_dispose(&info->data); +} + +#define FLAG_SET(opts,flag_name) (((opts)->flags & flag_name) != 0) + +/* - score < 0 means files cannot be compared + * - score >= 100 means files are exact match + * - score == 0 means files are completely different + */ +static int similarity_measure( + int *score, + git_diff *diff, + const git_diff_find_options *opts, + void **cache, + size_t a_idx, + size_t b_idx) +{ + git_diff_file *a_file = similarity_get_file(diff, a_idx); + git_diff_file *b_file = similarity_get_file(diff, b_idx); + bool exact_match = FLAG_SET(opts, GIT_DIFF_FIND_EXACT_MATCH_ONLY); + int error = 0; + similarity_info a_info, b_info; + + *score = -1; + + /* don't try to compare things that aren't files */ + if (!GIT_MODE_ISBLOB(a_file->mode) || !GIT_MODE_ISBLOB(b_file->mode)) + return 0; + + /* if exact match is requested, force calculation of missing OIDs now */ + if (exact_match) { + if (git_oid_is_zero(&a_file->id) && + diff->old_src == GIT_ITERATOR_WORKDIR && + !git_diff__oid_for_file(&a_file->id, + diff, a_file->path, a_file->mode, a_file->size)) + a_file->flags |= GIT_DIFF_FLAG_VALID_ID; + + if (git_oid_is_zero(&b_file->id) && + diff->new_src == GIT_ITERATOR_WORKDIR && + !git_diff__oid_for_file(&b_file->id, + diff, b_file->path, b_file->mode, b_file->size)) + b_file->flags |= GIT_DIFF_FLAG_VALID_ID; + } + + /* check OID match as a quick test */ + if (git_oid__cmp(&a_file->id, &b_file->id) == 0) { + *score = 100; + return 0; + } + + /* don't calculate signatures if we are doing exact match */ + if (exact_match) { + *score = 0; + return 0; + } + + memset(&a_info, 0, sizeof(a_info)); + memset(&b_info, 0, sizeof(b_info)); + + /* set up similarity data (will try to update missing file sizes) */ + if (!cache[a_idx] && (error = similarity_init(&a_info, diff, a_idx)) < 0) + return error; + if (!cache[b_idx] && (error = similarity_init(&b_info, diff, b_idx)) < 0) + goto cleanup; + + /* check if file sizes are nowhere near each other */ + if (a_file->size > 127 && + b_file->size > 127 && + (a_file->size > (b_file->size << 3) || + b_file->size > (a_file->size << 3))) + goto cleanup; + + /* update signature cache if needed */ + if (!cache[a_idx]) { + if ((error = similarity_sig(&a_info, opts, cache)) < 0) + goto cleanup; + } + if (!cache[b_idx]) { + if ((error = similarity_sig(&b_info, opts, cache)) < 0) + goto cleanup; + } + + /* calculate similarity provided that the metric choose to process + * both the a and b files (some may not if file is too big, etc). + */ + if (cache[a_idx] && cache[b_idx]) + error = opts->metric->similarity( + score, cache[a_idx], cache[b_idx], opts->metric->payload); + +cleanup: + similarity_unload(&a_info); + similarity_unload(&b_info); + + return error; +} + +static int calc_self_similarity( + git_diff *diff, + const git_diff_find_options *opts, + size_t delta_idx, + void **cache) +{ + int error, similarity = -1; + git_diff_delta *delta = GIT_VECTOR_GET(&diff->deltas, delta_idx); + + if ((delta->flags & GIT_DIFF_FLAG__HAS_SELF_SIMILARITY) != 0) + return 0; + + error = similarity_measure( + &similarity, diff, opts, cache, 2 * delta_idx, 2 * delta_idx + 1); + if (error < 0) + return error; + + if (similarity >= 0) { + delta->similarity = (uint16_t)similarity; + delta->flags |= GIT_DIFF_FLAG__HAS_SELF_SIMILARITY; + } + + return 0; +} + +static void handle_non_blob( + git_diff *diff, + const git_diff_find_options *opts, + size_t delta_idx) +{ + git_diff_delta *delta = GIT_VECTOR_GET(&diff->deltas, delta_idx); + + /* skip things that are blobs */ + if (GIT_MODE_ISBLOB(delta->old_file.mode)) + return; + + /* honor "remove unmodified" flag for non-blobs (eg submodules) */ + if (delta->status == GIT_DELTA_UNMODIFIED && + FLAG_SET(opts, GIT_DIFF_FIND_REMOVE_UNMODIFIED)) + delta->flags |= GIT_DIFF_FLAG__TO_DELETE; +} + +static bool is_rename_target( + git_diff *diff, + const git_diff_find_options *opts, + size_t delta_idx, + void **cache) +{ + git_diff_delta *delta = GIT_VECTOR_GET(&diff->deltas, delta_idx); + + /* skip things that aren't plain blobs */ + if (!GIT_MODE_ISBLOB(delta->new_file.mode)) + return false; + + /* only consider ADDED, RENAMED, COPIED, and split MODIFIED as + * targets; maybe include UNTRACKED if requested. + */ + switch (delta->status) { + case GIT_DELTA_UNMODIFIED: + case GIT_DELTA_DELETED: + case GIT_DELTA_IGNORED: + case GIT_DELTA_CONFLICTED: + return false; + + case GIT_DELTA_MODIFIED: + if (!FLAG_SET(opts, GIT_DIFF_FIND_REWRITES) && + !FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES)) + return false; + + if (calc_self_similarity(diff, opts, delta_idx, cache) < 0) + return false; + + if (FLAG_SET(opts, GIT_DIFF_BREAK_REWRITES) && + delta->similarity < opts->break_rewrite_threshold) { + delta->flags |= GIT_DIFF_FLAG__TO_SPLIT; + break; + } + if (FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES) && + delta->similarity < opts->rename_from_rewrite_threshold) { + delta->flags |= GIT_DIFF_FLAG__TO_SPLIT; + break; + } + + return false; + + case GIT_DELTA_UNTRACKED: + if (!FLAG_SET(opts, GIT_DIFF_FIND_FOR_UNTRACKED)) + return false; + break; + + default: /* all other status values should be checked */ + break; + } + + delta->flags |= GIT_DIFF_FLAG__IS_RENAME_TARGET; + return true; +} + +static bool is_rename_source( + git_diff *diff, + const git_diff_find_options *opts, + size_t delta_idx, + void **cache) +{ + git_diff_delta *delta = GIT_VECTOR_GET(&diff->deltas, delta_idx); + + /* skip things that aren't blobs */ + if (!GIT_MODE_ISBLOB(delta->old_file.mode)) + return false; + + switch (delta->status) { + case GIT_DELTA_ADDED: + case GIT_DELTA_UNTRACKED: + case GIT_DELTA_UNREADABLE: + case GIT_DELTA_IGNORED: + case GIT_DELTA_CONFLICTED: + return false; + + case GIT_DELTA_DELETED: + case GIT_DELTA_TYPECHANGE: + break; + + case GIT_DELTA_UNMODIFIED: + if (!FLAG_SET(opts, GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED)) + return false; + if (FLAG_SET(opts, GIT_DIFF_FIND_REMOVE_UNMODIFIED)) + delta->flags |= GIT_DIFF_FLAG__TO_DELETE; + break; + + default: /* MODIFIED, RENAMED, COPIED */ + /* if we're finding copies, this could be a source */ + if (FLAG_SET(opts, GIT_DIFF_FIND_COPIES)) + break; + + /* otherwise, this is only a source if we can split it */ + if (!FLAG_SET(opts, GIT_DIFF_FIND_REWRITES) && + !FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES)) + return false; + + if (calc_self_similarity(diff, opts, delta_idx, cache) < 0) + return false; + + if (FLAG_SET(opts, GIT_DIFF_BREAK_REWRITES) && + delta->similarity < opts->break_rewrite_threshold) { + delta->flags |= GIT_DIFF_FLAG__TO_SPLIT; + break; + } + + if (FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES) && + delta->similarity < opts->rename_from_rewrite_threshold) + break; + + return false; + } + + delta->flags |= GIT_DIFF_FLAG__IS_RENAME_SOURCE; + return true; +} + +GIT_INLINE(bool) delta_is_split(git_diff_delta *delta) +{ + return (delta->status == GIT_DELTA_TYPECHANGE || + (delta->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0); +} + +GIT_INLINE(bool) delta_is_new_only(git_diff_delta *delta) +{ + return (delta->status == GIT_DELTA_ADDED || + delta->status == GIT_DELTA_UNTRACKED || + delta->status == GIT_DELTA_UNREADABLE || + delta->status == GIT_DELTA_IGNORED); +} + +GIT_INLINE(void) delta_make_rename( + git_diff_delta *to, const git_diff_delta *from, uint16_t similarity) +{ + to->status = GIT_DELTA_RENAMED; + to->similarity = similarity; + to->nfiles = 2; + memcpy(&to->old_file, &from->old_file, sizeof(to->old_file)); + to->flags &= ~GIT_DIFF_FLAG__TO_SPLIT; +} + +typedef struct { + size_t idx; + uint16_t similarity; +} diff_find_match; + +int git_diff_find_similar( + git_diff *diff, + const git_diff_find_options *given_opts) +{ + size_t s, t; + int error = 0, result; + uint16_t similarity; + git_diff_delta *src, *tgt; + git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; + size_t num_deltas, num_srcs = 0, num_tgts = 0; + size_t tried_srcs = 0, tried_tgts = 0; + size_t num_rewrites = 0, num_updates = 0, num_bumped = 0, + num_to_delete = 0; + size_t sigcache_size; + void **sigcache = NULL; /* cache of similarity metric file signatures */ + diff_find_match *tgt2src = NULL; + diff_find_match *src2tgt = NULL; + diff_find_match *tgt2src_copy = NULL; + diff_find_match *best_match; + git_diff_file swap; + + GIT_ASSERT_ARG(diff); + + if ((error = normalize_find_opts(diff, &opts, given_opts)) < 0) + return error; + + num_deltas = diff->deltas.length; + + /* TODO: maybe abort if deltas.length > rename_limit ??? */ + if (!num_deltas || !git__is_uint32(num_deltas)) + goto cleanup; + + /* No flags set; nothing to do */ + if ((opts.flags & GIT_DIFF_FIND_ALL) == 0) + goto cleanup; + + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&sigcache_size, num_deltas, 2); + sigcache = git__calloc(sigcache_size, sizeof(void *)); + GIT_ERROR_CHECK_ALLOC(sigcache); + + /* Label rename sources and targets + * + * This will also set self-similarity scores for MODIFIED files and + * mark them for splitting if break-rewrites is enabled + */ + git_vector_foreach(&diff->deltas, t, tgt) { + handle_non_blob(diff, &opts, t); + + if (is_rename_source(diff, &opts, t, sigcache)) + ++num_srcs; + + if (is_rename_target(diff, &opts, t, sigcache)) + ++num_tgts; + + if ((tgt->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0) + num_rewrites++; + + if ((tgt->flags & GIT_DIFF_FLAG__TO_DELETE) != 0) + num_to_delete++; + } + + /* If there are no candidate srcs or tgts, no need to find matches */ + if (!num_srcs || !num_tgts) + goto split_and_delete; + + src2tgt = git__calloc(num_deltas, sizeof(diff_find_match)); + GIT_ERROR_CHECK_ALLOC(src2tgt); + tgt2src = git__calloc(num_deltas, sizeof(diff_find_match)); + GIT_ERROR_CHECK_ALLOC(tgt2src); + + if (FLAG_SET(&opts, GIT_DIFF_FIND_COPIES)) { + tgt2src_copy = git__calloc(num_deltas, sizeof(diff_find_match)); + GIT_ERROR_CHECK_ALLOC(tgt2src_copy); + } + + /* + * Find best-fit matches for rename / copy candidates + */ + +find_best_matches: + tried_tgts = num_bumped = 0; + + git_vector_foreach(&diff->deltas, t, tgt) { + /* skip things that are not rename targets */ + if ((tgt->flags & GIT_DIFF_FLAG__IS_RENAME_TARGET) == 0) + continue; + + tried_srcs = 0; + + git_vector_foreach(&diff->deltas, s, src) { + /* skip things that are not rename sources */ + if ((src->flags & GIT_DIFF_FLAG__IS_RENAME_SOURCE) == 0) + continue; + + /* calculate similarity for this pair and find best match */ + if (s == t) + result = -1; /* don't measure self-similarity here */ + else if ((error = similarity_measure( + &result, diff, &opts, sigcache, 2 * s, 2 * t + 1)) < 0) + goto cleanup; + + if (result < 0) + continue; + similarity = (uint16_t)result; + + /* is this a better rename? */ + if (tgt2src[t].similarity < similarity && + src2tgt[s].similarity < similarity) + { + /* eject old mapping */ + if (src2tgt[s].similarity > 0) { + tgt2src[src2tgt[s].idx].similarity = 0; + num_bumped++; + } + if (tgt2src[t].similarity > 0) { + src2tgt[tgt2src[t].idx].similarity = 0; + num_bumped++; + } + + /* write new mapping */ + tgt2src[t].idx = s; + tgt2src[t].similarity = similarity; + src2tgt[s].idx = t; + src2tgt[s].similarity = similarity; + } + + /* keep best absolute match for copies */ + if (tgt2src_copy != NULL && + tgt2src_copy[t].similarity < similarity) + { + tgt2src_copy[t].idx = s; + tgt2src_copy[t].similarity = similarity; + } + + if (++tried_srcs >= num_srcs) + break; + + /* cap on maximum targets we'll examine (per "tgt" file) */ + if (tried_srcs > opts.rename_limit) + break; + } + + if (++tried_tgts >= num_tgts) + break; + } + + if (num_bumped > 0) /* try again if we bumped some items */ + goto find_best_matches; + + /* + * Rewrite the diffs with renames / copies + */ + + git_vector_foreach(&diff->deltas, t, tgt) { + /* skip things that are not rename targets */ + if ((tgt->flags & GIT_DIFF_FLAG__IS_RENAME_TARGET) == 0) + continue; + + /* check if this delta was the target of a similarity */ + if (tgt2src[t].similarity) + best_match = &tgt2src[t]; + else if (tgt2src_copy && tgt2src_copy[t].similarity) + best_match = &tgt2src_copy[t]; + else + continue; + + s = best_match->idx; + src = GIT_VECTOR_GET(&diff->deltas, s); + + /* possible scenarios: + * 1. from DELETE to ADD/UNTRACK/IGNORE = RENAME + * 2. from DELETE to SPLIT/TYPECHANGE = RENAME + DELETE + * 3. from SPLIT/TYPECHANGE to ADD/UNTRACK/IGNORE = ADD + RENAME + * 4. from SPLIT/TYPECHANGE to SPLIT/TYPECHANGE = RENAME + SPLIT + * 5. from OTHER to ADD/UNTRACK/IGNORE = OTHER + COPY + */ + + if (src->status == GIT_DELTA_DELETED) { + + if (delta_is_new_only(tgt)) { + + if (best_match->similarity < opts.rename_threshold) + continue; + + delta_make_rename(tgt, src, best_match->similarity); + + src->flags |= GIT_DIFF_FLAG__TO_DELETE; + num_rewrites++; + } else { + GIT_ASSERT(delta_is_split(tgt)); + + if (best_match->similarity < opts.rename_from_rewrite_threshold) + continue; + + memcpy(&swap, &tgt->old_file, sizeof(swap)); + + delta_make_rename(tgt, src, best_match->similarity); + num_rewrites--; + + GIT_ASSERT(src->status == GIT_DELTA_DELETED); + memcpy(&src->old_file, &swap, sizeof(src->old_file)); + memset(&src->new_file, 0, sizeof(src->new_file)); + src->new_file.path = src->old_file.path; + src->new_file.flags |= GIT_DIFF_FLAG_VALID_ID; + git_oid_clear(&src->new_file.id, diff->opts.oid_type); + + num_updates++; + + if (src2tgt[t].similarity > 0 && src2tgt[t].idx > t) { + /* what used to be at src t is now at src s */ + tgt2src[src2tgt[t].idx].idx = s; + } + } + } + + else if (delta_is_split(src)) { + + if (delta_is_new_only(tgt)) { + + if (best_match->similarity < opts.rename_threshold) + continue; + + delta_make_rename(tgt, src, best_match->similarity); + + src->status = (diff->new_src == GIT_ITERATOR_WORKDIR) ? + GIT_DELTA_UNTRACKED : GIT_DELTA_ADDED; + src->nfiles = 1; + memset(&src->old_file, 0, sizeof(src->old_file)); + src->old_file.path = src->new_file.path; + src->old_file.flags |= GIT_DIFF_FLAG_VALID_ID; + git_oid_clear(&src->old_file.id, diff->opts.oid_type); + + src->flags &= ~GIT_DIFF_FLAG__TO_SPLIT; + num_rewrites--; + + num_updates++; + } else { + GIT_ASSERT(delta_is_split(src)); + + if (best_match->similarity < opts.rename_from_rewrite_threshold) + continue; + + memcpy(&swap, &tgt->old_file, sizeof(swap)); + + delta_make_rename(tgt, src, best_match->similarity); + num_rewrites--; + num_updates++; + + memcpy(&src->old_file, &swap, sizeof(src->old_file)); + + /* if we've just swapped the new element into the correct + * place, clear the SPLIT and RENAME_TARGET flags + */ + if (tgt2src[s].idx == t && + tgt2src[s].similarity > + opts.rename_from_rewrite_threshold) { + src->status = GIT_DELTA_RENAMED; + src->similarity = tgt2src[s].similarity; + tgt2src[s].similarity = 0; + src->flags &= ~(GIT_DIFF_FLAG__TO_SPLIT | GIT_DIFF_FLAG__IS_RENAME_TARGET); + num_rewrites--; + } + /* otherwise, if we just overwrote a source, update mapping */ + else if (src2tgt[t].similarity > 0 && src2tgt[t].idx > t) { + /* what used to be at src t is now at src s */ + tgt2src[src2tgt[t].idx].idx = s; + } + + num_updates++; + } + } + + else if (FLAG_SET(&opts, GIT_DIFF_FIND_COPIES)) { + if (tgt2src_copy[t].similarity < opts.copy_threshold) + continue; + + /* always use best possible source for copy */ + best_match = &tgt2src_copy[t]; + src = GIT_VECTOR_GET(&diff->deltas, best_match->idx); + + if (delta_is_split(tgt)) { + error = insert_delete_side_of_split(diff, &diff->deltas, tgt); + if (error < 0) + goto cleanup; + num_rewrites--; + } + + if (!delta_is_split(tgt) && !delta_is_new_only(tgt)) + continue; + + tgt->status = GIT_DELTA_COPIED; + tgt->similarity = best_match->similarity; + tgt->nfiles = 2; + memcpy(&tgt->old_file, &src->old_file, sizeof(tgt->old_file)); + tgt->flags &= ~GIT_DIFF_FLAG__TO_SPLIT; + + num_updates++; + } + } + +split_and_delete: + /* + * Actually split and delete entries as needed + */ + + if (num_rewrites > 0 || num_updates > 0 || num_to_delete > 0) { + size_t apply_len = diff->deltas.length - + num_rewrites - num_to_delete; + + error = apply_splits_and_deletes( + diff, apply_len, + FLAG_SET(&opts, GIT_DIFF_BREAK_REWRITES) && + !FLAG_SET(&opts, GIT_DIFF_BREAK_REWRITES_FOR_RENAMES_ONLY)); + } + +cleanup: + git__free(tgt2src); + git__free(src2tgt); + git__free(tgt2src_copy); + + if (sigcache) { + for (t = 0; t < num_deltas * 2; ++t) { + if (sigcache[t] != NULL) + opts.metric->free_signature(sigcache[t], opts.metric->payload); + } + git__free(sigcache); + } + + if (!given_opts || !given_opts->metric) + git__free(opts.metric); + + return error; +} + +#undef FLAG_SET diff --git a/src/libgit2/diff_tform.h b/src/libgit2/diff_tform.h new file mode 100644 index 00000000000..7abb8b3fedd --- /dev/null +++ b/src/libgit2/diff_tform.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_diff_tform_h__ +#define INCLUDE_diff_tform_h__ + +#include "common.h" + +#include "diff_file.h" + +extern int git_diff_find_similar__hashsig_for_file( + void **out, const git_diff_file *f, const char *path, void *p); + +extern int git_diff_find_similar__hashsig_for_buf( + void **out, const git_diff_file *f, const char *buf, size_t len, void *p); + +extern void git_diff_find_similar__hashsig_free(void *sig, void *payload); + +extern int git_diff_find_similar__calc_similarity( + int *score, void *siga, void *sigb, void *payload); + +#endif diff --git a/src/libgit2/diff_xdiff.c b/src/libgit2/diff_xdiff.c new file mode 100644 index 00000000000..5f56c5209a0 --- /dev/null +++ b/src/libgit2/diff_xdiff.c @@ -0,0 +1,261 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "diff_xdiff.h" + +#include "git2/errors.h" +#include "diff.h" +#include "diff_driver.h" +#include "patch_generate.h" +#include "utf8.h" + +static int git_xdiff_scan_int(const char **str, int *value) +{ + const char *scan = *str; + int v = 0, digits = 0; + /* find next digit */ + for (scan = *str; *scan && !git__isdigit(*scan); scan++); + /* parse next number */ + for (; git__isdigit(*scan); scan++, digits++) + v = (v * 10) + (*scan - '0'); + *str = scan; + *value = v; + return (digits > 0) ? 0 : -1; +} + +static int git_xdiff_parse_hunk(git_diff_hunk *hunk, const char *header) +{ + /* expect something of the form "@@ -%d[,%d] +%d[,%d] @@" */ + if (*header != '@') + goto fail; + if (git_xdiff_scan_int(&header, &hunk->old_start) < 0) + goto fail; + if (*header == ',') { + if (git_xdiff_scan_int(&header, &hunk->old_lines) < 0) + goto fail; + } else + hunk->old_lines = 1; + if (git_xdiff_scan_int(&header, &hunk->new_start) < 0) + goto fail; + if (*header == ',') { + if (git_xdiff_scan_int(&header, &hunk->new_lines) < 0) + goto fail; + } else + hunk->new_lines = 1; + if (hunk->old_start < 0 || hunk->new_start < 0) + goto fail; + + return 0; + +fail: + git_error_set(GIT_ERROR_INVALID, "malformed hunk header from xdiff"); + return -1; +} + +typedef struct { + git_xdiff_output *xo; + git_patch_generated *patch; + git_diff_hunk hunk; + int old_lineno, new_lineno; + mmfile_t xd_old_data, xd_new_data; +} git_xdiff_info; + +static int diff_update_lines( + git_xdiff_info *info, + git_diff_line *line, + const char *content, + size_t content_len) +{ + const char *scan = content, *scan_end = content + content_len; + + for (line->num_lines = 0; scan < scan_end; ++scan) + if (*scan == '\n') + ++line->num_lines; + + line->content = content; + line->content_len = content_len; + + /* expect " "/"-"/"+", then data */ + switch (line->origin) { + case GIT_DIFF_LINE_ADDITION: + case GIT_DIFF_LINE_DEL_EOFNL: + line->old_lineno = -1; + line->new_lineno = info->new_lineno; + info->new_lineno += (int)line->num_lines; + break; + case GIT_DIFF_LINE_DELETION: + case GIT_DIFF_LINE_ADD_EOFNL: + line->old_lineno = info->old_lineno; + line->new_lineno = -1; + info->old_lineno += (int)line->num_lines; + break; + case GIT_DIFF_LINE_CONTEXT: + case GIT_DIFF_LINE_CONTEXT_EOFNL: + line->old_lineno = info->old_lineno; + line->new_lineno = info->new_lineno; + info->old_lineno += (int)line->num_lines; + info->new_lineno += (int)line->num_lines; + break; + default: + git_error_set(GIT_ERROR_INVALID, "unknown diff line origin %02x", + (unsigned int)line->origin); + return -1; + } + + return 0; +} + +static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len) +{ + git_xdiff_info *info = priv; + git_patch_generated *patch = info->patch; + const git_diff_delta *delta = patch->base.delta; + git_patch_generated_output *output = &info->xo->output; + git_diff_line line; + size_t buffer_len; + + if (len == 1) { + output->error = git_xdiff_parse_hunk(&info->hunk, bufs[0].ptr); + if (output->error < 0) + return output->error; + + info->hunk.header_len = bufs[0].size; + if (info->hunk.header_len >= sizeof(info->hunk.header)) + info->hunk.header_len = sizeof(info->hunk.header) - 1; + + /* Sanitize the hunk header in case there is invalid Unicode */ + buffer_len = git_utf8_valid_buf_length(bufs[0].ptr, info->hunk.header_len); + /* Sanitizing the hunk header may delete the newline, so add it back again if there is room */ + if (buffer_len < info->hunk.header_len) { + bufs[0].ptr[buffer_len] = '\n'; + buffer_len += 1; + info->hunk.header_len = buffer_len; + } + + memcpy(info->hunk.header, bufs[0].ptr, info->hunk.header_len); + info->hunk.header[info->hunk.header_len] = '\0'; + + if (output->hunk_cb != NULL && + (output->error = output->hunk_cb( + delta, &info->hunk, output->payload))) + return output->error; + + info->old_lineno = info->hunk.old_start; + info->new_lineno = info->hunk.new_start; + } + + if (len == 2 || len == 3) { + /* expect " "/"-"/"+", then data */ + line.origin = + (*bufs[0].ptr == '+') ? GIT_DIFF_LINE_ADDITION : + (*bufs[0].ptr == '-') ? GIT_DIFF_LINE_DELETION : + GIT_DIFF_LINE_CONTEXT; + + if (line.origin == GIT_DIFF_LINE_ADDITION) + line.content_offset = bufs[1].ptr - info->xd_new_data.ptr; + else if (line.origin == GIT_DIFF_LINE_DELETION) + line.content_offset = bufs[1].ptr - info->xd_old_data.ptr; + else + line.content_offset = -1; + + output->error = diff_update_lines( + info, &line, bufs[1].ptr, bufs[1].size); + + if (!output->error && output->data_cb != NULL) + output->error = output->data_cb( + delta, &info->hunk, &line, output->payload); + } + + if (len == 3 && !output->error) { + /* If we have a '+' and a third buf, then we have added a line + * without a newline and the old code had one, so DEL_EOFNL. + * If we have a '-' and a third buf, then we have removed a line + * with out a newline but added a blank line, so ADD_EOFNL. + */ + line.origin = + (*bufs[0].ptr == '+') ? GIT_DIFF_LINE_DEL_EOFNL : + (*bufs[0].ptr == '-') ? GIT_DIFF_LINE_ADD_EOFNL : + GIT_DIFF_LINE_CONTEXT_EOFNL; + + line.content_offset = -1; + + output->error = diff_update_lines( + info, &line, bufs[2].ptr, bufs[2].size); + + if (!output->error && output->data_cb != NULL) + output->error = output->data_cb( + delta, &info->hunk, &line, output->payload); + } + + return output->error; +} + +static int git_xdiff(git_patch_generated_output *output, git_patch_generated *patch) +{ + git_xdiff_output *xo = (git_xdiff_output *)output; + git_xdiff_info info; + git_diff_find_context_payload findctxt; + + memset(&info, 0, sizeof(info)); + info.patch = patch; + info.xo = xo; + + xo->callback.priv = &info; + + git_diff_find_context_init( + &xo->config.find_func, &findctxt, git_patch_generated_driver(patch)); + xo->config.find_func_priv = &findctxt; + + if (xo->config.find_func != NULL) + xo->config.flags |= XDL_EMIT_FUNCNAMES; + else + xo->config.flags &= ~XDL_EMIT_FUNCNAMES; + + /* TODO: check ofile.opts_flags to see if driver-specific per-file + * updates are needed to xo->params.flags + */ + + if (git_patch_generated_old_data(&info.xd_old_data.ptr, &info.xd_old_data.size, patch) < 0 || + git_patch_generated_new_data(&info.xd_new_data.ptr, &info.xd_new_data.size, patch) < 0) + return -1; + + xdl_diff(&info.xd_old_data, &info.xd_new_data, + &xo->params, &xo->config, &xo->callback); + + git_diff_find_context_clear(&findctxt); + + return xo->output.error; +} + +void git_xdiff_init(git_xdiff_output *xo, const git_diff_options *opts) +{ + uint32_t flags = opts ? opts->flags : 0; + + xo->output.diff_cb = git_xdiff; + + xo->config.ctxlen = opts ? opts->context_lines : 3; + xo->config.interhunkctxlen = opts ? opts->interhunk_lines : 0; + + if (flags & GIT_DIFF_IGNORE_WHITESPACE) + xo->params.flags |= XDF_WHITESPACE_FLAGS; + if (flags & GIT_DIFF_IGNORE_WHITESPACE_CHANGE) + xo->params.flags |= XDF_IGNORE_WHITESPACE_CHANGE; + if (flags & GIT_DIFF_IGNORE_WHITESPACE_EOL) + xo->params.flags |= XDF_IGNORE_WHITESPACE_AT_EOL; + if (flags & GIT_DIFF_INDENT_HEURISTIC) + xo->params.flags |= XDF_INDENT_HEURISTIC; + + if (flags & GIT_DIFF_PATIENCE) + xo->params.flags |= XDF_PATIENCE_DIFF; + if (flags & GIT_DIFF_MINIMAL) + xo->params.flags |= XDF_NEED_MINIMAL; + + if (flags & GIT_DIFF_IGNORE_BLANK_LINES) + xo->params.flags |= XDF_IGNORE_BLANK_LINES; + + xo->callback.out_line = git_xdiff_cb; +} diff --git a/src/libgit2/diff_xdiff.h b/src/libgit2/diff_xdiff.h new file mode 100644 index 00000000000..327dc7c4ab3 --- /dev/null +++ b/src/libgit2/diff_xdiff.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_diff_xdiff_h__ +#define INCLUDE_diff_xdiff_h__ + +#include "common.h" + +#include "diff.h" +#include "xdiff.h" +#include "patch_generate.h" + +/* xdiff cannot cope with large files. these files should not be passed to + * xdiff. callers should treat these large files as binary. + */ +#define GIT_XDIFF_MAX_SIZE (INT64_C(1024) * 1024 * 1023) + +/* A git_xdiff_output is a git_patch_generate_output with extra fields + * necessary to use libxdiff. Calling git_xdiff_init() will set the diff_cb + * field of the output to use xdiff to generate the diffs. + */ +typedef struct { + git_patch_generated_output output; + + xdemitconf_t config; + xpparam_t params; + xdemitcb_t callback; +} git_xdiff_output; + +void git_xdiff_init(git_xdiff_output *xo, const git_diff_options *opts); + +#endif diff --git a/src/libgit2/email.c b/src/libgit2/email.c new file mode 100644 index 00000000000..c1470c16323 --- /dev/null +++ b/src/libgit2/email.c @@ -0,0 +1,317 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "email.h" + +#include "common.h" +#include "buf.h" +#include "diff_generate.h" +#include "diff_stats.h" +#include "patch.h" +#include "date.h" + +#include "git2/email.h" +#include "git2/patch.h" +#include "git2/sys/email.h" +#include "git2/version.h" + +/* + * Git uses a "magic" timestamp to indicate that an email message + * is from `git format-patch` (or our equivalent). + */ +#define EMAIL_TIMESTAMP "Mon Sep 17 00:00:00 2001" + +GIT_INLINE(int) include_prefix( + size_t patch_count, + git_email_create_options *opts) +{ + return ((!opts->subject_prefix || *opts->subject_prefix) || + (opts->flags & GIT_EMAIL_CREATE_ALWAYS_NUMBER) != 0 || + opts->reroll_number || + (patch_count > 1 && !(opts->flags & GIT_EMAIL_CREATE_OMIT_NUMBERS))); +} + +static int append_prefix( + git_str *out, + size_t patch_idx, + size_t patch_count, + git_email_create_options *opts) +{ + const char *subject_prefix = opts->subject_prefix ? + opts->subject_prefix : "PATCH"; + + git_str_putc(out, '['); + + if (*subject_prefix) + git_str_puts(out, subject_prefix); + + if (opts->reroll_number) { + if (*subject_prefix) + git_str_putc(out, ' '); + + git_str_printf(out, "v%" PRIuZ, opts->reroll_number); + } + + if ((opts->flags & GIT_EMAIL_CREATE_ALWAYS_NUMBER) != 0 || + (patch_count > 1 && !(opts->flags & GIT_EMAIL_CREATE_OMIT_NUMBERS))) { + size_t start_number = opts->start_number ? + opts->start_number : 1; + + if (*subject_prefix || opts->reroll_number) + git_str_putc(out, ' '); + + git_str_printf(out, "%" PRIuZ "/%" PRIuZ, + patch_idx + (start_number - 1), + patch_count + (start_number - 1)); + } + + git_str_puts(out, "]"); + + return git_str_oom(out) ? -1 : 0; +} + +static int append_date( + git_str *out, + const git_time *date) +{ + int error; + + if ((error = git_str_printf(out, "Date: ")) == 0 && + (error = git_date_rfc2822_fmt(out, date->time, date->offset)) == 0) + error = git_str_putc(out, '\n'); + + return error; +} + +static int append_subject( + git_str *out, + size_t patch_idx, + size_t patch_count, + const char *summary, + git_email_create_options *opts) +{ + bool prefix = include_prefix(patch_count, opts); + size_t summary_len = summary ? strlen(summary) : 0; + int error; + + if (summary_len) { + const char *nl = strchr(summary, '\n'); + + if (nl) + summary_len = (nl - summary); + } + + if ((error = git_str_puts(out, "Subject: ")) < 0) + return error; + + if (prefix && + (error = append_prefix(out, patch_idx, patch_count, opts)) < 0) + return error; + + if (prefix && summary_len && (error = git_str_putc(out, ' ')) < 0) + return error; + + if (summary_len && + (error = git_str_put(out, summary, summary_len)) < 0) + return error; + + return git_str_putc(out, '\n'); +} + +static int append_header( + git_str *out, + size_t patch_idx, + size_t patch_count, + const git_oid *commit_id, + const char *summary, + const git_signature *author, + git_email_create_options *opts) +{ + char id[GIT_OID_MAX_HEXSIZE + 1]; + int error; + + git_oid_tostr(id, GIT_OID_MAX_HEXSIZE + 1, commit_id); + + if ((error = git_str_printf(out, "From %s %s\n", id, EMAIL_TIMESTAMP)) < 0 || + (error = git_str_printf(out, "From: %s <%s>\n", author->name, author->email)) < 0 || + (error = append_date(out, &author->when)) < 0 || + (error = append_subject(out, patch_idx, patch_count, summary, opts)) < 0) + return error; + + if ((error = git_str_putc(out, '\n')) < 0) + return error; + + return 0; +} + +static int append_body(git_str *out, const char *body) +{ + size_t body_len; + int error; + + if (!body) + return 0; + + body_len = strlen(body); + + if ((error = git_str_puts(out, body)) < 0) + return error; + + if (body_len && body[body_len - 1] != '\n') + error = git_str_putc(out, '\n'); + + return error; +} + +static int append_diffstat(git_str *out, git_diff *diff) +{ + git_diff_stats *stats = NULL; + unsigned int format_flags; + int error; + + format_flags = GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY; + + if ((error = git_diff_get_stats(&stats, diff)) == 0 && + (error = git_diff__stats_to_buf(out, stats, format_flags, 0)) == 0) + error = git_str_putc(out, '\n'); + + git_diff_stats_free(stats); + return error; +} + +static int append_patches(git_str *out, git_diff *diff) +{ + size_t i, deltas; + int error = 0; + + deltas = git_diff_num_deltas(diff); + + for (i = 0; i < deltas; ++i) { + git_patch *patch = NULL; + + if ((error = git_patch_from_diff(&patch, diff, i)) >= 0) + error = git_patch__to_buf(out, patch); + + git_patch_free(patch); + + if (error < 0) + break; + } + + return error; +} + +int git_email__append_from_diff( + git_str *out, + git_diff *diff, + size_t patch_idx, + size_t patch_count, + const git_oid *commit_id, + const char *summary, + const char *body, + const git_signature *author, + const git_email_create_options *given_opts) +{ + git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(diff); + GIT_ASSERT_ARG(!patch_idx || patch_idx <= patch_count); + GIT_ASSERT_ARG(commit_id); + GIT_ASSERT_ARG(author); + + GIT_ERROR_CHECK_VERSION(given_opts, + GIT_EMAIL_CREATE_OPTIONS_VERSION, + "git_email_create_options"); + + if (given_opts) + memcpy(&opts, given_opts, sizeof(git_email_create_options)); + + if ((error = append_header(out, patch_idx, patch_count, commit_id, summary, author, &opts)) == 0 && + (error = append_body(out, body)) == 0 && + (error = git_str_puts(out, "---\n")) == 0 && + (error = append_diffstat(out, diff)) == 0 && + (error = append_patches(out, diff)) == 0) + error = git_str_puts(out, "--\nlibgit2 " LIBGIT2_VERSION "\n\n"); + + return error; +} + +int git_email_create_from_diff( + git_buf *out, + git_diff *diff, + size_t patch_idx, + size_t patch_count, + const git_oid *commit_id, + const char *summary, + const char *body, + const git_signature *author, + const git_email_create_options *given_opts) +{ + git_str email = GIT_STR_INIT; + int error; + + git_buf_tostr(&email, out); + + error = git_email__append_from_diff(&email, diff, patch_idx, + patch_count, commit_id, summary, body, author, + given_opts); + + if (error == 0) + error = git_buf_fromstr(out, &email); + + git_str_dispose(&email); + return error; +} + +int git_email_create_from_commit( + git_buf *out, + git_commit *commit, + const git_email_create_options *given_opts) +{ + git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT; + git_diff *diff = NULL; + git_repository *repo; + git_diff_options *diff_opts; + git_diff_find_options *find_opts; + const git_signature *author; + const char *summary, *body; + const git_oid *commit_id; + int error = -1; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(commit); + + GIT_ERROR_CHECK_VERSION(given_opts, + GIT_EMAIL_CREATE_OPTIONS_VERSION, + "git_email_create_options"); + + if (given_opts) + memcpy(&opts, given_opts, sizeof(git_email_create_options)); + + repo = git_commit_owner(commit); + author = git_commit_author(commit); + summary = git_commit_summary(commit); + body = git_commit_body(commit); + commit_id = git_commit_id(commit); + diff_opts = &opts.diff_opts; + find_opts = &opts.diff_find_opts; + + if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0) + goto done; + + if ((opts.flags & GIT_EMAIL_CREATE_NO_RENAMES) == 0 && + (error = git_diff_find_similar(diff, find_opts)) < 0) + goto done; + + error = git_email_create_from_diff(out, diff, 1, 1, commit_id, summary, body, author, &opts); + +done: + git_diff_free(diff); + return error; +} diff --git a/src/libgit2/email.h b/src/libgit2/email.h new file mode 100644 index 00000000000..083e56d5c45 --- /dev/null +++ b/src/libgit2/email.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_email_h__ +#define INCLUDE_email_h__ + +#include "common.h" + +#include "git2/email.h" + +extern int git_email__append_from_diff( + git_str *out, + git_diff *diff, + size_t patch_idx, + size_t patch_count, + const git_oid *commit_id, + const char *summary, + const char *body, + const git_signature *author, + const git_email_create_options *given_opts); + +#endif diff --git a/src/libgit2/experimental.h.in b/src/libgit2/experimental.h.in new file mode 100644 index 00000000000..25fb14b9d17 --- /dev/null +++ b/src/libgit2/experimental.h.in @@ -0,0 +1,13 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_experimental_h__ +#define INCLUDE_experimental_h__ + +#cmakedefine GIT_EXPERIMENTAL_SHA256 1 + +#endif diff --git a/src/libgit2/fetch.c b/src/libgit2/fetch.c new file mode 100644 index 00000000000..3769f951176 --- /dev/null +++ b/src/libgit2/fetch.c @@ -0,0 +1,241 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "fetch.h" + +#include "git2/oid.h" +#include "git2/refs.h" +#include "git2/revwalk.h" +#include "git2/transport.h" +#include "git2/sys/remote.h" + +#include "oid.h" +#include "remote.h" +#include "refspec.h" +#include "pack.h" +#include "repository.h" +#include "refs.h" +#include "transports/smart.h" + +static int maybe_want(git_remote *remote, git_remote_head *head, git_refspec *tagspec, git_remote_autotag_option_t tagopt) +{ + int match = 0, valid; + + if (git_reference_name_is_valid(&valid, head->name) < 0) + return -1; + + if (!valid) + return 0; + + if (tagopt == GIT_REMOTE_DOWNLOAD_TAGS_ALL) { + /* + * If tagopt is --tags, always request tags + * in addition to the remote's refspecs + */ + if (git_refspec_src_matches(tagspec, head->name)) + match = 1; + } + + if (!match && git_remote__matching_refspec(remote, head->name)) + match = 1; + + if (!match) + return 0; + + return git_vector_insert(&remote->refs, head); +} + +static int mark_local(git_remote *remote) +{ + git_remote_head *head; + git_odb *odb; + size_t i; + + if (git_repository_odb__weakptr(&odb, remote->repo) < 0) + return -1; + + git_vector_foreach(&remote->refs, i, head) { + /* If we have the object, mark it so we don't ask for it. + However if we are unshallowing or changing history + depth, we need to ask for it even though the head + exists locally. */ + if (remote->nego.depth == GIT_FETCH_DEPTH_FULL && + git_odb_exists(odb, &head->oid)) + head->local = 1; + else + remote->need_pack = 1; + } + + return 0; +} + +static int maybe_want_oid(git_remote *remote, git_refspec *spec) +{ + git_remote_head *oid_head; + + oid_head = git__calloc(1, sizeof(git_remote_head)); + GIT_ERROR_CHECK_ALLOC(oid_head); + + git_oid_from_string(&oid_head->oid, spec->src, remote->repo->oid_type); + + if (spec->dst) { + oid_head->name = git__strdup(spec->dst); + GIT_ERROR_CHECK_ALLOC(oid_head->name); + } + + if (git_vector_insert(&remote->local_heads, oid_head) < 0 || + git_vector_insert(&remote->refs, oid_head) < 0) + return -1; + + return 0; +} + +static int filter_wants(git_remote *remote, const git_fetch_options *opts) +{ + git_remote_head **heads; + git_refspec tagspec, head, *spec; + int error = 0; + size_t i, heads_len; + unsigned int remote_caps; + unsigned int oid_mask = GIT_REMOTE_CAPABILITY_TIP_OID | + GIT_REMOTE_CAPABILITY_REACHABLE_OID; + git_remote_autotag_option_t tagopt = remote->download_tags; + + if (opts && opts->download_tags != GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED) + tagopt = opts->download_tags; + + git_vector_clear(&remote->refs); + if ((error = git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true)) < 0) + return error; + + /* + * The fetch refspec can be NULL, and what this means is that the + * user didn't specify one. This is fine, as it means that we're + * not interested in any particular branch but just the remote's + * HEAD, which will be stored in FETCH_HEAD after the fetch. + */ + if (remote->active_refspecs.length == 0) { + if ((error = git_refspec__parse(&head, "HEAD", true)) < 0) + goto cleanup; + + error = git_refspec__dwim_one(&remote->active_refspecs, &head, &remote->refs); + git_refspec__dispose(&head); + + if (error < 0) + goto cleanup; + } + + if ((error = git_remote_ls((const git_remote_head ***)&heads, &heads_len, remote)) < 0 || + (error = git_remote_capabilities(&remote_caps, remote)) < 0) + goto cleanup; + + /* Handle remote heads */ + for (i = 0; i < heads_len; i++) { + if ((error = maybe_want(remote, heads[i], &tagspec, tagopt)) < 0) + goto cleanup; + } + + /* Handle explicitly specified OID specs */ + git_vector_foreach(&remote->active_refspecs, i, spec) { + if (!git_oid__is_hexstr(spec->src, remote->repo->oid_type)) + continue; + + if (!(remote_caps & oid_mask)) { + git_error_set(GIT_ERROR_INVALID, "cannot fetch a specific object from the remote repository"); + error = -1; + goto cleanup; + } + + if ((error = maybe_want_oid(remote, spec)) < 0) + goto cleanup; + } + + error = mark_local(remote); + +cleanup: + git_refspec__dispose(&tagspec); + + return error; +} + +/* + * In this first version, we push all our refs in and start sending + * them out. When we get an ACK we hide that commit and continue + * traversing until we're done + */ +int git_fetch_negotiate(git_remote *remote, const git_fetch_options *opts) +{ + git_transport *t = remote->transport; + int error; + + remote->need_pack = 0; + + if (opts) { + GIT_ASSERT_ARG(opts->depth >= 0); + remote->nego.depth = opts->depth; + } + + if (filter_wants(remote, opts) < 0) + return -1; + + /* Don't try to negotiate when we don't want anything */ + if (!remote->need_pack) + return 0; + + /* + * Now we have everything set up so we can start tell the + * server what we want and what we have. + */ + remote->nego.refs = (const git_remote_head * const *)remote->refs.contents; + remote->nego.refs_len = remote->refs.length; + + if (git_repository__shallow_roots(&remote->nego.shallow_roots, + &remote->nego.shallow_roots_len, + remote->repo) < 0) + return -1; + + error = t->negotiate_fetch(t, + remote->repo, + &remote->nego); + + git__free(remote->nego.shallow_roots); + + return error; +} + +int git_fetch_download_pack(git_remote *remote) +{ + git_oidarray shallow_roots = { NULL }; + git_transport *t = remote->transport; + int error; + + if (!remote->need_pack) + return 0; + + if ((error = t->download_pack(t, remote->repo, &remote->stats)) != 0 || + (error = t->shallow_roots(&shallow_roots, t)) != 0) + return error; + + error = git_repository__shallow_roots_write(remote->repo, &shallow_roots); + + git_oidarray_dispose(&shallow_roots); + return error; +} + +int git_fetch_options_init(git_fetch_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_fetch_options, GIT_FETCH_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_fetch_init_options(git_fetch_options *opts, unsigned int version) +{ + return git_fetch_options_init(opts, version); +} +#endif diff --git a/src/libgit2/fetch.h b/src/libgit2/fetch.h new file mode 100644 index 00000000000..493366dedf1 --- /dev/null +++ b/src/libgit2/fetch.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_fetch_h__ +#define INCLUDE_fetch_h__ + +#include "common.h" + +#include "git2/remote.h" + +int git_fetch_negotiate(git_remote *remote, const git_fetch_options *opts); + +int git_fetch_download_pack(git_remote *remote); + +int git_fetch_setup_walk(git_revwalk **out, git_repository *repo); + +#endif diff --git a/src/libgit2/fetchhead.c b/src/libgit2/fetchhead.c new file mode 100644 index 00000000000..1555d1cd9e5 --- /dev/null +++ b/src/libgit2/fetchhead.c @@ -0,0 +1,340 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "fetchhead.h" + +#include "git2/types.h" +#include "git2/oid.h" + +#include "str.h" +#include "futils.h" +#include "filebuf.h" +#include "refs.h" +#include "net.h" +#include "repository.h" + +int git_fetchhead_ref_cmp(const void *a, const void *b) +{ + const git_fetchhead_ref *one = (const git_fetchhead_ref *)a; + const git_fetchhead_ref *two = (const git_fetchhead_ref *)b; + + if (one->is_merge && !two->is_merge) + return -1; + if (two->is_merge && !one->is_merge) + return 1; + + if (one->ref_name && two->ref_name) + return strcmp(one->ref_name, two->ref_name); + else if (one->ref_name) + return -1; + else if (two->ref_name) + return 1; + + return 0; +} + +static char *sanitized_remote_url(const char *remote_url) +{ + git_net_url url = GIT_NET_URL_INIT; + char *sanitized = NULL; + int error; + + if (git_net_url_parse(&url, remote_url) == 0) { + git_str buf = GIT_STR_INIT; + + git__free(url.username); + git__free(url.password); + url.username = url.password = NULL; + + if ((error = git_net_url_fmt(&buf, &url)) < 0) + goto fallback; + + sanitized = git_str_detach(&buf); + } + +fallback: + if (!sanitized) + sanitized = git__strdup(remote_url); + + git_net_url_dispose(&url); + return sanitized; +} + +int git_fetchhead_ref_create( + git_fetchhead_ref **out, + git_oid *oid, + unsigned int is_merge, + const char *ref_name, + const char *remote_url) +{ + git_fetchhead_ref *fetchhead_ref; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(oid); + + *out = NULL; + + fetchhead_ref = git__malloc(sizeof(git_fetchhead_ref)); + GIT_ERROR_CHECK_ALLOC(fetchhead_ref); + + memset(fetchhead_ref, 0x0, sizeof(git_fetchhead_ref)); + + git_oid_cpy(&fetchhead_ref->oid, oid); + fetchhead_ref->is_merge = is_merge; + + if (ref_name) { + fetchhead_ref->ref_name = git__strdup(ref_name); + GIT_ERROR_CHECK_ALLOC(fetchhead_ref->ref_name); + } + + if (remote_url) { + fetchhead_ref->remote_url = sanitized_remote_url(remote_url); + GIT_ERROR_CHECK_ALLOC(fetchhead_ref->remote_url); + } + + *out = fetchhead_ref; + + return 0; +} + +static int fetchhead_ref_write( + git_filebuf *file, + git_fetchhead_ref *fetchhead_ref) +{ + char oid[GIT_OID_MAX_HEXSIZE + 1]; + const char *type = NULL, *name = NULL; + int head = 0; + + GIT_ASSERT_ARG(file); + GIT_ASSERT_ARG(fetchhead_ref); + + git_oid_tostr(oid, GIT_OID_MAX_HEXSIZE + 1, &fetchhead_ref->oid); + + if (git__prefixcmp(fetchhead_ref->ref_name, GIT_REFS_HEADS_DIR) == 0) { + type = "branch "; + name = fetchhead_ref->ref_name + strlen(GIT_REFS_HEADS_DIR); + } else if(git__prefixcmp(fetchhead_ref->ref_name, + GIT_REFS_TAGS_DIR) == 0) { + type = "tag "; + name = fetchhead_ref->ref_name + strlen(GIT_REFS_TAGS_DIR); + } else if (!git__strcmp(fetchhead_ref->ref_name, GIT_HEAD_FILE)) { + head = 1; + } else { + type = ""; + name = fetchhead_ref->ref_name; + } + + if (head) + return git_filebuf_printf(file, "%s\t\t%s\n", oid, fetchhead_ref->remote_url); + + return git_filebuf_printf(file, "%s\t%s\t%s'%s' of %s\n", + oid, + (fetchhead_ref->is_merge) ? "" : "not-for-merge", + type, + name, + fetchhead_ref->remote_url); +} + +int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs) +{ + git_filebuf file = GIT_FILEBUF_INIT; + git_str path = GIT_STR_INIT; + unsigned int i; + git_fetchhead_ref *fetchhead_ref; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(fetchhead_refs); + + if (git_str_joinpath(&path, repo->gitdir, GIT_FETCH_HEAD_FILE) < 0) + return -1; + + if (git_filebuf_open(&file, path.ptr, GIT_FILEBUF_APPEND, GIT_REFS_FILE_MODE) < 0) { + git_str_dispose(&path); + return -1; + } + + git_str_dispose(&path); + + git_vector_sort(fetchhead_refs); + + git_vector_foreach(fetchhead_refs, i, fetchhead_ref) + fetchhead_ref_write(&file, fetchhead_ref); + + return git_filebuf_commit(&file); +} + +static int fetchhead_ref_parse( + git_oid *oid, + unsigned int *is_merge, + git_str *ref_name, + const char **remote_url, + char *line, + size_t line_num, + git_oid_t oid_type) +{ + char *oid_str, *is_merge_str, *desc, *name = NULL; + const char *type = NULL; + int error = 0; + + *remote_url = NULL; + + if (!*line) { + git_error_set(GIT_ERROR_FETCHHEAD, + "empty line in FETCH_HEAD line %"PRIuZ, line_num); + return -1; + } + + /* Compat with old git clients that wrote FETCH_HEAD like a loose ref. */ + if ((oid_str = git__strsep(&line, "\t")) == NULL) { + oid_str = line; + line += strlen(line); + + *is_merge = 1; + } + + if (strlen(oid_str) != git_oid_hexsize(oid_type)) { + git_error_set(GIT_ERROR_FETCHHEAD, + "invalid object ID in FETCH_HEAD line %"PRIuZ, line_num); + return -1; + } + + if (git_oid_from_string(oid, oid_str, oid_type) < 0) { + const git_error *oid_err = git_error_last(); + const char *err_msg = oid_err ? oid_err->message : "invalid object ID"; + + git_error_set(GIT_ERROR_FETCHHEAD, "%s in FETCH_HEAD line %"PRIuZ, + err_msg, line_num); + return -1; + } + + /* Parse new data from newer git clients */ + if (*line) { + if ((is_merge_str = git__strsep(&line, "\t")) == NULL) { + git_error_set(GIT_ERROR_FETCHHEAD, + "invalid description data in FETCH_HEAD line %"PRIuZ, line_num); + return -1; + } + + if (*is_merge_str == '\0') + *is_merge = 1; + else if (strcmp(is_merge_str, "not-for-merge") == 0) + *is_merge = 0; + else { + git_error_set(GIT_ERROR_FETCHHEAD, + "invalid for-merge entry in FETCH_HEAD line %"PRIuZ, line_num); + return -1; + } + + if ((desc = line) == NULL) { + git_error_set(GIT_ERROR_FETCHHEAD, + "invalid description in FETCH_HEAD line %"PRIuZ, line_num); + return -1; + } + + if (git__prefixcmp(desc, "branch '") == 0) { + type = GIT_REFS_HEADS_DIR; + name = desc + 8; + } else if (git__prefixcmp(desc, "tag '") == 0) { + type = GIT_REFS_TAGS_DIR; + name = desc + 5; + } else if (git__prefixcmp(desc, "'") == 0) + name = desc + 1; + + if (name) { + if ((desc = strstr(name, "' ")) == NULL || + git__prefixcmp(desc, "' of ") != 0) { + git_error_set(GIT_ERROR_FETCHHEAD, + "invalid description in FETCH_HEAD line %"PRIuZ, line_num); + return -1; + } + + *desc = '\0'; + desc += 5; + } + + *remote_url = desc; + } + + git_str_clear(ref_name); + + if (type) + git_str_join(ref_name, '/', type, name); + else if(name) + git_str_puts(ref_name, name); + + return error; +} + +int git_repository_fetchhead_foreach( + git_repository *repo, + git_repository_fetchhead_foreach_cb cb, + void *payload) +{ + git_str path = GIT_STR_INIT, file = GIT_STR_INIT, name = GIT_STR_INIT; + const char *ref_name; + git_oid oid; + const char *remote_url; + unsigned int is_merge = 0; + char *buffer, *line; + size_t line_num = 0; + int error = 0; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(cb); + + if (git_str_joinpath(&path, repo->gitdir, GIT_FETCH_HEAD_FILE) < 0) + return -1; + + if ((error = git_futils_readbuffer(&file, git_str_cstr(&path))) < 0) + goto done; + + buffer = file.ptr; + + while ((line = git__strsep(&buffer, "\n")) != NULL) { + ++line_num; + + if ((error = fetchhead_ref_parse(&oid, &is_merge, &name, + &remote_url, line, line_num, + repo->oid_type)) < 0) + goto done; + + if (git_str_len(&name) > 0) + ref_name = git_str_cstr(&name); + else + ref_name = NULL; + + error = cb(ref_name, remote_url, &oid, is_merge, payload); + if (error) { + git_error_set_after_callback(error); + goto done; + } + } + + if (*buffer) { + git_error_set(GIT_ERROR_FETCHHEAD, "no EOL at line %"PRIuZ, line_num+1); + error = -1; + goto done; + } + +done: + git_str_dispose(&file); + git_str_dispose(&path); + git_str_dispose(&name); + + return error; +} + +void git_fetchhead_ref_free(git_fetchhead_ref *fetchhead_ref) +{ + if (fetchhead_ref == NULL) + return; + + git__free(fetchhead_ref->remote_url); + git__free(fetchhead_ref->ref_name); + git__free(fetchhead_ref); +} + diff --git a/src/fetchhead.h b/src/libgit2/fetchhead.h similarity index 88% rename from src/fetchhead.h rename to src/libgit2/fetchhead.h index 74fce049b20..9e51710108b 100644 --- a/src/fetchhead.h +++ b/src/libgit2/fetchhead.h @@ -7,16 +7,17 @@ #ifndef INCLUDE_fetchhead_h__ #define INCLUDE_fetchhead_h__ +#include "common.h" + +#include "oid.h" #include "vector.h" -struct git_fetchhead_ref { +typedef struct git_fetchhead_ref { git_oid oid; unsigned int is_merge; char *ref_name; char *remote_url; -}; - -typedef struct git_fetchhead_ref git_fetchhead_ref; +} git_fetchhead_ref; int git_fetchhead_ref_create( git_fetchhead_ref **fetchhead_ref_out, diff --git a/src/libgit2/filter.c b/src/libgit2/filter.c new file mode 100644 index 00000000000..9e0910c8c26 --- /dev/null +++ b/src/libgit2/filter.c @@ -0,0 +1,1221 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "filter.h" + +#include "buf.h" +#include "common.h" +#include "futils.h" +#include "hash.h" +#include "repository.h" +#include "runtime.h" +#include "git2/sys/filter.h" +#include "git2/config.h" +#include "blob.h" +#include "attr_file.h" +#include "array.h" +#include "path.h" + +struct git_filter_source { + git_repository *repo; + const char *path; + git_oid oid; /* zero if unknown (which is likely) */ + uint16_t filemode; /* zero if unknown */ + git_filter_mode_t mode; + git_filter_options options; +}; + +typedef struct { + const char *filter_name; + git_filter *filter; + void *payload; +} git_filter_entry; + +struct git_filter_list { + git_array_t(git_filter_entry) filters; + git_filter_source source; + git_str *temp_buf; + char path[GIT_FLEX_ARRAY]; +}; + +typedef struct { + char *filter_name; + git_filter *filter; + int priority; + int initialized; + size_t nattrs, nmatches; + char *attrdata; + const char *attrs[GIT_FLEX_ARRAY]; +} git_filter_def; + +static int filter_def_priority_cmp(const void *a, const void *b) +{ + int pa = ((const git_filter_def *)a)->priority; + int pb = ((const git_filter_def *)b)->priority; + return (pa < pb) ? -1 : (pa > pb) ? 1 : 0; +} + +struct git_filter_registry { + git_rwlock lock; + git_vector filters; +}; + +static struct git_filter_registry filter_registry; + +static void git_filter_global_shutdown(void); + + +static int filter_def_scan_attrs( + git_str *attrs, size_t *nattr, size_t *nmatch, const char *attr_str) +{ + const char *start, *scan = attr_str; + int has_eq; + + *nattr = *nmatch = 0; + + if (!scan) + return 0; + + while (*scan) { + while (git__isspace(*scan)) scan++; + + for (start = scan, has_eq = 0; *scan && !git__isspace(*scan); ++scan) { + if (*scan == '=') + has_eq = 1; + } + + if (scan > start) { + (*nattr)++; + if (has_eq || *start == '-' || *start == '+' || *start == '!') + (*nmatch)++; + + if (has_eq) + git_str_putc(attrs, '='); + git_str_put(attrs, start, scan - start); + git_str_putc(attrs, '\0'); + } + } + + return 0; +} + +static void filter_def_set_attrs(git_filter_def *fdef) +{ + char *scan = fdef->attrdata; + size_t i; + + for (i = 0; i < fdef->nattrs; ++i) { + const char *name, *value; + + switch (*scan) { + case '=': + name = scan + 1; + for (scan++; *scan != '='; scan++) /* find '=' */; + *scan++ = '\0'; + value = scan; + break; + case '-': + name = scan + 1; value = git_attr__false; break; + case '+': + name = scan + 1; value = git_attr__true; break; + case '!': + name = scan + 1; value = git_attr__unset; break; + default: + name = scan; value = NULL; break; + } + + fdef->attrs[i] = name; + fdef->attrs[i + fdef->nattrs] = value; + + scan += strlen(scan) + 1; + } +} + +static int filter_def_name_key_check(const void *key, const void *fdef) +{ + const char *name = + fdef ? ((const git_filter_def *)fdef)->filter_name : NULL; + return name ? git__strcmp(key, name) : -1; +} + +static int filter_def_filter_key_check(const void *key, const void *fdef) +{ + const void *filter = fdef ? ((const git_filter_def *)fdef)->filter : NULL; + return (key == filter) ? 0 : -1; +} + +/* Note: callers must lock the registry before calling this function */ +static int filter_registry_insert( + const char *name, git_filter *filter, int priority) +{ + git_filter_def *fdef; + size_t nattr = 0, nmatch = 0, alloc_len; + git_str attrs = GIT_STR_INIT; + + if (filter_def_scan_attrs(&attrs, &nattr, &nmatch, filter->attributes) < 0) + return -1; + + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_len, nattr, 2); + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_len, alloc_len, sizeof(char *)); + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, sizeof(git_filter_def)); + + fdef = git__calloc(1, alloc_len); + GIT_ERROR_CHECK_ALLOC(fdef); + + fdef->filter_name = git__strdup(name); + GIT_ERROR_CHECK_ALLOC(fdef->filter_name); + + fdef->filter = filter; + fdef->priority = priority; + fdef->nattrs = nattr; + fdef->nmatches = nmatch; + fdef->attrdata = git_str_detach(&attrs); + + filter_def_set_attrs(fdef); + + if (git_vector_insert(&filter_registry.filters, fdef) < 0) { + git__free(fdef->filter_name); + git__free(fdef->attrdata); + git__free(fdef); + return -1; + } + + git_vector_sort(&filter_registry.filters); + return 0; +} + +int git_filter_global_init(void) +{ + git_filter *crlf = NULL, *ident = NULL; + int error = 0; + + if (git_rwlock_init(&filter_registry.lock) < 0) + return -1; + + if ((error = git_vector_init(&filter_registry.filters, 2, + filter_def_priority_cmp)) < 0) + goto done; + + if ((crlf = git_crlf_filter_new()) == NULL || + filter_registry_insert( + GIT_FILTER_CRLF, crlf, GIT_FILTER_CRLF_PRIORITY) < 0 || + (ident = git_ident_filter_new()) == NULL || + filter_registry_insert( + GIT_FILTER_IDENT, ident, GIT_FILTER_IDENT_PRIORITY) < 0) + error = -1; + + if (!error) + error = git_runtime_shutdown_register(git_filter_global_shutdown); + +done: + if (error) { + git_filter_free(crlf); + git_filter_free(ident); + } + + return error; +} + +static void git_filter_global_shutdown(void) +{ + size_t pos; + git_filter_def *fdef; + + if (git_rwlock_wrlock(&filter_registry.lock) < 0) + return; + + git_vector_foreach(&filter_registry.filters, pos, fdef) { + if (fdef->filter && fdef->filter->shutdown) { + fdef->filter->shutdown(fdef->filter); + fdef->initialized = false; + } + + git__free(fdef->filter_name); + git__free(fdef->attrdata); + git__free(fdef); + } + + git_vector_dispose(&filter_registry.filters); + + git_rwlock_wrunlock(&filter_registry.lock); + git_rwlock_free(&filter_registry.lock); +} + +/* Note: callers must lock the registry before calling this function */ +static int filter_registry_find(size_t *pos, const char *name) +{ + return git_vector_search2( + pos, &filter_registry.filters, filter_def_name_key_check, name); +} + +/* Note: callers must lock the registry before calling this function */ +static git_filter_def *filter_registry_lookup(size_t *pos, const char *name) +{ + git_filter_def *fdef = NULL; + + if (!filter_registry_find(pos, name)) + fdef = git_vector_get(&filter_registry.filters, *pos); + + return fdef; +} + + +int git_filter_register( + const char *name, git_filter *filter, int priority) +{ + int error; + + GIT_ASSERT_ARG(name); + GIT_ASSERT_ARG(filter); + + if (git_rwlock_wrlock(&filter_registry.lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock filter registry"); + return -1; + } + + if (!filter_registry_find(NULL, name)) { + git_error_set( + GIT_ERROR_FILTER, "attempt to reregister existing filter '%s'", name); + error = GIT_EEXISTS; + goto done; + } + + error = filter_registry_insert(name, filter, priority); + +done: + git_rwlock_wrunlock(&filter_registry.lock); + return error; +} + +int git_filter_unregister(const char *name) +{ + size_t pos; + git_filter_def *fdef; + int error = 0; + + GIT_ASSERT_ARG(name); + + /* cannot unregister default filters */ + if (!strcmp(GIT_FILTER_CRLF, name) || !strcmp(GIT_FILTER_IDENT, name)) { + git_error_set(GIT_ERROR_FILTER, "cannot unregister filter '%s'", name); + return -1; + } + + if (git_rwlock_wrlock(&filter_registry.lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock filter registry"); + return -1; + } + + if ((fdef = filter_registry_lookup(&pos, name)) == NULL) { + git_error_set(GIT_ERROR_FILTER, "cannot find filter '%s' to unregister", name); + error = GIT_ENOTFOUND; + goto done; + } + + git_vector_remove(&filter_registry.filters, pos); + + if (fdef->initialized && fdef->filter && fdef->filter->shutdown) { + fdef->filter->shutdown(fdef->filter); + fdef->initialized = false; + } + + git__free(fdef->filter_name); + git__free(fdef->attrdata); + git__free(fdef); + +done: + git_rwlock_wrunlock(&filter_registry.lock); + return error; +} + +static int filter_initialize(git_filter_def *fdef) +{ + int error = 0; + + if (!fdef->initialized && fdef->filter && fdef->filter->initialize) { + if ((error = fdef->filter->initialize(fdef->filter)) < 0) + return error; + } + + fdef->initialized = true; + return 0; +} + +git_filter *git_filter_lookup(const char *name) +{ + size_t pos; + git_filter_def *fdef; + git_filter *filter = NULL; + + if (git_rwlock_rdlock(&filter_registry.lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock filter registry"); + return NULL; + } + + if ((fdef = filter_registry_lookup(&pos, name)) == NULL || + (!fdef->initialized && filter_initialize(fdef) < 0)) + goto done; + + filter = fdef->filter; + +done: + git_rwlock_rdunlock(&filter_registry.lock); + return filter; +} + +void git_filter_free(git_filter *filter) +{ + git__free(filter); +} + +git_repository *git_filter_source_repo(const git_filter_source *src) +{ + return src->repo; +} + +const char *git_filter_source_path(const git_filter_source *src) +{ + return src->path; +} + +uint16_t git_filter_source_filemode(const git_filter_source *src) +{ + return src->filemode; +} + +const git_oid *git_filter_source_id(const git_filter_source *src) +{ + return git_oid_is_zero(&src->oid) ? NULL : &src->oid; +} + +git_filter_mode_t git_filter_source_mode(const git_filter_source *src) +{ + return src->mode; +} + +uint32_t git_filter_source_flags(const git_filter_source *src) +{ + return src->options.flags; +} + +static int filter_list_new( + git_filter_list **out, const git_filter_source *src) +{ + git_filter_list *fl = NULL; + size_t pathlen = src->path ? strlen(src->path) : 0, alloclen; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_filter_list), pathlen); + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); + + fl = git__calloc(1, alloclen); + GIT_ERROR_CHECK_ALLOC(fl); + + if (src->path) + memcpy(fl->path, src->path, pathlen); + fl->source.repo = src->repo; + fl->source.path = fl->path; + fl->source.mode = src->mode; + + memcpy(&fl->source.options, &src->options, sizeof(git_filter_options)); + + *out = fl; + return 0; +} + +static int filter_list_check_attributes( + const char ***out, + git_repository *repo, + git_filter_session *filter_session, + git_filter_def *fdef, + const git_filter_source *src) +{ + const char **strs = git__calloc(fdef->nattrs, sizeof(const char *)); + git_attr_options attr_opts = GIT_ATTR_OPTIONS_INIT; + size_t i; + int error; + + GIT_ERROR_CHECK_ALLOC(strs); + + if ((src->options.flags & GIT_FILTER_NO_SYSTEM_ATTRIBUTES) != 0) + attr_opts.flags |= GIT_ATTR_CHECK_NO_SYSTEM; + + if ((src->options.flags & GIT_FILTER_ATTRIBUTES_FROM_HEAD) != 0) + attr_opts.flags |= GIT_ATTR_CHECK_INCLUDE_HEAD; + + if ((src->options.flags & GIT_FILTER_ATTRIBUTES_FROM_COMMIT) != 0) { + attr_opts.flags |= GIT_ATTR_CHECK_INCLUDE_COMMIT; + +#ifndef GIT_DEPRECATE_HARD + if (src->options.commit_id) + git_oid_cpy(&attr_opts.attr_commit_id, src->options.commit_id); + else +#endif + git_oid_cpy(&attr_opts.attr_commit_id, &src->options.attr_commit_id); + } + + error = git_attr_get_many_with_session( + strs, repo, filter_session->attr_session, &attr_opts, src->path, fdef->nattrs, fdef->attrs); + + /* if no values were found but no matches are needed, it's okay! */ + if (error == GIT_ENOTFOUND && !fdef->nmatches) { + git_error_clear(); + git__free((void *)strs); + return 0; + } + + for (i = 0; !error && i < fdef->nattrs; ++i) { + const char *want = fdef->attrs[fdef->nattrs + i]; + git_attr_value_t want_type, found_type; + + if (!want) + continue; + + want_type = git_attr_value(want); + found_type = git_attr_value(strs[i]); + + if (want_type != found_type) + error = GIT_ENOTFOUND; + else if (want_type == GIT_ATTR_VALUE_STRING && + strcmp(want, strs[i]) && + strcmp(want, "*")) + error = GIT_ENOTFOUND; + } + + if (error) + git__free((void *)strs); + else + *out = strs; + + return error; +} + +int git_filter_list_new( + git_filter_list **out, + git_repository *repo, + git_filter_mode_t mode, + uint32_t flags) +{ + git_filter_source src = { 0 }; + src.repo = repo; + src.path = NULL; + src.mode = mode; + src.options.flags = flags; + return filter_list_new(out, &src); +} + +int git_filter_list__load( + git_filter_list **filters, + git_repository *repo, + git_blob *blob, /* can be NULL */ + const char *path, + git_filter_mode_t mode, + git_filter_session *filter_session) +{ + int error = 0; + git_filter_list *fl = NULL; + git_filter_source src = { 0 }; + git_filter_entry *fe; + size_t idx; + git_filter_def *fdef; + + if (git_rwlock_rdlock(&filter_registry.lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock filter registry"); + return -1; + } + + src.repo = repo; + src.path = path; + src.mode = mode; + + memcpy(&src.options, &filter_session->options, sizeof(git_filter_options)); + + if (blob) + git_oid_cpy(&src.oid, git_blob_id(blob)); + + git_vector_foreach(&filter_registry.filters, idx, fdef) { + const char **values = NULL; + void *payload = NULL; + + if (!fdef || !fdef->filter) + continue; + + if (fdef->nattrs > 0) { + error = filter_list_check_attributes( + &values, repo, + filter_session, fdef, &src); + + if (error == GIT_ENOTFOUND) { + error = 0; + continue; + } else if (error < 0) + break; + } + + if (!fdef->initialized && (error = filter_initialize(fdef)) < 0) + break; + + if (fdef->filter->check) + error = fdef->filter->check( + fdef->filter, &payload, &src, values); + + git__free((void *)values); + + if (error == GIT_PASSTHROUGH) + error = 0; + else if (error < 0) + break; + else { + if (!fl) { + if ((error = filter_list_new(&fl, &src)) < 0) + break; + + fl->temp_buf = filter_session->temp_buf; + } + + fe = git_array_alloc(fl->filters); + GIT_ERROR_CHECK_ALLOC(fe); + + fe->filter = fdef->filter; + fe->filter_name = fdef->filter_name; + fe->payload = payload; + } + } + + git_rwlock_rdunlock(&filter_registry.lock); + + if (error && fl != NULL) { + git_array_clear(fl->filters); + git__free(fl); + fl = NULL; + } + + *filters = fl; + return error; +} + +int git_filter_list_load_ext( + git_filter_list **filters, + git_repository *repo, + git_blob *blob, /* can be NULL */ + const char *path, + git_filter_mode_t mode, + git_filter_options *opts) +{ + git_filter_session filter_session = GIT_FILTER_SESSION_INIT; + + if (opts) + memcpy(&filter_session.options, opts, sizeof(git_filter_options)); + + return git_filter_list__load( + filters, repo, blob, path, mode, &filter_session); +} + +int git_filter_list_load( + git_filter_list **filters, + git_repository *repo, + git_blob *blob, /* can be NULL */ + const char *path, + git_filter_mode_t mode, + uint32_t flags) +{ + git_filter_session filter_session = GIT_FILTER_SESSION_INIT; + + filter_session.options.flags = flags; + + return git_filter_list__load( + filters, repo, blob, path, mode, &filter_session); +} + +void git_filter_list_free(git_filter_list *fl) +{ + uint32_t i; + + if (!fl) + return; + + for (i = 0; i < git_array_size(fl->filters); ++i) { + git_filter_entry *fe = git_array_get(fl->filters, i); + if (fe->filter->cleanup) + fe->filter->cleanup(fe->filter, fe->payload); + } + + git_array_clear(fl->filters); + git__free(fl); +} + +int git_filter_list_contains( + git_filter_list *fl, + const char *name) +{ + size_t i; + + GIT_ASSERT_ARG(name); + + if (!fl) + return 0; + + for (i = 0; i < fl->filters.size; i++) { + if (strcmp(fl->filters.ptr[i].filter_name, name) == 0) + return 1; + } + + return 0; +} + +int git_filter_list_push( + git_filter_list *fl, git_filter *filter, void *payload) +{ + int error = 0; + size_t pos; + git_filter_def *fdef = NULL; + git_filter_entry *fe; + + GIT_ASSERT_ARG(fl); + GIT_ASSERT_ARG(filter); + + if (git_rwlock_rdlock(&filter_registry.lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock filter registry"); + return -1; + } + + if (git_vector_search2( + &pos, &filter_registry.filters, + filter_def_filter_key_check, filter) == 0) + fdef = git_vector_get(&filter_registry.filters, pos); + + git_rwlock_rdunlock(&filter_registry.lock); + + if (fdef == NULL) { + git_error_set(GIT_ERROR_FILTER, "cannot use an unregistered filter"); + return -1; + } + + if (!fdef->initialized && (error = filter_initialize(fdef)) < 0) + return error; + + fe = git_array_alloc(fl->filters); + GIT_ERROR_CHECK_ALLOC(fe); + fe->filter = filter; + fe->payload = payload; + + return 0; +} + +size_t git_filter_list_length(const git_filter_list *fl) +{ + return fl ? git_array_size(fl->filters) : 0; +} + +struct buf_stream { + git_writestream parent; + git_str *target; + bool complete; +}; + +static int buf_stream_write( + git_writestream *s, const char *buffer, size_t len) +{ + struct buf_stream *buf_stream = (struct buf_stream *)s; + GIT_ASSERT_ARG(buf_stream); + GIT_ASSERT(buf_stream->complete == 0); + + return git_str_put(buf_stream->target, buffer, len); +} + +static int buf_stream_close(git_writestream *s) +{ + struct buf_stream *buf_stream = (struct buf_stream *)s; + GIT_ASSERT_ARG(buf_stream); + + GIT_ASSERT(buf_stream->complete == 0); + buf_stream->complete = 1; + + return 0; +} + +static void buf_stream_free(git_writestream *s) +{ + GIT_UNUSED(s); +} + +static void buf_stream_init(struct buf_stream *writer, git_str *target) +{ + memset(writer, 0, sizeof(struct buf_stream)); + + writer->parent.write = buf_stream_write; + writer->parent.close = buf_stream_close; + writer->parent.free = buf_stream_free; + writer->target = target; + + git_str_clear(target); +} + +int git_filter_list_apply_to_buffer( + git_buf *out, + git_filter_list *filters, + const char *in, + size_t in_len) +{ + GIT_BUF_WRAP_PRIVATE(out, git_filter_list__apply_to_buffer, filters, in, in_len); +} + +int git_filter_list__apply_to_buffer( + git_str *out, + git_filter_list *filters, + const char *in, + size_t in_len) +{ + struct buf_stream writer; + int error; + + buf_stream_init(&writer, out); + + if ((error = git_filter_list_stream_buffer(filters, + in, in_len, &writer.parent)) < 0) + return error; + + GIT_ASSERT(writer.complete); + return error; +} + +int git_filter_list__convert_buf( + git_str *out, + git_filter_list *filters, + git_str *in) +{ + int error; + + if (!filters || git_filter_list_length(filters) == 0) { + git_str_swap(out, in); + git_str_dispose(in); + return 0; + } + + error = git_filter_list__apply_to_buffer(out, filters, + in->ptr, in->size); + + if (!error) + git_str_dispose(in); + + return error; +} + +int git_filter_list_apply_to_file( + git_buf *out, + git_filter_list *filters, + git_repository *repo, + const char *path) +{ + GIT_BUF_WRAP_PRIVATE(out, git_filter_list__apply_to_file, filters, repo, path); +} + +int git_filter_list__apply_to_file( + git_str *out, + git_filter_list *filters, + git_repository *repo, + const char *path) +{ + struct buf_stream writer; + int error; + + buf_stream_init(&writer, out); + + if ((error = git_filter_list_stream_file( + filters, repo, path, &writer.parent)) < 0) + return error; + + GIT_ASSERT(writer.complete); + return error; +} + +static int buf_from_blob(git_str *out, git_blob *blob) +{ + git_object_size_t rawsize = git_blob_rawsize(blob); + + if (!git__is_sizet(rawsize)) { + git_error_set(GIT_ERROR_OS, "blob is too large to filter"); + return -1; + } + + git_str_attach_notowned(out, git_blob_rawcontent(blob), (size_t)rawsize); + return 0; +} + +int git_filter_list_apply_to_blob( + git_buf *out, + git_filter_list *filters, + git_blob *blob) +{ + GIT_BUF_WRAP_PRIVATE(out, git_filter_list__apply_to_blob, filters, blob); +} + +int git_filter_list__apply_to_blob( + git_str *out, + git_filter_list *filters, + git_blob *blob) +{ + struct buf_stream writer; + int error; + + buf_stream_init(&writer, out); + + if ((error = git_filter_list_stream_blob( + filters, blob, &writer.parent)) < 0) + return error; + + GIT_ASSERT(writer.complete); + return error; +} + +struct buffered_stream { + git_writestream parent; + git_filter *filter; + int (*write_fn)(git_filter *, void **, git_str *, const git_str *, const git_filter_source *); + int (*legacy_write_fn)(git_filter *, void **, git_buf *, const git_buf *, const git_filter_source *); + const git_filter_source *source; + void **payload; + git_str input; + git_str temp_buf; + git_str *output; + git_writestream *target; +}; + +static int buffered_stream_write( + git_writestream *s, const char *buffer, size_t len) +{ + struct buffered_stream *buffered_stream = (struct buffered_stream *)s; + GIT_ASSERT_ARG(buffered_stream); + + return git_str_put(&buffered_stream->input, buffer, len); +} + +#ifndef GIT_DEPRECATE_HARD +# define BUF_TO_STRUCT(b, s) \ + (b)->ptr = (s)->ptr; \ + (b)->size = (s)->size; \ + (b)->reserved = (s)->asize; +# define STRUCT_TO_BUF(s, b) \ + (s)->ptr = (b)->ptr; \ + (s)->size = (b)->size; \ + (s)->asize = (b)->reserved; +#endif + +static int buffered_stream_close(git_writestream *s) +{ + struct buffered_stream *buffered_stream = (struct buffered_stream *)s; + git_str *writebuf; + git_error *last_error; + int error; + + GIT_ASSERT_ARG(buffered_stream); + +#ifndef GIT_DEPRECATE_HARD + if (buffered_stream->write_fn == NULL) { + git_buf legacy_output = GIT_BUF_INIT, + legacy_input = GIT_BUF_INIT; + + BUF_TO_STRUCT(&legacy_output, buffered_stream->output); + BUF_TO_STRUCT(&legacy_input, &buffered_stream->input); + + error = buffered_stream->legacy_write_fn( + buffered_stream->filter, + buffered_stream->payload, + &legacy_output, + &legacy_input, + buffered_stream->source); + + STRUCT_TO_BUF(buffered_stream->output, &legacy_output); + STRUCT_TO_BUF(&buffered_stream->input, &legacy_input); + } else +#endif + error = buffered_stream->write_fn( + buffered_stream->filter, + buffered_stream->payload, + buffered_stream->output, + &buffered_stream->input, + buffered_stream->source); + + if (error == GIT_PASSTHROUGH) { + writebuf = &buffered_stream->input; + } else if (error == 0) { + writebuf = buffered_stream->output; + } else { + /* close stream before erroring out taking care + * to preserve the original error */ + git_error_save(&last_error); + buffered_stream->target->close(buffered_stream->target); + git_error_restore(last_error); + return error; + } + + if ((error = buffered_stream->target->write( + buffered_stream->target, writebuf->ptr, writebuf->size)) == 0) + error = buffered_stream->target->close(buffered_stream->target); + + return error; +} + +static void buffered_stream_free(git_writestream *s) +{ + struct buffered_stream *buffered_stream = (struct buffered_stream *)s; + + if (buffered_stream) { + git_str_dispose(&buffered_stream->input); + git_str_dispose(&buffered_stream->temp_buf); + git__free(buffered_stream); + } +} + +int git_filter_buffered_stream_new( + git_writestream **out, + git_filter *filter, + int (*write_fn)(git_filter *, void **, git_str *, const git_str *, const git_filter_source *), + git_str *temp_buf, + void **payload, + const git_filter_source *source, + git_writestream *target) +{ + struct buffered_stream *buffered_stream = git__calloc(1, sizeof(struct buffered_stream)); + GIT_ERROR_CHECK_ALLOC(buffered_stream); + + buffered_stream->parent.write = buffered_stream_write; + buffered_stream->parent.close = buffered_stream_close; + buffered_stream->parent.free = buffered_stream_free; + buffered_stream->filter = filter; + buffered_stream->write_fn = write_fn; + buffered_stream->output = temp_buf ? temp_buf : &buffered_stream->temp_buf; + buffered_stream->payload = payload; + buffered_stream->source = source; + buffered_stream->target = target; + + if (temp_buf) + git_str_clear(temp_buf); + + *out = (git_writestream *)buffered_stream; + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +static int buffered_legacy_stream_new( + git_writestream **out, + git_filter *filter, + int (*legacy_write_fn)(git_filter *, void **, git_buf *, const git_buf *, const git_filter_source *), + git_str *temp_buf, + void **payload, + const git_filter_source *source, + git_writestream *target) +{ + struct buffered_stream *buffered_stream = git__calloc(1, sizeof(struct buffered_stream)); + GIT_ERROR_CHECK_ALLOC(buffered_stream); + + buffered_stream->parent.write = buffered_stream_write; + buffered_stream->parent.close = buffered_stream_close; + buffered_stream->parent.free = buffered_stream_free; + buffered_stream->filter = filter; + buffered_stream->legacy_write_fn = legacy_write_fn; + buffered_stream->output = temp_buf ? temp_buf : &buffered_stream->temp_buf; + buffered_stream->payload = payload; + buffered_stream->source = source; + buffered_stream->target = target; + + if (temp_buf) + git_str_clear(temp_buf); + + *out = (git_writestream *)buffered_stream; + return 0; +} +#endif + +static int setup_stream( + git_writestream **out, + git_filter_entry *fe, + git_filter_list *filters, + git_writestream *last_stream) +{ +#ifndef GIT_DEPRECATE_HARD + GIT_ASSERT(fe->filter->stream || fe->filter->apply); + + /* + * If necessary, create a stream that proxies the traditional + * application. + */ + if (!fe->filter->stream) { + /* Create a stream that proxies the one-shot apply */ + return buffered_legacy_stream_new(out, + fe->filter, fe->filter->apply, filters->temp_buf, + &fe->payload, &filters->source, last_stream); + } +#endif + + GIT_ASSERT(fe->filter->stream); + return fe->filter->stream(out, fe->filter, + &fe->payload, &filters->source, last_stream); +} + +static int stream_list_init( + git_writestream **out, + git_vector *streams, + git_filter_list *filters, + git_writestream *target) +{ + git_writestream *last_stream = target; + size_t i; + int error = 0; + + *out = NULL; + + if (!filters) { + *out = target; + return 0; + } + + /* Create filters last to first to get the chaining direction */ + for (i = 0; i < git_array_size(filters->filters); ++i) { + size_t filter_idx = (filters->source.mode == GIT_FILTER_TO_WORKTREE) ? + git_array_size(filters->filters) - 1 - i : i; + + git_filter_entry *fe = git_array_get(filters->filters, filter_idx); + git_writestream *filter_stream; + + error = setup_stream(&filter_stream, fe, filters, last_stream); + + if (error < 0) + goto out; + + git_vector_insert(streams, filter_stream); + last_stream = filter_stream; + } + +out: + if (error) + last_stream->close(last_stream); + else + *out = last_stream; + + return error; +} + +static void filter_streams_free(git_vector *streams) +{ + git_writestream *stream; + size_t i; + + git_vector_foreach(streams, i, stream) + stream->free(stream); + git_vector_dispose(streams); +} + +int git_filter_list_stream_file( + git_filter_list *filters, + git_repository *repo, + const char *path, + git_writestream *target) +{ + char buf[GIT_BUFSIZE_FILTERIO]; + git_str abspath = GIT_STR_INIT; + const char *base = repo ? git_repository_workdir(repo) : NULL; + git_vector filter_streams = GIT_VECTOR_INIT; + git_writestream *stream_start; + ssize_t readlen; + int fd = -1, error, initialized = 0; + + if ((error = stream_list_init( + &stream_start, &filter_streams, filters, target)) < 0 || + (error = git_fs_path_join_unrooted(&abspath, path, base, NULL)) < 0 || + (error = git_path_validate_str_length(repo, &abspath)) < 0) + goto done; + + initialized = 1; + + if ((fd = git_futils_open_ro(abspath.ptr)) < 0) { + error = fd; + goto done; + } + + while ((readlen = p_read(fd, buf, sizeof(buf))) > 0) { + if ((error = stream_start->write(stream_start, buf, readlen)) < 0) + goto done; + } + + if (readlen < 0) + error = -1; + +done: + if (initialized) + error |= stream_start->close(stream_start); + + if (fd >= 0) + p_close(fd); + filter_streams_free(&filter_streams); + git_str_dispose(&abspath); + return error; +} + +int git_filter_list_stream_buffer( + git_filter_list *filters, + const char *buffer, + size_t len, + git_writestream *target) +{ + git_vector filter_streams = GIT_VECTOR_INIT; + git_writestream *stream_start; + int error, initialized = 0; + + if ((error = stream_list_init(&stream_start, &filter_streams, filters, target)) < 0) + goto out; + initialized = 1; + + if ((error = stream_start->write(stream_start, buffer, len)) < 0) + goto out; + +out: + if (initialized) + error |= stream_start->close(stream_start); + + filter_streams_free(&filter_streams); + return error; +} + +int git_filter_list_stream_blob( + git_filter_list *filters, + git_blob *blob, + git_writestream *target) +{ + git_str in = GIT_STR_INIT; + + if (buf_from_blob(&in, blob) < 0) + return -1; + + if (filters) + git_oid_cpy(&filters->source.oid, git_blob_id(blob)); + + return git_filter_list_stream_buffer(filters, in.ptr, in.size, target); +} + +int git_filter_init(git_filter *filter, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE(filter, version, git_filter, GIT_FILTER_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD + +int git_filter_list_stream_data( + git_filter_list *filters, + git_buf *data, + git_writestream *target) +{ + return git_filter_list_stream_buffer(filters, data->ptr, data->size, target); +} + +int git_filter_list_apply_to_data( + git_buf *tgt, git_filter_list *filters, git_buf *src) +{ + return git_filter_list_apply_to_buffer(tgt, filters, src->ptr, src->size); +} + +#endif diff --git a/src/libgit2/filter.h b/src/libgit2/filter.h new file mode 100644 index 00000000000..58cb4b42407 --- /dev/null +++ b/src/libgit2/filter.h @@ -0,0 +1,85 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_filter_h__ +#define INCLUDE_filter_h__ + +#include "common.h" + +#include "attr_file.h" +#include "git2/filter.h" +#include "git2/sys/filter.h" + +/* Amount of file to examine for NUL byte when checking binary-ness */ +#define GIT_FILTER_BYTES_TO_CHECK_NUL 8000 + +typedef struct { + git_filter_options options; + git_attr_session *attr_session; + git_str *temp_buf; +} git_filter_session; + +#define GIT_FILTER_SESSION_INIT {GIT_FILTER_OPTIONS_INIT, 0} + +extern int git_filter_global_init(void); + +extern void git_filter_free(git_filter *filter); + +extern int git_filter_list__load( + git_filter_list **filters, + git_repository *repo, + git_blob *blob, /* can be NULL */ + const char *path, + git_filter_mode_t mode, + git_filter_session *filter_session); + +int git_filter_list__apply_to_buffer( + git_str *out, + git_filter_list *filters, + const char *in, + size_t in_len); +int git_filter_list__apply_to_file( + git_str *out, + git_filter_list *filters, + git_repository *repo, + const char *path); +int git_filter_list__apply_to_blob( + git_str *out, + git_filter_list *filters, + git_blob *blob); + +/* + * The given input buffer will be converted to the given output buffer. + * The input buffer will be freed (_if_ it was allocated). + */ +extern int git_filter_list__convert_buf( + git_str *out, + git_filter_list *filters, + git_str *in); + +extern int git_filter_list__apply_to_file( + git_str *out, + git_filter_list *filters, + git_repository *repo, + const char *path); + +/* + * Available filters + */ + +extern git_filter *git_crlf_filter_new(void); +extern git_filter *git_ident_filter_new(void); + +extern int git_filter_buffered_stream_new( + git_writestream **out, + git_filter *filter, + int (*write_fn)(git_filter *, void **, git_str *, const git_str *, const git_filter_source *), + git_str *temp_buf, + void **payload, + const git_filter_source *source, + git_writestream *target); + +#endif diff --git a/src/libgit2/git2.rc b/src/libgit2/git2.rc new file mode 100644 index 00000000000..07592d15220 --- /dev/null +++ b/src/libgit2/git2.rc @@ -0,0 +1,59 @@ +#include +#include "../../include/git2/version.h" + +#ifndef LIBGIT2_FILENAME +# ifdef __GNUC__ +# define LIBGIT2_FILENAME git2 +# else +# define LIBGIT2_FILENAME "git2" +# endif +#endif + +#ifndef LIBGIT2_COMMENTS +# define LIBGIT2_COMMENTS "For more information visit https://libgit2.org/" +#endif + +#ifdef __GNUC__ +# define _STR(x) #x +# define STR(x) _STR(x) +#else +# define STR(x) x +#endif + +#ifdef __GNUC__ +VS_VERSION_INFO VERSIONINFO +#else +VS_VERSION_INFO VERSIONINFO MOVEABLE IMPURE LOADONCALL DISCARDABLE +#endif + FILEVERSION LIBGIT2_VERSION_MAJOR,LIBGIT2_VERSION_MINOR,LIBGIT2_VERSION_REVISION,LIBGIT2_VERSION_PATCH + PRODUCTVERSION LIBGIT2_VERSION_MAJOR,LIBGIT2_VERSION_MINOR,LIBGIT2_VERSION_REVISION,LIBGIT2_VERSION_PATCH + FILEFLAGSMASK VS_FFI_FILEFLAGSMASK +#ifdef _DEBUG + FILEFLAGS VS_FF_DEBUG +#else + FILEFLAGS 0 +#endif + FILEOS VOS_NT_WINDOWS32 + FILETYPE VFT_DLL + FILESUBTYPE VFT2_UNKNOWN +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904E4" + //language ID = U.S. English, char set = Windows, Multilingual + BEGIN + VALUE "FileDescription", "libgit2 - the Git linkable library\0" + VALUE "FileVersion", LIBGIT2_VERSION "\0" + VALUE "InternalName", STR(LIBGIT2_FILENAME) ".dll\0" + VALUE "LegalCopyright", "Copyright (C) the libgit2 contributors. All rights reserved.\0" + VALUE "OriginalFilename", STR(LIBGIT2_FILENAME) ".dll\0" + VALUE "ProductName", "libgit2\0" + VALUE "ProductVersion", LIBGIT2_VERSION "\0" + VALUE "Comments", LIBGIT2_COMMENTS "\0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0409, 1252 + END +END diff --git a/src/libgit2/grafts.c b/src/libgit2/grafts.c new file mode 100644 index 00000000000..d31b4efddf9 --- /dev/null +++ b/src/libgit2/grafts.c @@ -0,0 +1,270 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "grafts.h" + +#include "futils.h" +#include "oid.h" +#include "oidarray.h" +#include "parse.h" +#include "hashmap_oid.h" + +GIT_HASHMAP_OID_SETUP(git_grafts_oidmap, git_commit_graft *); + +struct git_grafts { + /* Map of `git_commit_graft`s */ + git_grafts_oidmap commits; + + /* Type of object IDs */ + git_oid_t oid_type; + + /* File backing the graft. NULL if it's an in-memory graft */ + char *path; + unsigned char path_checksum[GIT_HASH_SHA256_SIZE]; +}; + +int git_grafts_new(git_grafts **out, git_oid_t oid_type) +{ + git_grafts *grafts; + + GIT_ASSERT_ARG(out && oid_type); + + grafts = git__calloc(1, sizeof(*grafts)); + GIT_ERROR_CHECK_ALLOC(grafts); + + grafts->oid_type = oid_type; + + *out = grafts; + return 0; +} + +int git_grafts_open( + git_grafts **out, + const char *path, + git_oid_t oid_type) +{ + git_grafts *grafts = NULL; + int error; + + GIT_ASSERT_ARG(out && path && oid_type); + + if ((error = git_grafts_new(&grafts, oid_type)) < 0) + goto error; + + grafts->path = git__strdup(path); + GIT_ERROR_CHECK_ALLOC(grafts->path); + + if ((error = git_grafts_refresh(grafts)) < 0) + goto error; + + *out = grafts; + +error: + if (error < 0) + git_grafts_free(grafts); + + return error; +} + +int git_grafts_open_or_refresh( + git_grafts **out, + const char *path, + git_oid_t oid_type) +{ + GIT_ASSERT_ARG(out && path && oid_type); + + return *out ? git_grafts_refresh(*out) : git_grafts_open(out, path, oid_type); +} + +void git_grafts_free(git_grafts *grafts) +{ + if (!grafts) + return; + git__free(grafts->path); + git_grafts_clear(grafts); + git_grafts_oidmap_dispose(&grafts->commits); + git__free(grafts); +} + +void git_grafts_clear(git_grafts *grafts) +{ + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + git_commit_graft *graft; + + if (!grafts) + return; + + while (git_grafts_oidmap_iterate(&iter, NULL, &graft, &grafts->commits) == 0) { + git__free(graft->parents.ptr); + git__free(graft); + } + + git_grafts_oidmap_clear(&grafts->commits); +} + +int git_grafts_refresh(git_grafts *grafts) +{ + git_str contents = GIT_STR_INIT; + int error, updated = 0; + + GIT_ASSERT_ARG(grafts); + + if (!grafts->path) + return 0; + + if ((error = git_futils_readbuffer_updated(&contents, grafts->path, + grafts->path_checksum, &updated)) < 0) { + + if (error == GIT_ENOTFOUND) { + git_grafts_clear(grafts); + error = 0; + } + + goto cleanup; + } + + if (!updated) { + goto cleanup; + } + + if ((error = git_grafts_parse(grafts, contents.ptr, contents.size)) < 0) + goto cleanup; + +cleanup: + git_str_dispose(&contents); + return error; +} + +int git_grafts_parse(git_grafts *grafts, const char *buf, size_t len) +{ + git_array_oid_t parents = GIT_ARRAY_INIT; + git_parse_ctx parser; + int error; + + git_grafts_clear(grafts); + + if ((error = git_parse_ctx_init(&parser, buf, len)) < 0) + goto error; + + for (; parser.remain_len; git_parse_advance_line(&parser)) { + git_oid graft_oid; + + if ((error = git_parse_advance_oid(&graft_oid, &parser, grafts->oid_type)) < 0) { + git_error_set(GIT_ERROR_GRAFTS, "invalid graft OID at line %" PRIuZ, parser.line_num); + goto error; + } + + while (parser.line_len && git_parse_advance_expected(&parser, "\n", 1) != 0) { + git_oid *id = git_array_alloc(parents); + GIT_ERROR_CHECK_ALLOC(id); + + if ((error = git_parse_advance_expected(&parser, " ", 1)) < 0 || + (error = git_parse_advance_oid(id, &parser, grafts->oid_type)) < 0) { + git_error_set(GIT_ERROR_GRAFTS, "invalid parent OID at line %" PRIuZ, parser.line_num); + goto error; + } + } + + if ((error = git_grafts_add(grafts, &graft_oid, parents)) < 0) + goto error; + + git_array_clear(parents); + } + +error: + git_array_clear(parents); + return error; +} + +int git_grafts_add(git_grafts *grafts, const git_oid *oid, git_array_oid_t parents) +{ + git_commit_graft *graft; + git_oid *parent_oid; + int error; + size_t i; + + GIT_ASSERT_ARG(grafts && oid); + + graft = git__calloc(1, sizeof(*graft)); + GIT_ERROR_CHECK_ALLOC(graft); + + git_array_init_to_size(graft->parents, git_array_size(parents)); + git_array_foreach(parents, i, parent_oid) { + git_oid *id = git_array_alloc(graft->parents); + GIT_ERROR_CHECK_ALLOC(id); + + git_oid_cpy(id, parent_oid); + } + git_oid_cpy(&graft->oid, oid); + + if ((error = git_grafts_remove(grafts, &graft->oid)) < 0 && error != GIT_ENOTFOUND) + goto cleanup; + + if ((error = git_grafts_oidmap_put(&grafts->commits, &graft->oid, graft)) < 0) + goto cleanup; + + return 0; + +cleanup: + git_array_clear(graft->parents); + git__free(graft); + return error; +} + +int git_grafts_remove(git_grafts *grafts, const git_oid *oid) +{ + git_commit_graft *graft; + int error; + + GIT_ASSERT_ARG(grafts && oid); + + if (git_grafts_oidmap_get(&graft, &grafts->commits, oid) != 0) + return GIT_ENOTFOUND; + + if ((error = git_grafts_oidmap_remove(&grafts->commits, oid)) < 0) + return error; + + git__free(graft->parents.ptr); + git__free(graft); + + return 0; +} + +int git_grafts_get(git_commit_graft **out, git_grafts *grafts, const git_oid *oid) +{ + GIT_ASSERT_ARG(out && grafts && oid); + return git_grafts_oidmap_get(out, &grafts->commits, oid); +} + +int git_grafts_oids(git_oid **out, size_t *out_len, git_grafts *grafts) +{ + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + git_array_oid_t array = GIT_ARRAY_INIT; + const git_oid *oid; + size_t existing; + + GIT_ASSERT_ARG(out && grafts); + + if ((existing = git_grafts_oidmap_size(&grafts->commits)) > 0) + git_array_init_to_size(array, existing); + + while (git_grafts_oidmap_iterate(&iter, &oid, NULL, &grafts->commits) == 0) { + git_oid *cpy = git_array_alloc(array); + GIT_ERROR_CHECK_ALLOC(cpy); + git_oid_cpy(cpy, oid); + } + + *out = array.ptr; + *out_len = array.size; + + return 0; +} + +size_t git_grafts_size(git_grafts *grafts) +{ + return git_grafts_oidmap_size(&grafts->commits); +} diff --git a/src/libgit2/grafts.h b/src/libgit2/grafts.h new file mode 100644 index 00000000000..ab59f56b07c --- /dev/null +++ b/src/libgit2/grafts.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_graft_h__ +#define INCLUDE_graft_h__ + +#include "common.h" +#include "oidarray.h" + +/** graft commit */ +typedef struct { + git_oid oid; + git_array_oid_t parents; +} git_commit_graft; + +typedef struct git_grafts git_grafts; + +int git_grafts_new(git_grafts **out, git_oid_t oid_type); +int git_grafts_open(git_grafts **out, const char *path, git_oid_t oid_type); +int git_grafts_open_or_refresh(git_grafts **out, const char *path, git_oid_t oid_type); +void git_grafts_free(git_grafts *grafts); +void git_grafts_clear(git_grafts *grafts); + +int git_grafts_refresh(git_grafts *grafts); +int git_grafts_parse(git_grafts *grafts, const char *buf, size_t len); +int git_grafts_add(git_grafts *grafts, const git_oid *oid, git_array_oid_t parents); +int git_grafts_remove(git_grafts *grafts, const git_oid *oid); +int git_grafts_get(git_commit_graft **out, git_grafts *grafts, const git_oid *oid); +int git_grafts_oids(git_oid **out, size_t *out_len, git_grafts *grafts); +size_t git_grafts_size(git_grafts *grafts); + +#endif diff --git a/src/libgit2/graph.c b/src/libgit2/graph.c new file mode 100644 index 00000000000..3cce7fd1185 --- /dev/null +++ b/src/libgit2/graph.c @@ -0,0 +1,249 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "revwalk.h" +#include "merge.h" +#include "git2/graph.h" + +static int interesting(git_pqueue *list, git_commit_list *roots) +{ + unsigned int i; + + for (i = 0; i < git_pqueue_size(list); i++) { + git_commit_list_node *commit = git_pqueue_get(list, i); + if ((commit->flags & STALE) == 0) + return 1; + } + + while(roots) { + if ((roots->item->flags & STALE) == 0) + return 1; + roots = roots->next; + } + + return 0; +} + +static int mark_parents(git_revwalk *walk, git_commit_list_node *one, + git_commit_list_node *two) +{ + unsigned int i; + git_commit_list *roots = NULL; + git_pqueue list; + + /* if the commit is repeated, we have a our merge base already */ + if (one == two) { + one->flags |= PARENT1 | PARENT2 | RESULT; + return 0; + } + + if (git_pqueue_init(&list, 0, 2, git_commit_list_generation_cmp) < 0) + return -1; + + if (git_commit_list_parse(walk, one) < 0) + goto on_error; + one->flags |= PARENT1; + if (git_pqueue_insert(&list, one) < 0) + goto on_error; + + if (git_commit_list_parse(walk, two) < 0) + goto on_error; + two->flags |= PARENT2; + if (git_pqueue_insert(&list, two) < 0) + goto on_error; + + /* as long as there are non-STALE commits */ + while (interesting(&list, roots)) { + git_commit_list_node *commit = git_pqueue_pop(&list); + unsigned int flags; + + if (commit == NULL) + break; + + flags = commit->flags & (PARENT1 | PARENT2 | STALE); + if (flags == (PARENT1 | PARENT2)) { + if (!(commit->flags & RESULT)) + commit->flags |= RESULT; + /* we mark the parents of a merge stale */ + flags |= STALE; + } + + for (i = 0; i < commit->out_degree; i++) { + git_commit_list_node *p = commit->parents[i]; + if ((p->flags & flags) == flags) + continue; + + if (git_commit_list_parse(walk, p) < 0) + goto on_error; + + p->flags |= flags; + if (git_pqueue_insert(&list, p) < 0) + goto on_error; + } + + /* Keep track of root commits, to make sure the path gets marked */ + if (commit->out_degree == 0) { + if (git_commit_list_insert(commit, &roots) == NULL) + goto on_error; + } + } + + git_commit_list_free(&roots); + git_pqueue_free(&list); + return 0; + +on_error: + git_commit_list_free(&roots); + git_pqueue_free(&list); + return -1; +} + + +static int ahead_behind(git_commit_list_node *one, git_commit_list_node *two, + size_t *ahead, size_t *behind) +{ + git_commit_list_node *commit; + git_pqueue pq; + int error = 0, i; + *ahead = 0; + *behind = 0; + + if (git_pqueue_init(&pq, 0, 2, git_commit_list_time_cmp) < 0) + return -1; + + if ((error = git_pqueue_insert(&pq, one)) < 0 || + (error = git_pqueue_insert(&pq, two)) < 0) + goto done; + + while ((commit = git_pqueue_pop(&pq)) != NULL) { + if (commit->flags & RESULT || + (commit->flags & (PARENT1 | PARENT2)) == (PARENT1 | PARENT2)) + continue; + else if (commit->flags & PARENT1) + (*ahead)++; + else if (commit->flags & PARENT2) + (*behind)++; + + for (i = 0; i < commit->out_degree; i++) { + git_commit_list_node *p = commit->parents[i]; + if ((error = git_pqueue_insert(&pq, p)) < 0) + goto done; + } + commit->flags |= RESULT; + } + +done: + git_pqueue_free(&pq); + return error; +} + +int git_graph_ahead_behind(size_t *ahead, size_t *behind, git_repository *repo, + const git_oid *local, const git_oid *upstream) +{ + git_revwalk *walk; + git_commit_list_node *commit_u, *commit_l; + + if (git_revwalk_new(&walk, repo) < 0) + return -1; + + commit_u = git_revwalk__commit_lookup(walk, upstream); + if (commit_u == NULL) + goto on_error; + + commit_l = git_revwalk__commit_lookup(walk, local); + if (commit_l == NULL) + goto on_error; + + if (mark_parents(walk, commit_l, commit_u) < 0) + goto on_error; + if (ahead_behind(commit_l, commit_u, ahead, behind) < 0) + goto on_error; + + git_revwalk_free(walk); + + return 0; + +on_error: + git_revwalk_free(walk); + return -1; +} + +int git_graph_descendant_of(git_repository *repo, const git_oid *commit, const git_oid *ancestor) +{ + if (git_oid_equal(commit, ancestor)) + return 0; + + return git_graph_reachable_from_any(repo, ancestor, commit, 1); +} + +int git_graph_reachable_from_any( + git_repository *repo, + const git_oid *commit_id, + const git_oid descendant_array[], + size_t length) +{ + git_revwalk *walk = NULL; + git_vector list; + git_commit_list *result = NULL; + git_commit_list_node *commit; + size_t i; + uint32_t minimum_generation = 0xffffffff; + int error = 0; + + if (!length) + return 0; + + for (i = 0; i < length; ++i) { + if (git_oid_equal(commit_id, &descendant_array[i])) + return 1; + } + + if ((error = git_vector_init(&list, length + 1, NULL)) < 0) + return error; + + if ((error = git_revwalk_new(&walk, repo)) < 0) + goto done; + + for (i = 0; i < length; i++) { + commit = git_revwalk__commit_lookup(walk, &descendant_array[i]); + if (commit == NULL) { + error = -1; + goto done; + } + + git_vector_insert(&list, commit); + if (minimum_generation > commit->generation) + minimum_generation = commit->generation; + } + + commit = git_revwalk__commit_lookup(walk, commit_id); + if (commit == NULL) { + error = -1; + goto done; + } + + if (minimum_generation > commit->generation) + minimum_generation = commit->generation; + + if ((error = git_merge__bases_many(&result, walk, commit, &list, minimum_generation)) < 0) + goto done; + + if (result) { + error = git_oid_equal(commit_id, &result->item->oid); + } else { + /* No merge-base found, it's not a descendant */ + error = 0; + } + +done: + git_commit_list_free(&result); + git_vector_dispose(&list); + git_revwalk_free(walk); + return error; +} diff --git a/src/libgit2/hashmap_oid.h b/src/libgit2/hashmap_oid.h new file mode 100644 index 00000000000..cb1143cc760 --- /dev/null +++ b/src/libgit2/hashmap_oid.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_hashmap_oid_h__ +#define INCLUDE_hashmap_oid_h__ + +#include "hashmap.h" + +GIT_INLINE(uint32_t) git_hashmap_oid_hashcode(const git_oid *oid) +{ + uint32_t hash; + memcpy(&hash, oid->id, sizeof(uint32_t)); + return hash; +} + +#define GIT_HASHMAP_OID_STRUCT(name, val_t) \ + GIT_HASHMAP_STRUCT(name, const git_oid *, val_t) +#define GIT_HASHMAP_OID_PROTOTYPES(name, val_t) \ + GIT_HASHMAP_PROTOTYPES(name, const git_oid *, val_t) +#define GIT_HASHMAP_OID_FUNCTIONS(name, scope, val_t) \ + GIT_HASHMAP_FUNCTIONS(name, scope, const git_oid *, val_t, git_hashmap_oid_hashcode, git_oid_equal) + +#define GIT_HASHMAP_OID_SETUP(name, val_t) \ + GIT_HASHMAP_OID_STRUCT(name, val_t) \ + GIT_HASHMAP_OID_FUNCTIONS(name, GIT_HASHMAP_INLINE, val_t) + +#endif diff --git a/src/libgit2/hashsig.c b/src/libgit2/hashsig.c new file mode 100644 index 00000000000..6b4fb835216 --- /dev/null +++ b/src/libgit2/hashsig.c @@ -0,0 +1,375 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "git2/sys/hashsig.h" +#include "futils.h" +#include "util.h" + +typedef uint32_t hashsig_t; +typedef uint64_t hashsig_state; + +#define HASHSIG_SCALE 100 + +#define HASHSIG_MAX_RUN 80 +#define HASHSIG_HASH_START INT64_C(0x012345678ABCDEF0) +#define HASHSIG_HASH_SHIFT 5 + +#define HASHSIG_HASH_MIX(S,CH) \ + (S) = ((S) << HASHSIG_HASH_SHIFT) - (S) + (hashsig_state)(CH) + +#define HASHSIG_HEAP_SIZE ((1 << 7) - 1) +#define HASHSIG_HEAP_MIN_SIZE 4 + +typedef int (*hashsig_cmp)(const void *a, const void *b, void *); + +typedef struct { + int size, asize; + hashsig_cmp cmp; + hashsig_t values[HASHSIG_HEAP_SIZE]; +} hashsig_heap; + +struct git_hashsig { + hashsig_heap mins; + hashsig_heap maxs; + size_t lines; + git_hashsig_option_t opt; +}; + +#define HEAP_LCHILD_OF(I) (((I)<<1)+1) +#define HEAP_RCHILD_OF(I) (((I)<<1)+2) +#define HEAP_PARENT_OF(I) (((I)-1)>>1) + +static void hashsig_heap_init(hashsig_heap *h, hashsig_cmp cmp) +{ + h->size = 0; + h->asize = HASHSIG_HEAP_SIZE; + h->cmp = cmp; +} + +static int hashsig_cmp_max(const void *a, const void *b, void *payload) +{ + hashsig_t av = *(const hashsig_t *)a, bv = *(const hashsig_t *)b; + GIT_UNUSED(payload); + return (av < bv) ? -1 : (av > bv) ? 1 : 0; +} + +static int hashsig_cmp_min(const void *a, const void *b, void *payload) +{ + hashsig_t av = *(const hashsig_t *)a, bv = *(const hashsig_t *)b; + GIT_UNUSED(payload); + return (av > bv) ? -1 : (av < bv) ? 1 : 0; +} + +static void hashsig_heap_up(hashsig_heap *h, int el) +{ + int parent_el = HEAP_PARENT_OF(el); + + while (el > 0 && h->cmp(&h->values[parent_el], &h->values[el], NULL) > 0) { + hashsig_t t = h->values[el]; + h->values[el] = h->values[parent_el]; + h->values[parent_el] = t; + + el = parent_el; + parent_el = HEAP_PARENT_OF(el); + } +} + +static void hashsig_heap_down(hashsig_heap *h, int el) +{ + hashsig_t v, lv, rv; + + /* 'el < h->size / 2' tests if el is bottom row of heap */ + + while (el < h->size / 2) { + int lel = HEAP_LCHILD_OF(el), rel = HEAP_RCHILD_OF(el), swapel; + + v = h->values[el]; + lv = h->values[lel]; + rv = h->values[rel]; + + if (h->cmp(&v, &lv, NULL) < 0 && h->cmp(&v, &rv, NULL) < 0) + break; + + swapel = (h->cmp(&lv, &rv, NULL) < 0) ? lel : rel; + + h->values[el] = h->values[swapel]; + h->values[swapel] = v; + + el = swapel; + } +} + +static void hashsig_heap_sort(hashsig_heap *h) +{ + /* only need to do this at the end for signature comparison */ + git__qsort_r(h->values, h->size, sizeof(hashsig_t), h->cmp, NULL); +} + +static void hashsig_heap_insert(hashsig_heap *h, hashsig_t val) +{ + /* if heap is not full, insert new element */ + if (h->size < h->asize) { + h->values[h->size++] = val; + hashsig_heap_up(h, h->size - 1); + } + + /* if heap is full, pop top if new element should replace it */ + else if (h->cmp(&val, &h->values[0], NULL) > 0) { + h->size--; + h->values[0] = h->values[h->size]; + hashsig_heap_down(h, 0); + } + +} + +typedef struct { + int use_ignores; + uint8_t ignore_ch[256]; +} hashsig_in_progress; + +static int hashsig_in_progress_init( + hashsig_in_progress *prog, git_hashsig *sig) +{ + int i; + + /* no more than one can be set */ + GIT_ASSERT(!(sig->opt & GIT_HASHSIG_IGNORE_WHITESPACE) || + !(sig->opt & GIT_HASHSIG_SMART_WHITESPACE)); + + if (sig->opt & GIT_HASHSIG_IGNORE_WHITESPACE) { + for (i = 0; i < 256; ++i) + prog->ignore_ch[i] = git__isspace_nonlf(i); + prog->use_ignores = 1; + } else if (sig->opt & GIT_HASHSIG_SMART_WHITESPACE) { + for (i = 0; i < 256; ++i) + prog->ignore_ch[i] = git__isspace(i); + prog->use_ignores = 1; + } else { + memset(prog, 0, sizeof(*prog)); + } + + return 0; +} + +static int hashsig_add_hashes( + git_hashsig *sig, + const uint8_t *data, + size_t size, + hashsig_in_progress *prog) +{ + const uint8_t *scan = data, *end = data + size; + hashsig_state state = HASHSIG_HASH_START; + int use_ignores = prog->use_ignores, len; + uint8_t ch; + + while (scan < end) { + state = HASHSIG_HASH_START; + + for (len = 0; scan < end && len < HASHSIG_MAX_RUN; ) { + ch = *scan; + + if (use_ignores) + for (; scan < end && git__isspace_nonlf(ch); ch = *scan) + ++scan; + else if (sig->opt & + (GIT_HASHSIG_IGNORE_WHITESPACE | GIT_HASHSIG_SMART_WHITESPACE)) + for (; scan < end && ch == '\r'; ch = *scan) + ++scan; + + /* peek at next character to decide what to do next */ + if (sig->opt & GIT_HASHSIG_SMART_WHITESPACE) + use_ignores = (ch == '\n'); + + if (scan >= end) + break; + ++scan; + + /* check run terminator */ + if (ch == '\n' || ch == '\0') { + sig->lines++; + break; + } + + ++len; + HASHSIG_HASH_MIX(state, ch); + } + + if (len > 0) { + hashsig_heap_insert(&sig->mins, (hashsig_t)state); + hashsig_heap_insert(&sig->maxs, (hashsig_t)state); + + while (scan < end && (*scan == '\n' || !*scan)) + ++scan; + } + } + + prog->use_ignores = use_ignores; + + return 0; +} + +static int hashsig_finalize_hashes(git_hashsig *sig) +{ + if (sig->mins.size < HASHSIG_HEAP_MIN_SIZE && + !(sig->opt & GIT_HASHSIG_ALLOW_SMALL_FILES)) { + git_error_set(GIT_ERROR_INVALID, + "file too small for similarity signature calculation"); + return GIT_EBUFS; + } + + hashsig_heap_sort(&sig->mins); + hashsig_heap_sort(&sig->maxs); + + return 0; +} + +static git_hashsig *hashsig_alloc(git_hashsig_option_t opts) +{ + git_hashsig *sig = git__calloc(1, sizeof(git_hashsig)); + if (!sig) + return NULL; + + hashsig_heap_init(&sig->mins, hashsig_cmp_min); + hashsig_heap_init(&sig->maxs, hashsig_cmp_max); + sig->opt = opts; + + return sig; +} + +int git_hashsig_create( + git_hashsig **out, + const char *buf, + size_t buflen, + git_hashsig_option_t opts) +{ + int error; + hashsig_in_progress prog; + git_hashsig *sig = hashsig_alloc(opts); + GIT_ERROR_CHECK_ALLOC(sig); + + if ((error = hashsig_in_progress_init(&prog, sig)) < 0) + return error; + + error = hashsig_add_hashes(sig, (const uint8_t *)buf, buflen, &prog); + + if (!error) + error = hashsig_finalize_hashes(sig); + + if (!error) + *out = sig; + else + git_hashsig_free(sig); + + return error; +} + +int git_hashsig_create_fromfile( + git_hashsig **out, + const char *path, + git_hashsig_option_t opts) +{ + uint8_t buf[0x1000]; + ssize_t buflen = 0; + int error = 0, fd; + hashsig_in_progress prog; + git_hashsig *sig = hashsig_alloc(opts); + GIT_ERROR_CHECK_ALLOC(sig); + + if ((fd = git_futils_open_ro(path)) < 0) { + git__free(sig); + return fd; + } + + if ((error = hashsig_in_progress_init(&prog, sig)) < 0) { + p_close(fd); + return error; + } + + while (!error) { + if ((buflen = p_read(fd, buf, sizeof(buf))) <= 0) { + if ((error = (int)buflen) < 0) + git_error_set(GIT_ERROR_OS, + "read error on '%s' calculating similarity hashes", path); + break; + } + + error = hashsig_add_hashes(sig, buf, buflen, &prog); + } + + p_close(fd); + + if (!error) + error = hashsig_finalize_hashes(sig); + + if (!error) + *out = sig; + else + git_hashsig_free(sig); + + return error; +} + +void git_hashsig_free(git_hashsig *sig) +{ + git__free(sig); +} + +static int hashsig_heap_compare(const hashsig_heap *a, const hashsig_heap *b) +{ + int matches = 0, i, j, cmp; + + GIT_ASSERT_WITH_RETVAL(a->cmp == b->cmp, 0); + + /* hash heaps are sorted - just look for overlap vs total */ + + for (i = 0, j = 0; i < a->size && j < b->size; ) { + cmp = a->cmp(&a->values[i], &b->values[j], NULL); + + if (cmp < 0) + ++i; + else if (cmp > 0) + ++j; + else { + ++i; ++j; ++matches; + } + } + + return HASHSIG_SCALE * (matches * 2) / (a->size + b->size); +} + +int git_hashsig_compare(const git_hashsig *a, const git_hashsig *b) +{ + /* if we have no elements in either file then each file is either + * empty or blank. if we're ignoring whitespace then the files are + * similar, otherwise they're dissimilar. + */ + if (a->mins.size == 0 && b->mins.size == 0) { + if ((!a->lines && !b->lines) || + (a->opt & GIT_HASHSIG_IGNORE_WHITESPACE)) + return HASHSIG_SCALE; + else + return 0; + } + + /* if we have fewer than the maximum number of elements, then just use + * one array since the two arrays will be the same + */ + if (a->mins.size < HASHSIG_HEAP_SIZE) { + return hashsig_heap_compare(&a->mins, &b->mins); + } else { + int mins, maxs; + + if ((mins = hashsig_heap_compare(&a->mins, &b->mins)) < 0) + return mins; + if ((maxs = hashsig_heap_compare(&a->maxs, &b->maxs)) < 0) + return maxs; + + return (mins + maxs) / 2; + } +} diff --git a/src/libgit2/ident.c b/src/libgit2/ident.c new file mode 100644 index 00000000000..97110c66410 --- /dev/null +++ b/src/libgit2/ident.c @@ -0,0 +1,139 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "git2/sys/filter.h" +#include "filter.h" +#include "str.h" + +static int ident_find_id( + const char **id_start, const char **id_end, const char *start, size_t len) +{ + const char *end = start + len, *found = NULL; + + while (len > 3 && (found = memchr(start, '$', len)) != NULL) { + size_t remaining = (size_t)(end - found) - 1; + if (remaining < 3) + return GIT_ENOTFOUND; + + start = found + 1; + len = remaining; + + if (start[0] == 'I' && start[1] == 'd') + break; + } + + if (len < 3 || !found) + return GIT_ENOTFOUND; + *id_start = found; + + if ((found = memchr(start + 2, '$', len - 2)) == NULL) + return GIT_ENOTFOUND; + + *id_end = found + 1; + return 0; +} + +static int ident_insert_id( + git_str *to, const git_str *from, const git_filter_source *src) +{ + char oid[GIT_OID_MAX_HEXSIZE + 1]; + const char *id_start, *id_end, *from_end = from->ptr + from->size; + size_t need_size; + + /* replace $Id$ with blob id */ + + if (!git_filter_source_id(src)) + return GIT_PASSTHROUGH; + + git_oid_tostr(oid, sizeof(oid), git_filter_source_id(src)); + + if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0) + return GIT_PASSTHROUGH; + + need_size = (size_t)(id_start - from->ptr) + + 5 /* "$Id: " */ + GIT_OID_MAX_HEXSIZE + 2 /* " $" */ + + (size_t)(from_end - id_end); + + if (git_str_grow(to, need_size) < 0) + return -1; + + git_str_set(to, from->ptr, (size_t)(id_start - from->ptr)); + git_str_put(to, "$Id: ", 5); + git_str_puts(to, oid); + git_str_put(to, " $", 2); + git_str_put(to, id_end, (size_t)(from_end - id_end)); + + return git_str_oom(to) ? -1 : 0; +} + +static int ident_remove_id( + git_str *to, const git_str *from) +{ + const char *id_start, *id_end, *from_end = from->ptr + from->size; + size_t need_size; + + if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0) + return GIT_PASSTHROUGH; + + need_size = (size_t)(id_start - from->ptr) + + 4 /* "$Id$" */ + (size_t)(from_end - id_end); + + if (git_str_grow(to, need_size) < 0) + return -1; + + git_str_set(to, from->ptr, (size_t)(id_start - from->ptr)); + git_str_put(to, "$Id$", 4); + git_str_put(to, id_end, (size_t)(from_end - id_end)); + + return git_str_oom(to) ? -1 : 0; +} + +static int ident_apply( + git_filter *self, + void **payload, + git_str *to, + const git_str *from, + const git_filter_source *src) +{ + GIT_UNUSED(self); GIT_UNUSED(payload); + + /* Don't filter binary files */ + if (git_str_is_binary(from)) + return GIT_PASSTHROUGH; + + if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE) + return ident_insert_id(to, from, src); + else + return ident_remove_id(to, from); +} + +static int ident_stream( + git_writestream **out, + git_filter *self, + void **payload, + const git_filter_source *src, + git_writestream *next) +{ + return git_filter_buffered_stream_new(out, + self, ident_apply, NULL, payload, src, next); +} + +git_filter *git_ident_filter_new(void) +{ + git_filter *f = git__calloc(1, sizeof(git_filter)); + if (f == NULL) + return NULL; + + f->version = GIT_FILTER_VERSION; + f->attributes = "+ident"; /* apply to files with ident attribute set */ + f->shutdown = git_filter_free; + f->stream = ident_stream; + + return f; +} diff --git a/src/libgit2/ignore.c b/src/libgit2/ignore.c new file mode 100644 index 00000000000..13a67343f6a --- /dev/null +++ b/src/libgit2/ignore.c @@ -0,0 +1,656 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "ignore.h" + +#include "git2/ignore.h" +#include "common.h" +#include "attrcache.h" +#include "fs_path.h" +#include "config.h" +#include "wildmatch.h" +#include "path.h" + +#define GIT_IGNORE_INTERNAL "[internal]exclude" + +#define GIT_IGNORE_DEFAULT_RULES ".\n..\n.git\n" + +/** + * A negative ignore pattern can negate a positive one without + * wildcards if it is a basename only and equals the basename of + * the positive pattern. Thus + * + * foo/bar + * !bar + * + * would result in foo/bar being unignored again while + * + * moo/foo/bar + * !foo/bar + * + * would do nothing. The reverse also holds true: a positive + * basename pattern can be negated by unignoring the basename in + * subdirectories. Thus + * + * bar + * !foo/bar + * + * would result in foo/bar being unignored again. As with the + * first case, + * + * foo/bar + * !moo/foo/bar + * + * would do nothing, again. + */ +static int does_negate_pattern(git_attr_fnmatch *rule, git_attr_fnmatch *neg) +{ + int (*cmp)(const char *, const char *, size_t); + git_attr_fnmatch *longer, *shorter; + char *p; + + if ((rule->flags & GIT_ATTR_FNMATCH_NEGATIVE) != 0 + || (neg->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0) + return false; + + if (neg->flags & GIT_ATTR_FNMATCH_ICASE) + cmp = git__strncasecmp; + else + cmp = git__strncmp; + + /* If lengths match we need to have an exact match */ + if (rule->length == neg->length) { + return cmp(rule->pattern, neg->pattern, rule->length) == 0; + } else if (rule->length < neg->length) { + shorter = rule; + longer = neg; + } else { + shorter = neg; + longer = rule; + } + + /* Otherwise, we need to check if the shorter + * rule is a basename only (that is, it contains + * no path separator) and, if so, if it + * matches the tail of the longer rule */ + p = longer->pattern + longer->length - shorter->length; + + if (p[-1] != '/') + return false; + if (memchr(shorter->pattern, '/', shorter->length) != NULL) + return false; + + return cmp(p, shorter->pattern, shorter->length) == 0; +} + +/** + * A negative ignore can only unignore a file which is given explicitly before, thus + * + * foo + * !foo/bar + * + * does not unignore 'foo/bar' as it's not in the list. However + * + * foo/ + * !foo/bar + * + * does unignore 'foo/bar', as it is contained within the 'foo/' rule. + */ +static int does_negate_rule(int *out, git_vector *rules, git_attr_fnmatch *match) +{ + int error = 0, wildmatch_flags, effective_flags; + size_t i; + git_attr_fnmatch *rule; + char *path; + git_str buf = GIT_STR_INIT; + + *out = 0; + + wildmatch_flags = WM_PATHNAME; + if (match->flags & GIT_ATTR_FNMATCH_ICASE) + wildmatch_flags |= WM_CASEFOLD; + + /* path of the file relative to the workdir, so we match the rules in subdirs */ + if (match->containing_dir) { + git_str_puts(&buf, match->containing_dir); + } + if (git_str_puts(&buf, match->pattern) < 0) + return -1; + + path = git_str_detach(&buf); + + git_vector_foreach(rules, i, rule) { + if (!(rule->flags & GIT_ATTR_FNMATCH_HASWILD)) { + if (does_negate_pattern(rule, match)) { + error = 0; + *out = 1; + goto out; + } + else + continue; + } + + git_str_clear(&buf); + if (rule->containing_dir) + git_str_puts(&buf, rule->containing_dir); + git_str_puts(&buf, rule->pattern); + + if (git_str_oom(&buf)) + goto out; + + /* + * if rule isn't for full path we match without PATHNAME flag + * as lines like *.txt should match something like dir/test.txt + * requiring * to also match / + */ + effective_flags = wildmatch_flags; + if (!(rule->flags & GIT_ATTR_FNMATCH_FULLPATH)) + effective_flags &= ~WM_PATHNAME; + + /* if we found a match, we want to keep this rule */ + if ((wildmatch(git_str_cstr(&buf), path, effective_flags)) == WM_MATCH) { + *out = 1; + error = 0; + goto out; + } + } + + error = 0; + +out: + git__free(path); + git_str_dispose(&buf); + return error; +} + +static int parse_ignore_file( + git_repository *repo, git_attr_file *attrs, const char *data, bool allow_macros) +{ + int error = 0; + int ignore_case = false; + const char *scan = data, *context = NULL; + git_attr_fnmatch *match = NULL; + + GIT_UNUSED(allow_macros); + + if (git_repository__configmap_lookup(&ignore_case, repo, GIT_CONFIGMAP_IGNORECASE) < 0) + git_error_clear(); + + /* if subdir file path, convert context for file paths */ + if (attrs->entry && + git_fs_path_root(attrs->entry->path) < 0 && + !git__suffixcmp(attrs->entry->path, "/" GIT_IGNORE_FILE)) + context = attrs->entry->path; + + if (git_mutex_lock(&attrs->lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock ignore file"); + return -1; + } + + while (!error && *scan) { + int valid_rule = 1; + + if (!match && !(match = git__calloc(1, sizeof(*match)))) { + error = -1; + break; + } + + match->flags = + GIT_ATTR_FNMATCH_ALLOWSPACE | GIT_ATTR_FNMATCH_ALLOWNEG; + + if (!(error = git_attr_fnmatch__parse( + match, &attrs->pool, context, &scan))) + { + match->flags |= GIT_ATTR_FNMATCH_IGNORE; + + if (ignore_case) + match->flags |= GIT_ATTR_FNMATCH_ICASE; + + scan = git__next_line(scan); + + /* + * If a negative match doesn't actually do anything, + * throw it away. As we cannot always verify whether a + * rule containing wildcards negates another rule, we + * do not optimize away these rules, though. + * */ + if (match->flags & GIT_ATTR_FNMATCH_NEGATIVE + && !(match->flags & GIT_ATTR_FNMATCH_HASWILD)) + error = does_negate_rule(&valid_rule, &attrs->rules, match); + + if (!error && valid_rule) + error = git_vector_insert(&attrs->rules, match); + } + + if (error != 0 || !valid_rule) { + match->pattern = NULL; + + if (error == GIT_ENOTFOUND) + error = 0; + } else { + match = NULL; /* vector now "owns" the match */ + } + } + + git_mutex_unlock(&attrs->lock); + git__free(match); + + return error; +} + +static int push_ignore_file( + git_ignores *ignores, + git_vector *which_list, + const char *base, + const char *filename) +{ + git_attr_file_source source = { GIT_ATTR_FILE_SOURCE_FILE, base, filename }; + git_attr_file *file = NULL; + int error = 0; + + error = git_attr_cache__get(&file, ignores->repo, NULL, &source, parse_ignore_file, false); + + if (error < 0) + return error; + + if (file != NULL) { + if ((error = git_vector_insert(which_list, file)) < 0) + git_attr_file__free(file); + } + + return error; +} + +static int push_one_ignore(void *payload, const char *path) +{ + git_ignores *ign = payload; + ign->depth++; + return push_ignore_file(ign, &ign->ign_path, path, GIT_IGNORE_FILE); +} + +static int get_internal_ignores(git_attr_file **out, git_repository *repo) +{ + git_attr_file_source source = { GIT_ATTR_FILE_SOURCE_MEMORY, NULL, GIT_IGNORE_INTERNAL }; + int error; + + if ((error = git_attr_cache__init(repo)) < 0) + return error; + + error = git_attr_cache__get(out, repo, NULL, &source, NULL, false); + + /* if internal rules list is empty, insert default rules */ + if (!error && !(*out)->rules.length) + error = parse_ignore_file(repo, *out, GIT_IGNORE_DEFAULT_RULES, false); + + return error; +} + +int git_ignore__for_path( + git_repository *repo, + const char *path, + git_ignores *ignores) +{ + int error = 0; + const char *workdir = git_repository_workdir(repo); + git_attr_cache *attrcache; + const char *excludes_file = NULL; + git_str infopath = GIT_STR_INIT; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(ignores); + GIT_ASSERT_ARG(path); + + memset(ignores, 0, sizeof(*ignores)); + ignores->repo = repo; + + /* Read the ignore_case flag */ + if ((error = git_repository__configmap_lookup( + &ignores->ignore_case, repo, GIT_CONFIGMAP_IGNORECASE)) < 0) + goto cleanup; + + if ((error = git_attr_cache__init(repo)) < 0) + goto cleanup; + + /* given a unrooted path in a non-bare repo, resolve it */ + if (workdir && git_fs_path_root(path) < 0) { + git_str local = GIT_STR_INIT; + + if ((error = git_fs_path_dirname_r(&local, path)) < 0 || + (error = git_fs_path_resolve_relative(&local, 0)) < 0 || + (error = git_fs_path_to_dir(&local)) < 0 || + (error = git_str_joinpath(&ignores->dir, workdir, local.ptr)) < 0 || + (error = git_path_validate_str_length(repo, &ignores->dir)) < 0) { + /* Nothing, we just want to stop on the first error */ + } + + git_str_dispose(&local); + } else { + if (!(error = git_str_joinpath(&ignores->dir, path, ""))) + error = git_path_validate_str_length(NULL, &ignores->dir); + } + + if (error < 0) + goto cleanup; + + if (workdir && !git__prefixcmp(ignores->dir.ptr, workdir)) + ignores->dir_root = strlen(workdir); + + /* set up internals */ + if ((error = get_internal_ignores(&ignores->ign_internal, repo)) < 0) + goto cleanup; + + /* load .gitignore up the path */ + if (workdir != NULL) { + error = git_fs_path_walk_up( + &ignores->dir, workdir, push_one_ignore, ignores); + if (error < 0) + goto cleanup; + } + + /* load .git/info/exclude if possible */ + if ((error = git_repository__item_path(&infopath, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 || + (error = push_ignore_file(ignores, &ignores->ign_global, infopath.ptr, GIT_IGNORE_FILE_INREPO)) < 0) { + if (error != GIT_ENOTFOUND) + goto cleanup; + error = 0; + } + + /* load core.excludesfile */ + attrcache = git_repository_attr_cache(repo); + excludes_file = git_attr_cache_excludesfile(attrcache); + + if (excludes_file != NULL) + error = push_ignore_file( + ignores, &ignores->ign_global, NULL, excludes_file); + +cleanup: + git_str_dispose(&infopath); + if (error < 0) + git_ignore__free(ignores); + + return error; +} + +int git_ignore__push_dir(git_ignores *ign, const char *dir) +{ + if (git_str_joinpath(&ign->dir, ign->dir.ptr, dir) < 0) + return -1; + + ign->depth++; + + return push_ignore_file( + ign, &ign->ign_path, ign->dir.ptr, GIT_IGNORE_FILE); +} + +int git_ignore__pop_dir(git_ignores *ign) +{ + if (ign->ign_path.length > 0) { + git_attr_file *file = git_vector_last(&ign->ign_path); + const char *start = file->entry->path, *end; + + /* - ign->dir looks something like "/home/user/a/b/" (or "a/b/c/d/") + * - file->path looks something like "a/b/.gitignore + * + * We are popping the last directory off ign->dir. We also want + * to remove the file from the vector if the popped directory + * matches the ignore path. We need to test if the "a/b" part of + * the file key matches the path we are about to pop. + */ + + if ((end = strrchr(start, '/')) != NULL) { + size_t dirlen = (end - start) + 1; + const char *relpath = ign->dir.ptr + ign->dir_root; + size_t pathlen = ign->dir.size - ign->dir_root; + + if (pathlen == dirlen && !memcmp(relpath, start, dirlen)) { + git_vector_pop(&ign->ign_path); + git_attr_file__free(file); + } + } + } + + if (--ign->depth > 0) { + git_str_rtruncate_at_char(&ign->dir, '/'); + git_fs_path_to_dir(&ign->dir); + } + + return 0; +} + +void git_ignore__free(git_ignores *ignores) +{ + unsigned int i; + git_attr_file *file; + + git_attr_file__free(ignores->ign_internal); + + git_vector_foreach(&ignores->ign_path, i, file) { + git_attr_file__free(file); + ignores->ign_path.contents[i] = NULL; + } + git_vector_dispose(&ignores->ign_path); + + git_vector_foreach(&ignores->ign_global, i, file) { + git_attr_file__free(file); + ignores->ign_global.contents[i] = NULL; + } + git_vector_dispose(&ignores->ign_global); + + git_str_dispose(&ignores->dir); +} + +static bool ignore_lookup_in_rules( + int *ignored, git_attr_file *file, git_attr_path *path) +{ + size_t j; + git_attr_fnmatch *match; + + git_vector_rforeach(&file->rules, j, match) { + if (match->flags & GIT_ATTR_FNMATCH_DIRECTORY && + path->is_dir == GIT_DIR_FLAG_FALSE) + continue; + if (git_attr_fnmatch__match(match, path)) { + *ignored = ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0) ? + GIT_IGNORE_TRUE : GIT_IGNORE_FALSE; + return true; + } + } + + return false; +} + +int git_ignore__lookup( + int *out, git_ignores *ignores, const char *pathname, git_dir_flag dir_flag) +{ + size_t i; + git_attr_file *file; + git_attr_path path; + + *out = GIT_IGNORE_NOTFOUND; + + if (git_attr_path__init( + &path, pathname, git_repository_workdir(ignores->repo), dir_flag) < 0) + return -1; + + /* first process builtins - success means path was found */ + if (ignore_lookup_in_rules(out, ignores->ign_internal, &path)) + goto cleanup; + + /* next process files in the path. + * this process has to process ignores in reverse order + * to ensure correct prioritization of rules + */ + git_vector_rforeach(&ignores->ign_path, i, file) { + if (ignore_lookup_in_rules(out, file, &path)) + goto cleanup; + } + + /* last process global ignores */ + git_vector_foreach(&ignores->ign_global, i, file) { + if (ignore_lookup_in_rules(out, file, &path)) + goto cleanup; + } + +cleanup: + git_attr_path__free(&path); + return 0; +} + +int git_ignore_add_rule(git_repository *repo, const char *rules) +{ + int error; + git_attr_file *ign_internal = NULL; + + if ((error = get_internal_ignores(&ign_internal, repo)) < 0) + return error; + + error = parse_ignore_file(repo, ign_internal, rules, false); + git_attr_file__free(ign_internal); + + return error; +} + +int git_ignore_clear_internal_rules(git_repository *repo) +{ + int error; + git_attr_file *ign_internal; + + if ((error = get_internal_ignores(&ign_internal, repo)) < 0) + return error; + + if (!(error = git_attr_file__clear_rules(ign_internal, true))) + error = parse_ignore_file( + repo, ign_internal, GIT_IGNORE_DEFAULT_RULES, false); + + git_attr_file__free(ign_internal); + return error; +} + +int git_ignore_path_is_ignored( + int *ignored, + git_repository *repo, + const char *pathname) +{ + int error; + const char *workdir; + git_attr_path path; + git_ignores ignores; + unsigned int i; + git_attr_file *file; + git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(ignored); + GIT_ASSERT_ARG(pathname); + + workdir = git_repository_workdir(repo); + + memset(&path, 0, sizeof(path)); + memset(&ignores, 0, sizeof(ignores)); + + if (!git__suffixcmp(pathname, "/")) + dir_flag = GIT_DIR_FLAG_TRUE; + else if (git_repository_is_bare(repo)) + dir_flag = GIT_DIR_FLAG_FALSE; + + if ((error = git_attr_path__init(&path, pathname, workdir, dir_flag)) < 0 || + (error = git_ignore__for_path(repo, path.path, &ignores)) < 0) + goto cleanup; + + while (1) { + /* first process builtins - success means path was found */ + if (ignore_lookup_in_rules(ignored, ignores.ign_internal, &path)) + goto cleanup; + + /* next process files in the path */ + git_vector_foreach(&ignores.ign_path, i, file) { + if (ignore_lookup_in_rules(ignored, file, &path)) + goto cleanup; + } + + /* last process global ignores */ + git_vector_foreach(&ignores.ign_global, i, file) { + if (ignore_lookup_in_rules(ignored, file, &path)) + goto cleanup; + } + + /* move up one directory */ + if (path.basename == path.path) + break; + path.basename[-1] = '\0'; + while (path.basename > path.path && *path.basename != '/') + path.basename--; + if (path.basename > path.path) + path.basename++; + path.is_dir = 1; + + if ((error = git_ignore__pop_dir(&ignores)) < 0) + break; + } + + *ignored = 0; + +cleanup: + git_attr_path__free(&path); + git_ignore__free(&ignores); + return error; +} + +int git_ignore__check_pathspec_for_exact_ignores( + git_repository *repo, + git_vector *vspec, + bool no_fnmatch) +{ + int error = 0; + size_t i; + git_attr_fnmatch *match; + int ignored; + git_str path = GIT_STR_INIT; + const char *filename; + git_index *idx; + + if ((error = git_repository__ensure_not_bare( + repo, "validate pathspec")) < 0 || + (error = git_repository_index(&idx, repo)) < 0) + return error; + + git_vector_foreach(vspec, i, match) { + /* skip wildcard matches (if they are being used) */ + if ((match->flags & GIT_ATTR_FNMATCH_HASWILD) != 0 && + !no_fnmatch) + continue; + + filename = match->pattern; + + /* if file is already in the index, it's fine */ + if (git_index_get_bypath(idx, filename, 0) != NULL) + continue; + + if ((error = git_repository_workdir_path(&path, repo, filename)) < 0) + break; + + /* is there a file on disk that matches this exactly? */ + if (!git_fs_path_isfile(path.ptr)) + continue; + + /* is that file ignored? */ + if ((error = git_ignore_path_is_ignored(&ignored, repo, filename)) < 0) + break; + + if (ignored) { + git_error_set(GIT_ERROR_INVALID, "pathspec contains ignored file '%s'", + filename); + error = GIT_EINVALIDSPEC; + break; + } + } + + git_index_free(idx); + git_str_dispose(&path); + + return error; +} diff --git a/src/libgit2/ignore.h b/src/libgit2/ignore.h new file mode 100644 index 00000000000..aa5ca62b7af --- /dev/null +++ b/src/libgit2/ignore.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_ignore_h__ +#define INCLUDE_ignore_h__ + +#include "common.h" + +#include "repository.h" +#include "vector.h" +#include "attr_file.h" + +#define GIT_IGNORE_FILE ".gitignore" +#define GIT_IGNORE_FILE_INREPO "exclude" +#define GIT_IGNORE_FILE_XDG "ignore" + +/* The git_ignores structure maintains three sets of ignores: + * - internal ignores + * - per directory ignores + * - global ignores (at lower priority than the others) + * As you traverse from one directory to another, you can push and pop + * directories onto git_ignores list efficiently. + */ +typedef struct { + git_repository *repo; + git_str dir; /* current directory reflected in ign_path */ + git_attr_file *ign_internal; + git_vector ign_path; + git_vector ign_global; + size_t dir_root; /* offset in dir to repo root */ + int ignore_case; + int depth; +} git_ignores; + +extern int git_ignore__for_path( + git_repository *repo, const char *path, git_ignores *ign); + +extern int git_ignore__push_dir(git_ignores *ign, const char *dir); + +extern int git_ignore__pop_dir(git_ignores *ign); + +extern void git_ignore__free(git_ignores *ign); + +enum { + GIT_IGNORE_UNCHECKED = -2, + GIT_IGNORE_NOTFOUND = -1, + GIT_IGNORE_FALSE = 0, + GIT_IGNORE_TRUE = 1 +}; + +extern int git_ignore__lookup(int *out, git_ignores *ign, const char *path, git_dir_flag dir_flag); + +/* command line Git sometimes generates an error message if given a + * pathspec that contains an exact match to an ignored file (provided + * --force isn't also given). This makes it easy to check it that has + * happened. Returns GIT_EINVALIDSPEC if the pathspec contains ignored + * exact matches (that are not already present in the index). + */ +extern int git_ignore__check_pathspec_for_exact_ignores( + git_repository *repo, git_vector *pathspec, bool no_fnmatch); + +#endif diff --git a/src/libgit2/index.c b/src/libgit2/index.c new file mode 100644 index 00000000000..e8a692169ca --- /dev/null +++ b/src/libgit2/index.c @@ -0,0 +1,3935 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "index.h" + +#include + +#include "repository.h" +#include "tree.h" +#include "tree-cache.h" +#include "hash.h" +#include "iterator.h" +#include "pathspec.h" +#include "ignore.h" +#include "blob.h" +#include "diff.h" +#include "varint.h" +#include "path.h" +#include "index_map.h" + +#include "git2/odb.h" +#include "git2/oid.h" +#include "git2/blob.h" +#include "git2/config.h" +#include "git2/sys/index.h" + +static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths, + unsigned int flags, + git_index_matched_path_cb cb, void *payload); + +static const size_t INDEX_HEADER_SIZE = 12; + +static const unsigned int INDEX_VERSION_NUMBER_DEFAULT = 2; +static const unsigned int INDEX_VERSION_NUMBER_LB = 2; +static const unsigned int INDEX_VERSION_NUMBER_EXT = 3; +static const unsigned int INDEX_VERSION_NUMBER_COMP = 4; +static const unsigned int INDEX_VERSION_NUMBER_UB = 4; + +static const unsigned int INDEX_HEADER_SIG = 0x44495243; +static const char INDEX_EXT_TREECACHE_SIG[] = {'T', 'R', 'E', 'E'}; +static const char INDEX_EXT_UNMERGED_SIG[] = {'R', 'E', 'U', 'C'}; +static const char INDEX_EXT_CONFLICT_NAME_SIG[] = {'N', 'A', 'M', 'E'}; + +#define INDEX_OWNER(idx) ((git_repository *)(GIT_REFCOUNT_OWNER(idx))) + +struct index_header { + uint32_t signature; + uint32_t version; + uint32_t entry_count; +}; + +struct index_extension { + char signature[4]; + uint32_t extension_size; +}; + +struct entry_time { + uint32_t seconds; + uint32_t nanoseconds; +}; + +struct entry_common { + struct entry_time ctime; + struct entry_time mtime; + uint32_t dev; + uint32_t ino; + uint32_t mode; + uint32_t uid; + uint32_t gid; + uint32_t file_size; +}; + +#define entry_short(oid_size) \ + struct { \ + struct entry_common common; \ + unsigned char oid[oid_size]; \ + uint16_t flags; \ + char path[1]; /* arbitrary length */ \ + } + +#define entry_long(oid_size) \ + struct { \ + struct entry_common common; \ + unsigned char oid[oid_size]; \ + uint16_t flags; \ + uint16_t flags_extended; \ + char path[1]; /* arbitrary length */ \ + } + +typedef entry_short(GIT_OID_SHA1_SIZE) index_entry_short_sha1; +typedef entry_long(GIT_OID_SHA1_SIZE) index_entry_long_sha1; + +#ifdef GIT_EXPERIMENTAL_SHA256 +typedef entry_short(GIT_OID_SHA256_SIZE) index_entry_short_sha256; +typedef entry_long(GIT_OID_SHA256_SIZE) index_entry_long_sha256; +#endif + +#undef entry_short +#undef entry_long + +struct entry_srch_key { + const char *path; + size_t pathlen; + int stage; +}; + +struct entry_internal { + git_index_entry entry; + size_t pathlen; + char path[GIT_FLEX_ARRAY]; +}; + +struct reuc_entry_internal { + git_index_reuc_entry entry; + size_t pathlen; + char path[GIT_FLEX_ARRAY]; +}; + +bool git_index__enforce_unsaved_safety = false; + +/* local declarations */ +static int read_extension(size_t *read_len, git_index *index, size_t checksum_size, const char *buffer, size_t buffer_size); +static int read_header(struct index_header *dest, const void *buffer); + +static int parse_index(git_index *index, const char *buffer, size_t buffer_size); +static bool is_index_extended(git_index *index); +static int write_index(unsigned char checksum[GIT_HASH_MAX_SIZE], size_t *checksum_size, git_index *index, git_filebuf *file); + +static void index_entry_free(git_index_entry *entry); +static void index_entry_reuc_free(git_index_reuc_entry *reuc); + +int git_index_entry_srch(const void *key, const void *array_member) +{ + const struct entry_srch_key *srch_key = key; + const struct entry_internal *entry = array_member; + int cmp; + size_t len1, len2, len; + + len1 = srch_key->pathlen; + len2 = entry->pathlen; + len = len1 < len2 ? len1 : len2; + + cmp = memcmp(srch_key->path, entry->path, len); + if (cmp) + return cmp; + if (len1 < len2) + return -1; + if (len1 > len2) + return 1; + + if (srch_key->stage != GIT_INDEX_STAGE_ANY) + return srch_key->stage - GIT_INDEX_ENTRY_STAGE(&entry->entry); + + return 0; +} + +int git_index_entry_isrch(const void *key, const void *array_member) +{ + const struct entry_srch_key *srch_key = key; + const struct entry_internal *entry = array_member; + int cmp; + size_t len1, len2, len; + + len1 = srch_key->pathlen; + len2 = entry->pathlen; + len = len1 < len2 ? len1 : len2; + + cmp = strncasecmp(srch_key->path, entry->path, len); + + if (cmp) + return cmp; + if (len1 < len2) + return -1; + if (len1 > len2) + return 1; + + if (srch_key->stage != GIT_INDEX_STAGE_ANY) + return srch_key->stage - GIT_INDEX_ENTRY_STAGE(&entry->entry); + + return 0; +} + +static int index_entry_srch_path(const void *path, const void *array_member) +{ + const git_index_entry *entry = array_member; + + return strcmp((const char *)path, entry->path); +} + +static int index_entry_isrch_path(const void *path, const void *array_member) +{ + const git_index_entry *entry = array_member; + + return strcasecmp((const char *)path, entry->path); +} + +int git_index_entry_cmp(const void *a, const void *b) +{ + int diff; + const git_index_entry *entry_a = a; + const git_index_entry *entry_b = b; + + diff = strcmp(entry_a->path, entry_b->path); + + if (diff == 0) + diff = (GIT_INDEX_ENTRY_STAGE(entry_a) - GIT_INDEX_ENTRY_STAGE(entry_b)); + + return diff; +} + +int git_index_entry_icmp(const void *a, const void *b) +{ + int diff; + const git_index_entry *entry_a = a; + const git_index_entry *entry_b = b; + + diff = strcasecmp(entry_a->path, entry_b->path); + + if (diff == 0) + diff = (GIT_INDEX_ENTRY_STAGE(entry_a) - GIT_INDEX_ENTRY_STAGE(entry_b)); + + return diff; +} + +static int conflict_name_cmp(const void *a, const void *b) +{ + const git_index_name_entry *name_a = a; + const git_index_name_entry *name_b = b; + + if (name_a->ancestor && !name_b->ancestor) + return 1; + + if (!name_a->ancestor && name_b->ancestor) + return -1; + + if (name_a->ancestor) + return strcmp(name_a->ancestor, name_b->ancestor); + + if (!name_a->ours || !name_b->ours) + return 0; + + return strcmp(name_a->ours, name_b->ours); +} + +/** + * TODO: enable this when resolving case insensitive conflicts + */ +#if 0 +static int conflict_name_icmp(const void *a, const void *b) +{ + const git_index_name_entry *name_a = a; + const git_index_name_entry *name_b = b; + + if (name_a->ancestor && !name_b->ancestor) + return 1; + + if (!name_a->ancestor && name_b->ancestor) + return -1; + + if (name_a->ancestor) + return strcasecmp(name_a->ancestor, name_b->ancestor); + + if (!name_a->ours || !name_b->ours) + return 0; + + return strcasecmp(name_a->ours, name_b->ours); +} +#endif + +static int reuc_srch(const void *key, const void *array_member) +{ + const git_index_reuc_entry *reuc = array_member; + + return strcmp(key, reuc->path); +} + +static int reuc_isrch(const void *key, const void *array_member) +{ + const git_index_reuc_entry *reuc = array_member; + + return strcasecmp(key, reuc->path); +} + +static int reuc_cmp(const void *a, const void *b) +{ + const git_index_reuc_entry *info_a = a; + const git_index_reuc_entry *info_b = b; + + return strcmp(info_a->path, info_b->path); +} + +static int reuc_icmp(const void *a, const void *b) +{ + const git_index_reuc_entry *info_a = a; + const git_index_reuc_entry *info_b = b; + + return strcasecmp(info_a->path, info_b->path); +} + +static void index_entry_reuc_free(git_index_reuc_entry *reuc) +{ + git__free(reuc); +} + +static void index_entry_free(git_index_entry *entry) +{ + if (!entry) + return; + + memset(&entry->id, 0, sizeof(entry->id)); + git__free(entry); +} + +unsigned int git_index__create_mode(unsigned int mode) +{ + if (S_ISLNK(mode)) + return S_IFLNK; + + if (S_ISDIR(mode) || (mode & S_IFMT) == (S_IFLNK | S_IFDIR)) + return (S_IFLNK | S_IFDIR); + + return S_IFREG | GIT_PERMS_CANONICAL(mode); +} + +static unsigned int index_merge_mode( + git_index *index, git_index_entry *existing, unsigned int mode) +{ + if (index->no_symlinks && S_ISREG(mode) && + existing && S_ISLNK(existing->mode)) + return existing->mode; + + if (index->distrust_filemode && S_ISREG(mode)) + return (existing && S_ISREG(existing->mode)) ? + existing->mode : git_index__create_mode(0666); + + return git_index__create_mode(mode); +} + +GIT_INLINE(int) index_find_in_entries( + size_t *out, git_vector *entries, git_vector_cmp entry_srch, + const char *path, size_t path_len, int stage) +{ + struct entry_srch_key srch_key; + srch_key.path = path; + srch_key.pathlen = !path_len ? strlen(path) : path_len; + srch_key.stage = stage; + return git_vector_bsearch2(out, entries, entry_srch, &srch_key); +} + +GIT_INLINE(int) index_find( + size_t *out, git_index *index, + const char *path, size_t path_len, int stage) +{ + git_vector_sort(&index->entries); + + return index_find_in_entries( + out, &index->entries, index->entries_search, path, path_len, stage); +} + +void git_index__set_ignore_case(git_index *index, bool ignore_case) +{ + index->ignore_case = ignore_case; + index->entries_map.ignore_case = ignore_case; + + if (ignore_case) { + index->entries_cmp_path = git__strcasecmp_cb; + index->entries_search = git_index_entry_isrch; + index->entries_search_path = index_entry_isrch_path; + index->reuc_search = reuc_isrch; + } else { + index->entries_cmp_path = git__strcmp_cb; + index->entries_search = git_index_entry_srch; + index->entries_search_path = index_entry_srch_path; + index->reuc_search = reuc_srch; + } + + git_vector_set_cmp(&index->entries, + ignore_case ? git_index_entry_icmp : git_index_entry_cmp); + git_vector_sort(&index->entries); + + git_vector_set_cmp(&index->reuc, ignore_case ? reuc_icmp : reuc_cmp); + git_vector_sort(&index->reuc); +} + +int git_index_open_ext( + git_index **index_out, + const char *index_path, + const git_index_options *opts) +{ + git_index *index; + int error = -1; + + GIT_ASSERT_ARG(index_out); + GIT_ERROR_CHECK_VERSION(opts, GIT_INDEX_OPTIONS_VERSION, "git_index_options"); + + if (opts && opts->oid_type) + GIT_ASSERT_ARG(git_oid_type_is_valid(opts->oid_type)); + + index = git__calloc(1, sizeof(git_index)); + GIT_ERROR_CHECK_ALLOC(index); + + index->oid_type = opts && opts->oid_type ? opts->oid_type : + GIT_OID_DEFAULT; + + if (git_pool_init(&index->tree_pool, 1) < 0) + goto fail; + + if (index_path != NULL) { + index->index_file_path = git__strdup(index_path); + if (!index->index_file_path) + goto fail; + + /* Check if index file is stored on disk already */ + if (git_fs_path_exists(index->index_file_path) == true) + index->on_disk = 1; + } + + if (git_vector_init(&index->entries, 32, git_index_entry_cmp) < 0 || + git_vector_init(&index->names, 8, conflict_name_cmp) < 0 || + git_vector_init(&index->reuc, 8, reuc_cmp) < 0 || + git_vector_init(&index->deleted, 8, git_index_entry_cmp) < 0) + goto fail; + + index->entries_cmp_path = git__strcmp_cb; + index->entries_search = git_index_entry_srch; + index->entries_search_path = index_entry_srch_path; + index->reuc_search = reuc_srch; + index->version = INDEX_VERSION_NUMBER_DEFAULT; + + if (index_path != NULL && (error = git_index_read(index, true)) < 0) + goto fail; + + *index_out = index; + GIT_REFCOUNT_INC(index); + + return 0; + +fail: + git_pool_clear(&index->tree_pool); + git_index_free(index); + return error; +} + +int git_index_open(git_index **index_out, const char *index_path) +{ + return git_index_open_ext(index_out, index_path, NULL); +} + +int git_index_new(git_index **out) +{ + return git_index_open_ext(out, NULL, NULL); +} + +int git_index_new_ext(git_index **out, const git_index_options *opts) +{ + return git_index_open_ext(out, NULL, opts); +} + +static void index_free(git_index *index) +{ + /* index iterators increment the refcount of the index, so if we + * get here then there should be no outstanding iterators. + */ + if (git_atomic32_get(&index->readers)) + return; + + git_index_clear(index); + git_index_entrymap_dispose(&index->entries_map); + git_vector_dispose(&index->entries); + git_vector_dispose(&index->names); + git_vector_dispose(&index->reuc); + git_vector_dispose(&index->deleted); + + git__free(index->index_file_path); + + git__memzero(index, sizeof(*index)); + git__free(index); +} + +void git_index_free(git_index *index) +{ + if (index == NULL) + return; + + GIT_REFCOUNT_DEC(index, index_free); +} + +/* call with locked index */ +static void index_free_deleted(git_index *index) +{ + int readers = (int)git_atomic32_get(&index->readers); + size_t i; + + if (readers > 0 || !index->deleted.length) + return; + + for (i = 0; i < index->deleted.length; ++i) { + git_index_entry *ie = git_atomic_swap(index->deleted.contents[i], NULL); + index_entry_free(ie); + } + + git_vector_clear(&index->deleted); +} + +/* call with locked index */ +static int index_remove_entry(git_index *index, size_t pos) +{ + int error = 0; + git_index_entry *entry = git_vector_get(&index->entries, pos); + + if (entry != NULL) { + git_tree_cache_invalidate_path(index->tree, entry->path); + git_index_entrymap_remove(&index->entries_map, entry); + } + + error = git_vector_remove(&index->entries, pos); + + if (!error) { + if (git_atomic32_get(&index->readers) > 0) { + error = git_vector_insert(&index->deleted, entry); + } else { + index_entry_free(entry); + } + + index->dirty = 1; + } + + return error; +} + +int git_index_clear(git_index *index) +{ + int error = 0; + + GIT_ASSERT_ARG(index); + + index->dirty = 1; + index->tree = NULL; + git_pool_clear(&index->tree_pool); + + git_index_entrymap_clear(&index->entries_map); + + while (!error && index->entries.length > 0) + error = index_remove_entry(index, index->entries.length - 1); + + if (error) + goto done; + + index_free_deleted(index); + + if ((error = git_index_name_clear(index)) < 0 || + (error = git_index_reuc_clear(index)) < 0) + goto done; + + git_futils_filestamp_set(&index->stamp, NULL); + +done: + return error; +} + +static int create_index_error(int error, const char *msg) +{ + git_error_set_str(GIT_ERROR_INDEX, msg); + return error; +} + +int git_index_set_caps(git_index *index, int caps) +{ + unsigned int old_ignore_case; + + GIT_ASSERT_ARG(index); + + old_ignore_case = index->ignore_case; + + if (caps == GIT_INDEX_CAPABILITY_FROM_OWNER) { + git_repository *repo = INDEX_OWNER(index); + int val; + + if (!repo) + return create_index_error( + -1, "cannot access repository to set index caps"); + + if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_IGNORECASE)) + index->ignore_case = (val != 0); + if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_FILEMODE)) + index->distrust_filemode = (val == 0); + if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_SYMLINKS)) + index->no_symlinks = (val == 0); + } + else { + index->ignore_case = ((caps & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0); + index->distrust_filemode = ((caps & GIT_INDEX_CAPABILITY_NO_FILEMODE) != 0); + index->no_symlinks = ((caps & GIT_INDEX_CAPABILITY_NO_SYMLINKS) != 0); + } + + if (old_ignore_case != index->ignore_case) { + git_index__set_ignore_case(index, (bool)index->ignore_case); + } + + return 0; +} + +int git_index_caps(const git_index *index) +{ + return ((index->ignore_case ? GIT_INDEX_CAPABILITY_IGNORE_CASE : 0) | + (index->distrust_filemode ? GIT_INDEX_CAPABILITY_NO_FILEMODE : 0) | + (index->no_symlinks ? GIT_INDEX_CAPABILITY_NO_SYMLINKS : 0)); +} + +#ifndef GIT_DEPRECATE_HARD +const git_oid *git_index_checksum(git_index *index) +{ + return (git_oid *)index->checksum; +} +#endif + +/** + * Returns 1 for changed, 0 for not changed and <0 for errors + */ +static int compare_checksum(git_index *index) +{ + int fd; + ssize_t bytes_read; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + size_t checksum_size = git_oid_size(index->oid_type); + + if ((fd = p_open(index->index_file_path, O_RDONLY)) < 0) + return fd; + + if (p_lseek(fd, (0 - (ssize_t)checksum_size), SEEK_END) < 0) { + p_close(fd); + git_error_set(GIT_ERROR_OS, "failed to seek to end of file"); + return -1; + } + + bytes_read = p_read(fd, checksum, checksum_size); + p_close(fd); + + if (bytes_read < (ssize_t)checksum_size) + return -1; + + return !!memcmp(checksum, index->checksum, checksum_size); +} + +int git_index_read(git_index *index, int force) +{ + int error = 0, updated; + git_str buffer = GIT_STR_INIT; + git_futils_filestamp stamp = index->stamp; + + if (!index->index_file_path) + return create_index_error(-1, + "failed to read index: The index is in-memory only"); + + index->on_disk = git_fs_path_exists(index->index_file_path); + + if (!index->on_disk) { + if (force && (error = git_index_clear(index)) < 0) + return error; + + index->dirty = 0; + return 0; + } + + if ((updated = git_futils_filestamp_check(&stamp, index->index_file_path) < 0) || + ((updated = compare_checksum(index)) < 0)) { + git_error_set( + GIT_ERROR_INDEX, + "failed to read index: '%s' no longer exists", + index->index_file_path); + return updated; + } + + if (!updated && !force) + return 0; + + error = git_futils_readbuffer(&buffer, index->index_file_path); + if (error < 0) + return error; + + index->tree = NULL; + git_pool_clear(&index->tree_pool); + + error = git_index_clear(index); + + if (!error) + error = parse_index(index, buffer.ptr, buffer.size); + + if (!error) { + git_futils_filestamp_set(&index->stamp, &stamp); + index->dirty = 0; + } + + git_str_dispose(&buffer); + return error; +} + +int git_index_read_safely(git_index *index) +{ + if (git_index__enforce_unsaved_safety && index->dirty) { + git_error_set(GIT_ERROR_INDEX, + "the index has unsaved changes that would be overwritten by this operation"); + return GIT_EINDEXDIRTY; + } + + return git_index_read(index, false); +} + +static bool is_racy_entry(git_index *index, const git_index_entry *entry) +{ + /* Git special-cases submodules in the check */ + if (S_ISGITLINK(entry->mode)) + return false; + + return git_index_entry_newer_than_index(entry, index); +} + +/* + * Force the next diff to take a look at those entries which have the + * same timestamp as the current index. + */ +static int truncate_racily_clean(git_index *index) +{ + size_t i; + int error; + git_index_entry *entry; + git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; + git_diff *diff = NULL; + git_vector paths = GIT_VECTOR_INIT; + git_diff_delta *delta; + + /* Nothing to do if there's no repo to talk about */ + if (!INDEX_OWNER(index)) + return 0; + + /* If there's no workdir, we can't know where to even check */ + if (!git_repository_workdir(INDEX_OWNER(index))) + return 0; + + diff_opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE | GIT_DIFF_IGNORE_SUBMODULES | GIT_DIFF_DISABLE_PATHSPEC_MATCH; + git_vector_foreach(&index->entries, i, entry) { + if ((entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE) == 0 && + is_racy_entry(index, entry)) + git_vector_insert(&paths, (char *)entry->path); + } + + if (paths.length == 0) + goto done; + + diff_opts.pathspec.count = paths.length; + diff_opts.pathspec.strings = (char **)paths.contents; + + if ((error = git_diff_index_to_workdir(&diff, INDEX_OWNER(index), index, &diff_opts)) < 0) { + git_vector_dispose(&paths); + return error; + } + + git_vector_foreach(&diff->deltas, i, delta) { + entry = (git_index_entry *)git_index_get_bypath(index, delta->old_file.path, 0); + + /* Ensure that we have a stage 0 for this file (ie, it's not a + * conflict), otherwise smudging it is quite pointless. + */ + if (entry) { + entry->file_size = 0; + index->dirty = 1; + } + } + +done: + git_diff_free(diff); + git_vector_dispose(&paths); + return 0; +} + +unsigned git_index_version(git_index *index) +{ + GIT_ASSERT_ARG(index); + + return index->version; +} + +int git_index_set_version(git_index *index, unsigned int version) +{ + GIT_ASSERT_ARG(index); + + if (version < INDEX_VERSION_NUMBER_LB || + version > INDEX_VERSION_NUMBER_UB) { + git_error_set(GIT_ERROR_INDEX, "invalid version number"); + return -1; + } + + index->version = version; + + return 0; +} + +int git_index_write(git_index *index) +{ + git_indexwriter writer = GIT_INDEXWRITER_INIT; + int error; + + truncate_racily_clean(index); + + if ((error = git_indexwriter_init(&writer, index)) == 0 && + (error = git_indexwriter_commit(&writer)) == 0) + index->dirty = 0; + + git_indexwriter_cleanup(&writer); + + return error; +} + +const char *git_index_path(const git_index *index) +{ + GIT_ASSERT_ARG_WITH_RETVAL(index, NULL); + return index->index_file_path; +} + +int git_index_write_tree(git_oid *oid, git_index *index) +{ + git_repository *repo; + + GIT_ASSERT_ARG(oid); + GIT_ASSERT_ARG(index); + + repo = INDEX_OWNER(index); + + if (repo == NULL) + return create_index_error(-1, "Failed to write tree. " + "the index file is not backed up by an existing repository"); + + return git_tree__write_index(oid, index, repo); +} + +int git_index_write_tree_to( + git_oid *oid, git_index *index, git_repository *repo) +{ + GIT_ASSERT_ARG(oid); + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(repo); + + return git_tree__write_index(oid, index, repo); +} + +size_t git_index_entrycount(const git_index *index) +{ + GIT_ASSERT_ARG(index); + + return index->entries.length; +} + +const git_index_entry *git_index_get_byindex( + git_index *index, size_t n) +{ + GIT_ASSERT_ARG_WITH_RETVAL(index, NULL); + + git_vector_sort(&index->entries); + return git_vector_get(&index->entries, n); +} + +const git_index_entry *git_index_get_bypath( + git_index *index, const char *path, int stage) +{ + git_index_entry key = {{ 0 }}; + git_index_entry *value; + + GIT_ASSERT_ARG_WITH_RETVAL(index, NULL); + + key.path = path; + GIT_INDEX_ENTRY_STAGE_SET(&key, stage); + + if (git_index_entrymap_get(&value, &index->entries_map, &key) != 0) { + git_error_set(GIT_ERROR_INDEX, "index does not contain '%s'", path); + return NULL; + } + + return value; +} + +void git_index_entry__init_from_stat( + git_index_entry *entry, struct stat *st, bool trust_mode) +{ + entry->ctime.seconds = (int32_t)st->st_ctime; + entry->mtime.seconds = (int32_t)st->st_mtime; +#if defined(GIT_NSEC) + entry->mtime.nanoseconds = st->st_mtime_nsec; + entry->ctime.nanoseconds = st->st_ctime_nsec; +#endif + entry->dev = st->st_rdev; + entry->ino = st->st_ino; + entry->mode = (!trust_mode && S_ISREG(st->st_mode)) ? + git_index__create_mode(0666) : git_index__create_mode(st->st_mode); + entry->uid = st->st_uid; + entry->gid = st->st_gid; + entry->file_size = (uint32_t)st->st_size; +} + +static void index_entry_adjust_namemask( + git_index_entry *entry, + size_t path_length) +{ + entry->flags &= ~GIT_INDEX_ENTRY_NAMEMASK; + + if (path_length < GIT_INDEX_ENTRY_NAMEMASK) + entry->flags |= path_length & GIT_INDEX_ENTRY_NAMEMASK; + else + entry->flags |= GIT_INDEX_ENTRY_NAMEMASK; +} + +/* When `from_workdir` is true, we will validate the paths to avoid placing + * paths that are invalid for the working directory on the current filesystem + * (eg, on Windows, we will disallow `GIT~1`, `AUX`, `COM1`, etc). This + * function will *always* prevent `.git` and directory traversal `../` from + * being added to the index. + */ +static int index_entry_create( + git_index_entry **out, + git_repository *repo, + const char *path, + struct stat *st, + bool from_workdir) +{ + size_t pathlen = strlen(path), alloclen; + struct entry_internal *entry; + unsigned int path_valid_flags = GIT_PATH_REJECT_INDEX_DEFAULTS; + uint16_t mode = 0; + + /* always reject placing `.git` in the index and directory traversal. + * when requested, disallow platform-specific filenames and upgrade to + * the platform-specific `.git` tests (eg, `git~1`, etc). + */ + if (from_workdir) + path_valid_flags |= GIT_PATH_REJECT_WORKDIR_DEFAULTS; + if (st) + mode = st->st_mode; + + if (!git_path_is_valid(repo, path, mode, path_valid_flags)) { + git_error_set(GIT_ERROR_INDEX, "invalid path: '%s'", path); + return -1; + } + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(struct entry_internal), pathlen); + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); + entry = git__calloc(1, alloclen); + GIT_ERROR_CHECK_ALLOC(entry); + + entry->pathlen = pathlen; + memcpy(entry->path, path, pathlen); + entry->entry.path = entry->path; + + *out = (git_index_entry *)entry; + return 0; +} + +static int index_entry_init( + git_index_entry **entry_out, + git_index *index, + const char *rel_path) +{ + int error = 0; + git_index_entry *entry = NULL; + git_str path = GIT_STR_INIT; + struct stat st; + git_oid oid; + git_repository *repo; + + if (INDEX_OWNER(index) == NULL) + return create_index_error(-1, + "could not initialize index entry. " + "Index is not backed up by an existing repository."); + + /* + * FIXME: this is duplicated with the work in + * git_blob__create_from_paths. It should accept an optional stat + * structure so we can pass in the one we have to do here. + */ + repo = INDEX_OWNER(index); + if (git_repository__ensure_not_bare(repo, "create blob from file") < 0) + return GIT_EBAREREPO; + + if (git_repository_workdir_path(&path, repo, rel_path) < 0) + return -1; + + error = git_fs_path_lstat(path.ptr, &st); + git_str_dispose(&path); + + if (error < 0) + return error; + + if (index_entry_create(&entry, INDEX_OWNER(index), rel_path, &st, true) < 0) + return -1; + + /* write the blob to disk and get the oid and stat info */ + error = git_blob__create_from_paths( + &oid, &st, INDEX_OWNER(index), NULL, rel_path, 0, true); + + if (error < 0) { + index_entry_free(entry); + return error; + } + + entry->id = oid; + git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode); + + *entry_out = (git_index_entry *)entry; + return 0; +} + +static git_index_reuc_entry *reuc_entry_alloc(const char *path) +{ + size_t pathlen = strlen(path), + structlen = sizeof(struct reuc_entry_internal), + alloclen; + struct reuc_entry_internal *entry; + + if (GIT_ADD_SIZET_OVERFLOW(&alloclen, structlen, pathlen) || + GIT_ADD_SIZET_OVERFLOW(&alloclen, alloclen, 1)) + return NULL; + + entry = git__calloc(1, alloclen); + if (!entry) + return NULL; + + entry->pathlen = pathlen; + memcpy(entry->path, path, pathlen); + entry->entry.path = entry->path; + + return (git_index_reuc_entry *)entry; +} + +static int index_entry_reuc_init(git_index_reuc_entry **reuc_out, + const char *path, + int ancestor_mode, const git_oid *ancestor_oid, + int our_mode, const git_oid *our_oid, + int their_mode, const git_oid *their_oid) +{ + git_index_reuc_entry *reuc = NULL; + + GIT_ASSERT_ARG(reuc_out); + GIT_ASSERT_ARG(path); + + *reuc_out = reuc = reuc_entry_alloc(path); + GIT_ERROR_CHECK_ALLOC(reuc); + + if ((reuc->mode[0] = ancestor_mode) > 0) { + GIT_ASSERT(ancestor_oid); + git_oid_cpy(&reuc->oid[0], ancestor_oid); + } + + if ((reuc->mode[1] = our_mode) > 0) { + GIT_ASSERT(our_oid); + git_oid_cpy(&reuc->oid[1], our_oid); + } + + if ((reuc->mode[2] = their_mode) > 0) { + GIT_ASSERT(their_oid); + git_oid_cpy(&reuc->oid[2], their_oid); + } + + return 0; +} + +static void index_entry_cpy( + git_index_entry *tgt, + const git_index_entry *src) +{ + const char *tgt_path = tgt->path; + memcpy(tgt, src, sizeof(*tgt)); + tgt->path = tgt_path; +} + +static int index_entry_dup( + git_index_entry **out, + git_index *index, + const git_index_entry *src) +{ + if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0) + return -1; + + index_entry_cpy(*out, src); + return 0; +} + +static void index_entry_cpy_nocache( + git_index_entry *tgt, + const git_index_entry *src) +{ + git_oid_cpy(&tgt->id, &src->id); + tgt->mode = src->mode; + tgt->flags = src->flags; + tgt->flags_extended = (src->flags_extended & GIT_INDEX_ENTRY_EXTENDED_FLAGS); +} + +static int index_entry_dup_nocache( + git_index_entry **out, + git_index *index, + const git_index_entry *src) +{ + if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0) + return -1; + + index_entry_cpy_nocache(*out, src); + return 0; +} + +static int has_file_name(git_index *index, + const git_index_entry *entry, size_t pos, int ok_to_replace) +{ + size_t len = strlen(entry->path); + int stage = GIT_INDEX_ENTRY_STAGE(entry); + const char *name = entry->path; + + while (pos < index->entries.length) { + struct entry_internal *p = index->entries.contents[pos++]; + + if (len >= p->pathlen) + break; + if (memcmp(name, p->path, len)) + break; + if (GIT_INDEX_ENTRY_STAGE(&p->entry) != stage) + continue; + if (p->path[len] != '/') + continue; + if (!ok_to_replace) + return -1; + + if (index_remove_entry(index, --pos) < 0) + break; + } + return 0; +} + +/* + * Do we have another file with a pathname that is a proper + * subset of the name we're trying to add? + */ +static int has_dir_name(git_index *index, + const git_index_entry *entry, int ok_to_replace) +{ + int stage = GIT_INDEX_ENTRY_STAGE(entry); + const char *name = entry->path; + const char *slash = name + strlen(name); + + for (;;) { + size_t len, pos; + + for (;;) { + slash--; + + if (slash <= entry->path) + return 0; + + if (*slash == '/') + break; + } + len = slash - name; + + if (!index_find(&pos, index, name, len, stage)) { + if (!ok_to_replace) + return -1; + + if (index_remove_entry(index, pos) < 0) + break; + continue; + } + + /* + * Trivial optimization: if we find an entry that + * already matches the sub-directory, then we know + * we're ok, and we can exit. + */ + for (; pos < index->entries.length; ++pos) { + struct entry_internal *p = index->entries.contents[pos]; + + if (p->pathlen <= len || + p->path[len] != '/' || + memcmp(p->path, name, len)) + break; /* not our subdirectory */ + + if (GIT_INDEX_ENTRY_STAGE(&p->entry) == stage) + return 0; + } + } + + return 0; +} + +static int check_file_directory_collision(git_index *index, + git_index_entry *entry, size_t pos, int ok_to_replace) +{ + if (has_file_name(index, entry, pos, ok_to_replace) < 0 || + has_dir_name(index, entry, ok_to_replace) < 0) { + git_error_set(GIT_ERROR_INDEX, + "'%s' appears as both a file and a directory", entry->path); + return -1; + } + + return 0; +} + +static int canonicalize_directory_path( + git_index *index, + git_index_entry *entry, + git_index_entry *existing) +{ + const git_index_entry *match, *best = NULL; + char *search, *sep; + size_t pos, search_len, best_len; + + if (!index->ignore_case) + return 0; + + /* item already exists in the index, simply re-use the existing case */ + if (existing) { + memcpy((char *)entry->path, existing->path, strlen(existing->path)); + return 0; + } + + /* nothing to do */ + if (strchr(entry->path, '/') == NULL) + return 0; + + if ((search = git__strdup(entry->path)) == NULL) + return -1; + + /* starting at the parent directory and descending to the root, find the + * common parent directory. + */ + while (!best && (sep = strrchr(search, '/'))) { + sep[1] = '\0'; + + search_len = strlen(search); + + git_vector_bsearch2( + &pos, &index->entries, index->entries_search_path, search); + + while ((match = git_vector_get(&index->entries, pos))) { + if (GIT_INDEX_ENTRY_STAGE(match) != 0) { + /* conflicts do not contribute to canonical paths */ + } else if (strncmp(search, match->path, search_len) == 0) { + /* prefer an exact match to the input filename */ + best = match; + best_len = search_len; + break; + } else if (strncasecmp(search, match->path, search_len) == 0) { + /* continue walking, there may be a path with an exact + * (case sensitive) match later in the index, but use this + * as the best match until that happens. + */ + if (!best) { + best = match; + best_len = search_len; + } + } else { + break; + } + + pos++; + } + + sep[0] = '\0'; + } + + if (best) + memcpy((char *)entry->path, best->path, best_len); + + git__free(search); + return 0; +} + +static int index_no_dups(void **old, void *new) +{ + const git_index_entry *entry = new; + GIT_UNUSED(old); + git_error_set(GIT_ERROR_INDEX, "'%s' appears multiple times at stage %d", + entry->path, GIT_INDEX_ENTRY_STAGE(entry)); + return GIT_EEXISTS; +} + +static void index_existing_and_best( + git_index_entry **existing, + size_t *existing_position, + git_index_entry **best, + git_index *index, + const git_index_entry *entry) +{ + git_index_entry *e; + size_t pos; + int error; + + error = index_find(&pos, + index, entry->path, 0, GIT_INDEX_ENTRY_STAGE(entry)); + + if (error == 0) { + *existing = index->entries.contents[pos]; + *existing_position = pos; + *best = index->entries.contents[pos]; + return; + } + + *existing = NULL; + *existing_position = 0; + *best = NULL; + + if (GIT_INDEX_ENTRY_STAGE(entry) == 0) { + for (; pos < index->entries.length; pos++) { + int (*strcomp)(const char *a, const char *b) = + index->ignore_case ? git__strcasecmp : git__strcmp; + + e = index->entries.contents[pos]; + + if (strcomp(entry->path, e->path) != 0) + break; + + if (GIT_INDEX_ENTRY_STAGE(e) == GIT_INDEX_STAGE_ANCESTOR) { + *best = e; + continue; + } else { + *best = e; + break; + } + } + } +} + +/* index_insert takes ownership of the new entry - if it can't insert + * it, then it will return an error **and also free the entry**. When + * it replaces an existing entry, it will update the entry_ptr with the + * actual entry in the index (and free the passed in one). + * + * trust_path is whether we use the given path, or whether (on case + * insensitive systems only) we try to canonicalize the given path to + * be within an existing directory. + * + * trust_mode is whether we trust the mode in entry_ptr. + * + * trust_id is whether we trust the id or it should be validated. + */ +static int index_insert( + git_index *index, + git_index_entry **entry_ptr, + int replace, + bool trust_path, + bool trust_mode, + bool trust_id) +{ + git_index_entry *existing, *best, *entry; + size_t path_length, position; + int error; + + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(entry_ptr); + + entry = *entry_ptr; + + /* Make sure that the path length flag is correct */ + path_length = ((struct entry_internal *)entry)->pathlen; + index_entry_adjust_namemask(entry, path_length); + + /* This entry is now up-to-date and should not be checked for raciness */ + entry->flags_extended |= GIT_INDEX_ENTRY_UPTODATE; + + git_vector_sort(&index->entries); + + /* + * Look if an entry with this path already exists, either staged, or (if + * this entry is a regular staged item) as the "ours" side of a conflict. + */ + index_existing_and_best(&existing, &position, &best, index, entry); + + /* Update the file mode */ + entry->mode = trust_mode ? + git_index__create_mode(entry->mode) : + index_merge_mode(index, best, entry->mode); + + /* Canonicalize the directory name */ + if (!trust_path && (error = canonicalize_directory_path(index, entry, best)) < 0) + goto out; + + /* Ensure that the given id exists (unless it's a submodule) */ + if (!trust_id && INDEX_OWNER(index) && + (entry->mode & GIT_FILEMODE_COMMIT) != GIT_FILEMODE_COMMIT) { + + if (!git_object__is_valid(INDEX_OWNER(index), &entry->id, + git_object__type_from_filemode(entry->mode))) { + error = -1; + goto out; + } + } + + /* Look for tree / blob name collisions, removing conflicts if requested */ + if ((error = check_file_directory_collision(index, entry, position, replace)) < 0) + goto out; + + /* + * If we are replacing an existing item, overwrite the existing entry + * and return it in place of the passed in one. + */ + if (existing) { + if (replace) { + index_entry_cpy(existing, entry); + + if (trust_path) + memcpy((char *)existing->path, entry->path, strlen(entry->path)); + } + + index_entry_free(entry); + *entry_ptr = existing; + } else { + /* + * If replace is not requested or no existing entry exists, insert + * at the sorted position. (Since we re-sort after each insert to + * check for dups, this is actually cheaper in the long run.) + */ + if ((error = git_vector_insert_sorted(&index->entries, entry, index_no_dups)) < 0 || + (error = git_index_entrymap_put(&index->entries_map, entry)) < 0) + goto out; + } + + index->dirty = 1; + +out: + if (error < 0) { + index_entry_free(*entry_ptr); + *entry_ptr = NULL; + } + + return error; +} + +static int index_conflict_to_reuc(git_index *index, const char *path) +{ + const git_index_entry *conflict_entries[3]; + int ancestor_mode, our_mode, their_mode; + git_oid const *ancestor_oid, *our_oid, *their_oid; + int ret; + + if ((ret = git_index_conflict_get(&conflict_entries[0], + &conflict_entries[1], &conflict_entries[2], index, path)) < 0) + return ret; + + ancestor_mode = conflict_entries[0] == NULL ? 0 : conflict_entries[0]->mode; + our_mode = conflict_entries[1] == NULL ? 0 : conflict_entries[1]->mode; + their_mode = conflict_entries[2] == NULL ? 0 : conflict_entries[2]->mode; + + ancestor_oid = conflict_entries[0] == NULL ? NULL : &conflict_entries[0]->id; + our_oid = conflict_entries[1] == NULL ? NULL : &conflict_entries[1]->id; + their_oid = conflict_entries[2] == NULL ? NULL : &conflict_entries[2]->id; + + if ((ret = git_index_reuc_add(index, path, ancestor_mode, ancestor_oid, + our_mode, our_oid, their_mode, their_oid)) >= 0) + ret = git_index_conflict_remove(index, path); + + return ret; +} + +GIT_INLINE(bool) is_file_or_link(const int filemode) +{ + return (filemode == GIT_FILEMODE_BLOB || + filemode == GIT_FILEMODE_BLOB_EXECUTABLE || + filemode == GIT_FILEMODE_LINK); +} + +GIT_INLINE(bool) valid_filemode(const int filemode) +{ + return (is_file_or_link(filemode) || filemode == GIT_FILEMODE_COMMIT); +} + +int git_index_add_from_buffer( + git_index *index, const git_index_entry *source_entry, + const void *buffer, size_t len) +{ + git_index_entry *entry = NULL; + int error = 0; + git_oid id; + + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(source_entry && source_entry->path); + + if (INDEX_OWNER(index) == NULL) + return create_index_error(-1, + "could not initialize index entry. " + "Index is not backed up by an existing repository."); + + if (!is_file_or_link(source_entry->mode)) { + git_error_set(GIT_ERROR_INDEX, "invalid filemode"); + return -1; + } + + if (len > UINT32_MAX) { + git_error_set(GIT_ERROR_INDEX, "buffer is too large"); + return -1; + } + + if (index_entry_dup(&entry, index, source_entry) < 0) + return -1; + + error = git_blob_create_from_buffer(&id, INDEX_OWNER(index), buffer, len); + if (error < 0) { + index_entry_free(entry); + return error; + } + + git_oid_cpy(&entry->id, &id); + entry->file_size = (uint32_t)len; + + if ((error = index_insert(index, &entry, 1, true, true, true)) < 0) + return error; + + /* Adding implies conflict was resolved, move conflict entries to REUC */ + if ((error = index_conflict_to_reuc(index, entry->path)) < 0 && error != GIT_ENOTFOUND) + return error; + + git_tree_cache_invalidate_path(index->tree, entry->path); + return 0; +} + +static int add_repo_as_submodule(git_index_entry **out, git_index *index, const char *path) +{ + git_repository *sub; + git_str abspath = GIT_STR_INIT; + git_repository *repo = INDEX_OWNER(index); + git_reference *head; + git_index_entry *entry; + struct stat st; + int error; + + if ((error = git_repository_workdir_path(&abspath, repo, path)) < 0) + return error; + + if ((error = p_stat(abspath.ptr, &st)) < 0) { + git_error_set(GIT_ERROR_OS, "failed to stat repository dir"); + return -1; + } + + if (index_entry_create(&entry, INDEX_OWNER(index), path, &st, true) < 0) + return -1; + + git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode); + + if ((error = git_repository_open(&sub, abspath.ptr)) < 0) + return error; + + if ((error = git_repository_head(&head, sub)) < 0) + return error; + + git_oid_cpy(&entry->id, git_reference_target(head)); + entry->mode = GIT_FILEMODE_COMMIT; + + git_reference_free(head); + git_repository_free(sub); + git_str_dispose(&abspath); + + *out = entry; + return 0; +} + +int git_index_add_bypath(git_index *index, const char *path) +{ + git_index_entry *entry = NULL; + int ret; + + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(path); + + if ((ret = index_entry_init(&entry, index, path)) == 0) + ret = index_insert(index, &entry, 1, false, false, true); + + /* If we were given a directory, let's see if it's a submodule */ + if (ret < 0 && ret != GIT_EDIRECTORY) + return ret; + + if (ret == GIT_EDIRECTORY) { + git_submodule *sm; + git_error *last_error; + + git_error_save(&last_error); + + ret = git_submodule_lookup(&sm, INDEX_OWNER(index), path); + if (ret == GIT_ENOTFOUND) { + git_error_restore(last_error); + return GIT_EDIRECTORY; + } + + git_error_free(last_error); + + /* + * EEXISTS means that there is a repository at that path, but it's not known + * as a submodule. We add its HEAD as an entry and don't register it. + */ + if (ret == GIT_EEXISTS) { + if ((ret = add_repo_as_submodule(&entry, index, path)) < 0) + return ret; + + if ((ret = index_insert(index, &entry, 1, false, false, true)) < 0) + return ret; + } else if (ret < 0) { + return ret; + } else { + ret = git_submodule_add_to_index(sm, false); + git_submodule_free(sm); + return ret; + } + } + + /* Adding implies conflict was resolved, move conflict entries to REUC */ + if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND) + return ret; + + git_tree_cache_invalidate_path(index->tree, entry->path); + return 0; +} + +int git_index_remove_bypath(git_index *index, const char *path) +{ + int ret; + + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(path); + + if (((ret = git_index_remove(index, path, 0)) < 0 && + ret != GIT_ENOTFOUND) || + ((ret = index_conflict_to_reuc(index, path)) < 0 && + ret != GIT_ENOTFOUND)) + return ret; + + if (ret == GIT_ENOTFOUND) + git_error_clear(); + + return 0; +} + +int git_index__fill(git_index *index, const git_vector *source_entries) +{ + const git_index_entry *source_entry = NULL; + int error = 0; + size_t i; + + GIT_ASSERT_ARG(index); + + if (!source_entries->length) + return 0; + + if (git_vector_size_hint(&index->entries, source_entries->length) < 0 || + git_index_entrymap_resize(&index->entries_map, (size_t)(source_entries->length * 1.3)) < 0) + return -1; + + git_vector_foreach(source_entries, i, source_entry) { + git_index_entry *entry = NULL; + + if ((error = index_entry_dup(&entry, index, source_entry)) < 0) + break; + + index_entry_adjust_namemask(entry, ((struct entry_internal *)entry)->pathlen); + entry->flags_extended |= GIT_INDEX_ENTRY_UPTODATE; + entry->mode = git_index__create_mode(entry->mode); + + if ((error = git_vector_insert(&index->entries, entry)) < 0 || + (error = git_index_entrymap_put(&index->entries_map, entry)) < 0) + break; + + index->dirty = 1; + } + + if (!error) + git_vector_sort(&index->entries); + + return error; +} + + +int git_index_add(git_index *index, const git_index_entry *source_entry) +{ + git_index_entry *entry = NULL; + int ret; + + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(source_entry && source_entry->path); + + if (!valid_filemode(source_entry->mode)) { + git_error_set(GIT_ERROR_INDEX, "invalid entry mode"); + return -1; + } + + if ((ret = index_entry_dup(&entry, index, source_entry)) < 0 || + (ret = index_insert(index, &entry, 1, true, true, false)) < 0) + return ret; + + git_tree_cache_invalidate_path(index->tree, entry->path); + return 0; +} + +int git_index_remove(git_index *index, const char *path, int stage) +{ + int error; + size_t position; + git_index_entry remove_key = {{ 0 }}; + + remove_key.path = path; + GIT_INDEX_ENTRY_STAGE_SET(&remove_key, stage); + + git_index_entrymap_remove(&index->entries_map, &remove_key); + + if (index_find(&position, index, path, 0, stage) < 0) { + git_error_set( + GIT_ERROR_INDEX, "index does not contain %s at stage %d", path, stage); + error = GIT_ENOTFOUND; + } else { + error = index_remove_entry(index, position); + } + + return error; +} + +int git_index_remove_directory(git_index *index, const char *dir, int stage) +{ + git_str pfx = GIT_STR_INIT; + int error = 0; + size_t pos; + git_index_entry *entry; + + if (!(error = git_str_sets(&pfx, dir)) && + !(error = git_fs_path_to_dir(&pfx))) + index_find(&pos, index, pfx.ptr, pfx.size, GIT_INDEX_STAGE_ANY); + + while (!error) { + entry = git_vector_get(&index->entries, pos); + if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0) + break; + + if (GIT_INDEX_ENTRY_STAGE(entry) != stage) { + ++pos; + continue; + } + + error = index_remove_entry(index, pos); + + /* removed entry at 'pos' so we don't need to increment */ + } + + git_str_dispose(&pfx); + + return error; +} + +int git_index_find_prefix(size_t *at_pos, git_index *index, const char *prefix) +{ + int error = 0; + size_t pos; + const git_index_entry *entry; + + index_find(&pos, index, prefix, strlen(prefix), GIT_INDEX_STAGE_ANY); + entry = git_vector_get(&index->entries, pos); + if (!entry || git__prefixcmp(entry->path, prefix) != 0) + error = GIT_ENOTFOUND; + + if (!error && at_pos) + *at_pos = pos; + + return error; +} + +int git_index__find_pos( + size_t *out, git_index *index, const char *path, size_t path_len, int stage) +{ + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(path); + return index_find(out, index, path, path_len, stage); +} + +int git_index_find(size_t *at_pos, git_index *index, const char *path) +{ + size_t pos; + + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(path); + + if (git_vector_bsearch2( + &pos, &index->entries, index->entries_search_path, path) < 0) { + git_error_set(GIT_ERROR_INDEX, "index does not contain %s", path); + return GIT_ENOTFOUND; + } + + /* Since our binary search only looked at path, we may be in the + * middle of a list of stages. + */ + for (; pos > 0; --pos) { + const git_index_entry *prev = git_vector_get(&index->entries, pos - 1); + + if (index->entries_cmp_path(prev->path, path) != 0) + break; + } + + if (at_pos) + *at_pos = pos; + + return 0; +} + +int git_index_conflict_add(git_index *index, + const git_index_entry *ancestor_entry, + const git_index_entry *our_entry, + const git_index_entry *their_entry) +{ + git_index_entry *entries[3] = { 0 }; + unsigned short i; + int ret = 0; + + GIT_ASSERT_ARG(index); + + if ((ancestor_entry && + (ret = index_entry_dup(&entries[0], index, ancestor_entry)) < 0) || + (our_entry && + (ret = index_entry_dup(&entries[1], index, our_entry)) < 0) || + (their_entry && + (ret = index_entry_dup(&entries[2], index, their_entry)) < 0)) + goto on_error; + + /* Validate entries */ + for (i = 0; i < 3; i++) { + if (entries[i] && !valid_filemode(entries[i]->mode)) { + git_error_set(GIT_ERROR_INDEX, "invalid filemode for stage %d entry", + i + 1); + ret = -1; + goto on_error; + } + } + + /* Remove existing index entries for each path */ + for (i = 0; i < 3; i++) { + if (entries[i] == NULL) + continue; + + if ((ret = git_index_remove(index, entries[i]->path, 0)) != 0) { + if (ret != GIT_ENOTFOUND) + goto on_error; + + git_error_clear(); + ret = 0; + } + } + + /* Add the conflict entries */ + for (i = 0; i < 3; i++) { + if (entries[i] == NULL) + continue; + + /* Make sure stage is correct */ + GIT_INDEX_ENTRY_STAGE_SET(entries[i], i + 1); + + if ((ret = index_insert(index, &entries[i], 1, true, true, false)) < 0) + goto on_error; + + entries[i] = NULL; /* don't free if later entry fails */ + } + + return 0; + +on_error: + for (i = 0; i < 3; i++) { + if (entries[i] != NULL) + index_entry_free(entries[i]); + } + + return ret; +} + +static int index_conflict__get_byindex( + const git_index_entry **ancestor_out, + const git_index_entry **our_out, + const git_index_entry **their_out, + git_index *index, + size_t n) +{ + const git_index_entry *conflict_entry; + const char *path = NULL; + size_t count; + int stage, len = 0; + + GIT_ASSERT_ARG(ancestor_out); + GIT_ASSERT_ARG(our_out); + GIT_ASSERT_ARG(their_out); + GIT_ASSERT_ARG(index); + + *ancestor_out = NULL; + *our_out = NULL; + *their_out = NULL; + + for (count = git_index_entrycount(index); n < count; ++n) { + conflict_entry = git_vector_get(&index->entries, n); + + if (path && index->entries_cmp_path(conflict_entry->path, path) != 0) + break; + + stage = GIT_INDEX_ENTRY_STAGE(conflict_entry); + path = conflict_entry->path; + + switch (stage) { + case 3: + *their_out = conflict_entry; + len++; + break; + case 2: + *our_out = conflict_entry; + len++; + break; + case 1: + *ancestor_out = conflict_entry; + len++; + break; + default: + break; + }; + } + + return len; +} + +int git_index_conflict_get( + const git_index_entry **ancestor_out, + const git_index_entry **our_out, + const git_index_entry **their_out, + git_index *index, + const char *path) +{ + size_t pos; + int len = 0; + + GIT_ASSERT_ARG(ancestor_out); + GIT_ASSERT_ARG(our_out); + GIT_ASSERT_ARG(their_out); + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(path); + + *ancestor_out = NULL; + *our_out = NULL; + *their_out = NULL; + + if (git_index_find(&pos, index, path) < 0) + return GIT_ENOTFOUND; + + if ((len = index_conflict__get_byindex( + ancestor_out, our_out, their_out, index, pos)) < 0) + return len; + else if (len == 0) + return GIT_ENOTFOUND; + + return 0; +} + +static int index_conflict_remove(git_index *index, const char *path) +{ + size_t pos = 0; + git_index_entry *conflict_entry; + int error = 0; + + if (path != NULL && git_index_find(&pos, index, path) < 0) + return GIT_ENOTFOUND; + + while ((conflict_entry = git_vector_get(&index->entries, pos)) != NULL) { + + if (path != NULL && + index->entries_cmp_path(conflict_entry->path, path) != 0) + break; + + if (GIT_INDEX_ENTRY_STAGE(conflict_entry) == 0) { + pos++; + continue; + } + + if ((error = index_remove_entry(index, pos)) < 0) + break; + } + + return error; +} + +int git_index_conflict_remove(git_index *index, const char *path) +{ + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(path); + return index_conflict_remove(index, path); +} + +int git_index_conflict_cleanup(git_index *index) +{ + GIT_ASSERT_ARG(index); + return index_conflict_remove(index, NULL); +} + +int git_index_has_conflicts(const git_index *index) +{ + size_t i; + git_index_entry *entry; + + GIT_ASSERT_ARG(index); + + git_vector_foreach(&index->entries, i, entry) { + if (GIT_INDEX_ENTRY_STAGE(entry) > 0) + return 1; + } + + return 0; +} + +int git_index_iterator_new( + git_index_iterator **iterator_out, + git_index *index) +{ + git_index_iterator *it; + int error; + + GIT_ASSERT_ARG(iterator_out); + GIT_ASSERT_ARG(index); + + it = git__calloc(1, sizeof(git_index_iterator)); + GIT_ERROR_CHECK_ALLOC(it); + + if ((error = git_index_snapshot_new(&it->snap, index)) < 0) { + git__free(it); + return error; + } + + it->index = index; + + *iterator_out = it; + return 0; +} + +int git_index_iterator_next( + const git_index_entry **out, + git_index_iterator *it) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(it); + + if (it->cur >= git_vector_length(&it->snap)) + return GIT_ITEROVER; + + *out = (git_index_entry *)git_vector_get(&it->snap, it->cur++); + return 0; +} + +void git_index_iterator_free(git_index_iterator *it) +{ + if (it == NULL) + return; + + git_index_snapshot_release(&it->snap, it->index); + git__free(it); +} + +int git_index_conflict_iterator_new( + git_index_conflict_iterator **iterator_out, + git_index *index) +{ + git_index_conflict_iterator *it = NULL; + + GIT_ASSERT_ARG(iterator_out); + GIT_ASSERT_ARG(index); + + it = git__calloc(1, sizeof(git_index_conflict_iterator)); + GIT_ERROR_CHECK_ALLOC(it); + + it->index = index; + + *iterator_out = it; + return 0; +} + +int git_index_conflict_next( + const git_index_entry **ancestor_out, + const git_index_entry **our_out, + const git_index_entry **their_out, + git_index_conflict_iterator *iterator) +{ + const git_index_entry *entry; + int len; + + GIT_ASSERT_ARG(ancestor_out); + GIT_ASSERT_ARG(our_out); + GIT_ASSERT_ARG(their_out); + GIT_ASSERT_ARG(iterator); + + *ancestor_out = NULL; + *our_out = NULL; + *their_out = NULL; + + while (iterator->cur < iterator->index->entries.length) { + entry = git_index_get_byindex(iterator->index, iterator->cur); + + if (git_index_entry_is_conflict(entry)) { + if ((len = index_conflict__get_byindex( + ancestor_out, + our_out, + their_out, + iterator->index, + iterator->cur)) < 0) + return len; + + iterator->cur += len; + return 0; + } + + iterator->cur++; + } + + return GIT_ITEROVER; +} + +void git_index_conflict_iterator_free(git_index_conflict_iterator *iterator) +{ + if (iterator == NULL) + return; + + git__free(iterator); +} + +size_t git_index_name_entrycount(git_index *index) +{ + GIT_ASSERT_ARG(index); + return index->names.length; +} + +const git_index_name_entry *git_index_name_get_byindex( + git_index *index, size_t n) +{ + GIT_ASSERT_ARG_WITH_RETVAL(index, NULL); + + git_vector_sort(&index->names); + return git_vector_get(&index->names, n); +} + +static void index_name_entry_free(git_index_name_entry *ne) +{ + if (!ne) + return; + git__free(ne->ancestor); + git__free(ne->ours); + git__free(ne->theirs); + git__free(ne); +} + +int git_index_name_add(git_index *index, + const char *ancestor, const char *ours, const char *theirs) +{ + git_index_name_entry *conflict_name; + + GIT_ASSERT_ARG((ancestor && ours) || (ancestor && theirs) || (ours && theirs)); + + conflict_name = git__calloc(1, sizeof(git_index_name_entry)); + GIT_ERROR_CHECK_ALLOC(conflict_name); + + if ((ancestor && !(conflict_name->ancestor = git__strdup(ancestor))) || + (ours && !(conflict_name->ours = git__strdup(ours))) || + (theirs && !(conflict_name->theirs = git__strdup(theirs))) || + git_vector_insert(&index->names, conflict_name) < 0) + { + index_name_entry_free(conflict_name); + return -1; + } + + index->dirty = 1; + return 0; +} + +int git_index_name_clear(git_index *index) +{ + size_t i; + git_index_name_entry *conflict_name; + + GIT_ASSERT_ARG(index); + + git_vector_foreach(&index->names, i, conflict_name) + index_name_entry_free(conflict_name); + + git_vector_clear(&index->names); + + index->dirty = 1; + + return 0; +} + +size_t git_index_reuc_entrycount(git_index *index) +{ + GIT_ASSERT_ARG(index); + return index->reuc.length; +} + +static int index_reuc_on_dup(void **old, void *new) +{ + index_entry_reuc_free(*old); + *old = new; + return GIT_EEXISTS; +} + +static int index_reuc_insert( + git_index *index, + git_index_reuc_entry *reuc) +{ + int res; + + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(reuc && reuc->path != NULL); + GIT_ASSERT(git_vector_is_sorted(&index->reuc)); + + res = git_vector_insert_sorted(&index->reuc, reuc, &index_reuc_on_dup); + index->dirty = 1; + + return res == GIT_EEXISTS ? 0 : res; +} + +int git_index_reuc_add(git_index *index, const char *path, + int ancestor_mode, const git_oid *ancestor_oid, + int our_mode, const git_oid *our_oid, + int their_mode, const git_oid *their_oid) +{ + git_index_reuc_entry *reuc = NULL; + int error = 0; + + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(path); + + if ((error = index_entry_reuc_init(&reuc, path, ancestor_mode, + ancestor_oid, our_mode, our_oid, their_mode, their_oid)) < 0 || + (error = index_reuc_insert(index, reuc)) < 0) + index_entry_reuc_free(reuc); + + return error; +} + +int git_index_reuc_find(size_t *at_pos, git_index *index, const char *path) +{ + return git_vector_bsearch2(at_pos, &index->reuc, index->reuc_search, path); +} + +const git_index_reuc_entry *git_index_reuc_get_bypath( + git_index *index, const char *path) +{ + size_t pos; + + GIT_ASSERT_ARG_WITH_RETVAL(index, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(path, NULL); + + if (!index->reuc.length) + return NULL; + + GIT_ASSERT_WITH_RETVAL(git_vector_is_sorted(&index->reuc), NULL); + + if (git_index_reuc_find(&pos, index, path) < 0) + return NULL; + + return git_vector_get(&index->reuc, pos); +} + +const git_index_reuc_entry *git_index_reuc_get_byindex( + git_index *index, size_t n) +{ + GIT_ASSERT_ARG_WITH_RETVAL(index, NULL); + GIT_ASSERT_WITH_RETVAL(git_vector_is_sorted(&index->reuc), NULL); + + return git_vector_get(&index->reuc, n); +} + +int git_index_reuc_remove(git_index *index, size_t position) +{ + int error; + git_index_reuc_entry *reuc; + + GIT_ASSERT_ARG(index); + GIT_ASSERT(git_vector_is_sorted(&index->reuc)); + + reuc = git_vector_get(&index->reuc, position); + error = git_vector_remove(&index->reuc, position); + + if (!error) + index_entry_reuc_free(reuc); + + index->dirty = 1; + return error; +} + +int git_index_reuc_clear(git_index *index) +{ + size_t i; + + GIT_ASSERT_ARG(index); + + for (i = 0; i < index->reuc.length; ++i) + index_entry_reuc_free(git_atomic_swap(index->reuc.contents[i], NULL)); + + git_vector_clear(&index->reuc); + + index->dirty = 1; + + return 0; +} + +static int index_error_invalid(const char *message) +{ + git_error_set(GIT_ERROR_INDEX, "invalid data in index - %s", message); + return -1; +} + +static int read_reuc(git_index *index, const char *buffer, size_t size) +{ + const char *endptr; + size_t oid_size = git_oid_size(index->oid_type); + size_t len; + int i; + + /* If called multiple times, the vector might already be initialized */ + if (index->reuc._alloc_size == 0 && + git_vector_init(&index->reuc, 16, reuc_cmp) < 0) + return -1; + + while (size) { + git_index_reuc_entry *lost; + + len = p_strnlen(buffer, size) + 1; + if (size <= len) + return index_error_invalid("reading reuc entries"); + + lost = reuc_entry_alloc(buffer); + GIT_ERROR_CHECK_ALLOC(lost); + + size -= len; + buffer += len; + + /* read 3 ASCII octal numbers for stage entries */ + for (i = 0; i < 3; i++) { + int64_t tmp; + + if (git__strntol64(&tmp, buffer, size, &endptr, 8) < 0 || + !endptr || endptr == buffer || *endptr || + tmp < 0 || tmp > UINT32_MAX) { + index_entry_reuc_free(lost); + return index_error_invalid("reading reuc entry stage"); + } + + lost->mode[i] = (uint32_t)tmp; + + len = (endptr + 1) - buffer; + if (size <= len) { + index_entry_reuc_free(lost); + return index_error_invalid("reading reuc entry stage"); + } + + size -= len; + buffer += len; + } + + /* read up to 3 OIDs for stage entries */ + for (i = 0; i < 3; i++) { + if (!lost->mode[i]) + continue; + if (size < oid_size) { + index_entry_reuc_free(lost); + return index_error_invalid("reading reuc entry oid"); + } + + if (git_oid_from_raw(&lost->oid[i], (const unsigned char *) buffer, index->oid_type) < 0) + return -1; + + size -= oid_size; + buffer += oid_size; + } + + /* entry was read successfully - insert into reuc vector */ + if (git_vector_insert(&index->reuc, lost) < 0) + return -1; + } + + /* entries are guaranteed to be sorted on-disk */ + git_vector_set_sorted(&index->reuc, true); + + return 0; +} + + +static int read_conflict_names(git_index *index, const char *buffer, size_t size) +{ + size_t len; + + /* This gets called multiple times, the vector might already be initialized */ + if (index->names._alloc_size == 0 && + git_vector_init(&index->names, 16, conflict_name_cmp) < 0) + return -1; + +#define read_conflict_name(ptr) \ + len = p_strnlen(buffer, size) + 1; \ + if (size < len) { \ + index_error_invalid("reading conflict name entries"); \ + goto out_err; \ + } \ + if (len == 1) \ + ptr = NULL; \ + else { \ + ptr = git__malloc(len); \ + GIT_ERROR_CHECK_ALLOC(ptr); \ + memcpy(ptr, buffer, len); \ + } \ + \ + buffer += len; \ + size -= len; + + while (size) { + git_index_name_entry *conflict_name = git__calloc(1, sizeof(git_index_name_entry)); + GIT_ERROR_CHECK_ALLOC(conflict_name); + + read_conflict_name(conflict_name->ancestor); + read_conflict_name(conflict_name->ours); + read_conflict_name(conflict_name->theirs); + + if (git_vector_insert(&index->names, conflict_name) < 0) + goto out_err; + + continue; + +out_err: + git__free(conflict_name->ancestor); + git__free(conflict_name->ours); + git__free(conflict_name->theirs); + git__free(conflict_name); + return -1; + } + +#undef read_conflict_name + + /* entries are guaranteed to be sorted on-disk */ + git_vector_set_sorted(&index->names, true); + + return 0; +} + +GIT_INLINE(size_t) index_entry_path_offset( + git_oid_t oid_type, + uint32_t flags) +{ + if (oid_type == GIT_OID_SHA1) + return (flags & GIT_INDEX_ENTRY_EXTENDED) ? + offsetof(index_entry_long_sha1, path) : + offsetof(index_entry_short_sha1, path); + +#ifdef GIT_EXPERIMENTAL_SHA256 + else if (oid_type == GIT_OID_SHA256) + return (flags & GIT_INDEX_ENTRY_EXTENDED) ? + offsetof(index_entry_long_sha256, path) : + offsetof(index_entry_short_sha256, path); +#endif + + git_error_set(GIT_ERROR_INTERNAL, "invalid oid type"); + return 0; +} + +GIT_INLINE(size_t) index_entry_flags_offset(git_oid_t oid_type) +{ + if (oid_type == GIT_OID_SHA1) + return offsetof(index_entry_long_sha1, flags_extended); + +#ifdef GIT_EXPERIMENTAL_SHA256 + else if (oid_type == GIT_OID_SHA256) + return offsetof(index_entry_long_sha256, flags_extended); +#endif + + git_error_set(GIT_ERROR_INTERNAL, "invalid oid type"); + return 0; +} + +static size_t index_entry_size( + size_t path_len, + size_t varint_len, + git_oid_t oid_type, + uint32_t flags) +{ + size_t offset, size; + + if (!(offset = index_entry_path_offset(oid_type, flags))) + return 0; + + if (varint_len) { + if (GIT_ADD_SIZET_OVERFLOW(&size, offset, path_len) || + GIT_ADD_SIZET_OVERFLOW(&size, size, 1) || + GIT_ADD_SIZET_OVERFLOW(&size, size, varint_len)) + return 0; + } else { + if (GIT_ADD_SIZET_OVERFLOW(&size, offset, path_len) || + GIT_ADD_SIZET_OVERFLOW(&size, size, 8)) + return 0; + + size &= ~7; + } + + return size; +} + +static int read_entry( + git_index_entry **out, + size_t *out_size, + git_index *index, + size_t checksum_size, + const void *buffer, + size_t buffer_size, + const char *last) +{ + size_t path_length, path_offset, entry_size; + const char *path_ptr; + struct entry_common *source_common = NULL; + index_entry_short_sha1 source_sha1; +#ifdef GIT_EXPERIMENTAL_SHA256 + index_entry_short_sha256 source_sha256; +#endif + git_index_entry entry = {{0}}; + bool compressed = index->version >= INDEX_VERSION_NUMBER_COMP; + char *tmp_path = NULL; + + size_t minimal_entry_size = index_entry_path_offset(index->oid_type, 0); + + if (checksum_size + minimal_entry_size > buffer_size) + return -1; + + /* buffer is not guaranteed to be aligned */ + switch (index->oid_type) { + case GIT_OID_SHA1: + source_common = &source_sha1.common; + memcpy(&source_sha1, buffer, sizeof(source_sha1)); + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + source_common = &source_sha256.common; + memcpy(&source_sha256, buffer, sizeof(source_sha256)); + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + + entry.ctime.seconds = (git_time_t)ntohl(source_common->ctime.seconds); + entry.ctime.nanoseconds = ntohl(source_common->ctime.nanoseconds); + entry.mtime.seconds = (git_time_t)ntohl(source_common->mtime.seconds); + entry.mtime.nanoseconds = ntohl(source_common->mtime.nanoseconds); + entry.dev = ntohl(source_common->dev); + entry.ino = ntohl(source_common->ino); + entry.mode = ntohl(source_common->mode); + entry.uid = ntohl(source_common->uid); + entry.gid = ntohl(source_common->gid); + entry.file_size = ntohl(source_common->file_size); + + switch (index->oid_type) { + case GIT_OID_SHA1: + if (git_oid_from_raw(&entry.id, source_sha1.oid, + GIT_OID_SHA1) < 0) + return -1; + entry.flags = ntohs(source_sha1.flags); + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + if (git_oid_from_raw(&entry.id, source_sha256.oid, + GIT_OID_SHA256) < 0) + return -1; + entry.flags = ntohs(source_sha256.flags); + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + + if (!(path_offset = index_entry_path_offset(index->oid_type, entry.flags))) + return -1; + + + if (entry.flags & GIT_INDEX_ENTRY_EXTENDED) { + uint16_t flags_raw; + size_t flags_offset; + + if (!(flags_offset = index_entry_flags_offset(index->oid_type))) + return -1; + + memcpy(&flags_raw, (const char *)buffer + flags_offset, sizeof(flags_raw)); + flags_raw = ntohs(flags_raw); + + memcpy(&entry.flags_extended, &flags_raw, sizeof(flags_raw)); + path_ptr = (const char *)buffer + path_offset; + } else { + path_ptr = (const char *)buffer + path_offset; + } + + if (!compressed) { + path_length = entry.flags & GIT_INDEX_ENTRY_NAMEMASK; + + /* if this is a very long string, we must find its + * real length without overflowing */ + if (path_length == 0xFFF) { + const char *path_end; + + path_end = memchr(path_ptr, '\0', buffer_size); + if (path_end == NULL) + return index_error_invalid("invalid path name"); + + path_length = path_end - path_ptr; + } + + entry_size = index_entry_size(path_length, 0, index->oid_type, entry.flags); + entry.path = (char *)path_ptr; + } else { + size_t varint_len, last_len, prefix_len, suffix_len, path_len; + uintmax_t strip_len; + + strip_len = git_decode_varint((const unsigned char *)path_ptr, &varint_len); + last_len = strlen(last); + + if (varint_len == 0 || last_len < strip_len) + return index_error_invalid("incorrect prefix length"); + + prefix_len = last_len - (size_t)strip_len; + suffix_len = strlen(path_ptr + varint_len); + + GIT_ERROR_CHECK_ALLOC_ADD(&path_len, prefix_len, suffix_len); + GIT_ERROR_CHECK_ALLOC_ADD(&path_len, path_len, 1); + + if (path_len > GIT_PATH_MAX) + return index_error_invalid("unreasonable path length"); + + tmp_path = git__malloc(path_len); + GIT_ERROR_CHECK_ALLOC(tmp_path); + + memcpy(tmp_path, last, prefix_len); + memcpy(tmp_path + prefix_len, path_ptr + varint_len, suffix_len + 1); + + entry_size = index_entry_size(suffix_len, varint_len, index->oid_type, entry.flags); + entry.path = tmp_path; + } + + if (entry_size == 0) + return -1; + + if (checksum_size + entry_size > buffer_size) { + git_error_set(GIT_ERROR_INTERNAL, "invalid index checksum"); + return -1; + } + + if (index_entry_dup(out, index, &entry) < 0) { + git__free(tmp_path); + return -1; + } + + git__free(tmp_path); + *out_size = entry_size; + return 0; +} + +static int read_header(struct index_header *dest, const void *buffer) +{ + const struct index_header *source = buffer; + + dest->signature = ntohl(source->signature); + if (dest->signature != INDEX_HEADER_SIG) + return index_error_invalid("incorrect header signature"); + + dest->version = ntohl(source->version); + if (dest->version < INDEX_VERSION_NUMBER_LB || + dest->version > INDEX_VERSION_NUMBER_UB) + return index_error_invalid("incorrect header version"); + + dest->entry_count = ntohl(source->entry_count); + return 0; +} + +static int read_extension(size_t *read_len, git_index *index, size_t checksum_size, const char *buffer, size_t buffer_size) +{ + struct index_extension dest; + size_t total_size; + + /* buffer is not guaranteed to be aligned */ + memcpy(&dest, buffer, sizeof(struct index_extension)); + dest.extension_size = ntohl(dest.extension_size); + + total_size = dest.extension_size + sizeof(struct index_extension); + + if (dest.extension_size > total_size || + buffer_size < total_size || + buffer_size - total_size < checksum_size) { + index_error_invalid("extension is truncated"); + return -1; + } + + /* optional extension */ + if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') { + /* tree cache */ + if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) { + if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size, index->oid_type, &index->tree_pool) < 0) + return -1; + } else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) { + if (read_reuc(index, buffer + 8, dest.extension_size) < 0) + return -1; + } else if (memcmp(dest.signature, INDEX_EXT_CONFLICT_NAME_SIG, 4) == 0) { + if (read_conflict_names(index, buffer + 8, dest.extension_size) < 0) + return -1; + } + /* else, unsupported extension. We cannot parse this, but we can skip + * it by returning `total_size */ + } else { + /* we cannot handle non-ignorable extensions; + * in fact they aren't even defined in the standard */ + git_error_set(GIT_ERROR_INDEX, "unsupported mandatory extension: '%.4s'", dest.signature); + return -1; + } + + *read_len = total_size; + + return 0; +} + +static int parse_index(git_index *index, const char *buffer, size_t buffer_size) +{ + int error = 0; + unsigned int i; + struct index_header header = { 0 }; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + unsigned char zero_checksum[GIT_HASH_MAX_SIZE] = { 0 }; + size_t checksum_size = git_hash_size(git_oid_algorithm(index->oid_type)); + const char *last = NULL; + const char *empty = ""; + +#define seek_forward(_increase) { \ + if (_increase >= buffer_size) { \ + error = index_error_invalid("ran out of data while parsing"); \ + goto done; } \ + buffer += _increase; \ + buffer_size -= _increase;\ +} + + if (buffer_size < INDEX_HEADER_SIZE + checksum_size) + return index_error_invalid("insufficient buffer space"); + + /* + * Precalculate the hash of the files's contents -- we'll match + * it to the provided checksum in the footer. + */ + git_hash_buf(checksum, buffer, buffer_size - checksum_size, + git_oid_algorithm(index->oid_type)); + + /* Parse header */ + if ((error = read_header(&header, buffer)) < 0) + return error; + + index->version = header.version; + if (index->version >= INDEX_VERSION_NUMBER_COMP) + last = empty; + + seek_forward(INDEX_HEADER_SIZE); + + GIT_ASSERT(!index->entries.length); + + if ((error = git_index_entrymap_resize(&index->entries_map, header.entry_count)) < 0) + return error; + + /* Parse all the entries */ + for (i = 0; i < header.entry_count && buffer_size > checksum_size; ++i) { + git_index_entry *entry = NULL; + size_t entry_size; + + if ((error = read_entry(&entry, &entry_size, index, checksum_size, buffer, buffer_size, last)) < 0) { + error = index_error_invalid("invalid entry"); + goto done; + } + + if ((error = git_vector_insert(&index->entries, entry)) < 0) { + index_entry_free(entry); + goto done; + } + + if ((error = git_index_entrymap_put(&index->entries_map, entry)) < 0) { + index_entry_free(entry); + goto done; + } + error = 0; + + if (index->version >= INDEX_VERSION_NUMBER_COMP) + last = entry->path; + + seek_forward(entry_size); + } + + if (i != header.entry_count) { + error = index_error_invalid("header entries changed while parsing"); + goto done; + } + + /* There's still space for some extensions! */ + while (buffer_size > checksum_size) { + size_t extension_size; + + if ((error = read_extension(&extension_size, index, checksum_size, buffer, buffer_size)) < 0) { + goto done; + } + + seek_forward(extension_size); + } + + if (buffer_size != checksum_size) { + error = index_error_invalid( + "buffer size does not match index footer size"); + goto done; + } + + /* + * SHA-1 or SHA-256 (depending on the repository's object format) + * over the content of the index file before this checksum. + * Note: checksum may be 0 if the index was written by a client + * where index.skipHash was set to true. + */ + if (memcmp(zero_checksum, buffer, checksum_size) != 0 && + memcmp(checksum, buffer, checksum_size) != 0) { + error = index_error_invalid( + "calculated checksum does not match expected"); + goto done; + } + + memcpy(index->checksum, checksum, checksum_size); + +#undef seek_forward + + /* Entries are stored case-sensitively on disk, so re-sort now if + * in-memory index is supposed to be case-insensitive + */ + git_vector_set_sorted(&index->entries, !index->ignore_case); + git_vector_sort(&index->entries); + + index->dirty = 0; +done: + return error; +} + +static bool is_index_extended(git_index *index) +{ + size_t i, extended; + git_index_entry *entry; + + extended = 0; + + git_vector_foreach(&index->entries, i, entry) { + entry->flags &= ~GIT_INDEX_ENTRY_EXTENDED; + if (entry->flags_extended & GIT_INDEX_ENTRY_EXTENDED_FLAGS) { + extended++; + entry->flags |= GIT_INDEX_ENTRY_EXTENDED; + } + } + + return (extended > 0); +} + +static int write_disk_entry( + git_index *index, + git_filebuf *file, + git_index_entry *entry, + const char *last) +{ + void *mem = NULL; + struct entry_common *ondisk_common = NULL; + size_t path_len, path_offset, disk_size; + int varint_len = 0; + char *path; + const char *path_start = entry->path; + size_t same_len = 0; + + index_entry_short_sha1 ondisk_sha1; + index_entry_long_sha1 ondisk_ext_sha1; +#ifdef GIT_EXPERIMENTAL_SHA256 + index_entry_short_sha256 ondisk_sha256; + index_entry_long_sha256 ondisk_ext_sha256; +#endif + + switch (index->oid_type) { + case GIT_OID_SHA1: + ondisk_common = &ondisk_sha1.common; + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + ondisk_common = &ondisk_sha256.common; + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + + path_len = ((struct entry_internal *)entry)->pathlen; + + if (last) { + const char *last_c = last; + + while (*path_start == *last_c) { + if (!*path_start || !*last_c) + break; + ++path_start; + ++last_c; + ++same_len; + } + path_len -= same_len; + varint_len = git_encode_varint(NULL, 0, strlen(last) - same_len); + } + + disk_size = index_entry_size(path_len, varint_len, index->oid_type, entry->flags); + + if (!disk_size || git_filebuf_reserve(file, &mem, disk_size) < 0) + return -1; + + memset(mem, 0x0, disk_size); + + /** + * Yes, we have to truncate. + * + * The on-disk format for Index entries clearly defines + * the time and size fields to be 4 bytes each -- so even if + * we store these values with 8 bytes on-memory, they must + * be truncated to 4 bytes before writing to disk. + * + * In 2038 I will be either too dead or too rich to care about this + */ + ondisk_common->ctime.seconds = htonl((uint32_t)entry->ctime.seconds); + ondisk_common->mtime.seconds = htonl((uint32_t)entry->mtime.seconds); + ondisk_common->ctime.nanoseconds = htonl(entry->ctime.nanoseconds); + ondisk_common->mtime.nanoseconds = htonl(entry->mtime.nanoseconds); + ondisk_common->dev = htonl(entry->dev); + ondisk_common->ino = htonl(entry->ino); + ondisk_common->mode = htonl(entry->mode); + ondisk_common->uid = htonl(entry->uid); + ondisk_common->gid = htonl(entry->gid); + ondisk_common->file_size = htonl((uint32_t)entry->file_size); + + switch (index->oid_type) { + case GIT_OID_SHA1: + git_oid_raw_cpy(ondisk_sha1.oid, entry->id.id, GIT_OID_SHA1_SIZE); + ondisk_sha1.flags = htons(entry->flags); + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + git_oid_raw_cpy(ondisk_sha256.oid, entry->id.id, GIT_OID_SHA256_SIZE); + ondisk_sha256.flags = htons(entry->flags); + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + + path_offset = index_entry_path_offset(index->oid_type, entry->flags); + + if (entry->flags & GIT_INDEX_ENTRY_EXTENDED) { + struct entry_common *ondisk_ext = NULL; + uint16_t flags_extended = htons(entry->flags_extended & + GIT_INDEX_ENTRY_EXTENDED_FLAGS); + + switch (index->oid_type) { + case GIT_OID_SHA1: + memcpy(&ondisk_ext_sha1, &ondisk_sha1, + sizeof(index_entry_short_sha1)); + ondisk_ext_sha1.flags_extended = flags_extended; + ondisk_ext = &ondisk_ext_sha1.common; + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + memcpy(&ondisk_ext_sha256, &ondisk_sha256, + sizeof(index_entry_short_sha256)); + ondisk_ext_sha256.flags_extended = flags_extended; + ondisk_ext = &ondisk_ext_sha256.common; + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + + memcpy(mem, ondisk_ext, path_offset); + } else { + switch (index->oid_type) { + case GIT_OID_SHA1: + memcpy(mem, &ondisk_sha1, path_offset); + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + memcpy(mem, &ondisk_sha256, path_offset); + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + } + + path = (char *)mem + path_offset; + disk_size -= path_offset; + + if (last) { + varint_len = git_encode_varint((unsigned char *) path, + disk_size, strlen(last) - same_len); + GIT_ASSERT(varint_len > 0); + + path += varint_len; + disk_size -= varint_len; + + /* + * If using path compression, we are not allowed + * to have additional trailing NULs. + */ + GIT_ASSERT(disk_size == path_len + 1); + } else { + /* + * If no path compression is used, we do have + * NULs as padding. As such, simply assert that + * we have enough space left to write the path. + */ + GIT_ASSERT(disk_size > path_len); + } + + memcpy(path, path_start, path_len + 1); + + return 0; +} + +static int write_entries(git_index *index, git_filebuf *file) +{ + int error = 0; + size_t i; + git_vector case_sorted = GIT_VECTOR_INIT, *entries = NULL; + git_index_entry *entry; + const char *last = NULL; + + /* If index->entries is sorted case-insensitively, then we need + * to re-sort it case-sensitively before writing */ + if (index->ignore_case) { + if ((error = git_vector_dup(&case_sorted, &index->entries, git_index_entry_cmp)) < 0) + goto done; + + git_vector_sort(&case_sorted); + entries = &case_sorted; + } else { + entries = &index->entries; + } + + if (index->version >= INDEX_VERSION_NUMBER_COMP) + last = ""; + + git_vector_foreach(entries, i, entry) { + if ((error = write_disk_entry(index, file, entry, last)) < 0) + break; + if (index->version >= INDEX_VERSION_NUMBER_COMP) + last = entry->path; + } + +done: + git_vector_dispose(&case_sorted); + return error; +} + +static int write_extension(git_filebuf *file, struct index_extension *header, git_str *data) +{ + struct index_extension ondisk; + + memset(&ondisk, 0x0, sizeof(struct index_extension)); + memcpy(&ondisk, header, 4); + ondisk.extension_size = htonl(header->extension_size); + + git_filebuf_write(file, &ondisk, sizeof(struct index_extension)); + return git_filebuf_write(file, data->ptr, data->size); +} + +static int create_name_extension_data(git_str *name_buf, git_index_name_entry *conflict_name) +{ + int error = 0; + + if (conflict_name->ancestor == NULL) + error = git_str_put(name_buf, "\0", 1); + else + error = git_str_put(name_buf, conflict_name->ancestor, strlen(conflict_name->ancestor) + 1); + + if (error != 0) + goto on_error; + + if (conflict_name->ours == NULL) + error = git_str_put(name_buf, "\0", 1); + else + error = git_str_put(name_buf, conflict_name->ours, strlen(conflict_name->ours) + 1); + + if (error != 0) + goto on_error; + + if (conflict_name->theirs == NULL) + error = git_str_put(name_buf, "\0", 1); + else + error = git_str_put(name_buf, conflict_name->theirs, strlen(conflict_name->theirs) + 1); + +on_error: + return error; +} + +static int write_name_extension(git_index *index, git_filebuf *file) +{ + git_str name_buf = GIT_STR_INIT; + git_vector *out = &index->names; + git_index_name_entry *conflict_name; + struct index_extension extension; + size_t i; + int error = 0; + + git_vector_foreach(out, i, conflict_name) { + if ((error = create_name_extension_data(&name_buf, conflict_name)) < 0) + goto done; + } + + memset(&extension, 0x0, sizeof(struct index_extension)); + memcpy(&extension.signature, INDEX_EXT_CONFLICT_NAME_SIG, 4); + extension.extension_size = (uint32_t)name_buf.size; + + error = write_extension(file, &extension, &name_buf); + + git_str_dispose(&name_buf); + +done: + return error; +} + +static int create_reuc_extension_data(git_str *reuc_buf, git_index *index, git_index_reuc_entry *reuc) +{ + size_t oid_size = git_oid_size(index->oid_type); + int i; + int error = 0; + + if ((error = git_str_put(reuc_buf, reuc->path, strlen(reuc->path) + 1)) < 0) + return error; + + for (i = 0; i < 3; i++) { + if ((error = git_str_printf(reuc_buf, "%o", reuc->mode[i])) < 0 || + (error = git_str_put(reuc_buf, "\0", 1)) < 0) + return error; + } + + for (i = 0; i < 3; i++) { + if (reuc->mode[i] && (error = git_str_put(reuc_buf, (char *)&reuc->oid[i].id, oid_size)) < 0) + return error; + } + + return 0; +} + +static int write_reuc_extension(git_index *index, git_filebuf *file) +{ + git_str reuc_buf = GIT_STR_INIT; + git_vector *out = &index->reuc; + git_index_reuc_entry *reuc; + struct index_extension extension; + size_t i; + int error = 0; + + git_vector_foreach(out, i, reuc) { + if ((error = create_reuc_extension_data(&reuc_buf, index, reuc)) < 0) + goto done; + } + + memset(&extension, 0x0, sizeof(struct index_extension)); + memcpy(&extension.signature, INDEX_EXT_UNMERGED_SIG, 4); + extension.extension_size = (uint32_t)reuc_buf.size; + + error = write_extension(file, &extension, &reuc_buf); + + git_str_dispose(&reuc_buf); + +done: + return error; +} + +static int write_tree_extension(git_index *index, git_filebuf *file) +{ + struct index_extension extension; + git_str buf = GIT_STR_INIT; + int error; + + if (index->tree == NULL) + return 0; + + if ((error = git_tree_cache_write(&buf, index->tree)) < 0) + return error; + + memset(&extension, 0x0, sizeof(struct index_extension)); + memcpy(&extension.signature, INDEX_EXT_TREECACHE_SIG, 4); + extension.extension_size = (uint32_t)buf.size; + + error = write_extension(file, &extension, &buf); + + git_str_dispose(&buf); + + return error; +} + +static void clear_uptodate(git_index *index) +{ + git_index_entry *entry; + size_t i; + + git_vector_foreach(&index->entries, i, entry) + entry->flags_extended &= ~GIT_INDEX_ENTRY_UPTODATE; +} + +static int write_index( + unsigned char checksum[GIT_HASH_MAX_SIZE], + size_t *checksum_size, + git_index *index, + git_filebuf *file) +{ + struct index_header header; + bool is_extended; + uint32_t index_version_number; + + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(file); + + GIT_ASSERT(index->oid_type); + + *checksum_size = git_hash_size(git_oid_algorithm(index->oid_type)); + + if (index->version <= INDEX_VERSION_NUMBER_EXT) { + is_extended = is_index_extended(index); + index_version_number = is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER_LB; + } else { + index_version_number = index->version; + } + + header.signature = htonl(INDEX_HEADER_SIG); + header.version = htonl(index_version_number); + header.entry_count = htonl((uint32_t)index->entries.length); + + if (git_filebuf_write(file, &header, sizeof(struct index_header)) < 0) + return -1; + + if (write_entries(index, file) < 0) + return -1; + + /* write the tree cache extension */ + if (index->tree != NULL && write_tree_extension(index, file) < 0) + return -1; + + /* write the rename conflict extension */ + if (index->names.length > 0 && write_name_extension(index, file) < 0) + return -1; + + /* write the reuc extension */ + if (index->reuc.length > 0 && write_reuc_extension(index, file) < 0) + return -1; + + /* get out the hash for all the contents we've appended to the file */ + git_filebuf_hash(checksum, file); + + /* write it at the end of the file */ + if (git_filebuf_write(file, checksum, *checksum_size) < 0) + return -1; + + /* file entries are no longer up to date */ + clear_uptodate(index); + + return 0; +} + +int git_index_entry_stage(const git_index_entry *entry) +{ + return GIT_INDEX_ENTRY_STAGE(entry); +} + +int git_index_entry_is_conflict(const git_index_entry *entry) +{ + return (GIT_INDEX_ENTRY_STAGE(entry) > 0); +} + +typedef struct read_tree_data { + git_index *index; + git_vector *old_entries; + git_vector *new_entries; + git_vector_cmp entry_cmp; + git_tree_cache *tree; +} read_tree_data; + +static int read_tree_cb( + const char *root, const git_tree_entry *tentry, void *payload) +{ + read_tree_data *data = payload; + git_index_entry *entry = NULL, *old_entry; + git_str path = GIT_STR_INIT; + size_t pos; + + if (git_tree_entry__is_tree(tentry)) + return 0; + + if (git_str_joinpath(&path, root, tentry->filename) < 0) + return -1; + + if (index_entry_create(&entry, INDEX_OWNER(data->index), path.ptr, NULL, false) < 0) + return -1; + + entry->mode = tentry->attr; + git_oid_cpy(&entry->id, git_tree_entry_id(tentry)); + + /* look for corresponding old entry and copy data to new entry */ + if (data->old_entries != NULL && + !index_find_in_entries( + &pos, data->old_entries, data->entry_cmp, path.ptr, 0, 0) && + (old_entry = git_vector_get(data->old_entries, pos)) != NULL && + entry->mode == old_entry->mode && + git_oid_equal(&entry->id, &old_entry->id)) + { + index_entry_cpy(entry, old_entry); + entry->flags_extended = 0; + } + + index_entry_adjust_namemask(entry, path.size); + git_str_dispose(&path); + + if (git_vector_insert(data->new_entries, entry) < 0) { + index_entry_free(entry); + return -1; + } + + return 0; +} + +int git_index_read_tree(git_index *index, const git_tree *tree) +{ + int error = 0; + git_vector entries = GIT_VECTOR_INIT; + git_index_entrymap entries_map = GIT_INDEX_ENTRYMAP_INIT; + read_tree_data data; + size_t i; + git_index_entry *e; + + git_vector_set_cmp(&entries, index->entries._cmp); /* match sort */ + + data.index = index; + data.old_entries = &index->entries; + data.new_entries = &entries; + data.entry_cmp = index->entries_search; + + index->tree = NULL; + git_pool_clear(&index->tree_pool); + + git_vector_sort(&index->entries); + + if ((error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data)) < 0) + goto cleanup; + + if ((error = git_index_entrymap_resize(&entries_map, entries.length)) < 0) + goto cleanup; + + git_vector_foreach(&entries, i, e) { + if ((error = git_index_entrymap_put(&entries_map, e)) < 0) { + git_error_set(GIT_ERROR_INDEX, "failed to insert entry into map"); + return error; + } + } + + error = 0; + + git_vector_sort(&entries); + + if ((error = git_index_clear(index)) < 0) + goto cleanup; + + git_vector_swap(&entries, &index->entries); + git_index_entrymap_swap(&entries_map, &index->entries_map); + + index->dirty = 1; + +cleanup: + git_vector_dispose(&entries); + git_index_entrymap_dispose(&entries_map); + + if (error < 0) + return error; + + error = git_tree_cache_read_tree(&index->tree, tree, index->oid_type, &index->tree_pool); + + return error; +} + +static int git_index_read_iterator( + git_index *index, + git_iterator *new_iterator, + size_t new_length_hint) +{ + git_vector new_entries = GIT_VECTOR_INIT, + remove_entries = GIT_VECTOR_INIT; + git_index_entrymap new_entries_map = GIT_INDEX_ENTRYMAP_INIT; + git_iterator *index_iterator = NULL; + git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT; + const git_index_entry *old_entry, *new_entry; + git_index_entry *entry; + size_t i; + int error; + + GIT_ASSERT((new_iterator->flags & GIT_ITERATOR_DONT_IGNORE_CASE)); + + if ((error = git_vector_init(&new_entries, new_length_hint, index->entries._cmp)) < 0 || + (error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0) + goto done; + + if (new_length_hint && + (error = git_index_entrymap_resize(&new_entries_map, new_length_hint)) < 0) + goto done; + + opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_INCLUDE_CONFLICTS; + + if ((error = git_iterator_for_index(&index_iterator, + git_index_owner(index), index, &opts)) < 0 || + ((error = git_iterator_current(&old_entry, index_iterator)) < 0 && + error != GIT_ITEROVER) || + ((error = git_iterator_current(&new_entry, new_iterator)) < 0 && + error != GIT_ITEROVER)) + goto done; + + while (true) { + git_index_entry + *dup_entry = NULL, + *add_entry = NULL, + *remove_entry = NULL; + int diff; + + error = 0; + + if (old_entry && new_entry) + diff = git_index_entry_cmp(old_entry, new_entry); + else if (!old_entry && new_entry) + diff = 1; + else if (old_entry && !new_entry) + diff = -1; + else + break; + + if (diff < 0) { + remove_entry = (git_index_entry *)old_entry; + } else if (diff > 0) { + dup_entry = (git_index_entry *)new_entry; + } else { + /* Path and stage are equal, if the OID is equal, keep it to + * keep the stat cache data. + */ + if (git_oid_equal(&old_entry->id, &new_entry->id) && + old_entry->mode == new_entry->mode) { + add_entry = (git_index_entry *)old_entry; + } else { + dup_entry = (git_index_entry *)new_entry; + remove_entry = (git_index_entry *)old_entry; + } + } + + if (dup_entry) { + if ((error = index_entry_dup_nocache(&add_entry, index, dup_entry)) < 0) + goto done; + + index_entry_adjust_namemask(add_entry, + ((struct entry_internal *)add_entry)->pathlen); + } + + /* invalidate this path in the tree cache if this is new (to + * invalidate the parent trees) + */ + if (dup_entry && !remove_entry && index->tree) + git_tree_cache_invalidate_path(index->tree, dup_entry->path); + + if (add_entry) { + if ((error = git_vector_insert(&new_entries, add_entry)) == 0) + error = git_index_entrymap_put(&new_entries_map, add_entry); + } + + if (remove_entry && error >= 0) + error = git_vector_insert(&remove_entries, remove_entry); + + if (error < 0) { + git_error_set(GIT_ERROR_INDEX, "failed to insert entry"); + goto done; + } + + if (diff <= 0) { + if ((error = git_iterator_advance(&old_entry, index_iterator)) < 0 && + error != GIT_ITEROVER) + goto done; + } + + if (diff >= 0) { + if ((error = git_iterator_advance(&new_entry, new_iterator)) < 0 && + error != GIT_ITEROVER) + goto done; + } + } + + if ((error = git_index_name_clear(index)) < 0 || + (error = git_index_reuc_clear(index)) < 0) + goto done; + + git_vector_swap(&new_entries, &index->entries); + git_index_entrymap_swap(&index->entries_map, &new_entries_map); + + git_vector_foreach(&remove_entries, i, entry) { + if (index->tree) + git_tree_cache_invalidate_path(index->tree, entry->path); + + index_entry_free(entry); + } + + clear_uptodate(index); + + index->dirty = 1; + error = 0; + +done: + git_index_entrymap_dispose(&new_entries_map); + git_vector_dispose(&new_entries); + git_vector_dispose(&remove_entries); + git_iterator_free(index_iterator); + return error; +} + +int git_index_read_index( + git_index *index, + const git_index *new_index) +{ + git_iterator *new_iterator = NULL; + git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT; + int error; + + opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_INCLUDE_CONFLICTS; + + if ((error = git_iterator_for_index(&new_iterator, + git_index_owner(new_index), (git_index *)new_index, &opts)) < 0 || + (error = git_index_read_iterator(index, new_iterator, + new_index->entries.length)) < 0) + goto done; + +done: + git_iterator_free(new_iterator); + return error; +} + +git_repository *git_index_owner(const git_index *index) +{ + return INDEX_OWNER(index); +} + +enum { + INDEX_ACTION_NONE = 0, + INDEX_ACTION_UPDATE = 1, + INDEX_ACTION_REMOVE = 2, + INDEX_ACTION_ADDALL = 3 +}; + +int git_index_add_all( + git_index *index, + const git_strarray *paths, + unsigned int flags, + git_index_matched_path_cb cb, + void *payload) +{ + int error; + git_repository *repo; + git_pathspec ps; + bool no_fnmatch = (flags & GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH) != 0; + + GIT_ASSERT_ARG(index); + + repo = INDEX_OWNER(index); + if ((error = git_repository__ensure_not_bare(repo, "index add all")) < 0) + return error; + + if ((error = git_pathspec__init(&ps, paths)) < 0) + return error; + + /* optionally check that pathspec doesn't mention any ignored files */ + if ((flags & GIT_INDEX_ADD_CHECK_PATHSPEC) != 0 && + (flags & GIT_INDEX_ADD_FORCE) == 0 && + (error = git_ignore__check_pathspec_for_exact_ignores( + repo, &ps.pathspec, no_fnmatch)) < 0) + goto cleanup; + + error = index_apply_to_wd_diff(index, INDEX_ACTION_ADDALL, paths, flags, cb, payload); + + if (error) + git_error_set_after_callback(error); + +cleanup: + git_pathspec__clear(&ps); + + return error; +} + +struct foreach_diff_data { + git_index *index; + const git_pathspec *pathspec; + unsigned int flags; + git_index_matched_path_cb cb; + void *payload; +}; + +static int apply_each_file(const git_diff_delta *delta, float progress, void *payload) +{ + struct foreach_diff_data *data = payload; + const char *match, *path; + int error = 0; + + GIT_UNUSED(progress); + + path = delta->old_file.path; + + /* We only want those which match the pathspecs */ + if (!git_pathspec__match( + &data->pathspec->pathspec, path, false, (bool)data->index->ignore_case, + &match, NULL)) + return 0; + + if (data->cb) + error = data->cb(path, match, data->payload); + + if (error > 0) /* skip this entry */ + return 0; + if (error < 0) /* actual error */ + return error; + + /* If the workdir item does not exist, remove it from the index. */ + if ((delta->new_file.flags & GIT_DIFF_FLAG_EXISTS) == 0) + error = git_index_remove_bypath(data->index, path); + else + error = git_index_add_bypath(data->index, delta->new_file.path); + + return error; +} + +static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths, + unsigned int flags, + git_index_matched_path_cb cb, void *payload) +{ + int error; + git_diff *diff; + git_pathspec ps; + git_repository *repo; + git_diff_options opts = GIT_DIFF_OPTIONS_INIT; + struct foreach_diff_data data = { + index, + NULL, + flags, + cb, + payload, + }; + + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(action == INDEX_ACTION_UPDATE || action == INDEX_ACTION_ADDALL); + + repo = INDEX_OWNER(index); + + if (!repo) { + return create_index_error(-1, + "cannot run update; the index is not backed up by a repository."); + } + + /* + * We do the matching ourselves instead of passing the list to + * diff because we want to tell the callback which one + * matched, which we do not know if we ask diff to filter for us. + */ + if ((error = git_pathspec__init(&ps, paths)) < 0) + return error; + + opts.flags = GIT_DIFF_INCLUDE_TYPECHANGE; + if (action == INDEX_ACTION_ADDALL) { + opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | + GIT_DIFF_RECURSE_UNTRACKED_DIRS; + + if (flags == GIT_INDEX_ADD_FORCE) + opts.flags |= GIT_DIFF_INCLUDE_IGNORED | + GIT_DIFF_RECURSE_IGNORED_DIRS; + } + + if ((error = git_diff_index_to_workdir(&diff, repo, index, &opts)) < 0) + goto cleanup; + + data.pathspec = &ps; + error = git_diff_foreach(diff, apply_each_file, NULL, NULL, NULL, &data); + git_diff_free(diff); + + if (error) /* make sure error is set if callback stopped iteration */ + git_error_set_after_callback(error); + +cleanup: + git_pathspec__clear(&ps); + return error; +} + +static int index_apply_to_all( + git_index *index, + int action, + const git_strarray *paths, + git_index_matched_path_cb cb, + void *payload) +{ + int error = 0; + size_t i; + git_pathspec ps; + const char *match; + git_str path = GIT_STR_INIT; + + GIT_ASSERT_ARG(index); + + if ((error = git_pathspec__init(&ps, paths)) < 0) + return error; + + git_vector_sort(&index->entries); + + for (i = 0; !error && i < index->entries.length; ++i) { + git_index_entry *entry = git_vector_get(&index->entries, i); + + /* check if path actually matches */ + if (!git_pathspec__match( + &ps.pathspec, entry->path, false, (bool)index->ignore_case, + &match, NULL)) + continue; + + /* issue notification callback if requested */ + if (cb && (error = cb(entry->path, match, payload)) != 0) { + if (error > 0) { /* return > 0 means skip this one */ + error = 0; + continue; + } + if (error < 0) /* return < 0 means abort */ + break; + } + + /* index manipulation may alter entry, so don't depend on it */ + if ((error = git_str_sets(&path, entry->path)) < 0) + break; + + switch (action) { + case INDEX_ACTION_NONE: + break; + case INDEX_ACTION_UPDATE: + error = git_index_add_bypath(index, path.ptr); + + if (error == GIT_ENOTFOUND) { + git_error_clear(); + + error = git_index_remove_bypath(index, path.ptr); + + if (!error) /* back up foreach if we removed this */ + i--; + } + break; + case INDEX_ACTION_REMOVE: + if (!(error = git_index_remove_bypath(index, path.ptr))) + i--; /* back up foreach if we removed this */ + break; + default: + git_error_set(GIT_ERROR_INVALID, "unknown index action %d", action); + error = -1; + break; + } + } + + git_str_dispose(&path); + git_pathspec__clear(&ps); + + return error; +} + +int git_index_remove_all( + git_index *index, + const git_strarray *pathspec, + git_index_matched_path_cb cb, + void *payload) +{ + int error = index_apply_to_all( + index, INDEX_ACTION_REMOVE, pathspec, cb, payload); + + if (error) /* make sure error is set if callback stopped iteration */ + git_error_set_after_callback(error); + + return error; +} + +int git_index_update_all( + git_index *index, + const git_strarray *pathspec, + git_index_matched_path_cb cb, + void *payload) +{ + int error = index_apply_to_wd_diff(index, INDEX_ACTION_UPDATE, pathspec, 0, cb, payload); + if (error) /* make sure error is set if callback stopped iteration */ + git_error_set_after_callback(error); + + return error; +} + +int git_index_snapshot_new(git_vector *snap, git_index *index) +{ + int error; + + GIT_REFCOUNT_INC(index); + + git_atomic32_inc(&index->readers); + git_vector_sort(&index->entries); + + error = git_vector_dup(snap, &index->entries, index->entries._cmp); + + if (error < 0) + git_index_snapshot_release(snap, index); + + return error; +} + +void git_index_snapshot_release(git_vector *snap, git_index *index) +{ + git_vector_dispose(snap); + + git_atomic32_dec(&index->readers); + + git_index_free(index); +} + +int git_index_snapshot_find( + size_t *out, git_vector *entries, git_vector_cmp entry_srch, + const char *path, size_t path_len, int stage) +{ + return index_find_in_entries(out, entries, entry_srch, path, path_len, stage); +} + +int git_indexwriter_init( + git_indexwriter *writer, + git_index *index) +{ + int filebuf_hash, error; + + GIT_REFCOUNT_INC(index); + + writer->index = index; + + filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(index->oid_type)); + GIT_ASSERT(filebuf_hash); + + if (!index->index_file_path) + return create_index_error(-1, + "failed to write index: The index is in-memory only"); + + if ((error = git_filebuf_open(&writer->file, + index->index_file_path, + git_filebuf_hash_flags(filebuf_hash), + GIT_INDEX_FILE_MODE)) < 0) { + if (error == GIT_ELOCKED) + git_error_set(GIT_ERROR_INDEX, "the index is locked; this might be due to a concurrent or crashed process"); + + return error; + } + + writer->should_write = 1; + + return 0; +} + +int git_indexwriter_init_for_operation( + git_indexwriter *writer, + git_repository *repo, + unsigned int *checkout_strategy) +{ + git_index *index; + int error; + + if ((error = git_repository_index__weakptr(&index, repo)) < 0 || + (error = git_indexwriter_init(writer, index)) < 0) + return error; + + writer->should_write = (*checkout_strategy & GIT_CHECKOUT_DONT_WRITE_INDEX) == 0; + *checkout_strategy |= GIT_CHECKOUT_DONT_WRITE_INDEX; + + return 0; +} + +int git_indexwriter_commit(git_indexwriter *writer) +{ + unsigned char checksum[GIT_HASH_MAX_SIZE]; + size_t checksum_size; + int error; + + if (!writer->should_write) + return 0; + + git_vector_sort(&writer->index->entries); + git_vector_sort(&writer->index->reuc); + + if ((error = write_index(checksum, &checksum_size, writer->index, &writer->file)) < 0) { + git_indexwriter_cleanup(writer); + return error; + } + + if ((error = git_filebuf_commit(&writer->file)) < 0) + return error; + + if ((error = git_futils_filestamp_check( + &writer->index->stamp, writer->index->index_file_path)) < 0) { + git_error_set(GIT_ERROR_OS, "could not read index timestamp"); + return -1; + } + + writer->index->dirty = 0; + writer->index->on_disk = 1; + memcpy(writer->index->checksum, checksum, checksum_size); + + git_index_free(writer->index); + writer->index = NULL; + + return 0; +} + +void git_indexwriter_cleanup(git_indexwriter *writer) +{ + git_filebuf_cleanup(&writer->file); + + git_index_free(writer->index); + writer->index = NULL; +} + +/* Deprecated functions */ + +#ifndef GIT_DEPRECATE_HARD +int git_index_add_frombuffer( + git_index *index, const git_index_entry *source_entry, + const void *buffer, size_t len) +{ + return git_index_add_from_buffer(index, source_entry, buffer, len); +} +#endif diff --git a/src/libgit2/index.h b/src/libgit2/index.h new file mode 100644 index 00000000000..588fe434adf --- /dev/null +++ b/src/libgit2/index.h @@ -0,0 +1,215 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_index_h__ +#define INCLUDE_index_h__ + +#include "common.h" + +#include "futils.h" +#include "filebuf.h" +#include "vector.h" +#include "tree-cache.h" +#include "index_map.h" +#include "git2/odb.h" +#include "git2/index.h" + +#define GIT_INDEX_FILE "index" +#define GIT_INDEX_FILE_MODE 0666 + +/* Helper to create index options based on repository options */ +#define GIT_INDEX_OPTIONS_FOR_REPO(r) \ + { GIT_INDEX_OPTIONS_VERSION, r ? r->oid_type : 0 } + +extern bool git_index__enforce_unsaved_safety; + +struct git_index { + git_refcount rc; + + char *index_file_path; + git_futils_filestamp stamp; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + + git_vector entries; + git_index_entrymap entries_map; + + git_vector deleted; /* deleted entries if readers > 0 */ + git_atomic32 readers; /* number of active iterators */ + + git_oid_t oid_type; + + unsigned int on_disk:1; + unsigned int ignore_case:1; + unsigned int distrust_filemode:1; + unsigned int no_symlinks:1; + unsigned int dirty:1; /* whether we have unsaved changes */ + + git_tree_cache *tree; + git_pool tree_pool; + + git_vector names; + git_vector reuc; + + git_vector_cmp entries_cmp_path; + git_vector_cmp entries_search; + git_vector_cmp entries_search_path; + git_vector_cmp reuc_search; + + unsigned int version; +}; + +struct git_index_iterator { + git_index *index; + git_vector snap; + size_t cur; +}; + +struct git_index_conflict_iterator { + git_index *index; + size_t cur; +}; + +extern void git_index_entry__init_from_stat( + git_index_entry *entry, struct stat *st, bool trust_mode); + +/* Index entry comparison functions for array sorting */ +extern int git_index_entry_cmp(const void *a, const void *b); +extern int git_index_entry_icmp(const void *a, const void *b); + +/* Index entry search functions for search using a search spec */ +extern int git_index_entry_srch(const void *a, const void *b); +extern int git_index_entry_isrch(const void *a, const void *b); + +/* Index time handling functions */ +GIT_INLINE(bool) git_index_time_eq(const git_index_time *one, const git_index_time *two) +{ + if (one->seconds != two->seconds) + return false; + +#ifdef GIT_NSEC + if (one->nanoseconds != two->nanoseconds) + return false; +#endif + + return true; +} + +/* + * Test if the given index time is newer than the given existing index entry. + * If the timestamps are exactly equivalent, then the given index time is + * considered "racily newer" than the existing index entry. + */ +GIT_INLINE(bool) git_index_entry_newer_than_index( + const git_index_entry *entry, git_index *index) +{ + /* If we never read the index, we can't have this race either */ + if (!index || index->stamp.mtime.tv_sec == 0) + return false; + + /* If the timestamp is the same or newer than the index, it's racy */ +#if defined(GIT_NSEC) + if ((int32_t)index->stamp.mtime.tv_sec < entry->mtime.seconds) + return true; + else if ((int32_t)index->stamp.mtime.tv_sec > entry->mtime.seconds) + return false; + else + return (uint32_t)index->stamp.mtime.tv_nsec <= entry->mtime.nanoseconds; +#else + return ((int32_t)index->stamp.mtime.tv_sec) <= entry->mtime.seconds; +#endif +} + +/* Search index for `path`, returning GIT_ENOTFOUND if it does not exist + * (but not setting an error message). + * + * `at_pos` is set to the position where it is or would be inserted. + * Pass `path_len` as strlen of path or 0 to call strlen internally. + */ +extern int git_index__find_pos( + size_t *at_pos, git_index *index, const char *path, size_t path_len, int stage); + +extern int git_index__fill(git_index *index, const git_vector *source_entries); + +extern void git_index__set_ignore_case(git_index *index, bool ignore_case); + +extern unsigned int git_index__create_mode(unsigned int mode); + +GIT_INLINE(const git_futils_filestamp *) git_index__filestamp(git_index *index) +{ + return &index->stamp; +} + +GIT_INLINE(unsigned char *) git_index__checksum(git_index *index) +{ + return index->checksum; +} + +/* Copy the current entries vector *and* increment the index refcount. + * Call `git_index__release_snapshot` when done. + */ +extern int git_index_snapshot_new(git_vector *snap, git_index *index); +extern void git_index_snapshot_release(git_vector *snap, git_index *index); + +/* Allow searching in a snapshot; entries must already be sorted! */ +extern int git_index_snapshot_find( + size_t *at_pos, git_vector *snap, git_vector_cmp entry_srch, + const char *path, size_t path_len, int stage); + +/* Replace an index with a new index */ +int git_index_read_index(git_index *index, const git_index *new_index); + +GIT_INLINE(int) git_index_is_dirty(git_index *index) +{ + return index->dirty; +} + +extern int git_index_read_safely(git_index *index); + +typedef struct { + git_index *index; + git_filebuf file; + unsigned int should_write:1; +} git_indexwriter; + +#define GIT_INDEXWRITER_INIT { NULL, GIT_FILEBUF_INIT } + +/* Lock the index for eventual writing. */ +extern int git_indexwriter_init(git_indexwriter *writer, git_index *index); + +/* Lock the index for eventual writing by a repository operation: a merge, + * revert, cherry-pick or a rebase. Note that the given checkout strategy + * will be updated for the operation's use so that checkout will not write + * the index. + */ +extern int git_indexwriter_init_for_operation( + git_indexwriter *writer, + git_repository *repo, + unsigned int *checkout_strategy); + +/* Write the index and unlock it. */ +extern int git_indexwriter_commit(git_indexwriter *writer); + +/* Cleanup an index writing session, unlocking the file (if it is still + * locked and freeing any data structures. + */ +extern void git_indexwriter_cleanup(git_indexwriter *writer); + +/* SHA256 support */ + +#ifndef GIT_EXPERIMENTAL_SHA256 + +int git_index_open_ext( + git_index **index_out, + const char *index_path, + const git_index_options *opts); + +GIT_EXTERN(int) git_index_new_ext( + git_index **index_out, + const git_index_options *opts); + +#endif + +#endif diff --git a/src/libgit2/index_map.c b/src/libgit2/index_map.c new file mode 100644 index 00000000000..4c50ca7ab63 --- /dev/null +++ b/src/libgit2/index_map.c @@ -0,0 +1,95 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" +#include "hashmap.h" +#include "index_map.h" + +typedef git_index_entrymap git_index_entrymap_default; +typedef git_index_entrymap git_index_entrymap_icase; + +/* This is __ac_X31_hash_string but with tolower and it takes the entry's stage into account */ +GIT_INLINE(uint32_t) git_index_entrymap_hash(const git_index_entry *e) +{ + const char *s = e->path; + uint32_t h = (uint32_t)git__tolower(*s); + if (h) { + for (++s ; *s; ++s) + h = (h << 5) - h + (uint32_t)git__tolower(*s); + } + return h + GIT_INDEX_ENTRY_STAGE(e); +} + +#define git_index_entrymap_equal_default(a, b) (GIT_INDEX_ENTRY_STAGE(a) == GIT_INDEX_ENTRY_STAGE(b) && strcmp(a->path, b->path) == 0) +#define git_index_entrymap_equal_icase(a, b) (GIT_INDEX_ENTRY_STAGE(a) == GIT_INDEX_ENTRY_STAGE(b) && strcasecmp(a->path, b->path) == 0) + +GIT_HASHMAP_FUNCTIONS(git_index_entrymap_default, GIT_HASHMAP_INLINE, git_index_entry *, git_index_entry *, git_index_entrymap_hash, git_index_entrymap_equal_default) +GIT_HASHMAP_FUNCTIONS(git_index_entrymap_icase, GIT_HASHMAP_INLINE, git_index_entry *, git_index_entry *, git_index_entrymap_hash, git_index_entrymap_equal_icase) + +int git_index_entrymap_put(git_index_entrymap *map, git_index_entry *e) +{ + if (map->ignore_case) + return git_index_entrymap_icase_put((git_index_entrymap_icase *)map, e, e); + else + return git_index_entrymap_default_put((git_index_entrymap_default *)map, e, e); +} + +int git_index_entrymap_get(git_index_entry **out, git_index_entrymap *map, git_index_entry *e) +{ + if (map->ignore_case) + return git_index_entrymap_icase_get(out, (git_index_entrymap_icase *)map, e); + else + return git_index_entrymap_default_get(out, (git_index_entrymap_default *)map, e); +} + +int git_index_entrymap_remove(git_index_entrymap *map, git_index_entry *e) +{ + if (map->ignore_case) + return git_index_entrymap_icase_remove((git_index_entrymap_icase *)map, e); + else + return git_index_entrymap_default_remove((git_index_entrymap_default *)map, e); +} + +int git_index_entrymap_resize(git_index_entrymap *map, size_t count) +{ + if (count > UINT32_MAX) { + git_error_set(GIT_ERROR_INDEX, "index map is out of bounds"); + return -1; + } + + if (map->ignore_case) + return git_index_entrymap_icase__resize((git_index_entrymap_icase *)map, (uint32_t)count); + else + return git_index_entrymap_default__resize((git_index_entrymap_default *)map, (uint32_t)count); +} + +void git_index_entrymap_swap(git_index_entrymap *a, git_index_entrymap *b) +{ + git_index_entrymap t; + + if (a != b) { + memcpy(&t, a, sizeof(t)); + memcpy(a, b, sizeof(t)); + memcpy(b, &t, sizeof(t)); + } +} + +void git_index_entrymap_clear(git_index_entrymap *map) +{ + if (map->ignore_case) + git_index_entrymap_icase_clear((git_index_entrymap_icase *)map); + else + git_index_entrymap_default_clear((git_index_entrymap_default *)map); +} + +void git_index_entrymap_dispose(git_index_entrymap *map) +{ + if (map->ignore_case) + git_index_entrymap_icase_dispose((git_index_entrymap_icase *)map); + else + git_index_entrymap_default_dispose((git_index_entrymap_default *)map); +} diff --git a/src/libgit2/index_map.h b/src/libgit2/index_map.h new file mode 100644 index 00000000000..177a3f196c6 --- /dev/null +++ b/src/libgit2/index_map.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_index_map_h__ +#define INCLUDE_index_map_h__ + +#include "common.h" +#include "hashmap.h" + +typedef struct { + unsigned int ignore_case; + GIT_HASHMAP_STRUCT_MEMBERS(git_index_entry *, git_index_entry *) +} git_index_entrymap; + +#define GIT_INDEX_ENTRYMAP_INIT { 0 } + +extern int git_index_entrymap_get(git_index_entry **out, git_index_entrymap *map, git_index_entry *e); +extern int git_index_entrymap_put(git_index_entrymap *map, git_index_entry *e); +extern int git_index_entrymap_remove(git_index_entrymap *map, git_index_entry *e); +extern int git_index_entrymap_resize(git_index_entrymap *map, size_t count); +extern void git_index_entrymap_swap(git_index_entrymap *a, git_index_entrymap *b); +extern void git_index_entrymap_clear(git_index_entrymap *map); +extern void git_index_entrymap_dispose(git_index_entrymap *map); + +#endif diff --git a/src/libgit2/indexer.c b/src/libgit2/indexer.c new file mode 100644 index 00000000000..cdd2b243134 --- /dev/null +++ b/src/libgit2/indexer.c @@ -0,0 +1,1479 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "indexer.h" + +#include "git2/indexer.h" +#include "git2/object.h" + +#include "commit.h" +#include "tree.h" +#include "tag.h" +#include "pack.h" +#include "mwindow.h" +#include "posix.h" +#include "pack.h" +#include "filebuf.h" +#include "oid.h" +#include "oidarray.h" +#include "zstream.h" +#include "object.h" +#include "hashmap_oid.h" + +size_t git_indexer__max_objects = UINT32_MAX; + +#define UINT31_MAX (0x7FFFFFFF) + +GIT_HASHMAP_OID_SETUP(git_indexer_oidmap, git_oid *); + +struct entry { + git_oid oid; + uint32_t crc; + uint32_t offset; + uint64_t offset_long; +}; + +struct git_indexer { + unsigned int parsed_header :1, + pack_committed :1, + have_stream :1, + have_delta :1, + do_fsync :1, + do_verify :1; + git_oid_t oid_type; + struct git_pack_header hdr; + struct git_pack_file *pack; + unsigned int mode; + off64_t off; + off64_t entry_start; + git_object_t entry_type; + git_str entry_data; + git_packfile_stream stream; + size_t nr_objects; + git_vector objects; + git_vector deltas; + unsigned int fanout[256]; + git_hash_ctx hash_ctx; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + char name[(GIT_HASH_MAX_SIZE * 2) + 1]; + git_indexer_progress_cb progress_cb; + void *progress_payload; + char objbuf[8*1024]; + + /* OIDs referenced from pack objects. Used for verification. */ + git_indexer_oidmap expected_oids; + + /* Needed to look up objects which we want to inject to fix a thin pack */ + git_odb *odb; + + /* Fields for calculating the packfile trailer (hash of everything before it) */ + char inbuf[GIT_HASH_MAX_SIZE]; + size_t inbuf_len; + git_hash_ctx trailer; +}; + +struct delta_info { + off64_t delta_off; +}; + +#ifndef GIT_DEPRECATE_HARD +const git_oid *git_indexer_hash(const git_indexer *idx) +{ + return (git_oid *)idx->checksum; +} +#endif + +const char *git_indexer_name(const git_indexer *idx) +{ + return idx->name; +} + +static int parse_header(struct git_pack_header *hdr, struct git_pack_file *pack) +{ + int error; + git_map map; + + if ((error = p_mmap(&map, sizeof(*hdr), GIT_PROT_READ, GIT_MAP_SHARED, pack->mwf.fd, 0)) < 0) + return error; + + memcpy(hdr, map.data, sizeof(*hdr)); + p_munmap(&map); + + /* Verify we recognize this pack file format. */ + if (hdr->hdr_signature != ntohl(PACK_SIGNATURE)) { + git_error_set(GIT_ERROR_INDEXER, "wrong pack signature"); + return -1; + } + + if (!pack_version_ok(hdr->hdr_version)) { + git_error_set(GIT_ERROR_INDEXER, "wrong pack version"); + return -1; + } + + return 0; +} + +static int objects_cmp(const void *a, const void *b) +{ + const struct entry *entrya = a; + const struct entry *entryb = b; + + return git_oid__cmp(&entrya->oid, &entryb->oid); +} + +int git_indexer_options_init(git_indexer_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_indexer_options, GIT_INDEXER_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_indexer_init_options(git_indexer_options *opts, unsigned int version) +{ + return git_indexer_options_init(opts, version); +} +#endif + +GIT_INLINE(git_hash_algorithm_t) indexer_hash_algorithm(git_indexer *idx) +{ + switch (idx->oid_type) { + case GIT_OID_SHA1: + return GIT_HASH_ALGORITHM_SHA1; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + return GIT_HASH_ALGORITHM_SHA256; +#endif + } + + return GIT_HASH_ALGORITHM_NONE; +} + +static int indexer_new( + git_indexer **out, + const char *prefix, + git_oid_t oid_type, + unsigned int mode, + git_odb *odb, + git_indexer_options *in_opts) +{ + git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT; + git_indexer *idx; + git_str path = GIT_STR_INIT, tmp_path = GIT_STR_INIT; + static const char suff[] = "/pack"; + git_hash_algorithm_t checksum_type; + int error, fd = -1; + + if (in_opts) + memcpy(&opts, in_opts, sizeof(opts)); + + if (oid_type) + GIT_ASSERT_ARG(git_oid_type_is_valid(oid_type)); + + idx = git__calloc(1, sizeof(git_indexer)); + GIT_ERROR_CHECK_ALLOC(idx); + idx->oid_type = oid_type ? oid_type : GIT_OID_DEFAULT; + idx->odb = odb; + idx->progress_cb = opts.progress_cb; + idx->progress_payload = opts.progress_cb_payload; + idx->mode = mode ? mode : GIT_PACK_FILE_MODE; + git_str_init(&idx->entry_data, 0); + + checksum_type = indexer_hash_algorithm(idx); + + if ((error = git_hash_ctx_init(&idx->hash_ctx, checksum_type)) < 0 || + (error = git_hash_ctx_init(&idx->trailer, checksum_type)) < 0) + goto cleanup; + + idx->do_verify = opts.verify; + + if (git_repository__fsync_gitdir) + idx->do_fsync = 1; + + error = git_str_joinpath(&path, prefix, suff); + if (error < 0) + goto cleanup; + + fd = git_futils_mktmp(&tmp_path, git_str_cstr(&path), idx->mode); + git_str_dispose(&path); + if (fd < 0) + goto cleanup; + + error = git_packfile_alloc(&idx->pack, git_str_cstr(&tmp_path), oid_type); + git_str_dispose(&tmp_path); + + if (error < 0) + goto cleanup; + + idx->pack->mwf.fd = fd; + if ((error = git_mwindow_file_register(&idx->pack->mwf)) < 0) + goto cleanup; + + *out = idx; + return 0; + +cleanup: + if (fd != -1) + p_close(fd); + + if (git_str_len(&tmp_path) > 0) + p_unlink(git_str_cstr(&tmp_path)); + + if (idx->pack != NULL) + p_unlink(idx->pack->pack_name); + + git_str_dispose(&path); + git_str_dispose(&tmp_path); + git__free(idx); + return -1; +} + +#ifdef GIT_EXPERIMENTAL_SHA256 +int git_indexer_new( + git_indexer **out, + const char *prefix, + git_indexer_options *opts) +{ + return indexer_new( + out, + prefix, + opts ? opts->oid_type : 0, + opts ? opts->mode : 0, + opts ? opts->odb : NULL, + opts); +} +#else +int git_indexer_new( + git_indexer **out, + const char *prefix, + unsigned int mode, + git_odb *odb, + git_indexer_options *opts) +{ + return indexer_new(out, prefix, GIT_OID_SHA1, mode, odb, opts); +} +#endif + +void git_indexer__set_fsync(git_indexer *idx, int do_fsync) +{ + idx->do_fsync = !!do_fsync; +} + +/* Try to store the delta so we can try to resolve it later */ +static int store_delta(git_indexer *idx) +{ + struct delta_info *delta; + + delta = git__calloc(1, sizeof(struct delta_info)); + GIT_ERROR_CHECK_ALLOC(delta); + delta->delta_off = idx->entry_start; + + if (git_vector_insert(&idx->deltas, delta) < 0) + return -1; + + return 0; +} + +static int hash_header(git_hash_ctx *ctx, off64_t len, git_object_t type) +{ + char buffer[64]; + size_t hdrlen; + int error; + + if ((error = git_odb__format_object_header(&hdrlen, + buffer, sizeof(buffer), (size_t)len, type)) < 0) + return error; + + return git_hash_update(ctx, buffer, hdrlen); +} + +static int hash_object_stream(git_indexer*idx, git_packfile_stream *stream) +{ + ssize_t read; + + GIT_ASSERT_ARG(idx); + GIT_ASSERT_ARG(stream); + + do { + if ((read = git_packfile_stream_read(stream, idx->objbuf, sizeof(idx->objbuf))) < 0) + break; + + if (idx->do_verify) + git_str_put(&idx->entry_data, idx->objbuf, read); + + git_hash_update(&idx->hash_ctx, idx->objbuf, read); + } while (read > 0); + + if (read < 0) + return (int)read; + + return 0; +} + +/* In order to create the packfile stream, we need to skip over the delta base description */ +static int advance_delta_offset(git_indexer *idx, git_object_t type) +{ + git_mwindow *w = NULL; + + GIT_ASSERT_ARG(type == GIT_PACKFILE_REF_DELTA || type == GIT_PACKFILE_OFS_DELTA); + + if (type == GIT_PACKFILE_REF_DELTA) { + idx->off += git_oid_size(idx->oid_type); + } else { + off64_t base_off; + int error = get_delta_base(&base_off, idx->pack, &w, &idx->off, type, idx->entry_start); + git_mwindow_close(&w); + if (error < 0) + return error; + } + + return 0; +} + +/* Read from the stream and discard any output */ +static int read_object_stream(git_indexer *idx, git_packfile_stream *stream) +{ + ssize_t read; + + GIT_ASSERT_ARG(stream); + + do { + read = git_packfile_stream_read(stream, idx->objbuf, sizeof(idx->objbuf)); + } while (read > 0); + + if (read < 0) + return (int)read; + + return 0; +} + +static int crc_object(uint32_t *crc_out, git_mwindow_file *mwf, off64_t start, off64_t size) +{ + void *ptr; + uint32_t crc; + unsigned int left, len; + git_mwindow *w = NULL; + + crc = crc32(0L, Z_NULL, 0); + while (size) { + ptr = git_mwindow_open(mwf, &w, start, (size_t)size, &left); + if (ptr == NULL) + return -1; + + len = min(left, (unsigned int)size); + crc = crc32(crc, ptr, len); + size -= len; + start += len; + git_mwindow_close(&w); + } + + *crc_out = htonl(crc); + return 0; +} + +static int add_expected_oid(git_indexer *idx, const git_oid *oid) +{ + /* + * If we know about that object because it is stored in our ODB or + * because we have already processed it as part of our pack file, we do + * not have to expect it. + */ + if ((!idx->odb || !git_odb_exists(idx->odb, oid)) && + !git_pack_oidmap_contains(&idx->pack->idx_cache, oid) && + !git_indexer_oidmap_contains(&idx->expected_oids, oid)) { + git_oid *dup = git__malloc(sizeof(*oid)); + GIT_ERROR_CHECK_ALLOC(dup); + git_oid_cpy(dup, oid); + return git_indexer_oidmap_put(&idx->expected_oids, dup, dup); + } + + return 0; +} + +static int check_object_connectivity(git_indexer *idx, const git_rawobj *obj) +{ + git_object *object; + git_oid *expected; + int error = 0; + + if (obj->type != GIT_OBJECT_BLOB && + obj->type != GIT_OBJECT_TREE && + obj->type != GIT_OBJECT_COMMIT && + obj->type != GIT_OBJECT_TAG) + return 0; + + if (git_object__from_raw(&object, obj->data, obj->len, obj->type, idx->oid_type) < 0) { + /* + * parse_raw returns EINVALID on invalid data; downgrade + * that to a normal -1 error code. + */ + error = -1; + goto out; + } + + if (git_indexer_oidmap_get(&expected, &idx->expected_oids, &object->cached.oid) == 0) { + git_indexer_oidmap_remove(&idx->expected_oids, &object->cached.oid); + git__free(expected); + } + + /* + * Check whether this is a known object. If so, we can just continue as + * we assume that the ODB has a complete graph. + */ + if (idx->odb && git_odb_exists(idx->odb, &object->cached.oid)) + return 0; + + switch (obj->type) { + case GIT_OBJECT_TREE: + { + git_tree *tree = (git_tree *) object; + git_tree_entry *entry; + size_t i; + + git_array_foreach(tree->entries, i, entry) + if (add_expected_oid(idx, &entry->oid) < 0) + goto out; + + break; + } + case GIT_OBJECT_COMMIT: + { + git_commit *commit = (git_commit *) object; + git_oid *parent_oid; + size_t i; + + git_array_foreach(commit->parent_ids, i, parent_oid) + if (add_expected_oid(idx, parent_oid) < 0) + goto out; + + if (add_expected_oid(idx, &commit->tree_id) < 0) + goto out; + + break; + } + case GIT_OBJECT_TAG: + { + git_tag *tag = (git_tag *) object; + + if (add_expected_oid(idx, &tag->target) < 0) + goto out; + + break; + } + case GIT_OBJECT_BLOB: + default: + break; + } + +out: + git_object_free(object); + + return error; +} + +static int store_object(git_indexer *idx) +{ + int i, error; + git_oid oid; + struct entry *entry; + off64_t entry_size; + struct git_pack_entry *pentry; + off64_t entry_start = idx->entry_start; + + entry = git__calloc(1, sizeof(*entry)); + GIT_ERROR_CHECK_ALLOC(entry); + + pentry = git__calloc(1, sizeof(struct git_pack_entry)); + GIT_ERROR_CHECK_ALLOC(pentry); + + if (git_hash_final(oid.id, &idx->hash_ctx)) { + git__free(pentry); + goto on_error; + } + +#ifdef GIT_EXPERIMENTAL_SHA256 + oid.type = idx->oid_type; +#endif + + entry_size = idx->off - entry_start; + if (entry_start > UINT31_MAX) { + entry->offset = UINT32_MAX; + entry->offset_long = entry_start; + } else { + entry->offset = (uint32_t)entry_start; + } + + if (idx->do_verify) { + git_rawobj rawobj = { + idx->entry_data.ptr, + idx->entry_data.size, + idx->entry_type + }; + + if ((error = check_object_connectivity(idx, &rawobj)) < 0) + goto on_error; + } + + git_oid_cpy(&pentry->id, &oid); + pentry->offset = entry_start; + + if (git_pack_oidmap_contains(&idx->pack->idx_cache, &pentry->id)) { + const char *idstr = git_oid_tostr_s(&pentry->id); + + if (!idstr) + git_error_set(GIT_ERROR_INDEXER, "failed to parse object id"); + else + git_error_set(GIT_ERROR_INDEXER, "duplicate object %s found in pack", idstr); + + git__free(pentry); + goto on_error; + } + + if ((error = git_pack_oidmap_put(&idx->pack->idx_cache, &pentry->id, pentry)) < 0) { + git__free(pentry); + git_error_set_oom(); + goto on_error; + } + + git_oid_cpy(&entry->oid, &oid); + + if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0) + goto on_error; + + /* Add the object to the list */ + if (git_vector_insert(&idx->objects, entry) < 0) + goto on_error; + + for (i = oid.id[0]; i < 256; ++i) { + idx->fanout[i]++; + } + + return 0; + +on_error: + git__free(entry); + + return -1; +} + +GIT_INLINE(bool) has_entry(git_indexer *idx, git_oid *id) +{ + return git_pack_oidmap_contains(&idx->pack->idx_cache, id); +} + +static int save_entry(git_indexer *idx, struct entry *entry, struct git_pack_entry *pentry, off64_t entry_start) +{ + int i; + + if (entry_start > UINT31_MAX) { + entry->offset = UINT32_MAX; + entry->offset_long = entry_start; + } else { + entry->offset = (uint32_t)entry_start; + } + + pentry->offset = entry_start; + + if (git_pack_oidmap_contains(&idx->pack->idx_cache, &pentry->id) || + git_pack_oidmap_put(&idx->pack->idx_cache, &pentry->id, pentry) < 0) { + git_error_set(GIT_ERROR_INDEXER, "cannot insert object into pack"); + return -1; + } + + /* Add the object to the list */ + if (git_vector_insert(&idx->objects, entry) < 0) + return -1; + + for (i = entry->oid.id[0]; i < 256; ++i) { + idx->fanout[i]++; + } + + return 0; +} + +static int hash_and_save(git_indexer *idx, git_rawobj *obj, off64_t entry_start) +{ + git_oid oid; + size_t entry_size; + struct entry *entry; + struct git_pack_entry *pentry = NULL; + + entry = git__calloc(1, sizeof(*entry)); + GIT_ERROR_CHECK_ALLOC(entry); + + if (git_odb__hashobj(&oid, obj, idx->oid_type) < 0) { + git_error_set(GIT_ERROR_INDEXER, "failed to hash object"); + goto on_error; + } + + pentry = git__calloc(1, sizeof(struct git_pack_entry)); + GIT_ERROR_CHECK_ALLOC(pentry); + + git_oid_cpy(&pentry->id, &oid); + git_oid_cpy(&entry->oid, &oid); + entry->crc = crc32(0L, Z_NULL, 0); + + entry_size = (size_t)(idx->off - entry_start); + if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0) + goto on_error; + + return save_entry(idx, entry, pentry, entry_start); + +on_error: + git__free(pentry); + git__free(entry); + git__free(obj->data); + return -1; +} + +static int do_progress_callback(git_indexer *idx, git_indexer_progress *stats) +{ + if (idx->progress_cb) + return git_error_set_after_callback_function( + idx->progress_cb(stats, idx->progress_payload), + "indexer progress"); + return 0; +} + +/* Hash everything but the checksum trailer */ +static void hash_partially(git_indexer *idx, const uint8_t *data, size_t size) +{ + size_t to_expell, to_keep; + size_t oid_size = git_oid_size(idx->oid_type); + + if (size == 0) + return; + + /* + * Easy case, dump the buffer and the data minus the trailing + * checksum (SHA1 or SHA256). + */ + if (size >= oid_size) { + git_hash_update(&idx->trailer, idx->inbuf, idx->inbuf_len); + git_hash_update(&idx->trailer, data, size - oid_size); + + data += size - oid_size; + memcpy(idx->inbuf, data, oid_size); + idx->inbuf_len = oid_size; + return; + } + + /* We can just append */ + if (idx->inbuf_len + size <= oid_size) { + memcpy(idx->inbuf + idx->inbuf_len, data, size); + idx->inbuf_len += size; + return; + } + + /* We need to partially drain the buffer and then append */ + to_keep = oid_size - size; + to_expell = idx->inbuf_len - to_keep; + + git_hash_update(&idx->trailer, idx->inbuf, to_expell); + + memmove(idx->inbuf, idx->inbuf + to_expell, to_keep); + memcpy(idx->inbuf + to_keep, data, size); + idx->inbuf_len += size - to_expell; +} + +#if defined(NO_MMAP) || !defined(GIT_WIN32) + +static int write_at(git_indexer *idx, const void *data, off64_t offset, size_t size) +{ + size_t remaining_size = size; + const char *ptr = (const char *)data; + + /* Handle data size larger that ssize_t */ + while (remaining_size > 0) { + ssize_t nb; + HANDLE_EINTR(nb, p_pwrite(idx->pack->mwf.fd, (void *)ptr, + remaining_size, offset)); + if (nb <= 0) + return -1; + + ptr += nb; + offset += nb; + remaining_size -= nb; + } + + return 0; +} + +static int append_to_pack(git_indexer *idx, const void *data, size_t size) +{ + if (write_at(idx, data, idx->pack->mwf.size, size) < 0) { + git_error_set(GIT_ERROR_OS, "cannot extend packfile '%s'", idx->pack->pack_name); + return -1; + } + + return 0; +} + +#else + +/* + * Windows may keep different views to a networked file for the mmap- and + * open-accessed versions of a file, so any writes done through + * `write(2)`/`pwrite(2)` may not be reflected on the data that `mmap(2)` is + * able to read. + */ + +static int write_at(git_indexer *idx, const void *data, off64_t offset, size_t size) +{ + git_file fd = idx->pack->mwf.fd; + size_t mmap_alignment; + size_t page_offset; + off64_t page_start; + unsigned char *map_data; + git_map map; + int error; + + GIT_ASSERT_ARG(data); + GIT_ASSERT_ARG(size); + + if ((error = git__mmap_alignment(&mmap_alignment)) < 0) + return error; + + /* the offset needs to be at the mmap boundary for the platform */ + page_offset = offset % mmap_alignment; + page_start = offset - page_offset; + + if ((error = p_mmap(&map, page_offset + size, GIT_PROT_WRITE, GIT_MAP_SHARED, fd, page_start)) < 0) + return error; + + map_data = (unsigned char *)map.data; + memcpy(map_data + page_offset, data, size); + p_munmap(&map); + + return 0; +} + +static int append_to_pack(git_indexer *idx, const void *data, size_t size) +{ + off64_t new_size; + size_t mmap_alignment; + size_t page_offset; + off64_t page_start; + off64_t current_size = idx->pack->mwf.size; + int error; + + if (!size) + return 0; + + if ((error = git__mmap_alignment(&mmap_alignment)) < 0) + return error; + + /* Write a single byte to force the file system to allocate space now or + * report an error, since we can't report errors when writing using mmap. + * Round the size up to the nearest page so that we only need to perform file + * I/O when we add a page, instead of whenever we write even a single byte. */ + new_size = current_size + size; + page_offset = new_size % mmap_alignment; + page_start = new_size - page_offset; + + if (p_pwrite(idx->pack->mwf.fd, data, 1, page_start + mmap_alignment - 1) < 0) { + git_error_set(GIT_ERROR_OS, "cannot extend packfile '%s'", idx->pack->pack_name); + return -1; + } + + return write_at(idx, data, idx->pack->mwf.size, size); +} + +#endif + +static int read_stream_object(git_indexer *idx, git_indexer_progress *stats) +{ + git_packfile_stream *stream = &idx->stream; + off64_t entry_start = idx->off; + size_t oid_size, entry_size; + git_object_t type; + git_mwindow *w = NULL; + int error; + + oid_size = git_oid_size(idx->oid_type); + + if (idx->pack->mwf.size <= idx->off + (long long)oid_size) + return GIT_EBUFS; + + if (!idx->have_stream) { + error = git_packfile_unpack_header(&entry_size, &type, idx->pack, &w, &idx->off); + if (error == GIT_EBUFS) { + idx->off = entry_start; + return error; + } + if (error < 0) + return error; + + git_mwindow_close(&w); + idx->entry_start = entry_start; + git_hash_init(&idx->hash_ctx); + git_str_clear(&idx->entry_data); + + if (type == GIT_PACKFILE_REF_DELTA || type == GIT_PACKFILE_OFS_DELTA) { + error = advance_delta_offset(idx, type); + if (error == GIT_EBUFS) { + idx->off = entry_start; + return error; + } + if (error < 0) + return error; + + idx->have_delta = 1; + } else { + idx->have_delta = 0; + + error = hash_header(&idx->hash_ctx, entry_size, type); + if (error < 0) + return error; + } + + idx->have_stream = 1; + idx->entry_type = type; + + error = git_packfile_stream_open(stream, idx->pack, idx->off); + if (error < 0) + return error; + } + + if (idx->have_delta) { + error = read_object_stream(idx, stream); + } else { + error = hash_object_stream(idx, stream); + } + + idx->off = stream->curpos; + if (error == GIT_EBUFS) + return error; + + /* We want to free the stream reasorces no matter what here */ + idx->have_stream = 0; + git_packfile_stream_dispose(stream); + + if (error < 0) + return error; + + if (idx->have_delta) { + error = store_delta(idx); + } else { + error = store_object(idx); + } + + if (error < 0) + return error; + + if (!idx->have_delta) { + stats->indexed_objects++; + } + stats->received_objects++; + + if ((error = do_progress_callback(idx, stats)) != 0) + return error; + + return 0; +} + +int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_indexer_progress *stats) +{ + int error = -1; + struct git_pack_header *hdr = &idx->hdr; + git_mwindow_file *mwf = &idx->pack->mwf; + + GIT_ASSERT_ARG(idx); + GIT_ASSERT_ARG(data); + GIT_ASSERT_ARG(stats); + + if ((error = append_to_pack(idx, data, size)) < 0) + return error; + + hash_partially(idx, data, (int)size); + + /* Make sure we set the new size of the pack */ + idx->pack->mwf.size += size; + + if (!idx->parsed_header) { + unsigned int total_objects; + + if ((unsigned)idx->pack->mwf.size < sizeof(struct git_pack_header)) + return 0; + + if ((error = parse_header(&idx->hdr, idx->pack)) < 0) + return error; + + idx->parsed_header = 1; + idx->nr_objects = ntohl(hdr->hdr_entries); + idx->off = sizeof(struct git_pack_header); + + if (idx->nr_objects <= git_indexer__max_objects) { + total_objects = (unsigned int)idx->nr_objects; + } else { + git_error_set(GIT_ERROR_INDEXER, "too many objects"); + return -1; + } + + idx->pack->has_cache = 1; + if (git_vector_init(&idx->objects, total_objects, objects_cmp) < 0) + return -1; + + if (git_vector_init(&idx->deltas, total_objects / 2, NULL) < 0) + return -1; + + stats->received_objects = 0; + stats->local_objects = 0; + stats->total_deltas = 0; + stats->indexed_deltas = 0; + stats->indexed_objects = 0; + stats->total_objects = total_objects; + + if ((error = do_progress_callback(idx, stats)) != 0) + return error; + } + + /* Now that we have data in the pack, let's try to parse it */ + + /* As the file grows any windows we try to use will be out of date */ + if ((error = git_mwindow_free_all(mwf)) < 0) + goto on_error; + + while (stats->indexed_objects < idx->nr_objects) { + if ((error = read_stream_object(idx, stats)) != 0) { + if (error == GIT_EBUFS) + break; + else + goto on_error; + } + } + + return 0; + +on_error: + git_mwindow_free_all(mwf); + return error; +} + +static int index_path(git_str *path, git_indexer *idx, const char *suffix) +{ + const char prefix[] = "pack-"; + size_t slash = (size_t)path->size; + + /* search backwards for '/' */ + while (slash > 0 && path->ptr[slash - 1] != '/') + slash--; + + if (git_str_grow(path, slash + 1 + strlen(prefix) + + git_oid_hexsize(idx->oid_type) + strlen(suffix) + 1) < 0) + return -1; + + git_str_truncate(path, slash); + git_str_puts(path, prefix); + git_str_puts(path, idx->name); + git_str_puts(path, suffix); + + return git_str_oom(path) ? -1 : 0; +} + +/** + * Rewind the packfile by the trailer, as we might need to fix the + * packfile by injecting objects at the tail and must overwrite it. + */ +static int seek_back_trailer(git_indexer *idx) +{ + idx->pack->mwf.size -= git_oid_size(idx->oid_type); + return git_mwindow_free_all(&idx->pack->mwf); +} + +static int inject_object(git_indexer *idx, git_oid *id) +{ + git_odb_object *obj = NULL; + struct entry *entry = NULL; + struct git_pack_entry *pentry = NULL; + unsigned char empty_checksum[GIT_HASH_MAX_SIZE] = {0}; + unsigned char hdr[64]; + git_str buf = GIT_STR_INIT; + off64_t entry_start; + const void *data; + size_t len, hdr_len; + size_t checksum_size; + int error; + + checksum_size = git_hash_size(indexer_hash_algorithm(idx)); + + if ((error = seek_back_trailer(idx)) < 0) + goto cleanup; + + entry_start = idx->pack->mwf.size; + + if ((error = git_odb_read(&obj, idx->odb, id)) < 0) { + git_error_set(GIT_ERROR_INDEXER, "missing delta bases"); + goto cleanup; + } + + data = git_odb_object_data(obj); + len = git_odb_object_size(obj); + + entry = git__calloc(1, sizeof(*entry)); + GIT_ERROR_CHECK_ALLOC(entry); + + entry->crc = crc32(0L, Z_NULL, 0); + + /* Write out the object header */ + if ((error = git_packfile__object_header(&hdr_len, hdr, len, git_odb_object_type(obj))) < 0 || + (error = append_to_pack(idx, hdr, hdr_len)) < 0) + goto cleanup; + + idx->pack->mwf.size += hdr_len; + entry->crc = crc32(entry->crc, hdr, (uInt)hdr_len); + + if ((error = git_zstream_deflatebuf(&buf, data, len)) < 0) + goto cleanup; + + /* And then the compressed object */ + if ((error = append_to_pack(idx, buf.ptr, buf.size)) < 0) + goto cleanup; + + idx->pack->mwf.size += buf.size; + entry->crc = htonl(crc32(entry->crc, (unsigned char *)buf.ptr, (uInt)buf.size)); + git_str_dispose(&buf); + + /* Write a fake trailer so the pack functions play ball */ + + if ((error = append_to_pack(idx, empty_checksum, checksum_size)) < 0) + goto cleanup; + + idx->pack->mwf.size += git_oid_size(idx->oid_type); + + pentry = git__calloc(1, sizeof(struct git_pack_entry)); + GIT_ERROR_CHECK_ALLOC(pentry); + + git_oid_cpy(&pentry->id, id); + git_oid_cpy(&entry->oid, id); + idx->off = entry_start + hdr_len + len; + + error = save_entry(idx, entry, pentry, entry_start); + +cleanup: + if (error) { + git__free(entry); + git__free(pentry); + } + + git_odb_object_free(obj); + return error; +} + +static int fix_thin_pack(git_indexer *idx, git_indexer_progress *stats) +{ + int error, found_ref_delta = 0; + unsigned int i; + struct delta_info *delta; + size_t size; + git_object_t type; + git_mwindow *w = NULL; + off64_t curpos = 0; + unsigned char *base_info; + unsigned int left = 0; + git_oid base; + + GIT_ASSERT(git_vector_length(&idx->deltas) > 0); + + if (idx->odb == NULL) { + git_error_set(GIT_ERROR_INDEXER, "cannot fix a thin pack without an ODB"); + return -1; + } + + /* Loop until we find the first REF delta */ + git_vector_foreach(&idx->deltas, i, delta) { + if (!delta) + continue; + + curpos = delta->delta_off; + error = git_packfile_unpack_header(&size, &type, idx->pack, &w, &curpos); + if (error < 0) + return error; + + if (type == GIT_PACKFILE_REF_DELTA) { + found_ref_delta = 1; + break; + } + } + + if (!found_ref_delta) { + git_error_set(GIT_ERROR_INDEXER, "no REF_DELTA found, cannot inject object"); + return -1; + } + + /* curpos now points to the base information, which is an OID */ + base_info = git_mwindow_open(&idx->pack->mwf, &w, curpos, git_oid_size(idx->oid_type), &left); + if (base_info == NULL) { + git_error_set(GIT_ERROR_INDEXER, "failed to map delta information"); + return -1; + } + + git_oid_from_raw(&base, base_info, idx->oid_type); + git_mwindow_close(&w); + + if (has_entry(idx, &base)) + return 0; + + if (inject_object(idx, &base) < 0) + return -1; + + stats->local_objects++; + + return 0; +} + +static int resolve_deltas(git_indexer *idx, git_indexer_progress *stats) +{ + unsigned int i; + int error; + struct delta_info *delta; + int progressed = 0, non_null = 0, progress_cb_result; + + while (idx->deltas.length > 0) { + progressed = 0; + non_null = 0; + git_vector_foreach(&idx->deltas, i, delta) { + git_rawobj obj = {0}; + + if (!delta) + continue; + + non_null = 1; + idx->off = delta->delta_off; + if ((error = git_packfile_unpack(&obj, idx->pack, &idx->off)) < 0) { + if (error == GIT_PASSTHROUGH) { + /* We have not seen the base object, we'll try again later. */ + continue; + } + return -1; + } + + if (idx->do_verify && check_object_connectivity(idx, &obj) < 0) + /* TODO: error? continue? */ + continue; + + if (hash_and_save(idx, &obj, delta->delta_off) < 0) + continue; + + git__free(obj.data); + stats->indexed_objects++; + stats->indexed_deltas++; + progressed = 1; + if ((progress_cb_result = do_progress_callback(idx, stats)) < 0) + return progress_cb_result; + + /* remove from the list */ + git_vector_set(NULL, &idx->deltas, i, NULL); + git__free(delta); + } + + /* if none were actually set, we're done */ + if (!non_null) + break; + + if (!progressed && (fix_thin_pack(idx, stats) < 0)) { + return -1; + } + } + + return 0; +} + +static int update_header_and_rehash(git_indexer *idx, git_indexer_progress *stats) +{ + void *ptr; + size_t chunk = 1024*1024; + off64_t hashed = 0; + git_mwindow *w = NULL; + git_mwindow_file *mwf; + unsigned int left; + + mwf = &idx->pack->mwf; + + git_hash_init(&idx->trailer); + + + /* Update the header to include the number of local objects we injected */ + idx->hdr.hdr_entries = htonl(stats->total_objects + stats->local_objects); + if (write_at(idx, &idx->hdr, 0, sizeof(struct git_pack_header)) < 0) + return -1; + + /* + * We now use the same technique as before to determine the + * hash. We keep reading up to the end and let + * hash_partially() keep the existing trailer out of the + * calculation. + */ + if (git_mwindow_free_all(mwf) < 0) + return -1; + + idx->inbuf_len = 0; + while (hashed < mwf->size) { + ptr = git_mwindow_open(mwf, &w, hashed, chunk, &left); + if (ptr == NULL) + return -1; + + hash_partially(idx, ptr, left); + hashed += left; + + git_mwindow_close(&w); + } + + return 0; +} + +int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats) +{ + git_mwindow *w = NULL; + unsigned int i, long_offsets = 0, left; + int error; + struct git_pack_idx_header hdr; + git_str filename = GIT_STR_INIT; + struct entry *entry; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + git_filebuf index_file = {0}; + void *packfile_trailer; + size_t checksum_size; + int filebuf_hash; + bool mismatch; + + if (!idx->parsed_header) { + git_error_set(GIT_ERROR_INDEXER, "incomplete pack header"); + return -1; + } + + checksum_size = git_hash_size(indexer_hash_algorithm(idx)); + filebuf_hash = git_filebuf_hash_flags(indexer_hash_algorithm(idx)); + GIT_ASSERT(checksum_size); + + /* Test for this before resolve_deltas(), as it plays with idx->off */ + if (idx->off + (ssize_t)checksum_size < idx->pack->mwf.size) { + git_error_set(GIT_ERROR_INDEXER, "unexpected data at the end of the pack"); + return -1; + } + if (idx->off + (ssize_t)checksum_size > idx->pack->mwf.size) { + git_error_set(GIT_ERROR_INDEXER, "missing trailer at the end of the pack"); + return -1; + } + + packfile_trailer = git_mwindow_open(&idx->pack->mwf, &w, idx->pack->mwf.size - checksum_size, checksum_size, &left); + if (packfile_trailer == NULL) { + git_mwindow_close(&w); + goto on_error; + } + + /* Compare the packfile trailer as it was sent to us and what we calculated */ + git_hash_final(checksum, &idx->trailer); + mismatch = !!memcmp(checksum, packfile_trailer, checksum_size); + git_mwindow_close(&w); + + if (mismatch) { + git_error_set(GIT_ERROR_INDEXER, "packfile trailer mismatch"); + return -1; + } + + /* Freeze the number of deltas */ + stats->total_deltas = stats->total_objects - stats->indexed_objects; + + if ((error = resolve_deltas(idx, stats)) < 0) + return error; + + if (stats->indexed_objects != stats->total_objects) { + git_error_set(GIT_ERROR_INDEXER, "early EOF"); + return -1; + } + + if (stats->local_objects > 0) { + if (update_header_and_rehash(idx, stats) < 0) + return -1; + + git_hash_final(checksum, &idx->trailer); + write_at(idx, checksum, idx->pack->mwf.size - checksum_size, checksum_size); + } + + /* + * Is the resulting graph fully connected or are we still + * missing some objects? In the second case, we can + * bail out due to an incomplete and thus corrupt + * packfile. + */ + if (git_indexer_oidmap_size(&idx->expected_oids) > 0) { + git_error_set(GIT_ERROR_INDEXER, "packfile is missing %"PRIuZ" objects", + (size_t)git_indexer_oidmap_size(&idx->expected_oids)); + return -1; + } + + git_vector_sort(&idx->objects); + + /* Use the trailer hash as the pack file name to ensure + * files with different contents have different names */ + memcpy(idx->checksum, checksum, checksum_size); + if (git_hash_fmt(idx->name, checksum, checksum_size) < 0) + return -1; + + git_str_sets(&filename, idx->pack->pack_name); + git_str_shorten(&filename, strlen("pack")); + git_str_puts(&filename, "idx"); + if (git_str_oom(&filename)) + return -1; + + if (git_filebuf_open(&index_file, filename.ptr, + filebuf_hash | (idx->do_fsync ? GIT_FILEBUF_FSYNC : 0), + idx->mode) < 0) + goto on_error; + + /* Write out the header */ + hdr.idx_signature = htonl(PACK_IDX_SIGNATURE); + hdr.idx_version = htonl(2); + git_filebuf_write(&index_file, &hdr, sizeof(hdr)); + + /* Write out the fanout table */ + for (i = 0; i < 256; ++i) { + uint32_t n = htonl(idx->fanout[i]); + git_filebuf_write(&index_file, &n, sizeof(n)); + } + + /* Write out the object names (SHA-1 hashes) */ + git_vector_foreach(&idx->objects, i, entry) { + git_filebuf_write(&index_file, &entry->oid.id, git_oid_size(idx->oid_type)); + } + + /* Write out the CRC32 values */ + git_vector_foreach(&idx->objects, i, entry) { + git_filebuf_write(&index_file, &entry->crc, sizeof(uint32_t)); + } + + /* Write out the offsets */ + git_vector_foreach(&idx->objects, i, entry) { + uint32_t n; + + if (entry->offset == UINT32_MAX) + n = htonl(0x80000000 | long_offsets++); + else + n = htonl(entry->offset); + + git_filebuf_write(&index_file, &n, sizeof(uint32_t)); + } + + /* Write out the long offsets */ + git_vector_foreach(&idx->objects, i, entry) { + uint32_t split[2]; + + if (entry->offset != UINT32_MAX) + continue; + + split[0] = htonl(entry->offset_long >> 32); + split[1] = htonl(entry->offset_long & 0xffffffff); + + git_filebuf_write(&index_file, &split, sizeof(uint32_t) * 2); + } + + /* Write out the packfile trailer to the index */ + if (git_filebuf_write(&index_file, checksum, checksum_size) < 0) + goto on_error; + + /* Write out the hash of the idx */ + if (git_filebuf_hash(checksum, &index_file) < 0) + goto on_error; + + git_filebuf_write(&index_file, checksum, checksum_size); + + /* Figure out what the final name should be */ + if (index_path(&filename, idx, ".idx") < 0) + goto on_error; + + /* Commit file */ + if (git_filebuf_commit_at(&index_file, filename.ptr) < 0) + goto on_error; + + if (git_mwindow_free_all(&idx->pack->mwf) < 0) + goto on_error; + +#if !defined(NO_MMAP) && defined(GIT_WIN32) + /* + * Some non-Windows remote filesystems fail when truncating files if the + * file permissions change after opening the file (done by p_mkstemp). + * + * Truncation is only needed when mmap is used to undo rounding up to next + * page_size in append_to_pack. + */ + if (p_ftruncate(idx->pack->mwf.fd, idx->pack->mwf.size) < 0) { + git_error_set(GIT_ERROR_OS, "failed to truncate pack file '%s'", idx->pack->pack_name); + return -1; + } +#endif + + if (idx->do_fsync && p_fsync(idx->pack->mwf.fd) < 0) { + git_error_set(GIT_ERROR_OS, "failed to fsync packfile"); + goto on_error; + } + + /* We need to close the descriptor here so Windows doesn't choke on commit_at */ + if (p_close(idx->pack->mwf.fd) < 0) { + git_error_set(GIT_ERROR_OS, "failed to close packfile"); + goto on_error; + } + + idx->pack->mwf.fd = -1; + + if (index_path(&filename, idx, ".pack") < 0) + goto on_error; + + /* And don't forget to rename the packfile to its new place. */ + if (p_rename(idx->pack->pack_name, git_str_cstr(&filename)) < 0) + goto on_error; + + /* And fsync the parent directory if we're asked to. */ + if (idx->do_fsync && + git_futils_fsync_parent(git_str_cstr(&filename)) < 0) + goto on_error; + + idx->pack_committed = 1; + + git_str_dispose(&filename); + return 0; + +on_error: + git_mwindow_free_all(&idx->pack->mwf); + git_filebuf_cleanup(&index_file); + git_str_dispose(&filename); + return -1; +} + +void git_indexer_free(git_indexer *idx) +{ + struct git_pack_entry *pentry; + git_oid *id; + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + + if (idx == NULL) + return; + + if (idx->have_stream) + git_packfile_stream_dispose(&idx->stream); + + git_vector_dispose_deep(&idx->objects); + + while (git_pack_oidmap_iterate(&iter, NULL, &pentry, &idx->pack->idx_cache) == 0) + git__free(pentry); + + git_pack_oidmap_dispose(&idx->pack->idx_cache); + + git_vector_dispose_deep(&idx->deltas); + + git_packfile_free(idx->pack, !idx->pack_committed); + + iter = GIT_HASHMAP_ITER_INIT; + while (git_indexer_oidmap_iterate(&iter, NULL, &id, &idx->expected_oids) == 0) + git__free(id); + + git_hash_ctx_cleanup(&idx->trailer); + git_hash_ctx_cleanup(&idx->hash_ctx); + git_str_dispose(&idx->entry_data); + git_indexer_oidmap_dispose(&idx->expected_oids); + git__free(idx); +} diff --git a/src/libgit2/indexer.h b/src/libgit2/indexer.h new file mode 100644 index 00000000000..8ee6115a656 --- /dev/null +++ b/src/libgit2/indexer.h @@ -0,0 +1,16 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_indexer_h__ +#define INCLUDE_indexer_h__ + +#include "common.h" + +#include "git2/indexer.h" + +extern void git_indexer__set_fsync(git_indexer *idx, int do_fsync); + +#endif diff --git a/src/libgit2/iterator.c b/src/libgit2/iterator.c new file mode 100644 index 00000000000..4eca11f7cd1 --- /dev/null +++ b/src/libgit2/iterator.c @@ -0,0 +1,2462 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "iterator.h" + +#include "tree.h" +#include "index.h" +#include "path.h" + +#define GIT_ITERATOR_FIRST_ACCESS (1 << 15) +#define GIT_ITERATOR_HONOR_IGNORES (1 << 16) +#define GIT_ITERATOR_IGNORE_DOT_GIT (1 << 17) + +#define iterator__flag(I,F) ((((git_iterator *)(I))->flags & GIT_ITERATOR_ ## F) != 0) +#define iterator__ignore_case(I) iterator__flag(I,IGNORE_CASE) +#define iterator__include_trees(I) iterator__flag(I,INCLUDE_TREES) +#define iterator__dont_autoexpand(I) iterator__flag(I,DONT_AUTOEXPAND) +#define iterator__do_autoexpand(I) !iterator__flag(I,DONT_AUTOEXPAND) +#define iterator__include_conflicts(I) iterator__flag(I,INCLUDE_CONFLICTS) +#define iterator__has_been_accessed(I) iterator__flag(I,FIRST_ACCESS) +#define iterator__honor_ignores(I) iterator__flag(I,HONOR_IGNORES) +#define iterator__ignore_dot_git(I) iterator__flag(I,IGNORE_DOT_GIT) +#define iterator__descend_symlinks(I) iterator__flag(I,DESCEND_SYMLINKS) + +static void iterator_set_ignore_case(git_iterator *iter, bool ignore_case) +{ + int (*vector_cmp)(const void *a, const void *b); + + if (ignore_case) + iter->flags |= GIT_ITERATOR_IGNORE_CASE; + else + iter->flags &= ~GIT_ITERATOR_IGNORE_CASE; + + iter->strcomp = ignore_case ? git__strcasecmp : git__strcmp; + iter->strncomp = ignore_case ? git__strncasecmp : git__strncmp; + iter->prefixcomp = ignore_case ? git__prefixcmp_icase : git__prefixcmp; + iter->entry_srch = ignore_case ? git_index_entry_isrch : git_index_entry_srch; + + vector_cmp = ignore_case ? git__strcasecmp_cb : git__strcmp_cb; + + git_vector_set_cmp(&iter->pathlist, vector_cmp); +} + +static int iterator_range_init( + git_iterator *iter, const char *start, const char *end) +{ + if (start && *start) { + iter->start = git__strdup(start); + GIT_ERROR_CHECK_ALLOC(iter->start); + + iter->start_len = strlen(iter->start); + } + + if (end && *end) { + iter->end = git__strdup(end); + GIT_ERROR_CHECK_ALLOC(iter->end); + + iter->end_len = strlen(iter->end); + } + + iter->started = (iter->start == NULL); + iter->ended = false; + + return 0; +} + +static void iterator_range_free(git_iterator *iter) +{ + if (iter->start) { + git__free(iter->start); + iter->start = NULL; + iter->start_len = 0; + } + + if (iter->end) { + git__free(iter->end); + iter->end = NULL; + iter->end_len = 0; + } +} + +static int iterator_reset_range( + git_iterator *iter, const char *start, const char *end) +{ + iterator_range_free(iter); + return iterator_range_init(iter, start, end); +} + +static int iterator_pathlist_init(git_iterator *iter, git_strarray *pathlist) +{ + size_t i; + + if (git_vector_init(&iter->pathlist, pathlist->count, NULL) < 0) + return -1; + + for (i = 0; i < pathlist->count; i++) { + if (!pathlist->strings[i]) + continue; + + if (git_vector_insert(&iter->pathlist, pathlist->strings[i]) < 0) + return -1; + } + + return 0; +} + +static int iterator_init_common( + git_iterator *iter, + git_repository *repo, + git_index *index, + git_iterator_options *given_opts) +{ + static git_iterator_options default_opts = GIT_ITERATOR_OPTIONS_INIT; + git_iterator_options *options = given_opts ? given_opts : &default_opts; + bool ignore_case; + int precompose; + int error; + + iter->repo = repo; + iter->index = index; + iter->flags = options->flags; + + if ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) { + ignore_case = true; + } else if ((iter->flags & GIT_ITERATOR_DONT_IGNORE_CASE) != 0) { + ignore_case = false; + } else if (repo) { + git_index *index; + + if ((error = git_repository_index__weakptr(&index, iter->repo)) < 0) + return error; + + ignore_case = !!index->ignore_case; + + if (ignore_case == 1) + iter->flags |= GIT_ITERATOR_IGNORE_CASE; + else + iter->flags |= GIT_ITERATOR_DONT_IGNORE_CASE; + } else { + ignore_case = false; + } + + /* try to look up precompose and set flag if appropriate */ + if (repo && + (iter->flags & GIT_ITERATOR_PRECOMPOSE_UNICODE) == 0 && + (iter->flags & GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE) == 0) { + + if (git_repository__configmap_lookup(&precompose, repo, GIT_CONFIGMAP_PRECOMPOSE) < 0) + git_error_clear(); + else if (precompose) + iter->flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE; + } + + if ((iter->flags & GIT_ITERATOR_DONT_AUTOEXPAND)) + iter->flags |= GIT_ITERATOR_INCLUDE_TREES; + + if ((error = iterator_range_init(iter, options->start, options->end)) < 0 || + (error = iterator_pathlist_init(iter, &options->pathlist)) < 0) + return error; + + iterator_set_ignore_case(iter, ignore_case); + return 0; +} + +static void iterator_clear(git_iterator *iter) +{ + iter->started = false; + iter->ended = false; + iter->stat_calls = 0; + iter->pathlist_walk_idx = 0; + iter->flags &= ~GIT_ITERATOR_FIRST_ACCESS; +} + +GIT_INLINE(bool) iterator_has_started( + git_iterator *iter, const char *path, bool is_submodule) +{ + size_t path_len; + + if (iter->start == NULL || iter->started == true) + return true; + + /* the starting path is generally a prefix - we have started once we + * are prefixed by this path + */ + iter->started = (iter->prefixcomp(path, iter->start) >= 0); + + if (iter->started) + return true; + + path_len = strlen(path); + + /* if, however, we are a submodule, then we support `start` being + * suffixed with a `/` for crazy legacy reasons. match `submod` + * with a start path of `submod/`. + */ + if (is_submodule && iter->start_len && path_len == iter->start_len - 1 && + iter->start[iter->start_len-1] == '/') + return true; + + /* if, however, our current path is a directory, and our starting path + * is _beneath_ that directory, then recurse into the directory (even + * though we have not yet "started") + */ + if (path_len > 0 && path[path_len-1] == '/' && + iter->strncomp(path, iter->start, path_len) == 0) + return true; + + return false; +} + +GIT_INLINE(bool) iterator_has_ended(git_iterator *iter, const char *path) +{ + if (iter->end == NULL) + return false; + else if (iter->ended) + return true; + + iter->ended = (iter->prefixcomp(path, iter->end) > 0); + return iter->ended; +} + +/* walker for the index and tree iterator that allows it to walk the sorted + * pathlist entries alongside sorted iterator entries. + */ +static bool iterator_pathlist_next_is(git_iterator *iter, const char *path) +{ + char *p; + size_t path_len, p_len, cmp_len, i; + int cmp; + + if (iter->pathlist.length == 0) + return true; + + git_vector_sort(&iter->pathlist); + + path_len = strlen(path); + + /* for comparison, drop the trailing slash on the current '/' */ + if (path_len && path[path_len-1] == '/') + path_len--; + + for (i = iter->pathlist_walk_idx; i < iter->pathlist.length; i++) { + p = iter->pathlist.contents[i]; + p_len = strlen(p); + + if (p_len && p[p_len-1] == '/') + p_len--; + + cmp_len = min(path_len, p_len); + + /* see if the pathlist entry is a prefix of this path */ + cmp = iter->strncomp(p, path, cmp_len); + + /* prefix match - see if there's an exact match, or if we were + * given a path that matches the directory + */ + if (cmp == 0) { + /* if this pathlist entry is not suffixed with a '/' then + * it matches a path that is a file or a directory. + * (eg, pathlist = "foo" and path is "foo" or "foo/" or + * "foo/something") + */ + if (p[cmp_len] == '\0' && + (path[cmp_len] == '\0' || path[cmp_len] == '/')) + return true; + + /* if this pathlist entry _is_ suffixed with a '/' then + * it matches only paths that are directories. + * (eg, pathlist = "foo/" and path is "foo/" or "foo/something") + */ + if (p[cmp_len] == '/' && path[cmp_len] == '/') + return true; + } + + /* this pathlist entry sorts before the given path, try the next */ + else if (cmp < 0) { + iter->pathlist_walk_idx++; + continue; + } + + /* this pathlist sorts after the given path, no match. */ + else if (cmp > 0) { + break; + } + } + + return false; +} + +typedef enum { + ITERATOR_PATHLIST_NONE = 0, + ITERATOR_PATHLIST_IS_FILE = 1, + ITERATOR_PATHLIST_IS_DIR = 2, + ITERATOR_PATHLIST_IS_PARENT = 3, + ITERATOR_PATHLIST_FULL = 4 +} iterator_pathlist_search_t; + +static iterator_pathlist_search_t iterator_pathlist_search( + git_iterator *iter, const char *path, size_t path_len) +{ + int (*vector_cmp)(const void *a, const void *b); + const char *p; + size_t idx; + int error; + + if (iter->pathlist.length == 0) + return ITERATOR_PATHLIST_FULL; + + git_vector_sort(&iter->pathlist); + + vector_cmp = (iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0 ? + git__strcasecmp_cb : git__strcmp_cb; + + error = git_vector_bsearch2(&idx, &iter->pathlist, vector_cmp, path); + + /* the given path was found in the pathlist. since the pathlist only + * matches directories when they're suffixed with a '/', analyze the + * path string to determine whether it's a directory or not. + */ + if (error == 0) { + if (path_len && path[path_len-1] == '/') + return ITERATOR_PATHLIST_IS_DIR; + + return ITERATOR_PATHLIST_IS_FILE; + } + + /* at this point, the path we're examining may be a directory (though we + * don't know that yet, since we're avoiding a stat unless it's necessary) + * so walk the pathlist looking for the given path with a '/' after it, + */ + while ((p = git_vector_get(&iter->pathlist, idx)) != NULL) { + if (iter->prefixcomp(p, path) != 0) + break; + + /* an exact match would have been matched by the bsearch above */ + GIT_ASSERT_WITH_RETVAL(p[path_len], ITERATOR_PATHLIST_NONE); + + /* is this a literal directory entry (eg `foo/`) or a file beneath */ + if (p[path_len] == '/') { + return (p[path_len+1] == '\0') ? + ITERATOR_PATHLIST_IS_DIR : + ITERATOR_PATHLIST_IS_PARENT; + } + + if (p[path_len] > '/') + break; + + idx++; + } + + return ITERATOR_PATHLIST_NONE; +} + +/* Empty iterator */ + +static int empty_iterator_noop(const git_index_entry **e, git_iterator *i) +{ + GIT_UNUSED(i); + + if (e) + *e = NULL; + + return GIT_ITEROVER; +} + +static int empty_iterator_advance_over( + const git_index_entry **e, + git_iterator_status_t *s, + git_iterator *i) +{ + *s = GIT_ITERATOR_STATUS_EMPTY; + return empty_iterator_noop(e, i); +} + +static int empty_iterator_reset(git_iterator *i) +{ + GIT_UNUSED(i); + return 0; +} + +static void empty_iterator_free(git_iterator *i) +{ + GIT_UNUSED(i); +} + +typedef struct { + git_iterator base; + git_iterator_callbacks cb; +} empty_iterator; + +int git_iterator_for_nothing( + git_iterator **out, + git_iterator_options *options) +{ + empty_iterator *iter; + + static git_iterator_callbacks callbacks = { + empty_iterator_noop, + empty_iterator_noop, + empty_iterator_noop, + empty_iterator_advance_over, + empty_iterator_reset, + empty_iterator_free + }; + + *out = NULL; + + iter = git__calloc(1, sizeof(empty_iterator)); + GIT_ERROR_CHECK_ALLOC(iter); + + iter->base.type = GIT_ITERATOR_EMPTY; + iter->base.cb = &callbacks; + iter->base.flags = options->flags; + + *out = &iter->base; + return 0; +} + +/* Tree iterator */ + +typedef struct { + git_tree_entry *tree_entry; + const char *parent_path; +} tree_iterator_entry; + +typedef struct { + git_tree *tree; + + /* path to this particular frame (folder) */ + git_str path; + + /* a sorted list of the entries for this frame (folder), these are + * actually pointers to the iterator's entry pool. + */ + git_vector entries; + tree_iterator_entry *current; + + size_t next_idx; + + /* on case insensitive iterations, we also have an array of other + * paths that were case insensitively equal to this one, and their + * tree objects. we have coalesced the tree entries into this frame. + * a child `tree_iterator_entry` will contain a pointer to its actual + * parent path. + */ + git_vector similar_trees; + git_array_t(git_str) similar_paths; +} tree_iterator_frame; + +typedef struct { + git_iterator base; + git_tree *root; + git_array_t(tree_iterator_frame) frames; + + git_index_entry entry; + git_str entry_path; + + /* a pool of entries to reduce the number of allocations */ + git_pool entry_pool; +} tree_iterator; + +GIT_INLINE(tree_iterator_frame *) tree_iterator_parent_frame( + tree_iterator *iter) +{ + return iter->frames.size > 1 ? + &iter->frames.ptr[iter->frames.size-2] : NULL; +} + +GIT_INLINE(tree_iterator_frame *) tree_iterator_current_frame( + tree_iterator *iter) +{ + return iter->frames.size ? &iter->frames.ptr[iter->frames.size-1] : NULL; +} + +GIT_INLINE(int) tree_entry_cmp( + const git_tree_entry *a, const git_tree_entry *b, bool icase) +{ + return git_fs_path_cmp( + a->filename, a->filename_len, a->attr == GIT_FILEMODE_TREE, + b->filename, b->filename_len, b->attr == GIT_FILEMODE_TREE, + icase ? git__strncasecmp : git__strncmp); +} + +GIT_INLINE(int) tree_iterator_entry_cmp_icase( + const void *ptr_a, const void *ptr_b) +{ + const tree_iterator_entry *a = (const tree_iterator_entry *)ptr_a; + const tree_iterator_entry *b = (const tree_iterator_entry *)ptr_b; + + return tree_entry_cmp(a->tree_entry, b->tree_entry, true); +} + +static int tree_iterator_entry_sort_icase(const void *ptr_a, const void *ptr_b) +{ + const tree_iterator_entry *a = (const tree_iterator_entry *)ptr_a; + const tree_iterator_entry *b = (const tree_iterator_entry *)ptr_b; + + int c = tree_entry_cmp(a->tree_entry, b->tree_entry, true); + + /* stabilize the sort order for filenames that are (case insensitively) + * the same by examining the parent path (case sensitively) before + * falling back to a case sensitive sort of the filename. + */ + if (!c && a->parent_path != b->parent_path) + c = git__strcmp(a->parent_path, b->parent_path); + + if (!c) + c = tree_entry_cmp(a->tree_entry, b->tree_entry, false); + + return c; +} + +static int tree_iterator_compute_path( + git_str *out, + tree_iterator_entry *entry) +{ + git_str_clear(out); + + if (entry->parent_path) + git_str_joinpath(out, entry->parent_path, entry->tree_entry->filename); + else + git_str_puts(out, entry->tree_entry->filename); + + if (git_tree_entry__is_tree(entry->tree_entry)) + git_str_putc(out, '/'); + + if (git_str_oom(out)) + return -1; + + return 0; +} + +static int tree_iterator_frame_init( + tree_iterator *iter, + git_tree *tree, + tree_iterator_entry *frame_entry) +{ + tree_iterator_frame *new_frame = NULL; + tree_iterator_entry *new_entry; + git_tree *dup = NULL; + git_tree_entry *tree_entry; + git_vector_cmp cmp; + size_t i; + int error = 0; + + new_frame = git_array_alloc(iter->frames); + GIT_ERROR_CHECK_ALLOC(new_frame); + + if ((error = git_tree_dup(&dup, tree)) < 0) + goto done; + + memset(new_frame, 0x0, sizeof(tree_iterator_frame)); + new_frame->tree = dup; + + if (frame_entry && + (error = tree_iterator_compute_path(&new_frame->path, frame_entry)) < 0) + goto done; + + cmp = iterator__ignore_case(&iter->base) ? + tree_iterator_entry_sort_icase : NULL; + + if ((error = git_vector_init(&new_frame->entries, + dup->entries.size, cmp)) < 0) + goto done; + + git_array_foreach(dup->entries, i, tree_entry) { + if ((new_entry = git_pool_malloc(&iter->entry_pool, 1)) == NULL) { + git_error_set_oom(); + error = -1; + goto done; + } + + new_entry->tree_entry = tree_entry; + new_entry->parent_path = new_frame->path.ptr; + + if ((error = git_vector_insert(&new_frame->entries, new_entry)) < 0) + goto done; + } + + git_vector_set_sorted(&new_frame->entries, + !iterator__ignore_case(&iter->base)); + +done: + if (error < 0) { + git_tree_free(dup); + git_array_pop(iter->frames); + } + + return error; +} + +GIT_INLINE(tree_iterator_entry *) tree_iterator_current_entry( + tree_iterator_frame *frame) +{ + return frame->current; +} + +GIT_INLINE(int) tree_iterator_frame_push_neighbors( + tree_iterator *iter, + tree_iterator_frame *parent_frame, + tree_iterator_frame *frame, + const char *filename) +{ + tree_iterator_entry *entry, *new_entry; + git_tree *tree = NULL; + git_tree_entry *tree_entry; + git_str *path; + size_t new_size, i; + int error = 0; + + while (parent_frame->next_idx < parent_frame->entries.length) { + entry = parent_frame->entries.contents[parent_frame->next_idx]; + + if (strcasecmp(filename, entry->tree_entry->filename) != 0) + break; + + if ((error = git_tree_lookup(&tree, + iter->base.repo, &entry->tree_entry->oid)) < 0) + break; + + if (git_vector_insert(&parent_frame->similar_trees, tree) < 0) + break; + + path = git_array_alloc(parent_frame->similar_paths); + GIT_ERROR_CHECK_ALLOC(path); + + memset(path, 0, sizeof(git_str)); + + if ((error = tree_iterator_compute_path(path, entry)) < 0) + break; + + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, + frame->entries.length, tree->entries.size); + git_vector_size_hint(&frame->entries, new_size); + + git_array_foreach(tree->entries, i, tree_entry) { + new_entry = git_pool_malloc(&iter->entry_pool, 1); + GIT_ERROR_CHECK_ALLOC(new_entry); + + new_entry->tree_entry = tree_entry; + new_entry->parent_path = path->ptr; + + if ((error = git_vector_insert(&frame->entries, new_entry)) < 0) + break; + } + + if (error) + break; + + parent_frame->next_idx++; + } + + return error; +} + +GIT_INLINE(int) tree_iterator_frame_push( + tree_iterator *iter, tree_iterator_entry *entry) +{ + tree_iterator_frame *parent_frame, *frame; + git_tree *tree = NULL; + int error; + + if ((error = git_tree_lookup(&tree, + iter->base.repo, &entry->tree_entry->oid)) < 0 || + (error = tree_iterator_frame_init(iter, tree, entry)) < 0) + goto done; + + parent_frame = tree_iterator_parent_frame(iter); + frame = tree_iterator_current_frame(iter); + + /* if we're case insensitive, then we may have another directory that + * is (case insensitively) equal to this one. coalesce those children + * into this tree. + */ + if (iterator__ignore_case(&iter->base)) + error = tree_iterator_frame_push_neighbors(iter, + parent_frame, frame, entry->tree_entry->filename); + +done: + git_tree_free(tree); + return error; +} + +static int tree_iterator_frame_pop(tree_iterator *iter) +{ + tree_iterator_frame *frame; + git_str *buf = NULL; + git_tree *tree; + size_t i; + + GIT_ASSERT(iter->frames.size); + + frame = git_array_pop(iter->frames); + + git_vector_dispose(&frame->entries); + git_tree_free(frame->tree); + + do { + buf = git_array_pop(frame->similar_paths); + git_str_dispose(buf); + } while (buf != NULL); + + git_array_clear(frame->similar_paths); + + git_vector_foreach(&frame->similar_trees, i, tree) + git_tree_free(tree); + + git_vector_dispose(&frame->similar_trees); + + git_str_dispose(&frame->path); + + return 0; +} + +static int tree_iterator_current( + const git_index_entry **out, git_iterator *i) +{ + tree_iterator *iter = (tree_iterator *)i; + + if (!iterator__has_been_accessed(i)) + return iter->base.cb->advance(out, i); + + if (!iter->frames.size) { + *out = NULL; + return GIT_ITEROVER; + } + + *out = &iter->entry; + return 0; +} + +static void tree_iterator_set_current( + tree_iterator *iter, + tree_iterator_frame *frame, + tree_iterator_entry *entry) +{ + git_tree_entry *tree_entry = entry->tree_entry; + + frame->current = entry; + + memset(&iter->entry, 0x0, sizeof(git_index_entry)); + + iter->entry.mode = tree_entry->attr; + iter->entry.path = iter->entry_path.ptr; + git_oid_cpy(&iter->entry.id, &tree_entry->oid); +} + +static int tree_iterator_advance(const git_index_entry **out, git_iterator *i) +{ + tree_iterator *iter = (tree_iterator *)i; + int error = 0; + + iter->base.flags |= GIT_ITERATOR_FIRST_ACCESS; + + /* examine tree entries until we find the next one to return */ + while (true) { + tree_iterator_entry *prev_entry, *entry; + tree_iterator_frame *frame; + bool is_tree; + + if ((frame = tree_iterator_current_frame(iter)) == NULL) { + error = GIT_ITEROVER; + break; + } + + /* no more entries in this frame. pop the frame out */ + if (frame->next_idx == frame->entries.length) { + if ((error = tree_iterator_frame_pop(iter)) < 0) + break; + + continue; + } + + /* we may have coalesced the contents of case-insensitively same-named + * directories, so do the sort now. + */ + if (frame->next_idx == 0 && !git_vector_is_sorted(&frame->entries)) + git_vector_sort(&frame->entries); + + /* we have more entries in the current frame, that's our next entry */ + prev_entry = tree_iterator_current_entry(frame); + entry = frame->entries.contents[frame->next_idx]; + frame->next_idx++; + + /* we can have collisions when iterating case insensitively. (eg, + * 'A/a' and 'a/A'). squash this one if it's already been seen. + */ + if (iterator__ignore_case(&iter->base) && + prev_entry && + tree_iterator_entry_cmp_icase(prev_entry, entry) == 0) + continue; + + if ((error = tree_iterator_compute_path(&iter->entry_path, entry)) < 0) + break; + + /* if this path is before our start, advance over this entry */ + if (!iterator_has_started(&iter->base, iter->entry_path.ptr, false)) + continue; + + /* if this path is after our end, stop */ + if (iterator_has_ended(&iter->base, iter->entry_path.ptr)) { + error = GIT_ITEROVER; + break; + } + + /* if we have a list of paths we're interested in, examine it */ + if (!iterator_pathlist_next_is(&iter->base, iter->entry_path.ptr)) + continue; + + is_tree = git_tree_entry__is_tree(entry->tree_entry); + + /* if we are *not* including trees then advance over this entry */ + if (is_tree && !iterator__include_trees(iter)) { + + /* if we've found a tree (and are not returning it to the caller) + * and we are autoexpanding, then we want to return the first + * child. push the new directory and advance. + */ + if (iterator__do_autoexpand(iter)) { + if ((error = tree_iterator_frame_push(iter, entry)) < 0) + break; + } + + continue; + } + + tree_iterator_set_current(iter, frame, entry); + + /* if we are autoexpanding, then push this as a new frame, so that + * the next call to `advance` will dive into this directory. + */ + if (is_tree && iterator__do_autoexpand(iter)) + error = tree_iterator_frame_push(iter, entry); + + break; + } + + if (out) + *out = (error == 0) ? &iter->entry : NULL; + + return error; +} + +static int tree_iterator_advance_into( + const git_index_entry **out, git_iterator *i) +{ + tree_iterator *iter = (tree_iterator *)i; + tree_iterator_frame *frame; + tree_iterator_entry *prev_entry; + int error; + + if (out) + *out = NULL; + + if ((frame = tree_iterator_current_frame(iter)) == NULL) + return GIT_ITEROVER; + + /* get the last seen entry */ + prev_entry = tree_iterator_current_entry(frame); + + /* it's legal to call advance_into when auto-expand is on. in this case, + * we will have pushed a new (empty) frame on to the stack for this + * new directory. since it's empty, its current_entry should be null. + */ + GIT_ASSERT(iterator__do_autoexpand(i) ^ (prev_entry != NULL)); + + if (prev_entry) { + if (!git_tree_entry__is_tree(prev_entry->tree_entry)) + return 0; + + if ((error = tree_iterator_frame_push(iter, prev_entry)) < 0) + return error; + } + + /* we've advanced into the directory in question, let advance + * find the first entry + */ + return tree_iterator_advance(out, i); +} + +static int tree_iterator_advance_over( + const git_index_entry **out, + git_iterator_status_t *status, + git_iterator *i) +{ + *status = GIT_ITERATOR_STATUS_NORMAL; + return git_iterator_advance(out, i); +} + +static void tree_iterator_clear(tree_iterator *iter) +{ + while (iter->frames.size) + tree_iterator_frame_pop(iter); + + git_array_clear(iter->frames); + + git_pool_clear(&iter->entry_pool); + git_str_clear(&iter->entry_path); + + iterator_clear(&iter->base); +} + +static int tree_iterator_init(tree_iterator *iter) +{ + int error; + + if ((error = git_pool_init(&iter->entry_pool, sizeof(tree_iterator_entry))) < 0 || + (error = tree_iterator_frame_init(iter, iter->root, NULL)) < 0) + return error; + + iter->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS; + + return 0; +} + +static int tree_iterator_reset(git_iterator *i) +{ + tree_iterator *iter = (tree_iterator *)i; + + tree_iterator_clear(iter); + return tree_iterator_init(iter); +} + +static void tree_iterator_free(git_iterator *i) +{ + tree_iterator *iter = (tree_iterator *)i; + + tree_iterator_clear(iter); + + git_tree_free(iter->root); + git_str_dispose(&iter->entry_path); +} + +int git_iterator_for_tree( + git_iterator **out, + git_tree *tree, + git_iterator_options *options) +{ + tree_iterator *iter; + int error; + + static git_iterator_callbacks callbacks = { + tree_iterator_current, + tree_iterator_advance, + tree_iterator_advance_into, + tree_iterator_advance_over, + tree_iterator_reset, + tree_iterator_free + }; + + *out = NULL; + + if (tree == NULL) + return git_iterator_for_nothing(out, options); + + iter = git__calloc(1, sizeof(tree_iterator)); + GIT_ERROR_CHECK_ALLOC(iter); + + iter->base.type = GIT_ITERATOR_TREE; + iter->base.cb = &callbacks; + + if ((error = iterator_init_common(&iter->base, + git_tree_owner(tree), NULL, options)) < 0 || + (error = git_tree_dup(&iter->root, tree)) < 0 || + (error = tree_iterator_init(iter)) < 0) + goto on_error; + + *out = &iter->base; + return 0; + +on_error: + git_iterator_free(&iter->base); + return error; +} + +int git_iterator_current_tree_entry( + const git_tree_entry **tree_entry, git_iterator *i) +{ + tree_iterator *iter; + tree_iterator_frame *frame; + tree_iterator_entry *entry; + + GIT_ASSERT(i->type == GIT_ITERATOR_TREE); + + iter = (tree_iterator *)i; + + frame = tree_iterator_current_frame(iter); + entry = tree_iterator_current_entry(frame); + + *tree_entry = entry->tree_entry; + return 0; +} + +int git_iterator_current_parent_tree( + const git_tree **parent_tree, git_iterator *i, size_t depth) +{ + tree_iterator *iter; + tree_iterator_frame *frame; + + GIT_ASSERT(i->type == GIT_ITERATOR_TREE); + + iter = (tree_iterator *)i; + + GIT_ASSERT(depth < iter->frames.size); + frame = &iter->frames.ptr[iter->frames.size-depth-1]; + + *parent_tree = frame->tree; + return 0; +} + +/* Filesystem iterator */ + +typedef struct { + struct stat st; + size_t path_len; + iterator_pathlist_search_t match; + git_oid id; + char path[GIT_FLEX_ARRAY]; +} filesystem_iterator_entry; + +typedef struct { + git_vector entries; + git_pool entry_pool; + size_t next_idx; + + size_t path_len; + int is_ignored; +} filesystem_iterator_frame; + +typedef struct { + git_iterator base; + char *root; + size_t root_len; + + unsigned int dirload_flags; + + git_tree *tree; + git_index *index; + git_vector index_snapshot; + + git_oid_t oid_type; + + git_array_t(filesystem_iterator_frame) frames; + git_ignores ignores; + + /* info about the current entry */ + git_index_entry entry; + git_str current_path; + int current_is_ignored; + + /* temporary buffer for advance_over */ + git_str tmp_buf; +} filesystem_iterator; + + +GIT_INLINE(filesystem_iterator_frame *) filesystem_iterator_parent_frame( + filesystem_iterator *iter) +{ + return iter->frames.size > 1 ? + &iter->frames.ptr[iter->frames.size-2] : NULL; +} + +GIT_INLINE(filesystem_iterator_frame *) filesystem_iterator_current_frame( + filesystem_iterator *iter) +{ + return iter->frames.size ? &iter->frames.ptr[iter->frames.size-1] : NULL; +} + +GIT_INLINE(filesystem_iterator_entry *) filesystem_iterator_current_entry( + filesystem_iterator_frame *frame) +{ + return frame->next_idx == 0 ? + NULL : frame->entries.contents[frame->next_idx-1]; +} + +static int filesystem_iterator_entry_cmp(const void *_a, const void *_b) +{ + const filesystem_iterator_entry *a = (const filesystem_iterator_entry *)_a; + const filesystem_iterator_entry *b = (const filesystem_iterator_entry *)_b; + + return git__strcmp(a->path, b->path); +} + +static int filesystem_iterator_entry_cmp_icase(const void *_a, const void *_b) +{ + const filesystem_iterator_entry *a = (const filesystem_iterator_entry *)_a; + const filesystem_iterator_entry *b = (const filesystem_iterator_entry *)_b; + + return git__strcasecmp(a->path, b->path); +} + +#define FILESYSTEM_MAX_DEPTH 100 + +/** + * Figure out if an entry is a submodule. + * + * We consider it a submodule if the path is listed as a submodule in + * either the tree or the index. + */ +static int filesystem_iterator_is_submodule( + bool *out, filesystem_iterator *iter, const char *path, size_t path_len) +{ + bool is_submodule = false; + int error; + + *out = false; + + /* first see if this path is a submodule in HEAD */ + if (iter->tree) { + git_tree_entry *entry; + + error = git_tree_entry_bypath(&entry, iter->tree, path); + + if (error < 0 && error != GIT_ENOTFOUND) + return error; + + if (!error) { + is_submodule = (entry->attr == GIT_FILEMODE_COMMIT); + git_tree_entry_free(entry); + } + } + + if (!is_submodule && iter->base.index) { + size_t pos; + + error = git_index_snapshot_find(&pos, + &iter->index_snapshot, iter->base.entry_srch, path, path_len, 0); + + if (error < 0 && error != GIT_ENOTFOUND) + return error; + + if (!error) { + git_index_entry *e = git_vector_get(&iter->index_snapshot, pos); + is_submodule = (e->mode == GIT_FILEMODE_COMMIT); + } + } + + *out = is_submodule; + return 0; +} + +static void filesystem_iterator_frame_push_ignores( + filesystem_iterator *iter, + filesystem_iterator_entry *frame_entry, + filesystem_iterator_frame *new_frame) +{ + filesystem_iterator_frame *previous_frame; + const char *path = frame_entry ? frame_entry->path : ""; + + if (!iterator__honor_ignores(&iter->base)) + return; + + if (git_ignore__lookup(&new_frame->is_ignored, + &iter->ignores, path, GIT_DIR_FLAG_TRUE) < 0) { + git_error_clear(); + new_frame->is_ignored = GIT_IGNORE_NOTFOUND; + } + + /* if this is not the top level directory... */ + if (frame_entry) { + const char *relative_path; + + previous_frame = filesystem_iterator_parent_frame(iter); + + /* push new ignores for files in this directory */ + relative_path = frame_entry->path + previous_frame->path_len; + + /* inherit ignored from parent if no rule specified */ + if (new_frame->is_ignored <= GIT_IGNORE_NOTFOUND) + new_frame->is_ignored = previous_frame->is_ignored; + + git_ignore__push_dir(&iter->ignores, relative_path); + } +} + +static void filesystem_iterator_frame_pop_ignores( + filesystem_iterator *iter) +{ + if (iterator__honor_ignores(&iter->base)) + git_ignore__pop_dir(&iter->ignores); +} + +GIT_INLINE(bool) filesystem_iterator_examine_path( + bool *is_dir_out, + iterator_pathlist_search_t *match_out, + filesystem_iterator *iter, + filesystem_iterator_entry *frame_entry, + const char *path, + size_t path_len) +{ + bool is_dir = 0; + iterator_pathlist_search_t match = ITERATOR_PATHLIST_FULL; + + *is_dir_out = false; + *match_out = ITERATOR_PATHLIST_NONE; + + if (iter->base.start_len) { + int cmp = iter->base.strncomp(path, iter->base.start, path_len); + + /* we haven't stat'ed `path` yet, so we don't yet know if it's a + * directory or not. special case if the current path may be a + * directory that matches the start prefix. + */ + if (cmp == 0) { + if (iter->base.start[path_len] == '/') + is_dir = true; + + else if (iter->base.start[path_len] != '\0') + cmp = -1; + } + + if (cmp < 0) + return false; + } + + if (iter->base.end_len) { + int cmp = iter->base.strncomp(path, iter->base.end, iter->base.end_len); + + if (cmp > 0) + return false; + } + + /* if we have a pathlist that we're limiting to, examine this path now + * to avoid a `stat` if we're not interested in the path. + */ + if (iter->base.pathlist.length) { + /* if our parent was explicitly included, so too are we */ + if (frame_entry && frame_entry->match != ITERATOR_PATHLIST_IS_PARENT) + match = ITERATOR_PATHLIST_FULL; + else + match = iterator_pathlist_search(&iter->base, path, path_len); + + if (match == ITERATOR_PATHLIST_NONE) + return false; + + /* Ensure that the pathlist entry lines up with what we expected */ + if (match == ITERATOR_PATHLIST_IS_DIR || + match == ITERATOR_PATHLIST_IS_PARENT) + is_dir = true; + } + + *is_dir_out = is_dir; + *match_out = match; + return true; +} + +GIT_INLINE(bool) filesystem_iterator_is_dot_git( + filesystem_iterator *iter, const char *path, size_t path_len) +{ + size_t len; + + if (!iterator__ignore_dot_git(&iter->base)) + return false; + + if ((len = path_len) < 4) + return false; + + if (path[len - 1] == '/') + len--; + + if (git__tolower(path[len - 1]) != 't' || + git__tolower(path[len - 2]) != 'i' || + git__tolower(path[len - 3]) != 'g' || + git__tolower(path[len - 4]) != '.') + return false; + + return (len == 4 || path[len - 5] == '/'); +} + +static int filesystem_iterator_entry_hash( + filesystem_iterator *iter, + filesystem_iterator_entry *entry) +{ + git_str fullpath = GIT_STR_INIT; + int error; + + if (S_ISDIR(entry->st.st_mode)) { + memset(&entry->id, 0, git_oid_size(iter->oid_type)); + return 0; + } + + if (iter->base.type == GIT_ITERATOR_WORKDIR) + return git_repository_hashfile(&entry->id, + iter->base.repo, entry->path, GIT_OBJECT_BLOB, NULL); + + if (!(error = git_str_joinpath(&fullpath, iter->root, entry->path)) && + !(error = git_path_validate_str_length(iter->base.repo, &fullpath))) + error = git_odb__hashfile(&entry->id, fullpath.ptr, GIT_OBJECT_BLOB, iter->oid_type); + + git_str_dispose(&fullpath); + return error; +} + +static int filesystem_iterator_entry_init( + filesystem_iterator_entry **out, + filesystem_iterator *iter, + filesystem_iterator_frame *frame, + const char *path, + size_t path_len, + struct stat *statbuf, + iterator_pathlist_search_t pathlist_match) +{ + filesystem_iterator_entry *entry; + size_t entry_size; + int error = 0; + + *out = NULL; + + /* Make sure to append two bytes, one for the path's null + * termination, one for a possible trailing '/' for folders. + */ + GIT_ERROR_CHECK_ALLOC_ADD(&entry_size, + sizeof(filesystem_iterator_entry), path_len); + GIT_ERROR_CHECK_ALLOC_ADD(&entry_size, entry_size, 2); + + entry = git_pool_malloc(&frame->entry_pool, entry_size); + GIT_ERROR_CHECK_ALLOC(entry); + + entry->path_len = path_len; + entry->match = pathlist_match; + memcpy(entry->path, path, path_len); + memcpy(&entry->st, statbuf, sizeof(struct stat)); + + /* Suffix directory paths with a '/' */ + if (S_ISDIR(entry->st.st_mode)) + entry->path[entry->path_len++] = '/'; + + entry->path[entry->path_len] = '\0'; + + if (iter->base.flags & GIT_ITERATOR_INCLUDE_HASH) + error = filesystem_iterator_entry_hash(iter, entry); + + if (!error) + *out = entry; + + return error; +} + +static int filesystem_iterator_frame_push( + filesystem_iterator *iter, + filesystem_iterator_entry *frame_entry) +{ + filesystem_iterator_frame *new_frame = NULL; + git_fs_path_diriter diriter = GIT_FS_PATH_DIRITER_INIT; + git_str root = GIT_STR_INIT; + const char *path; + filesystem_iterator_entry *entry; + struct stat statbuf; + size_t path_len; + int error; + + if (iter->frames.size == FILESYSTEM_MAX_DEPTH) { + git_error_set(GIT_ERROR_REPOSITORY, + "directory nesting too deep (%"PRIuZ")", iter->frames.size); + return -1; + } + + new_frame = git_array_alloc(iter->frames); + GIT_ERROR_CHECK_ALLOC(new_frame); + + memset(new_frame, 0, sizeof(filesystem_iterator_frame)); + + if (frame_entry) + git_str_joinpath(&root, iter->root, frame_entry->path); + else + git_str_puts(&root, iter->root); + + if (git_str_oom(&root) || + git_path_validate_str_length(iter->base.repo, &root) < 0) { + error = -1; + goto done; + } + + new_frame->path_len = frame_entry ? frame_entry->path_len : 0; + + /* Any error here is equivalent to the dir not existing, skip over it */ + if ((error = git_fs_path_diriter_init( + &diriter, root.ptr, iter->dirload_flags)) < 0) { + error = GIT_ENOTFOUND; + goto done; + } + + if ((error = git_vector_init(&new_frame->entries, 64, + iterator__ignore_case(&iter->base) ? + filesystem_iterator_entry_cmp_icase : + filesystem_iterator_entry_cmp)) < 0) + goto done; + + if ((error = git_pool_init(&new_frame->entry_pool, 1)) < 0) + goto done; + + /* check if this directory is ignored */ + filesystem_iterator_frame_push_ignores(iter, frame_entry, new_frame); + + while ((error = git_fs_path_diriter_next(&diriter)) == 0) { + iterator_pathlist_search_t pathlist_match = ITERATOR_PATHLIST_FULL; + git_str path_str = GIT_STR_INIT; + bool dir_expected = false; + + if ((error = git_fs_path_diriter_fullpath(&path, &path_len, &diriter)) < 0) + goto done; + + path_str.ptr = (char *)path; + path_str.size = path_len; + + if ((error = git_path_validate_str_length(iter->base.repo, &path_str)) < 0) + goto done; + + GIT_ASSERT(path_len > iter->root_len); + + /* remove the prefix if requested */ + path += iter->root_len; + path_len -= iter->root_len; + + /* examine start / end and the pathlist to see if this path is in it. + * note that since we haven't yet stat'ed the path, we cannot know + * whether it's a directory yet or not, so this can give us an + * expected type (S_IFDIR or S_IFREG) that we should examine) + */ + if (!filesystem_iterator_examine_path(&dir_expected, &pathlist_match, + iter, frame_entry, path, path_len)) + continue; + + /* TODO: don't need to stat if assume unchanged for this path and + * we have an index, we can just copy the data out of it. + */ + + if ((error = git_fs_path_diriter_stat(&statbuf, &diriter)) < 0) { + /* file was removed between readdir and lstat */ + if (error == GIT_ENOTFOUND) + continue; + + /* treat the file as unreadable */ + memset(&statbuf, 0, sizeof(statbuf)); + statbuf.st_mode = GIT_FILEMODE_UNREADABLE; + + error = 0; + } + + iter->base.stat_calls++; + + /* Ignore wacky things in the filesystem */ + if (!S_ISDIR(statbuf.st_mode) && + !S_ISREG(statbuf.st_mode) && + !S_ISLNK(statbuf.st_mode) && + statbuf.st_mode != GIT_FILEMODE_UNREADABLE) + continue; + + if (filesystem_iterator_is_dot_git(iter, path, path_len)) + continue; + + /* convert submodules to GITLINK and remove trailing slashes */ + if (S_ISDIR(statbuf.st_mode)) { + bool submodule = false; + + if ((error = filesystem_iterator_is_submodule(&submodule, + iter, path, path_len)) < 0) + goto done; + + if (submodule) + statbuf.st_mode = GIT_FILEMODE_COMMIT; + } + + /* Ensure that the pathlist entry lines up with what we expected */ + else if (dir_expected) + continue; + + if ((error = filesystem_iterator_entry_init(&entry, + iter, new_frame, path, path_len, &statbuf, pathlist_match)) < 0) + goto done; + + git_vector_insert(&new_frame->entries, entry); + } + + if (error == GIT_ITEROVER) + error = 0; + + /* sort now that directory suffix is added */ + git_vector_sort(&new_frame->entries); + +done: + if (error < 0) + git_array_pop(iter->frames); + + git_str_dispose(&root); + git_fs_path_diriter_free(&diriter); + return error; +} + +GIT_INLINE(int) filesystem_iterator_frame_pop(filesystem_iterator *iter) +{ + filesystem_iterator_frame *frame; + + GIT_ASSERT(iter->frames.size); + + frame = git_array_pop(iter->frames); + filesystem_iterator_frame_pop_ignores(iter); + + git_pool_clear(&frame->entry_pool); + git_vector_dispose(&frame->entries); + + return 0; +} + +static void filesystem_iterator_set_current( + filesystem_iterator *iter, + filesystem_iterator_entry *entry) +{ + /* + * Index entries are limited to 32 bit timestamps. We can safely + * cast this since workdir times are only used in the cache; any + * mismatch will cause a hash recomputation which is unfortunate + * but affects only people who set their filetimes to 2038. + * (Same with the file size.) + */ + iter->entry.ctime.seconds = (int32_t)entry->st.st_ctime; + iter->entry.mtime.seconds = (int32_t)entry->st.st_mtime; + +#if defined(GIT_NSEC) + iter->entry.ctime.nanoseconds = entry->st.st_ctime_nsec; + iter->entry.mtime.nanoseconds = entry->st.st_mtime_nsec; +#else + iter->entry.ctime.nanoseconds = 0; + iter->entry.mtime.nanoseconds = 0; +#endif + + iter->entry.dev = entry->st.st_dev; + iter->entry.ino = entry->st.st_ino; + iter->entry.mode = git_futils_canonical_mode(entry->st.st_mode); + iter->entry.uid = entry->st.st_uid; + iter->entry.gid = entry->st.st_gid; + iter->entry.file_size = (uint32_t)entry->st.st_size; + + if (iter->base.flags & GIT_ITERATOR_INCLUDE_HASH) + git_oid_cpy(&iter->entry.id, &entry->id); + else + git_oid_clear(&iter->entry.id, iter->oid_type); + + iter->entry.path = entry->path; + + iter->current_is_ignored = GIT_IGNORE_UNCHECKED; +} + +static int filesystem_iterator_current( + const git_index_entry **out, git_iterator *i) +{ + filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base); + + if (!iterator__has_been_accessed(i)) + return iter->base.cb->advance(out, i); + + if (!iter->frames.size) { + *out = NULL; + return GIT_ITEROVER; + } + + *out = &iter->entry; + return 0; +} + +static int filesystem_iterator_is_dir( + bool *is_dir, + const filesystem_iterator *iter, + const filesystem_iterator_entry *entry) +{ + struct stat st; + git_str fullpath = GIT_STR_INIT; + int error = 0; + + if (S_ISDIR(entry->st.st_mode)) { + *is_dir = 1; + goto done; + } + + if (!iterator__descend_symlinks(iter) || !S_ISLNK(entry->st.st_mode)) { + *is_dir = 0; + goto done; + } + + if ((error = git_str_joinpath(&fullpath, iter->root, entry->path)) < 0 || + (error = git_path_validate_str_length(iter->base.repo, &fullpath)) < 0 || + (error = p_stat(fullpath.ptr, &st)) < 0) + goto done; + + *is_dir = S_ISDIR(st.st_mode); + +done: + git_str_dispose(&fullpath); + return error; +} + +static int filesystem_iterator_advance( + const git_index_entry **out, git_iterator *i) +{ + filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base); + bool is_dir; + int error = 0; + + iter->base.flags |= GIT_ITERATOR_FIRST_ACCESS; + + /* examine filesystem entries until we find the next one to return */ + while (true) { + filesystem_iterator_frame *frame; + filesystem_iterator_entry *entry; + + if ((frame = filesystem_iterator_current_frame(iter)) == NULL) { + error = GIT_ITEROVER; + break; + } + + /* no more entries in this frame. pop the frame out */ + if (frame->next_idx == frame->entries.length) { + filesystem_iterator_frame_pop(iter); + continue; + } + + /* we have more entries in the current frame, that's our next entry */ + entry = frame->entries.contents[frame->next_idx]; + frame->next_idx++; + + if ((error = filesystem_iterator_is_dir(&is_dir, iter, entry)) < 0) + break; + + if (is_dir) { + if (iterator__do_autoexpand(iter)) { + error = filesystem_iterator_frame_push(iter, entry); + + /* may get GIT_ENOTFOUND due to races or permission problems + * that we want to quietly swallow + */ + if (error == GIT_ENOTFOUND) + continue; + else if (error < 0) + break; + } + + if (!iterator__include_trees(iter)) + continue; + } + + filesystem_iterator_set_current(iter, entry); + break; + } + + if (out) + *out = (error == 0) ? &iter->entry : NULL; + + return error; +} + +static int filesystem_iterator_advance_into( + const git_index_entry **out, git_iterator *i) +{ + filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base); + filesystem_iterator_frame *frame; + filesystem_iterator_entry *prev_entry; + int error; + + if (out) + *out = NULL; + + if ((frame = filesystem_iterator_current_frame(iter)) == NULL) + return GIT_ITEROVER; + + /* get the last seen entry */ + prev_entry = filesystem_iterator_current_entry(frame); + + /* it's legal to call advance_into when auto-expand is on. in this case, + * we will have pushed a new (empty) frame on to the stack for this + * new directory. since it's empty, its current_entry should be null. + */ + GIT_ASSERT(iterator__do_autoexpand(i) ^ (prev_entry != NULL)); + + if (prev_entry) { + if (prev_entry->st.st_mode != GIT_FILEMODE_COMMIT && + !S_ISDIR(prev_entry->st.st_mode)) + return 0; + + if ((error = filesystem_iterator_frame_push(iter, prev_entry)) < 0) + return error; + } + + /* we've advanced into the directory in question, let advance + * find the first entry + */ + return filesystem_iterator_advance(out, i); +} + +int git_iterator_current_workdir_path(git_str **out, git_iterator *i) +{ + filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base); + const git_index_entry *entry; + + if (i->type != GIT_ITERATOR_FS && + i->type != GIT_ITERATOR_WORKDIR) { + *out = NULL; + return 0; + } + + git_str_truncate(&iter->current_path, iter->root_len); + + if (git_iterator_current(&entry, i) < 0 || + git_str_puts(&iter->current_path, entry->path) < 0) + return -1; + + *out = &iter->current_path; + return 0; +} + +GIT_INLINE(git_dir_flag) entry_dir_flag(git_index_entry *entry) +{ +#if defined(GIT_WIN32) && !defined(__MINGW32__) + return (entry && entry->mode) ? + (S_ISDIR(entry->mode) ? GIT_DIR_FLAG_TRUE : GIT_DIR_FLAG_FALSE) : + GIT_DIR_FLAG_UNKNOWN; +#else + GIT_UNUSED(entry); + return GIT_DIR_FLAG_UNKNOWN; +#endif +} + +static void filesystem_iterator_update_ignored(filesystem_iterator *iter) +{ + filesystem_iterator_frame *frame; + git_dir_flag dir_flag = entry_dir_flag(&iter->entry); + + if (git_ignore__lookup(&iter->current_is_ignored, + &iter->ignores, iter->entry.path, dir_flag) < 0) { + git_error_clear(); + iter->current_is_ignored = GIT_IGNORE_NOTFOUND; + } + + /* use ignore from containing frame stack */ + if (iter->current_is_ignored <= GIT_IGNORE_NOTFOUND) { + frame = filesystem_iterator_current_frame(iter); + iter->current_is_ignored = frame->is_ignored; + } +} + +GIT_INLINE(bool) filesystem_iterator_current_is_ignored( + filesystem_iterator *iter) +{ + if (iter->current_is_ignored == GIT_IGNORE_UNCHECKED) + filesystem_iterator_update_ignored(iter); + + return (iter->current_is_ignored == GIT_IGNORE_TRUE); +} + +bool git_iterator_current_is_ignored(git_iterator *i) +{ + filesystem_iterator *iter = NULL; + + if (i->type != GIT_ITERATOR_WORKDIR) + return false; + + iter = GIT_CONTAINER_OF(i, filesystem_iterator, base); + + return filesystem_iterator_current_is_ignored(iter); +} + +bool git_iterator_current_tree_is_ignored(git_iterator *i) +{ + filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base); + filesystem_iterator_frame *frame; + + if (i->type != GIT_ITERATOR_WORKDIR) + return false; + + frame = filesystem_iterator_current_frame(iter); + return (frame->is_ignored == GIT_IGNORE_TRUE); +} + +static int filesystem_iterator_advance_over( + const git_index_entry **out, + git_iterator_status_t *status, + git_iterator *i) +{ + filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base); + filesystem_iterator_frame *current_frame; + filesystem_iterator_entry *current_entry; + const git_index_entry *entry = NULL; + const char *base; + int error = 0; + + *out = NULL; + *status = GIT_ITERATOR_STATUS_NORMAL; + + GIT_ASSERT(iterator__has_been_accessed(i)); + + current_frame = filesystem_iterator_current_frame(iter); + GIT_ASSERT(current_frame); + + current_entry = filesystem_iterator_current_entry(current_frame); + GIT_ASSERT(current_entry); + + if ((error = git_iterator_current(&entry, i)) < 0) + return error; + + if (!S_ISDIR(entry->mode)) { + if (filesystem_iterator_current_is_ignored(iter)) + *status = GIT_ITERATOR_STATUS_IGNORED; + + return filesystem_iterator_advance(out, i); + } + + git_str_clear(&iter->tmp_buf); + if ((error = git_str_puts(&iter->tmp_buf, entry->path)) < 0) + return error; + + base = iter->tmp_buf.ptr; + + /* scan inside the directory looking for files. if we find nothing, + * we will remain EMPTY. if we find any ignored item, upgrade EMPTY to + * IGNORED. if we find a real actual item, upgrade all the way to NORMAL + * and then stop. + * + * however, if we're here looking for a pathlist item (but are not + * actually in the pathlist ourselves) then start at FILTERED instead of + * EMPTY. callers then know that this path was not something they asked + * about. + */ + *status = current_entry->match == ITERATOR_PATHLIST_IS_PARENT ? + GIT_ITERATOR_STATUS_FILTERED : GIT_ITERATOR_STATUS_EMPTY; + + while (entry && !iter->base.prefixcomp(entry->path, base)) { + if (filesystem_iterator_current_is_ignored(iter)) { + /* if we found an explicitly ignored item, then update from + * EMPTY to IGNORED + */ + *status = GIT_ITERATOR_STATUS_IGNORED; + } else if (S_ISDIR(entry->mode)) { + error = filesystem_iterator_advance_into(&entry, i); + + if (!error) + continue; + + /* this directory disappeared, ignore it */ + else if (error == GIT_ENOTFOUND) + error = 0; + + /* a real error occurred */ + else + break; + } else { + /* we found a non-ignored item, treat parent as untracked */ + *status = GIT_ITERATOR_STATUS_NORMAL; + break; + } + + if ((error = git_iterator_advance(&entry, i)) < 0) + break; + } + + /* wrap up scan back to base directory */ + while (entry && !iter->base.prefixcomp(entry->path, base)) { + if ((error = git_iterator_advance(&entry, i)) < 0) + break; + } + + if (!error) + *out = entry; + + return error; +} + +static void filesystem_iterator_clear(filesystem_iterator *iter) +{ + while (iter->frames.size) + filesystem_iterator_frame_pop(iter); + + git_array_clear(iter->frames); + git_ignore__free(&iter->ignores); + + git_str_dispose(&iter->tmp_buf); + + iterator_clear(&iter->base); +} + +static int filesystem_iterator_init(filesystem_iterator *iter) +{ + int error; + + if (iterator__honor_ignores(&iter->base) && + (error = git_ignore__for_path(iter->base.repo, + ".gitignore", &iter->ignores)) < 0) + return error; + + if ((error = filesystem_iterator_frame_push(iter, NULL)) < 0) + return error; + + iter->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS; + + return 0; +} + +static int filesystem_iterator_reset(git_iterator *i) +{ + filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base); + + filesystem_iterator_clear(iter); + return filesystem_iterator_init(iter); +} + +static void filesystem_iterator_free(git_iterator *i) +{ + filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base); + git__free(iter->root); + git_str_dispose(&iter->current_path); + git_tree_free(iter->tree); + if (iter->index) + git_index_snapshot_release(&iter->index_snapshot, iter->index); + filesystem_iterator_clear(iter); +} + +static int iterator_for_filesystem( + git_iterator **out, + git_repository *repo, + const char *root, + git_index *index, + git_tree *tree, + git_iterator_t type, + git_iterator_options *options) +{ + filesystem_iterator *iter; + size_t root_len; + int error; + + static git_iterator_callbacks callbacks = { + filesystem_iterator_current, + filesystem_iterator_advance, + filesystem_iterator_advance_into, + filesystem_iterator_advance_over, + filesystem_iterator_reset, + filesystem_iterator_free + }; + + *out = NULL; + + if (root == NULL) + return git_iterator_for_nothing(out, options); + + iter = git__calloc(1, sizeof(filesystem_iterator)); + GIT_ERROR_CHECK_ALLOC(iter); + + iter->base.type = type; + iter->base.cb = &callbacks; + + root_len = strlen(root); + + iter->root = git__malloc(root_len+2); + GIT_ERROR_CHECK_ALLOC(iter->root); + + memcpy(iter->root, root, root_len); + + if (root_len == 0 || root[root_len-1] != '/') { + iter->root[root_len] = '/'; + root_len++; + } + iter->root[root_len] = '\0'; + iter->root_len = root_len; + + if ((error = git_str_puts(&iter->current_path, iter->root)) < 0) + goto on_error; + + if ((error = iterator_init_common(&iter->base, repo, index, options)) < 0) + goto on_error; + + if (tree && (error = git_tree_dup(&iter->tree, tree)) < 0) + goto on_error; + + if (index && + (error = git_index_snapshot_new(&iter->index_snapshot, index)) < 0) + goto on_error; + + iter->index = index; + iter->dirload_flags = + (iterator__ignore_case(&iter->base) ? + GIT_FS_PATH_DIR_IGNORE_CASE : 0) | + (iterator__flag(&iter->base, PRECOMPOSE_UNICODE) ? + GIT_FS_PATH_DIR_PRECOMPOSE_UNICODE : 0); + + iter->oid_type = options->oid_type; + + if ((error = filesystem_iterator_init(iter)) < 0) + goto on_error; + + *out = &iter->base; + return 0; + +on_error: + git_iterator_free(&iter->base); + return error; +} + +int git_iterator_for_filesystem( + git_iterator **out, + const char *root, + git_iterator_options *given_opts) +{ + git_iterator_options options = GIT_ITERATOR_OPTIONS_INIT; + + if (given_opts) + memcpy(&options, given_opts, sizeof(git_iterator_options)); + + return iterator_for_filesystem(out, + NULL, root, NULL, NULL, GIT_ITERATOR_FS, &options); +} + +int git_iterator_for_workdir_ext( + git_iterator **out, + git_repository *repo, + const char *repo_workdir, + git_index *index, + git_tree *tree, + git_iterator_options *given_opts) +{ + git_iterator_options options = GIT_ITERATOR_OPTIONS_INIT; + + if (!repo_workdir) { + if (git_repository__ensure_not_bare(repo, "scan working directory") < 0) + return GIT_EBAREREPO; + + repo_workdir = git_repository_workdir(repo); + } + + /* upgrade to a workdir iterator, adding necessary internal flags */ + if (given_opts) + memcpy(&options, given_opts, sizeof(git_iterator_options)); + + options.flags |= GIT_ITERATOR_HONOR_IGNORES | + GIT_ITERATOR_IGNORE_DOT_GIT; + + if (!options.oid_type) + options.oid_type = repo->oid_type; + else if (options.oid_type != repo->oid_type) + git_error_set(GIT_ERROR_INVALID, + "specified object ID type does not match repository object ID type"); + + return iterator_for_filesystem(out, + repo, repo_workdir, index, tree, GIT_ITERATOR_WORKDIR, &options); +} + + +/* Index iterator */ + + +typedef struct { + git_iterator base; + git_vector entries; + size_t next_idx; + + /* the pseudotree entry */ + git_index_entry tree_entry; + git_str tree_buf; + bool skip_tree; + + const git_index_entry *entry; +} index_iterator; + +static int index_iterator_current( + const git_index_entry **out, git_iterator *i) +{ + index_iterator *iter = (index_iterator *)i; + + if (!iterator__has_been_accessed(i)) + return iter->base.cb->advance(out, i); + + if (iter->entry == NULL) { + *out = NULL; + return GIT_ITEROVER; + } + + *out = iter->entry; + return 0; +} + +static bool index_iterator_create_pseudotree( + const git_index_entry **out, + index_iterator *iter, + const char *path) +{ + const char *prev_path, *relative_path, *dirsep; + size_t common_len; + + prev_path = iter->entry ? iter->entry->path : ""; + + /* determine if the new path is in a different directory from the old */ + common_len = git_fs_path_common_dirlen(prev_path, path); + relative_path = path + common_len; + + if ((dirsep = strchr(relative_path, '/')) == NULL) + return false; + + git_str_clear(&iter->tree_buf); + git_str_put(&iter->tree_buf, path, (dirsep - path) + 1); + + iter->tree_entry.mode = GIT_FILEMODE_TREE; + iter->tree_entry.path = iter->tree_buf.ptr; + + *out = &iter->tree_entry; + return true; +} + +static int index_iterator_skip_pseudotree(index_iterator *iter) +{ + GIT_ASSERT(iterator__has_been_accessed(&iter->base)); + GIT_ASSERT(S_ISDIR(iter->entry->mode)); + + while (true) { + const git_index_entry *next_entry = NULL; + + if (++iter->next_idx >= iter->entries.length) + return GIT_ITEROVER; + + next_entry = iter->entries.contents[iter->next_idx]; + + if (iter->base.strncomp(iter->tree_buf.ptr, next_entry->path, + iter->tree_buf.size) != 0) + break; + } + + iter->skip_tree = false; + return 0; +} + +static int index_iterator_advance( + const git_index_entry **out, git_iterator *i) +{ + index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base); + const git_index_entry *entry = NULL; + bool is_submodule; + int error = 0; + + iter->base.flags |= GIT_ITERATOR_FIRST_ACCESS; + + while (true) { + if (iter->next_idx >= iter->entries.length) { + error = GIT_ITEROVER; + break; + } + + /* we were not asked to expand this pseudotree. advance over it. */ + if (iter->skip_tree) { + index_iterator_skip_pseudotree(iter); + continue; + } + + entry = iter->entries.contents[iter->next_idx]; + is_submodule = S_ISGITLINK(entry->mode); + + if (!iterator_has_started(&iter->base, entry->path, is_submodule)) { + iter->next_idx++; + continue; + } + + if (iterator_has_ended(&iter->base, entry->path)) { + error = GIT_ITEROVER; + break; + } + + /* if we have a list of paths we're interested in, examine it */ + if (!iterator_pathlist_next_is(&iter->base, entry->path)) { + iter->next_idx++; + continue; + } + + /* if this is a conflict, skip it unless we're including conflicts */ + if (git_index_entry_is_conflict(entry) && + !iterator__include_conflicts(&iter->base)) { + iter->next_idx++; + continue; + } + + /* we've found what will be our next _file_ entry. but if we are + * returning trees entries, we may need to return a pseudotree + * entry that will contain this. don't advance over this entry, + * though, we still need to return it on the next `advance`. + */ + if (iterator__include_trees(&iter->base) && + index_iterator_create_pseudotree(&entry, iter, entry->path)) { + + /* Note whether this pseudo tree should be expanded or not */ + iter->skip_tree = iterator__dont_autoexpand(&iter->base); + break; + } + + iter->next_idx++; + break; + } + + iter->entry = (error == 0) ? entry : NULL; + + if (out) + *out = iter->entry; + + return error; +} + +static int index_iterator_advance_into( + const git_index_entry **out, git_iterator *i) +{ + index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base); + + if (! S_ISDIR(iter->tree_entry.mode)) { + if (out) + *out = NULL; + + return 0; + } + + iter->skip_tree = false; + return index_iterator_advance(out, i); +} + +static int index_iterator_advance_over( + const git_index_entry **out, + git_iterator_status_t *status, + git_iterator *i) +{ + index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base); + const git_index_entry *entry; + int error; + + if ((error = index_iterator_current(&entry, i)) < 0) + return error; + + if (S_ISDIR(entry->mode)) + index_iterator_skip_pseudotree(iter); + + *status = GIT_ITERATOR_STATUS_NORMAL; + return index_iterator_advance(out, i); +} + +static void index_iterator_clear(index_iterator *iter) +{ + iterator_clear(&iter->base); +} + +static int index_iterator_init(index_iterator *iter) +{ + iter->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS; + iter->next_idx = 0; + iter->skip_tree = false; + return 0; +} + +static int index_iterator_reset(git_iterator *i) +{ + index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base); + + index_iterator_clear(iter); + return index_iterator_init(iter); +} + +static void index_iterator_free(git_iterator *i) +{ + index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base); + + git_index_snapshot_release(&iter->entries, iter->base.index); + git_str_dispose(&iter->tree_buf); +} + +int git_iterator_for_index( + git_iterator **out, + git_repository *repo, + git_index *index, + git_iterator_options *options) +{ + index_iterator *iter; + int error; + + static git_iterator_callbacks callbacks = { + index_iterator_current, + index_iterator_advance, + index_iterator_advance_into, + index_iterator_advance_over, + index_iterator_reset, + index_iterator_free + }; + + *out = NULL; + + if (index == NULL) + return git_iterator_for_nothing(out, options); + + iter = git__calloc(1, sizeof(index_iterator)); + GIT_ERROR_CHECK_ALLOC(iter); + + iter->base.type = GIT_ITERATOR_INDEX; + iter->base.cb = &callbacks; + + if ((error = iterator_init_common(&iter->base, repo, index, options)) < 0 || + (error = git_index_snapshot_new(&iter->entries, index)) < 0 || + (error = index_iterator_init(iter)) < 0) + goto on_error; + + git_vector_set_cmp(&iter->entries, iterator__ignore_case(&iter->base) ? + git_index_entry_icmp : git_index_entry_cmp); + git_vector_sort(&iter->entries); + + *out = &iter->base; + return 0; + +on_error: + git_iterator_free(&iter->base); + return error; +} + + +/* Iterator API */ + +int git_iterator_reset_range( + git_iterator *i, const char *start, const char *end) +{ + if (iterator_reset_range(i, start, end) < 0) + return -1; + + return i->cb->reset(i); +} + +int git_iterator_set_ignore_case(git_iterator *i, bool ignore_case) +{ + GIT_ASSERT(!iterator__has_been_accessed(i)); + iterator_set_ignore_case(i, ignore_case); + return 0; +} + +void git_iterator_free(git_iterator *iter) +{ + if (iter == NULL) + return; + + iter->cb->free(iter); + + git_vector_dispose(&iter->pathlist); + git__free(iter->start); + git__free(iter->end); + + memset(iter, 0, sizeof(*iter)); + + git__free(iter); +} + +int git_iterator_foreach( + git_iterator *iterator, + git_iterator_foreach_cb cb, + void *data) +{ + const git_index_entry *iterator_item; + int error = 0; + + if ((error = git_iterator_current(&iterator_item, iterator)) < 0) + goto done; + + if ((error = cb(iterator_item, data)) != 0) + goto done; + + while (true) { + if ((error = git_iterator_advance(&iterator_item, iterator)) < 0) + goto done; + + if ((error = cb(iterator_item, data)) != 0) + goto done; + } + +done: + if (error == GIT_ITEROVER) + error = 0; + + return error; +} + +int git_iterator_walk( + git_iterator **iterators, + size_t cnt, + git_iterator_walk_cb cb, + void *data) +{ + const git_index_entry **iterator_item; /* next in each iterator */ + const git_index_entry **cur_items; /* current path in each iter */ + const git_index_entry *first_match; + size_t i, j; + int error = 0; + + iterator_item = git__calloc(cnt, sizeof(git_index_entry *)); + cur_items = git__calloc(cnt, sizeof(git_index_entry *)); + + GIT_ERROR_CHECK_ALLOC(iterator_item); + GIT_ERROR_CHECK_ALLOC(cur_items); + + /* Set up the iterators */ + for (i = 0; i < cnt; i++) { + error = git_iterator_current(&iterator_item[i], iterators[i]); + + if (error < 0 && error != GIT_ITEROVER) + goto done; + } + + while (true) { + for (i = 0; i < cnt; i++) + cur_items[i] = NULL; + + first_match = NULL; + + /* Find the next path(s) to consume from each iterator */ + for (i = 0; i < cnt; i++) { + if (iterator_item[i] == NULL) + continue; + + if (first_match == NULL) { + first_match = iterator_item[i]; + cur_items[i] = iterator_item[i]; + } else { + int path_diff = git_index_entry_cmp(iterator_item[i], first_match); + + if (path_diff < 0) { + /* Found an index entry that sorts before the one we're + * looking at. Forget that we've seen the other and + * look at the other iterators for this path. + */ + for (j = 0; j < i; j++) + cur_items[j] = NULL; + + first_match = iterator_item[i]; + cur_items[i] = iterator_item[i]; + } else if (path_diff == 0) { + cur_items[i] = iterator_item[i]; + } + } + } + + if (first_match == NULL) + break; + + if ((error = cb(cur_items, data)) != 0) + goto done; + + /* Advance each iterator that participated */ + for (i = 0; i < cnt; i++) { + if (cur_items[i] == NULL) + continue; + + error = git_iterator_advance(&iterator_item[i], iterators[i]); + + if (error < 0 && error != GIT_ITEROVER) + goto done; + } + } + +done: + git__free((git_index_entry **)iterator_item); + git__free((git_index_entry **)cur_items); + + if (error == GIT_ITEROVER) + error = 0; + + return error; +} diff --git a/src/libgit2/iterator.h b/src/libgit2/iterator.h new file mode 100644 index 00000000000..7963ce7e22c --- /dev/null +++ b/src/libgit2/iterator.h @@ -0,0 +1,325 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_iterator_h__ +#define INCLUDE_iterator_h__ + +#include "common.h" + +#include "git2/index.h" +#include "vector.h" +#include "str.h" +#include "ignore.h" + +typedef struct git_iterator git_iterator; + +typedef enum { + GIT_ITERATOR_EMPTY = 0, + GIT_ITERATOR_TREE = 1, + GIT_ITERATOR_INDEX = 2, + GIT_ITERATOR_WORKDIR = 3, + GIT_ITERATOR_FS = 4 +} git_iterator_t; + +typedef enum { + /** ignore case for entry sort order */ + GIT_ITERATOR_IGNORE_CASE = (1u << 0), + /** force case sensitivity for entry sort order */ + GIT_ITERATOR_DONT_IGNORE_CASE = (1u << 1), + /** return tree items in addition to blob items */ + GIT_ITERATOR_INCLUDE_TREES = (1u << 2), + /** don't flatten trees, requiring advance_into (implies INCLUDE_TREES) */ + GIT_ITERATOR_DONT_AUTOEXPAND = (1u << 3), + /** convert precomposed unicode to decomposed unicode */ + GIT_ITERATOR_PRECOMPOSE_UNICODE = (1u << 4), + /** never convert precomposed unicode to decomposed unicode */ + GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE = (1u << 5), + /** include conflicts */ + GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 6), + /** descend into symlinked directories */ + GIT_ITERATOR_DESCEND_SYMLINKS = (1u << 7), + /** hash files in workdir or filesystem iterators */ + GIT_ITERATOR_INCLUDE_HASH = (1u << 8) +} git_iterator_flag_t; + +typedef enum { + GIT_ITERATOR_STATUS_NORMAL = 0, + GIT_ITERATOR_STATUS_IGNORED = 1, + GIT_ITERATOR_STATUS_EMPTY = 2, + GIT_ITERATOR_STATUS_FILTERED = 3 +} git_iterator_status_t; + +typedef struct { + const char *start; + const char *end; + + /* paths to include in the iterator (literal). if set, any paths not + * listed here will be excluded from iteration. + */ + git_strarray pathlist; + + /* flags, from above */ + unsigned int flags; + + /* oid type - necessary for non-workdir filesystem iterators */ + git_oid_t oid_type; +} git_iterator_options; + +#define GIT_ITERATOR_OPTIONS_INIT {0} + +typedef struct { + int (*current)(const git_index_entry **, git_iterator *); + int (*advance)(const git_index_entry **, git_iterator *); + int (*advance_into)(const git_index_entry **, git_iterator *); + int (*advance_over)( + const git_index_entry **, git_iterator_status_t *, git_iterator *); + int (*reset)(git_iterator *); + void (*free)(git_iterator *); +} git_iterator_callbacks; + +struct git_iterator { + git_iterator_t type; + git_iterator_callbacks *cb; + + git_repository *repo; + git_index *index; + + char *start; + size_t start_len; + + char *end; + size_t end_len; + + bool started; + bool ended; + git_vector pathlist; + size_t pathlist_walk_idx; + int (*strcomp)(const char *a, const char *b); + int (*strncomp)(const char *a, const char *b, size_t n); + int (*prefixcomp)(const char *str, const char *prefix); + int (*entry_srch)(const void *key, const void *array_member); + size_t stat_calls; + unsigned int flags; +}; + +extern int git_iterator_for_nothing( + git_iterator **out, + git_iterator_options *options); + +/* tree iterators will match the ignore_case value from the index of the + * repository, unless you override with a non-zero flag value + */ +extern int git_iterator_for_tree( + git_iterator **out, + git_tree *tree, + git_iterator_options *options); + +/* index iterators will take the ignore_case value from the index; the + * ignore_case flags are not used + */ +extern int git_iterator_for_index( + git_iterator **out, + git_repository *repo, + git_index *index, + git_iterator_options *options); + +extern int git_iterator_for_workdir_ext( + git_iterator **out, + git_repository *repo, + const char *repo_workdir, + git_index *index, + git_tree *tree, + git_iterator_options *options); + +/* workdir iterators will match the ignore_case value from the index of the + * repository, unless you override with a non-zero flag value + */ +GIT_INLINE(int) git_iterator_for_workdir( + git_iterator **out, + git_repository *repo, + git_index *index, + git_tree *tree, + git_iterator_options *options) +{ + return git_iterator_for_workdir_ext(out, repo, NULL, index, tree, options); +} + +/* for filesystem iterators, you have to explicitly pass in the ignore_case + * behavior that you desire + */ +extern int git_iterator_for_filesystem( + git_iterator **out, + const char *root, + git_iterator_options *options); + +extern void git_iterator_free(git_iterator *iter); + +/* Return a git_index_entry structure for the current value the iterator + * is looking at or NULL if the iterator is at the end. + * + * The entry may noy be fully populated. Tree iterators will only have a + * value mode, OID, and path. Workdir iterators will not have an OID (but + * you can use `git_iterator_current_oid()` to calculate it on demand). + * + * You do not need to free the entry. It is still "owned" by the iterator. + * Once you call `git_iterator_advance()` then the old entry is no longer + * guaranteed to be valid - it may be freed or just overwritten in place. + */ +GIT_INLINE(int) git_iterator_current( + const git_index_entry **entry, git_iterator *iter) +{ + return iter->cb->current(entry, iter); +} + +/** + * Advance to the next item for the iterator. + * + * If GIT_ITERATOR_INCLUDE_TREES is set, this may be a tree item. If + * GIT_ITERATOR_DONT_AUTOEXPAND is set, calling this again when on a tree + * item will skip over all the items under that tree. + */ +GIT_INLINE(int) git_iterator_advance( + const git_index_entry **entry, git_iterator *iter) +{ + return iter->cb->advance(entry, iter); +} + +/** + * Iterate into a tree item (when GIT_ITERATOR_DONT_AUTOEXPAND is set). + * + * git_iterator_advance() steps through all items being iterated over + * (either with or without trees, depending on GIT_ITERATOR_INCLUDE_TREES), + * but if GIT_ITERATOR_DONT_AUTOEXPAND is set, it will skip to the next + * sibling of a tree instead of going to the first child of the tree. In + * that case, use this function to advance to the first child of the tree. + * + * If the current item is not a tree, this is a no-op. + * + * For filesystem and working directory iterators, a tree (i.e. directory) + * can be empty. In that case, this function returns GIT_ENOTFOUND and + * does not advance. That can't happen for tree and index iterators. + */ +GIT_INLINE(int) git_iterator_advance_into( + const git_index_entry **entry, git_iterator *iter) +{ + return iter->cb->advance_into(entry, iter); +} + +/* Advance over a directory and check if it contains no files or just + * ignored files. + * + * In a tree or the index, all directories will contain files, but in the + * working directory it is possible to have an empty directory tree or a + * tree that only contains ignored files. Many Git operations treat these + * cases specially. This advances over a directory (presumably an + * untracked directory) but checks during the scan if there are any files + * and any non-ignored files. + */ +GIT_INLINE(int) git_iterator_advance_over( + const git_index_entry **entry, + git_iterator_status_t *status, + git_iterator *iter) +{ + return iter->cb->advance_over(entry, status, iter); +} + +/** + * Go back to the start of the iteration. + */ +GIT_INLINE(int) git_iterator_reset(git_iterator *iter) +{ + return iter->cb->reset(iter); +} + +/** + * Go back to the start of the iteration after updating the `start` and + * `end` pathname boundaries of the iteration. + */ +extern int git_iterator_reset_range( + git_iterator *iter, const char *start, const char *end); + +GIT_INLINE(git_iterator_t) git_iterator_type(git_iterator *iter) +{ + return iter->type; +} + +GIT_INLINE(git_repository *) git_iterator_owner(git_iterator *iter) +{ + return iter->repo; +} + +GIT_INLINE(git_index *) git_iterator_index(git_iterator *iter) +{ + return iter->index; +} + +GIT_INLINE(git_iterator_flag_t) git_iterator_flags(git_iterator *iter) +{ + return iter->flags; +} + +GIT_INLINE(bool) git_iterator_ignore_case(git_iterator *iter) +{ + return ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0); +} + +extern int git_iterator_set_ignore_case( + git_iterator *iter, bool ignore_case); + +extern int git_iterator_current_tree_entry( + const git_tree_entry **entry_out, git_iterator *iter); + +extern int git_iterator_current_parent_tree( + const git_tree **tree_out, git_iterator *iter, size_t depth); + +extern bool git_iterator_current_is_ignored(git_iterator *iter); + +extern bool git_iterator_current_tree_is_ignored(git_iterator *iter); + +/** + * Get full path of the current item from a workdir iterator. This will + * return NULL for a non-workdir iterator. The git_str is still owned by + * the iterator; this is exposed just for efficiency. + */ +extern int git_iterator_current_workdir_path( + git_str **path, git_iterator *iter); + +/** + * Retrieve the index stored in the iterator. + * + * Only implemented for the workdir and index iterators. + */ +extern git_index *git_iterator_index(git_iterator *iter); + +typedef int (*git_iterator_foreach_cb)( + const git_index_entry *entry, + void *data); + +/** + * Walk the given iterator and invoke the callback for each path + * contained in the iterator. + */ +extern int git_iterator_foreach( + git_iterator *iterator, + git_iterator_foreach_cb cb, + void *data); + +typedef int (*git_iterator_walk_cb)( + const git_index_entry **entries, + void *data); + +/** + * Walk the given iterators in lock-step. The given callback will be + * called for each unique path, with the index entry in each iterator + * (or NULL if the given iterator does not contain that path). + */ +extern int git_iterator_walk( + git_iterator **iterators, + size_t cnt, + git_iterator_walk_cb cb, + void *data); + +#endif diff --git a/src/libgit2/libgit2.c b/src/libgit2/libgit2.c new file mode 100644 index 00000000000..37e0bd012fe --- /dev/null +++ b/src/libgit2/libgit2.c @@ -0,0 +1,278 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include "alloc.h" +#include "buf.h" +#include "common.h" +#include "filter.h" +#include "hash.h" +#include "merge_driver.h" +#include "pool.h" +#include "mwindow.h" +#include "oid.h" +#include "rand.h" +#include "runtime.h" +#include "settings.h" +#include "sysdir.h" +#include "thread.h" +#include "git2/global.h" +#include "streams/registry.h" +#include "streams/mbedtls.h" +#include "streams/openssl.h" +#include "streams/socket.h" +#include "transports/ssh_libssh2.h" + +#ifdef GIT_WIN32 +# include "win32/w32_leakcheck.h" +#endif + +int git_libgit2_init(void) +{ + static git_runtime_init_fn init_fns[] = { +#ifdef GIT_WIN32 + git_win32_leakcheck_global_init, +#endif + git_allocator_global_init, + git_error_global_init, + git_threads_global_init, + git_oid_global_init, + git_rand_global_init, + git_hash_global_init, + git_sysdir_global_init, + git_filter_global_init, + git_merge_driver_global_init, + git_transport_ssh_libssh2_global_init, + git_stream_registry_global_init, + git_socket_stream_global_init, + git_openssl_stream_global_init, + git_mbedtls_stream_global_init, + git_mwindow_global_init, + git_pool_global_init, + git_settings_global_init + }; + + return git_runtime_init(init_fns, ARRAY_SIZE(init_fns)); +} + +int git_libgit2_shutdown(void) +{ + return git_runtime_shutdown(); +} + +int git_libgit2_version(int *major, int *minor, int *rev) +{ + *major = LIBGIT2_VERSION_MAJOR; + *minor = LIBGIT2_VERSION_MINOR; + *rev = LIBGIT2_VERSION_REVISION; + + return 0; +} + +const char *git_libgit2_prerelease(void) +{ + return LIBGIT2_VERSION_PRERELEASE; +} + +int git_libgit2_features(void) +{ + return 0 +#ifdef GIT_THREADS + | GIT_FEATURE_THREADS +#endif +#ifdef GIT_HTTPS + | GIT_FEATURE_HTTPS +#endif +#ifdef GIT_SSH + | GIT_FEATURE_SSH +#endif +#ifdef GIT_NSEC + | GIT_FEATURE_NSEC +#endif + | GIT_FEATURE_HTTP_PARSER + | GIT_FEATURE_REGEX +#ifdef GIT_I18N_ICONV + | GIT_FEATURE_I18N +#endif +#if defined(GIT_AUTH_NTLM) + | GIT_FEATURE_AUTH_NTLM +#endif +#if defined(GIT_AUTH_NEGOTIATE) + | GIT_FEATURE_AUTH_NEGOTIATE +#endif + | GIT_FEATURE_COMPRESSION + | GIT_FEATURE_SHA1 +#ifdef GIT_EXPERIMENTAL_SHA256 + | GIT_FEATURE_SHA256 +#endif + ; +} + +const char *git_libgit2_feature_backend(git_feature_t feature) +{ + switch (feature) { + case GIT_FEATURE_THREADS: +#if defined(GIT_THREADS_PTHREADS) + return "pthread"; +#elif defined(GIT_THREADS_WIN32) + return "win32"; +#elif defined(GIT_THREADS) + GIT_ASSERT_WITH_RETVAL(!"Unknown threads backend", NULL); +#endif + break; + + case GIT_FEATURE_HTTPS: +#if defined(GIT_HTTPS_OPENSSL) + return "openssl"; +#elif defined(GIT_HTTPS_OPENSSL_DYNAMIC) + return "openssl-dynamic"; +#elif defined(GIT_HTTPS_MBEDTLS) + return "mbedtls"; +#elif defined(GIT_HTTPS_SECURETRANSPORT) + return "securetransport"; +#elif defined(GIT_HTTPS_SCHANNEL) + return "schannel"; +#elif defined(GIT_HTTPS_WINHTTP) + return "winhttp"; +#elif defined(GIT_HTTPS) + GIT_ASSERT_WITH_RETVAL(!"Unknown HTTPS backend", NULL); +#endif + break; + + case GIT_FEATURE_SSH: +#if defined(GIT_SSH_EXEC) + return "exec"; +#elif defined(GIT_SSH_LIBSSH2) + return "libssh2"; +#elif defined(GIT_SSH) + GIT_ASSERT_WITH_RETVAL(!"Unknown SSH backend", NULL); +#endif + break; + + case GIT_FEATURE_NSEC: +#if defined(GIT_NSEC_MTIMESPEC) + return "mtimespec"; +#elif defined(GIT_NSEC_MTIM) + return "mtim"; +#elif defined(GIT_NSEC_MTIME_NSEC) + return "mtime_nsec"; +#elif defined(GIT_NSEC_WIN32) + return "win32"; +#elif defined(GIT_NSEC) + GIT_ASSERT_WITH_RETVAL(!"Unknown high-resolution time backend", NULL); +#endif + break; + + case GIT_FEATURE_HTTP_PARSER: +#if defined(GIT_HTTPPARSER_HTTPPARSER) + return "httpparser"; +#elif defined(GIT_HTTPPARSER_LLHTTP) + return "llhttp"; +#elif defined(GIT_HTTPPARSER_BUILTIN) + return "builtin"; +#endif + GIT_ASSERT_WITH_RETVAL(!"Unknown HTTP parser backend", NULL); + break; + + case GIT_FEATURE_REGEX: +#if defined(GIT_REGEX_REGCOMP_L) + return "regcomp_l"; +#elif defined(GIT_REGEX_REGCOMP) + return "regcomp"; +#elif defined(GIT_REGEX_PCRE) + return "pcre"; +#elif defined(GIT_REGEX_PCRE2) + return "pcre2"; +#elif defined(GIT_REGEX_BUILTIN) + return "builtin"; +#endif + GIT_ASSERT_WITH_RETVAL(!"Unknown regular expression backend", NULL); + break; + + case GIT_FEATURE_I18N: +#if defined(GIT_I18N_ICONV) + return "iconv"; +#elif defined(GIT_I18N) + GIT_ASSERT_WITH_RETVAL(!"Unknown internationalization backend", NULL); +#endif + break; + + case GIT_FEATURE_AUTH_NTLM: +#if defined(GIT_AUTH_NTLM_BUILTIN) + return "builtin"; +#elif defined(GIT_AUTH_NTLM_SSPI) + return "sspi"; +#elif defined(GIT_AUTH_NTLM) + GIT_ASSERT_WITH_RETVAL(!"Unknown NTLM backend", NULL); +#endif + break; + + case GIT_FEATURE_AUTH_NEGOTIATE: +#if defined(GIT_AUTH_NEGOTIATE_GSSFRAMEWORK) + return "gssframework"; +#elif defined(GIT_AUTH_NEGOTIATE_GSSAPI) + return "gssapi"; +#elif defined(GIT_AUTH_NEGOTIATE_SSPI) + return "sspi"; +#elif defined(GIT_AUTH_NEGOTIATE) + GIT_ASSERT_WITH_RETVAL(!"Unknown Negotiate backend", NULL); +#endif + break; + + case GIT_FEATURE_COMPRESSION: +#if defined(GIT_COMPRESSION_ZLIB) + return "zlib"; +#elif defined(GIT_COMPRESSION_BUILTIN) + return "builtin"; +#else + GIT_ASSERT_WITH_RETVAL(!"Unknown compression backend", NULL); +#endif + break; + + case GIT_FEATURE_SHA1: +#if defined(GIT_SHA1_BUILTIN) + return "builtin"; +#elif defined(GIT_SHA1_OPENSSL) + return "openssl"; +#elif defined(GIT_SHA1_OPENSSL_FIPS) + return "openssl-fips"; +#elif defined(GIT_SHA1_OPENSSL_DYNAMIC) + return "openssl-dynamic"; +#elif defined(GIT_SHA1_MBEDTLS) + return "mbedtls"; +#elif defined(GIT_SHA1_COMMON_CRYPTO) + return "commoncrypto"; +#elif defined(GIT_SHA1_WIN32) + return "win32"; +#else + GIT_ASSERT_WITH_RETVAL(!"Unknown SHA1 backend", NULL); +#endif + break; + + case GIT_FEATURE_SHA256: +#if defined(GIT_EXPERIMENTAL_SHA256) && defined(GIT_SHA256_BUILTIN) + return "builtin"; +#elif defined(GIT_EXPERIMENTAL_SHA256) && defined(GIT_SHA256_OPENSSL) + return "openssl"; +#elif defined(GIT_EXPERIMENTAL_SHA256) && defined(GIT_SHA256_OPENSSL_FIPS) + return "openssl-fips"; +#elif defined(GIT_EXPERIMENTAL_SHA256) && defined(GIT_SHA256_OPENSSL_DYNAMIC) + return "openssl-dynamic"; +#elif defined(GIT_EXPERIMENTAL_SHA256) && defined(GIT_SHA256_MBEDTLS) + return "mbedtls"; +#elif defined(GIT_EXPERIMENTAL_SHA256) && defined(GIT_SHA256_COMMON_CRYPTO) + return "commoncrypto"; +#elif defined(GIT_EXPERIMENTAL_SHA256) && defined(GIT_SHA256_WIN32) + return "win32"; +#elif defined(GIT_EXPERIMENTAL_SHA256) + GIT_ASSERT_WITH_RETVAL(!"Unknown SHA256 backend", NULL); +#endif + break; + } + + return NULL; +} diff --git a/src/libgit2/mailmap.c b/src/libgit2/mailmap.c new file mode 100644 index 00000000000..05c88ff02c1 --- /dev/null +++ b/src/libgit2/mailmap.c @@ -0,0 +1,500 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "mailmap.h" + +#include "common.h" +#include "config.h" +#include "fs_path.h" +#include "repository.h" +#include "signature.h" +#include "git2/config.h" +#include "git2/revparse.h" +#include "blob.h" +#include "parse.h" +#include "path.h" + +#define MM_FILE ".mailmap" +#define MM_FILE_CONFIG "mailmap.file" +#define MM_BLOB_CONFIG "mailmap.blob" +#define MM_BLOB_DEFAULT "HEAD:" MM_FILE + +static void mailmap_entry_free(git_mailmap_entry *entry) +{ + if (!entry) + return; + + git__free(entry->real_name); + git__free(entry->real_email); + git__free(entry->replace_name); + git__free(entry->replace_email); + git__free(entry); +} + +/* + * First we sort by replace_email, then replace_name (if present). + * Entries with names are greater than entries without. + */ +static int mailmap_entry_cmp(const void *a_raw, const void *b_raw) +{ + const git_mailmap_entry *a = (const git_mailmap_entry *)a_raw; + const git_mailmap_entry *b = (const git_mailmap_entry *)b_raw; + int cmp; + + GIT_ASSERT_ARG(a && a->replace_email); + GIT_ASSERT_ARG(b && b->replace_email); + + cmp = git__strcmp(a->replace_email, b->replace_email); + if (cmp) + return cmp; + + /* NULL replace_names are less than not-NULL ones */ + if (a->replace_name == NULL || b->replace_name == NULL) + return (int)(a->replace_name != NULL) - (int)(b->replace_name != NULL); + + return git__strcmp(a->replace_name, b->replace_name); +} + +/* Replace the old entry with the new on duplicate. */ +static int mailmap_entry_replace(void **old_raw, void *new_raw) +{ + mailmap_entry_free((git_mailmap_entry *)*old_raw); + *old_raw = new_raw; + return GIT_EEXISTS; +} + +/* Check if we're at the end of line, w/ comments */ +static bool is_eol(git_parse_ctx *ctx) +{ + char c; + return git_parse_peek(&c, ctx, GIT_PARSE_PEEK_SKIP_WHITESPACE) < 0 || c == '#'; +} + +static int advance_until( + const char **start, size_t *len, git_parse_ctx *ctx, char needle) +{ + *start = ctx->line; + while (ctx->line_len > 0 && *ctx->line != '#' && *ctx->line != needle) + git_parse_advance_chars(ctx, 1); + + if (ctx->line_len == 0 || *ctx->line == '#') + return -1; /* end of line */ + + *len = ctx->line - *start; + git_parse_advance_chars(ctx, 1); /* advance past needle */ + return 0; +} + +/* + * Parse a single entry from a mailmap file. + * + * The output git_strs will be non-owning, and should be copied before being + * persisted. + */ +static int parse_mailmap_entry( + git_str *real_name, git_str *real_email, + git_str *replace_name, git_str *replace_email, + git_parse_ctx *ctx) +{ + const char *start; + size_t len; + + git_str_clear(real_name); + git_str_clear(real_email); + git_str_clear(replace_name); + git_str_clear(replace_email); + + git_parse_advance_ws(ctx); + if (is_eol(ctx)) + return -1; /* blank line */ + + /* Parse the real name */ + if (advance_until(&start, &len, ctx, '<') < 0) + return -1; + + git_str_attach_notowned(real_name, start, len); + git_str_rtrim(real_name); + + /* + * If this is the last email in the line, this is the email to replace, + * otherwise, it's the real email. + */ + if (advance_until(&start, &len, ctx, '>') < 0) + return -1; + + /* If we aren't at the end of the line, parse a second name and email */ + if (!is_eol(ctx)) { + git_str_attach_notowned(real_email, start, len); + + git_parse_advance_ws(ctx); + if (advance_until(&start, &len, ctx, '<') < 0) + return -1; + git_str_attach_notowned(replace_name, start, len); + git_str_rtrim(replace_name); + + if (advance_until(&start, &len, ctx, '>') < 0) + return -1; + } + + git_str_attach_notowned(replace_email, start, len); + + if (!is_eol(ctx)) + return -1; + + return 0; +} + +int git_mailmap_new(git_mailmap **out) +{ + int error; + git_mailmap *mm = git__calloc(1, sizeof(git_mailmap)); + GIT_ERROR_CHECK_ALLOC(mm); + + error = git_vector_init(&mm->entries, 0, mailmap_entry_cmp); + if (error < 0) { + git__free(mm); + return error; + } + *out = mm; + return 0; +} + +void git_mailmap_free(git_mailmap *mm) +{ + size_t idx; + git_mailmap_entry *entry; + if (!mm) + return; + + git_vector_foreach(&mm->entries, idx, entry) + mailmap_entry_free(entry); + + git_vector_dispose(&mm->entries); + git__free(mm); +} + +static int mailmap_add_entry_unterminated( + git_mailmap *mm, + const char *real_name, size_t real_name_size, + const char *real_email, size_t real_email_size, + const char *replace_name, size_t replace_name_size, + const char *replace_email, size_t replace_email_size) +{ + int error; + git_mailmap_entry *entry = git__calloc(1, sizeof(git_mailmap_entry)); + GIT_ERROR_CHECK_ALLOC(entry); + + GIT_ASSERT_ARG(mm); + GIT_ASSERT_ARG(replace_email && *replace_email); + + if (real_name_size > 0) { + entry->real_name = git__substrdup(real_name, real_name_size); + GIT_ERROR_CHECK_ALLOC(entry->real_name); + } + if (real_email_size > 0) { + entry->real_email = git__substrdup(real_email, real_email_size); + GIT_ERROR_CHECK_ALLOC(entry->real_email); + } + if (replace_name_size > 0) { + entry->replace_name = git__substrdup(replace_name, replace_name_size); + GIT_ERROR_CHECK_ALLOC(entry->replace_name); + } + entry->replace_email = git__substrdup(replace_email, replace_email_size); + GIT_ERROR_CHECK_ALLOC(entry->replace_email); + + error = git_vector_insert_sorted(&mm->entries, entry, mailmap_entry_replace); + if (error == GIT_EEXISTS) + error = GIT_OK; + else if (error < 0) + mailmap_entry_free(entry); + + return error; +} + +int git_mailmap_add_entry( + git_mailmap *mm, const char *real_name, const char *real_email, + const char *replace_name, const char *replace_email) +{ + return mailmap_add_entry_unterminated( + mm, + real_name, real_name ? strlen(real_name) : 0, + real_email, real_email ? strlen(real_email) : 0, + replace_name, replace_name ? strlen(replace_name) : 0, + replace_email, strlen(replace_email)); +} + +static int mailmap_add_buffer(git_mailmap *mm, const char *buf, size_t len) +{ + int error = 0; + git_parse_ctx ctx; + + /* Scratch buffers containing the real parsed names & emails */ + git_str real_name = GIT_STR_INIT; + git_str real_email = GIT_STR_INIT; + git_str replace_name = GIT_STR_INIT; + git_str replace_email = GIT_STR_INIT; + + /* Buffers may not contain '\0's. */ + if (memchr(buf, '\0', len) != NULL) + return -1; + + git_parse_ctx_init(&ctx, buf, len); + + /* Run the parser */ + while (ctx.remain_len > 0) { + error = parse_mailmap_entry( + &real_name, &real_email, &replace_name, &replace_email, &ctx); + if (error < 0) { + error = 0; /* Skip lines which don't contain a valid entry */ + git_parse_advance_line(&ctx); + continue; /* TODO: warn */ + } + + /* NOTE: Can't use add_entry(...) as our buffers aren't terminated */ + error = mailmap_add_entry_unterminated( + mm, real_name.ptr, real_name.size, real_email.ptr, real_email.size, + replace_name.ptr, replace_name.size, replace_email.ptr, replace_email.size); + if (error < 0) + goto cleanup; + + error = 0; + } + +cleanup: + git_str_dispose(&real_name); + git_str_dispose(&real_email); + git_str_dispose(&replace_name); + git_str_dispose(&replace_email); + return error; +} + +int git_mailmap_from_buffer(git_mailmap **out, const char *data, size_t len) +{ + int error = git_mailmap_new(out); + if (error < 0) + return error; + + error = mailmap_add_buffer(*out, data, len); + if (error < 0) { + git_mailmap_free(*out); + *out = NULL; + } + return error; +} + +static int mailmap_add_blob( + git_mailmap *mm, git_repository *repo, const char *rev) +{ + git_object *object = NULL; + git_blob *blob = NULL; + git_str content = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(mm); + GIT_ASSERT_ARG(repo); + + error = git_revparse_single(&object, repo, rev); + if (error < 0) + goto cleanup; + + error = git_object_peel((git_object **)&blob, object, GIT_OBJECT_BLOB); + if (error < 0) + goto cleanup; + + error = git_blob__getbuf(&content, blob); + if (error < 0) + goto cleanup; + + error = mailmap_add_buffer(mm, content.ptr, content.size); + if (error < 0) + goto cleanup; + +cleanup: + git_str_dispose(&content); + git_blob_free(blob); + git_object_free(object); + return error; +} + +static int mailmap_add_file_ondisk( + git_mailmap *mm, const char *path, git_repository *repo) +{ + const char *base = repo ? git_repository_workdir(repo) : NULL; + git_str fullpath = GIT_STR_INIT; + git_str content = GIT_STR_INIT; + int error; + + error = git_fs_path_join_unrooted(&fullpath, path, base, NULL); + if (error < 0) + goto cleanup; + + error = git_path_validate_str_length(repo, &fullpath); + if (error < 0) + goto cleanup; + + error = git_futils_readbuffer(&content, fullpath.ptr); + if (error < 0) + goto cleanup; + + error = mailmap_add_buffer(mm, content.ptr, content.size); + if (error < 0) + goto cleanup; + +cleanup: + git_str_dispose(&fullpath); + git_str_dispose(&content); + return error; +} + +/* NOTE: Only expose with an error return, currently never errors */ +static void mailmap_add_from_repository(git_mailmap *mm, git_repository *repo) +{ + git_config *config = NULL; + git_str rev_buf = GIT_STR_INIT; + git_str path_buf = GIT_STR_INIT; + const char *rev = NULL; + const char *path = NULL; + + /* If we're in a bare repo, default blob to 'HEAD:.mailmap' */ + if (repo->is_bare) + rev = MM_BLOB_DEFAULT; + + /* Try to load 'mailmap.file' and 'mailmap.blob' cfgs from the repo */ + if (git_repository_config(&config, repo) == 0) { + if (git_config__get_string_buf(&rev_buf, config, MM_BLOB_CONFIG) == 0) + rev = rev_buf.ptr; + if (git_config__get_path(&path_buf, config, MM_FILE_CONFIG) == 0) + path = path_buf.ptr; + } + + /* + * Load mailmap files in order, overriding previous entries with new ones. + * 1. The '.mailmap' file in the repository's workdir root, + * 2. The blob described by the 'mailmap.blob' config (default HEAD:.mailmap), + * 3. The file described by the 'mailmap.file' config. + * + * We ignore errors from these loads, as these files may not exist, or may + * contain invalid information, and we don't want to report that error. + * + * XXX: Warn? + */ + if (!repo->is_bare) + mailmap_add_file_ondisk(mm, MM_FILE, repo); + if (rev != NULL) + mailmap_add_blob(mm, repo, rev); + if (path != NULL) + mailmap_add_file_ondisk(mm, path, repo); + + git_str_dispose(&rev_buf); + git_str_dispose(&path_buf); + git_config_free(config); +} + +int git_mailmap_from_repository(git_mailmap **out, git_repository *repo) +{ + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + if ((error = git_mailmap_new(out)) < 0) + return error; + + mailmap_add_from_repository(*out, repo); + return 0; +} + +const git_mailmap_entry *git_mailmap_entry_lookup( + const git_mailmap *mm, const char *name, const char *email) +{ + int error; + ssize_t fallback = -1; + size_t idx; + git_mailmap_entry *entry; + + /* The lookup needle we want to use only sets the replace_email. */ + git_mailmap_entry needle = { NULL }; + needle.replace_email = (char *)email; + + GIT_ASSERT_ARG_WITH_RETVAL(email, NULL); + + if (!mm) + return NULL; + + /* + * We want to find the place to start looking. so we do a binary search for + * the "fallback" nameless entry. If we find it, we advance past it and record + * the index. + */ + error = git_vector_bsearch(&idx, (git_vector *)&mm->entries, &needle); + if (error >= 0) + fallback = idx++; + else if (error != GIT_ENOTFOUND) + return NULL; + + /* do a linear search for an exact match */ + for (; idx < git_vector_length(&mm->entries); ++idx) { + entry = git_vector_get(&mm->entries, idx); + + if (git__strcmp(entry->replace_email, email)) + break; /* it's a different email, so we're done looking */ + + /* should be specific */ + GIT_ASSERT_WITH_RETVAL(entry->replace_name, NULL); + if (!name || !git__strcmp(entry->replace_name, name)) + return entry; + } + + if (fallback < 0) + return NULL; /* no fallback */ + return git_vector_get(&mm->entries, fallback); +} + +int git_mailmap_resolve( + const char **real_name, const char **real_email, + const git_mailmap *mailmap, + const char *name, const char *email) +{ + const git_mailmap_entry *entry = NULL; + + GIT_ASSERT(name); + GIT_ASSERT(email); + + *real_name = name; + *real_email = email; + + if ((entry = git_mailmap_entry_lookup(mailmap, name, email))) { + if (entry->real_name) + *real_name = entry->real_name; + if (entry->real_email) + *real_email = entry->real_email; + } + return 0; +} + +int git_mailmap_resolve_signature( + git_signature **out, const git_mailmap *mailmap, const git_signature *sig) +{ + const char *name = NULL; + const char *email = NULL; + int error; + + if (!sig) + return 0; + + error = git_mailmap_resolve(&name, &email, mailmap, sig->name, sig->email); + if (error < 0) + return error; + + error = git_signature_new(out, name, email, sig->when.time, sig->when.offset); + if (error < 0) + return error; + + /* Copy over the sign, as git_signature_new doesn't let you pass it. */ + (*out)->when.sign = sig->when.sign; + return 0; +} diff --git a/src/libgit2/mailmap.h b/src/libgit2/mailmap.h new file mode 100644 index 00000000000..2c9736a4a85 --- /dev/null +++ b/src/libgit2/mailmap.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_mailmap_h__ +#define INCLUDE_mailmap_h__ + +#include "git2/mailmap.h" +#include "vector.h" + +/* + * A mailmap is stored as a sorted vector of 'git_mailmap_entry's. These entries + * are sorted first by 'replace_email', and then by 'replace_name'. NULL + * replace_names are ordered first. + * + * Looking up a name and email in the mailmap is done with a binary search. + */ +struct git_mailmap { + git_vector entries; +}; + +/* Single entry parsed from a mailmap */ +typedef struct git_mailmap_entry { + char *real_name; /**< the real name (may be NULL) */ + char *real_email; /**< the real email (may be NULL) */ + char *replace_name; /**< the name to replace (may be NULL) */ + char *replace_email; /**< the email to replace */ +} git_mailmap_entry; + +const git_mailmap_entry *git_mailmap_entry_lookup( + const git_mailmap *mm, const char *name, const char *email); + +#endif diff --git a/src/libgit2/merge.c b/src/libgit2/merge.c new file mode 100644 index 00000000000..b73454d7755 --- /dev/null +++ b/src/libgit2/merge.c @@ -0,0 +1,3449 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "merge.h" + +#include "posix.h" +#include "str.h" +#include "repository.h" +#include "revwalk.h" +#include "commit_list.h" +#include "fs_path.h" +#include "refs.h" +#include "object.h" +#include "iterator.h" +#include "refs.h" +#include "diff.h" +#include "diff_generate.h" +#include "diff_tform.h" +#include "checkout.h" +#include "tree.h" +#include "blob.h" +#include "oid.h" +#include "index.h" +#include "filebuf.h" +#include "config.h" +#include "oidarray.h" +#include "annotated_commit.h" +#include "commit.h" +#include "oidarray.h" +#include "merge_driver.h" +#include "array.h" + +#include "git2/types.h" +#include "git2/repository.h" +#include "git2/object.h" +#include "git2/commit.h" +#include "git2/merge.h" +#include "git2/refs.h" +#include "git2/reset.h" +#include "git2/checkout.h" +#include "git2/signature.h" +#include "git2/config.h" +#include "git2/tree.h" +#include "git2/oidarray.h" +#include "git2/annotated_commit.h" +#include "git2/sys/index.h" +#include "git2/sys/hashsig.h" + +#define GIT_MERGE_INDEX_ENTRY_EXISTS(X) ((X).mode != 0) +#define GIT_MERGE_INDEX_ENTRY_ISFILE(X) S_ISREG((X).mode) + + +typedef enum { + TREE_IDX_ANCESTOR = 0, + TREE_IDX_OURS = 1, + TREE_IDX_THEIRS = 2 +} merge_tree_index_t; + +/* Tracks D/F conflicts */ +struct merge_diff_df_data { + const char *df_path; + const char *prev_path; + git_merge_diff *prev_conflict; +}; + +/* + * This acts as a negative cache entry marker. In case we've tried to calculate + * similarity metrics for a given blob already but `git_hashsig` determined + * that it's too small in order to have a meaningful hash signature, we will + * insert the address of this marker instead of `NULL`. Like this, we can + * easily check whether we have checked a gien entry already and skip doing the + * calculation again and again. + */ +static int cache_invalid_marker; + +/* Merge base computation */ + +static int merge_bases_many(git_commit_list **out, git_revwalk **walk_out, git_repository *repo, size_t length, const git_oid input_array[]) +{ + git_revwalk *walk = NULL; + git_vector list; + git_commit_list *result = NULL; + git_commit_list_node *commit; + int error = -1; + unsigned int i; + + if (length < 2) { + git_error_set(GIT_ERROR_INVALID, "at least two commits are required to find an ancestor"); + return -1; + } + + if (git_vector_init(&list, length - 1, NULL) < 0) + return -1; + + if (git_revwalk_new(&walk, repo) < 0) + goto on_error; + + for (i = 1; i < length; i++) { + commit = git_revwalk__commit_lookup(walk, &input_array[i]); + if (commit == NULL) + goto on_error; + + git_vector_insert(&list, commit); + } + + commit = git_revwalk__commit_lookup(walk, &input_array[0]); + if (commit == NULL) + goto on_error; + + if (git_merge__bases_many(&result, walk, commit, &list, 0) < 0) + goto on_error; + + if (!result) { + git_error_set(GIT_ERROR_MERGE, "no merge base found"); + error = GIT_ENOTFOUND; + goto on_error; + } + + *out = result; + *walk_out = walk; + + git_vector_dispose(&list); + return 0; + +on_error: + git_vector_dispose(&list); + git_revwalk_free(walk); + return error; +} + +int git_merge_base_many(git_oid *out, git_repository *repo, size_t length, const git_oid input_array[]) +{ + git_revwalk *walk; + git_commit_list *result = NULL; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(input_array); + + if ((error = merge_bases_many(&result, &walk, repo, length, input_array)) < 0) + return error; + + git_oid_cpy(out, &result->item->oid); + + git_commit_list_free(&result); + git_revwalk_free(walk); + + return 0; +} + +int git_merge_bases_many(git_oidarray *out, git_repository *repo, size_t length, const git_oid input_array[]) +{ + git_revwalk *walk; + git_commit_list *list, *result = NULL; + int error = 0; + git_array_oid_t array; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(input_array); + + if ((error = merge_bases_many(&result, &walk, repo, length, input_array)) < 0) + return error; + + git_array_init(array); + + list = result; + while (list) { + git_oid *id = git_array_alloc(array); + if (id == NULL) { + error = -1; + goto cleanup; + } + + git_oid_cpy(id, &list->item->oid); + list = list->next; + } + + git_oidarray__from_array(out, &array); + +cleanup: + git_commit_list_free(&result); + git_revwalk_free(walk); + + return error; +} + +int git_merge_base_octopus(git_oid *out, git_repository *repo, size_t length, const git_oid input_array[]) +{ + git_oid result; + unsigned int i; + int error = -1; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(input_array); + + if (length < 2) { + git_error_set(GIT_ERROR_INVALID, "at least two commits are required to find an ancestor"); + return -1; + } + + result = input_array[0]; + for (i = 1; i < length; i++) { + error = git_merge_base(&result, repo, &result, &input_array[i]); + if (error < 0) + return error; + } + + *out = result; + + return 0; +} + +static int merge_bases(git_commit_list **out, git_revwalk **walk_out, git_repository *repo, const git_oid *one, const git_oid *two) +{ + git_revwalk *walk; + git_vector list; + git_commit_list *result = NULL; + git_commit_list_node *commit; + void *contents[1]; + + if (git_revwalk_new(&walk, repo) < 0) + return -1; + + commit = git_revwalk__commit_lookup(walk, two); + if (commit == NULL) + goto on_error; + + /* This is just one value, so we can do it on the stack */ + memset(&list, 0x0, sizeof(git_vector)); + contents[0] = commit; + list.length = 1; + list.contents = contents; + + commit = git_revwalk__commit_lookup(walk, one); + if (commit == NULL) + goto on_error; + + if (git_merge__bases_many(&result, walk, commit, &list, 0) < 0) + goto on_error; + + if (!result) { + git_revwalk_free(walk); + git_error_set(GIT_ERROR_MERGE, "no merge base found"); + return GIT_ENOTFOUND; + } + + *out = result; + *walk_out = walk; + + return 0; + +on_error: + git_revwalk_free(walk); + return -1; + +} + +int git_merge_base(git_oid *out, git_repository *repo, const git_oid *one, const git_oid *two) +{ + int error; + git_revwalk *walk; + git_commit_list *result; + + if ((error = merge_bases(&result, &walk, repo, one, two)) < 0) + return error; + + git_oid_cpy(out, &result->item->oid); + git_commit_list_free(&result); + git_revwalk_free(walk); + + return 0; +} + +int git_merge_bases(git_oidarray *out, git_repository *repo, const git_oid *one, const git_oid *two) +{ + int error; + git_revwalk *walk; + git_commit_list *result, *list; + git_array_oid_t array; + + git_array_init(array); + + if ((error = merge_bases(&result, &walk, repo, one, two)) < 0) + return error; + + list = result; + while (list) { + git_oid *id = git_array_alloc(array); + if (id == NULL) + goto on_error; + + git_oid_cpy(id, &list->item->oid); + list = list->next; + } + + git_oidarray__from_array(out, &array); + git_commit_list_free(&result); + git_revwalk_free(walk); + + return 0; + +on_error: + git_commit_list_free(&result); + git_revwalk_free(walk); + return -1; +} + +static int interesting(git_pqueue *list) +{ + size_t i; + + for (i = 0; i < git_pqueue_size(list); i++) { + git_commit_list_node *commit = git_pqueue_get(list, i); + if ((commit->flags & STALE) == 0) + return 1; + } + + return 0; +} + +static int clear_commit_marks_1(git_commit_list **plist, + git_commit_list_node *commit, unsigned int mark) +{ + while (commit) { + unsigned int i; + + if (!(mark & commit->flags)) + return 0; + + commit->flags &= ~mark; + + for (i = 1; i < commit->out_degree; i++) { + git_commit_list_node *p = commit->parents[i]; + if (git_commit_list_insert(p, plist) == NULL) + return -1; + } + + commit = commit->out_degree ? commit->parents[0] : NULL; + } + + return 0; +} + +static int clear_commit_marks_many(git_vector *commits, unsigned int mark) +{ + git_commit_list *list = NULL; + git_commit_list_node *c; + unsigned int i; + + git_vector_foreach(commits, i, c) { + if (git_commit_list_insert(c, &list) == NULL) + return -1; + } + + while (list) + if (clear_commit_marks_1(&list, git_commit_list_pop(&list), mark) < 0) + return -1; + return 0; +} + +static int clear_commit_marks(git_commit_list_node *commit, unsigned int mark) +{ + git_commit_list *list = NULL; + if (git_commit_list_insert(commit, &list) == NULL) + return -1; + while (list) + if (clear_commit_marks_1(&list, git_commit_list_pop(&list), mark) < 0) + return -1; + return 0; +} + +static int paint_down_to_common( + git_commit_list **out, + git_revwalk *walk, + git_commit_list_node *one, + git_vector *twos, + uint32_t minimum_generation) +{ + git_pqueue list; + git_commit_list *result = NULL; + git_commit_list_node *two; + + int error; + unsigned int i; + + if (git_pqueue_init(&list, 0, twos->length * 2, git_commit_list_generation_cmp) < 0) + return -1; + + one->flags |= PARENT1; + if (git_pqueue_insert(&list, one) < 0) + return -1; + + git_vector_foreach(twos, i, two) { + if (git_commit_list_parse(walk, two) < 0) + return -1; + + two->flags |= PARENT2; + if (git_pqueue_insert(&list, two) < 0) + return -1; + } + + /* as long as there are non-STALE commits */ + while (interesting(&list)) { + git_commit_list_node *commit = git_pqueue_pop(&list); + int flags; + + if (commit == NULL) + break; + + flags = commit->flags & (PARENT1 | PARENT2 | STALE); + if (flags == (PARENT1 | PARENT2)) { + if (!(commit->flags & RESULT)) { + commit->flags |= RESULT; + if (git_commit_list_insert(commit, &result) == NULL) + return -1; + } + /* we mark the parents of a merge stale */ + flags |= STALE; + } + + for (i = 0; i < commit->out_degree; i++) { + git_commit_list_node *p = commit->parents[i]; + if ((p->flags & flags) == flags) + continue; + if (p->generation < minimum_generation) + continue; + + if ((error = git_commit_list_parse(walk, p)) < 0) + return error; + + p->flags |= flags; + if (git_pqueue_insert(&list, p) < 0) + return -1; + } + } + + git_pqueue_free(&list); + *out = result; + return 0; +} + +static int remove_redundant(git_revwalk *walk, git_vector *commits, uint32_t minimum_generation) +{ + git_vector work = GIT_VECTOR_INIT; + unsigned char *redundant; + unsigned int *filled_index; + unsigned int i, j; + int error = 0; + + redundant = git__calloc(commits->length, 1); + GIT_ERROR_CHECK_ALLOC(redundant); + filled_index = git__calloc((commits->length - 1), sizeof(unsigned int)); + GIT_ERROR_CHECK_ALLOC(filled_index); + + for (i = 0; i < commits->length; ++i) { + if ((error = git_commit_list_parse(walk, commits->contents[i])) < 0) + goto done; + } + + for (i = 0; i < commits->length; ++i) { + git_commit_list *common = NULL; + git_commit_list_node *commit = commits->contents[i]; + + if (redundant[i]) + continue; + + git_vector_clear(&work); + + for (j = 0; j < commits->length; j++) { + if (i == j || redundant[j]) + continue; + + filled_index[work.length] = j; + if ((error = git_vector_insert(&work, commits->contents[j])) < 0) + goto done; + } + + error = paint_down_to_common(&common, walk, commit, &work, minimum_generation); + if (error < 0) + goto done; + + if (commit->flags & PARENT2) + redundant[i] = 1; + + for (j = 0; j < work.length; j++) { + git_commit_list_node *w = work.contents[j]; + if (w->flags & PARENT1) + redundant[filled_index[j]] = 1; + } + + git_commit_list_free(&common); + + if ((error = clear_commit_marks(commit, ALL_FLAGS)) < 0 || + (error = clear_commit_marks_many(&work, ALL_FLAGS)) < 0) + goto done; + } + + for (i = 0; i < commits->length; ++i) { + if (redundant[i]) + commits->contents[i] = NULL; + } + +done: + git__free(redundant); + git__free(filled_index); + git_vector_dispose(&work); + return error; +} + +int git_merge__bases_many( + git_commit_list **out, + git_revwalk *walk, + git_commit_list_node *one, + git_vector *twos, + uint32_t minimum_generation) +{ + int error; + unsigned int i; + git_commit_list_node *two; + git_commit_list *result = NULL, *tmp = NULL; + + /* If there's only the one commit, there can be no merge bases */ + if (twos->length == 0) { + *out = NULL; + return 0; + } + + /* if the commit is repeated, we have a our merge base already */ + git_vector_foreach(twos, i, two) { + if (one == two) + return git_commit_list_insert(one, out) ? 0 : -1; + } + + if (git_commit_list_parse(walk, one) < 0) + return -1; + + error = paint_down_to_common(&result, walk, one, twos, minimum_generation); + if (error < 0) + return error; + + /* filter out any stale commits in the results */ + tmp = result; + result = NULL; + + while (tmp) { + git_commit_list_node *c = git_commit_list_pop(&tmp); + if (!(c->flags & STALE)) + if (git_commit_list_insert_by_date(c, &result) == NULL) + return -1; + } + + /* + * more than one merge base -- see if there are redundant merge + * bases and remove them + */ + if (result && result->next) { + git_vector redundant = GIT_VECTOR_INIT; + + while (result) + git_vector_insert(&redundant, git_commit_list_pop(&result)); + + if ((error = clear_commit_marks(one, ALL_FLAGS)) < 0 || + (error = clear_commit_marks_many(twos, ALL_FLAGS)) < 0 || + (error = remove_redundant(walk, &redundant, minimum_generation)) < 0) { + git_vector_dispose(&redundant); + return error; + } + + git_vector_foreach(&redundant, i, two) { + if (two != NULL) + git_commit_list_insert_by_date(two, &result); + } + + git_vector_dispose(&redundant); + } + + *out = result; + return 0; +} + +int git_repository_mergehead_foreach( + git_repository *repo, + git_repository_mergehead_foreach_cb cb, + void *payload) +{ + git_str merge_head_path = GIT_STR_INIT, merge_head_file = GIT_STR_INIT; + char *buffer, *line; + size_t line_num = 1; + git_oid oid; + int error = 0; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(cb); + + if ((error = git_str_joinpath(&merge_head_path, repo->gitdir, + GIT_MERGE_HEAD_FILE)) < 0) + return error; + + if ((error = git_futils_readbuffer(&merge_head_file, + git_str_cstr(&merge_head_path))) < 0) + goto cleanup; + + buffer = merge_head_file.ptr; + + while ((line = git__strsep(&buffer, "\n")) != NULL) { + if (strlen(line) != git_oid_hexsize(repo->oid_type)) { + git_error_set(GIT_ERROR_INVALID, "unable to parse OID - invalid length"); + error = -1; + goto cleanup; + } + + if ((error = git_oid_from_string(&oid, line, repo->oid_type)) < 0) + goto cleanup; + + if ((error = cb(&oid, payload)) != 0) { + git_error_set_after_callback(error); + goto cleanup; + } + + ++line_num; + } + + if (*buffer) { + git_error_set(GIT_ERROR_MERGE, "no EOL at line %"PRIuZ, line_num); + error = -1; + goto cleanup; + } + +cleanup: + git_str_dispose(&merge_head_path); + git_str_dispose(&merge_head_file); + + return error; +} + +GIT_INLINE(int) index_entry_cmp(const git_index_entry *a, const git_index_entry *b) +{ + int value = 0; + + if (a->path == NULL) + return (b->path == NULL) ? 0 : 1; + + if ((value = a->mode - b->mode) == 0 && + (value = git_oid__cmp(&a->id, &b->id)) == 0) + value = strcmp(a->path, b->path); + + return value; +} + +/* Conflict resolution */ + +static int merge_conflict_resolve_trivial( + int *resolved, + git_merge_diff_list *diff_list, + const git_merge_diff *conflict) +{ + int ours_empty, theirs_empty; + int ours_changed, theirs_changed, ours_theirs_differ; + git_index_entry const *result = NULL; + int error = 0; + + GIT_ASSERT_ARG(resolved); + GIT_ASSERT_ARG(diff_list); + GIT_ASSERT_ARG(conflict); + + *resolved = 0; + + if (conflict->type == GIT_MERGE_DIFF_DIRECTORY_FILE || + conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED) + return 0; + + if (conflict->our_status == GIT_DELTA_RENAMED || + conflict->their_status == GIT_DELTA_RENAMED) + return 0; + + ours_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry); + theirs_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry); + + ours_changed = (conflict->our_status != GIT_DELTA_UNMODIFIED); + theirs_changed = (conflict->their_status != GIT_DELTA_UNMODIFIED); + ours_theirs_differ = ours_changed && theirs_changed && + index_entry_cmp(&conflict->our_entry, &conflict->their_entry); + + /* + * Note: with only one ancestor, some cases are not distinct: + * + * 16: ancest:anc1/anc2, head:anc1, remote:anc2 = result:no merge + * 3: ancest:(empty)^, head:head, remote:(empty) = result:no merge + * 2: ancest:(empty)^, head:(empty), remote:remote = result:no merge + * + * Note that the two cases that take D/F conflicts into account + * specifically do not need to be explicitly tested, as D/F conflicts + * would fail the *empty* test: + * + * 3ALT: ancest:(empty)+, head:head, remote:*empty* = result:head + * 2ALT: ancest:(empty)+, head:*empty*, remote:remote = result:remote + * + * Note that many of these cases need not be explicitly tested, as + * they simply degrade to "all different" cases (eg, 11): + * + * 4: ancest:(empty)^, head:head, remote:remote = result:no merge + * 7: ancest:ancest+, head:(empty), remote:remote = result:no merge + * 9: ancest:ancest+, head:head, remote:(empty) = result:no merge + * 11: ancest:ancest+, head:head, remote:remote = result:no merge + */ + + /* 5ALT: ancest:*, head:head, remote:head = result:head */ + if (ours_changed && !ours_empty && !ours_theirs_differ) + result = &conflict->our_entry; + /* 6: ancest:ancest+, head:(empty), remote:(empty) = result:no merge */ + else if (ours_changed && ours_empty && theirs_empty) + *resolved = 0; + /* 8: ancest:ancest^, head:(empty), remote:ancest = result:no merge */ + else if (ours_empty && !theirs_changed) + *resolved = 0; + /* 10: ancest:ancest^, head:ancest, remote:(empty) = result:no merge */ + else if (!ours_changed && theirs_empty) + *resolved = 0; + /* 13: ancest:ancest+, head:head, remote:ancest = result:head */ + else if (ours_changed && !theirs_changed) + result = &conflict->our_entry; + /* 14: ancest:ancest+, head:ancest, remote:remote = result:remote */ + else if (!ours_changed && theirs_changed) + result = &conflict->their_entry; + else + *resolved = 0; + + if (result != NULL && + GIT_MERGE_INDEX_ENTRY_EXISTS(*result) && + (error = git_vector_insert(&diff_list->staged, (void *)result)) >= 0) + *resolved = 1; + + /* Note: trivial resolution does not update the REUC. */ + + return error; +} + +static int merge_conflict_resolve_one_removed( + int *resolved, + git_merge_diff_list *diff_list, + const git_merge_diff *conflict) +{ + int ours_empty, theirs_empty; + int ours_changed, theirs_changed; + int error = 0; + + GIT_ASSERT_ARG(resolved); + GIT_ASSERT_ARG(diff_list); + GIT_ASSERT_ARG(conflict); + + *resolved = 0; + + if (conflict->type == GIT_MERGE_DIFF_DIRECTORY_FILE || + conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED) + return 0; + + ours_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry); + theirs_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry); + + ours_changed = (conflict->our_status != GIT_DELTA_UNMODIFIED); + theirs_changed = (conflict->their_status != GIT_DELTA_UNMODIFIED); + + /* Removed in both */ + if (ours_changed && ours_empty && theirs_empty) + *resolved = 1; + /* Removed in ours */ + else if (ours_empty && !theirs_changed) + *resolved = 1; + /* Removed in theirs */ + else if (!ours_changed && theirs_empty) + *resolved = 1; + + if (*resolved) + git_vector_insert(&diff_list->resolved, (git_merge_diff *)conflict); + + return error; +} + +static int merge_conflict_resolve_one_renamed( + int *resolved, + git_merge_diff_list *diff_list, + const git_merge_diff *conflict) +{ + int ours_renamed, theirs_renamed; + int ours_changed, theirs_changed; + git_index_entry *merged; + int error = 0; + + GIT_ASSERT_ARG(resolved); + GIT_ASSERT_ARG(diff_list); + GIT_ASSERT_ARG(conflict); + + *resolved = 0; + + if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) || + !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry)) + return 0; + + ours_renamed = (conflict->our_status == GIT_DELTA_RENAMED); + theirs_renamed = (conflict->their_status == GIT_DELTA_RENAMED); + + if (!ours_renamed && !theirs_renamed) + return 0; + + /* Reject one file in a 2->1 conflict */ + if (conflict->type == GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1 || + conflict->type == GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2 || + conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED) + return 0; + + ours_changed = (git_oid__cmp(&conflict->ancestor_entry.id, &conflict->our_entry.id) != 0) || + (conflict->ancestor_entry.mode != conflict->our_entry.mode); + + theirs_changed = (git_oid__cmp(&conflict->ancestor_entry.id, &conflict->their_entry.id) != 0) || + (conflict->ancestor_entry.mode != conflict->their_entry.mode); + + /* if both are modified (and not to a common target) require a merge */ + if (ours_changed && theirs_changed && + git_oid__cmp(&conflict->our_entry.id, &conflict->their_entry.id) != 0) + return 0; + + if ((merged = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry))) == NULL) + return -1; + + if (ours_changed) + memcpy(merged, &conflict->our_entry, sizeof(git_index_entry)); + else + memcpy(merged, &conflict->their_entry, sizeof(git_index_entry)); + + if (ours_renamed) + merged->path = conflict->our_entry.path; + else + merged->path = conflict->their_entry.path; + + *resolved = 1; + + git_vector_insert(&diff_list->staged, merged); + git_vector_insert(&diff_list->resolved, (git_merge_diff *)conflict); + + return error; +} + +static bool merge_conflict_can_resolve_contents( + const git_merge_diff *conflict) +{ + if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) || + !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry)) + return false; + + /* Reject D/F conflicts */ + if (conflict->type == GIT_MERGE_DIFF_DIRECTORY_FILE) + return false; + + /* Reject submodules. */ + if (S_ISGITLINK(conflict->ancestor_entry.mode) || + S_ISGITLINK(conflict->our_entry.mode) || + S_ISGITLINK(conflict->their_entry.mode)) + return false; + + /* Reject link/file conflicts. */ + if ((S_ISLNK(conflict->ancestor_entry.mode) ^ + S_ISLNK(conflict->our_entry.mode)) || + (S_ISLNK(conflict->ancestor_entry.mode) ^ + S_ISLNK(conflict->their_entry.mode))) + return false; + + /* Reject name conflicts */ + if (conflict->type == GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1 || + conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED) + return false; + + if ((conflict->our_status & GIT_DELTA_RENAMED) == GIT_DELTA_RENAMED && + (conflict->their_status & GIT_DELTA_RENAMED) == GIT_DELTA_RENAMED && + strcmp(conflict->ancestor_entry.path, conflict->their_entry.path) != 0) + return false; + + return true; +} + +static int merge_conflict_invoke_driver( + git_index_entry **out, + const char *name, + git_merge_driver *driver, + git_merge_diff_list *diff_list, + git_merge_driver_source *src) +{ + git_index_entry *result; + git_buf buf = {0}; + const char *path; + uint32_t mode; + git_odb *odb = NULL; + git_oid oid; + int error; + + *out = NULL; + + if ((error = driver->apply(driver, &path, &mode, &buf, name, src)) < 0 || + (error = git_repository_odb(&odb, src->repo)) < 0 || + (error = git_odb_write(&oid, odb, buf.ptr, buf.size, GIT_OBJECT_BLOB)) < 0) + goto done; + + result = git_pool_mallocz(&diff_list->pool, sizeof(git_index_entry)); + GIT_ERROR_CHECK_ALLOC(result); + + git_oid_cpy(&result->id, &oid); + result->mode = mode; + result->file_size = (uint32_t)buf.size; + + result->path = git_pool_strdup(&diff_list->pool, path); + GIT_ERROR_CHECK_ALLOC(result->path); + + *out = result; + +done: + git_buf_dispose(&buf); + git_odb_free(odb); + + return error; +} + +static int merge_conflict_resolve_contents( + int *resolved, + git_merge_diff_list *diff_list, + const git_merge_diff *conflict, + const git_merge_options *merge_opts, + const git_merge_file_options *file_opts) +{ + git_merge_driver_source source = {0}; + git_merge_file_result result = {0}; + git_merge_driver *driver; + git_merge_driver__builtin builtin = {{0}}; + git_index_entry *merge_result = NULL; + git_odb *odb = NULL; + const char *name; + bool fallback = false; + int error; + + GIT_ASSERT_ARG(resolved); + GIT_ASSERT_ARG(diff_list); + GIT_ASSERT_ARG(conflict); + + *resolved = 0; + + if (!merge_conflict_can_resolve_contents(conflict)) + return 0; + + source.repo = diff_list->repo; + source.default_driver = merge_opts->default_driver; + source.file_opts = file_opts; + source.ancestor = GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry) ? + &conflict->ancestor_entry : NULL; + source.ours = GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ? + &conflict->our_entry : NULL; + source.theirs = GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry) ? + &conflict->their_entry : NULL; + + if (file_opts->favor != GIT_MERGE_FILE_FAVOR_NORMAL) { + /* if the user requested a particular type of resolution (via the + * favor flag) then let that override the gitattributes and use + * the builtin driver. + */ + name = "text"; + builtin.base.apply = git_merge_driver__builtin_apply; + builtin.favor = file_opts->favor; + + driver = &builtin.base; + } else { + /* find the merge driver for this file */ + if ((error = git_merge_driver_for_source(&name, &driver, &source)) < 0) + goto done; + + if (driver == NULL) + fallback = true; + } + + if (driver) { + error = merge_conflict_invoke_driver(&merge_result, name, driver, + diff_list, &source); + + if (error == GIT_PASSTHROUGH) + fallback = true; + } + + if (fallback) { + error = merge_conflict_invoke_driver(&merge_result, "text", + &git_merge_driver__text.base, diff_list, &source); + } + + if (error < 0) { + if (error == GIT_EMERGECONFLICT) + error = 0; + + goto done; + } + + git_vector_insert(&diff_list->staged, merge_result); + git_vector_insert(&diff_list->resolved, (git_merge_diff *)conflict); + + *resolved = 1; + +done: + git_merge_file_result_free(&result); + git_odb_free(odb); + + return error; +} + +static int merge_conflict_resolve( + int *out, + git_merge_diff_list *diff_list, + const git_merge_diff *conflict, + const git_merge_options *merge_opts, + const git_merge_file_options *file_opts) +{ + int resolved = 0; + int error = 0; + + *out = 0; + + if ((error = merge_conflict_resolve_trivial( + &resolved, diff_list, conflict)) < 0) + goto done; + + if (!resolved && (error = merge_conflict_resolve_one_removed( + &resolved, diff_list, conflict)) < 0) + goto done; + + if (!resolved && (error = merge_conflict_resolve_one_renamed( + &resolved, diff_list, conflict)) < 0) + goto done; + + if (!resolved && (error = merge_conflict_resolve_contents( + &resolved, diff_list, conflict, merge_opts, file_opts)) < 0) + goto done; + + *out = resolved; + +done: + return error; +} + +/* Rename detection and coalescing */ + +struct merge_diff_similarity { + unsigned char similarity; + size_t other_idx; +}; + +static int index_entry_similarity_calc( + void **out, + git_repository *repo, + git_index_entry *entry, + const git_merge_options *opts) +{ + git_blob *blob; + git_diff_file diff_file; + git_object_size_t blobsize; + int error; + + if (*out || *out == &cache_invalid_marker) + return 0; + + *out = NULL; + + git_oid_clear(&diff_file.id, repo->oid_type); + + if ((error = git_blob_lookup(&blob, repo, &entry->id)) < 0) + return error; + + git_oid_cpy(&diff_file.id, &entry->id); + diff_file.path = entry->path; + diff_file.size = entry->file_size; + diff_file.mode = entry->mode; + diff_file.flags = 0; + + blobsize = git_blob_rawsize(blob); + + /* file too big for rename processing */ + if (!git__is_sizet(blobsize)) + return 0; + + error = opts->metric->buffer_signature(out, &diff_file, + git_blob_rawcontent(blob), (size_t)blobsize, + opts->metric->payload); + if (error == GIT_EBUFS) + *out = &cache_invalid_marker; + + git_blob_free(blob); + + return error; +} + +static int index_entry_similarity_inexact( + git_repository *repo, + git_index_entry *a, + size_t a_idx, + git_index_entry *b, + size_t b_idx, + void **cache, + const git_merge_options *opts) +{ + int score = 0; + int error = 0; + + if (!GIT_MODE_ISBLOB(a->mode) || !GIT_MODE_ISBLOB(b->mode)) + return 0; + + /* update signature cache if needed */ + if ((error = index_entry_similarity_calc(&cache[a_idx], repo, a, opts)) < 0 || + (error = index_entry_similarity_calc(&cache[b_idx], repo, b, opts)) < 0) + return error; + + /* some metrics may not wish to process this file (too big / too small) */ + if (cache[a_idx] == &cache_invalid_marker || cache[b_idx] == &cache_invalid_marker) + return 0; + + /* compare signatures */ + if (opts->metric->similarity(&score, cache[a_idx], cache[b_idx], opts->metric->payload) < 0) + return -1; + + /* clip score */ + if (score < 0) + score = 0; + else if (score > 100) + score = 100; + + return score; +} + +/* Tracks deletes by oid for merge_diff_mark_similarity_exact(). This is a +* non-shrinking queue where next_pos is the next position to dequeue. +*/ +typedef struct { + git_array_t(size_t) arr; + size_t next_pos; + size_t first_entry; +} deletes_by_oid_queue; + +GIT_HASHMAP_OID_SETUP(git_merge_deletes_oidmap, deletes_by_oid_queue *); + +static void deletes_by_oid_dispose(git_merge_deletes_oidmap *map) +{ + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + deletes_by_oid_queue *queue; + + if (!map) + return; + + while (git_merge_deletes_oidmap_iterate(&iter, NULL, &queue, map) == 0) + git_array_clear(queue->arr); + + git_merge_deletes_oidmap_dispose(map); +} + +static int deletes_by_oid_enqueue(git_merge_deletes_oidmap *map, git_pool *pool, const git_oid *id, size_t idx) +{ + deletes_by_oid_queue *queue; + size_t *array_entry; + + if (git_merge_deletes_oidmap_get(&queue, map, id) != 0) { + queue = git_pool_malloc(pool, sizeof(deletes_by_oid_queue)); + GIT_ERROR_CHECK_ALLOC(queue); + + git_array_init(queue->arr); + queue->next_pos = 0; + queue->first_entry = idx; + + if (git_merge_deletes_oidmap_put(map, id, queue) < 0) + return -1; + } else { + array_entry = git_array_alloc(queue->arr); + GIT_ERROR_CHECK_ALLOC(array_entry); + *array_entry = idx; + } + + return 0; +} + +static int deletes_by_oid_dequeue(size_t *idx, git_merge_deletes_oidmap *map, const git_oid *id) +{ + deletes_by_oid_queue *queue; + size_t *array_entry; + int error; + + if ((error = git_merge_deletes_oidmap_get(&queue, map, id)) != 0) + return error; + + if (queue->next_pos == 0) { + *idx = queue->first_entry; + } else { + array_entry = git_array_get(queue->arr, queue->next_pos - 1); + if (array_entry == NULL) + return GIT_ENOTFOUND; + + *idx = *array_entry; + } + + queue->next_pos++; + return 0; +} + +static int merge_diff_mark_similarity_exact( + git_merge_diff_list *diff_list, + struct merge_diff_similarity *similarity_ours, + struct merge_diff_similarity *similarity_theirs) +{ + size_t i, j; + git_merge_diff *conflict_src, *conflict_tgt; + git_merge_deletes_oidmap ours_deletes_by_oid = GIT_HASHMAP_INIT, + theirs_deletes_by_oid = GIT_HASHMAP_INIT; + int error = 0; + + /* Build a map of object ids to conflicts */ + git_vector_foreach(&diff_list->conflicts, i, conflict_src) { + /* Items can be the source of a rename iff they have an item in the + * ancestor slot and lack an item in the ours or theirs slot. */ + if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->ancestor_entry)) + continue; + + /* + * Ignore empty files because it has always the same blob sha1 + * and will lead to incorrect matches between all entries. + */ + if (git_oid_equal(&conflict_src->ancestor_entry.id, &git_oid__empty_blob_sha1)) + continue; + + if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->our_entry)) { + error = deletes_by_oid_enqueue(&ours_deletes_by_oid, &diff_list->pool, &conflict_src->ancestor_entry.id, i); + if (error < 0) + goto done; + } + + if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->their_entry)) { + error = deletes_by_oid_enqueue(&theirs_deletes_by_oid, &diff_list->pool, &conflict_src->ancestor_entry.id, i); + if (error < 0) + goto done; + } + } + + git_vector_foreach(&diff_list->conflicts, j, conflict_tgt) { + if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->ancestor_entry)) + continue; + + if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->our_entry)) { + if (deletes_by_oid_dequeue(&i, &ours_deletes_by_oid, &conflict_tgt->our_entry.id) == 0) { + similarity_ours[i].similarity = 100; + similarity_ours[i].other_idx = j; + + similarity_ours[j].similarity = 100; + similarity_ours[j].other_idx = i; + } + } + + if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->their_entry)) { + if (deletes_by_oid_dequeue(&i, &theirs_deletes_by_oid, &conflict_tgt->their_entry.id) == 0) { + similarity_theirs[i].similarity = 100; + similarity_theirs[i].other_idx = j; + + similarity_theirs[j].similarity = 100; + similarity_theirs[j].other_idx = i; + } + } + } + +done: + deletes_by_oid_dispose(&ours_deletes_by_oid); + deletes_by_oid_dispose(&theirs_deletes_by_oid); + + return error; +} + +static int merge_diff_mark_similarity_inexact( + git_repository *repo, + git_merge_diff_list *diff_list, + struct merge_diff_similarity *similarity_ours, + struct merge_diff_similarity *similarity_theirs, + void **cache, + const git_merge_options *opts) +{ + size_t i, j; + git_merge_diff *conflict_src, *conflict_tgt; + int similarity; + + git_vector_foreach(&diff_list->conflicts, i, conflict_src) { + /* Items can be the source of a rename iff they have an item in the + * ancestor slot and lack an item in the ours or theirs slot. */ + if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->ancestor_entry) || + (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->our_entry) && + GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->their_entry))) + continue; + + git_vector_foreach(&diff_list->conflicts, j, conflict_tgt) { + size_t our_idx = diff_list->conflicts.length + j; + size_t their_idx = (diff_list->conflicts.length * 2) + j; + + if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->ancestor_entry)) + continue; + + if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->our_entry) && + !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->our_entry)) { + similarity = index_entry_similarity_inexact(repo, &conflict_src->ancestor_entry, i, &conflict_tgt->our_entry, our_idx, cache, opts); + + if (similarity == GIT_EBUFS) + continue; + else if (similarity < 0) + return similarity; + + if (similarity > similarity_ours[i].similarity && + similarity > similarity_ours[j].similarity) { + /* Clear previous best similarity */ + if (similarity_ours[i].similarity > 0) + similarity_ours[similarity_ours[i].other_idx].similarity = 0; + + if (similarity_ours[j].similarity > 0) + similarity_ours[similarity_ours[j].other_idx].similarity = 0; + + similarity_ours[i].similarity = similarity; + similarity_ours[i].other_idx = j; + + similarity_ours[j].similarity = similarity; + similarity_ours[j].other_idx = i; + } + } + + if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->their_entry) && + !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->their_entry)) { + similarity = index_entry_similarity_inexact(repo, &conflict_src->ancestor_entry, i, &conflict_tgt->their_entry, their_idx, cache, opts); + + if (similarity > similarity_theirs[i].similarity && + similarity > similarity_theirs[j].similarity) { + /* Clear previous best similarity */ + if (similarity_theirs[i].similarity > 0) + similarity_theirs[similarity_theirs[i].other_idx].similarity = 0; + + if (similarity_theirs[j].similarity > 0) + similarity_theirs[similarity_theirs[j].other_idx].similarity = 0; + + similarity_theirs[i].similarity = similarity; + similarity_theirs[i].other_idx = j; + + similarity_theirs[j].similarity = similarity; + similarity_theirs[j].other_idx = i; + } + } + } + } + + return 0; +} + +/* + * Rename conflicts: + * + * Ancestor Ours Theirs + * + * 0a A A A No rename + * b A A* A No rename (ours was rewritten) + * c A A A* No rename (theirs rewritten) + * 1a A A B[A] Rename or rename/edit + * b A B[A] A (automergeable) + * 2 A B[A] B[A] Both renamed (automergeable) + * 3a A B[A] Rename/delete + * b A B[A] (same) + * 4a A B[A] B Rename/add [B~ours B~theirs] + * b A B B[A] (same) + * 5 A B[A] C[A] Both renamed ("1 -> 2") + * 6 A C[A] Both renamed ("2 -> 1") + * B C[B] [C~ours C~theirs] (automergeable) + */ +static void merge_diff_mark_rename_conflict( + git_merge_diff_list *diff_list, + struct merge_diff_similarity *similarity_ours, + bool ours_renamed, + size_t ours_source_idx, + struct merge_diff_similarity *similarity_theirs, + bool theirs_renamed, + size_t theirs_source_idx, + git_merge_diff *target, + const git_merge_options *opts) +{ + git_merge_diff *ours_source = NULL, *theirs_source = NULL; + + if (ours_renamed) + ours_source = diff_list->conflicts.contents[ours_source_idx]; + + if (theirs_renamed) + theirs_source = diff_list->conflicts.contents[theirs_source_idx]; + + /* Detect 2->1 conflicts */ + if (ours_renamed && theirs_renamed) { + /* Both renamed to the same target name. */ + if (ours_source_idx == theirs_source_idx) + ours_source->type = GIT_MERGE_DIFF_BOTH_RENAMED; + else { + ours_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1; + theirs_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1; + } + } else if (ours_renamed) { + /* If our source was also renamed in theirs, this is a 1->2 */ + if (similarity_theirs[ours_source_idx].similarity >= opts->rename_threshold) + ours_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2; + + else if (GIT_MERGE_INDEX_ENTRY_EXISTS(target->their_entry)) { + ours_source->type = GIT_MERGE_DIFF_RENAMED_ADDED; + target->type = GIT_MERGE_DIFF_RENAMED_ADDED; + } + + else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(ours_source->their_entry)) + ours_source->type = GIT_MERGE_DIFF_RENAMED_DELETED; + + else if (ours_source->type == GIT_MERGE_DIFF_MODIFIED_DELETED) + ours_source->type = GIT_MERGE_DIFF_RENAMED_MODIFIED; + } else if (theirs_renamed) { + /* If their source was also renamed in ours, this is a 1->2 */ + if (similarity_ours[theirs_source_idx].similarity >= opts->rename_threshold) + theirs_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2; + + else if (GIT_MERGE_INDEX_ENTRY_EXISTS(target->our_entry)) { + theirs_source->type = GIT_MERGE_DIFF_RENAMED_ADDED; + target->type = GIT_MERGE_DIFF_RENAMED_ADDED; + } + + else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(theirs_source->our_entry)) + theirs_source->type = GIT_MERGE_DIFF_RENAMED_DELETED; + + else if (theirs_source->type == GIT_MERGE_DIFF_MODIFIED_DELETED) + theirs_source->type = GIT_MERGE_DIFF_RENAMED_MODIFIED; + } +} + +GIT_INLINE(void) merge_diff_coalesce_rename( + git_index_entry *source_entry, + git_delta_t *source_status, + git_index_entry *target_entry, + git_delta_t *target_status) +{ + /* Coalesce the rename target into the rename source. */ + memcpy(source_entry, target_entry, sizeof(git_index_entry)); + *source_status = GIT_DELTA_RENAMED; + + memset(target_entry, 0x0, sizeof(git_index_entry)); + *target_status = GIT_DELTA_UNMODIFIED; +} + +static void merge_diff_list_coalesce_renames( + git_merge_diff_list *diff_list, + struct merge_diff_similarity *similarity_ours, + struct merge_diff_similarity *similarity_theirs, + const git_merge_options *opts) +{ + size_t i; + bool ours_renamed = 0, theirs_renamed = 0; + size_t ours_source_idx = 0, theirs_source_idx = 0; + git_merge_diff *ours_source, *theirs_source, *target; + + for (i = 0; i < diff_list->conflicts.length; i++) { + target = diff_list->conflicts.contents[i]; + + ours_renamed = 0; + theirs_renamed = 0; + + if (GIT_MERGE_INDEX_ENTRY_EXISTS(target->our_entry) && + similarity_ours[i].similarity >= opts->rename_threshold) { + ours_source_idx = similarity_ours[i].other_idx; + + ours_source = diff_list->conflicts.contents[ours_source_idx]; + + merge_diff_coalesce_rename( + &ours_source->our_entry, + &ours_source->our_status, + &target->our_entry, + &target->our_status); + + similarity_ours[ours_source_idx].similarity = 0; + similarity_ours[i].similarity = 0; + + ours_renamed = 1; + } + + /* insufficient to determine direction */ + if (GIT_MERGE_INDEX_ENTRY_EXISTS(target->their_entry) && + similarity_theirs[i].similarity >= opts->rename_threshold) { + theirs_source_idx = similarity_theirs[i].other_idx; + + theirs_source = diff_list->conflicts.contents[theirs_source_idx]; + + merge_diff_coalesce_rename( + &theirs_source->their_entry, + &theirs_source->their_status, + &target->their_entry, + &target->their_status); + + similarity_theirs[theirs_source_idx].similarity = 0; + similarity_theirs[i].similarity = 0; + + theirs_renamed = 1; + } + + merge_diff_mark_rename_conflict(diff_list, + similarity_ours, ours_renamed, ours_source_idx, + similarity_theirs, theirs_renamed, theirs_source_idx, + target, opts); + } +} + +static int merge_diff_empty(const git_vector *conflicts, size_t idx, void *p) +{ + git_merge_diff *conflict = conflicts->contents[idx]; + + GIT_UNUSED(p); + + return (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry) && + !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) && + !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry)); +} + +static void merge_diff_list_count_candidates( + git_merge_diff_list *diff_list, + size_t *src_count, + size_t *tgt_count) +{ + git_merge_diff *entry; + size_t i; + + *src_count = 0; + *tgt_count = 0; + + git_vector_foreach(&diff_list->conflicts, i, entry) { + if (GIT_MERGE_INDEX_ENTRY_EXISTS(entry->ancestor_entry) && + (!GIT_MERGE_INDEX_ENTRY_EXISTS(entry->our_entry) || + !GIT_MERGE_INDEX_ENTRY_EXISTS(entry->their_entry))) + (*src_count)++; + else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(entry->ancestor_entry)) + (*tgt_count)++; + } +} + +int git_merge_diff_list__find_renames( + git_repository *repo, + git_merge_diff_list *diff_list, + const git_merge_options *opts) +{ + struct merge_diff_similarity *similarity_ours, *similarity_theirs; + void **cache = NULL; + size_t cache_size = 0; + size_t src_count, tgt_count, i; + int error = 0; + + GIT_ASSERT_ARG(diff_list); + GIT_ASSERT_ARG(opts); + + if ((opts->flags & GIT_MERGE_FIND_RENAMES) == 0 || + !diff_list->conflicts.length) + return 0; + + similarity_ours = git__calloc(diff_list->conflicts.length, + sizeof(struct merge_diff_similarity)); + GIT_ERROR_CHECK_ALLOC(similarity_ours); + + similarity_theirs = git__calloc(diff_list->conflicts.length, + sizeof(struct merge_diff_similarity)); + GIT_ERROR_CHECK_ALLOC(similarity_theirs); + + /* Calculate similarity between items that were deleted from the ancestor + * and added in the other branch. + */ + if ((error = merge_diff_mark_similarity_exact(diff_list, similarity_ours, similarity_theirs)) < 0) + goto done; + + if (opts->rename_threshold < 100 && diff_list->conflicts.length <= opts->target_limit) { + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&cache_size, diff_list->conflicts.length, 3); + cache = git__calloc(cache_size, sizeof(void *)); + GIT_ERROR_CHECK_ALLOC(cache); + + merge_diff_list_count_candidates(diff_list, &src_count, &tgt_count); + + if (src_count > opts->target_limit || tgt_count > opts->target_limit) { + /* TODO: report! */ + } else { + if ((error = merge_diff_mark_similarity_inexact( + repo, diff_list, similarity_ours, similarity_theirs, cache, opts)) < 0) + goto done; + } + } + + /* For entries that are appropriately similar, merge the new name's entry + * into the old name. + */ + merge_diff_list_coalesce_renames(diff_list, similarity_ours, similarity_theirs, opts); + + /* And remove any entries that were merged and are now empty. */ + git_vector_remove_matching(&diff_list->conflicts, merge_diff_empty, NULL); + +done: + if (cache != NULL) { + for (i = 0; i < cache_size; ++i) { + if (cache[i] != NULL && cache[i] != &cache_invalid_marker) + opts->metric->free_signature(cache[i], opts->metric->payload); + } + + git__free(cache); + } + + git__free(similarity_ours); + git__free(similarity_theirs); + + return error; +} + +/* Directory/file conflict handling */ + +GIT_INLINE(const char *) merge_diff_path( + const git_merge_diff *conflict) +{ + if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry)) + return conflict->ancestor_entry.path; + else if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry)) + return conflict->our_entry.path; + else if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry)) + return conflict->their_entry.path; + + return NULL; +} + +GIT_INLINE(bool) merge_diff_any_side_added_or_modified( + const git_merge_diff *conflict) +{ + if (conflict->our_status == GIT_DELTA_ADDED || + conflict->our_status == GIT_DELTA_MODIFIED || + conflict->their_status == GIT_DELTA_ADDED || + conflict->their_status == GIT_DELTA_MODIFIED) + return true; + + return false; +} + +GIT_INLINE(bool) path_is_prefixed(const char *parent, const char *child) +{ + size_t child_len = strlen(child); + size_t parent_len = strlen(parent); + + if (child_len < parent_len || + strncmp(parent, child, parent_len) != 0) + return 0; + + return (child[parent_len] == '/'); +} + +GIT_INLINE(int) merge_diff_detect_df_conflict( + struct merge_diff_df_data *df_data, + git_merge_diff *conflict) +{ + const char *cur_path = merge_diff_path(conflict); + + /* Determine if this is a D/F conflict or the child of one */ + if (df_data->df_path && + path_is_prefixed(df_data->df_path, cur_path)) + conflict->type = GIT_MERGE_DIFF_DF_CHILD; + else if(df_data->df_path) + df_data->df_path = NULL; + else if (df_data->prev_path && + merge_diff_any_side_added_or_modified(df_data->prev_conflict) && + merge_diff_any_side_added_or_modified(conflict) && + path_is_prefixed(df_data->prev_path, cur_path)) { + conflict->type = GIT_MERGE_DIFF_DF_CHILD; + + df_data->prev_conflict->type = GIT_MERGE_DIFF_DIRECTORY_FILE; + df_data->df_path = df_data->prev_path; + } + + df_data->prev_path = cur_path; + df_data->prev_conflict = conflict; + + return 0; +} + +/* Conflict handling */ + +GIT_INLINE(int) merge_diff_detect_type( + git_merge_diff *conflict) +{ + if (conflict->our_status == GIT_DELTA_ADDED && + conflict->their_status == GIT_DELTA_ADDED) + conflict->type = GIT_MERGE_DIFF_BOTH_ADDED; + else if (conflict->our_status == GIT_DELTA_MODIFIED && + conflict->their_status == GIT_DELTA_MODIFIED) + conflict->type = GIT_MERGE_DIFF_BOTH_MODIFIED; + else if (conflict->our_status == GIT_DELTA_DELETED && + conflict->their_status == GIT_DELTA_DELETED) + conflict->type = GIT_MERGE_DIFF_BOTH_DELETED; + else if (conflict->our_status == GIT_DELTA_MODIFIED && + conflict->their_status == GIT_DELTA_DELETED) + conflict->type = GIT_MERGE_DIFF_MODIFIED_DELETED; + else if (conflict->our_status == GIT_DELTA_DELETED && + conflict->their_status == GIT_DELTA_MODIFIED) + conflict->type = GIT_MERGE_DIFF_MODIFIED_DELETED; + else + conflict->type = GIT_MERGE_DIFF_NONE; + + return 0; +} + +GIT_INLINE(int) index_entry_dup_pool( + git_index_entry *out, + git_pool *pool, + const git_index_entry *src) +{ + if (src != NULL) { + memcpy(out, src, sizeof(git_index_entry)); + if ((out->path = git_pool_strdup(pool, src->path)) == NULL) + return -1; + } + + return 0; +} + +GIT_INLINE(int) merge_delta_type_from_index_entries( + const git_index_entry *ancestor, + const git_index_entry *other) +{ + if (ancestor == NULL && other == NULL) + return GIT_DELTA_UNMODIFIED; + else if (ancestor == NULL && other != NULL) + return GIT_DELTA_ADDED; + else if (ancestor != NULL && other == NULL) + return GIT_DELTA_DELETED; + else if (S_ISDIR(ancestor->mode) ^ S_ISDIR(other->mode)) + return GIT_DELTA_TYPECHANGE; + else if(S_ISLNK(ancestor->mode) ^ S_ISLNK(other->mode)) + return GIT_DELTA_TYPECHANGE; + else if (git_oid__cmp(&ancestor->id, &other->id) || + ancestor->mode != other->mode) + return GIT_DELTA_MODIFIED; + + return GIT_DELTA_UNMODIFIED; +} + +static git_merge_diff *merge_diff_from_index_entries( + git_merge_diff_list *diff_list, + const git_index_entry **entries) +{ + git_merge_diff *conflict; + git_pool *pool = &diff_list->pool; + + if ((conflict = git_pool_mallocz(pool, sizeof(git_merge_diff))) == NULL) + return NULL; + + if (index_entry_dup_pool(&conflict->ancestor_entry, pool, entries[TREE_IDX_ANCESTOR]) < 0 || + index_entry_dup_pool(&conflict->our_entry, pool, entries[TREE_IDX_OURS]) < 0 || + index_entry_dup_pool(&conflict->their_entry, pool, entries[TREE_IDX_THEIRS]) < 0) + return NULL; + + conflict->our_status = merge_delta_type_from_index_entries( + entries[TREE_IDX_ANCESTOR], entries[TREE_IDX_OURS]); + conflict->their_status = merge_delta_type_from_index_entries( + entries[TREE_IDX_ANCESTOR], entries[TREE_IDX_THEIRS]); + + return conflict; +} + +/* Merge trees */ + +static int merge_diff_list_insert_conflict( + git_merge_diff_list *diff_list, + struct merge_diff_df_data *merge_df_data, + const git_index_entry *tree_items[3]) +{ + git_merge_diff *conflict; + + if ((conflict = merge_diff_from_index_entries(diff_list, tree_items)) == NULL || + merge_diff_detect_type(conflict) < 0 || + merge_diff_detect_df_conflict(merge_df_data, conflict) < 0 || + git_vector_insert(&diff_list->conflicts, conflict) < 0) + return -1; + + return 0; +} + +static int merge_diff_list_insert_unmodified( + git_merge_diff_list *diff_list, + const git_index_entry *tree_items[3]) +{ + int error = 0; + git_index_entry *entry; + + entry = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry)); + GIT_ERROR_CHECK_ALLOC(entry); + + if ((error = index_entry_dup_pool(entry, &diff_list->pool, tree_items[0])) >= 0) + error = git_vector_insert(&diff_list->staged, entry); + + return error; +} + +struct merge_diff_find_data { + git_merge_diff_list *diff_list; + struct merge_diff_df_data df_data; +}; + +static int queue_difference(const git_index_entry **entries, void *data) +{ + struct merge_diff_find_data *find_data = data; + bool item_modified = false; + size_t i; + + if (!entries[0] || !entries[1] || !entries[2]) { + item_modified = true; + } else { + for (i = 1; i < 3; i++) { + if (index_entry_cmp(entries[0], entries[i]) != 0) { + item_modified = true; + break; + } + } + } + + return item_modified ? + merge_diff_list_insert_conflict( + find_data->diff_list, &find_data->df_data, entries) : + merge_diff_list_insert_unmodified(find_data->diff_list, entries); +} + +int git_merge_diff_list__find_differences( + git_merge_diff_list *diff_list, + git_iterator *ancestor_iter, + git_iterator *our_iter, + git_iterator *their_iter) +{ + git_iterator *iterators[3] = { ancestor_iter, our_iter, their_iter }; + struct merge_diff_find_data find_data = { diff_list }; + + return git_iterator_walk(iterators, 3, queue_difference, &find_data); +} + +git_merge_diff_list *git_merge_diff_list__alloc(git_repository *repo) +{ + git_merge_diff_list *diff_list = git__calloc(1, sizeof(git_merge_diff_list)); + + if (diff_list == NULL) + return NULL; + + diff_list->repo = repo; + + + if (git_pool_init(&diff_list->pool, 1) < 0 || + git_vector_init(&diff_list->staged, 0, NULL) < 0 || + git_vector_init(&diff_list->conflicts, 0, NULL) < 0 || + git_vector_init(&diff_list->resolved, 0, NULL) < 0) { + git_merge_diff_list__free(diff_list); + return NULL; + } + + return diff_list; +} + +void git_merge_diff_list__free(git_merge_diff_list *diff_list) +{ + if (!diff_list) + return; + + git_vector_dispose(&diff_list->staged); + git_vector_dispose(&diff_list->conflicts); + git_vector_dispose(&diff_list->resolved); + git_pool_clear(&diff_list->pool); + git__free(diff_list); +} + +static int merge_normalize_opts( + git_repository *repo, + git_merge_options *opts, + const git_merge_options *given) +{ + git_config *cfg = NULL; + git_config_entry *entry = NULL; + int error = 0; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(opts); + + if ((error = git_repository_config__weakptr(&cfg, repo)) < 0) + return error; + + if (given != NULL) { + memcpy(opts, given, sizeof(git_merge_options)); + } else { + git_merge_options init = GIT_MERGE_OPTIONS_INIT; + memcpy(opts, &init, sizeof(init)); + } + + if ((opts->flags & GIT_MERGE_FIND_RENAMES) && !opts->rename_threshold) + opts->rename_threshold = GIT_MERGE_DEFAULT_RENAME_THRESHOLD; + + if (given && given->default_driver) { + opts->default_driver = git__strdup(given->default_driver); + GIT_ERROR_CHECK_ALLOC(opts->default_driver); + } else { + error = git_config_get_entry(&entry, cfg, "merge.default"); + + if (error == 0) { + opts->default_driver = git__strdup(entry->value); + GIT_ERROR_CHECK_ALLOC(opts->default_driver); + } else if (error == GIT_ENOTFOUND) { + error = 0; + } else { + goto done; + } + } + + if (!opts->target_limit) { + int limit = git_config__get_int_force(cfg, "merge.renamelimit", 0); + + if (!limit) + limit = git_config__get_int_force(cfg, "diff.renamelimit", 0); + + opts->target_limit = (limit <= 0) ? + GIT_MERGE_DEFAULT_TARGET_LIMIT : (unsigned int)limit; + } + + /* assign the internal metric with whitespace flag as payload */ + if (!opts->metric) { + opts->metric = git__malloc(sizeof(git_diff_similarity_metric)); + GIT_ERROR_CHECK_ALLOC(opts->metric); + + opts->metric->file_signature = git_diff_find_similar__hashsig_for_file; + opts->metric->buffer_signature = git_diff_find_similar__hashsig_for_buf; + opts->metric->free_signature = git_diff_find_similar__hashsig_free; + opts->metric->similarity = git_diff_find_similar__calc_similarity; + opts->metric->payload = (void *)GIT_HASHSIG_SMART_WHITESPACE; + } + +done: + git_config_entry_free(entry); + return error; +} + + +static int merge_index_insert_reuc( + git_index *index, + size_t idx, + const git_index_entry *entry) +{ + const git_index_reuc_entry *reuc; + int mode[3] = { 0, 0, 0 }; + git_oid const *oid[3] = { NULL, NULL, NULL }; + size_t i; + + if (!GIT_MERGE_INDEX_ENTRY_EXISTS(*entry)) + return 0; + + if ((reuc = git_index_reuc_get_bypath(index, entry->path)) != NULL) { + for (i = 0; i < 3; i++) { + mode[i] = reuc->mode[i]; + oid[i] = &reuc->oid[i]; + } + } + + mode[idx] = entry->mode; + oid[idx] = &entry->id; + + return git_index_reuc_add(index, entry->path, + mode[0], oid[0], mode[1], oid[1], mode[2], oid[2]); +} + +static int index_update_reuc(git_index *index, git_merge_diff_list *diff_list) +{ + int error; + size_t i; + git_merge_diff *conflict; + + /* Add each entry in the resolved conflict to the REUC independently, since + * the paths may differ due to renames. */ + git_vector_foreach(&diff_list->resolved, i, conflict) { + const git_index_entry *ancestor = + GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry) ? + &conflict->ancestor_entry : NULL; + + const git_index_entry *ours = + GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ? + &conflict->our_entry : NULL; + + const git_index_entry *theirs = + GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry) ? + &conflict->their_entry : NULL; + + if (ancestor != NULL && + (error = merge_index_insert_reuc(index, TREE_IDX_ANCESTOR, ancestor)) < 0) + return error; + + if (ours != NULL && + (error = merge_index_insert_reuc(index, TREE_IDX_OURS, ours)) < 0) + return error; + + if (theirs != NULL && + (error = merge_index_insert_reuc(index, TREE_IDX_THEIRS, theirs)) < 0) + return error; + } + + return 0; +} + +static int index_from_diff_list( + git_index **out, + git_merge_diff_list *diff_list, + git_oid_t oid_type, + bool skip_reuc) +{ + git_index *index; + size_t i; + git_merge_diff *conflict; + git_index_options index_opts = GIT_INDEX_OPTIONS_INIT; + int error = 0; + + *out = NULL; + + index_opts.oid_type = oid_type; + + if ((error = git_index_new_ext(&index, &index_opts)) < 0) + return error; + + if ((error = git_index__fill(index, &diff_list->staged)) < 0) + goto on_error; + + git_vector_foreach(&diff_list->conflicts, i, conflict) { + const git_index_entry *ancestor = + GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry) ? + &conflict->ancestor_entry : NULL; + + const git_index_entry *ours = + GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ? + &conflict->our_entry : NULL; + + const git_index_entry *theirs = + GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry) ? + &conflict->their_entry : NULL; + + if ((error = git_index_conflict_add(index, ancestor, ours, theirs)) < 0) + goto on_error; + } + + /* Add each rename entry to the rename portion of the index. */ + git_vector_foreach(&diff_list->conflicts, i, conflict) { + const char *ancestor_path, *our_path, *their_path; + + if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry)) + continue; + + ancestor_path = conflict->ancestor_entry.path; + + our_path = + GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ? + conflict->our_entry.path : NULL; + + their_path = + GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry) ? + conflict->their_entry.path : NULL; + + if ((our_path && strcmp(ancestor_path, our_path) != 0) || + (their_path && strcmp(ancestor_path, their_path) != 0)) { + if ((error = git_index_name_add(index, ancestor_path, our_path, their_path)) < 0) + goto on_error; + } + } + + if (!skip_reuc) { + if ((error = index_update_reuc(index, diff_list)) < 0) + goto on_error; + } + + *out = index; + return 0; + +on_error: + git_index_free(index); + return error; +} + +static git_iterator *iterator_given_or_empty(git_iterator **empty, git_iterator *given) +{ + git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT; + + if (given) + return given; + + opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + if (git_iterator_for_nothing(empty, &opts) < 0) + return NULL; + + return *empty; +} + +int git_merge__iterators( + git_index **out, + git_repository *repo, + git_iterator *ancestor_iter, + git_iterator *our_iter, + git_iterator *theirs_iter, + const git_merge_options *given_opts) +{ + git_iterator *empty_ancestor = NULL, + *empty_ours = NULL, + *empty_theirs = NULL; + git_merge_diff_list *diff_list; + git_merge_options opts; + git_merge_file_options file_opts = GIT_MERGE_FILE_OPTIONS_INIT; + git_merge_diff *conflict; + git_vector changes; + size_t i; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + *out = NULL; + + GIT_ERROR_CHECK_VERSION( + given_opts, GIT_MERGE_OPTIONS_VERSION, "git_merge_options"); + + if ((error = merge_normalize_opts(repo, &opts, given_opts)) < 0) + return error; + + file_opts.favor = opts.file_favor; + file_opts.flags = opts.file_flags; + + /* use the git-inspired labels when virtual base building */ + if (opts.flags & GIT_MERGE_VIRTUAL_BASE) { + file_opts.ancestor_label = "merged common ancestors"; + file_opts.our_label = "Temporary merge branch 1"; + file_opts.their_label = "Temporary merge branch 2"; + file_opts.flags |= GIT_MERGE_FILE_ACCEPT_CONFLICTS; + file_opts.marker_size = GIT_MERGE_CONFLICT_MARKER_SIZE + 2; + } + + diff_list = git_merge_diff_list__alloc(repo); + GIT_ERROR_CHECK_ALLOC(diff_list); + + ancestor_iter = iterator_given_or_empty(&empty_ancestor, ancestor_iter); + our_iter = iterator_given_or_empty(&empty_ours, our_iter); + theirs_iter = iterator_given_or_empty(&empty_theirs, theirs_iter); + + if ((error = git_merge_diff_list__find_differences( + diff_list, ancestor_iter, our_iter, theirs_iter)) < 0 || + (error = git_merge_diff_list__find_renames(repo, diff_list, &opts)) < 0) + goto done; + + memcpy(&changes, &diff_list->conflicts, sizeof(git_vector)); + git_vector_clear(&diff_list->conflicts); + + git_vector_foreach(&changes, i, conflict) { + int resolved = 0; + + if ((error = merge_conflict_resolve( + &resolved, diff_list, conflict, &opts, &file_opts)) < 0) + goto done; + + if (!resolved) { + if ((opts.flags & GIT_MERGE_FAIL_ON_CONFLICT)) { + git_error_set(GIT_ERROR_MERGE, "merge conflicts exist"); + error = GIT_EMERGECONFLICT; + goto done; + } + + git_vector_insert(&diff_list->conflicts, conflict); + } + } + + error = index_from_diff_list(out, diff_list, repo->oid_type, + (opts.flags & GIT_MERGE_SKIP_REUC)); + +done: + if (!given_opts || !given_opts->metric) + git__free(opts.metric); + + git__free((char *)opts.default_driver); + + git_merge_diff_list__free(diff_list); + git_iterator_free(empty_ancestor); + git_iterator_free(empty_ours); + git_iterator_free(empty_theirs); + + return error; +} + +int git_merge_trees( + git_index **out, + git_repository *repo, + const git_tree *ancestor_tree, + const git_tree *our_tree, + const git_tree *their_tree, + const git_merge_options *merge_opts) +{ + git_iterator *ancestor_iter = NULL, *our_iter = NULL, *their_iter = NULL; + git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index_options index_opts = GIT_INDEX_OPTIONS_FOR_REPO(repo); + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + /* if one side is treesame to the ancestor, take the other side */ + if (ancestor_tree && merge_opts && (merge_opts->flags & GIT_MERGE_SKIP_REUC)) { + const git_tree *result = NULL; + const git_oid *ancestor_tree_id = git_tree_id(ancestor_tree); + + if (our_tree && !git_oid_cmp(ancestor_tree_id, git_tree_id(our_tree))) + result = their_tree; + else if (their_tree && !git_oid_cmp(ancestor_tree_id, git_tree_id(their_tree))) + result = our_tree; + + if (result) { + if ((error = git_index_new_ext(out, &index_opts)) == 0) + error = git_index_read_tree(*out, result); + + return error; + } + } + + iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + if ((error = git_iterator_for_tree( + &ancestor_iter, (git_tree *)ancestor_tree, &iter_opts)) < 0 || + (error = git_iterator_for_tree( + &our_iter, (git_tree *)our_tree, &iter_opts)) < 0 || + (error = git_iterator_for_tree( + &their_iter, (git_tree *)their_tree, &iter_opts)) < 0) + goto done; + + error = git_merge__iterators( + out, repo, ancestor_iter, our_iter, their_iter, merge_opts); + +done: + git_iterator_free(ancestor_iter); + git_iterator_free(our_iter); + git_iterator_free(their_iter); + + return error; +} + +static int merge_annotated_commits( + git_index **index_out, + git_annotated_commit **base_out, + git_repository *repo, + git_annotated_commit *our_commit, + git_annotated_commit *their_commit, + size_t recursion_level, + const git_merge_options *opts); + +GIT_INLINE(int) insert_head_ids( + git_array_oid_t *ids, + const git_annotated_commit *annotated_commit) +{ + git_oid *id; + size_t i; + + if (annotated_commit->type == GIT_ANNOTATED_COMMIT_REAL) { + id = git_array_alloc(*ids); + GIT_ERROR_CHECK_ALLOC(id); + + git_oid_cpy(id, git_commit_id(annotated_commit->commit)); + } else { + for (i = 0; i < annotated_commit->parents.size; i++) { + id = git_array_alloc(*ids); + GIT_ERROR_CHECK_ALLOC(id); + + git_oid_cpy(id, &annotated_commit->parents.ptr[i]); + } + } + + return 0; +} + +static int create_virtual_base( + git_annotated_commit **out, + git_repository *repo, + git_annotated_commit *one, + git_annotated_commit *two, + const git_merge_options *opts, + size_t recursion_level) +{ + git_annotated_commit *result = NULL; + git_index *index = NULL; + git_merge_options virtual_opts = GIT_MERGE_OPTIONS_INIT; + + /* Conflicts in the merge base creation do not propagate to conflicts + * in the result; the conflicted base will act as the common ancestor. + */ + if (opts) + memcpy(&virtual_opts, opts, sizeof(git_merge_options)); + + virtual_opts.flags &= ~GIT_MERGE_FAIL_ON_CONFLICT; + virtual_opts.flags |= GIT_MERGE_VIRTUAL_BASE; + + if ((merge_annotated_commits(&index, NULL, repo, one, two, + recursion_level + 1, &virtual_opts)) < 0) + return -1; + + result = git__calloc(1, sizeof(git_annotated_commit)); + GIT_ERROR_CHECK_ALLOC(result); + result->type = GIT_ANNOTATED_COMMIT_VIRTUAL; + result->index = index; + + if (insert_head_ids(&result->parents, one) < 0 || + insert_head_ids(&result->parents, two) < 0) { + git_annotated_commit_free(result); + return -1; + } + + *out = result; + return 0; +} + +static int compute_base( + git_annotated_commit **out, + git_repository *repo, + const git_annotated_commit *one, + const git_annotated_commit *two, + const git_merge_options *given_opts, + size_t recursion_level) +{ + git_array_oid_t head_ids = GIT_ARRAY_INIT; + git_oidarray bases = {0}; + git_annotated_commit *base = NULL, *other = NULL, *new_base = NULL; + git_merge_options opts = GIT_MERGE_OPTIONS_INIT; + size_t i, base_count; + int error; + + *out = NULL; + + if (given_opts) + memcpy(&opts, given_opts, sizeof(git_merge_options)); + + /* With more than two commits, merge_bases_many finds the base of + * the first commit and a hypothetical merge of the others. Since + * "one" may itself be a virtual commit, which insert_head_ids + * substitutes multiple ancestors for, it needs to be added + * after "two" which is always a single real commit. + */ + if ((error = insert_head_ids(&head_ids, two)) < 0 || + (error = insert_head_ids(&head_ids, one)) < 0 || + (error = git_merge_bases_many(&bases, repo, + head_ids.size, head_ids.ptr)) < 0) + goto done; + + base_count = (opts.flags & GIT_MERGE_NO_RECURSIVE) ? 0 : bases.count; + + if (base_count) + git_oidarray__reverse(&bases); + + if ((error = git_annotated_commit_lookup(&base, repo, &bases.ids[0])) < 0) + goto done; + + for (i = 1; i < base_count; i++) { + recursion_level++; + + if (opts.recursion_limit && recursion_level > opts.recursion_limit) + break; + + if ((error = git_annotated_commit_lookup(&other, repo, + &bases.ids[i])) < 0 || + (error = create_virtual_base(&new_base, repo, base, other, &opts, + recursion_level)) < 0) + goto done; + + git_annotated_commit_free(base); + git_annotated_commit_free(other); + + base = new_base; + new_base = NULL; + other = NULL; + } + +done: + if (error == 0) + *out = base; + else + git_annotated_commit_free(base); + + git_annotated_commit_free(other); + git_annotated_commit_free(new_base); + git_oidarray_dispose(&bases); + git_array_clear(head_ids); + return error; +} + +static int iterator_for_annotated_commit( + git_iterator **out, + git_annotated_commit *commit) +{ + git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT; + int error; + + opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + if (commit == NULL) { + error = git_iterator_for_nothing(out, &opts); + } else if (commit->type == GIT_ANNOTATED_COMMIT_VIRTUAL) { + error = git_iterator_for_index(out, git_index_owner(commit->index), commit->index, &opts); + } else { + if (!commit->tree && + (error = git_commit_tree(&commit->tree, commit->commit)) < 0) + goto done; + + error = git_iterator_for_tree(out, commit->tree, &opts); + } + +done: + return error; +} + +static int merge_annotated_commits( + git_index **index_out, + git_annotated_commit **base_out, + git_repository *repo, + git_annotated_commit *ours, + git_annotated_commit *theirs, + size_t recursion_level, + const git_merge_options *opts) +{ + git_annotated_commit *base = NULL; + git_iterator *base_iter = NULL, *our_iter = NULL, *their_iter = NULL; + int error; + + if ((error = compute_base(&base, repo, ours, theirs, opts, + recursion_level)) < 0) { + + if (error != GIT_ENOTFOUND) + goto done; + + git_error_clear(); + } + + if ((error = iterator_for_annotated_commit(&base_iter, base)) < 0 || + (error = iterator_for_annotated_commit(&our_iter, ours)) < 0 || + (error = iterator_for_annotated_commit(&their_iter, theirs)) < 0 || + (error = git_merge__iterators(index_out, repo, base_iter, our_iter, + their_iter, opts)) < 0) + goto done; + + if (base_out) { + *base_out = base; + base = NULL; + } + +done: + git_annotated_commit_free(base); + git_iterator_free(base_iter); + git_iterator_free(our_iter); + git_iterator_free(their_iter); + return error; +} + + +int git_merge_commits( + git_index **out, + git_repository *repo, + const git_commit *our_commit, + const git_commit *their_commit, + const git_merge_options *opts) +{ + git_annotated_commit *ours = NULL, *theirs = NULL, *base = NULL; + int error = 0; + + if ((error = git_annotated_commit_from_commit(&ours, (git_commit *)our_commit)) < 0 || + (error = git_annotated_commit_from_commit(&theirs, (git_commit *)their_commit)) < 0) + goto done; + + error = merge_annotated_commits(out, &base, repo, ours, theirs, 0, opts); + +done: + git_annotated_commit_free(ours); + git_annotated_commit_free(theirs); + git_annotated_commit_free(base); + return error; +} + +/* Merge setup / cleanup */ + +static int write_merge_head( + git_repository *repo, + const git_annotated_commit *heads[], + size_t heads_len) +{ + git_filebuf file = GIT_FILEBUF_INIT; + git_str file_path = GIT_STR_INIT; + size_t i; + int error = 0; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(heads); + + if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_MERGE_HEAD_FILE)) < 0 || + (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_MERGE_FILE_MODE)) < 0) + goto cleanup; + + for (i = 0; i < heads_len; i++) { + if ((error = git_filebuf_printf(&file, "%s\n", heads[i]->id_str)) < 0) + goto cleanup; + } + + error = git_filebuf_commit(&file); + +cleanup: + if (error < 0) + git_filebuf_cleanup(&file); + + git_str_dispose(&file_path); + + return error; +} + +static int write_merge_mode(git_repository *repo) +{ + git_filebuf file = GIT_FILEBUF_INIT; + git_str file_path = GIT_STR_INIT; + int error = 0; + + GIT_ASSERT_ARG(repo); + + if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_MERGE_MODE_FILE)) < 0 || + (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_MERGE_FILE_MODE)) < 0) + goto cleanup; + + if ((error = git_filebuf_write(&file, "no-ff", 5)) < 0) + goto cleanup; + + error = git_filebuf_commit(&file); + +cleanup: + if (error < 0) + git_filebuf_cleanup(&file); + + git_str_dispose(&file_path); + + return error; +} + +struct merge_msg_entry { + const git_annotated_commit *merge_head; + bool written; +}; + +static int msg_entry_is_branch( + const struct merge_msg_entry *entry, + git_vector *entries) +{ + GIT_UNUSED(entries); + + return (entry->written == 0 && + entry->merge_head->remote_url == NULL && + entry->merge_head->ref_name != NULL && + git__strncmp(GIT_REFS_HEADS_DIR, entry->merge_head->ref_name, strlen(GIT_REFS_HEADS_DIR)) == 0); +} + +static int msg_entry_is_tracking( + const struct merge_msg_entry *entry, + git_vector *entries) +{ + GIT_UNUSED(entries); + + return (entry->written == 0 && + entry->merge_head->remote_url == NULL && + entry->merge_head->ref_name != NULL && + git__strncmp(GIT_REFS_REMOTES_DIR, entry->merge_head->ref_name, strlen(GIT_REFS_REMOTES_DIR)) == 0); +} + +static int msg_entry_is_tag( + const struct merge_msg_entry *entry, + git_vector *entries) +{ + GIT_UNUSED(entries); + + return (entry->written == 0 && + entry->merge_head->remote_url == NULL && + entry->merge_head->ref_name != NULL && + git__strncmp(GIT_REFS_TAGS_DIR, entry->merge_head->ref_name, strlen(GIT_REFS_TAGS_DIR)) == 0); +} + +static int msg_entry_is_remote( + const struct merge_msg_entry *entry, + git_vector *entries) +{ + if (entry->written == 0 && + entry->merge_head->remote_url != NULL && + entry->merge_head->ref_name != NULL && + git__strncmp(GIT_REFS_HEADS_DIR, entry->merge_head->ref_name, strlen(GIT_REFS_HEADS_DIR)) == 0) + { + struct merge_msg_entry *existing; + + /* Match only branches from the same remote */ + if (entries->length == 0) + return 1; + + existing = git_vector_get(entries, 0); + + return (git__strcmp(existing->merge_head->remote_url, + entry->merge_head->remote_url) == 0); + } + + return 0; +} + +static int msg_entry_is_oid( + const struct merge_msg_entry *merge_msg_entry) +{ + return (merge_msg_entry->written == 0 && + merge_msg_entry->merge_head->ref_name == NULL && + merge_msg_entry->merge_head->remote_url == NULL); +} + +static int merge_msg_entry_written( + const struct merge_msg_entry *merge_msg_entry) +{ + return (merge_msg_entry->written == 1); +} + +static int merge_msg_entries( + git_vector *v, + const struct merge_msg_entry *entries, + size_t len, + int (*match)(const struct merge_msg_entry *entry, git_vector *entries)) +{ + size_t i; + int matches, total = 0; + + git_vector_clear(v); + + for (i = 0; i < len; i++) { + if ((matches = match(&entries[i], v)) < 0) + return matches; + else if (!matches) + continue; + + git_vector_insert(v, (struct merge_msg_entry *)&entries[i]); + total++; + } + + return total; +} + +static int merge_msg_write_entries( + git_filebuf *file, + git_vector *entries, + const char *item_name, + const char *item_plural_name, + size_t ref_name_skip, + const char *source, + char sep) +{ + struct merge_msg_entry *entry; + size_t i; + int error = 0; + + if (entries->length == 0) + return 0; + + if (sep && (error = git_filebuf_printf(file, "%c ", sep)) < 0) + goto done; + + if ((error = git_filebuf_printf(file, "%s ", + (entries->length == 1) ? item_name : item_plural_name)) < 0) + goto done; + + git_vector_foreach(entries, i, entry) { + if (i > 0 && + (error = git_filebuf_printf(file, "%s", (i == entries->length - 1) ? " and " : ", ")) < 0) + goto done; + + if ((error = git_filebuf_printf(file, "'%s'", entry->merge_head->ref_name + ref_name_skip)) < 0) + goto done; + + entry->written = 1; + } + + if (source) + error = git_filebuf_printf(file, " of %s", source); + +done: + return error; +} + +static int merge_msg_write_branches( + git_filebuf *file, + git_vector *entries, + char sep) +{ + return merge_msg_write_entries(file, entries, + "branch", "branches", strlen(GIT_REFS_HEADS_DIR), NULL, sep); +} + +static int merge_msg_write_tracking( + git_filebuf *file, + git_vector *entries, + char sep) +{ + return merge_msg_write_entries(file, entries, + "remote-tracking branch", "remote-tracking branches", 0, NULL, sep); +} + +static int merge_msg_write_tags( + git_filebuf *file, + git_vector *entries, + char sep) +{ + return merge_msg_write_entries(file, entries, + "tag", "tags", strlen(GIT_REFS_TAGS_DIR), NULL, sep); +} + +static int merge_msg_write_remotes( + git_filebuf *file, + git_vector *entries, + char sep) +{ + const char *source; + + if (entries->length == 0) + return 0; + + source = ((struct merge_msg_entry *)entries->contents[0])->merge_head->remote_url; + + return merge_msg_write_entries(file, entries, + "branch", "branches", strlen(GIT_REFS_HEADS_DIR), source, sep); +} + +static int write_merge_msg( + git_repository *repo, + const git_annotated_commit *heads[], + size_t heads_len) +{ + git_filebuf file = GIT_FILEBUF_INIT; + git_str file_path = GIT_STR_INIT; + struct merge_msg_entry *entries; + git_vector matching = GIT_VECTOR_INIT; + size_t i; + char sep = 0; + int error = 0; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(heads); + + entries = git__calloc(heads_len, sizeof(struct merge_msg_entry)); + GIT_ERROR_CHECK_ALLOC(entries); + + if (git_vector_init(&matching, heads_len, NULL) < 0) { + git__free(entries); + return -1; + } + + for (i = 0; i < heads_len; i++) + entries[i].merge_head = heads[i]; + + if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 || + (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_MERGE_FILE_MODE)) < 0 || + (error = git_filebuf_write(&file, "Merge ", 6)) < 0) + goto cleanup; + + /* + * This is to emulate the format of MERGE_MSG by core git. + * + * Core git will write all the commits specified by OID, in the order + * provided, until the first named branch or tag is reached, at which + * point all branches will be written in the order provided, then all + * tags, then all remote tracking branches and finally all commits that + * were specified by OID that were not already written. + * + * Yes. Really. + */ + for (i = 0; i < heads_len; i++) { + if (!msg_entry_is_oid(&entries[i])) + break; + + if ((error = git_filebuf_printf(&file, + "%scommit '%s'", (i > 0) ? "; " : "", + entries[i].merge_head->id_str)) < 0) + goto cleanup; + + entries[i].written = 1; + } + + if (i) + sep = ';'; + + if ((error = merge_msg_entries(&matching, entries, heads_len, msg_entry_is_branch)) < 0 || + (error = merge_msg_write_branches(&file, &matching, sep)) < 0) + goto cleanup; + + if (matching.length) + sep =','; + + if ((error = merge_msg_entries(&matching, entries, heads_len, msg_entry_is_tracking)) < 0 || + (error = merge_msg_write_tracking(&file, &matching, sep)) < 0) + goto cleanup; + + if (matching.length) + sep =','; + + if ((error = merge_msg_entries(&matching, entries, heads_len, msg_entry_is_tag)) < 0 || + (error = merge_msg_write_tags(&file, &matching, sep)) < 0) + goto cleanup; + + if (matching.length) + sep =','; + + /* We should never be called with multiple remote branches, but handle + * it in case we are... */ + while ((error = merge_msg_entries(&matching, entries, heads_len, msg_entry_is_remote)) > 0) { + if ((error = merge_msg_write_remotes(&file, &matching, sep)) < 0) + goto cleanup; + + if (matching.length) + sep =','; + } + + if (error < 0) + goto cleanup; + + for (i = 0; i < heads_len; i++) { + if (merge_msg_entry_written(&entries[i])) + continue; + + if ((error = git_filebuf_printf(&file, "; commit '%s'", + entries[i].merge_head->id_str)) < 0) + goto cleanup; + } + + if ((error = git_filebuf_printf(&file, "\n")) < 0 || + (error = git_filebuf_commit(&file)) < 0) + goto cleanup; + +cleanup: + if (error < 0) + git_filebuf_cleanup(&file); + + git_str_dispose(&file_path); + + git_vector_dispose(&matching); + git__free(entries); + + return error; +} + +int git_merge__setup( + git_repository *repo, + const git_annotated_commit *our_head, + const git_annotated_commit *heads[], + size_t heads_len) +{ + int error = 0; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(our_head); + GIT_ASSERT_ARG(heads); + + if ((error = git_repository__set_orig_head(repo, git_annotated_commit_id(our_head))) == 0 && + (error = write_merge_head(repo, heads, heads_len)) == 0 && + (error = write_merge_mode(repo)) == 0) { + error = write_merge_msg(repo, heads, heads_len); + } + + return error; +} + +/* Merge branches */ + +static int merge_ancestor_head( + git_annotated_commit **ancestor_head, + git_repository *repo, + const git_annotated_commit *our_head, + const git_annotated_commit **their_heads, + size_t their_heads_len) +{ + git_oid *oids, ancestor_oid; + size_t i, alloc_len; + int error = 0; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(our_head); + GIT_ASSERT_ARG(their_heads); + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, their_heads_len, 1); + oids = git__calloc(alloc_len, sizeof(git_oid)); + GIT_ERROR_CHECK_ALLOC(oids); + + git_oid_cpy(&oids[0], git_commit_id(our_head->commit)); + + for (i = 0; i < their_heads_len; i++) + git_oid_cpy(&oids[i + 1], git_annotated_commit_id(their_heads[i])); + + if ((error = git_merge_base_many(&ancestor_oid, repo, their_heads_len + 1, oids)) < 0) + goto on_error; + + error = git_annotated_commit_lookup(ancestor_head, repo, &ancestor_oid); + +on_error: + git__free(oids); + return error; +} + +static const char *merge_their_label(const char *branchname) +{ + const char *slash; + + if ((slash = strrchr(branchname, '/')) == NULL) + return branchname; + + if (*(slash+1) == '\0') + return "theirs"; + + return slash+1; +} + +static int merge_normalize_checkout_opts( + git_checkout_options *out, + git_repository *repo, + const git_checkout_options *given_checkout_opts, + unsigned int checkout_strategy, + git_annotated_commit *ancestor, + const git_annotated_commit *our_head, + const git_annotated_commit **their_heads, + size_t their_heads_len) +{ + git_checkout_options default_checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; + int error = 0; + + GIT_UNUSED(repo); + + if (given_checkout_opts != NULL) + memcpy(out, given_checkout_opts, sizeof(git_checkout_options)); + else + memcpy(out, &default_checkout_opts, sizeof(git_checkout_options)); + + out->checkout_strategy = checkout_strategy; + + if (!out->ancestor_label) { + if (ancestor && ancestor->type == GIT_ANNOTATED_COMMIT_REAL) + out->ancestor_label = git_commit_summary(ancestor->commit); + else if (ancestor) + out->ancestor_label = "merged common ancestors"; + else + out->ancestor_label = "empty base"; + } + + if (!out->our_label) { + if (our_head && our_head->ref_name) + out->our_label = our_head->ref_name; + else + out->our_label = "ours"; + } + + if (!out->their_label) { + if (their_heads_len == 1 && their_heads[0]->ref_name) + out->their_label = merge_their_label(their_heads[0]->ref_name); + else if (their_heads_len == 1) + out->their_label = their_heads[0]->id_str; + else + out->their_label = "theirs"; + } + + return error; +} + +static int merge_check_index(size_t *conflicts, git_repository *repo, git_index *index_new, git_vector *merged_paths) +{ + git_tree *head_tree = NULL; + git_index *index_repo = NULL; + git_iterator *iter_repo = NULL, *iter_new = NULL; + git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; + git_diff *staged_diff_list = NULL, *index_diff_list = NULL; + git_diff_delta *delta; + git_diff_options opts = GIT_DIFF_OPTIONS_INIT; + git_vector staged_paths = GIT_VECTOR_INIT; + size_t i; + int error = 0; + + GIT_UNUSED(merged_paths); + + *conflicts = 0; + + /* No staged changes may exist unless the change staged is identical to + * the result of the merge. This allows one to apply to merge manually, + * then run merge. Any other staged change would be overwritten by + * a reset merge. + */ + if ((error = git_repository_head_tree(&head_tree, repo)) < 0 || + (error = git_repository_index(&index_repo, repo)) < 0 || + (error = git_diff_tree_to_index(&staged_diff_list, repo, head_tree, index_repo, &opts)) < 0) + goto done; + + if (staged_diff_list->deltas.length == 0) + goto done; + + git_vector_foreach(&staged_diff_list->deltas, i, delta) { + if ((error = git_vector_insert(&staged_paths, (char *)delta->new_file.path)) < 0) + goto done; + } + + iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + iter_opts.pathlist.strings = (char **)staged_paths.contents; + iter_opts.pathlist.count = staged_paths.length; + + if ((error = git_iterator_for_index(&iter_repo, repo, index_repo, &iter_opts)) < 0 || + (error = git_iterator_for_index(&iter_new, repo, index_new, &iter_opts)) < 0 || + (error = git_diff__from_iterators(&index_diff_list, repo, iter_repo, iter_new, &opts)) < 0) + goto done; + + *conflicts = index_diff_list->deltas.length; + +done: + git_tree_free(head_tree); + git_index_free(index_repo); + git_iterator_free(iter_repo); + git_iterator_free(iter_new); + git_diff_free(staged_diff_list); + git_diff_free(index_diff_list); + git_vector_dispose(&staged_paths); + + return error; +} + +static int merge_check_workdir(size_t *conflicts, git_repository *repo, git_index *index_new, git_vector *merged_paths) +{ + git_diff *wd_diff_list = NULL; + git_diff_options opts = GIT_DIFF_OPTIONS_INIT; + int error = 0; + + GIT_UNUSED(index_new); + + *conflicts = 0; + + /* We need to have merged at least 1 file for the possibility to exist to + * have conflicts with the workdir. Passing 0 as the pathspec count parameter + * will consider all files in the working directory, that is, we may detect + * a conflict if there were untracked files in the workdir prior to starting + * the merge. This typically happens when cherry-picking a commit whose + * changes have already been applied. + */ + if (merged_paths->length == 0) + return 0; + + opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED; + + /* Workdir changes may exist iff they do not conflict with changes that + * will be applied by the merge (including conflicts). Ensure that there + * are no changes in the workdir to these paths. + */ + opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH; + opts.pathspec.count = merged_paths->length; + opts.pathspec.strings = (char **)merged_paths->contents; + opts.ignore_submodules = GIT_SUBMODULE_IGNORE_ALL; + + if ((error = git_diff_index_to_workdir(&wd_diff_list, repo, NULL, &opts)) < 0) + goto done; + + *conflicts = wd_diff_list->deltas.length; + +done: + git_diff_free(wd_diff_list); + + return error; +} + +int git_merge__check_result(git_repository *repo, git_index *index_new) +{ + git_tree *head_tree = NULL; + git_iterator *iter_head = NULL, *iter_new = NULL; + git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; + git_diff *merged_list = NULL; + git_diff_options opts = GIT_DIFF_OPTIONS_INIT; + git_diff_delta *delta; + git_vector paths = GIT_VECTOR_INIT; + size_t i, index_conflicts = 0, wd_conflicts = 0, conflicts; + const git_index_entry *e; + int error = 0; + + iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + if ((error = git_repository_head_tree(&head_tree, repo)) < 0 || + (error = git_iterator_for_tree(&iter_head, head_tree, &iter_opts)) < 0 || + (error = git_iterator_for_index(&iter_new, repo, index_new, &iter_opts)) < 0 || + (error = git_diff__from_iterators(&merged_list, repo, iter_head, iter_new, &opts)) < 0) + goto done; + + git_vector_foreach(&merged_list->deltas, i, delta) { + if ((error = git_vector_insert(&paths, (char *)delta->new_file.path)) < 0) + goto done; + } + + for (i = 0; i < git_index_entrycount(index_new); i++) { + e = git_index_get_byindex(index_new, i); + + if (git_index_entry_is_conflict(e) && + (git_vector_last(&paths) == NULL || + strcmp(git_vector_last(&paths), e->path) != 0)) { + + if ((error = git_vector_insert(&paths, (char *)e->path)) < 0) + goto done; + } + } + + /* Make sure the index and workdir state do not prevent merging */ + if ((error = merge_check_index(&index_conflicts, repo, index_new, &paths)) < 0 || + (error = merge_check_workdir(&wd_conflicts, repo, index_new, &paths)) < 0) + goto done; + + if ((conflicts = index_conflicts + wd_conflicts) > 0) { + git_error_set(GIT_ERROR_MERGE, "%" PRIuZ " uncommitted change%s would be overwritten by merge", + conflicts, (conflicts != 1) ? "s" : ""); + error = GIT_ECONFLICT; + } + +done: + git_vector_dispose(&paths); + git_tree_free(head_tree); + git_iterator_free(iter_head); + git_iterator_free(iter_new); + git_diff_free(merged_list); + + return error; +} + +int git_merge__append_conflicts_to_merge_msg( + git_repository *repo, + git_index *index) +{ + git_filebuf file = GIT_FILEBUF_INIT; + git_str file_path = GIT_STR_INIT; + const char *last = NULL; + size_t i; + int error; + + if (!git_index_has_conflicts(index)) + return 0; + + if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 || + (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_APPEND, GIT_MERGE_FILE_MODE)) < 0) + goto cleanup; + + git_filebuf_printf(&file, "\n#Conflicts:\n"); + + for (i = 0; i < git_index_entrycount(index); i++) { + const git_index_entry *e = git_index_get_byindex(index, i); + + if (!git_index_entry_is_conflict(e)) + continue; + + if (last == NULL || strcmp(e->path, last) != 0) + git_filebuf_printf(&file, "#\t%s\n", e->path); + + last = e->path; + } + + error = git_filebuf_commit(&file); + +cleanup: + if (error < 0) + git_filebuf_cleanup(&file); + + git_str_dispose(&file_path); + + return error; +} + +static int merge_state_cleanup(git_repository *repo) +{ + const char *state_files[] = { + GIT_MERGE_HEAD_FILE, + GIT_MERGE_MODE_FILE, + GIT_MERGE_MSG_FILE, + }; + + return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files)); +} + +static int merge_heads( + git_annotated_commit **ancestor_head_out, + git_annotated_commit **our_head_out, + git_repository *repo, + git_reference *our_ref, + const git_annotated_commit **their_heads, + size_t their_heads_len) +{ + git_annotated_commit *ancestor_head = NULL, *our_head = NULL; + int error = 0; + + *ancestor_head_out = NULL; + *our_head_out = NULL; + + if ((error = git_annotated_commit_from_ref(&our_head, repo, our_ref)) < 0) + goto done; + + if ((error = merge_ancestor_head(&ancestor_head, repo, our_head, their_heads, their_heads_len)) < 0) { + if (error != GIT_ENOTFOUND) + goto done; + + git_error_clear(); + error = 0; + } + + *ancestor_head_out = ancestor_head; + *our_head_out = our_head; + +done: + if (error < 0) { + git_annotated_commit_free(ancestor_head); + git_annotated_commit_free(our_head); + } + + return error; +} + +static int merge_preference(git_merge_preference_t *out, git_repository *repo) +{ + git_config *config; + const char *value; + int bool_value, error = 0; + + *out = GIT_MERGE_PREFERENCE_NONE; + + if ((error = git_repository_config_snapshot(&config, repo)) < 0) + goto done; + + if ((error = git_config_get_string(&value, config, "merge.ff")) < 0) { + if (error == GIT_ENOTFOUND) { + git_error_clear(); + error = 0; + } + + goto done; + } + + if (git_config_parse_bool(&bool_value, value) == 0) { + if (!bool_value) + *out |= GIT_MERGE_PREFERENCE_NO_FASTFORWARD; + } else { + if (strcasecmp(value, "only") == 0) + *out |= GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY; + } + +done: + git_config_free(config); + return error; +} + +int git_merge_analysis_for_ref( + git_merge_analysis_t *analysis_out, + git_merge_preference_t *preference_out, + git_repository *repo, + git_reference *our_ref, + const git_annotated_commit **their_heads, + size_t their_heads_len) +{ + git_annotated_commit *ancestor_head = NULL, *our_head = NULL; + int error = 0; + bool unborn; + + GIT_ASSERT_ARG(analysis_out); + GIT_ASSERT_ARG(preference_out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(their_heads && their_heads_len > 0); + + if (their_heads_len != 1) { + git_error_set(GIT_ERROR_MERGE, "can only merge a single branch"); + error = -1; + goto done; + } + + *analysis_out = GIT_MERGE_ANALYSIS_NONE; + + if ((error = merge_preference(preference_out, repo)) < 0) + goto done; + + if ((error = git_reference__is_unborn_head(&unborn, our_ref, repo)) < 0) + goto done; + + if (unborn) { + *analysis_out |= GIT_MERGE_ANALYSIS_FASTFORWARD | GIT_MERGE_ANALYSIS_UNBORN; + error = 0; + goto done; + } + + if ((error = merge_heads(&ancestor_head, &our_head, repo, our_ref, their_heads, their_heads_len)) < 0) + goto done; + + /* We're up-to-date if we're trying to merge our own common ancestor. */ + if (ancestor_head && git_oid_equal( + git_annotated_commit_id(ancestor_head), git_annotated_commit_id(their_heads[0]))) + *analysis_out |= GIT_MERGE_ANALYSIS_UP_TO_DATE; + + /* We're fastforwardable if we're our own common ancestor. */ + else if (ancestor_head && git_oid_equal( + git_annotated_commit_id(ancestor_head), git_annotated_commit_id(our_head))) + *analysis_out |= GIT_MERGE_ANALYSIS_FASTFORWARD | GIT_MERGE_ANALYSIS_NORMAL; + + /* Otherwise, just a normal merge is possible. */ + else + *analysis_out |= GIT_MERGE_ANALYSIS_NORMAL; + +done: + git_annotated_commit_free(ancestor_head); + git_annotated_commit_free(our_head); + return error; +} + +int git_merge_analysis( + git_merge_analysis_t *analysis_out, + git_merge_preference_t *preference_out, + git_repository *repo, + const git_annotated_commit **their_heads, + size_t their_heads_len) +{ + git_reference *head_ref = NULL; + int error = 0; + + if ((error = git_reference_lookup(&head_ref, repo, GIT_HEAD_FILE)) < 0) { + git_error_set(GIT_ERROR_MERGE, "failed to lookup HEAD reference"); + return error; + } + + error = git_merge_analysis_for_ref(analysis_out, preference_out, repo, head_ref, their_heads, their_heads_len); + + git_reference_free(head_ref); + + return error; +} + +int git_merge( + git_repository *repo, + const git_annotated_commit **their_heads, + size_t their_heads_len, + const git_merge_options *merge_opts, + const git_checkout_options *given_checkout_opts) +{ + git_reference *our_ref = NULL; + git_checkout_options checkout_opts; + git_annotated_commit *our_head = NULL, *base = NULL; + git_index *repo_index = NULL, *index = NULL; + git_indexwriter indexwriter = GIT_INDEXWRITER_INIT; + unsigned int checkout_strategy; + int error = 0; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(their_heads && their_heads_len > 0); + + if (their_heads_len != 1) { + git_error_set(GIT_ERROR_MERGE, "can only merge a single branch"); + return -1; + } + + if ((error = git_repository__ensure_not_bare(repo, "merge")) < 0) + goto done; + + checkout_strategy = given_checkout_opts ? + given_checkout_opts->checkout_strategy : 0; + + if ((error = git_indexwriter_init_for_operation(&indexwriter, repo, + &checkout_strategy)) < 0) + goto done; + + if ((error = git_repository_index(&repo_index, repo) < 0) || + (error = git_index_read(repo_index, 0) < 0)) + goto done; + + /* Write the merge setup files to the repository. */ + if ((error = git_annotated_commit_from_head(&our_head, repo)) < 0 || + (error = git_merge__setup(repo, our_head, their_heads, + their_heads_len)) < 0) + goto done; + + /* TODO: octopus */ + + if ((error = merge_annotated_commits(&index, &base, repo, our_head, + (git_annotated_commit *)their_heads[0], 0, merge_opts)) < 0 || + (error = git_merge__check_result(repo, index)) < 0 || + (error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0) + goto done; + + /* check out the merge results */ + + if ((error = merge_normalize_checkout_opts(&checkout_opts, repo, + given_checkout_opts, checkout_strategy, + base, our_head, their_heads, their_heads_len)) < 0 || + (error = git_checkout_index(repo, index, &checkout_opts)) < 0) + goto done; + + error = git_indexwriter_commit(&indexwriter); + +done: + if (error < 0) + merge_state_cleanup(repo); + + git_indexwriter_cleanup(&indexwriter); + git_index_free(index); + git_annotated_commit_free(our_head); + git_annotated_commit_free(base); + git_reference_free(our_ref); + git_index_free(repo_index); + + return error; +} + +int git_merge_options_init(git_merge_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_merge_options, GIT_MERGE_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_merge_init_options(git_merge_options *opts, unsigned int version) +{ + return git_merge_options_init(opts, version); +} +#endif + +int git_merge_file_input_init(git_merge_file_input *input, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + input, version, git_merge_file_input, GIT_MERGE_FILE_INPUT_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_merge_file_init_input(git_merge_file_input *input, unsigned int version) +{ + return git_merge_file_input_init(input, version); +} +#endif + +int git_merge_file_options_init( + git_merge_file_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_merge_file_options, GIT_MERGE_FILE_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_merge_file_init_options( + git_merge_file_options *opts, unsigned int version) +{ + return git_merge_file_options_init(opts, version); +} +#endif diff --git a/src/libgit2/merge.h b/src/libgit2/merge.h new file mode 100644 index 00000000000..23932905e80 --- /dev/null +++ b/src/libgit2/merge.h @@ -0,0 +1,204 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_merge_h__ +#define INCLUDE_merge_h__ + +#include "common.h" + +#include "vector.h" +#include "commit_list.h" +#include "pool.h" +#include "iterator.h" + +#include "git2/types.h" +#include "git2/merge.h" +#include "git2/sys/merge.h" + +#define GIT_MERGE_MSG_FILE "MERGE_MSG" +#define GIT_MERGE_MODE_FILE "MERGE_MODE" +#define GIT_MERGE_FILE_MODE 0666 + +#define GIT_MERGE_DEFAULT_RENAME_THRESHOLD 50 +#define GIT_MERGE_DEFAULT_TARGET_LIMIT 1000 + +/** Types of changes when files are merged from branch to branch. */ +typedef enum { + /* No conflict - a change only occurs in one branch. */ + GIT_MERGE_DIFF_NONE = 0, + + /* Occurs when a file is modified in both branches. */ + GIT_MERGE_DIFF_BOTH_MODIFIED = (1 << 0), + + /* Occurs when a file is added in both branches. */ + GIT_MERGE_DIFF_BOTH_ADDED = (1 << 1), + + /* Occurs when a file is deleted in both branches. */ + GIT_MERGE_DIFF_BOTH_DELETED = (1 << 2), + + /* Occurs when a file is modified in one branch and deleted in the other. */ + GIT_MERGE_DIFF_MODIFIED_DELETED = (1 << 3), + + /* Occurs when a file is renamed in one branch and modified in the other. */ + GIT_MERGE_DIFF_RENAMED_MODIFIED = (1 << 4), + + /* Occurs when a file is renamed in one branch and deleted in the other. */ + GIT_MERGE_DIFF_RENAMED_DELETED = (1 << 5), + + /* Occurs when a file is renamed in one branch and a file with the same + * name is added in the other. Eg, A->B and new file B. Core git calls + * this a "rename/delete". */ + GIT_MERGE_DIFF_RENAMED_ADDED = (1 << 6), + + /* Occurs when both a file is renamed to the same name in the ours and + * theirs branches. Eg, A->B and A->B in both. Automergeable. */ + GIT_MERGE_DIFF_BOTH_RENAMED = (1 << 7), + + /* Occurs when a file is renamed to different names in the ours and theirs + * branches. Eg, A->B and A->C. */ + GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2 = (1 << 8), + + /* Occurs when two files are renamed to the same name in the ours and + * theirs branches. Eg, A->C and B->C. */ + GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1 = (1 << 9), + + /* Occurs when an item at a path in one branch is a directory, and an + * item at the same path in a different branch is a file. */ + GIT_MERGE_DIFF_DIRECTORY_FILE = (1 << 10), + + /* The child of a folder that is in a directory/file conflict. */ + GIT_MERGE_DIFF_DF_CHILD = (1 << 11) +} git_merge_diff_t; + +typedef struct { + git_repository *repo; + git_pool pool; + + /* Vector of git_index_entry that represent the merged items that + * have been staged, either because only one side changed, or because + * the two changes were non-conflicting and mergeable. These items + * will be written as staged entries in the main index. + */ + git_vector staged; + + /* Vector of git_merge_diff entries that represent the conflicts that + * have not been automerged. These items will be written to high-stage + * entries in the main index. + */ + git_vector conflicts; + + /* Vector of git_merge_diff that have been automerged. These items + * will be written to the REUC when the index is produced. + */ + git_vector resolved; +} git_merge_diff_list; + +/** + * Description of changes to one file across three trees. + */ +typedef struct { + git_merge_diff_t type; + + git_index_entry ancestor_entry; + + git_index_entry our_entry; + git_delta_t our_status; + + git_index_entry their_entry; + git_delta_t their_status; + +} git_merge_diff; + +int git_merge__bases_many( + git_commit_list **out, + git_revwalk *walk, + git_commit_list_node *one, + git_vector *twos, + uint32_t minimum_generation); + +/* + * Three-way tree differencing + */ + +git_merge_diff_list *git_merge_diff_list__alloc(git_repository *repo); + +int git_merge_diff_list__find_differences( + git_merge_diff_list *merge_diff_list, + git_iterator *ancestor_iterator, + git_iterator *ours_iter, + git_iterator *theirs_iter); + +int git_merge_diff_list__find_renames(git_repository *repo, git_merge_diff_list *merge_diff_list, const git_merge_options *opts); + +void git_merge_diff_list__free(git_merge_diff_list *diff_list); + +/* Merge metadata setup */ + +int git_merge__setup( + git_repository *repo, + const git_annotated_commit *our_head, + const git_annotated_commit *heads[], + size_t heads_len); + +int git_merge__iterators( + git_index **out, + git_repository *repo, + git_iterator *ancestor_iter, + git_iterator *our_iter, + git_iterator *their_iter, + const git_merge_options *given_opts); + +int git_merge__check_result(git_repository *repo, git_index *index_new); + +int git_merge__append_conflicts_to_merge_msg(git_repository *repo, git_index *index); + +/* Merge files */ + +GIT_INLINE(const char *) git_merge_file__best_path( + const char *ancestor, + const char *ours, + const char *theirs) +{ + if (!ancestor) { + if (ours && theirs && strcmp(ours, theirs) == 0) + return ours; + + return NULL; + } + + if (ours && strcmp(ancestor, ours) == 0) + return theirs; + else if(theirs && strcmp(ancestor, theirs) == 0) + return ours; + + return NULL; +} + +GIT_INLINE(uint32_t) git_merge_file__best_mode( + uint32_t ancestor, uint32_t ours, uint32_t theirs) +{ + /* + * If ancestor didn't exist and either ours or theirs is executable, + * assume executable. Otherwise, if any mode changed from the ancestor, + * use that one. + */ + if (!ancestor) { + if (ours == GIT_FILEMODE_BLOB_EXECUTABLE || + theirs == GIT_FILEMODE_BLOB_EXECUTABLE) + return GIT_FILEMODE_BLOB_EXECUTABLE; + + return GIT_FILEMODE_BLOB; + } else if (ours && theirs) { + if (ancestor == ours) + return theirs; + + return ours; + } + + return 0; +} + +#endif diff --git a/src/libgit2/merge_driver.c b/src/libgit2/merge_driver.c new file mode 100644 index 00000000000..300964afdea --- /dev/null +++ b/src/libgit2/merge_driver.c @@ -0,0 +1,432 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "merge_driver.h" + +#include "vector.h" +#include "runtime.h" +#include "merge.h" +#include "git2/merge.h" +#include "git2/sys/merge.h" + +static const char *merge_driver_name__text = "text"; +static const char *merge_driver_name__union = "union"; +static const char *merge_driver_name__binary = "binary"; + +struct merge_driver_registry { + git_rwlock lock; + git_vector drivers; +}; + +typedef struct { + git_merge_driver *driver; + int initialized; + char name[GIT_FLEX_ARRAY]; +} git_merge_driver_entry; + +static struct merge_driver_registry merge_driver_registry; + +static void git_merge_driver_global_shutdown(void); + +git_repository *git_merge_driver_source_repo( + const git_merge_driver_source *src) +{ + GIT_ASSERT_ARG_WITH_RETVAL(src, NULL); + return src->repo; +} + +const git_index_entry *git_merge_driver_source_ancestor( + const git_merge_driver_source *src) +{ + GIT_ASSERT_ARG_WITH_RETVAL(src, NULL); + return src->ancestor; +} + +const git_index_entry *git_merge_driver_source_ours( + const git_merge_driver_source *src) +{ + GIT_ASSERT_ARG_WITH_RETVAL(src, NULL); + return src->ours; +} + +const git_index_entry *git_merge_driver_source_theirs( + const git_merge_driver_source *src) +{ + GIT_ASSERT_ARG_WITH_RETVAL(src, NULL); + return src->theirs; +} + +const git_merge_file_options *git_merge_driver_source_file_options( + const git_merge_driver_source *src) +{ + GIT_ASSERT_ARG_WITH_RETVAL(src, NULL); + return src->file_opts; +} + +int git_merge_driver__builtin_apply( + git_merge_driver *self, + const char **path_out, + uint32_t *mode_out, + git_buf *merged_out, + const char *filter_name, + const git_merge_driver_source *src) +{ + git_merge_driver__builtin *driver = (git_merge_driver__builtin *)self; + git_merge_file_options file_opts = GIT_MERGE_FILE_OPTIONS_INIT; + git_merge_file_result result = {0}; + int error; + + GIT_UNUSED(filter_name); + + if (src->file_opts) + memcpy(&file_opts, src->file_opts, sizeof(git_merge_file_options)); + + if (driver->favor) + file_opts.favor = driver->favor; + + if ((error = git_merge_file_from_index(&result, src->repo, + src->ancestor, src->ours, src->theirs, &file_opts)) < 0) + goto done; + + if (!result.automergeable && + !(file_opts.flags & GIT_MERGE_FILE_ACCEPT_CONFLICTS)) { + error = GIT_EMERGECONFLICT; + goto done; + } + + *path_out = git_merge_file__best_path( + src->ancestor ? src->ancestor->path : NULL, + src->ours ? src->ours->path : NULL, + src->theirs ? src->theirs->path : NULL); + + *mode_out = git_merge_file__best_mode( + src->ancestor ? src->ancestor->mode : 0, + src->ours ? src->ours->mode : 0, + src->theirs ? src->theirs->mode : 0); + + merged_out->ptr = (char *)result.ptr; + merged_out->size = result.len; + merged_out->reserved = 0; + result.ptr = NULL; + +done: + git_merge_file_result_free(&result); + return error; +} + +static int merge_driver_binary_apply( + git_merge_driver *self, + const char **path_out, + uint32_t *mode_out, + git_buf *merged_out, + const char *filter_name, + const git_merge_driver_source *src) +{ + GIT_UNUSED(self); + GIT_UNUSED(path_out); + GIT_UNUSED(mode_out); + GIT_UNUSED(merged_out); + GIT_UNUSED(filter_name); + GIT_UNUSED(src); + + return GIT_EMERGECONFLICT; +} + +static int merge_driver_entry_cmp(const void *a, const void *b) +{ + const git_merge_driver_entry *entry_a = a; + const git_merge_driver_entry *entry_b = b; + + return strcmp(entry_a->name, entry_b->name); +} + +static int merge_driver_entry_search(const void *a, const void *b) +{ + const char *name_a = a; + const git_merge_driver_entry *entry_b = b; + + return strcmp(name_a, entry_b->name); +} + +git_merge_driver__builtin git_merge_driver__text = { + { + GIT_MERGE_DRIVER_VERSION, + NULL, + NULL, + git_merge_driver__builtin_apply, + }, + GIT_MERGE_FILE_FAVOR_NORMAL +}; + +git_merge_driver__builtin git_merge_driver__union = { + { + GIT_MERGE_DRIVER_VERSION, + NULL, + NULL, + git_merge_driver__builtin_apply, + }, + GIT_MERGE_FILE_FAVOR_UNION +}; + +git_merge_driver git_merge_driver__binary = { + GIT_MERGE_DRIVER_VERSION, + NULL, + NULL, + merge_driver_binary_apply +}; + +/* Note: callers must lock the registry before calling this function */ +static int merge_driver_registry_insert( + const char *name, git_merge_driver *driver) +{ + git_merge_driver_entry *entry; + + entry = git__calloc(1, sizeof(git_merge_driver_entry) + strlen(name) + 1); + GIT_ERROR_CHECK_ALLOC(entry); + + strcpy(entry->name, name); + entry->driver = driver; + + return git_vector_insert_sorted( + &merge_driver_registry.drivers, entry, NULL); +} + +int git_merge_driver_global_init(void) +{ + int error; + + if (git_rwlock_init(&merge_driver_registry.lock) < 0) + return -1; + + if ((error = git_vector_init(&merge_driver_registry.drivers, 3, + merge_driver_entry_cmp)) < 0) + goto done; + + if ((error = merge_driver_registry_insert( + merge_driver_name__text, &git_merge_driver__text.base)) < 0 || + (error = merge_driver_registry_insert( + merge_driver_name__union, &git_merge_driver__union.base)) < 0 || + (error = merge_driver_registry_insert( + merge_driver_name__binary, &git_merge_driver__binary)) < 0) + goto done; + + error = git_runtime_shutdown_register(git_merge_driver_global_shutdown); + +done: + if (error < 0) + git_vector_dispose_deep(&merge_driver_registry.drivers); + + return error; +} + +static void git_merge_driver_global_shutdown(void) +{ + git_merge_driver_entry *entry; + size_t i; + + if (git_rwlock_wrlock(&merge_driver_registry.lock) < 0) + return; + + git_vector_foreach(&merge_driver_registry.drivers, i, entry) { + if (entry->driver->shutdown) + entry->driver->shutdown(entry->driver); + + git__free(entry); + } + + git_vector_dispose(&merge_driver_registry.drivers); + + git_rwlock_wrunlock(&merge_driver_registry.lock); + git_rwlock_free(&merge_driver_registry.lock); +} + +/* Note: callers must lock the registry before calling this function */ +static int merge_driver_registry_find(size_t *pos, const char *name) +{ + return git_vector_search2(pos, &merge_driver_registry.drivers, + merge_driver_entry_search, name); +} + +/* Note: callers must lock the registry before calling this function */ +static git_merge_driver_entry *merge_driver_registry_lookup( + size_t *pos, const char *name) +{ + git_merge_driver_entry *entry = NULL; + + if (!merge_driver_registry_find(pos, name)) + entry = git_vector_get(&merge_driver_registry.drivers, *pos); + + return entry; +} + +int git_merge_driver_register(const char *name, git_merge_driver *driver) +{ + int error; + + GIT_ASSERT_ARG(name); + GIT_ASSERT_ARG(driver); + + if (git_rwlock_wrlock(&merge_driver_registry.lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock merge driver registry"); + return -1; + } + + if (!merge_driver_registry_find(NULL, name)) { + git_error_set(GIT_ERROR_MERGE, "attempt to reregister existing driver '%s'", + name); + error = GIT_EEXISTS; + goto done; + } + + error = merge_driver_registry_insert(name, driver); + +done: + git_rwlock_wrunlock(&merge_driver_registry.lock); + return error; +} + +int git_merge_driver_unregister(const char *name) +{ + git_merge_driver_entry *entry; + size_t pos; + int error = 0; + + if (git_rwlock_wrlock(&merge_driver_registry.lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock merge driver registry"); + return -1; + } + + if ((entry = merge_driver_registry_lookup(&pos, name)) == NULL) { + git_error_set(GIT_ERROR_MERGE, "cannot find merge driver '%s' to unregister", + name); + error = GIT_ENOTFOUND; + goto done; + } + + git_vector_remove(&merge_driver_registry.drivers, pos); + + if (entry->initialized && entry->driver->shutdown) { + entry->driver->shutdown(entry->driver); + entry->initialized = false; + } + + git__free(entry); + +done: + git_rwlock_wrunlock(&merge_driver_registry.lock); + return error; +} + +git_merge_driver *git_merge_driver_lookup(const char *name) +{ + git_merge_driver_entry *entry; + size_t pos; + int error; + + /* If we've decided the merge driver to use internally - and not + * based on user configuration (in merge_driver_name_for_path) + * then we can use a hardcoded name to compare instead of bothering + * to take a lock and look it up in the vector. + */ + if (name == merge_driver_name__text) + return &git_merge_driver__text.base; + else if (name == merge_driver_name__binary) + return &git_merge_driver__binary; + + if (git_rwlock_rdlock(&merge_driver_registry.lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock merge driver registry"); + return NULL; + } + + entry = merge_driver_registry_lookup(&pos, name); + + git_rwlock_rdunlock(&merge_driver_registry.lock); + + if (entry == NULL) { + git_error_set(GIT_ERROR_MERGE, "cannot use an unregistered filter"); + return NULL; + } + + if (!entry->initialized) { + if (entry->driver->initialize && + (error = entry->driver->initialize(entry->driver)) < 0) + return NULL; + + entry->initialized = 1; + } + + return entry->driver; +} + +static int merge_driver_name_for_path( + const char **out, + git_repository *repo, + const char *path, + const char *default_driver) +{ + const char *value; + int error; + + *out = NULL; + + if ((error = git_attr_get(&value, repo, 0, path, "merge")) < 0) + return error; + + /* set: use the built-in 3-way merge driver ("text") */ + if (GIT_ATTR_IS_TRUE(value)) + *out = merge_driver_name__text; + + /* unset: do not merge ("binary") */ + else if (GIT_ATTR_IS_FALSE(value)) + *out = merge_driver_name__binary; + + else if (GIT_ATTR_IS_UNSPECIFIED(value) && default_driver) + *out = default_driver; + + else if (GIT_ATTR_IS_UNSPECIFIED(value)) + *out = merge_driver_name__text; + + else + *out = value; + + return 0; +} + + +GIT_INLINE(git_merge_driver *) merge_driver_lookup_with_wildcard( + const char *name) +{ + git_merge_driver *driver = git_merge_driver_lookup(name); + + if (driver == NULL) + driver = git_merge_driver_lookup("*"); + + return driver; +} + +int git_merge_driver_for_source( + const char **name_out, + git_merge_driver **driver_out, + const git_merge_driver_source *src) +{ + const char *path, *driver_name; + int error = 0; + + path = git_merge_file__best_path( + src->ancestor ? src->ancestor->path : NULL, + src->ours ? src->ours->path : NULL, + src->theirs ? src->theirs->path : NULL); + + if ((error = merge_driver_name_for_path( + &driver_name, src->repo, path, src->default_driver)) < 0) + return error; + + *name_out = driver_name; + *driver_out = merge_driver_lookup_with_wildcard(driver_name); + return error; +} + diff --git a/src/libgit2/merge_driver.h b/src/libgit2/merge_driver.h new file mode 100644 index 00000000000..6b7da5287bd --- /dev/null +++ b/src/libgit2/merge_driver.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_merge_driver_h__ +#define INCLUDE_merge_driver_h__ + +#include "common.h" + +#include "git2/merge.h" +#include "git2/index.h" +#include "git2/sys/merge.h" + +struct git_merge_driver_source { + git_repository *repo; + const char *default_driver; + const git_merge_file_options *file_opts; + + const git_index_entry *ancestor; + const git_index_entry *ours; + const git_index_entry *theirs; +}; + +typedef struct git_merge_driver__builtin { + git_merge_driver base; + git_merge_file_favor_t favor; +} git_merge_driver__builtin; + +extern int git_merge_driver_global_init(void); + +extern int git_merge_driver_for_path( + char **name_out, + git_merge_driver **driver_out, + git_repository *repo, + const char *path); + +/* Merge driver configuration */ +extern int git_merge_driver_for_source( + const char **name_out, + git_merge_driver **driver_out, + const git_merge_driver_source *src); + +extern int git_merge_driver__builtin_apply( + git_merge_driver *self, + const char **path_out, + uint32_t *mode_out, + git_buf *merged_out, + const char *filter_name, + const git_merge_driver_source *src); + +/* Merge driver for text files, performs a standard three-way merge */ +extern git_merge_driver__builtin git_merge_driver__text; + +/* Merge driver for union-style merging */ +extern git_merge_driver__builtin git_merge_driver__union; + +/* Merge driver for unmergeable (binary) files: always produces conflicts */ +extern git_merge_driver git_merge_driver__binary; + +#endif diff --git a/src/libgit2/merge_file.c b/src/libgit2/merge_file.c new file mode 100644 index 00000000000..ffe83cf2a3b --- /dev/null +++ b/src/libgit2/merge_file.c @@ -0,0 +1,325 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "repository.h" +#include "posix.h" +#include "futils.h" +#include "index.h" +#include "diff_xdiff.h" +#include "merge.h" + +#include "git2/repository.h" +#include "git2/object.h" +#include "git2/index.h" +#include "git2/merge.h" + +/* only examine the first 8000 bytes for binaryness. + * https://github.com/git/git/blob/77bd3ea9f54f1584147b594abc04c26ca516d987/xdiff-interface.c#L197 + */ +#define GIT_MERGE_FILE_BINARY_SIZE 8000 + +#define GIT_MERGE_FILE_SIDE_EXISTS(X) ((X)->mode != 0) + +static int merge_file_input_from_index( + git_merge_file_input *input_out, + git_odb_object **odb_object_out, + git_odb *odb, + const git_index_entry *entry) +{ + int error = 0; + + GIT_ASSERT_ARG(input_out); + GIT_ASSERT_ARG(odb_object_out); + GIT_ASSERT_ARG(odb); + GIT_ASSERT_ARG(entry); + + if ((error = git_odb_read(odb_object_out, odb, &entry->id)) < 0) + goto done; + + input_out->path = entry->path; + input_out->mode = entry->mode; + input_out->ptr = (char *)git_odb_object_data(*odb_object_out); + input_out->size = git_odb_object_size(*odb_object_out); + +done: + return error; +} + +static void merge_file_normalize_opts( + git_merge_file_options *out, + const git_merge_file_options *given_opts) +{ + if (given_opts) + memcpy(out, given_opts, sizeof(git_merge_file_options)); + else { + git_merge_file_options default_opts = GIT_MERGE_FILE_OPTIONS_INIT; + memcpy(out, &default_opts, sizeof(git_merge_file_options)); + } +} + +static int merge_file__xdiff( + git_merge_file_result *out, + const git_merge_file_input *ancestor, + const git_merge_file_input *ours, + const git_merge_file_input *theirs, + const git_merge_file_options *given_opts) +{ + xmparam_t xmparam; + mmfile_t ancestor_mmfile = {0}, our_mmfile = {0}, their_mmfile = {0}; + mmbuffer_t mmbuffer; + git_merge_file_options options = GIT_MERGE_FILE_OPTIONS_INIT; + const char *path; + int xdl_result; + int error = 0; + + memset(out, 0x0, sizeof(git_merge_file_result)); + + merge_file_normalize_opts(&options, given_opts); + + memset(&xmparam, 0x0, sizeof(xmparam_t)); + + if (ours->size > LONG_MAX || + theirs->size > LONG_MAX || + (ancestor && ancestor->size > LONG_MAX)) { + git_error_set(GIT_ERROR_MERGE, "failed to merge files"); + error = -1; + goto done; + } + + if (ancestor) { + xmparam.ancestor = (options.ancestor_label) ? + options.ancestor_label : ancestor->path; + ancestor_mmfile.ptr = (char *)ancestor->ptr; + ancestor_mmfile.size = (long)ancestor->size; + } + + xmparam.file1 = (options.our_label) ? + options.our_label : ours->path; + our_mmfile.ptr = (char *)ours->ptr; + our_mmfile.size = (long)ours->size; + + xmparam.file2 = (options.their_label) ? + options.their_label : theirs->path; + their_mmfile.ptr = (char *)theirs->ptr; + their_mmfile.size = (long)theirs->size; + + if (options.favor == GIT_MERGE_FILE_FAVOR_OURS) + xmparam.favor = XDL_MERGE_FAVOR_OURS; + else if (options.favor == GIT_MERGE_FILE_FAVOR_THEIRS) + xmparam.favor = XDL_MERGE_FAVOR_THEIRS; + else if (options.favor == GIT_MERGE_FILE_FAVOR_UNION) + xmparam.favor = XDL_MERGE_FAVOR_UNION; + + xmparam.level = (options.flags & GIT_MERGE_FILE_SIMPLIFY_ALNUM) ? + XDL_MERGE_ZEALOUS_ALNUM : XDL_MERGE_ZEALOUS; + + if (options.flags & GIT_MERGE_FILE_STYLE_DIFF3) + xmparam.style = XDL_MERGE_DIFF3; + if (options.flags & GIT_MERGE_FILE_STYLE_ZDIFF3) + xmparam.style = XDL_MERGE_ZEALOUS_DIFF3; + + if (options.flags & GIT_MERGE_FILE_IGNORE_WHITESPACE) + xmparam.xpp.flags |= XDF_IGNORE_WHITESPACE; + if (options.flags & GIT_MERGE_FILE_IGNORE_WHITESPACE_CHANGE) + xmparam.xpp.flags |= XDF_IGNORE_WHITESPACE_CHANGE; + if (options.flags & GIT_MERGE_FILE_IGNORE_WHITESPACE_EOL) + xmparam.xpp.flags |= XDF_IGNORE_WHITESPACE_AT_EOL; + + if (options.flags & GIT_MERGE_FILE_DIFF_PATIENCE) + xmparam.xpp.flags |= XDF_PATIENCE_DIFF; + + if (options.flags & GIT_MERGE_FILE_DIFF_MINIMAL) + xmparam.xpp.flags |= XDF_NEED_MINIMAL; + + xmparam.marker_size = options.marker_size; + + if ((xdl_result = xdl_merge(&ancestor_mmfile, &our_mmfile, + &their_mmfile, &xmparam, &mmbuffer)) < 0) { + git_error_set(GIT_ERROR_MERGE, "failed to merge files"); + error = -1; + goto done; + } + + path = git_merge_file__best_path( + ancestor ? ancestor->path : NULL, + ours->path, + theirs->path); + + if (path != NULL && (out->path = git__strdup(path)) == NULL) { + error = -1; + goto done; + } + + out->automergeable = (xdl_result == 0); + out->ptr = (const char *)mmbuffer.ptr; + out->len = mmbuffer.size; + out->mode = git_merge_file__best_mode( + ancestor ? ancestor->mode : 0, + ours->mode, + theirs->mode); + +done: + if (error < 0) + git_merge_file_result_free(out); + + return error; +} + +static bool merge_file__is_binary(const git_merge_file_input *file) +{ + size_t len = file ? file->size : 0; + + if (len > GIT_XDIFF_MAX_SIZE) + return true; + if (len > GIT_MERGE_FILE_BINARY_SIZE) + len = GIT_MERGE_FILE_BINARY_SIZE; + + return len ? (memchr(file->ptr, 0, len) != NULL) : false; +} + +static int merge_file__binary( + git_merge_file_result *out, + const git_merge_file_input *ours, + const git_merge_file_input *theirs, + const git_merge_file_options *given_opts) +{ + const git_merge_file_input *favored = NULL; + + memset(out, 0x0, sizeof(git_merge_file_result)); + + if (given_opts && given_opts->favor == GIT_MERGE_FILE_FAVOR_OURS) + favored = ours; + else if (given_opts && given_opts->favor == GIT_MERGE_FILE_FAVOR_THEIRS) + favored = theirs; + else + goto done; + + if ((out->path = git__strdup(favored->path)) == NULL || + (out->ptr = git__malloc(favored->size)) == NULL) + goto done; + + memcpy((char *)out->ptr, favored->ptr, favored->size); + out->len = favored->size; + out->mode = favored->mode; + out->automergeable = 1; + +done: + return 0; +} + +static int merge_file__from_inputs( + git_merge_file_result *out, + const git_merge_file_input *ancestor, + const git_merge_file_input *ours, + const git_merge_file_input *theirs, + const git_merge_file_options *given_opts) +{ + if (merge_file__is_binary(ancestor) || + merge_file__is_binary(ours) || + merge_file__is_binary(theirs)) + return merge_file__binary(out, ours, theirs, given_opts); + + return merge_file__xdiff(out, ancestor, ours, theirs, given_opts); +} + +static git_merge_file_input *git_merge_file__normalize_inputs( + git_merge_file_input *out, + const git_merge_file_input *given) +{ + memcpy(out, given, sizeof(git_merge_file_input)); + + if (!out->path) + out->path = "file.txt"; + + if (!out->mode) + out->mode = 0100644; + + return out; +} + +int git_merge_file( + git_merge_file_result *out, + const git_merge_file_input *ancestor, + const git_merge_file_input *ours, + const git_merge_file_input *theirs, + const git_merge_file_options *options) +{ + git_merge_file_input inputs[3] = { {0} }; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(ours); + GIT_ASSERT_ARG(theirs); + + memset(out, 0x0, sizeof(git_merge_file_result)); + + if (ancestor) + ancestor = git_merge_file__normalize_inputs(&inputs[0], ancestor); + + ours = git_merge_file__normalize_inputs(&inputs[1], ours); + theirs = git_merge_file__normalize_inputs(&inputs[2], theirs); + + return merge_file__from_inputs(out, ancestor, ours, theirs, options); +} + +int git_merge_file_from_index( + git_merge_file_result *out, + git_repository *repo, + const git_index_entry *ancestor, + const git_index_entry *ours, + const git_index_entry *theirs, + const git_merge_file_options *options) +{ + git_merge_file_input *ancestor_ptr = NULL, + ancestor_input = {0}, our_input = {0}, their_input = {0}; + git_odb *odb = NULL; + git_odb_object *odb_object[3] = { 0 }; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(ours); + GIT_ASSERT_ARG(theirs); + + memset(out, 0x0, sizeof(git_merge_file_result)); + + if ((error = git_repository_odb(&odb, repo)) < 0) + goto done; + + if (ancestor) { + if ((error = merge_file_input_from_index( + &ancestor_input, &odb_object[0], odb, ancestor)) < 0) + goto done; + + ancestor_ptr = &ancestor_input; + } + + if ((error = merge_file_input_from_index(&our_input, &odb_object[1], odb, ours)) < 0 || + (error = merge_file_input_from_index(&their_input, &odb_object[2], odb, theirs)) < 0) + goto done; + + error = merge_file__from_inputs(out, + ancestor_ptr, &our_input, &their_input, options); + +done: + git_odb_object_free(odb_object[0]); + git_odb_object_free(odb_object[1]); + git_odb_object_free(odb_object[2]); + git_odb_free(odb); + + return error; +} + +void git_merge_file_result_free(git_merge_file_result *result) +{ + if (result == NULL) + return; + + git__free((char *)result->path); + git__free((char *)result->ptr); +} diff --git a/src/libgit2/message.c b/src/libgit2/message.c new file mode 100644 index 00000000000..fe0000a7fae --- /dev/null +++ b/src/libgit2/message.c @@ -0,0 +1,75 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "buf.h" + +#include "git2/message.h" + +static size_t line_length_without_trailing_spaces(const char *line, size_t len) +{ + while (len) { + unsigned char c = line[len - 1]; + if (!git__isspace(c)) + break; + len--; + } + + return len; +} + +/* Greatly inspired from git.git "stripspace" */ +/* see https://github.com/git/git/blob/497215d8811ac7b8955693ceaad0899ecd894ed2/builtin/stripspace.c#L4-67 */ +static int git_message__prettify( + git_str *message_out, + const char *message, + int strip_comments, + char comment_char) +{ + const size_t message_len = strlen(message); + + int consecutive_empty_lines = 0; + size_t i, line_length, rtrimmed_line_length; + const char *next_newline; + + for (i = 0; i < strlen(message); i += line_length) { + next_newline = memchr(message + i, '\n', message_len - i); + + if (next_newline != NULL) { + line_length = next_newline - (message + i) + 1; + } else { + line_length = message_len - i; + } + + if (strip_comments && line_length && message[i] == comment_char) + continue; + + rtrimmed_line_length = line_length_without_trailing_spaces(message + i, line_length); + + if (!rtrimmed_line_length) { + consecutive_empty_lines++; + continue; + } + + if (consecutive_empty_lines > 0 && message_out->size > 0) + git_str_putc(message_out, '\n'); + + consecutive_empty_lines = 0; + git_str_put(message_out, message + i, rtrimmed_line_length); + git_str_putc(message_out, '\n'); + } + + return git_str_oom(message_out) ? -1 : 0; +} + +int git_message_prettify( + git_buf *message_out, + const char *message, + int strip_comments, + char comment_char) +{ + GIT_BUF_WRAP_PRIVATE(message_out, git_message__prettify, message, strip_comments, comment_char); +} diff --git a/src/libgit2/midx.c b/src/libgit2/midx.c new file mode 100644 index 00000000000..01152c73230 --- /dev/null +++ b/src/libgit2/midx.c @@ -0,0 +1,940 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "midx.h" + +#include "array.h" +#include "buf.h" +#include "filebuf.h" +#include "futils.h" +#include "hash.h" +#include "odb.h" +#include "pack.h" +#include "fs_path.h" +#include "repository.h" +#include "str.h" + +#define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */ +#define MIDX_VERSION 1 +#define MIDX_OBJECT_ID_VERSION 1 +struct git_midx_header { + uint32_t signature; + uint8_t version; + uint8_t object_id_version; + uint8_t chunks; + uint8_t base_midx_files; + uint32_t packfiles; +}; + +#define MIDX_PACKFILE_NAMES_ID 0x504e414d /* "PNAM" */ +#define MIDX_OID_FANOUT_ID 0x4f494446 /* "OIDF" */ +#define MIDX_OID_LOOKUP_ID 0x4f49444c /* "OIDL" */ +#define MIDX_OBJECT_OFFSETS_ID 0x4f4f4646 /* "OOFF" */ +#define MIDX_OBJECT_LARGE_OFFSETS_ID 0x4c4f4646 /* "LOFF" */ + +struct git_midx_chunk { + off64_t offset; + size_t length; +}; + +typedef int (*midx_write_cb)(const char *buf, size_t size, void *cb_data); + +static int midx_error(const char *message) +{ + git_error_set(GIT_ERROR_ODB, "invalid multi-pack-index file - %s", message); + return -1; +} + +static int midx_parse_packfile_names( + git_midx_file *idx, + const unsigned char *data, + uint32_t packfiles, + struct git_midx_chunk *chunk) +{ + int error; + uint32_t i; + char *packfile_name = (char *)(data + chunk->offset); + size_t chunk_size = chunk->length, len; + if (chunk->offset == 0) + return midx_error("missing Packfile Names chunk"); + if (chunk->length == 0) + return midx_error("empty Packfile Names chunk"); + if ((error = git_vector_init(&idx->packfile_names, packfiles, git__strcmp_cb)) < 0) + return error; + for (i = 0; i < packfiles; ++i) { + len = p_strnlen(packfile_name, chunk_size); + if (len == 0) + return midx_error("empty packfile name"); + if (len + 1 > chunk_size) + return midx_error("unterminated packfile name"); + git_vector_insert(&idx->packfile_names, packfile_name); + if (i && strcmp(git_vector_get(&idx->packfile_names, i - 1), packfile_name) >= 0) + return midx_error("packfile names are not sorted"); + if (strlen(packfile_name) <= strlen(".idx") || git__suffixcmp(packfile_name, ".idx") != 0) + return midx_error("non-.idx packfile name"); + if (strchr(packfile_name, '/') != NULL || strchr(packfile_name, '\\') != NULL) + return midx_error("non-local packfile"); + packfile_name += len + 1; + chunk_size -= len + 1; + } + return 0; +} + +static int midx_parse_oid_fanout( + git_midx_file *idx, + const unsigned char *data, + struct git_midx_chunk *chunk_oid_fanout) +{ + uint32_t i, nr; + if (chunk_oid_fanout->offset == 0) + return midx_error("missing OID Fanout chunk"); + if (chunk_oid_fanout->length == 0) + return midx_error("empty OID Fanout chunk"); + if (chunk_oid_fanout->length != 256 * 4) + return midx_error("OID Fanout chunk has wrong length"); + + idx->oid_fanout = (const uint32_t *)(data + chunk_oid_fanout->offset); + nr = 0; + for (i = 0; i < 256; ++i) { + uint32_t n = ntohl(idx->oid_fanout[i]); + if (n < nr) + return midx_error("index is non-monotonic"); + nr = n; + } + idx->num_objects = nr; + return 0; +} + +static int midx_parse_oid_lookup( + git_midx_file *idx, + const unsigned char *data, + struct git_midx_chunk *chunk_oid_lookup) +{ + size_t oid_size = git_oid_size(idx->oid_type); + + if (chunk_oid_lookup->offset == 0) + return midx_error("missing OID Lookup chunk"); + if (chunk_oid_lookup->length == 0) + return midx_error("empty OID Lookup chunk"); + if (chunk_oid_lookup->length != idx->num_objects * oid_size) + return midx_error("OID Lookup chunk has wrong length"); + + idx->oid_lookup = (unsigned char *)(data + chunk_oid_lookup->offset); + + return 0; +} + +static int midx_parse_object_offsets( + git_midx_file *idx, + const unsigned char *data, + struct git_midx_chunk *chunk_object_offsets) +{ + if (chunk_object_offsets->offset == 0) + return midx_error("missing Object Offsets chunk"); + if (chunk_object_offsets->length == 0) + return midx_error("empty Object Offsets chunk"); + if (chunk_object_offsets->length != idx->num_objects * 8) + return midx_error("Object Offsets chunk has wrong length"); + + idx->object_offsets = data + chunk_object_offsets->offset; + + return 0; +} + +static int midx_parse_object_large_offsets( + git_midx_file *idx, + const unsigned char *data, + struct git_midx_chunk *chunk_object_large_offsets) +{ + if (chunk_object_large_offsets->length == 0) + return 0; + if (chunk_object_large_offsets->length % 8 != 0) + return midx_error("malformed Object Large Offsets chunk"); + + idx->object_large_offsets = data + chunk_object_large_offsets->offset; + idx->num_object_large_offsets = chunk_object_large_offsets->length / 8; + + return 0; +} + +int git_midx_parse( + git_midx_file *idx, + const unsigned char *data, + size_t size) +{ + struct git_midx_header *hdr; + const unsigned char *chunk_hdr; + struct git_midx_chunk *last_chunk; + uint32_t i; + off64_t last_chunk_offset, chunk_offset, trailer_offset; + size_t checksum_size, oid_size; + int error; + struct git_midx_chunk chunk_packfile_names = {0}, + chunk_oid_fanout = {0}, + chunk_oid_lookup = {0}, + chunk_object_offsets = {0}, + chunk_object_large_offsets = {0}, + chunk_unknown = {0}; + + GIT_ASSERT_ARG(idx); + + oid_size = git_oid_size(idx->oid_type); + + if (size < sizeof(struct git_midx_header) + oid_size) + return midx_error("multi-pack index is too short"); + + hdr = ((struct git_midx_header *)data); + + if (hdr->signature != htonl(MIDX_SIGNATURE) || + hdr->version != MIDX_VERSION || + hdr->object_id_version != MIDX_OBJECT_ID_VERSION) { + return midx_error("unsupported multi-pack index version"); + } + if (hdr->chunks == 0) + return midx_error("no chunks in multi-pack index"); + + /* + * The very first chunk's offset should be after the header, all the chunk + * headers, and a special zero chunk. + */ + last_chunk_offset = + sizeof(struct git_midx_header) + + (1 + hdr->chunks) * 12; + + checksum_size = oid_size; + trailer_offset = size - checksum_size; + + if (trailer_offset < last_chunk_offset) + return midx_error("wrong index size"); + memcpy(idx->checksum, data + trailer_offset, checksum_size); + + chunk_hdr = data + sizeof(struct git_midx_header); + last_chunk = NULL; + for (i = 0; i < hdr->chunks; ++i, chunk_hdr += 12) { + uint32_t chunk_id = ntohl(*((uint32_t *)(chunk_hdr + 0))); + uint64_t high_offset = ((uint64_t)ntohl(*((uint32_t *)(chunk_hdr + 4)))) & 0xffffffffu; + uint64_t low_offset = ((uint64_t)ntohl(*((uint32_t *)(chunk_hdr + 8)))) & 0xffffffffu; + + if (high_offset >= INT32_MAX) + return midx_error("chunk offset out of range"); + chunk_offset = (off64_t)(high_offset << 32 | low_offset); + if (chunk_offset < last_chunk_offset) + return midx_error("chunks are non-monotonic"); + if (chunk_offset >= trailer_offset) + return midx_error("chunks extend beyond the trailer"); + if (last_chunk != NULL) + last_chunk->length = (size_t)(chunk_offset - last_chunk_offset); + last_chunk_offset = chunk_offset; + + switch (chunk_id) { + case MIDX_PACKFILE_NAMES_ID: + chunk_packfile_names.offset = last_chunk_offset; + last_chunk = &chunk_packfile_names; + break; + + case MIDX_OID_FANOUT_ID: + chunk_oid_fanout.offset = last_chunk_offset; + last_chunk = &chunk_oid_fanout; + break; + + case MIDX_OID_LOOKUP_ID: + chunk_oid_lookup.offset = last_chunk_offset; + last_chunk = &chunk_oid_lookup; + break; + + case MIDX_OBJECT_OFFSETS_ID: + chunk_object_offsets.offset = last_chunk_offset; + last_chunk = &chunk_object_offsets; + break; + + case MIDX_OBJECT_LARGE_OFFSETS_ID: + chunk_object_large_offsets.offset = last_chunk_offset; + last_chunk = &chunk_object_large_offsets; + break; + + default: + chunk_unknown.offset = last_chunk_offset; + last_chunk = &chunk_unknown; + break; + } + } + last_chunk->length = (size_t)(trailer_offset - last_chunk_offset); + + error = midx_parse_packfile_names( + idx, data, ntohl(hdr->packfiles), &chunk_packfile_names); + if (error < 0) + return error; + error = midx_parse_oid_fanout(idx, data, &chunk_oid_fanout); + if (error < 0) + return error; + error = midx_parse_oid_lookup(idx, data, &chunk_oid_lookup); + if (error < 0) + return error; + error = midx_parse_object_offsets(idx, data, &chunk_object_offsets); + if (error < 0) + return error; + error = midx_parse_object_large_offsets(idx, data, &chunk_object_large_offsets); + if (error < 0) + return error; + + return 0; +} + +int git_midx_open( + git_midx_file **idx_out, + const char *path, + git_oid_t oid_type) +{ + git_midx_file *idx; + git_file fd = -1; + size_t idx_size; + struct stat st; + int error; + + GIT_ASSERT_ARG(idx_out && path && oid_type); + + /* TODO: properly open the file without access time using O_NOATIME */ + fd = git_futils_open_ro(path); + if (fd < 0) + return fd; + + if (p_fstat(fd, &st) < 0) { + p_close(fd); + git_error_set(GIT_ERROR_ODB, "multi-pack-index file not found - '%s'", path); + return -1; + } + + if (!S_ISREG(st.st_mode) || !git__is_sizet(st.st_size)) { + p_close(fd); + git_error_set(GIT_ERROR_ODB, "invalid pack index '%s'", path); + return -1; + } + idx_size = (size_t)st.st_size; + + idx = git__calloc(1, sizeof(git_midx_file)); + GIT_ERROR_CHECK_ALLOC(idx); + + idx->oid_type = oid_type; + + error = git_str_sets(&idx->filename, path); + if (error < 0) + return error; + + error = git_futils_mmap_ro(&idx->index_map, fd, 0, idx_size); + p_close(fd); + if (error < 0) { + git_midx_free(idx); + return error; + } + + if ((error = git_midx_parse(idx, idx->index_map.data, idx_size)) < 0) { + git_midx_free(idx); + return error; + } + + *idx_out = idx; + return 0; +} + +bool git_midx_needs_refresh( + const git_midx_file *idx, + const char *path) +{ + git_file fd = -1; + struct stat st; + ssize_t bytes_read; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + size_t checksum_size; + + /* TODO: properly open the file without access time using O_NOATIME */ + fd = git_futils_open_ro(path); + if (fd < 0) + return true; + + if (p_fstat(fd, &st) < 0) { + p_close(fd); + return true; + } + + if (!S_ISREG(st.st_mode) || + !git__is_sizet(st.st_size) || + (size_t)st.st_size != idx->index_map.len) { + p_close(fd); + return true; + } + + checksum_size = git_oid_size(idx->oid_type); + bytes_read = p_pread(fd, checksum, checksum_size, st.st_size - checksum_size); + p_close(fd); + + if (bytes_read != (ssize_t)checksum_size) + return true; + + return (memcmp(checksum, idx->checksum, checksum_size) != 0); +} + +int git_midx_entry_find( + git_midx_entry *e, + git_midx_file *idx, + const git_oid *short_oid, + size_t len) +{ + int pos, found = 0; + size_t pack_index, oid_size, oid_hexsize; + uint32_t hi, lo; + unsigned char *current = NULL; + const unsigned char *object_offset; + off64_t offset; + + GIT_ASSERT_ARG(idx); + + oid_size = git_oid_size(idx->oid_type); + oid_hexsize = git_oid_hexsize(idx->oid_type); + + hi = ntohl(idx->oid_fanout[(int)short_oid->id[0]]); + lo = ((short_oid->id[0] == 0x0) ? 0 : ntohl(idx->oid_fanout[(int)short_oid->id[0] - 1])); + + pos = git_pack__lookup_id(idx->oid_lookup, oid_size, lo, hi, short_oid->id, idx->oid_type); + + if (pos >= 0) { + /* An object matching exactly the oid was found */ + found = 1; + current = idx->oid_lookup + (pos * oid_size); + } else { + /* No object was found */ + /* pos refers to the object with the "closest" oid to short_oid */ + pos = -1 - pos; + if (pos < (int)idx->num_objects) { + current = idx->oid_lookup + (pos * oid_size); + + if (!git_oid_raw_ncmp(short_oid->id, current, len)) + found = 1; + } + } + + if (found && len != oid_hexsize && pos + 1 < (int)idx->num_objects) { + /* Check for ambiguousity */ + const unsigned char *next = current + oid_size; + + if (!git_oid_raw_ncmp(short_oid->id, next, len)) + found = 2; + } + + if (!found) + return git_odb__error_notfound("failed to find offset for multi-pack index entry", short_oid, len); + if (found > 1) + return git_odb__error_ambiguous("found multiple offsets for multi-pack index entry"); + + object_offset = idx->object_offsets + pos * 8; + offset = ntohl(*((uint32_t *)(object_offset + 4))); + if (idx->object_large_offsets && offset & 0x80000000) { + uint32_t object_large_offsets_pos = (uint32_t) (offset ^ 0x80000000); + const unsigned char *object_large_offsets_index = idx->object_large_offsets; + + /* Make sure we're not being sent out of bounds */ + if (object_large_offsets_pos >= idx->num_object_large_offsets) + return git_odb__error_notfound("invalid index into the object large offsets table", short_oid, len); + + object_large_offsets_index += 8 * object_large_offsets_pos; + + offset = (((uint64_t)ntohl(*((uint32_t *)(object_large_offsets_index + 0)))) << 32) | + ntohl(*((uint32_t *)(object_large_offsets_index + 4))); + } + pack_index = ntohl(*((uint32_t *)(object_offset + 0))); + if (pack_index >= git_vector_length(&idx->packfile_names)) + return midx_error("invalid index into the packfile names table"); + e->pack_index = pack_index; + e->offset = offset; + git_oid_from_raw(&e->sha1, current, idx->oid_type); + return 0; +} + +int git_midx_foreach_entry( + git_midx_file *idx, + git_odb_foreach_cb cb, + void *data) +{ + git_oid oid; + size_t oid_size, i; + int error; + + GIT_ASSERT_ARG(idx); + + oid_size = git_oid_size(idx->oid_type); + + for (i = 0; i < idx->num_objects; ++i) { + if ((error = git_oid_from_raw(&oid, &idx->oid_lookup[i * oid_size], idx->oid_type)) < 0) + return error; + + if ((error = cb(&oid, data)) != 0) + return git_error_set_after_callback(error); + } + + return error; +} + +int git_midx_close(git_midx_file *idx) +{ + GIT_ASSERT_ARG(idx); + + if (idx->index_map.data) + git_futils_mmap_free(&idx->index_map); + + git_vector_dispose(&idx->packfile_names); + + return 0; +} + +void git_midx_free(git_midx_file *idx) +{ + if (!idx) + return; + + git_str_dispose(&idx->filename); + git_midx_close(idx); + git__free(idx); +} + +static int packfile__cmp(const void *a_, const void *b_) +{ + const struct git_pack_file *a = a_; + const struct git_pack_file *b = b_; + + return strcmp(a->pack_name, b->pack_name); +} + +int git_midx_writer_new( + git_midx_writer **out, + const char *pack_dir +#ifdef GIT_EXPERIMENTAL_SHA256 + , git_midx_writer_options *opts +#endif + ) +{ + git_midx_writer *w; + git_oid_t oid_type; + + GIT_ASSERT_ARG(out && pack_dir); + +#ifdef GIT_EXPERIMENTAL_SHA256 + GIT_ERROR_CHECK_VERSION(opts, + GIT_MIDX_WRITER_OPTIONS_VERSION, + "git_midx_writer_options"); + + oid_type = opts && opts->oid_type ? opts->oid_type : GIT_OID_DEFAULT; + GIT_ASSERT_ARG(git_oid_type_is_valid(oid_type)); +#else + oid_type = GIT_OID_SHA1; +#endif + + w = git__calloc(1, sizeof(git_midx_writer)); + GIT_ERROR_CHECK_ALLOC(w); + + if (git_str_sets(&w->pack_dir, pack_dir) < 0) { + git__free(w); + return -1; + } + git_fs_path_squash_slashes(&w->pack_dir); + + if (git_vector_init(&w->packs, 0, packfile__cmp) < 0) { + git_str_dispose(&w->pack_dir); + git__free(w); + return -1; + } + + w->oid_type = oid_type; + + *out = w; + return 0; +} + +void git_midx_writer_free(git_midx_writer *w) +{ + struct git_pack_file *p; + size_t i; + + if (!w) + return; + + git_vector_foreach (&w->packs, i, p) + git_mwindow_put_pack(p); + git_vector_dispose(&w->packs); + git_str_dispose(&w->pack_dir); + git__free(w); +} + +int git_midx_writer_add( + git_midx_writer *w, + const char *idx_path) +{ + git_str idx_path_buf = GIT_STR_INIT; + int error; + struct git_pack_file *p; + + error = git_fs_path_prettify(&idx_path_buf, idx_path, git_str_cstr(&w->pack_dir)); + if (error < 0) + return error; + + error = git_mwindow_get_pack(&p, git_str_cstr(&idx_path_buf), w->oid_type); + git_str_dispose(&idx_path_buf); + if (error < 0) + return error; + + error = git_vector_insert(&w->packs, p); + if (error < 0) { + git_mwindow_put_pack(p); + return error; + } + + return 0; +} + +typedef git_array_t(git_midx_entry) object_entry_array_t; + +struct object_entry_cb_state { + uint32_t pack_index; + object_entry_array_t *object_entries_array; +}; + +static int object_entry__cb(const git_oid *oid, off64_t offset, void *data) +{ + struct object_entry_cb_state *state = (struct object_entry_cb_state *)data; + + git_midx_entry *entry = git_array_alloc(*state->object_entries_array); + GIT_ERROR_CHECK_ALLOC(entry); + + git_oid_cpy(&entry->sha1, oid); + entry->offset = offset; + entry->pack_index = state->pack_index; + + return 0; +} + +static int object_entry__cmp(const void *a_, const void *b_) +{ + const git_midx_entry *a = (const git_midx_entry *)a_; + const git_midx_entry *b = (const git_midx_entry *)b_; + + return git_oid_cmp(&a->sha1, &b->sha1); +} + +static int write_offset(off64_t offset, midx_write_cb write_cb, void *cb_data) +{ + int error; + uint32_t word; + + word = htonl((uint32_t)((offset >> 32) & 0xffffffffu)); + error = write_cb((const char *)&word, sizeof(word), cb_data); + if (error < 0) + return error; + word = htonl((uint32_t)((offset >> 0) & 0xffffffffu)); + error = write_cb((const char *)&word, sizeof(word), cb_data); + if (error < 0) + return error; + + return 0; +} + +static int write_chunk_header(int chunk_id, off64_t offset, midx_write_cb write_cb, void *cb_data) +{ + uint32_t word = htonl(chunk_id); + int error = write_cb((const char *)&word, sizeof(word), cb_data); + if (error < 0) + return error; + return write_offset(offset, write_cb, cb_data); + + return 0; +} + +static int midx_write_buf(const char *buf, size_t size, void *data) +{ + git_str *b = (git_str *)data; + return git_str_put(b, buf, size); +} + +struct midx_write_hash_context { + midx_write_cb write_cb; + void *cb_data; + git_hash_ctx *ctx; +}; + +static int midx_write_hash(const char *buf, size_t size, void *data) +{ + struct midx_write_hash_context *ctx = (struct midx_write_hash_context *)data; + int error; + + if (ctx->ctx) { + error = git_hash_update(ctx->ctx, buf, size); + if (error < 0) + return error; + } + + return ctx->write_cb(buf, size, ctx->cb_data); +} + +static int midx_write( + git_midx_writer *w, + midx_write_cb write_cb, + void *cb_data) +{ + int error = 0; + size_t i; + struct git_pack_file *p; + struct git_midx_header hdr = {0}; + uint32_t oid_fanout_count; + uint32_t object_large_offsets_count; + uint32_t oid_fanout[256]; + off64_t offset; + git_str packfile_names = GIT_STR_INIT, + oid_lookup = GIT_STR_INIT, + object_offsets = GIT_STR_INIT, + object_large_offsets = GIT_STR_INIT; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + size_t checksum_size, oid_size; + git_midx_entry *entry; + object_entry_array_t object_entries_array = GIT_ARRAY_INIT; + git_vector object_entries = GIT_VECTOR_INIT; + git_hash_ctx ctx; + git_hash_algorithm_t checksum_type; + struct midx_write_hash_context hash_cb_data = {0}; + + hdr.signature = htonl(MIDX_SIGNATURE); + hdr.version = MIDX_VERSION; + hdr.object_id_version = MIDX_OBJECT_ID_VERSION; + hdr.base_midx_files = 0; + + hash_cb_data.write_cb = write_cb; + hash_cb_data.cb_data = cb_data; + hash_cb_data.ctx = &ctx; + + oid_size = git_oid_size(w->oid_type); + checksum_type = git_oid_algorithm(w->oid_type); + checksum_size = git_hash_size(checksum_type); + GIT_ASSERT(oid_size && checksum_type && checksum_size); + + if ((error = git_hash_ctx_init(&ctx, checksum_type)) < 0) + return error; + + cb_data = &hash_cb_data; + write_cb = midx_write_hash; + + git_vector_sort(&w->packs); + git_vector_foreach (&w->packs, i, p) { + git_str relative_index = GIT_STR_INIT; + struct object_entry_cb_state state = {0}; + size_t path_len; + + state.pack_index = (uint32_t)i; + state.object_entries_array = &object_entries_array; + + error = git_str_sets(&relative_index, p->pack_name); + if (error < 0) + goto cleanup; + error = git_fs_path_make_relative(&relative_index, git_str_cstr(&w->pack_dir)); + if (error < 0) { + git_str_dispose(&relative_index); + goto cleanup; + } + path_len = git_str_len(&relative_index); + if (path_len <= strlen(".pack") || git__suffixcmp(git_str_cstr(&relative_index), ".pack") != 0) { + git_str_dispose(&relative_index); + git_error_set(GIT_ERROR_INVALID, "invalid packfile name: '%s'", p->pack_name); + error = -1; + goto cleanup; + } + path_len -= strlen(".pack"); + + git_str_put(&packfile_names, git_str_cstr(&relative_index), path_len); + git_str_puts(&packfile_names, ".idx"); + git_str_putc(&packfile_names, '\0'); + git_str_dispose(&relative_index); + + error = git_pack_foreach_entry_offset(p, object_entry__cb, &state); + if (error < 0) + goto cleanup; + } + + /* Sort the object entries. */ + error = git_vector_init(&object_entries, git_array_size(object_entries_array), object_entry__cmp); + if (error < 0) + goto cleanup; + git_array_foreach (object_entries_array, i, entry) { + if ((error = git_vector_set(NULL, &object_entries, i, entry)) < 0) + goto cleanup; + } + git_vector_set_sorted(&object_entries, 0); + git_vector_sort(&object_entries); + git_vector_uniq(&object_entries, NULL); + + /* Pad the packfile names so it is a multiple of four. */ + while (git_str_len(&packfile_names) & 3) + git_str_putc(&packfile_names, '\0'); + + /* Fill the OID Fanout table. */ + oid_fanout_count = 0; + for (i = 0; i < 256; i++) { + while (oid_fanout_count < git_vector_length(&object_entries) && + ((const git_midx_entry *)git_vector_get(&object_entries, oid_fanout_count))->sha1.id[0] <= i) + ++oid_fanout_count; + oid_fanout[i] = htonl(oid_fanout_count); + } + + /* Fill the OID Lookup table. */ + git_vector_foreach (&object_entries, i, entry) { + error = git_str_put(&oid_lookup, + (char *)&entry->sha1.id, oid_size); + + if (error < 0) + goto cleanup; + } + + /* Fill the Object Offsets and Object Large Offsets tables. */ + object_large_offsets_count = 0; + git_vector_foreach (&object_entries, i, entry) { + uint32_t word; + + word = htonl((uint32_t)entry->pack_index); + error = git_str_put(&object_offsets, (const char *)&word, sizeof(word)); + if (error < 0) + goto cleanup; + if (entry->offset >= 0x80000000l) { + word = htonl(0x80000000u | object_large_offsets_count++); + if ((error = write_offset(entry->offset, midx_write_buf, &object_large_offsets)) < 0) + goto cleanup; + } else { + word = htonl((uint32_t)entry->offset & 0x7fffffffu); + } + + error = git_str_put(&object_offsets, (const char *)&word, sizeof(word)); + if (error < 0) + goto cleanup; + } + + /* Write the header. */ + hdr.packfiles = htonl((uint32_t)git_vector_length(&w->packs)); + hdr.chunks = 4; + if (git_str_len(&object_large_offsets) > 0) + hdr.chunks++; + error = write_cb((const char *)&hdr, sizeof(hdr), cb_data); + if (error < 0) + goto cleanup; + + /* Write the chunk headers. */ + offset = sizeof(hdr) + (hdr.chunks + 1) * 12; + error = write_chunk_header(MIDX_PACKFILE_NAMES_ID, offset, write_cb, cb_data); + if (error < 0) + goto cleanup; + offset += git_str_len(&packfile_names); + error = write_chunk_header(MIDX_OID_FANOUT_ID, offset, write_cb, cb_data); + if (error < 0) + goto cleanup; + offset += sizeof(oid_fanout); + error = write_chunk_header(MIDX_OID_LOOKUP_ID, offset, write_cb, cb_data); + if (error < 0) + goto cleanup; + offset += git_str_len(&oid_lookup); + error = write_chunk_header(MIDX_OBJECT_OFFSETS_ID, offset, write_cb, cb_data); + if (error < 0) + goto cleanup; + offset += git_str_len(&object_offsets); + if (git_str_len(&object_large_offsets) > 0) { + error = write_chunk_header(MIDX_OBJECT_LARGE_OFFSETS_ID, offset, write_cb, cb_data); + if (error < 0) + goto cleanup; + offset += git_str_len(&object_large_offsets); + } + error = write_chunk_header(0, offset, write_cb, cb_data); + if (error < 0) + goto cleanup; + + /* Write all the chunks. */ + error = write_cb(git_str_cstr(&packfile_names), git_str_len(&packfile_names), cb_data); + if (error < 0) + goto cleanup; + error = write_cb((const char *)oid_fanout, sizeof(oid_fanout), cb_data); + if (error < 0) + goto cleanup; + error = write_cb(git_str_cstr(&oid_lookup), git_str_len(&oid_lookup), cb_data); + if (error < 0) + goto cleanup; + error = write_cb(git_str_cstr(&object_offsets), git_str_len(&object_offsets), cb_data); + if (error < 0) + goto cleanup; + error = write_cb(git_str_cstr(&object_large_offsets), git_str_len(&object_large_offsets), cb_data); + if (error < 0) + goto cleanup; + + /* Finalize the checksum and write the trailer. */ + error = git_hash_final(checksum, &ctx); + if (error < 0) + goto cleanup; + + hash_cb_data.ctx = NULL; + + error = write_cb((char *)checksum, checksum_size, cb_data); + if (error < 0) + goto cleanup; + +cleanup: + git_array_clear(object_entries_array); + git_vector_dispose(&object_entries); + git_str_dispose(&packfile_names); + git_str_dispose(&oid_lookup); + git_str_dispose(&object_offsets); + git_str_dispose(&object_large_offsets); + git_hash_ctx_cleanup(&ctx); + return error; +} + +static int midx_write_filebuf(const char *buf, size_t size, void *data) +{ + git_filebuf *f = (git_filebuf *)data; + return git_filebuf_write(f, buf, size); +} + +int git_midx_writer_commit( + git_midx_writer *w) +{ + int error; + int filebuf_flags = GIT_FILEBUF_DO_NOT_BUFFER; + git_str midx_path = GIT_STR_INIT; + git_filebuf output = GIT_FILEBUF_INIT; + + error = git_str_joinpath(&midx_path, git_str_cstr(&w->pack_dir), "multi-pack-index"); + if (error < 0) + return error; + + if (git_repository__fsync_gitdir) + filebuf_flags |= GIT_FILEBUF_FSYNC; + error = git_filebuf_open(&output, git_str_cstr(&midx_path), filebuf_flags, 0644); + git_str_dispose(&midx_path); + if (error < 0) + return error; + + error = midx_write(w, midx_write_filebuf, &output); + if (error < 0) { + git_filebuf_cleanup(&output); + return error; + } + + return git_filebuf_commit(&output); +} + +int git_midx_writer_dump( + git_buf *midx, + git_midx_writer *w) +{ + git_str str = GIT_STR_INIT; + int error; + + if ((error = git_buf_tostr(&str, midx)) < 0 || + (error = midx_write(w, midx_write_buf, &str)) == 0) + error = git_buf_fromstr(midx, &str); + + git_str_dispose(&str); + return error; +} diff --git a/src/libgit2/midx.h b/src/libgit2/midx.h new file mode 100644 index 00000000000..5107f69cba3 --- /dev/null +++ b/src/libgit2/midx.h @@ -0,0 +1,121 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_midx_h__ +#define INCLUDE_midx_h__ + +#include "common.h" + +#include + +#include "git2/sys/midx.h" + +#include "map.h" +#include "mwindow.h" +#include "odb.h" +#include "oid.h" + +/* + * A multi-pack-index file. + * + * This file contains a merged index for multiple independent .pack files. This + * can help speed up locating objects without requiring a garbage collection + * cycle to create a single .pack file. + * + * Support for this feature was added in git 2.21, and requires the + * `core.multiPackIndex` config option to be set. + */ +typedef struct git_midx_file { + git_map index_map; + + /* The table of Packfile Names. */ + git_vector packfile_names; + + /* The OID Fanout table. */ + const uint32_t *oid_fanout; + /* The total number of objects in the index. */ + uint32_t num_objects; + + /* The OID Lookup table. */ + unsigned char *oid_lookup; + + /* The Object Offsets table. Each entry has two 4-byte fields with the pack index and the offset. */ + const unsigned char *object_offsets; + + /* The Object Large Offsets table. */ + const unsigned char *object_large_offsets; + /* The number of entries in the Object Large Offsets table. Each entry has an 8-byte with an offset */ + size_t num_object_large_offsets; + + /* + * The trailer of the file. Contains the checksum of the whole + * file, in the repository's object format hash. + */ + unsigned char checksum[GIT_HASH_MAX_SIZE]; + + /* The type of object IDs in the midx. */ + git_oid_t oid_type; + + /* something like ".git/objects/pack/multi-pack-index". */ + git_str filename; +} git_midx_file; + +/* + * An entry in the multi-pack-index file. Similar in purpose to git_pack_entry. + */ +typedef struct git_midx_entry { + /* The index within idx->packfile_names where the packfile name can be found. */ + size_t pack_index; + /* The offset within the .pack file where the requested object is found. */ + off64_t offset; + /* The SHA-1 hash of the requested object. */ + git_oid sha1; +} git_midx_entry; + +/* + * A writer for `multi-pack-index` files. + */ +struct git_midx_writer { + /* + * The path of the directory where the .pack/.idx files are stored. The + * `multi-pack-index` file will be written to the same directory. + */ + git_str pack_dir; + + /* The list of `git_pack_file`s. */ + git_vector packs; + + /* The object ID type of the writer. */ + git_oid_t oid_type; +}; + +int git_midx_open( + git_midx_file **idx_out, + const char *path, + git_oid_t oid_type); +bool git_midx_needs_refresh( + const git_midx_file *idx, + const char *path); +int git_midx_entry_find( + git_midx_entry *e, + git_midx_file *idx, + const git_oid *short_oid, + size_t len); +int git_midx_foreach_entry( + git_midx_file *idx, + git_odb_foreach_cb cb, + void *data); +int git_midx_close(git_midx_file *idx); +void git_midx_free(git_midx_file *idx); + +/* This is exposed for use in the fuzzers. */ +int git_midx_parse( + git_midx_file *idx, + const unsigned char *data, + size_t size); + +#endif diff --git a/src/libgit2/mwindow.c b/src/libgit2/mwindow.c new file mode 100644 index 00000000000..7e4054df664 --- /dev/null +++ b/src/libgit2/mwindow.c @@ -0,0 +1,537 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "mwindow.h" + +#include "vector.h" +#include "futils.h" +#include "map.h" +#include "runtime.h" +#include "pack.h" + +#define DEFAULT_WINDOW_SIZE \ + (sizeof(void*) >= 8 \ + ? 1 * 1024 * 1024 * 1024 \ + : 32 * 1024 * 1024) + +#define DEFAULT_MAPPED_LIMIT \ + ((1024 * 1024) * (sizeof(void*) >= 8 ? UINT64_C(8192) : UINT64_C(256))) + +/* default is unlimited */ +#define DEFAULT_FILE_LIMIT 0 + +size_t git_mwindow__window_size = DEFAULT_WINDOW_SIZE; +size_t git_mwindow__mapped_limit = DEFAULT_MAPPED_LIMIT; +size_t git_mwindow__file_limit = DEFAULT_FILE_LIMIT; + +/* Mutex to control access to `git_mwindow__mem_ctl` and `git_mwindow__pack_cache`. */ +git_mutex git_mwindow__mutex; + +/* Whenever you want to read or modify this, grab `git_mwindow__mutex` */ +git_mwindow_ctl git_mwindow__mem_ctl; + +/* Global list of mwindow files, to open packs once across repos */ +GIT_HASHMAP_STR_FUNCTIONS(git_mwindow_packmap, , struct git_pack_file *); +git_mwindow_packmap git_mwindow__pack_cache; + +static void git_mwindow_global_shutdown(void) +{ + git_mutex_free(&git_mwindow__mutex); + git_mwindow_packmap_dispose(&git_mwindow__pack_cache); +} + +int git_mwindow_global_init(void) +{ + int error; + + if ((error = git_mutex_init(&git_mwindow__mutex)) < 0) + return error; + + return git_runtime_shutdown_register(git_mwindow_global_shutdown); +} + +int git_mwindow_get_pack( + struct git_pack_file **out, + const char *path, + git_oid_t oid_type) +{ + struct git_pack_file *pack; + char *packname; + int error; + + if ((error = git_packfile__name(&packname, path)) < 0) + return error; + + if (git_mutex_lock(&git_mwindow__mutex) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock mwindow mutex"); + return -1; + } + + error = git_mwindow_packmap_get(&pack, &git_mwindow__pack_cache, packname); + git__free(packname); + + if (error == 0) { + git_atomic32_inc(&pack->refcount); + git_mutex_unlock(&git_mwindow__mutex); + *out = pack; + return 0; + } else if (error != GIT_ENOTFOUND) { + return error; + } + + /* If we didn't find it, we need to create it */ + if ((error = git_packfile_alloc(&pack, path, oid_type)) < 0) { + git_mutex_unlock(&git_mwindow__mutex); + return error; + } + + git_atomic32_inc(&pack->refcount); + + error = git_mwindow_packmap_put(&git_mwindow__pack_cache, pack->pack_name, pack); + git_mutex_unlock(&git_mwindow__mutex); + + if (error < 0) { + git_packfile_free(pack, false); + return error; + } + + *out = pack; + return 0; +} + +int git_mwindow_put_pack(struct git_pack_file *pack) +{ + int count, error; + struct git_pack_file *pack_to_delete = NULL; + + if ((error = git_mutex_lock(&git_mwindow__mutex)) < 0) + return error; + + /* if we cannot find it, the state is corrupted */ + GIT_ASSERT(git_mwindow_packmap_contains(&git_mwindow__pack_cache, pack->pack_name)); + + count = git_atomic32_dec(&pack->refcount); + if (count == 0) { + git_mwindow_packmap_remove(&git_mwindow__pack_cache, pack->pack_name); + pack_to_delete = pack; + } + git_mutex_unlock(&git_mwindow__mutex); + git_packfile_free(pack_to_delete, false); + + return 0; +} + +/* + * Free all the windows in a sequence, typically because we're done + * with the file. Needs to hold the git_mwindow__mutex. + */ +static int git_mwindow_free_all_locked(git_mwindow_file *mwf) +{ + git_mwindow_ctl *ctl = &git_mwindow__mem_ctl; + size_t i; + + /* + * Remove these windows from the global list + */ + for (i = 0; i < ctl->windowfiles.length; ++i){ + if (git_vector_get(&ctl->windowfiles, i) == mwf) { + git_vector_remove(&ctl->windowfiles, i); + break; + } + } + + if (ctl->windowfiles.length == 0) { + git_vector_dispose(&ctl->windowfiles); + ctl->windowfiles.contents = NULL; + } + + while (mwf->windows) { + git_mwindow *w = mwf->windows; + GIT_ASSERT(w->inuse_cnt == 0); + + ctl->mapped -= w->window_map.len; + ctl->open_windows--; + + git_futils_mmap_free(&w->window_map); + + mwf->windows = w->next; + git__free(w); + } + + return 0; +} + +int git_mwindow_free_all(git_mwindow_file *mwf) +{ + int error; + + if (git_mutex_lock(&git_mwindow__mutex)) { + git_error_set(GIT_ERROR_THREAD, "unable to lock mwindow mutex"); + return -1; + } + + error = git_mwindow_free_all_locked(mwf); + + git_mutex_unlock(&git_mwindow__mutex); + + return error; +} + +/* + * Check if a window 'win' contains both the address 'offset' and 'extra'. + * + * 'extra' is the size of the hash we're using as we always want to make sure + * that it's contained. + */ +int git_mwindow_contains(git_mwindow *win, off64_t offset, off64_t extra) +{ + off64_t win_off = win->offset; + return win_off <= offset + && (offset + extra) <= (off64_t)(win_off + win->window_map.len); +} + +#define GIT_MWINDOW__LRU -1 +#define GIT_MWINDOW__MRU 1 + +/* + * Find the least- or most-recently-used window in a file that is not currently + * being used. The 'only_unused' flag controls whether the caller requires the + * file to only have unused windows. If '*out_window' is non-null, it is used as + * a starting point for the comparison. + * + * Returns whether such a window was found in the file. + */ +static bool git_mwindow_scan_recently_used( + git_mwindow_file *mwf, + git_mwindow **out_window, + git_mwindow **out_last, + bool only_unused, + int comparison_sign) +{ + git_mwindow *w, *w_last; + git_mwindow *lru_window = NULL, *lru_last = NULL; + bool found = false; + + GIT_ASSERT_ARG(mwf); + GIT_ASSERT_ARG(out_window); + + lru_window = *out_window; + if (out_last) + lru_last = *out_last; + + for (w_last = NULL, w = mwf->windows; w; w_last = w, w = w->next) { + if (w->inuse_cnt) { + if (only_unused) + return false; + /* This window is currently being used. Skip it. */ + continue; + } + + /* + * If the current one is more (or less) recent than the last one, + * store it in the output parameter. If lru_window is NULL, + * it's the first loop, so store it as well. + */ + if (!lru_window || (comparison_sign * w->last_used) > lru_window->last_used) { + lru_window = w; + lru_last = w_last; + found = true; + } + } + + if (!found) + return false; + + *out_window = lru_window; + if (out_last) + *out_last = lru_last; + return true; +} + +/* + * Close the least recently used window (that is currently not being used) out + * of all the files. Called under lock from new_window_locked. + */ +static int git_mwindow_close_lru_window_locked(void) +{ + git_mwindow_ctl *ctl = &git_mwindow__mem_ctl; + git_mwindow_file *cur; + size_t i; + git_mwindow *lru_window = NULL, *lru_last = NULL, **list = NULL; + + git_vector_foreach(&ctl->windowfiles, i, cur) { + if (git_mwindow_scan_recently_used( + cur, &lru_window, &lru_last, false, GIT_MWINDOW__LRU)) { + list = &cur->windows; + } + } + + if (!lru_window) { + git_error_set(GIT_ERROR_OS, "failed to close memory window; couldn't find LRU"); + return -1; + } + + ctl->mapped -= lru_window->window_map.len; + git_futils_mmap_free(&lru_window->window_map); + + if (lru_last) + lru_last->next = lru_window->next; + else + *list = lru_window->next; + + git__free(lru_window); + ctl->open_windows--; + + return 0; +} + +/* + * Finds the file that does not have any open windows AND whose + * most-recently-used window is the least-recently used one across all + * currently open files. + * + * Called under lock from new_window_locked. + */ +static int git_mwindow_find_lru_file_locked(git_mwindow_file **out) +{ + git_mwindow_ctl *ctl = &git_mwindow__mem_ctl; + git_mwindow_file *lru_file = NULL, *current_file = NULL; + git_mwindow *lru_window = NULL; + size_t i; + + git_vector_foreach(&ctl->windowfiles, i, current_file) { + git_mwindow *mru_window = NULL; + if (!git_mwindow_scan_recently_used( + current_file, &mru_window, NULL, true, GIT_MWINDOW__MRU)) { + continue; + } + if (!lru_window || lru_window->last_used > mru_window->last_used) { + lru_window = mru_window; + lru_file = current_file; + } + } + + if (!lru_file) { + git_error_set(GIT_ERROR_OS, "failed to close memory window file; couldn't find LRU"); + return -1; + } + + *out = lru_file; + return 0; +} + +/* This gets called under lock from git_mwindow_open */ +static git_mwindow *new_window_locked( + git_file fd, + off64_t size, + off64_t offset) +{ + git_mwindow_ctl *ctl = &git_mwindow__mem_ctl; + size_t walign = git_mwindow__window_size / 2; + off64_t len; + git_mwindow *w; + + w = git__calloc(1, sizeof(*w)); + + if (w == NULL) + return NULL; + + w->offset = (offset / walign) * walign; + + len = size - w->offset; + if (len > (off64_t)git_mwindow__window_size) + len = (off64_t)git_mwindow__window_size; + + ctl->mapped += (size_t)len; + + while (git_mwindow__mapped_limit < ctl->mapped && + git_mwindow_close_lru_window_locked() == 0) /* nop */; + + /* + * We treat `mapped_limit` as a soft limit. If we can't find a + * window to close and are above the limit, we still mmap the new + * window. + */ + + if (git_futils_mmap_ro(&w->window_map, fd, w->offset, (size_t)len) < 0) { + /* + * The first error might be down to memory fragmentation even if + * we're below our soft limits, so free up what we can and try again. + */ + + while (git_mwindow_close_lru_window_locked() == 0) + /* nop */; + + if (git_futils_mmap_ro(&w->window_map, fd, w->offset, (size_t)len) < 0) { + git__free(w); + return NULL; + } + } + + ctl->mmap_calls++; + ctl->open_windows++; + + if (ctl->mapped > ctl->peak_mapped) + ctl->peak_mapped = ctl->mapped; + + if (ctl->open_windows > ctl->peak_open_windows) + ctl->peak_open_windows = ctl->open_windows; + + return w; +} + +/* + * Open a new window, closing the least recenty used until we have + * enough space. Don't forget to add it to your list + */ +unsigned char *git_mwindow_open( + git_mwindow_file *mwf, + git_mwindow **cursor, + off64_t offset, + size_t extra, + unsigned int *left) +{ + git_mwindow_ctl *ctl = &git_mwindow__mem_ctl; + git_mwindow *w = *cursor; + + if (git_mutex_lock(&git_mwindow__mutex)) { + git_error_set(GIT_ERROR_THREAD, "unable to lock mwindow mutex"); + return NULL; + } + + if (!w || !(git_mwindow_contains(w, offset, extra))) { + if (w) { + w->inuse_cnt--; + } + + for (w = mwf->windows; w; w = w->next) { + if (git_mwindow_contains(w, offset, extra)) + break; + } + + /* + * If there isn't a suitable window, we need to create a new + * one. + */ + if (!w) { + w = new_window_locked(mwf->fd, mwf->size, offset); + if (w == NULL) { + git_mutex_unlock(&git_mwindow__mutex); + return NULL; + } + w->next = mwf->windows; + mwf->windows = w; + } + } + + /* If we changed w, store it in the cursor */ + if (w != *cursor) { + w->last_used = ctl->used_ctr++; + w->inuse_cnt++; + *cursor = w; + } + + offset -= w->offset; + + if (left) + *left = (unsigned int)(w->window_map.len - offset); + + git_mutex_unlock(&git_mwindow__mutex); + return (unsigned char *) w->window_map.data + offset; +} + +int git_mwindow_file_register(git_mwindow_file *mwf) +{ + git_vector closed_files = GIT_VECTOR_INIT; + git_mwindow_ctl *ctl = &git_mwindow__mem_ctl; + int error; + size_t i; + git_mwindow_file *closed_file = NULL; + + if (git_mutex_lock(&git_mwindow__mutex)) { + git_error_set(GIT_ERROR_THREAD, "unable to lock mwindow mutex"); + return -1; + } + + if (ctl->windowfiles.length == 0 && + (error = git_vector_init(&ctl->windowfiles, 8, NULL)) < 0) { + git_mutex_unlock(&git_mwindow__mutex); + goto cleanup; + } + + if (git_mwindow__file_limit) { + git_mwindow_file *lru_file; + while (git_mwindow__file_limit <= ctl->windowfiles.length && + git_mwindow_find_lru_file_locked(&lru_file) == 0) { + if ((error = git_vector_insert(&closed_files, lru_file)) < 0) { + /* + * Exceeding the file limit seems preferable to being open to + * data races that can end up corrupting the heap. + */ + break; + } + git_mwindow_free_all_locked(lru_file); + } + } + + error = git_vector_insert(&ctl->windowfiles, mwf); + git_mutex_unlock(&git_mwindow__mutex); + if (error < 0) + goto cleanup; + + /* + * Once we have released the global windowfiles lock, we can close each + * individual file. Before doing so, acquire that file's lock to avoid + * closing a file that is currently being used. + */ + git_vector_foreach(&closed_files, i, closed_file) { + error = git_mutex_lock(&closed_file->lock); + if (error < 0) + continue; + p_close(closed_file->fd); + closed_file->fd = -1; + git_mutex_unlock(&closed_file->lock); + } + +cleanup: + git_vector_dispose(&closed_files); + return error; +} + +void git_mwindow_file_deregister(git_mwindow_file *mwf) +{ + git_mwindow_ctl *ctl = &git_mwindow__mem_ctl; + git_mwindow_file *cur; + size_t i; + + if (git_mutex_lock(&git_mwindow__mutex)) + return; + + git_vector_foreach(&ctl->windowfiles, i, cur) { + if (cur == mwf) { + git_vector_remove(&ctl->windowfiles, i); + git_mutex_unlock(&git_mwindow__mutex); + return; + } + } + git_mutex_unlock(&git_mwindow__mutex); +} + +void git_mwindow_close(git_mwindow **window) +{ + git_mwindow *w = *window; + if (w) { + if (git_mutex_lock(&git_mwindow__mutex)) { + git_error_set(GIT_ERROR_THREAD, "unable to lock mwindow mutex"); + return; + } + + w->inuse_cnt--; + git_mutex_unlock(&git_mwindow__mutex); + *window = NULL; + } +} diff --git a/src/libgit2/mwindow.h b/src/libgit2/mwindow.h new file mode 100644 index 00000000000..43352c4196b --- /dev/null +++ b/src/libgit2/mwindow.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_mwindow__ +#define INCLUDE_mwindow__ + +#include "common.h" + +#include "map.h" +#include "vector.h" +#include "hashmap_str.h" + +GIT_HASHMAP_STR_STRUCT(git_mwindow_packmap, struct git_pack_file *); +GIT_HASHMAP_STR_PROTOTYPES(git_mwindow_packmap, struct git_pack_file *); + +typedef struct git_mwindow { + struct git_mwindow *next; + git_map window_map; + off64_t offset; + size_t last_used; + size_t inuse_cnt; +} git_mwindow; + +typedef struct git_mwindow_file { + git_mutex lock; /* protects updates to fd */ + git_mwindow *windows; + int fd; + off64_t size; +} git_mwindow_file; + +typedef struct git_mwindow_ctl { + size_t mapped; + unsigned int open_windows; + unsigned int mmap_calls; + unsigned int peak_open_windows; + size_t peak_mapped; + size_t used_ctr; + git_vector windowfiles; +} git_mwindow_ctl; + +int git_mwindow_contains(git_mwindow *win, off64_t offset, off64_t extra); +int git_mwindow_free_all(git_mwindow_file *mwf); /* locks */ +unsigned char *git_mwindow_open(git_mwindow_file *mwf, git_mwindow **cursor, off64_t offset, size_t extra, unsigned int *left); +int git_mwindow_file_register(git_mwindow_file *mwf); +void git_mwindow_file_deregister(git_mwindow_file *mwf); +void git_mwindow_close(git_mwindow **w_cursor); + +extern int git_mwindow_global_init(void); + +struct git_pack_file; /* just declaration to avoid cyclical includes */ +int git_mwindow_get_pack( + struct git_pack_file **out, + const char *path, + git_oid_t oid_type); +int git_mwindow_put_pack(struct git_pack_file *pack); + +#endif diff --git a/src/libgit2/notes.c b/src/libgit2/notes.c new file mode 100644 index 00000000000..393c1363a39 --- /dev/null +++ b/src/libgit2/notes.c @@ -0,0 +1,810 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "notes.h" + +#include "buf.h" +#include "refs.h" +#include "config.h" +#include "iterator.h" +#include "signature.h" +#include "blob.h" + +static int note_error_notfound(void) +{ + git_error_set(GIT_ERROR_INVALID, "note could not be found"); + return GIT_ENOTFOUND; +} + +static int find_subtree_in_current_level( + git_tree **out, + git_repository *repo, + git_tree *parent, + const char *annotated_object_sha, + int fanout) +{ + size_t i; + const git_tree_entry *entry; + + *out = NULL; + + if (parent == NULL) + return note_error_notfound(); + + for (i = 0; i < git_tree_entrycount(parent); i++) { + entry = git_tree_entry_byindex(parent, i); + + if (!git__ishex(git_tree_entry_name(entry))) + continue; + + if (S_ISDIR(git_tree_entry_filemode(entry)) + && strlen(git_tree_entry_name(entry)) == 2 + && !strncmp(git_tree_entry_name(entry), annotated_object_sha + fanout, 2)) + return git_tree_lookup(out, repo, git_tree_entry_id(entry)); + + /* Not a DIR, so do we have an already existing blob? */ + if (!strcmp(git_tree_entry_name(entry), annotated_object_sha + fanout)) + return GIT_EEXISTS; + } + + return note_error_notfound(); +} + +static int find_subtree_r(git_tree **out, git_tree *root, + git_repository *repo, const char *target, int *fanout) +{ + int error; + git_tree *subtree = NULL; + + *out = NULL; + + error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout); + if (error == GIT_EEXISTS) + return git_tree_lookup(out, repo, git_tree_id(root)); + + if (error < 0) + return error; + + *fanout += 2; + error = find_subtree_r(out, subtree, repo, target, fanout); + git_tree_free(subtree); + + return error; +} + +static int find_blob(git_oid *blob, git_tree *tree, const char *target) +{ + size_t i; + const git_tree_entry *entry; + + for (i=0; iid, note_oid); + + if (git_signature_dup(¬e->author, git_commit_author(commit)) < 0 || + git_signature_dup(¬e->committer, git_commit_committer(commit)) < 0) + return -1; + + blobsize = git_blob_rawsize(blob); + GIT_ERROR_CHECK_BLOBSIZE(blobsize); + + note->message = git__strndup(git_blob_rawcontent(blob), (size_t)blobsize); + GIT_ERROR_CHECK_ALLOC(note->message); + + *out = note; + return 0; +} + +static int note_lookup( + git_note **out, + git_repository *repo, + git_commit *commit, + git_tree *tree, + const char *target) +{ + int error, fanout = 0; + git_oid oid; + git_blob *blob = NULL; + git_note *note = NULL; + git_tree *subtree = NULL; + + if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0) + goto cleanup; + + if ((error = find_blob(&oid, subtree, target + fanout)) < 0) + goto cleanup; + + if ((error = git_blob_lookup(&blob, repo, &oid)) < 0) + goto cleanup; + + if ((error = note_new(¬e, &oid, commit, blob)) < 0) + goto cleanup; + + *out = note; + +cleanup: + git_tree_free(subtree); + git_blob_free(blob); + return error; +} + +static int note_remove( + git_oid *notes_commit_out, + git_repository *repo, + const git_signature *author, const git_signature *committer, + const char *notes_ref, git_tree *tree, + const char *target, git_commit **parents) +{ + int error; + git_tree *tree_after_removal = NULL; + git_oid oid; + + if ((error = manipulate_note_in_tree_r( + &tree_after_removal, repo, tree, NULL, target, 0, + remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0) + goto cleanup; + + error = git_commit_create(&oid, repo, notes_ref, author, committer, + NULL, GIT_NOTES_DEFAULT_MSG_RM, + tree_after_removal, + *parents == NULL ? 0 : 1, + (const git_commit **) parents); + + if (error < 0) + goto cleanup; + + if (notes_commit_out) + git_oid_cpy(notes_commit_out, &oid); + +cleanup: + git_tree_free(tree_after_removal); + return error; +} + +static int note_get_default_ref(git_str *out, git_repository *repo) +{ + git_config *cfg; + int error; + + if ((error = git_repository_config__weakptr(&cfg, repo)) < 0) + return error; + + error = git_config__get_string_buf(out, cfg, "core.notesref"); + + if (error == GIT_ENOTFOUND) + error = git_str_puts(out, GIT_NOTES_DEFAULT_REF); + + return error; +} + +static int normalize_namespace(git_str *out, git_repository *repo, const char *notes_ref) +{ + if (notes_ref) + return git_str_puts(out, notes_ref); + + return note_get_default_ref(out, repo); +} + +static int retrieve_note_commit( + git_commit **commit_out, + git_str *notes_ref_out, + git_repository *repo, + const char *notes_ref) +{ + int error; + git_oid oid; + + if ((error = normalize_namespace(notes_ref_out, repo, notes_ref)) < 0) + return error; + + if ((error = git_reference_name_to_id(&oid, repo, notes_ref_out->ptr)) < 0) + return error; + + if (git_commit_lookup(commit_out, repo, &oid) < 0) + return error; + + return 0; +} + +int git_note_commit_read( + git_note **out, + git_repository *repo, + git_commit *notes_commit, + const git_oid *oid) +{ + int error; + git_tree *tree = NULL; + char target[GIT_OID_MAX_HEXSIZE + 1]; + + git_oid_tostr(target, sizeof(target), oid); + + if ((error = git_commit_tree(&tree, notes_commit)) < 0) + goto cleanup; + + error = note_lookup(out, repo, notes_commit, tree, target); + +cleanup: + git_tree_free(tree); + return error; +} + +int git_note_read(git_note **out, git_repository *repo, + const char *notes_ref_in, const git_oid *oid) +{ + int error; + git_str notes_ref = GIT_STR_INIT; + git_commit *commit = NULL; + + error = retrieve_note_commit(&commit, ¬es_ref, repo, notes_ref_in); + + if (error < 0) + goto cleanup; + + error = git_note_commit_read(out, repo, commit, oid); + +cleanup: + git_str_dispose(¬es_ref); + git_commit_free(commit); + return error; +} + +int git_note_commit_create( + git_oid *notes_commit_out, + git_oid *notes_blob_out, + git_repository *repo, + git_commit *parent, + const git_signature *author, + const git_signature *committer, + const git_oid *oid, + const char *note, + int allow_note_overwrite) +{ + int error; + git_tree *tree = NULL; + char target[GIT_OID_MAX_HEXSIZE + 1]; + + git_oid_tostr(target, sizeof(target), oid); + + if (parent != NULL && (error = git_commit_tree(&tree, parent)) < 0) + goto cleanup; + + error = note_write(notes_commit_out, notes_blob_out, repo, author, + committer, NULL, note, tree, target, &parent, allow_note_overwrite); + + if (error < 0) + goto cleanup; + +cleanup: + git_tree_free(tree); + return error; +} + +int git_note_create( + git_oid *out, + git_repository *repo, + const char *notes_ref_in, + const git_signature *author, + const git_signature *committer, + const git_oid *oid, + const char *note, + int allow_note_overwrite) +{ + int error; + git_str notes_ref = GIT_STR_INIT; + git_commit *existing_notes_commit = NULL; + git_reference *ref = NULL; + git_oid notes_blob_oid, notes_commit_oid; + + error = retrieve_note_commit(&existing_notes_commit, ¬es_ref, + repo, notes_ref_in); + + if (error < 0 && error != GIT_ENOTFOUND) + goto cleanup; + + error = git_note_commit_create(¬es_commit_oid, + ¬es_blob_oid, + repo, existing_notes_commit, author, + committer, oid, note, + allow_note_overwrite); + if (error < 0) + goto cleanup; + + error = git_reference_create(&ref, repo, notes_ref.ptr, + ¬es_commit_oid, 1, NULL); + + if (out != NULL) + git_oid_cpy(out, ¬es_blob_oid); + +cleanup: + git_str_dispose(¬es_ref); + git_commit_free(existing_notes_commit); + git_reference_free(ref); + return error; +} + +int git_note_commit_remove( + git_oid *notes_commit_out, + git_repository *repo, + git_commit *notes_commit, + const git_signature *author, + const git_signature *committer, + const git_oid *oid) +{ + int error; + git_tree *tree = NULL; + char target[GIT_OID_MAX_HEXSIZE + 1]; + + git_oid_tostr(target, sizeof(target), oid); + + if ((error = git_commit_tree(&tree, notes_commit)) < 0) + goto cleanup; + + error = note_remove(notes_commit_out, + repo, author, committer, NULL, tree, target, ¬es_commit); + +cleanup: + git_tree_free(tree); + return error; +} + +int git_note_remove(git_repository *repo, const char *notes_ref_in, + const git_signature *author, const git_signature *committer, + const git_oid *oid) +{ + int error; + git_str notes_ref_target = GIT_STR_INIT; + git_commit *existing_notes_commit = NULL; + git_oid new_notes_commit; + git_reference *notes_ref = NULL; + + error = retrieve_note_commit(&existing_notes_commit, ¬es_ref_target, + repo, notes_ref_in); + + if (error < 0) + goto cleanup; + + error = git_note_commit_remove(&new_notes_commit, repo, + existing_notes_commit, author, committer, oid); + if (error < 0) + goto cleanup; + + error = git_reference_create(¬es_ref, repo, notes_ref_target.ptr, + &new_notes_commit, 1, NULL); + +cleanup: + git_str_dispose(¬es_ref_target); + git_reference_free(notes_ref); + git_commit_free(existing_notes_commit); + return error; +} + +int git_note_default_ref(git_buf *out, git_repository *repo) +{ + GIT_BUF_WRAP_PRIVATE(out, note_get_default_ref, repo); +} + +const git_signature *git_note_committer(const git_note *note) +{ + GIT_ASSERT_ARG_WITH_RETVAL(note, NULL); + return note->committer; +} + +const git_signature *git_note_author(const git_note *note) +{ + GIT_ASSERT_ARG_WITH_RETVAL(note, NULL); + return note->author; +} + +const char *git_note_message(const git_note *note) +{ + GIT_ASSERT_ARG_WITH_RETVAL(note, NULL); + return note->message; +} + +const git_oid *git_note_id(const git_note *note) +{ + GIT_ASSERT_ARG_WITH_RETVAL(note, NULL); + return ¬e->id; +} + +void git_note_free(git_note *note) +{ + if (note == NULL) + return; + + git_signature_free(note->committer); + git_signature_free(note->author); + git__free(note->message); + git__free(note); +} + +static int process_entry_path( + git_oid *annotated_object_id, + git_note_iterator *it, + const char *entry_path) +{ + int error = 0; + size_t i = 0, j = 0, len; + git_str buf = GIT_STR_INIT; + + if ((error = git_str_puts(&buf, entry_path)) < 0) + goto cleanup; + + len = git_str_len(&buf); + + while (i < len) { + if (buf.ptr[i] == '/') { + i++; + continue; + } + + if (git__fromhex(buf.ptr[i]) < 0) { + /* This is not a note entry */ + goto cleanup; + } + + if (i != j) + buf.ptr[j] = buf.ptr[i]; + + i++; + j++; + } + + buf.ptr[j] = '\0'; + buf.size = j; + + if (j != git_oid_hexsize(it->repo->oid_type)) { + /* This is not a note entry */ + goto cleanup; + } + + error = git_oid_from_string(annotated_object_id, buf.ptr, it->repo->oid_type); + +cleanup: + git_str_dispose(&buf); + return error; +} + +int git_note_foreach( + git_repository *repo, + const char *notes_ref, + git_note_foreach_cb note_cb, + void *payload) +{ + int error; + git_note_iterator *iter = NULL; + git_oid note_id, annotated_id; + + if ((error = git_note_iterator_new(&iter, repo, notes_ref)) < 0) + return error; + + while (!(error = git_note_next(¬e_id, &annotated_id, iter))) { + if ((error = note_cb(¬e_id, &annotated_id, payload)) != 0) { + git_error_set_after_callback(error); + break; + } + } + + if (error == GIT_ITEROVER) + error = 0; + + git_note_iterator_free(iter); + return error; +} + +void git_note_iterator_free(git_note_iterator *it) +{ + if (it == NULL) + return; + + git_iterator_free(it); +} + +int git_note_commit_iterator_new( + git_note_iterator **it, + git_commit *notes_commit) +{ + int error; + git_tree *tree; + + if ((error = git_commit_tree(&tree, notes_commit)) < 0) + goto cleanup; + + if ((error = git_iterator_for_tree(it, tree, NULL)) < 0) + git_iterator_free(*it); + +cleanup: + git_tree_free(tree); + + return error; +} + +int git_note_iterator_new( + git_note_iterator **it, + git_repository *repo, + const char *notes_ref_in) +{ + int error; + git_commit *commit = NULL; + git_str notes_ref = GIT_STR_INIT; + + error = retrieve_note_commit(&commit, ¬es_ref, repo, notes_ref_in); + if (error < 0) + goto cleanup; + + error = git_note_commit_iterator_new(it, commit); + +cleanup: + git_str_dispose(¬es_ref); + git_commit_free(commit); + + return error; +} + +int git_note_next( + git_oid *note_id, + git_oid *annotated_id, + git_note_iterator *it) +{ + int error; + const git_index_entry *item; + + if ((error = git_iterator_current(&item, it)) < 0) + return error; + + git_oid_cpy(note_id, &item->id); + + if ((error = process_entry_path(annotated_id, it, item->path)) < 0) + return error; + + if ((error = git_iterator_advance(NULL, it)) < 0 && error != GIT_ITEROVER) + return error; + + return 0; +} diff --git a/src/notes.h b/src/libgit2/notes.h similarity index 85% rename from src/notes.h rename to src/libgit2/notes.h index 2f119e3c37f..2168e459510 100644 --- a/src/notes.h +++ b/src/libgit2/notes.h @@ -10,6 +10,7 @@ #include "common.h" #include "git2/oid.h" +#include "git2/types.h" #define GIT_NOTES_DEFAULT_REF "refs/notes/commits" @@ -20,9 +21,12 @@ "Notes removed by 'git_note_remove' from libgit2" struct git_note { - git_oid oid; + git_oid id; + + git_signature *author; + git_signature *committer; char *message; }; -#endif /* INCLUDE_notes_h__ */ +#endif diff --git a/src/libgit2/object.c b/src/libgit2/object.c new file mode 100644 index 00000000000..f20dbb6cf36 --- /dev/null +++ b/src/libgit2/object.c @@ -0,0 +1,692 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "object.h" + +#include "git2/object.h" + +#include "repository.h" + +#include "buf.h" +#include "commit.h" +#include "hash.h" +#include "tree.h" +#include "blob.h" +#include "oid.h" +#include "tag.h" + +bool git_object__strict_input_validation = true; + +size_t git_object__size(git_object_t type); + +typedef struct { + const char *str; /* type name string */ + size_t size; /* size in bytes of the object structure */ + + int (*parse)(void *self, git_odb_object *obj, git_oid_t oid_type); + int (*parse_raw)(void *self, const char *data, size_t size, git_oid_t oid_type); + void (*free)(void *self); +} git_object_def; + +static git_object_def git_objects_table[] = { + /* 0 = unused */ + { "", 0, NULL, NULL, NULL }, + + /* 1 = GIT_OBJECT_COMMIT */ + { "commit", sizeof(git_commit), git_commit__parse, git_commit__parse_raw, git_commit__free }, + + /* 2 = GIT_OBJECT_TREE */ + { "tree", sizeof(git_tree), git_tree__parse, git_tree__parse_raw, git_tree__free }, + + /* 3 = GIT_OBJECT_BLOB */ + { "blob", sizeof(git_blob), git_blob__parse, git_blob__parse_raw, git_blob__free }, + + /* 4 = GIT_OBJECT_TAG */ + { "tag", sizeof(git_tag), git_tag__parse, git_tag__parse_raw, git_tag__free } +}; + +int git_object__from_raw( + git_object **object_out, + const char *data, + size_t size, + git_object_t object_type, + git_oid_t oid_type) +{ + git_object_def *def; + git_object *object; + size_t object_size; + int error; + + GIT_ASSERT_ARG(object_out); + *object_out = NULL; + + /* Validate type match */ + if (object_type != GIT_OBJECT_BLOB && + object_type != GIT_OBJECT_TREE && + object_type != GIT_OBJECT_COMMIT && + object_type != GIT_OBJECT_TAG) { + git_error_set(GIT_ERROR_INVALID, "the requested type is invalid"); + return GIT_ENOTFOUND; + } + + if ((object_size = git_object__size(object_type)) == 0) { + git_error_set(GIT_ERROR_INVALID, "the requested type is invalid"); + return GIT_ENOTFOUND; + } + + /* Allocate and initialize base object */ + object = git__calloc(1, object_size); + GIT_ERROR_CHECK_ALLOC(object); + object->cached.flags = GIT_CACHE_STORE_PARSED; + object->cached.type = object_type; + if ((error = git_odb__hash(&object->cached.oid, data, size, object_type, oid_type)) < 0) + return error; + + /* Parse raw object data */ + def = &git_objects_table[object_type]; + GIT_ASSERT(def->free && def->parse_raw); + + if ((error = def->parse_raw(object, data, size, oid_type)) < 0) { + def->free(object); + return error; + } + + git_cached_obj_incref(object); + *object_out = object; + + return 0; +} + +int git_object__init_from_odb_object( + git_object **object_out, + git_repository *repo, + git_odb_object *odb_obj, + git_object_t type) +{ + size_t object_size; + git_object *object = NULL; + + GIT_ASSERT_ARG(object_out); + *object_out = NULL; + + /* Validate type match */ + if (type != GIT_OBJECT_ANY && type != odb_obj->cached.type) { + git_error_set(GIT_ERROR_INVALID, + "the requested type does not match the type in the ODB"); + return GIT_ENOTFOUND; + } + + if ((object_size = git_object__size(odb_obj->cached.type)) == 0) { + git_error_set(GIT_ERROR_INVALID, "the requested type is invalid"); + return GIT_ENOTFOUND; + } + + /* Allocate and initialize base object */ + object = git__calloc(1, object_size); + GIT_ERROR_CHECK_ALLOC(object); + + git_oid_cpy(&object->cached.oid, &odb_obj->cached.oid); + object->cached.type = odb_obj->cached.type; + object->cached.size = odb_obj->cached.size; + object->repo = repo; + + *object_out = object; + return 0; +} + +int git_object__from_odb_object( + git_object **object_out, + git_repository *repo, + git_odb_object *odb_obj, + git_object_t type) +{ + int error; + git_object_def *def; + git_object *object = NULL; + + if ((error = git_object__init_from_odb_object(&object, repo, odb_obj, type)) < 0) + return error; + + /* Parse raw object data */ + def = &git_objects_table[odb_obj->cached.type]; + GIT_ASSERT(def->free && def->parse); + + if ((error = def->parse(object, odb_obj, repo->oid_type)) < 0) { + /* + * parse returns EINVALID on invalid data; downgrade + * that to a normal -1 error code. + */ + def->free(object); + return -1; + } + + *object_out = git_cache_store_parsed(&repo->objects, object); + return 0; +} + +void git_object__free(void *obj) +{ + git_object_t type = ((git_object *)obj)->cached.type; + + if (type < 0 || ((size_t)type) >= ARRAY_SIZE(git_objects_table) || + !git_objects_table[type].free) + git__free(obj); + else + git_objects_table[type].free(obj); +} + +int git_object_lookup_prefix( + git_object **object_out, + git_repository *repo, + const git_oid *id, + size_t len, + git_object_t type) +{ + git_object *object = NULL; + git_odb *odb = NULL; + git_odb_object *odb_obj = NULL; + size_t oid_hexsize; + int error = 0; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(object_out); + GIT_ASSERT_ARG(id); + + if (len < GIT_OID_MINPREFIXLEN) { + git_error_set(GIT_ERROR_OBJECT, "ambiguous lookup - OID prefix is too short"); + return GIT_EAMBIGUOUS; + } + + error = git_repository_odb__weakptr(&odb, repo); + if (error < 0) + return error; + + oid_hexsize = git_oid_hexsize(repo->oid_type); + + if (len > oid_hexsize) + len = oid_hexsize; + + if (len == oid_hexsize) { + git_cached_obj *cached = NULL; + + /* We want to match the full id : we can first look up in the cache, + * since there is no need to check for non ambiguousity + */ + cached = git_cache_get_any(&repo->objects, id); + if (cached != NULL) { + if (cached->flags == GIT_CACHE_STORE_PARSED) { + object = (git_object *)cached; + + if (type != GIT_OBJECT_ANY && type != object->cached.type) { + git_object_free(object); + git_error_set(GIT_ERROR_INVALID, + "the requested type does not match the type in the ODB"); + return GIT_ENOTFOUND; + } + + *object_out = object; + return 0; + } else if (cached->flags == GIT_CACHE_STORE_RAW) { + odb_obj = (git_odb_object *)cached; + } else { + GIT_ASSERT(!"Wrong caching type in the global object cache"); + } + } else { + /* Object was not found in the cache, let's explore the backends. + * We could just use git_odb_read_unique_short_oid, + * it is the same cost for packed and loose object backends, + * but it may be much more costly for sqlite and hiredis. + */ + error = git_odb_read(&odb_obj, odb, id); + } + } else { + git_oid short_oid; + + git_oid_clear(&short_oid, repo->oid_type); + git_oid__cpy_prefix(&short_oid, id, len); + + /* If len < GIT_OID_SHA1_HEXSIZE (a strict short oid was given), we have + * 2 options : + * - We always search in the cache first. If we find that short oid is + * ambiguous, we can stop. But in all the other cases, we must then + * explore all the backends (to find an object if there was match, + * or to check that oid is not ambiguous if we have found 1 match in + * the cache) + * - We never explore the cache, go right to exploring the backends + * We chose the latter : we explore directly the backends. + */ + error = git_odb_read_prefix(&odb_obj, odb, &short_oid, len); + } + + if (error < 0) + return error; + + GIT_ASSERT(odb_obj); + error = git_object__from_odb_object(object_out, repo, odb_obj, type); + + git_odb_object_free(odb_obj); + + return error; +} + +int git_object_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_object_t type) { + return git_object_lookup_prefix(object_out, + repo, id, git_oid_hexsize(repo->oid_type), type); +} + +void git_object_free(git_object *object) +{ + if (object == NULL) + return; + + git_cached_obj_decref(object); +} + +const git_oid *git_object_id(const git_object *obj) +{ + GIT_ASSERT_ARG_WITH_RETVAL(obj, NULL); + return &obj->cached.oid; +} + +git_object_t git_object_type(const git_object *obj) +{ + GIT_ASSERT_ARG_WITH_RETVAL(obj, GIT_OBJECT_INVALID); + return obj->cached.type; +} + +git_repository *git_object_owner(const git_object *obj) +{ + GIT_ASSERT_ARG_WITH_RETVAL(obj, NULL); + return obj->repo; +} + +const char *git_object_type2string(git_object_t type) +{ + if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table)) + return ""; + + return git_objects_table[type].str; +} + +git_object_t git_object_string2type(const char *str) +{ + if (!str) + return GIT_OBJECT_INVALID; + + return git_object_stringn2type(str, strlen(str)); +} + +git_object_t git_object_stringn2type(const char *str, size_t len) +{ + size_t i; + + if (!str || !len || !*str) + return GIT_OBJECT_INVALID; + + for (i = 0; i < ARRAY_SIZE(git_objects_table); i++) + if (*git_objects_table[i].str && + !git__prefixncmp(str, len, git_objects_table[i].str)) + return (git_object_t)i; + + return GIT_OBJECT_INVALID; +} + +int git_object_type_is_valid(git_object_t type) +{ + if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table)) + return 0; + + return (git_objects_table[type].size > 0) ? 1 : 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_object_typeisloose(git_object_t type) +{ + return git_object_type_is_valid(type); +} +#endif + +size_t git_object__size(git_object_t type) +{ + if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table)) + return 0; + + return git_objects_table[type].size; +} + +static int dereference_object(git_object **dereferenced, git_object *obj) +{ + git_object_t type = git_object_type(obj); + + switch (type) { + case GIT_OBJECT_COMMIT: + return git_commit_tree((git_tree **)dereferenced, (git_commit*)obj); + + case GIT_OBJECT_TAG: + return git_tag_target(dereferenced, (git_tag*)obj); + + case GIT_OBJECT_BLOB: + case GIT_OBJECT_TREE: + return GIT_EPEEL; + + default: + return GIT_EINVALIDSPEC; + } +} + +static int peel_error(int error, const git_oid *oid, git_object_t type) +{ + const char *type_name; + char hex_oid[GIT_OID_MAX_HEXSIZE + 1]; + + type_name = git_object_type2string(type); + + git_oid_nfmt(hex_oid, GIT_OID_MAX_HEXSIZE + 1, oid); + + git_error_set(GIT_ERROR_OBJECT, "the git_object of id '%s' can not be " + "successfully peeled into a %s (git_object_t=%i).", hex_oid, type_name, type); + + return error; +} + +static int check_type_combination(git_object_t type, git_object_t target) +{ + if (type == target) + return 0; + + switch (type) { + case GIT_OBJECT_BLOB: + case GIT_OBJECT_TREE: + /* a blob or tree can never be peeled to anything but themselves */ + return GIT_EINVALIDSPEC; + break; + case GIT_OBJECT_COMMIT: + /* a commit can only be peeled to a tree */ + if (target != GIT_OBJECT_TREE && target != GIT_OBJECT_ANY) + return GIT_EINVALIDSPEC; + break; + case GIT_OBJECT_TAG: + /* a tag may point to anything, so we let anything through */ + break; + default: + return GIT_EINVALIDSPEC; + } + + return 0; +} + +int git_object_peel( + git_object **peeled, + const git_object *object, + git_object_t target_type) +{ + git_object *source, *deref = NULL; + int error; + + GIT_ASSERT_ARG(object); + GIT_ASSERT_ARG(peeled); + + GIT_ASSERT_ARG(target_type == GIT_OBJECT_TAG || + target_type == GIT_OBJECT_COMMIT || + target_type == GIT_OBJECT_TREE || + target_type == GIT_OBJECT_BLOB || + target_type == GIT_OBJECT_ANY); + + if ((error = check_type_combination(git_object_type(object), target_type)) < 0) + return peel_error(error, git_object_id(object), target_type); + + if (git_object_type(object) == target_type) + return git_object_dup(peeled, (git_object *)object); + + source = (git_object *)object; + + while (!(error = dereference_object(&deref, source))) { + + if (source != object) + git_object_free(source); + + if (git_object_type(deref) == target_type) { + *peeled = deref; + return 0; + } + + if (target_type == GIT_OBJECT_ANY && + git_object_type(deref) != git_object_type(object)) + { + *peeled = deref; + return 0; + } + + source = deref; + deref = NULL; + } + + if (source != object) + git_object_free(source); + + git_object_free(deref); + + if (error) + error = peel_error(error, git_object_id(object), target_type); + + return error; +} + +int git_object_dup(git_object **dest, git_object *source) +{ + git_cached_obj_incref(source); + *dest = source; + return 0; +} + +int git_object_lookup_bypath( + git_object **out, + const git_object *treeish, + const char *path, + git_object_t type) +{ + int error = -1; + git_tree *tree = NULL; + git_tree_entry *entry = NULL; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(treeish); + GIT_ASSERT_ARG(path); + + if ((error = git_object_peel((git_object**)&tree, treeish, GIT_OBJECT_TREE)) < 0 || + (error = git_tree_entry_bypath(&entry, tree, path)) < 0) + { + goto cleanup; + } + + if (type != GIT_OBJECT_ANY && git_tree_entry_type(entry) != type) + { + git_error_set(GIT_ERROR_OBJECT, + "object at path '%s' is not of the asked-for type %d", + path, type); + error = GIT_EINVALIDSPEC; + goto cleanup; + } + + error = git_tree_entry_to_object(out, git_object_owner(treeish), entry); + +cleanup: + git_tree_entry_free(entry); + git_tree_free(tree); + return error; +} + +static int git_object__short_id(git_str *out, const git_object *obj) +{ + git_repository *repo; + git_oid id; + git_odb *odb; + size_t oid_hexsize; + int len, error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(obj); + + repo = git_object_owner(obj); + + git_oid_clear(&id, repo->oid_type); + oid_hexsize = git_oid_hexsize(repo->oid_type); + + if ((error = git_repository__abbrev_length(&len, repo)) < 0) + return error; + + if ((size_t)len == oid_hexsize) { + if ((error = git_oid_cpy(&id, &obj->cached.oid)) < 0) { + return error; + } + } + + if ((error = git_repository_odb(&odb, repo)) < 0) + return error; + + while ((size_t)len < oid_hexsize) { + /* set up short oid */ + memcpy(&id.id, &obj->cached.oid.id, (len + 1) / 2); + if (len & 1) + id.id[len / 2] &= 0xf0; + + error = git_odb_exists_prefix(NULL, odb, &id, len); + if (error != GIT_EAMBIGUOUS) + break; + + git_error_clear(); + len++; + } + + if (!error && !(error = git_str_grow(out, len + 1))) { + git_oid_tostr(out->ptr, len + 1, &id); + out->size = len; + } + + git_odb_free(odb); + + return error; +} + +int git_object_short_id(git_buf *out, const git_object *obj) +{ + GIT_BUF_WRAP_PRIVATE(out, git_object__short_id, obj); +} + +bool git_object__is_valid( + git_repository *repo, const git_oid *id, git_object_t expected_type) +{ + git_odb *odb; + git_object_t actual_type; + size_t len; + int error; + + if (!git_object__strict_input_validation) + return true; + + if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 || + (error = git_odb_read_header(&len, &actual_type, odb, id)) < 0) + return false; + + if (expected_type != GIT_OBJECT_ANY && expected_type != actual_type) { + git_error_set(GIT_ERROR_INVALID, + "the requested type does not match the type in the ODB"); + return false; + } + + return true; +} + +int git_object_rawcontent_is_valid( + int *valid, + const char *buf, + size_t len, + git_object_t object_type +#ifdef GIT_EXPERIMENTAL_SHA256 + , git_oid_t oid_type +#endif + ) +{ + git_object *obj = NULL; + int error; + +#ifndef GIT_EXPERIMENTAL_SHA256 + git_oid_t oid_type = GIT_OID_SHA1; +#endif + + GIT_ASSERT_ARG(valid); + GIT_ASSERT_ARG(buf); + + /* Blobs are always valid; don't bother parsing. */ + if (object_type == GIT_OBJECT_BLOB) { + *valid = 1; + return 0; + } + + error = git_object__from_raw(&obj, buf, len, object_type, oid_type); + git_object_free(obj); + + if (error == 0) { + *valid = 1; + return 0; + } else if (error == GIT_EINVALID) { + *valid = 0; + return 0; + } + + return error; +} + +int git_object__parse_oid_header( + git_oid *oid, + const char **buffer_out, + const char *buffer_end, + const char *header, + git_oid_t oid_type) +{ + const size_t sha_len = git_oid_hexsize(oid_type); + const size_t header_len = strlen(header); + + const char *buffer = *buffer_out; + + if (buffer + (header_len + sha_len + 1) > buffer_end) + return -1; + + if (memcmp(buffer, header, header_len) != 0) + return -1; + + if (buffer[header_len + sha_len] != '\n') + return -1; + + if (git_oid_from_prefix(oid, buffer + header_len, sha_len, oid_type) < 0) + return -1; + + *buffer_out = buffer + (header_len + sha_len + 1); + + return 0; +} + +int git_object__write_oid_header( + git_str *buf, + const char *header, + const git_oid *oid) +{ + size_t hex_size = git_oid_hexsize(git_oid_type(oid)); + char hex_oid[GIT_OID_MAX_HEXSIZE]; + + if (!hex_size) { + git_error_set(GIT_ERROR_INVALID, "unknown type"); + return -1; + } + + git_oid_fmt(hex_oid, oid); + git_str_puts(buf, header); + git_str_put(buf, hex_oid, hex_size); + git_str_putc(buf, '\n'); + + return git_str_oom(buf) ? -1 : 0; +} diff --git a/src/libgit2/object.h b/src/libgit2/object.h new file mode 100644 index 00000000000..b6c604c8178 --- /dev/null +++ b/src/libgit2/object.h @@ -0,0 +1,86 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_object_h__ +#define INCLUDE_object_h__ + +#include "common.h" + +#include "repository.h" + +#define GIT_OBJECT_SIZE_MAX UINT64_MAX + +extern bool git_object__strict_input_validation; + +/** Base git object for inheritance */ +struct git_object { + git_cached_obj cached; + git_repository *repo; +}; + +/* fully free the object; internal method, DO NOT EXPORT */ +void git_object__free(void *object); + +/* + * Parse object from raw data. Note that the resulting object is + * tied to the lifetime of the data, as some objects simply point + * into it. + */ +int git_object__from_raw( + git_object **object_out, + const char *data, + size_t size, + git_object_t object_type, + git_oid_t oid_type); + +int git_object__init_from_odb_object( + git_object **object_out, + git_repository *repo, + git_odb_object *odb_obj, + git_object_t type); + +int git_object__from_odb_object( + git_object **object_out, + git_repository *repo, + git_odb_object *odb_obj, + git_object_t type); + +int git_object__resolve_to_type(git_object **obj, git_object_t type); + +git_object_t git_object_stringn2type(const char *str, size_t len); + +int git_object__parse_oid_header( + git_oid *oid, + const char **buffer_out, + const char *buffer_end, + const char *header, + git_oid_t oid_type); + +int git_object__write_oid_header( + git_str *buf, + const char *header, + const git_oid *oid); + +bool git_object__is_valid( + git_repository *repo, const git_oid *id, git_object_t expected_type); + +GIT_INLINE(git_object_t) git_object__type_from_filemode(git_filemode_t mode) +{ + switch (mode) { + case GIT_FILEMODE_TREE: + return GIT_OBJECT_TREE; + case GIT_FILEMODE_COMMIT: + return GIT_OBJECT_COMMIT; + case GIT_FILEMODE_BLOB: + case GIT_FILEMODE_BLOB_EXECUTABLE: + case GIT_FILEMODE_LINK: + return GIT_OBJECT_BLOB; + default: + return GIT_OBJECT_INVALID; + } +} + +#endif diff --git a/src/libgit2/object_api.c b/src/libgit2/object_api.c new file mode 100644 index 00000000000..d45abd5ce74 --- /dev/null +++ b/src/libgit2/object_api.c @@ -0,0 +1,148 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "git2/object.h" + +#include "repository.h" +#include "commit.h" +#include "tree.h" +#include "blob.h" +#include "tag.h" + +/** + * Commit + */ +int git_commit_lookup(git_commit **out, git_repository *repo, const git_oid *id) +{ + return git_object_lookup((git_object **)out, repo, id, GIT_OBJECT_COMMIT); +} + +int git_commit_lookup_prefix(git_commit **out, git_repository *repo, const git_oid *id, size_t len) +{ + return git_object_lookup_prefix((git_object **)out, repo, id, len, GIT_OBJECT_COMMIT); +} + +void git_commit_free(git_commit *obj) +{ + git_object_free((git_object *)obj); +} + +const git_oid *git_commit_id(const git_commit *obj) +{ + return git_object_id((const git_object *)obj); +} + +git_repository *git_commit_owner(const git_commit *obj) +{ + return git_object_owner((const git_object *)obj); +} + +int git_commit_dup(git_commit **out, git_commit *obj) +{ + return git_object_dup((git_object **)out, (git_object *)obj); +} + +/** + * Tree + */ +int git_tree_lookup(git_tree **out, git_repository *repo, const git_oid *id) +{ + return git_object_lookup((git_object **)out, repo, id, GIT_OBJECT_TREE); +} + +int git_tree_lookup_prefix(git_tree **out, git_repository *repo, const git_oid *id, size_t len) +{ + return git_object_lookup_prefix((git_object **)out, repo, id, len, GIT_OBJECT_TREE); +} + +void git_tree_free(git_tree *obj) +{ + git_object_free((git_object *)obj); +} + +const git_oid *git_tree_id(const git_tree *obj) +{ + return git_object_id((const git_object *)obj); +} + +git_repository *git_tree_owner(const git_tree *obj) +{ + return git_object_owner((const git_object *)obj); +} + +int git_tree_dup(git_tree **out, git_tree *obj) +{ + return git_object_dup((git_object **)out, (git_object *)obj); +} + +/** + * Tag + */ +int git_tag_lookup(git_tag **out, git_repository *repo, const git_oid *id) +{ + return git_object_lookup((git_object **)out, repo, id, GIT_OBJECT_TAG); +} + +int git_tag_lookup_prefix(git_tag **out, git_repository *repo, const git_oid *id, size_t len) +{ + return git_object_lookup_prefix((git_object **)out, repo, id, len, GIT_OBJECT_TAG); +} + +void git_tag_free(git_tag *obj) +{ + git_object_free((git_object *)obj); +} + +const git_oid *git_tag_id(const git_tag *obj) +{ + return git_object_id((const git_object *)obj); +} + +git_repository *git_tag_owner(const git_tag *obj) +{ + return git_object_owner((const git_object *)obj); +} + +int git_tag_dup(git_tag **out, git_tag *obj) +{ + return git_object_dup((git_object **)out, (git_object *)obj); +} + +/** + * Blob + */ +int git_blob_lookup(git_blob **out, git_repository *repo, const git_oid *id) +{ + return git_object_lookup((git_object **)out, repo, id, GIT_OBJECT_BLOB); +} + +int git_blob_lookup_prefix(git_blob **out, git_repository *repo, const git_oid *id, size_t len) +{ + return git_object_lookup_prefix((git_object **)out, repo, id, len, GIT_OBJECT_BLOB); +} + +void git_blob_free(git_blob *obj) +{ + git_object_free((git_object *)obj); +} + +const git_oid *git_blob_id(const git_blob *obj) +{ + return git_object_id((const git_object *)obj); +} + +git_repository *git_blob_owner(const git_blob *obj) +{ + return git_object_owner((const git_object *)obj); +} + +int git_blob_dup(git_blob **out, git_blob *obj) +{ + return git_object_dup((git_object **)out, (git_object *)obj); +} diff --git a/src/libgit2/odb.c b/src/libgit2/odb.c new file mode 100644 index 00000000000..e58f3c9426b --- /dev/null +++ b/src/libgit2/odb.c @@ -0,0 +1,1982 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "odb.h" + +#include +#include "git2/object.h" +#include "git2/sys/odb_backend.h" +#include "futils.h" +#include "hash.h" +#include "delta.h" +#include "filter.h" +#include "repository.h" +#include "blob.h" +#include "oid.h" + +#include "git2/odb_backend.h" +#include "git2/oid.h" +#include "git2/oidarray.h" + +#define GIT_ALTERNATES_FILE "info/alternates" + +#define GIT_ALTERNATES_MAX_DEPTH 5 + +/* + * We work under the assumption that most objects for long-running + * operations will be packed + */ +int git_odb__loose_priority = GIT_ODB_DEFAULT_LOOSE_PRIORITY; +int git_odb__packed_priority = GIT_ODB_DEFAULT_PACKED_PRIORITY; + +bool git_odb__strict_hash_verification = true; + +typedef struct +{ + git_odb_backend *backend; + int priority; + bool is_alternate; + ino_t disk_inode; +} backend_internal; + +static git_cache *odb_cache(git_odb *odb) +{ + git_repository *owner = GIT_REFCOUNT_OWNER(odb); + if (owner != NULL) { + return &owner->objects; + } + + return &odb->own_cache; +} + +static int odb_otype_fast(git_object_t *type_p, git_odb *db, const git_oid *id); +static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth); +static int error_null_oid(int error, const char *message); + +static git_object_t odb_hardcoded_type(const git_oid *id) +{ + if (!git_oid_cmp(id, &git_oid__empty_tree_sha1)) + return GIT_OBJECT_TREE; + + return GIT_OBJECT_INVALID; +} + +static int odb_read_hardcoded(bool *found, git_rawobj *raw, const git_oid *id) +{ + git_object_t type; + + *found = false; + + if ((type = odb_hardcoded_type(id)) == GIT_OBJECT_INVALID) + return 0; + + raw->type = type; + raw->len = 0; + raw->data = git__calloc(1, sizeof(uint8_t)); + GIT_ERROR_CHECK_ALLOC(raw->data); + + *found = true; + return 0; +} + +int git_odb__format_object_header( + size_t *written, + char *hdr, + size_t hdr_size, + git_object_size_t obj_len, + git_object_t obj_type) +{ + const char *type_str = git_object_type2string(obj_type); + int hdr_max = (hdr_size > INT_MAX-2) ? (INT_MAX-2) : (int)hdr_size; + int len; + + len = p_snprintf(hdr, hdr_max, "%s %"PRId64, type_str, (int64_t)obj_len); + + if (len < 0 || len >= hdr_max) { + git_error_set(GIT_ERROR_OS, "object header creation failed"); + return -1; + } + + *written = (size_t)(len + 1); + return 0; +} + +int git_odb__hashobj(git_oid *id, git_rawobj *obj, git_oid_t oid_type) +{ + git_str_vec vec[2]; + char header[64]; + size_t hdrlen; + git_hash_algorithm_t algorithm; + int error; + + GIT_ASSERT_ARG(id); + GIT_ASSERT_ARG(obj); + + if (!git_object_type_is_valid(obj->type)) { + git_error_set(GIT_ERROR_INVALID, "invalid object type"); + return -1; + } + + if (!(algorithm = git_oid_algorithm(oid_type))) { + git_error_set(GIT_ERROR_INVALID, "unknown oid type"); + return -1; + } + + if (!obj->data && obj->len != 0) { + git_error_set(GIT_ERROR_INVALID, "invalid object"); + return -1; + } + + if ((error = git_odb__format_object_header(&hdrlen, + header, sizeof(header), obj->len, obj->type)) < 0) + return error; + + vec[0].data = header; + vec[0].len = hdrlen; + vec[1].data = obj->data; + vec[1].len = obj->len; + +#ifdef GIT_EXPERIMENTAL_SHA256 + id->type = oid_type; +#endif + + return git_hash_vec(id->id, vec, 2, algorithm); +} + + +static git_odb_object *odb_object__alloc(const git_oid *oid, git_rawobj *source) +{ + git_odb_object *object = git__calloc(1, sizeof(git_odb_object)); + + if (object != NULL) { + git_oid_cpy(&object->cached.oid, oid); + object->cached.type = source->type; + object->cached.size = source->len; + object->buffer = source->data; + } + + return object; +} + +void git_odb_object__free(void *object) +{ + if (object != NULL) { + git__free(((git_odb_object *)object)->buffer); + git__free(object); + } +} + +const git_oid *git_odb_object_id(git_odb_object *object) +{ + return &object->cached.oid; +} + +const void *git_odb_object_data(git_odb_object *object) +{ + return object->buffer; +} + +size_t git_odb_object_size(git_odb_object *object) +{ + return object->cached.size; +} + +git_object_t git_odb_object_type(git_odb_object *object) +{ + return object->cached.type; +} + +int git_odb_object_dup(git_odb_object **dest, git_odb_object *source) +{ + git_cached_obj_incref(source); + *dest = source; + return 0; +} + +void git_odb_object_free(git_odb_object *object) +{ + if (object == NULL) + return; + + git_cached_obj_decref(object); +} + +int git_odb__hashfd( + git_oid *out, + git_file fd, + size_t size, + git_object_t object_type, + git_oid_t oid_type) +{ + size_t hdr_len; + char hdr[64], buffer[GIT_BUFSIZE_FILEIO]; + git_hash_ctx ctx; + git_hash_algorithm_t algorithm; + ssize_t read_len = 0; + int error = 0; + + if (!git_object_type_is_valid(object_type)) { + git_error_set(GIT_ERROR_INVALID, "invalid object type for hash"); + return -1; + } + + if (!(algorithm = git_oid_algorithm(oid_type))) { + git_error_set(GIT_ERROR_INVALID, "unknown oid type"); + return -1; + } + + if ((error = git_hash_ctx_init(&ctx, algorithm)) < 0) + return error; + + if ((error = git_odb__format_object_header(&hdr_len, hdr, + sizeof(hdr), size, object_type)) < 0) + goto done; + + if ((error = git_hash_update(&ctx, hdr, hdr_len)) < 0) + goto done; + + while (size > 0 && (read_len = p_read(fd, buffer, sizeof(buffer))) > 0) { + if ((error = git_hash_update(&ctx, buffer, read_len)) < 0) + goto done; + + size -= read_len; + } + + /* If p_read returned an error code, the read obviously failed. + * If size is not zero, the file was truncated after we originally + * stat'd it, so we consider this a read failure too */ + if (read_len < 0 || size > 0) { + git_error_set(GIT_ERROR_OS, "error reading file for hashing"); + error = -1; + + goto done; + } + + error = git_hash_final(out->id, &ctx); + +#ifdef GIT_EXPERIMENTAL_SHA256 + out->type = oid_type; +#endif + +done: + git_hash_ctx_cleanup(&ctx); + return error; +} + +int git_odb__hashfd_filtered( + git_oid *out, + git_file fd, + size_t size, + git_object_t object_type, + git_oid_t oid_type, + git_filter_list *fl) +{ + int error; + git_str raw = GIT_STR_INIT; + + if (!fl) + return git_odb__hashfd(out, fd, size, object_type, oid_type); + + /* size of data is used in header, so we have to read the whole file + * into memory to apply filters before beginning to calculate the hash + */ + + if (!(error = git_futils_readbuffer_fd(&raw, fd, size))) { + git_str post = GIT_STR_INIT; + + error = git_filter_list__convert_buf(&post, fl, &raw); + + if (!error) + error = git_odb__hash(out, post.ptr, post.size, object_type, oid_type); + + git_str_dispose(&post); + } + + return error; +} + +int git_odb__hashlink(git_oid *out, const char *path, git_oid_t oid_type) +{ + struct stat st; + int size; + int result; + + if (git_fs_path_lstat(path, &st) < 0) + return -1; + + if (!git__is_int(st.st_size) || (int)st.st_size < 0) { + git_error_set(GIT_ERROR_FILESYSTEM, "file size overflow for 32-bit systems"); + return -1; + } + + size = (int)st.st_size; + + if (S_ISLNK(st.st_mode)) { + char *link_data; + int read_len; + size_t alloc_size; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, size, 1); + link_data = git__malloc(alloc_size); + GIT_ERROR_CHECK_ALLOC(link_data); + + read_len = p_readlink(path, link_data, size); + if (read_len == -1) { + git_error_set(GIT_ERROR_OS, "failed to read symlink data for '%s'", path); + git__free(link_data); + return -1; + } + GIT_ASSERT(read_len <= size); + link_data[read_len] = '\0'; + + result = git_odb__hash(out, link_data, read_len, GIT_OBJECT_BLOB, oid_type); + git__free(link_data); + } else { + int fd = git_futils_open_ro(path); + if (fd < 0) + return -1; + result = git_odb__hashfd(out, fd, size, GIT_OBJECT_BLOB, oid_type); + p_close(fd); + } + + return result; +} + +int git_odb__hashfile( + git_oid *out, + const char *path, + git_object_t object_type, + git_oid_t oid_type) +{ + uint64_t size; + int fd, error = 0; + + if ((fd = git_futils_open_ro(path)) < 0) + return fd; + + if ((error = git_futils_filesize(&size, fd)) < 0) + goto done; + + if (!git__is_sizet(size)) { + git_error_set(GIT_ERROR_OS, "file size overflow for 32-bit systems"); + error = -1; + goto done; + } + + error = git_odb__hashfd(out, fd, (size_t)size, object_type, oid_type); + +done: + p_close(fd); + return error; +} + +#ifdef GIT_EXPERIMENTAL_SHA256 +int git_odb_hashfile( + git_oid *out, + const char *path, + git_object_t object_type, + git_oid_t oid_type) +{ + return git_odb__hashfile(out, path, object_type, oid_type); +} +#else +int git_odb_hashfile( + git_oid *out, + const char *path, + git_object_t object_type) +{ + return git_odb__hashfile(out, path, object_type, GIT_OID_SHA1); +} +#endif + +int git_odb__hash( + git_oid *id, + const void *data, + size_t len, + git_object_t object_type, + git_oid_t oid_type) +{ + git_rawobj raw; + + GIT_ASSERT_ARG(id); + + raw.data = (void *)data; + raw.len = len; + raw.type = object_type; + + return git_odb__hashobj(id, &raw, oid_type); +} + +#ifdef GIT_EXPERIMENTAL_SHA256 +int git_odb_hash( + git_oid *out, + const void *data, + size_t len, + git_object_t object_type, + git_oid_t oid_type) +{ + return git_odb__hash(out, data, len, object_type, oid_type); +} +#else +int git_odb_hash( + git_oid *out, + const void *data, + size_t len, + git_object_t type) +{ + return git_odb__hash(out, data, len, type, GIT_OID_SHA1); +} +#endif + +/** + * FAKE WSTREAM + */ + +typedef struct { + git_odb_stream stream; + char *buffer; + size_t size, written; + git_object_t type; +} fake_wstream; + +static int fake_wstream__fwrite(git_odb_stream *_stream, const git_oid *oid) +{ + fake_wstream *stream = (fake_wstream *)_stream; + return _stream->backend->write(_stream->backend, oid, stream->buffer, stream->size, stream->type); +} + +static int fake_wstream__write(git_odb_stream *_stream, const char *data, size_t len) +{ + fake_wstream *stream = (fake_wstream *)_stream; + + GIT_ASSERT(stream->written + len <= stream->size); + + memcpy(stream->buffer + stream->written, data, len); + stream->written += len; + return 0; +} + +static void fake_wstream__free(git_odb_stream *_stream) +{ + fake_wstream *stream = (fake_wstream *)_stream; + + git__free(stream->buffer); + git__free(stream); +} + +static int init_fake_wstream(git_odb_stream **stream_p, git_odb_backend *backend, git_object_size_t size, git_object_t type) +{ + fake_wstream *stream; + size_t blobsize; + + GIT_ERROR_CHECK_BLOBSIZE(size); + blobsize = (size_t)size; + + stream = git__calloc(1, sizeof(fake_wstream)); + GIT_ERROR_CHECK_ALLOC(stream); + + stream->size = blobsize; + stream->type = type; + stream->buffer = git__malloc(blobsize); + if (stream->buffer == NULL) { + git__free(stream); + return -1; + } + + stream->stream.backend = backend; + stream->stream.read = NULL; /* read only */ + stream->stream.write = &fake_wstream__write; + stream->stream.finalize_write = &fake_wstream__fwrite; + stream->stream.free = &fake_wstream__free; + stream->stream.mode = GIT_STREAM_WRONLY; + + *stream_p = (git_odb_stream *)stream; + return 0; +} + +/*********************************************************** + * + * OBJECT DATABASE PUBLIC API + * + * Public calls for the ODB functionality + * + ***********************************************************/ + +static int backend_sort_cmp(const void *a, const void *b) +{ + const backend_internal *backend_a = (const backend_internal *)(a); + const backend_internal *backend_b = (const backend_internal *)(b); + + if (backend_b->priority == backend_a->priority) { + if (backend_a->is_alternate) + return -1; + if (backend_b->is_alternate) + return 1; + return 0; + } + return (backend_b->priority - backend_a->priority); +} + +static void normalize_options( + git_odb_options *opts, + const git_odb_options *given_opts) +{ + git_odb_options init = GIT_ODB_OPTIONS_INIT; + + if (given_opts) + memcpy(opts, given_opts, sizeof(git_odb_options)); + else + memcpy(opts, &init, sizeof(git_odb_options)); + + if (!opts->oid_type) + opts->oid_type = GIT_OID_DEFAULT; +} + +int git_odb_new_ext(git_odb **out, const git_odb_options *opts) +{ + git_odb *db; + + GIT_ASSERT_ARG(out); + GIT_ERROR_CHECK_VERSION(opts, GIT_ODB_OPTIONS_VERSION, "git_odb_options"); + + db = git__calloc(1, sizeof(*db)); + GIT_ERROR_CHECK_ALLOC(db); + + normalize_options(&db->options, opts); + + if (git_mutex_init(&db->lock) < 0) { + git__free(db); + return -1; + } + if (git_cache_init(&db->own_cache) < 0) { + git_mutex_free(&db->lock); + git__free(db); + return -1; + } + if (git_vector_init(&db->backends, 4, backend_sort_cmp) < 0) { + git_cache_dispose(&db->own_cache); + git_mutex_free(&db->lock); + git__free(db); + return -1; + } + + *out = db; + GIT_REFCOUNT_INC(db); + return 0; +} + +int git_odb_new(git_odb **out) +{ + return git_odb_new_ext(out, NULL); +} + +static int add_backend_internal( + git_odb *odb, git_odb_backend *backend, + int priority, bool is_alternate, ino_t disk_inode) +{ + backend_internal *internal; + + GIT_ASSERT_ARG(odb); + GIT_ASSERT_ARG(backend); + + GIT_ERROR_CHECK_VERSION(backend, GIT_ODB_BACKEND_VERSION, "git_odb_backend"); + + /* Check if the backend is already owned by another ODB */ + GIT_ASSERT(!backend->odb || backend->odb == odb); + + internal = git__malloc(sizeof(backend_internal)); + GIT_ERROR_CHECK_ALLOC(internal); + + internal->backend = backend; + internal->priority = priority; + internal->is_alternate = is_alternate; + internal->disk_inode = disk_inode; + + if (git_mutex_lock(&odb->lock) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return -1; + } + if (git_vector_insert(&odb->backends, internal) < 0) { + git_mutex_unlock(&odb->lock); + git__free(internal); + return -1; + } + git_vector_sort(&odb->backends); + internal->backend->odb = odb; + git_mutex_unlock(&odb->lock); + return 0; +} + +int git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority) +{ + return add_backend_internal(odb, backend, priority, false, 0); +} + +int git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority) +{ + return add_backend_internal(odb, backend, priority, true, 0); +} + +size_t git_odb_num_backends(git_odb *odb) +{ + size_t length; + bool locked = true; + + GIT_ASSERT_ARG(odb); + + if (git_mutex_lock(&odb->lock) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + locked = false; + } + length = odb->backends.length; + if (locked) + git_mutex_unlock(&odb->lock); + return length; +} + +static int git_odb__error_unsupported_in_backend(const char *action) +{ + git_error_set(GIT_ERROR_ODB, + "cannot %s - unsupported in the loaded odb backends", action); + return -1; +} + + +int git_odb_get_backend(git_odb_backend **out, git_odb *odb, size_t pos) +{ + backend_internal *internal; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(odb); + + + if ((error = git_mutex_lock(&odb->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return error; + } + internal = git_vector_get(&odb->backends, pos); + + if (!internal || !internal->backend) { + git_mutex_unlock(&odb->lock); + + git_error_set(GIT_ERROR_ODB, "no ODB backend loaded at index %" PRIuZ, pos); + return GIT_ENOTFOUND; + } + *out = internal->backend; + git_mutex_unlock(&odb->lock); + + return 0; +} + +int git_odb__add_default_backends( + git_odb *db, const char *objects_dir, + bool as_alternates, int alternate_depth) +{ + size_t i = 0; + struct stat st; + ino_t inode; + git_odb_backend *loose, *packed; + git_odb_backend_loose_options loose_opts = GIT_ODB_BACKEND_LOOSE_OPTIONS_INIT; + git_odb_backend_pack_options pack_opts = GIT_ODB_BACKEND_PACK_OPTIONS_INIT; + + /* TODO: inodes are not really relevant on Win32, so we need to find + * a cross-platform workaround for this */ +#ifdef GIT_WIN32 + GIT_UNUSED(i); + GIT_UNUSED(&st); + + inode = 0; +#else + if (p_stat(objects_dir, &st) < 0) { + if (as_alternates) + /* this should warn */ + return 0; + + git_error_set(GIT_ERROR_ODB, "failed to load object database in '%s'", objects_dir); + return -1; + } + + inode = st.st_ino; + + if (git_mutex_lock(&db->lock) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return -1; + } + for (i = 0; i < db->backends.length; ++i) { + backend_internal *backend = git_vector_get(&db->backends, i); + if (backend->disk_inode == inode) { + git_mutex_unlock(&db->lock); + return 0; + } + } + git_mutex_unlock(&db->lock); +#endif + + if (db->do_fsync) + loose_opts.flags |= GIT_ODB_BACKEND_LOOSE_FSYNC; + + loose_opts.oid_type = db->options.oid_type; + pack_opts.oid_type = db->options.oid_type; + + /* add the loose object backend */ + if (git_odb__backend_loose(&loose, objects_dir, &loose_opts) < 0 || + add_backend_internal(db, loose, git_odb__loose_priority, as_alternates, inode) < 0) + return -1; + + /* add the packed file backend */ +#ifdef GIT_EXPERIMENTAL_SHA256 + if (git_odb_backend_pack(&packed, objects_dir, &pack_opts) < 0) + return -1; +#else + GIT_UNUSED(pack_opts); + + if (git_odb_backend_pack(&packed, objects_dir) < 0) + return -1; +#endif + + if (add_backend_internal(db, packed, git_odb__packed_priority, as_alternates, inode) < 0) + return -1; + + if (git_mutex_lock(&db->lock) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return -1; + } + if (!db->cgraph && + git_commit_graph_new(&db->cgraph, objects_dir, false, db->options.oid_type) < 0) { + git_mutex_unlock(&db->lock); + return -1; + } + git_mutex_unlock(&db->lock); + + return load_alternates(db, objects_dir, alternate_depth); +} + +static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth) +{ + git_str alternates_path = GIT_STR_INIT; + git_str alternates_buf = GIT_STR_INIT; + char *buffer; + const char *alternate; + int result = 0; + + /* Git reports an error, we just ignore anything deeper */ + if (alternate_depth > GIT_ALTERNATES_MAX_DEPTH) + return 0; + + if (git_str_joinpath(&alternates_path, objects_dir, GIT_ALTERNATES_FILE) < 0) + return -1; + + if (git_fs_path_exists(alternates_path.ptr) == false) { + git_str_dispose(&alternates_path); + return 0; + } + + if (git_futils_readbuffer(&alternates_buf, alternates_path.ptr) < 0) { + git_str_dispose(&alternates_path); + return -1; + } + + buffer = (char *)alternates_buf.ptr; + + /* add each alternate as a new backend; one alternate per line */ + while ((alternate = git__strtok(&buffer, "\r\n")) != NULL) { + if (*alternate == '\0' || *alternate == '#') + continue; + + /* Relative path: build based on the current `objects` folder. */ + if (*alternate == '.') { + if ((result = git_str_joinpath(&alternates_path, objects_dir, alternate)) < 0) + break; + alternate = git_str_cstr(&alternates_path); + } + + if ((result = git_odb__add_default_backends(odb, alternate, true, alternate_depth + 1)) < 0) + break; + } + + git_str_dispose(&alternates_path); + git_str_dispose(&alternates_buf); + + return result; +} + +int git_odb_add_disk_alternate(git_odb *odb, const char *path) +{ + return git_odb__add_default_backends(odb, path, true, 0); +} + +int git_odb_set_commit_graph(git_odb *odb, git_commit_graph *cgraph) +{ + int error = 0; + + GIT_ASSERT_ARG(odb); + + if ((error = git_mutex_lock(&odb->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the db lock"); + return error; + } + git_commit_graph_free(odb->cgraph); + odb->cgraph = cgraph; + git_mutex_unlock(&odb->lock); + + return error; +} + +int git_odb_open_ext( + git_odb **out, + const char *objects_dir, + const git_odb_options *opts) +{ + git_odb *db; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(objects_dir); + GIT_ERROR_CHECK_VERSION(opts, GIT_ODB_OPTIONS_VERSION, "git_odb_options"); + + *out = NULL; + + if (git_odb_new_ext(&db, opts) < 0) + return -1; + + if (git_odb__add_default_backends(db, objects_dir, 0, 0) < 0) { + git_odb_free(db); + return -1; + } + + *out = db; + return 0; +} + +int git_odb_open(git_odb **out, const char *objects_dir) +{ + return git_odb_open_ext(out, objects_dir, NULL); +} + +int git_odb__set_caps(git_odb *odb, int caps) +{ + if (caps == GIT_ODB_CAP_FROM_OWNER) { + git_repository *repo = GIT_REFCOUNT_OWNER(odb); + int val; + + if (!repo) { + git_error_set(GIT_ERROR_ODB, "cannot access repository to set odb caps"); + return -1; + } + + if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_FSYNCOBJECTFILES)) + odb->do_fsync = !!val; + } + + return 0; +} + +static void odb_free(git_odb *db) +{ + size_t i; + bool locked = true; + + if (git_mutex_lock(&db->lock) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + locked = false; + } + for (i = 0; i < db->backends.length; ++i) { + backend_internal *internal = git_vector_get(&db->backends, i); + git_odb_backend *backend = internal->backend; + + backend->free(backend); + + git__free(internal); + } + if (locked) + git_mutex_unlock(&db->lock); + + git_commit_graph_free(db->cgraph); + git_vector_dispose(&db->backends); + git_cache_dispose(&db->own_cache); + git_mutex_free(&db->lock); + + git__memzero(db, sizeof(*db)); + git__free(db); +} + +void git_odb_free(git_odb *db) +{ + if (db == NULL) + return; + + GIT_REFCOUNT_DEC(db, odb_free); +} + +static int odb_exists_1( + git_odb *db, + const git_oid *id, + bool only_refreshed) +{ + size_t i; + bool found = false; + int error; + + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return error; + } + for (i = 0; i < db->backends.length && !found; ++i) { + backend_internal *internal = git_vector_get(&db->backends, i); + git_odb_backend *b = internal->backend; + + if (only_refreshed && !b->refresh) + continue; + + if (b->exists != NULL) + found = (bool)b->exists(b, id); + } + git_mutex_unlock(&db->lock); + + return (int)found; +} + +int git_odb__get_commit_graph_file(git_commit_graph_file **out, git_odb *db) +{ + int error = 0; + git_commit_graph_file *result = NULL; + + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the db lock"); + return error; + } + if (!db->cgraph) { + error = GIT_ENOTFOUND; + goto done; + } + error = git_commit_graph_get_file(&result, db->cgraph); + if (error) + goto done; + *out = result; + +done: + git_mutex_unlock(&db->lock); + return error; +} + +static int odb_freshen_1( + git_odb *db, + const git_oid *id, + bool only_refreshed) +{ + size_t i; + bool found = false; + int error; + + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return error; + } + for (i = 0; i < db->backends.length && !found; ++i) { + backend_internal *internal = git_vector_get(&db->backends, i); + git_odb_backend *b = internal->backend; + + if (only_refreshed && !b->refresh) + continue; + + if (b->freshen != NULL) + found = !b->freshen(b, id); + else if (b->exists != NULL) + found = b->exists(b, id); + } + git_mutex_unlock(&db->lock); + + return (int)found; +} + +int git_odb__freshen(git_odb *db, const git_oid *id) +{ + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(id); + + if (odb_freshen_1(db, id, false)) + return 1; + + if (!git_odb_refresh(db)) + return odb_freshen_1(db, id, true); + + /* Failed to refresh, hence not found */ + return 0; +} + +int git_odb_exists(git_odb *db, const git_oid *id) +{ + return git_odb_exists_ext(db, id, 0); +} + +int git_odb_exists_ext(git_odb *db, const git_oid *id, unsigned int flags) +{ + git_odb_object *object; + + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(id); + + if (git_oid_is_zero(id)) + return 0; + + if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) { + git_odb_object_free(object); + return 1; + } + + if (odb_exists_1(db, id, false)) + return 1; + + if (!(flags & GIT_ODB_LOOKUP_NO_REFRESH) && !git_odb_refresh(db)) + return odb_exists_1(db, id, true); + + /* Failed to refresh, hence not found */ + return 0; +} + +static int odb_exists_prefix_1(git_oid *out, git_odb *db, + const git_oid *key, size_t len, bool only_refreshed) +{ + size_t i; + int error = GIT_ENOTFOUND, num_found = 0; + git_oid last_found = GIT_OID_NONE, found; + + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return error; + } + error = GIT_ENOTFOUND; + for (i = 0; i < db->backends.length; ++i) { + backend_internal *internal = git_vector_get(&db->backends, i); + git_odb_backend *b = internal->backend; + + if (only_refreshed && !b->refresh) + continue; + + if (!b->exists_prefix) + continue; + + error = b->exists_prefix(&found, b, key, len); + if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH) + continue; + if (error) { + git_mutex_unlock(&db->lock); + return error; + } + + /* make sure found item doesn't introduce ambiguity */ + if (num_found) { + if (git_oid__cmp(&last_found, &found)) { + git_mutex_unlock(&db->lock); + return git_odb__error_ambiguous("multiple matches for prefix"); + } + } else { + git_oid_cpy(&last_found, &found); + num_found++; + } + } + git_mutex_unlock(&db->lock); + + if (!num_found) + return GIT_ENOTFOUND; + + if (out) + git_oid_cpy(out, &last_found); + + return 0; +} + +int git_odb_exists_prefix( + git_oid *out, git_odb *db, const git_oid *short_id, size_t len) +{ + int error; + git_oid key = GIT_OID_NONE; + + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(short_id); + + if (len < GIT_OID_MINPREFIXLEN) + return git_odb__error_ambiguous("prefix length too short"); + + if (len >= git_oid_hexsize(db->options.oid_type)) { + if (git_odb_exists(db, short_id)) { + if (out) + git_oid_cpy(out, short_id); + return 0; + } else { + return git_odb__error_notfound( + "no match for id prefix", short_id, len); + } + } + + git_oid__cpy_prefix(&key, short_id, len); + + error = odb_exists_prefix_1(out, db, &key, len, false); + + if (error == GIT_ENOTFOUND && !git_odb_refresh(db)) + error = odb_exists_prefix_1(out, db, &key, len, true); + + if (error == GIT_ENOTFOUND) + return git_odb__error_notfound("no match for id prefix", &key, len); + + return error; +} + +int git_odb_expand_ids( + git_odb *db, + git_odb_expand_id *ids, + size_t count) +{ + size_t hex_size, i; + + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(ids); + + hex_size = git_oid_hexsize(db->options.oid_type); + + for (i = 0; i < count; i++) { + git_odb_expand_id *query = &ids[i]; + int error = GIT_EAMBIGUOUS; + + if (!query->type) + query->type = GIT_OBJECT_ANY; + + /* if we have a short OID, expand it first */ + if (query->length >= GIT_OID_MINPREFIXLEN && query->length < hex_size) { + git_oid actual_id; + + error = odb_exists_prefix_1(&actual_id, db, &query->id, query->length, false); + if (!error) { + git_oid_cpy(&query->id, &actual_id); + query->length = (unsigned short)hex_size; + } + } + + /* + * now we ought to have a 40-char OID, either because we've expanded it + * or because the user passed a full OID. Ensure its type is right. + */ + if (query->length >= hex_size) { + git_object_t actual_type; + + error = odb_otype_fast(&actual_type, db, &query->id); + if (!error) { + if (query->type != GIT_OBJECT_ANY && query->type != actual_type) + error = GIT_ENOTFOUND; + else + query->type = actual_type; + } + } + + switch (error) { + /* no errors, so we've successfully expanded the OID */ + case 0: + continue; + + /* the object is missing or ambiguous */ + case GIT_ENOTFOUND: + case GIT_EAMBIGUOUS: + git_oid_clear(&query->id, db->options.oid_type); + query->length = 0; + query->type = 0; + break; + + /* something went very wrong with the ODB; bail hard */ + default: + return error; + } + } + + git_error_clear(); + return 0; +} + +int git_odb_read_header(size_t *len_p, git_object_t *type_p, git_odb *db, const git_oid *id) +{ + int error; + git_odb_object *object = NULL; + + error = git_odb__read_header_or_object(&object, len_p, type_p, db, id); + + if (object) + git_odb_object_free(object); + + return error; +} + +static int odb_read_header_1( + size_t *len_p, git_object_t *type_p, git_odb *db, + const git_oid *id, bool only_refreshed) +{ + size_t i; + git_object_t ht; + bool passthrough = false; + int error; + + if (!only_refreshed && (ht = odb_hardcoded_type(id)) != GIT_OBJECT_INVALID) { + *type_p = ht; + *len_p = 0; + return 0; + } + + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return error; + } + for (i = 0; i < db->backends.length; ++i) { + backend_internal *internal = git_vector_get(&db->backends, i); + git_odb_backend *b = internal->backend; + + if (only_refreshed && !b->refresh) + continue; + + if (!b->read_header) { + passthrough = true; + continue; + } + + error = b->read_header(len_p, type_p, b, id); + + switch (error) { + case GIT_PASSTHROUGH: + passthrough = true; + break; + case GIT_ENOTFOUND: + break; + default: + git_mutex_unlock(&db->lock); + return error; + } + } + git_mutex_unlock(&db->lock); + + return passthrough ? GIT_PASSTHROUGH : GIT_ENOTFOUND; +} + +int git_odb__read_header_or_object( + git_odb_object **out, size_t *len_p, git_object_t *type_p, + git_odb *db, const git_oid *id) +{ + int error = GIT_ENOTFOUND; + git_odb_object *object; + + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(id); + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(len_p); + GIT_ASSERT_ARG(type_p); + + *out = NULL; + + if (git_oid_is_zero(id)) + return error_null_oid(GIT_ENOTFOUND, "cannot read object"); + + if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) { + *len_p = object->cached.size; + *type_p = object->cached.type; + *out = object; + return 0; + } + + error = odb_read_header_1(len_p, type_p, db, id, false); + + if (error == GIT_ENOTFOUND && !git_odb_refresh(db)) + error = odb_read_header_1(len_p, type_p, db, id, true); + + if (error == GIT_ENOTFOUND) + return git_odb__error_notfound("cannot read header for", id, git_oid_hexsize(db->options.oid_type)); + + /* we found the header; return early */ + if (!error) + return 0; + + if (error == GIT_PASSTHROUGH) { + /* + * no backend has header-reading functionality + * so try using `git_odb_read` instead + */ + error = git_odb_read(&object, db, id); + if (!error) { + *len_p = object->cached.size; + *type_p = object->cached.type; + *out = object; + } + } + + return error; +} + +static int odb_read_1( + git_odb_object **out, + git_odb *db, + const git_oid *id, + bool only_refreshed) +{ + size_t i; + git_rawobj raw; + git_odb_object *object; + git_oid hashed; + bool found = false; + int error = 0; + + if (!only_refreshed) { + if ((error = odb_read_hardcoded(&found, &raw, id)) < 0) + return error; + } + + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return error; + } + for (i = 0; i < db->backends.length && !found; ++i) { + backend_internal *internal = git_vector_get(&db->backends, i); + git_odb_backend *b = internal->backend; + + if (only_refreshed && !b->refresh) + continue; + + if (b->read != NULL) { + error = b->read(&raw.data, &raw.len, &raw.type, b, id); + if (error == GIT_PASSTHROUGH || error == GIT_ENOTFOUND) + continue; + + if (error < 0) { + git_mutex_unlock(&db->lock); + return error; + } + + found = true; + } + } + git_mutex_unlock(&db->lock); + + if (!found) + return GIT_ENOTFOUND; + + if (git_odb__strict_hash_verification) { + if ((error = git_odb__hash(&hashed, raw.data, raw.len, raw.type, db->options.oid_type)) < 0) + goto out; + + if (!git_oid_equal(id, &hashed)) { + error = git_odb__error_mismatch(id, &hashed); + goto out; + } + } + + git_error_clear(); + if ((object = odb_object__alloc(id, &raw)) == NULL) { + error = -1; + goto out; + } + + *out = git_cache_store_raw(odb_cache(db), object); + +out: + if (error) + git__free(raw.data); + return error; +} + +int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id) +{ + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(id); + + if (git_oid_is_zero(id)) + return error_null_oid(GIT_ENOTFOUND, "cannot read object"); + + *out = git_cache_get_raw(odb_cache(db), id); + if (*out != NULL) + return 0; + + error = odb_read_1(out, db, id, false); + + if (error == GIT_ENOTFOUND && !git_odb_refresh(db)) + error = odb_read_1(out, db, id, true); + + if (error == GIT_ENOTFOUND) + return git_odb__error_notfound("no match for id", id, git_oid_hexsize(git_oid_type(id))); + + return error; +} + +static int odb_otype_fast(git_object_t *type_p, git_odb *db, const git_oid *id) +{ + git_odb_object *object; + size_t _unused; + int error; + + if (git_oid_is_zero(id)) + return error_null_oid(GIT_ENOTFOUND, "cannot get object type"); + + if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) { + *type_p = object->cached.type; + git_odb_object_free(object); + return 0; + } + + error = odb_read_header_1(&_unused, type_p, db, id, false); + + if (error == GIT_PASSTHROUGH) { + error = odb_read_1(&object, db, id, false); + if (!error) + *type_p = object->cached.type; + git_odb_object_free(object); + } + + return error; +} + +static int read_prefix_1(git_odb_object **out, git_odb *db, + const git_oid *key, size_t len, bool only_refreshed) +{ + size_t i; + int error = 0; + git_oid found_full_oid = GIT_OID_NONE; + git_rawobj raw = {0}; + void *data = NULL; + bool found = false; + git_odb_object *object; + + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return error; + } + for (i = 0; i < db->backends.length; ++i) { + backend_internal *internal = git_vector_get(&db->backends, i); + git_odb_backend *b = internal->backend; + + if (only_refreshed && !b->refresh) + continue; + + if (b->read_prefix != NULL) { + git_oid full_oid; + error = b->read_prefix(&full_oid, &raw.data, &raw.len, &raw.type, b, key, len); + + if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH) { + error = 0; + continue; + } + + if (error) { + git_mutex_unlock(&db->lock); + goto out; + } + + git__free(data); + data = raw.data; + + if (found && git_oid__cmp(&full_oid, &found_full_oid)) { + git_str buf = GIT_STR_INIT; + const char *idstr; + + if ((idstr = git_oid_tostr_s(&full_oid)) == NULL) { + git_str_puts(&buf, "failed to parse object id"); + } else { + git_str_printf(&buf, "multiple matches for prefix: %s", idstr); + + if ((idstr = git_oid_tostr_s(&found_full_oid)) != NULL) + git_str_printf(&buf, " %s", idstr); + } + + error = git_odb__error_ambiguous(buf.ptr); + git_str_dispose(&buf); + git_mutex_unlock(&db->lock); + goto out; + } + + found_full_oid = full_oid; + found = true; + } + } + git_mutex_unlock(&db->lock); + + if (!found) + return GIT_ENOTFOUND; + + if (git_odb__strict_hash_verification) { + git_oid hash; + + if ((error = git_odb__hash(&hash, raw.data, raw.len, raw.type, db->options.oid_type)) < 0) + goto out; + + if (!git_oid_equal(&found_full_oid, &hash)) { + error = git_odb__error_mismatch(&found_full_oid, &hash); + goto out; + } + } + + if ((object = odb_object__alloc(&found_full_oid, &raw)) == NULL) { + error = -1; + goto out; + } + + *out = git_cache_store_raw(odb_cache(db), object); + +out: + if (error) + git__free(raw.data); + + return error; +} + +int git_odb_read_prefix( + git_odb_object **out, git_odb *db, const git_oid *short_id, size_t len) +{ + git_oid key = GIT_OID_NONE; + size_t hex_size; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(db); + + hex_size = git_oid_hexsize(db->options.oid_type); + + if (len < GIT_OID_MINPREFIXLEN) + return git_odb__error_ambiguous("prefix length too short"); + + if (len > hex_size) + len = hex_size; + + if (len == hex_size) { + *out = git_cache_get_raw(odb_cache(db), short_id); + if (*out != NULL) + return 0; + } + + git_oid__cpy_prefix(&key, short_id, len); + + error = read_prefix_1(out, db, &key, len, false); + + if (error == GIT_ENOTFOUND && !git_odb_refresh(db)) + error = read_prefix_1(out, db, &key, len, true); + + if (error == GIT_ENOTFOUND) + return git_odb__error_notfound("no match for prefix", &key, len); + + return error; +} + +int git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload) +{ + unsigned int i; + git_vector backends = GIT_VECTOR_INIT; + backend_internal *internal; + int error = 0; + + /* Make a copy of the backends vector to invoke the callback without holding the lock. */ + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + goto cleanup; + } + error = git_vector_dup(&backends, &db->backends, NULL); + git_mutex_unlock(&db->lock); + + if (error < 0) + goto cleanup; + + git_vector_foreach(&backends, i, internal) { + git_odb_backend *b = internal->backend; + error = b->foreach(b, cb, payload); + if (error != 0) + goto cleanup; + } + +cleanup: + git_vector_dispose(&backends); + + return error; +} + +int git_odb_write( + git_oid *oid, git_odb *db, const void *data, size_t len, git_object_t type) +{ + size_t i; + int error; + git_odb_stream *stream; + + GIT_ASSERT_ARG(oid); + GIT_ASSERT_ARG(db); + + if ((error = git_odb__hash(oid, data, len, type, db->options.oid_type)) < 0) + return error; + + if (git_oid_is_zero(oid)) + return error_null_oid(GIT_EINVALID, "cannot write object"); + + if (git_odb__freshen(db, oid)) + return 0; + + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return error; + } + for (i = 0, error = GIT_ERROR; i < db->backends.length && error < 0; ++i) { + backend_internal *internal = git_vector_get(&db->backends, i); + git_odb_backend *b = internal->backend; + + /* we don't write in alternates! */ + if (internal->is_alternate) + continue; + + if (b->write != NULL) + error = b->write(b, oid, data, len, type); + } + git_mutex_unlock(&db->lock); + + if (!error || error == GIT_PASSTHROUGH) + return 0; + + /* if no backends were able to write the object directly, we try a + * streaming write to the backends; just write the whole object into the + * stream in one push + */ + if ((error = git_odb_open_wstream(&stream, db, len, type)) != 0) + return error; + + if ((error = stream->write(stream, data, len)) == 0) + error = stream->finalize_write(stream, oid); + + git_odb_stream_free(stream); + return error; +} + +static int hash_header(git_hash_ctx *ctx, git_object_size_t size, git_object_t type) +{ + char header[64]; + size_t hdrlen; + int error; + + if ((error = git_odb__format_object_header(&hdrlen, + header, sizeof(header), size, type)) < 0) + return error; + + return git_hash_update(ctx, header, hdrlen); +} + +int git_odb_open_wstream( + git_odb_stream **stream, git_odb *db, git_object_size_t size, git_object_t type) +{ + size_t i, writes = 0; + int error = GIT_ERROR; + git_hash_ctx *ctx = NULL; + + GIT_ASSERT_ARG(stream); + GIT_ASSERT_ARG(db); + + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return error; + } + error = GIT_ERROR; + for (i = 0; i < db->backends.length && error < 0; ++i) { + backend_internal *internal = git_vector_get(&db->backends, i); + git_odb_backend *b = internal->backend; + + /* we don't write in alternates! */ + if (internal->is_alternate) + continue; + + if (b->writestream != NULL) { + ++writes; + error = b->writestream(stream, b, size, type); + } else if (b->write != NULL) { + ++writes; + error = init_fake_wstream(stream, b, size, type); + } + } + git_mutex_unlock(&db->lock); + + if (error < 0) { + if (error == GIT_PASSTHROUGH) + error = 0; + else if (!writes) + error = git_odb__error_unsupported_in_backend("write object"); + + goto done; + } + + ctx = git__malloc(sizeof(git_hash_ctx)); + GIT_ERROR_CHECK_ALLOC(ctx); + + if ((error = git_hash_ctx_init(ctx, git_oid_algorithm(db->options.oid_type))) < 0 || + (error = hash_header(ctx, size, type)) < 0) + goto done; + +#ifdef GIT_EXPERIMENTAL_SHA256 + (*stream)->oid_type = db->options.oid_type; +#endif + (*stream)->hash_ctx = ctx; + (*stream)->declared_size = size; + (*stream)->received_bytes = 0; + +done: + if (error) + git__free(ctx); + return error; +} + +static int git_odb_stream__invalid_length( + const git_odb_stream *stream, + const char *action) +{ + git_error_set(GIT_ERROR_ODB, + "cannot %s - " + "Invalid length. %"PRId64" was expected. The " + "total size of the received chunks amounts to %"PRId64".", + action, stream->declared_size, stream->received_bytes); + + return -1; +} + +int git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len) +{ + git_hash_update(stream->hash_ctx, buffer, len); + + stream->received_bytes += len; + + if (stream->received_bytes > stream->declared_size) + return git_odb_stream__invalid_length(stream, + "stream_write()"); + + return stream->write(stream, buffer, len); +} + +int git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream) +{ + if (stream->received_bytes != stream->declared_size) + return git_odb_stream__invalid_length(stream, + "stream_finalize_write()"); + + git_hash_final(out->id, stream->hash_ctx); + +#ifdef GIT_EXPERIMENTAL_SHA256 + out->type = stream->oid_type; +#endif + + if (git_odb__freshen(stream->backend->odb, out)) + return 0; + + return stream->finalize_write(stream, out); +} + +int git_odb_stream_read(git_odb_stream *stream, char *buffer, size_t len) +{ + return stream->read(stream, buffer, len); +} + +void git_odb_stream_free(git_odb_stream *stream) +{ + if (stream == NULL) + return; + + if (stream->hash_ctx) + git_hash_ctx_cleanup(stream->hash_ctx); + git__free(stream->hash_ctx); + stream->free(stream); +} + +int git_odb_open_rstream( + git_odb_stream **stream, + size_t *len, + git_object_t *type, + git_odb *db, + const git_oid *oid) +{ + size_t i, reads = 0; + int error = GIT_ERROR; + + GIT_ASSERT_ARG(stream); + GIT_ASSERT_ARG(db); + + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return error; + } + error = GIT_ERROR; + for (i = 0; i < db->backends.length && error < 0; ++i) { + backend_internal *internal = git_vector_get(&db->backends, i); + git_odb_backend *b = internal->backend; + + if (b->readstream != NULL) { + ++reads; + error = b->readstream(stream, len, type, b, oid); + } + } + git_mutex_unlock(&db->lock); + + if (error == GIT_PASSTHROUGH) + error = 0; + if (error < 0 && !reads) + error = git_odb__error_unsupported_in_backend("read object streamed"); + + return error; +} + +int git_odb_write_pack(struct git_odb_writepack **out, git_odb *db, git_indexer_progress_cb progress_cb, void *progress_payload) +{ + size_t i, writes = 0; + int error = GIT_ERROR; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(db); + + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return error; + } + error = GIT_ERROR; + for (i = 0; i < db->backends.length && error < 0; ++i) { + backend_internal *internal = git_vector_get(&db->backends, i); + git_odb_backend *b = internal->backend; + + /* we don't write in alternates! */ + if (internal->is_alternate) + continue; + + if (b->writepack != NULL) { + ++writes; + error = b->writepack(out, b, db, progress_cb, progress_payload); + } + } + git_mutex_unlock(&db->lock); + + if (error == GIT_PASSTHROUGH) + error = 0; + if (error < 0 && !writes) + error = git_odb__error_unsupported_in_backend("write pack"); + + return error; +} + +int git_odb_write_multi_pack_index(git_odb *db) +{ + size_t i, writes = 0; + int error = GIT_ERROR; + + GIT_ASSERT_ARG(db); + + for (i = 0; i < db->backends.length && error < 0; ++i) { + backend_internal *internal = git_vector_get(&db->backends, i); + git_odb_backend *b = internal->backend; + + /* we don't write in alternates! */ + if (internal->is_alternate) + continue; + + if (b->writemidx != NULL) { + ++writes; + error = b->writemidx(b); + } + } + + if (error == GIT_PASSTHROUGH) + error = 0; + if (error < 0 && !writes) + error = git_odb__error_unsupported_in_backend("write multi-pack-index"); + + return error; +} + +void *git_odb_backend_data_alloc(git_odb_backend *backend, size_t len) +{ + GIT_UNUSED(backend); + return git__malloc(len); +} + +#ifndef GIT_DEPRECATE_HARD +void *git_odb_backend_malloc(git_odb_backend *backend, size_t len) +{ + return git_odb_backend_data_alloc(backend, len); +} +#endif + +void git_odb_backend_data_free(git_odb_backend *backend, void *data) +{ + GIT_UNUSED(backend); + git__free(data); +} + +int git_odb_refresh(git_odb *db) +{ + size_t i; + int error; + + GIT_ASSERT_ARG(db); + + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return error; + } + for (i = 0; i < db->backends.length; ++i) { + backend_internal *internal = git_vector_get(&db->backends, i); + git_odb_backend *b = internal->backend; + + if (b->refresh != NULL) { + int error = b->refresh(b); + if (error < 0) { + git_mutex_unlock(&db->lock); + return error; + } + } + } + if (db->cgraph) + git_commit_graph_refresh(db->cgraph); + git_mutex_unlock(&db->lock); + + return 0; +} + +int git_odb__error_mismatch(const git_oid *expected, const git_oid *actual) +{ + char expected_oid[GIT_OID_MAX_HEXSIZE + 1], + actual_oid[GIT_OID_MAX_HEXSIZE + 1]; + + git_oid_tostr(expected_oid, git_oid_hexsize(git_oid_type(expected)) + 1, expected); + git_oid_tostr(actual_oid, git_oid_hexsize(git_oid_type(actual)) + 1, actual); + + git_error_set(GIT_ERROR_ODB, "object hash mismatch - expected %s but got %s", + expected_oid, actual_oid); + + return GIT_EMISMATCH; +} + +int git_odb__error_notfound( + const char *message, const git_oid *oid, size_t oid_len) +{ + if (oid != NULL) { + char oid_str[GIT_OID_MAX_HEXSIZE + 1]; + git_oid_tostr(oid_str, oid_len+1, oid); + git_error_set(GIT_ERROR_ODB, "object not found - %s (%.*s)", + message, (int) oid_len, oid_str); + } else + git_error_set(GIT_ERROR_ODB, "object not found - %s", message); + + return GIT_ENOTFOUND; +} + +static int error_null_oid(int error, const char *message) +{ + git_error_set(GIT_ERROR_ODB, "odb: %s: null OID cannot exist", message); + return error; +} + +int git_odb__error_ambiguous(const char *message) +{ + git_error_set(GIT_ERROR_ODB, "ambiguous OID prefix - %s", message); + return GIT_EAMBIGUOUS; +} + +int git_odb_init_backend(git_odb_backend *backend, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + backend, version, git_odb_backend, GIT_ODB_BACKEND_INIT); + return 0; +} diff --git a/src/libgit2/odb.h b/src/libgit2/odb.h new file mode 100644 index 00000000000..fa50b984971 --- /dev/null +++ b/src/libgit2/odb.h @@ -0,0 +1,194 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_odb_h__ +#define INCLUDE_odb_h__ + +#include "common.h" + +#include "git2/odb.h" +#include "git2/odb_backend.h" +#include "git2/oid.h" +#include "git2/types.h" +#include "git2/sys/commit_graph.h" + +#include "cache.h" +#include "commit_graph.h" +#include "filter.h" +#include "posix.h" +#include "vector.h" + +#define GIT_OBJECTS_DIR "objects/" +#define GIT_OBJECT_DIR_MODE 0777 +#define GIT_OBJECT_FILE_MODE 0444 + +#define GIT_ODB_DEFAULT_LOOSE_PRIORITY 1 +#define GIT_ODB_DEFAULT_PACKED_PRIORITY 2 + +extern bool git_odb__strict_hash_verification; + +/* DO NOT EXPORT */ +typedef struct { + void *data; /**< Raw, decompressed object data. */ + size_t len; /**< Total number of bytes in data. */ + git_object_t type; /**< Type of this object. */ +} git_rawobj; + +/* EXPORT */ +struct git_odb_object { + git_cached_obj cached; + void *buffer; +}; + +/* EXPORT */ +struct git_odb { + git_refcount rc; + git_mutex lock; /* protects backends */ + git_odb_options options; + git_vector backends; + git_cache own_cache; + git_commit_graph *cgraph; + unsigned int do_fsync :1; +}; + +typedef enum { + GIT_ODB_CAP_FROM_OWNER = -1 +} git_odb_cap_t; + +/* + * Set the capabilities for the object database. + */ +int git_odb__set_caps(git_odb *odb, int caps); + +/* + * Add the default loose and packed backends for a database. + */ +int git_odb__add_default_backends( + git_odb *db, const char *objects_dir, + bool as_alternates, int alternate_depth); + +/* + * Hash a git_rawobj internally. + * The `git_rawobj` is supposed to be previously initialized + */ +int git_odb__hashobj(git_oid *id, git_rawobj *obj, git_oid_t oid_type); + +/* + * Format the object header such as it would appear in the on-disk object + */ +int git_odb__format_object_header(size_t *out_len, char *hdr, size_t hdr_size, git_object_size_t obj_len, git_object_t obj_type); + +/* + * Hash an open file descriptor. + * This is a performance call when the contents of a fd need to be hashed, + * but the fd is already open and we have the size of the contents. + * + * Saves us some `stat` calls. + * + * The fd is never closed, not even on error. It must be opened and closed + * by the caller + */ +int git_odb__hashfd( + git_oid *out, + git_file fd, + size_t size, + git_object_t object_type, + git_oid_t oid_type); + +/* + * Hash an open file descriptor applying an array of filters + * Acts just like git_odb__hashfd with the addition of filters... + */ +int git_odb__hashfd_filtered( + git_oid *out, + git_file fd, + size_t len, + git_object_t object_type, + git_oid_t oid_type, + git_filter_list *fl); + +/* + * Hash a `path`, assuming it could be a POSIX symlink: if the path is a + * symlink, then the raw contents of the symlink will be hashed. Otherwise, + * this will fallback to `git_odb__hashfd`. + * + * The hash type for this call is always `GIT_OBJECT_BLOB` because + * symlinks may only point to blobs. + */ +int git_odb__hashlink(git_oid *out, const char *path, git_oid_t oid_type); + +/** + * Generate a GIT_EMISMATCH error for the ODB. + */ +int git_odb__error_mismatch( + const git_oid *expected, const git_oid *actual); + +/* + * Generate a GIT_ENOTFOUND error for the ODB. + */ +int git_odb__error_notfound( + const char *message, const git_oid *oid, size_t oid_len); + +/* + * Generate a GIT_EAMBIGUOUS error for the ODB. + */ +int git_odb__error_ambiguous(const char *message); + +/* + * Attempt to read object header or just return whole object if it could + * not be read. + */ +int git_odb__read_header_or_object( + git_odb_object **out, size_t *len_p, git_object_t *type_p, + git_odb *db, const git_oid *id); + +/* + * Attempt to get the ODB's commit-graph file. This object is still owned by + * the ODB. If the repository does not contain a commit-graph, it will return + * GIT_ENOTFOUND. + */ +int git_odb__get_commit_graph_file(git_commit_graph_file **out, git_odb *odb); + +/* freshen an entry in the object database */ +int git_odb__freshen(git_odb *db, const git_oid *id); + +/* fully free the object; internal method, DO NOT EXPORT */ +void git_odb_object__free(void *object); + +/* SHA256 support */ + +int git_odb__hash( + git_oid *out, + const void *data, + size_t len, + git_object_t object_type, + git_oid_t oid_type); + +int git_odb__hashfile( + git_oid *out, + const char *path, + git_object_t object_type, + git_oid_t oid_type); + +int git_odb__backend_loose( + git_odb_backend **out, + const char *objects_dir, + git_odb_backend_loose_options *opts); + +#ifndef GIT_EXPERIMENTAL_SHA256 + +int git_odb_open_ext( + git_odb **odb_out, + const char *objects_dir, + const git_odb_options *opts); + +int git_odb_new_ext( + git_odb **odb, + const git_odb_options *opts); + +#endif + +#endif diff --git a/src/libgit2/odb_loose.c b/src/libgit2/odb_loose.c new file mode 100644 index 00000000000..a91e296ae67 --- /dev/null +++ b/src/libgit2/odb_loose.c @@ -0,0 +1,1243 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include +#include "git2/object.h" +#include "git2/sys/odb_backend.h" +#include "futils.h" +#include "hash.h" +#include "odb.h" +#include "delta.h" +#include "filebuf.h" +#include "object.h" +#include "zstream.h" + +#include "git2/odb_backend.h" +#include "git2/types.h" + +/* maximum possible header length */ +#define MAX_HEADER_LEN 64 + +typedef struct { /* object header data */ + git_object_t type; /* object type */ + size_t size; /* object size */ +} obj_hdr; + +typedef struct { + git_odb_stream stream; + git_filebuf fbuf; +} loose_writestream; + +typedef struct { + git_odb_stream stream; + git_map map; + char start[MAX_HEADER_LEN]; + size_t start_len; + size_t start_read; + git_zstream zstream; +} loose_readstream; + +typedef struct loose_backend { + git_odb_backend parent; + + git_odb_backend_loose_options options; + size_t oid_hexsize; + + size_t objects_dirlen; + char objects_dir[GIT_FLEX_ARRAY]; +} loose_backend; + +/* State structure for exploring directories, + * in order to locate objects matching a short oid. + */ +typedef struct { + loose_backend *backend; + + size_t dir_len; + + /* Hex formatted oid to match (and its length) */ + unsigned char short_oid[GIT_OID_MAX_HEXSIZE]; + size_t short_oid_len; + + /* Number of matching objects found so far */ + int found; + + /* Hex formatted oid of the object found */ + unsigned char res_oid[GIT_OID_MAX_HEXSIZE]; +} loose_locate_object_state; + + +/*********************************************************** + * + * MISCELLANEOUS HELPER FUNCTIONS + * + ***********************************************************/ + +static int object_file_name( + git_str *name, const loose_backend *be, const git_oid *id) +{ + /* append loose object filename: aa/aaa... (41 bytes plus NUL) */ + size_t path_size = be->oid_hexsize + 1; + + git_str_set(name, be->objects_dir, be->objects_dirlen); + git_fs_path_to_dir(name); + + if (git_str_grow_by(name, path_size + 1) < 0) + return -1; + + git_oid_pathfmt(name->ptr + name->size, id); + name->size += path_size; + name->ptr[name->size] = '\0'; + + return 0; +} + +static int object_mkdir(const git_str *name, const loose_backend *be) +{ + return git_futils_mkdir_relative( + name->ptr + be->objects_dirlen, + be->objects_dir, + be->options.dir_mode, + GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR, NULL); +} + +static int parse_header_packlike( + obj_hdr *out, size_t *out_len, const unsigned char *data, size_t len) +{ + unsigned long c; + size_t shift, size, used = 0; + + if (len == 0) + goto on_error; + + c = data[used++]; + out->type = (c >> 4) & 7; + + size = c & 15; + shift = 4; + while (c & 0x80) { + if (len <= used) + goto on_error; + + if (sizeof(size_t) * 8 <= shift) + goto on_error; + + c = data[used++]; + size += (c & 0x7f) << shift; + shift += 7; + } + + out->size = size; + + if (out_len) + *out_len = used; + + return 0; + +on_error: + git_error_set(GIT_ERROR_OBJECT, "failed to parse loose object: invalid header"); + return -1; +} + +static int parse_header( + obj_hdr *out, + size_t *out_len, + const unsigned char *_data, + size_t data_len) +{ + const char *data = (char *)_data; + size_t i, typename_len, size_idx, size_len; + int64_t size; + + *out_len = 0; + + /* find the object type name */ + for (i = 0, typename_len = 0; i < data_len; i++, typename_len++) { + if (data[i] == ' ') + break; + } + + if (typename_len == data_len) + goto on_error; + + out->type = git_object_stringn2type(data, typename_len); + + size_idx = typename_len + 1; + for (i = size_idx, size_len = 0; i < data_len; i++, size_len++) { + if (data[i] == '\0') + break; + } + + if (i == data_len) + goto on_error; + + if (git__strntol64(&size, &data[size_idx], size_len, NULL, 10) < 0 || + size < 0) + goto on_error; + + if ((uint64_t)size > SIZE_MAX) { + git_error_set(GIT_ERROR_OBJECT, "object is larger than available memory"); + return -1; + } + + out->size = (size_t)size; + + if (GIT_ADD_SIZET_OVERFLOW(out_len, i, 1)) + goto on_error; + + return 0; + +on_error: + git_error_set(GIT_ERROR_OBJECT, "failed to parse loose object: invalid header"); + return -1; +} + +static int is_zlib_compressed_data(unsigned char *data, size_t data_len) +{ + unsigned int w; + + if (data_len < 2) + return 0; + + w = ((unsigned int)(data[0]) << 8) + data[1]; + return (data[0] & 0x8F) == 0x08 && !(w % 31); +} + +/*********************************************************** + * + * ODB OBJECT READING & WRITING + * + * Backend for the public API; read headers and full objects + * from the ODB. Write raw data to the ODB. + * + ***********************************************************/ + + +/* + * At one point, there was a loose object format that was intended to + * mimic the format used in pack-files. This was to allow easy copying + * of loose object data into packs. This format is no longer used, but + * we must still read it. + */ +static int read_loose_packlike(git_rawobj *out, git_str *obj) +{ + git_str body = GIT_STR_INIT; + const unsigned char *obj_data; + obj_hdr hdr; + size_t obj_len, head_len, alloc_size; + int error; + + obj_data = (unsigned char *)obj->ptr; + obj_len = obj->size; + + /* + * read the object header, which is an (uncompressed) + * binary encoding of the object type and size. + */ + if ((error = parse_header_packlike(&hdr, &head_len, obj_data, obj_len)) < 0) + goto done; + + if (!git_object_type_is_valid(hdr.type) || head_len > obj_len) { + git_error_set(GIT_ERROR_ODB, "failed to inflate loose object"); + error = -1; + goto done; + } + + obj_data += head_len; + obj_len -= head_len; + + /* + * allocate a buffer and inflate the data into it + */ + if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, hdr.size, 1) || + git_str_init(&body, alloc_size) < 0) { + error = -1; + goto done; + } + + if ((error = git_zstream_inflatebuf(&body, obj_data, obj_len)) < 0) + goto done; + + out->len = hdr.size; + out->type = hdr.type; + out->data = git_str_detach(&body); + +done: + git_str_dispose(&body); + return error; +} + +static int read_loose_standard(git_rawobj *out, git_str *obj) +{ + git_zstream zstream = GIT_ZSTREAM_INIT; + unsigned char head[MAX_HEADER_LEN], *body = NULL; + size_t decompressed, head_len, body_len, alloc_size; + obj_hdr hdr; + int error; + + if ((error = git_zstream_init(&zstream, GIT_ZSTREAM_INFLATE)) < 0 || + (error = git_zstream_set_input(&zstream, git_str_cstr(obj), git_str_len(obj))) < 0) + goto done; + + decompressed = sizeof(head); + + /* + * inflate the initial part of the compressed buffer in order to + * parse the header; read the largest header possible, then push the + * remainder into the body buffer. + */ + if ((error = git_zstream_get_output(head, &decompressed, &zstream)) < 0 || + (error = parse_header(&hdr, &head_len, head, decompressed)) < 0) + goto done; + + if (!git_object_type_is_valid(hdr.type)) { + git_error_set(GIT_ERROR_ODB, "failed to inflate disk object"); + error = -1; + goto done; + } + + /* + * allocate a buffer and inflate the object data into it + * (including the initial sequence in the head buffer). + */ + if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, hdr.size, 1) || + (body = git__calloc(1, alloc_size)) == NULL) { + error = -1; + goto done; + } + + GIT_ASSERT(decompressed >= head_len); + body_len = decompressed - head_len; + + if (body_len) + memcpy(body, head + head_len, body_len); + + decompressed = hdr.size - body_len; + if ((error = git_zstream_get_output(body + body_len, &decompressed, &zstream)) < 0) + goto done; + + if (!git_zstream_done(&zstream)) { + git_error_set(GIT_ERROR_ZLIB, "failed to finish zlib inflation: stream aborted prematurely"); + error = -1; + goto done; + } + + body[hdr.size] = '\0'; + + out->data = body; + out->len = hdr.size; + out->type = hdr.type; + +done: + if (error < 0) + git__free(body); + + git_zstream_free(&zstream); + return error; +} + +static int read_loose(git_rawobj *out, git_str *loc) +{ + int error; + git_str obj = GIT_STR_INIT; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(loc); + + if (git_str_oom(loc)) + return -1; + + out->data = NULL; + out->len = 0; + out->type = GIT_OBJECT_INVALID; + + if ((error = git_futils_readbuffer(&obj, loc->ptr)) < 0) + goto done; + + if (!is_zlib_compressed_data((unsigned char *)obj.ptr, obj.size)) + error = read_loose_packlike(out, &obj); + else + error = read_loose_standard(out, &obj); + +done: + git_str_dispose(&obj); + return error; +} + +static int read_header_loose_packlike( + git_rawobj *out, const unsigned char *data, size_t len) +{ + obj_hdr hdr; + size_t header_len; + int error; + + if ((error = parse_header_packlike(&hdr, &header_len, data, len)) < 0) + return error; + + out->len = hdr.size; + out->type = hdr.type; + + return error; +} + +static int read_header_loose_standard( + git_rawobj *out, const unsigned char *data, size_t len) +{ + git_zstream zs = GIT_ZSTREAM_INIT; + obj_hdr hdr = {0}; + unsigned char inflated[MAX_HEADER_LEN] = {0}; + size_t header_len, inflated_len = sizeof(inflated); + int error; + + if ((error = git_zstream_init(&zs, GIT_ZSTREAM_INFLATE)) < 0 || + (error = git_zstream_set_input(&zs, data, len)) < 0 || + (error = git_zstream_get_output_chunk(inflated, &inflated_len, &zs)) < 0 || + (error = parse_header(&hdr, &header_len, inflated, inflated_len)) < 0) + goto done; + + out->len = hdr.size; + out->type = hdr.type; + +done: + git_zstream_free(&zs); + return error; +} + +static int read_header_loose(git_rawobj *out, git_str *loc) +{ + unsigned char obj[1024]; + ssize_t obj_len; + int fd, error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(loc); + + if (git_str_oom(loc)) + return -1; + + out->data = NULL; + + if ((error = fd = git_futils_open_ro(loc->ptr)) < 0) + goto done; + + if ((obj_len = p_read(fd, obj, sizeof(obj))) < 0) { + error = (int)obj_len; + goto done; + } + + if (!is_zlib_compressed_data(obj, (size_t)obj_len)) + error = read_header_loose_packlike(out, obj, (size_t)obj_len); + else + error = read_header_loose_standard(out, obj, (size_t)obj_len); + + if (!error && !git_object_type_is_valid(out->type)) { + git_error_set(GIT_ERROR_ZLIB, "failed to read loose object header"); + error = -1; + goto done; + } + +done: + if (fd >= 0) + p_close(fd); + return error; +} + +static int locate_object( + git_str *object_location, + loose_backend *backend, + const git_oid *oid) +{ + int error = object_file_name(object_location, backend, oid); + + if (!error && !git_fs_path_exists(object_location->ptr)) + return GIT_ENOTFOUND; + + return error; +} + +/* Explore an entry of a directory and see if it matches a short oid */ +static int fn_locate_object_short_oid(void *state, git_str *pathbuf) { + loose_locate_object_state *sstate = (loose_locate_object_state *)state; + size_t hex_size = sstate->backend->oid_hexsize; + + if (git_str_len(pathbuf) - sstate->dir_len != hex_size - 2) { + /* Entry cannot be an object. Continue to next entry */ + return 0; + } + + if (git_fs_path_isdir(pathbuf->ptr) == false) { + /* We are already in the directory matching the 2 first hex characters, + * compare the first ncmp characters of the oids */ + if (!memcmp(sstate->short_oid + 2, + (unsigned char *)pathbuf->ptr + sstate->dir_len, + sstate->short_oid_len - 2)) { + + if (!sstate->found) { + sstate->res_oid[0] = sstate->short_oid[0]; + sstate->res_oid[1] = sstate->short_oid[1]; + memcpy(sstate->res_oid + 2, + pathbuf->ptr+sstate->dir_len, + hex_size - 2); + } + sstate->found++; + } + } + + if (sstate->found > 1) + return GIT_EAMBIGUOUS; + + return 0; +} + +/* Locate an object matching a given short oid */ +static int locate_object_short_oid( + git_str *object_location, + git_oid *res_oid, + loose_backend *backend, + const git_oid *short_oid, + size_t len) +{ + char *objects_dir = backend->objects_dir; + size_t dir_len = strlen(objects_dir), alloc_len; + loose_locate_object_state state; + int error; + + /* prealloc memory for OBJ_DIR/xx/xx..38x..xx */ + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, dir_len, backend->oid_hexsize); + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 3); + if (git_str_grow(object_location, alloc_len) < 0) + return -1; + + git_str_set(object_location, objects_dir, dir_len); + git_fs_path_to_dir(object_location); + + /* save adjusted position at end of dir so it can be restored later */ + dir_len = git_str_len(object_location); + + /* Convert raw oid to hex formatted oid */ + git_oid_fmt((char *)state.short_oid, short_oid); + + /* Explore OBJ_DIR/xx/ where xx is the beginning of hex formatted short oid */ + if (git_str_put(object_location, (char *)state.short_oid, 3) < 0) + return -1; + object_location->ptr[object_location->size - 1] = '/'; + + /* Check that directory exists */ + if (git_fs_path_isdir(object_location->ptr) == false) + return git_odb__error_notfound("no matching loose object for prefix", + short_oid, len); + + state.backend = backend; + state.dir_len = git_str_len(object_location); + state.short_oid_len = len; + state.found = 0; + + /* Explore directory to find a unique object matching short_oid */ + error = git_fs_path_direach( + object_location, 0, fn_locate_object_short_oid, &state); + if (error < 0 && error != GIT_EAMBIGUOUS) + return error; + + if (!state.found) + return git_odb__error_notfound("no matching loose object for prefix", + short_oid, len); + + if (state.found > 1) + return git_odb__error_ambiguous("multiple matches in loose objects"); + + /* Convert obtained hex formatted oid to raw */ + error = git_oid_from_prefix(res_oid, (char *)state.res_oid, + git_oid_hexsize(backend->options.oid_type), + backend->options.oid_type); + + if (error) + return error; + + /* Update the location according to the oid obtained */ + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, dir_len, backend->oid_hexsize); + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2); + + git_str_truncate(object_location, dir_len); + if (git_str_grow(object_location, alloc_len) < 0) + return -1; + + git_oid_pathfmt(object_location->ptr + dir_len, res_oid); + + object_location->size += backend->oid_hexsize + 1; + object_location->ptr[object_location->size] = '\0'; + + return 0; +} + +/*********************************************************** + * + * LOOSE BACKEND PUBLIC API + * + * Implement the git_odb_backend API calls + * + ***********************************************************/ + +static int loose_backend__read_header(size_t *len_p, git_object_t *type_p, git_odb_backend *backend, const git_oid *oid) +{ + git_str object_path = GIT_STR_INIT; + git_rawobj raw; + int error; + + GIT_ASSERT_ARG(backend); + GIT_ASSERT_ARG(oid); + + raw.len = 0; + raw.type = GIT_OBJECT_INVALID; + + if (locate_object(&object_path, (loose_backend *)backend, oid) < 0) { + error = git_odb__error_notfound("no matching loose object", + oid, ((struct loose_backend *)backend)->oid_hexsize); + } else if ((error = read_header_loose(&raw, &object_path)) == 0) { + *len_p = raw.len; + *type_p = raw.type; + } + + git_str_dispose(&object_path); + + return error; +} + +static int loose_backend__read(void **buffer_p, size_t *len_p, git_object_t *type_p, git_odb_backend *backend, const git_oid *oid) +{ + git_str object_path = GIT_STR_INIT; + git_rawobj raw; + int error = 0; + + GIT_ASSERT_ARG(backend); + GIT_ASSERT_ARG(oid); + + if (locate_object(&object_path, (loose_backend *)backend, oid) < 0) { + error = git_odb__error_notfound("no matching loose object", + oid, ((struct loose_backend *)backend)->oid_hexsize); + } else if ((error = read_loose(&raw, &object_path)) == 0) { + *buffer_p = raw.data; + *len_p = raw.len; + *type_p = raw.type; + } + + git_str_dispose(&object_path); + + return error; +} + +static int loose_backend__read_prefix( + git_oid *out_oid, + void **buffer_p, + size_t *len_p, + git_object_t *type_p, + git_odb_backend *_backend, + const git_oid *short_oid, + size_t len) +{ + struct loose_backend *backend = (struct loose_backend *)_backend; + int error = 0; + + GIT_ASSERT_ARG(len >= GIT_OID_MINPREFIXLEN && + len <= backend->oid_hexsize); + + if (len == backend->oid_hexsize) { + /* We can fall back to regular read method */ + error = loose_backend__read(buffer_p, len_p, type_p, _backend, short_oid); + if (!error) + git_oid_cpy(out_oid, short_oid); + } else { + git_str object_path = GIT_STR_INIT; + git_rawobj raw; + + GIT_ASSERT_ARG(backend && short_oid); + + if ((error = locate_object_short_oid(&object_path, out_oid, + (loose_backend *)backend, short_oid, len)) == 0 && + (error = read_loose(&raw, &object_path)) == 0) + { + *buffer_p = raw.data; + *len_p = raw.len; + *type_p = raw.type; + } + + git_str_dispose(&object_path); + } + + return error; +} + +static int loose_backend__exists(git_odb_backend *backend, const git_oid *oid) +{ + git_str object_path = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(backend); + GIT_ASSERT_ARG(oid); + + error = locate_object(&object_path, (loose_backend *)backend, oid); + + git_str_dispose(&object_path); + + return !error; +} + +static int loose_backend__exists_prefix( + git_oid *out, git_odb_backend *backend, const git_oid *short_id, size_t len) +{ + git_str object_path = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(backend); + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(short_id); + GIT_ASSERT_ARG(len >= GIT_OID_MINPREFIXLEN); + + error = locate_object_short_oid( + &object_path, out, (loose_backend *)backend, short_id, len); + + git_str_dispose(&object_path); + + return error; +} + +struct foreach_state { + struct loose_backend *backend; + size_t dir_len; + git_odb_foreach_cb cb; + void *data; +}; + +GIT_INLINE(int) filename_to_oid(struct loose_backend *backend, git_oid *oid, const char *ptr) +{ + int v; + size_t i = 0; + + if (strlen(ptr) != backend->oid_hexsize + 1) + return -1; + + if (ptr[2] != '/') { + return -1; + } + + v = (git__fromhex(ptr[i]) << 4) | git__fromhex(ptr[i+1]); + if (v < 0) + return -1; + + oid->id[0] = (unsigned char) v; + + ptr += 3; + for (i = 0; i < backend->oid_hexsize - 2; i += 2) { + v = (git__fromhex(ptr[i]) << 4) | git__fromhex(ptr[i + 1]); + if (v < 0) + return -1; + + oid->id[1 + i/2] = (unsigned char) v; + } + +#ifdef GIT_EXPERIMENTAL_SHA256 + oid->type = backend->options.oid_type; +#endif + + return 0; +} + +static int foreach_object_dir_cb(void *_state, git_str *path) +{ + git_oid oid; + struct foreach_state *state = (struct foreach_state *) _state; + + if (filename_to_oid(state->backend, &oid, path->ptr + state->dir_len) < 0) + return 0; + + return git_error_set_after_callback_function( + state->cb(&oid, state->data), "git_odb_foreach"); +} + +static int foreach_cb(void *_state, git_str *path) +{ + struct foreach_state *state = (struct foreach_state *) _state; + + /* non-dir is some stray file, ignore it */ + if (!git_fs_path_isdir(git_str_cstr(path))) + return 0; + + return git_fs_path_direach(path, 0, foreach_object_dir_cb, state); +} + +static int loose_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb cb, void *data) +{ + char *objects_dir; + int error; + git_str buf = GIT_STR_INIT; + struct foreach_state state; + loose_backend *backend = (loose_backend *) _backend; + + GIT_ASSERT_ARG(backend); + GIT_ASSERT_ARG(cb); + + objects_dir = backend->objects_dir; + + git_str_sets(&buf, objects_dir); + git_fs_path_to_dir(&buf); + if (git_str_oom(&buf)) + return -1; + + memset(&state, 0, sizeof(state)); + state.backend = backend; + state.cb = cb; + state.data = data; + state.dir_len = git_str_len(&buf); + + error = git_fs_path_direach(&buf, 0, foreach_cb, &state); + + git_str_dispose(&buf); + + return error; +} + +static int loose_backend__writestream_finalize(git_odb_stream *_stream, const git_oid *oid) +{ + loose_writestream *stream = (loose_writestream *)_stream; + loose_backend *backend = (loose_backend *)_stream->backend; + git_str final_path = GIT_STR_INIT; + int error = 0; + + if (object_file_name(&final_path, backend, oid) < 0 || + object_mkdir(&final_path, backend) < 0) + error = -1; + else + error = git_filebuf_commit_at( + &stream->fbuf, final_path.ptr); + + git_str_dispose(&final_path); + + return error; +} + +static int loose_backend__writestream_write(git_odb_stream *_stream, const char *data, size_t len) +{ + loose_writestream *stream = (loose_writestream *)_stream; + return git_filebuf_write(&stream->fbuf, data, len); +} + +static void loose_backend__writestream_free(git_odb_stream *_stream) +{ + loose_writestream *stream = (loose_writestream *)_stream; + + git_filebuf_cleanup(&stream->fbuf); + git__free(stream); +} + +static int filebuf_flags(loose_backend *backend) +{ + int flags = GIT_FILEBUF_TEMPORARY | + (backend->options.compression_level << GIT_FILEBUF_DEFLATE_SHIFT); + + if ((backend->options.flags & GIT_ODB_BACKEND_LOOSE_FSYNC) || + git_repository__fsync_gitdir) + flags |= GIT_FILEBUF_FSYNC; + + return flags; +} + +static int loose_backend__writestream(git_odb_stream **stream_out, git_odb_backend *_backend, git_object_size_t length, git_object_t type) +{ + loose_backend *backend; + loose_writestream *stream = NULL; + char hdr[MAX_HEADER_LEN]; + git_str tmp_path = GIT_STR_INIT; + size_t hdrlen; + int error; + + GIT_ASSERT_ARG(_backend); + + backend = (loose_backend *)_backend; + *stream_out = NULL; + + if ((error = git_odb__format_object_header(&hdrlen, + hdr, sizeof(hdr), length, type)) < 0) + return error; + + stream = git__calloc(1, sizeof(loose_writestream)); + GIT_ERROR_CHECK_ALLOC(stream); + + stream->stream.backend = _backend; + stream->stream.read = NULL; /* read only */ + stream->stream.write = &loose_backend__writestream_write; + stream->stream.finalize_write = &loose_backend__writestream_finalize; + stream->stream.free = &loose_backend__writestream_free; + stream->stream.mode = GIT_STREAM_WRONLY; + + if (git_str_joinpath(&tmp_path, backend->objects_dir, "tmp_object") < 0 || + git_filebuf_open(&stream->fbuf, tmp_path.ptr, filebuf_flags(backend), + backend->options.file_mode) < 0 || + stream->stream.write((git_odb_stream *)stream, hdr, hdrlen) < 0) + { + git_filebuf_cleanup(&stream->fbuf); + git__free(stream); + stream = NULL; + } + git_str_dispose(&tmp_path); + *stream_out = (git_odb_stream *)stream; + + return !stream ? -1 : 0; +} + +static int loose_backend__readstream_read( + git_odb_stream *_stream, + char *buffer, + size_t buffer_len) +{ + loose_readstream *stream = (loose_readstream *)_stream; + size_t start_remain = stream->start_len - stream->start_read; + int total = 0, error; + + buffer_len = min(buffer_len, INT_MAX); + + /* + * if we read more than just the header in the initial read, play + * that back for the caller. + */ + if (start_remain && buffer_len) { + size_t chunk = min(start_remain, buffer_len); + memcpy(buffer, stream->start + stream->start_read, chunk); + + buffer += chunk; + stream->start_read += chunk; + + total += (int)chunk; + buffer_len -= chunk; + } + + if (buffer_len) { + size_t chunk = buffer_len; + + if ((error = git_zstream_get_output(buffer, &chunk, &stream->zstream)) < 0) + return error; + + total += (int)chunk; + } + + return (int)total; +} + +static void loose_backend__readstream_free(git_odb_stream *_stream) +{ + loose_readstream *stream = (loose_readstream *)_stream; + + git_futils_mmap_free(&stream->map); + git_zstream_free(&stream->zstream); + git__free(stream); +} + +static int loose_backend__readstream_packlike( + obj_hdr *hdr, + loose_readstream *stream) +{ + const unsigned char *data; + size_t data_len, head_len; + int error; + + data = stream->map.data; + data_len = stream->map.len; + + /* + * read the object header, which is an (uncompressed) + * binary encoding of the object type and size. + */ + if ((error = parse_header_packlike(hdr, &head_len, data, data_len)) < 0) + return error; + + if (!git_object_type_is_valid(hdr->type)) { + git_error_set(GIT_ERROR_ODB, "failed to inflate loose object"); + return -1; + } + + return git_zstream_set_input(&stream->zstream, + data + head_len, data_len - head_len); +} + +static int loose_backend__readstream_standard( + obj_hdr *hdr, + loose_readstream *stream) +{ + unsigned char head[MAX_HEADER_LEN]; + size_t init, head_len; + int error; + + if ((error = git_zstream_set_input(&stream->zstream, + stream->map.data, stream->map.len)) < 0) + return error; + + init = sizeof(head); + + /* + * inflate the initial part of the compressed buffer in order to + * parse the header; read the largest header possible, then store + * it in the `start` field of the stream object. + */ + if ((error = git_zstream_get_output(head, &init, &stream->zstream)) < 0 || + (error = parse_header(hdr, &head_len, head, init)) < 0) + return error; + + if (!git_object_type_is_valid(hdr->type)) { + git_error_set(GIT_ERROR_ODB, "failed to inflate disk object"); + return -1; + } + + if (init > head_len) { + stream->start_len = init - head_len; + memcpy(stream->start, head + head_len, init - head_len); + } + + return 0; +} + +static int loose_backend__readstream( + git_odb_stream **stream_out, + size_t *len_out, + git_object_t *type_out, + git_odb_backend *_backend, + const git_oid *oid) +{ + loose_backend *backend; + loose_readstream *stream = NULL; + git_hash_ctx *hash_ctx = NULL; + git_str object_path = GIT_STR_INIT; + git_hash_algorithm_t algorithm; + obj_hdr hdr; + int error = 0; + + GIT_ASSERT_ARG(stream_out); + GIT_ASSERT_ARG(len_out); + GIT_ASSERT_ARG(type_out); + GIT_ASSERT_ARG(_backend); + GIT_ASSERT_ARG(oid); + + backend = (loose_backend *)_backend; + *stream_out = NULL; + *len_out = 0; + *type_out = GIT_OBJECT_INVALID; + + if (locate_object(&object_path, backend, oid) < 0) { + error = git_odb__error_notfound("no matching loose object", + oid, backend->oid_hexsize); + goto done; + } + + stream = git__calloc(1, sizeof(loose_readstream)); + GIT_ERROR_CHECK_ALLOC(stream); + + hash_ctx = git__malloc(sizeof(git_hash_ctx)); + GIT_ERROR_CHECK_ALLOC(hash_ctx); + + algorithm = git_oid_algorithm(backend->options.oid_type); + + if ((error = git_hash_ctx_init(hash_ctx, algorithm)) < 0 || + (error = git_futils_mmap_ro_file(&stream->map, object_path.ptr)) < 0 || + (error = git_zstream_init(&stream->zstream, GIT_ZSTREAM_INFLATE)) < 0) + goto done; + + /* check for a packlike loose object */ + if (!is_zlib_compressed_data(stream->map.data, stream->map.len)) + error = loose_backend__readstream_packlike(&hdr, stream); + else + error = loose_backend__readstream_standard(&hdr, stream); + + if (error < 0) + goto done; + + stream->stream.backend = _backend; + stream->stream.hash_ctx = hash_ctx; + stream->stream.read = &loose_backend__readstream_read; + stream->stream.free = &loose_backend__readstream_free; + + *stream_out = (git_odb_stream *)stream; + *len_out = hdr.size; + *type_out = hdr.type; + +done: + if (error < 0) { + if (stream) { + git_futils_mmap_free(&stream->map); + git_zstream_free(&stream->zstream); + git__free(stream); + } + if (hash_ctx) { + git_hash_ctx_cleanup(hash_ctx); + git__free(hash_ctx); + } + } + + git_str_dispose(&object_path); + return error; +} + +static int loose_backend__write(git_odb_backend *_backend, const git_oid *oid, const void *data, size_t len, git_object_t type) +{ + int error = 0; + git_str final_path = GIT_STR_INIT; + char header[MAX_HEADER_LEN]; + size_t header_len; + git_filebuf fbuf = GIT_FILEBUF_INIT; + loose_backend *backend; + + backend = (loose_backend *)_backend; + + /* prepare the header for the file */ + if ((error = git_odb__format_object_header(&header_len, + header, sizeof(header), len, type)) < 0) + goto cleanup; + + if (git_str_joinpath(&final_path, backend->objects_dir, "tmp_object") < 0 || + git_filebuf_open(&fbuf, final_path.ptr, filebuf_flags(backend), + backend->options.file_mode) < 0) + { + error = -1; + goto cleanup; + } + + git_filebuf_write(&fbuf, header, header_len); + git_filebuf_write(&fbuf, data, len); + + if (object_file_name(&final_path, backend, oid) < 0 || + object_mkdir(&final_path, backend) < 0 || + git_filebuf_commit_at(&fbuf, final_path.ptr) < 0) + error = -1; + +cleanup: + if (error < 0) + git_filebuf_cleanup(&fbuf); + git_str_dispose(&final_path); + return error; +} + +static int loose_backend__freshen( + git_odb_backend *_backend, + const git_oid *oid) +{ + loose_backend *backend = (loose_backend *)_backend; + git_str path = GIT_STR_INIT; + int error; + + if (object_file_name(&path, backend, oid) < 0) + return -1; + + error = git_futils_touch(path.ptr, NULL); + git_str_dispose(&path); + + return error; +} + +static void loose_backend__free(git_odb_backend *_backend) +{ + git__free(_backend); +} + +static void normalize_options( + git_odb_backend_loose_options *opts, + const git_odb_backend_loose_options *given_opts) +{ + git_odb_backend_loose_options init = GIT_ODB_BACKEND_LOOSE_OPTIONS_INIT; + + if (given_opts) + memcpy(opts, given_opts, sizeof(git_odb_backend_loose_options)); + else + memcpy(opts, &init, sizeof(git_odb_backend_loose_options)); + + if (opts->compression_level < 0) + opts->compression_level = Z_BEST_SPEED; + + if (opts->dir_mode == 0) + opts->dir_mode = GIT_OBJECT_DIR_MODE; + + if (opts->file_mode == 0) + opts->file_mode = GIT_OBJECT_FILE_MODE; + + if (opts->oid_type == 0) + opts->oid_type = GIT_OID_DEFAULT; +} + +int git_odb__backend_loose( + git_odb_backend **backend_out, + const char *objects_dir, + git_odb_backend_loose_options *opts) +{ + loose_backend *backend; + size_t objects_dirlen, alloclen; + + GIT_ASSERT_ARG(backend_out); + GIT_ASSERT_ARG(objects_dir); + + objects_dirlen = strlen(objects_dir); + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(loose_backend), objects_dirlen); + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 2); + backend = git__calloc(1, alloclen); + GIT_ERROR_CHECK_ALLOC(backend); + + backend->parent.version = GIT_ODB_BACKEND_VERSION; + backend->objects_dirlen = objects_dirlen; + memcpy(backend->objects_dir, objects_dir, objects_dirlen); + + if (backend->objects_dir[backend->objects_dirlen - 1] != '/') + backend->objects_dir[backend->objects_dirlen++] = '/'; + + normalize_options(&backend->options, opts); + backend->oid_hexsize = git_oid_hexsize(backend->options.oid_type); + + backend->parent.read = &loose_backend__read; + backend->parent.write = &loose_backend__write; + backend->parent.read_prefix = &loose_backend__read_prefix; + backend->parent.read_header = &loose_backend__read_header; + backend->parent.writestream = &loose_backend__writestream; + backend->parent.readstream = &loose_backend__readstream; + backend->parent.exists = &loose_backend__exists; + backend->parent.exists_prefix = &loose_backend__exists_prefix; + backend->parent.foreach = &loose_backend__foreach; + backend->parent.freshen = &loose_backend__freshen; + backend->parent.free = &loose_backend__free; + + *backend_out = (git_odb_backend *)backend; + return 0; +} + + +#ifdef GIT_EXPERIMENTAL_SHA256 +int git_odb_backend_loose( + git_odb_backend **backend_out, + const char *objects_dir, + git_odb_backend_loose_options *opts) +{ + return git_odb__backend_loose(backend_out, objects_dir, opts); +} +#else +int git_odb_backend_loose( + git_odb_backend **backend_out, + const char *objects_dir, + int compression_level, + int do_fsync, + unsigned int dir_mode, + unsigned int file_mode) +{ + git_odb_backend_loose_flag_t flags = 0; + git_odb_backend_loose_options opts = GIT_ODB_BACKEND_LOOSE_OPTIONS_INIT; + + if (do_fsync) + flags |= GIT_ODB_BACKEND_LOOSE_FSYNC; + + opts.flags = flags; + opts.compression_level = compression_level; + opts.dir_mode = dir_mode; + opts.file_mode = file_mode; + opts.oid_type = GIT_OID_DEFAULT; + + return git_odb__backend_loose(backend_out, objects_dir, &opts); +} +#endif diff --git a/src/libgit2/odb_mempack.c b/src/libgit2/odb_mempack.c new file mode 100644 index 00000000000..2f3fba9ad98 --- /dev/null +++ b/src/libgit2/odb_mempack.c @@ -0,0 +1,221 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "buf.h" +#include "futils.h" +#include "hash.h" +#include "odb.h" +#include "array.h" +#include "pack-objects.h" + +#include "git2/odb_backend.h" +#include "git2/object.h" +#include "git2/types.h" +#include "git2/pack.h" +#include "git2/sys/odb_backend.h" +#include "git2/sys/mempack.h" + +struct memobject { + git_oid oid; + size_t len; + git_object_t type; + char data[GIT_FLEX_ARRAY]; +}; + +GIT_HASHMAP_OID_SETUP(git_odb_mempack_oidmap, struct memobject *); + +struct memory_packer_db { + git_odb_backend parent; + git_odb_mempack_oidmap objects; + git_array_t(struct memobject *) commits; +}; + +static int impl__write(git_odb_backend *_backend, const git_oid *oid, const void *data, size_t len, git_object_t type) +{ + struct memory_packer_db *db = (struct memory_packer_db *)_backend; + struct memobject *obj = NULL; + size_t alloc_len; + + if (git_odb_mempack_oidmap_contains(&db->objects, oid)) + return 0; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, sizeof(struct memobject), len); + obj = git__malloc(alloc_len); + GIT_ERROR_CHECK_ALLOC(obj); + + memcpy(obj->data, data, len); + git_oid_cpy(&obj->oid, oid); + obj->len = len; + obj->type = type; + + if (git_odb_mempack_oidmap_put(&db->objects, &obj->oid, obj) < 0) + return -1; + + if (type == GIT_OBJECT_COMMIT) { + struct memobject **store = git_array_alloc(db->commits); + GIT_ERROR_CHECK_ALLOC(store); + *store = obj; + } + + return 0; +} + +static int impl__exists(git_odb_backend *backend, const git_oid *oid) +{ + struct memory_packer_db *db = (struct memory_packer_db *)backend; + + return git_odb_mempack_oidmap_contains(&db->objects, oid); +} + +static int impl__read(void **buffer_p, size_t *len_p, git_object_t *type_p, git_odb_backend *backend, const git_oid *oid) +{ + struct memory_packer_db *db = (struct memory_packer_db *)backend; + struct memobject *obj; + int error; + + if ((error = git_odb_mempack_oidmap_get(&obj, &db->objects, oid)) != 0) + return error; + + *len_p = obj->len; + *type_p = obj->type; + *buffer_p = git__malloc(obj->len); + GIT_ERROR_CHECK_ALLOC(*buffer_p); + + memcpy(*buffer_p, obj->data, obj->len); + return 0; +} + +static int impl__read_header(size_t *len_p, git_object_t *type_p, git_odb_backend *backend, const git_oid *oid) +{ + struct memory_packer_db *db = (struct memory_packer_db *)backend; + struct memobject *obj; + int error; + + if ((error = git_odb_mempack_oidmap_get(&obj, &db->objects, oid)) != 0) + return error; + + *len_p = obj->len; + *type_p = obj->type; + return 0; +} + +static int git_mempack__dump( + git_str *pack, + git_repository *repo, + git_odb_backend *_backend) +{ + struct memory_packer_db *db = (struct memory_packer_db *)_backend; + git_packbuilder *packbuilder; + uint32_t i; + int err = -1; + + if (git_packbuilder_new(&packbuilder, repo) < 0) + return -1; + + git_packbuilder_set_threads(packbuilder, 0); + + for (i = 0; i < db->commits.size; ++i) { + struct memobject *commit = db->commits.ptr[i]; + + err = git_packbuilder_insert_commit(packbuilder, &commit->oid); + if (err < 0) + goto cleanup; + } + + err = git_packbuilder__write_buf(pack, packbuilder); + +cleanup: + git_packbuilder_free(packbuilder); + return err; +} + +int git_mempack_write_thin_pack(git_odb_backend *backend, git_packbuilder *pb) +{ + struct memory_packer_db *db = (struct memory_packer_db *)backend; + const git_oid *oid; + git_hashmap_iter_t iter = GIT_HASHMAP_INIT; + int err; + + while (true) { + err = git_odb_mempack_oidmap_iterate(&iter, &oid, NULL, &db->objects); + + if (err == GIT_ITEROVER) + break; + else if (err != 0) + return err; + + err = git_packbuilder_insert(pb, oid, NULL); + if (err != 0) + return err; + } + + return 0; +} + +int git_mempack_dump( + git_buf *pack, + git_repository *repo, + git_odb_backend *_backend) +{ + GIT_BUF_WRAP_PRIVATE(pack, git_mempack__dump, repo, _backend); +} + +int git_mempack_reset(git_odb_backend *_backend) +{ + struct memory_packer_db *db = (struct memory_packer_db *)_backend; + struct memobject *object = NULL; + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + + while (git_odb_mempack_oidmap_iterate(&iter, NULL, &object, &db->objects) == 0) + git__free(object); + + git_array_clear(db->commits); + git_odb_mempack_oidmap_clear(&db->objects); + + return 0; +} + +static void impl__free(git_odb_backend *_backend) +{ + struct memory_packer_db *db = (struct memory_packer_db *)_backend; + + git_mempack_reset(_backend); + git_odb_mempack_oidmap_dispose(&db->objects); + git__free(db); +} + +int git_mempack_new(git_odb_backend **out) +{ + struct memory_packer_db *db; + + GIT_ASSERT_ARG(out); + + db = git__calloc(1, sizeof(struct memory_packer_db)); + GIT_ERROR_CHECK_ALLOC(db); + + db->parent.version = GIT_ODB_BACKEND_VERSION; + db->parent.read = &impl__read; + db->parent.write = &impl__write; + db->parent.read_header = &impl__read_header; + db->parent.exists = &impl__exists; + db->parent.free = &impl__free; + + *out = (git_odb_backend *)db; + return 0; +} + +int git_mempack_object_count(size_t *out, git_odb_backend *_backend) +{ + struct memory_packer_db *db = (struct memory_packer_db *)_backend; + + GIT_ASSERT_ARG(_backend); + + *out = (size_t)git_odb_mempack_oidmap_size(&db->objects); + return 0; +} diff --git a/src/libgit2/odb_pack.c b/src/libgit2/odb_pack.c new file mode 100644 index 00000000000..ef8d35309fe --- /dev/null +++ b/src/libgit2/odb_pack.c @@ -0,0 +1,994 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include +#include "git2/repository.h" +#include "git2/indexer.h" +#include "git2/sys/odb_backend.h" +#include "delta.h" +#include "futils.h" +#include "hash.h" +#include "midx.h" +#include "mwindow.h" +#include "odb.h" +#include "pack.h" + +#include "git2/odb_backend.h" + +/* re-freshen pack files no more than every 2 seconds */ +#define FRESHEN_FREQUENCY 2 + +struct pack_backend { + git_odb_backend parent; + git_odb_backend_pack_options opts; + git_midx_file *midx; + git_vector midx_packs; + git_vector packs; + struct git_pack_file *last_found; + char *pack_folder; +}; + +struct pack_writepack { + struct git_odb_writepack parent; + git_indexer *indexer; +}; + +/** + * The wonderful tale of a Packed Object lookup query + * =================================================== + * A riveting and epic story of epicness and ASCII + * art, presented by yours truly, + * Sir Vicent of Marti + * + * + * Chapter 1: Once upon a time... + * Initialization of the Pack Backend + * -------------------------------------------------- + * + * # git_odb_backend_pack + * | Creates the pack backend structure, initializes the + * | callback pointers to our default read() and exist() methods, + * | and tries to find the `pack` folder, if it exists. ODBs without a `pack` + * | folder are ignored altogether. If there is a `pack` folder, it tries to + * | preload all the known packfiles in the ODB. + * | + * |-# pack_backend__refresh + * | The `multi-pack-index` is loaded if it exists and is valid. + * | Then we run a `dirent` callback through every file in the pack folder, + * | even those present in `multi-pack-index`. The unindexed packfiles are + * | then sorted according to a sorting callback. + * | + * |-# refresh_multi_pack_index + * | Detect the presence of the `multi-pack-index` file. If it needs to be + * | refreshed, frees the old copy and tries to load the new one, together + * | with all the packfiles it indexes. If the process fails, fall back to + * | the old behavior, as if the `multi-pack-index` file was not there. + * | + * |-# packfile_load__cb + * | | This callback is called from `dirent` with every single file + * | | inside the pack folder. We find the packs by actually locating + * | | their index (ends in ".idx"). From that index, we verify that + * | | the corresponding packfile exists and is valid, and if so, we + * | | add it to the pack list. + * | | + * | # git_mwindow_get_pack + * | Make sure that there's a packfile to back this index, and store + * | some very basic information regarding the packfile itself, + * | such as the full path, the size, and the modification time. + * | We don't actually open the packfile to check for internal consistency. + * | + * |-# packfile_sort__cb + * Sort all the preloaded packs according to some specific criteria: + * we prioritize the "newer" packs because it's more likely they + * contain the objects we are looking for, and we prioritize local + * packs over remote ones. + * + * + * + * Chapter 2: To be, or not to be... + * A standard packed `exist` query for an OID + * -------------------------------------------------- + * + * # pack_backend__exists / pack_backend__exists_prefix + * | Check if the given oid (or an oid prefix) exists in any of the + * | packs that have been loaded for our ODB. + * | + * |-# pack_entry_find / pack_entry_find_prefix + * | If there is a multi-pack-index present, search the oid in that + * | index first. If it is not found there, iterate through all the unindexed + * | packs that have been preloaded (starting by the pack where the latest + * | object was found) to try to find the OID in one of them. + * | + * |-# git_midx_entry_find + * | Search for the oid in the multi-pack-index. See + * | + * | for specifics on the multi-pack-index format and how do we find + * | entries in it. + * | + * |-# git_pack_entry_find + * | Check the index of an individual unindexed pack to see if the + * | OID can be found. If we can find the offset to that inside of the + * | index, that means the object is contained inside of the packfile and + * | we can stop searching. Before returning, we verify that the + * | packfile behind the index we are searching still exists on disk. + * | + * |-# pack_entry_find_offset + * | Mmap the actual index file to disk if it hasn't been opened + * | yet, and run a binary search through it to find the OID. + * | See + * | for specifics on the Packfile Index format and how do we find + * | entries in it. + * | + * |-# pack_index_open + * | Guess the name of the index based on the full path to the + * | packfile, open it and verify its contents. Only if the index + * | has not been opened already. + * | + * |-# pack_index_check + * Mmap the index file and do a quick run through the header + * to guess the index version (right now we support v1 and v2), + * and to verify that the size of the index makes sense. + * + * + * + * Chapter 3: The neverending story... + * A standard packed `lookup` query for an OID + * -------------------------------------------------- + * + * # pack_backend__read / pack_backend__read_prefix + * | Check if the given oid (or an oid prefix) exists in any of the + * | packs that have been loaded for our ODB. If it does, open the packfile and + * | read from it. + * | + * |-# git_packfile_unpack + * Armed with a packfile and the offset within it, we can finally unpack + * the object pointed at by the oid. This involves mmapping part of + * the `.pack` file, and uncompressing the object within it (if it is + * stored in the undelfitied representation), or finding a base object and + * applying some deltas to its uncompressed representation (if it is stored + * in the deltified representation). See + * + * for specifics on the Packfile format and how do we read from it. + * + */ + + +/*********************************************************** + * + * FORWARD DECLARATIONS + * + ***********************************************************/ + +static int packfile_sort__cb(const void *a_, const void *b_); + +static int packfile_load__cb(void *_data, git_str *path); + +static int packfile_byname_search_cmp(const void *path, const void *pack_entry); + +static int pack_entry_find(struct git_pack_entry *e, + struct pack_backend *backend, const git_oid *oid); + +/* Can find the offset of an object given + * a prefix of an identifier. + * Sets GIT_EAMBIGUOUS if short oid is ambiguous. + * This method assumes that len is between + * GIT_OID_MINPREFIXLEN and the hexsize for the hash type. + */ +static int pack_entry_find_prefix( + struct git_pack_entry *e, + struct pack_backend *backend, + const git_oid *short_oid, + size_t len); + + + +/*********************************************************** + * + * PACK WINDOW MANAGEMENT + * + ***********************************************************/ + +static int packfile_byname_search_cmp(const void *path_, const void *p_) +{ + const git_str *path = (const git_str *)path_; + const struct git_pack_file *p = (const struct git_pack_file *)p_; + + return strncmp(p->pack_name, git_str_cstr(path), git_str_len(path)); +} + +static int packfile_sort__cb(const void *a_, const void *b_) +{ + const struct git_pack_file *a = a_; + const struct git_pack_file *b = b_; + int st; + + /* + * Local packs tend to contain objects specific to our + * variant of the project than remote ones. In addition, + * remote ones could be on a network mounted filesystem. + * Favor local ones for these reasons. + */ + st = a->pack_local - b->pack_local; + if (st) + return -st; + + /* + * Younger packs tend to contain more recent objects, + * and more recent objects tend to get accessed more + * often. + */ + if (a->mtime < b->mtime) + return 1; + else if (a->mtime == b->mtime) + return 0; + + return -1; +} + + +static int packfile_load__cb(void *data, git_str *path) +{ + struct pack_backend *backend = data; + struct git_pack_file *pack; + const char *path_str = git_str_cstr(path); + git_str index_prefix = GIT_STR_INIT; + size_t cmp_len = git_str_len(path); + int error; + + if (cmp_len <= strlen(".idx") || git__suffixcmp(path_str, ".idx") != 0) + return 0; /* not an index */ + + cmp_len -= strlen(".idx"); + git_str_attach_notowned(&index_prefix, path_str, cmp_len); + + if (git_vector_search2(NULL, &backend->midx_packs, packfile_byname_search_cmp, &index_prefix) == 0) + return 0; + if (git_vector_search2(NULL, &backend->packs, packfile_byname_search_cmp, &index_prefix) == 0) + return 0; + + error = git_mwindow_get_pack(&pack, path->ptr, backend->opts.oid_type); + + /* ignore missing .pack file as git does */ + if (error == GIT_ENOTFOUND) { + git_error_clear(); + return 0; + } + + if (!error) + error = git_vector_insert(&backend->packs, pack); + + return error; + +} + +static int pack_entry_find(struct git_pack_entry *e, struct pack_backend *backend, const git_oid *oid) +{ + struct git_pack_file *last_found = backend->last_found, *p; + git_midx_entry midx_entry; + size_t oid_hexsize = git_oid_hexsize(backend->opts.oid_type); + size_t i; + + if (backend->midx && + git_midx_entry_find(&midx_entry, backend->midx, oid, oid_hexsize) == 0 && + midx_entry.pack_index < git_vector_length(&backend->midx_packs)) { + e->offset = midx_entry.offset; + git_oid_cpy(&e->id, &midx_entry.sha1); + e->p = git_vector_get(&backend->midx_packs, midx_entry.pack_index); + return 0; + } + + if (last_found && + git_pack_entry_find(e, last_found, oid, oid_hexsize) == 0) + return 0; + + git_vector_foreach(&backend->packs, i, p) { + if (p == last_found) + continue; + + if (git_pack_entry_find(e, p, oid, oid_hexsize) == 0) { + backend->last_found = p; + return 0; + } + } + + return git_odb__error_notfound( + "failed to find pack entry", oid, oid_hexsize); +} + +static int pack_entry_find_prefix( + struct git_pack_entry *e, + struct pack_backend *backend, + const git_oid *short_oid, + size_t len) +{ + int error; + size_t i; + git_oid found_full_oid; + bool found = false; + struct git_pack_file *last_found = backend->last_found, *p; + git_midx_entry midx_entry; + +#ifdef GIT_EXPERIMENTAL_SHA256 + git_oid_clear(&found_full_oid, short_oid->type); +#else + git_oid_clear(&found_full_oid, GIT_OID_SHA1); +#endif + + if (backend->midx) { + error = git_midx_entry_find(&midx_entry, backend->midx, short_oid, len); + if (error == GIT_EAMBIGUOUS) + return error; + if (!error && midx_entry.pack_index < git_vector_length(&backend->midx_packs)) { + e->offset = midx_entry.offset; + git_oid_cpy(&e->id, &midx_entry.sha1); + e->p = git_vector_get(&backend->midx_packs, midx_entry.pack_index); + git_oid_cpy(&found_full_oid, &e->id); + found = true; + } + } + + if (last_found) { + error = git_pack_entry_find(e, last_found, short_oid, len); + if (error == GIT_EAMBIGUOUS) + return error; + if (!error) { + if (found && git_oid_cmp(&e->id, &found_full_oid)) + return git_odb__error_ambiguous("found multiple pack entries"); + git_oid_cpy(&found_full_oid, &e->id); + found = true; + } + } + + git_vector_foreach(&backend->packs, i, p) { + if (p == last_found) + continue; + + error = git_pack_entry_find(e, p, short_oid, len); + if (error == GIT_EAMBIGUOUS) + return error; + if (!error) { + if (found && git_oid_cmp(&e->id, &found_full_oid)) + return git_odb__error_ambiguous("found multiple pack entries"); + git_oid_cpy(&found_full_oid, &e->id); + found = true; + backend->last_found = p; + } + } + + if (!found) + return git_odb__error_notfound("no matching pack entry for prefix", + short_oid, len); + else + return 0; +} + +/*********************************************************** + * + * MULTI-PACK-INDEX SUPPORT + * + * Functions needed to support the multi-pack-index. + * + ***********************************************************/ + +/* + * Remove the multi-pack-index, and move all midx_packs to packs. + */ +static int remove_multi_pack_index(struct pack_backend *backend) +{ + size_t i, j = git_vector_length(&backend->packs); + struct pack_backend *p; + int error = git_vector_size_hint( + &backend->packs, + j + git_vector_length(&backend->midx_packs)); + if (error < 0) + return error; + + git_vector_foreach(&backend->midx_packs, i, p) + git_vector_set(NULL, &backend->packs, j++, p); + git_vector_clear(&backend->midx_packs); + + git_midx_free(backend->midx); + backend->midx = NULL; + + return 0; +} + +/* + * Loads a single .pack file referred to by the multi-pack-index. These must + * match the order in which they are declared in the multi-pack-index file, + * since these files are referred to by their index. + */ +static int process_multi_pack_index_pack( + struct pack_backend *backend, + size_t i, + const char *packfile_name) +{ + int error; + struct git_pack_file *pack; + size_t found_position; + git_str pack_path = GIT_STR_INIT, index_prefix = GIT_STR_INIT; + + error = git_str_joinpath(&pack_path, backend->pack_folder, packfile_name); + if (error < 0) + return error; + + /* This is ensured by midx_parse_packfile_name() */ + if (git_str_len(&pack_path) <= strlen(".idx") || git__suffixcmp(git_str_cstr(&pack_path), ".idx") != 0) + return git_odb__error_notfound("midx file contained a non-index", NULL, 0); + + git_str_attach_notowned(&index_prefix, git_str_cstr(&pack_path), git_str_len(&pack_path) - strlen(".idx")); + + if (git_vector_search2(&found_position, &backend->packs, packfile_byname_search_cmp, &index_prefix) == 0) { + /* Pack was found in the packs list. Moving it to the midx_packs list. */ + git_str_dispose(&pack_path); + git_vector_set(NULL, &backend->midx_packs, i, git_vector_get(&backend->packs, found_position)); + git_vector_remove(&backend->packs, found_position); + return 0; + } + + /* Pack was not found. Allocate a new one. */ + error = git_mwindow_get_pack( + &pack, + git_str_cstr(&pack_path), + backend->opts.oid_type); + git_str_dispose(&pack_path); + if (error < 0) + return error; + + git_vector_set(NULL, &backend->midx_packs, i, pack); + return 0; +} + +/* + * Reads the multi-pack-index. If this fails for whatever reason, the + * multi-pack-index object is freed, and all the packfiles that are related to + * it are moved to the unindexed packfiles vector. + */ +static int refresh_multi_pack_index(struct pack_backend *backend) +{ + int error; + git_str midx_path = GIT_STR_INIT; + const char *packfile_name; + size_t i; + + error = git_str_joinpath(&midx_path, backend->pack_folder, "multi-pack-index"); + if (error < 0) + return error; + + /* + * Check whether the multi-pack-index has changed. If it has, close any + * old multi-pack-index and move all the packfiles to the unindexed + * packs. This is done to prevent losing any open packfiles in case + * refreshing the new multi-pack-index fails, or the file is deleted. + */ + if (backend->midx) { + if (!git_midx_needs_refresh(backend->midx, git_str_cstr(&midx_path))) { + git_str_dispose(&midx_path); + return 0; + } + error = remove_multi_pack_index(backend); + if (error < 0) { + git_str_dispose(&midx_path); + return error; + } + } + + error = git_midx_open(&backend->midx, git_str_cstr(&midx_path), + backend->opts.oid_type); + + git_str_dispose(&midx_path); + if (error < 0) + return error; + + git_vector_resize_to(&backend->midx_packs, git_vector_length(&backend->midx->packfile_names)); + + git_vector_foreach(&backend->midx->packfile_names, i, packfile_name) { + error = process_multi_pack_index_pack(backend, i, packfile_name); + if (error < 0) { + /* + * Something failed during reading multi-pack-index. + * Restore the state of backend as if the + * multi-pack-index was never there, and move all + * packfiles that have been processed so far to the + * unindexed packs. + */ + git_vector_resize_to(&backend->midx_packs, i); + remove_multi_pack_index(backend); + return error; + } + } + + return 0; +} + +/*********************************************************** + * + * PACKED BACKEND PUBLIC API + * + * Implement the git_odb_backend API calls + * + ***********************************************************/ +static int pack_backend__refresh(git_odb_backend *backend_) +{ + int error; + struct stat st; + git_str path = GIT_STR_INIT; + struct pack_backend *backend = (struct pack_backend *)backend_; + + if (backend->pack_folder == NULL) + return 0; + + if (p_stat(backend->pack_folder, &st) < 0 || !S_ISDIR(st.st_mode)) + return git_odb__error_notfound("failed to refresh packfiles", NULL, 0); + + if (refresh_multi_pack_index(backend) < 0) { + /* + * It is okay if this fails. We will just not use the + * multi-pack-index in this case. + */ + git_error_clear(); + } + + /* reload all packs */ + git_str_sets(&path, backend->pack_folder); + error = git_fs_path_direach(&path, 0, packfile_load__cb, backend); + + git_str_dispose(&path); + git_vector_sort(&backend->packs); + + return error; +} + +static int pack_backend__read_header( + size_t *len_p, git_object_t *type_p, + struct git_odb_backend *backend, const git_oid *oid) +{ + struct git_pack_entry e; + int error; + + GIT_ASSERT_ARG(len_p); + GIT_ASSERT_ARG(type_p); + GIT_ASSERT_ARG(backend); + GIT_ASSERT_ARG(oid); + + if ((error = pack_entry_find(&e, (struct pack_backend *)backend, oid)) < 0) + return error; + + return git_packfile_resolve_header(len_p, type_p, e.p, e.offset); +} + +static int pack_backend__freshen( + git_odb_backend *backend, const git_oid *oid) +{ + struct git_pack_entry e; + time_t now; + int error; + + if ((error = pack_entry_find(&e, (struct pack_backend *)backend, oid)) < 0) + return error; + + now = time(NULL); + + if (e.p->last_freshen > now - FRESHEN_FREQUENCY) + return 0; + + if ((error = git_futils_touch(e.p->pack_name, &now)) < 0) + return error; + + e.p->last_freshen = now; + return 0; +} + +static int pack_backend__read( + void **buffer_p, size_t *len_p, git_object_t *type_p, + git_odb_backend *backend, const git_oid *oid) +{ + struct git_pack_entry e; + git_rawobj raw = {NULL}; + int error; + + if ((error = pack_entry_find(&e, (struct pack_backend *)backend, oid)) < 0 || + (error = git_packfile_unpack(&raw, e.p, &e.offset)) < 0) + return error; + + *buffer_p = raw.data; + *len_p = raw.len; + *type_p = raw.type; + + return 0; +} + +static int pack_backend__read_prefix( + git_oid *out_oid, + void **buffer_p, + size_t *len_p, + git_object_t *type_p, + git_odb_backend *_backend, + const git_oid *short_oid, + size_t len) +{ + struct pack_backend *backend = (struct pack_backend *)_backend; + int error = 0; + + if (len < GIT_OID_MINPREFIXLEN) + error = git_odb__error_ambiguous("prefix length too short"); + + else if (len >= git_oid_hexsize(backend->opts.oid_type)) { + /* We can fall back to regular read method */ + error = pack_backend__read(buffer_p, len_p, type_p, _backend, short_oid); + if (!error) + git_oid_cpy(out_oid, short_oid); + } else { + struct git_pack_entry e; + git_rawobj raw = {NULL}; + + if ((error = pack_entry_find_prefix(&e, + backend, short_oid, len)) == 0 && + (error = git_packfile_unpack(&raw, e.p, &e.offset)) == 0) + { + *buffer_p = raw.data; + *len_p = raw.len; + *type_p = raw.type; + git_oid_cpy(out_oid, &e.id); + } + } + + return error; +} + +static int pack_backend__exists(git_odb_backend *backend, const git_oid *oid) +{ + struct git_pack_entry e; + return pack_entry_find(&e, (struct pack_backend *)backend, oid) == 0; +} + +static int pack_backend__exists_prefix( + git_oid *out, git_odb_backend *backend, const git_oid *short_id, size_t len) +{ + int error; + struct pack_backend *pb = (struct pack_backend *)backend; + struct git_pack_entry e = {0}; + + error = pack_entry_find_prefix(&e, pb, short_id, len); + git_oid_cpy(out, &e.id); + return error; +} + +static int pack_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb cb, void *data) +{ + int error; + struct git_pack_file *p; + struct pack_backend *backend; + unsigned int i; + + GIT_ASSERT_ARG(_backend); + GIT_ASSERT_ARG(cb); + + backend = (struct pack_backend *)_backend; + + /* Make sure we know about the packfiles */ + if ((error = pack_backend__refresh(_backend)) != 0) + return error; + + if (backend->midx && (error = git_midx_foreach_entry(backend->midx, cb, data)) != 0) + return error; + git_vector_foreach(&backend->packs, i, p) { + if ((error = git_pack_foreach_entry(p, cb, data)) != 0) + return error; + } + + return 0; +} + +static int pack_backend__writepack_append(struct git_odb_writepack *_writepack, const void *data, size_t size, git_indexer_progress *stats) +{ + struct pack_writepack *writepack = (struct pack_writepack *)_writepack; + + GIT_ASSERT_ARG(writepack); + + return git_indexer_append(writepack->indexer, data, size, stats); +} + +static int pack_backend__writepack_commit(struct git_odb_writepack *_writepack, git_indexer_progress *stats) +{ + struct pack_writepack *writepack = (struct pack_writepack *)_writepack; + + GIT_ASSERT_ARG(writepack); + + return git_indexer_commit(writepack->indexer, stats); +} + +static void pack_backend__writepack_free(struct git_odb_writepack *_writepack) +{ + struct pack_writepack *writepack; + + if (!_writepack) + return; + + writepack = (struct pack_writepack *)_writepack; + + git_indexer_free(writepack->indexer); + git__free(writepack); +} + +static int pack_backend__writepack(struct git_odb_writepack **out, + git_odb_backend *_backend, + git_odb *odb, + git_indexer_progress_cb progress_cb, + void *progress_payload) +{ + git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT; + struct pack_backend *backend; + struct pack_writepack *writepack; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(_backend); + + *out = NULL; + + opts.progress_cb = progress_cb; + opts.progress_cb_payload = progress_payload; + + backend = (struct pack_backend *)_backend; + + writepack = git__calloc(1, sizeof(struct pack_writepack)); + GIT_ERROR_CHECK_ALLOC(writepack); + +#ifdef GIT_EXPERIMENTAL_SHA256 + opts.odb = odb; + opts.oid_type = backend->opts.oid_type; + + error = git_indexer_new(&writepack->indexer, + backend->pack_folder, + &opts); +#else + error = git_indexer_new(&writepack->indexer, + backend->pack_folder, 0, odb, &opts); +#endif + + if (error < 0) + return -1; + + writepack->parent.backend = _backend; + writepack->parent.append = pack_backend__writepack_append; + writepack->parent.commit = pack_backend__writepack_commit; + writepack->parent.free = pack_backend__writepack_free; + + *out = (git_odb_writepack *)writepack; + + return 0; +} + +static int get_idx_path( + git_str *idx_path, + struct pack_backend *backend, + struct git_pack_file *p) +{ + size_t path_len; + int error; + + error = git_fs_path_prettify(idx_path, p->pack_name, backend->pack_folder); + if (error < 0) + return error; + path_len = git_str_len(idx_path); + if (path_len <= strlen(".pack") || git__suffixcmp(git_str_cstr(idx_path), ".pack") != 0) + return git_odb__error_notfound("packfile does not end in .pack", NULL, 0); + path_len -= strlen(".pack"); + error = git_str_splice(idx_path, path_len, strlen(".pack"), ".idx", strlen(".idx")); + if (error < 0) + return error; + + return 0; +} + +static int pack_backend__writemidx(git_odb_backend *_backend) +{ + struct pack_backend *backend; + git_midx_writer *w = NULL; + struct git_pack_file *p; + size_t i; + int error = 0; + +#ifdef GIT_EXPERIMENTAL_SHA256 + git_midx_writer_options midx_opts = GIT_MIDX_WRITER_OPTIONS_INIT; +#endif + + GIT_ASSERT_ARG(_backend); + + backend = (struct pack_backend *)_backend; + +#ifdef GIT_EXPERIMENTAL_SHA256 + midx_opts.oid_type = backend->opts.oid_type; +#endif + + error = git_midx_writer_new(&w, backend->pack_folder +#ifdef GIT_EXPERIMENTAL_SHA256 + , &midx_opts +#endif + ); + + if (error < 0) + return error; + + git_vector_foreach(&backend->midx_packs, i, p) { + git_str idx_path = GIT_STR_INIT; + error = get_idx_path(&idx_path, backend, p); + if (error < 0) + goto cleanup; + error = git_midx_writer_add(w, git_str_cstr(&idx_path)); + git_str_dispose(&idx_path); + if (error < 0) + goto cleanup; + } + git_vector_foreach(&backend->packs, i, p) { + git_str idx_path = GIT_STR_INIT; + error = get_idx_path(&idx_path, backend, p); + if (error < 0) + goto cleanup; + error = git_midx_writer_add(w, git_str_cstr(&idx_path)); + git_str_dispose(&idx_path); + if (error < 0) + goto cleanup; + } + + /* + * Invalidate the previous midx before writing the new one. + */ + error = remove_multi_pack_index(backend); + if (error < 0) + goto cleanup; + error = git_midx_writer_commit(w); + if (error < 0) + goto cleanup; + error = refresh_multi_pack_index(backend); + +cleanup: + git_midx_writer_free(w); + return error; +} + +static void pack_backend__free(git_odb_backend *_backend) +{ + struct pack_backend *backend; + struct git_pack_file *p; + size_t i; + + if (!_backend) + return; + + backend = (struct pack_backend *)_backend; + + git_vector_foreach(&backend->midx_packs, i, p) + git_mwindow_put_pack(p); + git_vector_foreach(&backend->packs, i, p) + git_mwindow_put_pack(p); + + git_midx_free(backend->midx); + git_vector_dispose(&backend->midx_packs); + git_vector_dispose(&backend->packs); + git__free(backend->pack_folder); + git__free(backend); +} + +static int pack_backend__alloc( + struct pack_backend **out, + size_t initial_size, + const git_odb_backend_pack_options *opts) +{ + struct pack_backend *backend = git__calloc(1, sizeof(struct pack_backend)); + GIT_ERROR_CHECK_ALLOC(backend); + + if (git_vector_init(&backend->midx_packs, 0, NULL) < 0) { + git__free(backend); + return -1; + } + + if (git_vector_init(&backend->packs, initial_size, packfile_sort__cb) < 0) { + git_vector_dispose(&backend->midx_packs); + git__free(backend); + return -1; + } + + if (opts) + memcpy(&backend->opts, opts, sizeof(git_odb_backend_pack_options)); + + if (!backend->opts.oid_type) + backend->opts.oid_type = GIT_OID_DEFAULT; + + backend->parent.version = GIT_ODB_BACKEND_VERSION; + + backend->parent.read = &pack_backend__read; + backend->parent.read_prefix = &pack_backend__read_prefix; + backend->parent.read_header = &pack_backend__read_header; + backend->parent.exists = &pack_backend__exists; + backend->parent.exists_prefix = &pack_backend__exists_prefix; + backend->parent.refresh = &pack_backend__refresh; + backend->parent.foreach = &pack_backend__foreach; + backend->parent.writepack = &pack_backend__writepack; + backend->parent.writemidx = &pack_backend__writemidx; + backend->parent.freshen = &pack_backend__freshen; + backend->parent.free = &pack_backend__free; + + *out = backend; + return 0; +} + +#ifdef GIT_EXPERIMENTAL_SHA256 +int git_odb_backend_one_pack( + git_odb_backend **backend_out, + const char *idx, + const git_odb_backend_pack_options *opts) +#else +int git_odb_backend_one_pack( + git_odb_backend **backend_out, + const char *idx) +#endif +{ + struct pack_backend *backend = NULL; + struct git_pack_file *packfile = NULL; + +#ifndef GIT_EXPERIMENTAL_SHA256 + git_odb_backend_pack_options *opts = NULL; +#endif + + git_oid_t oid_type = opts ? opts->oid_type : 0; + + if (pack_backend__alloc(&backend, 1, opts) < 0) + return -1; + + if (git_mwindow_get_pack(&packfile, idx, oid_type) < 0 || + git_vector_insert(&backend->packs, packfile) < 0) { + pack_backend__free((git_odb_backend *)backend); + return -1; + } + + *backend_out = (git_odb_backend *)backend; + return 0; +} + +#ifdef GIT_EXPERIMENTAL_SHA256 +int git_odb_backend_pack( + git_odb_backend **backend_out, + const char *objects_dir, + const git_odb_backend_pack_options *opts) +#else +int git_odb_backend_pack( + git_odb_backend **backend_out, + const char *objects_dir) +#endif +{ + int error = 0; + struct pack_backend *backend = NULL; + git_str path = GIT_STR_INIT; + +#ifndef GIT_EXPERIMENTAL_SHA256 + git_odb_backend_pack_options *opts = NULL; +#endif + + if (pack_backend__alloc(&backend, 8, opts) < 0) + return -1; + + if (!(error = git_str_joinpath(&path, objects_dir, "pack")) && + git_fs_path_isdir(git_str_cstr(&path))) { + backend->pack_folder = git_str_detach(&path); + error = pack_backend__refresh((git_odb_backend *)backend); + } + + if (error < 0) { + pack_backend__free((git_odb_backend *)backend); + backend = NULL; + } + + *backend_out = (git_odb_backend *)backend; + + git_str_dispose(&path); + + return error; +} diff --git a/src/libgit2/oid.c b/src/libgit2/oid.c new file mode 100644 index 00000000000..db549f6abec --- /dev/null +++ b/src/libgit2/oid.c @@ -0,0 +1,508 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "oid.h" + +#include "git2/oid.h" +#include "repository.h" +#include "runtime.h" +#include +#include + +const git_oid git_oid__empty_blob_sha1 = + GIT_OID_INIT(GIT_OID_SHA1, + { 0xe6, 0x9d, 0xe2, 0x9b, 0xb2, 0xd1, 0xd6, 0x43, 0x4b, 0x8b, + 0x29, 0xae, 0x77, 0x5a, 0xd8, 0xc2, 0xe4, 0x8c, 0x53, 0x91 }); +const git_oid git_oid__empty_tree_sha1 = + GIT_OID_INIT(GIT_OID_SHA1, + { 0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60, + 0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04 }); + +static const unsigned char git_oid_zero[GIT_OID_MAX_SIZE] = {0}; + +static int oid_error_invalid(const char *msg) +{ + git_error_set(GIT_ERROR_INVALID, "unable to parse OID - %s", msg); + return -1; +} + +int git_oid_from_prefix(git_oid *out, const char *str, size_t len, git_oid_t type) +{ + size_t size, p; + int v; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(str); + + if (!(size = git_oid_size(type))) + return oid_error_invalid("unknown type"); + + if (!len) + return oid_error_invalid("too short"); + + if (len > git_oid_hexsize(type)) + return oid_error_invalid("too long"); + +#ifdef GIT_EXPERIMENTAL_SHA256 + out->type = type; +#endif + memset(out->id, 0, size); + + for (p = 0; p < len; p++) { + v = git__fromhex(str[p]); + if (v < 0) + return oid_error_invalid("contains invalid characters"); + + out->id[p / 2] |= (unsigned char)(v << (p % 2 ? 0 : 4)); + } + + return 0; +} + +int git_oid_from_string(git_oid *out, const char *str, git_oid_t type) +{ + size_t hexsize; + + if (!(hexsize = git_oid_hexsize(type))) + return oid_error_invalid("unknown type"); + + if (git_oid_from_prefix(out, str, hexsize, type) < 0) + return -1; + + if (str[hexsize] != '\0') + return oid_error_invalid("too long"); + + return 0; +} + +int git_oid_from_raw(git_oid *out, const unsigned char *raw, git_oid_t type) +{ + size_t size; + + if (!(size = git_oid_size(type))) + return oid_error_invalid("unknown type"); + +#ifdef GIT_EXPERIMENTAL_SHA256 + out->type = type; +#endif + memcpy(out->id, raw, size); + return 0; +} + +int git_oid_fromstrn( + git_oid *out, + const char *str, + size_t length) +{ + return git_oid_from_prefix(out, str, length, GIT_OID_SHA1); +} + +int git_oid_fromstrp(git_oid *out, const char *str) +{ + return git_oid_from_prefix(out, str, strlen(str), GIT_OID_SHA1); +} + +int git_oid_fromstr(git_oid *out, const char *str) +{ + return git_oid_from_prefix(out, str, GIT_OID_SHA1_HEXSIZE, GIT_OID_SHA1); +} + +int git_oid_nfmt(char *str, size_t n, const git_oid *oid) +{ + size_t hex_size; + + if (!oid) { + memset(str, 0, n); + return 0; + } + + if (!(hex_size = git_oid_hexsize(git_oid_type(oid)))) + return oid_error_invalid("unknown type"); + + if (n > hex_size) { + memset(&str[hex_size], 0, n - hex_size); + n = hex_size; + } + + git_oid_fmt_substr(str, oid, 0, n); + return 0; +} + +int git_oid_fmt(char *str, const git_oid *oid) +{ + return git_oid_nfmt(str, git_oid_hexsize(git_oid_type(oid)), oid); +} + +int git_oid_pathfmt(char *str, const git_oid *oid) +{ + size_t hex_size; + + if (!(hex_size = git_oid_hexsize(git_oid_type(oid)))) + return oid_error_invalid("unknown type"); + + git_oid_fmt_substr(str, oid, 0, 2); + str[2] = '/'; + git_oid_fmt_substr(&str[3], oid, 2, (hex_size - 2)); + return 0; +} + +static git_tlsdata_key thread_str_key; + +static void GIT_SYSTEM_CALL thread_str_free(void *s) +{ + char *str = (char *)s; + git__free(str); +} + +static void thread_str_global_shutdown(void) +{ + char *str = git_tlsdata_get(thread_str_key); + git_tlsdata_set(thread_str_key, NULL); + + git__free(str); + git_tlsdata_dispose(thread_str_key); +} + +int git_oid_global_init(void) +{ + if (git_tlsdata_init(&thread_str_key, thread_str_free) != 0) + return -1; + + return git_runtime_shutdown_register(thread_str_global_shutdown); +} + +char *git_oid_tostr_s(const git_oid *oid) +{ + char *str; + + if ((str = git_tlsdata_get(thread_str_key)) == NULL) { + if ((str = git__malloc(GIT_OID_MAX_HEXSIZE + 1)) == NULL) + return NULL; + + git_tlsdata_set(thread_str_key, str); + } + + git_oid_nfmt(str, git_oid_hexsize(git_oid_type(oid)) + 1, oid); + return str; +} + +char *git_oid_allocfmt(const git_oid *oid) +{ + size_t hex_size = git_oid_hexsize(git_oid_type(oid)); + char *str = git__malloc(hex_size + 1); + + if (!hex_size || !str) + return NULL; + + if (git_oid_nfmt(str, hex_size + 1, oid) < 0) { + git__free(str); + return NULL; + } + + return str; +} + +char *git_oid_tostr(char *out, size_t n, const git_oid *oid) +{ + size_t hex_size; + + if (!out || n == 0) + return ""; + + hex_size = oid ? git_oid_hexsize(git_oid_type(oid)) : 0; + + if (n > hex_size + 1) + n = hex_size + 1; + + git_oid_nfmt(out, n - 1, oid); /* allow room for terminating NUL */ + out[n - 1] = '\0'; + + return out; +} + +int git_oid_fromraw(git_oid *out, const unsigned char *raw) +{ + return git_oid_from_raw(out, raw, GIT_OID_SHA1); +} + +int git_oid_cpy(git_oid *out, const git_oid *src) +{ + size_t size; + + if (!(size = git_oid_size(git_oid_type(src)))) + return oid_error_invalid("unknown type"); + +#ifdef GIT_EXPERIMENTAL_SHA256 + out->type = src->type; +#endif + + return git_oid_raw_cpy(out->id, src->id, size); +} + +int git_oid_cmp(const git_oid *a, const git_oid *b) +{ + return git_oid__cmp(a, b); +} + +int git_oid_equal(const git_oid *a, const git_oid *b) +{ + return (git_oid__cmp(a, b) == 0); +} + +int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, size_t len) +{ +#ifdef GIT_EXPERIMENTAL_SHA256 + if (oid_a->type != oid_b->type) + return oid_a->type - oid_b->type; +#endif + + return git_oid_raw_ncmp(oid_a->id, oid_b->id, len); +} + +int git_oid_strcmp(const git_oid *oid_a, const char *str) +{ + const unsigned char *a; + unsigned char strval; + long size = (long)git_oid_size(git_oid_type(oid_a)); + int hexval; + + for (a = oid_a->id; *str && (a - oid_a->id) < size; ++a) { + if ((hexval = git__fromhex(*str++)) < 0) + return -1; + strval = (unsigned char)(hexval << 4); + if (*str) { + if ((hexval = git__fromhex(*str++)) < 0) + return -1; + strval |= hexval; + } + if (*a != strval) + return (*a - strval); + } + + return 0; +} + +int git_oid_streq(const git_oid *oid_a, const char *str) +{ + return git_oid_strcmp(oid_a, str) == 0 ? 0 : -1; +} + +int git_oid_is_zero(const git_oid *oid_a) +{ + const unsigned char *a = oid_a->id; + size_t size = git_oid_size(git_oid_type(oid_a)); + +#ifdef GIT_EXPERIMENTAL_SHA256 + if (!oid_a->type) + return 1; + else if (!size) + return 0; +#endif + + return git_oid_raw_cmp(a, git_oid_zero, size) == 0 ? 1 : 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_oid_iszero(const git_oid *oid_a) +{ + return git_oid_is_zero(oid_a); +} +#endif + +typedef short node_index; + +typedef union { + const char *tail; + node_index children[16]; +} trie_node; + +struct git_oid_shorten { + trie_node *nodes; + size_t node_count, size; + int min_length, full; +}; + +static int resize_trie(git_oid_shorten *self, size_t new_size) +{ + self->nodes = git__reallocarray(self->nodes, new_size, sizeof(trie_node)); + GIT_ERROR_CHECK_ALLOC(self->nodes); + + if (new_size > self->size) { + memset(&self->nodes[self->size], 0x0, (new_size - self->size) * sizeof(trie_node)); + } + + self->size = new_size; + return 0; +} + +static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, const char *oid) +{ + trie_node *node, *leaf; + node_index idx_leaf; + + if (os->node_count >= os->size) { + if (resize_trie(os, os->size * 2) < 0) + return NULL; + } + + idx_leaf = (node_index)os->node_count++; + + if (os->node_count == SHRT_MAX) { + os->full = 1; + return NULL; + } + + node = &os->nodes[idx]; + node->children[push_at] = -idx_leaf; + + leaf = &os->nodes[idx_leaf]; + leaf->tail = oid; + + return node; +} + +git_oid_shorten *git_oid_shorten_new(size_t min_length) +{ + git_oid_shorten *os; + + GIT_ASSERT_ARG_WITH_RETVAL((size_t)((int)min_length) == min_length, NULL); + + os = git__calloc(1, sizeof(git_oid_shorten)); + if (os == NULL) + return NULL; + + if (resize_trie(os, 16) < 0) { + git__free(os); + return NULL; + } + + os->node_count = 1; + os->min_length = (int)min_length; + + return os; +} + +void git_oid_shorten_free(git_oid_shorten *os) +{ + if (os == NULL) + return; + + git__free(os->nodes); + git__free(os); +} + + +/* + * What wizardry is this? + * + * This is just a memory-optimized trie: basically a very fancy + * 16-ary tree, which is used to store the prefixes of the OID + * strings. + * + * Read more: http://en.wikipedia.org/wiki/Trie + * + * Magic that happens in this method: + * + * - Each node in the trie is an union, so it can work both as + * a normal node, or as a leaf. + * + * - Each normal node points to 16 children (one for each possible + * character in the oid). This is *not* stored in an array of + * pointers, because in a 64-bit arch this would be sucking + * 16*sizeof(void*) = 128 bytes of memory per node, which is + * insane. What we do is store Node Indexes, and use these indexes + * to look up each node in the om->index array. These indexes are + * signed shorts, so this limits the amount of unique OIDs that + * fit in the structure to about 20000 (assuming a more or less uniform + * distribution). + * + * - All the nodes in om->index array are stored contiguously in + * memory, and each of them is 32 bytes, so we fit 2x nodes per + * cache line. Convenient for speed. + * + * - To differentiate the leafs from the normal nodes, we store all + * the indexes towards a leaf as a negative index (indexes to normal + * nodes are positives). When we find that one of the children for + * a node has a negative value, that means it's going to be a leaf. + * This reduces the amount of indexes we have by two, but also reduces + * the size of each node by 1-4 bytes (the amount we would need to + * add a `is_leaf` field): this is good because it allows the nodes + * to fit cleanly in cache lines. + * + * - Once we reach an empty children, instead of continuing to insert + * new nodes for each remaining character of the OID, we store a pointer + * to the tail in the leaf; if the leaf is reached again, we turn it + * into a normal node and use the tail to create a new leaf. + * + * This is a pretty good balance between performance and memory usage. + */ +int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid) +{ + int i; + bool is_leaf; + node_index idx; + + if (os->full) { + git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - OID set full"); + return -1; + } + + if (text_oid == NULL) + return os->min_length; + + idx = 0; + is_leaf = false; + + for (i = 0; i < GIT_OID_SHA1_HEXSIZE; ++i) { + int c = git__fromhex(text_oid[i]); + trie_node *node; + + if (c == -1) { + git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - invalid hex value"); + return -1; + } + + node = &os->nodes[idx]; + + if (is_leaf) { + const char *tail; + + tail = node->tail; + node->tail = NULL; + + node = push_leaf(os, idx, git__fromhex(tail[0]), &tail[1]); + if (node == NULL) { + if (os->full) + git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - OID set full"); + return -1; + } + } + + if (node->children[c] == 0) { + if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL) { + if (os->full) + git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - OID set full"); + return -1; + } + break; + } + + idx = node->children[c]; + is_leaf = false; + + if (idx < 0) { + node->children[c] = idx = -idx; + is_leaf = true; + } + } + + if (++i > os->min_length) + os->min_length = i; + + return os->min_length; +} + diff --git a/src/libgit2/oid.h b/src/libgit2/oid.h new file mode 100644 index 00000000000..cfcabce7111 --- /dev/null +++ b/src/libgit2/oid.h @@ -0,0 +1,278 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_oid_h__ +#define INCLUDE_oid_h__ + +#include "common.h" + +#include "git2/experimental.h" +#include "git2/oid.h" +#include "hash.h" + +#ifdef GIT_EXPERIMENTAL_SHA256 +# define GIT_OID_NONE { 0, { 0 } } +# define GIT_OID_INIT(type, ...) { type, __VA_ARGS__ } +#else +# define GIT_OID_NONE { { 0 } } +# define GIT_OID_INIT(type, ...) { __VA_ARGS__ } +#endif + +extern const git_oid git_oid__empty_blob_sha1; +extern const git_oid git_oid__empty_tree_sha1; + +GIT_INLINE(git_oid_t) git_oid_type(const git_oid *oid) +{ +#ifdef GIT_EXPERIMENTAL_SHA256 + return oid->type; +#else + GIT_UNUSED(oid); + return GIT_OID_SHA1; +#endif +} + +GIT_INLINE(size_t) git_oid_size(git_oid_t type) +{ + switch (type) { + case GIT_OID_SHA1: + return GIT_OID_SHA1_SIZE; + +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + return GIT_OID_SHA256_SIZE; +#endif + + } + + return 0; +} + +GIT_INLINE(size_t) git_oid_hexsize(git_oid_t type) +{ + switch (type) { + case GIT_OID_SHA1: + return GIT_OID_SHA1_HEXSIZE; + +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + return GIT_OID_SHA256_HEXSIZE; +#endif + + } + + return 0; +} + +GIT_INLINE(bool) git_oid_type_is_valid(git_oid_t type) +{ + return (type == GIT_OID_SHA1 +#ifdef GIT_EXPERIMENTAL_SHA256 + || type == GIT_OID_SHA256 +#endif + ); +} + +GIT_INLINE(const char *) git_oid_type_name(git_oid_t type) +{ + switch (type) { + case GIT_OID_SHA1: + return "sha1"; + +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + return "sha256"; +#endif + } + + return "unknown"; +} + +GIT_INLINE(git_oid_t) git_oid_type_fromstr(const char *name) +{ + if (strcmp(name, "sha1") == 0) + return GIT_OID_SHA1; + +#ifdef GIT_EXPERIMENTAL_SHA256 + if (strcmp(name, "sha256") == 0) + return GIT_OID_SHA256; +#endif + + return 0; +} + +GIT_INLINE(git_oid_t) git_oid_type_fromstrn(const char *name, size_t len) +{ + if (len == CONST_STRLEN("sha1") && strncmp(name, "sha1", len) == 0) + return GIT_OID_SHA1; + +#ifdef GIT_EXPERIMENTAL_SHA256 + if (len == CONST_STRLEN("sha256") && strncmp(name, "sha256", len) == 0) + return GIT_OID_SHA256; +#endif + + return 0; +} + +GIT_INLINE(git_hash_algorithm_t) git_oid_algorithm(git_oid_t type) +{ + switch (type) { + case GIT_OID_SHA1: + return GIT_HASH_ALGORITHM_SHA1; + +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + return GIT_HASH_ALGORITHM_SHA256; +#endif + + } + + return 0; +} + +/** + * Format a git_oid into a newly allocated c-string. + * + * The c-string is owned by the caller and needs to be manually freed. + * + * @param id the oid structure to format + * @return the c-string; NULL if memory is exhausted. Caller must + * deallocate the string with git__free(). + */ +char *git_oid_allocfmt(const git_oid *id); + +/** + * Format the requested nibbles of an object id. + * + * @param str the string to write into + * @param oid the oid structure to format + * @param start the starting number of nibbles + * @param count the number of nibbles to format + */ +GIT_INLINE(void) git_oid_fmt_substr( + char *str, + const git_oid *oid, + size_t start, + size_t count) +{ + static char hex[] = "0123456789abcdef"; + size_t i, end = start + count, min = start / 2, max = end / 2; + + if (start & 1) + *str++ = hex[oid->id[min++] & 0x0f]; + + for (i = min; i < max; i++) { + *str++ = hex[oid->id[i] >> 4]; + *str++ = hex[oid->id[i] & 0x0f]; + } + + if (end & 1) + *str++ = hex[oid->id[i] >> 4]; +} + +GIT_INLINE(int) git_oid_raw_ncmp( + const unsigned char *sha1, + const unsigned char *sha2, + size_t len) +{ + if (len > GIT_OID_MAX_HEXSIZE) + len = GIT_OID_MAX_HEXSIZE; + + while (len > 1) { + if (*sha1 != *sha2) + return 1; + sha1++; + sha2++; + len -= 2; + }; + + if (len) + if ((*sha1 ^ *sha2) & 0xf0) + return 1; + + return 0; +} + +GIT_INLINE(int) git_oid_raw_cmp( + const unsigned char *sha1, + const unsigned char *sha2, + size_t size) +{ + return memcmp(sha1, sha2, size); +} + +GIT_INLINE(int) git_oid_raw_cpy( + unsigned char *dst, + const unsigned char *src, + size_t size) +{ + memcpy(dst, src, size); + return 0; +} + +/* + * Compare two oid structures. + * + * @param a first oid structure. + * @param b second oid structure. + * @return <0, 0, >0 if a < b, a == b, a > b. + */ +GIT_INLINE(int) git_oid__cmp(const git_oid *a, const git_oid *b) +{ +#ifdef GIT_EXPERIMENTAL_SHA256 + if (a->type != b->type) + return a->type - b->type; + + return git_oid_raw_cmp(a->id, b->id, git_oid_size(a->type)); +#else + return git_oid_raw_cmp(a->id, b->id, git_oid_size(GIT_OID_SHA1)); +#endif +} + +GIT_INLINE(void) git_oid__cpy_prefix( + git_oid *out, const git_oid *id, size_t len) +{ +#ifdef GIT_EXPERIMENTAL_SHA256 + out->type = id->type; +#endif + + memcpy(&out->id, id->id, (len + 1) / 2); + + if (len & 1) + out->id[len / 2] &= 0xF0; +} + +GIT_INLINE(bool) git_oid__is_hexstr(const char *str, git_oid_t type) +{ + size_t i; + + for (i = 0; str[i] != '\0'; i++) { + if (git__fromhex(str[i]) < 0) + return false; + } + + return (i == git_oid_hexsize(type)); +} + +GIT_INLINE(void) git_oid_clear(git_oid *out, git_oid_t type) +{ + memset(out->id, 0, git_oid_size(type)); + +#ifdef GIT_EXPERIMENTAL_SHA256 + out->type = type; +#endif +} + +/* SHA256 support */ + +#ifndef GIT_EXPERIMENTAL_SHA256 +int git_oid_from_string(git_oid *out, const char *str, git_oid_t type); +int git_oid_from_prefix(git_oid *out, const char *str, size_t len, git_oid_t type); +int git_oid_from_raw(git_oid *out, const unsigned char *data, git_oid_t type); +#endif + +int git_oid_global_init(void); + +#endif diff --git a/src/libgit2/oidarray.c b/src/libgit2/oidarray.c new file mode 100644 index 00000000000..37f67756aa5 --- /dev/null +++ b/src/libgit2/oidarray.c @@ -0,0 +1,89 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "oidarray.h" + +#include "git2/oidarray.h" +#include "array.h" + +void git_oidarray_dispose(git_oidarray *arr) +{ + git__free(arr->ids); +} + +void git_oidarray__from_array(git_oidarray *out, const git_array_oid_t *array) +{ + out->count = array->size; + out->ids = array->ptr; +} + +void git_oidarray__to_array(git_array_oid_t *out, const git_oidarray *array) +{ + out->ptr = array->ids; + out->size = array->count; + out->asize = array->count; +} + +void git_oidarray__reverse(git_oidarray *arr) +{ + size_t i; + git_oid tmp; + + for (i = 0; i < arr->count / 2; i++) { + git_oid_cpy(&tmp, &arr->ids[i]); + git_oid_cpy(&arr->ids[i], &arr->ids[(arr->count-1)-i]); + git_oid_cpy(&arr->ids[(arr->count-1)-i], &tmp); + } +} + +int git_oidarray__add(git_array_oid_t *arr, git_oid *id) +{ + git_oid *add, *iter; + size_t i; + + git_array_foreach(*arr, i, iter) { + if (git_oid_cmp(iter, id) == 0) + return 0; + } + + if ((add = git_array_alloc(*arr)) == NULL) + return -1; + + git_oid_cpy(add, id); + return 0; +} + +bool git_oidarray__remove(git_array_oid_t *arr, git_oid *id) +{ + bool found = false; + size_t remain, i; + git_oid *iter; + + git_array_foreach(*arr, i, iter) { + if (git_oid_cmp(iter, id) == 0) { + arr->size--; + remain = arr->size - i; + + if (remain > 0) + memmove(&arr->ptr[i], &arr->ptr[i+1], remain * sizeof(git_oid)); + + found = true; + break; + } + } + + return found; +} + +#ifndef GIT_DEPRECATE_HARD + +void git_oidarray_free(git_oidarray *arr) +{ + git_oidarray_dispose(arr); +} + +#endif diff --git a/src/libgit2/oidarray.h b/src/libgit2/oidarray.h new file mode 100644 index 00000000000..8f1543a32e0 --- /dev/null +++ b/src/libgit2/oidarray.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_oidarray_h__ +#define INCLUDE_oidarray_h__ + +#include "common.h" + +#include "git2/oidarray.h" +#include "array.h" + +typedef git_array_t(git_oid) git_array_oid_t; + +extern void git_oidarray__reverse(git_oidarray *arr); +extern void git_oidarray__from_array(git_oidarray *out, const git_array_oid_t *array); +extern void git_oidarray__to_array(git_array_oid_t *out, const git_oidarray *array); + +int git_oidarray__add(git_array_oid_t *arr, git_oid *id); +bool git_oidarray__remove(git_array_oid_t *arr, git_oid *id); + +#endif diff --git a/src/libgit2/pack-objects.c b/src/libgit2/pack-objects.c new file mode 100644 index 00000000000..072d2902d89 --- /dev/null +++ b/src/libgit2/pack-objects.c @@ -0,0 +1,1864 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "pack-objects.h" + +#include "buf.h" +#include "zstream.h" +#include "delta.h" +#include "iterator.h" +#include "pack.h" +#include "thread.h" +#include "tree.h" +#include "util.h" +#include "revwalk.h" +#include "commit_list.h" + +#include "git2/pack.h" +#include "git2/commit.h" +#include "git2/tag.h" +#include "git2/indexer.h" +#include "git2/config.h" + +struct unpacked { + git_pobject *object; + void *data; + struct git_delta_index *index; + size_t depth; +}; + +struct tree_walk_context { + git_packbuilder *pb; + git_str buf; +}; + +struct pack_write_context { + git_indexer *indexer; + git_indexer_progress *stats; +}; + +struct walk_object { + git_oid id; + unsigned int uninteresting:1, + seen:1; +}; + +#ifdef GIT_THREADS +# define GIT_PACKBUILDER__MUTEX_OP(pb, mtx, op) git_mutex_##op(&(pb)->mtx) +#else +# define GIT_PACKBUILDER__MUTEX_OP(pb, mtx, op) git__noop() +#endif + +#define git_packbuilder__cache_lock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, cache_mutex, lock) +#define git_packbuilder__cache_unlock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, cache_mutex, unlock) +#define git_packbuilder__progress_lock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, progress_mutex, lock) +#define git_packbuilder__progress_unlock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, progress_mutex, unlock) + +/* The minimal interval between progress updates (in seconds). */ +#define MIN_PROGRESS_UPDATE_INTERVAL 0.5 + +/* Size of the buffer to feed to zlib */ +#define COMPRESS_BUFLEN (1024 * 1024) + +GIT_HASHMAP_OID_FUNCTIONS(git_packbuilder_pobjectmap, GIT_HASHMAP_INLINE, git_pobject *); +GIT_HASHMAP_OID_FUNCTIONS(git_packbuilder_walk_objectmap, GIT_HASHMAP_INLINE, struct walk_object *); + +static unsigned name_hash(const char *name) +{ + unsigned c, hash = 0; + + if (!name) + return 0; + + /* + * This effectively just creates a sortable number from the + * last sixteen non-whitespace characters. Last characters + * count "most", so things that end in ".c" sort together. + */ + while ((c = *name++) != 0) { + if (git__isspace(c)) + continue; + hash = (hash >> 2) + (c << 24); + } + return hash; +} + +static int packbuilder_config(git_packbuilder *pb) +{ + git_config *config; + int ret = 0; + int64_t val; + + if ((ret = git_repository_config_snapshot(&config, pb->repo)) < 0) + return ret; + +#define config_get(KEY,DST,DFLT) do { \ + ret = git_config_get_int64(&val, config, KEY); \ + if (!ret) { \ + if (!git__is_sizet(val)) { \ + git_error_set(GIT_ERROR_CONFIG, \ + "configuration value '%s' is too large", KEY); \ + ret = -1; \ + goto out; \ + } \ + (DST) = (size_t)val; \ + } else if (ret == GIT_ENOTFOUND) { \ + (DST) = (DFLT); \ + ret = 0; \ + } else if (ret < 0) goto out; } while (0) + + config_get("pack.deltaCacheSize", pb->max_delta_cache_size, + GIT_PACK_DELTA_CACHE_SIZE); + config_get("pack.deltaCacheLimit", pb->cache_max_small_delta_size, + GIT_PACK_DELTA_CACHE_LIMIT); + config_get("pack.deltaCacheSize", pb->big_file_threshold, + GIT_PACK_BIG_FILE_THRESHOLD); + config_get("pack.windowMemory", pb->window_memory_limit, 0); + +#undef config_get + +out: + git_config_free(config); + + return ret; +} + +int git_packbuilder_new(git_packbuilder **out, git_repository *repo) +{ + git_hash_algorithm_t hash_algorithm; + git_packbuilder *pb; + + *out = NULL; + + pb = git__calloc(1, sizeof(*pb)); + GIT_ERROR_CHECK_ALLOC(pb); + + pb->oid_type = repo->oid_type; + + hash_algorithm = git_oid_algorithm(pb->oid_type); + GIT_ASSERT(hash_algorithm); + + if (git_pool_init(&pb->object_pool, sizeof(struct walk_object)) < 0) + goto on_error; + + pb->repo = repo; + pb->nr_threads = 1; /* do not spawn any thread by default */ + + if (git_hash_ctx_init(&pb->ctx, hash_algorithm) < 0 || + git_zstream_init(&pb->zstream, GIT_ZSTREAM_DEFLATE) < 0 || + git_repository_odb(&pb->odb, repo) < 0 || + packbuilder_config(pb) < 0) + goto on_error; + +#ifdef GIT_THREADS + + if (git_mutex_init(&pb->cache_mutex) || + git_mutex_init(&pb->progress_mutex) || + git_cond_init(&pb->progress_cond)) + { + git_error_set(GIT_ERROR_OS, "failed to initialize packbuilder mutex"); + goto on_error; + } + +#endif + + *out = pb; + return 0; + +on_error: + git_packbuilder_free(pb); + return -1; +} + +unsigned int git_packbuilder_set_threads(git_packbuilder *pb, unsigned int n) +{ + GIT_ASSERT_ARG(pb); + +#ifdef GIT_THREADS + pb->nr_threads = n; +#else + GIT_UNUSED(n); + GIT_ASSERT(pb->nr_threads == 1); +#endif + + return pb->nr_threads; +} + +static int rehash(git_packbuilder *pb) +{ + git_pobject *po; + size_t i; + + git_packbuilder_pobjectmap_clear(&pb->object_ix); + + for (i = 0, po = pb->object_list; i < pb->nr_objects; i++, po++) { + if (git_packbuilder_pobjectmap_put(&pb->object_ix, &po->id, po) < 0) + return -1; + } + + return 0; +} + +int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid, + const char *name) +{ + git_pobject *po; + size_t newsize; + int ret; + + GIT_ASSERT_ARG(pb); + GIT_ASSERT_ARG(oid); + + /* If the object already exists in the hash table, then we don't + * have any work to do */ + if (git_packbuilder_pobjectmap_contains(&pb->object_ix, oid)) + return 0; + + if (pb->nr_objects >= pb->nr_alloc) { + GIT_ERROR_CHECK_ALLOC_ADD(&newsize, pb->nr_alloc, 1024); + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&newsize, newsize / 2, 3); + + if (!git__is_uint32(newsize)) { + git_error_set(GIT_ERROR_NOMEMORY, "packfile too large to fit in memory."); + return -1; + } + + pb->nr_alloc = newsize; + + pb->object_list = git__reallocarray(pb->object_list, + pb->nr_alloc, sizeof(*po)); + GIT_ERROR_CHECK_ALLOC(pb->object_list); + + if (rehash(pb) < 0) + return -1; + } + + po = pb->object_list + pb->nr_objects; + memset(po, 0x0, sizeof(*po)); + + if ((ret = git_odb_read_header(&po->size, &po->type, pb->odb, oid)) < 0) + return ret; + + pb->nr_objects++; + git_oid_cpy(&po->id, oid); + po->hash = name_hash(name); + + if (git_packbuilder_pobjectmap_put(&pb->object_ix, &po->id, po) < 0) { + git_error_set_oom(); + return -1; + } + + pb->done = false; + + if (pb->progress_cb) { + uint64_t current_time = git_time_monotonic(); + uint64_t elapsed = current_time - pb->last_progress_report_time; + + if (elapsed >= MIN_PROGRESS_UPDATE_INTERVAL) { + pb->last_progress_report_time = current_time; + + ret = pb->progress_cb( + GIT_PACKBUILDER_ADDING_OBJECTS, + pb->nr_objects, 0, pb->progress_cb_payload); + + if (ret) + return git_error_set_after_callback(ret); + } + } + + return 0; +} + +static int get_delta(void **out, git_odb *odb, git_pobject *po) +{ + git_odb_object *src = NULL, *trg = NULL; + size_t delta_size; + void *delta_buf; + int error; + + *out = NULL; + + if (git_odb_read(&src, odb, &po->delta->id) < 0 || + git_odb_read(&trg, odb, &po->id) < 0) + goto on_error; + + error = git_delta(&delta_buf, &delta_size, + git_odb_object_data(src), git_odb_object_size(src), + git_odb_object_data(trg), git_odb_object_size(trg), + 0); + + if (error < 0 && error != GIT_EBUFS) + goto on_error; + + if (error == GIT_EBUFS || delta_size != po->delta_size) { + git_error_set(GIT_ERROR_INVALID, "delta size changed"); + goto on_error; + } + + *out = delta_buf; + + git_odb_object_free(src); + git_odb_object_free(trg); + return 0; + +on_error: + git_odb_object_free(src); + git_odb_object_free(trg); + return -1; +} + +static int write_object( + git_packbuilder *pb, + git_pobject *po, + int (*write_cb)(void *buf, size_t size, void *cb_data), + void *cb_data) +{ + git_odb_object *obj = NULL; + git_object_t type; + unsigned char hdr[10], *zbuf = NULL; + void *data = NULL; + size_t hdr_len, zbuf_len = COMPRESS_BUFLEN, data_len, oid_size; + int error; + + oid_size = git_oid_size(pb->oid_type); + + /* + * If we have a delta base, let's use the delta to save space. + * Otherwise load the whole object. 'data' ends up pointing to + * whatever data we want to put into the packfile. + */ + if (po->delta) { + if (po->delta_data) + data = po->delta_data; + else if ((error = get_delta(&data, pb->odb, po)) < 0) + goto done; + + data_len = po->delta_size; + type = GIT_PACKFILE_REF_DELTA; + } else { + if ((error = git_odb_read(&obj, pb->odb, &po->id)) < 0) + goto done; + + data = (void *)git_odb_object_data(obj); + data_len = git_odb_object_size(obj); + type = git_odb_object_type(obj); + } + + /* Write header */ + if ((error = git_packfile__object_header(&hdr_len, hdr, data_len, type)) < 0 || + (error = write_cb(hdr, hdr_len, cb_data)) < 0 || + (error = git_hash_update(&pb->ctx, hdr, hdr_len)) < 0) + goto done; + + if (type == GIT_PACKFILE_REF_DELTA) { + if ((error = write_cb(po->delta->id.id, oid_size, cb_data)) < 0 || + (error = git_hash_update(&pb->ctx, po->delta->id.id, oid_size)) < 0) + goto done; + } + + /* Write data */ + if (po->z_delta_size) { + data_len = po->z_delta_size; + + if ((error = write_cb(data, data_len, cb_data)) < 0 || + (error = git_hash_update(&pb->ctx, data, data_len)) < 0) + goto done; + } else { + zbuf = git__malloc(zbuf_len); + GIT_ERROR_CHECK_ALLOC(zbuf); + + git_zstream_reset(&pb->zstream); + + if ((error = git_zstream_set_input(&pb->zstream, data, data_len)) < 0) + goto done; + + while (!git_zstream_done(&pb->zstream)) { + if ((error = git_zstream_get_output(zbuf, &zbuf_len, &pb->zstream)) < 0 || + (error = write_cb(zbuf, zbuf_len, cb_data)) < 0 || + (error = git_hash_update(&pb->ctx, zbuf, zbuf_len)) < 0) + goto done; + + zbuf_len = COMPRESS_BUFLEN; /* reuse buffer */ + } + } + + /* + * If po->delta is true, data is a delta and it is our + * responsibility to free it (otherwise it's a git_object's + * data). We set po->delta_data to NULL in case we got the + * data from there instead of get_delta(). If we didn't, + * there's no harm. + */ + if (po->delta) { + git__free(data); + po->delta_data = NULL; + } + + pb->nr_written++; + +done: + git__free(zbuf); + git_odb_object_free(obj); + return error; +} + +enum write_one_status { + WRITE_ONE_SKIP = -1, /* already written */ + WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */ + WRITE_ONE_WRITTEN = 1, /* normal */ + WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */ +}; + +static int write_one( + enum write_one_status *status, + git_packbuilder *pb, + git_pobject *po, + int (*write_cb)(void *buf, size_t size, void *cb_data), + void *cb_data) +{ + int error; + + if (po->recursing) { + *status = WRITE_ONE_RECURSIVE; + return 0; + } else if (po->written) { + *status = WRITE_ONE_SKIP; + return 0; + } + + if (po->delta) { + po->recursing = 1; + + if ((error = write_one(status, pb, po->delta, write_cb, cb_data)) < 0) + return error; + + /* we cannot depend on this one */ + if (*status == WRITE_ONE_RECURSIVE) + po->delta = NULL; + } + + *status = WRITE_ONE_WRITTEN; + po->written = 1; + po->recursing = 0; + + return write_object(pb, po, write_cb, cb_data); +} + +GIT_INLINE(void) add_to_write_order(git_pobject **wo, size_t *endp, + git_pobject *po) +{ + if (po->filled) + return; + wo[(*endp)++] = po; + po->filled = 1; +} + +static void add_descendants_to_write_order(git_pobject **wo, size_t *endp, + git_pobject *po) +{ + int add_to_order = 1; + while (po) { + if (add_to_order) { + git_pobject *s; + /* add this node... */ + add_to_write_order(wo, endp, po); + /* all its siblings... */ + for (s = po->delta_sibling; s; s = s->delta_sibling) { + add_to_write_order(wo, endp, s); + } + } + /* drop down a level to add left subtree nodes if possible */ + if (po->delta_child) { + add_to_order = 1; + po = po->delta_child; + } else { + add_to_order = 0; + /* our sibling might have some children, it is next */ + if (po->delta_sibling) { + po = po->delta_sibling; + continue; + } + /* go back to our parent node */ + po = po->delta; + while (po && !po->delta_sibling) { + /* we're on the right side of a subtree, keep + * going up until we can go right again */ + po = po->delta; + } + if (!po) { + /* done- we hit our original root node */ + return; + } + /* pass it off to sibling at this level */ + po = po->delta_sibling; + } + }; +} + +static void add_family_to_write_order(git_pobject **wo, size_t *endp, + git_pobject *po) +{ + git_pobject *root; + + for (root = po; root->delta; root = root->delta) + ; /* nothing */ + add_descendants_to_write_order(wo, endp, root); +} + +static int cb_tag_foreach(const char *name, git_oid *oid, void *data) +{ + git_packbuilder *pb = data; + git_pobject *po; + + GIT_UNUSED(name); + + if (git_packbuilder_pobjectmap_get(&po, &pb->object_ix, oid) != 0) + return 0; + + po->tagged = 1; + + /* TODO: peel objects */ + + return 0; +} + +static int compute_write_order(git_pobject ***out, git_packbuilder *pb) +{ + size_t i, wo_end, last_untagged; + git_pobject **wo; + + *out = NULL; + + if (!pb->nr_objects) + return 0; + + if ((wo = git__mallocarray(pb->nr_objects, sizeof(*wo))) == NULL) + return -1; + + for (i = 0; i < pb->nr_objects; i++) { + git_pobject *po = pb->object_list + i; + po->tagged = 0; + po->filled = 0; + po->delta_child = NULL; + po->delta_sibling = NULL; + } + + /* + * Fully connect delta_child/delta_sibling network. + * Make sure delta_sibling is sorted in the original + * recency order. + */ + for (i = pb->nr_objects; i > 0;) { + git_pobject *po = &pb->object_list[--i]; + if (!po->delta) + continue; + /* Mark me as the first child */ + po->delta_sibling = po->delta->delta_child; + po->delta->delta_child = po; + } + + /* + * Mark objects that are at the tip of tags. + */ + if (git_tag_foreach(pb->repo, &cb_tag_foreach, pb) < 0) { + git__free(wo); + return -1; + } + + /* + * Give the objects in the original recency order until + * we see a tagged tip. + */ + for (i = wo_end = 0; i < pb->nr_objects; i++) { + git_pobject *po = pb->object_list + i; + if (po->tagged) + break; + add_to_write_order(wo, &wo_end, po); + } + last_untagged = i; + + /* + * Then fill all the tagged tips. + */ + for (; i < pb->nr_objects; i++) { + git_pobject *po = pb->object_list + i; + if (po->tagged) + add_to_write_order(wo, &wo_end, po); + } + + /* + * And then all remaining commits and tags. + */ + for (i = last_untagged; i < pb->nr_objects; i++) { + git_pobject *po = pb->object_list + i; + if (po->type != GIT_OBJECT_COMMIT && + po->type != GIT_OBJECT_TAG) + continue; + add_to_write_order(wo, &wo_end, po); + } + + /* + * And then all the trees. + */ + for (i = last_untagged; i < pb->nr_objects; i++) { + git_pobject *po = pb->object_list + i; + if (po->type != GIT_OBJECT_TREE) + continue; + add_to_write_order(wo, &wo_end, po); + } + + /* + * Finally all the rest in really tight order + */ + for (i = last_untagged; i < pb->nr_objects; i++) { + git_pobject *po = pb->object_list + i; + if (!po->filled) + add_family_to_write_order(wo, &wo_end, po); + } + + if (wo_end != pb->nr_objects) { + git__free(wo); + git_error_set(GIT_ERROR_INVALID, "invalid write order"); + return -1; + } + + *out = wo; + return 0; +} + +static int write_pack(git_packbuilder *pb, + int (*write_cb)(void *buf, size_t size, void *cb_data), + void *cb_data) +{ + git_pobject **write_order; + git_pobject *po; + enum write_one_status status; + struct git_pack_header ph; + git_oid entry_oid; + size_t i = 0; + int error; + + if ((error = compute_write_order(&write_order, pb)) < 0) + return error; + + if (!git__is_uint32(pb->nr_objects)) { + git_error_set(GIT_ERROR_INVALID, "too many objects"); + error = -1; + goto done; + } + + /* Write pack header */ + ph.hdr_signature = htonl(PACK_SIGNATURE); + ph.hdr_version = htonl(PACK_VERSION); + ph.hdr_entries = htonl(pb->nr_objects); + + if ((error = write_cb(&ph, sizeof(ph), cb_data)) < 0 || + (error = git_hash_update(&pb->ctx, &ph, sizeof(ph))) < 0) + goto done; + + pb->nr_remaining = pb->nr_objects; + do { + pb->nr_written = 0; + for ( ; i < pb->nr_objects; ++i) { + po = write_order[i]; + + if ((error = write_one(&status, pb, po, write_cb, cb_data)) < 0) + goto done; + } + + pb->nr_remaining -= pb->nr_written; + } while (pb->nr_remaining && i < pb->nr_objects); + + if ((error = git_hash_final(entry_oid.id, &pb->ctx)) < 0) + goto done; + + error = write_cb(entry_oid.id, git_oid_size(pb->oid_type), cb_data); + +done: + /* if callback cancelled writing, we must still free delta_data */ + for ( ; i < pb->nr_objects; ++i) { + po = write_order[i]; + if (po->delta_data) { + git__free(po->delta_data); + po->delta_data = NULL; + } + } + + git__free(write_order); + return error; +} + +static int write_pack_buf(void *buf, size_t size, void *data) +{ + git_str *b = (git_str *)data; + return git_str_put(b, buf, size); +} + +static int type_size_sort(const void *_a, const void *_b) +{ + const git_pobject *a = (git_pobject *)_a; + const git_pobject *b = (git_pobject *)_b; + + if (a->type > b->type) + return -1; + if (a->type < b->type) + return 1; + if (a->hash > b->hash) + return -1; + if (a->hash < b->hash) + return 1; + /* + * TODO + * + if (a->preferred_base > b->preferred_base) + return -1; + if (a->preferred_base < b->preferred_base) + return 1; + */ + if (a->size > b->size) + return -1; + if (a->size < b->size) + return 1; + return a < b ? -1 : (a > b); /* newest first */ +} + +static int delta_cacheable( + git_packbuilder *pb, + size_t src_size, + size_t trg_size, + size_t delta_size) +{ + size_t new_size; + + if (git__add_sizet_overflow(&new_size, pb->delta_cache_size, delta_size)) + return 0; + + if (pb->max_delta_cache_size && new_size > pb->max_delta_cache_size) + return 0; + + if (delta_size < pb->cache_max_small_delta_size) + return 1; + + /* cache delta, if objects are large enough compared to delta size */ + if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10)) + return 1; + + return 0; +} + +static int try_delta(git_packbuilder *pb, struct unpacked *trg, + struct unpacked *src, size_t max_depth, + size_t *mem_usage, int *ret) +{ + git_pobject *trg_object = trg->object; + git_pobject *src_object = src->object; + git_odb_object *obj; + size_t trg_size, src_size, delta_size, sizediff, max_size, sz; + size_t ref_depth; + void *delta_buf; + + /* Don't bother doing diffs between different types */ + if (trg_object->type != src_object->type) { + *ret = -1; + return 0; + } + + *ret = 0; + + /* TODO: support reuse-delta */ + + /* Let's not bust the allowed depth. */ + if (src->depth >= max_depth) + return 0; + + /* Now some size filtering heuristics. */ + trg_size = trg_object->size; + if (!trg_object->delta) { + max_size = trg_size/2 - 20; + ref_depth = 1; + } else { + max_size = trg_object->delta_size; + ref_depth = trg->depth; + } + + max_size = (uint64_t)max_size * (max_depth - src->depth) / + (max_depth - ref_depth + 1); + if (max_size == 0) + return 0; + + src_size = src_object->size; + sizediff = src_size < trg_size ? trg_size - src_size : 0; + if (sizediff >= max_size) + return 0; + if (trg_size < src_size / 32) + return 0; + + /* Load data if not already done */ + if (!trg->data) { + if (git_odb_read(&obj, pb->odb, &trg_object->id) < 0) + return -1; + + sz = git_odb_object_size(obj); + trg->data = git__malloc(sz); + GIT_ERROR_CHECK_ALLOC(trg->data); + memcpy(trg->data, git_odb_object_data(obj), sz); + + git_odb_object_free(obj); + + if (sz != trg_size) { + git_error_set(GIT_ERROR_INVALID, + "inconsistent target object length"); + return -1; + } + + *mem_usage += sz; + } + if (!src->data) { + size_t obj_sz; + + if (git_odb_read(&obj, pb->odb, &src_object->id) < 0 || + !git__is_ulong(obj_sz = git_odb_object_size(obj))) + return -1; + + sz = obj_sz; + src->data = git__malloc(sz); + GIT_ERROR_CHECK_ALLOC(src->data); + memcpy(src->data, git_odb_object_data(obj), sz); + + git_odb_object_free(obj); + + if (sz != src_size) { + git_error_set(GIT_ERROR_INVALID, + "inconsistent source object length"); + return -1; + } + + *mem_usage += sz; + } + if (!src->index) { + if (git_delta_index_init(&src->index, src->data, src_size) < 0) + return 0; /* suboptimal pack - out of memory */ + + *mem_usage += git_delta_index_size(src->index); + } + + if (git_delta_create_from_index(&delta_buf, &delta_size, src->index, trg->data, trg_size, + max_size) < 0) + return 0; + + if (trg_object->delta) { + /* Prefer only shallower same-sized deltas. */ + if (delta_size == trg_object->delta_size && + src->depth + 1 >= trg->depth) { + git__free(delta_buf); + return 0; + } + } + + GIT_ASSERT(git_packbuilder__cache_lock(pb) == 0); + + if (trg_object->delta_data) { + git__free(trg_object->delta_data); + GIT_ASSERT(pb->delta_cache_size >= trg_object->delta_size); + pb->delta_cache_size -= trg_object->delta_size; + trg_object->delta_data = NULL; + } + if (delta_cacheable(pb, src_size, trg_size, delta_size)) { + bool overflow = git__add_sizet_overflow( + &pb->delta_cache_size, pb->delta_cache_size, delta_size); + + GIT_ASSERT(git_packbuilder__cache_unlock(pb) == 0); + + if (overflow) { + git__free(delta_buf); + return -1; + } + + trg_object->delta_data = git__realloc(delta_buf, delta_size); + GIT_ERROR_CHECK_ALLOC(trg_object->delta_data); + } else { + /* create delta when writing the pack */ + GIT_ASSERT(git_packbuilder__cache_unlock(pb) == 0); + git__free(delta_buf); + } + + trg_object->delta = src_object; + trg_object->delta_size = delta_size; + trg->depth = src->depth + 1; + + *ret = 1; + return 0; +} + +static size_t check_delta_limit(git_pobject *me, size_t n) +{ + git_pobject *child = me->delta_child; + size_t m = n; + + while (child) { + size_t c = check_delta_limit(child, n + 1); + if (m < c) + m = c; + child = child->delta_sibling; + } + return m; +} + +static size_t free_unpacked(struct unpacked *n) +{ + size_t freed_mem = 0; + + if (n->index) { + freed_mem += git_delta_index_size(n->index); + git_delta_index_free(n->index); + } + n->index = NULL; + + if (n->data) { + freed_mem += n->object->size; + git__free(n->data); + n->data = NULL; + } + n->object = NULL; + n->depth = 0; + return freed_mem; +} + +static int report_delta_progress( + git_packbuilder *pb, uint32_t count, bool force) +{ + int ret; + + if (pb->failure) + return pb->failure; + + if (pb->progress_cb) { + uint64_t current_time = git_time_monotonic(); + uint64_t elapsed = current_time - pb->last_progress_report_time; + + if (force || elapsed >= MIN_PROGRESS_UPDATE_INTERVAL) { + pb->last_progress_report_time = current_time; + + ret = pb->progress_cb( + GIT_PACKBUILDER_DELTAFICATION, + count, pb->nr_objects, pb->progress_cb_payload); + + if (ret) { + pb->failure = ret; + return git_error_set_after_callback(ret); + } + } + } + + return 0; +} + +static int find_deltas(git_packbuilder *pb, git_pobject **list, + size_t *list_size, size_t window, size_t depth) +{ + git_pobject *po; + git_str zbuf = GIT_STR_INIT; + struct unpacked *array; + size_t idx = 0, count = 0; + size_t mem_usage = 0; + size_t i; + int error = -1; + + array = git__calloc(window, sizeof(struct unpacked)); + GIT_ERROR_CHECK_ALLOC(array); + + for (;;) { + struct unpacked *n = array + idx; + size_t max_depth, j, best_base = SIZE_MAX; + + GIT_ASSERT(git_packbuilder__progress_lock(pb) == 0); + if (!*list_size) { + GIT_ASSERT(git_packbuilder__progress_unlock(pb) == 0); + break; + } + + pb->nr_deltified += 1; + if ((error = report_delta_progress(pb, pb->nr_deltified, false)) < 0) { + GIT_ASSERT(git_packbuilder__progress_unlock(pb) == 0); + goto on_error; + } + + po = *list++; + (*list_size)--; + GIT_ASSERT(git_packbuilder__progress_unlock(pb) == 0); + + mem_usage -= free_unpacked(n); + n->object = po; + + while (pb->window_memory_limit && + mem_usage > pb->window_memory_limit && + count > 1) { + size_t tail = (idx + window - count) % window; + mem_usage -= free_unpacked(array + tail); + count--; + } + + /* + * If the current object is at pack edge, take the depth the + * objects that depend on the current object into account + * otherwise they would become too deep. + */ + max_depth = depth; + if (po->delta_child) { + size_t delta_limit = check_delta_limit(po, 0); + + if (delta_limit > max_depth) + goto next; + + max_depth -= delta_limit; + } + + j = window; + while (--j > 0) { + int ret; + size_t other_idx = idx + j; + struct unpacked *m; + + if (other_idx >= window) + other_idx -= window; + + m = array + other_idx; + if (!m->object) + break; + + if (try_delta(pb, n, m, max_depth, &mem_usage, &ret) < 0) + goto on_error; + if (ret < 0) + break; + else if (ret > 0) + best_base = other_idx; + } + + /* + * If we decided to cache the delta data, then it is best + * to compress it right away. First because we have to do + * it anyway, and doing it here while we're threaded will + * save a lot of time in the non threaded write phase, + * as well as allow for caching more deltas within + * the same cache size limit. + * ... + * But only if not writing to stdout, since in that case + * the network is most likely throttling writes anyway, + * and therefore it is best to go to the write phase ASAP + * instead, as we can afford spending more time compressing + * between writes at that moment. + */ + if (po->delta_data) { + if (git_zstream_deflatebuf(&zbuf, po->delta_data, po->delta_size) < 0) + goto on_error; + + git__free(po->delta_data); + po->delta_data = git__malloc(zbuf.size); + GIT_ERROR_CHECK_ALLOC(po->delta_data); + + memcpy(po->delta_data, zbuf.ptr, zbuf.size); + po->z_delta_size = zbuf.size; + git_str_clear(&zbuf); + + GIT_ASSERT(git_packbuilder__cache_lock(pb) == 0); + pb->delta_cache_size -= po->delta_size; + pb->delta_cache_size += po->z_delta_size; + GIT_ASSERT(git_packbuilder__cache_unlock(pb) == 0); + } + + /* + * If we made n a delta, and if n is already at max + * depth, leaving it in the window is pointless. we + * should evict it first. + */ + if (po->delta && max_depth <= n->depth) + continue; + + /* + * Move the best delta base up in the window, after the + * currently deltified object, to keep it longer. It will + * be the first base object to be attempted next. + */ + if (po->delta) { + struct unpacked swap = array[best_base]; + size_t dist = (window + idx - best_base) % window; + size_t dst = best_base; + while (dist--) { + size_t src = (dst + 1) % window; + array[dst] = array[src]; + dst = src; + } + array[dst] = swap; + } + + next: + idx++; + if (count + 1 < window) + count++; + if (idx >= window) + idx = 0; + } + error = 0; + +on_error: + for (i = 0; i < window; ++i) { + git__free(array[i].index); + git__free(array[i].data); + } + git__free(array); + git_str_dispose(&zbuf); + + return error; +} + +#ifdef GIT_THREADS + +struct thread_params { + git_thread thread; + git_packbuilder *pb; + + git_pobject **list; + + git_cond cond; + git_mutex mutex; + + size_t list_size; + size_t remaining; + + size_t window; + size_t depth; + size_t working; + size_t data_ready; + + /* A pb->progress_cb can stop the packing process by returning an error. + When that happens, all threads observe the error and stop voluntarily. */ + bool stopped; +}; + +static void *threaded_find_deltas(void *arg) +{ + struct thread_params *me = arg; + + while (me->remaining) { + if (find_deltas(me->pb, me->list, &me->remaining, + me->window, me->depth) < 0) { + me->stopped = true; + GIT_ASSERT_WITH_RETVAL(git_packbuilder__progress_lock(me->pb) == 0, NULL); + me->working = false; + git_cond_signal(&me->pb->progress_cond); + GIT_ASSERT_WITH_RETVAL(git_packbuilder__progress_unlock(me->pb) == 0, NULL); + return NULL; + } + + GIT_ASSERT_WITH_RETVAL(git_packbuilder__progress_lock(me->pb) == 0, NULL); + me->working = 0; + git_cond_signal(&me->pb->progress_cond); + GIT_ASSERT_WITH_RETVAL(git_packbuilder__progress_unlock(me->pb) == 0, NULL); + + if (git_mutex_lock(&me->mutex)) { + git_error_set(GIT_ERROR_THREAD, "unable to lock packfile condition mutex"); + return NULL; + } + + while (!me->data_ready) + git_cond_wait(&me->cond, &me->mutex); + + /* + * We must not set ->data_ready before we wait on the + * condition because the main thread may have set it to 1 + * before we get here. In order to be sure that new + * work is available if we see 1 in ->data_ready, it + * was initialized to 0 before this thread was spawned + * and we reset it to 0 right away. + */ + me->data_ready = 0; + git_mutex_unlock(&me->mutex); + } + /* leave ->working 1 so that this doesn't get more work assigned */ + return NULL; +} + +static int ll_find_deltas(git_packbuilder *pb, git_pobject **list, + size_t list_size, size_t window, size_t depth) +{ + struct thread_params *p; + size_t i; + int ret, active_threads = 0; + + if (!pb->nr_threads) + pb->nr_threads = git__online_cpus(); + + if (pb->nr_threads <= 1) { + return find_deltas(pb, list, &list_size, window, depth); + } + + p = git__mallocarray(pb->nr_threads, sizeof(*p)); + GIT_ERROR_CHECK_ALLOC(p); + + /* Partition the work among the threads */ + for (i = 0; i < pb->nr_threads; ++i) { + size_t sub_size = list_size / (pb->nr_threads - i); + + /* don't use too small segments or no deltas will be found */ + if (sub_size < 2*window && i+1 < pb->nr_threads) + sub_size = 0; + + p[i].pb = pb; + p[i].window = window; + p[i].depth = depth; + p[i].working = 1; + p[i].data_ready = 0; + p[i].stopped = 0; + + /* try to split chunks on "path" boundaries */ + while (sub_size && sub_size < list_size && + list[sub_size]->hash && + list[sub_size]->hash == list[sub_size-1]->hash) + sub_size++; + + p[i].list = list; + p[i].list_size = sub_size; + p[i].remaining = sub_size; + + list += sub_size; + list_size -= sub_size; + } + + /* Start work threads */ + for (i = 0; i < pb->nr_threads; ++i) { + if (!p[i].list_size) + continue; + + git_mutex_init(&p[i].mutex); + git_cond_init(&p[i].cond); + + ret = git_thread_create(&p[i].thread, + threaded_find_deltas, &p[i]); + if (ret) { + git_error_set(GIT_ERROR_THREAD, "unable to create thread"); + return -1; + } + active_threads++; + } + + /* + * Now let's wait for work completion. Each time a thread is done + * with its work, we steal half of the remaining work from the + * thread with the largest number of unprocessed objects and give + * it to that newly idle thread. This ensure good load balancing + * until the remaining object list segments are simply too short + * to be worth splitting anymore. + */ + while (active_threads) { + struct thread_params *target = NULL; + struct thread_params *victim = NULL; + size_t sub_size = 0; + + /* Start by locating a thread that has transitioned its + * 'working' flag from 1 -> 0. This indicates that it is + * ready to receive more work using our work-stealing + * algorithm. */ + GIT_ASSERT(git_packbuilder__progress_lock(pb) == 0); + for (;;) { + for (i = 0; !target && i < pb->nr_threads; i++) + if (!p[i].working) + target = &p[i]; + if (target) + break; + git_cond_wait(&pb->progress_cond, &pb->progress_mutex); + } + + /* At this point we hold the progress lock and have located + * a thread to receive more work. We still need to locate a + * thread from which to steal work (the victim). */ + for (i = 0; i < pb->nr_threads; i++) + if (p[i].remaining > 2*window && + (!victim || victim->remaining < p[i].remaining)) + victim = &p[i]; + + if (victim && !target->stopped) { + sub_size = victim->remaining / 2; + list = victim->list + victim->list_size - sub_size; + while (sub_size && list[0]->hash && + list[0]->hash == list[-1]->hash) { + list++; + sub_size--; + } + if (!sub_size) { + /* + * It is possible for some "paths" to have + * so many objects that no hash boundary + * might be found. Let's just steal the + * exact half in that case. + */ + sub_size = victim->remaining / 2; + list -= sub_size; + } + target->list = list; + victim->list_size -= sub_size; + victim->remaining -= sub_size; + } + target->list_size = sub_size; + target->remaining = sub_size; + target->working = 1; /* even when target->stopped, so that we don't process this thread again */ + GIT_ASSERT(git_packbuilder__progress_unlock(pb) == 0); + + if (git_mutex_lock(&target->mutex)) { + git_error_set(GIT_ERROR_THREAD, "unable to lock packfile condition mutex"); + git__free(p); + return -1; + } + + target->data_ready = 1; + git_cond_signal(&target->cond); + git_mutex_unlock(&target->mutex); + + if (target->stopped || !sub_size) { + git_thread_join(&target->thread, NULL); + git_cond_free(&target->cond); + git_mutex_free(&target->mutex); + active_threads--; + } + } + + git__free(p); + return pb->failure; +} + +#else +#define ll_find_deltas(pb, l, ls, w, d) find_deltas(pb, l, &ls, w, d) +#endif + +int git_packbuilder__prepare(git_packbuilder *pb) +{ + git_pobject **delta_list; + size_t i, n = 0; + int error; + + if (pb->nr_objects == 0 || pb->done) + return 0; /* nothing to do */ + + /* + * Although we do not report progress during deltafication, we + * at least report that we are in the deltafication stage + */ + if (pb->progress_cb) { + if ((error = pb->progress_cb(GIT_PACKBUILDER_DELTAFICATION, 0, pb->nr_objects, pb->progress_cb_payload)) < 0) + return git_error_set_after_callback(error); + } + + delta_list = git__mallocarray(pb->nr_objects, sizeof(*delta_list)); + GIT_ERROR_CHECK_ALLOC(delta_list); + + for (i = 0; i < pb->nr_objects; ++i) { + git_pobject *po = pb->object_list + i; + + /* Make sure the item is within our size limits */ + if (po->size < 50 || po->size > pb->big_file_threshold) + continue; + + delta_list[n++] = po; + } + + if (n > 1) { + git__tsort((void **)delta_list, n, type_size_sort); + if ((error = ll_find_deltas(pb, delta_list, n, + GIT_PACK_WINDOW + 1, + GIT_PACK_DEPTH)) < 0) { + git__free(delta_list); + return error; + } + } + + error = report_delta_progress(pb, pb->nr_objects, true); + + pb->done = true; + git__free(delta_list); + return error; +} + +#define PREPARE_PACK error = git_packbuilder__prepare(pb); if (error < 0) { return error; } + +int git_packbuilder_foreach(git_packbuilder *pb, int (*cb)(void *buf, size_t size, void *payload), void *payload) +{ + int error; + PREPARE_PACK; + return write_pack(pb, cb, payload); +} + +int git_packbuilder__write_buf(git_str *buf, git_packbuilder *pb) +{ + int error; + PREPARE_PACK; + + return write_pack(pb, &write_pack_buf, buf); +} + +int git_packbuilder_write_buf(git_buf *buf, git_packbuilder *pb) +{ + GIT_BUF_WRAP_PRIVATE(buf, git_packbuilder__write_buf, pb); +} + +static int write_cb(void *buf, size_t len, void *payload) +{ + struct pack_write_context *ctx = payload; + return git_indexer_append(ctx->indexer, buf, len, ctx->stats); +} + +int git_packbuilder_write( + git_packbuilder *pb, + const char *path, + unsigned int mode, + git_indexer_progress_cb progress_cb, + void *progress_cb_payload) +{ + int error = -1; + git_str object_path = GIT_STR_INIT; + git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT; + git_indexer *indexer = NULL; + git_indexer_progress stats; + struct pack_write_context ctx; + int t; + + PREPARE_PACK; + + if (path == NULL) { + if ((error = git_repository__item_path(&object_path, pb->repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0) + goto cleanup; + if ((error = git_str_joinpath(&object_path, git_str_cstr(&object_path), "pack")) < 0) + goto cleanup; + path = git_str_cstr(&object_path); + } + + opts.progress_cb = progress_cb; + opts.progress_cb_payload = progress_cb_payload; + +#ifdef GIT_EXPERIMENTAL_SHA256 + opts.mode = mode; + opts.odb = pb->odb; + opts.oid_type = pb->oid_type; + + error = git_indexer_new(&indexer, path, &opts); +#else + error = git_indexer_new(&indexer, path, mode, pb->odb, &opts); +#endif + + if (error < 0) + goto cleanup; + + if (!git_repository__configmap_lookup(&t, pb->repo, GIT_CONFIGMAP_FSYNCOBJECTFILES) && t) + git_indexer__set_fsync(indexer, 1); + + ctx.indexer = indexer; + ctx.stats = &stats; + + if ((error = git_packbuilder_foreach(pb, write_cb, &ctx)) < 0) + goto cleanup; + + if ((error = git_indexer_commit(indexer, &stats)) < 0) + goto cleanup; + +#ifndef GIT_DEPRECATE_HARD + git_oid_cpy(&pb->pack_oid, git_indexer_hash(indexer)); +#endif + + pb->pack_name = git__strdup(git_indexer_name(indexer)); + GIT_ERROR_CHECK_ALLOC(pb->pack_name); + +cleanup: + git_indexer_free(indexer); + git_str_dispose(&object_path); + return error; +} + +#undef PREPARE_PACK + +#ifndef GIT_DEPRECATE_HARD +const git_oid *git_packbuilder_hash(git_packbuilder *pb) +{ + return &pb->pack_oid; +} +#endif + +const char *git_packbuilder_name(git_packbuilder *pb) +{ + return pb->pack_name; +} + + +static int cb_tree_walk( + const char *root, const git_tree_entry *entry, void *payload) +{ + int error; + struct tree_walk_context *ctx = payload; + + /* A commit inside a tree represents a submodule commit and should be skipped. */ + if (git_tree_entry_type(entry) == GIT_OBJECT_COMMIT) + return 0; + + if (!(error = git_str_sets(&ctx->buf, root)) && + !(error = git_str_puts(&ctx->buf, git_tree_entry_name(entry)))) + error = git_packbuilder_insert( + ctx->pb, git_tree_entry_id(entry), git_str_cstr(&ctx->buf)); + + return error; +} + +int git_packbuilder_insert_commit(git_packbuilder *pb, const git_oid *oid) +{ + git_commit *commit; + + if (git_commit_lookup(&commit, pb->repo, oid) < 0 || + git_packbuilder_insert(pb, oid, NULL) < 0) + return -1; + + if (git_packbuilder_insert_tree(pb, git_commit_tree_id(commit)) < 0) + return -1; + + git_commit_free(commit); + return 0; +} + +int git_packbuilder_insert_tree(git_packbuilder *pb, const git_oid *oid) +{ + int error; + git_tree *tree = NULL; + struct tree_walk_context context = { pb, GIT_STR_INIT }; + + if (!(error = git_tree_lookup(&tree, pb->repo, oid)) && + !(error = git_packbuilder_insert(pb, oid, NULL))) + error = git_tree_walk(tree, GIT_TREEWALK_PRE, cb_tree_walk, &context); + + git_tree_free(tree); + git_str_dispose(&context.buf); + return error; +} + +int git_packbuilder_insert_recur(git_packbuilder *pb, const git_oid *id, const char *name) +{ + git_object *obj; + int error; + + GIT_ASSERT_ARG(pb); + GIT_ASSERT_ARG(id); + + if ((error = git_object_lookup(&obj, pb->repo, id, GIT_OBJECT_ANY)) < 0) + return error; + + switch (git_object_type(obj)) { + case GIT_OBJECT_BLOB: + error = git_packbuilder_insert(pb, id, name); + break; + case GIT_OBJECT_TREE: + error = git_packbuilder_insert_tree(pb, id); + break; + case GIT_OBJECT_COMMIT: + error = git_packbuilder_insert_commit(pb, id); + break; + case GIT_OBJECT_TAG: + if ((error = git_packbuilder_insert(pb, id, name)) < 0) + goto cleanup; + error = git_packbuilder_insert_recur(pb, git_tag_target_id((git_tag *) obj), NULL); + break; + + default: + git_error_set(GIT_ERROR_INVALID, "unknown object type"); + error = -1; + } + +cleanup: + git_object_free(obj); + return error; +} + +size_t git_packbuilder_object_count(git_packbuilder *pb) +{ + return pb->nr_objects; +} + +size_t git_packbuilder_written(git_packbuilder *pb) +{ + return pb->nr_written; +} + +static int lookup_walk_object(struct walk_object **out, git_packbuilder *pb, const git_oid *id) +{ + struct walk_object *obj; + + obj = git_pool_mallocz(&pb->object_pool, 1); + if (!obj) { + git_error_set_oom(); + return -1; + } + + git_oid_cpy(&obj->id, id); + + *out = obj; + return 0; +} + +static int retrieve_object(struct walk_object **out, git_packbuilder *pb, const git_oid *id) +{ + struct walk_object *obj; + int error; + + error = git_packbuilder_walk_objectmap_get(&obj, &pb->walk_objects, id); + + if (error == GIT_ENOTFOUND) { + if ((error = lookup_walk_object(&obj, pb, id)) < 0) + return error; + + if ((error = git_packbuilder_walk_objectmap_put(&pb->walk_objects, &obj->id, obj)) < 0) + return error; + } else if (error != 0) { + return error; + } + + *out = obj; + return 0; +} + +static int mark_blob_uninteresting(git_packbuilder *pb, const git_oid *id) +{ + int error; + struct walk_object *obj; + + if ((error = retrieve_object(&obj, pb, id)) < 0) + return error; + + obj->uninteresting = 1; + + return 0; +} + +static int mark_tree_uninteresting(git_packbuilder *pb, const git_oid *id) +{ + struct walk_object *obj; + git_tree *tree; + int error; + size_t i; + + if ((error = retrieve_object(&obj, pb, id)) < 0) + return error; + + if (obj->uninteresting) + return 0; + + obj->uninteresting = 1; + + if ((error = git_tree_lookup(&tree, pb->repo, id)) < 0) + return error; + + for (i = 0; i < git_tree_entrycount(tree); i++) { + const git_tree_entry *entry = git_tree_entry_byindex(tree, i); + const git_oid *entry_id = git_tree_entry_id(entry); + switch (git_tree_entry_type(entry)) { + case GIT_OBJECT_TREE: + if ((error = mark_tree_uninteresting(pb, entry_id)) < 0) + goto cleanup; + break; + case GIT_OBJECT_BLOB: + if ((error = mark_blob_uninteresting(pb, entry_id)) < 0) + goto cleanup; + break; + default: + /* it's a submodule or something unknown, we don't want it */ + ; + } + } + +cleanup: + git_tree_free(tree); + return error; +} + +/* + * Mark the edges of the graph uninteresting. Since we start from a + * git_revwalk, the commits are already uninteresting, but we need to + * mark the trees and blobs. + */ +static int mark_edges_uninteresting(git_packbuilder *pb, git_commit_list *commits) +{ + int error; + git_commit_list *list; + git_commit *commit; + + for (list = commits; list; list = list->next) { + if (!list->item->uninteresting) + continue; + + if ((error = git_commit_lookup(&commit, pb->repo, &list->item->oid)) < 0) + return error; + + error = mark_tree_uninteresting(pb, git_commit_tree_id(commit)); + git_commit_free(commit); + + if (error < 0) + return error; + } + + return 0; +} + +static int pack_objects_insert_tree(git_packbuilder *pb, git_tree *tree) +{ + size_t i; + int error; + git_tree *subtree; + struct walk_object *obj; + const char *name; + + if ((error = retrieve_object(&obj, pb, git_tree_id(tree))) < 0) + return error; + + if (obj->seen || obj->uninteresting) + return 0; + + obj->seen = 1; + + if ((error = git_packbuilder_insert(pb, &obj->id, NULL))) + return error; + + for (i = 0; i < git_tree_entrycount(tree); i++) { + const git_tree_entry *entry = git_tree_entry_byindex(tree, i); + const git_oid *entry_id = git_tree_entry_id(entry); + switch (git_tree_entry_type(entry)) { + case GIT_OBJECT_TREE: + if ((error = git_tree_lookup(&subtree, pb->repo, entry_id)) < 0) + return error; + + error = pack_objects_insert_tree(pb, subtree); + git_tree_free(subtree); + + if (error < 0) + return error; + + break; + case GIT_OBJECT_BLOB: + if ((error = retrieve_object(&obj, pb, entry_id)) < 0) + return error; + if (obj->uninteresting) + continue; + name = git_tree_entry_name(entry); + if ((error = git_packbuilder_insert(pb, entry_id, name)) < 0) + return error; + break; + default: + /* it's a submodule or something unknown, we don't want it */ + ; + } + } + + + return error; +} + +static int pack_objects_insert_commit(git_packbuilder *pb, struct walk_object *obj) +{ + int error; + git_commit *commit = NULL; + git_tree *tree = NULL; + + obj->seen = 1; + + if ((error = git_packbuilder_insert(pb, &obj->id, NULL)) < 0) + return error; + + if ((error = git_commit_lookup(&commit, pb->repo, &obj->id)) < 0) + return error; + + if ((error = git_tree_lookup(&tree, pb->repo, git_commit_tree_id(commit))) < 0) + goto cleanup; + + if ((error = pack_objects_insert_tree(pb, tree)) < 0) + goto cleanup; + +cleanup: + git_commit_free(commit); + git_tree_free(tree); + return error; +} + +int git_packbuilder_insert_walk(git_packbuilder *pb, git_revwalk *walk) +{ + int error; + git_oid id; + struct walk_object *obj; + + GIT_ASSERT_ARG(pb); + GIT_ASSERT_ARG(walk); + + if ((error = mark_edges_uninteresting(pb, walk->user_input)) < 0) + return error; + + /* + * TODO: git marks the parents of the edges + * uninteresting. This may provide a speed advantage, but does + * seem to assume the remote does not have a single-commit + * history on the other end. + */ + + /* walk down each tree up to the blobs and insert them, stopping when uninteresting */ + while ((error = git_revwalk_next(&id, walk)) == 0) { + if ((error = retrieve_object(&obj, pb, &id)) < 0) + return error; + + if (obj->seen || obj->uninteresting) + continue; + + if ((error = pack_objects_insert_commit(pb, obj)) < 0) + return error; + } + + if (error == GIT_ITEROVER) + error = 0; + + return error; +} + +int git_packbuilder_set_callbacks(git_packbuilder *pb, git_packbuilder_progress progress_cb, void *progress_cb_payload) +{ + if (!pb) + return -1; + + pb->progress_cb = progress_cb; + pb->progress_cb_payload = progress_cb_payload; + + return 0; +} + +void git_packbuilder_free(git_packbuilder *pb) +{ + if (pb == NULL) + return; + +#ifdef GIT_THREADS + + git_mutex_free(&pb->cache_mutex); + git_mutex_free(&pb->progress_mutex); + git_cond_free(&pb->progress_cond); + +#endif + + if (pb->odb) + git_odb_free(pb->odb); + + git_packbuilder_pobjectmap_dispose(&pb->object_ix); + + if (pb->object_list) + git__free(pb->object_list); + + git_packbuilder_walk_objectmap_dispose(&pb->walk_objects); + git_pool_clear(&pb->object_pool); + + git_hash_ctx_cleanup(&pb->ctx); + git_zstream_free(&pb->zstream); + + git__free(pb->pack_name); + + git__free(pb); +} diff --git a/src/libgit2/pack-objects.h b/src/libgit2/pack-objects.h new file mode 100644 index 00000000000..ad04fb0abc6 --- /dev/null +++ b/src/libgit2/pack-objects.h @@ -0,0 +1,117 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_pack_objects_h__ +#define INCLUDE_pack_objects_h__ + +#include "common.h" + +#include "str.h" +#include "hash.h" +#include "zstream.h" +#include "pool.h" +#include "indexer.h" +#include "hashmap_oid.h" + +#include "git2/oid.h" +#include "git2/pack.h" + +#define GIT_PACK_WINDOW 10 /* number of objects to possibly delta against */ +#define GIT_PACK_DEPTH 50 /* max delta depth */ +#define GIT_PACK_DELTA_CACHE_SIZE (256 * 1024 * 1024) +#define GIT_PACK_DELTA_CACHE_LIMIT 1000 +#define GIT_PACK_BIG_FILE_THRESHOLD (512 * 1024 * 1024) + +typedef struct git_pobject { + git_oid id; + git_object_t type; + off64_t offset; + + size_t size; + + unsigned int hash; /* name hint hash */ + + struct git_pobject *delta; /* delta base object */ + struct git_pobject *delta_child; /* deltified objects who bases me */ + struct git_pobject *delta_sibling; /* other deltified objects + * who uses the same base as + * me */ + + void *delta_data; + size_t delta_size; + size_t z_delta_size; + + unsigned int written:1, + recursing:1, + tagged:1, + filled:1; +} git_pobject; + +typedef struct walk_object walk_object; + +GIT_HASHMAP_OID_STRUCT(git_packbuilder_pobjectmap, git_pobject *); +GIT_HASHMAP_OID_STRUCT(git_packbuilder_walk_objectmap, walk_object *); + +struct git_packbuilder { + git_repository *repo; /* associated repository */ + git_odb *odb; /* associated object database */ + + git_oid_t oid_type; + + git_hash_ctx ctx; + git_zstream zstream; + + uint32_t nr_objects, + nr_deltified, + nr_written, + nr_remaining; + + size_t nr_alloc; + + git_pobject *object_list; + + git_packbuilder_pobjectmap object_ix; + git_packbuilder_walk_objectmap walk_objects; + git_pool object_pool; + +#ifndef GIT_DEPRECATE_HARD + git_oid pack_oid; /* hash of written pack */ +#endif + char *pack_name; /* name of written pack */ + + /* synchronization objects */ + git_mutex cache_mutex; + git_mutex progress_mutex; + git_cond progress_cond; + + /* configs */ + size_t delta_cache_size; + size_t max_delta_cache_size; + size_t cache_max_small_delta_size; + size_t big_file_threshold; + size_t window_memory_limit; + + unsigned int nr_threads; /* nr of threads to use */ + + git_packbuilder_progress progress_cb; + void *progress_cb_payload; + + /* the time progress was last reported, in millisecond ticks */ + uint64_t last_progress_report_time; + + bool done; + + /* A non-zero error code in failure causes all threads to shut themselves + down. Some functions will return this error code. */ + volatile int failure; +}; + +int git_packbuilder__write_buf(git_str *buf, git_packbuilder *pb); +int git_packbuilder__prepare(git_packbuilder *pb); + + +#endif diff --git a/src/libgit2/pack.c b/src/libgit2/pack.c new file mode 100644 index 00000000000..a70975a75bf --- /dev/null +++ b/src/libgit2/pack.c @@ -0,0 +1,1664 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "pack.h" + +#include "delta.h" +#include "futils.h" +#include "mwindow.h" +#include "odb.h" +#include "oid.h" +#include "oidarray.h" +#include "hashmap_oid.h" + +/* Option to bypass checking existence of '.keep' files */ +bool git_disable_pack_keep_file_checks = false; + +static int packfile_open_locked(struct git_pack_file *p); +static off64_t nth_packed_object_offset_locked(struct git_pack_file *p, uint32_t n); +static int packfile_unpack_compressed( + git_rawobj *obj, + struct git_pack_file *p, + git_mwindow **w_curs, + off64_t *curpos, + size_t size, + git_object_t type); + +/* Can find the offset of an object given + * a prefix of an identifier. + * Throws GIT_EAMBIGUOUSOIDPREFIX if short oid + * is ambiguous within the pack. + * This method assumes that len is between + * GIT_OID_MINPREFIXLEN and the oid type's hexsize. + */ +static int pack_entry_find_offset( + off64_t *offset_out, + git_oid *found_oid, + struct git_pack_file *p, + const git_oid *short_oid, + size_t len); + +#define off64_hash(key) (uint32_t)((key)>>33^(key)^(key)<<11) +#define off64_equal(a, b) ((a) == (b)) + +GIT_HASHMAP_FUNCTIONS(git_pack_offsetmap, GIT_HASHMAP_INLINE, off64_t, git_pack_cache_entry *, off64_hash, off64_equal); +GIT_HASHMAP_OID_FUNCTIONS(git_pack_oidmap, , struct git_pack_entry *); + +static int packfile_error(const char *message) +{ + git_error_set(GIT_ERROR_ODB, "invalid pack file - %s", message); + return -1; +} + +/******************** + * Delta base cache + ********************/ + +static git_pack_cache_entry *new_cache_object(git_rawobj *source) +{ + git_pack_cache_entry *e = git__calloc(1, sizeof(git_pack_cache_entry)); + if (!e) + return NULL; + + git_atomic32_inc(&e->refcount); + memcpy(&e->raw, source, sizeof(git_rawobj)); + + return e; +} + +static void free_cache_object(void *o) +{ + git_pack_cache_entry *e = (git_pack_cache_entry *)o; + + if (e != NULL) { + git__free(e->raw.data); + git__free(e); + } +} + +static void cache_free(git_pack_cache *cache) +{ + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + git_pack_cache_entry *entry; + + while (git_pack_offsetmap_iterate(&iter, NULL, &entry, &cache->entries) == 0) + free_cache_object(entry); + + git_pack_offsetmap_dispose(&cache->entries); +} + +static int cache_init(git_pack_cache *cache) +{ + cache->memory_limit = GIT_PACK_CACHE_MEMORY_LIMIT; + + if (git_mutex_init(&cache->lock)) { + git_error_set(GIT_ERROR_OS, "failed to initialize pack cache mutex"); + return -1; + } + + return 0; +} + +static git_pack_cache_entry *cache_get(git_pack_cache *cache, off64_t offset) +{ + git_pack_cache_entry *entry = NULL; + + if (git_mutex_lock(&cache->lock) < 0) + return NULL; + + if (git_pack_offsetmap_get(&entry, &cache->entries, offset) == 0) { + git_atomic32_inc(&entry->refcount); + entry->last_usage = cache->use_ctr++; + } + + git_mutex_unlock(&cache->lock); + + return entry; +} + +/* Run with the cache lock held */ +static void free_lowest_entry(git_pack_cache *cache) +{ + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + git_pack_cache_entry *entry; + off64_t offset; + + while (git_pack_offsetmap_iterate(&iter, &offset, &entry, &cache->entries) == 0) { + if (entry && git_atomic32_get(&entry->refcount) == 0) { + cache->memory_used -= entry->raw.len; + git_pack_offsetmap_remove(&cache->entries, offset); + free_cache_object(entry); + } + } +} + +static int cache_add( + git_pack_cache_entry **cached_out, + git_pack_cache *cache, + git_rawobj *base, + off64_t offset) +{ + git_pack_cache_entry *entry; + int exists; + + if (base->len > GIT_PACK_CACHE_SIZE_LIMIT) + return -1; + + entry = new_cache_object(base); + if (entry) { + if (git_mutex_lock(&cache->lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock cache"); + git__free(entry); + return -1; + } + /* Add it to the cache if nobody else has */ + exists = git_pack_offsetmap_contains(&cache->entries, offset); + if (!exists) { + while (cache->memory_used + base->len > cache->memory_limit) + free_lowest_entry(cache); + + git_pack_offsetmap_put(&cache->entries, offset, entry); + cache->memory_used += entry->raw.len; + + *cached_out = entry; + } + git_mutex_unlock(&cache->lock); + /* Somebody beat us to adding it into the cache */ + if (exists) { + git__free(entry); + return -1; + } + } + + return 0; +} + +/*********************************************************** + * + * PACK INDEX METHODS + * + ***********************************************************/ + +static void pack_index_free(struct git_pack_file *p) +{ + if (p->ids) { + git__free(p->ids); + p->ids = NULL; + } + if (p->index_map.data) { + git_futils_mmap_free(&p->index_map); + p->index_map.data = NULL; + } +} + +/* Run with the packfile lock held */ +static int pack_index_check_locked(const char *path, struct git_pack_file *p) +{ + struct git_pack_idx_header *hdr; + uint32_t version, nr = 0, i, *index; + void *idx_map; + size_t idx_size; + struct stat st; + int error; + + /* TODO: properly open the file without access time using O_NOATIME */ + git_file fd = git_futils_open_ro(path); + if (fd < 0) + return fd; + + if (p_fstat(fd, &st) < 0) { + p_close(fd); + git_error_set(GIT_ERROR_OS, "unable to stat pack index '%s'", path); + return -1; + } + + if (!S_ISREG(st.st_mode) || + !git__is_sizet(st.st_size) || + (idx_size = (size_t)st.st_size) < (size_t)((4 * 256) + (p->oid_size * 2))) { + p_close(fd); + git_error_set(GIT_ERROR_ODB, "invalid pack index '%s'", path); + return -1; + } + + error = git_futils_mmap_ro(&p->index_map, fd, 0, idx_size); + + p_close(fd); + + if (error < 0) + return error; + + hdr = idx_map = p->index_map.data; + + if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) { + version = ntohl(hdr->idx_version); + + if (version < 2 || version > 2) { + git_futils_mmap_free(&p->index_map); + return packfile_error("unsupported index version"); + } + + } else { + version = 1; + } + + index = idx_map; + + if (version > 1) + index += 2; /* skip index header */ + + for (i = 0; i < 256; i++) { + uint32_t n = ntohl(index[i]); + if (n < nr) { + git_futils_mmap_free(&p->index_map); + return packfile_error("index is non-monotonic"); + } + nr = n; + } + + if (version == 1) { + /* + * Total size: + * - 256 index entries 4 bytes each + * - 24/36-byte entries * nr (20/32 byte SHA + 4-byte offset) + * - 20/32-byte SHA of the packfile + * - 20/32-byte SHA file checksum + */ + if (idx_size != (4 * 256 + ((uint64_t) nr * (p->oid_size + 4)) + (p->oid_size * 2))) { + git_futils_mmap_free(&p->index_map); + return packfile_error("index is corrupted"); + } + } else if (version == 2) { + /* + * Minimum size: + * - 8 bytes of header + * - 256 index entries 4 bytes each + * - 20/32-byte SHA entry * nr + * - 4-byte crc entry * nr + * - 4-byte offset entry * nr + * - 20/32-byte SHA of the packfile + * - 20/32-byte SHA file checksum + * And after the 4-byte offset table might be a + * variable sized table containing 8-byte entries + * for offsets larger than 2^31. + */ + uint64_t min_size = 8 + (4 * 256) + ((uint64_t)nr * (p->oid_size + 4 + 4)) + (p->oid_size * 2); + uint64_t max_size = min_size; + + if (nr) + max_size += (nr - 1)*8; + + if (idx_size < min_size || idx_size > max_size) { + git_futils_mmap_free(&p->index_map); + return packfile_error("wrong index size"); + } + } + + p->num_objects = nr; + p->index_version = version; + return 0; +} + +/* Run with the packfile lock held */ +static int pack_index_open_locked(struct git_pack_file *p) +{ + int error = 0; + size_t name_len; + git_str idx_name = GIT_STR_INIT; + + if (p->index_version > -1) + goto cleanup; + + /* checked by git_pack_file alloc */ + name_len = strlen(p->pack_name); + GIT_ASSERT(name_len > strlen(".pack")); + + if ((error = git_str_init(&idx_name, name_len)) < 0) + goto cleanup; + + git_str_put(&idx_name, p->pack_name, name_len - strlen(".pack")); + git_str_puts(&idx_name, ".idx"); + if (git_str_oom(&idx_name)) { + error = -1; + goto cleanup; + } + + if (p->index_version == -1) + error = pack_index_check_locked(idx_name.ptr, p); + +cleanup: + git_str_dispose(&idx_name); + + return error; +} + +static unsigned char *pack_window_open( + struct git_pack_file *p, + git_mwindow **w_cursor, + off64_t offset, + unsigned int *left) +{ + unsigned char *pack_data = NULL; + + if (git_mutex_lock(&p->lock) < 0) { + git_error_set(GIT_ERROR_THREAD, "unable to lock packfile"); + return NULL; + } + if (git_mutex_lock(&p->mwf.lock) < 0) { + git_mutex_unlock(&p->lock); + git_error_set(GIT_ERROR_THREAD, "unable to lock packfile"); + return NULL; + } + + if (p->mwf.fd == -1 && packfile_open_locked(p) < 0) + goto cleanup; + + /* Since packfiles end in a hash of their content and it's + * pointless to ask for an offset into the middle of that + * hash, and the pack_window_contains function above wouldn't match + * don't allow an offset too close to the end of the file. + * + * Don't allow a negative offset, as that means we've wrapped + * around. + */ + if (offset > (p->mwf.size - p->oid_size)) + goto cleanup; + if (offset < 0) + goto cleanup; + + pack_data = git_mwindow_open(&p->mwf, w_cursor, offset, p->oid_size, left); + +cleanup: + git_mutex_unlock(&p->mwf.lock); + git_mutex_unlock(&p->lock); + return pack_data; + } + +/* + * The per-object header is a pretty dense thing, which is + * - first byte: low four bits are "size", + * then three bits of "type", + * with the high bit being "size continues". + * - each byte afterwards: low seven bits are size continuation, + * with the high bit being "size continues" + */ +int git_packfile__object_header(size_t *out, unsigned char *hdr, size_t size, git_object_t type) +{ + unsigned char *hdr_base; + unsigned char c; + + GIT_ASSERT_ARG(type >= GIT_OBJECT_COMMIT && type <= GIT_PACKFILE_REF_DELTA); + + /* TODO: add support for chunked objects; see git.git 6c0d19b1 */ + + c = (unsigned char)((type << 4) | (size & 15)); + size >>= 4; + hdr_base = hdr; + + while (size) { + *hdr++ = c | 0x80; + c = size & 0x7f; + size >>= 7; + } + *hdr++ = c; + + *out = (hdr - hdr_base); + return 0; +} + + +static int packfile_unpack_header1( + unsigned long *usedp, + size_t *sizep, + git_object_t *type, + const unsigned char *buf, + unsigned long len) +{ + unsigned shift; + unsigned long size, c; + unsigned long used = 0; + + c = buf[used++]; + *type = (c >> 4) & 7; + size = c & 15; + shift = 4; + while (c & 0x80) { + if (len <= used) { + git_error_set(GIT_ERROR_ODB, "buffer too small"); + return GIT_EBUFS; + } + + if (bitsizeof(long) <= shift) { + *usedp = 0; + git_error_set(GIT_ERROR_ODB, "packfile corrupted"); + return -1; + } + + c = buf[used++]; + size += (c & 0x7f) << shift; + shift += 7; + } + + *sizep = (size_t)size; + *usedp = used; + return 0; +} + +int git_packfile_unpack_header( + size_t *size_p, + git_object_t *type_p, + struct git_pack_file *p, + git_mwindow **w_curs, + off64_t *curpos) +{ + unsigned char *base; + unsigned int left; + unsigned long used; + int error; + + if ((error = git_mutex_lock(&p->lock)) < 0) + return error; + if ((error = git_mutex_lock(&p->mwf.lock)) < 0) { + git_mutex_unlock(&p->lock); + return error; + } + + if (p->mwf.fd == -1 && (error = packfile_open_locked(p)) < 0) { + git_mutex_unlock(&p->lock); + git_mutex_unlock(&p->mwf.lock); + return error; + } + + /* pack_window_open() assures us we have [base, base + oid_size) + * available as a range that we can look at at. (It's actually + * the hash size that is assured.) With our object header + * encoding the maximum deflated object size is 2^137, which is + * just insane, so we know won't exceed what we have been given. + */ + base = git_mwindow_open(&p->mwf, w_curs, *curpos, p->oid_size, &left); + git_mutex_unlock(&p->lock); + git_mutex_unlock(&p->mwf.lock); + if (base == NULL) + return GIT_EBUFS; + + error = packfile_unpack_header1(&used, size_p, type_p, base, left); + git_mwindow_close(w_curs); + if (error == GIT_EBUFS) + return error; + else if (error < 0) + return packfile_error("header length is zero"); + + *curpos += used; + return 0; +} + +int git_packfile_resolve_header( + size_t *size_p, + git_object_t *type_p, + struct git_pack_file *p, + off64_t offset) +{ + git_mwindow *w_curs = NULL; + off64_t curpos = offset; + size_t size; + git_object_t type; + off64_t base_offset; + int error; + + error = git_mutex_lock(&p->lock); + if (error < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock packfile reader"); + return error; + } + error = git_mutex_lock(&p->mwf.lock); + if (error < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock packfile reader"); + git_mutex_unlock(&p->lock); + return error; + } + + if (p->mwf.fd == -1 && (error = packfile_open_locked(p)) < 0) { + git_mutex_unlock(&p->mwf.lock); + git_mutex_unlock(&p->lock); + return error; + } + git_mutex_unlock(&p->mwf.lock); + git_mutex_unlock(&p->lock); + + error = git_packfile_unpack_header(&size, &type, p, &w_curs, &curpos); + if (error < 0) + return error; + + if (type == GIT_PACKFILE_OFS_DELTA || type == GIT_PACKFILE_REF_DELTA) { + size_t base_size; + git_packfile_stream stream; + + error = get_delta_base(&base_offset, p, &w_curs, &curpos, type, offset); + git_mwindow_close(&w_curs); + + if (error < 0) + return error; + + if ((error = git_packfile_stream_open(&stream, p, curpos)) < 0) + return error; + error = git_delta_read_header_fromstream(&base_size, size_p, &stream); + git_packfile_stream_dispose(&stream); + if (error < 0) + return error; + } else { + *size_p = size; + base_offset = 0; + } + + while (type == GIT_PACKFILE_OFS_DELTA || type == GIT_PACKFILE_REF_DELTA) { + curpos = base_offset; + error = git_packfile_unpack_header(&size, &type, p, &w_curs, &curpos); + if (error < 0) + return error; + if (type != GIT_PACKFILE_OFS_DELTA && type != GIT_PACKFILE_REF_DELTA) + break; + + error = get_delta_base(&base_offset, p, &w_curs, &curpos, type, base_offset); + git_mwindow_close(&w_curs); + + if (error < 0) + return error; + } + *type_p = type; + + return error; +} + +#define SMALL_STACK_SIZE 64 + +/** + * Generate the chain of dependencies which we need to get to the + * object at `off`. `chain` is used a stack, popping gives the right + * order to apply deltas on. If an object is found in the pack's base + * cache, we stop calculating there. + */ +static int pack_dependency_chain(git_dependency_chain *chain_out, + git_pack_cache_entry **cached_out, off64_t *cached_off, + struct pack_chain_elem *small_stack, size_t *stack_sz, + struct git_pack_file *p, off64_t obj_offset) +{ + git_dependency_chain chain = GIT_ARRAY_INIT; + git_mwindow *w_curs = NULL; + off64_t curpos = obj_offset, base_offset; + int error = 0, use_heap = 0; + size_t size, elem_pos; + git_object_t type; + + elem_pos = 0; + while (true) { + struct pack_chain_elem *elem; + git_pack_cache_entry *cached = NULL; + + /* if we have a base cached, we can stop here instead */ + if ((cached = cache_get(&p->bases, obj_offset)) != NULL) { + *cached_out = cached; + *cached_off = obj_offset; + break; + } + + /* if we run out of space on the small stack, use the array */ + if (elem_pos == SMALL_STACK_SIZE) { + git_array_init_to_size(chain, elem_pos); + GIT_ERROR_CHECK_ARRAY(chain); + memcpy(chain.ptr, small_stack, elem_pos * sizeof(struct pack_chain_elem)); + chain.size = elem_pos; + use_heap = 1; + } + + curpos = obj_offset; + if (!use_heap) { + elem = &small_stack[elem_pos]; + } else { + elem = git_array_alloc(chain); + if (!elem) { + error = -1; + goto on_error; + } + } + + elem->base_key = obj_offset; + + error = git_packfile_unpack_header(&size, &type, p, &w_curs, &curpos); + if (error < 0) + goto on_error; + + elem->offset = curpos; + elem->size = size; + elem->type = type; + elem->base_key = obj_offset; + + if (type != GIT_PACKFILE_OFS_DELTA && type != GIT_PACKFILE_REF_DELTA) + break; + + error = get_delta_base(&base_offset, p, &w_curs, &curpos, type, obj_offset); + git_mwindow_close(&w_curs); + + if (error < 0) + goto on_error; + + /* we need to pass the pos *after* the delta-base bit */ + elem->offset = curpos; + + /* go through the loop again, but with the new object */ + obj_offset = base_offset; + elem_pos++; + } + + + *stack_sz = elem_pos + 1; + *chain_out = chain; + return error; + +on_error: + git_array_clear(chain); + return error; +} + +int git_packfile_unpack( + git_rawobj *obj, + struct git_pack_file *p, + off64_t *obj_offset) +{ + git_mwindow *w_curs = NULL; + off64_t curpos = *obj_offset; + int error, free_base = 0; + git_dependency_chain chain = GIT_ARRAY_INIT; + struct pack_chain_elem *elem = NULL, *stack; + git_pack_cache_entry *cached = NULL; + struct pack_chain_elem small_stack[SMALL_STACK_SIZE]; + size_t stack_size = 0, elem_pos, alloclen; + int base_type; + + error = git_mutex_lock(&p->lock); + if (error < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock packfile reader"); + return error; + } + error = git_mutex_lock(&p->mwf.lock); + if (error < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock packfile reader"); + git_mutex_unlock(&p->lock); + return error; + } + + if (p->mwf.fd == -1) + error = packfile_open_locked(p); + git_mutex_unlock(&p->mwf.lock); + git_mutex_unlock(&p->lock); + if (error < 0) + return error; + + /* + * TODO: optionally check the CRC on the packfile + */ + + error = pack_dependency_chain(&chain, &cached, obj_offset, small_stack, &stack_size, p, *obj_offset); + if (error < 0) + return error; + + obj->data = NULL; + obj->len = 0; + obj->type = GIT_OBJECT_INVALID; + + /* let's point to the right stack */ + stack = chain.ptr ? chain.ptr : small_stack; + + elem_pos = stack_size; + if (cached) { + memcpy(obj, &cached->raw, sizeof(git_rawobj)); + base_type = obj->type; + elem_pos--; /* stack_size includes the base, which isn't actually there */ + } else { + elem = &stack[--elem_pos]; + base_type = elem->type; + } + + switch (base_type) { + case GIT_OBJECT_COMMIT: + case GIT_OBJECT_TREE: + case GIT_OBJECT_BLOB: + case GIT_OBJECT_TAG: + if (!cached) { + curpos = elem->offset; + error = packfile_unpack_compressed(obj, p, &w_curs, &curpos, elem->size, elem->type); + git_mwindow_close(&w_curs); + base_type = elem->type; + } + if (error < 0) + goto cleanup; + break; + case GIT_PACKFILE_OFS_DELTA: + case GIT_PACKFILE_REF_DELTA: + error = packfile_error("dependency chain ends in a delta"); + goto cleanup; + default: + error = packfile_error("invalid packfile type in header"); + goto cleanup; + } + + /* + * Finding the object we want a cached base element is + * problematic, as we need to make sure we don't accidentally + * give the caller the cached object, which it would then feel + * free to free, so we need to copy the data. + */ + if (cached && stack_size == 1) { + void *data = obj->data; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, obj->len, 1); + obj->data = git__malloc(alloclen); + GIT_ERROR_CHECK_ALLOC(obj->data); + + memcpy(obj->data, data, obj->len + 1); + git_atomic32_dec(&cached->refcount); + goto cleanup; + } + + /* we now apply each consecutive delta until we run out */ + while (elem_pos > 0 && !error) { + git_rawobj base, delta; + + /* + * We can now try to add the base to the cache, as + * long as it's not already the cached one. + */ + if (!cached) + free_base = !!cache_add(&cached, &p->bases, obj, elem->base_key); + + elem = &stack[elem_pos - 1]; + curpos = elem->offset; + error = packfile_unpack_compressed(&delta, p, &w_curs, &curpos, elem->size, elem->type); + git_mwindow_close(&w_curs); + + if (error < 0) { + /* We have transferred ownership of the data to the cache. */ + obj->data = NULL; + break; + } + + /* the current object becomes the new base, on which we apply the delta */ + base = *obj; + obj->data = NULL; + obj->len = 0; + obj->type = GIT_OBJECT_INVALID; + + error = git_delta_apply(&obj->data, &obj->len, base.data, base.len, delta.data, delta.len); + obj->type = base_type; + + /* + * We usually don't want to free the base at this + * point, as we put it into the cache in the previous + * iteration. free_base lets us know that we got the + * base object directly from the packfile, so we can free it. + */ + git__free(delta.data); + if (free_base) { + free_base = 0; + git__free(base.data); + } + + if (cached) { + git_atomic32_dec(&cached->refcount); + cached = NULL; + } + + if (error < 0) + break; + + elem_pos--; + } + +cleanup: + if (error < 0) { + git__free(obj->data); + if (cached) + git_atomic32_dec(&cached->refcount); + } + + if (elem) + *obj_offset = curpos; + + git_array_clear(chain); + return error; +} + +int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, off64_t curpos) +{ + memset(obj, 0, sizeof(git_packfile_stream)); + obj->curpos = curpos; + obj->p = p; + + if (git_zstream_init(&obj->zstream, GIT_ZSTREAM_INFLATE) < 0) { + git_error_set(GIT_ERROR_ZLIB, "failed to init packfile stream"); + return -1; + } + + return 0; +} + +ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len) +{ + unsigned int window_len; + unsigned char *in; + int error; + + if (obj->done) + return 0; + + if ((in = pack_window_open(obj->p, &obj->mw, obj->curpos, &window_len)) == NULL) + return GIT_EBUFS; + + if ((error = git_zstream_set_input(&obj->zstream, in, window_len)) < 0 || + (error = git_zstream_get_output_chunk(buffer, &len, &obj->zstream)) < 0) { + git_mwindow_close(&obj->mw); + git_error_set(GIT_ERROR_ZLIB, "error reading from the zlib stream"); + return -1; + } + + git_mwindow_close(&obj->mw); + + obj->curpos += window_len - obj->zstream.in_len; + + if (git_zstream_eos(&obj->zstream)) + obj->done = 1; + + /* If we didn't write anything out but we're not done, we need more data */ + if (!len && !git_zstream_eos(&obj->zstream)) + return GIT_EBUFS; + + return len; + +} + +void git_packfile_stream_dispose(git_packfile_stream *obj) +{ + git_zstream_free(&obj->zstream); +} + +static int packfile_unpack_compressed( + git_rawobj *obj, + struct git_pack_file *p, + git_mwindow **mwindow, + off64_t *position, + size_t size, + git_object_t type) +{ + git_zstream zstream = GIT_ZSTREAM_INIT; + size_t buffer_len, total = 0; + char *data = NULL; + int error; + + GIT_ERROR_CHECK_ALLOC_ADD(&buffer_len, size, 1); + data = git__calloc(1, buffer_len); + GIT_ERROR_CHECK_ALLOC(data); + + if ((error = git_zstream_init(&zstream, GIT_ZSTREAM_INFLATE)) < 0) { + git_error_set(GIT_ERROR_ZLIB, "failed to init zlib stream on unpack"); + goto out; + } + + do { + size_t bytes = buffer_len - total; + unsigned int window_len, consumed; + unsigned char *in; + + if ((in = pack_window_open(p, mwindow, *position, &window_len)) == NULL) { + error = -1; + goto out; + } + + if ((error = git_zstream_set_input(&zstream, in, window_len)) < 0 || + (error = git_zstream_get_output_chunk(data + total, &bytes, &zstream)) < 0) { + git_mwindow_close(mwindow); + goto out; + } + + git_mwindow_close(mwindow); + + consumed = window_len - (unsigned int)zstream.in_len; + + if (!bytes && !consumed) { + git_error_set(GIT_ERROR_ZLIB, "error inflating zlib stream"); + error = -1; + goto out; + } + + *position += consumed; + total += bytes; + } while (!git_zstream_eos(&zstream)); + + if (total != size || !git_zstream_eos(&zstream)) { + git_error_set(GIT_ERROR_ZLIB, "error inflating zlib stream"); + error = -1; + goto out; + } + + obj->type = type; + obj->len = size; + obj->data = data; + +out: + git_zstream_free(&zstream); + if (error) + git__free(data); + + return error; +} + +/* + * curpos is where the data starts, delta_obj_offset is the where the + * header starts + */ +int get_delta_base( + off64_t *delta_base_out, + struct git_pack_file *p, + git_mwindow **w_curs, + off64_t *curpos, + git_object_t type, + off64_t delta_obj_offset) +{ + unsigned int left = 0; + unsigned char *base_info; + off64_t base_offset; + git_oid unused; + + GIT_ASSERT_ARG(delta_base_out); + + base_info = pack_window_open(p, w_curs, *curpos, &left); + /* Assumption: the only reason this would fail is because the file is too small */ + if (base_info == NULL) + return GIT_EBUFS; + /* pack_window_open() assured us we have + * [base_info, base_info + oid_size) as a range that we can look + * at without walking off the end of the mapped window. Its + * actually the hash size that is assured. An OFS_DELTA longer + * than the hash size is stupid, as then a REF_DELTA would be + * smaller to store. + */ + if (type == GIT_PACKFILE_OFS_DELTA) { + unsigned used = 0; + unsigned char c = base_info[used++]; + size_t unsigned_base_offset = c & 127; + while (c & 128) { + if (left <= used) + return GIT_EBUFS; + unsigned_base_offset += 1; + if (!unsigned_base_offset || MSB(unsigned_base_offset, 7)) + return packfile_error("overflow"); + c = base_info[used++]; + unsigned_base_offset = (unsigned_base_offset << 7) + (c & 127); + } + if (unsigned_base_offset == 0 || (size_t)delta_obj_offset <= unsigned_base_offset) + return packfile_error("out of bounds"); + base_offset = delta_obj_offset - unsigned_base_offset; + *curpos += used; + } else if (type == GIT_PACKFILE_REF_DELTA) { + git_oid base_oid; + git_oid_from_raw(&base_oid, base_info, p->oid_type); + + /* If we have the cooperative cache, search in it first */ + if (p->has_cache) { + struct git_pack_entry *entry; + + if (git_pack_oidmap_get(&entry, &p->idx_cache, &base_oid) == 0) { + if (entry->offset == 0) + return packfile_error("delta offset is zero"); + + *curpos += p->oid_size; + *delta_base_out = entry->offset; + return 0; + } else { + /* If we're building an index, don't try to find the pack + * entry; we just haven't seen it yet. We'll make + * progress again in the next loop. + */ + return GIT_PASSTHROUGH; + } + } + + /* The base entry _must_ be in the same pack */ + if (pack_entry_find_offset(&base_offset, &unused, p, &base_oid, p->oid_hexsize) < 0) + return packfile_error("base entry delta is not in the same pack"); + *curpos += p->oid_size; + } else + return packfile_error("unknown object type"); + + if (base_offset == 0) + return packfile_error("delta offset is zero"); + + *delta_base_out = base_offset; + return 0; +} + +/*********************************************************** + * + * PACKFILE METHODS + * + ***********************************************************/ + +void git_packfile_free(struct git_pack_file *p, bool unlink_packfile) +{ + bool locked = true; + + if (!p) + return; + + cache_free(&p->bases); + + if (git_mutex_lock(&p->lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock packfile"); + locked = false; + } + if (p->mwf.fd >= 0) { + git_mwindow_free_all(&p->mwf); + p_close(p->mwf.fd); + p->mwf.fd = -1; + } + if (locked) + git_mutex_unlock(&p->lock); + + if (unlink_packfile) + p_unlink(p->pack_name); + + pack_index_free(p); + + git__free(p->bad_object_ids); + + git_mutex_free(&p->bases.lock); + git_mutex_free(&p->mwf.lock); + git_mutex_free(&p->lock); + git__free(p); +} + +/* Run with the packfile and mwf locks held */ +static int packfile_open_locked(struct git_pack_file *p) +{ + struct stat st; + struct git_pack_header hdr; + unsigned char checksum[GIT_OID_MAX_SIZE]; + unsigned char *idx_checksum; + + if (pack_index_open_locked(p) < 0) + return git_odb__error_notfound("failed to open packfile", NULL, 0); + + if (p->mwf.fd >= 0) + return 0; + + /* TODO: open with noatime */ + p->mwf.fd = git_futils_open_ro(p->pack_name); + if (p->mwf.fd < 0) + goto cleanup; + + if (p_fstat(p->mwf.fd, &st) < 0) { + git_error_set(GIT_ERROR_OS, "could not stat packfile"); + goto cleanup; + } + + /* If we created the struct before we had the pack we lack size. */ + if (!p->mwf.size) { + if (!S_ISREG(st.st_mode)) + goto cleanup; + p->mwf.size = (off64_t)st.st_size; + } else if (p->mwf.size != st.st_size) + goto cleanup; + +#if 0 + /* We leave these file descriptors open with sliding mmap; + * there is no point keeping them open across exec(), though. + */ + fd_flag = fcntl(p->mwf.fd, F_GETFD, 0); + if (fd_flag < 0) + goto cleanup; + + fd_flag |= FD_CLOEXEC; + if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1) + goto cleanup; +#endif + + /* Verify we recognize this pack file format. */ + if (p_read(p->mwf.fd, &hdr, sizeof(hdr)) < 0 || + hdr.hdr_signature != htonl(PACK_SIGNATURE) || + !pack_version_ok(hdr.hdr_version)) + goto cleanup; + + /* Verify the pack matches its index. */ + if (p->num_objects != ntohl(hdr.hdr_entries) || + p_pread(p->mwf.fd, checksum, p->oid_size, p->mwf.size - p->oid_size) < 0) + goto cleanup; + + idx_checksum = ((unsigned char *)p->index_map.data) + + p->index_map.len - (p->oid_size * 2); + + if (git_oid_raw_cmp(checksum, idx_checksum, p->oid_size) != 0) + goto cleanup; + + if (git_mwindow_file_register(&p->mwf) < 0) + goto cleanup; + + return 0; + +cleanup: + git_error_set(GIT_ERROR_OS, "invalid packfile '%s'", p->pack_name); + + if (p->mwf.fd >= 0) + p_close(p->mwf.fd); + p->mwf.fd = -1; + + return -1; +} + +int git_packfile__name(char **out, const char *path) +{ + size_t path_len; + git_str buf = GIT_STR_INIT; + + path_len = strlen(path); + + if (path_len < strlen(".idx")) + return git_odb__error_notfound("invalid packfile path", NULL, 0); + + if (git_str_printf(&buf, "%.*s.pack", (int)(path_len - strlen(".idx")), path) < 0) + return -1; + + *out = git_str_detach(&buf); + return 0; +} + +int git_packfile_alloc( + struct git_pack_file **pack_out, + const char *path, + git_oid_t oid_type) +{ + struct stat st; + struct git_pack_file *p; + size_t path_len = path ? strlen(path) : 0, alloc_len; + + *pack_out = NULL; + + if (path_len < strlen(".idx")) + return git_odb__error_notfound("invalid packfile path", NULL, 0); + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, sizeof(*p), path_len); + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2); + + p = git__calloc(1, alloc_len); + GIT_ERROR_CHECK_ALLOC(p); + + memcpy(p->pack_name, path, path_len + 1); + + /* + * Make sure a corresponding .pack file exists and that + * the index looks sane. + */ + if (git__suffixcmp(path, ".idx") == 0) { + size_t root_len = path_len - strlen(".idx"); + + if (!git_disable_pack_keep_file_checks) { + memcpy(p->pack_name + root_len, ".keep", sizeof(".keep")); + if (git_fs_path_exists(p->pack_name) == true) + p->pack_keep = 1; + } + + memcpy(p->pack_name + root_len, ".pack", sizeof(".pack")); + } + + if (p_stat(p->pack_name, &st) < 0 || !S_ISREG(st.st_mode)) { + git__free(p); + return git_odb__error_notfound("packfile not found", NULL, 0); + } + + /* ok, it looks sane as far as we can check without + * actually mapping the pack file. + */ + p->mwf.fd = -1; + p->mwf.size = st.st_size; + p->pack_local = 1; + p->mtime = (git_time_t)st.st_mtime; + p->index_version = -1; + p->oid_type = oid_type ? oid_type : GIT_OID_DEFAULT; + p->oid_size = (unsigned int)git_oid_size(p->oid_type); + p->oid_hexsize = (unsigned int)git_oid_hexsize(p->oid_type); + + if (git_mutex_init(&p->lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to initialize packfile mutex"); + git__free(p); + return -1; + } + + if (git_mutex_init(&p->mwf.lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to initialize packfile window mutex"); + git_mutex_free(&p->lock); + git__free(p); + return -1; + } + + if (cache_init(&p->bases) < 0) { + git_mutex_free(&p->mwf.lock); + git_mutex_free(&p->lock); + git__free(p); + return -1; + } + + *pack_out = p; + + return 0; +} + +/*********************************************************** + * + * PACKFILE ENTRY SEARCH INTERNALS + * + ***********************************************************/ + +static off64_t nth_packed_object_offset_locked(struct git_pack_file *p, uint32_t n) +{ + const unsigned char *index, *end; + uint32_t off32; + + index = p->index_map.data; + end = index + p->index_map.len; + index += 4 * 256; + if (p->index_version == 1) + return ntohl(*((uint32_t *)(index + (p->oid_size + 4) * (size_t) n))); + + index += 8 + (size_t) p->num_objects * (p->oid_size + 4); + off32 = ntohl(*((uint32_t *)(index + 4 * n))); + if (!(off32 & 0x80000000)) + return off32; + index += (size_t) p->num_objects * 4 + (off32 & 0x7fffffff) * 8; + + /* Make sure we're not being sent out of bounds */ + if (index >= end - 8) + return -1; + + return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) | + ntohl(*((uint32_t *)(index + 4))); +} + +static int git__memcmp4(const void *a, const void *b) { + return memcmp(a, b, 4); +} + +int git_pack_foreach_entry( + struct git_pack_file *p, + git_odb_foreach_cb cb, + void *data) +{ + const unsigned char *index, *current; + uint32_t i; + int error = 0; + git_array_oid_t oids = GIT_ARRAY_INIT; + git_oid *oid; + + if (git_mutex_lock(&p->lock) < 0) + return packfile_error("failed to get lock for git_pack_foreach_entry"); + + if ((error = pack_index_open_locked(p)) < 0) { + git_mutex_unlock(&p->lock); + return error; + } + + if (!p->index_map.data) { + git_error_set(GIT_ERROR_INTERNAL, "internal error: p->index_map.data == NULL"); + git_mutex_unlock(&p->lock); + return -1; + } + + index = p->index_map.data; + + if (p->index_version > 1) + index += 8; + + index += 4 * 256; + + if (p->ids == NULL) { + git_vector offsets, oids; + + if ((error = git_vector_init(&oids, p->num_objects, NULL))) { + git_mutex_unlock(&p->lock); + return error; + } + + if ((error = git_vector_init(&offsets, p->num_objects, git__memcmp4))) { + git_mutex_unlock(&p->lock); + return error; + } + + if (p->index_version > 1) { + const unsigned char *off = index + + (p->oid_size + 4) * p->num_objects; + + for (i = 0; i < p->num_objects; i++) + git_vector_insert(&offsets, (void*)&off[4 * i]); + + git_vector_sort(&offsets); + git_vector_foreach(&offsets, i, current) + git_vector_insert(&oids, (void*)&index[5 * (current - off)]); + } else { + for (i = 0; i < p->num_objects; i++) + git_vector_insert(&offsets, (void*)&index[(p->oid_size + 4) * i]); + git_vector_sort(&offsets); + git_vector_foreach(&offsets, i, current) + git_vector_insert(&oids, (void*)¤t[4]); + } + + git_vector_dispose(&offsets); + p->ids = (unsigned char **)git_vector_detach(NULL, NULL, &oids); + } + + /* + * We need to copy the OIDs to another array before we + * relinquish the lock to avoid races. We can also take + * this opportunity to put them into normal form. + */ + git_array_init_to_size(oids, p->num_objects); + if (!oids.ptr) { + git_mutex_unlock(&p->lock); + git_array_clear(oids); + GIT_ERROR_CHECK_ARRAY(oids); + } + for (i = 0; i < p->num_objects; i++) { + oid = git_array_alloc(oids); + if (!oid) { + git_mutex_unlock(&p->lock); + git_array_clear(oids); + GIT_ERROR_CHECK_ALLOC(oid); + } + git_oid_from_raw(oid, p->ids[i], p->oid_type); + } + + git_mutex_unlock(&p->lock); + + git_array_foreach(oids, i, oid) { + if ((error = cb(oid, data)) != 0) { + git_error_set_after_callback(error); + break; + } + } + + git_array_clear(oids); + return error; +} + +int git_pack_foreach_entry_offset( + struct git_pack_file *p, + git_pack_foreach_entry_offset_cb cb, + void *data) +{ + const unsigned char *index; + off64_t current_offset; + git_oid current_oid; + uint32_t i; + int error = 0; + + if (git_mutex_lock(&p->lock) < 0) + return packfile_error("failed to get lock for git_pack_foreach_entry_offset"); + + index = p->index_map.data; + if (index == NULL) { + if ((error = pack_index_open_locked(p)) < 0) + goto cleanup; + + if (!p->index_map.data) { + git_error_set(GIT_ERROR_INTERNAL, "internal error: p->index_map.data == NULL"); + goto cleanup; + } + + index = p->index_map.data; + } + + if (p->index_version > 1) + index += 8; + + index += 4 * 256; + + /* all offsets should have been validated by pack_index_check_locked */ + if (p->index_version > 1) { + const unsigned char *offsets = index + + (p->oid_size + 4) * p->num_objects; + const unsigned char *large_offset_ptr; + const unsigned char *large_offsets = index + + (p->oid_size + 8) * p->num_objects; + const unsigned char *large_offsets_end = ((const unsigned char *)p->index_map.data) + p->index_map.len - p->oid_size; + + for (i = 0; i < p->num_objects; i++) { + current_offset = ntohl(*(const uint32_t *)(offsets + 4 * i)); + if (current_offset & 0x80000000) { + large_offset_ptr = large_offsets + (current_offset & 0x7fffffff) * 8; + if (large_offset_ptr >= large_offsets_end) { + error = packfile_error("invalid large offset"); + goto cleanup; + } + current_offset = (((off64_t)ntohl(*((uint32_t *)(large_offset_ptr + 0)))) << 32) | + ntohl(*((uint32_t *)(large_offset_ptr + 4))); + } + + git_oid_from_raw(¤t_oid, (index + p->oid_size * i), p->oid_type); + if ((error = cb(¤t_oid, current_offset, data)) != 0) { + error = git_error_set_after_callback(error); + goto cleanup; + } + } + } else { + for (i = 0; i < p->num_objects; i++) { + current_offset = ntohl(*(const uint32_t *)(index + (p->oid_size + 4) * i)); + git_oid_from_raw(¤t_oid, (index + (p->oid_size + 4) * i + 4), p->oid_type); + if ((error = cb(¤t_oid, current_offset, data)) != 0) { + error = git_error_set_after_callback(error); + goto cleanup; + } + } + } + +cleanup: + git_mutex_unlock(&p->lock); + return error; +} + +int git_pack__lookup_id( + const void *oid_lookup_table, + size_t stride, + unsigned lo, + unsigned hi, + const unsigned char *oid_prefix, + const git_oid_t oid_type) +{ + const unsigned char *base = oid_lookup_table; + size_t oid_size = git_oid_size(oid_type); + + while (lo < hi) { + unsigned mi = (lo + hi) / 2; + int cmp = git_oid_raw_cmp(base + mi * stride, oid_prefix, oid_size); + + if (!cmp) + return mi; + + if (cmp > 0) + hi = mi; + else + lo = mi+1; + } + + return -((int)lo)-1; +} + +static int pack_entry_find_offset( + off64_t *offset_out, + git_oid *found_oid, + struct git_pack_file *p, + const git_oid *short_oid, + size_t len) +{ + const uint32_t *level1_ofs; + size_t ofs_delta = 0; + const unsigned char *index; + unsigned hi, lo, stride; + int pos, found = 0; + off64_t offset; + const unsigned char *current = 0; + int error = 0; + + *offset_out = 0; + + if (git_mutex_lock(&p->lock) < 0) + return packfile_error("failed to get lock for pack_entry_find_offset"); + + if ((error = pack_index_open_locked(p)) < 0) + goto cleanup; + + if (!p->index_map.data) { + git_error_set(GIT_ERROR_INTERNAL, "internal error: p->index_map.data == NULL"); + goto cleanup; + } + + index = p->index_map.data; + level1_ofs = p->index_map.data; + + if (p->index_version > 1) { + level1_ofs += 2; + ofs_delta = 2; + index += 8; + } + + if ((size_t)short_oid->id[0] + ofs_delta >= p->index_map.len) { + git_error_set(GIT_ERROR_INTERNAL, "internal error: p->short_oid->[0] out of bounds"); + goto cleanup; + } + + index += 4 * 256; + hi = ntohl(level1_ofs[(int)short_oid->id[0]]); + lo = ((short_oid->id[0] == 0x0) ? 0 : ntohl(level1_ofs[(int)short_oid->id[0] - 1])); + + if (p->index_version > 1) { + stride = p->oid_size; + } else { + stride = p->oid_size + 4; + index += 4; + } + +#ifdef INDEX_DEBUG_LOOKUP + printf("%02x%02x%02x... lo %u hi %u nr %d\n", + short_oid->id[0], short_oid->id[1], short_oid->id[2], lo, hi, p->num_objects); +#endif + + pos = git_pack__lookup_id(index, stride, lo, hi, + short_oid->id, p->oid_type); + + if (pos >= 0) { + /* An object matching exactly the oid was found */ + found = 1; + current = index + pos * stride; + } else { + /* No object was found */ + /* pos refers to the object with the "closest" oid to short_oid */ + pos = - 1 - pos; + if (pos < (int)p->num_objects) { + current = index + pos * stride; + + if (!git_oid_raw_ncmp(short_oid->id, current, len)) + found = 1; + } + } + + if (found && + len != p->oid_hexsize && + pos + 1 < (int)p->num_objects) { + /* Check for ambiguousity */ + const unsigned char *next = current + stride; + + if (!git_oid_raw_ncmp(short_oid->id, next, len)) { + found = 2; + } + } + + if (!found) { + error = git_odb__error_notfound("failed to find offset for pack entry", short_oid, len); + goto cleanup; + } + if (found > 1) { + error = git_odb__error_ambiguous("found multiple offsets for pack entry"); + goto cleanup; + } + + if ((offset = nth_packed_object_offset_locked(p, pos)) < 0) { + git_error_set(GIT_ERROR_ODB, "packfile index is corrupt"); + error = -1; + goto cleanup; + } + + *offset_out = offset; + git_oid_from_raw(found_oid, current, p->oid_type); + +#ifdef INDEX_DEBUG_LOOKUP + { + char hex_sha1[p->oid_hexsize + 1]; + git_oid_fmt(hex_sha1, found_oid); + hex_sha1[p->oid_hexsize] = '\0'; + printf("found lo=%d %s\n", lo, hex_sha1); + } +#endif + +cleanup: + git_mutex_unlock(&p->lock); + return error; +} + +int git_pack_entry_find( + struct git_pack_entry *e, + struct git_pack_file *p, + const git_oid *short_oid, + size_t len) +{ + off64_t offset; + git_oid found_oid; + int error; + + GIT_ASSERT_ARG(p); + + if (len == p->oid_hexsize && p->num_bad_objects) { + unsigned i; + for (i = 0; i < p->num_bad_objects; i++) + if (git_oid__cmp(short_oid, &p->bad_object_ids[i]) == 0) + return packfile_error("bad object found in packfile"); + } + + error = pack_entry_find_offset(&offset, &found_oid, p, short_oid, len); + if (error < 0) + return error; + + error = git_mutex_lock(&p->lock); + if (error < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock packfile reader"); + return error; + } + error = git_mutex_lock(&p->mwf.lock); + if (error < 0) { + git_mutex_unlock(&p->lock); + git_error_set(GIT_ERROR_OS, "failed to lock packfile reader"); + return error; + } + + /* we found a unique entry in the index; + * make sure the packfile backing the index + * still exists on disk */ + if (p->mwf.fd == -1) + error = packfile_open_locked(p); + git_mutex_unlock(&p->mwf.lock); + git_mutex_unlock(&p->lock); + if (error < 0) + return error; + + e->offset = offset; + e->p = p; + + git_oid_cpy(&e->id, &found_oid); + return 0; +} diff --git a/src/libgit2/pack.h b/src/libgit2/pack.h new file mode 100644 index 00000000000..e802d60747c --- /dev/null +++ b/src/libgit2/pack.h @@ -0,0 +1,223 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_pack_h__ +#define INCLUDE_pack_h__ + +#include "common.h" + +#include "git2/oid.h" + +#include "array.h" +#include "map.h" +#include "mwindow.h" +#include "odb.h" +#include "zstream.h" +#include "oid.h" +#include "hashmap_oid.h" + +/** + * Function type for callbacks from git_pack_foreach_entry_offset. + */ +typedef int git_pack_foreach_entry_offset_cb( + const git_oid *id, + off64_t offset, + void *payload); + +#define GIT_PACK_FILE_MODE 0444 + +#define PACK_SIGNATURE 0x5041434b /* "PACK" */ +#define PACK_VERSION 2 +#define pack_version_ok(v) ((v) == htonl(2)) + +#define GIT_PACKFILE_OFS_DELTA 6 +#define GIT_PACKFILE_REF_DELTA 7 + +struct git_pack_header { + uint32_t hdr_signature; + uint32_t hdr_version; + uint32_t hdr_entries; +}; + +/* + * The first four bytes of index formats later than version 1 should + * start with this signature, as all older git binaries would find this + * value illegal and abort reading the file. + * + * This is the case because the number of objects in a packfile + * cannot exceed 1,431,660,000 as every object would need at least + * 3 bytes of data and the overall packfile cannot exceed 4 GiB with + * version 1 of the index file due to the offsets limited to 32 bits. + * Clearly the signature exceeds this maximum. + * + * Very old git binaries will also compare the first 4 bytes to the + * next 4 bytes in the index and abort with a "non-monotonic index" + * error if the second 4 byte word is smaller than the first 4 + * byte word. This would be true in the proposed future index + * format as idx_signature would be greater than idx_version. + */ + +#define PACK_IDX_SIGNATURE 0xff744f63 /* "\377tOc" */ + +struct git_pack_idx_header { + uint32_t idx_signature; + uint32_t idx_version; +}; + +typedef struct git_pack_cache_entry { + size_t last_usage; /* enough? */ + git_atomic32 refcount; + git_rawobj raw; +} git_pack_cache_entry; + +struct pack_chain_elem { + off64_t base_key; + off64_t offset; + size_t size; + git_object_t type; +}; + +typedef git_array_t(struct pack_chain_elem) git_dependency_chain; + +#define GIT_PACK_CACHE_MEMORY_LIMIT 16 * 1024 * 1024 +#define GIT_PACK_CACHE_SIZE_LIMIT 1024 * 1024 /* don't bother caching anything over 1MB */ + +struct git_pack_entry { + off64_t offset; + git_oid id; + struct git_pack_file *p; +}; + +GIT_HASHMAP_STRUCT(git_pack_offsetmap, off64_t, git_pack_cache_entry *); + +GIT_HASHMAP_OID_STRUCT(git_pack_oidmap, struct git_pack_entry *); +GIT_HASHMAP_OID_PROTOTYPES(git_pack_oidmap, struct git_pack_entry *); + +typedef struct { + size_t memory_used; + size_t memory_limit; + size_t use_ctr; + git_mutex lock; + git_pack_offsetmap entries; +} git_pack_cache; + +struct git_pack_file { + git_mwindow_file mwf; + git_map index_map; + git_mutex lock; /* protect updates to index_map */ + git_atomic32 refcount; + + uint32_t num_objects; + uint32_t num_bad_objects; + git_oid *bad_object_ids; /* array of git_oid */ + + git_oid_t oid_type; + unsigned oid_hexsize:7, + oid_size:6, + pack_local:1, + pack_keep:1, + has_cache:1; + + int index_version; + git_time_t mtime; + + git_pack_oidmap idx_cache; + unsigned char **ids; + + git_pack_cache bases; /* delta base cache */ + + time_t last_freshen; /* last time the packfile was freshened */ + + /* something like ".git/objects/pack/xxxxx.pack" */ + char pack_name[GIT_FLEX_ARRAY]; /* more */ +}; + +/** + * Return the position where an OID (or a prefix) would be inserted within + * the OID Lookup Table of an .idx file. This performs binary search + * between the lo and hi indices. + * + * The stride parameter is provided because .idx files version 1 store the + * OIDs interleaved with the 4-byte file offsets of the objects within the + * .pack file (stride = oid_size + 4), whereas files with version 2 store + * them in a contiguous flat array (stride = oid_size). + */ +int git_pack__lookup_id( + const void *id_lookup_table, + size_t stride, + unsigned lo, + unsigned hi, + const unsigned char *id_prefix, + const git_oid_t oid_type); + +typedef struct git_packfile_stream { + off64_t curpos; + int done; + git_zstream zstream; + struct git_pack_file *p; + git_mwindow *mw; +} git_packfile_stream; + +int git_packfile__object_header(size_t *out, unsigned char *hdr, size_t size, git_object_t type); + +int git_packfile__name(char **out, const char *path); + +int git_packfile_unpack_header( + size_t *size_p, + git_object_t *type_p, + struct git_pack_file *p, + git_mwindow **w_curs, + off64_t *curpos); + +int git_packfile_resolve_header( + size_t *size_p, + git_object_t *type_p, + struct git_pack_file *p, + off64_t offset); + +int git_packfile_unpack(git_rawobj *obj, struct git_pack_file *p, off64_t *obj_offset); + +int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, off64_t curpos); +ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len); +void git_packfile_stream_dispose(git_packfile_stream *obj); + +int get_delta_base( + off64_t *delta_base_out, + struct git_pack_file *p, + git_mwindow **w_curs, + off64_t *curpos, + git_object_t type, + off64_t delta_obj_offset); + +void git_packfile_free(struct git_pack_file *p, bool unlink_packfile); +int git_packfile_alloc( + struct git_pack_file **pack_out, + const char *path, + git_oid_t oid_type); + +int git_pack_entry_find( + struct git_pack_entry *e, + struct git_pack_file *p, + const git_oid *short_id, + size_t len); +int git_pack_foreach_entry( + struct git_pack_file *p, + git_odb_foreach_cb cb, + void *data); +/** + * Similar to git_pack_foreach_entry, but: + * - It also provides the offset of the object within the + * packfile. + * - It does not sort the objects in any order. + * - It retains the lock while invoking the callback. + */ +int git_pack_foreach_entry_offset( + struct git_pack_file *p, + git_pack_foreach_entry_offset_cb cb, + void *data); + +#endif diff --git a/src/libgit2/parse.c b/src/libgit2/parse.c new file mode 100644 index 00000000000..ea693462144 --- /dev/null +++ b/src/libgit2/parse.c @@ -0,0 +1,138 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#include "parse.h" +#include "oid.h" + +int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len) +{ + if (content && content_len) { + ctx->content = content; + ctx->content_len = content_len; + } else { + ctx->content = ""; + ctx->content_len = 0; + } + + ctx->remain = ctx->content; + ctx->remain_len = ctx->content_len; + ctx->line = ctx->remain; + ctx->line_len = git__linenlen(ctx->line, ctx->remain_len); + ctx->line_num = 1; + + return 0; +} + +void git_parse_ctx_clear(git_parse_ctx *ctx) +{ + memset(ctx, 0, sizeof(*ctx)); + ctx->content = ""; +} + +void git_parse_advance_line(git_parse_ctx *ctx) +{ + ctx->line += ctx->line_len; + ctx->remain_len -= ctx->line_len; + ctx->line_len = git__linenlen(ctx->line, ctx->remain_len); + ctx->line_num++; +} + +void git_parse_advance_chars(git_parse_ctx *ctx, size_t char_cnt) +{ + ctx->line += char_cnt; + ctx->remain_len -= char_cnt; + ctx->line_len -= char_cnt; +} + +int git_parse_advance_expected( + git_parse_ctx *ctx, + const char *expected, + size_t expected_len) +{ + if (ctx->line_len < expected_len) + return -1; + + if (memcmp(ctx->line, expected, expected_len) != 0) + return -1; + + git_parse_advance_chars(ctx, expected_len); + return 0; +} + +int git_parse_advance_ws(git_parse_ctx *ctx) +{ + int ret = -1; + + while (ctx->line_len > 0 && + ctx->line[0] != '\n' && + git__isspace(ctx->line[0])) { + ctx->line++; + ctx->line_len--; + ctx->remain_len--; + ret = 0; + } + + return ret; +} + +int git_parse_advance_nl(git_parse_ctx *ctx) +{ + if (ctx->line_len != 1 || ctx->line[0] != '\n') + return -1; + + git_parse_advance_line(ctx); + return 0; +} + +int git_parse_advance_digit(int64_t *out, git_parse_ctx *ctx, int base) +{ + const char *end; + int ret; + + if (ctx->line_len < 1 || !git__isdigit(ctx->line[0])) + return -1; + + if ((ret = git__strntol64(out, ctx->line, ctx->line_len, &end, base)) < 0) + return -1; + + git_parse_advance_chars(ctx, (end - ctx->line)); + return 0; +} + +int git_parse_advance_oid(git_oid *out, git_parse_ctx *ctx, git_oid_t oid_type) +{ + size_t oid_hexsize = git_oid_hexsize(oid_type); + GIT_ASSERT(oid_hexsize); + + if (ctx->line_len < oid_hexsize) + return -1; + if ((git_oid_from_prefix(out, ctx->line, oid_hexsize, oid_type)) < 0) + return -1; + git_parse_advance_chars(ctx, oid_hexsize); + return 0; +} + +int git_parse_peek(char *out, git_parse_ctx *ctx, int flags) +{ + size_t remain = ctx->line_len; + const char *ptr = ctx->line; + + while (remain) { + char c = *ptr; + + if ((flags & GIT_PARSE_PEEK_SKIP_WHITESPACE) && + git__isspace(c)) { + remain--; + ptr++; + continue; + } + + *out = c; + return 0; + } + + return -1; +} diff --git a/src/libgit2/parse.h b/src/libgit2/parse.h new file mode 100644 index 00000000000..beef1de12fb --- /dev/null +++ b/src/libgit2/parse.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_parse_h__ +#define INCLUDE_parse_h__ + +#include "common.h" + +typedef struct { + /* Original content buffer */ + const char *content; + size_t content_len; + + /* The remaining (unparsed) buffer */ + const char *remain; + size_t remain_len; + + const char *line; + size_t line_len; + size_t line_num; +} git_parse_ctx; + +#define GIT_PARSE_CTX_INIT { 0 } + +int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len); +void git_parse_ctx_clear(git_parse_ctx *ctx); + +#define git_parse_ctx_contains_s(ctx, str) \ + git_parse_ctx_contains(ctx, str, sizeof(str) - 1) + +GIT_INLINE(bool) git_parse_ctx_contains( + git_parse_ctx *ctx, const char *str, size_t len) +{ + return (ctx->line_len >= len && memcmp(ctx->line, str, len) == 0); +} + +void git_parse_advance_line(git_parse_ctx *ctx); +void git_parse_advance_chars(git_parse_ctx *ctx, size_t char_cnt); +int git_parse_advance_expected( + git_parse_ctx *ctx, + const char *expected, + size_t expected_len); + +#define git_parse_advance_expected_str(ctx, str) \ + git_parse_advance_expected(ctx, str, strlen(str)) + +int git_parse_advance_ws(git_parse_ctx *ctx); +int git_parse_advance_nl(git_parse_ctx *ctx); +int git_parse_advance_digit(int64_t *out, git_parse_ctx *ctx, int base); +int git_parse_advance_oid(git_oid *out, git_parse_ctx *ctx, git_oid_t oid_type); + +enum GIT_PARSE_PEEK_FLAGS { + GIT_PARSE_PEEK_SKIP_WHITESPACE = (1 << 0) +}; + +int git_parse_peek(char *out, git_parse_ctx *ctx, int flags); + +#endif diff --git a/src/libgit2/patch.c b/src/libgit2/patch.c new file mode 100644 index 00000000000..a30546f3ce6 --- /dev/null +++ b/src/libgit2/patch.c @@ -0,0 +1,230 @@ +/* +* Copyright (C) the libgit2 contributors. All rights reserved. +* +* This file is part of libgit2, distributed under the GNU GPL v2 with +* a Linking Exception. For full terms see the included COPYING file. +*/ + +#include "patch.h" + +#include "git2/patch.h" +#include "diff.h" + +int git_patch__invoke_callbacks( + git_patch *patch, + git_diff_file_cb file_cb, + git_diff_binary_cb binary_cb, + git_diff_hunk_cb hunk_cb, + git_diff_line_cb line_cb, + void *payload) +{ + int error = 0; + uint32_t i, j; + + if (file_cb) + error = file_cb(patch->delta, 0, payload); + + if (error) + return error; + + if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0) { + if (binary_cb) + error = binary_cb(patch->delta, &patch->binary, payload); + + return error; + } + + if (!hunk_cb && !line_cb) + return error; + + for (i = 0; !error && i < git_array_size(patch->hunks); ++i) { + git_patch_hunk *h = git_array_get(patch->hunks, i); + + if (hunk_cb) + error = hunk_cb(patch->delta, &h->hunk, payload); + + if (!line_cb) + continue; + + for (j = 0; !error && j < h->line_count; ++j) { + git_diff_line *l = + git_array_get(patch->lines, h->line_start + j); + + error = line_cb(patch->delta, &h->hunk, l, payload); + } + } + + return error; +} + +size_t git_patch_size( + git_patch *patch, + int include_context, + int include_hunk_headers, + int include_file_headers) +{ + size_t out; + + GIT_ASSERT_ARG(patch); + + out = patch->content_size; + + if (!include_context) + out -= patch->context_size; + + if (include_hunk_headers) + out += patch->header_size; + + if (include_file_headers) { + git_str file_header = GIT_STR_INIT; + + if (git_diff_delta__format_file_header( + &file_header, patch->delta, NULL, NULL, 0, true) < 0) + git_error_clear(); + else + out += git_str_len(&file_header); + + git_str_dispose(&file_header); + } + + return out; +} + +int git_patch_line_stats( + size_t *total_ctxt, + size_t *total_adds, + size_t *total_dels, + const git_patch *patch) +{ + size_t totals[3], idx; + + memset(totals, 0, sizeof(totals)); + + for (idx = 0; idx < git_array_size(patch->lines); ++idx) { + git_diff_line *line = git_array_get(patch->lines, idx); + if (!line) + continue; + + switch (line->origin) { + case GIT_DIFF_LINE_CONTEXT: totals[0]++; break; + case GIT_DIFF_LINE_ADDITION: totals[1]++; break; + case GIT_DIFF_LINE_DELETION: totals[2]++; break; + default: + /* diff --stat and --numstat don't count EOFNL marks because + * they will always be paired with a ADDITION or DELETION line. + */ + break; + } + } + + if (total_ctxt) + *total_ctxt = totals[0]; + if (total_adds) + *total_adds = totals[1]; + if (total_dels) + *total_dels = totals[2]; + + return 0; +} + +const git_diff_delta *git_patch_get_delta(const git_patch *patch) +{ + GIT_ASSERT_ARG_WITH_RETVAL(patch, NULL); + return patch->delta; +} + +size_t git_patch_num_hunks(const git_patch *patch) +{ + GIT_ASSERT_ARG(patch); + return git_array_size(patch->hunks); +} + +static int patch_error_outofrange(const char *thing) +{ + git_error_set(GIT_ERROR_INVALID, "patch %s index out of range", thing); + return GIT_ENOTFOUND; +} + +int git_patch_get_hunk( + const git_diff_hunk **out, + size_t *lines_in_hunk, + git_patch *patch, + size_t hunk_idx) +{ + git_patch_hunk *hunk; + GIT_ASSERT_ARG(patch); + + hunk = git_array_get(patch->hunks, hunk_idx); + + if (!hunk) { + if (out) *out = NULL; + if (lines_in_hunk) *lines_in_hunk = 0; + return patch_error_outofrange("hunk"); + } + + if (out) *out = &hunk->hunk; + if (lines_in_hunk) *lines_in_hunk = hunk->line_count; + return 0; +} + +int git_patch_num_lines_in_hunk(const git_patch *patch, size_t hunk_idx) +{ + git_patch_hunk *hunk; + GIT_ASSERT_ARG(patch); + + if (!(hunk = git_array_get(patch->hunks, hunk_idx))) + return patch_error_outofrange("hunk"); + return (int)hunk->line_count; +} + +int git_patch_get_line_in_hunk( + const git_diff_line **out, + git_patch *patch, + size_t hunk_idx, + size_t line_of_hunk) +{ + git_patch_hunk *hunk; + git_diff_line *line; + + GIT_ASSERT_ARG(patch); + + if (!(hunk = git_array_get(patch->hunks, hunk_idx))) { + if (out) *out = NULL; + return patch_error_outofrange("hunk"); + } + + if (line_of_hunk >= hunk->line_count || + !(line = git_array_get( + patch->lines, hunk->line_start + line_of_hunk))) { + if (out) *out = NULL; + return patch_error_outofrange("line"); + } + + if (out) *out = line; + return 0; +} + +git_repository *git_patch_owner(const git_patch *patch) +{ + return patch->repo; +} + +int git_patch_from_diff(git_patch **out, git_diff *diff, size_t idx) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(diff); + GIT_ASSERT_ARG(diff->patch_fn); + return diff->patch_fn(out, diff, idx); +} + +static void git_patch__free(git_patch *patch) +{ + if (patch->free_fn) + patch->free_fn(patch); +} + +void git_patch_free(git_patch *patch) +{ + if (patch) + GIT_REFCOUNT_DEC(patch, git_patch__free); +} diff --git a/src/libgit2/patch.h b/src/libgit2/patch.h new file mode 100644 index 00000000000..86328e886e7 --- /dev/null +++ b/src/libgit2/patch.h @@ -0,0 +1,75 @@ +/* +* Copyright (C) the libgit2 contributors. All rights reserved. +* +* This file is part of libgit2, distributed under the GNU GPL v2 with +* a Linking Exception. For full terms see the included COPYING file. +*/ +#ifndef INCLUDE_patch_h__ +#define INCLUDE_patch_h__ + +#include "common.h" + +#include "git2/patch.h" +#include "array.h" + +/* cached information about a hunk in a patch */ +typedef struct git_patch_hunk { + git_diff_hunk hunk; + size_t line_start; + size_t line_count; +} git_patch_hunk; + +struct git_patch { + git_refcount rc; + + git_repository *repo; /* may be null */ + + git_diff_options diff_opts; + + git_diff_delta *delta; + git_diff_binary binary; + git_array_t(git_patch_hunk) hunks; + git_array_t(git_diff_line) lines; + + size_t header_size; + size_t content_size; + size_t context_size; + + void (*free_fn)(git_patch *patch); +}; + +extern int git_patch__invoke_callbacks( + git_patch *patch, + git_diff_file_cb file_cb, + git_diff_binary_cb binary_cb, + git_diff_hunk_cb hunk_cb, + git_diff_line_cb line_cb, + void *payload); + +extern int git_patch_line_stats( + size_t *total_ctxt, + size_t *total_adds, + size_t *total_dels, + const git_patch *patch); + +/** Options for parsing patch files. */ +typedef struct { + /** + * The length of the prefix (in path segments) for the filenames. + * This prefix will be removed when looking for files. The default is 1. + */ + uint32_t prefix_len; + + /** + * The type of object IDs in the patch file. The default is + * `GIT_OID_DEFAULT`. + */ + git_oid_t oid_type; +} git_patch_options; + +#define GIT_PATCH_OPTIONS_INIT { 1, GIT_OID_DEFAULT } + +extern int git_patch__to_buf(git_str *out, git_patch *patch); +extern void git_patch_free(git_patch *patch); + +#endif diff --git a/src/libgit2/patch_generate.c b/src/libgit2/patch_generate.c new file mode 100644 index 00000000000..079bc53ae9b --- /dev/null +++ b/src/libgit2/patch_generate.c @@ -0,0 +1,934 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "patch_generate.h" + +#include "git2/blob.h" +#include "diff.h" +#include "diff_generate.h" +#include "diff_file.h" +#include "diff_driver.h" +#include "diff_xdiff.h" +#include "delta.h" +#include "zstream.h" +#include "futils.h" + +static void diff_output_init( + git_patch_generated_output *, const git_diff_options *, git_diff_file_cb, + git_diff_binary_cb, git_diff_hunk_cb, git_diff_line_cb, void*); + +static void diff_output_to_patch( + git_patch_generated_output *, git_patch_generated *); + +static void patch_generated_free(git_patch *p) +{ + git_patch_generated *patch = (git_patch_generated *)p; + + git_array_clear(patch->base.lines); + git_array_clear(patch->base.hunks); + + git__free((char *)patch->base.binary.old_file.data); + git__free((char *)patch->base.binary.new_file.data); + + git_diff_file_content__clear(&patch->ofile); + git_diff_file_content__clear(&patch->nfile); + + git_diff_free(patch->diff); /* decrements refcount */ + patch->diff = NULL; + + git_pool_clear(&patch->flattened); + + git__free((char *)patch->base.diff_opts.old_prefix); + git__free((char *)patch->base.diff_opts.new_prefix); + + if (patch->flags & GIT_PATCH_GENERATED_ALLOCATED) + git__free(patch); +} + +static void patch_generated_update_binary(git_patch_generated *patch) +{ + if ((patch->base.delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0) + return; + + if ((patch->ofile.file->flags & GIT_DIFF_FLAG_BINARY) != 0 || + (patch->nfile.file->flags & GIT_DIFF_FLAG_BINARY) != 0) + patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY; + + else if (patch->ofile.file->size > GIT_XDIFF_MAX_SIZE || + patch->nfile.file->size > GIT_XDIFF_MAX_SIZE) + patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY; + + else if ((patch->ofile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0 && + (patch->nfile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0) + patch->base.delta->flags |= GIT_DIFF_FLAG_NOT_BINARY; +} + +static void patch_generated_init_common(git_patch_generated *patch) +{ + patch->base.free_fn = patch_generated_free; + + patch_generated_update_binary(patch); + + patch->flags |= GIT_PATCH_GENERATED_INITIALIZED; + + if (patch->diff) + git_diff_addref(patch->diff); +} + +static int patch_generated_normalize_options( + git_diff_options *out, + const git_diff_options *opts, + git_repository *repo) +{ + if (opts) { + GIT_ERROR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); + memcpy(out, opts, sizeof(git_diff_options)); + } else { + git_diff_options default_opts = GIT_DIFF_OPTIONS_INIT; + memcpy(out, &default_opts, sizeof(git_diff_options)); + } + + if (repo && opts && opts->oid_type && repo->oid_type != opts->oid_type) { + /* + * This limitation feels unnecessary - we should consider + * allowing users to generate diffs with a different object + * ID format than the repository. + */ + git_error_set(GIT_ERROR_INVALID, + "specified object ID type does not match repository object ID type"); + return -1; + } else if (repo) { + out->oid_type = repo->oid_type; + } else if (opts && opts->oid_type) { + out->oid_type = opts->oid_type; + } else { + out->oid_type = GIT_OID_DEFAULT; + } + + out->old_prefix = opts && opts->old_prefix ? + git__strdup(opts->old_prefix) : + git__strdup(DIFF_OLD_PREFIX_DEFAULT); + + out->new_prefix = opts && opts->new_prefix ? + git__strdup(opts->new_prefix) : + git__strdup(DIFF_NEW_PREFIX_DEFAULT); + + GIT_ERROR_CHECK_ALLOC(out->old_prefix); + GIT_ERROR_CHECK_ALLOC(out->new_prefix); + + return 0; +} + +static int patch_generated_init( + git_patch_generated *patch, git_diff *diff, size_t delta_index) +{ + int error = 0; + + memset(patch, 0, sizeof(*patch)); + + patch->diff = diff; + patch->base.repo = diff->repo; + patch->base.delta = git_vector_get(&diff->deltas, delta_index); + patch->delta_index = delta_index; + + if ((error = patch_generated_normalize_options( + &patch->base.diff_opts, &diff->opts, diff->repo)) < 0 || + (error = git_diff_file_content__init_from_diff( + &patch->ofile, diff, patch->base.delta, true)) < 0 || + (error = git_diff_file_content__init_from_diff( + &patch->nfile, diff, patch->base.delta, false)) < 0) + return error; + + patch_generated_init_common(patch); + + return 0; +} + +static int patch_generated_alloc_from_diff( + git_patch_generated **out, git_diff *diff, size_t delta_index) +{ + int error; + git_patch_generated *patch = git__calloc(1, sizeof(git_patch_generated)); + GIT_ERROR_CHECK_ALLOC(patch); + + if (!(error = patch_generated_init(patch, diff, delta_index))) { + patch->flags |= GIT_PATCH_GENERATED_ALLOCATED; + GIT_REFCOUNT_INC(&patch->base); + } else { + git__free(patch); + patch = NULL; + } + + *out = patch; + return error; +} + +GIT_INLINE(bool) should_skip_binary(git_patch_generated *patch, git_diff_file *file) +{ + if ((patch->base.diff_opts.flags & GIT_DIFF_SHOW_BINARY) != 0) + return false; + + return (file->flags & GIT_DIFF_FLAG_BINARY) != 0; +} + +static bool patch_generated_diffable(git_patch_generated *patch) +{ + size_t olen, nlen; + + if (patch->base.delta->status == GIT_DELTA_UNMODIFIED) + return false; + + /* if we've determined this to be binary (and we are not showing binary + * data) then we have skipped loading the map data. instead, query the + * file data itself. + */ + if ((patch->base.delta->flags & GIT_DIFF_FLAG_BINARY) != 0 && + (patch->base.diff_opts.flags & GIT_DIFF_SHOW_BINARY) == 0) { + olen = (size_t)patch->ofile.file->size; + nlen = (size_t)patch->nfile.file->size; + } else { + olen = patch->ofile.map.len; + nlen = patch->nfile.map.len; + } + + /* if both sides are empty, files are identical */ + if (!olen && !nlen) + return false; + + /* otherwise, check the file sizes and the oid */ + return (olen != nlen || + !git_oid_equal(&patch->ofile.file->id, &patch->nfile.file->id)); +} + +static int patch_generated_load(git_patch_generated *patch, git_patch_generated_output *output) +{ + int error = 0; + bool incomplete_data; + + if ((patch->flags & GIT_PATCH_GENERATED_LOADED) != 0) + return 0; + + /* if no hunk and data callbacks and user doesn't care if data looks + * binary, then there is no need to actually load the data + */ + if ((patch->ofile.opts_flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0 && + output && !output->binary_cb && !output->hunk_cb && !output->data_cb) + return 0; + + incomplete_data = + (((patch->ofile.flags & GIT_DIFF_FLAG__NO_DATA) != 0 || + (patch->ofile.file->flags & GIT_DIFF_FLAG_VALID_ID) != 0) && + ((patch->nfile.flags & GIT_DIFF_FLAG__NO_DATA) != 0 || + (patch->nfile.file->flags & GIT_DIFF_FLAG_VALID_ID) != 0)); + + if ((error = git_diff_file_content__load( + &patch->ofile, &patch->base.diff_opts)) < 0 || + (error = git_diff_file_content__load( + &patch->nfile, &patch->base.diff_opts)) < 0 || + should_skip_binary(patch, patch->nfile.file)) + goto cleanup; + + /* if previously missing an oid, and now that we have it the two sides + * are the same (and not submodules), update MODIFIED -> UNMODIFIED + */ + if (incomplete_data && + patch->ofile.file->mode == patch->nfile.file->mode && + patch->ofile.file->mode != GIT_FILEMODE_COMMIT && + git_oid_equal(&patch->ofile.file->id, &patch->nfile.file->id) && + patch->base.delta->status == GIT_DELTA_MODIFIED) /* not RENAMED/COPIED! */ + patch->base.delta->status = GIT_DELTA_UNMODIFIED; + +cleanup: + patch_generated_update_binary(patch); + + if (!error) { + if (patch_generated_diffable(patch)) + patch->flags |= GIT_PATCH_GENERATED_DIFFABLE; + + patch->flags |= GIT_PATCH_GENERATED_LOADED; + } + + return error; +} + +static int patch_generated_invoke_file_callback( + git_patch_generated *patch, git_patch_generated_output *output) +{ + float progress = patch->diff ? + ((float)patch->delta_index / patch->diff->deltas.length) : 1.0f; + + if (!output->file_cb) + return 0; + + return git_error_set_after_callback_function( + output->file_cb(patch->base.delta, progress, output->payload), + "git_patch"); +} + +static int create_binary( + git_diff_binary_t *out_type, + char **out_data, + size_t *out_datalen, + size_t *out_inflatedlen, + const char *a_data, + size_t a_datalen, + const char *b_data, + size_t b_datalen) +{ + git_str deflate = GIT_STR_INIT, delta = GIT_STR_INIT; + size_t delta_data_len = 0; + int error; + + /* The git_delta function accepts unsigned long only */ + if (!git__is_ulong(a_datalen) || !git__is_ulong(b_datalen)) + return GIT_EBUFS; + + if ((error = git_zstream_deflatebuf(&deflate, b_data, b_datalen)) < 0) + goto done; + + /* The git_delta function accepts unsigned long only */ + if (!git__is_ulong(deflate.size)) { + error = GIT_EBUFS; + goto done; + } + + if (a_datalen && b_datalen) { + void *delta_data; + + error = git_delta(&delta_data, &delta_data_len, + a_data, a_datalen, + b_data, b_datalen, + deflate.size); + + if (error == 0) { + error = git_zstream_deflatebuf( + &delta, delta_data, delta_data_len); + + git__free(delta_data); + } else if (error == GIT_EBUFS) { + error = 0; + } + + if (error < 0) + goto done; + } + + if (delta.size && delta.size < deflate.size) { + *out_type = GIT_DIFF_BINARY_DELTA; + *out_datalen = delta.size; + *out_data = git_str_detach(&delta); + *out_inflatedlen = delta_data_len; + } else { + *out_type = GIT_DIFF_BINARY_LITERAL; + *out_datalen = deflate.size; + *out_data = git_str_detach(&deflate); + *out_inflatedlen = b_datalen; + } + +done: + git_str_dispose(&deflate); + git_str_dispose(&delta); + + return error; +} + +static int diff_binary(git_patch_generated_output *output, git_patch_generated *patch) +{ + git_diff_binary binary = {0}; + const char *old_data = patch->ofile.map.data; + const char *new_data = patch->nfile.map.data; + size_t old_len = patch->ofile.map.len, + new_len = patch->nfile.map.len; + int error; + + /* Only load contents if the user actually wants to diff + * binary files. */ + if (patch->base.diff_opts.flags & GIT_DIFF_SHOW_BINARY) { + binary.contains_data = 1; + + /* Create the old->new delta (as the "new" side of the patch), + * and the new->old delta (as the "old" side) + */ + if ((error = create_binary(&binary.old_file.type, + (char **)&binary.old_file.data, + &binary.old_file.datalen, + &binary.old_file.inflatedlen, + new_data, new_len, old_data, old_len)) < 0 || + (error = create_binary(&binary.new_file.type, + (char **)&binary.new_file.data, + &binary.new_file.datalen, + &binary.new_file.inflatedlen, + old_data, old_len, new_data, new_len)) < 0) + return error; + } + + error = git_error_set_after_callback_function( + output->binary_cb(patch->base.delta, &binary, output->payload), + "git_patch"); + + git__free((char *) binary.old_file.data); + git__free((char *) binary.new_file.data); + + return error; +} + +static int patch_generated_create( + git_patch_generated *patch, + git_patch_generated_output *output) +{ + int error = 0; + + if ((patch->flags & GIT_PATCH_GENERATED_DIFFED) != 0) + return 0; + + /* if we are not looking at the binary or text data, don't do the diff */ + if (!output->binary_cb && !output->hunk_cb && !output->data_cb) + return 0; + + if ((patch->flags & GIT_PATCH_GENERATED_LOADED) == 0 && + (error = patch_generated_load(patch, output)) < 0) + return error; + + if ((patch->flags & GIT_PATCH_GENERATED_DIFFABLE) == 0) + return 0; + + if ((patch->base.delta->flags & GIT_DIFF_FLAG_BINARY) != 0) { + if (output->binary_cb) + error = diff_binary(output, patch); + } + else { + if (output->diff_cb) + error = output->diff_cb(output, patch); + } + + patch->flags |= GIT_PATCH_GENERATED_DIFFED; + return error; +} + +static int diff_required(git_diff *diff, const char *action) +{ + if (diff) + return 0; + git_error_set(GIT_ERROR_INVALID, "must provide valid diff to %s", action); + return -1; +} + +typedef struct { + git_patch_generated patch; + git_diff_delta delta; + char paths[GIT_FLEX_ARRAY]; +} patch_generated_with_delta; + +static int diff_single_generate(patch_generated_with_delta *pd, git_xdiff_output *xo) +{ + int error = 0; + git_patch_generated *patch = &pd->patch; + bool has_old = ((patch->ofile.flags & GIT_DIFF_FLAG__NO_DATA) == 0); + bool has_new = ((patch->nfile.flags & GIT_DIFF_FLAG__NO_DATA) == 0); + + pd->delta.status = has_new ? + (has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) : + (has_old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED); + + if (git_oid_equal(&patch->nfile.file->id, &patch->ofile.file->id)) + pd->delta.status = GIT_DELTA_UNMODIFIED; + + patch->base.delta = &pd->delta; + + patch_generated_init_common(patch); + + if (pd->delta.status == GIT_DELTA_UNMODIFIED && + !(patch->ofile.opts_flags & GIT_DIFF_INCLUDE_UNMODIFIED)) { + + /* Even empty patches are flagged as binary, and even though + * there's no difference, we flag this as "containing data" + * (the data is known to be empty, as opposed to wholly unknown). + */ + if (patch->base.diff_opts.flags & GIT_DIFF_SHOW_BINARY) + patch->base.binary.contains_data = 1; + + return error; + } + + error = patch_generated_invoke_file_callback(patch, (git_patch_generated_output *)xo); + + if (!error) + error = patch_generated_create(patch, (git_patch_generated_output *)xo); + + return error; +} + +static int patch_generated_from_sources( + patch_generated_with_delta *pd, + git_xdiff_output *xo, + git_diff_file_content_src *oldsrc, + git_diff_file_content_src *newsrc, + const git_diff_options *given_opts) +{ + int error = 0; + git_repository *repo = + oldsrc->blob ? git_blob_owner(oldsrc->blob) : + newsrc->blob ? git_blob_owner(newsrc->blob) : NULL; + git_diff_file *lfile = &pd->delta.old_file, *rfile = &pd->delta.new_file; + git_diff_file_content *ldata = &pd->patch.ofile, *rdata = &pd->patch.nfile; + git_diff_options *opts = &pd->patch.base.diff_opts; + + if ((error = patch_generated_normalize_options(opts, given_opts, repo)) < 0) + return error; + + if ((opts->flags & GIT_DIFF_REVERSE) != 0) { + void *tmp = lfile; lfile = rfile; rfile = tmp; + tmp = ldata; ldata = rdata; rdata = tmp; + } + + pd->patch.base.delta = &pd->delta; + + if (!oldsrc->as_path) { + if (newsrc->as_path) + oldsrc->as_path = newsrc->as_path; + else + oldsrc->as_path = newsrc->as_path = "file"; + } + else if (!newsrc->as_path) + newsrc->as_path = oldsrc->as_path; + + lfile->path = oldsrc->as_path; + rfile->path = newsrc->as_path; + + if ((error = git_diff_file_content__init_from_src( + ldata, repo, opts, oldsrc, lfile)) < 0 || + (error = git_diff_file_content__init_from_src( + rdata, repo, opts, newsrc, rfile)) < 0) + return error; + + return diff_single_generate(pd, xo); +} + +static int patch_generated_with_delta_alloc( + patch_generated_with_delta **out, + const char **old_path, + const char **new_path) +{ + patch_generated_with_delta *pd; + size_t old_len = *old_path ? strlen(*old_path) : 0; + size_t new_len = *new_path ? strlen(*new_path) : 0; + size_t alloc_len; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, sizeof(*pd), old_len); + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, new_len); + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2); + + *out = pd = git__calloc(1, alloc_len); + GIT_ERROR_CHECK_ALLOC(pd); + + pd->patch.flags = GIT_PATCH_GENERATED_ALLOCATED; + + if (*old_path) { + memcpy(&pd->paths[0], *old_path, old_len); + *old_path = &pd->paths[0]; + } else if (*new_path) + *old_path = &pd->paths[old_len + 1]; + + if (*new_path) { + memcpy(&pd->paths[old_len + 1], *new_path, new_len); + *new_path = &pd->paths[old_len + 1]; + } else if (*old_path) + *new_path = &pd->paths[0]; + + return 0; +} + +static int diff_from_sources( + git_diff_file_content_src *oldsrc, + git_diff_file_content_src *newsrc, + const git_diff_options *opts, + git_diff_file_cb file_cb, + git_diff_binary_cb binary_cb, + git_diff_hunk_cb hunk_cb, + git_diff_line_cb data_cb, + void *payload) +{ + int error = 0; + patch_generated_with_delta pd; + git_xdiff_output xo; + + memset(&xo, 0, sizeof(xo)); + diff_output_init( + &xo.output, opts, file_cb, binary_cb, hunk_cb, data_cb, payload); + git_xdiff_init(&xo, opts); + + memset(&pd, 0, sizeof(pd)); + + error = patch_generated_from_sources(&pd, &xo, oldsrc, newsrc, opts); + + git_patch_free(&pd.patch.base); + + return error; +} + +static int patch_from_sources( + git_patch **out, + git_diff_file_content_src *oldsrc, + git_diff_file_content_src *newsrc, + const git_diff_options *opts) +{ + int error = 0; + patch_generated_with_delta *pd; + git_xdiff_output xo; + + GIT_ASSERT_ARG(out); + *out = NULL; + + if ((error = patch_generated_with_delta_alloc( + &pd, &oldsrc->as_path, &newsrc->as_path)) < 0) + return error; + + memset(&xo, 0, sizeof(xo)); + diff_output_to_patch(&xo.output, &pd->patch); + git_xdiff_init(&xo, opts); + + if (!(error = patch_generated_from_sources(pd, &xo, oldsrc, newsrc, opts))) + *out = (git_patch *)pd; + else + git_patch_free((git_patch *)pd); + + return error; +} + +int git_diff_blobs( + const git_blob *old_blob, + const char *old_path, + const git_blob *new_blob, + const char *new_path, + const git_diff_options *opts, + git_diff_file_cb file_cb, + git_diff_binary_cb binary_cb, + git_diff_hunk_cb hunk_cb, + git_diff_line_cb data_cb, + void *payload) +{ + git_diff_file_content_src osrc = + GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path); + git_diff_file_content_src nsrc = + GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path); + return diff_from_sources( + &osrc, &nsrc, opts, file_cb, binary_cb, hunk_cb, data_cb, payload); +} + +int git_patch_from_blobs( + git_patch **out, + const git_blob *old_blob, + const char *old_path, + const git_blob *new_blob, + const char *new_path, + const git_diff_options *opts) +{ + git_diff_file_content_src osrc = + GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path); + git_diff_file_content_src nsrc = + GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path); + return patch_from_sources(out, &osrc, &nsrc, opts); +} + +int git_diff_blob_to_buffer( + const git_blob *old_blob, + const char *old_path, + const char *buf, + size_t buflen, + const char *buf_path, + const git_diff_options *opts, + git_diff_file_cb file_cb, + git_diff_binary_cb binary_cb, + git_diff_hunk_cb hunk_cb, + git_diff_line_cb data_cb, + void *payload) +{ + git_diff_file_content_src osrc = + GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path); + git_diff_file_content_src nsrc = + GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path); + return diff_from_sources( + &osrc, &nsrc, opts, file_cb, binary_cb, hunk_cb, data_cb, payload); +} + +int git_patch_from_blob_and_buffer( + git_patch **out, + const git_blob *old_blob, + const char *old_path, + const void *buf, + size_t buflen, + const char *buf_path, + const git_diff_options *opts) +{ + git_diff_file_content_src osrc = + GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path); + git_diff_file_content_src nsrc = + GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path); + return patch_from_sources(out, &osrc, &nsrc, opts); +} + +int git_diff_buffers( + const void *old_buf, + size_t old_len, + const char *old_path, + const void *new_buf, + size_t new_len, + const char *new_path, + const git_diff_options *opts, + git_diff_file_cb file_cb, + git_diff_binary_cb binary_cb, + git_diff_hunk_cb hunk_cb, + git_diff_line_cb data_cb, + void *payload) +{ + git_diff_file_content_src osrc = + GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path); + git_diff_file_content_src nsrc = + GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path); + return diff_from_sources( + &osrc, &nsrc, opts, file_cb, binary_cb, hunk_cb, data_cb, payload); +} + +int git_patch_from_buffers( + git_patch **out, + const void *old_buf, + size_t old_len, + const char *old_path, + const void *new_buf, + size_t new_len, + const char *new_path, + const git_diff_options *opts) +{ + git_diff_file_content_src osrc = + GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path); + git_diff_file_content_src nsrc = + GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path); + return patch_from_sources(out, &osrc, &nsrc, opts); +} + +int git_patch_generated_from_diff( + git_patch **patch_ptr, git_diff *diff, size_t idx) +{ + int error = 0; + git_xdiff_output xo; + git_diff_delta *delta = NULL; + git_patch_generated *patch = NULL; + + if (patch_ptr) *patch_ptr = NULL; + + if (diff_required(diff, "git_patch_from_diff") < 0) + return -1; + + delta = git_vector_get(&diff->deltas, idx); + if (!delta) { + git_error_set(GIT_ERROR_INVALID, "index out of range for delta in diff"); + return GIT_ENOTFOUND; + } + + if (git_diff_delta__should_skip(&diff->opts, delta)) + return 0; + + /* don't load the patch data unless we need it for binary check */ + if (!patch_ptr && + ((delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0 || + (diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0)) + return 0; + + if ((error = patch_generated_alloc_from_diff(&patch, diff, idx)) < 0) + return error; + + memset(&xo, 0, sizeof(xo)); + diff_output_to_patch(&xo.output, patch); + git_xdiff_init(&xo, &diff->opts); + + error = patch_generated_invoke_file_callback(patch, &xo.output); + + if (!error) + error = patch_generated_create(patch, &xo.output); + + if (!error) { + /* TODO: if cumulative diff size is < 0.5 total size, flatten patch */ + /* TODO: and unload the file content */ + } + + if (error || !patch_ptr) + git_patch_free(&patch->base); + else + *patch_ptr = &patch->base; + + return error; +} + +git_diff_driver *git_patch_generated_driver(git_patch_generated *patch) +{ + /* ofile driver is representative for whole patch */ + return patch->ofile.driver; +} + +int git_patch_generated_old_data( + char **ptr, long *len, git_patch_generated *patch) +{ + if (patch->ofile.map.len > LONG_MAX || + patch->ofile.map.len > GIT_XDIFF_MAX_SIZE) { + git_error_set(GIT_ERROR_INVALID, "files too large for diff"); + return -1; + } + + *ptr = patch->ofile.map.data; + *len = (long)patch->ofile.map.len; + + return 0; +} + +int git_patch_generated_new_data( + char **ptr, long *len, git_patch_generated *patch) +{ + if (patch->ofile.map.len > LONG_MAX || + patch->ofile.map.len > GIT_XDIFF_MAX_SIZE) { + git_error_set(GIT_ERROR_INVALID, "files too large for diff"); + return -1; + } + + *ptr = patch->nfile.map.data; + *len = (long)patch->nfile.map.len; + + return 0; +} + +static int patch_generated_file_cb( + const git_diff_delta *delta, + float progress, + void *payload) +{ + GIT_UNUSED(delta); GIT_UNUSED(progress); GIT_UNUSED(payload); + return 0; +} + +static int patch_generated_binary_cb( + const git_diff_delta *delta, + const git_diff_binary *binary, + void *payload) +{ + git_patch *patch = payload; + + GIT_UNUSED(delta); + + memcpy(&patch->binary, binary, sizeof(git_diff_binary)); + + if (binary->old_file.data) { + patch->binary.old_file.data = git__malloc(binary->old_file.datalen); + GIT_ERROR_CHECK_ALLOC(patch->binary.old_file.data); + + memcpy((char *)patch->binary.old_file.data, + binary->old_file.data, binary->old_file.datalen); + } + + if (binary->new_file.data) { + patch->binary.new_file.data = git__malloc(binary->new_file.datalen); + GIT_ERROR_CHECK_ALLOC(patch->binary.new_file.data); + + memcpy((char *)patch->binary.new_file.data, + binary->new_file.data, binary->new_file.datalen); + } + + return 0; +} + +static int git_patch_hunk_cb( + const git_diff_delta *delta, + const git_diff_hunk *hunk_, + void *payload) +{ + git_patch_generated *patch = payload; + git_patch_hunk *hunk; + + GIT_UNUSED(delta); + + hunk = git_array_alloc(patch->base.hunks); + GIT_ERROR_CHECK_ALLOC(hunk); + + memcpy(&hunk->hunk, hunk_, sizeof(hunk->hunk)); + + patch->base.header_size += hunk_->header_len; + + hunk->line_start = git_array_size(patch->base.lines); + hunk->line_count = 0; + + return 0; +} + +static int patch_generated_line_cb( + const git_diff_delta *delta, + const git_diff_hunk *hunk_, + const git_diff_line *line_, + void *payload) +{ + git_patch_generated *patch = payload; + git_patch_hunk *hunk; + git_diff_line *line; + + GIT_UNUSED(delta); + GIT_UNUSED(hunk_); + + hunk = git_array_last(patch->base.hunks); + GIT_ASSERT(hunk); /* programmer error if no hunk is available */ + + line = git_array_alloc(patch->base.lines); + GIT_ERROR_CHECK_ALLOC(line); + + memcpy(line, line_, sizeof(*line)); + + /* do some bookkeeping so we can provide old/new line numbers */ + + patch->base.content_size += line->content_len; + + if (line->origin == GIT_DIFF_LINE_ADDITION || + line->origin == GIT_DIFF_LINE_DELETION) + patch->base.content_size += 1; + else if (line->origin == GIT_DIFF_LINE_CONTEXT) { + patch->base.content_size += 1; + patch->base.context_size += line->content_len + 1; + } else if (line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL) + patch->base.context_size += line->content_len; + + hunk->line_count++; + + return 0; +} + +static void diff_output_init( + git_patch_generated_output *out, + const git_diff_options *opts, + git_diff_file_cb file_cb, + git_diff_binary_cb binary_cb, + git_diff_hunk_cb hunk_cb, + git_diff_line_cb data_cb, + void *payload) +{ + GIT_UNUSED(opts); + + memset(out, 0, sizeof(*out)); + + out->file_cb = file_cb; + out->binary_cb = binary_cb; + out->hunk_cb = hunk_cb; + out->data_cb = data_cb; + out->payload = payload; +} + +static void diff_output_to_patch( + git_patch_generated_output *out, git_patch_generated *patch) +{ + diff_output_init( + out, + NULL, + patch_generated_file_cb, + patch_generated_binary_cb, + git_patch_hunk_cb, + patch_generated_line_cb, + patch); +} diff --git a/src/libgit2/patch_generate.h b/src/libgit2/patch_generate.h new file mode 100644 index 00000000000..56e3e9df475 --- /dev/null +++ b/src/libgit2/patch_generate.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_patch_generate_h__ +#define INCLUDE_patch_generate_h__ + +#include "common.h" + +#include "diff.h" +#include "diff_file.h" +#include "patch.h" + +enum { + GIT_PATCH_GENERATED_ALLOCATED = (1 << 0), + GIT_PATCH_GENERATED_INITIALIZED = (1 << 1), + GIT_PATCH_GENERATED_LOADED = (1 << 2), + /* the two sides are different */ + GIT_PATCH_GENERATED_DIFFABLE = (1 << 3), + /* the difference between the two sides has been computed */ + GIT_PATCH_GENERATED_DIFFED = (1 << 4), + GIT_PATCH_GENERATED_FLATTENED = (1 << 5) +}; + +struct git_patch_generated { + struct git_patch base; + + git_diff *diff; /* for refcount purposes, maybe NULL for blob diffs */ + size_t delta_index; + git_diff_file_content ofile; + git_diff_file_content nfile; + uint32_t flags; + git_pool flattened; +}; + +typedef struct git_patch_generated git_patch_generated; + +extern git_diff_driver *git_patch_generated_driver(git_patch_generated *); + +extern int git_patch_generated_old_data( + char **, long *, git_patch_generated *); +extern int git_patch_generated_new_data( + char **, long *, git_patch_generated *); +extern int git_patch_generated_from_diff( + git_patch **, git_diff *, size_t); + +typedef struct git_patch_generated_output git_patch_generated_output; + +struct git_patch_generated_output { + /* these callbacks are issued with the diff data */ + git_diff_file_cb file_cb; + git_diff_binary_cb binary_cb; + git_diff_hunk_cb hunk_cb; + git_diff_line_cb data_cb; + void *payload; + + /* this records the actual error in cases where it may be obscured */ + int error; + + /* this callback is used to do the diff and drive the other callbacks. + * see diff_xdiff.h for how to use this in practice for now. + */ + int (*diff_cb)(git_patch_generated_output *output, + git_patch_generated *patch); +}; + +#endif diff --git a/src/libgit2/patch_parse.c b/src/libgit2/patch_parse.c new file mode 100644 index 00000000000..0a157178074 --- /dev/null +++ b/src/libgit2/patch_parse.c @@ -0,0 +1,1239 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "patch_parse.h" + +#include "git2/patch.h" +#include "patch.h" +#include "diff_parse.h" +#include "fs_path.h" + +typedef struct { + git_patch base; + + git_patch_parse_ctx *ctx; + + /* the paths from the `diff --git` header, these will be used if this is not + * a rename (and rename paths are specified) or if no `+++`/`---` line specify + * the paths. + */ + char *header_old_path, *header_new_path; + + /* renamed paths are precise and are not prefixed */ + char *rename_old_path, *rename_new_path; + + /* the paths given in `---` and `+++` lines */ + char *old_path, *new_path; + + /* the prefixes from the old/new paths */ + char *old_prefix, *new_prefix; +} git_patch_parsed; + +static int git_parse_err(const char *fmt, ...) GIT_FORMAT_PRINTF(1, 2); +static int git_parse_err(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + git_error_vset(GIT_ERROR_PATCH, fmt, ap); + va_end(ap); + + return -1; +} + +static size_t header_path_len(git_patch_parse_ctx *ctx) +{ + bool inquote = 0; + bool quoted = git_parse_ctx_contains_s(&ctx->parse_ctx, "\""); + size_t len; + + for (len = quoted; len < ctx->parse_ctx.line_len; len++) { + if (!quoted && git__isspace(ctx->parse_ctx.line[len])) + break; + else if (quoted && !inquote && ctx->parse_ctx.line[len] == '"') { + len++; + break; + } + + inquote = (!inquote && ctx->parse_ctx.line[len] == '\\'); + } + + return len; +} + +static int parse_header_path_buf(git_str *path, git_patch_parse_ctx *ctx, size_t path_len) +{ + int error; + + if ((error = git_str_put(path, ctx->parse_ctx.line, path_len)) < 0) + return error; + + git_parse_advance_chars(&ctx->parse_ctx, path_len); + + git_str_rtrim(path); + + if (path->size > 0 && path->ptr[0] == '"' && + (error = git_str_unquote(path)) < 0) + return error; + + git_fs_path_squash_slashes(path); + + if (!path->size) + return git_parse_err("patch contains empty path at line %"PRIuZ, + ctx->parse_ctx.line_num); + + return 0; +} + +static int parse_header_path(char **out, git_patch_parse_ctx *ctx) +{ + git_str path = GIT_STR_INIT; + int error; + + if ((error = parse_header_path_buf(&path, ctx, header_path_len(ctx))) < 0) + goto out; + *out = git_str_detach(&path); + +out: + git_str_dispose(&path); + return error; +} + +static int parse_header_git_oldpath( + git_patch_parsed *patch, git_patch_parse_ctx *ctx) +{ + git_str old_path = GIT_STR_INIT; + int error; + + if (patch->old_path) { + error = git_parse_err("patch contains duplicate old path at line %"PRIuZ, + ctx->parse_ctx.line_num); + goto out; + } + + if ((error = parse_header_path_buf(&old_path, ctx, ctx->parse_ctx.line_len - 1)) < 0) + goto out; + + patch->old_path = git_str_detach(&old_path); + +out: + git_str_dispose(&old_path); + return error; +} + +static int parse_header_git_newpath( + git_patch_parsed *patch, git_patch_parse_ctx *ctx) +{ + git_str new_path = GIT_STR_INIT; + int error; + + if (patch->new_path) { + error = git_parse_err("patch contains duplicate new path at line %"PRIuZ, + ctx->parse_ctx.line_num); + goto out; + } + + if ((error = parse_header_path_buf(&new_path, ctx, ctx->parse_ctx.line_len - 1)) < 0) + goto out; + patch->new_path = git_str_detach(&new_path); + +out: + git_str_dispose(&new_path); + return error; +} + +static int parse_header_mode(uint16_t *mode, git_patch_parse_ctx *ctx) +{ + int64_t m; + + if ((git_parse_advance_digit(&m, &ctx->parse_ctx, 8)) < 0) + return git_parse_err("invalid file mode at line %"PRIuZ, ctx->parse_ctx.line_num); + + if (m > UINT16_MAX) + return -1; + + *mode = (uint16_t)m; + + return 0; +} + +static int parse_header_oid( + git_oid *oid, + uint16_t *oid_len, + git_patch_parse_ctx *ctx) +{ + size_t hexsize, len; + + hexsize = git_oid_hexsize(ctx->opts.oid_type); + + for (len = 0; + len < ctx->parse_ctx.line_len && len < hexsize; + len++) { + if (!git__isxdigit(ctx->parse_ctx.line[len])) + break; + } + + if (len < GIT_OID_MINPREFIXLEN || len > hexsize || + git_oid_from_prefix(oid, ctx->parse_ctx.line, len, ctx->opts.oid_type) < 0) + return git_parse_err("invalid hex formatted object id at line %"PRIuZ, + ctx->parse_ctx.line_num); + + git_parse_advance_chars(&ctx->parse_ctx, len); + + *oid_len = (uint16_t)len; + + return 0; +} + +static int parse_header_git_index( + git_patch_parsed *patch, git_patch_parse_ctx *ctx) +{ + char c; + + if (parse_header_oid(&patch->base.delta->old_file.id, + &patch->base.delta->old_file.id_abbrev, ctx) < 0 || + git_parse_advance_expected_str(&ctx->parse_ctx, "..") < 0 || + parse_header_oid(&patch->base.delta->new_file.id, + &patch->base.delta->new_file.id_abbrev, ctx) < 0) + return -1; + + if (git_parse_peek(&c, &ctx->parse_ctx, 0) == 0 && c == ' ') { + uint16_t mode = 0; + + git_parse_advance_chars(&ctx->parse_ctx, 1); + + if (parse_header_mode(&mode, ctx) < 0) + return -1; + + if (!patch->base.delta->new_file.mode) + patch->base.delta->new_file.mode = mode; + + if (!patch->base.delta->old_file.mode) + patch->base.delta->old_file.mode = mode; + } + + return 0; +} + +static int parse_header_git_oldmode( + git_patch_parsed *patch, git_patch_parse_ctx *ctx) +{ + return parse_header_mode(&patch->base.delta->old_file.mode, ctx); +} + +static int parse_header_git_newmode( + git_patch_parsed *patch, git_patch_parse_ctx *ctx) +{ + return parse_header_mode(&patch->base.delta->new_file.mode, ctx); +} + +static int parse_header_git_deletedfilemode( + git_patch_parsed *patch, + git_patch_parse_ctx *ctx) +{ + git__free((char *)patch->base.delta->new_file.path); + + patch->base.delta->new_file.path = NULL; + patch->base.delta->status = GIT_DELTA_DELETED; + patch->base.delta->nfiles = 1; + + return parse_header_mode(&patch->base.delta->old_file.mode, ctx); +} + +static int parse_header_git_newfilemode( + git_patch_parsed *patch, + git_patch_parse_ctx *ctx) +{ + git__free((char *)patch->base.delta->old_file.path); + + patch->base.delta->old_file.path = NULL; + patch->base.delta->status = GIT_DELTA_ADDED; + patch->base.delta->nfiles = 1; + + return parse_header_mode(&patch->base.delta->new_file.mode, ctx); +} + +static int parse_header_rename( + char **out, + git_patch_parse_ctx *ctx) +{ + git_str path = GIT_STR_INIT; + + if (parse_header_path_buf(&path, ctx, header_path_len(ctx)) < 0) + return -1; + + /* Note: the `rename from` and `rename to` lines include the literal + * filename. They do *not* include the prefix. (Who needs consistency?) + */ + *out = git_str_detach(&path); + return 0; +} + +static int parse_header_renamefrom( + git_patch_parsed *patch, git_patch_parse_ctx *ctx) +{ + patch->base.delta->status = GIT_DELTA_RENAMED; + return parse_header_rename(&patch->rename_old_path, ctx); +} + +static int parse_header_renameto( + git_patch_parsed *patch, git_patch_parse_ctx *ctx) +{ + patch->base.delta->status = GIT_DELTA_RENAMED; + return parse_header_rename(&patch->rename_new_path, ctx); +} + +static int parse_header_copyfrom( + git_patch_parsed *patch, git_patch_parse_ctx *ctx) +{ + patch->base.delta->status = GIT_DELTA_COPIED; + return parse_header_rename(&patch->rename_old_path, ctx); +} + +static int parse_header_copyto( + git_patch_parsed *patch, git_patch_parse_ctx *ctx) +{ + patch->base.delta->status = GIT_DELTA_COPIED; + return parse_header_rename(&patch->rename_new_path, ctx); +} + +static int parse_header_percent(uint16_t *out, git_patch_parse_ctx *ctx) +{ + int64_t val; + + if (git_parse_advance_digit(&val, &ctx->parse_ctx, 10) < 0) + return -1; + + if (git_parse_advance_expected_str(&ctx->parse_ctx, "%") < 0) + return -1; + + if (val < 0 || val > 100) + return -1; + + *out = (uint16_t)val; + return 0; +} + +static int parse_header_similarity( + git_patch_parsed *patch, git_patch_parse_ctx *ctx) +{ + if (parse_header_percent(&patch->base.delta->similarity, ctx) < 0) + return git_parse_err("invalid similarity percentage at line %"PRIuZ, + ctx->parse_ctx.line_num); + + return 0; +} + +static int parse_header_dissimilarity( + git_patch_parsed *patch, git_patch_parse_ctx *ctx) +{ + uint16_t dissimilarity; + + if (parse_header_percent(&dissimilarity, ctx) < 0) + return git_parse_err("invalid similarity percentage at line %"PRIuZ, + ctx->parse_ctx.line_num); + + patch->base.delta->similarity = 100 - dissimilarity; + + return 0; +} + +static int parse_header_start(git_patch_parsed *patch, git_patch_parse_ctx *ctx) +{ + if (parse_header_path(&patch->header_old_path, ctx) < 0) + return git_parse_err("corrupt old path in git diff header at line %"PRIuZ, + ctx->parse_ctx.line_num); + + if (git_parse_advance_ws(&ctx->parse_ctx) < 0 || + parse_header_path(&patch->header_new_path, ctx) < 0) + return git_parse_err("corrupt new path in git diff header at line %"PRIuZ, + ctx->parse_ctx.line_num); + + /* + * We cannot expect to be able to always parse paths correctly at this + * point. Due to the possibility of unquoted names, whitespaces in + * filenames and custom prefixes we have to allow that, though, and just + * proceed here. We then hope for the "---" and "+++" lines to fix that + * for us. + */ + if (!git_parse_ctx_contains(&ctx->parse_ctx, "\n", 1) && + !git_parse_ctx_contains(&ctx->parse_ctx, "\r\n", 2)) { + git_parse_advance_chars(&ctx->parse_ctx, ctx->parse_ctx.line_len - 1); + + git__free(patch->header_old_path); + patch->header_old_path = NULL; + git__free(patch->header_new_path); + patch->header_new_path = NULL; + } + + return 0; +} + +typedef enum { + STATE_START, + + STATE_DIFF, + STATE_FILEMODE, + STATE_MODE, + STATE_INDEX, + STATE_PATH, + + STATE_SIMILARITY, + STATE_RENAME, + STATE_COPY, + + STATE_END +} parse_header_state; + +typedef struct { + const char *str; + parse_header_state expected_state; + parse_header_state next_state; + int(*fn)(git_patch_parsed *, git_patch_parse_ctx *); +} parse_header_transition; + +static const parse_header_transition transitions[] = { + /* Start */ + { "diff --git " , STATE_START, STATE_DIFF, parse_header_start }, + + { "deleted file mode " , STATE_DIFF, STATE_FILEMODE, parse_header_git_deletedfilemode }, + { "new file mode " , STATE_DIFF, STATE_FILEMODE, parse_header_git_newfilemode }, + { "old mode " , STATE_DIFF, STATE_MODE, parse_header_git_oldmode }, + { "new mode " , STATE_MODE, STATE_END, parse_header_git_newmode }, + + { "index " , STATE_FILEMODE, STATE_INDEX, parse_header_git_index }, + { "index " , STATE_DIFF, STATE_INDEX, parse_header_git_index }, + { "index " , STATE_END, STATE_INDEX, parse_header_git_index }, + + { "--- " , STATE_DIFF, STATE_PATH, parse_header_git_oldpath }, + { "--- " , STATE_INDEX, STATE_PATH, parse_header_git_oldpath }, + { "--- " , STATE_FILEMODE, STATE_PATH, parse_header_git_oldpath }, + { "+++ " , STATE_PATH, STATE_END, parse_header_git_newpath }, + { "GIT binary patch" , STATE_INDEX, STATE_END, NULL }, + { "Binary files " , STATE_INDEX, STATE_END, NULL }, + + { "similarity index " , STATE_END, STATE_SIMILARITY, parse_header_similarity }, + { "similarity index " , STATE_DIFF, STATE_SIMILARITY, parse_header_similarity }, + { "dissimilarity index ", STATE_DIFF, STATE_SIMILARITY, parse_header_dissimilarity }, + { "rename from " , STATE_SIMILARITY, STATE_RENAME, parse_header_renamefrom }, + { "rename old " , STATE_SIMILARITY, STATE_RENAME, parse_header_renamefrom }, + { "copy from " , STATE_SIMILARITY, STATE_COPY, parse_header_copyfrom }, + { "rename to " , STATE_RENAME, STATE_END, parse_header_renameto }, + { "rename new " , STATE_RENAME, STATE_END, parse_header_renameto }, + { "copy to " , STATE_COPY, STATE_END, parse_header_copyto }, + + /* Next patch */ + { "diff --git " , STATE_END, 0, NULL }, + { "@@ -" , STATE_END, 0, NULL }, + { "-- " , STATE_INDEX, 0, NULL }, + { "-- " , STATE_END, 0, NULL }, +}; + +static int parse_header_git( + git_patch_parsed *patch, + git_patch_parse_ctx *ctx) +{ + size_t i; + int error = 0; + parse_header_state state = STATE_START; + + /* Parse remaining header lines */ + for (; ctx->parse_ctx.remain_len > 0; git_parse_advance_line(&ctx->parse_ctx)) { + bool found = false; + + if (ctx->parse_ctx.line_len == 0 || ctx->parse_ctx.line[ctx->parse_ctx.line_len - 1] != '\n') + break; + + for (i = 0; i < ARRAY_SIZE(transitions); i++) { + const parse_header_transition *transition = &transitions[i]; + size_t op_len = strlen(transition->str); + + if (transition->expected_state != state || + git__prefixcmp(ctx->parse_ctx.line, transition->str) != 0) + continue; + + state = transition->next_state; + + /* Do not advance if this is the patch separator */ + if (transition->fn == NULL) + goto done; + + git_parse_advance_chars(&ctx->parse_ctx, op_len); + + if ((error = transition->fn(patch, ctx)) < 0) + goto done; + + git_parse_advance_ws(&ctx->parse_ctx); + + if (git_parse_advance_expected_str(&ctx->parse_ctx, "\n") < 0 || + ctx->parse_ctx.line_len > 0) { + error = git_parse_err("trailing data at line %"PRIuZ, ctx->parse_ctx.line_num); + goto done; + } + + found = true; + break; + } + + if (!found) { + error = git_parse_err("invalid patch header at line %"PRIuZ, + ctx->parse_ctx.line_num); + goto done; + } + } + + if (state != STATE_END) { + error = git_parse_err("unexpected header line %"PRIuZ, ctx->parse_ctx.line_num); + goto done; + } + +done: + return error; +} + +static int parse_int(int *out, git_patch_parse_ctx *ctx) +{ + int64_t num; + + if (git_parse_advance_digit(&num, &ctx->parse_ctx, 10) < 0 || !git__is_int(num)) + return -1; + + *out = (int)num; + return 0; +} + +static int parse_hunk_header( + git_patch_hunk *hunk, + git_patch_parse_ctx *ctx) +{ + const char *header_start = ctx->parse_ctx.line; + char c; + + hunk->hunk.old_lines = 1; + hunk->hunk.new_lines = 1; + + if (git_parse_advance_expected_str(&ctx->parse_ctx, "@@ -") < 0 || + parse_int(&hunk->hunk.old_start, ctx) < 0) + goto fail; + + if (git_parse_peek(&c, &ctx->parse_ctx, 0) == 0 && c == ',') { + if (git_parse_advance_expected_str(&ctx->parse_ctx, ",") < 0 || + parse_int(&hunk->hunk.old_lines, ctx) < 0) + goto fail; + } + + if (git_parse_advance_expected_str(&ctx->parse_ctx, " +") < 0 || + parse_int(&hunk->hunk.new_start, ctx) < 0) + goto fail; + + if (git_parse_peek(&c, &ctx->parse_ctx, 0) == 0 && c == ',') { + if (git_parse_advance_expected_str(&ctx->parse_ctx, ",") < 0 || + parse_int(&hunk->hunk.new_lines, ctx) < 0) + goto fail; + } + + if (git_parse_advance_expected_str(&ctx->parse_ctx, " @@") < 0) + goto fail; + + git_parse_advance_line(&ctx->parse_ctx); + + if (!hunk->hunk.old_lines && !hunk->hunk.new_lines) + goto fail; + + hunk->hunk.header_len = ctx->parse_ctx.line - header_start; + if (hunk->hunk.header_len > (GIT_DIFF_HUNK_HEADER_SIZE - 1)) + return git_parse_err("oversized patch hunk header at line %"PRIuZ, + ctx->parse_ctx.line_num); + + memcpy(hunk->hunk.header, header_start, hunk->hunk.header_len); + hunk->hunk.header[hunk->hunk.header_len] = '\0'; + + return 0; + +fail: + git_error_set(GIT_ERROR_PATCH, "invalid patch hunk header at line %"PRIuZ, + ctx->parse_ctx.line_num); + return -1; +} + +static int eof_for_origin(int origin) { + if (origin == GIT_DIFF_LINE_ADDITION) + return GIT_DIFF_LINE_DEL_EOFNL; + if (origin == GIT_DIFF_LINE_DELETION) + return GIT_DIFF_LINE_ADD_EOFNL; + return GIT_DIFF_LINE_CONTEXT_EOFNL; +} + +static int parse_hunk_body( + git_patch_parsed *patch, + git_patch_hunk *hunk, + git_patch_parse_ctx *ctx) +{ + git_diff_line *line; + int error = 0; + + int oldlines = hunk->hunk.old_lines; + int newlines = hunk->hunk.new_lines; + int last_origin = 0; + + for (; + ctx->parse_ctx.remain_len > 1 && + (oldlines || newlines) && + !git_parse_ctx_contains_s(&ctx->parse_ctx, "@@ -"); + git_parse_advance_line(&ctx->parse_ctx)) { + + int old_lineno, new_lineno, origin, prefix = 1; + char c; + + if (git__add_int_overflow(&old_lineno, hunk->hunk.old_start, hunk->hunk.old_lines) || + git__sub_int_overflow(&old_lineno, old_lineno, oldlines) || + git__add_int_overflow(&new_lineno, hunk->hunk.new_start, hunk->hunk.new_lines) || + git__sub_int_overflow(&new_lineno, new_lineno, newlines)) { + error = git_parse_err("unrepresentable line count at line %"PRIuZ, + ctx->parse_ctx.line_num); + goto done; + } + + if (ctx->parse_ctx.line_len == 0 || ctx->parse_ctx.line[ctx->parse_ctx.line_len - 1] != '\n') { + error = git_parse_err("invalid patch instruction at line %"PRIuZ, + ctx->parse_ctx.line_num); + goto done; + } + + git_parse_peek(&c, &ctx->parse_ctx, 0); + + switch (c) { + case '\n': + prefix = 0; + /* fall through */ + + case ' ': + origin = GIT_DIFF_LINE_CONTEXT; + oldlines--; + newlines--; + break; + + case '-': + origin = GIT_DIFF_LINE_DELETION; + oldlines--; + new_lineno = -1; + break; + + case '+': + origin = GIT_DIFF_LINE_ADDITION; + newlines--; + old_lineno = -1; + break; + + case '\\': + /* + * If there are no oldlines left, then this is probably + * the "\ No newline at end of file" marker. Do not + * verify its format, as it may be localized. + */ + if (!oldlines) { + prefix = 0; + origin = eof_for_origin(last_origin); + old_lineno = -1; + new_lineno = -1; + break; + } + /* fall through */ + + default: + error = git_parse_err("invalid patch hunk at line %"PRIuZ, ctx->parse_ctx.line_num); + goto done; + } + + line = git_array_alloc(patch->base.lines); + GIT_ERROR_CHECK_ALLOC(line); + + memset(line, 0x0, sizeof(git_diff_line)); + + line->content_len = ctx->parse_ctx.line_len - prefix; + line->content = git__strndup(ctx->parse_ctx.line + prefix, line->content_len); + GIT_ERROR_CHECK_ALLOC(line->content); + line->content_offset = ctx->parse_ctx.content_len - ctx->parse_ctx.remain_len; + line->origin = origin; + line->num_lines = 1; + line->old_lineno = old_lineno; + line->new_lineno = new_lineno; + + hunk->line_count++; + + last_origin = origin; + } + + if (oldlines || newlines) { + error = git_parse_err( + "invalid patch hunk, expected %d old lines and %d new lines", + hunk->hunk.old_lines, hunk->hunk.new_lines); + goto done; + } + + /* + * Handle "\ No newline at end of file". Only expect the leading + * backslash, though, because the rest of the string could be + * localized. Because `diff` optimizes for the case where you + * want to apply the patch by hand. + */ + if (git_parse_ctx_contains_s(&ctx->parse_ctx, "\\ ") && + git_array_size(patch->base.lines) > 0) { + + line = git_array_get(patch->base.lines, git_array_size(patch->base.lines) - 1); + + if (line->content_len < 1) { + error = git_parse_err("last line has no trailing newline"); + goto done; + } + + line = git_array_alloc(patch->base.lines); + GIT_ERROR_CHECK_ALLOC(line); + + memset(line, 0x0, sizeof(git_diff_line)); + + line->content_len = ctx->parse_ctx.line_len; + line->content = git__strndup(ctx->parse_ctx.line, line->content_len); + GIT_ERROR_CHECK_ALLOC(line->content); + line->content_offset = ctx->parse_ctx.content_len - ctx->parse_ctx.remain_len; + line->origin = eof_for_origin(last_origin); + line->num_lines = 1; + line->old_lineno = -1; + line->new_lineno = -1; + + hunk->line_count++; + + git_parse_advance_line(&ctx->parse_ctx); + } + +done: + return error; +} + +static int parse_patch_header( + git_patch_parsed *patch, + git_patch_parse_ctx *ctx) +{ + int error = 0; + + for (; ctx->parse_ctx.remain_len > 0; git_parse_advance_line(&ctx->parse_ctx)) { + /* This line is too short to be a patch header. */ + if (ctx->parse_ctx.line_len < 6) + continue; + + /* This might be a hunk header without a patch header, provide a + * sensible error message. */ + if (git_parse_ctx_contains_s(&ctx->parse_ctx, "@@ -")) { + size_t line_num = ctx->parse_ctx.line_num; + git_patch_hunk hunk; + + /* If this cannot be parsed as a hunk header, it's just leading + * noise, continue. + */ + if (parse_hunk_header(&hunk, ctx) < 0) { + git_error_clear(); + continue; + } + + error = git_parse_err("invalid hunk header outside patch at line %"PRIuZ, + line_num); + goto done; + } + + /* This buffer is too short to contain a patch. */ + if (ctx->parse_ctx.remain_len < ctx->parse_ctx.line_len + 6) + break; + + /* A proper git patch */ + if (git_parse_ctx_contains_s(&ctx->parse_ctx, "diff --git ")) { + error = parse_header_git(patch, ctx); + goto done; + } + + error = 0; + continue; + } + + git_error_set(GIT_ERROR_PATCH, "no patch found"); + error = GIT_ENOTFOUND; + +done: + return error; +} + +static int parse_patch_binary_side( + git_diff_binary_file *binary, + git_patch_parse_ctx *ctx) +{ + git_diff_binary_t type = GIT_DIFF_BINARY_NONE; + git_str base85 = GIT_STR_INIT, decoded = GIT_STR_INIT; + int64_t len; + int error = 0; + + if (git_parse_ctx_contains_s(&ctx->parse_ctx, "literal ")) { + type = GIT_DIFF_BINARY_LITERAL; + git_parse_advance_chars(&ctx->parse_ctx, 8); + } else if (git_parse_ctx_contains_s(&ctx->parse_ctx, "delta ")) { + type = GIT_DIFF_BINARY_DELTA; + git_parse_advance_chars(&ctx->parse_ctx, 6); + } else { + error = git_parse_err( + "unknown binary delta type at line %"PRIuZ, ctx->parse_ctx.line_num); + goto done; + } + + if (git_parse_advance_digit(&len, &ctx->parse_ctx, 10) < 0 || + git_parse_advance_nl(&ctx->parse_ctx) < 0 || len < 0) { + error = git_parse_err("invalid binary size at line %"PRIuZ, ctx->parse_ctx.line_num); + goto done; + } + + while (ctx->parse_ctx.line_len) { + char c; + size_t encoded_len, decoded_len = 0, decoded_orig = decoded.size; + + git_parse_peek(&c, &ctx->parse_ctx, 0); + + if (c == '\n') + break; + else if (c >= 'A' && c <= 'Z') + decoded_len = c - 'A' + 1; + else if (c >= 'a' && c <= 'z') + decoded_len = c - 'a' + (('z' - 'a') + 1) + 1; + + if (!decoded_len) { + error = git_parse_err("invalid binary length at line %"PRIuZ, ctx->parse_ctx.line_num); + goto done; + } + + git_parse_advance_chars(&ctx->parse_ctx, 1); + + encoded_len = ((decoded_len / 4) + !!(decoded_len % 4)) * 5; + + if (!encoded_len || !ctx->parse_ctx.line_len || encoded_len > ctx->parse_ctx.line_len - 1) { + error = git_parse_err("truncated binary data at line %"PRIuZ, ctx->parse_ctx.line_num); + goto done; + } + + if ((error = git_str_decode_base85( + &decoded, ctx->parse_ctx.line, encoded_len, decoded_len)) < 0) + goto done; + + if (decoded.size - decoded_orig != decoded_len) { + error = git_parse_err("truncated binary data at line %"PRIuZ, ctx->parse_ctx.line_num); + goto done; + } + + git_parse_advance_chars(&ctx->parse_ctx, encoded_len); + + if (git_parse_advance_nl(&ctx->parse_ctx) < 0) { + error = git_parse_err("trailing data at line %"PRIuZ, ctx->parse_ctx.line_num); + goto done; + } + } + + binary->type = type; + binary->inflatedlen = (size_t)len; + binary->datalen = decoded.size; + binary->data = git_str_detach(&decoded); + +done: + git_str_dispose(&base85); + git_str_dispose(&decoded); + return error; +} + +static int parse_patch_binary( + git_patch_parsed *patch, + git_patch_parse_ctx *ctx) +{ + int error; + + if (git_parse_advance_expected_str(&ctx->parse_ctx, "GIT binary patch") < 0 || + git_parse_advance_nl(&ctx->parse_ctx) < 0) + return git_parse_err("corrupt git binary header at line %"PRIuZ, ctx->parse_ctx.line_num); + + /* parse old->new binary diff */ + if ((error = parse_patch_binary_side( + &patch->base.binary.new_file, ctx)) < 0) + return error; + + if (git_parse_advance_nl(&ctx->parse_ctx) < 0) + return git_parse_err("corrupt git binary separator at line %"PRIuZ, + ctx->parse_ctx.line_num); + + /* parse new->old binary diff */ + if ((error = parse_patch_binary_side( + &patch->base.binary.old_file, ctx)) < 0) + return error; + + if (git_parse_advance_nl(&ctx->parse_ctx) < 0) + return git_parse_err("corrupt git binary patch separator at line %"PRIuZ, + ctx->parse_ctx.line_num); + + patch->base.binary.contains_data = 1; + patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY; + return 0; +} + +static int parse_patch_binary_nodata( + git_patch_parsed *patch, + git_patch_parse_ctx *ctx) +{ + const char *old = patch->old_path ? patch->old_path : patch->header_old_path; + const char *new = patch->new_path ? patch->new_path : patch->header_new_path; + + if (!old || !new) + return git_parse_err("corrupt binary data without paths at line %"PRIuZ, ctx->parse_ctx.line_num); + + if (patch->base.delta->status == GIT_DELTA_ADDED) + old = "/dev/null"; + else if (patch->base.delta->status == GIT_DELTA_DELETED) + new = "/dev/null"; + + if (git_parse_advance_expected_str(&ctx->parse_ctx, "Binary files ") < 0 || + git_parse_advance_expected_str(&ctx->parse_ctx, old) < 0 || + git_parse_advance_expected_str(&ctx->parse_ctx, " and ") < 0 || + git_parse_advance_expected_str(&ctx->parse_ctx, new) < 0 || + git_parse_advance_expected_str(&ctx->parse_ctx, " differ") < 0 || + git_parse_advance_nl(&ctx->parse_ctx) < 0) + return git_parse_err("corrupt git binary header at line %"PRIuZ, ctx->parse_ctx.line_num); + + patch->base.binary.contains_data = 0; + patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY; + return 0; +} + +static int parse_patch_hunks( + git_patch_parsed *patch, + git_patch_parse_ctx *ctx) +{ + git_patch_hunk *hunk; + int error = 0; + + while (git_parse_ctx_contains_s(&ctx->parse_ctx, "@@ -")) { + hunk = git_array_alloc(patch->base.hunks); + GIT_ERROR_CHECK_ALLOC(hunk); + + memset(hunk, 0, sizeof(git_patch_hunk)); + + hunk->line_start = git_array_size(patch->base.lines); + hunk->line_count = 0; + + if ((error = parse_hunk_header(hunk, ctx)) < 0 || + (error = parse_hunk_body(patch, hunk, ctx)) < 0) + goto done; + } + + patch->base.delta->flags |= GIT_DIFF_FLAG_NOT_BINARY; + +done: + return error; +} + +static int parse_patch_body( + git_patch_parsed *patch, git_patch_parse_ctx *ctx) +{ + if (git_parse_ctx_contains_s(&ctx->parse_ctx, "GIT binary patch")) + return parse_patch_binary(patch, ctx); + else if (git_parse_ctx_contains_s(&ctx->parse_ctx, "Binary files ")) + return parse_patch_binary_nodata(patch, ctx); + else + return parse_patch_hunks(patch, ctx); +} + +static int check_header_names( + const char *one, + const char *two, + const char *old_or_new, + bool two_null) +{ + if (!one || !two) + return 0; + + if (two_null && strcmp(two, "/dev/null") != 0) + return git_parse_err("expected %s path of '/dev/null'", old_or_new); + + else if (!two_null && strcmp(one, two) != 0) + return git_parse_err("mismatched %s path names", old_or_new); + + return 0; +} + +static int check_prefix( + char **out, + size_t *out_len, + git_patch_parsed *patch, + const char *path_start) +{ + const char *path = path_start; + size_t prefix_len = patch->ctx->opts.prefix_len; + size_t remain_len = prefix_len; + + *out = NULL; + *out_len = 0; + + if (prefix_len == 0) + goto done; + + /* leading slashes do not count as part of the prefix in git apply */ + while (*path == '/') + path++; + + while (*path && remain_len) { + if (*path == '/') + remain_len--; + + path++; + } + + if (remain_len || !*path) + return git_parse_err( + "header filename does not contain %"PRIuZ" path components", + prefix_len); + +done: + *out_len = (path - path_start); + *out = git__strndup(path_start, *out_len); + + return (*out == NULL) ? -1 : 0; +} + +static int check_filenames(git_patch_parsed *patch) +{ + const char *prefixed_new, *prefixed_old; + size_t old_prefixlen = 0, new_prefixlen = 0; + bool added = (patch->base.delta->status == GIT_DELTA_ADDED); + bool deleted = (patch->base.delta->status == GIT_DELTA_DELETED); + + if (patch->old_path && !patch->new_path) + return git_parse_err("missing new path"); + + if (!patch->old_path && patch->new_path) + return git_parse_err("missing old path"); + + /* Ensure (non-renamed) paths match */ + if (check_header_names(patch->header_old_path, patch->old_path, "old", added) < 0 || + check_header_names(patch->header_new_path, patch->new_path, "new", deleted) < 0) + return -1; + + prefixed_old = (!added && patch->old_path) ? patch->old_path : patch->header_old_path; + prefixed_new = (!deleted && patch->new_path) ? patch->new_path : patch->header_new_path; + + if ((prefixed_old && check_prefix(&patch->old_prefix, &old_prefixlen, patch, prefixed_old) < 0) || + (prefixed_new && check_prefix(&patch->new_prefix, &new_prefixlen, patch, prefixed_new) < 0)) + return -1; + + /* Prefer the rename filenames as they are unambiguous and unprefixed */ + if (patch->rename_old_path) + patch->base.delta->old_file.path = patch->rename_old_path; + else if (prefixed_old) + patch->base.delta->old_file.path = prefixed_old + old_prefixlen; + else + patch->base.delta->old_file.path = NULL; + + if (patch->rename_new_path) + patch->base.delta->new_file.path = patch->rename_new_path; + else if (prefixed_new) + patch->base.delta->new_file.path = prefixed_new + new_prefixlen; + else + patch->base.delta->new_file.path = NULL; + + if (!patch->base.delta->old_file.path && + !patch->base.delta->new_file.path) + return git_parse_err("git diff header lacks old / new paths"); + + return 0; +} + +static int check_patch(git_patch_parsed *patch) +{ + git_diff_delta *delta = patch->base.delta; + + if (check_filenames(patch) < 0) + return -1; + + if (delta->old_file.path && + delta->status != GIT_DELTA_DELETED && + !delta->new_file.mode) + delta->new_file.mode = delta->old_file.mode; + + if (delta->status == GIT_DELTA_MODIFIED && + !(delta->flags & GIT_DIFF_FLAG_BINARY) && + delta->new_file.mode == delta->old_file.mode && + git_array_size(patch->base.hunks) == 0) + return git_parse_err("patch with no hunks"); + + if (delta->status == GIT_DELTA_ADDED) { + git_oid_clear(&delta->old_file.id, + patch->base.diff_opts.oid_type); + delta->old_file.id_abbrev = 0; + } + + if (delta->status == GIT_DELTA_DELETED) { + git_oid_clear(&delta->new_file.id, + patch->base.diff_opts.oid_type); + delta->new_file.id_abbrev = 0; + } + + return 0; +} + +git_patch_parse_ctx *git_patch_parse_ctx_init( + const char *content, + size_t content_len, + const git_patch_options *opts) +{ + git_patch_parse_ctx *ctx; + git_patch_options default_opts = GIT_PATCH_OPTIONS_INIT; + + if ((ctx = git__calloc(1, sizeof(git_patch_parse_ctx))) == NULL) + return NULL; + + if ((git_parse_ctx_init(&ctx->parse_ctx, content, content_len)) < 0) { + git__free(ctx); + return NULL; + } + + if (opts) + memcpy(&ctx->opts, opts, sizeof(git_patch_options)); + else + memcpy(&ctx->opts, &default_opts, sizeof(git_patch_options)); + + GIT_REFCOUNT_INC(ctx); + return ctx; +} + +static void patch_parse_ctx_free(git_patch_parse_ctx *ctx) +{ + if (!ctx) + return; + + git_parse_ctx_clear(&ctx->parse_ctx); + git__free(ctx); +} + +void git_patch_parse_ctx_free(git_patch_parse_ctx *ctx) +{ + GIT_REFCOUNT_DEC(ctx, patch_parse_ctx_free); +} + +int git_patch_parsed_from_diff(git_patch **out, git_diff *d, size_t idx) +{ + git_diff_parsed *diff = (git_diff_parsed *)d; + git_patch *p; + + if ((p = git_vector_get(&diff->patches, idx)) == NULL) + return -1; + + GIT_REFCOUNT_INC(p); + *out = p; + + return 0; +} + +static void patch_parsed__free(git_patch *p) +{ + git_patch_parsed *patch = (git_patch_parsed *)p; + git_diff_line *line; + size_t i; + + if (!patch) + return; + + git_patch_parse_ctx_free(patch->ctx); + + git__free((char *)patch->base.binary.old_file.data); + git__free((char *)patch->base.binary.new_file.data); + git_array_clear(patch->base.hunks); + git_array_foreach(patch->base.lines, i, line) + git__free((char *) line->content); + git_array_clear(patch->base.lines); + git__free(patch->base.delta); + + git__free(patch->old_prefix); + git__free(patch->new_prefix); + git__free(patch->header_old_path); + git__free(patch->header_new_path); + git__free(patch->rename_old_path); + git__free(patch->rename_new_path); + git__free(patch->old_path); + git__free(patch->new_path); + git__free(patch); +} + +int git_patch_parse( + git_patch **out, + git_patch_parse_ctx *ctx) +{ + git_patch_parsed *patch; + size_t start, used; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(ctx); + + *out = NULL; + + patch = git__calloc(1, sizeof(git_patch_parsed)); + GIT_ERROR_CHECK_ALLOC(patch); + + patch->ctx = ctx; + GIT_REFCOUNT_INC(patch->ctx); + + patch->base.free_fn = patch_parsed__free; + + patch->base.delta = git__calloc(1, sizeof(git_diff_delta)); + GIT_ERROR_CHECK_ALLOC(patch->base.delta); + + patch->base.delta->status = GIT_DELTA_MODIFIED; + patch->base.delta->nfiles = 2; + + patch->base.diff_opts.oid_type = ctx->opts.oid_type; + + start = ctx->parse_ctx.remain_len; + + if ((error = parse_patch_header(patch, ctx)) < 0 || + (error = parse_patch_body(patch, ctx)) < 0 || + (error = check_patch(patch)) < 0) + goto done; + + used = start - ctx->parse_ctx.remain_len; + ctx->parse_ctx.remain += used; + + patch->base.diff_opts.old_prefix = patch->old_prefix; + patch->base.diff_opts.new_prefix = patch->new_prefix; + patch->base.diff_opts.flags |= GIT_DIFF_SHOW_BINARY; + + GIT_REFCOUNT_INC(&patch->base); + *out = &patch->base; + +done: + if (error < 0) + patch_parsed__free(&patch->base); + + return error; +} + +int git_patch_from_buffer( + git_patch **out, + const char *content, + size_t content_len, + const git_patch_options *opts) +{ + git_patch_parse_ctx *ctx; + int error; + + ctx = git_patch_parse_ctx_init(content, content_len, opts); + GIT_ERROR_CHECK_ALLOC(ctx); + + error = git_patch_parse(out, ctx); + + git_patch_parse_ctx_free(ctx); + return error; +} + diff --git a/src/libgit2/patch_parse.h b/src/libgit2/patch_parse.h new file mode 100644 index 00000000000..140629da857 --- /dev/null +++ b/src/libgit2/patch_parse.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_patch_parse_h__ +#define INCLUDE_patch_parse_h__ + +#include "common.h" + +#include "parse.h" +#include "patch.h" + +typedef struct { + git_refcount rc; + + git_patch_options opts; + + git_parse_ctx parse_ctx; +} git_patch_parse_ctx; + +extern git_patch_parse_ctx *git_patch_parse_ctx_init( + const char *content, + size_t content_len, + const git_patch_options *opts); + +extern void git_patch_parse_ctx_free(git_patch_parse_ctx *ctx); + +/** + * Create a patch for a single file from the contents of a patch buffer. + * + * @param out The patch to be created + * @param contents The contents of a patch file + * @param contents_len The length of the patch file + * @param opts The git_patch_options + * @return 0 on success, <0 on failure. + */ +extern int git_patch_from_buffer( + git_patch **out, + const char *contents, + size_t contents_len, + const git_patch_options *opts); + +extern int git_patch_parse( + git_patch **out, + git_patch_parse_ctx *ctx); + +extern int git_patch_parsed_from_diff(git_patch **, git_diff *, size_t); + +#endif diff --git a/src/libgit2/path.c b/src/libgit2/path.c new file mode 100644 index 00000000000..4b584fb8056 --- /dev/null +++ b/src/libgit2/path.c @@ -0,0 +1,375 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "path.h" + +#include "repository.h" +#include "fs_path.h" +#include "utf8.h" + +typedef struct { + git_repository *repo; + uint16_t file_mode; + unsigned int flags; +} repository_path_validate_data; + +static int32_t next_hfs_char(const char **in, size_t *len) +{ + while (*len) { + uint32_t codepoint; + int cp_len = git_utf8_iterate(&codepoint, *in, *len); + if (cp_len < 0) + return -1; + + (*in) += cp_len; + (*len) -= cp_len; + + /* these code points are ignored completely */ + switch (codepoint) { + case 0x200c: /* ZERO WIDTH NON-JOINER */ + case 0x200d: /* ZERO WIDTH JOINER */ + case 0x200e: /* LEFT-TO-RIGHT MARK */ + case 0x200f: /* RIGHT-TO-LEFT MARK */ + case 0x202a: /* LEFT-TO-RIGHT EMBEDDING */ + case 0x202b: /* RIGHT-TO-LEFT EMBEDDING */ + case 0x202c: /* POP DIRECTIONAL FORMATTING */ + case 0x202d: /* LEFT-TO-RIGHT OVERRIDE */ + case 0x202e: /* RIGHT-TO-LEFT OVERRIDE */ + case 0x206a: /* INHIBIT SYMMETRIC SWAPPING */ + case 0x206b: /* ACTIVATE SYMMETRIC SWAPPING */ + case 0x206c: /* INHIBIT ARABIC FORM SHAPING */ + case 0x206d: /* ACTIVATE ARABIC FORM SHAPING */ + case 0x206e: /* NATIONAL DIGIT SHAPES */ + case 0x206f: /* NOMINAL DIGIT SHAPES */ + case 0xfeff: /* ZERO WIDTH NO-BREAK SPACE */ + continue; + } + + /* fold into lowercase -- this will only fold characters in + * the ASCII range, which is perfectly fine, because the + * git folder name can only be composed of ascii characters + */ + return git__tolower((int)codepoint); + } + return 0; /* NULL byte -- end of string */ +} + +static bool validate_dotgit_hfs_generic( + const char *path, + size_t len, + const char *needle, + size_t needle_len) +{ + size_t i; + char c; + + if (next_hfs_char(&path, &len) != '.') + return true; + + for (i = 0; i < needle_len; i++) { + c = next_hfs_char(&path, &len); + if (c != needle[i]) + return true; + } + + if (next_hfs_char(&path, &len) != '\0') + return true; + + return false; +} + +static bool validate_dotgit_hfs(const char *path, size_t len) +{ + return validate_dotgit_hfs_generic(path, len, "git", CONST_STRLEN("git")); +} + +GIT_INLINE(bool) validate_dotgit_ntfs( + git_repository *repo, + const char *path, + size_t len) +{ + git_str *reserved = git_repository__reserved_names_win32; + size_t reserved_len = git_repository__reserved_names_win32_len; + size_t start = 0, i; + + if (repo) + git_repository__reserved_names(&reserved, &reserved_len, repo, true); + + for (i = 0; i < reserved_len; i++) { + git_str *r = &reserved[i]; + + if (len >= r->size && + strncasecmp(path, r->ptr, r->size) == 0) { + start = r->size; + break; + } + } + + if (!start) + return true; + + /* + * Reject paths that start with Windows-style directory separators + * (".git\") or NTFS alternate streams (".git:") and could be used + * to write to the ".git" directory on Windows platforms. + */ + if (path[start] == '\\' || path[start] == ':') + return false; + + /* Reject paths like '.git ' or '.git.' */ + for (i = start; i < len; i++) { + if (path[i] != ' ' && path[i] != '.') + return true; + } + + return false; +} + +/* + * Windows paths that end with spaces and/or dots are elided to the + * path without them for backward compatibility. That is to say + * that opening file "foo ", "foo." or even "foo . . ." will all + * map to a filename of "foo". This function identifies spaces and + * dots at the end of a filename, whether the proper end of the + * filename (end of string) or a colon (which would indicate a + * Windows alternate data stream.) + */ +GIT_INLINE(bool) ntfs_end_of_filename(const char *path) +{ + const char *c = path; + + for (;; c++) { + if (*c == '\0' || *c == ':') + return true; + if (*c != ' ' && *c != '.') + return false; + } + + return true; +} + +GIT_INLINE(bool) validate_dotgit_ntfs_generic( + const char *name, + size_t len, + const char *dotgit_name, + size_t dotgit_len, + const char *shortname_pfix) +{ + int i, saw_tilde; + + if (name[0] == '.' && len >= dotgit_len && + !strncasecmp(name + 1, dotgit_name, dotgit_len)) { + return !ntfs_end_of_filename(name + dotgit_len + 1); + } + + /* Detect the basic NTFS shortname with the first six chars */ + if (!strncasecmp(name, dotgit_name, 6) && name[6] == '~' && + name[7] >= '1' && name[7] <= '4') + return !ntfs_end_of_filename(name + 8); + + /* Catch fallback names */ + for (i = 0, saw_tilde = 0; i < 8; i++) { + if (name[i] == '\0') { + return true; + } else if (saw_tilde) { + if (name[i] < '0' || name[i] > '9') + return true; + } else if (name[i] == '~') { + if (name[i+1] < '1' || name[i+1] > '9') + return true; + saw_tilde = 1; + } else if (i >= 6) { + return true; + } else if ((unsigned char)name[i] > 127) { + return true; + } else if (git__tolower(name[i]) != shortname_pfix[i]) { + return true; + } + } + + return !ntfs_end_of_filename(name + i); +} + +/* + * Return the length of the common prefix between str and prefix, comparing them + * case-insensitively (must be ASCII to match). + */ +GIT_INLINE(size_t) common_prefix_icase(const char *str, size_t len, const char *prefix) +{ + size_t count = 0; + + while (len > 0 && git__tolower(*str) == git__tolower(*prefix)) { + count++; + str++; + prefix++; + len--; + } + + return count; +} + +static bool validate_repo_component( + const char *component, + size_t len, + void *payload) +{ + repository_path_validate_data *data = (repository_path_validate_data *)payload; + + if (data->flags & GIT_PATH_REJECT_DOT_GIT_HFS) { + if (!validate_dotgit_hfs(component, len)) + return false; + + if (S_ISLNK(data->file_mode) && + git_path_is_gitfile(component, len, GIT_PATH_GITFILE_GITMODULES, GIT_PATH_FS_HFS)) + return false; + } + + if (data->flags & GIT_PATH_REJECT_DOT_GIT_NTFS) { + if (!validate_dotgit_ntfs(data->repo, component, len)) + return false; + + if (S_ISLNK(data->file_mode) && + git_path_is_gitfile(component, len, GIT_PATH_GITFILE_GITMODULES, GIT_PATH_FS_NTFS)) + return false; + } + + /* don't bother rerunning the `.git` test if we ran the HFS or NTFS + * specific tests, they would have already rejected `.git`. + */ + if ((data->flags & GIT_PATH_REJECT_DOT_GIT_HFS) == 0 && + (data->flags & GIT_PATH_REJECT_DOT_GIT_NTFS) == 0 && + (data->flags & GIT_PATH_REJECT_DOT_GIT_LITERAL)) { + if (len >= 4 && + component[0] == '.' && + (component[1] == 'g' || component[1] == 'G') && + (component[2] == 'i' || component[2] == 'I') && + (component[3] == 't' || component[3] == 'T')) { + if (len == 4) + return false; + + if (S_ISLNK(data->file_mode) && + common_prefix_icase(component, len, ".gitmodules") == len) + return false; + } + } + + return true; +} + +GIT_INLINE(unsigned int) dotgit_flags( + git_repository *repo, + unsigned int flags) +{ + int protectHFS = 0, protectNTFS = 1; + int error = 0; + + flags |= GIT_PATH_REJECT_DOT_GIT_LITERAL; + +#ifdef __APPLE__ + protectHFS = 1; +#endif + + if (repo && !protectHFS) + error = git_repository__configmap_lookup(&protectHFS, repo, GIT_CONFIGMAP_PROTECTHFS); + if (!error && protectHFS) + flags |= GIT_PATH_REJECT_DOT_GIT_HFS; + + if (repo) + error = git_repository__configmap_lookup(&protectNTFS, repo, GIT_CONFIGMAP_PROTECTNTFS); + if (!error && protectNTFS) + flags |= GIT_PATH_REJECT_DOT_GIT_NTFS; + + return flags; +} + +GIT_INLINE(unsigned int) length_flags( + git_repository *repo, + unsigned int flags) +{ +#ifdef GIT_WIN32 + int allow = 0; + + if (repo && + git_repository__configmap_lookup(&allow, repo, GIT_CONFIGMAP_LONGPATHS) < 0) + allow = 0; + + if (allow) + flags &= ~GIT_FS_PATH_REJECT_LONG_PATHS; + +#else + GIT_UNUSED(repo); + flags &= ~GIT_FS_PATH_REJECT_LONG_PATHS; +#endif + + return flags; +} + +bool git_path_str_is_valid( + git_repository *repo, + const git_str *path, + uint16_t file_mode, + unsigned int flags) +{ + repository_path_validate_data data = {0}; + + /* Upgrade the ".git" checks based on platform */ + if ((flags & GIT_PATH_REJECT_DOT_GIT)) + flags = dotgit_flags(repo, flags); + + /* Update the length checks based on platform */ + if ((flags & GIT_FS_PATH_REJECT_LONG_PATHS)) + flags = length_flags(repo, flags); + + data.repo = repo; + data.file_mode = file_mode; + data.flags = flags; + + return git_fs_path_str_is_valid_ext(path, flags, NULL, validate_repo_component, NULL, &data); +} + +static const struct { + const char *file; + const char *hash; + size_t filelen; +} gitfiles[] = { + { "gitignore", "gi250a", CONST_STRLEN("gitignore") }, + { "gitmodules", "gi7eba", CONST_STRLEN("gitmodules") }, + { "gitattributes", "gi7d29", CONST_STRLEN("gitattributes") } +}; + +extern int git_path_is_gitfile( + const char *path, + size_t pathlen, + git_path_gitfile gitfile, + git_path_fs fs) +{ + const char *file, *hash; + size_t filelen; + + if (!(gitfile >= GIT_PATH_GITFILE_GITIGNORE && gitfile < ARRAY_SIZE(gitfiles))) { + git_error_set(GIT_ERROR_OS, "invalid gitfile for path validation"); + return -1; + } + + file = gitfiles[gitfile].file; + filelen = gitfiles[gitfile].filelen; + hash = gitfiles[gitfile].hash; + + switch (fs) { + case GIT_PATH_FS_GENERIC: + return !validate_dotgit_ntfs_generic(path, pathlen, file, filelen, hash) || + !validate_dotgit_hfs_generic(path, pathlen, file, filelen); + case GIT_PATH_FS_NTFS: + return !validate_dotgit_ntfs_generic(path, pathlen, file, filelen, hash); + case GIT_PATH_FS_HFS: + return !validate_dotgit_hfs_generic(path, pathlen, file, filelen); + default: + git_error_set(GIT_ERROR_OS, "invalid filesystem for path validation"); + return -1; + } +} + diff --git a/src/libgit2/path.h b/src/libgit2/path.h new file mode 100644 index 00000000000..c4a2c425021 --- /dev/null +++ b/src/libgit2/path.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_path_h__ +#define INCLUDE_path_h__ + +#include "common.h" + +#include "fs_path.h" +#include + +#define GIT_PATH_REJECT_DOT_GIT (GIT_FS_PATH_REJECT_MAX << 1) +#define GIT_PATH_REJECT_DOT_GIT_LITERAL (GIT_FS_PATH_REJECT_MAX << 2) +#define GIT_PATH_REJECT_DOT_GIT_HFS (GIT_FS_PATH_REJECT_MAX << 3) +#define GIT_PATH_REJECT_DOT_GIT_NTFS (GIT_FS_PATH_REJECT_MAX << 4) + +/* Paths that should never be written into the working directory. */ +#define GIT_PATH_REJECT_WORKDIR_DEFAULTS \ + GIT_FS_PATH_REJECT_FILESYSTEM_DEFAULTS | GIT_PATH_REJECT_DOT_GIT + +/* Paths that should never be written to the index. */ +#define GIT_PATH_REJECT_INDEX_DEFAULTS \ + GIT_FS_PATH_REJECT_TRAVERSAL | GIT_PATH_REJECT_DOT_GIT + +extern bool git_path_str_is_valid( + git_repository *repo, + const git_str *path, + uint16_t file_mode, + unsigned int flags); + +GIT_INLINE(bool) git_path_is_valid( + git_repository *repo, + const char *path, + uint16_t file_mode, + unsigned int flags) +{ + git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX); + return git_path_str_is_valid(repo, &str, file_mode, flags); +} + +GIT_INLINE(int) git_path_validate_str_length( + git_repository *repo, + const git_str *path) +{ + if (!git_path_str_is_valid(repo, path, 0, GIT_FS_PATH_REJECT_LONG_PATHS)) { + if (path->size == SIZE_MAX) + git_error_set(GIT_ERROR_FILESYSTEM, "path too long: '%s'", path->ptr); + else + git_error_set(GIT_ERROR_FILESYSTEM, "path too long: '%.*s'", (int)path->size, path->ptr); + + return -1; + } + + return 0; +} + +GIT_INLINE(int) git_path_validate_length( + git_repository *repo, + const char *path) +{ + git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX); + return git_path_validate_str_length(repo, &str); +} + +#endif diff --git a/src/libgit2/pathspec.c b/src/libgit2/pathspec.c new file mode 100644 index 00000000000..26684c08104 --- /dev/null +++ b/src/libgit2/pathspec.c @@ -0,0 +1,722 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "pathspec.h" + +#include "git2/pathspec.h" +#include "git2/diff.h" +#include "attr_file.h" +#include "iterator.h" +#include "repository.h" +#include "index.h" +#include "bitvec.h" +#include "diff.h" +#include "wildmatch.h" + +/* what is the common non-wildcard prefix for all items in the pathspec */ +char *git_pathspec_prefix(const git_strarray *pathspec) +{ + git_str prefix = GIT_STR_INIT; + const char *scan; + + if (!pathspec || !pathspec->count || + git_str_common_prefix(&prefix, pathspec->strings, pathspec->count) < 0) + return NULL; + + /* diff prefix will only be leading non-wildcards */ + for (scan = prefix.ptr; *scan; ++scan) { + if (git__iswildcard(*scan) && + (scan == prefix.ptr || (*(scan - 1) != '\\'))) + break; + } + git_str_truncate(&prefix, scan - prefix.ptr); + + if (prefix.size <= 0) { + git_str_dispose(&prefix); + return NULL; + } + + git_str_unescape(&prefix); + + return git_str_detach(&prefix); +} + +/* is there anything in the spec that needs to be filtered on */ +bool git_pathspec_is_empty(const git_strarray *pathspec) +{ + size_t i; + + if (pathspec == NULL) + return true; + + for (i = 0; i < pathspec->count; ++i) { + const char *str = pathspec->strings[i]; + + if (str && str[0]) + return false; + } + + return true; +} + +/* build a vector of fnmatch patterns to evaluate efficiently */ +int git_pathspec__vinit( + git_vector *vspec, const git_strarray *strspec, git_pool *strpool) +{ + size_t i; + + memset(vspec, 0, sizeof(*vspec)); + + if (git_pathspec_is_empty(strspec)) + return 0; + + if (git_vector_init(vspec, strspec->count, NULL) < 0) + return -1; + + for (i = 0; i < strspec->count; ++i) { + int ret; + const char *pattern = strspec->strings[i]; + git_attr_fnmatch *match = git__calloc(1, sizeof(git_attr_fnmatch)); + if (!match) + return -1; + + match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE | GIT_ATTR_FNMATCH_ALLOWNEG; + + ret = git_attr_fnmatch__parse(match, strpool, NULL, &pattern); + if (ret == GIT_ENOTFOUND) { + git__free(match); + continue; + } else if (ret < 0) { + git__free(match); + return ret; + } + + if (git_vector_insert(vspec, match) < 0) + return -1; + } + + return 0; +} + +/* free data from the pathspec vector */ +void git_pathspec__vfree(git_vector *vspec) +{ + git_vector_dispose_deep(vspec); +} + +struct pathspec_match_context { + int wildmatch_flags; + int (*strcomp)(const char *, const char *); + int (*strncomp)(const char *, const char *, size_t); +}; + +static void pathspec_match_context_init( + struct pathspec_match_context *ctxt, + bool disable_fnmatch, + bool casefold) +{ + if (disable_fnmatch) + ctxt->wildmatch_flags = -1; + else if (casefold) + ctxt->wildmatch_flags = WM_CASEFOLD; + else + ctxt->wildmatch_flags = 0; + + if (casefold) { + ctxt->strcomp = git__strcasecmp; + ctxt->strncomp = git__strncasecmp; + } else { + ctxt->strcomp = git__strcmp; + ctxt->strncomp = git__strncmp; + } +} + +static int pathspec_match_one( + const git_attr_fnmatch *match, + struct pathspec_match_context *ctxt, + const char *path) +{ + int result = (match->flags & GIT_ATTR_FNMATCH_MATCH_ALL) ? 0 : WM_NOMATCH; + + if (result == WM_NOMATCH) + result = ctxt->strcomp(match->pattern, path) ? WM_NOMATCH : 0; + + if (ctxt->wildmatch_flags >= 0 && result == WM_NOMATCH) + result = wildmatch(match->pattern, path, ctxt->wildmatch_flags); + + /* if we didn't match, look for exact dirname prefix match */ + if (result == WM_NOMATCH && + (match->flags & GIT_ATTR_FNMATCH_HASWILD) == 0 && + ctxt->strncomp(path, match->pattern, match->length) == 0 && + path[match->length] == '/') + result = 0; + + /* if we didn't match and this is a negative match, check for exact + * match of filename with leading '!' + */ + if (result == WM_NOMATCH && + (match->flags & GIT_ATTR_FNMATCH_NEGATIVE) != 0 && + *path == '!' && + ctxt->strncomp(path + 1, match->pattern, match->length) == 0 && + (!path[match->length + 1] || path[match->length + 1] == '/')) + return 1; + + if (result == 0) + return (match->flags & GIT_ATTR_FNMATCH_NEGATIVE) ? 0 : 1; + return -1; +} + +static int git_pathspec__match_at( + size_t *matched_at, + const git_vector *vspec, + struct pathspec_match_context *ctxt, + const char *path0, + const char *path1) +{ + int result = GIT_ENOTFOUND; + size_t i = 0; + const git_attr_fnmatch *match; + + git_vector_foreach(vspec, i, match) { + if (path0 && (result = pathspec_match_one(match, ctxt, path0)) >= 0) + break; + if (path1 && (result = pathspec_match_one(match, ctxt, path1)) >= 0) + break; + } + + *matched_at = i; + return result; +} + +/* match a path against the vectorized pathspec */ +bool git_pathspec__match( + const git_vector *vspec, + const char *path, + bool disable_fnmatch, + bool casefold, + const char **matched_pathspec, + size_t *matched_at) +{ + int result; + size_t pos; + struct pathspec_match_context ctxt; + + if (matched_pathspec) + *matched_pathspec = NULL; + if (matched_at) + *matched_at = GIT_PATHSPEC_NOMATCH; + + if (!vspec || !vspec->length) + return true; + + pathspec_match_context_init(&ctxt, disable_fnmatch, casefold); + + result = git_pathspec__match_at(&pos, vspec, &ctxt, path, NULL); + if (result >= 0) { + if (matched_pathspec) { + const git_attr_fnmatch *match = git_vector_get(vspec, pos); + *matched_pathspec = match->pattern; + } + + if (matched_at) + *matched_at = pos; + } + + return (result > 0); +} + + +int git_pathspec__init(git_pathspec *ps, const git_strarray *paths) +{ + int error = 0; + + memset(ps, 0, sizeof(*ps)); + + ps->prefix = git_pathspec_prefix(paths); + + if ((error = git_pool_init(&ps->pool, 1)) < 0 || + (error = git_pathspec__vinit(&ps->pathspec, paths, &ps->pool)) < 0) + git_pathspec__clear(ps); + + return error; +} + +void git_pathspec__clear(git_pathspec *ps) +{ + git__free(ps->prefix); + git_pathspec__vfree(&ps->pathspec); + git_pool_clear(&ps->pool); + memset(ps, 0, sizeof(*ps)); +} + +int git_pathspec_new(git_pathspec **out, const git_strarray *pathspec) +{ + int error = 0; + git_pathspec *ps = git__malloc(sizeof(git_pathspec)); + GIT_ERROR_CHECK_ALLOC(ps); + + if ((error = git_pathspec__init(ps, pathspec)) < 0) { + git__free(ps); + return error; + } + + GIT_REFCOUNT_INC(ps); + *out = ps; + return 0; +} + +static void pathspec_free(git_pathspec *ps) +{ + git_pathspec__clear(ps); + git__free(ps); +} + +void git_pathspec_free(git_pathspec *ps) +{ + if (!ps) + return; + GIT_REFCOUNT_DEC(ps, pathspec_free); +} + +int git_pathspec_matches_path( + const git_pathspec *ps, uint32_t flags, const char *path) +{ + bool no_fnmatch = (flags & GIT_PATHSPEC_NO_GLOB) != 0; + bool casefold = (flags & GIT_PATHSPEC_IGNORE_CASE) != 0; + + GIT_ASSERT_ARG(ps); + GIT_ASSERT_ARG(path); + + return (0 != git_pathspec__match( + &ps->pathspec, path, no_fnmatch, casefold, NULL, NULL)); +} + +static void pathspec_match_free(git_pathspec_match_list *m) +{ + if (!m) + return; + + git_pathspec_free(m->pathspec); + m->pathspec = NULL; + + git_array_clear(m->matches); + git_array_clear(m->failures); + git_pool_clear(&m->pool); + git__free(m); +} + +static git_pathspec_match_list *pathspec_match_alloc( + git_pathspec *ps, int datatype) +{ + git_pathspec_match_list *m = git__calloc(1, sizeof(git_pathspec_match_list)); + if (!m) + return NULL; + + if (git_pool_init(&m->pool, 1) < 0) + return NULL; + + /* need to keep reference to pathspec and increment refcount because + * failures array stores pointers to the pattern strings of the + * pathspec that had no matches + */ + GIT_REFCOUNT_INC(ps); + m->pathspec = ps; + m->datatype = datatype; + + return m; +} + +GIT_INLINE(size_t) pathspec_mark_pattern(git_bitvec *used, size_t pos) +{ + if (!git_bitvec_get(used, pos)) { + git_bitvec_set(used, pos, true); + return 1; + } + + return 0; +} + +static size_t pathspec_mark_remaining( + git_bitvec *used, + git_vector *patterns, + struct pathspec_match_context *ctxt, + size_t start, + const char *path0, + const char *path1) +{ + size_t count = 0; + + if (path1 == path0) + path1 = NULL; + + for (; start < patterns->length; ++start) { + const git_attr_fnmatch *pat = git_vector_get(patterns, start); + + if (git_bitvec_get(used, start)) + continue; + + if (path0 && pathspec_match_one(pat, ctxt, path0) > 0) + count += pathspec_mark_pattern(used, start); + else if (path1 && pathspec_match_one(pat, ctxt, path1) > 0) + count += pathspec_mark_pattern(used, start); + } + + return count; +} + +static int pathspec_build_failure_array( + git_pathspec_string_array_t *failures, + git_vector *patterns, + git_bitvec *used, + git_pool *pool) +{ + size_t pos; + char **failed; + const git_attr_fnmatch *pat; + + for (pos = 0; pos < patterns->length; ++pos) { + if (git_bitvec_get(used, pos)) + continue; + + if ((failed = git_array_alloc(*failures)) == NULL) + return -1; + + pat = git_vector_get(patterns, pos); + + if ((*failed = git_pool_strdup(pool, pat->pattern)) == NULL) + return -1; + } + + return 0; +} + +static int pathspec_match_from_iterator( + git_pathspec_match_list **out, + git_iterator *iter, + uint32_t flags, + git_pathspec *ps) +{ + int error = 0; + git_pathspec_match_list *m = NULL; + const git_index_entry *entry = NULL; + struct pathspec_match_context ctxt; + git_vector *patterns = &ps->pathspec; + bool find_failures = out && (flags & GIT_PATHSPEC_FIND_FAILURES) != 0; + bool failures_only = !out || (flags & GIT_PATHSPEC_FAILURES_ONLY) != 0; + size_t pos, used_ct = 0, found_files = 0; + git_index *index = NULL; + git_bitvec used_patterns; + char **file; + + if (git_bitvec_init(&used_patterns, patterns->length) < 0) + return -1; + + if (out) { + *out = m = pathspec_match_alloc(ps, PATHSPEC_DATATYPE_STRINGS); + GIT_ERROR_CHECK_ALLOC(m); + } + + if ((error = git_iterator_reset_range(iter, ps->prefix, ps->prefix)) < 0) + goto done; + + if (git_iterator_type(iter) == GIT_ITERATOR_WORKDIR && + (error = git_repository_index__weakptr( + &index, git_iterator_owner(iter))) < 0) + goto done; + + pathspec_match_context_init( + &ctxt, (flags & GIT_PATHSPEC_NO_GLOB) != 0, + git_iterator_ignore_case(iter)); + + while (!(error = git_iterator_advance(&entry, iter))) { + /* search for match with entry->path */ + int result = git_pathspec__match_at( + &pos, patterns, &ctxt, entry->path, NULL); + + /* no matches for this path */ + if (result < 0) + continue; + + /* if result was a negative pattern match, then don't list file */ + if (!result) { + used_ct += pathspec_mark_pattern(&used_patterns, pos); + continue; + } + + /* check if path is ignored and untracked */ + if (index != NULL && + git_iterator_current_is_ignored(iter) && + git_index__find_pos(NULL, index, entry->path, 0, GIT_INDEX_STAGE_ANY) < 0) + continue; + + /* mark the matched pattern as used */ + used_ct += pathspec_mark_pattern(&used_patterns, pos); + ++found_files; + + /* if find_failures is on, check if any later patterns also match */ + if (find_failures && used_ct < patterns->length) + used_ct += pathspec_mark_remaining( + &used_patterns, patterns, &ctxt, pos + 1, entry->path, NULL); + + /* if only looking at failures, exit early or just continue */ + if (failures_only || !out) { + if (used_ct == patterns->length) + break; + continue; + } + + /* insert matched path into matches array */ + if ((file = (char **)git_array_alloc(m->matches)) == NULL || + (*file = git_pool_strdup(&m->pool, entry->path)) == NULL) { + error = -1; + goto done; + } + } + + if (error < 0 && error != GIT_ITEROVER) + goto done; + error = 0; + + /* insert patterns that had no matches into failures array */ + if (find_failures && used_ct < patterns->length && + (error = pathspec_build_failure_array( + &m->failures, patterns, &used_patterns, &m->pool)) < 0) + goto done; + + /* if every pattern failed to match, then we have failed */ + if ((flags & GIT_PATHSPEC_NO_MATCH_ERROR) != 0 && !found_files) { + git_error_set(GIT_ERROR_INVALID, "no matching files were found"); + error = GIT_ENOTFOUND; + } + +done: + git_bitvec_free(&used_patterns); + + if (error < 0) { + pathspec_match_free(m); + if (out) *out = NULL; + } + + return error; +} + +static git_iterator_flag_t pathspec_match_iter_flags(uint32_t flags) +{ + git_iterator_flag_t f = 0; + + if ((flags & GIT_PATHSPEC_IGNORE_CASE) != 0) + f |= GIT_ITERATOR_IGNORE_CASE; + else if ((flags & GIT_PATHSPEC_USE_CASE) != 0) + f |= GIT_ITERATOR_DONT_IGNORE_CASE; + + return f; +} + +int git_pathspec_match_workdir( + git_pathspec_match_list **out, + git_repository *repo, + uint32_t flags, + git_pathspec *ps) +{ + git_iterator *iter; + git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; + int error = 0; + + GIT_ASSERT_ARG(repo); + + iter_opts.flags = pathspec_match_iter_flags(flags); + + if (!(error = git_iterator_for_workdir(&iter, repo, NULL, NULL, &iter_opts))) { + error = pathspec_match_from_iterator(out, iter, flags, ps); + git_iterator_free(iter); + } + + return error; +} + +int git_pathspec_match_index( + git_pathspec_match_list **out, + git_index *index, + uint32_t flags, + git_pathspec *ps) +{ + git_iterator *iter; + git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; + int error = 0; + + GIT_ASSERT_ARG(index); + + iter_opts.flags = pathspec_match_iter_flags(flags); + + if (!(error = git_iterator_for_index(&iter, git_index_owner(index), index, &iter_opts))) { + error = pathspec_match_from_iterator(out, iter, flags, ps); + git_iterator_free(iter); + } + + return error; +} + +int git_pathspec_match_tree( + git_pathspec_match_list **out, + git_tree *tree, + uint32_t flags, + git_pathspec *ps) +{ + git_iterator *iter; + git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; + int error = 0; + + GIT_ASSERT_ARG(tree); + + iter_opts.flags = pathspec_match_iter_flags(flags); + + if (!(error = git_iterator_for_tree(&iter, tree, &iter_opts))) { + error = pathspec_match_from_iterator(out, iter, flags, ps); + git_iterator_free(iter); + } + + return error; +} + +int git_pathspec_match_diff( + git_pathspec_match_list **out, + git_diff *diff, + uint32_t flags, + git_pathspec *ps) +{ + int error = 0; + git_pathspec_match_list *m = NULL; + struct pathspec_match_context ctxt; + git_vector *patterns = &ps->pathspec; + bool find_failures = out && (flags & GIT_PATHSPEC_FIND_FAILURES) != 0; + bool failures_only = !out || (flags & GIT_PATHSPEC_FAILURES_ONLY) != 0; + size_t i, pos, used_ct = 0, found_deltas = 0; + const git_diff_delta *delta, **match; + git_bitvec used_patterns; + + GIT_ASSERT_ARG(diff); + + if (git_bitvec_init(&used_patterns, patterns->length) < 0) + return -1; + + if (out) { + *out = m = pathspec_match_alloc(ps, PATHSPEC_DATATYPE_DIFF); + GIT_ERROR_CHECK_ALLOC(m); + } + + pathspec_match_context_init( + &ctxt, (flags & GIT_PATHSPEC_NO_GLOB) != 0, + git_diff_is_sorted_icase(diff)); + + git_vector_foreach(&diff->deltas, i, delta) { + /* search for match with delta */ + int result = git_pathspec__match_at( + &pos, patterns, &ctxt, delta->old_file.path, delta->new_file.path); + + /* no matches for this path */ + if (result < 0) + continue; + + /* mark the matched pattern as used */ + used_ct += pathspec_mark_pattern(&used_patterns, pos); + + /* if result was a negative pattern match, then don't list file */ + if (!result) + continue; + + ++found_deltas; + + /* if find_failures is on, check if any later patterns also match */ + if (find_failures && used_ct < patterns->length) + used_ct += pathspec_mark_remaining( + &used_patterns, patterns, &ctxt, pos + 1, + delta->old_file.path, delta->new_file.path); + + /* if only looking at failures, exit early or just continue */ + if (failures_only || !out) { + if (used_ct == patterns->length) + break; + continue; + } + + /* insert matched delta into matches array */ + if (!(match = (const git_diff_delta **)git_array_alloc(m->matches))) { + error = -1; + goto done; + } else { + *match = delta; + } + } + + /* insert patterns that had no matches into failures array */ + if (find_failures && used_ct < patterns->length && + (error = pathspec_build_failure_array( + &m->failures, patterns, &used_patterns, &m->pool)) < 0) + goto done; + + /* if every pattern failed to match, then we have failed */ + if ((flags & GIT_PATHSPEC_NO_MATCH_ERROR) != 0 && !found_deltas) { + git_error_set(GIT_ERROR_INVALID, "no matching deltas were found"); + error = GIT_ENOTFOUND; + } + +done: + git_bitvec_free(&used_patterns); + + if (error < 0) { + pathspec_match_free(m); + if (out) *out = NULL; + } + + return error; +} + +void git_pathspec_match_list_free(git_pathspec_match_list *m) +{ + if (m) + pathspec_match_free(m); +} + +size_t git_pathspec_match_list_entrycount( + const git_pathspec_match_list *m) +{ + return m ? git_array_size(m->matches) : 0; +} + +const char *git_pathspec_match_list_entry( + const git_pathspec_match_list *m, size_t pos) +{ + if (!m || m->datatype != PATHSPEC_DATATYPE_STRINGS || + !git_array_valid_index(m->matches, pos)) + return NULL; + + return *((const char **)git_array_get(m->matches, pos)); +} + +const git_diff_delta *git_pathspec_match_list_diff_entry( + const git_pathspec_match_list *m, size_t pos) +{ + if (!m || m->datatype != PATHSPEC_DATATYPE_DIFF || + !git_array_valid_index(m->matches, pos)) + return NULL; + + return *((const git_diff_delta **)git_array_get(m->matches, pos)); +} + +size_t git_pathspec_match_list_failed_entrycount( + const git_pathspec_match_list *m) +{ + return m ? git_array_size(m->failures) : 0; +} + +const char * git_pathspec_match_list_failed_entry( + const git_pathspec_match_list *m, size_t pos) +{ + char **entry = m ? git_array_get(m->failures, pos) : NULL; + + return entry ? *entry : NULL; +} diff --git a/src/libgit2/pathspec.h b/src/libgit2/pathspec.h new file mode 100644 index 00000000000..0256cb92775 --- /dev/null +++ b/src/libgit2/pathspec.h @@ -0,0 +1,76 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_pathspec_h__ +#define INCLUDE_pathspec_h__ + +#include "common.h" + +#include "git2/pathspec.h" +#include "str.h" +#include "vector.h" +#include "pool.h" +#include "array.h" + +/* public compiled pathspec */ +struct git_pathspec { + git_refcount rc; + char *prefix; + git_vector pathspec; + git_pool pool; +}; + +enum { + PATHSPEC_DATATYPE_STRINGS = 0, + PATHSPEC_DATATYPE_DIFF = 1 +}; + +typedef git_array_t(char *) git_pathspec_string_array_t; + +/* public interface to pathspec matching */ +struct git_pathspec_match_list { + git_pathspec *pathspec; + git_array_t(void *) matches; + git_pathspec_string_array_t failures; + git_pool pool; + int datatype; +}; + +/* what is the common non-wildcard prefix for all items in the pathspec */ +extern char *git_pathspec_prefix(const git_strarray *pathspec); + +/* is there anything in the spec that needs to be filtered on */ +extern bool git_pathspec_is_empty(const git_strarray *pathspec); + +/* build a vector of fnmatch patterns to evaluate efficiently */ +extern int git_pathspec__vinit( + git_vector *vspec, const git_strarray *strspec, git_pool *strpool); + +/* free data from the pathspec vector */ +extern void git_pathspec__vfree(git_vector *vspec); + +#define GIT_PATHSPEC_NOMATCH ((size_t)-1) + +/* + * Match a path against the vectorized pathspec. + * The matched pathspec is passed back into the `matched_pathspec` parameter, + * unless it is passed as NULL by the caller. + */ +extern bool git_pathspec__match( + const git_vector *vspec, + const char *path, + bool disable_fnmatch, + bool casefold, + const char **matched_pathspec, + size_t *matched_at); + +/* easy pathspec setup */ + +extern int git_pathspec__init(git_pathspec *ps, const git_strarray *paths); + +extern void git_pathspec__clear(git_pathspec *ps); + +#endif diff --git a/src/libgit2/proxy.c b/src/libgit2/proxy.c new file mode 100644 index 00000000000..ef91ad6eaaa --- /dev/null +++ b/src/libgit2/proxy.c @@ -0,0 +1,49 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "proxy.h" + +#include "git2/proxy.h" + +int git_proxy_options_init(git_proxy_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_proxy_options, GIT_PROXY_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_proxy_init_options(git_proxy_options *opts, unsigned int version) +{ + return git_proxy_options_init(opts, version); +} +#endif + +int git_proxy_options_dup(git_proxy_options *tgt, const git_proxy_options *src) +{ + if (!src) { + git_proxy_options_init(tgt, GIT_PROXY_OPTIONS_VERSION); + return 0; + } + + memcpy(tgt, src, sizeof(git_proxy_options)); + if (src->url) { + tgt->url = git__strdup(src->url); + GIT_ERROR_CHECK_ALLOC(tgt->url); + } + + return 0; +} + +void git_proxy_options_dispose(git_proxy_options *opts) +{ + if (!opts) + return; + + git__free((char *) opts->url); + opts->url = NULL; +} diff --git a/src/libgit2/proxy.h b/src/libgit2/proxy.h new file mode 100644 index 00000000000..7c0ab598d9a --- /dev/null +++ b/src/libgit2/proxy.h @@ -0,0 +1,17 @@ +/* +* Copyright (C) the libgit2 contributors. All rights reserved. +* +* This file is part of libgit2, distributed under the GNU GPL v2 with +* a Linking Exception. For full terms see the included COPYING file. +*/ +#ifndef INCLUDE_proxy_h__ +#define INCLUDE_proxy_h__ + +#include "common.h" + +#include "git2/proxy.h" + +extern int git_proxy_options_dup(git_proxy_options *tgt, const git_proxy_options *src); +extern void git_proxy_options_dispose(git_proxy_options *opts); + +#endif diff --git a/src/libgit2/push.c b/src/libgit2/push.c new file mode 100644 index 00000000000..b0e84173c74 --- /dev/null +++ b/src/libgit2/push.c @@ -0,0 +1,619 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "push.h" + +#include "git2.h" + +#include "pack.h" +#include "pack-objects.h" +#include "remote.h" +#include "vector.h" +#include "tree.h" + +static int push_spec_rref_cmp(const void *a, const void *b) +{ + const push_spec *push_spec_a = a, *push_spec_b = b; + + return strcmp(push_spec_a->refspec.dst, push_spec_b->refspec.dst); +} + +static int push_status_ref_cmp(const void *a, const void *b) +{ + const push_status *push_status_a = a, *push_status_b = b; + + return strcmp(push_status_a->ref, push_status_b->ref); +} + +int git_push_new(git_push **out, git_remote *remote, const git_push_options *opts) +{ + git_push *p; + + *out = NULL; + + GIT_ERROR_CHECK_VERSION(opts, GIT_PUSH_OPTIONS_VERSION, "git_push_options"); + + p = git__calloc(1, sizeof(*p)); + GIT_ERROR_CHECK_ALLOC(p); + + p->repo = remote->repo; + p->remote = remote; + p->report_status = 1; + p->pb_parallelism = opts ? opts->pb_parallelism : 1; + + if (opts) { + GIT_ERROR_CHECK_VERSION(&opts->callbacks, GIT_REMOTE_CALLBACKS_VERSION, "git_remote_callbacks"); + memcpy(&p->callbacks, &opts->callbacks, sizeof(git_remote_callbacks)); + } + + if (git_vector_init(&p->specs, 0, push_spec_rref_cmp) < 0) { + git__free(p); + return -1; + } + + if (git_vector_init(&p->status, 0, push_status_ref_cmp) < 0) { + git_vector_dispose(&p->specs); + git__free(p); + return -1; + } + + if (git_vector_init(&p->updates, 0, NULL) < 0) { + git_vector_dispose(&p->status); + git_vector_dispose(&p->specs); + git__free(p); + return -1; + } + + if (git_vector_init(&p->remote_push_options, 0, git__strcmp_cb) < 0) { + git_vector_dispose(&p->status); + git_vector_dispose(&p->specs); + git_vector_dispose(&p->updates); + git__free(p); + return -1; + } + + *out = p; + return 0; +} + +static void free_refspec(push_spec *spec) +{ + if (spec == NULL) + return; + + git_refspec__dispose(&spec->refspec); + git__free(spec); +} + +static int check_rref(char *ref) +{ + if (git__prefixcmp(ref, "refs/")) { + git_error_set(GIT_ERROR_INVALID, "not a valid reference '%s'", ref); + return -1; + } + + return 0; +} + +static int check_lref(git_push *push, char *ref) +{ + /* lref must be resolvable to an existing object */ + git_object *obj; + int error = git_revparse_single(&obj, push->repo, ref); + git_object_free(obj); + + if (!error) + return 0; + + if (error == GIT_ENOTFOUND) + git_error_set(GIT_ERROR_REFERENCE, + "src refspec '%s' does not match any existing object", ref); + else + git_error_set(GIT_ERROR_INVALID, "not a valid reference '%s'", ref); + return -1; +} + +static int parse_refspec(git_push *push, push_spec **spec, const char *str) +{ + push_spec *s; + + *spec = NULL; + + s = git__calloc(1, sizeof(*s)); + GIT_ERROR_CHECK_ALLOC(s); + + git_oid_clear(&s->loid, push->repo->oid_type); + git_oid_clear(&s->roid, push->repo->oid_type); + + if (git_refspec__parse(&s->refspec, str, false) < 0) { + git_error_set(GIT_ERROR_INVALID, "invalid refspec %s", str); + goto on_error; + } + + if (s->refspec.src && s->refspec.src[0] != '\0' && + check_lref(push, s->refspec.src) < 0) { + goto on_error; + } + + if (check_rref(s->refspec.dst) < 0) + goto on_error; + + *spec = s; + return 0; + +on_error: + free_refspec(s); + return -1; +} + +int git_push_add_refspec(git_push *push, const char *refspec) +{ + push_spec *spec; + + if (parse_refspec(push, &spec, refspec) < 0 || + git_vector_insert(&push->specs, spec) < 0) + return -1; + + return 0; +} + +int git_push_update_tips(git_push *push, const git_remote_callbacks *callbacks) +{ + git_str remote_ref_name = GIT_STR_INIT; + size_t i, j; + git_refspec *fetch_spec; + push_spec *push_spec = NULL; + git_reference *remote_ref; + push_status *status; + int error = 0; + + git_vector_foreach(&push->status, i, status) { + int fire_callback = 1; + + /* Skip unsuccessful updates which have non-empty messages */ + if (status->msg) + continue; + + /* Find the corresponding remote ref */ + fetch_spec = git_remote__matching_refspec(push->remote, status->ref); + if (!fetch_spec) + continue; + + /* Clear the buffer which can be dirty from previous iteration */ + git_str_clear(&remote_ref_name); + + if ((error = git_refspec__transform(&remote_ref_name, fetch_spec, status->ref)) < 0) + goto on_error; + + /* Find matching push ref spec */ + git_vector_foreach(&push->specs, j, push_spec) { + if (!strcmp(push_spec->refspec.dst, status->ref)) + break; + } + + /* Could not find the corresponding push ref spec for this push update */ + if (j == push->specs.length) + continue; + + /* Update the remote ref */ + if (git_oid_is_zero(&push_spec->loid)) { + error = git_reference_lookup(&remote_ref, push->remote->repo, git_str_cstr(&remote_ref_name)); + + if (error >= 0) { + error = git_reference_delete(remote_ref); + git_reference_free(remote_ref); + } + } else { + error = git_reference_create(NULL, push->remote->repo, + git_str_cstr(&remote_ref_name), &push_spec->loid, 1, + "update by push"); + } + + if (error < 0) { + if (error != GIT_ENOTFOUND) + goto on_error; + + git_error_clear(); + fire_callback = 0; + } + + if (!fire_callback || !callbacks) + continue; + + if (callbacks->update_refs) + error = callbacks->update_refs( + git_str_cstr(&remote_ref_name), + &push_spec->roid, &push_spec->loid, + &push_spec->refspec, callbacks->payload); +#ifndef GIT_DEPRECATE_HARD + else if (callbacks->update_tips) + error = callbacks->update_tips( + git_str_cstr(&remote_ref_name), + &push_spec->roid, &push_spec->loid, + callbacks->payload); +#endif + + if (error < 0) { + git_error_set_after_callback_function(error, "git_remote_push"); + goto on_error; + } + } + + error = 0; + +on_error: + git_str_dispose(&remote_ref_name); + return error; +} + +/** + * Insert all tags until we find a non-tag object, which is returned + * in `out`. + */ +static int enqueue_tag(git_object **out, git_push *push, git_oid *id) +{ + git_object *obj = NULL, *target = NULL; + int error; + + if ((error = git_object_lookup(&obj, push->repo, id, GIT_OBJECT_TAG)) < 0) + return error; + + while (git_object_type(obj) == GIT_OBJECT_TAG) { + if ((error = git_packbuilder_insert(push->pb, git_object_id(obj), NULL)) < 0) + break; + + if ((error = git_tag_target(&target, (git_tag *) obj)) < 0) + break; + + git_object_free(obj); + obj = target; + } + + if (error < 0) + git_object_free(obj); + else + *out = obj; + + return error; +} + +static int queue_objects(git_push *push) +{ + git_remote_head *head; + push_spec *spec; + git_revwalk *rw; + unsigned int i; + int error = -1; + + if (git_revwalk_new(&rw, push->repo) < 0) + return -1; + + git_revwalk_sorting(rw, GIT_SORT_TIME); + + git_vector_foreach(&push->specs, i, spec) { + git_object_t type; + git_oid id; + size_t size; + + if (git_oid_is_zero(&spec->loid)) + /* + * Delete reference on remote side; + * nothing to do here. + */ + continue; + + if (git_oid_equal(&spec->loid, &spec->roid)) + continue; /* up-to-date */ + + if ((error = git_odb_read_header(&size, &type, push->repo->_odb, &spec->loid)) < 0) + goto on_error; + + if (type == GIT_OBJECT_TAG) { + git_object *target; + + if ((error = enqueue_tag(&target, push, &spec->loid)) < 0) + goto on_error; + + type = git_object_type(target); + git_oid_cpy(&id, git_object_id(target)); + + git_object_free(target); + } else { + git_oid_cpy(&id, &spec->loid); + } + + if (type == GIT_OBJECT_COMMIT) + error = git_revwalk_push(rw, &id); + else + error = git_packbuilder_insert(push->pb, &id, NULL); + + if (error < 0) + goto on_error; + + if (!spec->refspec.force) { + git_oid base; + + if (git_oid_is_zero(&spec->roid)) + continue; + + if (!git_odb_exists(push->repo->_odb, &spec->roid)) { + git_error_set(GIT_ERROR_REFERENCE, + "cannot push because a reference that you are trying to update on the remote contains commits that are not present locally."); + error = GIT_ENONFASTFORWARD; + goto on_error; + } + + error = git_merge_base(&base, push->repo, + &spec->loid, &spec->roid); + + if (error == GIT_ENOTFOUND || + (!error && !git_oid_equal(&base, &spec->roid))) { + git_error_set(GIT_ERROR_REFERENCE, + "cannot push non-fastforwardable reference"); + error = GIT_ENONFASTFORWARD; + goto on_error; + } + + if (error < 0) + goto on_error; + } + } + + git_vector_foreach(&push->remote->refs, i, head) { + if (git_oid_is_zero(&head->oid)) + continue; + + if ((error = git_revwalk_hide(rw, &head->oid)) < 0 && + error != GIT_ENOTFOUND && error != GIT_EINVALIDSPEC && error != GIT_EPEEL) + goto on_error; + } + + error = git_packbuilder_insert_walk(push->pb, rw); + +on_error: + git_revwalk_free(rw); + return error; +} + +static int add_update(git_push *push, push_spec *spec) +{ + git_push_update *u = git__calloc(1, sizeof(git_push_update)); + GIT_ERROR_CHECK_ALLOC(u); + + u->src_refname = git__strdup(spec->refspec.src); + GIT_ERROR_CHECK_ALLOC(u->src_refname); + + u->dst_refname = git__strdup(spec->refspec.dst); + GIT_ERROR_CHECK_ALLOC(u->dst_refname); + + git_oid_cpy(&u->src, &spec->roid); + git_oid_cpy(&u->dst, &spec->loid); + + return git_vector_insert(&push->updates, u); +} + +static int calculate_work(git_push *push) +{ + git_remote_head *head; + push_spec *spec; + unsigned int i, j; + + /* Update local and remote oids*/ + + git_vector_foreach(&push->specs, i, spec) { + if (spec->refspec.src && spec->refspec.src[0]!= '\0') { + /* This is a create or update. Local ref must exist. */ + + git_object *obj; + int error = git_revparse_single(&obj, push->repo, spec->refspec.src); + + if (error < 0) { + git_object_free(obj); + git_error_set(GIT_ERROR_REFERENCE, "src refspec %s does not match any", spec->refspec.src); + return -1; + } + + git_oid_cpy(&spec->loid, git_object_id(obj)); + git_object_free(obj); + } + + /* Remote ref may or may not (e.g. during create) already exist. */ + git_vector_foreach(&push->remote->refs, j, head) { + if (!strcmp(spec->refspec.dst, head->name)) { + git_oid_cpy(&spec->roid, &head->oid); + break; + } + } + + if (add_update(push, spec) < 0) + return -1; + } + + return 0; +} + +static int do_push(git_push *push) +{ + int error = 0; + git_transport *transport = push->remote->transport; + git_remote_callbacks *callbacks = &push->callbacks; + + if (!transport->push) { + git_error_set(GIT_ERROR_NET, "remote transport doesn't support push"); + error = -1; + goto on_error; + } + + /* + * A pack-file MUST be sent if either create or update command + * is used, even if the server already has all the necessary + * objects. In this case the client MUST send an empty pack-file. + */ + + if ((error = git_packbuilder_new(&push->pb, push->repo)) < 0) + goto on_error; + + git_packbuilder_set_threads(push->pb, push->pb_parallelism); + + if (callbacks && callbacks->pack_progress) + if ((error = git_packbuilder_set_callbacks(push->pb, callbacks->pack_progress, callbacks->payload)) < 0) + goto on_error; + + if ((error = calculate_work(push)) < 0) + goto on_error; + + if (callbacks && callbacks->push_negotiation) { + git_error_clear(); + + error = callbacks->push_negotiation( + (const git_push_update **) push->updates.contents, + push->updates.length, callbacks->payload); + + if (error < 0) { + git_error_set_after_callback_function(error, + "push_negotiation"); + goto on_error; + } + + error = 0; + } + + if ((error = queue_objects(push)) < 0 || + (error = transport->push(transport, push)) < 0) + goto on_error; + +on_error: + git_packbuilder_free(push->pb); + return error; +} + +static int filter_refs(git_remote *remote) +{ + const git_remote_head **heads; + size_t heads_len, i; + + git_vector_clear(&remote->refs); + + if (git_remote_ls(&heads, &heads_len, remote) < 0) + return -1; + + for (i = 0; i < heads_len; i++) { + if (git_vector_insert(&remote->refs, (void *)heads[i]) < 0) + return -1; + } + + return 0; +} + +int git_push_finish(git_push *push) +{ + int error; + unsigned int remote_caps; + + if (!git_remote_connected(push->remote)) { + git_error_set(GIT_ERROR_NET, "remote is disconnected"); + return -1; + } + + if ((error = git_remote_capabilities(&remote_caps, push->remote)) < 0) { + git_error_set(GIT_ERROR_INVALID, "remote capabilities not available"); + return -1; + } + + if (git_vector_length(&push->remote_push_options) > 0 && + !(remote_caps & GIT_REMOTE_CAPABILITY_PUSH_OPTIONS)) { + git_error_set(GIT_ERROR_INVALID, "push-options not supported by remote"); + return -1; + } + + if ((error = filter_refs(push->remote)) < 0 || + (error = do_push(push)) < 0) + return error; + + if (!push->unpack_ok) { + error = -1; + git_error_set(GIT_ERROR_NET, "unpacking the sent packfile failed on the remote"); + } + + return error; +} + +int git_push_status_foreach(git_push *push, + int (*cb)(const char *ref, const char *msg, void *data), + void *data) +{ + push_status *status; + unsigned int i; + + git_vector_foreach(&push->status, i, status) { + int error = cb(status->ref, status->msg, data); + if (error) + return git_error_set_after_callback(error); + } + + return 0; +} + +void git_push_status_free(push_status *status) +{ + if (status == NULL) + return; + + git__free(status->msg); + git__free(status->ref); + git__free(status); +} + +void git_push_free(git_push *push) +{ + push_spec *spec; + push_status *status; + git_push_update *update; + char *option; + unsigned int i; + + if (push == NULL) + return; + + git_vector_foreach(&push->specs, i, spec) { + free_refspec(spec); + } + git_vector_dispose(&push->specs); + + git_vector_foreach(&push->status, i, status) { + git_push_status_free(status); + } + git_vector_dispose(&push->status); + + git_vector_foreach(&push->updates, i, update) { + git__free(update->src_refname); + git__free(update->dst_refname); + git__free(update); + } + git_vector_dispose(&push->updates); + + git_vector_foreach(&push->remote_push_options, i, option) { + git__free(option); + } + git_vector_dispose(&push->remote_push_options); + + git__free(push); +} + +int git_push_options_init(git_push_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_push_options, GIT_PUSH_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_push_init_options(git_push_options *opts, unsigned int version) +{ + return git_push_options_init(opts, version); +} +#endif diff --git a/src/libgit2/push.h b/src/libgit2/push.h new file mode 100644 index 00000000000..40a1823e45b --- /dev/null +++ b/src/libgit2/push.h @@ -0,0 +1,129 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_push_h__ +#define INCLUDE_push_h__ + +#include "common.h" + +#include "git2.h" +#include "refspec.h" +#include "remote.h" + +typedef struct push_spec { + struct git_refspec refspec; + + git_oid loid; + git_oid roid; +} push_spec; + +typedef struct push_status { + bool ok; + + char *ref; + char *msg; +} push_status; + +struct git_push { + git_repository *repo; + git_packbuilder *pb; + git_remote *remote; + git_vector specs; + git_vector updates; + bool report_status; + git_vector remote_push_options; + + /* report-status */ + bool unpack_ok; + git_vector status; + + /* options */ + unsigned pb_parallelism; + git_remote_callbacks callbacks; +}; + +/** + * Free the given push status object + * + * @param status The push status object + */ +void git_push_status_free(push_status *status); + +/** + * Create a new push object + * + * @param out New push object + * @param remote Remote instance + * @param opts Push options or NULL + * + * @return 0 or an error code + */ +int git_push_new(git_push **out, git_remote *remote, const git_push_options *opts); + +/** + * Add a refspec to be pushed + * + * @param push The push object + * @param refspec Refspec string + * + * @return 0 or an error code + */ +int git_push_add_refspec(git_push *push, const char *refspec); + +/** + * Update remote tips after a push + * + * @param push The push object + * @param callbacks the callbacks to use for this connection + * + * @return 0 or an error code + */ +int git_push_update_tips(git_push *push, const git_remote_callbacks *callbacks); + +/** + * Perform the push + * + * This function will return an error in case of a protocol error or + * the server being unable to unpack the data we sent. + * + * The return value does not reflect whether the server accepted or + * refused any reference updates. Use `git_push_status_foreach()` in + * order to find out which updates were accepted or rejected. + * + * @param push The push object + * + * @return 0 or an error code + */ +int git_push_finish(git_push *push); + +/** + * Invoke callback `cb' on each status entry + * + * For each of the updated references, we receive a status report in the + * form of `ok refs/heads/master` or `ng refs/heads/master `. + * `msg != NULL` means the reference has not been updated for the given + * reason. + * + * Return a non-zero value from the callback to stop the loop. + * + * @param push The push object + * @param cb The callback to call on each object + * @param data The payload passed to the callback + * + * @return 0 on success, non-zero callback return value, or error code + */ +int git_push_status_foreach(git_push *push, + int (*cb)(const char *ref, const char *msg, void *data), + void *data); + +/** + * Free the given push object + * + * @param push The push object + */ +void git_push_free(git_push *push); + +#endif diff --git a/src/libgit2/reader.c b/src/libgit2/reader.c new file mode 100644 index 00000000000..df2b2807f55 --- /dev/null +++ b/src/libgit2/reader.c @@ -0,0 +1,269 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "reader.h" + +#include "futils.h" +#include "blob.h" + +#include "git2/tree.h" +#include "git2/blob.h" +#include "git2/index.h" +#include "git2/repository.h" + +/* tree reader */ + +typedef struct { + git_reader reader; + git_tree *tree; +} tree_reader; + +static int tree_reader_read( + git_str *out, + git_oid *out_id, + git_filemode_t *out_filemode, + git_reader *_reader, + const char *filename) +{ + tree_reader *reader = (tree_reader *)_reader; + git_tree_entry *tree_entry = NULL; + git_blob *blob = NULL; + git_object_size_t blobsize; + int error; + + if ((error = git_tree_entry_bypath(&tree_entry, reader->tree, filename)) < 0 || + (error = git_blob_lookup(&blob, git_tree_owner(reader->tree), git_tree_entry_id(tree_entry))) < 0) + goto done; + + blobsize = git_blob_rawsize(blob); + GIT_ERROR_CHECK_BLOBSIZE(blobsize); + + if ((error = git_str_set(out, git_blob_rawcontent(blob), (size_t)blobsize)) < 0) + goto done; + + if (out_id) + git_oid_cpy(out_id, git_tree_entry_id(tree_entry)); + + if (out_filemode) + *out_filemode = git_tree_entry_filemode(tree_entry); + +done: + git_blob_free(blob); + git_tree_entry_free(tree_entry); + return error; +} + +int git_reader_for_tree(git_reader **out, git_tree *tree) +{ + tree_reader *reader; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(tree); + + reader = git__calloc(1, sizeof(tree_reader)); + GIT_ERROR_CHECK_ALLOC(reader); + + reader->reader.read = tree_reader_read; + reader->tree = tree; + + *out = (git_reader *)reader; + return 0; +} + +/* workdir reader */ + +typedef struct { + git_reader reader; + git_repository *repo; + git_index *index; +} workdir_reader; + +static int workdir_reader_read( + git_str *out, + git_oid *out_id, + git_filemode_t *out_filemode, + git_reader *_reader, + const char *filename) +{ + workdir_reader *reader = (workdir_reader *)_reader; + git_str path = GIT_STR_INIT; + struct stat st; + git_filemode_t filemode; + git_filter_list *filters = NULL; + const git_index_entry *idx_entry; + git_oid id; + int error; + + if ((error = git_repository_workdir_path(&path, reader->repo, filename)) < 0) + goto done; + + if ((error = p_lstat(path.ptr, &st)) < 0) { + if (error == -1 && errno == ENOENT) + error = GIT_ENOTFOUND; + + git_error_set(GIT_ERROR_OS, "could not stat '%s'", path.ptr); + goto done; + } + + filemode = git_futils_canonical_mode(st.st_mode); + + /* + * Patch application - for example - uses the filtered version of + * the working directory data to match git. So we will run the + * workdir -> ODB filter on the contents in this workdir reader. + */ + if ((error = git_filter_list_load(&filters, reader->repo, NULL, filename, + GIT_FILTER_TO_ODB, GIT_FILTER_DEFAULT)) < 0) + goto done; + + if ((error = git_filter_list__apply_to_file(out, + filters, reader->repo, path.ptr)) < 0) + goto done; + + if (out_id || reader->index) { + if ((error = git_odb__hash(&id, out->ptr, out->size, GIT_OBJECT_BLOB, reader->repo->oid_type)) < 0) + goto done; + } + + if (reader->index) { + if (!(idx_entry = git_index_get_bypath(reader->index, filename, 0)) || + filemode != idx_entry->mode || + !git_oid_equal(&id, &idx_entry->id)) { + error = GIT_READER_MISMATCH; + goto done; + } + } + + if (out_id) + git_oid_cpy(out_id, &id); + + if (out_filemode) + *out_filemode = filemode; + +done: + git_filter_list_free(filters); + git_str_dispose(&path); + return error; +} + +int git_reader_for_workdir( + git_reader **out, + git_repository *repo, + bool validate_index) +{ + workdir_reader *reader; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + reader = git__calloc(1, sizeof(workdir_reader)); + GIT_ERROR_CHECK_ALLOC(reader); + + reader->reader.read = workdir_reader_read; + reader->repo = repo; + + if (validate_index && + (error = git_repository_index__weakptr(&reader->index, repo)) < 0) { + git__free(reader); + return error; + } + + *out = (git_reader *)reader; + return 0; +} + +/* index reader */ + +typedef struct { + git_reader reader; + git_repository *repo; + git_index *index; +} index_reader; + +static int index_reader_read( + git_str *out, + git_oid *out_id, + git_filemode_t *out_filemode, + git_reader *_reader, + const char *filename) +{ + index_reader *reader = (index_reader *)_reader; + const git_index_entry *entry; + git_blob *blob; + int error; + + if ((entry = git_index_get_bypath(reader->index, filename, 0)) == NULL) + return GIT_ENOTFOUND; + + if ((error = git_blob_lookup(&blob, reader->repo, &entry->id)) < 0) + goto done; + + if (out_id) + git_oid_cpy(out_id, &entry->id); + + if (out_filemode) + *out_filemode = entry->mode; + + error = git_blob__getbuf(out, blob); + +done: + git_blob_free(blob); + return error; +} + +int git_reader_for_index( + git_reader **out, + git_repository *repo, + git_index *index) +{ + index_reader *reader; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + reader = git__calloc(1, sizeof(index_reader)); + GIT_ERROR_CHECK_ALLOC(reader); + + reader->reader.read = index_reader_read; + reader->repo = repo; + + if (index) { + reader->index = index; + } else if ((error = git_repository_index__weakptr(&reader->index, repo)) < 0) { + git__free(reader); + return error; + } + + *out = (git_reader *)reader; + return 0; +} + +/* generic */ + +int git_reader_read( + git_str *out, + git_oid *out_id, + git_filemode_t *out_filemode, + git_reader *reader, + const char *filename) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(reader); + GIT_ASSERT_ARG(filename); + + return reader->read(out, out_id, out_filemode, reader, filename); +} + +void git_reader_free(git_reader *reader) +{ + if (!reader) + return; + + git__free(reader); +} diff --git a/src/libgit2/reader.h b/src/libgit2/reader.h new file mode 100644 index 00000000000..b58dc93f673 --- /dev/null +++ b/src/libgit2/reader.h @@ -0,0 +1,107 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_reader_h__ +#define INCLUDE_reader_h__ + +#include "common.h" + +/* Returned when the workdir does not match the index */ +#define GIT_READER_MISMATCH 1 + +typedef struct git_reader git_reader; + +/* + * The `git_reader` structure is a generic interface for reading the + * contents of a file by its name, and implementations are provided + * for reading out of a tree, the index, and the working directory. + * + * Note that the reader implementation is meant to have a short + * lifecycle and does not increase the refcount of the object that + * it's reading. Callers should ensure that they do not use a + * reader after disposing the underlying object that it reads. + */ +struct git_reader { + int (*read)(git_str *out, git_oid *out_oid, git_filemode_t *mode, git_reader *reader, const char *filename); +}; + +/** + * Create a `git_reader` that will allow random access to the given + * tree. Paths requested via `git_reader_read` will be rooted at this + * tree, callers are not expected to recurse through tree lookups. Thus, + * you can request to read `/src/foo.c` and the tree provided to this + * function will be searched to find another tree named `src`, which + * will then be opened to find `foo.c`. + * + * @param out The reader for the given tree + * @param tree The tree object to read + * @return 0 on success, or an error code < 0 + */ +extern int git_reader_for_tree( + git_reader **out, + git_tree *tree); + +/** + * Create a `git_reader` that will allow random access to the given + * index, or the repository's index. + * + * @param out The reader for the given index + * @param repo The repository containing the index + * @param index The index to read, or NULL to use the repository's index + * @return 0 on success, or an error code < 0 + */ +extern int git_reader_for_index( + git_reader **out, + git_repository *repo, + git_index *index); + +/** + * Create a `git_reader` that will allow random access to the given + * repository's working directory. Note that the contents are read + * in repository format, meaning any workdir -> odb filters are + * applied. + * + * If `validate_index` is set to true, reads of files will hash the + * on-disk contents and ensure that the resulting object ID matches + * the repository's index. This ensures that the working directory + * is unmodified from the index contents. + * + * @param out The reader for the given working directory + * @param repo The repository containing the working directory + * @param validate_index If true, the working directory contents will + * be compared to the index contents during read to ensure that + * the working directory is unmodified. + * @return 0 on success, or an error code < 0 + */ +extern int git_reader_for_workdir( + git_reader **out, + git_repository *repo, + bool validate_index); + +/** + * Read the given filename from the reader and populate the given buffer + * with the contents and the given oid with the object ID. + * + * @param out The buffer to populate with the file contents + * @param out_id The oid to populate with the object ID + * @param reader The reader to read + * @param filename The filename to read from the reader + */ +extern int git_reader_read( + git_str *out, + git_oid *out_id, + git_filemode_t *out_filemode, + git_reader *reader, + const char *filename); + +/** + * Free the given reader and any associated objects. + * + * @param reader The reader to free + */ +extern void git_reader_free(git_reader *reader); + +#endif diff --git a/src/libgit2/rebase.c b/src/libgit2/rebase.c new file mode 100644 index 00000000000..eb8a728357c --- /dev/null +++ b/src/libgit2/rebase.c @@ -0,0 +1,1469 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "str.h" +#include "repository.h" +#include "posix.h" +#include "filebuf.h" +#include "commit.h" +#include "merge.h" +#include "array.h" +#include "config.h" +#include "annotated_commit.h" +#include "index.h" + +#include +#include +#include +#include +#include +#include +#include + +#define REBASE_APPLY_DIR "rebase-apply" +#define REBASE_MERGE_DIR "rebase-merge" + +#define HEAD_NAME_FILE "head-name" +#define ORIG_HEAD_FILE "orig-head" +#define HEAD_FILE "head" +#define ONTO_FILE "onto" +#define ONTO_NAME_FILE "onto_name" +#define QUIET_FILE "quiet" +#define INTERACTIVE_FILE "interactive" + +#define MSGNUM_FILE "msgnum" +#define END_FILE "end" +#define CMT_FILE_FMT "cmt.%" PRIuZ +#define CURRENT_FILE "current" +#define REWRITTEN_FILE "rewritten" + +#define ORIG_DETACHED_HEAD "detached HEAD" + +#define NOTES_DEFAULT_REF NULL + +#define REBASE_DIR_MODE 0777 +#define REBASE_FILE_MODE 0666 + +typedef enum { + GIT_REBASE_NONE = 0, + GIT_REBASE_APPLY = 1, + GIT_REBASE_MERGE = 2, + GIT_REBASE_INTERACTIVE = 3 +} git_rebase_t; + +struct git_rebase { + git_repository *repo; + + git_rebase_options options; + + git_rebase_t type; + char *state_path; + + /* Temporary buffer for paths within the state path. */ + git_str state_filename; + + unsigned int head_detached:1, + inmemory:1, + quiet:1, + started:1; + + git_array_t(git_rebase_operation) operations; + size_t current; + + /* Used by in-memory rebase */ + git_index *index; + git_commit *last_commit; + + /* Used by regular (not in-memory) merge-style rebase */ + git_oid orig_head_id; + char *orig_head_name; + + git_oid onto_id; + char *onto_name; +}; + +#define GIT_REBASE_STATE_INIT {0} + +static int rebase_state_type( + git_rebase_t *type_out, + char **path_out, + git_repository *repo) +{ + git_str path = GIT_STR_INIT; + git_str interactive_path = GIT_STR_INIT; + git_rebase_t type = GIT_REBASE_NONE; + + if (git_str_joinpath(&path, repo->gitdir, REBASE_APPLY_DIR) < 0) + return -1; + + if (git_fs_path_isdir(git_str_cstr(&path))) { + type = GIT_REBASE_APPLY; + goto done; + } + + git_str_clear(&path); + if (git_str_joinpath(&path, repo->gitdir, REBASE_MERGE_DIR) < 0) + return -1; + + if (git_fs_path_isdir(git_str_cstr(&path))) { + if (git_str_joinpath(&interactive_path, path.ptr, INTERACTIVE_FILE) < 0) + return -1; + + if (git_fs_path_isfile(interactive_path.ptr)) + type = GIT_REBASE_INTERACTIVE; + else + type = GIT_REBASE_MERGE; + + goto done; + } + +done: + *type_out = type; + + if (type != GIT_REBASE_NONE && path_out) + *path_out = git_str_detach(&path); + + git_str_dispose(&path); + git_str_dispose(&interactive_path); + + return 0; +} + +GIT_INLINE(int) rebase_readfile( + git_str *out, + git_rebase *rebase, + const char *filename) +{ + /* + * `rebase->state_filename` is a temporary buffer to avoid + * unnecessary allocations and copies of `rebase->state_path`. + * At the start and end of this function it always contains the + * contents of `rebase->state_path` itself. + */ + size_t state_path_len = rebase->state_filename.size; + int error; + + git_str_clear(out); + + if ((error = git_str_joinpath(&rebase->state_filename, rebase->state_filename.ptr, filename)) < 0 || + (error = git_futils_readbuffer(out, rebase->state_filename.ptr)) < 0) + goto done; + + git_str_rtrim(out); + +done: + git_str_truncate(&rebase->state_filename, state_path_len); + return error; +} + +GIT_INLINE(int) rebase_readint( + size_t *out, + git_str *asc_out, + git_rebase *rebase, + const char *filename) +{ + int32_t num; + const char *eol; + int error = 0; + + if ((error = rebase_readfile(asc_out, rebase, filename)) < 0) + return error; + + if (git__strntol32(&num, asc_out->ptr, asc_out->size, &eol, 10) < 0 || num < 0 || *eol) { + git_error_set(GIT_ERROR_REBASE, "the file '%s' contains an invalid numeric value", filename); + return -1; + } + + *out = (size_t) num; + + return 0; +} + +GIT_INLINE(int) rebase_readoid( + git_oid *out, + git_str *str_out, + git_rebase *rebase, + const char *filename) +{ + int error; + + if ((error = rebase_readfile(str_out, rebase, filename)) < 0) + return error; + + if (str_out->size != git_oid_hexsize(rebase->repo->oid_type) || + git_oid_from_string(out, str_out->ptr, rebase->repo->oid_type) < 0) { + git_error_set(GIT_ERROR_REBASE, "the file '%s' contains an invalid object ID", filename); + return -1; + } + + return 0; +} + +static git_rebase_operation *rebase_operation_alloc( + git_rebase *rebase, + git_rebase_operation_t type, + git_oid *id, + const char *exec) +{ + git_rebase_operation *operation; + + GIT_ASSERT_WITH_RETVAL((type == GIT_REBASE_OPERATION_EXEC) == !id, NULL); + GIT_ASSERT_WITH_RETVAL((type == GIT_REBASE_OPERATION_EXEC) == !!exec, NULL); + + if ((operation = git_array_alloc(rebase->operations)) == NULL) + return NULL; + + operation->type = type; + git_oid_cpy((git_oid *)&operation->id, id); + operation->exec = exec; + + return operation; +} + +static int rebase_open_merge(git_rebase *rebase) +{ + git_str buf = GIT_STR_INIT, cmt = GIT_STR_INIT; + git_oid id; + git_rebase_operation *operation; + size_t i, msgnum = 0, end; + int error; + + /* Read 'msgnum' if it exists (otherwise, let msgnum = 0) */ + if ((error = rebase_readint(&msgnum, &buf, rebase, MSGNUM_FILE)) < 0 && + error != GIT_ENOTFOUND) + goto done; + + if (msgnum) { + rebase->started = 1; + rebase->current = msgnum - 1; + } + + /* Read 'end' */ + if ((error = rebase_readint(&end, &buf, rebase, END_FILE)) < 0) + goto done; + + /* Read 'current' if it exists */ + if ((error = rebase_readoid(&id, &buf, rebase, CURRENT_FILE)) < 0 && + error != GIT_ENOTFOUND) + goto done; + + /* Read cmt.* */ + git_array_init_to_size(rebase->operations, end); + GIT_ERROR_CHECK_ARRAY(rebase->operations); + + for (i = 0; i < end; i++) { + git_str_clear(&cmt); + + if ((error = git_str_printf(&cmt, "cmt.%" PRIuZ, (i+1))) < 0 || + (error = rebase_readoid(&id, &buf, rebase, cmt.ptr)) < 0) + goto done; + + operation = rebase_operation_alloc(rebase, GIT_REBASE_OPERATION_PICK, &id, NULL); + GIT_ERROR_CHECK_ALLOC(operation); + } + + /* Read 'onto_name' */ + if ((error = rebase_readfile(&buf, rebase, ONTO_NAME_FILE)) < 0) + goto done; + + rebase->onto_name = git_str_detach(&buf); + +done: + git_str_dispose(&cmt); + git_str_dispose(&buf); + + return error; +} + +static int rebase_alloc(git_rebase **out, const git_rebase_options *rebase_opts) +{ + git_rebase *rebase = git__calloc(1, sizeof(git_rebase)); + GIT_ERROR_CHECK_ALLOC(rebase); + + *out = NULL; + + if (rebase_opts) + memcpy(&rebase->options, rebase_opts, sizeof(git_rebase_options)); + else + git_rebase_options_init(&rebase->options, GIT_REBASE_OPTIONS_VERSION); + + if (rebase_opts && rebase_opts->rewrite_notes_ref) { + rebase->options.rewrite_notes_ref = git__strdup(rebase_opts->rewrite_notes_ref); + GIT_ERROR_CHECK_ALLOC(rebase->options.rewrite_notes_ref); + } + + *out = rebase; + + return 0; +} + +static int rebase_check_versions(const git_rebase_options *given_opts) +{ + GIT_ERROR_CHECK_VERSION(given_opts, GIT_REBASE_OPTIONS_VERSION, "git_rebase_options"); + + if (given_opts) + GIT_ERROR_CHECK_VERSION(&given_opts->checkout_options, GIT_CHECKOUT_OPTIONS_VERSION, "git_checkout_options"); + + return 0; +} + +int git_rebase_open( + git_rebase **out, + git_repository *repo, + const git_rebase_options *given_opts) +{ + git_rebase *rebase; + git_str orig_head_name = GIT_STR_INIT, + orig_head_id = GIT_STR_INIT, + onto_id = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(repo); + + if ((error = rebase_check_versions(given_opts)) < 0) + return error; + + if (rebase_alloc(&rebase, given_opts) < 0) + return -1; + + rebase->repo = repo; + + if ((error = rebase_state_type(&rebase->type, &rebase->state_path, repo)) < 0) + goto done; + + if (rebase->type == GIT_REBASE_NONE) { + git_error_set(GIT_ERROR_REBASE, "there is no rebase in progress"); + error = GIT_ENOTFOUND; + goto done; + } + + if ((error = git_str_puts(&rebase->state_filename, rebase->state_path)) < 0) + goto done; + + if ((error = rebase_readfile(&orig_head_name, rebase, HEAD_NAME_FILE)) < 0) + goto done; + + git_str_rtrim(&orig_head_name); + + if (strcmp(ORIG_DETACHED_HEAD, orig_head_name.ptr) == 0) + rebase->head_detached = 1; + + if ((error = rebase_readoid(&rebase->orig_head_id, &orig_head_id, rebase, ORIG_HEAD_FILE)) < 0) { + /* Previous versions of git.git used 'head' here; support that. */ + if (error == GIT_ENOTFOUND) + error = rebase_readoid(&rebase->orig_head_id, &orig_head_id, rebase, HEAD_FILE); + + if (error < 0) + goto done; + } + + if ((error = rebase_readoid(&rebase->onto_id, &onto_id, rebase, ONTO_FILE)) < 0) + goto done; + + if (!rebase->head_detached) + rebase->orig_head_name = git_str_detach(&orig_head_name); + + switch (rebase->type) { + case GIT_REBASE_INTERACTIVE: + git_error_set(GIT_ERROR_REBASE, "interactive rebase is not supported"); + error = -1; + break; + case GIT_REBASE_MERGE: + error = rebase_open_merge(rebase); + break; + case GIT_REBASE_APPLY: + git_error_set(GIT_ERROR_REBASE, "patch application rebase is not supported"); + error = -1; + break; + default: + abort(); + } + +done: + if (error == 0) + *out = rebase; + else + git_rebase_free(rebase); + + git_str_dispose(&orig_head_name); + git_str_dispose(&orig_head_id); + git_str_dispose(&onto_id); + return error; +} + +static int rebase_cleanup(git_rebase *rebase) +{ + if (!rebase || rebase->inmemory) + return 0; + + return git_fs_path_isdir(rebase->state_path) ? + git_futils_rmdir_r(rebase->state_path, NULL, GIT_RMDIR_REMOVE_FILES) : + 0; +} + +static int rebase_setupfile(git_rebase *rebase, const char *filename, int flags, const char *fmt, ...) +{ + git_str path = GIT_STR_INIT, + contents = GIT_STR_INIT; + va_list ap; + int error; + + va_start(ap, fmt); + git_str_vprintf(&contents, fmt, ap); + va_end(ap); + + if ((error = git_str_joinpath(&path, rebase->state_path, filename)) == 0) + error = git_futils_writebuffer(&contents, path.ptr, flags, REBASE_FILE_MODE); + + git_str_dispose(&path); + git_str_dispose(&contents); + + return error; +} + +static const char *rebase_onto_name(const git_annotated_commit *onto) +{ + if (onto->ref_name && git__strncmp(onto->ref_name, "refs/heads/", 11) == 0) + return onto->ref_name + 11; + else if (onto->ref_name) + return onto->ref_name; + else + return onto->id_str; +} + +static int rebase_setupfiles_merge(git_rebase *rebase) +{ + git_str commit_filename = GIT_STR_INIT; + char id_str[GIT_OID_MAX_HEXSIZE + 1]; + git_rebase_operation *operation; + size_t i; + int error = 0; + + if ((error = rebase_setupfile(rebase, END_FILE, 0, "%" PRIuZ "\n", git_array_size(rebase->operations))) < 0 || + (error = rebase_setupfile(rebase, ONTO_NAME_FILE, 0, "%s\n", rebase->onto_name)) < 0) + goto done; + + for (i = 0; i < git_array_size(rebase->operations); i++) { + operation = git_array_get(rebase->operations, i); + + git_str_clear(&commit_filename); + git_str_printf(&commit_filename, CMT_FILE_FMT, i+1); + + git_oid_tostr(id_str, GIT_OID_MAX_HEXSIZE + 1, &operation->id); + + if ((error = rebase_setupfile(rebase, commit_filename.ptr, 0, "%s\n", id_str)) < 0) + goto done; + } + +done: + git_str_dispose(&commit_filename); + return error; +} + +static int rebase_setupfiles(git_rebase *rebase) +{ + char onto[GIT_OID_MAX_HEXSIZE + 1], orig_head[GIT_OID_MAX_HEXSIZE + 1]; + const char *orig_head_name; + + git_oid_tostr(onto, GIT_OID_MAX_HEXSIZE + 1, &rebase->onto_id); + git_oid_tostr(orig_head, GIT_OID_MAX_HEXSIZE + 1, &rebase->orig_head_id); + + if (p_mkdir(rebase->state_path, REBASE_DIR_MODE) < 0) { + git_error_set(GIT_ERROR_OS, "failed to create rebase directory '%s'", rebase->state_path); + return -1; + } + + orig_head_name = rebase->head_detached ? ORIG_DETACHED_HEAD : + rebase->orig_head_name; + + if (git_repository__set_orig_head(rebase->repo, &rebase->orig_head_id) < 0 || + rebase_setupfile(rebase, HEAD_NAME_FILE, 0, "%s\n", orig_head_name) < 0 || + rebase_setupfile(rebase, ONTO_FILE, 0, "%s\n", onto) < 0 || + rebase_setupfile(rebase, ORIG_HEAD_FILE, 0, "%s\n", orig_head) < 0 || + rebase_setupfile(rebase, QUIET_FILE, 0, rebase->quiet ? "t\n" : "\n") < 0) + return -1; + + return rebase_setupfiles_merge(rebase); +} + +int git_rebase_options_init(git_rebase_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_rebase_options, GIT_REBASE_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_rebase_init_options(git_rebase_options *opts, unsigned int version) +{ + return git_rebase_options_init(opts, version); +} +#endif + +static int rebase_ensure_not_in_progress(git_repository *repo) +{ + int error; + git_rebase_t type; + + if ((error = rebase_state_type(&type, NULL, repo)) < 0) + return error; + + if (type != GIT_REBASE_NONE) { + git_error_set(GIT_ERROR_REBASE, "there is an existing rebase in progress"); + return -1; + } + + return 0; +} + +static int rebase_ensure_not_dirty( + git_repository *repo, + bool check_index, + bool check_workdir, + int fail_with) +{ + git_tree *head = NULL; + git_index *index = NULL; + git_diff *diff = NULL; + int error = 0; + + if (check_index) { + if ((error = git_repository_head_tree(&head, repo)) < 0 || + (error = git_repository_index(&index, repo)) < 0 || + (error = git_diff_tree_to_index(&diff, repo, head, index, NULL)) < 0) + goto done; + + if (git_diff_num_deltas(diff) > 0) { + git_error_set(GIT_ERROR_REBASE, "uncommitted changes exist in index"); + error = fail_with; + goto done; + } + + git_diff_free(diff); + diff = NULL; + } + + if (check_workdir) { + git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; + diff_opts.ignore_submodules = GIT_SUBMODULE_IGNORE_UNTRACKED; + if ((error = git_diff_index_to_workdir(&diff, repo, index, &diff_opts)) < 0) + goto done; + + if (git_diff_num_deltas(diff) > 0) { + git_error_set(GIT_ERROR_REBASE, "unstaged changes exist in workdir"); + error = fail_with; + goto done; + } + } + +done: + git_diff_free(diff); + git_index_free(index); + git_tree_free(head); + + return error; +} + +static int rebase_init_operations( + git_rebase *rebase, + git_repository *repo, + const git_annotated_commit *branch, + const git_annotated_commit *upstream, + const git_annotated_commit *onto) +{ + git_revwalk *revwalk = NULL; + git_commit *commit; + git_oid id; + bool merge; + git_rebase_operation *operation; + int error; + + if (!upstream) + upstream = onto; + + if ((error = git_revwalk_new(&revwalk, rebase->repo)) < 0 || + (error = git_revwalk_push(revwalk, git_annotated_commit_id(branch))) < 0 || + (error = git_revwalk_hide(revwalk, git_annotated_commit_id(upstream))) < 0) + goto done; + + git_revwalk_sorting(revwalk, GIT_SORT_REVERSE); + + while ((error = git_revwalk_next(&id, revwalk)) == 0) { + if ((error = git_commit_lookup(&commit, repo, &id)) < 0) + goto done; + + merge = (git_commit_parentcount(commit) > 1); + git_commit_free(commit); + + if (merge) + continue; + + operation = rebase_operation_alloc(rebase, GIT_REBASE_OPERATION_PICK, &id, NULL); + GIT_ERROR_CHECK_ALLOC(operation); + } + + error = 0; + +done: + git_revwalk_free(revwalk); + return error; +} + +static int rebase_init_merge( + git_rebase *rebase, + git_repository *repo, + const git_annotated_commit *branch, + const git_annotated_commit *upstream, + const git_annotated_commit *onto) +{ + git_reference *head_ref = NULL; + git_commit *onto_commit = NULL; + git_str reflog = GIT_STR_INIT; + git_str state_path = GIT_STR_INIT; + int error; + + GIT_UNUSED(upstream); + + if ((error = git_str_joinpath(&state_path, repo->gitdir, REBASE_MERGE_DIR)) < 0 || + (error = git_str_put(&rebase->state_filename, state_path.ptr, state_path.size)) < 0) + goto done; + + rebase->state_path = git_str_detach(&state_path); + GIT_ERROR_CHECK_ALLOC(rebase->state_path); + + if (branch->ref_name && strcmp(branch->ref_name, "HEAD")) { + rebase->orig_head_name = git__strdup(branch->ref_name); + GIT_ERROR_CHECK_ALLOC(rebase->orig_head_name); + } else { + rebase->head_detached = 1; + } + + rebase->onto_name = git__strdup(rebase_onto_name(onto)); + GIT_ERROR_CHECK_ALLOC(rebase->onto_name); + + rebase->quiet = rebase->options.quiet; + + git_oid_cpy(&rebase->orig_head_id, git_annotated_commit_id(branch)); + git_oid_cpy(&rebase->onto_id, git_annotated_commit_id(onto)); + + if ((error = rebase_setupfiles(rebase)) < 0 || + (error = git_str_printf(&reflog, + "rebase: checkout %s", rebase_onto_name(onto))) < 0 || + (error = git_commit_lookup( + &onto_commit, repo, git_annotated_commit_id(onto))) < 0 || + (error = git_checkout_tree(repo, + (git_object *)onto_commit, &rebase->options.checkout_options)) < 0 || + (error = git_reference_create(&head_ref, repo, GIT_HEAD_FILE, + git_annotated_commit_id(onto), 1, reflog.ptr)) < 0) + goto done; + +done: + git_reference_free(head_ref); + git_commit_free(onto_commit); + git_str_dispose(&reflog); + git_str_dispose(&state_path); + + return error; +} + +static int rebase_init_inmemory( + git_rebase *rebase, + git_repository *repo, + const git_annotated_commit *branch, + const git_annotated_commit *upstream, + const git_annotated_commit *onto) +{ + GIT_UNUSED(branch); + GIT_UNUSED(upstream); + + return git_commit_lookup( + &rebase->last_commit, repo, git_annotated_commit_id(onto)); +} + +int git_rebase_init( + git_rebase **out, + git_repository *repo, + const git_annotated_commit *branch, + const git_annotated_commit *upstream, + const git_annotated_commit *onto, + const git_rebase_options *given_opts) +{ + git_rebase *rebase = NULL; + git_annotated_commit *head_branch = NULL; + git_reference *head_ref = NULL; + bool inmemory = (given_opts && given_opts->inmemory); + int error; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(upstream || onto); + + *out = NULL; + + if (!onto) + onto = upstream; + + if ((error = rebase_check_versions(given_opts)) < 0) + goto done; + + if (!inmemory) { + if ((error = git_repository__ensure_not_bare(repo, "rebase")) < 0 || + (error = rebase_ensure_not_in_progress(repo)) < 0 || + (error = rebase_ensure_not_dirty(repo, true, true, GIT_ERROR)) < 0) + goto done; + } + + if (!branch) { + if ((error = git_repository_head(&head_ref, repo)) < 0 || + (error = git_annotated_commit_from_ref(&head_branch, repo, head_ref)) < 0) + goto done; + + branch = head_branch; + } + + if (rebase_alloc(&rebase, given_opts) < 0) + return -1; + + rebase->repo = repo; + rebase->inmemory = inmemory; + rebase->type = GIT_REBASE_MERGE; + + if ((error = rebase_init_operations(rebase, repo, branch, upstream, onto)) < 0) + goto done; + + if (inmemory) + error = rebase_init_inmemory(rebase, repo, branch, upstream, onto); + else + error = rebase_init_merge(rebase, repo, branch ,upstream, onto); + + if (error == 0) + *out = rebase; + +done: + git_reference_free(head_ref); + git_annotated_commit_free(head_branch); + + if (error < 0) { + rebase_cleanup(rebase); + git_rebase_free(rebase); + } + + return error; +} + +static void normalize_checkout_options_for_apply( + git_checkout_options *checkout_opts, + git_rebase *rebase, + git_commit *current_commit) +{ + memcpy(checkout_opts, &rebase->options.checkout_options, sizeof(git_checkout_options)); + + if (!checkout_opts->ancestor_label) + checkout_opts->ancestor_label = "ancestor"; + + if (rebase->type == GIT_REBASE_MERGE) { + if (!checkout_opts->our_label) + checkout_opts->our_label = rebase->onto_name; + + if (!checkout_opts->their_label) + checkout_opts->their_label = git_commit_summary(current_commit); + } else { + abort(); + } +} + +GIT_INLINE(int) rebase_movenext(git_rebase *rebase) +{ + size_t next = rebase->started ? rebase->current + 1 : 0; + + if (next == git_array_size(rebase->operations)) + return GIT_ITEROVER; + + rebase->started = 1; + rebase->current = next; + + return 0; +} + +static int rebase_next_merge( + git_rebase_operation **out, + git_rebase *rebase) +{ + git_str path = GIT_STR_INIT; + git_commit *current_commit = NULL, *parent_commit = NULL; + git_tree *current_tree = NULL, *head_tree = NULL, *parent_tree = NULL; + git_index *index = NULL; + git_indexwriter indexwriter = GIT_INDEXWRITER_INIT; + git_rebase_operation *operation; + git_checkout_options checkout_opts; + char current_idstr[GIT_OID_MAX_HEXSIZE + 1]; + unsigned int parent_count; + int error; + + *out = NULL; + + operation = git_array_get(rebase->operations, rebase->current); + + if ((error = git_commit_lookup(¤t_commit, rebase->repo, &operation->id)) < 0 || + (error = git_commit_tree(¤t_tree, current_commit)) < 0 || + (error = git_repository_head_tree(&head_tree, rebase->repo)) < 0) + goto done; + + if ((parent_count = git_commit_parentcount(current_commit)) > 1) { + git_error_set(GIT_ERROR_REBASE, "cannot rebase a merge commit"); + error = -1; + goto done; + } else if (parent_count) { + if ((error = git_commit_parent(&parent_commit, current_commit, 0)) < 0 || + (error = git_commit_tree(&parent_tree, parent_commit)) < 0) + goto done; + } + + git_oid_tostr(current_idstr, GIT_OID_MAX_HEXSIZE + 1, &operation->id); + + normalize_checkout_options_for_apply(&checkout_opts, rebase, current_commit); + + if ((error = git_indexwriter_init_for_operation(&indexwriter, rebase->repo, &checkout_opts.checkout_strategy)) < 0 || + (error = rebase_setupfile(rebase, MSGNUM_FILE, 0, "%" PRIuZ "\n", rebase->current+1)) < 0 || + (error = rebase_setupfile(rebase, CURRENT_FILE, 0, "%s\n", current_idstr)) < 0 || + (error = git_merge_trees(&index, rebase->repo, parent_tree, head_tree, current_tree, &rebase->options.merge_options)) < 0 || + (error = git_merge__check_result(rebase->repo, index)) < 0 || + (error = git_checkout_index(rebase->repo, index, &checkout_opts)) < 0 || + (error = git_indexwriter_commit(&indexwriter)) < 0) + goto done; + + *out = operation; + +done: + git_indexwriter_cleanup(&indexwriter); + git_index_free(index); + git_tree_free(current_tree); + git_tree_free(head_tree); + git_tree_free(parent_tree); + git_commit_free(parent_commit); + git_commit_free(current_commit); + git_str_dispose(&path); + + return error; +} + +static int rebase_next_inmemory( + git_rebase_operation **out, + git_rebase *rebase) +{ + git_commit *current_commit = NULL, *parent_commit = NULL; + git_tree *current_tree = NULL, *head_tree = NULL, *parent_tree = NULL; + git_rebase_operation *operation; + git_index *index = NULL; + unsigned int parent_count; + int error; + + *out = NULL; + + operation = git_array_get(rebase->operations, rebase->current); + + if ((error = git_commit_lookup(¤t_commit, rebase->repo, &operation->id)) < 0 || + (error = git_commit_tree(¤t_tree, current_commit)) < 0) + goto done; + + if ((parent_count = git_commit_parentcount(current_commit)) > 1) { + git_error_set(GIT_ERROR_REBASE, "cannot rebase a merge commit"); + error = -1; + goto done; + } else if (parent_count) { + if ((error = git_commit_parent(&parent_commit, current_commit, 0)) < 0 || + (error = git_commit_tree(&parent_tree, parent_commit)) < 0) + goto done; + } + + if ((error = git_commit_tree(&head_tree, rebase->last_commit)) < 0 || + (error = git_merge_trees(&index, rebase->repo, parent_tree, head_tree, current_tree, &rebase->options.merge_options)) < 0) + goto done; + + if (!rebase->index) { + rebase->index = index; + index = NULL; + } else { + if ((error = git_index_read_index(rebase->index, index)) < 0) + goto done; + } + + *out = operation; + +done: + git_commit_free(current_commit); + git_commit_free(parent_commit); + git_tree_free(current_tree); + git_tree_free(head_tree); + git_tree_free(parent_tree); + git_index_free(index); + + return error; +} + +int git_rebase_next( + git_rebase_operation **out, + git_rebase *rebase) +{ + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(rebase); + + if ((error = rebase_movenext(rebase)) < 0) + return error; + + if (rebase->inmemory) + error = rebase_next_inmemory(out, rebase); + else if (rebase->type == GIT_REBASE_MERGE) + error = rebase_next_merge(out, rebase); + else + abort(); + + return error; +} + +int git_rebase_inmemory_index( + git_index **out, + git_rebase *rebase) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(rebase); + GIT_ASSERT_ARG(rebase->index); + + GIT_REFCOUNT_INC(rebase->index); + *out = rebase->index; + + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +static int create_signed( + git_oid *out, + git_rebase *rebase, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + git_tree *tree, + size_t parent_count, + const git_commit **parents) +{ + git_str commit_content = GIT_STR_INIT; + git_buf commit_signature = { NULL, 0, 0 }, + signature_field = { NULL, 0, 0 }; + int error; + + git_error_clear(); + + if ((error = git_commit__create_buffer(&commit_content, + rebase->repo, author, committer, message_encoding, + message, tree, parent_count, parents)) < 0) + goto done; + + error = rebase->options.signing_cb(&commit_signature, + &signature_field, commit_content.ptr, + rebase->options.payload); + + if (error) { + if (error != GIT_PASSTHROUGH) + git_error_set_after_callback_function(error, "signing_cb"); + + goto done; + } + + error = git_commit_create_with_signature(out, rebase->repo, + commit_content.ptr, + commit_signature.size > 0 ? commit_signature.ptr : NULL, + signature_field.size > 0 ? signature_field.ptr : NULL); + +done: + git_buf_dispose(&commit_signature); + git_buf_dispose(&signature_field); + git_str_dispose(&commit_content); + return error; +} +#endif + +static int rebase_commit__create( + git_commit **out, + git_rebase *rebase, + git_index *index, + git_commit *parent_commit, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message) +{ + git_rebase_operation *operation; + git_commit *current_commit = NULL, *commit = NULL; + git_tree *parent_tree = NULL, *tree = NULL; + git_oid tree_id, commit_id; + int error; + + operation = git_array_get(rebase->operations, rebase->current); + + if (git_index_has_conflicts(index)) { + git_error_set(GIT_ERROR_REBASE, "conflicts have not been resolved"); + error = GIT_EUNMERGED; + goto done; + } + + if ((error = git_commit_lookup(¤t_commit, rebase->repo, &operation->id)) < 0 || + (error = git_commit_tree(&parent_tree, parent_commit)) < 0 || + (error = git_index_write_tree_to(&tree_id, index, rebase->repo)) < 0 || + (error = git_tree_lookup(&tree, rebase->repo, &tree_id)) < 0) + goto done; + + if (git_oid_equal(&tree_id, git_tree_id(parent_tree))) { + git_error_set(GIT_ERROR_REBASE, "this patch has already been applied"); + error = GIT_EAPPLIED; + goto done; + } + + if (!author) + author = git_commit_author(current_commit); + + if (!message) { + message_encoding = git_commit_message_encoding(current_commit); + message = git_commit_message(current_commit); + } + + git_error_clear(); + error = GIT_PASSTHROUGH; + + if (rebase->options.commit_create_cb) { + error = rebase->options.commit_create_cb(&commit_id, + author, committer, message_encoding, message, + tree, 1, (const git_commit **)&parent_commit, + rebase->options.payload); + + git_error_set_after_callback_function(error, + "commit_create_cb"); + } +#ifndef GIT_DEPRECATE_HARD + else if (rebase->options.signing_cb) { + error = create_signed(&commit_id, rebase, author, + committer, message_encoding, message, tree, + 1, (const git_commit **)&parent_commit); + } +#endif + + if (error == GIT_PASSTHROUGH) + error = git_commit_create(&commit_id, rebase->repo, NULL, + author, committer, message_encoding, message, + tree, 1, (const git_commit **)&parent_commit); + + if (error) + goto done; + + if ((error = git_commit_lookup(&commit, rebase->repo, &commit_id)) < 0) + goto done; + + *out = commit; + +done: + if (error < 0) + git_commit_free(commit); + + git_commit_free(current_commit); + git_tree_free(parent_tree); + git_tree_free(tree); + + return error; +} + +static int rebase_commit_merge( + git_oid *commit_id, + git_rebase *rebase, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message) +{ + git_rebase_operation *operation; + git_reference *head = NULL; + git_commit *head_commit = NULL, *commit = NULL; + git_index *index = NULL; + char old_idstr[GIT_OID_MAX_HEXSIZE + 1], new_idstr[GIT_OID_MAX_HEXSIZE + 1]; + int error; + + operation = git_array_get(rebase->operations, rebase->current); + GIT_ASSERT(operation); + + if ((error = rebase_ensure_not_dirty(rebase->repo, false, true, GIT_EUNMERGED)) < 0 || + (error = git_repository_head(&head, rebase->repo)) < 0 || + (error = git_reference_peel((git_object **)&head_commit, head, GIT_OBJECT_COMMIT)) < 0 || + (error = git_repository_index(&index, rebase->repo)) < 0 || + (error = rebase_commit__create(&commit, rebase, index, head_commit, + author, committer, message_encoding, message)) < 0 || + (error = git_reference__update_for_commit( + rebase->repo, NULL, "HEAD", git_commit_id(commit), "rebase")) < 0) + goto done; + + git_oid_tostr(old_idstr, GIT_OID_MAX_HEXSIZE + 1, &operation->id); + git_oid_tostr(new_idstr, GIT_OID_MAX_HEXSIZE + 1, git_commit_id(commit)); + + if ((error = rebase_setupfile(rebase, REWRITTEN_FILE, O_CREAT|O_WRONLY|O_APPEND, + "%s %s\n", old_idstr, new_idstr)) < 0) + goto done; + + git_oid_cpy(commit_id, git_commit_id(commit)); + +done: + git_index_free(index); + git_reference_free(head); + git_commit_free(head_commit); + git_commit_free(commit); + return error; +} + +static int rebase_commit_inmemory( + git_oid *commit_id, + git_rebase *rebase, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message) +{ + git_commit *commit = NULL; + int error = 0; + + GIT_ASSERT_ARG(rebase->index); + GIT_ASSERT_ARG(rebase->last_commit); + GIT_ASSERT_ARG(rebase->current < rebase->operations.size); + + if ((error = rebase_commit__create(&commit, rebase, rebase->index, + rebase->last_commit, author, committer, message_encoding, message)) < 0) + goto done; + + git_commit_free(rebase->last_commit); + rebase->last_commit = commit; + + git_oid_cpy(commit_id, git_commit_id(commit)); + +done: + if (error < 0) + git_commit_free(commit); + + return error; +} + +int git_rebase_commit( + git_oid *id, + git_rebase *rebase, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message) +{ + int error; + + GIT_ASSERT_ARG(rebase); + GIT_ASSERT_ARG(committer); + + if (rebase->inmemory) + error = rebase_commit_inmemory( + id, rebase, author, committer, message_encoding, message); + else if (rebase->type == GIT_REBASE_MERGE) + error = rebase_commit_merge( + id, rebase, author, committer, message_encoding, message); + else + abort(); + + return error; +} + +int git_rebase_abort(git_rebase *rebase) +{ + git_reference *orig_head_ref = NULL; + git_commit *orig_head_commit = NULL; + int error; + + GIT_ASSERT_ARG(rebase); + + if (rebase->inmemory) + return 0; + + error = rebase->head_detached ? + git_reference_create(&orig_head_ref, rebase->repo, GIT_HEAD_FILE, + &rebase->orig_head_id, 1, "rebase: aborting") : + git_reference_symbolic_create( + &orig_head_ref, rebase->repo, GIT_HEAD_FILE, rebase->orig_head_name, 1, + "rebase: aborting"); + + if (error < 0) + goto done; + + if ((error = git_commit_lookup( + &orig_head_commit, rebase->repo, &rebase->orig_head_id)) < 0 || + (error = git_reset(rebase->repo, (git_object *)orig_head_commit, + GIT_RESET_HARD, &rebase->options.checkout_options)) < 0) + goto done; + + error = rebase_cleanup(rebase); + +done: + git_commit_free(orig_head_commit); + git_reference_free(orig_head_ref); + + return error; +} + +static int notes_ref_lookup(git_str *out, git_rebase *rebase) +{ + git_config *config = NULL; + int do_rewrite, error; + + if (rebase->options.rewrite_notes_ref) { + git_str_attach_notowned(out, + rebase->options.rewrite_notes_ref, + strlen(rebase->options.rewrite_notes_ref)); + return 0; + } + + if ((error = git_repository_config(&config, rebase->repo)) < 0 || + (error = git_config_get_bool(&do_rewrite, config, "notes.rewrite.rebase")) < 0) { + + if (error != GIT_ENOTFOUND) + goto done; + + git_error_clear(); + do_rewrite = 1; + } + + error = do_rewrite ? + git_config__get_string_buf(out, config, "notes.rewriteref") : + GIT_ENOTFOUND; + +done: + git_config_free(config); + return error; +} + +static int rebase_copy_note( + git_rebase *rebase, + const char *notes_ref, + git_oid *from, + git_oid *to, + const git_signature *committer) +{ + git_note *note = NULL; + git_oid note_id; + git_signature *who = NULL; + int error; + + if ((error = git_note_read(¬e, rebase->repo, notes_ref, from)) < 0) { + if (error == GIT_ENOTFOUND) { + git_error_clear(); + error = 0; + } + + goto done; + } + + if (!committer) { + if((error = git_signature_default(&who, rebase->repo)) < 0) { + if (error != GIT_ENOTFOUND || + (error = git_signature_now(&who, "unknown", "unknown")) < 0) + goto done; + + git_error_clear(); + } + + committer = who; + } + + error = git_note_create(¬e_id, rebase->repo, notes_ref, + git_note_author(note), committer, to, git_note_message(note), 0); + +done: + git_note_free(note); + git_signature_free(who); + + return error; +} + +static int rebase_copy_notes( + git_rebase *rebase, + const git_signature *committer) +{ + git_str path = GIT_STR_INIT, + rewritten = GIT_STR_INIT, + notes_ref = GIT_STR_INIT; + char *pair_list, *fromstr, *tostr, *end; + git_oid from, to; + unsigned int linenum = 1; + int error = 0; + + if ((error = notes_ref_lookup(¬es_ref, rebase)) < 0) { + if (error == GIT_ENOTFOUND) { + git_error_clear(); + error = 0; + } + + goto done; + } + + if ((error = git_str_joinpath(&path, rebase->state_path, REWRITTEN_FILE)) < 0 || + (error = git_futils_readbuffer(&rewritten, path.ptr)) < 0) + goto done; + + pair_list = rewritten.ptr; + + while (*pair_list) { + fromstr = pair_list; + + if ((end = strchr(fromstr, '\n')) == NULL) + goto on_error; + + pair_list = end+1; + *end = '\0'; + + if ((end = strchr(fromstr, ' ')) == NULL) + goto on_error; + + tostr = end+1; + *end = '\0'; + + if (strlen(fromstr) != git_oid_hexsize(rebase->repo->oid_type) || + strlen(tostr) != git_oid_hexsize(rebase->repo->oid_type) || + git_oid_from_string(&from, fromstr, rebase->repo->oid_type) < 0 || + git_oid_from_string(&to, tostr, rebase->repo->oid_type) < 0) + goto on_error; + + if ((error = rebase_copy_note(rebase, notes_ref.ptr, &from, &to, committer)) < 0) + goto done; + + linenum++; + } + + goto done; + +on_error: + git_error_set(GIT_ERROR_REBASE, "invalid rewritten file at line %d", linenum); + error = -1; + +done: + git_str_dispose(&rewritten); + git_str_dispose(&path); + git_str_dispose(¬es_ref); + + return error; +} + +static int return_to_orig_head(git_rebase *rebase) +{ + git_reference *terminal_ref = NULL, *branch_ref = NULL, *head_ref = NULL; + git_commit *terminal_commit = NULL; + git_str branch_msg = GIT_STR_INIT, head_msg = GIT_STR_INIT; + char onto[GIT_OID_MAX_HEXSIZE + 1]; + int error = 0; + + git_oid_tostr(onto, GIT_OID_MAX_HEXSIZE + 1, &rebase->onto_id); + + if ((error = git_str_printf(&branch_msg, + "rebase finished: %s onto %s", rebase->orig_head_name, onto)) == 0 && + (error = git_str_printf(&head_msg, + "rebase finished: returning to %s", rebase->orig_head_name)) == 0 && + (error = git_repository_head(&terminal_ref, rebase->repo)) == 0 && + (error = git_reference_peel((git_object **)&terminal_commit, + terminal_ref, GIT_OBJECT_COMMIT)) == 0 && + (error = git_reference_create_matching(&branch_ref, + rebase->repo, rebase->orig_head_name, + git_commit_id(terminal_commit), 1, + &rebase->orig_head_id, branch_msg.ptr)) == 0) + error = git_reference_symbolic_create(&head_ref, + rebase->repo, GIT_HEAD_FILE, rebase->orig_head_name, 1, + head_msg.ptr); + + git_str_dispose(&head_msg); + git_str_dispose(&branch_msg); + git_commit_free(terminal_commit); + git_reference_free(head_ref); + git_reference_free(branch_ref); + git_reference_free(terminal_ref); + + return error; +} + +int git_rebase_finish( + git_rebase *rebase, + const git_signature *signature) +{ + int error = 0; + + GIT_ASSERT_ARG(rebase); + + if (rebase->inmemory) + return 0; + + if (!rebase->head_detached) + error = return_to_orig_head(rebase); + + if (error == 0 && (error = rebase_copy_notes(rebase, signature)) == 0) + error = rebase_cleanup(rebase); + + return error; +} + +const char *git_rebase_orig_head_name(git_rebase *rebase) { + GIT_ASSERT_ARG_WITH_RETVAL(rebase, NULL); + return rebase->orig_head_name; +} + +const git_oid *git_rebase_orig_head_id(git_rebase *rebase) { + GIT_ASSERT_ARG_WITH_RETVAL(rebase, NULL); + return &rebase->orig_head_id; +} + +const char *git_rebase_onto_name(git_rebase *rebase) { + GIT_ASSERT_ARG_WITH_RETVAL(rebase, NULL); + return rebase->onto_name; +} + +const git_oid *git_rebase_onto_id(git_rebase *rebase) { + return &rebase->onto_id; +} + +size_t git_rebase_operation_entrycount(git_rebase *rebase) +{ + GIT_ASSERT_ARG_WITH_RETVAL(rebase, 0); + + return git_array_size(rebase->operations); +} + +size_t git_rebase_operation_current(git_rebase *rebase) +{ + GIT_ASSERT_ARG_WITH_RETVAL(rebase, 0); + + return rebase->started ? rebase->current : GIT_REBASE_NO_OPERATION; +} + +git_rebase_operation *git_rebase_operation_byindex(git_rebase *rebase, size_t idx) +{ + GIT_ASSERT_ARG_WITH_RETVAL(rebase, NULL); + + return git_array_get(rebase->operations, idx); +} + +void git_rebase_free(git_rebase *rebase) +{ + if (rebase == NULL) + return; + + git_index_free(rebase->index); + git_commit_free(rebase->last_commit); + git__free(rebase->onto_name); + git__free(rebase->orig_head_name); + git__free(rebase->state_path); + git_str_dispose(&rebase->state_filename); + git_array_clear(rebase->operations); + git__free((char *)rebase->options.rewrite_notes_ref); + git__free(rebase); +} diff --git a/src/libgit2/refdb.c b/src/libgit2/refdb.c new file mode 100644 index 00000000000..bf08b8c1fa3 --- /dev/null +++ b/src/libgit2/refdb.c @@ -0,0 +1,442 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "refdb.h" + +#include "git2/object.h" +#include "git2/refs.h" +#include "git2/refdb.h" +#include "git2/sys/refdb_backend.h" + +#include "hash.h" +#include "refs.h" +#include "reflog.h" +#include "posix.h" + +#define DEFAULT_NESTING_LEVEL 5 +#define MAX_NESTING_LEVEL 10 + +int git_refdb_new(git_refdb **out, git_repository *repo) +{ + git_refdb *db; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + db = git__calloc(1, sizeof(*db)); + GIT_ERROR_CHECK_ALLOC(db); + + db->repo = repo; + + *out = db; + GIT_REFCOUNT_INC(db); + return 0; +} + +int git_refdb_open(git_refdb **out, git_repository *repo) +{ + git_refdb_backend *backend; + git_refdb *db; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + *out = NULL; + + if ((error = git_refdb_new(&db, repo)) < 0) + goto out; + + switch (repo->refdb_type) { + case GIT_REFDB_FILES: + if ((error = git_refdb_backend_fs(&backend, repo)) < 0) + goto out; + break; + default: + git_error_set(GIT_ERROR_REFERENCE, "unknown reference storage format"); + error = GIT_EINVALID; + goto out; + } + + db->repo = repo; + db->backend = backend; + *out = db; +out: + if (error) + git_refdb_free(db); + return error; +} + +static void refdb_free_backend(git_refdb *db) +{ + if (db->backend) + db->backend->free(db->backend); +} + +int git_refdb_set_backend(git_refdb *db, git_refdb_backend *backend) +{ + GIT_ERROR_CHECK_VERSION(backend, GIT_REFDB_BACKEND_VERSION, "git_refdb_backend"); + + if (!backend->exists || !backend->lookup || !backend->iterator || + !backend->write || !backend->rename || !backend->del || + !backend->has_log || !backend->ensure_log || !backend->free || + !backend->reflog_read || !backend->reflog_write || + !backend->reflog_rename || !backend->reflog_delete || + (backend->lock && !backend->unlock)) { + git_error_set(GIT_ERROR_REFERENCE, "incomplete refdb backend implementation"); + return GIT_EINVALID; + } + + refdb_free_backend(db); + db->backend = backend; + + return 0; +} + +int git_refdb_compress(git_refdb *db) +{ + GIT_ASSERT_ARG(db); + + if (db->backend->compress) + return db->backend->compress(db->backend); + + return 0; +} + +void git_refdb__free(git_refdb *db) +{ + refdb_free_backend(db); + git__memzero(db, sizeof(*db)); + git__free(db); +} + +void git_refdb_free(git_refdb *db) +{ + if (db == NULL) + return; + + GIT_REFCOUNT_DEC(db, git_refdb__free); +} + +int git_refdb_init(git_refdb *refdb, const char *head_target, mode_t mode, uint32_t flags) +{ + GIT_ASSERT_ARG(refdb); + GIT_ASSERT_ARG(refdb->backend); + + if (!refdb->backend->init) + return 0; + return refdb->backend->init(refdb->backend, head_target, mode, flags); +} + +int git_refdb_exists(int *exists, git_refdb *refdb, const char *ref_name) +{ + GIT_ASSERT_ARG(exists); + GIT_ASSERT_ARG(refdb); + GIT_ASSERT_ARG(refdb->backend); + + return refdb->backend->exists(exists, refdb->backend, ref_name); +} + +int git_refdb_lookup(git_reference **out, git_refdb *db, const char *ref_name) +{ + git_reference *ref; + int error; + + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(db->backend); + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(ref_name); + + error = db->backend->lookup(&ref, db->backend, ref_name); + if (error < 0) + return error; + + GIT_REFCOUNT_INC(db); + ref->db = db; + + *out = ref; + return 0; +} + +int git_refdb_resolve( + git_reference **out, + git_refdb *db, + const char *ref_name, + int max_nesting) +{ + git_reference *ref = NULL; + int error = 0, nesting; + + *out = NULL; + + if (max_nesting > MAX_NESTING_LEVEL) + max_nesting = MAX_NESTING_LEVEL; + else if (max_nesting < 0) + max_nesting = DEFAULT_NESTING_LEVEL; + + if ((error = git_refdb_lookup(&ref, db, ref_name)) < 0) + goto out; + + for (nesting = 0; nesting < max_nesting; nesting++) { + git_reference *resolved; + + if (ref->type == GIT_REFERENCE_DIRECT) + break; + + if ((error = git_refdb_lookup(&resolved, db, git_reference_symbolic_target(ref))) < 0) { + /* If we found a symbolic reference with a nonexistent target, return it. */ + if (error == GIT_ENOTFOUND) { + error = 0; + *out = ref; + ref = NULL; + } + goto out; + } + + git_reference_free(ref); + ref = resolved; + } + + if (ref->type != GIT_REFERENCE_DIRECT && max_nesting != 0) { + git_error_set(GIT_ERROR_REFERENCE, + "cannot resolve reference (>%u levels deep)", max_nesting); + error = -1; + goto out; + } + + *out = ref; + ref = NULL; +out: + git_reference_free(ref); + return error; +} + +int git_refdb_iterator(git_reference_iterator **out, git_refdb *db, const char *glob) +{ + int error; + + if (!db->backend || !db->backend->iterator) { + git_error_set(GIT_ERROR_REFERENCE, "this backend doesn't support iterators"); + return -1; + } + + if ((error = db->backend->iterator(out, db->backend, glob)) < 0) + return error; + + GIT_REFCOUNT_INC(db); + (*out)->db = db; + + return 0; +} + +int git_refdb_iterator_next(git_reference **out, git_reference_iterator *iter) +{ + int error; + + if ((error = iter->next(out, iter)) < 0) + return error; + + GIT_REFCOUNT_INC(iter->db); + (*out)->db = iter->db; + + return 0; +} + +int git_refdb_iterator_next_name(const char **out, git_reference_iterator *iter) +{ + return iter->next_name(out, iter); +} + +void git_refdb_iterator_free(git_reference_iterator *iter) +{ + GIT_REFCOUNT_DEC(iter->db, git_refdb__free); + iter->free(iter); +} + +int git_refdb_write(git_refdb *db, git_reference *ref, int force, const git_signature *who, const char *message, const git_oid *old_id, const char *old_target) +{ + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(db->backend); + + GIT_REFCOUNT_INC(db); + ref->db = db; + + return db->backend->write(db->backend, ref, force, who, message, old_id, old_target); +} + +int git_refdb_rename( + git_reference **out, + git_refdb *db, + const char *old_name, + const char *new_name, + int force, + const git_signature *who, + const char *message) +{ + int error; + + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(db->backend); + + error = db->backend->rename(out, db->backend, old_name, new_name, force, who, message); + if (error < 0) + return error; + + if (out) { + GIT_REFCOUNT_INC(db); + (*out)->db = db; + } + + return 0; +} + +int git_refdb_delete(struct git_refdb *db, const char *ref_name, const git_oid *old_id, const char *old_target) +{ + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(db->backend); + + return db->backend->del(db->backend, ref_name, old_id, old_target); +} + +int git_refdb_reflog_read(git_reflog **out, git_refdb *db, const char *name) +{ + int error; + + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(db->backend); + + if ((error = db->backend->reflog_read(out, db->backend, name)) < 0) + return error; + + GIT_REFCOUNT_INC(db); + (*out)->db = db; + + return 0; +} + +int git_refdb_should_write_reflog(int *out, git_refdb *db, const git_reference *ref) +{ + int error, logall; + + error = git_repository__configmap_lookup(&logall, db->repo, GIT_CONFIGMAP_LOGALLREFUPDATES); + if (error < 0) + return error; + + /* Defaults to the opposite of the repo being bare */ + if (logall == GIT_LOGALLREFUPDATES_UNSET) + logall = !git_repository_is_bare(db->repo); + + *out = 0; + switch (logall) { + case GIT_LOGALLREFUPDATES_FALSE: + *out = 0; + break; + + case GIT_LOGALLREFUPDATES_TRUE: + /* Only write if it already has a log, + * or if it's under heads/, remotes/ or notes/ + */ + *out = git_refdb_has_log(db, ref->name) || + !git__prefixcmp(ref->name, GIT_REFS_HEADS_DIR) || + !git__strcmp(ref->name, GIT_HEAD_FILE) || + !git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR) || + !git__prefixcmp(ref->name, GIT_REFS_NOTES_DIR); + break; + + case GIT_LOGALLREFUPDATES_ALWAYS: + *out = 1; + break; + } + + return 0; +} + +int git_refdb_should_write_head_reflog(int *out, git_refdb *db, const git_reference *ref) +{ + git_reference *head = NULL, *resolved = NULL; + const char *name; + int error; + + *out = 0; + + if (ref->type == GIT_REFERENCE_SYMBOLIC) { + error = 0; + goto out; + } + + if ((error = git_refdb_lookup(&head, db, GIT_HEAD_FILE)) < 0) + goto out; + + if (git_reference_type(head) == GIT_REFERENCE_DIRECT) + goto out; + + /* Go down the symref chain until we find the branch */ + if ((error = git_refdb_resolve(&resolved, db, git_reference_symbolic_target(head), -1)) < 0) { + if (error != GIT_ENOTFOUND) + goto out; + error = 0; + name = git_reference_symbolic_target(head); + } else if (git_reference_type(resolved) == GIT_REFERENCE_SYMBOLIC) { + name = git_reference_symbolic_target(resolved); + } else { + name = git_reference_name(resolved); + } + + if (strcmp(name, ref->name)) + goto out; + + *out = 1; + +out: + git_reference_free(resolved); + git_reference_free(head); + return error; +} + +int git_refdb_has_log(git_refdb *db, const char *refname) +{ + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(refname); + + return db->backend->has_log(db->backend, refname); +} + +int git_refdb_ensure_log(git_refdb *db, const char *refname) +{ + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(refname); + + return db->backend->ensure_log(db->backend, refname); +} + +int git_refdb_init_backend(git_refdb_backend *backend, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + backend, version, git_refdb_backend, GIT_REFDB_BACKEND_INIT); + return 0; +} + +int git_refdb_lock(void **payload, git_refdb *db, const char *refname) +{ + GIT_ASSERT_ARG(payload); + GIT_ASSERT_ARG(db); + GIT_ASSERT_ARG(refname); + + if (!db->backend->lock) { + git_error_set(GIT_ERROR_REFERENCE, "backend does not support locking"); + return -1; + } + + return db->backend->lock(payload, db->backend, refname); +} + +int git_refdb_unlock(git_refdb *db, void *payload, int success, int update_reflog, const git_reference *ref, const git_signature *sig, const char *message) +{ + GIT_ASSERT_ARG(db); + + return db->backend->unlock(db->backend, payload, success, update_reflog, ref, sig, message); +} diff --git a/src/libgit2/refdb.h b/src/libgit2/refdb.h new file mode 100644 index 00000000000..6a5ae32992d --- /dev/null +++ b/src/libgit2/refdb.h @@ -0,0 +1,152 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_refdb_h__ +#define INCLUDE_refdb_h__ + +#include "common.h" + +#include "git2/refdb.h" +#include "repository.h" + +#define GIT_INVALID_HEAD "refs/heads/.invalid" + +struct git_refdb { + git_refcount rc; + git_repository *repo; + git_refdb_backend *backend; +}; + +void git_refdb__free(git_refdb *db); + +int git_refdb_init(git_refdb *refdb, + const char *head_target, + mode_t mode, + uint32_t flags); + +int git_refdb_exists( + int *exists, + git_refdb *refdb, + const char *ref_name); + +int git_refdb_lookup( + git_reference **out, + git_refdb *refdb, + const char *ref_name); + +/** + * Resolve the reference by following symbolic references. + * + * Given a reference name, this function will follow any symbolic references up + * to `max_nesting` deep and return the resolved direct reference. If any of + * the intermediate symbolic references points to a non-existing reference, + * then that symbolic reference is returned instead with an error code of `0`. + * If the given reference is a direct reference already, it is returned + * directly. + * + * If `max_nesting` is `0`, the reference will not be resolved. If it's + * negative, it will be set to the default resolve depth which is `5`. + * + * @param out Pointer to store the result in. + * @param db The refdb to use for resolving the reference. + * @param ref_name The reference name to lookup and resolve. + * @param max_nesting The maximum nesting depth. + * @return `0` on success, a negative error code otherwise. + */ +int git_refdb_resolve( + git_reference **out, + git_refdb *db, + const char *ref_name, + int max_nesting); + +int git_refdb_rename( + git_reference **out, + git_refdb *db, + const char *old_name, + const char *new_name, + int force, + const git_signature *who, + const char *message); + +int git_refdb_iterator(git_reference_iterator **out, git_refdb *db, const char *glob); +int git_refdb_iterator_next(git_reference **out, git_reference_iterator *iter); +int git_refdb_iterator_next_name(const char **out, git_reference_iterator *iter); +void git_refdb_iterator_free(git_reference_iterator *iter); + +int git_refdb_write(git_refdb *refdb, git_reference *ref, int force, const git_signature *who, const char *message, const git_oid *old_id, const char *old_target); +int git_refdb_delete(git_refdb *refdb, const char *ref_name, const git_oid *old_id, const char *old_target); + +int git_refdb_reflog_read(git_reflog **out, git_refdb *db, const char *name); +int git_refdb_reflog_write(git_reflog *reflog); + +/** + * Determine whether a reflog entry should be created for the given reference. + * + * Whether or not writing to a reference should create a reflog entry is + * dependent on a number of things. Most importantly, there's the + * "core.logAllRefUpdates" setting that controls in which situations a + * reference should get a corresponding reflog entry. The following values for + * it are understood: + * + * - "false": Do not log reference updates. + * + * - "true": Log normal reference updates. This will write entries for + * references in "refs/heads", "refs/remotes", "refs/notes" and + * "HEAD" or if the reference already has a log entry. + * + * - "always": Always create a reflog entry. + * + * If unset, the value will default to "true" for non-bare repositories and + * "false" for bare ones. + * + * @param out pointer to which the result will be written, `1` means a reflog + * entry should be written, `0` means none should be written. + * @param db The refdb to decide this for. + * @param ref The reference one wants to check. + * @return `0` on success, a negative error code otherwise. + */ +int git_refdb_should_write_reflog(int *out, git_refdb *db, const git_reference *ref); + +/** + * Determine whether a reflog entry should be created for HEAD if creating one + * for the given reference + * + * In case the given reference is being pointed to by HEAD, then creating a + * reflog entry for this reference also requires us to create a corresponding + * reflog entry for HEAD. This function can be used to determine that scenario. + * + * @param out pointer to which the result will be written, `1` means a reflog + * entry should be written, `0` means none should be written. + * @param db The refdb to decide this for. + * @param ref The reference one wants to check. + * @return `0` on success, a negative error code otherwise. + */ +int git_refdb_should_write_head_reflog(int *out, git_refdb *db, const git_reference *ref); + +int git_refdb_has_log(git_refdb *db, const char *refname); +int git_refdb_ensure_log(git_refdb *refdb, const char *refname); + +int git_refdb_lock(void **payload, git_refdb *db, const char *refname); +int git_refdb_unlock(git_refdb *db, void *payload, int success, int update_reflog, const git_reference *ref, const git_signature *sig, const char *message); + +GIT_INLINE(const char *) git_refdb_type_name(git_refdb_t type) +{ + switch (type) { + case GIT_REFDB_FILES: + return "files"; + default: + return "unknown"; + } +} + +GIT_INLINE(git_refdb_t) git_refdb_type_fromstr(const char *name) +{ + if (strcmp(name, "files") == 0) + return GIT_REFDB_FILES; + return 0; +} + +#endif diff --git a/src/libgit2/refdb_fs.c b/src/libgit2/refdb_fs.c new file mode 100644 index 00000000000..6db7a3029d7 --- /dev/null +++ b/src/libgit2/refdb_fs.c @@ -0,0 +1,2630 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "refs.h" +#include "hash.h" +#include "repository.h" +#include "futils.h" +#include "filebuf.h" +#include "pack.h" +#include "parse.h" +#include "reflog.h" +#include "refdb.h" +#include "iterator.h" +#include "sortedcache.h" +#include "signature.h" +#include "wildmatch.h" +#include "path.h" + +#include +#include +#include +#include +#include +#include + +#define DEFAULT_NESTING_LEVEL 5 +#define MAX_NESTING_LEVEL 10 + +enum { + PACKREF_HAS_PEEL = 1, + PACKREF_WAS_LOOSE = 2, + PACKREF_CANNOT_PEEL = 4, + PACKREF_SHADOWED = 8 +}; + +enum { + PEELING_NONE = 0, + PEELING_STANDARD, + PEELING_FULL +}; + +struct packref { + git_oid oid; + git_oid peel; + char flags; + char name[GIT_FLEX_ARRAY]; +}; + +typedef struct refdb_fs_backend { + git_refdb_backend parent; + + git_repository *repo; + /* path to git directory */ + char *gitpath; + /* path to common objects' directory */ + char *commonpath; + + git_oid_t oid_type; + + unsigned int fsync : 1, + sorted : 1; + int peeling_mode; + git_iterator_flag_t iterator_flags; + uint32_t direach_flags; + git_sortedcache *refcache; + git_map packed_refs_map; + git_mutex prlock; /* protect packed_refs_map */ + git_futils_filestamp packed_refs_stamp; +} refdb_fs_backend; + +static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name); +static char *packed_set_peeling_mode(char *data, size_t data_sz, refdb_fs_backend *backend); + +GIT_INLINE(int) loose_path( + git_str *out, + const char *base, + const char *refname) +{ + if (git_str_joinpath(out, base, refname) < 0) + return -1; + + return git_fs_path_validate_str_length_with_suffix(out, + CONST_STRLEN(".lock")); +} + +GIT_INLINE(int) reflog_path( + git_str *out, + git_repository *repo, + const char *refname) +{ + const char *base; + int error; + + base = (strcmp(refname, GIT_HEAD_FILE) == 0) ? repo->gitdir : + repo->commondir; + + if ((error = git_str_joinpath(out, base, GIT_REFLOG_DIR)) < 0) + return error; + + return loose_path(out, out->ptr, refname); +} + +static int packref_cmp(const void *a_, const void *b_) +{ + const struct packref *a = a_, *b = b_; + return strcmp(a->name, b->name); +} + +static int packed_reload(refdb_fs_backend *backend) +{ + int error; + git_str packedrefs = GIT_STR_INIT; + size_t oid_hexsize = git_oid_hexsize(backend->oid_type); + char *scan, *eof, *eol; + + if (!backend->gitpath) + return 0; + + error = git_sortedcache_lockandload(backend->refcache, &packedrefs); + + /* + * If we can't find the packed-refs, clear table and return. + * Any other error just gets passed through. + * If no error, and file wasn't changed, just return. + * Anything else means we need to refresh the packed refs. + */ + if (error <= 0) { + if (error == GIT_ENOTFOUND) { + GIT_UNUSED(git_sortedcache_clear(backend->refcache, true)); + git_error_clear(); + error = 0; + } + return error; + } + + /* At this point, refresh the packed refs from the loaded buffer. */ + + GIT_UNUSED(git_sortedcache_clear(backend->refcache, false)); + + scan = packedrefs.ptr; + eof = scan + packedrefs.size; + + scan = packed_set_peeling_mode(scan, packedrefs.size, backend); + if (!scan) + goto parse_failed; + + while (scan < eof && *scan == '#') { + if (!(eol = strchr(scan, '\n'))) + goto parse_failed; + scan = eol + 1; + } + + while (scan < eof) { + struct packref *ref; + git_oid oid; + + /* parse " \n" */ + + if (git_oid_from_prefix(&oid, scan, oid_hexsize, backend->oid_type) < 0) + goto parse_failed; + scan += oid_hexsize; + + if (*scan++ != ' ') + goto parse_failed; + if (!(eol = strchr(scan, '\n'))) + goto parse_failed; + *eol = '\0'; + if (eol[-1] == '\r') + eol[-1] = '\0'; + + if (git_sortedcache_upsert((void **)&ref, backend->refcache, scan) < 0) + goto parse_failed; + scan = eol + 1; + + git_oid_cpy(&ref->oid, &oid); + + /* look for optional "^\n" */ + + if (*scan == '^') { + if (git_oid_from_prefix(&oid, scan + 1, oid_hexsize, backend->oid_type) < 0) + goto parse_failed; + scan += oid_hexsize + 1; + + if (scan < eof) { + if (!(eol = strchr(scan, '\n'))) + goto parse_failed; + scan = eol + 1; + } + + git_oid_cpy(&ref->peel, &oid); + ref->flags |= PACKREF_HAS_PEEL; + } + else if (backend->peeling_mode == PEELING_FULL || + (backend->peeling_mode == PEELING_STANDARD && + git__prefixcmp(ref->name, GIT_REFS_TAGS_DIR) == 0)) + ref->flags |= PACKREF_CANNOT_PEEL; + } + + git_sortedcache_wunlock(backend->refcache); + git_str_dispose(&packedrefs); + + return 0; + +parse_failed: + git_error_set(GIT_ERROR_REFERENCE, "corrupted packed references file"); + + GIT_UNUSED(git_sortedcache_clear(backend->refcache, false)); + git_sortedcache_wunlock(backend->refcache); + git_str_dispose(&packedrefs); + + return -1; +} + +static int loose_parse_oid( + git_oid *oid, + const char *filename, + git_str *file_content, + git_oid_t oid_type) +{ + const char *str = git_str_cstr(file_content); + size_t oid_hexsize = git_oid_hexsize(oid_type); + + if (git_str_len(file_content) < oid_hexsize) + goto corrupted; + + /* we need to get 40 OID characters from the file */ + if (git_oid_from_prefix(oid, str, oid_hexsize, oid_type) < 0) + goto corrupted; + + /* If the file is longer than 40 chars, the 41st must be a space */ + str += oid_hexsize; + if (*str == '\0' || git__isspace(*str)) + return 0; + +corrupted: + git_error_set(GIT_ERROR_REFERENCE, "corrupted loose reference file: %s", filename); + return -1; +} + +static int loose_readbuffer(git_str *buf, const char *base, const char *path) +{ + int error; + + if ((error = loose_path(buf, base, path)) < 0 || + (error = git_futils_readbuffer(buf, buf->ptr)) < 0) + git_str_dispose(buf); + + return error; +} + +static int loose_lookup_to_packfile(refdb_fs_backend *backend, const char *name) +{ + int error = 0; + git_str ref_file = GIT_STR_INIT; + struct packref *ref = NULL; + git_oid oid; + + /* if we fail to load the loose reference, assume someone changed + * the filesystem under us and skip it... + */ + if (loose_readbuffer(&ref_file, backend->gitpath, name) < 0) { + git_error_clear(); + goto done; + } + + /* skip symbolic refs */ + if (!git__prefixcmp(git_str_cstr(&ref_file), GIT_SYMREF)) + goto done; + + /* parse OID from file */ + if ((error = loose_parse_oid(&oid, name, &ref_file, backend->oid_type)) < 0) + goto done; + + if ((error = git_sortedcache_wlock(backend->refcache)) < 0) + goto done; + + if (!(error = git_sortedcache_upsert( + (void **)&ref, backend->refcache, name))) { + + git_oid_cpy(&ref->oid, &oid); + ref->flags = PACKREF_WAS_LOOSE; + } + + git_sortedcache_wunlock(backend->refcache); + +done: + git_str_dispose(&ref_file); + return error; +} + +static int _dirent_loose_load(void *payload, git_str *full_path) +{ + refdb_fs_backend *backend = payload; + const char *file_path; + + if (git__suffixcmp(full_path->ptr, ".lock") == 0) + return 0; + + if (git_fs_path_isdir(full_path->ptr)) { + int error = git_fs_path_direach( + full_path, backend->direach_flags, _dirent_loose_load, backend); + /* Race with the filesystem, ignore it */ + if (error == GIT_ENOTFOUND) { + git_error_clear(); + return 0; + } + + return error; + } + + file_path = full_path->ptr + strlen(backend->gitpath); + + return loose_lookup_to_packfile(backend, file_path); +} + +/* + * Load all the loose references from the repository + * into the in-memory Packfile, and build a vector with + * all the references so it can be written back to + * disk. + */ +static int packed_loadloose(refdb_fs_backend *backend) +{ + int error; + git_str refs_path = GIT_STR_INIT; + + if (git_str_joinpath(&refs_path, backend->gitpath, GIT_REFS_DIR) < 0) + return -1; + + /* + * Load all the loose files from disk into the Packfile table. + * This will overwrite any old packed entries with their + * updated loose versions + */ + error = git_fs_path_direach( + &refs_path, backend->direach_flags, _dirent_loose_load, backend); + + git_str_dispose(&refs_path); + + return error; +} + +static int refdb_fs_backend__init(struct git_refdb_backend *_backend, + const char *head_target, + mode_t mode, + uint32_t flags) +{ + refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + git_str path = GIT_STR_INIT, content = GIT_STR_INIT; + int error; + + /* + * As most references are per repository and not per worktree we don't + * want to set up the typical "refs/" hierarchy in worktrees. + */ + if ((flags & GIT_REFDB_BACKEND_INIT_IS_WORKTREE) == 0) { + mode_t dmode; + + if (mode == GIT_REPOSITORY_INIT_SHARED_UMASK) + dmode = 0777; + else if (mode == GIT_REPOSITORY_INIT_SHARED_GROUP) + dmode = (0775 | S_ISGID); + else if (mode == GIT_REPOSITORY_INIT_SHARED_ALL) + dmode = (0777 | S_ISGID); + else + dmode = mode; + + if ((error = git_str_joinpath(&path, backend->gitpath, GIT_REFS_HEADS_DIR)) < 0 || + (error = git_futils_mkdir(path.ptr, dmode, 0) < 0) || + (error = git_str_joinpath(&path, backend->gitpath, GIT_REFS_TAGS_DIR)) < 0 || + (error = git_futils_mkdir(path.ptr, dmode, 0) < 0)) + goto out; + } + + if (head_target) { + if ((error = git_str_joinpath(&path, backend->gitpath, GIT_HEAD_FILE)) < 0) + goto out; + + if ((flags & GIT_REFDB_BACKEND_INIT_FORCE_HEAD) == 0 && + git_fs_path_exists(path.ptr)) + goto out; + + if ((error = git_str_printf(&content, GIT_SYMREF "%s\n", head_target)) < 0 || + (error = git_futils_writebuffer(&content, path.ptr, 0, mode)) < 0) + goto out; + } + + error = 0; + +out: + git_str_dispose(&content); + git_str_dispose(&path); + + return error; +} + +static int refdb_fs_backend__exists( + int *exists, + git_refdb_backend *_backend, + const char *ref_name) +{ + refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + git_str ref_path = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(backend); + + *exists = 0; + + if ((error = loose_path(&ref_path, backend->gitpath, ref_name)) < 0) + goto out; + + if (git_fs_path_isfile(ref_path.ptr)) { + *exists = 1; + goto out; + } + + if ((error = packed_reload(backend)) < 0) + goto out; + + if (git_sortedcache_lookup(backend->refcache, ref_name) != NULL) { + *exists = 1; + goto out; + } + +out: + git_str_dispose(&ref_path); + return error; +} + +static const char *loose_parse_symbolic(git_str *file_content) +{ + const unsigned int header_len = (unsigned int)strlen(GIT_SYMREF); + const char *refname_start; + + refname_start = (const char *)file_content->ptr; + + if (git_str_len(file_content) < header_len + 1) { + git_error_set(GIT_ERROR_REFERENCE, "corrupted loose reference file"); + return NULL; + } + + /* + * Assume we have already checked for the header + * before calling this function + */ + refname_start += header_len; + + return refname_start; +} + +/* + * Returns whether a reference is stored per worktree or not. + * Per-worktree references are: + * + * - all pseudorefs, e.g. HEAD and MERGE_HEAD + * - all references stored inside of "refs/bisect/" + */ +static bool is_per_worktree_ref(const char *ref_name) +{ + return git__prefixcmp(ref_name, "refs/") != 0 || + git__prefixcmp(ref_name, "refs/bisect/") == 0 || + git__prefixcmp(ref_name, "refs/worktree/") == 0 || + git__prefixcmp(ref_name, "refs/rewritten/") == 0; +} + +static int loose_lookup( + git_reference **out, + refdb_fs_backend *backend, + const char *ref_name) +{ + git_str ref_file = GIT_STR_INIT; + int error = 0; + const char *ref_dir; + + if (out) + *out = NULL; + + if (is_per_worktree_ref(ref_name)) + ref_dir = backend->gitpath; + else + ref_dir = backend->commonpath; + + if ((error = loose_readbuffer(&ref_file, ref_dir, ref_name)) < 0) + /* cannot read loose ref file - gah */; + else if (git__prefixcmp(git_str_cstr(&ref_file), GIT_SYMREF) == 0) { + const char *target; + + git_str_rtrim(&ref_file); + + if (!(target = loose_parse_symbolic(&ref_file))) + error = -1; + else if (out != NULL) + *out = git_reference__alloc_symbolic(ref_name, target); + } else { + git_oid oid; + + if (!(error = loose_parse_oid(&oid, ref_name, &ref_file, backend->oid_type)) && + out != NULL) + *out = git_reference__alloc(ref_name, &oid, NULL); + } + + git_str_dispose(&ref_file); + return error; +} + +static int ref_error_notfound(const char *name) +{ + git_error_set(GIT_ERROR_REFERENCE, "reference '%s' not found", name); + return GIT_ENOTFOUND; +} + +static char *packed_set_peeling_mode( + char *data, + size_t data_sz, + refdb_fs_backend *backend) +{ + static const char *traits_header = "# pack-refs with:"; + char *eol; + backend->peeling_mode = PEELING_NONE; + + if (git__prefixncmp(data, data_sz, traits_header) == 0) { + size_t hdr_sz = strlen(traits_header); + const char *sorted = " sorted "; + const char *peeled = " peeled "; + const char *fully_peeled = " fully-peeled "; + data += hdr_sz; + data_sz -= hdr_sz; + + eol = memchr(data, '\n', data_sz); + + if (!eol) + return NULL; + + if (git__memmem(data, eol - data, fully_peeled, strlen(fully_peeled))) + backend->peeling_mode = PEELING_FULL; + else if (git__memmem(data, eol - data, peeled, strlen(peeled))) + backend->peeling_mode = PEELING_STANDARD; + + backend->sorted = NULL != git__memmem(data, eol - data, sorted, strlen(sorted)); + + return eol + 1; + } + return data; +} + +static void packed_map_free(refdb_fs_backend *backend) +{ + if (backend->packed_refs_map.data) { +#ifdef GIT_WIN32 + git__free(backend->packed_refs_map.data); +#else + git_futils_mmap_free(&backend->packed_refs_map); +#endif + backend->packed_refs_map.data = NULL; + backend->packed_refs_map.len = 0; + git_futils_filestamp_set(&backend->packed_refs_stamp, NULL); + } +} + +static int packed_map_check(refdb_fs_backend *backend) +{ + int error = 0; + git_file fd = -1; + struct stat st; + + if ((error = git_mutex_lock(&backend->prlock)) < 0) + return error; + + if (backend->packed_refs_map.data && + !git_futils_filestamp_check( + &backend->packed_refs_stamp, backend->refcache->path)) { + git_mutex_unlock(&backend->prlock); + return error; + } + packed_map_free(backend); + + fd = git_futils_open_ro(backend->refcache->path); + if (fd < 0) { + git_mutex_unlock(&backend->prlock); + if (fd == GIT_ENOTFOUND) { + git_error_clear(); + return 0; + } + return fd; + } + + if (p_fstat(fd, &st) < 0) { + p_close(fd); + git_mutex_unlock(&backend->prlock); + git_error_set(GIT_ERROR_OS, "unable to stat packed-refs '%s'", backend->refcache->path); + return -1; + } + + if (st.st_size == 0) { + p_close(fd); + git_mutex_unlock(&backend->prlock); + return 0; + } + + git_futils_filestamp_set_from_stat(&backend->packed_refs_stamp, &st); + +#ifdef GIT_WIN32 + /* on windows, we copy the entire file into memory rather than using + * mmap() because using mmap() on windows also locks the file and this + * map is long-lived. */ + backend->packed_refs_map.len = (size_t)st.st_size; + backend->packed_refs_map.data = + git__malloc(backend->packed_refs_map.len); + GIT_ERROR_CHECK_ALLOC(backend->packed_refs_map.data); + { + ssize_t bytesread = + p_read(fd, backend->packed_refs_map.data, + backend->packed_refs_map.len); + error = (bytesread == (ssize_t)backend->packed_refs_map.len) ? 0 : -1; + } +#else + error = git_futils_mmap_ro(&backend->packed_refs_map, fd, 0, (size_t)st.st_size); +#endif + p_close(fd); + if (error < 0) { + git_mutex_unlock(&backend->prlock); + return error; + } + + packed_set_peeling_mode( + backend->packed_refs_map.data, backend->packed_refs_map.len, + backend); + + git_mutex_unlock(&backend->prlock); + return error; +} + +/* + * Find beginning of packed-ref record pointed to by p. + * buf - a lower-bound pointer to some memory buffer + * p - an upper-bound pointer to the same memory buffer + */ +static const char *start_of_record(const char *buf, const char *p) +{ + const char *nl = p; + while (true) { + nl = git__memrchr(buf, '\n', nl - buf); + if (!nl) + return buf; + + if (nl[1] == '^' && nl > buf) + --nl; + else + break; + }; + return nl + 1; +} + +/* + * Find end of packed-ref record pointed to by p. + * end - an upper-bound pointer to some memory buffer + * p - a lower-bound pointer to the same memory buffer + */ +static const char *end_of_record(const char *p, const char *end) +{ + while (1) { + size_t sz = end - p; + p = memchr(p, '\n', sz); + if (!p) + return end; + ++p; + if (p < end && p[0] == '^') + ++p; + else + break; + } + return p; +} + +static int cmp_record_to_refname( + const char *rec, + size_t data_end, + const char *ref_name, + git_oid_t oid_type) +{ + const size_t ref_len = strlen(ref_name); + int cmp_val; + const char *end; + size_t oid_hexsize = git_oid_hexsize(oid_type); + + rec += oid_hexsize + 1; /* + space */ + + /* an incomplete (corrupt) record is treated as less than ref_name */ + if (data_end < oid_hexsize + 3) + return -1; + + data_end -= oid_hexsize + 1; + + end = memchr(rec, '\n', data_end); + if (end) + data_end = end - rec; + + cmp_val = memcmp(rec, ref_name, min(ref_len, data_end)); + + if (cmp_val == 0 && data_end != ref_len) + return (data_end > ref_len) ? 1 : -1; + return cmp_val; +} + +static int packed_unsorted_lookup( + git_reference **out, + refdb_fs_backend *backend, + const char *ref_name) +{ + int error = 0; + struct packref *entry; + + if ((error = packed_reload(backend)) < 0) + return error; + + if (git_sortedcache_rlock(backend->refcache) < 0) + return -1; + + entry = git_sortedcache_lookup(backend->refcache, ref_name); + if (!entry) { + error = ref_error_notfound(ref_name); + } else { + *out = git_reference__alloc(ref_name, &entry->oid, &entry->peel); + if (!*out) + error = -1; + } + + git_sortedcache_runlock(backend->refcache); + + return error; +} + +static int packed_lookup( + git_reference **out, + refdb_fs_backend *backend, + const char *ref_name) +{ + int error = 0; + const char *left, *right, *data_end; + size_t oid_hexsize = git_oid_hexsize(backend->oid_type); + + if ((error = packed_map_check(backend)) < 0) + return error; + + if (!backend->sorted) + return packed_unsorted_lookup(out, backend, ref_name); + + left = backend->packed_refs_map.data; + right = data_end = (const char *) backend->packed_refs_map.data + + backend->packed_refs_map.len; + + while (left < right && *left == '#') { + if (!(left = memchr(left, '\n', data_end - left))) + goto parse_failed; + left++; + } + + while (left < right) { + const char *mid, *rec; + int compare; + + mid = left + (right - left) / 2; + rec = start_of_record(left, mid); + compare = cmp_record_to_refname(rec, data_end - rec, ref_name, backend->oid_type); + + if (compare < 0) { + left = end_of_record(mid, right); + } else if (compare > 0) { + right = rec; + } else { + const char *eol; + git_oid oid, peel, *peel_ptr = NULL; + + if (data_end - rec < (long)oid_hexsize || + git_oid_from_prefix(&oid, rec, oid_hexsize, backend->oid_type) < 0) { + goto parse_failed; + } + rec += oid_hexsize + 1; + if (!(eol = memchr(rec, '\n', data_end - rec))) { + goto parse_failed; + } + + /* look for optional "^\n" */ + + if (eol + 1 < data_end) { + rec = eol + 1; + + if (*rec == '^') { + rec++; + if (data_end - rec < (long)oid_hexsize || + git_oid_from_prefix(&peel, rec, oid_hexsize, backend->oid_type) < 0) { + goto parse_failed; + } + peel_ptr = &peel; + } + } + + *out = git_reference__alloc(ref_name, &oid, peel_ptr); + if (!*out) { + return -1; + } + + return 0; + } + } + return ref_error_notfound(ref_name); + +parse_failed: + git_error_set(GIT_ERROR_REFERENCE, "corrupted packed references file"); + return -1; +} + +static int refdb_fs_backend__lookup( + git_reference **out, + git_refdb_backend *_backend, + const char *ref_name) +{ + refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + int error; + + GIT_ASSERT_ARG(backend); + + if (!(error = loose_lookup(out, backend, ref_name))) + return 0; + + /* only try to lookup this reference on the packfile if it + * wasn't found on the loose refs; not if there was a critical error */ + if (error == GIT_ENOTFOUND) { + git_error_clear(); + error = packed_lookup(out, backend, ref_name); + } + return error; +} + +typedef struct { + git_reference_iterator parent; + + char *glob; + + git_pool pool; + git_vector loose; + + git_sortedcache *cache; + size_t loose_pos; + size_t packed_pos; +} refdb_fs_iter; + +static void refdb_fs_backend__iterator_free(git_reference_iterator *_iter) +{ + refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent); + + git_vector_dispose(&iter->loose); + git_pool_clear(&iter->pool); + git_sortedcache_free(iter->cache); + git__free(iter); +} + +struct iter_load_context { + refdb_fs_backend *backend; + refdb_fs_iter *iter; + + /* + * If we have a glob with a prefix (eg `refs/heads/ *`) then we can + * optimize our prefix to avoid walking refs that we know won't + * match. This is that prefix. + */ + const char *ref_prefix; + size_t ref_prefix_len; + + /* Temporary variables to avoid unnecessary allocations */ + git_str ref_name; + git_str path; +}; + +static void iter_load_optimize_prefix(struct iter_load_context *ctx) +{ + const char *pos, *last_sep = NULL; + + if (!ctx->iter->glob) + return; + + for (pos = ctx->iter->glob; *pos; pos++) { + switch (*pos) { + case '?': + case '*': + case '[': + case '\\': + break; + case '/': + last_sep = pos; + /* FALLTHROUGH */ + default: + continue; + } + break; + } + + if (last_sep) { + ctx->ref_prefix = ctx->iter->glob; + ctx->ref_prefix_len = (last_sep - ctx->ref_prefix) + 1; + } +} + +static int iter_load_paths( + struct iter_load_context *ctx, + const char *root_path, + bool worktree) +{ + git_iterator *fsit = NULL; + git_iterator_options fsit_opts = GIT_ITERATOR_OPTIONS_INIT; + const git_index_entry *entry; + int error = 0; + + fsit_opts.flags = ctx->backend->iterator_flags; + + git_str_clear(&ctx->path); + git_str_puts(&ctx->path, root_path); + git_str_put(&ctx->path, ctx->ref_prefix, ctx->ref_prefix_len); + + fsit_opts.flags = ctx->backend->iterator_flags; + fsit_opts.oid_type = ctx->backend->oid_type; + + if ((error = git_iterator_for_filesystem(&fsit, ctx->path.ptr, &fsit_opts)) < 0) { + /* + * Subdirectories - either glob provided or per-worktree refs - need + * not exist. + */ + if ((worktree || ctx->iter->glob) && error == GIT_ENOTFOUND) + error = 0; + + goto done; + } + + git_str_clear(&ctx->ref_name); + git_str_put(&ctx->ref_name, ctx->ref_prefix, ctx->ref_prefix_len); + + while (git_iterator_advance(&entry, fsit) == 0) { + char *ref_dup; + + git_str_truncate(&ctx->ref_name, ctx->ref_prefix_len); + git_str_puts(&ctx->ref_name, entry->path); + + if (worktree) { + if (!is_per_worktree_ref(ctx->ref_name.ptr)) + continue; + } else { + if (git_repository_is_worktree(ctx->backend->repo) && + is_per_worktree_ref(ctx->ref_name.ptr)) + continue; + } + + if (git__suffixcmp(ctx->ref_name.ptr, ".lock") == 0) + continue; + + if (ctx->iter->glob && wildmatch(ctx->iter->glob, ctx->ref_name.ptr, 0)) + continue; + + ref_dup = git_pool_strdup(&ctx->iter->pool, ctx->ref_name.ptr); + GIT_ERROR_CHECK_ALLOC(ref_dup); + + if ((error = git_vector_insert(&ctx->iter->loose, ref_dup)) < 0) + goto done; + } + +done: + git_iterator_free(fsit); + return error; +} + +#define iter_load_context_init(b, i) { b, i, GIT_REFS_DIR, CONST_STRLEN(GIT_REFS_DIR) } +#define iter_load_context_dispose(ctx) do { \ + git_str_dispose(&((ctx)->path)); \ + git_str_dispose(&((ctx)->ref_name)); \ +} while(0) + +static int iter_load_loose_paths( + refdb_fs_backend *backend, + refdb_fs_iter *iter) +{ + struct iter_load_context ctx = iter_load_context_init(backend, iter); + + int error = 0; + + if (!backend->commonpath) + return 0; + + iter_load_optimize_prefix(&ctx); + + if ((error = iter_load_paths(&ctx, + backend->commonpath, false)) < 0) + goto done; + + if (git_repository_is_worktree(backend->repo)) { + if ((error = iter_load_paths(&ctx, + backend->gitpath, true)) < 0) + goto done; + } + +done: + iter_load_context_dispose(&ctx); + return error; +} + +static int refdb_fs_backend__iterator_next( + git_reference **out, git_reference_iterator *_iter) +{ + int error = GIT_ITEROVER; + refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent); + refdb_fs_backend *backend = GIT_CONTAINER_OF(iter->parent.db->backend, refdb_fs_backend, parent); + struct packref *ref; + + while (iter->loose_pos < iter->loose.length) { + const char *path = git_vector_get(&iter->loose, iter->loose_pos++); + + if (loose_lookup(out, backend, path) == 0) { + ref = git_sortedcache_lookup(iter->cache, path); + if (ref) + ref->flags |= PACKREF_SHADOWED; + + return 0; + } + + git_error_clear(); + } + + error = GIT_ITEROVER; + while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) { + ref = git_sortedcache_entry(iter->cache, iter->packed_pos++); + if (!ref) /* stop now if another thread deleted refs and we past end */ + break; + + if (ref->flags & PACKREF_SHADOWED) + continue; + if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0) + continue; + + *out = git_reference__alloc(ref->name, &ref->oid, &ref->peel); + error = (*out != NULL) ? 0 : -1; + break; + } + + return error; +} + +static int refdb_fs_backend__iterator_next_name( + const char **out, git_reference_iterator *_iter) +{ + int error = GIT_ITEROVER; + refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent); + refdb_fs_backend *backend = GIT_CONTAINER_OF(iter->parent.db->backend, refdb_fs_backend, parent); + struct packref *ref; + + while (iter->loose_pos < iter->loose.length) { + const char *path = git_vector_get(&iter->loose, iter->loose_pos++); + struct packref *ref; + + if (loose_lookup(NULL, backend, path) == 0) { + ref = git_sortedcache_lookup(iter->cache, path); + if (ref) + ref->flags |= PACKREF_SHADOWED; + + *out = path; + return 0; + } + + git_error_clear(); + } + + error = GIT_ITEROVER; + while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) { + ref = git_sortedcache_entry(iter->cache, iter->packed_pos++); + if (!ref) /* stop now if another thread deleted refs and we past end */ + break; + + if (ref->flags & PACKREF_SHADOWED) + continue; + if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0) + continue; + + *out = ref->name; + error = 0; + break; + } + + return error; +} + +static int refdb_fs_backend__iterator( + git_reference_iterator **out, git_refdb_backend *_backend, const char *glob) +{ + refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + refdb_fs_iter *iter = NULL; + int error; + + GIT_ASSERT_ARG(backend); + + iter = git__calloc(1, sizeof(refdb_fs_iter)); + GIT_ERROR_CHECK_ALLOC(iter); + + if ((error = git_pool_init(&iter->pool, 1)) < 0) + goto out; + + if ((error = git_vector_init(&iter->loose, 8, NULL)) < 0) + goto out; + + if (glob != NULL && + (iter->glob = git_pool_strdup(&iter->pool, glob)) == NULL) { + error = GIT_ERROR_NOMEMORY; + goto out; + } + + if ((error = iter_load_loose_paths(backend, iter)) < 0) + goto out; + + if ((error = packed_reload(backend)) < 0) + goto out; + + if ((error = git_sortedcache_copy(&iter->cache, backend->refcache, 1, NULL, NULL)) < 0) + goto out; + + iter->parent.next = refdb_fs_backend__iterator_next; + iter->parent.next_name = refdb_fs_backend__iterator_next_name; + iter->parent.free = refdb_fs_backend__iterator_free; + + *out = (git_reference_iterator *)iter; +out: + if (error) + refdb_fs_backend__iterator_free((git_reference_iterator *)iter); + return error; +} + +static bool ref_is_available( + const char *old_ref, const char *new_ref, const char *this_ref) +{ + if (old_ref == NULL || strcmp(old_ref, this_ref)) { + size_t reflen = strlen(this_ref); + size_t newlen = strlen(new_ref); + size_t cmplen = reflen < newlen ? reflen : newlen; + const char *lead = reflen < newlen ? new_ref : this_ref; + + if (!strncmp(new_ref, this_ref, cmplen) && lead[cmplen] == '/') { + return false; + } + } + + return true; +} + +static int reference_path_available( + refdb_fs_backend *backend, + const char *new_ref, + const char *old_ref, + int force) +{ + size_t i; + int error; + + if ((error = packed_reload(backend)) < 0) + return error; + + if (!force) { + int exists; + + if ((error = refdb_fs_backend__exists( + &exists, (git_refdb_backend *)backend, new_ref)) < 0) { + return error; + } + + if (exists) { + git_error_set(GIT_ERROR_REFERENCE, + "failed to write reference '%s': a reference with " + "that name already exists.", new_ref); + return GIT_EEXISTS; + } + } + + if ((error = git_sortedcache_rlock(backend->refcache)) < 0) + return error; + + for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) { + struct packref *ref = git_sortedcache_entry(backend->refcache, i); + + if (ref && !ref_is_available(old_ref, new_ref, ref->name)) { + git_sortedcache_runlock(backend->refcache); + git_error_set(GIT_ERROR_REFERENCE, + "path to reference '%s' collides with existing one", new_ref); + return -1; + } + } + + git_sortedcache_runlock(backend->refcache); + return 0; +} + +static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char *name) +{ + int error, filebuf_flags; + git_str ref_path = GIT_STR_INIT; + const char *basedir; + + GIT_ASSERT_ARG(file); + GIT_ASSERT_ARG(backend); + GIT_ASSERT_ARG(name); + + if (!git_path_is_valid(backend->repo, name, 0, GIT_FS_PATH_REJECT_FILESYSTEM_DEFAULTS)) { + git_error_set(GIT_ERROR_INVALID, "invalid reference name '%s'", name); + return GIT_EINVALIDSPEC; + } + + if (is_per_worktree_ref(name)) + basedir = backend->gitpath; + else + basedir = backend->commonpath; + + /* Remove a possibly existing empty directory hierarchy + * which name would collide with the reference name + */ + if ((error = git_futils_rmdir_r(name, basedir, GIT_RMDIR_SKIP_NONEMPTY)) < 0) + return error; + + if ((error = loose_path(&ref_path, basedir, name)) < 0) + return error; + + filebuf_flags = GIT_FILEBUF_CREATE_LEADING_DIRS; + if (backend->fsync) + filebuf_flags |= GIT_FILEBUF_FSYNC; + + error = git_filebuf_open(file, ref_path.ptr, filebuf_flags, GIT_REFS_FILE_MODE); + + if (error == GIT_EDIRECTORY) + git_error_set(GIT_ERROR_REFERENCE, "cannot lock ref '%s', there are refs beneath that folder", name); + + git_str_dispose(&ref_path); + return error; +} + +static int loose_commit(git_filebuf *file, const git_reference *ref) +{ + GIT_ASSERT_ARG(file); + GIT_ASSERT_ARG(ref); + + if (ref->type == GIT_REFERENCE_DIRECT) { + char oid[GIT_OID_MAX_HEXSIZE + 1]; + git_oid_nfmt(oid, sizeof(oid), &ref->target.oid); + + git_filebuf_printf(file, "%s\n", oid); + } else if (ref->type == GIT_REFERENCE_SYMBOLIC) { + git_filebuf_printf(file, GIT_SYMREF "%s\n", ref->target.symbolic); + } else { + GIT_ASSERT(0); + } + + return git_filebuf_commit(file); +} + +static int refdb_fs_backend__lock(void **out, git_refdb_backend *_backend, const char *refname) +{ + int error; + git_filebuf *lock; + refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + + lock = git__calloc(1, sizeof(git_filebuf)); + GIT_ERROR_CHECK_ALLOC(lock); + + if ((error = loose_lock(lock, backend, refname)) < 0) { + git__free(lock); + return error; + } + + *out = lock; + return 0; +} + +static int refdb_fs_backend__write_tail( + git_refdb_backend *_backend, + const git_reference *ref, + git_filebuf *file, + int update_reflog, + const git_oid *old_id, + const char *old_target, + const git_signature *who, + const char *message); + +static int refdb_fs_backend__delete_tail( + git_refdb_backend *_backend, + git_filebuf *file, + const char *ref_name, + const git_oid *old_id, + const char *old_target); + +static int refdb_fs_backend__unlock(git_refdb_backend *backend, void *payload, int success, int update_reflog, + const git_reference *ref, const git_signature *sig, const char *message) +{ + git_filebuf *lock = (git_filebuf *) payload; + int error = 0; + + if (success == 2) + error = refdb_fs_backend__delete_tail(backend, lock, ref->name, NULL, NULL); + else if (success) + error = refdb_fs_backend__write_tail(backend, ref, lock, update_reflog, NULL, NULL, sig, message); + else + git_filebuf_cleanup(lock); + + git__free(lock); + return error; +} + +/* + * Find out what object this reference resolves to. + * + * For references that point to a 'big' tag (e.g. an + * actual tag object on the repository), we need to + * cache on the packfile the OID of the object to + * which that 'big tag' is pointing to. + */ +static int packed_find_peel(refdb_fs_backend *backend, struct packref *ref) +{ + git_object *object; + + if (ref->flags & PACKREF_HAS_PEEL || ref->flags & PACKREF_CANNOT_PEEL) + return 0; + + /* + * Find the tagged object in the repository + */ + if (git_object_lookup(&object, backend->repo, &ref->oid, GIT_OBJECT_ANY) < 0) + return -1; + + /* + * If the tagged object is a Tag object, we need to resolve it; + * if the ref is actually a 'weak' ref, we don't need to resolve + * anything. + */ + if (git_object_type(object) == GIT_OBJECT_TAG) { + git_tag *tag = (git_tag *)object; + + /* + * Find the object pointed at by this tag + */ + git_oid_cpy(&ref->peel, git_tag_target_id(tag)); + ref->flags |= PACKREF_HAS_PEEL; + + /* + * The reference has now cached the resolved OID, and is + * marked at such. When written to the packfile, it'll be + * accompanied by this resolved oid + */ + } + + git_object_free(object); + return 0; +} + +/* + * Write a single reference into a packfile + */ +static int packed_write_ref(struct packref *ref, git_filebuf *file) +{ + char oid[GIT_OID_MAX_HEXSIZE + 1]; + git_oid_nfmt(oid, sizeof(oid), &ref->oid); + + /* + * For references that peel to an object in the repo, we must + * write the resulting peel on a separate line, e.g. + * + * 6fa8a902cc1d18527e1355773c86721945475d37 refs/tags/libgit2-0.4 + * ^2ec0cb7959b0bf965d54f95453f5b4b34e8d3100 + * + * This obviously only applies to tags. + * The required peels have already been loaded into `ref->peel_target`. + */ + if (ref->flags & PACKREF_HAS_PEEL) { + char peel[GIT_OID_MAX_HEXSIZE + 1]; + git_oid_nfmt(peel, sizeof(peel), &ref->peel); + + if (git_filebuf_printf(file, "%s %s\n^%s\n", oid, ref->name, peel) < 0) + return -1; + } else { + if (git_filebuf_printf(file, "%s %s\n", oid, ref->name) < 0) + return -1; + } + + return 0; +} + +/* + * Remove all loose references + * + * Once we have successfully written a packfile, + * all the loose references that were packed must be + * removed from disk. + * + * This is a dangerous method; make sure the packfile + * is well-written, because we are destructing references + * here otherwise. + */ +static int packed_remove_loose(refdb_fs_backend *backend) +{ + size_t i; + git_filebuf lock = GIT_FILEBUF_INIT; + git_str ref_content = GIT_STR_INIT; + int error = 0; + + /* backend->refcache is already locked when this is called */ + + for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) { + struct packref *ref = git_sortedcache_entry(backend->refcache, i); + git_oid current_id; + + if (!ref || !(ref->flags & PACKREF_WAS_LOOSE)) + continue; + + git_filebuf_cleanup(&lock); + + /* We need to stop anybody from updating the ref while we try to do a safe delete */ + error = loose_lock(&lock, backend, ref->name); + /* If someone else is updating it, let them do it */ + if (error == GIT_EEXISTS || error == GIT_ENOTFOUND) + continue; + + if (error < 0) { + git_str_dispose(&ref_content); + git_error_set(GIT_ERROR_REFERENCE, "failed to lock loose reference '%s'", ref->name); + return error; + } + + error = git_futils_readbuffer(&ref_content, lock.path_original); + /* Someone else beat us to cleaning up the ref, let's simply continue */ + if (error == GIT_ENOTFOUND) + continue; + + /* This became a symref between us packing and trying to delete it, so ignore it */ + if (!git__prefixcmp(ref_content.ptr, GIT_SYMREF)) + continue; + + /* Figure out the current id; if we find a bad ref file, skip it so we can do the rest */ + if (loose_parse_oid(¤t_id, lock.path_original, &ref_content, backend->oid_type) < 0) + continue; + + /* If the ref moved since we packed it, we must not delete it */ + if (!git_oid_equal(¤t_id, &ref->oid)) + continue; + + /* + * if we fail to remove a single file, this is *not* good, + * but we should keep going and remove as many as possible. + * If we fail to remove, the ref is still in the old state, so + * we haven't lost information. + */ + p_unlink(lock.path_original); + } + + git_str_dispose(&ref_content); + git_filebuf_cleanup(&lock); + return 0; +} + +/* + * Write all the contents in the in-memory packfile to disk. + */ +static int packed_write(refdb_fs_backend *backend) +{ + git_sortedcache *refcache = backend->refcache; + git_filebuf pack_file = GIT_FILEBUF_INIT; + int error, open_flags = 0; + size_t i; + + /* take lock and close up packed-refs mmap if open */ + if ((error = git_mutex_lock(&backend->prlock)) < 0) { + return error; + } + + packed_map_free(backend); + + git_mutex_unlock(&backend->prlock); + + /* lock the cache to updates while we do this */ + if ((error = git_sortedcache_wlock(refcache)) < 0) + return error; + + if (backend->fsync) + open_flags = GIT_FILEBUF_FSYNC; + + /* Open the file! */ + if ((error = git_filebuf_open(&pack_file, git_sortedcache_path(refcache), open_flags, GIT_PACKEDREFS_FILE_MODE)) < 0) + goto fail; + + /* Packfiles have a header... apparently + * This is in fact not required, but we might as well print it + * just for kicks */ + if ((error = git_filebuf_printf(&pack_file, "%s\n", GIT_PACKEDREFS_HEADER)) < 0) + goto fail; + + for (i = 0; i < git_sortedcache_entrycount(refcache); ++i) { + struct packref *ref = git_sortedcache_entry(refcache, i); + + GIT_ASSERT_WITH_CLEANUP(ref, { + error = -1; + goto fail; + }); + + if ((error = packed_find_peel(backend, ref)) < 0) + goto fail; + + if ((error = packed_write_ref(ref, &pack_file)) < 0) + goto fail; + } + + /* if we've written all the references properly, we can commit + * the packfile to make the changes effective */ + if ((error = git_filebuf_commit(&pack_file)) < 0) + goto fail; + + /* when and only when the packfile has been properly written, + * we can go ahead and remove the loose refs */ + if ((error = packed_remove_loose(backend)) < 0) + goto fail; + + git_sortedcache_updated(refcache); + git_sortedcache_wunlock(refcache); + + /* we're good now */ + return 0; + +fail: + git_filebuf_cleanup(&pack_file); + git_sortedcache_wunlock(refcache); + + return error; +} + +static int packed_delete(refdb_fs_backend *backend, const char *ref_name) +{ + size_t pack_pos; + int error, found = 0; + + if ((error = packed_reload(backend)) < 0) + goto cleanup; + + if ((error = git_sortedcache_wlock(backend->refcache)) < 0) + goto cleanup; + + /* If a packed reference exists, remove it from the packfile and repack if necessary */ + error = git_sortedcache_lookup_index(&pack_pos, backend->refcache, ref_name); + if (error == 0) { + error = git_sortedcache_remove(backend->refcache, pack_pos); + found = 1; + } + if (error == GIT_ENOTFOUND) + error = 0; + + git_sortedcache_wunlock(backend->refcache); + + if (found) + error = packed_write(backend); + +cleanup: + return error; +} + +static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, const git_oid *old, const git_oid *new, const git_signature *author, const char *message); + +static int cmp_old_ref(int *cmp, git_refdb_backend *backend, const char *name, + const git_oid *old_id, const char *old_target) +{ + int error = 0; + git_reference *old_ref = NULL; + + *cmp = 0; + /* It "matches" if there is no old value to compare against */ + if (!old_id && !old_target) + return 0; + + if ((error = refdb_fs_backend__lookup(&old_ref, backend, name)) < 0) { + if (error == GIT_ENOTFOUND && old_id && git_oid_is_zero(old_id)) + return 0; + goto out; + } + + /* If the types don't match, there's no way the values do */ + if (old_id && old_ref->type != GIT_REFERENCE_DIRECT) { + *cmp = -1; + goto out; + } + if (old_target && old_ref->type != GIT_REFERENCE_SYMBOLIC) { + *cmp = 1; + goto out; + } + + if (old_id && old_ref->type == GIT_REFERENCE_DIRECT) + *cmp = git_oid_cmp(old_id, &old_ref->target.oid); + + if (old_target && old_ref->type == GIT_REFERENCE_SYMBOLIC) + *cmp = git__strcmp(old_target, old_ref->target.symbolic); + +out: + git_reference_free(old_ref); + + return error; +} + +/* + * The git.git comment regarding this, for your viewing pleasure: + * + * Special hack: If a branch is updated directly and HEAD + * points to it (may happen on the remote side of a push + * for example) then logically the HEAD reflog should be + * updated too. + * A generic solution implies reverse symref information, + * but finding all symrefs pointing to the given branch + * would be rather costly for this rare event (the direct + * update of a branch) to be worth it. So let's cheat and + * check with HEAD only which should cover 99% of all usage + * scenarios (even 100% of the default ones). + */ +static int maybe_append_head(refdb_fs_backend *backend, const git_reference *ref, const git_signature *who, const char *message) +{ + git_reference *head = NULL; + git_refdb *refdb = NULL; + int error, write_reflog; + git_oid old_id; + + if ((error = git_repository_refdb(&refdb, backend->repo)) < 0 || + (error = git_refdb_should_write_head_reflog(&write_reflog, refdb, ref)) < 0) + goto out; + if (!write_reflog) + goto out; + + /* if we can't resolve, we use {0}*40 as old id */ + if (git_reference_name_to_id(&old_id, backend->repo, ref->name) < 0) + memset(&old_id, 0, sizeof(old_id)); + + if ((error = git_reference_lookup(&head, backend->repo, GIT_HEAD_FILE)) < 0 || + (error = reflog_append(backend, head, &old_id, git_reference_target(ref), who, message)) < 0) + goto out; + +out: + git_reference_free(head); + git_refdb_free(refdb); + return error; +} + +static int refdb_fs_backend__write( + git_refdb_backend *_backend, + const git_reference *ref, + int force, + const git_signature *who, + const char *message, + const git_oid *old_id, + const char *old_target) +{ + refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + git_filebuf file = GIT_FILEBUF_INIT; + int error = 0; + + GIT_ASSERT_ARG(backend); + + if ((error = reference_path_available(backend, ref->name, NULL, force)) < 0) + return error; + + /* We need to perform the reflog append and old value check under the ref's lock */ + if ((error = loose_lock(&file, backend, ref->name)) < 0) + return error; + + return refdb_fs_backend__write_tail(_backend, ref, &file, true, old_id, old_target, who, message); +} + +static int refdb_fs_backend__write_tail( + git_refdb_backend *_backend, + const git_reference *ref, + git_filebuf *file, + int update_reflog, + const git_oid *old_id, + const char *old_target, + const git_signature *who, + const char *message) +{ + refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + int error = 0, cmp = 0, should_write; + const char *new_target = NULL; + const git_oid *new_id = NULL; + + if ((error = cmp_old_ref(&cmp, _backend, ref->name, old_id, old_target)) < 0) + goto on_error; + + if (cmp) { + git_error_set(GIT_ERROR_REFERENCE, "old reference value does not match"); + error = GIT_EMODIFIED; + goto on_error; + } + + if (ref->type == GIT_REFERENCE_SYMBOLIC) + new_target = ref->target.symbolic; + else + new_id = &ref->target.oid; + + error = cmp_old_ref(&cmp, _backend, ref->name, new_id, new_target); + if (error < 0 && error != GIT_ENOTFOUND) + goto on_error; + + /* Don't update if we have the same value */ + if (!error && !cmp) { + error = 0; + goto on_error; /* not really error */ + } + + if (update_reflog) { + git_refdb *refdb; + + if ((error = git_repository_refdb__weakptr(&refdb, backend->repo)) < 0 || + (error = git_refdb_should_write_reflog(&should_write, refdb, ref)) < 0) + goto on_error; + + if (should_write) { + if ((error = reflog_append(backend, ref, NULL, NULL, who, message)) < 0) + goto on_error; + if ((error = maybe_append_head(backend, ref, who, message)) < 0) + goto on_error; + } + } + + return loose_commit(file, ref); + +on_error: + git_filebuf_cleanup(file); + return error; +} + +static int refdb_fs_backend__prune_refs( + refdb_fs_backend *backend, + const char *ref_name, + const char *prefix) +{ + git_str relative_path = GIT_STR_INIT; + git_str base_path = GIT_STR_INIT; + size_t commonlen; + int error; + + GIT_ASSERT_ARG(backend); + GIT_ASSERT_ARG(ref_name); + + if ((error = git_str_sets(&relative_path, ref_name)) < 0) + goto cleanup; + + git_fs_path_squash_slashes(&relative_path); + if ((commonlen = git_fs_path_common_dirlen("refs/heads/", git_str_cstr(&relative_path))) == strlen("refs/heads/") || + (commonlen = git_fs_path_common_dirlen("refs/tags/", git_str_cstr(&relative_path))) == strlen("refs/tags/") || + (commonlen = git_fs_path_common_dirlen("refs/remotes/", git_str_cstr(&relative_path))) == strlen("refs/remotes/")) { + + git_str_truncate(&relative_path, commonlen); + + if (prefix) + error = git_str_join3(&base_path, '/', + backend->commonpath, prefix, + git_str_cstr(&relative_path)); + else + error = git_str_joinpath(&base_path, + backend->commonpath, + git_str_cstr(&relative_path)); + + if (!error) + error = git_path_validate_str_length(NULL, &base_path); + + if (error < 0) + goto cleanup; + + error = git_futils_rmdir_r(ref_name + commonlen, + git_str_cstr(&base_path), + GIT_RMDIR_EMPTY_PARENTS | GIT_RMDIR_SKIP_ROOT); + + if (error == GIT_ENOTFOUND) + error = 0; + } + +cleanup: + git_str_dispose(&relative_path); + git_str_dispose(&base_path); + return error; +} + +static int refdb_fs_backend__delete( + git_refdb_backend *_backend, + const char *ref_name, + const git_oid *old_id, const char *old_target) +{ + refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + git_filebuf file = GIT_FILEBUF_INIT; + int error = 0; + + GIT_ASSERT_ARG(backend); + GIT_ASSERT_ARG(ref_name); + + if ((error = loose_lock(&file, backend, ref_name)) < 0) + return error; + + if ((error = refdb_reflog_fs__delete(_backend, ref_name)) < 0) { + git_filebuf_cleanup(&file); + return error; + } + + return refdb_fs_backend__delete_tail(_backend, &file, ref_name, old_id, old_target); +} + +static int loose_delete(refdb_fs_backend *backend, const char *ref_name) +{ + git_str path = GIT_STR_INIT; + int error = 0; + + if ((error = loose_path(&path, backend->commonpath, ref_name)) < 0) + return error; + + error = p_unlink(path.ptr); + if (error < 0 && errno == ENOENT) + error = GIT_ENOTFOUND; + else if (error != 0) + error = -1; + + git_str_dispose(&path); + + return error; +} + +static int refdb_fs_backend__delete_tail( + git_refdb_backend *_backend, + git_filebuf *file, + const char *ref_name, + const git_oid *old_id, const char *old_target) +{ + refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + int error = 0, cmp = 0; + bool packed_deleted = 0; + + error = cmp_old_ref(&cmp, _backend, ref_name, old_id, old_target); + if (error < 0) + goto cleanup; + + if (cmp) { + git_error_set(GIT_ERROR_REFERENCE, "old reference value does not match"); + error = GIT_EMODIFIED; + goto cleanup; + } + + /* + * To ensure that an external observer will see either the current ref value + * (because the loose ref still exists), or a missing ref (after the packed-file is + * unlocked, there will be nothing left), we must ensure things happen in the + * following order: + * + * - the packed-ref file is locked and loaded, as well as a loose one, if it exists + * - we optimistically delete a packed ref, keeping track of whether it existed + * - we delete the loose ref, note that we have its .lock + * - the loose ref is "unlocked", then the packed-ref file is rewritten and unlocked + * - we should prune the path components if a loose ref was deleted + * + * Note that, because our packed backend doesn't expose its filesystem lock, + * we might not be able to guarantee that this is what actually happens (ie. + * as our current code never write packed-refs.lock, nothing stops observers + * from grabbing a "stale" value from there). + */ + if ((error = packed_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND) + goto cleanup; + + if (error == 0) + packed_deleted = 1; + + if ((error = loose_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND) + goto cleanup; + + if (error == GIT_ENOTFOUND) { + error = packed_deleted ? 0 : ref_error_notfound(ref_name); + goto cleanup; + } + +cleanup: + git_filebuf_cleanup(file); + if (error == 0) + error = refdb_fs_backend__prune_refs(backend, ref_name, ""); + return error; +} + +static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name); + +static int refdb_fs_backend__rename( + git_reference **out, + git_refdb_backend *_backend, + const char *old_name, + const char *new_name, + int force, + const git_signature *who, + const char *message) +{ + refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + git_reference *old, *new = NULL; + git_filebuf file = GIT_FILEBUF_INIT; + int error; + + GIT_ASSERT_ARG(backend); + + if ((error = reference_path_available( + backend, new_name, old_name, force)) < 0 || + (error = refdb_fs_backend__lookup(&old, _backend, old_name)) < 0) + return error; + + if ((error = loose_lock(&file, backend, old->name)) < 0) { + git_reference_free(old); + return error; + } + + new = git_reference__realloc(&old, new_name); + if (!new) { + git_reference_free(old); + git_filebuf_cleanup(&file); + return -1; + } + + if ((error = refdb_fs_backend__delete_tail(_backend, &file, old_name, NULL, NULL)) < 0) { + git_reference_free(new); + git_filebuf_cleanup(&file); + return error; + } + + if ((error = loose_lock(&file, backend, new_name)) < 0) { + git_reference_free(new); + return error; + } + + /* Try to rename the refog; it's ok if the old doesn't exist */ + error = refdb_reflog_fs__rename(_backend, old_name, new_name); + if (((error == 0) || (error == GIT_ENOTFOUND)) && + ((error = reflog_append(backend, new, git_reference_target(new), NULL, who, message)) < 0)) { + git_reference_free(new); + git_filebuf_cleanup(&file); + return error; + } + + if ((error = loose_commit(&file, new)) < 0 || out == NULL) { + git_reference_free(new); + git_filebuf_cleanup(&file); + return error; + } + + *out = new; + return 0; +} + +static int refdb_fs_backend__compress(git_refdb_backend *_backend) +{ + int error; + refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + + GIT_ASSERT_ARG(backend); + + if ((error = packed_reload(backend)) < 0 || /* load the existing packfile */ + (error = packed_loadloose(backend)) < 0 || /* add all the loose refs */ + (error = packed_write(backend)) < 0) /* write back to disk */ + return error; + + return 0; +} + +static void refdb_fs_backend__free(git_refdb_backend *_backend) +{ + refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + + if (!backend) + return; + + git_sortedcache_free(backend->refcache); + + git_mutex_lock(&backend->prlock); + packed_map_free(backend); + git_mutex_unlock(&backend->prlock); + git_mutex_free(&backend->prlock); + + git__free(backend->gitpath); + git__free(backend->commonpath); + git__free(backend); +} + +static char *setup_namespace(git_repository *repo, const char *in) +{ + git_str path = GIT_STR_INIT; + char *parts, *start, *end, *out = NULL; + + if (!in) + goto done; + + git_str_puts(&path, in); + + /* if the repo is not namespaced, nothing else to do */ + if (repo->namespace == NULL) { + out = git_str_detach(&path); + goto done; + } + + parts = end = git__strdup(repo->namespace); + if (parts == NULL) + goto done; + + /* + * From `man gitnamespaces`: + * namespaces which include a / will expand to a hierarchy + * of namespaces; for example, GIT_NAMESPACE=foo/bar will store + * refs under refs/namespaces/foo/refs/namespaces/bar/ + */ + while ((start = git__strsep(&end, "/")) != NULL) + git_str_printf(&path, "refs/namespaces/%s/", start); + + git_str_printf(&path, "refs/namespaces/%s/refs", end); + git__free(parts); + + /* Make sure that the folder with the namespace exists */ + if (git_futils_mkdir_relative(git_str_cstr(&path), in, 0777, + GIT_MKDIR_PATH, NULL) < 0) + goto done; + + /* Return root of the namespaced gitpath, i.e. without the trailing 'refs' */ + git_str_rtruncate_at_char(&path, '/'); + git_str_putc(&path, '/'); + out = git_str_detach(&path); + +done: + git_str_dispose(&path); + return out; +} + +static int reflog_alloc( + git_reflog **reflog, + const char *name, + git_oid_t oid_type) +{ + git_reflog *log; + + *reflog = NULL; + + log = git__calloc(1, sizeof(git_reflog)); + GIT_ERROR_CHECK_ALLOC(log); + + log->ref_name = git__strdup(name); + GIT_ERROR_CHECK_ALLOC(log->ref_name); + + log->oid_type = oid_type; + + if (git_vector_init(&log->entries, 0, NULL) < 0) { + git__free(log->ref_name); + git__free(log); + return -1; + } + + *reflog = log; + + return 0; +} + +static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size) +{ + git_parse_ctx parser = GIT_PARSE_CTX_INIT; + + if ((git_parse_ctx_init(&parser, buf, buf_size)) < 0) + return -1; + + for (; parser.remain_len; git_parse_advance_line(&parser)) { + git_reflog_entry *entry; + const char *sig; + char c; + + entry = git__calloc(1, sizeof(*entry)); + GIT_ERROR_CHECK_ALLOC(entry); + entry->committer = git__calloc(1, sizeof(*entry->committer)); + GIT_ERROR_CHECK_ALLOC(entry->committer); + + if (git_parse_advance_oid(&entry->oid_old, &parser, log->oid_type) < 0 || + git_parse_advance_expected(&parser, " ", 1) < 0 || + git_parse_advance_oid(&entry->oid_cur, &parser, log->oid_type) < 0) + goto next; + + sig = parser.line; + while (git_parse_peek(&c, &parser, 0) == 0 && c != '\t' && c != '\n') + git_parse_advance_chars(&parser, 1); + + if (git_signature__parse(entry->committer, &sig, parser.line, NULL, 0) < 0) + goto next; + + if (c == '\t') { + size_t len; + git_parse_advance_chars(&parser, 1); + + len = parser.line_len; + if (parser.line[len - 1] == '\n') + len--; + + entry->msg = git__strndup(parser.line, len); + GIT_ERROR_CHECK_ALLOC(entry->msg); + } + + if ((git_vector_insert(&log->entries, entry)) < 0) { + git_reflog_entry__free(entry); + return -1; + } + + continue; + +next: + git_reflog_entry__free(entry); + } + + return 0; +} + +static int create_new_reflog_file(const char *filepath) +{ + int fd, error; + + if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0) + return error; + + if ((fd = p_open(filepath, + O_WRONLY | O_CREAT, + GIT_REFLOG_FILE_MODE)) < 0) + return -1; + + return p_close(fd); +} + +static int refdb_reflog_fs__ensure_log(git_refdb_backend *_backend, const char *name) +{ + refdb_fs_backend *backend; + git_repository *repo; + git_str path = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(_backend && name); + + backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + repo = backend->repo; + + if ((error = reflog_path(&path, repo, name)) < 0) + return error; + + error = create_new_reflog_file(git_str_cstr(&path)); + git_str_dispose(&path); + + return error; +} + +static int has_reflog(git_repository *repo, const char *name) +{ + int ret = 0; + git_str path = GIT_STR_INIT; + + if (reflog_path(&path, repo, name) < 0) + goto cleanup; + + ret = git_fs_path_isfile(git_str_cstr(&path)); + +cleanup: + git_str_dispose(&path); + return ret; +} + +static int refdb_reflog_fs__has_log(git_refdb_backend *_backend, const char *name) +{ + refdb_fs_backend *backend; + + GIT_ASSERT_ARG(_backend); + GIT_ASSERT_ARG(name); + + backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + + return has_reflog(backend->repo, name); +} + +static int refdb_reflog_fs__read( + git_reflog **out, + git_refdb_backend *_backend, + const char *name) +{ + int error = -1; + git_str log_path = GIT_STR_INIT; + git_str log_file = GIT_STR_INIT; + git_reflog *log = NULL; + git_repository *repo; + refdb_fs_backend *backend; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(_backend); + GIT_ASSERT_ARG(name); + + backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + repo = backend->repo; + + if (reflog_alloc(&log, name, backend->oid_type) < 0) + return -1; + + if (reflog_path(&log_path, repo, name) < 0) + goto cleanup; + + error = git_futils_readbuffer(&log_file, git_str_cstr(&log_path)); + if (error < 0 && error != GIT_ENOTFOUND) + goto cleanup; + + if ((error == GIT_ENOTFOUND) && + ((error = create_new_reflog_file(git_str_cstr(&log_path))) < 0)) + goto cleanup; + + if ((error = reflog_parse(log, + git_str_cstr(&log_file), git_str_len(&log_file))) < 0) + goto cleanup; + + *out = log; + goto success; + +cleanup: + git_reflog_free(log); + +success: + git_str_dispose(&log_file); + git_str_dispose(&log_path); + + return error; +} + +static int serialize_reflog_entry( + git_str *buf, + const git_oid *oid_old, + const git_oid *oid_new, + const git_signature *committer, + const char *msg) +{ + char raw_old[GIT_OID_MAX_HEXSIZE + 1]; + char raw_new[GIT_OID_MAX_HEXSIZE + 1]; + + git_oid_tostr(raw_old, GIT_OID_MAX_HEXSIZE + 1, oid_old); + git_oid_tostr(raw_new, GIT_OID_MAX_HEXSIZE + 1, oid_new); + + git_str_clear(buf); + + git_str_puts(buf, raw_old); + git_str_putc(buf, ' '); + git_str_puts(buf, raw_new); + + git_signature__writebuf(buf, " ", committer); + + /* drop trailing LF */ + git_str_rtrim(buf); + + if (msg) { + size_t i; + + git_str_putc(buf, '\t'); + git_str_puts(buf, msg); + + for (i = 0; i < buf->size - 2; i++) + if (buf->ptr[i] == '\n') + buf->ptr[i] = ' '; + git_str_rtrim(buf); + } + + git_str_putc(buf, '\n'); + + return git_str_oom(buf); +} + +static int lock_reflog(git_filebuf *file, refdb_fs_backend *backend, const char *refname) +{ + git_repository *repo; + git_str log_path = GIT_STR_INIT; + int error; + + repo = backend->repo; + + if (!git_path_is_valid(backend->repo, refname, 0, GIT_FS_PATH_REJECT_FILESYSTEM_DEFAULTS)) { + git_error_set(GIT_ERROR_INVALID, "invalid reference name '%s'", refname); + return GIT_EINVALIDSPEC; + } + + if (reflog_path(&log_path, repo, refname) < 0) + return -1; + + if (!git_fs_path_isfile(git_str_cstr(&log_path))) { + git_error_set(GIT_ERROR_INVALID, + "log file for reference '%s' doesn't exist", refname); + error = -1; + goto cleanup; + } + + error = git_filebuf_open(file, git_str_cstr(&log_path), 0, GIT_REFLOG_FILE_MODE); + +cleanup: + git_str_dispose(&log_path); + + return error; +} + +static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflog) +{ + int error = -1; + unsigned int i; + git_reflog_entry *entry; + refdb_fs_backend *backend; + git_str log = GIT_STR_INIT; + git_filebuf fbuf = GIT_FILEBUF_INIT; + + GIT_ASSERT_ARG(_backend); + GIT_ASSERT_ARG(reflog); + + backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + + if ((error = lock_reflog(&fbuf, backend, reflog->ref_name)) < 0) + return -1; + + git_vector_foreach(&reflog->entries, i, entry) { + if (serialize_reflog_entry(&log, &(entry->oid_old), &(entry->oid_cur), entry->committer, entry->msg) < 0) + goto cleanup; + + if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0) + goto cleanup; + } + + error = git_filebuf_commit(&fbuf); + goto success; + +cleanup: + git_filebuf_cleanup(&fbuf); + +success: + git_str_dispose(&log); + + return error; +} + +/* Append to the reflog, must be called under reference lock */ +static int reflog_append( + refdb_fs_backend *backend, + const git_reference *ref, + const git_oid *old, + const git_oid *new, + const git_signature *who, + const char *message) +{ + int error, is_symbolic, open_flags; + git_oid old_id, new_id; + git_str buf = GIT_STR_INIT, path = GIT_STR_INIT; + git_repository *repo = backend->repo; + + is_symbolic = ref->type == GIT_REFERENCE_SYMBOLIC; + + /* "normal" symbolic updates do not write */ + if (is_symbolic && + strcmp(ref->name, GIT_HEAD_FILE) && + !(old && new)) + return 0; + + /* From here on is_symbolic also means that it's HEAD */ + + git_oid_clear(&old_id, backend->oid_type); + git_oid_clear(&new_id, backend->oid_type); + + if (old) { + git_oid_cpy(&old_id, old); + } else { + error = git_reference_name_to_id(&old_id, repo, ref->name); + if (error < 0 && error != GIT_ENOTFOUND) + return error; + } + + if (new) { + git_oid_cpy(&new_id, new); + } else { + if (!is_symbolic) { + git_oid_cpy(&new_id, git_reference_target(ref)); + } else { + error = git_reference_name_to_id(&new_id, repo, git_reference_symbolic_target(ref)); + if (error < 0 && error != GIT_ENOTFOUND) + return error; + /* detaching HEAD does not create an entry */ + if (error == GIT_ENOTFOUND) + return 0; + + git_error_clear(); + } + } + + if ((error = serialize_reflog_entry(&buf, &old_id, &new_id, who, message)) < 0) + goto cleanup; + + if ((error = reflog_path(&path, repo, ref->name)) < 0) + goto cleanup; + + if (((error = git_futils_mkpath2file(git_str_cstr(&path), 0777)) < 0) && + (error != GIT_EEXISTS)) { + goto cleanup; + } + + /* If the new branch matches part of the namespace of a previously deleted branch, + * there maybe an obsolete/unused directory (or directory hierarchy) in the way. + */ + if (git_fs_path_isdir(git_str_cstr(&path))) { + if ((error = git_futils_rmdir_r(git_str_cstr(&path), NULL, GIT_RMDIR_SKIP_NONEMPTY)) < 0) { + if (error == GIT_ENOTFOUND) + error = 0; + } else if (git_fs_path_isdir(git_str_cstr(&path))) { + git_error_set(GIT_ERROR_REFERENCE, "cannot create reflog at '%s', there are reflogs beneath that folder", + ref->name); + error = GIT_EDIRECTORY; + } + + if (error != 0) + goto cleanup; + } + + open_flags = O_WRONLY | O_CREAT | O_APPEND; + + if (backend->fsync) + open_flags |= O_FSYNC; + + error = git_futils_writebuffer(&buf, git_str_cstr(&path), open_flags, GIT_REFLOG_FILE_MODE); + +cleanup: + git_str_dispose(&buf); + git_str_dispose(&path); + + return error; +} + +static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name) +{ + int error = 0, fd; + git_str old_path = GIT_STR_INIT; + git_str new_path = GIT_STR_INIT; + git_str temp_path = GIT_STR_INIT; + git_str normalized = GIT_STR_INIT; + git_repository *repo; + refdb_fs_backend *backend; + + GIT_ASSERT_ARG(_backend); + GIT_ASSERT_ARG(old_name); + GIT_ASSERT_ARG(new_name); + + backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + repo = backend->repo; + + if ((error = git_reference__normalize_name( + &normalized, new_name, GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL)) < 0) + return error; + + if (git_str_joinpath(&temp_path, repo->gitdir, GIT_REFLOG_DIR) < 0) + return -1; + + if ((error = loose_path(&old_path, git_str_cstr(&temp_path), old_name)) < 0) + return error; + + if ((error = loose_path(&new_path, git_str_cstr(&temp_path), git_str_cstr(&normalized))) < 0) + return error; + + if (!git_fs_path_exists(git_str_cstr(&old_path))) { + error = GIT_ENOTFOUND; + goto cleanup; + } + + /* + * Move the reflog to a temporary place. This two-phase renaming is required + * in order to cope with funny renaming use cases when one tries to move a reference + * to a partially colliding namespace: + * - a/b -> a/b/c + * - a/b/c/d -> a/b/c + */ + if ((error = loose_path(&temp_path, git_str_cstr(&temp_path), "temp_reflog")) < 0) + return error; + + if ((fd = git_futils_mktmp(&temp_path, git_str_cstr(&temp_path), GIT_REFLOG_FILE_MODE)) < 0) { + error = -1; + goto cleanup; + } + + p_close(fd); + + if (p_rename(git_str_cstr(&old_path), git_str_cstr(&temp_path)) < 0) { + git_error_set(GIT_ERROR_OS, "failed to rename reflog for %s", new_name); + error = -1; + goto cleanup; + } + + if (git_fs_path_isdir(git_str_cstr(&new_path)) && + (git_futils_rmdir_r(git_str_cstr(&new_path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) { + error = -1; + goto cleanup; + } + + if (git_futils_mkpath2file(git_str_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) { + error = -1; + goto cleanup; + } + + if (p_rename(git_str_cstr(&temp_path), git_str_cstr(&new_path)) < 0) { + git_error_set(GIT_ERROR_OS, "failed to rename reflog for %s", new_name); + error = -1; + } + +cleanup: + git_str_dispose(&temp_path); + git_str_dispose(&old_path); + git_str_dispose(&new_path); + git_str_dispose(&normalized); + + return error; +} + +static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name) +{ + refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent); + git_str path = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(_backend); + GIT_ASSERT_ARG(name); + + if ((error = reflog_path(&path, backend->repo, name)) < 0) + goto out; + + /* + * If a reference was moved downwards, eg refs/heads/br2 -> refs/heads/br2/new-name, + * refs/heads/br2 does exist but it's a directory. That's a valid situation. + * Proceed only if it's a file. + */ + if (!git_fs_path_isfile(path.ptr)) + goto out; + + if ((error = p_unlink(path.ptr)) < 0) + goto out; + + error = refdb_fs_backend__prune_refs(backend, name, GIT_REFLOG_DIR); + +out: + git_str_dispose(&path); + + return error; +} + +int git_refdb_backend_fs( + git_refdb_backend **backend_out, + git_repository *repository) +{ + int t = 0; + git_str gitpath = GIT_STR_INIT; + refdb_fs_backend *backend; + + backend = git__calloc(1, sizeof(refdb_fs_backend)); + GIT_ERROR_CHECK_ALLOC(backend); + if (git_mutex_init(&backend->prlock) < 0) { + git__free(backend); + return -1; + } + + + if (git_refdb_init_backend(&backend->parent, GIT_REFDB_BACKEND_VERSION) < 0) + goto fail; + + backend->repo = repository; + backend->oid_type = repository->oid_type; + + if (repository->gitdir) { + backend->gitpath = setup_namespace(repository, repository->gitdir); + + if (backend->gitpath == NULL) + goto fail; + } + + if (repository->commondir) { + backend->commonpath = setup_namespace(repository, repository->commondir); + + if (backend->commonpath == NULL) + goto fail; + } + + if (git_str_joinpath(&gitpath, backend->commonpath, GIT_PACKEDREFS_FILE) < 0 || + git_sortedcache_new( + &backend->refcache, offsetof(struct packref, name), + NULL, NULL, packref_cmp, git_str_cstr(&gitpath)) < 0) + goto fail; + + git_str_dispose(&gitpath); + + if (!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_IGNORECASE) && t) { + backend->iterator_flags |= GIT_ITERATOR_IGNORE_CASE; + backend->direach_flags |= GIT_FS_PATH_DIR_IGNORE_CASE; + } + if (!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_PRECOMPOSE) && t) { + backend->iterator_flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE; + backend->direach_flags |= GIT_FS_PATH_DIR_PRECOMPOSE_UNICODE; + } + if ((!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_FSYNCOBJECTFILES) && t) || + git_repository__fsync_gitdir) + backend->fsync = 1; + backend->iterator_flags |= GIT_ITERATOR_DESCEND_SYMLINKS; + + backend->parent.init = &refdb_fs_backend__init; + backend->parent.exists = &refdb_fs_backend__exists; + backend->parent.lookup = &refdb_fs_backend__lookup; + backend->parent.iterator = &refdb_fs_backend__iterator; + backend->parent.write = &refdb_fs_backend__write; + backend->parent.del = &refdb_fs_backend__delete; + backend->parent.rename = &refdb_fs_backend__rename; + backend->parent.compress = &refdb_fs_backend__compress; + backend->parent.lock = &refdb_fs_backend__lock; + backend->parent.unlock = &refdb_fs_backend__unlock; + backend->parent.has_log = &refdb_reflog_fs__has_log; + backend->parent.ensure_log = &refdb_reflog_fs__ensure_log; + backend->parent.free = &refdb_fs_backend__free; + backend->parent.reflog_read = &refdb_reflog_fs__read; + backend->parent.reflog_write = &refdb_reflog_fs__write; + backend->parent.reflog_rename = &refdb_reflog_fs__rename; + backend->parent.reflog_delete = &refdb_reflog_fs__delete; + + *backend_out = (git_refdb_backend *)backend; + return 0; + +fail: + git_mutex_free(&backend->prlock); + git_str_dispose(&gitpath); + git__free(backend->gitpath); + git__free(backend->commonpath); + git__free(backend); + return -1; +} diff --git a/src/libgit2/reflog.c b/src/libgit2/reflog.c new file mode 100644 index 00000000000..b998172916e --- /dev/null +++ b/src/libgit2/reflog.c @@ -0,0 +1,233 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "reflog.h" + +#include "repository.h" +#include "filebuf.h" +#include "signature.h" +#include "refdb.h" + +#include "git2/sys/refdb_backend.h" + +void git_reflog_entry__free(git_reflog_entry *entry) +{ + git_signature_free(entry->committer); + + git__free(entry->msg); + git__free(entry); +} + +void git_reflog_free(git_reflog *reflog) +{ + size_t i; + git_reflog_entry *entry; + + if (reflog == NULL) + return; + + if (reflog->db) + GIT_REFCOUNT_DEC(reflog->db, git_refdb__free); + + for (i=0; i < reflog->entries.length; i++) { + entry = git_vector_get(&reflog->entries, i); + + git_reflog_entry__free(entry); + } + + git_vector_dispose(&reflog->entries); + git__free(reflog->ref_name); + git__free(reflog); +} + +int git_reflog_read(git_reflog **reflog, git_repository *repo, const char *name) +{ + git_refdb *refdb; + int error; + + GIT_ASSERT_ARG(reflog); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0) + return error; + + return git_refdb_reflog_read(reflog, refdb, name); +} + +int git_reflog_write(git_reflog *reflog) +{ + git_refdb *db; + + GIT_ASSERT_ARG(reflog); + GIT_ASSERT_ARG(reflog->db); + + db = reflog->db; + return db->backend->reflog_write(db->backend, reflog); +} + +int git_reflog_append( + git_reflog *reflog, + const git_oid *new_oid, + const git_signature *committer, + const char *msg) +{ + const git_reflog_entry *previous; + git_reflog_entry *entry; + + GIT_ASSERT_ARG(reflog); + GIT_ASSERT_ARG(new_oid); + GIT_ASSERT_ARG(committer); + + entry = git__calloc(1, sizeof(git_reflog_entry)); + GIT_ERROR_CHECK_ALLOC(entry); + + if ((git_signature_dup(&entry->committer, committer)) < 0) + goto cleanup; + + if (msg != NULL) { + size_t i, msglen = strlen(msg); + + if ((entry->msg = git__strndup(msg, msglen)) == NULL) + goto cleanup; + + /* + * Replace all newlines with spaces, except for + * the final trailing newline. + */ + for (i = 0; i < msglen; i++) + if (entry->msg[i] == '\n') + entry->msg[i] = ' '; + } + + previous = git_reflog_entry_byindex(reflog, 0); + + if (previous == NULL) + git_oid_clear(&entry->oid_old, reflog->oid_type); + else + git_oid_cpy(&entry->oid_old, &previous->oid_cur); + + git_oid_cpy(&entry->oid_cur, new_oid); + + if (git_vector_insert(&reflog->entries, entry) < 0) + goto cleanup; + + return 0; + +cleanup: + git_reflog_entry__free(entry); + return -1; +} + +int git_reflog_rename(git_repository *repo, const char *old_name, const char *new_name) +{ + git_refdb *refdb; + int error; + + if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0) + return -1; + + return refdb->backend->reflog_rename(refdb->backend, old_name, new_name); +} + +int git_reflog_delete(git_repository *repo, const char *name) +{ + git_refdb *refdb; + int error; + + if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0) + return -1; + + return refdb->backend->reflog_delete(refdb->backend, name); +} + +size_t git_reflog_entrycount(git_reflog *reflog) +{ + GIT_ASSERT_ARG_WITH_RETVAL(reflog, 0); + return reflog->entries.length; +} + +const git_reflog_entry *git_reflog_entry_byindex(const git_reflog *reflog, size_t idx) +{ + GIT_ASSERT_ARG_WITH_RETVAL(reflog, NULL); + + if (idx >= reflog->entries.length) + return NULL; + + return git_vector_get( + &reflog->entries, reflog_inverse_index(idx, reflog->entries.length)); +} + +const git_oid *git_reflog_entry_id_old(const git_reflog_entry *entry) +{ + GIT_ASSERT_ARG_WITH_RETVAL(entry, NULL); + return &entry->oid_old; +} + +const git_oid *git_reflog_entry_id_new(const git_reflog_entry *entry) +{ + GIT_ASSERT_ARG_WITH_RETVAL(entry, NULL); + return &entry->oid_cur; +} + +const git_signature *git_reflog_entry_committer(const git_reflog_entry *entry) +{ + GIT_ASSERT_ARG_WITH_RETVAL(entry, NULL); + return entry->committer; +} + +const char *git_reflog_entry_message(const git_reflog_entry *entry) +{ + GIT_ASSERT_ARG_WITH_RETVAL(entry, NULL); + return entry->msg; +} + +int git_reflog_drop(git_reflog *reflog, size_t idx, int rewrite_previous_entry) +{ + size_t entrycount; + git_reflog_entry *entry, *previous; + + entrycount = git_reflog_entrycount(reflog); + + entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx); + + if (entry == NULL) { + git_error_set(GIT_ERROR_REFERENCE, "no reflog entry at index %"PRIuZ, idx); + return GIT_ENOTFOUND; + } + + git_reflog_entry__free(entry); + + if (git_vector_remove( + &reflog->entries, reflog_inverse_index(idx, entrycount)) < 0) + return -1; + + if (!rewrite_previous_entry) + return 0; + + /* No need to rewrite anything when removing the most recent entry */ + if (idx == 0) + return 0; + + /* Have the latest entry just been dropped? */ + if (entrycount == 1) + return 0; + + entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1); + + /* If the oldest entry has just been removed... */ + if (idx == entrycount - 1) { + /* ...clear the oid_old member of the "new" oldest entry */ + git_oid_clear(&entry->oid_old, reflog->oid_type); + return 0; + } + + previous = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx); + git_oid_cpy(&entry->oid_old, &previous->oid_cur); + + return 0; +} diff --git a/src/reflog.h b/src/libgit2/reflog.h similarity index 75% rename from src/reflog.h rename to src/libgit2/reflog.h index 9444ebd108d..ab3afdf10fc 100644 --- a/src/reflog.h +++ b/src/libgit2/reflog.h @@ -8,6 +8,7 @@ #define INCLUDE_reflog_h__ #include "common.h" + #include "git2/reflog.h" #include "vector.h" @@ -15,8 +16,6 @@ #define GIT_REFLOG_DIR_MODE 0777 #define GIT_REFLOG_FILE_MODE 0666 -#define GIT_REFLOG_SIZE_MIN (2*GIT_OID_HEXSZ+2+17) - struct git_reflog_entry { git_oid oid_old; git_oid oid_cur; @@ -27,9 +26,17 @@ struct git_reflog_entry { }; struct git_reflog { + git_refdb *db; char *ref_name; - git_repository *owner; + git_oid_t oid_type; git_vector entries; }; -#endif /* INCLUDE_reflog_h__ */ +GIT_INLINE(size_t) reflog_inverse_index(size_t idx, size_t total) +{ + return (total - 1) - idx; +} + +void git_reflog_entry__free(git_reflog_entry *entry); + +#endif diff --git a/src/libgit2/refs.c b/src/libgit2/refs.c new file mode 100644 index 00000000000..ac9063d59d1 --- /dev/null +++ b/src/libgit2/refs.c @@ -0,0 +1,1428 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "refs.h" + +#include "hash.h" +#include "repository.h" +#include "futils.h" +#include "filebuf.h" +#include "pack.h" +#include "reflog.h" +#include "refdb.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +bool git_reference__enable_symbolic_ref_target_validation = true; + +enum { + GIT_PACKREF_HAS_PEEL = 1, + GIT_PACKREF_WAS_LOOSE = 2 +}; + +static git_reference *alloc_ref(const char *name) +{ + git_reference *ref = NULL; + size_t namelen = strlen(name), reflen; + + if (!GIT_ADD_SIZET_OVERFLOW(&reflen, sizeof(git_reference), namelen) && + !GIT_ADD_SIZET_OVERFLOW(&reflen, reflen, 1) && + (ref = git__calloc(1, reflen)) != NULL) + memcpy(ref->name, name, namelen + 1); + + return ref; +} + +git_reference *git_reference__alloc_symbolic( + const char *name, const char *target) +{ + git_reference *ref; + + GIT_ASSERT_ARG_WITH_RETVAL(name, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(target, NULL); + + ref = alloc_ref(name); + if (!ref) + return NULL; + + ref->type = GIT_REFERENCE_SYMBOLIC; + + if ((ref->target.symbolic = git__strdup(target)) == NULL) { + git__free(ref); + return NULL; + } + + return ref; +} + +git_reference *git_reference__alloc( + const char *name, + const git_oid *oid, + const git_oid *peel) +{ + git_oid_t oid_type; + git_reference *ref; + + GIT_ASSERT_ARG_WITH_RETVAL(name, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(oid, NULL); + + ref = alloc_ref(name); + if (!ref) + return NULL; + + ref->type = GIT_REFERENCE_DIRECT; + git_oid_cpy(&ref->target.oid, oid); + +#ifdef GIT_EXPERIMENTAL_SHA256 + oid_type = oid->type; +#else + oid_type = GIT_OID_SHA1; +#endif + + if (peel != NULL) + git_oid_cpy(&ref->peel, peel); + else + git_oid_clear(&ref->peel, oid_type); + + return ref; +} + +git_reference *git_reference__realloc( + git_reference **ptr_to_ref, const char *name) +{ + size_t namelen, reflen; + git_reference *rewrite = NULL; + + GIT_ASSERT_ARG_WITH_RETVAL(ptr_to_ref, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(name, NULL); + + namelen = strlen(name); + + if (!GIT_ADD_SIZET_OVERFLOW(&reflen, sizeof(git_reference), namelen) && + !GIT_ADD_SIZET_OVERFLOW(&reflen, reflen, 1) && + (rewrite = git__realloc(*ptr_to_ref, reflen)) != NULL) + memcpy(rewrite->name, name, namelen + 1); + + *ptr_to_ref = NULL; + + return rewrite; +} + +int git_reference_dup(git_reference **dest, git_reference *source) +{ + if (source->type == GIT_REFERENCE_SYMBOLIC) + *dest = git_reference__alloc_symbolic(source->name, source->target.symbolic); + else + *dest = git_reference__alloc(source->name, &source->target.oid, &source->peel); + + GIT_ERROR_CHECK_ALLOC(*dest); + + (*dest)->db = source->db; + GIT_REFCOUNT_INC((*dest)->db); + + return 0; +} + +void git_reference_free(git_reference *reference) +{ + if (reference == NULL) + return; + + if (reference->type == GIT_REFERENCE_SYMBOLIC) + git__free(reference->target.symbolic); + + if (reference->db) + GIT_REFCOUNT_DEC(reference->db, git_refdb__free); + + git__free(reference); +} + +int git_reference_delete(git_reference *ref) +{ + const git_oid *old_id = NULL; + const char *old_target = NULL; + + if (!strcmp(ref->name, "HEAD")) { + git_error_set(GIT_ERROR_REFERENCE, "cannot delete HEAD"); + return GIT_ERROR; + } + + if (ref->type == GIT_REFERENCE_DIRECT) + old_id = &ref->target.oid; + else + old_target = ref->target.symbolic; + + return git_refdb_delete(ref->db, ref->name, old_id, old_target); +} + +int git_reference_remove(git_repository *repo, const char *name) +{ + git_refdb *db; + int error; + + if ((error = git_repository_refdb__weakptr(&db, repo)) < 0) + return error; + + return git_refdb_delete(db, name, NULL, NULL); +} + +int git_reference_lookup(git_reference **ref_out, + git_repository *repo, const char *name) +{ + return git_reference_lookup_resolved(ref_out, repo, name, 0); +} + +int git_reference_name_to_id( + git_oid *out, git_repository *repo, const char *name) +{ + int error; + git_reference *ref; + + if ((error = git_reference_lookup_resolved(&ref, repo, name, -1)) < 0) + return error; + + git_oid_cpy(out, git_reference_target(ref)); + git_reference_free(ref); + return 0; +} + +static int reference_normalize_for_repo( + git_refname_t out, + git_repository *repo, + const char *name, + bool validate) +{ + int precompose; + unsigned int flags = GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL; + + if (!git_repository__configmap_lookup(&precompose, repo, GIT_CONFIGMAP_PRECOMPOSE) && + precompose) + flags |= GIT_REFERENCE_FORMAT__PRECOMPOSE_UNICODE; + + if (!validate) + flags |= GIT_REFERENCE_FORMAT__VALIDATION_DISABLE; + + return git_reference_normalize_name(out, GIT_REFNAME_MAX, name, flags); +} + +int git_reference_lookup_resolved( + git_reference **ref_out, + git_repository *repo, + const char *name, + int max_nesting) +{ + git_refname_t normalized; + git_refdb *refdb; + int error = 0; + + GIT_ASSERT_ARG(ref_out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + if ((error = reference_normalize_for_repo(normalized, repo, name, true)) < 0 || + (error = git_repository_refdb__weakptr(&refdb, repo)) < 0 || + (error = git_refdb_resolve(ref_out, refdb, normalized, max_nesting)) < 0) + return error; + + /* + * The resolved reference may be a symbolic reference in case its + * target doesn't exist. If the user asked us to resolve (e.g. + * `max_nesting != 0`), then we need to return an error in case we got + * a symbolic reference back. + */ + if (max_nesting && git_reference_type(*ref_out) == GIT_REFERENCE_SYMBOLIC) { + git_reference_free(*ref_out); + *ref_out = NULL; + return GIT_ENOTFOUND; + } + + return 0; +} + +int git_reference_dwim(git_reference **out, git_repository *repo, const char *refname) +{ + int error = 0, i, valid; + bool fallbackmode = true, foundvalid = false; + git_reference *ref; + git_str refnamebuf = GIT_STR_INIT, name = GIT_STR_INIT; + + static const char *formatters[] = { + "%s", + GIT_REFS_DIR "%s", + GIT_REFS_TAGS_DIR "%s", + GIT_REFS_HEADS_DIR "%s", + GIT_REFS_REMOTES_DIR "%s", + GIT_REFS_REMOTES_DIR "%s/" GIT_HEAD_FILE, + NULL + }; + + if (*refname) + git_str_puts(&name, refname); + else { + git_str_puts(&name, GIT_HEAD_FILE); + fallbackmode = false; + } + + for (i = 0; formatters[i] && (fallbackmode || i == 0); i++) { + + git_str_clear(&refnamebuf); + + if ((error = git_str_printf(&refnamebuf, formatters[i], git_str_cstr(&name))) < 0 || + (error = git_reference_name_is_valid(&valid, git_str_cstr(&refnamebuf))) < 0) + goto cleanup; + + if (!valid) { + error = GIT_EINVALIDSPEC; + continue; + } + foundvalid = true; + + error = git_reference_lookup_resolved(&ref, repo, git_str_cstr(&refnamebuf), -1); + + if (!error) { + *out = ref; + error = 0; + goto cleanup; + } + + if (error != GIT_ENOTFOUND) + goto cleanup; + } + +cleanup: + if (error && !foundvalid) { + /* never found a valid reference name */ + git_error_set(GIT_ERROR_REFERENCE, + "could not use '%s' as valid reference name", git_str_cstr(&name)); + } + + if (error == GIT_ENOTFOUND) + git_error_set(GIT_ERROR_REFERENCE, "no reference found for shorthand '%s'", refname); + + git_str_dispose(&name); + git_str_dispose(&refnamebuf); + return error; +} + +/** + * Getters + */ +git_reference_t git_reference_type(const git_reference *ref) +{ + GIT_ASSERT_ARG(ref); + return ref->type; +} + +const char *git_reference_name(const git_reference *ref) +{ + GIT_ASSERT_ARG_WITH_RETVAL(ref, NULL); + return ref->name; +} + +git_repository *git_reference_owner(const git_reference *ref) +{ + GIT_ASSERT_ARG_WITH_RETVAL(ref, NULL); + return ref->db->repo; +} + +const git_oid *git_reference_target(const git_reference *ref) +{ + GIT_ASSERT_ARG_WITH_RETVAL(ref, NULL); + + if (ref->type != GIT_REFERENCE_DIRECT) + return NULL; + + return &ref->target.oid; +} + +const git_oid *git_reference_target_peel(const git_reference *ref) +{ + GIT_ASSERT_ARG_WITH_RETVAL(ref, NULL); + + if (ref->type != GIT_REFERENCE_DIRECT || git_oid_is_zero(&ref->peel)) + return NULL; + + return &ref->peel; +} + +const char *git_reference_symbolic_target(const git_reference *ref) +{ + GIT_ASSERT_ARG_WITH_RETVAL(ref, NULL); + + if (ref->type != GIT_REFERENCE_SYMBOLIC) + return NULL; + + return ref->target.symbolic; +} + +static int reference__create( + git_reference **ref_out, + git_repository *repo, + const char *name, + const git_oid *oid, + const char *symbolic, + int force, + const git_signature *signature, + const char *log_message, + const git_oid *old_id, + const char *old_target) +{ + git_refname_t normalized; + git_refdb *refdb; + git_reference *ref = NULL; + int error = 0; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + GIT_ASSERT_ARG(symbolic || signature); + + if (ref_out) + *ref_out = NULL; + + error = reference_normalize_for_repo(normalized, repo, name, true); + if (error < 0) + return error; + + error = git_repository_refdb__weakptr(&refdb, repo); + if (error < 0) + return error; + + if (oid != NULL) { + GIT_ASSERT(symbolic == NULL); + + if (!git_object__is_valid(repo, oid, GIT_OBJECT_ANY)) { + git_error_set(GIT_ERROR_REFERENCE, + "target OID for the reference doesn't exist on the repository"); + return -1; + } + + ref = git_reference__alloc(normalized, oid, NULL); + } else { + git_refname_t normalized_target; + + error = reference_normalize_for_repo(normalized_target, repo, + symbolic, git_reference__enable_symbolic_ref_target_validation); + + if (error < 0) + return error; + + ref = git_reference__alloc_symbolic(normalized, normalized_target); + } + + GIT_ERROR_CHECK_ALLOC(ref); + + if ((error = git_refdb_write(refdb, ref, force, signature, log_message, old_id, old_target)) < 0) { + git_reference_free(ref); + return error; + } + + if (ref_out == NULL) + git_reference_free(ref); + else + *ref_out = ref; + + return 0; +} + +static int refs_configured_ident(git_signature **out, const git_repository *repo) +{ + if (repo->ident_name && repo->ident_email) + return git_signature_now(out, repo->ident_name, repo->ident_email); + + /* if not configured let us fall-through to the next method */ + return -1; +} + +int git_reference__log_signature(git_signature **out, git_repository *repo) +{ + int error; + git_signature *who; + + if(((error = refs_configured_ident(&who, repo)) < 0) && + ((error = git_signature_default(&who, repo)) < 0) && + ((error = git_signature_now(&who, "unknown", "unknown")) < 0)) + return error; + + *out = who; + return 0; +} + +int git_reference_create_matching( + git_reference **ref_out, + git_repository *repo, + const char *name, + const git_oid *id, + int force, + const git_oid *old_id, + const char *log_message) + +{ + int error; + git_signature *who = NULL; + + GIT_ASSERT_ARG(id); + + if ((error = git_reference__log_signature(&who, repo)) < 0) + return error; + + error = reference__create( + ref_out, repo, name, id, NULL, force, who, log_message, old_id, NULL); + + git_signature_free(who); + return error; +} + +int git_reference_create( + git_reference **ref_out, + git_repository *repo, + const char *name, + const git_oid *id, + int force, + const char *log_message) +{ + return git_reference_create_matching(ref_out, repo, name, id, force, NULL, log_message); +} + +int git_reference_symbolic_create_matching( + git_reference **ref_out, + git_repository *repo, + const char *name, + const char *target, + int force, + const char *old_target, + const char *log_message) +{ + int error; + git_signature *who = NULL; + + GIT_ASSERT_ARG(target); + + if ((error = git_reference__log_signature(&who, repo)) < 0) + return error; + + error = reference__create( + ref_out, repo, name, NULL, target, force, who, log_message, NULL, old_target); + + git_signature_free(who); + return error; +} + +int git_reference_symbolic_create( + git_reference **ref_out, + git_repository *repo, + const char *name, + const char *target, + int force, + const char *log_message) +{ + return git_reference_symbolic_create_matching(ref_out, repo, name, target, force, NULL, log_message); +} + +static int ensure_is_an_updatable_direct_reference(git_reference *ref) +{ + if (ref->type == GIT_REFERENCE_DIRECT) + return 0; + + git_error_set(GIT_ERROR_REFERENCE, "cannot set OID on symbolic reference"); + return -1; +} + +int git_reference_set_target( + git_reference **out, + git_reference *ref, + const git_oid *id, + const char *log_message) +{ + int error; + git_repository *repo; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(ref); + GIT_ASSERT_ARG(id); + + repo = ref->db->repo; + + if ((error = ensure_is_an_updatable_direct_reference(ref)) < 0) + return error; + + return git_reference_create_matching(out, repo, ref->name, id, 1, &ref->target.oid, log_message); +} + +static int ensure_is_an_updatable_symbolic_reference(git_reference *ref) +{ + if (ref->type == GIT_REFERENCE_SYMBOLIC) + return 0; + + git_error_set(GIT_ERROR_REFERENCE, "cannot set symbolic target on a direct reference"); + return -1; +} + +int git_reference_symbolic_set_target( + git_reference **out, + git_reference *ref, + const char *target, + const char *log_message) +{ + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(ref); + GIT_ASSERT_ARG(target); + + if ((error = ensure_is_an_updatable_symbolic_reference(ref)) < 0) + return error; + + return git_reference_symbolic_create_matching( + out, ref->db->repo, ref->name, target, 1, ref->target.symbolic, log_message); +} + +typedef struct { + const char *old_name; + git_refname_t new_name; +} refs_update_head_payload; + +static int refs_update_head(git_repository *worktree, void *_payload) +{ + refs_update_head_payload *payload = (refs_update_head_payload *)_payload; + git_reference *head = NULL, *updated = NULL; + int error; + + if ((error = git_reference_lookup(&head, worktree, GIT_HEAD_FILE)) < 0) + goto out; + + if (git_reference_type(head) != GIT_REFERENCE_SYMBOLIC || + git__strcmp(git_reference_symbolic_target(head), payload->old_name) != 0) + goto out; + + /* Update HEAD if it was pointing to the reference being renamed */ + if ((error = git_reference_symbolic_set_target(&updated, head, payload->new_name, NULL)) < 0) { + git_error_set(GIT_ERROR_REFERENCE, "failed to update HEAD after renaming reference"); + goto out; + } + +out: + git_reference_free(updated); + git_reference_free(head); + return error; +} + +int git_reference_rename( + git_reference **out, + git_reference *ref, + const char *new_name, + int force, + const char *log_message) +{ + refs_update_head_payload payload; + git_signature *signature = NULL; + git_repository *repo; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(ref); + + repo = git_reference_owner(ref); + + if ((error = git_reference__log_signature(&signature, repo)) < 0 || + (error = reference_normalize_for_repo(payload.new_name, repo, new_name, true)) < 0 || + (error = git_refdb_rename(out, ref->db, ref->name, payload.new_name, force, signature, log_message)) < 0) + goto out; + + payload.old_name = ref->name; + + /* We may have to update any HEAD that was pointing to the renamed reference. */ + if ((error = git_repository_foreach_worktree(repo, refs_update_head, &payload)) < 0) + goto out; + +out: + git_signature_free(signature); + return error; +} + +int git_reference_resolve(git_reference **ref_out, const git_reference *ref) +{ + switch (git_reference_type(ref)) { + case GIT_REFERENCE_DIRECT: + return git_reference_lookup(ref_out, ref->db->repo, ref->name); + + case GIT_REFERENCE_SYMBOLIC: + return git_reference_lookup_resolved(ref_out, ref->db->repo, ref->target.symbolic, -1); + + default: + git_error_set(GIT_ERROR_REFERENCE, "invalid reference"); + return -1; + } +} + +int git_reference_foreach( + git_repository *repo, + git_reference_foreach_cb callback, + void *payload) +{ + git_reference_iterator *iter; + git_reference *ref; + int error; + + if ((error = git_reference_iterator_new(&iter, repo)) < 0) + return error; + + while (!(error = git_reference_next(&ref, iter))) { + if ((error = callback(ref, payload)) != 0) { + git_error_set_after_callback(error); + break; + } + } + + if (error == GIT_ITEROVER) + error = 0; + + git_reference_iterator_free(iter); + return error; +} + +int git_reference_foreach_name( + git_repository *repo, + git_reference_foreach_name_cb callback, + void *payload) +{ + git_reference_iterator *iter; + const char *refname; + int error; + + if ((error = git_reference_iterator_new(&iter, repo)) < 0) + return error; + + while (!(error = git_reference_next_name(&refname, iter))) { + if ((error = callback(refname, payload)) != 0) { + git_error_set_after_callback(error); + break; + } + } + + if (error == GIT_ITEROVER) + error = 0; + + git_reference_iterator_free(iter); + return error; +} + +int git_reference_foreach_glob( + git_repository *repo, + const char *glob, + git_reference_foreach_name_cb callback, + void *payload) +{ + git_reference_iterator *iter; + const char *refname; + int error; + + if ((error = git_reference_iterator_glob_new(&iter, repo, glob)) < 0) + return error; + + while (!(error = git_reference_next_name(&refname, iter))) { + if ((error = callback(refname, payload)) != 0) { + git_error_set_after_callback(error); + break; + } + } + + if (error == GIT_ITEROVER) + error = 0; + + git_reference_iterator_free(iter); + return error; +} + +int git_reference_iterator_new(git_reference_iterator **out, git_repository *repo) +{ + git_refdb *refdb; + + if (git_repository_refdb__weakptr(&refdb, repo) < 0) + return -1; + + return git_refdb_iterator(out, refdb, NULL); +} + +int git_reference_iterator_glob_new( + git_reference_iterator **out, git_repository *repo, const char *glob) +{ + git_refdb *refdb; + + if (git_repository_refdb__weakptr(&refdb, repo) < 0) + return -1; + + return git_refdb_iterator(out, refdb, glob); +} + +int git_reference_next(git_reference **out, git_reference_iterator *iter) +{ + return git_refdb_iterator_next(out, iter); +} + +int git_reference_next_name(const char **out, git_reference_iterator *iter) +{ + return git_refdb_iterator_next_name(out, iter); +} + +void git_reference_iterator_free(git_reference_iterator *iter) +{ + if (iter == NULL) + return; + + git_refdb_iterator_free(iter); +} + +static int cb__reflist_add(const char *ref, void *data) +{ + char *name = git__strdup(ref); + GIT_ERROR_CHECK_ALLOC(name); + return git_vector_insert((git_vector *)data, name); +} + +int git_reference_list( + git_strarray *array, + git_repository *repo) +{ + git_vector ref_list; + + GIT_ASSERT_ARG(array); + GIT_ASSERT_ARG(repo); + + array->strings = NULL; + array->count = 0; + + if (git_vector_init(&ref_list, 8, NULL) < 0) + return -1; + + if (git_reference_foreach_name( + repo, &cb__reflist_add, (void *)&ref_list) < 0) { + git_vector_dispose(&ref_list); + return -1; + } + + array->strings = (char **)git_vector_detach(&array->count, NULL, &ref_list); + + return 0; +} + +static int is_valid_ref_char(char ch) +{ + if ((unsigned) ch <= ' ' || ch == '\177') /* ASCII control characters */ + return 0; + + switch (ch) { + case '~': + case '^': + case ':': + case '\\': + case '?': + case '[': + return 0; + default: + return 1; + } +} + +static int ensure_segment_validity(const char *name, char may_contain_glob, bool allow_caret_prefix) +{ + const char *current = name; + const char *start = current; + char prev = '\0'; + const int lock_len = (int)strlen(GIT_FILELOCK_EXTENSION); + int segment_len; + + if (*current == '.') + return -1; /* Refname starts with "." */ + if (allow_caret_prefix && *current == '^') + start++; + + for (current = start; ; current++) { + if (*current == '\0' || *current == '/') + break; + + if (!is_valid_ref_char(*current)) + return -1; /* Illegal character in refname */ + + if (prev == '.' && *current == '.') + return -1; /* Refname contains ".." */ + + if (prev == '@' && *current == '{') + return -1; /* Refname contains "@{" */ + + if (*current == '*') { + if (!may_contain_glob) + return -1; + may_contain_glob = 0; + } + + prev = *current; + } + + segment_len = (int)(current - name); + + /* A refname component can not end with ".lock" */ + if (segment_len >= lock_len && + !memcmp(current - lock_len, GIT_FILELOCK_EXTENSION, lock_len)) + return -1; + + return segment_len; +} + +static bool is_valid_normalized_name(const char *name, size_t len) +{ + size_t i; + char c; + + GIT_ASSERT_ARG(name); + GIT_ASSERT_ARG(len > 0); + + for (i = 0; i < len; i++) + { + c = name[i]; + if (i == 0 && c == '^') + continue; /* The first character is allowed to be "^" for negative refspecs */ + + if ((c < 'A' || c > 'Z') && c != '_') + return false; + } + + if (*name == '_' || name[len - 1] == '_') + return false; + + return true; +} + +/* Inspired from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/refs.c#L36-100 */ +int git_reference__normalize_name( + git_str *buf, + const char *name, + unsigned int flags) +{ + const char *current; + int segment_len, segments_count = 0, error = GIT_EINVALIDSPEC; + unsigned int process_flags; + bool normalize = (buf != NULL); + bool allow_caret_prefix = true; + bool validate = (flags & GIT_REFERENCE_FORMAT__VALIDATION_DISABLE) == 0; + +#ifdef GIT_I18N_ICONV + git_fs_path_iconv_t ic = GIT_PATH_ICONV_INIT; +#endif + + GIT_ASSERT_ARG(name); + + process_flags = flags; + current = (char *)name; + + if (validate && *current == '/') + goto cleanup; + + if (normalize) + git_str_clear(buf); + +#ifdef GIT_I18N_ICONV + if ((flags & GIT_REFERENCE_FORMAT__PRECOMPOSE_UNICODE) != 0) { + size_t namelen = strlen(current); + if ((error = git_fs_path_iconv_init_precompose(&ic)) < 0 || + (error = git_fs_path_iconv(&ic, ¤t, &namelen)) < 0) + goto cleanup; + error = GIT_EINVALIDSPEC; + } +#endif + + if (!validate) { + git_str_sets(buf, current); + + error = git_str_oom(buf) ? -1 : 0; + goto cleanup; + } + + while (true) { + char may_contain_glob = process_flags & GIT_REFERENCE_FORMAT_REFSPEC_PATTERN; + + segment_len = ensure_segment_validity(current, may_contain_glob, allow_caret_prefix); + if (segment_len < 0) + goto cleanup; + + if (segment_len > 0) { + /* + * There may only be one glob in a pattern, thus we reset + * the pattern-flag in case the current segment has one. + */ + if (memchr(current, '*', segment_len)) + process_flags &= ~GIT_REFERENCE_FORMAT_REFSPEC_PATTERN; + + if (normalize) { + size_t cur_len = git_str_len(buf); + + git_str_joinpath(buf, git_str_cstr(buf), current); + git_str_truncate(buf, + cur_len + segment_len + (segments_count ? 1 : 0)); + + if (git_str_oom(buf)) { + error = -1; + goto cleanup; + } + } + + segments_count++; + } + + /* No empty segment is allowed when not normalizing */ + if (segment_len == 0 && !normalize) + goto cleanup; + + if (current[segment_len] == '\0') + break; + + current += segment_len + 1; + + /* + * A caret prefix is only allowed in the first segment to signify a + * negative refspec. + */ + allow_caret_prefix = false; + } + + /* A refname can not be empty */ + if (segment_len == 0 && segments_count == 0) + goto cleanup; + + /* A refname can not end with "." */ + if (current[segment_len - 1] == '.') + goto cleanup; + + /* A refname can not end with "/" */ + if (current[segment_len - 1] == '/') + goto cleanup; + + if ((segments_count == 1 ) && !(flags & GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL)) + goto cleanup; + + if ((segments_count == 1 ) && + !(flags & GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND) && + !(is_valid_normalized_name(name, (size_t)segment_len) || + ((flags & GIT_REFERENCE_FORMAT_REFSPEC_PATTERN) && !strcmp("*", name)))) + goto cleanup; + + if ((segments_count > 1) + && !(flags & GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND) + && (is_valid_normalized_name(name, strchr(name, '/') - name))) + goto cleanup; + + error = 0; + +cleanup: + if (error == GIT_EINVALIDSPEC) + git_error_set( + GIT_ERROR_REFERENCE, + "the given reference name '%s' is not valid", name); + + if (error && normalize) + git_str_dispose(buf); + +#ifdef GIT_I18N_ICONV + git_fs_path_iconv_clear(&ic); +#endif + + return error; +} + +int git_reference_normalize_name( + char *buffer_out, + size_t buffer_size, + const char *name, + unsigned int flags) +{ + git_str buf = GIT_STR_INIT; + int error; + + if ((error = git_reference__normalize_name(&buf, name, flags)) < 0) + goto cleanup; + + if (git_str_len(&buf) > buffer_size - 1) { + git_error_set( + GIT_ERROR_REFERENCE, + "the provided buffer is too short to hold the normalization of '%s'", name); + error = GIT_EBUFS; + goto cleanup; + } + + if ((error = git_str_copy_cstr(buffer_out, buffer_size, &buf)) < 0) + goto cleanup; + + error = 0; + +cleanup: + git_str_dispose(&buf); + return error; +} + +#define GIT_REFERENCE_TYPEMASK (GIT_REFERENCE_DIRECT | GIT_REFERENCE_SYMBOLIC) + +int git_reference_cmp( + const git_reference *ref1, + const git_reference *ref2) +{ + git_reference_t type1, type2; + int ret; + + GIT_ASSERT_ARG(ref1); + GIT_ASSERT_ARG(ref2); + + if ((ret = strcmp(ref1->name, ref2->name)) != 0) + return ret; + + type1 = git_reference_type(ref1); + type2 = git_reference_type(ref2); + + /* let's put symbolic refs before OIDs */ + if (type1 != type2) + return (type1 == GIT_REFERENCE_SYMBOLIC) ? -1 : 1; + + if (type1 == GIT_REFERENCE_SYMBOLIC) + return strcmp(ref1->target.symbolic, ref2->target.symbolic); + + return git_oid__cmp(&ref1->target.oid, &ref2->target.oid); +} + +int git_reference__cmp_cb(const void *a, const void *b) +{ + return git_reference_cmp( + (const git_reference *)a, (const git_reference *)b); +} + +/* + * Starting with the reference given by `ref_name`, follows symbolic + * references until a direct reference is found and updated the OID + * on that direct reference to `oid`. + */ +int git_reference__update_terminal( + git_repository *repo, + const char *ref_name, + const git_oid *oid, + const git_signature *sig, + const char *log_message) +{ + git_reference *ref = NULL, *ref2 = NULL; + git_signature *who = NULL; + git_refdb *refdb = NULL; + const git_signature *to_use; + int error = 0; + + if (!sig && (error = git_reference__log_signature(&who, repo)) < 0) + goto out; + + to_use = sig ? sig : who; + + if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0) + goto out; + + if ((error = git_refdb_resolve(&ref, refdb, ref_name, -1)) < 0) { + if (error == GIT_ENOTFOUND) { + git_error_clear(); + error = reference__create(&ref2, repo, ref_name, oid, NULL, 0, to_use, + log_message, NULL, NULL); + } + goto out; + } + + /* In case the resolved reference is symbolic, then it's a dangling symref. */ + if (git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC) { + error = reference__create(&ref2, repo, ref->target.symbolic, oid, NULL, 0, to_use, + log_message, NULL, NULL); + } else { + error = reference__create(&ref2, repo, ref->name, oid, NULL, 1, to_use, + log_message, &ref->target.oid, NULL); + } + +out: + git_reference_free(ref2); + git_reference_free(ref); + git_signature_free(who); + return error; +} + +static const char *commit_type(const git_commit *commit) +{ + unsigned int count = git_commit_parentcount(commit); + + if (count >= 2) + return " (merge)"; + else if (count == 0) + return " (initial)"; + else + return ""; +} + +int git_reference__update_for_commit( + git_repository *repo, + git_reference *ref, + const char *ref_name, + const git_oid *id, + const char *operation) +{ + git_reference *ref_new = NULL; + git_commit *commit = NULL; + git_str reflog_msg = GIT_STR_INIT; + const git_signature *who; + int error; + + if ((error = git_commit_lookup(&commit, repo, id)) < 0 || + (error = git_str_printf(&reflog_msg, "%s%s: %s", + operation ? operation : "commit", + commit_type(commit), + git_commit_summary(commit))) < 0) + goto done; + + who = git_commit_committer(commit); + + if (ref) { + if ((error = ensure_is_an_updatable_direct_reference(ref)) < 0) + return error; + + error = reference__create(&ref_new, repo, ref->name, id, NULL, 1, who, + git_str_cstr(&reflog_msg), &ref->target.oid, NULL); + } + else + error = git_reference__update_terminal( + repo, ref_name, id, who, git_str_cstr(&reflog_msg)); + +done: + git_reference_free(ref_new); + git_str_dispose(&reflog_msg); + git_commit_free(commit); + return error; +} + +int git_reference_has_log(git_repository *repo, const char *refname) +{ + int error; + git_refdb *refdb; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(refname); + + if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0) + return error; + + return git_refdb_has_log(refdb, refname); +} + +int git_reference_ensure_log(git_repository *repo, const char *refname) +{ + int error; + git_refdb *refdb; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(refname); + + if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0) + return error; + + return git_refdb_ensure_log(refdb, refname); +} + +int git_reference__is_branch(const char *ref_name) +{ + return git__prefixcmp(ref_name, GIT_REFS_HEADS_DIR) == 0; +} + +int git_reference_is_branch(const git_reference *ref) +{ + GIT_ASSERT_ARG(ref); + return git_reference__is_branch(ref->name); +} + +int git_reference__is_remote(const char *ref_name) +{ + return git__prefixcmp(ref_name, GIT_REFS_REMOTES_DIR) == 0; +} + +int git_reference_is_remote(const git_reference *ref) +{ + GIT_ASSERT_ARG(ref); + return git_reference__is_remote(ref->name); +} + +int git_reference__is_tag(const char *ref_name) +{ + return git__prefixcmp(ref_name, GIT_REFS_TAGS_DIR) == 0; +} + +int git_reference_is_tag(const git_reference *ref) +{ + GIT_ASSERT_ARG(ref); + return git_reference__is_tag(ref->name); +} + +int git_reference__is_note(const char *ref_name) +{ + return git__prefixcmp(ref_name, GIT_REFS_NOTES_DIR) == 0; +} + +int git_reference_is_note(const git_reference *ref) +{ + GIT_ASSERT_ARG(ref); + return git_reference__is_note(ref->name); +} + +static int peel_error(int error, const git_reference *ref, const char *msg) +{ + git_error_set( + GIT_ERROR_INVALID, + "the reference '%s' cannot be peeled - %s", git_reference_name(ref), msg); + return error; +} + +int git_reference_peel( + git_object **peeled, + const git_reference *ref, + git_object_t target_type) +{ + const git_reference *resolved = NULL; + git_reference *allocated = NULL; + git_object *target = NULL; + int error; + + GIT_ASSERT_ARG(ref); + + if (ref->type == GIT_REFERENCE_DIRECT) { + resolved = ref; + } else { + if ((error = git_reference_resolve(&allocated, ref)) < 0) + return peel_error(error, ref, "Cannot resolve reference"); + + resolved = allocated; + } + + /* + * If we try to peel an object to a tag, we cannot use + * the fully peeled object, as that will always resolve + * to a commit. So we only want to use the peeled value + * if it is not zero and the target is not a tag. + */ + if (target_type != GIT_OBJECT_TAG && !git_oid_is_zero(&resolved->peel)) { + error = git_object_lookup(&target, + git_reference_owner(ref), &resolved->peel, GIT_OBJECT_ANY); + } else { + error = git_object_lookup(&target, + git_reference_owner(ref), &resolved->target.oid, GIT_OBJECT_ANY); + } + + if (error < 0) { + peel_error(error, ref, "Cannot retrieve reference target"); + goto cleanup; + } + + if (target_type == GIT_OBJECT_ANY && git_object_type(target) != GIT_OBJECT_TAG) + error = git_object_dup(peeled, target); + else + error = git_object_peel(peeled, target, target_type); + +cleanup: + git_object_free(target); + git_reference_free(allocated); + + return error; +} + +int git_reference__name_is_valid( + int *valid, + const char *refname, + unsigned int flags) +{ + int error; + + GIT_ASSERT(valid && refname); + + *valid = 0; + + error = git_reference__normalize_name(NULL, refname, flags); + + if (!error) + *valid = 1; + else if (error == GIT_EINVALIDSPEC) + error = 0; + + return error; +} + +int git_reference_name_is_valid(int *valid, const char *refname) +{ + return git_reference__name_is_valid(valid, refname, GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL); +} + +const char *git_reference__shorthand(const char *name) +{ + if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR)) + return name + strlen(GIT_REFS_HEADS_DIR); + else if (!git__prefixcmp(name, GIT_REFS_TAGS_DIR)) + return name + strlen(GIT_REFS_TAGS_DIR); + else if (!git__prefixcmp(name, GIT_REFS_REMOTES_DIR)) + return name + strlen(GIT_REFS_REMOTES_DIR); + else if (!git__prefixcmp(name, GIT_REFS_DIR)) + return name + strlen(GIT_REFS_DIR); + + /* No shorthands are available, so just return the name. */ + return name; +} + +const char *git_reference_shorthand(const git_reference *ref) +{ + return git_reference__shorthand(ref->name); +} + +int git_reference__is_unborn_head(bool *unborn, const git_reference *ref, git_repository *repo) +{ + int error; + git_reference *tmp_ref; + + GIT_ASSERT_ARG(unborn); + GIT_ASSERT_ARG(ref); + GIT_ASSERT_ARG(repo); + + if (ref->type == GIT_REFERENCE_DIRECT) { + *unborn = 0; + return 0; + } + + error = git_reference_lookup_resolved(&tmp_ref, repo, ref->name, -1); + git_reference_free(tmp_ref); + + if (error != 0 && error != GIT_ENOTFOUND) + return error; + else if (error == GIT_ENOTFOUND && git__strcmp(ref->name, GIT_HEAD_FILE) == 0) + *unborn = true; + else + *unborn = false; + + return 0; +} + +/* Deprecated functions */ + +#ifndef GIT_DEPRECATE_HARD + +int git_reference_is_valid_name(const char *refname) +{ + int valid = 0; + + git_reference__name_is_valid(&valid, refname, GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL); + + return valid; +} + +#endif diff --git a/src/libgit2/refs.h b/src/libgit2/refs.h new file mode 100644 index 00000000000..a06965b60d8 --- /dev/null +++ b/src/libgit2/refs.h @@ -0,0 +1,135 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_refs_h__ +#define INCLUDE_refs_h__ + +#include "common.h" + +#include "git2/oid.h" +#include "git2/refs.h" +#include "git2/refdb.h" +#include "str.h" +#include "oid.h" + +extern bool git_reference__enable_symbolic_ref_target_validation; + +#define GIT_REFS_DIR "refs/" +#define GIT_REFS_HEADS_DIR GIT_REFS_DIR "heads/" +#define GIT_REFS_TAGS_DIR GIT_REFS_DIR "tags/" +#define GIT_REFS_REMOTES_DIR GIT_REFS_DIR "remotes/" +#define GIT_REFS_NOTES_DIR GIT_REFS_DIR "notes/" +#define GIT_REFS_DIR_MODE 0777 +#define GIT_REFS_FILE_MODE 0666 + +#define GIT_RENAMED_REF_FILE GIT_REFS_DIR "RENAMED-REF" + +#define GIT_SYMREF "ref: " +#define GIT_PACKEDREFS_FILE "packed-refs" +#define GIT_PACKEDREFS_HEADER "# pack-refs with: peeled fully-peeled sorted " +#define GIT_PACKEDREFS_FILE_MODE 0666 + +#define GIT_HEAD_FILE "HEAD" +#define GIT_ORIG_HEAD_FILE "ORIG_HEAD" +#define GIT_FETCH_HEAD_FILE "FETCH_HEAD" +#define GIT_MERGE_HEAD_FILE "MERGE_HEAD" +#define GIT_REVERT_HEAD_FILE "REVERT_HEAD" +#define GIT_CHERRYPICK_HEAD_FILE "CHERRY_PICK_HEAD" +#define GIT_BISECT_LOG_FILE "BISECT_LOG" +#define GIT_REBASE_MERGE_DIR "rebase-merge/" +#define GIT_REBASE_MERGE_INTERACTIVE_FILE GIT_REBASE_MERGE_DIR "interactive" +#define GIT_REBASE_APPLY_DIR "rebase-apply/" +#define GIT_REBASE_APPLY_REBASING_FILE GIT_REBASE_APPLY_DIR "rebasing" +#define GIT_REBASE_APPLY_APPLYING_FILE GIT_REBASE_APPLY_DIR "applying" + +#define GIT_SEQUENCER_DIR "sequencer/" +#define GIT_SEQUENCER_HEAD_FILE GIT_SEQUENCER_DIR "head" +#define GIT_SEQUENCER_OPTIONS_FILE GIT_SEQUENCER_DIR "options" +#define GIT_SEQUENCER_TODO_FILE GIT_SEQUENCER_DIR "todo" + +#define GIT_STASH_FILE "stash" +#define GIT_REFS_STASH_FILE GIT_REFS_DIR GIT_STASH_FILE + +#define GIT_REFERENCE_FORMAT__PRECOMPOSE_UNICODE (1u << 16) +#define GIT_REFERENCE_FORMAT__VALIDATION_DISABLE (1u << 15) + +#define GIT_REFNAME_MAX 1024 + +typedef char git_refname_t[GIT_REFNAME_MAX]; + +struct git_reference { + git_refdb *db; + git_reference_t type; + + union { + git_oid oid; + char *symbolic; + } target; + + git_oid peel; + char name[GIT_FLEX_ARRAY]; +}; + +/** + * Reallocate the reference with a new name + * + * Note that this is a dangerous operation, as on success, all existing + * pointers to the old reference will now be dangling. Only call this on objects + * you control, possibly using `git_reference_dup`. + */ +git_reference *git_reference__realloc(git_reference **ptr_to_ref, const char *name); + +int git_reference__normalize_name(git_str *buf, const char *name, unsigned int flags); +int git_reference__update_terminal(git_repository *repo, const char *ref_name, const git_oid *oid, const git_signature *sig, const char *log_message); +int git_reference__name_is_valid(int *valid, const char *name, unsigned int flags); +int git_reference__is_branch(const char *ref_name); +int git_reference__is_remote(const char *ref_name); +int git_reference__is_tag(const char *ref_name); +int git_reference__is_note(const char *ref_name); +const char *git_reference__shorthand(const char *name); + +/* + * A `git_reference_cmp` wrapper suitable for passing to generic + * comparators, like `vector_cmp` / `tsort` / etc. + */ +int git_reference__cmp_cb(const void *a, const void *b); + +/** + * Lookup a reference by name and try to resolve to an OID. + * + * You can control how many dereferences this will attempt to resolve the + * reference with the `max_deref` parameter, or pass -1 to use a sane + * default. If you pass 0 for `max_deref`, this will not attempt to resolve + * the reference. For any value of `max_deref` other than 0, not + * successfully resolving the reference will be reported as an error. + + * The generated reference must be freed by the user. + * + * @param reference_out Pointer to the looked-up reference + * @param repo The repository to look up the reference + * @param name The long name for the reference (e.g. HEAD, ref/heads/master, refs/tags/v0.1.0, ...) + * @param max_deref Maximum number of dereferences to make of symbolic refs, 0 means simple lookup, < 0 means use default reasonable value + * @return 0 on success or < 0 on error; not being able to resolve the reference is an error unless 0 was passed for max_deref + */ +int git_reference_lookup_resolved( + git_reference **reference_out, + git_repository *repo, + const char *name, + int max_deref); + +int git_reference__log_signature(git_signature **out, git_repository *repo); + +/** Update a reference after a commit. */ +int git_reference__update_for_commit( + git_repository *repo, + git_reference *ref, + const char *ref_name, + const git_oid *id, + const char *operation); + +int git_reference__is_unborn_head(bool *unborn, const git_reference *ref, git_repository *repo); + +#endif diff --git a/src/libgit2/refspec.c b/src/libgit2/refspec.c new file mode 100644 index 00000000000..7ce8ef4ba71 --- /dev/null +++ b/src/libgit2/refspec.c @@ -0,0 +1,447 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "refspec.h" + +#include "buf.h" +#include "refs.h" +#include "util.h" +#include "vector.h" +#include "wildmatch.h" + +int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch) +{ + /* Ported from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/remote.c#L518-636 */ + + size_t llen; + int is_glob = 0; + const char *lhs, *rhs; + int valid = 0; + unsigned int flags; + bool is_neg_refspec = false; + + GIT_ASSERT_ARG(refspec); + GIT_ASSERT_ARG(input); + + memset(refspec, 0x0, sizeof(git_refspec)); + refspec->push = !is_fetch; + + lhs = input; + if (*lhs == '+') { + refspec->force = 1; + lhs++; + } + if (*lhs == '^') { + is_neg_refspec = true; + } + + rhs = strrchr(lhs, ':'); + + /* + * Before going on, special case ":" (or "+:") as a refspec + * for matching refs. + */ + if (!is_fetch && rhs == lhs && rhs[1] == '\0') { + refspec->matching = 1; + refspec->string = git__strdup(input); + GIT_ERROR_CHECK_ALLOC(refspec->string); + refspec->src = git__strdup(""); + GIT_ERROR_CHECK_ALLOC(refspec->src); + refspec->dst = git__strdup(""); + GIT_ERROR_CHECK_ALLOC(refspec->dst); + return 0; + } + + if (rhs) { + size_t rlen = strlen(++rhs); + if (rlen || !is_fetch) { + is_glob = (1 <= rlen && strchr(rhs, '*')); + refspec->dst = git__strndup(rhs, rlen); + } + } + + llen = (rhs ? (size_t)(rhs - lhs - 1) : strlen(lhs)); + if (1 <= llen && memchr(lhs, '*', llen)) { + /* + * If the lefthand side contains a glob, then one of the following must be + * true, otherwise the spec is invalid + * 1) the rhs exists and also contains a glob + * 2) it is a negative refspec (i.e. no rhs) + * 3) the rhs doesn't exist and we're fetching + */ + if ((rhs && !is_glob) || (rhs && is_neg_refspec) || (!rhs && is_fetch && !is_neg_refspec)) + goto invalid; + is_glob = 1; + } else if (rhs && is_glob) + goto invalid; + + refspec->pattern = is_glob; + refspec->src = git__strndup(lhs, llen); + flags = GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL | + GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND | + (is_glob ? GIT_REFERENCE_FORMAT_REFSPEC_PATTERN : 0); + + if (is_fetch) { + /* + * LHS + * - empty is allowed; it means HEAD. + * - otherwise it must be a valid looking ref. + */ + if (!*refspec->src) + ; /* empty is ok */ + else if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0) + goto on_error; + else if (!valid) + goto invalid; + + /* + * RHS + * - missing is ok, and is same as empty. + * - empty is ok; it means not to store. + * - otherwise it must be a valid looking ref. + */ + if (!refspec->dst) + ; /* ok */ + else if (!*refspec->dst) + ; /* ok */ + else if (git_reference__name_is_valid(&valid, refspec->dst, flags) < 0) + goto on_error; + else if (!valid) + goto invalid; + } else { + /* + * LHS + * - empty is allowed; it means delete. + * - when wildcarded, it must be a valid looking ref. + * - otherwise, it must be an extended SHA-1, but + * there is no existing way to validate this. + */ + if (!*refspec->src) + ; /* empty is ok */ + else if (is_glob) { + if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0) + goto on_error; + else if (!valid) + goto invalid; + } + else { + ; /* anything goes, for now */ + } + + /* + * RHS + * - missing is allowed, but LHS then must be a + * valid looking ref. + * - empty is not allowed. + * - otherwise it must be a valid looking ref. + */ + if (!refspec->dst) { + if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0) + goto on_error; + else if (!valid) + goto invalid; + } else if (!*refspec->dst) { + goto invalid; + } else { + if (git_reference__name_is_valid(&valid, refspec->dst, flags) < 0) + goto on_error; + else if (!valid) + goto invalid; + } + + /* if the RHS is empty, then it's a copy of the LHS */ + if (!refspec->dst) { + refspec->dst = git__strdup(refspec->src); + GIT_ERROR_CHECK_ALLOC(refspec->dst); + } + } + + refspec->string = git__strdup(input); + GIT_ERROR_CHECK_ALLOC(refspec->string); + + return 0; + +invalid: + git_error_set(GIT_ERROR_INVALID, + "'%s' is not a valid refspec.", input); + git_refspec__dispose(refspec); + return GIT_EINVALIDSPEC; + +on_error: + git_refspec__dispose(refspec); + return -1; +} + +void git_refspec__dispose(git_refspec *refspec) +{ + if (refspec == NULL) + return; + + git__free(refspec->src); + git__free(refspec->dst); + git__free(refspec->string); + + memset(refspec, 0x0, sizeof(git_refspec)); +} + +int git_refspec_parse(git_refspec **out_refspec, const char *input, int is_fetch) +{ + git_refspec *refspec; + GIT_ASSERT_ARG(out_refspec); + GIT_ASSERT_ARG(input); + + *out_refspec = NULL; + + refspec = git__malloc(sizeof(git_refspec)); + GIT_ERROR_CHECK_ALLOC(refspec); + + if (git_refspec__parse(refspec, input, !!is_fetch) != 0) { + git__free(refspec); + return -1; + } + + *out_refspec = refspec; + return 0; +} + +void git_refspec_free(git_refspec *refspec) +{ + git_refspec__dispose(refspec); + git__free(refspec); +} + +const char *git_refspec_src(const git_refspec *refspec) +{ + return refspec == NULL ? NULL : refspec->src; +} + +const char *git_refspec_dst(const git_refspec *refspec) +{ + return refspec == NULL ? NULL : refspec->dst; +} + +const char *git_refspec_string(const git_refspec *refspec) +{ + return refspec == NULL ? NULL : refspec->string; +} + +int git_refspec_force(const git_refspec *refspec) +{ + GIT_ASSERT_ARG(refspec); + + return refspec->force; +} + +int git_refspec_src_matches_negative(const git_refspec *refspec, const char *refname) +{ + if (refspec == NULL || refspec->src == NULL || !git_refspec_is_negative(refspec)) + return false; + + return (wildmatch(refspec->src + 1, refname, 0) == 0); +} + +int git_refspec_src_matches(const git_refspec *refspec, const char *refname) +{ + if (refspec == NULL || refspec->src == NULL) + return false; + + return (wildmatch(refspec->src, refname, 0) == 0); +} + +int git_refspec_dst_matches(const git_refspec *refspec, const char *refname) +{ + if (refspec == NULL || refspec->dst == NULL) + return false; + + return (wildmatch(refspec->dst, refname, 0) == 0); +} + +static int refspec_transform( + git_str *out, const char *from, const char *to, const char *name) +{ + const char *from_star, *to_star; + size_t replacement_len, star_offset; + + git_str_clear(out); + + /* + * There are two parts to each side of a refspec, the bit + * before the star and the bit after it. The star can be in + * the middle of the pattern, so we need to look at each bit + * individually. + */ + from_star = strchr(from, '*'); + to_star = strchr(to, '*'); + + GIT_ASSERT(from_star && to_star); + + /* star offset, both in 'from' and in 'name' */ + star_offset = from_star - from; + + /* the first half is copied over */ + git_str_put(out, to, to_star - to); + + /* + * Copy over the name, but exclude the trailing part in "from" starting + * after the glob + */ + replacement_len = strlen(name + star_offset) - strlen(from_star + 1); + git_str_put(out, name + star_offset, replacement_len); + + return git_str_puts(out, to_star + 1); +} + +int git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name) +{ + GIT_BUF_WRAP_PRIVATE(out, git_refspec__transform, spec, name); +} + +int git_refspec__transform(git_str *out, const git_refspec *spec, const char *name) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(spec); + GIT_ASSERT_ARG(name); + + if (!git_refspec_src_matches(spec, name)) { + git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the source", name); + return -1; + } + + if (!spec->pattern) + return git_str_puts(out, spec->dst ? spec->dst : ""); + + return refspec_transform(out, spec->src, spec->dst, name); +} + +int git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name) +{ + GIT_BUF_WRAP_PRIVATE(out, git_refspec__rtransform, spec, name); +} + +int git_refspec__rtransform(git_str *out, const git_refspec *spec, const char *name) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(spec); + GIT_ASSERT_ARG(name); + + if (!git_refspec_dst_matches(spec, name)) { + git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the destination", name); + return -1; + } + + if (!spec->pattern) + return git_str_puts(out, spec->src); + + return refspec_transform(out, spec->dst, spec->src, name); +} + +int git_refspec__serialize(git_str *out, const git_refspec *refspec) +{ + if (refspec->force) + git_str_putc(out, '+'); + + git_str_printf(out, "%s:%s", + refspec->src != NULL ? refspec->src : "", + refspec->dst != NULL ? refspec->dst : ""); + + return git_str_oom(out) == false; +} + +int git_refspec_is_wildcard(const git_refspec *spec) +{ + GIT_ASSERT_ARG(spec); + GIT_ASSERT_ARG(spec->src); + + return (spec->src[strlen(spec->src) - 1] == '*'); +} + +int git_refspec_is_negative(const git_refspec *spec) +{ + GIT_ASSERT_ARG(spec); + GIT_ASSERT_ARG(spec->src); + + return (spec->src[0] == '^' && spec->dst == NULL); +} + +git_direction git_refspec_direction(const git_refspec *spec) +{ + GIT_ASSERT_ARG(spec); + + return spec->push; +} + +int git_refspec__dwim_one(git_vector *out, git_refspec *spec, git_vector *refs) +{ + git_str buf = GIT_STR_INIT; + size_t j, pos; + git_remote_head key; + git_refspec *cur; + + const char *formatters[] = { + GIT_REFS_DIR "%s", + GIT_REFS_TAGS_DIR "%s", + GIT_REFS_HEADS_DIR "%s", + NULL + }; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(spec); + GIT_ASSERT_ARG(refs); + + cur = git__calloc(1, sizeof(git_refspec)); + GIT_ERROR_CHECK_ALLOC(cur); + + cur->force = spec->force; + cur->push = spec->push; + cur->pattern = spec->pattern; + cur->matching = spec->matching; + cur->string = git__strdup(spec->string); + + /* shorthand on the lhs */ + if (git__prefixcmp(spec->src, GIT_REFS_DIR)) { + for (j = 0; formatters[j]; j++) { + git_str_clear(&buf); + git_str_printf(&buf, formatters[j], spec->src); + GIT_ERROR_CHECK_ALLOC_STR(&buf); + + key.name = (char *) git_str_cstr(&buf); + if (!git_vector_search(&pos, refs, &key)) { + /* we found something to match the shorthand, set src to that */ + cur->src = git_str_detach(&buf); + } + } + } + + /* No shorthands found, copy over the name */ + if (cur->src == NULL && spec->src != NULL) { + cur->src = git__strdup(spec->src); + GIT_ERROR_CHECK_ALLOC(cur->src); + } + + if (spec->dst && git__prefixcmp(spec->dst, GIT_REFS_DIR)) { + /* if it starts with "remotes" then we just prepend "refs/" */ + if (!git__prefixcmp(spec->dst, "remotes/")) { + git_str_puts(&buf, GIT_REFS_DIR); + } else { + git_str_puts(&buf, GIT_REFS_HEADS_DIR); + } + + git_str_puts(&buf, spec->dst); + GIT_ERROR_CHECK_ALLOC_STR(&buf); + + cur->dst = git_str_detach(&buf); + } + + git_str_dispose(&buf); + + if (cur->dst == NULL && spec->dst != NULL) { + cur->dst = git__strdup(spec->dst); + GIT_ERROR_CHECK_ALLOC(cur->dst); + } + + return git_vector_insert(out, cur); +} diff --git a/src/libgit2/refspec.h b/src/libgit2/refspec.h new file mode 100644 index 00000000000..37612216c04 --- /dev/null +++ b/src/libgit2/refspec.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_refspec_h__ +#define INCLUDE_refspec_h__ + +#include "common.h" + +#include "git2/refspec.h" +#include "str.h" +#include "vector.h" + +struct git_refspec { + char *string; + char *src; + char *dst; + unsigned int force :1, + push : 1, + pattern :1, + matching :1; +}; + +#define GIT_REFSPEC_TAGS "refs/tags/*:refs/tags/*" + +int git_refspec__transform(git_str *out, const git_refspec *spec, const char *name); +int git_refspec__rtransform(git_str *out, const git_refspec *spec, const char *name); + +int git_refspec__parse( + struct git_refspec *refspec, + const char *str, + bool is_fetch); + +void git_refspec__dispose(git_refspec *refspec); + +int git_refspec__serialize(git_str *out, const git_refspec *refspec); + +/** + * Determines if a refspec is a wildcard refspec. + * + * @param spec the refspec + * @return 1 if the refspec is a wildcard, 0 otherwise + */ +int git_refspec_is_wildcard(const git_refspec *spec); + +/** + * Determines if a refspec is a negative refspec. + * + * @param spec the refspec + * @return 1 if the refspec is a negative, 0 otherwise + */ +int git_refspec_is_negative(const git_refspec *spec); + +/** + * DWIM `spec` with `refs` existing on the remote, append the dwim'ed + * result in `out`. + */ +int git_refspec__dwim_one(git_vector *out, git_refspec *spec, git_vector *refs); + +#endif diff --git a/src/libgit2/remote.c b/src/libgit2/remote.c new file mode 100644 index 00000000000..0b674c5ef2c --- /dev/null +++ b/src/libgit2/remote.c @@ -0,0 +1,3157 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "remote.h" + +#include "buf.h" +#include "branch.h" +#include "config.h" +#include "repository.h" +#include "fetch.h" +#include "refs.h" +#include "refspec.h" +#include "fetchhead.h" +#include "push.h" +#include "proxy.h" +#include "strarray.h" + +#include "git2/config.h" +#include "git2/types.h" +#include "git2/oid.h" +#include "git2/net.h" +#include "transports/smart.h" + +#define CONFIG_URL_FMT "remote.%s.url" +#define CONFIG_PUSHURL_FMT "remote.%s.pushurl" +#define CONFIG_FETCH_FMT "remote.%s.fetch" +#define CONFIG_PUSH_FMT "remote.%s.push" +#define CONFIG_TAGOPT_FMT "remote.%s.tagopt" + +static int dwim_refspecs(git_vector *out, git_vector *refspecs, git_vector *refs); +static int lookup_remote_prune_config(git_remote *remote, git_config *config, const char *name); +static int apply_insteadof(char **out, git_config *config, const char *url, int direction, bool use_default_if_empty); + +static int add_refspec_to(git_vector *vector, const char *string, bool is_fetch) +{ + git_refspec *spec; + + spec = git__calloc(1, sizeof(git_refspec)); + GIT_ERROR_CHECK_ALLOC(spec); + + if (git_refspec__parse(spec, string, is_fetch) < 0) { + git__free(spec); + return -1; + } + + spec->push = !is_fetch; + if (git_vector_insert(vector, spec) < 0) { + git_refspec__dispose(spec); + git__free(spec); + return -1; + } + + return 0; +} + +static int add_refspec(git_remote *remote, const char *string, bool is_fetch) +{ + return add_refspec_to(&remote->refspecs, string, is_fetch); +} + +static int download_tags_value(git_remote *remote, git_config *cfg) +{ + git_config_entry *ce; + git_str buf = GIT_STR_INIT; + int error; + + if (git_str_printf(&buf, "remote.%s.tagopt", remote->name) < 0) + return -1; + + error = git_config__lookup_entry(&ce, cfg, git_str_cstr(&buf), false); + git_str_dispose(&buf); + + if (!error && ce && ce->value) { + if (!strcmp(ce->value, "--no-tags")) + remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_NONE; + else if (!strcmp(ce->value, "--tags")) + remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL; + } + + git_config_entry_free(ce); + return error; +} + +static int ensure_remote_name_is_valid(const char *name) +{ + int valid, error; + + error = git_remote_name_is_valid(&valid, name); + + if (!error && !valid) { + git_error_set( + GIT_ERROR_CONFIG, + "'%s' is not a valid remote name.", name ? name : "(null)"); + error = GIT_EINVALIDSPEC; + } + + return error; +} + +static int write_add_refspec(git_repository *repo, const char *name, const char *refspec, bool fetch) +{ + git_config *cfg; + git_str var = GIT_STR_INIT; + git_refspec spec; + const char *fmt; + int error; + + if ((error = git_repository_config__weakptr(&cfg, repo)) < 0) + return error; + + fmt = fetch ? CONFIG_FETCH_FMT : CONFIG_PUSH_FMT; + + if ((error = ensure_remote_name_is_valid(name)) < 0) + return error; + + if ((error = git_refspec__parse(&spec, refspec, fetch)) < 0) + return error; + + git_refspec__dispose(&spec); + + if ((error = git_str_printf(&var, fmt, name)) < 0) + return error; + + /* + * "$^" is an unmatchable regexp: it will not match anything at all, so + * all values will be considered new and we will not replace any + * present value. + */ + if ((error = git_config_set_multivar(cfg, var.ptr, "$^", refspec)) < 0) { + goto cleanup; + } + +cleanup: + git_str_dispose(&var); + return 0; +} + +static int canonicalize_url(git_str *out, const char *in) +{ + if (in == NULL || strlen(in) == 0) { + git_error_set(GIT_ERROR_INVALID, "cannot set empty URL"); + return GIT_EINVALIDSPEC; + } + +#ifdef GIT_WIN32 + /* Given a UNC path like \\server\path, we need to convert this + * to //server/path for compatibility with core git. + */ + if (in[0] == '\\' && in[1] == '\\' && + (git__isalpha(in[2]) || git__isdigit(in[2]))) { + const char *c; + for (c = in; *c; c++) + git_str_putc(out, *c == '\\' ? '/' : *c); + + return git_str_oom(out) ? -1 : 0; + } +#endif + + return git_str_puts(out, in); +} + +static int default_fetchspec_for_name(git_str *buf, const char *name) +{ + if (git_str_printf(buf, "+refs/heads/*:refs/remotes/%s/*", name) < 0) + return -1; + + return 0; +} + +static int ensure_remote_doesnot_exist(git_repository *repo, const char *name) +{ + int error; + git_remote *remote; + + error = git_remote_lookup(&remote, repo, name); + + if (error == GIT_ENOTFOUND) + return 0; + + if (error < 0) + return error; + + git_remote_free(remote); + + git_error_set(GIT_ERROR_CONFIG, "remote '%s' already exists", name); + + return GIT_EEXISTS; +} + +int git_remote_create_options_init(git_remote_create_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_remote_create_options, GIT_REMOTE_CREATE_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_remote_create_init_options(git_remote_create_options *opts, unsigned int version) +{ + return git_remote_create_options_init(opts, version); +} +#endif + +int git_remote_create_with_opts(git_remote **out, const char *url, const git_remote_create_options *opts) +{ + git_remote *remote = NULL; + git_config *config_ro = NULL, *config_rw; + git_str canonical_url = GIT_STR_INIT; + git_str var = GIT_STR_INIT; + git_str specbuf = GIT_STR_INIT; + const git_remote_create_options dummy_opts = GIT_REMOTE_CREATE_OPTIONS_INIT; + int error = -1; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(url); + + if (!opts) { + opts = &dummy_opts; + } + + GIT_ERROR_CHECK_VERSION(opts, GIT_REMOTE_CREATE_OPTIONS_VERSION, "git_remote_create_options"); + + if (opts->name != NULL) { + if ((error = ensure_remote_name_is_valid(opts->name)) < 0) + return error; + + if (opts->repository && + (error = ensure_remote_doesnot_exist(opts->repository, opts->name)) < 0) + return error; + } + + if (opts->repository) { + if ((error = git_repository_config_snapshot(&config_ro, opts->repository)) < 0) + goto on_error; + } + + remote = git__calloc(1, sizeof(git_remote)); + GIT_ERROR_CHECK_ALLOC(remote); + + remote->repo = opts->repository; + + if ((error = git_vector_init(&remote->refs, 8, NULL)) < 0 || + (error = canonicalize_url(&canonical_url, url)) < 0) + goto on_error; + + if (opts->repository && !(opts->flags & GIT_REMOTE_CREATE_SKIP_INSTEADOF)) { + if ((error = apply_insteadof(&remote->url, config_ro, canonical_url.ptr, GIT_DIRECTION_FETCH, true)) < 0 || + (error = apply_insteadof(&remote->pushurl, config_ro, canonical_url.ptr, GIT_DIRECTION_PUSH, false)) < 0) + goto on_error; + } else { + remote->url = git__strdup(canonical_url.ptr); + GIT_ERROR_CHECK_ALLOC(remote->url); + } + + if (opts->name != NULL) { + remote->name = git__strdup(opts->name); + GIT_ERROR_CHECK_ALLOC(remote->name); + + if (opts->repository && + ((error = git_str_printf(&var, CONFIG_URL_FMT, opts->name)) < 0 || + (error = git_repository_config__weakptr(&config_rw, opts->repository)) < 0 || + (error = git_config_set_string(config_rw, var.ptr, canonical_url.ptr)) < 0)) + goto on_error; + } + + if (opts->fetchspec != NULL || + (opts->name && !(opts->flags & GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC))) { + const char *fetch = NULL; + if (opts->fetchspec) { + fetch = opts->fetchspec; + } else { + if ((error = default_fetchspec_for_name(&specbuf, opts->name)) < 0) + goto on_error; + + fetch = git_str_cstr(&specbuf); + } + + if ((error = add_refspec(remote, fetch, true)) < 0) + goto on_error; + + /* only write for named remotes with a repository */ + if (opts->repository && opts->name && + ((error = write_add_refspec(opts->repository, opts->name, fetch, true)) < 0 || + (error = lookup_remote_prune_config(remote, config_ro, opts->name)) < 0)) + goto on_error; + + /* Move the data over to where the matching functions can find them */ + if ((error = dwim_refspecs(&remote->active_refspecs, &remote->refspecs, &remote->refs)) < 0) + goto on_error; + } + + /* A remote without a name doesn't download tags */ + if (!opts->name) + remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_NONE; + else + remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO; + + + git_str_dispose(&var); + + *out = remote; + error = 0; + +on_error: + if (error) + git_remote_free(remote); + + git_config_free(config_ro); + git_str_dispose(&specbuf); + git_str_dispose(&canonical_url); + git_str_dispose(&var); + return error; +} + +int git_remote_create(git_remote **out, git_repository *repo, const char *name, const char *url) +{ + git_str buf = GIT_STR_INIT; + int error; + git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT; + + /* Those 2 tests are duplicated here because of backward-compatibility */ + if ((error = ensure_remote_name_is_valid(name)) < 0) + return error; + + if (canonicalize_url(&buf, url) < 0) + return GIT_ERROR; + + git_str_clear(&buf); + + opts.repository = repo; + opts.name = name; + + error = git_remote_create_with_opts(out, url, &opts); + + git_str_dispose(&buf); + + return error; +} + +int git_remote_create_with_fetchspec(git_remote **out, git_repository *repo, const char *name, const char *url, const char *fetch) +{ + int error; + git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT; + + if ((error = ensure_remote_name_is_valid(name)) < 0) + return error; + + opts.repository = repo; + opts.name = name; + opts.fetchspec = fetch; + opts.flags = GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC; + + return git_remote_create_with_opts(out, url, &opts); +} + +int git_remote_create_anonymous(git_remote **out, git_repository *repo, const char *url) +{ + git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT; + + opts.repository = repo; + + return git_remote_create_with_opts(out, url, &opts); +} + +int git_remote_create_detached(git_remote **out, const char *url) +{ + return git_remote_create_with_opts(out, url, NULL); +} + +int git_remote_dup(git_remote **dest, git_remote *source) +{ + size_t i; + int error = 0; + git_refspec *spec; + git_remote *remote = git__calloc(1, sizeof(git_remote)); + GIT_ERROR_CHECK_ALLOC(remote); + + if (source->name != NULL) { + remote->name = git__strdup(source->name); + GIT_ERROR_CHECK_ALLOC(remote->name); + } + + if (source->url != NULL) { + remote->url = git__strdup(source->url); + GIT_ERROR_CHECK_ALLOC(remote->url); + } + + if (source->pushurl != NULL) { + remote->pushurl = git__strdup(source->pushurl); + GIT_ERROR_CHECK_ALLOC(remote->pushurl); + } + + remote->repo = source->repo; + remote->download_tags = source->download_tags; + remote->prune_refs = source->prune_refs; + + if (git_vector_init(&remote->refs, 32, NULL) < 0 || + git_vector_init(&remote->refspecs, 2, NULL) < 0 || + git_vector_init(&remote->active_refspecs, 2, NULL) < 0) { + error = -1; + goto cleanup; + } + + git_vector_foreach(&source->refspecs, i, spec) { + if ((error = add_refspec(remote, spec->string, !spec->push)) < 0) + goto cleanup; + } + + *dest = remote; + +cleanup: + + if (error < 0) + git__free(remote); + + return error; +} + +struct refspec_cb_data { + git_remote *remote; + int fetch; +}; + +static int refspec_cb(const git_config_entry *entry, void *payload) +{ + struct refspec_cb_data *data = (struct refspec_cb_data *)payload; + return add_refspec(data->remote, entry->value, data->fetch); +} + +static int get_optional_config( + bool *found, git_config *config, git_str *buf, + git_config_foreach_cb cb, void *payload) +{ + int error = 0; + const char *key = git_str_cstr(buf); + + if (git_str_oom(buf)) + return -1; + + if (cb != NULL) + error = git_config_get_multivar_foreach(config, key, NULL, cb, payload); + else + error = git_config_get_string(payload, config, key); + + if (found) + *found = !error; + + if (error == GIT_ENOTFOUND) { + git_error_clear(); + error = 0; + } + + return error; +} + +int git_remote_lookup(git_remote **out, git_repository *repo, const char *name) +{ + git_remote *remote = NULL; + git_str buf = GIT_STR_INIT; + const char *val; + int error = 0; + git_config *config; + struct refspec_cb_data data = { NULL }; + bool optional_setting_found = false, found; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + if ((error = ensure_remote_name_is_valid(name)) < 0) + return error; + + if ((error = git_repository_config_snapshot(&config, repo)) < 0) + return error; + + remote = git__calloc(1, sizeof(git_remote)); + GIT_ERROR_CHECK_ALLOC(remote); + + remote->name = git__strdup(name); + GIT_ERROR_CHECK_ALLOC(remote->name); + + if (git_vector_init(&remote->refs, 32, NULL) < 0 || + git_vector_init(&remote->refspecs, 2, NULL) < 0 || + git_vector_init(&remote->passive_refspecs, 2, NULL) < 0 || + git_vector_init(&remote->active_refspecs, 2, NULL) < 0) { + error = -1; + goto cleanup; + } + + if ((error = git_str_printf(&buf, "remote.%s.url", name)) < 0) + goto cleanup; + + if ((error = get_optional_config(&found, config, &buf, NULL, (void *)&val)) < 0) + goto cleanup; + + optional_setting_found |= found; + + remote->repo = repo; + remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO; + + if (found && strlen(val) > 0) { + if ((error = apply_insteadof(&remote->url, config, val, GIT_DIRECTION_FETCH, true)) < 0 || + (error = apply_insteadof(&remote->pushurl, config, val, GIT_DIRECTION_PUSH, false)) < 0) + goto cleanup; + } + + val = NULL; + git_str_clear(&buf); + git_str_printf(&buf, "remote.%s.pushurl", name); + + if ((error = get_optional_config(&found, config, &buf, NULL, (void *)&val)) < 0) + goto cleanup; + + optional_setting_found |= found; + + if (!optional_setting_found) { + error = GIT_ENOTFOUND; + git_error_set(GIT_ERROR_CONFIG, "remote '%s' does not exist", name); + goto cleanup; + } + + if (found && strlen(val) > 0) { + if (remote->pushurl) + git__free(remote->pushurl); + + if ((error = apply_insteadof(&remote->pushurl, config, val, GIT_DIRECTION_FETCH, true)) < 0) + goto cleanup; + } + + data.remote = remote; + data.fetch = true; + + git_str_clear(&buf); + git_str_printf(&buf, "remote.%s.fetch", name); + + if ((error = get_optional_config(NULL, config, &buf, refspec_cb, &data)) < 0) + goto cleanup; + + data.fetch = false; + git_str_clear(&buf); + git_str_printf(&buf, "remote.%s.push", name); + + if ((error = get_optional_config(NULL, config, &buf, refspec_cb, &data)) < 0) + goto cleanup; + + if ((error = download_tags_value(remote, config)) < 0) + goto cleanup; + + if ((error = lookup_remote_prune_config(remote, config, name)) < 0) + goto cleanup; + + /* Move the data over to where the matching functions can find them */ + if ((error = dwim_refspecs(&remote->active_refspecs, &remote->refspecs, &remote->refs)) < 0) + goto cleanup; + + *out = remote; + +cleanup: + git_config_free(config); + git_str_dispose(&buf); + + if (error < 0) + git_remote_free(remote); + + return error; +} + +static int lookup_remote_prune_config(git_remote *remote, git_config *config, const char *name) +{ + git_str buf = GIT_STR_INIT; + int error = 0; + + git_str_printf(&buf, "remote.%s.prune", name); + + if ((error = git_config_get_bool(&remote->prune_refs, config, git_str_cstr(&buf))) < 0) { + if (error == GIT_ENOTFOUND) { + git_error_clear(); + + if ((error = git_config_get_bool(&remote->prune_refs, config, "fetch.prune")) < 0) { + if (error == GIT_ENOTFOUND) { + git_error_clear(); + error = 0; + } + } + } + } + + git_str_dispose(&buf); + return error; +} + +const char *git_remote_name(const git_remote *remote) +{ + GIT_ASSERT_ARG_WITH_RETVAL(remote, NULL); + return remote->name; +} + +git_repository *git_remote_owner(const git_remote *remote) +{ + GIT_ASSERT_ARG_WITH_RETVAL(remote, NULL); + return remote->repo; +} + +const char *git_remote_url(const git_remote *remote) +{ + GIT_ASSERT_ARG_WITH_RETVAL(remote, NULL); + return remote->url; +} + +int git_remote_set_instance_url(git_remote *remote, const char *url) +{ + char *tmp; + + GIT_ASSERT_ARG(remote); + GIT_ASSERT_ARG(url); + + if ((tmp = git__strdup(url)) == NULL) + return -1; + + git__free(remote->url); + remote->url = tmp; + + return 0; +} + +static int set_url(git_repository *repo, const char *remote, const char *pattern, const char *url) +{ + git_config *cfg; + git_str buf = GIT_STR_INIT, canonical_url = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(remote); + + if ((error = ensure_remote_name_is_valid(remote)) < 0) + return error; + + if ((error = git_repository_config__weakptr(&cfg, repo)) < 0) + return error; + + if ((error = git_str_printf(&buf, pattern, remote)) < 0) + return error; + + if (url) { + if ((error = canonicalize_url(&canonical_url, url)) < 0) + goto cleanup; + + error = git_config_set_string(cfg, buf.ptr, url); + } else { + error = git_config_delete_entry(cfg, buf.ptr); + } + +cleanup: + git_str_dispose(&canonical_url); + git_str_dispose(&buf); + + return error; +} + +int git_remote_set_url(git_repository *repo, const char *remote, const char *url) +{ + return set_url(repo, remote, CONFIG_URL_FMT, url); +} + +const char *git_remote_pushurl(const git_remote *remote) +{ + GIT_ASSERT_ARG_WITH_RETVAL(remote, NULL); + return remote->pushurl; +} + +int git_remote_set_instance_pushurl(git_remote *remote, const char *url) +{ + char *tmp; + + GIT_ASSERT_ARG(remote); + GIT_ASSERT_ARG(url); + + if ((tmp = git__strdup(url)) == NULL) + return -1; + + git__free(remote->pushurl); + remote->pushurl = tmp; + + return 0; +} + +int git_remote_set_pushurl(git_repository *repo, const char *remote, const char *url) +{ + return set_url(repo, remote, CONFIG_PUSHURL_FMT, url); +} + +static int resolve_url( + git_str *resolved_url, + const char *url, + int direction, + const git_remote_callbacks *callbacks) +{ +#ifdef GIT_DEPRECATE_HARD + GIT_UNUSED(direction); + GIT_UNUSED(callbacks); +#else + git_buf buf = GIT_BUF_INIT; + int error; + + if (callbacks && callbacks->resolve_url) { + error = callbacks->resolve_url(&buf, url, direction, callbacks->payload); + + if (error != GIT_PASSTHROUGH) { + git_error_set_after_callback_function(error, "git_resolve_url_cb"); + + git_str_set(resolved_url, buf.ptr, buf.size); + git_buf_dispose(&buf); + + return error; + } + } +#endif + + return git_str_sets(resolved_url, url); +} + +int git_remote__urlfordirection( + git_str *url_out, + struct git_remote *remote, + int direction, + const git_remote_callbacks *callbacks) +{ + const char *url = NULL; + + GIT_ASSERT_ARG(remote); + GIT_ASSERT_ARG(direction == GIT_DIRECTION_FETCH || direction == GIT_DIRECTION_PUSH); + + if (callbacks && callbacks->remote_ready) { + int status = callbacks->remote_ready(remote, direction, callbacks->payload); + + if (status != 0 && status != GIT_PASSTHROUGH) { + git_error_set_after_callback_function(status, "git_remote_ready_cb"); + return status; + } + } + + if (direction == GIT_DIRECTION_FETCH) + url = remote->url; + else if (direction == GIT_DIRECTION_PUSH) + url = remote->pushurl ? remote->pushurl : remote->url; + + if (!url) { + git_error_set(GIT_ERROR_INVALID, + "malformed remote '%s' - missing %s URL", + remote->name ? remote->name : "(anonymous)", + direction == GIT_DIRECTION_FETCH ? "fetch" : "push"); + return GIT_EINVALID; + } + + return resolve_url(url_out, url, direction, callbacks); +} + +int git_remote_connect_options_init( + git_remote_connect_options *opts, + unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_remote_connect_options, GIT_REMOTE_CONNECT_OPTIONS_INIT); + return 0; +} + +int git_remote_connect_options_dup( + git_remote_connect_options *dst, + const git_remote_connect_options *src) +{ + memcpy(dst, src, sizeof(git_remote_connect_options)); + + if (git_proxy_options_dup(&dst->proxy_opts, &src->proxy_opts) < 0 || + git_strarray_copy(&dst->custom_headers, &src->custom_headers) < 0) + return -1; + + return 0; +} + +void git_remote_connect_options_dispose(git_remote_connect_options *opts) +{ + if (!opts) + return; + + git_strarray_dispose(&opts->custom_headers); + git_proxy_options_dispose(&opts->proxy_opts); +} + +static size_t http_header_name_length(const char *http_header) +{ + const char *colon = strchr(http_header, ':'); + if (!colon) + return 0; + return colon - http_header; +} + +static bool is_malformed_http_header(const char *http_header) +{ + const char *c; + size_t name_len; + + /* Disallow \r and \n */ + if ((c = strchr(http_header, '\r')) != NULL) + return true; + if ((c = strchr(http_header, '\n')) != NULL) + return true; + + /* Require a header name followed by : */ + if ((name_len = http_header_name_length(http_header)) < 1) + return true; + + return false; +} + +static char *forbidden_custom_headers[] = { + "User-Agent", + "Host", + "Accept", + "Content-Type", + "Transfer-Encoding", + "Content-Length", +}; + +static bool is_forbidden_custom_header(const char *custom_header) +{ + unsigned long i; + size_t name_len = http_header_name_length(custom_header); + + /* Disallow headers that we set */ + for (i = 0; i < ARRAY_SIZE(forbidden_custom_headers); i++) + if (strncmp(forbidden_custom_headers[i], custom_header, name_len) == 0) + return true; + + return false; +} + +static int validate_custom_headers(const git_strarray *custom_headers) +{ + size_t i; + + if (!custom_headers) + return 0; + + for (i = 0; i < custom_headers->count; i++) { + if (is_malformed_http_header(custom_headers->strings[i])) { + git_error_set(GIT_ERROR_INVALID, "custom HTTP header '%s' is malformed", custom_headers->strings[i]); + return -1; + } + + if (is_forbidden_custom_header(custom_headers->strings[i])) { + git_error_set(GIT_ERROR_INVALID, "custom HTTP header '%s' is already set by libgit2", custom_headers->strings[i]); + return -1; + } + } + + return 0; +} + +static int lookup_redirect_config( + git_remote_redirect_t *out, + git_repository *repo) +{ + git_config *config; + const char *value; + int bool_value, error = 0; + + if (!repo) { + *out = GIT_REMOTE_REDIRECT_INITIAL; + return 0; + } + + if ((error = git_repository_config_snapshot(&config, repo)) < 0) + goto done; + + if ((error = git_config_get_string(&value, config, "http.followRedirects")) < 0) { + if (error == GIT_ENOTFOUND) { + *out = GIT_REMOTE_REDIRECT_INITIAL; + error = 0; + } + + goto done; + } + + if (git_config_parse_bool(&bool_value, value) == 0) { + *out = bool_value ? GIT_REMOTE_REDIRECT_ALL : + GIT_REMOTE_REDIRECT_NONE; + } else if (strcasecmp(value, "initial") == 0) { + *out = GIT_REMOTE_REDIRECT_INITIAL; + } else { + git_error_set(GIT_ERROR_CONFIG, "invalid configuration setting '%s' for 'http.followRedirects'", value); + error = -1; + } + +done: + git_config_free(config); + return error; +} + +int git_remote_connect_options_normalize( + git_remote_connect_options *dst, + git_repository *repo, + const git_remote_connect_options *src) +{ + git_remote_connect_options_dispose(dst); + git_remote_connect_options_init(dst, GIT_REMOTE_CONNECT_OPTIONS_VERSION); + + if (src) { + GIT_ERROR_CHECK_VERSION(src, GIT_REMOTE_CONNECT_OPTIONS_VERSION, "git_remote_connect_options"); + GIT_ERROR_CHECK_VERSION(&src->callbacks, GIT_REMOTE_CALLBACKS_VERSION, "git_remote_callbacks"); + GIT_ERROR_CHECK_VERSION(&src->proxy_opts, GIT_PROXY_OPTIONS_VERSION, "git_proxy_options"); + + if (validate_custom_headers(&src->custom_headers) < 0 || + git_remote_connect_options_dup(dst, src) < 0) + return -1; + } + + if (dst->follow_redirects == 0) { + if (lookup_redirect_config(&dst->follow_redirects, repo) < 0) + return -1; + } + + return 0; +} + +int git_remote_connect_ext( + git_remote *remote, + git_direction direction, + const git_remote_connect_options *given_opts) +{ + git_remote_connect_options opts = GIT_REMOTE_CONNECT_OPTIONS_INIT; + git_str url = GIT_STR_INIT; + git_transport *t; + int error; + + GIT_ASSERT_ARG(remote); + + if (given_opts) + memcpy(&opts, given_opts, sizeof(git_remote_connect_options)); + + GIT_ERROR_CHECK_VERSION(&opts.callbacks, GIT_REMOTE_CALLBACKS_VERSION, "git_remote_callbacks"); + GIT_ERROR_CHECK_VERSION(&opts.proxy_opts, GIT_PROXY_OPTIONS_VERSION, "git_proxy_options"); + + t = remote->transport; + + if ((error = git_remote__urlfordirection(&url, remote, direction, &opts.callbacks)) < 0) + goto on_error; + + /* If we don't have a transport object yet, and the caller specified a + * custom transport factory, use that */ + if (!t && opts.callbacks.transport && + (error = opts.callbacks.transport(&t, remote, opts.callbacks.payload)) < 0) + goto on_error; + + /* If we still don't have a transport, then use the global + * transport registrations which map URI schemes to transport factories */ + if (!t && (error = git_transport_new(&t, remote, url.ptr)) < 0) + goto on_error; + + if ((error = t->connect(t, url.ptr, direction, &opts)) != 0) + goto on_error; + + remote->transport = t; + + git_str_dispose(&url); + + return 0; + +on_error: + if (t) + t->free(t); + + git_str_dispose(&url); + + if (t == remote->transport) + remote->transport = NULL; + + return error; +} + +int git_remote_connect( + git_remote *remote, + git_direction direction, + const git_remote_callbacks *callbacks, + const git_proxy_options *proxy, + const git_strarray *custom_headers) +{ + git_remote_connect_options opts = GIT_REMOTE_CONNECT_OPTIONS_INIT; + + if (callbacks) + memcpy(&opts.callbacks, callbacks, sizeof(git_remote_callbacks)); + + if (proxy) + memcpy(&opts.proxy_opts, proxy, sizeof(git_proxy_options)); + + if (custom_headers) + memcpy(&opts.custom_headers, custom_headers, sizeof(git_strarray)); + + return git_remote_connect_ext(remote, direction, &opts); +} + +int git_remote_ls(const git_remote_head ***out, size_t *size, git_remote *remote) +{ + GIT_ASSERT_ARG(remote); + + if (!remote->transport) { + git_error_set(GIT_ERROR_NET, "this remote has never connected"); + return -1; + } + + return remote->transport->ls(out, size, remote->transport); +} + +int git_remote_capabilities(unsigned int *out, git_remote *remote) +{ + GIT_ASSERT_ARG(remote); + + *out = 0; + + if (!remote->transport) { + git_error_set(GIT_ERROR_NET, "this remote has never connected"); + return -1; + } + + return remote->transport->capabilities(out, remote->transport); +} + +int git_remote_oid_type(git_oid_t *out, git_remote *remote) +{ + GIT_ASSERT_ARG(remote); + + if (!remote->transport) { + git_error_set(GIT_ERROR_NET, "this remote has never connected"); + *out = 0; + return -1; + } + +#ifdef GIT_EXPERIMENTAL_SHA256 + return remote->transport->oid_type(out, remote->transport); +#else + *out = GIT_OID_SHA1; + return 0; +#endif +} + +static int lookup_config(char **out, git_config *cfg, const char *name) +{ + git_config_entry *ce = NULL; + int error; + + if ((error = git_config__lookup_entry(&ce, cfg, name, false)) < 0) + return error; + + if (ce && ce->value) { + *out = git__strdup(ce->value); + GIT_ERROR_CHECK_ALLOC(*out); + } else { + error = GIT_ENOTFOUND; + } + + git_config_entry_free(ce); + return error; +} + +static void url_config_trim(git_net_url *url) +{ + size_t len = strlen(url->path); + + if (url->path[len - 1] == '/') { + len--; + } else { + while (len && url->path[len - 1] != '/') + len--; + } + + url->path[len] = '\0'; +} + +static int http_proxy_config(char **out, git_remote *remote, git_net_url *url) +{ + git_config *cfg = NULL; + git_str buf = GIT_STR_INIT; + git_net_url lookup_url = GIT_NET_URL_INIT; + int error; + + if ((error = git_net_url_dup(&lookup_url, url)) < 0) + goto done; + + if (remote->repo) { + if ((error = git_repository_config(&cfg, remote->repo)) < 0) + goto done; + } else { + if ((error = git_config_open_default(&cfg)) < 0) + goto done; + } + + /* remote..proxy config setting */ + if (remote->name && remote->name[0]) { + git_str_clear(&buf); + + if ((error = git_str_printf(&buf, "remote.%s.proxy", remote->name)) < 0 || + (error = lookup_config(out, cfg, buf.ptr)) != GIT_ENOTFOUND) + goto done; + } + + while (true) { + git_str_clear(&buf); + + if ((error = git_str_puts(&buf, "http.")) < 0 || + (error = git_net_url_fmt(&buf, &lookup_url)) < 0 || + (error = git_str_puts(&buf, ".proxy")) < 0 || + (error = lookup_config(out, cfg, buf.ptr)) != GIT_ENOTFOUND) + goto done; + + if (! lookup_url.path[0]) + break; + + url_config_trim(&lookup_url); + } + + git_str_clear(&buf); + + error = lookup_config(out, cfg, "http.proxy"); + +done: + git_config_free(cfg); + git_str_dispose(&buf); + git_net_url_dispose(&lookup_url); + return error; +} + +static int http_proxy_env(char **out, git_remote *remote, git_net_url *url) +{ + git_str proxy_env = GIT_STR_INIT, no_proxy_env = GIT_STR_INIT; + bool use_ssl = (strcmp(url->scheme, "https") == 0); + int error; + + GIT_UNUSED(remote); + + /* http_proxy / https_proxy environment variables */ + error = git__getenv(&proxy_env, use_ssl ? "https_proxy" : "http_proxy"); + + /* try uppercase environment variables */ + if (error == GIT_ENOTFOUND) + error = git__getenv(&proxy_env, use_ssl ? "HTTPS_PROXY" : "HTTP_PROXY"); + + if (error) + goto done; + + /* no_proxy/NO_PROXY environment variables */ + error = git__getenv(&no_proxy_env, "no_proxy"); + + if (error == GIT_ENOTFOUND) + error = git__getenv(&no_proxy_env, "NO_PROXY"); + + if (error && error != GIT_ENOTFOUND) + goto done; + + if (!git_net_url_matches_pattern_list(url, no_proxy_env.ptr)) + *out = git_str_detach(&proxy_env); + else + error = GIT_ENOTFOUND; + +done: + git_str_dispose(&proxy_env); + git_str_dispose(&no_proxy_env); + return error; +} + +int git_remote__http_proxy(char **out, git_remote *remote, git_net_url *url) +{ + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(remote); + + *out = NULL; + + /* + * Go through the possible sources for proxy configuration, + * Examine the various git config options first, then + * consult environment variables. + */ + if ((error = http_proxy_config(out, remote, url)) != GIT_ENOTFOUND || + (error = http_proxy_env(out, remote, url)) != GIT_ENOTFOUND) + return error; + + return 0; +} + +/* DWIM `refspecs` based on `refs` and append the output to `out` */ +static int dwim_refspecs(git_vector *out, git_vector *refspecs, git_vector *refs) +{ + size_t i; + git_refspec *spec; + + git_vector_foreach(refspecs, i, spec) { + if (git_refspec__dwim_one(out, spec, refs) < 0) + return -1; + } + + return 0; +} + +static void free_refspecs(git_vector *vec) +{ + size_t i; + git_refspec *spec; + + git_vector_foreach(vec, i, spec) { + git_refspec__dispose(spec); + git__free(spec); + } + + git_vector_clear(vec); +} + +static int remote_head_cmp(const void *_a, const void *_b) +{ + const git_remote_head *a = (git_remote_head *) _a; + const git_remote_head *b = (git_remote_head *) _b; + + return git__strcmp_cb(a->name, b->name); +} + +static int ls_to_vector(git_vector *out, git_remote *remote) +{ + git_remote_head **heads; + size_t heads_len, i; + + if (git_remote_ls((const git_remote_head ***)&heads, &heads_len, remote) < 0) + return -1; + + if (git_vector_init(out, heads_len, remote_head_cmp) < 0) + return -1; + + for (i = 0; i < heads_len; i++) { + if (git_vector_insert(out, heads[i]) < 0) + return -1; + } + + return 0; +} + +static int connect_or_reset_options( + git_remote *remote, + int direction, + git_remote_connect_options *opts) +{ + if (!git_remote_connected(remote)) { + return git_remote_connect_ext(remote, direction, opts); + } else { + return remote->transport->set_connect_opts(remote->transport, opts); + } +} + +/* Download from an already connected remote. */ +static int git_remote__download( + git_remote *remote, + const git_strarray *refspecs, + const git_fetch_options *opts) +{ + git_vector *to_active, specs = GIT_VECTOR_INIT, refs = GIT_VECTOR_INIT; + size_t i; + int error; + + if (ls_to_vector(&refs, remote) < 0) + return -1; + + if ((error = git_vector_init(&specs, 0, NULL)) < 0) + goto on_error; + + remote->passed_refspecs = 0; + if (!refspecs || !refspecs->count) { + to_active = &remote->refspecs; + } else { + for (i = 0; i < refspecs->count; i++) { + if ((error = add_refspec_to(&specs, refspecs->strings[i], true)) < 0) + goto on_error; + } + + to_active = &specs; + remote->passed_refspecs = 1; + } + + free_refspecs(&remote->passive_refspecs); + if ((error = dwim_refspecs(&remote->passive_refspecs, &remote->refspecs, &refs)) < 0) + goto on_error; + + free_refspecs(&remote->active_refspecs); + error = dwim_refspecs(&remote->active_refspecs, to_active, &refs); + + git_vector_dispose(&refs); + free_refspecs(&specs); + git_vector_dispose(&specs); + + if (error < 0) + goto on_error; + + if (remote->push) { + git_push_free(remote->push); + remote->push = NULL; + } + + if ((error = git_fetch_negotiate(remote, opts)) < 0) + goto on_error; + + error = git_fetch_download_pack(remote); + +on_error: + git_vector_dispose(&refs); + free_refspecs(&specs); + git_vector_dispose(&specs); + return error; +} + +int git_remote_download( + git_remote *remote, + const git_strarray *refspecs, + const git_fetch_options *opts) +{ + git_remote_connect_options connect_opts = GIT_REMOTE_CONNECT_OPTIONS_INIT; + int error; + + GIT_ASSERT_ARG(remote); + + if (!remote->repo) { + git_error_set(GIT_ERROR_INVALID, "cannot download detached remote"); + return -1; + } + + if (git_remote_connect_options__from_fetch_opts(&connect_opts, + remote, opts) < 0) + return -1; + + if ((error = connect_or_reset_options(remote, GIT_DIRECTION_FETCH, &connect_opts)) < 0) + return error; + + error = git_remote__download(remote, refspecs, opts); + + git_remote_connect_options_dispose(&connect_opts); + + return error; +} + +int git_remote_fetch( + git_remote *remote, + const git_strarray *refspecs, + const git_fetch_options *opts, + const char *reflog_message) +{ + git_remote_autotag_option_t tagopt = remote->download_tags; + bool prune = false; + git_str reflog_msg_buf = GIT_STR_INIT; + git_remote_connect_options connect_opts = GIT_REMOTE_CONNECT_OPTIONS_INIT; + unsigned int capabilities; + git_oid_t oid_type; + unsigned int update_flags = GIT_REMOTE_UPDATE_FETCHHEAD; + int error; + + GIT_ASSERT_ARG(remote); + + if (!remote->repo) { + git_error_set(GIT_ERROR_INVALID, "cannot download detached remote"); + return -1; + } + + if (git_remote_connect_options__from_fetch_opts(&connect_opts, + remote, opts) < 0) + return -1; + + if ((error = connect_or_reset_options(remote, GIT_DIRECTION_FETCH, &connect_opts)) < 0) + return error; + + if (opts) { + update_flags = opts->update_fetchhead; + tagopt = opts->download_tags; + } + + if ((error = git_remote_capabilities(&capabilities, remote)) < 0 || + (error = git_remote_oid_type(&oid_type, remote)) < 0) + return error; + + /* Connect and download everything */ + error = git_remote__download(remote, refspecs, opts); + + /* We don't need to be connected anymore */ + git_remote_disconnect(remote); + + /* If the download failed, return the error */ + if (error != 0) + goto done; + + /* Default reflog message */ + if (reflog_message) + git_str_sets(&reflog_msg_buf, reflog_message); + else { + git_str_printf(&reflog_msg_buf, "fetch %s", + remote->name ? remote->name : remote->url); + } + + /* Create "remote/foo" branches for all remote branches */ + error = git_remote_update_tips(remote, + &connect_opts.callbacks, + update_flags, + tagopt, + git_str_cstr(&reflog_msg_buf)); + + git_str_dispose(&reflog_msg_buf); + + if (error < 0) + goto done; + + if (opts && opts->prune == GIT_FETCH_PRUNE) + prune = true; + else if (opts && opts->prune == GIT_FETCH_PRUNE_UNSPECIFIED && remote->prune_refs) + prune = true; + else if (opts && opts->prune == GIT_FETCH_NO_PRUNE) + prune = false; + else + prune = remote->prune_refs; + + if (prune) + error = git_remote_prune(remote, &connect_opts.callbacks); + +done: + git_remote_connect_options_dispose(&connect_opts); + return error; +} + +static int remote_head_for_fetchspec_src(git_remote_head **out, git_vector *update_heads, const char *fetchspec_src) +{ + unsigned int i; + git_remote_head *remote_ref; + + GIT_ASSERT_ARG(update_heads); + GIT_ASSERT_ARG(fetchspec_src); + + *out = NULL; + + git_vector_foreach(update_heads, i, remote_ref) { + if (strcmp(remote_ref->name, fetchspec_src) == 0) { + *out = remote_ref; + break; + } + } + + return 0; +} + +static int ref_to_update(int *update, git_str *remote_name, git_remote *remote, git_refspec *spec, const char *ref_name) +{ + int error = 0; + git_repository *repo; + git_str upstream_remote = GIT_STR_INIT; + git_str upstream_name = GIT_STR_INIT; + + repo = git_remote_owner(remote); + + if ((!git_reference__is_branch(ref_name)) || + !git_remote_name(remote) || + (error = git_branch__upstream_remote(&upstream_remote, repo, ref_name) < 0) || + git__strcmp(git_remote_name(remote), git_str_cstr(&upstream_remote)) || + (error = git_branch__upstream_name(&upstream_name, repo, ref_name)) < 0 || + !git_refspec_dst_matches(spec, git_str_cstr(&upstream_name)) || + (error = git_refspec__rtransform(remote_name, spec, upstream_name.ptr)) < 0) { + /* Not an error if there is no upstream */ + if (error == GIT_ENOTFOUND) { + git_error_clear(); + error = 0; + } + + *update = 0; + } else { + *update = 1; + } + + git_str_dispose(&upstream_remote); + git_str_dispose(&upstream_name); + return error; +} + +static int remote_head_for_ref(git_remote_head **out, git_remote *remote, git_refspec *spec, git_vector *update_heads, git_reference *ref) +{ + git_reference *resolved_ref = NULL; + git_str remote_name = GIT_STR_INIT; + git_config *config = NULL; + const char *ref_name; + int error = 0, update; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(spec); + GIT_ASSERT_ARG(ref); + + *out = NULL; + + error = git_reference_resolve(&resolved_ref, ref); + + /* If we're in an unborn branch, let's pretend nothing happened */ + if (error == GIT_ENOTFOUND && git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC) { + ref_name = git_reference_symbolic_target(ref); + error = 0; + } else { + ref_name = git_reference_name(resolved_ref); + } + + /* + * The ref name may be unresolvable - perhaps it's pointing to + * something invalid. In this case, there is no remote head for + * this ref. + */ + if (!ref_name) { + error = 0; + goto cleanup; + } + + if ((error = ref_to_update(&update, &remote_name, remote, spec, ref_name)) < 0) + goto cleanup; + + if (update) + error = remote_head_for_fetchspec_src(out, update_heads, git_str_cstr(&remote_name)); + +cleanup: + git_str_dispose(&remote_name); + git_reference_free(resolved_ref); + git_config_free(config); + return error; +} + +static int git_remote_write_fetchhead(git_remote *remote, git_refspec *spec, git_vector *update_heads) +{ + git_reference *head_ref = NULL; + git_fetchhead_ref *fetchhead_ref; + git_remote_head *remote_ref, *merge_remote_ref; + git_vector fetchhead_refs; + bool include_all_fetchheads; + unsigned int i = 0; + int error = 0; + + GIT_ASSERT_ARG(remote); + + /* no heads, nothing to do */ + if (update_heads->length == 0) + return 0; + + if (git_vector_init(&fetchhead_refs, update_heads->length, git_fetchhead_ref_cmp) < 0) + return -1; + + /* Iff refspec is * (but not subdir slash star), include tags */ + include_all_fetchheads = (strcmp(GIT_REFS_HEADS_DIR "*", git_refspec_src(spec)) == 0); + + /* Determine what to merge: if refspec was a wildcard, just use HEAD */ + if (git_refspec_is_wildcard(spec)) { + if ((error = git_reference_lookup(&head_ref, remote->repo, GIT_HEAD_FILE)) < 0 || + (error = remote_head_for_ref(&merge_remote_ref, remote, spec, update_heads, head_ref)) < 0) + goto cleanup; + } else { + /* If we're fetching a single refspec, that's the only thing that should be in FETCH_HEAD. */ + if ((error = remote_head_for_fetchspec_src(&merge_remote_ref, update_heads, git_refspec_src(spec))) < 0) + goto cleanup; + } + + /* Create the FETCH_HEAD file */ + git_vector_foreach(update_heads, i, remote_ref) { + int merge_this_fetchhead = (merge_remote_ref == remote_ref); + + if (!include_all_fetchheads && + !git_refspec_src_matches(spec, remote_ref->name) && + !merge_this_fetchhead) + continue; + + if (git_fetchhead_ref_create(&fetchhead_ref, + &remote_ref->oid, + merge_this_fetchhead, + remote_ref->name, + git_remote_url(remote)) < 0) + goto cleanup; + + if (git_vector_insert(&fetchhead_refs, fetchhead_ref) < 0) + goto cleanup; + } + + git_fetchhead_write(remote->repo, &fetchhead_refs); + +cleanup: + for (i = 0; i < fetchhead_refs.length; ++i) + git_fetchhead_ref_free(fetchhead_refs.contents[i]); + + git_vector_dispose(&fetchhead_refs); + git_reference_free(head_ref); + + return error; +} + +/** + * Generate a list of candidates for pruning by getting a list of + * references which match the rhs of an active refspec. + */ +static int prune_candidates(git_vector *candidates, git_remote *remote) +{ + git_strarray arr = { 0 }; + size_t i; + int error; + + if ((error = git_reference_list(&arr, remote->repo)) < 0) + return error; + + for (i = 0; i < arr.count; i++) { + const char *refname = arr.strings[i]; + char *refname_dup; + + if (!git_remote__matching_dst_refspec(remote, refname)) + continue; + + refname_dup = git__strdup(refname); + GIT_ERROR_CHECK_ALLOC(refname_dup); + + if ((error = git_vector_insert(candidates, refname_dup)) < 0) + goto out; + } + +out: + git_strarray_dispose(&arr); + return error; +} + +static int find_head(const void *_a, const void *_b) +{ + git_remote_head *a = (git_remote_head *) _a; + git_remote_head *b = (git_remote_head *) _b; + + return strcmp(a->name, b->name); +} + +int git_remote_prune(git_remote *remote, const git_remote_callbacks *callbacks) +{ + size_t i, j; + git_vector remote_refs = GIT_VECTOR_INIT; + git_vector candidates = GIT_VECTOR_INIT; + const git_refspec *spec; + const char *refname; + int error; + git_oid zero_id; + + GIT_ASSERT(remote && remote->repo); + git_oid_clear(&zero_id, remote->repo->oid_type); + + if (callbacks) + GIT_ERROR_CHECK_VERSION(callbacks, GIT_REMOTE_CALLBACKS_VERSION, "git_remote_callbacks"); + + if ((error = ls_to_vector(&remote_refs, remote)) < 0) + goto cleanup; + + git_vector_set_cmp(&remote_refs, find_head); + + if ((error = prune_candidates(&candidates, remote)) < 0) + goto cleanup; + + /* + * Remove those entries from the candidate list for which we + * can find a remote reference in at least one refspec. + */ + git_vector_foreach(&candidates, i, refname) { + git_vector_foreach(&remote->active_refspecs, j, spec) { + git_str buf = GIT_STR_INIT; + size_t pos; + char *src_name; + git_remote_head key = {0}; + + if (!git_refspec_dst_matches(spec, refname)) + continue; + + if ((error = git_refspec__rtransform(&buf, spec, refname)) < 0) + goto cleanup; + + key.name = (char *) git_str_cstr(&buf); + error = git_vector_bsearch(&pos, &remote_refs, &key); + git_str_dispose(&buf); + + if (error < 0 && error != GIT_ENOTFOUND) + goto cleanup; + + if (error == GIT_ENOTFOUND) + continue; + + /* If we did find a source, remove it from the candidates. */ + if ((error = git_vector_set((void **) &src_name, &candidates, i, NULL)) < 0) + goto cleanup; + + git__free(src_name); + break; + } + } + + /* + * For those candidates still left in the list, we need to + * remove them. We do not remove symrefs, as those are for + * stuff like origin/HEAD which will never match, but we do + * not want to remove them. + */ + git_vector_foreach(&candidates, i, refname) { + git_reference *ref; + git_oid id; + + if (refname == NULL) + continue; + + error = git_reference_lookup(&ref, remote->repo, refname); + /* as we want it gone, let's not consider this an error */ + if (error == GIT_ENOTFOUND) + continue; + + if (error < 0) + goto cleanup; + + if (git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC) { + git_reference_free(ref); + continue; + } + + git_oid_cpy(&id, git_reference_target(ref)); + error = git_reference_delete(ref); + git_reference_free(ref); + + if (error < 0) + goto cleanup; + + if (callbacks && callbacks->update_refs) + error = callbacks->update_refs(refname, &id, + &zero_id, NULL, callbacks->payload); +#ifndef GIT_DEPRECATE_HARD + else if (callbacks && callbacks->update_tips) + error = callbacks->update_tips(refname, &id, + &zero_id, callbacks->payload); +#endif + + if (error < 0) { + git_error_set_after_callback_function(error, "git_remote_fetch"); + goto cleanup; + } + } + +cleanup: + git_vector_dispose(&remote_refs); + git_vector_dispose_deep(&candidates); + return error; +} + +static int update_ref( + const git_remote *remote, + const char *ref_name, + git_oid *id, + git_refspec *spec, + const char *msg, + const git_remote_callbacks *callbacks) +{ + git_reference *ref; + git_oid old_id; + int error; + + GIT_ASSERT(remote && remote->repo); + git_oid_clear(&old_id, remote->repo->oid_type); + + error = git_reference_name_to_id(&old_id, remote->repo, ref_name); + + if (error < 0 && error != GIT_ENOTFOUND) + return error; + else if (error == 0 && git_oid_equal(&old_id, id)) + return 0; + + /* If we did find a current reference, make sure we haven't lost a race */ + if (error) + error = git_reference_create(&ref, remote->repo, ref_name, id, true, msg); + else + error = git_reference_create_matching(&ref, remote->repo, ref_name, id, true, &old_id, msg); + + git_reference_free(ref); + + if (error < 0) + return error; + + if (callbacks && callbacks->update_refs) + error = callbacks->update_refs(ref_name, &old_id, + id, spec, callbacks->payload); +#ifndef GIT_DEPRECATE_HARD + else if (callbacks && callbacks->update_tips) + error = callbacks->update_tips(ref_name, &old_id, + id, callbacks->payload); +#endif + + if (error < 0) { + git_error_set_after_callback_function(error, "git_remote_fetch"); + return error; + } + + return 0; +} + +static int update_one_tip( + git_vector *update_heads, + git_remote *remote, + git_refspec *spec, + git_remote_head *head, + git_refspec *tagspec, + unsigned int update_flags, + git_remote_autotag_option_t tagopt, + const char *log_message, + const git_remote_callbacks *callbacks) +{ + git_odb *odb; + git_str refname = GIT_STR_INIT; + git_reference *ref = NULL; + bool autotag = false, updated = false; + git_oid old; + int valid; + int error; + + GIT_ASSERT(remote && remote->repo); + + if ((error = git_repository_odb__weakptr(&odb, remote->repo)) < 0) + goto done; + + /* Ignore malformed ref names (which also saves us from tag^{} */ + if ((error = git_reference_name_is_valid(&valid, head->name)) < 0) + goto done; + + if (!valid) + goto done; + + /* If we have a tag, see if the auto-follow rules say to update it */ + if (git_refspec_src_matches(tagspec, head->name)) { + if (tagopt == GIT_REMOTE_DOWNLOAD_TAGS_AUTO) + autotag = true; + + if (tagopt != GIT_REMOTE_DOWNLOAD_TAGS_NONE) { + if (git_str_puts(&refname, head->name) < 0) + goto done; + } + } + + /* If we didn't want to auto-follow the tag, check if the refspec matches */ + if (!autotag && git_refspec_src_matches(spec, head->name)) { + if (spec->dst) { + if ((error = git_refspec__transform(&refname, spec, head->name)) < 0) + goto done; + } else { + /* + * no rhs means store it in FETCH_HEAD, even if we don't + * update anything else. + */ + error = git_vector_insert(update_heads, head); + goto done; + } + } + + /* If we still don't have a refname, we don't want it */ + if (git_str_len(&refname) == 0) + goto done; + + /* In autotag mode, only create tags for objects already in db */ + if (autotag && !git_odb_exists(odb, &head->oid)) + goto done; + + if (!autotag && (error = git_vector_insert(update_heads, head)) < 0) + goto done; + + error = git_reference_name_to_id(&old, remote->repo, refname.ptr); + + if (error < 0 && error != GIT_ENOTFOUND) + goto done; + + if (!(error || error == GIT_ENOTFOUND) && + !spec->force && + !git_graph_descendant_of(remote->repo, &head->oid, &old)) { + error = 0; + goto done; + } + + if (error == GIT_ENOTFOUND) { + git_oid_clear(&old, remote->repo->oid_type); + error = 0; + + if (autotag && (error = git_vector_insert(update_heads, head)) < 0) + goto done; + } + + if ((updated = !git_oid_equal(&old, &head->oid))) { + /* In autotag mode, don't overwrite any locally-existing tags */ + error = git_reference_create(&ref, remote->repo, refname.ptr, &head->oid, !autotag, + log_message); + + if (error < 0) { + if (error == GIT_EEXISTS) + error = 0; + + goto done; + } + } + + if (!callbacks || + (!updated && (update_flags & GIT_REMOTE_UPDATE_REPORT_UNCHANGED) == 0)) + goto done; + + if (callbacks && callbacks->update_refs) + error = callbacks->update_refs(refname.ptr, &old, + &head->oid, spec, callbacks->payload); +#ifndef GIT_DEPRECATE_HARD + else if (callbacks && callbacks->update_tips) + error = callbacks->update_tips(refname.ptr, &old, + &head->oid, callbacks->payload); +#endif + + if (error < 0) + git_error_set_after_callback_function(error, "git_remote_fetch"); + +done: + git_reference_free(ref); + git_str_dispose(&refname); + return error; +} + +static int update_tips_for_spec( + git_remote *remote, + const git_remote_callbacks *callbacks, + unsigned int update_flags, + git_remote_autotag_option_t tagopt, + git_refspec *spec, + git_vector *refs, + const char *log_message) +{ + git_refspec tagspec; + git_remote_head *head, oid_head; + git_vector update_heads; + int error = 0; + size_t i; + + GIT_ASSERT_ARG(remote && remote->repo); + + if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0) + return -1; + + /* Make a copy of the transport's refs */ + if (git_vector_init(&update_heads, 16, NULL) < 0) + return -1; + + /* Update tips based on the remote heads */ + git_vector_foreach(refs, i, head) { + if (update_one_tip(&update_heads, + remote, spec, head, &tagspec, + update_flags, tagopt, log_message, + callbacks) < 0) + goto on_error; + } + + /* Handle specified oid sources */ + if (git_oid__is_hexstr(spec->src, remote->repo->oid_type)) { + git_oid id; + + if ((error = git_oid_from_string(&id, spec->src, remote->repo->oid_type)) < 0) + goto on_error; + + if (spec->dst && + (error = update_ref(remote, spec->dst, &id, spec, log_message, callbacks)) < 0) + goto on_error; + + git_oid_cpy(&oid_head.oid, &id); + oid_head.name = spec->src; + + if ((error = git_vector_insert(&update_heads, &oid_head)) < 0) + goto on_error; + } + + if ((update_flags & GIT_REMOTE_UPDATE_FETCHHEAD) && + (error = git_remote_write_fetchhead(remote, spec, &update_heads)) < 0) + goto on_error; + + git_refspec__dispose(&tagspec); + git_vector_dispose(&update_heads); + return 0; + +on_error: + git_refspec__dispose(&tagspec); + git_vector_dispose(&update_heads); + return -1; + +} + +/** + * Iteration over the three vectors, with a pause whenever we find a match + * + * On each stop, we store the iteration stat in the inout i,j,k + * parameters, and return the currently matching passive refspec as + * well as the head which we matched. + */ +static int next_head(const git_remote *remote, git_vector *refs, + git_refspec **out_spec, git_remote_head **out_head, + size_t *out_i, size_t *out_j, size_t *out_k) +{ + const git_vector *active, *passive; + git_remote_head *head; + git_refspec *spec, *passive_spec; + size_t i, j, k; + int valid; + + active = &remote->active_refspecs; + passive = &remote->passive_refspecs; + + i = *out_i; + j = *out_j; + k = *out_k; + + for (; i < refs->length; i++) { + head = git_vector_get(refs, i); + + if (git_reference_name_is_valid(&valid, head->name) < 0) + return -1; + + if (!valid) + continue; + + for (; j < active->length; j++) { + spec = git_vector_get(active, j); + + if (!git_refspec_src_matches(spec, head->name)) + continue; + + for (; k < passive->length; k++) { + passive_spec = git_vector_get(passive, k); + + if (!git_refspec_src_matches(passive_spec, head->name)) + continue; + + *out_spec = passive_spec; + *out_head = head; + *out_i = i; + *out_j = j; + *out_k = k + 1; + return 0; + + } + k = 0; + } + j = 0; + } + + return GIT_ITEROVER; +} + +static int opportunistic_updates( + const git_remote *remote, + const git_remote_callbacks *callbacks, + git_vector *refs, + const char *msg) +{ + size_t i, j, k; + git_refspec *spec; + git_remote_head *head; + git_str refname = GIT_STR_INIT; + int error = 0; + + i = j = k = 0; + + /* Handle refspecs matching remote heads */ + while ((error = next_head(remote, refs, &spec, &head, &i, &j, &k)) == 0) { + /* + * If we got here, there is a refspec which was used + * for fetching which matches the source of one of the + * passive refspecs, so we should update that + * remote-tracking branch, but not add it to + * FETCH_HEAD + */ + + git_str_clear(&refname); + if ((error = git_refspec__transform(&refname, spec, head->name)) < 0 || + (error = update_ref(remote, refname.ptr, &head->oid, spec, msg, callbacks)) < 0) + goto cleanup; + } + + if (error != GIT_ITEROVER) + goto cleanup; + + error = 0; + +cleanup: + git_str_dispose(&refname); + return error; +} + +static int truncate_fetch_head(const char *gitdir) +{ + git_str path = GIT_STR_INIT; + int error; + + if ((error = git_str_joinpath(&path, gitdir, GIT_FETCH_HEAD_FILE)) < 0) + return error; + + error = git_futils_truncate(path.ptr, GIT_REFS_FILE_MODE); + git_str_dispose(&path); + + return error; +} + +int git_remote_update_tips( + git_remote *remote, + const git_remote_callbacks *callbacks, + unsigned int update_flags, + git_remote_autotag_option_t download_tags, + const char *reflog_message) +{ + git_refspec *spec, tagspec; + git_vector refs = GIT_VECTOR_INIT; + git_remote_autotag_option_t tagopt; + int error; + size_t i; + + /* push has its own logic hidden away in the push object */ + if (remote->push) { + return git_push_update_tips(remote->push, callbacks); + } + + if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0) + return -1; + + + if ((error = ls_to_vector(&refs, remote)) < 0) + goto out; + + if (download_tags == GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED) + tagopt = remote->download_tags; + else + tagopt = download_tags; + + if ((error = truncate_fetch_head(git_repository_path(remote->repo))) < 0) + goto out; + + if (tagopt == GIT_REMOTE_DOWNLOAD_TAGS_ALL) { + if ((error = update_tips_for_spec(remote, callbacks, update_flags, tagopt, &tagspec, &refs, reflog_message)) < 0) + goto out; + } + + git_vector_foreach(&remote->active_refspecs, i, spec) { + if (spec->push) + continue; + + if ((error = update_tips_for_spec(remote, callbacks, update_flags, tagopt, spec, &refs, reflog_message)) < 0) + goto out; + } + + /* Only try to do opportunistic updates if the refspec lists differ. */ + if (remote->passed_refspecs) + error = opportunistic_updates(remote, callbacks, &refs, reflog_message); + +out: + git_vector_dispose(&refs); + git_refspec__dispose(&tagspec); + return error; +} + +int git_remote_connected(const git_remote *remote) +{ + GIT_ASSERT_ARG(remote); + + if (!remote->transport || !remote->transport->is_connected) + return 0; + + /* Ask the transport if it's connected. */ + return remote->transport->is_connected(remote->transport); +} + +int git_remote_stop(git_remote *remote) +{ + GIT_ASSERT_ARG(remote); + + if (remote->transport && remote->transport->cancel) + remote->transport->cancel(remote->transport); + + return 0; +} + +int git_remote_disconnect(git_remote *remote) +{ + GIT_ASSERT_ARG(remote); + + if (git_remote_connected(remote)) + remote->transport->close(remote->transport); + + return 0; +} + +static void free_heads(git_vector *heads) +{ + git_remote_head *head; + size_t i; + + git_vector_foreach(heads, i, head) { + git__free(head->name); + git__free(head); + } +} + +void git_remote_free(git_remote *remote) +{ + if (remote == NULL) + return; + + if (remote->transport != NULL) { + git_remote_disconnect(remote); + + remote->transport->free(remote->transport); + remote->transport = NULL; + } + + git_vector_dispose(&remote->refs); + + free_refspecs(&remote->refspecs); + git_vector_dispose(&remote->refspecs); + + free_refspecs(&remote->active_refspecs); + git_vector_dispose(&remote->active_refspecs); + + free_refspecs(&remote->passive_refspecs); + git_vector_dispose(&remote->passive_refspecs); + + free_heads(&remote->local_heads); + git_vector_dispose(&remote->local_heads); + + git_push_free(remote->push); + git__free(remote->url); + git__free(remote->pushurl); + git__free(remote->name); + git__free(remote); +} + +static int remote_list_cb(const git_config_entry *entry, void *payload) +{ + git_vector *list = payload; + const char *name = entry->name + strlen("remote."); + size_t namelen = strlen(name); + char *remote_name; + + /* we know name matches "remote..(push)?url" */ + + if (!strcmp(&name[namelen - 4], ".url")) + remote_name = git__strndup(name, namelen - 4); /* strip ".url" */ + else + remote_name = git__strndup(name, namelen - 8); /* strip ".pushurl" */ + GIT_ERROR_CHECK_ALLOC(remote_name); + + return git_vector_insert(list, remote_name); +} + +int git_remote_list(git_strarray *remotes_list, git_repository *repo) +{ + int error; + git_config *cfg; + git_vector list = GIT_VECTOR_INIT; + + if ((error = git_repository_config__weakptr(&cfg, repo)) < 0) + return error; + + if ((error = git_vector_init(&list, 4, git__strcmp_cb)) < 0) + return error; + + error = git_config_foreach_match( + cfg, "^remote\\..*\\.(push)?url$", remote_list_cb, &list); + + if (error < 0) { + git_vector_dispose_deep(&list); + return error; + } + + git_vector_uniq(&list, git__free); + + remotes_list->strings = + (char **)git_vector_detach(&remotes_list->count, NULL, &list); + + return 0; +} + +const git_indexer_progress *git_remote_stats(git_remote *remote) +{ + GIT_ASSERT_ARG_WITH_RETVAL(remote, NULL); + return &remote->stats; +} + +git_remote_autotag_option_t git_remote_autotag(const git_remote *remote) +{ + return remote->download_tags; +} + +int git_remote_set_autotag(git_repository *repo, const char *remote, git_remote_autotag_option_t value) +{ + git_str var = GIT_STR_INIT; + git_config *config; + int error; + + GIT_ASSERT_ARG(repo && remote); + + if ((error = ensure_remote_name_is_valid(remote)) < 0) + return error; + + if ((error = git_repository_config__weakptr(&config, repo)) < 0) + return error; + + if ((error = git_str_printf(&var, CONFIG_TAGOPT_FMT, remote))) + return error; + + switch (value) { + case GIT_REMOTE_DOWNLOAD_TAGS_NONE: + error = git_config_set_string(config, var.ptr, "--no-tags"); + break; + case GIT_REMOTE_DOWNLOAD_TAGS_ALL: + error = git_config_set_string(config, var.ptr, "--tags"); + break; + case GIT_REMOTE_DOWNLOAD_TAGS_AUTO: + error = git_config_delete_entry(config, var.ptr); + if (error == GIT_ENOTFOUND) + error = 0; + break; + default: + git_error_set(GIT_ERROR_INVALID, "invalid value for the tagopt setting"); + error = -1; + } + + git_str_dispose(&var); + return error; +} + +int git_remote_prune_refs(const git_remote *remote) +{ + return remote->prune_refs; +} + +static int rename_remote_config_section( + git_repository *repo, + const char *old_name, + const char *new_name) +{ + git_str old_section_name = GIT_STR_INIT, + new_section_name = GIT_STR_INIT; + int error = -1; + + if (git_str_printf(&old_section_name, "remote.%s", old_name) < 0) + goto cleanup; + + if (new_name && + (git_str_printf(&new_section_name, "remote.%s", new_name) < 0)) + goto cleanup; + + error = git_config_rename_section( + repo, + git_str_cstr(&old_section_name), + new_name ? git_str_cstr(&new_section_name) : NULL); + +cleanup: + git_str_dispose(&old_section_name); + git_str_dispose(&new_section_name); + + return error; +} + +struct update_data { + git_config *config; + const char *old_remote_name; + const char *new_remote_name; +}; + +static int update_config_entries_cb( + const git_config_entry *entry, + void *payload) +{ + struct update_data *data = (struct update_data *)payload; + + if (strcmp(entry->value, data->old_remote_name)) + return 0; + + return git_config_set_string( + data->config, entry->name, data->new_remote_name); +} + +static int update_branch_remote_config_entry( + git_repository *repo, + const char *old_name, + const char *new_name) +{ + int error; + struct update_data data = { NULL }; + + if ((error = git_repository_config__weakptr(&data.config, repo)) < 0) + return error; + + data.old_remote_name = old_name; + data.new_remote_name = new_name; + + return git_config_foreach_match( + data.config, "branch\\..+\\.remote", update_config_entries_cb, &data); +} + +static int rename_one_remote_reference( + git_reference *reference_in, + const char *old_remote_name, + const char *new_remote_name) +{ + int error; + git_reference *ref = NULL, *dummy = NULL; + git_str namespace = GIT_STR_INIT, old_namespace = GIT_STR_INIT; + git_str new_name = GIT_STR_INIT; + git_str log_message = GIT_STR_INIT; + size_t pfx_len; + const char *target; + + if ((error = git_str_printf(&namespace, GIT_REFS_REMOTES_DIR "%s/", new_remote_name)) < 0) + return error; + + pfx_len = strlen(GIT_REFS_REMOTES_DIR) + strlen(old_remote_name) + 1; + git_str_puts(&new_name, namespace.ptr); + if ((error = git_str_puts(&new_name, git_reference_name(reference_in) + pfx_len)) < 0) + goto cleanup; + + if ((error = git_str_printf(&log_message, + "renamed remote %s to %s", + old_remote_name, new_remote_name)) < 0) + goto cleanup; + + if ((error = git_reference_rename(&ref, reference_in, git_str_cstr(&new_name), 1, + git_str_cstr(&log_message))) < 0) + goto cleanup; + + if (git_reference_type(ref) != GIT_REFERENCE_SYMBOLIC) + goto cleanup; + + /* Handle refs like origin/HEAD -> origin/master */ + target = git_reference_symbolic_target(ref); + if ((error = git_str_printf(&old_namespace, GIT_REFS_REMOTES_DIR "%s/", old_remote_name)) < 0) + goto cleanup; + + if (git__prefixcmp(target, old_namespace.ptr)) + goto cleanup; + + git_str_clear(&new_name); + git_str_puts(&new_name, namespace.ptr); + if ((error = git_str_puts(&new_name, target + pfx_len)) < 0) + goto cleanup; + + error = git_reference_symbolic_set_target(&dummy, ref, git_str_cstr(&new_name), + git_str_cstr(&log_message)); + + git_reference_free(dummy); + +cleanup: + git_reference_free(reference_in); + git_reference_free(ref); + git_str_dispose(&namespace); + git_str_dispose(&old_namespace); + git_str_dispose(&new_name); + git_str_dispose(&log_message); + return error; +} + +static int rename_remote_references( + git_repository *repo, + const char *old_name, + const char *new_name) +{ + int error; + git_str buf = GIT_STR_INIT; + git_reference *ref; + git_reference_iterator *iter; + + if ((error = git_str_printf(&buf, GIT_REFS_REMOTES_DIR "%s/*", old_name)) < 0) + return error; + + error = git_reference_iterator_glob_new(&iter, repo, git_str_cstr(&buf)); + git_str_dispose(&buf); + + if (error < 0) + return error; + + while ((error = git_reference_next(&ref, iter)) == 0) { + if ((error = rename_one_remote_reference(ref, old_name, new_name)) < 0) + break; + } + + git_reference_iterator_free(iter); + + return (error == GIT_ITEROVER) ? 0 : error; +} + +static int rename_fetch_refspecs(git_vector *problems, git_remote *remote, const char *new_name) +{ + git_config *config; + git_str base = GIT_STR_INIT, var = GIT_STR_INIT, val = GIT_STR_INIT; + const git_refspec *spec; + size_t i; + int error = 0; + + if ((error = git_repository_config__weakptr(&config, remote->repo)) < 0) + return error; + + if ((error = git_vector_init(problems, 1, NULL)) < 0) + return error; + + if ((error = default_fetchspec_for_name(&base, remote->name)) < 0) + return error; + + git_vector_foreach(&remote->refspecs, i, spec) { + if (spec->push) + continue; + + /* Does the dst part of the refspec follow the expected format? */ + if (strcmp(git_str_cstr(&base), spec->string)) { + char *dup; + + dup = git__strdup(spec->string); + GIT_ERROR_CHECK_ALLOC(dup); + + if ((error = git_vector_insert(problems, dup)) < 0) + break; + + continue; + } + + /* If we do want to move it to the new section */ + + git_str_clear(&val); + git_str_clear(&var); + + if (default_fetchspec_for_name(&val, new_name) < 0 || + git_str_printf(&var, "remote.%s.fetch", new_name) < 0) + { + error = -1; + break; + } + + if ((error = git_config_set_string( + config, git_str_cstr(&var), git_str_cstr(&val))) < 0) + break; + } + + git_str_dispose(&base); + git_str_dispose(&var); + git_str_dispose(&val); + + if (error < 0) { + char *str; + git_vector_foreach(problems, i, str) + git__free(str); + + git_vector_dispose(problems); + } + + return error; +} + +int git_remote_rename(git_strarray *out, git_repository *repo, const char *name, const char *new_name) +{ + int error; + git_vector problem_refspecs = GIT_VECTOR_INIT; + git_remote *remote = NULL; + + GIT_ASSERT_ARG(out && repo && name && new_name); + + if ((error = git_remote_lookup(&remote, repo, name)) < 0) + return error; + + if ((error = ensure_remote_name_is_valid(new_name)) < 0) + goto cleanup; + + if ((error = ensure_remote_doesnot_exist(repo, new_name)) < 0) + goto cleanup; + + if ((error = rename_remote_config_section(repo, name, new_name)) < 0) + goto cleanup; + + if ((error = update_branch_remote_config_entry(repo, name, new_name)) < 0) + goto cleanup; + + if ((error = rename_remote_references(repo, name, new_name)) < 0) + goto cleanup; + + if ((error = rename_fetch_refspecs(&problem_refspecs, remote, new_name)) < 0) + goto cleanup; + + out->count = problem_refspecs.length; + out->strings = (char **) problem_refspecs.contents; + +cleanup: + if (error < 0) + git_vector_dispose(&problem_refspecs); + + git_remote_free(remote); + return error; +} + +int git_remote_name_is_valid(int *valid, const char *remote_name) +{ + git_str buf = GIT_STR_INIT; + git_refspec refspec = {0}; + int error; + + GIT_ASSERT(valid); + + *valid = 0; + + if (!remote_name || *remote_name == '\0') + return 0; + + if ((error = git_str_printf(&buf, "refs/heads/test:refs/remotes/%s/test", remote_name)) < 0) + goto done; + + error = git_refspec__parse(&refspec, git_str_cstr(&buf), true); + + if (!error) + *valid = 1; + else if (error == GIT_EINVALIDSPEC) + error = 0; + +done: + git_str_dispose(&buf); + git_refspec__dispose(&refspec); + + return error; +} + +git_refspec *git_remote__matching_refspec(git_remote *remote, const char *refname) +{ + git_refspec *spec; + git_refspec *match = NULL; + size_t i; + + git_vector_foreach(&remote->active_refspecs, i, spec) { + if (spec->push) + continue; + + if (git_refspec_src_matches_negative(spec, refname)) + return NULL; + + if (git_refspec_src_matches(spec, refname) && match == NULL) + match = spec; + } + + return match; +} + +git_refspec *git_remote__matching_dst_refspec(git_remote *remote, const char *refname) +{ + git_refspec *spec; + size_t i; + + git_vector_foreach(&remote->active_refspecs, i, spec) { + if (spec->push) + continue; + + if (git_refspec_dst_matches(spec, refname)) + return spec; + } + + return NULL; +} + +int git_remote_add_fetch(git_repository *repo, const char *remote, const char *refspec) +{ + return write_add_refspec(repo, remote, refspec, true); +} + +int git_remote_add_push(git_repository *repo, const char *remote, const char *refspec) +{ + return write_add_refspec(repo, remote, refspec, false); +} + +static int copy_refspecs(git_strarray *array, const git_remote *remote, unsigned int push) +{ + size_t i; + git_vector refspecs; + git_refspec *spec; + char *dup; + + if (git_vector_init(&refspecs, remote->refspecs.length, NULL) < 0) + return -1; + + git_vector_foreach(&remote->refspecs, i, spec) { + if (spec->push != push) + continue; + + if ((dup = git__strdup(spec->string)) == NULL) + goto on_error; + + if (git_vector_insert(&refspecs, dup) < 0) { + git__free(dup); + goto on_error; + } + } + + array->strings = (char **)refspecs.contents; + array->count = refspecs.length; + + return 0; + +on_error: + git_vector_dispose_deep(&refspecs); + + return -1; +} + +int git_remote_get_fetch_refspecs(git_strarray *array, const git_remote *remote) +{ + return copy_refspecs(array, remote, false); +} + +int git_remote_get_push_refspecs(git_strarray *array, const git_remote *remote) +{ + return copy_refspecs(array, remote, true); +} + +size_t git_remote_refspec_count(const git_remote *remote) +{ + return remote->refspecs.length; +} + +const git_refspec *git_remote_get_refspec(const git_remote *remote, size_t n) +{ + return git_vector_get(&remote->refspecs, n); +} + +int git_remote_init_callbacks(git_remote_callbacks *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_remote_callbacks, GIT_REMOTE_CALLBACKS_INIT); + return 0; +} + +/* asserts a branch..remote format */ +static const char *name_offset(size_t *len_out, const char *name) +{ + size_t prefix_len; + const char *dot; + + prefix_len = strlen("remote."); + dot = strchr(name + prefix_len, '.'); + + GIT_ASSERT_ARG_WITH_RETVAL(dot, NULL); + + *len_out = dot - name - prefix_len; + return name + prefix_len; +} + +static int remove_branch_config_related_entries( + git_repository *repo, + const char *remote_name) +{ + int error; + git_config *config; + git_config_entry *entry; + git_config_iterator *iter; + git_str buf = GIT_STR_INIT; + + if ((error = git_repository_config__weakptr(&config, repo)) < 0) + return error; + + if ((error = git_config_iterator_glob_new(&iter, config, "branch\\..+\\.remote")) < 0) + return error; + + /* find any branches with us as upstream and remove that config */ + while ((error = git_config_next(&entry, iter)) == 0) { + const char *branch; + size_t branch_len; + + if (strcmp(remote_name, entry->value)) + continue; + + if ((branch = name_offset(&branch_len, entry->name)) == NULL) { + error = -1; + break; + } + + git_str_clear(&buf); + if ((error = git_str_printf(&buf, "branch.%.*s.merge", (int)branch_len, branch)) < 0) + break; + + if ((error = git_config_delete_entry(config, git_str_cstr(&buf))) < 0) { + if (error != GIT_ENOTFOUND) + break; + git_error_clear(); + } + + git_str_clear(&buf); + if ((error = git_str_printf(&buf, "branch.%.*s.remote", (int)branch_len, branch)) < 0) + break; + + if ((error = git_config_delete_entry(config, git_str_cstr(&buf))) < 0) { + if (error != GIT_ENOTFOUND) + break; + git_error_clear(); + } + } + + if (error == GIT_ITEROVER) + error = 0; + + git_str_dispose(&buf); + git_config_iterator_free(iter); + return error; +} + +static int remove_refs(git_repository *repo, const git_refspec *spec) +{ + git_reference_iterator *iter = NULL; + git_vector refs; + const char *name; + char *dup; + int error; + size_t i; + + if ((error = git_vector_init(&refs, 8, NULL)) < 0) + return error; + + if ((error = git_reference_iterator_new(&iter, repo)) < 0) + goto cleanup; + + while ((error = git_reference_next_name(&name, iter)) == 0) { + if (!git_refspec_dst_matches(spec, name)) + continue; + + dup = git__strdup(name); + if (!dup) { + error = -1; + goto cleanup; + } + + if ((error = git_vector_insert(&refs, dup)) < 0) + goto cleanup; + } + if (error == GIT_ITEROVER) + error = 0; + if (error < 0) + goto cleanup; + + git_vector_foreach(&refs, i, name) { + if ((error = git_reference_remove(repo, name)) < 0) + break; + } + +cleanup: + git_reference_iterator_free(iter); + git_vector_foreach(&refs, i, dup) { + git__free(dup); + } + git_vector_dispose(&refs); + return error; +} + +static int remove_remote_tracking(git_repository *repo, const char *remote_name) +{ + git_remote *remote; + int error; + size_t i, count; + + /* we want to use what's on the config, regardless of changes to the instance in memory */ + if ((error = git_remote_lookup(&remote, repo, remote_name)) < 0) + return error; + + count = git_remote_refspec_count(remote); + for (i = 0; i < count; i++) { + const git_refspec *refspec = git_remote_get_refspec(remote, i); + + /* shouldn't ever actually happen */ + if (refspec == NULL) + continue; + + if ((error = remove_refs(repo, refspec)) < 0) + break; + } + + git_remote_free(remote); + return error; +} + +int git_remote_delete(git_repository *repo, const char *name) +{ + int error; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + if ((error = remove_branch_config_related_entries(repo, name)) < 0 || + (error = remove_remote_tracking(repo, name)) < 0 || + (error = rename_remote_config_section(repo, name, NULL)) < 0) + return error; + + return 0; +} + +int git_remote_default_branch(git_buf *out, git_remote *remote) +{ + GIT_BUF_WRAP_PRIVATE(out, git_remote__default_branch, remote); +} + +int git_remote__default_branch(git_str *out, git_remote *remote) +{ + const git_remote_head **heads; + const git_remote_head *guess = NULL; + const git_oid *head_id; + size_t heads_len, i; + git_str local_default = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(out); + + if ((error = git_remote_ls(&heads, &heads_len, remote)) < 0) + goto done; + + if (heads_len == 0 || strcmp(heads[0]->name, GIT_HEAD_FILE)) { + error = GIT_ENOTFOUND; + goto done; + } + + /* the first one must be HEAD so if that has the symref info, we're done */ + if (heads[0]->symref_target) { + error = git_str_puts(out, heads[0]->symref_target); + goto done; + } + + /* + * If there's no symref information, we have to look over them + * and guess. We return the first match unless the default + * branch is a candidate. Then we return the default branch. + */ + + if ((error = git_repository_initialbranch(&local_default, remote->repo)) < 0) + goto done; + + head_id = &heads[0]->oid; + + for (i = 1; i < heads_len; i++) { + if (git_oid_cmp(head_id, &heads[i]->oid)) + continue; + + if (git__prefixcmp(heads[i]->name, GIT_REFS_HEADS_DIR)) + continue; + + if (!guess) { + guess = heads[i]; + continue; + } + + if (!git__strcmp(local_default.ptr, heads[i]->name)) { + guess = heads[i]; + break; + } + } + + if (!guess) { + error = GIT_ENOTFOUND; + goto done; + } + + error = git_str_puts(out, guess->name); + +done: + git_str_dispose(&local_default); + return error; +} + +int git_remote_upload( + git_remote *remote, + const git_strarray *refspecs, + const git_push_options *opts) +{ + git_remote_connect_options connect_opts = GIT_REMOTE_CONNECT_OPTIONS_INIT; + git_push *push; + git_refspec *spec; + size_t i; + int error; + + GIT_ASSERT_ARG(remote); + + if (!remote->repo) { + git_error_set(GIT_ERROR_INVALID, "cannot download detached remote"); + return -1; + } + + if ((error = git_remote_connect_options__from_push_opts( + &connect_opts, remote, opts)) < 0) + goto cleanup; + + if ((error = connect_or_reset_options(remote, GIT_DIRECTION_PUSH, &connect_opts)) < 0) + goto cleanup; + + free_refspecs(&remote->active_refspecs); + if ((error = dwim_refspecs(&remote->active_refspecs, &remote->refspecs, &remote->refs)) < 0) + goto cleanup; + + if (remote->push) { + git_push_free(remote->push); + remote->push = NULL; + } + + if ((error = git_push_new(&remote->push, remote, opts)) < 0) + goto cleanup; + + push = remote->push; + + if (refspecs && refspecs->count > 0) { + for (i = 0; i < refspecs->count; i++) { + if ((error = git_push_add_refspec(push, refspecs->strings[i])) < 0) + goto cleanup; + } + } else { + git_vector_foreach(&remote->refspecs, i, spec) { + if (!spec->push) + continue; + if ((error = git_push_add_refspec(push, spec->string)) < 0) + goto cleanup; + } + } + + if (opts && opts->remote_push_options.count > 0) + for (i = 0; i < opts->remote_push_options.count; ++i) { + char *optstr = git__strdup(opts->remote_push_options.strings[i]); + GIT_ERROR_CHECK_ALLOC(optstr); + + if ((error = git_vector_insert(&push->remote_push_options, optstr)) < 0) + goto cleanup; + } + + error = git_push_finish(push); + + if (connect_opts.callbacks.push_update_reference) { + const int cb_error = git_push_status_foreach(push, connect_opts.callbacks.push_update_reference, connect_opts.callbacks.payload); + if (!error) + error = cb_error; + } + +cleanup: + git_remote_connect_options_dispose(&connect_opts); + return error; +} + +int git_remote_push( + git_remote *remote, + const git_strarray *refspecs, + const git_push_options *opts) +{ + git_remote_connect_options connect_opts = GIT_REMOTE_CONNECT_OPTIONS_INIT; + int error; + + GIT_ASSERT_ARG(remote); + + if (!remote->repo) { + git_error_set(GIT_ERROR_INVALID, "cannot download detached remote"); + return -1; + } + + if (git_remote_connect_options__from_push_opts(&connect_opts, + remote, opts) < 0) + return -1; + + if ((error = git_remote_upload(remote, refspecs, opts)) < 0) + goto done; + + error = git_remote_update_tips(remote, &connect_opts.callbacks, 0, 0, NULL); + +done: + git_remote_disconnect(remote); + git_remote_connect_options_dispose(&connect_opts); + return error; +} + +#define PREFIX "url" +#define SUFFIX_FETCH "insteadof" +#define SUFFIX_PUSH "pushinsteadof" + +static int apply_insteadof(char **out, git_config *config, const char *url, int direction, bool use_default_if_empty) +{ + size_t match_length, prefix_length, suffix_length; + char *replacement = NULL; + const char *regexp; + + git_str result = GIT_STR_INIT; + git_config_entry *entry; + git_config_iterator *iter; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(config); + GIT_ASSERT_ARG(url); + GIT_ASSERT_ARG(direction == GIT_DIRECTION_FETCH || direction == GIT_DIRECTION_PUSH); + + /* Add 1 to prefix/suffix length due to the additional escaped dot */ + prefix_length = strlen(PREFIX) + 1; + if (direction == GIT_DIRECTION_FETCH) { + regexp = PREFIX "\\..*\\." SUFFIX_FETCH; + suffix_length = strlen(SUFFIX_FETCH) + 1; + } else { + regexp = PREFIX "\\..*\\." SUFFIX_PUSH; + suffix_length = strlen(SUFFIX_PUSH) + 1; + } + + if (git_config_iterator_glob_new(&iter, config, regexp) < 0) + return -1; + + match_length = 0; + while (git_config_next(&entry, iter) == 0) { + size_t n, replacement_length; + + /* Check if entry value is a prefix of URL */ + if (git__prefixcmp(url, entry->value)) + continue; + + /* Check if entry value is longer than previous + * prefixes */ + if ((n = strlen(entry->value)) <= match_length) + continue; + + git__free(replacement); + match_length = n; + + /* Cut off prefix and suffix of the value */ + replacement_length = + strlen(entry->name) - (prefix_length + suffix_length); + replacement = git__strndup(entry->name + prefix_length, + replacement_length); + } + + git_config_iterator_free(iter); + + if (match_length == 0 && use_default_if_empty) { + *out = git__strdup(url); + return *out ? 0 : -1; + } else if (match_length == 0) { + *out = NULL; + return 0; + } + + git_str_printf(&result, "%s%s", replacement, url + match_length); + + git__free(replacement); + + *out = git_str_detach(&result); + return 0; +} + +/* Deprecated functions */ + +#ifndef GIT_DEPRECATE_HARD + +int git_remote_is_valid_name(const char *remote_name) +{ + int valid = 0; + + git_remote_name_is_valid(&valid, remote_name); + return valid; +} + +#endif diff --git a/src/libgit2/remote.h b/src/libgit2/remote.h new file mode 100644 index 00000000000..cc560f16941 --- /dev/null +++ b/src/libgit2/remote.h @@ -0,0 +1,100 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_remote_h__ +#define INCLUDE_remote_h__ + +#include "common.h" + +#include "git2/remote.h" +#include "git2/transport.h" +#include "git2/sys/remote.h" +#include "git2/sys/transport.h" + +#include "refspec.h" +#include "vector.h" +#include "net.h" +#include "proxy.h" + +#define GIT_REMOTE_ORIGIN "origin" + +struct git_remote { + char *name; + char *url; + char *pushurl; + git_vector refs; + git_vector refspecs; + git_vector active_refspecs; + git_vector passive_refspecs; + git_vector local_heads; + git_transport *transport; + git_repository *repo; + git_push *push; + git_indexer_progress stats; + unsigned int need_pack; + git_remote_autotag_option_t download_tags; + int prune_refs; + int passed_refspecs; + git_fetch_negotiation nego; +}; + +int git_remote__urlfordirection(git_str *url_out, struct git_remote *remote, int direction, const git_remote_callbacks *callbacks); +int git_remote__http_proxy(char **out, git_remote *remote, git_net_url *url); + +git_refspec *git_remote__matching_refspec(git_remote *remote, const char *refname); +git_refspec *git_remote__matching_dst_refspec(git_remote *remote, const char *refname); + +int git_remote__default_branch(git_str *out, git_remote *remote); + +int git_remote_connect_options_dup( + git_remote_connect_options *dst, + const git_remote_connect_options *src); +int git_remote_connect_options_normalize( + git_remote_connect_options *dst, + git_repository *repo, + const git_remote_connect_options *src); + +int git_remote_capabilities(unsigned int *out, git_remote *remote); + + +#define git_remote_connect_options__copy_opts(out, in) \ + if (in) { \ + (out)->callbacks = (in)->callbacks; \ + (out)->proxy_opts = (in)->proxy_opts; \ + (out)->custom_headers = (in)->custom_headers; \ + (out)->follow_redirects = (in)->follow_redirects; \ + } + +GIT_INLINE(int) git_remote_connect_options__from_fetch_opts( + git_remote_connect_options *out, + git_remote *remote, + const git_fetch_options *fetch_opts) +{ + git_remote_connect_options tmp = GIT_REMOTE_CONNECT_OPTIONS_INIT; + git_remote_connect_options__copy_opts(&tmp, fetch_opts); + return git_remote_connect_options_normalize(out, remote->repo, &tmp); +} + +GIT_INLINE(int) git_remote_connect_options__from_push_opts( + git_remote_connect_options *out, + git_remote *remote, + const git_push_options *push_opts) +{ + git_remote_connect_options tmp = GIT_REMOTE_CONNECT_OPTIONS_INIT; + git_remote_connect_options__copy_opts(&tmp, push_opts); + return git_remote_connect_options_normalize(out, remote->repo, &tmp); +} + +#undef git_remote_connect_options__copy_opts + +GIT_INLINE(void) git_remote_connect_options__dispose( + git_remote_connect_options *opts) +{ + git_proxy_options_dispose(&opts->proxy_opts); + git_strarray_dispose(&opts->custom_headers); +} + +#endif diff --git a/src/repo_template.h b/src/libgit2/repo_template.h similarity index 85% rename from src/repo_template.h rename to src/libgit2/repo_template.h index 90ffe851b93..06ed8e84fd4 100644 --- a/src/repo_template.h +++ b/src/libgit2/repo_template.h @@ -11,10 +11,10 @@ #define GIT_OBJECTS_PACK_DIR GIT_OBJECTS_DIR "pack/" #define GIT_HOOKS_DIR "hooks/" -#define GIT_HOOKS_DIR_MODE 0755 +#define GIT_HOOKS_DIR_MODE 0777 #define GIT_HOOKS_README_FILE GIT_HOOKS_DIR "README.sample" -#define GIT_HOOKS_README_MODE 0755 +#define GIT_HOOKS_README_MODE 0777 #define GIT_HOOKS_README_CONTENT \ "#!/bin/sh\n"\ "#\n"\ @@ -23,16 +23,16 @@ "# more information.\n" #define GIT_INFO_DIR "info/" -#define GIT_INFO_DIR_MODE 0755 +#define GIT_INFO_DIR_MODE 0777 #define GIT_INFO_EXCLUDE_FILE GIT_INFO_DIR "exclude" -#define GIT_INFO_EXCLUDE_MODE 0644 +#define GIT_INFO_EXCLUDE_MODE 0666 #define GIT_INFO_EXCLUDE_CONTENT \ "# File patterns to ignore; see `git help ignore` for more information.\n"\ "# Lines that start with '#' are comments.\n" #define GIT_DESC_FILE "description" -#define GIT_DESC_MODE 0644 +#define GIT_DESC_MODE 0666 #define GIT_DESC_CONTENT \ "Unnamed repository; edit this file 'description' to name the repository.\n" @@ -45,8 +45,6 @@ typedef struct { static repo_template_item repo_template[] = { { GIT_OBJECTS_INFO_DIR, GIT_OBJECT_DIR_MODE, NULL }, /* '/objects/info/' */ { GIT_OBJECTS_PACK_DIR, GIT_OBJECT_DIR_MODE, NULL }, /* '/objects/pack/' */ - { GIT_REFS_HEADS_DIR, GIT_REFS_DIR_MODE, NULL }, /* '/refs/heads/' */ - { GIT_REFS_TAGS_DIR, GIT_REFS_DIR_MODE, NULL }, /* '/refs/tags/' */ { GIT_HOOKS_DIR, GIT_HOOKS_DIR_MODE, NULL }, /* '/hooks/' */ { GIT_INFO_DIR, GIT_INFO_DIR_MODE, NULL }, /* '/info/' */ { GIT_DESC_FILE, GIT_DESC_MODE, GIT_DESC_CONTENT }, diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c new file mode 100644 index 00000000000..c7ce8699a8f --- /dev/null +++ b/src/libgit2/repository.c @@ -0,0 +1,4136 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "repository.h" + +#include + +#include "git2/object.h" +#include "git2/sys/refdb_backend.h" +#include "git2/sys/repository.h" + +#include "buf.h" +#include "common.h" +#include "commit.h" +#include "grafts.h" +#include "tag.h" +#include "blob.h" +#include "futils.h" +#include "sysdir.h" +#include "filebuf.h" +#include "index.h" +#include "config.h" +#include "refs.h" +#include "filter.h" +#include "odb.h" +#include "refdb.h" +#include "remote.h" +#include "merge.h" +#include "diff_driver.h" +#include "annotated_commit.h" +#include "submodule.h" +#include "worktree.h" +#include "path.h" + +#ifdef GIT_WIN32 +# include "win32/w32_util.h" +#endif + +bool git_repository__validate_ownership = true; +bool git_repository__fsync_gitdir = false; + +static const struct { + git_repository_item_t parent; + git_repository_item_t fallback; + const char *name; + bool directory; +} items[] = { + { GIT_REPOSITORY_ITEM_GITDIR, GIT_REPOSITORY_ITEM__LAST, NULL, true }, + { GIT_REPOSITORY_ITEM_WORKDIR, GIT_REPOSITORY_ITEM__LAST, NULL, true }, + { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM__LAST, NULL, true }, + { GIT_REPOSITORY_ITEM_GITDIR, GIT_REPOSITORY_ITEM__LAST, "index", false }, + { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "objects", true }, + { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "refs", true }, + { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "packed-refs", false }, + { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "remotes", true }, + { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "config", false }, + { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "info", true }, + { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "hooks", true }, + { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "logs", true }, + { GIT_REPOSITORY_ITEM_GITDIR, GIT_REPOSITORY_ITEM__LAST, "modules", true }, + { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "worktrees", true }, + { GIT_REPOSITORY_ITEM_GITDIR, GIT_REPOSITORY_ITEM_GITDIR, "config.worktree", false } +}; + +static int check_repositoryformatversion(int *version, git_config *config); +static int check_extensions(git_config *config, int version); +static int load_global_config(git_config **config, bool use_env); +static int load_objectformat(git_repository *repo, git_config *config); +static int load_refstorage_format(git_repository *repo, git_config *config); + +#define GIT_COMMONDIR_FILE "commondir" +#define GIT_GITDIR_FILE "gitdir" + +#define GIT_FILE_CONTENT_PREFIX "gitdir:" + +#define GIT_BRANCH_DEFAULT "master" + +#define GIT_REPO_VERSION_DEFAULT 0 +#define GIT_REPO_VERSION_MAX 1 + +git_str git_repository__reserved_names_win32[] = { + { DOT_GIT, 0, CONST_STRLEN(DOT_GIT) }, + { GIT_DIR_SHORTNAME, 0, CONST_STRLEN(GIT_DIR_SHORTNAME) } +}; +size_t git_repository__reserved_names_win32_len = 2; + +git_str git_repository__reserved_names_posix[] = { + { DOT_GIT, 0, CONST_STRLEN(DOT_GIT) }, +}; +size_t git_repository__reserved_names_posix_len = 1; + +static void set_odb(git_repository *repo, git_odb *odb) +{ + if (odb) { + GIT_REFCOUNT_OWN(odb, repo); + GIT_REFCOUNT_INC(odb); + } + + if ((odb = git_atomic_swap(repo->_odb, odb)) != NULL) { + GIT_REFCOUNT_OWN(odb, NULL); + git_odb_free(odb); + } +} + +static void set_refdb(git_repository *repo, git_refdb *refdb) +{ + if (refdb) { + GIT_REFCOUNT_OWN(refdb, repo); + GIT_REFCOUNT_INC(refdb); + } + + if ((refdb = git_atomic_swap(repo->_refdb, refdb)) != NULL) { + GIT_REFCOUNT_OWN(refdb, NULL); + git_refdb_free(refdb); + } +} + +static void set_config(git_repository *repo, git_config *config) +{ + if (config) { + GIT_REFCOUNT_OWN(config, repo); + GIT_REFCOUNT_INC(config); + } + + if ((config = git_atomic_swap(repo->_config, config)) != NULL) { + GIT_REFCOUNT_OWN(config, NULL); + git_config_free(config); + } + + git_repository__configmap_lookup_cache_clear(repo); +} + +static void set_index(git_repository *repo, git_index *index) +{ + if (index) { + GIT_REFCOUNT_OWN(index, repo); + GIT_REFCOUNT_INC(index); + } + + if ((index = git_atomic_swap(repo->_index, index)) != NULL) { + GIT_REFCOUNT_OWN(index, NULL); + git_index_free(index); + } +} + +int git_repository__cleanup(git_repository *repo) +{ + GIT_ASSERT_ARG(repo); + + git_repository_submodule_cache_clear(repo); + git_cache_clear(&repo->objects); + git_attr_cache_flush(repo); + git_grafts_free(repo->grafts); + repo->grafts = NULL; + git_grafts_free(repo->shallow_grafts); + repo->shallow_grafts = NULL; + + set_config(repo, NULL); + set_index(repo, NULL); + set_odb(repo, NULL); + set_refdb(repo, NULL); + + return 0; +} + +void git_repository_free(git_repository *repo) +{ + size_t i; + + if (repo == NULL) + return; + + git_repository__cleanup(repo); + + git_cache_dispose(&repo->objects); + + git_diff_driver_registry_free(repo->diff_drivers); + repo->diff_drivers = NULL; + + for (i = 0; i < repo->reserved_names.size; i++) + git_str_dispose(git_array_get(repo->reserved_names, i)); + git_array_clear(repo->reserved_names); + + git__free(repo->gitlink); + git__free(repo->gitdir); + git__free(repo->commondir); + git__free(repo->workdir); + git__free(repo->namespace); + git__free(repo->ident_name); + git__free(repo->ident_email); + + git__memzero(repo, sizeof(*repo)); + git__free(repo); +} + +/* Check if we have a separate commondir (e.g. we have a worktree) */ +static int lookup_commondir( + bool *separate, + git_str *commondir, + git_str *repository_path, + uint32_t flags) +{ + git_str common_link = GIT_STR_INIT; + int error; + + /* Environment variable overrides configuration */ + if ((flags & GIT_REPOSITORY_OPEN_FROM_ENV)) { + error = git__getenv(commondir, "GIT_COMMON_DIR"); + + if (!error || error != GIT_ENOTFOUND) + goto done; + } + + /* + * If there's no commondir file, the repository path is the + * common path, but it needs a trailing slash. + */ + if (!git_fs_path_contains_file(repository_path, GIT_COMMONDIR_FILE)) { + if ((error = git_str_set(commondir, repository_path->ptr, repository_path->size)) == 0) + error = git_fs_path_to_dir(commondir); + + *separate = false; + goto done; + } + + *separate = true; + + if ((error = git_str_joinpath(&common_link, repository_path->ptr, GIT_COMMONDIR_FILE)) < 0 || + (error = git_futils_readbuffer(&common_link, common_link.ptr)) < 0) + goto done; + + git_str_rtrim(&common_link); + if (git_fs_path_is_relative(common_link.ptr)) { + if ((error = git_str_joinpath(commondir, repository_path->ptr, common_link.ptr)) < 0) + goto done; + } else { + git_str_swap(commondir, &common_link); + } + + /* Make sure the commondir path always has a trailing slash */ + error = git_fs_path_prettify_dir(commondir, commondir->ptr, NULL); + +done: + git_str_dispose(&common_link); + return error; +} + +GIT_INLINE(int) validate_repo_path(git_str *path) +{ + /* + * The longest static path in a repository (or commondir) is the + * packed refs file. (Loose refs may be longer since they + * include the reference name, but will be validated when the + * path is constructed.) + */ + static size_t suffix_len = + CONST_STRLEN("objects/pack/pack-.pack.lock") + + GIT_OID_MAX_HEXSIZE; + + return git_fs_path_validate_str_length_with_suffix( + path, suffix_len); +} + +/* + * Git repository open methods + * + * Open a repository object from its path + */ +static int is_valid_repository_path( + bool *out, + git_str *repository_path, + git_str *common_path, + uint32_t flags) +{ + bool separate_commondir = false; + int error; + + *out = false; + + if ((error = lookup_commondir(&separate_commondir, + common_path, repository_path, flags)) < 0) + return error; + + /* Ensure HEAD file exists */ + if (git_fs_path_contains_file(repository_path, GIT_HEAD_FILE) == false) + return 0; + + /* Check files in common dir */ + if (git_fs_path_contains_dir(common_path, GIT_OBJECTS_DIR) == false) + return 0; + if (git_fs_path_contains_dir(common_path, GIT_REFS_DIR) == false) + return 0; + + /* Ensure the repo (and commondir) are valid paths */ + if ((error = validate_repo_path(common_path)) < 0 || + (separate_commondir && + (error = validate_repo_path(repository_path)) < 0)) + return error; + + *out = true; + return 0; +} + +static git_repository *repository_alloc(void) +{ + git_repository *repo = git__calloc(1, sizeof(git_repository)); + + if (repo == NULL || + git_cache_init(&repo->objects) < 0) + goto on_error; + + git_array_init_to_size(repo->reserved_names, 4); + if (!repo->reserved_names.ptr) + goto on_error; + + /* set all the entries in the configmap cache to `unset` */ + git_repository__configmap_lookup_cache_clear(repo); + + return repo; + +on_error: + if (repo) + git_cache_dispose(&repo->objects); + + git__free(repo); + return NULL; +} + +int git_repository_new_ext( + git_repository **out, + git_repository_new_options *opts) +{ + git_repository *repo; + + GIT_ASSERT_ARG(out); + GIT_ERROR_CHECK_VERSION(opts, + GIT_REPOSITORY_NEW_OPTIONS_VERSION, + "git_repository_new_options"); + + if (opts && opts->oid_type) + GIT_ASSERT_ARG(git_oid_type_is_valid(opts->oid_type)); + + *out = repo = repository_alloc(); + GIT_ERROR_CHECK_ALLOC(repo); + + repo->is_bare = 1; + repo->is_worktree = 0; + repo->oid_type = opts && opts->oid_type ? opts->oid_type : + GIT_OID_DEFAULT; + + /* + * This is a bit dirty, as this repository doesn't really have a refdb + * in the first place. But we do expect that we can create an "empty" + * ref iterator from such a repository, and things keep on working like + * this. + * + * It might make sense to eventually create an "in-memory" refdb type + * to serve this purpose. + */ + repo->refdb_type = GIT_REFDB_FILES; + + return 0; +} + +int git_repository_new(git_repository **out) +{ + return git_repository_new_ext(out, NULL); +} + +static int load_config_data(git_repository *repo, const git_config *config) +{ + int is_bare; + + int err = git_config_get_bool(&is_bare, config, "core.bare"); + if (err < 0 && err != GIT_ENOTFOUND) + return err; + + /* Try to figure out if it's bare, default to non-bare if it's not set */ + if (err != GIT_ENOTFOUND) + repo->is_bare = is_bare && !repo->is_worktree; + else + repo->is_bare = 0; + + return 0; +} + +static int load_workdir( + git_repository *repo, + git_config *config, + git_str *parent_path) +{ + git_config_entry *ce = NULL; + git_str worktree = GIT_STR_INIT; + git_str path = GIT_STR_INIT; + git_str workdir_env = GIT_STR_INIT; + const char *value = NULL; + int error; + + if (repo->is_bare) + return 0; + + /* Environment variables are preferred */ + if (repo->use_env) { + error = git__getenv(&workdir_env, "GIT_WORK_TREE"); + + if (error == 0) + value = workdir_env.ptr; + else if (error == GIT_ENOTFOUND) + error = 0; + else + goto cleanup; + } + + /* Examine configuration values if necessary */ + if (!value) { + if ((error = git_config__lookup_entry(&ce, config, + "core.worktree", false)) < 0) + return error; + + if (ce && ce->value) + value = ce->value; + } + + if (repo->is_worktree) { + char *gitlink = git_worktree__read_link(repo->gitdir, GIT_GITDIR_FILE); + if (!gitlink) { + error = -1; + goto cleanup; + } + + git_str_attach(&worktree, gitlink, 0); + + if ((git_fs_path_dirname_r(&worktree, worktree.ptr)) < 0 || + git_fs_path_to_dir(&worktree) < 0) { + error = -1; + goto cleanup; + } + + repo->workdir = git_str_detach(&worktree); + } else if (value) { + if (!*value) { + git_error_set(GIT_ERROR_NET, "working directory cannot be set to empty path"); + error = -1; + goto cleanup; + } + + if ((error = git_fs_path_prettify_dir(&worktree, + value, repo->gitdir)) < 0) + goto cleanup; + + repo->workdir = git_str_detach(&worktree); + } else if (parent_path && git_fs_path_isdir(parent_path->ptr)) { + repo->workdir = git_str_detach(parent_path); + } else { + if (git_fs_path_dirname_r(&worktree, repo->gitdir) < 0 || + git_fs_path_to_dir(&worktree) < 0) { + error = -1; + goto cleanup; + } + + repo->workdir = git_str_detach(&worktree); + } + + GIT_ERROR_CHECK_ALLOC(repo->workdir); + +cleanup: + git_str_dispose(&path); + git_str_dispose(&workdir_env); + git_config_entry_free(ce); + return error; +} + +/* + * This function returns furthest offset into path where a ceiling dir + * is found, so we can stop processing the path at that point. + * + * Note: converting this to use git_strs instead of GIT_PATH_MAX buffers on + * the stack could remove directories name limits, but at the cost of doing + * repeated malloc/frees inside the loop below, so let's not do it now. + */ +static size_t find_ceiling_dir_offset( + const char *path, + const char *ceiling_directories) +{ + char buf[GIT_PATH_MAX + 1]; + char buf2[GIT_PATH_MAX + 1]; + const char *ceil, *sep; + size_t len, max_len = 0, min_len; + + GIT_ASSERT_ARG(path); + + min_len = (size_t)(git_fs_path_root(path) + 1); + + if (ceiling_directories == NULL || min_len == 0) + return min_len; + + for (sep = ceil = ceiling_directories; *sep; ceil = sep + 1) { + for (sep = ceil; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++); + len = sep - ceil; + + if (len == 0 || len >= sizeof(buf) || git_fs_path_root(ceil) == -1) + continue; + + strncpy(buf, ceil, len); + buf[len] = '\0'; + + if (p_realpath(buf, buf2) == NULL) + continue; + + len = strlen(buf2); + if (len > 0 && buf2[len-1] == '/') + buf[--len] = '\0'; + + if (!strncmp(path, buf2, len) && + (path[len] == '/' || !path[len]) && + len > max_len) + { + max_len = len; + } + } + + return (max_len <= min_len ? min_len : max_len); +} + +/* + * Read the contents of `file_path` and set `path_out` to the repo dir that + * it points to. Before calling, set `path_out` to the base directory that + * should be used if the contents of `file_path` are a relative path. + */ +static int read_gitfile(git_str *path_out, const char *file_path) +{ + int error = 0; + git_str file = GIT_STR_INIT; + size_t prefix_len = strlen(GIT_FILE_CONTENT_PREFIX); + + GIT_ASSERT_ARG(path_out); + GIT_ASSERT_ARG(file_path); + + if (git_futils_readbuffer(&file, file_path) < 0) + return -1; + + git_str_rtrim(&file); + /* apparently on Windows, some people use backslashes in paths */ + git_fs_path_mkposix(file.ptr); + + if (git_str_len(&file) <= prefix_len || + memcmp(git_str_cstr(&file), GIT_FILE_CONTENT_PREFIX, prefix_len) != 0) + { + git_error_set(GIT_ERROR_REPOSITORY, + "the `.git` file at '%s' is malformed", file_path); + error = -1; + } + else if ((error = git_fs_path_dirname_r(path_out, file_path)) >= 0) { + const char *gitlink = git_str_cstr(&file) + prefix_len; + while (*gitlink && git__isspace(*gitlink)) gitlink++; + + error = git_fs_path_prettify_dir( + path_out, gitlink, git_str_cstr(path_out)); + } + + git_str_dispose(&file); + return error; +} + +typedef struct { + const char *repo_path; + git_str tmp; + bool *is_safe; +} validate_ownership_data; + +static int validate_ownership_cb(const git_config_entry *entry, void *payload) +{ + validate_ownership_data *data = payload; + const char *test_path; + + if (strcmp(entry->value, "") == 0) { + *data->is_safe = false; + } else if (strcmp(entry->value, "*") == 0) { + *data->is_safe = true; + } else { + if (git_str_sets(&data->tmp, entry->value) < 0) + return -1; + + if (!git_fs_path_is_root(data->tmp.ptr)) { + /* Input must not have trailing backslash. */ + if (!data->tmp.size || + data->tmp.ptr[data->tmp.size - 1] == '/') + return 0; + + if (git_fs_path_to_dir(&data->tmp) < 0) + return -1; + } + + test_path = data->tmp.ptr; + + /* + * Git - and especially, Git for Windows - does some + * truly bizarre things with paths that start with a + * forward slash; and expects you to escape that with + * `%(prefix)`. This syntax generally means to add the + * prefix that Git was installed to (eg `/usr/local`) + * unless it's an absolute path, in which case the + * leading `%(prefix)/` is just removed. And Git for + * Windows expects you to use this syntax for absolute + * Unix-style paths (in "Git Bash" or Windows Subsystem + * for Linux). + * + * Worse, the behavior used to be that a leading `/` was + * not absolute. It would indicate that Git for Windows + * should add the prefix. So `//` is required for absolute + * Unix-style paths. Yes, this is truly horrifying. + * + * Emulate that behavior, I guess, but only for absolute + * paths. We won't deal with the Git install prefix. Also, + * give WSL users an escape hatch where they don't have to + * think about this and can use the literal path that the + * filesystem APIs provide (`//wsl.localhost/...`). + */ + if (strncmp(test_path, "%(prefix)//", strlen("%(prefix)//")) == 0) + test_path += strlen("%(prefix)/"); + + if (strcmp(test_path, data->repo_path) == 0) + *data->is_safe = true; + } + + return 0; +} + +static int validate_ownership_config( + bool *is_safe, + const char *path, + bool use_env) +{ + validate_ownership_data ownership_data = { + path, GIT_STR_INIT, is_safe + }; + git_config *config; + int error; + + if (load_global_config(&config, use_env) != 0) + return 0; + + error = git_config_get_multivar_foreach(config, + "safe.directory", NULL, + validate_ownership_cb, + &ownership_data); + + if (error == GIT_ENOTFOUND) + error = 0; + + git_config_free(config); + git_str_dispose(&ownership_data.tmp); + + return error; +} + +static int validate_ownership_path(bool *is_safe, const char *path) +{ + git_fs_path_owner_t owner_level = + GIT_FS_PATH_OWNER_CURRENT_USER | + GIT_FS_PATH_USER_IS_ADMINISTRATOR | + GIT_FS_PATH_OWNER_RUNNING_SUDO; + int error = 0; + + if (path) + error = git_fs_path_owner_is(is_safe, path, owner_level); + + if (error == GIT_ENOTFOUND) { + *is_safe = true; + error = 0; + } else if (error == GIT_EINVALID) { + *is_safe = false; + error = 0; + } + + return error; +} + +static int validate_ownership(git_repository *repo) +{ + const char *validation_paths[3] = { NULL }, *path = NULL; + size_t validation_len = 0, i; + bool is_safe = false; + int error = 0; + + /* + * If there's a worktree, validate the permissions to it *and* + * the git directory, and use the worktree as the configuration + * key for allowlisting the directory. In a bare setup, only + * look at the gitdir and use that as the allowlist. So we + * examine all `validation_paths` but use only the first as + * the configuration lookup. + */ + + if (repo->workdir) + validation_paths[validation_len++] = repo->workdir; + + if (repo->gitlink) + validation_paths[validation_len++] = repo->gitlink; + + validation_paths[validation_len++] = repo->gitdir; + + for (i = 0; i < validation_len; i++) { + path = validation_paths[i]; + + if ((error = validate_ownership_path(&is_safe, path)) < 0) + goto done; + + if (!is_safe) + break; + } + + if (is_safe || + (error = validate_ownership_config( + &is_safe, validation_paths[0], repo->use_env)) < 0) + goto done; + + if (!is_safe) { + size_t path_len = git_fs_path_is_root(path) ? + strlen(path) : git_fs_path_dirlen(path); + + git_error_set(GIT_ERROR_CONFIG, + "repository path '%.*s' is not owned by current user", + (int)min(path_len, INT_MAX), path); + error = GIT_EOWNER; + } + +done: + return error; +} + +struct repo_paths { + git_str gitdir; + git_str workdir; + git_str gitlink; + git_str commondir; +}; + +#define REPO_PATHS_INIT { GIT_STR_INIT } + +GIT_INLINE(void) repo_paths_dispose(struct repo_paths *paths) +{ + git_str_dispose(&paths->gitdir); + git_str_dispose(&paths->workdir); + git_str_dispose(&paths->gitlink); + git_str_dispose(&paths->commondir); +} + +static int find_repo_traverse( + struct repo_paths *out, + const char *start_path, + const char *ceiling_dirs, + uint32_t flags) +{ + git_str path = GIT_STR_INIT; + git_str repo_link = GIT_STR_INIT; + git_str common_link = GIT_STR_INIT; + struct stat st; + dev_t initial_device = 0; + int min_iterations; + bool in_dot_git, is_valid; + size_t ceiling_offset = 0; + int error; + + git_str_clear(&out->gitdir); + + if ((error = git_fs_path_prettify(&path, start_path, NULL)) < 0) + return error; + + /* + * In each loop we look first for a `.git` dir within the + * directory, then to see if the directory itself is a repo. + * + * In other words: if we start in /a/b/c, then we look at: + * /a/b/c/.git, /a/b/c, /a/b/.git, /a/b, /a/.git, /a + * + * With GIT_REPOSITORY_OPEN_BARE or GIT_REPOSITORY_OPEN_NO_DOTGIT, + * we assume we started with /a/b/c.git and don't append .git the + * first time through. min_iterations indicates the number of + * iterations left before going further counts as a search. + */ + if (flags & (GIT_REPOSITORY_OPEN_BARE | GIT_REPOSITORY_OPEN_NO_DOTGIT)) { + in_dot_git = true; + min_iterations = 1; + } else { + in_dot_git = false; + min_iterations = 2; + } + + for (;;) { + if (!(flags & GIT_REPOSITORY_OPEN_NO_DOTGIT)) { + if (!in_dot_git) { + if ((error = git_str_joinpath(&path, path.ptr, DOT_GIT)) < 0) + goto out; + } + in_dot_git = !in_dot_git; + } + + if (p_stat(path.ptr, &st) == 0) { + /* check that we have not crossed device boundaries */ + if (initial_device == 0) + initial_device = st.st_dev; + else if (st.st_dev != initial_device && + !(flags & GIT_REPOSITORY_OPEN_CROSS_FS)) + break; + + if (S_ISDIR(st.st_mode)) { + if ((error = is_valid_repository_path(&is_valid, &path, &common_link, flags)) < 0) + goto out; + + if (is_valid) { + if ((error = git_fs_path_to_dir(&path)) < 0 || + (error = git_str_set(&out->gitdir, path.ptr, path.size)) < 0) + goto out; + + if ((error = git_str_attach(&out->gitlink, git_worktree__read_link(path.ptr, GIT_GITDIR_FILE), 0)) < 0) + goto out; + + git_str_swap(&common_link, &out->commondir); + + break; + } + } else if (S_ISREG(st.st_mode) && git__suffixcmp(path.ptr, "/" DOT_GIT) == 0) { + if ((error = read_gitfile(&repo_link, path.ptr)) < 0 || + (error = is_valid_repository_path(&is_valid, &repo_link, &common_link, flags)) < 0) + goto out; + + if (is_valid) { + git_str_swap(&out->gitdir, &repo_link); + + if ((error = git_str_put(&out->gitlink, path.ptr, path.size)) < 0) + goto out; + + git_str_swap(&common_link, &out->commondir); + } + break; + } + } + + /* + * Move up one directory. If we're in_dot_git, we'll + * search the parent itself next. If we're !in_dot_git, + * we'll search .git in the parent directory next (added + * at the top of the loop). + */ + if ((error = git_fs_path_dirname_r(&path, path.ptr)) < 0) + goto out; + + /* + * Once we've checked the directory (and .git if + * applicable), find the ceiling for a search. + */ + if (min_iterations && (--min_iterations == 0)) + ceiling_offset = find_ceiling_dir_offset(path.ptr, ceiling_dirs); + + /* Check if we should stop searching here. */ + if (min_iterations == 0 && + (path.ptr[ceiling_offset] == 0 || (flags & GIT_REPOSITORY_OPEN_NO_SEARCH))) + break; + } + + if (!(flags & GIT_REPOSITORY_OPEN_BARE)) { + if (!git_str_len(&out->gitdir)) + git_str_clear(&out->workdir); + else if ((error = git_fs_path_dirname_r(&out->workdir, path.ptr)) < 0 || + (error = git_fs_path_to_dir(&out->workdir)) < 0) + goto out; + } + + /* If we didn't find the repository, and we don't have any other + * error to report, report that. */ + if (!git_str_len(&out->gitdir)) { + git_error_set(GIT_ERROR_REPOSITORY, "could not find repository at '%s'", start_path); + error = GIT_ENOTFOUND; + goto out; + } + +out: + if (error) + repo_paths_dispose(out); + + git_str_dispose(&path); + git_str_dispose(&repo_link); + git_str_dispose(&common_link); + return error; +} + +static int load_grafts(git_repository *repo) +{ + git_str path = GIT_STR_INIT; + int error; + + /* refresh if they've both been opened previously */ + if (repo->grafts && repo->shallow_grafts) { + if ((error = git_grafts_refresh(repo->grafts)) < 0 || + (error = git_grafts_refresh(repo->shallow_grafts)) < 0) + return error; + } + + /* resolve info path, which may not be found for inmemory repository */ + if ((error = git_repository__item_path(&path, repo, GIT_REPOSITORY_ITEM_INFO)) < 0) { + if (error != GIT_ENOTFOUND) + return error; + + /* create empty/inmemory grafts for inmemory repository */ + if (!repo->grafts && (error = git_grafts_new(&repo->grafts, repo->oid_type)) < 0) + return error; + + if (!repo->shallow_grafts && (error = git_grafts_new(&repo->shallow_grafts, repo->oid_type)) < 0) + return error; + + return 0; + } + + /* load grafts from disk */ + if ((error = git_str_joinpath(&path, path.ptr, "grafts")) < 0 || + (error = git_grafts_open_or_refresh(&repo->grafts, path.ptr, repo->oid_type)) < 0) + goto error; + + git_str_clear(&path); + + if ((error = git_str_joinpath(&path, repo->gitdir, "shallow")) < 0 || + (error = git_grafts_open_or_refresh(&repo->shallow_grafts, path.ptr, repo->oid_type)) < 0) + goto error; + +error: + git_str_dispose(&path); + return error; +} + +static int find_repo( + struct repo_paths *out, + const char *start_path, + const char *ceiling_dirs, + uint32_t flags) +{ + bool use_env = !!(flags & GIT_REPOSITORY_OPEN_FROM_ENV); + git_str gitdir_buf = GIT_STR_INIT, + ceiling_dirs_buf = GIT_STR_INIT, + across_fs_buf = GIT_STR_INIT; + int error; + + if (use_env && !start_path) { + error = git__getenv(&gitdir_buf, "GIT_DIR"); + + if (!error) { + start_path = gitdir_buf.ptr; + flags |= GIT_REPOSITORY_OPEN_NO_SEARCH; + flags |= GIT_REPOSITORY_OPEN_NO_DOTGIT; + } else if (error == GIT_ENOTFOUND) { + start_path = "."; + } else { + goto done; + } + } + + if (use_env && !ceiling_dirs) { + error = git__getenv(&ceiling_dirs_buf, + "GIT_CEILING_DIRECTORIES"); + + if (!error) + ceiling_dirs = ceiling_dirs_buf.ptr; + else if (error != GIT_ENOTFOUND) + goto done; + } + + if (use_env) { + error = git__getenv(&across_fs_buf, + "GIT_DISCOVERY_ACROSS_FILESYSTEM"); + + if (!error) { + int across_fs = 0; + + if ((error = git_config_parse_bool(&across_fs, + git_str_cstr(&across_fs_buf))) < 0) + goto done; + + if (across_fs) + flags |= GIT_REPOSITORY_OPEN_CROSS_FS; + } else if (error != GIT_ENOTFOUND) { + goto done; + } + } + + error = find_repo_traverse(out, start_path, ceiling_dirs, flags); + +done: + git_str_dispose(&gitdir_buf); + git_str_dispose(&ceiling_dirs_buf); + git_str_dispose(&across_fs_buf); + + return error; +} + +static int read_repository_format(git_repository *repo) +{ + int error; + git_str config_path = GIT_STR_INIT; + git_config *config = NULL; + int version = 0; + + if ((error = git_repository__item_path(&config_path, repo, + GIT_REPOSITORY_ITEM_CONFIG)) < 0) + goto out; + + /* + * We'd like to have the config, but git doesn't particularly + * care if it's not there, so we need to deal with that. + * + * Further note that we only read the repository's own configuration. + * No other configuration should play a role here. Most importantly, we + * ignore conditional includes as those may cause a cyclic dependency + * between reading the format and evaluating the conditionals. This is + * for example the case with "onbranch" conditions. + */ + error = git_config_open_ondisk(&config, config_path.ptr); + if (error < 0 && error != GIT_ENOTFOUND) + goto out; + + if (config && + (error = check_repositoryformatversion(&version, config)) < 0) + goto out; + + if ((error = check_extensions(config, version)) < 0) + goto out; + + if (version > 0) { + if ((error = load_objectformat(repo, config)) < 0 || + (error = load_refstorage_format(repo, config)) < 0) + goto out; + } else { + repo->oid_type = GIT_OID_DEFAULT; + repo->refdb_type = GIT_REFDB_FILES; + } + +out: + git_str_dispose(&config_path); + git_config_free(config); + + return error; +} + +int git_repository_open_bare( + git_repository **repo_ptr, + const char *bare_path) +{ + git_str path = GIT_STR_INIT, common_path = GIT_STR_INIT; + git_repository *repo = NULL; + bool is_valid; + int error; + + if ((error = git_fs_path_prettify_dir(&path, bare_path, NULL)) < 0 || + (error = is_valid_repository_path(&is_valid, &path, &common_path, 0)) < 0) + return error; + + if (!is_valid) { + git_str_dispose(&path); + git_str_dispose(&common_path); + git_error_set(GIT_ERROR_REPOSITORY, "path is not a repository: %s", bare_path); + return GIT_ENOTFOUND; + } + + repo = repository_alloc(); + GIT_ERROR_CHECK_ALLOC(repo); + + repo->gitdir = git_str_detach(&path); + GIT_ERROR_CHECK_ALLOC(repo->gitdir); + repo->commondir = git_str_detach(&common_path); + GIT_ERROR_CHECK_ALLOC(repo->commondir); + + /* of course we're bare! */ + repo->is_bare = 1; + repo->is_worktree = 0; + repo->workdir = NULL; + + if ((error = read_repository_format(repo)) < 0) + goto cleanup; + + *repo_ptr = repo; + +cleanup: + return error; +} + +static int repo_load_namespace(git_repository *repo) +{ + git_str namespace_buf = GIT_STR_INIT; + int error; + + if (!repo->use_env) + return 0; + + error = git__getenv(&namespace_buf, "GIT_NAMESPACE"); + + if (error == 0) + repo->namespace = git_str_detach(&namespace_buf); + else if (error != GIT_ENOTFOUND) + return error; + + return 0; +} + +static int repo_is_worktree(unsigned *out, const git_repository *repo) +{ + git_str gitdir_link = GIT_STR_INIT; + int error; + + /* Worktrees cannot have the same commondir and gitdir */ + if (repo->commondir && repo->gitdir + && !strcmp(repo->commondir, repo->gitdir)) { + *out = 0; + return 0; + } + + if ((error = git_str_joinpath(&gitdir_link, repo->gitdir, "gitdir")) < 0) + return -1; + + /* A 'gitdir' file inside a git directory is currently + * only used when the repository is a working tree. */ + *out = !!git_fs_path_exists(gitdir_link.ptr); + + git_str_dispose(&gitdir_link); + return error; +} + +int git_repository_open_ext( + git_repository **repo_ptr, + const char *start_path, + unsigned int flags, + const char *ceiling_dirs) +{ + struct repo_paths paths = { GIT_STR_INIT }; + git_repository *repo = NULL; + git_config *config = NULL; + unsigned is_worktree; + int error; + + if (repo_ptr) + *repo_ptr = NULL; + + error = find_repo(&paths, start_path, ceiling_dirs, flags); + + if (error < 0 || !repo_ptr) + goto cleanup; + + repo = repository_alloc(); + GIT_ERROR_CHECK_ALLOC(repo); + + repo->use_env = !!(flags & GIT_REPOSITORY_OPEN_FROM_ENV); + + repo->gitdir = git_str_detach(&paths.gitdir); + GIT_ERROR_CHECK_ALLOC(repo->gitdir); + + if (paths.gitlink.size) { + repo->gitlink = git_str_detach(&paths.gitlink); + GIT_ERROR_CHECK_ALLOC(repo->gitlink); + } + if (paths.commondir.size) { + repo->commondir = git_str_detach(&paths.commondir); + GIT_ERROR_CHECK_ALLOC(repo->commondir); + } + + if ((error = repo_is_worktree(&is_worktree, repo)) < 0) + goto cleanup; + + repo->is_worktree = is_worktree; + + error = read_repository_format(repo); + if (error < 0) + goto cleanup; + + if ((error = load_grafts(repo)) < 0) + goto cleanup; + + if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0) { + repo->is_bare = 1; + } else { + error = git_repository_config_snapshot(&config, repo); + if (error < 0 && error != GIT_ENOTFOUND) + goto cleanup; + + if (config && + ((error = load_config_data(repo, config)) < 0 || + (error = load_workdir(repo, config, &paths.workdir)) < 0)) + goto cleanup; + } + + if ((error = repo_load_namespace(repo)) < 0) + goto cleanup; + + /* + * Ensure that the git directory and worktree are + * owned by the current user. + */ + if (git_repository__validate_ownership && + (error = validate_ownership(repo)) < 0) + goto cleanup; + +cleanup: + repo_paths_dispose(&paths); + git_config_free(config); + + if (error < 0) + git_repository_free(repo); + else if (repo_ptr) + *repo_ptr = repo; + + return error; +} + +int git_repository_open(git_repository **repo_out, const char *path) +{ + return git_repository_open_ext( + repo_out, path, GIT_REPOSITORY_OPEN_NO_SEARCH, NULL); +} + +int git_repository_open_from_worktree(git_repository **repo_out, git_worktree *wt) +{ + git_str path = GIT_STR_INIT; + git_repository *repo = NULL; + size_t len; + int err; + + GIT_ASSERT_ARG(repo_out); + GIT_ASSERT_ARG(wt); + + *repo_out = NULL; + len = strlen(wt->gitlink_path); + + if (len <= 4 || strcasecmp(wt->gitlink_path + len - 4, ".git")) { + err = -1; + goto out; + } + + if ((err = git_str_set(&path, wt->gitlink_path, len - 4)) < 0) + goto out; + + if ((err = git_repository_open(&repo, path.ptr)) < 0) + goto out; + + *repo_out = repo; + +out: + git_str_dispose(&path); + + return err; +} + +int git_repository_wrap_odb(git_repository **out, git_odb *odb) +{ + git_repository *repo; + + repo = repository_alloc(); + GIT_ERROR_CHECK_ALLOC(repo); + + GIT_ASSERT(git_oid_type_is_valid(odb->options.oid_type)); + repo->oid_type = odb->options.oid_type; + + /* + * This is a bit dirty, as this repository doesn't really have a refdb + * in the first place. But we do expect that we can create an "empty" + * ref iterator from such a repository, and things keep on working like + * this. + * + * It might make sense to eventually create an "in-memory" refdb type + * to serve this purpose. + */ + repo->refdb_type = GIT_REFDB_FILES; + + git_repository_set_odb(repo, odb); + *out = repo; + + return 0; +} + +int git_repository_discover( + git_buf *out, + const char *start_path, + int across_fs, + const char *ceiling_dirs) +{ + struct repo_paths paths = { GIT_STR_INIT }; + uint32_t flags = across_fs ? GIT_REPOSITORY_OPEN_CROSS_FS : 0; + int error; + + GIT_ASSERT_ARG(start_path); + + if ((error = find_repo(&paths, start_path, ceiling_dirs, flags)) == 0) + error = git_buf_fromstr(out, &paths.gitdir); + + repo_paths_dispose(&paths); + return error; +} + +static int has_config_worktree(bool *out, git_config *cfg) +{ + int worktreeconfig = 0, error; + + *out = false; + + error = git_config_get_bool(&worktreeconfig, cfg, "extensions.worktreeconfig"); + + if (error == 0) + *out = worktreeconfig; + else if (error == GIT_ENOTFOUND) + *out = false; + else + return error; + + return 0; +} + +static int load_config( + git_config **out, + git_repository *repo, + const char *global_config_path, + const char *xdg_config_path, + const char *system_config_path, + const char *programdata_path) +{ + git_str config_path = GIT_STR_INIT; + git_config *cfg = NULL; + git_config_level_t write_order; + bool has_worktree; + int error; + + GIT_ASSERT_ARG(out); + + if ((error = git_config_new(&cfg)) < 0) + return error; + + if (repo) { + if ((error = git_repository__item_path(&config_path, repo, GIT_REPOSITORY_ITEM_CONFIG)) == 0) + error = git_config_add_file_ondisk(cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, repo, 0); + + if (error && error != GIT_ENOTFOUND) + goto on_error; + + if ((error = has_config_worktree(&has_worktree, cfg)) == 0 && + has_worktree && + (error = git_repository__item_path(&config_path, repo, GIT_REPOSITORY_ITEM_WORKTREE_CONFIG)) == 0) + error = git_config_add_file_ondisk(cfg, config_path.ptr, GIT_CONFIG_LEVEL_WORKTREE, repo, 0); + + if (error && error != GIT_ENOTFOUND) + goto on_error; + + git_str_dispose(&config_path); + } + + if (global_config_path != NULL && + (error = git_config_add_file_ondisk( + cfg, global_config_path, GIT_CONFIG_LEVEL_GLOBAL, repo, 0)) < 0 && + error != GIT_ENOTFOUND) + goto on_error; + + if (xdg_config_path != NULL && + (error = git_config_add_file_ondisk( + cfg, xdg_config_path, GIT_CONFIG_LEVEL_XDG, repo, 0)) < 0 && + error != GIT_ENOTFOUND) + goto on_error; + + if (system_config_path != NULL && + (error = git_config_add_file_ondisk( + cfg, system_config_path, GIT_CONFIG_LEVEL_SYSTEM, repo, 0)) < 0 && + error != GIT_ENOTFOUND) + goto on_error; + + if (programdata_path != NULL && + (error = git_config_add_file_ondisk( + cfg, programdata_path, GIT_CONFIG_LEVEL_PROGRAMDATA, repo, 0)) < 0 && + error != GIT_ENOTFOUND) + goto on_error; + + git_error_clear(); /* clear any lingering ENOTFOUND errors */ + + write_order = GIT_CONFIG_LEVEL_LOCAL; + + if ((error = git_config_set_writeorder(cfg, &write_order, 1)) < 0) + goto on_error; + + *out = cfg; + return 0; + +on_error: + git_str_dispose(&config_path); + git_config_free(cfg); + *out = NULL; + return error; +} + +static const char *path_unless_empty(git_str *buf) +{ + return git_str_len(buf) > 0 ? git_str_cstr(buf) : NULL; +} + +GIT_INLINE(int) config_path_system(git_str *out, bool use_env) +{ + if (use_env) { + git_str no_system_buf = GIT_STR_INIT; + int no_system = 0; + int error; + + error = git__getenv(&no_system_buf, "GIT_CONFIG_NOSYSTEM"); + + if (error && error != GIT_ENOTFOUND) + return error; + + error = git_config_parse_bool(&no_system, no_system_buf.ptr); + git_str_dispose(&no_system_buf); + + if (no_system) + return 0; + + error = git__getenv(out, "GIT_CONFIG_SYSTEM"); + + if (error == 0 || error != GIT_ENOTFOUND) + return 0; + } + + git_config__find_system(out); + return 0; +} + +GIT_INLINE(int) config_path_global(git_str *out, bool use_env) +{ + if (use_env) { + int error = git__getenv(out, "GIT_CONFIG_GLOBAL"); + + if (error == 0 || error != GIT_ENOTFOUND) + return 0; + } + + git_config__find_global(out); + return 0; +} + +int git_repository_config__weakptr(git_config **out, git_repository *repo) +{ + int error = 0; + + if (repo->_config == NULL) { + git_str system_buf = GIT_STR_INIT; + git_str global_buf = GIT_STR_INIT; + git_str xdg_buf = GIT_STR_INIT; + git_str programdata_buf = GIT_STR_INIT; + bool use_env = repo->use_env; + git_config *config = NULL; + + if (!(error = config_path_system(&system_buf, use_env)) && + !(error = config_path_global(&global_buf, use_env))) { + git_config__find_xdg(&xdg_buf); + git_config__find_programdata(&programdata_buf); + } + + if (!error) { + /* + * If there is no global file, open a backend + * for it anyway. + */ + if (git_str_len(&global_buf) == 0) + git_config__global_location(&global_buf); + + error = load_config( + &config, repo, + path_unless_empty(&global_buf), + path_unless_empty(&xdg_buf), + path_unless_empty(&system_buf), + path_unless_empty(&programdata_buf)); + } + + if (!error) { + GIT_REFCOUNT_OWN(config, repo); + + if (git_atomic_compare_and_swap(&repo->_config, NULL, config) != NULL) { + GIT_REFCOUNT_OWN(config, NULL); + git_config_free(config); + } + } + + git_str_dispose(&global_buf); + git_str_dispose(&xdg_buf); + git_str_dispose(&system_buf); + git_str_dispose(&programdata_buf); + } + + *out = repo->_config; + return error; +} + +int git_repository_config(git_config **out, git_repository *repo) +{ + if (git_repository_config__weakptr(out, repo) < 0) + return -1; + + GIT_REFCOUNT_INC(*out); + return 0; +} + +int git_repository_config_snapshot(git_config **out, git_repository *repo) +{ + int error; + git_config *weak; + + if ((error = git_repository_config__weakptr(&weak, repo)) < 0) + return error; + + return git_config_snapshot(out, weak); +} + +int git_repository_set_config(git_repository *repo, git_config *config) +{ + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(config); + + set_config(repo, config); + return 0; +} + +static int repository_odb_path(git_str *out, git_repository *repo) +{ + int error = GIT_ENOTFOUND; + + if (repo->use_env) + error = git__getenv(out, "GIT_OBJECT_DIRECTORY"); + + if (error == GIT_ENOTFOUND) + error = git_repository__item_path(out, repo, + GIT_REPOSITORY_ITEM_OBJECTS); + + return error; +} + +static int repository_odb_alternates( + git_odb *odb, + git_repository *repo) +{ + git_str alternates = GIT_STR_INIT; + char *sep, *alt; + int error; + + if (!repo->use_env) + return 0; + + error = git__getenv(&alternates, "GIT_ALTERNATE_OBJECT_DIRECTORIES"); + + if (error != 0) + return (error == GIT_ENOTFOUND) ? 0 : error; + + alt = alternates.ptr; + + while (*alt) { + sep = strchr(alt, GIT_PATH_LIST_SEPARATOR); + + if (sep) + *sep = '\0'; + + error = git_odb_add_disk_alternate(odb, alt); + + if (sep) + alt = sep + 1; + else + break; + } + + git_str_dispose(&alternates); + return 0; +} + +int git_repository_odb__weakptr(git_odb **out, git_repository *repo) +{ + int error = 0; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(out); + + *out = git_atomic_load(repo->_odb); + if (*out == NULL) { + git_str odb_path = GIT_STR_INIT; + git_odb_options odb_opts = GIT_ODB_OPTIONS_INIT; + git_odb *odb; + + odb_opts.oid_type = repo->oid_type; + + if ((error = repository_odb_path(&odb_path, repo)) < 0 || + (error = git_odb_new_ext(&odb, &odb_opts)) < 0 || + (error = repository_odb_alternates(odb, repo)) < 0) + return error; + + GIT_REFCOUNT_OWN(odb, repo); + + if ((error = git_odb__set_caps(odb, GIT_ODB_CAP_FROM_OWNER)) < 0 || + (error = git_odb__add_default_backends(odb, odb_path.ptr, 0, 0)) < 0) { + git_odb_free(odb); + return error; + } + + if (git_atomic_compare_and_swap(&repo->_odb, NULL, odb) != NULL) { + GIT_REFCOUNT_OWN(odb, NULL); + git_odb_free(odb); + } + + git_str_dispose(&odb_path); + *out = git_atomic_load(repo->_odb); + } + + return error; +} + +int git_repository_odb(git_odb **out, git_repository *repo) +{ + if (git_repository_odb__weakptr(out, repo) < 0) + return -1; + + GIT_REFCOUNT_INC(*out); + return 0; +} + +int git_repository_set_odb(git_repository *repo, git_odb *odb) +{ + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(odb); + + set_odb(repo, odb); + return 0; +} + +int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo) +{ + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + if (repo->_refdb == NULL) { + git_refdb *refdb; + + error = git_refdb_open(&refdb, repo); + if (!error) { + GIT_REFCOUNT_OWN(refdb, repo); + + if (git_atomic_compare_and_swap(&repo->_refdb, NULL, refdb) != NULL) { + GIT_REFCOUNT_OWN(refdb, NULL); + git_refdb_free(refdb); + } + } + } + + *out = repo->_refdb; + return error; +} + +int git_repository_refdb(git_refdb **out, git_repository *repo) +{ + if (git_repository_refdb__weakptr(out, repo) < 0) + return -1; + + GIT_REFCOUNT_INC(*out); + return 0; +} + +int git_repository_set_refdb(git_repository *repo, git_refdb *refdb) +{ + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(refdb); + + set_refdb(repo, refdb); + return 0; +} + +static int repository_index_path(git_str *out, git_repository *repo) +{ + int error = GIT_ENOTFOUND; + + if (repo->use_env) + error = git__getenv(out, "GIT_INDEX_FILE"); + + if (error == GIT_ENOTFOUND) + error = git_repository__item_path(out, repo, + GIT_REPOSITORY_ITEM_INDEX); + + return error; +} + +int git_repository_index__weakptr(git_index **out, git_repository *repo) +{ + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + if (repo->_index == NULL) { + git_str index_path = GIT_STR_INIT; + git_index *index; + git_index_options index_opts = GIT_INDEX_OPTIONS_INIT; + + if ((error = repository_index_path(&index_path, repo)) < 0) + return error; + + index_opts.oid_type = repo->oid_type; + error = git_index_open_ext(&index, index_path.ptr, &index_opts); + + if (!error) { + GIT_REFCOUNT_OWN(index, repo); + + if (git_atomic_compare_and_swap(&repo->_index, NULL, index) != NULL) { + GIT_REFCOUNT_OWN(index, NULL); + git_index_free(index); + } + + error = git_index_set_caps(repo->_index, + GIT_INDEX_CAPABILITY_FROM_OWNER); + } + + git_str_dispose(&index_path); + } + + *out = repo->_index; + return error; +} + +int git_repository_index(git_index **out, git_repository *repo) +{ + if (git_repository_index__weakptr(out, repo) < 0) + return -1; + + GIT_REFCOUNT_INC(*out); + return 0; +} + +int git_repository_set_index(git_repository *repo, git_index *index) +{ + GIT_ASSERT_ARG(repo); + set_index(repo, index); + return 0; +} + +int git_repository_grafts__weakptr(git_grafts **out, git_repository *repo) +{ + GIT_ASSERT_ARG(out && repo); + GIT_ASSERT(repo->grafts); + *out = repo->grafts; + return 0; +} + +int git_repository_shallow_grafts__weakptr(git_grafts **out, git_repository *repo) +{ + GIT_ASSERT_ARG(out && repo); + GIT_ASSERT(repo->shallow_grafts); + *out = repo->shallow_grafts; + return 0; +} + +int git_repository_set_namespace(git_repository *repo, const char *namespace) +{ + git__free(repo->namespace); + + if (namespace == NULL) { + repo->namespace = NULL; + return 0; + } + + return (repo->namespace = git__strdup(namespace)) ? 0 : -1; +} + +const char *git_repository_get_namespace(git_repository *repo) +{ + return repo->namespace; +} + +#ifdef GIT_WIN32 +static int reserved_names_add8dot3(git_repository *repo, const char *path) +{ + char *name = git_win32_path_8dot3_name(path); + const char *def = GIT_DIR_SHORTNAME; + const char *def_dot_git = DOT_GIT; + size_t name_len, def_len = CONST_STRLEN(GIT_DIR_SHORTNAME); + size_t def_dot_git_len = CONST_STRLEN(DOT_GIT); + git_str *buf; + + if (!name) + return 0; + + name_len = strlen(name); + + if ((name_len == def_len && memcmp(name, def, def_len) == 0) || + (name_len == def_dot_git_len && memcmp(name, def_dot_git, def_dot_git_len) == 0)) { + git__free(name); + return 0; + } + + if ((buf = git_array_alloc(repo->reserved_names)) == NULL) + return -1; + + git_str_attach(buf, name, name_len); + return true; +} + +bool git_repository__reserved_names( + git_str **out, size_t *outlen, git_repository *repo, bool include_ntfs) +{ + GIT_UNUSED(include_ntfs); + + if (repo->reserved_names.size == 0) { + git_str *buf; + size_t i; + + /* Add the static defaults */ + for (i = 0; i < git_repository__reserved_names_win32_len; i++) { + if ((buf = git_array_alloc(repo->reserved_names)) == NULL) + goto on_error; + + buf->ptr = git_repository__reserved_names_win32[i].ptr; + buf->size = git_repository__reserved_names_win32[i].size; + } + + /* Try to add any repo-specific reserved names - the gitlink file + * within a submodule or the repository (if the repository directory + * is beneath the workdir). These are typically `.git`, but should + * be protected in case they are not. Note, repo and workdir paths + * are always prettified to end in `/`, so a prefixcmp is safe. + */ + if (!repo->is_bare) { + int (*prefixcmp)(const char *, const char *); + int error, ignorecase; + + error = git_repository__configmap_lookup( + &ignorecase, repo, GIT_CONFIGMAP_IGNORECASE); + prefixcmp = (error || ignorecase) ? git__prefixcmp_icase : + git__prefixcmp; + + if (repo->gitlink && + reserved_names_add8dot3(repo, repo->gitlink) < 0) + goto on_error; + + if (repo->gitdir && + prefixcmp(repo->gitdir, repo->workdir) == 0 && + reserved_names_add8dot3(repo, repo->gitdir) < 0) + goto on_error; + } + } + + *out = repo->reserved_names.ptr; + *outlen = repo->reserved_names.size; + + return true; + + /* Always give good defaults, even on OOM */ +on_error: + *out = git_repository__reserved_names_win32; + *outlen = git_repository__reserved_names_win32_len; + + return false; +} +#else +bool git_repository__reserved_names( + git_str **out, size_t *outlen, git_repository *repo, bool include_ntfs) +{ + GIT_UNUSED(repo); + + if (include_ntfs) { + *out = git_repository__reserved_names_win32; + *outlen = git_repository__reserved_names_win32_len; + } else { + *out = git_repository__reserved_names_posix; + *outlen = git_repository__reserved_names_posix_len; + } + + return true; +} +#endif + +static int check_repositoryformatversion(int *version, git_config *config) +{ + int error; + + error = git_config_get_int32(version, config, "core.repositoryformatversion"); + + /* git ignores this if the config variable isn't there */ + if (error == GIT_ENOTFOUND) + return 0; + + if (error < 0) + return -1; + + if (*version < 0) { + git_error_set(GIT_ERROR_REPOSITORY, + "invalid repository version %d", *version); + } + + if (GIT_REPO_VERSION_MAX < *version) { + git_error_set(GIT_ERROR_REPOSITORY, + "unsupported repository version %d; only versions up to %d are supported", + *version, GIT_REPO_VERSION_MAX); + return -1; + } + + return 0; +} + +static const char *builtin_extensions[] = { + "noop", + "objectformat", + "worktreeconfig", + "preciousobjects", + "refstorage", +}; + +static git_vector user_extensions = { 0, git__strcmp_cb }; + +static int check_valid_extension(const git_config_entry *entry, void *payload) +{ + git_str cfg = GIT_STR_INIT; + bool reject; + const char *extension; + size_t i; + int error = 0; + + GIT_UNUSED(payload); + + git_vector_foreach (&user_extensions, i, extension) { + git_str_clear(&cfg); + + /* + * Users can specify that they don't want to support an + * extension with a '!' prefix. + */ + if ((reject = (extension[0] == '!')) == true) + extension = &extension[1]; + + if ((error = git_str_printf(&cfg, "extensions.%s", extension)) < 0) + goto done; + + if (strcmp(entry->name, cfg.ptr) == 0) { + if (reject) + goto fail; + + goto done; + } + } + + for (i = 0; i < ARRAY_SIZE(builtin_extensions); i++) { + git_str_clear(&cfg); + extension = builtin_extensions[i]; + + if ((error = git_str_printf(&cfg, "extensions.%s", extension)) < 0) + goto done; + + if (strcmp(entry->name, cfg.ptr) == 0) + goto done; + } + +fail: + git_error_set(GIT_ERROR_REPOSITORY, "unsupported extension name %s", entry->name); + error = -1; + +done: + git_str_dispose(&cfg); + return error; +} + +static int check_extensions(git_config *config, int version) +{ + if (version < 1) + return 0; + + return git_config_foreach_match(config, "^extensions\\.", check_valid_extension, NULL); +} + +static int load_objectformat(git_repository *repo, git_config *config) +{ + git_config_entry *entry = NULL; + int error; + + if ((error = git_config_get_entry(&entry, config, "extensions.objectformat")) < 0) { + if (error == GIT_ENOTFOUND) { + repo->oid_type = GIT_OID_DEFAULT; + git_error_clear(); + error = 0; + } + + goto done; + } + + if ((repo->oid_type = git_oid_type_fromstr(entry->value)) == 0) { + git_error_set(GIT_ERROR_REPOSITORY, + "unknown object format '%s'", entry->value); + error = GIT_EINVALID; + } + +done: + git_config_entry_free(entry); + return error; +} + +int git_repository__set_objectformat( + git_repository *repo, + git_oid_t oid_type) +{ + git_config *cfg; + + /* + * Older clients do not necessarily understand the + * `objectformat` extension, even when it's set to an + * object format that they understand (SHA1). Do not set + * the objectformat extension unless we're not using the + * default object format. + */ + if (oid_type == GIT_OID_DEFAULT) + return 0; + + if (!git_repository_is_empty(repo) && repo->oid_type != oid_type) { + git_error_set(GIT_ERROR_REPOSITORY, + "cannot change object id type of existing repository"); + return -1; + } + + if (git_repository_config__weakptr(&cfg, repo) < 0) + return -1; + + if (git_config_set_int32(cfg, + "core.repositoryformatversion", 1) < 0 || + git_config_set_string(cfg, "extensions.objectformat", + git_oid_type_name(oid_type)) < 0) + return -1; + + /* + * During repo init, we may create some backends with the + * default oid type. Clear them so that we create them with + * the proper oid type. + */ + if (repo->oid_type != oid_type) { + set_index(repo, NULL); + set_odb(repo, NULL); + set_refdb(repo, NULL); + + repo->oid_type = oid_type; + } + + return 0; +} + +static int load_refstorage_format(git_repository *repo, git_config *config) +{ + git_config_entry *entry = NULL; + int error; + + if ((error = git_config_get_entry(&entry, config, "extensions.refstorage")) < 0) { + if (error == GIT_ENOTFOUND) { + repo->refdb_type = GIT_REFDB_FILES; + git_error_clear(); + error = 0; + } + + goto done; + } + + if ((repo->refdb_type = git_refdb_type_fromstr(entry->value)) == 0) { + git_error_set(GIT_ERROR_REPOSITORY, + "unknown refstorage format '%s'", entry->value); + error = GIT_EINVALID; + } + +done: + git_config_entry_free(entry); + return error; +} + +int git_repository__extensions(char ***out, size_t *out_len) +{ + git_vector extensions; + const char *builtin, *user; + char *extension; + size_t i, j; + + if (git_vector_init(&extensions, 8, git__strcmp_cb) < 0) + return -1; + + for (i = 0; i < ARRAY_SIZE(builtin_extensions); i++) { + bool match = false; + + builtin = builtin_extensions[i]; + + git_vector_foreach (&user_extensions, j, user) { + if (user[0] == '!' && strcmp(builtin, &user[1]) == 0) { + match = true; + break; + } + } + + if (match) + continue; + + if ((extension = git__strdup(builtin)) == NULL || + git_vector_insert(&extensions, extension) < 0) + return -1; + } + + git_vector_foreach (&user_extensions, i, user) { + if (user[0] == '!') + continue; + + if ((extension = git__strdup(user)) == NULL || + git_vector_insert(&extensions, extension) < 0) + return -1; + } + + git_vector_sort(&extensions); + + *out = (char **)git_vector_detach(out_len, NULL, &extensions); + return 0; +} + +static int dup_ext_err(void **old, void *extension) +{ + GIT_UNUSED(old); + GIT_UNUSED(extension); + return GIT_EEXISTS; +} + +int git_repository__set_extensions(const char **extensions, size_t len) +{ + char *extension; + size_t i, j; + int error; + + git_repository__free_extensions(); + + for (i = 0; i < len; i++) { + bool is_builtin = false; + + for (j = 0; j < ARRAY_SIZE(builtin_extensions); j++) { + if (strcmp(builtin_extensions[j], extensions[i]) == 0) { + is_builtin = true; + break; + } + } + + if (is_builtin) + continue; + + if ((extension = git__strdup(extensions[i])) == NULL) + return -1; + + if ((error = git_vector_insert_sorted(&user_extensions, extension, dup_ext_err)) < 0) { + git__free(extension); + + if (error != GIT_EEXISTS) + return -1; + } + } + + return 0; +} + +void git_repository__free_extensions(void) +{ + git_vector_dispose_deep(&user_extensions); +} + +static bool is_chmod_supported(const char *file_path) +{ + struct stat st1, st2; + + if (p_stat(file_path, &st1) < 0) + return false; + + if (p_chmod(file_path, st1.st_mode ^ S_IXUSR) < 0) + return false; + + if (p_stat(file_path, &st2) < 0) + return false; + + return (st1.st_mode != st2.st_mode); +} + +static bool is_filesystem_case_insensitive(const char *gitdir_path) +{ + git_str path = GIT_STR_INIT; + int is_insensitive = -1; + + if (!git_str_joinpath(&path, gitdir_path, "CoNfIg")) + is_insensitive = git_fs_path_exists(git_str_cstr(&path)); + + git_str_dispose(&path); + return is_insensitive; +} + +/* + * Return a configuration object with only the global and system + * configurations; no repository-level configuration. + */ +static int load_global_config(git_config **config, bool use_env) +{ + git_str global_buf = GIT_STR_INIT; + git_str xdg_buf = GIT_STR_INIT; + git_str system_buf = GIT_STR_INIT; + git_str programdata_buf = GIT_STR_INIT; + int error; + + if (!(error = config_path_system(&system_buf, use_env)) && + !(error = config_path_global(&global_buf, use_env))) { + git_config__find_xdg(&xdg_buf); + git_config__find_programdata(&programdata_buf); + + error = load_config(config, NULL, + path_unless_empty(&global_buf), + path_unless_empty(&xdg_buf), + path_unless_empty(&system_buf), + path_unless_empty(&programdata_buf)); + } + + git_str_dispose(&global_buf); + git_str_dispose(&xdg_buf); + git_str_dispose(&system_buf); + git_str_dispose(&programdata_buf); + + return error; +} + +static bool are_symlinks_supported(const char *wd_path, bool use_env) +{ + git_config *config = NULL; + int symlinks = 0; + + /* + * To emulate Git for Windows, symlinks on Windows must be explicitly + * opted-in. We examine the system configuration for a core.symlinks + * set to true. If found, we then examine the filesystem to see if + * symlinks are _actually_ supported by the current user. If that is + * _not_ set, then we do not test or enable symlink support. + */ +#ifdef GIT_WIN32 + if (load_global_config(&config, use_env) < 0 || + git_config_get_bool(&symlinks, config, "core.symlinks") < 0 || + !symlinks) + goto done; +#else + GIT_UNUSED(use_env); +#endif + + if (!(symlinks = git_fs_path_supports_symlinks(wd_path))) + goto done; + +done: + git_config_free(config); + return symlinks != 0; +} + +static int create_empty_file(const char *path, mode_t mode) +{ + int fd; + + if ((fd = p_creat(path, mode)) < 0) { + git_error_set(GIT_ERROR_OS, "error while creating '%s'", path); + return -1; + } + + if (p_close(fd) < 0) { + git_error_set(GIT_ERROR_OS, "error while closing '%s'", path); + return -1; + } + + return 0; +} + +static int repo_local_config( + git_config **out, + git_str *config_dir, + git_repository *repo, + const char *repo_dir) +{ + int error = 0; + git_config *parent; + const char *cfg_path; + + if (git_str_joinpath(config_dir, repo_dir, GIT_CONFIG_FILENAME_INREPO) < 0) + return -1; + cfg_path = git_str_cstr(config_dir); + + /* make LOCAL config if missing */ + if (!git_fs_path_isfile(cfg_path) && + (error = create_empty_file(cfg_path, GIT_CONFIG_FILE_MODE)) < 0) + return error; + + /* if no repo, just open that file directly */ + if (!repo) + return git_config_open_ondisk(out, cfg_path); + + /* otherwise, open parent config and get that level */ + if ((error = git_repository_config__weakptr(&parent, repo)) < 0) + return error; + + if (git_config_open_level(out, parent, GIT_CONFIG_LEVEL_LOCAL) < 0) { + git_error_clear(); + + if (!(error = git_config_add_file_ondisk( + parent, cfg_path, GIT_CONFIG_LEVEL_LOCAL, repo, false))) + error = git_config_open_level(out, parent, GIT_CONFIG_LEVEL_LOCAL); + } + + git_config_free(parent); + + return error; +} + +static int repo_init_fs_configs( + git_config *cfg, + const char *cfg_path, + const char *repo_dir, + const char *work_dir, + bool update_ignorecase, + bool use_env) +{ + int error = 0; + + if (!work_dir) + work_dir = repo_dir; + + if ((error = git_config_set_bool( + cfg, "core.filemode", is_chmod_supported(cfg_path))) < 0) + return error; + + if (!are_symlinks_supported(work_dir, use_env)) { + if ((error = git_config_set_bool(cfg, "core.symlinks", false)) < 0) + return error; + } else if (git_config_delete_entry(cfg, "core.symlinks") < 0) + git_error_clear(); + + if (update_ignorecase) { + if (is_filesystem_case_insensitive(repo_dir)) { + if ((error = git_config_set_bool(cfg, "core.ignorecase", true)) < 0) + return error; + } else if (git_config_delete_entry(cfg, "core.ignorecase") < 0) + git_error_clear(); + } + +#ifdef GIT_I18N_ICONV + if ((error = git_config_set_bool( + cfg, "core.precomposeunicode", + git_fs_path_does_decompose_unicode(work_dir))) < 0) + return error; + /* on non-iconv platforms, don't even set core.precomposeunicode */ +#endif + + return 0; +} + +static int repo_init_config( + const char *repo_dir, + const char *work_dir, + uint32_t flags, + uint32_t mode, + git_oid_t oid_type, + git_refdb_t refdb_type) +{ + int error = 0; + git_str cfg_path = GIT_STR_INIT, worktree_path = GIT_STR_INIT; + git_config *config = NULL; + bool is_bare = ((flags & GIT_REPOSITORY_INIT_BARE) != 0); + bool is_reinit = ((flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0); + bool use_env = ((flags & GIT_REPOSITORY_OPEN_FROM_ENV) != 0); + int version = GIT_REPO_VERSION_DEFAULT; + + if ((error = repo_local_config(&config, &cfg_path, NULL, repo_dir)) < 0) + goto cleanup; + + if (is_reinit && + (error = check_repositoryformatversion(&version, config)) < 0) + goto cleanup; + + if ((error = check_extensions(config, version)) < 0) + goto cleanup; + +#define SET_REPO_CONFIG(TYPE, NAME, VAL) do { \ + if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \ + goto cleanup; } while (0) + + SET_REPO_CONFIG(bool, "core.bare", is_bare); + SET_REPO_CONFIG(int32, "core.repositoryformatversion", version); + + if ((error = repo_init_fs_configs( + config, cfg_path.ptr, repo_dir, work_dir, + !is_reinit, use_env)) < 0) + goto cleanup; + + if (!is_bare) { + SET_REPO_CONFIG(bool, "core.logallrefupdates", true); + + if (!(flags & GIT_REPOSITORY_INIT__NATURAL_WD)) { + if ((error = git_str_sets(&worktree_path, work_dir)) < 0) + goto cleanup; + + if ((flags & GIT_REPOSITORY_INIT_RELATIVE_GITLINK)) + if ((error = git_fs_path_make_relative(&worktree_path, repo_dir)) < 0) + goto cleanup; + + SET_REPO_CONFIG(string, "core.worktree", worktree_path.ptr); + } else if (is_reinit) { + if (git_config_delete_entry(config, "core.worktree") < 0) + git_error_clear(); + } + } + + if (mode == GIT_REPOSITORY_INIT_SHARED_GROUP) { + SET_REPO_CONFIG(int32, "core.sharedrepository", 1); + SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true); + } + else if (mode == GIT_REPOSITORY_INIT_SHARED_ALL) { + SET_REPO_CONFIG(int32, "core.sharedrepository", 2); + SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true); + } + + if (oid_type != GIT_OID_DEFAULT) { + SET_REPO_CONFIG(int32, "core.repositoryformatversion", 1); + SET_REPO_CONFIG(string, "extensions.objectformat", git_oid_type_name(oid_type)); + } + + if (refdb_type != GIT_REFDB_FILES) { + SET_REPO_CONFIG(int32, "core.repositoryformatversion", 1); + SET_REPO_CONFIG(string, "extensions.refstorage", git_refdb_type_name(refdb_type)); + } + +cleanup: + git_str_dispose(&cfg_path); + git_str_dispose(&worktree_path); + git_config_free(config); + + return error; +} + +static int repo_reinit_submodule_fs(git_submodule *sm, const char *n, void *p) +{ + git_repository *smrepo = NULL; + GIT_UNUSED(n); GIT_UNUSED(p); + + if (git_submodule_open(&smrepo, sm) < 0 || + git_repository_reinit_filesystem(smrepo, true) < 0) + git_error_clear(); + git_repository_free(smrepo); + + return 0; +} + +int git_repository_reinit_filesystem(git_repository *repo, int recurse) +{ + int error = 0; + git_str path = GIT_STR_INIT; + git_config *config = NULL; + const char *repo_dir = git_repository_path(repo); + + if (!(error = repo_local_config(&config, &path, repo, repo_dir))) + error = repo_init_fs_configs(config, path.ptr, repo_dir, + git_repository_workdir(repo), true, repo->use_env); + + git_config_free(config); + git_str_dispose(&path); + + git_repository__configmap_lookup_cache_clear(repo); + + if (!repo->is_bare && recurse) + (void)git_submodule_foreach(repo, repo_reinit_submodule_fs, NULL); + + return error; +} + +static int repo_write_template( + const char *git_dir, + bool allow_overwrite, + const char *file, + mode_t mode, + bool hidden, + const char *content) +{ + git_str path = GIT_STR_INIT; + int fd, error = 0, flags; + + if (git_str_joinpath(&path, git_dir, file) < 0) + return -1; + + if (allow_overwrite) + flags = O_WRONLY | O_CREAT | O_TRUNC; + else + flags = O_WRONLY | O_CREAT | O_EXCL; + + fd = p_open(git_str_cstr(&path), flags, mode); + + if (fd >= 0) { + error = p_write(fd, content, strlen(content)); + + p_close(fd); + } + else if (errno != EEXIST) + error = fd; + +#ifdef GIT_WIN32 + if (!error && hidden) { + if (git_win32__set_hidden(path.ptr, true) < 0) + error = -1; + } +#else + GIT_UNUSED(hidden); +#endif + + git_str_dispose(&path); + + if (error) + git_error_set(GIT_ERROR_OS, + "failed to initialize repository with template '%s'", file); + + return error; +} + +static int repo_write_gitlink( + const char *in_dir, const char *to_repo, bool use_relative_path) +{ + int error; + git_str buf = GIT_STR_INIT; + git_str path_to_repo = GIT_STR_INIT; + struct stat st; + + git_fs_path_dirname_r(&buf, to_repo); + git_fs_path_to_dir(&buf); + if (git_str_oom(&buf)) + return -1; + + /* don't write gitlink to natural workdir */ + if (git__suffixcmp(to_repo, "/" DOT_GIT "/") == 0 && + strcmp(in_dir, buf.ptr) == 0) + { + error = GIT_PASSTHROUGH; + goto cleanup; + } + + if ((error = git_str_joinpath(&buf, in_dir, DOT_GIT)) < 0) + goto cleanup; + + if (!p_stat(buf.ptr, &st) && !S_ISREG(st.st_mode)) { + git_error_set(GIT_ERROR_REPOSITORY, + "cannot overwrite gitlink file into path '%s'", in_dir); + error = GIT_EEXISTS; + goto cleanup; + } + + git_str_clear(&buf); + + error = git_str_sets(&path_to_repo, to_repo); + + if (!error && use_relative_path) + error = git_fs_path_make_relative(&path_to_repo, in_dir); + + if (!error) + error = git_str_printf(&buf, "%s %s\n", GIT_FILE_CONTENT_PREFIX, path_to_repo.ptr); + + if (!error) + error = repo_write_template(in_dir, true, DOT_GIT, 0666, true, buf.ptr); + +cleanup: + git_str_dispose(&buf); + git_str_dispose(&path_to_repo); + return error; +} + +static mode_t pick_dir_mode(git_repository_init_options *opts) +{ + if (opts->mode == GIT_REPOSITORY_INIT_SHARED_UMASK) + return 0777; + if (opts->mode == GIT_REPOSITORY_INIT_SHARED_GROUP) + return (0775 | S_ISGID); + if (opts->mode == GIT_REPOSITORY_INIT_SHARED_ALL) + return (0777 | S_ISGID); + return opts->mode; +} + +#include "repo_template.h" + +static int repo_init_structure( + const char *repo_dir, + const char *work_dir, + git_repository_init_options *opts) +{ + int error = 0; + repo_template_item *tpl; + bool external_tpl = + opts->template_path != NULL || + (opts->flags & GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE) != 0; + mode_t dmode = pick_dir_mode(opts); + bool chmod = opts->mode != GIT_REPOSITORY_INIT_SHARED_UMASK; + + /* Hide the ".git" directory */ +#ifdef GIT_WIN32 + if ((opts->flags & GIT_REPOSITORY_INIT__HAS_DOTGIT) != 0) { + if (git_win32__set_hidden(repo_dir, true) < 0) { + git_error_set(GIT_ERROR_OS, + "failed to mark Git repository folder as hidden"); + return -1; + } + } +#endif + + /* Create the .git gitlink if appropriate */ + if ((opts->flags & GIT_REPOSITORY_INIT_BARE) == 0 && + (opts->flags & GIT_REPOSITORY_INIT__NATURAL_WD) == 0) + { + if (repo_write_gitlink(work_dir, repo_dir, opts->flags & GIT_REPOSITORY_INIT_RELATIVE_GITLINK) < 0) + return -1; + } + + /* Copy external template if requested */ + if (external_tpl) { + git_config *cfg = NULL; + const char *tdir = NULL; + bool default_template = false; + git_str template_buf = GIT_STR_INIT; + + if (opts->template_path) + tdir = opts->template_path; + else if ((error = git_config_open_default(&cfg)) >= 0) { + if (!git_config__get_path(&template_buf, cfg, "init.templatedir")) + tdir = template_buf.ptr; + git_error_clear(); + } + + if (!tdir) { + if (!(error = git_sysdir_find_template_dir(&template_buf))) + tdir = template_buf.ptr; + default_template = true; + } + + /* + * If tdir was the empty string, treat it like tdir was a path to an + * empty directory (so, don't do any copying). This is the behavior + * that git(1) exhibits, although it doesn't seem to be officially + * documented. + */ + if (tdir && git__strcmp(tdir, "") != 0) { + uint32_t cpflags = GIT_CPDIR_COPY_SYMLINKS | + GIT_CPDIR_SIMPLE_TO_MODE | + GIT_CPDIR_COPY_DOTFILES; + if (opts->mode != GIT_REPOSITORY_INIT_SHARED_UMASK) + cpflags |= GIT_CPDIR_CHMOD_DIRS; + error = git_futils_cp_r(tdir, repo_dir, cpflags, dmode); + } + + git_str_dispose(&template_buf); + git_config_free(cfg); + + /* If tdir does not exist, then do not error out. This matches the + * behaviour of git(1), which just prints a warning and continues. + * TODO: issue warning when warning API is available. + * `git` prints to stderr: 'warning: templates not found in /path/to/tdir' + */ + if (error < 0) { + if (!default_template && error != GIT_ENOTFOUND) + return error; + + /* if template was default, ignore error and use internal */ + git_error_clear(); + external_tpl = false; + error = 0; + } + } + + /* Copy internal template + * - always ensure existence of dirs + * - only create files if no external template was specified + */ + for (tpl = repo_template; !error && tpl->path; ++tpl) { + if (!tpl->content) { + uint32_t mkdir_flags = GIT_MKDIR_PATH; + if (chmod) + mkdir_flags |= GIT_MKDIR_CHMOD; + + error = git_futils_mkdir_relative( + tpl->path, repo_dir, dmode, mkdir_flags, NULL); + } + else if (!external_tpl) { + const char *content = tpl->content; + + if (opts->description && strcmp(tpl->path, GIT_DESC_FILE) == 0) + content = opts->description; + + error = repo_write_template( + repo_dir, false, tpl->path, tpl->mode, false, content); + } + } + + return error; +} + +static int mkdir_parent(git_str *buf, uint32_t mode, bool skip2) +{ + /* When making parent directories during repository initialization + * don't try to set gid or grant world write access + */ + return git_futils_mkdir( + buf->ptr, mode & ~(S_ISGID | 0002), + GIT_MKDIR_PATH | GIT_MKDIR_VERIFY_DIR | + (skip2 ? GIT_MKDIR_SKIP_LAST2 : GIT_MKDIR_SKIP_LAST)); +} + +static int repo_init_directories( + git_str *repo_path, + git_str *wd_path, + const char *given_repo, + git_repository_init_options *opts) +{ + int error = 0; + bool is_bare, add_dotgit, has_dotgit, natural_wd; + mode_t dirmode; + + /* There are three possible rules for what we are allowed to create: + * - MKPATH means anything we need + * - MKDIR means just the .git directory and its parent and the workdir + * - Neither means only the .git directory can be created + * + * There are 5 "segments" of path that we might need to deal with: + * 1. The .git directory + * 2. The parent of the .git directory + * 3. Everything above the parent of the .git directory + * 4. The working directory (often the same as #2) + * 5. Everything above the working directory (often the same as #3) + * + * For all directories created, we start with the init_mode value for + * permissions and then strip off bits in some cases: + * + * For MKPATH, we create #3 (and #5) paths without S_ISGID or S_IWOTH + * For MKPATH and MKDIR, we create #2 (and #4) without S_ISGID + * For all rules, we create #1 using the untouched init_mode + */ + + /* set up repo path */ + + is_bare = ((opts->flags & GIT_REPOSITORY_INIT_BARE) != 0); + + add_dotgit = + !is_bare && !opts->workdir_path && + git__suffixcmp(given_repo, "/" DOT_GIT) != 0 && + git__suffixcmp(given_repo, "/" GIT_DIR) != 0; + + if (git_str_joinpath(repo_path, given_repo, add_dotgit ? GIT_DIR : "") < 0) + return -1; + + git_fs_path_mkposix(repo_path->ptr); + + has_dotgit = (git__suffixcmp(repo_path->ptr, "/" GIT_DIR) == 0); + if (has_dotgit) + opts->flags |= GIT_REPOSITORY_INIT__HAS_DOTGIT; + + /* set up workdir path */ + + if (!is_bare) { + if (opts->workdir_path) { + if (git_fs_path_join_unrooted( + wd_path, opts->workdir_path, repo_path->ptr, NULL) < 0) + return -1; + } else if (has_dotgit) { + if (git_fs_path_dirname_r(wd_path, repo_path->ptr) < 0) + return -1; + } else { + git_error_set(GIT_ERROR_REPOSITORY, "cannot pick working directory" + " for non-bare repository that isn't a '.git' directory"); + return -1; + } + + if (git_fs_path_to_dir(wd_path) < 0) + return -1; + } else { + git_str_clear(wd_path); + } + + natural_wd = + has_dotgit && + wd_path->size > 0 && + wd_path->size + strlen(GIT_DIR) == repo_path->size && + memcmp(repo_path->ptr, wd_path->ptr, wd_path->size) == 0; + if (natural_wd) + opts->flags |= GIT_REPOSITORY_INIT__NATURAL_WD; + + /* create directories as needed / requested */ + + dirmode = pick_dir_mode(opts); + + if ((opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0) { + /* create path #5 */ + if (wd_path->size > 0 && + (error = mkdir_parent(wd_path, dirmode, false)) < 0) + return error; + + /* create path #3 (if not the same as #5) */ + if (!natural_wd && + (error = mkdir_parent(repo_path, dirmode, has_dotgit)) < 0) + return error; + } + + if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 || + (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0) + { + /* create path #4 */ + if (wd_path->size > 0 && + (error = git_futils_mkdir( + wd_path->ptr, dirmode & ~S_ISGID, + GIT_MKDIR_VERIFY_DIR)) < 0) + return error; + + /* create path #2 (if not the same as #4) */ + if (!natural_wd && + (error = git_futils_mkdir( + repo_path->ptr, dirmode & ~S_ISGID, + GIT_MKDIR_VERIFY_DIR | GIT_MKDIR_SKIP_LAST)) < 0) + return error; + } + + if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 || + (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0 || + has_dotgit) + { + /* create path #1 */ + error = git_futils_mkdir(repo_path->ptr, dirmode, + GIT_MKDIR_VERIFY_DIR | ((dirmode & S_ISGID) ? GIT_MKDIR_CHMOD : 0)); + } + + /* prettify both directories now that they are created */ + + if (!error) { + error = git_fs_path_prettify_dir(repo_path, repo_path->ptr, NULL); + + if (!error && wd_path->size > 0) + error = git_fs_path_prettify_dir(wd_path, wd_path->ptr, NULL); + } + + return error; +} + +static int get_initial_head(char **out, uint32_t *flags, + git_repository_init_options *opts) +{ + git_str cfg_branch = GIT_STR_INIT, prefixed = GIT_STR_INIT; + const char *initial_head = NULL; + git_config *cfg = NULL; + int error; + + if (opts->initial_head) { + initial_head = opts->initial_head; + *flags |= GIT_REFDB_BACKEND_INIT_FORCE_HEAD; + } else if (git_config_open_default(&cfg) >= 0 && + git_config__get_string_buf(&cfg_branch, cfg, "init.defaultbranch") >= 0 && + *cfg_branch.ptr) { + initial_head = cfg_branch.ptr; + } + + if (!initial_head) + initial_head = GIT_BRANCH_DEFAULT; + + if (git__prefixcmp(initial_head, GIT_REFS_DIR) != 0) { + git_str_printf(&prefixed, GIT_REFS_HEADS_DIR "%s", initial_head); + initial_head = prefixed.ptr; + } + + if ((*out = git__strdup(initial_head)) == NULL) { + error = -1; + goto out; + } + + error = 0; + +out: + git_str_dispose(&cfg_branch); + git_str_dispose(&prefixed); + git_config_free(cfg); + + return error; +} + +static int repo_init_create_origin(git_repository *repo, const char *url) +{ + int error; + git_remote *remote; + + if (!(error = git_remote_create(&remote, repo, GIT_REMOTE_ORIGIN, url))) { + git_remote_free(remote); + } + + return error; +} + +int git_repository_init( + git_repository **repo_out, const char *path, unsigned is_bare) +{ + git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; + + opts.flags = GIT_REPOSITORY_INIT_MKPATH; /* don't love this default */ + if (is_bare) + opts.flags |= GIT_REPOSITORY_INIT_BARE; + + return git_repository_init_ext(repo_out, path, &opts); +} + +int git_repository_init_ext( + git_repository **out, + const char *given_repo, + git_repository_init_options *opts) +{ + git_str repo_path = GIT_STR_INIT, wd_path = GIT_STR_INIT, + common_path = GIT_STR_INIT, path = GIT_STR_INIT; + const char *wd; + git_refdb *refdb; + bool is_valid; + git_oid_t oid_type = GIT_OID_DEFAULT; + git_refdb_t refdb_type = GIT_REFDB_FILES; + char *initial_head = NULL; + uint32_t refdb_init_flags = 0; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(given_repo); + GIT_ASSERT_ARG(opts); + + GIT_ERROR_CHECK_VERSION(opts, GIT_REPOSITORY_INIT_OPTIONS_VERSION, "git_repository_init_options"); + +#ifdef GIT_EXPERIMENTAL_SHA256 + if (opts->oid_type) + oid_type = opts->oid_type; +#endif + if (opts->refdb_type) + refdb_type = opts->refdb_type; + + if ((error = repo_init_directories(&repo_path, &wd_path, given_repo, opts)) < 0) + goto out; + + wd = (opts->flags & GIT_REPOSITORY_INIT_BARE) ? NULL : git_str_cstr(&wd_path); + + if ((error = is_valid_repository_path(&is_valid, &repo_path, &common_path, opts->flags)) < 0) + goto out; + + if (is_valid) { + if ((opts->flags & GIT_REPOSITORY_INIT_NO_REINIT) != 0) { + git_error_set(GIT_ERROR_REPOSITORY, + "attempt to reinitialize '%s'", given_repo); + error = GIT_EEXISTS; + goto out; + } + + opts->flags |= GIT_REPOSITORY_INIT__IS_REINIT; + + if ((error = repo_init_config(repo_path.ptr, wd, opts->flags, opts->mode, + oid_type, refdb_type)) < 0) + goto out; + + /* + * If we are re-initializing an existing repository we only + * want to reset HEAD in case the user explicitly provided a + * new target. + */ + if (opts->initial_head && + (error = get_initial_head(&initial_head, &refdb_init_flags, opts)) < 0) + goto out; + + /* TODO: reinitialize the templates */ + } else { + git_str content = GIT_STR_INIT_CONST("ref: " GIT_INVALID_HEAD, + strlen("ref: " GIT_INVALID_HEAD)); + mode_t dmode = pick_dir_mode(opts); + + if ((error = repo_init_structure(repo_path.ptr, wd, opts)) < 0 || + (error = repo_init_config(repo_path.ptr, wd, opts->flags, opts->mode, + oid_type, refdb_type)) < 0) + goto out; + + /* + * Both HEAD and "refs/" must exist in a repository regardless + * of its ref format, otherwise it won't be recognized as a Git + * repository. As such, we create these now so that we can open + * the repository and properly initialize its refdb. + */ + if ((error = git_str_joinpath(&path, repo_path.ptr, GIT_HEAD_FILE)) < 0) + goto out; + + /* Only overwrite HEAD if it wasn't created by the template. */ + if (!git_fs_path_exists(path.ptr)) { + if ((error = git_futils_writebuffer(&content, path.ptr, 0, + GIT_REFS_FILE_MODE)) < 0) + goto out; + + /* + * Ask the backend to force-overwrite the invalid HEAD + * we have just written. + */ + refdb_init_flags |= GIT_REFDB_BACKEND_INIT_FORCE_HEAD; + } + + if ((error = git_str_joinpath(&path, repo_path.ptr, GIT_REFS_DIR)) < 0 || + (error = git_futils_mkdir(path.ptr, dmode, 0)) < 0) + goto out; + + /* + * Note that we also compute the initial HEAD in case the file + * already exists. This is done because the file HEAD may not + * actually be relevant to the backend. E.g. reftables do not + * care for this file at all, so we would end up with no HEAD + * at all if we didn't ask the backend to write one in that + * case. + * + * That being said, we don't force-overwrite HEAD so that the + * backends can first check whether the templates have already + * created a HEAD. + */ + if ((error = get_initial_head(&initial_head, &refdb_init_flags, opts)) < 0) + goto out; + } + + if ((error = git_repository_open(out, repo_path.ptr)) < 0) + goto out; + + if ((error = git_repository_refdb__weakptr(&refdb, *out)) < 0 || + (error = git_refdb_init(refdb, initial_head, opts->mode, refdb_init_flags)) < 0) + goto out; + + if (opts->origin_url && + (error = repo_init_create_origin(*out, opts->origin_url)) < 0) + goto out; + +out: + git_str_dispose(&common_path); + git_str_dispose(&repo_path); + git_str_dispose(&wd_path); + git_str_dispose(&path); + git__free(initial_head); + + return error; +} + +int git_repository_head_detached(git_repository *repo) +{ + git_reference *ref; + git_odb *odb = NULL; + int exists; + + if (git_repository_odb__weakptr(&odb, repo) < 0) + return -1; + + if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0) + return -1; + + if (git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC) { + git_reference_free(ref); + return 0; + } + + exists = git_odb_exists(odb, git_reference_target(ref)); + + git_reference_free(ref); + return exists; +} + +int git_repository_head_detached_for_worktree(git_repository *repo, const char *name) +{ + git_reference *ref = NULL; + int error; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + if ((error = git_repository_head_for_worktree(&ref, repo, name)) < 0) + goto out; + + error = (git_reference_type(ref) != GIT_REFERENCE_SYMBOLIC); +out: + git_reference_free(ref); + + return error; +} + +int git_repository_head(git_reference **head_out, git_repository *repo) +{ + git_reference *head; + int error; + + GIT_ASSERT_ARG(head_out); + + if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0) + return error; + + if (git_reference_type(head) == GIT_REFERENCE_DIRECT) { + *head_out = head; + return 0; + } + + error = git_reference_lookup_resolved(head_out, repo, git_reference_symbolic_target(head), -1); + git_reference_free(head); + + return error == GIT_ENOTFOUND ? GIT_EUNBORNBRANCH : error; +} + +int git_repository_head_for_worktree(git_reference **out, git_repository *repo, const char *name) +{ + git_repository *worktree_repo = NULL; + git_worktree *worktree = NULL; + git_reference *head = NULL; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + *out = NULL; + + if ((error = git_worktree_lookup(&worktree, repo, name)) < 0 || + (error = git_repository_open_from_worktree(&worktree_repo, worktree)) < 0 || + (error = git_reference_lookup(&head, worktree_repo, GIT_HEAD_FILE)) < 0) + goto out; + + if (git_reference_type(head) != GIT_REFERENCE_DIRECT) { + if ((error = git_reference_lookup_resolved(out, worktree_repo, git_reference_symbolic_target(head), -1)) < 0) + goto out; + } else { + *out = head; + head = NULL; + } + +out: + git_reference_free(head); + git_worktree_free(worktree); + git_repository_free(worktree_repo); + return error; +} + +int git_repository_foreach_worktree(git_repository *repo, + git_repository_foreach_worktree_cb cb, + void *payload) +{ + git_strarray worktrees = {0}; + git_repository *worktree_repo = NULL; + git_worktree *worktree = NULL; + int error; + size_t i; + + /* apply operation to repository supplied when commondir is empty, implying there's + * no linked worktrees to iterate, which can occur when using custom odb/refdb + */ + if (!repo->commondir) + return cb(repo, payload); + + if ((error = git_repository_open(&worktree_repo, repo->commondir)) < 0 || + (error = cb(worktree_repo, payload) != 0)) + goto out; + + git_repository_free(worktree_repo); + worktree_repo = NULL; + + if ((error = git_worktree_list(&worktrees, repo)) < 0) + goto out; + + for (i = 0; i < worktrees.count; i++) { + git_repository_free(worktree_repo); + worktree_repo = NULL; + git_worktree_free(worktree); + worktree = NULL; + + if ((error = git_worktree_lookup(&worktree, repo, worktrees.strings[i]) < 0) || + (error = git_repository_open_from_worktree(&worktree_repo, worktree)) < 0) { + if (error != GIT_ENOTFOUND) + goto out; + error = 0; + continue; + } + + if ((error = cb(worktree_repo, payload)) != 0) + goto out; + } + +out: + git_strarray_dispose(&worktrees); + git_repository_free(worktree_repo); + git_worktree_free(worktree); + return error; +} + +int git_repository_head_unborn(git_repository *repo) +{ + git_reference *ref = NULL; + int error; + + error = git_repository_head(&ref, repo); + git_reference_free(ref); + + if (error == GIT_EUNBORNBRANCH) { + git_error_clear(); + return 1; + } + + if (error < 0) + return -1; + + return 0; +} + +static int repo_contains_no_reference(git_repository *repo) +{ + git_reference_iterator *iter; + const char *refname; + int error; + + if ((error = git_reference_iterator_new(&iter, repo)) < 0) + return error; + + error = git_reference_next_name(&refname, iter); + git_reference_iterator_free(iter); + + if (error == GIT_ITEROVER) + return 1; + + return error; +} + +int git_repository_initialbranch(git_str *out, git_repository *repo) +{ + git_config *config; + git_config_entry *entry = NULL; + const char *branch; + int valid, error; + + if ((error = git_repository_config__weakptr(&config, repo)) < 0) + return error; + + if ((error = git_config_get_entry(&entry, config, "init.defaultbranch")) == 0 && + *entry->value) { + branch = entry->value; + } + else if (!error || error == GIT_ENOTFOUND) { + branch = GIT_BRANCH_DEFAULT; + } + else { + goto done; + } + + if ((error = git_str_puts(out, GIT_REFS_HEADS_DIR)) < 0 || + (error = git_str_puts(out, branch)) < 0 || + (error = git_reference_name_is_valid(&valid, out->ptr)) < 0) + goto done; + + if (!valid) { + git_error_set(GIT_ERROR_INVALID, "the value of init.defaultBranch is not a valid branch name"); + error = -1; + } + +done: + git_config_entry_free(entry); + return error; +} + +int git_repository_is_empty(git_repository *repo) +{ + git_reference *head = NULL; + git_str initialbranch = GIT_STR_INIT; + int result = 0; + + if ((result = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0 || + (result = git_repository_initialbranch(&initialbranch, repo)) < 0) + goto done; + + result = (git_reference_type(head) == GIT_REFERENCE_SYMBOLIC && + strcmp(git_reference_symbolic_target(head), initialbranch.ptr) == 0 && + repo_contains_no_reference(repo)); + +done: + git_reference_free(head); + git_str_dispose(&initialbranch); + + return result; +} + +static const char *resolved_parent_path(const git_repository *repo, git_repository_item_t item, git_repository_item_t fallback) +{ + const char *parent; + + switch (item) { + case GIT_REPOSITORY_ITEM_GITDIR: + parent = git_repository_path(repo); + break; + case GIT_REPOSITORY_ITEM_WORKDIR: + parent = git_repository_workdir(repo); + break; + case GIT_REPOSITORY_ITEM_COMMONDIR: + parent = git_repository_commondir(repo); + break; + default: + git_error_set(GIT_ERROR_INVALID, "invalid item directory"); + return NULL; + } + if (!parent && fallback != GIT_REPOSITORY_ITEM__LAST) + return resolved_parent_path(repo, fallback, GIT_REPOSITORY_ITEM__LAST); + + return parent; +} + +int git_repository_item_path( + git_buf *out, + const git_repository *repo, + git_repository_item_t item) +{ + GIT_BUF_WRAP_PRIVATE(out, git_repository__item_path, repo, item); +} + +int git_repository__item_path( + git_str *out, + const git_repository *repo, + git_repository_item_t item) +{ + const char *parent = resolved_parent_path(repo, items[item].parent, items[item].fallback); + if (parent == NULL) { + git_error_set(GIT_ERROR_INVALID, "path cannot exist in repository"); + return GIT_ENOTFOUND; + } + + if (git_str_sets(out, parent) < 0) + return -1; + + if (items[item].name) { + if (git_str_joinpath(out, parent, items[item].name) < 0) + return -1; + } + + if (items[item].directory) { + if (git_fs_path_to_dir(out) < 0) + return -1; + } + + return 0; +} + +const char *git_repository_path(const git_repository *repo) +{ + GIT_ASSERT_ARG_WITH_RETVAL(repo, NULL); + return repo->gitdir; +} + +const char *git_repository_workdir(const git_repository *repo) +{ + GIT_ASSERT_ARG_WITH_RETVAL(repo, NULL); + + if (repo->is_bare) + return NULL; + + return repo->workdir; +} + +int git_repository_workdir_path( + git_str *out, git_repository *repo, const char *path) +{ + int error; + + if (!repo->workdir) { + git_error_set(GIT_ERROR_REPOSITORY, "repository has no working directory"); + return GIT_EBAREREPO; + } + + if (!(error = git_str_joinpath(out, repo->workdir, path))) + error = git_path_validate_str_length(repo, out); + + return error; +} + +const char *git_repository_commondir(const git_repository *repo) +{ + GIT_ASSERT_ARG_WITH_RETVAL(repo, NULL); + return repo->commondir; +} + +int git_repository_set_workdir( + git_repository *repo, const char *workdir, int update_gitlink) +{ + int error = 0; + git_str path = GIT_STR_INIT; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(workdir); + + if (git_fs_path_prettify_dir(&path, workdir, NULL) < 0) + return -1; + + if (repo->workdir && strcmp(repo->workdir, path.ptr) == 0) { + git_str_dispose(&path); + return 0; + } + + if (update_gitlink) { + git_config *config; + + if (git_repository_config__weakptr(&config, repo) < 0) { + git_str_dispose(&path); + return -1; + } + + error = repo_write_gitlink(path.ptr, git_repository_path(repo), false); + + /* passthrough error means gitlink is unnecessary */ + if (error == GIT_PASSTHROUGH) + error = git_config_delete_entry(config, "core.worktree"); + else if (!error) + error = git_config_set_string(config, "core.worktree", path.ptr); + + if (!error) + error = git_config_set_bool(config, "core.bare", false); + } + + if (!error) { + char *old_workdir = repo->workdir; + + repo->workdir = git_str_detach(&path); + repo->is_bare = 0; + + git__free(old_workdir); + } + git_str_dispose(&path); + + return error; +} + +int git_repository_is_bare(const git_repository *repo) +{ + GIT_ASSERT_ARG(repo); + return repo->is_bare; +} + +int git_repository_is_worktree(const git_repository *repo) +{ + GIT_ASSERT_ARG(repo); + return repo->is_worktree; +} + +int git_repository_set_bare(git_repository *repo) +{ + int error; + git_config *config; + + GIT_ASSERT_ARG(repo); + + if (repo->is_bare) + return 0; + + if ((error = git_repository_config__weakptr(&config, repo)) < 0) + return error; + + if ((error = git_config_set_bool(config, "core.bare", true)) < 0) + return error; + + if ((error = git_config__update_entry(config, "core.worktree", NULL, true, true)) < 0) + return error; + + git__free(repo->workdir); + repo->workdir = NULL; + repo->is_bare = 1; + + return 0; +} + +int git_repository_head_commit(git_commit **commit, git_repository *repo) +{ + git_reference *head; + git_object *obj; + int error; + + if ((error = git_repository_head(&head, repo)) < 0) + return error; + + if ((error = git_reference_peel(&obj, head, GIT_OBJECT_COMMIT)) < 0) + goto cleanup; + + *commit = (git_commit *)obj; + +cleanup: + git_reference_free(head); + return error; +} + +int git_repository_head_tree(git_tree **tree, git_repository *repo) +{ + git_reference *head; + git_object *obj; + int error; + + if ((error = git_repository_head(&head, repo)) < 0) + return error; + + if ((error = git_reference_peel(&obj, head, GIT_OBJECT_TREE)) < 0) + goto cleanup; + + *tree = (git_tree *)obj; + +cleanup: + git_reference_free(head); + return error; +} + +int git_repository__set_orig_head(git_repository *repo, const git_oid *orig_head) +{ + git_filebuf file = GIT_FILEBUF_INIT; + git_str file_path = GIT_STR_INIT; + char orig_head_str[GIT_OID_MAX_HEXSIZE]; + int error = 0; + + git_oid_fmt(orig_head_str, orig_head); + + if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_ORIG_HEAD_FILE)) == 0 && + (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_MERGE_FILE_MODE)) == 0 && + (error = git_filebuf_printf(&file, "%.*s\n", (int)git_oid_hexsize(repo->oid_type), orig_head_str)) == 0) + error = git_filebuf_commit(&file); + + if (error < 0) + git_filebuf_cleanup(&file); + + git_str_dispose(&file_path); + + return error; +} + +static int git_repository__message(git_str *out, git_repository *repo) +{ + git_str path = GIT_STR_INIT; + struct stat st; + int error; + + if (git_str_joinpath(&path, repo->gitdir, GIT_MERGE_MSG_FILE) < 0) + return -1; + + if ((error = p_stat(git_str_cstr(&path), &st)) < 0) { + if (errno == ENOENT) + error = GIT_ENOTFOUND; + git_error_set(GIT_ERROR_OS, "could not access message file"); + } else { + error = git_futils_readbuffer(out, git_str_cstr(&path)); + } + + git_str_dispose(&path); + + return error; +} + +int git_repository_message(git_buf *out, git_repository *repo) +{ + GIT_BUF_WRAP_PRIVATE(out, git_repository__message, repo); +} + +int git_repository_message_remove(git_repository *repo) +{ + git_str path = GIT_STR_INIT; + int error; + + if (git_str_joinpath(&path, repo->gitdir, GIT_MERGE_MSG_FILE) < 0) + return -1; + + error = p_unlink(git_str_cstr(&path)); + git_str_dispose(&path); + + return error; +} + +int git_repository_hashfile( + git_oid *out, + git_repository *repo, + const char *path, + git_object_t type, + const char *as_path) +{ + int error; + git_filter_list *fl = NULL; + git_file fd = -1; + uint64_t len; + git_str full_path = GIT_STR_INIT; + const char *workdir = git_repository_workdir(repo); + + /* as_path can be NULL */ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(path); + GIT_ASSERT_ARG(repo); + + if ((error = git_fs_path_join_unrooted(&full_path, path, workdir, NULL)) < 0 || + (error = git_path_validate_str_length(repo, &full_path)) < 0) + return error; + + /* + * NULL as_path means that we should derive it from the + * given path. + */ + if (!as_path) { + if (workdir && !git__prefixcmp(full_path.ptr, workdir)) + as_path = full_path.ptr + strlen(workdir); + else + as_path = ""; + } + + /* passing empty string for "as_path" indicated --no-filters */ + if (strlen(as_path) > 0) { + error = git_filter_list_load( + &fl, repo, NULL, as_path, + GIT_FILTER_TO_ODB, GIT_FILTER_DEFAULT); + + if (error < 0) + return error; + } + + /* at this point, error is a count of the number of loaded filters */ + + fd = git_futils_open_ro(full_path.ptr); + if (fd < 0) { + error = fd; + goto cleanup; + } + + if ((error = git_futils_filesize(&len, fd)) < 0) + goto cleanup; + + if (!git__is_sizet(len)) { + git_error_set(GIT_ERROR_OS, "file size overflow for 32-bit systems"); + error = -1; + goto cleanup; + } + + error = git_odb__hashfd_filtered(out, fd, (size_t)len, type, repo->oid_type, fl); + +cleanup: + if (fd >= 0) + p_close(fd); + git_filter_list_free(fl); + git_str_dispose(&full_path); + + return error; +} + +static int checkout_message(git_str *out, git_reference *old, const char *new) +{ + const char *idstr; + + git_str_puts(out, "checkout: moving from "); + + if (git_reference_type(old) == GIT_REFERENCE_SYMBOLIC) { + git_str_puts(out, git_reference__shorthand(git_reference_symbolic_target(old))); + } else { + if ((idstr = git_oid_tostr_s(git_reference_target(old))) == NULL) + return -1; + + git_str_puts(out, idstr); + } + + git_str_puts(out, " to "); + + if (git_reference__is_branch(new) || + git_reference__is_tag(new) || + git_reference__is_remote(new)) + git_str_puts(out, git_reference__shorthand(new)); + else + git_str_puts(out, new); + + if (git_str_oom(out)) + return -1; + + return 0; +} + +static int detach(git_repository *repo, const git_oid *id, const char *new) +{ + int error; + git_str log_message = GIT_STR_INIT; + git_object *object = NULL, *peeled = NULL; + git_reference *new_head = NULL, *current = NULL; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(id); + + if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0) + return error; + + if ((error = git_object_lookup(&object, repo, id, GIT_OBJECT_ANY)) < 0) + goto cleanup; + + if ((error = git_object_peel(&peeled, object, GIT_OBJECT_COMMIT)) < 0) + goto cleanup; + + if (new == NULL && + (new = git_oid_tostr_s(git_object_id(peeled))) == NULL) { + error = -1; + goto cleanup; + } + + if ((error = checkout_message(&log_message, current, new)) < 0) + goto cleanup; + + error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), true, git_str_cstr(&log_message)); + +cleanup: + git_str_dispose(&log_message); + git_object_free(object); + git_object_free(peeled); + git_reference_free(current); + git_reference_free(new_head); + return error; +} + +int git_repository_set_head( + git_repository *repo, + const char *refname) +{ + git_reference *ref = NULL, *current = NULL, *new_head = NULL; + git_str log_message = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(refname); + + if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0) + return error; + + if ((error = checkout_message(&log_message, current, refname)) < 0) + goto cleanup; + + error = git_reference_lookup(&ref, repo, refname); + if (error < 0 && error != GIT_ENOTFOUND) + goto cleanup; + + if (ref && current->type == GIT_REFERENCE_SYMBOLIC && git__strcmp(current->target.symbolic, ref->name) && + git_reference_is_branch(ref) && git_branch_is_checked_out(ref)) { + git_error_set(GIT_ERROR_REPOSITORY, "cannot set HEAD to reference '%s' as it is the current HEAD " + "of a linked repository.", git_reference_name(ref)); + error = -1; + goto cleanup; + } + + if (!error) { + if (git_reference_is_branch(ref)) { + error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, + git_reference_name(ref), true, git_str_cstr(&log_message)); + } else { + error = detach(repo, git_reference_target(ref), + git_reference_is_tag(ref) || git_reference_is_remote(ref) ? refname : NULL); + } + } else if (git_reference__is_branch(refname)) { + error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname, + true, git_str_cstr(&log_message)); + } + +cleanup: + git_str_dispose(&log_message); + git_reference_free(current); + git_reference_free(ref); + git_reference_free(new_head); + return error; +} + +int git_repository_set_head_detached( + git_repository *repo, + const git_oid *committish) +{ + return detach(repo, committish, NULL); +} + +int git_repository_set_head_detached_from_annotated( + git_repository *repo, + const git_annotated_commit *committish) +{ + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(committish); + + return detach(repo, git_annotated_commit_id(committish), committish->description); +} + +int git_repository_detach_head(git_repository *repo) +{ + git_reference *old_head = NULL, *new_head = NULL, *current = NULL; + git_object *object = NULL; + git_str log_message = GIT_STR_INIT; + const char *idstr; + int error; + + GIT_ASSERT_ARG(repo); + + if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0) + return error; + + if ((error = git_repository_head(&old_head, repo)) < 0) + goto cleanup; + + if ((error = git_object_lookup(&object, repo, git_reference_target(old_head), GIT_OBJECT_COMMIT)) < 0) + goto cleanup; + + if ((idstr = git_oid_tostr_s(git_object_id(object))) == NULL) { + error = -1; + goto cleanup; + } + + if ((error = checkout_message(&log_message, current, idstr)) < 0) + goto cleanup; + + error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_reference_target(old_head), + 1, git_str_cstr(&log_message)); + +cleanup: + git_str_dispose(&log_message); + git_object_free(object); + git_reference_free(old_head); + git_reference_free(new_head); + git_reference_free(current); + return error; +} + +/** + * Loosely ported from git.git + * https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L198-289 + */ +int git_repository_state(git_repository *repo) +{ + git_str repo_path = GIT_STR_INIT; + int state = GIT_REPOSITORY_STATE_NONE; + + GIT_ASSERT_ARG(repo); + + if (git_str_puts(&repo_path, repo->gitdir) < 0) + return -1; + + if (git_fs_path_contains_file(&repo_path, GIT_REBASE_MERGE_INTERACTIVE_FILE)) + state = GIT_REPOSITORY_STATE_REBASE_INTERACTIVE; + else if (git_fs_path_contains_dir(&repo_path, GIT_REBASE_MERGE_DIR)) + state = GIT_REPOSITORY_STATE_REBASE_MERGE; + else if (git_fs_path_contains_file(&repo_path, GIT_REBASE_APPLY_REBASING_FILE)) + state = GIT_REPOSITORY_STATE_REBASE; + else if (git_fs_path_contains_file(&repo_path, GIT_REBASE_APPLY_APPLYING_FILE)) + state = GIT_REPOSITORY_STATE_APPLY_MAILBOX; + else if (git_fs_path_contains_dir(&repo_path, GIT_REBASE_APPLY_DIR)) + state = GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE; + else if (git_fs_path_contains_file(&repo_path, GIT_MERGE_HEAD_FILE)) + state = GIT_REPOSITORY_STATE_MERGE; + else if (git_fs_path_contains_file(&repo_path, GIT_REVERT_HEAD_FILE)) { + state = GIT_REPOSITORY_STATE_REVERT; + if (git_fs_path_contains_file(&repo_path, GIT_SEQUENCER_TODO_FILE)) { + state = GIT_REPOSITORY_STATE_REVERT_SEQUENCE; + } + } else if (git_fs_path_contains_file(&repo_path, GIT_CHERRYPICK_HEAD_FILE)) { + state = GIT_REPOSITORY_STATE_CHERRYPICK; + if (git_fs_path_contains_file(&repo_path, GIT_SEQUENCER_TODO_FILE)) { + state = GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE; + } + } else if (git_fs_path_contains_file(&repo_path, GIT_BISECT_LOG_FILE)) + state = GIT_REPOSITORY_STATE_BISECT; + + git_str_dispose(&repo_path); + return state; +} + +int git_repository__cleanup_files( + git_repository *repo, const char *files[], size_t files_len) +{ + git_str buf = GIT_STR_INIT; + size_t i; + int error; + + for (error = 0, i = 0; !error && i < files_len; ++i) { + const char *path; + + if (git_str_joinpath(&buf, repo->gitdir, files[i]) < 0) + return -1; + + path = git_str_cstr(&buf); + + if (git_fs_path_isfile(path)) { + error = p_unlink(path); + } else if (git_fs_path_isdir(path)) { + error = git_futils_rmdir_r(path, NULL, + GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS); + } + + git_str_clear(&buf); + } + + git_str_dispose(&buf); + return error; +} + +static const char *state_files[] = { + GIT_MERGE_HEAD_FILE, + GIT_MERGE_MODE_FILE, + GIT_MERGE_MSG_FILE, + GIT_REVERT_HEAD_FILE, + GIT_CHERRYPICK_HEAD_FILE, + GIT_BISECT_LOG_FILE, + GIT_REBASE_MERGE_DIR, + GIT_REBASE_APPLY_DIR, + GIT_SEQUENCER_DIR, +}; + +int git_repository_state_cleanup(git_repository *repo) +{ + GIT_ASSERT_ARG(repo); + + return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files)); +} + +int git_repository__shallow_roots( + git_oid **out, + size_t *out_len, + git_repository *repo) +{ + int error = 0; + + if (!repo->shallow_grafts && (error = load_grafts(repo)) < 0) + return error; + + if ((error = git_grafts_refresh(repo->shallow_grafts)) < 0) + return error; + + if ((error = git_grafts_oids(out, out_len, repo->shallow_grafts)) < 0) + return error; + + return 0; +} + +int git_repository__shallow_roots_write(git_repository *repo, git_oidarray *roots) +{ + git_filebuf file = GIT_FILEBUF_INIT; + git_str path = GIT_STR_INIT; + char oid_str[GIT_OID_MAX_HEXSIZE + 1]; + size_t i; + int filebuf_hash, error = 0; + + GIT_ASSERT_ARG(repo); + + filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(repo->oid_type)); + GIT_ASSERT(filebuf_hash); + + if ((error = git_str_joinpath(&path, repo->gitdir, "shallow")) < 0) + goto on_error; + + if ((error = git_filebuf_open(&file, git_str_cstr(&path), filebuf_hash, 0666)) < 0) + goto on_error; + + for (i = 0; i < roots->count; i++) { + git_oid_tostr(oid_str, sizeof(oid_str), &roots->ids[i]); + git_filebuf_write(&file, oid_str, git_oid_hexsize(repo->oid_type)); + git_filebuf_write(&file, "\n", 1); + } + + git_filebuf_commit(&file); + + if ((error = load_grafts(repo)) < 0) { + error = -1; + goto on_error; + } + + if (!roots->count) + remove(path.ptr); + +on_error: + git_str_dispose(&path); + + return error; +} + +int git_repository_is_shallow(git_repository *repo) +{ + git_str path = GIT_STR_INIT; + struct stat st; + int error; + + if ((error = git_str_joinpath(&path, repo->gitdir, "shallow")) < 0) + return error; + + error = git_fs_path_lstat(path.ptr, &st); + git_str_dispose(&path); + + if (error == GIT_ENOTFOUND) { + git_error_clear(); + return 0; + } + + if (error < 0) + return error; + + return st.st_size == 0 ? 0 : 1; +} + +int git_repository_init_options_init( + git_repository_init_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_repository_init_options, + GIT_REPOSITORY_INIT_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_repository_init_init_options( + git_repository_init_options *opts, unsigned int version) +{ + return git_repository_init_options_init(opts, version); +} +#endif + +int git_repository_ident(const char **name, const char **email, const git_repository *repo) +{ + *name = repo->ident_name; + *email = repo->ident_email; + + return 0; +} + +int git_repository_set_ident(git_repository *repo, const char *name, const char *email) +{ + char *tmp_name = NULL, *tmp_email = NULL; + + if (name) { + tmp_name = git__strdup(name); + GIT_ERROR_CHECK_ALLOC(tmp_name); + } + + if (email) { + tmp_email = git__strdup(email); + GIT_ERROR_CHECK_ALLOC(tmp_email); + } + + tmp_name = git_atomic_swap(repo->ident_name, tmp_name); + tmp_email = git_atomic_swap(repo->ident_email, tmp_email); + + git__free(tmp_name); + git__free(tmp_email); + + return 0; +} + +int git_repository_submodule_cache_all(git_repository *repo) +{ + GIT_ASSERT_ARG(repo); + return git_submodule_cache_init(&repo->submodule_cache, repo); +} + +int git_repository_submodule_cache_clear(git_repository *repo) +{ + int error = 0; + GIT_ASSERT_ARG(repo); + + error = git_submodule_cache_free(repo->submodule_cache); + repo->submodule_cache = NULL; + return error; +} + +git_oid_t git_repository_oid_type(git_repository *repo) +{ + return repo ? repo->oid_type : 0; +} + +struct mergehead_data { + git_repository *repo; + git_vector *parents; +}; + +static int insert_mergehead(const git_oid *oid, void *payload) +{ + git_commit *commit; + struct mergehead_data *data = (struct mergehead_data *)payload; + + if (git_commit_lookup(&commit, data->repo, oid) < 0) + return -1; + + return git_vector_insert(data->parents, commit); +} + +int git_repository_commit_parents(git_commitarray *out, git_repository *repo) +{ + git_commit *first_parent = NULL, *commit; + git_reference *head_ref = NULL; + git_vector parents = GIT_VECTOR_INIT; + struct mergehead_data data; + size_t i; + int error; + + GIT_ASSERT_ARG(out && repo); + + out->count = 0; + out->commits = NULL; + + error = git_revparse_ext((git_object **)&first_parent, &head_ref, repo, "HEAD"); + + if (error != 0) { + if (error == GIT_ENOTFOUND) + error = 0; + + goto done; + } + + if ((error = git_vector_insert(&parents, first_parent)) < 0) + goto done; + + data.repo = repo; + data.parents = &parents; + + error = git_repository_mergehead_foreach(repo, insert_mergehead, &data); + + if (error == GIT_ENOTFOUND) + error = 0; + else if (error != 0) + goto done; + + out->commits = (git_commit **)git_vector_detach(&out->count, NULL, &parents); + +done: + git_vector_foreach(&parents, i, commit) + git__free(commit); + + git_reference_free(head_ref); + return error; +} + +int git_repository__abbrev_length(int *out, git_repository *repo) +{ + size_t oid_hexsize; + int len; + int error; + + oid_hexsize = git_oid_hexsize(repo->oid_type); + + if ((error = git_repository__configmap_lookup(&len, repo, GIT_CONFIGMAP_ABBREV)) < 0) + return error; + + if (len < GIT_ABBREV_MINIMUM) { + git_error_set(GIT_ERROR_CONFIG, "invalid oid abbreviation setting: '%d'", len); + return -1; + } + + if (len == GIT_ABBREV_FALSE || (size_t)len > oid_hexsize) + len = (int)oid_hexsize; + + *out = len; + + return error; +} diff --git a/src/libgit2/repository.h b/src/libgit2/repository.h new file mode 100644 index 00000000000..bdf1562fa74 --- /dev/null +++ b/src/libgit2/repository.h @@ -0,0 +1,295 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_repository_h__ +#define INCLUDE_repository_h__ + +#include "common.h" + +#include "git2/common.h" +#include "git2/oid.h" +#include "git2/odb.h" +#include "git2/repository.h" +#include "git2/object.h" +#include "git2/config.h" +#include "git2/sys/repository.h" + +#include "array.h" +#include "cache.h" +#include "refs.h" +#include "str.h" +#include "object.h" +#include "attrcache.h" +#include "submodule.h" +#include "diff_driver.h" +#include "grafts.h" + +#define DOT_GIT ".git" +#define GIT_DIR DOT_GIT "/" +#define GIT_DIR_MODE 0755 +#define GIT_BARE_DIR_MODE 0777 + +/* Default DOS-compatible 8.3 "short name" for a git repository, "GIT~1" */ +#define GIT_DIR_SHORTNAME "GIT~1" + +extern bool git_repository__fsync_gitdir; +extern bool git_repository__validate_ownership; + +/** Cvar cache identifiers */ +typedef enum { + GIT_CONFIGMAP_AUTO_CRLF = 0, /* core.autocrlf */ + GIT_CONFIGMAP_EOL, /* core.eol */ + GIT_CONFIGMAP_SYMLINKS, /* core.symlinks */ + GIT_CONFIGMAP_IGNORECASE, /* core.ignorecase */ + GIT_CONFIGMAP_FILEMODE, /* core.filemode */ + GIT_CONFIGMAP_IGNORESTAT, /* core.ignorestat */ + GIT_CONFIGMAP_TRUSTCTIME, /* core.trustctime */ + GIT_CONFIGMAP_ABBREV, /* core.abbrev */ + GIT_CONFIGMAP_PRECOMPOSE, /* core.precomposeunicode */ + GIT_CONFIGMAP_SAFE_CRLF, /* core.safecrlf */ + GIT_CONFIGMAP_LOGALLREFUPDATES, /* core.logallrefupdates */ + GIT_CONFIGMAP_PROTECTHFS, /* core.protectHFS */ + GIT_CONFIGMAP_PROTECTNTFS, /* core.protectNTFS */ + GIT_CONFIGMAP_FSYNCOBJECTFILES, /* core.fsyncObjectFiles */ + GIT_CONFIGMAP_LONGPATHS, /* core.longpaths */ + GIT_CONFIGMAP_CACHE_MAX +} git_configmap_item; + +/** + * Configuration map value enumerations + * + * These are the values that are actually stored in the configmap cache, + * instead of their string equivalents. These values are internal and + * symbolic; make sure that none of them is set to `-1`, since that is + * the unique identifier for "not cached" + */ +typedef enum { + /* The value hasn't been loaded from the cache yet */ + GIT_CONFIGMAP_NOT_CACHED = -1, + + /* core.safecrlf: false, 'fail', 'warn' */ + GIT_SAFE_CRLF_FALSE = 0, + GIT_SAFE_CRLF_FAIL = 1, + GIT_SAFE_CRLF_WARN = 2, + + /* core.autocrlf: false, true, 'input; */ + GIT_AUTO_CRLF_FALSE = 0, + GIT_AUTO_CRLF_TRUE = 1, + GIT_AUTO_CRLF_INPUT = 2, + GIT_AUTO_CRLF_DEFAULT = GIT_AUTO_CRLF_FALSE, + + /* core.eol: unset, 'crlf', 'lf', 'native' */ + GIT_EOL_UNSET = 0, + GIT_EOL_CRLF = 1, + GIT_EOL_LF = 2, +#ifdef GIT_WIN32 + GIT_EOL_NATIVE = GIT_EOL_CRLF, +#else + GIT_EOL_NATIVE = GIT_EOL_LF, +#endif + GIT_EOL_DEFAULT = GIT_EOL_NATIVE, + + /* core.symlinks: bool */ + GIT_SYMLINKS_DEFAULT = GIT_CONFIGMAP_TRUE, + /* core.ignorecase */ + GIT_IGNORECASE_DEFAULT = GIT_CONFIGMAP_FALSE, + /* core.filemode */ + GIT_FILEMODE_DEFAULT = GIT_CONFIGMAP_TRUE, + /* core.ignorestat */ + GIT_IGNORESTAT_DEFAULT = GIT_CONFIGMAP_FALSE, + /* core.trustctime */ + GIT_TRUSTCTIME_DEFAULT = GIT_CONFIGMAP_TRUE, + /* core.abbrev */ + GIT_ABBREV_FALSE = GIT_OID_MAX_HEXSIZE, + GIT_ABBREV_MINIMUM = 4, + GIT_ABBREV_DEFAULT = 7, + /* core.precomposeunicode */ + GIT_PRECOMPOSE_DEFAULT = GIT_CONFIGMAP_FALSE, + /* core.safecrlf */ + GIT_SAFE_CRLF_DEFAULT = GIT_CONFIGMAP_FALSE, + /* core.logallrefupdates */ + GIT_LOGALLREFUPDATES_FALSE = GIT_CONFIGMAP_FALSE, + GIT_LOGALLREFUPDATES_TRUE = GIT_CONFIGMAP_TRUE, + GIT_LOGALLREFUPDATES_UNSET = 2, + GIT_LOGALLREFUPDATES_ALWAYS = 3, + GIT_LOGALLREFUPDATES_DEFAULT = GIT_LOGALLREFUPDATES_UNSET, + /* core.protectHFS */ + GIT_PROTECTHFS_DEFAULT = GIT_CONFIGMAP_FALSE, + /* core.protectNTFS */ + GIT_PROTECTNTFS_DEFAULT = GIT_CONFIGMAP_TRUE, + /* core.fsyncObjectFiles */ + GIT_FSYNCOBJECTFILES_DEFAULT = GIT_CONFIGMAP_FALSE, + /* core.longpaths */ + GIT_LONGPATHS_DEFAULT = GIT_CONFIGMAP_FALSE +} git_configmap_value; + +/* internal repository init flags */ +enum { + GIT_REPOSITORY_INIT__HAS_DOTGIT = (1u << 16), + GIT_REPOSITORY_INIT__NATURAL_WD = (1u << 17), + GIT_REPOSITORY_INIT__IS_REINIT = (1u << 18) +}; + +/** Internal structure for repository object */ +struct git_repository { + git_odb *_odb; + git_refdb *_refdb; + git_config *_config; + git_index *_index; + + git_cache objects; + git_attr_cache *attrcache; + git_diff_driver_registry *diff_drivers; + + char *gitlink; + char *gitdir; + char *commondir; + char *workdir; + char *namespace; + + char *ident_name; + char *ident_email; + + git_array_t(git_str) reserved_names; + + unsigned use_env:1, + is_bare:1, + is_worktree:1; + git_oid_t oid_type; + git_refdb_t refdb_type; + + unsigned int lru_counter; + + git_grafts *grafts; + git_grafts *shallow_grafts; + + git_atomic32 attr_session_key; + + intptr_t configmap_cache[GIT_CONFIGMAP_CACHE_MAX]; + git_submodule_cache *submodule_cache; +}; + +GIT_INLINE(git_attr_cache *) git_repository_attr_cache(git_repository *repo) +{ + return repo->attrcache; +} + +int git_repository_head_commit(git_commit **commit, git_repository *repo); +int git_repository_head_tree(git_tree **tree, git_repository *repo); + +typedef int (*git_repository_foreach_worktree_cb)(git_repository *, void *); + +int git_repository_foreach_worktree(git_repository *repo, + git_repository_foreach_worktree_cb cb, + void *payload); + +/* + * Weak pointers to repository internals. + * + * The returned pointers do not need to be freed. Do not keep + * permanent references to these (i.e. between API calls), since they may + * become invalidated if the user replaces a repository internal. + */ +int git_repository_config__weakptr(git_config **out, git_repository *repo); +int git_repository_odb__weakptr(git_odb **out, git_repository *repo); +int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo); +int git_repository_index__weakptr(git_index **out, git_repository *repo); +int git_repository_grafts__weakptr(git_grafts **out, git_repository *repo); +int git_repository_shallow_grafts__weakptr(git_grafts **out, git_repository *repo); + +/* + * Configuration map cache + * + * Efficient access to the most used config variables of a repository. + * The cache is cleared every time the config backend is replaced. + */ +int git_repository__configmap_lookup(int *out, git_repository *repo, git_configmap_item item); +void git_repository__configmap_lookup_cache_clear(git_repository *repo); + +/** Return the length that object names will be abbreviated to. */ +int git_repository__abbrev_length(int *out, git_repository *repo); + +int git_repository__item_path(git_str *out, const git_repository *repo, git_repository_item_t item); + +GIT_INLINE(int) git_repository__ensure_not_bare( + git_repository *repo, + const char *operation_name) +{ + if (!git_repository_is_bare(repo)) + return 0; + + git_error_set( + GIT_ERROR_REPOSITORY, + "cannot %s. This operation is not allowed against bare repositories.", + operation_name); + + return GIT_EBAREREPO; +} + +int git_repository__set_orig_head(git_repository *repo, const git_oid *orig_head); + +int git_repository__cleanup_files(git_repository *repo, const char *files[], size_t files_len); + +/* The default "reserved names" for a repository */ +extern git_str git_repository__reserved_names_win32[]; +extern size_t git_repository__reserved_names_win32_len; + +extern git_str git_repository__reserved_names_posix[]; +extern size_t git_repository__reserved_names_posix_len; + +/* + * Gets any "reserved names" in the repository. This will return paths + * that should not be allowed in the repository (like ".git") to avoid + * conflicting with the repository path, or with alternate mechanisms to + * the repository path (eg, "GIT~1"). Every attempt will be made to look + * up all possible reserved names - if there was a conflict for the shortname + * GIT~1, for example, this function will try to look up the alternate + * shortname. If that fails, this function returns false, but out and outlen + * will still be populated with good defaults. + */ +bool git_repository__reserved_names( + git_str **out, size_t *outlen, git_repository *repo, bool include_ntfs); + +int git_repository__shallow_roots(git_oid **out, size_t *out_len, git_repository *repo); +int git_repository__shallow_roots_write(git_repository *repo, git_oidarray *roots); + +/* + * The default branch for the repository; the `init.defaultBranch` + * configuration option, if set, or `master` if it is not. + */ +int git_repository_initialbranch(git_str *out, git_repository *repo); + +/* + * Given a relative `path`, this makes it absolute based on the + * repository's working directory. This will perform validation + * to ensure that the path is not longer than MAX_PATH on Windows + * (unless `core.longpaths` is set in the repo config). + */ +int git_repository_workdir_path(git_str *out, git_repository *repo, const char *path); + +int git_repository__extensions(char ***out, size_t *out_len); +int git_repository__set_extensions(const char **extensions, size_t len); +void git_repository__free_extensions(void); + +/* + * Set the object format (OID type) for a repository; this will set + * both the configuration and the internal value for the oid type. + */ +int git_repository__set_objectformat( + git_repository *repo, + git_oid_t oid_type); + +/* SHA256 support */ + +#ifndef GIT_EXPERIMENTAL_SHA256 + +GIT_EXTERN(int) git_repository_new_ext( + git_repository **out, + git_repository_new_options *opts); + +#endif + +#endif diff --git a/src/libgit2/reset.c b/src/libgit2/reset.c new file mode 100644 index 00000000000..605c4afd5e2 --- /dev/null +++ b/src/libgit2/reset.c @@ -0,0 +1,204 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "commit.h" +#include "tag.h" +#include "merge.h" +#include "diff.h" +#include "annotated_commit.h" +#include "git2/reset.h" +#include "git2/checkout.h" +#include "git2/merge.h" +#include "git2/refs.h" + +#define ERROR_MSG "Cannot perform reset" + +int git_reset_default( + git_repository *repo, + const git_object *target, + const git_strarray *pathspecs) +{ + git_object *commit = NULL; + git_tree *tree = NULL; + git_diff *diff = NULL; + git_diff_options opts = GIT_DIFF_OPTIONS_INIT; + size_t i, max_i; + git_index_entry entry; + int error; + git_index *index = NULL; + + GIT_ASSERT_ARG(pathspecs && pathspecs->count > 0); + + memset(&entry, 0, sizeof(git_index_entry)); + + if ((error = git_repository_index(&index, repo)) < 0) + goto cleanup; + + if (target) { + if (git_object_owner(target) != repo) { + git_error_set(GIT_ERROR_OBJECT, + "%s_default - The given target does not belong to this repository.", ERROR_MSG); + return -1; + } + + if ((error = git_object_peel(&commit, target, GIT_OBJECT_COMMIT)) < 0 || + (error = git_commit_tree(&tree, (git_commit *)commit)) < 0) + goto cleanup; + } + + opts.pathspec = *pathspecs; + opts.flags = GIT_DIFF_REVERSE; + + if ((error = git_diff_tree_to_index( + &diff, repo, tree, index, &opts)) < 0) + goto cleanup; + + for (i = 0, max_i = git_diff_num_deltas(diff); i < max_i; ++i) { + const git_diff_delta *delta = git_diff_get_delta(diff, i); + + GIT_ASSERT(delta->status == GIT_DELTA_ADDED || + delta->status == GIT_DELTA_MODIFIED || + delta->status == GIT_DELTA_CONFLICTED || + delta->status == GIT_DELTA_DELETED); + + error = git_index_conflict_remove(index, delta->old_file.path); + if (error < 0) { + if (delta->status == GIT_DELTA_ADDED && error == GIT_ENOTFOUND) + git_error_clear(); + else + goto cleanup; + } + + if (delta->status == GIT_DELTA_DELETED) { + if ((error = git_index_remove(index, delta->old_file.path, 0)) < 0) + goto cleanup; + } else { + entry.mode = delta->new_file.mode; + git_oid_cpy(&entry.id, &delta->new_file.id); + entry.path = (char *)delta->new_file.path; + + if ((error = git_index_add(index, &entry)) < 0) + goto cleanup; + } + } + + error = git_index_write(index); + +cleanup: + git_object_free(commit); + git_tree_free(tree); + git_index_free(index); + git_diff_free(diff); + + return error; +} + +static int reset( + git_repository *repo, + const git_object *target, + const char *to, + git_reset_t reset_type, + const git_checkout_options *checkout_opts) +{ + git_object *commit = NULL; + git_index *index = NULL; + git_tree *tree = NULL; + int error = 0; + git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; + git_str log_message = GIT_STR_INIT; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(target); + + if (checkout_opts) + opts = *checkout_opts; + + if (git_object_owner(target) != repo) { + git_error_set(GIT_ERROR_OBJECT, + "%s - The given target does not belong to this repository.", ERROR_MSG); + return -1; + } + + if (reset_type != GIT_RESET_SOFT && + (error = git_repository__ensure_not_bare(repo, + reset_type == GIT_RESET_MIXED ? "reset mixed" : "reset hard")) < 0) + return error; + + if ((error = git_object_peel(&commit, target, GIT_OBJECT_COMMIT)) < 0 || + (error = git_repository_index(&index, repo)) < 0 || + (error = git_commit_tree(&tree, (git_commit *)commit)) < 0) + goto cleanup; + + if (reset_type == GIT_RESET_SOFT && + (git_repository_state(repo) == GIT_REPOSITORY_STATE_MERGE || + git_index_has_conflicts(index))) + { + git_error_set(GIT_ERROR_OBJECT, "%s (soft) in the middle of a merge", ERROR_MSG); + error = GIT_EUNMERGED; + goto cleanup; + } + + if ((error = git_str_printf(&log_message, "reset: moving to %s", to)) < 0) + return error; + + if (reset_type == GIT_RESET_HARD) { + /* overwrite working directory with the new tree */ + opts.checkout_strategy = GIT_CHECKOUT_FORCE; + + if ((error = git_checkout_tree(repo, (git_object *)tree, &opts)) < 0) + goto cleanup; + } + + /* move HEAD to the new target */ + if ((error = git_reference__update_terminal(repo, GIT_HEAD_FILE, + git_object_id(commit), NULL, git_str_cstr(&log_message))) < 0) + goto cleanup; + + if (reset_type > GIT_RESET_SOFT) { + /* reset index to the target content */ + + if ((error = git_index_read_tree(index, tree)) < 0 || + (error = git_index_write(index)) < 0) + goto cleanup; + + if ((error = git_repository_state_cleanup(repo)) < 0) { + git_error_set(GIT_ERROR_INDEX, "%s - failed to clean up merge data", ERROR_MSG); + goto cleanup; + } + } + +cleanup: + git_object_free(commit); + git_index_free(index); + git_tree_free(tree); + git_str_dispose(&log_message); + + return error; +} + +int git_reset( + git_repository *repo, + const git_object *target, + git_reset_t reset_type, + const git_checkout_options *checkout_opts) +{ + char to[GIT_OID_MAX_HEXSIZE + 1]; + + git_oid_tostr(to, GIT_OID_MAX_HEXSIZE + 1, git_object_id(target)); + return reset(repo, target, to, reset_type, checkout_opts); +} + +int git_reset_from_annotated( + git_repository *repo, + const git_annotated_commit *commit, + git_reset_t reset_type, + const git_checkout_options *checkout_opts) +{ + return reset(repo, (git_object *) commit->commit, commit->description, reset_type, checkout_opts); +} diff --git a/src/libgit2/revert.c b/src/libgit2/revert.c new file mode 100644 index 00000000000..2fb53f8f541 --- /dev/null +++ b/src/libgit2/revert.c @@ -0,0 +1,239 @@ +/* +* Copyright (C) the libgit2 contributors. All rights reserved. +* +* This file is part of libgit2, distributed under the GNU GPL v2 with +* a Linking Exception. For full terms see the included COPYING file. +*/ + +#include "common.h" + +#include "repository.h" +#include "filebuf.h" +#include "merge.h" +#include "index.h" + +#include "git2/types.h" +#include "git2/merge.h" +#include "git2/revert.h" +#include "git2/commit.h" +#include "git2/sys/commit.h" + +#define GIT_REVERT_FILE_MODE 0666 + +static int write_revert_head( + git_repository *repo, + const char *commit_oidstr) +{ + git_filebuf file = GIT_FILEBUF_INIT; + git_str file_path = GIT_STR_INIT; + int error = 0; + + if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_REVERT_HEAD_FILE)) >= 0 && + (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_REVERT_FILE_MODE)) >= 0 && + (error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0) + error = git_filebuf_commit(&file); + + if (error < 0) + git_filebuf_cleanup(&file); + + git_str_dispose(&file_path); + + return error; +} + +static int write_merge_msg( + git_repository *repo, + const char *commit_oidstr, + const char *commit_msgline) +{ + git_filebuf file = GIT_FILEBUF_INIT; + git_str file_path = GIT_STR_INIT; + int error = 0; + + if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 || + (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_REVERT_FILE_MODE)) < 0 || + (error = git_filebuf_printf(&file, "Revert \"%s\"\n\nThis reverts commit %s.\n", + commit_msgline, commit_oidstr)) < 0) + goto cleanup; + + error = git_filebuf_commit(&file); + +cleanup: + if (error < 0) + git_filebuf_cleanup(&file); + + git_str_dispose(&file_path); + + return error; +} + +static int revert_normalize_opts( + git_repository *repo, + git_revert_options *opts, + const git_revert_options *given, + const char *their_label) +{ + int error = 0; + unsigned int default_checkout_strategy = GIT_CHECKOUT_ALLOW_CONFLICTS; + + GIT_UNUSED(repo); + + if (given != NULL) + memcpy(opts, given, sizeof(git_revert_options)); + else { + git_revert_options default_opts = GIT_REVERT_OPTIONS_INIT; + memcpy(opts, &default_opts, sizeof(git_revert_options)); + } + + if (!opts->checkout_opts.checkout_strategy) + opts->checkout_opts.checkout_strategy = default_checkout_strategy; + + if (!opts->checkout_opts.our_label) + opts->checkout_opts.our_label = "HEAD"; + + if (!opts->checkout_opts.their_label) + opts->checkout_opts.their_label = their_label; + + return error; +} + +static int revert_state_cleanup(git_repository *repo) +{ + const char *state_files[] = { GIT_REVERT_HEAD_FILE, GIT_MERGE_MSG_FILE }; + + return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files)); +} + +static int revert_seterr(git_commit *commit, const char *fmt) +{ + char commit_id[GIT_OID_MAX_HEXSIZE + 1]; + + git_oid_tostr(commit_id, GIT_OID_MAX_HEXSIZE + 1, git_commit_id(commit)); + git_error_set(GIT_ERROR_REVERT, fmt, commit_id); + + return -1; +} + +int git_revert_commit( + git_index **out, + git_repository *repo, + git_commit *revert_commit, + git_commit *our_commit, + unsigned int mainline, + const git_merge_options *merge_opts) +{ + git_commit *parent_commit = NULL; + git_tree *parent_tree = NULL, *our_tree = NULL, *revert_tree = NULL; + int parent = 0, error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(revert_commit); + GIT_ASSERT_ARG(our_commit); + + if (git_commit_parentcount(revert_commit) > 1) { + if (!mainline) + return revert_seterr(revert_commit, + "mainline branch is not specified but %s is a merge commit"); + + parent = mainline; + } else { + if (mainline) + return revert_seterr(revert_commit, + "mainline branch specified but %s is not a merge commit"); + + parent = git_commit_parentcount(revert_commit); + } + + if (parent && + ((error = git_commit_parent(&parent_commit, revert_commit, (parent - 1))) < 0 || + (error = git_commit_tree(&parent_tree, parent_commit)) < 0)) + goto done; + + if ((error = git_commit_tree(&revert_tree, revert_commit)) < 0 || + (error = git_commit_tree(&our_tree, our_commit)) < 0) + goto done; + + error = git_merge_trees(out, repo, revert_tree, our_tree, parent_tree, merge_opts); + +done: + git_tree_free(parent_tree); + git_tree_free(our_tree); + git_tree_free(revert_tree); + git_commit_free(parent_commit); + + return error; +} + +int git_revert( + git_repository *repo, + git_commit *commit, + const git_revert_options *given_opts) +{ + git_revert_options opts; + git_reference *our_ref = NULL; + git_commit *our_commit = NULL; + char commit_id[GIT_OID_MAX_HEXSIZE + 1]; + const char *commit_msg; + git_str their_label = GIT_STR_INIT; + git_index *index = NULL; + git_indexwriter indexwriter = GIT_INDEXWRITER_INIT; + int error; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(commit); + + GIT_ERROR_CHECK_VERSION(given_opts, GIT_REVERT_OPTIONS_VERSION, "git_revert_options"); + + if ((error = git_repository__ensure_not_bare(repo, "revert")) < 0) + return error; + + git_oid_tostr(commit_id, GIT_OID_MAX_HEXSIZE + 1, git_commit_id(commit)); + + if ((commit_msg = git_commit_summary(commit)) == NULL) { + error = -1; + goto on_error; + } + + if ((error = git_str_printf(&their_label, "parent of %.7s... %s", commit_id, commit_msg)) < 0 || + (error = revert_normalize_opts(repo, &opts, given_opts, git_str_cstr(&their_label))) < 0 || + (error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 || + (error = write_revert_head(repo, commit_id)) < 0 || + (error = write_merge_msg(repo, commit_id, commit_msg)) < 0 || + (error = git_repository_head(&our_ref, repo)) < 0 || + (error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJECT_COMMIT)) < 0 || + (error = git_revert_commit(&index, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 || + (error = git_merge__check_result(repo, index)) < 0 || + (error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0 || + (error = git_checkout_index(repo, index, &opts.checkout_opts)) < 0 || + (error = git_indexwriter_commit(&indexwriter)) < 0) + goto on_error; + + goto done; + +on_error: + revert_state_cleanup(repo); + +done: + git_indexwriter_cleanup(&indexwriter); + git_index_free(index); + git_commit_free(our_commit); + git_reference_free(our_ref); + git_str_dispose(&their_label); + + return error; +} + +int git_revert_options_init(git_revert_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_revert_options, GIT_REVERT_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_revert_init_options(git_revert_options *opts, unsigned int version) +{ + return git_revert_options_init(opts, version); +} +#endif diff --git a/src/libgit2/revparse.c b/src/libgit2/revparse.c new file mode 100644 index 00000000000..2238ba5269c --- /dev/null +++ b/src/libgit2/revparse.c @@ -0,0 +1,968 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "str.h" +#include "tree.h" +#include "refdb.h" +#include "regexp.h" +#include "date.h" + +#include "git2.h" + +static int maybe_sha_or_abbrev( + git_object **out, + git_repository *repo, + const char *spec, + size_t speclen) +{ + git_oid oid; + + if (git_oid_from_prefix(&oid, spec, speclen, repo->oid_type) < 0) + return GIT_ENOTFOUND; + + return git_object_lookup_prefix(out, repo, &oid, speclen, GIT_OBJECT_ANY); +} + +static int maybe_sha( + git_object **out, + git_repository *repo, + const char *spec) +{ + size_t speclen = strlen(spec); + + if (speclen != git_oid_hexsize(repo->oid_type)) + return GIT_ENOTFOUND; + + return maybe_sha_or_abbrev(out, repo, spec, speclen); +} + +static int maybe_abbrev(git_object **out, git_repository *repo, const char *spec) +{ + size_t speclen = strlen(spec); + + return maybe_sha_or_abbrev(out, repo, spec, speclen); +} + +static int build_regex(git_regexp *regex, const char *pattern) +{ + int error; + + if (*pattern == '\0') { + git_error_set(GIT_ERROR_REGEX, "empty pattern"); + return GIT_EINVALIDSPEC; + } + + error = git_regexp_compile(regex, pattern, 0); + if (!error) + return 0; + + git_regexp_dispose(regex); + + return error; +} + +static int maybe_describe(git_object**out, git_repository *repo, const char *spec) +{ + const char *substr; + int error; + git_regexp regex; + + substr = strstr(spec, "-g"); + + if (substr == NULL) + return GIT_ENOTFOUND; + + if (build_regex(®ex, ".+-[0-9]+-g[0-9a-fA-F]+") < 0) + return -1; + + error = git_regexp_match(®ex, spec); + git_regexp_dispose(®ex); + + if (error) + return GIT_ENOTFOUND; + + return maybe_abbrev(out, repo, substr+2); +} + +static int revparse_lookup_object( + git_object **object_out, + git_reference **reference_out, + git_repository *repo, + const char *spec) +{ + int error; + git_reference *ref; + + if ((error = maybe_sha(object_out, repo, spec)) != GIT_ENOTFOUND) + return error; + + error = git_reference_dwim(&ref, repo, spec); + if (!error) { + + error = git_object_lookup( + object_out, repo, git_reference_target(ref), GIT_OBJECT_ANY); + + if (!error) + *reference_out = ref; + + return error; + } + + if (error != GIT_ENOTFOUND) + return error; + + if ((strlen(spec) < git_oid_hexsize(repo->oid_type)) && + ((error = maybe_abbrev(object_out, repo, spec)) != GIT_ENOTFOUND)) + return error; + + if ((error = maybe_describe(object_out, repo, spec)) != GIT_ENOTFOUND) + return error; + + git_error_set(GIT_ERROR_REFERENCE, "revspec '%s' not found", spec); + return GIT_ENOTFOUND; +} + +static int try_parse_numeric(int *n, const char *curly_braces_content) +{ + int32_t content; + const char *end_ptr; + + if (git__strntol32(&content, curly_braces_content, strlen(curly_braces_content), + &end_ptr, 10) < 0) + return -1; + + if (*end_ptr != '\0') + return -1; + + *n = (int)content; + return 0; +} + +static int retrieve_previously_checked_out_branch_or_revision(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, size_t position) +{ + git_reference *ref = NULL; + git_reflog *reflog = NULL; + git_regexp preg; + int error = -1; + size_t i, numentries, cur; + const git_reflog_entry *entry; + const char *msg; + git_str buf = GIT_STR_INIT; + + cur = position; + + if (*identifier != '\0' || *base_ref != NULL) + return GIT_EINVALIDSPEC; + + if (build_regex(&preg, "checkout: moving from (.*) to .*") < 0) + return -1; + + if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0) + goto cleanup; + + if (git_reflog_read(&reflog, repo, GIT_HEAD_FILE) < 0) + goto cleanup; + + numentries = git_reflog_entrycount(reflog); + + for (i = 0; i < numentries; i++) { + git_regmatch regexmatches[2]; + + entry = git_reflog_entry_byindex(reflog, i); + msg = git_reflog_entry_message(entry); + if (!msg) + continue; + + if (git_regexp_search(&preg, msg, 2, regexmatches) < 0) + continue; + + cur--; + + if (cur > 0) + continue; + + if ((git_str_put(&buf, msg+regexmatches[1].start, regexmatches[1].end - regexmatches[1].start)) < 0) + goto cleanup; + + if ((error = git_reference_dwim(base_ref, repo, git_str_cstr(&buf))) == 0) + goto cleanup; + + if (error < 0 && error != GIT_ENOTFOUND) + goto cleanup; + + error = maybe_abbrev(out, repo, git_str_cstr(&buf)); + + goto cleanup; + } + + error = GIT_ENOTFOUND; + +cleanup: + git_reference_free(ref); + git_str_dispose(&buf); + git_regexp_dispose(&preg); + git_reflog_free(reflog); + return error; +} + +static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, size_t identifier) +{ + git_reflog *reflog; + size_t numentries; + const git_reflog_entry *entry = NULL; + bool search_by_pos = (identifier <= 100000000); + + if (git_reflog_read(&reflog, git_reference_owner(ref), git_reference_name(ref)) < 0) + return -1; + + numentries = git_reflog_entrycount(reflog); + + if (search_by_pos) { + if (numentries < identifier + 1) + goto notfound; + + entry = git_reflog_entry_byindex(reflog, identifier); + git_oid_cpy(oid, git_reflog_entry_id_new(entry)); + } else { + size_t i; + git_time commit_time; + + for (i = 0; i < numentries; i++) { + entry = git_reflog_entry_byindex(reflog, i); + commit_time = git_reflog_entry_committer(entry)->when; + + if (commit_time.time > (git_time_t)identifier) + continue; + + git_oid_cpy(oid, git_reflog_entry_id_new(entry)); + break; + } + + if (i == numentries) { + if (entry == NULL) + goto notfound; + + /* + * TODO: emit a warning (log for 'branch' only goes back to ...) + */ + git_oid_cpy(oid, git_reflog_entry_id_new(entry)); + } + } + + git_reflog_free(reflog); + return 0; + +notfound: + git_error_set( + GIT_ERROR_REFERENCE, + "reflog for '%s' has only %"PRIuZ" entries, asked for %"PRIuZ, + git_reference_name(ref), numentries, identifier); + + git_reflog_free(reflog); + return GIT_ENOTFOUND; +} + +static int retrieve_revobject_from_reflog(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, size_t position) +{ + git_reference *ref; + git_oid oid; + int error = -1; + + if (*base_ref == NULL) { + /* + * When HEAD@{n} is specified, do not use dwim, which would resolve the + * reference (to the current branch that HEAD is pointing to). + */ + if (position > 0 && strcmp(identifier, GIT_HEAD_FILE) == 0) + error = git_reference_lookup(&ref, repo, GIT_HEAD_FILE); + else + error = git_reference_dwim(&ref, repo, identifier); + + if (error < 0) + return error; + } else { + ref = *base_ref; + *base_ref = NULL; + } + + if (position == 0) { + error = git_object_lookup(out, repo, git_reference_target(ref), GIT_OBJECT_ANY); + goto cleanup; + } + + if ((error = retrieve_oid_from_reflog(&oid, ref, position)) < 0) + goto cleanup; + + error = git_object_lookup(out, repo, &oid, GIT_OBJECT_ANY); + +cleanup: + git_reference_free(ref); + return error; +} + +static int retrieve_remote_tracking_reference(git_reference **base_ref, const char *identifier, git_repository *repo) +{ + git_reference *tracking, *ref; + int error = -1; + + if (*base_ref == NULL) { + if ((error = git_reference_dwim(&ref, repo, identifier)) < 0) + return error; + } else { + ref = *base_ref; + *base_ref = NULL; + } + + if (!git_reference_is_branch(ref)) { + error = GIT_EINVALIDSPEC; + goto cleanup; + } + + if ((error = git_branch_upstream(&tracking, ref)) < 0) + goto cleanup; + + *base_ref = tracking; + +cleanup: + git_reference_free(ref); + return error; +} + +static int handle_at_syntax(git_object **out, git_reference **ref, const char *spec, size_t identifier_len, git_repository *repo, const char *curly_braces_content) +{ + bool is_numeric; + int parsed = 0, error = -1; + git_str identifier = GIT_STR_INIT; + git_time_t timestamp; + + GIT_ASSERT(*out == NULL); + + if (git_str_put(&identifier, spec, identifier_len) < 0) + return -1; + + is_numeric = !try_parse_numeric(&parsed, curly_braces_content); + + if (*curly_braces_content == '-' && (!is_numeric || parsed == 0)) { + error = GIT_EINVALIDSPEC; + goto cleanup; + } + + if (is_numeric) { + if (parsed < 0) + error = retrieve_previously_checked_out_branch_or_revision(out, ref, repo, git_str_cstr(&identifier), -parsed); + else + error = retrieve_revobject_from_reflog(out, ref, repo, git_str_cstr(&identifier), parsed); + + goto cleanup; + } + + if (!strcmp(curly_braces_content, "u") || !strcmp(curly_braces_content, "upstream")) { + error = retrieve_remote_tracking_reference(ref, git_str_cstr(&identifier), repo); + + goto cleanup; + } + + if (git_date_parse(×tamp, curly_braces_content) < 0) { + error = GIT_EINVALIDSPEC; + goto cleanup; + } + + error = retrieve_revobject_from_reflog(out, ref, repo, git_str_cstr(&identifier), (size_t)timestamp); + +cleanup: + git_str_dispose(&identifier); + return error; +} + +static git_object_t parse_obj_type(const char *str) +{ + if (!strcmp(str, "commit")) + return GIT_OBJECT_COMMIT; + + if (!strcmp(str, "tree")) + return GIT_OBJECT_TREE; + + if (!strcmp(str, "blob")) + return GIT_OBJECT_BLOB; + + if (!strcmp(str, "tag")) + return GIT_OBJECT_TAG; + + return GIT_OBJECT_INVALID; +} + +static int dereference_to_non_tag(git_object **out, git_object *obj) +{ + if (git_object_type(obj) == GIT_OBJECT_TAG) + return git_tag_peel(out, (git_tag *)obj); + + return git_object_dup(out, obj); +} + +static int handle_caret_parent_syntax(git_object **out, git_object *obj, int n) +{ + git_object *temp_commit = NULL; + int error; + + if ((error = git_object_peel(&temp_commit, obj, GIT_OBJECT_COMMIT)) < 0) + return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ? + GIT_EINVALIDSPEC : error; + + if (n == 0) { + *out = temp_commit; + return 0; + } + + error = git_commit_parent((git_commit **)out, (git_commit*)temp_commit, n - 1); + + git_object_free(temp_commit); + return error; +} + +static int handle_linear_syntax(git_object **out, git_object *obj, int n) +{ + git_object *temp_commit = NULL; + int error; + + if ((error = git_object_peel(&temp_commit, obj, GIT_OBJECT_COMMIT)) < 0) + return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ? + GIT_EINVALIDSPEC : error; + + error = git_commit_nth_gen_ancestor((git_commit **)out, (git_commit*)temp_commit, n); + + git_object_free(temp_commit); + return error; +} + +static int handle_colon_syntax( + git_object **out, + git_object *obj, + const char *path) +{ + git_object *tree; + int error = -1; + git_tree_entry *entry = NULL; + + if ((error = git_object_peel(&tree, obj, GIT_OBJECT_TREE)) < 0) + return error == GIT_ENOTFOUND ? GIT_EINVALIDSPEC : error; + + if (*path == '\0') { + *out = tree; + return 0; + } + + /* + * TODO: Handle the relative path syntax + * (:./relative/path and :../relative/path) + */ + if ((error = git_tree_entry_bypath(&entry, (git_tree *)tree, path)) < 0) + goto cleanup; + + error = git_tree_entry_to_object(out, git_object_owner(tree), entry); + +cleanup: + git_tree_entry_free(entry); + git_object_free(tree); + + return error; +} + +static int walk_and_search(git_object **out, git_revwalk *walk, git_regexp *regex) +{ + int error; + git_oid oid; + git_object *obj; + + while (!(error = git_revwalk_next(&oid, walk))) { + + error = git_object_lookup(&obj, git_revwalk_repository(walk), &oid, GIT_OBJECT_COMMIT); + if ((error < 0) && (error != GIT_ENOTFOUND)) + return -1; + + if (!git_regexp_match(regex, git_commit_message((git_commit*)obj))) { + *out = obj; + return 0; + } + + git_object_free(obj); + } + + if (error < 0 && error == GIT_ITEROVER) + error = GIT_ENOTFOUND; + + return error; +} + +static int handle_grep_syntax(git_object **out, git_repository *repo, const git_oid *spec_oid, const char *pattern) +{ + git_regexp preg; + git_revwalk *walk = NULL; + int error; + + if ((error = build_regex(&preg, pattern)) < 0) + return error; + + if ((error = git_revwalk_new(&walk, repo)) < 0) + goto cleanup; + + git_revwalk_sorting(walk, GIT_SORT_TIME); + + if (spec_oid == NULL) { + if ((error = git_revwalk_push_glob(walk, "refs/*")) < 0) + goto cleanup; + } else if ((error = git_revwalk_push(walk, spec_oid)) < 0) + goto cleanup; + + error = walk_and_search(out, walk, &preg); + +cleanup: + git_regexp_dispose(&preg); + git_revwalk_free(walk); + + return error; +} + +static int handle_caret_curly_syntax(git_object **out, git_object *obj, const char *curly_braces_content) +{ + git_object_t expected_type; + + if (*curly_braces_content == '\0') + return dereference_to_non_tag(out, obj); + + if (*curly_braces_content == '/') + return handle_grep_syntax(out, git_object_owner(obj), git_object_id(obj), curly_braces_content + 1); + + expected_type = parse_obj_type(curly_braces_content); + + if (expected_type == GIT_OBJECT_INVALID) + return GIT_EINVALIDSPEC; + + return git_object_peel(out, obj, expected_type); +} + +static int extract_curly_braces_content(git_str *buf, const char *spec, size_t *pos) +{ + git_str_clear(buf); + + GIT_ASSERT_ARG(spec[*pos] == '^' || spec[*pos] == '@'); + + (*pos)++; + + if (spec[*pos] == '\0' || spec[*pos] != '{') + return GIT_EINVALIDSPEC; + + (*pos)++; + + while (spec[*pos] != '}') { + if (spec[*pos] == '\0') + return GIT_EINVALIDSPEC; + + if (git_str_putc(buf, spec[(*pos)++]) < 0) + return -1; + } + + (*pos)++; + + return 0; +} + +static int extract_path(git_str *buf, const char *spec, size_t *pos) +{ + git_str_clear(buf); + + GIT_ASSERT_ARG(spec[*pos] == ':'); + + (*pos)++; + + if (git_str_puts(buf, spec + *pos) < 0) + return -1; + + *pos += git_str_len(buf); + + return 0; +} + +static int extract_how_many(int *n, const char *spec, size_t *pos) +{ + const char *end_ptr; + int parsed, accumulated; + char kind = spec[*pos]; + + GIT_ASSERT_ARG(spec[*pos] == '^' || spec[*pos] == '~'); + + accumulated = 0; + + do { + do { + (*pos)++; + accumulated++; + } while (spec[(*pos)] == kind && kind == '~'); + + if (git__isdigit(spec[*pos])) { + if (git__strntol32(&parsed, spec + *pos, strlen(spec + *pos), &end_ptr, 10) < 0) + return GIT_EINVALIDSPEC; + + accumulated += (parsed - 1); + *pos = end_ptr - spec; + } + + } while (spec[(*pos)] == kind && kind == '~'); + + *n = accumulated; + + return 0; +} + +static int object_from_reference(git_object **object, git_reference *reference) +{ + git_reference *resolved = NULL; + int error; + + if (git_reference_resolve(&resolved, reference) < 0) + return -1; + + error = git_object_lookup(object, reference->db->repo, git_reference_target(resolved), GIT_OBJECT_ANY); + git_reference_free(resolved); + + return error; +} + +static int ensure_base_rev_loaded(git_object **object, git_reference **reference, const char *spec, size_t identifier_len, git_repository *repo, bool allow_empty_identifier) +{ + int error; + git_str identifier = GIT_STR_INIT; + + if (*object != NULL) + return 0; + + if (*reference != NULL) + return object_from_reference(object, *reference); + + if (!allow_empty_identifier && identifier_len == 0) + return GIT_EINVALIDSPEC; + + if (git_str_put(&identifier, spec, identifier_len) < 0) + return -1; + + error = revparse_lookup_object(object, reference, repo, git_str_cstr(&identifier)); + git_str_dispose(&identifier); + + return error; +} + +static int ensure_base_rev_is_not_known_yet(git_object *object) +{ + if (object == NULL) + return 0; + + return GIT_EINVALIDSPEC; +} + +static bool any_left_hand_identifier(git_object *object, git_reference *reference, size_t identifier_len) +{ + if (object != NULL) + return true; + + if (reference != NULL) + return true; + + if (identifier_len > 0) + return true; + + return false; +} + +static int ensure_left_hand_identifier_is_not_known_yet(git_object *object, git_reference *reference) +{ + if (!ensure_base_rev_is_not_known_yet(object) && reference == NULL) + return 0; + + return GIT_EINVALIDSPEC; +} + +static int revparse( + git_object **object_out, + git_reference **reference_out, + size_t *identifier_len_out, + git_repository *repo, + const char *spec) +{ + size_t pos = 0, identifier_len = 0; + int error = -1, n; + git_str buf = GIT_STR_INIT; + + git_reference *reference = NULL; + git_object *base_rev = NULL; + + bool should_return_reference = true; + bool parsed = false; + + GIT_ASSERT_ARG(object_out); + GIT_ASSERT_ARG(reference_out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(spec); + + *object_out = NULL; + *reference_out = NULL; + + while (!parsed && spec[pos]) { + switch (spec[pos]) { + case '^': + should_return_reference = false; + + if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0) + goto cleanup; + + if (spec[pos+1] == '{') { + git_object *temp_object = NULL; + + if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0) + goto cleanup; + + if ((error = handle_caret_curly_syntax(&temp_object, base_rev, git_str_cstr(&buf))) < 0) + goto cleanup; + + git_object_free(base_rev); + base_rev = temp_object; + } else { + git_object *temp_object = NULL; + + if ((error = extract_how_many(&n, spec, &pos)) < 0) + goto cleanup; + + if ((error = handle_caret_parent_syntax(&temp_object, base_rev, n)) < 0) + goto cleanup; + + git_object_free(base_rev); + base_rev = temp_object; + } + break; + + case '~': + { + git_object *temp_object = NULL; + + should_return_reference = false; + + if ((error = extract_how_many(&n, spec, &pos)) < 0) + goto cleanup; + + if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0) + goto cleanup; + + if ((error = handle_linear_syntax(&temp_object, base_rev, n)) < 0) + goto cleanup; + + git_object_free(base_rev); + base_rev = temp_object; + break; + } + + case ':': + { + git_object *temp_object = NULL; + + should_return_reference = false; + + if ((error = extract_path(&buf, spec, &pos)) < 0) + goto cleanup; + + if (any_left_hand_identifier(base_rev, reference, identifier_len)) { + if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, true)) < 0) + goto cleanup; + + if ((error = handle_colon_syntax(&temp_object, base_rev, git_str_cstr(&buf))) < 0) + goto cleanup; + } else { + if (*git_str_cstr(&buf) == '/') { + if ((error = handle_grep_syntax(&temp_object, repo, NULL, git_str_cstr(&buf) + 1)) < 0) + goto cleanup; + } else { + + /* + * TODO: support merge-stage path lookup (":2:Makefile") + * and plain index blob lookup (:i-am/a/blob) + */ + git_error_set(GIT_ERROR_INVALID, "unimplemented"); + error = GIT_ERROR; + goto cleanup; + } + } + + git_object_free(base_rev); + base_rev = temp_object; + break; + } + + case '@': + if (spec[pos+1] == '{') { + git_object *temp_object = NULL; + + if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0) + goto cleanup; + + if ((error = ensure_base_rev_is_not_known_yet(base_rev)) < 0) + goto cleanup; + + if ((error = handle_at_syntax(&temp_object, &reference, spec, identifier_len, repo, git_str_cstr(&buf))) < 0) + goto cleanup; + + if (temp_object != NULL) + base_rev = temp_object; + break; + } else if (spec[pos + 1] == '\0' && !pos) { + spec = "HEAD"; + identifier_len = 4; + parsed = true; + break; + } + /* fall through */ + + default: + if ((error = ensure_left_hand_identifier_is_not_known_yet(base_rev, reference)) < 0) + goto cleanup; + + pos++; + identifier_len++; + } + } + + if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0) + goto cleanup; + + if (!should_return_reference) { + git_reference_free(reference); + reference = NULL; + } + + *object_out = base_rev; + *reference_out = reference; + *identifier_len_out = identifier_len; + error = 0; + +cleanup: + if (error) { + if (error == GIT_EINVALIDSPEC) + git_error_set(GIT_ERROR_INVALID, + "failed to parse revision specifier - Invalid pattern '%s'", spec); + + git_object_free(base_rev); + git_reference_free(reference); + } + + git_str_dispose(&buf); + return error; +} + +int git_revparse_ext( + git_object **object_out, + git_reference **reference_out, + git_repository *repo, + const char *spec) +{ + int error; + size_t identifier_len; + git_object *obj = NULL; + git_reference *ref = NULL; + + if ((error = revparse(&obj, &ref, &identifier_len, repo, spec)) < 0) + goto cleanup; + + *object_out = obj; + *reference_out = ref; + GIT_UNUSED(identifier_len); + + return 0; + +cleanup: + git_object_free(obj); + git_reference_free(ref); + return error; +} + +int git_revparse_single(git_object **out, git_repository *repo, const char *spec) +{ + int error; + git_object *obj = NULL; + git_reference *ref = NULL; + + *out = NULL; + + if ((error = git_revparse_ext(&obj, &ref, repo, spec)) < 0) + goto cleanup; + + git_reference_free(ref); + + *out = obj; + + return 0; + +cleanup: + git_object_free(obj); + git_reference_free(ref); + return error; +} + +int git_revparse( + git_revspec *revspec, + git_repository *repo, + const char *spec) +{ + const char *dotdot; + int error = 0; + + GIT_ASSERT_ARG(revspec); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(spec); + + memset(revspec, 0x0, sizeof(*revspec)); + + if ((dotdot = strstr(spec, "..")) != NULL) { + char *lstr; + const char *rstr; + revspec->flags = GIT_REVSPEC_RANGE; + + /* + * Following git.git, don't allow '..' because it makes command line + * arguments which can be either paths or revisions ambiguous when the + * path is almost certainly intended. The empty range '...' is still + * allowed. + */ + if (!git__strcmp(spec, "..")) { + git_error_set(GIT_ERROR_INVALID, "invalid pattern '..'"); + return GIT_EINVALIDSPEC; + } + + lstr = git__substrdup(spec, dotdot - spec); + rstr = dotdot + 2; + if (dotdot[2] == '.') { + revspec->flags |= GIT_REVSPEC_MERGE_BASE; + rstr++; + } + + error = git_revparse_single( + &revspec->from, + repo, + *lstr == '\0' ? "HEAD" : lstr); + + if (!error) { + error = git_revparse_single( + &revspec->to, + repo, + *rstr == '\0' ? "HEAD" : rstr); + } + + git__free((void*)lstr); + } else { + revspec->flags = GIT_REVSPEC_SINGLE; + error = git_revparse_single(&revspec->from, repo, spec); + } + + return error; +} diff --git a/src/libgit2/revwalk.c b/src/libgit2/revwalk.c new file mode 100644 index 00000000000..a793a8e179c --- /dev/null +++ b/src/libgit2/revwalk.c @@ -0,0 +1,849 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "revwalk.h" + +#include "commit.h" +#include "odb.h" +#include "pool.h" + +#include "git2/revparse.h" +#include "merge.h" +#include "vector.h" +#include "hashmap_oid.h" + +GIT_HASHMAP_OID_FUNCTIONS(git_revwalk_oidmap, GIT_HASHMAP_INLINE, git_commit_list_node *); + +static int get_revision(git_commit_list_node **out, git_revwalk *walk, git_commit_list **list); + +git_commit_list_node *git_revwalk__commit_lookup( + git_revwalk *walk, const git_oid *oid) +{ + git_commit_list_node *commit; + + /* lookup and reserve space if not already present */ + if (git_revwalk_oidmap_get(&commit, &walk->commits, oid) == 0) + return commit; + + commit = git_commit_list_alloc_node(walk); + if (commit == NULL) + return NULL; + + git_oid_cpy(&commit->oid, oid); + + if (git_revwalk_oidmap_put(&walk->commits, &commit->oid, commit) < 0) + return NULL; + + return commit; +} + +int git_revwalk__push_commit(git_revwalk *walk, const git_oid *oid, const git_revwalk__push_options *opts) +{ + git_oid commit_id; + int error; + git_object *obj, *oobj; + git_commit_list_node *commit; + git_commit_list *list; + + if ((error = git_object_lookup(&oobj, walk->repo, oid, GIT_OBJECT_ANY)) < 0) + return error; + + error = git_object_peel(&obj, oobj, GIT_OBJECT_COMMIT); + git_object_free(oobj); + + if (error == GIT_ENOTFOUND || error == GIT_EINVALIDSPEC || error == GIT_EPEEL) { + /* If this comes from e.g. push_glob("tags"), ignore this */ + if (opts->from_glob) + return 0; + + git_error_set(GIT_ERROR_INVALID, "object is not a committish"); + return error; + } + if (error < 0) + return error; + + git_oid_cpy(&commit_id, git_object_id(obj)); + git_object_free(obj); + + commit = git_revwalk__commit_lookup(walk, &commit_id); + if (commit == NULL) + return -1; /* error already reported by failed lookup */ + + /* A previous hide already told us we don't want this commit */ + if (commit->uninteresting) + return 0; + + if (opts->uninteresting) { + walk->limited = 1; + walk->did_hide = 1; + } else { + walk->did_push = 1; + } + + commit->uninteresting = opts->uninteresting; + list = walk->user_input; + + /* To insert by date, we need to parse so we know the date. */ + if (opts->insert_by_date && ((error = git_commit_list_parse(walk, commit)) < 0)) + return error; + + if ((opts->insert_by_date == 0 || + git_commit_list_insert_by_date(commit, &list) == NULL) && + git_commit_list_insert(commit, &list) == NULL) { + git_error_set_oom(); + return -1; + } + + walk->user_input = list; + + return 0; +} + +int git_revwalk_push(git_revwalk *walk, const git_oid *oid) +{ + git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT; + + GIT_ASSERT_ARG(walk); + GIT_ASSERT_ARG(oid); + + return git_revwalk__push_commit(walk, oid, &opts); +} + + +int git_revwalk_hide(git_revwalk *walk, const git_oid *oid) +{ + git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT; + + GIT_ASSERT_ARG(walk); + GIT_ASSERT_ARG(oid); + + opts.uninteresting = 1; + return git_revwalk__push_commit(walk, oid, &opts); +} + +int git_revwalk__push_ref(git_revwalk *walk, const char *refname, const git_revwalk__push_options *opts) +{ + git_oid oid; + + int error = git_reference_name_to_id(&oid, walk->repo, refname); + if (opts->from_glob && (error == GIT_ENOTFOUND || error == GIT_EINVALIDSPEC || error == GIT_EPEEL)) { + return 0; + } else if (error < 0) { + return -1; + } + + return git_revwalk__push_commit(walk, &oid, opts); +} + +int git_revwalk__push_glob(git_revwalk *walk, const char *glob, const git_revwalk__push_options *given_opts) +{ + git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT; + int error = 0; + git_str buf = GIT_STR_INIT; + git_reference *ref; + git_reference_iterator *iter; + size_t wildcard; + + GIT_ASSERT_ARG(walk); + GIT_ASSERT_ARG(glob); + + if (given_opts) + memcpy(&opts, given_opts, sizeof(opts)); + + /* refs/ is implied if not given in the glob */ + if (git__prefixcmp(glob, GIT_REFS_DIR) != 0) + git_str_joinpath(&buf, GIT_REFS_DIR, glob); + else + git_str_puts(&buf, glob); + GIT_ERROR_CHECK_ALLOC_STR(&buf); + + /* If no '?', '*' or '[' exist, we append '/ *' to the glob */ + wildcard = strcspn(glob, "?*["); + if (!glob[wildcard]) + git_str_put(&buf, "/*", 2); + + if ((error = git_reference_iterator_glob_new(&iter, walk->repo, buf.ptr)) < 0) + goto out; + + opts.from_glob = true; + while ((error = git_reference_next(&ref, iter)) == 0) { + error = git_revwalk__push_ref(walk, git_reference_name(ref), &opts); + git_reference_free(ref); + if (error < 0) + break; + } + git_reference_iterator_free(iter); + + if (error == GIT_ITEROVER) + error = 0; +out: + git_str_dispose(&buf); + return error; +} + +int git_revwalk_push_glob(git_revwalk *walk, const char *glob) +{ + git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT; + + GIT_ASSERT_ARG(walk); + GIT_ASSERT_ARG(glob); + + return git_revwalk__push_glob(walk, glob, &opts); +} + +int git_revwalk_hide_glob(git_revwalk *walk, const char *glob) +{ + git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT; + + GIT_ASSERT_ARG(walk); + GIT_ASSERT_ARG(glob); + + opts.uninteresting = 1; + return git_revwalk__push_glob(walk, glob, &opts); +} + +int git_revwalk_push_head(git_revwalk *walk) +{ + git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT; + + GIT_ASSERT_ARG(walk); + + return git_revwalk__push_ref(walk, GIT_HEAD_FILE, &opts); +} + +int git_revwalk_hide_head(git_revwalk *walk) +{ + git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT; + + GIT_ASSERT_ARG(walk); + + opts.uninteresting = 1; + return git_revwalk__push_ref(walk, GIT_HEAD_FILE, &opts); +} + +int git_revwalk_push_ref(git_revwalk *walk, const char *refname) +{ + git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT; + + GIT_ASSERT_ARG(walk); + GIT_ASSERT_ARG(refname); + + return git_revwalk__push_ref(walk, refname, &opts); +} + +int git_revwalk_push_range(git_revwalk *walk, const char *range) +{ + git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT; + git_revspec revspec; + int error = 0; + + if ((error = git_revparse(&revspec, walk->repo, range))) + return error; + + if (!revspec.to) { + git_error_set(GIT_ERROR_INVALID, "invalid revspec: range not provided"); + error = GIT_EINVALIDSPEC; + goto out; + } + + if (revspec.flags & GIT_REVSPEC_MERGE_BASE) { + /* TODO: support "..." */ + git_error_set(GIT_ERROR_INVALID, "symmetric differences not implemented in revwalk"); + error = GIT_EINVALIDSPEC; + goto out; + } + + opts.uninteresting = 1; + if ((error = git_revwalk__push_commit(walk, git_object_id(revspec.from), &opts))) + goto out; + + opts.uninteresting = 0; + error = git_revwalk__push_commit(walk, git_object_id(revspec.to), &opts); + +out: + git_object_free(revspec.from); + git_object_free(revspec.to); + return error; +} + +int git_revwalk_hide_ref(git_revwalk *walk, const char *refname) +{ + git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT; + + GIT_ASSERT_ARG(walk); + GIT_ASSERT_ARG(refname); + + opts.uninteresting = 1; + return git_revwalk__push_ref(walk, refname, &opts); +} + +static int revwalk_enqueue_timesort(git_revwalk *walk, git_commit_list_node *commit) +{ + return git_pqueue_insert(&walk->iterator_time, commit); +} + +static int revwalk_enqueue_unsorted(git_revwalk *walk, git_commit_list_node *commit) +{ + return git_commit_list_insert(commit, &walk->iterator_rand) ? 0 : -1; +} + +static int revwalk_next_timesort(git_commit_list_node **object_out, git_revwalk *walk) +{ + git_commit_list_node *next; + + while ((next = git_pqueue_pop(&walk->iterator_time)) != NULL) { + /* Some commits might become uninteresting after being added to the list */ + if (!next->uninteresting) { + *object_out = next; + return 0; + } + } + + git_error_clear(); + return GIT_ITEROVER; +} + +static int revwalk_next_unsorted(git_commit_list_node **object_out, git_revwalk *walk) +{ + int error; + git_commit_list_node *next; + + while (!(error = get_revision(&next, walk, &walk->iterator_rand))) { + /* Some commits might become uninteresting after being added to the list */ + if (!next->uninteresting) { + *object_out = next; + return 0; + } + } + + return error; +} + +static int revwalk_next_toposort(git_commit_list_node **object_out, git_revwalk *walk) +{ + int error; + git_commit_list_node *next; + + while (!(error = get_revision(&next, walk, &walk->iterator_topo))) { + /* Some commits might become uninteresting after being added to the list */ + if (!next->uninteresting) { + *object_out = next; + return 0; + } + } + + return error; +} + +static int revwalk_next_reverse(git_commit_list_node **object_out, git_revwalk *walk) +{ + *object_out = git_commit_list_pop(&walk->iterator_reverse); + return *object_out ? 0 : GIT_ITEROVER; +} + +static void mark_parents_uninteresting(git_commit_list_node *commit) +{ + unsigned short i; + git_commit_list *parents = NULL; + + for (i = 0; i < commit->out_degree; i++) + git_commit_list_insert(commit->parents[i], &parents); + + + while (parents) { + commit = git_commit_list_pop(&parents); + + while (commit) { + if (commit->uninteresting) + break; + + commit->uninteresting = 1; + /* + * If we've reached this commit some other way + * already, we need to mark its parents uninteresting + * as well. + */ + if (!commit->parents) + break; + + for (i = 0; i < commit->out_degree; i++) + git_commit_list_insert(commit->parents[i], &parents); + commit = commit->parents[0]; + } + } +} + +static int add_parents_to_list(git_revwalk *walk, git_commit_list_node *commit, git_commit_list **list) +{ + unsigned short i; + int error; + + if (commit->added) + return 0; + + commit->added = 1; + + /* + * Go full on in the uninteresting case as we want to include + * as many of these as we can. + * + * Usually we haven't parsed the parent of a parent, but if we + * have it we reached it via other means so we want to mark + * its parents recursively too. + */ + if (commit->uninteresting) { + for (i = 0; i < commit->out_degree; i++) { + git_commit_list_node *p = commit->parents[i]; + p->uninteresting = 1; + + /* git does it gently here, but we don't like missing objects */ + if ((error = git_commit_list_parse(walk, p)) < 0) + return error; + + if (p->parents) + mark_parents_uninteresting(p); + + p->seen = 1; + git_commit_list_insert_by_date(p, list); + } + + return 0; + } + + /* + * Now on to what we do if the commit is indeed + * interesting. Here we do want things like first-parent take + * effect as this is what we'll be showing. + */ + for (i = 0; i < commit->out_degree; i++) { + git_commit_list_node *p = commit->parents[i]; + + if ((error = git_commit_list_parse(walk, p)) < 0) + return error; + + if (walk->hide_cb && walk->hide_cb(&p->oid, walk->hide_cb_payload)) + continue; + + if (!p->seen) { + p->seen = 1; + git_commit_list_insert_by_date(p, list); + } + + if (walk->first_parent) + break; + } + return 0; +} + +/* How many uninteresting commits we want to look at after we run out of interesting ones */ +#define SLOP 5 + +static int still_interesting(git_commit_list *list, int64_t time, int slop) +{ + /* The empty list is pretty boring */ + if (!list) + return 0; + + /* + * If the destination list has commits with an earlier date than our + * source, we want to reset the slop counter as we're not done. + */ + if (time <= list->item->time) + return SLOP; + + for (; list; list = list->next) { + /* + * If the destination list still contains interesting commits we + * want to continue looking. + */ + if (!list->item->uninteresting || list->item->time > time) + return SLOP; + } + + /* Everything's uninteresting, reduce the count */ + return slop - 1; +} + +static int limit_list(git_commit_list **out, git_revwalk *walk, git_commit_list *commits) +{ + int error, slop = SLOP; + int64_t time = INT64_MAX; + git_commit_list *list = commits; + git_commit_list *newlist = NULL; + git_commit_list **p = &newlist; + + while (list) { + git_commit_list_node *commit = git_commit_list_pop(&list); + + if ((error = add_parents_to_list(walk, commit, &list)) < 0) + return error; + + if (commit->uninteresting) { + mark_parents_uninteresting(commit); + + slop = still_interesting(list, time, slop); + if (slop) + continue; + + break; + } + + if (walk->hide_cb && walk->hide_cb(&commit->oid, walk->hide_cb_payload)) + continue; + + time = commit->time; + p = &git_commit_list_insert(commit, p)->next; + } + + git_commit_list_free(&list); + *out = newlist; + return 0; +} + +static int get_revision(git_commit_list_node **out, git_revwalk *walk, git_commit_list **list) +{ + int error; + git_commit_list_node *commit; + + commit = git_commit_list_pop(list); + if (!commit) { + git_error_clear(); + return GIT_ITEROVER; + } + + /* + * If we did not run limit_list and we must add parents to the + * list ourselves. + */ + if (!walk->limited) { + if ((error = add_parents_to_list(walk, commit, list)) < 0) + return error; + } + + *out = commit; + return 0; +} + +static int sort_in_topological_order(git_commit_list **out, git_revwalk *walk, git_commit_list *list) +{ + git_commit_list *ll = NULL, *newlist, **pptr; + git_commit_list_node *next; + git_pqueue queue; + git_vector_cmp queue_cmp = NULL; + unsigned short i; + int error; + + if (walk->sorting & GIT_SORT_TIME) + queue_cmp = git_commit_list_time_cmp; + + if ((error = git_pqueue_init(&queue, 0, 8, queue_cmp))) + return error; + + /* + * Start by resetting the in-degree to 1 for the commits in + * our list. We want to go through this list again, so we + * store it in the commit list as we extract it from the lower + * machinery. + */ + for (ll = list; ll; ll = ll->next) { + ll->item->in_degree = 1; + } + + /* + * Count up how many children each commit has. We limit + * ourselves to those commits in the original list (in-degree + * of 1) avoiding setting it for any parent that was hidden. + */ + for(ll = list; ll; ll = ll->next) { + for (i = 0; i < ll->item->out_degree; ++i) { + git_commit_list_node *parent = ll->item->parents[i]; + if (parent->in_degree) + parent->in_degree++; + } + } + + /* + * Now we find the tips i.e. those not reachable from any other node + * i.e. those which still have an in-degree of 1. + */ + for(ll = list; ll; ll = ll->next) { + if (ll->item->in_degree == 1) { + if ((error = git_pqueue_insert(&queue, ll->item))) + goto cleanup; + } + } + + /* + * We need to output the tips in the order that they came out of the + * traversal, so if we're not doing time-sorting, we need to reverse the + * pqueue in order to get them to come out as we inserted them. + */ + if ((walk->sorting & GIT_SORT_TIME) == 0) + git_pqueue_reverse(&queue); + + + pptr = &newlist; + newlist = NULL; + while ((next = git_pqueue_pop(&queue)) != NULL) { + for (i = 0; i < next->out_degree; ++i) { + git_commit_list_node *parent = next->parents[i]; + if (parent->in_degree == 0) + continue; + + if (--parent->in_degree == 1) { + if ((error = git_pqueue_insert(&queue, parent))) + goto cleanup; + } + } + + /* All the children of 'item' have been emitted (since we got to it via the priority queue) */ + next->in_degree = 0; + + pptr = &git_commit_list_insert(next, pptr)->next; + } + + *out = newlist; + error = 0; + +cleanup: + git_pqueue_free(&queue); + return error; +} + +static int prepare_walk(git_revwalk *walk) +{ + int error = 0; + git_commit_list *list, *commits = NULL, *commits_last = NULL; + git_commit_list_node *next; + + /* If there were no pushes, we know that the walk is already over */ + if (!walk->did_push) { + git_error_clear(); + return GIT_ITEROVER; + } + + /* + * This is a bit convoluted, but necessary to maintain the order of + * the commits. This is especially important in situations where + * git_revwalk__push_glob is called with a git_revwalk__push_options + * setting insert_by_date = 1, which is critical for fetch negotiation. + */ + for (list = walk->user_input; list; list = list->next) { + git_commit_list_node *commit = list->item; + if ((error = git_commit_list_parse(walk, commit)) < 0) + return error; + + if (commit->uninteresting) + mark_parents_uninteresting(commit); + + if (!commit->seen) { + git_commit_list *new_list = NULL; + if ((new_list = git_commit_list_create(commit, NULL)) == NULL) { + git_error_set_oom(); + return -1; + } + + commit->seen = 1; + if (commits_last == NULL) + commits = new_list; + else + commits_last->next = new_list; + + commits_last = new_list; + } + } + + if (walk->limited && (error = limit_list(&commits, walk, commits)) < 0) + return error; + + if (walk->sorting & GIT_SORT_TOPOLOGICAL) { + error = sort_in_topological_order(&walk->iterator_topo, walk, commits); + git_commit_list_free(&commits); + + if (error < 0) + return error; + + walk->get_next = &revwalk_next_toposort; + } else if (walk->sorting & GIT_SORT_TIME) { + for (list = commits; list && !error; list = list->next) + error = walk->enqueue(walk, list->item); + + git_commit_list_free(&commits); + + if (error < 0) + return error; + } else { + walk->iterator_rand = commits; + walk->get_next = revwalk_next_unsorted; + } + + if (walk->sorting & GIT_SORT_REVERSE) { + + while ((error = walk->get_next(&next, walk)) == 0) + if (git_commit_list_insert(next, &walk->iterator_reverse) == NULL) + return -1; + + if (error != GIT_ITEROVER) + return error; + + walk->get_next = &revwalk_next_reverse; + } + + walk->walking = 1; + return 0; +} + + +int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo) +{ + git_revwalk *walk = git__calloc(1, sizeof(git_revwalk)); + GIT_ERROR_CHECK_ALLOC(walk); + + if (git_pqueue_init(&walk->iterator_time, 0, 8, git_commit_list_time_cmp) < 0 || + git_pool_init(&walk->commit_pool, COMMIT_ALLOC) < 0) + return -1; + + walk->get_next = &revwalk_next_unsorted; + walk->enqueue = &revwalk_enqueue_unsorted; + + walk->repo = repo; + + if (git_repository_odb(&walk->odb, repo) < 0) { + git_revwalk_free(walk); + return -1; + } + + *revwalk_out = walk; + return 0; +} + +void git_revwalk_free(git_revwalk *walk) +{ + if (walk == NULL) + return; + + git_revwalk_reset(walk); + git_odb_free(walk->odb); + + git_revwalk_oidmap_dispose(&walk->commits); + git_pool_clear(&walk->commit_pool); + git_pqueue_free(&walk->iterator_time); + git__free(walk); +} + +git_repository *git_revwalk_repository(git_revwalk *walk) +{ + GIT_ASSERT_ARG_WITH_RETVAL(walk, NULL); + + return walk->repo; +} + +int git_revwalk_sorting(git_revwalk *walk, unsigned int sort_mode) +{ + GIT_ASSERT_ARG(walk); + + if (walk->walking) + git_revwalk_reset(walk); + + walk->sorting = sort_mode; + + if (walk->sorting & GIT_SORT_TIME) { + walk->get_next = &revwalk_next_timesort; + walk->enqueue = &revwalk_enqueue_timesort; + } else { + walk->get_next = &revwalk_next_unsorted; + walk->enqueue = &revwalk_enqueue_unsorted; + } + + if (walk->sorting != GIT_SORT_NONE) + walk->limited = 1; + + return 0; +} + +int git_revwalk_simplify_first_parent(git_revwalk *walk) +{ + walk->first_parent = 1; + return 0; +} + +int git_revwalk_next(git_oid *oid, git_revwalk *walk) +{ + int error; + git_commit_list_node *next; + + GIT_ASSERT_ARG(walk); + GIT_ASSERT_ARG(oid); + + if (!walk->walking) { + if ((error = prepare_walk(walk)) < 0) + return error; + } + + error = walk->get_next(&next, walk); + + if (error == GIT_ITEROVER) { + git_revwalk_reset(walk); + git_error_clear(); + return GIT_ITEROVER; + } + + if (!error) + git_oid_cpy(oid, &next->oid); + + return error; +} + +int git_revwalk_reset(git_revwalk *walk) +{ + git_commit_list_node *commit; + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + + GIT_ASSERT_ARG(walk); + + while (git_revwalk_oidmap_iterate(&iter, NULL, &commit, &walk->commits) == 0) { + commit->seen = 0; + commit->in_degree = 0; + commit->topo_delay = 0; + commit->uninteresting = 0; + commit->added = 0; + commit->flags = 0; + } + + git_pqueue_clear(&walk->iterator_time); + git_commit_list_free(&walk->iterator_topo); + git_commit_list_free(&walk->iterator_rand); + git_commit_list_free(&walk->iterator_reverse); + git_commit_list_free(&walk->user_input); + walk->first_parent = 0; + walk->walking = 0; + walk->limited = 0; + walk->did_push = walk->did_hide = 0; + walk->sorting = GIT_SORT_NONE; + + return 0; +} + +int git_revwalk_add_hide_cb( + git_revwalk *walk, + git_revwalk_hide_cb hide_cb, + void *payload) +{ + GIT_ASSERT_ARG(walk); + + if (walk->walking) + git_revwalk_reset(walk); + + walk->hide_cb = hide_cb; + walk->hide_cb_payload = payload; + + if (hide_cb) + walk->limited = 1; + + return 0; +} + diff --git a/src/libgit2/revwalk.h b/src/libgit2/revwalk.h new file mode 100644 index 00000000000..632b2807cd8 --- /dev/null +++ b/src/libgit2/revwalk.h @@ -0,0 +1,73 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_revwalk_h__ +#define INCLUDE_revwalk_h__ + +#include "common.h" + +#include "git2/revwalk.h" +#include "commit_list.h" +#include "pqueue.h" +#include "pool.h" +#include "vector.h" +#include "hashmap_oid.h" + +GIT_HASHMAP_OID_STRUCT(git_revwalk_oidmap, git_commit_list_node *); + +struct git_revwalk { + git_repository *repo; + git_odb *odb; + + git_revwalk_oidmap commits; + git_pool commit_pool; + + git_commit_list *iterator_topo; + git_commit_list *iterator_rand; + git_commit_list *iterator_reverse; + git_pqueue iterator_time; + + int (*get_next)(git_commit_list_node **, git_revwalk *); + int (*enqueue)(git_revwalk *, git_commit_list_node *); + + unsigned walking:1, + first_parent: 1, + did_hide: 1, + did_push: 1, + limited: 1; + unsigned int sorting; + + /* the pushes and hides */ + git_commit_list *user_input; + + /* hide callback */ + git_revwalk_hide_cb hide_cb; + void *hide_cb_payload; +}; + +git_commit_list_node *git_revwalk__commit_lookup(git_revwalk *walk, const git_oid *oid); + +typedef struct { + int uninteresting; + int from_glob; + int insert_by_date; +} git_revwalk__push_options; + +#define GIT_REVWALK__PUSH_OPTIONS_INIT { 0 } + +int git_revwalk__push_commit(git_revwalk *walk, + const git_oid *oid, + const git_revwalk__push_options *opts); + +int git_revwalk__push_ref(git_revwalk *walk, + const char *refname, + const git_revwalk__push_options *opts); + +int git_revwalk__push_glob(git_revwalk *walk, + const char *glob, + const git_revwalk__push_options *given_opts); + +#endif diff --git a/src/libgit2/settings.c b/src/libgit2/settings.c new file mode 100644 index 00000000000..2e000f3c69f --- /dev/null +++ b/src/libgit2/settings.c @@ -0,0 +1,493 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "settings.h" + +#include +#include "alloc.h" +#include "buf.h" +#include "cache.h" +#include "common.h" +#include "filter.h" +#include "grafts.h" +#include "hash.h" +#include "index.h" +#include "merge_driver.h" +#include "pool.h" +#include "mwindow.h" +#include "object.h" +#include "odb.h" +#include "rand.h" +#include "refs.h" +#include "runtime.h" +#include "sysdir.h" +#include "thread.h" +#include "git2/global.h" +#include "streams/registry.h" +#include "streams/mbedtls.h" +#include "streams/openssl.h" +#include "streams/socket.h" +#include "transports/smart.h" +#include "transports/http.h" +#include "transports/ssh_libssh2.h" + +#ifdef GIT_WIN32 +# include "win32/w32_leakcheck.h" +#endif + +/* Declarations for tuneable settings */ +extern size_t git_mwindow__window_size; +extern size_t git_mwindow__mapped_limit; +extern size_t git_mwindow__file_limit; +extern size_t git_indexer__max_objects; +extern bool git_disable_pack_keep_file_checks; +extern int git_odb__packed_priority; +extern int git_odb__loose_priority; +extern int git_socket_stream__connect_timeout; +extern int git_socket_stream__timeout; + +char *git__user_agent; +char *git__user_agent_product; +char *git__ssl_ciphers; + +static void settings_global_shutdown(void) +{ + git__free(git__user_agent); + git__free(git__user_agent_product); + + git__free(git__ssl_ciphers); + git_repository__free_extensions(); +} + +int git_settings_global_init(void) +{ + return git_runtime_shutdown_register(settings_global_shutdown); +} + +static int config_level_to_sysdir(int *out, int config_level) +{ + switch (config_level) { + case GIT_CONFIG_LEVEL_SYSTEM: + *out = GIT_SYSDIR_SYSTEM; + return 0; + case GIT_CONFIG_LEVEL_XDG: + *out = GIT_SYSDIR_XDG; + return 0; + case GIT_CONFIG_LEVEL_GLOBAL: + *out = GIT_SYSDIR_GLOBAL; + return 0; + case GIT_CONFIG_LEVEL_PROGRAMDATA: + *out = GIT_SYSDIR_PROGRAMDATA; + return 0; + default: + break; + } + + git_error_set( + GIT_ERROR_INVALID, "invalid config path selector %d", config_level); + return -1; +} + +const char *git_settings__user_agent_product(void) +{ + return git__user_agent_product ? git__user_agent_product : + "git/2.0"; +} + +const char *git_settings__user_agent(void) +{ + return git__user_agent ? git__user_agent : + "libgit2 " LIBGIT2_VERSION; +} + +int git_libgit2_opts(int key, ...) +{ + int error = 0; + va_list ap; + + va_start(ap, key); + + switch (key) { + case GIT_OPT_SET_MWINDOW_SIZE: + git_mwindow__window_size = va_arg(ap, size_t); + break; + + case GIT_OPT_GET_MWINDOW_SIZE: + *(va_arg(ap, size_t *)) = git_mwindow__window_size; + break; + + case GIT_OPT_SET_MWINDOW_MAPPED_LIMIT: + git_mwindow__mapped_limit = va_arg(ap, size_t); + break; + + case GIT_OPT_GET_MWINDOW_MAPPED_LIMIT: + *(va_arg(ap, size_t *)) = git_mwindow__mapped_limit; + break; + + case GIT_OPT_SET_MWINDOW_FILE_LIMIT: + git_mwindow__file_limit = va_arg(ap, size_t); + break; + + case GIT_OPT_GET_MWINDOW_FILE_LIMIT: + *(va_arg(ap, size_t *)) = git_mwindow__file_limit; + break; + + case GIT_OPT_GET_SEARCH_PATH: + { + int sysdir = va_arg(ap, int); + git_buf *out = va_arg(ap, git_buf *); + git_str str = GIT_STR_INIT; + const git_str *tmp; + int level; + + if ((error = git_buf_tostr(&str, out)) < 0 || + (error = config_level_to_sysdir(&level, sysdir)) < 0 || + (error = git_sysdir_get(&tmp, level)) < 0 || + (error = git_str_put(&str, tmp->ptr, tmp->size)) < 0) + break; + + error = git_buf_fromstr(out, &str); + } + break; + + case GIT_OPT_SET_SEARCH_PATH: + { + int level; + + if ((error = config_level_to_sysdir(&level, va_arg(ap, int))) >= 0) + error = git_sysdir_set(level, va_arg(ap, const char *)); + } + break; + + case GIT_OPT_SET_CACHE_OBJECT_LIMIT: + { + git_object_t type = (git_object_t)va_arg(ap, int); + size_t size = va_arg(ap, size_t); + error = git_cache_set_max_object_size(type, size); + break; + } + + case GIT_OPT_SET_CACHE_MAX_SIZE: + git_cache__max_storage = va_arg(ap, ssize_t); + break; + + case GIT_OPT_ENABLE_CACHING: + git_cache__enabled = (va_arg(ap, int) != 0); + break; + + case GIT_OPT_GET_CACHED_MEMORY: + *(va_arg(ap, ssize_t *)) = git_cache__current_storage.val; + *(va_arg(ap, ssize_t *)) = git_cache__max_storage; + break; + + case GIT_OPT_GET_TEMPLATE_PATH: + { + git_buf *out = va_arg(ap, git_buf *); + git_str str = GIT_STR_INIT; + const git_str *tmp; + + if ((error = git_buf_tostr(&str, out)) < 0 || + (error = git_sysdir_get(&tmp, GIT_SYSDIR_TEMPLATE)) < 0 || + (error = git_str_put(&str, tmp->ptr, tmp->size)) < 0) + break; + + error = git_buf_fromstr(out, &str); + } + break; + + case GIT_OPT_SET_TEMPLATE_PATH: + error = git_sysdir_set(GIT_SYSDIR_TEMPLATE, va_arg(ap, const char *)); + break; + + case GIT_OPT_SET_SSL_CERT_LOCATIONS: +#if defined(GIT_HTTPS_OPENSSL) || defined(GIT_HTTPS_OPENSSL_DYNAMIC) + { + const char *file = va_arg(ap, const char *); + const char *path = va_arg(ap, const char *); + error = git_openssl__set_cert_location(file, path); + } +#elif defined(GIT_HTTPS_MBEDTLS) + { + const char *file = va_arg(ap, const char *); + const char *path = va_arg(ap, const char *); + error = git_mbedtls__set_cert_location(file, path); + } +#else + git_error_set(GIT_ERROR_SSL, "TLS backend doesn't support certificate locations"); + error = -1; +#endif + break; + + case GIT_OPT_ADD_SSL_X509_CERT: +#if defined(GIT_HTTPS_OPENSSL) || defined(GIT_HTTPS_OPENSSL_DYNAMIC) + { + X509 *cert = va_arg(ap, X509 *); + error = git_openssl__add_x509_cert(cert); + } +#else + git_error_set(GIT_ERROR_SSL, "TLS backend doesn't support adding of the raw certs"); + error = -1; +#endif + break; + + case GIT_OPT_SET_USER_AGENT: + { + const char *new_agent = va_arg(ap, const char *); + + git__free(git__user_agent); + + if (new_agent) { + git__user_agent= git__strdup(new_agent); + + if (!git__user_agent) + error = -1; + } else { + git__user_agent = NULL; + } + } + break; + + case GIT_OPT_GET_USER_AGENT: + { + git_buf *out = va_arg(ap, git_buf *); + git_str str = GIT_STR_INIT; + + if ((error = git_buf_tostr(&str, out)) < 0 || + (error = git_str_puts(&str, git_settings__user_agent())) < 0) + break; + + error = git_buf_fromstr(out, &str); + } + break; + + case GIT_OPT_SET_USER_AGENT_PRODUCT: + { + const char *new_agent = va_arg(ap, const char *); + + git__free(git__user_agent_product); + + if (new_agent) { + git__user_agent_product = git__strdup(new_agent); + + if (!git__user_agent_product) + error = -1; + } else { + git__user_agent_product = NULL; + } + } + break; + + case GIT_OPT_GET_USER_AGENT_PRODUCT: + { + git_buf *out = va_arg(ap, git_buf *); + git_str str = GIT_STR_INIT; + + if ((error = git_buf_tostr(&str, out)) < 0 || + (error = git_str_puts(&str, git_settings__user_agent_product())) < 0) + break; + + error = git_buf_fromstr(out, &str); + } + break; + + case GIT_OPT_ENABLE_STRICT_OBJECT_CREATION: + git_object__strict_input_validation = (va_arg(ap, int) != 0); + break; + + case GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION: + git_reference__enable_symbolic_ref_target_validation = (va_arg(ap, int) != 0); + break; + + case GIT_OPT_SET_SSL_CIPHERS: +#if defined(GIT_HTTPS_OPENSSL) || \ + defined(GIT_HTTPS_OPENSSL_DYNAMIC) || \ + defined(GIT_HTTPS_MBEDTLS) + { + git__free(git__ssl_ciphers); + git__ssl_ciphers = git__strdup(va_arg(ap, const char *)); + if (!git__ssl_ciphers) { + git_error_set_oom(); + error = -1; + } + } +#else + git_error_set(GIT_ERROR_SSL, "TLS backend doesn't support custom ciphers"); + error = -1; +#endif + break; + + case GIT_OPT_ENABLE_OFS_DELTA: + git_smart__ofs_delta_enabled = (va_arg(ap, int) != 0); + break; + + case GIT_OPT_ENABLE_FSYNC_GITDIR: + git_repository__fsync_gitdir = (va_arg(ap, int) != 0); + break; + + case GIT_OPT_GET_WINDOWS_SHAREMODE: +#ifdef GIT_WIN32 + *(va_arg(ap, unsigned long *)) = git_win32__createfile_sharemode; +#endif + break; + + case GIT_OPT_SET_WINDOWS_SHAREMODE: +#ifdef GIT_WIN32 + git_win32__createfile_sharemode = va_arg(ap, unsigned long); +#endif + break; + + case GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION: + git_odb__strict_hash_verification = (va_arg(ap, int) != 0); + break; + + case GIT_OPT_SET_ALLOCATOR: + error = git_allocator_setup(va_arg(ap, git_allocator *)); + break; + + case GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY: + git_index__enforce_unsaved_safety = (va_arg(ap, int) != 0); + break; + + case GIT_OPT_SET_PACK_MAX_OBJECTS: + git_indexer__max_objects = va_arg(ap, size_t); + break; + + case GIT_OPT_GET_PACK_MAX_OBJECTS: + *(va_arg(ap, size_t *)) = git_indexer__max_objects; + break; + + case GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS: + git_disable_pack_keep_file_checks = (va_arg(ap, int) != 0); + break; + + case GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE: + git_http__expect_continue = (va_arg(ap, int) != 0); + break; + + case GIT_OPT_SET_ODB_PACKED_PRIORITY: + git_odb__packed_priority = va_arg(ap, int); + break; + + case GIT_OPT_SET_ODB_LOOSE_PRIORITY: + git_odb__loose_priority = va_arg(ap, int); + break; + + case GIT_OPT_SET_EXTENSIONS: + { + const char **extensions = va_arg(ap, const char **); + size_t len = va_arg(ap, size_t); + error = git_repository__set_extensions(extensions, len); + } + break; + + case GIT_OPT_GET_EXTENSIONS: + { + git_strarray *out = va_arg(ap, git_strarray *); + char **extensions; + size_t len; + + if ((error = git_repository__extensions(&extensions, &len)) < 0) + break; + + out->strings = extensions; + out->count = len; + } + break; + + case GIT_OPT_GET_OWNER_VALIDATION: + *(va_arg(ap, int *)) = git_repository__validate_ownership; + break; + + case GIT_OPT_SET_OWNER_VALIDATION: + git_repository__validate_ownership = (va_arg(ap, int) != 0); + break; + + case GIT_OPT_GET_HOMEDIR: + { + git_buf *out = va_arg(ap, git_buf *); + git_str str = GIT_STR_INIT; + const git_str *tmp; + + if ((error = git_buf_tostr(&str, out)) < 0 || + (error = git_sysdir_get(&tmp, GIT_SYSDIR_HOME)) < 0 || + (error = git_str_put(&str, tmp->ptr, tmp->size)) < 0) + break; + + error = git_buf_fromstr(out, &str); + } + break; + + case GIT_OPT_SET_HOMEDIR: + error = git_sysdir_set(GIT_SYSDIR_HOME, va_arg(ap, const char *)); + break; + + case GIT_OPT_GET_SERVER_CONNECT_TIMEOUT: + *(va_arg(ap, int *)) = git_socket_stream__connect_timeout; + break; + + case GIT_OPT_SET_SERVER_CONNECT_TIMEOUT: + { + int timeout = va_arg(ap, int); + + if (timeout < 0) { + git_error_set(GIT_ERROR_INVALID, "invalid connect timeout"); + error = -1; + } else { + git_socket_stream__connect_timeout = timeout; + } + } + break; + + case GIT_OPT_GET_SERVER_TIMEOUT: + *(va_arg(ap, int *)) = git_socket_stream__timeout; + break; + + case GIT_OPT_SET_SERVER_TIMEOUT: + { + int timeout = va_arg(ap, int); + + if (timeout < 0) { + git_error_set(GIT_ERROR_INVALID, "invalid timeout"); + error = -1; + } else { + git_socket_stream__timeout = timeout; + } + } + break; + + default: + git_error_set(GIT_ERROR_INVALID, "invalid option key"); + error = -1; + } + + va_end(ap); + + return error; +} + +const char *git_libgit2_buildinfo(git_buildinfo_t key) +{ + switch (key) { + +#ifdef GIT_BUILD_CPU + case GIT_BUILDINFO_CPU: + return GIT_BUILD_CPU; + break; +#endif + +#ifdef GIT_BUILD_COMMIT + case GIT_BUILDINFO_COMMIT: + return GIT_BUILD_COMMIT; + break; +#endif + + default: + break; + } + + return NULL; +} diff --git a/src/libgit2/settings.h b/src/libgit2/settings.h new file mode 100644 index 00000000000..292936676aa --- /dev/null +++ b/src/libgit2/settings.h @@ -0,0 +1,15 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_settings_h__ +#define INCLUDE_settings_h__ + +extern int git_settings_global_init(void); + +extern const char *git_settings__user_agent(void); +extern const char *git_settings__user_agent_product(void); + +#endif diff --git a/src/libgit2/signature.c b/src/libgit2/signature.c new file mode 100644 index 00000000000..71da4162371 --- /dev/null +++ b/src/libgit2/signature.c @@ -0,0 +1,456 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "signature.h" + +#include "repository.h" +#include "git2/common.h" +#include "posix.h" +#include "date.h" + +void git_signature_free(git_signature *sig) +{ + if (sig == NULL) + return; + + git__free(sig->name); + sig->name = NULL; + git__free(sig->email); + sig->email = NULL; + git__free(sig); +} + +static int signature_parse_error(const char *msg) +{ + git_error_set(GIT_ERROR_INVALID, "failed to parse signature - %s", msg); + return GIT_EINVALID; +} + +static int signature_error(const char *msg) +{ + git_error_set(GIT_ERROR_INVALID, "failed to parse signature - %s", msg); + return -1; +} + +static bool contains_angle_brackets(const char *input) +{ + return strchr(input, '<') != NULL || strchr(input, '>') != NULL; +} + +static bool is_crud(unsigned char c) +{ + return c <= 32 || + c == ',' || + c == ':' || + c == ';' || + c == '<' || + c == '>' || + c == '"' || + c == '\\' || + c == '\''; +} + +static char *extract_trimmed(const char *ptr, size_t len) +{ + while (len && is_crud((unsigned char)ptr[0])) { + ptr++; len--; + } + + while (len && is_crud((unsigned char)ptr[len - 1])) { + len--; + } + + return git__substrdup(ptr, len); +} + +int git_signature_new(git_signature **sig_out, const char *name, const char *email, git_time_t time, int offset) +{ + git_signature *p = NULL; + + GIT_ASSERT_ARG(name); + GIT_ASSERT_ARG(email); + + *sig_out = NULL; + + if (contains_angle_brackets(name) || + contains_angle_brackets(email)) { + return signature_error( + "Neither `name` nor `email` should contain angle brackets chars."); + } + + p = git__calloc(1, sizeof(git_signature)); + GIT_ERROR_CHECK_ALLOC(p); + + p->name = extract_trimmed(name, strlen(name)); + GIT_ERROR_CHECK_ALLOC(p->name); + p->email = extract_trimmed(email, strlen(email)); + GIT_ERROR_CHECK_ALLOC(p->email); + + if (p->name[0] == '\0' || p->email[0] == '\0') { + git_signature_free(p); + return signature_error("Signature cannot have an empty name or email"); + } + + p->when.time = time; + p->when.offset = offset; + p->when.sign = (offset < 0) ? '-' : '+'; + + *sig_out = p; + return 0; +} + +int git_signature_dup(git_signature **dest, const git_signature *source) +{ + git_signature *signature; + + if (source == NULL) + return 0; + + signature = git__calloc(1, sizeof(git_signature)); + GIT_ERROR_CHECK_ALLOC(signature); + + signature->name = git__strdup(source->name); + GIT_ERROR_CHECK_ALLOC(signature->name); + + signature->email = git__strdup(source->email); + GIT_ERROR_CHECK_ALLOC(signature->email); + + signature->when.time = source->when.time; + signature->when.offset = source->when.offset; + signature->when.sign = source->when.sign; + + *dest = signature; + + return 0; +} + +int git_signature__pdup(git_signature **dest, const git_signature *source, git_pool *pool) +{ + git_signature *signature; + + if (source == NULL) + return 0; + + signature = git_pool_mallocz(pool, sizeof(git_signature)); + GIT_ERROR_CHECK_ALLOC(signature); + + signature->name = git_pool_strdup(pool, source->name); + GIT_ERROR_CHECK_ALLOC(signature->name); + + signature->email = git_pool_strdup(pool, source->email); + GIT_ERROR_CHECK_ALLOC(signature->email); + + signature->when.time = source->when.time; + signature->when.offset = source->when.offset; + signature->when.sign = source->when.sign; + + *dest = signature; + + return 0; +} + +static void current_time(time_t *now_out, int *offset_out) +{ + time_t offset; + struct tm _utc, *utc_tm; + + /* + * Get the current time as seconds since the epoch and + * transform that into a tm struct containing the time at + * UTC. Give that to mktime which considers it a local time + * (tm_isdst = -1 asks it to take DST into account) and gives + * us that time as seconds since the epoch. The difference + * between its return value and 'now' is our offset to UTC. + */ + time(now_out); + utc_tm = p_gmtime_r(now_out, &_utc); + utc_tm->tm_isdst = -1; + offset = (time_t)difftime(*now_out, mktime(utc_tm)); + offset /= 60; + + *offset_out = (int)offset; +} + +int git_signature_now( + git_signature **sig_out, + const char *name, + const char *email) +{ + time_t now; + int offset; + + current_time(&now, &offset); + + return git_signature_new(sig_out, name, email, now, offset); +} + +int git_signature_default(git_signature **out, git_repository *repo) +{ + int error; + git_config *cfg; + const char *user_name, *user_email; + + if ((error = git_repository_config_snapshot(&cfg, repo)) < 0) + return error; + + if (!(error = git_config_get_string(&user_name, cfg, "user.name")) && + !(error = git_config_get_string(&user_email, cfg, "user.email"))) + error = git_signature_now(out, user_name, user_email); + + git_config_free(cfg); + return error; +} + +static int user_from_env( + git_signature **out, + git_repository *repo, + const char *name_env_var, + const char *email_env_var, + const char *date_env_var, + time_t default_time, + int default_offset) +{ + int error; + git_config *cfg; + const char *name, *email, *date; + git_time_t timestamp; + int offset; + git_str name_env = GIT_STR_INIT; + git_str email_env = GIT_STR_INIT; + git_str date_env = GIT_STR_INIT; + + if ((error = git_repository_config_snapshot(&cfg, repo)) < 0) + return error; + + /* Check if the environment variable for the name is set */ + if (!(git__getenv(&name_env, name_env_var))) { + name = git_str_cstr(&name_env); + } else { + /* or else read the configuration value. */ + if ((error = git_config_get_string(&name, cfg, "user.name")) < 0) + goto done; + } + + /* Check if the environment variable for the email is set. */ + if (!(git__getenv(&email_env, email_env_var))) { + email = git_str_cstr(&email_env); + } else { + if ((error = git_config_get_string(&email, cfg, "user.email")) == GIT_ENOTFOUND) { + git_error *last_error; + + git_error_save(&last_error); + + if ((error = git__getenv(&email_env, "EMAIL")) < 0) { + git_error_restore(last_error); + error = GIT_ENOTFOUND; + goto done; + } + + email = git_str_cstr(&email_env); + git_error_free(last_error); + } else if (error < 0) { + goto done; + } + } + + /* Check if the environment variable for the timestamp is set */ + if (!(git__getenv(&date_env, date_env_var))) { + date = git_str_cstr(&date_env); + + if ((error = git_date_offset_parse(×tamp, &offset, date)) < 0) + goto done; + } else { + timestamp = default_time; + offset = default_offset; + } + + error = git_signature_new(out, name, email, timestamp, offset); + +done: + git_config_free(cfg); + git_str_dispose(&name_env); + git_str_dispose(&email_env); + git_str_dispose(&date_env); + return error; +} + +int git_signature_default_from_env( + git_signature **author_out, + git_signature **committer_out, + git_repository *repo) +{ + git_signature *author = NULL, *committer = NULL; + time_t now; + int offset; + int error; + + GIT_ASSERT_ARG(author_out || committer_out); + GIT_ASSERT_ARG(repo); + + current_time(&now, &offset); + + if (author_out && + (error = user_from_env(&author, repo, "GIT_AUTHOR_NAME", + "GIT_AUTHOR_EMAIL", "GIT_AUTHOR_DATE", + now, offset)) < 0) + goto on_error; + + if (committer_out && + (error = user_from_env(&committer, repo, "GIT_COMMITTER_NAME", + "GIT_COMMITTER_EMAIL", "GIT_COMMITTER_DATE", + now, offset)) < 0) + goto on_error; + + if (author_out) + *author_out = author; + + if (committer_out) + *committer_out = committer; + + return 0; + +on_error: + git__free(author); + git__free(committer); + return error; +} + +int git_signature__parse(git_signature *sig, const char **buffer_out, + const char *buffer_end, const char *header, char ender) +{ + const char *buffer = *buffer_out; + const char *email_start, *email_end; + + memset(sig, 0, sizeof(git_signature)); + + if (ender && + (buffer_end = memchr(buffer, ender, buffer_end - buffer)) == NULL) + return signature_parse_error("no newline given"); + + if (header) { + const size_t header_len = strlen(header); + + if (buffer + header_len >= buffer_end || memcmp(buffer, header, header_len) != 0) + return signature_parse_error("expected prefix doesn't match actual"); + + buffer += header_len; + } + + email_start = git__memrchr(buffer, '<', buffer_end - buffer); + email_end = git__memrchr(buffer, '>', buffer_end - buffer); + + if (!email_start || !email_end || email_end <= email_start) + return signature_parse_error("malformed e-mail"); + + email_start += 1; + sig->name = extract_trimmed(buffer, email_start - buffer - 1); + sig->email = extract_trimmed(email_start, email_end - email_start); + + /* Do we even have a time at the end of the signature? */ + if (email_end + 2 < buffer_end) { + const char *time_start = email_end + 2; + const char *time_end; + + if (git__strntol64(&sig->when.time, time_start, + buffer_end - time_start, &time_end, 10) < 0) { + git__free(sig->name); + git__free(sig->email); + sig->name = sig->email = NULL; + return signature_parse_error("invalid Unix timestamp"); + } + + /* do we have a timezone? */ + if (time_end + 1 < buffer_end) { + int offset, hours, mins; + const char *tz_start, *tz_end; + + tz_start = time_end + 1; + + if ((tz_start[0] != '-' && tz_start[0] != '+') || + git__strntol32(&offset, tz_start + 1, + buffer_end - tz_start - 1, &tz_end, 10) < 0) { + /* malformed timezone, just assume it's zero */ + offset = 0; + } + + hours = offset / 100; + mins = offset % 100; + + /* + * only store timezone if it's not overflowing; + * see http://www.worldtimezone.com/faq.html + */ + if (hours <= 14 && mins <= 59) { + sig->when.offset = (hours * 60) + mins; + sig->when.sign = tz_start[0]; + if (tz_start[0] == '-') + sig->when.offset = -sig->when.offset; + } + } + } + + *buffer_out = buffer_end + 1; + return 0; +} + +int git_signature_from_buffer(git_signature **out, const char *buf) +{ + git_signature *sig; + const char *buf_end; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(buf); + + *out = NULL; + + sig = git__calloc(1, sizeof(git_signature)); + GIT_ERROR_CHECK_ALLOC(sig); + + buf_end = buf + strlen(buf); + error = git_signature__parse(sig, &buf, buf_end, NULL, '\0'); + + if (error) + git__free(sig); + else + *out = sig; + + return error; +} + +void git_signature__writebuf(git_str *buf, const char *header, const git_signature *sig) +{ + int offset, hours, mins; + char sign; + + offset = sig->when.offset; + sign = (sig->when.offset < 0 || sig->when.sign == '-') ? '-' : '+'; + + if (offset < 0) + offset = -offset; + + hours = offset / 60; + mins = offset % 60; + + git_str_printf(buf, "%s%s <%s> %u %c%02d%02d\n", + header ? header : "", sig->name, sig->email, + (unsigned)sig->when.time, sign, hours, mins); +} + +bool git_signature__equal(const git_signature *one, const git_signature *two) +{ + GIT_ASSERT_ARG(one); + GIT_ASSERT_ARG(two); + + return + git__strcmp(one->name, two->name) == 0 && + git__strcmp(one->email, two->email) == 0 && + one->when.time == two->when.time && + one->when.offset == two->when.offset && + one->when.sign == two->when.sign; +} + diff --git a/src/libgit2/signature.h b/src/libgit2/signature.h new file mode 100644 index 00000000000..a5645e9bba5 --- /dev/null +++ b/src/libgit2/signature.h @@ -0,0 +1,22 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_signature_h__ +#define INCLUDE_signature_h__ + +#include "common.h" + +#include "git2/common.h" +#include "git2/signature.h" +#include "repository.h" +#include + +int git_signature__parse(git_signature *sig, const char **buffer_out, const char *buffer_end, const char *header, char ender); +void git_signature__writebuf(git_str *buf, const char *header, const git_signature *sig); +bool git_signature__equal(const git_signature *one, const git_signature *two); +int git_signature__pdup(git_signature **dest, const git_signature *source, git_pool *pool); + +#endif diff --git a/src/libgit2/stash.c b/src/libgit2/stash.c new file mode 100644 index 00000000000..23c82b408fd --- /dev/null +++ b/src/libgit2/stash.c @@ -0,0 +1,1290 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "repository.h" +#include "commit.h" +#include "tree.h" +#include "reflog.h" +#include "blob.h" +#include "git2/diff.h" +#include "git2/stash.h" +#include "git2/status.h" +#include "git2/checkout.h" +#include "git2/index.h" +#include "git2/transaction.h" +#include "git2/merge.h" +#include "index.h" +#include "signature.h" +#include "iterator.h" +#include "merge.h" +#include "diff.h" +#include "diff_generate.h" +#include "strarray.h" + +static int create_error(int error, const char *msg) +{ + git_error_set(GIT_ERROR_STASH, "cannot stash changes - %s", msg); + return error; +} + +static int retrieve_head(git_reference **out, git_repository *repo) +{ + int error = git_repository_head(out, repo); + + if (error == GIT_EUNBORNBRANCH) + return create_error(error, "you do not have the initial commit yet."); + + return error; +} + +static int append_abbreviated_oid(git_str *out, const git_oid *b_commit) +{ + char *formatted_oid; + + formatted_oid = git_oid_allocfmt(b_commit); + GIT_ERROR_CHECK_ALLOC(formatted_oid); + + git_str_put(out, formatted_oid, 7); + git__free(formatted_oid); + + return git_str_oom(out) ? -1 : 0; +} + +static int append_commit_description(git_str *out, git_commit *commit) +{ + const char *summary = git_commit_summary(commit); + GIT_ERROR_CHECK_ALLOC(summary); + + if (append_abbreviated_oid(out, git_commit_id(commit)) < 0) + return -1; + + git_str_putc(out, ' '); + git_str_puts(out, summary); + git_str_putc(out, '\n'); + + return git_str_oom(out) ? -1 : 0; +} + +static int retrieve_base_commit_and_message( + git_commit **b_commit, + git_str *stash_message, + git_repository *repo) +{ + git_reference *head = NULL; + int error; + + if ((error = retrieve_head(&head, repo)) < 0) + return error; + + if (strcmp("HEAD", git_reference_name(head)) == 0) + error = git_str_puts(stash_message, "(no branch): "); + else + error = git_str_printf( + stash_message, + "%s: ", + git_reference_name(head) + strlen(GIT_REFS_HEADS_DIR)); + if (error < 0) + goto cleanup; + + if ((error = git_commit_lookup( + b_commit, repo, git_reference_target(head))) < 0) + goto cleanup; + + if ((error = append_commit_description(stash_message, *b_commit)) < 0) + goto cleanup; + +cleanup: + git_reference_free(head); + return error; +} + +static int build_tree_from_index( + git_tree **out, + git_repository *repo, + git_index *index) +{ + int error; + git_oid i_tree_oid; + + if ((error = git_index_write_tree_to(&i_tree_oid, index, repo)) < 0) + return error; + + return git_tree_lookup(out, repo, &i_tree_oid); +} + +static int commit_index( + git_commit **i_commit, + git_repository *repo, + git_index *index, + const git_signature *stasher, + const char *message, + const git_commit *parent) +{ + git_tree *i_tree = NULL; + git_oid i_commit_oid; + git_str msg = GIT_STR_INIT; + int error; + + if ((error = build_tree_from_index(&i_tree, repo, index)) < 0) + goto cleanup; + + if ((error = git_str_printf(&msg, "index on %s\n", message)) < 0) + goto cleanup; + + if ((error = git_commit_create( + &i_commit_oid, + git_index_owner(index), + NULL, + stasher, + stasher, + NULL, + git_str_cstr(&msg), + i_tree, + 1, + &parent)) < 0) + goto cleanup; + + error = git_commit_lookup(i_commit, git_index_owner(index), &i_commit_oid); + +cleanup: + git_tree_free(i_tree); + git_str_dispose(&msg); + return error; +} + +struct stash_update_rules { + bool include_changed; + bool include_untracked; + bool include_ignored; +}; + +/* + * Similar to git_index_add_bypath but able to operate on any + * index without making assumptions about the repository's index + */ +static int stash_to_index( + git_repository *repo, + git_index *index, + const char *path) +{ + git_index *repo_index = NULL; + git_index_entry entry = {{0}}; + struct stat st; + int error; + + if (!git_repository_is_bare(repo) && + (error = git_repository_index__weakptr(&repo_index, repo)) < 0) + return error; + + if ((error = git_blob__create_from_paths( + &entry.id, &st, repo, NULL, path, 0, true)) < 0) + return error; + + git_index_entry__init_from_stat(&entry, &st, + (repo_index == NULL || !repo_index->distrust_filemode)); + + entry.path = path; + + return git_index_add(index, &entry); +} + +static int stash_update_index_from_paths( + git_repository *repo, + git_index *index, + const git_strarray *paths) +{ + unsigned int status_flags; + size_t i; + int error = 0; + + for (i = 0; i < paths->count; i++) { + git_status_file(&status_flags, repo, paths->strings[i]); + + if (status_flags & (GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_DELETED)) { + if ((error = git_index_remove(index, paths->strings[i], 0)) < 0) + return error; + } else { + if ((error = stash_to_index(repo, index, paths->strings[i])) < 0) + return error; + } + } + + return error; +} + +static int stash_update_index_from_diff( + git_repository *repo, + git_index *index, + const git_diff *diff, + struct stash_update_rules *data) +{ + int error = 0; + size_t d, max_d = git_diff_num_deltas(diff); + + for (d = 0; !error && d < max_d; ++d) { + const char *add_path = NULL; + const git_diff_delta *delta = git_diff_get_delta(diff, d); + + switch (delta->status) { + case GIT_DELTA_IGNORED: + if (data->include_ignored) + add_path = delta->new_file.path; + break; + + case GIT_DELTA_UNTRACKED: + if (data->include_untracked && + delta->new_file.mode != GIT_FILEMODE_TREE) + add_path = delta->new_file.path; + break; + + case GIT_DELTA_ADDED: + case GIT_DELTA_MODIFIED: + if (data->include_changed) + add_path = delta->new_file.path; + break; + + case GIT_DELTA_DELETED: + if (data->include_changed && + !git_index_find(NULL, index, delta->old_file.path)) + error = git_index_remove(index, delta->old_file.path, 0); + break; + + default: + /* Unimplemented */ + git_error_set( + GIT_ERROR_INVALID, + "cannot update index. Unimplemented status (%d)", + delta->status); + return -1; + } + + if (add_path != NULL) + error = stash_to_index(repo, index, add_path); + } + + return error; +} + +static int build_untracked_tree( + git_tree **tree_out, + git_repository *repo, + git_commit *i_commit, + uint32_t flags) +{ + git_index *i_index = NULL; + git_tree *i_tree = NULL; + git_diff *diff = NULL; + git_diff_options opts = GIT_DIFF_OPTIONS_INIT; + git_index_options index_opts = GIT_INDEX_OPTIONS_FOR_REPO(repo); + struct stash_update_rules data = {0}; + int error; + + if ((error = git_index_new_ext(&i_index, &index_opts)) < 0) + goto cleanup; + + if (flags & GIT_STASH_INCLUDE_UNTRACKED) { + opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | + GIT_DIFF_RECURSE_UNTRACKED_DIRS; + data.include_untracked = true; + } + + if (flags & GIT_STASH_INCLUDE_IGNORED) { + opts.flags |= GIT_DIFF_INCLUDE_IGNORED | + GIT_DIFF_RECURSE_IGNORED_DIRS; + data.include_ignored = true; + } + + if ((error = git_commit_tree(&i_tree, i_commit)) < 0) + goto cleanup; + + if ((error = git_diff_tree_to_workdir(&diff, repo, i_tree, &opts)) < 0) + goto cleanup; + + if ((error = stash_update_index_from_diff(repo, i_index, diff, &data)) < 0) + goto cleanup; + + error = build_tree_from_index(tree_out, repo, i_index); + +cleanup: + git_diff_free(diff); + git_tree_free(i_tree); + git_index_free(i_index); + return error; +} + +static int commit_untracked( + git_commit **u_commit, + git_repository *repo, + const git_signature *stasher, + const char *message, + git_commit *i_commit, + uint32_t flags) +{ + git_tree *u_tree = NULL; + git_oid u_commit_oid; + git_str msg = GIT_STR_INIT; + int error; + + if ((error = build_untracked_tree(&u_tree, repo, i_commit, flags)) < 0) + goto cleanup; + + if ((error = git_str_printf(&msg, "untracked files on %s\n", message)) < 0) + goto cleanup; + + if ((error = git_commit_create( + &u_commit_oid, + repo, + NULL, + stasher, + stasher, + NULL, + git_str_cstr(&msg), + u_tree, + 0, + NULL)) < 0) + goto cleanup; + + error = git_commit_lookup(u_commit, repo, &u_commit_oid); + +cleanup: + git_tree_free(u_tree); + git_str_dispose(&msg); + return error; +} + +static git_diff_delta *stash_delta_merge( + const git_diff_delta *a, + const git_diff_delta *b, + git_pool *pool) +{ + /* Special case for stash: if a file is deleted in the index, but exists + * in the working tree, we need to stash the workdir copy for the workdir. + */ + if (a->status == GIT_DELTA_DELETED && b->status == GIT_DELTA_UNTRACKED) { + git_diff_delta *dup = git_diff__delta_dup(b, pool); + + if (dup) + dup->status = GIT_DELTA_MODIFIED; + return dup; + } + + return git_diff__merge_like_cgit(a, b, pool); +} + +static int build_workdir_tree( + git_tree **tree_out, + git_repository *repo, + git_index *i_index, + git_commit *b_commit) +{ + git_tree *b_tree = NULL; + git_diff *diff = NULL, *idx_to_wd = NULL; + git_diff_options opts = GIT_DIFF_OPTIONS_INIT; + struct stash_update_rules data = {0}; + int error; + + opts.flags = GIT_DIFF_IGNORE_SUBMODULES | GIT_DIFF_INCLUDE_UNTRACKED; + + if ((error = git_commit_tree(&b_tree, b_commit)) < 0) + goto cleanup; + + if ((error = git_diff_tree_to_index(&diff, repo, b_tree, i_index, &opts)) < 0 || + (error = git_diff_index_to_workdir(&idx_to_wd, repo, i_index, &opts)) < 0 || + (error = git_diff__merge(diff, idx_to_wd, stash_delta_merge)) < 0) + goto cleanup; + + data.include_changed = true; + + if ((error = stash_update_index_from_diff(repo, i_index, diff, &data)) < 0) + goto cleanup; + + error = build_tree_from_index(tree_out, repo, i_index); + +cleanup: + git_diff_free(idx_to_wd); + git_diff_free(diff); + git_tree_free(b_tree); + + return error; +} + +static int build_stash_commit_from_tree( + git_oid *w_commit_oid, + git_repository *repo, + const git_signature *stasher, + const char *message, + git_commit *i_commit, + git_commit *b_commit, + git_commit *u_commit, + const git_tree *tree) +{ + const git_commit *parents[] = { NULL, NULL, NULL }; + + parents[0] = b_commit; + parents[1] = i_commit; + parents[2] = u_commit; + + return git_commit_create( + w_commit_oid, + repo, + NULL, + stasher, + stasher, + NULL, + message, + tree, + u_commit ? 3 : 2, + parents); +} + +static int build_stash_commit_from_index( + git_oid *w_commit_oid, + git_repository *repo, + const git_signature *stasher, + const char *message, + git_commit *i_commit, + git_commit *b_commit, + git_commit *u_commit, + git_index *index) +{ + git_tree *tree; + int error; + + if ((error = build_tree_from_index(&tree, repo, index)) < 0) + goto cleanup; + + error = build_stash_commit_from_tree( + w_commit_oid, + repo, + stasher, + message, + i_commit, + b_commit, + u_commit, + tree); + +cleanup: + git_tree_free(tree); + return error; +} + +static int commit_worktree( + git_oid *w_commit_oid, + git_repository *repo, + const git_signature *stasher, + const char *message, + git_commit *i_commit, + git_commit *b_commit, + git_commit *u_commit) +{ + git_index *i_index = NULL, *r_index = NULL; + git_tree *w_tree = NULL; + git_index_options index_opts = GIT_INDEX_OPTIONS_FOR_REPO(repo); + int error = 0, ignorecase; + + if ((error = git_repository_index(&r_index, repo) < 0) || + (error = git_index_new_ext(&i_index, &index_opts)) < 0 || + (error = git_index__fill(i_index, &r_index->entries) < 0) || + (error = git_repository__configmap_lookup(&ignorecase, repo, GIT_CONFIGMAP_IGNORECASE)) < 0) + goto cleanup; + + git_index__set_ignore_case(i_index, ignorecase); + + if ((error = build_workdir_tree(&w_tree, repo, i_index, b_commit)) < 0) + goto cleanup; + + error = build_stash_commit_from_tree( + w_commit_oid, + repo, + stasher, + message, + i_commit, + b_commit, + u_commit, + w_tree + ); + +cleanup: + git_tree_free(w_tree); + git_index_free(i_index); + git_index_free(r_index); + return error; +} + +static int prepare_worktree_commit_message(git_str *out, const char *user_message) +{ + git_str buf = GIT_STR_INIT; + int error = 0; + + if (!user_message) { + git_str_printf(&buf, "WIP on %s", git_str_cstr(out)); + } else { + const char *colon; + + if ((colon = strchr(git_str_cstr(out), ':')) == NULL) + goto cleanup; + + git_str_puts(&buf, "On "); + git_str_put(&buf, git_str_cstr(out), colon - out->ptr); + git_str_printf(&buf, ": %s\n", user_message); + } + + if (git_str_oom(&buf)) { + error = -1; + goto cleanup; + } + + git_str_swap(out, &buf); + +cleanup: + git_str_dispose(&buf); + return error; +} + +static int update_reflog( + git_oid *w_commit_oid, + git_repository *repo, + const char *message) +{ + git_reference *stash; + int error; + + if ((error = git_reference_ensure_log(repo, GIT_REFS_STASH_FILE)) < 0) + return error; + + error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1, message); + + git_reference_free(stash); + + return error; +} + +static int is_dirty_cb(const char *path, unsigned int status, void *payload) +{ + GIT_UNUSED(path); + GIT_UNUSED(status); + GIT_UNUSED(payload); + + return GIT_PASSTHROUGH; +} + +static int ensure_there_are_changes_to_stash(git_repository *repo, uint32_t flags) +{ + int error; + git_status_options opts = GIT_STATUS_OPTIONS_INIT; + + opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; + opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES; + + if (flags & GIT_STASH_INCLUDE_UNTRACKED) + opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED | + GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS; + + if (flags & GIT_STASH_INCLUDE_IGNORED) + opts.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED | + GIT_STATUS_OPT_RECURSE_IGNORED_DIRS; + + error = git_status_foreach_ext(repo, &opts, is_dirty_cb, NULL); + + if (error == GIT_PASSTHROUGH) + return 0; + + if (!error) + return create_error(GIT_ENOTFOUND, "there is nothing to stash."); + + return error; +} + +static int has_changes_cb( + const char *path, + unsigned int status, + void *payload) +{ + GIT_UNUSED(path); + GIT_UNUSED(status); + GIT_UNUSED(payload); + + if (status == GIT_STATUS_CURRENT) + return GIT_ENOTFOUND; + + return 0; +} + +static int ensure_there_are_changes_to_stash_paths( + git_repository *repo, + uint32_t flags, + const git_strarray *paths) +{ + int error; + git_status_options opts = GIT_STATUS_OPTIONS_INIT; + + opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; + opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES | + GIT_STATUS_OPT_INCLUDE_UNMODIFIED | + GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH; + + if (flags & GIT_STASH_INCLUDE_UNTRACKED) + opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED | + GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS; + + if (flags & GIT_STASH_INCLUDE_IGNORED) + opts.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED | + GIT_STATUS_OPT_RECURSE_IGNORED_DIRS; + + git_strarray_copy(&opts.pathspec, paths); + + error = git_status_foreach_ext(repo, &opts, has_changes_cb, NULL); + + git_strarray_dispose(&opts.pathspec); + + if (error == GIT_ENOTFOUND) + return create_error(GIT_ENOTFOUND, "one of the files does not have any changes to stash."); + + return error; +} + +static int reset_index_and_workdir(git_repository *repo, git_commit *commit, uint32_t flags) +{ + git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; + + opts.checkout_strategy = GIT_CHECKOUT_FORCE; + if (flags & GIT_STASH_INCLUDE_UNTRACKED) + opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_UNTRACKED; + if (flags & GIT_STASH_INCLUDE_IGNORED) + opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_IGNORED; + + return git_checkout_tree(repo, (git_object *)commit, &opts); +} + +int git_stash_save( + git_oid *out, + git_repository *repo, + const git_signature *stasher, + const char *message, + uint32_t flags) +{ + git_stash_save_options opts = GIT_STASH_SAVE_OPTIONS_INIT; + + GIT_ASSERT_ARG(stasher); + + opts.stasher = stasher; + opts.message = message; + opts.flags = flags; + + return git_stash_save_with_opts(out, repo, &opts); +} + +int git_stash_save_with_opts( + git_oid *out, + git_repository *repo, + const git_stash_save_options *opts) +{ + git_index *index = NULL, *paths_index = NULL; + git_commit *b_commit = NULL, *i_commit = NULL, *u_commit = NULL; + git_str msg = GIT_STR_INIT; + git_tree *tree = NULL; + git_reference *head = NULL; + git_index_options index_opts = GIT_INDEX_OPTIONS_FOR_REPO(repo); + bool has_paths = false; + + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(opts && opts->stasher); + + has_paths = opts->paths.count > 0; + + if ((error = git_repository__ensure_not_bare(repo, "stash save")) < 0) + return error; + + if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0) + goto cleanup; + + if (!has_paths && + (error = ensure_there_are_changes_to_stash(repo, opts->flags)) < 0) + goto cleanup; + else if (has_paths && + (error = ensure_there_are_changes_to_stash_paths( + repo, opts->flags, &opts->paths)) < 0) + goto cleanup; + + if ((error = git_repository_index(&index, repo)) < 0) + goto cleanup; + + if ((error = commit_index(&i_commit, repo, index, opts->stasher, + git_str_cstr(&msg), b_commit)) < 0) + goto cleanup; + + if ((opts->flags & (GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)) && + (error = commit_untracked(&u_commit, repo, opts->stasher, + git_str_cstr(&msg), i_commit, opts->flags)) < 0) + goto cleanup; + + if ((error = prepare_worktree_commit_message(&msg, opts->message)) < 0) + goto cleanup; + + if (!has_paths) { + if ((error = commit_worktree(out, repo, opts->stasher, git_str_cstr(&msg), + i_commit, b_commit, u_commit)) < 0) + goto cleanup; + } else { + if ((error = git_index_new_ext(&paths_index, &index_opts)) < 0 || + (error = retrieve_head(&head, repo)) < 0 || + (error = git_reference_peel((git_object**)&tree, head, GIT_OBJECT_TREE)) < 0 || + (error = git_index_read_tree(paths_index, tree)) < 0 || + (error = stash_update_index_from_paths(repo, paths_index, &opts->paths)) < 0 || + (error = build_stash_commit_from_index(out, repo, opts->stasher, git_str_cstr(&msg), + i_commit, b_commit, u_commit, paths_index)) < 0) + goto cleanup; + } + + git_str_rtrim(&msg); + + if ((error = update_reflog(out, repo, git_str_cstr(&msg))) < 0) + goto cleanup; + + if (!(opts->flags & GIT_STASH_KEEP_ALL) && + (error = reset_index_and_workdir(repo, + (opts->flags & GIT_STASH_KEEP_INDEX) ? i_commit : b_commit,opts->flags)) < 0) + goto cleanup; + +cleanup: + git_str_dispose(&msg); + git_commit_free(i_commit); + git_commit_free(b_commit); + git_commit_free(u_commit); + git_tree_free(tree); + git_reference_free(head); + git_index_free(index); + git_index_free(paths_index); + + return error; +} + +static int retrieve_stash_commit( + git_commit **commit, + git_repository *repo, + size_t index) +{ + git_reference *stash = NULL; + git_reflog *reflog = NULL; + int error; + size_t max; + const git_reflog_entry *entry; + + if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0) + goto cleanup; + + if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0) + goto cleanup; + + max = git_reflog_entrycount(reflog); + if (!max || index > max - 1) { + error = GIT_ENOTFOUND; + git_error_set(GIT_ERROR_STASH, "no stashed state at position %" PRIuZ, index); + goto cleanup; + } + + entry = git_reflog_entry_byindex(reflog, index); + if ((error = git_commit_lookup(commit, repo, git_reflog_entry_id_new(entry))) < 0) + goto cleanup; + +cleanup: + git_reference_free(stash); + git_reflog_free(reflog); + return error; +} + +static int retrieve_stash_trees( + git_tree **out_stash_tree, + git_tree **out_base_tree, + git_tree **out_index_tree, + git_tree **out_index_parent_tree, + git_tree **out_untracked_tree, + git_commit *stash_commit) +{ + git_tree *stash_tree = NULL; + git_commit *base_commit = NULL; + git_tree *base_tree = NULL; + git_commit *index_commit = NULL; + git_tree *index_tree = NULL; + git_commit *index_parent_commit = NULL; + git_tree *index_parent_tree = NULL; + git_commit *untracked_commit = NULL; + git_tree *untracked_tree = NULL; + int error; + + if ((error = git_commit_tree(&stash_tree, stash_commit)) < 0) + goto cleanup; + + if ((error = git_commit_parent(&base_commit, stash_commit, 0)) < 0) + goto cleanup; + if ((error = git_commit_tree(&base_tree, base_commit)) < 0) + goto cleanup; + + if ((error = git_commit_parent(&index_commit, stash_commit, 1)) < 0) + goto cleanup; + if ((error = git_commit_tree(&index_tree, index_commit)) < 0) + goto cleanup; + + if ((error = git_commit_parent(&index_parent_commit, index_commit, 0)) < 0) + goto cleanup; + if ((error = git_commit_tree(&index_parent_tree, index_parent_commit)) < 0) + goto cleanup; + + if (git_commit_parentcount(stash_commit) == 3) { + if ((error = git_commit_parent(&untracked_commit, stash_commit, 2)) < 0) + goto cleanup; + if ((error = git_commit_tree(&untracked_tree, untracked_commit)) < 0) + goto cleanup; + } + + *out_stash_tree = stash_tree; + *out_base_tree = base_tree; + *out_index_tree = index_tree; + *out_index_parent_tree = index_parent_tree; + *out_untracked_tree = untracked_tree; + +cleanup: + git_commit_free(untracked_commit); + git_commit_free(index_parent_commit); + git_commit_free(index_commit); + git_commit_free(base_commit); + if (error < 0) { + git_tree_free(stash_tree); + git_tree_free(base_tree); + git_tree_free(index_tree); + git_tree_free(index_parent_tree); + git_tree_free(untracked_tree); + } + return error; +} + +static int merge_indexes( + git_index **out, + git_repository *repo, + git_tree *ancestor_tree, + git_index *ours_index, + git_index *theirs_index) +{ + git_iterator *ancestor = NULL, *ours = NULL, *theirs = NULL; + git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; + int error; + + iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + if ((error = git_iterator_for_tree(&ancestor, ancestor_tree, &iter_opts)) < 0 || + (error = git_iterator_for_index(&ours, repo, ours_index, &iter_opts)) < 0 || + (error = git_iterator_for_index(&theirs, repo, theirs_index, &iter_opts)) < 0) + goto done; + + error = git_merge__iterators(out, repo, ancestor, ours, theirs, NULL); + +done: + git_iterator_free(ancestor); + git_iterator_free(ours); + git_iterator_free(theirs); + return error; +} + +static int merge_index_and_tree( + git_index **out, + git_repository *repo, + git_tree *ancestor_tree, + git_index *ours_index, + git_tree *theirs_tree) +{ + git_iterator *ancestor = NULL, *ours = NULL, *theirs = NULL; + git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; + int error; + + iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + if ((error = git_iterator_for_tree(&ancestor, ancestor_tree, &iter_opts)) < 0 || + (error = git_iterator_for_index(&ours, repo, ours_index, &iter_opts)) < 0 || + (error = git_iterator_for_tree(&theirs, theirs_tree, &iter_opts)) < 0) + goto done; + + error = git_merge__iterators(out, repo, ancestor, ours, theirs, NULL); + +done: + git_iterator_free(ancestor); + git_iterator_free(ours); + git_iterator_free(theirs); + return error; +} + +static void normalize_apply_options( + git_stash_apply_options *opts, + const git_stash_apply_options *given_apply_opts) +{ + if (given_apply_opts != NULL) { + memcpy(opts, given_apply_opts, sizeof(git_stash_apply_options)); + } else { + git_stash_apply_options default_apply_opts = GIT_STASH_APPLY_OPTIONS_INIT; + memcpy(opts, &default_apply_opts, sizeof(git_stash_apply_options)); + } + + opts->checkout_options.checkout_strategy |= GIT_CHECKOUT_NO_REFRESH; + + if (!opts->checkout_options.our_label) + opts->checkout_options.our_label = "Updated upstream"; + + if (!opts->checkout_options.their_label) + opts->checkout_options.their_label = "Stashed changes"; +} + +int git_stash_apply_options_init(git_stash_apply_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_stash_apply_options, GIT_STASH_APPLY_OPTIONS_INIT); + return 0; +} + +int git_stash_save_options_init(git_stash_save_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_stash_save_options, GIT_STASH_SAVE_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_stash_apply_init_options(git_stash_apply_options *opts, unsigned int version) +{ + return git_stash_apply_options_init(opts, version); +} +#endif + +#define NOTIFY_PROGRESS(opts, progress_type) \ + do { \ + if ((opts).progress_cb && \ + (error = (opts).progress_cb((progress_type), (opts).progress_payload))) { \ + error = (error < 0) ? error : -1; \ + goto cleanup; \ + } \ + } while(false); + +static int ensure_clean_index(git_repository *repo, git_index *index) +{ + git_tree *head_tree = NULL; + git_diff *index_diff = NULL; + int error = 0; + + if ((error = git_repository_head_tree(&head_tree, repo)) < 0 || + (error = git_diff_tree_to_index( + &index_diff, repo, head_tree, index, NULL)) < 0) + goto done; + + if (git_diff_num_deltas(index_diff) > 0) { + git_error_set(GIT_ERROR_STASH, "%" PRIuZ " uncommitted changes exist in the index", + git_diff_num_deltas(index_diff)); + error = GIT_EUNCOMMITTED; + } + +done: + git_diff_free(index_diff); + git_tree_free(head_tree); + return error; +} + +static int stage_new_file(const git_index_entry **entries, void *data) +{ + git_index *index = data; + + if(entries[0] == NULL) + return git_index_add(index, entries[1]); + else + return git_index_add(index, entries[0]); +} + +static int stage_new_files( + git_index **out, + git_repository *repo, + git_tree *parent_tree, + git_tree *tree) +{ + git_iterator *iterators[2] = { NULL, NULL }; + git_iterator_options iterator_options = GIT_ITERATOR_OPTIONS_INIT; + git_index *index = NULL; + git_index_options index_opts = GIT_INDEX_OPTIONS_FOR_REPO(repo); + int error; + + if ((error = git_index_new_ext(&index, &index_opts)) < 0 || + (error = git_iterator_for_tree( + &iterators[0], parent_tree, &iterator_options)) < 0 || + (error = git_iterator_for_tree( + &iterators[1], tree, &iterator_options)) < 0) + goto done; + + error = git_iterator_walk(iterators, 2, stage_new_file, index); + +done: + if (error < 0) + git_index_free(index); + else + *out = index; + + git_iterator_free(iterators[0]); + git_iterator_free(iterators[1]); + + return error; +} + +int git_stash_apply( + git_repository *repo, + size_t index, + const git_stash_apply_options *given_opts) +{ + git_stash_apply_options opts; + unsigned int checkout_strategy; + git_commit *stash_commit = NULL; + git_tree *stash_tree = NULL; + git_tree *stash_parent_tree = NULL; + git_tree *index_tree = NULL; + git_tree *index_parent_tree = NULL; + git_tree *untracked_tree = NULL; + git_index *stash_adds = NULL; + git_index *repo_index = NULL; + git_index *unstashed_index = NULL; + git_index *modified_index = NULL; + git_index *untracked_index = NULL; + int error; + + GIT_ERROR_CHECK_VERSION(given_opts, GIT_STASH_APPLY_OPTIONS_VERSION, "git_stash_apply_options"); + + normalize_apply_options(&opts, given_opts); + checkout_strategy = opts.checkout_options.checkout_strategy; + + NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_LOADING_STASH); + + /* Retrieve commit corresponding to the given stash */ + if ((error = retrieve_stash_commit(&stash_commit, repo, index)) < 0) + goto cleanup; + + /* Retrieve all trees in the stash */ + if ((error = retrieve_stash_trees( + &stash_tree, &stash_parent_tree, &index_tree, + &index_parent_tree, &untracked_tree, stash_commit)) < 0) + goto cleanup; + + /* Load repo index */ + if ((error = git_repository_index(&repo_index, repo)) < 0) + goto cleanup; + + NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX); + + if ((error = ensure_clean_index(repo, repo_index)) < 0) + goto cleanup; + + /* Restore index if required */ + if ((opts.flags & GIT_STASH_APPLY_REINSTATE_INDEX) && + git_oid_cmp(git_tree_id(stash_parent_tree), git_tree_id(index_tree))) { + + if ((error = merge_index_and_tree( + &unstashed_index, repo, index_parent_tree, repo_index, index_tree)) < 0) + goto cleanup; + + if (git_index_has_conflicts(unstashed_index)) { + error = GIT_ECONFLICT; + goto cleanup; + } + + /* Otherwise, stage any new files in the stash tree. (Note: their + * previously unstaged contents are staged, not the previously staged.) + */ + } else if ((opts.flags & GIT_STASH_APPLY_REINSTATE_INDEX) == 0) { + if ((error = stage_new_files(&stash_adds, repo, + stash_parent_tree, stash_tree)) < 0 || + (error = merge_indexes(&unstashed_index, repo, + stash_parent_tree, repo_index, stash_adds)) < 0) + goto cleanup; + } + + NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED); + + /* Restore modified files in workdir */ + if ((error = merge_index_and_tree( + &modified_index, repo, stash_parent_tree, repo_index, stash_tree)) < 0) + goto cleanup; + + /* If applicable, restore untracked / ignored files in workdir */ + if (untracked_tree) { + NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED); + + if ((error = merge_index_and_tree(&untracked_index, repo, NULL, repo_index, untracked_tree)) < 0) + goto cleanup; + } + + if (untracked_index) { + opts.checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX; + + NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED); + + if ((error = git_checkout_index(repo, untracked_index, &opts.checkout_options)) < 0) + goto cleanup; + + opts.checkout_options.checkout_strategy = checkout_strategy; + } + + + /* If there are conflicts in the modified index, then we need to actually + * check that out as the repo's index. Otherwise, we don't update the + * index. + */ + + if (!git_index_has_conflicts(modified_index)) + opts.checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX; + + /* Check out the modified index using the existing repo index as baseline, + * so that existing modifications in the index can be rewritten even when + * checking out safely. + */ + opts.checkout_options.baseline_index = repo_index; + + NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED); + + if ((error = git_checkout_index(repo, modified_index, &opts.checkout_options)) < 0) + goto cleanup; + + if (unstashed_index && !git_index_has_conflicts(modified_index)) { + if ((error = git_index_read_index(repo_index, unstashed_index)) < 0) + goto cleanup; + } + + NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_DONE); + + error = git_index_write(repo_index); + +cleanup: + git_index_free(untracked_index); + git_index_free(modified_index); + git_index_free(unstashed_index); + git_index_free(stash_adds); + git_index_free(repo_index); + git_tree_free(untracked_tree); + git_tree_free(index_parent_tree); + git_tree_free(index_tree); + git_tree_free(stash_parent_tree); + git_tree_free(stash_tree); + git_commit_free(stash_commit); + return error; +} + +int git_stash_foreach( + git_repository *repo, + git_stash_cb callback, + void *payload) +{ + git_reference *stash; + git_reflog *reflog = NULL; + int error; + size_t i, max; + const git_reflog_entry *entry; + + error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE); + if (error == GIT_ENOTFOUND) { + git_error_clear(); + return 0; + } + if (error < 0) + goto cleanup; + + if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0) + goto cleanup; + + max = git_reflog_entrycount(reflog); + for (i = 0; i < max; i++) { + entry = git_reflog_entry_byindex(reflog, i); + + error = callback(i, + git_reflog_entry_message(entry), + git_reflog_entry_id_new(entry), + payload); + + if (error) { + git_error_set_after_callback(error); + break; + } + } + +cleanup: + git_reference_free(stash); + git_reflog_free(reflog); + return error; +} + +int git_stash_drop( + git_repository *repo, + size_t index) +{ + git_transaction *tx; + git_reference *stash = NULL; + git_reflog *reflog = NULL; + size_t max; + int error; + + if ((error = git_transaction_new(&tx, repo)) < 0) + return error; + + if ((error = git_transaction_lock_ref(tx, GIT_REFS_STASH_FILE)) < 0) + goto cleanup; + + if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0) + goto cleanup; + + if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0) + goto cleanup; + + max = git_reflog_entrycount(reflog); + + if (!max || index > max - 1) { + error = GIT_ENOTFOUND; + git_error_set(GIT_ERROR_STASH, "no stashed state at position %" PRIuZ, index); + goto cleanup; + } + + if ((error = git_reflog_drop(reflog, index, true)) < 0) + goto cleanup; + + if ((error = git_transaction_set_reflog(tx, GIT_REFS_STASH_FILE, reflog)) < 0) + goto cleanup; + + if (max == 1) { + if ((error = git_transaction_remove(tx, GIT_REFS_STASH_FILE)) < 0) + goto cleanup; + } else if (index == 0) { + const git_reflog_entry *entry; + + entry = git_reflog_entry_byindex(reflog, 0); + if ((error = git_transaction_set_target(tx, GIT_REFS_STASH_FILE, &entry->oid_cur, NULL, NULL)) < 0) + goto cleanup; + } + + error = git_transaction_commit(tx); + +cleanup: + git_reference_free(stash); + git_transaction_free(tx); + git_reflog_free(reflog); + return error; +} + +int git_stash_pop( + git_repository *repo, + size_t index, + const git_stash_apply_options *options) +{ + int error; + + if ((error = git_stash_apply(repo, index, options)) < 0) + return error; + + return git_stash_drop(repo, index); +} diff --git a/src/libgit2/status.c b/src/libgit2/status.c new file mode 100644 index 00000000000..06356ad8b8b --- /dev/null +++ b/src/libgit2/status.c @@ -0,0 +1,584 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "status.h" + +#include "git2.h" +#include "futils.h" +#include "hash.h" +#include "vector.h" +#include "tree.h" +#include "git2/status.h" +#include "repository.h" +#include "ignore.h" +#include "index.h" +#include "wildmatch.h" + +#include "git2/diff.h" +#include "diff.h" +#include "diff_generate.h" + +static unsigned int index_delta2status(const git_diff_delta *head2idx) +{ + git_status_t st = GIT_STATUS_CURRENT; + + switch (head2idx->status) { + case GIT_DELTA_ADDED: + case GIT_DELTA_COPIED: + st = GIT_STATUS_INDEX_NEW; + break; + case GIT_DELTA_DELETED: + st = GIT_STATUS_INDEX_DELETED; + break; + case GIT_DELTA_MODIFIED: + st = GIT_STATUS_INDEX_MODIFIED; + break; + case GIT_DELTA_RENAMED: + st = GIT_STATUS_INDEX_RENAMED; + + if (!git_oid_equal(&head2idx->old_file.id, &head2idx->new_file.id)) + st |= GIT_STATUS_INDEX_MODIFIED; + break; + case GIT_DELTA_TYPECHANGE: + st = GIT_STATUS_INDEX_TYPECHANGE; + break; + case GIT_DELTA_CONFLICTED: + st = GIT_STATUS_CONFLICTED; + break; + default: + break; + } + + return st; +} + +static unsigned int workdir_delta2status( + git_diff *diff, git_diff_delta *idx2wd) +{ + git_status_t st = GIT_STATUS_CURRENT; + + switch (idx2wd->status) { + case GIT_DELTA_ADDED: + case GIT_DELTA_COPIED: + case GIT_DELTA_UNTRACKED: + st = GIT_STATUS_WT_NEW; + break; + case GIT_DELTA_UNREADABLE: + st = GIT_STATUS_WT_UNREADABLE; + break; + case GIT_DELTA_DELETED: + st = GIT_STATUS_WT_DELETED; + break; + case GIT_DELTA_MODIFIED: + st = GIT_STATUS_WT_MODIFIED; + break; + case GIT_DELTA_IGNORED: + st = GIT_STATUS_IGNORED; + break; + case GIT_DELTA_RENAMED: + st = GIT_STATUS_WT_RENAMED; + + if (!git_oid_equal(&idx2wd->old_file.id, &idx2wd->new_file.id)) { + /* if OIDs don't match, we might need to calculate them now to + * discern between RENAMED vs RENAMED+MODIFIED + */ + if (git_oid_is_zero(&idx2wd->old_file.id) && + diff->old_src == GIT_ITERATOR_WORKDIR && + !git_diff__oid_for_file( + &idx2wd->old_file.id, diff, idx2wd->old_file.path, + idx2wd->old_file.mode, idx2wd->old_file.size)) + idx2wd->old_file.flags |= GIT_DIFF_FLAG_VALID_ID; + + if (git_oid_is_zero(&idx2wd->new_file.id) && + diff->new_src == GIT_ITERATOR_WORKDIR && + !git_diff__oid_for_file( + &idx2wd->new_file.id, diff, idx2wd->new_file.path, + idx2wd->new_file.mode, idx2wd->new_file.size)) + idx2wd->new_file.flags |= GIT_DIFF_FLAG_VALID_ID; + + if (!git_oid_equal(&idx2wd->old_file.id, &idx2wd->new_file.id)) + st |= GIT_STATUS_WT_MODIFIED; + } + break; + case GIT_DELTA_TYPECHANGE: + st = GIT_STATUS_WT_TYPECHANGE; + break; + case GIT_DELTA_CONFLICTED: + st = GIT_STATUS_CONFLICTED; + break; + default: + break; + } + + return st; +} + +static bool status_is_included( + git_status_list *status, + git_diff_delta *head2idx, + git_diff_delta *idx2wd) +{ + if (!(status->opts.flags & GIT_STATUS_OPT_EXCLUDE_SUBMODULES)) + return 1; + + /* if excluding submodules and this is a submodule everywhere */ + if (head2idx) { + if (head2idx->status != GIT_DELTA_ADDED && + head2idx->old_file.mode != GIT_FILEMODE_COMMIT) + return 1; + if (head2idx->status != GIT_DELTA_DELETED && + head2idx->new_file.mode != GIT_FILEMODE_COMMIT) + return 1; + } + if (idx2wd) { + if (idx2wd->status != GIT_DELTA_ADDED && + idx2wd->old_file.mode != GIT_FILEMODE_COMMIT) + return 1; + if (idx2wd->status != GIT_DELTA_DELETED && + idx2wd->new_file.mode != GIT_FILEMODE_COMMIT) + return 1; + } + + /* only get here if every valid mode is GIT_FILEMODE_COMMIT */ + return 0; +} + +static git_status_t status_compute( + git_status_list *status, + git_diff_delta *head2idx, + git_diff_delta *idx2wd) +{ + git_status_t st = GIT_STATUS_CURRENT; + + if (head2idx) + st |= index_delta2status(head2idx); + + if (idx2wd) + st |= workdir_delta2status(status->idx2wd, idx2wd); + + return st; +} + +static int status_collect( + git_diff_delta *head2idx, + git_diff_delta *idx2wd, + void *payload) +{ + git_status_list *status = payload; + git_status_entry *status_entry; + + if (!status_is_included(status, head2idx, idx2wd)) + return 0; + + status_entry = git__malloc(sizeof(git_status_entry)); + GIT_ERROR_CHECK_ALLOC(status_entry); + + status_entry->status = status_compute(status, head2idx, idx2wd); + status_entry->head_to_index = head2idx; + status_entry->index_to_workdir = idx2wd; + + return git_vector_insert(&status->paired, status_entry); +} + +GIT_INLINE(int) status_entry_cmp_base( + const void *a, + const void *b, + int (*strcomp)(const char *a, const char *b)) +{ + const git_status_entry *entry_a = a; + const git_status_entry *entry_b = b; + const git_diff_delta *delta_a, *delta_b; + + delta_a = entry_a->index_to_workdir ? entry_a->index_to_workdir : + entry_a->head_to_index; + delta_b = entry_b->index_to_workdir ? entry_b->index_to_workdir : + entry_b->head_to_index; + + if (!delta_a && delta_b) + return -1; + if (delta_a && !delta_b) + return 1; + if (!delta_a && !delta_b) + return 0; + + return strcomp(delta_a->new_file.path, delta_b->new_file.path); +} + +static int status_entry_icmp(const void *a, const void *b) +{ + return status_entry_cmp_base(a, b, git__strcasecmp); +} + +static int status_entry_cmp(const void *a, const void *b) +{ + return status_entry_cmp_base(a, b, git__strcmp); +} + +static git_status_list *git_status_list_alloc(git_index *index) +{ + git_status_list *status = NULL; + int (*entrycmp)(const void *a, const void *b); + + if (!(status = git__calloc(1, sizeof(git_status_list)))) + return NULL; + + entrycmp = index->ignore_case ? status_entry_icmp : status_entry_cmp; + + if (git_vector_init(&status->paired, 0, entrycmp) < 0) { + git__free(status); + return NULL; + } + + return status; +} + +static int status_validate_options(const git_status_options *opts) +{ + if (!opts) + return 0; + + GIT_ERROR_CHECK_VERSION(opts, GIT_STATUS_OPTIONS_VERSION, "git_status_options"); + + if (opts->show > GIT_STATUS_SHOW_WORKDIR_ONLY) { + git_error_set(GIT_ERROR_INVALID, "unknown status 'show' option"); + return -1; + } + + if ((opts->flags & GIT_STATUS_OPT_NO_REFRESH) != 0 && + (opts->flags & GIT_STATUS_OPT_UPDATE_INDEX) != 0) { + git_error_set(GIT_ERROR_INVALID, "updating index from status " + "is not allowed when index refresh is disabled"); + return -1; + } + + return 0; +} + +int git_status_list_new( + git_status_list **out, + git_repository *repo, + const git_status_options *opts) +{ + git_index *index = NULL; + git_status_list *status = NULL; + git_diff_options diffopt = GIT_DIFF_OPTIONS_INIT; + git_diff_find_options findopt = GIT_DIFF_FIND_OPTIONS_INIT; + git_tree *head = NULL; + git_status_show_t show = + opts ? opts->show : GIT_STATUS_SHOW_INDEX_AND_WORKDIR; + int error = 0; + unsigned int flags = opts ? opts->flags : GIT_STATUS_OPT_DEFAULTS; + + *out = NULL; + + if (status_validate_options(opts) < 0) + return -1; + + if ((error = git_repository__ensure_not_bare(repo, "status")) < 0 || + (error = git_repository_index(&index, repo)) < 0) + return error; + + if (opts != NULL && opts->baseline != NULL) { + head = opts->baseline; + } else { + /* if there is no HEAD, that's okay - we'll make an empty iterator */ + if ((error = git_repository_head_tree(&head, repo)) < 0) { + if (error != GIT_ENOTFOUND && error != GIT_EUNBORNBRANCH) + goto done; + git_error_clear(); + } + } + + /* refresh index from disk unless prevented */ + if ((flags & GIT_STATUS_OPT_NO_REFRESH) == 0 && + git_index_read_safely(index) < 0) + git_error_clear(); + + status = git_status_list_alloc(index); + GIT_ERROR_CHECK_ALLOC(status); + + if (opts) { + memcpy(&status->opts, opts, sizeof(git_status_options)); + memcpy(&diffopt.pathspec, &opts->pathspec, sizeof(diffopt.pathspec)); + } + + diffopt.flags = GIT_DIFF_INCLUDE_TYPECHANGE; + findopt.flags = GIT_DIFF_FIND_FOR_UNTRACKED; + + if ((flags & GIT_STATUS_OPT_INCLUDE_UNTRACKED) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNTRACKED; + if ((flags & GIT_STATUS_OPT_INCLUDE_IGNORED) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_IGNORED; + if ((flags & GIT_STATUS_OPT_INCLUDE_UNMODIFIED) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNMODIFIED; + if ((flags & GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_RECURSE_UNTRACKED_DIRS; + if ((flags & GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_DISABLE_PATHSPEC_MATCH; + if ((flags & GIT_STATUS_OPT_RECURSE_IGNORED_DIRS) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_RECURSE_IGNORED_DIRS; + if ((flags & GIT_STATUS_OPT_EXCLUDE_SUBMODULES) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_IGNORE_SUBMODULES; + if ((flags & GIT_STATUS_OPT_UPDATE_INDEX) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_UPDATE_INDEX; + if ((flags & GIT_STATUS_OPT_INCLUDE_UNREADABLE) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNREADABLE; + if ((flags & GIT_STATUS_OPT_INCLUDE_UNREADABLE_AS_UNTRACKED) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED; + + if ((flags & GIT_STATUS_OPT_RENAMES_FROM_REWRITES) != 0) + findopt.flags = findopt.flags | + GIT_DIFF_FIND_AND_BREAK_REWRITES | + GIT_DIFF_FIND_RENAMES_FROM_REWRITES | + GIT_DIFF_BREAK_REWRITES_FOR_RENAMES_ONLY; + + if (opts != NULL && opts->rename_threshold != 0) + findopt.rename_threshold = opts->rename_threshold; + + if (show != GIT_STATUS_SHOW_WORKDIR_ONLY) { + if ((error = git_diff_tree_to_index( + &status->head2idx, repo, head, index, &diffopt)) < 0) + goto done; + + if ((flags & GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX) != 0 && + (error = git_diff_find_similar(status->head2idx, &findopt)) < 0) + goto done; + } + + if (show != GIT_STATUS_SHOW_INDEX_ONLY) { + if ((error = git_diff_index_to_workdir( + &status->idx2wd, repo, index, &diffopt)) < 0) { + goto done; + } + + if ((flags & GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR) != 0 && + (error = git_diff_find_similar(status->idx2wd, &findopt)) < 0) + goto done; + } + + error = git_diff__paired_foreach( + status->head2idx, status->idx2wd, status_collect, status); + if (error < 0) + goto done; + + if (flags & GIT_STATUS_OPT_SORT_CASE_SENSITIVELY) + git_vector_set_cmp(&status->paired, status_entry_cmp); + if (flags & GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY) + git_vector_set_cmp(&status->paired, status_entry_icmp); + + if ((flags & + (GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX | + GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR | + GIT_STATUS_OPT_SORT_CASE_SENSITIVELY | + GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY)) != 0) + git_vector_sort(&status->paired); + +done: + if (error < 0) { + git_status_list_free(status); + status = NULL; + } + + *out = status; + + if (opts == NULL || opts->baseline != head) + git_tree_free(head); + git_index_free(index); + + return error; +} + +size_t git_status_list_entrycount(git_status_list *status) +{ + GIT_ASSERT_ARG_WITH_RETVAL(status, 0); + + return status->paired.length; +} + +const git_status_entry *git_status_byindex(git_status_list *status, size_t i) +{ + GIT_ASSERT_ARG_WITH_RETVAL(status, NULL); + + return git_vector_get(&status->paired, i); +} + +void git_status_list_free(git_status_list *status) +{ + if (status == NULL) + return; + + git_diff_free(status->head2idx); + git_diff_free(status->idx2wd); + + git_vector_dispose_deep(&status->paired); + + git__memzero(status, sizeof(*status)); + git__free(status); +} + +int git_status_foreach_ext( + git_repository *repo, + const git_status_options *opts, + git_status_cb cb, + void *payload) +{ + git_status_list *status; + const git_status_entry *status_entry; + size_t i; + int error = 0; + + if ((error = git_status_list_new(&status, repo, opts)) < 0) { + return error; + } + + git_vector_foreach(&status->paired, i, status_entry) { + const char *path = status_entry->head_to_index ? + status_entry->head_to_index->old_file.path : + status_entry->index_to_workdir->old_file.path; + + if ((error = cb(path, status_entry->status, payload)) != 0) { + git_error_set_after_callback(error); + break; + } + } + + git_status_list_free(status); + + return error; +} + +int git_status_foreach(git_repository *repo, git_status_cb cb, void *payload) +{ + return git_status_foreach_ext(repo, NULL, cb, payload); +} + +struct status_file_info { + char *expected; + unsigned int count; + unsigned int status; + int wildmatch_flags; + int ambiguous; +}; + +static int get_one_status(const char *path, unsigned int status, void *data) +{ + struct status_file_info *sfi = data; + int (*strcomp)(const char *a, const char *b); + + sfi->count++; + sfi->status = status; + + strcomp = (sfi->wildmatch_flags & WM_CASEFOLD) ? git__strcasecmp : git__strcmp; + + if (sfi->count > 1 || + (strcomp(sfi->expected, path) != 0 && + wildmatch(sfi->expected, path, sfi->wildmatch_flags) != 0)) + { + sfi->ambiguous = true; + return GIT_EAMBIGUOUS; /* git_error_set will be done by caller */ + } + + return 0; +} + +int git_status_file( + unsigned int *status_flags, + git_repository *repo, + const char *path) +{ + int error; + git_status_options opts = GIT_STATUS_OPTIONS_INIT; + struct status_file_info sfi = {0}; + git_index *index; + + GIT_ASSERT_ARG(status_flags); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(path); + + if ((error = git_repository_index__weakptr(&index, repo)) < 0) + return error; + + if ((sfi.expected = git__strdup(path)) == NULL) + return -1; + if (index->ignore_case) + sfi.wildmatch_flags = WM_CASEFOLD; + + opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; + opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED | + GIT_STATUS_OPT_RECURSE_IGNORED_DIRS | + GIT_STATUS_OPT_INCLUDE_UNTRACKED | + GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS | + GIT_STATUS_OPT_INCLUDE_UNMODIFIED | + GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH; + opts.pathspec.count = 1; + opts.pathspec.strings = &sfi.expected; + + error = git_status_foreach_ext(repo, &opts, get_one_status, &sfi); + + if (error < 0 && sfi.ambiguous) { + git_error_set(GIT_ERROR_INVALID, + "ambiguous path '%s' given to git_status_file", sfi.expected); + error = GIT_EAMBIGUOUS; + } + + if (!error && !sfi.count) { + git_error_set(GIT_ERROR_INVALID, + "attempt to get status of nonexistent file '%s'", path); + error = GIT_ENOTFOUND; + } + + *status_flags = sfi.status; + + git__free(sfi.expected); + + return error; +} + +int git_status_should_ignore( + int *ignored, + git_repository *repo, + const char *path) +{ + return git_ignore_path_is_ignored(ignored, repo, path); +} + +int git_status_options_init(git_status_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_status_options, GIT_STATUS_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_status_init_options(git_status_options *opts, unsigned int version) +{ + return git_status_options_init(opts, version); +} +#endif + +int git_status_list_get_perfdata( + git_diff_perfdata *out, const git_status_list *status) +{ + GIT_ASSERT_ARG(out); + + GIT_ERROR_CHECK_VERSION(out, GIT_DIFF_PERFDATA_VERSION, "git_diff_perfdata"); + + out->stat_calls = 0; + out->oid_calculations = 0; + + if (status->head2idx) { + out->stat_calls += status->head2idx->perf.stat_calls; + out->oid_calculations += status->head2idx->perf.oid_calculations; + } + if (status->idx2wd) { + out->stat_calls += status->idx2wd->perf.stat_calls; + out->oid_calculations += status->idx2wd->perf.oid_calculations; + } + + return 0; +} + diff --git a/src/libgit2/status.h b/src/libgit2/status.h new file mode 100644 index 00000000000..907479a2222 --- /dev/null +++ b/src/libgit2/status.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_status_h__ +#define INCLUDE_status_h__ + +#include "common.h" + +#include "diff.h" +#include "git2/status.h" +#include "git2/diff.h" + +struct git_status_list { + git_status_options opts; + + git_diff *head2idx; + git_diff *idx2wd; + + git_vector paired; +}; + +#endif diff --git a/src/libgit2/strarray.c b/src/libgit2/strarray.c new file mode 100644 index 00000000000..25e75f02ad9 --- /dev/null +++ b/src/libgit2/strarray.c @@ -0,0 +1,65 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "util.h" + +#include "common.h" +#include "strarray.h" + +int git_strarray_copy(git_strarray *tgt, const git_strarray *src) +{ + size_t i; + + GIT_ASSERT_ARG(tgt); + GIT_ASSERT_ARG(src); + + memset(tgt, 0, sizeof(*tgt)); + + if (!src->count) + return 0; + + tgt->strings = git__calloc(src->count, sizeof(char *)); + GIT_ERROR_CHECK_ALLOC(tgt->strings); + + for (i = 0; i < src->count; ++i) { + if (!src->strings[i]) + continue; + + tgt->strings[tgt->count] = git__strdup(src->strings[i]); + if (!tgt->strings[tgt->count]) { + git_strarray_dispose(tgt); + memset(tgt, 0, sizeof(*tgt)); + return -1; + } + + tgt->count++; + } + + return 0; +} + +void git_strarray_dispose(git_strarray *array) +{ + size_t i; + + if (array == NULL) + return; + + for (i = 0; i < array->count; ++i) + git__free(array->strings[i]); + + git__free(array->strings); + + memset(array, 0, sizeof(*array)); +} + +#ifndef GIT_DEPRECATE_HARD +void git_strarray_free(git_strarray *array) +{ + git_strarray_dispose(array); +} +#endif diff --git a/src/libgit2/strarray.h b/src/libgit2/strarray.h new file mode 100644 index 00000000000..1984805357a --- /dev/null +++ b/src/libgit2/strarray.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_strarray_h__ +#define INCLUDE_strarray_h__ + +#include "common.h" +#include "git2/strarray.h" + +/** + * Copy a string array object from source to target. + * + * Note: target is overwritten and hence should be empty, otherwise its + * contents are leaked. Call git_strarray_free() if necessary. + * + * @param tgt target + * @param src source + * @return 0 on success, < 0 on allocation failure + */ +extern int git_strarray_copy(git_strarray *tgt, const git_strarray *src); + +#endif diff --git a/src/libgit2/stream.h b/src/libgit2/stream.h new file mode 100644 index 00000000000..f16b026fbe8 --- /dev/null +++ b/src/libgit2/stream.h @@ -0,0 +1,86 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_stream_h__ +#define INCLUDE_stream_h__ + +#include "common.h" +#include "git2/sys/stream.h" + +GIT_INLINE(int) git_stream_connect(git_stream *st) +{ + return st->connect(st); +} + +GIT_INLINE(int) git_stream_is_encrypted(git_stream *st) +{ + return st->encrypted; +} + +GIT_INLINE(int) git_stream_certificate(git_cert **out, git_stream *st) +{ + if (!st->encrypted) { + git_error_set(GIT_ERROR_INVALID, "an unencrypted stream does not have a certificate"); + return -1; + } + + return st->certificate(out, st); +} + +GIT_INLINE(int) git_stream_supports_proxy(git_stream *st) +{ + return st->proxy_support; +} + +GIT_INLINE(int) git_stream_set_proxy(git_stream *st, const git_proxy_options *proxy_opts) +{ + if (!st->proxy_support) { + git_error_set(GIT_ERROR_INVALID, "proxy not supported on this stream"); + return -1; + } + + return st->set_proxy(st, proxy_opts); +} + +GIT_INLINE(ssize_t) git_stream_read(git_stream *st, void *data, size_t len) +{ + return st->read(st, data, len); +} + +GIT_INLINE(ssize_t) git_stream_write(git_stream *st, const char *data, size_t len, int flags) +{ + return st->write(st, data, len, flags); +} + +GIT_INLINE(int) git_stream__write_full(git_stream *st, const char *data, size_t len, int flags) +{ + size_t total_written = 0; + + while (total_written < len) { + ssize_t written = git_stream_write(st, data + total_written, len - total_written, flags); + if (written <= 0) + return -1; + + total_written += written; + } + + return 0; +} + +GIT_INLINE(int) git_stream_close(git_stream *st) +{ + return st->close(st); +} + +GIT_INLINE(void) git_stream_free(git_stream *st) +{ + if (!st) + return; + + st->free(st); +} + +#endif diff --git a/src/libgit2/streams/mbedtls.c b/src/libgit2/streams/mbedtls.c new file mode 100644 index 00000000000..ccf0f110303 --- /dev/null +++ b/src/libgit2/streams/mbedtls.c @@ -0,0 +1,479 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "streams/mbedtls.h" + +#ifdef GIT_HTTPS_MBEDTLS + +#include + +#include "runtime.h" +#include "stream.h" +#include "streams/socket.h" +#include "git2/transport.h" +#include "util.h" + +#ifndef GIT_DEFAULT_CERT_LOCATION +#define GIT_DEFAULT_CERT_LOCATION NULL +#endif + +/* Work around C90-conformance issues */ +#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) +# if defined(_MSC_VER) +# define inline __inline +# elif defined(__GNUC__) +# define inline __inline__ +# else +# define inline +# endif +#endif + +#include +#include +#include +#include + +#undef inline + +#define GIT_SSL_DEFAULT_CIPHERS "TLS1-3-AES-128-GCM-SHA256:TLS1-3-AES-256-GCM-SHA384:TLS1-3-CHACHA20-POLY1305-SHA256:TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256:TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256:TLS-ECDHE-ECDSA-WITH-AES-256-GCM-SHA384:TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384:TLS-ECDHE-ECDSA-WITH-CHACHA20-POLY1305-SHA256:TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-CHACHA20-POLY1305-SHA256" +#define GIT_SSL_DEFAULT_CIPHERS_COUNT 12 + +static int ciphers_list[GIT_SSL_DEFAULT_CIPHERS_COUNT]; + +static bool initialized = false; +static mbedtls_ssl_config mbedtls_config; +static mbedtls_ctr_drbg_context mbedtls_rng; +static mbedtls_entropy_context mbedtls_entropy; + +static bool has_ca_chain = false; +static mbedtls_x509_crt mbedtls_ca_chain; + +/** + * This function aims to clean-up the SSL context which + * we allocated. + */ +static void shutdown_ssl(void) +{ + if (has_ca_chain) { + mbedtls_x509_crt_free(&mbedtls_ca_chain); + has_ca_chain = false; + } + + if (initialized) { + mbedtls_ctr_drbg_free(&mbedtls_rng); + mbedtls_ssl_config_free(&mbedtls_config); + mbedtls_entropy_free(&mbedtls_entropy); + initialized = false; + } +} + +int git_mbedtls_stream_global_init(void) +{ + int loaded = 0; + char *crtpath = GIT_DEFAULT_CERT_LOCATION; + struct stat statbuf; + + size_t ciphers_known = 0; + char *cipher_name = NULL; + char *cipher_string = NULL; + char *cipher_string_tmp = NULL; + + mbedtls_ssl_config_init(&mbedtls_config); + mbedtls_entropy_init(&mbedtls_entropy); + mbedtls_ctr_drbg_init(&mbedtls_rng); + + if (mbedtls_ssl_config_defaults(&mbedtls_config, + MBEDTLS_SSL_IS_CLIENT, + MBEDTLS_SSL_TRANSPORT_STREAM, + MBEDTLS_SSL_PRESET_DEFAULT) != 0) { + git_error_set(GIT_ERROR_SSL, "failed to initialize mbedTLS"); + goto cleanup; + } + + /* + * Configure TLSv1.2 or better; if the minor version constant isn't + * defined then this version of mbedTLS doesn't support such an old + * version, so we need not do anything. + */ +#ifdef MBEDTLS_SSL_MINOR_VERSION_3 + mbedtls_ssl_conf_min_version(&mbedtls_config, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3); +#endif + + /* verify_server_cert is responsible for making the check. + * OPTIONAL because REQUIRED drops the certificate as soon as the check + * is made, so we can never see the certificate and override it. */ + mbedtls_ssl_conf_authmode(&mbedtls_config, MBEDTLS_SSL_VERIFY_OPTIONAL); + + /* set the list of allowed ciphersuites */ + ciphers_known = 0; + cipher_string = cipher_string_tmp = git__strdup(GIT_SSL_DEFAULT_CIPHERS); + GIT_ERROR_CHECK_ALLOC(cipher_string); + + while ((cipher_name = git__strtok(&cipher_string_tmp, ":")) != NULL) { + int cipherid = mbedtls_ssl_get_ciphersuite_id(cipher_name); + if (cipherid == 0) continue; + + if (ciphers_known >= ARRAY_SIZE(ciphers_list)) { + git_error_set(GIT_ERROR_SSL, "out of cipher list space"); + goto cleanup; + } + + ciphers_list[ciphers_known++] = cipherid; + } + git__free(cipher_string); + + if (!ciphers_known) { + git_error_set(GIT_ERROR_SSL, "no cipher could be enabled"); + goto cleanup; + } + mbedtls_ssl_conf_ciphersuites(&mbedtls_config, ciphers_list); + + /* Seeding the random number generator */ + + if (mbedtls_ctr_drbg_seed(&mbedtls_rng, mbedtls_entropy_func, + &mbedtls_entropy, NULL, 0) != 0) { + git_error_set(GIT_ERROR_SSL, "failed to initialize mbedTLS entropy pool"); + goto cleanup; + } + + mbedtls_ssl_conf_rng(&mbedtls_config, mbedtls_ctr_drbg_random, &mbedtls_rng); + + /* load default certificates */ + if (crtpath != NULL && stat(crtpath, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) + loaded = (git_mbedtls__set_cert_location(crtpath, NULL) == 0); + + if (!loaded && crtpath != NULL && stat(crtpath, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) + loaded = (git_mbedtls__set_cert_location(NULL, crtpath) == 0); + + initialized = true; + + return git_runtime_shutdown_register(shutdown_ssl); + +cleanup: + mbedtls_ctr_drbg_free(&mbedtls_rng); + mbedtls_ssl_config_free(&mbedtls_config); + mbedtls_entropy_free(&mbedtls_entropy); + + return -1; +} + +static int bio_read(void *b, unsigned char *buf, size_t len) +{ + git_stream *io = (git_stream *) b; + return (int) git_stream_read(io, buf, min(len, INT_MAX)); +} + +static int bio_write(void *b, const unsigned char *buf, size_t len) +{ + git_stream *io = (git_stream *) b; + return (int) git_stream_write(io, (const char *)buf, min(len, INT_MAX), 0); +} + +static int ssl_set_error(mbedtls_ssl_context *ssl, int error) +{ + char errbuf[512]; + int ret = -1; + + GIT_ASSERT(error != MBEDTLS_ERR_SSL_WANT_READ); + GIT_ASSERT(error != MBEDTLS_ERR_SSL_WANT_WRITE); + + if (error != 0) + mbedtls_strerror( error, errbuf, 512 ); + + switch(error) { + case 0: + git_error_set(GIT_ERROR_SSL, "SSL error: unknown error"); + break; + + case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: + git_error_set(GIT_ERROR_SSL, "SSL error: %#04x [%x] - %s", error, mbedtls_ssl_get_verify_result(ssl), errbuf); + ret = GIT_ECERTIFICATE; + break; + + default: + git_error_set(GIT_ERROR_SSL, "SSL error: %#04x - %s", error, errbuf); + } + + return ret; +} + +static int ssl_teardown(mbedtls_ssl_context *ssl) +{ + int ret = 0; + + ret = mbedtls_ssl_close_notify(ssl); + if (ret < 0) + ret = ssl_set_error(ssl, ret); + + mbedtls_ssl_free(ssl); + return ret; +} + +static int verify_server_cert(mbedtls_ssl_context *ssl) +{ + int ret = -1; + + if ((ret = mbedtls_ssl_get_verify_result(ssl)) != 0) { + char vrfy_buf[512]; + int len = mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "", ret); + if (len >= 1) vrfy_buf[len - 1] = '\0'; /* Remove trailing \n */ + git_error_set(GIT_ERROR_SSL, "the SSL certificate is invalid: %#04x - %s", ret, vrfy_buf); + return GIT_ECERTIFICATE; + } + + return 0; +} + +typedef struct { + git_stream parent; + git_stream *io; + int owned; + bool connected; + char *host; + mbedtls_ssl_context *ssl; + git_cert_x509 cert_info; +} mbedtls_stream; + + +static int mbedtls_connect(git_stream *stream) +{ + int ret; + mbedtls_stream *st = (mbedtls_stream *) stream; + + if (st->owned && (ret = git_stream_connect(st->io)) < 0) + return ret; + + st->connected = true; + + mbedtls_ssl_set_hostname(st->ssl, st->host); + + mbedtls_ssl_set_bio(st->ssl, st->io, bio_write, bio_read, NULL); + + if ((ret = mbedtls_ssl_handshake(st->ssl)) != 0) + return ssl_set_error(st->ssl, ret); + + return verify_server_cert(st->ssl); +} + +static int mbedtls_certificate(git_cert **out, git_stream *stream) +{ + unsigned char *encoded_cert; + mbedtls_stream *st = (mbedtls_stream *) stream; + + const mbedtls_x509_crt *cert = mbedtls_ssl_get_peer_cert(st->ssl); + if (!cert) { + git_error_set(GIT_ERROR_SSL, "the server did not provide a certificate"); + return -1; + } + + /* Retrieve the length of the certificate first */ + if (cert->raw.len == 0) { + git_error_set(GIT_ERROR_NET, "failed to retrieve certificate information"); + return -1; + } + + encoded_cert = git__malloc(cert->raw.len); + GIT_ERROR_CHECK_ALLOC(encoded_cert); + memcpy(encoded_cert, cert->raw.p, cert->raw.len); + + st->cert_info.parent.cert_type = GIT_CERT_X509; + st->cert_info.data = encoded_cert; + st->cert_info.len = cert->raw.len; + + *out = &st->cert_info.parent; + + return 0; +} + +static int mbedtls_set_proxy(git_stream *stream, const git_proxy_options *proxy_options) +{ + mbedtls_stream *st = (mbedtls_stream *) stream; + + return git_stream_set_proxy(st->io, proxy_options); +} + +static ssize_t mbedtls_stream_write(git_stream *stream, const char *data, size_t len, int flags) +{ + mbedtls_stream *st = (mbedtls_stream *) stream; + int written; + + GIT_UNUSED(flags); + + /* + * `mbedtls_ssl_write` can only represent INT_MAX bytes + * written via its return value. We thus need to clamp + * the maximum number of bytes written. + */ + len = min(len, INT_MAX); + + if ((written = mbedtls_ssl_write(st->ssl, (const unsigned char *)data, len)) <= 0) + return ssl_set_error(st->ssl, written); + + return written; +} + +static ssize_t mbedtls_stream_read(git_stream *stream, void *data, size_t len) +{ + mbedtls_stream *st = (mbedtls_stream *) stream; + int ret; + + if ((ret = mbedtls_ssl_read(st->ssl, (unsigned char *)data, len)) <= 0) + ssl_set_error(st->ssl, ret); + + return ret; +} + +static int mbedtls_stream_close(git_stream *stream) +{ + mbedtls_stream *st = (mbedtls_stream *) stream; + int ret = 0; + + if (st->connected && (ret = ssl_teardown(st->ssl)) != 0) + return -1; + + st->connected = false; + + return st->owned ? git_stream_close(st->io) : 0; +} + +static void mbedtls_stream_free(git_stream *stream) +{ + mbedtls_stream *st = (mbedtls_stream *) stream; + + if (st->owned) + git_stream_free(st->io); + + git__free(st->host); + git__free(st->cert_info.data); + mbedtls_ssl_free(st->ssl); + git__free(st->ssl); + git__free(st); +} + +static int mbedtls_stream_wrap( + git_stream **out, + git_stream *in, + const char *host, + int owned) +{ + mbedtls_stream *st; + int error; + + st = git__calloc(1, sizeof(mbedtls_stream)); + GIT_ERROR_CHECK_ALLOC(st); + + st->io = in; + st->owned = owned; + + st->ssl = git__malloc(sizeof(mbedtls_ssl_context)); + GIT_ERROR_CHECK_ALLOC(st->ssl); + mbedtls_ssl_init(st->ssl); + if (mbedtls_ssl_setup(st->ssl, &mbedtls_config)) { + git_error_set(GIT_ERROR_SSL, "failed to create ssl object"); + error = -1; + goto out_err; + } + + st->host = git__strdup(host); + GIT_ERROR_CHECK_ALLOC(st->host); + + st->parent.version = GIT_STREAM_VERSION; + st->parent.encrypted = 1; + st->parent.proxy_support = git_stream_supports_proxy(st->io); + st->parent.connect = mbedtls_connect; + st->parent.certificate = mbedtls_certificate; + st->parent.set_proxy = mbedtls_set_proxy; + st->parent.read = mbedtls_stream_read; + st->parent.write = mbedtls_stream_write; + st->parent.close = mbedtls_stream_close; + st->parent.free = mbedtls_stream_free; + + *out = (git_stream *) st; + return 0; + +out_err: + mbedtls_ssl_free(st->ssl); + git_stream_close(st->io); + git_stream_free(st->io); + git__free(st); + + return error; +} + +int git_mbedtls_stream_wrap( + git_stream **out, + git_stream *in, + const char *host) +{ + return mbedtls_stream_wrap(out, in, host, 0); +} + +int git_mbedtls_stream_new( + git_stream **out, + const char *host, + const char *port) +{ + git_stream *stream; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(host); + GIT_ASSERT_ARG(port); + + if ((error = git_socket_stream_new(&stream, host, port)) < 0) + return error; + + if ((error = mbedtls_stream_wrap(out, stream, host, 1)) < 0) { + git_stream_close(stream); + git_stream_free(stream); + } + + return error; +} + +int git_mbedtls__set_cert_location(const char *file, const char *path) +{ + int ret = 0; + char errbuf[512]; + + GIT_ASSERT_ARG(file || path); + + if (has_ca_chain) + mbedtls_x509_crt_free(&mbedtls_ca_chain); + + mbedtls_x509_crt_init(&mbedtls_ca_chain); + + if (file) + ret = mbedtls_x509_crt_parse_file(&mbedtls_ca_chain, file); + + if (ret >= 0 && path) + ret = mbedtls_x509_crt_parse_path(&mbedtls_ca_chain, path); + + /* mbedtls_x509_crt_parse_path returns the number of invalid certs on success */ + if (ret < 0) { + mbedtls_x509_crt_free(&mbedtls_ca_chain); + mbedtls_strerror( ret, errbuf, 512 ); + git_error_set(GIT_ERROR_SSL, "failed to load CA certificates: %#04x - %s", ret, errbuf); + return -1; + } + + mbedtls_ssl_conf_ca_chain(&mbedtls_config, &mbedtls_ca_chain, NULL); + has_ca_chain = true; + + return 0; +} + +#else + +#include "stream.h" + +int git_mbedtls_stream_global_init(void) +{ + return 0; +} + +#endif diff --git a/src/libgit2/streams/mbedtls.h b/src/libgit2/streams/mbedtls.h new file mode 100644 index 00000000000..76c0627a2ca --- /dev/null +++ b/src/libgit2/streams/mbedtls.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_steams_mbedtls_h__ +#define INCLUDE_steams_mbedtls_h__ + +#include "common.h" + +#include "git2/sys/stream.h" + +extern int git_mbedtls_stream_global_init(void); + +#ifdef GIT_HTTPS_MBEDTLS +extern int git_mbedtls__set_cert_location(const char *file, const char *path); + +extern int git_mbedtls_stream_new(git_stream **out, const char *host, const char *port); +extern int git_mbedtls_stream_wrap(git_stream **out, git_stream *in, const char *host); +#endif + +#endif diff --git a/src/libgit2/streams/openssl.c b/src/libgit2/streams/openssl.c new file mode 100644 index 00000000000..f12b699f9b0 --- /dev/null +++ b/src/libgit2/streams/openssl.c @@ -0,0 +1,768 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "streams/openssl.h" +#include "streams/openssl_legacy.h" +#include "streams/openssl_dynamic.h" + +#if defined(GIT_HTTPS_OPENSSL) || defined(GIT_HTTPS_OPENSSL_DYNAMIC) + +#include + +#include "common.h" +#include "runtime.h" +#include "settings.h" +#include "posix.h" +#include "stream.h" +#include "net.h" +#include "streams/socket.h" +#include "git2/transport.h" +#include "git2/sys/openssl.h" + +#ifndef GIT_WIN32 +# include +# include +# include +#endif + +#ifndef GIT_HTTPS_OPENSSL_DYNAMIC +# include +# include +# include +# include +#endif + +extern char *git__ssl_ciphers; + +SSL_CTX *git__ssl_ctx; + +#define GIT_SSL_DEFAULT_CIPHERS "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305" + + +static BIO_METHOD *git_stream_bio_method; +static int init_bio_method(void); + +/** + * This function aims to clean-up the SSL context which + * we allocated. + */ +static void shutdown_ssl(void) +{ + if (git_stream_bio_method) { + BIO_meth_free(git_stream_bio_method); + git_stream_bio_method = NULL; + } + + if (git__ssl_ctx) { + SSL_CTX_free(git__ssl_ctx); + git__ssl_ctx = NULL; + } +} + +#ifdef VALGRIND +# if !defined(GIT_HTTPS_OPENSSL_LEGACY) && !defined(GIT_HTTPS_OPENSSL_DYNAMIC) + +static void *git_openssl_malloc(size_t bytes, const char *file, int line) +{ + GIT_UNUSED(file); + GIT_UNUSED(line); + return git__calloc(1, bytes); +} + +static void *git_openssl_realloc(void *mem, size_t size, const char *file, int line) +{ + GIT_UNUSED(file); + GIT_UNUSED(line); + return git__realloc(mem, size); +} + +static void git_openssl_free(void *mem, const char *file, int line) +{ + GIT_UNUSED(file); + GIT_UNUSED(line); + git__free(mem); +} +# else /* !GIT_HTTPS_OPENSSL_LEGACY && !GIT_HTTPS_OPENSSL_DYNAMIC */ +static void *git_openssl_malloc(size_t bytes) +{ + return git__calloc(1, bytes); +} + +static void *git_openssl_realloc(void *mem, size_t size) +{ + return git__realloc(mem, size); +} + +static void git_openssl_free(void *mem) +{ + git__free(mem); +} +# endif /* !GIT_HTTPS_OPENSSL_LEGACY && !GIT_HTTPS_OPENSSL_DYNAMIC */ +#endif /* VALGRIND */ + +static int openssl_init(void) +{ + long ssl_opts = SSL_OP_NO_SSLv2 | + SSL_OP_NO_SSLv3 | + SSL_OP_NO_TLSv1 | + SSL_OP_NO_TLSv1_1; + const char *ciphers = git__ssl_ciphers; +#ifdef VALGRIND + static bool allocators_initialized = false; +#endif + + /* Older OpenSSL and MacOS OpenSSL doesn't have this */ +#ifdef SSL_OP_NO_COMPRESSION + ssl_opts |= SSL_OP_NO_COMPRESSION; +#endif + +#ifdef VALGRIND + /* + * Swap in our own allocator functions that initialize + * allocated memory to avoid spurious valgrind warnings. + * Don't error on failure; many builds of OpenSSL do not + * allow you to set these functions. + */ + if (!allocators_initialized) { + CRYPTO_set_mem_functions(git_openssl_malloc, + git_openssl_realloc, + git_openssl_free); + allocators_initialized = true; + } +#endif + + OPENSSL_init_ssl(0, NULL); + + /* + * Despite the name SSLv23_method, this is actually a version- + * flexible context, which honors the protocol versions + * specified in `ssl_opts`. So we only support TLSv1.2 and + * higher. + */ + if (!(git__ssl_ctx = SSL_CTX_new(SSLv23_method()))) + goto error; + + SSL_CTX_set_options(git__ssl_ctx, ssl_opts); + SSL_CTX_set_mode(git__ssl_ctx, SSL_MODE_AUTO_RETRY); + SSL_CTX_set_verify(git__ssl_ctx, SSL_VERIFY_NONE, NULL); + if (!SSL_CTX_set_default_verify_paths(git__ssl_ctx)) + goto error; + + if (!ciphers) + ciphers = GIT_SSL_DEFAULT_CIPHERS; + + if(!SSL_CTX_set_cipher_list(git__ssl_ctx, ciphers)) + goto error; + + if (init_bio_method() < 0) + goto error; + + return git_runtime_shutdown_register(shutdown_ssl); + +error: + git_error_set(GIT_ERROR_NET, "could not initialize openssl: %s", + ERR_error_string(ERR_get_error(), NULL)); + SSL_CTX_free(git__ssl_ctx); + git__ssl_ctx = NULL; + return -1; +} + +/* + * When we use dynamic loading, we defer OpenSSL initialization until + * it's first used. `openssl_ensure_initialized` will do the work + * under a mutex. + */ +git_mutex openssl_mutex; +bool openssl_initialized; + +int git_openssl_stream_global_init(void) +{ +#ifndef GIT_HTTPS_OPENSSL_DYNAMIC + return openssl_init(); +#else + if (git_mutex_init(&openssl_mutex) != 0) + return -1; + + return 0; +#endif +} + +static int openssl_ensure_initialized(void) +{ +#ifdef GIT_HTTPS_OPENSSL_DYNAMIC + int error = 0; + + if (git_mutex_lock(&openssl_mutex) != 0) + return -1; + + if (!openssl_initialized) { + if ((error = git_openssl_stream_dynamic_init()) == 0) + error = openssl_init(); + + openssl_initialized = !error; + } + + error |= git_mutex_unlock(&openssl_mutex); + return error; + +#else + return 0; +#endif +} + +#if !defined(GIT_HTTPS_OPENSSL_LEGACY) && !defined(GIT_HTTPS_OPENSSL_DYNAMIC) +int git_openssl_set_locking(void) +{ +# ifdef GIT_THREADS + return 0; +# else + git_error_set(GIT_ERROR_THREAD, "libgit2 was not built with threads"); + return -1; +# endif +} +#endif + + +static int bio_create(BIO *b) +{ + BIO_set_init(b, 1); + BIO_set_data(b, NULL); + + return 1; +} + +static int bio_destroy(BIO *b) +{ + if (!b) + return 0; + + BIO_set_data(b, NULL); + + return 1; +} + +static int bio_read(BIO *b, char *buf, int len) +{ + git_stream *io = (git_stream *) BIO_get_data(b); + + return (int) git_stream_read(io, buf, len); +} + +static int bio_write(BIO *b, const char *buf, int len) +{ + git_stream *io = (git_stream *) BIO_get_data(b); + return (int) git_stream_write(io, buf, len, 0); +} + +static long bio_ctrl(BIO *b, int cmd, long num, void *ptr) +{ + GIT_UNUSED(b); + GIT_UNUSED(num); + GIT_UNUSED(ptr); + + if (cmd == BIO_CTRL_FLUSH) + return 1; + + return 0; +} + +static int bio_gets(BIO *b, char *buf, int len) +{ + GIT_UNUSED(b); + GIT_UNUSED(buf); + GIT_UNUSED(len); + return -1; +} + +static int bio_puts(BIO *b, const char *str) +{ + return bio_write(b, str, strlen(str)); +} + +static int init_bio_method(void) +{ + /* Set up the BIO_METHOD we use for wrapping our own stream implementations */ + git_stream_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK | BIO_get_new_index(), "git_stream"); + GIT_ERROR_CHECK_ALLOC(git_stream_bio_method); + + BIO_meth_set_write(git_stream_bio_method, bio_write); + BIO_meth_set_read(git_stream_bio_method, bio_read); + BIO_meth_set_puts(git_stream_bio_method, bio_puts); + BIO_meth_set_gets(git_stream_bio_method, bio_gets); + BIO_meth_set_ctrl(git_stream_bio_method, bio_ctrl); + BIO_meth_set_create(git_stream_bio_method, bio_create); + BIO_meth_set_destroy(git_stream_bio_method, bio_destroy); + + return 0; +} + +static int ssl_set_error(SSL *ssl, int error) +{ + int err; + unsigned long e; + + err = SSL_get_error(ssl, error); + + GIT_ASSERT(err != SSL_ERROR_WANT_READ); + GIT_ASSERT(err != SSL_ERROR_WANT_WRITE); + + switch (err) { + case SSL_ERROR_WANT_CONNECT: + case SSL_ERROR_WANT_ACCEPT: + git_error_set(GIT_ERROR_SSL, "SSL error: connection failure"); + break; + case SSL_ERROR_WANT_X509_LOOKUP: + git_error_set(GIT_ERROR_SSL, "SSL error: x509 error"); + break; + case SSL_ERROR_SYSCALL: + e = ERR_get_error(); + if (e > 0) { + char errmsg[256]; + ERR_error_string_n(e, errmsg, sizeof(errmsg)); + git_error_set(GIT_ERROR_NET, "SSL error: %s", errmsg); + break; + } else if (error < 0) { + git_error_set(GIT_ERROR_OS, "SSL error: syscall failure"); + break; + } + git_error_set(GIT_ERROR_SSL, "SSL error: received early EOF"); + return GIT_EEOF; + break; + case SSL_ERROR_SSL: + { + char errmsg[256]; + e = ERR_get_error(); + ERR_error_string_n(e, errmsg, sizeof(errmsg)); + git_error_set(GIT_ERROR_SSL, "SSL error: %s", errmsg); + break; + } + case SSL_ERROR_NONE: + case SSL_ERROR_ZERO_RETURN: + default: + git_error_set(GIT_ERROR_SSL, "SSL error: unknown error"); + break; + } + return -1; +} + +static int ssl_teardown(SSL *ssl) +{ + int ret; + + ret = SSL_shutdown(ssl); + if (ret < 0) + ret = ssl_set_error(ssl, ret); + else + ret = 0; + + return ret; +} + +static bool check_host_name(const char *host, const char *name) +{ + return !strcasecmp(host, name) || + git_net_hostname_matches_cert(host, name); +} + +static int verify_server_cert(SSL *ssl, const char *host) +{ + X509 *cert = NULL; + X509_NAME *peer_name; + ASN1_STRING *str; + unsigned char *peer_cn = NULL; + int matched = -1, type = GEN_DNS; + GENERAL_NAMES *alts; + struct in6_addr addr6; + struct in_addr addr4; + void *addr = NULL; + int i = -1, j, error = 0; + + if (SSL_get_verify_result(ssl) != X509_V_OK) { + git_error_set(GIT_ERROR_SSL, "the SSL certificate is invalid"); + return GIT_ECERTIFICATE; + } + + /* Try to parse the host as an IP address to see if it is */ + if (p_inet_pton(AF_INET, host, &addr4)) { + type = GEN_IPADD; + addr = &addr4; + } else { + if (p_inet_pton(AF_INET6, host, &addr6)) { + type = GEN_IPADD; + addr = &addr6; + } + } + + + cert = SSL_get_peer_certificate(ssl); + if (!cert) { + error = -1; + git_error_set(GIT_ERROR_SSL, "the server did not provide a certificate"); + goto cleanup; + } + + /* Check the alternative names */ + alts = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); + if (alts) { + int num; + + num = sk_GENERAL_NAME_num(alts); + for (i = 0; i < num && matched != 1; i++) { + const GENERAL_NAME *gn = sk_GENERAL_NAME_value(alts, i); + const char *name = (char *) ASN1_STRING_get0_data(gn->d.ia5); + size_t namelen = (size_t) ASN1_STRING_length(gn->d.ia5); + + /* Skip any names of a type we're not looking for */ + if (gn->type != type) + continue; + + if (type == GEN_DNS) { + /* If it contains embedded NULs, don't even try */ + if (memchr(name, '\0', namelen)) + continue; + + matched = !!check_host_name(host, name); + } else if (type == GEN_IPADD) { + /* Here name isn't so much a name but a binary representation of the IP */ + matched = addr && !!memcmp(name, addr, namelen); + } + } + } + GENERAL_NAMES_free(alts); + + if (matched == 0) + goto cert_fail_name; + + if (matched == 1) { + goto cleanup; + } + + /* If no alternative names are available, check the common name */ + peer_name = X509_get_subject_name(cert); + if (peer_name == NULL) + goto on_error; + + if (peer_name) { + /* Get the index of the last CN entry */ + while ((j = X509_NAME_get_index_by_NID(peer_name, NID_commonName, i)) >= 0) + i = j; + } + + if (i < 0) + goto on_error; + + str = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(peer_name, i)); + if (str == NULL) + goto on_error; + + /* Work around a bug in OpenSSL whereby ASN1_STRING_to_UTF8 fails if it's already in utf-8 */ + if (ASN1_STRING_type(str) == V_ASN1_UTF8STRING) { + int size = ASN1_STRING_length(str); + + if (size > 0) { + peer_cn = OPENSSL_malloc(size + 1); + GIT_ERROR_CHECK_ALLOC(peer_cn); + memcpy(peer_cn, ASN1_STRING_get0_data(str), size); + peer_cn[size] = '\0'; + } else { + goto cert_fail_name; + } + } else { + int size = ASN1_STRING_to_UTF8(&peer_cn, str); + GIT_ERROR_CHECK_ALLOC(peer_cn); + if (memchr(peer_cn, '\0', size)) + goto cert_fail_name; + } + + if (!check_host_name(host, (char *)peer_cn)) + goto cert_fail_name; + + goto cleanup; + +cert_fail_name: + error = GIT_ECERTIFICATE; + git_error_set(GIT_ERROR_SSL, "hostname does not match certificate"); + goto cleanup; + +on_error: + error = ssl_set_error(ssl, 0); + goto cleanup; + +cleanup: + X509_free(cert); + OPENSSL_free(peer_cn); + return error; +} + +typedef struct { + git_stream parent; + git_stream *io; + int owned; + bool connected; + char *host; + SSL *ssl; + git_cert_x509 cert_info; +} openssl_stream; + +static int openssl_connect(git_stream *stream) +{ + int ret; + BIO *bio; + openssl_stream *st = (openssl_stream *) stream; + + if (st->owned && (ret = git_stream_connect(st->io)) < 0) + return ret; + + bio = BIO_new(git_stream_bio_method); + GIT_ERROR_CHECK_ALLOC(bio); + + BIO_set_data(bio, st->io); + SSL_set_bio(st->ssl, bio, bio); + + /* specify the host in case SNI is needed */ +#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME + SSL_set_tlsext_host_name(st->ssl, st->host); +#endif + + if ((ret = SSL_connect(st->ssl)) <= 0) + return ssl_set_error(st->ssl, ret); + + st->connected = true; + + return verify_server_cert(st->ssl, st->host); +} + +static int openssl_certificate(git_cert **out, git_stream *stream) +{ + openssl_stream *st = (openssl_stream *) stream; + X509 *cert = SSL_get_peer_certificate(st->ssl); + unsigned char *guard, *encoded_cert = NULL; + int error, len; + + /* Retrieve the length of the certificate first */ + len = i2d_X509(cert, NULL); + if (len < 0) { + git_error_set(GIT_ERROR_NET, "failed to retrieve certificate information"); + error = -1; + goto out; + } + + encoded_cert = git__malloc(len); + GIT_ERROR_CHECK_ALLOC(encoded_cert); + /* i2d_X509 makes 'guard' point to just after the data */ + guard = encoded_cert; + + len = i2d_X509(cert, &guard); + if (len < 0) { + git_error_set(GIT_ERROR_NET, "failed to retrieve certificate information"); + error = -1; + goto out; + } + + st->cert_info.parent.cert_type = GIT_CERT_X509; + st->cert_info.data = encoded_cert; + st->cert_info.len = len; + encoded_cert = NULL; + + *out = &st->cert_info.parent; + error = 0; + +out: + git__free(encoded_cert); + X509_free(cert); + return error; +} + +static int openssl_set_proxy(git_stream *stream, const git_proxy_options *proxy_opts) +{ + openssl_stream *st = (openssl_stream *) stream; + + return git_stream_set_proxy(st->io, proxy_opts); +} + +static ssize_t openssl_write(git_stream *stream, const char *data, size_t data_len, int flags) +{ + openssl_stream *st = (openssl_stream *) stream; + int ret, len = min(data_len, INT_MAX); + + GIT_UNUSED(flags); + + if ((ret = SSL_write(st->ssl, data, len)) <= 0) + return ssl_set_error(st->ssl, ret); + + return ret; +} + +static ssize_t openssl_read(git_stream *stream, void *data, size_t len) +{ + openssl_stream *st = (openssl_stream *) stream; + int ret; + + if ((ret = SSL_read(st->ssl, data, len)) <= 0) + return ssl_set_error(st->ssl, ret); + + return ret; +} + +static int openssl_close(git_stream *stream) +{ + openssl_stream *st = (openssl_stream *) stream; + int ret; + + if (st->connected && (ret = ssl_teardown(st->ssl)) < 0) + return -1; + + st->connected = false; + + return st->owned ? git_stream_close(st->io) : 0; +} + +static void openssl_free(git_stream *stream) +{ + openssl_stream *st = (openssl_stream *) stream; + + if (st->owned) + git_stream_free(st->io); + + SSL_free(st->ssl); + git__free(st->host); + git__free(st->cert_info.data); + git__free(st); +} + +static int openssl_stream_wrap( + git_stream **out, + git_stream *in, + const char *host, + int owned) +{ + openssl_stream *st; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(in); + GIT_ASSERT_ARG(host); + + st = git__calloc(1, sizeof(openssl_stream)); + GIT_ERROR_CHECK_ALLOC(st); + + st->io = in; + st->owned = owned; + + st->ssl = SSL_new(git__ssl_ctx); + if (st->ssl == NULL) { + git_error_set(GIT_ERROR_SSL, "failed to create ssl object"); + git__free(st); + return -1; + } + + st->host = git__strdup(host); + GIT_ERROR_CHECK_ALLOC(st->host); + + st->parent.version = GIT_STREAM_VERSION; + st->parent.encrypted = 1; + st->parent.proxy_support = git_stream_supports_proxy(st->io); + st->parent.connect = openssl_connect; + st->parent.certificate = openssl_certificate; + st->parent.set_proxy = openssl_set_proxy; + st->parent.read = openssl_read; + st->parent.write = openssl_write; + st->parent.close = openssl_close; + st->parent.free = openssl_free; + + *out = (git_stream *) st; + return 0; +} + +int git_openssl_stream_wrap(git_stream **out, git_stream *in, const char *host) +{ + if (openssl_ensure_initialized() < 0) + return -1; + + return openssl_stream_wrap(out, in, host, 0); +} + +int git_openssl_stream_new(git_stream **out, const char *host, const char *port) +{ + git_stream *stream = NULL; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(host); + GIT_ASSERT_ARG(port); + + if (openssl_ensure_initialized() < 0) + return -1; + + if ((error = git_socket_stream_new(&stream, host, port)) < 0) + return error; + + if ((error = openssl_stream_wrap(out, stream, host, 1)) < 0) { + git_stream_close(stream); + git_stream_free(stream); + } + + return error; +} + +int git_openssl__set_cert_location(const char *file, const char *path) +{ + if (openssl_ensure_initialized() < 0) + return -1; + + if (SSL_CTX_load_verify_locations(git__ssl_ctx, file, path) == 0) { + char errmsg[256]; + + ERR_error_string_n(ERR_get_error(), errmsg, sizeof(errmsg)); + git_error_set(GIT_ERROR_SSL, "OpenSSL error: failed to load certificates: %s", + errmsg); + + return -1; + } + return 0; +} + +int git_openssl__add_x509_cert(X509 *cert) +{ + X509_STORE *cert_store; + + if (openssl_ensure_initialized() < 0) + return -1; + + if (!(cert_store = SSL_CTX_get_cert_store(git__ssl_ctx))) + return -1; + + if (cert && X509_STORE_add_cert(cert_store, cert) == 0) { + git_error_set(GIT_ERROR_SSL, "OpenSSL error: failed to add raw X509 certificate"); + return -1; + } + + return 0; +} + +int git_openssl__reset_context(void) +{ + shutdown_ssl(); + return openssl_init(); +} + +#else + +#include "stream.h" +#include "git2/sys/openssl.h" + +int git_openssl_stream_global_init(void) +{ + return 0; +} + +int git_openssl_set_locking(void) +{ + git_error_set(GIT_ERROR_SSL, "libgit2 was not built with OpenSSL support"); + return -1; +} + +#endif diff --git a/src/libgit2/streams/openssl.h b/src/libgit2/streams/openssl.h new file mode 100644 index 00000000000..2a5f04099bc --- /dev/null +++ b/src/libgit2/streams/openssl.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_streams_openssl_h__ +#define INCLUDE_streams_openssl_h__ + +#include "common.h" +#include "streams/openssl_legacy.h" +#include "streams/openssl_dynamic.h" + +#include "git2/sys/stream.h" + +extern int git_openssl_stream_global_init(void); + +#if defined(GIT_HTTPS_OPENSSL) +# include +# include +# include +# include +# endif + +#if defined(GIT_HTTPS_OPENSSL) || defined(GIT_HTTPS_OPENSSL_DYNAMIC) +extern int git_openssl__set_cert_location(const char *file, const char *path); +extern int git_openssl__add_x509_cert(X509 *cert); +extern int git_openssl__reset_context(void); +extern int git_openssl_stream_new(git_stream **out, const char *host, const char *port); +extern int git_openssl_stream_wrap(git_stream **out, git_stream *in, const char *host); +#endif + +#endif diff --git a/src/libgit2/streams/openssl_dynamic.c b/src/libgit2/streams/openssl_dynamic.c new file mode 100644 index 00000000000..3ab292073d9 --- /dev/null +++ b/src/libgit2/streams/openssl_dynamic.c @@ -0,0 +1,318 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "streams/openssl.h" +#include "streams/openssl_dynamic.h" + +#ifdef GIT_HTTPS_OPENSSL_DYNAMIC + +#include "runtime.h" + +#include + +unsigned char *(*ASN1_STRING_data)(ASN1_STRING *x); +const unsigned char *(*ASN1_STRING_get0_data)(const ASN1_STRING *x); +int (*ASN1_STRING_length)(const ASN1_STRING *x); +int (*ASN1_STRING_to_UTF8)(unsigned char **out, const ASN1_STRING *in); +int (*ASN1_STRING_type)(const ASN1_STRING *x); + +void *(*BIO_get_data)(BIO *a); +int (*BIO_get_new_index)(void); +int (*OPENSSL_init_ssl)(uint64_t opts, const void *settings); +void (*BIO_meth_free)(BIO_METHOD *biom); +int (*BIO_meth_set_create)(BIO_METHOD *biom, int (*create) (BIO *)); +int (*BIO_meth_set_ctrl)(BIO_METHOD *biom, long (*ctrl) (BIO *, int, long, void *)); +int (*BIO_meth_set_destroy)(BIO_METHOD *biom, int (*destroy) (BIO *)); +int (*BIO_meth_set_gets)(BIO_METHOD *biom, int (*gets) (BIO *, char *, int)); +int (*BIO_meth_set_puts)(BIO_METHOD *biom, int (*puts) (BIO *, const char *)); +int (*BIO_meth_set_read)(BIO_METHOD *biom, int (*read) (BIO *, char *, int)); +int (*BIO_meth_set_write)(BIO_METHOD *biom, int (*write) (BIO *, const char *, int)); +BIO_METHOD *(*BIO_meth_new)(int type, const char *name); +BIO *(*BIO_new)(const BIO_METHOD *type); +void (*BIO_set_data)(BIO *a, void *ptr); +void (*BIO_set_init)(BIO *a, int init); + +void (*CRYPTO_free)(void *ptr, const char *file, int line); +void *(*CRYPTO_malloc)(size_t num, const char *file, int line); +int (*CRYPTO_num_locks)(void); +void (*CRYPTO_set_locking_callback)(void (*func)(int mode, int type, const char *file, int line)); +int (*CRYPTO_set_mem_functions)(void *(*m)(size_t bytes), void *(*r)(void *mem, size_t size), void (*f)(void *mem)); +int (*CRYPTO_THREADID_set_callback)(void (*func)(CRYPTO_THREADID *id)); +void (*CRYPTO_THREADID_set_numeric)(CRYPTO_THREADID *id, unsigned long val); + +char *(*ERR_error_string)(unsigned long e, char *buf); +void (*ERR_error_string_n)(unsigned long e, char *buf, size_t len); +unsigned long (*ERR_get_error)(void); + +int (*SSL_connect)(SSL *ssl); +long (*SSL_ctrl)(SSL *ssl, int cmd, long arg, void *parg); +void (*SSL_free)(SSL *ssl); +int (*SSL_get_error)(SSL *ssl, int ret); +X509 *(*SSL_get_peer_certificate)(const SSL *ssl); +long (*SSL_get_verify_result)(const SSL *ssl); +int (*SSL_library_init)(void); +void (*SSL_load_error_strings)(void); +SSL *(*SSL_new)(SSL_CTX *ctx); +int (*SSL_read)(SSL *ssl, const void *buf, int num); +void (*SSL_set_bio)(SSL *ssl, BIO *rbio, BIO *wbio); +int (*SSL_shutdown)(SSL *ssl); +int (*SSL_write)(SSL *ssl, const void *buf, int num); + +long (*SSL_CTX_ctrl)(SSL_CTX *ctx, int cmd, long larg, void *parg); +void (*SSL_CTX_free)(SSL_CTX *ctx); +SSL_CTX *(*SSL_CTX_new)(const SSL_METHOD *method); +X509_STORE *(*SSL_CTX_get_cert_store)(const SSL_CTX *); +int (*SSL_CTX_set_cipher_list)(SSL_CTX *ctx, const char *str); +int (*SSL_CTX_set_default_verify_paths)(SSL_CTX *ctx); +long (*SSL_CTX_set_options)(SSL_CTX *ctx, long options); +void (*SSL_CTX_set_verify)(SSL_CTX *ctx, int mode, int (*verify_callback)(int, X509_STORE_CTX *)); +int (*SSL_CTX_load_verify_locations)(SSL_CTX *ctx, const char *CAfile, const char *CApath); + +const SSL_METHOD *(*SSLv23_method)(void); +const SSL_METHOD *(*TLS_method)(void); + +ASN1_STRING *(*X509_NAME_ENTRY_get_data)(const X509_NAME_ENTRY *ne); +X509_NAME_ENTRY *(*X509_NAME_get_entry)(X509_NAME *name, int loc); +int (*X509_NAME_get_index_by_NID)(X509_NAME *name, int nid, int lastpos); +void (*X509_free)(X509 *a); +void *(*X509_get_ext_d2i)(const X509 *x, int nid, int *crit, int *idx); +X509_NAME *(*X509_get_subject_name)(const X509 *x); +int (*X509_STORE_add_cert)(X509_STORE *ctx, X509 *x); + +int (*i2d_X509)(X509 *a, unsigned char **ppout); + +int (*OPENSSL_sk_num)(const void *sk); +void *(*OPENSSL_sk_value)(const void *sk, int i); +void (*OPENSSL_sk_free)(void *sk); + +int (*sk_num)(const void *sk); +void *(*sk_value)(const void *sk, int i); +void (*sk_free)(void *sk); + +static void *openssl_handle; + +GIT_INLINE(void *) openssl_sym(int *err, const char *name, bool required) +{ + void *symbol; + + /* if we've seen an err, noop to retain it */ + if (*err) + return NULL; + + + if ((symbol = dlsym(openssl_handle, name)) == NULL && required) { + const char *msg = dlerror(); + git_error_set(GIT_ERROR_SSL, "could not load ssl function '%s': %s", name, msg ? msg : "unknown error"); + *err = -1; + } + + return symbol; +} + +static void dynamic_shutdown(void) +{ + dlclose(openssl_handle); + openssl_handle = NULL; +} + +int git_openssl_stream_dynamic_init(void) +{ + int err = 0; + + if ((openssl_handle = dlopen("libssl.so.1.1", RTLD_NOW)) == NULL && + (openssl_handle = dlopen("libssl.1.1.dylib", RTLD_NOW)) == NULL && + (openssl_handle = dlopen("libssl.so.1.0.0", RTLD_NOW)) == NULL && + (openssl_handle = dlopen("libssl.1.0.0.dylib", RTLD_NOW)) == NULL && + (openssl_handle = dlopen("libssl.so.10", RTLD_NOW)) == NULL && + (openssl_handle = dlopen("libssl.so.3", RTLD_NOW)) == NULL && + (openssl_handle = dlopen("libssl.3.dylib", RTLD_NOW)) == NULL) { + git_error_set(GIT_ERROR_SSL, "could not load ssl libraries"); + return -1; + } + + ASN1_STRING_data = (unsigned char *(*)(ASN1_STRING *x))openssl_sym(&err, "ASN1_STRING_data", false); + ASN1_STRING_get0_data = (const unsigned char *(*)(const ASN1_STRING *x))openssl_sym(&err, "ASN1_STRING_get0_data", false); + ASN1_STRING_length = (int (*)(const ASN1_STRING *))openssl_sym(&err, "ASN1_STRING_length", true); + ASN1_STRING_to_UTF8 = (int (*)(unsigned char **, const ASN1_STRING *))openssl_sym(&err, "ASN1_STRING_to_UTF8", true); + ASN1_STRING_type = (int (*)(const ASN1_STRING *))openssl_sym(&err, "ASN1_STRING_type", true); + + BIO_get_data = (void *(*)(BIO *))openssl_sym(&err, "BIO_get_data", false); + BIO_get_new_index = (int (*)(void))openssl_sym(&err, "BIO_get_new_index", false); + BIO_meth_free = (void (*)(BIO_METHOD *))openssl_sym(&err, "BIO_meth_free", false); + BIO_meth_new = (BIO_METHOD *(*)(int, const char *))openssl_sym(&err, "BIO_meth_new", false); + BIO_meth_set_create = (int (*)(BIO_METHOD *, int (*)(BIO *)))openssl_sym(&err, "BIO_meth_set_create", false); + BIO_meth_set_ctrl = (int (*)(BIO_METHOD *, long (*)(BIO *, int, long, void *)))openssl_sym(&err, "BIO_meth_set_ctrl", false); + BIO_meth_set_destroy = (int (*)(BIO_METHOD *, int (*)(BIO *)))openssl_sym(&err, "BIO_meth_set_destroy", false); + BIO_meth_set_gets = (int (*)(BIO_METHOD *, int (*)(BIO *, char *, int)))openssl_sym(&err, "BIO_meth_set_gets", false); + BIO_meth_set_puts = (int (*)(BIO_METHOD *, int (*)(BIO *, const char *)))openssl_sym(&err, "BIO_meth_set_puts", false); + BIO_meth_set_read = (int (*)(BIO_METHOD *, int (*)(BIO *, char *, int)))openssl_sym(&err, "BIO_meth_set_read", false); + BIO_meth_set_write = (int (*)(BIO_METHOD *, int (*)(BIO *, const char *, int)))openssl_sym(&err, "BIO_meth_set_write", false); + BIO_new = (BIO *(*)(const BIO_METHOD *))openssl_sym(&err, "BIO_new", true); + BIO_set_data = (void (*)(BIO *a, void *))openssl_sym(&err, "BIO_set_data", false); + BIO_set_init = (void (*)(BIO *a, int))openssl_sym(&err, "BIO_set_init", false); + + CRYPTO_free = (void (*)(void *, const char *, int))openssl_sym(&err, "CRYPTO_free", true); + CRYPTO_malloc = (void *(*)(size_t, const char *, int))openssl_sym(&err, "CRYPTO_malloc", true); + CRYPTO_num_locks = (int (*)(void))openssl_sym(&err, "CRYPTO_num_locks", false); + CRYPTO_set_locking_callback = (void (*)(void (*)(int, int, const char *, int)))openssl_sym(&err, "CRYPTO_set_locking_callback", false); + CRYPTO_set_mem_functions = (int (*)(void *(*)(size_t), void *(*)(void *, size_t), void (*f)(void *)))openssl_sym(&err, "CRYPTO_set_mem_functions", true); + + CRYPTO_THREADID_set_callback = (int (*)(void (*)(CRYPTO_THREADID *)))openssl_sym(&err, "CRYPTO_THREADID_set_callback", false); + CRYPTO_THREADID_set_numeric = (void (*)(CRYPTO_THREADID *, unsigned long))openssl_sym(&err, "CRYPTO_THREADID_set_numeric", false); + + ERR_error_string = (char *(*)(unsigned long, char *))openssl_sym(&err, "ERR_error_string", true); + ERR_error_string_n = (void (*)(unsigned long, char *, size_t))openssl_sym(&err, "ERR_error_string_n", true); + ERR_get_error = (unsigned long (*)(void))openssl_sym(&err, "ERR_get_error", true); + + OPENSSL_init_ssl = (int (*)(uint64_t opts, const void *settings))openssl_sym(&err, "OPENSSL_init_ssl", false); + OPENSSL_sk_num = (int (*)(const void *))openssl_sym(&err, "OPENSSL_sk_num", false); + OPENSSL_sk_value = (void *(*)(const void *sk, int i))openssl_sym(&err, "OPENSSL_sk_value", false); + OPENSSL_sk_free = (void (*)(void *))openssl_sym(&err, "OPENSSL_sk_free", false); + + sk_num = (int (*)(const void *))openssl_sym(&err, "sk_num", false); + sk_value = (void *(*)(const void *sk, int i))openssl_sym(&err, "sk_value", false); + sk_free = (void (*)(void *))openssl_sym(&err, "sk_free", false); + + SSL_connect = (int (*)(SSL *))openssl_sym(&err, "SSL_connect", true); + SSL_ctrl = (long (*)(SSL *, int, long, void *))openssl_sym(&err, "SSL_ctrl", true); + SSL_library_init = (int (*)(void))openssl_sym(&err, "SSL_library_init", false); + SSL_free = (void (*)(SSL *))openssl_sym(&err, "SSL_free", true); + SSL_get_error = (int (*)(SSL *, int))openssl_sym(&err, "SSL_get_error", true); + SSL_get_verify_result = (long (*)(const SSL *ssl))openssl_sym(&err, "SSL_get_verify_result", true); + SSL_load_error_strings = (void (*)(void))openssl_sym(&err, "SSL_load_error_strings", false); + SSL_new = (SSL *(*)(SSL_CTX *))openssl_sym(&err, "SSL_new", true); + SSL_read = (int (*)(SSL *, const void *, int))openssl_sym(&err, "SSL_read", true); + SSL_set_bio = (void (*)(SSL *, BIO *, BIO *))openssl_sym(&err, "SSL_set_bio", true); + SSL_shutdown = (int (*)(SSL *ssl))openssl_sym(&err, "SSL_shutdown", true); + SSL_write = (int (*)(SSL *, const void *, int))openssl_sym(&err, "SSL_write", true); + + if (!(SSL_get_peer_certificate = (X509 *(*)(const SSL *))openssl_sym(&err, "SSL_get_peer_certificate", false))) { + SSL_get_peer_certificate = (X509 *(*)(const SSL *))openssl_sym(&err, "SSL_get1_peer_certificate", true); + } + + SSL_CTX_ctrl = (long (*)(SSL_CTX *, int, long, void *))openssl_sym(&err, "SSL_CTX_ctrl", true); + SSL_CTX_free = (void (*)(SSL_CTX *))openssl_sym(&err, "SSL_CTX_free", true); + SSL_CTX_new = (SSL_CTX *(*)(const SSL_METHOD *))openssl_sym(&err, "SSL_CTX_new", true); + SSL_CTX_get_cert_store = (X509_STORE *(*)(const SSL_CTX *))openssl_sym(&err, "SSL_CTX_get_cert_store", true); + SSL_CTX_set_cipher_list = (int (*)(SSL_CTX *, const char *))openssl_sym(&err, "SSL_CTX_set_cipher_list", true); + SSL_CTX_set_default_verify_paths = (int (*)(SSL_CTX *ctx))openssl_sym(&err, "SSL_CTX_set_default_verify_paths", true); + SSL_CTX_set_options = (long (*)(SSL_CTX *, long))openssl_sym(&err, "SSL_CTX_set_options", false); + SSL_CTX_set_verify = (void (*)(SSL_CTX *, int, int (*)(int, X509_STORE_CTX *)))openssl_sym(&err, "SSL_CTX_set_verify", true); + SSL_CTX_load_verify_locations = (int (*)(SSL_CTX *, const char *, const char *))openssl_sym(&err, "SSL_CTX_load_verify_locations", true); + + SSLv23_method = (const SSL_METHOD *(*)(void))openssl_sym(&err, "SSLv23_method", false); + TLS_method = (const SSL_METHOD *(*)(void))openssl_sym(&err, "TLS_method", false); + + X509_NAME_ENTRY_get_data = (ASN1_STRING *(*)(const X509_NAME_ENTRY *))openssl_sym(&err, "X509_NAME_ENTRY_get_data", true); + X509_NAME_get_entry = (X509_NAME_ENTRY *(*)(X509_NAME *, int))openssl_sym(&err, "X509_NAME_get_entry", true); + X509_NAME_get_index_by_NID = (int (*)(X509_NAME *, int, int))openssl_sym(&err, "X509_NAME_get_index_by_NID", true); + X509_free = (void (*)(X509 *))openssl_sym(&err, "X509_free", true); + X509_get_ext_d2i = (void *(*)(const X509 *x, int nid, int *crit, int *idx))openssl_sym(&err, "X509_get_ext_d2i", true); + X509_get_subject_name = (X509_NAME *(*)(const X509 *))openssl_sym(&err, "X509_get_subject_name", true); + X509_STORE_add_cert = (int (*)(X509_STORE *ctx, X509 *x))openssl_sym(&err, "X509_STORE_add_cert", true); + + i2d_X509 = (int (*)(X509 *a, unsigned char **ppout))openssl_sym(&err, "i2d_X509", true); + + if (err) + goto on_error; + + /* Add legacy functionality */ + if (!OPENSSL_init_ssl) { + OPENSSL_init_ssl = OPENSSL_init_ssl__legacy; + + if (!SSL_library_init || + !SSL_load_error_strings || + !CRYPTO_num_locks || + !CRYPTO_set_locking_callback || + !CRYPTO_THREADID_set_callback || + !CRYPTO_THREADID_set_numeric) { + git_error_set(GIT_ERROR_SSL, "could not load legacy openssl initialization functions"); + goto on_error; + } + } + + if (!SSL_CTX_set_options) + SSL_CTX_set_options = SSL_CTX_set_options__legacy; + + if (TLS_method) + SSLv23_method = TLS_method; + + if (!BIO_meth_new) { + BIO_meth_new = BIO_meth_new__legacy; + BIO_meth_new = BIO_meth_new__legacy; + BIO_meth_free = BIO_meth_free__legacy; + BIO_meth_set_write = BIO_meth_set_write__legacy; + BIO_meth_set_read = BIO_meth_set_read__legacy; + BIO_meth_set_puts = BIO_meth_set_puts__legacy; + BIO_meth_set_gets = BIO_meth_set_gets__legacy; + BIO_meth_set_ctrl = BIO_meth_set_ctrl__legacy; + BIO_meth_set_create = BIO_meth_set_create__legacy; + BIO_meth_set_destroy = BIO_meth_set_destroy__legacy; + BIO_get_new_index = BIO_get_new_index__legacy; + BIO_set_data = BIO_set_data__legacy; + BIO_set_init = BIO_set_init__legacy; + BIO_get_data = BIO_get_data__legacy; + } + + if (!ASN1_STRING_get0_data) { + if (!ASN1_STRING_data) { + git_error_set(GIT_ERROR_SSL, "could not load legacy openssl string function"); + goto on_error; + } + + ASN1_STRING_get0_data = ASN1_STRING_get0_data__legacy; + } + + if ((!OPENSSL_sk_num && !sk_num) || + (!OPENSSL_sk_value && !sk_value) || + (!OPENSSL_sk_free && !sk_free)) { + git_error_set(GIT_ERROR_SSL, "could not load legacy openssl stack functions"); + goto on_error; + } + + if (git_runtime_shutdown_register(dynamic_shutdown) != 0) + goto on_error; + + return 0; + +on_error: + dlclose(openssl_handle); + return -1; +} + + +int sk_GENERAL_NAME_num(const GENERAL_NAME *sk) +{ + if (OPENSSL_sk_num) + return OPENSSL_sk_num(sk); + else if (sk_num) + return sk_num(sk); + + GIT_ASSERT_WITH_RETVAL(false, 0); + return 0; +} + +GENERAL_NAME *sk_GENERAL_NAME_value(const GENERAL_NAME *sk, int i) +{ + if (OPENSSL_sk_value) + return OPENSSL_sk_value(sk, i); + else if (sk_value) + return sk_value(sk, i); + + GIT_ASSERT_WITH_RETVAL(false, NULL); + return NULL; +} + +void GENERAL_NAMES_free(GENERAL_NAME *sk) +{ + if (OPENSSL_sk_free) + OPENSSL_sk_free(sk); + else if (sk_free) + sk_free(sk); +} + +#endif /* GIT_HTTPS_OPENSSL_DYNAMIC */ diff --git a/src/libgit2/streams/openssl_dynamic.h b/src/libgit2/streams/openssl_dynamic.h new file mode 100644 index 00000000000..ca97c54068d --- /dev/null +++ b/src/libgit2/streams/openssl_dynamic.h @@ -0,0 +1,214 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_streams_openssl_dynamic_h__ +#define INCLUDE_streams_openssl_dynamic_h__ + +#ifdef GIT_HTTPS_OPENSSL_DYNAMIC + +/* These constants are taken from the OpenSSL 3.0 headers. */ + +# define BIO_CTRL_FLUSH 11 + +# define BIO_TYPE_SOURCE_SINK 0x0400 + +# define CRYPTO_LOCK 1 + +# define GEN_DNS 2 +# define GEN_IPADD 7 + +# define NID_commonName 13 +# define NID_subject_alt_name 85 + +# define SSL_VERIFY_NONE 0x00 + +# define SSL_CTRL_OPTIONS 32 +# define SSL_CTRL_MODE 33 +# define SSL_CTRL_SET_TLSEXT_HOSTNAME 55 + +# define SSL_ERROR_NONE 0 +# define SSL_ERROR_SSL 1 +# define SSL_ERROR_WANT_READ 2 +# define SSL_ERROR_WANT_WRITE 3 +# define SSL_ERROR_WANT_X509_LOOKUP 4 +# define SSL_ERROR_SYSCALL 5 +# define SSL_ERROR_ZERO_RETURN 6 +# define SSL_ERROR_WANT_CONNECT 7 +# define SSL_ERROR_WANT_ACCEPT 8 + +# define SSL_OP_NO_COMPRESSION 0x00020000L +# define SSL_OP_NO_SSLv2 0x01000000L +# define SSL_OP_NO_SSLv3 0x02000000L +# define SSL_OP_NO_TLSv1 0x04000000L +# define SSL_OP_NO_TLSv1_1 0x10000000L + +# define SSL_MODE_AUTO_RETRY 0x00000004L + +# define TLSEXT_NAMETYPE_host_name 0 + +# define V_ASN1_UTF8STRING 12 + +# define X509_V_OK 0 + +/* Most of the OpenSSL types are mercifully opaque, so we can treat them like `void *` */ +typedef struct bio_st BIO; +typedef struct bio_method_st BIO_METHOD; +typedef void bio_info_cb; +typedef void * CRYPTO_EX_DATA; +typedef void CRYPTO_THREADID; +typedef void GENERAL_NAMES; +typedef void SSL; +typedef void SSL_CTX; +typedef void SSL_METHOD; +typedef void X509; +typedef void X509_NAME; +typedef void X509_NAME_ENTRY; +typedef void X509_STORE; +typedef void X509_STORE_CTX; + +typedef struct { + int length; + int type; + unsigned char *data; + long flags; +} ASN1_STRING; + +typedef struct { + int type; + union { + char *ptr; + ASN1_STRING *ia5; + } d; +} GENERAL_NAME; + +struct bio_st { + BIO_METHOD *method; + /* bio, mode, argp, argi, argl, ret */ + long (*callback) (struct bio_st *, int, const char *, int, long, long); + char *cb_arg; /* first argument for the callback */ + int init; + int shutdown; + int flags; /* extra storage */ + int retry_reason; + int num; + void *ptr; + struct bio_st *next_bio; /* used by filter BIOs */ + struct bio_st *prev_bio; /* used by filter BIOs */ + int references; + unsigned long num_read; + unsigned long num_write; + CRYPTO_EX_DATA ex_data; +}; + +struct bio_method_st { + int type; + const char *name; + int (*bwrite) (BIO *, const char *, int); + int (*bread) (BIO *, char *, int); + int (*bputs) (BIO *, const char *); + int (*bgets) (BIO *, char *, int); + long (*ctrl) (BIO *, int, long, void *); + int (*create) (BIO *); + int (*destroy) (BIO *); + long (*callback_ctrl) (BIO *, int, bio_info_cb *); +}; + +extern unsigned char *(*ASN1_STRING_data)(ASN1_STRING *x); +extern const unsigned char *(*ASN1_STRING_get0_data)(const ASN1_STRING *x); +extern int (*ASN1_STRING_length)(const ASN1_STRING *x); +extern int (*ASN1_STRING_to_UTF8)(unsigned char **out, const ASN1_STRING *in); +extern int (*ASN1_STRING_type)(const ASN1_STRING *x); + +extern void *(*BIO_get_data)(BIO *a); +extern int (*BIO_get_new_index)(void); +extern int (*OPENSSL_init_ssl)(uint64_t opts, const void *settings); +extern void (*BIO_meth_free)(BIO_METHOD *biom); +extern int (*BIO_meth_set_create)(BIO_METHOD *biom, int (*create) (BIO *)); +extern int (*BIO_meth_set_ctrl)(BIO_METHOD *biom, long (*ctrl) (BIO *, int, long, void *)); +extern int (*BIO_meth_set_destroy)(BIO_METHOD *biom, int (*destroy) (BIO *)); +extern int (*BIO_meth_set_gets)(BIO_METHOD *biom, int (*gets) (BIO *, char *, int)); +extern int (*BIO_meth_set_puts)(BIO_METHOD *biom, int (*puts) (BIO *, const char *)); +extern int (*BIO_meth_set_read)(BIO_METHOD *biom, int (*read) (BIO *, char *, int)); +extern int (*BIO_meth_set_write)(BIO_METHOD *biom, int (*write) (BIO *, const char *, int)); +extern BIO_METHOD *(*BIO_meth_new)(int type, const char *name); +extern BIO *(*BIO_new)(const BIO_METHOD *type); +extern void (*BIO_set_data)(BIO *a, void *ptr); +extern void (*BIO_set_init)(BIO *a, int init); + +extern void (*CRYPTO_free)(void *ptr, const char *file, int line); +extern void *(*CRYPTO_malloc)(size_t num, const char *file, int line); +extern int (*CRYPTO_num_locks)(void); +extern void (*CRYPTO_set_locking_callback)(void (*func)(int mode, int type, const char *file, int line)); +extern int (*CRYPTO_set_mem_functions)(void *(*m)(size_t bytes), void *(*r)(void *mem, size_t size), void (*f)(void *mem)); +extern int (*CRYPTO_THREADID_set_callback)(void (*func)(CRYPTO_THREADID *id)); +extern void (*CRYPTO_THREADID_set_numeric)(CRYPTO_THREADID *id, unsigned long val); + +extern char *(*ERR_error_string)(unsigned long e, char *buf); +extern void (*ERR_error_string_n)(unsigned long e, char *buf, size_t len); +extern unsigned long (*ERR_get_error)(void); + +# define OPENSSL_malloc(num) CRYPTO_malloc(num, __FILE__, __LINE__) +# define OPENSSL_free(addr) CRYPTO_free(addr, __FILE__, __LINE__) + +extern int (*SSL_connect)(SSL *ssl); +extern long (*SSL_ctrl)(SSL *ssl, int cmd, long arg, void *parg); +extern void (*SSL_free)(SSL *ssl); +extern int (*SSL_get_error)(SSL *ssl, int ret); +extern X509 *(*SSL_get_peer_certificate)(const SSL *ssl); +extern long (*SSL_get_verify_result)(const SSL *ssl); +extern int (*SSL_library_init)(void); +extern void (*SSL_load_error_strings)(void); +extern SSL *(*SSL_new)(SSL_CTX *ctx); +extern int (*SSL_read)(SSL *ssl, const void *buf, int num); +extern void (*SSL_set_bio)(SSL *ssl, BIO *rbio, BIO *wbio); +extern int (*SSL_shutdown)(SSL *ssl); +extern int (*SSL_write)(SSL *ssl, const void *buf, int num); + +# define SSL_set_tlsext_host_name(s, name) SSL_ctrl((s), SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, (char *)(name)); + +extern long (*SSL_CTX_ctrl)(SSL_CTX *ctx, int cmd, long larg, void *parg); +extern void (*SSL_CTX_free)(SSL_CTX *ctx); +extern SSL_CTX *(*SSL_CTX_new)(const SSL_METHOD *method); +extern X509_STORE *(*SSL_CTX_get_cert_store)(const SSL_CTX *ctx); +extern int (*SSL_CTX_set_cipher_list)(SSL_CTX *ctx, const char *str); +extern int (*SSL_CTX_set_default_verify_paths)(SSL_CTX *ctx); +extern long (*SSL_CTX_set_options)(SSL_CTX *ctx, long options); +extern void (*SSL_CTX_set_verify)(SSL_CTX *ctx, int mode, int (*verify_callback)(int, X509_STORE_CTX *)); +extern int (*SSL_CTX_load_verify_locations)(SSL_CTX *ctx, const char *CAfile, const char *CApath); + +# define SSL_CTX_set_mode(ctx, mode) SSL_CTX_ctrl((ctx), SSL_CTRL_MODE, (mode), NULL); + +extern const SSL_METHOD *(*SSLv23_method)(void); +extern const SSL_METHOD *(*TLS_method)(void); + +extern ASN1_STRING *(*X509_NAME_ENTRY_get_data)(const X509_NAME_ENTRY *ne); +extern X509_NAME_ENTRY *(*X509_NAME_get_entry)(X509_NAME *name, int loc); +extern int (*X509_NAME_get_index_by_NID)(X509_NAME *name, int nid, int lastpos); +extern void (*X509_free)(X509 *a); +extern void *(*X509_get_ext_d2i)(const X509 *x, int nid, int *crit, int *idx); +extern X509_NAME *(*X509_get_subject_name)(const X509 *x); +extern int (*X509_STORE_add_cert)(X509_STORE *ctx, X509 *x); + +extern int (*i2d_X509)(X509 *a, unsigned char **ppout); + +extern int (*OPENSSL_sk_num)(const void *sk); +extern void *(*OPENSSL_sk_value)(const void *sk, int i); +extern void (*OPENSSL_sk_free)(void *sk); + +extern int (*sk_num)(const void *sk); +extern void *(*sk_value)(const void *sk, int i); +extern void (*sk_free)(void *sk); + +extern int sk_GENERAL_NAME_num(const GENERAL_NAME *sk); +extern GENERAL_NAME *sk_GENERAL_NAME_value(const GENERAL_NAME *sk, int i); +extern void GENERAL_NAMES_free(GENERAL_NAME *sk); + +extern int git_openssl_stream_dynamic_init(void); + +#endif /* GIT_HTTPS_OPENSSL_DYNAMIC */ + +#endif diff --git a/src/libgit2/streams/openssl_legacy.c b/src/libgit2/streams/openssl_legacy.c new file mode 100644 index 00000000000..7d361263f49 --- /dev/null +++ b/src/libgit2/streams/openssl_legacy.c @@ -0,0 +1,203 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "streams/openssl.h" +#include "streams/openssl_legacy.h" + +#include "runtime.h" +#include "git2/sys/openssl.h" + +#if defined(GIT_HTTPS_OPENSSL) && !defined(GIT_HTTPS_OPENSSL_DYNAMIC) +# include +# include +# include +# include +#endif + +#if defined(GIT_HTTPS_OPENSSL_LEGACY) || defined(GIT_HTTPS_OPENSSL_DYNAMIC) + +/* + * OpenSSL 1.1 made BIO opaque so we have to use functions to interact with it + * which do not exist in previous versions. We define these inline functions so + * we can program against the interface instead of littering the implementation + * with ifdefs. We do the same for OPENSSL_init_ssl. + */ + +int OPENSSL_init_ssl__legacy(uint64_t opts, const void *settings) +{ + GIT_UNUSED(opts); + GIT_UNUSED(settings); + SSL_load_error_strings(); + SSL_library_init(); + return 0; +} + +BIO_METHOD *BIO_meth_new__legacy(int type, const char *name) +{ + BIO_METHOD *meth = git__calloc(1, sizeof(BIO_METHOD)); + if (!meth) { + return NULL; + } + + meth->type = type; + meth->name = name; + + return meth; +} + +void BIO_meth_free__legacy(BIO_METHOD *biom) +{ + git__free(biom); +} + +int BIO_meth_set_write__legacy(BIO_METHOD *biom, int (*write) (BIO *, const char *, int)) +{ + biom->bwrite = write; + return 1; +} + +int BIO_meth_set_read__legacy(BIO_METHOD *biom, int (*read) (BIO *, char *, int)) +{ + biom->bread = read; + return 1; +} + +int BIO_meth_set_puts__legacy(BIO_METHOD *biom, int (*puts) (BIO *, const char *)) +{ + biom->bputs = puts; + return 1; +} + +int BIO_meth_set_gets__legacy(BIO_METHOD *biom, int (*gets) (BIO *, char *, int)) + +{ + biom->bgets = gets; + return 1; +} + +int BIO_meth_set_ctrl__legacy(BIO_METHOD *biom, long (*ctrl) (BIO *, int, long, void *)) +{ + biom->ctrl = ctrl; + return 1; +} + +int BIO_meth_set_create__legacy(BIO_METHOD *biom, int (*create) (BIO *)) +{ + biom->create = create; + return 1; +} + +int BIO_meth_set_destroy__legacy(BIO_METHOD *biom, int (*destroy) (BIO *)) +{ + biom->destroy = destroy; + return 1; +} + +int BIO_get_new_index__legacy(void) +{ + /* This exists as of 1.1 so before we'd just have 0 */ + return 0; +} + +void BIO_set_init__legacy(BIO *b, int init) +{ + b->init = init; +} + +void BIO_set_data__legacy(BIO *a, void *ptr) +{ + a->ptr = ptr; +} + +void *BIO_get_data__legacy(BIO *a) +{ + return a->ptr; +} + +const unsigned char *ASN1_STRING_get0_data__legacy(const ASN1_STRING *x) +{ + return ASN1_STRING_data((ASN1_STRING *)x); +} + +long SSL_CTX_set_options__legacy(SSL_CTX *ctx, long op) +{ + return SSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, op, NULL); +} + +# if defined(GIT_THREADS) +static git_mutex *openssl_locks; + +static void openssl_locking_function(int mode, int n, const char *file, int line) +{ + int lock; + + GIT_UNUSED(file); + GIT_UNUSED(line); + + lock = mode & CRYPTO_LOCK; + + if (lock) + (void)git_mutex_lock(&openssl_locks[n]); + else + git_mutex_unlock(&openssl_locks[n]); +} + +static void shutdown_ssl_locking(void) +{ + int num_locks, i; + + num_locks = CRYPTO_num_locks(); + CRYPTO_set_locking_callback(NULL); + + for (i = 0; i < num_locks; ++i) + git_mutex_free(&openssl_locks[i]); + git__free(openssl_locks); +} + +static void threadid_cb(CRYPTO_THREADID *threadid) +{ + GIT_UNUSED(threadid); + CRYPTO_THREADID_set_numeric(threadid, git_thread_currentid()); +} + +int git_openssl_set_locking(void) +{ + int num_locks, i; + +#ifndef GIT_THREADS + git_error_set(GIT_ERROR_THREAD, "libgit2 was not built with threads"); + return -1; +#endif + +#ifdef GIT_HTTPS_OPENSSL_DYNAMIC + /* + * This function is required on legacy versions of OpenSSL; when building + * with dynamically-loaded OpenSSL, we detect whether we loaded it or not. + */ + if (!CRYPTO_set_locking_callback) + return 0; +#endif + + CRYPTO_THREADID_set_callback(threadid_cb); + + num_locks = CRYPTO_num_locks(); + openssl_locks = git__calloc(num_locks, sizeof(git_mutex)); + GIT_ERROR_CHECK_ALLOC(openssl_locks); + + for (i = 0; i < num_locks; i++) { + if (git_mutex_init(&openssl_locks[i]) != 0) { + git_error_set(GIT_ERROR_SSL, "failed to initialize openssl locks"); + return -1; + } + } + + CRYPTO_set_locking_callback(openssl_locking_function); + return git_runtime_shutdown_register(shutdown_ssl_locking); +} +#endif /* GIT_THREADS */ + +#endif /* GIT_HTTPS_OPENSSL_LEGACY || GIT_HTTPS_OPENSSL_DYNAMIC */ diff --git a/src/libgit2/streams/openssl_legacy.h b/src/libgit2/streams/openssl_legacy.h new file mode 100644 index 00000000000..205c984adcc --- /dev/null +++ b/src/libgit2/streams/openssl_legacy.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_streams_openssl_legacy_h__ +#define INCLUDE_streams_openssl_legacy_h__ + +#include "streams/openssl_dynamic.h" + +#if defined(GIT_HTTPS_OPENSSL) && !defined(GIT_HTTPS_OPENSSL_DYNAMIC) +# include +# include +# include +# include + +# if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L) || \ + (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L) +# define GIT_HTTPS_OPENSSL_LEGACY +# endif +#endif + +#if defined(GIT_HTTPS_OPENSSL_LEGACY) && !defined(GIT_HTTPS_OPENSSL_DYNAMIC) +# define OPENSSL_init_ssl OPENSSL_init_ssl__legacy +# define BIO_meth_new BIO_meth_new__legacy +# define BIO_meth_free BIO_meth_free__legacy +# define BIO_meth_set_write BIO_meth_set_write__legacy +# define BIO_meth_set_read BIO_meth_set_read__legacy +# define BIO_meth_set_puts BIO_meth_set_puts__legacy +# define BIO_meth_set_gets BIO_meth_set_gets__legacy +# define BIO_meth_set_ctrl BIO_meth_set_ctrl__legacy +# define BIO_meth_set_create BIO_meth_set_create__legacy +# define BIO_meth_set_destroy BIO_meth_set_destroy__legacy +# define BIO_get_new_index BIO_get_new_index__legacy +# define BIO_set_data BIO_set_data__legacy +# define BIO_set_init BIO_set_init__legacy +# define BIO_get_data BIO_get_data__legacy +# define ASN1_STRING_get0_data ASN1_STRING_get0_data__legacy +#endif + +#if defined(GIT_HTTPS_OPENSSL_LEGACY) || defined(GIT_HTTPS_OPENSSL_DYNAMIC) + +extern int OPENSSL_init_ssl__legacy(uint64_t opts, const void *settings); +extern BIO_METHOD *BIO_meth_new__legacy(int type, const char *name); +extern void BIO_meth_free__legacy(BIO_METHOD *biom); +extern int BIO_meth_set_write__legacy(BIO_METHOD *biom, int (*write) (BIO *, const char *, int)); +extern int BIO_meth_set_read__legacy(BIO_METHOD *biom, int (*read) (BIO *, char *, int)); +extern int BIO_meth_set_puts__legacy(BIO_METHOD *biom, int (*puts) (BIO *, const char *)); +extern int BIO_meth_set_gets__legacy(BIO_METHOD *biom, int (*gets) (BIO *, char *, int)); +extern int BIO_meth_set_ctrl__legacy(BIO_METHOD *biom, long (*ctrl) (BIO *, int, long, void *)); +extern int BIO_meth_set_create__legacy(BIO_METHOD *biom, int (*create) (BIO *)); +extern int BIO_meth_set_destroy__legacy(BIO_METHOD *biom, int (*destroy) (BIO *)); +extern int BIO_get_new_index__legacy(void); +extern void BIO_set_data__legacy(BIO *a, void *ptr); +extern void BIO_set_init__legacy(BIO *b, int init); +extern void *BIO_get_data__legacy(BIO *a); +extern const unsigned char *ASN1_STRING_get0_data__legacy(const ASN1_STRING *x); +extern long SSL_CTX_set_options__legacy(SSL_CTX *ctx, long op); + +#endif + +#endif diff --git a/src/libgit2/streams/registry.c b/src/libgit2/streams/registry.c new file mode 100644 index 00000000000..e60e1cd63e8 --- /dev/null +++ b/src/libgit2/streams/registry.c @@ -0,0 +1,119 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "streams/registry.h" + +#include "runtime.h" +#include "streams/tls.h" +#include "streams/mbedtls.h" +#include "streams/openssl.h" +#include "streams/stransport.h" + +struct stream_registry { + git_rwlock lock; + git_stream_registration callbacks; + git_stream_registration tls_callbacks; +}; + +static struct stream_registry stream_registry; + +static void shutdown_stream_registry(void) +{ + git_rwlock_free(&stream_registry.lock); +} + +int git_stream_registry_global_init(void) +{ + if (git_rwlock_init(&stream_registry.lock) < 0) + return -1; + + return git_runtime_shutdown_register(shutdown_stream_registry); +} + +GIT_INLINE(void) stream_registration_cpy( + git_stream_registration *target, + git_stream_registration *src) +{ + if (src) + memcpy(target, src, sizeof(git_stream_registration)); + else + memset(target, 0, sizeof(git_stream_registration)); +} + +int git_stream_registry_lookup(git_stream_registration *out, git_stream_t type) +{ + git_stream_registration *target; + int error = GIT_ENOTFOUND; + + GIT_ASSERT_ARG(out); + + switch(type) { + case GIT_STREAM_STANDARD: + target = &stream_registry.callbacks; + break; + case GIT_STREAM_TLS: + target = &stream_registry.tls_callbacks; + break; + default: + git_error_set(GIT_ERROR_INVALID, "invalid stream type"); + return -1; + } + + if (git_rwlock_rdlock(&stream_registry.lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock stream registry"); + return -1; + } + + if (target->init) { + stream_registration_cpy(out, target); + error = 0; + } + + git_rwlock_rdunlock(&stream_registry.lock); + return error; +} + +int git_stream_register(git_stream_t type, git_stream_registration *registration) +{ + GIT_ASSERT(!registration || registration->init); + + GIT_ERROR_CHECK_VERSION(registration, GIT_STREAM_VERSION, "stream_registration"); + + if (git_rwlock_wrlock(&stream_registry.lock) < 0) { + git_error_set(GIT_ERROR_OS, "failed to lock stream registry"); + return -1; + } + + if ((type & GIT_STREAM_STANDARD) == GIT_STREAM_STANDARD) + stream_registration_cpy(&stream_registry.callbacks, registration); + + if ((type & GIT_STREAM_TLS) == GIT_STREAM_TLS) + stream_registration_cpy(&stream_registry.tls_callbacks, registration); + + git_rwlock_wrunlock(&stream_registry.lock); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_stream_register_tls( + int GIT_CALLBACK(ctor)(git_stream **out, const char *host, const char *port)) +{ + git_stream_registration registration = {0}; + + if (ctor) { + registration.version = GIT_STREAM_VERSION; + registration.init = ctor; + registration.wrap = NULL; + + return git_stream_register(GIT_STREAM_TLS, ®istration); + } else { + return git_stream_register(GIT_STREAM_TLS, NULL); + } +} +#endif diff --git a/src/libgit2/streams/registry.h b/src/libgit2/streams/registry.h new file mode 100644 index 00000000000..adc2b8bdf36 --- /dev/null +++ b/src/libgit2/streams/registry.h @@ -0,0 +1,19 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_streams_registry_h__ +#define INCLUDE_streams_registry_h__ + +#include "common.h" +#include "git2/sys/stream.h" + +/** Configure stream registry. */ +int git_stream_registry_global_init(void); + +/** Lookup a stream registration. */ +extern int git_stream_registry_lookup(git_stream_registration *out, git_stream_t type); + +#endif diff --git a/src/libgit2/streams/schannel.c b/src/libgit2/streams/schannel.c new file mode 100644 index 00000000000..062758a2587 --- /dev/null +++ b/src/libgit2/streams/schannel.c @@ -0,0 +1,715 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "streams/schannel.h" + +#ifdef GIT_HTTPS_SCHANNEL + +#define SECURITY_WIN32 + +#include +#include +#include + +#include "stream.h" +#include "streams/socket.h" + +#ifndef SP_PROT_TLS1_2_CLIENT +# define SP_PROT_TLS1_2_CLIENT 2048 +#endif + +#ifndef SP_PROT_TLS1_3_CLIENT +# define SP_PROT_TLS1_3_CLIENT 8192 +#endif + +#ifndef SECBUFFER_ALERT +# define SECBUFFER_ALERT 17 +#endif + +#define READ_BLOCKSIZE (16 * 1024) + +typedef enum { + STATE_NONE = 0, + STATE_CRED = 1, + STATE_CONTEXT = 2, + STATE_CERTIFICATE = 3 +} schannel_state; + +typedef struct { + git_stream parent; + git_stream *io; + int owned; + bool connected; + wchar_t *host_w; + + schannel_state state; + + CredHandle cred; + CtxtHandle context; + SecPkgContext_StreamSizes stream_sizes; + + CERT_CONTEXT *certificate; + const CERT_CHAIN_CONTEXT *cert_chain; + git_cert_x509 x509; + + git_str plaintext_in; + git_str ciphertext_in; +} schannel_stream; + +static int connect_context(schannel_stream *st) +{ + SCHANNEL_CRED cred = { 0 }; + SECURITY_STATUS status = SEC_E_INTERNAL_ERROR; + DWORD context_flags; + static size_t MAX_RETRIES = 1024; + size_t retries; + ssize_t read_len; + int error = 0; + + if (st->owned && (error = git_stream_connect(st->io)) < 0) + return error; + + cred.dwVersion = SCHANNEL_CRED_VERSION; + cred.dwFlags = SCH_CRED_IGNORE_NO_REVOCATION_CHECK | + SCH_CRED_IGNORE_REVOCATION_OFFLINE | + SCH_CRED_MANUAL_CRED_VALIDATION | + SCH_CRED_NO_DEFAULT_CREDS | + SCH_CRED_NO_SERVERNAME_CHECK; + cred.grbitEnabledProtocols = SP_PROT_TLS1_2_CLIENT | + SP_PROT_TLS1_3_CLIENT; + + if (AcquireCredentialsHandleW(NULL, SCHANNEL_NAME_W, + SECPKG_CRED_OUTBOUND, NULL, &cred, NULL, + NULL, &st->cred, NULL) != SEC_E_OK) { + git_error_set(GIT_ERROR_OS, "could not acquire credentials handle"); + return -1; + } + + st->state = STATE_CRED; + + context_flags = ISC_REQ_ALLOCATE_MEMORY | + ISC_REQ_CONFIDENTIALITY | + ISC_REQ_REPLAY_DETECT | + ISC_REQ_SEQUENCE_DETECT | + ISC_REQ_STREAM; + + for (retries = 0; retries < MAX_RETRIES; retries++) { + SecBuffer input_buf[] = { + { (unsigned long)st->ciphertext_in.size, + SECBUFFER_TOKEN, + st->ciphertext_in.size ? st->ciphertext_in.ptr : NULL }, + { 0, SECBUFFER_EMPTY, NULL } + }; + SecBuffer output_buf[] = { { 0, SECBUFFER_TOKEN, NULL }, + { 0, SECBUFFER_ALERT, NULL } }; + + SecBufferDesc input_buf_desc = { SECBUFFER_VERSION, 2, input_buf }; + SecBufferDesc output_buf_desc = { SECBUFFER_VERSION, 2, output_buf }; + + status = InitializeSecurityContextW(&st->cred, + retries ? &st->context : NULL, st->host_w, + context_flags, 0, 0, retries ? &input_buf_desc : NULL, 0, + retries ? NULL : &st->context, &output_buf_desc, + &context_flags, NULL); + + if (status == SEC_E_OK || status == SEC_I_CONTINUE_NEEDED) { + st->state = STATE_CONTEXT; + + if (output_buf[0].cbBuffer > 0) { + error = git_stream__write_full(st->io, + output_buf[0].pvBuffer, + output_buf[0].cbBuffer, 0); + + FreeContextBuffer(output_buf[0].pvBuffer); + } + + /* handle any leftover, unprocessed data */ + if (input_buf[1].BufferType == SECBUFFER_EXTRA) { + GIT_ASSERT(st->ciphertext_in.size > input_buf[1].cbBuffer); + + git_str_consume_bytes(&st->ciphertext_in, + st->ciphertext_in.size - input_buf[1].cbBuffer); + } else { + git_str_clear(&st->ciphertext_in); + } + + if (error < 0 || status == SEC_E_OK) + break; + } else if (status == SEC_E_INCOMPLETE_MESSAGE) { + /* we need additional data from the client; */ + if (git_str_grow_by(&st->ciphertext_in, READ_BLOCKSIZE) < 0) { + error = -1; + break; + } + + if ((read_len = git_stream_read(st->io, + st->ciphertext_in.ptr + st->ciphertext_in.size, + (st->ciphertext_in.asize - st->ciphertext_in.size))) < 0) { + error = -1; + break; + } + + GIT_ASSERT((size_t)read_len <= + st->ciphertext_in.asize - st->ciphertext_in.size); + st->ciphertext_in.size += read_len; + } else { + git_error_set(GIT_ERROR_OS, + "could not initialize security context"); + error = -1; + break; + } + + GIT_ASSERT(st->ciphertext_in.size < ULONG_MAX); + } + + if (retries == MAX_RETRIES) { + git_error_set(GIT_ERROR_SSL, + "could not initialize security context: too many retries"); + error = -1; + } + + if (!error) { + if (QueryContextAttributesW(&st->context, + SECPKG_ATTR_STREAM_SIZES, + &st->stream_sizes) != SEC_E_OK) { + git_error_set(GIT_ERROR_SSL, + "could not query stream sizes"); + error = -1; + } + } + + return error; +} + +static int set_certificate_lookup_error(DWORD status) +{ + switch (status) { + case CERT_TRUST_IS_NOT_TIME_VALID: + git_error_set(GIT_ERROR_SSL, + "certificate is expired or not yet valid"); + break; + case CERT_TRUST_IS_REVOKED: + git_error_set(GIT_ERROR_SSL, "certificate is revoked"); + break; + case CERT_TRUST_IS_NOT_SIGNATURE_VALID: + case CERT_TRUST_IS_NOT_VALID_FOR_USAGE: + case CERT_TRUST_INVALID_EXTENSION: + case CERT_TRUST_INVALID_POLICY_CONSTRAINTS: + case CERT_TRUST_INVALID_BASIC_CONSTRAINTS: + case CERT_TRUST_INVALID_NAME_CONSTRAINTS: + case CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT: + case CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT: + case CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT: + case CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT: + case CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY: + case CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT: + git_error_set(GIT_ERROR_SSL, "certificate is not valid"); + break; + case CERT_TRUST_IS_UNTRUSTED_ROOT: + case CERT_TRUST_IS_CYCLIC: + case CERT_TRUST_IS_EXPLICIT_DISTRUST: + git_error_set(GIT_ERROR_SSL, "certificate is not trusted"); + break; + case CERT_TRUST_REVOCATION_STATUS_UNKNOWN: + git_error_set(GIT_ERROR_SSL, + "certificate revocation status could not be verified"); + break; + case CERT_TRUST_IS_OFFLINE_REVOCATION: + git_error_set(GIT_ERROR_SSL, + "certificate revocation is offline or stale"); + break; + case CERT_TRUST_HAS_WEAK_SIGNATURE: + git_error_set(GIT_ERROR_SSL, "certificate has a weak signature"); + break; + default: + git_error_set(GIT_ERROR_SSL, + "unknown certificate lookup failure: %d", status); + return -1; + } + + return GIT_ECERTIFICATE; +} + +static int set_certificate_validation_error(DWORD status) +{ + switch (status) { + case TRUST_E_CERT_SIGNATURE: + git_error_set(GIT_ERROR_SSL, + "the certificate cannot be verified"); + break; + case CRYPT_E_REVOKED: + git_error_set(GIT_ERROR_SSL, + "the certificate or signature has been revoked"); + break; + case CERT_E_UNTRUSTEDROOT: + git_error_set(GIT_ERROR_SSL, + "the certificate root is not trusted"); + break; + case CERT_E_UNTRUSTEDTESTROOT: + git_error_set(GIT_ERROR_SSL, + "the certificate root is a test certificate"); + break; + case CERT_E_CHAINING: + git_error_set(GIT_ERROR_SSL, + "the certificate chain is invalid"); + break; + case CERT_E_WRONG_USAGE: + case CERT_E_PURPOSE: + git_error_set(GIT_ERROR_SSL, + "the certificate is not valid for this usage"); + break; + case CERT_E_EXPIRED: + git_error_set(GIT_ERROR_SSL, + "certificate is expired or not yet valid"); + break; + case CERT_E_INVALID_NAME: + case CERT_E_CN_NO_MATCH: + git_error_set(GIT_ERROR_SSL, + "certificate is not valid for this hostname"); + break; + case CERT_E_INVALID_POLICY: + case TRUST_E_BASIC_CONSTRAINTS: + case CERT_E_CRITICAL: + case CERT_E_VALIDITYPERIODNESTING: + git_error_set(GIT_ERROR_SSL, "certificate is not valid"); + break; + case CRYPT_E_NO_REVOCATION_CHECK: + git_error_set(GIT_ERROR_SSL, + "certificate revocation status could not be verified"); + break; + case CRYPT_E_REVOCATION_OFFLINE: + git_error_set(GIT_ERROR_SSL, + "certificate revocation is offline or stale"); + break; + case CERT_E_ROLE: + git_error_set(GIT_ERROR_SSL, "certificate authority is not valid"); + break; + default: + git_error_set(GIT_ERROR_SSL, + "unknown certificate policy checking failure: %d", + status); + return -1; + } + + return GIT_ECERTIFICATE; +} + +static int check_certificate(schannel_stream* st) +{ + CERT_CHAIN_PARA cert_chain_parameters; + SSL_EXTRA_CERT_CHAIN_POLICY_PARA ssl_policy_parameters; + CERT_CHAIN_POLICY_PARA cert_policy_parameters = + { sizeof(CERT_CHAIN_POLICY_PARA), 0, &ssl_policy_parameters }; + CERT_CHAIN_POLICY_STATUS cert_policy_status; + + memset(&cert_chain_parameters, 0, sizeof(CERT_CHAIN_PARA)); + cert_chain_parameters.cbSize = sizeof(CERT_CHAIN_PARA); + + if (QueryContextAttributesW(&st->context, + SECPKG_ATTR_REMOTE_CERT_CONTEXT, + &st->certificate) != SEC_E_OK) { + git_error_set(GIT_ERROR_OS, + "could not query remote certificate context"); + return -1; + } + + /* TODO: do we really want to do revokcation checking ? */ + if (!CertGetCertificateChain(NULL, st->certificate, NULL, + st->certificate->hCertStore, &cert_chain_parameters, + CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT, + NULL, &st->cert_chain)) { + git_error_set(GIT_ERROR_OS, "could not query remote certificate chain"); + CertFreeCertificateContext(st->certificate); + return -1; + } + + st->state = STATE_CERTIFICATE; + + /* Set up the x509 certificate data for future callbacks */ + + st->x509.parent.cert_type = GIT_CERT_X509; + st->x509.data = st->certificate->pbCertEncoded; + st->x509.len = st->certificate->cbCertEncoded; + + /* Handle initial certificate validation */ + + if (st->cert_chain->TrustStatus.dwErrorStatus != CERT_TRUST_NO_ERROR) + return set_certificate_lookup_error(st->cert_chain->TrustStatus.dwErrorStatus); + + ssl_policy_parameters.cbSize = sizeof(SSL_EXTRA_CERT_CHAIN_POLICY_PARA); + ssl_policy_parameters.dwAuthType = AUTHTYPE_SERVER; + ssl_policy_parameters.pwszServerName = st->host_w; + + if (!CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL, + st->cert_chain, &cert_policy_parameters, + &cert_policy_status)) { + git_error_set(GIT_ERROR_OS, "could not verify certificate chain policy"); + return -1; + } + + if (cert_policy_status.dwError != SEC_E_OK) + return set_certificate_validation_error(cert_policy_status.dwError); + + return 0; +} + +static int schannel_connect(git_stream *stream) +{ + schannel_stream *st = (schannel_stream *)stream; + int error; + + GIT_ASSERT(st->state == STATE_NONE); + + if ((error = connect_context(st)) < 0 || + (error = check_certificate(st)) < 0) + return error; + + st->connected = 1; + return 0; +} + +static int schannel_certificate(git_cert **out, git_stream *stream) +{ + schannel_stream *st = (schannel_stream *)stream; + + *out = &st->x509.parent; + return 0; +} + +static int schannel_set_proxy( + git_stream *stream, + const git_proxy_options *proxy_options) +{ + schannel_stream *st = (schannel_stream *)stream; + return git_stream_set_proxy(st->io, proxy_options); +} + +static ssize_t schannel_write( + git_stream *stream, + const char *data, + size_t data_len, + int flags) +{ + schannel_stream *st = (schannel_stream *)stream; + SecBuffer encrypt_buf[3]; + SecBufferDesc encrypt_buf_desc = { SECBUFFER_VERSION, 3, encrypt_buf }; + git_str ciphertext_out = GIT_STR_INIT; + ssize_t total_len = 0; + + GIT_UNUSED(flags); + + if (data_len > SSIZE_MAX) + data_len = SSIZE_MAX; + + git_str_init(&ciphertext_out, + st->stream_sizes.cbHeader + + st->stream_sizes.cbMaximumMessage + + st->stream_sizes.cbTrailer); + + while (data_len > 0) { + size_t message_len = min(data_len, st->stream_sizes.cbMaximumMessage); + size_t ciphertext_len, ciphertext_written = 0; + + encrypt_buf[0].BufferType = SECBUFFER_STREAM_HEADER; + encrypt_buf[0].cbBuffer = st->stream_sizes.cbHeader; + encrypt_buf[0].pvBuffer = ciphertext_out.ptr; + + encrypt_buf[1].BufferType = SECBUFFER_DATA; + encrypt_buf[1].cbBuffer = (unsigned long)message_len; + encrypt_buf[1].pvBuffer = + ciphertext_out.ptr + st->stream_sizes.cbHeader; + + encrypt_buf[2].BufferType = SECBUFFER_STREAM_TRAILER; + encrypt_buf[2].cbBuffer = st->stream_sizes.cbTrailer; + encrypt_buf[2].pvBuffer = + ciphertext_out.ptr + st->stream_sizes.cbHeader + + message_len; + + memcpy(ciphertext_out.ptr + st->stream_sizes.cbHeader, data, message_len); + + if (EncryptMessage(&st->context, 0, &encrypt_buf_desc, 0) != SEC_E_OK) { + git_error_set(GIT_ERROR_OS, "could not encrypt tls message"); + total_len = -1; + goto done; + } + + ciphertext_len = encrypt_buf[0].cbBuffer + + encrypt_buf[1].cbBuffer + + encrypt_buf[2].cbBuffer; + + while (ciphertext_written < ciphertext_len) { + ssize_t chunk_len = git_stream_write(st->io, + ciphertext_out.ptr + ciphertext_written, + ciphertext_len - ciphertext_written, 0); + + if (chunk_len < 0) { + total_len = -1; + goto done; + } + + ciphertext_len -= chunk_len; + ciphertext_written += chunk_len; + } + + total_len += message_len; + + data += message_len; + data_len -= message_len; + } + +done: + git_str_dispose(&ciphertext_out); + return total_len; +} + +static ssize_t schannel_read(git_stream *stream, void *_data, size_t data_len) +{ + schannel_stream *st = (schannel_stream *)stream; + char *data = (char *)_data; + SecBuffer decrypt_buf[4]; + SecBufferDesc decrypt_buf_desc = { SECBUFFER_VERSION, 4, decrypt_buf }; + SECURITY_STATUS status; + ssize_t chunk_len, total_len = 0; + + if (data_len > SSIZE_MAX) + data_len = SSIZE_MAX; + + /* + * Loop until we have some bytes to return - we may have decrypted + * bytes queued or ciphertext from the wire that we can decrypt and + * return. Return any queued bytes if they're available to avoid a + * network read, which may block. We may return less than the + * caller requested, and they can retry for an actual network + */ + while ((size_t)total_len < data_len) { + if (st->plaintext_in.size > 0) { + size_t copy_len = min(st->plaintext_in.size, data_len); + + memcpy(data, st->plaintext_in.ptr, copy_len); + git_str_consume_bytes(&st->plaintext_in, copy_len); + + data += copy_len; + data_len -= copy_len; + + total_len += copy_len; + + continue; + } + + if (st->ciphertext_in.size > 0) { + decrypt_buf[0].BufferType = SECBUFFER_DATA; + decrypt_buf[0].cbBuffer = (unsigned long)min(st->ciphertext_in.size, ULONG_MAX); + decrypt_buf[0].pvBuffer = st->ciphertext_in.ptr; + + decrypt_buf[1].BufferType = SECBUFFER_EMPTY; + decrypt_buf[1].cbBuffer = 0; + decrypt_buf[1].pvBuffer = NULL; + + decrypt_buf[2].BufferType = SECBUFFER_EMPTY; + decrypt_buf[2].cbBuffer = 0; + decrypt_buf[2].pvBuffer = NULL; + + decrypt_buf[3].BufferType = SECBUFFER_EMPTY; + decrypt_buf[3].cbBuffer = 0; + decrypt_buf[3].pvBuffer = NULL; + + status = DecryptMessage(&st->context, &decrypt_buf_desc, 0, NULL); + + if (status == SEC_E_OK) { + GIT_ASSERT(decrypt_buf[0].BufferType == SECBUFFER_STREAM_HEADER); + GIT_ASSERT(decrypt_buf[1].BufferType == SECBUFFER_DATA); + GIT_ASSERT(decrypt_buf[2].BufferType == SECBUFFER_STREAM_TRAILER); + + if (git_str_put(&st->plaintext_in, decrypt_buf[1].pvBuffer, decrypt_buf[1].cbBuffer) < 0) { + total_len = -1; + goto done; + } + + if (decrypt_buf[3].BufferType == SECBUFFER_EXTRA) { + git_str_consume_bytes(&st->ciphertext_in, (st->ciphertext_in.size - decrypt_buf[3].cbBuffer)); + } else { + git_str_clear(&st->ciphertext_in); + } + + continue; + } else if (status == SEC_E_CONTEXT_EXPIRED) { + break; + } else if (status != SEC_E_INCOMPLETE_MESSAGE) { + git_error_set(GIT_ERROR_SSL, "could not decrypt tls message"); + total_len = -1; + goto done; + } + } + + if (total_len != 0) + break; + + if (git_str_grow_by(&st->ciphertext_in, READ_BLOCKSIZE) < 0) { + total_len = -1; + goto done; + } + + if ((chunk_len = git_stream_read(st->io, st->ciphertext_in.ptr + st->ciphertext_in.size, st->ciphertext_in.asize - st->ciphertext_in.size)) < 0) { + total_len = -1; + goto done; + } + + st->ciphertext_in.size += chunk_len; + } + +done: + return total_len; +} + +static int schannel_close(git_stream *stream) +{ + schannel_stream *st = (schannel_stream *)stream; + int error = 0; + + if (st->connected) { + SecBuffer shutdown_buf; + SecBufferDesc shutdown_buf_desc = + { SECBUFFER_VERSION, 1, &shutdown_buf }; + DWORD shutdown_message = SCHANNEL_SHUTDOWN, shutdown_flags; + + shutdown_buf.BufferType = SECBUFFER_TOKEN; + shutdown_buf.cbBuffer = sizeof(DWORD); + shutdown_buf.pvBuffer = &shutdown_message; + + if (ApplyControlToken(&st->context, &shutdown_buf_desc) != SEC_E_OK) { + git_error_set(GIT_ERROR_SSL, "could not shutdown stream"); + error = -1; + } + + shutdown_buf.BufferType = SECBUFFER_TOKEN; + shutdown_buf.cbBuffer = 0; + shutdown_buf.pvBuffer = NULL; + + shutdown_flags = ISC_REQ_ALLOCATE_MEMORY | + ISC_REQ_CONFIDENTIALITY | + ISC_REQ_REPLAY_DETECT | + ISC_REQ_SEQUENCE_DETECT | + ISC_REQ_STREAM; + + if (InitializeSecurityContext(&st->cred, &st->context, + NULL, shutdown_flags, 0, 0, + &shutdown_buf_desc, 0, NULL, + &shutdown_buf_desc, &shutdown_flags, + NULL) == SEC_E_OK) { + if (shutdown_buf.cbBuffer > 0) { + if (git_stream__write_full(st->io, + shutdown_buf.pvBuffer, + shutdown_buf.cbBuffer, 0) < 0) + error = -1; + + FreeContextBuffer(shutdown_buf.pvBuffer); + } + } + } + + st->connected = false; + + if (st->owned && git_stream_close(st->io) < 0) + error = -1; + + return error; +} + +static void schannel_free(git_stream *stream) +{ + schannel_stream *st = (schannel_stream *)stream; + + if (st->state >= STATE_CERTIFICATE) { + CertFreeCertificateContext(st->certificate); + CertFreeCertificateChain(st->cert_chain); + } + + if (st->state >= STATE_CONTEXT) + DeleteSecurityContext(&st->context); + + if (st->state >= STATE_CRED) + FreeCredentialsHandle(&st->cred); + + st->state = STATE_NONE; + + git_str_dispose(&st->ciphertext_in); + git_str_dispose(&st->plaintext_in); + + git__free(st->host_w); + + if (st->owned) + git_stream_free(st->io); + + git__free(st); +} + +static int schannel_stream_wrap( + git_stream **out, + git_stream *in, + const char *host, + int owned) +{ + schannel_stream *st; + + st = git__calloc(1, sizeof(schannel_stream)); + GIT_ERROR_CHECK_ALLOC(st); + + st->io = in; + st->owned = owned; + + if (git_utf8_to_16_alloc(&st->host_w, host) < 0) { + git__free(st); + return -1; + } + + st->parent.version = GIT_STREAM_VERSION; + st->parent.encrypted = 1; + st->parent.proxy_support = git_stream_supports_proxy(st->io); + st->parent.connect = schannel_connect; + st->parent.certificate = schannel_certificate; + st->parent.set_proxy = schannel_set_proxy; + st->parent.read = schannel_read; + st->parent.write = schannel_write; + st->parent.close = schannel_close; + st->parent.free = schannel_free; + + *out = (git_stream *)st; + return 0; +} + +extern int git_schannel_stream_new( + git_stream **out, + const char *host, + const char *port) +{ + git_stream *stream; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(host); + GIT_ASSERT_ARG(port); + + if ((error = git_socket_stream_new(&stream, host, port)) < 0) + return error; + + if ((error = schannel_stream_wrap(out, stream, host, 1)) < 0) { + git_stream_close(stream); + git_stream_free(stream); + } + + return error; +} + +extern int git_schannel_stream_wrap( + git_stream **out, + git_stream *in, + const char *host) +{ + return schannel_stream_wrap(out, in, host, 0); +} + +#endif diff --git a/src/libgit2/streams/schannel.h b/src/libgit2/streams/schannel.h new file mode 100644 index 00000000000..153bdbf96e7 --- /dev/null +++ b/src/libgit2/streams/schannel.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_steams_schannel_h__ +#define INCLUDE_steams_schannel_h__ + +#include "common.h" + +#include "git2/sys/stream.h" + +#ifdef GIT_HTTPS_SCHANNEL + +extern int git_schannel_stream_new( + git_stream **out, + const char *host, + const char *port); + +extern int git_schannel_stream_wrap( + git_stream **out, + git_stream *in, + const char *host); + +#endif + +#endif diff --git a/src/libgit2/streams/socket.c b/src/libgit2/streams/socket.c new file mode 100644 index 00000000000..a463312fd85 --- /dev/null +++ b/src/libgit2/streams/socket.c @@ -0,0 +1,428 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "streams/socket.h" + +#include "posix.h" +#include "registry.h" +#include "runtime.h" +#include "stream.h" + +#ifndef _WIN32 +# include +# include +# include +# include +# include +# include +# include +#else +# include +# include +# ifdef _MSC_VER +# pragma comment(lib, "ws2_32") +# endif +#endif + +int git_socket_stream__connect_timeout = 0; +int git_socket_stream__timeout = 0; + +#ifdef GIT_WIN32 +static void net_set_error(const char *str) +{ + int error = WSAGetLastError(); + char * win32_error = git_win32_get_error_message(error); + + if (win32_error) { + git_error_set(GIT_ERROR_NET, "%s: %s", str, win32_error); + git__free(win32_error); + } else { + git_error_set(GIT_ERROR_NET, "%s", str); + } +} +#else +static void net_set_error(const char *str) +{ + git_error_set(GIT_ERROR_NET, "%s: %s", str, strerror(errno)); +} +#endif + +static int close_socket(GIT_SOCKET s) +{ + if (s == INVALID_SOCKET) + return 0; + +#ifdef GIT_WIN32 + if (closesocket(s) != 0) { + net_set_error("could not close socket"); + return -1; + } + + return 0; +#else + return close(s); +#endif + +} + +static int set_nonblocking(GIT_SOCKET s) +{ +#ifdef GIT_WIN32 + unsigned long nonblocking = 1; + + if (ioctlsocket(s, FIONBIO, &nonblocking) != 0) { + net_set_error("could not set socket non-blocking"); + return -1; + } +#else + int flags; + + if ((flags = fcntl(s, F_GETFL, 0)) == -1) { + net_set_error("could not query socket flags"); + return -1; + } + + flags |= O_NONBLOCK; + + if (fcntl(s, F_SETFL, flags) != 0) { + net_set_error("could not set socket non-blocking"); + return -1; + } +#endif + + return 0; +} + +/* Promote a sockerr to an errno for our error handling routines */ +static int handle_sockerr(GIT_SOCKET socket) +{ + int sockerr; + socklen_t errlen = sizeof(sockerr); + + if (getsockopt(socket, SOL_SOCKET, SO_ERROR, + (void *)&sockerr, &errlen) < 0) + return -1; + + if (sockerr == ETIMEDOUT) + return GIT_TIMEOUT; + + errno = sockerr; + return -1; +} + +GIT_INLINE(bool) connect_would_block(int error) +{ +#ifdef GIT_WIN32 + if (error == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK) + return true; +#endif + + if (error == -1 && errno == EINPROGRESS) + return true; + + return false; +} + +static int connect_with_timeout( + GIT_SOCKET socket, + const struct sockaddr *address, + socklen_t address_len, + int timeout) +{ + struct pollfd fd; + int error; + + if (timeout && (error = set_nonblocking(socket)) < 0) + return error; + + error = connect(socket, address, address_len); + + if (error == 0 || !connect_would_block(error)) + return error; + + fd.fd = socket; + fd.events = POLLOUT; + fd.revents = 0; + + error = p_poll(&fd, 1, timeout); + + if (error == 0) { + return GIT_TIMEOUT; + } else if (error != 1) { + return -1; + } else if ((fd.revents & (POLLPRI | POLLHUP | POLLERR))) { + return handle_sockerr(socket); + } else if ((fd.revents & POLLOUT) != POLLOUT) { + git_error_set(GIT_ERROR_NET, + "unknown error while polling for connect: %d", + fd.revents); + return -1; + } + + return 0; +} + +static int socket_connect(git_stream *stream) +{ + git_socket_stream *st = (git_socket_stream *) stream; + GIT_SOCKET s = INVALID_SOCKET; + struct addrinfo *info = NULL, *p; + struct addrinfo hints; + int error; + + memset(&hints, 0x0, sizeof(struct addrinfo)); + hints.ai_socktype = SOCK_STREAM; + hints.ai_family = AF_UNSPEC; + + if ((error = p_getaddrinfo(st->host, st->port, &hints, &info)) != 0) { + git_error_set(GIT_ERROR_NET, + "failed to resolve address for %s: %s", + st->host, p_gai_strerror(error)); + return -1; + } + + for (p = info; p != NULL; p = p->ai_next) { + s = socket(p->ai_family, p->ai_socktype | SOCK_CLOEXEC, p->ai_protocol); + + if (s == INVALID_SOCKET) + continue; + + error = connect_with_timeout(s, p->ai_addr, + (socklen_t)p->ai_addrlen, + st->parent.connect_timeout); + + if (error == 0) + break; + + /* If we can't connect, try the next one */ + close_socket(s); + s = INVALID_SOCKET; + + if (error == GIT_TIMEOUT) + break; + } + + /* Oops, we couldn't connect to any address */ + if (s == INVALID_SOCKET) { + if (error == GIT_TIMEOUT) + git_error_set(GIT_ERROR_NET, "failed to connect to %s: Operation timed out", st->host); + else + git_error_set(GIT_ERROR_OS, "failed to connect to %s", st->host); + error = -1; + goto done; + } + + if (st->parent.timeout && !st->parent.connect_timeout && + (error = set_nonblocking(s)) < 0) + return error; + + st->s = s; + error = 0; + +done: + p_freeaddrinfo(info); + return error; +} + +static ssize_t socket_write( + git_stream *stream, + const char *data, + size_t len, + int flags) +{ + git_socket_stream *st = (git_socket_stream *) stream; + struct pollfd fd; + ssize_t ret; + + GIT_ASSERT(flags == 0); + GIT_UNUSED(flags); + + ret = p_send(st->s, data, len, 0); + + if (st->parent.timeout && ret < 0 && + (errno == EAGAIN || errno != EWOULDBLOCK)) { + fd.fd = st->s; + fd.events = POLLOUT; + fd.revents = 0; + + ret = p_poll(&fd, 1, st->parent.timeout); + + if (ret == 1) { + ret = p_send(st->s, data, len, 0); + } else if (ret == 0) { + git_error_set(GIT_ERROR_NET, + "could not write to socket: timed out"); + return GIT_TIMEOUT; + } + } + + if (ret < 0) { + net_set_error("error receiving data from socket"); + return -1; + } + + return ret; +} + +static ssize_t socket_read( + git_stream *stream, + void *data, + size_t len) +{ + git_socket_stream *st = (git_socket_stream *) stream; + struct pollfd fd; + ssize_t ret; + + ret = p_recv(st->s, data, len, 0); + + if (st->parent.timeout && ret < 0 && + (errno == EAGAIN || errno != EWOULDBLOCK)) { + fd.fd = st->s; + fd.events = POLLIN; + fd.revents = 0; + + ret = p_poll(&fd, 1, st->parent.timeout); + + if (ret == 1) { + ret = p_recv(st->s, data, len, 0); + } else if (ret == 0) { + git_error_set(GIT_ERROR_NET, + "could not read from socket: timed out"); + return GIT_TIMEOUT; + } + } + + if (ret < 0) { + net_set_error("error receiving data from socket"); + return -1; + } + + return ret; +} + +static int socket_close(git_stream *stream) +{ + git_socket_stream *st = (git_socket_stream *) stream; + int error; + + error = close_socket(st->s); + st->s = INVALID_SOCKET; + + return error; +} + +static void socket_free(git_stream *stream) +{ + git_socket_stream *st = (git_socket_stream *) stream; + + git__free(st->host); + git__free(st->port); + git__free(st); +} + +static int default_socket_stream_new( + git_stream **out, + const char *host, + const char *port) +{ + git_socket_stream *st; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(host); + GIT_ASSERT_ARG(port); + + st = git__calloc(1, sizeof(git_socket_stream)); + GIT_ERROR_CHECK_ALLOC(st); + + st->host = git__strdup(host); + GIT_ERROR_CHECK_ALLOC(st->host); + + if (port) { + st->port = git__strdup(port); + GIT_ERROR_CHECK_ALLOC(st->port); + } + + st->parent.version = GIT_STREAM_VERSION; + st->parent.timeout = git_socket_stream__timeout; + st->parent.connect_timeout = git_socket_stream__connect_timeout; + st->parent.connect = socket_connect; + st->parent.write = socket_write; + st->parent.read = socket_read; + st->parent.close = socket_close; + st->parent.free = socket_free; + st->s = INVALID_SOCKET; + + *out = (git_stream *) st; + return 0; +} + +int git_socket_stream_new( + git_stream **out, + const char *host, + const char *port) +{ + int (*init)(git_stream **, const char *, const char *) = NULL; + git_stream_registration custom = {0}; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(host); + GIT_ASSERT_ARG(port); + + if ((error = git_stream_registry_lookup(&custom, GIT_STREAM_STANDARD)) == 0) + init = custom.init; + else if (error == GIT_ENOTFOUND) + init = default_socket_stream_new; + else + return error; + + if (!init) { + git_error_set(GIT_ERROR_NET, "there is no socket stream available"); + return -1; + } + + return init(out, host, port); +} + +#ifdef GIT_WIN32 + +static void socket_stream_global_shutdown(void) +{ + WSACleanup(); +} + +int git_socket_stream_global_init(void) +{ + WORD winsock_version; + WSADATA wsa_data; + + winsock_version = MAKEWORD(2, 2); + + if (WSAStartup(winsock_version, &wsa_data) != 0) { + git_error_set(GIT_ERROR_OS, "could not initialize Windows Socket Library"); + return -1; + } + + if (LOBYTE(wsa_data.wVersion) != 2 || + HIBYTE(wsa_data.wVersion) != 2) { + git_error_set(GIT_ERROR_SSL, "Windows Socket Library does not support Winsock 2.2"); + return -1; + } + + return git_runtime_shutdown_register(socket_stream_global_shutdown); +} + +#else + +#include "stream.h" + +int git_socket_stream_global_init(void) +{ + return 0; +} + + #endif diff --git a/src/libgit2/streams/socket.h b/src/libgit2/streams/socket.h new file mode 100644 index 00000000000..73e8de099a6 --- /dev/null +++ b/src/libgit2/streams/socket.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_streams_socket_h__ +#define INCLUDE_streams_socket_h__ + +#include "common.h" + +#include "stream.h" + +typedef struct { + git_stream parent; + char *host; + char *port; + GIT_SOCKET s; +} git_socket_stream; + +extern int git_socket_stream_new(git_stream **out, const char *host, const char *port); + +extern int git_socket_stream_global_init(void); + +#endif diff --git a/src/libgit2/streams/stransport.c b/src/libgit2/streams/stransport.c new file mode 100644 index 00000000000..3dbc403b7f4 --- /dev/null +++ b/src/libgit2/streams/stransport.c @@ -0,0 +1,385 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "streams/stransport.h" + +#ifdef GIT_HTTPS_SECURETRANSPORT + +#include +#include +#include + +#include "common.h" +#include "trace.h" +#include "git2/transport.h" +#include "streams/socket.h" + +static int stransport_error(OSStatus ret) +{ + CFStringRef message_ref = NULL; + const char *message_cstr = NULL; + char *message_ptr = NULL; + + if (ret == noErr || ret == errSSLClosedGraceful) { + git_error_clear(); + return 0; + } + +#if !TARGET_OS_IPHONE + message_ref = SecCopyErrorMessageString(ret, NULL); + GIT_ERROR_CHECK_ALLOC(message_ref); + + /* + * Attempt the cheap CFString conversion; this can return NULL + * when that would be expensive. In that case, call the more + * expensive function. + */ + message_cstr = CFStringGetCStringPtr(message_ref, kCFStringEncodingUTF8); + + if (!message_cstr) { + /* Provide buffer to convert from UTF16 to UTF8 */ + size_t message_size = CFStringGetLength(message_ref) * 2 + 1; + + message_cstr = message_ptr = git__malloc(message_size); + GIT_ERROR_CHECK_ALLOC(message_ptr); + + if (!CFStringGetCString(message_ref, message_ptr, message_size, kCFStringEncodingUTF8)) { + git_error_set(GIT_ERROR_NET, "SecureTransport error: %d", (unsigned int)ret); + goto done; + } + } + + git_error_set(GIT_ERROR_NET, "SecureTransport error: %s", message_cstr); + +done: + git__free(message_ptr); + CFRelease(message_ref); +#else + git_error_set(GIT_ERROR_NET, "SecureTransport error: OSStatus %d", (unsigned int)ret); + GIT_UNUSED(message_ref); + GIT_UNUSED(message_cstr); + GIT_UNUSED(message_ptr); +#endif + + return -1; +} + +typedef struct { + git_stream parent; + git_stream *io; + int owned; + int error; + SSLContextRef ctx; + CFDataRef der_data; + git_cert_x509 cert_info; +} stransport_stream; + +static int stransport_connect(git_stream *stream) +{ + stransport_stream *st = (stransport_stream *) stream; + int error; + SecTrustRef trust = NULL; + SecTrustResultType sec_res; + OSStatus ret; + + if (st->owned && (error = git_stream_connect(st->io)) < 0) + return error; + + ret = SSLHandshake(st->ctx); + + if (ret != errSSLServerAuthCompleted && st->error != 0) + return -1; + else if (ret != errSSLServerAuthCompleted) { + git_error_set(GIT_ERROR_SSL, "unexpected return value from ssl handshake %d", (int)ret); + return -1; + } + + if ((ret = SSLCopyPeerTrust(st->ctx, &trust)) != noErr) + goto on_error; + + if (!trust) + return GIT_ECERTIFICATE; + + if ((ret = SecTrustEvaluate(trust, &sec_res)) != noErr) + goto on_error; + + CFRelease(trust); + + if (sec_res == kSecTrustResultInvalid || sec_res == kSecTrustResultOtherError) { + git_error_set(GIT_ERROR_SSL, "internal security trust error"); + return -1; + } + + if (sec_res == kSecTrustResultDeny || sec_res == kSecTrustResultRecoverableTrustFailure || + sec_res == kSecTrustResultFatalTrustFailure) { + git_error_set(GIT_ERROR_SSL, "untrusted connection error"); + return GIT_ECERTIFICATE; + } + + return 0; + +on_error: + if (trust) + CFRelease(trust); + + return stransport_error(ret); +} + +static int stransport_certificate(git_cert **out, git_stream *stream) +{ + stransport_stream *st = (stransport_stream *) stream; + SecTrustRef trust = NULL; + SecCertificateRef sec_cert; + OSStatus ret; + + if ((ret = SSLCopyPeerTrust(st->ctx, &trust)) != noErr) + return stransport_error(ret); + + sec_cert = SecTrustGetCertificateAtIndex(trust, 0); + st->der_data = SecCertificateCopyData(sec_cert); + CFRelease(trust); + + if (st->der_data == NULL) { + git_error_set(GIT_ERROR_SSL, "retrieved invalid certificate data"); + return -1; + } + + st->cert_info.parent.cert_type = GIT_CERT_X509; + st->cert_info.data = (void *) CFDataGetBytePtr(st->der_data); + st->cert_info.len = CFDataGetLength(st->der_data); + + *out = (git_cert *)&st->cert_info; + return 0; +} + +static int stransport_set_proxy( + git_stream *stream, + const git_proxy_options *proxy_opts) +{ + stransport_stream *st = (stransport_stream *) stream; + + return git_stream_set_proxy(st->io, proxy_opts); +} + +/* + * Contrary to typical network IO callbacks, Secure Transport write callback is + * expected to write *all* passed data, not just as much as it can, and any + * other case would be considered a failure. + * + * This behavior is actually not specified in the Apple documentation, but is + * required for things to work correctly (and incidentally, that's also how + * Apple implements it in its projects at opensource.apple.com). + * + * Libgit2 streams happen to already have this very behavior so this is just + * passthrough. + */ +static OSStatus write_cb(SSLConnectionRef conn, const void *data, size_t *len) +{ + stransport_stream *st = (stransport_stream *)conn; + git_stream *io = st->io; + OSStatus ret; + + st->error = 0; + + ret = git_stream__write_full(io, data, *len, 0); + + if (ret < 0) { + st->error = ret; + return (ret == GIT_TIMEOUT) ? + -9853 /* errSSLNetworkTimeout */: + -36 /* ioErr */; + } + + return noErr; +} + +static ssize_t stransport_write(git_stream *stream, const char *data, size_t len, int flags) +{ + stransport_stream *st = (stransport_stream *) stream; + size_t data_len, processed; + OSStatus ret; + + GIT_UNUSED(flags); + + data_len = min(len, SSIZE_MAX); + if ((ret = SSLWrite(st->ctx, data, data_len, &processed)) != noErr) { + if (st->error == GIT_TIMEOUT) + return GIT_TIMEOUT; + + return stransport_error(ret); + } + + GIT_ASSERT(processed < SSIZE_MAX); + return (ssize_t)processed; +} + +/* + * Contrary to typical network IO callbacks, Secure Transport read callback is + * expected to read *exactly* the requested number of bytes, not just as much + * as it can, and any other case would be considered a failure. + * + * This behavior is actually not specified in the Apple documentation, but is + * required for things to work correctly (and incidentally, that's also how + * Apple implements it in its projects at opensource.apple.com). + */ +static OSStatus read_cb(SSLConnectionRef conn, void *data, size_t *len) +{ + stransport_stream *st = (stransport_stream *)conn; + git_stream *io = st->io; + OSStatus error = noErr; + size_t off = 0; + ssize_t ret; + + st->error = 0; + + do { + ret = git_stream_read(io, data + off, *len - off); + + if (ret < 0) { + st->error = ret; + error = (ret == GIT_TIMEOUT) ? + -9853 /* errSSLNetworkTimeout */: + -36 /* ioErr */; + break; + } else if (ret == 0) { + error = errSSLClosedGraceful; + break; + } + + off += ret; + } while (off < *len); + + *len = off; + return error; +} + +static ssize_t stransport_read(git_stream *stream, void *data, size_t len) +{ + stransport_stream *st = (stransport_stream *)stream; + size_t processed; + OSStatus ret; + + if ((ret = SSLRead(st->ctx, data, len, &processed)) != noErr) { + /* This specific SecureTransport error is not well described */ + if (ret == -9806) + git_trace(GIT_TRACE_INFO, "SecureTraceport error during SSLRead: returned -9806 (connection closed via error)"); + + if (st->error == GIT_TIMEOUT) + return GIT_TIMEOUT; + + return stransport_error(ret); + } + + return processed; +} + +static int stransport_close(git_stream *stream) +{ + stransport_stream *st = (stransport_stream *) stream; + OSStatus ret; + + ret = SSLClose(st->ctx); + if (ret != noErr && ret != errSSLClosedGraceful) + return stransport_error(ret); + + return st->owned ? git_stream_close(st->io) : 0; +} + +static void stransport_free(git_stream *stream) +{ + stransport_stream *st = (stransport_stream *) stream; + + if (st->owned) + git_stream_free(st->io); + + CFRelease(st->ctx); + if (st->der_data) + CFRelease(st->der_data); + git__free(st); +} + +static int stransport_wrap( + git_stream **out, + git_stream *in, + const char *host, + int owned) +{ + stransport_stream *st; + OSStatus ret; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(in); + GIT_ASSERT_ARG(host); + + st = git__calloc(1, sizeof(stransport_stream)); + GIT_ERROR_CHECK_ALLOC(st); + + st->io = in; + st->owned = owned; + + st->ctx = SSLCreateContext(NULL, kSSLClientSide, kSSLStreamType); + if (!st->ctx) { + git_error_set(GIT_ERROR_NET, "failed to create SSL context"); + git__free(st); + return -1; + } + + if ((ret = SSLSetIOFuncs(st->ctx, read_cb, write_cb)) != noErr || + (ret = SSLSetConnection(st->ctx, st)) != noErr || + (ret = SSLSetSessionOption(st->ctx, kSSLSessionOptionBreakOnServerAuth, true)) != noErr || + (ret = SSLSetProtocolVersionMin(st->ctx, kTLSProtocol12)) != noErr || + (ret = SSLSetPeerDomainName(st->ctx, host, strlen(host))) != noErr) { + CFRelease(st->ctx); + git__free(st); + return stransport_error(ret); + } + + st->parent.version = GIT_STREAM_VERSION; + st->parent.encrypted = 1; + st->parent.proxy_support = git_stream_supports_proxy(st->io); + st->parent.connect = stransport_connect; + st->parent.certificate = stransport_certificate; + st->parent.set_proxy = stransport_set_proxy; + st->parent.read = stransport_read; + st->parent.write = stransport_write; + st->parent.close = stransport_close; + st->parent.free = stransport_free; + + *out = (git_stream *) st; + return 0; +} + +int git_stransport_stream_wrap( + git_stream **out, + git_stream *in, + const char *host) +{ + return stransport_wrap(out, in, host, 0); +} + +int git_stransport_stream_new(git_stream **out, const char *host, const char *port) +{ + git_stream *stream = NULL; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(host); + + error = git_socket_stream_new(&stream, host, port); + + if (!error) + error = stransport_wrap(out, stream, host, 1); + + if (error < 0 && stream) { + git_stream_close(stream); + git_stream_free(stream); + } + + return error; +} + +#endif diff --git a/src/libgit2/streams/stransport.h b/src/libgit2/streams/stransport.h new file mode 100644 index 00000000000..e1b936b53ba --- /dev/null +++ b/src/libgit2/streams/stransport.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_streams_stransport_h__ +#define INCLUDE_streams_stransport_h__ + +#include "common.h" + +#include "git2/sys/stream.h" + +#ifdef GIT_HTTPS_SECURETRANSPORT + +extern int git_stransport_stream_new(git_stream **out, const char *host, const char *port); +extern int git_stransport_stream_wrap(git_stream **out, git_stream *in, const char *host); + +#endif + +#endif diff --git a/src/libgit2/streams/tls.c b/src/libgit2/streams/tls.c new file mode 100644 index 00000000000..47ef2689f3f --- /dev/null +++ b/src/libgit2/streams/tls.c @@ -0,0 +1,82 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2/errors.h" + +#include "common.h" +#include "streams/registry.h" +#include "streams/tls.h" +#include "streams/mbedtls.h" +#include "streams/openssl.h" +#include "streams/stransport.h" +#include "streams/schannel.h" + +int git_tls_stream_new(git_stream **out, const char *host, const char *port) +{ + int (*init)(git_stream **, const char *, const char *) = NULL; + git_stream_registration custom = {0}; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(host); + GIT_ASSERT_ARG(port); + + if ((error = git_stream_registry_lookup(&custom, GIT_STREAM_TLS)) == 0) { + init = custom.init; + } else if (error == GIT_ENOTFOUND) { +#if defined(GIT_HTTPS_SECURETRANSPORT) + init = git_stransport_stream_new; +#elif defined(GIT_HTTPS_OPENSSL) || \ + defined(GIT_HTTPS_OPENSSL_DYNAMIC) + init = git_openssl_stream_new; +#elif defined(GIT_HTTPS_MBEDTLS) + init = git_mbedtls_stream_new; +#elif defined(GIT_HTTPS_SCHANNEL) + init = git_schannel_stream_new; +#endif + } else { + return error; + } + + if (!init) { + git_error_set(GIT_ERROR_SSL, "there is no TLS stream available"); + return -1; + } + + return init(out, host, port); +} + +int git_tls_stream_wrap(git_stream **out, git_stream *in, const char *host) +{ + int (*wrap)(git_stream **, git_stream *, const char *) = NULL; + git_stream_registration custom = {0}; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(in); + + if (git_stream_registry_lookup(&custom, GIT_STREAM_TLS) == 0) { + wrap = custom.wrap; + } else { +#if defined(GIT_HTTPS_SECURETRANSPORT) + wrap = git_stransport_stream_wrap; +#elif defined(GIT_HTTPS_OPENSSL) || \ + defined(GIT_HTTPS_OPENSSL_DYNAMIC) + wrap = git_openssl_stream_wrap; +#elif defined(GIT_HTTPS_MBEDTLS) + wrap = git_mbedtls_stream_wrap; +#elif defined(GIT_HTTPS_SCHANNEL) + wrap = git_schannel_stream_wrap; +#endif + } + + if (!wrap) { + git_error_set(GIT_ERROR_SSL, "there is no TLS stream available"); + return -1; + } + + return wrap(out, in, host); +} diff --git a/src/libgit2/streams/tls.h b/src/libgit2/streams/tls.h new file mode 100644 index 00000000000..465a6ea89e1 --- /dev/null +++ b/src/libgit2/streams/tls.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_streams_tls_h__ +#define INCLUDE_streams_tls_h__ + +#include "common.h" + +#include "git2/sys/stream.h" + +/** + * Create a TLS stream with the most appropriate backend available for + * the current platform, whether that's SecureTransport on macOS, + * OpenSSL or mbedTLS on other Unixes, or something else entirely. + */ +extern int git_tls_stream_new(git_stream **out, const char *host, const char *port); + +/** + * Create a TLS stream on top of an existing insecure stream, using + * the most appropriate backend available for the current platform. + * + * This allows us to create a CONNECT stream on top of a proxy; + * using SecureTransport on macOS, OpenSSL or mbedTLS on other + * Unixes, or something else entirely. + */ +extern int git_tls_stream_wrap(git_stream **out, git_stream *in, const char *host); + +#endif diff --git a/src/libgit2/submodule.c b/src/libgit2/submodule.c new file mode 100644 index 00000000000..7444e8c678b --- /dev/null +++ b/src/libgit2/submodule.c @@ -0,0 +1,2425 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "submodule.h" + +#include "buf.h" +#include "branch.h" +#include "vector.h" +#include "posix.h" +#include "config_backend.h" +#include "config.h" +#include "repository.h" +#include "tree.h" +#include "iterator.h" +#include "fs_path.h" +#include "str.h" +#include "index.h" +#include "worktree.h" +#include "clone.h" +#include "path.h" + +#include "git2/config.h" +#include "git2/sys/config.h" +#include "git2/types.h" +#include "git2/index.h" + +#define GIT_MODULES_FILE ".gitmodules" + +static git_configmap _sm_update_map[] = { + {GIT_CONFIGMAP_STRING, "checkout", GIT_SUBMODULE_UPDATE_CHECKOUT}, + {GIT_CONFIGMAP_STRING, "rebase", GIT_SUBMODULE_UPDATE_REBASE}, + {GIT_CONFIGMAP_STRING, "merge", GIT_SUBMODULE_UPDATE_MERGE}, + {GIT_CONFIGMAP_STRING, "none", GIT_SUBMODULE_UPDATE_NONE}, + {GIT_CONFIGMAP_FALSE, NULL, GIT_SUBMODULE_UPDATE_NONE}, + {GIT_CONFIGMAP_TRUE, NULL, GIT_SUBMODULE_UPDATE_CHECKOUT}, +}; + +static git_configmap _sm_ignore_map[] = { + {GIT_CONFIGMAP_STRING, "none", GIT_SUBMODULE_IGNORE_NONE}, + {GIT_CONFIGMAP_STRING, "untracked", GIT_SUBMODULE_IGNORE_UNTRACKED}, + {GIT_CONFIGMAP_STRING, "dirty", GIT_SUBMODULE_IGNORE_DIRTY}, + {GIT_CONFIGMAP_STRING, "all", GIT_SUBMODULE_IGNORE_ALL}, + {GIT_CONFIGMAP_FALSE, NULL, GIT_SUBMODULE_IGNORE_NONE}, + {GIT_CONFIGMAP_TRUE, NULL, GIT_SUBMODULE_IGNORE_ALL}, +}; + +static git_configmap _sm_recurse_map[] = { + {GIT_CONFIGMAP_STRING, "on-demand", GIT_SUBMODULE_RECURSE_ONDEMAND}, + {GIT_CONFIGMAP_FALSE, NULL, GIT_SUBMODULE_RECURSE_NO}, + {GIT_CONFIGMAP_TRUE, NULL, GIT_SUBMODULE_RECURSE_YES}, +}; + +enum { + CACHE_OK = 0, + CACHE_REFRESH = 1, + CACHE_FLUSH = 2 +}; +enum { + GITMODULES_EXISTING = 0, + GITMODULES_CREATE = 1 +}; + +static int submodule_alloc(git_submodule **out, git_repository *repo, const char *name); +static git_config_backend *open_gitmodules(git_repository *repo, int gitmod); +static int gitmodules_snapshot(git_config **snap, git_repository *repo); +static int get_url_base(git_str *url, git_repository *repo); +static int lookup_head_remote_key(git_str *remote_key, git_repository *repo); +static int lookup_default_remote(git_remote **remote, git_repository *repo); +static int submodule_load_each(const git_config_entry *entry, void *payload); +static int submodule_read_config(git_submodule *sm, git_config *cfg); +static int submodule_load_from_wd_lite(git_submodule *); +static void submodule_get_index_status(unsigned int *, git_submodule *); +static void submodule_get_wd_status(unsigned int *, git_submodule *, git_repository *, git_submodule_ignore_t); +static void submodule_update_from_index_entry(git_submodule *sm, const git_index_entry *ie); +static void submodule_update_from_head_data(git_submodule *sm, mode_t mode, const git_oid *id); + +static int submodule_cmp(const void *a, const void *b) +{ + return strcmp(((git_submodule *)a)->name, ((git_submodule *)b)->name); +} + +static int submodule_config_key_trunc_puts(git_str *key, const char *suffix) +{ + ssize_t idx = git_str_rfind(key, '.'); + git_str_truncate(key, (size_t)(idx + 1)); + return git_str_puts(key, suffix); +} + +/* + * PUBLIC APIS + */ + +static void submodule_set_lookup_error(int error, const char *name) +{ + if (!error) + return; + + git_error_set(GIT_ERROR_SUBMODULE, (error == GIT_ENOTFOUND) ? + "no submodule named '%s'" : + "submodule '%s' has not been added yet", name); +} + +typedef struct { + const char *path; + char *name; +} fbp_data; + +static int find_by_path(const git_config_entry *entry, void *payload) +{ + fbp_data *data = payload; + + if (!strcmp(entry->value, data->path)) { + const char *fdot, *ldot; + fdot = strchr(entry->name, '.'); + ldot = strrchr(entry->name, '.'); + data->name = git__strndup(fdot + 1, ldot - fdot - 1); + GIT_ERROR_CHECK_ALLOC(data->name); + } + + return 0; +} + +/* + * Checks to see if the submodule shares its name with a file or directory that + * already exists on the index. If so, the submodule cannot be added. + */ +static int is_path_occupied(bool *occupied, git_repository *repo, const char *path) +{ + int error = 0; + git_index *index; + git_str dir = GIT_STR_INIT; + *occupied = false; + + if ((error = git_repository_index__weakptr(&index, repo)) < 0) + goto out; + + if ((error = git_index_find(NULL, index, path)) != GIT_ENOTFOUND) { + if (!error) { + git_error_set(GIT_ERROR_SUBMODULE, + "File '%s' already exists in the index", path); + *occupied = true; + } + goto out; + } + + if ((error = git_str_sets(&dir, path)) < 0) + goto out; + + if ((error = git_fs_path_to_dir(&dir)) < 0) + goto out; + + if ((error = git_index_find_prefix(NULL, index, dir.ptr)) != GIT_ENOTFOUND) { + if (!error) { + git_error_set(GIT_ERROR_SUBMODULE, + "Directory '%s' already exists in the index", path); + *occupied = true; + } + goto out; + } + + error = 0; + +out: + git_str_dispose(&dir); + return error; +} + +GIT_HASHMAP_STR_SETUP(git_submodule_namemap, char *); + +/** + * Release the name map returned by 'load_submodule_names'. + */ +static void free_submodule_names(git_submodule_namemap *names) +{ + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + const char *key; + char *value; + + if (names == NULL) + return; + + while (git_submodule_namemap_iterate(&iter, &key, &value, names) == 0) { + git__free((char *)key); + git__free(value); + } + + git_submodule_namemap_dispose(names); + git__free(names); + + return; +} + +/** + * Map submodule paths to names. + * TODO: for some use-cases, this might need case-folding on a + * case-insensitive filesystem + */ +static int load_submodule_names(git_submodule_namemap **out, git_repository *repo, git_config *cfg) +{ + const char *key = "^submodule\\..*\\.path$"; + char *value; + git_config_iterator *iter = NULL; + git_config_entry *entry; + git_str buf = GIT_STR_INIT; + git_submodule_namemap *names; + int isvalid, error; + + *out = NULL; + + if ((names = git__calloc(1, sizeof(git_submodule_namemap))) == NULL) { + error = -1; + goto out; + } + + if ((error = git_config_iterator_glob_new(&iter, cfg, key)) < 0) + goto out; + + while ((error = git_config_next(&entry, iter)) == 0) { + const char *fdot, *ldot; + fdot = strchr(entry->name, '.'); + ldot = strrchr(entry->name, '.'); + + if (git_submodule_namemap_contains(names, entry->value)) { + git_error_set(GIT_ERROR_SUBMODULE, + "duplicated submodule path '%s'", entry->value); + error = -1; + goto out; + } + + git_str_clear(&buf); + git_str_put(&buf, fdot + 1, ldot - fdot - 1); + isvalid = git_submodule_name_is_valid(repo, buf.ptr, 0); + if (isvalid < 0) { + error = isvalid; + goto out; + } + if (!isvalid) + continue; + + if ((value = git__strdup(entry->value)) == NULL) { + error = -1; + goto out; + } + + if ((error = git_submodule_namemap_put(names, value, git_str_detach(&buf))) < 0) { + git_error_set(GIT_ERROR_NOMEMORY, "error inserting submodule into hash table"); + error = -1; + goto out; + } + } + if (error == GIT_ITEROVER) + error = 0; + + *out = names; + names = NULL; + +out: + free_submodule_names(names); + git_str_dispose(&buf); + git_config_iterator_free(iter); + return error; +} + +GIT_HASHMAP_STR_FUNCTIONS(git_submodule_cache, GIT_HASHMAP_INLINE, git_submodule *); + +int git_submodule__map(git_submodule_cache *cache, git_repository *repo); + +int git_submodule_cache_init(git_submodule_cache **out, git_repository *repo) +{ + git_submodule_cache *cache = NULL; + int error = 0; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + if ((cache = git__calloc(1, sizeof(git_submodule_cache))) == NULL) + return -1; + + if ((error = git_submodule__map(cache, repo)) < 0) { + git_submodule_cache_free(cache); + return error; + } + + *out = cache; + return error; +} + +int git_submodule_cache_free(git_submodule_cache *cache) +{ + git_submodule *sm = NULL; + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + + if (cache == NULL) + return 0; + + while (git_submodule_cache_iterate(&iter, NULL, &sm, cache) == 0) + git_submodule_free(sm); + + git_submodule_cache_dispose(cache); + git__free(cache); + return 0; +} + +int git_submodule_lookup( + git_submodule **out, /* NULL if user only wants to test existence */ + git_repository *repo, + const char *name) /* trailing slash is allowed */ +{ + return git_submodule__lookup_with_cache(out, repo, name, repo->submodule_cache); +} + +int git_submodule__lookup_with_cache( + git_submodule **out, /* NULL if user only wants to test existence */ + git_repository *repo, + const char *name, /* trailing slash is allowed */ + git_submodule_cache *cache) +{ + int error; + unsigned int location; + git_submodule *sm; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + if (repo->is_bare) { + git_error_set(GIT_ERROR_SUBMODULE, "cannot get submodules without a working tree"); + return -1; + } + + if (cache != NULL) { + if (git_submodule_cache_get(&sm, cache, name) == 0) { + if (out) { + *out = sm; + GIT_REFCOUNT_INC(*out); + } + return 0; + } + } + + if ((error = submodule_alloc(&sm, repo, name)) < 0) + return error; + + if ((error = git_submodule_reload(sm, false)) < 0) { + git_submodule_free(sm); + return error; + } + + if ((error = git_submodule_location(&location, sm)) < 0) { + git_submodule_free(sm); + return error; + } + + /* If it's not configured or we're looking by path */ + if (location == 0 || location == GIT_SUBMODULE_STATUS_IN_WD) { + git_config_backend *mods; + const char *pattern = "^submodule\\..*\\.path$"; + git_str path = GIT_STR_INIT; + fbp_data data = { NULL, NULL }; + + git_str_puts(&path, name); + while (path.ptr[path.size-1] == '/') { + path.ptr[--path.size] = '\0'; + } + data.path = path.ptr; + + mods = open_gitmodules(repo, GITMODULES_EXISTING); + + if (mods) + error = git_config_backend_foreach_match(mods, pattern, find_by_path, &data); + + git_config_backend_free(mods); + + if (error < 0) { + git_submodule_free(sm); + git_str_dispose(&path); + return error; + } + + if (data.name) { + git__free(sm->name); + sm->name = data.name; + sm->path = git_str_detach(&path); + + /* Try to load again with the right name */ + if ((error = git_submodule_reload(sm, false)) < 0) { + git_submodule_free(sm); + return error; + } + } + + git_str_dispose(&path); + } + + if ((error = git_submodule_location(&location, sm)) < 0) { + git_submodule_free(sm); + return error; + } + + /* If we still haven't found it, do the WD check */ + if (location == 0 || location == GIT_SUBMODULE_STATUS_IN_WD) { + git_submodule_free(sm); + error = GIT_ENOTFOUND; + + /* If it's not configured, we still check if there's a repo at the path */ + if (git_repository_workdir(repo)) { + git_str path = GIT_STR_INIT; + if (git_str_join3(&path, '/', + git_repository_workdir(repo), + name, DOT_GIT) < 0 || + git_path_validate_str_length(NULL, &path) < 0) + return -1; + + if (git_fs_path_exists(path.ptr)) + error = GIT_EEXISTS; + + git_str_dispose(&path); + } + + submodule_set_lookup_error(error, name); + return error; + } + + if (out) + *out = sm; + else + git_submodule_free(sm); + + return 0; +} + +int git_submodule_name_is_valid(git_repository *repo, const char *name, int flags) +{ + git_str buf = GIT_STR_INIT; + int error, isvalid; + + if (flags == 0) + flags = GIT_FS_PATH_REJECT_FILESYSTEM_DEFAULTS; + + /* Avoid allocating a new string if we can avoid it */ + if (strchr(name, '\\') != NULL) { + if ((error = git_fs_path_normalize_slashes(&buf, name)) < 0) + return error; + } else { + git_str_attach_notowned(&buf, name, strlen(name)); + } + + isvalid = git_path_is_valid(repo, buf.ptr, 0, flags); + git_str_dispose(&buf); + + return isvalid; +} + +static void submodule_free_dup(void *sm) +{ + git_submodule_free(sm); +} + +static int submodule_get_or_create( + git_submodule **out, + git_repository *repo, + git_submodule_cache *cache, + const char *name) +{ + git_submodule *sm = NULL; + int error; + + if (git_submodule_cache_get(&sm, cache, name) == 0) + goto done; + + /* if the submodule doesn't exist yet in the map, create it */ + if ((error = submodule_alloc(&sm, repo, name)) < 0) + return error; + + if ((error = git_submodule_cache_put(cache, sm->name, sm)) < 0) { + git_submodule_free(sm); + return error; + } + +done: + GIT_REFCOUNT_INC(sm); + *out = sm; + return 0; +} + +static int submodules_from_index( + git_submodule_cache *cache, + git_index *idx, + git_config *cfg) +{ + int error; + git_iterator *i = NULL; + const git_index_entry *entry; + git_submodule_namemap *names; + + if ((error = load_submodule_names(&names, git_index_owner(idx), cfg))) + goto done; + + if ((error = git_iterator_for_index(&i, git_index_owner(idx), idx, NULL)) < 0) + goto done; + + while (!(error = git_iterator_advance(&entry, i))) { + git_submodule *sm; + + if (git_submodule_cache_get(&sm, cache, entry->path) == 0) { + if (S_ISGITLINK(entry->mode)) + submodule_update_from_index_entry(sm, entry); + else + sm->flags |= GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE; + } else if (S_ISGITLINK(entry->mode)) { + const char *name; + + if (git_submodule_namemap_get((char **)&name, names, entry->path) != 0) + name = entry->path; + + if (!submodule_get_or_create(&sm, git_index_owner(idx), cache, name)) { + submodule_update_from_index_entry(sm, entry); + git_submodule_free(sm); + } + } + } + + if (error == GIT_ITEROVER) + error = 0; + +done: + git_iterator_free(i); + free_submodule_names(names); + + return error; +} + +static int submodules_from_head( + git_submodule_cache *cache, + git_tree *head, + git_config *cfg) +{ + int error; + git_iterator *i = NULL; + const git_index_entry *entry; + git_submodule_namemap *names; + + if ((error = load_submodule_names(&names, git_tree_owner(head), cfg))) + goto done; + + if ((error = git_iterator_for_tree(&i, head, NULL)) < 0) + goto done; + + while (!(error = git_iterator_advance(&entry, i))) { + git_submodule *sm; + + if (git_submodule_cache_get(&sm, cache, entry->path) == 0) { + if (S_ISGITLINK(entry->mode)) + submodule_update_from_head_data(sm, entry->mode, &entry->id); + else + sm->flags |= GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE; + } else if (S_ISGITLINK(entry->mode)) { + const char *name; + + if (git_submodule_namemap_get((char **)&name, names, entry->path) != 0) + name = entry->path; + + if (!submodule_get_or_create(&sm, git_tree_owner(head), cache, name)) { + submodule_update_from_head_data( + sm, entry->mode, &entry->id); + git_submodule_free(sm); + } + } + } + + if (error == GIT_ITEROVER) + error = 0; + +done: + git_iterator_free(i); + free_submodule_names(names); + + return error; +} + +/* If have_sm is true, sm is populated, otherwise map an repo are. */ +typedef struct { + git_config *mods; + git_submodule_cache *cache; + git_repository *repo; +} lfc_data; + +int git_submodule__map(git_submodule_cache *cache, git_repository *repo) +{ + int error = 0; + git_index *idx = NULL; + git_tree *head = NULL; + git_str path = GIT_STR_INIT; + git_submodule *sm; + git_config *mods = NULL; + bool has_workdir; + + GIT_ASSERT_ARG(cache); + GIT_ASSERT_ARG(repo); + + /* get sources that we will need to check */ + if (git_repository_index(&idx, repo) < 0) + git_error_clear(); + if (git_repository_head_tree(&head, repo) < 0) + git_error_clear(); + + has_workdir = git_repository_workdir(repo) != NULL; + + if (has_workdir && + (error = git_repository_workdir_path(&path, repo, GIT_MODULES_FILE)) < 0) + goto cleanup; + + /* add submodule information from .gitmodules */ + if (has_workdir) { + lfc_data data = { 0 }; + data.cache = cache; + data.repo = repo; + + if ((error = gitmodules_snapshot(&mods, repo)) < 0) { + if (error == GIT_ENOTFOUND) + error = 0; + goto cleanup; + } + + data.mods = mods; + if ((error = git_config_foreach( + mods, submodule_load_each, &data)) < 0) + goto cleanup; + } + /* add back submodule information from index */ + if (mods && idx) { + if ((error = submodules_from_index(cache, idx, mods)) < 0) + goto cleanup; + } + /* add submodule information from HEAD */ + if (mods && head) { + if ((error = submodules_from_head(cache, head, mods)) < 0) + goto cleanup; + } + /* shallow scan submodules in work tree as needed */ + if (has_workdir) { + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + + while (git_submodule_cache_iterate(&iter, NULL, &sm, cache) == 0) { + if ((error = submodule_load_from_wd_lite(sm)) < 0) + goto cleanup; + } + } + +cleanup: + git_config_free(mods); + /* TODO: if we got an error, mark submodule config as invalid? */ + git_index_free(idx); + git_tree_free(head); + git_str_dispose(&path); + return error; +} + +int git_submodule_foreach( + git_repository *repo, + git_submodule_cb callback, + void *payload) +{ + git_vector snapshot = GIT_VECTOR_INIT; + git_submodule_cache *submodules; + git_submodule *sm; + git_hashmap_iter_t iter; + int error; + size_t i; + + if (repo->is_bare) { + git_error_set(GIT_ERROR_SUBMODULE, "cannot get submodules without a working tree"); + return -1; + } + + if ((submodules = git__calloc(1, sizeof(git_submodule_cache))) == NULL) + return -1; + + if ((error = git_submodule__map(submodules, repo)) < 0) + goto done; + + if (!(error = git_vector_init(&snapshot, + git_submodule_cache_size(submodules), + submodule_cmp))) { + for (iter = GIT_HASHMAP_ITER_INIT; + git_submodule_cache_iterate(&iter, NULL, &sm, submodules) == 0; ) { + if ((error = git_vector_insert(&snapshot, sm)) < 0) + break; + + GIT_REFCOUNT_INC(sm); + } + } + + if (error < 0) + goto done; + + git_vector_uniq(&snapshot, submodule_free_dup); + + git_vector_foreach(&snapshot, i, sm) { + if ((error = callback(sm, sm->name, payload)) != 0) { + git_error_set_after_callback(error); + break; + } + } + +done: + git_vector_foreach(&snapshot, i, sm) + git_submodule_free(sm); + git_vector_dispose(&snapshot); + + for (iter = GIT_HASHMAP_ITER_INIT; + git_submodule_cache_iterate(&iter, NULL, &sm, submodules) == 0; ) + git_submodule_free(sm); + + git_submodule_cache_dispose(submodules); + git__free(submodules); + + return error; +} + +static int submodule_repo_init( + git_repository **out, + git_repository *parent_repo, + const char *path, + const char *url, + bool use_gitlink) +{ + int error = 0; + git_str workdir = GIT_STR_INIT, repodir = GIT_STR_INIT; + git_repository_init_options initopt = GIT_REPOSITORY_INIT_OPTIONS_INIT; + git_repository *subrepo = NULL; + + error = git_repository_workdir_path(&workdir, parent_repo, path); + if (error < 0) + goto cleanup; + + initopt.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_NO_REINIT; + initopt.origin_url = url; + + /* init submodule repository and add origin remote as needed */ + + /* New style: sub-repo goes in /modules// with a + * gitlink in the sub-repo workdir directory to that repository + * + * Old style: sub-repo goes directly into repo//.git/ + */ + if (use_gitlink) { + error = git_repository__item_path(&repodir, parent_repo, GIT_REPOSITORY_ITEM_MODULES); + if (error < 0) + goto cleanup; + error = git_str_joinpath(&repodir, repodir.ptr, path); + if (error < 0) + goto cleanup; + + initopt.workdir_path = workdir.ptr; + initopt.flags |= + GIT_REPOSITORY_INIT_RELATIVE_GITLINK; + + error = git_repository_init_ext(&subrepo, repodir.ptr, &initopt); + } else + error = git_repository_init_ext(&subrepo, workdir.ptr, &initopt); + +cleanup: + git_str_dispose(&workdir); + git_str_dispose(&repodir); + + *out = subrepo; + + return error; +} + +static int git_submodule__resolve_url( + git_str *out, + git_repository *repo, + const char *url) +{ + int error = 0; + git_str normalized = GIT_STR_INIT; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(url); + + /* We do this in all platforms in case someone on Windows created the .gitmodules */ + if (strchr(url, '\\')) { + if ((error = git_fs_path_normalize_slashes(&normalized, url)) < 0) + return error; + + url = normalized.ptr; + } + + + if (git_fs_path_is_relative(url)) { + if (!(error = get_url_base(out, repo))) + error = git_fs_path_apply_relative(out, url); + } else if (strchr(url, ':') != NULL || url[0] == '/') { + error = git_str_sets(out, url); + } else { + git_error_set(GIT_ERROR_SUBMODULE, "invalid format for submodule URL"); + error = -1; + } + + git_str_dispose(&normalized); + return error; +} + +int git_submodule_resolve_url( + git_buf *out, + git_repository *repo, + const char *url) +{ + GIT_BUF_WRAP_PRIVATE(out, git_submodule__resolve_url, repo, url); +} + +int git_submodule_add_setup( + git_submodule **out, + git_repository *repo, + const char *url, + const char *path, + int use_gitlink) +{ + int error = 0; + git_config_backend *mods = NULL; + git_submodule *sm = NULL; + git_str name = GIT_STR_INIT, real_url = GIT_STR_INIT; + git_repository *subrepo = NULL; + bool path_occupied; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(url); + GIT_ASSERT_ARG(path); + + /* see if there is already an entry for this submodule */ + + if (git_submodule_lookup(NULL, repo, path) < 0) + git_error_clear(); + else { + git_error_set(GIT_ERROR_SUBMODULE, + "attempt to add submodule '%s' that already exists", path); + return GIT_EEXISTS; + } + + /* validate and normalize path */ + + if (git__prefixcmp(path, git_repository_workdir(repo)) == 0) + path += strlen(git_repository_workdir(repo)); + + if (git_fs_path_root(path) >= 0) { + git_error_set(GIT_ERROR_SUBMODULE, "submodule path must be a relative path"); + error = -1; + goto cleanup; + } + + if ((error = is_path_occupied(&path_occupied, repo, path)) < 0) + goto cleanup; + + if (path_occupied) { + error = GIT_EEXISTS; + goto cleanup; + } + + /* update .gitmodules */ + + if (!(mods = open_gitmodules(repo, GITMODULES_CREATE))) { + git_error_set(GIT_ERROR_SUBMODULE, + "adding submodules to a bare repository is not supported"); + return -1; + } + + if ((error = git_str_printf(&name, "submodule.%s.path", path)) < 0 || + (error = git_config_backend_set_string(mods, name.ptr, path)) < 0) + goto cleanup; + + if ((error = submodule_config_key_trunc_puts(&name, "url")) < 0 || + (error = git_config_backend_set_string(mods, name.ptr, url)) < 0) + goto cleanup; + + git_str_clear(&name); + + /* init submodule repository and add origin remote as needed */ + + error = git_repository_workdir_path(&name, repo, path); + if (error < 0) + goto cleanup; + + /* if the repo does not already exist, then init a new repo and add it. + * Otherwise, just add the existing repo. + */ + if (!(git_fs_path_exists(name.ptr) && + git_fs_path_contains(&name, DOT_GIT))) { + + /* resolve the actual URL to use */ + if ((error = git_submodule__resolve_url(&real_url, repo, url)) < 0) + goto cleanup; + + if ((error = submodule_repo_init(&subrepo, repo, path, real_url.ptr, use_gitlink)) < 0) + goto cleanup; + } + + if ((error = git_submodule_lookup(&sm, repo, path)) < 0) + goto cleanup; + + error = git_submodule_init(sm, false); + +cleanup: + if (error && sm) { + git_submodule_free(sm); + sm = NULL; + } + if (out != NULL) + *out = sm; + + git_config_backend_free(mods); + git_repository_free(subrepo); + git_str_dispose(&real_url); + git_str_dispose(&name); + + return error; +} + +int git_submodule_repo_init( + git_repository **out, + const git_submodule *sm, + int use_gitlink) +{ + int error; + git_repository *sub_repo = NULL; + const char *configured_url; + git_config *cfg = NULL; + git_str buf = GIT_STR_INIT; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(sm); + + /* get the configured remote url of the submodule */ + if ((error = git_str_printf(&buf, "submodule.%s.url", sm->name)) < 0 || + (error = git_repository_config_snapshot(&cfg, sm->repo)) < 0 || + (error = git_config_get_string(&configured_url, cfg, buf.ptr)) < 0 || + (error = submodule_repo_init(&sub_repo, sm->repo, sm->path, configured_url, use_gitlink)) < 0) + goto done; + + *out = sub_repo; + +done: + git_config_free(cfg); + git_str_dispose(&buf); + return error; +} + +static int clone_return_origin(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload) +{ + GIT_UNUSED(url); + GIT_UNUSED(payload); + return git_remote_lookup(out, repo, name); +} + +static int clone_return_repo(git_repository **out, const char *path, int bare, void *payload) +{ + git_submodule *sm = payload; + + GIT_UNUSED(path); + GIT_UNUSED(bare); + return git_submodule_open(out, sm); +} + +int git_submodule_clone(git_repository **out, git_submodule *submodule, const git_submodule_update_options *given_opts) +{ + int error; + git_repository *clone; + git_str rel_path = GIT_STR_INIT; + git_submodule_update_options sub_opts = GIT_SUBMODULE_UPDATE_OPTIONS_INIT; + git_clone_options opts = GIT_CLONE_OPTIONS_INIT; + + GIT_ASSERT_ARG(submodule); + + if (given_opts) + memcpy(&sub_opts, given_opts, sizeof(sub_opts)); + + GIT_ERROR_CHECK_VERSION(&sub_opts, GIT_SUBMODULE_UPDATE_OPTIONS_VERSION, "git_submodule_update_options"); + + memcpy(&opts.checkout_opts, &sub_opts.checkout_opts, sizeof(sub_opts.checkout_opts)); + memcpy(&opts.fetch_opts, &sub_opts.fetch_opts, sizeof(sub_opts.fetch_opts)); + opts.repository_cb = clone_return_repo; + opts.repository_cb_payload = submodule; + opts.remote_cb = clone_return_origin; + opts.remote_cb_payload = submodule; + + error = git_repository_workdir_path(&rel_path, git_submodule_owner(submodule), git_submodule_path(submodule)); + if (error < 0) + goto cleanup; + + error = git_clone__submodule(&clone, git_submodule_url(submodule), git_str_cstr(&rel_path), &opts); + if (error < 0) + goto cleanup; + + if (!out) + git_repository_free(clone); + else + *out = clone; + +cleanup: + git_str_dispose(&rel_path); + + return error; +} + +int git_submodule_add_finalize(git_submodule *sm) +{ + int error; + git_index *index; + + GIT_ASSERT_ARG(sm); + + if ((error = git_repository_index__weakptr(&index, sm->repo)) < 0 || + (error = git_index_add_bypath(index, GIT_MODULES_FILE)) < 0) + return error; + + return git_submodule_add_to_index(sm, true); +} + +int git_submodule_add_to_index(git_submodule *sm, int write_index) +{ + int error; + git_repository *sm_repo = NULL; + git_index *index; + git_str path = GIT_STR_INIT; + git_commit *head; + git_index_entry entry; + struct stat st; + + GIT_ASSERT_ARG(sm); + + /* force reload of wd OID by git_submodule_open */ + sm->flags = sm->flags & ~GIT_SUBMODULE_STATUS__WD_OID_VALID; + + if ((error = git_repository_index__weakptr(&index, sm->repo)) < 0 || + (error = git_repository_workdir_path(&path, sm->repo, sm->path)) < 0 || + (error = git_submodule_open(&sm_repo, sm)) < 0) + goto cleanup; + + /* read stat information for submodule working directory */ + if (p_stat(path.ptr, &st) < 0) { + git_error_set(GIT_ERROR_SUBMODULE, + "cannot add submodule without working directory"); + error = -1; + goto cleanup; + } + + memset(&entry, 0, sizeof(entry)); + entry.path = sm->path; + git_index_entry__init_from_stat( + &entry, &st, !(git_index_caps(index) & GIT_INDEX_CAPABILITY_NO_FILEMODE)); + + /* calling git_submodule_open will have set sm->wd_oid if possible */ + if ((sm->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID) == 0) { + git_error_set(GIT_ERROR_SUBMODULE, + "cannot add submodule without HEAD to index"); + error = -1; + goto cleanup; + } + git_oid_cpy(&entry.id, &sm->wd_oid); + + if ((error = git_commit_lookup(&head, sm_repo, &sm->wd_oid)) < 0) + goto cleanup; + + entry.ctime.seconds = (int32_t)git_commit_time(head); + entry.ctime.nanoseconds = 0; + entry.mtime.seconds = (int32_t)git_commit_time(head); + entry.mtime.nanoseconds = 0; + + git_commit_free(head); + + /* add it */ + error = git_index_add(index, &entry); + + /* write it, if requested */ + if (!error && write_index) { + error = git_index_write(index); + + if (!error) + git_oid_cpy(&sm->index_oid, &sm->wd_oid); + } + +cleanup: + git_repository_free(sm_repo); + git_str_dispose(&path); + return error; +} + +static const char *submodule_update_to_str(git_submodule_update_t update) +{ + int i; + for (i = 0; i < (int)ARRAY_SIZE(_sm_update_map); ++i) + if (_sm_update_map[i].map_value == (int)update) + return _sm_update_map[i].str_match; + return NULL; +} + +git_repository *git_submodule_owner(git_submodule *submodule) +{ + GIT_ASSERT_ARG_WITH_RETVAL(submodule, NULL); + return submodule->repo; +} + +const char *git_submodule_name(git_submodule *submodule) +{ + GIT_ASSERT_ARG_WITH_RETVAL(submodule, NULL); + return submodule->name; +} + +const char *git_submodule_path(git_submodule *submodule) +{ + GIT_ASSERT_ARG_WITH_RETVAL(submodule, NULL); + return submodule->path; +} + +const char *git_submodule_url(git_submodule *submodule) +{ + GIT_ASSERT_ARG_WITH_RETVAL(submodule, NULL); + return submodule->url; +} + +static int write_var(git_repository *repo, const char *name, const char *var, const char *val) +{ + git_str key = GIT_STR_INIT; + git_config_backend *mods; + int error; + + mods = open_gitmodules(repo, GITMODULES_CREATE); + if (!mods) + return -1; + + if ((error = git_str_printf(&key, "submodule.%s.%s", name, var)) < 0) + goto cleanup; + + if (val) + error = git_config_backend_set_string(mods, key.ptr, val); + else + error = git_config_backend_delete(mods, key.ptr); + + git_str_dispose(&key); + +cleanup: + git_config_backend_free(mods); + return error; +} + +static int write_mapped_var(git_repository *repo, const char *name, git_configmap *maps, size_t nmaps, const char *var, int ival) +{ + git_configmap_t type; + const char *val; + + if (git_config_lookup_map_enum(&type, &val, maps, nmaps, ival) < 0) { + git_error_set(GIT_ERROR_SUBMODULE, "invalid value for %s", var); + return -1; + } + + if (type == GIT_CONFIGMAP_TRUE) + val = "true"; + + return write_var(repo, name, var, val); +} + +const char *git_submodule_branch(git_submodule *submodule) +{ + GIT_ASSERT_ARG_WITH_RETVAL(submodule, NULL); + return submodule->branch; +} + +int git_submodule_set_branch(git_repository *repo, const char *name, const char *branch) +{ + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + return write_var(repo, name, "branch", branch); +} + +int git_submodule_set_url(git_repository *repo, const char *name, const char *url) +{ + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + GIT_ASSERT_ARG(url); + + return write_var(repo, name, "url", url); +} + +const git_oid *git_submodule_index_id(git_submodule *submodule) +{ + GIT_ASSERT_ARG_WITH_RETVAL(submodule, NULL); + + if (submodule->flags & GIT_SUBMODULE_STATUS__INDEX_OID_VALID) + return &submodule->index_oid; + else + return NULL; +} + +const git_oid *git_submodule_head_id(git_submodule *submodule) +{ + GIT_ASSERT_ARG_WITH_RETVAL(submodule, NULL); + + if (submodule->flags & GIT_SUBMODULE_STATUS__HEAD_OID_VALID) + return &submodule->head_oid; + else + return NULL; +} + +const git_oid *git_submodule_wd_id(git_submodule *submodule) +{ + GIT_ASSERT_ARG_WITH_RETVAL(submodule, NULL); + + /* load unless we think we have a valid oid */ + if (!(submodule->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID)) { + git_repository *subrepo; + + /* calling submodule open grabs the HEAD OID if possible */ + if (!git_submodule_open_bare(&subrepo, submodule)) + git_repository_free(subrepo); + else + git_error_clear(); + } + + if (submodule->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID) + return &submodule->wd_oid; + else + return NULL; +} + +git_submodule_ignore_t git_submodule_ignore(git_submodule *submodule) +{ + GIT_ASSERT_ARG_WITH_RETVAL(submodule, GIT_SUBMODULE_IGNORE_UNSPECIFIED); + + return (submodule->ignore < GIT_SUBMODULE_IGNORE_NONE) ? + GIT_SUBMODULE_IGNORE_NONE : submodule->ignore; +} + +int git_submodule_set_ignore(git_repository *repo, const char *name, git_submodule_ignore_t ignore) +{ + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + return write_mapped_var(repo, name, _sm_ignore_map, ARRAY_SIZE(_sm_ignore_map), "ignore", ignore); +} + +git_submodule_update_t git_submodule_update_strategy(git_submodule *submodule) +{ + GIT_ASSERT_ARG_WITH_RETVAL(submodule, GIT_SUBMODULE_UPDATE_NONE); + + return (submodule->update < GIT_SUBMODULE_UPDATE_CHECKOUT) ? + GIT_SUBMODULE_UPDATE_CHECKOUT : submodule->update; +} + +int git_submodule_set_update(git_repository *repo, const char *name, git_submodule_update_t update) +{ + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + return write_mapped_var(repo, name, _sm_update_map, ARRAY_SIZE(_sm_update_map), "update", update); +} + +git_submodule_recurse_t git_submodule_fetch_recurse_submodules( + git_submodule *submodule) +{ + GIT_ASSERT_ARG_WITH_RETVAL(submodule, GIT_SUBMODULE_RECURSE_NO); + return submodule->fetch_recurse; +} + +int git_submodule_set_fetch_recurse_submodules(git_repository *repo, const char *name, git_submodule_recurse_t recurse) +{ + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + return write_mapped_var(repo, name, _sm_recurse_map, ARRAY_SIZE(_sm_recurse_map), "fetchRecurseSubmodules", recurse); +} + +static int submodule_repo_create( + git_repository **out, + git_repository *parent_repo, + const char *path) +{ + int error = 0; + git_str workdir = GIT_STR_INIT, repodir = GIT_STR_INIT; + git_repository_init_options initopt = GIT_REPOSITORY_INIT_OPTIONS_INIT; + git_repository *subrepo = NULL; + + initopt.flags = + GIT_REPOSITORY_INIT_MKPATH | + GIT_REPOSITORY_INIT_NO_REINIT | + GIT_REPOSITORY_INIT_RELATIVE_GITLINK; + + /* Workdir: path to sub-repo working directory */ + error = git_repository_workdir_path(&workdir, parent_repo, path); + if (error < 0) + goto cleanup; + + initopt.workdir_path = workdir.ptr; + + /** + * Repodir: path to the sub-repo. sub-repo goes in: + * /modules// with a gitlink in the + * sub-repo workdir directory to that repository. + */ + error = git_repository__item_path(&repodir, parent_repo, GIT_REPOSITORY_ITEM_MODULES); + if (error < 0) + goto cleanup; + error = git_str_joinpath(&repodir, repodir.ptr, path); + if (error < 0) + goto cleanup; + + error = git_repository_init_ext(&subrepo, repodir.ptr, &initopt); + +cleanup: + git_str_dispose(&workdir); + git_str_dispose(&repodir); + + *out = subrepo; + + return error; +} + +/** + * Callback to override sub-repository creation when + * cloning a sub-repository. + */ +static int git_submodule_update_repo_init_cb( + git_repository **out, + const char *path, + int bare, + void *payload) +{ + git_submodule *sm; + + GIT_UNUSED(bare); + + sm = payload; + + return submodule_repo_create(out, sm->repo, path); +} + +int git_submodule_update_options_init(git_submodule_update_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_submodule_update_options, GIT_SUBMODULE_UPDATE_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_submodule_update_init_options(git_submodule_update_options *opts, unsigned int version) +{ + return git_submodule_update_options_init(opts, version); +} +#endif + +int git_submodule_update(git_submodule *sm, int init, git_submodule_update_options *_update_options) +{ + int error; + unsigned int submodule_status; + git_config *config = NULL; + const char *submodule_url; + git_repository *sub_repo = NULL; + git_remote *remote = NULL; + git_object *target_commit = NULL; + git_str buf = GIT_STR_INIT; + git_submodule_update_options update_options = GIT_SUBMODULE_UPDATE_OPTIONS_INIT; + git_clone_options clone_options = GIT_CLONE_OPTIONS_INIT; + + GIT_ASSERT_ARG(sm); + + if (_update_options) + memcpy(&update_options, _update_options, sizeof(git_submodule_update_options)); + + GIT_ERROR_CHECK_VERSION(&update_options, GIT_SUBMODULE_UPDATE_OPTIONS_VERSION, "git_submodule_update_options"); + + /* Copy over the remote callbacks */ + memcpy(&clone_options.fetch_opts, &update_options.fetch_opts, sizeof(git_fetch_options)); + + /* Get the status of the submodule to determine if it is already initialized */ + if ((error = git_submodule_status(&submodule_status, sm->repo, sm->name, GIT_SUBMODULE_IGNORE_UNSPECIFIED)) < 0) + goto done; + + /* If the submodule is configured but hasn't been added, skip it */ + if (submodule_status == GIT_SUBMODULE_STATUS_IN_CONFIG) + goto done; + + /* + * If submodule work dir is not already initialized, check to see + * what we need to do (initialize, clone, return error...) + */ + if (submodule_status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) { + /* + * Work dir is not initialized, check to see if the submodule + * info has been copied into .git/config + */ + if ((error = git_repository_config_snapshot(&config, sm->repo)) < 0 || + (error = git_str_printf(&buf, "submodule.%s.url", git_submodule_name(sm))) < 0) + goto done; + + if ((error = git_config_get_string(&submodule_url, config, git_str_cstr(&buf))) < 0) { + /* + * If the error is not "not found" or if it is "not found" and we are not + * initializing the submodule, then return error. + */ + if (error != GIT_ENOTFOUND) + goto done; + + if (!init) { + git_error_set(GIT_ERROR_SUBMODULE, "submodule is not initialized"); + error = GIT_ERROR; + goto done; + } + + /* The submodule has not been initialized yet - initialize it now.*/ + if ((error = git_submodule_init(sm, 0)) < 0) + goto done; + + git_config_free(config); + config = NULL; + + if ((error = git_repository_config_snapshot(&config, sm->repo)) < 0 || + (error = git_config_get_string(&submodule_url, config, git_str_cstr(&buf))) < 0) + goto done; + } + + /** submodule is initialized - now clone it **/ + /* override repo creation */ + clone_options.repository_cb = git_submodule_update_repo_init_cb; + clone_options.repository_cb_payload = sm; + + /* + * Do not perform checkout as part of clone, instead we + * will checkout the specific commit manually. + */ + clone_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_NONE; + + if ((error = git_clone__submodule(&sub_repo, submodule_url, sm->path, &clone_options)) < 0 || + (error = git_repository_set_head_detached(sub_repo, git_submodule_index_id(sm))) < 0 || + (error = git_checkout_head(sub_repo, &update_options.checkout_opts)) != 0) + goto done; + } else { + const git_oid *oid; + + /** + * Work dir is initialized - look up the commit in the parent repository's index, + * update the workdir contents of the subrepository, and set the subrepository's + * head to the new commit. + */ + if ((error = git_submodule_open(&sub_repo, sm)) < 0) + goto done; + + if ((oid = git_submodule_index_id(sm)) == NULL) { + git_error_set(GIT_ERROR_SUBMODULE, "could not get ID of submodule in index"); + error = -1; + goto done; + } + + /* Look up the target commit in the submodule. */ + if ((error = git_object_lookup(&target_commit, sub_repo, oid, GIT_OBJECT_COMMIT)) < 0) { + /* If it isn't found then fetch and try again. */ + if (error != GIT_ENOTFOUND || !update_options.allow_fetch || + (error = lookup_default_remote(&remote, sub_repo)) < 0 || + (error = git_remote_fetch(remote, NULL, &update_options.fetch_opts, NULL)) < 0 || + (error = git_object_lookup(&target_commit, sub_repo, git_submodule_index_id(sm), GIT_OBJECT_COMMIT)) < 0) + goto done; + } + + if ((error = git_checkout_tree(sub_repo, target_commit, &update_options.checkout_opts)) != 0 || + (error = git_repository_set_head_detached(sub_repo, git_submodule_index_id(sm))) < 0) + goto done; + + /* Invalidate the wd flags as the workdir has been updated. */ + sm->flags = sm->flags & + ~(GIT_SUBMODULE_STATUS_IN_WD | + GIT_SUBMODULE_STATUS__WD_OID_VALID | + GIT_SUBMODULE_STATUS__WD_SCANNED); + } + +done: + git_str_dispose(&buf); + git_config_free(config); + git_object_free(target_commit); + git_remote_free(remote); + git_repository_free(sub_repo); + + return error; +} + +int git_submodule_init(git_submodule *sm, int overwrite) +{ + int error; + const char *val; + git_str key = GIT_STR_INIT, effective_submodule_url = GIT_STR_INIT; + git_config *cfg = NULL; + + if (!sm->url) { + git_error_set(GIT_ERROR_SUBMODULE, + "no URL configured for submodule '%s'", sm->name); + return -1; + } + + if ((error = git_repository_config(&cfg, sm->repo)) < 0) + return error; + + /* write "submodule.NAME.url" */ + + if ((error = git_submodule__resolve_url(&effective_submodule_url, sm->repo, sm->url)) < 0 || + (error = git_str_printf(&key, "submodule.%s.url", sm->name)) < 0 || + (error = git_config__update_entry( + cfg, key.ptr, effective_submodule_url.ptr, overwrite != 0, false)) < 0) + goto cleanup; + + /* write "submodule.NAME.update" if not default */ + + val = (sm->update == GIT_SUBMODULE_UPDATE_CHECKOUT) ? + NULL : submodule_update_to_str(sm->update); + + if ((error = git_str_printf(&key, "submodule.%s.update", sm->name)) < 0 || + (error = git_config__update_entry( + cfg, key.ptr, val, overwrite != 0, false)) < 0) + goto cleanup; + + /* success */ + +cleanup: + git_config_free(cfg); + git_str_dispose(&key); + git_str_dispose(&effective_submodule_url); + + return error; +} + +int git_submodule_sync(git_submodule *sm) +{ + git_str key = GIT_STR_INIT, url = GIT_STR_INIT, remote_name = GIT_STR_INIT; + git_repository *smrepo = NULL; + git_config *cfg = NULL; + int error = 0; + + if (!sm->url) { + git_error_set(GIT_ERROR_SUBMODULE, "no URL configured for submodule '%s'", sm->name); + return -1; + } + + /* copy URL over to config only if it already exists */ + if ((error = git_repository_config__weakptr(&cfg, sm->repo)) < 0 || + (error = git_str_printf(&key, "submodule.%s.url", sm->name)) < 0 || + (error = git_submodule__resolve_url(&url, sm->repo, sm->url)) < 0 || + (error = git_config__update_entry(cfg, key.ptr, url.ptr, true, true)) < 0) + goto out; + + if (!(sm->flags & GIT_SUBMODULE_STATUS_IN_WD)) + goto out; + + /* if submodule exists in the working directory, update remote url */ + if ((error = git_submodule_open(&smrepo, sm)) < 0 || + (error = git_repository_config__weakptr(&cfg, smrepo)) < 0) + goto out; + + if (lookup_head_remote_key(&remote_name, smrepo) == 0) { + if ((error = git_str_join3(&key, '.', "remote", remote_name.ptr, "url")) < 0) + goto out; + } else if ((error = git_str_sets(&key, "remote.origin.url")) < 0) { + goto out; + } + + if ((error = git_config__update_entry(cfg, key.ptr, url.ptr, true, false)) < 0) + goto out; + +out: + git_repository_free(smrepo); + git_str_dispose(&remote_name); + git_str_dispose(&key); + git_str_dispose(&url); + return error; +} + +static int git_submodule__open( + git_repository **subrepo, git_submodule *sm, bool bare) +{ + int error; + git_str path = GIT_STR_INIT; + unsigned int flags = GIT_REPOSITORY_OPEN_NO_SEARCH; + const char *wd; + + GIT_ASSERT_ARG(sm); + GIT_ASSERT_ARG(subrepo); + + if (git_repository__ensure_not_bare( + sm->repo, "open submodule repository") < 0) + return GIT_EBAREREPO; + + wd = git_repository_workdir(sm->repo); + + if (git_str_join3(&path, '/', wd, sm->path, DOT_GIT) < 0) + return -1; + + sm->flags = sm->flags & + ~(GIT_SUBMODULE_STATUS_IN_WD | + GIT_SUBMODULE_STATUS__WD_OID_VALID | + GIT_SUBMODULE_STATUS__WD_SCANNED); + + if (bare) + flags |= GIT_REPOSITORY_OPEN_BARE; + + error = git_repository_open_ext(subrepo, path.ptr, flags, wd); + + /* if we opened the submodule successfully, grab HEAD OID, etc. */ + if (!error) { + sm->flags |= GIT_SUBMODULE_STATUS_IN_WD | + GIT_SUBMODULE_STATUS__WD_SCANNED; + + if (!git_reference_name_to_id(&sm->wd_oid, *subrepo, GIT_HEAD_FILE)) + sm->flags |= GIT_SUBMODULE_STATUS__WD_OID_VALID; + else + git_error_clear(); + } else if (git_fs_path_exists(path.ptr)) { + sm->flags |= GIT_SUBMODULE_STATUS__WD_SCANNED | + GIT_SUBMODULE_STATUS_IN_WD; + } else { + git_str_rtruncate_at_char(&path, '/'); /* remove "/.git" */ + + if (git_fs_path_isdir(path.ptr)) + sm->flags |= GIT_SUBMODULE_STATUS__WD_SCANNED; + } + + git_str_dispose(&path); + + return error; +} + +int git_submodule_open_bare(git_repository **subrepo, git_submodule *sm) +{ + return git_submodule__open(subrepo, sm, true); +} + +int git_submodule_open(git_repository **subrepo, git_submodule *sm) +{ + return git_submodule__open(subrepo, sm, false); +} + +static void submodule_update_from_index_entry( + git_submodule *sm, const git_index_entry *ie) +{ + bool already_found = (sm->flags & GIT_SUBMODULE_STATUS_IN_INDEX) != 0; + + if (!S_ISGITLINK(ie->mode)) { + if (!already_found) + sm->flags |= GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE; + } else { + if (already_found) + sm->flags |= GIT_SUBMODULE_STATUS__INDEX_MULTIPLE_ENTRIES; + else + git_oid_cpy(&sm->index_oid, &ie->id); + + sm->flags |= GIT_SUBMODULE_STATUS_IN_INDEX | + GIT_SUBMODULE_STATUS__INDEX_OID_VALID; + } +} + +static int submodule_update_index(git_submodule *sm) +{ + git_index *index; + const git_index_entry *ie; + + if (git_repository_index__weakptr(&index, sm->repo) < 0) + return -1; + + sm->flags = sm->flags & + ~(GIT_SUBMODULE_STATUS_IN_INDEX | + GIT_SUBMODULE_STATUS__INDEX_OID_VALID); + + if (!(ie = git_index_get_bypath(index, sm->path, 0))) + return 0; + + submodule_update_from_index_entry(sm, ie); + + return 0; +} + +static void submodule_update_from_head_data( + git_submodule *sm, mode_t mode, const git_oid *id) +{ + if (!S_ISGITLINK(mode)) + sm->flags |= GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE; + else { + git_oid_cpy(&sm->head_oid, id); + + sm->flags |= GIT_SUBMODULE_STATUS_IN_HEAD | + GIT_SUBMODULE_STATUS__HEAD_OID_VALID; + } +} + +static int submodule_update_head(git_submodule *submodule) +{ + git_tree *head = NULL; + git_tree_entry *te = NULL; + + submodule->flags = submodule->flags & + ~(GIT_SUBMODULE_STATUS_IN_HEAD | + GIT_SUBMODULE_STATUS__HEAD_OID_VALID); + + /* if we can't look up file in current head, then done */ + if (git_repository_head_tree(&head, submodule->repo) < 0 || + git_tree_entry_bypath(&te, head, submodule->path) < 0) + git_error_clear(); + else + submodule_update_from_head_data(submodule, te->attr, git_tree_entry_id(te)); + + git_tree_entry_free(te); + git_tree_free(head); + return 0; +} + +int git_submodule_reload(git_submodule *sm, int force) +{ + git_config *mods = NULL; + int error; + + GIT_UNUSED(force); + + GIT_ASSERT_ARG(sm); + + if ((error = git_submodule_name_is_valid(sm->repo, sm->name, 0)) <= 0) + /* This should come with a warning, but we've no API for that */ + goto out; + + if (git_repository_is_bare(sm->repo)) + goto out; + + /* refresh config data */ + if ((error = gitmodules_snapshot(&mods, sm->repo)) < 0 && error != GIT_ENOTFOUND) + goto out; + + if (mods != NULL && (error = submodule_read_config(sm, mods)) < 0) + goto out; + + /* refresh wd data */ + sm->flags &= + ~(GIT_SUBMODULE_STATUS_IN_WD | + GIT_SUBMODULE_STATUS__WD_OID_VALID | + GIT_SUBMODULE_STATUS__WD_FLAGS); + + if ((error = submodule_load_from_wd_lite(sm)) < 0 || + (error = submodule_update_index(sm)) < 0 || + (error = submodule_update_head(sm)) < 0) + goto out; + +out: + git_config_free(mods); + return error; +} + +static void submodule_copy_oid_maybe( + git_oid *tgt, const git_oid *src, bool valid) +{ + if (tgt) { + if (valid) + memcpy(tgt, src, sizeof(*tgt)); + else + memset(tgt, 0, sizeof(*tgt)); + } +} + +int git_submodule__status( + unsigned int *out_status, + git_oid *out_head_id, + git_oid *out_index_id, + git_oid *out_wd_id, + git_submodule *sm, + git_submodule_ignore_t ign) +{ + unsigned int status; + git_repository *smrepo = NULL; + + if (ign == GIT_SUBMODULE_IGNORE_UNSPECIFIED) + ign = sm->ignore; + + /* only return location info if ignore == all */ + if (ign == GIT_SUBMODULE_IGNORE_ALL) { + *out_status = (sm->flags & GIT_SUBMODULE_STATUS__IN_FLAGS); + return 0; + } + + /* If the user has requested caching submodule state, performing these + * expensive operations (especially `submodule_update_head`, which is + * bottlenecked on `git_repository_head_tree`) eliminates much of the + * advantage. We will, therefore, interpret the request for caching to + * apply here to and skip them. + */ + + if (sm->repo->submodule_cache == NULL) { + /* refresh the index OID */ + if (submodule_update_index(sm) < 0) + return -1; + + /* refresh the HEAD OID */ + if (submodule_update_head(sm) < 0) + return -1; + } + + /* for ignore == dirty, don't scan the working directory */ + if (ign == GIT_SUBMODULE_IGNORE_DIRTY) { + /* git_submodule_open_bare will load WD OID data */ + if (git_submodule_open_bare(&smrepo, sm) < 0) + git_error_clear(); + else + git_repository_free(smrepo); + smrepo = NULL; + } else if (git_submodule_open(&smrepo, sm) < 0) { + git_error_clear(); + smrepo = NULL; + } + + status = GIT_SUBMODULE_STATUS__CLEAR_INTERNAL(sm->flags); + + submodule_get_index_status(&status, sm); + submodule_get_wd_status(&status, sm, smrepo, ign); + + git_repository_free(smrepo); + + *out_status = status; + + submodule_copy_oid_maybe(out_head_id, &sm->head_oid, + (sm->flags & GIT_SUBMODULE_STATUS__HEAD_OID_VALID) != 0); + submodule_copy_oid_maybe(out_index_id, &sm->index_oid, + (sm->flags & GIT_SUBMODULE_STATUS__INDEX_OID_VALID) != 0); + submodule_copy_oid_maybe(out_wd_id, &sm->wd_oid, + (sm->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID) != 0); + + return 0; +} + +int git_submodule_status(unsigned int *status, git_repository *repo, const char *name, git_submodule_ignore_t ignore) +{ + git_submodule *sm; + int error; + + GIT_ASSERT_ARG(status); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + if ((error = git_submodule_lookup(&sm, repo, name)) < 0) + return error; + + error = git_submodule__status(status, NULL, NULL, NULL, sm, ignore); + git_submodule_free(sm); + + return error; +} + +int git_submodule_location(unsigned int *location, git_submodule *sm) +{ + GIT_ASSERT_ARG(location); + GIT_ASSERT_ARG(sm); + + return git_submodule__status( + location, NULL, NULL, NULL, sm, GIT_SUBMODULE_IGNORE_ALL); +} + +/* + * INTERNAL FUNCTIONS + */ + +static int submodule_alloc( + git_submodule **out, git_repository *repo, const char *name) +{ + size_t namelen; + git_submodule *sm; + + if (!name || !(namelen = strlen(name))) { + git_error_set(GIT_ERROR_SUBMODULE, "invalid submodule name"); + return -1; + } + + sm = git__calloc(1, sizeof(git_submodule)); + GIT_ERROR_CHECK_ALLOC(sm); + + sm->name = sm->path = git__strdup(name); + if (!sm->name) { + git__free(sm); + return -1; + } + + GIT_REFCOUNT_INC(sm); + sm->ignore = sm->ignore_default = GIT_SUBMODULE_IGNORE_NONE; + sm->update = sm->update_default = GIT_SUBMODULE_UPDATE_CHECKOUT; + sm->fetch_recurse = sm->fetch_recurse_default = GIT_SUBMODULE_RECURSE_NO; + sm->repo = repo; + sm->branch = NULL; + + *out = sm; + return 0; +} + +static void submodule_release(git_submodule *sm) +{ + if (!sm) + return; + + if (sm->repo) { + sm->repo = NULL; + } + + if (sm->path != sm->name) + git__free(sm->path); + git__free(sm->name); + git__free(sm->url); + git__free(sm->branch); + git__memzero(sm, sizeof(*sm)); + git__free(sm); +} + +int git_submodule_dup(git_submodule **out, git_submodule *source) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(source); + + GIT_REFCOUNT_INC(source); + + *out = source; + return 0; +} + +void git_submodule_free(git_submodule *sm) +{ + if (!sm) + return; + GIT_REFCOUNT_DEC(sm, submodule_release); +} + +static int submodule_config_error(const char *property, const char *value) +{ + git_error_set(GIT_ERROR_INVALID, + "invalid value for submodule '%s' property: '%s'", property, value); + return -1; +} + +int git_submodule_parse_ignore(git_submodule_ignore_t *out, const char *value) +{ + int val; + + if (git_config_lookup_map_value( + &val, _sm_ignore_map, ARRAY_SIZE(_sm_ignore_map), value) < 0) { + *out = GIT_SUBMODULE_IGNORE_NONE; + return submodule_config_error("ignore", value); + } + + *out = (git_submodule_ignore_t)val; + return 0; +} + +int git_submodule_parse_update(git_submodule_update_t *out, const char *value) +{ + int val; + + if (git_config_lookup_map_value( + &val, _sm_update_map, ARRAY_SIZE(_sm_update_map), value) < 0) { + *out = GIT_SUBMODULE_UPDATE_CHECKOUT; + return submodule_config_error("update", value); + } + + *out = (git_submodule_update_t)val; + return 0; +} + +static int submodule_parse_recurse(git_submodule_recurse_t *out, const char *value) +{ + int val; + + if (git_config_lookup_map_value( + &val, _sm_recurse_map, ARRAY_SIZE(_sm_recurse_map), value) < 0) { + *out = GIT_SUBMODULE_RECURSE_YES; + return submodule_config_error("recurse", value); + } + + *out = (git_submodule_recurse_t)val; + return 0; +} + +static int get_value(const char **out, git_config *cfg, git_str *buf, const char *name, const char *field) +{ + int error; + + git_str_clear(buf); + + if ((error = git_str_printf(buf, "submodule.%s.%s", name, field)) < 0 || + (error = git_config_get_string(out, cfg, buf->ptr)) < 0) + return error; + + return error; +} + +static bool looks_like_command_line_option(const char *s) +{ + if (s && s[0] == '-') + return true; + + return false; +} + +static int submodule_read_config(git_submodule *sm, git_config *cfg) +{ + git_str key = GIT_STR_INIT; + const char *value; + int error, in_config = 0; + + /* + * TODO: Look up path in index and if it is present but not a GITLINK + * then this should be deleted (at least to match git's behavior) + */ + + if ((error = get_value(&value, cfg, &key, sm->name, "path")) == 0) { + in_config = 1; + /* We would warn here if we had that API */ + if (!looks_like_command_line_option(value)) { + /* + * TODO: if case insensitive filesystem, then the following strcmp + * should be strcasecmp + */ + if (strcmp(sm->name, value) != 0) { + if (sm->path != sm->name) + git__free(sm->path); + sm->path = git__strdup(value); + GIT_ERROR_CHECK_ALLOC(sm->path); + } + + } + } else if (error != GIT_ENOTFOUND) { + goto cleanup; + } + + if ((error = get_value(&value, cfg, &key, sm->name, "url")) == 0) { + /* We would warn here if we had that API */ + if (!looks_like_command_line_option(value)) { + in_config = 1; + sm->url = git__strdup(value); + GIT_ERROR_CHECK_ALLOC(sm->url); + } + } else if (error != GIT_ENOTFOUND) { + goto cleanup; + } + + if ((error = get_value(&value, cfg, &key, sm->name, "branch")) == 0) { + in_config = 1; + sm->branch = git__strdup(value); + GIT_ERROR_CHECK_ALLOC(sm->branch); + } else if (error != GIT_ENOTFOUND) { + goto cleanup; + } + + if ((error = get_value(&value, cfg, &key, sm->name, "update")) == 0) { + in_config = 1; + if ((error = git_submodule_parse_update(&sm->update, value)) < 0) + goto cleanup; + sm->update_default = sm->update; + } else if (error != GIT_ENOTFOUND) { + goto cleanup; + } + + if ((error = get_value(&value, cfg, &key, sm->name, "fetchRecurseSubmodules")) == 0) { + in_config = 1; + if ((error = submodule_parse_recurse(&sm->fetch_recurse, value)) < 0) + goto cleanup; + sm->fetch_recurse_default = sm->fetch_recurse; + } else if (error != GIT_ENOTFOUND) { + goto cleanup; + } + + if ((error = get_value(&value, cfg, &key, sm->name, "ignore")) == 0) { + in_config = 1; + if ((error = git_submodule_parse_ignore(&sm->ignore, value)) < 0) + goto cleanup; + sm->ignore_default = sm->ignore; + } else if (error != GIT_ENOTFOUND) { + goto cleanup; + } + + if (in_config) + sm->flags |= GIT_SUBMODULE_STATUS_IN_CONFIG; + + error = 0; + +cleanup: + git_str_dispose(&key); + return error; +} + +static int submodule_load_each(const git_config_entry *entry, void *payload) +{ + lfc_data *data = payload; + const char *namestart, *property; + git_submodule_cache *cache = data->cache; + git_str name = GIT_STR_INIT; + git_submodule *sm; + int error, isvalid; + + if (git__prefixcmp(entry->name, "submodule.") != 0) + return 0; + + namestart = entry->name + strlen("submodule."); + property = strrchr(namestart, '.'); + + if (!property || (property == namestart)) + return 0; + + property++; + + if ((error = git_str_set(&name, namestart, property - namestart -1)) < 0) + return error; + + isvalid = git_submodule_name_is_valid(data->repo, name.ptr, 0); + if (isvalid <= 0) { + error = isvalid; + goto done; + } + + /* + * Now that we have the submodule's name, we can use that to + * figure out whether it's in the map. If it's not, we create + * a new submodule, load the config and insert it. If it's + * already inserted, we've already loaded it, so we skip. + */ + if (git_submodule_cache_contains(cache, name.ptr)) { + error = 0; + goto done; + } + + if ((error = submodule_alloc(&sm, data->repo, name.ptr)) < 0) + goto done; + + if ((error = submodule_read_config(sm, data->mods)) < 0) { + git_submodule_free(sm); + goto done; + } + + if ((error = git_submodule_cache_put(cache, sm->name, sm)) < 0) + goto done; + + error = 0; + +done: + git_str_dispose(&name); + return error; +} + +static int submodule_load_from_wd_lite(git_submodule *sm) +{ + git_str path = GIT_STR_INIT; + + if (git_repository_workdir_path(&path, sm->repo, sm->path) < 0) + return -1; + + if (git_fs_path_isdir(path.ptr)) + sm->flags |= GIT_SUBMODULE_STATUS__WD_SCANNED; + + if (git_fs_path_contains(&path, DOT_GIT)) + sm->flags |= GIT_SUBMODULE_STATUS_IN_WD; + + git_str_dispose(&path); + return 0; +} + +/** + * Requests a snapshot of $WORK_TREE/.gitmodules. + * + * Returns GIT_ENOTFOUND in case no .gitmodules file exist + */ +static int gitmodules_snapshot(git_config **snap, git_repository *repo) +{ + git_config *mods = NULL; + git_str path = GIT_STR_INIT; + int error; + + if (git_repository_workdir(repo) == NULL) + return GIT_ENOTFOUND; + + if ((error = git_repository_workdir_path(&path, repo, GIT_MODULES_FILE)) < 0) + return error; + + if ((error = git_config_open_ondisk(&mods, path.ptr)) < 0) + goto cleanup; + git_str_dispose(&path); + + if ((error = git_config_snapshot(snap, mods)) < 0) + goto cleanup; + + error = 0; + +cleanup: + if (mods) + git_config_free(mods); + git_str_dispose(&path); + + return error; +} + +static git_config_backend *open_gitmodules( + git_repository *repo, + int okay_to_create) +{ + git_str path = GIT_STR_INIT; + git_config_backend *mods = NULL; + + if (git_repository_workdir(repo) != NULL) { + if (git_repository_workdir_path(&path, repo, GIT_MODULES_FILE) != 0) + return NULL; + + if (okay_to_create || git_fs_path_isfile(path.ptr)) { + /* git_config_backend_from_file should only fail if OOM */ + if (git_config_backend_from_file(&mods, path.ptr) < 0) + mods = NULL; + /* open should only fail here if the file is malformed */ + else if (git_config_backend_open(mods, GIT_CONFIG_LEVEL_LOCAL, repo) < 0) { + git_config_backend_free(mods); + mods = NULL; + } + } + } + + git_str_dispose(&path); + + return mods; +} + +/* Lookup name of remote of the local tracking branch HEAD points to */ +static int lookup_head_remote_key(git_str *remote_name, git_repository *repo) +{ + int error; + git_reference *head = NULL; + git_str upstream_name = GIT_STR_INIT; + + /* lookup and dereference HEAD */ + if ((error = git_repository_head(&head, repo)) < 0) + return error; + + /** + * If head does not refer to a branch, then return + * GIT_ENOTFOUND to indicate that we could not find + * a remote key for the local tracking branch HEAD points to. + **/ + if (!git_reference_is_branch(head)) { + git_error_set(GIT_ERROR_INVALID, + "HEAD does not refer to a branch."); + error = GIT_ENOTFOUND; + goto done; + } + + /* lookup remote tracking branch of HEAD */ + if ((error = git_branch__upstream_name( + &upstream_name, + repo, + git_reference_name(head))) < 0) + goto done; + + /* lookup remote of remote tracking branch */ + if ((error = git_branch__remote_name(remote_name, repo, upstream_name.ptr)) < 0) + goto done; + +done: + git_str_dispose(&upstream_name); + git_reference_free(head); + + return error; +} + +/* Lookup the remote of the local tracking branch HEAD points to */ +static int lookup_head_remote(git_remote **remote, git_repository *repo) +{ + int error; + git_str remote_name = GIT_STR_INIT; + + /* lookup remote of remote tracking branch name */ + if (!(error = lookup_head_remote_key(&remote_name, repo))) + error = git_remote_lookup(remote, repo, remote_name.ptr); + + git_str_dispose(&remote_name); + + return error; +} + +/* Lookup remote, either from HEAD or fall back on origin */ +static int lookup_default_remote(git_remote **remote, git_repository *repo) +{ + int error = lookup_head_remote(remote, repo); + + /* if that failed, use 'origin' instead */ + if (error == GIT_ENOTFOUND || error == GIT_EUNBORNBRANCH) + error = git_remote_lookup(remote, repo, "origin"); + + if (error == GIT_ENOTFOUND) + git_error_set( + GIT_ERROR_SUBMODULE, + "cannot get default remote for submodule - no local tracking " + "branch for HEAD and origin does not exist"); + + return error; +} + +static int get_url_base(git_str *url, git_repository *repo) +{ + int error; + git_worktree *wt = NULL; + git_remote *remote = NULL; + + if ((error = lookup_default_remote(&remote, repo)) == 0) { + error = git_str_sets(url, git_remote_url(remote)); + goto out; + } else if (error != GIT_ENOTFOUND) + goto out; + else + git_error_clear(); + + /* if repository does not have a default remote, use workdir instead */ + if (git_repository_is_worktree(repo)) { + if ((error = git_worktree_open_from_repository(&wt, repo)) < 0) + goto out; + error = git_str_sets(url, wt->parent_path); + } else { + error = git_str_sets(url, git_repository_workdir(repo)); + } + +out: + git_remote_free(remote); + git_worktree_free(wt); + + return error; +} + +static void submodule_get_index_status(unsigned int *status, git_submodule *sm) +{ + const git_oid *head_oid = git_submodule_head_id(sm); + const git_oid *index_oid = git_submodule_index_id(sm); + + *status = *status & ~GIT_SUBMODULE_STATUS__INDEX_FLAGS; + + if (!head_oid) { + if (index_oid) + *status |= GIT_SUBMODULE_STATUS_INDEX_ADDED; + } + else if (!index_oid) + *status |= GIT_SUBMODULE_STATUS_INDEX_DELETED; + else if (!git_oid_equal(head_oid, index_oid)) + *status |= GIT_SUBMODULE_STATUS_INDEX_MODIFIED; +} + + +static void submodule_get_wd_status( + unsigned int *status, + git_submodule *sm, + git_repository *sm_repo, + git_submodule_ignore_t ign) +{ + const git_oid *index_oid = git_submodule_index_id(sm); + const git_oid *wd_oid = + (sm->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID) ? &sm->wd_oid : NULL; + git_tree *sm_head = NULL; + git_index *index = NULL; + git_diff_options opt = GIT_DIFF_OPTIONS_INIT; + git_diff *diff; + + *status = *status & ~GIT_SUBMODULE_STATUS__WD_FLAGS; + + if (!index_oid) { + if (wd_oid) + *status |= GIT_SUBMODULE_STATUS_WD_ADDED; + } + else if (!wd_oid) { + if ((sm->flags & GIT_SUBMODULE_STATUS__WD_SCANNED) != 0 && + (sm->flags & GIT_SUBMODULE_STATUS_IN_WD) == 0) + *status |= GIT_SUBMODULE_STATUS_WD_UNINITIALIZED; + else + *status |= GIT_SUBMODULE_STATUS_WD_DELETED; + } + else if (!git_oid_equal(index_oid, wd_oid)) + *status |= GIT_SUBMODULE_STATUS_WD_MODIFIED; + + /* if we have no repo, then we're done */ + if (!sm_repo) + return; + + /* the diffs below could be optimized with an early termination + * option to the git_diff functions, but for now this is sufficient + * (and certainly no worse that what core git does). + */ + + if (ign == GIT_SUBMODULE_IGNORE_NONE) + opt.flags |= GIT_DIFF_INCLUDE_UNTRACKED; + + (void)git_repository_index__weakptr(&index, sm_repo); + + /* if we don't have an unborn head, check diff with index */ + if (git_repository_head_tree(&sm_head, sm_repo) < 0) + git_error_clear(); + else { + /* perform head to index diff on submodule */ + if (git_diff_tree_to_index(&diff, sm_repo, sm_head, index, &opt) < 0) + git_error_clear(); + else { + if (git_diff_num_deltas(diff) > 0) + *status |= GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED; + git_diff_free(diff); + diff = NULL; + } + + git_tree_free(sm_head); + } + + /* perform index-to-workdir diff on submodule */ + if (git_diff_index_to_workdir(&diff, sm_repo, index, &opt) < 0) + git_error_clear(); + else { + size_t untracked = + git_diff_num_deltas_of_type(diff, GIT_DELTA_UNTRACKED); + + if (untracked > 0) + *status |= GIT_SUBMODULE_STATUS_WD_UNTRACKED; + + if (git_diff_num_deltas(diff) != untracked) + *status |= GIT_SUBMODULE_STATUS_WD_WD_MODIFIED; + + git_diff_free(diff); + diff = NULL; + } +} diff --git a/src/libgit2/submodule.h b/src/libgit2/submodule.h new file mode 100644 index 00000000000..2690d8dd9fe --- /dev/null +++ b/src/libgit2/submodule.h @@ -0,0 +1,163 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_submodule_h__ +#define INCLUDE_submodule_h__ + +#include "common.h" + +#include "git2/submodule.h" +#include "git2/repository.h" +#include "futils.h" +#include "hashmap.h" + +/* Notes: + * + * Submodule information can be in four places: the index, the config files + * (both .git/config and .gitmodules), the HEAD tree, and the working + * directory. + * + * In the index: + * - submodule is found by path + * - may be missing, present, or of the wrong type + * - will have an oid if present + * + * In the HEAD tree: + * - submodule is found by path + * - may be missing, present, or of the wrong type + * - will have an oid if present + * + * In the config files: + * - submodule is found by submodule "name" which is usually the path + * - may be missing or present + * - will have a name, path, url, and other properties + * + * In the working directory: + * - submodule is found by path + * - may be missing, an empty directory, a checked out directory, + * or of the wrong type + * - if checked out, will have a HEAD oid + * - if checked out, will have git history that can be used to compare oids + * - if checked out, may have modified files and/or untracked files + */ + +/** + * Description of submodule + * + * This record describes a submodule found in a repository. There should be + * an entry for every submodule found in the HEAD and index, and for every + * submodule described in .gitmodules. The fields are as follows: + * + * - `rc` tracks the refcount of how many hash table entries in the + * git_submodule_cache there are for this submodule. It only comes into + * play if the name and path of the submodule differ. + * + * - `name` is the name of the submodule from .gitmodules. + * - `path` is the path to the submodule from the repo root. It is almost + * always the same as `name`. + * - `url` is the url for the submodule. + * - `update` is a git_submodule_update_t value - see gitmodules(5) update. + * - `update_default` is the update value from the config + * - `ignore` is a git_submodule_ignore_t value - see gitmodules(5) ignore. + * - `ignore_default` is the ignore value from the config + * - `fetch_recurse` is a git_submodule_recurse_t value - see gitmodules(5) + * fetchRecurseSubmodules. + * - `fetch_recurse_default` is the recurse value from the config + * + * - `repo` is the parent repository that contains this submodule. + * - `flags` after for internal use, tracking where this submodule has been + * found (head, index, config, workdir) and known status info, etc. + * - `head_oid` is the oid for the submodule path in the repo HEAD. + * - `index_oid` is the oid for the submodule recorded in the index. + * - `wd_oid` is the oid for the HEAD of the checked out submodule. + * + * If the submodule has been added to .gitmodules but not yet git added, + * then the `index_oid` will be zero but still marked valid. If the + * submodule has been deleted, but the delete has not been committed yet, + * then the `index_oid` will be set, but the `url` will be NULL. + */ +struct git_submodule { + git_refcount rc; + + /* information from config */ + char *name; + char *path; /* important: may just point to "name" string */ + char *url; + char *branch; + git_submodule_update_t update; + git_submodule_update_t update_default; + git_submodule_ignore_t ignore; + git_submodule_ignore_t ignore_default; + git_submodule_recurse_t fetch_recurse; + git_submodule_recurse_t fetch_recurse_default; + + /* internal information */ + git_repository *repo; + uint32_t flags; + git_oid head_oid; + git_oid index_oid; + git_oid wd_oid; +}; + +/* Additional flags on top of public GIT_SUBMODULE_STATUS values */ +enum { + GIT_SUBMODULE_STATUS__WD_SCANNED = (1u << 20), + GIT_SUBMODULE_STATUS__HEAD_OID_VALID = (1u << 21), + GIT_SUBMODULE_STATUS__INDEX_OID_VALID = (1u << 22), + GIT_SUBMODULE_STATUS__WD_OID_VALID = (1u << 23), + GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE = (1u << 24), + GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE = (1u << 25), + GIT_SUBMODULE_STATUS__WD_NOT_SUBMODULE = (1u << 26), + GIT_SUBMODULE_STATUS__INDEX_MULTIPLE_ENTRIES = (1u << 27) +}; + +#define GIT_SUBMODULE_STATUS__CLEAR_INTERNAL(S) \ + ((S) & ~(0xFFFFFFFFu << 20)) + +GIT_HASHMAP_STR_STRUCT(git_submodule_cache, git_submodule *); + +/* Initialize an external submodule cache for the provided repo. */ +extern int git_submodule_cache_init(git_submodule_cache **out, git_repository *repo); + +/* Release the resources of the submodule cache. */ +extern int git_submodule_cache_free(git_submodule_cache *cache); + +/* Submodule lookup with an explicit cache */ +extern int git_submodule__lookup_with_cache( + git_submodule **out, git_repository *repo, const char *path, git_submodule_cache *cache); + +/* Internal status fn returns status and optionally the various OIDs */ +extern int git_submodule__status( + unsigned int *out_status, + git_oid *out_head_id, + git_oid *out_index_id, + git_oid *out_wd_id, + git_submodule *sm, + git_submodule_ignore_t ign); + +/* Open submodule repository as bare repo for quick HEAD check, etc. */ +extern int git_submodule_open_bare( + git_repository **repo, + git_submodule *submodule); + +extern int git_submodule_parse_ignore( + git_submodule_ignore_t *out, const char *value); +extern int git_submodule_parse_update( + git_submodule_update_t *out, const char *value); + +/** + * Check whether a submodule's name is valid. + * + * Check the path against the path validity rules, either the filesystem + * defaults (like checkout does) or whichever you want to compare against. + * + * @param repo the repository which contains the submodule + * @param name the name to check + * @param flags the `GIT_PATH` flags to use for the check (0 to use filesystem defaults) + */ +extern int git_submodule_name_is_valid(git_repository *repo, const char *name, int flags); + +#endif diff --git a/src/libgit2/sysdir.c b/src/libgit2/sysdir.c new file mode 100644 index 00000000000..7838a6789c5 --- /dev/null +++ b/src/libgit2/sysdir.c @@ -0,0 +1,650 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "sysdir.h" + +#include "runtime.h" +#include "str.h" +#include "fs_path.h" +#include +#if GIT_WIN32 +# include "fs_path.h" +# include "win32/path_w32.h" +# include "win32/utf-conv.h" +#else +# include +# include +#endif + +#ifdef GIT_WIN32 +# define REG_GITFORWINDOWS_KEY L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1" +# define REG_GITFORWINDOWS_KEY_WOW64 L"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1" + +static int expand_win32_path(git_win32_path dest, const wchar_t *src) +{ + DWORD len = ExpandEnvironmentStringsW(src, dest, GIT_WIN_PATH_UTF16); + + if (!len || len > GIT_WIN_PATH_UTF16) + return -1; + + return 0; +} + +static int win32_path_to_utf8(git_str *dest, const wchar_t *src) +{ + git_win32_utf8_path utf8_path; + + if (git_win32_path_to_utf8(utf8_path, src) < 0) { + git_error_set(GIT_ERROR_OS, "unable to convert path to UTF-8"); + return -1; + } + + /* Convert backslashes to forward slashes */ + git_fs_path_mkposix(utf8_path); + + return git_str_sets(dest, utf8_path); +} + +static git_win32_path mock_registry; +static bool mock_registry_set; + +extern int git_win32__set_registry_system_dir(const wchar_t *mock_sysdir) +{ + if (!mock_sysdir) { + mock_registry[0] = L'\0'; + mock_registry_set = false; + } else { + size_t len = wcslen(mock_sysdir); + + if (len > GIT_WIN_PATH_MAX) { + git_error_set(GIT_ERROR_INVALID, "mock path too long"); + return -1; + } + + wcscpy(mock_registry, mock_sysdir); + mock_registry_set = true; + } + + return 0; +} + +static int lookup_registry_key( + git_win32_path out, + const HKEY hive, + const wchar_t* key, + const wchar_t *value) +{ + HKEY hkey; + DWORD type, size; + int error = GIT_ENOTFOUND; + + /* + * Registry data may not be NUL terminated, provide room to do + * it ourselves. + */ + size = (DWORD)((sizeof(git_win32_path) - 1) * sizeof(wchar_t)); + + if (RegOpenKeyExW(hive, key, 0, KEY_READ, &hkey) != 0) + return GIT_ENOTFOUND; + + if (RegQueryValueExW(hkey, value, NULL, &type, (LPBYTE)out, &size) == 0 && + type == REG_SZ && + size > 0 && + size < sizeof(git_win32_path)) { + size_t wsize = size / sizeof(wchar_t); + size_t len = wsize - 1; + + if (out[wsize - 1] != L'\0') { + len = wsize; + out[wsize] = L'\0'; + } + + if (out[len - 1] == L'\\') + out[len - 1] = L'\0'; + + if (_waccess(out, F_OK) == 0) + error = 0; + } + + RegCloseKey(hkey); + return error; +} + +static int find_sysdir_in_registry(git_win32_path out) +{ + if (mock_registry_set) { + if (mock_registry[0] == L'\0') + return GIT_ENOTFOUND; + + wcscpy(out, mock_registry); + return 0; + } + + if (lookup_registry_key(out, HKEY_CURRENT_USER, REG_GITFORWINDOWS_KEY, L"InstallLocation") == 0 || + lookup_registry_key(out, HKEY_CURRENT_USER, REG_GITFORWINDOWS_KEY_WOW64, L"InstallLocation") == 0 || + lookup_registry_key(out, HKEY_LOCAL_MACHINE, REG_GITFORWINDOWS_KEY, L"InstallLocation") == 0 || + lookup_registry_key(out, HKEY_LOCAL_MACHINE, REG_GITFORWINDOWS_KEY_WOW64, L"InstallLocation") == 0) + return 0; + + return GIT_ENOTFOUND; +} + +static int find_sysdir_in_path(git_win32_path out) +{ + size_t out_len; + + if (git_win32_path_find_executable(out, L"git.exe") < 0 && + git_win32_path_find_executable(out, L"git.cmd") < 0) + return GIT_ENOTFOUND; + + out_len = wcslen(out); + + /* Trim the file name */ + if (out_len <= CONST_STRLEN(L"git.exe")) + return GIT_ENOTFOUND; + + out_len -= CONST_STRLEN(L"git.exe"); + + if (out_len && out[out_len - 1] == L'\\') + out_len--; + + /* + * Git for Windows usually places the command in a 'bin' or + * 'cmd' directory, trim that. + */ + if (out_len >= CONST_STRLEN(L"\\bin") && + wcsncmp(&out[out_len - CONST_STRLEN(L"\\bin")], L"\\bin", CONST_STRLEN(L"\\bin")) == 0) + out_len -= CONST_STRLEN(L"\\bin"); + else if (out_len >= CONST_STRLEN(L"\\cmd") && + wcsncmp(&out[out_len - CONST_STRLEN(L"\\cmd")], L"\\cmd", CONST_STRLEN(L"\\cmd")) == 0) + out_len -= CONST_STRLEN(L"\\cmd"); + + if (!out_len) + return GIT_ENOTFOUND; + + out[out_len] = L'\0'; + return 0; +} + +static int find_win32_dirs( + git_str *out, + const wchar_t* tmpl[]) +{ + git_win32_path path16; + git_str buf = GIT_STR_INIT; + + git_str_clear(out); + + for (; *tmpl != NULL; tmpl++) { + if (!expand_win32_path(path16, *tmpl) && + path16[0] != L'%' && + !_waccess(path16, F_OK)) { + win32_path_to_utf8(&buf, path16); + + if (buf.size) + git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr); + } + } + + git_str_dispose(&buf); + + return (git_str_oom(out) ? -1 : 0); +} + +static int append_subdir(git_str *out, git_str *path, const char *subdir) +{ + static const char* architecture_roots[] = { + "", + "mingw64", + "mingw32", + NULL + }; + const char **root; + size_t orig_path_len = path->size; + + for (root = architecture_roots; *root; root++) { + if ((*root[0] && git_str_joinpath(path, path->ptr, *root) < 0) || + git_str_joinpath(path, path->ptr, subdir) < 0) + return -1; + + if (git_fs_path_exists(path->ptr) && + git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, path->ptr) < 0) + return -1; + + git_str_truncate(path, orig_path_len); + } + + return 0; +} + +int git_win32__find_system_dirs(git_str *out, const char *subdir) +{ + git_win32_path pathdir, regdir; + git_str path8 = GIT_STR_INIT; + bool has_pathdir, has_regdir; + int error; + + has_pathdir = (find_sysdir_in_path(pathdir) == 0); + has_regdir = (find_sysdir_in_registry(regdir) == 0); + + if (!has_pathdir && !has_regdir) + return 0; + + /* + * Usually the git in the path is the same git in the registry, + * in this case there's no need to duplicate the paths. + */ + if (has_pathdir && has_regdir && wcscmp(pathdir, regdir) == 0) + has_regdir = false; + + if (has_pathdir) { + if ((error = win32_path_to_utf8(&path8, pathdir)) < 0 || + (error = append_subdir(out, &path8, subdir)) < 0) + goto done; + } + + if (has_regdir) { + if ((error = win32_path_to_utf8(&path8, regdir)) < 0 || + (error = append_subdir(out, &path8, subdir)) < 0) + goto done; + } + +done: + git_str_dispose(&path8); + return error; +} +#endif /* WIN32 */ + +static int git_sysdir_guess_programdata_dirs(git_str *out) +{ +#ifdef GIT_WIN32 + static const wchar_t *programdata_tmpls[2] = { + L"%PROGRAMDATA%\\Git", + NULL, + }; + + return find_win32_dirs(out, programdata_tmpls); +#else + git_str_clear(out); + return 0; +#endif +} + +static int git_sysdir_guess_system_dirs(git_str *out) +{ +#ifdef GIT_WIN32 + return git_win32__find_system_dirs(out, "etc"); +#else + return git_str_sets(out, "/etc"); +#endif +} + +#ifndef GIT_WIN32 +static int get_passwd_home(git_str *out, uid_t uid) +{ + struct passwd pwd, *pwdptr; + char *buf = NULL; + long buflen; + int error; + + GIT_ASSERT_ARG(out); + + if ((buflen = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1) + buflen = 1024; + + do { + buf = git__realloc(buf, buflen); + error = getpwuid_r(uid, &pwd, buf, buflen, &pwdptr); + buflen *= 2; + } while (error == ERANGE && buflen <= 8192); + + if (error) { + git_error_set(GIT_ERROR_OS, "failed to get passwd entry"); + goto out; + } + + if (!pwdptr) { + git_error_set(GIT_ERROR_OS, "no passwd entry found for user"); + goto out; + } + + if ((error = git_str_puts(out, pwdptr->pw_dir)) < 0) + goto out; + +out: + git__free(buf); + return error; +} +#endif + +static int git_sysdir_guess_home_dirs(git_str *out) +{ +#ifdef GIT_WIN32 + static const wchar_t *global_tmpls[4] = { + L"%HOME%\\", + L"%HOMEDRIVE%%HOMEPATH%\\", + L"%USERPROFILE%\\", + NULL, + }; + + return find_win32_dirs(out, global_tmpls); +#else + int error; + uid_t uid, euid; + const char *sandbox_id; + + uid = getuid(); + euid = geteuid(); + + /** + * If APP_SANDBOX_CONTAINER_ID is set, we are running in a + * sandboxed environment on macOS. + */ + sandbox_id = getenv("APP_SANDBOX_CONTAINER_ID"); + + /* + * In case we are running setuid, use the configuration + * of the effective user. + * + * If we are running in a sandboxed environment on macOS, + * we have to get the HOME dir from the password entry file. + */ + if (!sandbox_id && uid == euid) + error = git__getenv(out, "HOME"); + else + error = get_passwd_home(out, euid); + + if (error == GIT_ENOTFOUND) { + git_error_clear(); + error = 0; + } + + return error; +#endif +} + +static int git_sysdir_guess_global_dirs(git_str *out) +{ + return git_sysdir_guess_home_dirs(out); +} + +static int git_sysdir_guess_xdg_dirs(git_str *out) +{ +#ifdef GIT_WIN32 + static const wchar_t *global_tmpls[7] = { + L"%XDG_CONFIG_HOME%\\git", + L"%APPDATA%\\git", + L"%LOCALAPPDATA%\\git", + L"%HOME%\\.config\\git", + L"%HOMEDRIVE%%HOMEPATH%\\.config\\git", + L"%USERPROFILE%\\.config\\git", + NULL, + }; + + return find_win32_dirs(out, global_tmpls); +#else + git_str env = GIT_STR_INIT; + int error; + uid_t uid, euid; + + uid = getuid(); + euid = geteuid(); + + /* + * In case we are running setuid, only look up passwd + * directory of the effective user. + */ + if (uid == euid) { + if ((error = git__getenv(&env, "XDG_CONFIG_HOME")) == 0) + error = git_str_joinpath(out, env.ptr, "git"); + + if (error == GIT_ENOTFOUND && (error = git__getenv(&env, "HOME")) == 0) + error = git_str_joinpath(out, env.ptr, ".config/git"); + } else { + if ((error = get_passwd_home(&env, euid)) == 0) + error = git_str_joinpath(out, env.ptr, ".config/git"); + } + + if (error == GIT_ENOTFOUND) { + git_error_clear(); + error = 0; + } + + git_str_dispose(&env); + return error; +#endif +} + +static int git_sysdir_guess_template_dirs(git_str *out) +{ +#ifdef GIT_WIN32 + return git_win32__find_system_dirs(out, "share/git-core/templates"); +#else + return git_str_sets(out, "/usr/share/git-core/templates"); +#endif +} + +struct git_sysdir__dir { + git_str buf; + int (*guess)(git_str *out); +}; + +static struct git_sysdir__dir git_sysdir__dirs[] = { + { GIT_STR_INIT, git_sysdir_guess_system_dirs }, + { GIT_STR_INIT, git_sysdir_guess_global_dirs }, + { GIT_STR_INIT, git_sysdir_guess_xdg_dirs }, + { GIT_STR_INIT, git_sysdir_guess_programdata_dirs }, + { GIT_STR_INIT, git_sysdir_guess_template_dirs }, + { GIT_STR_INIT, git_sysdir_guess_home_dirs } +}; + +static void git_sysdir_global_shutdown(void) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(git_sysdir__dirs); ++i) + git_str_dispose(&git_sysdir__dirs[i].buf); +} + +int git_sysdir_global_init(void) +{ + size_t i; + int error = 0; + + for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); i++) + error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf); + + if (error) + return error; + + return git_runtime_shutdown_register(git_sysdir_global_shutdown); +} + +int git_sysdir_reset(void) +{ + size_t i; + int error = 0; + + for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); ++i) { + git_str_dispose(&git_sysdir__dirs[i].buf); + error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf); + } + + return error; +} + +static int git_sysdir_check_selector(git_sysdir_t which) +{ + if (which < ARRAY_SIZE(git_sysdir__dirs)) + return 0; + + git_error_set(GIT_ERROR_INVALID, "config directory selector out of range"); + return -1; +} + + +int git_sysdir_get(const git_str **out, git_sysdir_t which) +{ + GIT_ASSERT_ARG(out); + + *out = NULL; + + GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which)); + + *out = &git_sysdir__dirs[which].buf; + return 0; +} + +#define PATH_MAGIC "$PATH" + +int git_sysdir_set(git_sysdir_t which, const char *search_path) +{ + const char *expand_path = NULL; + git_str merge = GIT_STR_INIT; + + GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which)); + + if (search_path != NULL) + expand_path = strstr(search_path, PATH_MAGIC); + + /* reset the default if this path has been cleared */ + if (!search_path) + git_sysdir__dirs[which].guess(&git_sysdir__dirs[which].buf); + + /* if $PATH is not referenced, then just set the path */ + if (!expand_path) { + if (search_path) + git_str_sets(&git_sysdir__dirs[which].buf, search_path); + + goto done; + } + + /* otherwise set to join(before $PATH, old value, after $PATH) */ + if (expand_path > search_path) + git_str_set(&merge, search_path, expand_path - search_path); + + if (git_str_len(&git_sysdir__dirs[which].buf)) + git_str_join(&merge, GIT_PATH_LIST_SEPARATOR, + merge.ptr, git_sysdir__dirs[which].buf.ptr); + + expand_path += strlen(PATH_MAGIC); + if (*expand_path) + git_str_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path); + + git_str_swap(&git_sysdir__dirs[which].buf, &merge); + git_str_dispose(&merge); + +done: + if (git_str_oom(&git_sysdir__dirs[which].buf)) + return -1; + + return 0; +} + +static int git_sysdir_find_in_dirlist( + git_str *path, + const char *name, + git_sysdir_t which, + const char *label) +{ + size_t len; + const char *scan, *next = NULL; + const git_str *syspath; + + GIT_ERROR_CHECK_ERROR(git_sysdir_get(&syspath, which)); + if (!syspath || !git_str_len(syspath)) + goto done; + + for (scan = git_str_cstr(syspath); scan; scan = next) { + /* find unescaped separator or end of string */ + for (next = scan; *next; ++next) { + if (*next == GIT_PATH_LIST_SEPARATOR && + (next <= scan || next[-1] != '\\')) + break; + } + + len = (size_t)(next - scan); + next = (*next ? next + 1 : NULL); + if (!len) + continue; + + GIT_ERROR_CHECK_ERROR(git_str_set(path, scan, len)); + if (name) + GIT_ERROR_CHECK_ERROR(git_str_joinpath(path, path->ptr, name)); + + if (git_fs_path_exists(path->ptr)) + return 0; + } + +done: + if (name) + git_error_set(GIT_ERROR_OS, "the %s file '%s' doesn't exist", label, name); + else + git_error_set(GIT_ERROR_OS, "the %s directory doesn't exist", label); + git_str_dispose(path); + return GIT_ENOTFOUND; +} + +int git_sysdir_find_system_file(git_str *path, const char *filename) +{ + return git_sysdir_find_in_dirlist( + path, filename, GIT_SYSDIR_SYSTEM, "system"); +} + +int git_sysdir_find_global_file(git_str *path, const char *filename) +{ + return git_sysdir_find_in_dirlist( + path, filename, GIT_SYSDIR_GLOBAL, "global"); +} + +int git_sysdir_find_xdg_file(git_str *path, const char *filename) +{ + return git_sysdir_find_in_dirlist( + path, filename, GIT_SYSDIR_XDG, "global/xdg"); +} + +int git_sysdir_find_programdata_file(git_str *path, const char *filename) +{ + return git_sysdir_find_in_dirlist( + path, filename, GIT_SYSDIR_PROGRAMDATA, "ProgramData"); +} + +int git_sysdir_find_template_dir(git_str *path) +{ + return git_sysdir_find_in_dirlist( + path, NULL, GIT_SYSDIR_TEMPLATE, "template"); +} + +int git_sysdir_find_homedir(git_str *path) +{ + return git_sysdir_find_in_dirlist( + path, NULL, GIT_SYSDIR_HOME, "home directory"); +} + +int git_sysdir_expand_global_file(git_str *path, const char *filename) +{ + int error; + + if ((error = git_sysdir_find_global_file(path, NULL)) == 0) { + if (filename) + error = git_str_joinpath(path, path->ptr, filename); + } + + return error; +} + +int git_sysdir_expand_homedir_file(git_str *path, const char *filename) +{ + int error; + + if ((error = git_sysdir_find_homedir(path)) == 0) { + if (filename) + error = git_str_joinpath(path, path->ptr, filename); + } + + return error; +} diff --git a/src/libgit2/sysdir.h b/src/libgit2/sysdir.h new file mode 100644 index 00000000000..03f59e1de81 --- /dev/null +++ b/src/libgit2/sysdir.h @@ -0,0 +1,145 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sysdir_h__ +#define INCLUDE_sysdir_h__ + +#include "common.h" + +#include "posix.h" +#include "str.h" + +/** + * Find a "global" file (i.e. one in a user's home directory). + * + * @param path buffer to write the full path into + * @param filename name of file to find in the home directory + * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error + */ +extern int git_sysdir_find_global_file(git_str *path, const char *filename); + +/** + * Find an "XDG" file (i.e. one in user's XDG config path). + * + * @param path buffer to write the full path into + * @param filename name of file to find in the home directory + * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error + */ +extern int git_sysdir_find_xdg_file(git_str *path, const char *filename); + +/** + * Find a "system" file (i.e. one shared for all users of the system). + * + * @param path buffer to write the full path into + * @param filename name of file to find in the home directory + * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error + */ +extern int git_sysdir_find_system_file(git_str *path, const char *filename); + +/** + * Find a "ProgramData" file (i.e. one in %PROGRAMDATA%) + * + * @param path buffer to write the full path into + * @param filename name of file to find in the ProgramData directory + * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error + */ +extern int git_sysdir_find_programdata_file(git_str *path, const char *filename); + +/** + * Find template directory. + * + * @param path buffer to write the full path into + * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error + */ +extern int git_sysdir_find_template_dir(git_str *path); + +/** + * Find the home directory. On Windows, this will look at the `HOME`, + * `HOMEPATH`, and `USERPROFILE` environment variables (in that order) + * and return the first path that is set and exists. On other systems, + * this will simply return the contents of the `HOME` environment variable. + * + * @param path buffer to write the full path into + * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error + */ +extern int git_sysdir_find_homedir(git_str *path); + +/** + * Expand the name of a "global" file -- by default inside the user's + * home directory, but can be overridden by the user configuration. + * Unlike `find_global_file` (above), this makes no attempt to check + * for the existence of the file, and is useful if you want the full + * path regardless of existence. + * + * @param path buffer to write the full path into + * @param filename name of file in the home directory + * @return 0 on success or -1 on error + */ +extern int git_sysdir_expand_global_file(git_str *path, const char *filename); + +/** + * Expand the name of a file in the user's home directory. This + * function makes no attempt to check for the existence of the file, + * and is useful if you want the full path regardless of existence. + * + * @param path buffer to write the full path into + * @param filename name of file in the home directory + * @return 0 on success or -1 on error + */ +extern int git_sysdir_expand_homedir_file(git_str *path, const char *filename); + +typedef enum { + GIT_SYSDIR_SYSTEM = 0, + GIT_SYSDIR_GLOBAL = 1, + GIT_SYSDIR_XDG = 2, + GIT_SYSDIR_PROGRAMDATA = 3, + GIT_SYSDIR_TEMPLATE = 4, + GIT_SYSDIR_HOME = 5, + GIT_SYSDIR__MAX = 6 +} git_sysdir_t; + +/** + * Configures global data for configuration file search paths. + * + * @return 0 on success, <0 on failure + */ +extern int git_sysdir_global_init(void); + +/** + * Get the search path for global/system/xdg files + * + * @param out pointer to git_str containing search path + * @param which which list of paths to return + * @return 0 on success, <0 on failure + */ +extern int git_sysdir_get(const git_str **out, git_sysdir_t which); + +/** + * Set search paths for global/system/xdg files + * + * The first occurrence of the magic string "$PATH" in the new value will + * be replaced with the old value of the search path. + * + * @param which Which search path to modify + * @param paths New search path (separated by GIT_PATH_LIST_SEPARATOR) + * @return 0 on success, <0 on failure (allocation error) + */ +extern int git_sysdir_set(git_sysdir_t which, const char *paths); + +/** + * Reset search paths for global/system/xdg files. + */ +extern int git_sysdir_reset(void); + +#ifdef GIT_WIN32 +/** Sets the registry system dir to a mock; for testing. */ +extern int git_win32__set_registry_system_dir(const wchar_t *mock_sysdir); + +/** Find the given system dir; for testing. */ +extern int git_win32__find_system_dirs(git_str *out, const char *subdir); +#endif + +#endif diff --git a/src/libgit2/tag.c b/src/libgit2/tag.c new file mode 100644 index 00000000000..d12efdb63b5 --- /dev/null +++ b/src/libgit2/tag.c @@ -0,0 +1,601 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "tag.h" + +#include "commit.h" +#include "signature.h" +#include "wildmatch.h" +#include "git2/object.h" +#include "git2/repository.h" +#include "git2/signature.h" +#include "git2/odb_backend.h" + +void git_tag__free(void *_tag) +{ + git_tag *tag = _tag; + git_signature_free(tag->tagger); + git__free(tag->message); + git__free(tag->tag_name); + git__free(tag); +} + +int git_tag_target(git_object **target, const git_tag *t) +{ + GIT_ASSERT_ARG(t); + return git_object_lookup(target, t->object.repo, &t->target, t->type); +} + +const git_oid *git_tag_target_id(const git_tag *t) +{ + GIT_ASSERT_ARG_WITH_RETVAL(t, NULL); + return &t->target; +} + +git_object_t git_tag_target_type(const git_tag *t) +{ + GIT_ASSERT_ARG_WITH_RETVAL(t, GIT_OBJECT_INVALID); + return t->type; +} + +const char *git_tag_name(const git_tag *t) +{ + GIT_ASSERT_ARG_WITH_RETVAL(t, NULL); + return t->tag_name; +} + +const git_signature *git_tag_tagger(const git_tag *t) +{ + return t->tagger; +} + +const char *git_tag_message(const git_tag *t) +{ + GIT_ASSERT_ARG_WITH_RETVAL(t, NULL); + return t->message; +} + +static int tag_error(const char *str) +{ + git_error_set(GIT_ERROR_TAG, "failed to parse tag: %s", str); + return GIT_EINVALID; +} + +static int tag_parse( + git_tag *tag, + const char *buffer, + const char *buffer_end, + git_oid_t oid_type) +{ + static const char *tag_types[] = { + NULL, "commit\n", "tree\n", "blob\n", "tag\n" + }; + size_t text_len, alloc_len; + const char *search; + unsigned int i; + int error; + + if (git_object__parse_oid_header(&tag->target, + &buffer, buffer_end, "object ", oid_type) < 0) + return tag_error("object field invalid"); + + if (buffer + 5 >= buffer_end) + return tag_error("object too short"); + + if (memcmp(buffer, "type ", 5) != 0) + return tag_error("type field not found"); + buffer += 5; + + tag->type = GIT_OBJECT_INVALID; + + for (i = 1; i < ARRAY_SIZE(tag_types); ++i) { + size_t type_length = strlen(tag_types[i]); + + if (buffer + type_length >= buffer_end) + return tag_error("object too short"); + + if (memcmp(buffer, tag_types[i], type_length) == 0) { + tag->type = i; + buffer += type_length; + break; + } + } + + if (tag->type == GIT_OBJECT_INVALID) + return tag_error("invalid object type"); + + if (buffer + 4 >= buffer_end) + return tag_error("object too short"); + + if (memcmp(buffer, "tag ", 4) != 0) + return tag_error("tag field not found"); + + buffer += 4; + + search = memchr(buffer, '\n', buffer_end - buffer); + if (search == NULL) + return tag_error("object too short"); + + text_len = search - buffer; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, text_len, 1); + tag->tag_name = git__malloc(alloc_len); + GIT_ERROR_CHECK_ALLOC(tag->tag_name); + + memcpy(tag->tag_name, buffer, text_len); + tag->tag_name[text_len] = '\0'; + + buffer = search + 1; + + tag->tagger = NULL; + if (buffer < buffer_end && *buffer != '\n') { + tag->tagger = git__malloc(sizeof(git_signature)); + GIT_ERROR_CHECK_ALLOC(tag->tagger); + + if ((error = git_signature__parse(tag->tagger, &buffer, buffer_end, "tagger ", '\n')) < 0) + return error; + } + + tag->message = NULL; + if (buffer < buffer_end) { + /* If we're not at the end of the header, search for it */ + if(*buffer != '\n') { + search = git__memmem(buffer, buffer_end - buffer, + "\n\n", 2); + if (search) + buffer = search + 1; + else + return tag_error("tag contains no message"); + } + + text_len = buffer_end - ++buffer; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, text_len, 1); + tag->message = git__malloc(alloc_len); + GIT_ERROR_CHECK_ALLOC(tag->message); + + memcpy(tag->message, buffer, text_len); + tag->message[text_len] = '\0'; + } + + return 0; +} + +int git_tag__parse_raw( + void *_tag, + const char *data, + size_t size, + git_oid_t oid_type) +{ + return tag_parse(_tag, data, data + size, oid_type); +} + +int git_tag__parse( + void *_tag, + git_odb_object *odb_obj, + git_oid_t oid_type) +{ + git_tag *tag = _tag; + const char *buffer = git_odb_object_data(odb_obj); + const char *buffer_end = buffer + git_odb_object_size(odb_obj); + + return tag_parse(tag, buffer, buffer_end, oid_type); +} + +static int retrieve_tag_reference( + git_reference **tag_reference_out, + git_str *ref_name_out, + git_repository *repo, + const char *tag_name) +{ + git_reference *tag_ref; + int error; + + *tag_reference_out = NULL; + + if (git_str_joinpath(ref_name_out, GIT_REFS_TAGS_DIR, tag_name) < 0) + return -1; + + error = git_reference_lookup(&tag_ref, repo, ref_name_out->ptr); + if (error < 0) + return error; /* Be it not foundo or corrupted */ + + *tag_reference_out = tag_ref; + + return 0; +} + +static int retrieve_tag_reference_oid( + git_oid *oid, + git_str *ref_name_out, + git_repository *repo, + const char *tag_name) +{ + if (git_str_joinpath(ref_name_out, GIT_REFS_TAGS_DIR, tag_name) < 0) + return -1; + + return git_reference_name_to_id(oid, repo, ref_name_out->ptr); +} + +static int write_tag_annotation( + git_oid *oid, + git_repository *repo, + const char *tag_name, + const git_object *target, + const git_signature *tagger, + const char *message) +{ + git_str tag = GIT_STR_INIT; + git_odb *odb; + + if (git_object__write_oid_header(&tag, "object ", git_object_id(target)) < 0) + goto on_error; + + git_str_printf(&tag, "type %s\n", git_object_type2string(git_object_type(target))); + git_str_printf(&tag, "tag %s\n", tag_name); + git_signature__writebuf(&tag, "tagger ", tagger); + git_str_putc(&tag, '\n'); + + if (git_str_puts(&tag, message) < 0) + goto on_error; + + if (git_repository_odb__weakptr(&odb, repo) < 0) + goto on_error; + + if (git_odb_write(oid, odb, tag.ptr, tag.size, GIT_OBJECT_TAG) < 0) + goto on_error; + + git_str_dispose(&tag); + return 0; + +on_error: + git_str_dispose(&tag); + git_error_set(GIT_ERROR_OBJECT, "failed to create tag annotation"); + return -1; +} + +static bool tag_name_is_valid(const char *tag_name) +{ + /* + * Discourage tag name starting with dash, + * https://github.com/git/git/commit/4f0accd638b8d2 + * and refuse to use HEAD as a tagname, + * https://github.com/git/git/commit/bbd445d5efd415 + */ + return tag_name[0] != '-' && git__strcmp(tag_name, "HEAD"); +} + +static int git_tag_create__internal( + git_oid *oid, + git_repository *repo, + const char *tag_name, + const git_object *target, + const git_signature *tagger, + const char *message, + int allow_ref_overwrite, + int create_tag_annotation) +{ + git_reference *new_ref = NULL; + git_str ref_name = GIT_STR_INIT; + + int error; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(tag_name); + GIT_ASSERT_ARG(target); + GIT_ASSERT_ARG(!create_tag_annotation || (tagger && message)); + + if (git_object_owner(target) != repo) { + git_error_set(GIT_ERROR_INVALID, "the given target does not belong to this repository"); + return -1; + } + + if (!tag_name_is_valid(tag_name)) { + git_error_set(GIT_ERROR_TAG, "'%s' is not a valid tag name", tag_name); + return -1; + } + + error = retrieve_tag_reference_oid(oid, &ref_name, repo, tag_name); + if (error < 0 && error != GIT_ENOTFOUND) + goto cleanup; + + /** Ensure the tag name doesn't conflict with an already existing + * reference unless overwriting has explicitly been requested **/ + if (error == 0 && !allow_ref_overwrite) { + git_str_dispose(&ref_name); + git_error_set(GIT_ERROR_TAG, "tag already exists"); + return GIT_EEXISTS; + } + + if (create_tag_annotation) { + if (write_tag_annotation(oid, repo, tag_name, target, tagger, message) < 0) { + git_str_dispose(&ref_name); + return -1; + } + } else + git_oid_cpy(oid, git_object_id(target)); + + error = git_reference_create(&new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite, NULL); + +cleanup: + git_reference_free(new_ref); + git_str_dispose(&ref_name); + return error; +} + +int git_tag_create( + git_oid *oid, + git_repository *repo, + const char *tag_name, + const git_object *target, + const git_signature *tagger, + const char *message, + int allow_ref_overwrite) +{ + return git_tag_create__internal(oid, repo, tag_name, target, tagger, message, allow_ref_overwrite, 1); +} + +int git_tag_annotation_create( + git_oid *oid, + git_repository *repo, + const char *tag_name, + const git_object *target, + const git_signature *tagger, + const char *message) +{ + GIT_ASSERT_ARG(oid); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(tag_name); + GIT_ASSERT_ARG(target); + GIT_ASSERT_ARG(tagger); + GIT_ASSERT_ARG(message); + + return write_tag_annotation(oid, repo, tag_name, target, tagger, message); +} + +int git_tag_create_lightweight( + git_oid *oid, + git_repository *repo, + const char *tag_name, + const git_object *target, + int allow_ref_overwrite) +{ + return git_tag_create__internal(oid, repo, tag_name, target, NULL, NULL, allow_ref_overwrite, 0); +} + +int git_tag_create_from_buffer(git_oid *oid, git_repository *repo, const char *buffer, int allow_ref_overwrite) +{ + git_tag tag; + int error; + git_odb *odb; + git_odb_stream *stream; + git_odb_object *target_obj; + + git_reference *new_ref = NULL; + git_str ref_name = GIT_STR_INIT; + + GIT_ASSERT_ARG(oid); + GIT_ASSERT_ARG(buffer); + + memset(&tag, 0, sizeof(tag)); + + if (git_repository_odb__weakptr(&odb, repo) < 0) + return -1; + + /* validate the buffer */ + if (tag_parse(&tag, buffer, buffer + strlen(buffer), repo->oid_type) < 0) + return -1; + + /* validate the target */ + if (git_odb_read(&target_obj, odb, &tag.target) < 0) + goto on_error; + + if (tag.type != target_obj->cached.type) { + git_error_set(GIT_ERROR_TAG, "the type for the given target is invalid"); + goto on_error; + } + + error = retrieve_tag_reference_oid(oid, &ref_name, repo, tag.tag_name); + if (error < 0 && error != GIT_ENOTFOUND) + goto on_error; + + /* We don't need these objects after this */ + git_signature_free(tag.tagger); + git__free(tag.tag_name); + git__free(tag.message); + git_odb_object_free(target_obj); + + /** Ensure the tag name doesn't conflict with an already existing + * reference unless overwriting has explicitly been requested **/ + if (error == 0 && !allow_ref_overwrite) { + git_str_dispose(&ref_name); + git_error_set(GIT_ERROR_TAG, "tag already exists"); + return GIT_EEXISTS; + } + + /* write the buffer */ + if ((error = git_odb_open_wstream( + &stream, odb, strlen(buffer), GIT_OBJECT_TAG)) < 0) { + git_str_dispose(&ref_name); + return error; + } + + if (!(error = git_odb_stream_write(stream, buffer, strlen(buffer)))) + error = git_odb_stream_finalize_write(oid, stream); + + git_odb_stream_free(stream); + + if (error < 0) { + git_str_dispose(&ref_name); + return error; + } + + error = git_reference_create( + &new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite, NULL); + + git_reference_free(new_ref); + git_str_dispose(&ref_name); + + return error; + +on_error: + git_signature_free(tag.tagger); + git__free(tag.tag_name); + git__free(tag.message); + git_odb_object_free(target_obj); + return -1; +} + +int git_tag_delete(git_repository *repo, const char *tag_name) +{ + git_reference *tag_ref; + git_str ref_name = GIT_STR_INIT; + int error; + + error = retrieve_tag_reference(&tag_ref, &ref_name, repo, tag_name); + + git_str_dispose(&ref_name); + + if (error < 0) + return error; + + error = git_reference_delete(tag_ref); + + git_reference_free(tag_ref); + + return error; +} + +typedef struct { + git_repository *repo; + git_tag_foreach_cb cb; + void *cb_data; +} tag_cb_data; + +static int tags_cb(const char *ref, void *data) +{ + int error; + git_oid oid; + tag_cb_data *d = (tag_cb_data *)data; + + if (git__prefixcmp(ref, GIT_REFS_TAGS_DIR) != 0) + return 0; /* no tag */ + + if (!(error = git_reference_name_to_id(&oid, d->repo, ref))) { + if ((error = d->cb(ref, &oid, d->cb_data)) != 0) + git_error_set_after_callback_function(error, "git_tag_foreach"); + } + + return error; +} + +int git_tag_foreach(git_repository *repo, git_tag_foreach_cb cb, void *cb_data) +{ + tag_cb_data data; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(cb); + + data.cb = cb; + data.cb_data = cb_data; + data.repo = repo; + + return git_reference_foreach_name(repo, &tags_cb, &data); +} + +typedef struct { + git_vector *taglist; + const char *pattern; +} tag_filter_data; + +#define GIT_REFS_TAGS_DIR_LEN strlen(GIT_REFS_TAGS_DIR) + +static int tag_list_cb(const char *tag_name, git_oid *oid, void *data) +{ + tag_filter_data *filter = (tag_filter_data *)data; + GIT_UNUSED(oid); + + if (!*filter->pattern || + wildmatch(filter->pattern, tag_name + GIT_REFS_TAGS_DIR_LEN, 0) == 0) + { + char *matched = git__strdup(tag_name + GIT_REFS_TAGS_DIR_LEN); + GIT_ERROR_CHECK_ALLOC(matched); + + return git_vector_insert(filter->taglist, matched); + } + + return 0; +} + +int git_tag_list_match(git_strarray *tag_names, const char *pattern, git_repository *repo) +{ + int error; + tag_filter_data filter; + git_vector taglist; + + GIT_ASSERT_ARG(tag_names); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(pattern); + + if ((error = git_vector_init(&taglist, 8, NULL)) < 0) + return error; + + filter.taglist = &taglist; + filter.pattern = pattern; + + error = git_tag_foreach(repo, &tag_list_cb, (void *)&filter); + + if (error < 0) + git_vector_dispose(&taglist); + + tag_names->strings = + (char **)git_vector_detach(&tag_names->count, NULL, &taglist); + + return 0; +} + +int git_tag_list(git_strarray *tag_names, git_repository *repo) +{ + return git_tag_list_match(tag_names, "", repo); +} + +int git_tag_peel(git_object **tag_target, const git_tag *tag) +{ + return git_object_peel(tag_target, (const git_object *)tag, GIT_OBJECT_ANY); +} + +int git_tag_name_is_valid(int *valid, const char *name) +{ + git_str ref_name = GIT_STR_INIT; + int error = 0; + + GIT_ASSERT(valid); + + *valid = 0; + + if (!name || !tag_name_is_valid(name)) + goto done; + + if ((error = git_str_puts(&ref_name, GIT_REFS_TAGS_DIR)) < 0 || + (error = git_str_puts(&ref_name, name)) < 0) + goto done; + + error = git_reference_name_is_valid(valid, ref_name.ptr); + +done: + git_str_dispose(&ref_name); + return error; +} + +/* Deprecated Functions */ + +#ifndef GIT_DEPRECATE_HARD +int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *buffer, int allow_ref_overwrite) +{ + return git_tag_create_from_buffer(oid, repo, buffer, allow_ref_overwrite); +} +#endif diff --git a/src/libgit2/tag.h b/src/libgit2/tag.h new file mode 100644 index 00000000000..fdaaa463ccf --- /dev/null +++ b/src/libgit2/tag.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_tag_h__ +#define INCLUDE_tag_h__ + +#include "common.h" + +#include "git2/tag.h" +#include "repository.h" +#include "odb.h" + +struct git_tag { + git_object object; + + git_oid target; + git_object_t type; + + char *tag_name; + git_signature *tagger; + char *message; +}; + +void git_tag__free(void *tag); +int git_tag__parse(void *tag, git_odb_object *obj, git_oid_t oid_type); +int git_tag__parse_raw(void *tag, const char *data, size_t size, git_oid_t oid_type); + +#endif diff --git a/src/libgit2/trace.c b/src/libgit2/trace.c new file mode 100644 index 00000000000..b0c56c4dc66 --- /dev/null +++ b/src/libgit2/trace.c @@ -0,0 +1,25 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "trace.h" + +#include "str.h" +#include "runtime.h" +#include "git2/trace.h" + +struct git_trace_data git_trace__data = {0}; + +int git_trace_set(git_trace_level_t level, git_trace_cb callback) +{ + GIT_ASSERT_ARG(level == 0 || callback != NULL); + + git_trace__data.level = level; + git_trace__data.callback = callback; + GIT_MEMORY_BARRIER; + + return 0; +} diff --git a/src/libgit2/trace.h b/src/libgit2/trace.h new file mode 100644 index 00000000000..239928dcbce --- /dev/null +++ b/src/libgit2/trace.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_trace_h__ +#define INCLUDE_trace_h__ + +#include "common.h" + +#include +#include "str.h" + +struct git_trace_data { + git_trace_level_t level; + git_trace_cb callback; +}; + +extern struct git_trace_data git_trace__data; + +GIT_INLINE(void) git_trace__write_fmt( + git_trace_level_t level, + const char *fmt, + va_list ap) +{ + git_trace_cb callback = git_trace__data.callback; + git_str message = GIT_STR_INIT; + + git_str_vprintf(&message, fmt, ap); + + callback(level, git_str_cstr(&message)); + + git_str_dispose(&message); +} + +#define git_trace_level() (git_trace__data.level) + +GIT_INLINE(void) git_trace(git_trace_level_t level, const char *fmt, ...) +{ + if (git_trace__data.level >= level && + git_trace__data.callback != NULL) { + va_list ap; + + va_start(ap, fmt); + git_trace__write_fmt(level, fmt, ap); + va_end(ap); + } +} + +#endif diff --git a/src/libgit2/trailer.c b/src/libgit2/trailer.c new file mode 100644 index 00000000000..c7579fb3b97 --- /dev/null +++ b/src/libgit2/trailer.c @@ -0,0 +1,430 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#include "array.h" +#include "common.h" +#include "git2/message.h" + +#include +#include +#include + +#define COMMENT_LINE_CHAR '#' +#define TRAILER_SEPARATORS ":" + +static const char *const git_generated_prefixes[] = { + "Signed-off-by: ", + "(cherry picked from commit ", + NULL +}; + +static int is_blank_line(const char *str) +{ + const char *s = str; + while (*s && *s != '\n' && git__isspace(*s)) + s++; + return !*s || *s == '\n'; +} + +static const char *next_line(const char *str) +{ + const char *nl = strchr(str, '\n'); + + if (nl) { + return nl + 1; + } else { + /* return pointer to the NUL terminator: */ + return str + strlen(str); + } +} + +/* + * Return the position of the start of the last line. If len is 0, return 0. + */ +static bool last_line(size_t *out, const char *buf, size_t len) +{ + size_t i; + + *out = 0; + + if (len == 0) + return false; + if (len == 1) + return true; + + /* + * Skip the last character (in addition to the null terminator), + * because if the last character is a newline, it is considered as part + * of the last line anyway. + */ + i = len - 2; + + for (; i > 0; i--) { + if (buf[i] == '\n') { + *out = i + 1; + return true; + } + } + return true; +} + +/* + * If the given line is of the form + * "..." or "...", sets out + * to the location of the separator and returns true. Otherwise, returns + * false. The optional whitespace is allowed there primarily to allow things + * like "Bug #43" where is "Bug" and is "#". + * + * The separator-starts-line case (in which this function returns true and + * sets out to 0) is distinguished from the non-well-formed-line case (in + * which this function returns false) because some callers of this function + * need such a distinction. + */ +static bool find_separator(size_t *out, const char *line, const char *separators) +{ + int whitespace_found = 0; + const char *c; + for (c = line; *c; c++) { + if (strchr(separators, *c)) { + *out = c - line; + return true; + } + + if (!whitespace_found && (git__isalnum(*c) || *c == '-')) + continue; + if (c != line && (*c == ' ' || *c == '\t')) { + whitespace_found = 1; + continue; + } + break; + } + return false; +} + +/* + * Inspect the given string and determine the true "end" of the log message, in + * order to find where to put a new Signed-off-by: line. Ignored are + * trailing comment lines and blank lines. To support "git commit -s + * --amend" on an existing commit, we also ignore "Conflicts:". To + * support "git commit -v", we truncate at cut lines. + * + * Returns the number of bytes from the tail to ignore, to be fed as + * the second parameter to append_signoff(). + */ +static size_t ignore_non_trailer(const char *buf, size_t len) +{ + size_t boc = 0, bol = 0; + int in_old_conflicts_block = 0; + size_t cutoff = len; + + while (bol < cutoff) { + const char *next_line = memchr(buf + bol, '\n', len - bol); + + if (!next_line) + next_line = buf + len; + else + next_line++; + + if (buf[bol] == COMMENT_LINE_CHAR || buf[bol] == '\n') { + /* is this the first of the run of comments? */ + if (!boc) + boc = bol; + /* otherwise, it is just continuing */ + } else if (git__prefixcmp(buf + bol, "Conflicts:\n") == 0) { + in_old_conflicts_block = 1; + if (!boc) + boc = bol; + } else if (in_old_conflicts_block && buf[bol] == '\t') { + ; /* a pathname in the conflicts block */ + } else if (boc) { + /* the previous was not trailing comment */ + boc = 0; + in_old_conflicts_block = 0; + } + bol = next_line - buf; + } + return boc ? len - boc : len - cutoff; +} + +/* + * Return the position of the start of the patch or the length of str if there + * is no patch in the message. + */ +static size_t find_patch_start(const char *str) +{ + const char *s; + + for (s = str; *s; s = next_line(s)) { + if (git__prefixcmp(s, "---") == 0 && git__isspace(s[3])) + return s - str; + } + + return s - str; +} + +/* + * Return the position of the first trailer line or len if there are no + * trailers. + */ +static size_t find_trailer_start(const char *buf, size_t len) +{ + const char *s; + size_t end_of_title, l; + int only_spaces = 1; + int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0; + /* + * Number of possible continuation lines encountered. This will be + * reset to 0 if we encounter a trailer (since those lines are to be + * considered continuations of that trailer), and added to + * non_trailer_lines if we encounter a non-trailer (since those lines + * are to be considered non-trailers). + */ + int possible_continuation_lines = 0; + + /* The first paragraph is the title and cannot be trailers */ + for (s = buf; s < buf + len; s = next_line(s)) { + if (s[0] == COMMENT_LINE_CHAR) + continue; + if (is_blank_line(s)) + break; + } + end_of_title = s - buf; + + /* + * Get the start of the trailers by looking starting from the end for a + * blank line before a set of non-blank lines that (i) are all + * trailers, or (ii) contains at least one Git-generated trailer and + * consists of at least 25% trailers. + */ + l = len; + while (last_line(&l, buf, l) && l >= end_of_title) { + const char *bol = buf + l; + const char *const *p; + size_t separator_pos = 0; + + if (bol[0] == COMMENT_LINE_CHAR) { + non_trailer_lines += possible_continuation_lines; + possible_continuation_lines = 0; + continue; + } + if (is_blank_line(bol)) { + if (only_spaces) + continue; + non_trailer_lines += possible_continuation_lines; + if (recognized_prefix && + trailer_lines * 3 >= non_trailer_lines) + return next_line(bol) - buf; + else if (trailer_lines && !non_trailer_lines) + return next_line(bol) - buf; + return len; + } + only_spaces = 0; + + for (p = git_generated_prefixes; *p; p++) { + if (git__prefixcmp(bol, *p) == 0) { + trailer_lines++; + possible_continuation_lines = 0; + recognized_prefix = 1; + goto continue_outer_loop; + } + } + + find_separator(&separator_pos, bol, TRAILER_SEPARATORS); + if (separator_pos >= 1 && !git__isspace(bol[0])) { + trailer_lines++; + possible_continuation_lines = 0; + if (recognized_prefix) + continue; + } else if (git__isspace(bol[0])) + possible_continuation_lines++; + else { + non_trailer_lines++; + non_trailer_lines += possible_continuation_lines; + possible_continuation_lines = 0; + } +continue_outer_loop: + ; + } + + return len; +} + +/* Return the position of the end of the trailers. */ +static size_t find_trailer_end(const char *buf, size_t len) +{ + return len - ignore_non_trailer(buf, len); +} + +static char *extract_trailer_block(const char *message, size_t *len) +{ + size_t patch_start = find_patch_start(message); + size_t trailer_end = find_trailer_end(message, patch_start); + size_t trailer_start = find_trailer_start(message, trailer_end); + + size_t trailer_len = trailer_end - trailer_start; + + char *buffer = git__malloc(trailer_len + 1); + if (buffer == NULL) + return NULL; + + memcpy(buffer, message + trailer_start, trailer_len); + buffer[trailer_len] = 0; + + *len = trailer_len; + + return buffer; +} + +enum trailer_state { + S_START = 0, + S_KEY = 1, + S_KEY_WS = 2, + S_SEP_WS = 3, + S_VALUE = 4, + S_VALUE_NL = 5, + S_VALUE_END = 6, + S_IGNORE = 7 +}; + +#define NEXT(st) { state = (st); ptr++; continue; } +#define GOTO(st) { state = (st); continue; } + +typedef git_array_t(git_message_trailer) git_array_trailer_t; + +int git_message_trailers(git_message_trailer_array *trailer_arr, const char *message) +{ + enum trailer_state state = S_START; + int rc = 0; + char *ptr; + char *key = NULL; + char *value = NULL; + git_array_trailer_t arr = GIT_ARRAY_INIT; + + size_t trailer_len; + char *trailer = extract_trailer_block(message, &trailer_len); + if (trailer == NULL) + return -1; + + for (ptr = trailer;;) { + switch (state) { + case S_START: { + if (*ptr == 0) { + goto ret; + } + + key = ptr; + GOTO(S_KEY); + } + case S_KEY: { + if (*ptr == 0) { + goto ret; + } + + if (git__isalnum(*ptr) || *ptr == '-') { + /* legal key character */ + NEXT(S_KEY); + } + + if (*ptr == ' ' || *ptr == '\t') { + /* optional whitespace before separator */ + *ptr = 0; + NEXT(S_KEY_WS); + } + + if (strchr(TRAILER_SEPARATORS, *ptr)) { + *ptr = 0; + NEXT(S_SEP_WS); + } + + /* illegal character */ + GOTO(S_IGNORE); + } + case S_KEY_WS: { + if (*ptr == 0) { + goto ret; + } + + if (*ptr == ' ' || *ptr == '\t') { + NEXT(S_KEY_WS); + } + + if (strchr(TRAILER_SEPARATORS, *ptr)) { + NEXT(S_SEP_WS); + } + + /* illegal character */ + GOTO(S_IGNORE); + } + case S_SEP_WS: { + if (*ptr == 0) { + goto ret; + } + + if (*ptr == ' ' || *ptr == '\t') { + NEXT(S_SEP_WS); + } + + value = ptr; + NEXT(S_VALUE); + } + case S_VALUE: { + if (*ptr == 0) { + GOTO(S_VALUE_END); + } + + if (*ptr == '\n') { + NEXT(S_VALUE_NL); + } + + NEXT(S_VALUE); + } + case S_VALUE_NL: { + if (*ptr == ' ') { + /* continuation; */ + NEXT(S_VALUE); + } + + ptr[-1] = 0; + GOTO(S_VALUE_END); + } + case S_VALUE_END: { + git_message_trailer *t = git_array_alloc(arr); + + t->key = key; + t->value = value; + + key = NULL; + value = NULL; + + GOTO(S_START); + } + case S_IGNORE: { + if (*ptr == 0) { + goto ret; + } + + if (*ptr == '\n') { + NEXT(S_START); + } + + NEXT(S_IGNORE); + } + } + } + +ret: + trailer_arr->_trailer_block = trailer; + trailer_arr->trailers = arr.ptr; + trailer_arr->count = arr.size; + + return rc; +} + +void git_message_trailer_array_free(git_message_trailer_array *arr) +{ + git__free(arr->_trailer_block); + git__free(arr->trailers); +} diff --git a/src/libgit2/transaction.c b/src/libgit2/transaction.c new file mode 100644 index 00000000000..1965498c0da --- /dev/null +++ b/src/libgit2/transaction.c @@ -0,0 +1,401 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "transaction.h" + +#include "repository.h" +#include "refdb.h" +#include "pool.h" +#include "reflog.h" +#include "signature.h" +#include "config.h" + +#include "git2/transaction.h" +#include "git2/signature.h" +#include "git2/sys/refs.h" +#include "git2/sys/refdb_backend.h" + +typedef enum { + TRANSACTION_NONE, + TRANSACTION_REFS, + TRANSACTION_CONFIG +} transaction_t; + +typedef struct { + const char *name; + void *payload; + + git_reference_t ref_type; + union { + git_oid id; + char *symbolic; + } target; + git_reflog *reflog; + + const char *message; + git_signature *sig; + + unsigned int committed :1, + remove :1; +} transaction_node; + +GIT_HASHMAP_STR_SETUP(git_transaction_nodemap, transaction_node *); + +struct git_transaction { + transaction_t type; + git_repository *repo; + git_refdb *db; + git_config *cfg; + void *cfg_data; + + git_transaction_nodemap locks; + git_pool pool; +}; + +int git_transaction_config_new( + git_transaction **out, + git_config *cfg, + void *data) +{ + git_transaction *tx; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(cfg); + + tx = git__calloc(1, sizeof(git_transaction)); + GIT_ERROR_CHECK_ALLOC(tx); + + tx->type = TRANSACTION_CONFIG; + tx->cfg = cfg; + tx->cfg_data = data; + + *out = tx; + return 0; +} + +int git_transaction_new(git_transaction **out, git_repository *repo) +{ + int error; + git_pool pool; + git_transaction *tx = NULL; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + + if ((error = git_pool_init(&pool, 1)) < 0) + goto on_error; + + tx = git_pool_mallocz(&pool, sizeof(git_transaction)); + if (!tx) { + error = -1; + goto on_error; + } + + if ((error = git_repository_refdb(&tx->db, repo)) < 0) + goto on_error; + + tx->type = TRANSACTION_REFS; + memcpy(&tx->pool, &pool, sizeof(git_pool)); + tx->repo = repo; + *out = tx; + return 0; + +on_error: + git_pool_clear(&pool); + return error; +} + +int git_transaction_lock_ref(git_transaction *tx, const char *refname) +{ + int error; + transaction_node *node; + + GIT_ASSERT_ARG(tx); + GIT_ASSERT_ARG(refname); + + node = git_pool_mallocz(&tx->pool, sizeof(transaction_node)); + GIT_ERROR_CHECK_ALLOC(node); + + node->name = git_pool_strdup(&tx->pool, refname); + GIT_ERROR_CHECK_ALLOC(node->name); + + if ((error = git_refdb_lock(&node->payload, tx->db, refname)) < 0) + return error; + + if ((error = git_transaction_nodemap_put(&tx->locks, node->name, node)) < 0) + goto cleanup; + + return 0; + +cleanup: + git_refdb_unlock(tx->db, node->payload, false, false, NULL, NULL, NULL); + + return error; +} + +static int find_locked(transaction_node **out, git_transaction *tx, const char *refname) +{ + transaction_node *node; + int error; + + error = git_transaction_nodemap_get(&node, &tx->locks, refname); + + if (error != 0) { + git_error_set(GIT_ERROR_REFERENCE, "the specified reference is not locked"); + return GIT_ENOTFOUND; + } + + *out = node; + return 0; +} + +static int copy_common(transaction_node *node, git_transaction *tx, const git_signature *sig, const char *msg) +{ + if (sig && git_signature__pdup(&node->sig, sig, &tx->pool) < 0) + return -1; + + if (!node->sig) { + git_signature *tmp; + int error; + + if (git_reference__log_signature(&tmp, tx->repo) < 0) + return -1; + + /* make sure the sig we use is in our pool */ + error = git_signature__pdup(&node->sig, tmp, &tx->pool); + git_signature_free(tmp); + if (error < 0) + return error; + } + + if (msg) { + node->message = git_pool_strdup(&tx->pool, msg); + GIT_ERROR_CHECK_ALLOC(node->message); + } + + return 0; +} + +int git_transaction_set_target(git_transaction *tx, const char *refname, const git_oid *target, const git_signature *sig, const char *msg) +{ + int error; + transaction_node *node; + + GIT_ASSERT_ARG(tx); + GIT_ASSERT_ARG(refname); + GIT_ASSERT_ARG(target); + + if ((error = find_locked(&node, tx, refname)) < 0) + return error; + + if ((error = copy_common(node, tx, sig, msg)) < 0) + return error; + + git_oid_cpy(&node->target.id, target); + node->ref_type = GIT_REFERENCE_DIRECT; + + return 0; +} + +int git_transaction_set_symbolic_target(git_transaction *tx, const char *refname, const char *target, const git_signature *sig, const char *msg) +{ + int error; + transaction_node *node; + + GIT_ASSERT_ARG(tx); + GIT_ASSERT_ARG(refname); + GIT_ASSERT_ARG(target); + + if ((error = find_locked(&node, tx, refname)) < 0) + return error; + + if ((error = copy_common(node, tx, sig, msg)) < 0) + return error; + + node->target.symbolic = git_pool_strdup(&tx->pool, target); + GIT_ERROR_CHECK_ALLOC(node->target.symbolic); + node->ref_type = GIT_REFERENCE_SYMBOLIC; + + return 0; +} + +int git_transaction_remove(git_transaction *tx, const char *refname) +{ + int error; + transaction_node *node; + + if ((error = find_locked(&node, tx, refname)) < 0) + return error; + + node->remove = true; + node->ref_type = GIT_REFERENCE_DIRECT; /* the id will be ignored */ + + return 0; +} + +static int dup_reflog(git_reflog **out, const git_reflog *in, git_pool *pool) +{ + git_reflog *reflog; + git_reflog_entry *entries; + size_t len, i; + + reflog = git_pool_mallocz(pool, sizeof(git_reflog)); + GIT_ERROR_CHECK_ALLOC(reflog); + + reflog->ref_name = git_pool_strdup(pool, in->ref_name); + GIT_ERROR_CHECK_ALLOC(reflog->ref_name); + + len = in->entries.length; + reflog->entries.length = len; + reflog->entries.contents = git_pool_mallocz(pool, len * sizeof(void *)); + GIT_ERROR_CHECK_ALLOC(reflog->entries.contents); + + entries = git_pool_mallocz(pool, len * sizeof(git_reflog_entry)); + GIT_ERROR_CHECK_ALLOC(entries); + + for (i = 0; i < len; i++) { + const git_reflog_entry *src; + git_reflog_entry *tgt; + + tgt = &entries[i]; + reflog->entries.contents[i] = tgt; + + src = git_vector_get(&in->entries, i); + git_oid_cpy(&tgt->oid_old, &src->oid_old); + git_oid_cpy(&tgt->oid_cur, &src->oid_cur); + + tgt->msg = git_pool_strdup(pool, src->msg); + GIT_ERROR_CHECK_ALLOC(tgt->msg); + + if (git_signature__pdup(&tgt->committer, src->committer, pool) < 0) + return -1; + } + + + *out = reflog; + return 0; +} + +int git_transaction_set_reflog(git_transaction *tx, const char *refname, const git_reflog *reflog) +{ + int error; + transaction_node *node; + + GIT_ASSERT_ARG(tx); + GIT_ASSERT_ARG(refname); + GIT_ASSERT_ARG(reflog); + + if ((error = find_locked(&node, tx, refname)) < 0) + return error; + + if ((error = dup_reflog(&node->reflog, reflog, &tx->pool)) < 0) + return error; + + return 0; +} + +static int update_target(git_refdb *db, transaction_node *node) +{ + git_reference *ref; + int error, update_reflog; + + if (node->ref_type == GIT_REFERENCE_DIRECT) { + ref = git_reference__alloc(node->name, &node->target.id, NULL); + } else if (node->ref_type == GIT_REFERENCE_SYMBOLIC) { + ref = git_reference__alloc_symbolic(node->name, node->target.symbolic); + } else { + abort(); + } + + GIT_ERROR_CHECK_ALLOC(ref); + update_reflog = node->reflog == NULL; + + if (node->remove) { + error = git_refdb_unlock(db, node->payload, 2, false, ref, NULL, NULL); + } else if (node->ref_type == GIT_REFERENCE_DIRECT) { + error = git_refdb_unlock(db, node->payload, true, update_reflog, ref, node->sig, node->message); + } else if (node->ref_type == GIT_REFERENCE_SYMBOLIC) { + error = git_refdb_unlock(db, node->payload, true, update_reflog, ref, node->sig, node->message); + } else { + abort(); + } + + git_reference_free(ref); + node->committed = true; + + return error; +} + +int git_transaction_commit(git_transaction *tx) +{ + transaction_node *node; + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + int error = 0; + + GIT_ASSERT_ARG(tx); + + if (tx->type == TRANSACTION_CONFIG) { + error = git_config_unlock(tx->cfg, tx->cfg_data, true); + tx->cfg = NULL; + tx->cfg_data = NULL; + + return error; + } + + while (git_transaction_nodemap_iterate(&iter, NULL, &node, &tx->locks) == 0) { + if (node->reflog) { + if ((error = tx->db->backend->reflog_write(tx->db->backend, node->reflog)) < 0) + return error; + } + + if (node->ref_type == GIT_REFERENCE_INVALID) { + /* ref was locked but not modified */ + if ((error = git_refdb_unlock(tx->db, node->payload, false, false, NULL, NULL, NULL)) < 0) { + return error; + } + node->committed = true; + } else { + if ((error = update_target(tx->db, node)) < 0) + return error; + } + } + + return 0; +} + +void git_transaction_free(git_transaction *tx) +{ + transaction_node *node; + git_pool pool; + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + + if (!tx) + return; + + if (tx->type == TRANSACTION_CONFIG) { + if (tx->cfg) + git_config_unlock(tx->cfg, tx->cfg_data, false); + + git__free(tx); + return; + } + + /* start by unlocking the ones we've left hanging, if any */ + while (git_transaction_nodemap_iterate(&iter, NULL, &node, &tx->locks) == 0) { + if (node->committed) + continue; + + git_refdb_unlock(tx->db, node->payload, false, false, NULL, NULL, NULL); + } + + git_refdb_free(tx->db); + git_transaction_nodemap_dispose(&tx->locks); + + /* tx is inside the pool, so we need to extract the data */ + memcpy(&pool, &tx->pool, sizeof(git_pool)); + git_pool_clear(&pool); +} diff --git a/src/libgit2/transaction.h b/src/libgit2/transaction.h new file mode 100644 index 00000000000..cb26017ae9f --- /dev/null +++ b/src/libgit2/transaction.h @@ -0,0 +1,17 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_transaction_h__ +#define INCLUDE_transaction_h__ + +#include "common.h" + +int git_transaction_config_new( + git_transaction **out, + git_config *cfg, + void *data); + +#endif diff --git a/src/libgit2/transport.c b/src/libgit2/transport.c new file mode 100644 index 00000000000..c31fca3a490 --- /dev/null +++ b/src/libgit2/transport.c @@ -0,0 +1,225 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "git2/types.h" +#include "git2/remote.h" +#include "git2/net.h" +#include "git2/transport.h" +#include "git2/sys/transport.h" +#include "fs_path.h" + +typedef struct transport_definition { + char *prefix; + git_transport_cb fn; + void *param; +} transport_definition; + +static git_smart_subtransport_definition http_subtransport_definition = { git_smart_subtransport_http, 1, NULL }; +static git_smart_subtransport_definition git_subtransport_definition = { git_smart_subtransport_git, 0, NULL }; + +#ifdef GIT_SSH +static git_smart_subtransport_definition ssh_subtransport_definition = { git_smart_subtransport_ssh, 0, NULL }; +#endif + +static transport_definition local_transport_definition = { "file://", git_transport_local, NULL }; + +static transport_definition transports[] = { + { "git://", git_transport_smart, &git_subtransport_definition }, + { "http://", git_transport_smart, &http_subtransport_definition }, + { "https://", git_transport_smart, &http_subtransport_definition }, + { "file://", git_transport_local, NULL }, + +#ifdef GIT_SSH + { "ssh://", git_transport_smart, &ssh_subtransport_definition }, + { "ssh+git://", git_transport_smart, &ssh_subtransport_definition }, + { "git+ssh://", git_transport_smart, &ssh_subtransport_definition }, +#endif + + { NULL, 0, 0 } +}; + +static git_vector custom_transports = GIT_VECTOR_INIT; + +#define GIT_TRANSPORT_COUNT (sizeof(transports)/sizeof(transports[0])) - 1 + +static transport_definition * transport_find_by_url(const char *url) +{ + size_t i = 0; + transport_definition *d; + + /* Find a user transport who wants to deal with this URI */ + git_vector_foreach(&custom_transports, i, d) { + if (strncasecmp(url, d->prefix, strlen(d->prefix)) == 0) { + return d; + } + } + + /* Find a system transport for this URI */ + for (i = 0; i < GIT_TRANSPORT_COUNT; ++i) { + d = &transports[i]; + + if (strncasecmp(url, d->prefix, strlen(d->prefix)) == 0) { + return d; + } + } + + return NULL; +} + +static int transport_find_fn( + git_transport_cb *out, + const char *url, + void **param) +{ + transport_definition *definition = transport_find_by_url(url); + +#ifdef GIT_WIN32 + /* On Windows, it might not be possible to discern between absolute local + * and ssh paths - first check if this is a valid local path that points + * to a directory and if so assume local path, else assume SSH */ + + /* Check to see if the path points to a file on the local file system */ + if (!definition && git_fs_path_exists(url) && git_fs_path_isdir(url)) + definition = &local_transport_definition; +#endif + + /* For other systems, perform the SSH check first, to avoid going to the + * filesystem if it is not necessary */ + + /* It could be a SSH remote path. Check to see if there's a : */ + if (!definition && strrchr(url, ':')) { + /* re-search transports again with ssh:// as url + * so that we can find a third party ssh transport */ + definition = transport_find_by_url("ssh://"); + } + +#ifndef GIT_WIN32 + /* Check to see if the path points to a file on the local file system */ + if (!definition && git_fs_path_exists(url) && git_fs_path_isdir(url)) + definition = &local_transport_definition; +#endif + + if (!definition) + return GIT_ENOTFOUND; + + *out = definition->fn; + *param = definition->param; + + return 0; +} + +/************** + * Public API * + **************/ + +int git_transport_new(git_transport **out, git_remote *owner, const char *url) +{ + git_transport_cb fn; + git_transport *transport; + void *param; + int error; + + if ((error = transport_find_fn(&fn, url, ¶m)) == GIT_ENOTFOUND) { + git_error_set(GIT_ERROR_NET, "unsupported URL protocol"); + return -1; + } else if (error < 0) + return error; + + if ((error = fn(&transport, owner, param)) < 0) + return error; + + GIT_ERROR_CHECK_VERSION(transport, GIT_TRANSPORT_VERSION, "git_transport"); + + *out = transport; + + return 0; +} + +int git_transport_register( + const char *scheme, + git_transport_cb cb, + void *param) +{ + git_str prefix = GIT_STR_INIT; + transport_definition *d, *definition = NULL; + size_t i; + int error = 0; + + GIT_ASSERT_ARG(scheme); + GIT_ASSERT_ARG(cb); + + if ((error = git_str_printf(&prefix, "%s://", scheme)) < 0) + goto on_error; + + git_vector_foreach(&custom_transports, i, d) { + if (strcasecmp(d->prefix, prefix.ptr) == 0) { + error = GIT_EEXISTS; + goto on_error; + } + } + + definition = git__calloc(1, sizeof(transport_definition)); + GIT_ERROR_CHECK_ALLOC(definition); + + definition->prefix = git_str_detach(&prefix); + definition->fn = cb; + definition->param = param; + + if (git_vector_insert(&custom_transports, definition) < 0) + goto on_error; + + return 0; + +on_error: + git_str_dispose(&prefix); + git__free(definition); + return error; +} + +int git_transport_unregister(const char *scheme) +{ + git_str prefix = GIT_STR_INIT; + transport_definition *d; + size_t i; + int error = 0; + + GIT_ASSERT_ARG(scheme); + + if ((error = git_str_printf(&prefix, "%s://", scheme)) < 0) + goto done; + + git_vector_foreach(&custom_transports, i, d) { + if (strcasecmp(d->prefix, prefix.ptr) == 0) { + if ((error = git_vector_remove(&custom_transports, i)) < 0) + goto done; + + git__free(d->prefix); + git__free(d); + + if (!custom_transports.length) + git_vector_dispose(&custom_transports); + + error = 0; + goto done; + } + } + + error = GIT_ENOTFOUND; + +done: + git_str_dispose(&prefix); + return error; +} + +int git_transport_init(git_transport *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_transport, GIT_TRANSPORT_INIT); + return 0; +} diff --git a/src/libgit2/transports/auth.c b/src/libgit2/transports/auth.c new file mode 100644 index 00000000000..90b6b124f0e --- /dev/null +++ b/src/libgit2/transports/auth.c @@ -0,0 +1,74 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "auth.h" + +#include "git2/sys/credential.h" + +static int basic_next_token( + git_str *out, + git_http_auth_context *ctx, + git_credential *c) +{ + git_credential_userpass_plaintext *cred; + git_str raw = GIT_STR_INIT; + int error = GIT_EAUTH; + + GIT_UNUSED(ctx); + + if (c->credtype != GIT_CREDENTIAL_USERPASS_PLAINTEXT) { + git_error_set(GIT_ERROR_INVALID, "invalid credential type for basic auth"); + goto on_error; + } + + cred = (git_credential_userpass_plaintext *)c; + + git_str_printf(&raw, "%s:%s", cred->username, cred->password); + + if (git_str_oom(&raw) || + git_str_puts(out, "Basic ") < 0 || + git_str_encode_base64(out, git_str_cstr(&raw), raw.size) < 0) + goto on_error; + + error = 0; + +on_error: + if (raw.size) + git__memzero(raw.ptr, raw.size); + + git_str_dispose(&raw); + return error; +} + +static git_http_auth_context basic_context = { + GIT_HTTP_AUTH_BASIC, + GIT_CREDENTIAL_USERPASS_PLAINTEXT, + 0, + NULL, + basic_next_token, + NULL, + NULL +}; + +int git_http_auth_basic( + git_http_auth_context **out, const git_net_url *url) +{ + GIT_UNUSED(url); + + *out = &basic_context; + return 0; +} + +int git_http_auth_dummy( + git_http_auth_context **out, const git_net_url *url) +{ + GIT_UNUSED(url); + + *out = NULL; + return GIT_PASSTHROUGH; +} + diff --git a/src/libgit2/transports/auth.h b/src/libgit2/transports/auth.h new file mode 100644 index 00000000000..9f6f8fd3b2d --- /dev/null +++ b/src/libgit2/transports/auth.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_transports_auth_h__ +#define INCLUDE_transports_auth_h__ + +#include "common.h" +#include "net.h" + +typedef enum { + GIT_HTTP_AUTH_BASIC = 1, + GIT_HTTP_AUTH_NEGOTIATE = 2, + GIT_HTTP_AUTH_NTLM = 4 +} git_http_auth_t; + +typedef struct git_http_auth_context git_http_auth_context; + +struct git_http_auth_context { + /** Type of scheme */ + git_http_auth_t type; + + /** Supported credentials */ + git_credential_t credtypes; + + /** Connection affinity or request affinity */ + unsigned connection_affinity : 1; + + /** Sets the challenge on the authentication context */ + int (*set_challenge)(git_http_auth_context *ctx, const char *challenge); + + /** Gets the next authentication token from the context */ + int (*next_token)(git_str *out, git_http_auth_context *ctx, git_credential *cred); + + /** Examines if all tokens have been presented. */ + int (*is_complete)(git_http_auth_context *ctx); + + /** Frees the authentication context */ + void (*free)(git_http_auth_context *ctx); +}; + +typedef struct { + /** Type of scheme */ + git_http_auth_t type; + + /** Name of the scheme (as used in the Authorization header) */ + const char *name; + + /** Credential types this scheme supports */ + git_credential_t credtypes; + + /** Function to initialize an authentication context */ + int (*init_context)( + git_http_auth_context **out, + const git_net_url *url); +} git_http_auth_scheme; + +int git_http_auth_dummy( + git_http_auth_context **out, + const git_net_url *url); + +int git_http_auth_basic( + git_http_auth_context **out, + const git_net_url *url); + +#endif diff --git a/src/libgit2/transports/auth_gssapi.c b/src/libgit2/transports/auth_gssapi.c new file mode 100644 index 00000000000..647f3ce3fa0 --- /dev/null +++ b/src/libgit2/transports/auth_gssapi.c @@ -0,0 +1,314 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "auth_negotiate.h" + +#if defined(GIT_AUTH_NEGOTIATE_GSSAPI) || \ + defined(GIT_AUTH_NEGOTIATE_GSSFRAMEWORK) + +#include "git2.h" +#include "auth.h" +#include "git2/sys/credential.h" + +#if defined(GIT_AUTH_NEGOTIATE_GSSFRAMEWORK) +# import +#elif defined(GIT_AUTH_NEGOTIATE_GSSAPI) +# include +# include +#endif + +static gss_OID_desc gssapi_oid_spnego = + { 6, (void *) "\x2b\x06\x01\x05\x05\x02" }; +static gss_OID_desc gssapi_oid_krb5 = + { 9, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" }; + +static gss_OID gssapi_oids[] = + { &gssapi_oid_spnego, &gssapi_oid_krb5, NULL }; + +typedef struct { + git_http_auth_context parent; + unsigned configured : 1, + complete : 1; + git_str target; + char *challenge; + gss_ctx_id_t gss_context; + gss_OID oid; +} http_auth_gssapi_context; + +static void gssapi_err_set( + OM_uint32 status_major, + OM_uint32 status_minor, + const char *message) +{ + gss_buffer_desc buffer = GSS_C_EMPTY_BUFFER; + OM_uint32 status_display, context = 0; + + if (gss_display_status(&status_display, status_major, GSS_C_GSS_CODE, + GSS_C_NO_OID, &context, &buffer) == GSS_S_COMPLETE) { + git_error_set(GIT_ERROR_NET, "%s: %.*s (%d.%d)", + message, (int)buffer.length, (const char *)buffer.value, + status_major, status_minor); + gss_release_buffer(&status_minor, &buffer); + } else { + git_error_set(GIT_ERROR_NET, "%s: unknown negotiate error (%d.%d)", + message, status_major, status_minor); + } +} + +static int gssapi_set_challenge( + git_http_auth_context *c, + const char *challenge) +{ + http_auth_gssapi_context *ctx = (http_auth_gssapi_context *)c; + + GIT_ASSERT_ARG(ctx); + GIT_ASSERT_ARG(challenge); + GIT_ASSERT(ctx->configured); + + git__free(ctx->challenge); + + ctx->challenge = git__strdup(challenge); + GIT_ERROR_CHECK_ALLOC(ctx->challenge); + + return 0; +} + +static void gssapi_context_dispose(http_auth_gssapi_context *ctx) +{ + OM_uint32 status_minor; + + if (ctx->gss_context != GSS_C_NO_CONTEXT) { + gss_delete_sec_context( + &status_minor, &ctx->gss_context, GSS_C_NO_BUFFER); + ctx->gss_context = GSS_C_NO_CONTEXT; + } + + git_str_dispose(&ctx->target); + + git__free(ctx->challenge); + ctx->challenge = NULL; +} + +static int gssapi_next_token( + git_str *buf, + git_http_auth_context *c, + git_credential *cred) +{ + http_auth_gssapi_context *ctx = (http_auth_gssapi_context *)c; + OM_uint32 status_major, status_minor; + gss_buffer_desc target_buffer = GSS_C_EMPTY_BUFFER, + input_token = GSS_C_EMPTY_BUFFER, + output_token = GSS_C_EMPTY_BUFFER; + gss_buffer_t input_token_ptr = GSS_C_NO_BUFFER; + git_str input_buf = GIT_STR_INIT; + gss_name_t server = NULL; + gss_OID mech; + size_t challenge_len; + int error = 0; + + GIT_ASSERT_ARG(buf); + GIT_ASSERT_ARG(ctx); + GIT_ASSERT_ARG(cred); + + GIT_ASSERT(ctx->configured); + GIT_ASSERT(cred->credtype == GIT_CREDENTIAL_DEFAULT); + + if (ctx->complete) + return 0; + + target_buffer.value = (void *)ctx->target.ptr; + target_buffer.length = ctx->target.size; + + status_major = gss_import_name(&status_minor, &target_buffer, + GSS_C_NT_HOSTBASED_SERVICE, &server); + + if (GSS_ERROR(status_major)) { + gssapi_err_set(status_major, status_minor, + "could not parse principal"); + error = -1; + goto done; + } + + challenge_len = ctx->challenge ? strlen(ctx->challenge) : 0; + + if (challenge_len < 9 || memcmp(ctx->challenge, "Negotiate", 9) != 0) { + git_error_set(GIT_ERROR_NET, "server did not request negotiate"); + error = -1; + goto done; + } + + if (challenge_len > 9) { + if (git_str_decode_base64(&input_buf, + ctx->challenge + 10, challenge_len - 10) < 0) { + git_error_set(GIT_ERROR_NET, "invalid negotiate challenge from server"); + error = -1; + goto done; + } + + input_token.value = input_buf.ptr; + input_token.length = input_buf.size; + input_token_ptr = &input_token; + } else if (ctx->gss_context != GSS_C_NO_CONTEXT) { + gssapi_context_dispose(ctx); + } + + mech = &gssapi_oid_spnego; + + status_major = gss_init_sec_context( + &status_minor, + GSS_C_NO_CREDENTIAL, + &ctx->gss_context, + server, + mech, + GSS_C_DELEG_FLAG | GSS_C_MUTUAL_FLAG, + GSS_C_INDEFINITE, + GSS_C_NO_CHANNEL_BINDINGS, + input_token_ptr, + NULL, + &output_token, + NULL, + NULL); + + if (GSS_ERROR(status_major)) { + gssapi_err_set(status_major, status_minor, "negotiate failure"); + error = -1; + goto done; + } + + /* This message merely told us auth was complete; we do not respond. */ + if (status_major == GSS_S_COMPLETE) { + gssapi_context_dispose(ctx); + ctx->complete = 1; + goto done; + } + + if (output_token.length == 0) { + git_error_set(GIT_ERROR_NET, "GSSAPI did not return token"); + error = -1; + goto done; + } + + git_str_puts(buf, "Negotiate "); + git_str_encode_base64(buf, output_token.value, output_token.length); + + if (git_str_oom(buf)) + error = -1; + +done: + gss_release_name(&status_minor, &server); + gss_release_buffer(&status_minor, (gss_buffer_t) &output_token); + git_str_dispose(&input_buf); + return error; +} + +static int gssapi_is_complete(git_http_auth_context *c) +{ + http_auth_gssapi_context *ctx = (http_auth_gssapi_context *)c; + + GIT_ASSERT_ARG(ctx); + + return (ctx->complete == 1); +} + +static void gssapi_context_free(git_http_auth_context *c) +{ + http_auth_gssapi_context *ctx = (http_auth_gssapi_context *)c; + + gssapi_context_dispose(ctx); + + ctx->configured = 0; + ctx->complete = 0; + ctx->oid = NULL; + + git__free(ctx); +} + +static int gssapi_init_context( + http_auth_gssapi_context *ctx, + const git_net_url *url) +{ + OM_uint32 status_major, status_minor; + gss_OID item, *oid; + gss_OID_set mechanism_list; + size_t i; + + /* Query supported mechanisms looking for SPNEGO) */ + status_major = gss_indicate_mechs(&status_minor, &mechanism_list); + + if (GSS_ERROR(status_major)) { + gssapi_err_set(status_major, status_minor, + "could not query mechanisms"); + return -1; + } + + if (mechanism_list) { + for (oid = gssapi_oids; *oid; oid++) { + for (i = 0; i < mechanism_list->count; i++) { + item = &mechanism_list->elements[i]; + + if (item->length == (*oid)->length && + memcmp(item->elements, (*oid)->elements, item->length) == 0) { + ctx->oid = *oid; + break; + } + + } + + if (ctx->oid) + break; + } + } + + gss_release_oid_set(&status_minor, &mechanism_list); + + if (!ctx->oid) { + git_error_set(GIT_ERROR_NET, "negotiate authentication is not supported"); + return GIT_EAUTH; + } + + git_str_puts(&ctx->target, "HTTP@"); + git_str_puts(&ctx->target, url->host); + + if (git_str_oom(&ctx->target)) + return -1; + + ctx->gss_context = GSS_C_NO_CONTEXT; + ctx->configured = 1; + + return 0; +} + +int git_http_auth_negotiate( + git_http_auth_context **out, + const git_net_url *url) +{ + http_auth_gssapi_context *ctx; + + *out = NULL; + + ctx = git__calloc(1, sizeof(http_auth_gssapi_context)); + GIT_ERROR_CHECK_ALLOC(ctx); + + if (gssapi_init_context(ctx, url) < 0) { + git__free(ctx); + return -1; + } + + ctx->parent.type = GIT_HTTP_AUTH_NEGOTIATE; + ctx->parent.credtypes = GIT_CREDENTIAL_DEFAULT; + ctx->parent.connection_affinity = 1; + ctx->parent.set_challenge = gssapi_set_challenge; + ctx->parent.next_token = gssapi_next_token; + ctx->parent.is_complete = gssapi_is_complete; + ctx->parent.free = gssapi_context_free; + + *out = (git_http_auth_context *)ctx; + + return 0; +} + +#endif /* GIT_AUTH_NEGOTIATE_GSS... */ diff --git a/src/libgit2/transports/auth_negotiate.h b/src/libgit2/transports/auth_negotiate.h new file mode 100644 index 00000000000..e528b402a9a --- /dev/null +++ b/src/libgit2/transports/auth_negotiate.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_transports_auth_negotiate_h__ +#define INCLUDE_transports_auth_negotiate_h__ + +#include "common.h" +#include "git2.h" +#include "auth.h" + +#ifdef GIT_AUTH_NEGOTIATE + +extern int git_http_auth_negotiate( + git_http_auth_context **out, + const git_net_url *url); + +#else + +#define git_http_auth_negotiate git_http_auth_dummy + +#endif /* GIT_AUTH_NEGOTIATE */ + +#endif diff --git a/src/libgit2/transports/auth_ntlm.h b/src/libgit2/transports/auth_ntlm.h new file mode 100644 index 00000000000..d83d1c4cd4d --- /dev/null +++ b/src/libgit2/transports/auth_ntlm.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_transports_auth_ntlm_h__ +#define INCLUDE_transports_auth_ntlm_h__ + +#include "auth.h" + +/* NTLM requires a full request/challenge/response */ +#define GIT_AUTH_STEPS_NTLM 2 + +#if defined(GIT_AUTH_NTLM) + +extern int git_http_auth_ntlm( + git_http_auth_context **out, + const git_net_url *url); + +#else + +#define git_http_auth_ntlm git_http_auth_dummy + +#endif /* GIT_AUTH_NTLM */ + +#endif + diff --git a/src/libgit2/transports/auth_ntlmclient.c b/src/libgit2/transports/auth_ntlmclient.c new file mode 100644 index 00000000000..b8c6e2353c1 --- /dev/null +++ b/src/libgit2/transports/auth_ntlmclient.c @@ -0,0 +1,227 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "auth_ntlm.h" + +#include "common.h" +#include "str.h" +#include "auth.h" +#include "git2/sys/credential.h" + +#ifdef GIT_AUTH_NTLM_BUILTIN + +#include "ntlmclient.h" + +typedef struct { + git_http_auth_context parent; + ntlm_client *ntlm; + char *challenge; + bool complete; +} http_auth_ntlm_context; + +static int ntlmclient_set_challenge( + git_http_auth_context *c, + const char *challenge) +{ + http_auth_ntlm_context *ctx = (http_auth_ntlm_context *)c; + + GIT_ASSERT_ARG(ctx); + GIT_ASSERT_ARG(challenge); + + git__free(ctx->challenge); + + ctx->challenge = git__strdup(challenge); + GIT_ERROR_CHECK_ALLOC(ctx->challenge); + + return 0; +} + +static int ntlmclient_set_credentials(http_auth_ntlm_context *ctx, git_credential *_cred) +{ + git_credential_userpass_plaintext *cred; + const char *sep, *username; + char *domain = NULL, *domainuser = NULL; + int error = 0; + + GIT_ASSERT(_cred->credtype == GIT_CREDENTIAL_USERPASS_PLAINTEXT); + cred = (git_credential_userpass_plaintext *)_cred; + + if ((sep = strchr(cred->username, '\\')) != NULL) { + domain = git__strndup(cred->username, (sep - cred->username)); + GIT_ERROR_CHECK_ALLOC(domain); + + domainuser = git__strdup(sep + 1); + GIT_ERROR_CHECK_ALLOC(domainuser); + + username = domainuser; + } else { + username = cred->username; + } + + if (ntlm_client_set_credentials(ctx->ntlm, + username, domain, cred->password) < 0) { + git_error_set(GIT_ERROR_NET, "could not set credentials: %s", + ntlm_client_errmsg(ctx->ntlm)); + error = -1; + goto done; + } + +done: + git__free(domain); + git__free(domainuser); + return error; +} + +static int ntlmclient_next_token( + git_str *buf, + git_http_auth_context *c, + git_credential *cred) +{ + http_auth_ntlm_context *ctx = (http_auth_ntlm_context *)c; + git_str input_buf = GIT_STR_INIT; + const unsigned char *msg; + size_t challenge_len, msg_len; + int error = GIT_EAUTH; + + GIT_ASSERT_ARG(buf); + GIT_ASSERT_ARG(ctx); + + GIT_ASSERT(ctx->ntlm); + + challenge_len = ctx->challenge ? strlen(ctx->challenge) : 0; + + if (ctx->complete) + ntlm_client_reset(ctx->ntlm); + + /* + * Set us complete now since it's the default case; the one + * incomplete case (successfully created a client request) + * will explicitly set that it requires a second step. + */ + ctx->complete = true; + + if (cred && ntlmclient_set_credentials(ctx, cred) != 0) + goto done; + + if (challenge_len < 4) { + git_error_set(GIT_ERROR_NET, "no ntlm challenge sent from server"); + goto done; + } else if (challenge_len == 4) { + if (memcmp(ctx->challenge, "NTLM", 4) != 0) { + git_error_set(GIT_ERROR_NET, "server did not request NTLM"); + goto done; + } + + if (ntlm_client_negotiate(&msg, &msg_len, ctx->ntlm) != 0) { + git_error_set(GIT_ERROR_NET, "ntlm authentication failed: %s", + ntlm_client_errmsg(ctx->ntlm)); + goto done; + } + + ctx->complete = false; + } else { + if (memcmp(ctx->challenge, "NTLM ", 5) != 0) { + git_error_set(GIT_ERROR_NET, "challenge from server was not NTLM"); + goto done; + } + + if (git_str_decode_base64(&input_buf, + ctx->challenge + 5, challenge_len - 5) < 0) { + git_error_set(GIT_ERROR_NET, "invalid NTLM challenge from server"); + goto done; + } + + if (ntlm_client_set_challenge(ctx->ntlm, + (const unsigned char *)input_buf.ptr, input_buf.size) != 0) { + git_error_set(GIT_ERROR_NET, "ntlm challenge failed: %s", + ntlm_client_errmsg(ctx->ntlm)); + goto done; + } + + if (ntlm_client_response(&msg, &msg_len, ctx->ntlm) != 0) { + git_error_set(GIT_ERROR_NET, "ntlm authentication failed: %s", + ntlm_client_errmsg(ctx->ntlm)); + goto done; + } + } + + git_str_puts(buf, "NTLM "); + git_str_encode_base64(buf, (const char *)msg, msg_len); + + if (git_str_oom(buf)) + goto done; + + error = 0; + +done: + git_str_dispose(&input_buf); + return error; +} + +static int ntlmclient_is_complete(git_http_auth_context *c) +{ + http_auth_ntlm_context *ctx = (http_auth_ntlm_context *)c; + + GIT_ASSERT_ARG(ctx); + return (ctx->complete == true); +} + +static void ntlmclient_context_free(git_http_auth_context *c) +{ + http_auth_ntlm_context *ctx = (http_auth_ntlm_context *)c; + + ntlm_client_free(ctx->ntlm); + git__free(ctx->challenge); + git__free(ctx); +} + +static int ntlmclient_init_context( + http_auth_ntlm_context *ctx, + const git_net_url *url) +{ + GIT_UNUSED(url); + + if ((ctx->ntlm = ntlm_client_init(NTLM_CLIENT_DEFAULTS)) == NULL) { + git_error_set_oom(); + return -1; + } + + return 0; +} + +int git_http_auth_ntlm( + git_http_auth_context **out, + const git_net_url *url) +{ + http_auth_ntlm_context *ctx; + + GIT_UNUSED(url); + + *out = NULL; + + ctx = git__calloc(1, sizeof(http_auth_ntlm_context)); + GIT_ERROR_CHECK_ALLOC(ctx); + + if (ntlmclient_init_context(ctx, url) < 0) { + git__free(ctx); + return -1; + } + + ctx->parent.type = GIT_HTTP_AUTH_NTLM; + ctx->parent.credtypes = GIT_CREDENTIAL_USERPASS_PLAINTEXT; + ctx->parent.connection_affinity = 1; + ctx->parent.set_challenge = ntlmclient_set_challenge; + ctx->parent.next_token = ntlmclient_next_token; + ctx->parent.is_complete = ntlmclient_is_complete; + ctx->parent.free = ntlmclient_context_free; + + *out = (git_http_auth_context *)ctx; + + return 0; +} + +#endif /* GIT_AUTH_NTLM_BUILTIN */ diff --git a/src/libgit2/transports/auth_sspi.c b/src/libgit2/transports/auth_sspi.c new file mode 100644 index 00000000000..ae8343e21d7 --- /dev/null +++ b/src/libgit2/transports/auth_sspi.c @@ -0,0 +1,345 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "auth_ntlm.h" +#include "auth_negotiate.h" + +#ifdef GIT_WIN32 + +#define SECURITY_WIN32 + +#include "git2.h" +#include "auth.h" +#include "git2/sys/credential.h" + +#include +#include + +typedef struct { + git_http_auth_context parent; + wchar_t *target; + + const char *package_name; + size_t package_name_len; + wchar_t *package_name_w; + SecPkgInfoW *package_info; + SEC_WINNT_AUTH_IDENTITY_W identity; + CredHandle cred; + CtxtHandle context; + + int has_identity : 1, + has_credentials : 1, + has_context : 1, + complete : 1; + git_str challenge; +} http_auth_sspi_context; + +static void sspi_reset_context(http_auth_sspi_context *ctx) +{ + if (ctx->has_identity) { + git__free(ctx->identity.User); + git__free(ctx->identity.Domain); + git__free(ctx->identity.Password); + + memset(&ctx->identity, 0, sizeof(SEC_WINNT_AUTH_IDENTITY_W)); + + ctx->has_identity = 0; + } + + if (ctx->has_credentials) { + FreeCredentialsHandle(&ctx->cred); + memset(&ctx->cred, 0, sizeof(CredHandle)); + + ctx->has_credentials = 0; + } + + if (ctx->has_context) { + DeleteSecurityContext(&ctx->context); + memset(&ctx->context, 0, sizeof(CtxtHandle)); + + ctx->has_context = 0; + } + + ctx->complete = 0; + + git_str_dispose(&ctx->challenge); +} + +static int sspi_set_challenge( + git_http_auth_context *c, + const char *challenge) +{ + http_auth_sspi_context *ctx = (http_auth_sspi_context *)c; + size_t challenge_len = strlen(challenge); + + git_str_clear(&ctx->challenge); + + if (strncmp(challenge, ctx->package_name, ctx->package_name_len) != 0) { + git_error_set(GIT_ERROR_NET, "invalid %s challenge from server", ctx->package_name); + return -1; + } + + /* + * A package type indicator without a base64 payload indicates the + * mechanism; it's not an actual challenge. Ignore it. + */ + if (challenge[ctx->package_name_len] == 0) { + return 0; + } else if (challenge[ctx->package_name_len] != ' ') { + git_error_set(GIT_ERROR_NET, "invalid %s challenge from server", ctx->package_name); + return -1; + } + + if (git_str_decode_base64(&ctx->challenge, + challenge + (ctx->package_name_len + 1), + challenge_len - (ctx->package_name_len + 1)) < 0) { + git_error_set(GIT_ERROR_NET, "invalid %s challenge from server", ctx->package_name); + return -1; + } + + GIT_ASSERT(ctx->challenge.size <= ULONG_MAX); + return 0; +} + +static int create_identity( + SEC_WINNT_AUTH_IDENTITY_W **out, + http_auth_sspi_context *ctx, + git_credential *cred) +{ + git_credential_userpass_plaintext *userpass; + wchar_t *username = NULL, *domain = NULL, *password = NULL; + int username_len = 0, domain_len = 0, password_len = 0; + const char *sep; + + if (cred->credtype == GIT_CREDENTIAL_DEFAULT) { + *out = NULL; + return 0; + } + + if (cred->credtype != GIT_CREDENTIAL_USERPASS_PLAINTEXT) { + git_error_set(GIT_ERROR_NET, "unknown credential type: %d", cred->credtype); + return -1; + } + + userpass = (git_credential_userpass_plaintext *)cred; + + if ((sep = strchr(userpass->username, '\\')) != NULL) { + GIT_ASSERT(sep - userpass->username < INT_MAX); + + username_len = git_utf8_to_16_alloc(&username, sep + 1); + domain_len = git_utf8_to_16_alloc_with_len(&domain, + userpass->username, (int)(sep - userpass->username)); + } else { + username_len = git_utf8_to_16_alloc(&username, + userpass->username); + } + + password_len = git_utf8_to_16_alloc(&password, userpass->password); + + if (username_len < 0 || domain_len < 0 || password_len < 0) { + git__free(username); + git__free(domain); + git__free(password); + return -1; + } + + ctx->identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; + ctx->identity.User = username; + ctx->identity.UserLength = (unsigned long)username_len; + ctx->identity.Password = password; + ctx->identity.PasswordLength = (unsigned long)password_len; + ctx->identity.Domain = domain; + ctx->identity.DomainLength = (unsigned long)domain_len; + + ctx->has_identity = 1; + + *out = &ctx->identity; + + return 0; +} + +static int sspi_next_token( + git_str *buf, + git_http_auth_context *c, + git_credential *cred) +{ + http_auth_sspi_context *ctx = (http_auth_sspi_context *)c; + SEC_WINNT_AUTH_IDENTITY_W *identity = NULL; + TimeStamp timestamp; + DWORD context_flags; + SecBuffer input_buf = { 0, SECBUFFER_TOKEN, NULL }; + SecBuffer output_buf = { 0, SECBUFFER_TOKEN, NULL }; + SecBufferDesc input_buf_desc = { SECBUFFER_VERSION, 1, &input_buf }; + SecBufferDesc output_buf_desc = { SECBUFFER_VERSION, 1, &output_buf }; + SECURITY_STATUS status; + + if (ctx->complete) + sspi_reset_context(ctx); + + if (!ctx->has_context) { + if (create_identity(&identity, ctx, cred) < 0) + return -1; + + status = AcquireCredentialsHandleW(NULL, ctx->package_name_w, + SECPKG_CRED_BOTH, NULL, identity, NULL, + NULL, &ctx->cred, ×tamp); + + if (status != SEC_E_OK) { + git_error_set(GIT_ERROR_OS, "could not acquire credentials"); + return -1; + } + + ctx->has_credentials = 1; + } + + context_flags = ISC_REQ_ALLOCATE_MEMORY | + ISC_REQ_CONFIDENTIALITY | + ISC_REQ_MUTUAL_AUTH; + + if (ctx->challenge.size > 0) { + input_buf.BufferType = SECBUFFER_TOKEN; + input_buf.cbBuffer = (unsigned long)ctx->challenge.size; + input_buf.pvBuffer = ctx->challenge.ptr; + } + + status = InitializeSecurityContextW(&ctx->cred, + ctx->has_context ? &ctx->context : NULL, + ctx->target, + context_flags, + 0, + SECURITY_NETWORK_DREP, + ctx->has_context ? &input_buf_desc : NULL, + 0, + ctx->has_context ? NULL : &ctx->context, + &output_buf_desc, + &context_flags, + NULL); + + if (status == SEC_I_COMPLETE_AND_CONTINUE || + status == SEC_I_COMPLETE_NEEDED) + status = CompleteAuthToken(&ctx->context, &output_buf_desc); + + if (status == SEC_E_OK) { + ctx->complete = 1; + } else if (status != SEC_I_CONTINUE_NEEDED) { + git_error_set(GIT_ERROR_OS, "could not initialize security context"); + return -1; + } + + ctx->has_context = 1; + git_str_clear(&ctx->challenge); + + if (output_buf.cbBuffer > 0) { + git_str_put(buf, ctx->package_name, ctx->package_name_len); + git_str_putc(buf, ' '); + git_str_encode_base64(buf, output_buf.pvBuffer, output_buf.cbBuffer); + + FreeContextBuffer(output_buf.pvBuffer); + + if (git_str_oom(buf)) + return -1; + } + + return 0; +} + +static int sspi_is_complete(git_http_auth_context *c) +{ + http_auth_sspi_context *ctx = (http_auth_sspi_context *)c; + + return ctx->complete; +} + +static void sspi_context_free(git_http_auth_context *c) +{ + http_auth_sspi_context *ctx = (http_auth_sspi_context *)c; + + sspi_reset_context(ctx); + + FreeContextBuffer(ctx->package_info); + git__free(ctx->target); + git__free(ctx); +} + +static int sspi_init_context( + git_http_auth_context **out, + git_http_auth_t type, + const git_net_url *url) +{ + http_auth_sspi_context *ctx; + git_str target = GIT_STR_INIT; + + *out = NULL; + + ctx = git__calloc(1, sizeof(http_auth_sspi_context)); + GIT_ERROR_CHECK_ALLOC(ctx); + + switch (type) { + case GIT_HTTP_AUTH_NTLM: + ctx->package_name = "NTLM"; + ctx->package_name_len = CONST_STRLEN("NTLM"); + ctx->package_name_w = L"NTLM"; + ctx->parent.credtypes = GIT_CREDENTIAL_USERPASS_PLAINTEXT | + GIT_CREDENTIAL_DEFAULT; + break; + case GIT_HTTP_AUTH_NEGOTIATE: + ctx->package_name = "Negotiate"; + ctx->package_name_len = CONST_STRLEN("Negotiate"); + ctx->package_name_w = L"Negotiate"; + ctx->parent.credtypes = GIT_CREDENTIAL_DEFAULT; + break; + default: + git_error_set(GIT_ERROR_NET, "unknown SSPI auth type: %d", ctx->parent.type); + git__free(ctx); + return -1; + } + + if (QuerySecurityPackageInfoW(ctx->package_name_w, &ctx->package_info) != SEC_E_OK) { + git_error_set(GIT_ERROR_OS, "could not query security package"); + git__free(ctx); + return -1; + } + + if (git_str_printf(&target, "http/%s", url->host) < 0 || + git_utf8_to_16_alloc(&ctx->target, target.ptr) < 0) { + FreeContextBuffer(ctx->package_info); + git__free(ctx); + return -1; + } + + ctx->parent.type = type; + ctx->parent.connection_affinity = 1; + ctx->parent.set_challenge = sspi_set_challenge; + ctx->parent.next_token = sspi_next_token; + ctx->parent.is_complete = sspi_is_complete; + ctx->parent.free = sspi_context_free; + + *out = (git_http_auth_context *)ctx; + + git_str_dispose(&target); + return 0; +} + +#ifdef GIT_AUTH_NEGOTIATE +int git_http_auth_negotiate( + git_http_auth_context **out, + const git_net_url *url) +{ + return sspi_init_context(out, GIT_HTTP_AUTH_NEGOTIATE, url); +} +#endif + +#ifdef GIT_AUTH_NTLM +int git_http_auth_ntlm( + git_http_auth_context **out, + const git_net_url *url) +{ + return sspi_init_context(out, GIT_HTTP_AUTH_NTLM, url); +} +#endif + +#endif /* GIT_WIN32 */ diff --git a/src/libgit2/transports/credential.c b/src/libgit2/transports/credential.c new file mode 100644 index 00000000000..7d0eacecf30 --- /dev/null +++ b/src/libgit2/transports/credential.c @@ -0,0 +1,486 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "git2/credential.h" +#include "git2/sys/credential.h" +#include "git2/credential_helpers.h" + +static int git_credential_ssh_key_type_new( + git_credential **cred, + const char *username, + const char *publickey, + const char *privatekey, + const char *passphrase, + git_credential_t credtype); + +int git_credential_has_username(git_credential *cred) +{ + if (cred->credtype == GIT_CREDENTIAL_DEFAULT) + return 0; + + return 1; +} + +const char *git_credential_get_username(git_credential *cred) +{ + switch (cred->credtype) { + case GIT_CREDENTIAL_USERNAME: + { + git_credential_username *c = (git_credential_username *) cred; + return c->username; + } + case GIT_CREDENTIAL_USERPASS_PLAINTEXT: + { + git_credential_userpass_plaintext *c = (git_credential_userpass_plaintext *) cred; + return c->username; + } + case GIT_CREDENTIAL_SSH_KEY: + case GIT_CREDENTIAL_SSH_MEMORY: + { + git_credential_ssh_key *c = (git_credential_ssh_key *) cred; + return c->username; + } + case GIT_CREDENTIAL_SSH_CUSTOM: + { + git_credential_ssh_custom *c = (git_credential_ssh_custom *) cred; + return c->username; + } + case GIT_CREDENTIAL_SSH_INTERACTIVE: + { + git_credential_ssh_interactive *c = (git_credential_ssh_interactive *) cred; + return c->username; + } + + default: + return NULL; + } +} + +static void plaintext_free(struct git_credential *cred) +{ + git_credential_userpass_plaintext *c = (git_credential_userpass_plaintext *)cred; + + git__free(c->username); + + /* Zero the memory which previously held the password */ + if (c->password) { + size_t pass_len = strlen(c->password); + git__memzero(c->password, pass_len); + git__free(c->password); + } + + git__free(c); +} + +int git_credential_userpass_plaintext_new( + git_credential **cred, + const char *username, + const char *password) +{ + git_credential_userpass_plaintext *c; + + GIT_ASSERT_ARG(cred); + GIT_ASSERT_ARG(username); + GIT_ASSERT_ARG(password); + + c = git__malloc(sizeof(git_credential_userpass_plaintext)); + GIT_ERROR_CHECK_ALLOC(c); + + c->parent.credtype = GIT_CREDENTIAL_USERPASS_PLAINTEXT; + c->parent.free = plaintext_free; + c->username = git__strdup(username); + + if (!c->username) { + git__free(c); + return -1; + } + + c->password = git__strdup(password); + + if (!c->password) { + git__free(c->username); + git__free(c); + return -1; + } + + *cred = &c->parent; + return 0; +} + +static void ssh_key_free(struct git_credential *cred) +{ + git_credential_ssh_key *c = + (git_credential_ssh_key *)cred; + + git__free(c->username); + + if (c->privatekey) { + /* Zero the memory which previously held the private key */ + size_t key_len = strlen(c->privatekey); + git__memzero(c->privatekey, key_len); + git__free(c->privatekey); + } + + if (c->passphrase) { + /* Zero the memory which previously held the passphrase */ + size_t pass_len = strlen(c->passphrase); + git__memzero(c->passphrase, pass_len); + git__free(c->passphrase); + } + + if (c->publickey) { + /* Zero the memory which previously held the public key */ + size_t key_len = strlen(c->publickey); + git__memzero(c->publickey, key_len); + git__free(c->publickey); + } + + git__free(c); +} + +static void ssh_interactive_free(struct git_credential *cred) +{ + git_credential_ssh_interactive *c = (git_credential_ssh_interactive *)cred; + + git__free(c->username); + + git__free(c); +} + +static void ssh_custom_free(struct git_credential *cred) +{ + git_credential_ssh_custom *c = (git_credential_ssh_custom *)cred; + + git__free(c->username); + + if (c->publickey) { + /* Zero the memory which previously held the publickey */ + size_t key_len = c->publickey_len; + git__memzero(c->publickey, key_len); + git__free(c->publickey); + } + + git__free(c); +} + +static void default_free(struct git_credential *cred) +{ + git_credential_default *c = (git_credential_default *)cred; + + git__free(c); +} + +static void username_free(struct git_credential *cred) +{ + git__free(cred); +} + +int git_credential_ssh_key_new( + git_credential **cred, + const char *username, + const char *publickey, + const char *privatekey, + const char *passphrase) +{ + return git_credential_ssh_key_type_new( + cred, + username, + publickey, + privatekey, + passphrase, + GIT_CREDENTIAL_SSH_KEY); +} + +int git_credential_ssh_key_memory_new( + git_credential **cred, + const char *username, + const char *publickey, + const char *privatekey, + const char *passphrase) +{ +#ifdef GIT_SSH_LIBSSH2_MEMORY_CREDENTIALS + return git_credential_ssh_key_type_new( + cred, + username, + publickey, + privatekey, + passphrase, + GIT_CREDENTIAL_SSH_MEMORY); +#else + GIT_UNUSED(cred); + GIT_UNUSED(username); + GIT_UNUSED(publickey); + GIT_UNUSED(privatekey); + GIT_UNUSED(passphrase); + + git_error_set(GIT_ERROR_INVALID, + "this version of libgit2 was not built with ssh memory credentials."); + return -1; +#endif +} + +static int git_credential_ssh_key_type_new( + git_credential **cred, + const char *username, + const char *publickey, + const char *privatekey, + const char *passphrase, + git_credential_t credtype) +{ + git_credential_ssh_key *c; + + GIT_ASSERT_ARG(username); + GIT_ASSERT_ARG(cred); + GIT_ASSERT_ARG(privatekey); + + c = git__calloc(1, sizeof(git_credential_ssh_key)); + GIT_ERROR_CHECK_ALLOC(c); + + c->parent.credtype = credtype; + c->parent.free = ssh_key_free; + + c->username = git__strdup(username); + GIT_ERROR_CHECK_ALLOC(c->username); + + c->privatekey = git__strdup(privatekey); + GIT_ERROR_CHECK_ALLOC(c->privatekey); + + if (publickey) { + c->publickey = git__strdup(publickey); + GIT_ERROR_CHECK_ALLOC(c->publickey); + } + + if (passphrase) { + c->passphrase = git__strdup(passphrase); + GIT_ERROR_CHECK_ALLOC(c->passphrase); + } + + *cred = &c->parent; + return 0; +} + +int git_credential_ssh_interactive_new( + git_credential **out, + const char *username, + git_credential_ssh_interactive_cb prompt_callback, + void *payload) +{ + git_credential_ssh_interactive *c; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(username); + GIT_ASSERT_ARG(prompt_callback); + + c = git__calloc(1, sizeof(git_credential_ssh_interactive)); + GIT_ERROR_CHECK_ALLOC(c); + + c->parent.credtype = GIT_CREDENTIAL_SSH_INTERACTIVE; + c->parent.free = ssh_interactive_free; + + c->username = git__strdup(username); + GIT_ERROR_CHECK_ALLOC(c->username); + + c->prompt_callback = prompt_callback; + c->payload = payload; + + *out = &c->parent; + return 0; +} + +int git_credential_ssh_key_from_agent(git_credential **cred, const char *username) { + git_credential_ssh_key *c; + + GIT_ASSERT_ARG(username); + GIT_ASSERT_ARG(cred); + + c = git__calloc(1, sizeof(git_credential_ssh_key)); + GIT_ERROR_CHECK_ALLOC(c); + + c->parent.credtype = GIT_CREDENTIAL_SSH_KEY; + c->parent.free = ssh_key_free; + + c->username = git__strdup(username); + GIT_ERROR_CHECK_ALLOC(c->username); + + c->privatekey = NULL; + + *cred = &c->parent; + return 0; +} + +int git_credential_ssh_custom_new( + git_credential **cred, + const char *username, + const char *publickey, + size_t publickey_len, + git_credential_sign_cb sign_callback, + void *payload) +{ + git_credential_ssh_custom *c; + + GIT_ASSERT_ARG(username); + GIT_ASSERT_ARG(cred); + + c = git__calloc(1, sizeof(git_credential_ssh_custom)); + GIT_ERROR_CHECK_ALLOC(c); + + c->parent.credtype = GIT_CREDENTIAL_SSH_CUSTOM; + c->parent.free = ssh_custom_free; + + c->username = git__strdup(username); + GIT_ERROR_CHECK_ALLOC(c->username); + + if (publickey_len > 0) { + c->publickey = git__malloc(publickey_len); + GIT_ERROR_CHECK_ALLOC(c->publickey); + + memcpy(c->publickey, publickey, publickey_len); + } + + c->publickey_len = publickey_len; + c->sign_callback = sign_callback; + c->payload = payload; + + *cred = &c->parent; + return 0; +} + +int git_credential_default_new(git_credential **cred) +{ + git_credential_default *c; + + GIT_ASSERT_ARG(cred); + + c = git__calloc(1, sizeof(git_credential_default)); + GIT_ERROR_CHECK_ALLOC(c); + + c->credtype = GIT_CREDENTIAL_DEFAULT; + c->free = default_free; + + *cred = c; + return 0; +} + +int git_credential_username_new(git_credential **cred, const char *username) +{ + git_credential_username *c; + size_t len, allocsize; + + GIT_ASSERT_ARG(cred); + + len = strlen(username); + + GIT_ERROR_CHECK_ALLOC_ADD(&allocsize, sizeof(git_credential_username), len); + GIT_ERROR_CHECK_ALLOC_ADD(&allocsize, allocsize, 1); + c = git__malloc(allocsize); + GIT_ERROR_CHECK_ALLOC(c); + + c->parent.credtype = GIT_CREDENTIAL_USERNAME; + c->parent.free = username_free; + memcpy(c->username, username, len + 1); + + *cred = (git_credential *) c; + return 0; +} + +void git_credential_free(git_credential *cred) +{ + if (!cred) + return; + + cred->free(cred); +} + +/* Deprecated credential functions */ + +#ifndef GIT_DEPRECATE_HARD +int git_cred_has_username(git_credential *cred) +{ + return git_credential_has_username(cred); +} + +const char *git_cred_get_username(git_credential *cred) +{ + return git_credential_get_username(cred); +} + +int git_cred_userpass_plaintext_new( + git_credential **out, + const char *username, + const char *password) +{ + return git_credential_userpass_plaintext_new(out,username, password); +} + +int git_cred_default_new(git_credential **out) +{ + return git_credential_default_new(out); +} + +int git_cred_username_new(git_credential **out, const char *username) +{ + return git_credential_username_new(out, username); +} + +int git_cred_ssh_key_new( + git_credential **out, + const char *username, + const char *publickey, + const char *privatekey, + const char *passphrase) +{ + return git_credential_ssh_key_new(out, username, + publickey, privatekey, passphrase); +} + +int git_cred_ssh_key_memory_new( + git_credential **out, + const char *username, + const char *publickey, + const char *privatekey, + const char *passphrase) +{ + return git_credential_ssh_key_memory_new(out, username, + publickey, privatekey, passphrase); +} + +int git_cred_ssh_interactive_new( + git_credential **out, + const char *username, + git_credential_ssh_interactive_cb prompt_callback, + void *payload) +{ + return git_credential_ssh_interactive_new(out, username, + prompt_callback, payload); +} + +int git_cred_ssh_key_from_agent( + git_credential **out, + const char *username) +{ + return git_credential_ssh_key_from_agent(out, username); +} + +int git_cred_ssh_custom_new( + git_credential **out, + const char *username, + const char *publickey, + size_t publickey_len, + git_credential_sign_cb sign_callback, + void *payload) +{ + return git_credential_ssh_custom_new(out, username, + publickey, publickey_len, sign_callback, payload); +} + +void git_cred_free(git_credential *cred) +{ + git_credential_free(cred); +} +#endif diff --git a/src/libgit2/transports/credential_helpers.c b/src/libgit2/transports/credential_helpers.c new file mode 100644 index 00000000000..6d34a4e97b8 --- /dev/null +++ b/src/libgit2/transports/credential_helpers.c @@ -0,0 +1,68 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "git2/credential_helpers.h" + +int git_credential_userpass( + git_credential **cred, + const char *url, + const char *user_from_url, + unsigned int allowed_types, + void *payload) +{ + git_credential_userpass_payload *userpass = (git_credential_userpass_payload*)payload; + const char *effective_username = NULL; + + GIT_UNUSED(url); + + if (!userpass || !userpass->password) return -1; + + /* Username resolution: a username can be passed with the URL, the + * credentials payload, or both. Here's what we do. Note that if we get + * this far, we know that any password the url may contain has already + * failed at least once, so we ignore it. + * + * | Payload | URL | Used | + * +-------------+----------+-----------+ + * | yes | no | payload | + * | yes | yes | payload | + * | no | yes | url | + * | no | no | FAIL | + */ + if (userpass->username) + effective_username = userpass->username; + else if (user_from_url) + effective_username = user_from_url; + else + return -1; + + if (GIT_CREDENTIAL_USERNAME & allowed_types) + return git_credential_username_new(cred, effective_username); + + if ((GIT_CREDENTIAL_USERPASS_PLAINTEXT & allowed_types) == 0 || + git_credential_userpass_plaintext_new(cred, effective_username, userpass->password) < 0) + return -1; + + return 0; +} + +/* Deprecated credential functions */ + +#ifndef GIT_DEPRECATE_HARD +int git_cred_userpass( + git_credential **out, + const char *url, + const char *user_from_url, + unsigned int allowed_types, + void *payload) +{ + return git_credential_userpass(out, url, user_from_url, + allowed_types, payload); +} +#endif diff --git a/src/libgit2/transports/git.c b/src/libgit2/transports/git.c new file mode 100644 index 00000000000..904de2518bf --- /dev/null +++ b/src/libgit2/transports/git.c @@ -0,0 +1,360 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "net.h" +#include "stream.h" +#include "streams/socket.h" +#include "git2/sys/transport.h" + +#define OWNING_SUBTRANSPORT(s) ((git_subtransport *)(s)->parent.subtransport) + +static const char prefix_git[] = "git://"; +static const char cmd_uploadpack[] = "git-upload-pack"; +static const char cmd_receivepack[] = "git-receive-pack"; + +typedef struct { + git_smart_subtransport_stream parent; + git_stream *io; + const char *cmd; + char *url; + unsigned sent_command : 1; +} git_proto_stream; + +typedef struct { + git_smart_subtransport parent; + git_transport *owner; + git_proto_stream *current_stream; +} git_subtransport; + +/* + * Create a git protocol request. + * + * For example: 0035git-upload-pack /libgit2/libgit2\0host=github.com\0 + */ +static int gen_proto(git_str *request, const char *cmd, const char *url) +{ + const char *delim, *repo; + char host[] = "host="; + size_t len; + + delim = strchr(url, '/'); + if (delim == NULL) { + git_error_set(GIT_ERROR_NET, "malformed URL"); + return -1; + } + + repo = delim; + if (repo[1] == '~') + ++repo; + + delim = strchr(url, ':'); + if (delim == NULL) + delim = strchr(url, '/'); + + len = 4 + strlen(cmd) + 1 + strlen(repo) + 1 + strlen(host) + (delim - url) + 1; + + git_str_grow(request, len); + git_str_printf(request, "%04x%s %s%c%s", + (unsigned int)(len & 0x0FFFF), cmd, repo, 0, host); + git_str_put(request, url, delim - url); + git_str_putc(request, '\0'); + + if (git_str_oom(request)) + return -1; + + return 0; +} + +static int send_command(git_proto_stream *s) +{ + git_str request = GIT_STR_INIT; + int error; + + if ((error = gen_proto(&request, s->cmd, s->url)) < 0) + goto cleanup; + + if ((error = git_stream__write_full(s->io, request.ptr, request.size, 0)) < 0) + goto cleanup; + + s->sent_command = 1; + +cleanup: + git_str_dispose(&request); + return error; +} + +static int git_proto_stream_read( + git_smart_subtransport_stream *stream, + char *buffer, + size_t buf_size, + size_t *bytes_read) +{ + git_proto_stream *s = (git_proto_stream *)stream; + ssize_t ret; + int error; + + *bytes_read = 0; + + if (!s->sent_command && (error = send_command(s)) < 0) + return error; + + ret = git_stream_read(s->io, buffer, min(buf_size, INT_MAX)); + + if (ret < 0) + return -1; + + *bytes_read = (size_t)ret; + return 0; +} + +static int git_proto_stream_write( + git_smart_subtransport_stream *stream, + const char *buffer, + size_t len) +{ + git_proto_stream *s = (git_proto_stream *)stream; + int error; + + if (!s->sent_command && (error = send_command(s)) < 0) + return error; + + return git_stream__write_full(s->io, buffer, len, 0); +} + +static void git_proto_stream_free(git_smart_subtransport_stream *stream) +{ + git_proto_stream *s; + git_subtransport *t; + + if (!stream) + return; + + s = (git_proto_stream *)stream; + t = OWNING_SUBTRANSPORT(s); + + t->current_stream = NULL; + + git_stream_close(s->io); + git_stream_free(s->io); + git__free(s->url); + git__free(s); +} + +static int git_proto_stream_alloc( + git_subtransport *t, + const char *url, + const char *cmd, + const char *host, + const char *port, + git_smart_subtransport_stream **stream) +{ + git_proto_stream *s; + + if (!stream) + return -1; + + s = git__calloc(1, sizeof(git_proto_stream)); + GIT_ERROR_CHECK_ALLOC(s); + + s->parent.subtransport = &t->parent; + s->parent.read = git_proto_stream_read; + s->parent.write = git_proto_stream_write; + s->parent.free = git_proto_stream_free; + + s->cmd = cmd; + s->url = git__strdup(url); + + if (!s->url) { + git__free(s); + return -1; + } + + if ((git_socket_stream_new(&s->io, host, port)) < 0) + return -1; + + GIT_ERROR_CHECK_VERSION(s->io, GIT_STREAM_VERSION, "git_stream"); + + *stream = &s->parent; + return 0; +} + +static int _git_uploadpack_ls( + git_subtransport *t, + const char *url, + git_smart_subtransport_stream **stream) +{ + git_net_url urldata = GIT_NET_URL_INIT; + const char *stream_url = url; + const char *host, *port; + git_proto_stream *s; + int error; + + *stream = NULL; + + if (!git__prefixcmp(url, prefix_git)) + stream_url += strlen(prefix_git); + + if ((error = git_net_url_parse(&urldata, url)) < 0) + return error; + + host = urldata.host; + port = urldata.port ? urldata.port : GIT_DEFAULT_PORT; + + error = git_proto_stream_alloc(t, stream_url, cmd_uploadpack, host, port, stream); + + git_net_url_dispose(&urldata); + + if (error < 0) { + git_proto_stream_free(*stream); + return error; + } + + s = (git_proto_stream *) *stream; + if ((error = git_stream_connect(s->io)) < 0) { + git_proto_stream_free(*stream); + return error; + } + + t->current_stream = s; + + return 0; +} + +static int _git_uploadpack( + git_subtransport *t, + const char *url, + git_smart_subtransport_stream **stream) +{ + GIT_UNUSED(url); + + if (t->current_stream) { + *stream = &t->current_stream->parent; + return 0; + } + + git_error_set(GIT_ERROR_NET, "must call UPLOADPACK_LS before UPLOADPACK"); + return -1; +} + +static int _git_receivepack_ls( + git_subtransport *t, + const char *url, + git_smart_subtransport_stream **stream) +{ + git_net_url urldata = GIT_NET_URL_INIT; + const char *stream_url = url; + git_proto_stream *s; + int error; + + *stream = NULL; + if (!git__prefixcmp(url, prefix_git)) + stream_url += strlen(prefix_git); + + if ((error = git_net_url_parse(&urldata, url)) < 0) + return error; + + error = git_proto_stream_alloc(t, stream_url, cmd_receivepack, urldata.host, urldata.port, stream); + + git_net_url_dispose(&urldata); + + if (error < 0) { + git_proto_stream_free(*stream); + return error; + } + + s = (git_proto_stream *) *stream; + + if ((error = git_stream_connect(s->io)) < 0) + return error; + + t->current_stream = s; + + return 0; +} + +static int _git_receivepack( + git_subtransport *t, + const char *url, + git_smart_subtransport_stream **stream) +{ + GIT_UNUSED(url); + + if (t->current_stream) { + *stream = &t->current_stream->parent; + return 0; + } + + git_error_set(GIT_ERROR_NET, "must call RECEIVEPACK_LS before RECEIVEPACK"); + return -1; +} + +static int _git_action( + git_smart_subtransport_stream **stream, + git_smart_subtransport *subtransport, + const char *url, + git_smart_service_t action) +{ + git_subtransport *t = (git_subtransport *) subtransport; + + switch (action) { + case GIT_SERVICE_UPLOADPACK_LS: + return _git_uploadpack_ls(t, url, stream); + + case GIT_SERVICE_UPLOADPACK: + return _git_uploadpack(t, url, stream); + + case GIT_SERVICE_RECEIVEPACK_LS: + return _git_receivepack_ls(t, url, stream); + + case GIT_SERVICE_RECEIVEPACK: + return _git_receivepack(t, url, stream); + } + + *stream = NULL; + return -1; +} + +static int _git_close(git_smart_subtransport *subtransport) +{ + git_subtransport *t = (git_subtransport *) subtransport; + + GIT_ASSERT(!t->current_stream); + + GIT_UNUSED(t); + + return 0; +} + +static void _git_free(git_smart_subtransport *subtransport) +{ + git_subtransport *t = (git_subtransport *) subtransport; + + git__free(t); +} + +int git_smart_subtransport_git(git_smart_subtransport **out, git_transport *owner, void *param) +{ + git_subtransport *t; + + GIT_UNUSED(param); + + if (!out) + return -1; + + t = git__calloc(1, sizeof(git_subtransport)); + GIT_ERROR_CHECK_ALLOC(t); + + t->owner = owner; + t->parent.action = _git_action; + t->parent.close = _git_close; + t->parent.free = _git_free; + + *out = (git_smart_subtransport *) t; + return 0; +} diff --git a/src/libgit2/transports/http.c b/src/libgit2/transports/http.c new file mode 100644 index 00000000000..923a825fa30 --- /dev/null +++ b/src/libgit2/transports/http.c @@ -0,0 +1,765 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#ifndef GIT_HTTPS_WINHTTP + +#include "net.h" +#include "remote.h" +#include "smart.h" +#include "auth.h" +#include "http.h" +#include "auth_negotiate.h" +#include "auth_ntlm.h" +#include "trace.h" +#include "streams/tls.h" +#include "streams/socket.h" +#include "httpclient.h" +#include "git2/sys/credential.h" + +bool git_http__expect_continue = false; + +typedef enum { + HTTP_STATE_NONE = 0, + HTTP_STATE_SENDING_REQUEST, + HTTP_STATE_RECEIVING_RESPONSE, + HTTP_STATE_DONE +} http_state; + +typedef struct { + git_http_method method; + const char *url; + const char *request_type; + const char *response_type; + unsigned int initial : 1, + chunked : 1; +} http_service; + +typedef struct { + git_smart_subtransport_stream parent; + const http_service *service; + http_state state; + unsigned replay_count; +} http_stream; + +typedef struct { + git_net_url url; + + git_credential *cred; + unsigned auth_schemetypes; + unsigned url_cred_presented : 1; +} http_server; + +typedef struct { + git_smart_subtransport parent; + transport_smart *owner; + + http_server server; + http_server proxy; + + git_http_client *http_client; +} http_subtransport; + +static const http_service upload_pack_ls_service = { + GIT_HTTP_METHOD_GET, "/info/refs?service=git-upload-pack", + NULL, + "application/x-git-upload-pack-advertisement", + 1, + 0 +}; +static const http_service upload_pack_service = { + GIT_HTTP_METHOD_POST, "/git-upload-pack", + "application/x-git-upload-pack-request", + "application/x-git-upload-pack-result", + 0, + 0 +}; +static const http_service receive_pack_ls_service = { + GIT_HTTP_METHOD_GET, "/info/refs?service=git-receive-pack", + NULL, + "application/x-git-receive-pack-advertisement", + 1, + 0 +}; +static const http_service receive_pack_service = { + GIT_HTTP_METHOD_POST, "/git-receive-pack", + "application/x-git-receive-pack-request", + "application/x-git-receive-pack-result", + 0, + 1 +}; + +#define SERVER_TYPE_REMOTE "remote" +#define SERVER_TYPE_PROXY "proxy" + +#define OWNING_SUBTRANSPORT(s) ((http_subtransport *)(s)->parent.subtransport) + +static int apply_url_credentials( + git_credential **cred, + unsigned int allowed_types, + const char *username, + const char *password) +{ + GIT_ASSERT_ARG(username); + + if (!password) + password = ""; + + if (allowed_types & GIT_CREDENTIAL_USERPASS_PLAINTEXT) + return git_credential_userpass_plaintext_new(cred, username, password); + + if ((allowed_types & GIT_CREDENTIAL_DEFAULT) && *username == '\0' && *password == '\0') + return git_credential_default_new(cred); + + return GIT_PASSTHROUGH; +} + +GIT_INLINE(void) free_cred(git_credential **cred) +{ + if (*cred) { + git_credential_free(*cred); + (*cred) = NULL; + } +} + +static int handle_auth( + http_server *server, + const char *server_type, + const char *url, + unsigned int allowed_schemetypes, + unsigned int allowed_credtypes, + git_credential_acquire_cb callback, + void *callback_payload) +{ + int error = 1; + + if (server->cred) + free_cred(&server->cred); + + /* Start with URL-specified credentials, if there were any. */ + if ((allowed_credtypes & GIT_CREDENTIAL_USERPASS_PLAINTEXT) && + !server->url_cred_presented && + server->url.username) { + error = apply_url_credentials(&server->cred, allowed_credtypes, server->url.username, server->url.password); + server->url_cred_presented = 1; + + /* treat GIT_PASSTHROUGH as if callback isn't set */ + if (error == GIT_PASSTHROUGH) + error = 1; + } + + if (error > 0 && callback) { + error = callback(&server->cred, url, server->url.username, allowed_credtypes, callback_payload); + + /* treat GIT_PASSTHROUGH as if callback isn't set */ + if (error == GIT_PASSTHROUGH) + error = 1; + } + + if (error > 0) { + git_error_set(GIT_ERROR_HTTP, "%s authentication required but no callback set", server_type); + error = GIT_EAUTH; + } + + if (!error) + server->auth_schemetypes = allowed_schemetypes; + + return error; +} + +GIT_INLINE(int) handle_remote_auth( + http_stream *stream, + git_http_response *response) +{ + http_subtransport *transport = OWNING_SUBTRANSPORT(stream); + git_remote_connect_options *connect_opts = &transport->owner->connect_opts; + + if (response->server_auth_credtypes == 0) { + git_error_set(GIT_ERROR_HTTP, "server requires authentication that we do not support"); + return GIT_EAUTH; + } + + /* Otherwise, prompt for credentials. */ + return handle_auth( + &transport->server, + SERVER_TYPE_REMOTE, + transport->owner->url, + response->server_auth_schemetypes, + response->server_auth_credtypes, + connect_opts->callbacks.credentials, + connect_opts->callbacks.payload); +} + +GIT_INLINE(int) handle_proxy_auth( + http_stream *stream, + git_http_response *response) +{ + http_subtransport *transport = OWNING_SUBTRANSPORT(stream); + git_remote_connect_options *connect_opts = &transport->owner->connect_opts; + + if (response->proxy_auth_credtypes == 0) { + git_error_set(GIT_ERROR_HTTP, "proxy requires authentication that we do not support"); + return GIT_EAUTH; + } + + /* Otherwise, prompt for credentials. */ + return handle_auth( + &transport->proxy, + SERVER_TYPE_PROXY, + connect_opts->proxy_opts.url, + response->server_auth_schemetypes, + response->proxy_auth_credtypes, + connect_opts->proxy_opts.credentials, + connect_opts->proxy_opts.payload); +} + +static bool allow_redirect(http_stream *stream) +{ + http_subtransport *transport = OWNING_SUBTRANSPORT(stream); + + switch (transport->owner->connect_opts.follow_redirects) { + case GIT_REMOTE_REDIRECT_INITIAL: + return (stream->service->initial == 1); + case GIT_REMOTE_REDIRECT_ALL: + return true; + default: + return false; + } +} + +static int handle_response( + bool *complete, + http_stream *stream, + git_http_response *response, + bool allow_replay) +{ + http_subtransport *transport = OWNING_SUBTRANSPORT(stream); + int error; + + *complete = false; + + if (allow_replay && git_http_response_is_redirect(response)) { + if (!response->location) { + git_error_set(GIT_ERROR_HTTP, "redirect without location"); + return -1; + } + + if (git_net_url_apply_redirect(&transport->server.url, response->location, allow_redirect(stream), stream->service->url) < 0) { + return -1; + } + + return 0; + } else if (git_http_response_is_redirect(response)) { + git_error_set(GIT_ERROR_HTTP, "unexpected redirect"); + return -1; + } + + /* If we're in the middle of challenge/response auth, continue. */ + if (allow_replay && response->resend_credentials) { + return 0; + } else if (allow_replay && response->status == GIT_HTTP_STATUS_UNAUTHORIZED) { + if ((error = handle_remote_auth(stream, response)) < 0) + return error; + + return git_http_client_skip_body(transport->http_client); + } else if (allow_replay && response->status == GIT_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED) { + if ((error = handle_proxy_auth(stream, response)) < 0) + return error; + + return git_http_client_skip_body(transport->http_client); + } else if (response->status == GIT_HTTP_STATUS_UNAUTHORIZED || + response->status == GIT_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED) { + git_error_set(GIT_ERROR_HTTP, "unexpected authentication failure"); + return GIT_EAUTH; + } + + if (response->status != GIT_HTTP_STATUS_OK) { + git_error_set(GIT_ERROR_HTTP, "unexpected http status code: %d", response->status); + return -1; + } + + /* The response must contain a Content-Type header. */ + if (!response->content_type) { + git_error_set(GIT_ERROR_HTTP, "no content-type header in response"); + return -1; + } + + /* The Content-Type header must match our expectation. */ + if (strcmp(response->content_type, stream->service->response_type) != 0) { + git_error_set(GIT_ERROR_HTTP, "invalid content-type: '%s'", response->content_type); + return -1; + } + + *complete = true; + stream->state = HTTP_STATE_RECEIVING_RESPONSE; + return 0; +} + +static int lookup_proxy( + bool *out_use, + http_subtransport *transport) +{ + git_remote_connect_options *connect_opts = &transport->owner->connect_opts; + const char *proxy; + git_remote *remote; + char *config = NULL; + int error = 0; + + *out_use = false; + git_net_url_dispose(&transport->proxy.url); + + switch (connect_opts->proxy_opts.type) { + case GIT_PROXY_SPECIFIED: + proxy = connect_opts->proxy_opts.url; + break; + + case GIT_PROXY_AUTO: + remote = transport->owner->owner; + + error = git_remote__http_proxy(&config, remote, &transport->server.url); + + if (error || !config) + goto done; + + proxy = config; + break; + + default: + return 0; + } + + if (!proxy || !*proxy || + (error = git_net_url_parse_http(&transport->proxy.url, proxy)) < 0) + goto done; + + if (!git_net_url_valid(&transport->proxy.url)) { + git_error_set(GIT_ERROR_HTTP, "invalid URL: '%s'", proxy); + error = -1; + goto done; + } + + *out_use = true; + +done: + git__free(config); + return error; +} + +static int generate_request( + git_net_url *url, + git_http_request *request, + http_stream *stream, + size_t len) +{ + http_subtransport *transport = OWNING_SUBTRANSPORT(stream); + bool use_proxy = false; + int error; + + if ((error = git_net_url_joinpath(url, + &transport->server.url, stream->service->url)) < 0 || + (error = lookup_proxy(&use_proxy, transport)) < 0) + return error; + + request->method = stream->service->method; + request->url = url; + request->credentials = transport->server.cred; + request->proxy = use_proxy ? &transport->proxy.url : NULL; + request->proxy_credentials = transport->proxy.cred; + request->custom_headers = &transport->owner->connect_opts.custom_headers; + + if (stream->service->method == GIT_HTTP_METHOD_POST) { + request->chunked = stream->service->chunked; + request->content_length = stream->service->chunked ? 0 : len; + request->content_type = stream->service->request_type; + request->accept = stream->service->response_type; + request->expect_continue = git_http__expect_continue; + } + + return 0; +} + +/* + * Read from an HTTP transport - for the first invocation of this function + * (ie, when stream->state == HTTP_STATE_NONE), we'll send a GET request + * to the remote host. We will stream that data back on all subsequent + * calls. + */ +static int http_stream_read( + git_smart_subtransport_stream *s, + char *buffer, + size_t buffer_size, + size_t *out_len) +{ + http_stream *stream = (http_stream *)s; + http_subtransport *transport = OWNING_SUBTRANSPORT(stream); + git_net_url url = GIT_NET_URL_INIT; + git_net_url proxy_url = GIT_NET_URL_INIT; + git_http_request request = {0}; + git_http_response response = {0}; + bool complete; + int error; + + *out_len = 0; + + if (stream->state == HTTP_STATE_NONE) { + stream->state = HTTP_STATE_SENDING_REQUEST; + stream->replay_count = 0; + } + + /* + * Formulate the URL, send the request and read the response + * headers. Some of the request body may also be read. + */ + while (stream->state == HTTP_STATE_SENDING_REQUEST && + stream->replay_count < GIT_HTTP_REPLAY_MAX) { + git_net_url_dispose(&url); + git_net_url_dispose(&proxy_url); + git_http_response_dispose(&response); + + if ((error = generate_request(&url, &request, stream, 0)) < 0 || + (error = git_http_client_send_request( + transport->http_client, &request)) < 0 || + (error = git_http_client_read_response( + &response, transport->http_client)) < 0 || + (error = handle_response(&complete, stream, &response, true)) < 0) + goto done; + + if (complete) + break; + + stream->replay_count++; + } + + if (stream->state == HTTP_STATE_SENDING_REQUEST) { + git_error_set(GIT_ERROR_HTTP, "too many redirects or authentication replays"); + error = GIT_ERROR; /* not GIT_EAUTH, because the exact cause is unclear */ + goto done; + } + + GIT_ASSERT(stream->state == HTTP_STATE_RECEIVING_RESPONSE); + + error = git_http_client_read_body(transport->http_client, buffer, buffer_size); + + if (error > 0) { + *out_len = error; + error = 0; + } + +done: + git_net_url_dispose(&url); + git_net_url_dispose(&proxy_url); + git_http_response_dispose(&response); + + return error; +} + +static bool needs_probe(http_stream *stream) +{ + http_subtransport *transport = OWNING_SUBTRANSPORT(stream); + + return (transport->server.auth_schemetypes == GIT_HTTP_AUTH_NTLM || + transport->server.auth_schemetypes == GIT_HTTP_AUTH_NEGOTIATE); +} + +static int send_probe(http_stream *stream) +{ + http_subtransport *transport = OWNING_SUBTRANSPORT(stream); + git_http_client *client = transport->http_client; + const char *probe = "0000"; + size_t len = 4; + git_net_url url = GIT_NET_URL_INIT; + git_http_request request = {0}; + git_http_response response = {0}; + bool complete = false; + size_t step, steps = 1; + int error; + + /* NTLM requires a full challenge/response */ + if (transport->server.auth_schemetypes == GIT_HTTP_AUTH_NTLM) + steps = GIT_AUTH_STEPS_NTLM; + + /* + * Send at most two requests: one without any authentication to see + * if we get prompted to authenticate. If we do, send a second one + * with the first authentication message. The final authentication + * message with the response will occur with the *actual* POST data. + */ + for (step = 0; step < steps && !complete; step++) { + git_net_url_dispose(&url); + git_http_response_dispose(&response); + + if ((error = generate_request(&url, &request, stream, len)) < 0 || + (error = git_http_client_send_request(client, &request)) < 0 || + (error = git_http_client_send_body(client, probe, len)) < 0 || + (error = git_http_client_read_response(&response, client)) < 0 || + (error = git_http_client_skip_body(client)) < 0 || + (error = handle_response(&complete, stream, &response, true)) < 0) + goto done; + } + +done: + git_http_response_dispose(&response); + git_net_url_dispose(&url); + return error; +} + +/* +* Write to an HTTP transport - for the first invocation of this function +* (ie, when stream->state == HTTP_STATE_NONE), we'll send a POST request +* to the remote host. If we're sending chunked data, then subsequent calls +* will write the additional data given in the buffer. If we're not chunking, +* then the caller should have given us all the data in the original call. +* The caller should call http_stream_read_response to get the result. +*/ +static int http_stream_write( + git_smart_subtransport_stream *s, + const char *buffer, + size_t len) +{ + http_stream *stream = GIT_CONTAINER_OF(s, http_stream, parent); + http_subtransport *transport = OWNING_SUBTRANSPORT(stream); + git_net_url url = GIT_NET_URL_INIT; + git_http_request request = {0}; + git_http_response response = {0}; + int error; + + while (stream->state == HTTP_STATE_NONE && + stream->replay_count < GIT_HTTP_REPLAY_MAX) { + + git_net_url_dispose(&url); + git_http_response_dispose(&response); + + /* + * If we're authenticating with a connection-based mechanism + * (NTLM, Kerberos), send a "probe" packet. Servers SHOULD + * authenticate an entire keep-alive connection, so ideally + * we should not need to authenticate but some servers do + * not support this. By sending a probe packet, we'll be + * able to follow up with a second POST using the actual + * data (and, in the degenerate case, the authentication + * header as well). + */ + if (needs_probe(stream) && (error = send_probe(stream)) < 0) + goto done; + + /* Send the regular POST request. */ + if ((error = generate_request(&url, &request, stream, len)) < 0 || + (error = git_http_client_send_request( + transport->http_client, &request)) < 0) + goto done; + + if (request.expect_continue && + git_http_client_has_response(transport->http_client)) { + bool complete; + + /* + * If we got a response to an expect/continue, then + * it's something other than a 100 and we should + * deal with the response somehow. + */ + if ((error = git_http_client_read_response(&response, transport->http_client)) < 0 || + (error = handle_response(&complete, stream, &response, true)) < 0) + goto done; + } else { + stream->state = HTTP_STATE_SENDING_REQUEST; + } + + stream->replay_count++; + } + + if (stream->state == HTTP_STATE_NONE) { + git_error_set(GIT_ERROR_HTTP, + "too many redirects or authentication replays"); + error = GIT_ERROR; /* not GIT_EAUTH because the exact cause is unclear */ + goto done; + } + + GIT_ASSERT(stream->state == HTTP_STATE_SENDING_REQUEST); + + error = git_http_client_send_body(transport->http_client, buffer, len); + +done: + git_http_response_dispose(&response); + git_net_url_dispose(&url); + return error; +} + +/* +* Read from an HTTP transport after it has been written to. This is the +* response from a POST request made by http_stream_write. +*/ +static int http_stream_read_response( + git_smart_subtransport_stream *s, + char *buffer, + size_t buffer_size, + size_t *out_len) +{ + http_stream *stream = (http_stream *)s; + http_subtransport *transport = OWNING_SUBTRANSPORT(stream); + git_http_client *client = transport->http_client; + git_http_response response = {0}; + bool complete; + int error; + + *out_len = 0; + + if (stream->state == HTTP_STATE_SENDING_REQUEST) { + if ((error = git_http_client_read_response(&response, client)) < 0 || + (error = handle_response(&complete, stream, &response, false)) < 0) + goto done; + + GIT_ASSERT(complete); + stream->state = HTTP_STATE_RECEIVING_RESPONSE; + } + + error = git_http_client_read_body(client, buffer, buffer_size); + + if (error > 0) { + *out_len = error; + error = 0; + } + +done: + git_http_response_dispose(&response); + return error; +} + +static void http_stream_free(git_smart_subtransport_stream *stream) +{ + http_stream *s = GIT_CONTAINER_OF(stream, http_stream, parent); + git__free(s); +} + +static const http_service *select_service(git_smart_service_t action) +{ + switch (action) { + case GIT_SERVICE_UPLOADPACK_LS: + return &upload_pack_ls_service; + case GIT_SERVICE_UPLOADPACK: + return &upload_pack_service; + case GIT_SERVICE_RECEIVEPACK_LS: + return &receive_pack_ls_service; + case GIT_SERVICE_RECEIVEPACK: + return &receive_pack_service; + } + + return NULL; +} + +static int http_action( + git_smart_subtransport_stream **out, + git_smart_subtransport *t, + const char *url, + git_smart_service_t action) +{ + http_subtransport *transport = GIT_CONTAINER_OF(t, http_subtransport, parent); + git_remote_connect_options *connect_opts = &transport->owner->connect_opts; + git_http_client_options opts = {0}; + http_stream *stream; + const http_service *service; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(t); + + *out = NULL; + + /* + * If we've seen a redirect then preserve the location that we've + * been given. This is important to continue authorization against + * the redirect target, not the user-given source; the endpoint may + * have redirected us from HTTP->HTTPS and is using an auth mechanism + * that would be insecure in plaintext (eg, HTTP Basic). + */ + if (!git_net_url_valid(&transport->server.url) && + (error = git_net_url_parse(&transport->server.url, url)) < 0) + return error; + + if ((service = select_service(action)) == NULL) { + git_error_set(GIT_ERROR_HTTP, "invalid action"); + return -1; + } + + stream = git__calloc(sizeof(http_stream), 1); + GIT_ERROR_CHECK_ALLOC(stream); + + opts.server_certificate_check_cb = connect_opts->callbacks.certificate_check; + opts.server_certificate_check_payload = connect_opts->callbacks.payload; + opts.proxy_certificate_check_cb = connect_opts->proxy_opts.certificate_check; + opts.proxy_certificate_check_payload = connect_opts->proxy_opts.payload; + + if (transport->http_client) { + git_http_client_set_options(transport->http_client, &opts); + } else { + if (git_http_client_new(&transport->http_client, &opts) < 0) + return -1; + } + + stream->service = service; + stream->parent.subtransport = &transport->parent; + + if (service->method == GIT_HTTP_METHOD_GET) { + stream->parent.read = http_stream_read; + } else { + stream->parent.write = http_stream_write; + stream->parent.read = http_stream_read_response; + } + + stream->parent.free = http_stream_free; + + *out = (git_smart_subtransport_stream *)stream; + return 0; +} + +static int http_close(git_smart_subtransport *t) +{ + http_subtransport *transport = GIT_CONTAINER_OF(t, http_subtransport, parent); + + free_cred(&transport->server.cred); + free_cred(&transport->proxy.cred); + + transport->server.url_cred_presented = false; + transport->proxy.url_cred_presented = false; + + git_net_url_dispose(&transport->server.url); + git_net_url_dispose(&transport->proxy.url); + + return 0; +} + +static void http_free(git_smart_subtransport *t) +{ + http_subtransport *transport = GIT_CONTAINER_OF(t, http_subtransport, parent); + + git_http_client_free(transport->http_client); + + http_close(t); + git__free(transport); +} + +int git_smart_subtransport_http(git_smart_subtransport **out, git_transport *owner, void *param) +{ + http_subtransport *transport; + + GIT_UNUSED(param); + + GIT_ASSERT_ARG(out); + + transport = git__calloc(sizeof(http_subtransport), 1); + GIT_ERROR_CHECK_ALLOC(transport); + + transport->owner = (transport_smart *)owner; + transport->parent.action = http_action; + transport->parent.close = http_close; + transport->parent.free = http_free; + + *out = (git_smart_subtransport *) transport; + return 0; +} + +#endif /* !GIT_HTTPS_WINHTTP */ diff --git a/src/libgit2/transports/http.h b/src/libgit2/transports/http.h new file mode 100644 index 00000000000..7410202a820 --- /dev/null +++ b/src/libgit2/transports/http.h @@ -0,0 +1,18 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_transports_http_h__ +#define INCLUDE_transports_http_h__ + +#include "settings.h" +#include "httpclient.h" + +#define GIT_HTTP_REPLAY_MAX 15 + +extern bool git_http__expect_continue; + +#endif diff --git a/src/libgit2/transports/httpclient.c b/src/libgit2/transports/httpclient.c new file mode 100644 index 00000000000..e27d40b5fd8 --- /dev/null +++ b/src/libgit2/transports/httpclient.c @@ -0,0 +1,1633 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" +#include "git2.h" + +#include "vector.h" +#include "trace.h" +#include "httpclient.h" +#include "http.h" +#include "auth.h" +#include "auth_negotiate.h" +#include "auth_ntlm.h" +#include "git2/sys/credential.h" +#include "net.h" +#include "stream.h" +#include "streams/socket.h" +#include "streams/tls.h" +#include "auth.h" +#include "httpparser.h" + +static git_http_auth_scheme auth_schemes[] = { + { GIT_HTTP_AUTH_NEGOTIATE, "Negotiate", GIT_CREDENTIAL_DEFAULT, git_http_auth_negotiate }, + { GIT_HTTP_AUTH_NTLM, "NTLM", GIT_CREDENTIAL_USERPASS_PLAINTEXT, git_http_auth_ntlm }, + { GIT_HTTP_AUTH_BASIC, "Basic", GIT_CREDENTIAL_USERPASS_PLAINTEXT, git_http_auth_basic }, +}; + +/* + * Use a 16kb read buffer to match the maximum size of a TLS packet. This + * is critical for compatibility with SecureTransport, which will always do + * a network read on every call, even if it has data buffered to return to + * you. That buffered data may be the _end_ of a keep-alive response, so + * if SecureTransport performs another network read, it will wait until the + * server ultimately times out before it returns that buffered data to you. + * Since SecureTransport only reads a single TLS packet at a time, by + * calling it with a read buffer that is the maximum size of a TLS packet, + * we ensure that it will never buffer. + */ +#define GIT_READ_BUFFER_SIZE (16 * 1024) + +typedef struct { + git_net_url url; + git_stream *stream; + + git_vector auth_challenges; + git_http_auth_context *auth_context; +} git_http_server; + +typedef enum { + PROXY = 1, + SERVER +} git_http_server_t; + +typedef enum { + NONE = 0, + SENDING_REQUEST, + SENDING_BODY, + SENT_REQUEST, + HAS_EARLY_RESPONSE, + READING_RESPONSE, + READING_BODY, + DONE +} http_client_state; + +/* Parser state */ +typedef enum { + PARSE_HEADER_NONE = 0, + PARSE_HEADER_NAME, + PARSE_HEADER_VALUE, + PARSE_HEADER_COMPLETE +} parse_header_state; + +typedef enum { + PARSE_STATUS_OK, + PARSE_STATUS_NO_OUTPUT, + PARSE_STATUS_ERROR +} parse_status; + +typedef struct { + git_http_client *client; + git_http_response *response; + + /* Temporary buffers to avoid extra mallocs */ + git_str parse_header_name; + git_str parse_header_value; + + /* Parser state */ + int error; + parse_status parse_status; + + /* Headers parsing */ + parse_header_state parse_header_state; + + /* Body parsing */ + char *output_buf; /* Caller's output buffer */ + size_t output_size; /* Size of caller's output buffer */ + size_t output_written; /* Bytes we've written to output buffer */ +} http_parser_context; + +/* HTTP client connection */ +struct git_http_client { + git_http_client_options opts; + + /* Are we writing to the proxy or server, and state of the client. */ + git_http_server_t current_server; + http_client_state state; + + git_http_parser parser; + + git_http_server server; + git_http_server proxy; + + unsigned request_count; + unsigned connected : 1, + proxy_connected : 1, + keepalive : 1, + request_chunked : 1; + + /* Temporary buffers to avoid extra mallocs */ + git_str request_msg; + git_str read_buf; + + /* A subset of information from the request */ + size_t request_body_len, + request_body_remain; + + /* + * When state == HAS_EARLY_RESPONSE, the response of our proxy + * that we have buffered and will deliver during read_response. + */ + git_http_response early_response; +}; + +bool git_http_response_is_redirect(git_http_response *response) +{ + return (response->status == GIT_HTTP_MOVED_PERMANENTLY || + response->status == GIT_HTTP_FOUND || + response->status == GIT_HTTP_SEE_OTHER || + response->status == GIT_HTTP_TEMPORARY_REDIRECT || + response->status == GIT_HTTP_PERMANENT_REDIRECT); +} + +void git_http_response_dispose(git_http_response *response) +{ + if (!response) + return; + + git__free(response->content_type); + git__free(response->location); + + memset(response, 0, sizeof(git_http_response)); +} + +static int on_header_complete(git_http_parser *parser) +{ + http_parser_context *ctx = (http_parser_context *) parser->data; + git_http_client *client = ctx->client; + git_http_response *response = ctx->response; + + git_str *name = &ctx->parse_header_name; + git_str *value = &ctx->parse_header_value; + + if (!strcasecmp("Content-Type", name->ptr)) { + if (response->content_type) { + git_error_set(GIT_ERROR_HTTP, + "multiple content-type headers"); + return -1; + } + + response->content_type = + git__strndup(value->ptr, value->size); + GIT_ERROR_CHECK_ALLOC(ctx->response->content_type); + } else if (!strcasecmp("Content-Length", name->ptr)) { + int64_t len; + + if (response->content_length) { + git_error_set(GIT_ERROR_HTTP, + "multiple content-length headers"); + return -1; + } + + if (git__strntol64(&len, value->ptr, value->size, + NULL, 10) < 0 || len < 0) { + git_error_set(GIT_ERROR_HTTP, + "invalid content-length"); + return -1; + } + + response->content_length = (size_t)len; + } else if (!strcasecmp("Transfer-Encoding", name->ptr) && + !strcasecmp("chunked", value->ptr)) { + ctx->response->chunked = 1; + } else if (!strcasecmp("Proxy-Authenticate", git_str_cstr(name))) { + char *dup = git__strndup(value->ptr, value->size); + GIT_ERROR_CHECK_ALLOC(dup); + + if (git_vector_insert(&client->proxy.auth_challenges, dup) < 0) + return -1; + } else if (!strcasecmp("WWW-Authenticate", name->ptr)) { + char *dup = git__strndup(value->ptr, value->size); + GIT_ERROR_CHECK_ALLOC(dup); + + if (git_vector_insert(&client->server.auth_challenges, dup) < 0) + return -1; + } else if (!strcasecmp("Location", name->ptr)) { + if (response->location) { + git_error_set(GIT_ERROR_HTTP, + "multiple location headers"); + return -1; + } + + response->location = git__strndup(value->ptr, value->size); + GIT_ERROR_CHECK_ALLOC(response->location); + } + + return 0; +} + +static int on_header_field(git_http_parser *parser, const char *str, size_t len) +{ + http_parser_context *ctx = (http_parser_context *) parser->data; + + switch (ctx->parse_header_state) { + /* + * We last saw a header value, process the name/value pair and + * get ready to handle this new name. + */ + case PARSE_HEADER_VALUE: + if (on_header_complete(parser) < 0) + return ctx->parse_status = PARSE_STATUS_ERROR; + + git_str_clear(&ctx->parse_header_name); + git_str_clear(&ctx->parse_header_value); + /* Fall through */ + + case PARSE_HEADER_NONE: + case PARSE_HEADER_NAME: + ctx->parse_header_state = PARSE_HEADER_NAME; + + if (git_str_put(&ctx->parse_header_name, str, len) < 0) + return ctx->parse_status = PARSE_STATUS_ERROR; + + break; + + default: + git_error_set(GIT_ERROR_HTTP, + "header name seen at unexpected time"); + return ctx->parse_status = PARSE_STATUS_ERROR; + } + + return 0; +} + +static int on_header_value(git_http_parser *parser, const char *str, size_t len) +{ + http_parser_context *ctx = (http_parser_context *) parser->data; + + switch (ctx->parse_header_state) { + case PARSE_HEADER_NAME: + case PARSE_HEADER_VALUE: + ctx->parse_header_state = PARSE_HEADER_VALUE; + + if (git_str_put(&ctx->parse_header_value, str, len) < 0) + return ctx->parse_status = PARSE_STATUS_ERROR; + + break; + + default: + git_error_set(GIT_ERROR_HTTP, + "header value seen at unexpected time"); + return ctx->parse_status = PARSE_STATUS_ERROR; + } + + return 0; +} + +GIT_INLINE(bool) challenge_matches_scheme( + const char *challenge, + git_http_auth_scheme *scheme) +{ + const char *scheme_name = scheme->name; + size_t scheme_len = strlen(scheme_name); + + if (!strncasecmp(challenge, scheme_name, scheme_len) && + (challenge[scheme_len] == '\0' || challenge[scheme_len] == ' ')) + return true; + + return false; +} + +static git_http_auth_scheme *scheme_for_challenge(const char *challenge) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(auth_schemes); i++) { + if (challenge_matches_scheme(challenge, &auth_schemes[i])) + return &auth_schemes[i]; + } + + return NULL; +} + +GIT_INLINE(void) collect_authinfo( + unsigned int *schemetypes, + unsigned int *credtypes, + git_vector *challenges) +{ + git_http_auth_scheme *scheme; + const char *challenge; + size_t i; + + *schemetypes = 0; + *credtypes = 0; + + git_vector_foreach(challenges, i, challenge) { + if ((scheme = scheme_for_challenge(challenge)) != NULL) { + *schemetypes |= scheme->type; + *credtypes |= scheme->credtypes; + } + } +} + +static int resend_needed(git_http_client *client, git_http_response *response) +{ + git_http_auth_context *auth_context; + + if (response->status == GIT_HTTP_STATUS_UNAUTHORIZED && + (auth_context = client->server.auth_context) && + auth_context->is_complete && + !auth_context->is_complete(auth_context)) + return 1; + + if (response->status == GIT_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED && + (auth_context = client->proxy.auth_context) && + auth_context->is_complete && + !auth_context->is_complete(auth_context)) + return 1; + + return 0; +} + +static int on_headers_complete(git_http_parser *parser) +{ + http_parser_context *ctx = (http_parser_context *) parser->data; + + /* Finalize the last seen header */ + switch (ctx->parse_header_state) { + case PARSE_HEADER_VALUE: + if (on_header_complete(parser) < 0) + return ctx->parse_status = PARSE_STATUS_ERROR; + + /* Fall through */ + + case PARSE_HEADER_NONE: + ctx->parse_header_state = PARSE_HEADER_COMPLETE; + break; + + default: + git_error_set(GIT_ERROR_HTTP, + "header completion at unexpected time"); + return ctx->parse_status = PARSE_STATUS_ERROR; + } + + ctx->response->status = git_http_parser_status_code(parser); + ctx->client->keepalive = git_http_parser_keep_alive(parser); + + /* Prepare for authentication */ + collect_authinfo(&ctx->response->server_auth_schemetypes, + &ctx->response->server_auth_credtypes, + &ctx->client->server.auth_challenges); + collect_authinfo(&ctx->response->proxy_auth_schemetypes, + &ctx->response->proxy_auth_credtypes, + &ctx->client->proxy.auth_challenges); + + ctx->response->resend_credentials = resend_needed(ctx->client, + ctx->response); + + if (ctx->response->content_type || ctx->response->chunked) + ctx->client->state = READING_BODY; + else + ctx->client->state = DONE; + + return git_http_parser_pause(parser); +} + +static int on_body(git_http_parser *parser, const char *buf, size_t len) +{ + http_parser_context *ctx = (http_parser_context *) parser->data; + size_t max_len; + + /* Saw data when we expected not to (eg, in consume_response_body) */ + if (ctx->output_buf == NULL || ctx->output_size == 0) { + ctx->parse_status = PARSE_STATUS_NO_OUTPUT; + return 0; + } + + GIT_ASSERT(ctx->output_size >= ctx->output_written); + + max_len = min(ctx->output_size - ctx->output_written, len); + max_len = min(max_len, INT_MAX); + + memcpy(ctx->output_buf + ctx->output_written, buf, max_len); + ctx->output_written += max_len; + + return 0; +} + +static int on_message_complete(git_http_parser *parser) +{ + http_parser_context *ctx = (http_parser_context *) parser->data; + + ctx->client->state = DONE; + return 0; +} + +GIT_INLINE(int) stream_write( + git_http_server *server, + const char *data, + size_t len) +{ + git_trace(GIT_TRACE_TRACE, + "Sending request:\n%.*s", (int)len, data); + + return git_stream__write_full(server->stream, data, len, 0); +} + +GIT_INLINE(int) client_write_request(git_http_client *client) +{ + git_stream *stream = client->current_server == PROXY ? + client->proxy.stream : client->server.stream; + + git_trace(GIT_TRACE_TRACE, + "Sending request:\n%.*s", + (int)client->request_msg.size, client->request_msg.ptr); + + return git_stream__write_full(stream, + client->request_msg.ptr, + client->request_msg.size, + 0); +} + +static const char *name_for_method(git_http_method method) +{ + switch (method) { + case GIT_HTTP_METHOD_GET: + return "GET"; + case GIT_HTTP_METHOD_POST: + return "POST"; + case GIT_HTTP_METHOD_CONNECT: + return "CONNECT"; + } + + return NULL; +} + +/* + * Find the scheme that is suitable for the given credentials, based on the + * server's auth challenges. + */ +static bool best_scheme_and_challenge( + git_http_auth_scheme **scheme_out, + const char **challenge_out, + git_vector *challenges, + git_credential *credentials) +{ + const char *challenge; + size_t i, j; + + for (i = 0; i < ARRAY_SIZE(auth_schemes); i++) { + git_vector_foreach(challenges, j, challenge) { + git_http_auth_scheme *scheme = &auth_schemes[i]; + + if (challenge_matches_scheme(challenge, scheme) && + (scheme->credtypes & credentials->credtype)) { + *scheme_out = scheme; + *challenge_out = challenge; + return true; + } + } + } + + return false; +} + +/* + * Find the challenge from the server for our current auth context. + */ +static const char *challenge_for_context( + git_vector *challenges, + git_http_auth_context *auth_ctx) +{ + const char *challenge; + size_t i, j; + + for (i = 0; i < ARRAY_SIZE(auth_schemes); i++) { + if (auth_schemes[i].type == auth_ctx->type) { + git_http_auth_scheme *scheme = &auth_schemes[i]; + + git_vector_foreach(challenges, j, challenge) { + if (challenge_matches_scheme(challenge, scheme)) + return challenge; + } + } + } + + return NULL; +} + +static const char *init_auth_context( + git_http_server *server, + git_vector *challenges, + git_credential *credentials) +{ + git_http_auth_scheme *scheme; + const char *challenge; + int error; + + if (!best_scheme_and_challenge(&scheme, &challenge, challenges, credentials)) { + git_error_set(GIT_ERROR_HTTP, "could not find appropriate mechanism for credentials"); + return NULL; + } + + error = scheme->init_context(&server->auth_context, &server->url); + + if (error == GIT_PASSTHROUGH) { + git_error_set(GIT_ERROR_HTTP, "'%s' authentication is not supported", scheme->name); + return NULL; + } + + return challenge; +} + +static void free_auth_context(git_http_server *server) +{ + if (!server->auth_context) + return; + + if (server->auth_context->free) + server->auth_context->free(server->auth_context); + + server->auth_context = NULL; +} + +static int apply_credentials( + git_str *buf, + git_http_server *server, + const char *header_name, + git_credential *credentials) +{ + git_http_auth_context *auth = server->auth_context; + git_vector *challenges = &server->auth_challenges; + const char *challenge = NULL; + git_str token = GIT_STR_INIT; + int error = 0; + + /* We've started a new request without creds; free the context. */ + if (auth && !credentials) { + free_auth_context(server); + return 0; + } + + /* We haven't authenticated, nor were we asked to. Nothing to do. */ + if (!auth && !git_vector_length(challenges)) + return 0; + + if (!auth) { + challenge = init_auth_context(server, challenges, credentials); + auth = server->auth_context; + + if (!challenge || !auth) { + error = -1; + goto done; + } + } else if (auth->set_challenge) { + challenge = challenge_for_context(challenges, auth); + } + + if (auth->set_challenge && challenge && + (error = auth->set_challenge(auth, challenge)) < 0) + goto done; + + if ((error = auth->next_token(&token, auth, credentials)) < 0) + goto done; + + if (auth->is_complete && auth->is_complete(auth)) { + /* + * If we're done with an auth mechanism with connection affinity, + * we don't need to send any more headers and can dispose the context. + */ + if (auth->connection_affinity) + free_auth_context(server); + } else if (!token.size) { + git_error_set(GIT_ERROR_HTTP, "failed to respond to authentication challenge"); + error = GIT_EAUTH; + goto done; + } + + if (token.size > 0) + error = git_str_printf(buf, "%s: %s\r\n", header_name, token.ptr); + +done: + git_str_dispose(&token); + return error; +} + +GIT_INLINE(int) apply_server_credentials( + git_str *buf, + git_http_client *client, + git_http_request *request) +{ + return apply_credentials(buf, + &client->server, + "Authorization", + request->credentials); +} + +GIT_INLINE(int) apply_proxy_credentials( + git_str *buf, + git_http_client *client, + git_http_request *request) +{ + return apply_credentials(buf, + &client->proxy, + "Proxy-Authorization", + request->proxy_credentials); +} + +static int puts_host_and_port(git_str *buf, git_net_url *url, bool force_port) +{ + bool ipv6 = git_net_url_is_ipv6(url); + + if (ipv6) + git_str_putc(buf, '['); + + git_str_puts(buf, url->host); + + if (ipv6) + git_str_putc(buf, ']'); + + if (force_port || !git_net_url_is_default_port(url)) { + git_str_putc(buf, ':'); + git_str_puts(buf, url->port); + } + + return git_str_oom(buf) ? -1 : 0; +} + +static int append_user_agent(git_str *buf) +{ + const char *product = git_settings__user_agent_product(); + const char *comment = git_settings__user_agent(); + + GIT_ASSERT(product && comment); + + if (!*product) + return 0; + + git_str_puts(buf, "User-Agent: "); + git_str_puts(buf, product); + + if (*comment) { + git_str_puts(buf, " ("); + git_str_puts(buf, comment); + git_str_puts(buf, ")"); + } + + git_str_puts(buf, "\r\n"); + + return git_str_oom(buf) ? -1 : 0; +} + +static int generate_connect_request( + git_http_client *client, + git_http_request *request) +{ + git_str *buf; + int error; + + git_str_clear(&client->request_msg); + buf = &client->request_msg; + + git_str_puts(buf, "CONNECT "); + puts_host_and_port(buf, &client->server.url, true); + git_str_puts(buf, " HTTP/1.1\r\n"); + + append_user_agent(buf); + + git_str_puts(buf, "Host: "); + puts_host_and_port(buf, &client->server.url, true); + git_str_puts(buf, "\r\n"); + + if ((error = apply_proxy_credentials(buf, client, request) < 0)) + return -1; + + git_str_puts(buf, "\r\n"); + + return git_str_oom(buf) ? -1 : 0; +} + +static bool use_connect_proxy(git_http_client *client) +{ + return client->proxy.url.host && !strcmp(client->server.url.scheme, "https"); +} + +static int generate_request( + git_http_client *client, + git_http_request *request) +{ + git_str *buf; + size_t i; + int error; + + GIT_ASSERT_ARG(client); + GIT_ASSERT_ARG(request); + + git_str_clear(&client->request_msg); + buf = &client->request_msg; + + /* GET|POST path HTTP/1.1 */ + git_str_puts(buf, name_for_method(request->method)); + git_str_putc(buf, ' '); + + if (request->proxy && strcmp(request->url->scheme, "https")) + git_net_url_fmt(buf, request->url); + else + git_net_url_fmt_path(buf, request->url); + + git_str_puts(buf, " HTTP/1.1\r\n"); + + append_user_agent(buf); + + git_str_puts(buf, "Host: "); + puts_host_and_port(buf, request->url, false); + git_str_puts(buf, "\r\n"); + + if (request->accept) + git_str_printf(buf, "Accept: %s\r\n", request->accept); + else + git_str_puts(buf, "Accept: */*\r\n"); + + if (request->content_type) + git_str_printf(buf, "Content-Type: %s\r\n", + request->content_type); + + if (request->chunked) + git_str_puts(buf, "Transfer-Encoding: chunked\r\n"); + + if (request->content_length > 0) + git_str_printf(buf, "Content-Length: %"PRIuZ "\r\n", + request->content_length); + + if (request->expect_continue) + git_str_printf(buf, "Expect: 100-continue\r\n"); + + if ((error = apply_server_credentials(buf, client, request)) < 0 || + (!use_connect_proxy(client) && + (error = apply_proxy_credentials(buf, client, request)) < 0)) + return error; + + if (request->custom_headers) { + for (i = 0; i < request->custom_headers->count; i++) { + const char *hdr = request->custom_headers->strings[i]; + + if (hdr) + git_str_printf(buf, "%s\r\n", hdr); + } + } + + git_str_puts(buf, "\r\n"); + + if (git_str_oom(buf)) + return -1; + + return 0; +} + +static int check_certificate( + git_stream *stream, + git_net_url *url, + int is_valid, + git_transport_certificate_check_cb cert_cb, + void *cert_cb_payload) +{ + git_cert *cert; + git_error *last_error; + int error; + + if ((error = git_stream_certificate(&cert, stream)) < 0) + return error; + + /* + * Allow callers to set an error - but save ours and clear + * it, so that we can detect if they set one and restore it + * if we need to. + */ + git_error_save(&last_error); + git_error_clear(); + + error = cert_cb(cert, is_valid, url->host, cert_cb_payload); + + if (error == GIT_PASSTHROUGH) { + error = is_valid ? 0 : -1; + + if (error) { + git_error_restore(last_error); + last_error = NULL; + } + } else if (error) { + if (!git_error_exists()) + git_error_set(GIT_ERROR_HTTP, + "user rejected certificate for %s", + url->host); + } + + git_error_free(last_error); + return error; +} + +static int server_connect_stream( + git_http_server *server, + git_transport_certificate_check_cb cert_cb, + void *cb_payload) +{ + int error; + + GIT_ERROR_CHECK_VERSION(server->stream, GIT_STREAM_VERSION, "git_stream"); + + error = git_stream_connect(server->stream); + + if (error && error != GIT_ECERTIFICATE) + return error; + + if (git_stream_is_encrypted(server->stream) && cert_cb != NULL) + error = check_certificate(server->stream, &server->url, !error, + cert_cb, cb_payload); + + return error; +} + +static void reset_auth_connection(git_http_server *server) +{ + /* + * If we've authenticated and we're doing "normal" + * authentication with a request affinity (Basic, Digest) + * then we want to _keep_ our context, since authentication + * survives even through non-keep-alive connections. If + * we've authenticated and we're doing connection-based + * authentication (NTLM, Negotiate) - indicated by the presence + * of an `is_complete` callback - then we need to restart + * authentication on a new connection. + */ + + if (server->auth_context && + server->auth_context->connection_affinity) + free_auth_context(server); +} + +/* + * Updates the server data structure with the new URL; returns 1 if the server + * has changed and we need to reconnect, returns 0 otherwise. + */ +GIT_INLINE(int) server_setup_from_url( + git_http_server *server, + git_net_url *url) +{ + GIT_ASSERT_ARG(url); + GIT_ASSERT_ARG(url->scheme); + GIT_ASSERT_ARG(url->host); + GIT_ASSERT_ARG(url->port); + + if (!server->url.scheme || strcmp(server->url.scheme, url->scheme) || + !server->url.host || strcmp(server->url.host, url->host) || + !server->url.port || strcmp(server->url.port, url->port)) { + git__free(server->url.scheme); + git__free(server->url.host); + git__free(server->url.port); + + server->url.scheme = git__strdup(url->scheme); + GIT_ERROR_CHECK_ALLOC(server->url.scheme); + + server->url.host = git__strdup(url->host); + GIT_ERROR_CHECK_ALLOC(server->url.host); + + server->url.port = git__strdup(url->port); + GIT_ERROR_CHECK_ALLOC(server->url.port); + + return 1; + } + + return 0; +} + +static bool parser_settings_initialized; +static git_http_parser_settings parser_settings; + +GIT_INLINE(git_http_parser_settings *) http_client_parser_settings(void) +{ + if (!parser_settings_initialized) { + parser_settings.on_header_field = on_header_field; + parser_settings.on_header_value = on_header_value; + parser_settings.on_headers_complete = on_headers_complete; + parser_settings.on_body = on_body; + parser_settings.on_message_complete = on_message_complete; + + parser_settings_initialized = true; + } + + return &parser_settings; +} + +static void reset_parser(git_http_client *client) +{ + git_http_parser_init(&client->parser, + GIT_HTTP_PARSER_RESPONSE, + http_client_parser_settings()); +} + +static int setup_hosts( + git_http_client *client, + git_http_request *request) +{ + int ret, diff = 0; + + GIT_ASSERT_ARG(client); + GIT_ASSERT_ARG(request); + + GIT_ASSERT(request->url); + + if ((ret = server_setup_from_url(&client->server, request->url)) < 0) + return ret; + + diff |= ret; + + if (request->proxy && + (ret = server_setup_from_url(&client->proxy, request->proxy)) < 0) + return ret; + + diff |= ret; + + if (diff) { + free_auth_context(&client->server); + free_auth_context(&client->proxy); + + client->connected = 0; + } + + return 0; +} + +GIT_INLINE(int) server_create_stream(git_http_server *server) +{ + git_net_url *url = &server->url; + + if (strcasecmp(url->scheme, "https") == 0) + return git_tls_stream_new(&server->stream, url->host, url->port); + else if (strcasecmp(url->scheme, "http") == 0) + return git_socket_stream_new(&server->stream, url->host, url->port); + + git_error_set(GIT_ERROR_HTTP, "unknown http scheme '%s'", url->scheme); + return -1; +} + +GIT_INLINE(void) save_early_response( + git_http_client *client, + git_http_response *response) +{ + /* Buffer the response so we can return it in read_response */ + client->state = HAS_EARLY_RESPONSE; + + memcpy(&client->early_response, response, sizeof(git_http_response)); + memset(response, 0, sizeof(git_http_response)); +} + +static int proxy_connect( + git_http_client *client, + git_http_request *request) +{ + git_http_response response = {0}; + int error; + + if (!client->proxy_connected || !client->keepalive) { + git_trace(GIT_TRACE_DEBUG, "Connecting to proxy %s port %s", + client->proxy.url.host, client->proxy.url.port); + + if ((error = server_create_stream(&client->proxy)) < 0 || + (error = server_connect_stream(&client->proxy, + client->opts.proxy_certificate_check_cb, + client->opts.proxy_certificate_check_payload)) < 0) + goto done; + + client->proxy_connected = 1; + } + + client->current_server = PROXY; + client->state = SENDING_REQUEST; + + if ((error = generate_connect_request(client, request)) < 0 || + (error = client_write_request(client)) < 0) + goto done; + + client->state = SENT_REQUEST; + + if ((error = git_http_client_read_response(&response, client)) < 0 || + (error = git_http_client_skip_body(client)) < 0) + goto done; + + GIT_ASSERT(client->state == DONE); + + if (response.status == GIT_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED) { + save_early_response(client, &response); + + error = GIT_RETRY; + goto done; + } else if (response.status != GIT_HTTP_STATUS_OK) { + git_error_set(GIT_ERROR_HTTP, "proxy returned unexpected status: %d", response.status); + error = -1; + goto done; + } + + reset_parser(client); + client->state = NONE; + +done: + git_http_response_dispose(&response); + return error; +} + +static int server_connect(git_http_client *client) +{ + git_net_url *url = &client->server.url; + git_transport_certificate_check_cb cert_cb; + void *cert_payload; + int error; + + client->current_server = SERVER; + + if (client->proxy.stream) + error = git_tls_stream_wrap(&client->server.stream, client->proxy.stream, url->host); + else + error = server_create_stream(&client->server); + + if (error < 0) + goto done; + + cert_cb = client->opts.server_certificate_check_cb; + cert_payload = client->opts.server_certificate_check_payload; + + error = server_connect_stream(&client->server, cert_cb, cert_payload); + +done: + return error; +} + +GIT_INLINE(void) close_stream(git_http_server *server) +{ + if (server->stream) { + git_stream_close(server->stream); + git_stream_free(server->stream); + server->stream = NULL; + } +} + +static int http_client_connect( + git_http_client *client, + git_http_request *request) +{ + bool use_proxy = false; + int error; + + if ((error = setup_hosts(client, request)) < 0) + goto on_error; + + /* We're connected to our destination server; no need to reconnect */ + if (client->connected && client->keepalive && + (client->state == NONE || client->state == DONE)) + return 0; + + client->connected = 0; + client->request_count = 0; + + close_stream(&client->server); + reset_auth_connection(&client->server); + + reset_parser(client); + + /* Reconnect to the proxy if necessary. */ + use_proxy = use_connect_proxy(client); + + if (use_proxy) { + if (!client->proxy_connected || !client->keepalive || + (client->state != NONE && client->state != DONE)) { + close_stream(&client->proxy); + reset_auth_connection(&client->proxy); + + client->proxy_connected = 0; + } + + if ((error = proxy_connect(client, request)) < 0) + goto on_error; + } + + git_trace(GIT_TRACE_DEBUG, "Connecting to remote %s port %s", + client->server.url.host, client->server.url.port); + + if ((error = server_connect(client)) < 0) + goto on_error; + + client->connected = 1; + return error; + +on_error: + if (error != GIT_RETRY) + close_stream(&client->proxy); + + close_stream(&client->server); + return error; +} + +GIT_INLINE(int) client_read(git_http_client *client) +{ + http_parser_context *parser_context = client->parser.data; + git_stream *stream; + char *buf = client->read_buf.ptr + client->read_buf.size; + size_t max_len; + ssize_t read_len; + + stream = client->current_server == PROXY ? + client->proxy.stream : client->server.stream; + + /* + * We use a git_str for convenience, but statically allocate it and + * don't resize. Limit our consumption to INT_MAX since calling + * functions use an int return type to return number of bytes read. + */ + max_len = client->read_buf.asize - client->read_buf.size; + max_len = min(max_len, INT_MAX); + + if (parser_context->output_size) + max_len = min(max_len, parser_context->output_size); + + if (max_len == 0) { + git_error_set(GIT_ERROR_HTTP, "no room in output buffer"); + return -1; + } + + read_len = git_stream_read(stream, buf, max_len); + + if (read_len >= 0) { + client->read_buf.size += read_len; + + git_trace(GIT_TRACE_TRACE, "Received:\n%.*s", + (int)read_len, buf); + } + + return (int)read_len; +} + +GIT_INLINE(int) client_read_and_parse(git_http_client *client) +{ + git_http_parser *parser = &client->parser; + http_parser_context *ctx = (http_parser_context *) parser->data; + unsigned char http_errno; + int read_len; + size_t parsed_len; + + /* + * If we have data in our read buffer, that means we stopped early + * when parsing headers. Use the data in the read buffer instead of + * reading more from the socket. + */ + if (!client->read_buf.size && (read_len = client_read(client)) < 0) + return read_len; + + parsed_len = git_http_parser_execute(parser, + client->read_buf.ptr, + client->read_buf.size); + http_errno = git_http_parser_errno(parser); + + if (parsed_len > INT_MAX) { + git_error_set(GIT_ERROR_HTTP, "unexpectedly large parse"); + return -1; + } + + if (ctx->parse_status == PARSE_STATUS_ERROR) { + client->connected = 0; + return ctx->error ? ctx->error : -1; + } + + /* + * If we finished reading the headers or body, we paused parsing. + * Otherwise the parser will start filling the body, or even parse + * a new response if the server pipelined us multiple responses. + * (This can happen in response to an expect/continue request, + * where the server gives you a 100 and 200 simultaneously.) + */ + if (http_errno == GIT_HTTP_PARSER_PAUSED) { + size_t additional_size; + + git_http_parser_resume(parser); + + /* + * http-parser has a "feature" where it will not deliver + * the final byte when paused in a callback. Consume + * that byte. + */ + if ((additional_size = git_http_parser_remain_after_pause(parser)) > 0) { + GIT_ASSERT((client->read_buf.size - parsed_len) >= additional_size); + + parsed_len += git_http_parser_execute(parser, + client->read_buf.ptr + parsed_len, + additional_size); + } + } + + /* Most failures will be reported in http_errno */ + else if (git_http_parser_errno(parser) != GIT_HTTP_PARSER_OK) { + git_error_set(GIT_ERROR_HTTP, "http parser error: %s", + git_http_parser_errmsg(parser, http_errno)); + return -1; + } + + /* Otherwise we should have consumed the entire buffer. */ + else if (parsed_len != client->read_buf.size) { + git_error_set(GIT_ERROR_HTTP, + "http parser did not consume entire buffer: %s", + git_http_parser_errmsg(parser, http_errno)); + return -1; + } + + /* recv returned 0, the server hung up on us */ + else if (!parsed_len) { + git_error_set(GIT_ERROR_HTTP, "unexpected EOF"); + return -1; + } + + git_str_consume_bytes(&client->read_buf, parsed_len); + + return (int)parsed_len; +} + +/* + * See if we've consumed the entire response body. If the client was + * reading the body but did not consume it entirely, it's possible that + * they knew that the stream had finished (in a git response, seeing a + * final flush) and stopped reading. But if the response was chunked, + * we may have not consumed the final chunk marker. Consume it to + * ensure that we don't have it waiting in our socket. If there's + * more than just a chunk marker, close the connection. + */ +static void complete_response_body(git_http_client *client) +{ + http_parser_context parser_context = {0}; + + /* If we're not keeping alive, don't bother. */ + if (!client->keepalive) { + client->connected = 0; + goto done; + } + + parser_context.client = client; + client->parser.data = &parser_context; + + /* If there was an error, just close the connection. */ + if (client_read_and_parse(client) < 0 || + parser_context.error != GIT_HTTP_PARSER_OK || + (parser_context.parse_status != PARSE_STATUS_OK && + parser_context.parse_status != PARSE_STATUS_NO_OUTPUT)) { + git_error_clear(); + client->connected = 0; + } + +done: + client->parser.data = NULL; + git_str_clear(&client->read_buf); +} + +int git_http_client_send_request( + git_http_client *client, + git_http_request *request) +{ + git_http_response response = {0}; + int error = -1; + + GIT_ASSERT_ARG(client); + GIT_ASSERT_ARG(request); + + /* If the client did not finish reading, clean up the stream. */ + if (client->state == READING_BODY) + complete_response_body(client); + + /* If we're waiting for proxy auth, don't sending more requests. */ + if (client->state == HAS_EARLY_RESPONSE) + return 0; + + if (git_trace_level() >= GIT_TRACE_DEBUG) { + git_str url = GIT_STR_INIT; + git_net_url_fmt(&url, request->url); + git_trace(GIT_TRACE_DEBUG, "Sending %s request to %s", + name_for_method(request->method), + url.ptr ? url.ptr : ""); + git_str_dispose(&url); + } + + if ((error = http_client_connect(client, request)) < 0 || + (error = generate_request(client, request)) < 0 || + (error = client_write_request(client)) < 0) + goto done; + + client->state = SENT_REQUEST; + + if (request->expect_continue) { + if ((error = git_http_client_read_response(&response, client)) < 0 || + (error = git_http_client_skip_body(client)) < 0) + goto done; + + error = 0; + + if (response.status != GIT_HTTP_STATUS_CONTINUE) { + save_early_response(client, &response); + goto done; + } + } + + if (request->content_length || request->chunked) { + client->state = SENDING_BODY; + client->request_body_len = request->content_length; + client->request_body_remain = request->content_length; + client->request_chunked = request->chunked; + } + + reset_parser(client); + +done: + if (error == GIT_RETRY) + error = 0; + + git_http_response_dispose(&response); + return error; +} + +bool git_http_client_has_response(git_http_client *client) +{ + return (client->state == HAS_EARLY_RESPONSE || + client->state > SENT_REQUEST); +} + +int git_http_client_send_body( + git_http_client *client, + const char *buffer, + size_t buffer_len) +{ + git_http_server *server; + git_str hdr = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(client); + + /* If we're waiting for proxy auth, don't sending more requests. */ + if (client->state == HAS_EARLY_RESPONSE) + return 0; + + if (client->state != SENDING_BODY) { + git_error_set(GIT_ERROR_HTTP, "client is in invalid state"); + return -1; + } + + if (!buffer_len) + return 0; + + server = &client->server; + + if (client->request_body_len) { + GIT_ASSERT(buffer_len <= client->request_body_remain); + + if ((error = stream_write(server, buffer, buffer_len)) < 0) + goto done; + + client->request_body_remain -= buffer_len; + } else { + if ((error = git_str_printf(&hdr, "%" PRIxZ "\r\n", buffer_len)) < 0 || + (error = stream_write(server, hdr.ptr, hdr.size)) < 0 || + (error = stream_write(server, buffer, buffer_len)) < 0 || + (error = stream_write(server, "\r\n", 2)) < 0) + goto done; + } + +done: + git_str_dispose(&hdr); + return error; +} + +static int complete_request(git_http_client *client) +{ + int error = 0; + + GIT_ASSERT_ARG(client); + GIT_ASSERT(client->state == SENDING_BODY); + + if (client->request_body_len && client->request_body_remain) { + git_error_set(GIT_ERROR_HTTP, "truncated write"); + error = -1; + } else if (client->request_chunked) { + error = stream_write(&client->server, "0\r\n\r\n", 5); + } + + client->state = SENT_REQUEST; + return error; +} + +int git_http_client_read_response( + git_http_response *response, + git_http_client *client) +{ + http_parser_context parser_context = {0}; + int error; + + GIT_ASSERT_ARG(response); + GIT_ASSERT_ARG(client); + + if (client->state == SENDING_BODY) { + if ((error = complete_request(client)) < 0) + goto done; + } + + if (client->state == HAS_EARLY_RESPONSE) { + memcpy(response, &client->early_response, sizeof(git_http_response)); + memset(&client->early_response, 0, sizeof(git_http_response)); + client->state = DONE; + return 0; + } + + if (client->state != SENT_REQUEST) { + git_error_set(GIT_ERROR_HTTP, "client is in invalid state"); + error = -1; + goto done; + } + + git_http_response_dispose(response); + + if (client->current_server == PROXY) { + git_vector_dispose_deep(&client->proxy.auth_challenges); + } else if(client->current_server == SERVER) { + git_vector_dispose_deep(&client->server.auth_challenges); + } + + client->state = READING_RESPONSE; + client->keepalive = 0; + client->parser.data = &parser_context; + + parser_context.client = client; + parser_context.response = response; + + while (client->state == READING_RESPONSE) { + if ((error = client_read_and_parse(client)) < 0) + goto done; + } + + GIT_ASSERT(client->state == READING_BODY || client->state == DONE); + +done: + git_str_dispose(&parser_context.parse_header_name); + git_str_dispose(&parser_context.parse_header_value); + client->parser.data = NULL; + + return error; +} + +int git_http_client_read_body( + git_http_client *client, + char *buffer, + size_t buffer_size) +{ + http_parser_context parser_context = {0}; + int error = 0; + + if (client->state == DONE) + return 0; + + if (client->state != READING_BODY) { + git_error_set(GIT_ERROR_HTTP, "client is in invalid state"); + return -1; + } + + /* + * Now we'll read from the socket and http_parser will pipeline the + * data directly to the client. + */ + + parser_context.client = client; + parser_context.output_buf = buffer; + parser_context.output_size = buffer_size; + + client->parser.data = &parser_context; + + /* + * Clients expect to get a non-zero amount of data from us, + * so we either block until we have data to return, until we + * hit EOF or there's an error. Do this in a loop, since we + * may end up reading only some stream metadata (like chunk + * information). + */ + while (!parser_context.output_written) { + error = client_read_and_parse(client); + + if (error <= 0) + goto done; + + if (client->state == DONE) + break; + } + + GIT_ASSERT(parser_context.output_written <= INT_MAX); + error = (int)parser_context.output_written; + +done: + if (error < 0) + client->connected = 0; + + client->parser.data = NULL; + + return error; +} + +int git_http_client_skip_body(git_http_client *client) +{ + http_parser_context parser_context = {0}; + int error; + + if (client->state == DONE) + return 0; + + if (client->state != READING_BODY) { + git_error_set(GIT_ERROR_HTTP, "client is in invalid state"); + return -1; + } + + parser_context.client = client; + client->parser.data = &parser_context; + + do { + error = client_read_and_parse(client); + + if (parser_context.error != GIT_HTTP_PARSER_OK || + (parser_context.parse_status != PARSE_STATUS_OK && + parser_context.parse_status != PARSE_STATUS_NO_OUTPUT)) { + git_error_set(GIT_ERROR_HTTP, + "unexpected data handled in callback"); + error = -1; + } + } while (error >= 0 && client->state != DONE); + + if (error < 0) + client->connected = 0; + + client->parser.data = NULL; + + return error; +} + +/* + * Create an http_client capable of communicating with the given remote + * host. + */ +int git_http_client_new( + git_http_client **out, + git_http_client_options *opts) +{ + git_http_client *client; + + GIT_ASSERT_ARG(out); + + client = git__calloc(1, sizeof(git_http_client)); + GIT_ERROR_CHECK_ALLOC(client); + + git_str_init(&client->read_buf, GIT_READ_BUFFER_SIZE); + GIT_ERROR_CHECK_ALLOC(client->read_buf.ptr); + + if (opts) + memcpy(&client->opts, opts, sizeof(git_http_client_options)); + + *out = client; + return 0; +} + +/* Update the options of an existing httpclient instance. */ +void git_http_client_set_options( + git_http_client *client, + git_http_client_options *opts) +{ + if (opts) + memcpy(&client->opts, opts, sizeof(git_http_client_options)); +} + +GIT_INLINE(void) http_server_close(git_http_server *server) +{ + if (server->stream) { + git_stream_close(server->stream); + git_stream_free(server->stream); + server->stream = NULL; + } + + git_net_url_dispose(&server->url); + + git_vector_dispose_deep(&server->auth_challenges); + free_auth_context(server); +} + +static void http_client_close(git_http_client *client) +{ + http_server_close(&client->server); + http_server_close(&client->proxy); + + git_str_dispose(&client->request_msg); + + client->state = 0; + client->request_count = 0; + client->connected = 0; + client->keepalive = 0; +} + +void git_http_client_free(git_http_client *client) +{ + if (!client) + return; + + http_client_close(client); + git_str_dispose(&client->read_buf); + git__free(client); +} diff --git a/src/libgit2/transports/httpclient.h b/src/libgit2/transports/httpclient.h new file mode 100644 index 00000000000..22c4dd09384 --- /dev/null +++ b/src/libgit2/transports/httpclient.h @@ -0,0 +1,200 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_transports_httpclient_h__ +#define INCLUDE_transports_httpclient_h__ + +#include "common.h" +#include "net.h" + +#define GIT_HTTP_STATUS_CONTINUE 100 +#define GIT_HTTP_STATUS_OK 200 +#define GIT_HTTP_MOVED_PERMANENTLY 301 +#define GIT_HTTP_FOUND 302 +#define GIT_HTTP_SEE_OTHER 303 +#define GIT_HTTP_TEMPORARY_REDIRECT 307 +#define GIT_HTTP_PERMANENT_REDIRECT 308 +#define GIT_HTTP_STATUS_UNAUTHORIZED 401 +#define GIT_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED 407 + +typedef struct git_http_client git_http_client; + +/** Method for the HTTP request */ +typedef enum { + GIT_HTTP_METHOD_GET, + GIT_HTTP_METHOD_POST, + GIT_HTTP_METHOD_CONNECT +} git_http_method; + +/** An HTTP request */ +typedef struct { + git_http_method method; /**< Method for the request */ + git_net_url *url; /**< Full request URL */ + git_net_url *proxy; /**< Proxy to use */ + + /* Headers */ + const char *accept; /**< Contents of the Accept header */ + const char *content_type; /**< Content-Type header (for POST) */ + git_credential *credentials; /**< Credentials to authenticate with */ + git_credential *proxy_credentials; /**< Credentials for proxy */ + git_strarray *custom_headers; /**< Additional headers to deliver */ + + /* To POST a payload, either set content_length OR set chunked. */ + size_t content_length; /**< Length of the POST body */ + unsigned chunked : 1, /**< Post with chunking */ + expect_continue : 1; /**< Use expect/continue negotiation */ +} git_http_request; + +typedef struct { + int status; + + /* Headers */ + char *content_type; + size_t content_length; + char *location; + + /* Authentication headers */ + unsigned server_auth_schemetypes; /**< Schemes requested by remote */ + unsigned server_auth_credtypes; /**< Supported cred types for remote */ + + unsigned proxy_auth_schemetypes; /**< Schemes requested by proxy */ + unsigned proxy_auth_credtypes; /**< Supported cred types for proxy */ + + unsigned chunked : 1, /**< Response body is chunked */ + resend_credentials : 1; /**< Resend with authentication */ +} git_http_response; + +typedef struct { + /** Certificate check callback for the remote */ + git_transport_certificate_check_cb server_certificate_check_cb; + void *server_certificate_check_payload; + + /** Certificate check callback for the proxy */ + git_transport_certificate_check_cb proxy_certificate_check_cb; + void *proxy_certificate_check_payload; +} git_http_client_options; + +/** + * Create a new httpclient instance with the given options. + * + * @param out pointer to receive the new instance + * @param opts options to create the client with or NULL for defaults + */ +extern int git_http_client_new( + git_http_client **out, + git_http_client_options *opts); + +/** + * Update the options of an existing httpclient instance. + * + * @param client the httpclient instance to modify + * @param opts new options or NULL to keep existing options + */ +extern void git_http_client_set_options( + git_http_client *client, + git_http_client_options *opts); + +/* + * Sends a request to the host specified by the request URL. If the + * method is POST, either the content_length or the chunked flag must + * be specified. The body should be provided in subsequent calls to + * git_http_client_send_body. + * + * @param client the client to write the request to + * @param request the request to send + */ +extern int git_http_client_send_request( + git_http_client *client, + git_http_request *request); + +/* + * After sending a request, there may already be a response to read -- + * either because there was a non-continue response to an expect: continue + * request, or because the server pipelined a response to us before we even + * sent the request. Examine the state. + * + * @param client the client to examine + * @return true if there's already a response to read, false otherwise + */ +extern bool git_http_client_has_response(git_http_client *client); + +/** + * Sends the given buffer to the remote as part of the request body. The + * request must have specified either a content_length or the chunked flag. + * + * @param client the client to write the request body to + * @param buffer the request body + * @param buffer_len number of bytes of the buffer to send + */ +extern int git_http_client_send_body( + git_http_client *client, + const char *buffer, + size_t buffer_len); + +/** + * Reads the headers of a response to a request. This will consume the + * entirety of the headers of a response from the server. The body (if any) + * can be read by calling git_http_client_read_body. Callers must free + * the response with git_http_response_dispose. + * + * @param response pointer to the response object to fill + * @param client the client to read the response from + */ +extern int git_http_client_read_response( + git_http_response *response, + git_http_client *client); + +/** + * Reads some or all of the body of a response. At most buffer_size (or + * INT_MAX) bytes will be read and placed into the buffer provided. The + * number of bytes read will be returned, or 0 to indicate that the end of + * the body has been read. + * + * @param client the client to read the response from + * @param buffer pointer to the buffer to fill + * @param buffer_size the maximum number of bytes to read + * @return the number of bytes read, 0 on end of body, or error code + */ +extern int git_http_client_read_body( + git_http_client *client, + char *buffer, + size_t buffer_size); + +/** + * Reads all of the (remainder of the) body of the response and ignores it. + * None of the data from the body will be returned to the caller. + * + * @param client the client to read the response from + * @return 0 or an error code + */ +extern int git_http_client_skip_body(git_http_client *client); + +/** + * Examines the status code of the response to determine if it is a + * redirect of any type (eg, 301, 302, etc). + * + * @param response the response to inspect + * @return true if the response is a redirect, false otherwise + */ +extern bool git_http_response_is_redirect(git_http_response *response); + +/** + * Frees any memory associated with the response. + * + * @param response the response to free + */ +extern void git_http_response_dispose(git_http_response *response); + +/** + * Frees any memory associated with the client. If any sockets are open, + * they will be closed. + * + * @param client the client to free + */ +extern void git_http_client_free(git_http_client *client); + +#endif diff --git a/src/libgit2/transports/httpparser.c b/src/libgit2/transports/httpparser.c new file mode 100644 index 00000000000..84833e61737 --- /dev/null +++ b/src/libgit2/transports/httpparser.c @@ -0,0 +1,128 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "httpparser.h" + +#include + +#if defined(GIT_HTTPPARSER_HTTPPARSER) + +#include "http_parser.h" + +static int on_message_begin(http_parser *p) +{ + git_http_parser *parser = (git_http_parser *)p; + return parser->settings.on_message_begin(parser); +} + +static int on_url(http_parser *p, const char *str, size_t len) +{ + git_http_parser *parser = (git_http_parser *)p; + return parser->settings.on_url(parser, str, len); +} + +static int on_header_field(http_parser *p, const char *str, size_t len) +{ + git_http_parser *parser = (git_http_parser *)p; + return parser->settings.on_header_field(parser, str, len); +} + +static int on_header_value(http_parser *p, const char *str, size_t len) +{ + git_http_parser *parser = (git_http_parser *)p; + return parser->settings.on_header_value(parser, str, len); +} + +static int on_headers_complete(http_parser *p) +{ + git_http_parser *parser = (git_http_parser *)p; + return parser->settings.on_headers_complete(parser); +} + +static int on_body(http_parser *p, const char *buf, size_t len) +{ + git_http_parser *parser = (git_http_parser *)p; + return parser->settings.on_body(parser, buf, len); +} + +static int on_message_complete(http_parser *p) +{ + git_http_parser *parser = (git_http_parser *)p; + return parser->settings.on_message_complete(parser); +} + +void git_http_parser_init( + git_http_parser *parser, + git_http_parser_t type, + git_http_parser_settings *settings) +{ + http_parser_init(&parser->parser, (enum http_parser_type)type); + memcpy(&parser->settings, settings, sizeof(git_http_parser_settings)); +} + +size_t git_http_parser_execute( + git_http_parser *parser, + const char *data, + size_t len) +{ + struct http_parser_settings settings_proxy; + + memset(&settings_proxy, 0, sizeof(struct http_parser_settings)); + + settings_proxy.on_message_begin = parser->settings.on_message_begin ? on_message_begin : NULL; + settings_proxy.on_url = parser->settings.on_url ? on_url : NULL; + settings_proxy.on_header_field = parser->settings.on_header_field ? on_header_field : NULL; + settings_proxy.on_header_value = parser->settings.on_header_value ? on_header_value : NULL; + settings_proxy.on_headers_complete = parser->settings.on_headers_complete ? on_headers_complete : NULL; + settings_proxy.on_body = parser->settings.on_body ? on_body : NULL; + settings_proxy.on_message_complete = parser->settings.on_message_complete ? on_message_complete : NULL; + + return http_parser_execute(&parser->parser, &settings_proxy, data, len); +} + +#elif defined(GIT_HTTPPARSER_LLHTTP) || defined(GIT_HTTPPARSER_BUILTIN) + +# include + +size_t git_http_parser_execute( + git_http_parser *parser, + const char* data, + size_t len) +{ + llhttp_errno_t error; + size_t parsed_len; + + /* + * Unlike http_parser, which returns the number of parsed + * bytes in the _execute() call, llhttp returns an error + * code. + */ + + if (data == NULL || len == 0) + error = llhttp_finish(parser); + else + error = llhttp_execute(parser, data, len); + + parsed_len = len; + + /* + * Adjust number of parsed bytes in case of error. + */ + if (error != HPE_OK) { + parsed_len = llhttp_get_error_pos(parser) - data; + + /* This isn't a real pause, just a way to stop parsing early. */ + if (error == HPE_PAUSED_UPGRADE) + llhttp_resume_after_upgrade(parser); + } + + return parsed_len; +} + +#else +# error unknown http-parser +#endif diff --git a/src/libgit2/transports/httpparser.h b/src/libgit2/transports/httpparser.h new file mode 100644 index 00000000000..1fe0dcf6406 --- /dev/null +++ b/src/libgit2/transports/httpparser.h @@ -0,0 +1,99 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_transports_httpparser_h__ +#define INCLUDE_transports_httpparser_h__ + +#include "git2_util.h" + +#if defined(GIT_HTTPPARSER_HTTPPARSER) + +# include + +typedef enum { + GIT_HTTP_PARSER_OK = HPE_OK, + GIT_HTTP_PARSER_PAUSED = HPE_PAUSED, +} git_http_parser_error_t; + +typedef enum { + GIT_HTTP_PARSER_REQUEST = HTTP_REQUEST, + GIT_HTTP_PARSER_RESPONSE = HTTP_RESPONSE, +} git_http_parser_t; + +typedef struct git_http_parser git_http_parser; + +typedef struct { + int (*on_message_begin)(git_http_parser *); + int (*on_url)(git_http_parser *, const char *, size_t); + int (*on_header_field)(git_http_parser *, const char *, size_t); + int (*on_header_value)(git_http_parser *, const char *, size_t); + int (*on_headers_complete)(git_http_parser *); + int (*on_body)(git_http_parser *, const char *, size_t); + int (*on_message_complete)(git_http_parser *); +} git_http_parser_settings; + +struct git_http_parser { + http_parser parser; + git_http_parser_settings settings; + void *data; +}; + +void git_http_parser_init( + git_http_parser *parser, + git_http_parser_t type, + git_http_parser_settings *settings); + +size_t git_http_parser_execute( + git_http_parser *parser, + const char *data, + size_t len); + +# define git_http_parser_status_code(parser) parser->parser.status_code +# define git_http_parser_keep_alive(parser) http_should_keep_alive(&parser->parser) +# define git_http_parser_pause(parser) (http_parser_pause(&parser->parser, 1), 0) +# define git_http_parser_resume(parser) http_parser_pause(&parser->parser, 0) +# define git_http_parser_remain_after_pause(parser) 1 +# define git_http_parser_errno(parser) parser->parser.http_errno +# define git_http_parser_errmsg(parser, errno) http_errno_description(errno) + +#elif defined(GIT_HTTPPARSER_LLHTTP) || defined(GIT_HTTPPARSER_BUILTIN) + +# include + +typedef enum { + GIT_HTTP_PARSER_OK = HPE_OK, + GIT_HTTP_PARSER_PAUSED = HPE_PAUSED, +} git_http_parser_error_t; + +typedef enum { + GIT_HTTP_PARSER_REQUEST = HTTP_REQUEST, + GIT_HTTP_PARSER_RESPONSE = HTTP_RESPONSE, +} git_http_parser_t; + +typedef llhttp_t git_http_parser; +typedef llhttp_settings_t git_http_parser_settings; + +# define git_http_parser_init(parser, direction, settings) llhttp_init(parser, (llhttp_type_t)direction, settings) + +size_t git_http_parser_execute( + git_http_parser *parser, + const char *data, + size_t len); + +# define git_http_parser_status_code(parser) parser->status_code +# define git_http_parser_keep_alive(parser) llhttp_should_keep_alive(parser) +# define git_http_parser_pause(parser) (llhttp_pause(parser), GIT_HTTP_PARSER_PAUSED) +# define git_http_parser_resume(parser) llhttp_resume(parser) +# define git_http_parser_remain_after_pause(parser) 0 +# define git_http_parser_errno(parser) parser->error +# define git_http_parser_errmsg(parser, errno) llhttp_get_error_reason(parser) + +#else +# error unknown http-parser +#endif + +#endif diff --git a/src/libgit2/transports/local.c b/src/libgit2/transports/local.c new file mode 100644 index 00000000000..854390534f2 --- /dev/null +++ b/src/libgit2/transports/local.c @@ -0,0 +1,778 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "pack-objects.h" +#include "refs.h" +#include "posix.h" +#include "fs_path.h" +#include "repository.h" +#include "odb.h" +#include "push.h" +#include "remote.h" +#include "proxy.h" + +#include "git2/types.h" +#include "git2/net.h" +#include "git2/repository.h" +#include "git2/object.h" +#include "git2/tag.h" +#include "git2/transport.h" +#include "git2/revwalk.h" +#include "git2/odb_backend.h" +#include "git2/pack.h" +#include "git2/commit.h" +#include "git2/revparse.h" +#include "git2/sys/remote.h" + +typedef struct { + git_transport parent; + git_remote *owner; + char *url; + int direction; + git_atomic32 cancelled; + git_repository *repo; + git_remote_connect_options connect_opts; + git_vector refs; + unsigned connected : 1, + have_refs : 1; +} transport_local; + +static void free_head(git_remote_head *head) +{ + git__free(head->name); + git__free(head->symref_target); + git__free(head); +} + +static void free_heads(git_vector *heads) +{ + git_remote_head *head; + size_t i; + + git_vector_foreach(heads, i, head) + free_head(head); + + git_vector_dispose(heads); +} + +static int add_ref(transport_local *t, const char *name) +{ + const char peeled[] = "^{}"; + git_reference *ref, *resolved; + git_remote_head *head; + git_oid obj_id; + git_object *obj = NULL, *target = NULL; + git_str buf = GIT_STR_INIT; + int error; + + if ((error = git_reference_lookup(&ref, t->repo, name)) < 0) + return error; + + error = git_reference_resolve(&resolved, ref); + if (error < 0) { + git_reference_free(ref); + if (!strcmp(name, GIT_HEAD_FILE) && error == GIT_ENOTFOUND) { + /* This is actually okay. Empty repos often have a HEAD that + * points to a nonexistent "refs/heads/master". */ + git_error_clear(); + return 0; + } + return error; + } + + git_oid_cpy(&obj_id, git_reference_target(resolved)); + git_reference_free(resolved); + + head = git__calloc(1, sizeof(git_remote_head)); + GIT_ERROR_CHECK_ALLOC(head); + + head->name = git__strdup(name); + GIT_ERROR_CHECK_ALLOC(head->name); + + git_oid_cpy(&head->oid, &obj_id); + + if (git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC) { + head->symref_target = git__strdup(git_reference_symbolic_target(ref)); + GIT_ERROR_CHECK_ALLOC(head->symref_target); + } + git_reference_free(ref); + + if ((error = git_vector_insert(&t->refs, head)) < 0) { + free_head(head); + return error; + } + + if ((error = git_object_lookup(&obj, t->repo, &head->oid, GIT_OBJECT_ANY)) < 0) + return error; + + head = NULL; + + /* If it's not an annotated tag, or if we're mocking + * git-receive-pack, just get out */ + if (git_object_type(obj) != GIT_OBJECT_TAG || + t->direction != GIT_DIRECTION_FETCH) { + git_object_free(obj); + return 0; + } + + /* And if it's a tag, peel it, and add it to the list */ + head = git__calloc(1, sizeof(git_remote_head)); + GIT_ERROR_CHECK_ALLOC(head); + + if (git_str_join(&buf, 0, name, peeled) < 0) { + free_head(head); + return -1; + } + head->name = git_str_detach(&buf); + + if (!(error = git_tag_peel(&target, (git_tag *)obj))) { + git_oid_cpy(&head->oid, git_object_id(target)); + + if ((error = git_vector_insert(&t->refs, head)) < 0) { + free_head(head); + } + } + + git_object_free(obj); + git_object_free(target); + + return error; +} + +static int store_refs(transport_local *t) +{ + size_t i; + git_remote_head *head; + git_strarray ref_names = {0}; + + GIT_ASSERT_ARG(t); + + if (git_reference_list(&ref_names, t->repo) < 0) + goto on_error; + + /* Clear all heads we might have fetched in a previous connect */ + git_vector_foreach(&t->refs, i, head) { + git__free(head->name); + git__free(head); + } + + /* Clear the vector so we can reuse it */ + git_vector_clear(&t->refs); + + /* Sort the references first */ + git__tsort((void **)ref_names.strings, ref_names.count, &git__strcmp_cb); + + /* Add HEAD iff direction is fetch */ + if (t->direction == GIT_DIRECTION_FETCH && add_ref(t, GIT_HEAD_FILE) < 0) + goto on_error; + + for (i = 0; i < ref_names.count; ++i) { + if (add_ref(t, ref_names.strings[i]) < 0) + goto on_error; + } + + t->have_refs = 1; + git_strarray_dispose(&ref_names); + return 0; + +on_error: + git_vector_dispose(&t->refs); + git_strarray_dispose(&ref_names); + return -1; +} + +/* + * Try to open the url as a git directory. The direction doesn't + * matter in this case because we're calculating the heads ourselves. + */ +static int local_connect( + git_transport *transport, + const char *url, + int direction, + const git_remote_connect_options *connect_opts) +{ + git_repository *repo; + int error; + transport_local *t = (transport_local *)transport; + const char *path; + git_str buf = GIT_STR_INIT; + + if (t->connected) + return 0; + + if (git_remote_connect_options_normalize(&t->connect_opts, t->owner->repo, connect_opts) < 0) + return -1; + + free_heads(&t->refs); + + t->url = git__strdup(url); + GIT_ERROR_CHECK_ALLOC(t->url); + t->direction = direction; + + /* 'url' may be a url or path; convert to a path */ + if ((error = git_fs_path_from_url_or_path(&buf, url)) < 0) { + git_str_dispose(&buf); + return error; + } + path = git_str_cstr(&buf); + + error = git_repository_open(&repo, path); + + git_str_dispose(&buf); + + if (error < 0) + return -1; + + t->repo = repo; + + if (store_refs(t) < 0) + return -1; + + t->connected = 1; + + return 0; +} + +static int local_set_connect_opts( + git_transport *transport, + const git_remote_connect_options *connect_opts) +{ + transport_local *t = (transport_local *)transport; + + if (!t->connected) { + git_error_set(GIT_ERROR_NET, "cannot reconfigure a transport that is not connected"); + return -1; + } + + return git_remote_connect_options_normalize(&t->connect_opts, t->owner->repo, connect_opts); +} + +static int local_capabilities(unsigned int *capabilities, git_transport *transport) +{ + GIT_UNUSED(transport); + + *capabilities = GIT_REMOTE_CAPABILITY_TIP_OID | + GIT_REMOTE_CAPABILITY_REACHABLE_OID; + return 0; +} + +#ifdef GIT_EXPERIMENTAL_SHA256 +static int local_oid_type(git_oid_t *out, git_transport *transport) +{ + transport_local *t = (transport_local *)transport; + + *out = t->repo->oid_type; + + return 0; +} +#endif + +static int local_ls(const git_remote_head ***out, size_t *size, git_transport *transport) +{ + transport_local *t = (transport_local *)transport; + + if (!t->have_refs) { + git_error_set(GIT_ERROR_NET, "the transport has not yet loaded the refs"); + return -1; + } + + *out = (const git_remote_head **)t->refs.contents; + *size = t->refs.length; + + return 0; +} + +static int local_negotiate_fetch( + git_transport *transport, + git_repository *repo, + const git_fetch_negotiation *wants) +{ + transport_local *t = (transport_local*)transport; + git_remote_head *rhead; + unsigned int i; + + GIT_UNUSED(wants); + + if (wants->depth) { + git_error_set(GIT_ERROR_NET, "shallow fetch is not supported by the local transport"); + return GIT_ENOTSUPPORTED; + } + + /* Fill in the loids */ + git_vector_foreach(&t->refs, i, rhead) { + git_object *obj; + + int error = git_revparse_single(&obj, repo, rhead->name); + if (!error) + git_oid_cpy(&rhead->loid, git_object_id(obj)); + else if (error != GIT_ENOTFOUND) + return error; + else + git_error_clear(); + git_object_free(obj); + } + + return 0; +} + +static int local_shallow_roots( + git_oidarray *out, + git_transport *transport) +{ + GIT_UNUSED(out); + GIT_UNUSED(transport); + + return 0; +} + +static int local_push_update_remote_ref( + git_repository *remote_repo, + const char *lref, + const char *rref, + git_oid *loid, + git_oid *roid) +{ + int error; + git_reference *remote_ref = NULL; + + /* check for lhs, if it's empty it means to delete */ + if (lref[0] != '\0') { + /* Create or update a ref */ + error = git_reference_create(NULL, remote_repo, rref, loid, + !git_oid_is_zero(roid), NULL); + } else { + /* Delete a ref */ + if ((error = git_reference_lookup(&remote_ref, remote_repo, rref)) < 0) { + if (error == GIT_ENOTFOUND) + error = 0; + return error; + } + + error = git_reference_delete(remote_ref); + git_reference_free(remote_ref); + } + + return error; +} + +static int transfer_to_push_transfer(const git_indexer_progress *stats, void *payload) +{ + const git_remote_callbacks *cbs = payload; + + if (!cbs || !cbs->push_transfer_progress) + return 0; + + return cbs->push_transfer_progress(stats->received_objects, stats->total_objects, stats->received_bytes, + cbs->payload); +} + +static int local_push( + git_transport *transport, + git_push *push) +{ + transport_local *t = (transport_local *)transport; + git_remote_callbacks *cbs = &t->connect_opts.callbacks; + git_repository *remote_repo = NULL; + push_spec *spec; + char *url = NULL; + const char *path; + git_str buf = GIT_STR_INIT, odb_path = GIT_STR_INIT; + int error; + size_t j; + + /* 'push->remote->url' may be a url or path; convert to a path */ + if ((error = git_fs_path_from_url_or_path(&buf, push->remote->url)) < 0) { + git_str_dispose(&buf); + return error; + } + path = git_str_cstr(&buf); + + error = git_repository_open(&remote_repo, path); + + git_str_dispose(&buf); + + if (error < 0) + return error; + + /* We don't currently support pushing locally to non-bare repos. Proper + non-bare repo push support would require checking configs to see if + we should override the default 'don't let this happen' behavior. + + Note that this is only an issue when pushing to the current branch, + but we forbid all pushes just in case */ + if (!remote_repo->is_bare) { + error = GIT_EBAREREPO; + git_error_set(GIT_ERROR_INVALID, "local push doesn't (yet) support pushing to non-bare repos."); + goto on_error; + } + + if ((error = git_repository__item_path(&odb_path, remote_repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0 + || (error = git_str_joinpath(&odb_path, odb_path.ptr, "pack")) < 0) + goto on_error; + + error = git_packbuilder_write(push->pb, odb_path.ptr, 0, transfer_to_push_transfer, (void *) cbs); + git_str_dispose(&odb_path); + + if (error < 0) + goto on_error; + + push->unpack_ok = 1; + + git_vector_foreach(&push->specs, j, spec) { + push_status *status; + const git_error *last; + char *ref = spec->refspec.dst; + + status = git__calloc(1, sizeof(push_status)); + if (!status) + goto on_error; + + status->ref = git__strdup(ref); + if (!status->ref) { + git_push_status_free(status); + goto on_error; + } + + error = local_push_update_remote_ref(remote_repo, spec->refspec.src, spec->refspec.dst, + &spec->loid, &spec->roid); + + switch (error) { + case GIT_OK: + break; + case GIT_EINVALIDSPEC: + status->msg = git__strdup("funny refname"); + break; + case GIT_ENOTFOUND: + status->msg = git__strdup("Remote branch not found to delete"); + break; + default: + last = git_error_last(); + + if (last->klass != GIT_ERROR_NONE) + status->msg = git__strdup(last->message); + else + status->msg = git__strdup("Unspecified error encountered"); + break; + } + + /* failed to allocate memory for a status message */ + if (error < 0 && !status->msg) { + git_push_status_free(status); + goto on_error; + } + + /* failed to insert the ref update status */ + if ((error = git_vector_insert(&push->status, status)) < 0) { + git_push_status_free(status); + goto on_error; + } + } + + if (push->specs.length) { + url = git__strdup(t->url); + + if (!url || t->parent.close(&t->parent) < 0 || + t->parent.connect(&t->parent, url, + GIT_DIRECTION_PUSH, NULL)) + goto on_error; + } + + error = 0; + +on_error: + git_repository_free(remote_repo); + git__free(url); + + return error; +} + +typedef struct foreach_data { + git_indexer_progress *stats; + git_indexer_progress_cb progress_cb; + void *progress_payload; + git_odb_writepack *writepack; +} foreach_data; + +static int foreach_cb(void *buf, size_t len, void *payload) +{ + foreach_data *data = (foreach_data*)payload; + + data->stats->received_bytes += len; + return data->writepack->append(data->writepack, buf, len, data->stats); +} + +static const char *counting_objects_fmt = "Counting objects %d\r"; +static const char *compressing_objects_fmt = "Compressing objects: %.0f%% (%d/%d)"; + +static int local_counting(int stage, unsigned int current, unsigned int total, void *payload) +{ + git_str progress_info = GIT_STR_INIT; + transport_local *t = payload; + int error; + + if (!t->connect_opts.callbacks.sideband_progress) + return 0; + + if (stage == GIT_PACKBUILDER_ADDING_OBJECTS) { + git_str_printf(&progress_info, counting_objects_fmt, current); + } else if (stage == GIT_PACKBUILDER_DELTAFICATION) { + float perc = (((float) current) / total) * 100; + git_str_printf(&progress_info, compressing_objects_fmt, perc, current, total); + if (current == total) + git_str_printf(&progress_info, ", done\n"); + else + git_str_putc(&progress_info, '\r'); + + } + + if (git_str_oom(&progress_info)) + return -1; + + if (progress_info.size > INT_MAX) { + git_error_set(GIT_ERROR_NET, "remote sent overly large progress data"); + git_str_dispose(&progress_info); + return -1; + } + + + error = t->connect_opts.callbacks.sideband_progress( + progress_info.ptr, + (int)progress_info.size, + t->connect_opts.callbacks.payload); + + git_str_dispose(&progress_info); + return error; +} + +static int foreach_reference_cb(git_reference *reference, void *payload) +{ + git_revwalk *walk = (git_revwalk *)payload; + int error; + + if (git_reference_type(reference) != GIT_REFERENCE_DIRECT) { + git_reference_free(reference); + return 0; + } + + error = git_revwalk_hide(walk, git_reference_target(reference)); + /* The reference is in the local repository, so the target may not + * exist on the remote. It also may not be a commit. */ + if (error == GIT_ENOTFOUND || error == GIT_ERROR_INVALID) { + git_error_clear(); + error = 0; + } + + git_reference_free(reference); + + return error; +} + +static int local_download_pack( + git_transport *transport, + git_repository *repo, + git_indexer_progress *stats) +{ + transport_local *t = (transport_local*)transport; + git_revwalk *walk = NULL; + git_remote_head *rhead; + unsigned int i; + int error = -1; + git_packbuilder *pack = NULL; + git_odb_writepack *writepack = NULL; + git_odb *odb = NULL; + git_str progress_info = GIT_STR_INIT; + foreach_data data = {0}; + + if ((error = git_revwalk_new(&walk, t->repo)) < 0) + goto cleanup; + + git_revwalk_sorting(walk, GIT_SORT_TIME); + + if ((error = git_packbuilder_new(&pack, t->repo)) < 0) + goto cleanup; + + git_packbuilder_set_callbacks(pack, local_counting, t); + + stats->total_objects = 0; + stats->indexed_objects = 0; + stats->received_objects = 0; + stats->received_bytes = 0; + + git_vector_foreach(&t->refs, i, rhead) { + git_object *obj; + if ((error = git_object_lookup(&obj, t->repo, &rhead->oid, GIT_OBJECT_ANY)) < 0) + goto cleanup; + + if (git_object_type(obj) == GIT_OBJECT_COMMIT) { + /* Revwalker includes only wanted commits */ + error = git_revwalk_push(walk, &rhead->oid); + } else { + /* Tag or some other wanted object. Add it on its own */ + error = git_packbuilder_insert_recur(pack, &rhead->oid, rhead->name); + } + git_object_free(obj); + if (error < 0) + goto cleanup; + } + + if ((error = git_reference_foreach(repo, foreach_reference_cb, walk))) + goto cleanup; + + if ((error = git_packbuilder_insert_walk(pack, walk))) + goto cleanup; + + if (t->connect_opts.callbacks.sideband_progress) { + if ((error = git_str_printf( + &progress_info, + counting_objects_fmt, + git_packbuilder_object_count(pack))) < 0 || + (error = t->connect_opts.callbacks.sideband_progress( + progress_info.ptr, + (int)progress_info.size, + t->connect_opts.callbacks.payload)) < 0) + goto cleanup; + } + + /* Walk the objects, building a packfile */ + if ((error = git_repository_odb__weakptr(&odb, repo)) < 0) + goto cleanup; + + /* One last one with the newline */ + if (t->connect_opts.callbacks.sideband_progress) { + git_str_clear(&progress_info); + + if ((error = git_str_printf( + &progress_info, + counting_objects_fmt, + git_packbuilder_object_count(pack))) < 0 || + (error = git_str_putc(&progress_info, '\n')) < 0 || + (error = t->connect_opts.callbacks.sideband_progress( + progress_info.ptr, + (int)progress_info.size, + t->connect_opts.callbacks.payload)) < 0) + goto cleanup; + } + + if ((error = git_odb_write_pack( + &writepack, + odb, + t->connect_opts.callbacks.transfer_progress, + t->connect_opts.callbacks.payload)) < 0) + goto cleanup; + + /* Write the data to the ODB */ + data.stats = stats; + data.progress_cb = t->connect_opts.callbacks.transfer_progress; + data.progress_payload = t->connect_opts.callbacks.payload; + data.writepack = writepack; + + /* autodetect */ + git_packbuilder_set_threads(pack, 0); + + if ((error = git_packbuilder_foreach(pack, foreach_cb, &data)) != 0) + goto cleanup; + + error = writepack->commit(writepack, stats); + +cleanup: + if (writepack) writepack->free(writepack); + git_str_dispose(&progress_info); + git_packbuilder_free(pack); + git_revwalk_free(walk); + return error; +} + +static int local_is_connected(git_transport *transport) +{ + transport_local *t = (transport_local *)transport; + + return t->connected; +} + +static void local_cancel(git_transport *transport) +{ + transport_local *t = (transport_local *)transport; + + git_atomic32_set(&t->cancelled, 1); +} + +static int local_close(git_transport *transport) +{ + transport_local *t = (transport_local *)transport; + + t->connected = 0; + + if (t->repo) { + git_repository_free(t->repo); + t->repo = NULL; + } + + if (t->url) { + git__free(t->url); + t->url = NULL; + } + + return 0; +} + +static void local_free(git_transport *transport) +{ + transport_local *t = (transport_local *)transport; + + free_heads(&t->refs); + + /* Close the transport, if it's still open. */ + local_close(transport); + + /* Free the transport */ + git__free(t); +} + +/************** + * Public API * + **************/ + +int git_transport_local(git_transport **out, git_remote *owner, void *param) +{ + int error; + transport_local *t; + + GIT_UNUSED(param); + + t = git__calloc(1, sizeof(transport_local)); + GIT_ERROR_CHECK_ALLOC(t); + + t->parent.version = GIT_TRANSPORT_VERSION; + t->parent.connect = local_connect; + t->parent.set_connect_opts = local_set_connect_opts; + t->parent.capabilities = local_capabilities; +#ifdef GIT_EXPERIMENTAL_SHA256 + t->parent.oid_type = local_oid_type; +#endif + t->parent.negotiate_fetch = local_negotiate_fetch; + t->parent.shallow_roots = local_shallow_roots; + t->parent.download_pack = local_download_pack; + t->parent.push = local_push; + t->parent.close = local_close; + t->parent.free = local_free; + t->parent.ls = local_ls; + t->parent.is_connected = local_is_connected; + t->parent.cancel = local_cancel; + + if ((error = git_vector_init(&t->refs, 0, NULL)) < 0) { + git__free(t); + return error; + } + + t->owner = owner; + + *out = (git_transport *) t; + + return 0; +} diff --git a/src/libgit2/transports/smart.c b/src/libgit2/transports/smart.c new file mode 100644 index 00000000000..ebd5a5b8663 --- /dev/null +++ b/src/libgit2/transports/smart.c @@ -0,0 +1,537 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "smart.h" + +#include "git2.h" +#include "git2/sys/remote.h" +#include "refs.h" +#include "refspec.h" +#include "proxy.h" + +int git_smart__recv(transport_smart *t) +{ + size_t bytes_read; + int ret; + + GIT_ASSERT_ARG(t); + GIT_ASSERT(t->current_stream); + + if (git_staticstr_remain(&t->buffer) == 0) { + git_error_set(GIT_ERROR_NET, "out of buffer space"); + return -1; + } + + ret = t->current_stream->read(t->current_stream, + git_staticstr_offset(&t->buffer), + git_staticstr_remain(&t->buffer), + &bytes_read); + + if (ret < 0) + return ret; + + GIT_ASSERT(bytes_read <= INT_MAX); + GIT_ASSERT(bytes_read <= git_staticstr_remain(&t->buffer)); + + git_staticstr_increase(&t->buffer, bytes_read); + + if (t->packetsize_cb && !t->cancelled.val) { + ret = t->packetsize_cb(bytes_read, t->packetsize_payload); + + if (ret) { + git_atomic32_set(&t->cancelled, 1); + return GIT_EUSER; + } + } + + return (int)bytes_read; +} + +GIT_INLINE(int) git_smart__reset_stream(transport_smart *t, bool close_subtransport) +{ + if (t->current_stream) { + t->current_stream->free(t->current_stream); + t->current_stream = NULL; + } + + if (close_subtransport) { + git__free(t->url); + t->url = NULL; + + if (t->wrapped->close(t->wrapped) < 0) + return -1; + + git__free(t->caps.object_format); + t->caps.object_format = NULL; + + git__free(t->caps.agent); + t->caps.agent = NULL; + } + + return 0; +} + +int git_smart__update_heads(transport_smart *t, git_vector *symrefs) +{ + size_t i; + git_pkt *pkt; + + git_vector_clear(&t->heads); + git_vector_foreach(&t->refs, i, pkt) { + git_pkt_ref *ref = (git_pkt_ref *) pkt; + if (pkt->type != GIT_PKT_REF) + continue; + + if (symrefs) { + git_refspec *spec; + git_str buf = GIT_STR_INIT; + size_t j; + int error = 0; + + git_vector_foreach(symrefs, j, spec) { + git_str_clear(&buf); + if (git_refspec_src_matches(spec, ref->head.name) && + !(error = git_refspec__transform(&buf, spec, ref->head.name))) { + git__free(ref->head.symref_target); + ref->head.symref_target = git_str_detach(&buf); + } + } + + git_str_dispose(&buf); + + if (error < 0) + return error; + } + + if (git_vector_insert(&t->heads, &ref->head) < 0) + return -1; + } + + return 0; +} + +static void free_symrefs(git_vector *symrefs) +{ + git_refspec *spec; + size_t i; + + git_vector_foreach(symrefs, i, spec) { + git_refspec__dispose(spec); + git__free(spec); + } + + git_vector_dispose(symrefs); +} + +static int git_smart__connect( + git_transport *transport, + const char *url, + int direction, + const git_remote_connect_options *connect_opts) +{ + transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent); + git_smart_subtransport_stream *stream; + int error; + git_pkt *pkt; + git_pkt_ref *first; + git_vector symrefs; + git_smart_service_t service; + + if (git_smart__reset_stream(t, true) < 0) + return -1; + + if (git_remote_connect_options_normalize(&t->connect_opts, t->owner->repo, connect_opts) < 0) + return -1; + + t->url = git__strdup(url); + GIT_ERROR_CHECK_ALLOC(t->url); + + t->direction = direction; + + if (GIT_DIRECTION_FETCH == t->direction) { + service = GIT_SERVICE_UPLOADPACK_LS; + } else if (GIT_DIRECTION_PUSH == t->direction) { + service = GIT_SERVICE_RECEIVEPACK_LS; + } else { + git_error_set(GIT_ERROR_NET, "invalid direction"); + return -1; + } + + if ((error = t->wrapped->action(&stream, t->wrapped, t->url, service)) < 0) + return error; + + /* Save off the current stream (i.e. socket) that we are working with */ + t->current_stream = stream; + + /* 2 flushes for RPC; 1 for stateful */ + if ((error = git_smart__store_refs(t, t->rpc ? 2 : 1)) < 0) + return error; + + /* Strip the comment packet for RPC */ + if (t->rpc) { + pkt = (git_pkt *)git_vector_get(&t->refs, 0); + + if (!pkt || GIT_PKT_COMMENT != pkt->type) { + git_error_set(GIT_ERROR_NET, "invalid response"); + return -1; + } else { + /* Remove the comment pkt from the list */ + git_vector_remove(&t->refs, 0); + git__free(pkt); + } + } + + /* We now have loaded the refs. */ + t->have_refs = 1; + + pkt = (git_pkt *)git_vector_get(&t->refs, 0); + if (pkt && GIT_PKT_REF != pkt->type) { + git_error_set(GIT_ERROR_NET, "invalid response"); + return -1; + } + first = (git_pkt_ref *)pkt; + + if ((error = git_vector_init(&symrefs, 1, NULL)) < 0) + return error; + + /* Detect capabilities */ + if ((error = git_smart__detect_caps(first, &t->caps, &symrefs)) == 0) { + /* If the only ref in the list is capabilities^{} with OID_ZERO, remove it */ + if (1 == t->refs.length && !strcmp(first->head.name, "capabilities^{}") && + git_oid_is_zero(&first->head.oid)) { + git_vector_clear(&t->refs); + git_pkt_free((git_pkt *)first); + } + + /* Keep a list of heads for _ls */ + git_smart__update_heads(t, &symrefs); + } else if (error == GIT_ENOTFOUND) { + /* There was no ref packet received, or the cap list was empty */ + error = 0; + } else { + git_error_set(GIT_ERROR_NET, "invalid response"); + goto cleanup; + } + + if (t->rpc && (error = git_smart__reset_stream(t, false)) < 0) + goto cleanup; + + /* We're now logically connected. */ + t->connected = 1; + +cleanup: + free_symrefs(&symrefs); + + return error; +} + +static int git_smart__set_connect_opts( + git_transport *transport, + const git_remote_connect_options *opts) +{ + transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent); + + if (!t->connected) { + git_error_set(GIT_ERROR_NET, "cannot reconfigure a transport that is not connected"); + return -1; + } + + return git_remote_connect_options_normalize(&t->connect_opts, t->owner->repo, opts); +} + +static int git_smart__capabilities(unsigned int *capabilities, git_transport *transport) +{ + transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent); + + *capabilities = 0; + + if (t->caps.push_options) + *capabilities |= GIT_REMOTE_CAPABILITY_PUSH_OPTIONS; + + if (t->caps.want_tip_sha1) + *capabilities |= GIT_REMOTE_CAPABILITY_TIP_OID; + + if (t->caps.want_reachable_sha1) + *capabilities |= GIT_REMOTE_CAPABILITY_REACHABLE_OID; + + return 0; +} + +#ifdef GIT_EXPERIMENTAL_SHA256 +static int git_smart__oid_type(git_oid_t *out, git_transport *transport) +{ + transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent); + + *out = 0; + + if (t->caps.object_format == NULL) { + *out = GIT_OID_DEFAULT; + } else { + *out = git_oid_type_fromstr(t->caps.object_format); + + if (!*out) { + git_error_set(GIT_ERROR_INVALID, + "unknown object format '%s'", + t->caps.object_format); + return -1; + } + } + + return 0; +} +#endif + +static int git_smart__ls(const git_remote_head ***out, size_t *size, git_transport *transport) +{ + transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent); + + if (!t->have_refs) { + git_error_set(GIT_ERROR_NET, "the transport has not yet loaded the refs"); + return -1; + } + + *out = (const git_remote_head **) t->heads.contents; + *size = t->heads.length; + + return 0; +} + +int git_smart__negotiation_step(git_transport *transport, void *data, size_t len) +{ + transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent); + git_smart_subtransport_stream *stream; + int error; + + if (t->rpc && git_smart__reset_stream(t, false) < 0) + return -1; + + if (GIT_DIRECTION_FETCH != t->direction) { + git_error_set(GIT_ERROR_NET, "this operation is only valid for fetch"); + return -1; + } + + if ((error = t->wrapped->action(&stream, t->wrapped, t->url, GIT_SERVICE_UPLOADPACK)) < 0) + return error; + + /* If this is a stateful implementation, the stream we get back should be the same */ + GIT_ASSERT(t->rpc || t->current_stream == stream); + + /* Save off the current stream (i.e. socket) that we are working with */ + t->current_stream = stream; + + if ((error = stream->write(stream, (const char *)data, len)) < 0) + return error; + + return 0; +} + +int git_smart__get_push_stream(transport_smart *t, git_smart_subtransport_stream **stream) +{ + int error; + + if (t->rpc && git_smart__reset_stream(t, false) < 0) + return -1; + + if (GIT_DIRECTION_PUSH != t->direction) { + git_error_set(GIT_ERROR_NET, "this operation is only valid for push"); + return -1; + } + + if ((error = t->wrapped->action(stream, t->wrapped, t->url, GIT_SERVICE_RECEIVEPACK)) < 0) + return error; + + /* If this is a stateful implementation, the stream we get back should be the same */ + GIT_ASSERT(t->rpc || t->current_stream == *stream); + + /* Save off the current stream (i.e. socket) that we are working with */ + t->current_stream = *stream; + + return 0; +} + +static void git_smart__cancel(git_transport *transport) +{ + transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent); + + git_atomic32_set(&t->cancelled, 1); +} + +static int git_smart__is_connected(git_transport *transport) +{ + transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent); + + return t->connected; +} + +static int git_smart__close(git_transport *transport) +{ + transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent); + git_vector *common = &t->common; + unsigned int i; + git_pkt *p; + git_smart_service_t service; + int ret; + git_smart_subtransport_stream *stream; + const char flush[] = "0000"; + + if (t->direction == GIT_DIRECTION_FETCH) { + service = GIT_SERVICE_UPLOADPACK; + } else if (t->direction == GIT_DIRECTION_PUSH) { + service = GIT_SERVICE_RECEIVEPACK; + } else { + git_error_set(GIT_ERROR_NET, "invalid direction"); + return -1; + } + + /* + * If we're still connected at this point and not using RPC, + * we should say goodbye by sending a flush, or git-daemon + * will complain that we disconnected unexpectedly. + */ + if (t->connected && !t->rpc && + !t->wrapped->action(&stream, t->wrapped, t->url, service)) { + t->current_stream->write(t->current_stream, flush, 4); + } + + ret = git_smart__reset_stream(t, true); + + git_vector_foreach(common, i, p) + git_pkt_free(p); + + git_vector_dispose(common); + + if (t->url) { + git__free(t->url); + t->url = NULL; + } + + t->connected = 0; + + return ret; +} + +static void git_smart__free(git_transport *transport) +{ + transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent); + git_vector *refs = &t->refs; + unsigned int i; + git_pkt *p; + + /* Make sure that the current stream is closed, if we have one. */ + git_smart__close(transport); + + /* Free the subtransport */ + t->wrapped->free(t->wrapped); + + git_vector_dispose(&t->heads); + git_vector_foreach(refs, i, p) + git_pkt_free(p); + + git_vector_dispose(refs); + + git_remote_connect_options_dispose(&t->connect_opts); + + git_array_dispose(t->shallow_roots); + + git__free(t->caps.object_format); + git__free(t->caps.agent); + git__free(t); +} + +static int ref_name_cmp(const void *a, const void *b) +{ + const git_pkt_ref *ref_a = a, *ref_b = b; + + return strcmp(ref_a->head.name, ref_b->head.name); +} + +int git_transport_smart_certificate_check(git_transport *transport, git_cert *cert, int valid, const char *hostname) +{ + transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent); + git_remote_connect_options *connect_opts = &t->connect_opts; + + GIT_ASSERT_ARG(transport); + GIT_ASSERT_ARG(cert); + GIT_ASSERT_ARG(hostname); + + if (!connect_opts->callbacks.certificate_check) + return GIT_PASSTHROUGH; + + return connect_opts->callbacks.certificate_check(cert, valid, hostname, connect_opts->callbacks.payload); +} + +int git_transport_smart_credentials(git_credential **out, git_transport *transport, const char *user, int methods) +{ + transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent); + git_remote_connect_options *connect_opts = &t->connect_opts; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(transport); + + if (!connect_opts->callbacks.credentials) + return GIT_PASSTHROUGH; + + return connect_opts->callbacks.credentials(out, t->url, user, methods, connect_opts->callbacks.payload); +} + +int git_transport_remote_connect_options( + git_remote_connect_options *out, + git_transport *transport) +{ + transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent); + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(transport); + + return git_remote_connect_options_dup(out, &t->connect_opts); +} + +int git_transport_smart(git_transport **out, git_remote *owner, void *param) +{ + transport_smart *t; + git_smart_subtransport_definition *definition = (git_smart_subtransport_definition *)param; + + if (!param) + return -1; + + t = git__calloc(1, sizeof(transport_smart)); + GIT_ERROR_CHECK_ALLOC(t); + + t->parent.version = GIT_TRANSPORT_VERSION; + t->parent.connect = git_smart__connect; + t->parent.set_connect_opts = git_smart__set_connect_opts; + t->parent.capabilities = git_smart__capabilities; +#ifdef GIT_EXPERIMENTAL_SHA256 + t->parent.oid_type = git_smart__oid_type; +#endif + t->parent.close = git_smart__close; + t->parent.free = git_smart__free; + t->parent.negotiate_fetch = git_smart__negotiate_fetch; + t->parent.shallow_roots = git_smart__shallow_roots; + t->parent.download_pack = git_smart__download_pack; + t->parent.push = git_smart__push; + t->parent.ls = git_smart__ls; + t->parent.is_connected = git_smart__is_connected; + t->parent.cancel = git_smart__cancel; + + t->owner = owner; + t->rpc = definition->rpc; + + if (git_vector_init(&t->refs, 16, ref_name_cmp) < 0 || + git_vector_init(&t->heads, 16, ref_name_cmp) < 0 || + definition->callback(&t->wrapped, &t->parent, definition->param) < 0) { + git_vector_dispose(&t->refs); + git_vector_dispose(&t->heads); + git__free(t); + return -1; + } + + git_staticstr_init(&t->buffer, GIT_SMART_BUFFER_SIZE); + + *out = (git_transport *) t; + return 0; +} diff --git a/src/libgit2/transports/smart.h b/src/libgit2/transports/smart.h new file mode 100644 index 00000000000..c987d93b53d --- /dev/null +++ b/src/libgit2/transports/smart.h @@ -0,0 +1,219 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_transports_smart_h__ +#define INCLUDE_transports_smart_h__ + +#include "common.h" + +#include "git2.h" +#include "vector.h" +#include "push.h" +#include "str.h" +#include "oidarray.h" +#include "staticstr.h" +#include "git2/sys/transport.h" + +#define GIT_SMART_BUFFER_SIZE 65536 + +#define GIT_SIDE_BAND_DATA 1 +#define GIT_SIDE_BAND_PROGRESS 2 +#define GIT_SIDE_BAND_ERROR 3 + +#define GIT_CAP_OFS_DELTA "ofs-delta" +#define GIT_CAP_MULTI_ACK "multi_ack" +#define GIT_CAP_MULTI_ACK_DETAILED "multi_ack_detailed" +#define GIT_CAP_SIDE_BAND "side-band" +#define GIT_CAP_SIDE_BAND_64K "side-band-64k" +#define GIT_CAP_INCLUDE_TAG "include-tag" +#define GIT_CAP_DELETE_REFS "delete-refs" +#define GIT_CAP_REPORT_STATUS "report-status" +#define GIT_CAP_THIN_PACK "thin-pack" +#define GIT_CAP_SYMREF "symref" +#define GIT_CAP_WANT_TIP_SHA1 "allow-tip-sha1-in-want" +#define GIT_CAP_WANT_REACHABLE_SHA1 "allow-reachable-sha1-in-want" +#define GIT_CAP_SHALLOW "shallow" +#define GIT_CAP_OBJECT_FORMAT "object-format=" +#define GIT_CAP_AGENT "agent=" +#define GIT_CAP_PUSH_OPTIONS "push-options" + +extern bool git_smart__ofs_delta_enabled; + +typedef enum { + GIT_PKT_CMD, + GIT_PKT_FLUSH, + GIT_PKT_REF, + GIT_PKT_HAVE, + GIT_PKT_ACK, + GIT_PKT_NAK, + GIT_PKT_COMMENT, + GIT_PKT_ERR, + GIT_PKT_DATA, + GIT_PKT_PROGRESS, + GIT_PKT_OK, + GIT_PKT_NG, + GIT_PKT_UNPACK, + GIT_PKT_SHALLOW, + GIT_PKT_UNSHALLOW +} git_pkt_type; + +/* Used for multi_ack and multi_ack_detailed */ +enum git_ack_status { + GIT_ACK_NONE, + GIT_ACK_CONTINUE, + GIT_ACK_COMMON, + GIT_ACK_READY +}; + +/* This would be a flush pkt */ +typedef struct { + git_pkt_type type; +} git_pkt; + +struct git_pkt_cmd { + git_pkt_type type; + char *cmd; + char *path; + char *host; +}; + +/* This is a pkt-line with some info in it */ +typedef struct { + git_pkt_type type; + git_remote_head head; + char *capabilities; +} git_pkt_ref; + +/* Useful later */ +typedef struct { + git_pkt_type type; + git_oid oid; + enum git_ack_status status; +} git_pkt_ack; + +typedef struct { + git_pkt_type type; + char comment[GIT_FLEX_ARRAY]; +} git_pkt_comment; + +typedef struct { + git_pkt_type type; + size_t len; + char data[GIT_FLEX_ARRAY]; +} git_pkt_data; + +typedef git_pkt_data git_pkt_progress; + +typedef struct { + git_pkt_type type; + size_t len; + char error[GIT_FLEX_ARRAY]; +} git_pkt_err; + +typedef struct { + git_pkt_type type; + char *ref; +} git_pkt_ok; + +typedef struct { + git_pkt_type type; + char *ref; + char *msg; +} git_pkt_ng; + +typedef struct { + git_pkt_type type; + int unpack_ok; +} git_pkt_unpack; + +typedef struct { + git_pkt_type type; + git_oid oid; +} git_pkt_shallow; + +typedef struct transport_smart_caps { + unsigned int common:1, + ofs_delta:1, + multi_ack:1, + multi_ack_detailed:1, + side_band:1, + side_band_64k:1, + include_tag:1, + delete_refs:1, + report_status:1, + thin_pack:1, + want_tip_sha1:1, + want_reachable_sha1:1, + shallow:1, + push_options:1; + char *object_format; + char *agent; +} transport_smart_caps; + +typedef int (*packetsize_cb)(size_t received, void *payload); + +typedef struct { + git_transport parent; + git_remote *owner; + char *url; + git_remote_connect_options connect_opts; + int direction; + git_smart_subtransport *wrapped; + git_smart_subtransport_stream *current_stream; + transport_smart_caps caps; + git_vector refs; + git_vector heads; + git_vector common; + git_array_oid_t shallow_roots; + git_atomic32 cancelled; + packetsize_cb packetsize_cb; + void *packetsize_payload; + unsigned rpc : 1, + have_refs : 1, + connected : 1; + git_staticstr_with_size(GIT_SMART_BUFFER_SIZE) buffer; +} transport_smart; + +/* smart_protocol.c */ +int git_smart__store_refs(transport_smart *t, int flushes); +int git_smart__detect_caps(git_pkt_ref *pkt, transport_smart_caps *caps, git_vector *symrefs); +int git_smart__push(git_transport *transport, git_push *push); + +int git_smart__negotiate_fetch( + git_transport *transport, + git_repository *repo, + const git_fetch_negotiation *wants); + +int git_smart__shallow_roots(git_oidarray *out, git_transport *transport); + +int git_smart__download_pack( + git_transport *transport, + git_repository *repo, + git_indexer_progress *stats); + +/* smart.c */ +int git_smart__recv(transport_smart *t); + +int git_smart__negotiation_step(git_transport *transport, void *data, size_t len); +int git_smart__get_push_stream(transport_smart *t, git_smart_subtransport_stream **out); + +int git_smart__update_heads(transport_smart *t, git_vector *symrefs); + +/* smart_pkt.c */ +typedef struct { + git_oid_t oid_type; + unsigned int seen_capabilities: 1; +} git_pkt_parse_data; + +int git_pkt_parse_line(git_pkt **head, const char **endptr, const char *line, size_t linelen, git_pkt_parse_data *data); +int git_pkt_buffer_flush(git_str *buf); +int git_pkt_send_flush(GIT_SOCKET s); +int git_pkt_buffer_done(git_str *buf); +int git_pkt_buffer_wants(const git_fetch_negotiation *wants, transport_smart_caps *caps, git_str *buf); +int git_pkt_buffer_have(git_oid *oid, git_str *buf); +void git_pkt_free(git_pkt *pkt); + +#endif diff --git a/src/libgit2/transports/smart_pkt.c b/src/libgit2/transports/smart_pkt.c new file mode 100644 index 00000000000..29ccb83ac72 --- /dev/null +++ b/src/libgit2/transports/smart_pkt.c @@ -0,0 +1,870 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "smart.h" +#include "util.h" +#include "posix.h" +#include "str.h" +#include "oid.h" + +#include "git2/types.h" +#include "git2/errors.h" +#include "git2/refs.h" +#include "git2/revwalk.h" + +#include + +#define PKT_DONE_STR "0009done\n" +#define PKT_FLUSH_STR "0000" +#define PKT_HAVE_PREFIX "have " +#define PKT_WANT_PREFIX "want " + +#define PKT_LEN_SIZE 4 +#define PKT_MAX_SIZE 0xffff +#define PKT_MAX_WANTLEN (PKT_LEN_SIZE + CONST_STRLEN(PKT_WANT_PREFIX) + GIT_OID_MAX_HEXSIZE + 1) + +static int flush_pkt(git_pkt **out) +{ + git_pkt *pkt; + + pkt = git__malloc(sizeof(git_pkt)); + GIT_ERROR_CHECK_ALLOC(pkt); + + pkt->type = GIT_PKT_FLUSH; + *out = pkt; + + return 0; +} + +/* the rest of the line will be useful for multi_ack and multi_ack_detailed */ +static int ack_pkt( + git_pkt **out, + const char *line, + size_t len, + git_pkt_parse_data *data) +{ + git_pkt_ack *pkt; + size_t oid_hexsize = git_oid_hexsize(data->oid_type); + + GIT_ASSERT(data && data->oid_type); + + pkt = git__calloc(1, sizeof(git_pkt_ack)); + GIT_ERROR_CHECK_ALLOC(pkt); + pkt->type = GIT_PKT_ACK; + + if (git__prefixncmp(line, len, "ACK ")) + goto out_err; + line += 4; + len -= 4; + + if (len < oid_hexsize || + git_oid_from_prefix(&pkt->oid, line, oid_hexsize, data->oid_type) < 0) + goto out_err; + line += oid_hexsize; + len -= oid_hexsize; + + if (len && line[0] == ' ') { + line++; + len--; + + if (!git__prefixncmp(line, len, "continue")) + pkt->status = GIT_ACK_CONTINUE; + else if (!git__prefixncmp(line, len, "common")) + pkt->status = GIT_ACK_COMMON; + else if (!git__prefixncmp(line, len, "ready")) + pkt->status = GIT_ACK_READY; + else + goto out_err; + } + + *out = (git_pkt *) pkt; + + return 0; + +out_err: + git_error_set(GIT_ERROR_NET, "error parsing ACK pkt-line"); + git__free(pkt); + return -1; +} + +static int nak_pkt(git_pkt **out) +{ + git_pkt *pkt; + + pkt = git__malloc(sizeof(git_pkt)); + GIT_ERROR_CHECK_ALLOC(pkt); + + pkt->type = GIT_PKT_NAK; + *out = pkt; + + return 0; +} + +static int comment_pkt(git_pkt **out, const char *line, size_t len) +{ + git_pkt_comment *pkt; + size_t alloclen; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_comment), len); + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); + pkt = git__malloc(alloclen); + GIT_ERROR_CHECK_ALLOC(pkt); + + pkt->type = GIT_PKT_COMMENT; + memcpy(pkt->comment, line, len); + pkt->comment[len] = '\0'; + + *out = (git_pkt *) pkt; + + return 0; +} + +static int err_pkt(git_pkt **out, const char *line, size_t len) +{ + git_pkt_err *pkt = NULL; + size_t alloclen; + + /* Remove "ERR " from the line */ + if (git__prefixncmp(line, len, "ERR ")) + goto out_err; + line += 4; + len -= 4; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len); + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); + pkt = git__malloc(alloclen); + GIT_ERROR_CHECK_ALLOC(pkt); + pkt->type = GIT_PKT_ERR; + pkt->len = len; + + memcpy(pkt->error, line, len); + pkt->error[len] = '\0'; + + *out = (git_pkt *) pkt; + + return 0; + +out_err: + git_error_set(GIT_ERROR_NET, "error parsing ERR pkt-line"); + git__free(pkt); + return -1; +} + +static int data_pkt(git_pkt **out, const char *line, size_t len) +{ + git_pkt_data *pkt; + size_t alloclen; + + line++; + len--; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len); + pkt = git__malloc(alloclen); + GIT_ERROR_CHECK_ALLOC(pkt); + + pkt->type = GIT_PKT_DATA; + pkt->len = len; + memcpy(pkt->data, line, len); + + *out = (git_pkt *) pkt; + + return 0; +} + +static int sideband_progress_pkt(git_pkt **out, const char *line, size_t len) +{ + git_pkt_progress *pkt; + size_t alloclen; + + line++; + len--; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len); + pkt = git__malloc(alloclen); + GIT_ERROR_CHECK_ALLOC(pkt); + + pkt->type = GIT_PKT_PROGRESS; + pkt->len = len; + memcpy(pkt->data, line, len); + + *out = (git_pkt *) pkt; + + return 0; +} + +static int sideband_error_pkt(git_pkt **out, const char *line, size_t len) +{ + git_pkt_err *pkt; + size_t alloc_len; + + line++; + len--; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, sizeof(git_pkt_err), len); + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1); + pkt = git__malloc(alloc_len); + GIT_ERROR_CHECK_ALLOC(pkt); + + pkt->type = GIT_PKT_ERR; + pkt->len = (int)len; + memcpy(pkt->error, line, len); + pkt->error[len] = '\0'; + + *out = (git_pkt *)pkt; + + return 0; +} + +static int set_data( + git_pkt_parse_data *data, + const char *line, + size_t len) +{ + const char *caps, *format_str = NULL, *eos; + size_t format_len; + git_oid_t remote_oid_type; + + GIT_ASSERT_ARG(data); + + if ((caps = memchr(line, '\0', len)) != NULL && + len > (size_t)((caps - line) + 1)) { + caps++; + + if (strncmp(caps, "object-format=", CONST_STRLEN("object-format=")) == 0) + format_str = caps + CONST_STRLEN("object-format="); + else if ((format_str = strstr(caps, " object-format=")) != NULL) + format_str += CONST_STRLEN(" object-format="); + } + + if (format_str) { + if ((eos = strchr(format_str, ' ')) == NULL) + eos = strchr(format_str, '\0'); + + GIT_ASSERT(eos); + + format_len = eos - format_str; + + if ((remote_oid_type = git_oid_type_fromstrn(format_str, format_len)) == 0) { + git_error_set(GIT_ERROR_INVALID, "unknown remote object format '%.*s'", (int)format_len, format_str); + return -1; + } + } else { + remote_oid_type = GIT_OID_SHA1; + } + + if (!data->oid_type) { + data->oid_type = remote_oid_type; + } else if (data->oid_type != remote_oid_type) { + git_error_set(GIT_ERROR_INVALID, + "the local object format '%s' does not match the remote object format '%s'", + git_oid_type_name(data->oid_type), + git_oid_type_name(remote_oid_type)); + return -1; + } + + return 0; +} + +/* + * Parse an other-ref line. + */ +static int ref_pkt( + git_pkt **out, + const char *line, + size_t len, + git_pkt_parse_data *data) +{ + git_pkt_ref *pkt; + size_t alloclen, oid_hexsize; + + pkt = git__calloc(1, sizeof(git_pkt_ref)); + GIT_ERROR_CHECK_ALLOC(pkt); + pkt->type = GIT_PKT_REF; + + /* Determine OID type from capabilities */ + if (!data->seen_capabilities && set_data(data, line, len) < 0) + return -1; + + GIT_ASSERT(data->oid_type); + oid_hexsize = git_oid_hexsize(data->oid_type); + + if (len < oid_hexsize || + git_oid_from_prefix(&pkt->head.oid, line, oid_hexsize, data->oid_type) < 0) + goto out_err; + line += oid_hexsize; + len -= oid_hexsize; + + if (git__prefixncmp(line, len, " ")) + goto out_err; + + line++; + len--; + + if (!len) + goto out_err; + + if (line[len - 1] == '\n') + --len; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1); + pkt->head.name = git__malloc(alloclen); + GIT_ERROR_CHECK_ALLOC(pkt->head.name); + + memcpy(pkt->head.name, line, len); + pkt->head.name[len] = '\0'; + + if (strlen(pkt->head.name) < len) { + if (!data->seen_capabilities) + pkt->capabilities = strchr(pkt->head.name, '\0') + 1; + else + goto out_err; + } + + data->seen_capabilities = 1; + + *out = (git_pkt *)pkt; + return 0; + +out_err: + git_error_set(GIT_ERROR_NET, "error parsing REF pkt-line"); + if (pkt) + git__free(pkt->head.name); + git__free(pkt); + return -1; +} + +static int ok_pkt(git_pkt **out, const char *line, size_t len) +{ + git_pkt_ok *pkt; + size_t alloc_len; + + pkt = git__malloc(sizeof(*pkt)); + GIT_ERROR_CHECK_ALLOC(pkt); + pkt->type = GIT_PKT_OK; + + if (git__prefixncmp(line, len, "ok ")) + goto out_err; + line += 3; + len -= 3; + + if (len && line[len - 1] == '\n') + --len; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, len, 1); + pkt->ref = git__malloc(alloc_len); + GIT_ERROR_CHECK_ALLOC(pkt->ref); + + memcpy(pkt->ref, line, len); + pkt->ref[len] = '\0'; + + *out = (git_pkt *)pkt; + return 0; + +out_err: + git_error_set(GIT_ERROR_NET, "error parsing OK pkt-line"); + git__free(pkt); + return -1; +} + +static int ng_pkt(git_pkt **out, const char *line, size_t len) +{ + git_pkt_ng *pkt; + const char *ptr, *eol; + size_t alloclen; + + pkt = git__malloc(sizeof(*pkt)); + GIT_ERROR_CHECK_ALLOC(pkt); + + pkt->ref = NULL; + pkt->type = GIT_PKT_NG; + + eol = line + len; + + if (git__prefixncmp(line, len, "ng ")) + goto out_err; + line += 3; + + if (!(ptr = memchr(line, ' ', eol - line))) + goto out_err; + len = ptr - line; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1); + pkt->ref = git__malloc(alloclen); + GIT_ERROR_CHECK_ALLOC(pkt->ref); + + memcpy(pkt->ref, line, len); + pkt->ref[len] = '\0'; + + line = ptr + 1; + if (line >= eol) + goto out_err; + + if (!(ptr = memchr(line, '\n', eol - line))) + goto out_err; + len = ptr - line; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1); + pkt->msg = git__malloc(alloclen); + GIT_ERROR_CHECK_ALLOC(pkt->msg); + + memcpy(pkt->msg, line, len); + pkt->msg[len] = '\0'; + + *out = (git_pkt *)pkt; + return 0; + +out_err: + git_error_set(GIT_ERROR_NET, "invalid packet line"); + git__free(pkt->ref); + git__free(pkt); + return -1; +} + +static int unpack_pkt(git_pkt **out, const char *line, size_t len) +{ + git_pkt_unpack *pkt; + + pkt = git__malloc(sizeof(*pkt)); + GIT_ERROR_CHECK_ALLOC(pkt); + pkt->type = GIT_PKT_UNPACK; + + if (!git__prefixncmp(line, len, "unpack ok")) + pkt->unpack_ok = 1; + else + pkt->unpack_ok = 0; + + *out = (git_pkt *)pkt; + return 0; +} + +static int shallow_pkt( + git_pkt **out, + const char *line, + size_t len, + git_pkt_parse_data *data) +{ + git_pkt_shallow *pkt; + size_t oid_hexsize = git_oid_hexsize(data->oid_type); + + GIT_ASSERT(data && data->oid_type); + + pkt = git__calloc(1, sizeof(git_pkt_shallow)); + GIT_ERROR_CHECK_ALLOC(pkt); + + pkt->type = GIT_PKT_SHALLOW; + + if (git__prefixncmp(line, len, "shallow ")) + goto out_err; + + line += 8; + len -= 8; + + if (len != oid_hexsize) + goto out_err; + + git_oid_from_prefix(&pkt->oid, line, oid_hexsize, data->oid_type); + line += oid_hexsize + 1; + len -= oid_hexsize + 1; + + *out = (git_pkt *)pkt; + + return 0; + +out_err: + git_error_set(GIT_ERROR_NET, "invalid packet line"); + git__free(pkt); + return -1; +} + +static int unshallow_pkt( + git_pkt **out, + const char *line, + size_t len, + git_pkt_parse_data *data) +{ + git_pkt_shallow *pkt; + size_t oid_hexsize = git_oid_hexsize(data->oid_type); + + GIT_ASSERT(data && data->oid_type); + + pkt = git__calloc(1, sizeof(git_pkt_shallow)); + GIT_ERROR_CHECK_ALLOC(pkt); + + pkt->type = GIT_PKT_UNSHALLOW; + + if (git__prefixncmp(line, len, "unshallow ")) + goto out_err; + + line += 10; + len -= 10; + + if (len != oid_hexsize) + goto out_err; + + git_oid_from_prefix(&pkt->oid, line, oid_hexsize, data->oid_type); + line += oid_hexsize + 1; + len -= oid_hexsize + 1; + + *out = (git_pkt *) pkt; + + return 0; + +out_err: + git_error_set(GIT_ERROR_NET, "invalid packet line"); + git__free(pkt); + return -1; +} + +static int parse_len(size_t *out, const char *line, size_t linelen) +{ + char num[PKT_LEN_SIZE + 1]; + int i, k, error; + int32_t len; + const char *num_end; + + /* Not even enough for the length */ + if (linelen < PKT_LEN_SIZE) + return GIT_EBUFS; + + memcpy(num, line, PKT_LEN_SIZE); + num[PKT_LEN_SIZE] = '\0'; + + for (i = 0; i < PKT_LEN_SIZE; ++i) { + if (!git__isxdigit(num[i])) { + /* Make sure there are no special characters before passing to error message */ + for (k = 0; k < PKT_LEN_SIZE; ++k) { + if(!git__isprint(num[k])) { + num[k] = '.'; + } + } + + git_error_set(GIT_ERROR_NET, "invalid hex digit in length: '%s'", num); + return -1; + } + } + + if ((error = git__strntol32(&len, num, PKT_LEN_SIZE, &num_end, 16)) < 0) + return error; + + if (len < 0) + return -1; + + *out = (size_t) len; + return 0; +} + +/* + * As per the documentation, the syntax is: + * + * pkt-line = data-pkt / flush-pkt + * data-pkt = pkt-len pkt-payload + * pkt-len = 4*(HEXDIG) + * pkt-payload = (pkt-len -4)*(OCTET) + * flush-pkt = "0000" + * + * Which means that the first four bytes are the length of the line, + * in ASCII hexadecimal (including itself) + */ + +int git_pkt_parse_line( + git_pkt **pkt, + const char **endptr, + const char *line, + size_t linelen, + git_pkt_parse_data *data) +{ + int error; + size_t len; + + if ((error = parse_len(&len, line, linelen)) < 0) { + /* + * If we fail to parse the length, it might be + * because the server is trying to send us the + * packfile already or because we do not yet have + * enough data. + */ + if (error == GIT_EBUFS) + ; + else if (!git__prefixncmp(line, linelen, "PACK")) + git_error_set(GIT_ERROR_NET, "unexpected pack file"); + else + git_error_set(GIT_ERROR_NET, "bad packet length"); + return error; + } + + /* + * Make sure there is enough in the buffer to satisfy + * this line. + */ + if (linelen < len) + return GIT_EBUFS; + + /* + * The length has to be exactly 0 in case of a flush + * packet or greater than PKT_LEN_SIZE, as the decoded + * length includes its own encoded length of four bytes. + */ + if (len != 0 && len < PKT_LEN_SIZE) + return GIT_ERROR; + + line += PKT_LEN_SIZE; + /* + * The Git protocol does not specify empty lines as part + * of the protocol. Not knowing what to do with an empty + * line, we should return an error upon hitting one. + */ + if (len == PKT_LEN_SIZE) { + git_error_set_str(GIT_ERROR_NET, "Invalid empty packet"); + return GIT_ERROR; + } + + if (len == 0) { /* Flush pkt */ + *endptr = line; + return flush_pkt(pkt); + } + + len -= PKT_LEN_SIZE; /* the encoded length includes its own size */ + + if (*line == GIT_SIDE_BAND_DATA) + error = data_pkt(pkt, line, len); + else if (*line == GIT_SIDE_BAND_PROGRESS) + error = sideband_progress_pkt(pkt, line, len); + else if (*line == GIT_SIDE_BAND_ERROR) + error = sideband_error_pkt(pkt, line, len); + else if (!git__prefixncmp(line, len, "ACK")) + error = ack_pkt(pkt, line, len, data); + else if (!git__prefixncmp(line, len, "NAK")) + error = nak_pkt(pkt); + else if (!git__prefixncmp(line, len, "ERR")) + error = err_pkt(pkt, line, len); + else if (*line == '#') + error = comment_pkt(pkt, line, len); + else if (!git__prefixncmp(line, len, "ok")) + error = ok_pkt(pkt, line, len); + else if (!git__prefixncmp(line, len, "ng")) + error = ng_pkt(pkt, line, len); + else if (!git__prefixncmp(line, len, "unpack")) + error = unpack_pkt(pkt, line, len); + else if (!git__prefixcmp(line, "shallow")) + error = shallow_pkt(pkt, line, len, data); + else if (!git__prefixcmp(line, "unshallow")) + error = unshallow_pkt(pkt, line, len, data); + else + error = ref_pkt(pkt, line, len, data); + + *endptr = line + len; + + return error; +} + +void git_pkt_free(git_pkt *pkt) +{ + if (pkt == NULL) { + return; + } + if (pkt->type == GIT_PKT_REF) { + git_pkt_ref *p = (git_pkt_ref *) pkt; + git__free(p->head.name); + git__free(p->head.symref_target); + } + + if (pkt->type == GIT_PKT_OK) { + git_pkt_ok *p = (git_pkt_ok *) pkt; + git__free(p->ref); + } + + if (pkt->type == GIT_PKT_NG) { + git_pkt_ng *p = (git_pkt_ng *) pkt; + git__free(p->ref); + git__free(p->msg); + } + + git__free(pkt); +} + +int git_pkt_buffer_flush(git_str *buf) +{ + return git_str_put(buf, PKT_FLUSH_STR, CONST_STRLEN(PKT_FLUSH_STR)); +} + +static int buffer_want_with_caps( + const git_remote_head *head, + transport_smart_caps *caps, + git_oid_t oid_type, + git_str *buf) +{ + git_str str = GIT_STR_INIT; + char oid[GIT_OID_MAX_HEXSIZE]; + size_t oid_hexsize, len; + + oid_hexsize = git_oid_hexsize(oid_type); + git_oid_fmt(oid, &head->oid); + + /* Prefer multi_ack_detailed */ + if (caps->multi_ack_detailed) + git_str_puts(&str, GIT_CAP_MULTI_ACK_DETAILED " "); + else if (caps->multi_ack) + git_str_puts(&str, GIT_CAP_MULTI_ACK " "); + + /* Prefer side-band-64k if the server supports both */ + if (caps->side_band_64k) + git_str_printf(&str, "%s ", GIT_CAP_SIDE_BAND_64K); + else if (caps->side_band) + git_str_printf(&str, "%s ", GIT_CAP_SIDE_BAND); + + if (caps->include_tag) + git_str_puts(&str, GIT_CAP_INCLUDE_TAG " "); + + if (caps->thin_pack) + git_str_puts(&str, GIT_CAP_THIN_PACK " "); + + if (caps->ofs_delta) + git_str_puts(&str, GIT_CAP_OFS_DELTA " "); + + if (caps->shallow) + git_str_puts(&str, GIT_CAP_SHALLOW " "); + + if (git_str_oom(&str)) + return -1; + + if (str.size > (PKT_MAX_SIZE - (PKT_MAX_WANTLEN + 1))) { + git_error_set(GIT_ERROR_NET, + "tried to produce packet with invalid caps length %" PRIuZ, str.size); + return -1; + } + + len = PKT_LEN_SIZE + CONST_STRLEN(PKT_WANT_PREFIX) + + oid_hexsize + 1 /* NUL */ + + git_str_len(&str) + 1 /* LF */; + + git_str_grow_by(buf, len); + git_str_printf(buf, + "%04x%s%.*s %s\n", (unsigned int)len, PKT_WANT_PREFIX, + (int)oid_hexsize, oid, git_str_cstr(&str)); + git_str_dispose(&str); + + GIT_ERROR_CHECK_ALLOC_STR(buf); + + return 0; +} + +/* + * All "want" packets have the same length and format, so what we do + * is overwrite the OID each time. + */ + +int git_pkt_buffer_wants( + const git_fetch_negotiation *wants, + transport_smart_caps *caps, + git_str *buf) +{ + const git_remote_head *head; + char oid[GIT_OID_MAX_HEXSIZE]; + git_oid_t oid_type; + size_t oid_hexsize, want_len, i = 0; + +#ifdef GIT_EXPERIMENTAL_SHA256 + oid_type = wants->refs_len > 0 ? wants->refs[0]->oid.type : GIT_OID_SHA1; +#else + oid_type = GIT_OID_SHA1; +#endif + + oid_hexsize = git_oid_hexsize(oid_type); + + want_len = PKT_LEN_SIZE + CONST_STRLEN(PKT_WANT_PREFIX) + + oid_hexsize + 1 /* LF */; + + if (caps->common) { + for (; i < wants->refs_len; ++i) { + head = wants->refs[i]; + if (!head->local) + break; + } + + if (buffer_want_with_caps(wants->refs[i], caps, oid_type, buf) < 0) + return -1; + + i++; + } + + for (; i < wants->refs_len; ++i) { + head = wants->refs[i]; + + if (head->local) + continue; + + git_oid_fmt(oid, &head->oid); + + git_str_printf(buf, "%04x%s%.*s\n", + (unsigned int)want_len, PKT_WANT_PREFIX, + (int)oid_hexsize, oid); + + if (git_str_oom(buf)) + return -1; + } + + /* Tell the server about our shallow objects */ + for (i = 0; i < wants->shallow_roots_len; i++) { + char oid[GIT_OID_MAX_HEXSIZE + 1]; + git_str shallow_buf = GIT_STR_INIT; + + git_oid_tostr(oid, GIT_OID_MAX_HEXSIZE + 1, &wants->shallow_roots[i]); + git_str_puts(&shallow_buf, "shallow "); + git_str_puts(&shallow_buf, oid); + git_str_putc(&shallow_buf, '\n'); + + git_str_printf(buf, "%04x%s", (unsigned int)git_str_len(&shallow_buf) + 4, git_str_cstr(&shallow_buf)); + + git_str_dispose(&shallow_buf); + + if (git_str_oom(buf)) + return -1; + } + + if (wants->depth > 0) { + git_str deepen_buf = GIT_STR_INIT; + + git_str_printf(&deepen_buf, "deepen %d\n", wants->depth); + git_str_printf(buf,"%04x%s", (unsigned int)git_str_len(&deepen_buf) + 4, git_str_cstr(&deepen_buf)); + + git_str_dispose(&deepen_buf); + + if (git_str_oom(buf)) + return -1; + } + + return git_pkt_buffer_flush(buf); +} + +int git_pkt_buffer_have(git_oid *oid, git_str *buf) +{ + char oid_str[GIT_OID_MAX_HEXSIZE]; + git_oid_t oid_type; + size_t oid_hexsize, have_len; + +#ifdef GIT_EXPERIMENTAL_SHA256 + oid_type = oid->type; +#else + oid_type = GIT_OID_SHA1; +#endif + + oid_hexsize = git_oid_hexsize(oid_type); + have_len = PKT_LEN_SIZE + CONST_STRLEN(PKT_HAVE_PREFIX) + + oid_hexsize + 1 /* LF */; + + git_oid_fmt(oid_str, oid); + return git_str_printf(buf, "%04x%s%.*s\n", + (unsigned int)have_len, PKT_HAVE_PREFIX, + (int)oid_hexsize, oid_str); +} + +int git_pkt_buffer_done(git_str *buf) +{ + return git_str_put(buf, PKT_DONE_STR, CONST_STRLEN(PKT_DONE_STR)); +} diff --git a/src/libgit2/transports/smart_protocol.c b/src/libgit2/transports/smart_protocol.c new file mode 100644 index 00000000000..87c1904589d --- /dev/null +++ b/src/libgit2/transports/smart_protocol.c @@ -0,0 +1,1276 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#include "git2.h" +#include "git2/odb_backend.h" + +#include "smart.h" +#include "refs.h" +#include "repository.h" +#include "push.h" +#include "pack-objects.h" +#include "remote.h" +#include "util.h" +#include "revwalk.h" + +#define NETWORK_XFER_THRESHOLD (100*1024) +/* The minimal interval between progress updates (in seconds). */ +#define MIN_PROGRESS_UPDATE_INTERVAL 0.5 + +bool git_smart__ofs_delta_enabled = true; + +int git_smart__store_refs(transport_smart *t, int flushes) +{ + git_vector *refs = &t->refs; + int error, flush = 0, recvd; + const char *line_end = NULL; + git_pkt *pkt = NULL; + git_pkt_parse_data pkt_parse_data = { 0 }; + size_t i; + + /* Clear existing refs in case git_remote_connect() is called again + * after git_remote_disconnect(). + */ + git_vector_foreach(refs, i, pkt) { + git_pkt_free(pkt); + } + git_vector_clear(refs); + pkt = NULL; + + do { + if (t->buffer.len > 0) + error = git_pkt_parse_line(&pkt, &line_end, + t->buffer.data, t->buffer.len, + &pkt_parse_data); + else + error = GIT_EBUFS; + + if (error < 0 && error != GIT_EBUFS) + return error; + + if (error == GIT_EBUFS) { + if ((recvd = git_smart__recv(t)) < 0) + return recvd; + + if (recvd == 0) { + git_error_set(GIT_ERROR_NET, "could not read refs from remote repository"); + return GIT_EEOF; + } + + continue; + } + + git_staticstr_consume(&t->buffer, line_end); + + if (pkt->type == GIT_PKT_ERR) { + git_error_set(GIT_ERROR_NET, "remote error: %s", ((git_pkt_err *)pkt)->error); + git__free(pkt); + return -1; + } + + if (pkt->type != GIT_PKT_FLUSH && git_vector_insert(refs, pkt) < 0) + return -1; + + if (pkt->type == GIT_PKT_FLUSH) { + flush++; + git_pkt_free(pkt); + } + } while (flush < flushes); + + return flush; +} + +static int append_symref(const char **out, git_vector *symrefs, const char *ptr) +{ + int error; + const char *end; + git_str buf = GIT_STR_INIT; + git_refspec *mapping = NULL; + + ptr += strlen(GIT_CAP_SYMREF); + if (*ptr != '=') + goto on_invalid; + + ptr++; + if (!(end = strchr(ptr, ' ')) && + !(end = strchr(ptr, '\0'))) + goto on_invalid; + + if ((error = git_str_put(&buf, ptr, end - ptr)) < 0) + return error; + + /* symref mapping has refspec format */ + mapping = git__calloc(1, sizeof(git_refspec)); + GIT_ERROR_CHECK_ALLOC(mapping); + + error = git_refspec__parse(mapping, git_str_cstr(&buf), true); + git_str_dispose(&buf); + + /* if the error isn't OOM, then it's a parse error; let's use a nicer message */ + if (error < 0) { + if (git_error_last()->klass != GIT_ERROR_NOMEMORY) + goto on_invalid; + + git__free(mapping); + return error; + } + + if ((error = git_vector_insert(symrefs, mapping)) < 0) + return error; + + *out = end; + return 0; + +on_invalid: + git_error_set(GIT_ERROR_NET, "remote sent invalid symref"); + git_refspec__dispose(mapping); + git__free(mapping); + return -1; +} + +int git_smart__detect_caps( + git_pkt_ref *pkt, + transport_smart_caps *caps, + git_vector *symrefs) +{ + const char *ptr, *start; + + /* No refs or capabilities, odd but not a problem */ + if (pkt == NULL || pkt->capabilities == NULL) + return GIT_ENOTFOUND; + + ptr = pkt->capabilities; + while (ptr != NULL && *ptr != '\0') { + if (*ptr == ' ') + ptr++; + + if (git_smart__ofs_delta_enabled && !git__prefixcmp(ptr, GIT_CAP_OFS_DELTA)) { + caps->common = caps->ofs_delta = 1; + ptr += strlen(GIT_CAP_OFS_DELTA); + continue; + } + + /* Keep multi_ack_detailed before multi_ack */ + if (!git__prefixcmp(ptr, GIT_CAP_MULTI_ACK_DETAILED)) { + caps->common = caps->multi_ack_detailed = 1; + ptr += strlen(GIT_CAP_MULTI_ACK_DETAILED); + continue; + } + + if (!git__prefixcmp(ptr, GIT_CAP_MULTI_ACK)) { + caps->common = caps->multi_ack = 1; + ptr += strlen(GIT_CAP_MULTI_ACK); + continue; + } + + if (!git__prefixcmp(ptr, GIT_CAP_INCLUDE_TAG)) { + caps->common = caps->include_tag = 1; + ptr += strlen(GIT_CAP_INCLUDE_TAG); + continue; + } + + /* Keep side-band check after side-band-64k */ + if (!git__prefixcmp(ptr, GIT_CAP_SIDE_BAND_64K)) { + caps->common = caps->side_band_64k = 1; + ptr += strlen(GIT_CAP_SIDE_BAND_64K); + continue; + } + + if (!git__prefixcmp(ptr, GIT_CAP_SIDE_BAND)) { + caps->common = caps->side_band = 1; + ptr += strlen(GIT_CAP_SIDE_BAND); + continue; + } + + if (!git__prefixcmp(ptr, GIT_CAP_DELETE_REFS)) { + caps->common = caps->delete_refs = 1; + ptr += strlen(GIT_CAP_DELETE_REFS); + continue; + } + + if (!git__prefixcmp(ptr, GIT_CAP_PUSH_OPTIONS)) { + caps->common = caps->push_options = 1; + ptr += strlen(GIT_CAP_PUSH_OPTIONS); + continue; + } + + if (!git__prefixcmp(ptr, GIT_CAP_THIN_PACK)) { + caps->common = caps->thin_pack = 1; + ptr += strlen(GIT_CAP_THIN_PACK); + continue; + } + + if (!git__prefixcmp(ptr, GIT_CAP_SYMREF)) { + int error; + + if ((error = append_symref(&ptr, symrefs, ptr)) < 0) + return error; + + continue; + } + + if (!git__prefixcmp(ptr, GIT_CAP_WANT_TIP_SHA1)) { + caps->common = caps->want_tip_sha1 = 1; + ptr += strlen(GIT_CAP_WANT_TIP_SHA1); + continue; + } + + if (!git__prefixcmp(ptr, GIT_CAP_WANT_REACHABLE_SHA1)) { + caps->common = caps->want_reachable_sha1 = 1; + ptr += strlen(GIT_CAP_WANT_REACHABLE_SHA1); + continue; + } + + if (!git__prefixcmp(ptr, GIT_CAP_OBJECT_FORMAT)) { + ptr += strlen(GIT_CAP_OBJECT_FORMAT); + + start = ptr; + ptr = strchr(ptr, ' '); + + if ((caps->object_format = git__strndup(start, (ptr - start))) == NULL) + return -1; + continue; + } + + if (!git__prefixcmp(ptr, GIT_CAP_AGENT)) { + ptr += strlen(GIT_CAP_AGENT); + + start = ptr; + ptr = strchr(ptr, ' '); + + if ((caps->agent = git__strndup(start, (ptr - start))) == NULL) + return -1; + continue; + } + + if (!git__prefixcmp(ptr, GIT_CAP_SHALLOW)) { + caps->common = caps->shallow = 1; + ptr += strlen(GIT_CAP_SHALLOW); + continue; + } + + /* We don't know this capability, so skip it */ + ptr = strchr(ptr, ' '); + } + + return 0; +} + +static int recv_pkt( + git_pkt **out_pkt, + git_pkt_type *out_type, + transport_smart *t) +{ + const char *ptr = t->buffer.data, *line_end = ptr; + git_pkt *pkt = NULL; + git_pkt_parse_data pkt_parse_data = { 0 }; + int error = 0, ret; + + pkt_parse_data.oid_type = t->owner->repo->oid_type; + pkt_parse_data.seen_capabilities = 1; + + do { + if (t->buffer.len > 0) + error = git_pkt_parse_line(&pkt, &line_end, ptr, + t->buffer.len, &pkt_parse_data); + else + error = GIT_EBUFS; + + if (error == 0) + break; /* return the pkt */ + + if (error < 0 && error != GIT_EBUFS) + return error; + + if ((ret = git_smart__recv(t)) < 0) { + return ret; + } else if (ret == 0) { + git_error_set(GIT_ERROR_NET, "could not read from remote repository"); + return GIT_EEOF; + } + } while (error); + + git_staticstr_consume(&t->buffer, line_end); + + if (out_type != NULL) + *out_type = pkt->type; + if (out_pkt != NULL) + *out_pkt = pkt; + else + git__free(pkt); + + return error; +} + +static int store_common(transport_smart *t) +{ + git_pkt *pkt = NULL; + int error; + + do { + if ((error = recv_pkt(&pkt, NULL, t)) < 0) + return error; + + if (t->rpc && (pkt->type == GIT_PKT_SHALLOW || + pkt->type == GIT_PKT_UNSHALLOW || + pkt->type == GIT_PKT_FLUSH)) { + git__free(pkt); + continue; + } + + if (pkt->type != GIT_PKT_ACK) { + git__free(pkt); + return 0; + } + + if (git_vector_insert(&t->common, pkt) < 0) { + git__free(pkt); + return -1; + } + } while (1); + + return 0; +} + +static int wait_while_ack(transport_smart *t) +{ + int error; + git_pkt *pkt = NULL; + git_pkt_ack *ack = NULL; + + while (1) { + git_pkt_free(pkt); + + if ((error = recv_pkt(&pkt, NULL, t)) < 0) + return error; + + if (pkt->type == GIT_PKT_NAK) + break; + if (pkt->type != GIT_PKT_ACK) + continue; + + ack = (git_pkt_ack*)pkt; + + if (ack->status != GIT_ACK_CONTINUE && + ack->status != GIT_ACK_COMMON && + ack->status != GIT_ACK_READY) { + break; + } + } + + git_pkt_free(pkt); + return 0; +} + +static int cap_not_sup_err(const char *cap_name) +{ + git_error_set(GIT_ERROR_NET, "server doesn't support %s", cap_name); + return GIT_EINVALID; +} + +/* Disables server capabilities we're not interested in */ +static int setup_caps( + transport_smart_caps *caps, + const git_fetch_negotiation *wants) +{ + if (wants->depth > 0) { + if (!caps->shallow) + return cap_not_sup_err(GIT_CAP_SHALLOW); + } else { + caps->shallow = 0; + } + + return 0; +} + +static int setup_shallow_roots( + git_array_oid_t *out, + const git_fetch_negotiation *wants) +{ + git_array_clear(*out); + + if (wants->shallow_roots_len > 0) { + git_array_init_to_size(*out, wants->shallow_roots_len); + GIT_ERROR_CHECK_ALLOC(out->ptr); + + memcpy(out->ptr, wants->shallow_roots, + sizeof(git_oid) * wants->shallow_roots_len); + out->size = wants->shallow_roots_len; + } + + return 0; +} + +int git_smart__negotiate_fetch( + git_transport *transport, + git_repository *repo, + const git_fetch_negotiation *wants) +{ + transport_smart *t = (transport_smart *)transport; + git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT; + git_str data = GIT_STR_INIT; + git_revwalk *walk = NULL; + int error = -1; + git_pkt_type pkt_type; + unsigned int i; + git_oid oid; + + if ((error = setup_caps(&t->caps, wants)) < 0 || + (error = setup_shallow_roots(&t->shallow_roots, wants)) < 0) + return error; + + if ((error = git_pkt_buffer_wants(wants, &t->caps, &data)) < 0) + return error; + + if ((error = git_revwalk_new(&walk, repo)) < 0) + goto on_error; + + opts.insert_by_date = 1; + if ((error = git_revwalk__push_glob(walk, "refs/*", &opts)) < 0) + goto on_error; + + if (wants->depth > 0) { + git_pkt_shallow *pkt; + + if ((error = git_smart__negotiation_step(&t->parent, data.ptr, data.size)) < 0) + goto on_error; + + if (!t->rpc) + git_str_clear(&data); + + while ((error = recv_pkt((git_pkt **)&pkt, NULL, t)) == 0) { + bool complete = false; + + if (pkt->type == GIT_PKT_SHALLOW) { + error = git_oidarray__add(&t->shallow_roots, &pkt->oid); + } else if (pkt->type == GIT_PKT_UNSHALLOW) { + git_oidarray__remove(&t->shallow_roots, &pkt->oid); + } else if (pkt->type == GIT_PKT_FLUSH) { + /* Server is done, stop processing shallow oids */ + complete = true; + } else { + git_error_set(GIT_ERROR_NET, "unexpected packet type"); + error = -1; + } + + git_pkt_free((git_pkt *) pkt); + + if (complete || error < 0) + break; + } + + if (error < 0) + goto on_error; + } + + /* + * Our support for ACK extensions is simply to parse them. On + * the first ACK we will accept that as enough common + * objects. We give up if we haven't found an answer in the + * first 256 we send. + */ + i = 0; + while (i < 256) { + error = git_revwalk_next(&oid, walk); + + if (error < 0) { + if (GIT_ITEROVER == error) + break; + + goto on_error; + } + + git_pkt_buffer_have(&oid, &data); + i++; + if (i % 20 == 0) { + if (t->cancelled.val) { + git_error_set(GIT_ERROR_NET, "The fetch was cancelled by the user"); + error = GIT_EUSER; + goto on_error; + } + + git_pkt_buffer_flush(&data); + if (git_str_oom(&data)) { + error = -1; + goto on_error; + } + + if ((error = git_smart__negotiation_step(&t->parent, data.ptr, data.size)) < 0) + goto on_error; + + git_str_clear(&data); + if (t->caps.multi_ack || t->caps.multi_ack_detailed) { + if ((error = store_common(t)) < 0) + goto on_error; + } else { + if ((error = recv_pkt(NULL, &pkt_type, t)) < 0) + goto on_error; + + if (pkt_type == GIT_PKT_ACK) { + break; + } else if (pkt_type == GIT_PKT_NAK) { + continue; + } else { + git_error_set(GIT_ERROR_NET, "unexpected pkt type"); + error = -1; + goto on_error; + } + } + } + + if (t->common.length > 0) + break; + + if (i % 20 == 0 && t->rpc) { + git_pkt_ack *pkt; + unsigned int j; + + if ((error = git_pkt_buffer_wants(wants, &t->caps, &data)) < 0) + goto on_error; + + git_vector_foreach(&t->common, j, pkt) { + if ((error = git_pkt_buffer_have(&pkt->oid, &data)) < 0) + goto on_error; + } + + if (git_str_oom(&data)) { + error = -1; + goto on_error; + } + } + } + + /* Tell the other end that we're done negotiating */ + if (t->rpc && t->common.length > 0) { + git_pkt_ack *pkt; + unsigned int j; + + if ((error = git_pkt_buffer_wants(wants, &t->caps, &data)) < 0) + goto on_error; + + git_vector_foreach(&t->common, j, pkt) { + if ((error = git_pkt_buffer_have(&pkt->oid, &data)) < 0) + goto on_error; + } + + if (git_str_oom(&data)) { + error = -1; + goto on_error; + } + } + + if ((error = git_pkt_buffer_done(&data)) < 0) + goto on_error; + + if (t->cancelled.val) { + git_error_set(GIT_ERROR_NET, "the fetch was cancelled"); + error = GIT_EUSER; + goto on_error; + } + + if ((error = git_smart__negotiation_step(&t->parent, data.ptr, data.size)) < 0) + goto on_error; + + git_str_dispose(&data); + git_revwalk_free(walk); + + /* Now let's eat up whatever the server gives us */ + if (!t->caps.multi_ack && !t->caps.multi_ack_detailed) { + if ((error = recv_pkt(NULL, &pkt_type, t)) < 0) + return error; + + if (pkt_type != GIT_PKT_ACK && pkt_type != GIT_PKT_NAK) { + git_error_set(GIT_ERROR_NET, "unexpected pkt type"); + return -1; + } + } else { + error = wait_while_ack(t); + } + + return error; + +on_error: + git_revwalk_free(walk); + git_str_dispose(&data); + return error; +} + +int git_smart__shallow_roots(git_oidarray *out, git_transport *transport) +{ + transport_smart *t = (transport_smart *)transport; + size_t len; + + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&len, t->shallow_roots.size, sizeof(git_oid)); + + out->count = t->shallow_roots.size; + + if (len) { + out->ids = git__malloc(len); + memcpy(out->ids, t->shallow_roots.ptr, len); + } else { + out->ids = NULL; + } + + return 0; +} + +static int no_sideband( + transport_smart *t, + struct git_odb_writepack *writepack, + git_indexer_progress *stats) +{ + int recvd; + + do { + if (t->cancelled.val) { + git_error_set(GIT_ERROR_NET, "the fetch was cancelled by the user"); + return GIT_EUSER; + } + + if (writepack->append(writepack, t->buffer.data, t->buffer.len, stats) < 0) + return -1; + + git_staticstr_clear(&t->buffer); + + if ((recvd = git_smart__recv(t)) < 0) + return recvd; + } while(recvd > 0); + + if (writepack->commit(writepack, stats) < 0) + return -1; + + return 0; +} + +struct network_packetsize_payload +{ + git_indexer_progress_cb callback; + void *payload; + git_indexer_progress *stats; + size_t last_fired_bytes; +}; + +static int network_packetsize(size_t received, void *payload) +{ + struct network_packetsize_payload *npp = (struct network_packetsize_payload*)payload; + + /* Accumulate bytes */ + npp->stats->received_bytes += received; + + /* Fire notification if the threshold is reached */ + if ((npp->stats->received_bytes - npp->last_fired_bytes) > NETWORK_XFER_THRESHOLD) { + npp->last_fired_bytes = npp->stats->received_bytes; + + if (npp->callback(npp->stats, npp->payload)) + return GIT_EUSER; + } + + return 0; +} + +int git_smart__download_pack( + git_transport *transport, + git_repository *repo, + git_indexer_progress *stats) +{ + transport_smart *t = (transport_smart *)transport; + git_odb *odb; + struct git_odb_writepack *writepack = NULL; + int error = 0; + struct network_packetsize_payload npp = {0}; + + git_indexer_progress_cb progress_cb = t->connect_opts.callbacks.transfer_progress; + void *progress_payload = t->connect_opts.callbacks.payload; + + memset(stats, 0, sizeof(git_indexer_progress)); + + if (progress_cb) { + npp.callback = progress_cb; + npp.payload = progress_payload; + npp.stats = stats; + t->packetsize_cb = &network_packetsize; + t->packetsize_payload = &npp; + + /* We might have something in the buffer already from negotiate_fetch */ + if (t->buffer.len > 0 && !t->cancelled.val) { + if (t->packetsize_cb(t->buffer.len, t->packetsize_payload)) + git_atomic32_set(&t->cancelled, 1); + } + } + + if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 || + ((error = git_odb_write_pack(&writepack, odb, progress_cb, progress_payload)) != 0)) + goto done; + + /* + * If the remote doesn't support the side-band, we can feed + * the data directly to the pack writer. Otherwise, we need to + * check which one belongs there. + */ + if (!t->caps.side_band && !t->caps.side_band_64k) { + error = no_sideband(t, writepack, stats); + goto done; + } + + do { + git_pkt *pkt = NULL; + + /* Check cancellation before network call */ + if (t->cancelled.val) { + git_error_clear(); + error = GIT_EUSER; + goto done; + } + + if ((error = recv_pkt(&pkt, NULL, t)) >= 0) { + /* Check cancellation after network call */ + if (t->cancelled.val) { + git_error_clear(); + error = GIT_EUSER; + } else if (pkt->type == GIT_PKT_PROGRESS) { + if (t->connect_opts.callbacks.sideband_progress) { + git_pkt_progress *p = (git_pkt_progress *) pkt; + + if (p->len > INT_MAX) { + git_error_set(GIT_ERROR_NET, "oversized progress message"); + error = GIT_ERROR; + goto done; + } + + error = t->connect_opts.callbacks.sideband_progress(p->data, (int)p->len, t->connect_opts.callbacks.payload); + } + } else if (pkt->type == GIT_PKT_DATA) { + git_pkt_data *p = (git_pkt_data *) pkt; + + if (p->len) + error = writepack->append(writepack, p->data, p->len, stats); + } else if (pkt->type == GIT_PKT_FLUSH) { + /* A flush indicates the end of the packfile */ + git__free(pkt); + break; + } + } + + git_pkt_free(pkt); + + if (error < 0) + goto done; + + } while (1); + + /* + * Trailing execution of progress_cb, if necessary... + * Only the callback through the npp datastructure currently + * updates the last_fired_bytes value. It is possible that + * progress has already been reported with the correct + * "received_bytes" value, but until (if?) this is unified + * then we will report progress again to be sure that the + * correct last received_bytes value is reported. + */ + if (npp.callback && npp.stats->received_bytes > npp.last_fired_bytes) { + error = npp.callback(npp.stats, npp.payload); + if (error != 0) + goto done; + } + + error = writepack->commit(writepack, stats); + +done: + if (writepack) + writepack->free(writepack); + if (progress_cb) { + t->packetsize_cb = NULL; + t->packetsize_payload = NULL; + } + + return error; +} + +static int gen_pktline(git_str *buf, git_push *push) +{ + push_spec *spec; + char *option; + size_t i, len; + char old_id[GIT_OID_MAX_HEXSIZE + 1], new_id[GIT_OID_MAX_HEXSIZE + 1]; + size_t old_id_len, new_id_len; + + git_vector_foreach(&push->specs, i, spec) { + len = strlen(spec->refspec.dst) + 7; + + if (i == 0) { + /* Need a leading \0 */ + ++len; + + if (push->report_status) + len += strlen(GIT_CAP_REPORT_STATUS) + 1; + + if (git_vector_length(&push->remote_push_options) > 0) + len += strlen(GIT_CAP_PUSH_OPTIONS) + 1; + + len += strlen(GIT_CAP_SIDE_BAND_64K) + 1; + } + + old_id_len = git_oid_hexsize(git_oid_type(&spec->roid)); + new_id_len = git_oid_hexsize(git_oid_type(&spec->loid)); + + len += (old_id_len + new_id_len); + + git_oid_fmt(old_id, &spec->roid); + old_id[old_id_len] = '\0'; + + git_oid_fmt(new_id, &spec->loid); + new_id[new_id_len] = '\0'; + + git_str_printf(buf, "%04"PRIxZ"%.*s %.*s %s", len, + (int)old_id_len, old_id, (int)new_id_len, new_id, + spec->refspec.dst); + + if (i == 0) { + git_str_putc(buf, '\0'); + + /* Core git always starts their capabilities string with a space */ + if (push->report_status) { + git_str_putc(buf, ' '); + git_str_printf(buf, GIT_CAP_REPORT_STATUS); + } + if (git_vector_length(&push->remote_push_options) > 0) { + git_str_putc(buf, ' '); + git_str_printf(buf, GIT_CAP_PUSH_OPTIONS); + } + git_str_putc(buf, ' '); + git_str_printf(buf, GIT_CAP_SIDE_BAND_64K); + } + + git_str_putc(buf, '\n'); + } + + if (git_vector_length(&push->remote_push_options) > 0) { + git_str_printf(buf, "0000"); + git_vector_foreach(&push->remote_push_options, i, option) { + git_str_printf(buf, "%04"PRIxZ"%s", strlen(option) + 4 , option); + } + } + + git_str_puts(buf, "0000"); + return git_str_oom(buf) ? -1 : 0; +} + +static int add_push_report_pkt(git_push *push, git_pkt *pkt) +{ + push_status *status; + + switch (pkt->type) { + case GIT_PKT_OK: + status = git__calloc(1, sizeof(push_status)); + GIT_ERROR_CHECK_ALLOC(status); + status->msg = NULL; + status->ref = git__strdup(((git_pkt_ok *)pkt)->ref); + if (!status->ref || + git_vector_insert(&push->status, status) < 0) { + git_push_status_free(status); + return -1; + } + break; + case GIT_PKT_NG: + status = git__calloc(1, sizeof(push_status)); + GIT_ERROR_CHECK_ALLOC(status); + status->ref = git__strdup(((git_pkt_ng *)pkt)->ref); + status->msg = git__strdup(((git_pkt_ng *)pkt)->msg); + if (!status->ref || !status->msg || + git_vector_insert(&push->status, status) < 0) { + git_push_status_free(status); + return -1; + } + break; + case GIT_PKT_UNPACK: + push->unpack_ok = ((git_pkt_unpack *)pkt)->unpack_ok; + break; + case GIT_PKT_FLUSH: + return GIT_ITEROVER; + default: + git_error_set(GIT_ERROR_NET, "report-status: protocol error"); + return -1; + } + + return 0; +} + +static int add_push_report_sideband_pkt(git_push *push, git_pkt_data *data_pkt, git_str *data_pkt_buf) +{ + git_pkt *pkt; + git_pkt_parse_data pkt_parse_data = { 0 }; + const char *line, *line_end = NULL; + size_t line_len; + int error; + int reading_from_buf = data_pkt_buf->size > 0; + + if (reading_from_buf) { + /* We had an existing partial packet, so add the new + * packet to the buffer and parse the whole thing */ + git_str_put(data_pkt_buf, data_pkt->data, data_pkt->len); + line = data_pkt_buf->ptr; + line_len = data_pkt_buf->size; + } + else { + line = data_pkt->data; + line_len = data_pkt->len; + } + + while (line_len > 0) { + error = git_pkt_parse_line(&pkt, &line_end, line, line_len, &pkt_parse_data); + + if (error == GIT_EBUFS) { + /* Buffer the data when the inner packet is split + * across multiple sideband packets */ + if (!reading_from_buf) + git_str_put(data_pkt_buf, line, line_len); + error = 0; + goto done; + } + else if (error < 0) + goto done; + + /* Advance in the buffer */ + line_len -= (line_end - line); + line = line_end; + + error = add_push_report_pkt(push, pkt); + + git_pkt_free(pkt); + + if (error < 0 && error != GIT_ITEROVER) + goto done; + } + + error = 0; + +done: + if (reading_from_buf) + git_str_consume(data_pkt_buf, line_end); + return error; +} + +static int parse_report(transport_smart *transport, git_push *push) +{ + git_pkt *pkt = NULL; + git_pkt_parse_data pkt_parse_data = { 0 }; + const char *line_end = NULL; + int error, recvd; + git_str data_pkt_buf = GIT_STR_INIT; + + for (;;) { + if (transport->buffer.len > 0) + error = git_pkt_parse_line(&pkt, &line_end, + transport->buffer.data, + transport->buffer.len, + &pkt_parse_data); + else + error = GIT_EBUFS; + + if (error < 0 && error != GIT_EBUFS) { + error = -1; + goto done; + } + + if (error == GIT_EBUFS) { + if ((recvd = git_smart__recv(transport)) < 0) { + error = recvd; + goto done; + } + + if (recvd == 0) { + git_error_set(GIT_ERROR_NET, "could not read report from remote repository"); + error = GIT_EEOF; + goto done; + } + continue; + } + + git_staticstr_consume(&transport->buffer, line_end); + error = 0; + + switch (pkt->type) { + case GIT_PKT_DATA: + /* This is a sideband packet which contains other packets */ + error = add_push_report_sideband_pkt(push, (git_pkt_data *)pkt, &data_pkt_buf); + break; + case GIT_PKT_ERR: + git_error_set(GIT_ERROR_NET, "report-status: Error reported: %s", + ((git_pkt_err *)pkt)->error); + error = -1; + break; + case GIT_PKT_PROGRESS: + if (transport->connect_opts.callbacks.sideband_progress) { + git_pkt_progress *p = (git_pkt_progress *) pkt; + + if (p->len > INT_MAX) { + git_error_set(GIT_ERROR_NET, "oversized progress message"); + error = GIT_ERROR; + goto done; + } + + error = transport->connect_opts.callbacks.sideband_progress(p->data, (int)p->len, transport->connect_opts.callbacks.payload); + } + break; + default: + error = add_push_report_pkt(push, pkt); + break; + } + + git_pkt_free(pkt); + + /* add_push_report_pkt returns GIT_ITEROVER when it receives a flush */ + if (error == GIT_ITEROVER) { + error = 0; + if (data_pkt_buf.size > 0) { + /* If there was data remaining in the pack data buffer, + * then the server sent a partial pkt-line */ + git_error_set(GIT_ERROR_NET, "incomplete pack data pkt-line"); + error = GIT_ERROR; + } + goto done; + } + + if (error < 0) { + goto done; + } + } +done: + git_str_dispose(&data_pkt_buf); + return error; +} + +static int add_ref_from_push_spec(git_vector *refs, push_spec *push_spec) +{ + git_pkt_ref *added = git__calloc(1, sizeof(git_pkt_ref)); + GIT_ERROR_CHECK_ALLOC(added); + + added->type = GIT_PKT_REF; + git_oid_cpy(&added->head.oid, &push_spec->loid); + added->head.name = git__strdup(push_spec->refspec.dst); + + if (!added->head.name || + git_vector_insert(refs, added) < 0) { + git_pkt_free((git_pkt *)added); + return -1; + } + + return 0; +} + +static int update_refs_from_report( + git_vector *refs, + git_vector *push_specs, + git_vector *push_report) +{ + git_pkt_ref *ref; + push_spec *push_spec; + push_status *push_status; + size_t i, j, refs_len; + int cmp; + + /* For each push spec we sent to the server, we should have + * gotten back a status packet in the push report */ + if (push_specs->length != push_report->length) { + git_error_set(GIT_ERROR_NET, "report-status: protocol error"); + return -1; + } + + /* We require that push_specs be sorted with push_spec_rref_cmp, + * and that push_report be sorted with push_status_ref_cmp */ + git_vector_sort(push_specs); + git_vector_sort(push_report); + + git_vector_foreach(push_specs, i, push_spec) { + push_status = git_vector_get(push_report, i); + + /* For each push spec we sent to the server, we should have + * gotten back a status packet in the push report which matches */ + if (strcmp(push_spec->refspec.dst, push_status->ref)) { + git_error_set(GIT_ERROR_NET, "report-status: protocol error"); + return -1; + } + } + + /* We require that refs be sorted with ref_name_cmp */ + git_vector_sort(refs); + i = j = 0; + refs_len = refs->length; + + /* Merge join push_specs with refs */ + while (i < push_specs->length && j < refs_len) { + push_spec = git_vector_get(push_specs, i); + push_status = git_vector_get(push_report, i); + ref = git_vector_get(refs, j); + + cmp = strcmp(push_spec->refspec.dst, ref->head.name); + + /* Iterate appropriately */ + if (cmp <= 0) i++; + if (cmp >= 0) j++; + + /* Add case */ + if (cmp < 0 && + !push_status->msg && + add_ref_from_push_spec(refs, push_spec) < 0) + return -1; + + /* Update case, delete case */ + if (cmp == 0 && + !push_status->msg) + git_oid_cpy(&ref->head.oid, &push_spec->loid); + } + + for (; i < push_specs->length; i++) { + push_spec = git_vector_get(push_specs, i); + push_status = git_vector_get(push_report, i); + + /* Add case */ + if (!push_status->msg && + add_ref_from_push_spec(refs, push_spec) < 0) + return -1; + } + + /* Remove any refs which we updated to have a zero OID. */ + git_vector_rforeach(refs, i, ref) { + if (git_oid_is_zero(&ref->head.oid)) { + git_vector_remove(refs, i); + git_pkt_free((git_pkt *)ref); + } + } + + git_vector_sort(refs); + + return 0; +} + +struct push_packbuilder_payload +{ + git_smart_subtransport_stream *stream; + git_packbuilder *pb; + git_push_transfer_progress_cb cb; + void *cb_payload; + size_t last_bytes; + uint64_t last_progress_report_time; +}; + +static int stream_thunk(void *buf, size_t size, void *data) +{ + int error = 0; + struct push_packbuilder_payload *payload = data; + + if ((error = payload->stream->write(payload->stream, (const char *)buf, size)) < 0) + return error; + + if (payload->cb) { + uint64_t current_time = git_time_monotonic(); + uint64_t elapsed = current_time - payload->last_progress_report_time; + payload->last_bytes += size; + + if (elapsed >= MIN_PROGRESS_UPDATE_INTERVAL) { + payload->last_progress_report_time = current_time; + error = payload->cb(payload->pb->nr_written, payload->pb->nr_objects, payload->last_bytes, payload->cb_payload); + } + } + + return error; +} + +int git_smart__push(git_transport *transport, git_push *push) +{ + transport_smart *t = (transport_smart *)transport; + git_remote_callbacks *cbs = &t->connect_opts.callbacks; + struct push_packbuilder_payload packbuilder_payload = {0}; + git_str pktline = GIT_STR_INIT; + int error = 0, need_pack = 0; + push_spec *spec; + unsigned int i; + + packbuilder_payload.pb = push->pb; + + if (cbs && cbs->push_transfer_progress) { + packbuilder_payload.cb = cbs->push_transfer_progress; + packbuilder_payload.cb_payload = cbs->payload; + } + +#ifdef PUSH_DEBUG +{ + git_remote_head *head; + char hex[GIT_OID_MAX_HEXSIZE+1], hex[GIT_OID_MAX_HEXSIZE] = '\0'; + + git_vector_foreach(&push->remote->refs, i, head) { + git_oid_fmt(hex, &head->oid); + fprintf(stderr, "%s (%s)\n", hex, head->name); + } + + git_vector_foreach(&push->specs, i, spec) { + git_oid_fmt(hex, &spec->roid); + fprintf(stderr, "%s (%s) -> ", hex, spec->lref); + git_oid_fmt(hex, &spec->loid); + fprintf(stderr, "%s (%s)\n", hex, spec->rref ? + spec->rref : spec->lref); + } +} +#endif + + /* + * Figure out if we need to send a packfile; which is in all + * cases except when we only send delete commands + */ + git_vector_foreach(&push->specs, i, spec) { + if (spec->refspec.src && spec->refspec.src[0] != '\0') { + need_pack = 1; + break; + } + } + + /* prepare pack before sending pack header to avoid timeouts */ + if (need_pack && ((error = git_packbuilder__prepare(push->pb))) < 0) + goto done; + + if ((error = git_smart__get_push_stream(t, &packbuilder_payload.stream)) < 0 || + (error = gen_pktline(&pktline, push)) < 0 || + (error = packbuilder_payload.stream->write(packbuilder_payload.stream, git_str_cstr(&pktline), git_str_len(&pktline))) < 0) + goto done; + + if (need_pack && + (error = git_packbuilder_foreach(push->pb, &stream_thunk, &packbuilder_payload)) < 0) + goto done; + + /* If we sent nothing or the server doesn't support report-status, then + * we consider the pack to have been unpacked successfully */ + if (!push->specs.length || !push->report_status) + push->unpack_ok = 1; + else if ((error = parse_report(t, push)) < 0) + goto done; + + /* If progress is being reported write the final report */ + if (cbs && cbs->push_transfer_progress) { + error = cbs->push_transfer_progress( + push->pb->nr_written, + push->pb->nr_objects, + packbuilder_payload.last_bytes, + cbs->payload); + + if (error < 0) + goto done; + } + + if (push->status.length) { + error = update_refs_from_report(&t->refs, &push->specs, &push->status); + if (error < 0) + goto done; + + error = git_smart__update_heads(t, NULL); + } + +done: + git_str_dispose(&pktline); + return error; +} diff --git a/src/libgit2/transports/ssh.c b/src/libgit2/transports/ssh.c new file mode 100644 index 00000000000..3f3a127f256 --- /dev/null +++ b/src/libgit2/transports/ssh.c @@ -0,0 +1,85 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "ssh_exec.h" +#include "ssh_libssh2.h" + +#include "transports/smart.h" + +int git_smart_subtransport_ssh( + git_smart_subtransport **out, + git_transport *owner, + void *param) +{ +#ifdef GIT_SSH_LIBSSH2 + return git_smart_subtransport_ssh_libssh2(out, owner, param); +#elif GIT_SSH_EXEC + return git_smart_subtransport_ssh_exec(out, owner, param); +#else + GIT_UNUSED(out); + GIT_UNUSED(owner); + GIT_UNUSED(param); + + git_error_set(GIT_ERROR_INVALID, "cannot create SSH transport; library was built without SSH support"); + return -1; +#endif +} + +static int transport_set_paths(git_transport *t, git_strarray *paths) +{ + transport_smart *smart = (transport_smart *)t; + +#ifdef GIT_SSH_LIBSSH2 + return git_smart_subtransport_ssh_libssh2_set_paths( + (git_smart_subtransport *)smart->wrapped, + paths->strings[0], + paths->strings[1]); +#elif GIT_SSH_EXEC + return git_smart_subtransport_ssh_exec_set_paths( + (git_smart_subtransport *)smart->wrapped, + paths->strings[0], + paths->strings[1]); +#else + GIT_UNUSED(t); + GIT_UNUSED(smart); + GIT_UNUSED(paths); + + GIT_ASSERT(!"cannot create SSH library; library was built without SSH support"); + return -1; +#endif +} + +int git_transport_ssh_with_paths( + git_transport **out, + git_remote *owner, + void *payload) +{ + git_strarray *paths = (git_strarray *) payload; + git_transport *transport; + int error; + + git_smart_subtransport_definition ssh_definition = { + git_smart_subtransport_ssh, + 0, /* no RPC */ + NULL + }; + + if (paths->count != 2) { + git_error_set(GIT_ERROR_SSH, "invalid ssh paths, must be two strings"); + return GIT_EINVALIDSPEC; + } + + if ((error = git_transport_smart(&transport, owner, &ssh_definition)) < 0) + return error; + + if ((error = transport_set_paths(transport, paths)) < 0) + return error; + + *out = transport; + return 0; +} + diff --git a/src/libgit2/transports/ssh_exec.c b/src/libgit2/transports/ssh_exec.c new file mode 100644 index 00000000000..91d46a38288 --- /dev/null +++ b/src/libgit2/transports/ssh_exec.c @@ -0,0 +1,386 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "ssh_exec.h" + +#ifdef GIT_SSH_EXEC + +#include "common.h" + +#include "config.h" +#include "net.h" +#include "path.h" +#include "futils.h" +#include "process.h" +#include "transports/smart.h" + +typedef struct { + git_smart_subtransport_stream parent; +} ssh_exec_subtransport_stream; + +typedef struct { + git_smart_subtransport parent; + git_transport *owner; + + ssh_exec_subtransport_stream *current_stream; + + char *cmd_uploadpack; + char *cmd_receivepack; + + git_smart_service_t action; + git_process *process; +} ssh_exec_subtransport; + +static int ssh_exec_subtransport_stream_read( + git_smart_subtransport_stream *s, + char *buffer, + size_t buf_size, + size_t *bytes_read) +{ + ssh_exec_subtransport *transport; + ssh_exec_subtransport_stream *stream = (ssh_exec_subtransport_stream *)s; + ssize_t ret; + + GIT_ASSERT_ARG(stream); + GIT_ASSERT(stream->parent.subtransport); + + transport = (ssh_exec_subtransport *)stream->parent.subtransport; + + if ((ret = git_process_read(transport->process, buffer, buf_size)) < 0) { + return (int)ret; + } + + *bytes_read = (size_t)ret; + return 0; +} + +static int ssh_exec_subtransport_stream_write( + git_smart_subtransport_stream *s, + const char *buffer, + size_t len) +{ + ssh_exec_subtransport *transport; + ssh_exec_subtransport_stream *stream = (ssh_exec_subtransport_stream *)s; + ssize_t ret; + + GIT_ASSERT(stream && stream->parent.subtransport); + + transport = (ssh_exec_subtransport *)stream->parent.subtransport; + + while (len > 0) { + if ((ret = git_process_write(transport->process, buffer, len)) < 0) + return (int)ret; + + len -= ret; + } + + return 0; +} + +static void ssh_exec_subtransport_stream_free(git_smart_subtransport_stream *s) +{ + ssh_exec_subtransport_stream *stream = (ssh_exec_subtransport_stream *)s; + + git__free(stream); +} + +static int ssh_exec_subtransport_stream_init( + ssh_exec_subtransport_stream **out, + ssh_exec_subtransport *transport) +{ + GIT_ASSERT_ARG(out); + + *out = git__calloc(sizeof(ssh_exec_subtransport_stream), 1); + GIT_ERROR_CHECK_ALLOC(*out); + + (*out)->parent.subtransport = &transport->parent; + (*out)->parent.read = ssh_exec_subtransport_stream_read; + (*out)->parent.write = ssh_exec_subtransport_stream_write; + (*out)->parent.free = ssh_exec_subtransport_stream_free; + + return 0; +} + +GIT_INLINE(int) ensure_transport_state( + ssh_exec_subtransport *transport, + git_smart_service_t expected, + git_smart_service_t next) +{ + if (transport->action != expected && transport->action != next) { + git_error_set(GIT_ERROR_NET, "invalid transport state"); + + return -1; + } + + return 0; +} + +static int get_ssh_cmdline( + git_vector *args, + bool *use_shell, + ssh_exec_subtransport *transport, + git_net_url *url, + const char *command) +{ + git_remote *remote = ((transport_smart *)transport->owner)->owner; + git_repository *repo = remote->repo; + git_config *cfg; + git_str ssh_cmd = GIT_STR_INIT, url_and_host = GIT_STR_INIT, + remote_cmd = GIT_STR_INIT; + const char *default_ssh_cmd = "ssh"; + int error; + + /* + * Safety check: like git, we forbid paths that look like an + * option as that could lead to injection to ssh that can make + * us do unexpected things + */ + if (git_process__is_cmdline_option(url->username)) { + git_error_set(GIT_ERROR_NET, "cannot ssh: username '%s' is ambiguous with command-line option", url->username); + return -1; + } else if (git_process__is_cmdline_option(url->host)) { + git_error_set(GIT_ERROR_NET, "cannot ssh: host '%s' is ambiguous with command-line option", url->host); + return -1; + } else if (git_process__is_cmdline_option(url->path)) { + git_error_set(GIT_ERROR_NET, "cannot ssh: path '%s' is ambiguous with command-line option", url->path); + return -1; + } + + if ((error = git_repository_config_snapshot(&cfg, repo)) < 0) + return error; + + if ((error = git__getenv(&ssh_cmd, "GIT_SSH_COMMAND")) == 0) + *use_shell = true; + else if (error != GIT_ENOTFOUND) + goto done; + else if ((error = git__getenv(&ssh_cmd, "GIT_SSH")) == 0) + *use_shell = false; + else if (error != GIT_ENOTFOUND) + goto done; + else if ((error = git_config__get_string_buf(&ssh_cmd, cfg, "core.sshcommand")) < 0 && error != GIT_ENOTFOUND) + goto done; + + git_error_clear(); + + if (!ssh_cmd.size && + git_str_puts(&ssh_cmd, default_ssh_cmd) < 0) + goto done; + + if ((error = git_vector_insert(args, git_str_detach(&ssh_cmd))) < 0) + goto done; + + if (url->port_specified) { + char *p = git__strdup("-p"); + char *port = git__strdup(url->port); + + if (!p || !port || + (error = git_vector_insert(args, p)) < 0 || + (error = git_vector_insert(args, port)) < 0) + goto done; + } + + if (url->username) { + if ((error = git_str_puts(&url_and_host, url->username)) < 0 || + (error = git_str_putc(&url_and_host, '@')) < 0) + goto done; + } + + if ((error = git_str_puts(&url_and_host, url->host)) < 0 || + (error = git_vector_insert(args, git_str_detach(&url_and_host))) < 0) + goto done; + + if ((error = git_str_puts(&remote_cmd, command)) < 0 || + (error = git_str_puts(&remote_cmd, " '")) < 0 || + (error = git_str_puts_escaped(&remote_cmd, url->path, "'!", "'\\", "'")) < 0 || + (error = git_str_puts(&remote_cmd, "'")) < 0 || + (error = git_vector_insert(args, git_str_detach(&remote_cmd))) < 0) + goto done; + +done: + git_str_dispose(&ssh_cmd); + git_str_dispose(&url_and_host); + git_str_dispose(&remote_cmd); + git_config_free(cfg); + return error; +} + +static int start_ssh( + ssh_exec_subtransport *transport, + git_smart_service_t action, + const char *sshpath) +{ + const char *env[] = { "GIT_DIR=" }; + + git_process_options process_opts = GIT_PROCESS_OPTIONS_INIT; + git_net_url url = GIT_NET_URL_INIT; + git_vector args = GIT_VECTOR_INIT; + bool use_shell = false; + const char *command; + int error; + + process_opts.capture_in = 1; + process_opts.capture_out = 1; + process_opts.capture_err = 0; + + switch (action) { + case GIT_SERVICE_UPLOADPACK_LS: + command = transport->cmd_uploadpack ? + transport->cmd_uploadpack : "git-upload-pack"; + break; + case GIT_SERVICE_RECEIVEPACK_LS: + command = transport->cmd_receivepack ? + transport->cmd_receivepack : "git-receive-pack"; + break; + default: + git_error_set(GIT_ERROR_NET, "invalid action"); + error = -1; + goto done; + } + + if (git_net_str_is_url(sshpath)) + error = git_net_url_parse(&url, sshpath); + else + error = git_net_url_parse_scp(&url, sshpath); + + if (error < 0) + goto done; + + if ((error = get_ssh_cmdline(&args, &use_shell, + transport, &url, command)) < 0) + goto done; + + process_opts.use_shell = use_shell; + + if ((error = git_process_new(&transport->process, + (const char **)args.contents, args.length, + env, ARRAY_SIZE(env), &process_opts)) < 0 || + (error = git_process_start(transport->process)) < 0) { + git_process_free(transport->process); + transport->process = NULL; + goto done; + } + +done: + git_vector_dispose_deep(&args); + git_net_url_dispose(&url); + return error; +} + +static int ssh_exec_subtransport_action( + git_smart_subtransport_stream **out, + git_smart_subtransport *t, + const char *sshpath, + git_smart_service_t action) +{ + ssh_exec_subtransport *transport = (ssh_exec_subtransport *)t; + ssh_exec_subtransport_stream *stream = NULL; + git_smart_service_t expected; + int error; + + switch (action) { + case GIT_SERVICE_UPLOADPACK_LS: + case GIT_SERVICE_RECEIVEPACK_LS: + if ((error = ensure_transport_state(transport, 0, 0)) < 0 || + (error = ssh_exec_subtransport_stream_init(&stream, transport)) < 0 || + (error = start_ssh(transport, action, sshpath)) < 0) + goto on_error; + + transport->current_stream = stream; + break; + + case GIT_SERVICE_UPLOADPACK: + case GIT_SERVICE_RECEIVEPACK: + expected = (action == GIT_SERVICE_UPLOADPACK) ? + GIT_SERVICE_UPLOADPACK_LS : GIT_SERVICE_RECEIVEPACK_LS; + + if ((error = ensure_transport_state(transport, expected, action)) < 0) + goto on_error; + + break; + + default: + git_error_set(GIT_ERROR_INVALID, "invalid service request"); + goto on_error; + } + + transport->action = action; + *out = &transport->current_stream->parent; + + return 0; + +on_error: + if (stream != NULL) + ssh_exec_subtransport_stream_free(&stream->parent); + + return -1; +} + +static int ssh_exec_subtransport_close(git_smart_subtransport *t) +{ + ssh_exec_subtransport *transport = (ssh_exec_subtransport *)t; + + if (transport->process) { + git_process_close(transport->process); + git_process_free(transport->process); + transport->process = NULL; + } + + transport->action = 0; + + return 0; +} + +static void ssh_exec_subtransport_free(git_smart_subtransport *t) +{ + ssh_exec_subtransport *transport = (ssh_exec_subtransport *)t; + + git__free(transport->cmd_uploadpack); + git__free(transport->cmd_receivepack); + git__free(transport); +} + +int git_smart_subtransport_ssh_exec( + git_smart_subtransport **out, + git_transport *owner, + void *payload) +{ + ssh_exec_subtransport *transport; + + GIT_UNUSED(payload); + + transport = git__calloc(sizeof(ssh_exec_subtransport), 1); + GIT_ERROR_CHECK_ALLOC(transport); + + transport->owner = owner; + transport->parent.action = ssh_exec_subtransport_action; + transport->parent.close = ssh_exec_subtransport_close; + transport->parent.free = ssh_exec_subtransport_free; + + *out = (git_smart_subtransport *) transport; + return 0; +} + +int git_smart_subtransport_ssh_exec_set_paths( + git_smart_subtransport *subtransport, + const char *cmd_uploadpack, + const char *cmd_receivepack) +{ + ssh_exec_subtransport *t = (ssh_exec_subtransport *)subtransport; + + git__free(t->cmd_uploadpack); + git__free(t->cmd_receivepack); + + t->cmd_uploadpack = git__strdup(cmd_uploadpack); + GIT_ERROR_CHECK_ALLOC(t->cmd_uploadpack); + + t->cmd_receivepack = git__strdup(cmd_receivepack); + GIT_ERROR_CHECK_ALLOC(t->cmd_receivepack); + + return 0; +} + +#endif diff --git a/src/libgit2/transports/ssh_exec.h b/src/libgit2/transports/ssh_exec.h new file mode 100644 index 00000000000..4bcba06b16b --- /dev/null +++ b/src/libgit2/transports/ssh_exec.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_transports_ssh_exec_h__ +#define INCLUDE_transports_ssh_exec_h__ + +#include "common.h" + +#include "git2.h" +#include "git2/transport.h" +#include "git2/sys/transport.h" + +int git_smart_subtransport_ssh_exec( + git_smart_subtransport **out, + git_transport *owner, + void *param); + +int git_smart_subtransport_ssh_exec_set_paths( + git_smart_subtransport *subtransport, + const char *cmd_uploadpack, + const char *cmd_receivepack); + +#endif diff --git a/src/libgit2/transports/ssh_libssh2.c b/src/libgit2/transports/ssh_libssh2.c new file mode 100644 index 00000000000..6469c8d6480 --- /dev/null +++ b/src/libgit2/transports/ssh_libssh2.c @@ -0,0 +1,1126 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "ssh_libssh2.h" + +#ifdef GIT_SSH_LIBSSH2 + +#include + +#include "runtime.h" +#include "net.h" +#include "smart.h" +#include "process.h" +#include "streams/socket.h" +#include "sysdir.h" + +#include "git2/credential.h" +#include "git2/sys/credential.h" + +#define OWNING_SUBTRANSPORT(s) ((ssh_subtransport *)(s)->parent.subtransport) + +extern int git_socket_stream__timeout; + +static const char cmd_uploadpack[] = "git-upload-pack"; +static const char cmd_receivepack[] = "git-receive-pack"; + +typedef struct { + git_smart_subtransport_stream parent; + git_stream *io; + LIBSSH2_SESSION *session; + LIBSSH2_CHANNEL *channel; + const char *cmd; + git_net_url url; + unsigned sent_command : 1; +} ssh_stream; + +typedef struct { + git_smart_subtransport parent; + transport_smart *owner; + ssh_stream *current_stream; + git_credential *cred; + char *cmd_uploadpack; + char *cmd_receivepack; +} ssh_subtransport; + +static int list_auth_methods(int *out, LIBSSH2_SESSION *session, const char *username); + +static void ssh_error(LIBSSH2_SESSION *session, const char *errmsg) +{ + char *ssherr; + libssh2_session_last_error(session, &ssherr, NULL, 0); + + git_error_set(GIT_ERROR_SSH, "%s: %s", errmsg, ssherr); +} + +/* + * Create a git protocol request. + * + * For example: git-upload-pack '/libgit2/libgit2' + */ +static int gen_proto(git_str *request, const char *cmd, git_net_url *url) +{ + const char *repo; + + repo = url->path; + + if (repo && repo[0] == '/' && repo[1] == '~') + repo++; + + if (!repo || !repo[0]) { + git_error_set(GIT_ERROR_NET, "malformed git protocol URL"); + return -1; + } + + git_str_puts(request, cmd); + git_str_puts(request, " '"); + git_str_puts(request, repo); + git_str_puts(request, "'"); + + if (git_str_oom(request)) + return -1; + + return 0; +} + +static int send_command(ssh_stream *s) +{ + int error; + git_str request = GIT_STR_INIT; + + error = gen_proto(&request, s->cmd, &s->url); + if (error < 0) + goto cleanup; + + error = libssh2_channel_exec(s->channel, request.ptr); + if (error < LIBSSH2_ERROR_NONE) { + ssh_error(s->session, "SSH could not execute request"); + goto cleanup; + } + + s->sent_command = 1; + +cleanup: + git_str_dispose(&request); + return error; +} + +static int ssh_stream_read( + git_smart_subtransport_stream *stream, + char *buffer, + size_t buf_size, + size_t *bytes_read) +{ + ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent); + ssize_t rc; + + *bytes_read = 0; + + if (!s->sent_command && send_command(s) < 0) + return -1; + + if ((rc = libssh2_channel_read(s->channel, buffer, buf_size)) < LIBSSH2_ERROR_NONE) { + ssh_error(s->session, "SSH could not read data"); + return -1; + } + + /* + * If we can't get anything out of stdout, it's typically a + * not-found error, so read from stderr and signal EOF on + * stderr. + */ + if (rc == 0) { + if ((rc = libssh2_channel_read_stderr(s->channel, buffer, buf_size)) > 0) { + git_error_set(GIT_ERROR_SSH, "%*s", (int)rc, buffer); + return GIT_EEOF; + } else if (rc < LIBSSH2_ERROR_NONE) { + ssh_error(s->session, "SSH could not read stderr"); + return -1; + } + } + + *bytes_read = rc; + + return 0; +} + +static int ssh_stream_write( + git_smart_subtransport_stream *stream, + const char *buffer, + size_t len) +{ + ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent); + size_t off = 0; + ssize_t ret = 0; + + if (!s->sent_command && send_command(s) < 0) + return -1; + + do { + ret = libssh2_channel_write(s->channel, buffer + off, len - off); + if (ret < 0) + break; + + off += ret; + + } while (off < len); + + if (ret < 0) { + ssh_error(s->session, "SSH could not write data"); + return -1; + } + + return 0; +} + +static void ssh_stream_free(git_smart_subtransport_stream *stream) +{ + ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent); + ssh_subtransport *t; + + if (!stream) + return; + + t = OWNING_SUBTRANSPORT(s); + t->current_stream = NULL; + + if (s->channel) { + libssh2_channel_close(s->channel); + libssh2_channel_free(s->channel); + s->channel = NULL; + } + + if (s->session) { + libssh2_session_disconnect(s->session, "closing transport"); + libssh2_session_free(s->session); + s->session = NULL; + } + + if (s->io) { + git_stream_close(s->io); + git_stream_free(s->io); + s->io = NULL; + } + + git_net_url_dispose(&s->url); + git__free(s); +} + +static int ssh_stream_alloc( + ssh_subtransport *t, + const char *cmd, + git_smart_subtransport_stream **stream) +{ + ssh_stream *s; + + GIT_ASSERT_ARG(stream); + + s = git__calloc(sizeof(ssh_stream), 1); + GIT_ERROR_CHECK_ALLOC(s); + + s->parent.subtransport = &t->parent; + s->parent.read = ssh_stream_read; + s->parent.write = ssh_stream_write; + s->parent.free = ssh_stream_free; + + s->cmd = cmd; + + *stream = &s->parent; + return 0; +} + +static int ssh_agent_auth(LIBSSH2_SESSION *session, git_credential_ssh_key *c) { + int rc = LIBSSH2_ERROR_NONE; + + struct libssh2_agent_publickey *curr, *prev = NULL; + + LIBSSH2_AGENT *agent = libssh2_agent_init(session); + + if (agent == NULL) + return -1; + + rc = libssh2_agent_connect(agent); + + if (rc != LIBSSH2_ERROR_NONE) { + rc = LIBSSH2_ERROR_AUTHENTICATION_FAILED; + goto shutdown; + } + + rc = libssh2_agent_list_identities(agent); + + if (rc != LIBSSH2_ERROR_NONE) + goto shutdown; + + while (1) { + rc = libssh2_agent_get_identity(agent, &curr, prev); + + if (rc < 0) + goto shutdown; + + /* rc is set to 1 whenever the ssh agent ran out of keys to check. + * Set the error code to authentication failure rather than erroring + * out with an untranslatable error code. + */ + if (rc == 1) { + rc = LIBSSH2_ERROR_AUTHENTICATION_FAILED; + goto shutdown; + } + + rc = libssh2_agent_userauth(agent, c->username, curr); + + if (rc == 0) + break; + + prev = curr; + } + +shutdown: + + if (rc != LIBSSH2_ERROR_NONE) + ssh_error(session, "error authenticating"); + + libssh2_agent_disconnect(agent); + libssh2_agent_free(agent); + + return rc; +} + +static int _git_ssh_authenticate_session( + LIBSSH2_SESSION *session, + git_credential *cred) +{ + int rc; + + do { + git_error_clear(); + switch (cred->credtype) { + case GIT_CREDENTIAL_USERPASS_PLAINTEXT: { + git_credential_userpass_plaintext *c = (git_credential_userpass_plaintext *)cred; + rc = libssh2_userauth_password(session, c->username, c->password); + break; + } + case GIT_CREDENTIAL_SSH_KEY: { + git_credential_ssh_key *c = (git_credential_ssh_key *)cred; + + if (c->privatekey) + rc = libssh2_userauth_publickey_fromfile( + session, c->username, c->publickey, + c->privatekey, c->passphrase); + else + rc = ssh_agent_auth(session, c); + + break; + } + case GIT_CREDENTIAL_SSH_CUSTOM: { + git_credential_ssh_custom *c = (git_credential_ssh_custom *)cred; + + rc = libssh2_userauth_publickey( + session, c->username, (const unsigned char *)c->publickey, + c->publickey_len, c->sign_callback, &c->payload); + break; + } + case GIT_CREDENTIAL_SSH_INTERACTIVE: { + void **abstract = libssh2_session_abstract(session); + git_credential_ssh_interactive *c = (git_credential_ssh_interactive *)cred; + + /* ideally, we should be able to set this by calling + * libssh2_session_init_ex() instead of libssh2_session_init(). + * libssh2's API is inconsistent here i.e. libssh2_userauth_publickey() + * allows you to pass the `abstract` as part of the call, whereas + * libssh2_userauth_keyboard_interactive() does not! + * + * The only way to set the `abstract` pointer is by calling + * libssh2_session_abstract(), which will replace the existing + * pointer as is done below. This is safe for now (at time of writing), + * but may not be valid in future. + */ + *abstract = c->payload; + + rc = libssh2_userauth_keyboard_interactive( + session, c->username, c->prompt_callback); + break; + } +#ifdef GIT_SSH_LIBSSH2_MEMORY_CREDENTIALS + case GIT_CREDENTIAL_SSH_MEMORY: { + git_credential_ssh_key *c = (git_credential_ssh_key *)cred; + + GIT_ASSERT(c->username); + GIT_ASSERT(c->privatekey); + + rc = libssh2_userauth_publickey_frommemory( + session, + c->username, + strlen(c->username), + c->publickey, + c->publickey ? strlen(c->publickey) : 0, + c->privatekey, + strlen(c->privatekey), + c->passphrase); + break; + } +#endif + default: + rc = LIBSSH2_ERROR_AUTHENTICATION_FAILED; + } + } while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc); + + if (rc == LIBSSH2_ERROR_PASSWORD_EXPIRED || + rc == LIBSSH2_ERROR_AUTHENTICATION_FAILED || + rc == LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED) + return GIT_EAUTH; + + if (rc != LIBSSH2_ERROR_NONE) { + if (git_error_last()->klass == GIT_ERROR_NONE) + ssh_error(session, "failed to authenticate SSH session"); + return -1; + } + + return 0; +} + +static int request_creds(git_credential **out, ssh_subtransport *t, const char *user, int auth_methods) +{ + int error, no_callback = 0; + git_credential *cred = NULL; + + if (!t->owner->connect_opts.callbacks.credentials) { + no_callback = 1; + } else { + error = t->owner->connect_opts.callbacks.credentials( + &cred, + t->owner->url, + user, + auth_methods, + t->owner->connect_opts.callbacks.payload); + + if (error == GIT_PASSTHROUGH) { + no_callback = 1; + } else if (error < 0) { + return error; + } else if (!cred) { + git_error_set(GIT_ERROR_SSH, "callback failed to initialize SSH credentials"); + return -1; + } + } + + if (no_callback) { + git_error_set(GIT_ERROR_SSH, "authentication required but no callback set"); + return GIT_EAUTH; + } + + if (!(cred->credtype & auth_methods)) { + cred->free(cred); + git_error_set(GIT_ERROR_SSH, "authentication callback returned unsupported credentials type"); + return GIT_EAUTH; + } + + *out = cred; + + return 0; +} + +#define SSH_DIR ".ssh" +#define KNOWN_HOSTS_FILE "known_hosts" + +/* + * Load the known_hosts file. + * + * Returns success but leaves the output NULL if we couldn't find the file. + */ +static int load_known_hosts(LIBSSH2_KNOWNHOSTS **hosts, LIBSSH2_SESSION *session) +{ + git_str path = GIT_STR_INIT, sshdir = GIT_STR_INIT; + LIBSSH2_KNOWNHOSTS *known_hosts = NULL; + int error; + + GIT_ASSERT_ARG(hosts); + + if ((error = git_sysdir_expand_homedir_file(&sshdir, SSH_DIR)) < 0 || + (error = git_str_joinpath(&path, git_str_cstr(&sshdir), KNOWN_HOSTS_FILE)) < 0) + goto out; + + if ((known_hosts = libssh2_knownhost_init(session)) == NULL) { + ssh_error(session, "error initializing known hosts"); + error = -1; + goto out; + } + + /* + * Try to read the file and consider not finding it as not trusting the + * host rather than an error. + */ + error = libssh2_knownhost_readfile(known_hosts, git_str_cstr(&path), LIBSSH2_KNOWNHOST_FILE_OPENSSH); + if (error == LIBSSH2_ERROR_FILE) + error = 0; + if (error < 0) + ssh_error(session, "error reading known_hosts"); + +out: + *hosts = known_hosts; + + git_str_dispose(&sshdir); + git_str_dispose(&path); + + return error; +} + +static void add_hostkey_pref_if_avail( + LIBSSH2_KNOWNHOSTS *known_hosts, + const char *hostname, + int port, + git_str *prefs, + int type, + const char *type_name) +{ + struct libssh2_knownhost *host = NULL; + const char key = '\0'; + int mask = LIBSSH2_KNOWNHOST_TYPE_PLAIN | LIBSSH2_KNOWNHOST_KEYENC_RAW | type; + int error; + + error = libssh2_knownhost_checkp(known_hosts, hostname, port, &key, 1, mask, &host); + if (error == LIBSSH2_KNOWNHOST_CHECK_MISMATCH) { + if (git_str_len(prefs) > 0) { + git_str_putc(prefs, ','); + } + git_str_puts(prefs, type_name); + } +} + +/* + * We figure out what kind of key we want to ask the remote for by trying to + * look it up with a nonsense key and using that mismatch to figure out what key + * we do have stored for the host. + * + * Populates prefs with the string to pass to libssh2_session_method_pref. + */ +static void find_hostkey_preference( + LIBSSH2_KNOWNHOSTS *known_hosts, + const char *hostname, + int port, + git_str *prefs) +{ + /* + * The order here is important as it indicates the priority of what will + * be preferred. + */ +#ifdef LIBSSH2_KNOWNHOST_KEY_ED25519 + add_hostkey_pref_if_avail(known_hosts, hostname, port, prefs, LIBSSH2_KNOWNHOST_KEY_ED25519, "ssh-ed25519"); +#endif +#ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_256 + add_hostkey_pref_if_avail(known_hosts, hostname, port, prefs, LIBSSH2_KNOWNHOST_KEY_ECDSA_256, "ecdsa-sha2-nistp256"); + add_hostkey_pref_if_avail(known_hosts, hostname, port, prefs, LIBSSH2_KNOWNHOST_KEY_ECDSA_384, "ecdsa-sha2-nistp384"); + add_hostkey_pref_if_avail(known_hosts, hostname, port, prefs, LIBSSH2_KNOWNHOST_KEY_ECDSA_521, "ecdsa-sha2-nistp521"); +#endif + add_hostkey_pref_if_avail(known_hosts, hostname, port, prefs, LIBSSH2_KNOWNHOST_KEY_SSHRSA, "rsa-sha2-512"); + add_hostkey_pref_if_avail(known_hosts, hostname, port, prefs, LIBSSH2_KNOWNHOST_KEY_SSHRSA, "rsa-sha2-256"); + add_hostkey_pref_if_avail(known_hosts, hostname, port, prefs, LIBSSH2_KNOWNHOST_KEY_SSHRSA, "ssh-rsa"); +} + +static int _git_ssh_session_create( + LIBSSH2_SESSION **session, + LIBSSH2_KNOWNHOSTS **hosts, + const char *hostname, + int port, + git_stream *io) +{ + git_socket_stream *socket = GIT_CONTAINER_OF(io, git_socket_stream, parent); + LIBSSH2_SESSION *s; + LIBSSH2_KNOWNHOSTS *known_hosts; + git_str prefs = GIT_STR_INIT; + int rc = 0; + + GIT_ASSERT_ARG(session); + GIT_ASSERT_ARG(hosts); + + s = libssh2_session_init(); + if (!s) { + git_error_set(GIT_ERROR_NET, "failed to initialize SSH session"); + return -1; + } + + if (git_socket_stream__timeout > 0) { + libssh2_session_set_timeout(s, git_socket_stream__timeout); + } + + if ((rc = load_known_hosts(&known_hosts, s)) < 0) { + ssh_error(s, "error loading known_hosts"); + libssh2_session_free(s); + return -1; + } + + find_hostkey_preference(known_hosts, hostname, port, &prefs); + if (git_str_len(&prefs) > 0) { + do { + rc = libssh2_session_method_pref(s, LIBSSH2_METHOD_HOSTKEY, git_str_cstr(&prefs)); + } while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc); + if (rc != LIBSSH2_ERROR_NONE) { + ssh_error(s, "failed to set hostkey preference"); + goto on_error; + } + } + git_str_dispose(&prefs); + + do { + rc = libssh2_session_handshake(s, socket->s); + } while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc); + + if (rc != LIBSSH2_ERROR_NONE) { + ssh_error(s, "failed to start SSH session"); + goto on_error; + } + + libssh2_session_set_blocking(s, 1); + + *session = s; + *hosts = known_hosts; + + return 0; + +on_error: + libssh2_knownhost_free(known_hosts); + libssh2_session_free(s); + return -1; +} + + +/* + * Returns the typemask argument to pass to libssh2_knownhost_check{,p} based on + * the type of key that libssh2_session_hostkey returns. + */ +static int fingerprint_type_mask(int keytype) +{ + int mask = LIBSSH2_KNOWNHOST_TYPE_PLAIN | LIBSSH2_KNOWNHOST_KEYENC_RAW; + return mask; + + switch (keytype) { + case LIBSSH2_HOSTKEY_TYPE_RSA: + mask |= LIBSSH2_KNOWNHOST_KEY_SSHRSA; + break; + case LIBSSH2_HOSTKEY_TYPE_DSS: + mask |= LIBSSH2_KNOWNHOST_KEY_SSHDSS; + break; +#ifdef LIBSSH2_HOSTKEY_TYPE_ECDSA_256 + case LIBSSH2_HOSTKEY_TYPE_ECDSA_256: + mask |= LIBSSH2_KNOWNHOST_KEY_ECDSA_256; + break; + case LIBSSH2_HOSTKEY_TYPE_ECDSA_384: + mask |= LIBSSH2_KNOWNHOST_KEY_ECDSA_384; + break; + case LIBSSH2_HOSTKEY_TYPE_ECDSA_521: + mask |= LIBSSH2_KNOWNHOST_KEY_ECDSA_521; + break; +#endif +#ifdef LIBSSH2_HOSTKEY_TYPE_ED25519 + case LIBSSH2_HOSTKEY_TYPE_ED25519: + mask |= LIBSSH2_KNOWNHOST_KEY_ED25519; + break; +#endif + } + + return mask; +} + +/* + * Check the host against the user's known_hosts file. + * + * Returns 1/0 for valid/''not-valid or <0 for an error + */ +static int check_against_known_hosts( + LIBSSH2_SESSION *session, + LIBSSH2_KNOWNHOSTS *known_hosts, + const char *hostname, + int port, + const char *key, + size_t key_len, + int key_type) +{ + int check, typemask, ret = 0; + struct libssh2_knownhost *host = NULL; + + if (known_hosts == NULL) + return 0; + + typemask = fingerprint_type_mask(key_type); + check = libssh2_knownhost_checkp(known_hosts, hostname, port, key, key_len, typemask, &host); + if (check == LIBSSH2_KNOWNHOST_CHECK_FAILURE) { + ssh_error(session, "error checking for known host"); + return -1; + } + + ret = check == LIBSSH2_KNOWNHOST_CHECK_MATCH ? 1 : 0; + + return ret; +} + +/* + * Perform the check for the session's certificate against known hosts if + * possible and then ask the user if they have a callback. + * + * Returns 1/0 for valid/not-valid or <0 for an error + */ +static int check_certificate( + LIBSSH2_SESSION *session, + LIBSSH2_KNOWNHOSTS *known_hosts, + git_transport_certificate_check_cb check_cb, + void *check_cb_payload, + const char *host, + int port) +{ + git_cert_hostkey cert = {{ 0 }}; + const char *key; + size_t cert_len; + int cert_type, cert_valid = 0, error = GIT_ECERTIFICATE; + + if ((key = libssh2_session_hostkey(session, &cert_len, &cert_type)) == NULL) { + ssh_error(session, "failed to retrieve hostkey"); + return -1; + } + + if ((cert_valid = check_against_known_hosts(session, known_hosts, host, port, key, cert_len, cert_type)) < 0) + return -1; + + cert.parent.cert_type = GIT_CERT_HOSTKEY_LIBSSH2; + if (key != NULL) { + cert.type |= GIT_CERT_SSH_RAW; + cert.hostkey = key; + cert.hostkey_len = cert_len; + switch (cert_type) { + case LIBSSH2_HOSTKEY_TYPE_RSA: + cert.raw_type = GIT_CERT_SSH_RAW_TYPE_RSA; + break; + case LIBSSH2_HOSTKEY_TYPE_DSS: + cert.raw_type = GIT_CERT_SSH_RAW_TYPE_DSS; + break; + +#ifdef LIBSSH2_HOSTKEY_TYPE_ECDSA_256 + case LIBSSH2_HOSTKEY_TYPE_ECDSA_256: + cert.raw_type = GIT_CERT_SSH_RAW_TYPE_KEY_ECDSA_256; + break; + case LIBSSH2_HOSTKEY_TYPE_ECDSA_384: + cert.raw_type = GIT_CERT_SSH_RAW_TYPE_KEY_ECDSA_384; + break; + case LIBSSH2_KNOWNHOST_KEY_ECDSA_521: + cert.raw_type = GIT_CERT_SSH_RAW_TYPE_KEY_ECDSA_521; + break; +#endif + +#ifdef LIBSSH2_HOSTKEY_TYPE_ED25519 + case LIBSSH2_HOSTKEY_TYPE_ED25519: + cert.raw_type = GIT_CERT_SSH_RAW_TYPE_KEY_ED25519; + break; +#endif + default: + cert.raw_type = GIT_CERT_SSH_RAW_TYPE_UNKNOWN; + } + } + +#ifdef LIBSSH2_HOSTKEY_HASH_SHA256 + key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA256); + if (key != NULL) { + cert.type |= GIT_CERT_SSH_SHA256; + memcpy(&cert.hash_sha256, key, 32); + } +#endif + + key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + if (key != NULL) { + cert.type |= GIT_CERT_SSH_SHA1; + memcpy(&cert.hash_sha1, key, 20); + } + + key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); + if (key != NULL) { + cert.type |= GIT_CERT_SSH_MD5; + memcpy(&cert.hash_md5, key, 16); + } + + if (cert.type == 0) { + git_error_set(GIT_ERROR_SSH, "unable to get the host key"); + return -1; + } + + if (check_cb != NULL) { + git_cert_hostkey *cert_ptr = &cert; + + error = check_cb((git_cert *)cert_ptr, cert_valid, host, + check_cb_payload); + + if (error == 0) + cert_valid = 1; + else if (error != GIT_PASSTHROUGH) + cert_valid = 0; + } + + if (!cert_valid) { + git_error_set(GIT_ERROR_SSH, "invalid or unknown remote ssh hostkey"); + return (error == GIT_PASSTHROUGH) ? GIT_ECERTIFICATE : error; + } + + return 0; +} + +#define SSH_DEFAULT_PORT "22" + +static int _git_ssh_setup_conn( + ssh_subtransport *t, + const char *url, + const char *cmd, + git_smart_subtransport_stream **stream) +{ + int auth_methods, error = 0, port; + ssh_stream *s; + git_credential *cred = NULL; + LIBSSH2_SESSION *session=NULL; + LIBSSH2_CHANNEL *channel=NULL; + LIBSSH2_KNOWNHOSTS *known_hosts = NULL; + + t->current_stream = NULL; + + *stream = NULL; + if (ssh_stream_alloc(t, cmd, stream) < 0) + return -1; + + s = (ssh_stream *)*stream; + s->session = NULL; + s->channel = NULL; + + if (git_net_str_is_url(url)) + error = git_net_url_parse(&s->url, url); + else + error = git_net_url_parse_scp(&s->url, url); + + if (error < 0) + goto done; + + /* Safety check: like git, we forbid paths that look like an option as + * that could lead to injection on the remote side */ + if (git_process__is_cmdline_option(s->url.path)) { + git_error_set(GIT_ERROR_NET, "cannot ssh: path '%s' is ambiguous with command-line option", s->url.path); + error = -1; + goto done; + } + + + if ((error = git_socket_stream_new(&s->io, s->url.host, s->url.port)) < 0 || + (error = git_stream_connect(s->io)) < 0) + goto done; + + /* + * Try to parse the port as a number, if we can't then fall back to + * default. It would be nice if we could get the port that was resolved + * as part of the stream connection, but that's not something that's + * exposed. + */ + if (git__strntol32(&port, s->url.port, strlen(s->url.port), NULL, 10) < 0) + port = -1; + + if ((error = _git_ssh_session_create(&session, &known_hosts, s->url.host, port, s->io)) < 0) + goto done; + + if ((error = check_certificate(session, known_hosts, t->owner->connect_opts.callbacks.certificate_check, t->owner->connect_opts.callbacks.payload, s->url.host, port)) < 0) + goto done; + + /* we need the username to ask for auth methods */ + if (!s->url.username) { + if ((error = request_creds(&cred, t, NULL, GIT_CREDENTIAL_USERNAME)) < 0) + goto done; + + s->url.username = git__strdup(((git_credential_username *) cred)->username); + cred->free(cred); + cred = NULL; + if (!s->url.username) + goto done; + } else if (s->url.username && s->url.password) { + if ((error = git_credential_userpass_plaintext_new(&cred, s->url.username, s->url.password)) < 0) + goto done; + } + + if ((error = list_auth_methods(&auth_methods, session, s->url.username)) < 0) + goto done; + + error = GIT_EAUTH; + /* if we already have something to try */ + if (cred && auth_methods & cred->credtype) + error = _git_ssh_authenticate_session(session, cred); + + while (error == GIT_EAUTH) { + if (cred) { + cred->free(cred); + cred = NULL; + } + + if ((error = request_creds(&cred, t, s->url.username, auth_methods)) < 0) + goto done; + + if (strcmp(s->url.username, git_credential_get_username(cred))) { + git_error_set(GIT_ERROR_SSH, "username does not match previous request"); + error = -1; + goto done; + } + + error = _git_ssh_authenticate_session(session, cred); + + if (error == GIT_EAUTH) { + /* refresh auth methods */ + if ((error = list_auth_methods(&auth_methods, session, s->url.username)) < 0) + goto done; + else + error = GIT_EAUTH; + } + } + + if (error < 0) + goto done; + + channel = libssh2_channel_open_session(session); + if (!channel) { + error = -1; + ssh_error(session, "Failed to open SSH channel"); + goto done; + } + + libssh2_channel_set_blocking(channel, 1); + + s->session = session; + s->channel = channel; + + t->current_stream = s; + +done: + if (known_hosts) + libssh2_knownhost_free(known_hosts); + + if (error < 0) { + ssh_stream_free(*stream); + + if (session) + libssh2_session_free(session); + } + + if (cred) + cred->free(cred); + + return error; +} + +static int ssh_uploadpack_ls( + ssh_subtransport *t, + const char *url, + git_smart_subtransport_stream **stream) +{ + const char *cmd = t->cmd_uploadpack ? t->cmd_uploadpack : cmd_uploadpack; + + return _git_ssh_setup_conn(t, url, cmd, stream); +} + +static int ssh_uploadpack( + ssh_subtransport *t, + const char *url, + git_smart_subtransport_stream **stream) +{ + GIT_UNUSED(url); + + if (t->current_stream) { + *stream = &t->current_stream->parent; + return 0; + } + + git_error_set(GIT_ERROR_NET, "must call UPLOADPACK_LS before UPLOADPACK"); + return -1; +} + +static int ssh_receivepack_ls( + ssh_subtransport *t, + const char *url, + git_smart_subtransport_stream **stream) +{ + const char *cmd = t->cmd_receivepack ? t->cmd_receivepack : cmd_receivepack; + + + return _git_ssh_setup_conn(t, url, cmd, stream); +} + +static int ssh_receivepack( + ssh_subtransport *t, + const char *url, + git_smart_subtransport_stream **stream) +{ + GIT_UNUSED(url); + + if (t->current_stream) { + *stream = &t->current_stream->parent; + return 0; + } + + git_error_set(GIT_ERROR_NET, "must call RECEIVEPACK_LS before RECEIVEPACK"); + return -1; +} + +static int _ssh_action( + git_smart_subtransport_stream **stream, + git_smart_subtransport *subtransport, + const char *url, + git_smart_service_t action) +{ + ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent); + + switch (action) { + case GIT_SERVICE_UPLOADPACK_LS: + return ssh_uploadpack_ls(t, url, stream); + + case GIT_SERVICE_UPLOADPACK: + return ssh_uploadpack(t, url, stream); + + case GIT_SERVICE_RECEIVEPACK_LS: + return ssh_receivepack_ls(t, url, stream); + + case GIT_SERVICE_RECEIVEPACK: + return ssh_receivepack(t, url, stream); + } + + *stream = NULL; + return -1; +} + +static int _ssh_close(git_smart_subtransport *subtransport) +{ + ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent); + + GIT_ASSERT(!t->current_stream); + + GIT_UNUSED(t); + + return 0; +} + +static void _ssh_free(git_smart_subtransport *subtransport) +{ + ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent); + + git__free(t->cmd_uploadpack); + git__free(t->cmd_receivepack); + git__free(t); +} + +#define SSH_AUTH_PUBLICKEY "publickey" +#define SSH_AUTH_PASSWORD "password" +#define SSH_AUTH_KEYBOARD_INTERACTIVE "keyboard-interactive" + +static int list_auth_methods(int *out, LIBSSH2_SESSION *session, const char *username) +{ + const char *list, *ptr; + + *out = 0; + + list = libssh2_userauth_list(session, username, + (unsigned int)strlen(username)); + + /* either error, or the remote accepts NONE auth, which is bizarre, let's punt */ + if (list == NULL && !libssh2_userauth_authenticated(session)) { + ssh_error(session, "remote rejected authentication"); + return GIT_EAUTH; + } + + ptr = list; + while (ptr) { + if (*ptr == ',') + ptr++; + + if (!git__prefixcmp(ptr, SSH_AUTH_PUBLICKEY)) { + *out |= GIT_CREDENTIAL_SSH_KEY; + *out |= GIT_CREDENTIAL_SSH_CUSTOM; +#ifdef GIT_SSH_LIBSSH2_MEMORY_CREDENTIALS + *out |= GIT_CREDENTIAL_SSH_MEMORY; +#endif + ptr += strlen(SSH_AUTH_PUBLICKEY); + continue; + } + + if (!git__prefixcmp(ptr, SSH_AUTH_PASSWORD)) { + *out |= GIT_CREDENTIAL_USERPASS_PLAINTEXT; + ptr += strlen(SSH_AUTH_PASSWORD); + continue; + } + + if (!git__prefixcmp(ptr, SSH_AUTH_KEYBOARD_INTERACTIVE)) { + *out |= GIT_CREDENTIAL_SSH_INTERACTIVE; + ptr += strlen(SSH_AUTH_KEYBOARD_INTERACTIVE); + continue; + } + + /* Skip it if we don't know it */ + ptr = strchr(ptr, ','); + } + + return 0; +} + +int git_smart_subtransport_ssh_libssh2( + git_smart_subtransport **out, + git_transport *owner, + void *param) +{ + ssh_subtransport *t; + + GIT_ASSERT_ARG(out); + + GIT_UNUSED(param); + + t = git__calloc(sizeof(ssh_subtransport), 1); + GIT_ERROR_CHECK_ALLOC(t); + + t->owner = (transport_smart *)owner; + t->parent.action = _ssh_action; + t->parent.close = _ssh_close; + t->parent.free = _ssh_free; + + *out = (git_smart_subtransport *) t; + return 0; +} + +int git_smart_subtransport_ssh_libssh2_set_paths( + git_smart_subtransport *subtransport, + const char *cmd_uploadpack, + const char *cmd_receivepack) +{ + ssh_subtransport *t = (ssh_subtransport *)subtransport; + + git__free(t->cmd_uploadpack); + git__free(t->cmd_receivepack); + + t->cmd_uploadpack = git__strdup(cmd_uploadpack); + GIT_ERROR_CHECK_ALLOC(t->cmd_uploadpack); + + t->cmd_receivepack = git__strdup(cmd_receivepack); + GIT_ERROR_CHECK_ALLOC(t->cmd_receivepack); + + return 0; +} + +static void shutdown_libssh2(void) +{ + libssh2_exit(); +} + +int git_transport_ssh_libssh2_global_init(void) +{ + if (libssh2_init(0) < 0) { + git_error_set(GIT_ERROR_SSH, "unable to initialize libssh2"); + return -1; + } + + return git_runtime_shutdown_register(shutdown_libssh2); +} + +#else /* GIT_SSH */ + +int git_transport_ssh_libssh2_global_init(void) +{ + return 0; +} + +#endif diff --git a/src/libgit2/transports/ssh_libssh2.h b/src/libgit2/transports/ssh_libssh2.h new file mode 100644 index 00000000000..3f8cc2a8ad9 --- /dev/null +++ b/src/libgit2/transports/ssh_libssh2.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_transports_libssh2_h__ +#define INCLUDE_transports_libssh2_h__ + +#include "common.h" + +#include "git2.h" +#include "git2/transport.h" +#include "git2/sys/transport.h" + +int git_transport_ssh_libssh2_global_init(void); + +int git_smart_subtransport_ssh_libssh2( + git_smart_subtransport **out, + git_transport *owner, + void *param); + +int git_smart_subtransport_ssh_libssh2_set_paths( + git_smart_subtransport *subtransport, + const char *cmd_uploadpack, + const char *cmd_receivepack); + +#endif diff --git a/src/libgit2/transports/winhttp.c b/src/libgit2/transports/winhttp.c new file mode 100644 index 00000000000..7141c284634 --- /dev/null +++ b/src/libgit2/transports/winhttp.c @@ -0,0 +1,1718 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" + +#ifdef GIT_HTTPS_WINHTTP + +#include "git2.h" +#include "git2/transport.h" +#include "posix.h" +#include "str.h" +#include "smart.h" +#include "remote.h" +#include "repository.h" +#include "http.h" +#include "git2/sys/credential.h" + +#include +#include + +/* For IInternetSecurityManager zone check */ +#include +#include + +#define WIDEN2(s) L ## s +#define WIDEN(s) WIDEN2(s) + +#define MAX_CONTENT_TYPE_LEN 100 +#define WINHTTP_OPTION_PEERDIST_EXTENSION_STATE 109 +#define CACHED_POST_BODY_BUF_SIZE 4096 +#define UUID_LENGTH_CCH 32 +#define TIMEOUT_INFINITE -1 +#define DEFAULT_CONNECT_TIMEOUT 60000 +#ifndef WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH +#define WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH 0 +#endif + +#ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 +# define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 0x00000200 +#endif + +#ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 +# define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 0x00000800 +#endif + +#ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3 +# define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3 0x00002000 +#endif + +#ifndef WINHTTP_NO_CLIENT_CERT_CONTEXT +# define WINHTTP_NO_CLIENT_CERT_CONTEXT NULL +#endif + +#ifndef HTTP_STATUS_PERMANENT_REDIRECT +# define HTTP_STATUS_PERMANENT_REDIRECT 308 +#endif + +#ifndef DWORD_MAX +# define DWORD_MAX 0xffffffff +#endif + +bool git_http__expect_continue = false; + +static const char *prefix_https = "https://"; +static const char *upload_pack_service = "upload-pack"; +static const char *upload_pack_ls_service_url = "/info/refs?service=git-upload-pack"; +static const char *upload_pack_service_url = "/git-upload-pack"; +static const char *receive_pack_service = "receive-pack"; +static const char *receive_pack_ls_service_url = "/info/refs?service=git-receive-pack"; +static const char *receive_pack_service_url = "/git-receive-pack"; +static const wchar_t *get_verb = L"GET"; +static const wchar_t *post_verb = L"POST"; +static const wchar_t *pragma_nocache = L"Pragma: no-cache"; +static const wchar_t *transfer_encoding = L"Transfer-Encoding: chunked"; +static const int no_check_cert_flags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID | + SECURITY_FLAG_IGNORE_CERT_DATE_INVALID | + SECURITY_FLAG_IGNORE_UNKNOWN_CA; + +#if defined(__MINGW32__) +static const CLSID CLSID_InternetSecurityManager_mingw = + { 0x7B8A2D94, 0x0AC9, 0x11D1, + { 0x89, 0x6C, 0x00, 0xC0, 0x4F, 0xB6, 0xBF, 0xC4 } }; +static const IID IID_IInternetSecurityManager_mingw = + { 0x79EAC9EE, 0xBAF9, 0x11CE, + { 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B } }; + +# define CLSID_InternetSecurityManager CLSID_InternetSecurityManager_mingw +# define IID_IInternetSecurityManager IID_IInternetSecurityManager_mingw +#endif + +#define OWNING_SUBTRANSPORT(s) ((winhttp_subtransport *)(s)->parent.subtransport) + +typedef enum { + GIT_WINHTTP_AUTH_BASIC = 1, + GIT_WINHTTP_AUTH_NTLM = 2, + GIT_WINHTTP_AUTH_NEGOTIATE = 4, + GIT_WINHTTP_AUTH_DIGEST = 8 +} winhttp_authmechanism_t; + +typedef struct { + git_smart_subtransport_stream parent; + const char *service; + const char *service_url; + const wchar_t *verb; + HINTERNET request; + wchar_t *request_uri; + char *chunk_buffer; + unsigned chunk_buffer_len; + HANDLE post_body; + DWORD post_body_len; + unsigned sent_request : 1, + received_response : 1, + chunked : 1, + status_sending_request_reached: 1; +} winhttp_stream; + +typedef struct { + git_net_url url; + git_credential *cred; + int auth_mechanisms; + bool url_cred_presented; +} winhttp_server; + +typedef struct { + git_smart_subtransport parent; + transport_smart *owner; + + winhttp_server server; + winhttp_server proxy; + + HINTERNET session; + HINTERNET connection; +} winhttp_subtransport; + +static int apply_userpass_credentials(HINTERNET request, DWORD target, int mechanisms, git_credential *cred) +{ + git_credential_userpass_plaintext *c = (git_credential_userpass_plaintext *)cred; + wchar_t *user = NULL, *pass = NULL; + int user_len = 0, pass_len = 0, error = 0; + DWORD native_scheme; + + if (mechanisms & GIT_WINHTTP_AUTH_NEGOTIATE) { + native_scheme = WINHTTP_AUTH_SCHEME_NEGOTIATE; + } else if (mechanisms & GIT_WINHTTP_AUTH_NTLM) { + native_scheme = WINHTTP_AUTH_SCHEME_NTLM; + } else if (mechanisms & GIT_WINHTTP_AUTH_DIGEST) { + native_scheme = WINHTTP_AUTH_SCHEME_DIGEST; + } else if (mechanisms & GIT_WINHTTP_AUTH_BASIC) { + native_scheme = WINHTTP_AUTH_SCHEME_BASIC; + } else { + git_error_set(GIT_ERROR_HTTP, "invalid authentication scheme"); + error = GIT_EAUTH; + goto done; + } + + if ((error = user_len = git_utf8_to_16_alloc(&user, c->username)) < 0) + goto done; + + if ((error = pass_len = git_utf8_to_16_alloc(&pass, c->password)) < 0) + goto done; + + if (!WinHttpSetCredentials(request, target, native_scheme, user, pass, NULL)) { + git_error_set(GIT_ERROR_OS, "failed to set credentials"); + error = -1; + } + +done: + if (user_len > 0) + git__memzero(user, user_len * sizeof(wchar_t)); + + if (pass_len > 0) + git__memzero(pass, pass_len * sizeof(wchar_t)); + + git__free(user); + git__free(pass); + + return error; +} + +static int apply_default_credentials(HINTERNET request, DWORD target, int mechanisms) +{ + DWORD autologon_level = WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW; + DWORD native_scheme = 0; + + if ((mechanisms & GIT_WINHTTP_AUTH_NEGOTIATE) != 0) { + native_scheme = WINHTTP_AUTH_SCHEME_NEGOTIATE; + } else if ((mechanisms & GIT_WINHTTP_AUTH_NTLM) != 0) { + native_scheme = WINHTTP_AUTH_SCHEME_NTLM; + } else { + git_error_set(GIT_ERROR_HTTP, "invalid authentication scheme"); + return GIT_EAUTH; + } + + /* + * Autologon policy must be "low" to use default creds. + * This is safe as the user has explicitly requested it. + */ + if (!WinHttpSetOption(request, WINHTTP_OPTION_AUTOLOGON_POLICY, &autologon_level, sizeof(DWORD))) { + git_error_set(GIT_ERROR_OS, "could not configure logon policy"); + return -1; + } + + if (!WinHttpSetCredentials(request, target, native_scheme, NULL, NULL, NULL)) { + git_error_set(GIT_ERROR_OS, "could not configure credentials"); + return -1; + } + + return 0; +} + +static int acquire_url_cred( + git_credential **cred, + unsigned int allowed_types, + const char *username, + const char *password) +{ + if (allowed_types & GIT_CREDENTIAL_USERPASS_PLAINTEXT) + return git_credential_userpass_plaintext_new(cred, username, password); + + if ((allowed_types & GIT_CREDENTIAL_DEFAULT) && *username == '\0' && *password == '\0') + return git_credential_default_new(cred); + + return 1; +} + +static int acquire_fallback_cred( + git_credential **cred, + const char *url, + unsigned int allowed_types) +{ + int error = 1; + + /* If the target URI supports integrated Windows authentication + * as an authentication mechanism */ + if (GIT_CREDENTIAL_DEFAULT & allowed_types) { + wchar_t *wide_url; + HRESULT hCoInitResult; + + /* Convert URL to wide characters */ + if (git_utf8_to_16_alloc(&wide_url, url) < 0) { + git_error_set(GIT_ERROR_OS, "failed to convert string to wide form"); + return -1; + } + + hCoInitResult = CoInitializeEx(NULL, COINIT_MULTITHREADED); + + if (SUCCEEDED(hCoInitResult) || hCoInitResult == RPC_E_CHANGED_MODE) { + IInternetSecurityManager *pISM; + + /* And if the target URI is in the My Computer, Intranet, or Trusted zones */ + if (SUCCEEDED(CoCreateInstance(&CLSID_InternetSecurityManager, NULL, + CLSCTX_ALL, &IID_IInternetSecurityManager, (void **)&pISM))) { + DWORD dwZone; + + if (SUCCEEDED(pISM->lpVtbl->MapUrlToZone(pISM, wide_url, &dwZone, 0)) && + (URLZONE_LOCAL_MACHINE == dwZone || + URLZONE_INTRANET == dwZone || + URLZONE_TRUSTED == dwZone)) { + git_credential *existing = *cred; + + if (existing) + existing->free(existing); + + /* Then use default Windows credentials to authenticate this request */ + error = git_credential_default_new(cred); + } + + pISM->lpVtbl->Release(pISM); + } + + /* Only uninitialize if the call to CoInitializeEx was successful. */ + if (SUCCEEDED(hCoInitResult)) + CoUninitialize(); + } + + git__free(wide_url); + } + + return error; +} + +static int certificate_check(winhttp_stream *s, int valid) +{ + int error; + winhttp_subtransport *t = OWNING_SUBTRANSPORT(s); + PCERT_CONTEXT cert_ctx; + DWORD cert_ctx_size = sizeof(cert_ctx); + git_cert_x509 cert; + + /* If there is no override, we should fail if WinHTTP doesn't think it's fine */ + if (t->owner->connect_opts.callbacks.certificate_check == NULL && !valid) { + if (git_error_last()->klass == GIT_ERROR_NONE) + git_error_set(GIT_ERROR_HTTP, "unknown certificate check failure"); + + return GIT_ECERTIFICATE; + } + + if (t->owner->connect_opts.callbacks.certificate_check == NULL || git__strcmp(t->server.url.scheme, "https") != 0) + return 0; + + if (!WinHttpQueryOption(s->request, WINHTTP_OPTION_SERVER_CERT_CONTEXT, &cert_ctx, &cert_ctx_size)) { + git_error_set(GIT_ERROR_OS, "failed to get server certificate"); + return -1; + } + + git_error_clear(); + cert.parent.cert_type = GIT_CERT_X509; + cert.data = cert_ctx->pbCertEncoded; + cert.len = cert_ctx->cbCertEncoded; + error = t->owner->connect_opts.callbacks.certificate_check((git_cert *) &cert, valid, t->server.url.host, t->owner->connect_opts.callbacks.payload); + CertFreeCertificateContext(cert_ctx); + + if (error == GIT_PASSTHROUGH) + error = valid ? 0 : GIT_ECERTIFICATE; + + if (error < 0 && git_error_last()->klass == GIT_ERROR_NONE) + git_error_set(GIT_ERROR_HTTP, "user cancelled certificate check"); + + return error; +} + +static void winhttp_stream_close(winhttp_stream *s) +{ + if (s->chunk_buffer) { + git__free(s->chunk_buffer); + s->chunk_buffer = NULL; + } + + if (s->post_body) { + CloseHandle(s->post_body); + s->post_body = NULL; + } + + if (s->request_uri) { + git__free(s->request_uri); + s->request_uri = NULL; + } + + if (s->request) { + WinHttpCloseHandle(s->request); + s->request = NULL; + } + + s->sent_request = 0; +} + +static int apply_credentials( + HINTERNET request, + git_net_url *url, + int target, + git_credential *creds, + int mechanisms) +{ + int error = 0; + + GIT_UNUSED(url); + + /* If we have creds, just apply them */ + if (creds && creds->credtype == GIT_CREDENTIAL_USERPASS_PLAINTEXT) + error = apply_userpass_credentials(request, target, mechanisms, creds); + else if (creds && creds->credtype == GIT_CREDENTIAL_DEFAULT) + error = apply_default_credentials(request, target, mechanisms); + + return error; +} + +static int winhttp_stream_connect(winhttp_stream *s) +{ + winhttp_subtransport *t = OWNING_SUBTRANSPORT(s); + git_str buf = GIT_STR_INIT; + char *proxy_url = NULL; + wchar_t ct[MAX_CONTENT_TYPE_LEN]; + LPCWSTR types[] = { L"*/*", NULL }; + BOOL peerdist = FALSE; + int error = -1; + unsigned long disable_redirects = WINHTTP_DISABLE_REDIRECTS; + int default_timeout = TIMEOUT_INFINITE; + int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT; + DWORD autologon_policy = WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH; + + const char *service_url = s->service_url; + size_t i; + const git_proxy_options *proxy_opts; + + /* If path already ends in /, remove the leading slash from service_url */ + if ((git__suffixcmp(t->server.url.path, "/") == 0) && (git__prefixcmp(service_url, "/") == 0)) + service_url++; + /* Prepare URL */ + git_str_printf(&buf, "%s%s", t->server.url.path, service_url); + + if (git_str_oom(&buf)) + return -1; + + /* Convert URL to wide characters */ + if (git_utf8_to_16_alloc(&s->request_uri, git_str_cstr(&buf)) < 0) { + git_error_set(GIT_ERROR_OS, "failed to convert string to wide form"); + goto on_error; + } + + /* Establish request */ + s->request = WinHttpOpenRequest( + t->connection, + s->verb, + s->request_uri, + NULL, + WINHTTP_NO_REFERER, + types, + git__strcmp(t->server.url.scheme, "https") == 0 ? WINHTTP_FLAG_SECURE : 0); + + if (!s->request) { + git_error_set(GIT_ERROR_OS, "failed to open request"); + goto on_error; + } + + /* Never attempt default credentials; we'll provide them explicitly. */ + if (!WinHttpSetOption(s->request, WINHTTP_OPTION_AUTOLOGON_POLICY, &autologon_policy, sizeof(DWORD))) + return -1; + + if (!WinHttpSetTimeouts(s->request, default_timeout, default_connect_timeout, default_timeout, default_timeout)) { + git_error_set(GIT_ERROR_OS, "failed to set timeouts for WinHTTP"); + goto on_error; + } + + proxy_opts = &t->owner->connect_opts.proxy_opts; + if (proxy_opts->type == GIT_PROXY_AUTO) { + /* Set proxy if necessary */ + if (git_remote__http_proxy(&proxy_url, t->owner->owner, &t->server.url) < 0) + goto on_error; + } + else if (proxy_opts->type == GIT_PROXY_SPECIFIED) { + proxy_url = git__strdup(proxy_opts->url); + GIT_ERROR_CHECK_ALLOC(proxy_url); + } + + if (proxy_url && *proxy_url) { + git_str processed_url = GIT_STR_INIT; + WINHTTP_PROXY_INFO proxy_info; + wchar_t *proxy_wide; + + git_net_url_dispose(&t->proxy.url); + + if ((error = git_net_url_parse_http(&t->proxy.url, proxy_url)) < 0) + goto on_error; + + if (!git_net_url_valid(&t->proxy.url)) { + git_error_set(GIT_ERROR_HTTP, "invalid URL: '%s'", proxy_url); + error = -1; + goto on_error; + } + + git_str_puts(&processed_url, t->proxy.url.scheme); + git_str_PUTS(&processed_url, "://"); + + if (git_net_url_is_ipv6(&t->proxy.url)) + git_str_putc(&processed_url, '['); + + git_str_puts(&processed_url, t->proxy.url.host); + + if (git_net_url_is_ipv6(&t->proxy.url)) + git_str_putc(&processed_url, ']'); + + if (!git_net_url_is_default_port(&t->proxy.url)) + git_str_printf(&processed_url, ":%s", t->proxy.url.port); + + if (git_str_oom(&processed_url)) { + error = -1; + goto on_error; + } + + /* Convert URL to wide characters */ + error = git_utf8_to_16_alloc(&proxy_wide, processed_url.ptr); + git_str_dispose(&processed_url); + if (error < 0) + goto on_error; + + proxy_info.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY; + proxy_info.lpszProxy = proxy_wide; + proxy_info.lpszProxyBypass = NULL; + + if (!WinHttpSetOption(s->request, + WINHTTP_OPTION_PROXY, + &proxy_info, + sizeof(WINHTTP_PROXY_INFO))) { + git_error_set(GIT_ERROR_OS, "failed to set proxy"); + git__free(proxy_wide); + goto on_error; + } + + git__free(proxy_wide); + + if ((error = apply_credentials(s->request, &t->proxy.url, WINHTTP_AUTH_TARGET_PROXY, t->proxy.cred, t->proxy.auth_mechanisms)) < 0) + goto on_error; + } + + /* Disable WinHTTP redirects so we can handle them manually. Why, you ask? + * http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/b2ff8879-ab9f-4218-8f09-16d25dff87ae + */ + if (!WinHttpSetOption(s->request, + WINHTTP_OPTION_DISABLE_FEATURE, + &disable_redirects, + sizeof(disable_redirects))) { + git_error_set(GIT_ERROR_OS, "failed to disable redirects"); + error = -1; + goto on_error; + } + + /* Strip unwanted headers (X-P2P-PeerDist, X-P2P-PeerDistEx) that WinHTTP + * adds itself. This option may not be supported by the underlying + * platform, so we do not error-check it */ + WinHttpSetOption(s->request, + WINHTTP_OPTION_PEERDIST_EXTENSION_STATE, + &peerdist, + sizeof(peerdist)); + + /* Send Pragma: no-cache header */ + if (!WinHttpAddRequestHeaders(s->request, pragma_nocache, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) { + git_error_set(GIT_ERROR_OS, "failed to add a header to the request"); + goto on_error; + } + + if (post_verb == s->verb) { + /* Send Content-Type and Accept headers -- only necessary on a POST */ + git_str_clear(&buf); + if (git_str_printf(&buf, + "Content-Type: application/x-git-%s-request", + s->service) < 0) + goto on_error; + + if (git_utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_str_cstr(&buf)) < 0) { + git_error_set(GIT_ERROR_OS, "failed to convert content-type to wide characters"); + goto on_error; + } + + if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L, + WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) { + git_error_set(GIT_ERROR_OS, "failed to add a header to the request"); + goto on_error; + } + + git_str_clear(&buf); + if (git_str_printf(&buf, + "Accept: application/x-git-%s-result", + s->service) < 0) + goto on_error; + + if (git_utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_str_cstr(&buf)) < 0) { + git_error_set(GIT_ERROR_OS, "failed to convert accept header to wide characters"); + goto on_error; + } + + if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L, + WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) { + git_error_set(GIT_ERROR_OS, "failed to add a header to the request"); + goto on_error; + } + } + + for (i = 0; i < t->owner->connect_opts.custom_headers.count; i++) { + if (t->owner->connect_opts.custom_headers.strings[i]) { + wchar_t *custom_header_wide = NULL; + + git_str_clear(&buf); + git_str_puts(&buf, t->owner->connect_opts.custom_headers.strings[i]); + + /* Convert header to wide characters */ + if ((error = git_utf8_to_16_alloc(&custom_header_wide, git_str_cstr(&buf))) < 0) + goto on_error; + + if (!WinHttpAddRequestHeaders(s->request, custom_header_wide, (ULONG)-1L, + WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) { + git_error_set(GIT_ERROR_OS, "failed to add a header to the request"); + git__free(custom_header_wide); + goto on_error; + } + + git__free(custom_header_wide); + } + } + + if ((error = apply_credentials(s->request, &t->server.url, WINHTTP_AUTH_TARGET_SERVER, t->server.cred, t->server.auth_mechanisms)) < 0) + goto on_error; + + /* We've done everything up to calling WinHttpSendRequest. */ + + error = 0; + +on_error: + if (error < 0) + winhttp_stream_close(s); + + git__free(proxy_url); + git_str_dispose(&buf); + return error; +} + +static int parse_unauthorized_response( + int *allowed_types, + int *allowed_mechanisms, + HINTERNET request) +{ + DWORD supported, first, target; + + *allowed_types = 0; + *allowed_mechanisms = 0; + + /* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes(). + * We can assume this was already done, since we know we are unauthorized. + */ + if (!WinHttpQueryAuthSchemes(request, &supported, &first, &target)) { + git_error_set(GIT_ERROR_OS, "failed to parse supported auth schemes"); + return GIT_EAUTH; + } + + if (WINHTTP_AUTH_SCHEME_NTLM & supported) { + *allowed_types |= GIT_CREDENTIAL_USERPASS_PLAINTEXT; + *allowed_types |= GIT_CREDENTIAL_DEFAULT; + *allowed_mechanisms |= GIT_WINHTTP_AUTH_NTLM; + } + + if (WINHTTP_AUTH_SCHEME_NEGOTIATE & supported) { + *allowed_types |= GIT_CREDENTIAL_DEFAULT; + *allowed_mechanisms |= GIT_WINHTTP_AUTH_NEGOTIATE; + } + + if (WINHTTP_AUTH_SCHEME_BASIC & supported) { + *allowed_types |= GIT_CREDENTIAL_USERPASS_PLAINTEXT; + *allowed_mechanisms |= GIT_WINHTTP_AUTH_BASIC; + } + + if (WINHTTP_AUTH_SCHEME_DIGEST & supported) { + *allowed_types |= GIT_CREDENTIAL_USERPASS_PLAINTEXT; + *allowed_mechanisms |= GIT_WINHTTP_AUTH_DIGEST; + } + + return 0; +} + +static int write_chunk(HINTERNET request, const char *buffer, size_t len) +{ + DWORD bytes_written; + git_str buf = GIT_STR_INIT; + + /* Chunk header */ + git_str_printf(&buf, "%"PRIXZ"\r\n", len); + + if (git_str_oom(&buf)) + return -1; + + if (!WinHttpWriteData(request, + git_str_cstr(&buf), (DWORD)git_str_len(&buf), + &bytes_written)) { + git_str_dispose(&buf); + git_error_set(GIT_ERROR_OS, "failed to write chunk header"); + return -1; + } + + git_str_dispose(&buf); + + /* Chunk body */ + if (!WinHttpWriteData(request, + buffer, (DWORD)len, + &bytes_written)) { + git_error_set(GIT_ERROR_OS, "failed to write chunk"); + return -1; + } + + /* Chunk footer */ + if (!WinHttpWriteData(request, + "\r\n", 2, + &bytes_written)) { + git_error_set(GIT_ERROR_OS, "failed to write chunk footer"); + return -1; + } + + return 0; +} + +static int winhttp_close_connection(winhttp_subtransport *t) +{ + int ret = 0; + + if (t->connection) { + if (!WinHttpCloseHandle(t->connection)) { + git_error_set(GIT_ERROR_OS, "unable to close connection"); + ret = -1; + } + + t->connection = NULL; + } + + if (t->session) { + if (!WinHttpCloseHandle(t->session)) { + git_error_set(GIT_ERROR_OS, "unable to close session"); + ret = -1; + } + + t->session = NULL; + } + + return ret; +} + +static void CALLBACK winhttp_status( + HINTERNET connection, + DWORD_PTR ctx, + DWORD code, + LPVOID info, + DWORD info_len) +{ + DWORD status; + + GIT_UNUSED(connection); + GIT_UNUSED(info_len); + + switch (code) { + case WINHTTP_CALLBACK_STATUS_SECURE_FAILURE: + status = *((DWORD *)info); + + if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID)) + git_error_set(GIT_ERROR_HTTP, "SSL certificate issued for different common name"); + else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID)) + git_error_set(GIT_ERROR_HTTP, "SSL certificate has expired"); + else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA)) + git_error_set(GIT_ERROR_HTTP, "SSL certificate signed by unknown CA"); + else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT)) + git_error_set(GIT_ERROR_HTTP, "SSL certificate is invalid"); + else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED)) + git_error_set(GIT_ERROR_HTTP, "certificate revocation check failed"); + else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED)) + git_error_set(GIT_ERROR_HTTP, "SSL certificate was revoked"); + else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR)) + git_error_set(GIT_ERROR_HTTP, "security libraries could not be loaded"); + else + git_error_set(GIT_ERROR_HTTP, "unknown security error %lu", status); + + break; + + case WINHTTP_CALLBACK_STATUS_SENDING_REQUEST: + ((winhttp_stream *) ctx)->status_sending_request_reached = 1; + + break; + } +} + +static int user_agent(bool *exists, git_str *out) +{ + const char *product = git_settings__user_agent_product(); + const char *comment = git_settings__user_agent(); + + GIT_ASSERT(product && comment); + + if (!*product) { + *exists = false; + return 0; + } + + git_str_puts(out, product); + + if (*comment) { + git_str_puts(out, " ("); + git_str_puts(out, comment); + git_str_puts(out, ")"); + } + + if (git_str_oom(out)) + return -1; + + *exists = true; + return 0; +} + +static int winhttp_connect( + winhttp_subtransport *t) +{ + wchar_t *wide_host = NULL; + int32_t port; + wchar_t *wide_ua = NULL; + git_str ipv6 = GIT_STR_INIT, ua = GIT_STR_INIT; + const char *host; + int error = -1; + int default_timeout = TIMEOUT_INFINITE; + int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT; + bool has_ua = true; + DWORD protocols = + WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 | + WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 | + WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 | + WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3; + + t->session = NULL; + t->connection = NULL; + + /* Prepare port */ + if (git__strntol32(&port, t->server.url.port, + strlen(t->server.url.port), NULL, 10) < 0) + goto on_error; + + /* IPv6? Add braces around the host. */ + if (git_net_url_is_ipv6(&t->server.url)) { + if (git_str_printf(&ipv6, "[%s]", t->server.url.host) < 0) + goto on_error; + + host = ipv6.ptr; + } else { + host = t->server.url.host; + } + + /* Prepare host */ + if (git_utf8_to_16_alloc(&wide_host, host) < 0) { + git_error_set(GIT_ERROR_OS, "unable to convert host to wide characters"); + goto on_error; + } + + if (user_agent(&has_ua, &ua) < 0) + goto on_error; + + if (has_ua && + git_utf8_to_16_alloc(&wide_ua, git_str_cstr(&ua)) < 0) { + git_error_set(GIT_ERROR_OS, "unable to convert host to wide characters"); + goto on_error; + } + + /* Establish session */ + t->session = WinHttpOpen( + wide_ua, + WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, + WINHTTP_NO_PROXY_NAME, + WINHTTP_NO_PROXY_BYPASS, + 0); + + if (!t->session) { + git_error_set(GIT_ERROR_OS, "failed to init WinHTTP"); + goto on_error; + } + + /* + * Do a best-effort attempt to enable TLS 1.3 and 1.2 but allow this to + * fail; if TLS 1.2 or 1.3 support is not available for some reason, + * ignore the failure (it will keep the default protocols). + */ + if (WinHttpSetOption(t->session, + WINHTTP_OPTION_SECURE_PROTOCOLS, + &protocols, + sizeof(protocols)) == FALSE) { + protocols &= ~WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3; + WinHttpSetOption(t->session, + WINHTTP_OPTION_SECURE_PROTOCOLS, + &protocols, + sizeof(protocols)); + } + + if (!WinHttpSetTimeouts(t->session, default_timeout, default_connect_timeout, default_timeout, default_timeout)) { + git_error_set(GIT_ERROR_OS, "failed to set timeouts for WinHTTP"); + goto on_error; + } + + + /* Establish connection */ + t->connection = WinHttpConnect( + t->session, + wide_host, + (INTERNET_PORT) port, + 0); + + if (!t->connection) { + git_error_set(GIT_ERROR_OS, "failed to connect to host"); + goto on_error; + } + + if (WinHttpSetStatusCallback( + t->connection, + winhttp_status, + WINHTTP_CALLBACK_FLAG_SECURE_FAILURE | WINHTTP_CALLBACK_FLAG_SEND_REQUEST, + 0 + ) == WINHTTP_INVALID_STATUS_CALLBACK) { + git_error_set(GIT_ERROR_OS, "failed to set status callback"); + goto on_error; + } + + error = 0; + +on_error: + if (error < 0) + winhttp_close_connection(t); + + git_str_dispose(&ua); + git_str_dispose(&ipv6); + git__free(wide_host); + git__free(wide_ua); + + return error; +} + +static int do_send_request(winhttp_stream *s, size_t len, bool chunked) +{ + int attempts; + bool success; + + if (len > DWORD_MAX) { + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + return -1; + } + + for (attempts = 0; attempts < 5; attempts++) { + if (chunked) { + success = WinHttpSendRequest(s->request, + WINHTTP_NO_ADDITIONAL_HEADERS, 0, + WINHTTP_NO_REQUEST_DATA, 0, + WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH, (DWORD_PTR)s); + } else { + success = WinHttpSendRequest(s->request, + WINHTTP_NO_ADDITIONAL_HEADERS, 0, + WINHTTP_NO_REQUEST_DATA, 0, + (DWORD)len, (DWORD_PTR)s); + } + + if (success || GetLastError() != (DWORD)SEC_E_BUFFER_TOO_SMALL) + break; + } + + return success ? 0 : -1; +} + +static int send_request(winhttp_stream *s, size_t len, bool chunked) +{ + int request_failed = 1, error, attempts = 0; + DWORD ignore_flags, send_request_error; + + git_error_clear(); + + while (request_failed && attempts++ < 3) { + int cert_valid = 1; + int client_cert_requested = 0; + request_failed = 0; + if ((error = do_send_request(s, len, chunked)) < 0) { + send_request_error = GetLastError(); + request_failed = 1; + switch (send_request_error) { + case ERROR_WINHTTP_SECURE_FAILURE: + cert_valid = 0; + break; + case ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED: + client_cert_requested = 1; + break; + default: + git_error_set(GIT_ERROR_OS, "failed to send request"); + return -1; + } + } + + /* + * Only check the certificate if we were able to reach the sending request phase, or + * received a secure failure error. Otherwise, the server certificate won't be available + * since the request wasn't able to complete (e.g. proxy auth required) + */ + if (!cert_valid || + (!request_failed && s->status_sending_request_reached)) { + git_error_clear(); + if ((error = certificate_check(s, cert_valid)) < 0) { + if (git_error_last()->klass == GIT_ERROR_NONE) + git_error_set(GIT_ERROR_OS, "user cancelled certificate check"); + + return error; + } + } + + /* if neither the request nor the certificate check returned errors, we're done */ + if (!request_failed) + return 0; + + if (!cert_valid) { + ignore_flags = no_check_cert_flags; + if (!WinHttpSetOption(s->request, WINHTTP_OPTION_SECURITY_FLAGS, &ignore_flags, sizeof(ignore_flags))) { + git_error_set(GIT_ERROR_OS, "failed to set security options"); + return -1; + } + } + + if (client_cert_requested) { + /* + * Client certificates are not supported, explicitly tell the server that + * (it's possible a client certificate was requested but is not required) + */ + if (!WinHttpSetOption(s->request, WINHTTP_OPTION_CLIENT_CERT_CONTEXT, WINHTTP_NO_CLIENT_CERT_CONTEXT, 0)) { + git_error_set(GIT_ERROR_OS, "failed to set client cert context"); + return -1; + } + } + } + + return error; +} + +static int acquire_credentials( + HINTERNET request, + winhttp_server *server, + const char *url_str, + git_credential_acquire_cb cred_cb, + void *cred_cb_payload) +{ + int allowed_types; + int error = 1; + + if (parse_unauthorized_response(&allowed_types, &server->auth_mechanisms, request) < 0) + return -1; + + if (allowed_types) { + git_credential_free(server->cred); + server->cred = NULL; + + /* Start with URL-specified credentials, if there were any. */ + if (!server->url_cred_presented && server->url.username && server->url.password) { + error = acquire_url_cred(&server->cred, allowed_types, server->url.username, server->url.password); + server->url_cred_presented = 1; + + if (error < 0) + return error; + } + + /* Next use the user-defined callback, if there is one. */ + if (error > 0 && cred_cb) { + error = cred_cb(&server->cred, url_str, server->url.username, allowed_types, cred_cb_payload); + + /* Treat GIT_PASSTHROUGH as though git_credential_acquire_cb isn't set */ + if (error == GIT_PASSTHROUGH) + error = 1; + else if (error < 0) + return error; + } + + /* Finally, invoke the fallback default credential lookup. */ + if (error > 0) { + error = acquire_fallback_cred(&server->cred, url_str, allowed_types); + + if (error < 0) + return error; + } + } + + /* + * No error occurred but we could not find appropriate credentials. + * This behaves like a pass-through. + */ + return error; +} + +static int winhttp_stream_read( + git_smart_subtransport_stream *stream, + char *buffer, + size_t buf_size, + size_t *bytes_read) +{ + winhttp_stream *s = (winhttp_stream *)stream; + winhttp_subtransport *t = OWNING_SUBTRANSPORT(s); + DWORD dw_bytes_read; + char replay_count = 0; + int error; + +replay: + /* Enforce a reasonable cap on the number of replays */ + if (replay_count++ >= GIT_HTTP_REPLAY_MAX) { + git_error_set(GIT_ERROR_HTTP, "too many redirects or authentication replays"); + return GIT_ERROR; /* not GIT_EAUTH because the exact cause is not clear */ + } + + /* Connect if necessary */ + if (!s->request && winhttp_stream_connect(s) < 0) + return -1; + + if (!s->received_response) { + DWORD status_code, status_code_length, content_type_length, bytes_written; + char expected_content_type_8[MAX_CONTENT_TYPE_LEN]; + wchar_t expected_content_type[MAX_CONTENT_TYPE_LEN], content_type[MAX_CONTENT_TYPE_LEN]; + + if (!s->sent_request) { + + if ((error = send_request(s, s->post_body_len, false)) < 0) + return error; + + s->sent_request = 1; + } + + if (s->chunked) { + GIT_ASSERT(s->verb == post_verb); + + /* Flush, if necessary */ + if (s->chunk_buffer_len > 0 && + write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0) + return -1; + + s->chunk_buffer_len = 0; + + /* Write the final chunk. */ + if (!WinHttpWriteData(s->request, + "0\r\n\r\n", 5, + &bytes_written)) { + git_error_set(GIT_ERROR_OS, "failed to write final chunk"); + return -1; + } + } + else if (s->post_body) { + char *buffer; + DWORD len = s->post_body_len, bytes_read; + + if (INVALID_SET_FILE_POINTER == SetFilePointer(s->post_body, + 0, 0, FILE_BEGIN) && + NO_ERROR != GetLastError()) { + git_error_set(GIT_ERROR_OS, "failed to reset file pointer"); + return -1; + } + + buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE); + GIT_ERROR_CHECK_ALLOC(buffer); + + while (len > 0) { + DWORD bytes_written; + + if (!ReadFile(s->post_body, buffer, + min(CACHED_POST_BODY_BUF_SIZE, len), + &bytes_read, NULL) || + !bytes_read) { + git__free(buffer); + git_error_set(GIT_ERROR_OS, "failed to read from temp file"); + return -1; + } + + if (!WinHttpWriteData(s->request, buffer, + bytes_read, &bytes_written)) { + git__free(buffer); + git_error_set(GIT_ERROR_OS, "failed to write data"); + return -1; + } + + len -= bytes_read; + GIT_ASSERT(bytes_read == bytes_written); + } + + git__free(buffer); + + /* Eagerly close the temp file */ + CloseHandle(s->post_body); + s->post_body = NULL; + } + + if (!WinHttpReceiveResponse(s->request, 0)) { + git_error_set(GIT_ERROR_OS, "failed to receive response"); + return -1; + } + + /* Verify that we got a 200 back */ + status_code_length = sizeof(status_code); + + if (!WinHttpQueryHeaders(s->request, + WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, + WINHTTP_HEADER_NAME_BY_INDEX, + &status_code, &status_code_length, + WINHTTP_NO_HEADER_INDEX)) { + git_error_set(GIT_ERROR_OS, "failed to retrieve status code"); + return -1; + } + + /* The implementation of WinHTTP prior to Windows 7 will not + * redirect to an identical URI. Some Git hosters use self-redirects + * as part of their DoS mitigation strategy. Check first to see if we + * have a redirect status code, and that we haven't already streamed + * a post body. (We can't replay a streamed POST.) */ + if (!s->chunked && + (HTTP_STATUS_MOVED == status_code || + HTTP_STATUS_REDIRECT == status_code || + (HTTP_STATUS_REDIRECT_METHOD == status_code && + get_verb == s->verb) || + HTTP_STATUS_REDIRECT_KEEP_VERB == status_code || + HTTP_STATUS_PERMANENT_REDIRECT == status_code)) { + + /* Check for Windows 7. This workaround is only necessary on + * Windows Vista and earlier. Windows 7 is version 6.1. */ + wchar_t *location; + DWORD location_length; + char *location8; + + /* OK, fetch the Location header from the redirect. */ + if (WinHttpQueryHeaders(s->request, + WINHTTP_QUERY_LOCATION, + WINHTTP_HEADER_NAME_BY_INDEX, + WINHTTP_NO_OUTPUT_BUFFER, + &location_length, + WINHTTP_NO_HEADER_INDEX) || + GetLastError() != ERROR_INSUFFICIENT_BUFFER) { + git_error_set(GIT_ERROR_OS, "failed to read Location header"); + return -1; + } + + location = git__malloc(location_length); + GIT_ERROR_CHECK_ALLOC(location); + + if (!WinHttpQueryHeaders(s->request, + WINHTTP_QUERY_LOCATION, + WINHTTP_HEADER_NAME_BY_INDEX, + location, + &location_length, + WINHTTP_NO_HEADER_INDEX)) { + git_error_set(GIT_ERROR_OS, "failed to read Location header"); + git__free(location); + return -1; + } + + /* Convert the Location header to UTF-8 */ + if (git_utf8_from_16_alloc(&location8, location) < 0) { + git_error_set(GIT_ERROR_OS, "failed to convert Location header to UTF-8"); + git__free(location); + return -1; + } + + git__free(location); + + /* Replay the request */ + winhttp_stream_close(s); + + if (!git__prefixcmp_icase(location8, prefix_https)) { + bool follow = (t->owner->connect_opts.follow_redirects != GIT_REMOTE_REDIRECT_NONE); + + /* Upgrade to secure connection; disconnect and start over */ + if (git_net_url_apply_redirect(&t->server.url, location8, follow, s->service_url) < 0) { + git__free(location8); + return -1; + } + + winhttp_close_connection(t); + + if (winhttp_connect(t) < 0) + return -1; + } + + git__free(location8); + goto replay; + } + + /* Handle authentication failures */ + if (status_code == HTTP_STATUS_DENIED) { + int error = acquire_credentials(s->request, + &t->server, + t->owner->url, + t->owner->connect_opts.callbacks.credentials, + t->owner->connect_opts.callbacks.payload); + + if (error < 0) { + return error; + } else if (!error) { + GIT_ASSERT(t->server.cred); + winhttp_stream_close(s); + goto replay; + } + } else if (status_code == HTTP_STATUS_PROXY_AUTH_REQ) { + int error = acquire_credentials(s->request, + &t->proxy, + t->owner->connect_opts.proxy_opts.url, + t->owner->connect_opts.proxy_opts.credentials, + t->owner->connect_opts.proxy_opts.payload); + + if (error < 0) { + return error; + } else if (!error) { + GIT_ASSERT(t->proxy.cred); + winhttp_stream_close(s); + goto replay; + } + } + + if (HTTP_STATUS_OK != status_code) { + git_error_set(GIT_ERROR_HTTP, "request failed with status code: %lu", status_code); + return -1; + } + + /* Verify that we got the correct content-type back */ + if (post_verb == s->verb) + p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-result", s->service); + else + p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-advertisement", s->service); + + if (git_utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8) < 0) { + git_error_set(GIT_ERROR_OS, "failed to convert expected content-type to wide characters"); + return -1; + } + + content_type_length = sizeof(content_type); + + if (!WinHttpQueryHeaders(s->request, + WINHTTP_QUERY_CONTENT_TYPE, + WINHTTP_HEADER_NAME_BY_INDEX, + &content_type, &content_type_length, + WINHTTP_NO_HEADER_INDEX)) { + git_error_set(GIT_ERROR_OS, "failed to retrieve response content-type"); + return -1; + } + + if (wcscmp(expected_content_type, content_type)) { + git_error_set(GIT_ERROR_HTTP, "received unexpected content-type"); + return -1; + } + + s->received_response = 1; + } + + if (!WinHttpReadData(s->request, + (LPVOID)buffer, + (DWORD)buf_size, + &dw_bytes_read)) + { + git_error_set(GIT_ERROR_OS, "failed to read data"); + return -1; + } + + *bytes_read = dw_bytes_read; + + return 0; +} + +static int winhttp_stream_write_single( + git_smart_subtransport_stream *stream, + const char *buffer, + size_t len) +{ + winhttp_stream *s = (winhttp_stream *)stream; + DWORD bytes_written; + int error; + + if (!s->request && winhttp_stream_connect(s) < 0) + return -1; + + /* This implementation of write permits only a single call. */ + if (s->sent_request) { + git_error_set(GIT_ERROR_HTTP, "subtransport configured for only one write"); + return -1; + } + + if ((error = send_request(s, len, false)) < 0) + return error; + + s->sent_request = 1; + + if (!WinHttpWriteData(s->request, + (LPCVOID)buffer, + (DWORD)len, + &bytes_written)) { + git_error_set(GIT_ERROR_OS, "failed to write data"); + return -1; + } + + GIT_ASSERT((DWORD)len == bytes_written); + + return 0; +} + +static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch) +{ + UUID uuid; + RPC_STATUS status = UuidCreate(&uuid); + int result; + + if (RPC_S_OK != status && + RPC_S_UUID_LOCAL_ONLY != status && + RPC_S_UUID_NO_ADDRESS != status) { + git_error_set(GIT_ERROR_HTTP, "unable to generate name for temp file"); + return -1; + } + + if (buffer_len_cch < UUID_LENGTH_CCH + 1) { + git_error_set(GIT_ERROR_HTTP, "buffer too small for name of temp file"); + return -1; + } + +#if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API) + result = swprintf_s(buffer, buffer_len_cch, +#else + result = wsprintfW(buffer, +#endif + L"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x", + uuid.Data1, uuid.Data2, uuid.Data3, + uuid.Data4[0], uuid.Data4[1], uuid.Data4[2], uuid.Data4[3], + uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]); + + if (result < UUID_LENGTH_CCH) { + git_error_set(GIT_ERROR_OS, "unable to generate name for temp file"); + return -1; + } + + return 0; +} + +static int get_temp_file(LPWSTR buffer, DWORD buffer_len_cch) +{ + size_t len; + + if (!GetTempPathW(buffer_len_cch, buffer)) { + git_error_set(GIT_ERROR_OS, "failed to get temp path"); + return -1; + } + + len = wcslen(buffer); + + if (buffer[len - 1] != '\\' && len < buffer_len_cch) + buffer[len++] = '\\'; + + if (put_uuid_string(&buffer[len], (size_t)buffer_len_cch - len) < 0) + return -1; + + return 0; +} + +static int winhttp_stream_write_buffered( + git_smart_subtransport_stream *stream, + const char *buffer, + size_t len) +{ + winhttp_stream *s = (winhttp_stream *)stream; + DWORD bytes_written; + + if (!s->request && winhttp_stream_connect(s) < 0) + return -1; + + /* Buffer the payload, using a temporary file so we delegate + * memory management of the data to the operating system. */ + if (!s->post_body) { + wchar_t temp_path[MAX_PATH + 1]; + + if (get_temp_file(temp_path, MAX_PATH + 1) < 0) + return -1; + + s->post_body = CreateFileW(temp_path, + GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_DELETE, NULL, + CREATE_NEW, + FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_SEQUENTIAL_SCAN, + NULL); + + if (INVALID_HANDLE_VALUE == s->post_body) { + s->post_body = NULL; + git_error_set(GIT_ERROR_OS, "failed to create temporary file"); + return -1; + } + } + + if (!WriteFile(s->post_body, buffer, (DWORD)len, &bytes_written, NULL)) { + git_error_set(GIT_ERROR_OS, "failed to write to temporary file"); + return -1; + } + + GIT_ASSERT((DWORD)len == bytes_written); + + s->post_body_len += bytes_written; + + return 0; +} + +static int winhttp_stream_write_chunked( + git_smart_subtransport_stream *stream, + const char *buffer, + size_t len) +{ + winhttp_stream *s = (winhttp_stream *)stream; + int error; + + if (!s->request && winhttp_stream_connect(s) < 0) + return -1; + + if (!s->sent_request) { + /* Send Transfer-Encoding: chunked header */ + if (!WinHttpAddRequestHeaders(s->request, + transfer_encoding, (ULONG) -1L, + WINHTTP_ADDREQ_FLAG_ADD)) { + git_error_set(GIT_ERROR_OS, "failed to add a header to the request"); + return -1; + } + + if ((error = send_request(s, 0, true)) < 0) + return error; + + s->sent_request = 1; + } + + if (len > CACHED_POST_BODY_BUF_SIZE) { + /* Flush, if necessary */ + if (s->chunk_buffer_len > 0) { + if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0) + return -1; + + s->chunk_buffer_len = 0; + } + + /* Write chunk directly */ + if (write_chunk(s->request, buffer, len) < 0) + return -1; + } + else { + /* Append as much to the buffer as we can */ + int count = (int)min(CACHED_POST_BODY_BUF_SIZE - s->chunk_buffer_len, len); + + if (!s->chunk_buffer) { + s->chunk_buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE); + GIT_ERROR_CHECK_ALLOC(s->chunk_buffer); + } + + memcpy(s->chunk_buffer + s->chunk_buffer_len, buffer, count); + s->chunk_buffer_len += count; + buffer += count; + len -= count; + + /* Is the buffer full? If so, then flush */ + if (CACHED_POST_BODY_BUF_SIZE == s->chunk_buffer_len) { + if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0) + return -1; + + s->chunk_buffer_len = 0; + + /* Is there any remaining data from the source? */ + if (len > 0) { + memcpy(s->chunk_buffer, buffer, len); + s->chunk_buffer_len = (unsigned int)len; + } + } + } + + return 0; +} + +static void winhttp_stream_free(git_smart_subtransport_stream *stream) +{ + winhttp_stream *s = (winhttp_stream *)stream; + + winhttp_stream_close(s); + git__free(s); +} + +static int winhttp_stream_alloc(winhttp_subtransport *t, winhttp_stream **stream) +{ + winhttp_stream *s; + + if (!stream) + return -1; + + s = git__calloc(1, sizeof(winhttp_stream)); + GIT_ERROR_CHECK_ALLOC(s); + + s->parent.subtransport = &t->parent; + s->parent.read = winhttp_stream_read; + s->parent.write = winhttp_stream_write_single; + s->parent.free = winhttp_stream_free; + + *stream = s; + + return 0; +} + +static int winhttp_uploadpack_ls( + winhttp_subtransport *t, + winhttp_stream *s) +{ + GIT_UNUSED(t); + + s->service = upload_pack_service; + s->service_url = upload_pack_ls_service_url; + s->verb = get_verb; + + return 0; +} + +static int winhttp_uploadpack( + winhttp_subtransport *t, + winhttp_stream *s) +{ + GIT_UNUSED(t); + + s->service = upload_pack_service; + s->service_url = upload_pack_service_url; + s->verb = post_verb; + + return 0; +} + +static int winhttp_receivepack_ls( + winhttp_subtransport *t, + winhttp_stream *s) +{ + GIT_UNUSED(t); + + s->service = receive_pack_service; + s->service_url = receive_pack_ls_service_url; + s->verb = get_verb; + + return 0; +} + +static int winhttp_receivepack( + winhttp_subtransport *t, + winhttp_stream *s) +{ + GIT_UNUSED(t); + + /* WinHTTP only supports Transfer-Encoding: chunked + * on Windows Vista (NT 6.0) and higher. */ + s->chunked = git_has_win32_version(6, 0, 0); + + if (s->chunked) + s->parent.write = winhttp_stream_write_chunked; + else + s->parent.write = winhttp_stream_write_buffered; + + s->service = receive_pack_service; + s->service_url = receive_pack_service_url; + s->verb = post_verb; + + return 0; +} + +static int winhttp_action( + git_smart_subtransport_stream **stream, + git_smart_subtransport *subtransport, + const char *url, + git_smart_service_t action) +{ + winhttp_subtransport *t = (winhttp_subtransport *)subtransport; + winhttp_stream *s; + int ret = -1; + + if (!t->connection) + if ((ret = git_net_url_parse(&t->server.url, url)) < 0 || + (ret = winhttp_connect(t)) < 0) + return ret; + + if (winhttp_stream_alloc(t, &s) < 0) + return -1; + + if (!stream) + return -1; + + switch (action) + { + case GIT_SERVICE_UPLOADPACK_LS: + ret = winhttp_uploadpack_ls(t, s); + break; + + case GIT_SERVICE_UPLOADPACK: + ret = winhttp_uploadpack(t, s); + break; + + case GIT_SERVICE_RECEIVEPACK_LS: + ret = winhttp_receivepack_ls(t, s); + break; + + case GIT_SERVICE_RECEIVEPACK: + ret = winhttp_receivepack(t, s); + break; + + default: + GIT_ASSERT(0); + } + + if (!ret) + *stream = &s->parent; + + return ret; +} + +static int winhttp_close(git_smart_subtransport *subtransport) +{ + winhttp_subtransport *t = (winhttp_subtransport *)subtransport; + + git_net_url_dispose(&t->server.url); + git_net_url_dispose(&t->proxy.url); + + if (t->server.cred) { + t->server.cred->free(t->server.cred); + t->server.cred = NULL; + } + + if (t->proxy.cred) { + t->proxy.cred->free(t->proxy.cred); + t->proxy.cred = NULL; + } + + return winhttp_close_connection(t); +} + +static void winhttp_free(git_smart_subtransport *subtransport) +{ + winhttp_subtransport *t = (winhttp_subtransport *)subtransport; + + winhttp_close(subtransport); + + git__free(t); +} + +int git_smart_subtransport_http(git_smart_subtransport **out, git_transport *owner, void *param) +{ + winhttp_subtransport *t; + + GIT_UNUSED(param); + + if (!out) + return -1; + + t = git__calloc(1, sizeof(winhttp_subtransport)); + GIT_ERROR_CHECK_ALLOC(t); + + t->owner = (transport_smart *)owner; + t->parent.action = winhttp_action; + t->parent.close = winhttp_close; + t->parent.free = winhttp_free; + + *out = (git_smart_subtransport *) t; + return 0; +} + +#endif /* GIT_HTTPS_WINHTTP */ diff --git a/src/libgit2/tree-cache.c b/src/libgit2/tree-cache.c new file mode 100644 index 00000000000..f3c895f4cf8 --- /dev/null +++ b/src/libgit2/tree-cache.c @@ -0,0 +1,287 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "tree-cache.h" + +#include "pool.h" +#include "tree.h" + +static git_tree_cache *find_child( + const git_tree_cache *tree, const char *path, const char *end) +{ + size_t i, dirlen = end ? (size_t)(end - path) : strlen(path); + + for (i = 0; i < tree->children_count; ++i) { + git_tree_cache *child = tree->children[i]; + + if (child->namelen == dirlen && !memcmp(path, child->name, dirlen)) + return child; + } + + return NULL; +} + +void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path) +{ + const char *ptr = path, *end; + + if (tree == NULL) + return; + + tree->entry_count = -1; + + while (ptr != NULL) { + end = strchr(ptr, '/'); + + if (end == NULL) /* End of path */ + break; + + tree = find_child(tree, ptr, end); + if (tree == NULL) /* We don't have that tree */ + return; + + tree->entry_count = -1; + ptr = end + 1; + } +} + +const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path) +{ + const char *ptr = path, *end; + + if (tree == NULL) { + return NULL; + } + + while (1) { + end = strchr(ptr, '/'); + + tree = find_child(tree, ptr, end); + if (tree == NULL) /* Can't find it */ + return NULL; + + if (end == NULL || *end + 1 == '\0') + return tree; + + ptr = end + 1; + } +} + +static int read_tree_internal( + git_tree_cache **out, + const char **buffer_in, + const char *buffer_end, + git_oid_t oid_type, + git_pool *pool) +{ + git_tree_cache *tree = NULL; + const char *name_start, *buffer; + size_t oid_size = git_oid_size(oid_type); + int count; + + buffer = name_start = *buffer_in; + + if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL) + goto corrupted; + + if (++buffer >= buffer_end) + goto corrupted; + + if (git_tree_cache_new(&tree, name_start, oid_type, pool) < 0) + return -1; + + /* Blank-terminated ASCII decimal number of entries in this tree */ + if (git__strntol32(&count, buffer, buffer_end - buffer, &buffer, 10) < 0) + goto corrupted; + + tree->entry_count = count; + + if (*buffer != ' ' || ++buffer >= buffer_end) + goto corrupted; + + /* Number of children of the tree, newline-terminated */ + if (git__strntol32(&count, buffer, buffer_end - buffer, &buffer, 10) < 0 || count < 0) + goto corrupted; + + tree->children_count = count; + + if (*buffer != '\n' || ++buffer > buffer_end) + goto corrupted; + + /* The OID is only there if it's not invalidated */ + if (tree->entry_count >= 0) { + /* 160-bit SHA-1 for this tree and it's children */ + if (buffer + oid_size > buffer_end) + goto corrupted; + + git_oid_from_raw(&tree->oid, (const unsigned char *)buffer, oid_type); + buffer += oid_size; + } + + /* Parse children: */ + if (tree->children_count > 0) { + size_t i, bufsize; + + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&bufsize, tree->children_count, sizeof(git_tree_cache*)); + + tree->children = git_pool_malloc(pool, bufsize); + GIT_ERROR_CHECK_ALLOC(tree->children); + + memset(tree->children, 0x0, bufsize); + + for (i = 0; i < tree->children_count; ++i) { + if (read_tree_internal(&tree->children[i], &buffer, buffer_end, oid_type, pool) < 0) + goto corrupted; + } + } + + *buffer_in = buffer; + *out = tree; + return 0; + + corrupted: + git_error_set(GIT_ERROR_INDEX, "corrupted TREE extension in index"); + return -1; +} + +int git_tree_cache_read( + git_tree_cache **tree, + const char *buffer, + size_t buffer_size, + git_oid_t oid_type, + git_pool *pool) +{ + const char *buffer_end = buffer + buffer_size; + + if (read_tree_internal(tree, &buffer, buffer_end, oid_type, pool) < 0) + return -1; + + if (buffer < buffer_end) { + git_error_set(GIT_ERROR_INDEX, "corrupted TREE extension in index (unexpected trailing data)"); + return -1; + } + + return 0; +} + +static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_pool *pool) +{ + git_repository *repo; + size_t i, j, nentries, ntrees, alloc_size; + int error; + + repo = git_tree_owner(tree); + + git_oid_cpy(&cache->oid, git_tree_id(tree)); + nentries = git_tree_entrycount(tree); + + /* + * We make sure we know how many trees we need to allocate for + * so we don't have to realloc and change the pointers for the + * parents. + */ + ntrees = 0; + for (i = 0; i < nentries; i++) { + const git_tree_entry *entry; + + entry = git_tree_entry_byindex(tree, i); + if (git_tree_entry_filemode(entry) == GIT_FILEMODE_TREE) + ntrees++; + } + + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_size, ntrees, sizeof(git_tree_cache *)); + + cache->children_count = ntrees; + cache->children = git_pool_mallocz(pool, alloc_size); + GIT_ERROR_CHECK_ALLOC(cache->children); + + j = 0; + for (i = 0; i < nentries; i++) { + const git_tree_entry *entry; + git_tree *subtree; + + entry = git_tree_entry_byindex(tree, i); + if (git_tree_entry_filemode(entry) != GIT_FILEMODE_TREE) { + cache->entry_count++; + continue; + } + + if ((error = git_tree_cache_new(&cache->children[j], git_tree_entry_name(entry), cache->oid_type, pool)) < 0) + return error; + + if ((error = git_tree_lookup(&subtree, repo, git_tree_entry_id(entry))) < 0) + return error; + + error = read_tree_recursive(cache->children[j], subtree, pool); + git_tree_free(subtree); + cache->entry_count += cache->children[j]->entry_count; + j++; + + if (error < 0) + return error; + } + + return 0; +} + +int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_oid_t oid_type, git_pool *pool) +{ + int error; + git_tree_cache *cache; + + if ((error = git_tree_cache_new(&cache, "", oid_type, pool)) < 0) + return error; + + if ((error = read_tree_recursive(cache, tree, pool)) < 0) + return error; + + *out = cache; + return 0; +} + +int git_tree_cache_new(git_tree_cache **out, const char *name, git_oid_t oid_type, git_pool *pool) +{ + size_t name_len, alloc_size; + git_tree_cache *tree; + + name_len = strlen(name); + + GIT_ERROR_CHECK_ALLOC_ADD3(&alloc_size, sizeof(git_tree_cache), name_len, 1); + + tree = git_pool_malloc(pool, alloc_size); + GIT_ERROR_CHECK_ALLOC(tree); + + memset(tree, 0x0, sizeof(git_tree_cache)); + /* NUL-terminated tree name */ + tree->oid_type = oid_type; + tree->namelen = name_len; + memcpy(tree->name, name, name_len); + tree->name[name_len] = '\0'; + + *out = tree; + return 0; +} + +static void write_tree(git_str *out, git_tree_cache *tree) +{ + size_t i; + + git_str_printf(out, "%s%c%"PRIdZ" %"PRIuZ"\n", tree->name, 0, tree->entry_count, tree->children_count); + + if (tree->entry_count != -1) + git_str_put(out, (char *)&tree->oid.id, git_oid_size(tree->oid_type)); + + for (i = 0; i < tree->children_count; i++) + write_tree(out, tree->children[i]); +} + +int git_tree_cache_write(git_str *out, git_tree_cache *tree) +{ + write_tree(out, tree); + + return git_str_oom(out) ? -1 : 0; +} diff --git a/src/libgit2/tree-cache.h b/src/libgit2/tree-cache.h new file mode 100644 index 00000000000..e4a73f277e0 --- /dev/null +++ b/src/libgit2/tree-cache.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_tree_cache_h__ +#define INCLUDE_tree_cache_h__ + +#include "common.h" + +#include "pool.h" +#include "str.h" +#include "git2/oid.h" + +typedef struct git_tree_cache { + struct git_tree_cache **children; + size_t children_count; + + git_oid_t oid_type; + + ssize_t entry_count; + git_oid oid; + size_t namelen; + char name[GIT_FLEX_ARRAY]; +} git_tree_cache; + +int git_tree_cache_write(git_str *out, git_tree_cache *tree); +int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size, git_oid_t oid_type, git_pool *pool); +void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path); +const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path); +int git_tree_cache_new(git_tree_cache **out, const char *name, git_oid_t oid_type, git_pool *pool); +/** + * Read a tree as the root of the tree cache (like for `git read-tree`) + */ +int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_oid_t oid_type, git_pool *pool); +void git_tree_cache_free(git_tree_cache *tree); + +#endif diff --git a/src/libgit2/tree.c b/src/libgit2/tree.c new file mode 100644 index 00000000000..2c89d516e74 --- /dev/null +++ b/src/libgit2/tree.c @@ -0,0 +1,1338 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "tree.h" + +#include "commit.h" +#include "git2/repository.h" +#include "git2/object.h" +#include "futils.h" +#include "tree-cache.h" +#include "index.h" +#include "path.h" + +#define DEFAULT_TREE_SIZE 16 +#define MAX_FILEMODE_BYTES 6 + +#define TREE_ENTRY_CHECK_NAMELEN(n) \ + if (n > UINT16_MAX) { git_error_set(GIT_ERROR_INVALID, "tree entry path too long"); } + +GIT_HASHMAP_STR_FUNCTIONS(git_treebuilder_entrymap, GIT_HASHMAP_INLINE, git_tree_entry *); + +static bool valid_filemode(const int filemode) +{ + return (filemode == GIT_FILEMODE_TREE + || filemode == GIT_FILEMODE_BLOB + || filemode == GIT_FILEMODE_BLOB_EXECUTABLE + || filemode == GIT_FILEMODE_LINK + || filemode == GIT_FILEMODE_COMMIT); +} + +GIT_INLINE(git_filemode_t) normalize_filemode(git_filemode_t filemode) +{ + /* Tree bits set, but it's not a commit */ + if (GIT_MODE_TYPE(filemode) == GIT_FILEMODE_TREE) + return GIT_FILEMODE_TREE; + + /* If any of the x bits are set */ + if (GIT_PERMS_IS_EXEC(filemode)) + return GIT_FILEMODE_BLOB_EXECUTABLE; + + /* 16XXXX means commit */ + if (GIT_MODE_TYPE(filemode) == GIT_FILEMODE_COMMIT) + return GIT_FILEMODE_COMMIT; + + /* 12XXXX means symlink */ + if (GIT_MODE_TYPE(filemode) == GIT_FILEMODE_LINK) + return GIT_FILEMODE_LINK; + + /* Otherwise, return a blob */ + return GIT_FILEMODE_BLOB; +} + +static int valid_entry_name(git_repository *repo, const char *filename) +{ + return *filename != '\0' && + git_path_is_valid(repo, filename, 0, + GIT_FS_PATH_REJECT_TRAVERSAL | GIT_PATH_REJECT_DOT_GIT | GIT_FS_PATH_REJECT_SLASH); +} + +static int entry_sort_cmp(const void *a, const void *b) +{ + const git_tree_entry *e1 = (const git_tree_entry *)a; + const git_tree_entry *e2 = (const git_tree_entry *)b; + + return git_fs_path_cmp( + e1->filename, e1->filename_len, git_tree_entry__is_tree(e1), + e2->filename, e2->filename_len, git_tree_entry__is_tree(e2), + git__strncmp); +} + +int git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2) +{ + return entry_sort_cmp(e1, e2); +} + +/** + * Allocate a new self-contained entry, with enough space after it to + * store the filename and the id. + */ +static git_tree_entry *alloc_entry(const char *filename, size_t filename_len, const git_oid *id) +{ + git_tree_entry *entry = NULL; + char *filename_ptr; + size_t tree_len; + +#ifdef GIT_EXPERIMENTAL_SHA256 + size_t oid_size = git_oid_size(id->type); +#else + size_t oid_size = GIT_OID_SHA1_SIZE; +#endif + + TREE_ENTRY_CHECK_NAMELEN(filename_len); + + if (GIT_ADD_SIZET_OVERFLOW(&tree_len, sizeof(git_tree_entry), filename_len) || + GIT_ADD_SIZET_OVERFLOW(&tree_len, tree_len, 1) || + GIT_ADD_SIZET_OVERFLOW(&tree_len, tree_len, oid_size)) + return NULL; + + entry = git__calloc(1, tree_len); + if (!entry) + return NULL; + + filename_ptr = ((char *) entry) + sizeof(git_tree_entry); + memcpy(filename_ptr, filename, filename_len); + entry->filename = filename_ptr; + entry->filename_len = (uint16_t)filename_len; + + git_oid_cpy(&entry->oid, id); + + return entry; +} + +struct tree_key_search { + const char *filename; + uint16_t filename_len; +}; + +static int homing_search_cmp(const void *key, const void *array_member) +{ + const struct tree_key_search *ksearch = key; + const git_tree_entry *entry = array_member; + + const uint16_t len1 = ksearch->filename_len; + const uint16_t len2 = entry->filename_len; + + return memcmp( + ksearch->filename, + entry->filename, + len1 < len2 ? len1 : len2 + ); +} + +/* + * Search for an entry in a given tree. + * + * Note that this search is performed in two steps because + * of the way tree entries are sorted internally in git: + * + * Entries in a tree are not sorted alphabetically; two entries + * with the same root prefix will have different positions + * depending on whether they are folders (subtrees) or normal files. + * + * Consequently, it is not possible to find an entry on the tree + * with a binary search if you don't know whether the filename + * you're looking for is a folder or a normal file. + * + * To work around this, we first perform a homing binary search + * on the tree, using the minimal length root prefix of our filename. + * Once the comparisons for this homing search start becoming + * ambiguous because of folder vs file sorting, we look linearly + * around the area for our target file. + */ +static int tree_key_search( + size_t *at_pos, + const git_tree *tree, + const char *filename, + size_t filename_len) +{ + struct tree_key_search ksearch; + const git_tree_entry *entry; + size_t homing, i; + + TREE_ENTRY_CHECK_NAMELEN(filename_len); + + ksearch.filename = filename; + ksearch.filename_len = (uint16_t)filename_len; + + /* Initial homing search; find an entry on the tree with + * the same prefix as the filename we're looking for */ + + if (git_array_search(&homing, + tree->entries, &homing_search_cmp, &ksearch) < 0) + return GIT_ENOTFOUND; /* just a signal error; not passed back to user */ + + /* We found a common prefix. Look forward as long as + * there are entries that share the common prefix */ + for (i = homing; i < tree->entries.size; ++i) { + entry = git_array_get(tree->entries, i); + + if (homing_search_cmp(&ksearch, entry) < 0) + break; + + if (entry->filename_len == filename_len && + memcmp(filename, entry->filename, filename_len) == 0) { + if (at_pos) + *at_pos = i; + + return 0; + } + } + + /* If we haven't found our filename yet, look backwards + * too as long as we have entries with the same prefix */ + if (homing > 0) { + i = homing - 1; + + do { + entry = git_array_get(tree->entries, i); + + if (homing_search_cmp(&ksearch, entry) > 0) + break; + + if (entry->filename_len == filename_len && + memcmp(filename, entry->filename, filename_len) == 0) { + if (at_pos) + *at_pos = i; + + return 0; + } + } while (i-- > 0); + } + + /* The filename doesn't exist at all */ + return GIT_ENOTFOUND; +} + +void git_tree_entry_free(git_tree_entry *entry) +{ + if (entry == NULL) + return; + + git__free(entry); +} + +int git_tree_entry_dup(git_tree_entry **dest, const git_tree_entry *source) +{ + git_tree_entry *cpy; + + GIT_ASSERT_ARG(source); + + cpy = alloc_entry(source->filename, source->filename_len, &source->oid); + if (cpy == NULL) + return -1; + + cpy->attr = source->attr; + + *dest = cpy; + return 0; +} + +void git_tree__free(void *_tree) +{ + git_tree *tree = _tree; + + git_odb_object_free(tree->odb_obj); + git_array_clear(tree->entries); + git__free(tree); +} + +git_filemode_t git_tree_entry_filemode(const git_tree_entry *entry) +{ + return normalize_filemode(entry->attr); +} + +git_filemode_t git_tree_entry_filemode_raw(const git_tree_entry *entry) +{ + return entry->attr; +} + +const char *git_tree_entry_name(const git_tree_entry *entry) +{ + GIT_ASSERT_ARG_WITH_RETVAL(entry, NULL); + return entry->filename; +} + +const git_oid *git_tree_entry_id(const git_tree_entry *entry) +{ + GIT_ASSERT_ARG_WITH_RETVAL(entry, NULL); + return &entry->oid; +} + +git_object_t git_tree_entry_type(const git_tree_entry *entry) +{ + GIT_ASSERT_ARG_WITH_RETVAL(entry, GIT_OBJECT_INVALID); + + if (S_ISGITLINK(entry->attr)) + return GIT_OBJECT_COMMIT; + else if (S_ISDIR(entry->attr)) + return GIT_OBJECT_TREE; + else + return GIT_OBJECT_BLOB; +} + +int git_tree_entry_to_object( + git_object **object_out, + git_repository *repo, + const git_tree_entry *entry) +{ + GIT_ASSERT_ARG(entry); + GIT_ASSERT_ARG(object_out); + + return git_object_lookup(object_out, repo, &entry->oid, GIT_OBJECT_ANY); +} + +static const git_tree_entry *entry_fromname( + const git_tree *tree, const char *name, size_t name_len) +{ + size_t idx; + + if (tree_key_search(&idx, tree, name, name_len) < 0) + return NULL; + + return git_array_get(tree->entries, idx); +} + +const git_tree_entry *git_tree_entry_byname( + const git_tree *tree, const char *filename) +{ + GIT_ASSERT_ARG_WITH_RETVAL(tree, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(filename, NULL); + + return entry_fromname(tree, filename, strlen(filename)); +} + +const git_tree_entry *git_tree_entry_byindex( + const git_tree *tree, size_t idx) +{ + GIT_ASSERT_ARG_WITH_RETVAL(tree, NULL); + return git_array_get(tree->entries, idx); +} + +const git_tree_entry *git_tree_entry_byid( + const git_tree *tree, const git_oid *id) +{ + size_t i; + const git_tree_entry *e; + + GIT_ASSERT_ARG_WITH_RETVAL(tree, NULL); + + git_array_foreach(tree->entries, i, e) { + if (git_oid_equal(&e->oid, id)) + return e; + } + + return NULL; +} + +size_t git_tree_entrycount(const git_tree *tree) +{ + GIT_ASSERT_ARG_WITH_RETVAL(tree, 0); + return tree->entries.size; +} + +size_t git_treebuilder_entrycount(git_treebuilder *bld) +{ + GIT_ASSERT_ARG_WITH_RETVAL(bld, 0); + + return git_treebuilder_entrymap_size(&bld->map); +} + +GIT_INLINE(void) set_error(const char *str, const char *path) +{ + if (path) + git_error_set(GIT_ERROR_TREE, "%s - %s", str, path); + else + git_error_set(GIT_ERROR_TREE, "%s", str); +} + +static int tree_error(const char *str, const char *path) +{ + set_error(str, path); + return -1; +} + +static int tree_parse_error(const char *str, const char *path) +{ + set_error(str, path); + return GIT_EINVALID; +} + +static int parse_mode(uint16_t *mode_out, const char *buffer, size_t buffer_len, const char **buffer_out) +{ + int32_t mode; + int error; + + if (!buffer_len || git__isspace(*buffer)) + return -1; + + if ((error = git__strntol32(&mode, buffer, buffer_len, buffer_out, 8)) < 0) + return error; + + if (mode < 0 || (uint32_t)mode > UINT16_MAX) + return -1; + + *mode_out = mode; + + return 0; +} + +int git_tree__parse_raw(void *_tree, const char *data, size_t size, git_oid_t oid_type) +{ + git_tree *tree = _tree; + const char *buffer; + const char *buffer_end; + const long oid_size = (long)git_oid_size(oid_type); + + buffer = data; + buffer_end = buffer + size; + + tree->odb_obj = NULL; + git_array_init_to_size(tree->entries, DEFAULT_TREE_SIZE); + GIT_ERROR_CHECK_ARRAY(tree->entries); + + while (buffer < buffer_end) { + git_tree_entry *entry; + size_t filename_len; + const char *nul; + uint16_t attr; + + if (parse_mode(&attr, buffer, buffer_end - buffer, &buffer) < 0 || !buffer) + return tree_parse_error("failed to parse tree: can't parse filemode", NULL); + + if (buffer >= buffer_end || (*buffer++) != ' ') + return tree_parse_error("failed to parse tree: missing space after filemode", NULL); + + if ((nul = memchr(buffer, 0, buffer_end - buffer)) == NULL) + return tree_parse_error("failed to parse tree: object is corrupted", NULL); + + if ((filename_len = nul - buffer) == 0 || filename_len > UINT16_MAX) + return tree_parse_error("failed to parse tree: can't parse filename", NULL); + + if ((buffer_end - (nul + 1)) < (long)oid_size) + return tree_parse_error("failed to parse tree: can't parse OID", NULL); + + /* Allocate the entry */ + entry = git_array_alloc(tree->entries); + GIT_ERROR_CHECK_ALLOC(entry); + + entry->attr = attr; + entry->filename_len = (uint16_t)filename_len; + entry->filename = buffer; + buffer += filename_len + 1; + + git_oid_from_raw(&entry->oid, (unsigned char *)buffer, oid_type); + buffer += oid_size; + } + + return 0; +} + +int git_tree__parse(void *_tree, git_odb_object *odb_obj, git_oid_t oid_type) +{ + git_tree *tree = _tree; + const char *data = git_odb_object_data(odb_obj); + size_t size = git_odb_object_size(odb_obj); + int error; + + if ((error = git_tree__parse_raw(tree, data, size, oid_type)) < 0 || + (error = git_odb_object_dup(&tree->odb_obj, odb_obj)) < 0) + return error; + + return error; +} + +static size_t find_next_dir(const char *dirname, git_index *index, size_t start) +{ + size_t dirlen, i, entries = git_index_entrycount(index); + + dirlen = strlen(dirname); + for (i = start; i < entries; ++i) { + const git_index_entry *entry = git_index_get_byindex(index, i); + if (strlen(entry->path) < dirlen || + memcmp(entry->path, dirname, dirlen) || + (dirlen > 0 && entry->path[dirlen] != '/')) { + break; + } + } + + return i; +} + +static git_object_t otype_from_mode(git_filemode_t filemode) +{ + switch (filemode) { + case GIT_FILEMODE_TREE: + return GIT_OBJECT_TREE; + case GIT_FILEMODE_COMMIT: + return GIT_OBJECT_COMMIT; + default: + return GIT_OBJECT_BLOB; + } +} + +static int check_entry(git_repository *repo, const char *filename, const git_oid *id, git_filemode_t filemode) +{ + if (!valid_filemode(filemode)) + return tree_error("failed to insert entry: invalid filemode for file", filename); + + if (!valid_entry_name(repo, filename)) + return tree_error("failed to insert entry: invalid name for a tree entry", filename); + + if (git_oid_is_zero(id)) + return tree_error("failed to insert entry: invalid null OID", filename); + + if (filemode != GIT_FILEMODE_COMMIT && + !git_object__is_valid(repo, id, otype_from_mode(filemode))) + return tree_error("failed to insert entry: invalid object specified", filename); + + return 0; +} + +static int git_treebuilder__write_with_buffer( + git_oid *oid, + git_treebuilder *bld, + git_str *buf) +{ + int error = 0; + size_t i, entrycount; + git_odb *odb; + git_tree_entry *entry; + git_vector entries = GIT_VECTOR_INIT; + size_t oid_size = git_oid_size(bld->repo->oid_type); + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + + git_str_clear(buf); + + entrycount = git_treebuilder_entrymap_size(&bld->map); + + if ((error = git_vector_init(&entries, entrycount, entry_sort_cmp)) < 0) + goto out; + + if (buf->asize == 0 && + (error = git_str_grow(buf, entrycount * 72)) < 0) + goto out; + + while (git_treebuilder_entrymap_iterate(&iter, NULL, &entry, &bld->map) == 0) { + if ((error = git_vector_insert(&entries, entry)) < 0) + goto out; + } + + git_vector_sort(&entries); + + for (i = 0; i < entries.length && !error; ++i) { + entry = git_vector_get(&entries, i); + + git_str_printf(buf, "%o ", entry->attr); + git_str_put(buf, entry->filename, entry->filename_len + 1); + git_str_put(buf, (char *)entry->oid.id, oid_size); + + if (git_str_oom(buf)) { + error = -1; + goto out; + } + } + + if ((error = git_repository_odb__weakptr(&odb, bld->repo)) == 0) + error = git_odb_write(oid, odb, buf->ptr, buf->size, GIT_OBJECT_TREE); + +out: + git_vector_dispose(&entries); + + return error; +} + +static int append_entry( + git_treebuilder *bld, + const char *filename, + const git_oid *id, + git_filemode_t filemode, + bool validate) +{ + git_tree_entry *entry; + int error = 0; + + if (validate && ((error = check_entry(bld->repo, filename, id, filemode)) < 0)) + return error; + + entry = alloc_entry(filename, strlen(filename), id); + GIT_ERROR_CHECK_ALLOC(entry); + + entry->attr = (uint16_t)filemode; + + if ((error = git_treebuilder_entrymap_put(&bld->map, entry->filename, entry)) < 0) { + git_tree_entry_free(entry); + git_error_set(GIT_ERROR_TREE, "failed to append entry %s to the tree builder", filename); + return -1; + } + + return 0; +} + +static int write_tree( + git_oid *oid, + git_repository *repo, + git_index *index, + const char *dirname, + size_t start, + git_str *shared_buf) +{ + git_treebuilder *bld = NULL; + size_t i, entries = git_index_entrycount(index); + int error; + size_t dirname_len = strlen(dirname); + const git_tree_cache *cache; + + cache = git_tree_cache_get(index->tree, dirname); + if (cache != NULL && cache->entry_count >= 0){ + git_oid_cpy(oid, &cache->oid); + return (int)find_next_dir(dirname, index, start); + } + + if ((error = git_treebuilder_new(&bld, repo, NULL)) < 0 || bld == NULL) + return -1; + + /* + * This loop is unfortunate, but necessary. The index doesn't have + * any directories, so we need to handle that manually, and we + * need to keep track of the current position. + */ + for (i = start; i < entries; ++i) { + const git_index_entry *entry = git_index_get_byindex(index, i); + const char *filename, *next_slash; + + /* + * If we've left our (sub)tree, exit the loop and return. The + * first check is an early out (and security for the + * third). The second check is a simple prefix comparison. The + * third check catches situations where there is a directory + * win32/sys and a file win32mmap.c. Without it, the following + * code believes there is a file win32/mmap.c + */ + if (strlen(entry->path) < dirname_len || + memcmp(entry->path, dirname, dirname_len) || + (dirname_len > 0 && entry->path[dirname_len] != '/')) { + break; + } + + filename = entry->path + dirname_len; + if (*filename == '/') + filename++; + next_slash = strchr(filename, '/'); + if (next_slash) { + git_oid sub_oid; + int written; + char *subdir, *last_comp; + + subdir = git__strndup(entry->path, next_slash - entry->path); + GIT_ERROR_CHECK_ALLOC(subdir); + + /* Write out the subtree */ + written = write_tree(&sub_oid, repo, index, subdir, i, shared_buf); + if (written < 0) { + git__free(subdir); + goto on_error; + } else { + i = written - 1; /* -1 because of the loop increment */ + } + + /* + * We need to figure out what we want toinsert + * into this tree. If we're traversing + * deps/zlib/, then we only want to write + * 'zlib' into the tree. + */ + last_comp = strrchr(subdir, '/'); + if (last_comp) { + last_comp++; /* Get rid of the '/' */ + } else { + last_comp = subdir; + } + + error = append_entry(bld, last_comp, &sub_oid, S_IFDIR, true); + git__free(subdir); + if (error < 0) + goto on_error; + } else { + error = append_entry(bld, filename, &entry->id, entry->mode, true); + if (error < 0) + goto on_error; + } + } + + if (git_treebuilder__write_with_buffer(oid, bld, shared_buf) < 0) + goto on_error; + + git_treebuilder_free(bld); + return (int)i; + +on_error: + git_treebuilder_free(bld); + return -1; +} + +int git_tree__write_index( + git_oid *oid, git_index *index, git_repository *repo) +{ + int ret; + git_tree *tree; + git_str shared_buf = GIT_STR_INIT; + bool old_ignore_case = false; + + GIT_ASSERT_ARG(oid); + GIT_ASSERT_ARG(index); + GIT_ASSERT_ARG(repo); + + if (git_index_has_conflicts(index)) { + git_error_set(GIT_ERROR_INDEX, + "cannot create a tree from a not fully merged index."); + return GIT_EUNMERGED; + } + + if (index->tree != NULL && index->tree->entry_count >= 0) { + git_oid_cpy(oid, &index->tree->oid); + return 0; + } + + /* The tree cache didn't help us; we'll have to write + * out a tree. If the index is ignore_case, we must + * make it case-sensitive for the duration of the tree-write + * operation. */ + + if (index->ignore_case) { + old_ignore_case = true; + git_index__set_ignore_case(index, false); + } + + ret = write_tree(oid, repo, index, "", 0, &shared_buf); + git_str_dispose(&shared_buf); + + if (old_ignore_case) + git_index__set_ignore_case(index, true); + + index->tree = NULL; + + if (ret < 0) + return ret; + + git_pool_clear(&index->tree_pool); + + if ((ret = git_tree_lookup(&tree, repo, oid)) < 0) + return ret; + + /* Read the tree cache into the index */ + ret = git_tree_cache_read_tree(&index->tree, tree, index->oid_type, &index->tree_pool); + git_tree_free(tree); + + return ret; +} + +int git_treebuilder_new( + git_treebuilder **builder_p, + git_repository *repo, + const git_tree *source) +{ + git_treebuilder *bld; + size_t i; + + GIT_ASSERT_ARG(builder_p); + GIT_ASSERT_ARG(repo); + + bld = git__calloc(1, sizeof(git_treebuilder)); + GIT_ERROR_CHECK_ALLOC(bld); + + bld->repo = repo; + + if (source != NULL) { + git_tree_entry *entry_src; + + git_array_foreach(source->entries, i, entry_src) { + if (append_entry( + bld, entry_src->filename, + &entry_src->oid, + entry_src->attr, + false) < 0) + goto on_error; + } + } + + *builder_p = bld; + return 0; + +on_error: + git_treebuilder_free(bld); + return -1; +} + +int git_treebuilder_insert( + const git_tree_entry **entry_out, + git_treebuilder *bld, + const char *filename, + const git_oid *id, + git_filemode_t filemode) +{ + git_tree_entry *entry; + int error; + + GIT_ASSERT_ARG(bld); + GIT_ASSERT_ARG(id); + GIT_ASSERT_ARG(filename); + + if ((error = check_entry(bld->repo, filename, id, filemode)) < 0) + return error; + + if (git_treebuilder_entrymap_get(&entry, &bld->map, filename) == 0) { + git_oid_cpy(&entry->oid, id); + } else { + entry = alloc_entry(filename, strlen(filename), id); + GIT_ERROR_CHECK_ALLOC(entry); + + if (git_treebuilder_entrymap_put(&bld->map, entry->filename, entry) < 0) { + git_tree_entry_free(entry); + git_error_set(GIT_ERROR_TREE, "failed to insert %s", filename); + return -1; + } + } + + entry->attr = filemode; + + if (entry_out) + *entry_out = entry; + + return 0; +} + +static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filename) +{ + git_tree_entry *entry; + + GIT_ASSERT_ARG_WITH_RETVAL(bld, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(filename, NULL); + + if (git_treebuilder_entrymap_get(&entry, &bld->map, filename) != 0) + return NULL; + + return entry; +} + +const git_tree_entry *git_treebuilder_get(git_treebuilder *bld, const char *filename) +{ + return treebuilder_get(bld, filename); +} + +int git_treebuilder_remove(git_treebuilder *bld, const char *filename) +{ + git_tree_entry *entry = treebuilder_get(bld, filename); + + if (entry == NULL) + return tree_error("failed to remove entry: file isn't in the tree", filename); + + git_treebuilder_entrymap_remove(&bld->map, filename); + git_tree_entry_free(entry); + + return 0; +} + +int git_treebuilder_write(git_oid *oid, git_treebuilder *bld) +{ + GIT_ASSERT_ARG(oid); + GIT_ASSERT_ARG(bld); + + return git_treebuilder__write_with_buffer(oid, bld, &bld->write_cache); +} + +int git_treebuilder_filter( + git_treebuilder *bld, + git_treebuilder_filter_cb filter, + void *payload) +{ + const char *filename; + git_tree_entry *entry; + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + + GIT_ASSERT_ARG(bld); + GIT_ASSERT_ARG(filter); + + while (git_treebuilder_entrymap_iterate(&iter, &filename, &entry, &bld->map) == 0) { + if (filter(entry, payload)) { + git_treebuilder_entrymap_remove(&bld->map, filename); + git_tree_entry_free(entry); + } + } + + return 0; +} + +int git_treebuilder_clear(git_treebuilder *bld) +{ + git_tree_entry *e; + git_hashmap_iter_t iter = GIT_HASHMAP_ITER_INIT; + + GIT_ASSERT_ARG(bld); + + while (git_treebuilder_entrymap_iterate(&iter, NULL, &e, &bld->map) == 0) + git_tree_entry_free(e); + + git_treebuilder_entrymap_clear(&bld->map); + + return 0; +} + +void git_treebuilder_free(git_treebuilder *bld) +{ + if (bld == NULL) + return; + + git_str_dispose(&bld->write_cache); + git_treebuilder_clear(bld); + git_treebuilder_entrymap_dispose(&bld->map); + git__free(bld); +} + +static size_t subpath_len(const char *path) +{ + const char *slash_pos = strchr(path, '/'); + if (slash_pos == NULL) + return strlen(path); + + return slash_pos - path; +} + +int git_tree_entry_bypath( + git_tree_entry **entry_out, + const git_tree *root, + const char *path) +{ + int error = 0; + git_tree *subtree; + const git_tree_entry *entry; + size_t filename_len; + + /* Find how long is the current path component (i.e. + * the filename between two slashes */ + filename_len = subpath_len(path); + + if (filename_len == 0) { + git_error_set(GIT_ERROR_TREE, "invalid tree path given"); + return GIT_ENOTFOUND; + } + + entry = entry_fromname(root, path, filename_len); + + if (entry == NULL) { + git_error_set(GIT_ERROR_TREE, + "the path '%.*s' does not exist in the given tree", (int) filename_len, path); + return GIT_ENOTFOUND; + } + + switch (path[filename_len]) { + case '/': + /* If there are more components in the path... + * then this entry *must* be a tree */ + if (!git_tree_entry__is_tree(entry)) { + git_error_set(GIT_ERROR_TREE, + "the path '%.*s' exists but is not a tree", (int) filename_len, path); + return GIT_ENOTFOUND; + } + + /* If there's only a slash left in the path, we + * return the current entry; otherwise, we keep + * walking down the path */ + if (path[filename_len + 1] != '\0') + break; + /* fall through */ + case '\0': + /* If there are no more components in the path, return + * this entry */ + return git_tree_entry_dup(entry_out, entry); + } + + if (git_tree_lookup(&subtree, root->object.repo, &entry->oid) < 0) + return -1; + + error = git_tree_entry_bypath( + entry_out, + subtree, + path + filename_len + 1 + ); + + git_tree_free(subtree); + return error; +} + +static int tree_walk( + const git_tree *tree, + git_treewalk_cb callback, + git_str *path, + void *payload, + bool preorder) +{ + int error = 0; + size_t i; + const git_tree_entry *entry; + + git_array_foreach(tree->entries, i, entry) { + if (preorder) { + error = callback(path->ptr, entry, payload); + if (error < 0) { /* negative value stops iteration */ + git_error_set_after_callback_function(error, "git_tree_walk"); + break; + } + if (error > 0) { /* positive value skips this entry */ + error = 0; + continue; + } + } + + if (git_tree_entry__is_tree(entry)) { + git_tree *subtree; + size_t path_len = git_str_len(path); + + error = git_tree_lookup(&subtree, tree->object.repo, &entry->oid); + if (error < 0) + break; + + /* append the next entry to the path */ + git_str_puts(path, entry->filename); + git_str_putc(path, '/'); + + if (git_str_oom(path)) + error = -1; + else + error = tree_walk(subtree, callback, path, payload, preorder); + + git_tree_free(subtree); + if (error != 0) + break; + + git_str_truncate(path, path_len); + } + + if (!preorder) { + error = callback(path->ptr, entry, payload); + if (error < 0) { /* negative value stops iteration */ + git_error_set_after_callback_function(error, "git_tree_walk"); + break; + } + error = 0; + } + } + + return error; +} + +int git_tree_walk( + const git_tree *tree, + git_treewalk_mode mode, + git_treewalk_cb callback, + void *payload) +{ + int error = 0; + git_str root_path = GIT_STR_INIT; + + if (mode != GIT_TREEWALK_POST && mode != GIT_TREEWALK_PRE) { + git_error_set(GIT_ERROR_INVALID, "invalid walking mode for tree walk"); + return -1; + } + + error = tree_walk( + tree, callback, &root_path, payload, (mode == GIT_TREEWALK_PRE)); + + git_str_dispose(&root_path); + + return error; +} + +static int compare_entries(const void *_a, const void *_b) +{ + const git_tree_update *a = (git_tree_update *) _a; + const git_tree_update *b = (git_tree_update *) _b; + + return strcmp(a->path, b->path); +} + +static int on_dup_entry(void **old, void *new) +{ + GIT_UNUSED(old); GIT_UNUSED(new); + + git_error_set(GIT_ERROR_TREE, "duplicate entries given for update"); + return -1; +} + +/* + * We keep the previous tree and the new one at each level of the + * stack. When we leave a level we're done with that tree and we can + * write it out to the odb. + */ +typedef struct { + git_treebuilder *bld; + git_tree *tree; + char *name; +} tree_stack_entry; + +/** Count how many slashes (i.e. path components) there are in this string */ +GIT_INLINE(size_t) count_slashes(const char *path) +{ + size_t count = 0; + const char *slash; + + while ((slash = strchr(path, '/')) != NULL) { + count++; + path = slash + 1; + } + + return count; +} + +static bool next_component(git_str *out, const char *in) +{ + const char *slash = strchr(in, '/'); + + git_str_clear(out); + + if (slash) + git_str_put(out, in, slash - in); + + return !!slash; +} + +static int create_popped_tree(tree_stack_entry *current, tree_stack_entry *popped, git_str *component) +{ + int error; + git_oid new_tree; + + git_tree_free(popped->tree); + + /* If the tree would be empty, remove it from the one higher up */ + if (git_treebuilder_entrycount(popped->bld) == 0) { + git_treebuilder_free(popped->bld); + error = git_treebuilder_remove(current->bld, popped->name); + git__free(popped->name); + return error; + } + + error = git_treebuilder_write(&new_tree, popped->bld); + git_treebuilder_free(popped->bld); + + if (error < 0) { + git__free(popped->name); + return error; + } + + /* We've written out the tree, now we have to put the new value into its parent */ + git_str_clear(component); + git_str_puts(component, popped->name); + git__free(popped->name); + + GIT_ERROR_CHECK_ALLOC(component->ptr); + + /* Error out if this would create a D/F conflict in this update */ + if (current->tree) { + const git_tree_entry *to_replace; + to_replace = git_tree_entry_byname(current->tree, component->ptr); + if (to_replace && git_tree_entry_type(to_replace) != GIT_OBJECT_TREE) { + git_error_set(GIT_ERROR_TREE, "D/F conflict when updating tree"); + return -1; + } + } + + return git_treebuilder_insert(NULL, current->bld, component->ptr, &new_tree, GIT_FILEMODE_TREE); +} + +int git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseline, size_t nupdates, const git_tree_update *updates) +{ + git_array_t(tree_stack_entry) stack = GIT_ARRAY_INIT; + tree_stack_entry *root_elem; + git_vector entries; + int error; + size_t i; + git_str component = GIT_STR_INIT; + + if ((error = git_vector_init(&entries, nupdates, compare_entries)) < 0) + return error; + + /* Sort the entries for treversal */ + for (i = 0 ; i < nupdates; i++) { + if ((error = git_vector_insert_sorted(&entries, (void *) &updates[i], on_dup_entry)) < 0) + goto cleanup; + } + + root_elem = git_array_alloc(stack); + GIT_ERROR_CHECK_ALLOC(root_elem); + memset(root_elem, 0, sizeof(*root_elem)); + + if (baseline && (error = git_tree_dup(&root_elem->tree, baseline)) < 0) + goto cleanup; + + if ((error = git_treebuilder_new(&root_elem->bld, repo, root_elem->tree)) < 0) + goto cleanup; + + for (i = 0; i < nupdates; i++) { + const git_tree_update *last_update = i == 0 ? NULL : git_vector_get(&entries, i-1); + const git_tree_update *update = git_vector_get(&entries, i); + size_t common_prefix = 0, steps_up, j; + const char *path; + + /* Figure out how much we need to change from the previous tree */ + if (last_update) + common_prefix = git_fs_path_common_dirlen(last_update->path, update->path); + + /* + * The entries are sorted, so when we find we're no + * longer in the same directory, we need to abandon + * the old tree (steps up) and dive down to the next + * one. + */ + steps_up = last_update == NULL ? 0 : count_slashes(&last_update->path[common_prefix]); + + for (j = 0; j < steps_up; j++) { + tree_stack_entry *current, *popped = git_array_pop(stack); + GIT_ASSERT(popped); + + current = git_array_last(stack); + GIT_ASSERT(current); + + if ((error = create_popped_tree(current, popped, &component)) < 0) + goto cleanup; + } + + /* Now that we've created the trees we popped from the stack, let's go back down */ + path = &update->path[common_prefix]; + while (next_component(&component, path)) { + tree_stack_entry *last, *new_entry; + const git_tree_entry *entry; + + last = git_array_last(stack); + entry = last->tree ? git_tree_entry_byname(last->tree, component.ptr) : NULL; + if (!entry) + entry = treebuilder_get(last->bld, component.ptr); + + if (entry && git_tree_entry_type(entry) != GIT_OBJECT_TREE) { + git_error_set(GIT_ERROR_TREE, "D/F conflict when updating tree"); + error = -1; + goto cleanup; + } + + new_entry = git_array_alloc(stack); + GIT_ERROR_CHECK_ALLOC(new_entry); + memset(new_entry, 0, sizeof(*new_entry)); + + new_entry->tree = NULL; + if (entry && (error = git_tree_lookup(&new_entry->tree, repo, git_tree_entry_id(entry))) < 0) + goto cleanup; + + if ((error = git_treebuilder_new(&new_entry->bld, repo, new_entry->tree)) < 0) + goto cleanup; + + new_entry->name = git__strdup(component.ptr); + GIT_ERROR_CHECK_ALLOC(new_entry->name); + + /* Get to the start of the next component */ + path += component.size + 1; + } + + /* After all that, we're finally at the place where we want to perform the update */ + switch (update->action) { + case GIT_TREE_UPDATE_UPSERT: + { + /* Make sure we're replacing something of the same type */ + tree_stack_entry *last = git_array_last(stack); + char *basename = git_fs_path_basename(update->path); + const git_tree_entry *e = git_treebuilder_get(last->bld, basename); + if (e && git_tree_entry_type(e) != git_object__type_from_filemode(update->filemode)) { + git__free(basename); + git_error_set(GIT_ERROR_TREE, "cannot replace '%s' with '%s' at '%s'", + git_object_type2string(git_tree_entry_type(e)), + git_object_type2string(git_object__type_from_filemode(update->filemode)), + update->path); + error = -1; + goto cleanup; + } + + error = git_treebuilder_insert(NULL, last->bld, basename, &update->id, update->filemode); + git__free(basename); + break; + } + case GIT_TREE_UPDATE_REMOVE: + { + tree_stack_entry *last = git_array_last(stack); + char *basename = git_fs_path_basename(update->path); + error = git_treebuilder_remove(last->bld, basename); + git__free(basename); + break; + } + default: + git_error_set(GIT_ERROR_TREE, "unknown action for update"); + error = -1; + goto cleanup; + } + + if (error < 0) + goto cleanup; + } + + /* We're done, go up the stack again and write out the tree */ + { + tree_stack_entry *current = NULL, *popped = NULL; + while ((popped = git_array_pop(stack)) != NULL) { + current = git_array_last(stack); + /* We've reached the top, current is the root tree */ + if (!current) + break; + + if ((error = create_popped_tree(current, popped, &component)) < 0) + goto cleanup; + } + + /* Write out the root tree */ + git__free(popped->name); + git_tree_free(popped->tree); + + error = git_treebuilder_write(out, popped->bld); + git_treebuilder_free(popped->bld); + if (error < 0) + goto cleanup; + } + +cleanup: + { + tree_stack_entry *e; + while ((e = git_array_pop(stack)) != NULL) { + git_treebuilder_free(e->bld); + git_tree_free(e->tree); + git__free(e->name); + } + } + + git_str_dispose(&component); + git_array_clear(stack); + git_vector_dispose(&entries); + return error; +} + +/* Deprecated Functions */ + +#ifndef GIT_DEPRECATE_HARD + +int git_treebuilder_write_with_buffer(git_oid *oid, git_treebuilder *bld, git_buf *buf) +{ + GIT_UNUSED(buf); + + return git_treebuilder_write(oid, bld); +} + +#endif diff --git a/src/libgit2/tree.h b/src/libgit2/tree.h new file mode 100644 index 00000000000..8deec60408e --- /dev/null +++ b/src/libgit2/tree.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_tree_h__ +#define INCLUDE_tree_h__ + +#include "common.h" + +#include "git2/tree.h" +#include "repository.h" +#include "odb.h" +#include "vector.h" +#include "pool.h" + +struct git_tree_entry { + uint16_t attr; + uint16_t filename_len; + git_oid oid; + const char *filename; +}; + +struct git_tree { + git_object object; + git_odb_object *odb_obj; + git_array_t(git_tree_entry) entries; +}; + +GIT_HASHMAP_STR_STRUCT(git_treebuilder_entrymap, git_tree_entry *); + +struct git_treebuilder { + git_repository *repo; + git_treebuilder_entrymap map; + git_str write_cache; +}; + +GIT_INLINE(bool) git_tree_entry__is_tree(const struct git_tree_entry *e) +{ + return (S_ISDIR(e->attr) && !S_ISGITLINK(e->attr)); +} + +void git_tree__free(void *tree); +int git_tree__parse(void *tree, git_odb_object *obj, git_oid_t oid_type); +int git_tree__parse_raw(void *_tree, const char *data, size_t size, git_oid_t oid_type); + +/** + * Write a tree to the given repository + */ +int git_tree__write_index( + git_oid *oid, git_index *index, git_repository *repo); + +/** + * Obsolete mode kept for compatibility reasons + */ +#define GIT_FILEMODE_BLOB_GROUP_WRITABLE 0100664 + +#endif diff --git a/src/libgit2/userdiff.h b/src/libgit2/userdiff.h new file mode 100644 index 00000000000..c9a80d71251 --- /dev/null +++ b/src/libgit2/userdiff.h @@ -0,0 +1,210 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_userdiff_h__ +#define INCLUDE_userdiff_h__ + +#include "regexp.h" + +/* + * This file isolates the built in diff driver function name patterns. + * Most of these patterns are taken from Git (with permission from the + * original authors for relicensing to libgit2). + */ + +typedef struct { + const char *name; + const char *fns; + const char *words; + int flags; +} git_diff_driver_definition; + +#define WORD_DEFAULT "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" + +/* + * These builtin driver definition macros have same signature as in core + * git userdiff.c so that the data can be extracted verbatim + */ +#define PATTERNS(NAME, FN_PATS, WORD_PAT) \ + { NAME, FN_PATS, WORD_PAT WORD_DEFAULT, 0 } +#define IPATTERN(NAME, FN_PATS, WORD_PAT) \ + { NAME, FN_PATS, WORD_PAT WORD_DEFAULT, GIT_REGEXP_ICASE } + +/* + * The table of diff driver patterns + * + * Function name patterns are a list of newline separated patterns that + * match a function declaration (i.e. the line you want in the hunk header), + * or a negative pattern prefixed with a '!' to reject a pattern (such as + * rejecting goto labels in C code). + * + * Word boundary patterns are just a simple pattern that will be OR'ed with + * the default value above (i.e. whitespace or non-ASCII characters). + */ +static git_diff_driver_definition builtin_defs[] = { + +IPATTERN("ada", + "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n" + "!^[ \t]*with[ \t].*$\n" + "^[ \t]*((procedure|function)[ \t]+.*)$\n" + "^[ \t]*((package|protected|task)[ \t]+.*)$", + /* -- */ + "[a-zA-Z][a-zA-Z0-9_]*" + "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?" + "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"), + +IPATTERN("fortran", + "!^([C*]|[ \t]*!)\n" + "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n" + "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA" + "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$", + /* -- */ + "[a-zA-Z][a-zA-Z0-9_]*" + "|\\.([Ee][Qq]|[Nn][Ee]|[Gg][TtEe]|[Ll][TtEe]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|[Aa][Nn][Dd]|[Oo][Rr]|[Nn]?[Ee][Qq][Vv]|[Nn][Oo][Tt])\\." + /* numbers and format statements like 2E14.4, or ES12.6, 9X. + * Don't worry about format statements without leading digits since + * they would have been matched above as a variable anyway. */ + "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?" + "|//|\\*\\*|::|[/<>=]="), + +PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$", + "[^<>= \t]+"), + +PATTERNS("java", + "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n" + "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$", + /* -- */ + "[a-zA-Z_][a-zA-Z0-9_]*" + "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" + "|[-+*/<>%&^|=!]=" + "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"), + +PATTERNS("matlab", + "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$", + "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"), + +PATTERNS("objc", + /* Negate C statements that can look like functions */ + "!^[ \t]*(do|for|if|else|return|switch|while)\n" + /* Objective-C methods */ + "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n" + /* C functions */ + "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n" + /* Objective-C class/protocol definitions */ + "^(@(implementation|interface|protocol)[ \t].*)$", + /* -- */ + "[a-zA-Z_][a-zA-Z0-9_]*" + "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" + "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), + +PATTERNS("pascal", + "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|" + "implementation|initialization|finalization)[ \t]*.*)$" + "\n" + "^(.*=[ \t]*(class|record).*)$", + /* -- */ + "[a-zA-Z_][a-zA-Z0-9_]*" + "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" + "|<>|<=|>=|:=|\\.\\."), + +PATTERNS("perl", + "^package .*\n" + "^sub [[:alnum:]_':]+[ \t]*" + "(\\([^)]*\\)[ \t]*)?" /* prototype */ + /* + * Attributes. A regex can't count nested parentheses, + * so just slurp up whatever we see, taking care not + * to accept lines like "sub foo; # defined elsewhere". + * + * An attribute could contain a semicolon, but at that + * point it seems reasonable enough to give up. + */ + "(:[^;#]*)?" + "(\\{[ \t]*)?" /* brace can come here or on the next line */ + "(#.*)?$\n" /* comment */ + "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*" + "(\\{[ \t]*)?" /* brace can come here or on the next line */ + "(#.*)?$\n" + "^=head[0-9] .*", /* POD */ + /* -- */ + "[[:alpha:]_'][[:alnum:]_']*" + "|0[xb]?[0-9a-fA-F_]*" + /* taking care not to interpret 3..5 as (3.)(.5) */ + "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?" + "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::" + "|&&=|\\|\\|=|//=|\\*\\*=" + "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?" + "|[-+*/%.^&<>=!|]=" + "|=~|!~" + "|<<|<>|<=>|>>"), + +PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$", + /* -- */ + "[a-zA-Z_][a-zA-Z0-9_]*" + "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?" + "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"), + +PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$", + /* -- */ + "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*" + "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?." + "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"), + +PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$", + "[={}\"]|[^={}\" \t]+"), + +PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$", + "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"), + +PATTERNS("cpp", + /* Jump targets or access declarations */ + "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n" + /* functions/methods, variables, and compounds at top level */ + "^((::[[:space:]]*)?[A-Za-z_].*)$", + /* -- */ + "[a-zA-Z_][a-zA-Z0-9_]*" + "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*" + "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"), + +PATTERNS("csharp", + /* Keywords */ + "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n" + /* Methods and constructors */ + "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n" + /* Properties */ + "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n" + /* Type definitions */ + "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n" + /* Namespace */ + "^[ \t]*(namespace[ \t]+.*)$", + /* -- */ + "[a-zA-Z_][a-zA-Z0-9_]*" + "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" + "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), + +PATTERNS("php", + "^[ \t]*(((public|private|protected|static|final)[ \t]+)*((class|function)[ \t].*))$", + /* -- */ + "[a-zA-Z_][a-zA-Z0-9_]*" + "|[-+0-9.e]+[fFlL]?|0[xX]?[0-9a-fA-F]+[lL]?" + "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), + +PATTERNS("javascript", + "([a-zA-Z_$][a-zA-Z0-9_$]*(\\.[a-zA-Z0-9_$]+)*[ \t]*=[ \t]*function([ \t][a-zA-Z_$][a-zA-Z0-9_$]*)?[^\\{]*)\n" + "([a-zA-Z_$][a-zA-Z0-9_$]*[ \t]*:[ \t]*function([ \t][a-zA-Z_$][a-zA-Z0-9_$]*)?[^\\{]*)\n" + "[^a-zA-Z0-9_\\$](function([ \t][a-zA-Z_$][a-zA-Z0-9_$]*)?[^\\{]*)", + /* -- */ + "[a-zA-Z_][a-zA-Z0-9_]*" + "|[-+0-9.e]+[fFlL]?|0[xX]?[0-9a-fA-F]+[lL]?" + "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), +}; + +#undef IPATTERN +#undef PATTERNS +#undef WORD_DEFAULT + +#endif + diff --git a/src/libgit2/worktree.c b/src/libgit2/worktree.c new file mode 100644 index 00000000000..9d05a956750 --- /dev/null +++ b/src/libgit2/worktree.c @@ -0,0 +1,689 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "worktree.h" + +#include "buf.h" +#include "refdb.h" +#include "repository.h" +#include "path.h" + +#include "git2/branch.h" +#include "git2/commit.h" +#include "git2/worktree.h" +#include "git2/sys/refdb_backend.h" + +static bool is_worktree_dir(const char *dir) +{ + git_str buf = GIT_STR_INIT; + int error; + + if (git_str_sets(&buf, dir) < 0) + return -1; + + error = git_fs_path_contains_file(&buf, "commondir") + && git_fs_path_contains_file(&buf, "gitdir") + && git_fs_path_contains_file(&buf, "HEAD"); + + git_str_dispose(&buf); + return error; +} + +int git_worktree_list(git_strarray *wts, git_repository *repo) +{ + git_vector worktrees = GIT_VECTOR_INIT; + git_str path = GIT_STR_INIT; + char *worktree; + size_t i, len; + int error; + + GIT_ASSERT_ARG(wts); + GIT_ASSERT_ARG(repo); + + wts->count = 0; + wts->strings = NULL; + + if ((error = git_str_joinpath(&path, repo->commondir, "worktrees/")) < 0) + goto exit; + if (!git_fs_path_exists(path.ptr) || git_fs_path_is_empty_dir(path.ptr)) + goto exit; + if ((error = git_fs_path_dirload(&worktrees, path.ptr, path.size, 0x0)) < 0) + goto exit; + + len = path.size; + + git_vector_foreach(&worktrees, i, worktree) { + git_str_truncate(&path, len); + git_str_puts(&path, worktree); + + if (!is_worktree_dir(path.ptr)) { + git_vector_remove(&worktrees, i); + git__free(worktree); + } + } + + wts->strings = (char **)git_vector_detach(&wts->count, NULL, &worktrees); + +exit: + git_str_dispose(&path); + + return error; +} + +char *git_worktree__read_link(const char *base, const char *file) +{ + git_str path = GIT_STR_INIT, buf = GIT_STR_INIT; + + GIT_ASSERT_ARG_WITH_RETVAL(base, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(file, NULL); + + if (git_str_joinpath(&path, base, file) < 0) + goto err; + if (git_futils_readbuffer(&buf, path.ptr) < 0) + goto err; + git_str_dispose(&path); + + git_str_rtrim(&buf); + + if (!git_fs_path_is_relative(buf.ptr)) + return git_str_detach(&buf); + + if (git_str_sets(&path, base) < 0) + goto err; + if (git_fs_path_apply_relative(&path, buf.ptr) < 0) + goto err; + git_str_dispose(&buf); + + return git_str_detach(&path); + +err: + git_str_dispose(&buf); + git_str_dispose(&path); + + return NULL; +} + +static int write_wtfile(const char *base, const char *file, const git_str *buf) +{ + git_str path = GIT_STR_INIT; + int err; + + GIT_ASSERT_ARG(base); + GIT_ASSERT_ARG(file); + GIT_ASSERT_ARG(buf); + + if ((err = git_str_joinpath(&path, base, file)) < 0) + goto out; + + if ((err = git_futils_writebuffer(buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0) + goto out; + +out: + git_str_dispose(&path); + + return err; +} + +static int open_worktree_dir(git_worktree **out, const char *parent, const char *dir, const char *name) +{ + git_str gitdir = GIT_STR_INIT; + git_worktree *wt = NULL; + int error = 0; + + if (!is_worktree_dir(dir)) { + error = -1; + goto out; + } + + if ((error = git_path_validate_length(NULL, dir)) < 0) + goto out; + + if ((wt = git__calloc(1, sizeof(*wt))) == NULL) { + error = -1; + goto out; + } + + if ((wt->name = git__strdup(name)) == NULL || + (wt->commondir_path = git_worktree__read_link(dir, "commondir")) == NULL || + (wt->gitlink_path = git_worktree__read_link(dir, "gitdir")) == NULL || + (parent && (wt->parent_path = git__strdup(parent)) == NULL) || + (wt->worktree_path = git_fs_path_dirname(wt->gitlink_path)) == NULL) { + error = -1; + goto out; + } + + if ((error = git_fs_path_prettify_dir(&gitdir, dir, NULL)) < 0) + goto out; + wt->gitdir_path = git_str_detach(&gitdir); + + if ((error = git_worktree_is_locked(NULL, wt)) < 0) + goto out; + wt->locked = !!error; + error = 0; + + *out = wt; + +out: + if (error) + git_worktree_free(wt); + git_str_dispose(&gitdir); + + return error; +} + +int git_worktree_lookup(git_worktree **out, git_repository *repo, const char *name) +{ + git_str path = GIT_STR_INIT; + git_worktree *wt = NULL; + int error; + + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + + *out = NULL; + + if ((error = git_str_join3(&path, '/', repo->commondir, "worktrees", name)) < 0) + goto out; + + if (!git_fs_path_isdir(path.ptr)) { + error = GIT_ENOTFOUND; + goto out; + } + + if ((error = (open_worktree_dir(out, git_repository_workdir(repo), path.ptr, name))) < 0) + goto out; + +out: + git_str_dispose(&path); + + if (error) + git_worktree_free(wt); + + return error; +} + +int git_worktree_open_from_repository(git_worktree **out, git_repository *repo) +{ + git_str parent = GIT_STR_INIT; + const char *gitdir, *commondir; + char *name = NULL; + int error = 0; + + if (!git_repository_is_worktree(repo)) { + git_error_set(GIT_ERROR_WORKTREE, "cannot open worktree of a non-worktree repo"); + error = -1; + goto out; + } + + gitdir = git_repository_path(repo); + commondir = git_repository_commondir(repo); + + if ((error = git_fs_path_prettify_dir(&parent, "..", commondir)) < 0) + goto out; + + /* The name is defined by the last component in '.git/worktree/%s' */ + name = git_fs_path_basename(gitdir); + + if ((error = open_worktree_dir(out, parent.ptr, gitdir, name)) < 0) + goto out; + +out: + git__free(name); + git_str_dispose(&parent); + + return error; +} + +void git_worktree_free(git_worktree *wt) +{ + if (!wt) + return; + + git__free(wt->commondir_path); + git__free(wt->worktree_path); + git__free(wt->gitlink_path); + git__free(wt->gitdir_path); + git__free(wt->parent_path); + git__free(wt->name); + git__free(wt); +} + +int git_worktree_validate(const git_worktree *wt) +{ + GIT_ASSERT_ARG(wt); + + if (!is_worktree_dir(wt->gitdir_path)) { + git_error_set(GIT_ERROR_WORKTREE, + "worktree gitdir ('%s') is not valid", + wt->gitlink_path); + return GIT_ERROR; + } + + if (wt->parent_path && !git_fs_path_exists(wt->parent_path)) { + git_error_set(GIT_ERROR_WORKTREE, + "worktree parent directory ('%s') does not exist ", + wt->parent_path); + return GIT_ERROR; + } + + if (!git_fs_path_exists(wt->commondir_path)) { + git_error_set(GIT_ERROR_WORKTREE, + "worktree common directory ('%s') does not exist ", + wt->commondir_path); + return GIT_ERROR; + } + + if (!git_fs_path_exists(wt->worktree_path)) { + git_error_set(GIT_ERROR_WORKTREE, + "worktree directory '%s' does not exist", + wt->worktree_path); + return GIT_ERROR; + } + + return 0; +} + +int git_worktree_add_options_init(git_worktree_add_options *opts, + unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE(opts, version, + git_worktree_add_options, GIT_WORKTREE_ADD_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_worktree_add_init_options(git_worktree_add_options *opts, + unsigned int version) +{ + return git_worktree_add_options_init(opts, version); +} +#endif + +int git_worktree_add(git_worktree **out, git_repository *repo, + const char *name, const char *worktree, + const git_worktree_add_options *opts) +{ + git_str invalid_head_content = GIT_STR_INIT_CONST("ref: " GIT_INVALID_HEAD, + strlen("ref: " GIT_INVALID_HEAD)); + git_str gitdir = GIT_STR_INIT, wddir = GIT_STR_INIT, buf = GIT_STR_INIT; + git_reference *ref = NULL, *head = NULL, *wt_head = NULL; + git_commit *commit = NULL; + git_repository *wt = NULL; + git_checkout_options coopts; + git_worktree_add_options wtopts = GIT_WORKTREE_ADD_OPTIONS_INIT; + git_refdb *refdb; + int err; + + GIT_ERROR_CHECK_VERSION( + opts, GIT_WORKTREE_ADD_OPTIONS_VERSION, "git_worktree_add_options"); + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(repo); + GIT_ASSERT_ARG(name); + GIT_ASSERT_ARG(worktree); + + *out = NULL; + + if (opts) + memcpy(&wtopts, opts, sizeof(wtopts)); + + memcpy(&coopts, &wtopts.checkout_options, sizeof(coopts)); + + if (wtopts.ref) { + if (!git_reference_is_branch(wtopts.ref)) { + git_error_set(GIT_ERROR_WORKTREE, "reference is not a branch"); + err = -1; + goto out; + } + + if ((err = git_reference_dup(&ref, wtopts.ref)) < 0) + goto out; + } else if (wtopts.checkout_existing && git_branch_lookup(&ref, repo, name, GIT_BRANCH_LOCAL) == 0) { + /* Do nothing */ + } else if ((err = git_repository_head(&head, repo)) < 0 || + (err = git_commit_lookup(&commit, repo, &head->target.oid)) < 0 || + (err = git_branch_create(&ref, repo, name, commit, false)) < 0) { + goto out; + } + + if (git_branch_is_checked_out(ref)) { + git_error_set(GIT_ERROR_WORKTREE, "reference %s is already checked out", + git_reference_name(ref)); + err = -1; + goto out; + } + + /* Create gitdir directory ".git/worktrees/" */ + if ((err = git_str_joinpath(&gitdir, repo->commondir, "worktrees")) < 0) + goto out; + if (!git_fs_path_exists(gitdir.ptr)) + if ((err = git_futils_mkdir(gitdir.ptr, 0755, GIT_MKDIR_EXCL)) < 0) + goto out; + if ((err = git_str_joinpath(&gitdir, gitdir.ptr, name)) < 0) + goto out; + if ((err = git_futils_mkdir(gitdir.ptr, 0755, GIT_MKDIR_EXCL)) < 0) + goto out; + if ((err = git_fs_path_prettify_dir(&gitdir, gitdir.ptr, NULL)) < 0) + goto out; + + /* Create worktree work dir */ + if ((err = git_futils_mkdir(worktree, 0755, GIT_MKDIR_EXCL)) < 0) + goto out; + if ((err = git_fs_path_prettify_dir(&wddir, worktree, NULL)) < 0) + goto out; + + if (wtopts.lock) { + int fd; + + if ((err = git_str_joinpath(&buf, gitdir.ptr, "locked")) < 0) + goto out; + + if ((fd = p_creat(buf.ptr, 0644)) < 0) { + err = fd; + goto out; + } + + p_close(fd); + git_str_clear(&buf); + } + + /* Create worktree .git file */ + if ((err = git_str_printf(&buf, "gitdir: %s\n", gitdir.ptr)) < 0) + goto out; + if ((err = write_wtfile(wddir.ptr, ".git", &buf)) < 0) + goto out; + + /* Create gitdir files */ + if ((err = git_fs_path_prettify_dir(&buf, repo->commondir, NULL) < 0) + || (err = git_str_putc(&buf, '\n')) < 0 + || (err = write_wtfile(gitdir.ptr, "commondir", &buf)) < 0) + goto out; + if ((err = git_str_joinpath(&buf, wddir.ptr, ".git")) < 0 + || (err = git_str_putc(&buf, '\n')) < 0 + || (err = write_wtfile(gitdir.ptr, "gitdir", &buf)) < 0) + goto out; + + /* + * Create the ref skeleton that is required to make a directory look + * like a Git repository. These are required regardless of the ref + * format used. + */ + if ((err = git_str_joinpath(&buf, gitdir.ptr, GIT_HEAD_FILE)) < 0 || + (err = git_futils_writebuffer(&invalid_head_content, buf.ptr, 0, GIT_REFS_FILE_MODE)) < 0 || + (err = git_str_joinpath(&buf, gitdir.ptr, GIT_REFS_DIR)) < 0 || + (err = git_futils_mkdir(buf.ptr, 0755, GIT_MKDIR_EXCL)) < 0) + goto out; + + if ((err = git_repository_open(&wt, wddir.ptr)) < 0) + goto out; + + /* Initialize the worktree refdb and set up HEAD. */ + if ((err = git_repository_refdb__weakptr(&refdb, wt)) < 0 || + (err = git_refdb_init(refdb, git_reference_name(ref), 0777, + GIT_REFDB_BACKEND_INIT_IS_WORKTREE | GIT_REFDB_BACKEND_INIT_FORCE_HEAD)) < 0) + goto out; + + /* Checkout worktree's HEAD */ + if ((err = git_checkout_head(wt, &coopts)) < 0) + goto out; + + /* Load result */ + if ((err = git_worktree_lookup(out, repo, name)) < 0) + goto out; + +out: + git_str_dispose(&gitdir); + git_str_dispose(&wddir); + git_str_dispose(&buf); + git_reference_free(ref); + git_reference_free(head); + git_reference_free(wt_head); + git_commit_free(commit); + git_repository_free(wt); + + return err; +} + +int git_worktree_lock(git_worktree *wt, const char *reason) +{ + git_str buf = GIT_STR_INIT, path = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(wt); + + if ((error = git_worktree_is_locked(NULL, wt)) < 0) + goto out; + if (error) { + error = GIT_ELOCKED; + goto out; + } + + if ((error = git_str_joinpath(&path, wt->gitdir_path, "locked")) < 0) + goto out; + + if (reason) + git_str_attach_notowned(&buf, reason, strlen(reason)); + + if ((error = git_futils_writebuffer(&buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0) + goto out; + + wt->locked = 1; + +out: + git_str_dispose(&path); + + return error; +} + +int git_worktree_unlock(git_worktree *wt) +{ + git_str path = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(wt); + + if ((error = git_worktree_is_locked(NULL, wt)) < 0) + return error; + if (!error) + return 1; + + if (git_str_joinpath(&path, wt->gitdir_path, "locked") < 0) + return -1; + + if (p_unlink(path.ptr) != 0) { + git_str_dispose(&path); + return -1; + } + + wt->locked = 0; + + git_str_dispose(&path); + + return 0; +} + +static int git_worktree__is_locked(git_str *reason, const git_worktree *wt) +{ + git_str path = GIT_STR_INIT; + int error, locked; + + GIT_ASSERT_ARG(wt); + + if (reason) + git_str_clear(reason); + + if ((error = git_str_joinpath(&path, wt->gitdir_path, "locked")) < 0) + goto out; + locked = git_fs_path_exists(path.ptr); + if (locked && reason && + (error = git_futils_readbuffer(reason, path.ptr)) < 0) + goto out; + + error = locked; +out: + git_str_dispose(&path); + + return error; +} + +int git_worktree_is_locked(git_buf *reason, const git_worktree *wt) +{ + git_str str = GIT_STR_INIT; + int error = 0; + + if (reason && (error = git_buf_tostr(&str, reason)) < 0) + return error; + + error = git_worktree__is_locked(reason ? &str : NULL, wt); + + if (error >= 0 && reason) { + if (git_buf_fromstr(reason, &str) < 0) + error = -1; + } + + git_str_dispose(&str); + return error; +} + +const char *git_worktree_name(const git_worktree *wt) +{ + GIT_ASSERT_ARG_WITH_RETVAL(wt, NULL); + return wt->name; +} + +const char *git_worktree_path(const git_worktree *wt) +{ + GIT_ASSERT_ARG_WITH_RETVAL(wt, NULL); + return wt->worktree_path; +} + +int git_worktree_prune_options_init( + git_worktree_prune_options *opts, + unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE(opts, version, + git_worktree_prune_options, GIT_WORKTREE_PRUNE_OPTIONS_INIT); + return 0; +} + +#ifndef GIT_DEPRECATE_HARD +int git_worktree_prune_init_options(git_worktree_prune_options *opts, + unsigned int version) +{ + return git_worktree_prune_options_init(opts, version); +} +#endif + +int git_worktree_is_prunable(git_worktree *wt, + git_worktree_prune_options *opts) +{ + git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT; + git_str path = GIT_STR_INIT; + int ret = 0; + + GIT_ERROR_CHECK_VERSION( + opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION, + "git_worktree_prune_options"); + + if (opts) + memcpy(&popts, opts, sizeof(popts)); + + if ((popts.flags & GIT_WORKTREE_PRUNE_LOCKED) == 0) { + git_str reason = GIT_STR_INIT; + + if ((ret = git_worktree__is_locked(&reason, wt)) < 0) + goto out; + + if (ret) { + git_error_set(GIT_ERROR_WORKTREE, + "not pruning locked working tree: '%s'", + reason.size ? reason.ptr : "is locked"); + + git_str_dispose(&reason); + ret = 0; + goto out; + } + } + + if ((popts.flags & GIT_WORKTREE_PRUNE_VALID) == 0 && + git_worktree_validate(wt) == 0) { + git_error_set(GIT_ERROR_WORKTREE, "not pruning valid working tree"); + goto out; + } + + if ((ret = git_str_printf(&path, "%s/worktrees/%s", wt->commondir_path, wt->name) < 0)) + goto out; + + if (!git_fs_path_exists(path.ptr)) { + git_error_set(GIT_ERROR_WORKTREE, "worktree gitdir ('%s') does not exist", path.ptr); + goto out; + } + + ret = 1; + +out: + git_str_dispose(&path); + return ret; +} + +int git_worktree_prune(git_worktree *wt, + git_worktree_prune_options *opts) +{ + git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT; + git_str path = GIT_STR_INIT; + char *wtpath; + int err; + + GIT_ERROR_CHECK_VERSION( + opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION, + "git_worktree_prune_options"); + + if (opts) + memcpy(&popts, opts, sizeof(popts)); + + if (!git_worktree_is_prunable(wt, &popts)) { + err = -1; + goto out; + } + + /* Delete gitdir in parent repository */ + if ((err = git_str_join3(&path, '/', wt->commondir_path, "worktrees", wt->name)) < 0) + goto out; + if (!git_fs_path_exists(path.ptr)) + { + git_error_set(GIT_ERROR_WORKTREE, "worktree gitdir '%s' does not exist", path.ptr); + err = -1; + goto out; + } + if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0) + goto out; + + /* Skip deletion of the actual working tree if it does + * not exist or deletion was not requested */ + if ((popts.flags & GIT_WORKTREE_PRUNE_WORKING_TREE) == 0 || + !git_fs_path_exists(wt->gitlink_path)) + { + goto out; + } + + if ((wtpath = git_fs_path_dirname(wt->gitlink_path)) == NULL) + goto out; + git_str_attach(&path, wtpath, 0); + if (!git_fs_path_exists(path.ptr)) + { + git_error_set(GIT_ERROR_WORKTREE, "working tree '%s' does not exist", path.ptr); + err = -1; + goto out; + } + if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0) + goto out; + +out: + git_str_dispose(&path); + + return err; +} diff --git a/src/libgit2/worktree.h b/src/libgit2/worktree.h new file mode 100644 index 00000000000..587189f81a2 --- /dev/null +++ b/src/libgit2/worktree.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_worktree_h__ +#define INCLUDE_worktree_h__ + +#include "common.h" + +#include "git2/common.h" +#include "git2/worktree.h" + +struct git_worktree { + /* Name of the working tree. This is the name of the + * containing directory in the `$PARENT/.git/worktrees/` + * directory. */ + char *name; + + /* Path to the where the worktree lives in the filesystem */ + char *worktree_path; + /* Path to the .git file in the working tree's repository */ + char *gitlink_path; + /* Path to the .git directory inside the parent's + * worktrees directory */ + char *gitdir_path; + /* Path to the common directory contained in the parent + * repository */ + char *commondir_path; + /* Path to the parent's working directory */ + char *parent_path; + + unsigned int locked:1; +}; + +char *git_worktree__read_link(const char *base, const char *file); + +#endif diff --git a/src/merge.c b/src/merge.c deleted file mode 100644 index e0010d6a4d7..00000000000 --- a/src/merge.c +++ /dev/null @@ -1,296 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "repository.h" -#include "revwalk.h" -#include "buffer.h" -#include "merge.h" -#include "refs.h" -#include "git2/repository.h" -#include "git2/merge.h" -#include "git2/reset.h" -#include "commit_list.h" - -int git_repository_merge_cleanup(git_repository *repo) -{ - int error = 0; - git_buf merge_head_path = GIT_BUF_INIT, - merge_mode_path = GIT_BUF_INIT, - merge_msg_path = GIT_BUF_INIT; - - assert(repo); - - if (git_buf_joinpath(&merge_head_path, repo->path_repository, GIT_MERGE_HEAD_FILE) < 0 || - git_buf_joinpath(&merge_mode_path, repo->path_repository, GIT_MERGE_MODE_FILE) < 0 || - git_buf_joinpath(&merge_msg_path, repo->path_repository, GIT_MERGE_MSG_FILE) < 0) - return -1; - - if (git_path_isfile(merge_head_path.ptr)) { - if ((error = p_unlink(merge_head_path.ptr)) < 0) - goto cleanup; - } - - if (git_path_isfile(merge_mode_path.ptr)) - (void)p_unlink(merge_mode_path.ptr); - - if (git_path_isfile(merge_msg_path.ptr)) - (void)p_unlink(merge_msg_path.ptr); - -cleanup: - git_buf_free(&merge_msg_path); - git_buf_free(&merge_mode_path); - git_buf_free(&merge_head_path); - - return error; -} - -int git_merge_base_many(git_oid *out, git_repository *repo, const git_oid input_array[], size_t length) -{ - git_revwalk *walk; - git_vector list; - git_commit_list *result = NULL; - int error = -1; - unsigned int i; - git_commit_list_node *commit; - - assert(out && repo && input_array); - - if (length < 2) { - giterr_set(GITERR_INVALID, "At least two commits are required to find an ancestor. Provided 'length' was %u.", length); - return -1; - } - - if (git_vector_init(&list, length - 1, NULL) < 0) - return -1; - - if (git_revwalk_new(&walk, repo) < 0) - goto cleanup; - - for (i = 1; i < length; i++) { - commit = git_revwalk__commit_lookup(walk, &input_array[i]); - if (commit == NULL) - goto cleanup; - - git_vector_insert(&list, commit); - } - - commit = git_revwalk__commit_lookup(walk, &input_array[0]); - if (commit == NULL) - goto cleanup; - - if (git_merge__bases_many(&result, walk, commit, &list) < 0) - goto cleanup; - - if (!result) { - error = GIT_ENOTFOUND; - goto cleanup; - } - - git_oid_cpy(out, &result->item->oid); - - error = 0; - -cleanup: - git_commit_list_free(&result); - git_revwalk_free(walk); - git_vector_free(&list); - return error; -} - -int git_merge_base(git_oid *out, git_repository *repo, const git_oid *one, const git_oid *two) -{ - git_revwalk *walk; - git_vector list; - git_commit_list *result = NULL; - git_commit_list_node *commit; - void *contents[1]; - - if (git_revwalk_new(&walk, repo) < 0) - return -1; - - commit = git_revwalk__commit_lookup(walk, two); - if (commit == NULL) - goto on_error; - - /* This is just one value, so we can do it on the stack */ - memset(&list, 0x0, sizeof(git_vector)); - contents[0] = commit; - list.length = 1; - list.contents = contents; - - commit = git_revwalk__commit_lookup(walk, one); - if (commit == NULL) - goto on_error; - - if (git_merge__bases_many(&result, walk, commit, &list) < 0) - goto on_error; - - if (!result) { - git_revwalk_free(walk); - giterr_clear(); - return GIT_ENOTFOUND; - } - - git_oid_cpy(out, &result->item->oid); - git_commit_list_free(&result); - git_revwalk_free(walk); - - return 0; - -on_error: - git_revwalk_free(walk); - return -1; -} - -static int interesting(git_pqueue *list) -{ - unsigned int i; - /* element 0 isn't used - we need to start at 1 */ - for (i = 1; i < list->size; i++) { - git_commit_list_node *commit = list->d[i]; - if ((commit->flags & STALE) == 0) - return 1; - } - - return 0; -} - -int git_merge__bases_many(git_commit_list **out, git_revwalk *walk, git_commit_list_node *one, git_vector *twos) -{ - int error; - unsigned int i; - git_commit_list_node *two; - git_commit_list *result = NULL, *tmp = NULL; - git_pqueue list; - - /* if the commit is repeated, we have a our merge base already */ - git_vector_foreach(twos, i, two) { - if (one == two) - return git_commit_list_insert(one, out) ? 0 : -1; - } - - if (git_pqueue_init(&list, twos->length * 2, git_commit_list_time_cmp) < 0) - return -1; - - if (git_commit_list_parse(walk, one) < 0) - return -1; - - one->flags |= PARENT1; - if (git_pqueue_insert(&list, one) < 0) - return -1; - - git_vector_foreach(twos, i, two) { - git_commit_list_parse(walk, two); - two->flags |= PARENT2; - if (git_pqueue_insert(&list, two) < 0) - return -1; - } - - /* as long as there are non-STALE commits */ - while (interesting(&list)) { - git_commit_list_node *commit; - int flags; - - commit = git_pqueue_pop(&list); - - flags = commit->flags & (PARENT1 | PARENT2 | STALE); - if (flags == (PARENT1 | PARENT2)) { - if (!(commit->flags & RESULT)) { - commit->flags |= RESULT; - if (git_commit_list_insert(commit, &result) == NULL) - return -1; - } - /* we mark the parents of a merge stale */ - flags |= STALE; - } - - for (i = 0; i < commit->out_degree; i++) { - git_commit_list_node *p = commit->parents[i]; - if ((p->flags & flags) == flags) - continue; - - if ((error = git_commit_list_parse(walk, p)) < 0) - return error; - - p->flags |= flags; - if (git_pqueue_insert(&list, p) < 0) - return -1; - } - } - - git_pqueue_free(&list); - - /* filter out any stale commits in the results */ - tmp = result; - result = NULL; - - while (tmp) { - struct git_commit_list *next = tmp->next; - if (!(tmp->item->flags & STALE)) - if (git_commit_list_insert_by_date(tmp->item, &result) == NULL) - return -1; - - git__free(tmp); - tmp = next; - } - - *out = result; - return 0; -} - -int git_repository_mergehead_foreach(git_repository *repo, - git_repository_mergehead_foreach_cb cb, - void *payload) -{ - git_buf merge_head_path = GIT_BUF_INIT, merge_head_file = GIT_BUF_INIT; - char *buffer, *line; - size_t line_num = 1; - git_oid oid; - int error = 0; - - assert(repo && cb); - - if ((error = git_buf_joinpath(&merge_head_path, repo->path_repository, - GIT_MERGE_HEAD_FILE)) < 0) - return error; - - if ((error = git_futils_readbuffer(&merge_head_file, - git_buf_cstr(&merge_head_path))) < 0) - goto cleanup; - - buffer = merge_head_file.ptr; - - while ((line = git__strsep(&buffer, "\n")) != NULL) { - if (strlen(line) != GIT_OID_HEXSZ) { - giterr_set(GITERR_INVALID, "Unable to parse OID - invalid length"); - error = -1; - goto cleanup; - } - - if ((error = git_oid_fromstr(&oid, line)) < 0) - goto cleanup; - - if (cb(&oid, payload) < 0) { - error = GIT_EUSER; - goto cleanup; - } - - ++line_num; - } - - if (*buffer) { - giterr_set(GITERR_MERGE, "No EOL at line %d", line_num); - error = -1; - goto cleanup; - } - -cleanup: - git_buf_free(&merge_head_path); - git_buf_free(&merge_head_file); - - return error; -} diff --git a/src/merge.h b/src/merge.h deleted file mode 100644 index 22c644270da..00000000000 --- a/src/merge.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_merge_h__ -#define INCLUDE_merge_h__ - -#include "git2/types.h" -#include "git2/merge.h" -#include "commit_list.h" -#include "vector.h" - -#define GIT_MERGE_MSG_FILE "MERGE_MSG" -#define GIT_MERGE_MODE_FILE "MERGE_MODE" - -#define MERGE_CONFIG_FILE_MODE 0666 - -int git_merge__bases_many(git_commit_list **out, git_revwalk *walk, git_commit_list_node *one, git_vector *twos); - -#endif diff --git a/src/message.c b/src/message.c deleted file mode 100644 index 0eff426f208..00000000000 --- a/src/message.c +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "message.h" - -static size_t line_length_without_trailing_spaces(const char *line, size_t len) -{ - while (len) { - unsigned char c = line[len - 1]; - if (!git__isspace(c)) - break; - len--; - } - - return len; -} - -/* Greatly inspired from git.git "stripspace" */ -/* see https://github.com/git/git/blob/497215d8811ac7b8955693ceaad0899ecd894ed2/builtin/stripspace.c#L4-67 */ -int git_message__prettify(git_buf *message_out, const char *message, int strip_comments) -{ - const size_t message_len = strlen(message); - - int consecutive_empty_lines = 0; - size_t i, line_length, rtrimmed_line_length; - char *next_newline; - - for (i = 0; i < strlen(message); i += line_length) { - next_newline = memchr(message + i, '\n', message_len - i); - - if (next_newline != NULL) { - line_length = next_newline - (message + i) + 1; - } else { - line_length = message_len - i; - } - - if (strip_comments && line_length && message[i] == '#') - continue; - - rtrimmed_line_length = line_length_without_trailing_spaces(message + i, line_length); - - if (!rtrimmed_line_length) { - consecutive_empty_lines++; - continue; - } - - if (consecutive_empty_lines > 0 && message_out->size > 0) - git_buf_putc(message_out, '\n'); - - consecutive_empty_lines = 0; - git_buf_put(message_out, message + i, rtrimmed_line_length); - git_buf_putc(message_out, '\n'); - } - - return git_buf_oom(message_out) ? -1 : 0; -} - -int git_message_prettify(char *message_out, size_t buffer_size, const char *message, int strip_comments) -{ - git_buf buf = GIT_BUF_INIT; - ssize_t out_size = -1; - - if (message_out && buffer_size) - *message_out = '\0'; - - if (git_message__prettify(&buf, message, strip_comments) < 0) - goto done; - - if (message_out && buf.size + 1 > buffer_size) { /* +1 for NUL byte */ - giterr_set(GITERR_INVALID, "Buffer too short to hold the cleaned message"); - goto done; - } - - if (message_out) - git_buf_copy_cstr(message_out, buffer_size, &buf); - - out_size = buf.size + 1; - -done: - git_buf_free(&buf); - return (int)out_size; -} diff --git a/src/message.h b/src/message.h deleted file mode 100644 index 3c4b8dc4552..00000000000 --- a/src/message.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_message_h__ -#define INCLUDE_message_h__ - -#include "git2/message.h" -#include "buffer.h" - -int git_message__prettify(git_buf *message_out, const char *message, int strip_comments); - -#endif /* INCLUDE_message_h__ */ diff --git a/src/mwindow.c b/src/mwindow.c deleted file mode 100644 index cb2ef78b039..00000000000 --- a/src/mwindow.c +++ /dev/null @@ -1,319 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "mwindow.h" -#include "vector.h" -#include "fileops.h" -#include "map.h" -#include "global.h" - -#define DEFAULT_WINDOW_SIZE \ - (sizeof(void*) >= 8 \ - ? 1 * 1024 * 1024 * 1024 \ - : 32 * 1024 * 1024) - -#define DEFAULT_MAPPED_LIMIT \ - ((1024 * 1024) * (sizeof(void*) >= 8 ? 8192ULL : 256UL)) - -size_t git_mwindow__window_size = DEFAULT_WINDOW_SIZE; -size_t git_mwindow__mapped_limit = DEFAULT_MAPPED_LIMIT; - -/* Whenever you want to read or modify this, grab git__mwindow_mutex */ -static git_mwindow_ctl mem_ctl; - -/* - * Free all the windows in a sequence, typically because we're done - * with the file - */ -void git_mwindow_free_all(git_mwindow_file *mwf) -{ - git_mwindow_ctl *ctl = &mem_ctl; - unsigned int i; - - if (git_mutex_lock(&git__mwindow_mutex)) { - giterr_set(GITERR_THREAD, "unable to lock mwindow mutex"); - return; - } - - /* - * Remove these windows from the global list - */ - for (i = 0; i < ctl->windowfiles.length; ++i){ - if (git_vector_get(&ctl->windowfiles, i) == mwf) { - git_vector_remove(&ctl->windowfiles, i); - break; - } - } - - if (ctl->windowfiles.length == 0) { - git_vector_free(&ctl->windowfiles); - ctl->windowfiles.contents = NULL; - } - - while (mwf->windows) { - git_mwindow *w = mwf->windows; - assert(w->inuse_cnt == 0); - - ctl->mapped -= w->window_map.len; - ctl->open_windows--; - - git_futils_mmap_free(&w->window_map); - - mwf->windows = w->next; - git__free(w); - } - - git_mutex_unlock(&git__mwindow_mutex); -} - -/* - * Check if a window 'win' contains the address 'offset' - */ -int git_mwindow_contains(git_mwindow *win, git_off_t offset) -{ - git_off_t win_off = win->offset; - return win_off <= offset - && offset <= (git_off_t)(win_off + win->window_map.len); -} - -/* - * Find the least-recently-used window in a file - */ -static void git_mwindow_scan_lru( - git_mwindow_file *mwf, - git_mwindow **lru_w, - git_mwindow **lru_l) -{ - git_mwindow *w, *w_l; - - for (w_l = NULL, w = mwf->windows; w; w = w->next) { - if (!w->inuse_cnt) { - /* - * If the current one is more recent than the last one, - * store it in the output parameter. If lru_w is NULL, - * it's the first loop, so store it as well. - */ - if (!*lru_w || w->last_used < (*lru_w)->last_used) { - *lru_w = w; - *lru_l = w_l; - } - } - w_l = w; - } -} - -/* - * Close the least recently used window. You should check to see if - * the file descriptors need closing from time to time. Called under - * lock from new_window. - */ -static int git_mwindow_close_lru(git_mwindow_file *mwf) -{ - git_mwindow_ctl *ctl = &mem_ctl; - unsigned int i; - git_mwindow *lru_w = NULL, *lru_l = NULL, **list = &mwf->windows; - - /* FIXME: Does this give us any advantage? */ - if(mwf->windows) - git_mwindow_scan_lru(mwf, &lru_w, &lru_l); - - for (i = 0; i < ctl->windowfiles.length; ++i) { - git_mwindow *last = lru_w; - git_mwindow_file *cur = git_vector_get(&ctl->windowfiles, i); - git_mwindow_scan_lru(cur, &lru_w, &lru_l); - if (lru_w != last) - list = &cur->windows; - } - - if (!lru_w) { - giterr_set(GITERR_OS, "Failed to close memory window. Couldn't find LRU"); - return -1; - } - - ctl->mapped -= lru_w->window_map.len; - git_futils_mmap_free(&lru_w->window_map); - - if (lru_l) - lru_l->next = lru_w->next; - else - *list = lru_w->next; - - git__free(lru_w); - ctl->open_windows--; - - return 0; -} - -/* This gets called under lock from git_mwindow_open */ -static git_mwindow *new_window( - git_mwindow_file *mwf, - git_file fd, - git_off_t size, - git_off_t offset) -{ - git_mwindow_ctl *ctl = &mem_ctl; - size_t walign = git_mwindow__window_size / 2; - git_off_t len; - git_mwindow *w; - - w = git__malloc(sizeof(*w)); - - if (w == NULL) - return NULL; - - memset(w, 0x0, sizeof(*w)); - w->offset = (offset / walign) * walign; - - len = size - w->offset; - if (len > (git_off_t)git_mwindow__window_size) - len = (git_off_t)git_mwindow__window_size; - - ctl->mapped += (size_t)len; - - while (git_mwindow__mapped_limit < ctl->mapped && - git_mwindow_close_lru(mwf) == 0) /* nop */; - - /* - * We treat `mapped_limit` as a soft limit. If we can't find a - * window to close and are above the limit, we still mmap the new - * window. - */ - - if (git_futils_mmap_ro(&w->window_map, fd, w->offset, (size_t)len) < 0) { - git__free(w); - return NULL; - } - - ctl->mmap_calls++; - ctl->open_windows++; - - if (ctl->mapped > ctl->peak_mapped) - ctl->peak_mapped = ctl->mapped; - - if (ctl->open_windows > ctl->peak_open_windows) - ctl->peak_open_windows = ctl->open_windows; - - return w; -} - -/* - * Open a new window, closing the least recenty used until we have - * enough space. Don't forget to add it to your list - */ -unsigned char *git_mwindow_open( - git_mwindow_file *mwf, - git_mwindow **cursor, - git_off_t offset, - size_t extra, - unsigned int *left) -{ - git_mwindow_ctl *ctl = &mem_ctl; - git_mwindow *w = *cursor; - - if (git_mutex_lock(&git__mwindow_mutex)) { - giterr_set(GITERR_THREAD, "unable to lock mwindow mutex"); - return NULL; - } - - if (!w || !(git_mwindow_contains(w, offset) && git_mwindow_contains(w, offset + extra))) { - if (w) { - w->inuse_cnt--; - } - - for (w = mwf->windows; w; w = w->next) { - if (git_mwindow_contains(w, offset) && - git_mwindow_contains(w, offset + extra)) - break; - } - - /* - * If there isn't a suitable window, we need to create a new - * one. - */ - if (!w) { - w = new_window(mwf, mwf->fd, mwf->size, offset); - if (w == NULL) { - git_mutex_unlock(&git__mwindow_mutex); - return NULL; - } - w->next = mwf->windows; - mwf->windows = w; - } - } - - /* If we changed w, store it in the cursor */ - if (w != *cursor) { - w->last_used = ctl->used_ctr++; - w->inuse_cnt++; - *cursor = w; - } - - offset -= w->offset; - - if (left) - *left = (unsigned int)(w->window_map.len - offset); - - git_mutex_unlock(&git__mwindow_mutex); - return (unsigned char *) w->window_map.data + offset; -} - -int git_mwindow_file_register(git_mwindow_file *mwf) -{ - git_mwindow_ctl *ctl = &mem_ctl; - int ret; - - if (git_mutex_lock(&git__mwindow_mutex)) { - giterr_set(GITERR_THREAD, "unable to lock mwindow mutex"); - return -1; - } - - if (ctl->windowfiles.length == 0 && - git_vector_init(&ctl->windowfiles, 8, NULL) < 0) { - git_mutex_unlock(&git__mwindow_mutex); - return -1; - } - - ret = git_vector_insert(&ctl->windowfiles, mwf); - git_mutex_unlock(&git__mwindow_mutex); - - return ret; -} - -void git_mwindow_file_deregister(git_mwindow_file *mwf) -{ - git_mwindow_ctl *ctl = &mem_ctl; - git_mwindow_file *cur; - unsigned int i; - - if (git_mutex_lock(&git__mwindow_mutex)) - return; - - git_vector_foreach(&ctl->windowfiles, i, cur) { - if (cur == mwf) { - git_vector_remove(&ctl->windowfiles, i); - git_mutex_unlock(&git__mwindow_mutex); - return; - } - } - git_mutex_unlock(&git__mwindow_mutex); -} - -void git_mwindow_close(git_mwindow **window) -{ - git_mwindow *w = *window; - if (w) { - if (git_mutex_lock(&git__mwindow_mutex)) { - giterr_set(GITERR_THREAD, "unable to lock mwindow mutex"); - return; - } - - w->inuse_cnt--; - git_mutex_unlock(&git__mwindow_mutex); - *window = NULL; - } -} diff --git a/src/mwindow.h b/src/mwindow.h deleted file mode 100644 index 0018ebbf047..00000000000 --- a/src/mwindow.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#ifndef INCLUDE_mwindow__ -#define INCLUDE_mwindow__ - -#include "map.h" -#include "vector.h" - -typedef struct git_mwindow { - struct git_mwindow *next; - git_map window_map; - git_off_t offset; - size_t last_used; - size_t inuse_cnt; -} git_mwindow; - -typedef struct git_mwindow_file { - git_mwindow *windows; - int fd; - git_off_t size; -} git_mwindow_file; - -typedef struct git_mwindow_ctl { - size_t mapped; - unsigned int open_windows; - unsigned int mmap_calls; - unsigned int peak_open_windows; - size_t peak_mapped; - size_t used_ctr; - git_vector windowfiles; -} git_mwindow_ctl; - -int git_mwindow_contains(git_mwindow *win, git_off_t offset); -void git_mwindow_free_all(git_mwindow_file *mwf); -unsigned char *git_mwindow_open(git_mwindow_file *mwf, git_mwindow **cursor, git_off_t offset, size_t extra, unsigned int *left); -int git_mwindow_file_register(git_mwindow_file *mwf); -void git_mwindow_file_deregister(git_mwindow_file *mwf); -void git_mwindow_close(git_mwindow **w_cursor); - -#endif diff --git a/src/netops.c b/src/netops.c deleted file mode 100644 index fd83a1cc329..00000000000 --- a/src/netops.c +++ /dev/null @@ -1,604 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef _WIN32 -# include -# include -# include -# include -# include -# include -# include -#else -# include -# ifdef _MSC_VER -# pragma comment(lib, "ws2_32") -# endif -#endif - -#ifdef __FreeBSD__ -# include -#endif - -#ifdef GIT_SSL -# include -# include -# include -#endif - -#include -#include "git2/errors.h" - -#include "common.h" -#include "netops.h" -#include "posix.h" -#include "buffer.h" - -#ifdef GIT_WIN32 -static void net_set_error(const char *str) -{ - int error = WSAGetLastError(); - char * win32_error = git_win32_get_error_message(error); - - if (win32_error) { - giterr_set(GITERR_NET, "%s: %s", str, win32_error); - git__free(win32_error); - } else { - giterr_set(GITERR_NET, str); - } -} -#else -static void net_set_error(const char *str) -{ - giterr_set(GITERR_NET, "%s: %s", str, strerror(errno)); -} -#endif - -#ifdef GIT_SSL -static int ssl_set_error(gitno_ssl *ssl, int error) -{ - int err; - unsigned long e; - - err = SSL_get_error(ssl->ssl, error); - - assert(err != SSL_ERROR_WANT_READ); - assert(err != SSL_ERROR_WANT_WRITE); - - switch (err) { - case SSL_ERROR_WANT_CONNECT: - case SSL_ERROR_WANT_ACCEPT: - giterr_set(GITERR_NET, "SSL error: connection failure\n"); - break; - case SSL_ERROR_WANT_X509_LOOKUP: - giterr_set(GITERR_NET, "SSL error: x509 error\n"); - break; - case SSL_ERROR_SYSCALL: - e = ERR_get_error(); - if (e > 0) { - giterr_set(GITERR_NET, "SSL error: %s", - ERR_error_string(e, NULL)); - break; - } else if (error < 0) { - giterr_set(GITERR_OS, "SSL error: syscall failure"); - break; - } - giterr_set(GITERR_NET, "SSL error: received early EOF"); - break; - case SSL_ERROR_SSL: - e = ERR_get_error(); - giterr_set(GITERR_NET, "SSL error: %s", - ERR_error_string(e, NULL)); - break; - case SSL_ERROR_NONE: - case SSL_ERROR_ZERO_RETURN: - default: - giterr_set(GITERR_NET, "SSL error: unknown error"); - break; - } - return -1; -} -#endif - -int gitno_recv(gitno_buffer *buf) -{ - return buf->recv(buf); -} - -#ifdef GIT_SSL -static int gitno__recv_ssl(gitno_buffer *buf) -{ - int ret; - - do { - ret = SSL_read(buf->socket->ssl.ssl, buf->data + buf->offset, buf->len - buf->offset); - } while (SSL_get_error(buf->socket->ssl.ssl, ret) == SSL_ERROR_WANT_READ); - - if (ret < 0) { - net_set_error("Error receiving socket data"); - return -1; - } - - buf->offset += ret; - return ret; -} -#endif - -static int gitno__recv(gitno_buffer *buf) -{ - int ret; - - ret = p_recv(buf->socket->socket, buf->data + buf->offset, buf->len - buf->offset, 0); - if (ret < 0) { - net_set_error("Error receiving socket data"); - return -1; - } - - buf->offset += ret; - return ret; -} - -void gitno_buffer_setup_callback( - gitno_socket *socket, - gitno_buffer *buf, - char *data, - size_t len, - int (*recv)(gitno_buffer *buf), void *cb_data) -{ - memset(data, 0x0, len); - buf->data = data; - buf->len = len; - buf->offset = 0; - buf->socket = socket; - buf->recv = recv; - buf->cb_data = cb_data; -} - -void gitno_buffer_setup(gitno_socket *socket, gitno_buffer *buf, char *data, size_t len) -{ -#ifdef GIT_SSL - if (socket->ssl.ctx) { - gitno_buffer_setup_callback(socket, buf, data, len, gitno__recv_ssl, NULL); - return; - } -#endif - - gitno_buffer_setup_callback(socket, buf, data, len, gitno__recv, NULL); -} - -/* Consume up to ptr and move the rest of the buffer to the beginning */ -void gitno_consume(gitno_buffer *buf, const char *ptr) -{ - size_t consumed; - - assert(ptr - buf->data >= 0); - assert(ptr - buf->data <= (int) buf->len); - - consumed = ptr - buf->data; - - memmove(buf->data, ptr, buf->offset - consumed); - memset(buf->data + buf->offset, 0x0, buf->len - buf->offset); - buf->offset -= consumed; -} - -/* Consume const bytes and move the rest of the buffer to the beginning */ -void gitno_consume_n(gitno_buffer *buf, size_t cons) -{ - memmove(buf->data, buf->data + cons, buf->len - buf->offset); - memset(buf->data + cons, 0x0, buf->len - buf->offset); - buf->offset -= cons; -} - -#ifdef GIT_SSL - -static int gitno_ssl_teardown(gitno_ssl *ssl) -{ - int ret; - - ret = SSL_shutdown(ssl->ssl); - if (ret < 0) - ret = ssl_set_error(ssl, ret); - else - ret = 0; - - SSL_free(ssl->ssl); - SSL_CTX_free(ssl->ctx); - return ret; -} - -/* Match host names according to RFC 2818 rules */ -static int match_host(const char *pattern, const char *host) -{ - for (;;) { - char c = tolower(*pattern++); - - if (c == '\0') - return *host ? -1 : 0; - - if (c == '*') { - c = *pattern; - /* '*' at the end matches everything left */ - if (c == '\0') - return 0; - - /* - * We've found a pattern, so move towards the next matching - * char. The '.' is handled specially because wildcards aren't - * allowed to cross subdomains. - */ - - while(*host) { - char h = tolower(*host); - if (c == h) - return match_host(pattern, host++); - if (h == '.') - return match_host(pattern, host); - host++; - } - return -1; - } - - if (c != tolower(*host++)) - return -1; - } - - return -1; -} - -static int check_host_name(const char *name, const char *host) -{ - if (!strcasecmp(name, host)) - return 0; - - if (match_host(name, host) < 0) - return -1; - - return 0; -} - -static int verify_server_cert(gitno_ssl *ssl, const char *host) -{ - X509 *cert; - X509_NAME *peer_name; - ASN1_STRING *str; - unsigned char *peer_cn = NULL; - int matched = -1, type = GEN_DNS; - GENERAL_NAMES *alts; - struct in6_addr addr6; - struct in_addr addr4; - void *addr; - int i = -1,j; - - if (SSL_get_verify_result(ssl->ssl) != X509_V_OK) { - giterr_set(GITERR_SSL, "The SSL certificate is invalid"); - return -1; - } - - /* Try to parse the host as an IP address to see if it is */ - if (p_inet_pton(AF_INET, host, &addr4)) { - type = GEN_IPADD; - addr = &addr4; - } else { - if(p_inet_pton(AF_INET6, host, &addr6)) { - type = GEN_IPADD; - addr = &addr6; - } - } - - - cert = SSL_get_peer_certificate(ssl->ssl); - - /* Check the alternative names */ - alts = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); - if (alts) { - int num; - - num = sk_GENERAL_NAME_num(alts); - for (i = 0; i < num && matched != 1; i++) { - const GENERAL_NAME *gn = sk_GENERAL_NAME_value(alts, i); - const char *name = (char *) ASN1_STRING_data(gn->d.ia5); - size_t namelen = (size_t) ASN1_STRING_length(gn->d.ia5); - - /* Skip any names of a type we're not looking for */ - if (gn->type != type) - continue; - - if (type == GEN_DNS) { - /* If it contains embedded NULs, don't even try */ - if (memchr(name, '\0', namelen)) - continue; - - if (check_host_name(name, host) < 0) - matched = 0; - else - matched = 1; - } else if (type == GEN_IPADD) { - /* Here name isn't so much a name but a binary representation of the IP */ - matched = !!memcmp(name, addr, namelen); - } - } - } - GENERAL_NAMES_free(alts); - - if (matched == 0) - goto cert_fail; - - if (matched == 1) - return 0; - - /* If no alternative names are available, check the common name */ - peer_name = X509_get_subject_name(cert); - if (peer_name == NULL) - goto on_error; - - if (peer_name) { - /* Get the index of the last CN entry */ - while ((j = X509_NAME_get_index_by_NID(peer_name, NID_commonName, i)) >= 0) - i = j; - } - - if (i < 0) - goto on_error; - - str = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(peer_name, i)); - if (str == NULL) - goto on_error; - - /* Work around a bug in OpenSSL whereby ASN1_STRING_to_UTF8 fails if it's already in utf-8 */ - if (ASN1_STRING_type(str) == V_ASN1_UTF8STRING) { - int size = ASN1_STRING_length(str); - - if (size > 0) { - peer_cn = OPENSSL_malloc(size + 1); - GITERR_CHECK_ALLOC(peer_cn); - memcpy(peer_cn, ASN1_STRING_data(str), size); - peer_cn[size] = '\0'; - } - } else { - int size = ASN1_STRING_to_UTF8(&peer_cn, str); - GITERR_CHECK_ALLOC(peer_cn); - if (memchr(peer_cn, '\0', size)) - goto cert_fail; - } - - if (check_host_name((char *)peer_cn, host) < 0) - goto cert_fail; - - OPENSSL_free(peer_cn); - - return 0; - -on_error: - OPENSSL_free(peer_cn); - return ssl_set_error(ssl, 0); - -cert_fail: - OPENSSL_free(peer_cn); - giterr_set(GITERR_SSL, "Certificate host name check failed"); - return -1; -} - -static int ssl_setup(gitno_socket *socket, const char *host, int flags) -{ - int ret; - - SSL_library_init(); - SSL_load_error_strings(); - socket->ssl.ctx = SSL_CTX_new(SSLv23_method()); - if (socket->ssl.ctx == NULL) - return ssl_set_error(&socket->ssl, 0); - - SSL_CTX_set_mode(socket->ssl.ctx, SSL_MODE_AUTO_RETRY); - SSL_CTX_set_verify(socket->ssl.ctx, SSL_VERIFY_NONE, NULL); - if (!SSL_CTX_set_default_verify_paths(socket->ssl.ctx)) - return ssl_set_error(&socket->ssl, 0); - - socket->ssl.ssl = SSL_new(socket->ssl.ctx); - if (socket->ssl.ssl == NULL) - return ssl_set_error(&socket->ssl, 0); - - if((ret = SSL_set_fd(socket->ssl.ssl, socket->socket)) == 0) - return ssl_set_error(&socket->ssl, ret); - - if ((ret = SSL_connect(socket->ssl.ssl)) <= 0) - return ssl_set_error(&socket->ssl, ret); - - if (GITNO_CONNECT_SSL_NO_CHECK_CERT & flags) - return 0; - - return verify_server_cert(&socket->ssl, host); -} -#endif - -static int gitno__close(GIT_SOCKET s) -{ -#ifdef GIT_WIN32 - if (SOCKET_ERROR == closesocket(s)) - return -1; - - if (0 != WSACleanup()) { - giterr_set(GITERR_OS, "Winsock cleanup failed"); - return -1; - } - - return 0; -#else - return close(s); -#endif -} - -int gitno_connect(gitno_socket *s_out, const char *host, const char *port, int flags) -{ - struct addrinfo *info = NULL, *p; - struct addrinfo hints; - GIT_SOCKET s = INVALID_SOCKET; - int ret; - -#ifdef GIT_WIN32 - /* on win32, the WSA context needs to be initialized - * before any socket calls can be performed */ - WSADATA wsd; - - if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) { - giterr_set(GITERR_OS, "Winsock init failed"); - return -1; - } - - if (LOBYTE(wsd.wVersion) != 2 || HIBYTE(wsd.wVersion) != 2) { - WSACleanup(); - giterr_set(GITERR_OS, "Winsock init failed"); - return -1; - } -#endif - - /* Zero the socket structure provided */ - memset(s_out, 0x0, sizeof(gitno_socket)); - - memset(&hints, 0x0, sizeof(struct addrinfo)); - hints.ai_socktype = SOCK_STREAM; - hints.ai_family = AF_UNSPEC; - - if ((ret = p_getaddrinfo(host, port, &hints, &info)) < 0) { - giterr_set(GITERR_NET, - "Failed to resolve address for %s: %s", host, p_gai_strerror(ret)); - return -1; - } - - for (p = info; p != NULL; p = p->ai_next) { - s = socket(p->ai_family, p->ai_socktype, p->ai_protocol); - - if (s == INVALID_SOCKET) { - net_set_error("error creating socket"); - break; - } - - if (connect(s, p->ai_addr, (socklen_t)p->ai_addrlen) == 0) - break; - - /* If we can't connect, try the next one */ - gitno__close(s); - s = INVALID_SOCKET; - } - - /* Oops, we couldn't connect to any address */ - if (s == INVALID_SOCKET && p == NULL) { - giterr_set(GITERR_OS, "Failed to connect to %s", host); - p_freeaddrinfo(info); - return -1; - } - - s_out->socket = s; - p_freeaddrinfo(info); - -#ifdef GIT_SSL - if ((flags & GITNO_CONNECT_SSL) && ssl_setup(s_out, host, flags) < 0) - return -1; -#else - /* SSL is not supported */ - if (flags & GITNO_CONNECT_SSL) { - giterr_set(GITERR_OS, "SSL is not supported by this copy of libgit2."); - return -1; - } -#endif - - return 0; -} - -#ifdef GIT_SSL -static int gitno_send_ssl(gitno_ssl *ssl, const char *msg, size_t len, int flags) -{ - int ret; - size_t off = 0; - - GIT_UNUSED(flags); - - while (off < len) { - ret = SSL_write(ssl->ssl, msg + off, len - off); - if (ret <= 0 && ret != SSL_ERROR_WANT_WRITE) - return ssl_set_error(ssl, ret); - - off += ret; - } - - return off; -} -#endif - -int gitno_send(gitno_socket *socket, const char *msg, size_t len, int flags) -{ - int ret; - size_t off = 0; - -#ifdef GIT_SSL - if (socket->ssl.ctx) - return gitno_send_ssl(&socket->ssl, msg, len, flags); -#endif - - while (off < len) { - errno = 0; - ret = p_send(socket->socket, msg + off, len - off, flags); - if (ret < 0) { - net_set_error("Error sending data"); - return -1; - } - - off += ret; - } - - return (int)off; -} - -int gitno_close(gitno_socket *s) -{ -#ifdef GIT_SSL - if (s->ssl.ctx && - gitno_ssl_teardown(&s->ssl) < 0) - return -1; -#endif - - return gitno__close(s->socket); -} - -int gitno_select_in(gitno_buffer *buf, long int sec, long int usec) -{ - fd_set fds; - struct timeval tv; - - tv.tv_sec = sec; - tv.tv_usec = usec; - - FD_ZERO(&fds); - FD_SET(buf->socket->socket, &fds); - - /* The select(2) interface is silly */ - return select((int)buf->socket->socket + 1, &fds, NULL, NULL, &tv); -} - -int gitno_extract_host_and_port(char **host, char **port, const char *url, const char *default_port) -{ - char *colon, *slash, *delim; - - colon = strchr(url, ':'); - slash = strchr(url, '/'); - - if (slash == NULL) { - giterr_set(GITERR_NET, "Malformed URL: missing /"); - return -1; - } - - if (colon == NULL) { - *port = git__strdup(default_port); - } else { - *port = git__strndup(colon + 1, slash - colon - 1); - } - GITERR_CHECK_ALLOC(*port); - - delim = colon == NULL ? slash : colon; - *host = git__strndup(url, delim - url); - GITERR_CHECK_ALLOC(*host); - - return 0; -} diff --git a/src/netops.h b/src/netops.h deleted file mode 100644 index f8ff42c40f5..00000000000 --- a/src/netops.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_netops_h__ -#define INCLUDE_netops_h__ - -#include "posix.h" -#include "common.h" - -#ifdef GIT_SSL -# include -#endif - -struct gitno_ssl { -#ifdef GIT_SSL - SSL_CTX *ctx; - SSL *ssl; -#else - size_t dummy; -#endif -}; - -typedef struct gitno_ssl gitno_ssl; - -/* Represents a socket that may or may not be using SSL */ -struct gitno_socket { - GIT_SOCKET socket; - gitno_ssl ssl; -}; - -typedef struct gitno_socket gitno_socket; - -struct gitno_buffer { - char *data; - size_t len; - size_t offset; - gitno_socket *socket; - int (*recv)(struct gitno_buffer *buffer); - void *cb_data; -}; - -typedef struct gitno_buffer gitno_buffer; - -/* Flags to gitno_connect */ -enum { - /* Attempt to create an SSL connection. */ - GITNO_CONNECT_SSL = 1, - - /* Valid only when GITNO_CONNECT_SSL is also specified. - * Indicates that the server certificate should not be validated. */ - GITNO_CONNECT_SSL_NO_CHECK_CERT = 2, -}; - -void gitno_buffer_setup(gitno_socket *t, gitno_buffer *buf, char *data, size_t len); -void gitno_buffer_setup_callback(gitno_socket *t, gitno_buffer *buf, char *data, size_t len, int (*recv)(gitno_buffer *buf), void *cb_data); -int gitno_recv(gitno_buffer *buf); - -void gitno_consume(gitno_buffer *buf, const char *ptr); -void gitno_consume_n(gitno_buffer *buf, size_t cons); - -int gitno_connect(gitno_socket *socket, const char *host, const char *port, int flags); -int gitno_send(gitno_socket *socket, const char *msg, size_t len, int flags); -int gitno_close(gitno_socket *s); -int gitno_select_in(gitno_buffer *buf, long int sec, long int usec); - -int gitno_extract_host_and_port(char **host, char **port, const char *url, const char *default_port); - -#endif diff --git a/src/notes.c b/src/notes.c deleted file mode 100644 index eff80bc8261..00000000000 --- a/src/notes.c +++ /dev/null @@ -1,614 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "notes.h" - -#include "git2.h" -#include "refs.h" -#include "config.h" -#include "iterator.h" -#include "signature.h" - -static int find_subtree_in_current_level( - git_tree **out, - git_repository *repo, - git_tree *parent, - const char *annotated_object_sha, - int fanout) -{ - size_t i; - const git_tree_entry *entry; - - *out = NULL; - - if (parent == NULL) - return GIT_ENOTFOUND; - - for (i = 0; i < git_tree_entrycount(parent); i++) { - entry = git_tree_entry_byindex(parent, i); - - if (!git__ishex(git_tree_entry_name(entry))) - continue; - - if (S_ISDIR(git_tree_entry_filemode(entry)) - && strlen(git_tree_entry_name(entry)) == 2 - && !strncmp(git_tree_entry_name(entry), annotated_object_sha + fanout, 2)) - return git_tree_lookup(out, repo, git_tree_entry_id(entry)); - - /* Not a DIR, so do we have an already existing blob? */ - if (!strcmp(git_tree_entry_name(entry), annotated_object_sha + fanout)) - return GIT_EEXISTS; - } - - return GIT_ENOTFOUND; -} - -static int find_subtree_r(git_tree **out, git_tree *root, - git_repository *repo, const char *target, int *fanout) -{ - int error; - git_tree *subtree = NULL; - - *out = NULL; - - error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout); - if (error == GIT_EEXISTS) { - return git_tree_lookup(out, repo, git_tree_id(root)); - } - - if (error < 0) - return error; - - *fanout += 2; - error = find_subtree_r(out, subtree, repo, target, fanout); - git_tree_free(subtree); - - return error; -} - -static int find_blob(git_oid *blob, git_tree *tree, const char *target) -{ - size_t i; - const git_tree_entry *entry; - - for (i=0; ioid, note_oid); - note->message = git__strdup((char *)git_blob_rawcontent(blob)); - GITERR_CHECK_ALLOC(note->message); - - *out = note; - - return 0; -} - -static int note_lookup(git_note **out, git_repository *repo, - git_tree *tree, const char *target) -{ - int error, fanout = 0; - git_oid oid; - git_blob *blob = NULL; - git_note *note = NULL; - git_tree *subtree = NULL; - - if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0) - goto cleanup; - - if ((error = find_blob(&oid, subtree, target + fanout)) < 0) - goto cleanup; - - if ((error = git_blob_lookup(&blob, repo, &oid)) < 0) - goto cleanup; - - if ((error = note_new(¬e, &oid, blob)) < 0) - goto cleanup; - - *out = note; - -cleanup: - git_tree_free(subtree); - git_blob_free(blob); - return error; -} - -static int note_remove(git_repository *repo, - const git_signature *author, const git_signature *committer, - const char *notes_ref, git_tree *tree, - const char *target, git_commit **parents) -{ - int error; - git_tree *tree_after_removal = NULL; - git_oid oid; - - if ((error = manipulate_note_in_tree_r( - &tree_after_removal, repo, tree, NULL, target, 0, - remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0) - goto cleanup; - - error = git_commit_create(&oid, repo, notes_ref, author, committer, - NULL, GIT_NOTES_DEFAULT_MSG_RM, - tree_after_removal, - *parents == NULL ? 0 : 1, - (const git_commit **) parents); - -cleanup: - git_tree_free(tree_after_removal); - return error; -} - -static int note_get_default_ref(const char **out, git_repository *repo) -{ - int ret; - git_config *cfg; - - *out = NULL; - - if (git_repository_config__weakptr(&cfg, repo) < 0) - return -1; - - ret = git_config_get_string(out, cfg, "core.notesRef"); - if (ret == GIT_ENOTFOUND) { - *out = GIT_NOTES_DEFAULT_REF; - return 0; - } - - return ret; -} - -static int normalize_namespace(const char **notes_ref, git_repository *repo) -{ - if (*notes_ref) - return 0; - - return note_get_default_ref(notes_ref, repo); -} - -static int retrieve_note_tree_and_commit( - git_tree **tree_out, - git_commit **commit_out, - git_repository *repo, - const char **notes_ref) -{ - int error; - git_oid oid; - - if ((error = normalize_namespace(notes_ref, repo)) < 0) - return error; - - if ((error = git_reference_name_to_id(&oid, repo, *notes_ref)) < 0) - return error; - - if (git_commit_lookup(commit_out, repo, &oid) < 0) - return error; - - if ((error = git_commit_tree(tree_out, *commit_out)) < 0) - return error; - - return 0; -} - -int git_note_read(git_note **out, git_repository *repo, - const char *notes_ref, const git_oid *oid) -{ - int error; - char *target = NULL; - git_tree *tree = NULL; - git_commit *commit = NULL; - - target = git_oid_allocfmt(oid); - GITERR_CHECK_ALLOC(target); - - if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) - goto cleanup; - - error = note_lookup(out, repo, tree, target); - -cleanup: - git__free(target); - git_tree_free(tree); - git_commit_free(commit); - return error; -} - -int git_note_create( - git_oid *out, - git_repository *repo, - const git_signature *author, - const git_signature *committer, - const char *notes_ref, - const git_oid *oid, - const char *note, - int allow_note_overwrite) -{ - int error; - char *target = NULL; - git_commit *commit = NULL; - git_tree *tree = NULL; - - target = git_oid_allocfmt(oid); - GITERR_CHECK_ALLOC(target); - - error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref); - - if (error < 0 && error != GIT_ENOTFOUND) - goto cleanup; - - error = note_write(out, repo, author, committer, notes_ref, - note, tree, target, &commit, allow_note_overwrite); - -cleanup: - git__free(target); - git_commit_free(commit); - git_tree_free(tree); - return error; -} - -int git_note_remove(git_repository *repo, const char *notes_ref, - const git_signature *author, const git_signature *committer, - const git_oid *oid) -{ - int error; - char *target = NULL; - git_commit *commit = NULL; - git_tree *tree = NULL; - - target = git_oid_allocfmt(oid); - GITERR_CHECK_ALLOC(target); - - if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) - goto cleanup; - - error = note_remove(repo, author, committer, notes_ref, - tree, target, &commit); - -cleanup: - git__free(target); - git_commit_free(commit); - git_tree_free(tree); - return error; -} - -int git_note_default_ref(const char **out, git_repository *repo) -{ - assert(repo); - return note_get_default_ref(out, repo); -} - -const char * git_note_message(const git_note *note) -{ - assert(note); - return note->message; -} - -const git_oid * git_note_oid(const git_note *note) -{ - assert(note); - return ¬e->oid; -} - -void git_note_free(git_note *note) -{ - if (note == NULL) - return; - - git__free(note->message); - git__free(note); -} - -static int process_entry_path( - const char* entry_path, - const git_oid *note_oid, - git_note_foreach_cb note_cb, - void *payload) -{ - int error = -1; - size_t i = 0, j = 0, len; - git_buf buf = GIT_BUF_INIT; - git_oid annotated_object_id; - - if ((error = git_buf_puts(&buf, entry_path)) < 0) - goto cleanup; - - len = git_buf_len(&buf); - - while (i < len) { - if (buf.ptr[i] == '/') { - i++; - continue; - } - - if (git__fromhex(buf.ptr[i]) < 0) { - /* This is not a note entry */ - goto cleanup; - } - - if (i != j) - buf.ptr[j] = buf.ptr[i]; - - i++; - j++; - } - - buf.ptr[j] = '\0'; - buf.size = j; - - if (j != GIT_OID_HEXSZ) { - /* This is not a note entry */ - goto cleanup; - } - - if ((error = git_oid_fromstr(&annotated_object_id, buf.ptr)) < 0) - goto cleanup; - - if (note_cb(note_oid, &annotated_object_id, payload)) - error = GIT_EUSER; - -cleanup: - git_buf_free(&buf); - return error; -} - -int git_note_foreach( - git_repository *repo, - const char *notes_ref, - git_note_foreach_cb note_cb, - void *payload) -{ - int error; - git_iterator *iter = NULL; - git_tree *tree = NULL; - git_commit *commit = NULL; - const git_index_entry *item; - - if (!(error = retrieve_note_tree_and_commit( - &tree, &commit, repo, ¬es_ref)) && - !(error = git_iterator_for_tree(&iter, tree))) - error = git_iterator_current(iter, &item); - - while (!error && item) { - error = process_entry_path(item->path, &item->oid, note_cb, payload); - - if (!error) - error = git_iterator_advance(iter, &item); - } - - git_iterator_free(iter); - git_tree_free(tree); - git_commit_free(commit); - - return error; -} diff --git a/src/object.c b/src/object.c deleted file mode 100644 index f59e4c7dad1..00000000000 --- a/src/object.c +++ /dev/null @@ -1,398 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include - -#include "git2/object.h" - -#include "common.h" -#include "repository.h" - -#include "commit.h" -#include "tree.h" -#include "blob.h" -#include "tag.h" - -static const int OBJECT_BASE_SIZE = 4096; - -static struct { - const char *str; /* type name string */ - int loose; /* valid loose object type flag */ - size_t size; /* size in bytes of the object structure */ -} git_objects_table[] = { - /* 0 = GIT_OBJ__EXT1 */ - { "", 0, 0}, - - /* 1 = GIT_OBJ_COMMIT */ - { "commit", 1, sizeof(struct git_commit)}, - - /* 2 = GIT_OBJ_TREE */ - { "tree", 1, sizeof(struct git_tree) }, - - /* 3 = GIT_OBJ_BLOB */ - { "blob", 1, sizeof(struct git_blob) }, - - /* 4 = GIT_OBJ_TAG */ - { "tag", 1, sizeof(struct git_tag) }, - - /* 5 = GIT_OBJ__EXT2 */ - { "", 0, 0 }, - - /* 6 = GIT_OBJ_OFS_DELTA */ - { "OFS_DELTA", 0, 0 }, - - /* 7 = GIT_OBJ_REF_DELTA */ - { "REF_DELTA", 0, 0 } -}; - -static int create_object(git_object **object_out, git_otype type) -{ - git_object *object = NULL; - - assert(object_out); - - *object_out = NULL; - - switch (type) { - case GIT_OBJ_COMMIT: - case GIT_OBJ_TAG: - case GIT_OBJ_BLOB: - case GIT_OBJ_TREE: - object = git__malloc(git_object__size(type)); - GITERR_CHECK_ALLOC(object); - memset(object, 0x0, git_object__size(type)); - break; - - default: - giterr_set(GITERR_INVALID, "The given type is invalid"); - return -1; - } - - object->type = type; - - *object_out = object; - return 0; -} - -int git_object__from_odb_object( - git_object **object_out, - git_repository *repo, - git_odb_object *odb_obj, - git_otype type) -{ - int error; - git_object *object = NULL; - - if (type != GIT_OBJ_ANY && type != odb_obj->raw.type) { - giterr_set(GITERR_INVALID, "The requested type does not match the type in the ODB"); - return GIT_ENOTFOUND; - } - - type = odb_obj->raw.type; - - if ((error = create_object(&object, type)) < 0) - return error; - - /* Initialize parent object */ - git_oid_cpy(&object->cached.oid, &odb_obj->cached.oid); - object->repo = repo; - - switch (type) { - case GIT_OBJ_COMMIT: - error = git_commit__parse((git_commit *)object, odb_obj); - break; - - case GIT_OBJ_TREE: - error = git_tree__parse((git_tree *)object, odb_obj); - break; - - case GIT_OBJ_TAG: - error = git_tag__parse((git_tag *)object, odb_obj); - break; - - case GIT_OBJ_BLOB: - error = git_blob__parse((git_blob *)object, odb_obj); - break; - - default: - break; - } - - if (error < 0) - git_object__free(object); - else - *object_out = git_cache_try_store(&repo->objects, object); - - return error; -} - -int git_object_lookup_prefix( - git_object **object_out, - git_repository *repo, - const git_oid *id, - size_t len, - git_otype type) -{ - git_object *object = NULL; - git_odb *odb = NULL; - git_odb_object *odb_obj; - int error = 0; - - assert(repo && object_out && id); - - if (len < GIT_OID_MINPREFIXLEN) - return GIT_EAMBIGUOUS; - - error = git_repository_odb__weakptr(&odb, repo); - if (error < 0) - return error; - - if (len > GIT_OID_HEXSZ) - len = GIT_OID_HEXSZ; - - if (len == GIT_OID_HEXSZ) { - /* We want to match the full id : we can first look up in the cache, - * since there is no need to check for non ambiguousity - */ - object = git_cache_get(&repo->objects, id); - if (object != NULL) { - if (type != GIT_OBJ_ANY && type != object->type) { - git_object_free(object); - giterr_set(GITERR_INVALID, "The requested type does not match the type in ODB"); - return GIT_ENOTFOUND; - } - - *object_out = object; - return 0; - } - - /* Object was not found in the cache, let's explore the backends. - * We could just use git_odb_read_unique_short_oid, - * it is the same cost for packed and loose object backends, - * but it may be much more costly for sqlite and hiredis. - */ - error = git_odb_read(&odb_obj, odb, id); - } else { - git_oid short_oid; - - /* We copy the first len*4 bits from id and fill the remaining with 0s */ - memcpy(short_oid.id, id->id, (len + 1) / 2); - if (len % 2) - short_oid.id[len / 2] &= 0xF0; - memset(short_oid.id + (len + 1) / 2, 0, (GIT_OID_HEXSZ - len) / 2); - - /* If len < GIT_OID_HEXSZ (a strict short oid was given), we have - * 2 options : - * - We always search in the cache first. If we find that short oid is - * ambiguous, we can stop. But in all the other cases, we must then - * explore all the backends (to find an object if there was match, - * or to check that oid is not ambiguous if we have found 1 match in - * the cache) - * - We never explore the cache, go right to exploring the backends - * We chose the latter : we explore directly the backends. - */ - error = git_odb_read_prefix(&odb_obj, odb, &short_oid, len); - } - - if (error < 0) - return error; - - error = git_object__from_odb_object(object_out, repo, odb_obj, type); - - git_odb_object_free(odb_obj); - - return error; -} - -int git_object_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_otype type) { - return git_object_lookup_prefix(object_out, repo, id, GIT_OID_HEXSZ, type); -} - -void git_object__free(void *_obj) -{ - git_object *object = (git_object *)_obj; - - assert(object); - - switch (object->type) { - case GIT_OBJ_COMMIT: - git_commit__free((git_commit *)object); - break; - - case GIT_OBJ_TREE: - git_tree__free((git_tree *)object); - break; - - case GIT_OBJ_TAG: - git_tag__free((git_tag *)object); - break; - - case GIT_OBJ_BLOB: - git_blob__free((git_blob *)object); - break; - - default: - git__free(object); - break; - } -} - -void git_object_free(git_object *object) -{ - if (object == NULL) - return; - - git_cached_obj_decref((git_cached_obj *)object, git_object__free); -} - -const git_oid *git_object_id(const git_object *obj) -{ - assert(obj); - return &obj->cached.oid; -} - -git_otype git_object_type(const git_object *obj) -{ - assert(obj); - return obj->type; -} - -git_repository *git_object_owner(const git_object *obj) -{ - assert(obj); - return obj->repo; -} - -const char *git_object_type2string(git_otype type) -{ - if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table)) - return ""; - - return git_objects_table[type].str; -} - -git_otype git_object_string2type(const char *str) -{ - size_t i; - - if (!str || !*str) - return GIT_OBJ_BAD; - - for (i = 0; i < ARRAY_SIZE(git_objects_table); i++) - if (!strcmp(str, git_objects_table[i].str)) - return (git_otype)i; - - return GIT_OBJ_BAD; -} - -int git_object_typeisloose(git_otype type) -{ - if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table)) - return 0; - - return git_objects_table[type].loose; -} - -size_t git_object__size(git_otype type) -{ - if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table)) - return 0; - - return git_objects_table[type].size; -} - -static int dereference_object(git_object **dereferenced, git_object *obj) -{ - git_otype type = git_object_type(obj); - - switch (type) { - case GIT_OBJ_COMMIT: - return git_commit_tree((git_tree **)dereferenced, (git_commit*)obj); - - case GIT_OBJ_TAG: - return git_tag_target(dereferenced, (git_tag*)obj); - - case GIT_OBJ_BLOB: - return GIT_ENOTFOUND; - - case GIT_OBJ_TREE: - return GIT_EAMBIGUOUS; - - default: - return GIT_EINVALIDSPEC; - } -} - -static int peel_error(int error, const git_oid *oid, git_otype type) -{ - const char *type_name; - char hex_oid[GIT_OID_HEXSZ + 1]; - - type_name = git_object_type2string(type); - - git_oid_fmt(hex_oid, oid); - hex_oid[GIT_OID_HEXSZ] = '\0'; - - giterr_set(GITERR_OBJECT, "The git_object of id '%s' can not be " - "successfully peeled into a %s (git_otype=%i).", hex_oid, type_name, type); - - return error; -} - -int git_object_peel( - git_object **peeled, - const git_object *object, - git_otype target_type) -{ - git_object *source, *deref = NULL; - int error; - - if (target_type != GIT_OBJ_TAG && - target_type != GIT_OBJ_COMMIT && - target_type != GIT_OBJ_TREE && - target_type != GIT_OBJ_BLOB && - target_type != GIT_OBJ_ANY) - return GIT_EINVALIDSPEC; - - assert(object && peeled); - - if (git_object_type(object) == target_type) - return git_object__dup(peeled, (git_object *)object); - - source = (git_object *)object; - - while (!(error = dereference_object(&deref, source))) { - - if (source != object) - git_object_free(source); - - if (git_object_type(deref) == target_type) { - *peeled = deref; - return 0; - } - - if (target_type == GIT_OBJ_ANY && - git_object_type(deref) != git_object_type(object)) - { - *peeled = deref; - return 0; - } - - source = deref; - deref = NULL; - } - - if (source != object) - git_object_free(source); - - git_object_free(deref); - - if (error) - error = peel_error(error, git_object_id(object), target_type); - - return error; -} - diff --git a/src/object.h b/src/object.h deleted file mode 100644 index 8788caba6a7..00000000000 --- a/src/object.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_object_h__ -#define INCLUDE_object_h__ - -/** Base git object for inheritance */ -struct git_object { - git_cached_obj cached; - git_repository *repo; - git_otype type; -}; - -/* fully free the object; internal method, DO NOT EXPORT */ -void git_object__free(void *object); - -GIT_INLINE(int) git_object__dup(git_object **dest, git_object *source) -{ - git_cached_obj_incref(source); - *dest = source; - return 0; -} - -int git_object__from_odb_object( - git_object **object_out, - git_repository *repo, - git_odb_object *odb_obj, - git_otype type); - -int git_object__resolve_to_type(git_object **obj, git_otype type); - -int git_oid__parse(git_oid *oid, const char **buffer_out, const char *buffer_end, const char *header); - -void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid); - -#endif diff --git a/src/odb.c b/src/odb.c deleted file mode 100644 index 24381e70e2d..00000000000 --- a/src/odb.c +++ /dev/null @@ -1,901 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include -#include "git2/object.h" -#include "fileops.h" -#include "hash.h" -#include "odb.h" -#include "delta-apply.h" -#include "filter.h" - -#include "git2/odb_backend.h" -#include "git2/oid.h" - -#define GIT_ALTERNATES_FILE "info/alternates" - -/* TODO: is this correct? */ -#define GIT_LOOSE_PRIORITY 2 -#define GIT_PACKED_PRIORITY 1 - -#define GIT_ALTERNATES_MAX_DEPTH 5 - -typedef struct -{ - git_odb_backend *backend; - int priority; - int is_alternate; -} backend_internal; - -static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth); - -int git_odb__format_object_header(char *hdr, size_t n, size_t obj_len, git_otype obj_type) -{ - const char *type_str = git_object_type2string(obj_type); - int len = p_snprintf(hdr, n, "%s %"PRIuZ, type_str, obj_len); - assert(len > 0 && len <= (int)n); - return len+1; -} - -int git_odb__hashobj(git_oid *id, git_rawobj *obj) -{ - git_buf_vec vec[2]; - char header[64]; - int hdrlen; - - assert(id && obj); - - if (!git_object_typeisloose(obj->type)) - return -1; - if (!obj->data && obj->len != 0) - return -1; - - hdrlen = git_odb__format_object_header(header, sizeof(header), obj->len, obj->type); - - vec[0].data = header; - vec[0].len = hdrlen; - vec[1].data = obj->data; - vec[1].len = obj->len; - - git_hash_vec(id, vec, 2); - - return 0; -} - - -static git_odb_object *new_odb_object(const git_oid *oid, git_rawobj *source) -{ - git_odb_object *object = git__malloc(sizeof(git_odb_object)); - memset(object, 0x0, sizeof(git_odb_object)); - - git_oid_cpy(&object->cached.oid, oid); - memcpy(&object->raw, source, sizeof(git_rawobj)); - - return object; -} - -static void free_odb_object(void *o) -{ - git_odb_object *object = (git_odb_object *)o; - - if (object != NULL) { - git__free(object->raw.data); - git__free(object); - } -} - -const git_oid *git_odb_object_id(git_odb_object *object) -{ - return &object->cached.oid; -} - -const void *git_odb_object_data(git_odb_object *object) -{ - return object->raw.data; -} - -size_t git_odb_object_size(git_odb_object *object) -{ - return object->raw.len; -} - -git_otype git_odb_object_type(git_odb_object *object) -{ - return object->raw.type; -} - -void git_odb_object_free(git_odb_object *object) -{ - if (object == NULL) - return; - - git_cached_obj_decref((git_cached_obj *)object, &free_odb_object); -} - -int git_odb__hashfd(git_oid *out, git_file fd, size_t size, git_otype type) -{ - int hdr_len; - char hdr[64], buffer[2048]; - git_hash_ctx ctx; - ssize_t read_len = 0; - int error = 0; - - if (!git_object_typeisloose(type)) { - giterr_set(GITERR_INVALID, "Invalid object type for hash"); - return -1; - } - - if ((error = git_hash_ctx_init(&ctx)) < 0) - return -1; - - hdr_len = git_odb__format_object_header(hdr, sizeof(hdr), size, type); - - if ((error = git_hash_update(&ctx, hdr, hdr_len)) < 0) - goto done; - - while (size > 0 && (read_len = p_read(fd, buffer, sizeof(buffer))) > 0) { - if ((error = git_hash_update(&ctx, buffer, read_len)) < 0) - goto done; - - size -= read_len; - } - - /* If p_read returned an error code, the read obviously failed. - * If size is not zero, the file was truncated after we originally - * stat'd it, so we consider this a read failure too */ - if (read_len < 0 || size > 0) { - giterr_set(GITERR_OS, "Error reading file for hashing"); - error = -1; - - goto done; - return -1; - } - - error = git_hash_final(out, &ctx); - -done: - git_hash_ctx_cleanup(&ctx); - return error; -} - -int git_odb__hashfd_filtered( - git_oid *out, git_file fd, size_t size, git_otype type, git_vector *filters) -{ - int error; - git_buf raw = GIT_BUF_INIT; - git_buf filtered = GIT_BUF_INIT; - - if (!filters || !filters->length) - return git_odb__hashfd(out, fd, size, type); - - /* size of data is used in header, so we have to read the whole file - * into memory to apply filters before beginning to calculate the hash - */ - - if (!(error = git_futils_readbuffer_fd(&raw, fd, size))) - error = git_filters_apply(&filtered, &raw, filters); - - git_buf_free(&raw); - - if (!error) - error = git_odb_hash(out, filtered.ptr, filtered.size, type); - - git_buf_free(&filtered); - - return error; -} - -int git_odb__hashlink(git_oid *out, const char *path) -{ - struct stat st; - git_off_t size; - int result; - - if (git_path_lstat(path, &st) < 0) - return -1; - - size = st.st_size; - - if (!git__is_sizet(size)) { - giterr_set(GITERR_OS, "File size overflow for 32-bit systems"); - return -1; - } - - if (S_ISLNK(st.st_mode)) { - char *link_data; - ssize_t read_len; - - link_data = git__malloc((size_t)(size + 1)); - GITERR_CHECK_ALLOC(link_data); - - read_len = p_readlink(path, link_data, (size_t)size); - link_data[size] = '\0'; - if (read_len != (ssize_t)size) { - giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", path); - return -1; - } - - result = git_odb_hash(out, link_data, (size_t)size, GIT_OBJ_BLOB); - git__free(link_data); - } else { - int fd = git_futils_open_ro(path); - if (fd < 0) - return -1; - result = git_odb__hashfd(out, fd, (size_t)size, GIT_OBJ_BLOB); - p_close(fd); - } - - return result; -} - -int git_odb_hashfile(git_oid *out, const char *path, git_otype type) -{ - git_off_t size; - int result, fd = git_futils_open_ro(path); - if (fd < 0) - return fd; - - if ((size = git_futils_filesize(fd)) < 0 || !git__is_sizet(size)) { - giterr_set(GITERR_OS, "File size overflow for 32-bit systems"); - p_close(fd); - return -1; - } - - result = git_odb__hashfd(out, fd, (size_t)size, type); - p_close(fd); - return result; -} - -int git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type) -{ - git_rawobj raw; - - assert(id); - - raw.data = (void *)data; - raw.len = len; - raw.type = type; - - return git_odb__hashobj(id, &raw); -} - -/** - * FAKE WSTREAM - */ - -typedef struct { - git_odb_stream stream; - char *buffer; - size_t size, written; - git_otype type; -} fake_wstream; - -static int fake_wstream__fwrite(git_oid *oid, git_odb_stream *_stream) -{ - fake_wstream *stream = (fake_wstream *)_stream; - return _stream->backend->write(oid, _stream->backend, stream->buffer, stream->size, stream->type); -} - -static int fake_wstream__write(git_odb_stream *_stream, const char *data, size_t len) -{ - fake_wstream *stream = (fake_wstream *)_stream; - - if (stream->written + len > stream->size) - return -1; - - memcpy(stream->buffer + stream->written, data, len); - stream->written += len; - return 0; -} - -static void fake_wstream__free(git_odb_stream *_stream) -{ - fake_wstream *stream = (fake_wstream *)_stream; - - git__free(stream->buffer); - git__free(stream); -} - -static int init_fake_wstream(git_odb_stream **stream_p, git_odb_backend *backend, size_t size, git_otype type) -{ - fake_wstream *stream; - - stream = git__calloc(1, sizeof(fake_wstream)); - GITERR_CHECK_ALLOC(stream); - - stream->size = size; - stream->type = type; - stream->buffer = git__malloc(size); - if (stream->buffer == NULL) { - git__free(stream); - return -1; - } - - stream->stream.backend = backend; - stream->stream.read = NULL; /* read only */ - stream->stream.write = &fake_wstream__write; - stream->stream.finalize_write = &fake_wstream__fwrite; - stream->stream.free = &fake_wstream__free; - stream->stream.mode = GIT_STREAM_WRONLY; - - *stream_p = (git_odb_stream *)stream; - return 0; -} - -/*********************************************************** - * - * OBJECT DATABASE PUBLIC API - * - * Public calls for the ODB functionality - * - ***********************************************************/ - -static int backend_sort_cmp(const void *a, const void *b) -{ - const backend_internal *backend_a = (const backend_internal *)(a); - const backend_internal *backend_b = (const backend_internal *)(b); - - if (backend_a->is_alternate == backend_b->is_alternate) - return (backend_b->priority - backend_a->priority); - - return backend_a->is_alternate ? 1 : -1; -} - -int git_odb_new(git_odb **out) -{ - git_odb *db = git__calloc(1, sizeof(*db)); - GITERR_CHECK_ALLOC(db); - - if (git_cache_init(&db->cache, GIT_DEFAULT_CACHE_SIZE, &free_odb_object) < 0 || - git_vector_init(&db->backends, 4, backend_sort_cmp) < 0) - { - git__free(db); - return -1; - } - - *out = db; - GIT_REFCOUNT_INC(db); - return 0; -} - -static int add_backend_internal(git_odb *odb, git_odb_backend *backend, int priority, int is_alternate) -{ - backend_internal *internal; - - assert(odb && backend); - - GITERR_CHECK_VERSION(backend, GIT_ODB_BACKEND_VERSION, "git_odb_backend"); - - /* Check if the backend is already owned by another ODB */ - assert(!backend->odb || backend->odb == odb); - - internal = git__malloc(sizeof(backend_internal)); - GITERR_CHECK_ALLOC(internal); - - internal->backend = backend; - internal->priority = priority; - internal->is_alternate = is_alternate; - - if (git_vector_insert(&odb->backends, internal) < 0) { - git__free(internal); - return -1; - } - - git_vector_sort(&odb->backends); - internal->backend->odb = odb; - return 0; -} - -int git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority) -{ - return add_backend_internal(odb, backend, priority, 0); -} - -int git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority) -{ - return add_backend_internal(odb, backend, priority, 1); -} - -static int add_default_backends(git_odb *db, const char *objects_dir, int as_alternates, int alternate_depth) -{ - git_odb_backend *loose, *packed; - - /* add the loose object backend */ - if (git_odb_backend_loose(&loose, objects_dir, -1, 0) < 0 || - add_backend_internal(db, loose, GIT_LOOSE_PRIORITY, as_alternates) < 0) - return -1; - - /* add the packed file backend */ - if (git_odb_backend_pack(&packed, objects_dir) < 0 || - add_backend_internal(db, packed, GIT_PACKED_PRIORITY, as_alternates) < 0) - return -1; - - return load_alternates(db, objects_dir, alternate_depth); -} - -static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth) -{ - git_buf alternates_path = GIT_BUF_INIT; - git_buf alternates_buf = GIT_BUF_INIT; - char *buffer; - const char *alternate; - int result = 0; - - /* Git reports an error, we just ignore anything deeper */ - if (alternate_depth > GIT_ALTERNATES_MAX_DEPTH) { - return 0; - } - - if (git_buf_joinpath(&alternates_path, objects_dir, GIT_ALTERNATES_FILE) < 0) - return -1; - - if (git_path_exists(alternates_path.ptr) == false) { - git_buf_free(&alternates_path); - return 0; - } - - if (git_futils_readbuffer(&alternates_buf, alternates_path.ptr) < 0) { - git_buf_free(&alternates_path); - return -1; - } - - buffer = (char *)alternates_buf.ptr; - - /* add each alternate as a new backend; one alternate per line */ - while ((alternate = git__strtok(&buffer, "\r\n")) != NULL) { - if (*alternate == '\0' || *alternate == '#') - continue; - - /* - * Relative path: build based on the current `objects` - * folder. However, relative paths are only allowed in - * the current repository. - */ - if (*alternate == '.' && !alternate_depth) { - if ((result = git_buf_joinpath(&alternates_path, objects_dir, alternate)) < 0) - break; - alternate = git_buf_cstr(&alternates_path); - } - - if ((result = add_default_backends(odb, alternate, 1, alternate_depth + 1)) < 0) - break; - } - - git_buf_free(&alternates_path); - git_buf_free(&alternates_buf); - - return result; -} - -int git_odb_add_disk_alternate(git_odb *odb, const char *path) -{ - return add_default_backends(odb, path, 1, 0); -} - -int git_odb_open(git_odb **out, const char *objects_dir) -{ - git_odb *db; - - assert(out && objects_dir); - - *out = NULL; - - if (git_odb_new(&db) < 0) - return -1; - - if (add_default_backends(db, objects_dir, 0, 0) < 0) { - git_odb_free(db); - return -1; - } - - *out = db; - return 0; -} - -static void odb_free(git_odb *db) -{ - unsigned int i; - - for (i = 0; i < db->backends.length; ++i) { - backend_internal *internal = git_vector_get(&db->backends, i); - git_odb_backend *backend = internal->backend; - - if (backend->free) backend->free(backend); - else git__free(backend); - - git__free(internal); - } - - git_vector_free(&db->backends); - git_cache_free(&db->cache); - git__free(db); -} - -void git_odb_free(git_odb *db) -{ - if (db == NULL) - return; - - GIT_REFCOUNT_DEC(db, odb_free); -} - -int git_odb_exists(git_odb *db, const git_oid *id) -{ - git_odb_object *object; - unsigned int i; - bool found = false; - bool refreshed = false; - - assert(db && id); - - if ((object = git_cache_get(&db->cache, id)) != NULL) { - git_odb_object_free(object); - return (int)true; - } - -attempt_lookup: - for (i = 0; i < db->backends.length && !found; ++i) { - backend_internal *internal = git_vector_get(&db->backends, i); - git_odb_backend *b = internal->backend; - - if (b->exists != NULL) - found = b->exists(b, id); - } - - if (!found && !refreshed) { - if (git_odb_refresh(db) < 0) { - giterr_clear(); - return (int)false; - } - - refreshed = true; - goto attempt_lookup; - } - - return (int)found; -} - -int git_odb_read_header(size_t *len_p, git_otype *type_p, git_odb *db, const git_oid *id) -{ - int error; - git_odb_object *object; - - error = git_odb__read_header_or_object(&object, len_p, type_p, db, id); - - if (object) - git_odb_object_free(object); - - return error; -} - -int git_odb__read_header_or_object( - git_odb_object **out, size_t *len_p, git_otype *type_p, - git_odb *db, const git_oid *id) -{ - unsigned int i; - int error = GIT_ENOTFOUND; - git_odb_object *object; - - assert(db && id && out && len_p && type_p); - - if ((object = git_cache_get(&db->cache, id)) != NULL) { - *len_p = object->raw.len; - *type_p = object->raw.type; - *out = object; - return 0; - } - - *out = NULL; - - for (i = 0; i < db->backends.length && error < 0; ++i) { - backend_internal *internal = git_vector_get(&db->backends, i); - git_odb_backend *b = internal->backend; - - if (b->read_header != NULL) - error = b->read_header(len_p, type_p, b, id); - } - - if (!error || error == GIT_PASSTHROUGH) - return 0; - - /* - * no backend could read only the header. - * try reading the whole object and freeing the contents - */ - if ((error = git_odb_read(&object, db, id)) < 0) - return error; /* error already set - pass along */ - - *len_p = object->raw.len; - *type_p = object->raw.type; - *out = object; - - return 0; -} - -int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id) -{ - unsigned int i; - int error; - bool refreshed = false; - git_rawobj raw; - - assert(out && db && id); - - if (db->backends.length == 0) { - giterr_set(GITERR_ODB, "Failed to lookup object: no backends loaded"); - return GIT_ENOTFOUND; - } - - *out = git_cache_get(&db->cache, id); - if (*out != NULL) - return 0; - -attempt_lookup: - error = GIT_ENOTFOUND; - - for (i = 0; i < db->backends.length && error < 0; ++i) { - backend_internal *internal = git_vector_get(&db->backends, i); - git_odb_backend *b = internal->backend; - - if (b->read != NULL) - error = b->read(&raw.data, &raw.len, &raw.type, b, id); - } - - if (error == GIT_ENOTFOUND && !refreshed) { - if ((error = git_odb_refresh(db)) < 0) - return error; - - refreshed = true; - goto attempt_lookup; - } - - if (error && error != GIT_PASSTHROUGH) - return error; - - *out = git_cache_try_store(&db->cache, new_odb_object(id, &raw)); - return 0; -} - -int git_odb_read_prefix( - git_odb_object **out, git_odb *db, const git_oid *short_id, size_t len) -{ - unsigned int i; - int error = GIT_ENOTFOUND; - git_oid found_full_oid = {{0}}; - git_rawobj raw; - void *data = NULL; - bool found = false, refreshed = false; - - assert(out && db); - - if (len < GIT_OID_MINPREFIXLEN) - return git_odb__error_ambiguous("prefix length too short"); - - if (len > GIT_OID_HEXSZ) - len = GIT_OID_HEXSZ; - - if (len == GIT_OID_HEXSZ) { - *out = git_cache_get(&db->cache, short_id); - if (*out != NULL) - return 0; - } - -attempt_lookup: - for (i = 0; i < db->backends.length; ++i) { - backend_internal *internal = git_vector_get(&db->backends, i); - git_odb_backend *b = internal->backend; - - if (b->read_prefix != NULL) { - git_oid full_oid; - error = b->read_prefix(&full_oid, &raw.data, &raw.len, &raw.type, b, short_id, len); - if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH) - continue; - - if (error) - return error; - - git__free(data); - data = raw.data; - - if (found && git_oid_cmp(&full_oid, &found_full_oid)) - return git_odb__error_ambiguous("multiple matches for prefix"); - - found_full_oid = full_oid; - found = true; - } - } - - if (!found && !refreshed) { - if ((error = git_odb_refresh(db)) < 0) - return error; - - refreshed = true; - goto attempt_lookup; - } - - if (!found) - return git_odb__error_notfound("no match for prefix", short_id); - - *out = git_cache_try_store(&db->cache, new_odb_object(&found_full_oid, &raw)); - return 0; -} - -int git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload) -{ - unsigned int i; - backend_internal *internal; - - git_vector_foreach(&db->backends, i, internal) { - git_odb_backend *b = internal->backend; - int error = b->foreach(b, cb, payload); - if (error < 0) - return error; - } - - return 0; -} - -int git_odb_write( - git_oid *oid, git_odb *db, const void *data, size_t len, git_otype type) -{ - unsigned int i; - int error = GIT_ERROR; - git_odb_stream *stream; - - assert(oid && db); - - git_odb_hash(oid, data, len, type); - if (git_odb_exists(db, oid)) - return 0; - - for (i = 0; i < db->backends.length && error < 0; ++i) { - backend_internal *internal = git_vector_get(&db->backends, i); - git_odb_backend *b = internal->backend; - - /* we don't write in alternates! */ - if (internal->is_alternate) - continue; - - if (b->write != NULL) - error = b->write(oid, b, data, len, type); - } - - if (!error || error == GIT_PASSTHROUGH) - return 0; - - /* if no backends were able to write the object directly, we try a streaming - * write to the backends; just write the whole object into the stream in one - * push */ - - if ((error = git_odb_open_wstream(&stream, db, len, type)) != 0) - return error; - - stream->write(stream, data, len); - error = stream->finalize_write(oid, stream); - stream->free(stream); - - return error; -} - -int git_odb_open_wstream( - git_odb_stream **stream, git_odb *db, size_t size, git_otype type) -{ - unsigned int i; - int error = GIT_ERROR; - - assert(stream && db); - - for (i = 0; i < db->backends.length && error < 0; ++i) { - backend_internal *internal = git_vector_get(&db->backends, i); - git_odb_backend *b = internal->backend; - - /* we don't write in alternates! */ - if (internal->is_alternate) - continue; - - if (b->writestream != NULL) - error = b->writestream(stream, b, size, type); - else if (b->write != NULL) - error = init_fake_wstream(stream, b, size, type); - } - - if (error == GIT_PASSTHROUGH) - error = 0; - - return error; -} - -int git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oid) -{ - unsigned int i; - int error = GIT_ERROR; - - assert(stream && db); - - for (i = 0; i < db->backends.length && error < 0; ++i) { - backend_internal *internal = git_vector_get(&db->backends, i); - git_odb_backend *b = internal->backend; - - if (b->readstream != NULL) - error = b->readstream(stream, b, oid); - } - - if (error == GIT_PASSTHROUGH) - error = 0; - - return error; -} - -int git_odb_write_pack(struct git_odb_writepack **out, git_odb *db, git_transfer_progress_callback progress_cb, void *progress_payload) -{ - unsigned int i; - int error = GIT_ERROR; - - assert(out && db); - - for (i = 0; i < db->backends.length && error < 0; ++i) { - backend_internal *internal = git_vector_get(&db->backends, i); - git_odb_backend *b = internal->backend; - - /* we don't write in alternates! */ - if (internal->is_alternate) - continue; - - if (b->writepack != NULL) - error = b->writepack(out, b, progress_cb, progress_payload); - } - - if (error == GIT_PASSTHROUGH) - error = 0; - - return error; -} - -void *git_odb_backend_malloc(git_odb_backend *backend, size_t len) -{ - GIT_UNUSED(backend); - return git__malloc(len); -} - -int git_odb_refresh(struct git_odb *db) -{ - unsigned int i; - assert(db); - - for (i = 0; i < db->backends.length; ++i) { - backend_internal *internal = git_vector_get(&db->backends, i); - git_odb_backend *b = internal->backend; - - if (b->refresh != NULL) { - int error = b->refresh(b); - if (error < 0) - return error; - } - } - - return 0; -} - -int git_odb__error_notfound(const char *message, const git_oid *oid) -{ - if (oid != NULL) { - char oid_str[GIT_OID_HEXSZ + 1]; - git_oid_tostr(oid_str, sizeof(oid_str), oid); - giterr_set(GITERR_ODB, "Object not found - %s (%s)", message, oid_str); - } else - giterr_set(GITERR_ODB, "Object not found - %s", message); - - return GIT_ENOTFOUND; -} - -int git_odb__error_ambiguous(const char *message) -{ - giterr_set(GITERR_ODB, "Ambiguous SHA1 prefix - %s", message); - return GIT_EAMBIGUOUS; -} - diff --git a/src/odb.h b/src/odb.h deleted file mode 100644 index 7c018cc5041..00000000000 --- a/src/odb.h +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_odb_h__ -#define INCLUDE_odb_h__ - -#include "git2/odb.h" -#include "git2/oid.h" -#include "git2/types.h" - -#include "vector.h" -#include "cache.h" -#include "posix.h" - -#define GIT_OBJECTS_DIR "objects/" -#define GIT_OBJECT_DIR_MODE 0777 -#define GIT_OBJECT_FILE_MODE 0444 - -/* DO NOT EXPORT */ -typedef struct { - void *data; /**< Raw, decompressed object data. */ - size_t len; /**< Total number of bytes in data. */ - git_otype type; /**< Type of this object. */ -} git_rawobj; - -/* EXPORT */ -struct git_odb_object { - git_cached_obj cached; - git_rawobj raw; -}; - -/* EXPORT */ -struct git_odb { - git_refcount rc; - git_vector backends; - git_cache cache; -}; - -/* - * Hash a git_rawobj internally. - * The `git_rawobj` is supposed to be previously initialized - */ -int git_odb__hashobj(git_oid *id, git_rawobj *obj); - -/* - * Format the object header such as it would appear in the on-disk object - */ -int git_odb__format_object_header(char *hdr, size_t n, size_t obj_len, git_otype obj_type); -/* - * Hash an open file descriptor. - * This is a performance call when the contents of a fd need to be hashed, - * but the fd is already open and we have the size of the contents. - * - * Saves us some `stat` calls. - * - * The fd is never closed, not even on error. It must be opened and closed - * by the caller - */ -int git_odb__hashfd(git_oid *out, git_file fd, size_t size, git_otype type); - -/* - * Hash an open file descriptor applying an array of filters - * Acts just like git_odb__hashfd with the addition of filters... - */ -int git_odb__hashfd_filtered( - git_oid *out, git_file fd, size_t len, git_otype type, git_vector *filters); - -/* - * Hash a `path`, assuming it could be a POSIX symlink: if the path is a - * symlink, then the raw contents of the symlink will be hashed. Otherwise, - * this will fallback to `git_odb__hashfd`. - * - * The hash type for this call is always `GIT_OBJ_BLOB` because symlinks may - * only point to blobs. - */ -int git_odb__hashlink(git_oid *out, const char *path); - -/* - * Generate a GIT_ENOTFOUND error for the ODB. - */ -int git_odb__error_notfound(const char *message, const git_oid *oid); - -/* - * Generate a GIT_EAMBIGUOUS error for the ODB. - */ -int git_odb__error_ambiguous(const char *message); - -/* - * Attempt to read object header or just return whole object if it could - * not be read. - */ -int git_odb__read_header_or_object( - git_odb_object **out, size_t *len_p, git_otype *type_p, - git_odb *db, const git_oid *id); - -#endif diff --git a/src/odb_loose.c b/src/odb_loose.c deleted file mode 100644 index 68083f7fd35..00000000000 --- a/src/odb_loose.c +++ /dev/null @@ -1,937 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include -#include "git2/object.h" -#include "git2/oid.h" -#include "fileops.h" -#include "hash.h" -#include "odb.h" -#include "delta-apply.h" -#include "filebuf.h" - -#include "git2/odb_backend.h" -#include "git2/types.h" - -typedef struct { /* object header data */ - git_otype type; /* object type */ - size_t size; /* object size */ -} obj_hdr; - -typedef struct { - git_odb_stream stream; - git_filebuf fbuf; -} loose_writestream; - -typedef struct loose_backend { - git_odb_backend parent; - - int object_zlib_level; /** loose object zlib compression level. */ - int fsync_object_files; /** loose object file fsync flag. */ - char *objects_dir; -} loose_backend; - -/* State structure for exploring directories, - * in order to locate objects matching a short oid. - */ -typedef struct { - size_t dir_len; - unsigned char short_oid[GIT_OID_HEXSZ]; /* hex formatted oid to match */ - size_t short_oid_len; - int found; /* number of matching - * objects already found */ - unsigned char res_oid[GIT_OID_HEXSZ]; /* hex formatted oid of - * the object found */ -} loose_locate_object_state; - - -/*********************************************************** - * - * MISCELANEOUS HELPER FUNCTIONS - * - ***********************************************************/ - -static int object_file_name(git_buf *name, const char *dir, const git_oid *id) -{ - git_buf_sets(name, dir); - - /* expand length for 40 hex sha1 chars + 2 * '/' + '\0' */ - if (git_buf_grow(name, git_buf_len(name) + GIT_OID_HEXSZ + 3) < 0) - return -1; - - git_path_to_dir(name); - - /* loose object filename: aa/aaa... (41 bytes) */ - git_oid_pathfmt(name->ptr + git_buf_len(name), id); - name->size += GIT_OID_HEXSZ + 1; - name->ptr[name->size] = '\0'; - - return 0; -} - - -static size_t get_binary_object_header(obj_hdr *hdr, git_buf *obj) -{ - unsigned char c; - unsigned char *data = (unsigned char *)obj->ptr; - size_t shift, size, used = 0; - - if (git_buf_len(obj) == 0) - return 0; - - c = data[used++]; - hdr->type = (c >> 4) & 7; - - size = c & 15; - shift = 4; - while (c & 0x80) { - if (git_buf_len(obj) <= used) - return 0; - if (sizeof(size_t) * 8 <= shift) - return 0; - c = data[used++]; - size += (c & 0x7f) << shift; - shift += 7; - } - hdr->size = size; - - return used; -} - -static size_t get_object_header(obj_hdr *hdr, unsigned char *data) -{ - char c, typename[10]; - size_t size, used = 0; - - /* - * type name string followed by space. - */ - while ((c = data[used]) != ' ') { - typename[used++] = c; - if (used >= sizeof(typename)) - return 0; - } - typename[used] = 0; - if (used == 0) - return 0; - hdr->type = git_object_string2type(typename); - used++; /* consume the space */ - - /* - * length follows immediately in decimal (without - * leading zeros). - */ - size = data[used++] - '0'; - if (size > 9) - return 0; - if (size) { - while ((c = data[used]) != '\0') { - size_t d = c - '0'; - if (d > 9) - break; - used++; - size = size * 10 + d; - } - } - hdr->size = size; - - /* - * the length must be followed by a zero byte - */ - if (data[used++] != '\0') - return 0; - - return used; -} - - - -/*********************************************************** - * - * ZLIB RELATED FUNCTIONS - * - ***********************************************************/ - -static void init_stream(z_stream *s, void *out, size_t len) -{ - memset(s, 0, sizeof(*s)); - s->next_out = out; - s->avail_out = (uInt)len; -} - -static void set_stream_input(z_stream *s, void *in, size_t len) -{ - s->next_in = in; - s->avail_in = (uInt)len; -} - -static void set_stream_output(z_stream *s, void *out, size_t len) -{ - s->next_out = out; - s->avail_out = (uInt)len; -} - - -static int start_inflate(z_stream *s, git_buf *obj, void *out, size_t len) -{ - int status; - - init_stream(s, out, len); - set_stream_input(s, obj->ptr, git_buf_len(obj)); - - if ((status = inflateInit(s)) < Z_OK) - return status; - - return inflate(s, 0); -} - -static int finish_inflate(z_stream *s) -{ - int status = Z_OK; - - while (status == Z_OK) - status = inflate(s, Z_FINISH); - - inflateEnd(s); - - if ((status != Z_STREAM_END) || (s->avail_in != 0)) { - giterr_set(GITERR_ZLIB, "Failed to finish ZLib inflation. Stream aborted prematurely"); - return -1; - } - - return 0; -} - -static int is_zlib_compressed_data(unsigned char *data) -{ - unsigned int w; - - w = ((unsigned int)(data[0]) << 8) + data[1]; - return (data[0] & 0x8F) == 0x08 && !(w % 31); -} - -static int inflate_buffer(void *in, size_t inlen, void *out, size_t outlen) -{ - z_stream zs; - int status = Z_OK; - - memset(&zs, 0x0, sizeof(zs)); - - zs.next_out = out; - zs.avail_out = (uInt)outlen; - - zs.next_in = in; - zs.avail_in = (uInt)inlen; - - if (inflateInit(&zs) < Z_OK) { - giterr_set(GITERR_ZLIB, "Failed to inflate buffer"); - return -1; - } - - while (status == Z_OK) - status = inflate(&zs, Z_FINISH); - - inflateEnd(&zs); - - if (status != Z_STREAM_END /* || zs.avail_in != 0 */ || - zs.total_out != outlen) - { - giterr_set(GITERR_ZLIB, "Failed to inflate buffer. Stream aborted prematurely"); - return -1; - } - - return 0; -} - -static void *inflate_tail(z_stream *s, void *hb, size_t used, obj_hdr *hdr) -{ - unsigned char *buf, *head = hb; - size_t tail; - - /* - * allocate a buffer to hold the inflated data and copy the - * initial sequence of inflated data from the tail of the - * head buffer, if any. - */ - if ((buf = git__malloc(hdr->size + 1)) == NULL) { - inflateEnd(s); - return NULL; - } - tail = s->total_out - used; - if (used > 0 && tail > 0) { - if (tail > hdr->size) - tail = hdr->size; - memcpy(buf, head + used, tail); - } - used = tail; - - /* - * inflate the remainder of the object data, if any - */ - if (hdr->size < used) - inflateEnd(s); - else { - set_stream_output(s, buf + used, hdr->size - used); - if (finish_inflate(s)) { - git__free(buf); - return NULL; - } - } - - return buf; -} - -/* - * At one point, there was a loose object format that was intended to - * mimic the format used in pack-files. This was to allow easy copying - * of loose object data into packs. This format is no longer used, but - * we must still read it. - */ -static int inflate_packlike_loose_disk_obj(git_rawobj *out, git_buf *obj) -{ - unsigned char *in, *buf; - obj_hdr hdr; - size_t len, used; - - /* - * read the object header, which is an (uncompressed) - * binary encoding of the object type and size. - */ - if ((used = get_binary_object_header(&hdr, obj)) == 0 || - !git_object_typeisloose(hdr.type)) { - giterr_set(GITERR_ODB, "Failed to inflate loose object."); - return -1; - } - - /* - * allocate a buffer and inflate the data into it - */ - buf = git__malloc(hdr.size + 1); - GITERR_CHECK_ALLOC(buf); - - in = ((unsigned char *)obj->ptr) + used; - len = obj->size - used; - if (inflate_buffer(in, len, buf, hdr.size) < 0) { - git__free(buf); - return -1; - } - buf[hdr.size] = '\0'; - - out->data = buf; - out->len = hdr.size; - out->type = hdr.type; - - return 0; -} - -static int inflate_disk_obj(git_rawobj *out, git_buf *obj) -{ - unsigned char head[64], *buf; - z_stream zs; - obj_hdr hdr; - size_t used; - - /* - * check for a pack-like loose object - */ - if (!is_zlib_compressed_data((unsigned char *)obj->ptr)) - return inflate_packlike_loose_disk_obj(out, obj); - - /* - * inflate the initial part of the io buffer in order - * to parse the object header (type and size). - */ - if (start_inflate(&zs, obj, head, sizeof(head)) < Z_OK || - (used = get_object_header(&hdr, head)) == 0 || - !git_object_typeisloose(hdr.type)) - { - giterr_set(GITERR_ODB, "Failed to inflate disk object."); - return -1; - } - - /* - * allocate a buffer and inflate the object data into it - * (including the initial sequence in the head buffer). - */ - if ((buf = inflate_tail(&zs, head, used, &hdr)) == NULL) - return -1; - buf[hdr.size] = '\0'; - - out->data = buf; - out->len = hdr.size; - out->type = hdr.type; - - return 0; -} - - - - - - -/*********************************************************** - * - * ODB OBJECT READING & WRITING - * - * Backend for the public API; read headers and full objects - * from the ODB. Write raw data to the ODB. - * - ***********************************************************/ - -static int read_loose(git_rawobj *out, git_buf *loc) -{ - int error; - git_buf obj = GIT_BUF_INIT; - - assert(out && loc); - - if (git_buf_oom(loc)) - return -1; - - out->data = NULL; - out->len = 0; - out->type = GIT_OBJ_BAD; - - if (!(error = git_futils_readbuffer(&obj, loc->ptr))) - error = inflate_disk_obj(out, &obj); - - git_buf_free(&obj); - - return error; -} - -static int read_header_loose(git_rawobj *out, git_buf *loc) -{ - int error = 0, z_return = Z_ERRNO, read_bytes; - git_file fd; - z_stream zs; - obj_hdr header_obj; - unsigned char raw_buffer[16], inflated_buffer[64]; - - assert(out && loc); - - if (git_buf_oom(loc)) - return -1; - - out->data = NULL; - - if ((fd = git_futils_open_ro(loc->ptr)) < 0) - return fd; - - init_stream(&zs, inflated_buffer, sizeof(inflated_buffer)); - - z_return = inflateInit(&zs); - - while (z_return == Z_OK) { - if ((read_bytes = p_read(fd, raw_buffer, sizeof(raw_buffer))) > 0) { - set_stream_input(&zs, raw_buffer, read_bytes); - z_return = inflate(&zs, 0); - } else - z_return = Z_STREAM_END; - } - - if ((z_return != Z_STREAM_END && z_return != Z_BUF_ERROR) - || get_object_header(&header_obj, inflated_buffer) == 0 - || git_object_typeisloose(header_obj.type) == 0) - { - giterr_set(GITERR_ZLIB, "Failed to read loose object header"); - error = -1; - } else { - out->len = header_obj.size; - out->type = header_obj.type; - } - - finish_inflate(&zs); - p_close(fd); - - return error; -} - -static int locate_object( - git_buf *object_location, - loose_backend *backend, - const git_oid *oid) -{ - int error = object_file_name(object_location, backend->objects_dir, oid); - - if (!error && !git_path_exists(object_location->ptr)) - return GIT_ENOTFOUND; - - return error; -} - -/* Explore an entry of a directory and see if it matches a short oid */ -static int fn_locate_object_short_oid(void *state, git_buf *pathbuf) { - loose_locate_object_state *sstate = (loose_locate_object_state *)state; - - if (git_buf_len(pathbuf) - sstate->dir_len != GIT_OID_HEXSZ - 2) { - /* Entry cannot be an object. Continue to next entry */ - return 0; - } - - if (git_path_isdir(pathbuf->ptr) == false) { - /* We are already in the directory matching the 2 first hex characters, - * compare the first ncmp characters of the oids */ - if (!memcmp(sstate->short_oid + 2, - (unsigned char *)pathbuf->ptr + sstate->dir_len, - sstate->short_oid_len - 2)) { - - if (!sstate->found) { - sstate->res_oid[0] = sstate->short_oid[0]; - sstate->res_oid[1] = sstate->short_oid[1]; - memcpy(sstate->res_oid+2, pathbuf->ptr+sstate->dir_len, GIT_OID_HEXSZ-2); - } - sstate->found++; - } - } - - if (sstate->found > 1) - return git_odb__error_ambiguous("multiple matches in loose objects"); - - return 0; -} - -/* Locate an object matching a given short oid */ -static int locate_object_short_oid( - git_buf *object_location, - git_oid *res_oid, - loose_backend *backend, - const git_oid *short_oid, - size_t len) -{ - char *objects_dir = backend->objects_dir; - size_t dir_len = strlen(objects_dir); - loose_locate_object_state state; - int error; - - /* prealloc memory for OBJ_DIR/xx/ */ - if (git_buf_grow(object_location, dir_len + 5) < 0) - return -1; - - git_buf_sets(object_location, objects_dir); - git_path_to_dir(object_location); - - /* save adjusted position at end of dir so it can be restored later */ - dir_len = git_buf_len(object_location); - - /* Convert raw oid to hex formatted oid */ - git_oid_fmt((char *)state.short_oid, short_oid); - - /* Explore OBJ_DIR/xx/ where xx is the beginning of hex formatted short oid */ - if (git_buf_printf(object_location, "%.2s/", state.short_oid) < 0) - return -1; - - /* Check that directory exists */ - if (git_path_isdir(object_location->ptr) == false) - return git_odb__error_notfound("no matching loose object for prefix", short_oid); - - state.dir_len = git_buf_len(object_location); - state.short_oid_len = len; - state.found = 0; - - /* Explore directory to find a unique object matching short_oid */ - error = git_path_direach( - object_location, fn_locate_object_short_oid, &state); - if (error) - return error; - - if (!state.found) - return git_odb__error_notfound("no matching loose object for prefix", short_oid); - - /* Convert obtained hex formatted oid to raw */ - error = git_oid_fromstr(res_oid, (char *)state.res_oid); - if (error) - return error; - - /* Update the location according to the oid obtained */ - - git_buf_truncate(object_location, dir_len); - if (git_buf_grow(object_location, dir_len + GIT_OID_HEXSZ + 2) < 0) - return -1; - - git_oid_pathfmt(object_location->ptr + dir_len, res_oid); - - object_location->size += GIT_OID_HEXSZ + 1; - object_location->ptr[object_location->size] = '\0'; - - return 0; -} - - - - - - - - - -/*********************************************************** - * - * LOOSE BACKEND PUBLIC API - * - * Implement the git_odb_backend API calls - * - ***********************************************************/ - -static int loose_backend__read_header(size_t *len_p, git_otype *type_p, git_odb_backend *backend, const git_oid *oid) -{ - git_buf object_path = GIT_BUF_INIT; - git_rawobj raw; - int error; - - assert(backend && oid); - - raw.len = 0; - raw.type = GIT_OBJ_BAD; - - if (locate_object(&object_path, (loose_backend *)backend, oid) < 0) - error = git_odb__error_notfound("no matching loose object", oid); - else if ((error = read_header_loose(&raw, &object_path)) == 0) { - *len_p = raw.len; - *type_p = raw.type; - } - - git_buf_free(&object_path); - - return error; -} - -static int loose_backend__read(void **buffer_p, size_t *len_p, git_otype *type_p, git_odb_backend *backend, const git_oid *oid) -{ - git_buf object_path = GIT_BUF_INIT; - git_rawobj raw; - int error = 0; - - assert(backend && oid); - - if (locate_object(&object_path, (loose_backend *)backend, oid) < 0) - error = git_odb__error_notfound("no matching loose object", oid); - else if ((error = read_loose(&raw, &object_path)) == 0) { - *buffer_p = raw.data; - *len_p = raw.len; - *type_p = raw.type; - } - - git_buf_free(&object_path); - - return error; -} - -static int loose_backend__read_prefix( - git_oid *out_oid, - void **buffer_p, - size_t *len_p, - git_otype *type_p, - git_odb_backend *backend, - const git_oid *short_oid, - size_t len) -{ - int error = 0; - - if (len < GIT_OID_MINPREFIXLEN) - error = git_odb__error_ambiguous("prefix length too short"); - - else if (len >= GIT_OID_HEXSZ) { - /* We can fall back to regular read method */ - error = loose_backend__read(buffer_p, len_p, type_p, backend, short_oid); - if (!error) - git_oid_cpy(out_oid, short_oid); - } else { - git_buf object_path = GIT_BUF_INIT; - git_rawobj raw; - - assert(backend && short_oid); - - if ((error = locate_object_short_oid(&object_path, out_oid, - (loose_backend *)backend, short_oid, len)) == 0 && - (error = read_loose(&raw, &object_path)) == 0) - { - *buffer_p = raw.data; - *len_p = raw.len; - *type_p = raw.type; - } - - git_buf_free(&object_path); - } - - return error; -} - -static int loose_backend__exists(git_odb_backend *backend, const git_oid *oid) -{ - git_buf object_path = GIT_BUF_INIT; - int error; - - assert(backend && oid); - - error = locate_object(&object_path, (loose_backend *)backend, oid); - - git_buf_free(&object_path); - - return !error; -} - -struct foreach_state { - size_t dir_len; - git_odb_foreach_cb cb; - void *data; - int cb_error; -}; - -GIT_INLINE(int) filename_to_oid(git_oid *oid, const char *ptr) -{ - int v, i = 0; - if (strlen(ptr) != 41) - return -1; - - if (ptr[2] != '/') { - return -1; - } - - v = (git__fromhex(ptr[i]) << 4) | git__fromhex(ptr[i+1]); - if (v < 0) - return -1; - - oid->id[0] = (unsigned char) v; - - ptr += 3; - for (i = 0; i < 38; i += 2) { - v = (git__fromhex(ptr[i]) << 4) | git__fromhex(ptr[i + 1]); - if (v < 0) - return -1; - - oid->id[1 + i/2] = (unsigned char) v; - } - - return 0; -} - -static int foreach_object_dir_cb(void *_state, git_buf *path) -{ - git_oid oid; - struct foreach_state *state = (struct foreach_state *) _state; - - if (filename_to_oid(&oid, path->ptr + state->dir_len) < 0) - return 0; - - if (state->cb(&oid, state->data)) { - state->cb_error = GIT_EUSER; - return -1; - } - - return 0; -} - -static int foreach_cb(void *_state, git_buf *path) -{ - struct foreach_state *state = (struct foreach_state *) _state; - - return git_path_direach(path, foreach_object_dir_cb, state); -} - -static int loose_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb cb, void *data) -{ - char *objects_dir; - int error; - git_buf buf = GIT_BUF_INIT; - struct foreach_state state; - loose_backend *backend = (loose_backend *) _backend; - - assert(backend && cb); - - objects_dir = backend->objects_dir; - - git_buf_sets(&buf, objects_dir); - git_path_to_dir(&buf); - - memset(&state, 0, sizeof(state)); - state.cb = cb; - state.data = data; - state.dir_len = git_buf_len(&buf); - - error = git_path_direach(&buf, foreach_cb, &state); - - git_buf_free(&buf); - - return state.cb_error ? state.cb_error : error; -} - -static int loose_backend__stream_fwrite(git_oid *oid, git_odb_stream *_stream) -{ - loose_writestream *stream = (loose_writestream *)_stream; - loose_backend *backend = (loose_backend *)_stream->backend; - git_buf final_path = GIT_BUF_INIT; - int error = 0; - - if (git_filebuf_hash(oid, &stream->fbuf) < 0 || - object_file_name(&final_path, backend->objects_dir, oid) < 0 || - git_futils_mkpath2file(final_path.ptr, GIT_OBJECT_DIR_MODE) < 0) - error = -1; - /* - * Don't try to add an existing object to the repository. This - * is what git does and allows us to sidestep the fact that - * we're not allowed to overwrite a read-only file on Windows. - */ - else if (git_path_exists(final_path.ptr) == true) - git_filebuf_cleanup(&stream->fbuf); - else - error = git_filebuf_commit_at( - &stream->fbuf, final_path.ptr, GIT_OBJECT_FILE_MODE); - - git_buf_free(&final_path); - - return error; -} - -static int loose_backend__stream_write(git_odb_stream *_stream, const char *data, size_t len) -{ - loose_writestream *stream = (loose_writestream *)_stream; - return git_filebuf_write(&stream->fbuf, data, len); -} - -static void loose_backend__stream_free(git_odb_stream *_stream) -{ - loose_writestream *stream = (loose_writestream *)_stream; - - git_filebuf_cleanup(&stream->fbuf); - git__free(stream); -} - -static int format_object_header(char *hdr, size_t n, size_t obj_len, git_otype obj_type) -{ - const char *type_str = git_object_type2string(obj_type); - int len = snprintf(hdr, n, "%s %"PRIuZ, type_str, obj_len); - - assert(len > 0); /* otherwise snprintf() is broken */ - assert(((size_t)len) < n); /* otherwise the caller is broken! */ - - return len+1; -} - -static int loose_backend__stream(git_odb_stream **stream_out, git_odb_backend *_backend, size_t length, git_otype type) -{ - loose_backend *backend; - loose_writestream *stream = NULL; - char hdr[64]; - git_buf tmp_path = GIT_BUF_INIT; - int hdrlen; - - assert(_backend); - - backend = (loose_backend *)_backend; - *stream_out = NULL; - - hdrlen = format_object_header(hdr, sizeof(hdr), length, type); - - stream = git__calloc(1, sizeof(loose_writestream)); - GITERR_CHECK_ALLOC(stream); - - stream->stream.backend = _backend; - stream->stream.read = NULL; /* read only */ - stream->stream.write = &loose_backend__stream_write; - stream->stream.finalize_write = &loose_backend__stream_fwrite; - stream->stream.free = &loose_backend__stream_free; - stream->stream.mode = GIT_STREAM_WRONLY; - - if (git_buf_joinpath(&tmp_path, backend->objects_dir, "tmp_object") < 0 || - git_filebuf_open(&stream->fbuf, tmp_path.ptr, - GIT_FILEBUF_HASH_CONTENTS | - GIT_FILEBUF_TEMPORARY | - (backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT)) < 0 || - stream->stream.write((git_odb_stream *)stream, hdr, hdrlen) < 0) - { - git_filebuf_cleanup(&stream->fbuf); - git__free(stream); - stream = NULL; - } - git_buf_free(&tmp_path); - *stream_out = (git_odb_stream *)stream; - - return !stream ? -1 : 0; -} - -static int loose_backend__write(git_oid *oid, git_odb_backend *_backend, const void *data, size_t len, git_otype type) -{ - int error = 0, header_len; - git_buf final_path = GIT_BUF_INIT; - char header[64]; - git_filebuf fbuf = GIT_FILEBUF_INIT; - loose_backend *backend; - - backend = (loose_backend *)_backend; - - /* prepare the header for the file */ - header_len = format_object_header(header, sizeof(header), len, type); - - if (git_buf_joinpath(&final_path, backend->objects_dir, "tmp_object") < 0 || - git_filebuf_open(&fbuf, final_path.ptr, - GIT_FILEBUF_TEMPORARY | - (backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT)) < 0) - { - error = -1; - goto cleanup; - } - - git_filebuf_write(&fbuf, header, header_len); - git_filebuf_write(&fbuf, data, len); - - if (object_file_name(&final_path, backend->objects_dir, oid) < 0 || - git_futils_mkpath2file(final_path.ptr, GIT_OBJECT_DIR_MODE) < 0 || - git_filebuf_commit_at(&fbuf, final_path.ptr, GIT_OBJECT_FILE_MODE) < 0) - error = -1; - -cleanup: - if (error < 0) - git_filebuf_cleanup(&fbuf); - git_buf_free(&final_path); - return error; -} - -static void loose_backend__free(git_odb_backend *_backend) -{ - loose_backend *backend; - assert(_backend); - backend = (loose_backend *)_backend; - - git__free(backend->objects_dir); - git__free(backend); -} - -int git_odb_backend_loose( - git_odb_backend **backend_out, - const char *objects_dir, - int compression_level, - int do_fsync) -{ - loose_backend *backend; - - backend = git__calloc(1, sizeof(loose_backend)); - GITERR_CHECK_ALLOC(backend); - - backend->parent.version = GIT_ODB_BACKEND_VERSION; - backend->objects_dir = git__strdup(objects_dir); - GITERR_CHECK_ALLOC(backend->objects_dir); - - if (compression_level < 0) - compression_level = Z_BEST_SPEED; - - backend->object_zlib_level = compression_level; - backend->fsync_object_files = do_fsync; - - backend->parent.read = &loose_backend__read; - backend->parent.write = &loose_backend__write; - backend->parent.read_prefix = &loose_backend__read_prefix; - backend->parent.read_header = &loose_backend__read_header; - backend->parent.writestream = &loose_backend__stream; - backend->parent.exists = &loose_backend__exists; - backend->parent.foreach = &loose_backend__foreach; - backend->parent.free = &loose_backend__free; - - *backend_out = (git_odb_backend *)backend; - return 0; -} diff --git a/src/odb_pack.c b/src/odb_pack.c deleted file mode 100644 index 9779ecd255c..00000000000 --- a/src/odb_pack.c +++ /dev/null @@ -1,625 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include -#include "git2/repository.h" -#include "git2/oid.h" -#include "fileops.h" -#include "hash.h" -#include "odb.h" -#include "delta-apply.h" -#include "sha1_lookup.h" -#include "mwindow.h" -#include "pack.h" - -#include "git2/odb_backend.h" - -struct pack_backend { - git_odb_backend parent; - git_vector packs; - struct git_pack_file *last_found; - char *pack_folder; -}; - -struct pack_writepack { - struct git_odb_writepack parent; - git_indexer_stream *indexer_stream; -}; - -/** - * The wonderful tale of a Packed Object lookup query - * =================================================== - * A riveting and epic story of epicness and ASCII - * art, presented by yours truly, - * Sir Vicent of Marti - * - * - * Chapter 1: Once upon a time... - * Initialization of the Pack Backend - * -------------------------------------------------- - * - * # git_odb_backend_pack - * | Creates the pack backend structure, initializes the - * | callback pointers to our default read() and exist() methods, - * | and tries to preload all the known packfiles in the ODB. - * | - * |-# packfile_load_all - * | Tries to find the `pack` folder, if it exists. ODBs without - * | a pack folder are ignored altogether. If there's a `pack` folder - * | we run a `dirent` callback through every file in the pack folder - * | to find our packfiles. The packfiles are then sorted according - * | to a sorting callback. - * | - * |-# packfile_load__cb - * | | This callback is called from `dirent` with every single file - * | | inside the pack folder. We find the packs by actually locating - * | | their index (ends in ".idx"). From that index, we verify that - * | | the corresponding packfile exists and is valid, and if so, we - * | | add it to the pack list. - * | | - * | |-# packfile_check - * | Make sure that there's a packfile to back this index, and store - * | some very basic information regarding the packfile itself, - * | such as the full path, the size, and the modification time. - * | We don't actually open the packfile to check for internal consistency. - * | - * |-# packfile_sort__cb - * Sort all the preloaded packs according to some specific criteria: - * we prioritize the "newer" packs because it's more likely they - * contain the objects we are looking for, and we prioritize local - * packs over remote ones. - * - * - * - * Chapter 2: To be, or not to be... - * A standard packed `exist` query for an OID - * -------------------------------------------------- - * - * # pack_backend__exists - * | Check if the given SHA1 oid exists in any of the packs - * | that have been loaded for our ODB. - * | - * |-# pack_entry_find - * | Iterate through all the packs that have been preloaded - * | (starting by the pack where the latest object was found) - * | to try to find the OID in one of them. - * | - * |-# pack_entry_find1 - * | Check the index of an individual pack to see if the SHA1 - * | OID can be found. If we can find the offset to that SHA1 - * | inside of the index, that means the object is contained - * | inside of the packfile and we can stop searching. - * | Before returning, we verify that the packfile behing the - * | index we are searching still exists on disk. - * | - * |-# pack_entry_find_offset - * | | Mmap the actual index file to disk if it hasn't been opened - * | | yet, and run a binary search through it to find the OID. - * | | See for specifics - * | | on the Packfile Index format and how do we find entries in it. - * | | - * | |-# pack_index_open - * | | Guess the name of the index based on the full path to the - * | | packfile, open it and verify its contents. Only if the index - * | | has not been opened already. - * | | - * | |-# pack_index_check - * | Mmap the index file and do a quick run through the header - * | to guess the index version (right now we support v1 and v2), - * | and to verify that the size of the index makes sense. - * | - * |-# packfile_open - * See `packfile_open` in Chapter 3 - * - * - * - * Chapter 3: The neverending story... - * A standard packed `lookup` query for an OID - * -------------------------------------------------- - * TODO - * - */ - - -/*********************************************************** - * - * FORWARD DECLARATIONS - * - ***********************************************************/ - -static void pack_window_free_all(struct pack_backend *backend, struct git_pack_file *p); -static int pack_window_contains(git_mwindow *win, off_t offset); - -static int packfile_sort__cb(const void *a_, const void *b_); - -static int packfile_load__cb(void *_data, git_buf *path); - -static int pack_entry_find(struct git_pack_entry *e, - struct pack_backend *backend, const git_oid *oid); - -/* Can find the offset of an object given - * a prefix of an identifier. - * Sets GIT_EAMBIGUOUS if short oid is ambiguous. - * This method assumes that len is between - * GIT_OID_MINPREFIXLEN and GIT_OID_HEXSZ. - */ -static int pack_entry_find_prefix( - struct git_pack_entry *e, - struct pack_backend *backend, - const git_oid *short_oid, - size_t len); - - - -/*********************************************************** - * - * PACK WINDOW MANAGEMENT - * - ***********************************************************/ - -GIT_INLINE(void) pack_window_free_all(struct pack_backend *backend, struct git_pack_file *p) -{ - GIT_UNUSED(backend); - git_mwindow_free_all(&p->mwf); -} - -GIT_INLINE(int) pack_window_contains(git_mwindow *win, off_t offset) -{ - /* We must promise at least 20 bytes (one hash) after the - * offset is available from this window, otherwise the offset - * is not actually in this window and a different window (which - * has that one hash excess) must be used. This is to support - * the object header and delta base parsing routines below. - */ - return git_mwindow_contains(win, offset + 20); -} - -static int packfile_sort__cb(const void *a_, const void *b_) -{ - const struct git_pack_file *a = a_; - const struct git_pack_file *b = b_; - int st; - - /* - * Local packs tend to contain objects specific to our - * variant of the project than remote ones. In addition, - * remote ones could be on a network mounted filesystem. - * Favor local ones for these reasons. - */ - st = a->pack_local - b->pack_local; - if (st) - return -st; - - /* - * Younger packs tend to contain more recent objects, - * and more recent objects tend to get accessed more - * often. - */ - if (a->mtime < b->mtime) - return 1; - else if (a->mtime == b->mtime) - return 0; - - return -1; -} - - - -static int packfile_load__cb(void *_data, git_buf *path) -{ - struct pack_backend *backend = (struct pack_backend *)_data; - struct git_pack_file *pack; - int error; - unsigned int i; - - if (git__suffixcmp(path->ptr, ".idx") != 0) - return 0; /* not an index */ - - for (i = 0; i < backend->packs.length; ++i) { - struct git_pack_file *p = git_vector_get(&backend->packs, i); - if (memcmp(p->pack_name, git_buf_cstr(path), git_buf_len(path) - strlen(".idx")) == 0) - return 0; - } - - error = git_packfile_check(&pack, path->ptr); - if (error == GIT_ENOTFOUND) - /* ignore missing .pack file as git does */ - return 0; - else if (error < 0) - return error; - - return git_vector_insert(&backend->packs, pack); -} - -static int pack_entry_find_inner( - struct git_pack_entry *e, - struct pack_backend *backend, - const git_oid *oid, - struct git_pack_file *last_found) -{ - unsigned int i; - - if (last_found && - git_pack_entry_find(e, last_found, oid, GIT_OID_HEXSZ) == 0) - return 0; - - for (i = 0; i < backend->packs.length; ++i) { - struct git_pack_file *p; - - p = git_vector_get(&backend->packs, i); - if (p == last_found) - continue; - - if (git_pack_entry_find(e, p, oid, GIT_OID_HEXSZ) == 0) { - backend->last_found = p; - return 0; - } - } - - return -1; -} - -static int pack_entry_find(struct git_pack_entry *e, struct pack_backend *backend, const git_oid *oid) -{ - struct git_pack_file *last_found = backend->last_found; - - if (backend->last_found && - git_pack_entry_find(e, backend->last_found, oid, GIT_OID_HEXSZ) == 0) - return 0; - - if (!pack_entry_find_inner(e, backend, oid, last_found)) - return 0; - - return git_odb__error_notfound("failed to find pack entry", oid); -} - -static unsigned pack_entry_find_prefix_inner( - struct git_pack_entry *e, - struct pack_backend *backend, - const git_oid *short_oid, - size_t len, - struct git_pack_file *last_found) -{ - int error; - unsigned int i; - unsigned found = 0; - - if (last_found) { - error = git_pack_entry_find(e, last_found, short_oid, len); - if (error == GIT_EAMBIGUOUS) - return error; - if (!error) - found = 1; - } - - for (i = 0; i < backend->packs.length; ++i) { - struct git_pack_file *p; - - p = git_vector_get(&backend->packs, i); - if (p == last_found) - continue; - - error = git_pack_entry_find(e, p, short_oid, len); - if (error == GIT_EAMBIGUOUS) - return error; - if (!error) { - if (++found > 1) - break; - backend->last_found = p; - } - } - - return found; -} - -static int pack_entry_find_prefix( - struct git_pack_entry *e, - struct pack_backend *backend, - const git_oid *short_oid, - size_t len) -{ - struct git_pack_file *last_found = backend->last_found; - unsigned int found = pack_entry_find_prefix_inner(e, backend, short_oid, len, last_found); - - if (!found) - return git_odb__error_notfound("no matching pack entry for prefix", short_oid); - else if (found > 1) - return git_odb__error_ambiguous("found multiple pack entries"); - else - return 0; -} - - -/*********************************************************** - * - * PACKED BACKEND PUBLIC API - * - * Implement the git_odb_backend API calls - * - ***********************************************************/ -static int pack_backend__refresh(git_odb_backend *_backend) -{ - struct pack_backend *backend = (struct pack_backend *)_backend; - - int error; - struct stat st; - git_buf path = GIT_BUF_INIT; - - if (backend->pack_folder == NULL) - return 0; - - if (p_stat(backend->pack_folder, &st) < 0 || !S_ISDIR(st.st_mode)) - return git_odb__error_notfound("failed to refresh packfiles", NULL); - - git_buf_sets(&path, backend->pack_folder); - - /* reload all packs */ - error = git_path_direach(&path, packfile_load__cb, (void *)backend); - - git_buf_free(&path); - - if (error < 0) - return error; - - git_vector_sort(&backend->packs); - return 0; -} - - -static int pack_backend__read_header(size_t *len_p, git_otype *type_p, struct git_odb_backend *backend, const git_oid *oid) -{ - struct git_pack_entry e; - int error; - - assert(len_p && type_p && backend && oid); - - if ((error = pack_entry_find(&e, (struct pack_backend *)backend, oid)) < 0) - return error; - - return git_packfile_resolve_header(len_p, type_p, e.p, e.offset); -} - -static int pack_backend__read(void **buffer_p, size_t *len_p, git_otype *type_p, git_odb_backend *backend, const git_oid *oid) -{ - struct git_pack_entry e; - git_rawobj raw; - int error; - - if ((error = pack_entry_find(&e, (struct pack_backend *)backend, oid)) < 0 || - (error = git_packfile_unpack(&raw, e.p, &e.offset)) < 0) - return error; - - *buffer_p = raw.data; - *len_p = raw.len; - *type_p = raw.type; - - return 0; -} - -static int pack_backend__read_prefix( - git_oid *out_oid, - void **buffer_p, - size_t *len_p, - git_otype *type_p, - git_odb_backend *backend, - const git_oid *short_oid, - size_t len) -{ - int error = 0; - - if (len < GIT_OID_MINPREFIXLEN) - error = git_odb__error_ambiguous("prefix length too short"); - - else if (len >= GIT_OID_HEXSZ) { - /* We can fall back to regular read method */ - error = pack_backend__read(buffer_p, len_p, type_p, backend, short_oid); - if (!error) - git_oid_cpy(out_oid, short_oid); - } else { - struct git_pack_entry e; - git_rawobj raw; - - if ((error = pack_entry_find_prefix( - &e, (struct pack_backend *)backend, short_oid, len)) == 0 && - (error = git_packfile_unpack(&raw, e.p, &e.offset)) == 0) - { - *buffer_p = raw.data; - *len_p = raw.len; - *type_p = raw.type; - git_oid_cpy(out_oid, &e.sha1); - } - } - - return error; -} - -static int pack_backend__exists(git_odb_backend *backend, const git_oid *oid) -{ - struct git_pack_entry e; - return pack_entry_find(&e, (struct pack_backend *)backend, oid) == 0; -} - -static int pack_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb cb, void *data) -{ - int error; - struct git_pack_file *p; - struct pack_backend *backend; - unsigned int i; - - assert(_backend && cb); - backend = (struct pack_backend *)_backend; - - /* Make sure we know about the packfiles */ - if ((error = pack_backend__refresh(_backend)) < 0) - return error; - - git_vector_foreach(&backend->packs, i, p) { - if ((error = git_pack_foreach_entry(p, cb, data)) < 0) - return error; - } - - return 0; -} - -static int pack_backend__writepack_add(struct git_odb_writepack *_writepack, const void *data, size_t size, git_transfer_progress *stats) -{ - struct pack_writepack *writepack = (struct pack_writepack *)_writepack; - - assert(writepack); - - return git_indexer_stream_add(writepack->indexer_stream, data, size, stats); -} - -static int pack_backend__writepack_commit(struct git_odb_writepack *_writepack, git_transfer_progress *stats) -{ - struct pack_writepack *writepack = (struct pack_writepack *)_writepack; - - assert(writepack); - - return git_indexer_stream_finalize(writepack->indexer_stream, stats); -} - -static void pack_backend__writepack_free(struct git_odb_writepack *_writepack) -{ - struct pack_writepack *writepack = (struct pack_writepack *)_writepack; - - assert(writepack); - - git_indexer_stream_free(writepack->indexer_stream); - git__free(writepack); -} - -static int pack_backend__writepack(struct git_odb_writepack **out, - git_odb_backend *_backend, - git_transfer_progress_callback progress_cb, - void *progress_payload) -{ - struct pack_backend *backend; - struct pack_writepack *writepack; - - assert(out && _backend); - - *out = NULL; - - backend = (struct pack_backend *)_backend; - - writepack = git__calloc(1, sizeof(struct pack_writepack)); - GITERR_CHECK_ALLOC(writepack); - - if (git_indexer_stream_new(&writepack->indexer_stream, - backend->pack_folder, progress_cb, progress_payload) < 0) { - git__free(writepack); - return -1; - } - - writepack->parent.backend = _backend; - writepack->parent.add = pack_backend__writepack_add; - writepack->parent.commit = pack_backend__writepack_commit; - writepack->parent.free = pack_backend__writepack_free; - - *out = (git_odb_writepack *)writepack; - - return 0; -} - -static void pack_backend__free(git_odb_backend *_backend) -{ - struct pack_backend *backend; - unsigned int i; - - assert(_backend); - - backend = (struct pack_backend *)_backend; - - for (i = 0; i < backend->packs.length; ++i) { - struct git_pack_file *p = git_vector_get(&backend->packs, i); - git_packfile_free(p); - } - - git_vector_free(&backend->packs); - git__free(backend->pack_folder); - git__free(backend); -} - -int git_odb_backend_one_pack(git_odb_backend **backend_out, const char *idx) -{ - struct pack_backend *backend = NULL; - struct git_pack_file *packfile = NULL; - - if (git_packfile_check(&packfile, idx) < 0) - return -1; - - backend = git__calloc(1, sizeof(struct pack_backend)); - GITERR_CHECK_ALLOC(backend); - backend->parent.version = GIT_ODB_BACKEND_VERSION; - - if (git_vector_init(&backend->packs, 1, NULL) < 0) - goto on_error; - - if (git_vector_insert(&backend->packs, packfile) < 0) - goto on_error; - - backend->parent.read = &pack_backend__read; - backend->parent.read_prefix = &pack_backend__read_prefix; - backend->parent.read_header = &pack_backend__read_header; - backend->parent.exists = &pack_backend__exists; - backend->parent.refresh = &pack_backend__refresh; - backend->parent.foreach = &pack_backend__foreach; - backend->parent.free = &pack_backend__free; - - *backend_out = (git_odb_backend *)backend; - - return 0; - -on_error: - git_vector_free(&backend->packs); - git__free(backend); - git__free(packfile); - return -1; -} - -int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir) -{ - struct pack_backend *backend = NULL; - git_buf path = GIT_BUF_INIT; - - backend = git__calloc(1, sizeof(struct pack_backend)); - GITERR_CHECK_ALLOC(backend); - backend->parent.version = GIT_ODB_BACKEND_VERSION; - - if (git_vector_init(&backend->packs, 8, packfile_sort__cb) < 0 || - git_buf_joinpath(&path, objects_dir, "pack") < 0) - { - git__free(backend); - return -1; - } - - if (git_path_isdir(git_buf_cstr(&path)) == true) { - int error; - - backend->pack_folder = git_buf_detach(&path); - error = pack_backend__refresh((git_odb_backend *)backend); - if (error < 0) - return error; - } - - backend->parent.read = &pack_backend__read; - backend->parent.read_prefix = &pack_backend__read_prefix; - backend->parent.read_header = &pack_backend__read_header; - backend->parent.exists = &pack_backend__exists; - backend->parent.refresh = &pack_backend__refresh; - backend->parent.foreach = &pack_backend__foreach; - backend->parent.writepack = &pack_backend__writepack; - backend->parent.free = &pack_backend__free; - - *backend_out = (git_odb_backend *)backend; - - git_buf_free(&path); - - return 0; -} diff --git a/src/offmap.h b/src/offmap.h deleted file mode 100644 index cd46fd687e5..00000000000 --- a/src/offmap.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (C) 2012 the libgit2 contributors - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_offmap_h__ -#define INCLUDE_offmap_h__ - -#include "common.h" -#include "git2/types.h" - -#define kmalloc git__malloc -#define kcalloc git__calloc -#define krealloc git__realloc -#define kfree git__free -#include "khash.h" - -__KHASH_TYPE(off, git_off_t, void *); -typedef khash_t(off) git_offmap; - -#define GIT__USE_OFFMAP \ - __KHASH_IMPL(off, static kh_inline, git_off_t, void *, 1, kh_int64_hash_func, kh_int64_hash_equal); - -#define git_offmap_alloc() kh_init(off) -#define git_offmap_free(h) kh_destroy(off, h), h = NULL -#define git_offmap_clear(h) kh_clear(off, h) - -#define git_offmap_num_entries(h) kh_size(h) - -#define git_offmap_lookup_index(h, k) kh_get(off, h, k) -#define git_offmap_valid_index(h, idx) (idx != kh_end(h)) - -#define git_offmap_exists(h, k) (kh_get(off, h, k) != kh_end(h)) - -#define git_offmap_value_at(h, idx) kh_val(h, idx) -#define git_offmap_set_value_at(h, idx, v) kh_val(h, idx) = v -#define git_offmap_delete_at(h, idx) kh_del(off, h, idx) - -#define git_offmap_insert(h, key, val, rval) do { \ - khiter_t __pos = kh_put(off, h, key, &rval); \ - if (rval >= 0) { \ - if (rval == 0) kh_key(h, __pos) = key; \ - kh_val(h, __pos) = val; \ - } } while (0) - -#define git_offmap_insert2(h, key, val, oldv, rval) do { \ - khiter_t __pos = kh_put(off, h, key, &rval); \ - if (rval >= 0) { \ - if (rval == 0) { \ - oldv = kh_val(h, __pos); \ - kh_key(h, __pos) = key; \ - } else { oldv = NULL; } \ - kh_val(h, __pos) = val; \ - } } while (0) - -#define git_offmap_delete(h, key) do { \ - khiter_t __pos = git_offmap_lookup_index(h, key); \ - if (git_offmap_valid_index(h, __pos)) \ - git_offmap_delete_at(h, __pos); } while (0) - -#define git_offmap_foreach kh_foreach -#define git_offmap_foreach_value kh_foreach_value - -#endif diff --git a/src/oid.c b/src/oid.c deleted file mode 100644 index 25c6fce22fe..00000000000 --- a/src/oid.c +++ /dev/null @@ -1,382 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "git2/oid.h" -#include "repository.h" -#include -#include - -static char to_hex[] = "0123456789abcdef"; - -static int oid_error_invalid(const char *msg) -{ - giterr_set(GITERR_INVALID, "Unable to parse OID - %s", msg); - return -1; -} - -int git_oid_fromstrn(git_oid *out, const char *str, size_t length) -{ - size_t p; - int v; - - if (length > GIT_OID_HEXSZ) - length = GIT_OID_HEXSZ; - - for (p = 0; p < length - 1; p += 2) { - v = (git__fromhex(str[p + 0]) << 4) - | git__fromhex(str[p + 1]); - - if (v < 0) - return oid_error_invalid("contains invalid characters"); - - out->id[p / 2] = (unsigned char)v; - } - - if (length % 2) { - v = (git__fromhex(str[p + 0]) << 4); - if (v < 0) - return oid_error_invalid("contains invalid characters"); - - out->id[p / 2] = (unsigned char)v; - p += 2; - } - - memset(out->id + p / 2, 0, (GIT_OID_HEXSZ - p) / 2); - - return 0; -} - -int git_oid_fromstr(git_oid *out, const char *str) -{ - return git_oid_fromstrn(out, str, GIT_OID_HEXSZ); -} - -GIT_INLINE(char) *fmt_one(char *str, unsigned int val) -{ - *str++ = to_hex[val >> 4]; - *str++ = to_hex[val & 0xf]; - return str; -} - -void git_oid_fmt(char *str, const git_oid *oid) -{ - size_t i; - - for (i = 0; i < sizeof(oid->id); i++) - str = fmt_one(str, oid->id[i]); -} - -void git_oid_pathfmt(char *str, const git_oid *oid) -{ - size_t i; - - str = fmt_one(str, oid->id[0]); - *str++ = '/'; - for (i = 1; i < sizeof(oid->id); i++) - str = fmt_one(str, oid->id[i]); -} - -char *git_oid_allocfmt(const git_oid *oid) -{ - char *str = git__malloc(GIT_OID_HEXSZ + 1); - if (!str) - return NULL; - git_oid_fmt(str, oid); - str[GIT_OID_HEXSZ] = '\0'; - return str; -} - -char *git_oid_tostr(char *out, size_t n, const git_oid *oid) -{ - char str[GIT_OID_HEXSZ]; - - if (!out || n == 0) - return ""; - - n--; /* allow room for terminating NUL */ - - if (oid == NULL) - n = 0; - - if (n > 0) { - git_oid_fmt(str, oid); - if (n > GIT_OID_HEXSZ) - n = GIT_OID_HEXSZ; - memcpy(out, str, n); - } - - out[n] = '\0'; - - return out; -} - -int git_oid__parse( - git_oid *oid, const char **buffer_out, - const char *buffer_end, const char *header) -{ - const size_t sha_len = GIT_OID_HEXSZ; - const size_t header_len = strlen(header); - - const char *buffer = *buffer_out; - - if (buffer + (header_len + sha_len + 1) > buffer_end) - return -1; - - if (memcmp(buffer, header, header_len) != 0) - return -1; - - if (buffer[header_len + sha_len] != '\n') - return -1; - - if (git_oid_fromstr(oid, buffer + header_len) < 0) - return -1; - - *buffer_out = buffer + (header_len + sha_len + 1); - - return 0; -} - -void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid) -{ - char hex_oid[GIT_OID_HEXSZ]; - - git_oid_fmt(hex_oid, oid); - git_buf_puts(buf, header); - git_buf_put(buf, hex_oid, GIT_OID_HEXSZ); - git_buf_putc(buf, '\n'); -} - -void git_oid_fromraw(git_oid *out, const unsigned char *raw) -{ - memcpy(out->id, raw, sizeof(out->id)); -} - -void git_oid_cpy(git_oid *out, const git_oid *src) -{ - memcpy(out->id, src->id, sizeof(out->id)); -} - -int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, size_t len) -{ - const unsigned char *a = oid_a->id; - const unsigned char *b = oid_b->id; - - do { - if (*a != *b) - return 1; - a++; - b++; - len -= 2; - } while (len > 1); - - if (len) - if ((*a ^ *b) & 0xf0) - return 1; - - return 0; -} - -int git_oid_streq(const git_oid *a, const char *str) -{ - git_oid id; - - if (git_oid_fromstr(&id, str) < 0) - return -1; - - return git_oid_cmp(a, &id) == 0 ? 0 : -1; -} - -int git_oid_iszero(const git_oid *oid_a) -{ - const unsigned char *a = oid_a->id; - unsigned int i; - for (i = 0; i < GIT_OID_RAWSZ; ++i, ++a) - if (*a != 0) - return 0; - return 1; -} - -typedef short node_index; - -typedef union { - const char *tail; - node_index children[16]; -} trie_node; - -struct git_oid_shorten { - trie_node *nodes; - size_t node_count, size; - int min_length, full; -}; - -static int resize_trie(git_oid_shorten *self, size_t new_size) -{ - self->nodes = git__realloc(self->nodes, new_size * sizeof(trie_node)); - GITERR_CHECK_ALLOC(self->nodes); - - if (new_size > self->size) { - memset(&self->nodes[self->size], 0x0, (new_size - self->size) * sizeof(trie_node)); - } - - self->size = new_size; - return 0; -} - -static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, const char *oid) -{ - trie_node *node, *leaf; - node_index idx_leaf; - - if (os->node_count >= os->size) { - if (resize_trie(os, os->size * 2) < 0) - return NULL; - } - - idx_leaf = (node_index)os->node_count++; - - if (os->node_count == SHRT_MAX) - os->full = 1; - - node = &os->nodes[idx]; - node->children[push_at] = -idx_leaf; - - leaf = &os->nodes[idx_leaf]; - leaf->tail = oid; - - return node; -} - -git_oid_shorten *git_oid_shorten_new(size_t min_length) -{ - git_oid_shorten *os; - - assert((size_t)((int)min_length) == min_length); - - os = git__calloc(1, sizeof(git_oid_shorten)); - if (os == NULL) - return NULL; - - if (resize_trie(os, 16) < 0) { - git__free(os); - return NULL; - } - - os->node_count = 1; - os->min_length = (int)min_length; - - return os; -} - -void git_oid_shorten_free(git_oid_shorten *os) -{ - git__free(os->nodes); - git__free(os); -} - - -/* - * What wizardry is this? - * - * This is just a memory-optimized trie: basically a very fancy - * 16-ary tree, which is used to store the prefixes of the OID - * strings. - * - * Read more: http://en.wikipedia.org/wiki/Trie - * - * Magic that happens in this method: - * - * - Each node in the trie is an union, so it can work both as - * a normal node, or as a leaf. - * - * - Each normal node points to 16 children (one for each possible - * character in the oid). This is *not* stored in an array of - * pointers, because in a 64-bit arch this would be sucking - * 16*sizeof(void*) = 128 bytes of memory per node, which is - * insane. What we do is store Node Indexes, and use these indexes - * to look up each node in the om->index array. These indexes are - * signed shorts, so this limits the amount of unique OIDs that - * fit in the structure to about 20000 (assuming a more or less uniform - * distribution). - * - * - All the nodes in om->index array are stored contiguously in - * memory, and each of them is 32 bytes, so we fit 2x nodes per - * cache line. Convenient for speed. - * - * - To differentiate the leafs from the normal nodes, we store all - * the indexes towards a leaf as a negative index (indexes to normal - * nodes are positives). When we find that one of the children for - * a node has a negative value, that means it's going to be a leaf. - * This reduces the amount of indexes we have by two, but also reduces - * the size of each node by 1-4 bytes (the amount we would need to - * add a `is_leaf` field): this is good because it allows the nodes - * to fit cleanly in cache lines. - * - * - Once we reach an empty children, instead of continuing to insert - * new nodes for each remaining character of the OID, we store a pointer - * to the tail in the leaf; if the leaf is reached again, we turn it - * into a normal node and use the tail to create a new leaf. - * - * This is a pretty good balance between performance and memory usage. - */ -int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid) -{ - int i; - bool is_leaf; - node_index idx; - - if (os->full) - return -1; - - if (text_oid == NULL) - return os->min_length; - - idx = 0; - is_leaf = false; - - for (i = 0; i < GIT_OID_HEXSZ; ++i) { - int c = git__fromhex(text_oid[i]); - trie_node *node; - - if (c == -1) { - giterr_set(GITERR_INVALID, "Unable to shorten OID - invalid hex value"); - return -1; - } - - node = &os->nodes[idx]; - - if (is_leaf) { - const char *tail; - - tail = node->tail; - node->tail = NULL; - - node = push_leaf(os, idx, git__fromhex(tail[0]), &tail[1]); - GITERR_CHECK_ALLOC(node); - } - - if (node->children[c] == 0) { - if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL) - return -1; - break; - } - - idx = node->children[c]; - is_leaf = false; - - if (idx < 0) { - node->children[c] = idx = -idx; - is_leaf = true; - } - } - - if (++i > os->min_length) - os->min_length = i; - - return os->min_length; -} - diff --git a/src/oidmap.h b/src/oidmap.h deleted file mode 100644 index 40274cd194f..00000000000 --- a/src/oidmap.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_oidmap_h__ -#define INCLUDE_oidmap_h__ - -#include "common.h" -#include "git2/oid.h" - -#define kmalloc git__malloc -#define kcalloc git__calloc -#define krealloc git__realloc -#define kfree git__free -#include "khash.h" - -__KHASH_TYPE(oid, const git_oid *, void *); -typedef khash_t(oid) git_oidmap; - -GIT_INLINE(khint_t) hash_git_oid(const git_oid *oid) -{ - int i; - khint_t h = 0; - for (i = 0; i < 20; ++i) - h = (h << 5) - h + oid->id[i]; - return h; -} - -#define GIT__USE_OIDMAP \ - __KHASH_IMPL(oid, static kh_inline, const git_oid *, void *, 1, hash_git_oid, git_oid_equal) - -#define git_oidmap_alloc() kh_init(oid) -#define git_oidmap_free(h) kh_destroy(oid,h), h = NULL - -#endif diff --git a/src/pack-objects.c b/src/pack-objects.c deleted file mode 100644 index a76f8a11150..00000000000 --- a/src/pack-objects.c +++ /dev/null @@ -1,1337 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "pack-objects.h" - -#include "compress.h" -#include "delta.h" -#include "iterator.h" -#include "netops.h" -#include "pack.h" -#include "thread-utils.h" -#include "tree.h" - -#include "git2/pack.h" -#include "git2/commit.h" -#include "git2/tag.h" -#include "git2/indexer.h" -#include "git2/config.h" - -GIT__USE_OIDMAP; - -struct unpacked { - git_pobject *object; - void *data; - struct git_delta_index *index; - unsigned int depth; -}; - -struct tree_walk_context { - git_packbuilder *pb; - git_buf buf; -}; - -#ifdef GIT_THREADS - -#define GIT_PACKBUILDER__MUTEX_OP(pb, mtx, op) do { \ - int result = git_mutex_##op(&(pb)->mtx); \ - assert(!result); \ - GIT_UNUSED(result); \ - } while (0) - -#else - -#define GIT_PACKBUILDER__MUTEX_OP(pb,mtx,op) GIT_UNUSED(pb) - -#endif /* GIT_THREADS */ - -#define git_packbuilder__cache_lock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, cache_mutex, lock) -#define git_packbuilder__cache_unlock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, cache_mutex, unlock) -#define git_packbuilder__progress_lock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, progress_mutex, lock) -#define git_packbuilder__progress_unlock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, progress_mutex, unlock) - -static unsigned name_hash(const char *name) -{ - unsigned c, hash = 0; - - if (!name) - return 0; - - /* - * This effectively just creates a sortable number from the - * last sixteen non-whitespace characters. Last characters - * count "most", so things that end in ".c" sort together. - */ - while ((c = *name++) != 0) { - if (git__isspace(c)) - continue; - hash = (hash >> 2) + (c << 24); - } - return hash; -} - -static int packbuilder_config(git_packbuilder *pb) -{ - git_config *config; - int ret; - int64_t val; - - if (git_repository_config__weakptr(&config, pb->repo) < 0) - return -1; - -#define config_get(KEY,DST,DFLT) do { \ - ret = git_config_get_int64(&val, config, KEY); \ - if (!ret) (DST) = val; \ - else if (ret == GIT_ENOTFOUND) (DST) = (DFLT); \ - else if (ret < 0) return -1; } while (0) - - config_get("pack.deltaCacheSize", pb->max_delta_cache_size, - GIT_PACK_DELTA_CACHE_SIZE); - config_get("pack.deltaCacheLimit", pb->cache_max_small_delta_size, - GIT_PACK_DELTA_CACHE_LIMIT); - config_get("pack.deltaCacheSize", pb->big_file_threshold, - GIT_PACK_BIG_FILE_THRESHOLD); - config_get("pack.windowMemory", pb->window_memory_limit, 0); - -#undef config_get - - return 0; -} - -int git_packbuilder_new(git_packbuilder **out, git_repository *repo) -{ - git_packbuilder *pb; - - *out = NULL; - - pb = git__calloc(1, sizeof(*pb)); - GITERR_CHECK_ALLOC(pb); - - pb->object_ix = git_oidmap_alloc(); - - if (!pb->object_ix) - goto on_error; - - pb->repo = repo; - pb->nr_threads = 1; /* do not spawn any thread by default */ - - if (git_hash_ctx_init(&pb->ctx) < 0 || - git_repository_odb(&pb->odb, repo) < 0 || - packbuilder_config(pb) < 0) - goto on_error; - -#ifdef GIT_THREADS - - if (git_mutex_init(&pb->cache_mutex) || - git_mutex_init(&pb->progress_mutex) || - git_cond_init(&pb->progress_cond)) - goto on_error; - -#endif - - *out = pb; - return 0; - -on_error: - git_packbuilder_free(pb); - return -1; -} - -unsigned int git_packbuilder_set_threads(git_packbuilder *pb, unsigned int n) -{ - assert(pb); - pb->nr_threads = n; - return pb->nr_threads; -} - -static void rehash(git_packbuilder *pb) -{ - git_pobject *po; - khiter_t pos; - unsigned int i; - int ret; - - kh_clear(oid, pb->object_ix); - for (i = 0, po = pb->object_list; i < pb->nr_objects; i++, po++) { - pos = kh_put(oid, pb->object_ix, &po->id, &ret); - kh_value(pb->object_ix, pos) = po; - } -} - -int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid, - const char *name) -{ - git_pobject *po; - khiter_t pos; - int ret; - - assert(pb && oid); - - /* If the object already exists in the hash table, then we don't - * have any work to do */ - pos = kh_get(oid, pb->object_ix, oid); - if (pos != kh_end(pb->object_ix)) - return 0; - - if (pb->nr_objects >= pb->nr_alloc) { - pb->nr_alloc = (pb->nr_alloc + 1024) * 3 / 2; - pb->object_list = git__realloc(pb->object_list, - pb->nr_alloc * sizeof(*po)); - GITERR_CHECK_ALLOC(pb->object_list); - rehash(pb); - } - - po = pb->object_list + pb->nr_objects; - memset(po, 0x0, sizeof(*po)); - - if (git_odb_read_header(&po->size, &po->type, pb->odb, oid) < 0) - return -1; - - pb->nr_objects++; - git_oid_cpy(&po->id, oid); - po->hash = name_hash(name); - - pos = kh_put(oid, pb->object_ix, &po->id, &ret); - assert(ret != 0); - kh_value(pb->object_ix, pos) = po; - - pb->done = false; - return 0; -} - -/* - * The per-object header is a pretty dense thing, which is - * - first byte: low four bits are "size", - * then three bits of "type", - * with the high bit being "size continues". - * - each byte afterwards: low seven bits are size continuation, - * with the high bit being "size continues" - */ -static int gen_pack_object_header( - unsigned char *hdr, - unsigned long size, - git_otype type) -{ - unsigned char *hdr_base; - unsigned char c; - - assert(type >= GIT_OBJ_COMMIT && type <= GIT_OBJ_REF_DELTA); - - /* TODO: add support for chunked objects; see git.git 6c0d19b1 */ - - c = (unsigned char)((type << 4) | (size & 15)); - size >>= 4; - hdr_base = hdr; - - while (size) { - *hdr++ = c | 0x80; - c = size & 0x7f; - size >>= 7; - } - *hdr++ = c; - - return (int)(hdr - hdr_base); -} - -static int get_delta(void **out, git_odb *odb, git_pobject *po) -{ - git_odb_object *src = NULL, *trg = NULL; - unsigned long delta_size; - void *delta_buf; - - *out = NULL; - - if (git_odb_read(&src, odb, &po->delta->id) < 0 || - git_odb_read(&trg, odb, &po->id) < 0) - goto on_error; - - delta_buf = git_delta( - git_odb_object_data(src), (unsigned long)git_odb_object_size(src), - git_odb_object_data(trg), (unsigned long)git_odb_object_size(trg), - &delta_size, 0); - - if (!delta_buf || delta_size != po->delta_size) { - giterr_set(GITERR_INVALID, "Delta size changed"); - goto on_error; - } - - *out = delta_buf; - - git_odb_object_free(src); - git_odb_object_free(trg); - return 0; - -on_error: - git_odb_object_free(src); - git_odb_object_free(trg); - return -1; -} - -static int write_object(git_buf *buf, git_packbuilder *pb, git_pobject *po) -{ - git_odb_object *obj = NULL; - git_buf zbuf = GIT_BUF_INIT; - git_otype type; - unsigned char hdr[10]; - unsigned int hdr_len; - unsigned long size; - void *data; - - if (po->delta) { - if (po->delta_data) - data = po->delta_data; - else if (get_delta(&data, pb->odb, po) < 0) - goto on_error; - size = po->delta_size; - type = GIT_OBJ_REF_DELTA; - } else { - if (git_odb_read(&obj, pb->odb, &po->id)) - goto on_error; - - data = (void *)git_odb_object_data(obj); - size = (unsigned long)git_odb_object_size(obj); - type = git_odb_object_type(obj); - } - - /* Write header */ - hdr_len = gen_pack_object_header(hdr, size, type); - - if (git_buf_put(buf, (char *)hdr, hdr_len) < 0) - goto on_error; - - if (git_hash_update(&pb->ctx, hdr, hdr_len) < 0) - goto on_error; - - if (type == GIT_OBJ_REF_DELTA) { - if (git_buf_put(buf, (char *)po->delta->id.id, GIT_OID_RAWSZ) < 0 || - git_hash_update(&pb->ctx, po->delta->id.id, GIT_OID_RAWSZ) < 0) - goto on_error; - } - - /* Write data */ - if (po->z_delta_size) - size = po->z_delta_size; - else if (git__compress(&zbuf, data, size) < 0) - goto on_error; - else { - if (po->delta) - git__free(data); - data = zbuf.ptr; - size = (unsigned long)zbuf.size; - } - - if (git_buf_put(buf, data, size) < 0 || - git_hash_update(&pb->ctx, data, size) < 0) - goto on_error; - - if (po->delta_data) - git__free(po->delta_data); - - git_odb_object_free(obj); - git_buf_free(&zbuf); - - pb->nr_written++; - return 0; - -on_error: - git_odb_object_free(obj); - git_buf_free(&zbuf); - return -1; -} - -enum write_one_status { - WRITE_ONE_SKIP = -1, /* already written */ - WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */ - WRITE_ONE_WRITTEN = 1, /* normal */ - WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */ -}; - -static int write_one(git_buf *buf, git_packbuilder *pb, git_pobject *po, - enum write_one_status *status) -{ - if (po->recursing) { - *status = WRITE_ONE_RECURSIVE; - return 0; - } else if (po->written) { - *status = WRITE_ONE_SKIP; - return 0; - } - - if (po->delta) { - po->recursing = 1; - if (write_one(buf, pb, po->delta, status) < 0) - return -1; - switch (*status) { - case WRITE_ONE_RECURSIVE: - /* we cannot depend on this one */ - po->delta = NULL; - break; - default: - break; - } - } - - po->written = 1; - po->recursing = 0; - return write_object(buf, pb, po); -} - -GIT_INLINE(void) add_to_write_order(git_pobject **wo, unsigned int *endp, - git_pobject *po) -{ - if (po->filled) - return; - wo[(*endp)++] = po; - po->filled = 1; -} - -static void add_descendants_to_write_order(git_pobject **wo, unsigned int *endp, - git_pobject *po) -{ - int add_to_order = 1; - while (po) { - if (add_to_order) { - git_pobject *s; - /* add this node... */ - add_to_write_order(wo, endp, po); - /* all its siblings... */ - for (s = po->delta_sibling; s; s = s->delta_sibling) { - add_to_write_order(wo, endp, s); - } - } - /* drop down a level to add left subtree nodes if possible */ - if (po->delta_child) { - add_to_order = 1; - po = po->delta_child; - } else { - add_to_order = 0; - /* our sibling might have some children, it is next */ - if (po->delta_sibling) { - po = po->delta_sibling; - continue; - } - /* go back to our parent node */ - po = po->delta; - while (po && !po->delta_sibling) { - /* we're on the right side of a subtree, keep - * going up until we can go right again */ - po = po->delta; - } - if (!po) { - /* done- we hit our original root node */ - return; - } - /* pass it off to sibling at this level */ - po = po->delta_sibling; - } - }; -} - -static void add_family_to_write_order(git_pobject **wo, unsigned int *endp, - git_pobject *po) -{ - git_pobject *root; - - for (root = po; root->delta; root = root->delta) - ; /* nothing */ - add_descendants_to_write_order(wo, endp, root); -} - -static int cb_tag_foreach(const char *name, git_oid *oid, void *data) -{ - git_packbuilder *pb = data; - git_pobject *po; - khiter_t pos; - - GIT_UNUSED(name); - - pos = kh_get(oid, pb->object_ix, oid); - if (pos == kh_end(pb->object_ix)) - return 0; - - po = kh_value(pb->object_ix, pos); - po->tagged = 1; - - /* TODO: peel objects */ - - return 0; -} - -static git_pobject **compute_write_order(git_packbuilder *pb) -{ - unsigned int i, wo_end, last_untagged; - - git_pobject **wo = git__malloc(sizeof(*wo) * pb->nr_objects); - - for (i = 0; i < pb->nr_objects; i++) { - git_pobject *po = pb->object_list + i; - po->tagged = 0; - po->filled = 0; - po->delta_child = NULL; - po->delta_sibling = NULL; - } - - /* - * Fully connect delta_child/delta_sibling network. - * Make sure delta_sibling is sorted in the original - * recency order. - */ - for (i = pb->nr_objects; i > 0;) { - git_pobject *po = &pb->object_list[--i]; - if (!po->delta) - continue; - /* Mark me as the first child */ - po->delta_sibling = po->delta->delta_child; - po->delta->delta_child = po; - } - - /* - * Mark objects that are at the tip of tags. - */ - if (git_tag_foreach(pb->repo, &cb_tag_foreach, pb) < 0) - return NULL; - - /* - * Give the objects in the original recency order until - * we see a tagged tip. - */ - for (i = wo_end = 0; i < pb->nr_objects; i++) { - git_pobject *po = pb->object_list + i; - if (po->tagged) - break; - add_to_write_order(wo, &wo_end, po); - } - last_untagged = i; - - /* - * Then fill all the tagged tips. - */ - for (; i < pb->nr_objects; i++) { - git_pobject *po = pb->object_list + i; - if (po->tagged) - add_to_write_order(wo, &wo_end, po); - } - - /* - * And then all remaining commits and tags. - */ - for (i = last_untagged; i < pb->nr_objects; i++) { - git_pobject *po = pb->object_list + i; - if (po->type != GIT_OBJ_COMMIT && - po->type != GIT_OBJ_TAG) - continue; - add_to_write_order(wo, &wo_end, po); - } - - /* - * And then all the trees. - */ - for (i = last_untagged; i < pb->nr_objects; i++) { - git_pobject *po = pb->object_list + i; - if (po->type != GIT_OBJ_TREE) - continue; - add_to_write_order(wo, &wo_end, po); - } - - /* - * Finally all the rest in really tight order - */ - for (i = last_untagged; i < pb->nr_objects; i++) { - git_pobject *po = pb->object_list + i; - if (!po->filled) - add_family_to_write_order(wo, &wo_end, po); - } - - if (wo_end != pb->nr_objects) { - giterr_set(GITERR_INVALID, "invalid write order"); - return NULL; - } - - return wo; -} - -static int write_pack(git_packbuilder *pb, - int (*cb)(void *buf, size_t size, void *data), - void *data) -{ - git_pobject **write_order; - git_pobject *po; - git_buf buf = GIT_BUF_INIT; - enum write_one_status status; - struct git_pack_header ph; - unsigned int i = 0; - - write_order = compute_write_order(pb); - if (write_order == NULL) - goto on_error; - - /* Write pack header */ - ph.hdr_signature = htonl(PACK_SIGNATURE); - ph.hdr_version = htonl(PACK_VERSION); - ph.hdr_entries = htonl(pb->nr_objects); - - if (cb(&ph, sizeof(ph), data) < 0) - goto on_error; - - if (git_hash_update(&pb->ctx, &ph, sizeof(ph)) < 0) - goto on_error; - - pb->nr_remaining = pb->nr_objects; - do { - pb->nr_written = 0; - for ( ; i < pb->nr_objects; ++i) { - po = write_order[i]; - if (write_one(&buf, pb, po, &status) < 0) - goto on_error; - if (cb(buf.ptr, buf.size, data) < 0) - goto on_error; - git_buf_clear(&buf); - } - - pb->nr_remaining -= pb->nr_written; - } while (pb->nr_remaining && i < pb->nr_objects); - - git__free(write_order); - git_buf_free(&buf); - - if (git_hash_final(&pb->pack_oid, &pb->ctx) < 0) - goto on_error; - - return cb(pb->pack_oid.id, GIT_OID_RAWSZ, data); - -on_error: - git__free(write_order); - git_buf_free(&buf); - return -1; -} - -static int write_pack_buf(void *buf, size_t size, void *data) -{ - git_buf *b = (git_buf *)data; - return git_buf_put(b, buf, size); -} - -static int write_pack_to_file(void *buf, size_t size, void *data) -{ - git_filebuf *file = (git_filebuf *)data; - return git_filebuf_write(file, buf, size); -} - -static int write_pack_file(git_packbuilder *pb, const char *path) -{ - git_filebuf file = GIT_FILEBUF_INIT; - - if (git_filebuf_open(&file, path, 0) < 0 || - write_pack(pb, &write_pack_to_file, &file) < 0 || - git_filebuf_commit(&file, GIT_PACK_FILE_MODE) < 0) { - git_filebuf_cleanup(&file); - return -1; - } - - return 0; -} - -static int type_size_sort(const void *_a, const void *_b) -{ - const git_pobject *a = (git_pobject *)_a; - const git_pobject *b = (git_pobject *)_b; - - if (a->type > b->type) - return -1; - if (a->type < b->type) - return 1; - if (a->hash > b->hash) - return -1; - if (a->hash < b->hash) - return 1; - /* - * TODO - * - if (a->preferred_base > b->preferred_base) - return -1; - if (a->preferred_base < b->preferred_base) - return 1; - */ - if (a->size > b->size) - return -1; - if (a->size < b->size) - return 1; - return a < b ? -1 : (a > b); /* newest first */ -} - -static int delta_cacheable(git_packbuilder *pb, unsigned long src_size, - unsigned long trg_size, unsigned long delta_size) -{ - if (pb->max_delta_cache_size && - pb->delta_cache_size + delta_size > pb->max_delta_cache_size) - return 0; - - if (delta_size < pb->cache_max_small_delta_size) - return 1; - - /* cache delta, if objects are large enough compared to delta size */ - if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10)) - return 1; - - return 0; -} - -static int try_delta(git_packbuilder *pb, struct unpacked *trg, - struct unpacked *src, unsigned int max_depth, - unsigned long *mem_usage, int *ret) -{ - git_pobject *trg_object = trg->object; - git_pobject *src_object = src->object; - git_odb_object *obj; - unsigned long trg_size, src_size, delta_size, - sizediff, max_size, sz; - unsigned int ref_depth; - void *delta_buf; - - /* Don't bother doing diffs between different types */ - if (trg_object->type != src_object->type) { - *ret = -1; - return 0; - } - - *ret = 0; - - /* TODO: support reuse-delta */ - - /* Let's not bust the allowed depth. */ - if (src->depth >= max_depth) - return 0; - - /* Now some size filtering heuristics. */ - trg_size = (unsigned long)trg_object->size; - if (!trg_object->delta) { - max_size = trg_size/2 - 20; - ref_depth = 1; - } else { - max_size = trg_object->delta_size; - ref_depth = trg->depth; - } - - max_size = (uint64_t)max_size * (max_depth - src->depth) / - (max_depth - ref_depth + 1); - if (max_size == 0) - return 0; - - src_size = (unsigned long)src_object->size; - sizediff = src_size < trg_size ? trg_size - src_size : 0; - if (sizediff >= max_size) - return 0; - if (trg_size < src_size / 32) - return 0; - - /* Load data if not already done */ - if (!trg->data) { - if (git_odb_read(&obj, pb->odb, &trg_object->id) < 0) - return -1; - - sz = (unsigned long)git_odb_object_size(obj); - trg->data = git__malloc(sz); - GITERR_CHECK_ALLOC(trg->data); - memcpy(trg->data, git_odb_object_data(obj), sz); - - git_odb_object_free(obj); - - if (sz != trg_size) { - giterr_set(GITERR_INVALID, - "Inconsistent target object length"); - return -1; - } - - *mem_usage += sz; - } - if (!src->data) { - if (git_odb_read(&obj, pb->odb, &src_object->id) < 0) - return -1; - - sz = (unsigned long)git_odb_object_size(obj); - src->data = git__malloc(sz); - GITERR_CHECK_ALLOC(src->data); - memcpy(src->data, git_odb_object_data(obj), sz); - - git_odb_object_free(obj); - - if (sz != src_size) { - giterr_set(GITERR_INVALID, - "Inconsistent source object length"); - return -1; - } - - *mem_usage += sz; - } - if (!src->index) { - src->index = git_delta_create_index(src->data, src_size); - if (!src->index) - return 0; /* suboptimal pack - out of memory */ - - *mem_usage += git_delta_sizeof_index(src->index); - } - - delta_buf = git_delta_create(src->index, trg->data, trg_size, - &delta_size, max_size); - if (!delta_buf) - return 0; - - if (trg_object->delta) { - /* Prefer only shallower same-sized deltas. */ - if (delta_size == trg_object->delta_size && - src->depth + 1 >= trg->depth) { - git__free(delta_buf); - return 0; - } - } - - git_packbuilder__cache_lock(pb); - if (trg_object->delta_data) { - git__free(trg_object->delta_data); - pb->delta_cache_size -= trg_object->delta_size; - trg_object->delta_data = NULL; - } - if (delta_cacheable(pb, src_size, trg_size, delta_size)) { - pb->delta_cache_size += delta_size; - git_packbuilder__cache_unlock(pb); - - trg_object->delta_data = git__realloc(delta_buf, delta_size); - GITERR_CHECK_ALLOC(trg_object->delta_data); - } else { - /* create delta when writing the pack */ - git_packbuilder__cache_unlock(pb); - git__free(delta_buf); - } - - trg_object->delta = src_object; - trg_object->delta_size = delta_size; - trg->depth = src->depth + 1; - - *ret = 1; - return 0; -} - -static unsigned int check_delta_limit(git_pobject *me, unsigned int n) -{ - git_pobject *child = me->delta_child; - unsigned int m = n; - - while (child) { - unsigned int c = check_delta_limit(child, n + 1); - if (m < c) - m = c; - child = child->delta_sibling; - } - return m; -} - -static unsigned long free_unpacked(struct unpacked *n) -{ - unsigned long freed_mem = git_delta_sizeof_index(n->index); - git_delta_free_index(n->index); - n->index = NULL; - if (n->data) { - freed_mem += (unsigned long)n->object->size; - git__free(n->data); - n->data = NULL; - } - n->object = NULL; - n->depth = 0; - return freed_mem; -} - -static int find_deltas(git_packbuilder *pb, git_pobject **list, - unsigned int *list_size, unsigned int window, - unsigned int depth) -{ - git_pobject *po; - git_buf zbuf = GIT_BUF_INIT; - struct unpacked *array; - uint32_t idx = 0, count = 0; - unsigned long mem_usage = 0; - unsigned int i; - int error = -1; - - array = git__calloc(window, sizeof(struct unpacked)); - GITERR_CHECK_ALLOC(array); - - for (;;) { - struct unpacked *n = array + idx; - unsigned int max_depth; - int j, best_base = -1; - - git_packbuilder__progress_lock(pb); - if (!*list_size) { - git_packbuilder__progress_unlock(pb); - break; - } - - po = *list++; - (*list_size)--; - git_packbuilder__progress_unlock(pb); - - mem_usage -= free_unpacked(n); - n->object = po; - - while (pb->window_memory_limit && - mem_usage > pb->window_memory_limit && - count > 1) { - uint32_t tail = (idx + window - count) % window; - mem_usage -= free_unpacked(array + tail); - count--; - } - - /* - * If the current object is at pack edge, take the depth the - * objects that depend on the current object into account - * otherwise they would become too deep. - */ - max_depth = depth; - if (po->delta_child) { - max_depth -= check_delta_limit(po, 0); - if (max_depth <= 0) - goto next; - } - - j = window; - while (--j > 0) { - int ret; - uint32_t other_idx = idx + j; - struct unpacked *m; - - if (other_idx >= window) - other_idx -= window; - - m = array + other_idx; - if (!m->object) - break; - - if (try_delta(pb, n, m, max_depth, &mem_usage, &ret) < 0) - goto on_error; - if (ret < 0) - break; - else if (ret > 0) - best_base = other_idx; - } - - /* - * If we decided to cache the delta data, then it is best - * to compress it right away. First because we have to do - * it anyway, and doing it here while we're threaded will - * save a lot of time in the non threaded write phase, - * as well as allow for caching more deltas within - * the same cache size limit. - * ... - * But only if not writing to stdout, since in that case - * the network is most likely throttling writes anyway, - * and therefore it is best to go to the write phase ASAP - * instead, as we can afford spending more time compressing - * between writes at that moment. - */ - if (po->delta_data) { - if (git__compress(&zbuf, po->delta_data, po->delta_size) < 0) - goto on_error; - - git__free(po->delta_data); - po->delta_data = git__malloc(zbuf.size); - GITERR_CHECK_ALLOC(po->delta_data); - - memcpy(po->delta_data, zbuf.ptr, zbuf.size); - po->z_delta_size = (unsigned long)zbuf.size; - git_buf_clear(&zbuf); - - git_packbuilder__cache_lock(pb); - pb->delta_cache_size -= po->delta_size; - pb->delta_cache_size += po->z_delta_size; - git_packbuilder__cache_unlock(pb); - } - - /* - * If we made n a delta, and if n is already at max - * depth, leaving it in the window is pointless. we - * should evict it first. - */ - if (po->delta && max_depth <= n->depth) - continue; - - /* - * Move the best delta base up in the window, after the - * currently deltified object, to keep it longer. It will - * be the first base object to be attempted next. - */ - if (po->delta) { - struct unpacked swap = array[best_base]; - int dist = (window + idx - best_base) % window; - int dst = best_base; - while (dist--) { - int src = (dst + 1) % window; - array[dst] = array[src]; - dst = src; - } - array[dst] = swap; - } - - next: - idx++; - if (count + 1 < window) - count++; - if (idx >= window) - idx = 0; - } - error = 0; - -on_error: - for (i = 0; i < window; ++i) { - git__free(array[i].index); - git__free(array[i].data); - } - git__free(array); - git_buf_free(&zbuf); - - return error; -} - -#ifdef GIT_THREADS - -struct thread_params { - git_thread thread; - git_packbuilder *pb; - - git_pobject **list; - - git_cond cond; - git_mutex mutex; - - unsigned int list_size; - unsigned int remaining; - - int window; - int depth; - int working; - int data_ready; -}; - -static void *threaded_find_deltas(void *arg) -{ - struct thread_params *me = arg; - - while (me->remaining) { - if (find_deltas(me->pb, me->list, &me->remaining, - me->window, me->depth) < 0) { - ; /* TODO */ - } - - git_packbuilder__progress_lock(me->pb); - me->working = 0; - git_cond_signal(&me->pb->progress_cond); - git_packbuilder__progress_unlock(me->pb); - - if (git_mutex_lock(&me->mutex)) { - giterr_set(GITERR_THREAD, "unable to lock packfile condition mutex"); - return NULL; - } - - while (!me->data_ready) - git_cond_wait(&me->cond, &me->mutex); - - /* - * We must not set ->data_ready before we wait on the - * condition because the main thread may have set it to 1 - * before we get here. In order to be sure that new - * work is available if we see 1 in ->data_ready, it - * was initialized to 0 before this thread was spawned - * and we reset it to 0 right away. - */ - me->data_ready = 0; - git_mutex_unlock(&me->mutex); - } - /* leave ->working 1 so that this doesn't get more work assigned */ - return NULL; -} - -static int ll_find_deltas(git_packbuilder *pb, git_pobject **list, - unsigned int list_size, unsigned int window, - unsigned int depth) -{ - struct thread_params *p; - int i, ret, active_threads = 0; - - if (!pb->nr_threads) - pb->nr_threads = git_online_cpus(); - - if (pb->nr_threads <= 1) { - find_deltas(pb, list, &list_size, window, depth); - return 0; - } - - p = git__malloc(pb->nr_threads * sizeof(*p)); - GITERR_CHECK_ALLOC(p); - - /* Partition the work among the threads */ - for (i = 0; i < pb->nr_threads; ++i) { - unsigned sub_size = list_size / (pb->nr_threads - i); - - /* don't use too small segments or no deltas will be found */ - if (sub_size < 2*window && i+1 < pb->nr_threads) - sub_size = 0; - - p[i].pb = pb; - p[i].window = window; - p[i].depth = depth; - p[i].working = 1; - p[i].data_ready = 0; - - /* try to split chunks on "path" boundaries */ - while (sub_size && sub_size < list_size && - list[sub_size]->hash && - list[sub_size]->hash == list[sub_size-1]->hash) - sub_size++; - - p[i].list = list; - p[i].list_size = sub_size; - p[i].remaining = sub_size; - - list += sub_size; - list_size -= sub_size; - } - - /* Start work threads */ - for (i = 0; i < pb->nr_threads; ++i) { - if (!p[i].list_size) - continue; - - git_mutex_init(&p[i].mutex); - git_cond_init(&p[i].cond); - - ret = git_thread_create(&p[i].thread, NULL, - threaded_find_deltas, &p[i]); - if (ret) { - giterr_set(GITERR_THREAD, "unable to create thread"); - return -1; - } - active_threads++; - } - - /* - * Now let's wait for work completion. Each time a thread is done - * with its work, we steal half of the remaining work from the - * thread with the largest number of unprocessed objects and give - * it to that newly idle thread. This ensure good load balancing - * until the remaining object list segments are simply too short - * to be worth splitting anymore. - */ - while (active_threads) { - struct thread_params *target = NULL; - struct thread_params *victim = NULL; - unsigned sub_size = 0; - - /* Start by locating a thread that has transitioned its - * 'working' flag from 1 -> 0. This indicates that it is - * ready to receive more work using our work-stealing - * algorithm. */ - git_packbuilder__progress_lock(pb); - for (;;) { - for (i = 0; !target && i < pb->nr_threads; i++) - if (!p[i].working) - target = &p[i]; - if (target) - break; - git_cond_wait(&pb->progress_cond, &pb->progress_mutex); - } - - /* At this point we hold the progress lock and have located - * a thread to receive more work. We still need to locate a - * thread from which to steal work (the victim). */ - for (i = 0; i < pb->nr_threads; i++) - if (p[i].remaining > 2*window && - (!victim || victim->remaining < p[i].remaining)) - victim = &p[i]; - - if (victim) { - sub_size = victim->remaining / 2; - list = victim->list + victim->list_size - sub_size; - while (sub_size && list[0]->hash && - list[0]->hash == list[-1]->hash) { - list++; - sub_size--; - } - if (!sub_size) { - /* - * It is possible for some "paths" to have - * so many objects that no hash boundary - * might be found. Let's just steal the - * exact half in that case. - */ - sub_size = victim->remaining / 2; - list -= sub_size; - } - target->list = list; - victim->list_size -= sub_size; - victim->remaining -= sub_size; - } - target->list_size = sub_size; - target->remaining = sub_size; - target->working = 1; - git_packbuilder__progress_unlock(pb); - - if (git_mutex_lock(&target->mutex)) { - giterr_set(GITERR_THREAD, "unable to lock packfile condition mutex"); - git__free(p); - return -1; - } - - target->data_ready = 1; - git_cond_signal(&target->cond); - git_mutex_unlock(&target->mutex); - - if (!sub_size) { - git_thread_join(target->thread, NULL); - git_cond_free(&target->cond); - git_mutex_free(&target->mutex); - active_threads--; - } - } - - git__free(p); - return 0; -} - -#else -#define ll_find_deltas(pb, l, ls, w, d) find_deltas(pb, l, &ls, w, d) -#endif - -static int prepare_pack(git_packbuilder *pb) -{ - git_pobject **delta_list; - unsigned int i, n = 0; - - if (pb->nr_objects == 0 || pb->done) - return 0; /* nothing to do */ - - delta_list = git__malloc(pb->nr_objects * sizeof(*delta_list)); - GITERR_CHECK_ALLOC(delta_list); - - for (i = 0; i < pb->nr_objects; ++i) { - git_pobject *po = pb->object_list + i; - - /* Make sure the item is within our size limits */ - if (po->size < 50 || po->size > pb->big_file_threshold) - continue; - - delta_list[n++] = po; - } - - if (n > 1) { - git__tsort((void **)delta_list, n, type_size_sort); - if (ll_find_deltas(pb, delta_list, n, - GIT_PACK_WINDOW + 1, - GIT_PACK_DEPTH) < 0) { - git__free(delta_list); - return -1; - } - } - - pb->done = true; - git__free(delta_list); - return 0; -} - -#define PREPARE_PACK if (prepare_pack(pb) < 0) { return -1; } - -int git_packbuilder_foreach(git_packbuilder *pb, int (*cb)(void *buf, size_t size, void *payload), void *payload) -{ - PREPARE_PACK; - return write_pack(pb, cb, payload); -} - -int git_packbuilder_write_buf(git_buf *buf, git_packbuilder *pb) -{ - PREPARE_PACK; - return write_pack(pb, &write_pack_buf, buf); -} - -int git_packbuilder_write(git_packbuilder *pb, const char *path) -{ - PREPARE_PACK; - return write_pack_file(pb, path); -} - -#undef PREPARE_PACK - -static int cb_tree_walk(const char *root, const git_tree_entry *entry, void *payload) -{ - struct tree_walk_context *ctx = payload; - - /* A commit inside a tree represents a submodule commit and should be skipped. */ - if (git_tree_entry_type(entry) == GIT_OBJ_COMMIT) - return 0; - - if (git_buf_sets(&ctx->buf, root) < 0 || - git_buf_puts(&ctx->buf, git_tree_entry_name(entry)) < 0) - return -1; - - return git_packbuilder_insert(ctx->pb, - git_tree_entry_id(entry), - git_buf_cstr(&ctx->buf)); -} - -int git_packbuilder_insert_tree(git_packbuilder *pb, const git_oid *oid) -{ - git_tree *tree; - struct tree_walk_context context = { pb, GIT_BUF_INIT }; - - if (git_tree_lookup(&tree, pb->repo, oid) < 0 || - git_packbuilder_insert(pb, oid, NULL) < 0) - return -1; - - if (git_tree_walk(tree, GIT_TREEWALK_PRE, cb_tree_walk, &context) < 0) { - git_tree_free(tree); - git_buf_free(&context.buf); - return -1; - } - - git_tree_free(tree); - git_buf_free(&context.buf); - return 0; -} - -uint32_t git_packbuilder_object_count(git_packbuilder *pb) -{ - return pb->nr_objects; -} - -uint32_t git_packbuilder_written(git_packbuilder *pb) -{ - return pb->nr_written; -} - -void git_packbuilder_free(git_packbuilder *pb) -{ - if (pb == NULL) - return; - -#ifdef GIT_THREADS - - git_mutex_free(&pb->cache_mutex); - git_mutex_free(&pb->progress_mutex); - git_cond_free(&pb->progress_cond); - -#endif - - if (pb->odb) - git_odb_free(pb->odb); - - if (pb->object_ix) - git_oidmap_free(pb->object_ix); - - if (pb->object_list) - git__free(pb->object_list); - - git_hash_ctx_cleanup(&pb->ctx); - - git__free(pb); -} diff --git a/src/pack-objects.h b/src/pack-objects.h deleted file mode 100644 index 8e7ba7f7837..00000000000 --- a/src/pack-objects.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#ifndef INCLUDE_pack_objects_h__ -#define INCLUDE_pack_objects_h__ - -#include "common.h" - -#include "buffer.h" -#include "hash.h" -#include "oidmap.h" -#include "netops.h" - -#include "git2/oid.h" - -#define GIT_PACK_WINDOW 10 /* number of objects to possibly delta against */ -#define GIT_PACK_DEPTH 50 /* max delta depth */ -#define GIT_PACK_DELTA_CACHE_SIZE (256 * 1024 * 1024) -#define GIT_PACK_DELTA_CACHE_LIMIT 1000 -#define GIT_PACK_BIG_FILE_THRESHOLD (512 * 1024 * 1024) - -typedef struct git_pobject { - git_oid id; - git_otype type; - git_off_t offset; - - size_t size; - - unsigned int hash; /* name hint hash */ - - struct git_pobject *delta; /* delta base object */ - struct git_pobject *delta_child; /* deltified objects who bases me */ - struct git_pobject *delta_sibling; /* other deltified objects - * who uses the same base as - * me */ - - void *delta_data; - unsigned long delta_size; - unsigned long z_delta_size; - - int written:1, - recursing:1, - tagged:1, - filled:1; -} git_pobject; - -struct git_packbuilder { - git_repository *repo; /* associated repository */ - git_odb *odb; /* associated object database */ - - git_hash_ctx ctx; - - uint32_t nr_objects, - nr_alloc, - nr_written, - nr_remaining; - - git_pobject *object_list; - - git_oidmap *object_ix; - - git_oid pack_oid; /* hash of written pack */ - - /* synchronization objects */ - git_mutex cache_mutex; - git_mutex progress_mutex; - git_cond progress_cond; - - /* configs */ - uint64_t delta_cache_size; - uint64_t max_delta_cache_size; - uint64_t cache_max_small_delta_size; - uint64_t big_file_threshold; - uint64_t window_memory_limit; - - int nr_threads; /* nr of threads to use */ - - bool done; -}; - -int git_packbuilder_write_buf(git_buf *buf, git_packbuilder *pb); - -#endif /* INCLUDE_pack_objects_h__ */ diff --git a/src/pack.c b/src/pack.c deleted file mode 100644 index f36f3cf6b63..00000000000 --- a/src/pack.c +++ /dev/null @@ -1,1149 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "odb.h" -#include "pack.h" -#include "delta-apply.h" -#include "sha1_lookup.h" -#include "mwindow.h" -#include "fileops.h" - -#include "git2/oid.h" -#include - -static int packfile_open(struct git_pack_file *p); -static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_t n); -int packfile_unpack_compressed( - git_rawobj *obj, - struct git_pack_file *p, - git_mwindow **w_curs, - git_off_t *curpos, - size_t size, - git_otype type); - -/* Can find the offset of an object given - * a prefix of an identifier. - * Throws GIT_EAMBIGUOUSOIDPREFIX if short oid - * is ambiguous within the pack. - * This method assumes that len is between - * GIT_OID_MINPREFIXLEN and GIT_OID_HEXSZ. - */ -static int pack_entry_find_offset( - git_off_t *offset_out, - git_oid *found_oid, - struct git_pack_file *p, - const git_oid *short_oid, - size_t len); - -static int packfile_error(const char *message) -{ - giterr_set(GITERR_ODB, "Invalid pack file - %s", message); - return -1; -} - -/******************** - * Delta base cache - ********************/ - -static git_pack_cache_entry *new_cache_object(git_rawobj *source) -{ - git_pack_cache_entry *e = git__calloc(1, sizeof(git_pack_cache_entry)); - if (!e) - return NULL; - - memcpy(&e->raw, source, sizeof(git_rawobj)); - - return e; -} - -static void free_cache_object(void *o) -{ - git_pack_cache_entry *e = (git_pack_cache_entry *)o; - - if (e != NULL) { - assert(e->refcount.val == 0); - git__free(e->raw.data); - git__free(e); - } -} - -static void cache_free(git_pack_cache *cache) -{ - khiter_t k; - - if (cache->entries) { - for (k = kh_begin(cache->entries); k != kh_end(cache->entries); k++) { - if (kh_exist(cache->entries, k)) - free_cache_object(kh_value(cache->entries, k)); - } - - git_offmap_free(cache->entries); - git_mutex_free(&cache->lock); - } -} - -static int cache_init(git_pack_cache *cache) -{ - memset(cache, 0, sizeof(git_pack_cache)); - cache->entries = git_offmap_alloc(); - GITERR_CHECK_ALLOC(cache->entries); - cache->memory_limit = GIT_PACK_CACHE_MEMORY_LIMIT; - git_mutex_init(&cache->lock); - - return 0; -} - -static git_pack_cache_entry *cache_get(git_pack_cache *cache, git_off_t offset) -{ - khiter_t k; - git_pack_cache_entry *entry = NULL; - - if (git_mutex_lock(&cache->lock) < 0) - return NULL; - - k = kh_get(off, cache->entries, offset); - if (k != kh_end(cache->entries)) { /* found it */ - entry = kh_value(cache->entries, k); - git_atomic_inc(&entry->refcount); - entry->last_usage = cache->use_ctr++; - } - git_mutex_unlock(&cache->lock); - - return entry; -} - -/* Run with the cache lock held */ -static void free_lowest_entry(git_pack_cache *cache) -{ - git_pack_cache_entry *entry; - khiter_t k; - - for (k = kh_begin(cache->entries); k != kh_end(cache->entries); k++) { - if (!kh_exist(cache->entries, k)) - continue; - - entry = kh_value(cache->entries, k); - - if (entry && entry->refcount.val == 0) { - cache->memory_used -= entry->raw.len; - kh_del(off, cache->entries, k); - free_cache_object(entry); - } - } -} - -static int cache_add(git_pack_cache *cache, git_rawobj *base, git_off_t offset) -{ - git_pack_cache_entry *entry; - int error, exists = 0; - khiter_t k; - - if (base->len > GIT_PACK_CACHE_SIZE_LIMIT) - return -1; - - entry = new_cache_object(base); - if (entry) { - if (git_mutex_lock(&cache->lock) < 0) { - giterr_set(GITERR_OS, "failed to lock cache"); - return -1; - } - /* Add it to the cache if nobody else has */ - exists = kh_get(off, cache->entries, offset) != kh_end(cache->entries); - if (!exists) { - while (cache->memory_used + base->len > cache->memory_limit) - free_lowest_entry(cache); - - k = kh_put(off, cache->entries, offset, &error); - assert(error != 0); - kh_value(cache->entries, k) = entry; - cache->memory_used += entry->raw.len; - } - git_mutex_unlock(&cache->lock); - /* Somebody beat us to adding it into the cache */ - if (exists) { - git__free(entry); - return -1; - } - } - - return 0; -} - -/*********************************************************** - * - * PACK INDEX METHODS - * - ***********************************************************/ - -static void pack_index_free(struct git_pack_file *p) -{ - if (p->oids) { - git__free(p->oids); - p->oids = NULL; - } - if (p->index_map.data) { - git_futils_mmap_free(&p->index_map); - p->index_map.data = NULL; - } -} - -static int pack_index_check(const char *path, struct git_pack_file *p) -{ - struct git_pack_idx_header *hdr; - uint32_t version, nr, i, *index; - void *idx_map; - size_t idx_size; - struct stat st; - int error; - /* TODO: properly open the file without access time using O_NOATIME */ - git_file fd = git_futils_open_ro(path); - if (fd < 0) - return fd; - - if (p_fstat(fd, &st) < 0 || - !S_ISREG(st.st_mode) || - !git__is_sizet(st.st_size) || - (idx_size = (size_t)st.st_size) < 4 * 256 + 20 + 20) - { - p_close(fd); - giterr_set(GITERR_OS, "Failed to check pack index."); - return -1; - } - - error = git_futils_mmap_ro(&p->index_map, fd, 0, idx_size); - - p_close(fd); - - if (error < 0) - return error; - - hdr = idx_map = p->index_map.data; - - if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) { - version = ntohl(hdr->idx_version); - - if (version < 2 || version > 2) { - git_futils_mmap_free(&p->index_map); - return packfile_error("unsupported index version"); - } - - } else - version = 1; - - nr = 0; - index = idx_map; - - if (version > 1) - index += 2; /* skip index header */ - - for (i = 0; i < 256; i++) { - uint32_t n = ntohl(index[i]); - if (n < nr) { - git_futils_mmap_free(&p->index_map); - return packfile_error("index is non-monotonic"); - } - nr = n; - } - - if (version == 1) { - /* - * Total size: - * - 256 index entries 4 bytes each - * - 24-byte entries * nr (20-byte sha1 + 4-byte offset) - * - 20-byte SHA1 of the packfile - * - 20-byte SHA1 file checksum - */ - if (idx_size != 4*256 + nr * 24 + 20 + 20) { - git_futils_mmap_free(&p->index_map); - return packfile_error("index is corrupted"); - } - } else if (version == 2) { - /* - * Minimum size: - * - 8 bytes of header - * - 256 index entries 4 bytes each - * - 20-byte sha1 entry * nr - * - 4-byte crc entry * nr - * - 4-byte offset entry * nr - * - 20-byte SHA1 of the packfile - * - 20-byte SHA1 file checksum - * And after the 4-byte offset table might be a - * variable sized table containing 8-byte entries - * for offsets larger than 2^31. - */ - unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20; - unsigned long max_size = min_size; - - if (nr) - max_size += (nr - 1)*8; - - if (idx_size < min_size || idx_size > max_size) { - git_futils_mmap_free(&p->index_map); - return packfile_error("wrong index size"); - } - } - - p->index_version = version; - p->num_objects = nr; - return 0; -} - -static int pack_index_open(struct git_pack_file *p) -{ - char *idx_name; - int error; - size_t name_len, offset; - - if (p->index_map.data) - return 0; - - idx_name = git__strdup(p->pack_name); - GITERR_CHECK_ALLOC(idx_name); - - name_len = strlen(idx_name); - offset = name_len - strlen(".pack"); - assert(offset < name_len); /* make sure no underflow */ - - strncpy(idx_name + offset, ".idx", name_len - offset); - - error = pack_index_check(idx_name, p); - git__free(idx_name); - - return error; -} - -static unsigned char *pack_window_open( - struct git_pack_file *p, - git_mwindow **w_cursor, - git_off_t offset, - unsigned int *left) -{ - if (p->mwf.fd == -1 && packfile_open(p) < 0) - return NULL; - - /* Since packfiles end in a hash of their content and it's - * pointless to ask for an offset into the middle of that - * hash, and the pack_window_contains function above wouldn't match - * don't allow an offset too close to the end of the file. - */ - if (offset > (p->mwf.size - 20)) - return NULL; - - return git_mwindow_open(&p->mwf, w_cursor, offset, 20, left); - } - -static int packfile_unpack_header1( - unsigned long *usedp, - size_t *sizep, - git_otype *type, - const unsigned char *buf, - unsigned long len) -{ - unsigned shift; - unsigned long size, c; - unsigned long used = 0; - - c = buf[used++]; - *type = (c >> 4) & 7; - size = c & 15; - shift = 4; - while (c & 0x80) { - if (len <= used) - return GIT_EBUFS; - - if (bitsizeof(long) <= shift) { - *usedp = 0; - return -1; - } - - c = buf[used++]; - size += (c & 0x7f) << shift; - shift += 7; - } - - *sizep = (size_t)size; - *usedp = used; - return 0; -} - -int git_packfile_unpack_header( - size_t *size_p, - git_otype *type_p, - git_mwindow_file *mwf, - git_mwindow **w_curs, - git_off_t *curpos) -{ - unsigned char *base; - unsigned int left; - unsigned long used; - int ret; - - /* pack_window_open() assures us we have [base, base + 20) available - * as a range that we can look at at. (Its actually the hash - * size that is assured.) With our object header encoding - * the maximum deflated object size is 2^137, which is just - * insane, so we know won't exceed what we have been given. - */ -// base = pack_window_open(p, w_curs, *curpos, &left); - base = git_mwindow_open(mwf, w_curs, *curpos, 20, &left); - if (base == NULL) - return GIT_EBUFS; - - ret = packfile_unpack_header1(&used, size_p, type_p, base, left); - git_mwindow_close(w_curs); - if (ret == GIT_EBUFS) - return ret; - else if (ret < 0) - return packfile_error("header length is zero"); - - *curpos += used; - return 0; -} - -int git_packfile_resolve_header( - size_t *size_p, - git_otype *type_p, - struct git_pack_file *p, - git_off_t offset) -{ - git_mwindow *w_curs = NULL; - git_off_t curpos = offset; - size_t size; - git_otype type; - git_off_t base_offset; - int error; - - error = git_packfile_unpack_header(&size, &type, &p->mwf, &w_curs, &curpos); - git_mwindow_close(&w_curs); - if (error < 0) - return error; - - if (type == GIT_OBJ_OFS_DELTA || type == GIT_OBJ_REF_DELTA) { - size_t base_size; - git_rawobj delta; - base_offset = get_delta_base(p, &w_curs, &curpos, type, offset); - git_mwindow_close(&w_curs); - error = packfile_unpack_compressed(&delta, p, &w_curs, &curpos, size, type); - git_mwindow_close(&w_curs); - if (error < 0) - return error; - error = git__delta_read_header(delta.data, delta.len, &base_size, size_p); - git__free(delta.data); - if (error < 0) - return error; - } else - *size_p = size; - - while (type == GIT_OBJ_OFS_DELTA || type == GIT_OBJ_REF_DELTA) { - curpos = base_offset; - error = git_packfile_unpack_header(&size, &type, &p->mwf, &w_curs, &curpos); - git_mwindow_close(&w_curs); - if (error < 0) - return error; - if (type != GIT_OBJ_OFS_DELTA && type != GIT_OBJ_REF_DELTA) - break; - base_offset = get_delta_base(p, &w_curs, &curpos, type, base_offset); - git_mwindow_close(&w_curs); - } - *type_p = type; - - return error; -} - -static int packfile_unpack_delta( - git_rawobj *obj, - struct git_pack_file *p, - git_mwindow **w_curs, - git_off_t *curpos, - size_t delta_size, - git_otype delta_type, - git_off_t obj_offset) -{ - git_off_t base_offset, base_key; - git_rawobj base, delta; - git_pack_cache_entry *cached = NULL; - int error, found_base = 0; - - base_offset = get_delta_base(p, w_curs, curpos, delta_type, obj_offset); - git_mwindow_close(w_curs); - if (base_offset == 0) - return packfile_error("delta offset is zero"); - if (base_offset < 0) /* must actually be an error code */ - return (int)base_offset; - - if (!p->bases.entries && (cache_init(&p->bases) < 0)) - return -1; - - base_key = base_offset; /* git_packfile_unpack modifies base_offset */ - if ((cached = cache_get(&p->bases, base_offset)) != NULL) { - memcpy(&base, &cached->raw, sizeof(git_rawobj)); - found_base = 1; - } - - if (!cached) { /* have to inflate it */ - error = git_packfile_unpack(&base, p, &base_offset); - - /* - * TODO: git.git tries to load the base from other packfiles - * or loose objects. - * - * We'll need to do this in order to support thin packs. - */ - if (error < 0) - return error; - } - - error = packfile_unpack_compressed(&delta, p, w_curs, curpos, delta_size, delta_type); - git_mwindow_close(w_curs); - - if (error < 0) { - if (!found_base) - git__free(base.data); - return error; - } - - obj->type = base.type; - error = git__delta_apply(obj, base.data, base.len, delta.data, delta.len); - if (error < 0) - goto on_error; - - if (found_base) - git_atomic_dec(&cached->refcount); - else if (cache_add(&p->bases, &base, base_key) < 0) - git__free(base.data); - -on_error: - git__free(delta.data); - - return error; /* error set by git__delta_apply */ -} - -int git_packfile_unpack( - git_rawobj *obj, - struct git_pack_file *p, - git_off_t *obj_offset) -{ - git_mwindow *w_curs = NULL; - git_off_t curpos = *obj_offset; - int error; - - size_t size = 0; - git_otype type; - - /* - * TODO: optionally check the CRC on the packfile - */ - - obj->data = NULL; - obj->len = 0; - obj->type = GIT_OBJ_BAD; - - error = git_packfile_unpack_header(&size, &type, &p->mwf, &w_curs, &curpos); - git_mwindow_close(&w_curs); - - if (error < 0) - return error; - - switch (type) { - case GIT_OBJ_OFS_DELTA: - case GIT_OBJ_REF_DELTA: - error = packfile_unpack_delta( - obj, p, &w_curs, &curpos, - size, type, *obj_offset); - break; - - case GIT_OBJ_COMMIT: - case GIT_OBJ_TREE: - case GIT_OBJ_BLOB: - case GIT_OBJ_TAG: - error = packfile_unpack_compressed( - obj, p, &w_curs, &curpos, - size, type); - break; - - default: - error = packfile_error("invalid packfile type in header");; - break; - } - - *obj_offset = curpos; - return error; -} - -static void *use_git_alloc(void *opaq, unsigned int count, unsigned int size) -{ - GIT_UNUSED(opaq); - return git__calloc(count, size); -} - -static void use_git_free(void *opaq, void *ptr) -{ - GIT_UNUSED(opaq); - git__free(ptr); -} - -int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, git_off_t curpos) -{ - int st; - - memset(obj, 0, sizeof(git_packfile_stream)); - obj->curpos = curpos; - obj->p = p; - obj->zstream.zalloc = use_git_alloc; - obj->zstream.zfree = use_git_free; - obj->zstream.next_in = Z_NULL; - obj->zstream.next_out = Z_NULL; - st = inflateInit(&obj->zstream); - if (st != Z_OK) { - git__free(obj); - giterr_set(GITERR_ZLIB, "Failed to inflate packfile"); - return -1; - } - - return 0; -} - -ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len) -{ - unsigned char *in; - size_t written; - int st; - - if (obj->done) - return 0; - - in = pack_window_open(obj->p, &obj->mw, obj->curpos, &obj->zstream.avail_in); - if (in == NULL) - return GIT_EBUFS; - - obj->zstream.next_out = buffer; - obj->zstream.avail_out = (unsigned int)len; - obj->zstream.next_in = in; - - st = inflate(&obj->zstream, Z_SYNC_FLUSH); - git_mwindow_close(&obj->mw); - - obj->curpos += obj->zstream.next_in - in; - written = len - obj->zstream.avail_out; - - if (st != Z_OK && st != Z_STREAM_END) { - giterr_set(GITERR_ZLIB, "Failed to inflate packfile"); - return -1; - } - - if (st == Z_STREAM_END) - obj->done = 1; - - - /* If we didn't write anything out but we're not done, we need more data */ - if (!written && st != Z_STREAM_END) - return GIT_EBUFS; - - return written; - -} - -void git_packfile_stream_free(git_packfile_stream *obj) -{ - inflateEnd(&obj->zstream); -} - -int packfile_unpack_compressed( - git_rawobj *obj, - struct git_pack_file *p, - git_mwindow **w_curs, - git_off_t *curpos, - size_t size, - git_otype type) -{ - int st; - z_stream stream; - unsigned char *buffer, *in; - - buffer = git__calloc(1, size + 1); - GITERR_CHECK_ALLOC(buffer); - - memset(&stream, 0, sizeof(stream)); - stream.next_out = buffer; - stream.avail_out = (uInt)size + 1; - stream.zalloc = use_git_alloc; - stream.zfree = use_git_free; - - st = inflateInit(&stream); - if (st != Z_OK) { - git__free(buffer); - giterr_set(GITERR_ZLIB, "Failed to inflate packfile"); - - return -1; - } - - do { - in = pack_window_open(p, w_curs, *curpos, &stream.avail_in); - stream.next_in = in; - st = inflate(&stream, Z_FINISH); - git_mwindow_close(w_curs); - - if (!stream.avail_out) - break; /* the payload is larger than it should be */ - - if (st == Z_BUF_ERROR && in == NULL) { - inflateEnd(&stream); - git__free(buffer); - return GIT_EBUFS; - } - - *curpos += stream.next_in - in; - } while (st == Z_OK || st == Z_BUF_ERROR); - - inflateEnd(&stream); - - if ((st != Z_STREAM_END) || stream.total_out != size) { - git__free(buffer); - giterr_set(GITERR_ZLIB, "Failed to inflate packfile"); - return -1; - } - - obj->type = type; - obj->len = size; - obj->data = buffer; - return 0; -} - -/* - * curpos is where the data starts, delta_obj_offset is the where the - * header starts - */ -git_off_t get_delta_base( - struct git_pack_file *p, - git_mwindow **w_curs, - git_off_t *curpos, - git_otype type, - git_off_t delta_obj_offset) -{ - unsigned int left = 0; - unsigned char *base_info; - git_off_t base_offset; - git_oid unused; - - base_info = pack_window_open(p, w_curs, *curpos, &left); - /* Assumption: the only reason this would fail is because the file is too small */ - if (base_info == NULL) - return GIT_EBUFS; - /* pack_window_open() assured us we have [base_info, base_info + 20) - * as a range that we can look at without walking off the - * end of the mapped window. Its actually the hash size - * that is assured. An OFS_DELTA longer than the hash size - * is stupid, as then a REF_DELTA would be smaller to store. - */ - if (type == GIT_OBJ_OFS_DELTA) { - unsigned used = 0; - unsigned char c = base_info[used++]; - base_offset = c & 127; - while (c & 128) { - if (left <= used) - return GIT_EBUFS; - base_offset += 1; - if (!base_offset || MSB(base_offset, 7)) - return 0; /* overflow */ - c = base_info[used++]; - base_offset = (base_offset << 7) + (c & 127); - } - base_offset = delta_obj_offset - base_offset; - if (base_offset <= 0 || base_offset >= delta_obj_offset) - return 0; /* out of bound */ - *curpos += used; - } else if (type == GIT_OBJ_REF_DELTA) { - /* If we have the cooperative cache, search in it first */ - if (p->has_cache) { - size_t pos; - struct git_pack_entry key; - - git_oid_fromraw(&key.sha1, base_info); - if (!git_vector_bsearch(&pos, &p->cache, &key)) { - *curpos += 20; - return ((struct git_pack_entry *)git_vector_get(&p->cache, pos))->offset; - } - } - /* The base entry _must_ be in the same pack */ - if (pack_entry_find_offset(&base_offset, &unused, p, (git_oid *)base_info, GIT_OID_HEXSZ) < 0) - return packfile_error("base entry delta is not in the same pack"); - *curpos += 20; - } else - return 0; - - return base_offset; -} - -/*********************************************************** - * - * PACKFILE METHODS - * - ***********************************************************/ - -static struct git_pack_file *packfile_alloc(size_t extra) -{ - struct git_pack_file *p = git__calloc(1, sizeof(*p) + extra); - if (p != NULL) - p->mwf.fd = -1; - return p; -} - - -void git_packfile_free(struct git_pack_file *p) -{ - assert(p); - - cache_free(&p->bases); - - git_mwindow_free_all(&p->mwf); - git_mwindow_file_deregister(&p->mwf); - - if (p->mwf.fd != -1) - p_close(p->mwf.fd); - - pack_index_free(p); - - git__free(p->bad_object_sha1); - git__free(p); -} - -static int packfile_open(struct git_pack_file *p) -{ - struct stat st; - struct git_pack_header hdr; - git_oid sha1; - unsigned char *idx_sha1; - - assert(p->index_map.data); - - if (!p->index_map.data && pack_index_open(p) < 0) - return git_odb__error_notfound("failed to open packfile", NULL); - - /* TODO: open with noatime */ - p->mwf.fd = git_futils_open_ro(p->pack_name); - if (p->mwf.fd < 0) { - p->mwf.fd = -1; - return -1; - } - - if (p_fstat(p->mwf.fd, &st) < 0 || - git_mwindow_file_register(&p->mwf) < 0) - goto cleanup; - - /* If we created the struct before we had the pack we lack size. */ - if (!p->mwf.size) { - if (!S_ISREG(st.st_mode)) - goto cleanup; - p->mwf.size = (git_off_t)st.st_size; - } else if (p->mwf.size != st.st_size) - goto cleanup; - -#if 0 - /* We leave these file descriptors open with sliding mmap; - * there is no point keeping them open across exec(), though. - */ - fd_flag = fcntl(p->mwf.fd, F_GETFD, 0); - if (fd_flag < 0) - goto cleanup; - - fd_flag |= FD_CLOEXEC; - if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1) - goto cleanup; -#endif - - /* Verify we recognize this pack file format. */ - if (p_read(p->mwf.fd, &hdr, sizeof(hdr)) < 0 || - hdr.hdr_signature != htonl(PACK_SIGNATURE) || - !pack_version_ok(hdr.hdr_version)) - goto cleanup; - - /* Verify the pack matches its index. */ - if (p->num_objects != ntohl(hdr.hdr_entries) || - p_lseek(p->mwf.fd, p->mwf.size - GIT_OID_RAWSZ, SEEK_SET) == -1 || - p_read(p->mwf.fd, sha1.id, GIT_OID_RAWSZ) < 0) - goto cleanup; - - idx_sha1 = ((unsigned char *)p->index_map.data) + p->index_map.len - 40; - - if (git_oid_cmp(&sha1, (git_oid *)idx_sha1) == 0) - return 0; - -cleanup: - giterr_set(GITERR_OS, "Invalid packfile '%s'", p->pack_name); - p_close(p->mwf.fd); - p->mwf.fd = -1; - return -1; -} - -int git_packfile_check(struct git_pack_file **pack_out, const char *path) -{ - struct stat st; - struct git_pack_file *p; - size_t path_len; - - *pack_out = NULL; - path_len = strlen(path); - p = packfile_alloc(path_len + 2); - GITERR_CHECK_ALLOC(p); - - /* - * Make sure a corresponding .pack file exists and that - * the index looks sane. - */ - path_len -= strlen(".idx"); - if (path_len < 1) { - git__free(p); - return git_odb__error_notfound("invalid packfile path", NULL); - } - - memcpy(p->pack_name, path, path_len); - - strcpy(p->pack_name + path_len, ".keep"); - if (git_path_exists(p->pack_name) == true) - p->pack_keep = 1; - - strcpy(p->pack_name + path_len, ".pack"); - if (p_stat(p->pack_name, &st) < 0 || !S_ISREG(st.st_mode)) { - git__free(p); - return git_odb__error_notfound("packfile not found", NULL); - } - - /* ok, it looks sane as far as we can check without - * actually mapping the pack file. - */ - p->mwf.size = st.st_size; - p->pack_local = 1; - p->mtime = (git_time_t)st.st_mtime; - - /* see if we can parse the sha1 oid in the packfile name */ - if (path_len < 40 || - git_oid_fromstr(&p->sha1, path + path_len - GIT_OID_HEXSZ) < 0) - memset(&p->sha1, 0x0, GIT_OID_RAWSZ); - - *pack_out = p; - - return 0; -} - -/*********************************************************** - * - * PACKFILE ENTRY SEARCH INTERNALS - * - ***********************************************************/ - -static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_t n) -{ - const unsigned char *index = p->index_map.data; - index += 4 * 256; - if (p->index_version == 1) { - return ntohl(*((uint32_t *)(index + 24 * n))); - } else { - uint32_t off; - index += 8 + p->num_objects * (20 + 4); - off = ntohl(*((uint32_t *)(index + 4 * n))); - if (!(off & 0x80000000)) - return off; - index += p->num_objects * 4 + (off & 0x7fffffff) * 8; - return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) | - ntohl(*((uint32_t *)(index + 4))); - } -} - -static int git__memcmp4(const void *a, const void *b) { - return memcmp(a, b, 4); -} - -int git_pack_foreach_entry( - struct git_pack_file *p, - git_odb_foreach_cb cb, - void *data) -{ - const unsigned char *index = p->index_map.data, *current; - uint32_t i; - - if (index == NULL) { - int error; - - if ((error = pack_index_open(p)) < 0) - return error; - - assert(p->index_map.data); - - index = p->index_map.data; - } - - if (p->index_version > 1) { - index += 8; - } - - index += 4 * 256; - - if (p->oids == NULL) { - git_vector offsets, oids; - int error; - - if ((error = git_vector_init(&oids, p->num_objects, NULL))) - return error; - - if ((error = git_vector_init(&offsets, p->num_objects, git__memcmp4))) - return error; - - if (p->index_version > 1) { - const unsigned char *off = index + 24 * p->num_objects; - for (i = 0; i < p->num_objects; i++) - git_vector_insert(&offsets, (void*)&off[4 * i]); - git_vector_sort(&offsets); - git_vector_foreach(&offsets, i, current) - git_vector_insert(&oids, (void*)&index[5 * (current - off)]); - } else { - for (i = 0; i < p->num_objects; i++) - git_vector_insert(&offsets, (void*)&index[24 * i]); - git_vector_sort(&offsets); - git_vector_foreach(&offsets, i, current) - git_vector_insert(&oids, (void*)¤t[4]); - } - git_vector_free(&offsets); - p->oids = (git_oid **)oids.contents; - } - - for (i = 0; i < p->num_objects; i++) - if (cb(p->oids[i], data)) - return GIT_EUSER; - - return 0; -} - -static int pack_entry_find_offset( - git_off_t *offset_out, - git_oid *found_oid, - struct git_pack_file *p, - const git_oid *short_oid, - size_t len) -{ - const uint32_t *level1_ofs = p->index_map.data; - const unsigned char *index = p->index_map.data; - unsigned hi, lo, stride; - int pos, found = 0; - const unsigned char *current = 0; - - *offset_out = 0; - - if (index == NULL) { - int error; - - if ((error = pack_index_open(p)) < 0) - return error; - - assert(p->index_map.data); - - index = p->index_map.data; - level1_ofs = p->index_map.data; - } - - if (p->index_version > 1) { - level1_ofs += 2; - index += 8; - } - - index += 4 * 256; - hi = ntohl(level1_ofs[(int)short_oid->id[0]]); - lo = ((short_oid->id[0] == 0x0) ? 0 : ntohl(level1_ofs[(int)short_oid->id[0] - 1])); - - if (p->index_version > 1) { - stride = 20; - } else { - stride = 24; - index += 4; - } - -#ifdef INDEX_DEBUG_LOOKUP - printf("%02x%02x%02x... lo %u hi %u nr %d\n", - short_oid->id[0], short_oid->id[1], short_oid->id[2], lo, hi, p->num_objects); -#endif - - /* Use git.git lookup code */ - pos = sha1_entry_pos(index, stride, 0, lo, hi, p->num_objects, short_oid->id); - - if (pos >= 0) { - /* An object matching exactly the oid was found */ - found = 1; - current = index + pos * stride; - } else { - /* No object was found */ - /* pos refers to the object with the "closest" oid to short_oid */ - pos = - 1 - pos; - if (pos < (int)p->num_objects) { - current = index + pos * stride; - - if (!git_oid_ncmp(short_oid, (const git_oid *)current, len)) - found = 1; - } - } - - if (found && len != GIT_OID_HEXSZ && pos + 1 < (int)p->num_objects) { - /* Check for ambiguousity */ - const unsigned char *next = current + stride; - - if (!git_oid_ncmp(short_oid, (const git_oid *)next, len)) { - found = 2; - } - } - - if (!found) - return git_odb__error_notfound("failed to find offset for pack entry", short_oid); - if (found > 1) - return git_odb__error_ambiguous("found multiple offsets for pack entry"); - *offset_out = nth_packed_object_offset(p, pos); - git_oid_fromraw(found_oid, current); - -#ifdef INDEX_DEBUG_LOOKUP - { - unsigned char hex_sha1[GIT_OID_HEXSZ + 1]; - git_oid_fmt(hex_sha1, found_oid); - hex_sha1[GIT_OID_HEXSZ] = '\0'; - printf("found lo=%d %s\n", lo, hex_sha1); - } -#endif - return 0; -} - -int git_pack_entry_find( - struct git_pack_entry *e, - struct git_pack_file *p, - const git_oid *short_oid, - size_t len) -{ - git_off_t offset; - git_oid found_oid; - int error; - - assert(p); - - if (len == GIT_OID_HEXSZ && p->num_bad_objects) { - unsigned i; - for (i = 0; i < p->num_bad_objects; i++) - if (git_oid_cmp(short_oid, &p->bad_object_sha1[i]) == 0) - return packfile_error("bad object found in packfile"); - } - - error = pack_entry_find_offset(&offset, &found_oid, p, short_oid, len); - if (error < 0) - return error; - - /* we found a unique entry in the index; - * make sure the packfile backing the index - * still exists on disk */ - if (p->mwf.fd == -1 && (error = packfile_open(p)) < 0) - return error; - - e->offset = offset; - e->p = p; - - git_oid_cpy(&e->sha1, &found_oid); - return 0; -} diff --git a/src/pack.h b/src/pack.h deleted file mode 100644 index 6c43d8f5b64..00000000000 --- a/src/pack.h +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#ifndef INCLUDE_pack_h__ -#define INCLUDE_pack_h__ - -#include - -#include "git2/oid.h" - -#include "common.h" -#include "map.h" -#include "mwindow.h" -#include "odb.h" - -#define GIT_PACK_FILE_MODE 0444 - -#define PACK_SIGNATURE 0x5041434b /* "PACK" */ -#define PACK_VERSION 2 -#define pack_version_ok(v) ((v) == htonl(2) || (v) == htonl(3)) -struct git_pack_header { - uint32_t hdr_signature; - uint32_t hdr_version; - uint32_t hdr_entries; -}; - -/* - * The first four bytes of index formats later than version 1 should - * start with this signature, as all older git binaries would find this - * value illegal and abort reading the file. - * - * This is the case because the number of objects in a packfile - * cannot exceed 1,431,660,000 as every object would need at least - * 3 bytes of data and the overall packfile cannot exceed 4 GiB with - * version 1 of the index file due to the offsets limited to 32 bits. - * Clearly the signature exceeds this maximum. - * - * Very old git binaries will also compare the first 4 bytes to the - * next 4 bytes in the index and abort with a "non-monotonic index" - * error if the second 4 byte word is smaller than the first 4 - * byte word. This would be true in the proposed future index - * format as idx_signature would be greater than idx_version. - */ - -#define PACK_IDX_SIGNATURE 0xff744f63 /* "\377tOc" */ - -struct git_pack_idx_header { - uint32_t idx_signature; - uint32_t idx_version; -}; - -typedef struct git_pack_cache_entry { - size_t last_usage; /* enough? */ - git_atomic refcount; - git_rawobj raw; -} git_pack_cache_entry; - -#include "offmap.h" - -GIT__USE_OFFMAP; - -#define GIT_PACK_CACHE_MEMORY_LIMIT 16 * 1024 * 1024 -#define GIT_PACK_CACHE_SIZE_LIMIT 1024 * 1024 /* don't bother caching anything over 1MB */ - -typedef struct { - size_t memory_used; - size_t memory_limit; - size_t use_ctr; - git_mutex lock; - git_offmap *entries; -} git_pack_cache; - -struct git_pack_file { - git_mwindow_file mwf; - git_map index_map; - - uint32_t num_objects; - uint32_t num_bad_objects; - git_oid *bad_object_sha1; /* array of git_oid */ - - int index_version; - git_time_t mtime; - unsigned pack_local:1, pack_keep:1, has_cache:1; - git_oid sha1; - git_vector cache; - git_oid **oids; - - git_pack_cache bases; /* delta base cache */ - - /* something like ".git/objects/pack/xxxxx.pack" */ - char pack_name[GIT_FLEX_ARRAY]; /* more */ -}; - -struct git_pack_entry { - git_off_t offset; - git_oid sha1; - struct git_pack_file *p; -}; - -typedef struct git_packfile_stream { - git_off_t curpos; - int done; - z_stream zstream; - struct git_pack_file *p; - git_mwindow *mw; -} git_packfile_stream; - -int git_packfile_unpack_header( - size_t *size_p, - git_otype *type_p, - git_mwindow_file *mwf, - git_mwindow **w_curs, - git_off_t *curpos); - -int git_packfile_resolve_header( - size_t *size_p, - git_otype *type_p, - struct git_pack_file *p, - git_off_t offset); - -int git_packfile_unpack(git_rawobj *obj, struct git_pack_file *p, git_off_t *obj_offset); -int packfile_unpack_compressed( - git_rawobj *obj, - struct git_pack_file *p, - git_mwindow **w_curs, - git_off_t *curpos, - size_t size, - git_otype type); - -int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, git_off_t curpos); -ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len); -void git_packfile_stream_free(git_packfile_stream *obj); - -git_off_t get_delta_base(struct git_pack_file *p, git_mwindow **w_curs, - git_off_t *curpos, git_otype type, - git_off_t delta_obj_offset); - -void git_packfile_free(struct git_pack_file *p); -int git_packfile_check(struct git_pack_file **pack_out, const char *path); -int git_pack_entry_find( - struct git_pack_entry *e, - struct git_pack_file *p, - const git_oid *short_oid, - size_t len); -int git_pack_foreach_entry( - struct git_pack_file *p, - git_odb_foreach_cb cb, - void *data); - -#endif diff --git a/src/path.c b/src/path.c deleted file mode 100644 index 5de58cce7b2..00000000000 --- a/src/path.c +++ /dev/null @@ -1,921 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "common.h" -#include "path.h" -#include "posix.h" -#ifdef GIT_WIN32 -#include "win32/dir.h" -#include "win32/posix.h" -#else -#include -#endif -#include -#include -#include - -#define LOOKS_LIKE_DRIVE_PREFIX(S) (git__isalpha((S)[0]) && (S)[1] == ':') - -#ifdef GIT_WIN32 -static bool looks_like_network_computer_name(const char *path, int pos) -{ - if (pos < 3) - return false; - - if (path[0] != '/' || path[1] != '/') - return false; - - while (pos-- > 2) { - if (path[pos] == '/') - return false; - } - - return true; -} -#endif - -/* - * Based on the Android implementation, BSD licensed. - * http://android.git.kernel.org/ - * - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -int git_path_basename_r(git_buf *buffer, const char *path) -{ - const char *endp, *startp; - int len, result; - - /* Empty or NULL string gets treated as "." */ - if (path == NULL || *path == '\0') { - startp = "."; - len = 1; - goto Exit; - } - - /* Strip trailing slashes */ - endp = path + strlen(path) - 1; - while (endp > path && *endp == '/') - endp--; - - /* All slashes becomes "/" */ - if (endp == path && *endp == '/') { - startp = "/"; - len = 1; - goto Exit; - } - - /* Find the start of the base */ - startp = endp; - while (startp > path && *(startp - 1) != '/') - startp--; - - /* Cast is safe because max path < max int */ - len = (int)(endp - startp + 1); - -Exit: - result = len; - - if (buffer != NULL && git_buf_set(buffer, startp, len) < 0) - return -1; - - return result; -} - -/* - * Based on the Android implementation, BSD licensed. - * Check http://android.git.kernel.org/ - */ -int git_path_dirname_r(git_buf *buffer, const char *path) -{ - const char *endp; - int result, len; - - /* Empty or NULL string gets treated as "." */ - if (path == NULL || *path == '\0') { - path = "."; - len = 1; - goto Exit; - } - - /* Strip trailing slashes */ - endp = path + strlen(path) - 1; - while (endp > path && *endp == '/') - endp--; - - /* Find the start of the dir */ - while (endp > path && *endp != '/') - endp--; - - /* Either the dir is "/" or there are no slashes */ - if (endp == path) { - path = (*endp == '/') ? "/" : "."; - len = 1; - goto Exit; - } - - do { - endp--; - } while (endp > path && *endp == '/'); - - /* Cast is safe because max path < max int */ - len = (int)(endp - path + 1); - -#ifdef GIT_WIN32 - /* Mimic unix behavior where '/.git' returns '/': 'C:/.git' will return - 'C:/' here */ - - if (len == 2 && LOOKS_LIKE_DRIVE_PREFIX(path)) { - len = 3; - goto Exit; - } - - /* Similarly checks if we're dealing with a network computer name - '//computername/.git' will return '//computername/' */ - - if (looks_like_network_computer_name(path, len)) { - len++; - goto Exit; - } - -#endif - -Exit: - result = len; - - if (buffer != NULL && git_buf_set(buffer, path, len) < 0) - return -1; - - return result; -} - - -char *git_path_dirname(const char *path) -{ - git_buf buf = GIT_BUF_INIT; - char *dirname; - - git_path_dirname_r(&buf, path); - dirname = git_buf_detach(&buf); - git_buf_free(&buf); /* avoid memleak if error occurs */ - - return dirname; -} - -char *git_path_basename(const char *path) -{ - git_buf buf = GIT_BUF_INIT; - char *basename; - - git_path_basename_r(&buf, path); - basename = git_buf_detach(&buf); - git_buf_free(&buf); /* avoid memleak if error occurs */ - - return basename; -} - -size_t git_path_basename_offset(git_buf *buffer) -{ - ssize_t slash; - - if (!buffer || buffer->size <= 0) - return 0; - - slash = git_buf_rfind_next(buffer, '/'); - - if (slash >= 0 && buffer->ptr[slash] == '/') - return (size_t)(slash + 1); - - return 0; -} - -const char *git_path_topdir(const char *path) -{ - size_t len; - ssize_t i; - - assert(path); - len = strlen(path); - - if (!len || path[len - 1] != '/') - return NULL; - - for (i = (ssize_t)len - 2; i >= 0; --i) - if (path[i] == '/') - break; - - return &path[i + 1]; -} - -int git_path_root(const char *path) -{ - int offset = 0; - - /* Does the root of the path look like a windows drive ? */ - if (LOOKS_LIKE_DRIVE_PREFIX(path)) - offset += 2; - -#ifdef GIT_WIN32 - /* Are we dealing with a windows network path? */ - else if ((path[0] == '/' && path[1] == '/') || - (path[0] == '\\' && path[1] == '\\')) - { - offset += 2; - - /* Skip the computer name segment */ - while (path[offset] && path[offset] != '/' && path[offset] != '\\') - offset++; - } -#endif - - if (path[offset] == '/' || path[offset] == '\\') - return offset; - - return -1; /* Not a real error - signals that path is not rooted */ -} - -int git_path_join_unrooted( - git_buf *path_out, const char *path, const char *base, ssize_t *root_at) -{ - int error, root; - - assert(path && path_out); - - root = git_path_root(path); - - if (base != NULL && root < 0) { - error = git_buf_joinpath(path_out, base, path); - - if (root_at) - *root_at = (ssize_t)strlen(base); - } - else { - error = git_buf_sets(path_out, path); - - if (root_at) - *root_at = (root < 0) ? 0 : (ssize_t)root; - } - - return error; -} - -int git_path_prettify(git_buf *path_out, const char *path, const char *base) -{ - char buf[GIT_PATH_MAX]; - - assert(path && path_out); - - /* construct path if needed */ - if (base != NULL && git_path_root(path) < 0) { - if (git_buf_joinpath(path_out, base, path) < 0) - return -1; - path = path_out->ptr; - } - - if (p_realpath(path, buf) == NULL) { - /* giterr_set resets the errno when dealing with a GITERR_OS kind of error */ - int error = (errno == ENOENT || errno == ENOTDIR) ? GIT_ENOTFOUND : -1; - giterr_set(GITERR_OS, "Failed to resolve path '%s'", path); - - git_buf_clear(path_out); - - return error; - } - - return git_buf_sets(path_out, buf); -} - -int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base) -{ - int error = git_path_prettify(path_out, path, base); - return (error < 0) ? error : git_path_to_dir(path_out); -} - -int git_path_to_dir(git_buf *path) -{ - if (path->asize > 0 && - git_buf_len(path) > 0 && - path->ptr[git_buf_len(path) - 1] != '/') - git_buf_putc(path, '/'); - - return git_buf_oom(path) ? -1 : 0; -} - -void git_path_string_to_dir(char* path, size_t size) -{ - size_t end = strlen(path); - - if (end && path[end - 1] != '/' && end < size) { - path[end] = '/'; - path[end + 1] = '\0'; - } -} - -int git__percent_decode(git_buf *decoded_out, const char *input) -{ - int len, hi, lo, i; - assert(decoded_out && input); - - len = (int)strlen(input); - git_buf_clear(decoded_out); - - for(i = 0; i < len; i++) - { - char c = input[i]; - - if (c != '%') - goto append; - - if (i >= len - 2) - goto append; - - hi = git__fromhex(input[i + 1]); - lo = git__fromhex(input[i + 2]); - - if (hi < 0 || lo < 0) - goto append; - - c = (char)(hi << 4 | lo); - i += 2; - -append: - if (git_buf_putc(decoded_out, c) < 0) - return -1; - } - - return 0; -} - -static int error_invalid_local_file_uri(const char *uri) -{ - giterr_set(GITERR_CONFIG, "'%s' is not a valid local file URI", uri); - return -1; -} - -int git_path_fromurl(git_buf *local_path_out, const char *file_url) -{ - int offset = 0, len; - - assert(local_path_out && file_url); - - if (git__prefixcmp(file_url, "file://") != 0) - return error_invalid_local_file_uri(file_url); - - offset += 7; - len = (int)strlen(file_url); - - if (offset < len && file_url[offset] == '/') - offset++; - else if (offset < len && git__prefixcmp(file_url + offset, "localhost/") == 0) - offset += 10; - else - return error_invalid_local_file_uri(file_url); - - if (offset >= len || file_url[offset] == '/') - return error_invalid_local_file_uri(file_url); - -#ifndef _MSC_VER - offset--; /* A *nix absolute path starts with a forward slash */ -#endif - - git_buf_clear(local_path_out); - - return git__percent_decode(local_path_out, file_url + offset); -} - -int git_path_walk_up( - git_buf *path, - const char *ceiling, - int (*cb)(void *data, git_buf *), - void *data) -{ - int error = 0; - git_buf iter; - ssize_t stop = 0, scan; - char oldc = '\0'; - - assert(path && cb); - - if (ceiling != NULL) { - if (git__prefixcmp(path->ptr, ceiling) == 0) - stop = (ssize_t)strlen(ceiling); - else - stop = git_buf_len(path); - } - scan = git_buf_len(path); - - iter.ptr = path->ptr; - iter.size = git_buf_len(path); - iter.asize = path->asize; - - while (scan >= stop) { - error = cb(data, &iter); - iter.ptr[scan] = oldc; - if (error < 0) - break; - scan = git_buf_rfind_next(&iter, '/'); - if (scan >= 0) { - scan++; - oldc = iter.ptr[scan]; - iter.size = scan; - iter.ptr[scan] = '\0'; - } - } - - if (scan >= 0) - iter.ptr[scan] = oldc; - - return error; -} - -bool git_path_exists(const char *path) -{ - assert(path); - return p_access(path, F_OK) == 0; -} - -bool git_path_isdir(const char *path) -{ - struct stat st; - if (p_stat(path, &st) < 0) - return false; - - return S_ISDIR(st.st_mode) != 0; -} - -bool git_path_isfile(const char *path) -{ - struct stat st; - - assert(path); - if (p_stat(path, &st) < 0) - return false; - - return S_ISREG(st.st_mode) != 0; -} - -#ifdef GIT_WIN32 - -bool git_path_is_empty_dir(const char *path) -{ - git_buf pathbuf = GIT_BUF_INIT; - HANDLE hFind = INVALID_HANDLE_VALUE; - wchar_t wbuf[GIT_WIN_PATH]; - WIN32_FIND_DATAW ffd; - bool retval = true; - - if (!git_path_isdir(path)) return false; - - git_buf_printf(&pathbuf, "%s\\*", path); - git__utf8_to_16(wbuf, GIT_WIN_PATH, git_buf_cstr(&pathbuf)); - - hFind = FindFirstFileW(wbuf, &ffd); - if (INVALID_HANDLE_VALUE == hFind) { - giterr_set(GITERR_OS, "Couldn't open '%s'", path); - return false; - } - - do { - if (!git_path_is_dot_or_dotdotW(ffd.cFileName)) { - retval = false; - } - } while (FindNextFileW(hFind, &ffd) != 0); - - FindClose(hFind); - git_buf_free(&pathbuf); - return retval; -} - -#else - -bool git_path_is_empty_dir(const char *path) -{ - DIR *dir = NULL; - struct dirent *e; - bool retval = true; - - if (!git_path_isdir(path)) return false; - - dir = opendir(path); - if (!dir) { - giterr_set(GITERR_OS, "Couldn't open '%s'", path); - return false; - } - - while ((e = readdir(dir)) != NULL) { - if (!git_path_is_dot_or_dotdot(e->d_name)) { - giterr_set(GITERR_INVALID, - "'%s' exists and is not an empty directory", path); - retval = false; - break; - } - } - closedir(dir); - - return retval; -} -#endif - -int git_path_lstat(const char *path, struct stat *st) -{ - int err = 0; - - if (p_lstat(path, st) < 0) { - err = (errno == ENOENT) ? GIT_ENOTFOUND : -1; - giterr_set(GITERR_OS, "Failed to stat file '%s'", path); - } - - return err; -} - -static bool _check_dir_contents( - git_buf *dir, - const char *sub, - bool (*predicate)(const char *)) -{ - bool result; - size_t dir_size = git_buf_len(dir); - size_t sub_size = strlen(sub); - - /* leave base valid even if we could not make space for subdir */ - if (git_buf_try_grow(dir, dir_size + sub_size + 2, false) < 0) - return false; - - /* save excursion */ - git_buf_joinpath(dir, dir->ptr, sub); - - result = predicate(dir->ptr); - - /* restore path */ - git_buf_truncate(dir, dir_size); - return result; -} - -bool git_path_contains(git_buf *dir, const char *item) -{ - return _check_dir_contents(dir, item, &git_path_exists); -} - -bool git_path_contains_dir(git_buf *base, const char *subdir) -{ - return _check_dir_contents(base, subdir, &git_path_isdir); -} - -bool git_path_contains_file(git_buf *base, const char *file) -{ - return _check_dir_contents(base, file, &git_path_isfile); -} - -int git_path_find_dir(git_buf *dir, const char *path, const char *base) -{ - int error = git_path_join_unrooted(dir, path, base, NULL); - - if (!error) { - char buf[GIT_PATH_MAX]; - if (p_realpath(dir->ptr, buf) != NULL) - error = git_buf_sets(dir, buf); - } - - /* call dirname if this is not a directory */ - if (!error && git_path_isdir(dir->ptr) == false) - error = git_path_dirname_r(dir, dir->ptr); - - if (!error) - error = git_path_to_dir(dir); - - return error; -} - -int git_path_resolve_relative(git_buf *path, size_t ceiling) -{ - char *base, *to, *from, *next; - size_t len; - - if (!path || git_buf_oom(path)) - return -1; - - if (ceiling > path->size) - ceiling = path->size; - - /* recognize drive prefixes, etc. that should not be backed over */ - if (ceiling == 0) - ceiling = git_path_root(path->ptr) + 1; - - /* recognize URL prefixes that should not be backed over */ - if (ceiling == 0) { - for (next = path->ptr; *next && git__isalpha(*next); ++next); - if (next[0] == ':' && next[1] == '/' && next[2] == '/') - ceiling = (next + 3) - path->ptr; - } - - base = to = from = path->ptr + ceiling; - - while (*from) { - for (next = from; *next && *next != '/'; ++next); - - len = next - from; - - if (len == 1 && from[0] == '.') - /* do nothing with singleton dot */; - - else if (len == 2 && from[0] == '.' && from[1] == '.') { - while (to > base && to[-1] == '/') to--; - while (to > base && to[-1] != '/') to--; - } - - else { - if (*next == '/') - len++; - - if (to != from) - memmove(to, from, len); - - to += len; - } - - from += len; - - while (*from == '/') from++; - } - - *to = '\0'; - - path->size = to - path->ptr; - - return 0; -} - -int git_path_apply_relative(git_buf *target, const char *relpath) -{ - git_buf_joinpath(target, git_buf_cstr(target), relpath); - return git_path_resolve_relative(target, 0); -} - -int git_path_cmp( - const char *name1, size_t len1, int isdir1, - const char *name2, size_t len2, int isdir2) -{ - unsigned char c1, c2; - size_t len = len1 < len2 ? len1 : len2; - int cmp; - - cmp = memcmp(name1, name2, len); - if (cmp) - return cmp; - - c1 = name1[len]; - c2 = name2[len]; - - if (c1 == '\0' && isdir1) - c1 = '/'; - - if (c2 == '\0' && isdir2) - c2 = '/'; - - return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; -} - -int git_path_icmp( - const char *name1, size_t len1, int isdir1, - const char *name2, size_t len2, int isdir2) -{ - unsigned char c1, c2; - size_t len = len1 < len2 ? len1 : len2; - int cmp; - - cmp = strncasecmp(name1, name2, len); - if (cmp) - return cmp; - - c1 = name1[len]; - c2 = name2[len]; - - if (c1 == '\0' && isdir1) - c1 = '/'; - - if (c2 == '\0' && isdir2) - c2 = '/'; - - return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; -} - -int git_path_direach( - git_buf *path, - int (*fn)(void *, git_buf *), - void *arg) -{ - ssize_t wd_len; - DIR *dir; - struct dirent *de, *de_buf; - - if (git_path_to_dir(path) < 0) - return -1; - - wd_len = git_buf_len(path); - - if ((dir = opendir(path->ptr)) == NULL) { - giterr_set(GITERR_OS, "Failed to open directory '%s'", path->ptr); - return -1; - } - -#if defined(__sun) || defined(__GNU__) - de_buf = git__malloc(sizeof(struct dirent) + FILENAME_MAX + 1); -#else - de_buf = git__malloc(sizeof(struct dirent)); -#endif - - while (p_readdir_r(dir, de_buf, &de) == 0 && de != NULL) { - int result; - - if (git_path_is_dot_or_dotdot(de->d_name)) - continue; - - if (git_buf_puts(path, de->d_name) < 0) { - closedir(dir); - git__free(de_buf); - return -1; - } - - result = fn(arg, path); - - git_buf_truncate(path, wd_len); /* restore path */ - - if (result < 0) { - closedir(dir); - git__free(de_buf); - return -1; - } - } - - closedir(dir); - git__free(de_buf); - return 0; -} - -int git_path_dirload( - const char *path, - size_t prefix_len, - size_t alloc_extra, - git_vector *contents) -{ - int error, need_slash; - DIR *dir; - struct dirent *de, *de_buf; - size_t path_len; - - assert(path != NULL && contents != NULL); - path_len = strlen(path); - assert(path_len > 0 && path_len >= prefix_len); - - if ((dir = opendir(path)) == NULL) { - giterr_set(GITERR_OS, "Failed to open directory '%s'", path); - return -1; - } - -#if defined(__sun) || defined(__GNU__) - de_buf = git__malloc(sizeof(struct dirent) + FILENAME_MAX + 1); -#else - de_buf = git__malloc(sizeof(struct dirent)); -#endif - - path += prefix_len; - path_len -= prefix_len; - need_slash = (path_len > 0 && path[path_len-1] != '/') ? 1 : 0; - - while ((error = p_readdir_r(dir, de_buf, &de)) == 0 && de != NULL) { - char *entry_path; - size_t entry_len; - - if (git_path_is_dot_or_dotdot(de->d_name)) - continue; - - entry_len = strlen(de->d_name); - - entry_path = git__malloc( - path_len + need_slash + entry_len + 1 + alloc_extra); - GITERR_CHECK_ALLOC(entry_path); - - if (path_len) - memcpy(entry_path, path, path_len); - if (need_slash) - entry_path[path_len] = '/'; - memcpy(&entry_path[path_len + need_slash], de->d_name, entry_len); - entry_path[path_len + need_slash + entry_len] = '\0'; - - if (git_vector_insert(contents, entry_path) < 0) { - closedir(dir); - git__free(de_buf); - return -1; - } - } - - closedir(dir); - git__free(de_buf); - - if (error != 0) - giterr_set(GITERR_OS, "Failed to process directory entry in '%s'", path); - - return error; -} - -int git_path_with_stat_cmp(const void *a, const void *b) -{ - const git_path_with_stat *psa = a, *psb = b; - return strcmp(psa->path, psb->path); -} - -int git_path_with_stat_cmp_icase(const void *a, const void *b) -{ - const git_path_with_stat *psa = a, *psb = b; - return strcasecmp(psa->path, psb->path); -} - -int git_path_dirload_with_stat( - const char *path, - size_t prefix_len, - bool ignore_case, - const char *start_stat, - const char *end_stat, - git_vector *contents) -{ - int error; - unsigned int i; - git_path_with_stat *ps; - git_buf full = GIT_BUF_INIT; - int (*strncomp)(const char *a, const char *b, size_t sz); - size_t start_len = start_stat ? strlen(start_stat) : 0; - size_t end_len = end_stat ? strlen(end_stat) : 0, cmp_len; - - if (git_buf_set(&full, path, prefix_len) < 0) - return -1; - - error = git_path_dirload( - path, prefix_len, sizeof(git_path_with_stat) + 1, contents); - if (error < 0) { - git_buf_free(&full); - return error; - } - - strncomp = ignore_case ? git__strncasecmp : git__strncmp; - - /* stat struct at start of git_path_with_stat, so shift path text */ - git_vector_foreach(contents, i, ps) { - size_t path_len = strlen((char *)ps); - memmove(ps->path, ps, path_len + 1); - ps->path_len = path_len; - } - - git_vector_foreach(contents, i, ps) { - /* skip if before start_stat or after end_stat */ - cmp_len = min(start_len, ps->path_len); - if (cmp_len && strncomp(ps->path, start_stat, cmp_len) < 0) - continue; - cmp_len = min(end_len, ps->path_len); - if (cmp_len && strncomp(ps->path, end_stat, cmp_len) > 0) - continue; - - if ((error = git_buf_joinpath(&full, full.ptr, ps->path)) < 0 || - (error = git_path_lstat(full.ptr, &ps->st)) < 0) - break; - - git_buf_truncate(&full, prefix_len); - - if (S_ISDIR(ps->st.st_mode)) { - ps->path[ps->path_len++] = '/'; - ps->path[ps->path_len] = '\0'; - } - } - - /* sort now that directory suffix is added */ - git_vector_sort(contents); - - git_buf_free(&full); - - return error; -} diff --git a/src/path.h b/src/path.h deleted file mode 100644 index feefd65d15b..00000000000 --- a/src/path.h +++ /dev/null @@ -1,358 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_path_h__ -#define INCLUDE_path_h__ - -#include "common.h" -#include "buffer.h" -#include "vector.h" - -/** - * Path manipulation utils - * - * These are path utilities that munge paths without actually - * looking at the real filesystem. - */ - -/* - * The dirname() function shall take a pointer to a character string - * that contains a pathname, and return a pointer to a string that is a - * pathname of the parent directory of that file. Trailing '/' characters - * in the path are not counted as part of the path. - * - * If path does not contain a '/', then dirname() shall return a pointer to - * the string ".". If path is a null pointer or points to an empty string, - * dirname() shall return a pointer to the string "." . - * - * The `git_path_dirname` implementation is thread safe. The returned - * string must be manually free'd. - * - * The `git_path_dirname_r` implementation writes the dirname to a `git_buf` - * if the buffer pointer is not NULL. - * It returns an error code < 0 if there is an allocation error, otherwise - * the length of the dirname (which will be > 0). - */ -extern char *git_path_dirname(const char *path); -extern int git_path_dirname_r(git_buf *buffer, const char *path); - -/* - * This function returns the basename of the file, which is the last - * part of its full name given by fname, with the drive letter and - * leading directories stripped off. For example, the basename of - * c:/foo/bar/file.ext is file.ext, and the basename of a:foo is foo. - * - * Trailing slashes and backslashes are significant: the basename of - * c:/foo/bar/ is an empty string after the rightmost slash. - * - * The `git_path_basename` implementation is thread safe. The returned - * string must be manually free'd. - * - * The `git_path_basename_r` implementation writes the basename to a `git_buf`. - * It returns an error code < 0 if there is an allocation error, otherwise - * the length of the basename (which will be >= 0). - */ -extern char *git_path_basename(const char *path); -extern int git_path_basename_r(git_buf *buffer, const char *path); - -/* Return the offset of the start of the basename. Unlike the other - * basename functions, this returns 0 if the path is empty. - */ -extern size_t git_path_basename_offset(git_buf *buffer); - -extern const char *git_path_topdir(const char *path); - -/** - * Find offset to root of path if path has one. - * - * This will return a number >= 0 which is the offset to the start of the - * path, if the path is rooted (i.e. "/rooted/path" returns 0 and - * "c:/windows/rooted/path" returns 2). If the path is not rooted, this - * returns < 0. - */ -extern int git_path_root(const char *path); - -/** - * Ensure path has a trailing '/'. - */ -extern int git_path_to_dir(git_buf *path); - -/** - * Ensure string has a trailing '/' if there is space for it. - */ -extern void git_path_string_to_dir(char* path, size_t size); - -/** - * Taken from git.git; returns nonzero if the given path is "." or "..". - */ -GIT_INLINE(int) git_path_is_dot_or_dotdot(const char *name) -{ - return (name[0] == '.' && - (name[1] == '\0' || - (name[1] == '.' && name[2] == '\0'))); -} - -#ifdef GIT_WIN32 -GIT_INLINE(int) git_path_is_dot_or_dotdotW(const wchar_t *name) -{ - return (name[0] == L'.' && - (name[1] == L'\0' || - (name[1] == L'.' && name[2] == L'\0'))); -} - -/** - * Convert backslashes in path to forward slashes. - */ -GIT_INLINE(void) git_path_mkposix(char *path) -{ - while (*path) { - if (*path == '\\') - *path = '/'; - - path++; - } -} -#else -# define git_path_mkposix(p) /* blank */ -#endif - -extern int git__percent_decode(git_buf *decoded_out, const char *input); - -/** - * Extract path from file:// URL. - */ -extern int git_path_fromurl(git_buf *local_path_out, const char *file_url); - - -/** - * Path filesystem utils - * - * These are path utilities that actually access the filesystem. - */ - -/** - * Check if a file exists and can be accessed. - * @return true or false - */ -extern bool git_path_exists(const char *path); - -/** - * Check if the given path points to a directory. - * @return true or false - */ -extern bool git_path_isdir(const char *path); - -/** - * Check if the given path points to a regular file. - * @return true or false - */ -extern bool git_path_isfile(const char *path); - -/** - * Check if the given path is a directory, and is empty. - */ -extern bool git_path_is_empty_dir(const char *path); - -/** - * Stat a file and/or link and set error if needed. - */ -extern int git_path_lstat(const char *path, struct stat *st); - -/** - * Check if the parent directory contains the item. - * - * @param dir Directory to check. - * @param item Item that might be in the directory. - * @return 0 if item exists in directory, <0 otherwise. - */ -extern bool git_path_contains(git_buf *dir, const char *item); - -/** - * Check if the given path contains the given subdirectory. - * - * @param parent Directory path that might contain subdir - * @param subdir Subdirectory name to look for in parent - * @param append_if_exists If true, then subdir will be appended to the parent path if it does exist - * @return true if subdirectory exists, false otherwise. - */ -extern bool git_path_contains_dir(git_buf *parent, const char *subdir); - -/** - * Check if the given path contains the given file. - * - * @param dir Directory path that might contain file - * @param file File name to look for in parent - * @param append_if_exists If true, then file will be appended to the path if it does exist - * @return true if file exists, false otherwise. - */ -extern bool git_path_contains_file(git_buf *dir, const char *file); - -/** - * Prepend base to unrooted path or just copy path over. - * - * This will optionally return the index into the path where the "root" - * is, either the end of the base directory prefix or the path root. - */ -extern int git_path_join_unrooted( - git_buf *path_out, const char *path, const char *base, ssize_t *root_at); - -/** - * Clean up path, prepending base if it is not already rooted. - */ -extern int git_path_prettify(git_buf *path_out, const char *path, const char *base); - -/** - * Clean up path, prepending base if it is not already rooted and - * appending a slash. - */ -extern int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base); - -/** - * Get a directory from a path. - * - * If path is a directory, this acts like `git_path_prettify_dir` - * (cleaning up path and appending a '/'). If path is a normal file, - * this prettifies it, then removed the filename a la dirname and - * appends the trailing '/'. If the path does not exist, it is - * treated like a regular filename. - */ -extern int git_path_find_dir(git_buf *dir, const char *path, const char *base); - -/** - * Resolve relative references within a path. - * - * This eliminates "./" and "../" relative references inside a path, - * as well as condensing multiple slashes into single ones. It will - * not touch the path before the "ceiling" length. - * - * Additionally, this will recognize an "c:/" drive prefix or a "xyz://" URL - * prefix and not touch that part of the path. - */ -extern int git_path_resolve_relative(git_buf *path, size_t ceiling); - -/** - * Apply a relative path to base path. - * - * Note that the base path could be a filename or a URL and this - * should still work. The relative path is walked segment by segment - * with three rules: series of slashes will be condensed to a single - * slash, "." will be eaten with no change, and ".." will remove a - * segment from the base path. - */ -extern int git_path_apply_relative(git_buf *target, const char *relpath); - -/** - * Walk each directory entry, except '.' and '..', calling fn(state). - * - * @param pathbuf buffer the function reads the initial directory - * path from, and updates with each successive entry's name. - * @param fn function to invoke with each entry. The first arg is - * the input state and the second arg is pathbuf. The function - * may modify the pathbuf, but only by appending new text. - * @param state to pass to fn as the first arg. - * @return 0 on success, GIT_EUSER on non-zero callback, or error code - */ -extern int git_path_direach( - git_buf *pathbuf, - int (*fn)(void *, git_buf *), - void *state); - -/** - * Sort function to order two paths - */ -extern int git_path_cmp( - const char *name1, size_t len1, int isdir1, - const char *name2, size_t len2, int isdir2); - -/** Path sort function that is case insensitive */ -extern int git_path_icmp( - const char *name1, size_t len1, int isdir1, - const char *name2, size_t len2, int isdir2); - -/** - * Invoke callback up path directory by directory until the ceiling is - * reached (inclusive of a final call at the root_path). - * - * Returning anything other than 0 from the callback function - * will stop the iteration and propogate the error to the caller. - * - * @param pathbuf Buffer the function reads the directory from and - * and updates with each successive name. - * @param ceiling Prefix of path at which to stop walking up. If NULL, - * this will walk all the way up to the root. If not a prefix of - * pathbuf, the callback will be invoked a single time on the - * original input path. - * @param fn Function to invoke on each path. The first arg is the - * input satte and the second arg is the pathbuf. The function - * should not modify the pathbuf. - * @param state Passed to fn as the first ath. - */ -extern int git_path_walk_up( - git_buf *pathbuf, - const char *ceiling, - int (*fn)(void *state, git_buf *), - void *state); - -/** - * Load all directory entries (except '.' and '..') into a vector. - * - * For cases where `git_path_direach()` is not appropriate, this - * allows you to load the filenames in a directory into a vector - * of strings. That vector can then be sorted, iterated, or whatever. - * Remember to free alloc of the allocated strings when you are done. - * - * @param path The directory to read from. - * @param prefix_len When inserting entries, the trailing part of path - * will be prefixed after this length. I.e. given path "/a/b" and - * prefix_len 3, the entries will look like "b/e1", "b/e2", etc. - * @param alloc_extra Extra bytes to add to each string allocation in - * case you want to append anything funny. - * @param contents Vector to fill with directory entry names. - */ -extern int git_path_dirload( - const char *path, - size_t prefix_len, - size_t alloc_extra, - git_vector *contents); - - -typedef struct { - struct stat st; - size_t path_len; - char path[GIT_FLEX_ARRAY]; -} git_path_with_stat; - -extern int git_path_with_stat_cmp(const void *a, const void *b); -extern int git_path_with_stat_cmp_icase(const void *a, const void *b); - -/** - * Load all directory entries along with stat info into a vector. - * - * This adds four things on top of plain `git_path_dirload`: - * - * 1. Each entry in the vector is a `git_path_with_stat` struct that - * contains both the path and the stat info - * 2. The entries will be sorted alphabetically - * 3. Entries that are directories will be suffixed with a '/' - * 4. Optionally, you can be a start and end prefix and only elements - * after the start and before the end (inclusively) will be stat'ed. - * - * @param path The directory to read from - * @param prefix_len The trailing part of path to prefix to entry paths - * @param ignore_case How to sort and compare paths with start/end limits - * @param start_stat As optimization, only stat values after this prefix - * @param end_stat As optimization, only stat values before this prefix - * @param contents Vector to fill with git_path_with_stat structures - */ -extern int git_path_dirload_with_stat( - const char *path, - size_t prefix_len, - bool ignore_case, - const char *start_stat, - const char *end_stat, - git_vector *contents); - -#endif diff --git a/src/pathspec.c b/src/pathspec.c deleted file mode 100644 index 2bde3ba5fee..00000000000 --- a/src/pathspec.c +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "pathspec.h" -#include "buf_text.h" -#include "attr_file.h" - -/* what is the common non-wildcard prefix for all items in the pathspec */ -char *git_pathspec_prefix(const git_strarray *pathspec) -{ - git_buf prefix = GIT_BUF_INIT; - const char *scan; - - if (!pathspec || !pathspec->count || - git_buf_text_common_prefix(&prefix, pathspec) < 0) - return NULL; - - /* diff prefix will only be leading non-wildcards */ - for (scan = prefix.ptr; *scan; ++scan) { - if (git__iswildcard(*scan) && - (scan == prefix.ptr || (*(scan - 1) != '\\'))) - break; - } - git_buf_truncate(&prefix, scan - prefix.ptr); - - if (prefix.size <= 0) { - git_buf_free(&prefix); - return NULL; - } - - git_buf_text_unescape(&prefix); - - return git_buf_detach(&prefix); -} - -/* is there anything in the spec that needs to be filtered on */ -bool git_pathspec_is_interesting(const git_strarray *pathspec) -{ - const char *str; - - if (pathspec == NULL || pathspec->count == 0) - return false; - if (pathspec->count > 1) - return true; - - str = pathspec->strings[0]; - if (!str || !str[0] || (!str[1] && (str[0] == '*' || str[0] == '.'))) - return false; - return true; -} - -/* build a vector of fnmatch patterns to evaluate efficiently */ -int git_pathspec_init( - git_vector *vspec, const git_strarray *strspec, git_pool *strpool) -{ - size_t i; - - memset(vspec, 0, sizeof(*vspec)); - - if (!git_pathspec_is_interesting(strspec)) - return 0; - - if (git_vector_init(vspec, strspec->count, NULL) < 0) - return -1; - - for (i = 0; i < strspec->count; ++i) { - int ret; - const char *pattern = strspec->strings[i]; - git_attr_fnmatch *match = git__calloc(1, sizeof(git_attr_fnmatch)); - if (!match) - return -1; - - match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE; - - ret = git_attr_fnmatch__parse(match, strpool, NULL, &pattern); - if (ret == GIT_ENOTFOUND) { - git__free(match); - continue; - } else if (ret < 0) - return ret; - - if (git_vector_insert(vspec, match) < 0) - return -1; - } - - return 0; -} - -/* free data from the pathspec vector */ -void git_pathspec_free(git_vector *vspec) -{ - git_attr_fnmatch *match; - unsigned int i; - - git_vector_foreach(vspec, i, match) { - git__free(match); - vspec->contents[i] = NULL; - } - - git_vector_free(vspec); -} - -/* match a path against the vectorized pathspec */ -bool git_pathspec_match_path( - git_vector *vspec, const char *path, bool disable_fnmatch, bool casefold) -{ - unsigned int i; - git_attr_fnmatch *match; - int fnmatch_flags = 0; - int (*use_strcmp)(const char *, const char *); - int (*use_strncmp)(const char *, const char *, size_t); - - if (!vspec || !vspec->length) - return true; - - if (disable_fnmatch) - fnmatch_flags = -1; - else if (casefold) - fnmatch_flags = FNM_CASEFOLD; - - if (casefold) { - use_strcmp = git__strcasecmp; - use_strncmp = git__strncasecmp; - } else { - use_strcmp = git__strcmp; - use_strncmp = git__strncmp; - } - - git_vector_foreach(vspec, i, match) { - int result = use_strcmp(match->pattern, path) ? FNM_NOMATCH : 0; - - if (fnmatch_flags >= 0 && result == FNM_NOMATCH) - result = p_fnmatch(match->pattern, path, fnmatch_flags); - - /* if we didn't match, look for exact dirname prefix match */ - if (result == FNM_NOMATCH && - (match->flags & GIT_ATTR_FNMATCH_HASWILD) == 0 && - use_strncmp(path, match->pattern, match->length) == 0 && - path[match->length] == '/') - result = 0; - - if (result == 0) - return (match->flags & GIT_ATTR_FNMATCH_NEGATIVE) ? false : true; - } - - return false; -} - diff --git a/src/pathspec.h b/src/pathspec.h deleted file mode 100644 index dde63c7d0fb..00000000000 --- a/src/pathspec.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_pathspec_h__ -#define INCLUDE_pathspec_h__ - -#include "common.h" -#include "buffer.h" -#include "vector.h" -#include "pool.h" - -/* what is the common non-wildcard prefix for all items in the pathspec */ -extern char *git_pathspec_prefix(const git_strarray *pathspec); - -/* is there anything in the spec that needs to be filtered on */ -extern bool git_pathspec_is_interesting(const git_strarray *pathspec); - -/* build a vector of fnmatch patterns to evaluate efficiently */ -extern int git_pathspec_init( - git_vector *vspec, const git_strarray *strspec, git_pool *strpool); - -/* free data from the pathspec vector */ -extern void git_pathspec_free(git_vector *vspec); - -/* match a path against the vectorized pathspec */ -extern bool git_pathspec_match_path( - git_vector *vspec, const char *path, bool disable_fnmatch, bool casefold); - -#endif diff --git a/src/pool.c b/src/pool.c deleted file mode 100644 index 64b5c6b0041..00000000000 --- a/src/pool.c +++ /dev/null @@ -1,301 +0,0 @@ -#include "pool.h" -#ifndef GIT_WIN32 -#include -#endif - -struct git_pool_page { - git_pool_page *next; - uint32_t size; - uint32_t avail; - char data[GIT_FLEX_ARRAY]; -}; - -#define GIT_POOL_MIN_USABLE 4 -#define GIT_POOL_MIN_PAGESZ 2 * sizeof(void*) - -static void *pool_alloc_page(git_pool *pool, uint32_t size); -static void pool_insert_page(git_pool *pool, git_pool_page *page); - -int git_pool_init( - git_pool *pool, uint32_t item_size, uint32_t items_per_page) -{ - assert(pool); - - if (!item_size) - item_size = 1; - /* round up item_size for decent object alignment */ - if (item_size > 4) - item_size = (item_size + 7) & ~7; - else if (item_size == 3) - item_size = 4; - - if (!items_per_page) - items_per_page = git_pool__suggest_items_per_page(item_size); - if (item_size * items_per_page < GIT_POOL_MIN_PAGESZ) - items_per_page = (GIT_POOL_MIN_PAGESZ + item_size - 1) / item_size; - - memset(pool, 0, sizeof(git_pool)); - pool->item_size = item_size; - pool->page_size = item_size * items_per_page; - - return 0; -} - -void git_pool_clear(git_pool *pool) -{ - git_pool_page *scan, *next; - - for (scan = pool->open; scan != NULL; scan = next) { - next = scan->next; - git__free(scan); - } - pool->open = NULL; - - for (scan = pool->full; scan != NULL; scan = next) { - next = scan->next; - git__free(scan); - } - pool->full = NULL; - - pool->free_list = NULL; - - pool->items = 0; - - pool->has_string_alloc = 0; - pool->has_multi_item_alloc = 0; - pool->has_large_page_alloc = 0; -} - -void git_pool_swap(git_pool *a, git_pool *b) -{ - git_pool temp; - - if (a == b) - return; - - memcpy(&temp, a, sizeof(temp)); - memcpy(a, b, sizeof(temp)); - memcpy(b, &temp, sizeof(temp)); -} - -static void pool_insert_page(git_pool *pool, git_pool_page *page) -{ - git_pool_page *scan; - - /* If there are no open pages or this page has the most open space, - * insert it at the beginning of the list. This is the common case. - */ - if (pool->open == NULL || pool->open->avail < page->avail) { - page->next = pool->open; - pool->open = page; - return; - } - - /* Otherwise insert into sorted position. */ - for (scan = pool->open; - scan->next && scan->next->avail > page->avail; - scan = scan->next); - page->next = scan->next; - scan->next = page; -} - -static void *pool_alloc_page(git_pool *pool, uint32_t size) -{ - git_pool_page *page; - uint32_t alloc_size; - - if (size <= pool->page_size) - alloc_size = pool->page_size; - else { - alloc_size = size; - pool->has_large_page_alloc = 1; - } - - page = git__calloc(1, alloc_size + sizeof(git_pool_page)); - if (!page) - return NULL; - - page->size = alloc_size; - page->avail = alloc_size - size; - - if (page->avail > 0) - pool_insert_page(pool, page); - else { - page->next = pool->full; - pool->full = page; - } - - pool->items++; - - return page->data; -} - -GIT_INLINE(void) pool_remove_page( - git_pool *pool, git_pool_page *page, git_pool_page *prev) -{ - if (prev == NULL) - pool->open = page->next; - else - prev->next = page->next; -} - -void *git_pool_malloc(git_pool *pool, uint32_t items) -{ - git_pool_page *scan = pool->open, *prev; - uint32_t size = items * pool->item_size; - void *ptr = NULL; - - pool->has_string_alloc = 0; - if (items > 1) - pool->has_multi_item_alloc = 1; - else if (pool->free_list != NULL) { - ptr = pool->free_list; - pool->free_list = *((void **)pool->free_list); - return ptr; - } - - /* just add a block if there is no open one to accomodate this */ - if (size >= pool->page_size || !scan || scan->avail < size) - return pool_alloc_page(pool, size); - - pool->items++; - - /* find smallest block in free list with space */ - for (scan = pool->open, prev = NULL; - scan->next && scan->next->avail >= size; - prev = scan, scan = scan->next); - - /* allocate space from the block */ - ptr = &scan->data[scan->size - scan->avail]; - scan->avail -= size; - - /* move to full list if there is almost no space left */ - if (scan->avail < pool->item_size || scan->avail < GIT_POOL_MIN_USABLE) { - pool_remove_page(pool, scan, prev); - scan->next = pool->full; - pool->full = scan; - } - /* reorder list if block is now smaller than the one after it */ - else if (scan->next != NULL && scan->next->avail > scan->avail) { - pool_remove_page(pool, scan, prev); - pool_insert_page(pool, scan); - } - - return ptr; -} - -char *git_pool_strndup(git_pool *pool, const char *str, size_t n) -{ - void *ptr = NULL; - - assert(pool && str && pool->item_size == sizeof(char)); - - if ((ptr = git_pool_malloc(pool, (uint32_t)(n + 1))) != NULL) { - memcpy(ptr, str, n); - *(((char *)ptr) + n) = '\0'; - } - pool->has_string_alloc = 1; - - return ptr; -} - -char *git_pool_strdup(git_pool *pool, const char *str) -{ - assert(pool && str && pool->item_size == sizeof(char)); - - return git_pool_strndup(pool, str, strlen(str)); -} - -char *git_pool_strdup_safe(git_pool *pool, const char *str) -{ - return str ? git_pool_strdup(pool, str) : NULL; -} - -char *git_pool_strcat(git_pool *pool, const char *a, const char *b) -{ - void *ptr; - size_t len_a, len_b; - - assert(pool && a && b && pool->item_size == sizeof(char)); - - len_a = a ? strlen(a) : 0; - len_b = b ? strlen(b) : 0; - - if ((ptr = git_pool_malloc(pool, (uint32_t)(len_a + len_b + 1))) != NULL) { - if (len_a) - memcpy(ptr, a, len_a); - if (len_b) - memcpy(((char *)ptr) + len_a, b, len_b); - *(((char *)ptr) + len_a + len_b) = '\0'; - } - pool->has_string_alloc = 1; - - return ptr; -} - -void git_pool_free(git_pool *pool, void *ptr) -{ - assert(pool && ptr && pool->item_size >= sizeof(void*)); - - *((void **)ptr) = pool->free_list; - pool->free_list = ptr; -} - -uint32_t git_pool__open_pages(git_pool *pool) -{ - uint32_t ct = 0; - git_pool_page *scan; - for (scan = pool->open; scan != NULL; scan = scan->next) ct++; - return ct; -} - -uint32_t git_pool__full_pages(git_pool *pool) -{ - uint32_t ct = 0; - git_pool_page *scan; - for (scan = pool->full; scan != NULL; scan = scan->next) ct++; - return ct; -} - -bool git_pool__ptr_in_pool(git_pool *pool, void *ptr) -{ - git_pool_page *scan; - for (scan = pool->open; scan != NULL; scan = scan->next) - if ((void *)scan->data <= ptr && - (void *)(((char *)scan->data) + scan->size) > ptr) - return true; - for (scan = pool->full; scan != NULL; scan = scan->next) - if ((void *)scan->data <= ptr && - (void *)(((char *)scan->data) + scan->size) > ptr) - return true; - return false; -} - -uint32_t git_pool__system_page_size(void) -{ - static uint32_t size = 0; - - if (!size) { -#ifdef GIT_WIN32 - SYSTEM_INFO info; - GetSystemInfo(&info); - size = (uint32_t)info.dwPageSize; -#elif defined(__amigaos4__) - size = (uint32_t)4096; /* 4K as there is no global value we can query */ -#else - size = (uint32_t)sysconf(_SC_PAGE_SIZE); -#endif - - size -= 2 * sizeof(void *); /* allow space for malloc overhead */ - } - - return size; -} - -uint32_t git_pool__suggest_items_per_page(uint32_t item_size) -{ - uint32_t page_bytes = - git_pool__system_page_size() - sizeof(git_pool_page); - return page_bytes / item_size; -} - diff --git a/src/pool.h b/src/pool.h deleted file mode 100644 index 2b262a5884d..00000000000 --- a/src/pool.h +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_pool_h__ -#define INCLUDE_pool_h__ - -#include "common.h" - -typedef struct git_pool_page git_pool_page; - -/** - * Chunked allocator. - * - * A `git_pool` can be used when you want to cheaply allocate - * multiple items of the same type and are willing to free them - * all together with a single call. The two most common cases - * are a set of fixed size items (such as lots of OIDs) or a - * bunch of strings. - * - * Internally, a `git_pool` allocates pages of memory and then - * deals out blocks from the trailing unused portion of each page. - * The pages guarantee that the number of actual allocations done - * will be much smaller than the number of items needed. - * - * For examples of how to set up a `git_pool` see `git_pool_init`. - */ -typedef struct { - git_pool_page *open; /* pages with space left */ - git_pool_page *full; /* pages with no space left */ - void *free_list; /* optional: list of freed blocks */ - uint32_t item_size; /* size of single alloc unit in bytes */ - uint32_t page_size; /* size of page in bytes */ - uint32_t items; - unsigned has_string_alloc : 1; /* was the strdup function used */ - unsigned has_multi_item_alloc : 1; /* was items ever > 1 in malloc */ - unsigned has_large_page_alloc : 1; /* are any pages > page_size */ -} git_pool; - -#define GIT_POOL_INIT_STRINGPOOL { 0, 0, 0, 1, 4000, 0, 0, 0, 0 } - -/** - * Initialize a pool. - * - * To allocation strings, use like this: - * - * git_pool_init(&string_pool, 1, 0); - * my_string = git_pool_strdup(&string_pool, your_string); - * - * To allocate items of fixed size, use like this: - * - * git_pool_init(&pool, sizeof(item), 0); - * my_item = git_pool_malloc(&pool, 1); - * - * Of course, you can use this in other ways, but those are the - * two most common patterns. - */ -extern int git_pool_init( - git_pool *pool, uint32_t item_size, uint32_t items_per_page); - -/** - * Free all items in pool - */ -extern void git_pool_clear(git_pool *pool); - -/** - * Swap two pools with one another - */ -extern void git_pool_swap(git_pool *a, git_pool *b); - -/** - * Allocate space for one or more items from a pool. - */ -extern void *git_pool_malloc(git_pool *pool, uint32_t items); - -/** - * Allocate space and zero it out. - */ -GIT_INLINE(void *) git_pool_mallocz(git_pool *pool, uint32_t items) -{ - void *ptr = git_pool_malloc(pool, items); - if (ptr) - memset(ptr, 0, (size_t)items * (size_t)pool->item_size); - return ptr; -} - -/** - * Allocate space and duplicate string data into it. - * - * This is allowed only for pools with item_size == sizeof(char) - */ -extern char *git_pool_strndup(git_pool *pool, const char *str, size_t n); - -/** - * Allocate space and duplicate a string into it. - * - * This is allowed only for pools with item_size == sizeof(char) - */ -extern char *git_pool_strdup(git_pool *pool, const char *str); - -/** - * Allocate space and duplicate a string into it, NULL is no error. - * - * This is allowed only for pools with item_size == sizeof(char) - */ -extern char *git_pool_strdup_safe(git_pool *pool, const char *str); - -/** - * Allocate space for the concatenation of two strings. - * - * This is allowed only for pools with item_size == sizeof(char) - */ -extern char *git_pool_strcat(git_pool *pool, const char *a, const char *b); - -/** - * Push a block back onto the free list for the pool. - * - * This is allowed only if the item_size is >= sizeof(void*). - * - * In some cases, it is helpful to "release" an allocated block - * for reuse. Pools don't support a general purpose free, but - * they will keep a simple free blocks linked list provided the - * native block size is large enough to hold a void pointer - */ -extern void git_pool_free(git_pool *pool, void *ptr); - -/* - * Misc utilities - */ - -extern uint32_t git_pool__open_pages(git_pool *pool); - -extern uint32_t git_pool__full_pages(git_pool *pool); - -extern bool git_pool__ptr_in_pool(git_pool *pool, void *ptr); - -extern uint32_t git_pool__system_page_size(void); - -extern uint32_t git_pool__suggest_items_per_page(uint32_t item_size); - -#endif diff --git a/src/posix.c b/src/posix.c deleted file mode 100644 index 95cd28edc57..00000000000 --- a/src/posix.c +++ /dev/null @@ -1,209 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "common.h" -#include "posix.h" -#include "path.h" -#include -#include - -#ifndef GIT_WIN32 - -#ifdef NO_ADDRINFO -int p_getaddrinfo( - const char *host, - const char *port, - struct addrinfo *hints, - struct addrinfo **info) -{ - GIT_UNUSED(hints); - - struct addrinfo *ainfo, *ai; - int p = 0; - - if ((ainfo = malloc(sizeof(struct addrinfo))) == NULL) - return -1; - - if ((ainfo->ai_hostent = gethostbyname(host)) == NULL) - return -2; - - ainfo->ai_servent = getservbyname(port, 0); - - if (ainfo->ai_servent) - ainfo->ai_port = ainfo->ai_servent->s_port; - else - ainfo->ai_port = atol(port); - - memcpy(&ainfo->ai_addr_in.sin_addr, - ainfo->ai_hostent->h_addr_list[0], - ainfo->ai_hostent->h_length); - - ainfo->ai_protocol = 0; - ainfo->ai_socktype = hints->ai_socktype; - ainfo->ai_family = ainfo->ai_hostent->h_addrtype; - ainfo->ai_addr_in.sin_family = ainfo->ai_family; - ainfo->ai_addr_in.sin_port = ainfo->ai_port; - ainfo->ai_addr = (struct addrinfo *)&ainfo->ai_addr_in; - ainfo->ai_addrlen = sizeof(struct sockaddr_in); - - *info = ainfo; - - if (ainfo->ai_hostent->h_addr_list[1] == NULL) { - ainfo->ai_next = NULL; - return 0; - } - - ai = ainfo; - - for (p = 1; ainfo->ai_hostent->h_addr_list[p] != NULL; p++) { - ai->ai_next = malloc(sizeof(struct addrinfo)); - memcpy(&ai->ai_next, ainfo, sizeof(struct addrinfo)); - memcpy(&ai->ai_next->ai_addr_in.sin_addr, - ainfo->ai_hostent->h_addr_list[p], - ainfo->ai_hostent->h_length); - ai->ai_next->ai_addr = (struct addrinfo *)&ai->ai_next->ai_addr_in; - ai = ai->ai_next; - } - - ai->ai_next = NULL; - return 0; -} - -void p_freeaddrinfo(struct addrinfo *info) -{ - struct addrinfo *p, *next; - - p = info; - - while(p != NULL) { - next = p->ai_next; - free(p); - p = next; - } -} - -const char *p_gai_strerror(int ret) -{ - switch(ret) { - case -1: - return "Out of memory"; - break; - - case -2: - return "Address lookup failed"; - break; - - default: - return "Unknown error"; - break; - } -} -#endif /* NO_ADDRINFO */ - -int p_open(const char *path, int flags, ...) -{ - mode_t mode = 0; - - if (flags & O_CREAT) - { - va_list arg_list; - - va_start(arg_list, flags); - mode = (mode_t)va_arg(arg_list, int); - va_end(arg_list); - } - - return open(path, flags | O_BINARY, mode); -} - -int p_creat(const char *path, mode_t mode) -{ - return open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode); -} - -int p_getcwd(char *buffer_out, size_t size) -{ - char *cwd_buffer; - - assert(buffer_out && size > 0); - - cwd_buffer = getcwd(buffer_out, size); - - if (cwd_buffer == NULL) - return -1; - - git_path_mkposix(buffer_out); - git_path_string_to_dir(buffer_out, size); /* append trailing slash */ - - return 0; -} - -int p_rename(const char *from, const char *to) -{ - if (!link(from, to)) { - p_unlink(from); - return 0; - } - - if (!rename(from, to)) - return 0; - - return -1; -} - -#endif /* GIT_WIN32 */ - -int p_read(git_file fd, void *buf, size_t cnt) -{ - char *b = buf; - while (cnt) { - ssize_t r; -#ifdef GIT_WIN32 - assert((size_t)((unsigned int)cnt) == cnt); - r = read(fd, b, (unsigned int)cnt); -#else - r = read(fd, b, cnt); -#endif - if (r < 0) { - if (errno == EINTR || errno == EAGAIN) - continue; - return -1; - } - if (!r) - break; - cnt -= r; - b += r; - } - return (int)(b - (char *)buf); -} - -int p_write(git_file fd, const void *buf, size_t cnt) -{ - const char *b = buf; - while (cnt) { - ssize_t r; -#ifdef GIT_WIN32 - assert((size_t)((unsigned int)cnt) == cnt); - r = write(fd, b, (unsigned int)cnt); -#else - r = write(fd, b, cnt); -#endif - if (r < 0) { - if (errno == EINTR || errno == EAGAIN) - continue; - return -1; - } - if (!r) { - errno = EPIPE; - return -1; - } - cnt -= r; - b += r; - } - return 0; -} - - diff --git a/src/posix.h b/src/posix.h deleted file mode 100644 index 9dd0c94d3cd..00000000000 --- a/src/posix.h +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_posix_h__ -#define INCLUDE_posix_h__ - -#include "common.h" -#include -#include -#include "fnmatch.h" - -#ifndef S_IFGITLINK -#define S_IFGITLINK 0160000 -#define S_ISGITLINK(m) (((m) & S_IFMT) == S_IFGITLINK) -#endif - -/* if S_ISGID is not defined, then don't try to set it */ -#ifndef S_ISGID -#define S_ISGID 0 -#endif - -#if !defined(O_BINARY) -#define O_BINARY 0 -#endif - -typedef int git_file; - -/** - * Standard POSIX Methods - * - * All the methods starting with the `p_` prefix are - * direct ports of the standard POSIX methods. - * - * Some of the methods are slightly wrapped to provide - * saner defaults. Some of these methods are emulated - * in Windows platforns. - * - * Use your manpages to check the docs on these. - * Straightforward - */ - -extern int p_read(git_file fd, void *buf, size_t cnt); -extern int p_write(git_file fd, const void *buf, size_t cnt); - -#define p_fstat(f,b) fstat(f, b) -#define p_lseek(f,n,w) lseek(f, n, w) -#define p_close(fd) close(fd) -#define p_umask(m) umask(m) - -extern int p_open(const char *path, int flags, ...); -extern int p_creat(const char *path, mode_t mode); -extern int p_getcwd(char *buffer_out, size_t size); -extern int p_rename(const char *from, const char *to); - -#ifndef GIT_WIN32 - -#define p_stat(p,b) stat(p, b) -#define p_chdir(p) chdir(p) -#define p_rmdir(p) rmdir(p) -#define p_chmod(p,m) chmod(p, m) -#define p_access(p,m) access(p,m) -#define p_recv(s,b,l,f) recv(s,b,l,f) -#define p_send(s,b,l,f) send(s,b,l,f) -typedef int GIT_SOCKET; -#define INVALID_SOCKET -1 - -#define p_localtime_r localtime_r -#define p_gmtime_r gmtime_r -#define p_gettimeofday gettimeofday - -#else - -typedef SOCKET GIT_SOCKET; -struct timezone; -extern struct tm * p_localtime_r (const time_t *timer, struct tm *result); -extern struct tm * p_gmtime_r (const time_t *timer, struct tm *result); -extern int p_gettimeofday(struct timeval *tv, struct timezone *tz); - - -#endif - -/** - * Platform-dependent methods - */ -#ifdef GIT_WIN32 -# include "win32/posix.h" -#else -# include "unix/posix.h" -#endif - -#ifdef NO_READDIR_R -# include -GIT_INLINE(int) p_readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) -{ - GIT_UNUSED(entry); - *result = readdir(dirp); - return 0; -} -#else /* NO_READDIR_R */ -# define p_readdir_r(d,e,r) readdir_r(d,e,r) -#endif - -#ifdef NO_ADDRINFO -# include -struct addrinfo { - struct hostent *ai_hostent; - struct servent *ai_servent; - struct sockaddr_in ai_addr_in; - struct sockaddr *ai_addr; - size_t ai_addrlen; - int ai_family; - int ai_socktype; - int ai_protocol; - long ai_port; - struct addrinfo *ai_next; -}; - -extern int p_getaddrinfo(const char *host, const char *port, - struct addrinfo *hints, struct addrinfo **info); -extern void p_freeaddrinfo(struct addrinfo *info); -extern const char *p_gai_strerror(int ret); -#else -# define p_getaddrinfo(a, b, c, d) getaddrinfo(a, b, c, d) -# define p_freeaddrinfo(a) freeaddrinfo(a) -# define p_gai_strerror(c) gai_strerror(c) -#endif /* NO_ADDRINFO */ - -#endif diff --git a/src/pqueue.c b/src/pqueue.c deleted file mode 100644 index 7819ed41e07..00000000000 --- a/src/pqueue.c +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - * - * This file is based on a modified version of the priority queue found - * in the Apache project and libpqueue library. - * - * https://github.com/vy/libpqueue - * - * Original file notice: - * - * Copyright 2010 Volkan Yazici - * Copyright 2006-2010 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -#include "common.h" -#include "pqueue.h" - -#define left(i) ((i) << 1) -#define right(i) (((i) << 1) + 1) -#define parent(i) ((i) >> 1) - -int git_pqueue_init(git_pqueue *q, size_t n, git_pqueue_cmp cmppri) -{ - assert(q); - - /* Need to allocate n+1 elements since element 0 isn't used. */ - q->d = git__malloc((n + 1) * sizeof(void *)); - GITERR_CHECK_ALLOC(q->d); - - q->size = 1; - q->avail = q->step = (n + 1); /* see comment above about n+1 */ - q->cmppri = cmppri; - - return 0; -} - - -void git_pqueue_free(git_pqueue *q) -{ - git__free(q->d); - q->d = NULL; -} - -void git_pqueue_clear(git_pqueue *q) -{ - q->size = 1; -} - -size_t git_pqueue_size(git_pqueue *q) -{ - /* queue element 0 exists but doesn't count since it isn't used. */ - return (q->size - 1); -} - - -static void bubble_up(git_pqueue *q, size_t i) -{ - size_t parent_node; - void *moving_node = q->d[i]; - - for (parent_node = parent(i); - ((i > 1) && q->cmppri(q->d[parent_node], moving_node)); - i = parent_node, parent_node = parent(i)) { - q->d[i] = q->d[parent_node]; - } - - q->d[i] = moving_node; -} - - -static size_t maxchild(git_pqueue *q, size_t i) -{ - size_t child_node = left(i); - - if (child_node >= q->size) - return 0; - - if ((child_node + 1) < q->size && - q->cmppri(q->d[child_node], q->d[child_node + 1])) - child_node++; /* use right child instead of left */ - - return child_node; -} - - -static void percolate_down(git_pqueue *q, size_t i) -{ - size_t child_node; - void *moving_node = q->d[i]; - - while ((child_node = maxchild(q, i)) != 0 && - q->cmppri(moving_node, q->d[child_node])) { - q->d[i] = q->d[child_node]; - i = child_node; - } - - q->d[i] = moving_node; -} - - -int git_pqueue_insert(git_pqueue *q, void *d) -{ - void *tmp; - size_t i; - size_t newsize; - - if (!q) return 1; - - /* allocate more memory if necessary */ - if (q->size >= q->avail) { - newsize = q->size + q->step; - tmp = git__realloc(q->d, sizeof(void *) * newsize); - GITERR_CHECK_ALLOC(tmp); - - q->d = tmp; - q->avail = newsize; - } - - /* insert item */ - i = q->size++; - q->d[i] = d; - bubble_up(q, i); - - return 0; -} - - -void *git_pqueue_pop(git_pqueue *q) -{ - void *head; - - if (!q || q->size == 1) - return NULL; - - head = q->d[1]; - q->d[1] = q->d[--q->size]; - percolate_down(q, 1); - - return head; -} - - -void *git_pqueue_peek(git_pqueue *q) -{ - if (!q || q->size == 1) - return NULL; - return q->d[1]; -} diff --git a/src/pqueue.h b/src/pqueue.h deleted file mode 100644 index ed71392854e..00000000000 --- a/src/pqueue.h +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - * - * This file is based on a modified version of the priority queue found - * in the Apache project and libpqueue library. - * - * https://github.com/vy/libpqueue - * - * Original file notice: - * - * Copyright 2010 Volkan Yazici - * Copyright 2006-2010 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -#ifndef INCLUDE_pqueue_h__ -#define INCLUDE_pqueue_h__ - -/** callback functions to get/set/compare the priority of an element */ -typedef int (*git_pqueue_cmp)(void *a, void *b); - -/** the priority queue handle */ -typedef struct { - size_t size, avail, step; - git_pqueue_cmp cmppri; - void **d; -} git_pqueue; - - -/** - * initialize the queue - * - * @param n the initial estimate of the number of queue items for which memory - * should be preallocated - * @param cmppri the callback function to compare two nodes of the queue - * - * @Return the handle or NULL for insufficent memory - */ -int git_pqueue_init(git_pqueue *q, size_t n, git_pqueue_cmp cmppri); - - -/** - * free all memory used by the queue - * @param q the queue - */ -void git_pqueue_free(git_pqueue *q); - -/** - * clear all the elements in the queue - * @param q the queue - */ -void git_pqueue_clear(git_pqueue *q); - -/** - * return the size of the queue. - * @param q the queue - */ -size_t git_pqueue_size(git_pqueue *q); - - -/** - * insert an item into the queue. - * @param q the queue - * @param d the item - * @return 0 on success - */ -int git_pqueue_insert(git_pqueue *q, void *d); - - -/** - * pop the highest-ranking item from the queue. - * @param p the queue - * @param d where to copy the entry to - * @return NULL on error, otherwise the entry - */ -void *git_pqueue_pop(git_pqueue *q); - - -/** - * access highest-ranking item without removing it. - * @param q the queue - * @param d the entry - * @return NULL on error, otherwise the entry - */ -void *git_pqueue_peek(git_pqueue *q); - -#endif /* PQUEUE_H */ -/** @} */ - diff --git a/src/push.c b/src/push.c deleted file mode 100644 index ddfe5ec079f..00000000000 --- a/src/push.c +++ /dev/null @@ -1,518 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "git2.h" - -#include "common.h" -#include "pack.h" -#include "pack-objects.h" -#include "remote.h" -#include "vector.h" -#include "push.h" - -int git_push_new(git_push **out, git_remote *remote) -{ - git_push *p; - - *out = NULL; - - p = git__calloc(1, sizeof(*p)); - GITERR_CHECK_ALLOC(p); - - p->repo = remote->repo; - p->remote = remote; - p->report_status = 1; - - if (git_vector_init(&p->specs, 0, NULL) < 0) { - git__free(p); - return -1; - } - - if (git_vector_init(&p->status, 0, NULL) < 0) { - git_vector_free(&p->specs); - git__free(p); - return -1; - } - - *out = p; - return 0; -} - -static void free_refspec(push_spec *spec) -{ - if (spec == NULL) - return; - - if (spec->lref) - git__free(spec->lref); - - if (spec->rref) - git__free(spec->rref); - - git__free(spec); -} - -static void free_status(push_status *status) -{ - if (status == NULL) - return; - - if (status->msg) - git__free(status->msg); - - git__free(status->ref); - git__free(status); -} - -static int check_rref(char *ref) -{ - if (git__prefixcmp(ref, "refs/")) { - giterr_set(GITERR_INVALID, "Not a valid reference '%s'", ref); - return -1; - } - - return 0; -} - -static int check_lref(git_push *push, char *ref) -{ - /* lref must be resolvable to an existing object */ - git_object *obj; - int error = git_revparse_single(&obj, push->repo, ref); - - if (error) { - if (error == GIT_ENOTFOUND) - giterr_set(GITERR_REFERENCE, - "src refspec '%s' does not match any existing object", ref); - else - giterr_set(GITERR_INVALID, "Not a valid reference '%s'", ref); - - return -1; - } else - git_object_free(obj); - - return 0; -} - -static int parse_refspec(git_push *push, push_spec **spec, const char *str) -{ - push_spec *s; - char *delim; - - *spec = NULL; - - s = git__calloc(1, sizeof(*s)); - GITERR_CHECK_ALLOC(s); - - if (str[0] == '+') { - s->force = true; - str++; - } - - delim = strchr(str, ':'); - if (delim == NULL) { - s->lref = git__strdup(str); - if (!s->lref || check_lref(push, s->lref) < 0) - goto on_error; - } else { - if (delim - str) { - s->lref = git__strndup(str, delim - str); - if (!s->lref || check_lref(push, s->lref) < 0) - goto on_error; - } - - if (strlen(delim + 1)) { - s->rref = git__strdup(delim + 1); - if (!s->rref || check_rref(s->rref) < 0) - goto on_error; - } - } - - if (!s->lref && !s->rref) - goto on_error; - - /* If rref is ommitted, use the same ref name as lref */ - if (!s->rref) { - s->rref = git__strdup(s->lref); - if (!s->rref || check_rref(s->rref) < 0) - goto on_error; - } - - *spec = s; - return 0; - -on_error: - free_refspec(s); - return -1; -} - -int git_push_add_refspec(git_push *push, const char *refspec) -{ - push_spec *spec; - - if (parse_refspec(push, &spec, refspec) < 0 || - git_vector_insert(&push->specs, spec) < 0) - return -1; - - return 0; -} - -int git_push_update_tips(git_push *push) -{ - git_refspec *fetch_spec = &push->remote->fetch; - git_buf remote_ref_name = GIT_BUF_INIT; - size_t i, j; - push_spec *push_spec; - git_reference *remote_ref; - push_status *status; - int error = 0; - - git_vector_foreach(&push->status, i, status) { - /* If this ref update was successful (ok, not ng), it will have an empty message */ - if (status->msg) - continue; - - /* Find the corresponding remote ref */ - if (!git_refspec_src_matches(fetch_spec, status->ref)) - continue; - - if ((error = git_refspec_transform_r(&remote_ref_name, fetch_spec, status->ref)) < 0) - goto on_error; - - /* Find matching push ref spec */ - git_vector_foreach(&push->specs, j, push_spec) { - if (!strcmp(push_spec->rref, status->ref)) - break; - } - - /* Could not find the corresponding push ref spec for this push update */ - if (j == push->specs.length) - continue; - - /* Update the remote ref */ - if (git_oid_iszero(&push_spec->loid)) { - error = git_reference_lookup(&remote_ref, push->remote->repo, git_buf_cstr(&remote_ref_name)); - - if (!error) { - if ((error = git_reference_delete(remote_ref)) < 0) - goto on_error; - } else if (error == GIT_ENOTFOUND) - giterr_clear(); - else - goto on_error; - } else if ((error = git_reference_create(NULL, push->remote->repo, git_buf_cstr(&remote_ref_name), &push_spec->loid, 1)) < 0) - goto on_error; - } - - error = 0; - -on_error: - git_buf_free(&remote_ref_name); - return error; -} - -static int revwalk(git_vector *commits, git_push *push) -{ - git_remote_head *head; - push_spec *spec; - git_revwalk *rw; - git_oid oid; - unsigned int i; - int error = -1; - - if (git_revwalk_new(&rw, push->repo) < 0) - return -1; - - git_revwalk_sorting(rw, GIT_SORT_TIME); - - git_vector_foreach(&push->specs, i, spec) { - git_otype type; - size_t size; - - if (git_oid_iszero(&spec->loid)) - /* - * Delete reference on remote side; - * nothing to do here. - */ - continue; - - if (git_oid_equal(&spec->loid, &spec->roid)) - continue; /* up-to-date */ - - if (git_odb_read_header(&size, &type, push->repo->_odb, &spec->loid) < 0) - goto on_error; - - if (type == GIT_OBJ_TAG) { - git_tag *tag; - git_object *target; - - if (git_packbuilder_insert(push->pb, &spec->loid, NULL) < 0) - goto on_error; - - if (git_tag_lookup(&tag, push->repo, &spec->loid) < 0) - goto on_error; - - if (git_tag_peel(&target, tag) < 0) { - git_tag_free(tag); - goto on_error; - } - git_tag_free(tag); - - if (git_object_type(target) == GIT_OBJ_COMMIT) { - if (git_revwalk_push(rw, git_object_id(target)) < 0) { - git_object_free(target); - goto on_error; - } - } else { - if (git_packbuilder_insert( - push->pb, git_object_id(target), NULL) < 0) { - git_object_free(target); - goto on_error; - } - } - git_object_free(target); - } else if (git_revwalk_push(rw, &spec->loid) < 0) - goto on_error; - - if (!spec->force) { - git_oid base; - - if (git_oid_iszero(&spec->roid)) - continue; - - if (!git_odb_exists(push->repo->_odb, &spec->roid)) { - giterr_clear(); - error = GIT_ENONFASTFORWARD; - goto on_error; - } - - error = git_merge_base(&base, push->repo, - &spec->loid, &spec->roid); - - if (error == GIT_ENOTFOUND || - (!error && !git_oid_equal(&base, &spec->roid))) { - giterr_clear(); - error = GIT_ENONFASTFORWARD; - goto on_error; - } - - if (error < 0) - goto on_error; - } - } - - git_vector_foreach(&push->remote->refs, i, head) { - if (git_oid_iszero(&head->oid)) - continue; - - /* TODO */ - git_revwalk_hide(rw, &head->oid); - } - - while ((error = git_revwalk_next(&oid, rw)) == 0) { - git_oid *o = git__malloc(GIT_OID_RAWSZ); - GITERR_CHECK_ALLOC(o); - git_oid_cpy(o, &oid); - if (git_vector_insert(commits, o) < 0) { - error = -1; - goto on_error; - } - } - -on_error: - git_revwalk_free(rw); - return error == GIT_ITEROVER ? 0 : error; -} - -static int queue_objects(git_push *push) -{ - git_vector commits; - git_oid *o; - unsigned int i; - int error; - - if (git_vector_init(&commits, 0, NULL) < 0) - return -1; - - if ((error = revwalk(&commits, push)) < 0) - goto on_error; - - if (!commits.length) { - git_vector_free(&commits); - return 0; /* nothing to do */ - } - - git_vector_foreach(&commits, i, o) { - if ((error = git_packbuilder_insert(push->pb, o, NULL)) < 0) - goto on_error; - } - - git_vector_foreach(&commits, i, o) { - git_object *obj; - - if ((error = git_object_lookup(&obj, push->repo, o, GIT_OBJ_ANY)) < 0) - goto on_error; - - switch (git_object_type(obj)) { - case GIT_OBJ_TAG: /* TODO: expect tags */ - case GIT_OBJ_COMMIT: - if ((error = git_packbuilder_insert_tree(push->pb, - git_commit_tree_id((git_commit *)obj))) < 0) { - git_object_free(obj); - goto on_error; - } - break; - case GIT_OBJ_TREE: - case GIT_OBJ_BLOB: - default: - git_object_free(obj); - giterr_set(GITERR_INVALID, "Given object type invalid"); - error = -1; - goto on_error; - } - git_object_free(obj); - } - error = 0; - -on_error: - git_vector_foreach(&commits, i, o) { - git__free(o); - } - git_vector_free(&commits); - return error; -} - -static int calculate_work(git_push *push) -{ - git_remote_head *head; - push_spec *spec; - unsigned int i, j; - - /* Update local and remote oids*/ - - git_vector_foreach(&push->specs, i, spec) { - if (spec->lref) { - /* This is a create or update. Local ref must exist. */ - if (git_reference_name_to_id( - &spec->loid, push->repo, spec->lref) < 0) { - giterr_set(GIT_ENOTFOUND, "No such reference '%s'", spec->lref); - return -1; - } - } - - if (spec->rref) { - /* Remote ref may or may not (e.g. during create) already exist. */ - git_vector_foreach(&push->remote->refs, j, head) { - if (!strcmp(spec->rref, head->name)) { - git_oid_cpy(&spec->roid, &head->oid); - break; - } - } - } - } - - return 0; -} - -static int do_push(git_push *push) -{ - int error; - git_transport *transport = push->remote->transport; - - if (!transport->push) { - giterr_set(GITERR_NET, "Remote transport doesn't support push"); - error = -1; - goto on_error; - } - - /* - * A pack-file MUST be sent if either create or update command - * is used, even if the server already has all the necessary - * objects. In this case the client MUST send an empty pack-file. - */ - - if ((error = git_packbuilder_new(&push->pb, push->repo)) < 0 || - (error = calculate_work(push)) < 0 || - (error = queue_objects(push)) < 0 || - (error = transport->push(transport, push)) < 0) - goto on_error; - - error = 0; - -on_error: - git_packbuilder_free(push->pb); - return error; -} - -static int cb_filter_refs(git_remote_head *ref, void *data) -{ - git_remote *remote = (git_remote *) data; - return git_vector_insert(&remote->refs, ref); -} - -static int filter_refs(git_remote *remote) -{ - git_vector_clear(&remote->refs); - return git_remote_ls(remote, cb_filter_refs, remote); -} - -int git_push_finish(git_push *push) -{ - int error; - - if (!git_remote_connected(push->remote) && - (error = git_remote_connect(push->remote, GIT_DIRECTION_PUSH)) < 0) - return error; - - if ((error = filter_refs(push->remote)) < 0 || - (error = do_push(push)) < 0) - return error; - - return 0; -} - -int git_push_unpack_ok(git_push *push) -{ - return push->unpack_ok; -} - -int git_push_status_foreach(git_push *push, - int (*cb)(const char *ref, const char *msg, void *data), - void *data) -{ - push_status *status; - unsigned int i; - - git_vector_foreach(&push->status, i, status) { - if (cb(status->ref, status->msg, data) < 0) - return GIT_EUSER; - } - - return 0; -} - -void git_push_free(git_push *push) -{ - push_spec *spec; - push_status *status; - unsigned int i; - - if (push == NULL) - return; - - git_vector_foreach(&push->specs, i, spec) { - free_refspec(spec); - } - git_vector_free(&push->specs); - - git_vector_foreach(&push->status, i, status) { - free_status(status); - } - git_vector_free(&push->status); - - git__free(push); -} diff --git a/src/push.h b/src/push.h deleted file mode 100644 index 0ac8ef94708..00000000000 --- a/src/push.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_push_h__ -#define INCLUDE_push_h__ - -#include "git2.h" - -typedef struct push_spec { - char *lref; - char *rref; - - git_oid loid; - git_oid roid; - - bool force; -} push_spec; - -typedef struct push_status { - bool ok; - - char *ref; - char *msg; -} push_status; - -struct git_push { - git_repository *repo; - git_packbuilder *pb; - git_remote *remote; - git_vector specs; - bool report_status; - - /* report-status */ - bool unpack_ok; - git_vector status; -}; - -#endif diff --git a/src/reflog.c b/src/reflog.c deleted file mode 100644 index 432680b9919..00000000000 --- a/src/reflog.c +++ /dev/null @@ -1,521 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "reflog.h" -#include "repository.h" -#include "filebuf.h" -#include "signature.h" - -static int reflog_init(git_reflog **reflog, const git_reference *ref) -{ - git_reflog *log; - - *reflog = NULL; - - log = git__calloc(1, sizeof(git_reflog)); - GITERR_CHECK_ALLOC(log); - - log->ref_name = git__strdup(ref->name); - GITERR_CHECK_ALLOC(log->ref_name); - - if (git_vector_init(&log->entries, 0, NULL) < 0) { - git__free(log->ref_name); - git__free(log); - return -1; - } - - log->owner = git_reference_owner(ref); - *reflog = log; - - return 0; -} - -static int serialize_reflog_entry( - git_buf *buf, - const git_oid *oid_old, - const git_oid *oid_new, - const git_signature *committer, - const char *msg) -{ - char raw_old[GIT_OID_HEXSZ+1]; - char raw_new[GIT_OID_HEXSZ+1]; - - git_oid_tostr(raw_old, GIT_OID_HEXSZ+1, oid_old); - git_oid_tostr(raw_new, GIT_OID_HEXSZ+1, oid_new); - - git_buf_clear(buf); - - git_buf_puts(buf, raw_old); - git_buf_putc(buf, ' '); - git_buf_puts(buf, raw_new); - - git_signature__writebuf(buf, " ", committer); - - /* drop trailing LF */ - git_buf_rtrim(buf); - - if (msg) { - git_buf_putc(buf, '\t'); - git_buf_puts(buf, msg); - } - - git_buf_putc(buf, '\n'); - - return git_buf_oom(buf); -} - -static int reflog_entry_new(git_reflog_entry **entry) -{ - git_reflog_entry *e; - - assert(entry); - - e = git__malloc(sizeof(git_reflog_entry)); - GITERR_CHECK_ALLOC(e); - - memset(e, 0, sizeof(git_reflog_entry)); - - *entry = e; - - return 0; -} - -static void reflog_entry_free(git_reflog_entry *entry) -{ - git_signature_free(entry->committer); - - git__free(entry->msg); - git__free(entry); -} - -static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size) -{ - const char *ptr; - git_reflog_entry *entry; - -#define seek_forward(_increase) do { \ - if (_increase >= buf_size) { \ - giterr_set(GITERR_INVALID, "Ran out of data while parsing reflog"); \ - goto fail; \ - } \ - buf += _increase; \ - buf_size -= _increase; \ - } while (0) - - while (buf_size > GIT_REFLOG_SIZE_MIN) { - if (reflog_entry_new(&entry) < 0) - return -1; - - entry->committer = git__malloc(sizeof(git_signature)); - GITERR_CHECK_ALLOC(entry->committer); - - if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < 0) - goto fail; - seek_forward(GIT_OID_HEXSZ + 1); - - if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < 0) - goto fail; - seek_forward(GIT_OID_HEXSZ + 1); - - ptr = buf; - - /* Seek forward to the end of the signature. */ - while (*buf && *buf != '\t' && *buf != '\n') - seek_forward(1); - - if (git_signature__parse(entry->committer, &ptr, buf + 1, NULL, *buf) < 0) - goto fail; - - if (*buf == '\t') { - /* We got a message. Read everything till we reach LF. */ - seek_forward(1); - ptr = buf; - - while (*buf && *buf != '\n') - seek_forward(1); - - entry->msg = git__strndup(ptr, buf - ptr); - GITERR_CHECK_ALLOC(entry->msg); - } else - entry->msg = NULL; - - while (*buf && *buf == '\n' && buf_size > 1) - seek_forward(1); - - if (git_vector_insert(&log->entries, entry) < 0) - goto fail; - } - - return 0; - -#undef seek_forward - -fail: - if (entry) - reflog_entry_free(entry); - - return -1; -} - -void git_reflog_free(git_reflog *reflog) -{ - unsigned int i; - git_reflog_entry *entry; - - if (reflog == NULL) - return; - - for (i=0; i < reflog->entries.length; i++) { - entry = git_vector_get(&reflog->entries, i); - - reflog_entry_free(entry); - } - - git_vector_free(&reflog->entries); - git__free(reflog->ref_name); - git__free(reflog); -} - -static int retrieve_reflog_path(git_buf *path, const git_reference *ref) -{ - return git_buf_join_n(path, '/', 3, - git_reference_owner(ref)->path_repository, GIT_REFLOG_DIR, ref->name); -} - -static int create_new_reflog_file(const char *filepath) -{ - int fd, error; - - if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0) - return error; - - if ((fd = p_open(filepath, - O_WRONLY | O_CREAT | O_TRUNC, - GIT_REFLOG_FILE_MODE)) < 0) - return -1; - - return p_close(fd); -} - -int git_reflog_read(git_reflog **reflog, const git_reference *ref) -{ - int error = -1; - git_buf log_path = GIT_BUF_INIT; - git_buf log_file = GIT_BUF_INIT; - git_reflog *log = NULL; - - assert(reflog && ref); - - *reflog = NULL; - - if (reflog_init(&log, ref) < 0) - return -1; - - if (retrieve_reflog_path(&log_path, ref) < 0) - goto cleanup; - - error = git_futils_readbuffer(&log_file, git_buf_cstr(&log_path)); - if (error < 0 && error != GIT_ENOTFOUND) - goto cleanup; - - if ((error == GIT_ENOTFOUND) && - ((error = create_new_reflog_file(git_buf_cstr(&log_path))) < 0)) - goto cleanup; - - if ((error = reflog_parse(log, - git_buf_cstr(&log_file), git_buf_len(&log_file))) < 0) - goto cleanup; - - *reflog = log; - goto success; - -cleanup: - git_reflog_free(log); - -success: - git_buf_free(&log_file); - git_buf_free(&log_path); - - return error; -} - -int git_reflog_write(git_reflog *reflog) -{ - int error = -1; - unsigned int i; - git_reflog_entry *entry; - git_buf log_path = GIT_BUF_INIT; - git_buf log = GIT_BUF_INIT; - git_filebuf fbuf = GIT_FILEBUF_INIT; - - assert(reflog); - - if (git_buf_join_n(&log_path, '/', 3, - git_repository_path(reflog->owner), GIT_REFLOG_DIR, reflog->ref_name) < 0) - return -1; - - if (!git_path_isfile(git_buf_cstr(&log_path))) { - giterr_set(GITERR_INVALID, - "Log file for reference '%s' doesn't exist.", reflog->ref_name); - goto cleanup; - } - - if ((error = git_filebuf_open(&fbuf, git_buf_cstr(&log_path), 0)) < 0) - goto cleanup; - - git_vector_foreach(&reflog->entries, i, entry) { - if (serialize_reflog_entry(&log, &(entry->oid_old), &(entry->oid_cur), entry->committer, entry->msg) < 0) - goto cleanup; - - if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0) - goto cleanup; - } - - error = git_filebuf_commit(&fbuf, GIT_REFLOG_FILE_MODE); - goto success; - -cleanup: - git_filebuf_cleanup(&fbuf); - -success: - git_buf_free(&log); - git_buf_free(&log_path); - return error; -} - -int git_reflog_append(git_reflog *reflog, const git_oid *new_oid, - const git_signature *committer, const char *msg) -{ - git_reflog_entry *entry; - const git_reflog_entry *previous; - const char *newline; - - assert(reflog && new_oid && committer); - - if (reflog_entry_new(&entry) < 0) - return -1; - - if ((entry->committer = git_signature_dup(committer)) == NULL) - goto cleanup; - - if (msg != NULL) { - if ((entry->msg = git__strdup(msg)) == NULL) - goto cleanup; - - newline = strchr(msg, '\n'); - - if (newline) { - if (newline[1] != '\0') { - giterr_set(GITERR_INVALID, "Reflog message cannot contain newline"); - goto cleanup; - } - - entry->msg[newline - msg] = '\0'; - } - } - - previous = git_reflog_entry_byindex(reflog, 0); - - if (previous == NULL) - git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO); - else - git_oid_cpy(&entry->oid_old, &previous->oid_cur); - - git_oid_cpy(&entry->oid_cur, new_oid); - - if (git_vector_insert(&reflog->entries, entry) < 0) - goto cleanup; - - return 0; - -cleanup: - reflog_entry_free(entry); - return -1; -} - -int git_reflog_rename(git_reference *ref, const char *new_name) -{ - int error = 0, fd; - git_buf old_path = GIT_BUF_INIT; - git_buf new_path = GIT_BUF_INIT; - git_buf temp_path = GIT_BUF_INIT; - git_buf normalized = GIT_BUF_INIT; - - assert(ref && new_name); - - if ((error = git_reference__normalize_name( - &normalized, new_name, GIT_REF_FORMAT_ALLOW_ONELEVEL)) < 0) - return error; - - if (git_buf_joinpath(&temp_path, git_reference_owner(ref)->path_repository, GIT_REFLOG_DIR) < 0) - return -1; - - if (git_buf_joinpath(&old_path, git_buf_cstr(&temp_path), ref->name) < 0) - return -1; - - if (git_buf_joinpath(&new_path, git_buf_cstr(&temp_path), git_buf_cstr(&normalized)) < 0) - return -1; - - /* - * Move the reflog to a temporary place. This two-phase renaming is required - * in order to cope with funny renaming use cases when one tries to move a reference - * to a partially colliding namespace: - * - a/b -> a/b/c - * - a/b/c/d -> a/b/c - */ - if (git_buf_joinpath(&temp_path, git_buf_cstr(&temp_path), "temp_reflog") < 0) - return -1; - - if ((fd = git_futils_mktmp(&temp_path, git_buf_cstr(&temp_path))) < 0) { - error = -1; - goto cleanup; - } - - p_close(fd); - - if (p_rename(git_buf_cstr(&old_path), git_buf_cstr(&temp_path)) < 0) { - giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name); - error = -1; - goto cleanup; - } - - if (git_path_isdir(git_buf_cstr(&new_path)) && - (git_futils_rmdir_r(git_buf_cstr(&new_path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) { - error = -1; - goto cleanup; - } - - if (git_futils_mkpath2file(git_buf_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) { - error = -1; - goto cleanup; - } - - if (p_rename(git_buf_cstr(&temp_path), git_buf_cstr(&new_path)) < 0) { - giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name); - error = -1; - } - -cleanup: - git_buf_free(&temp_path); - git_buf_free(&old_path); - git_buf_free(&new_path); - git_buf_free(&normalized); - - return error; -} - -int git_reflog_delete(git_reference *ref) -{ - int error; - git_buf path = GIT_BUF_INIT; - - error = retrieve_reflog_path(&path, ref); - - if (!error && git_path_exists(path.ptr)) - error = p_unlink(path.ptr); - - git_buf_free(&path); - - return error; -} - -size_t git_reflog_entrycount(git_reflog *reflog) -{ - assert(reflog); - return reflog->entries.length; -} - -GIT_INLINE(size_t) reflog_inverse_index(size_t idx, size_t total) -{ - return (total - 1) - idx; -} - -const git_reflog_entry * git_reflog_entry_byindex(git_reflog *reflog, size_t idx) -{ - assert(reflog); - - if (idx >= reflog->entries.length) - return NULL; - - return git_vector_get( - &reflog->entries, reflog_inverse_index(idx, reflog->entries.length)); -} - -const git_oid * git_reflog_entry_id_old(const git_reflog_entry *entry) -{ - assert(entry); - return &entry->oid_old; -} - -const git_oid * git_reflog_entry_id_new(const git_reflog_entry *entry) -{ - assert(entry); - return &entry->oid_cur; -} - -const git_signature * git_reflog_entry_committer(const git_reflog_entry *entry) -{ - assert(entry); - return entry->committer; -} - -const char * git_reflog_entry_message(const git_reflog_entry *entry) -{ - assert(entry); - return entry->msg; -} - -int git_reflog_drop( - git_reflog *reflog, - size_t idx, - int rewrite_previous_entry) -{ - size_t entrycount; - git_reflog_entry *entry, *previous; - - assert(reflog); - - entrycount = git_reflog_entrycount(reflog); - - entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx); - - if (entry == NULL) - return GIT_ENOTFOUND; - - reflog_entry_free(entry); - - if (git_vector_remove( - &reflog->entries, reflog_inverse_index(idx, entrycount)) < 0) - return -1; - - if (!rewrite_previous_entry) - return 0; - - /* No need to rewrite anything when removing the most recent entry */ - if (idx == 0) - return 0; - - /* Have the latest entry just been dropped? */ - if (entrycount == 1) - return 0; - - entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1); - - /* If the oldest entry has just been removed... */ - if (idx == entrycount - 1) { - /* ...clear the oid_old member of the "new" oldest entry */ - if (git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO) < 0) - return -1; - - return 0; - } - - previous = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx); - git_oid_cpy(&entry->oid_old, &previous->oid_cur); - - return 0; -} diff --git a/src/refs.c b/src/refs.c deleted file mode 100644 index e75f51001ba..00000000000 --- a/src/refs.c +++ /dev/null @@ -1,1999 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "refs.h" -#include "hash.h" -#include "repository.h" -#include "fileops.h" -#include "pack.h" -#include "reflog.h" - -#include -#include -#include -#include - -GIT__USE_STRMAP; - -#define DEFAULT_NESTING_LEVEL 5 -#define MAX_NESTING_LEVEL 10 - -enum { - GIT_PACKREF_HAS_PEEL = 1, - GIT_PACKREF_WAS_LOOSE = 2 -}; - -struct packref { - git_oid oid; - git_oid peel; - char flags; - char name[GIT_FLEX_ARRAY]; -}; - -static int reference_read( - git_buf *file_content, - time_t *mtime, - const char *repo_path, - const char *ref_name, - int *updated); - -/* loose refs */ -static int loose_parse_symbolic(git_reference *ref, git_buf *file_content); -static int loose_parse_oid(git_oid *ref, git_buf *file_content); -static int loose_lookup(git_reference *ref); -static int loose_lookup_to_packfile(struct packref **ref_out, - git_repository *repo, const char *name); -static int loose_write(git_reference *ref); - -/* packed refs */ -static int packed_parse_peel(struct packref *tag_ref, - const char **buffer_out, const char *buffer_end); -static int packed_parse_oid(struct packref **ref_out, - const char **buffer_out, const char *buffer_end); -static int packed_load(git_repository *repo); -static int packed_loadloose(git_repository *repository); -static int packed_write_ref(struct packref *ref, git_filebuf *file); -static int packed_find_peel(git_repository *repo, struct packref *ref); -static int packed_remove_loose(git_repository *repo, git_vector *packing_list); -static int packed_sort(const void *a, const void *b); -static int packed_lookup(git_reference *ref); -static int packed_write(git_repository *repo); - -/* internal helpers */ -static int reference_path_available(git_repository *repo, - const char *ref, const char *old_ref); -static int reference_delete(git_reference *ref); -static int reference_lookup(git_reference *ref); - -void git_reference_free(git_reference *reference) -{ - if (reference == NULL) - return; - - git__free(reference->name); - reference->name = NULL; - - if (reference->flags & GIT_REF_SYMBOLIC) { - git__free(reference->target.symbolic); - reference->target.symbolic = NULL; - } - - git__free(reference); -} - -static int reference_alloc( - git_reference **ref_out, - git_repository *repo, - const char *name) -{ - git_reference *reference = NULL; - - assert(ref_out && repo && name); - - reference = git__malloc(sizeof(git_reference)); - GITERR_CHECK_ALLOC(reference); - - memset(reference, 0x0, sizeof(git_reference)); - reference->owner = repo; - - reference->name = git__strdup(name); - GITERR_CHECK_ALLOC(reference->name); - - *ref_out = reference; - return 0; -} - -static int reference_read( - git_buf *file_content, - time_t *mtime, - const char *repo_path, - const char *ref_name, - int *updated) -{ - git_buf path = GIT_BUF_INIT; - int result; - - assert(file_content && repo_path && ref_name); - - /* Determine the full path of the file */ - if (git_buf_joinpath(&path, repo_path, ref_name) < 0) - return -1; - - result = git_futils_readbuffer_updated( - file_content, path.ptr, mtime, NULL, updated); - git_buf_free(&path); - - return result; -} - -static int loose_parse_symbolic(git_reference *ref, git_buf *file_content) -{ - const unsigned int header_len = (unsigned int)strlen(GIT_SYMREF); - const char *refname_start; - - refname_start = (const char *)file_content->ptr; - - if (git_buf_len(file_content) < header_len + 1) { - giterr_set(GITERR_REFERENCE, "Corrupted loose reference file"); - return -1; - } - - /* - * Assume we have already checked for the header - * before calling this function - */ - refname_start += header_len; - - ref->target.symbolic = git__strdup(refname_start); - GITERR_CHECK_ALLOC(ref->target.symbolic); - - return 0; -} - -static int loose_parse_oid(git_oid *oid, git_buf *file_content) -{ - size_t len; - const char *str; - - len = git_buf_len(file_content); - if (len < GIT_OID_HEXSZ) - goto corrupted; - - /* str is guranteed to be zero-terminated */ - str = git_buf_cstr(file_content); - - /* we need to get 40 OID characters from the file */ - if (git_oid_fromstr(oid, git_buf_cstr(file_content)) < 0) - goto corrupted; - - /* If the file is longer than 40 chars, the 41st must be a space */ - str += GIT_OID_HEXSZ; - if (*str == '\0' || git__isspace(*str)) - return 0; - -corrupted: - giterr_set(GITERR_REFERENCE, "Corrupted loose reference file"); - return -1; -} - -static git_ref_t loose_guess_rtype(const git_buf *full_path) -{ - git_buf ref_file = GIT_BUF_INIT; - git_ref_t type; - - type = GIT_REF_INVALID; - - if (git_futils_readbuffer(&ref_file, full_path->ptr) == 0) { - if (git__prefixcmp((const char *)(ref_file.ptr), GIT_SYMREF) == 0) - type = GIT_REF_SYMBOLIC; - else - type = GIT_REF_OID; - } - - git_buf_free(&ref_file); - return type; -} - -static int loose_lookup(git_reference *ref) -{ - int result, updated; - git_buf ref_file = GIT_BUF_INIT; - - result = reference_read(&ref_file, &ref->mtime, - ref->owner->path_repository, ref->name, &updated); - - if (result < 0) - return result; - - if (!updated) - return 0; - - if (ref->flags & GIT_REF_SYMBOLIC) { - git__free(ref->target.symbolic); - ref->target.symbolic = NULL; - } - - ref->flags = 0; - - if (git__prefixcmp((const char *)(ref_file.ptr), GIT_SYMREF) == 0) { - ref->flags |= GIT_REF_SYMBOLIC; - git_buf_rtrim(&ref_file); - result = loose_parse_symbolic(ref, &ref_file); - } else { - ref->flags |= GIT_REF_OID; - result = loose_parse_oid(&ref->target.oid, &ref_file); - } - - git_buf_free(&ref_file); - return result; -} - -static int loose_lookup_to_packfile( - struct packref **ref_out, - git_repository *repo, - const char *name) -{ - git_buf ref_file = GIT_BUF_INIT; - struct packref *ref = NULL; - size_t name_len; - - *ref_out = NULL; - - if (reference_read(&ref_file, NULL, repo->path_repository, name, NULL) < 0) - return -1; - - git_buf_rtrim(&ref_file); - - name_len = strlen(name); - ref = git__malloc(sizeof(struct packref) + name_len + 1); - GITERR_CHECK_ALLOC(ref); - - memcpy(ref->name, name, name_len); - ref->name[name_len] = 0; - - if (loose_parse_oid(&ref->oid, &ref_file) < 0) { - git_buf_free(&ref_file); - git__free(ref); - return -1; - } - - ref->flags = GIT_PACKREF_WAS_LOOSE; - - *ref_out = ref; - git_buf_free(&ref_file); - return 0; -} - -static int loose_write(git_reference *ref) -{ - git_filebuf file = GIT_FILEBUF_INIT; - git_buf ref_path = GIT_BUF_INIT; - struct stat st; - - /* Remove a possibly existing empty directory hierarchy - * which name would collide with the reference name - */ - if (git_futils_rmdir_r(ref->name, ref->owner->path_repository, - GIT_RMDIR_SKIP_NONEMPTY) < 0) - return -1; - - if (git_buf_joinpath(&ref_path, ref->owner->path_repository, ref->name) < 0) - return -1; - - if (git_filebuf_open(&file, ref_path.ptr, GIT_FILEBUF_FORCE) < 0) { - git_buf_free(&ref_path); - return -1; - } - - git_buf_free(&ref_path); - - if (ref->flags & GIT_REF_OID) { - char oid[GIT_OID_HEXSZ + 1]; - - git_oid_fmt(oid, &ref->target.oid); - oid[GIT_OID_HEXSZ] = '\0'; - - git_filebuf_printf(&file, "%s\n", oid); - - } else if (ref->flags & GIT_REF_SYMBOLIC) { - git_filebuf_printf(&file, GIT_SYMREF "%s\n", ref->target.symbolic); - } else { - assert(0); /* don't let this happen */ - } - - if (p_stat(ref_path.ptr, &st) == 0) - ref->mtime = st.st_mtime; - - return git_filebuf_commit(&file, GIT_REFS_FILE_MODE); -} - -static int packed_parse_peel( - struct packref *tag_ref, - const char **buffer_out, - const char *buffer_end) -{ - const char *buffer = *buffer_out + 1; - - assert(buffer[-1] == '^'); - - /* Ensure it's not the first entry of the file */ - if (tag_ref == NULL) - goto corrupt; - - /* Ensure reference is a tag */ - if (git__prefixcmp(tag_ref->name, GIT_REFS_TAGS_DIR) != 0) - goto corrupt; - - if (buffer + GIT_OID_HEXSZ > buffer_end) - goto corrupt; - - /* Is this a valid object id? */ - if (git_oid_fromstr(&tag_ref->peel, buffer) < 0) - goto corrupt; - - buffer = buffer + GIT_OID_HEXSZ; - if (*buffer == '\r') - buffer++; - - if (buffer != buffer_end) { - if (*buffer == '\n') - buffer++; - else - goto corrupt; - } - - *buffer_out = buffer; - return 0; - -corrupt: - giterr_set(GITERR_REFERENCE, "The packed references file is corrupted"); - return -1; -} - -static int packed_parse_oid( - struct packref **ref_out, - const char **buffer_out, - const char *buffer_end) -{ - struct packref *ref = NULL; - - const char *buffer = *buffer_out; - const char *refname_begin, *refname_end; - - size_t refname_len; - git_oid id; - - refname_begin = (buffer + GIT_OID_HEXSZ + 1); - if (refname_begin >= buffer_end || refname_begin[-1] != ' ') - goto corrupt; - - /* Is this a valid object id? */ - if (git_oid_fromstr(&id, buffer) < 0) - goto corrupt; - - refname_end = memchr(refname_begin, '\n', buffer_end - refname_begin); - if (refname_end == NULL) - refname_end = buffer_end; - - if (refname_end[-1] == '\r') - refname_end--; - - refname_len = refname_end - refname_begin; - - ref = git__malloc(sizeof(struct packref) + refname_len + 1); - GITERR_CHECK_ALLOC(ref); - - memcpy(ref->name, refname_begin, refname_len); - ref->name[refname_len] = 0; - - git_oid_cpy(&ref->oid, &id); - - ref->flags = 0; - - *ref_out = ref; - *buffer_out = refname_end + 1; - - return 0; - -corrupt: - git__free(ref); - giterr_set(GITERR_REFERENCE, "The packed references file is corrupted"); - return -1; -} - -static int packed_load(git_repository *repo) -{ - int result, updated; - git_buf packfile = GIT_BUF_INIT; - const char *buffer_start, *buffer_end; - git_refcache *ref_cache = &repo->references; - - /* First we make sure we have allocated the hash table */ - if (ref_cache->packfile == NULL) { - ref_cache->packfile = git_strmap_alloc(); - GITERR_CHECK_ALLOC(ref_cache->packfile); - } - - result = reference_read(&packfile, &ref_cache->packfile_time, - repo->path_repository, GIT_PACKEDREFS_FILE, &updated); - - /* - * If we couldn't find the file, we need to clear the table and - * return. On any other error, we return that error. If everything - * went fine and the file wasn't updated, then there's nothing new - * for us here, so just return. Anything else means we need to - * refresh the packed refs. - */ - if (result == GIT_ENOTFOUND) { - git_strmap_clear(ref_cache->packfile); - return 0; - } - - if (result < 0) - return -1; - - if (!updated) - return 0; - - /* - * At this point, we want to refresh the packed refs. We already - * have the contents in our buffer. - */ - git_strmap_clear(ref_cache->packfile); - - buffer_start = (const char *)packfile.ptr; - buffer_end = (const char *)(buffer_start) + packfile.size; - - while (buffer_start < buffer_end && buffer_start[0] == '#') { - buffer_start = strchr(buffer_start, '\n'); - if (buffer_start == NULL) - goto parse_failed; - - buffer_start++; - } - - while (buffer_start < buffer_end) { - int err; - struct packref *ref = NULL; - - if (packed_parse_oid(&ref, &buffer_start, buffer_end) < 0) - goto parse_failed; - - if (buffer_start[0] == '^') { - if (packed_parse_peel(ref, &buffer_start, buffer_end) < 0) - goto parse_failed; - } - - git_strmap_insert(ref_cache->packfile, ref->name, ref, err); - if (err < 0) - goto parse_failed; - } - - git_buf_free(&packfile); - return 0; - -parse_failed: - git_strmap_free(ref_cache->packfile); - ref_cache->packfile = NULL; - git_buf_free(&packfile); - return -1; -} - - -struct dirent_list_data { - git_repository *repo; - size_t repo_path_len; - unsigned int list_flags; - - int (*callback)(const char *, void *); - void *callback_payload; - int callback_error; -}; - -static int _dirent_loose_listall(void *_data, git_buf *full_path) -{ - struct dirent_list_data *data = (struct dirent_list_data *)_data; - const char *file_path = full_path->ptr + data->repo_path_len; - - if (git_path_isdir(full_path->ptr) == true) - return git_path_direach(full_path, _dirent_loose_listall, _data); - - /* do not add twice a reference that exists already in the packfile */ - if ((data->list_flags & GIT_REF_PACKED) != 0 && - git_strmap_exists(data->repo->references.packfile, file_path)) - return 0; - - if (data->list_flags != GIT_REF_LISTALL) { - if ((data->list_flags & loose_guess_rtype(full_path)) == 0) - return 0; /* we are filtering out this reference */ - } - - /* Locked references aren't returned */ - if (!git__suffixcmp(file_path, GIT_FILELOCK_EXTENSION)) - return 0; - - if (data->callback(file_path, data->callback_payload)) - data->callback_error = GIT_EUSER; - - return data->callback_error; -} - -static int _dirent_loose_load(void *data, git_buf *full_path) -{ - git_repository *repository = (git_repository *)data; - void *old_ref = NULL; - struct packref *ref; - const char *file_path; - int err; - - if (git_path_isdir(full_path->ptr) == true) - return git_path_direach(full_path, _dirent_loose_load, repository); - - file_path = full_path->ptr + strlen(repository->path_repository); - - if (loose_lookup_to_packfile(&ref, repository, file_path) < 0) - return -1; - - git_strmap_insert2( - repository->references.packfile, ref->name, ref, old_ref, err); - if (err < 0) { - git__free(ref); - return -1; - } - - git__free(old_ref); - return 0; -} - -/* - * Load all the loose references from the repository - * into the in-memory Packfile, and build a vector with - * all the references so it can be written back to - * disk. - */ -static int packed_loadloose(git_repository *repository) -{ - git_buf refs_path = GIT_BUF_INIT; - int result; - - /* the packfile must have been previously loaded! */ - assert(repository->references.packfile); - - if (git_buf_joinpath(&refs_path, repository->path_repository, GIT_REFS_DIR) < 0) - return -1; - - /* - * Load all the loose files from disk into the Packfile table. - * This will overwrite any old packed entries with their - * updated loose versions - */ - result = git_path_direach(&refs_path, _dirent_loose_load, repository); - git_buf_free(&refs_path); - - return result; -} - -/* - * Write a single reference into a packfile - */ -static int packed_write_ref(struct packref *ref, git_filebuf *file) -{ - char oid[GIT_OID_HEXSZ + 1]; - - git_oid_fmt(oid, &ref->oid); - oid[GIT_OID_HEXSZ] = 0; - - /* - * For references that peel to an object in the repo, we must - * write the resulting peel on a separate line, e.g. - * - * 6fa8a902cc1d18527e1355773c86721945475d37 refs/tags/libgit2-0.4 - * ^2ec0cb7959b0bf965d54f95453f5b4b34e8d3100 - * - * This obviously only applies to tags. - * The required peels have already been loaded into `ref->peel_target`. - */ - if (ref->flags & GIT_PACKREF_HAS_PEEL) { - char peel[GIT_OID_HEXSZ + 1]; - git_oid_fmt(peel, &ref->peel); - peel[GIT_OID_HEXSZ] = 0; - - if (git_filebuf_printf(file, "%s %s\n^%s\n", oid, ref->name, peel) < 0) - return -1; - } else { - if (git_filebuf_printf(file, "%s %s\n", oid, ref->name) < 0) - return -1; - } - - return 0; -} - -/* - * Find out what object this reference resolves to. - * - * For references that point to a 'big' tag (e.g. an - * actual tag object on the repository), we need to - * cache on the packfile the OID of the object to - * which that 'big tag' is pointing to. - */ -static int packed_find_peel(git_repository *repo, struct packref *ref) -{ - git_object *object; - - if (ref->flags & GIT_PACKREF_HAS_PEEL) - return 0; - - /* - * Only applies to tags, i.e. references - * in the /refs/tags folder - */ - if (git__prefixcmp(ref->name, GIT_REFS_TAGS_DIR) != 0) - return 0; - - /* - * Find the tagged object in the repository - */ - if (git_object_lookup(&object, repo, &ref->oid, GIT_OBJ_ANY) < 0) - return -1; - - /* - * If the tagged object is a Tag object, we need to resolve it; - * if the ref is actually a 'weak' ref, we don't need to resolve - * anything. - */ - if (git_object_type(object) == GIT_OBJ_TAG) { - git_tag *tag = (git_tag *)object; - - /* - * Find the object pointed at by this tag - */ - git_oid_cpy(&ref->peel, git_tag_target_id(tag)); - ref->flags |= GIT_PACKREF_HAS_PEEL; - - /* - * The reference has now cached the resolved OID, and is - * marked at such. When written to the packfile, it'll be - * accompanied by this resolved oid - */ - } - - git_object_free(object); - return 0; -} - -/* - * Remove all loose references - * - * Once we have successfully written a packfile, - * all the loose references that were packed must be - * removed from disk. - * - * This is a dangerous method; make sure the packfile - * is well-written, because we are destructing references - * here otherwise. - */ -static int packed_remove_loose(git_repository *repo, git_vector *packing_list) -{ - unsigned int i; - git_buf full_path = GIT_BUF_INIT; - int failed = 0; - - for (i = 0; i < packing_list->length; ++i) { - struct packref *ref = git_vector_get(packing_list, i); - - if ((ref->flags & GIT_PACKREF_WAS_LOOSE) == 0) - continue; - - if (git_buf_joinpath(&full_path, repo->path_repository, ref->name) < 0) - return -1; /* critical; do not try to recover on oom */ - - if (git_path_exists(full_path.ptr) == true && p_unlink(full_path.ptr) < 0) { - if (failed) - continue; - - giterr_set(GITERR_REFERENCE, - "Failed to remove loose reference '%s' after packing: %s", - full_path.ptr, strerror(errno)); - - failed = 1; - } - - /* - * if we fail to remove a single file, this is *not* good, - * but we should keep going and remove as many as possible. - * After we've removed as many files as possible, we return - * the error code anyway. - */ - } - - git_buf_free(&full_path); - return failed ? -1 : 0; -} - -static int packed_sort(const void *a, const void *b) -{ - const struct packref *ref_a = (const struct packref *)a; - const struct packref *ref_b = (const struct packref *)b; - - return strcmp(ref_a->name, ref_b->name); -} - -/* - * Write all the contents in the in-memory packfile to disk. - */ -static int packed_write(git_repository *repo) -{ - git_filebuf pack_file = GIT_FILEBUF_INIT; - unsigned int i; - git_buf pack_file_path = GIT_BUF_INIT; - git_vector packing_list; - unsigned int total_refs; - - assert(repo && repo->references.packfile); - - total_refs = - (unsigned int)git_strmap_num_entries(repo->references.packfile); - - if (git_vector_init(&packing_list, total_refs, packed_sort) < 0) - return -1; - - /* Load all the packfile into a vector */ - { - struct packref *reference; - - /* cannot fail: vector already has the right size */ - git_strmap_foreach_value(repo->references.packfile, reference, { - git_vector_insert(&packing_list, reference); - }); - } - - /* sort the vector so the entries appear sorted on the packfile */ - git_vector_sort(&packing_list); - - /* Now we can open the file! */ - if (git_buf_joinpath(&pack_file_path, repo->path_repository, GIT_PACKEDREFS_FILE) < 0) - goto cleanup_memory; - - if (git_filebuf_open(&pack_file, pack_file_path.ptr, 0) < 0) - goto cleanup_packfile; - - /* Packfiles have a header... apparently - * This is in fact not required, but we might as well print it - * just for kicks */ - if (git_filebuf_printf(&pack_file, "%s\n", GIT_PACKEDREFS_HEADER) < 0) - goto cleanup_packfile; - - for (i = 0; i < packing_list.length; ++i) { - struct packref *ref = (struct packref *)git_vector_get(&packing_list, i); - - if (packed_find_peel(repo, ref) < 0) - goto cleanup_packfile; - - if (packed_write_ref(ref, &pack_file) < 0) - goto cleanup_packfile; - } - - /* if we've written all the references properly, we can commit - * the packfile to make the changes effective */ - if (git_filebuf_commit(&pack_file, GIT_PACKEDREFS_FILE_MODE) < 0) - goto cleanup_memory; - - /* when and only when the packfile has been properly written, - * we can go ahead and remove the loose refs */ - if (packed_remove_loose(repo, &packing_list) < 0) - goto cleanup_memory; - - { - struct stat st; - if (p_stat(pack_file_path.ptr, &st) == 0) - repo->references.packfile_time = st.st_mtime; - } - - git_vector_free(&packing_list); - git_buf_free(&pack_file_path); - - /* we're good now */ - return 0; - -cleanup_packfile: - git_filebuf_cleanup(&pack_file); - -cleanup_memory: - git_vector_free(&packing_list); - git_buf_free(&pack_file_path); - - return -1; -} - -struct reference_available_t { - const char *new_ref; - const char *old_ref; - int available; -}; - -static int _reference_available_cb(const char *ref, void *data) -{ - struct reference_available_t *d; - - assert(ref && data); - d = (struct reference_available_t *)data; - - if (!d->old_ref || strcmp(d->old_ref, ref)) { - size_t reflen = strlen(ref); - size_t newlen = strlen(d->new_ref); - size_t cmplen = reflen < newlen ? reflen : newlen; - const char *lead = reflen < newlen ? d->new_ref : ref; - - if (!strncmp(d->new_ref, ref, cmplen) && lead[cmplen] == '/') { - d->available = 0; - return -1; - } - } - - return 0; -} - -static int reference_path_available( - git_repository *repo, - const char *ref, - const char* old_ref) -{ - int error; - struct reference_available_t data; - - data.new_ref = ref; - data.old_ref = old_ref; - data.available = 1; - - error = git_reference_foreach( - repo, GIT_REF_LISTALL, _reference_available_cb, (void *)&data); - if (error < 0) - return error; - - if (!data.available) { - giterr_set(GITERR_REFERENCE, - "The path to reference '%s' collides with an existing one", ref); - return -1; - } - - return 0; -} - -static int reference_exists(int *exists, git_repository *repo, const char *ref_name) -{ - git_buf ref_path = GIT_BUF_INIT; - - if (packed_load(repo) < 0) - return -1; - - if (git_buf_joinpath(&ref_path, repo->path_repository, ref_name) < 0) - return -1; - - if (git_path_isfile(ref_path.ptr) == true || - git_strmap_exists(repo->references.packfile, ref_path.ptr)) - { - *exists = 1; - } else { - *exists = 0; - } - - git_buf_free(&ref_path); - return 0; -} - -/* - * Check if a reference could be written to disk, based on: - * - * - Whether a reference with the same name already exists, - * and we are allowing or disallowing overwrites - * - * - Whether the name of the reference would collide with - * an existing path - */ -static int reference_can_write( - git_repository *repo, - const char *refname, - const char *previous_name, - int force) -{ - /* see if the reference shares a path with an existing reference; - * if a path is shared, we cannot create the reference, even when forcing */ - if (reference_path_available(repo, refname, previous_name) < 0) - return -1; - - /* check if the reference actually exists, but only if we are not forcing - * the rename. If we are forcing, it's OK to overwrite */ - if (!force) { - int exists; - - if (reference_exists(&exists, repo, refname) < 0) - return -1; - - /* We cannot proceed if the reference already exists and we're not forcing - * the rename; the existing one would be overwritten */ - if (exists) { - giterr_set(GITERR_REFERENCE, - "A reference with that name (%s) already exists", refname); - return GIT_EEXISTS; - } - } - - /* FIXME: if the reference exists and we are forcing, do we really need to - * remove the reference first? - * - * Two cases: - * - * - the reference already exists and is loose: not a problem, the file - * gets overwritten on disk - * - * - the reference already exists and is packed: we write a new one as - * loose, which by all means renders the packed one useless - */ - - return 0; -} - - -static int packed_lookup(git_reference *ref) -{ - struct packref *pack_ref = NULL; - git_strmap *packfile_refs; - khiter_t pos; - - if (packed_load(ref->owner) < 0) - return -1; - - /* maybe the packfile hasn't changed at all, so we don't - * have to re-lookup the reference */ - if ((ref->flags & GIT_REF_PACKED) && - ref->mtime == ref->owner->references.packfile_time) - return 0; - - if (ref->flags & GIT_REF_SYMBOLIC) { - git__free(ref->target.symbolic); - ref->target.symbolic = NULL; - } - - /* Look up on the packfile */ - packfile_refs = ref->owner->references.packfile; - pos = git_strmap_lookup_index(packfile_refs, ref->name); - if (!git_strmap_valid_index(packfile_refs, pos)) { - giterr_set(GITERR_REFERENCE, "Reference '%s' not found", ref->name); - return GIT_ENOTFOUND; - } - - pack_ref = git_strmap_value_at(packfile_refs, pos); - - ref->flags = GIT_REF_OID | GIT_REF_PACKED; - ref->mtime = ref->owner->references.packfile_time; - git_oid_cpy(&ref->target.oid, &pack_ref->oid); - - return 0; -} - -static int reference_lookup(git_reference *ref) -{ - int result; - - result = loose_lookup(ref); - if (result == 0) - return 0; - - /* only try to lookup this reference on the packfile if it - * wasn't found on the loose refs; not if there was a critical error */ - if (result == GIT_ENOTFOUND) { - giterr_clear(); - result = packed_lookup(ref); - if (result == 0) - return 0; - } - - /* unexpected error; free the reference */ - git_reference_free(ref); - return result; -} - -/* - * Delete a reference. - * This is an internal method; the reference is removed - * from disk or the packfile, but the pointer is not freed - */ -static int reference_delete(git_reference *ref) -{ - int result; - - assert(ref); - - /* If the reference is packed, this is an expensive operation. - * We need to reload the packfile, remove the reference from the - * packing list, and repack */ - if (ref->flags & GIT_REF_PACKED) { - git_strmap *packfile_refs; - struct packref *packref; - khiter_t pos; - - /* load the existing packfile */ - if (packed_load(ref->owner) < 0) - return -1; - - packfile_refs = ref->owner->references.packfile; - pos = git_strmap_lookup_index(packfile_refs, ref->name); - if (!git_strmap_valid_index(packfile_refs, pos)) { - giterr_set(GITERR_REFERENCE, - "Reference %s stopped existing in the packfile", ref->name); - return -1; - } - - packref = git_strmap_value_at(packfile_refs, pos); - git_strmap_delete_at(packfile_refs, pos); - - git__free(packref); - if (packed_write(ref->owner) < 0) - return -1; - - /* If the reference is loose, we can just remove the reference - * from the filesystem */ - } else { - git_reference *ref_in_pack; - git_buf full_path = GIT_BUF_INIT; - - if (git_buf_joinpath(&full_path, ref->owner->path_repository, ref->name) < 0) - return -1; - - result = p_unlink(full_path.ptr); - git_buf_free(&full_path); /* done with path at this point */ - - if (result < 0) { - giterr_set(GITERR_OS, "Failed to unlink '%s'", full_path.ptr); - return -1; - } - - /* When deleting a loose reference, we have to ensure that an older - * packed version of it doesn't exist */ - if (git_reference_lookup(&ref_in_pack, ref->owner, ref->name) == 0) { - assert((ref_in_pack->flags & GIT_REF_PACKED) != 0); - return git_reference_delete(ref_in_pack); - } - - giterr_clear(); - } - - return 0; -} - -int git_reference_delete(git_reference *ref) -{ - int result = reference_delete(ref); - git_reference_free(ref); - return result; -} - -int git_reference_lookup(git_reference **ref_out, - git_repository *repo, const char *name) -{ - return git_reference_lookup_resolved(ref_out, repo, name, 0); -} - -int git_reference_name_to_id( - git_oid *out, git_repository *repo, const char *name) -{ - int error; - git_reference *ref; - - if ((error = git_reference_lookup_resolved(&ref, repo, name, -1)) < 0) - return error; - - git_oid_cpy(out, git_reference_target(ref)); - git_reference_free(ref); - return 0; -} - -int git_reference_lookup_resolved( - git_reference **ref_out, - git_repository *repo, - const char *name, - int max_nesting) -{ - git_reference *scan; - int result, nesting; - - assert(ref_out && repo && name); - - *ref_out = NULL; - - if (max_nesting > MAX_NESTING_LEVEL) - max_nesting = MAX_NESTING_LEVEL; - else if (max_nesting < 0) - max_nesting = DEFAULT_NESTING_LEVEL; - - scan = git__calloc(1, sizeof(git_reference)); - GITERR_CHECK_ALLOC(scan); - - scan->name = git__calloc(GIT_REFNAME_MAX + 1, sizeof(char)); - GITERR_CHECK_ALLOC(scan->name); - - if ((result = git_reference__normalize_name_lax( - scan->name, - GIT_REFNAME_MAX, - name)) < 0) { - git_reference_free(scan); - return result; - } - - scan->target.symbolic = git__strdup(scan->name); - GITERR_CHECK_ALLOC(scan->target.symbolic); - - scan->owner = repo; - scan->flags = GIT_REF_SYMBOLIC; - - for (nesting = max_nesting; - nesting >= 0 && (scan->flags & GIT_REF_SYMBOLIC) != 0; - nesting--) - { - if (nesting != max_nesting) - strncpy(scan->name, scan->target.symbolic, GIT_REFNAME_MAX); - - scan->mtime = 0; - - if ((result = reference_lookup(scan)) < 0) - return result; /* lookup git_reference_free on scan already */ - } - - if ((scan->flags & GIT_REF_OID) == 0 && max_nesting != 0) { - giterr_set(GITERR_REFERENCE, - "Cannot resolve reference (>%u levels deep)", max_nesting); - git_reference_free(scan); - return -1; - } - - *ref_out = scan; - return 0; -} - -/** - * Getters - */ -git_ref_t git_reference_type(const git_reference *ref) -{ - assert(ref); - - if (ref->flags & GIT_REF_OID) - return GIT_REF_OID; - - if (ref->flags & GIT_REF_SYMBOLIC) - return GIT_REF_SYMBOLIC; - - return GIT_REF_INVALID; -} - -int git_reference_is_packed(git_reference *ref) -{ - assert(ref); - return !!(ref->flags & GIT_REF_PACKED); -} - -const char *git_reference_name(const git_reference *ref) -{ - assert(ref); - return ref->name; -} - -git_repository *git_reference_owner(const git_reference *ref) -{ - assert(ref); - return ref->owner; -} - -const git_oid *git_reference_target(const git_reference *ref) -{ - assert(ref); - - if ((ref->flags & GIT_REF_OID) == 0) - return NULL; - - return &ref->target.oid; -} - -const char *git_reference_symbolic_target(const git_reference *ref) -{ - assert(ref); - - if ((ref->flags & GIT_REF_SYMBOLIC) == 0) - return NULL; - - return ref->target.symbolic; -} - -int git_reference_symbolic_create( - git_reference **ref_out, - git_repository *repo, - const char *name, - const char *target, - int force) -{ - char normalized[GIT_REFNAME_MAX]; - git_reference *ref = NULL; - int error; - - if ((error = git_reference__normalize_name_lax( - normalized, - sizeof(normalized), - name)) < 0) - return error; - - if ((error = reference_can_write(repo, normalized, NULL, force)) < 0) - return error; - - if (reference_alloc(&ref, repo, normalized) < 0) - return -1; - - ref->flags |= GIT_REF_SYMBOLIC; - - /* set the target; this will normalize the name automatically - * and write the reference on disk */ - if (git_reference_symbolic_set_target(ref, target) < 0) { - git_reference_free(ref); - return -1; - } - if (ref_out == NULL) { - git_reference_free(ref); - } else { - *ref_out = ref; - } - - return 0; -} - -int git_reference_create( - git_reference **ref_out, - git_repository *repo, - const char *name, - const git_oid *id, - int force) -{ - int error; - git_reference *ref = NULL; - char normalized[GIT_REFNAME_MAX]; - - if ((error = git_reference__normalize_name_lax( - normalized, - sizeof(normalized), - name)) < 0) - return error; - - if ((error = reference_can_write(repo, normalized, NULL, force)) < 0) - return error; - - if (reference_alloc(&ref, repo, name) < 0) - return -1; - - ref->flags |= GIT_REF_OID; - - /* set the oid; this will write the reference on disk */ - if (git_reference_set_target(ref, id) < 0) { - git_reference_free(ref); - return -1; - } - - if (ref_out == NULL) { - git_reference_free(ref); - } else { - *ref_out = ref; - } - - return 0; -} -/* - * Change the OID target of a reference. - * - * For both loose and packed references, just change - * the oid in memory and (over)write the file in disk. - * - * We do not repack packed references because of performance - * reasons. - */ -int git_reference_set_target(git_reference *ref, const git_oid *id) -{ - git_odb *odb = NULL; - - if ((ref->flags & GIT_REF_OID) == 0) { - giterr_set(GITERR_REFERENCE, "Cannot set OID on symbolic reference"); - return -1; - } - - assert(ref->owner); - - if (git_repository_odb__weakptr(&odb, ref->owner) < 0) - return -1; - - /* Don't let the user create references to OIDs that - * don't exist in the ODB */ - if (!git_odb_exists(odb, id)) { - giterr_set(GITERR_REFERENCE, - "Target OID for the reference doesn't exist on the repository"); - return -1; - } - - /* Update the OID value on `ref` */ - git_oid_cpy(&ref->target.oid, id); - - /* Write back to disk */ - return loose_write(ref); -} - -/* - * Change the target of a symbolic reference. - * - * This is easy because symrefs cannot be inside - * a pack. We just change the target in memory - * and overwrite the file on disk. - */ -int git_reference_symbolic_set_target(git_reference *ref, const char *target) -{ - int error; - char normalized[GIT_REFNAME_MAX]; - - if ((ref->flags & GIT_REF_SYMBOLIC) == 0) { - giterr_set(GITERR_REFERENCE, - "Cannot set symbolic target on a direct reference"); - return -1; - } - - if ((error = git_reference__normalize_name_lax( - normalized, - sizeof(normalized), - target)) < 0) - return error; - - git__free(ref->target.symbolic); - ref->target.symbolic = git__strdup(normalized); - GITERR_CHECK_ALLOC(ref->target.symbolic); - - return loose_write(ref); -} - -int git_reference_rename(git_reference *ref, const char *new_name, int force) -{ - int result; - unsigned int normalization_flags; - git_buf aux_path = GIT_BUF_INIT; - char normalized[GIT_REFNAME_MAX]; - bool should_head_be_updated = false; - - normalization_flags = ref->flags & GIT_REF_SYMBOLIC ? - GIT_REF_FORMAT_ALLOW_ONELEVEL - : GIT_REF_FORMAT_NORMAL; - - if ((result = git_reference_normalize_name( - normalized, - sizeof(normalized), - new_name, - normalization_flags)) < 0) - return result; - - if ((result = reference_can_write(ref->owner, normalized, ref->name, force)) < 0) - return result; - - /* Initialize path now so we won't get an allocation failure once - * we actually start removing things. */ - if (git_buf_joinpath(&aux_path, ref->owner->path_repository, new_name) < 0) - return -1; - - /* - * Check if we have to update HEAD. - */ - if ((should_head_be_updated = git_branch_is_head(ref)) < 0) - goto cleanup; - - /* - * Now delete the old ref and remove an possibly existing directory - * named `new_name`. Note that using the internal `reference_delete` - * method deletes the ref from disk but doesn't free the pointer, so - * we can still access the ref's attributes for creating the new one - */ - if (reference_delete(ref) < 0) - goto cleanup; - - /* - * Finally we can create the new reference. - */ - if (ref->flags & GIT_REF_SYMBOLIC) { - result = git_reference_symbolic_create( - NULL, ref->owner, new_name, ref->target.symbolic, force); - } else { - result = git_reference_create( - NULL, ref->owner, new_name, &ref->target.oid, force); - } - - if (result < 0) - goto rollback; - - /* - * Update HEAD it was poiting to the reference being renamed. - */ - if (should_head_be_updated && - git_repository_set_head(ref->owner, new_name) < 0) { - giterr_set(GITERR_REFERENCE, - "Failed to update HEAD after renaming reference"); - goto cleanup; - } - - /* - * Rename the reflog file, if it exists. - */ - if ((git_reference_has_log(ref)) && (git_reflog_rename(ref, new_name) < 0)) - goto cleanup; - - /* - * Change the name of the reference given by the user. - */ - git__free(ref->name); - ref->name = git__strdup(new_name); - - /* The reference is no longer packed */ - ref->flags &= ~GIT_REF_PACKED; - - git_buf_free(&aux_path); - return 0; - -cleanup: - git_buf_free(&aux_path); - return -1; - -rollback: - /* - * Try to create the old reference again, ignore failures - */ - if (ref->flags & GIT_REF_SYMBOLIC) - git_reference_symbolic_create( - NULL, ref->owner, ref->name, ref->target.symbolic, 0); - else - git_reference_create( - NULL, ref->owner, ref->name, &ref->target.oid, 0); - - /* The reference is no longer packed */ - ref->flags &= ~GIT_REF_PACKED; - - git_buf_free(&aux_path); - return -1; -} - -int git_reference_resolve(git_reference **ref_out, const git_reference *ref) -{ - if (ref->flags & GIT_REF_OID) - return git_reference_lookup(ref_out, ref->owner, ref->name); - else - return git_reference_lookup_resolved(ref_out, ref->owner, ref->target.symbolic, -1); -} - -int git_reference_packall(git_repository *repo) -{ - if (packed_load(repo) < 0 || /* load the existing packfile */ - packed_loadloose(repo) < 0 || /* add all the loose refs */ - packed_write(repo) < 0) /* write back to disk */ - return -1; - - return 0; -} - -int git_reference_foreach( - git_repository *repo, - unsigned int list_flags, - git_reference_foreach_cb callback, - void *payload) -{ - int result; - struct dirent_list_data data; - git_buf refs_path = GIT_BUF_INIT; - - /* list all the packed references first */ - if (list_flags & GIT_REF_PACKED) { - const char *ref_name; - void *ref; - GIT_UNUSED(ref); - - if (packed_load(repo) < 0) - return -1; - - git_strmap_foreach(repo->references.packfile, ref_name, ref, { - if (callback(ref_name, payload)) - return GIT_EUSER; - }); - } - - /* now list the loose references, trying not to - * duplicate the ref names already in the packed-refs file */ - - data.repo_path_len = strlen(repo->path_repository); - data.list_flags = list_flags; - data.repo = repo; - data.callback = callback; - data.callback_payload = payload; - data.callback_error = 0; - - if (git_buf_joinpath(&refs_path, repo->path_repository, GIT_REFS_DIR) < 0) - return -1; - - result = git_path_direach(&refs_path, _dirent_loose_listall, &data); - - git_buf_free(&refs_path); - - return data.callback_error ? GIT_EUSER : result; -} - -static int cb__reflist_add(const char *ref, void *data) -{ - return git_vector_insert((git_vector *)data, git__strdup(ref)); -} - -int git_reference_list( - git_strarray *array, - git_repository *repo, - unsigned int list_flags) -{ - git_vector ref_list; - - assert(array && repo); - - array->strings = NULL; - array->count = 0; - - if (git_vector_init(&ref_list, 8, NULL) < 0) - return -1; - - if (git_reference_foreach( - repo, list_flags, &cb__reflist_add, (void *)&ref_list) < 0) { - git_vector_free(&ref_list); - return -1; - } - - array->strings = (char **)ref_list.contents; - array->count = ref_list.length; - return 0; -} - -int git_reference_reload(git_reference *ref) -{ - return reference_lookup(ref); -} - -void git_repository__refcache_free(git_refcache *refs) -{ - assert(refs); - - if (refs->packfile) { - struct packref *reference; - - git_strmap_foreach_value(refs->packfile, reference, { - git__free(reference); - }); - - git_strmap_free(refs->packfile); - } -} - -static int is_valid_ref_char(char ch) -{ - if ((unsigned) ch <= ' ') - return 0; - - switch (ch) { - case '~': - case '^': - case ':': - case '\\': - case '?': - case '[': - case '*': - return 0; - default: - return 1; - } -} - -static int ensure_segment_validity(const char *name) -{ - const char *current = name; - char prev = '\0'; - - if (*current == '.') - return -1; /* Refname starts with "." */ - - for (current = name; ; current++) { - if (*current == '\0' || *current == '/') - break; - - if (!is_valid_ref_char(*current)) - return -1; /* Illegal character in refname */ - - if (prev == '.' && *current == '.') - return -1; /* Refname contains ".." */ - - if (prev == '@' && *current == '{') - return -1; /* Refname contains "@{" */ - - prev = *current; - } - - return (int)(current - name); -} - -static bool is_all_caps_and_underscore(const char *name, size_t len) -{ - size_t i; - char c; - - assert(name && len > 0); - - for (i = 0; i < len; i++) - { - c = name[i]; - if ((c < 'A' || c > 'Z') && c != '_') - return false; - } - - if (*name == '_' || name[len - 1] == '_') - return false; - - return true; -} - -int git_reference__normalize_name( - git_buf *buf, - const char *name, - unsigned int flags) -{ - // Inspired from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/refs.c#L36-100 - - char *current; - int segment_len, segments_count = 0, error = GIT_EINVALIDSPEC; - unsigned int process_flags; - bool normalize = (buf != NULL); - assert(name); - - process_flags = flags; - current = (char *)name; - - if (normalize) - git_buf_clear(buf); - - while (true) { - segment_len = ensure_segment_validity(current); - if (segment_len < 0) { - if ((process_flags & GIT_REF_FORMAT_REFSPEC_PATTERN) && - current[0] == '*' && - (current[1] == '\0' || current[1] == '/')) { - /* Accept one wildcard as a full refname component. */ - process_flags &= ~GIT_REF_FORMAT_REFSPEC_PATTERN; - segment_len = 1; - } else - goto cleanup; - } - - if (segment_len > 0) { - if (normalize) { - size_t cur_len = git_buf_len(buf); - - git_buf_joinpath(buf, git_buf_cstr(buf), current); - git_buf_truncate(buf, - cur_len + segment_len + (segments_count ? 1 : 0)); - - if (git_buf_oom(buf)) { - error = -1; - goto cleanup; - } - } - - segments_count++; - } - - /* This means that there's a leading slash in the refname */ - if (segment_len == 0 && segments_count == 0) { - goto cleanup; - } - - if (current[segment_len] == '\0') - break; - - current += segment_len + 1; - } - - /* A refname can not be empty */ - if (segment_len == 0 && segments_count == 0) - goto cleanup; - - /* A refname can not end with "." */ - if (current[segment_len - 1] == '.') - goto cleanup; - - /* A refname can not end with "/" */ - if (current[segment_len - 1] == '/') - goto cleanup; - - /* A refname can not end with ".lock" */ - if (!git__suffixcmp(name, GIT_FILELOCK_EXTENSION)) - goto cleanup; - - if ((segments_count == 1 ) && !(flags & GIT_REF_FORMAT_ALLOW_ONELEVEL)) - goto cleanup; - - if ((segments_count == 1 ) && - !(is_all_caps_and_underscore(name, (size_t)segment_len) || - ((flags & GIT_REF_FORMAT_REFSPEC_PATTERN) && !strcmp("*", name)))) - goto cleanup; - - if ((segments_count > 1) - && (is_all_caps_and_underscore(name, strchr(name, '/') - name))) - goto cleanup; - - error = 0; - -cleanup: - if (error == GIT_EINVALIDSPEC) - giterr_set( - GITERR_REFERENCE, - "The given reference name '%s' is not valid", name); - - if (error && normalize) - git_buf_free(buf); - - return error; -} - -int git_reference_normalize_name( - char *buffer_out, - size_t buffer_size, - const char *name, - unsigned int flags) -{ - git_buf buf = GIT_BUF_INIT; - int error; - - if ((error = git_reference__normalize_name(&buf, name, flags)) < 0) - goto cleanup; - - if (git_buf_len(&buf) > buffer_size - 1) { - giterr_set( - GITERR_REFERENCE, - "The provided buffer is too short to hold the normalization of '%s'", name); - error = GIT_EBUFS; - goto cleanup; - } - - git_buf_copy_cstr(buffer_out, buffer_size, &buf); - - error = 0; - -cleanup: - git_buf_free(&buf); - return error; -} - -int git_reference__normalize_name_lax( - char *buffer_out, - size_t out_size, - const char *name) -{ - return git_reference_normalize_name( - buffer_out, - out_size, - name, - GIT_REF_FORMAT_ALLOW_ONELEVEL); -} -#define GIT_REF_TYPEMASK (GIT_REF_OID | GIT_REF_SYMBOLIC) - -int git_reference_cmp(git_reference *ref1, git_reference *ref2) -{ - assert(ref1 && ref2); - - /* let's put symbolic refs before OIDs */ - if ((ref1->flags & GIT_REF_TYPEMASK) != (ref2->flags & GIT_REF_TYPEMASK)) - return (ref1->flags & GIT_REF_SYMBOLIC) ? -1 : 1; - - if (ref1->flags & GIT_REF_SYMBOLIC) - return strcmp(ref1->target.symbolic, ref2->target.symbolic); - - return git_oid_cmp(&ref1->target.oid, &ref2->target.oid); -} - -/* Update the reference named `ref_name` so it points to `oid` */ -int git_reference__update(git_repository *repo, const git_oid *oid, const char *ref_name) -{ - git_reference *ref; - int res; - - res = git_reference_lookup(&ref, repo, ref_name); - - /* If we haven't found the reference at all, we assume we need to create - * a new reference and that's it */ - if (res == GIT_ENOTFOUND) { - giterr_clear(); - return git_reference_create(NULL, repo, ref_name, oid, 1); - } - - if (res < 0) - return -1; - - /* If we have found a reference, but it's symbolic, we need to update - * the direct reference it points to */ - if (git_reference_type(ref) == GIT_REF_SYMBOLIC) { - git_reference *aux; - const char *sym_target; - - /* The target pointed at by this reference */ - sym_target = git_reference_symbolic_target(ref); - - /* resolve the reference to the target it points to */ - res = git_reference_resolve(&aux, ref); - - /* - * if the symbolic reference pointed to an inexisting ref, - * this is means we're creating a new branch, for example. - * We need to create a new direct reference with that name - */ - if (res == GIT_ENOTFOUND) { - giterr_clear(); - res = git_reference_create(NULL, repo, sym_target, oid, 1); - git_reference_free(ref); - return res; - } - - /* free the original symbolic reference now; not before because - * we're using the `sym_target` pointer */ - git_reference_free(ref); - - if (res < 0) - return -1; - - /* store the newly found direct reference in its place */ - ref = aux; - } - - /* ref is made to point to `oid`: ref is either the original reference, - * or the target of the symbolic reference we've looked up */ - res = git_reference_set_target(ref, oid); - git_reference_free(ref); - return res; -} - -struct glob_cb_data { - const char *glob; - int (*callback)(const char *, void *); - void *payload; -}; - -static int fromglob_cb(const char *reference_name, void *payload) -{ - struct glob_cb_data *data = (struct glob_cb_data *)payload; - - if (!p_fnmatch(data->glob, reference_name, 0)) - return data->callback(reference_name, data->payload); - - return 0; -} - -int git_reference_foreach_glob( - git_repository *repo, - const char *glob, - unsigned int list_flags, - int (*callback)( - const char *reference_name, - void *payload), - void *payload) -{ - struct glob_cb_data data; - - assert(repo && glob && callback); - - data.glob = glob; - data.callback = callback; - data.payload = payload; - - return git_reference_foreach( - repo, list_flags, fromglob_cb, &data); -} - -int git_reference_has_log( - git_reference *ref) -{ - git_buf path = GIT_BUF_INIT; - int result; - - assert(ref); - - if (git_buf_join_n(&path, '/', 3, ref->owner->path_repository, GIT_REFLOG_DIR, ref->name) < 0) - return -1; - - result = git_path_isfile(git_buf_cstr(&path)); - git_buf_free(&path); - - return result; -} - -int git_reference__is_branch(const char *ref_name) -{ - return git__prefixcmp(ref_name, GIT_REFS_HEADS_DIR) == 0; -} - -int git_reference_is_branch(git_reference *ref) -{ - assert(ref); - return git_reference__is_branch(ref->name); -} - -int git_reference_is_remote(git_reference *ref) -{ - assert(ref); - return git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR) == 0; -} - -static int peel_error(int error, git_reference *ref, const char* msg) -{ - giterr_set( - GITERR_INVALID, - "The reference '%s' cannot be peeled - %s", git_reference_name(ref), msg); - return error; -} - -static int reference_target(git_object **object, git_reference *ref) -{ - const git_oid *oid; - - oid = git_reference_target(ref); - - return git_object_lookup(object, git_reference_owner(ref), oid, GIT_OBJ_ANY); -} - -int git_reference_peel( - git_object **peeled, - git_reference *ref, - git_otype target_type) -{ - git_reference *resolved = NULL; - git_object *target = NULL; - int error; - - assert(ref); - - if ((error = git_reference_resolve(&resolved, ref)) < 0) - return peel_error(error, ref, "Cannot resolve reference"); - - if ((error = reference_target(&target, resolved)) < 0) { - peel_error(error, ref, "Cannot retrieve reference target"); - goto cleanup; - } - - if (target_type == GIT_OBJ_ANY && git_object_type(target) != GIT_OBJ_TAG) - error = git_object__dup(peeled, target); - else - error = git_object_peel(peeled, target, target_type); - -cleanup: - git_object_free(target); - git_reference_free(resolved); - return error; -} - -int git_reference__is_valid_name( - const char *refname, - unsigned int flags) -{ - int error; - - error = git_reference__normalize_name(NULL, refname, flags) == 0; - giterr_clear(); - - return error; -} - -int git_reference_is_valid_name( - const char *refname) -{ - return git_reference__is_valid_name( - refname, - GIT_REF_FORMAT_ALLOW_ONELEVEL); -} diff --git a/src/refs.h b/src/refs.h deleted file mode 100644 index 1228cea877d..00000000000 --- a/src/refs.h +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_refs_h__ -#define INCLUDE_refs_h__ - -#include "common.h" -#include "git2/oid.h" -#include "git2/refs.h" -#include "strmap.h" -#include "buffer.h" - -#define GIT_REFS_DIR "refs/" -#define GIT_REFS_HEADS_DIR GIT_REFS_DIR "heads/" -#define GIT_REFS_TAGS_DIR GIT_REFS_DIR "tags/" -#define GIT_REFS_REMOTES_DIR GIT_REFS_DIR "remotes/" -#define GIT_REFS_DIR_MODE 0777 -#define GIT_REFS_FILE_MODE 0666 - -#define GIT_RENAMED_REF_FILE GIT_REFS_DIR "RENAMED-REF" - -#define GIT_SYMREF "ref: " -#define GIT_PACKEDREFS_FILE "packed-refs" -#define GIT_PACKEDREFS_HEADER "# pack-refs with: peeled " -#define GIT_PACKEDREFS_FILE_MODE 0666 - -#define GIT_HEAD_FILE "HEAD" -#define GIT_ORIG_HEAD_FILE "ORIG_HEAD" -#define GIT_FETCH_HEAD_FILE "FETCH_HEAD" -#define GIT_MERGE_HEAD_FILE "MERGE_HEAD" -#define GIT_REVERT_HEAD_FILE "REVERT_HEAD" -#define GIT_CHERRY_PICK_HEAD_FILE "CHERRY_PICK_HEAD" -#define GIT_BISECT_LOG_FILE "BISECT_LOG" -#define GIT_REBASE_MERGE_DIR "rebase-merge/" -#define GIT_REBASE_MERGE_INTERACTIVE_FILE GIT_REBASE_MERGE_DIR "interactive" -#define GIT_REBASE_APPLY_DIR "rebase-apply/" -#define GIT_REBASE_APPLY_REBASING_FILE GIT_REBASE_APPLY_DIR "rebasing" -#define GIT_REBASE_APPLY_APPLYING_FILE GIT_REBASE_APPLY_DIR "applying" -#define GIT_REFS_HEADS_MASTER_FILE GIT_REFS_HEADS_DIR "master" - -#define GIT_STASH_FILE "stash" -#define GIT_REFS_STASH_FILE GIT_REFS_DIR GIT_STASH_FILE - -#define GIT_REFNAME_MAX 1024 - -struct git_reference { - unsigned int flags; - git_repository *owner; - char *name; - time_t mtime; - - union { - git_oid oid; - char *symbolic; - } target; -}; - -typedef struct { - git_strmap *packfile; - time_t packfile_time; -} git_refcache; - -void git_repository__refcache_free(git_refcache *refs); - -int git_reference__normalize_name_lax(char *buffer_out, size_t out_size, const char *name); -int git_reference__normalize_name(git_buf *buf, const char *name, unsigned int flags); -int git_reference__is_valid_name(const char *refname, unsigned int flags); -int git_reference__update(git_repository *repo, const git_oid *oid, const char *ref_name); -int git_reference__is_branch(const char *ref_name); - -/** - * Lookup a reference by name and try to resolve to an OID. - * - * You can control how many dereferences this will attempt to resolve the - * reference with the `max_deref` parameter, or pass -1 to use a sane - * default. If you pass 0 for `max_deref`, this will not attempt to resolve - * the reference. For any value of `max_deref` other than 0, not - * successfully resolving the reference will be reported as an error. - - * The generated reference must be freed by the user. - * - * @param reference_out Pointer to the looked-up reference - * @param repo The repository to look up the reference - * @param name The long name for the reference (e.g. HEAD, ref/heads/master, refs/tags/v0.1.0, ...) - * @param max_deref Maximum number of dereferences to make of symbolic refs, 0 means simple lookup, < 0 means use default reasonable value - * @return 0 on success or < 0 on error; not being able to resolve the reference is an error unless 0 was passed for max_deref - */ -int git_reference_lookup_resolved( - git_reference **reference_out, - git_repository *repo, - const char *name, - int max_deref); - -#endif diff --git a/src/refspec.c b/src/refspec.c deleted file mode 100644 index bd69f58aeee..00000000000 --- a/src/refspec.c +++ /dev/null @@ -1,248 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "git2/errors.h" - -#include "common.h" -#include "refspec.h" -#include "util.h" -#include "posix.h" -#include "refs.h" - -int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch) -{ - // Ported from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/remote.c#L518-636 - - size_t llen; - int is_glob = 0; - const char *lhs, *rhs; - int flags; - - assert(refspec && input); - - memset(refspec, 0x0, sizeof(git_refspec)); - - lhs = input; - if (*lhs == '+') { - refspec->force = 1; - lhs++; - } - - rhs = strrchr(lhs, ':'); - - /* - * Before going on, special case ":" (or "+:") as a refspec - * for matching refs. - */ - if (!is_fetch && rhs == lhs && rhs[1] == '\0') { - refspec->matching = 1; - return 0; - } - - if (rhs) { - size_t rlen = strlen(++rhs); - is_glob = (1 <= rlen && strchr(rhs, '*')); - refspec->dst = git__strndup(rhs, rlen); - } - - llen = (rhs ? (size_t)(rhs - lhs - 1) : strlen(lhs)); - if (1 <= llen && memchr(lhs, '*', llen)) { - if ((rhs && !is_glob) || (!rhs && is_fetch)) - goto invalid; - is_glob = 1; - } else if (rhs && is_glob) - goto invalid; - - refspec->pattern = is_glob; - refspec->src = git__strndup(lhs, llen); - flags = GIT_REF_FORMAT_ALLOW_ONELEVEL - | (is_glob ? GIT_REF_FORMAT_REFSPEC_PATTERN : 0); - - if (is_fetch) { - /* - * LHS - * - empty is allowed; it means HEAD. - * - otherwise it must be a valid looking ref. - */ - if (!*refspec->src) - ; /* empty is ok */ - else if (!git_reference__is_valid_name(refspec->src, flags)) - goto invalid; - /* - * RHS - * - missing is ok, and is same as empty. - * - empty is ok; it means not to store. - * - otherwise it must be a valid looking ref. - */ - if (!refspec->dst) - ; /* ok */ - else if (!*refspec->dst) - ; /* ok */ - else if (!git_reference__is_valid_name(refspec->dst, flags)) - goto invalid; - } else { - /* - * LHS - * - empty is allowed; it means delete. - * - when wildcarded, it must be a valid looking ref. - * - otherwise, it must be an extended SHA-1, but - * there is no existing way to validate this. - */ - if (!*refspec->src) - ; /* empty is ok */ - else if (is_glob) { - if (!git_reference__is_valid_name(refspec->src, flags)) - goto invalid; - } - else { - ; /* anything goes, for now */ - } - /* - * RHS - * - missing is allowed, but LHS then must be a - * valid looking ref. - * - empty is not allowed. - * - otherwise it must be a valid looking ref. - */ - if (!refspec->dst) { - if (!git_reference__is_valid_name(refspec->src, flags)) - goto invalid; - } else if (!*refspec->dst) { - goto invalid; - } else { - if (!git_reference__is_valid_name(refspec->dst, flags)) - goto invalid; - } - } - - return 0; - - invalid: - return -1; -} - -void git_refspec__free(git_refspec *refspec) -{ - if (refspec == NULL) - return; - - git__free(refspec->src); - git__free(refspec->dst); -} - -const char *git_refspec_src(const git_refspec *refspec) -{ - return refspec == NULL ? NULL : refspec->src; -} - -const char *git_refspec_dst(const git_refspec *refspec) -{ - return refspec == NULL ? NULL : refspec->dst; -} - -int git_refspec_force(const git_refspec *refspec) -{ - assert(refspec); - - return refspec->force; -} - -int git_refspec_src_matches(const git_refspec *refspec, const char *refname) -{ - if (refspec == NULL || refspec->src == NULL) - return false; - - return (p_fnmatch(refspec->src, refname, 0) == 0); -} - -int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, const char *name) -{ - size_t baselen, namelen; - - baselen = strlen(spec->dst); - if (outlen <= baselen) { - giterr_set(GITERR_INVALID, "Reference name too long"); - return GIT_EBUFS; - } - - /* - * No '*' at the end means that it's mapped to one specific local - * branch, so no actual transformation is needed. - */ - if (spec->dst[baselen - 1] != '*') { - memcpy(out, spec->dst, baselen + 1); /* include '\0' */ - return 0; - } - - /* There's a '*' at the end, so remove its length */ - baselen--; - - /* skip the prefix, -1 is for the '*' */ - name += strlen(spec->src) - 1; - - namelen = strlen(name); - - if (outlen <= baselen + namelen) { - giterr_set(GITERR_INVALID, "Reference name too long"); - return GIT_EBUFS; - } - - memcpy(out, spec->dst, baselen); - memcpy(out + baselen, name, namelen + 1); - - return 0; -} - -static int refspec_transform(git_buf *out, const char *from, const char *to, const char *name) -{ - if (git_buf_sets(out, to) < 0) - return -1; - - /* - * No '*' at the end means that it's mapped to one specific - * branch, so no actual transformation is needed. - */ - if (git_buf_len(out) > 0 && out->ptr[git_buf_len(out) - 1] != '*') - return 0; - - git_buf_truncate(out, git_buf_len(out) - 1); /* remove trailing '*' */ - git_buf_puts(out, name + strlen(from) - 1); - - if (git_buf_oom(out)) - return -1; - - return 0; -} - -int git_refspec_transform_r(git_buf *out, const git_refspec *spec, const char *name) -{ - return refspec_transform(out, spec->src, spec->dst, name); -} - -int git_refspec_transform_l(git_buf *out, const git_refspec *spec, const char *name) -{ - return refspec_transform(out, spec->dst, spec->src, name); -} - -int git_refspec__serialize(git_buf *out, const git_refspec *refspec) -{ - if (refspec->force) - git_buf_putc(out, '+'); - - git_buf_printf(out, "%s:%s", - refspec->src != NULL ? refspec->src : "", - refspec->dst != NULL ? refspec->dst : ""); - - return git_buf_oom(out) == false; -} - -int git_refspec_is_wildcard(const git_refspec *spec) -{ - assert(spec && spec->src); - - return (spec->src[strlen(spec->src) - 1] == '*'); -} diff --git a/src/refspec.h b/src/refspec.h deleted file mode 100644 index a7a4dd83423..00000000000 --- a/src/refspec.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_refspec_h__ -#define INCLUDE_refspec_h__ - -#include "git2/refspec.h" -#include "buffer.h" - -struct git_refspec { - struct git_refspec *next; - char *src; - char *dst; - unsigned int force :1, - pattern :1, - matching :1; -}; - -#define GIT_REFSPEC_TAGS "refs/tags/*:refs/tags/*" - -int git_refspec_parse(struct git_refspec *refspec, const char *str); -int git_refspec__parse( - struct git_refspec *refspec, - const char *str, - bool is_fetch); - -void git_refspec__free(git_refspec *refspec); - -/** - * Transform a reference to its target following the refspec's rules, - * and writes the results into a git_buf. - * - * @param out where to store the target name - * @param spec the refspec - * @param name the name of the reference to transform - * @return 0 or error if buffer allocation fails - */ -int git_refspec_transform_r(git_buf *out, const git_refspec *spec, const char *name); - -/** - * Transform a reference from its target following the refspec's rules, - * and writes the results into a git_buf. - * - * @param out where to store the source name - * @param spec the refspec - * @param name the name of the reference to transform - * @return 0 or error if buffer allocation fails - */ -int git_refspec_transform_l(git_buf *out, const git_refspec *spec, const char *name); - -int git_refspec__serialize(git_buf *out, const git_refspec *refspec); - -/** - * Determines if a refspec is a wildcard refspec. - * - * @param spec the refspec - * @return 1 if the refspec is a wildcard, 0 otherwise - */ -int git_refspec_is_wildcard(const git_refspec *spec); - -#endif diff --git a/src/remote.c b/src/remote.c deleted file mode 100644 index 920ca7a18cb..00000000000 --- a/src/remote.c +++ /dev/null @@ -1,1382 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "git2/config.h" -#include "git2/types.h" -#include "git2/oid.h" - -#include "config.h" -#include "repository.h" -#include "remote.h" -#include "fetch.h" -#include "refs.h" -#include "refspec.h" -#include "fetchhead.h" - -#include - -static int parse_remote_refspec(git_config *cfg, git_refspec *refspec, const char *var, bool is_fetch) -{ - int error; - const char *val; - - if ((error = git_config_get_string(&val, cfg, var)) < 0) - return error; - - return git_refspec__parse(refspec, val, is_fetch); -} - -static int download_tags_value(git_remote *remote, git_config *cfg) -{ - const char *val; - git_buf buf = GIT_BUF_INIT; - int error; - - if (remote->download_tags != GIT_REMOTE_DOWNLOAD_TAGS_UNSET) - return 0; - - /* This is the default, let's see if we need to change it */ - remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO; - if (git_buf_printf(&buf, "remote.%s.tagopt", remote->name) < 0) - return -1; - - error = git_config_get_string(&val, cfg, git_buf_cstr(&buf)); - git_buf_free(&buf); - if (!error && !strcmp(val, "--no-tags")) - remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_NONE; - else if (!error && !strcmp(val, "--tags")) - remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL; - - if (error == GIT_ENOTFOUND) - error = 0; - - return error; -} - -static int ensure_remote_name_is_valid(const char *name) -{ - git_buf buf = GIT_BUF_INIT; - git_refspec refspec; - int error = -1; - - if (!name || *name == '\0') - goto cleanup; - - git_buf_printf(&buf, "refs/heads/test:refs/remotes/%s/test", name); - error = git_refspec__parse(&refspec, git_buf_cstr(&buf), true); - - git_buf_free(&buf); - git_refspec__free(&refspec); - -cleanup: - if (error) { - giterr_set( - GITERR_CONFIG, - "'%s' is not a valid remote name.", name); - error = GIT_EINVALIDSPEC; - } - - return error; -} - -static int create_internal(git_remote **out, git_repository *repo, const char *name, const char *url, const char *fetch) -{ - git_remote *remote; - git_buf fetchbuf = GIT_BUF_INIT; - int error = -1; - - /* name is optional */ - assert(out && repo && url); - - remote = git__calloc(1, sizeof(git_remote)); - GITERR_CHECK_ALLOC(remote); - - remote->repo = repo; - remote->check_cert = 1; - remote->update_fetchhead = 1; - - if (git_vector_init(&remote->refs, 32, NULL) < 0) - goto on_error; - - remote->url = git__strdup(url); - GITERR_CHECK_ALLOC(remote->url); - - if (name != NULL) { - remote->name = git__strdup(name); - GITERR_CHECK_ALLOC(remote->name); - } - - if (fetch != NULL) { - if (git_refspec__parse(&remote->fetch, fetch, true) < 0) - goto on_error; - } - - /* A remote without a name doesn't download tags */ - if (!name) { - remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_NONE; - } - - *out = remote; - git_buf_free(&fetchbuf); - return 0; - -on_error: - git_remote_free(remote); - git_buf_free(&fetchbuf); - return error; -} - -static int ensure_remote_doesnot_exist(git_repository *repo, const char *name) -{ - int error; - git_remote *remote; - - error = git_remote_load(&remote, repo, name); - - if (error == GIT_ENOTFOUND) - return 0; - - if (error < 0) - return error; - - git_remote_free(remote); - - giterr_set( - GITERR_CONFIG, - "Remote '%s' already exists.", name); - - return GIT_EEXISTS; -} - - -int git_remote_create(git_remote **out, git_repository *repo, const char *name, const char *url) -{ - git_buf buf = GIT_BUF_INIT; - git_remote *remote = NULL; - int error; - - if ((error = ensure_remote_name_is_valid(name)) < 0) - return error; - - if ((error = ensure_remote_doesnot_exist(repo, name)) < 0) - return error; - - if (git_buf_printf(&buf, "+refs/heads/*:refs/remotes/%s/*", name) < 0) - return -1; - - if (create_internal(&remote, repo, name, url, git_buf_cstr(&buf)) < 0) - goto on_error; - - git_buf_free(&buf); - - if (git_remote_save(remote) < 0) - goto on_error; - - *out = remote; - - return 0; - -on_error: - git_buf_free(&buf); - git_remote_free(remote); - return -1; -} - -int git_remote_create_inmemory(git_remote **out, git_repository *repo, const char *fetch, const char *url) -{ - int error; - git_remote *remote; - - if ((error = create_internal(&remote, repo, NULL, url, fetch)) < 0) - return error; - - *out = remote; - return 0; -} - -int git_remote_load(git_remote **out, git_repository *repo, const char *name) -{ - git_remote *remote; - git_buf buf = GIT_BUF_INIT; - const char *val; - int error = 0; - git_config *config; - - assert(out && repo && name); - - if ((error = ensure_remote_name_is_valid(name)) < 0) - return error; - - if (git_repository_config__weakptr(&config, repo) < 0) - return -1; - - remote = git__malloc(sizeof(git_remote)); - GITERR_CHECK_ALLOC(remote); - - memset(remote, 0x0, sizeof(git_remote)); - remote->check_cert = 1; - remote->update_fetchhead = 1; - remote->name = git__strdup(name); - GITERR_CHECK_ALLOC(remote->name); - - if (git_vector_init(&remote->refs, 32, NULL) < 0) { - error = -1; - goto cleanup; - } - - if (git_buf_printf(&buf, "remote.%s.url", name) < 0) { - error = -1; - goto cleanup; - } - - if ((error = git_config_get_string(&val, config, git_buf_cstr(&buf))) < 0) - goto cleanup; - - if (strlen(val) == 0) { - giterr_set(GITERR_INVALID, "Malformed remote '%s' - missing URL", name); - error = -1; - goto cleanup; - } - - remote->repo = repo; - remote->url = git__strdup(val); - GITERR_CHECK_ALLOC(remote->url); - - git_buf_clear(&buf); - if (git_buf_printf(&buf, "remote.%s.pushurl", name) < 0) { - error = -1; - goto cleanup; - } - - error = git_config_get_string(&val, config, git_buf_cstr(&buf)); - if (error == GIT_ENOTFOUND) { - val = NULL; - error = 0; - } - - if (error < 0) { - error = -1; - goto cleanup; - } - - if (val) { - remote->pushurl = git__strdup(val); - GITERR_CHECK_ALLOC(remote->pushurl); - } - - git_buf_clear(&buf); - if (git_buf_printf(&buf, "remote.%s.fetch", name) < 0) { - error = -1; - goto cleanup; - } - - error = parse_remote_refspec(config, &remote->fetch, git_buf_cstr(&buf), true); - if (error == GIT_ENOTFOUND) - error = 0; - - if (error < 0) { - error = -1; - goto cleanup; - } - - git_buf_clear(&buf); - if (git_buf_printf(&buf, "remote.%s.push", name) < 0) { - error = -1; - goto cleanup; - } - - error = parse_remote_refspec(config, &remote->push, git_buf_cstr(&buf), false); - if (error == GIT_ENOTFOUND) - error = 0; - - if (error < 0) { - error = -1; - goto cleanup; - } - - if (download_tags_value(remote, config) < 0) - goto cleanup; - - *out = remote; - -cleanup: - git_buf_free(&buf); - - if (error < 0) - git_remote_free(remote); - - return error; -} - -static int update_config_refspec( - git_config *config, - const char *remote_name, - const git_refspec *refspec, - int git_direction) -{ - git_buf name = GIT_BUF_INIT, value = GIT_BUF_INIT; - int error = -1; - - if (refspec->src == NULL || refspec->dst == NULL) - return 0; - - if (git_buf_printf( - &name, - "remote.%s.%s", - remote_name, - git_direction == GIT_DIRECTION_FETCH ? "fetch" : "push") < 0) - goto cleanup; - - if (git_refspec__serialize(&value, refspec) < 0) - goto cleanup; - - error = git_config_set_string( - config, - git_buf_cstr(&name), - git_buf_cstr(&value)); - -cleanup: - git_buf_free(&name); - git_buf_free(&value); - - return error; -} - -int git_remote_save(const git_remote *remote) -{ - int error; - git_config *config; - const char *tagopt = NULL; - git_buf buf = GIT_BUF_INIT; - - assert(remote); - - if (!remote->name) { - giterr_set(GITERR_INVALID, "Can't save an in-memory remote."); - return GIT_EINVALIDSPEC; - } - - if ((error = ensure_remote_name_is_valid(remote->name)) < 0) - return error; - - if (git_repository_config__weakptr(&config, remote->repo) < 0) - return -1; - - if (git_buf_printf(&buf, "remote.%s.url", remote->name) < 0) - return -1; - - if (git_config_set_string(config, git_buf_cstr(&buf), remote->url) < 0) { - git_buf_free(&buf); - return -1; - } - - git_buf_clear(&buf); - if (git_buf_printf(&buf, "remote.%s.pushurl", remote->name) < 0) - return -1; - - if (remote->pushurl) { - if (git_config_set_string(config, git_buf_cstr(&buf), remote->pushurl) < 0) { - git_buf_free(&buf); - return -1; - } - } else { - int error = git_config_delete_entry(config, git_buf_cstr(&buf)); - if (error == GIT_ENOTFOUND) { - error = 0; - giterr_clear(); - } - if (error < 0) { - git_buf_free(&buf); - return -1; - } - } - - if (update_config_refspec( - config, - remote->name, - &remote->fetch, - GIT_DIRECTION_FETCH) < 0) - goto on_error; - - if (update_config_refspec( - config, - remote->name, - &remote->push, - GIT_DIRECTION_PUSH) < 0) - goto on_error; - - /* - * What action to take depends on the old and new values. This - * is describes by the table below. tagopt means whether the - * is already a value set in the config - * - * AUTO ALL or NONE - * +-----------------------+ - * tagopt | remove | set | - * +---------+-------------| - * !tagopt | nothing | set | - * +---------+-------------+ - */ - - git_buf_clear(&buf); - if (git_buf_printf(&buf, "remote.%s.tagopt", remote->name) < 0) - goto on_error; - - error = git_config_get_string(&tagopt, config, git_buf_cstr(&buf)); - if (error < 0 && error != GIT_ENOTFOUND) - goto on_error; - - if (remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL) { - if (git_config_set_string(config, git_buf_cstr(&buf), "--tags") < 0) - goto on_error; - } else if (remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_NONE) { - if (git_config_set_string(config, git_buf_cstr(&buf), "--no-tags") < 0) - goto on_error; - } else if (tagopt) { - if (git_config_delete_entry(config, git_buf_cstr(&buf)) < 0) - goto on_error; - } - - git_buf_free(&buf); - - return 0; - -on_error: - git_buf_free(&buf); - return -1; -} - -const char *git_remote_name(const git_remote *remote) -{ - assert(remote); - return remote->name; -} - -const char *git_remote_url(const git_remote *remote) -{ - assert(remote); - return remote->url; -} - -int git_remote_set_url(git_remote *remote, const char* url) -{ - assert(remote); - assert(url); - - git__free(remote->url); - remote->url = git__strdup(url); - GITERR_CHECK_ALLOC(remote->url); - - return 0; -} - -const char *git_remote_pushurl(const git_remote *remote) -{ - assert(remote); - return remote->pushurl; -} - -int git_remote_set_pushurl(git_remote *remote, const char* url) -{ - assert(remote); - - git__free(remote->pushurl); - if (url) { - remote->pushurl = git__strdup(url); - GITERR_CHECK_ALLOC(remote->pushurl); - } else { - remote->pushurl = NULL; - } - return 0; -} - -int git_remote_set_fetchspec(git_remote *remote, const char *spec) -{ - git_refspec refspec; - - assert(remote && spec); - - if (git_refspec__parse(&refspec, spec, true) < 0) - return -1; - - git_refspec__free(&remote->fetch); - memcpy(&remote->fetch, &refspec, sizeof(git_refspec)); - - return 0; -} - -const git_refspec *git_remote_fetchspec(const git_remote *remote) -{ - assert(remote); - return &remote->fetch; -} - -int git_remote_set_pushspec(git_remote *remote, const char *spec) -{ - git_refspec refspec; - - assert(remote && spec); - - if (git_refspec__parse(&refspec, spec, false) < 0) - return -1; - - git_refspec__free(&remote->push); - remote->push.src = refspec.src; - remote->push.dst = refspec.dst; - - return 0; -} - -const git_refspec *git_remote_pushspec(const git_remote *remote) -{ - assert(remote); - return &remote->push; -} - -const char* git_remote__urlfordirection(git_remote *remote, int direction) -{ - assert(remote); - - if (direction == GIT_DIRECTION_FETCH) { - return remote->url; - } - - if (direction == GIT_DIRECTION_PUSH) { - return remote->pushurl ? remote->pushurl : remote->url; - } - - return NULL; -} - -int git_remote_connect(git_remote *remote, git_direction direction) -{ - git_transport *t; - const char *url; - int flags = GIT_TRANSPORTFLAGS_NONE; - - assert(remote); - - t = remote->transport; - - url = git_remote__urlfordirection(remote, direction); - if (url == NULL ) - return -1; - - /* A transport could have been supplied in advance with - * git_remote_set_transport */ - if (!t && git_transport_new(&t, remote, url) < 0) - return -1; - - if (t->set_callbacks && - t->set_callbacks(t, remote->callbacks.progress, NULL, remote->callbacks.payload) < 0) - goto on_error; - - if (!remote->check_cert) - flags |= GIT_TRANSPORTFLAGS_NO_CHECK_CERT; - - if (t->connect(t, url, remote->cred_acquire_cb, remote->cred_acquire_payload, direction, flags) < 0) - goto on_error; - - remote->transport = t; - - return 0; - -on_error: - t->free(t); - - if (t == remote->transport) - remote->transport = NULL; - - return -1; -} - -int git_remote_ls(git_remote *remote, git_headlist_cb list_cb, void *payload) -{ - assert(remote); - - if (!git_remote_connected(remote)) { - giterr_set(GITERR_NET, "The remote is not connected"); - return -1; - } - - return remote->transport->ls(remote->transport, list_cb, payload); -} - -int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, char **proxy_url) -{ - git_config *cfg; - const char *val; - - assert(remote); - - if (!proxy_url || !remote->repo) - return -1; - - *proxy_url = NULL; - - if (git_repository_config__weakptr(&cfg, remote->repo) < 0) - return -1; - - /* Go through the possible sources for proxy configuration, from most specific - * to least specific. */ - - /* remote..proxy config setting */ - if (remote->name && 0 != *(remote->name)) { - git_buf buf = GIT_BUF_INIT; - - if (git_buf_printf(&buf, "remote.%s.proxy", remote->name) < 0) - return -1; - - if (!git_config_get_string(&val, cfg, git_buf_cstr(&buf)) && - val && ('\0' != *val)) { - git_buf_free(&buf); - - *proxy_url = git__strdup(val); - GITERR_CHECK_ALLOC(*proxy_url); - return 0; - } - - git_buf_free(&buf); - } - - /* http.proxy config setting */ - if (!git_config_get_string(&val, cfg, "http.proxy") && - val && ('\0' != *val)) { - *proxy_url = git__strdup(val); - GITERR_CHECK_ALLOC(*proxy_url); - return 0; - } - - /* HTTP_PROXY / HTTPS_PROXY environment variables */ - val = use_ssl ? getenv("HTTPS_PROXY") : getenv("HTTP_PROXY"); - - if (val && ('\0' != *val)) { - *proxy_url = git__strdup(val); - GITERR_CHECK_ALLOC(*proxy_url); - return 0; - } - - return 0; -} - -int git_remote_download( - git_remote *remote, - git_transfer_progress_callback progress_cb, - void *progress_payload) -{ - int error; - - assert(remote); - - if ((error = git_fetch_negotiate(remote)) < 0) - return error; - - return git_fetch_download_pack(remote, progress_cb, progress_payload); -} - -static int update_tips_callback(git_remote_head *head, void *payload) -{ - git_vector *refs = (git_vector *)payload; - - return git_vector_insert(refs, head); -} - -static int remote_head_for_fetchspec_src(git_remote_head **out, git_vector *update_heads, const char *fetchspec_src) -{ - unsigned int i; - git_remote_head *remote_ref; - - assert(update_heads && fetchspec_src); - - *out = NULL; - - git_vector_foreach(update_heads, i, remote_ref) { - if (strcmp(remote_ref->name, fetchspec_src) == 0) { - *out = remote_ref; - break; - } - } - - return 0; -} - -static int remote_head_for_ref(git_remote_head **out, git_remote *remote, git_vector *update_heads, git_reference *ref) -{ - git_reference *resolved_ref = NULL; - git_reference *tracking_ref = NULL; - git_buf remote_name = GIT_BUF_INIT; - int error = 0; - - assert(out && remote && ref); - - *out = NULL; - - if ((error = git_reference_resolve(&resolved_ref, ref)) < 0 || - (!git_reference_is_branch(resolved_ref)) || - (error = git_branch_tracking(&tracking_ref, resolved_ref)) < 0 || - (error = git_refspec_transform_l(&remote_name, &remote->fetch, git_reference_name(tracking_ref))) < 0) { - /* Not an error if HEAD is orphaned or no tracking branch */ - if (error == GIT_ENOTFOUND) - error = 0; - - goto cleanup; - } - - error = remote_head_for_fetchspec_src(out, update_heads, git_buf_cstr(&remote_name)); - -cleanup: - git_reference_free(tracking_ref); - git_reference_free(resolved_ref); - git_buf_free(&remote_name); - return error; -} - -static int git_remote_write_fetchhead(git_remote *remote, git_vector *update_heads) -{ - struct git_refspec *spec; - git_reference *head_ref = NULL; - git_fetchhead_ref *fetchhead_ref; - git_remote_head *remote_ref, *merge_remote_ref; - git_vector fetchhead_refs; - bool include_all_fetchheads; - unsigned int i = 0; - int error = 0; - - assert(remote); - - spec = &remote->fetch; - - if (git_vector_init(&fetchhead_refs, update_heads->length, git_fetchhead_ref_cmp) < 0) - return -1; - - /* Iff refspec is * (but not subdir slash star), include tags */ - include_all_fetchheads = (strcmp(GIT_REFS_HEADS_DIR "*", git_refspec_src(spec)) == 0); - - /* Determine what to merge: if refspec was a wildcard, just use HEAD */ - if (git_refspec_is_wildcard(spec)) { - if ((error = git_reference_lookup(&head_ref, remote->repo, GIT_HEAD_FILE)) < 0 || - (error = remote_head_for_ref(&merge_remote_ref, remote, update_heads, head_ref)) < 0) - goto cleanup; - } else { - /* If we're fetching a single refspec, that's the only thing that should be in FETCH_HEAD. */ - if ((error = remote_head_for_fetchspec_src(&merge_remote_ref, update_heads, git_refspec_src(spec))) < 0) - goto cleanup; - } - - /* Create the FETCH_HEAD file */ - git_vector_foreach(update_heads, i, remote_ref) { - int merge_this_fetchhead = (merge_remote_ref == remote_ref); - - if (!include_all_fetchheads && - !git_refspec_src_matches(spec, remote_ref->name) && - !merge_this_fetchhead) - continue; - - if (git_fetchhead_ref_create(&fetchhead_ref, - &remote_ref->oid, - merge_this_fetchhead, - remote_ref->name, - git_remote_url(remote)) < 0) - goto cleanup; - - if (git_vector_insert(&fetchhead_refs, fetchhead_ref) < 0) - goto cleanup; - } - - git_fetchhead_write(remote->repo, &fetchhead_refs); - -cleanup: - for (i = 0; i < fetchhead_refs.length; ++i) - git_fetchhead_ref_free(fetchhead_refs.contents[i]); - - git_vector_free(&fetchhead_refs); - git_reference_free(head_ref); - - return error; -} - -int git_remote_update_tips(git_remote *remote) -{ - int error = 0, autotag; - unsigned int i = 0; - git_buf refname = GIT_BUF_INIT; - git_oid old; - git_odb *odb; - git_remote_head *head; - git_reference *ref; - struct git_refspec *spec; - git_refspec tagspec; - git_vector refs, update_heads; - - assert(remote); - - spec = &remote->fetch; - - if (git_repository_odb__weakptr(&odb, remote->repo) < 0) - return -1; - - if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0) - return -1; - - /* Make a copy of the transport's refs */ - if (git_vector_init(&refs, 16, NULL) < 0 || - git_vector_init(&update_heads, 16, NULL) < 0) - return -1; - - if (git_remote_ls(remote, update_tips_callback, &refs) < 0) - goto on_error; - - /* Let's go find HEAD, if it exists. Check only the first ref in the vector. */ - if (refs.length > 0) { - head = (git_remote_head *)refs.contents[0]; - - if (!strcmp(head->name, GIT_HEAD_FILE)) { - if (git_reference_create(&ref, remote->repo, GIT_FETCH_HEAD_FILE, &head->oid, 1) < 0) - goto on_error; - - i = 1; - git_reference_free(ref); - } - } - - for (; i < refs.length; ++i) { - head = (git_remote_head *)refs.contents[i]; - autotag = 0; - - /* Ignore malformed ref names (which also saves us from tag^{} */ - if (!git_reference_is_valid_name(head->name)) - continue; - - if (git_refspec_src_matches(spec, head->name)) { - if (git_refspec_transform_r(&refname, spec, head->name) < 0) - goto on_error; - } else if (remote->download_tags != GIT_REMOTE_DOWNLOAD_TAGS_NONE) { - - if (remote->download_tags != GIT_REMOTE_DOWNLOAD_TAGS_ALL) - autotag = 1; - - if (!git_refspec_src_matches(&tagspec, head->name)) - continue; - - git_buf_clear(&refname); - if (git_buf_puts(&refname, head->name) < 0) - goto on_error; - } else { - continue; - } - - if (autotag && !git_odb_exists(odb, &head->oid)) - continue; - - if (git_vector_insert(&update_heads, head) < 0) - goto on_error; - - error = git_reference_name_to_id(&old, remote->repo, refname.ptr); - if (error < 0 && error != GIT_ENOTFOUND) - goto on_error; - - if (error == GIT_ENOTFOUND) - memset(&old, 0, GIT_OID_RAWSZ); - - if (!git_oid_cmp(&old, &head->oid)) - continue; - - /* In autotag mode, don't overwrite any locally-existing tags */ - error = git_reference_create(&ref, remote->repo, refname.ptr, &head->oid, !autotag); - if (error < 0 && error != GIT_EEXISTS) - goto on_error; - - git_reference_free(ref); - - if (remote->callbacks.update_tips != NULL) { - if (remote->callbacks.update_tips(refname.ptr, &old, &head->oid, remote->callbacks.payload) < 0) - goto on_error; - } - } - - if (git_remote_update_fetchhead(remote) && - (error = git_remote_write_fetchhead(remote, &update_heads)) < 0) - goto on_error; - - git_vector_free(&refs); - git_vector_free(&update_heads); - git_refspec__free(&tagspec); - git_buf_free(&refname); - return 0; - -on_error: - git_vector_free(&refs); - git_vector_free(&update_heads); - git_refspec__free(&tagspec); - git_buf_free(&refname); - return -1; - -} - -int git_remote_connected(git_remote *remote) -{ - assert(remote); - - if (!remote->transport || !remote->transport->is_connected) - return 0; - - /* Ask the transport if it's connected. */ - return remote->transport->is_connected(remote->transport); -} - -void git_remote_stop(git_remote *remote) -{ - assert(remote); - - if (remote->transport && remote->transport->cancel) - remote->transport->cancel(remote->transport); -} - -void git_remote_disconnect(git_remote *remote) -{ - assert(remote); - - if (git_remote_connected(remote)) - remote->transport->close(remote->transport); -} - -void git_remote_free(git_remote *remote) -{ - if (remote == NULL) - return; - - if (remote->transport != NULL) { - git_remote_disconnect(remote); - - remote->transport->free(remote->transport); - remote->transport = NULL; - } - - git_vector_free(&remote->refs); - - git_refspec__free(&remote->fetch); - git_refspec__free(&remote->push); - git__free(remote->url); - git__free(remote->pushurl); - git__free(remote->name); - git__free(remote); -} - -struct cb_data { - git_vector *list; - regex_t *preg; -}; - -static int remote_list_cb(const git_config_entry *entry, void *data_) -{ - struct cb_data *data = (struct cb_data *)data_; - size_t nmatch = 2; - regmatch_t pmatch[2]; - const char *name = entry->name; - - if (!regexec(data->preg, name, nmatch, pmatch, 0)) { - char *remote_name = git__strndup(&name[pmatch[1].rm_so], pmatch[1].rm_eo - pmatch[1].rm_so); - GITERR_CHECK_ALLOC(remote_name); - - if (git_vector_insert(data->list, remote_name) < 0) - return -1; - } - - return 0; -} - -int git_remote_list(git_strarray *remotes_list, git_repository *repo) -{ - git_config *cfg; - git_vector list; - regex_t preg; - struct cb_data data; - int error; - - if (git_repository_config__weakptr(&cfg, repo) < 0) - return -1; - - if (git_vector_init(&list, 4, NULL) < 0) - return -1; - - if (regcomp(&preg, "^remote\\.(.*)\\.url$", REG_EXTENDED) < 0) { - giterr_set(GITERR_OS, "Remote catch regex failed to compile"); - return -1; - } - - data.list = &list; - data.preg = &preg; - error = git_config_foreach(cfg, remote_list_cb, &data); - regfree(&preg); - if (error < 0) { - size_t i; - char *elem; - git_vector_foreach(&list, i, elem) { - git__free(elem); - } - - git_vector_free(&list); - - /* cb error is converted to GIT_EUSER by git_config_foreach */ - if (error == GIT_EUSER) - error = -1; - - return error; - } - - remotes_list->strings = (char **)list.contents; - remotes_list->count = list.length; - - return 0; -} - -void git_remote_check_cert(git_remote *remote, int check) -{ - assert(remote); - - remote->check_cert = check; -} - -int git_remote_set_callbacks(git_remote *remote, git_remote_callbacks *callbacks) -{ - assert(remote && callbacks); - - GITERR_CHECK_VERSION(callbacks, GIT_REMOTE_CALLBACKS_VERSION, "git_remote_callbacks"); - - memcpy(&remote->callbacks, callbacks, sizeof(git_remote_callbacks)); - - if (remote->transport && remote->transport->set_callbacks) - remote->transport->set_callbacks(remote->transport, - remote->callbacks.progress, - NULL, - remote->callbacks.payload); - - return 0; -} - -void git_remote_set_cred_acquire_cb( - git_remote *remote, - git_cred_acquire_cb cred_acquire_cb, - void *payload) -{ - assert(remote); - - remote->cred_acquire_cb = cred_acquire_cb; - remote->cred_acquire_payload = payload; -} - -int git_remote_set_transport(git_remote *remote, git_transport *transport) -{ - assert(remote && transport); - - GITERR_CHECK_VERSION(transport, GIT_TRANSPORT_VERSION, "git_transport"); - - if (remote->transport) { - giterr_set(GITERR_NET, "A transport is already bound to this remote"); - return -1; - } - - remote->transport = transport; - return 0; -} - -const git_transfer_progress* git_remote_stats(git_remote *remote) -{ - assert(remote); - return &remote->stats; -} - -git_remote_autotag_option_t git_remote_autotag(git_remote *remote) -{ - return remote->download_tags; -} - -void git_remote_set_autotag(git_remote *remote, git_remote_autotag_option_t value) -{ - remote->download_tags = value; -} - -static int rename_remote_config_section( - git_repository *repo, - const char *old_name, - const char *new_name) -{ - git_buf old_section_name = GIT_BUF_INIT, - new_section_name = GIT_BUF_INIT; - int error = -1; - - if (git_buf_printf(&old_section_name, "remote.%s", old_name) < 0) - goto cleanup; - - if (git_buf_printf(&new_section_name, "remote.%s", new_name) < 0) - goto cleanup; - - error = git_config_rename_section( - repo, - git_buf_cstr(&old_section_name), - git_buf_cstr(&new_section_name)); - -cleanup: - git_buf_free(&old_section_name); - git_buf_free(&new_section_name); - - return error; -} - -struct update_data -{ - git_config *config; - const char *old_remote_name; - const char *new_remote_name; -}; - -static int update_config_entries_cb( - const git_config_entry *entry, - void *payload) -{ - struct update_data *data = (struct update_data *)payload; - - if (strcmp(entry->value, data->old_remote_name)) - return 0; - - return git_config_set_string( - data->config, - entry->name, - data->new_remote_name); -} - -static int update_branch_remote_config_entry( - git_repository *repo, - const char *old_name, - const char *new_name) -{ - git_config *config; - struct update_data data; - - if (git_repository_config__weakptr(&config, repo) < 0) - return -1; - - data.config = config; - data.old_remote_name = old_name; - data.new_remote_name = new_name; - - return git_config_foreach_match( - config, - "branch\\..+\\.remote", - update_config_entries_cb, &data); -} - -static int rename_cb(const char *ref, void *data) -{ - if (git__prefixcmp(ref, GIT_REFS_REMOTES_DIR)) - return 0; - - return git_vector_insert((git_vector *)data, git__strdup(ref)); -} - -static int rename_one_remote_reference( - git_repository *repo, - const char *reference_name, - const char *old_remote_name, - const char *new_remote_name) -{ - int error = -1; - git_buf new_name = GIT_BUF_INIT; - git_reference *reference = NULL; - - if (git_buf_printf( - &new_name, - GIT_REFS_REMOTES_DIR "%s%s", - new_remote_name, - reference_name + strlen(GIT_REFS_REMOTES_DIR) + strlen(old_remote_name)) < 0) - return -1; - - if (git_reference_lookup(&reference, repo, reference_name) < 0) - goto cleanup; - - error = git_reference_rename(reference, git_buf_cstr(&new_name), 0); - -cleanup: - git_reference_free(reference); - git_buf_free(&new_name); - return error; -} - -static int rename_remote_references( - git_repository *repo, - const char *old_name, - const char *new_name) -{ - git_vector refnames; - int error = -1; - unsigned int i; - char *name; - - if (git_vector_init(&refnames, 8, NULL) < 0) - goto cleanup; - - if (git_reference_foreach( - repo, - GIT_REF_LISTALL, - rename_cb, - &refnames) < 0) - goto cleanup; - - git_vector_foreach(&refnames, i, name) { - if ((error = rename_one_remote_reference(repo, name, old_name, new_name)) < 0) - goto cleanup; - } - - error = 0; -cleanup: - git_vector_foreach(&refnames, i, name) { - git__free(name); - } - - git_vector_free(&refnames); - return error; -} - -static int rename_fetch_refspecs( - git_remote *remote, - const char *new_name, - int (*callback)(const char *problematic_refspec, void *payload), - void *payload) -{ - git_config *config; - const git_refspec *fetch_refspec; - git_buf dst_prefix = GIT_BUF_INIT, serialized = GIT_BUF_INIT; - const char* pos; - int error = -1; - - fetch_refspec = git_remote_fetchspec(remote); - - /* Is there a refspec to deal with? */ - if (fetch_refspec->src == NULL && - fetch_refspec->dst == NULL) - return 0; - - if (git_refspec__serialize(&serialized, fetch_refspec) < 0) - goto cleanup; - - /* Is it an in-memory remote? */ - if (remote->name == '\0') { - error = (callback(git_buf_cstr(&serialized), payload) < 0) ? GIT_EUSER : 0; - goto cleanup; - } - - if (git_buf_printf(&dst_prefix, ":refs/remotes/%s/", remote->name) < 0) - goto cleanup; - - pos = strstr(git_buf_cstr(&serialized), git_buf_cstr(&dst_prefix)); - - /* Does the dst part of the refspec follow the extected standard format? */ - if (!pos) { - error = (callback(git_buf_cstr(&serialized), payload) < 0) ? GIT_EUSER : 0; - goto cleanup; - } - - if (git_buf_splice( - &serialized, - pos - git_buf_cstr(&serialized) + strlen(":refs/remotes/"), - strlen(remote->name), new_name, - strlen(new_name)) < 0) - goto cleanup; - - git_refspec__free(&remote->fetch); - - if (git_refspec__parse(&remote->fetch, git_buf_cstr(&serialized), true) < 0) - goto cleanup; - - if (git_repository_config__weakptr(&config, remote->repo) < 0) - goto cleanup; - - error = update_config_refspec(config, new_name, &remote->fetch, GIT_DIRECTION_FETCH); - -cleanup: - git_buf_free(&serialized); - git_buf_free(&dst_prefix); - return error; -} - -int git_remote_rename( - git_remote *remote, - const char *new_name, - git_remote_rename_problem_cb callback, - void *payload) -{ - int error; - - assert(remote && new_name); - - if (!remote->name) { - giterr_set(GITERR_INVALID, "Can't rename an in-memory remote."); - return GIT_EINVALIDSPEC; - } - - if ((error = ensure_remote_name_is_valid(new_name)) < 0) - return error; - - if (remote->repo) { - if ((error = ensure_remote_doesnot_exist(remote->repo, new_name)) < 0) - return error; - - if (!remote->name) { - if ((error = rename_fetch_refspecs( - remote, - new_name, - callback, - payload)) < 0) - return error; - - remote->name = git__strdup(new_name); - - if (!remote->name) return 0; - return git_remote_save(remote); - } - - if ((error = rename_remote_config_section( - remote->repo, - remote->name, - new_name)) < 0) - return error; - - if ((error = update_branch_remote_config_entry( - remote->repo, - remote->name, - new_name)) < 0) - return error; - - if ((error = rename_remote_references( - remote->repo, - remote->name, - new_name)) < 0) - return error; - - if ((error = rename_fetch_refspecs( - remote, - new_name, - callback, - payload)) < 0) - return error; - } - - git__free(remote->name); - remote->name = git__strdup(new_name); - - return 0; -} - -int git_remote_update_fetchhead(git_remote *remote) -{ - return remote->update_fetchhead; -} - -void git_remote_set_update_fetchhead(git_remote *remote, int value) -{ - remote->update_fetchhead = value; -} diff --git a/src/remote.h b/src/remote.h deleted file mode 100644 index 4c1a18aa7f8..00000000000 --- a/src/remote.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_remote_h__ -#define INCLUDE_remote_h__ - -#include "git2/remote.h" -#include "git2/transport.h" - -#include "refspec.h" -#include "repository.h" - -#define GIT_REMOTE_ORIGIN "origin" - -struct git_remote { - char *name; - char *url; - char *pushurl; - git_vector refs; - struct git_refspec fetch; - struct git_refspec push; - git_cred_acquire_cb cred_acquire_cb; - void *cred_acquire_payload; - git_transport *transport; - git_repository *repo; - git_remote_callbacks callbacks; - git_transfer_progress stats; - unsigned int need_pack; - git_remote_autotag_option_t download_tags; - unsigned int check_cert; - unsigned int update_fetchhead; -}; - -const char* git_remote__urlfordirection(struct git_remote *remote, int direction); -int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, char **proxy_url); - -#endif diff --git a/src/repository.c b/src/repository.c deleted file mode 100644 index 014b40aff4a..00000000000 --- a/src/repository.c +++ /dev/null @@ -1,1641 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include -#include - -#include "git2/object.h" - -#include "common.h" -#include "repository.h" -#include "commit.h" -#include "tag.h" -#include "blob.h" -#include "fileops.h" -#include "config.h" -#include "refs.h" -#include "filter.h" -#include "odb.h" -#include "remote.h" -#include "merge.h" - -#define GIT_FILE_CONTENT_PREFIX "gitdir:" - -#define GIT_BRANCH_MASTER "master" - -#define GIT_REPO_VERSION 0 - -#define GIT_TEMPLATE_DIR "/usr/share/git-core/templates" - -static void drop_odb(git_repository *repo) -{ - if (repo->_odb != NULL) { - GIT_REFCOUNT_OWN(repo->_odb, NULL); - git_odb_free(repo->_odb); - repo->_odb = NULL; - } -} - -static void drop_config(git_repository *repo) -{ - if (repo->_config != NULL) { - GIT_REFCOUNT_OWN(repo->_config, NULL); - git_config_free(repo->_config); - repo->_config = NULL; - } - - git_repository__cvar_cache_clear(repo); -} - -static void drop_index(git_repository *repo) -{ - if (repo->_index != NULL) { - GIT_REFCOUNT_OWN(repo->_index, NULL); - git_index_free(repo->_index); - repo->_index = NULL; - } -} - -void git_repository_free(git_repository *repo) -{ - if (repo == NULL) - return; - - git_cache_free(&repo->objects); - git_repository__refcache_free(&repo->references); - git_attr_cache_flush(repo); - git_submodule_config_free(repo); - - git__free(repo->path_repository); - git__free(repo->workdir); - - drop_config(repo); - drop_index(repo); - drop_odb(repo); - - git__free(repo); -} - -/* - * Git repository open methods - * - * Open a repository object from its path - */ -static bool valid_repository_path(git_buf *repository_path) -{ - /* Check OBJECTS_DIR first, since it will generate the longest path name */ - if (git_path_contains_dir(repository_path, GIT_OBJECTS_DIR) == false) - return false; - - /* Ensure HEAD file exists */ - if (git_path_contains_file(repository_path, GIT_HEAD_FILE) == false) - return false; - - if (git_path_contains_dir(repository_path, GIT_REFS_DIR) == false) - return false; - - return true; -} - -static git_repository *repository_alloc(void) -{ - git_repository *repo = git__malloc(sizeof(git_repository)); - if (!repo) - return NULL; - - memset(repo, 0x0, sizeof(git_repository)); - - if (git_cache_init(&repo->objects, GIT_DEFAULT_CACHE_SIZE, &git_object__free) < 0) { - git__free(repo); - return NULL; - } - - /* set all the entries in the cvar cache to `unset` */ - git_repository__cvar_cache_clear(repo); - - return repo; -} - -static int load_config_data(git_repository *repo) -{ - int is_bare; - git_config *config; - - if (git_repository_config__weakptr(&config, repo) < 0) - return -1; - - /* Try to figure out if it's bare, default to non-bare if it's not set */ - if (git_config_get_bool(&is_bare, config, "core.bare") < 0) - repo->is_bare = 0; - else - repo->is_bare = is_bare; - - return 0; -} - -static int load_workdir(git_repository *repo, git_buf *parent_path) -{ - int error; - git_config *config; - const char *worktree; - git_buf worktree_buf = GIT_BUF_INIT; - - if (repo->is_bare) - return 0; - - if (git_repository_config__weakptr(&config, repo) < 0) - return -1; - - error = git_config_get_string(&worktree, config, "core.worktree"); - if (!error && worktree != NULL) { - error = git_path_prettify_dir( - &worktree_buf, worktree, repo->path_repository); - if (error < 0) - return error; - repo->workdir = git_buf_detach(&worktree_buf); - } - else if (error != GIT_ENOTFOUND) - return error; - else { - giterr_clear(); - - if (parent_path && git_path_isdir(parent_path->ptr)) - repo->workdir = git_buf_detach(parent_path); - else { - git_path_dirname_r(&worktree_buf, repo->path_repository); - git_path_to_dir(&worktree_buf); - repo->workdir = git_buf_detach(&worktree_buf); - } - } - - GITERR_CHECK_ALLOC(repo->workdir); - - return 0; -} - -/* - * This function returns furthest offset into path where a ceiling dir - * is found, so we can stop processing the path at that point. - * - * Note: converting this to use git_bufs instead of GIT_PATH_MAX buffers on - * the stack could remove directories name limits, but at the cost of doing - * repeated malloc/frees inside the loop below, so let's not do it now. - */ -static int find_ceiling_dir_offset( - const char *path, - const char *ceiling_directories) -{ - char buf[GIT_PATH_MAX + 1]; - char buf2[GIT_PATH_MAX + 1]; - const char *ceil, *sep; - size_t len, max_len = 0, min_len; - - assert(path); - - min_len = (size_t)(git_path_root(path) + 1); - - if (ceiling_directories == NULL || min_len == 0) - return (int)min_len; - - for (sep = ceil = ceiling_directories; *sep; ceil = sep + 1) { - for (sep = ceil; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++); - len = sep - ceil; - - if (len == 0 || len >= sizeof(buf) || git_path_root(ceil) == -1) - continue; - - strncpy(buf, ceil, len); - buf[len] = '\0'; - - if (p_realpath(buf, buf2) == NULL) - continue; - - len = strlen(buf2); - if (len > 0 && buf2[len-1] == '/') - buf[--len] = '\0'; - - if (!strncmp(path, buf2, len) && - path[len] == '/' && - len > max_len) - { - max_len = len; - } - } - - return (int)(max_len <= min_len ? min_len : max_len); -} - -/* - * Read the contents of `file_path` and set `path_out` to the repo dir that - * it points to. Before calling, set `path_out` to the base directory that - * should be used if the contents of `file_path` are a relative path. - */ -static int read_gitfile(git_buf *path_out, const char *file_path) -{ - int error = 0; - git_buf file = GIT_BUF_INIT; - size_t prefix_len = strlen(GIT_FILE_CONTENT_PREFIX); - - assert(path_out && file_path); - - if (git_futils_readbuffer(&file, file_path) < 0) - return -1; - - git_buf_rtrim(&file); - - if (git_buf_len(&file) <= prefix_len || - memcmp(git_buf_cstr(&file), GIT_FILE_CONTENT_PREFIX, prefix_len) != 0) - { - giterr_set(GITERR_REPOSITORY, "The `.git` file at '%s' is malformed", file_path); - error = -1; - } - else if ((error = git_path_dirname_r(path_out, file_path)) >= 0) { - const char *gitlink = git_buf_cstr(&file) + prefix_len; - while (*gitlink && git__isspace(*gitlink)) gitlink++; - error = git_path_prettify_dir( - path_out, gitlink, git_buf_cstr(path_out)); - } - - git_buf_free(&file); - return error; -} - -static int find_repo( - git_buf *repo_path, - git_buf *parent_path, - const char *start_path, - uint32_t flags, - const char *ceiling_dirs) -{ - int error; - git_buf path = GIT_BUF_INIT; - struct stat st; - dev_t initial_device = 0; - bool try_with_dot_git = false; - int ceiling_offset; - - git_buf_free(repo_path); - - if ((error = git_path_prettify_dir(&path, start_path, NULL)) < 0) - return error; - - ceiling_offset = find_ceiling_dir_offset(path.ptr, ceiling_dirs); - - if ((error = git_buf_joinpath(&path, path.ptr, DOT_GIT)) < 0) - return error; - - while (!error && !git_buf_len(repo_path)) { - if (p_stat(path.ptr, &st) == 0) { - /* check that we have not crossed device boundaries */ - if (initial_device == 0) - initial_device = st.st_dev; - else if (st.st_dev != initial_device && - (flags & GIT_REPOSITORY_OPEN_CROSS_FS) == 0) - break; - - if (S_ISDIR(st.st_mode)) { - if (valid_repository_path(&path)) { - git_path_to_dir(&path); - git_buf_set(repo_path, path.ptr, path.size); - break; - } - } - else if (S_ISREG(st.st_mode)) { - git_buf repo_link = GIT_BUF_INIT; - - if (!(error = read_gitfile(&repo_link, path.ptr))) { - if (valid_repository_path(&repo_link)) - git_buf_swap(repo_path, &repo_link); - - git_buf_free(&repo_link); - break; - } - git_buf_free(&repo_link); - } - } - - /* move up one directory level */ - if (git_path_dirname_r(&path, path.ptr) < 0) { - error = -1; - break; - } - - if (try_with_dot_git) { - /* if we tried original dir with and without .git AND either hit - * directory ceiling or NO_SEARCH was requested, then be done. - */ - if (path.ptr[ceiling_offset] == '\0' || - (flags & GIT_REPOSITORY_OPEN_NO_SEARCH) != 0) - break; - /* otherwise look first for .git item */ - error = git_buf_joinpath(&path, path.ptr, DOT_GIT); - } - try_with_dot_git = !try_with_dot_git; - } - - if (!error && parent_path != NULL) { - if (!git_buf_len(repo_path)) - git_buf_clear(parent_path); - else { - git_path_dirname_r(parent_path, path.ptr); - git_path_to_dir(parent_path); - } - if (git_buf_oom(parent_path)) - return -1; - } - - git_buf_free(&path); - - if (!git_buf_len(repo_path) && !error) { - giterr_set(GITERR_REPOSITORY, - "Could not find repository from '%s'", start_path); - error = GIT_ENOTFOUND; - } - - return error; -} - -int git_repository_open_ext( - git_repository **repo_ptr, - const char *start_path, - unsigned int flags, - const char *ceiling_dirs) -{ - int error; - git_buf path = GIT_BUF_INIT, parent = GIT_BUF_INIT; - git_repository *repo; - - if (repo_ptr) - *repo_ptr = NULL; - - error = find_repo(&path, &parent, start_path, flags, ceiling_dirs); - if (error < 0 || !repo_ptr) - return error; - - repo = repository_alloc(); - GITERR_CHECK_ALLOC(repo); - - repo->path_repository = git_buf_detach(&path); - GITERR_CHECK_ALLOC(repo->path_repository); - - if ((error = load_config_data(repo)) < 0 || - (error = load_workdir(repo, &parent)) < 0) - { - git_repository_free(repo); - return error; - } - - git_buf_free(&parent); - *repo_ptr = repo; - return 0; -} - -int git_repository_open(git_repository **repo_out, const char *path) -{ - return git_repository_open_ext( - repo_out, path, GIT_REPOSITORY_OPEN_NO_SEARCH, NULL); -} - -int git_repository_wrap_odb(git_repository **repo_out, git_odb *odb) -{ - git_repository *repo; - - repo = repository_alloc(); - GITERR_CHECK_ALLOC(repo); - - git_repository_set_odb(repo, odb); - *repo_out = repo; - - return 0; -} - -int git_repository_discover( - char *repository_path, - size_t size, - const char *start_path, - int across_fs, - const char *ceiling_dirs) -{ - git_buf path = GIT_BUF_INIT; - uint32_t flags = across_fs ? GIT_REPOSITORY_OPEN_CROSS_FS : 0; - int error; - - assert(start_path && repository_path && size > 0); - - *repository_path = '\0'; - - if ((error = find_repo(&path, NULL, start_path, flags, ceiling_dirs)) < 0) - return error != GIT_ENOTFOUND ? -1 : error; - - if (size < (size_t)(path.size + 1)) { - giterr_set(GITERR_REPOSITORY, - "The given buffer is too small to store the discovered path"); - git_buf_free(&path); - return -1; - } - - /* success: we discovered a repository */ - git_buf_copy_cstr(repository_path, size, &path); - git_buf_free(&path); - return 0; -} - -static int load_config( - git_config **out, - git_repository *repo, - const char *global_config_path, - const char *xdg_config_path, - const char *system_config_path) -{ - int error; - git_buf config_path = GIT_BUF_INIT; - git_config *cfg = NULL; - - assert(repo && out); - - if ((error = git_config_new(&cfg)) < 0) - return error; - - error = git_buf_joinpath( - &config_path, repo->path_repository, GIT_CONFIG_FILENAME_INREPO); - if (error < 0) - goto on_error; - - if ((error = git_config_add_file_ondisk( - cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, 0)) < 0 && - error != GIT_ENOTFOUND) - goto on_error; - - git_buf_free(&config_path); - - if (global_config_path != NULL && - (error = git_config_add_file_ondisk( - cfg, global_config_path, GIT_CONFIG_LEVEL_GLOBAL, 0)) < 0 && - error != GIT_ENOTFOUND) - goto on_error; - - if (xdg_config_path != NULL && - (error = git_config_add_file_ondisk( - cfg, xdg_config_path, GIT_CONFIG_LEVEL_XDG, 0)) < 0 && - error != GIT_ENOTFOUND) - goto on_error; - - if (system_config_path != NULL && - (error = git_config_add_file_ondisk( - cfg, system_config_path, GIT_CONFIG_LEVEL_SYSTEM, 0)) < 0 && - error != GIT_ENOTFOUND) - goto on_error; - - giterr_clear(); /* clear any lingering ENOTFOUND errors */ - - *out = cfg; - return 0; - -on_error: - git_buf_free(&config_path); - git_config_free(cfg); - *out = NULL; - return error; -} - -int git_repository_config__weakptr(git_config **out, git_repository *repo) -{ - if (repo->_config == NULL) { - git_buf global_buf = GIT_BUF_INIT, xdg_buf = GIT_BUF_INIT, system_buf = GIT_BUF_INIT; - int res; - - const char *global_config_path = NULL; - const char *xdg_config_path = NULL; - const char *system_config_path = NULL; - - if (git_config_find_global_r(&global_buf) == 0) - global_config_path = global_buf.ptr; - - if (git_config_find_xdg_r(&xdg_buf) == 0) - xdg_config_path = xdg_buf.ptr; - - if (git_config_find_system_r(&system_buf) == 0) - system_config_path = system_buf.ptr; - - res = load_config(&repo->_config, repo, global_config_path, xdg_config_path, system_config_path); - - git_buf_free(&global_buf); - git_buf_free(&xdg_buf); - git_buf_free(&system_buf); - - if (res < 0) - return -1; - - GIT_REFCOUNT_OWN(repo->_config, repo); - } - - *out = repo->_config; - return 0; -} - -int git_repository_config(git_config **out, git_repository *repo) -{ - if (git_repository_config__weakptr(out, repo) < 0) - return -1; - - GIT_REFCOUNT_INC(*out); - return 0; -} - -void git_repository_set_config(git_repository *repo, git_config *config) -{ - assert(repo && config); - - drop_config(repo); - - repo->_config = config; - GIT_REFCOUNT_OWN(repo->_config, repo); -} - -int git_repository_odb__weakptr(git_odb **out, git_repository *repo) -{ - assert(repo && out); - - if (repo->_odb == NULL) { - git_buf odb_path = GIT_BUF_INIT; - int res; - - if (git_buf_joinpath(&odb_path, repo->path_repository, GIT_OBJECTS_DIR) < 0) - return -1; - - res = git_odb_open(&repo->_odb, odb_path.ptr); - git_buf_free(&odb_path); /* done with path */ - - if (res < 0) - return -1; - - GIT_REFCOUNT_OWN(repo->_odb, repo); - } - - *out = repo->_odb; - return 0; -} - -int git_repository_odb(git_odb **out, git_repository *repo) -{ - if (git_repository_odb__weakptr(out, repo) < 0) - return -1; - - GIT_REFCOUNT_INC(*out); - return 0; -} - -void git_repository_set_odb(git_repository *repo, git_odb *odb) -{ - assert(repo && odb); - - drop_odb(repo); - - repo->_odb = odb; - GIT_REFCOUNT_OWN(repo->_odb, repo); - GIT_REFCOUNT_INC(odb); -} - -int git_repository_index__weakptr(git_index **out, git_repository *repo) -{ - assert(out && repo); - - if (repo->_index == NULL) { - int res; - git_buf index_path = GIT_BUF_INIT; - - if (git_buf_joinpath(&index_path, repo->path_repository, GIT_INDEX_FILE) < 0) - return -1; - - res = git_index_open(&repo->_index, index_path.ptr); - git_buf_free(&index_path); /* done with path */ - - if (res < 0) - return -1; - - GIT_REFCOUNT_OWN(repo->_index, repo); - - if (git_index_set_caps(repo->_index, GIT_INDEXCAP_FROM_OWNER) < 0) - return -1; - } - - *out = repo->_index; - return 0; -} - -int git_repository_index(git_index **out, git_repository *repo) -{ - if (git_repository_index__weakptr(out, repo) < 0) - return -1; - - GIT_REFCOUNT_INC(*out); - return 0; -} - -void git_repository_set_index(git_repository *repo, git_index *index) -{ - assert(repo && index); - - drop_index(repo); - - repo->_index = index; - GIT_REFCOUNT_OWN(repo->_index, repo); - GIT_REFCOUNT_INC(index); -} - -static int check_repositoryformatversion(git_config *config) -{ - int version; - - if (git_config_get_int32(&version, config, "core.repositoryformatversion") < 0) - return -1; - - if (GIT_REPO_VERSION < version) { - giterr_set(GITERR_REPOSITORY, - "Unsupported repository version %d. Only versions up to %d are supported.", - version, GIT_REPO_VERSION); - return -1; - } - - return 0; -} - -static int repo_init_create_head(const char *git_dir, const char *ref_name) -{ - git_buf ref_path = GIT_BUF_INIT; - git_filebuf ref = GIT_FILEBUF_INIT; - const char *fmt; - - if (git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE) < 0 || - git_filebuf_open(&ref, ref_path.ptr, 0) < 0) - goto fail; - - if (!ref_name) - ref_name = GIT_BRANCH_MASTER; - - if (git__prefixcmp(ref_name, GIT_REFS_DIR) == 0) - fmt = "ref: %s\n"; - else - fmt = "ref: " GIT_REFS_HEADS_DIR "%s\n"; - - if (git_filebuf_printf(&ref, fmt, ref_name) < 0 || - git_filebuf_commit(&ref, GIT_REFS_FILE_MODE) < 0) - goto fail; - - git_buf_free(&ref_path); - return 0; - -fail: - git_buf_free(&ref_path); - git_filebuf_cleanup(&ref); - return -1; -} - -static bool is_chmod_supported(const char *file_path) -{ - struct stat st1, st2; - static int _is_supported = -1; - - if (_is_supported > -1) - return _is_supported; - - if (p_stat(file_path, &st1) < 0) - return false; - - if (p_chmod(file_path, st1.st_mode ^ S_IXUSR) < 0) - return false; - - if (p_stat(file_path, &st2) < 0) - return false; - - _is_supported = (st1.st_mode != st2.st_mode); - - return _is_supported; -} - -static bool is_filesystem_case_insensitive(const char *gitdir_path) -{ - git_buf path = GIT_BUF_INIT; - static int _is_insensitive = -1; - - if (_is_insensitive > -1) - return _is_insensitive; - - if (git_buf_joinpath(&path, gitdir_path, "CoNfIg") < 0) - goto cleanup; - - _is_insensitive = git_path_exists(git_buf_cstr(&path)); - -cleanup: - git_buf_free(&path); - return _is_insensitive; -} - -static bool are_symlinks_supported(const char *wd_path) -{ - git_buf path = GIT_BUF_INIT; - int fd; - struct stat st; - static int _symlinks_supported = -1; - - if (_symlinks_supported > -1) - return _symlinks_supported; - - if ((fd = git_futils_mktmp(&path, wd_path)) < 0 || - p_close(fd) < 0 || - p_unlink(path.ptr) < 0 || - p_symlink("testing", path.ptr) < 0 || - p_lstat(path.ptr, &st) < 0) - _symlinks_supported = false; - else - _symlinks_supported = (S_ISLNK(st.st_mode) != 0); - - (void)p_unlink(path.ptr); - git_buf_free(&path); - - return _symlinks_supported; -} - -static int create_empty_file(const char *path, mode_t mode) -{ - int fd; - - if ((fd = p_creat(path, mode)) < 0) { - giterr_set(GITERR_OS, "Error while creating '%s'", path); - return -1; - } - - if (p_close(fd) < 0) { - giterr_set(GITERR_OS, "Error while closing '%s'", path); - return -1; - } - - return 0; -} - -static int repo_init_config( - const char *repo_dir, - const char *work_dir, - git_repository_init_options *opts) -{ - int error = 0; - git_buf cfg_path = GIT_BUF_INIT; - git_config *config = NULL; - -#define SET_REPO_CONFIG(TYPE, NAME, VAL) do {\ - if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \ - goto cleanup; } while (0) - - if (git_buf_joinpath(&cfg_path, repo_dir, GIT_CONFIG_FILENAME_INREPO) < 0) - return -1; - - if (!git_path_isfile(git_buf_cstr(&cfg_path)) && - create_empty_file(git_buf_cstr(&cfg_path), GIT_CONFIG_FILE_MODE) < 0) { - git_buf_free(&cfg_path); - return -1; - } - - if (git_config_open_ondisk(&config, git_buf_cstr(&cfg_path)) < 0) { - git_buf_free(&cfg_path); - return -1; - } - - if ((opts->flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0 && - (error = check_repositoryformatversion(config)) < 0) - goto cleanup; - - SET_REPO_CONFIG( - bool, "core.bare", (opts->flags & GIT_REPOSITORY_INIT_BARE) != 0); - SET_REPO_CONFIG( - int32, "core.repositoryformatversion", GIT_REPO_VERSION); - SET_REPO_CONFIG( - bool, "core.filemode", is_chmod_supported(git_buf_cstr(&cfg_path))); - - if (!(opts->flags & GIT_REPOSITORY_INIT_BARE)) { - SET_REPO_CONFIG(bool, "core.logallrefupdates", true); - - if (!are_symlinks_supported(work_dir)) - SET_REPO_CONFIG(bool, "core.symlinks", false); - - if (!(opts->flags & GIT_REPOSITORY_INIT__NATURAL_WD)) { - SET_REPO_CONFIG(string, "core.worktree", work_dir); - } - else if ((opts->flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0) { - if (git_config_delete_entry(config, "core.worktree") < 0) - giterr_clear(); - } - } else { - if (!are_symlinks_supported(repo_dir)) - SET_REPO_CONFIG(bool, "core.symlinks", false); - } - - if (!(opts->flags & GIT_REPOSITORY_INIT__IS_REINIT) && - is_filesystem_case_insensitive(repo_dir)) - SET_REPO_CONFIG(bool, "core.ignorecase", true); - - if (opts->mode == GIT_REPOSITORY_INIT_SHARED_GROUP) { - SET_REPO_CONFIG(int32, "core.sharedrepository", 1); - SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true); - } - else if (opts->mode == GIT_REPOSITORY_INIT_SHARED_ALL) { - SET_REPO_CONFIG(int32, "core.sharedrepository", 2); - SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true); - } - -cleanup: - git_buf_free(&cfg_path); - git_config_free(config); - - return error; -} - -static int repo_write_template( - const char *git_dir, - bool allow_overwrite, - const char *file, - mode_t mode, - bool hidden, - const char *content) -{ - git_buf path = GIT_BUF_INIT; - int fd, error = 0, flags; - - if (git_buf_joinpath(&path, git_dir, file) < 0) - return -1; - - if (allow_overwrite) - flags = O_WRONLY | O_CREAT | O_TRUNC; - else - flags = O_WRONLY | O_CREAT | O_EXCL; - - fd = p_open(git_buf_cstr(&path), flags, mode); - - if (fd >= 0) { - error = p_write(fd, content, strlen(content)); - - p_close(fd); - } - else if (errno != EEXIST) - error = fd; - -#ifdef GIT_WIN32 - if (!error && hidden) { - if (p_hide_directory__w32(path.ptr) < 0) - error = -1; - } -#else - GIT_UNUSED(hidden); -#endif - - git_buf_free(&path); - - if (error) - giterr_set(GITERR_OS, - "Failed to initialize repository with template '%s'", file); - - return error; -} - -static int repo_write_gitlink( - const char *in_dir, const char *to_repo) -{ - int error; - git_buf buf = GIT_BUF_INIT; - struct stat st; - - git_path_dirname_r(&buf, to_repo); - git_path_to_dir(&buf); - if (git_buf_oom(&buf)) - return -1; - - /* don't write gitlink to natural workdir */ - if (git__suffixcmp(to_repo, "/" DOT_GIT "/") == 0 && - strcmp(in_dir, buf.ptr) == 0) - { - error = GIT_PASSTHROUGH; - goto cleanup; - } - - if ((error = git_buf_joinpath(&buf, in_dir, DOT_GIT)) < 0) - goto cleanup; - - if (!p_stat(buf.ptr, &st) && !S_ISREG(st.st_mode)) { - giterr_set(GITERR_REPOSITORY, - "Cannot overwrite gitlink file into path '%s'", in_dir); - error = GIT_EEXISTS; - goto cleanup; - } - - git_buf_clear(&buf); - - error = git_buf_printf(&buf, "%s %s", GIT_FILE_CONTENT_PREFIX, to_repo); - - if (!error) - error = repo_write_template(in_dir, true, DOT_GIT, 0644, true, buf.ptr); - -cleanup: - git_buf_free(&buf); - return error; -} - -static mode_t pick_dir_mode(git_repository_init_options *opts) -{ - if (opts->mode == GIT_REPOSITORY_INIT_SHARED_UMASK) - return 0755; - if (opts->mode == GIT_REPOSITORY_INIT_SHARED_GROUP) - return (0775 | S_ISGID); - if (opts->mode == GIT_REPOSITORY_INIT_SHARED_ALL) - return (0777 | S_ISGID); - return opts->mode; -} - -#include "repo_template.h" - -static int repo_init_structure( - const char *repo_dir, - const char *work_dir, - git_repository_init_options *opts) -{ - int error = 0; - repo_template_item *tpl; - bool external_tpl = - ((opts->flags & GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE) != 0); - mode_t dmode = pick_dir_mode(opts); - - /* Hide the ".git" directory */ -#ifdef GIT_WIN32 - if ((opts->flags & GIT_REPOSITORY_INIT__HAS_DOTGIT) != 0) { - if (p_hide_directory__w32(repo_dir) < 0) { - giterr_set(GITERR_REPOSITORY, - "Failed to mark Git repository folder as hidden"); - return -1; - } - } -#endif - - /* Create the .git gitlink if appropriate */ - if ((opts->flags & GIT_REPOSITORY_INIT_BARE) == 0 && - (opts->flags & GIT_REPOSITORY_INIT__NATURAL_WD) == 0) - { - if (repo_write_gitlink(work_dir, repo_dir) < 0) - return -1; - } - - /* Copy external template if requested */ - if (external_tpl) { - git_config *cfg; - const char *tdir; - - if (opts->template_path) - tdir = opts->template_path; - else if ((error = git_config_open_default(&cfg)) < 0) - return error; - else { - error = git_config_get_string(&tdir, cfg, "init.templatedir"); - - git_config_free(cfg); - - if (error && error != GIT_ENOTFOUND) - return error; - - giterr_clear(); - tdir = GIT_TEMPLATE_DIR; - } - - /* FIXME: GIT_CPDIR_CHMOD cannot applied here as an attempt - * would be made to chmod() all directories up to the last - * component of repo_dir, e.g., also /home etc. Recall that - * repo_dir is prettified at this point. - * - * Best probably would be to have the same logic as in - * git_futils_mkdir(), i.e., to separate the base from - * the path. - */ - error = git_futils_cp_r(tdir, repo_dir, - GIT_CPDIR_COPY_SYMLINKS /*| GIT_CPDIR_CHMOD*/, dmode); - - if (error < 0) { - if (strcmp(tdir, GIT_TEMPLATE_DIR) != 0) - return error; - - /* if template was default, ignore error and use internal */ - giterr_clear(); - external_tpl = false; - error = 0; - } - } - - /* Copy internal template - * - always ensure existence of dirs - * - only create files if no external template was specified - */ - for (tpl = repo_template; !error && tpl->path; ++tpl) { - if (!tpl->content) - error = git_futils_mkdir( - tpl->path, repo_dir, dmode, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD); - else if (!external_tpl) { - const char *content = tpl->content; - - if (opts->description && strcmp(tpl->path, GIT_DESC_FILE) == 0) - content = opts->description; - - error = repo_write_template( - repo_dir, false, tpl->path, tpl->mode, false, content); - } - } - - return error; -} - -static int repo_init_directories( - git_buf *repo_path, - git_buf *wd_path, - const char *given_repo, - git_repository_init_options *opts) -{ - int error = 0; - bool add_dotgit, has_dotgit, natural_wd; - mode_t dirmode; - - /* set up repo path */ - - add_dotgit = - (opts->flags & GIT_REPOSITORY_INIT_NO_DOTGIT_DIR) == 0 && - (opts->flags & GIT_REPOSITORY_INIT_BARE) == 0 && - git__suffixcmp(given_repo, "/" DOT_GIT) != 0 && - git__suffixcmp(given_repo, "/" GIT_DIR) != 0; - - if (git_buf_joinpath(repo_path, given_repo, add_dotgit ? GIT_DIR : "") < 0) - return -1; - - has_dotgit = (git__suffixcmp(repo_path->ptr, "/" GIT_DIR) == 0); - if (has_dotgit) - opts->flags |= GIT_REPOSITORY_INIT__HAS_DOTGIT; - - /* set up workdir path */ - - if ((opts->flags & GIT_REPOSITORY_INIT_BARE) == 0) { - if (opts->workdir_path) { - if (git_path_join_unrooted( - wd_path, opts->workdir_path, repo_path->ptr, NULL) < 0) - return -1; - } else if (has_dotgit) { - if (git_path_dirname_r(wd_path, repo_path->ptr) < 0) - return -1; - } else { - giterr_set(GITERR_REPOSITORY, "Cannot pick working directory" - " for non-bare repository that isn't a '.git' directory"); - return -1; - } - - if (git_path_to_dir(wd_path) < 0) - return -1; - } else { - git_buf_clear(wd_path); - } - - natural_wd = - has_dotgit && - wd_path->size > 0 && - wd_path->size + strlen(GIT_DIR) == repo_path->size && - memcmp(repo_path->ptr, wd_path->ptr, wd_path->size) == 0; - if (natural_wd) - opts->flags |= GIT_REPOSITORY_INIT__NATURAL_WD; - - /* create directories as needed / requested */ - - dirmode = pick_dir_mode(opts); - - if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 && has_dotgit) { - git_buf p = GIT_BUF_INIT; - if ((error = git_path_dirname_r(&p, repo_path->ptr)) >= 0) - error = git_futils_mkdir(p.ptr, NULL, dirmode, 0); - git_buf_free(&p); - } - - if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 || - (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0 || - has_dotgit) - { - uint32_t mkflag = GIT_MKDIR_CHMOD; - if ((opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0) - mkflag |= GIT_MKDIR_PATH; - error = git_futils_mkdir(repo_path->ptr, NULL, dirmode, mkflag); - } - - if (wd_path->size > 0 && - !natural_wd && - ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 || - (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0)) - error = git_futils_mkdir(wd_path->ptr, NULL, dirmode & ~S_ISGID, - (opts->flags & GIT_REPOSITORY_INIT_MKPATH) ? GIT_MKDIR_PATH : 0); - - /* prettify both directories now that they are created */ - - if (!error) { - error = git_path_prettify_dir(repo_path, repo_path->ptr, NULL); - - if (!error && wd_path->size > 0) - error = git_path_prettify_dir(wd_path, wd_path->ptr, NULL); - } - - return error; -} - -static int repo_init_create_origin(git_repository *repo, const char *url) -{ - int error; - git_remote *remote; - - if (!(error = git_remote_create(&remote, repo, GIT_REMOTE_ORIGIN, url))) { - git_remote_free(remote); - } - - return error; -} - -int git_repository_init( - git_repository **repo_out, const char *path, unsigned is_bare) -{ - git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; - - opts.flags = GIT_REPOSITORY_INIT_MKPATH; /* don't love this default */ - if (is_bare) - opts.flags |= GIT_REPOSITORY_INIT_BARE; - - return git_repository_init_ext(repo_out, path, &opts); -} - -int git_repository_init_ext( - git_repository **out, - const char *given_repo, - git_repository_init_options *opts) -{ - int error; - git_buf repo_path = GIT_BUF_INIT, wd_path = GIT_BUF_INIT; - - assert(out && given_repo && opts); - - GITERR_CHECK_VERSION(opts, GIT_REPOSITORY_INIT_OPTIONS_VERSION, "git_repository_init_options"); - - error = repo_init_directories(&repo_path, &wd_path, given_repo, opts); - if (error < 0) - goto cleanup; - - if (valid_repository_path(&repo_path)) { - - if ((opts->flags & GIT_REPOSITORY_INIT_NO_REINIT) != 0) { - giterr_set(GITERR_REPOSITORY, - "Attempt to reinitialize '%s'", given_repo); - error = GIT_EEXISTS; - goto cleanup; - } - - opts->flags |= GIT_REPOSITORY_INIT__IS_REINIT; - - error = repo_init_config( - git_buf_cstr(&repo_path), git_buf_cstr(&wd_path), opts); - - /* TODO: reinitialize the templates */ - } - else { - if (!(error = repo_init_structure( - git_buf_cstr(&repo_path), git_buf_cstr(&wd_path), opts)) && - !(error = repo_init_config( - git_buf_cstr(&repo_path), git_buf_cstr(&wd_path), opts))) - error = repo_init_create_head( - git_buf_cstr(&repo_path), opts->initial_head); - } - if (error < 0) - goto cleanup; - - error = git_repository_open(out, git_buf_cstr(&repo_path)); - - if (!error && opts->origin_url) - error = repo_init_create_origin(*out, opts->origin_url); - -cleanup: - git_buf_free(&repo_path); - git_buf_free(&wd_path); - - return error; -} - -int git_repository_head_detached(git_repository *repo) -{ - git_reference *ref; - git_odb *odb = NULL; - int exists; - - if (git_repository_odb__weakptr(&odb, repo) < 0) - return -1; - - if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0) - return -1; - - if (git_reference_type(ref) == GIT_REF_SYMBOLIC) { - git_reference_free(ref); - return 0; - } - - exists = git_odb_exists(odb, git_reference_target(ref)); - - git_reference_free(ref); - return exists; -} - -int git_repository_head(git_reference **head_out, git_repository *repo) -{ - git_reference *head; - int error; - - if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0) - return error; - - if (git_reference_type(head) == GIT_REF_OID) { - *head_out = head; - return 0; - } - - error = git_reference_lookup_resolved(head_out, repo, git_reference_symbolic_target(head), -1); - git_reference_free(head); - - return error == GIT_ENOTFOUND ? GIT_EORPHANEDHEAD : error; -} - -int git_repository_head_orphan(git_repository *repo) -{ - git_reference *ref = NULL; - int error; - - error = git_repository_head(&ref, repo); - git_reference_free(ref); - - if (error == GIT_EORPHANEDHEAD) - return 1; - - if (error < 0) - return -1; - - return 0; -} - -static int at_least_one_cb(const char *refname, void *payload) -{ - GIT_UNUSED(refname); - GIT_UNUSED(payload); - - return GIT_EUSER; -} - -static int repo_contains_no_reference(git_repository *repo) -{ - int error; - - error = git_reference_foreach(repo, GIT_REF_LISTALL, at_least_one_cb, NULL); - - if (error == GIT_EUSER) - return 0; - - return error == 0 ? 1 : error; -} - -int git_repository_is_empty(git_repository *repo) -{ - git_reference *head = NULL; - int error; - - if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0) - return -1; - - if (!(error = git_reference_type(head) == GIT_REF_SYMBOLIC)) - goto cleanup; - - if (!(error = strcmp( - git_reference_symbolic_target(head), - GIT_REFS_HEADS_DIR "master") == 0)) - goto cleanup; - - error = repo_contains_no_reference(repo); - -cleanup: - git_reference_free(head); - return error < 0 ? -1 : error; -} - -const char *git_repository_path(git_repository *repo) -{ - assert(repo); - return repo->path_repository; -} - -const char *git_repository_workdir(git_repository *repo) -{ - assert(repo); - - if (repo->is_bare) - return NULL; - - return repo->workdir; -} - -int git_repository_set_workdir( - git_repository *repo, const char *workdir, int update_gitlink) -{ - int error = 0; - git_buf path = GIT_BUF_INIT; - - assert(repo && workdir); - - if (git_path_prettify_dir(&path, workdir, NULL) < 0) - return -1; - - if (repo->workdir && strcmp(repo->workdir, path.ptr) == 0) - return 0; - - if (update_gitlink) { - git_config *config; - - if (git_repository_config__weakptr(&config, repo) < 0) - return -1; - - error = repo_write_gitlink(path.ptr, git_repository_path(repo)); - - /* passthrough error means gitlink is unnecessary */ - if (error == GIT_PASSTHROUGH) - error = git_config_delete_entry(config, "core.worktree"); - else if (!error) - error = git_config_set_string(config, "core.worktree", path.ptr); - - if (!error) - error = git_config_set_bool(config, "core.bare", false); - } - - if (!error) { - char *old_workdir = repo->workdir; - - repo->workdir = git_buf_detach(&path); - repo->is_bare = 0; - - git__free(old_workdir); - } - - return error; -} - -int git_repository_is_bare(git_repository *repo) -{ - assert(repo); - return repo->is_bare; -} - -int git_repository_head_tree(git_tree **tree, git_repository *repo) -{ - git_reference *head; - git_object *obj; - int error; - - if ((error = git_repository_head(&head, repo)) < 0) - return error; - - if ((error = git_reference_peel(&obj, head, GIT_OBJ_TREE)) < 0) - goto cleanup; - - *tree = (git_tree *)obj; - -cleanup: - git_reference_free(head); - return error; -} - -int git_repository_message(char *buffer, size_t len, git_repository *repo) -{ - git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT; - struct stat st; - int error; - - if (git_buf_joinpath(&path, repo->path_repository, GIT_MERGE_MSG_FILE) < 0) - return -1; - - if ((error = p_stat(git_buf_cstr(&path), &st)) < 0) { - if (errno == ENOENT) - error = GIT_ENOTFOUND; - } - else if (buffer != NULL) { - error = git_futils_readbuffer(&buf, git_buf_cstr(&path)); - git_buf_copy_cstr(buffer, len, &buf); - } - - git_buf_free(&path); - git_buf_free(&buf); - - if (!error) - error = (int)st.st_size + 1; /* add 1 for NUL byte */ - - return error; -} - -int git_repository_message_remove(git_repository *repo) -{ - git_buf path = GIT_BUF_INIT; - int error; - - if (git_buf_joinpath(&path, repo->path_repository, GIT_MERGE_MSG_FILE) < 0) - return -1; - - error = p_unlink(git_buf_cstr(&path)); - git_buf_free(&path); - - return error; -} - -int git_repository_hashfile( - git_oid *out, - git_repository *repo, - const char *path, - git_otype type, - const char *as_path) -{ - int error; - git_vector filters = GIT_VECTOR_INIT; - git_file fd = -1; - git_off_t len; - git_buf full_path = GIT_BUF_INIT; - - assert(out && path && repo); /* as_path can be NULL */ - - /* At some point, it would be nice if repo could be NULL to just - * apply filter rules defined in system and global files, but for - * now that is not possible because git_filters_load() needs it. - */ - - error = git_path_join_unrooted( - &full_path, path, repo ? git_repository_workdir(repo) : NULL, NULL); - if (error < 0) - return error; - - if (!as_path) - as_path = path; - - /* passing empty string for "as_path" indicated --no-filters */ - if (strlen(as_path) > 0) { - error = git_filters_load(&filters, repo, as_path, GIT_FILTER_TO_ODB); - if (error < 0) - return error; - } else { - error = 0; - } - - /* at this point, error is a count of the number of loaded filters */ - - fd = git_futils_open_ro(full_path.ptr); - if (fd < 0) { - error = fd; - goto cleanup; - } - - len = git_futils_filesize(fd); - if (len < 0) { - error = (int)len; - goto cleanup; - } - - if (!git__is_sizet(len)) { - giterr_set(GITERR_OS, "File size overflow for 32-bit systems"); - error = -1; - goto cleanup; - } - - error = git_odb__hashfd_filtered(out, fd, (size_t)len, type, &filters); - -cleanup: - if (fd >= 0) - p_close(fd); - git_filters_free(&filters); - git_buf_free(&full_path); - - return error; -} - -static bool looks_like_a_branch(const char *refname) -{ - return git__prefixcmp(refname, GIT_REFS_HEADS_DIR) == 0; -} - -int git_repository_set_head( - git_repository* repo, - const char* refname) -{ - git_reference *ref, - *new_head = NULL; - int error; - - assert(repo && refname); - - error = git_reference_lookup(&ref, repo, refname); - if (error < 0 && error != GIT_ENOTFOUND) - return error; - - if (!error) { - if (git_reference_is_branch(ref)) - error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, git_reference_name(ref), 1); - else - error = git_repository_set_head_detached(repo, git_reference_target(ref)); - } else if (looks_like_a_branch(refname)) - error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname, 1); - - git_reference_free(ref); - git_reference_free(new_head); - return error; -} - -int git_repository_set_head_detached( - git_repository* repo, - const git_oid* commitish) -{ - int error; - git_object *object, - *peeled = NULL; - git_reference *new_head = NULL; - - assert(repo && commitish); - - if ((error = git_object_lookup(&object, repo, commitish, GIT_OBJ_ANY)) < 0) - return error; - - if ((error = git_object_peel(&peeled, object, GIT_OBJ_COMMIT)) < 0) - goto cleanup; - - error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), 1); - -cleanup: - git_object_free(object); - git_object_free(peeled); - git_reference_free(new_head); - return error; -} - -int git_repository_detach_head( - git_repository* repo) -{ - git_reference *old_head = NULL, - *new_head = NULL; - git_object *object = NULL; - int error; - - assert(repo); - - if ((error = git_repository_head(&old_head, repo)) < 0) - return error; - - if ((error = git_object_lookup(&object, repo, git_reference_target(old_head), GIT_OBJ_COMMIT)) < 0) - goto cleanup; - - error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_reference_target(old_head), 1); - -cleanup: - git_object_free(object); - git_reference_free(old_head); - git_reference_free(new_head); - return error; -} - -/** - * Loosely ported from git.git - * https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L198-289 - */ -int git_repository_state(git_repository *repo) -{ - git_buf repo_path = GIT_BUF_INIT; - int state = GIT_REPOSITORY_STATE_NONE; - - assert(repo); - - if (git_buf_puts(&repo_path, repo->path_repository) < 0) - return -1; - - if (git_path_contains_file(&repo_path, GIT_REBASE_MERGE_INTERACTIVE_FILE)) - state = GIT_REPOSITORY_STATE_REBASE_INTERACTIVE; - else if (git_path_contains_dir(&repo_path, GIT_REBASE_MERGE_DIR)) - state = GIT_REPOSITORY_STATE_REBASE_MERGE; - else if (git_path_contains_file(&repo_path, GIT_REBASE_APPLY_REBASING_FILE)) - state = GIT_REPOSITORY_STATE_REBASE; - else if (git_path_contains_file(&repo_path, GIT_REBASE_APPLY_APPLYING_FILE)) - state = GIT_REPOSITORY_STATE_APPLY_MAILBOX; - else if (git_path_contains_dir(&repo_path, GIT_REBASE_APPLY_DIR)) - state = GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE; - else if (git_path_contains_file(&repo_path, GIT_MERGE_HEAD_FILE)) - state = GIT_REPOSITORY_STATE_MERGE; - else if(git_path_contains_file(&repo_path, GIT_REVERT_HEAD_FILE)) - state = GIT_REPOSITORY_STATE_REVERT; - else if(git_path_contains_file(&repo_path, GIT_CHERRY_PICK_HEAD_FILE)) - state = GIT_REPOSITORY_STATE_CHERRY_PICK; - else if(git_path_contains_file(&repo_path, GIT_BISECT_LOG_FILE)) - state = GIT_REPOSITORY_STATE_BISECT; - - git_buf_free(&repo_path); - return state; -} diff --git a/src/repository.h b/src/repository.h deleted file mode 100644 index f19758fe470..00000000000 --- a/src/repository.h +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_repository_h__ -#define INCLUDE_repository_h__ - -#include "git2/common.h" -#include "git2/oid.h" -#include "git2/odb.h" -#include "git2/repository.h" -#include "git2/object.h" - -#include "index.h" -#include "cache.h" -#include "refs.h" -#include "buffer.h" -#include "odb.h" -#include "object.h" -#include "attr.h" -#include "strmap.h" - -#define DOT_GIT ".git" -#define GIT_DIR DOT_GIT "/" -#define GIT_DIR_MODE 0755 -#define GIT_BARE_DIR_MODE 0777 - -/** Cvar cache identifiers */ -typedef enum { - GIT_CVAR_AUTO_CRLF = 0, /* core.autocrlf */ - GIT_CVAR_EOL, /* core.eol */ - GIT_CVAR_CACHE_MAX -} git_cvar_cached; - -/** - * CVAR value enumerations - * - * These are the values that are actually stored in the cvar cache, instead - * of their string equivalents. These values are internal and symbolic; - * make sure that none of them is set to `-1`, since that is the unique - * identifier for "not cached" - */ -typedef enum { - /* The value hasn't been loaded from the cache yet */ - GIT_CVAR_NOT_CACHED = -1, - - /* core.safecrlf: false, 'fail', 'warn' */ - GIT_SAFE_CRLF_FALSE = 0, - GIT_SAFE_CRLF_FAIL = 1, - GIT_SAFE_CRLF_WARN = 2, - - /* core.autocrlf: false, true, 'input; */ - GIT_AUTO_CRLF_FALSE = 0, - GIT_AUTO_CRLF_TRUE = 1, - GIT_AUTO_CRLF_INPUT = 2, - GIT_AUTO_CRLF_DEFAULT = GIT_AUTO_CRLF_FALSE, - - /* core.eol: unset, 'crlf', 'lf', 'native' */ - GIT_EOL_UNSET = 0, - GIT_EOL_CRLF = 1, - GIT_EOL_LF = 2, -#ifdef GIT_WIN32 - GIT_EOL_NATIVE = GIT_EOL_CRLF, -#else - GIT_EOL_NATIVE = GIT_EOL_LF, -#endif - GIT_EOL_DEFAULT = GIT_EOL_NATIVE -} git_cvar_value; - -/* internal repository init flags */ -enum { - GIT_REPOSITORY_INIT__HAS_DOTGIT = (1u << 16), - GIT_REPOSITORY_INIT__NATURAL_WD = (1u << 17), - GIT_REPOSITORY_INIT__IS_REINIT = (1u << 18), -}; - -/** Internal structure for repository object */ -struct git_repository { - git_odb *_odb; - git_config *_config; - git_index *_index; - - git_cache objects; - git_refcache references; - git_attr_cache attrcache; - git_strmap *submodules; - - char *path_repository; - char *workdir; - - unsigned is_bare:1; - unsigned int lru_counter; - - git_cvar_value cvar_cache[GIT_CVAR_CACHE_MAX]; -}; - -GIT_INLINE(git_attr_cache *) git_repository_attr_cache(git_repository *repo) -{ - return &repo->attrcache; -} - -int git_repository_head_tree(git_tree **tree, git_repository *repo); - -/* - * Weak pointers to repository internals. - * - * The returned pointers do not need to be freed. Do not keep - * permanent references to these (i.e. between API calls), since they may - * become invalidated if the user replaces a repository internal. - */ -int git_repository_config__weakptr(git_config **out, git_repository *repo); -int git_repository_odb__weakptr(git_odb **out, git_repository *repo); -int git_repository_index__weakptr(git_index **out, git_repository *repo); - -/* - * CVAR cache - * - * Efficient access to the most used config variables of a repository. - * The cache is cleared everytime the config backend is replaced. - */ -int git_repository__cvar(int *out, git_repository *repo, git_cvar_cached cvar); -void git_repository__cvar_cache_clear(git_repository *repo); - -/* - * Submodule cache - */ -extern void git_submodule_config_free(git_repository *repo); - -GIT_INLINE(int) git_repository__ensure_not_bare( - git_repository *repo, - const char *operation_name) -{ - if (!git_repository_is_bare(repo)) - return 0; - - giterr_set( - GITERR_REPOSITORY, - "Cannot %s. This operation is not allowed against bare repositories.", - operation_name); - - return GIT_EBAREREPO; -} - -#endif diff --git a/src/reset.c b/src/reset.c deleted file mode 100644 index b637e373097..00000000000 --- a/src/reset.c +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "commit.h" -#include "tag.h" -#include "merge.h" -#include "git2/reset.h" -#include "git2/checkout.h" -#include "git2/merge.h" - -#define ERROR_MSG "Cannot perform reset" - -static int update_head(git_repository *repo, git_object *commit) -{ - int error; - git_reference *head = NULL, *target = NULL; - - error = git_repository_head(&head, repo); - - if (error < 0 && error != GIT_EORPHANEDHEAD) - return error; - - if (error == GIT_EORPHANEDHEAD) { - giterr_clear(); - - /* - * TODO: This is a bit weak as this doesn't support chained - * symbolic references. yet. - */ - if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0) - goto cleanup; - - if ((error = git_reference_create( - &target, - repo, - git_reference_symbolic_target(head), - git_object_id(commit), 0)) < 0) - goto cleanup; - } else { - if ((error = git_reference_set_target(head, git_object_id(commit))) < 0) - goto cleanup; - } - - error = 0; - -cleanup: - git_reference_free(head); - git_reference_free(target); - return error; -} - -int git_reset( - git_repository *repo, - git_object *target, - git_reset_t reset_type) -{ - git_object *commit = NULL; - git_index *index = NULL; - git_tree *tree = NULL; - int error = 0; - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - assert(repo && target); - - if (git_object_owner(target) != repo) { - giterr_set(GITERR_OBJECT, - "%s - The given target does not belong to this repository.", ERROR_MSG); - return -1; - } - - if (reset_type != GIT_RESET_SOFT && - (error = git_repository__ensure_not_bare(repo, - reset_type == GIT_RESET_MIXED ? "reset mixed" : "reset hard")) < 0) - return error; - - if ((error = git_object_peel(&commit, target, GIT_OBJ_COMMIT)) < 0 || - (error = git_repository_index(&index, repo)) < 0 || - (error = git_commit_tree(&tree, (git_commit *)commit)) < 0) - goto cleanup; - - if (reset_type == GIT_RESET_SOFT && - (git_repository_state(repo) == GIT_REPOSITORY_STATE_MERGE || - git_index_has_conflicts(index))) - { - giterr_set(GITERR_OBJECT, "%s (soft) in the middle of a merge.", ERROR_MSG); - error = GIT_EUNMERGED; - goto cleanup; - } - - /* move HEAD to the new target */ - if ((error = update_head(repo, commit)) < 0) - goto cleanup; - - if (reset_type == GIT_RESET_HARD) { - /* overwrite working directory with HEAD */ - opts.checkout_strategy = GIT_CHECKOUT_FORCE; - - if ((error = git_checkout_tree(repo, (git_object *)tree, &opts)) < 0) - goto cleanup; - } - - if (reset_type > GIT_RESET_SOFT) { - /* reset index to the target content */ - - if ((error = git_index_read_tree(index, tree)) < 0 || - (error = git_index_write(index)) < 0) - goto cleanup; - - if ((error = git_repository_merge_cleanup(repo)) < 0) { - giterr_set(GITERR_INDEX, "%s - failed to clean up merge data", ERROR_MSG); - goto cleanup; - } - } - -cleanup: - git_object_free(commit); - git_index_free(index); - git_tree_free(tree); - - return error; -} diff --git a/src/revparse.c b/src/revparse.c deleted file mode 100644 index 05ee1c57d8c..00000000000 --- a/src/revparse.c +++ /dev/null @@ -1,845 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include - -#include "common.h" -#include "buffer.h" -#include "tree.h" - -#include "git2.h" - -static int disambiguate_refname(git_reference **out, git_repository *repo, const char *refname) -{ - int error, i; - bool fallbackmode = true; - git_reference *ref; - git_buf refnamebuf = GIT_BUF_INIT, name = GIT_BUF_INIT; - - static const char* formatters[] = { - "%s", - GIT_REFS_DIR "%s", - GIT_REFS_TAGS_DIR "%s", - GIT_REFS_HEADS_DIR "%s", - GIT_REFS_REMOTES_DIR "%s", - GIT_REFS_REMOTES_DIR "%s/" GIT_HEAD_FILE, - NULL - }; - - if (*refname) - git_buf_puts(&name, refname); - else { - git_buf_puts(&name, GIT_HEAD_FILE); - fallbackmode = false; - } - - for (i = 0; formatters[i] && (fallbackmode || i == 0); i++) { - - git_buf_clear(&refnamebuf); - - if ((error = git_buf_printf(&refnamebuf, formatters[i], git_buf_cstr(&name))) < 0) - goto cleanup; - - if (!git_reference_is_valid_name(git_buf_cstr(&refnamebuf))) { - error = GIT_EINVALIDSPEC; - continue; - } - - error = git_reference_lookup_resolved(&ref, repo, git_buf_cstr(&refnamebuf), -1); - - if (!error) { - *out = ref; - error = 0; - goto cleanup; - } - - if (error != GIT_ENOTFOUND) - goto cleanup; - } - -cleanup: - git_buf_free(&name); - git_buf_free(&refnamebuf); - return error; -} - -static int maybe_sha_or_abbrev(git_object**out, git_repository *repo, const char *spec) -{ - git_oid oid; - size_t speclen = strlen(spec); - - if (git_oid_fromstrn(&oid, spec, speclen) < 0) - return GIT_ENOTFOUND; - - return git_object_lookup_prefix(out, repo, &oid, speclen, GIT_OBJ_ANY); -} - -static int build_regex(regex_t *regex, const char *pattern) -{ - int error; - - if (*pattern == '\0') { - giterr_set(GITERR_REGEX, "Empty pattern"); - return GIT_EINVALIDSPEC; - } - - error = regcomp(regex, pattern, REG_EXTENDED); - if (!error) - return 0; - - error = giterr_set_regex(regex, error); - - regfree(regex); - - return error; -} - -static int maybe_describe(git_object**out, git_repository *repo, const char *spec) -{ - const char *substr; - int error; - regex_t regex; - - substr = strstr(spec, "-g"); - - if (substr == NULL) - return GIT_ENOTFOUND; - - if (build_regex(®ex, ".+-[0-9]+-g[0-9a-fA-F]+") < 0) - return -1; - - error = regexec(®ex, spec, 0, NULL, 0); - regfree(®ex); - - if (error) - return GIT_ENOTFOUND; - - return maybe_sha_or_abbrev(out, repo, substr+2); -} - -static int revparse_lookup_object(git_object **out, git_repository *repo, const char *spec) -{ - int error; - git_reference *ref; - - error = maybe_describe(out, repo, spec); - if (!error) - return 0; - - if (error < 0 && error != GIT_ENOTFOUND) - return error; - - error = disambiguate_refname(&ref, repo, spec); - if (!error) { - error = git_object_lookup(out, repo, git_reference_target(ref), GIT_OBJ_ANY); - git_reference_free(ref); - return error; - } - - if (error < 0 && error != GIT_ENOTFOUND) - return error; - - error = maybe_sha_or_abbrev(out, repo, spec); - if (!error) - return 0; - - if (error < 0 && error != GIT_ENOTFOUND) - return error; - - giterr_set(GITERR_REFERENCE, "Refspec '%s' not found.", spec); - return GIT_ENOTFOUND; -} - -static int try_parse_numeric(int *n, const char *curly_braces_content) -{ - int32_t content; - const char *end_ptr; - - if (git__strtol32(&content, curly_braces_content, &end_ptr, 10) < 0) - return -1; - - if (*end_ptr != '\0') - return -1; - - *n = (int)content; - return 0; -} - -static int retrieve_previously_checked_out_branch_or_revision(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, size_t position) -{ - git_reference *ref = NULL; - git_reflog *reflog = NULL; - regex_t preg; - int error = -1; - size_t i, numentries, cur; - const git_reflog_entry *entry; - const char *msg; - regmatch_t regexmatches[2]; - git_buf buf = GIT_BUF_INIT; - - cur = position; - - if (*identifier != '\0' || *base_ref != NULL) - return GIT_EINVALIDSPEC; - - if (build_regex(&preg, "checkout: moving from (.*) to .*") < 0) - return -1; - - if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0) - goto cleanup; - - if (git_reflog_read(&reflog, ref) < 0) - goto cleanup; - - numentries = git_reflog_entrycount(reflog); - - for (i = 0; i < numentries; i++) { - entry = git_reflog_entry_byindex(reflog, i); - msg = git_reflog_entry_message(entry); - - if (regexec(&preg, msg, 2, regexmatches, 0)) - continue; - - cur--; - - if (cur > 0) - continue; - - git_buf_put(&buf, msg+regexmatches[1].rm_so, regexmatches[1].rm_eo - regexmatches[1].rm_so); - - if ((error = disambiguate_refname(base_ref, repo, git_buf_cstr(&buf))) == 0) - goto cleanup; - - if (error < 0 && error != GIT_ENOTFOUND) - goto cleanup; - - error = maybe_sha_or_abbrev(out, repo, git_buf_cstr(&buf)); - - goto cleanup; - } - - error = GIT_ENOTFOUND; - -cleanup: - git_reference_free(ref); - git_buf_free(&buf); - regfree(&preg); - git_reflog_free(reflog); - return error; -} - -static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, size_t identifier) -{ - git_reflog *reflog; - int error = -1; - size_t numentries; - const git_reflog_entry *entry; - bool search_by_pos = (identifier <= 100000000); - - if (git_reflog_read(&reflog, ref) < 0) - return -1; - - numentries = git_reflog_entrycount(reflog); - - if (search_by_pos) { - if (numentries < identifier + 1) { - giterr_set( - GITERR_REFERENCE, - "Reflog for '%s' has only "PRIuZ" entries, asked for "PRIuZ, - git_reference_name(ref), numentries, identifier); - - error = GIT_ENOTFOUND; - goto cleanup; - } - - entry = git_reflog_entry_byindex(reflog, identifier); - git_oid_cpy(oid, git_reflog_entry_id_new(entry)); - error = 0; - goto cleanup; - - } else { - size_t i; - git_time commit_time; - - for (i = 0; i < numentries; i++) { - entry = git_reflog_entry_byindex(reflog, i); - commit_time = git_reflog_entry_committer(entry)->when; - - if (commit_time.time > (git_time_t)identifier) - continue; - - git_oid_cpy(oid, git_reflog_entry_id_new(entry)); - error = 0; - goto cleanup; - } - - error = GIT_ENOTFOUND; - } - -cleanup: - git_reflog_free(reflog); - return error; -} - -static int retrieve_revobject_from_reflog(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, size_t position) -{ - git_reference *ref; - git_oid oid; - int error = -1; - - if (*base_ref == NULL) { - if ((error = disambiguate_refname(&ref, repo, identifier)) < 0) - return error; - } else { - ref = *base_ref; - *base_ref = NULL; - } - - if (position == 0) { - error = git_object_lookup(out, repo, git_reference_target(ref), GIT_OBJ_ANY); - goto cleanup; - } - - if ((error = retrieve_oid_from_reflog(&oid, ref, position)) < 0) - goto cleanup; - - error = git_object_lookup(out, repo, &oid, GIT_OBJ_ANY); - -cleanup: - git_reference_free(ref); - return error; -} - -static int retrieve_remote_tracking_reference(git_reference **base_ref, const char *identifier, git_repository *repo) -{ - git_reference *tracking, *ref; - int error = -1; - - if (*base_ref == NULL) { - if ((error = disambiguate_refname(&ref, repo, identifier)) < 0) - return error; - } else { - ref = *base_ref; - *base_ref = NULL; - } - - if (!git_reference_is_branch(ref)) { - error = GIT_EINVALIDSPEC; - goto cleanup; - } - - if ((error = git_branch_tracking(&tracking, ref)) < 0) - goto cleanup; - - *base_ref = tracking; - -cleanup: - git_reference_free(ref); - return error; -} - -static int handle_at_syntax(git_object **out, git_reference **ref, const char *spec, size_t identifier_len, git_repository* repo, const char *curly_braces_content) -{ - bool is_numeric; - int parsed = 0, error = -1; - git_buf identifier = GIT_BUF_INIT; - git_time_t timestamp; - - assert(*out == NULL); - - if (git_buf_put(&identifier, spec, identifier_len) < 0) - return -1; - - is_numeric = !try_parse_numeric(&parsed, curly_braces_content); - - if (*curly_braces_content == '-' && (!is_numeric || parsed == 0)) { - error = GIT_EINVALIDSPEC; - goto cleanup; - } - - if (is_numeric) { - if (parsed < 0) - error = retrieve_previously_checked_out_branch_or_revision(out, ref, repo, git_buf_cstr(&identifier), -parsed); - else - error = retrieve_revobject_from_reflog(out, ref, repo, git_buf_cstr(&identifier), parsed); - - goto cleanup; - } - - if (!strcmp(curly_braces_content, "u") || !strcmp(curly_braces_content, "upstream")) { - error = retrieve_remote_tracking_reference(ref, git_buf_cstr(&identifier), repo); - - goto cleanup; - } - - if (git__date_parse(×tamp, curly_braces_content) < 0) - goto cleanup; - - error = retrieve_revobject_from_reflog(out, ref, repo, git_buf_cstr(&identifier), (size_t)timestamp); - -cleanup: - git_buf_free(&identifier); - return error; -} - -static git_otype parse_obj_type(const char *str) -{ - if (!strcmp(str, "commit")) - return GIT_OBJ_COMMIT; - - if (!strcmp(str, "tree")) - return GIT_OBJ_TREE; - - if (!strcmp(str, "blob")) - return GIT_OBJ_BLOB; - - if (!strcmp(str, "tag")) - return GIT_OBJ_TAG; - - return GIT_OBJ_BAD; -} - -static int dereference_to_non_tag(git_object **out, git_object *obj) -{ - if (git_object_type(obj) == GIT_OBJ_TAG) - return git_tag_peel(out, (git_tag *)obj); - - return git_object__dup(out, obj); -} - -static int handle_caret_parent_syntax(git_object **out, git_object *obj, int n) -{ - git_object *temp_commit = NULL; - int error; - - if ((error = git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT)) < 0) - return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ? - GIT_EINVALIDSPEC : error; - - if (n == 0) { - *out = temp_commit; - return 0; - } - - error = git_commit_parent((git_commit **)out, (git_commit*)temp_commit, n - 1); - - git_object_free(temp_commit); - return error; -} - -static int handle_linear_syntax(git_object **out, git_object *obj, int n) -{ - git_object *temp_commit = NULL; - int error; - - if ((error = git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT)) < 0) - return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ? - GIT_EINVALIDSPEC : error; - - error = git_commit_nth_gen_ancestor((git_commit **)out, (git_commit*)temp_commit, n); - - git_object_free(temp_commit); - return error; -} - -static int handle_colon_syntax( - git_object **out, - git_object *obj, - const char *path) -{ - git_object *tree; - int error = -1; - git_tree_entry *entry = NULL; - - if ((error = git_object_peel(&tree, obj, GIT_OBJ_TREE)) < 0) - return error == GIT_ENOTFOUND ? GIT_EINVALIDSPEC : error; - - if (*path == '\0') { - *out = tree; - return 0; - } - - /* - * TODO: Handle the relative path syntax - * (:./relative/path and :../relative/path) - */ - if ((error = git_tree_entry_bypath(&entry, (git_tree *)tree, path)) < 0) - goto cleanup; - - error = git_tree_entry_to_object(out, git_object_owner(tree), entry); - -cleanup: - git_tree_entry_free(entry); - git_object_free(tree); - - return error; -} - -static int walk_and_search(git_object **out, git_revwalk *walk, regex_t *regex) -{ - int error; - git_oid oid; - git_object *obj; - - while (!(error = git_revwalk_next(&oid, walk))) { - - if ((error = git_object_lookup(&obj, git_revwalk_repository(walk), &oid, GIT_OBJ_COMMIT) < 0) && - (error != GIT_ENOTFOUND)) - return -1; - - if (!regexec(regex, git_commit_message((git_commit*)obj), 0, NULL, 0)) { - *out = obj; - return 0; - } - - git_object_free(obj); - } - - if (error < 0 && error == GIT_ITEROVER) - error = GIT_ENOTFOUND; - - return error; -} - -static int handle_grep_syntax(git_object **out, git_repository *repo, const git_oid *spec_oid, const char *pattern) -{ - regex_t preg; - git_revwalk *walk = NULL; - int error; - - if ((error = build_regex(&preg, pattern)) < 0) - return error; - - if ((error = git_revwalk_new(&walk, repo)) < 0) - goto cleanup; - - git_revwalk_sorting(walk, GIT_SORT_TIME); - - if (spec_oid == NULL) { - // TODO: @carlosmn: The glob should be refs/* but this makes git_revwalk_next() fails - if ((error = git_revwalk_push_glob(walk, GIT_REFS_HEADS_DIR "*")) < 0) - goto cleanup; - } else if ((error = git_revwalk_push(walk, spec_oid)) < 0) - goto cleanup; - - error = walk_and_search(out, walk, &preg); - -cleanup: - regfree(&preg); - git_revwalk_free(walk); - - return error; -} - -static int handle_caret_curly_syntax(git_object **out, git_object *obj, const char *curly_braces_content) -{ - git_otype expected_type; - - if (*curly_braces_content == '\0') - return dereference_to_non_tag(out, obj); - - if (*curly_braces_content == '/') - return handle_grep_syntax(out, git_object_owner(obj), git_object_id(obj), curly_braces_content + 1); - - expected_type = parse_obj_type(curly_braces_content); - - if (expected_type == GIT_OBJ_BAD) - return GIT_EINVALIDSPEC; - - return git_object_peel(out, obj, expected_type); -} - -static int extract_curly_braces_content(git_buf *buf, const char *spec, size_t *pos) -{ - git_buf_clear(buf); - - assert(spec[*pos] == '^' || spec[*pos] == '@'); - - (*pos)++; - - if (spec[*pos] == '\0' || spec[*pos] != '{') - return GIT_EINVALIDSPEC; - - (*pos)++; - - while (spec[*pos] != '}') { - if (spec[*pos] == '\0') - return GIT_EINVALIDSPEC; - - git_buf_putc(buf, spec[(*pos)++]); - } - - (*pos)++; - - return 0; -} - -static int extract_path(git_buf *buf, const char *spec, size_t *pos) -{ - git_buf_clear(buf); - - assert(spec[*pos] == ':'); - - (*pos)++; - - if (git_buf_puts(buf, spec + *pos) < 0) - return -1; - - *pos += git_buf_len(buf); - - return 0; -} - -static int extract_how_many(int *n, const char *spec, size_t *pos) -{ - const char *end_ptr; - int parsed, accumulated; - char kind = spec[*pos]; - - assert(spec[*pos] == '^' || spec[*pos] == '~'); - - accumulated = 0; - - do { - do { - (*pos)++; - accumulated++; - } while (spec[(*pos)] == kind && kind == '~'); - - if (git__isdigit(spec[*pos])) { - if ((git__strtol32(&parsed, spec + *pos, &end_ptr, 10) < 0) < 0) - return GIT_EINVALIDSPEC; - - accumulated += (parsed - 1); - *pos = end_ptr - spec; - } - - } while (spec[(*pos)] == kind && kind == '~'); - - *n = accumulated; - - return 0; -} - -static int object_from_reference(git_object **object, git_reference *reference) -{ - git_reference *resolved = NULL; - int error; - - if (git_reference_resolve(&resolved, reference) < 0) - return -1; - - error = git_object_lookup(object, reference->owner, git_reference_target(resolved), GIT_OBJ_ANY); - git_reference_free(resolved); - - return error; -} - -static int ensure_base_rev_loaded(git_object **object, git_reference **reference, const char *spec, size_t identifier_len, git_repository *repo, bool allow_empty_identifier) -{ - int error; - git_buf identifier = GIT_BUF_INIT; - - if (*object != NULL) - return 0; - - if (*reference != NULL) { - if ((error = object_from_reference(object, *reference)) < 0) - return error; - - git_reference_free(*reference); - *reference = NULL; - return 0; - } - - if (!allow_empty_identifier && identifier_len == 0) - return GIT_EINVALIDSPEC; - - if (git_buf_put(&identifier, spec, identifier_len) < 0) - return -1; - - error = revparse_lookup_object(object, repo, git_buf_cstr(&identifier)); - git_buf_free(&identifier); - - return error; -} - -static int ensure_base_rev_is_not_known_yet(git_object *object) -{ - if (object == NULL) - return 0; - - return GIT_EINVALIDSPEC; -} - -static bool any_left_hand_identifier(git_object *object, git_reference *reference, size_t identifier_len) -{ - if (object != NULL) - return true; - - if (reference != NULL) - return true; - - if (identifier_len > 0) - return true; - - return false; -} - -static int ensure_left_hand_identifier_is_not_known_yet(git_object *object, git_reference *reference) -{ - if (!ensure_base_rev_is_not_known_yet(object) && reference == NULL) - return 0; - - return GIT_EINVALIDSPEC; -} - -int git_revparse_single(git_object **out, git_repository *repo, const char *spec) -{ - size_t pos = 0, identifier_len = 0; - int error = -1, n; - git_buf buf = GIT_BUF_INIT; - - git_reference *reference = NULL; - git_object *base_rev = NULL; - - assert(out && repo && spec); - - *out = NULL; - - while (spec[pos]) { - switch (spec[pos]) { - case '^': - if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0) - goto cleanup; - - if (spec[pos+1] == '{') { - git_object *temp_object = NULL; - - if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0) - goto cleanup; - - if ((error = handle_caret_curly_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0) - goto cleanup; - - git_object_free(base_rev); - base_rev = temp_object; - } else { - git_object *temp_object = NULL; - - if ((error = extract_how_many(&n, spec, &pos)) < 0) - goto cleanup; - - if ((error = handle_caret_parent_syntax(&temp_object, base_rev, n)) < 0) - goto cleanup; - - git_object_free(base_rev); - base_rev = temp_object; - } - break; - - case '~': - { - git_object *temp_object = NULL; - - if ((error = extract_how_many(&n, spec, &pos)) < 0) - goto cleanup; - - if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0) - goto cleanup; - - if ((error = handle_linear_syntax(&temp_object, base_rev, n)) < 0) - goto cleanup; - - git_object_free(base_rev); - base_rev = temp_object; - break; - } - - case ':': - { - git_object *temp_object = NULL; - - if ((error = extract_path(&buf, spec, &pos)) < 0) - goto cleanup; - - if (any_left_hand_identifier(base_rev, reference, identifier_len)) { - if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, true)) < 0) - goto cleanup; - - if ((error = handle_colon_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0) - goto cleanup; - } else { - if (*git_buf_cstr(&buf) == '/') { - if ((error = handle_grep_syntax(&temp_object, repo, NULL, git_buf_cstr(&buf) + 1)) < 0) - goto cleanup; - } else { - - /* - * TODO: support merge-stage path lookup (":2:Makefile") - * and plain index blob lookup (:i-am/a/blob) - */ - giterr_set(GITERR_INVALID, "Unimplemented"); - error = GIT_ERROR; - goto cleanup; - } - } - - git_object_free(base_rev); - base_rev = temp_object; - break; - } - - case '@': - { - if (spec[pos+1] == '{') { - git_object *temp_object = NULL; - - if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0) - goto cleanup; - - if ((error = ensure_base_rev_is_not_known_yet(base_rev)) < 0) - goto cleanup; - - if ((error = handle_at_syntax(&temp_object, &reference, spec, identifier_len, repo, git_buf_cstr(&buf))) < 0) - goto cleanup; - - if (temp_object != NULL) - base_rev = temp_object; - break; - } else { - /* Fall through */ - } - } - - default: - if ((error = ensure_left_hand_identifier_is_not_known_yet(base_rev, reference)) < 0) - goto cleanup; - - pos++; - identifier_len++; - } - } - - if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0) - goto cleanup; - - *out = base_rev; - error = 0; - -cleanup: - if (error) { - if (error == GIT_EINVALIDSPEC) - giterr_set(GITERR_INVALID, - "Failed to parse revision specifier - Invalid pattern '%s'", spec); - - git_object_free(base_rev); - } - git_reference_free(reference); - git_buf_free(&buf); - return error; -} diff --git a/src/revwalk.c b/src/revwalk.c deleted file mode 100644 index 02834ab3659..00000000000 --- a/src/revwalk.c +++ /dev/null @@ -1,508 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "commit.h" -#include "odb.h" -#include "pool.h" - -#include "revwalk.h" -#include "merge.h" - -#include - -git_commit_list_node *git_revwalk__commit_lookup( - git_revwalk *walk, const git_oid *oid) -{ - git_commit_list_node *commit; - khiter_t pos; - int ret; - - /* lookup and reserve space if not already present */ - pos = kh_get(oid, walk->commits, oid); - if (pos != kh_end(walk->commits)) - return kh_value(walk->commits, pos); - - commit = git_commit_list_alloc_node(walk); - if (commit == NULL) - return NULL; - - git_oid_cpy(&commit->oid, oid); - - pos = kh_put(oid, walk->commits, &commit->oid, &ret); - assert(ret != 0); - kh_value(walk->commits, pos) = commit; - - return commit; -} - -static void mark_uninteresting(git_commit_list_node *commit) -{ - unsigned short i; - assert(commit); - - commit->uninteresting = 1; - - /* This means we've reached a merge base, so there's no need to walk any more */ - if ((commit->flags & (RESULT | STALE)) == RESULT) - return; - - for (i = 0; i < commit->out_degree; ++i) - if (!commit->parents[i]->uninteresting) - mark_uninteresting(commit->parents[i]); -} - -static int process_commit(git_revwalk *walk, git_commit_list_node *commit, int hide) -{ - int error; - - if (hide) - mark_uninteresting(commit); - - if (commit->seen) - return 0; - - commit->seen = 1; - - if ((error = git_commit_list_parse(walk, commit)) < 0) - return error; - - return walk->enqueue(walk, commit); -} - -static int process_commit_parents(git_revwalk *walk, git_commit_list_node *commit) -{ - unsigned short i; - int error = 0; - - for (i = 0; i < commit->out_degree && !error; ++i) - error = process_commit(walk, commit->parents[i], commit->uninteresting); - - return error; -} - -static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting) -{ - git_object *obj; - git_otype type; - git_commit_list_node *commit; - - if (git_object_lookup(&obj, walk->repo, oid, GIT_OBJ_ANY) < 0) - return -1; - - type = git_object_type(obj); - git_object_free(obj); - - if (type != GIT_OBJ_COMMIT) { - giterr_set(GITERR_INVALID, "Object is no commit object"); - return -1; - } - - commit = git_revwalk__commit_lookup(walk, oid); - if (commit == NULL) - return -1; /* error already reported by failed lookup */ - - commit->uninteresting = uninteresting; - if (walk->one == NULL && !uninteresting) { - walk->one = commit; - } else { - if (git_vector_insert(&walk->twos, commit) < 0) - return -1; - } - - return 0; -} - -int git_revwalk_push(git_revwalk *walk, const git_oid *oid) -{ - assert(walk && oid); - return push_commit(walk, oid, 0); -} - - -int git_revwalk_hide(git_revwalk *walk, const git_oid *oid) -{ - assert(walk && oid); - return push_commit(walk, oid, 1); -} - -static int push_ref(git_revwalk *walk, const char *refname, int hide) -{ - git_oid oid; - - if (git_reference_name_to_id(&oid, walk->repo, refname) < 0) - return -1; - - return push_commit(walk, &oid, hide); -} - -struct push_cb_data { - git_revwalk *walk; - int hide; -}; - -static int push_glob_cb(const char *refname, void *data_) -{ - struct push_cb_data *data = (struct push_cb_data *)data_; - - return push_ref(data->walk, refname, data->hide); -} - -static int push_glob(git_revwalk *walk, const char *glob, int hide) -{ - git_buf buf = GIT_BUF_INIT; - struct push_cb_data data; - regex_t preg; - - assert(walk && glob); - - /* refs/ is implied if not given in the glob */ - if (strncmp(glob, GIT_REFS_DIR, strlen(GIT_REFS_DIR))) { - git_buf_printf(&buf, GIT_REFS_DIR "%s", glob); - } else { - git_buf_puts(&buf, glob); - } - - /* If no '?', '*' or '[' exist, we append '/ *' to the glob */ - memset(&preg, 0x0, sizeof(regex_t)); - if (regcomp(&preg, "[?*[]", REG_EXTENDED)) { - giterr_set(GITERR_OS, "Regex failed to compile"); - git_buf_free(&buf); - return -1; - } - - if (regexec(&preg, glob, 0, NULL, 0)) - git_buf_puts(&buf, "/*"); - - if (git_buf_oom(&buf)) - goto on_error; - - data.walk = walk; - data.hide = hide; - - if (git_reference_foreach_glob( - walk->repo, git_buf_cstr(&buf), GIT_REF_LISTALL, push_glob_cb, &data) < 0) - goto on_error; - - regfree(&preg); - git_buf_free(&buf); - return 0; - -on_error: - regfree(&preg); - git_buf_free(&buf); - return -1; -} - -int git_revwalk_push_glob(git_revwalk *walk, const char *glob) -{ - assert(walk && glob); - return push_glob(walk, glob, 0); -} - -int git_revwalk_hide_glob(git_revwalk *walk, const char *glob) -{ - assert(walk && glob); - return push_glob(walk, glob, 1); -} - -int git_revwalk_push_head(git_revwalk *walk) -{ - assert(walk); - return push_ref(walk, GIT_HEAD_FILE, 0); -} - -int git_revwalk_hide_head(git_revwalk *walk) -{ - assert(walk); - return push_ref(walk, GIT_HEAD_FILE, 1); -} - -int git_revwalk_push_ref(git_revwalk *walk, const char *refname) -{ - assert(walk && refname); - return push_ref(walk, refname, 0); -} - -int git_revwalk_hide_ref(git_revwalk *walk, const char *refname) -{ - assert(walk && refname); - return push_ref(walk, refname, 1); -} - -static int revwalk_enqueue_timesort(git_revwalk *walk, git_commit_list_node *commit) -{ - return git_pqueue_insert(&walk->iterator_time, commit); -} - -static int revwalk_enqueue_unsorted(git_revwalk *walk, git_commit_list_node *commit) -{ - return git_commit_list_insert(commit, &walk->iterator_rand) ? 0 : -1; -} - -static int revwalk_next_timesort(git_commit_list_node **object_out, git_revwalk *walk) -{ - int error; - git_commit_list_node *next; - - while ((next = git_pqueue_pop(&walk->iterator_time)) != NULL) { - if ((error = process_commit_parents(walk, next)) < 0) - return error; - - if (!next->uninteresting) { - *object_out = next; - return 0; - } - } - - giterr_clear(); - return GIT_ITEROVER; -} - -static int revwalk_next_unsorted(git_commit_list_node **object_out, git_revwalk *walk) -{ - int error; - git_commit_list_node *next; - - while ((next = git_commit_list_pop(&walk->iterator_rand)) != NULL) { - if ((error = process_commit_parents(walk, next)) < 0) - return error; - - if (!next->uninteresting) { - *object_out = next; - return 0; - } - } - - giterr_clear(); - return GIT_ITEROVER; -} - -static int revwalk_next_toposort(git_commit_list_node **object_out, git_revwalk *walk) -{ - git_commit_list_node *next; - unsigned short i; - - for (;;) { - next = git_commit_list_pop(&walk->iterator_topo); - if (next == NULL) { - giterr_clear(); - return GIT_ITEROVER; - } - - if (next->in_degree > 0) { - next->topo_delay = 1; - continue; - } - - for (i = 0; i < next->out_degree; ++i) { - git_commit_list_node *parent = next->parents[i]; - - if (--parent->in_degree == 0 && parent->topo_delay) { - parent->topo_delay = 0; - if (git_commit_list_insert(parent, &walk->iterator_topo) == NULL) - return -1; - } - } - - *object_out = next; - return 0; - } -} - -static int revwalk_next_reverse(git_commit_list_node **object_out, git_revwalk *walk) -{ - *object_out = git_commit_list_pop(&walk->iterator_reverse); - return *object_out ? 0 : GIT_ITEROVER; -} - - -static int prepare_walk(git_revwalk *walk) -{ - int error; - unsigned int i; - git_commit_list_node *next, *two; - git_commit_list *bases = NULL; - - /* - * If walk->one is NULL, there were no positive references, - * so we know that the walk is already over. - */ - if (walk->one == NULL) { - giterr_clear(); - return GIT_ITEROVER; - } - - /* first figure out what the merge bases are */ - if (git_merge__bases_many(&bases, walk, walk->one, &walk->twos) < 0) - return -1; - - git_commit_list_free(&bases); - if (process_commit(walk, walk->one, walk->one->uninteresting) < 0) - return -1; - - git_vector_foreach(&walk->twos, i, two) { - if (process_commit(walk, two, two->uninteresting) < 0) - return -1; - } - - if (walk->sorting & GIT_SORT_TOPOLOGICAL) { - unsigned short i; - - while ((error = walk->get_next(&next, walk)) == 0) { - for (i = 0; i < next->out_degree; ++i) { - git_commit_list_node *parent = next->parents[i]; - parent->in_degree++; - } - - if (git_commit_list_insert(next, &walk->iterator_topo) == NULL) - return -1; - } - - if (error != GIT_ITEROVER) - return error; - - walk->get_next = &revwalk_next_toposort; - } - - if (walk->sorting & GIT_SORT_REVERSE) { - - while ((error = walk->get_next(&next, walk)) == 0) - if (git_commit_list_insert(next, &walk->iterator_reverse) == NULL) - return -1; - - if (error != GIT_ITEROVER) - return error; - - walk->get_next = &revwalk_next_reverse; - } - - walk->walking = 1; - return 0; -} - - -int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo) -{ - git_revwalk *walk; - - walk = git__malloc(sizeof(git_revwalk)); - GITERR_CHECK_ALLOC(walk); - - memset(walk, 0x0, sizeof(git_revwalk)); - - walk->commits = git_oidmap_alloc(); - GITERR_CHECK_ALLOC(walk->commits); - - if (git_pqueue_init(&walk->iterator_time, 8, git_commit_list_time_cmp) < 0 || - git_vector_init(&walk->twos, 4, NULL) < 0 || - git_pool_init(&walk->commit_pool, 1, - git_pool__suggest_items_per_page(COMMIT_ALLOC) * COMMIT_ALLOC) < 0) - return -1; - - walk->get_next = &revwalk_next_unsorted; - walk->enqueue = &revwalk_enqueue_unsorted; - - walk->repo = repo; - - if (git_repository_odb(&walk->odb, repo) < 0) { - git_revwalk_free(walk); - return -1; - } - - *revwalk_out = walk; - return 0; -} - -void git_revwalk_free(git_revwalk *walk) -{ - if (walk == NULL) - return; - - git_revwalk_reset(walk); - git_odb_free(walk->odb); - - git_oidmap_free(walk->commits); - git_pool_clear(&walk->commit_pool); - git_pqueue_free(&walk->iterator_time); - git_vector_free(&walk->twos); - git__free(walk); -} - -git_repository *git_revwalk_repository(git_revwalk *walk) -{ - assert(walk); - return walk->repo; -} - -void git_revwalk_sorting(git_revwalk *walk, unsigned int sort_mode) -{ - assert(walk); - - if (walk->walking) - git_revwalk_reset(walk); - - walk->sorting = sort_mode; - - if (walk->sorting & GIT_SORT_TIME) { - walk->get_next = &revwalk_next_timesort; - walk->enqueue = &revwalk_enqueue_timesort; - } else { - walk->get_next = &revwalk_next_unsorted; - walk->enqueue = &revwalk_enqueue_unsorted; - } -} - -int git_revwalk_next(git_oid *oid, git_revwalk *walk) -{ - int error; - git_commit_list_node *next; - - assert(walk && oid); - - if (!walk->walking) { - if ((error = prepare_walk(walk)) < 0) - return error; - } - - error = walk->get_next(&next, walk); - - if (error == GIT_ITEROVER) { - git_revwalk_reset(walk); - giterr_clear(); - return GIT_ITEROVER; - } - - if (!error) - git_oid_cpy(oid, &next->oid); - - return error; -} - -void git_revwalk_reset(git_revwalk *walk) -{ - git_commit_list_node *commit; - - assert(walk); - - kh_foreach_value(walk->commits, commit, { - commit->seen = 0; - commit->in_degree = 0; - commit->topo_delay = 0; - commit->uninteresting = 0; - }); - - git_pqueue_clear(&walk->iterator_time); - git_commit_list_free(&walk->iterator_topo); - git_commit_list_free(&walk->iterator_rand); - git_commit_list_free(&walk->iterator_reverse); - walk->walking = 0; - - walk->one = NULL; - git_vector_clear(&walk->twos); -} - diff --git a/src/revwalk.h b/src/revwalk.h deleted file mode 100644 index 22696dfcd5d..00000000000 --- a/src/revwalk.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_revwalk_h__ -#define INCLUDE_revwalk_h__ - -#include "git2/revwalk.h" -#include "oidmap.h" -#include "commit_list.h" -#include "pqueue.h" -#include "pool.h" -#include "vector.h" - -GIT__USE_OIDMAP; - -struct git_revwalk { - git_repository *repo; - git_odb *odb; - - git_oidmap *commits; - git_pool commit_pool; - - git_commit_list *iterator_topo; - git_commit_list *iterator_rand; - git_commit_list *iterator_reverse; - git_pqueue iterator_time; - - int (*get_next)(git_commit_list_node **, git_revwalk *); - int (*enqueue)(git_revwalk *, git_commit_list_node *); - - unsigned walking:1; - unsigned int sorting; - - /* merge base calculation */ - git_commit_list_node *one; - git_vector twos; -}; - -git_commit_list_node *git_revwalk__commit_lookup(git_revwalk *walk, const git_oid *oid); - -#endif diff --git a/src/sha1_lookup.c b/src/sha1_lookup.c deleted file mode 100644 index b7e66cc69f4..00000000000 --- a/src/sha1_lookup.c +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include - -#include "sha1_lookup.h" -#include "common.h" - -/* - * Conventional binary search loop looks like this: - * - * unsigned lo, hi; - * do { - * unsigned mi = (lo + hi) / 2; - * int cmp = "entry pointed at by mi" minus "target"; - * if (!cmp) - * return (mi is the wanted one) - * if (cmp > 0) - * hi = mi; "mi is larger than target" - * else - * lo = mi+1; "mi is smaller than target" - * } while (lo < hi); - * - * The invariants are: - * - * - When entering the loop, lo points at a slot that is never - * above the target (it could be at the target), hi points at a - * slot that is guaranteed to be above the target (it can never - * be at the target). - * - * - We find a point 'mi' between lo and hi (mi could be the same - * as lo, but never can be as same as hi), and check if it hits - * the target. There are three cases: - * - * - if it is a hit, we are happy. - * - * - if it is strictly higher than the target, we set it to hi, - * and repeat the search. - * - * - if it is strictly lower than the target, we update lo to - * one slot after it, because we allow lo to be at the target. - * - * If the loop exits, there is no matching entry. - * - * When choosing 'mi', we do not have to take the "middle" but - * anywhere in between lo and hi, as long as lo <= mi < hi is - * satisfied. When we somehow know that the distance between the - * target and lo is much shorter than the target and hi, we could - * pick mi that is much closer to lo than the midway. - * - * Now, we can take advantage of the fact that SHA-1 is a good hash - * function, and as long as there are enough entries in the table, we - * can expect uniform distribution. An entry that begins with for - * example "deadbeef..." is much likely to appear much later than in - * the midway of the table. It can reasonably be expected to be near - * 87% (222/256) from the top of the table. - * - * However, we do not want to pick "mi" too precisely. If the entry at - * the 87% in the above example turns out to be higher than the target - * we are looking for, we would end up narrowing the search space down - * only by 13%, instead of 50% we would get if we did a simple binary - * search. So we would want to hedge our bets by being less aggressive. - * - * The table at "table" holds at least "nr" entries of "elem_size" - * bytes each. Each entry has the SHA-1 key at "key_offset". The - * table is sorted by the SHA-1 key of the entries. The caller wants - * to find the entry with "key", and knows that the entry at "lo" is - * not higher than the entry it is looking for, and that the entry at - * "hi" is higher than the entry it is looking for. - */ -int sha1_entry_pos(const void *table, - size_t elem_size, - size_t key_offset, - unsigned lo, unsigned hi, unsigned nr, - const unsigned char *key) -{ - const unsigned char *base = (const unsigned char*)table; - const unsigned char *hi_key, *lo_key; - unsigned ofs_0; - - if (!nr || lo >= hi) - return -1; - - if (nr == hi) - hi_key = NULL; - else - hi_key = base + elem_size * hi + key_offset; - lo_key = base + elem_size * lo + key_offset; - - ofs_0 = 0; - do { - int cmp; - unsigned ofs, mi, range; - unsigned lov, hiv, kyv; - const unsigned char *mi_key; - - range = hi - lo; - if (hi_key) { - for (ofs = ofs_0; ofs < 20; ofs++) - if (lo_key[ofs] != hi_key[ofs]) - break; - ofs_0 = ofs; - /* - * byte 0 thru (ofs-1) are the same between - * lo and hi; ofs is the first byte that is - * different. - */ - hiv = hi_key[ofs_0]; - if (ofs_0 < 19) - hiv = (hiv << 8) | hi_key[ofs_0+1]; - } else { - hiv = 256; - if (ofs_0 < 19) - hiv <<= 8; - } - lov = lo_key[ofs_0]; - kyv = key[ofs_0]; - if (ofs_0 < 19) { - lov = (lov << 8) | lo_key[ofs_0+1]; - kyv = (kyv << 8) | key[ofs_0+1]; - } - assert(lov < hiv); - - if (kyv < lov) - return -1 - lo; - if (hiv < kyv) - return -1 - hi; - - /* - * Even if we know the target is much closer to 'hi' - * than 'lo', if we pick too precisely and overshoot - * (e.g. when we know 'mi' is closer to 'hi' than to - * 'lo', pick 'mi' that is higher than the target), we - * end up narrowing the search space by a smaller - * amount (i.e. the distance between 'mi' and 'hi') - * than what we would have (i.e. about half of 'lo' - * and 'hi'). Hedge our bets to pick 'mi' less - * aggressively, i.e. make 'mi' a bit closer to the - * middle than we would otherwise pick. - */ - kyv = (kyv * 6 + lov + hiv) / 8; - if (lov < hiv - 1) { - if (kyv == lov) - kyv++; - else if (kyv == hiv) - kyv--; - } - mi = (range - 1) * (kyv - lov) / (hiv - lov) + lo; - -#ifdef INDEX_DEBUG_LOOKUP - printf("lo %u hi %u rg %u mi %u ", lo, hi, range, mi); - printf("ofs %u lov %x, hiv %x, kyv %x\n", - ofs_0, lov, hiv, kyv); -#endif - - if (!(lo <= mi && mi < hi)) { - giterr_set(GITERR_INVALID, "Assertion failure. Binary search invariant is false"); - return -1; - } - - mi_key = base + elem_size * mi + key_offset; - cmp = memcmp(mi_key + ofs_0, key + ofs_0, 20 - ofs_0); - if (!cmp) - return mi; - if (cmp > 0) { - hi = mi; - hi_key = mi_key; - } else { - lo = mi + 1; - lo_key = mi_key + elem_size; - } - } while (lo < hi); - return -((int)lo)-1; -} diff --git a/src/sha1_lookup.h b/src/sha1_lookup.h deleted file mode 100644 index 9a353727303..00000000000 --- a/src/sha1_lookup.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_sha1_lookup_h__ -#define INCLUDE_sha1_lookup_h__ - -#include - -int sha1_entry_pos(const void *table, - size_t elem_size, - size_t key_offset, - unsigned lo, unsigned hi, unsigned nr, - const unsigned char *key); - -#endif diff --git a/src/signature.c b/src/signature.c deleted file mode 100644 index 3380097fff0..00000000000 --- a/src/signature.c +++ /dev/null @@ -1,340 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "signature.h" -#include "repository.h" -#include "git2/common.h" - -void git_signature_free(git_signature *sig) -{ - if (sig == NULL) - return; - - git__free(sig->name); - sig->name = NULL; - git__free(sig->email); - sig->email = NULL; - git__free(sig); -} - -static const char *skip_leading_spaces(const char *buffer, const char *buffer_end) -{ - while (*buffer == ' ' && buffer < buffer_end) - buffer++; - - return buffer; -} - -static const char *skip_trailing_spaces(const char *buffer_start, const char *buffer_end) -{ - while (*buffer_end == ' ' && buffer_end > buffer_start) - buffer_end--; - - return buffer_end; -} - -static int signature_error(const char *msg) -{ - giterr_set(GITERR_INVALID, "Failed to process signature - %s", msg); - return -1; -} - -static int process_trimming(const char *input, char **storage, const char *input_end, int fail_when_empty) -{ - const char *left, *right; - size_t trimmed_input_length; - - assert(storage); - - left = skip_leading_spaces(input, input_end); - right = skip_trailing_spaces(input, input_end - 1); - - if (right < left) { - if (fail_when_empty) - return signature_error("input is either empty of contains only spaces"); - - right = left - 1; - } - - trimmed_input_length = right - left + 1; - - *storage = git__malloc(trimmed_input_length + 1); - GITERR_CHECK_ALLOC(*storage); - - memcpy(*storage, left, trimmed_input_length); - (*storage)[trimmed_input_length] = 0; - - return 0; -} - -static bool contains_angle_brackets(const char *input) -{ - if (strchr(input, '<') != NULL) - return true; - - return strchr(input, '>') != NULL; -} - -int git_signature_new(git_signature **sig_out, const char *name, const char *email, git_time_t time, int offset) -{ - git_signature *p = NULL; - - assert(name && email); - - *sig_out = NULL; - - p = git__calloc(1, sizeof(git_signature)); - GITERR_CHECK_ALLOC(p); - - if (process_trimming(name, &p->name, name + strlen(name), 1) < 0 || - process_trimming(email, &p->email, email + strlen(email), 1) < 0) - { - git_signature_free(p); - return -1; - } - - if (contains_angle_brackets(p->email) || - contains_angle_brackets(p->name)) - { - git_signature_free(p); - return signature_error("Neither `name` nor `email` should contain angle brackets chars."); - } - - p->when.time = time; - p->when.offset = offset; - - *sig_out = p; - - return 0; -} - -git_signature *git_signature_dup(const git_signature *sig) -{ - git_signature *new; - if (git_signature_new(&new, sig->name, sig->email, sig->when.time, sig->when.offset) < 0) - return NULL; - return new; -} - -int git_signature_now(git_signature **sig_out, const char *name, const char *email) -{ - time_t now; - time_t offset; - struct tm *utc_tm; - git_signature *sig; - struct tm _utc; - - *sig_out = NULL; - - /* - * Get the current time as seconds since the epoch and - * transform that into a tm struct containing the time at - * UTC. Give that to mktime which considers it a local time - * (tm_isdst = -1 asks it to take DST into account) and gives - * us that time as seconds since the epoch. The difference - * between its return value and 'now' is our offset to UTC. - */ - time(&now); - utc_tm = p_gmtime_r(&now, &_utc); - utc_tm->tm_isdst = -1; - offset = (time_t)difftime(now, mktime(utc_tm)); - offset /= 60; - - if (git_signature_new(&sig, name, email, now, (int)offset) < 0) - return -1; - - *sig_out = sig; - - return 0; -} - -static int timezone_error(const char *msg) -{ - giterr_set(GITERR_INVALID, "Failed to parse TZ offset - %s", msg); - return -1; -} - -static int parse_timezone_offset(const char *buffer, int *offset_out) -{ - int dec_offset; - int mins, hours, offset; - - const char *offset_start; - const char *offset_end; - - offset_start = buffer; - - if (*offset_start == '\n') { - *offset_out = 0; - return 0; - } - - if (offset_start[0] != '-' && offset_start[0] != '+') - return timezone_error("does not start with '+' or '-'"); - - if (offset_start[1] < '0' || offset_start[1] > '9') - return timezone_error("expected initial digit"); - - if (git__strtol32(&dec_offset, offset_start + 1, &offset_end, 10) < 0) - return timezone_error("not a valid number"); - - if (offset_end - offset_start != 5) - return timezone_error("invalid length"); - - if (dec_offset > 1400) - return timezone_error("value too large"); - - hours = dec_offset / 100; - mins = dec_offset % 100; - - if (hours > 14) // see http://www.worldtimezone.com/faq.html - return timezone_error("hour value too large"); - - if (mins > 59) - return timezone_error("minutes value too large"); - - offset = (hours * 60) + mins; - - if (offset_start[0] == '-') - offset *= -1; - - *offset_out = offset; - - return 0; -} - -static int process_next_token(const char **buffer_out, char **storage, - const char *token_end, const char *right_boundary) -{ - int error = process_trimming(*buffer_out, storage, token_end, 0); - if (error < 0) - return error; - - *buffer_out = token_end + 1; - - if (*buffer_out > right_boundary) - return signature_error("signature is too short"); - - return 0; -} - -static const char *scan_for_previous_token(const char *buffer, const char *left_boundary) -{ - const char *start; - - if (buffer <= left_boundary) - return NULL; - - start = skip_trailing_spaces(left_boundary, buffer); - - /* Search for previous occurence of space */ - while (start[-1] != ' ' && start > left_boundary) - start--; - - return start; -} - -static int parse_time(git_time_t *time_out, const char *buffer) -{ - int error; - int64_t time; - - if (*buffer == '+' || *buffer == '-') { - giterr_set(GITERR_INVALID, "Failed while parsing time. '%s' actually looks like a timezone offset.", buffer); - return -1; - } - - error = git__strtol64(&time, buffer, &buffer, 10); - - if (!error) - *time_out = (git_time_t)time; - - return error; -} - -int git_signature__parse(git_signature *sig, const char **buffer_out, - const char *buffer_end, const char *header, char ender) -{ - const char *buffer = *buffer_out; - const char *line_end, *name_end, *email_end, *tz_start, *time_start; - int error = 0; - - memset(sig, 0, sizeof(git_signature)); - - if ((line_end = memchr(buffer, ender, buffer_end - buffer)) == NULL) - return signature_error("no newline given"); - - if (header) { - const size_t header_len = strlen(header); - - if (memcmp(buffer, header, header_len) != 0) - return signature_error("expected prefix doesn't match actual"); - - buffer += header_len; - } - - if (buffer > line_end) - return signature_error("signature too short"); - - if ((name_end = strchr(buffer, '<')) == NULL) - return signature_error("character '<' not allowed in signature"); - - if ((email_end = strchr(name_end, '>')) == NULL) - return signature_error("character '>' not allowed in signature"); - - if (email_end < name_end) - return signature_error("malformed e-mail"); - - error = process_next_token(&buffer, &sig->name, name_end, line_end); - if (error < 0) - return error; - - error = process_next_token(&buffer, &sig->email, email_end, line_end); - if (error < 0) - return error; - - tz_start = scan_for_previous_token(line_end - 1, buffer); - - if (tz_start == NULL) - goto clean_exit; /* No timezone nor date */ - - time_start = scan_for_previous_token(tz_start - 1, buffer); - if (time_start == NULL || parse_time(&sig->when.time, time_start) < 0) { - /* The tz_start might point at the time */ - parse_time(&sig->when.time, tz_start); - goto clean_exit; - } - - if (parse_timezone_offset(tz_start, &sig->when.offset) < 0) { - sig->when.time = 0; /* Bogus timezone, we reset the time */ - } - -clean_exit: - *buffer_out = line_end + 1; - return 0; -} - -void git_signature__writebuf(git_buf *buf, const char *header, const git_signature *sig) -{ - int offset, hours, mins; - char sign; - - offset = sig->when.offset; - sign = (sig->when.offset < 0) ? '-' : '+'; - - if (offset < 0) - offset = -offset; - - hours = offset / 60; - mins = offset % 60; - - git_buf_printf(buf, "%s%s <%s> %u %c%02d%02d\n", - header ? header : "", sig->name, sig->email, - (unsigned)sig->when.time, sign, hours, mins); -} - diff --git a/src/signature.h b/src/signature.h deleted file mode 100644 index 24655cbf578..00000000000 --- a/src/signature.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_signature_h__ -#define INCLUDE_signature_h__ - -#include "git2/common.h" -#include "git2/signature.h" -#include "repository.h" -#include - -int git_signature__parse(git_signature *sig, const char **buffer_out, const char *buffer_end, const char *header, char ender); -void git_signature__writebuf(git_buf *buf, const char *header, const git_signature *sig); - -#endif diff --git a/src/stash.c b/src/stash.c deleted file mode 100644 index 877af3312a9..00000000000 --- a/src/stash.c +++ /dev/null @@ -1,655 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "repository.h" -#include "commit.h" -#include "tree.h" -#include "reflog.h" -#include "git2/diff.h" -#include "git2/stash.h" -#include "git2/status.h" -#include "git2/checkout.h" -#include "signature.h" - -static int create_error(int error, const char *msg) -{ - giterr_set(GITERR_STASH, "Cannot stash changes - %s", msg); - return error; -} - -static int retrieve_head(git_reference **out, git_repository *repo) -{ - int error = git_repository_head(out, repo); - - if (error == GIT_EORPHANEDHEAD) - return create_error(error, "You do not have the initial commit yet."); - - return error; -} - -static int append_abbreviated_oid(git_buf *out, const git_oid *b_commit) -{ - char *formatted_oid; - - formatted_oid = git_oid_allocfmt(b_commit); - GITERR_CHECK_ALLOC(formatted_oid); - - git_buf_put(out, formatted_oid, 7); - git__free(formatted_oid); - - return git_buf_oom(out) ? -1 : 0; -} - -static int append_commit_description(git_buf *out, git_commit* commit) -{ - const char *message; - size_t pos = 0, len; - - if (append_abbreviated_oid(out, git_commit_id(commit)) < 0) - return -1; - - message = git_commit_message(commit); - len = strlen(message); - - /* TODO: Replace with proper commit short message - * when git_commit_message_short() is implemented. - */ - while (pos < len && message[pos] != '\n') - pos++; - - git_buf_putc(out, ' '); - git_buf_put(out, message, pos); - git_buf_putc(out, '\n'); - - return git_buf_oom(out) ? -1 : 0; -} - -static int retrieve_base_commit_and_message( - git_commit **b_commit, - git_buf *stash_message, - git_repository *repo) -{ - git_reference *head = NULL; - int error; - - if ((error = retrieve_head(&head, repo)) < 0) - return error; - - if (strcmp("HEAD", git_reference_name(head)) == 0) - error = git_buf_puts(stash_message, "(no branch): "); - else - error = git_buf_printf( - stash_message, - "%s: ", - git_reference_name(head) + strlen(GIT_REFS_HEADS_DIR)); - if (error < 0) - goto cleanup; - - if ((error = git_commit_lookup( - b_commit, repo, git_reference_target(head))) < 0) - goto cleanup; - - if ((error = append_commit_description(stash_message, *b_commit)) < 0) - goto cleanup; - -cleanup: - git_reference_free(head); - return error; -} - -static int build_tree_from_index(git_tree **out, git_index *index) -{ - int error; - git_oid i_tree_oid; - - if ((error = git_index_write_tree(&i_tree_oid, index)) < 0) - return -1; - - return git_tree_lookup(out, git_index_owner(index), &i_tree_oid); -} - -static int commit_index( - git_commit **i_commit, - git_index *index, - git_signature *stasher, - const char *message, - const git_commit *parent) -{ - git_tree *i_tree = NULL; - git_oid i_commit_oid; - git_buf msg = GIT_BUF_INIT; - int error; - - if ((error = build_tree_from_index(&i_tree, index)) < 0) - goto cleanup; - - if ((error = git_buf_printf(&msg, "index on %s\n", message)) < 0) - goto cleanup; - - if ((error = git_commit_create( - &i_commit_oid, - git_index_owner(index), - NULL, - stasher, - stasher, - NULL, - git_buf_cstr(&msg), - i_tree, - 1, - &parent)) < 0) - goto cleanup; - - error = git_commit_lookup(i_commit, git_index_owner(index), &i_commit_oid); - -cleanup: - git_tree_free(i_tree); - git_buf_free(&msg); - return error; -} - -struct cb_data { - git_index *index; - - int error; - - bool include_changed; - bool include_untracked; - bool include_ignored; -}; - -static int update_index_cb( - const git_diff_delta *delta, - float progress, - void *payload) -{ - struct cb_data *data = (struct cb_data *)payload; - const char *add_path = NULL; - - GIT_UNUSED(progress); - - switch (delta->status) { - case GIT_DELTA_IGNORED: - if (data->include_ignored) - add_path = delta->new_file.path; - break; - - case GIT_DELTA_UNTRACKED: - if (data->include_untracked) - add_path = delta->new_file.path; - break; - - case GIT_DELTA_ADDED: - case GIT_DELTA_MODIFIED: - if (data->include_changed) - add_path = delta->new_file.path; - break; - - case GIT_DELTA_DELETED: - if (!data->include_changed) - break; - if (git_index_find(NULL, data->index, delta->old_file.path) == 0) - data->error = git_index_remove( - data->index, delta->old_file.path, 0); - break; - - default: - /* Unimplemented */ - giterr_set( - GITERR_INVALID, - "Cannot update index. Unimplemented status (%d)", - delta->status); - data->error = -1; - break; - } - - if (add_path != NULL) - data->error = git_index_add_bypath(data->index, add_path); - - return data->error; -} - -static int build_untracked_tree( - git_tree **tree_out, - git_index *index, - git_commit *i_commit, - uint32_t flags) -{ - git_tree *i_tree = NULL; - git_diff_list *diff = NULL; - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - struct cb_data data = {0}; - int error; - - git_index_clear(index); - - data.index = index; - - if (flags & GIT_STASH_INCLUDE_UNTRACKED) { - opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | - GIT_DIFF_RECURSE_UNTRACKED_DIRS; - data.include_untracked = true; - } - - if (flags & GIT_STASH_INCLUDE_IGNORED) { - opts.flags |= GIT_DIFF_INCLUDE_IGNORED; - data.include_ignored = true; - } - - if ((error = git_commit_tree(&i_tree, i_commit)) < 0) - goto cleanup; - - if ((error = git_diff_tree_to_workdir( - &diff, git_index_owner(index), i_tree, &opts)) < 0) - goto cleanup; - - if ((error = git_diff_foreach( - diff, update_index_cb, NULL, NULL, &data)) < 0) - { - if (error == GIT_EUSER) - error = data.error; - goto cleanup; - } - - error = build_tree_from_index(tree_out, index); - -cleanup: - git_diff_list_free(diff); - git_tree_free(i_tree); - return error; -} - -static int commit_untracked( - git_commit **u_commit, - git_index *index, - git_signature *stasher, - const char *message, - git_commit *i_commit, - uint32_t flags) -{ - git_tree *u_tree = NULL; - git_oid u_commit_oid; - git_buf msg = GIT_BUF_INIT; - int error; - - if ((error = build_untracked_tree(&u_tree, index, i_commit, flags)) < 0) - goto cleanup; - - if ((error = git_buf_printf(&msg, "untracked files on %s\n", message)) < 0) - goto cleanup; - - if ((error = git_commit_create( - &u_commit_oid, - git_index_owner(index), - NULL, - stasher, - stasher, - NULL, - git_buf_cstr(&msg), - u_tree, - 0, - NULL)) < 0) - goto cleanup; - - error = git_commit_lookup(u_commit, git_index_owner(index), &u_commit_oid); - -cleanup: - git_tree_free(u_tree); - git_buf_free(&msg); - return error; -} - -static int build_workdir_tree( - git_tree **tree_out, - git_index *index, - git_commit *b_commit) -{ - git_repository *repo = git_index_owner(index); - git_tree *b_tree = NULL; - git_diff_list *diff = NULL, *diff2 = NULL; - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - struct cb_data data = {0}; - int error; - - if ((error = git_commit_tree(&b_tree, b_commit)) < 0) - goto cleanup; - - if ((error = git_diff_tree_to_index(&diff, repo, b_tree, NULL, &opts)) < 0) - goto cleanup; - - if ((error = git_diff_index_to_workdir(&diff2, repo, NULL, &opts)) < 0) - goto cleanup; - - if ((error = git_diff_merge(diff, diff2)) < 0) - goto cleanup; - - data.index = index; - data.include_changed = true; - - if ((error = git_diff_foreach( - diff, update_index_cb, NULL, NULL, &data)) < 0) - { - if (error == GIT_EUSER) - error = data.error; - goto cleanup; - } - - - if ((error = build_tree_from_index(tree_out, index)) < 0) - goto cleanup; - -cleanup: - git_diff_list_free(diff); - git_diff_list_free(diff2); - git_tree_free(b_tree); - - return error; -} - -static int commit_worktree( - git_oid *w_commit_oid, - git_index *index, - git_signature *stasher, - const char *message, - git_commit *i_commit, - git_commit *b_commit, - git_commit *u_commit) -{ - int error = 0; - git_tree *w_tree = NULL, *i_tree = NULL; - const git_commit *parents[] = { NULL, NULL, NULL }; - - parents[0] = b_commit; - parents[1] = i_commit; - parents[2] = u_commit; - - if ((error = git_commit_tree(&i_tree, i_commit)) < 0) - goto cleanup; - - if ((error = git_index_read_tree(index, i_tree)) < 0) - goto cleanup; - - if ((error = build_workdir_tree(&w_tree, index, b_commit)) < 0) - goto cleanup; - - error = git_commit_create( - w_commit_oid, - git_index_owner(index), - NULL, - stasher, - stasher, - NULL, - message, - w_tree, - u_commit ? 3 : 2, - parents); - -cleanup: - git_tree_free(i_tree); - git_tree_free(w_tree); - return error; -} - -static int prepare_worktree_commit_message( - git_buf* msg, - const char *user_message) -{ - git_buf buf = GIT_BUF_INIT; - int error; - - if ((error = git_buf_set(&buf, git_buf_cstr(msg), git_buf_len(msg))) < 0) - return error; - - git_buf_clear(msg); - - if (!user_message) - git_buf_printf(msg, "WIP on %s", git_buf_cstr(&buf)); - else { - const char *colon; - - if ((colon = strchr(git_buf_cstr(&buf), ':')) == NULL) - goto cleanup; - - git_buf_puts(msg, "On "); - git_buf_put(msg, git_buf_cstr(&buf), colon - buf.ptr); - git_buf_printf(msg, ": %s\n", user_message); - } - - error = (git_buf_oom(msg) || git_buf_oom(&buf)) ? -1 : 0; - -cleanup: - git_buf_free(&buf); - - return error; -} - -static int update_reflog( - git_oid *w_commit_oid, - git_repository *repo, - git_signature *stasher, - const char *message) -{ - git_reference *stash = NULL; - git_reflog *reflog = NULL; - int error; - - if ((error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1)) < 0) - goto cleanup; - - if ((error = git_reflog_read(&reflog, stash)) < 0) - goto cleanup; - - if ((error = git_reflog_append(reflog, w_commit_oid, stasher, message)) < 0) - goto cleanup; - - if ((error = git_reflog_write(reflog)) < 0) - goto cleanup; - -cleanup: - git_reference_free(stash); - git_reflog_free(reflog); - return error; -} - -static int is_dirty_cb(const char *path, unsigned int status, void *payload) -{ - GIT_UNUSED(path); - GIT_UNUSED(status); - GIT_UNUSED(payload); - - return 1; -} - -static int ensure_there_are_changes_to_stash( - git_repository *repo, - bool include_untracked_files, - bool include_ignored_files) -{ - int error; - git_status_options opts = GIT_STATUS_OPTIONS_INIT; - - opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; - if (include_untracked_files) - opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | - GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS; - - if (include_ignored_files) - opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED; - - error = git_status_foreach_ext(repo, &opts, is_dirty_cb, NULL); - - if (error == GIT_EUSER) - return 0; - - if (!error) - return create_error(GIT_ENOTFOUND, "There is nothing to stash."); - - return error; -} - -static int reset_index_and_workdir( - git_repository *repo, - git_commit *commit, - bool remove_untracked) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - opts.checkout_strategy = GIT_CHECKOUT_FORCE; - - if (remove_untracked) - opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_UNTRACKED; - - return git_checkout_tree(repo, (git_object *)commit, &opts); -} - -int git_stash_save( - git_oid *out, - git_repository *repo, - git_signature *stasher, - const char *message, - uint32_t flags) -{ - git_index *index = NULL; - git_commit *b_commit = NULL, *i_commit = NULL, *u_commit = NULL; - git_buf msg = GIT_BUF_INIT; - int error; - - assert(out && repo && stasher); - - if ((error = git_repository__ensure_not_bare(repo, "stash save")) < 0) - return error; - - if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0) - goto cleanup; - - if ((error = ensure_there_are_changes_to_stash( - repo, - (flags & GIT_STASH_INCLUDE_UNTRACKED) != 0, - (flags & GIT_STASH_INCLUDE_IGNORED) != 0)) < 0) - goto cleanup; - - if ((error = git_repository_index(&index, repo)) < 0) - goto cleanup; - - if ((error = commit_index( - &i_commit, index, stasher, git_buf_cstr(&msg), b_commit)) < 0) - goto cleanup; - - if ((flags & (GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)) && - (error = commit_untracked( - &u_commit, index, stasher, git_buf_cstr(&msg), - i_commit, flags)) < 0) - goto cleanup; - - if ((error = prepare_worktree_commit_message(&msg, message)) < 0) - goto cleanup; - - if ((error = commit_worktree( - out, index, stasher, git_buf_cstr(&msg), - i_commit, b_commit, u_commit)) < 0) - goto cleanup; - - git_buf_rtrim(&msg); - - if ((error = update_reflog(out, repo, stasher, git_buf_cstr(&msg))) < 0) - goto cleanup; - - if ((error = reset_index_and_workdir( - repo, - ((flags & GIT_STASH_KEEP_INDEX) != 0) ? i_commit : b_commit, - (flags & GIT_STASH_INCLUDE_UNTRACKED) != 0)) < 0) - goto cleanup; - -cleanup: - - git_buf_free(&msg); - git_commit_free(i_commit); - git_commit_free(b_commit); - git_commit_free(u_commit); - git_index_free(index); - - return error; -} - -int git_stash_foreach( - git_repository *repo, - git_stash_cb callback, - void *payload) -{ - git_reference *stash; - git_reflog *reflog = NULL; - int error; - size_t i, max; - const git_reflog_entry *entry; - - error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE); - if (error == GIT_ENOTFOUND) - return 0; - if (error < 0) - goto cleanup; - - if ((error = git_reflog_read(&reflog, stash)) < 0) - goto cleanup; - - max = git_reflog_entrycount(reflog); - for (i = 0; i < max; i++) { - entry = git_reflog_entry_byindex(reflog, i); - - if (callback(i, - git_reflog_entry_message(entry), - git_reflog_entry_id_new(entry), - payload)) { - error = GIT_EUSER; - break; - } - } - -cleanup: - git_reference_free(stash); - git_reflog_free(reflog); - return error; -} - -int git_stash_drop( - git_repository *repo, - size_t index) -{ - git_reference *stash; - git_reflog *reflog = NULL; - size_t max; - int error; - - if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0) - return error; - - if ((error = git_reflog_read(&reflog, stash)) < 0) - goto cleanup; - - max = git_reflog_entrycount(reflog); - - if (index > max - 1) { - error = GIT_ENOTFOUND; - giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index); - goto cleanup; - } - - if ((error = git_reflog_drop(reflog, index, true)) < 0) - goto cleanup; - - if ((error = git_reflog_write(reflog)) < 0) - goto cleanup; - - if (max == 1) { - error = git_reference_delete(stash); - stash = NULL; - } - -cleanup: - git_reference_free(stash); - git_reflog_free(reflog); - return error; -} diff --git a/src/status.c b/src/status.c deleted file mode 100644 index 282cb396b10..00000000000 --- a/src/status.c +++ /dev/null @@ -1,293 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "git2.h" -#include "fileops.h" -#include "hash.h" -#include "vector.h" -#include "tree.h" -#include "git2/status.h" -#include "repository.h" -#include "ignore.h" - -#include "git2/diff.h" -#include "diff.h" -#include "diff_output.h" - -static unsigned int index_delta2status(git_delta_t index_status) -{ - unsigned int st = GIT_STATUS_CURRENT; - - switch (index_status) { - case GIT_DELTA_ADDED: - case GIT_DELTA_COPIED: - st = GIT_STATUS_INDEX_NEW; - break; - case GIT_DELTA_DELETED: - st = GIT_STATUS_INDEX_DELETED; - break; - case GIT_DELTA_MODIFIED: - st = GIT_STATUS_INDEX_MODIFIED; - break; - case GIT_DELTA_RENAMED: - st = GIT_STATUS_INDEX_RENAMED; - break; - case GIT_DELTA_TYPECHANGE: - st = GIT_STATUS_INDEX_TYPECHANGE; - break; - default: - break; - } - - return st; -} - -static unsigned int workdir_delta2status(git_delta_t workdir_status) -{ - unsigned int st = GIT_STATUS_CURRENT; - - switch (workdir_status) { - case GIT_DELTA_ADDED: - case GIT_DELTA_RENAMED: - case GIT_DELTA_COPIED: - case GIT_DELTA_UNTRACKED: - st = GIT_STATUS_WT_NEW; - break; - case GIT_DELTA_DELETED: - st = GIT_STATUS_WT_DELETED; - break; - case GIT_DELTA_MODIFIED: - st = GIT_STATUS_WT_MODIFIED; - break; - case GIT_DELTA_IGNORED: - st = GIT_STATUS_IGNORED; - break; - case GIT_DELTA_TYPECHANGE: - st = GIT_STATUS_WT_TYPECHANGE; - break; - default: - break; - } - - return st; -} - -typedef struct { - git_status_cb cb; - void *payload; -} status_user_callback; - -static int status_invoke_cb( - git_diff_delta *i2h, git_diff_delta *w2i, void *payload) -{ - status_user_callback *usercb = payload; - const char *path = NULL; - unsigned int status = 0; - - if (w2i) { - path = w2i->old_file.path; - status |= workdir_delta2status(w2i->status); - } - if (i2h) { - path = i2h->old_file.path; - status |= index_delta2status(i2h->status); - } - - return usercb->cb(path, status, usercb->payload); -} - -int git_status_foreach_ext( - git_repository *repo, - const git_status_options *opts, - git_status_cb cb, - void *payload) -{ - int err = 0; - git_diff_options diffopt = GIT_DIFF_OPTIONS_INIT; - git_diff_list *idx2head = NULL, *wd2idx = NULL; - git_tree *head = NULL; - git_status_show_t show = - opts ? opts->show : GIT_STATUS_SHOW_INDEX_AND_WORKDIR; - status_user_callback usercb; - - assert(show <= GIT_STATUS_SHOW_INDEX_THEN_WORKDIR); - - GITERR_CHECK_VERSION(opts, GIT_STATUS_OPTIONS_VERSION, "git_status_options"); - - if (show != GIT_STATUS_SHOW_INDEX_ONLY && - (err = git_repository__ensure_not_bare(repo, "status")) < 0) - return err; - - /* if there is no HEAD, that's okay - we'll make an empty iterator */ - if (((err = git_repository_head_tree(&head, repo)) < 0) && - !(err == GIT_ENOTFOUND || err == GIT_EORPHANEDHEAD)) - return err; - - memcpy(&diffopt.pathspec, &opts->pathspec, sizeof(diffopt.pathspec)); - - diffopt.flags = GIT_DIFF_INCLUDE_TYPECHANGE; - - if ((opts->flags & GIT_STATUS_OPT_INCLUDE_UNTRACKED) != 0) - diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNTRACKED; - if ((opts->flags & GIT_STATUS_OPT_INCLUDE_IGNORED) != 0) - diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_IGNORED; - if ((opts->flags & GIT_STATUS_OPT_INCLUDE_UNMODIFIED) != 0) - diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNMODIFIED; - if ((opts->flags & GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS) != 0) - diffopt.flags = diffopt.flags | GIT_DIFF_RECURSE_UNTRACKED_DIRS; - if ((opts->flags & GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH) != 0) - diffopt.flags = diffopt.flags | GIT_DIFF_DISABLE_PATHSPEC_MATCH; - /* TODO: support EXCLUDE_SUBMODULES flag */ - - if (show != GIT_STATUS_SHOW_WORKDIR_ONLY && - (err = git_diff_tree_to_index(&idx2head, repo, head, NULL, &diffopt)) < 0) - goto cleanup; - - if (show != GIT_STATUS_SHOW_INDEX_ONLY && - (err = git_diff_index_to_workdir(&wd2idx, repo, NULL, &diffopt)) < 0) - goto cleanup; - - usercb.cb = cb; - usercb.payload = payload; - - if (show == GIT_STATUS_SHOW_INDEX_THEN_WORKDIR) { - if ((err = git_diff__paired_foreach( - idx2head, NULL, status_invoke_cb, &usercb)) < 0) - goto cleanup; - - git_diff_list_free(idx2head); - idx2head = NULL; - } - - err = git_diff__paired_foreach(idx2head, wd2idx, status_invoke_cb, &usercb); - -cleanup: - git_tree_free(head); - git_diff_list_free(idx2head); - git_diff_list_free(wd2idx); - - if (err == GIT_EUSER) - giterr_clear(); - - return err; -} - -int git_status_foreach( - git_repository *repo, - git_status_cb callback, - void *payload) -{ - git_status_options opts = GIT_STATUS_OPTIONS_INIT; - - opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; - opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED | - GIT_STATUS_OPT_INCLUDE_UNTRACKED | - GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS; - - return git_status_foreach_ext(repo, &opts, callback, payload); -} - -struct status_file_info { - char *expected; - unsigned int count; - unsigned int status; - int fnm_flags; - int ambiguous; -}; - -static int get_one_status(const char *path, unsigned int status, void *data) -{ - struct status_file_info *sfi = data; - int (*strcomp)(const char *a, const char *b); - - sfi->count++; - sfi->status = status; - - strcomp = (sfi->fnm_flags & FNM_CASEFOLD) ? git__strcasecmp : git__strcmp; - - if (sfi->count > 1 || - (strcomp(sfi->expected, path) != 0 && - p_fnmatch(sfi->expected, path, sfi->fnm_flags) != 0)) - { - sfi->ambiguous = true; - return GIT_EAMBIGUOUS; - } - - return 0; -} - -int git_status_file( - unsigned int *status_flags, - git_repository *repo, - const char *path) -{ - int error; - git_status_options opts = GIT_STATUS_OPTIONS_INIT; - struct status_file_info sfi = {0}; - git_index *index; - - assert(status_flags && repo && path); - - if ((error = git_repository_index__weakptr(&index, repo)) < 0) - return error; - - if ((sfi.expected = git__strdup(path)) == NULL) - return -1; - if (index->ignore_case) - sfi.fnm_flags = FNM_CASEFOLD; - - opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; - opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED | - GIT_STATUS_OPT_INCLUDE_UNTRACKED | - GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS | - GIT_STATUS_OPT_INCLUDE_UNMODIFIED; - opts.pathspec.count = 1; - opts.pathspec.strings = &sfi.expected; - - error = git_status_foreach_ext(repo, &opts, get_one_status, &sfi); - - if (error < 0 && sfi.ambiguous) { - giterr_set(GITERR_INVALID, - "Ambiguous path '%s' given to git_status_file", sfi.expected); - error = GIT_EAMBIGUOUS; - } - - if (!error && !sfi.count) { - git_buf full = GIT_BUF_INIT; - - /* if the file actually exists and we still did not get a callback - * for it, then it must be contained inside an ignored directory, so - * mark it as such instead of generating an error. - */ - if (!git_buf_joinpath(&full, git_repository_workdir(repo), path) && - git_path_exists(full.ptr)) - sfi.status = GIT_STATUS_IGNORED; - else { - giterr_set(GITERR_INVALID, - "Attempt to get status of nonexistent file '%s'", path); - error = GIT_ENOTFOUND; - } - - git_buf_free(&full); - } - - *status_flags = sfi.status; - - git__free(sfi.expected); - - return error; -} - -int git_status_should_ignore( - int *ignored, - git_repository *repo, - const char *path) -{ - return git_ignore_path_is_ignored(ignored, repo, path); -} - diff --git a/src/strmap.h b/src/strmap.h deleted file mode 100644 index 44176a0fcf5..00000000000 --- a/src/strmap.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_strmap_h__ -#define INCLUDE_strmap_h__ - -#include "common.h" - -#define kmalloc git__malloc -#define kcalloc git__calloc -#define krealloc git__realloc -#define kfree git__free -#include "khash.h" - -__KHASH_TYPE(str, const char *, void *); -typedef khash_t(str) git_strmap; - -#define GIT__USE_STRMAP \ - __KHASH_IMPL(str, static kh_inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal) - -#define git_strmap_alloc() kh_init(str) -#define git_strmap_free(h) kh_destroy(str, h), h = NULL -#define git_strmap_clear(h) kh_clear(str, h) - -#define git_strmap_num_entries(h) kh_size(h) - -#define git_strmap_lookup_index(h, k) kh_get(str, h, k) -#define git_strmap_valid_index(h, idx) (idx != kh_end(h)) - -#define git_strmap_exists(h, k) (kh_get(str, h, k) != kh_end(h)) - -#define git_strmap_value_at(h, idx) kh_val(h, idx) -#define git_strmap_set_value_at(h, idx, v) kh_val(h, idx) = v -#define git_strmap_delete_at(h, idx) kh_del(str, h, idx) - -#define git_strmap_insert(h, key, val, rval) do { \ - khiter_t __pos = kh_put(str, h, key, &rval); \ - if (rval >= 0) { \ - if (rval == 0) kh_key(h, __pos) = key; \ - kh_val(h, __pos) = val; \ - } } while (0) - -#define git_strmap_insert2(h, key, val, oldv, rval) do { \ - khiter_t __pos = kh_put(str, h, key, &rval); \ - if (rval >= 0) { \ - if (rval == 0) { \ - oldv = kh_val(h, __pos); \ - kh_key(h, __pos) = key; \ - } else { oldv = NULL; } \ - kh_val(h, __pos) = val; \ - } } while (0) - -#define git_strmap_delete(h, key) do { \ - khiter_t __pos = git_strmap_lookup_index(h, key); \ - if (git_strmap_valid_index(h, __pos)) \ - git_strmap_delete_at(h, __pos); } while (0) - -#define git_strmap_foreach kh_foreach -#define git_strmap_foreach_value kh_foreach_value - -#endif diff --git a/src/submodule.c b/src/submodule.c deleted file mode 100644 index 3593064980d..00000000000 --- a/src/submodule.c +++ /dev/null @@ -1,1511 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "git2/config.h" -#include "git2/types.h" -#include "git2/repository.h" -#include "git2/index.h" -#include "git2/submodule.h" -#include "buffer.h" -#include "buf_text.h" -#include "vector.h" -#include "posix.h" -#include "config_file.h" -#include "config.h" -#include "repository.h" -#include "submodule.h" -#include "tree.h" -#include "iterator.h" - -#define GIT_MODULES_FILE ".gitmodules" - -static git_cvar_map _sm_update_map[] = { - {GIT_CVAR_STRING, "checkout", GIT_SUBMODULE_UPDATE_CHECKOUT}, - {GIT_CVAR_STRING, "rebase", GIT_SUBMODULE_UPDATE_REBASE}, - {GIT_CVAR_STRING, "merge", GIT_SUBMODULE_UPDATE_MERGE}, - {GIT_CVAR_STRING, "none", GIT_SUBMODULE_UPDATE_NONE}, -}; - -static git_cvar_map _sm_ignore_map[] = { - {GIT_CVAR_STRING, "none", GIT_SUBMODULE_IGNORE_NONE}, - {GIT_CVAR_STRING, "untracked", GIT_SUBMODULE_IGNORE_UNTRACKED}, - {GIT_CVAR_STRING, "dirty", GIT_SUBMODULE_IGNORE_DIRTY}, - {GIT_CVAR_STRING, "all", GIT_SUBMODULE_IGNORE_ALL}, -}; - -static kh_inline khint_t str_hash_no_trailing_slash(const char *s) -{ - khint_t h; - - for (h = 0; *s; ++s) - if (s[1] != '\0' || *s != '/') - h = (h << 5) - h + *s; - - return h; -} - -static kh_inline int str_equal_no_trailing_slash(const char *a, const char *b) -{ - size_t alen = a ? strlen(a) : 0; - size_t blen = b ? strlen(b) : 0; - - if (alen > 0 && a[alen - 1] == '/') - alen--; - if (blen > 0 && b[blen - 1] == '/') - blen--; - - return (alen == blen && strncmp(a, b, alen) == 0); -} - -__KHASH_IMPL( - str, static kh_inline, const char *, void *, 1, - str_hash_no_trailing_slash, str_equal_no_trailing_slash); - -static int load_submodule_config(git_repository *repo); -static git_config_backend *open_gitmodules(git_repository *, bool, const git_oid *); -static int lookup_head_remote(git_buf *url, git_repository *repo); -static int submodule_get(git_submodule **, git_repository *, const char *, const char *); -static void submodule_release(git_submodule *sm, int decr); -static int submodule_load_from_index(git_repository *, const git_index_entry *); -static int submodule_load_from_head(git_repository*, const char*, const git_oid*); -static int submodule_load_from_config(const git_config_entry *, void *); -static int submodule_load_from_wd_lite(git_submodule *, const char *, void *); -static int submodule_update_config(git_submodule *, const char *, const char *, bool, bool); -static void submodule_mode_mismatch(git_repository *, const char *, unsigned int); -static int submodule_index_status(unsigned int *status, git_submodule *sm); -static int submodule_wd_status(unsigned int *status, git_submodule *sm); - -static int submodule_cmp(const void *a, const void *b) -{ - return strcmp(((git_submodule *)a)->name, ((git_submodule *)b)->name); -} - -static int submodule_config_key_trunc_puts(git_buf *key, const char *suffix) -{ - ssize_t idx = git_buf_rfind(key, '.'); - git_buf_truncate(key, (size_t)(idx + 1)); - return git_buf_puts(key, suffix); -} - -/* - * PUBLIC APIS - */ - -int git_submodule_lookup( - git_submodule **sm_ptr, /* NULL if user only wants to test existence */ - git_repository *repo, - const char *name) /* trailing slash is allowed */ -{ - int error; - khiter_t pos; - - assert(repo && name); - - if ((error = load_submodule_config(repo)) < 0) - return error; - - pos = git_strmap_lookup_index(repo->submodules, name); - - if (!git_strmap_valid_index(repo->submodules, pos)) { - error = GIT_ENOTFOUND; - - /* check if a plausible submodule exists at path */ - if (git_repository_workdir(repo)) { - git_buf path = GIT_BUF_INIT; - - if (git_buf_joinpath(&path, git_repository_workdir(repo), name) < 0) - return -1; - - if (git_path_contains_dir(&path, DOT_GIT)) - error = GIT_EEXISTS; - - git_buf_free(&path); - } - - return error; - } - - if (sm_ptr) - *sm_ptr = git_strmap_value_at(repo->submodules, pos); - - return 0; -} - -int git_submodule_foreach( - git_repository *repo, - int (*callback)(git_submodule *sm, const char *name, void *payload), - void *payload) -{ - int error; - git_submodule *sm; - git_vector seen = GIT_VECTOR_INIT; - seen._cmp = submodule_cmp; - - assert(repo && callback); - - if ((error = load_submodule_config(repo)) < 0) - return error; - - git_strmap_foreach_value(repo->submodules, sm, { - /* Usually the following will not come into play - it just prevents - * us from issuing a callback twice for a submodule where the name - * and path are not the same. - */ - if (sm->refcount > 1) { - if (git_vector_bsearch(NULL, &seen, sm) != GIT_ENOTFOUND) - continue; - if ((error = git_vector_insert(&seen, sm)) < 0) - break; - } - - if (callback(sm, sm->name, payload)) { - giterr_clear(); - error = GIT_EUSER; - break; - } - }); - - git_vector_free(&seen); - - return error; -} - -void git_submodule_config_free(git_repository *repo) -{ - git_strmap *smcfg; - git_submodule *sm; - - assert(repo); - - smcfg = repo->submodules; - repo->submodules = NULL; - - if (smcfg == NULL) - return; - - git_strmap_foreach_value(smcfg, sm, { - submodule_release(sm,1); - }); - git_strmap_free(smcfg); -} - -int git_submodule_add_setup( - git_submodule **submodule, - git_repository *repo, - const char *url, - const char *path, - int use_gitlink) -{ - int error = 0; - git_config_backend *mods = NULL; - git_submodule *sm; - git_buf name = GIT_BUF_INIT, real_url = GIT_BUF_INIT; - git_repository_init_options initopt = GIT_REPOSITORY_INIT_OPTIONS_INIT; - git_repository *subrepo = NULL; - - assert(repo && url && path); - - /* see if there is already an entry for this submodule */ - - if (git_submodule_lookup(&sm, repo, path) < 0) - giterr_clear(); - else { - giterr_set(GITERR_SUBMODULE, - "Attempt to add a submodule that already exists"); - return GIT_EEXISTS; - } - - /* resolve parameters */ - - if (url[0] == '.' && (url[1] == '/' || (url[1] == '.' && url[2] == '/'))) { - if (!(error = lookup_head_remote(&real_url, repo))) - error = git_path_apply_relative(&real_url, url); - } else if (strchr(url, ':') != NULL || url[0] == '/') { - error = git_buf_sets(&real_url, url); - } else { - giterr_set(GITERR_SUBMODULE, "Invalid format for submodule URL"); - error = -1; - } - if (error) - goto cleanup; - - /* validate and normalize path */ - - if (git__prefixcmp(path, git_repository_workdir(repo)) == 0) - path += strlen(git_repository_workdir(repo)); - - if (git_path_root(path) >= 0) { - giterr_set(GITERR_SUBMODULE, "Submodule path must be a relative path"); - error = -1; - goto cleanup; - } - - /* update .gitmodules */ - - if ((mods = open_gitmodules(repo, true, NULL)) == NULL) { - giterr_set(GITERR_SUBMODULE, - "Adding submodules to a bare repository is not supported (for now)"); - return -1; - } - - if ((error = git_buf_printf(&name, "submodule.%s.path", path)) < 0 || - (error = git_config_file_set_string(mods, name.ptr, path)) < 0) - goto cleanup; - - if ((error = submodule_config_key_trunc_puts(&name, "url")) < 0 || - (error = git_config_file_set_string(mods, name.ptr, real_url.ptr)) < 0) - goto cleanup; - - git_buf_clear(&name); - - /* init submodule repository and add origin remote as needed */ - - error = git_buf_joinpath(&name, git_repository_workdir(repo), path); - if (error < 0) - goto cleanup; - - /* New style: sub-repo goes in /modules// with a - * gitlink in the sub-repo workdir directory to that repository - * - * Old style: sub-repo goes directly into repo//.git/ - */ - - initopt.flags = GIT_REPOSITORY_INIT_MKPATH | - GIT_REPOSITORY_INIT_NO_REINIT; - initopt.origin_url = real_url.ptr; - - if (git_path_exists(name.ptr) && - git_path_contains(&name, DOT_GIT)) - { - /* repo appears to already exist - reinit? */ - } - else if (use_gitlink) { - git_buf repodir = GIT_BUF_INIT; - - error = git_buf_join_n( - &repodir, '/', 3, git_repository_path(repo), "modules", path); - if (error < 0) - goto cleanup; - - initopt.workdir_path = name.ptr; - initopt.flags |= GIT_REPOSITORY_INIT_NO_DOTGIT_DIR; - - error = git_repository_init_ext(&subrepo, repodir.ptr, &initopt); - - git_buf_free(&repodir); - } - else { - error = git_repository_init_ext(&subrepo, name.ptr, &initopt); - } - if (error < 0) - goto cleanup; - - /* add submodule to hash and "reload" it */ - - if (!(error = submodule_get(&sm, repo, path, NULL)) && - !(error = git_submodule_reload(sm))) - error = git_submodule_init(sm, false); - -cleanup: - if (submodule != NULL) - *submodule = !error ? sm : NULL; - - if (mods != NULL) - git_config_file_free(mods); - git_repository_free(subrepo); - git_buf_free(&real_url); - git_buf_free(&name); - - return error; -} - -int git_submodule_add_finalize(git_submodule *sm) -{ - int error; - git_index *index; - - assert(sm); - - if ((error = git_repository_index__weakptr(&index, sm->owner)) < 0 || - (error = git_index_add_bypath(index, GIT_MODULES_FILE)) < 0) - return error; - - return git_submodule_add_to_index(sm, true); -} - -int git_submodule_add_to_index(git_submodule *sm, int write_index) -{ - int error; - git_repository *repo, *sm_repo = NULL; - git_index *index; - git_buf path = GIT_BUF_INIT; - git_commit *head; - git_index_entry entry; - struct stat st; - - assert(sm); - - repo = sm->owner; - - /* force reload of wd OID by git_submodule_open */ - sm->flags = sm->flags & ~GIT_SUBMODULE_STATUS__WD_OID_VALID; - - if ((error = git_repository_index__weakptr(&index, repo)) < 0 || - (error = git_buf_joinpath( - &path, git_repository_workdir(repo), sm->path)) < 0 || - (error = git_submodule_open(&sm_repo, sm)) < 0) - goto cleanup; - - /* read stat information for submodule working directory */ - if (p_stat(path.ptr, &st) < 0) { - giterr_set(GITERR_SUBMODULE, - "Cannot add submodule without working directory"); - error = -1; - goto cleanup; - } - - memset(&entry, 0, sizeof(entry)); - entry.path = sm->path; - git_index_entry__init_from_stat(&entry, &st); - - /* calling git_submodule_open will have set sm->wd_oid if possible */ - if ((sm->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID) == 0) { - giterr_set(GITERR_SUBMODULE, - "Cannot add submodule without HEAD to index"); - error = -1; - goto cleanup; - } - git_oid_cpy(&entry.oid, &sm->wd_oid); - - if ((error = git_commit_lookup(&head, sm_repo, &sm->wd_oid)) < 0) - goto cleanup; - - entry.ctime.seconds = git_commit_time(head); - entry.ctime.nanoseconds = 0; - entry.mtime.seconds = git_commit_time(head); - entry.mtime.nanoseconds = 0; - - git_commit_free(head); - - /* add it */ - error = git_index_add(index, &entry); - - /* write it, if requested */ - if (!error && write_index) { - error = git_index_write(index); - - if (!error) - git_oid_cpy(&sm->index_oid, &sm->wd_oid); - } - -cleanup: - git_repository_free(sm_repo); - git_buf_free(&path); - return error; -} - -int git_submodule_save(git_submodule *submodule) -{ - int error = 0; - git_config_backend *mods; - git_buf key = GIT_BUF_INIT; - - assert(submodule); - - mods = open_gitmodules(submodule->owner, true, NULL); - if (!mods) { - giterr_set(GITERR_SUBMODULE, - "Adding submodules to a bare repository is not supported (for now)"); - return -1; - } - - if ((error = git_buf_printf(&key, "submodule.%s.", submodule->name)) < 0) - goto cleanup; - - /* save values for path, url, update, ignore, fetchRecurseSubmodules */ - - if ((error = submodule_config_key_trunc_puts(&key, "path")) < 0 || - (error = git_config_file_set_string(mods, key.ptr, submodule->path)) < 0) - goto cleanup; - - if ((error = submodule_config_key_trunc_puts(&key, "url")) < 0 || - (error = git_config_file_set_string(mods, key.ptr, submodule->url)) < 0) - goto cleanup; - - if (!(error = submodule_config_key_trunc_puts(&key, "update")) && - submodule->update != GIT_SUBMODULE_UPDATE_DEFAULT) - { - const char *val = (submodule->update == GIT_SUBMODULE_UPDATE_CHECKOUT) ? - NULL : _sm_update_map[submodule->update].str_match; - error = git_config_file_set_string(mods, key.ptr, val); - } - if (error < 0) - goto cleanup; - - if (!(error = submodule_config_key_trunc_puts(&key, "ignore")) && - submodule->ignore != GIT_SUBMODULE_IGNORE_DEFAULT) - { - const char *val = (submodule->ignore == GIT_SUBMODULE_IGNORE_NONE) ? - NULL : _sm_ignore_map[submodule->ignore].str_match; - error = git_config_file_set_string(mods, key.ptr, val); - } - if (error < 0) - goto cleanup; - - if ((error = submodule_config_key_trunc_puts( - &key, "fetchRecurseSubmodules")) < 0 || - (error = git_config_file_set_string( - mods, key.ptr, submodule->fetch_recurse ? "true" : "false")) < 0) - goto cleanup; - - /* update internal defaults */ - - submodule->ignore_default = submodule->ignore; - submodule->update_default = submodule->update; - submodule->flags |= GIT_SUBMODULE_STATUS_IN_CONFIG; - -cleanup: - if (mods != NULL) - git_config_file_free(mods); - git_buf_free(&key); - - return error; -} - -git_repository *git_submodule_owner(git_submodule *submodule) -{ - assert(submodule); - return submodule->owner; -} - -const char *git_submodule_name(git_submodule *submodule) -{ - assert(submodule); - return submodule->name; -} - -const char *git_submodule_path(git_submodule *submodule) -{ - assert(submodule); - return submodule->path; -} - -const char *git_submodule_url(git_submodule *submodule) -{ - assert(submodule); - return submodule->url; -} - -int git_submodule_set_url(git_submodule *submodule, const char *url) -{ - assert(submodule && url); - - git__free(submodule->url); - - submodule->url = git__strdup(url); - GITERR_CHECK_ALLOC(submodule->url); - - return 0; -} - -const git_oid *git_submodule_index_id(git_submodule *submodule) -{ - assert(submodule); - - if (submodule->flags & GIT_SUBMODULE_STATUS__INDEX_OID_VALID) - return &submodule->index_oid; - else - return NULL; -} - -const git_oid *git_submodule_head_id(git_submodule *submodule) -{ - assert(submodule); - - if (submodule->flags & GIT_SUBMODULE_STATUS__HEAD_OID_VALID) - return &submodule->head_oid; - else - return NULL; -} - -const git_oid *git_submodule_wd_id(git_submodule *submodule) -{ - assert(submodule); - - if (!(submodule->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID)) { - git_repository *subrepo; - - /* calling submodule open grabs the HEAD OID if possible */ - if (!git_submodule_open(&subrepo, submodule)) - git_repository_free(subrepo); - else - giterr_clear(); - } - - if (submodule->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID) - return &submodule->wd_oid; - else - return NULL; -} - -git_submodule_ignore_t git_submodule_ignore(git_submodule *submodule) -{ - assert(submodule); - return submodule->ignore; -} - -git_submodule_ignore_t git_submodule_set_ignore( - git_submodule *submodule, git_submodule_ignore_t ignore) -{ - git_submodule_ignore_t old; - - assert(submodule); - - if (ignore == GIT_SUBMODULE_IGNORE_DEFAULT) - ignore = submodule->ignore_default; - - old = submodule->ignore; - submodule->ignore = ignore; - return old; -} - -git_submodule_update_t git_submodule_update(git_submodule *submodule) -{ - assert(submodule); - return submodule->update; -} - -git_submodule_update_t git_submodule_set_update( - git_submodule *submodule, git_submodule_update_t update) -{ - git_submodule_update_t old; - - assert(submodule); - - if (update == GIT_SUBMODULE_UPDATE_DEFAULT) - update = submodule->update_default; - - old = submodule->update; - submodule->update = update; - return old; -} - -int git_submodule_fetch_recurse_submodules( - git_submodule *submodule) -{ - assert(submodule); - return submodule->fetch_recurse; -} - -int git_submodule_set_fetch_recurse_submodules( - git_submodule *submodule, - int fetch_recurse_submodules) -{ - int old; - - assert(submodule); - - old = submodule->fetch_recurse; - submodule->fetch_recurse = (fetch_recurse_submodules != 0); - return old; -} - -int git_submodule_init(git_submodule *submodule, int overwrite) -{ - int error; - - /* write "submodule.NAME.url" */ - - if (!submodule->url) { - giterr_set(GITERR_SUBMODULE, - "No URL configured for submodule '%s'", submodule->name); - return -1; - } - - error = submodule_update_config( - submodule, "url", submodule->url, overwrite != 0, false); - if (error < 0) - return error; - - /* write "submodule.NAME.update" if not default */ - - if (submodule->update == GIT_SUBMODULE_UPDATE_CHECKOUT) - error = submodule_update_config( - submodule, "update", NULL, (overwrite != 0), false); - else if (submodule->update != GIT_SUBMODULE_UPDATE_DEFAULT) - error = submodule_update_config( - submodule, "update", - _sm_update_map[submodule->update].str_match, - (overwrite != 0), false); - - return error; -} - -int git_submodule_sync(git_submodule *submodule) -{ - if (!submodule->url) { - giterr_set(GITERR_SUBMODULE, - "No URL configured for submodule '%s'", submodule->name); - return -1; - } - - /* copy URL over to config only if it already exists */ - - return submodule_update_config( - submodule, "url", submodule->url, true, true); -} - -int git_submodule_open( - git_repository **subrepo, - git_submodule *submodule) -{ - int error; - git_buf path = GIT_BUF_INIT; - git_repository *repo; - const char *workdir; - - assert(submodule && subrepo); - - repo = submodule->owner; - workdir = git_repository_workdir(repo); - - if (!workdir) { - giterr_set(GITERR_REPOSITORY, - "Cannot open submodule repository in a bare repo"); - return GIT_ENOTFOUND; - } - - if ((submodule->flags & GIT_SUBMODULE_STATUS_IN_WD) == 0) { - giterr_set(GITERR_REPOSITORY, - "Cannot open submodule repository that is not checked out"); - return GIT_ENOTFOUND; - } - - if (git_buf_joinpath(&path, workdir, submodule->path) < 0) - return -1; - - error = git_repository_open(subrepo, path.ptr); - - git_buf_free(&path); - - /* if we have opened the submodule successfully, let's grab the HEAD OID */ - if (!error && !(submodule->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID)) { - if (!git_reference_name_to_id( - &submodule->wd_oid, *subrepo, GIT_HEAD_FILE)) - submodule->flags |= GIT_SUBMODULE_STATUS__WD_OID_VALID; - else - giterr_clear(); - } - - return error; -} - -int git_submodule_reload_all(git_repository *repo) -{ - assert(repo); - git_submodule_config_free(repo); - return load_submodule_config(repo); -} - -int git_submodule_reload(git_submodule *submodule) -{ - git_repository *repo; - git_index *index; - int error; - size_t pos; - git_tree *head; - git_config_backend *mods; - - assert(submodule); - - /* refresh index data */ - - repo = submodule->owner; - if (git_repository_index__weakptr(&index, repo) < 0) - return -1; - - submodule->flags = submodule->flags & - ~(GIT_SUBMODULE_STATUS_IN_INDEX | - GIT_SUBMODULE_STATUS__INDEX_OID_VALID); - - if (!git_index_find(&pos, index, submodule->path)) { - const git_index_entry *entry = git_index_get_byindex(index, pos); - - if (S_ISGITLINK(entry->mode)) { - if ((error = submodule_load_from_index(repo, entry)) < 0) - return error; - } else { - submodule_mode_mismatch( - repo, entry->path, GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE); - } - } - - /* refresh HEAD tree data */ - - if (!(error = git_repository_head_tree(&head, repo))) { - git_tree_entry *te; - - submodule->flags = submodule->flags & - ~(GIT_SUBMODULE_STATUS_IN_HEAD | - GIT_SUBMODULE_STATUS__HEAD_OID_VALID); - - if (!(error = git_tree_entry_bypath(&te, head, submodule->path))) { - - if (S_ISGITLINK(te->attr)) { - error = submodule_load_from_head(repo, submodule->path, &te->oid); - } else { - submodule_mode_mismatch( - repo, submodule->path, - GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE); - } - - git_tree_entry_free(te); - } - else if (error == GIT_ENOTFOUND) { - giterr_clear(); - error = 0; - } - - git_tree_free(head); - } - - if (error < 0) - return error; - - /* refresh config data */ - - if ((mods = open_gitmodules(repo, false, NULL)) != NULL) { - git_buf path = GIT_BUF_INIT; - - git_buf_sets(&path, "submodule\\."); - git_buf_text_puts_escape_regex(&path, submodule->name); - git_buf_puts(&path, ".*"); - - if (git_buf_oom(&path)) - error = -1; - else - error = git_config_file_foreach_match( - mods, path.ptr, submodule_load_from_config, repo); - - git_buf_free(&path); - git_config_file_free(mods); - } - - if (error < 0) - return error; - - /* refresh wd data */ - - submodule->flags = submodule->flags & - ~(GIT_SUBMODULE_STATUS_IN_WD | GIT_SUBMODULE_STATUS__WD_OID_VALID); - - error = submodule_load_from_wd_lite(submodule, submodule->path, NULL); - - return error; -} - -int git_submodule_status( - unsigned int *status, - git_submodule *submodule) -{ - int error = 0; - unsigned int status_val; - - assert(status && submodule); - - status_val = GIT_SUBMODULE_STATUS__CLEAR_INTERNAL(submodule->flags); - - if (submodule->ignore != GIT_SUBMODULE_IGNORE_ALL) { - if (!(error = submodule_index_status(&status_val, submodule))) - error = submodule_wd_status(&status_val, submodule); - } - - *status = status_val; - - return error; -} - -int git_submodule_location( - unsigned int *location_status, - git_submodule *submodule) -{ - assert(location_status && submodule); - - *location_status = submodule->flags & - (GIT_SUBMODULE_STATUS_IN_HEAD | GIT_SUBMODULE_STATUS_IN_INDEX | - GIT_SUBMODULE_STATUS_IN_CONFIG | GIT_SUBMODULE_STATUS_IN_WD); - - return 0; -} - - -/* - * INTERNAL FUNCTIONS - */ - -static git_submodule *submodule_alloc(git_repository *repo, const char *name) -{ - git_submodule *sm; - - if (!name || !strlen(name)) { - giterr_set(GITERR_SUBMODULE, "Invalid submodule name"); - return NULL; - } - - sm = git__calloc(1, sizeof(git_submodule)); - if (sm == NULL) - goto fail; - - sm->path = sm->name = git__strdup(name); - if (!sm->name) - goto fail; - - sm->owner = repo; - sm->refcount = 1; - - return sm; - -fail: - submodule_release(sm, 0); - return NULL; -} - -static void submodule_release(git_submodule *sm, int decr) -{ - if (!sm) - return; - - sm->refcount -= decr; - - if (sm->refcount == 0) { - if (sm->name != sm->path) { - git__free(sm->path); - sm->path = NULL; - } - - git__free(sm->name); - sm->name = NULL; - - git__free(sm->url); - sm->url = NULL; - - sm->owner = NULL; - - git__free(sm); - } -} - -static int submodule_get( - git_submodule **sm_ptr, - git_repository *repo, - const char *name, - const char *alternate) -{ - git_strmap *smcfg = repo->submodules; - khiter_t pos; - git_submodule *sm; - int error; - - assert(repo && name); - - pos = git_strmap_lookup_index(smcfg, name); - - if (!git_strmap_valid_index(smcfg, pos) && alternate) - pos = git_strmap_lookup_index(smcfg, alternate); - - if (!git_strmap_valid_index(smcfg, pos)) { - sm = submodule_alloc(repo, name); - - /* insert value at name - if another thread beats us to it, then use - * their record and release our own. - */ - pos = kh_put(str, smcfg, sm->name, &error); - - if (error < 0) { - submodule_release(sm, 1); - sm = NULL; - } else if (error == 0) { - submodule_release(sm, 1); - sm = git_strmap_value_at(smcfg, pos); - } else { - git_strmap_set_value_at(smcfg, pos, sm); - } - } else { - sm = git_strmap_value_at(smcfg, pos); - } - - *sm_ptr = sm; - - return (sm != NULL) ? 0 : -1; -} - -static int submodule_load_from_index( - git_repository *repo, const git_index_entry *entry) -{ - git_submodule *sm; - - if (submodule_get(&sm, repo, entry->path, NULL) < 0) - return -1; - - if (sm->flags & GIT_SUBMODULE_STATUS_IN_INDEX) { - sm->flags |= GIT_SUBMODULE_STATUS__INDEX_MULTIPLE_ENTRIES; - return 0; - } - - sm->flags |= GIT_SUBMODULE_STATUS_IN_INDEX; - - git_oid_cpy(&sm->index_oid, &entry->oid); - sm->flags |= GIT_SUBMODULE_STATUS__INDEX_OID_VALID; - - return 0; -} - -static int submodule_load_from_head( - git_repository *repo, const char *path, const git_oid *oid) -{ - git_submodule *sm; - - if (submodule_get(&sm, repo, path, NULL) < 0) - return -1; - - sm->flags |= GIT_SUBMODULE_STATUS_IN_HEAD; - - git_oid_cpy(&sm->head_oid, oid); - sm->flags |= GIT_SUBMODULE_STATUS__HEAD_OID_VALID; - - return 0; -} - -static int submodule_config_error(const char *property, const char *value) -{ - giterr_set(GITERR_INVALID, - "Invalid value for submodule '%s' property: '%s'", property, value); - return -1; -} - -static int submodule_load_from_config( - const git_config_entry *entry, void *data) -{ - git_repository *repo = data; - git_strmap *smcfg = repo->submodules; - const char *namestart, *property, *alternate = NULL; - const char *key = entry->name, *value = entry->value; - git_buf name = GIT_BUF_INIT; - git_submodule *sm; - bool is_path; - int error = 0; - - if (git__prefixcmp(key, "submodule.") != 0) - return 0; - - namestart = key + strlen("submodule."); - property = strrchr(namestart, '.'); - if (property == NULL) - return 0; - property++; - is_path = (strcasecmp(property, "path") == 0); - - if (git_buf_set(&name, namestart, property - namestart - 1) < 0) - return -1; - - if (submodule_get(&sm, repo, name.ptr, is_path ? value : NULL) < 0) { - git_buf_free(&name); - return -1; - } - - sm->flags |= GIT_SUBMODULE_STATUS_IN_CONFIG; - - /* Only from config might we get differing names & paths. If so, then - * update the submodule and insert under the alternative key. - */ - - /* TODO: if case insensitive filesystem, then the following strcmps - * should be strcasecmp - */ - - if (strcmp(sm->name, name.ptr) != 0) { - alternate = sm->name = git_buf_detach(&name); - } else if (is_path && value && strcmp(sm->path, value) != 0) { - alternate = sm->path = git__strdup(value); - if (!sm->path) - error = -1; - } - if (alternate) { - void *old_sm = NULL; - git_strmap_insert2(smcfg, alternate, sm, old_sm, error); - - if (error >= 0) - sm->refcount++; /* inserted under a new key */ - - /* if we replaced an old module under this key, release the old one */ - if (old_sm && ((git_submodule *)old_sm) != sm) { - submodule_release(old_sm, 1); - /* TODO: log warning about multiple submodules with same path */ - } - } - - git_buf_free(&name); - if (error < 0) - return error; - - /* TODO: Look up path in index and if it is present but not a GITLINK - * then this should be deleted (at least to match git's behavior) - */ - - if (is_path) - return 0; - - /* copy other properties into submodule entry */ - if (strcasecmp(property, "url") == 0) { - git__free(sm->url); - sm->url = NULL; - - if (value != NULL && (sm->url = git__strdup(value)) == NULL) - return -1; - } - else if (strcasecmp(property, "update") == 0) { - int val; - if (git_config_lookup_map_value( - &val, _sm_update_map, ARRAY_SIZE(_sm_update_map), value) < 0) - return submodule_config_error("update", value); - sm->update_default = sm->update = (git_submodule_update_t)val; - } - else if (strcasecmp(property, "fetchRecurseSubmodules") == 0) { - if (git__parse_bool(&sm->fetch_recurse, value) < 0) - return submodule_config_error("fetchRecurseSubmodules", value); - } - else if (strcasecmp(property, "ignore") == 0) { - int val; - if (git_config_lookup_map_value( - &val, _sm_ignore_map, ARRAY_SIZE(_sm_ignore_map), value) < 0) - return submodule_config_error("ignore", value); - sm->ignore_default = sm->ignore = (git_submodule_ignore_t)val; - } - /* ignore other unknown submodule properties */ - - return 0; -} - -static int submodule_load_from_wd_lite( - git_submodule *sm, const char *name, void *payload) -{ - git_repository *repo = git_submodule_owner(sm); - git_buf path = GIT_BUF_INIT; - - GIT_UNUSED(name); - GIT_UNUSED(payload); - - if (git_buf_joinpath(&path, git_repository_workdir(repo), sm->path) < 0) - return -1; - - if (git_path_isdir(path.ptr)) - sm->flags |= GIT_SUBMODULE_STATUS__WD_SCANNED; - - if (git_path_contains(&path, DOT_GIT)) - sm->flags |= GIT_SUBMODULE_STATUS_IN_WD; - - git_buf_free(&path); - - return 0; -} - -static void submodule_mode_mismatch( - git_repository *repo, const char *path, unsigned int flag) -{ - khiter_t pos = git_strmap_lookup_index(repo->submodules, path); - - if (git_strmap_valid_index(repo->submodules, pos)) { - git_submodule *sm = git_strmap_value_at(repo->submodules, pos); - - sm->flags |= flag; - } -} - -static int load_submodule_config_from_index( - git_repository *repo, git_oid *gitmodules_oid) -{ - int error; - git_index *index; - git_iterator *i; - const git_index_entry *entry; - - if ((error = git_repository_index__weakptr(&index, repo)) < 0 || - (error = git_iterator_for_index(&i, index)) < 0) - return error; - - error = git_iterator_current(i, &entry); - - while (!error && entry != NULL) { - - if (S_ISGITLINK(entry->mode)) { - error = submodule_load_from_index(repo, entry); - if (error < 0) - break; - } else { - submodule_mode_mismatch( - repo, entry->path, GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE); - - if (strcmp(entry->path, GIT_MODULES_FILE) == 0) - git_oid_cpy(gitmodules_oid, &entry->oid); - } - - error = git_iterator_advance(i, &entry); - } - - git_iterator_free(i); - - return error; -} - -static int load_submodule_config_from_head( - git_repository *repo, git_oid *gitmodules_oid) -{ - int error; - git_tree *head; - git_iterator *i; - const git_index_entry *entry; - - if ((error = git_repository_head_tree(&head, repo)) < 0) - return error; - - if ((error = git_iterator_for_tree(&i, head)) < 0) { - git_tree_free(head); - return error; - } - - error = git_iterator_current(i, &entry); - - while (!error && entry != NULL) { - - if (S_ISGITLINK(entry->mode)) { - error = submodule_load_from_head(repo, entry->path, &entry->oid); - if (error < 0) - break; - } else { - submodule_mode_mismatch( - repo, entry->path, GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE); - - if (strcmp(entry->path, GIT_MODULES_FILE) == 0 && - git_oid_iszero(gitmodules_oid)) - git_oid_cpy(gitmodules_oid, &entry->oid); - } - - error = git_iterator_advance(i, &entry); - } - - git_iterator_free(i); - git_tree_free(head); - - return error; -} - -static git_config_backend *open_gitmodules( - git_repository *repo, - bool okay_to_create, - const git_oid *gitmodules_oid) -{ - const char *workdir = git_repository_workdir(repo); - git_buf path = GIT_BUF_INIT; - git_config_backend *mods = NULL; - - if (workdir != NULL) { - if (git_buf_joinpath(&path, workdir, GIT_MODULES_FILE) != 0) - return NULL; - - if (okay_to_create || git_path_isfile(path.ptr)) { - /* git_config_file__ondisk should only fail if OOM */ - if (git_config_file__ondisk(&mods, path.ptr) < 0) - mods = NULL; - /* open should only fail here if the file is malformed */ - else if (git_config_file_open(mods, GIT_CONFIG_LEVEL_LOCAL) < 0) { - git_config_file_free(mods); - mods = NULL; - } - } - } - - if (!mods && gitmodules_oid && !git_oid_iszero(gitmodules_oid)) { - /* TODO: Retrieve .gitmodules content from ODB */ - - /* Should we actually do this? Core git does not, but it means you - * can't really get much information about submodules on bare repos. - */ - } - - git_buf_free(&path); - - return mods; -} - -static int load_submodule_config(git_repository *repo) -{ - int error; - git_oid gitmodules_oid; - git_buf path = GIT_BUF_INIT; - git_config_backend *mods = NULL; - - if (repo->submodules) - return 0; - - memset(&gitmodules_oid, 0, sizeof(gitmodules_oid)); - - /* Submodule data is kept in a hashtable keyed by both name and path. - * These are usually the same, but that is not guaranteed. - */ - if (!repo->submodules) { - repo->submodules = git_strmap_alloc(); - GITERR_CHECK_ALLOC(repo->submodules); - } - - /* add submodule information from index */ - - if ((error = load_submodule_config_from_index(repo, &gitmodules_oid)) < 0) - goto cleanup; - - /* add submodule information from HEAD */ - - if ((error = load_submodule_config_from_head(repo, &gitmodules_oid)) < 0) - goto cleanup; - - /* add submodule information from .gitmodules */ - - if ((mods = open_gitmodules(repo, false, &gitmodules_oid)) != NULL) - error = git_config_file_foreach(mods, submodule_load_from_config, repo); - - if (error != 0) - goto cleanup; - - /* shallow scan submodules in work tree */ - - if (!git_repository_is_bare(repo)) - error = git_submodule_foreach(repo, submodule_load_from_wd_lite, NULL); - -cleanup: - git_buf_free(&path); - - if (mods != NULL) - git_config_file_free(mods); - - if (error) - git_submodule_config_free(repo); - - return error; -} - -static int lookup_head_remote(git_buf *url, git_repository *repo) -{ - int error; - git_config *cfg; - git_reference *head = NULL, *remote = NULL; - const char *tgt, *scan; - git_buf key = GIT_BUF_INIT; - - /* 1. resolve HEAD -> refs/heads/BRANCH - * 2. lookup config branch.BRANCH.remote -> ORIGIN - * 3. lookup remote.ORIGIN.url - */ - - if ((error = git_repository_config__weakptr(&cfg, repo)) < 0) - return error; - - if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0) { - giterr_set(GITERR_SUBMODULE, - "Cannot resolve relative URL when HEAD cannot be resolved"); - error = GIT_ENOTFOUND; - goto cleanup; - } - - if (git_reference_type(head) != GIT_REF_SYMBOLIC) { - giterr_set(GITERR_SUBMODULE, - "Cannot resolve relative URL when HEAD is not symbolic"); - error = GIT_ENOTFOUND; - goto cleanup; - } - - if ((error = git_branch_tracking(&remote, head)) < 0) - goto cleanup; - - /* remote should refer to something like refs/remotes/ORIGIN/BRANCH */ - - if (git_reference_type(remote) != GIT_REF_SYMBOLIC || - git__prefixcmp(git_reference_symbolic_target(remote), GIT_REFS_REMOTES_DIR) != 0) - { - giterr_set(GITERR_SUBMODULE, - "Cannot resolve relative URL when HEAD is not symbolic"); - error = GIT_ENOTFOUND; - goto cleanup; - } - - scan = tgt = git_reference_symbolic_target(remote) + strlen(GIT_REFS_REMOTES_DIR); - while (*scan && (*scan != '/' || (scan > tgt && scan[-1] != '\\'))) - scan++; /* find non-escaped slash to end ORIGIN name */ - - error = git_buf_printf(&key, "remote.%.*s.url", (int)(scan - tgt), tgt); - if (error < 0) - goto cleanup; - - if ((error = git_config_get_string(&tgt, cfg, key.ptr)) < 0) - goto cleanup; - - error = git_buf_sets(url, tgt); - -cleanup: - git_buf_free(&key); - git_reference_free(head); - git_reference_free(remote); - - return error; -} - -static int submodule_update_config( - git_submodule *submodule, - const char *attr, - const char *value, - bool overwrite, - bool only_existing) -{ - int error; - git_config *config; - git_buf key = GIT_BUF_INIT; - const char *old = NULL; - - assert(submodule); - - error = git_repository_config__weakptr(&config, submodule->owner); - if (error < 0) - return error; - - error = git_buf_printf(&key, "submodule.%s.%s", submodule->name, attr); - if (error < 0) - goto cleanup; - - if (git_config_get_string(&old, config, key.ptr) < 0) - giterr_clear(); - - if (!old && only_existing) - goto cleanup; - if (old && !overwrite) - goto cleanup; - if ((!old && !value) || (old && value && strcmp(old, value) == 0)) - goto cleanup; - - if (!value) - error = git_config_delete_entry(config, key.ptr); - else - error = git_config_set_string(config, key.ptr, value); - -cleanup: - git_buf_free(&key); - return error; -} - -static int submodule_index_status(unsigned int *status, git_submodule *sm) -{ - const git_oid *head_oid = git_submodule_head_id(sm); - const git_oid *index_oid = git_submodule_index_id(sm); - - if (!head_oid) { - if (index_oid) - *status |= GIT_SUBMODULE_STATUS_INDEX_ADDED; - } - else if (!index_oid) - *status |= GIT_SUBMODULE_STATUS_INDEX_DELETED; - else if (!git_oid_equal(head_oid, index_oid)) - *status |= GIT_SUBMODULE_STATUS_INDEX_MODIFIED; - - return 0; -} - -static int submodule_wd_status(unsigned int *status, git_submodule *sm) -{ - int error = 0; - const git_oid *wd_oid, *index_oid; - git_repository *sm_repo = NULL; - - /* open repo now if we need it (so wd_id() call won't reopen) */ - if ((sm->ignore == GIT_SUBMODULE_IGNORE_NONE || - sm->ignore == GIT_SUBMODULE_IGNORE_UNTRACKED) && - (sm->flags & GIT_SUBMODULE_STATUS_IN_WD) != 0) - { - if ((error = git_submodule_open(&sm_repo, sm)) < 0) - return error; - } - - index_oid = git_submodule_index_id(sm); - wd_oid = git_submodule_wd_id(sm); - - if (!index_oid) { - if (wd_oid) - *status |= GIT_SUBMODULE_STATUS_WD_ADDED; - } - else if (!wd_oid) { - if ((sm->flags & GIT_SUBMODULE_STATUS__WD_SCANNED) != 0 && - (sm->flags & GIT_SUBMODULE_STATUS_IN_WD) == 0) - *status |= GIT_SUBMODULE_STATUS_WD_UNINITIALIZED; - else - *status |= GIT_SUBMODULE_STATUS_WD_DELETED; - } - else if (!git_oid_equal(index_oid, wd_oid)) - *status |= GIT_SUBMODULE_STATUS_WD_MODIFIED; - - if (sm_repo != NULL) { - git_tree *sm_head; - git_diff_options opt = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff; - - /* the diffs below could be optimized with an early termination - * option to the git_diff functions, but for now this is sufficient - * (and certainly no worse that what core git does). - */ - - /* perform head-to-index diff on submodule */ - - if ((error = git_repository_head_tree(&sm_head, sm_repo)) < 0) - return error; - - if (sm->ignore == GIT_SUBMODULE_IGNORE_NONE) - opt.flags |= GIT_DIFF_INCLUDE_UNTRACKED; - - error = git_diff_tree_to_index(&diff, sm_repo, sm_head, NULL, &opt); - - if (!error) { - if (git_diff_num_deltas(diff) > 0) - *status |= GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED; - - git_diff_list_free(diff); - diff = NULL; - } - - git_tree_free(sm_head); - - if (error < 0) - return error; - - /* perform index-to-workdir diff on submodule */ - - error = git_diff_index_to_workdir(&diff, sm_repo, NULL, &opt); - - if (!error) { - size_t untracked = - git_diff_num_deltas_of_type(diff, GIT_DELTA_UNTRACKED); - - if (untracked > 0) - *status |= GIT_SUBMODULE_STATUS_WD_UNTRACKED; - - if ((git_diff_num_deltas(diff) - untracked) > 0) - *status |= GIT_SUBMODULE_STATUS_WD_WD_MODIFIED; - - git_diff_list_free(diff); - diff = NULL; - } - - git_repository_free(sm_repo); - } - - return error; -} diff --git a/src/submodule.h b/src/submodule.h deleted file mode 100644 index ba8e2518e83..00000000000 --- a/src/submodule.h +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_submodule_h__ -#define INCLUDE_submodule_h__ - -/* Notes: - * - * Submodule information can be in four places: the index, the config files - * (both .git/config and .gitmodules), the HEAD tree, and the working - * directory. - * - * In the index: - * - submodule is found by path - * - may be missing, present, or of the wrong type - * - will have an oid if present - * - * In the HEAD tree: - * - submodule is found by path - * - may be missing, present, or of the wrong type - * - will have an oid if present - * - * In the config files: - * - submodule is found by submodule "name" which is usually the path - * - may be missing or present - * - will have a name, path, url, and other properties - * - * In the working directory: - * - submodule is found by path - * - may be missing, an empty directory, a checked out directory, - * or of the wrong type - * - if checked out, will have a HEAD oid - * - if checked out, will have git history that can be used to compare oids - * - if checked out, may have modified files and/or untracked files - */ - -/** - * Description of submodule - * - * This record describes a submodule found in a repository. There should be - * an entry for every submodule found in the HEAD and index, and for every - * submodule described in .gitmodules. The fields are as follows: - * - * - `owner` is the git_repository containing this submodule - * - `name` is the name of the submodule from .gitmodules. - * - `path` is the path to the submodule from the repo root. It is almost - * always the same as `name`. - * - `url` is the url for the submodule. - * - `tree_oid` is the SHA1 for the submodule path in the repo HEAD. - * - `index_oid` is the SHA1 for the submodule recorded in the index. - * - `workdir_oid` is the SHA1 for the HEAD of the checked out submodule. - * - `update` is a git_submodule_update_t value - see gitmodules(5) update. - * - `ignore` is a git_submodule_ignore_t value - see gitmodules(5) ignore. - * - `fetch_recurse` is 0 or 1 - see gitmodules(5) fetchRecurseSubmodules. - * - `refcount` tracks how many hashmap entries there are for this submodule. - * It only comes into play if the name and path of the submodule differ. - * - `flags` is for internal use, tracking where this submodule has been - * found (head, index, config, workdir) and other misc info about it. - * - * If the submodule has been added to .gitmodules but not yet git added, - * then the `index_oid` will be valid and zero. If the submodule has been - * deleted, but the delete has not been committed yet, then the `index_oid` - * will be set, but the `url` will be NULL. - */ -struct git_submodule { - git_repository *owner; - char *name; - char *path; /* important: may point to same string data as "name" */ - char *url; - uint32_t flags; - git_oid head_oid; - git_oid index_oid; - git_oid wd_oid; - /* information from config */ - git_submodule_update_t update; - git_submodule_update_t update_default; - git_submodule_ignore_t ignore; - git_submodule_ignore_t ignore_default; - int fetch_recurse; - /* internal information */ - int refcount; -}; - -/* Additional flags on top of public GIT_SUBMODULE_STATUS values */ -enum { - GIT_SUBMODULE_STATUS__WD_SCANNED = (1u << 20), - GIT_SUBMODULE_STATUS__HEAD_OID_VALID = (1u << 21), - GIT_SUBMODULE_STATUS__INDEX_OID_VALID = (1u << 22), - GIT_SUBMODULE_STATUS__WD_OID_VALID = (1u << 23), - GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE = (1u << 24), - GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE = (1u << 25), - GIT_SUBMODULE_STATUS__WD_NOT_SUBMODULE = (1u << 26), - GIT_SUBMODULE_STATUS__INDEX_MULTIPLE_ENTRIES = (1u << 27), -}; - -#define GIT_SUBMODULE_STATUS__CLEAR_INTERNAL(S) \ - ((S) & ~(0xFFFFFFFFu << 20)) - -#endif diff --git a/src/tag.c b/src/tag.c deleted file mode 100644 index 592299e40d0..00000000000 --- a/src/tag.c +++ /dev/null @@ -1,484 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "commit.h" -#include "tag.h" -#include "signature.h" -#include "message.h" -#include "git2/object.h" -#include "git2/repository.h" -#include "git2/signature.h" - -void git_tag__free(git_tag *tag) -{ - git_signature_free(tag->tagger); - git__free(tag->message); - git__free(tag->tag_name); - git__free(tag); -} - -const git_oid *git_tag_id(const git_tag *c) -{ - return git_object_id((const git_object *)c); -} - -int git_tag_target(git_object **target, const git_tag *t) -{ - assert(t); - return git_object_lookup(target, t->object.repo, &t->target, t->type); -} - -const git_oid *git_tag_target_id(const git_tag *t) -{ - assert(t); - return &t->target; -} - -git_otype git_tag_target_type(const git_tag *t) -{ - assert(t); - return t->type; -} - -const char *git_tag_name(const git_tag *t) -{ - assert(t); - return t->tag_name; -} - -const git_signature *git_tag_tagger(const git_tag *t) -{ - return t->tagger; -} - -const char *git_tag_message(const git_tag *t) -{ - assert(t); - return t->message; -} - -static int tag_error(const char *str) -{ - giterr_set(GITERR_TAG, "Failed to parse tag. %s", str); - return -1; -} - -int git_tag__parse_buffer(git_tag *tag, const char *buffer, size_t length) -{ - static const char *tag_types[] = { - NULL, "commit\n", "tree\n", "blob\n", "tag\n" - }; - - unsigned int i; - size_t text_len; - char *search; - - const char *buffer_end = buffer + length; - - if (git_oid__parse(&tag->target, &buffer, buffer_end, "object ") < 0) - return tag_error("Object field invalid"); - - if (buffer + 5 >= buffer_end) - return tag_error("Object too short"); - - if (memcmp(buffer, "type ", 5) != 0) - return tag_error("Type field not found"); - buffer += 5; - - tag->type = GIT_OBJ_BAD; - - for (i = 1; i < ARRAY_SIZE(tag_types); ++i) { - size_t type_length = strlen(tag_types[i]); - - if (buffer + type_length >= buffer_end) - return tag_error("Object too short"); - - if (memcmp(buffer, tag_types[i], type_length) == 0) { - tag->type = i; - buffer += type_length; - break; - } - } - - if (tag->type == GIT_OBJ_BAD) - return tag_error("Invalid object type"); - - if (buffer + 4 >= buffer_end) - return tag_error("Object too short"); - - if (memcmp(buffer, "tag ", 4) != 0) - return tag_error("Tag field not found"); - - buffer += 4; - - search = memchr(buffer, '\n', buffer_end - buffer); - if (search == NULL) - return tag_error("Object too short"); - - text_len = search - buffer; - - tag->tag_name = git__malloc(text_len + 1); - GITERR_CHECK_ALLOC(tag->tag_name); - - memcpy(tag->tag_name, buffer, text_len); - tag->tag_name[text_len] = '\0'; - - buffer = search + 1; - - tag->tagger = NULL; - if (*buffer != '\n') { - tag->tagger = git__malloc(sizeof(git_signature)); - GITERR_CHECK_ALLOC(tag->tagger); - - if (git_signature__parse(tag->tagger, &buffer, buffer_end, "tagger ", '\n') < 0) - return -1; - } - - tag->message = NULL; - if (buffer < buffer_end) { - if( *buffer != '\n' ) - return tag_error("No new line before message"); - - text_len = buffer_end - ++buffer; - - tag->message = git__malloc(text_len + 1); - GITERR_CHECK_ALLOC(tag->message); - - memcpy(tag->message, buffer, text_len); - tag->message[text_len] = '\0'; - } - - return 0; -} - -static int retrieve_tag_reference( - git_reference **tag_reference_out, - git_buf *ref_name_out, - git_repository *repo, - const char *tag_name) -{ - git_reference *tag_ref; - int error; - - *tag_reference_out = NULL; - - if (git_buf_joinpath(ref_name_out, GIT_REFS_TAGS_DIR, tag_name) < 0) - return -1; - - error = git_reference_lookup(&tag_ref, repo, ref_name_out->ptr); - if (error < 0) - return error; /* Be it not foundo or corrupted */ - - *tag_reference_out = tag_ref; - - return 0; -} - -static int retrieve_tag_reference_oid( - git_oid *oid, - git_buf *ref_name_out, - git_repository *repo, - const char *tag_name) -{ - if (git_buf_joinpath(ref_name_out, GIT_REFS_TAGS_DIR, tag_name) < 0) - return -1; - - return git_reference_name_to_id(oid, repo, ref_name_out->ptr); -} - -static int write_tag_annotation( - git_oid *oid, - git_repository *repo, - const char *tag_name, - const git_object *target, - const git_signature *tagger, - const char *message) -{ - git_buf tag = GIT_BUF_INIT; - git_odb *odb; - - git_oid__writebuf(&tag, "object ", git_object_id(target)); - git_buf_printf(&tag, "type %s\n", git_object_type2string(git_object_type(target))); - git_buf_printf(&tag, "tag %s\n", tag_name); - git_signature__writebuf(&tag, "tagger ", tagger); - git_buf_putc(&tag, '\n'); - - if (git_buf_puts(&tag, message) < 0) - goto on_error; - - if (git_repository_odb__weakptr(&odb, repo) < 0) - goto on_error; - - if (git_odb_write(oid, odb, tag.ptr, tag.size, GIT_OBJ_TAG) < 0) - goto on_error; - - git_buf_free(&tag); - return 0; - -on_error: - git_buf_free(&tag); - giterr_set(GITERR_OBJECT, "Failed to create tag annotation."); - return -1; -} - -static int git_tag_create__internal( - git_oid *oid, - git_repository *repo, - const char *tag_name, - const git_object *target, - const git_signature *tagger, - const char *message, - int allow_ref_overwrite, - int create_tag_annotation) -{ - git_reference *new_ref = NULL; - git_buf ref_name = GIT_BUF_INIT; - - int error; - - assert(repo && tag_name && target); - assert(!create_tag_annotation || (tagger && message)); - - if (git_object_owner(target) != repo) { - giterr_set(GITERR_INVALID, "The given target does not belong to this repository"); - return -1; - } - - error = retrieve_tag_reference_oid(oid, &ref_name, repo, tag_name); - if (error < 0 && error != GIT_ENOTFOUND) - goto cleanup; - - /** Ensure the tag name doesn't conflict with an already existing - * reference unless overwriting has explictly been requested **/ - if (error == 0 && !allow_ref_overwrite) { - git_buf_free(&ref_name); - giterr_set(GITERR_TAG, "Tag already exists"); - return GIT_EEXISTS; - } - - if (create_tag_annotation) { - if (write_tag_annotation(oid, repo, tag_name, target, tagger, message) < 0) - return -1; - } else - git_oid_cpy(oid, git_object_id(target)); - - error = git_reference_create(&new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite); - -cleanup: - git_reference_free(new_ref); - git_buf_free(&ref_name); - return error; -} - -int git_tag_create( - git_oid *oid, - git_repository *repo, - const char *tag_name, - const git_object *target, - const git_signature *tagger, - const char *message, - int allow_ref_overwrite) -{ - return git_tag_create__internal(oid, repo, tag_name, target, tagger, message, allow_ref_overwrite, 1); -} - -int git_tag_create_lightweight( - git_oid *oid, - git_repository *repo, - const char *tag_name, - const git_object *target, - int allow_ref_overwrite) -{ - return git_tag_create__internal(oid, repo, tag_name, target, NULL, NULL, allow_ref_overwrite, 0); -} - -int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *buffer, int allow_ref_overwrite) -{ - git_tag tag; - int error; - git_odb *odb; - git_odb_stream *stream; - git_odb_object *target_obj; - - git_reference *new_ref = NULL; - git_buf ref_name = GIT_BUF_INIT; - - assert(oid && buffer); - - memset(&tag, 0, sizeof(tag)); - - if (git_repository_odb__weakptr(&odb, repo) < 0) - return -1; - - /* validate the buffer */ - if (git_tag__parse_buffer(&tag, buffer, strlen(buffer)) < 0) - return -1; - - /* validate the target */ - if (git_odb_read(&target_obj, odb, &tag.target) < 0) - goto on_error; - - if (tag.type != target_obj->raw.type) { - giterr_set(GITERR_TAG, "The type for the given target is invalid"); - goto on_error; - } - - error = retrieve_tag_reference_oid(oid, &ref_name, repo, tag.tag_name); - if (error < 0 && error != GIT_ENOTFOUND) - goto on_error; - - /* We don't need these objects after this */ - git_signature_free(tag.tagger); - git__free(tag.tag_name); - git__free(tag.message); - git_odb_object_free(target_obj); - - /** Ensure the tag name doesn't conflict with an already existing - * reference unless overwriting has explictly been requested **/ - if (error == 0 && !allow_ref_overwrite) { - giterr_set(GITERR_TAG, "Tag already exists"); - return GIT_EEXISTS; - } - - /* write the buffer */ - if (git_odb_open_wstream(&stream, odb, strlen(buffer), GIT_OBJ_TAG) < 0) - return -1; - - stream->write(stream, buffer, strlen(buffer)); - - error = stream->finalize_write(oid, stream); - stream->free(stream); - - if (error < 0) { - git_buf_free(&ref_name); - return -1; - } - - error = git_reference_create(&new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite); - - git_reference_free(new_ref); - git_buf_free(&ref_name); - - return error; - -on_error: - git_signature_free(tag.tagger); - git__free(tag.tag_name); - git__free(tag.message); - git_odb_object_free(target_obj); - return -1; -} - -int git_tag_delete(git_repository *repo, const char *tag_name) -{ - int error; - git_reference *tag_ref; - git_buf ref_name = GIT_BUF_INIT; - - error = retrieve_tag_reference(&tag_ref, &ref_name, repo, tag_name); - - git_buf_free(&ref_name); - - if (error < 0) - return error; - - return git_reference_delete(tag_ref); -} - -int git_tag__parse(git_tag *tag, git_odb_object *obj) -{ - assert(tag); - return git_tag__parse_buffer(tag, obj->raw.data, obj->raw.len); -} - -typedef struct { - git_repository *repo; - git_tag_foreach_cb cb; - void *cb_data; -} tag_cb_data; - -static int tags_cb(const char *ref, void *data) -{ - git_oid oid; - tag_cb_data *d = (tag_cb_data *)data; - - if (git__prefixcmp(ref, GIT_REFS_TAGS_DIR) != 0) - return 0; /* no tag */ - - if (git_reference_name_to_id(&oid, d->repo, ref) < 0) - return -1; - - return d->cb(ref, &oid, d->cb_data); -} - -int git_tag_foreach(git_repository *repo, git_tag_foreach_cb cb, void *cb_data) -{ - tag_cb_data data; - - assert(repo && cb); - - data.cb = cb; - data.cb_data = cb_data; - data.repo = repo; - - return git_reference_foreach( - repo, GIT_REF_OID | GIT_REF_PACKED, &tags_cb, &data); -} - -typedef struct { - git_vector *taglist; - const char *pattern; -} tag_filter_data; - -#define GIT_REFS_TAGS_DIR_LEN strlen(GIT_REFS_TAGS_DIR) - -static int tag_list_cb(const char *tag_name, git_oid *oid, void *data) -{ - tag_filter_data *filter = (tag_filter_data *)data; - GIT_UNUSED(oid); - - if (!*filter->pattern || p_fnmatch(filter->pattern, tag_name + GIT_REFS_TAGS_DIR_LEN, 0) == 0) - return git_vector_insert(filter->taglist, git__strdup(tag_name + GIT_REFS_TAGS_DIR_LEN)); - - return 0; -} - -int git_tag_list_match(git_strarray *tag_names, const char *pattern, git_repository *repo) -{ - int error; - tag_filter_data filter; - git_vector taglist; - - assert(tag_names && repo && pattern); - - if (git_vector_init(&taglist, 8, NULL) < 0) - return -1; - - filter.taglist = &taglist; - filter.pattern = pattern; - - error = git_tag_foreach(repo, &tag_list_cb, (void *)&filter); - if (error < 0) { - git_vector_free(&taglist); - return -1; - } - - tag_names->strings = (char **)taglist.contents; - tag_names->count = taglist.length; - return 0; -} - -int git_tag_list(git_strarray *tag_names, git_repository *repo) -{ - return git_tag_list_match(tag_names, "", repo); -} - -int git_tag_peel(git_object **tag_target, const git_tag *tag) -{ - return git_object_peel(tag_target, (const git_object *)tag, GIT_OBJ_ANY); -} diff --git a/src/tag.h b/src/tag.h deleted file mode 100644 index c8e421ee63b..00000000000 --- a/src/tag.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_tag_h__ -#define INCLUDE_tag_h__ - -#include "git2/tag.h" -#include "repository.h" -#include "odb.h" - -struct git_tag { - git_object object; - - git_oid target; - git_otype type; - - char *tag_name; - git_signature *tagger; - char *message; -}; - -void git_tag__free(git_tag *tag); -int git_tag__parse(git_tag *tag, git_odb_object *obj); -int git_tag__parse_buffer(git_tag *tag, const char *data, size_t len); - -#endif diff --git a/src/thread-utils.c b/src/thread-utils.c deleted file mode 100644 index c3baf411ad3..00000000000 --- a/src/thread-utils.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "common.h" -#include "thread-utils.h" - -#ifdef _WIN32 -# define WIN32_LEAN_AND_MEAN -# include -#elif defined(hpux) || defined(__hpux) || defined(_hpux) -# include -#endif - -/* - * By doing this in two steps we can at least get - * the function to be somewhat coherent, even - * with this disgusting nest of #ifdefs. - */ -#ifndef _SC_NPROCESSORS_ONLN -# ifdef _SC_NPROC_ONLN -# define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN -# elif defined _SC_CRAY_NCPU -# define _SC_NPROCESSORS_ONLN _SC_CRAY_NCPU -# endif -#endif - -int git_online_cpus(void) -{ -#ifdef _SC_NPROCESSORS_ONLN - long ncpus; -#endif - -#ifdef _WIN32 - SYSTEM_INFO info; - GetSystemInfo(&info); - - if ((int)info.dwNumberOfProcessors > 0) - return (int)info.dwNumberOfProcessors; -#elif defined(hpux) || defined(__hpux) || defined(_hpux) - struct pst_dynamic psd; - - if (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0)) - return (int)psd.psd_proc_cnt; -#endif - -#ifdef _SC_NPROCESSORS_ONLN - if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0) - return (int)ncpus; -#endif - - return 1; -} diff --git a/src/thread-utils.h b/src/thread-utils.h deleted file mode 100644 index 2ca290adfaf..00000000000 --- a/src/thread-utils.h +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_thread_utils_h__ -#define INCLUDE_thread_utils_h__ - -#include "common.h" - -/* Common operations even if threading has been disabled */ -typedef struct { -#if defined(GIT_WIN32) - volatile long val; -#else - volatile int val; -#endif -} git_atomic; - -GIT_INLINE(void) git_atomic_set(git_atomic *a, int val) -{ - a->val = val; -} - -#ifdef GIT_THREADS - -#define git_thread pthread_t -#define git_thread_create(thread, attr, start_routine, arg) pthread_create(thread, attr, start_routine, arg) -#define git_thread_kill(thread) pthread_cancel(thread) -#define git_thread_exit(status) pthread_exit(status) -#define git_thread_join(id, status) pthread_join(id, status) - -/* Pthreads Mutex */ -#define git_mutex pthread_mutex_t -#define git_mutex_init(a) pthread_mutex_init(a, NULL) -#define git_mutex_lock(a) pthread_mutex_lock(a) -#define git_mutex_unlock(a) pthread_mutex_unlock(a) -#define git_mutex_free(a) pthread_mutex_destroy(a) - -/* Pthreads condition vars */ -#define git_cond pthread_cond_t -#define git_cond_init(c) pthread_cond_init(c, NULL) -#define git_cond_free(c) pthread_cond_destroy(c) -#define git_cond_wait(c, l) pthread_cond_wait(c, l) -#define git_cond_signal(c) pthread_cond_signal(c) -#define git_cond_broadcast(c) pthread_cond_broadcast(c) - -GIT_INLINE(int) git_atomic_inc(git_atomic *a) -{ -#if defined(GIT_WIN32) - return InterlockedIncrement(&a->val); -#elif defined(__GNUC__) - return __sync_add_and_fetch(&a->val, 1); -#else -# error "Unsupported architecture for atomic operations" -#endif -} - -GIT_INLINE(int) git_atomic_dec(git_atomic *a) -{ -#if defined(GIT_WIN32) - return InterlockedDecrement(&a->val); -#elif defined(__GNUC__) - return __sync_sub_and_fetch(&a->val, 1); -#else -# error "Unsupported architecture for atomic operations" -#endif -} - -#else - -#define git_thread unsigned int -#define git_thread_create(thread, attr, start_routine, arg) (void)0 -#define git_thread_kill(thread) (void)0 -#define git_thread_exit(status) (void)0 -#define git_thread_join(id, status) (void)0 - -/* Pthreads Mutex */ -#define git_mutex unsigned int -#define git_mutex_init(a) (void)0 -#define git_mutex_lock(a) 0 -#define git_mutex_unlock(a) (void)0 -#define git_mutex_free(a) (void)0 - -/* Pthreads condition vars */ -#define git_cond unsigned int -#define git_cond_init(c, a) (void)0 -#define git_cond_free(c) (void)0 -#define git_cond_wait(c, l) (void)0 -#define git_cond_signal(c) (void)0 -#define git_cond_broadcast(c) (void)0 - -GIT_INLINE(int) git_atomic_inc(git_atomic *a) -{ - return ++a->val; -} - -GIT_INLINE(int) git_atomic_dec(git_atomic *a) -{ - return --a->val; -} - -#endif - -extern int git_online_cpus(void); - -#endif /* INCLUDE_thread_utils_h__ */ diff --git a/src/transport.c b/src/transport.c deleted file mode 100644 index adb6d5355a6..00000000000 --- a/src/transport.c +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "common.h" -#include "git2/types.h" -#include "git2/remote.h" -#include "git2/net.h" -#include "git2/transport.h" -#include "path.h" - -typedef struct transport_definition { - char *prefix; - unsigned priority; - git_transport_cb fn; - void *param; -} transport_definition; - -static transport_definition local_transport_definition = { "file://", 1, git_transport_local, NULL }; -static transport_definition dummy_transport_definition = { NULL, 1, git_transport_dummy, NULL }; - -static git_smart_subtransport_definition http_subtransport_definition = { git_smart_subtransport_http, 1 }; -static git_smart_subtransport_definition git_subtransport_definition = { git_smart_subtransport_git, 0 }; - -static transport_definition transports[] = { - {"git://", 1, git_transport_smart, &git_subtransport_definition}, - {"http://", 1, git_transport_smart, &http_subtransport_definition}, - {"https://", 1, git_transport_smart, &http_subtransport_definition}, - {"file://", 1, git_transport_local, NULL}, - {"git+ssh://", 1, git_transport_dummy, NULL}, - {"ssh+git://", 1, git_transport_dummy, NULL}, - {NULL, 0, 0} -}; - -#define GIT_TRANSPORT_COUNT (sizeof(transports)/sizeof(transports[0])) - 1 - -static int transport_find_fn(const char *url, git_transport_cb *callback, void **param) -{ - size_t i = 0; - unsigned priority = 0; - transport_definition *definition = NULL, *definition_iter; - - // First, check to see if it's an obvious URL, which a URL scheme - for (i = 0; i < GIT_TRANSPORT_COUNT; ++i) { - definition_iter = &transports[i]; - - if (strncasecmp(url, definition_iter->prefix, strlen(definition_iter->prefix))) - continue; - - if (definition_iter->priority > priority) - definition = definition_iter; - } - -#ifdef GIT_WIN32 - /* On Windows, it might not be possible to discern between absolute local - * and ssh paths - first check if this is a valid local path that points - * to a directory and if so assume local path, else assume SSH */ - - /* Check to see if the path points to a file on the local file system */ - if (!definition && git_path_exists(url) && git_path_isdir(url)) - definition = &local_transport_definition; - - /* It could be a SSH remote path. Check to see if there's a : - * SSH is an unsupported transport mechanism in this version of libgit2 */ - if (!definition && strrchr(url, ':')) - definition = &dummy_transport_definition; -#else - /* For other systems, perform the SSH check first, to avoid going to the - * filesystem if it is not necessary */ - - /* It could be a SSH remote path. Check to see if there's a : - * SSH is an unsupported transport mechanism in this version of libgit2 */ - if (!definition && strrchr(url, ':')) - definition = &dummy_transport_definition; - - /* Check to see if the path points to a file on the local file system */ - if (!definition && git_path_exists(url) && git_path_isdir(url)) - definition = &local_transport_definition; -#endif - - if (!definition) - return -1; - - *callback = definition->fn; - *param = definition->param; - - return 0; -} - -/************** - * Public API * - **************/ - -int git_transport_dummy(git_transport **transport, git_remote *owner, void *param) -{ - GIT_UNUSED(transport); - GIT_UNUSED(owner); - GIT_UNUSED(param); - giterr_set(GITERR_NET, "This transport isn't implemented. Sorry"); - return -1; -} - -int git_transport_new(git_transport **out, git_remote *owner, const char *url) -{ - git_transport_cb fn; - git_transport *transport; - void *param; - int error; - - if (transport_find_fn(url, &fn, ¶m) < 0) { - giterr_set(GITERR_NET, "Unsupported URL protocol"); - return -1; - } - - error = fn(&transport, owner, param); - if (error < 0) - return error; - - *out = transport; - - return 0; -} - -/* from remote.h */ -int git_remote_valid_url(const char *url) -{ - git_transport_cb fn; - void *param; - - return !transport_find_fn(url, &fn, ¶m); -} - -int git_remote_supported_url(const char* url) -{ - git_transport_cb fn; - void *param; - - if (transport_find_fn(url, &fn, ¶m) < 0) - return 0; - - return fn != &git_transport_dummy; -} diff --git a/src/transports/cred.c b/src/transports/cred.c deleted file mode 100644 index ecb02606225..00000000000 --- a/src/transports/cred.c +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "git2.h" -#include "smart.h" -#include "git2/cred_helpers.h" - -static void plaintext_free(struct git_cred *cred) -{ - git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred; - size_t pass_len = strlen(c->password); - - git__free(c->username); - - /* Zero the memory which previously held the password */ - memset(c->password, 0x0, pass_len); - git__free(c->password); - - memset(c, 0, sizeof(*c)); - - git__free(c); -} - -int git_cred_userpass_plaintext_new( - git_cred **cred, - const char *username, - const char *password) -{ - git_cred_userpass_plaintext *c; - - if (!cred) - return -1; - - c = git__malloc(sizeof(git_cred_userpass_plaintext)); - GITERR_CHECK_ALLOC(c); - - c->parent.credtype = GIT_CREDTYPE_USERPASS_PLAINTEXT; - c->parent.free = plaintext_free; - c->username = git__strdup(username); - - if (!c->username) { - git__free(c); - return -1; - } - - c->password = git__strdup(password); - - if (!c->password) { - git__free(c->username); - git__free(c); - return -1; - } - - *cred = &c->parent; - return 0; -} diff --git a/src/transports/cred_helpers.c b/src/transports/cred_helpers.c deleted file mode 100644 index 8d8eb99907d..00000000000 --- a/src/transports/cred_helpers.c +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "git2/cred_helpers.h" - -int git_cred_userpass( - git_cred **cred, - const char *url, - unsigned int allowed_types, - void *payload) -{ - git_cred_userpass_payload *userpass = (git_cred_userpass_payload*)payload; - - GIT_UNUSED(url); - - if (!userpass || !userpass->username || !userpass->password) return -1; - - if ((GIT_CREDTYPE_USERPASS_PLAINTEXT & allowed_types) == 0 || - git_cred_userpass_plaintext_new(cred, userpass->username, userpass->password) < 0) - return -1; - - return 0; -} diff --git a/src/transports/git.c b/src/transports/git.c deleted file mode 100644 index ba6dbfea932..00000000000 --- a/src/transports/git.c +++ /dev/null @@ -1,348 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "git2.h" -#include "buffer.h" -#include "netops.h" - -#define OWNING_SUBTRANSPORT(s) ((git_subtransport *)(s)->parent.subtransport) - -static const char prefix_git[] = "git://"; -static const char cmd_uploadpack[] = "git-upload-pack"; -static const char cmd_receivepack[] = "git-receive-pack"; - -typedef struct { - git_smart_subtransport_stream parent; - gitno_socket socket; - const char *cmd; - char *url; - unsigned sent_command : 1; -} git_stream; - -typedef struct { - git_smart_subtransport parent; - git_transport *owner; - git_stream *current_stream; -} git_subtransport; - -/* - * Create a git protocol request. - * - * For example: 0035git-upload-pack /libgit2/libgit2\0host=github.com\0 - */ -static int gen_proto(git_buf *request, const char *cmd, const char *url) -{ - char *delim, *repo; - char host[] = "host="; - size_t len; - - delim = strchr(url, '/'); - if (delim == NULL) { - giterr_set(GITERR_NET, "Malformed URL"); - return -1; - } - - repo = delim; - - delim = strchr(url, ':'); - if (delim == NULL) - delim = strchr(url, '/'); - - len = 4 + strlen(cmd) + 1 + strlen(repo) + 1 + strlen(host) + (delim - url) + 1; - - git_buf_grow(request, len); - git_buf_printf(request, "%04x%s %s%c%s", - (unsigned int)(len & 0x0FFFF), cmd, repo, 0, host); - git_buf_put(request, url, delim - url); - git_buf_putc(request, '\0'); - - if (git_buf_oom(request)) - return -1; - - return 0; -} - -static int send_command(git_stream *s) -{ - int error; - git_buf request = GIT_BUF_INIT; - - error = gen_proto(&request, s->cmd, s->url); - if (error < 0) - goto cleanup; - - /* It looks like negative values are errors here, and positive values - * are the number of bytes sent. */ - error = gitno_send(&s->socket, request.ptr, request.size, 0); - - if (error >= 0) - s->sent_command = 1; - -cleanup: - git_buf_free(&request); - return error; -} - -static int git_stream_read( - git_smart_subtransport_stream *stream, - char *buffer, - size_t buf_size, - size_t *bytes_read) -{ - git_stream *s = (git_stream *)stream; - gitno_buffer buf; - - *bytes_read = 0; - - if (!s->sent_command && send_command(s) < 0) - return -1; - - gitno_buffer_setup(&s->socket, &buf, buffer, buf_size); - - if (gitno_recv(&buf) < 0) - return -1; - - *bytes_read = buf.offset; - - return 0; -} - -static int git_stream_write( - git_smart_subtransport_stream *stream, - const char *buffer, - size_t len) -{ - git_stream *s = (git_stream *)stream; - - if (!s->sent_command && send_command(s) < 0) - return -1; - - return gitno_send(&s->socket, buffer, len, 0); -} - -static void git_stream_free(git_smart_subtransport_stream *stream) -{ - git_stream *s = (git_stream *)stream; - git_subtransport *t = OWNING_SUBTRANSPORT(s); - int ret; - - GIT_UNUSED(ret); - - t->current_stream = NULL; - - if (s->socket.socket) { - ret = gitno_close(&s->socket); - assert(!ret); - } - - git__free(s->url); - git__free(s); -} - -static int git_stream_alloc( - git_subtransport *t, - const char *url, - const char *cmd, - git_smart_subtransport_stream **stream) -{ - git_stream *s; - - if (!stream) - return -1; - - s = git__calloc(sizeof(git_stream), 1); - GITERR_CHECK_ALLOC(s); - - s->parent.subtransport = &t->parent; - s->parent.read = git_stream_read; - s->parent.write = git_stream_write; - s->parent.free = git_stream_free; - - s->cmd = cmd; - s->url = git__strdup(url); - - if (!s->url) { - git__free(s); - return -1; - } - - *stream = &s->parent; - return 0; -} - -static int _git_uploadpack_ls( - git_subtransport *t, - const char *url, - git_smart_subtransport_stream **stream) -{ - char *host, *port; - git_stream *s; - - *stream = NULL; - - if (!git__prefixcmp(url, prefix_git)) - url += strlen(prefix_git); - - if (git_stream_alloc(t, url, cmd_uploadpack, stream) < 0) - return -1; - - s = (git_stream *)*stream; - - if (gitno_extract_host_and_port(&host, &port, url, GIT_DEFAULT_PORT) < 0) - goto on_error; - - if (gitno_connect(&s->socket, host, port, 0) < 0) - goto on_error; - - t->current_stream = s; - git__free(host); - git__free(port); - return 0; - -on_error: - if (*stream) - git_stream_free(*stream); - - git__free(host); - git__free(port); - return -1; -} - -static int _git_uploadpack( - git_subtransport *t, - const char *url, - git_smart_subtransport_stream **stream) -{ - GIT_UNUSED(url); - - if (t->current_stream) { - *stream = &t->current_stream->parent; - return 0; - } - - giterr_set(GITERR_NET, "Must call UPLOADPACK_LS before UPLOADPACK"); - return -1; -} - -static int _git_receivepack_ls( - git_subtransport *t, - const char *url, - git_smart_subtransport_stream **stream) -{ - char *host, *port; - git_stream *s; - - *stream = NULL; - - if (!git__prefixcmp(url, prefix_git)) - url += strlen(prefix_git); - - if (git_stream_alloc(t, url, cmd_receivepack, stream) < 0) - return -1; - - s = (git_stream *)*stream; - - if (gitno_extract_host_and_port(&host, &port, url, GIT_DEFAULT_PORT) < 0) - goto on_error; - - if (gitno_connect(&s->socket, host, port, 0) < 0) - goto on_error; - - t->current_stream = s; - git__free(host); - git__free(port); - return 0; - -on_error: - if (*stream) - git_stream_free(*stream); - - git__free(host); - git__free(port); - return -1; -} - -static int _git_receivepack( - git_subtransport *t, - const char *url, - git_smart_subtransport_stream **stream) -{ - GIT_UNUSED(url); - - if (t->current_stream) { - *stream = &t->current_stream->parent; - return 0; - } - - giterr_set(GITERR_NET, "Must call RECEIVEPACK_LS before RECEIVEPACK"); - return -1; -} - -static int _git_action( - git_smart_subtransport_stream **stream, - git_smart_subtransport *subtransport, - const char *url, - git_smart_service_t action) -{ - git_subtransport *t = (git_subtransport *) subtransport; - - switch (action) { - case GIT_SERVICE_UPLOADPACK_LS: - return _git_uploadpack_ls(t, url, stream); - - case GIT_SERVICE_UPLOADPACK: - return _git_uploadpack(t, url, stream); - - case GIT_SERVICE_RECEIVEPACK_LS: - return _git_receivepack_ls(t, url, stream); - - case GIT_SERVICE_RECEIVEPACK: - return _git_receivepack(t, url, stream); - } - - *stream = NULL; - return -1; -} - -static int _git_close(git_smart_subtransport *subtransport) -{ - git_subtransport *t = (git_subtransport *) subtransport; - - assert(!t->current_stream); - - GIT_UNUSED(t); - - return 0; -} - -static void _git_free(git_smart_subtransport *subtransport) -{ - git_subtransport *t = (git_subtransport *) subtransport; - - assert(!t->current_stream); - - git__free(t); -} - -int git_smart_subtransport_git(git_smart_subtransport **out, git_transport *owner) -{ - git_subtransport *t; - - if (!out) - return -1; - - t = git__calloc(sizeof(git_subtransport), 1); - GITERR_CHECK_ALLOC(t); - - t->owner = owner; - t->parent.action = _git_action; - t->parent.close = _git_close; - t->parent.free = _git_free; - - *out = (git_smart_subtransport *) t; - return 0; -} diff --git a/src/transports/http.c b/src/transports/http.c deleted file mode 100644 index 96a9c394297..00000000000 --- a/src/transports/http.c +++ /dev/null @@ -1,859 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef GIT_WINHTTP - -#include "git2.h" -#include "http_parser.h" -#include "buffer.h" -#include "netops.h" -#include "smart.h" - -static const char *prefix_http = "http://"; -static const char *prefix_https = "https://"; -static const char *upload_pack_service = "upload-pack"; -static const char *upload_pack_ls_service_url = "/info/refs?service=git-upload-pack"; -static const char *upload_pack_service_url = "/git-upload-pack"; -static const char *receive_pack_service = "receive-pack"; -static const char *receive_pack_ls_service_url = "/info/refs?service=git-receive-pack"; -static const char *receive_pack_service_url = "/git-receive-pack"; -static const char *get_verb = "GET"; -static const char *post_verb = "POST"; -static const char *basic_authtype = "Basic"; - -#define OWNING_SUBTRANSPORT(s) ((http_subtransport *)(s)->parent.subtransport) - -#define PARSE_ERROR_GENERIC -1 -#define PARSE_ERROR_REPLAY -2 - -#define CHUNK_SIZE 4096 - -enum last_cb { - NONE, - FIELD, - VALUE -}; - -typedef enum { - GIT_HTTP_AUTH_BASIC = 1, -} http_authmechanism_t; - -typedef struct { - git_smart_subtransport_stream parent; - const char *service; - const char *service_url; - const char *verb; - char *chunk_buffer; - unsigned chunk_buffer_len; - unsigned sent_request : 1, - received_response : 1, - chunked : 1; -} http_stream; - -typedef struct { - git_smart_subtransport parent; - transport_smart *owner; - gitno_socket socket; - const char *path; - char *host; - char *port; - git_cred *cred; - http_authmechanism_t auth_mechanism; - unsigned connected : 1, - use_ssl : 1; - - /* Parser structures */ - http_parser parser; - http_parser_settings settings; - gitno_buffer parse_buffer; - git_buf parse_header_name; - git_buf parse_header_value; - char parse_buffer_data[2048]; - char *content_type; - git_vector www_authenticate; - enum last_cb last_cb; - int parse_error; - unsigned parse_finished : 1; -} http_subtransport; - -typedef struct { - http_stream *s; - http_subtransport *t; - - /* Target buffer details from read() */ - char *buffer; - size_t buf_size; - size_t *bytes_read; -} parser_context; - -static int apply_basic_credential(git_buf *buf, git_cred *cred) -{ - git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred; - git_buf raw = GIT_BUF_INIT; - int error = -1; - - git_buf_printf(&raw, "%s:%s", c->username, c->password); - - if (git_buf_oom(&raw) || - git_buf_puts(buf, "Authorization: Basic ") < 0 || - git_buf_put_base64(buf, git_buf_cstr(&raw), raw.size) < 0 || - git_buf_puts(buf, "\r\n") < 0) - goto on_error; - - error = 0; - -on_error: - if (raw.size) - memset(raw.ptr, 0x0, raw.size); - - git_buf_free(&raw); - return error; -} - -static int gen_request( - git_buf *buf, - http_stream *s, - size_t content_length) -{ - http_subtransport *t = OWNING_SUBTRANSPORT(s); - - if (!t->path) - t->path = "/"; - - git_buf_printf(buf, "%s %s%s HTTP/1.1\r\n", s->verb, t->path, s->service_url); - git_buf_puts(buf, "User-Agent: git/1.0 (libgit2 " LIBGIT2_VERSION ")\r\n"); - git_buf_printf(buf, "Host: %s\r\n", t->host); - - if (s->chunked || content_length > 0) { - git_buf_printf(buf, "Accept: application/x-git-%s-result\r\n", s->service); - git_buf_printf(buf, "Content-Type: application/x-git-%s-request\r\n", s->service); - - if (s->chunked) - git_buf_puts(buf, "Transfer-Encoding: chunked\r\n"); - else - git_buf_printf(buf, "Content-Length: %"PRIuZ "\r\n", content_length); - } else - git_buf_puts(buf, "Accept: */*\r\n"); - - /* Apply credentials to the request */ - if (t->cred && t->cred->credtype == GIT_CREDTYPE_USERPASS_PLAINTEXT && - t->auth_mechanism == GIT_HTTP_AUTH_BASIC && - apply_basic_credential(buf, t->cred) < 0) - return -1; - - git_buf_puts(buf, "\r\n"); - - if (git_buf_oom(buf)) - return -1; - - return 0; -} - -static int parse_unauthorized_response( - git_vector *www_authenticate, - int *allowed_types, - http_authmechanism_t *auth_mechanism) -{ - unsigned i; - char *entry; - - git_vector_foreach(www_authenticate, i, entry) { - if (!strncmp(entry, basic_authtype, 5) && - (entry[5] == '\0' || entry[5] == ' ')) { - *allowed_types |= GIT_CREDTYPE_USERPASS_PLAINTEXT; - *auth_mechanism = GIT_HTTP_AUTH_BASIC; - } - } - - return 0; -} - -static int on_header_ready(http_subtransport *t) -{ - git_buf *name = &t->parse_header_name; - git_buf *value = &t->parse_header_value; - char *dup; - - if (!t->content_type && !strcasecmp("Content-Type", git_buf_cstr(name))) { - t->content_type = git__strdup(git_buf_cstr(value)); - GITERR_CHECK_ALLOC(t->content_type); - } - else if (!strcmp("WWW-Authenticate", git_buf_cstr(name))) { - dup = git__strdup(git_buf_cstr(value)); - GITERR_CHECK_ALLOC(dup); - git_vector_insert(&t->www_authenticate, dup); - } - - return 0; -} - -static int on_header_field(http_parser *parser, const char *str, size_t len) -{ - parser_context *ctx = (parser_context *) parser->data; - http_subtransport *t = ctx->t; - - /* Both parse_header_name and parse_header_value are populated - * and ready for consumption */ - if (VALUE == t->last_cb) - if (on_header_ready(t) < 0) - return t->parse_error = PARSE_ERROR_GENERIC; - - if (NONE == t->last_cb || VALUE == t->last_cb) - git_buf_clear(&t->parse_header_name); - - if (git_buf_put(&t->parse_header_name, str, len) < 0) - return t->parse_error = PARSE_ERROR_GENERIC; - - t->last_cb = FIELD; - return 0; -} - -static int on_header_value(http_parser *parser, const char *str, size_t len) -{ - parser_context *ctx = (parser_context *) parser->data; - http_subtransport *t = ctx->t; - - assert(NONE != t->last_cb); - - if (FIELD == t->last_cb) - git_buf_clear(&t->parse_header_value); - - if (git_buf_put(&t->parse_header_value, str, len) < 0) - return t->parse_error = PARSE_ERROR_GENERIC; - - t->last_cb = VALUE; - return 0; -} - -static int on_headers_complete(http_parser *parser) -{ - parser_context *ctx = (parser_context *) parser->data; - http_subtransport *t = ctx->t; - http_stream *s = ctx->s; - git_buf buf = GIT_BUF_INIT; - - /* Both parse_header_name and parse_header_value are populated - * and ready for consumption. */ - if (VALUE == t->last_cb) - if (on_header_ready(t) < 0) - return t->parse_error = PARSE_ERROR_GENERIC; - - /* Check for an authentication failure. */ - if (parser->status_code == 401 && - get_verb == s->verb && - t->owner->cred_acquire_cb) { - int allowed_types = 0; - - if (parse_unauthorized_response(&t->www_authenticate, - &allowed_types, &t->auth_mechanism) < 0) - return t->parse_error = PARSE_ERROR_GENERIC; - - if (allowed_types && - (!t->cred || 0 == (t->cred->credtype & allowed_types))) { - - if (t->owner->cred_acquire_cb(&t->cred, - t->owner->url, - allowed_types, - t->owner->cred_acquire_payload) < 0) - return PARSE_ERROR_GENERIC; - - assert(t->cred); - - /* Successfully acquired a credential. */ - return t->parse_error = PARSE_ERROR_REPLAY; - } - } - - /* Check for a 200 HTTP status code. */ - if (parser->status_code != 200) { - giterr_set(GITERR_NET, - "Unexpected HTTP status code: %d", - parser->status_code); - return t->parse_error = PARSE_ERROR_GENERIC; - } - - /* The response must contain a Content-Type header. */ - if (!t->content_type) { - giterr_set(GITERR_NET, "No Content-Type header in response"); - return t->parse_error = PARSE_ERROR_GENERIC; - } - - /* The Content-Type header must match our expectation. */ - if (get_verb == s->verb) - git_buf_printf(&buf, - "application/x-git-%s-advertisement", - ctx->s->service); - else - git_buf_printf(&buf, - "application/x-git-%s-result", - ctx->s->service); - - if (git_buf_oom(&buf)) - return t->parse_error = PARSE_ERROR_GENERIC; - - if (strcmp(t->content_type, git_buf_cstr(&buf))) { - git_buf_free(&buf); - giterr_set(GITERR_NET, - "Invalid Content-Type: %s", - t->content_type); - return t->parse_error = PARSE_ERROR_GENERIC; - } - - git_buf_free(&buf); - - return 0; -} - -static int on_message_complete(http_parser *parser) -{ - parser_context *ctx = (parser_context *) parser->data; - http_subtransport *t = ctx->t; - - t->parse_finished = 1; - - return 0; -} - -static int on_body_fill_buffer(http_parser *parser, const char *str, size_t len) -{ - parser_context *ctx = (parser_context *) parser->data; - http_subtransport *t = ctx->t; - - if (ctx->buf_size < len) { - giterr_set(GITERR_NET, "Can't fit data in the buffer"); - return t->parse_error = PARSE_ERROR_GENERIC; - } - - memcpy(ctx->buffer, str, len); - *(ctx->bytes_read) += len; - ctx->buffer += len; - ctx->buf_size -= len; - - return 0; -} - -static void clear_parser_state(http_subtransport *t) -{ - unsigned i; - char *entry; - - http_parser_init(&t->parser, HTTP_RESPONSE); - gitno_buffer_setup(&t->socket, - &t->parse_buffer, - t->parse_buffer_data, - sizeof(t->parse_buffer_data)); - - t->last_cb = NONE; - t->parse_error = 0; - t->parse_finished = 0; - - git_buf_free(&t->parse_header_name); - git_buf_init(&t->parse_header_name, 0); - - git_buf_free(&t->parse_header_value); - git_buf_init(&t->parse_header_value, 0); - - git__free(t->content_type); - t->content_type = NULL; - - git_vector_foreach(&t->www_authenticate, i, entry) - git__free(entry); - - git_vector_free(&t->www_authenticate); -} - -static int write_chunk(gitno_socket *socket, const char *buffer, size_t len) -{ - git_buf buf = GIT_BUF_INIT; - - /* Chunk header */ - git_buf_printf(&buf, "%X\r\n", (unsigned)len); - - if (git_buf_oom(&buf)) - return -1; - - if (gitno_send(socket, buf.ptr, buf.size, 0) < 0) { - git_buf_free(&buf); - return -1; - } - - git_buf_free(&buf); - - /* Chunk body */ - if (len > 0 && gitno_send(socket, buffer, len, 0) < 0) - return -1; - - /* Chunk footer */ - if (gitno_send(socket, "\r\n", 2, 0) < 0) - return -1; - - return 0; -} - -static int http_stream_read( - git_smart_subtransport_stream *stream, - char *buffer, - size_t buf_size, - size_t *bytes_read) -{ - http_stream *s = (http_stream *)stream; - http_subtransport *t = OWNING_SUBTRANSPORT(s); - parser_context ctx; - size_t bytes_parsed; - -replay: - *bytes_read = 0; - - assert(t->connected); - - if (!s->sent_request) { - git_buf request = GIT_BUF_INIT; - - clear_parser_state(t); - - if (gen_request(&request, s, 0) < 0) { - giterr_set(GITERR_NET, "Failed to generate request"); - return -1; - } - - if (gitno_send(&t->socket, request.ptr, request.size, 0) < 0) { - git_buf_free(&request); - return -1; - } - - git_buf_free(&request); - - s->sent_request = 1; - } - - if (!s->received_response) { - if (s->chunked) { - assert(s->verb == post_verb); - - /* Flush, if necessary */ - if (s->chunk_buffer_len > 0 && - write_chunk(&t->socket, s->chunk_buffer, s->chunk_buffer_len) < 0) - return -1; - - s->chunk_buffer_len = 0; - - /* Write the final chunk. */ - if (gitno_send(&t->socket, "0\r\n\r\n", 5, 0) < 0) - return -1; - } - - s->received_response = 1; - } - - while (!*bytes_read && !t->parse_finished) { - t->parse_buffer.offset = 0; - - if (gitno_recv(&t->parse_buffer) < 0) - return -1; - - /* This call to http_parser_execute will result in invocations of the - * on_* family of callbacks. The most interesting of these is - * on_body_fill_buffer, which is called when data is ready to be copied - * into the target buffer. We need to marshal the buffer, buf_size, and - * bytes_read parameters to this callback. */ - ctx.t = t; - ctx.s = s; - ctx.buffer = buffer; - ctx.buf_size = buf_size; - ctx.bytes_read = bytes_read; - - /* Set the context, call the parser, then unset the context. */ - t->parser.data = &ctx; - - bytes_parsed = http_parser_execute(&t->parser, - &t->settings, - t->parse_buffer.data, - t->parse_buffer.offset); - - t->parser.data = NULL; - - /* If there was a handled authentication failure, then parse_error - * will have signaled us that we should replay the request. */ - if (PARSE_ERROR_REPLAY == t->parse_error) { - s->sent_request = 0; - goto replay; - } - - if (t->parse_error < 0) - return -1; - - if (bytes_parsed != t->parse_buffer.offset) { - giterr_set(GITERR_NET, - "HTTP parser error: %s", - http_errno_description((enum http_errno)t->parser.http_errno)); - return -1; - } - } - - return 0; -} - -static int http_stream_write_chunked( - git_smart_subtransport_stream *stream, - const char *buffer, - size_t len) -{ - http_stream *s = (http_stream *)stream; - http_subtransport *t = OWNING_SUBTRANSPORT(s); - - assert(t->connected); - - /* Send the request, if necessary */ - if (!s->sent_request) { - git_buf request = GIT_BUF_INIT; - - clear_parser_state(t); - - if (gen_request(&request, s, 0) < 0) { - giterr_set(GITERR_NET, "Failed to generate request"); - return -1; - } - - if (gitno_send(&t->socket, request.ptr, request.size, 0) < 0) { - git_buf_free(&request); - return -1; - } - - git_buf_free(&request); - - s->sent_request = 1; - } - - if (len > CHUNK_SIZE) { - /* Flush, if necessary */ - if (s->chunk_buffer_len > 0) { - if (write_chunk(&t->socket, s->chunk_buffer, s->chunk_buffer_len) < 0) - return -1; - - s->chunk_buffer_len = 0; - } - - /* Write chunk directly */ - if (write_chunk(&t->socket, buffer, len) < 0) - return -1; - } - else { - /* Append as much to the buffer as we can */ - int count = min(CHUNK_SIZE - s->chunk_buffer_len, len); - - if (!s->chunk_buffer) - s->chunk_buffer = git__malloc(CHUNK_SIZE); - - memcpy(s->chunk_buffer + s->chunk_buffer_len, buffer, count); - s->chunk_buffer_len += count; - buffer += count; - len -= count; - - /* Is the buffer full? If so, then flush */ - if (CHUNK_SIZE == s->chunk_buffer_len) { - if (write_chunk(&t->socket, s->chunk_buffer, s->chunk_buffer_len) < 0) - return -1; - - s->chunk_buffer_len = 0; - - if (len > 0) { - memcpy(s->chunk_buffer, buffer, len); - s->chunk_buffer_len = len; - } - } - } - - return 0; -} - -static int http_stream_write_single( - git_smart_subtransport_stream *stream, - const char *buffer, - size_t len) -{ - http_stream *s = (http_stream *)stream; - http_subtransport *t = OWNING_SUBTRANSPORT(s); - git_buf request = GIT_BUF_INIT; - - assert(t->connected); - - if (s->sent_request) { - giterr_set(GITERR_NET, "Subtransport configured for only one write"); - return -1; - } - - clear_parser_state(t); - - if (gen_request(&request, s, len) < 0) { - giterr_set(GITERR_NET, "Failed to generate request"); - return -1; - } - - if (gitno_send(&t->socket, request.ptr, request.size, 0) < 0) - goto on_error; - - if (len && gitno_send(&t->socket, buffer, len, 0) < 0) - goto on_error; - - git_buf_free(&request); - s->sent_request = 1; - - return 0; - -on_error: - git_buf_free(&request); - return -1; -} - -static void http_stream_free(git_smart_subtransport_stream *stream) -{ - http_stream *s = (http_stream *)stream; - - if (s->chunk_buffer) - git__free(s->chunk_buffer); - - git__free(s); -} - -static int http_stream_alloc(http_subtransport *t, - git_smart_subtransport_stream **stream) -{ - http_stream *s; - - if (!stream) - return -1; - - s = git__calloc(sizeof(http_stream), 1); - GITERR_CHECK_ALLOC(s); - - s->parent.subtransport = &t->parent; - s->parent.read = http_stream_read; - s->parent.write = http_stream_write_single; - s->parent.free = http_stream_free; - - *stream = (git_smart_subtransport_stream *)s; - return 0; -} - -static int http_uploadpack_ls( - http_subtransport *t, - git_smart_subtransport_stream **stream) -{ - http_stream *s; - - if (http_stream_alloc(t, stream) < 0) - return -1; - - s = (http_stream *)*stream; - - s->service = upload_pack_service; - s->service_url = upload_pack_ls_service_url; - s->verb = get_verb; - - return 0; -} - -static int http_uploadpack( - http_subtransport *t, - git_smart_subtransport_stream **stream) -{ - http_stream *s; - - if (http_stream_alloc(t, stream) < 0) - return -1; - - s = (http_stream *)*stream; - - s->service = upload_pack_service; - s->service_url = upload_pack_service_url; - s->verb = post_verb; - - return 0; -} - -static int http_receivepack_ls( - http_subtransport *t, - git_smart_subtransport_stream **stream) -{ - http_stream *s; - - if (http_stream_alloc(t, stream) < 0) - return -1; - - s = (http_stream *)*stream; - - s->service = receive_pack_service; - s->service_url = receive_pack_ls_service_url; - s->verb = get_verb; - - return 0; -} - -static int http_receivepack( - http_subtransport *t, - git_smart_subtransport_stream **stream) -{ - http_stream *s; - - if (http_stream_alloc(t, stream) < 0) - return -1; - - s = (http_stream *)*stream; - - /* Use Transfer-Encoding: chunked for this request */ - s->chunked = 1; - s->parent.write = http_stream_write_chunked; - - s->service = receive_pack_service; - s->service_url = receive_pack_service_url; - s->verb = post_verb; - - return 0; -} - -static int http_action( - git_smart_subtransport_stream **stream, - git_smart_subtransport *subtransport, - const char *url, - git_smart_service_t action) -{ - http_subtransport *t = (http_subtransport *)subtransport; - const char *default_port = NULL; - int flags = 0, ret; - - if (!stream) - return -1; - - if (!t->host || !t->port || !t->path) { - if (!git__prefixcmp(url, prefix_http)) { - url = url + strlen(prefix_http); - default_port = "80"; - } - - if (!git__prefixcmp(url, prefix_https)) { - url += strlen(prefix_https); - default_port = "443"; - t->use_ssl = 1; - } - - if (!default_port) - return -1; - - if ((ret = gitno_extract_host_and_port(&t->host, &t->port, - url, default_port)) < 0) - return ret; - - t->path = strchr(url, '/'); - } - - if (!t->connected || - !http_should_keep_alive(&t->parser) || - !http_body_is_final(&t->parser)) { - - if (t->socket.socket) - gitno_close(&t->socket); - - if (t->use_ssl) { - int transport_flags; - - if (t->owner->parent.read_flags(&t->owner->parent, &transport_flags) < 0) - return -1; - - flags |= GITNO_CONNECT_SSL; - - if (GIT_TRANSPORTFLAGS_NO_CHECK_CERT & transport_flags) - flags |= GITNO_CONNECT_SSL_NO_CHECK_CERT; - } - - if (gitno_connect(&t->socket, t->host, t->port, flags) < 0) - return -1; - - t->connected = 1; - } - - switch (action) - { - case GIT_SERVICE_UPLOADPACK_LS: - return http_uploadpack_ls(t, stream); - - case GIT_SERVICE_UPLOADPACK: - return http_uploadpack(t, stream); - - case GIT_SERVICE_RECEIVEPACK_LS: - return http_receivepack_ls(t, stream); - - case GIT_SERVICE_RECEIVEPACK: - return http_receivepack(t, stream); - } - - *stream = NULL; - return -1; -} - -static int http_close(git_smart_subtransport *subtransport) -{ - http_subtransport *t = (http_subtransport *) subtransport; - - clear_parser_state(t); - - if (t->socket.socket) { - gitno_close(&t->socket); - memset(&t->socket, 0x0, sizeof(gitno_socket)); - } - - if (t->cred) { - t->cred->free(t->cred); - t->cred = NULL; - } - - if (t->host) { - git__free(t->host); - t->host = NULL; - } - - if (t->port) { - git__free(t->port); - t->port = NULL; - } - - return 0; -} - -static void http_free(git_smart_subtransport *subtransport) -{ - http_subtransport *t = (http_subtransport *) subtransport; - - http_close(subtransport); - - git__free(t); -} - -int git_smart_subtransport_http(git_smart_subtransport **out, git_transport *owner) -{ - http_subtransport *t; - - if (!out) - return -1; - - t = git__calloc(sizeof(http_subtransport), 1); - GITERR_CHECK_ALLOC(t); - - t->owner = (transport_smart *)owner; - t->parent.action = http_action; - t->parent.close = http_close; - t->parent.free = http_free; - - t->settings.on_header_field = on_header_field; - t->settings.on_header_value = on_header_value; - t->settings.on_headers_complete = on_headers_complete; - t->settings.on_body = on_body_fill_buffer; - t->settings.on_message_complete = on_message_complete; - - *out = (git_smart_subtransport *) t; - return 0; -} - -#endif /* !GIT_WINHTTP */ diff --git a/src/transports/local.c b/src/transports/local.c deleted file mode 100644 index 44431d58776..00000000000 --- a/src/transports/local.c +++ /dev/null @@ -1,438 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "common.h" -#include "git2/types.h" -#include "git2/net.h" -#include "git2/repository.h" -#include "git2/object.h" -#include "git2/tag.h" -#include "git2/transport.h" -#include "git2/revwalk.h" -#include "git2/odb_backend.h" -#include "git2/pack.h" -#include "git2/commit.h" -#include "git2/revparse.h" -#include "pack-objects.h" -#include "refs.h" -#include "posix.h" -#include "path.h" -#include "buffer.h" -#include "repository.h" -#include "odb.h" - -typedef struct { - git_transport parent; - git_remote *owner; - char *url; - int direction; - int flags; - git_atomic cancelled; - git_repository *repo; - git_vector refs; - unsigned connected : 1; -} transport_local; - -static int add_ref(transport_local *t, const char *name) -{ - const char peeled[] = "^{}"; - git_remote_head *head; - git_object *obj = NULL, *target = NULL; - git_buf buf = GIT_BUF_INIT; - int error; - - head = git__calloc(1, sizeof(git_remote_head)); - GITERR_CHECK_ALLOC(head); - - head->name = git__strdup(name); - GITERR_CHECK_ALLOC(head->name); - - error = git_reference_name_to_id(&head->oid, t->repo, name); - if (error < 0) { - git__free(head->name); - git__free(head); - if (!strcmp(name, GIT_HEAD_FILE) && error == GIT_ENOTFOUND) { - /* This is actually okay. Empty repos often have a HEAD that points to - * a nonexistent "refs/heads/master". */ - giterr_clear(); - return 0; - } - return error; - } - - if (git_vector_insert(&t->refs, head) < 0) - { - git__free(head->name); - git__free(head); - return -1; - } - - /* If it's not a tag, we don't need to try to peel it */ - if (git__prefixcmp(name, GIT_REFS_TAGS_DIR)) - return 0; - - if (git_object_lookup(&obj, t->repo, &head->oid, GIT_OBJ_ANY) < 0) - return -1; - - head = NULL; - - /* If it's not an annotated tag, just get out */ - if (git_object_type(obj) != GIT_OBJ_TAG) { - git_object_free(obj); - return 0; - } - - /* And if it's a tag, peel it, and add it to the list */ - head = git__calloc(1, sizeof(git_remote_head)); - GITERR_CHECK_ALLOC(head); - if (git_buf_join(&buf, 0, name, peeled) < 0) - return -1; - - head->name = git_buf_detach(&buf); - - if (git_tag_peel(&target, (git_tag *) obj) < 0) - goto on_error; - - git_oid_cpy(&head->oid, git_object_id(target)); - git_object_free(obj); - git_object_free(target); - - if (git_vector_insert(&t->refs, head) < 0) - return -1; - - return 0; - -on_error: - git_object_free(obj); - git_object_free(target); - return -1; -} - -static int store_refs(transport_local *t) -{ - unsigned int i; - git_strarray ref_names = {0}; - - assert(t); - - if (git_reference_list(&ref_names, t->repo, GIT_REF_LISTALL) < 0 || - git_vector_init(&t->refs, (unsigned int)ref_names.count, NULL) < 0) - goto on_error; - - /* Sort the references first */ - git__tsort((void **)ref_names.strings, ref_names.count, &git__strcmp_cb); - - /* Add HEAD */ - if (add_ref(t, GIT_HEAD_FILE) < 0) - goto on_error; - - for (i = 0; i < ref_names.count; ++i) { - if (add_ref(t, ref_names.strings[i]) < 0) - goto on_error; - } - - git_strarray_free(&ref_names); - return 0; - -on_error: - git_vector_free(&t->refs); - git_strarray_free(&ref_names); - return -1; -} - -/* - * Try to open the url as a git directory. The direction doesn't - * matter in this case because we're calulating the heads ourselves. - */ -static int local_connect( - git_transport *transport, - const char *url, - git_cred_acquire_cb cred_acquire_cb, - void *cred_acquire_payload, - int direction, int flags) -{ - git_repository *repo; - int error; - transport_local *t = (transport_local *) transport; - const char *path; - git_buf buf = GIT_BUF_INIT; - - GIT_UNUSED(cred_acquire_cb); - GIT_UNUSED(cred_acquire_payload); - - t->url = git__strdup(url); - GITERR_CHECK_ALLOC(t->url); - t->direction = direction; - t->flags = flags; - - /* The repo layer doesn't want the prefix */ - if (!git__prefixcmp(t->url, "file://")) { - if (git_path_fromurl(&buf, t->url) < 0) { - git_buf_free(&buf); - return -1; - } - path = git_buf_cstr(&buf); - - } else { /* We assume transport->url is already a path */ - path = t->url; - } - - error = git_repository_open(&repo, path); - - git_buf_free(&buf); - - if (error < 0) - return -1; - - t->repo = repo; - - if (store_refs(t) < 0) - return -1; - - t->connected = 1; - - return 0; -} - -static int local_ls(git_transport *transport, git_headlist_cb list_cb, void *payload) -{ - transport_local *t = (transport_local *)transport; - unsigned int i; - git_remote_head *head = NULL; - - if (!t->connected) { - giterr_set(GITERR_NET, "The transport is not connected"); - return -1; - } - - git_vector_foreach(&t->refs, i, head) { - if (list_cb(head, payload)) - return GIT_EUSER; - } - - return 0; -} - -static int local_negotiate_fetch( - git_transport *transport, - git_repository *repo, - const git_remote_head * const *refs, - size_t count) -{ - transport_local *t = (transport_local*)transport; - git_remote_head *rhead; - unsigned int i; - - GIT_UNUSED(refs); - GIT_UNUSED(count); - - /* Fill in the loids */ - git_vector_foreach(&t->refs, i, rhead) { - git_object *obj; - - int error = git_revparse_single(&obj, repo, rhead->name); - if (!error) - git_oid_cpy(&rhead->loid, git_object_id(obj)); - else if (error != GIT_ENOTFOUND) - return error; - git_object_free(obj); - giterr_clear(); - } - - return 0; -} - -typedef struct foreach_data { - git_transfer_progress *stats; - git_transfer_progress_callback progress_cb; - void *progress_payload; - git_odb_writepack *writepack; -} foreach_data; - -static int foreach_cb(void *buf, size_t len, void *payload) -{ - foreach_data *data = (foreach_data*)payload; - - data->stats->received_bytes += len; - return data->writepack->add(data->writepack, buf, len, data->stats); -} - -static int local_download_pack( - git_transport *transport, - git_repository *repo, - git_transfer_progress *stats, - git_transfer_progress_callback progress_cb, - void *progress_payload) -{ - transport_local *t = (transport_local*)transport; - git_revwalk *walk = NULL; - git_remote_head *rhead; - unsigned int i; - int error = -1; - git_oid oid; - git_packbuilder *pack = NULL; - git_odb_writepack *writepack = NULL; - git_odb *odb = NULL; - - if ((error = git_revwalk_new(&walk, t->repo)) < 0) - goto cleanup; - git_revwalk_sorting(walk, GIT_SORT_TIME); - - if ((error = git_packbuilder_new(&pack, t->repo)) < 0) - goto cleanup; - - stats->total_objects = 0; - stats->indexed_objects = 0; - stats->received_objects = 0; - stats->received_bytes = 0; - - git_vector_foreach(&t->refs, i, rhead) { - git_object *obj; - if ((error = git_object_lookup(&obj, t->repo, &rhead->oid, GIT_OBJ_ANY)) < 0) - goto cleanup; - - if (git_object_type(obj) == GIT_OBJ_COMMIT) { - /* Revwalker includes only wanted commits */ - error = git_revwalk_push(walk, &rhead->oid); - if (!git_oid_iszero(&rhead->loid)) - error = git_revwalk_hide(walk, &rhead->loid); - } else { - /* Tag or some other wanted object. Add it on its own */ - error = git_packbuilder_insert(pack, &rhead->oid, rhead->name); - } - git_object_free(obj); - } - - /* Walk the objects, building a packfile */ - if ((error = git_repository_odb__weakptr(&odb, repo)) < 0) - goto cleanup; - - while ((error = git_revwalk_next(&oid, walk)) == 0) { - git_commit *commit; - - /* Skip commits we already have */ - if (git_odb_exists(odb, &oid)) continue; - - if (!git_object_lookup((git_object**)&commit, t->repo, &oid, GIT_OBJ_COMMIT)) { - const git_oid *tree_oid = git_commit_tree_id(commit); - - /* Add the commit and its tree */ - if ((error = git_packbuilder_insert(pack, &oid, NULL)) < 0 || - (error = git_packbuilder_insert_tree(pack, tree_oid)) < 0) { - git_commit_free(commit); - goto cleanup; - } - - git_commit_free(commit); - } - } - - if ((error = git_odb_write_pack(&writepack, odb, progress_cb, progress_payload)) < 0) - goto cleanup; - - /* Write the data to the ODB */ - { - foreach_data data = {0}; - data.stats = stats; - data.progress_cb = progress_cb; - data.progress_payload = progress_payload; - data.writepack = writepack; - - if ((error = git_packbuilder_foreach(pack, foreach_cb, &data)) < 0) - goto cleanup; - } - error = writepack->commit(writepack, stats); - -cleanup: - if (writepack) writepack->free(writepack); - git_packbuilder_free(pack); - git_revwalk_free(walk); - return error; -} - -static int local_is_connected(git_transport *transport) -{ - transport_local *t = (transport_local *)transport; - - return t->connected; -} - -static int local_read_flags(git_transport *transport, int *flags) -{ - transport_local *t = (transport_local *)transport; - - *flags = t->flags; - - return 0; -} - -static void local_cancel(git_transport *transport) -{ - transport_local *t = (transport_local *)transport; - - git_atomic_set(&t->cancelled, 1); -} - -static int local_close(git_transport *transport) -{ - transport_local *t = (transport_local *)transport; - - t->connected = 0; - git_repository_free(t->repo); - t->repo = NULL; - - return 0; -} - -static void local_free(git_transport *transport) -{ - unsigned int i; - transport_local *t = (transport_local *) transport; - git_vector *vec = &t->refs; - git_remote_head *head; - - assert(transport); - - git_vector_foreach (vec, i, head) { - git__free(head->name); - git__free(head); - } - git_vector_free(vec); - - git__free(t->url); - git__free(t); -} - -/************** - * Public API * - **************/ - -int git_transport_local(git_transport **out, git_remote *owner, void *param) -{ - transport_local *t; - - GIT_UNUSED(param); - - t = git__calloc(1, sizeof(transport_local)); - GITERR_CHECK_ALLOC(t); - - t->parent.version = GIT_TRANSPORT_VERSION; - t->parent.connect = local_connect; - t->parent.negotiate_fetch = local_negotiate_fetch; - t->parent.download_pack = local_download_pack; - t->parent.close = local_close; - t->parent.free = local_free; - t->parent.ls = local_ls; - t->parent.is_connected = local_is_connected; - t->parent.read_flags = local_read_flags; - t->parent.cancel = local_cancel; - - t->owner = owner; - - *out = (git_transport *) t; - - return 0; -} diff --git a/src/transports/smart.c b/src/transports/smart.c deleted file mode 100644 index af6fec53531..00000000000 --- a/src/transports/smart.c +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "git2.h" -#include "smart.h" -#include "refs.h" - -static int git_smart__recv_cb(gitno_buffer *buf) -{ - transport_smart *t = (transport_smart *) buf->cb_data; - size_t old_len, bytes_read; - int error; - - assert(t->current_stream); - - old_len = buf->offset; - - if ((error = t->current_stream->read(t->current_stream, buf->data + buf->offset, buf->len - buf->offset, &bytes_read)) < 0) - return error; - - buf->offset += bytes_read; - - if (t->packetsize_cb) - t->packetsize_cb((int)bytes_read, t->packetsize_payload); - - return (int)(buf->offset - old_len); -} - -GIT_INLINE(int) git_smart__reset_stream(transport_smart *t, bool close_subtransport) -{ - if (t->current_stream) { - t->current_stream->free(t->current_stream); - t->current_stream = NULL; - } - - if (close_subtransport && - t->wrapped->close(t->wrapped) < 0) - return -1; - - return 0; -} - -static int git_smart__set_callbacks( - git_transport *transport, - git_transport_message_cb progress_cb, - git_transport_message_cb error_cb, - void *message_cb_payload) -{ - transport_smart *t = (transport_smart *)transport; - - t->progress_cb = progress_cb; - t->error_cb = error_cb; - t->message_cb_payload = message_cb_payload; - - return 0; -} - -static int git_smart__connect( - git_transport *transport, - const char *url, - git_cred_acquire_cb cred_acquire_cb, - void *cred_acquire_payload, - int direction, - int flags) -{ - transport_smart *t = (transport_smart *)transport; - git_smart_subtransport_stream *stream; - int error; - git_pkt *pkt; - git_pkt_ref *first; - git_smart_service_t service; - - if (git_smart__reset_stream(t, true) < 0) - return -1; - - t->url = git__strdup(url); - GITERR_CHECK_ALLOC(t->url); - - t->direction = direction; - t->flags = flags; - t->cred_acquire_cb = cred_acquire_cb; - t->cred_acquire_payload = cred_acquire_payload; - - if (GIT_DIRECTION_FETCH == t->direction) - service = GIT_SERVICE_UPLOADPACK_LS; - else if (GIT_DIRECTION_PUSH == t->direction) - service = GIT_SERVICE_RECEIVEPACK_LS; - else { - giterr_set(GITERR_NET, "Invalid direction"); - return -1; - } - - if ((error = t->wrapped->action(&stream, t->wrapped, t->url, service)) < 0) - return error; - - /* Save off the current stream (i.e. socket) that we are working with */ - t->current_stream = stream; - - gitno_buffer_setup_callback(NULL, &t->buffer, t->buffer_data, sizeof(t->buffer_data), git_smart__recv_cb, t); - - /* 2 flushes for RPC; 1 for stateful */ - if ((error = git_smart__store_refs(t, t->rpc ? 2 : 1)) < 0) - return error; - - /* Strip the comment packet for RPC */ - if (t->rpc) { - pkt = (git_pkt *)git_vector_get(&t->refs, 0); - - if (!pkt || GIT_PKT_COMMENT != pkt->type) { - giterr_set(GITERR_NET, "Invalid response"); - return -1; - } else { - /* Remove the comment pkt from the list */ - git_vector_remove(&t->refs, 0); - git__free(pkt); - } - } - - /* We now have loaded the refs. */ - t->have_refs = 1; - - first = (git_pkt_ref *)git_vector_get(&t->refs, 0); - - /* Detect capabilities */ - if (git_smart__detect_caps(first, &t->caps) < 0) - return -1; - - /* If the only ref in the list is capabilities^{} with OID_ZERO, remove it */ - if (1 == t->refs.length && !strcmp(first->head.name, "capabilities^{}") && - git_oid_iszero(&first->head.oid)) { - git_vector_clear(&t->refs); - git_pkt_free((git_pkt *)first); - } - - if (t->rpc && git_smart__reset_stream(t, false) < 0) - return -1; - - /* We're now logically connected. */ - t->connected = 1; - - return 0; -} - -static int git_smart__ls(git_transport *transport, git_headlist_cb list_cb, void *payload) -{ - transport_smart *t = (transport_smart *)transport; - unsigned int i; - git_pkt *p = NULL; - - if (!t->have_refs) { - giterr_set(GITERR_NET, "The transport has not yet loaded the refs"); - return -1; - } - - git_vector_foreach(&t->refs, i, p) { - git_pkt_ref *pkt = NULL; - - if (p->type != GIT_PKT_REF) - continue; - - pkt = (git_pkt_ref *)p; - - if (list_cb(&pkt->head, payload)) - return GIT_EUSER; - } - - return 0; -} - -int git_smart__negotiation_step(git_transport *transport, void *data, size_t len) -{ - transport_smart *t = (transport_smart *)transport; - git_smart_subtransport_stream *stream; - int error; - - if (t->rpc && git_smart__reset_stream(t, false) < 0) - return -1; - - if (GIT_DIRECTION_FETCH != t->direction) { - giterr_set(GITERR_NET, "This operation is only valid for fetch"); - return -1; - } - - if ((error = t->wrapped->action(&stream, t->wrapped, t->url, GIT_SERVICE_UPLOADPACK)) < 0) - return error; - - /* If this is a stateful implementation, the stream we get back should be the same */ - assert(t->rpc || t->current_stream == stream); - - /* Save off the current stream (i.e. socket) that we are working with */ - t->current_stream = stream; - - if ((error = stream->write(stream, (const char *)data, len)) < 0) - return error; - - gitno_buffer_setup_callback(NULL, &t->buffer, t->buffer_data, sizeof(t->buffer_data), git_smart__recv_cb, t); - - return 0; -} - -int git_smart__get_push_stream(transport_smart *t, git_smart_subtransport_stream **stream) -{ - int error; - - if (t->rpc && git_smart__reset_stream(t, false) < 0) - return -1; - - if (GIT_DIRECTION_PUSH != t->direction) { - giterr_set(GITERR_NET, "This operation is only valid for push"); - return -1; - } - - if ((error = t->wrapped->action(stream, t->wrapped, t->url, GIT_SERVICE_RECEIVEPACK)) < 0) - return error; - - /* If this is a stateful implementation, the stream we get back should be the same */ - assert(t->rpc || t->current_stream == *stream); - - /* Save off the current stream (i.e. socket) that we are working with */ - t->current_stream = *stream; - - gitno_buffer_setup_callback(NULL, &t->buffer, t->buffer_data, sizeof(t->buffer_data), git_smart__recv_cb, t); - - return 0; -} - -static void git_smart__cancel(git_transport *transport) -{ - transport_smart *t = (transport_smart *)transport; - - git_atomic_set(&t->cancelled, 1); -} - -static int git_smart__is_connected(git_transport *transport) -{ - transport_smart *t = (transport_smart *)transport; - - return t->connected; -} - -static int git_smart__read_flags(git_transport *transport, int *flags) -{ - transport_smart *t = (transport_smart *)transport; - - *flags = t->flags; - - return 0; -} - -static int git_smart__close(git_transport *transport) -{ - transport_smart *t = (transport_smart *)transport; - git_vector *refs = &t->refs; - git_vector *common = &t->common; - unsigned int i; - git_pkt *p; - int ret; - - ret = git_smart__reset_stream(t, true); - - git_vector_foreach(refs, i, p) - git_pkt_free(p); - - git_vector_free(refs); - - git_vector_foreach(common, i, p) - git_pkt_free(p); - - git_vector_free(common); - - if (t->url) { - git__free(t->url); - t->url = NULL; - } - - t->connected = 0; - - return ret; -} - -static void git_smart__free(git_transport *transport) -{ - transport_smart *t = (transport_smart *)transport; - - /* Make sure that the current stream is closed, if we have one. */ - git_smart__close(transport); - - /* Free the subtransport */ - t->wrapped->free(t->wrapped); - - git__free(t); -} - -int git_transport_smart(git_transport **out, git_remote *owner, void *param) -{ - transport_smart *t; - git_smart_subtransport_definition *definition = (git_smart_subtransport_definition *)param; - - if (!param) - return -1; - - t = git__calloc(sizeof(transport_smart), 1); - GITERR_CHECK_ALLOC(t); - - t->parent.version = GIT_TRANSPORT_VERSION; - t->parent.set_callbacks = git_smart__set_callbacks; - t->parent.connect = git_smart__connect; - t->parent.close = git_smart__close; - t->parent.free = git_smart__free; - t->parent.negotiate_fetch = git_smart__negotiate_fetch; - t->parent.download_pack = git_smart__download_pack; - t->parent.push = git_smart__push; - t->parent.ls = git_smart__ls; - t->parent.is_connected = git_smart__is_connected; - t->parent.read_flags = git_smart__read_flags; - t->parent.cancel = git_smart__cancel; - - t->owner = owner; - t->rpc = definition->rpc; - - if (git_vector_init(&t->refs, 16, NULL) < 0) { - git__free(t); - return -1; - } - - if (definition->callback(&t->wrapped, &t->parent) < 0) { - git__free(t); - return -1; - } - - *out = (git_transport *) t; - return 0; -} diff --git a/src/transports/smart.h b/src/transports/smart.h deleted file mode 100644 index a9e894b65aa..00000000000 --- a/src/transports/smart.h +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "git2.h" -#include "vector.h" -#include "netops.h" -#include "buffer.h" -#include "push.h" - -#define GIT_SIDE_BAND_DATA 1 -#define GIT_SIDE_BAND_PROGRESS 2 -#define GIT_SIDE_BAND_ERROR 3 - -#define GIT_CAP_OFS_DELTA "ofs-delta" -#define GIT_CAP_MULTI_ACK "multi_ack" -#define GIT_CAP_SIDE_BAND "side-band" -#define GIT_CAP_SIDE_BAND_64K "side-band-64k" -#define GIT_CAP_INCLUDE_TAG "include-tag" -#define GIT_CAP_DELETE_REFS "delete-refs" -#define GIT_CAP_REPORT_STATUS "report-status" - -enum git_pkt_type { - GIT_PKT_CMD, - GIT_PKT_FLUSH, - GIT_PKT_REF, - GIT_PKT_HAVE, - GIT_PKT_ACK, - GIT_PKT_NAK, - GIT_PKT_PACK, - GIT_PKT_COMMENT, - GIT_PKT_ERR, - GIT_PKT_DATA, - GIT_PKT_PROGRESS, - GIT_PKT_OK, - GIT_PKT_NG, - GIT_PKT_UNPACK, -}; - -/* Used for multi-ack */ -enum git_ack_status { - GIT_ACK_NONE, - GIT_ACK_CONTINUE, - GIT_ACK_COMMON, - GIT_ACK_READY -}; - -/* This would be a flush pkt */ -typedef struct { - enum git_pkt_type type; -} git_pkt; - -struct git_pkt_cmd { - enum git_pkt_type type; - char *cmd; - char *path; - char *host; -}; - -/* This is a pkt-line with some info in it */ -typedef struct { - enum git_pkt_type type; - git_remote_head head; - char *capabilities; -} git_pkt_ref; - -/* Useful later */ -typedef struct { - enum git_pkt_type type; - git_oid oid; - enum git_ack_status status; -} git_pkt_ack; - -typedef struct { - enum git_pkt_type type; - char comment[GIT_FLEX_ARRAY]; -} git_pkt_comment; - -typedef struct { - enum git_pkt_type type; - int len; - char data[GIT_FLEX_ARRAY]; -} git_pkt_data; - -typedef git_pkt_data git_pkt_progress; - -typedef struct { - enum git_pkt_type type; - char error[GIT_FLEX_ARRAY]; -} git_pkt_err; - -typedef struct { - enum git_pkt_type type; - char *ref; -} git_pkt_ok; - -typedef struct { - enum git_pkt_type type; - char *ref; - char *msg; -} git_pkt_ng; - -typedef struct { - enum git_pkt_type type; - int unpack_ok; -} git_pkt_unpack; - -typedef struct transport_smart_caps { - int common:1, - ofs_delta:1, - multi_ack: 1, - side_band:1, - side_band_64k:1, - include_tag:1, - delete_refs:1, - report_status:1; -} transport_smart_caps; - -typedef void (*packetsize_cb)(size_t received, void *payload); - -typedef struct { - git_transport parent; - git_remote *owner; - char *url; - git_cred_acquire_cb cred_acquire_cb; - void *cred_acquire_payload; - int direction; - int flags; - git_transport_message_cb progress_cb; - git_transport_message_cb error_cb; - void *message_cb_payload; - git_smart_subtransport *wrapped; - git_smart_subtransport_stream *current_stream; - transport_smart_caps caps; - git_vector refs; - git_vector common; - git_atomic cancelled; - packetsize_cb packetsize_cb; - void *packetsize_payload; - unsigned rpc : 1, - have_refs : 1, - connected : 1; - gitno_buffer buffer; - char buffer_data[65536]; -} transport_smart; - -/* smart_protocol.c */ -int git_smart__store_refs(transport_smart *t, int flushes); -int git_smart__detect_caps(git_pkt_ref *pkt, transport_smart_caps *caps); -int git_smart__push(git_transport *transport, git_push *push); - -int git_smart__negotiate_fetch( - git_transport *transport, - git_repository *repo, - const git_remote_head * const *refs, - size_t count); - -int git_smart__download_pack( - git_transport *transport, - git_repository *repo, - git_transfer_progress *stats, - git_transfer_progress_callback progress_cb, - void *progress_payload); - -/* smart.c */ -int git_smart__negotiation_step(git_transport *transport, void *data, size_t len); -int git_smart__get_push_stream(transport_smart *t, git_smart_subtransport_stream **out); - -/* smart_pkt.c */ -int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_t len); -int git_pkt_buffer_flush(git_buf *buf); -int git_pkt_send_flush(GIT_SOCKET s); -int git_pkt_buffer_done(git_buf *buf); -int git_pkt_buffer_wants(const git_remote_head * const *refs, size_t count, transport_smart_caps *caps, git_buf *buf); -int git_pkt_buffer_have(git_oid *oid, git_buf *buf); -void git_pkt_free(git_pkt *pkt); diff --git a/src/transports/smart_pkt.c b/src/transports/smart_pkt.c deleted file mode 100644 index 51edd9179bf..00000000000 --- a/src/transports/smart_pkt.c +++ /dev/null @@ -1,524 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" - -#include "git2/types.h" -#include "git2/errors.h" -#include "git2/refs.h" -#include "git2/revwalk.h" - -#include "smart.h" -#include "util.h" -#include "netops.h" -#include "posix.h" -#include "buffer.h" - -#include - -#define PKT_LEN_SIZE 4 -static const char pkt_done_str[] = "0009done\n"; -static const char pkt_flush_str[] = "0000"; -static const char pkt_have_prefix[] = "0032have "; -static const char pkt_want_prefix[] = "0032want "; - -static int flush_pkt(git_pkt **out) -{ - git_pkt *pkt; - - pkt = git__malloc(sizeof(git_pkt)); - GITERR_CHECK_ALLOC(pkt); - - pkt->type = GIT_PKT_FLUSH; - *out = pkt; - - return 0; -} - -/* the rest of the line will be useful for multi_ack */ -static int ack_pkt(git_pkt **out, const char *line, size_t len) -{ - git_pkt_ack *pkt; - GIT_UNUSED(line); - GIT_UNUSED(len); - - pkt = git__calloc(1, sizeof(git_pkt_ack)); - GITERR_CHECK_ALLOC(pkt); - - pkt->type = GIT_PKT_ACK; - line += 3; - len -= 3; - - if (len >= GIT_OID_HEXSZ) { - git_oid_fromstr(&pkt->oid, line + 1); - line += GIT_OID_HEXSZ + 1; - len -= GIT_OID_HEXSZ + 1; - } - - if (len >= 7) { - if (!git__prefixcmp(line + 1, "continue")) - pkt->status = GIT_ACK_CONTINUE; - } - - *out = (git_pkt *) pkt; - - return 0; -} - -static int nak_pkt(git_pkt **out) -{ - git_pkt *pkt; - - pkt = git__malloc(sizeof(git_pkt)); - GITERR_CHECK_ALLOC(pkt); - - pkt->type = GIT_PKT_NAK; - *out = pkt; - - return 0; -} - -static int pack_pkt(git_pkt **out) -{ - git_pkt *pkt; - - pkt = git__malloc(sizeof(git_pkt)); - GITERR_CHECK_ALLOC(pkt); - - pkt->type = GIT_PKT_PACK; - *out = pkt; - - return 0; -} - -static int comment_pkt(git_pkt **out, const char *line, size_t len) -{ - git_pkt_comment *pkt; - - pkt = git__malloc(sizeof(git_pkt_comment) + len + 1); - GITERR_CHECK_ALLOC(pkt); - - pkt->type = GIT_PKT_COMMENT; - memcpy(pkt->comment, line, len); - pkt->comment[len] = '\0'; - - *out = (git_pkt *) pkt; - - return 0; -} - -static int err_pkt(git_pkt **out, const char *line, size_t len) -{ - git_pkt_err *pkt; - - /* Remove "ERR " from the line */ - line += 4; - len -= 4; - pkt = git__malloc(sizeof(git_pkt_err) + len + 1); - GITERR_CHECK_ALLOC(pkt); - - pkt->type = GIT_PKT_ERR; - memcpy(pkt->error, line, len); - pkt->error[len] = '\0'; - - *out = (git_pkt *) pkt; - - return 0; -} - -static int data_pkt(git_pkt **out, const char *line, size_t len) -{ - git_pkt_data *pkt; - - line++; - len--; - pkt = git__malloc(sizeof(git_pkt_data) + len); - GITERR_CHECK_ALLOC(pkt); - - pkt->type = GIT_PKT_DATA; - pkt->len = (int) len; - memcpy(pkt->data, line, len); - - *out = (git_pkt *) pkt; - - return 0; -} - -static int progress_pkt(git_pkt **out, const char *line, size_t len) -{ - git_pkt_progress *pkt; - - line++; - len--; - pkt = git__malloc(sizeof(git_pkt_progress) + len); - GITERR_CHECK_ALLOC(pkt); - - pkt->type = GIT_PKT_PROGRESS; - pkt->len = (int) len; - memcpy(pkt->data, line, len); - - *out = (git_pkt *) pkt; - - return 0; -} - -/* - * Parse an other-ref line. - */ -static int ref_pkt(git_pkt **out, const char *line, size_t len) -{ - int error; - git_pkt_ref *pkt; - - pkt = git__malloc(sizeof(git_pkt_ref)); - GITERR_CHECK_ALLOC(pkt); - - memset(pkt, 0x0, sizeof(git_pkt_ref)); - pkt->type = GIT_PKT_REF; - if ((error = git_oid_fromstr(&pkt->head.oid, line)) < 0) - goto error_out; - - /* Check for a bit of consistency */ - if (line[GIT_OID_HEXSZ] != ' ') { - giterr_set(GITERR_NET, "Error parsing pkt-line"); - error = -1; - goto error_out; - } - - /* Jump from the name */ - line += GIT_OID_HEXSZ + 1; - len -= (GIT_OID_HEXSZ + 1); - - if (line[len - 1] == '\n') - --len; - - pkt->head.name = git__malloc(len + 1); - GITERR_CHECK_ALLOC(pkt->head.name); - - memcpy(pkt->head.name, line, len); - pkt->head.name[len] = '\0'; - - if (strlen(pkt->head.name) < len) { - pkt->capabilities = strchr(pkt->head.name, '\0') + 1; - } - - *out = (git_pkt *)pkt; - return 0; - -error_out: - git__free(pkt); - return error; -} - -static int ok_pkt(git_pkt **out, const char *line, size_t len) -{ - git_pkt_ok *pkt; - const char *ptr; - - pkt = git__malloc(sizeof(*pkt)); - GITERR_CHECK_ALLOC(pkt); - - pkt->type = GIT_PKT_OK; - - line += 3; /* skip "ok " */ - ptr = strchr(line, '\n'); - len = ptr - line; - - pkt->ref = git__malloc(len + 1); - GITERR_CHECK_ALLOC(pkt->ref); - - memcpy(pkt->ref, line, len); - pkt->ref[len] = '\0'; - - *out = (git_pkt *)pkt; - return 0; -} - -static int ng_pkt(git_pkt **out, const char *line, size_t len) -{ - git_pkt_ng *pkt; - const char *ptr; - - pkt = git__malloc(sizeof(*pkt)); - GITERR_CHECK_ALLOC(pkt); - - pkt->type = GIT_PKT_NG; - - line += 3; /* skip "ng " */ - ptr = strchr(line, ' '); - len = ptr - line; - - pkt->ref = git__malloc(len + 1); - GITERR_CHECK_ALLOC(pkt->ref); - - memcpy(pkt->ref, line, len); - pkt->ref[len] = '\0'; - - line = ptr + 1; - ptr = strchr(line, '\n'); - len = ptr - line; - - pkt->msg = git__malloc(len + 1); - GITERR_CHECK_ALLOC(pkt->msg); - - memcpy(pkt->msg, line, len); - pkt->msg[len] = '\0'; - - *out = (git_pkt *)pkt; - return 0; -} - -static int unpack_pkt(git_pkt **out, const char *line, size_t len) -{ - git_pkt_unpack *pkt; - - GIT_UNUSED(len); - - pkt = git__malloc(sizeof(*pkt)); - GITERR_CHECK_ALLOC(pkt); - - pkt->type = GIT_PKT_UNPACK; - if (!git__prefixcmp(line, "unpack ok")) - pkt->unpack_ok = 1; - else - pkt->unpack_ok = 0; - - *out = (git_pkt *)pkt; - return 0; -} - -static int32_t parse_len(const char *line) -{ - char num[PKT_LEN_SIZE + 1]; - int i, error; - int32_t len; - const char *num_end; - - memcpy(num, line, PKT_LEN_SIZE); - num[PKT_LEN_SIZE] = '\0'; - - for (i = 0; i < PKT_LEN_SIZE; ++i) { - if (!isxdigit(num[i])) { - giterr_set(GITERR_NET, "Found invalid hex digit in length"); - return -1; - } - } - - if ((error = git__strtol32(&len, num, &num_end, 16)) < 0) - return error; - - return len; -} - -/* - * As per the documentation, the syntax is: - * - * pkt-line = data-pkt / flush-pkt - * data-pkt = pkt-len pkt-payload - * pkt-len = 4*(HEXDIG) - * pkt-payload = (pkt-len -4)*(OCTET) - * flush-pkt = "0000" - * - * Which means that the first four bytes are the length of the line, - * in ASCII hexadecimal (including itself) - */ - -int git_pkt_parse_line( - git_pkt **head, const char *line, const char **out, size_t bufflen) -{ - int ret; - int32_t len; - - /* Not even enough for the length */ - if (bufflen > 0 && bufflen < PKT_LEN_SIZE) - return GIT_EBUFS; - - len = parse_len(line); - if (len < 0) { - /* - * If we fail to parse the length, it might be because the - * server is trying to send us the packfile already. - */ - if (bufflen >= 4 && !git__prefixcmp(line, "PACK")) { - giterr_clear(); - *out = line; - return pack_pkt(head); - } - - return (int)len; - } - - /* - * If we were given a buffer length, then make sure there is - * enough in the buffer to satisfy this line - */ - if (bufflen > 0 && bufflen < (size_t)len) - return GIT_EBUFS; - - line += PKT_LEN_SIZE; - /* - * TODO: How do we deal with empty lines? Try again? with the next - * line? - */ - if (len == PKT_LEN_SIZE) { - *out = line; - return 0; - } - - if (len == 0) { /* Flush pkt */ - *out = line; - return flush_pkt(head); - } - - len -= PKT_LEN_SIZE; /* the encoded length includes its own size */ - - if (*line == GIT_SIDE_BAND_DATA) - ret = data_pkt(head, line, len); - else if (*line == GIT_SIDE_BAND_PROGRESS) - ret = progress_pkt(head, line, len); - else if (!git__prefixcmp(line, "ACK")) - ret = ack_pkt(head, line, len); - else if (!git__prefixcmp(line, "NAK")) - ret = nak_pkt(head); - else if (!git__prefixcmp(line, "ERR ")) - ret = err_pkt(head, line, len); - else if (*line == '#') - ret = comment_pkt(head, line, len); - else if (!git__prefixcmp(line, "ok")) - ret = ok_pkt(head, line, len); - else if (!git__prefixcmp(line, "ng")) - ret = ng_pkt(head, line, len); - else if (!git__prefixcmp(line, "unpack")) - ret = unpack_pkt(head, line, len); - else - ret = ref_pkt(head, line, len); - - *out = line + len; - - return ret; -} - -void git_pkt_free(git_pkt *pkt) -{ - if (pkt->type == GIT_PKT_REF) { - git_pkt_ref *p = (git_pkt_ref *) pkt; - git__free(p->head.name); - } - - if (pkt->type == GIT_PKT_OK) { - git_pkt_ok *p = (git_pkt_ok *) pkt; - git__free(p->ref); - } - - if (pkt->type == GIT_PKT_NG) { - git_pkt_ng *p = (git_pkt_ng *) pkt; - git__free(p->ref); - git__free(p->msg); - } - - git__free(pkt); -} - -int git_pkt_buffer_flush(git_buf *buf) -{ - return git_buf_put(buf, pkt_flush_str, strlen(pkt_flush_str)); -} - -static int buffer_want_with_caps(const git_remote_head *head, transport_smart_caps *caps, git_buf *buf) -{ - git_buf str = GIT_BUF_INIT; - char oid[GIT_OID_HEXSZ +1] = {0}; - unsigned int len; - - /* Prefer side-band-64k if the server supports both */ - if (caps->side_band) { - if (caps->side_band_64k) - git_buf_printf(&str, "%s ", GIT_CAP_SIDE_BAND_64K); - else - git_buf_printf(&str, "%s ", GIT_CAP_SIDE_BAND); - } - if (caps->ofs_delta) - git_buf_puts(&str, GIT_CAP_OFS_DELTA " "); - - if (caps->multi_ack) - git_buf_puts(&str, GIT_CAP_MULTI_ACK " "); - - if (caps->include_tag) - git_buf_puts(&str, GIT_CAP_INCLUDE_TAG " "); - - if (git_buf_oom(&str)) - return -1; - - len = (unsigned int) - (strlen("XXXXwant ") + GIT_OID_HEXSZ + 1 /* NUL */ + - git_buf_len(&str) + 1 /* LF */); - git_buf_grow(buf, git_buf_len(buf) + len); - git_oid_fmt(oid, &head->oid); - git_buf_printf(buf, "%04xwant %s %s\n", len, oid, git_buf_cstr(&str)); - git_buf_free(&str); - - return git_buf_oom(buf); -} - -/* - * All "want" packets have the same length and format, so what we do - * is overwrite the OID each time. - */ - -int git_pkt_buffer_wants( - const git_remote_head * const *refs, - size_t count, - transport_smart_caps *caps, - git_buf *buf) -{ - size_t i = 0; - const git_remote_head *head; - - if (caps->common) { - for (; i < count; ++i) { - head = refs[i]; - if (!head->local) - break; - } - - if (buffer_want_with_caps(refs[i], caps, buf) < 0) - return -1; - - i++; - } - - for (; i < count; ++i) { - char oid[GIT_OID_HEXSZ]; - - head = refs[i]; - if (head->local) - continue; - - git_oid_fmt(oid, &head->oid); - git_buf_put(buf, pkt_want_prefix, strlen(pkt_want_prefix)); - git_buf_put(buf, oid, GIT_OID_HEXSZ); - git_buf_putc(buf, '\n'); - if (git_buf_oom(buf)) - return -1; - } - - return git_pkt_buffer_flush(buf); -} - -int git_pkt_buffer_have(git_oid *oid, git_buf *buf) -{ - char oidhex[GIT_OID_HEXSZ + 1]; - - memset(oidhex, 0x0, sizeof(oidhex)); - git_oid_fmt(oidhex, oid); - return git_buf_printf(buf, "%s%s\n", pkt_have_prefix, oidhex); -} - -int git_pkt_buffer_done(git_buf *buf) -{ - return git_buf_puts(buf, pkt_done_str); -} diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c deleted file mode 100644 index 184b21a0b32..00000000000 --- a/src/transports/smart_protocol.c +++ /dev/null @@ -1,703 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "git2.h" - -#include "smart.h" -#include "refs.h" -#include "repository.h" -#include "push.h" -#include "pack-objects.h" -#include "remote.h" - -#define NETWORK_XFER_THRESHOLD (100*1024) - -int git_smart__store_refs(transport_smart *t, int flushes) -{ - gitno_buffer *buf = &t->buffer; - git_vector *refs = &t->refs; - int error, flush = 0, recvd; - const char *line_end; - git_pkt *pkt; - - /* Clear existing refs in case git_remote_connect() is called again - * after git_remote_disconnect(). - */ - git_vector_clear(refs); - - do { - if (buf->offset > 0) - error = git_pkt_parse_line(&pkt, buf->data, &line_end, buf->offset); - else - error = GIT_EBUFS; - - if (error < 0 && error != GIT_EBUFS) - return -1; - - if (error == GIT_EBUFS) { - if ((recvd = gitno_recv(buf)) < 0) - return -1; - - if (recvd == 0 && !flush) { - giterr_set(GITERR_NET, "Early EOF"); - return -1; - } - - continue; - } - - gitno_consume(buf, line_end); - if (pkt->type == GIT_PKT_ERR) { - giterr_set(GITERR_NET, "Remote error: %s", ((git_pkt_err *)pkt)->error); - git__free(pkt); - return -1; - } - - if (pkt->type != GIT_PKT_FLUSH && git_vector_insert(refs, pkt) < 0) - return -1; - - if (pkt->type == GIT_PKT_FLUSH) { - flush++; - git_pkt_free(pkt); - } - } while (flush < flushes); - - return flush; -} - -int git_smart__detect_caps(git_pkt_ref *pkt, transport_smart_caps *caps) -{ - const char *ptr; - - /* No refs or capabilites, odd but not a problem */ - if (pkt == NULL || pkt->capabilities == NULL) - return 0; - - ptr = pkt->capabilities; - while (ptr != NULL && *ptr != '\0') { - if (*ptr == ' ') - ptr++; - - if (!git__prefixcmp(ptr, GIT_CAP_OFS_DELTA)) { - caps->common = caps->ofs_delta = 1; - ptr += strlen(GIT_CAP_OFS_DELTA); - continue; - } - - if (!git__prefixcmp(ptr, GIT_CAP_MULTI_ACK)) { - caps->common = caps->multi_ack = 1; - ptr += strlen(GIT_CAP_MULTI_ACK); - continue; - } - - if (!git__prefixcmp(ptr, GIT_CAP_INCLUDE_TAG)) { - caps->common = caps->include_tag = 1; - ptr += strlen(GIT_CAP_INCLUDE_TAG); - continue; - } - - /* Keep side-band check after side-band-64k */ - if (!git__prefixcmp(ptr, GIT_CAP_SIDE_BAND_64K)) { - caps->common = caps->side_band_64k = 1; - ptr += strlen(GIT_CAP_SIDE_BAND_64K); - continue; - } - - if (!git__prefixcmp(ptr, GIT_CAP_SIDE_BAND)) { - caps->common = caps->side_band = 1; - ptr += strlen(GIT_CAP_SIDE_BAND); - continue; - } - - if (!git__prefixcmp(ptr, GIT_CAP_DELETE_REFS)) { - caps->common = caps->delete_refs = 1; - ptr += strlen(GIT_CAP_DELETE_REFS); - continue; - } - - /* We don't know this capability, so skip it */ - ptr = strchr(ptr, ' '); - } - - return 0; -} - -static int recv_pkt(git_pkt **out, gitno_buffer *buf) -{ - const char *ptr = buf->data, *line_end = ptr; - git_pkt *pkt; - int pkt_type, error = 0, ret; - - do { - if (buf->offset > 0) - error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->offset); - else - error = GIT_EBUFS; - - if (error == 0) - break; /* return the pkt */ - - if (error < 0 && error != GIT_EBUFS) - return -1; - - if ((ret = gitno_recv(buf)) < 0) - return -1; - } while (error); - - gitno_consume(buf, line_end); - pkt_type = pkt->type; - if (out != NULL) - *out = pkt; - else - git__free(pkt); - - return pkt_type; -} - -static int store_common(transport_smart *t) -{ - git_pkt *pkt = NULL; - gitno_buffer *buf = &t->buffer; - - do { - if (recv_pkt(&pkt, buf) < 0) - return -1; - - if (pkt->type == GIT_PKT_ACK) { - if (git_vector_insert(&t->common, pkt) < 0) - return -1; - } else { - git__free(pkt); - return 0; - } - - } while (1); - - return 0; -} - -static int fetch_setup_walk(git_revwalk **out, git_repository *repo) -{ - git_revwalk *walk; - git_strarray refs; - unsigned int i; - git_reference *ref; - - if (git_reference_list(&refs, repo, GIT_REF_LISTALL) < 0) - return -1; - - if (git_revwalk_new(&walk, repo) < 0) - return -1; - - git_revwalk_sorting(walk, GIT_SORT_TIME); - - for (i = 0; i < refs.count; ++i) { - /* No tags */ - if (!git__prefixcmp(refs.strings[i], GIT_REFS_TAGS_DIR)) - continue; - - if (git_reference_lookup(&ref, repo, refs.strings[i]) < 0) - goto on_error; - - if (git_reference_type(ref) == GIT_REF_SYMBOLIC) - continue; - if (git_revwalk_push(walk, git_reference_target(ref)) < 0) - goto on_error; - - git_reference_free(ref); - } - - git_strarray_free(&refs); - *out = walk; - return 0; - -on_error: - git_reference_free(ref); - git_strarray_free(&refs); - return -1; -} - -int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, const git_remote_head * const *refs, size_t count) -{ - transport_smart *t = (transport_smart *)transport; - gitno_buffer *buf = &t->buffer; - git_buf data = GIT_BUF_INIT; - git_revwalk *walk = NULL; - int error = -1, pkt_type; - unsigned int i; - git_oid oid; - - /* No own logic, do our thing */ - if ((error = git_pkt_buffer_wants(refs, count, &t->caps, &data)) < 0) - return error; - - if ((error = fetch_setup_walk(&walk, repo)) < 0) - goto on_error; - /* - * We don't support any kind of ACK extensions, so the negotiation - * boils down to sending what we have and listening for an ACK - * every once in a while. - */ - i = 0; - while (true) { - error = git_revwalk_next(&oid, walk); - - if (error < 0) { - if (GIT_ITEROVER == error) - break; - - goto on_error; - } - - git_pkt_buffer_have(&oid, &data); - i++; - if (i % 20 == 0) { - if (t->cancelled.val) { - giterr_set(GITERR_NET, "The fetch was cancelled by the user"); - error = GIT_EUSER; - goto on_error; - } - - git_pkt_buffer_flush(&data); - if (git_buf_oom(&data)) { - error = -1; - goto on_error; - } - - if ((error = git_smart__negotiation_step(&t->parent, data.ptr, data.size)) < 0) - goto on_error; - - git_buf_clear(&data); - if (t->caps.multi_ack) { - if ((error = store_common(t)) < 0) - goto on_error; - } else { - pkt_type = recv_pkt(NULL, buf); - - if (pkt_type == GIT_PKT_ACK) { - break; - } else if (pkt_type == GIT_PKT_NAK) { - continue; - } else if (pkt_type < 0) { - /* recv_pkt returned an error */ - error = pkt_type; - goto on_error; - } else { - giterr_set(GITERR_NET, "Unexpected pkt type"); - error = -1; - goto on_error; - } - } - } - - if (t->common.length > 0) - break; - - if (i % 20 == 0 && t->rpc) { - git_pkt_ack *pkt; - unsigned int i; - - if ((error = git_pkt_buffer_wants(refs, count, &t->caps, &data)) < 0) - goto on_error; - - git_vector_foreach(&t->common, i, pkt) { - if ((error = git_pkt_buffer_have(&pkt->oid, &data)) < 0) - goto on_error; - } - - if (git_buf_oom(&data)) { - error = -1; - goto on_error; - } - } - } - - /* Tell the other end that we're done negotiating */ - if (t->rpc && t->common.length > 0) { - git_pkt_ack *pkt; - unsigned int i; - - if ((error = git_pkt_buffer_wants(refs, count, &t->caps, &data)) < 0) - goto on_error; - - git_vector_foreach(&t->common, i, pkt) { - if ((error = git_pkt_buffer_have(&pkt->oid, &data)) < 0) - goto on_error; - } - - if (git_buf_oom(&data)) { - error = -1; - goto on_error; - } - } - - if ((error = git_pkt_buffer_done(&data)) < 0) - goto on_error; - - if (t->cancelled.val) { - giterr_set(GITERR_NET, "The fetch was cancelled by the user"); - error = GIT_EUSER; - goto on_error; - } - if ((error = git_smart__negotiation_step(&t->parent, data.ptr, data.size)) < 0) - goto on_error; - - git_buf_free(&data); - git_revwalk_free(walk); - - /* Now let's eat up whatever the server gives us */ - if (!t->caps.multi_ack) { - pkt_type = recv_pkt(NULL, buf); - - if (pkt_type < 0) { - return pkt_type; - } else if (pkt_type != GIT_PKT_ACK && pkt_type != GIT_PKT_NAK) { - giterr_set(GITERR_NET, "Unexpected pkt type"); - return -1; - } - } else { - git_pkt_ack *pkt; - do { - if ((error = recv_pkt((git_pkt **)&pkt, buf)) < 0) - return error; - - if (pkt->type == GIT_PKT_NAK || - (pkt->type == GIT_PKT_ACK && pkt->status != GIT_ACK_CONTINUE)) { - git__free(pkt); - break; - } - - git__free(pkt); - } while (1); - } - - return 0; - -on_error: - git_revwalk_free(walk); - git_buf_free(&data); - return error; -} - -static int no_sideband(transport_smart *t, struct git_odb_writepack *writepack, gitno_buffer *buf, git_transfer_progress *stats) -{ - int recvd; - - do { - if (t->cancelled.val) { - giterr_set(GITERR_NET, "The fetch was cancelled by the user"); - return GIT_EUSER; - } - - if (writepack->add(writepack, buf->data, buf->offset, stats) < 0) - return -1; - - gitno_consume_n(buf, buf->offset); - - if ((recvd = gitno_recv(buf)) < 0) - return -1; - } while(recvd > 0); - - if (writepack->commit(writepack, stats)) - return -1; - - return 0; -} - -struct network_packetsize_payload -{ - git_transfer_progress_callback callback; - void *payload; - git_transfer_progress *stats; - size_t last_fired_bytes; -}; - -static void network_packetsize(size_t received, void *payload) -{ - struct network_packetsize_payload *npp = (struct network_packetsize_payload*)payload; - - /* Accumulate bytes */ - npp->stats->received_bytes += received; - - /* Fire notification if the threshold is reached */ - if ((npp->stats->received_bytes - npp->last_fired_bytes) > NETWORK_XFER_THRESHOLD) { - npp->last_fired_bytes = npp->stats->received_bytes; - npp->callback(npp->stats, npp->payload); - } -} - -int git_smart__download_pack( - git_transport *transport, - git_repository *repo, - git_transfer_progress *stats, - git_transfer_progress_callback progress_cb, - void *progress_payload) -{ - transport_smart *t = (transport_smart *)transport; - gitno_buffer *buf = &t->buffer; - git_odb *odb; - struct git_odb_writepack *writepack = NULL; - int error = -1; - struct network_packetsize_payload npp = {0}; - - memset(stats, 0, sizeof(git_transfer_progress)); - - if (progress_cb) { - npp.callback = progress_cb; - npp.payload = progress_payload; - npp.stats = stats; - t->packetsize_cb = &network_packetsize; - t->packetsize_payload = &npp; - - /* We might have something in the buffer already from negotiate_fetch */ - if (t->buffer.offset > 0) - t->packetsize_cb((int)t->buffer.offset, t->packetsize_payload); - } - - if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 || - ((error = git_odb_write_pack(&writepack, odb, progress_cb, progress_payload)) < 0)) - goto on_error; - - /* - * If the remote doesn't support the side-band, we can feed - * the data directly to the pack writer. Otherwise, we need to - * check which one belongs there. - */ - if (!t->caps.side_band && !t->caps.side_band_64k) { - if (no_sideband(t, writepack, buf, stats) < 0) - goto on_error; - - goto on_success; - } - - do { - git_pkt *pkt; - - if (t->cancelled.val) { - giterr_set(GITERR_NET, "The fetch was cancelled by the user"); - error = GIT_EUSER; - goto on_error; - } - - if (recv_pkt(&pkt, buf) < 0) - goto on_error; - - if (pkt->type == GIT_PKT_PROGRESS) { - if (t->progress_cb) { - git_pkt_progress *p = (git_pkt_progress *) pkt; - t->progress_cb(p->data, p->len, t->message_cb_payload); - } - git__free(pkt); - } else if (pkt->type == GIT_PKT_DATA) { - git_pkt_data *p = (git_pkt_data *) pkt; - if (writepack->add(writepack, p->data, p->len, stats) < 0) - goto on_error; - - git__free(pkt); - } else if (pkt->type == GIT_PKT_FLUSH) { - /* A flush indicates the end of the packfile */ - git__free(pkt); - break; - } - } while (1); - - if (writepack->commit(writepack, stats) < 0) - goto on_error; - -on_success: - error = 0; - -on_error: - if (writepack) - writepack->free(writepack); - - /* Trailing execution of progress_cb, if necessary */ - if (npp.callback && npp.stats->received_bytes > npp.last_fired_bytes) - npp.callback(npp.stats, npp.payload); - - return error; -} - -static int gen_pktline(git_buf *buf, git_push *push) -{ - push_spec *spec; - size_t i, len; - char old_id[41], new_id[41]; - - old_id[40] = '\0'; new_id[40] = '\0'; - - git_vector_foreach(&push->specs, i, spec) { - len = 2*GIT_OID_HEXSZ + 7 + strlen(spec->rref); - - if (i == 0) { - ++len; /* '\0' */ - if (push->report_status) - len += strlen(GIT_CAP_REPORT_STATUS); - } - - git_oid_fmt(old_id, &spec->roid); - git_oid_fmt(new_id, &spec->loid); - - git_buf_printf(buf, "%04"PRIxZ"%s %s %s", len, old_id, new_id, spec->rref); - - if (i == 0) { - git_buf_putc(buf, '\0'); - if (push->report_status) - git_buf_printf(buf, GIT_CAP_REPORT_STATUS); - } - - git_buf_putc(buf, '\n'); - } - - git_buf_puts(buf, "0000"); - return git_buf_oom(buf) ? -1 : 0; -} - -static int parse_report(gitno_buffer *buf, git_push *push) -{ - git_pkt *pkt; - const char *line_end; - int error, recvd; - - for (;;) { - if (buf->offset > 0) - error = git_pkt_parse_line(&pkt, buf->data, - &line_end, buf->offset); - else - error = GIT_EBUFS; - - if (error < 0 && error != GIT_EBUFS) - return -1; - - if (error == GIT_EBUFS) { - if ((recvd = gitno_recv(buf)) < 0) - return -1; - - if (recvd == 0) { - giterr_set(GITERR_NET, "Early EOF"); - return -1; - } - continue; - } - - gitno_consume(buf, line_end); - - if (pkt->type == GIT_PKT_OK) { - push_status *status = git__malloc(sizeof(push_status)); - GITERR_CHECK_ALLOC(status); - status->ref = git__strdup(((git_pkt_ok *)pkt)->ref); - status->msg = NULL; - git_pkt_free(pkt); - if (git_vector_insert(&push->status, status) < 0) { - git__free(status); - return -1; - } - continue; - } - - if (pkt->type == GIT_PKT_NG) { - push_status *status = git__malloc(sizeof(push_status)); - GITERR_CHECK_ALLOC(status); - status->ref = git__strdup(((git_pkt_ng *)pkt)->ref); - status->msg = git__strdup(((git_pkt_ng *)pkt)->msg); - git_pkt_free(pkt); - if (git_vector_insert(&push->status, status) < 0) { - git__free(status); - return -1; - } - continue; - } - - if (pkt->type == GIT_PKT_UNPACK) { - push->unpack_ok = ((git_pkt_unpack *)pkt)->unpack_ok; - git_pkt_free(pkt); - continue; - } - - if (pkt->type == GIT_PKT_FLUSH) { - git_pkt_free(pkt); - return 0; - } - - git_pkt_free(pkt); - giterr_set(GITERR_NET, "report-status: protocol error"); - return -1; - } -} - -static int stream_thunk(void *buf, size_t size, void *data) -{ - git_smart_subtransport_stream *s = (git_smart_subtransport_stream *)data; - - return s->write(s, (const char *)buf, size); -} - -int git_smart__push(git_transport *transport, git_push *push) -{ - transport_smart *t = (transport_smart *)transport; - git_smart_subtransport_stream *s; - git_buf pktline = GIT_BUF_INIT; - char *url = NULL; - int error = -1; - -#ifdef PUSH_DEBUG -{ - git_remote_head *head; - push_spec *spec; - unsigned int i; - char hex[41]; hex[40] = '\0'; - - git_vector_foreach(&push->remote->refs, i, head) { - git_oid_fmt(hex, &head->oid); - fprintf(stderr, "%s (%s)\n", hex, head->name); - } - - git_vector_foreach(&push->specs, i, spec) { - git_oid_fmt(hex, &spec->roid); - fprintf(stderr, "%s (%s) -> ", hex, spec->lref); - git_oid_fmt(hex, &spec->loid); - fprintf(stderr, "%s (%s)\n", hex, spec->rref ? - spec->rref : spec->lref); - } -} -#endif - - if (git_smart__get_push_stream(t, &s) < 0 || - gen_pktline(&pktline, push) < 0 || - s->write(s, git_buf_cstr(&pktline), git_buf_len(&pktline)) < 0 || - git_packbuilder_foreach(push->pb, &stream_thunk, s) < 0) - goto on_error; - - /* If we sent nothing or the server doesn't support report-status, then - * we consider the pack to have been unpacked successfully */ - if (!push->specs.length || !push->report_status) - push->unpack_ok = 1; - else if (parse_report(&t->buffer, push) < 0) - goto on_error; - - /* If we updated at least one ref, then we need to re-acquire the list of - * refs so the caller can call git_remote_update_tips afterward. TODO: Use - * the data from the push report to do this without another network call */ - if (push->specs.length) { - git_cred_acquire_cb cred_cb = t->cred_acquire_cb; - void *cred_payload = t->cred_acquire_payload; - int flags = t->flags; - - url = git__strdup(t->url); - - if (!url || t->parent.close(&t->parent) < 0 || - t->parent.connect(&t->parent, url, cred_cb, cred_payload, GIT_DIRECTION_PUSH, flags)) - goto on_error; - } - - error = 0; - -on_error: - git__free(url); - git_buf_free(&pktline); - - return error; -} diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c deleted file mode 100644 index 808f6afaaca..00000000000 --- a/src/transports/winhttp.c +++ /dev/null @@ -1,1001 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#ifdef GIT_WINHTTP - -#include "git2.h" -#include "git2/transport.h" -#include "buffer.h" -#include "posix.h" -#include "netops.h" -#include "smart.h" -#include "remote.h" -#include "repository.h" - -#include -#pragma comment(lib, "winhttp") - -/* For UuidCreate */ -#pragma comment(lib, "rpcrt4") - -#define WIDEN2(s) L ## s -#define WIDEN(s) WIDEN2(s) - -#define MAX_CONTENT_TYPE_LEN 100 -#define WINHTTP_OPTION_PEERDIST_EXTENSION_STATE 109 -#define CACHED_POST_BODY_BUF_SIZE 4096 -#define UUID_LENGTH_CCH 32 - -static const char *prefix_http = "http://"; -static const char *prefix_https = "https://"; -static const char *upload_pack_service = "upload-pack"; -static const char *upload_pack_ls_service_url = "/info/refs?service=git-upload-pack"; -static const char *upload_pack_service_url = "/git-upload-pack"; -static const char *receive_pack_service = "receive-pack"; -static const char *receive_pack_ls_service_url = "/info/refs?service=git-receive-pack"; -static const char *receive_pack_service_url = "/git-receive-pack"; -static const wchar_t *get_verb = L"GET"; -static const wchar_t *post_verb = L"POST"; -static const wchar_t *pragma_nocache = L"Pragma: no-cache"; -static const wchar_t *transfer_encoding = L"Transfer-Encoding: chunked"; -static const int no_check_cert_flags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID | - SECURITY_FLAG_IGNORE_CERT_DATE_INVALID | - SECURITY_FLAG_IGNORE_UNKNOWN_CA; - -#define OWNING_SUBTRANSPORT(s) ((winhttp_subtransport *)(s)->parent.subtransport) - -typedef enum { - GIT_WINHTTP_AUTH_BASIC = 1, -} winhttp_authmechanism_t; - -typedef struct { - git_smart_subtransport_stream parent; - const char *service; - const char *service_url; - const wchar_t *verb; - HINTERNET request; - char *chunk_buffer; - unsigned chunk_buffer_len; - HANDLE post_body; - DWORD post_body_len; - unsigned sent_request : 1, - received_response : 1, - chunked : 1; -} winhttp_stream; - -typedef struct { - git_smart_subtransport parent; - transport_smart *owner; - const char *path; - char *host; - char *port; - git_cred *cred; - int auth_mechanism; - HINTERNET session; - HINTERNET connection; - unsigned use_ssl : 1; -} winhttp_subtransport; - -static int apply_basic_credential(HINTERNET request, git_cred *cred) -{ - git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred; - git_buf buf = GIT_BUF_INIT, raw = GIT_BUF_INIT; - wchar_t *wide = NULL; - int error = -1, wide_len; - - git_buf_printf(&raw, "%s:%s", c->username, c->password); - - if (git_buf_oom(&raw) || - git_buf_puts(&buf, "Authorization: Basic ") < 0 || - git_buf_put_base64(&buf, git_buf_cstr(&raw), raw.size) < 0) - goto on_error; - - wide_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, - git_buf_cstr(&buf), -1, NULL, 0); - - if (!wide_len) { - giterr_set(GITERR_OS, "Failed to measure string for wide conversion"); - goto on_error; - } - - wide = git__malloc(wide_len * sizeof(wchar_t)); - - if (!wide) - goto on_error; - - if (!MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, - git_buf_cstr(&buf), -1, wide, wide_len)) { - giterr_set(GITERR_OS, "Failed to convert string to wide form"); - goto on_error; - } - - if (!WinHttpAddRequestHeaders(request, wide, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) { - giterr_set(GITERR_OS, "Failed to add a header to the request"); - goto on_error; - } - - error = 0; - -on_error: - /* We were dealing with plaintext passwords, so clean up after ourselves a bit. */ - if (wide) - memset(wide, 0x0, wide_len * sizeof(wchar_t)); - - if (buf.size) - memset(buf.ptr, 0x0, buf.size); - - if (raw.size) - memset(raw.ptr, 0x0, raw.size); - - git__free(wide); - git_buf_free(&buf); - git_buf_free(&raw); - return error; -} - -static int winhttp_stream_connect(winhttp_stream *s) -{ - winhttp_subtransport *t = OWNING_SUBTRANSPORT(s); - git_buf buf = GIT_BUF_INIT; - char *proxy_url = NULL; - wchar_t url[GIT_WIN_PATH], ct[MAX_CONTENT_TYPE_LEN]; - wchar_t *types[] = { L"*/*", NULL }; - BOOL peerdist = FALSE; - int error = -1; - - /* Prepare URL */ - git_buf_printf(&buf, "%s%s", t->path, s->service_url); - - if (git_buf_oom(&buf)) - return -1; - - git__utf8_to_16(url, GIT_WIN_PATH, git_buf_cstr(&buf)); - - /* Establish request */ - s->request = WinHttpOpenRequest( - t->connection, - s->verb, - url, - NULL, - WINHTTP_NO_REFERER, - types, - t->use_ssl ? WINHTTP_FLAG_SECURE : 0); - - if (!s->request) { - giterr_set(GITERR_OS, "Failed to open request"); - goto on_error; - } - - /* Set proxy if necessary */ - if (git_remote__get_http_proxy(t->owner->owner, t->use_ssl, &proxy_url) < 0) - goto on_error; - - if (proxy_url) { - WINHTTP_PROXY_INFO proxy_info; - size_t wide_len; - - git__utf8_to_16(url, GIT_WIN_PATH, proxy_url); - - wide_len = wcslen(url); - - /* Strip any trailing forward slash on the proxy URL; - * WinHTTP doesn't like it if one is present */ - if (L'/' == url[wide_len - 1]) - url[wide_len - 1] = L'\0'; - - proxy_info.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY; - proxy_info.lpszProxy = url; - proxy_info.lpszProxyBypass = NULL; - - if (!WinHttpSetOption(s->request, - WINHTTP_OPTION_PROXY, - &proxy_info, - sizeof(WINHTTP_PROXY_INFO))) { - giterr_set(GITERR_OS, "Failed to set proxy"); - goto on_error; - } - } - - /* Strip unwanted headers (X-P2P-PeerDist, X-P2P-PeerDistEx) that WinHTTP - * adds itself. This option may not be supported by the underlying - * platform, so we do not error-check it */ - WinHttpSetOption(s->request, - WINHTTP_OPTION_PEERDIST_EXTENSION_STATE, - &peerdist, - sizeof(peerdist)); - - /* Send Pragma: no-cache header */ - if (!WinHttpAddRequestHeaders(s->request, pragma_nocache, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) { - giterr_set(GITERR_OS, "Failed to add a header to the request"); - goto on_error; - } - - /* Send Content-Type header -- only necessary on a POST */ - if (post_verb == s->verb) { - git_buf_clear(&buf); - if (git_buf_printf(&buf, "Content-Type: application/x-git-%s-request", s->service) < 0) - goto on_error; - - git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)); - - if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) { - giterr_set(GITERR_OS, "Failed to add a header to the request"); - goto on_error; - } - } - - /* If requested, disable certificate validation */ - if (t->use_ssl) { - int flags; - - if (t->owner->parent.read_flags(&t->owner->parent, &flags) < 0) - goto on_error; - - if ((GIT_TRANSPORTFLAGS_NO_CHECK_CERT & flags) && - !WinHttpSetOption(s->request, WINHTTP_OPTION_SECURITY_FLAGS, - (LPVOID)&no_check_cert_flags, sizeof(no_check_cert_flags))) { - giterr_set(GITERR_OS, "Failed to set options to ignore cert errors"); - goto on_error; - } - } - - /* If we have a credential on the subtransport, apply it to the request */ - if (t->cred && - t->cred->credtype == GIT_CREDTYPE_USERPASS_PLAINTEXT && - t->auth_mechanism == GIT_WINHTTP_AUTH_BASIC && - apply_basic_credential(s->request, t->cred) < 0) - goto on_error; - - /* We've done everything up to calling WinHttpSendRequest. */ - - error = 0; - -on_error: - git__free(proxy_url); - git_buf_free(&buf); - return error; -} - -static int parse_unauthorized_response( - HINTERNET request, - int *allowed_types, - int *auth_mechanism) -{ - DWORD supported, first, target; - - *allowed_types = 0; - *auth_mechanism = 0; - - /* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes(). - * We can assume this was already done, since we know we are unauthorized. - */ - if (!WinHttpQueryAuthSchemes(request, &supported, &first, &target)) { - giterr_set(GITERR_OS, "Failed to parse supported auth schemes"); - return -1; - } - - if (WINHTTP_AUTH_SCHEME_BASIC & supported) { - *allowed_types |= GIT_CREDTYPE_USERPASS_PLAINTEXT; - *auth_mechanism = GIT_WINHTTP_AUTH_BASIC; - } - - return 0; -} - -static int write_chunk(HINTERNET request, const char *buffer, size_t len) -{ - DWORD bytes_written; - git_buf buf = GIT_BUF_INIT; - - /* Chunk header */ - git_buf_printf(&buf, "%X\r\n", len); - - if (git_buf_oom(&buf)) - return -1; - - if (!WinHttpWriteData(request, - git_buf_cstr(&buf), (DWORD)git_buf_len(&buf), - &bytes_written)) { - git_buf_free(&buf); - giterr_set(GITERR_OS, "Failed to write chunk header"); - return -1; - } - - git_buf_free(&buf); - - /* Chunk body */ - if (!WinHttpWriteData(request, - buffer, (DWORD)len, - &bytes_written)) { - giterr_set(GITERR_OS, "Failed to write chunk"); - return -1; - } - - /* Chunk footer */ - if (!WinHttpWriteData(request, - "\r\n", 2, - &bytes_written)) { - giterr_set(GITERR_OS, "Failed to write chunk footer"); - return -1; - } - - return 0; -} - -static int winhttp_stream_read( - git_smart_subtransport_stream *stream, - char *buffer, - size_t buf_size, - size_t *bytes_read) -{ - winhttp_stream *s = (winhttp_stream *)stream; - winhttp_subtransport *t = OWNING_SUBTRANSPORT(s); - DWORD dw_bytes_read; - -replay: - /* Connect if necessary */ - if (!s->request && winhttp_stream_connect(s) < 0) - return -1; - - if (!s->received_response) { - DWORD status_code, status_code_length, content_type_length, bytes_written; - char expected_content_type_8[MAX_CONTENT_TYPE_LEN]; - wchar_t expected_content_type[MAX_CONTENT_TYPE_LEN], content_type[MAX_CONTENT_TYPE_LEN]; - - if (!s->sent_request) { - if (!WinHttpSendRequest(s->request, - WINHTTP_NO_ADDITIONAL_HEADERS, 0, - WINHTTP_NO_REQUEST_DATA, 0, - s->post_body_len, 0)) { - giterr_set(GITERR_OS, "Failed to send request"); - return -1; - } - - s->sent_request = 1; - } - - if (s->chunked) { - assert(s->verb == post_verb); - - /* Flush, if necessary */ - if (s->chunk_buffer_len > 0 && - write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0) - return -1; - - s->chunk_buffer_len = 0; - - /* Write the final chunk. */ - if (!WinHttpWriteData(s->request, - "0\r\n\r\n", 5, - &bytes_written)) { - giterr_set(GITERR_OS, "Failed to write final chunk"); - return -1; - } - } - else if (s->post_body) { - char *buffer; - DWORD len = s->post_body_len, bytes_read; - - if (INVALID_SET_FILE_POINTER == SetFilePointer(s->post_body, - 0, 0, FILE_BEGIN) && - NO_ERROR != GetLastError()) { - giterr_set(GITERR_OS, "Failed to reset file pointer"); - return -1; - } - - buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE); - - while (len > 0) { - DWORD bytes_written; - - if (!ReadFile(s->post_body, buffer, - min(CACHED_POST_BODY_BUF_SIZE, len), - &bytes_read, NULL) || - !bytes_read) { - git__free(buffer); - giterr_set(GITERR_OS, "Failed to read from temp file"); - return -1; - } - - if (!WinHttpWriteData(s->request, buffer, - bytes_read, &bytes_written)) { - git__free(buffer); - giterr_set(GITERR_OS, "Failed to write data"); - return -1; - } - - len -= bytes_read; - assert(bytes_read == bytes_written); - } - - git__free(buffer); - - /* Eagerly close the temp file */ - CloseHandle(s->post_body); - s->post_body = NULL; - } - - if (!WinHttpReceiveResponse(s->request, 0)) { - giterr_set(GITERR_OS, "Failed to receive response"); - return -1; - } - - /* Verify that we got a 200 back */ - status_code_length = sizeof(status_code); - - if (!WinHttpQueryHeaders(s->request, - WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, - WINHTTP_HEADER_NAME_BY_INDEX, - &status_code, &status_code_length, - WINHTTP_NO_HEADER_INDEX)) { - giterr_set(GITERR_OS, "Failed to retreive status code"); - return -1; - } - - /* Handle authentication failures */ - if (HTTP_STATUS_DENIED == status_code && - get_verb == s->verb && t->owner->cred_acquire_cb) { - int allowed_types; - - if (parse_unauthorized_response(s->request, &allowed_types, &t->auth_mechanism) < 0) - return -1; - - if (allowed_types && - (!t->cred || 0 == (t->cred->credtype & allowed_types))) { - - if (t->owner->cred_acquire_cb(&t->cred, t->owner->url, allowed_types, t->owner->cred_acquire_payload) < 0) - return -1; - - assert(t->cred); - - WinHttpCloseHandle(s->request); - s->request = NULL; - s->sent_request = 0; - - /* Successfully acquired a credential */ - goto replay; - } - } - - if (HTTP_STATUS_OK != status_code) { - giterr_set(GITERR_NET, "Request failed with status code: %d", status_code); - return -1; - } - - /* Verify that we got the correct content-type back */ - if (post_verb == s->verb) - snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-result", s->service); - else - snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-advertisement", s->service); - - git__utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8); - content_type_length = sizeof(content_type); - - if (!WinHttpQueryHeaders(s->request, - WINHTTP_QUERY_CONTENT_TYPE, - WINHTTP_HEADER_NAME_BY_INDEX, - &content_type, &content_type_length, - WINHTTP_NO_HEADER_INDEX)) { - giterr_set(GITERR_OS, "Failed to retrieve response content-type"); - return -1; - } - - if (wcscmp(expected_content_type, content_type)) { - giterr_set(GITERR_NET, "Received unexpected content-type"); - return -1; - } - - s->received_response = 1; - } - - if (!WinHttpReadData(s->request, - (LPVOID)buffer, - (DWORD)buf_size, - &dw_bytes_read)) - { - giterr_set(GITERR_OS, "Failed to read data"); - return -1; - } - - *bytes_read = dw_bytes_read; - - return 0; -} - -static int winhttp_stream_write_single( - git_smart_subtransport_stream *stream, - const char *buffer, - size_t len) -{ - winhttp_stream *s = (winhttp_stream *)stream; - winhttp_subtransport *t = OWNING_SUBTRANSPORT(s); - DWORD bytes_written; - - if (!s->request && winhttp_stream_connect(s) < 0) - return -1; - - /* This implementation of write permits only a single call. */ - if (s->sent_request) { - giterr_set(GITERR_NET, "Subtransport configured for only one write"); - return -1; - } - - if (!WinHttpSendRequest(s->request, - WINHTTP_NO_ADDITIONAL_HEADERS, 0, - WINHTTP_NO_REQUEST_DATA, 0, - (DWORD)len, 0)) { - giterr_set(GITERR_OS, "Failed to send request"); - return -1; - } - - s->sent_request = 1; - - if (!WinHttpWriteData(s->request, - (LPCVOID)buffer, - (DWORD)len, - &bytes_written)) { - giterr_set(GITERR_OS, "Failed to write data"); - return -1; - } - - assert((DWORD)len == bytes_written); - - return 0; -} - -static int put_uuid_string(LPWSTR buffer, DWORD buffer_len_cch) -{ - UUID uuid; - RPC_STATUS status = UuidCreate(&uuid); - int result; - - if (RPC_S_OK != status && - RPC_S_UUID_LOCAL_ONLY != status && - RPC_S_UUID_NO_ADDRESS != status) { - giterr_set(GITERR_NET, "Unable to generate name for temp file"); - return -1; - } - - if (buffer_len_cch < (UUID_LENGTH_CCH + 1)) { - giterr_set(GITERR_NET, "Buffer insufficient to generate temp file name"); - return -1; - } - - result = wsprintfW(buffer, L"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x", - uuid.Data1, uuid.Data2, uuid.Data3, - uuid.Data4[0], uuid.Data4[1], uuid.Data4[2], uuid.Data4[3], - uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]); - - if (result != UUID_LENGTH_CCH) { - giterr_set(GITERR_OS, "Unable to generate name for temp file"); - return -1; - } - - return 0; -} - -static int get_temp_file(LPWSTR buffer, DWORD buffer_len_cch) -{ - size_t len; - - if (!GetTempPathW(buffer_len_cch, buffer)) { - giterr_set(GITERR_OS, "Failed to get temp path"); - return -1; - } - - len = wcslen(buffer); - - /* 1 prefix character for the backslash, 1 postfix for - * the null terminator */ - if (buffer_len_cch - len < 1 + UUID_LENGTH_CCH + 1) { - giterr_set(GITERR_NET, "Buffer insufficient to generate temp file name"); - return -1; - } - - if (buffer[len - 1] != '\\') - buffer[len++] = '\\'; - - if (put_uuid_string(&buffer[len], UUID_LENGTH_CCH + 1) < 0) - return -1; - - return 0; -} - -static int winhttp_stream_write_buffered( - git_smart_subtransport_stream *stream, - const char *buffer, - size_t len) -{ - winhttp_stream *s = (winhttp_stream *)stream; - winhttp_subtransport *t = OWNING_SUBTRANSPORT(s); - DWORD bytes_written; - - if (!s->request && winhttp_stream_connect(s) < 0) - return -1; - - /* Buffer the payload, using a temporary file so we delegate - * memory management of the data to the operating system. */ - if (!s->post_body) { - wchar_t temp_path[MAX_PATH + 1]; - - if (get_temp_file(temp_path, MAX_PATH + 1) < 0) - return -1; - - s->post_body = CreateFileW(temp_path, - GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_DELETE, NULL, - CREATE_NEW, - FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_SEQUENTIAL_SCAN, - NULL); - - if (INVALID_HANDLE_VALUE == s->post_body) { - s->post_body = NULL; - giterr_set(GITERR_OS, "Failed to create temporary file"); - return -1; - } - } - - if (!WriteFile(s->post_body, buffer, (DWORD)len, &bytes_written, NULL)) { - giterr_set(GITERR_OS, "Failed to write to temporary file"); - return -1; - } - - assert((DWORD)len == bytes_written); - - s->post_body_len += bytes_written; - - return 0; -} - -static int winhttp_stream_write_chunked( - git_smart_subtransport_stream *stream, - const char *buffer, - size_t len) -{ - winhttp_stream *s = (winhttp_stream *)stream; - winhttp_subtransport *t = OWNING_SUBTRANSPORT(s); - - if (!s->request && winhttp_stream_connect(s) < 0) - return -1; - - if (!s->sent_request) { - /* Send Transfer-Encoding: chunked header */ - if (!WinHttpAddRequestHeaders(s->request, - transfer_encoding, (ULONG) -1L, - WINHTTP_ADDREQ_FLAG_ADD)) { - giterr_set(GITERR_OS, "Failed to add a header to the request"); - return -1; - } - - if (!WinHttpSendRequest(s->request, - WINHTTP_NO_ADDITIONAL_HEADERS, 0, - WINHTTP_NO_REQUEST_DATA, 0, - WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH, 0)) { - giterr_set(GITERR_OS, "Failed to send request"); - return -1; - } - - s->sent_request = 1; - } - - if (len > CACHED_POST_BODY_BUF_SIZE) { - /* Flush, if necessary */ - if (s->chunk_buffer_len > 0) { - if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0) - return -1; - - s->chunk_buffer_len = 0; - } - - /* Write chunk directly */ - if (write_chunk(s->request, buffer, len) < 0) - return -1; - } - else { - /* Append as much to the buffer as we can */ - int count = min(CACHED_POST_BODY_BUF_SIZE - s->chunk_buffer_len, (int)len); - - if (!s->chunk_buffer) - s->chunk_buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE); - - memcpy(s->chunk_buffer + s->chunk_buffer_len, buffer, count); - s->chunk_buffer_len += count; - buffer += count; - len -= count; - - /* Is the buffer full? If so, then flush */ - if (CACHED_POST_BODY_BUF_SIZE == s->chunk_buffer_len) { - if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0) - return -1; - - s->chunk_buffer_len = 0; - - /* Is there any remaining data from the source? */ - if (len > 0) { - memcpy(s->chunk_buffer, buffer, len); - s->chunk_buffer_len = (unsigned int)len; - } - } - } - - return 0; -} - -static void winhttp_stream_free(git_smart_subtransport_stream *stream) -{ - winhttp_stream *s = (winhttp_stream *)stream; - - if (s->chunk_buffer) { - git__free(s->chunk_buffer); - s->chunk_buffer = NULL; - } - - if (s->post_body) { - CloseHandle(s->post_body); - s->post_body = NULL; - } - - if (s->request) { - WinHttpCloseHandle(s->request); - s->request = NULL; - } - - git__free(s); -} - -static int winhttp_stream_alloc(winhttp_subtransport *t, winhttp_stream **stream) -{ - winhttp_stream *s; - - if (!stream) - return -1; - - s = git__calloc(sizeof(winhttp_stream), 1); - GITERR_CHECK_ALLOC(s); - - s->parent.subtransport = &t->parent; - s->parent.read = winhttp_stream_read; - s->parent.write = winhttp_stream_write_single; - s->parent.free = winhttp_stream_free; - - *stream = s; - - return 0; -} - -static int winhttp_connect( - winhttp_subtransport *t, - const char *url) -{ - wchar_t *ua = L"git/1.0 (libgit2 " WIDEN(LIBGIT2_VERSION) L")"; - wchar_t host[GIT_WIN_PATH]; - int32_t port; - const char *default_port; - int ret; - - if (!git__prefixcmp(url, prefix_http)) { - url = url + strlen(prefix_http); - default_port = "80"; - } - - if (!git__prefixcmp(url, prefix_https)) { - url += strlen(prefix_https); - default_port = "443"; - t->use_ssl = 1; - } - - if ((ret = gitno_extract_host_and_port(&t->host, &t->port, url, default_port)) < 0) - return ret; - - t->path = strchr(url, '/'); - - /* Prepare port */ - if (git__strtol32(&port, t->port, NULL, 10) < 0) - return -1; - - /* Prepare host */ - git__utf8_to_16(host, GIT_WIN_PATH, t->host); - - /* Establish session */ - t->session = WinHttpOpen( - ua, - WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, - WINHTTP_NO_PROXY_NAME, - WINHTTP_NO_PROXY_BYPASS, - 0); - - if (!t->session) { - giterr_set(GITERR_OS, "Failed to init WinHTTP"); - return -1; - } - - /* Establish connection */ - t->connection = WinHttpConnect( - t->session, - host, - port, - 0); - - if (!t->connection) { - giterr_set(GITERR_OS, "Failed to connect to host"); - return -1; - } - - return 0; -} - -static int winhttp_uploadpack_ls( - winhttp_subtransport *t, - winhttp_stream *s) -{ - s->service = upload_pack_service; - s->service_url = upload_pack_ls_service_url; - s->verb = get_verb; - - return 0; -} - -static int winhttp_uploadpack( - winhttp_subtransport *t, - winhttp_stream *s) -{ - s->service = upload_pack_service; - s->service_url = upload_pack_service_url; - s->verb = post_verb; - - return 0; -} - -static int winhttp_receivepack_ls( - winhttp_subtransport *t, - winhttp_stream *s) -{ - s->service = receive_pack_service; - s->service_url = receive_pack_ls_service_url; - s->verb = get_verb; - - return 0; -} - -static int winhttp_receivepack( - winhttp_subtransport *t, - winhttp_stream *s) -{ - /* WinHTTP only supports Transfer-Encoding: chunked - * on Windows Vista (NT 6.0) and higher. */ - s->chunked = LOBYTE(LOWORD(GetVersion())) >= 6; - - if (s->chunked) - s->parent.write = winhttp_stream_write_chunked; - else - s->parent.write = winhttp_stream_write_buffered; - - s->service = receive_pack_service; - s->service_url = receive_pack_service_url; - s->verb = post_verb; - - return 0; -} - -static int winhttp_action( - git_smart_subtransport_stream **stream, - git_smart_subtransport *subtransport, - const char *url, - git_smart_service_t action) -{ - winhttp_subtransport *t = (winhttp_subtransport *)subtransport; - winhttp_stream *s; - int ret = -1; - - if (!t->connection && - winhttp_connect(t, url) < 0) - return -1; - - if (winhttp_stream_alloc(t, &s) < 0) - return -1; - - if (!stream) - return -1; - - switch (action) - { - case GIT_SERVICE_UPLOADPACK_LS: - ret = winhttp_uploadpack_ls(t, s); - break; - - case GIT_SERVICE_UPLOADPACK: - ret = winhttp_uploadpack(t, s); - break; - - case GIT_SERVICE_RECEIVEPACK_LS: - ret = winhttp_receivepack_ls(t, s); - break; - - case GIT_SERVICE_RECEIVEPACK: - ret = winhttp_receivepack(t, s); - break; - - default: - assert(0); - } - - if (!ret) - *stream = &s->parent; - - return ret; -} - -static int winhttp_close(git_smart_subtransport *subtransport) -{ - winhttp_subtransport *t = (winhttp_subtransport *)subtransport; - int ret = 0; - - if (t->host) { - git__free(t->host); - t->host = NULL; - } - - if (t->port) { - git__free(t->port); - t->port = NULL; - } - - if (t->cred) { - t->cred->free(t->cred); - t->cred = NULL; - } - - if (t->connection) { - if (!WinHttpCloseHandle(t->connection)) { - giterr_set(GITERR_OS, "Unable to close connection"); - ret = -1; - } - - t->connection = NULL; - } - - if (t->session) { - if (!WinHttpCloseHandle(t->session)) { - giterr_set(GITERR_OS, "Unable to close session"); - ret = -1; - } - - t->session = NULL; - } - - return ret; -} - -static void winhttp_free(git_smart_subtransport *subtransport) -{ - winhttp_subtransport *t = (winhttp_subtransport *)subtransport; - - winhttp_close(subtransport); - - git__free(t); -} - -int git_smart_subtransport_http(git_smart_subtransport **out, git_transport *owner) -{ - winhttp_subtransport *t; - - if (!out) - return -1; - - t = git__calloc(sizeof(winhttp_subtransport), 1); - GITERR_CHECK_ALLOC(t); - - t->owner = (transport_smart *)owner; - t->parent.action = winhttp_action; - t->parent.close = winhttp_close; - t->parent.free = winhttp_free; - - *out = (git_smart_subtransport *) t; - return 0; -} - -#endif /* GIT_WINHTTP */ diff --git a/src/tree-cache.c b/src/tree-cache.c deleted file mode 100644 index 97ffc2acfba..00000000000 --- a/src/tree-cache.c +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "tree-cache.h" - -static git_tree_cache *find_child(const git_tree_cache *tree, const char *path) -{ - size_t i, dirlen; - const char *end; - - end = strchr(path, '/'); - if (end == NULL) { - end = strrchr(path, '\0'); - } - - dirlen = end - path; - - for (i = 0; i < tree->children_count; ++i) { - const char *childname = tree->children[i]->name; - - if (strlen(childname) == dirlen && !memcmp(path, childname, dirlen)) - return tree->children[i]; - } - - return NULL; -} - -void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path) -{ - const char *ptr = path, *end; - - if (tree == NULL) - return; - - tree->entries = -1; - - while (ptr != NULL) { - end = strchr(ptr, '/'); - - if (end == NULL) /* End of path */ - break; - - tree = find_child(tree, ptr); - if (tree == NULL) /* We don't have that tree */ - return; - - tree->entries = -1; - ptr = end + 1; - } -} - -const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path) -{ - const char *ptr = path, *end; - - if (tree == NULL) { - return NULL; - } - - while (1) { - end = strchr(ptr, '/'); - - tree = find_child(tree, ptr); - if (tree == NULL) { /* Can't find it */ - return NULL; - } - - if (end == NULL || *end + 1 == '\0') - return tree; - - ptr = end + 1; - } -} - -static int read_tree_internal(git_tree_cache **out, - const char **buffer_in, const char *buffer_end, git_tree_cache *parent) -{ - git_tree_cache *tree = NULL; - const char *name_start, *buffer; - int count; - size_t name_len; - - buffer = name_start = *buffer_in; - - if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL) - goto corrupted; - - if (++buffer >= buffer_end) - goto corrupted; - - name_len = strlen(name_start); - tree = git__malloc(sizeof(git_tree_cache) + name_len + 1); - GITERR_CHECK_ALLOC(tree); - - memset(tree, 0x0, sizeof(git_tree_cache)); - tree->parent = parent; - - /* NUL-terminated tree name */ - memcpy(tree->name, name_start, name_len); - tree->name[name_len] = '\0'; - - /* Blank-terminated ASCII decimal number of entries in this tree */ - if (git__strtol32(&count, buffer, &buffer, 10) < 0) - goto corrupted; - - tree->entries = count; - - if (*buffer != ' ' || ++buffer >= buffer_end) - goto corrupted; - - /* Number of children of the tree, newline-terminated */ - if (git__strtol32(&count, buffer, &buffer, 10) < 0 || count < 0) - goto corrupted; - - tree->children_count = count; - - if (*buffer != '\n' || ++buffer > buffer_end) - goto corrupted; - - /* The SHA1 is only there if it's not invalidated */ - if (tree->entries >= 0) { - /* 160-bit SHA-1 for this tree and it's children */ - if (buffer + GIT_OID_RAWSZ > buffer_end) - goto corrupted; - - git_oid_fromraw(&tree->oid, (const unsigned char *)buffer); - buffer += GIT_OID_RAWSZ; - } - - /* Parse children: */ - if (tree->children_count > 0) { - unsigned int i; - - tree->children = git__malloc(tree->children_count * sizeof(git_tree_cache *)); - GITERR_CHECK_ALLOC(tree->children); - - for (i = 0; i < tree->children_count; ++i) { - if (read_tree_internal(&tree->children[i], &buffer, buffer_end, tree) < 0) - return -1; - } - } - - *buffer_in = buffer; - *out = tree; - return 0; - - corrupted: - git_tree_cache_free(tree); - giterr_set(GITERR_INDEX, "Corruped TREE extension in index"); - return -1; -} - -int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size) -{ - const char *buffer_end = buffer + buffer_size; - - if (read_tree_internal(tree, &buffer, buffer_end, NULL) < 0) - return -1; - - if (buffer < buffer_end) { - giterr_set(GITERR_INDEX, "Corruped TREE extension in index (unexpected trailing data)"); - return -1; - } - - return 0; -} - -void git_tree_cache_free(git_tree_cache *tree) -{ - unsigned int i; - - if (tree == NULL) - return; - - for (i = 0; i < tree->children_count; ++i) - git_tree_cache_free(tree->children[i]); - - git__free(tree->children); - git__free(tree); -} diff --git a/src/tree-cache.h b/src/tree-cache.h deleted file mode 100644 index 805483a78a4..00000000000 --- a/src/tree-cache.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#ifndef INCLUDE_tree_cache_h__ -#define INCLUDE_tree_cache_h__ - -#include "common.h" -#include "git2/oid.h" - -struct git_tree_cache { - struct git_tree_cache *parent; - struct git_tree_cache **children; - size_t children_count; - - ssize_t entries; - git_oid oid; - char name[GIT_FLEX_ARRAY]; -}; - -typedef struct git_tree_cache git_tree_cache; - -int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size); -void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path); -const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path); -void git_tree_cache_free(git_tree_cache *tree); - -#endif diff --git a/src/tree.c b/src/tree.c deleted file mode 100644 index e05105a9da0..00000000000 --- a/src/tree.c +++ /dev/null @@ -1,930 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "commit.h" -#include "tree.h" -#include "git2/repository.h" -#include "git2/object.h" - -#define DEFAULT_TREE_SIZE 16 -#define MAX_FILEMODE_BYTES 6 - -static bool valid_filemode(const int filemode) -{ - return (filemode == GIT_FILEMODE_TREE - || filemode == GIT_FILEMODE_BLOB - || filemode == GIT_FILEMODE_BLOB_EXECUTABLE - || filemode == GIT_FILEMODE_LINK - || filemode == GIT_FILEMODE_COMMIT); -} - -GIT_INLINE(git_filemode_t) normalize_filemode(git_filemode_t filemode) -{ - /* Tree bits set, but it's not a commit */ - if (filemode & GIT_FILEMODE_TREE && !(filemode & 0100000)) - return GIT_FILEMODE_TREE; - - /* If any of the x bits is set */ - if (filemode & 0111) - return GIT_FILEMODE_BLOB_EXECUTABLE; - - /* 16XXXX means commit */ - if ((filemode & GIT_FILEMODE_COMMIT) == GIT_FILEMODE_COMMIT) - return GIT_FILEMODE_COMMIT; - - /* 12XXXX means commit */ - if ((filemode & GIT_FILEMODE_LINK) == GIT_FILEMODE_LINK) - return GIT_FILEMODE_LINK; - - /* Otherwise, return a blob */ - return GIT_FILEMODE_BLOB; -} - -static int valid_entry_name(const char *filename) -{ - return *filename != '\0' && - strchr(filename, '/') == NULL && - (*filename != '.' || - (strcmp(filename, ".") != 0 && - strcmp(filename, "..") != 0 && - strcmp(filename, DOT_GIT) != 0)); -} - -int git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2) -{ - return git_path_cmp( - e1->filename, e1->filename_len, git_tree_entry__is_tree(e1), - e2->filename, e2->filename_len, git_tree_entry__is_tree(e2)); -} - -int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2) -{ - return git_path_icmp( - e1->filename, e1->filename_len, git_tree_entry__is_tree(e1), - e2->filename, e2->filename_len, git_tree_entry__is_tree(e2)); -} - -static int entry_sort_cmp(const void *a, const void *b) -{ - return git_tree_entry_cmp((const git_tree_entry *)a, (const git_tree_entry *)b); -} - -static git_tree_entry *alloc_entry(const char *filename) -{ - git_tree_entry *entry = NULL; - size_t filename_len = strlen(filename); - - entry = git__malloc(sizeof(git_tree_entry) + filename_len + 1); - if (!entry) - return NULL; - - memset(entry, 0x0, sizeof(git_tree_entry)); - memcpy(entry->filename, filename, filename_len); - entry->filename[filename_len] = 0; - entry->filename_len = filename_len; - - return entry; -} - -struct tree_key_search { - const char *filename; - size_t filename_len; -}; - -static int homing_search_cmp(const void *key, const void *array_member) -{ - const struct tree_key_search *ksearch = key; - const git_tree_entry *entry = array_member; - - const size_t len1 = ksearch->filename_len; - const size_t len2 = entry->filename_len; - - return memcmp( - ksearch->filename, - entry->filename, - len1 < len2 ? len1 : len2 - ); -} - -/* - * Search for an entry in a given tree. - * - * Note that this search is performed in two steps because - * of the way tree entries are sorted internally in git: - * - * Entries in a tree are not sorted alphabetically; two entries - * with the same root prefix will have different positions - * depending on whether they are folders (subtrees) or normal files. - * - * Consequently, it is not possible to find an entry on the tree - * with a binary search if you don't know whether the filename - * you're looking for is a folder or a normal file. - * - * To work around this, we first perform a homing binary search - * on the tree, using the minimal length root prefix of our filename. - * Once the comparisons for this homing search start becoming - * ambiguous because of folder vs file sorting, we look linearly - * around the area for our target file. - */ -static int tree_key_search( - size_t *at_pos, git_vector *entries, const char *filename, size_t filename_len) -{ - struct tree_key_search ksearch; - const git_tree_entry *entry; - size_t homing, i; - - ksearch.filename = filename; - ksearch.filename_len = filename_len; - - /* Initial homing search; find an entry on the tree with - * the same prefix as the filename we're looking for */ - if (git_vector_bsearch2(&homing, entries, &homing_search_cmp, &ksearch) < 0) - return GIT_ENOTFOUND; - - /* We found a common prefix. Look forward as long as - * there are entries that share the common prefix */ - for (i = homing; i < entries->length; ++i) { - entry = entries->contents[i]; - - if (homing_search_cmp(&ksearch, entry) < 0) - break; - - if (entry->filename_len == filename_len && - memcmp(filename, entry->filename, filename_len) == 0) { - if (at_pos) - *at_pos = i; - - return 0; - } - } - - /* If we haven't found our filename yet, look backwards - * too as long as we have entries with the same prefix */ - if (homing > 0) { - i = homing - 1; - - do { - entry = entries->contents[i]; - - if (homing_search_cmp(&ksearch, entry) > 0) - break; - - if (entry->filename_len == filename_len && - memcmp(filename, entry->filename, filename_len) == 0) { - if (at_pos) - *at_pos = i; - - return 0; - } - } while (i-- > 0); - } - - /* The filename doesn't exist at all */ - return GIT_ENOTFOUND; -} - -void git_tree_entry_free(git_tree_entry *entry) -{ - if (entry == NULL) - return; - - git__free(entry); -} - -git_tree_entry *git_tree_entry_dup(const git_tree_entry *entry) -{ - size_t total_size; - git_tree_entry *copy; - - assert(entry); - - total_size = sizeof(git_tree_entry) + entry->filename_len + 1; - - copy = git__malloc(total_size); - if (!copy) - return NULL; - - memcpy(copy, entry, total_size); - - return copy; -} - -void git_tree__free(git_tree *tree) -{ - unsigned int i; - - for (i = 0; i < tree->entries.length; ++i) { - git_tree_entry *e = git_vector_get(&tree->entries, i); - git_tree_entry_free(e); - } - - git_vector_free(&tree->entries); - git__free(tree); -} - -const git_oid *git_tree_id(const git_tree *t) -{ - return git_object_id((const git_object *)t); -} - -git_repository *git_tree_owner(const git_tree *t) -{ - return git_object_owner((const git_object *)t); -} - -git_filemode_t git_tree_entry_filemode(const git_tree_entry *entry) -{ - return (git_filemode_t)entry->attr; -} - -const char *git_tree_entry_name(const git_tree_entry *entry) -{ - assert(entry); - return entry->filename; -} - -const git_oid *git_tree_entry_id(const git_tree_entry *entry) -{ - assert(entry); - return &entry->oid; -} - -git_otype git_tree_entry_type(const git_tree_entry *entry) -{ - assert(entry); - - if (S_ISGITLINK(entry->attr)) - return GIT_OBJ_COMMIT; - else if (S_ISDIR(entry->attr)) - return GIT_OBJ_TREE; - else - return GIT_OBJ_BLOB; -} - -int git_tree_entry_to_object( - git_object **object_out, - git_repository *repo, - const git_tree_entry *entry) -{ - assert(entry && object_out); - return git_object_lookup(object_out, repo, &entry->oid, GIT_OBJ_ANY); -} - -static const git_tree_entry *entry_fromname( - git_tree *tree, const char *name, size_t name_len) -{ - size_t idx; - - if (tree_key_search(&idx, &tree->entries, name, name_len) < 0) - return NULL; - - return git_vector_get(&tree->entries, idx); -} - -const git_tree_entry *git_tree_entry_byname( - git_tree *tree, const char *filename) -{ - assert(tree && filename); - return entry_fromname(tree, filename, strlen(filename)); -} - -const git_tree_entry *git_tree_entry_byindex( - git_tree *tree, size_t idx) -{ - assert(tree); - return git_vector_get(&tree->entries, idx); -} - -const git_tree_entry *git_tree_entry_byoid( - const git_tree *tree, const git_oid *oid) -{ - size_t i; - const git_tree_entry *e; - - assert(tree); - - git_vector_foreach(&tree->entries, i, e) { - if (memcmp(&e->oid.id, &oid->id, sizeof(oid->id)) == 0) - return e; - } - - return NULL; -} - -int git_tree__prefix_position(git_tree *tree, const char *path) -{ - git_vector *entries = &tree->entries; - struct tree_key_search ksearch; - size_t at_pos; - - if (!path) - return 0; - - ksearch.filename = path; - ksearch.filename_len = strlen(path); - - /* Find tree entry with appropriate prefix */ - git_vector_bsearch2(&at_pos, entries, &homing_search_cmp, &ksearch); - - for (; at_pos < entries->length; ++at_pos) { - const git_tree_entry *entry = entries->contents[at_pos]; - if (homing_search_cmp(&ksearch, entry) < 0) - break; - } - - for (; at_pos > 0; --at_pos) { - const git_tree_entry *entry = entries->contents[at_pos - 1]; - if (homing_search_cmp(&ksearch, entry) > 0) - break; - } - - return (int)at_pos; -} - -size_t git_tree_entrycount(const git_tree *tree) -{ - assert(tree); - return tree->entries.length; -} - -unsigned int git_treebuilder_entrycount(git_treebuilder *bld) -{ - assert(bld); - return bld->entries.length; -} - -static int tree_error(const char *str, const char *path) -{ - if (path) - giterr_set(GITERR_TREE, "%s - %s", str, path); - else - giterr_set(GITERR_TREE, "%s", str); - return -1; -} - -static int tree_parse_buffer(git_tree *tree, const char *buffer, const char *buffer_end) -{ - if (git_vector_init(&tree->entries, DEFAULT_TREE_SIZE, entry_sort_cmp) < 0) - return -1; - - while (buffer < buffer_end) { - git_tree_entry *entry; - int attr; - - if (git__strtol32(&attr, buffer, &buffer, 8) < 0 || !buffer) - return tree_error("Failed to parse tree. Can't parse filemode", NULL); - - attr = normalize_filemode(attr); /* make sure to normalize the filemode */ - - if (*buffer++ != ' ') - return tree_error("Failed to parse tree. Object is corrupted", NULL); - - if (memchr(buffer, 0, buffer_end - buffer) == NULL) - return tree_error("Failed to parse tree. Object is corrupted", NULL); - - /** Allocate the entry and store it in the entries vector */ - { - entry = alloc_entry(buffer); - GITERR_CHECK_ALLOC(entry); - - if (git_vector_insert(&tree->entries, entry) < 0) - return -1; - - entry->attr = attr; - } - - while (buffer < buffer_end && *buffer != 0) - buffer++; - - buffer++; - - git_oid_fromraw(&entry->oid, (const unsigned char *)buffer); - buffer += GIT_OID_RAWSZ; - } - - return 0; -} - -int git_tree__parse(git_tree *tree, git_odb_object *obj) -{ - assert(tree); - return tree_parse_buffer(tree, (char *)obj->raw.data, (char *)obj->raw.data + obj->raw.len); -} - -static size_t find_next_dir(const char *dirname, git_index *index, size_t start) -{ - size_t dirlen, i, entries = git_index_entrycount(index); - - dirlen = strlen(dirname); - for (i = start; i < entries; ++i) { - const git_index_entry *entry = git_index_get_byindex(index, i); - if (strlen(entry->path) < dirlen || - memcmp(entry->path, dirname, dirlen) || - (dirlen > 0 && entry->path[dirlen] != '/')) { - break; - } - } - - return i; -} - -static int append_entry( - git_treebuilder *bld, - const char *filename, - const git_oid *id, - git_filemode_t filemode) -{ - git_tree_entry *entry; - - if (!valid_entry_name(filename)) - return tree_error("Failed to insert entry. Invalid name for a tree entry", filename); - - entry = alloc_entry(filename); - GITERR_CHECK_ALLOC(entry); - - git_oid_cpy(&entry->oid, id); - entry->attr = (uint16_t)filemode; - - if (git_vector_insert(&bld->entries, entry) < 0) - return -1; - - return 0; -} - -static int write_tree( - git_oid *oid, - git_repository *repo, - git_index *index, - const char *dirname, - size_t start) -{ - git_treebuilder *bld = NULL; - size_t i, entries = git_index_entrycount(index); - int error; - size_t dirname_len = strlen(dirname); - const git_tree_cache *cache; - - cache = git_tree_cache_get(index->tree, dirname); - if (cache != NULL && cache->entries >= 0){ - git_oid_cpy(oid, &cache->oid); - return (int)find_next_dir(dirname, index, start); - } - - if ((error = git_treebuilder_create(&bld, NULL)) < 0 || bld == NULL) - return -1; - - /* - * This loop is unfortunate, but necessary. The index doesn't have - * any directores, so we need to handle that manually, and we - * need to keep track of the current position. - */ - for (i = start; i < entries; ++i) { - const git_index_entry *entry = git_index_get_byindex(index, i); - const char *filename, *next_slash; - - /* - * If we've left our (sub)tree, exit the loop and return. The - * first check is an early out (and security for the - * third). The second check is a simple prefix comparison. The - * third check catches situations where there is a directory - * win32/sys and a file win32mmap.c. Without it, the following - * code believes there is a file win32/mmap.c - */ - if (strlen(entry->path) < dirname_len || - memcmp(entry->path, dirname, dirname_len) || - (dirname_len > 0 && entry->path[dirname_len] != '/')) { - break; - } - - filename = entry->path + dirname_len; - if (*filename == '/') - filename++; - next_slash = strchr(filename, '/'); - if (next_slash) { - git_oid sub_oid; - int written; - char *subdir, *last_comp; - - subdir = git__strndup(entry->path, next_slash - entry->path); - GITERR_CHECK_ALLOC(subdir); - - /* Write out the subtree */ - written = write_tree(&sub_oid, repo, index, subdir, i); - if (written < 0) { - tree_error("Failed to write subtree", subdir); - git__free(subdir); - goto on_error; - } else { - i = written - 1; /* -1 because of the loop increment */ - } - - /* - * We need to figure out what we want toinsert - * into this tree. If we're traversing - * deps/zlib/, then we only want to write - * 'zlib' into the tree. - */ - last_comp = strrchr(subdir, '/'); - if (last_comp) { - last_comp++; /* Get rid of the '/' */ - } else { - last_comp = subdir; - } - - error = append_entry(bld, last_comp, &sub_oid, S_IFDIR); - git__free(subdir); - if (error < 0) - goto on_error; - } else { - error = append_entry(bld, filename, &entry->oid, entry->mode); - if (error < 0) - goto on_error; - } - } - - if (git_treebuilder_write(oid, repo, bld) < 0) - goto on_error; - - git_treebuilder_free(bld); - return (int)i; - -on_error: - git_treebuilder_free(bld); - return -1; -} - -int git_tree__write_index( - git_oid *oid, git_index *index, git_repository *repo) -{ - int ret; - - assert(oid && index && repo); - - if (git_index_has_conflicts(index)) { - giterr_set(GITERR_INDEX, - "Cannot create a tree from a not fully merged index."); - return GIT_EUNMERGED; - } - - if (index->tree != NULL && index->tree->entries >= 0) { - git_oid_cpy(oid, &index->tree->oid); - return 0; - } - - /* The tree cache didn't help us */ - ret = write_tree(oid, repo, index, "", 0); - return ret < 0 ? ret : 0; -} - -static void sort_entries(git_treebuilder *bld) -{ - git_vector_sort(&bld->entries); -} - -int git_treebuilder_create(git_treebuilder **builder_p, const git_tree *source) -{ - git_treebuilder *bld; - size_t i, source_entries = DEFAULT_TREE_SIZE; - - assert(builder_p); - - bld = git__calloc(1, sizeof(git_treebuilder)); - GITERR_CHECK_ALLOC(bld); - - if (source != NULL) - source_entries = source->entries.length; - - if (git_vector_init(&bld->entries, source_entries, entry_sort_cmp) < 0) - goto on_error; - - if (source != NULL) { - for (i = 0; i < source->entries.length; ++i) { - git_tree_entry *entry_src = source->entries.contents[i]; - - if (append_entry( - bld, entry_src->filename, - &entry_src->oid, - entry_src->attr) < 0) - goto on_error; - } - } - - *builder_p = bld; - return 0; - -on_error: - git_treebuilder_free(bld); - return -1; -} - -int git_treebuilder_insert( - const git_tree_entry **entry_out, - git_treebuilder *bld, - const char *filename, - const git_oid *id, - git_filemode_t filemode) -{ - git_tree_entry *entry; - size_t pos; - - assert(bld && id && filename); - - if (!valid_filemode(filemode)) - return tree_error("Failed to insert entry. Invalid filemode for file", filename); - - if (!valid_entry_name(filename)) - return tree_error("Failed to insert entry. Invalid name for a tree entry", filename); - - if (!tree_key_search(&pos, &bld->entries, filename, strlen(filename))) { - entry = git_vector_get(&bld->entries, pos); - if (entry->removed) - entry->removed = 0; - } else { - entry = alloc_entry(filename); - GITERR_CHECK_ALLOC(entry); - - if (git_vector_insert(&bld->entries, entry) < 0) - return -1; - } - - git_oid_cpy(&entry->oid, id); - entry->attr = filemode; - - if (entry_out) - *entry_out = entry; - - return 0; -} - -static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filename) -{ - size_t idx; - git_tree_entry *entry; - - assert(bld && filename); - - if (tree_key_search(&idx, &bld->entries, filename, strlen(filename)) < 0) - return NULL; - - entry = git_vector_get(&bld->entries, idx); - if (entry->removed) - return NULL; - - return entry; -} - -const git_tree_entry *git_treebuilder_get(git_treebuilder *bld, const char *filename) -{ - return treebuilder_get(bld, filename); -} - -int git_treebuilder_remove(git_treebuilder *bld, const char *filename) -{ - git_tree_entry *remove_ptr = treebuilder_get(bld, filename); - - if (remove_ptr == NULL || remove_ptr->removed) - return tree_error("Failed to remove entry. File isn't in the tree", filename); - - remove_ptr->removed = 1; - return 0; -} - -int git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *bld) -{ - unsigned int i; - git_buf tree = GIT_BUF_INIT; - git_odb *odb; - - assert(bld); - - sort_entries(bld); - - /* Grow the buffer beforehand to an estimated size */ - git_buf_grow(&tree, bld->entries.length * 72); - - for (i = 0; i < bld->entries.length; ++i) { - git_tree_entry *entry = bld->entries.contents[i]; - - if (entry->removed) - continue; - - git_buf_printf(&tree, "%o ", entry->attr); - git_buf_put(&tree, entry->filename, entry->filename_len + 1); - git_buf_put(&tree, (char *)entry->oid.id, GIT_OID_RAWSZ); - } - - if (git_buf_oom(&tree)) - goto on_error; - - if (git_repository_odb__weakptr(&odb, repo) < 0) - goto on_error; - - - if (git_odb_write(oid, odb, tree.ptr, tree.size, GIT_OBJ_TREE) < 0) - goto on_error; - - git_buf_free(&tree); - return 0; - -on_error: - git_buf_free(&tree); - return -1; -} - -void git_treebuilder_filter( - git_treebuilder *bld, - git_treebuilder_filter_cb filter, - void *payload) -{ - unsigned int i; - - assert(bld && filter); - - for (i = 0; i < bld->entries.length; ++i) { - git_tree_entry *entry = bld->entries.contents[i]; - if (!entry->removed && filter(entry, payload)) - entry->removed = 1; - } -} - -void git_treebuilder_clear(git_treebuilder *bld) -{ - unsigned int i; - assert(bld); - - for (i = 0; i < bld->entries.length; ++i) { - git_tree_entry *e = bld->entries.contents[i]; - git_tree_entry_free(e); - } - - git_vector_clear(&bld->entries); -} - -void git_treebuilder_free(git_treebuilder *bld) -{ - if (bld == NULL) - return; - - git_treebuilder_clear(bld); - git_vector_free(&bld->entries); - git__free(bld); -} - -static size_t subpath_len(const char *path) -{ - const char *slash_pos = strchr(path, '/'); - if (slash_pos == NULL) - return strlen(path); - - return slash_pos - path; -} - -int git_tree_entry_bypath( - git_tree_entry **entry_out, - git_tree *root, - const char *path) -{ - int error = 0; - git_tree *subtree; - const git_tree_entry *entry; - size_t filename_len; - - /* Find how long is the current path component (i.e. - * the filename between two slashes */ - filename_len = subpath_len(path); - - if (filename_len == 0) { - giterr_set(GITERR_TREE, "Invalid tree path given"); - return GIT_ENOTFOUND; - } - - entry = entry_fromname(root, path, filename_len); - - if (entry == NULL) { - giterr_set(GITERR_TREE, - "The path '%s' does not exist in the given tree", path); - return GIT_ENOTFOUND; - } - - switch (path[filename_len]) { - case '/': - /* If there are more components in the path... - * then this entry *must* be a tree */ - if (!git_tree_entry__is_tree(entry)) { - giterr_set(GITERR_TREE, - "The path '%s' does not exist in the given tree", path); - return GIT_ENOTFOUND; - } - - /* If there's only a slash left in the path, we - * return the current entry; otherwise, we keep - * walking down the path */ - if (path[filename_len + 1] != '\0') - break; - - case '\0': - /* If there are no more components in the path, return - * this entry */ - *entry_out = git_tree_entry_dup(entry); - return 0; - } - - if (git_tree_lookup(&subtree, root->object.repo, &entry->oid) < 0) - return -1; - - error = git_tree_entry_bypath( - entry_out, - subtree, - path + filename_len + 1 - ); - - git_tree_free(subtree); - return error; -} - -static int tree_walk( - const git_tree *tree, - git_treewalk_cb callback, - git_buf *path, - void *payload, - bool preorder) -{ - int error = 0; - size_t i; - - for (i = 0; i < tree->entries.length; ++i) { - const git_tree_entry *entry = tree->entries.contents[i]; - - if (preorder) { - error = callback(path->ptr, entry, payload); - if (error > 0) - continue; - if (error < 0) - return GIT_EUSER; - } - - if (git_tree_entry__is_tree(entry)) { - git_tree *subtree; - size_t path_len = git_buf_len(path); - - if ((error = git_tree_lookup( - &subtree, tree->object.repo, &entry->oid)) < 0) - break; - - /* append the next entry to the path */ - git_buf_puts(path, entry->filename); - git_buf_putc(path, '/'); - - if (git_buf_oom(path)) - return -1; - - error = tree_walk(subtree, callback, path, payload, preorder); - if (error != 0) - break; - - git_buf_truncate(path, path_len); - git_tree_free(subtree); - } - - if (!preorder && callback(path->ptr, entry, payload) < 0) { - error = GIT_EUSER; - break; - } - } - - return error; -} - -int git_tree_walk( - const git_tree *tree, - git_treewalk_mode mode, - git_treewalk_cb callback, - void *payload) -{ - int error = 0; - git_buf root_path = GIT_BUF_INIT; - - switch (mode) { - case GIT_TREEWALK_POST: - error = tree_walk(tree, callback, &root_path, payload, false); - break; - - case GIT_TREEWALK_PRE: - error = tree_walk(tree, callback, &root_path, payload, true); - break; - - default: - giterr_set(GITERR_INVALID, "Invalid walking mode for tree walk"); - return -1; - } - - git_buf_free(&root_path); - - return error; -} - diff --git a/src/tree.h b/src/tree.h deleted file mode 100644 index 27afd4fd4d4..00000000000 --- a/src/tree.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_tree_h__ -#define INCLUDE_tree_h__ - -#include "git2/tree.h" -#include "repository.h" -#include "odb.h" -#include "vector.h" - -struct git_tree_entry { - uint16_t removed; - uint16_t attr; - git_oid oid; - size_t filename_len; - char filename[1]; -}; - -struct git_tree { - git_object object; - git_vector entries; -}; - -struct git_treebuilder { - git_vector entries; -}; - -GIT_INLINE(int) git_tree__dup(git_tree **dest, git_tree *source) -{ - return git_object__dup((git_object **)dest, (git_object *)source); -} - -GIT_INLINE(bool) git_tree_entry__is_tree(const struct git_tree_entry *e) -{ - return (S_ISDIR(e->attr) && !S_ISGITLINK(e->attr)); -} - -extern int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2); - -void git_tree__free(git_tree *tree); -int git_tree__parse(git_tree *tree, git_odb_object *obj); - -/** - * Lookup the first position in the tree with a given prefix. - * - * @param tree a previously loaded tree. - * @param prefix the beginning of a path to find in the tree. - * @return index of the first item at or after the given prefix. - */ -int git_tree__prefix_position(git_tree *tree, const char *prefix); - - -/** - * Write a tree to the given repository - */ -int git_tree__write_index( - git_oid *oid, git_index *index, git_repository *repo); - -/** - * Obsolete mode kept for compatibility reasons - */ -#define GIT_FILEMODE_BLOB_GROUP_WRITABLE 0100664 - -#endif diff --git a/src/unix/map.c b/src/unix/map.c deleted file mode 100644 index 7de99c99d6b..00000000000 --- a/src/unix/map.c +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include - -#ifndef GIT_WIN32 - -#include "map.h" -#include -#include - -int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset) -{ - int mprot = 0; - int mflag = 0; - - GIT_MMAP_VALIDATE(out, len, prot, flags); - - out->data = NULL; - out->len = 0; - - if (prot & GIT_PROT_WRITE) - mprot = PROT_WRITE; - else if (prot & GIT_PROT_READ) - mprot = PROT_READ; - - if ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED) - mflag = MAP_SHARED; - else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE) - mflag = MAP_PRIVATE; - else - mflag = MAP_SHARED; - - out->data = mmap(NULL, len, mprot, mflag, fd, offset); - - if (!out->data || out->data == MAP_FAILED) { - giterr_set(GITERR_OS, "Failed to mmap. Could not write data"); - return -1; - } - - out->len = len; - - return 0; -} - -int p_munmap(git_map *map) -{ - assert(map != NULL); - munmap(map->data, map->len); - - return 0; -} - -#endif - diff --git a/src/unix/posix.h b/src/unix/posix.h deleted file mode 100644 index c738b531de7..00000000000 --- a/src/unix/posix.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_posix__w32_h__ -#define INCLUDE_posix__w32_h__ - -#include - -#define p_lstat(p,b) lstat(p,b) -#define p_readlink(a, b, c) readlink(a, b, c) -#define p_symlink(o,n) symlink(o, n) -#define p_link(o,n) link(o, n) -#define p_unlink(p) unlink(p) -#define p_mkdir(p,m) mkdir(p, m) -#define p_fsync(fd) fsync(fd) - -/* The OpenBSD realpath function behaves differently */ -#if !defined(__OpenBSD__) -# define p_realpath(p, po) realpath(p, po) -#endif - -#define p_vsnprintf(b, c, f, a) vsnprintf(b, c, f, a) -#define p_snprintf(b, c, f, ...) snprintf(b, c, f, __VA_ARGS__) -#define p_mkstemp(p) mkstemp(p) -#define p_setenv(n,v,o) setenv(n,v,o) -#define p_inet_pton(a, b, c) inet_pton(a, b, c) - -/* see win32/posix.h for explanation about why this exists */ -#define p_lstat_posixly(p,b) lstat(p,b) - -#endif diff --git a/src/unix/realpath.c b/src/unix/realpath.c deleted file mode 100644 index f382c2b73e0..00000000000 --- a/src/unix/realpath.c +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include - -#ifdef __OpenBSD__ - -#include -#include -#include -#include - -char *p_realpath(const char *pathname, char *resolved) -{ - char *ret; - - if ((ret = realpath(pathname, resolved)) == NULL) - return NULL; - - /* Figure out if the file exists */ - if (!access(ret, F_OK)) - ret; - - return NULL; -} - -#endif diff --git a/src/util.c b/src/util.c deleted file mode 100644 index 059108ece1f..00000000000 --- a/src/util.c +++ /dev/null @@ -1,609 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include -#include "common.h" -#include -#include -#include -#include "posix.h" - -#ifdef _MSC_VER -# include -#endif - -void git_libgit2_version(int *major, int *minor, int *rev) -{ - *major = LIBGIT2_VER_MAJOR; - *minor = LIBGIT2_VER_MINOR; - *rev = LIBGIT2_VER_REVISION; -} - -int git_libgit2_capabilities() -{ - return 0 -#ifdef GIT_THREADS - | GIT_CAP_THREADS -#endif -#if defined(GIT_SSL) || defined(GIT_WINHTTP) - | GIT_CAP_HTTPS -#endif - ; -} - -/* Declarations for tuneable settings */ -extern size_t git_mwindow__window_size; -extern size_t git_mwindow__mapped_limit; - -void git_libgit2_opts(int key, ...) -{ - va_list ap; - - va_start(ap, key); - - switch(key) { - case GIT_OPT_SET_MWINDOW_SIZE: - git_mwindow__window_size = va_arg(ap, size_t); - break; - - case GIT_OPT_GET_MWINDOW_SIZE: - *(va_arg(ap, size_t *)) = git_mwindow__window_size; - break; - - case GIT_OPT_SET_MWINDOW_MAPPED_LIMIT: - git_mwindow__mapped_limit = va_arg(ap, size_t); - break; - - case GIT_OPT_GET_MWINDOW_MAPPED_LIMIT: - *(va_arg(ap, size_t *)) = git_mwindow__mapped_limit; - break; - } - - va_end(ap); -} - -void git_strarray_free(git_strarray *array) -{ - size_t i; - for (i = 0; i < array->count; ++i) - git__free(array->strings[i]); - - git__free(array->strings); -} - -int git_strarray_copy(git_strarray *tgt, const git_strarray *src) -{ - size_t i; - - assert(tgt && src); - - memset(tgt, 0, sizeof(*tgt)); - - if (!src->count) - return 0; - - tgt->strings = git__calloc(src->count, sizeof(char *)); - GITERR_CHECK_ALLOC(tgt->strings); - - for (i = 0; i < src->count; ++i) { - tgt->strings[tgt->count] = git__strdup(src->strings[i]); - - if (!tgt->strings[tgt->count]) { - git_strarray_free(tgt); - memset(tgt, 0, sizeof(*tgt)); - return -1; - } - - tgt->count++; - } - - return 0; -} - -int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int base) -{ - const char *p; - int64_t n, nn; - int c, ovfl, v, neg, ndig; - - p = nptr; - neg = 0; - n = 0; - ndig = 0; - ovfl = 0; - - /* - * White space - */ - while (git__isspace(*p)) - p++; - - /* - * Sign - */ - if (*p == '-' || *p == '+') - if (*p++ == '-') - neg = 1; - - /* - * Base - */ - if (base == 0) { - if (*p != '0') - base = 10; - else { - base = 8; - if (p[1] == 'x' || p[1] == 'X') { - p += 2; - base = 16; - } - } - } else if (base == 16 && *p == '0') { - if (p[1] == 'x' || p[1] == 'X') - p += 2; - } else if (base < 0 || 36 < base) - goto Return; - - /* - * Non-empty sequence of digits - */ - for (;; p++,ndig++) { - c = *p; - v = base; - if ('0'<=c && c<='9') - v = c - '0'; - else if ('a'<=c && c<='z') - v = c - 'a' + 10; - else if ('A'<=c && c<='Z') - v = c - 'A' + 10; - if (v >= base) - break; - nn = n*base + v; - if (nn < n) - ovfl = 1; - n = nn; - } - -Return: - if (ndig == 0) { - giterr_set(GITERR_INVALID, "Failed to convert string to long. Not a number"); - return -1; - } - - if (endptr) - *endptr = p; - - if (ovfl) { - giterr_set(GITERR_INVALID, "Failed to convert string to long. Overflow error"); - return -1; - } - - *result = neg ? -n : n; - return 0; -} - -int git__strtol32(int32_t *result, const char *nptr, const char **endptr, int base) -{ - int error; - int32_t tmp_int; - int64_t tmp_long; - - if ((error = git__strtol64(&tmp_long, nptr, endptr, base)) < 0) - return error; - - tmp_int = tmp_long & 0xFFFFFFFF; - if (tmp_int != tmp_long) { - giterr_set(GITERR_INVALID, "Failed to convert. '%s' is too large", nptr); - return -1; - } - - *result = tmp_int; - - return error; -} - -int git__strcmp(const char *a, const char *b) -{ - while (*a && *b && *a == *b) - ++a, ++b; - return (int)(*(const unsigned char *)a) - (int)(*(const unsigned char *)b); -} - -int git__strcasecmp(const char *a, const char *b) -{ - while (*a && *b && tolower(*a) == tolower(*b)) - ++a, ++b; - return (tolower(*a) - tolower(*b)); -} - -int git__strncmp(const char *a, const char *b, size_t sz) -{ - while (sz && *a && *b && *a == *b) - --sz, ++a, ++b; - if (!sz) - return 0; - return (int)(*(const unsigned char *)a) - (int)(*(const unsigned char *)b); -} - -int git__strncasecmp(const char *a, const char *b, size_t sz) -{ - int al, bl; - - do { - al = (unsigned char)tolower(*a); - bl = (unsigned char)tolower(*b); - ++a, ++b; - } while (--sz && al && al == bl); - - return al - bl; -} - -void git__strntolower(char *str, size_t len) -{ - size_t i; - - for (i = 0; i < len; ++i) { - str[i] = (char) tolower(str[i]); - } -} - -void git__strtolower(char *str) -{ - git__strntolower(str, strlen(str)); -} - -int git__prefixcmp(const char *str, const char *prefix) -{ - for (;;) { - unsigned char p = *(prefix++), s; - if (!p) - return 0; - if ((s = *(str++)) != p) - return s - p; - } -} - -int git__prefixcmp_icase(const char *str, const char *prefix) -{ - return strncasecmp(str, prefix, strlen(prefix)); -} - -int git__suffixcmp(const char *str, const char *suffix) -{ - size_t a = strlen(str); - size_t b = strlen(suffix); - if (a < b) - return -1; - return strcmp(str + (a - b), suffix); -} - -char *git__strtok(char **end, const char *sep) -{ - char *ptr = *end; - - while (*ptr && strchr(sep, *ptr)) - ++ptr; - - if (*ptr) { - char *start = ptr; - *end = start + 1; - - while (**end && !strchr(sep, **end)) - ++*end; - - if (**end) { - **end = '\0'; - ++*end; - } - - return start; - } - - return NULL; -} - -/* Similar to strtok, but does not collapse repeated tokens. */ -char *git__strsep(char **end, const char *sep) -{ - char *start = *end, *ptr = *end; - - while (*ptr && !strchr(sep, *ptr)) - ++ptr; - - if (*ptr) { - *end = ptr + 1; - *ptr = '\0'; - - return start; - } - - return NULL; -} - -void git__hexdump(const char *buffer, size_t len) -{ - static const size_t LINE_WIDTH = 16; - - size_t line_count, last_line, i, j; - const char *line; - - line_count = (len / LINE_WIDTH); - last_line = (len % LINE_WIDTH); - - for (i = 0; i < line_count; ++i) { - line = buffer + (i * LINE_WIDTH); - for (j = 0; j < LINE_WIDTH; ++j, ++line) - printf("%02X ", (unsigned char)*line & 0xFF); - - printf("| "); - - line = buffer + (i * LINE_WIDTH); - for (j = 0; j < LINE_WIDTH; ++j, ++line) - printf("%c", (*line >= 32 && *line <= 126) ? *line : '.'); - - printf("\n"); - } - - if (last_line > 0) { - - line = buffer + (line_count * LINE_WIDTH); - for (j = 0; j < last_line; ++j, ++line) - printf("%02X ", (unsigned char)*line & 0xFF); - - for (j = 0; j < (LINE_WIDTH - last_line); ++j) - printf(" "); - - printf("| "); - - line = buffer + (line_count * LINE_WIDTH); - for (j = 0; j < last_line; ++j, ++line) - printf("%c", (*line >= 32 && *line <= 126) ? *line : '.'); - - printf("\n"); - } - - printf("\n"); -} - -#ifdef GIT_LEGACY_HASH -uint32_t git__hash(const void *key, int len, unsigned int seed) -{ - const uint32_t m = 0x5bd1e995; - const int r = 24; - uint32_t h = seed ^ len; - - const unsigned char *data = (const unsigned char *)key; - - while(len >= 4) { - uint32_t k = *(uint32_t *)data; - - k *= m; - k ^= k >> r; - k *= m; - - h *= m; - h ^= k; - - data += 4; - len -= 4; - } - - switch(len) { - case 3: h ^= data[2] << 16; - case 2: h ^= data[1] << 8; - case 1: h ^= data[0]; - h *= m; - }; - - h ^= h >> 13; - h *= m; - h ^= h >> 15; - - return h; -} -#else -/* - Cross-platform version of Murmurhash3 - http://code.google.com/p/smhasher/wiki/MurmurHash3 - by Austin Appleby (aappleby@gmail.com) - - This code is on the public domain. -*/ -uint32_t git__hash(const void *key, int len, uint32_t seed) -{ - -#define MURMUR_BLOCK() {\ - k1 *= c1; \ - k1 = git__rotl(k1,11);\ - k1 *= c2;\ - h1 ^= k1;\ - h1 = h1*3 + 0x52dce729;\ - c1 = c1*5 + 0x7b7d159c;\ - c2 = c2*5 + 0x6bce6396;\ -} - - const uint8_t *data = (const uint8_t*)key; - const int nblocks = len / 4; - - const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4); - const uint8_t *tail = (const uint8_t *)(data + nblocks * 4); - - uint32_t h1 = 0x971e137b ^ seed; - uint32_t k1; - - uint32_t c1 = 0x95543787; - uint32_t c2 = 0x2ad7eb25; - - int i; - - for (i = -nblocks; i; i++) { - k1 = blocks[i]; - MURMUR_BLOCK(); - } - - k1 = 0; - - switch(len & 3) { - case 3: k1 ^= tail[2] << 16; - case 2: k1 ^= tail[1] << 8; - case 1: k1 ^= tail[0]; - MURMUR_BLOCK(); - } - - h1 ^= len; - h1 ^= h1 >> 16; - h1 *= 0x85ebca6b; - h1 ^= h1 >> 13; - h1 *= 0xc2b2ae35; - h1 ^= h1 >> 16; - - return h1; -} -#endif - -/** - * A modified `bsearch` from the BSD glibc. - * - * Copyright (c) 1990 Regents of the University of California. - * All rights reserved. - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. [rescinded 22 July 1999] - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -int git__bsearch( - void **array, - size_t array_len, - const void *key, - int (*compare)(const void *, const void *), - size_t *position) -{ - size_t lim; - int cmp = -1; - void **part, **base = array; - - for (lim = array_len; lim != 0; lim >>= 1) { - part = base + (lim >> 1); - cmp = (*compare)(key, *part); - if (cmp == 0) { - base = part; - break; - } - if (cmp > 0) { /* key > p; take right partition */ - base = part + 1; - lim--; - } /* else take left partition */ - } - - if (position) - *position = (base - array); - - return (cmp == 0) ? 0 : GIT_ENOTFOUND; -} - -int git__bsearch_r( - void **array, - size_t array_len, - const void *key, - int (*compare_r)(const void *, const void *, void *), - void *payload, - size_t *position) -{ - size_t lim; - int cmp = -1; - void **part, **base = array; - - for (lim = array_len; lim != 0; lim >>= 1) { - part = base + (lim >> 1); - cmp = (*compare_r)(key, *part, payload); - if (cmp == 0) { - base = part; - break; - } - if (cmp > 0) { /* key > p; take right partition */ - base = part + 1; - lim--; - } /* else take left partition */ - } - - if (position) - *position = (base - array); - - return (cmp == 0) ? 0 : GIT_ENOTFOUND; -} - -/** - * A strcmp wrapper - * - * We don't want direct pointers to the CRT on Windows, we may - * get stdcall conflicts. - */ -int git__strcmp_cb(const void *a, const void *b) -{ - const char *stra = (const char *)a; - const char *strb = (const char *)b; - - return strcmp(stra, strb); -} - -int git__parse_bool(int *out, const char *value) -{ - /* A missing value means true */ - if (value == NULL || - !strcasecmp(value, "true") || - !strcasecmp(value, "yes") || - !strcasecmp(value, "on")) { - *out = 1; - return 0; - } - if (!strcasecmp(value, "false") || - !strcasecmp(value, "no") || - !strcasecmp(value, "off") || - value[0] == '\0') { - *out = 0; - return 0; - } - - return -1; -} - -size_t git__unescape(char *str) -{ - char *scan, *pos = str; - - for (scan = str; *scan; pos++, scan++) { - if (*scan == '\\' && *(scan + 1) != '\0') - scan++; /* skip '\' but include next char */ - if (pos != scan) - *pos = *scan; - } - - if (pos != scan) { - *pos = '\0'; - } - - return (pos - str); -} diff --git a/src/util.h b/src/util.h deleted file mode 100644 index e75d777a8e0..00000000000 --- a/src/util.h +++ /dev/null @@ -1,288 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_util_h__ -#define INCLUDE_util_h__ - -#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) -#define bitsizeof(x) (CHAR_BIT * sizeof(x)) -#define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits)))) -#ifndef min -# define min(a,b) ((a) < (b) ? (a) : (b)) -#endif -#ifndef max -# define max(a,b) ((a) > (b) ? (a) : (b)) -#endif - -/* - * Custom memory allocation wrappers - * that set error code and error message - * on allocation failure - */ -GIT_INLINE(void *) git__malloc(size_t len) -{ - void *ptr = malloc(len); - if (!ptr) giterr_set_oom(); - return ptr; -} - -GIT_INLINE(void *) git__calloc(size_t nelem, size_t elsize) -{ - void *ptr = calloc(nelem, elsize); - if (!ptr) giterr_set_oom(); - return ptr; -} - -GIT_INLINE(char *) git__strdup(const char *str) -{ - char *ptr = strdup(str); - if (!ptr) giterr_set_oom(); - return ptr; -} - -GIT_INLINE(char *) git__strndup(const char *str, size_t n) -{ - size_t length = 0; - char *ptr; - - while (length < n && str[length]) - ++length; - - ptr = (char*)malloc(length + 1); - if (!ptr) { - giterr_set_oom(); - return NULL; - } - - if (length) - memcpy(ptr, str, length); - - ptr[length] = '\0'; - - return ptr; -} - -GIT_INLINE(void *) git__realloc(void *ptr, size_t size) -{ - void *new_ptr = realloc(ptr, size); - if (!new_ptr) giterr_set_oom(); - return new_ptr; -} - -#define git__free(ptr) free(ptr) - -#define STRCMP_CASESELECT(IGNORE_CASE, STR1, STR2) \ - ((IGNORE_CASE) ? strcasecmp((STR1), (STR2)) : strcmp((STR1), (STR2))) - -#define CASESELECT(IGNORE_CASE, ICASE, CASE) \ - ((IGNORE_CASE) ? (ICASE) : (CASE)) - -extern int git__prefixcmp(const char *str, const char *prefix); -extern int git__prefixcmp_icase(const char *str, const char *prefix); -extern int git__suffixcmp(const char *str, const char *suffix); - -GIT_INLINE(int) git__signum(int val) -{ - return ((val > 0) - (val < 0)); -} - -extern int git__strtol32(int32_t *n, const char *buff, const char **end_buf, int base); -extern int git__strtol64(int64_t *n, const char *buff, const char **end_buf, int base); - -extern void git__hexdump(const char *buffer, size_t n); -extern uint32_t git__hash(const void *key, int len, uint32_t seed); - -/** @return true if p fits into the range of a size_t */ -GIT_INLINE(int) git__is_sizet(git_off_t p) -{ - size_t r = (size_t)p; - return p == (git_off_t)r; -} - -/* 32-bit cross-platform rotl */ -#ifdef _MSC_VER /* use built-in method in MSVC */ -# define git__rotl(v, s) (uint32_t)_rotl(v, s) -#else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */ -# define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s)))) -#endif - -extern char *git__strtok(char **end, const char *sep); -extern char *git__strsep(char **end, const char *sep); - -extern void git__strntolower(char *str, size_t len); -extern void git__strtolower(char *str); - -GIT_INLINE(const char *) git__next_line(const char *s) -{ - while (*s && *s != '\n') s++; - while (*s == '\n' || *s == '\r') s++; - return s; -} - -typedef int (*git__tsort_cmp)(const void *a, const void *b); - -extern void git__tsort(void **dst, size_t size, git__tsort_cmp cmp); - -typedef int (*git__tsort_r_cmp)(const void *a, const void *b, void *payload); - -extern void git__tsort_r( - void **dst, size_t size, git__tsort_r_cmp cmp, void *payload); - - -/** - * @param position If non-NULL, this will be set to the position where the - * element is or would be inserted if not found. - * @return 0 if found; GIT_ENOTFOUND if not found - */ -extern int git__bsearch( - void **array, - size_t array_len, - const void *key, - int (*compare)(const void *key, const void *element), - size_t *position); - -extern int git__bsearch_r( - void **array, - size_t array_len, - const void *key, - int (*compare_r)(const void *key, const void *element, void *payload), - void *payload, - size_t *position); - -extern int git__strcmp_cb(const void *a, const void *b); - -extern int git__strcmp(const char *a, const char *b); -extern int git__strcasecmp(const char *a, const char *b); -extern int git__strncmp(const char *a, const char *b, size_t sz); -extern int git__strncasecmp(const char *a, const char *b, size_t sz); - -typedef struct { - short refcount; - void *owner; -} git_refcount; - -typedef void (*git_refcount_freeptr)(void *r); - -#define GIT_REFCOUNT_INC(r) { \ - ((git_refcount *)(r))->refcount++; \ -} - -#define GIT_REFCOUNT_DEC(_r, do_free) { \ - git_refcount *r = (git_refcount *)(_r); \ - r->refcount--; \ - if (r->refcount <= 0 && r->owner == NULL) { do_free(_r); } \ -} - -#define GIT_REFCOUNT_OWN(r, o) { \ - ((git_refcount *)(r))->owner = o; \ -} - -#define GIT_REFCOUNT_OWNER(r) (((git_refcount *)(r))->owner) - -static signed char from_hex[] = { --1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */ --1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */ --1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */ --1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */ --1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */ --1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */ --1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */ --1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */ --1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */ --1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */ --1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */ --1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */ --1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */ --1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */ --1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */ -}; - -GIT_INLINE(int) git__fromhex(char h) -{ - return from_hex[(unsigned char) h]; -} - -GIT_INLINE(int) git__ishex(const char *str) -{ - unsigned i; - for (i=0; i> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - - return v; -} - -GIT_INLINE(size_t) git__size_t_powerof2(size_t v) -{ - return git__size_t_bitmask(v) + 1; -} - -GIT_INLINE(bool) git__isupper(int c) -{ - return (c >= 'A' && c <= 'Z'); -} - -GIT_INLINE(bool) git__isalpha(int c) -{ - return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); -} - -GIT_INLINE(bool) git__isdigit(int c) -{ - return (c >= '0' && c <= '9'); -} - -GIT_INLINE(bool) git__isspace(int c) -{ - return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v' || c == 0x85 /* Unicode CR+LF */); -} - -GIT_INLINE(bool) git__iswildcard(int c) -{ - return (c == '*' || c == '?' || c == '['); -} - -/* - * Parse a string value as a boolean, just like Core Git - * does. - * - * Valid values for true are: 'true', 'yes', 'on' - * Valid values for false are: 'false', 'no', 'off' - */ -extern int git__parse_bool(int *out, const char *value); - -/* - * Parse a string into a value as a git_time_t. - * - * Sample valid input: - * - "yesterday" - * - "July 17, 2003" - * - "2003-7-17 08:23" - */ -int git__date_parse(git_time_t *out, const char *date); - -/* - * Unescapes a string in-place. - * - * Edge cases behavior: - * - "jackie\" -> "jacky\" - * - "chan\\" -> "chan\" - */ -extern size_t git__unescape(char *str); - -#endif /* INCLUDE_util_h__ */ diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt new file mode 100644 index 00000000000..0811057f3a3 --- /dev/null +++ b/src/util/CMakeLists.txt @@ -0,0 +1,81 @@ +# util: a shared library for common utility functions for libgit2 projects + +add_library(util OBJECT) + +configure_file(git2_features.h.in git2_features.h) + +set(UTIL_INCLUDES + "${PROJECT_BINARY_DIR}/src/util" + "${PROJECT_BINARY_DIR}/include" + "${PROJECT_SOURCE_DIR}/src/util" + "${PROJECT_SOURCE_DIR}/include") + +file(GLOB UTIL_SRC *.c *.h allocators/*.c allocators/*.h hash.h) +list(SORT UTIL_SRC) + +# +# Platform specific sources +# + +if(WIN32 AND NOT CYGWIN) + file(GLOB UTIL_SRC_OS win32/*.c win32/*.h) + list(SORT UTIL_SRC_OS) +elseif(NOT AMIGA) + file(GLOB UTIL_SRC_OS unix/*.c unix/*.h) + list(SORT UTIL_SRC_OS) +endif() + +# +# Hash backend selection +# + +if(USE_SHA1 STREQUAL "builtin") + file(GLOB UTIL_SRC_SHA1 hash/collisiondetect.* hash/sha1dc/*) + target_compile_definitions(util PRIVATE SHA1DC_NO_STANDARD_INCLUDES=1) + target_compile_definitions(util PRIVATE SHA1DC_CUSTOM_INCLUDE_SHA1_C=\"git2_util.h\") + target_compile_definitions(util PRIVATE SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"git2_util.h\") +elseif(USE_SHA1 STREQUAL "openssl" OR + USE_SHA1 STREQUAL "openssl-dynamic" OR + USE_SHA1 STREQUAL "openssl-fips") + add_definitions(-DOPENSSL_API_COMPAT=0x10100000L) + file(GLOB UTIL_SRC_SHA1 hash/openssl.*) +elseif(USE_SHA1 STREQUAL "commoncrypto") + file(GLOB UTIL_SRC_SHA1 hash/common_crypto.*) +elseif(USE_SHA1 STREQUAL "mbedtls") + file(GLOB UTIL_SRC_SHA1 hash/mbedtls.*) +elseif(USE_SHA1 STREQUAL "win32") + file(GLOB UTIL_SRC_SHA1 hash/win32.*) +else() + message(FATAL_ERROR "Asked for unknown SHA1 backend: ${USE_SHA1}") +endif() + +list(SORT UTIL_SRC_SHA1) + +if(USE_SHA256 STREQUAL "builtin") + file(GLOB UTIL_SRC_SHA256 hash/builtin.* hash/rfc6234/*) +elseif(USE_SHA256 STREQUAL "openssl" OR + USE_SHA256 STREQUAL "openssl-dynamic" OR + USE_SHA256 STREQUAL "openssl-fips") + add_definitions(-DOPENSSL_API_COMPAT=0x10100000L) + file(GLOB UTIL_SRC_SHA256 hash/openssl.*) +elseif(USE_SHA256 STREQUAL "commoncrypto") + file(GLOB UTIL_SRC_SHA256 hash/common_crypto.*) +elseif(USE_SHA256 STREQUAL "mbedtls") + file(GLOB UTIL_SRC_SHA256 hash/mbedtls.*) +elseif(USE_SHA256 STREQUAL "win32") + file(GLOB UTIL_SRC_SHA256 hash/win32.*) +else() + message(FATAL_ERROR "asked for unknown SHA256 backend: ${USE_SHA256}") +endif() + +list(SORT UTIL_SRC_SHA256) + +# +# Build the library +# + +target_sources(util PRIVATE ${UTIL_SRC} ${UTIL_SRC_OS} ${UTIL_SRC_SHA1} ${UTIL_SRC_SHA256}) +ide_split_sources(util) + +target_include_directories(util PRIVATE ${UTIL_INCLUDES} ${LIBGIT2_DEPENDENCY_INCLUDES} PUBLIC ${libgit2_SOURCE_DIR}/include) +target_include_directories(util SYSTEM PRIVATE ${LIBGIT2_SYSTEM_INCLUDES}) diff --git a/src/util/alloc.c b/src/util/alloc.c new file mode 100644 index 00000000000..1059cb65744 --- /dev/null +++ b/src/util/alloc.c @@ -0,0 +1,118 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "alloc.h" +#include "runtime.h" + +#include "allocators/stdalloc.h" +#include "allocators/debugalloc.h" +#include "allocators/failalloc.h" +#include "allocators/win32_leakcheck.h" + +/* Fail any allocation until git_libgit2_init is called. */ +git_allocator git__allocator = { + git_failalloc_malloc, + git_failalloc_realloc, + git_failalloc_free +}; + +void *git__calloc(size_t nelem, size_t elsize) +{ + size_t newsize; + void *ptr; + + if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize)) + return NULL; + + if ((ptr = git__malloc(newsize))) + memset(ptr, 0, newsize); + + return ptr; +} + +void *git__reallocarray(void *ptr, size_t nelem, size_t elsize) +{ + size_t newsize; + + if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize)) + return NULL; + + return git__realloc(ptr, newsize); +} + +void *git__mallocarray(size_t nelem, size_t elsize) +{ + return git__reallocarray(NULL, nelem, elsize); +} + +char *git__strdup(const char *str) +{ + size_t len = strlen(str) + 1; + void *ptr = git__malloc(len); + + if (ptr) + memcpy(ptr, str, len); + + return ptr; +} + +char *git__strndup(const char *str, size_t n) +{ + size_t len = p_strnlen(str, n); + char *ptr = git__malloc(len + 1); + + if (ptr) { + memcpy(ptr, str, len); + ptr[len] = '\0'; + } + + return ptr; +} + +char *git__substrdup(const char *str, size_t n) +{ + char *ptr = git__malloc(n + 1); + + if (ptr) { + memcpy(ptr, str, n); + ptr[n] = '\0'; + } + + return ptr; +} + +static int setup_default_allocator(void) +{ +#if defined(GIT_DEBUG_LEAKCHECK_WIN32) + return git_win32_leakcheck_init_allocator(&git__allocator); +#elif defined(GIT_DEBUG_STRICT_ALLOC) + return git_debugalloc_init_allocator(&git__allocator); +#else + return git_stdalloc_init_allocator(&git__allocator); +#endif +} + +int git_allocator_global_init(void) +{ + /* + * We don't want to overwrite any allocator which has been set + * before the init function is called. + */ + if (git__allocator.gmalloc != git_failalloc_malloc) + return 0; + + return setup_default_allocator(); +} + +int git_allocator_setup(git_allocator *allocator) +{ + if (!allocator) + return setup_default_allocator(); + + memcpy(&git__allocator, allocator, sizeof(*allocator)); + return 0; +} diff --git a/src/util/alloc.h b/src/util/alloc.h new file mode 100644 index 00000000000..32b614b25cc --- /dev/null +++ b/src/util/alloc.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_alloc_h__ +#define INCLUDE_alloc_h__ + +#include "git2/sys/alloc.h" + +#include "git2_util.h" + +extern git_allocator git__allocator; + +GIT_INLINE(void *) git__malloc(size_t len) +{ + void *p = git__allocator.gmalloc(len, __FILE__, __LINE__); + + if (!p) + git_error_set_oom(); + + return p; +} + +GIT_INLINE(void *) git__realloc(void *ptr, size_t size) +{ + void *p = git__allocator.grealloc(ptr, size, __FILE__, __LINE__); + + if (!p) + git_error_set_oom(); + + return p; +} + +GIT_INLINE(void) git__free(void *ptr) +{ + git__allocator.gfree(ptr); +} + +extern void *git__calloc(size_t nelem, size_t elsize); +extern void *git__mallocarray(size_t nelem, size_t elsize); +extern void *git__reallocarray(void *ptr, size_t nelem, size_t elsize); + +extern char *git__strdup(const char *str); +extern char *git__strndup(const char *str, size_t n); +extern char *git__substrdup(const char *str, size_t n); + +/** + * This function is being called by our global setup routines to + * initialize the standard allocator. + */ +int git_allocator_global_init(void); + +/** + * Switch out libgit2's global memory allocator + * + * @param allocator The new allocator that should be used. All function pointers + * of it need to be set correctly. + * @return An error code or 0. + */ +int git_allocator_setup(git_allocator *allocator); + +#endif diff --git a/src/util/allocators/debugalloc.c b/src/util/allocators/debugalloc.c new file mode 100644 index 00000000000..44022cd785a --- /dev/null +++ b/src/util/allocators/debugalloc.c @@ -0,0 +1,73 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "debugalloc.h" + +static void *debugalloc__malloc(size_t len, const char *file, int line) +{ + unsigned char *ptr; + size_t total = len + sizeof(size_t); + + GIT_UNUSED(file); + GIT_UNUSED(line); + + if (!len || (ptr = malloc(total)) == NULL) + return NULL; + + memcpy(ptr, &len, sizeof(size_t)); + return ptr + sizeof(size_t); +} + +static void *debugalloc__realloc(void *_ptr, size_t len, const char *file, int line) +{ + unsigned char *ptr = _ptr, *newptr; + size_t original_len; + size_t total = len + sizeof(size_t); + + GIT_UNUSED(file); + GIT_UNUSED(line); + + if (!len && !ptr) + return NULL; + + if (!len) { + free(ptr - sizeof(size_t)); + return NULL; + } + + if ((newptr = malloc(total)) == NULL) + return NULL; + + if (ptr) { + memcpy(&original_len, ptr - sizeof(size_t), sizeof(size_t)); + memcpy(newptr + sizeof(size_t), ptr, min(len, original_len)); + + memset(ptr - sizeof(size_t), 0xfd, original_len + sizeof(size_t)); + free(ptr - sizeof(size_t)); + } + + memcpy(newptr, &len, sizeof(size_t)); + return newptr + sizeof(size_t); +} + +static void debugalloc__free(void *_ptr) +{ + unsigned char *ptr = _ptr; + + if (!ptr) + return; + + free(ptr - sizeof(size_t)); +} + +int git_debugalloc_init_allocator(git_allocator *allocator) +{ + allocator->gmalloc = debugalloc__malloc; + allocator->grealloc = debugalloc__realloc; + allocator->gfree = debugalloc__free; + return 0; +} diff --git a/src/util/allocators/debugalloc.h b/src/util/allocators/debugalloc.h new file mode 100644 index 00000000000..dea0ca31cc1 --- /dev/null +++ b/src/util/allocators/debugalloc.h @@ -0,0 +1,17 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_allocators_debugalloc_h__ +#define INCLUDE_allocators_debugalloc_h__ + +#include "git2_util.h" + +#include "alloc.h" + +int git_debugalloc_init_allocator(git_allocator *allocator); + +#endif diff --git a/src/util/allocators/failalloc.c b/src/util/allocators/failalloc.c new file mode 100644 index 00000000000..c1025e32fef --- /dev/null +++ b/src/util/allocators/failalloc.c @@ -0,0 +1,32 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "failalloc.h" + +void *git_failalloc_malloc(size_t len, const char *file, int line) +{ + GIT_UNUSED(len); + GIT_UNUSED(file); + GIT_UNUSED(line); + + return NULL; +} + +void *git_failalloc_realloc(void *ptr, size_t size, const char *file, int line) +{ + GIT_UNUSED(ptr); + GIT_UNUSED(size); + GIT_UNUSED(file); + GIT_UNUSED(line); + + return NULL; +} + +void git_failalloc_free(void *ptr) +{ + GIT_UNUSED(ptr); +} diff --git a/src/util/allocators/failalloc.h b/src/util/allocators/failalloc.h new file mode 100644 index 00000000000..a3788e634ee --- /dev/null +++ b/src/util/allocators/failalloc.h @@ -0,0 +1,17 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_allocators_failalloc_h__ +#define INCLUDE_allocators_failalloc_h__ + +#include "git2_util.h" + +extern void *git_failalloc_malloc(size_t len, const char *file, int line); +extern void *git_failalloc_realloc(void *ptr, size_t size, const char *file, int line); +extern void git_failalloc_free(void *ptr); + +#endif diff --git a/src/util/allocators/stdalloc.c b/src/util/allocators/stdalloc.c new file mode 100644 index 00000000000..65ec40fbe9d --- /dev/null +++ b/src/util/allocators/stdalloc.c @@ -0,0 +1,37 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "stdalloc.h" + +static void *stdalloc__malloc(size_t len, const char *file, int line) +{ + GIT_UNUSED(file); + GIT_UNUSED(line); + + return malloc(len); +} + +static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line) +{ + GIT_UNUSED(file); + GIT_UNUSED(line); + + return realloc(ptr, size); +} + +static void stdalloc__free(void *ptr) +{ + free(ptr); +} + +int git_stdalloc_init_allocator(git_allocator *allocator) +{ + allocator->gmalloc = stdalloc__malloc; + allocator->grealloc = stdalloc__realloc; + allocator->gfree = stdalloc__free; + return 0; +} diff --git a/src/util/allocators/stdalloc.h b/src/util/allocators/stdalloc.h new file mode 100644 index 00000000000..955038cb0d5 --- /dev/null +++ b/src/util/allocators/stdalloc.h @@ -0,0 +1,17 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_allocators_stdalloc_h__ +#define INCLUDE_allocators_stdalloc_h__ + +#include "git2_util.h" + +#include "alloc.h" + +int git_stdalloc_init_allocator(git_allocator *allocator); + +#endif diff --git a/src/util/allocators/win32_leakcheck.c b/src/util/allocators/win32_leakcheck.c new file mode 100644 index 00000000000..f17f432718f --- /dev/null +++ b/src/util/allocators/win32_leakcheck.c @@ -0,0 +1,50 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "win32_leakcheck.h" + +#if defined(GIT_DEBUG_LEAKCHECK_WIN32) + +#include "win32/w32_leakcheck.h" + +static void *leakcheck_malloc(size_t len, const char *file, int line) +{ + void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line); + if (!ptr) git_error_set_oom(); + return ptr; +} + +static void *leakcheck_realloc(void *ptr, size_t size, const char *file, int line) +{ + void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line); + if (!new_ptr) git_error_set_oom(); + return new_ptr; +} + +static void leakcheck_free(void *ptr) +{ + free(ptr); +} + +int git_win32_leakcheck_init_allocator(git_allocator *allocator) +{ + allocator->gmalloc = leakcheck_malloc; + allocator->grealloc = leakcheck_realloc; + allocator->gfree = leakcheck_free; + return 0; +} + +#else + +int git_win32_leakcheck_init_allocator(git_allocator *allocator) +{ + GIT_UNUSED(allocator); + git_error_set(GIT_EINVALID, "leakcheck memory allocator not available"); + return -1; +} + +#endif diff --git a/src/util/allocators/win32_leakcheck.h b/src/util/allocators/win32_leakcheck.h new file mode 100644 index 00000000000..edcd9307f90 --- /dev/null +++ b/src/util/allocators/win32_leakcheck.h @@ -0,0 +1,17 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_allocators_win32_leakcheck_h +#define INCLUDE_allocators_win32_leakcheck_h + +#include "git2_util.h" + +#include "alloc.h" + +int git_win32_leakcheck_init_allocator(git_allocator *allocator); + +#endif diff --git a/src/util/array.h b/src/util/array.h new file mode 100644 index 00000000000..515e6e3ab46 --- /dev/null +++ b/src/util/array.h @@ -0,0 +1,130 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_array_h__ +#define INCLUDE_array_h__ + +#include "git2_util.h" + +/* + * Use this to declare a typesafe resizable array of items, a la: + * + * git_array_t(int) my_ints = GIT_ARRAY_INIT; + * ... + * int *i = git_array_alloc(my_ints); + * GIT_ERROR_CHECK_ALLOC(i); + * ... + * git_array_clear(my_ints); + * + * You may also want to do things like: + * + * typedef git_array_t(my_struct) my_struct_array_t; + */ +#define git_array_t(type) struct { type *ptr; size_t size, asize; } + +#define GIT_ARRAY_INIT { NULL, 0, 0 } + +#define git_array_init(a) \ + do { (a).size = (a).asize = 0; (a).ptr = NULL; } while (0) + +#define git_array_init_to_size(a, desired) \ + do { (a).size = 0; (a).asize = desired; (a).ptr = git__calloc(desired, sizeof(*(a).ptr)); } while (0) + +#define git_array_dispose(a) \ + do { git__free((a).ptr); } while (0) + +#define git_array_clear(a) \ + do { git__free((a).ptr); git_array_init(a); } while (0) + +#define GIT_ERROR_CHECK_ARRAY(a) GIT_ERROR_CHECK_ALLOC((a).ptr) + +GIT_INLINE(void *) git_array__alloc(void *arr, size_t *size, size_t *asize, size_t item_size) +{ + size_t new_size; + void *new_array; + + if (*size < *asize) + return arr; + + if (*size < 8) { + new_size = 8; + } else { + if (GIT_MULTIPLY_SIZET_OVERFLOW(&new_size, *asize, 3)) + goto on_oom; + + new_size /= 2; + } + + if ((new_array = git__reallocarray(arr, new_size, item_size)) == NULL) + goto on_oom; + + *asize = new_size; + + return new_array; + +on_oom: + git__free(arr); + *size = 0; + *asize = 0; + return NULL; +} + +#define git_array_alloc(a) \ + (((a).size < (a).asize || \ + ((a).ptr = git_array__alloc((a).ptr, &(a).size, &(a).asize, sizeof(*(a).ptr))) != NULL) ? &(a).ptr[(a).size++] : (void *)NULL) + +#define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : (void *)NULL) + +#define git_array_pop(a) ((a).size ? &(a).ptr[--(a).size] : (void *)NULL) + +#define git_array_get(a, i) (((i) < (a).size) ? &(a).ptr[(i)] : (void *)NULL) + +#define git_array_size(a) (a).size + +#define git_array_valid_index(a, i) ((i) < (a).size) + +#define git_array_foreach(a, i, element) \ + for ((i) = 0; (i) < (a).size && ((element) = &(a).ptr[(i)]); (i)++) + +typedef int (*git_array_compare_cb)(const void *, const void *); + +GIT_INLINE(int) git_array__search( + size_t *out, + void *array_ptr, + size_t item_size, + size_t array_len, + git_array_compare_cb compare, + const void *key) +{ + size_t lim; + unsigned char *part, *array = array_ptr, *base = array_ptr; + int cmp = -1; + + for (lim = array_len; lim != 0; lim >>= 1) { + part = base + (lim >> 1) * item_size; + cmp = (*compare)(key, part); + + if (cmp == 0) { + base = part; + break; + } + if (cmp > 0) { /* key > p; take right partition */ + base = part + 1 * item_size; + lim--; + } /* else take left partition */ + } + + if (out) + *out = (base - array) / item_size; + + return (cmp == 0) ? 0 : GIT_ENOTFOUND; +} + +#define git_array_search(out, a, cmp, key) \ + git_array__search(out, (a).ptr, sizeof(*(a).ptr), (a).size, \ + (cmp), (key)) + +#endif diff --git a/src/util/assert_safe.h b/src/util/assert_safe.h new file mode 100644 index 00000000000..cc0bac551d2 --- /dev/null +++ b/src/util/assert_safe.h @@ -0,0 +1,74 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_assert_safe_h__ +#define INCLUDE_assert_safe_h__ + +/* + * In a debug build, we'll assert(3) for aide in debugging. In release + * builds, we will provide macros that will set an error message that + * indicate a failure and return. Note that memory leaks can occur in + * a release-mode assertion failure -- it is impractical to provide + * safe clean up routines in these very extreme failures, but care + * should be taken to not leak very large objects. + */ + +#if (defined(_DEBUG) || defined(GIT_ASSERT_HARD)) && GIT_ASSERT_HARD != 0 +# include + +# define GIT_ASSERT(expr) assert(expr) +# define GIT_ASSERT_ARG(expr) assert(expr) + +# define GIT_ASSERT_WITH_RETVAL(expr, fail) assert(expr) +# define GIT_ASSERT_ARG_WITH_RETVAL(expr, fail) assert(expr) + +# define GIT_ASSERT_WITH_CLEANUP(expr, cleanup) assert(expr) +#else + +/** Internal consistency check to stop the function. */ +# define GIT_ASSERT(expr) GIT_ASSERT_WITH_RETVAL(expr, -1) + +/** + * Assert that a consumer-provided argument is valid, setting an + * actionable error message and returning -1 if it is not. + */ +# define GIT_ASSERT_ARG(expr) GIT_ASSERT_ARG_WITH_RETVAL(expr, -1) + +/** Internal consistency check to return the `fail` param on failure. */ +# define GIT_ASSERT_WITH_RETVAL(expr, fail) \ + GIT_ASSERT__WITH_RETVAL(expr, GIT_ERROR_INTERNAL, "unrecoverable internal error", fail) + +/** + * Assert that a consumer-provided argument is valid, setting an + * actionable error message and returning the `fail` param if not. + */ +# define GIT_ASSERT_ARG_WITH_RETVAL(expr, fail) \ + GIT_ASSERT__WITH_RETVAL(expr, GIT_ERROR_INVALID, "invalid argument", fail) + +# define GIT_ASSERT__WITH_RETVAL(expr, code, msg, fail) do { \ + if (!(expr)) { \ + git_error_set(code, "%s: '%s'", msg, #expr); \ + return fail; \ + } \ + } while(0) + +/** + * Go to to the given label on assertion failures; useful when you have + * taken a lock or otherwise need to release a resource. + */ +# define GIT_ASSERT_WITH_CLEANUP(expr, cleanup) \ + GIT_ASSERT__WITH_CLEANUP(expr, GIT_ERROR_INTERNAL, "unrecoverable internal error", cleanup) + +# define GIT_ASSERT__WITH_CLEANUP(expr, code, msg, cleanup) do { \ + if (!(expr)) { \ + git_error_set(code, "%s: '%s'", msg, #expr); \ + cleanup; \ + } \ + } while(0) + +#endif /* GIT_ASSERT_HARD */ + +#endif diff --git a/src/util/bitvec.h b/src/util/bitvec.h new file mode 100644 index 00000000000..544832d9525 --- /dev/null +++ b/src/util/bitvec.h @@ -0,0 +1,75 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_bitvec_h__ +#define INCLUDE_bitvec_h__ + +#include "common.h" + +/* + * This is a silly little fixed length bit vector type that will store + * vectors of 64 bits or less directly in the structure and allocate + * memory for vectors longer than 64 bits. You can use the two versions + * transparently through the API and avoid heap allocation completely when + * using a short bit vector as a result. + */ +typedef struct { + size_t length; + union { + uint64_t *words; + uint64_t bits; + } u; +} git_bitvec; + +GIT_INLINE(int) git_bitvec_init(git_bitvec *bv, size_t capacity) +{ + memset(bv, 0x0, sizeof(*bv)); + + if (capacity >= 64) { + bv->length = (capacity / 64) + 1; + bv->u.words = git__calloc(bv->length, sizeof(uint64_t)); + if (!bv->u.words) + return -1; + } + + return 0; +} + +#define GIT_BITVEC_MASK(BIT) ((uint64_t)1 << (BIT % 64)) +#define GIT_BITVEC_WORD(BV, BIT) (BV->length ? &BV->u.words[BIT / 64] : &BV->u.bits) + +GIT_INLINE(void) git_bitvec_set(git_bitvec *bv, size_t bit, bool on) +{ + uint64_t *word = GIT_BITVEC_WORD(bv, bit); + uint64_t mask = GIT_BITVEC_MASK(bit); + + if (on) + *word |= mask; + else + *word &= ~mask; +} + +GIT_INLINE(bool) git_bitvec_get(git_bitvec *bv, size_t bit) +{ + uint64_t *word = GIT_BITVEC_WORD(bv, bit); + return (*word & GIT_BITVEC_MASK(bit)) != 0; +} + +GIT_INLINE(void) git_bitvec_clear(git_bitvec *bv) +{ + if (!bv->length) + bv->u.bits = 0; + else + memset(bv->u.words, 0x0, bv->length * sizeof(uint64_t)); +} + +GIT_INLINE(void) git_bitvec_free(git_bitvec *bv) +{ + if (bv->length) + git__free(bv->u.words); +} + +#endif diff --git a/src/util/cc-compat.h b/src/util/cc-compat.h new file mode 100644 index 00000000000..ae81970eae8 --- /dev/null +++ b/src/util/cc-compat.h @@ -0,0 +1,110 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_cc_compat_h__ +#define INCLUDE_cc_compat_h__ + +#include + +/* + * See if our compiler is known to support flexible array members. + */ +#ifndef GIT_FLEX_ARRAY +# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) +# define GIT_FLEX_ARRAY /* empty */ +# elif defined(__GNUC__) +# if (__GNUC__ >= 3) +# define GIT_FLEX_ARRAY /* empty */ +# else +# define GIT_FLEX_ARRAY 0 /* older GNU extension */ +# endif +# endif + +/* Default to safer but a bit wasteful traditional style */ +# ifndef GIT_FLEX_ARRAY +# define GIT_FLEX_ARRAY 1 +# endif +#endif + +#if defined(__GNUC__) +# define GIT_ALIGN(x,size) x __attribute__ ((aligned(size))) +#elif defined(_MSC_VER) +# define GIT_ALIGN(x,size) __declspec(align(size)) x +#else +# define GIT_ALIGN(x,size) x +#endif + +#if defined(__GNUC__) +# define GIT_UNUSED(x) \ + do { \ + __typeof__(x) _unused __attribute__((unused)); \ + _unused = (x); \ + } while (0) +# define GIT_UNUSED_ARG __attribute__((unused)) +# define GIT_UNUSED_FUNCTION __attribute__((unused)) +#else +# define GIT_UNUSED(x) ((void)(x)) +# define GIT_UNUSED_ARG +# define GIT_UNUSED_FUNCTION +#endif + +/* Define the printf format specifier to use for size_t output */ +#if defined(_MSC_VER) || defined(__MINGW32__) + +/* Visual Studio 2012 and prior lack PRId64 entirely */ +# ifndef PRId64 +# define PRId64 "I64d" +# endif + +/* The first block is needed to avoid warnings on MingW amd64 */ +# if (SIZE_MAX == ULLONG_MAX) +# define PRIuZ "I64u" +# define PRIxZ "I64x" +# define PRIXZ "I64X" +# define PRIdZ "I64d" +# else +# define PRIuZ "Iu" +# define PRIxZ "Ix" +# define PRIXZ "IX" +# define PRIdZ "Id" +# endif + +#else +# define PRIuZ "zu" +# define PRIxZ "zx" +# define PRIXZ "zX" +# define PRIdZ "zd" +#endif + +/* Microsoft Visual C/C++ */ +#if defined(_MSC_VER) +/* disable "deprecated function" warnings */ +# pragma warning ( disable : 4996 ) +/* disable "conditional expression is constant" level 4 warnings */ +# pragma warning ( disable : 4127 ) +#endif + +#if defined (_MSC_VER) + typedef unsigned char bool; +# ifndef true +# define true 1 +# endif +# ifndef false +# define false 0 +# endif +#else +# include +#endif + +#ifndef va_copy +# ifdef __va_copy +# define va_copy(dst, src) __va_copy(dst, src) +# else +# define va_copy(dst, src) ((dst) = (src)) +# endif +#endif + +#endif diff --git a/src/util/ctype_compat.h b/src/util/ctype_compat.h new file mode 100644 index 00000000000..462c8a17f17 --- /dev/null +++ b/src/util/ctype_compat.h @@ -0,0 +1,70 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_ctype_compat_h__ +#define INCLUDE_ctype_compat_h__ + +/* + * The Microsoft C runtime (MSVCRT) may take a heavy lock on the + * locale in order to figure out how the `ctype` functions work. + * This is deeply slow. Provide our own to avoid that. + */ + +#ifdef GIT_WIN32 + +GIT_INLINE(int) git__tolower(int c) +{ + return (c >= 'A' && c <= 'Z') ? (c + 32) : c; +} + +GIT_INLINE(int) git__toupper(int c) +{ + return (c >= 'a' && c <= 'z') ? (c - 32) : c; +} + +GIT_INLINE(bool) git__isalpha(int c) +{ + return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); +} + +GIT_INLINE(bool) git__isdigit(int c) +{ + return (c >= '0' && c <= '9'); +} + +GIT_INLINE(bool) git__isalnum(int c) +{ + return git__isalpha(c) || git__isdigit(c); +} + +GIT_INLINE(bool) git__isspace(int c) +{ + return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v'); +} + +GIT_INLINE(bool) git__isxdigit(int c) +{ + return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')); +} + +GIT_INLINE(bool) git__isprint(int c) +{ + return (c >= ' ' && c <= '~'); +} + +#else +# define git__tolower(a) tolower((unsigned char)(a)) +# define git__toupper(a) toupper((unsigned char)(a)) + +# define git__isalpha(a) (!!isalpha((unsigned char)(a))) +# define git__isdigit(a) (!!isdigit((unsigned char)(a))) +# define git__isalnum(a) (!!isalnum((unsigned char)(a))) +# define git__isspace(a) (!!isspace((unsigned char)(a))) +# define git__isxdigit(a) (!!isxdigit((unsigned char)(a))) +# define git__isprint(a) (!!isprint((unsigned char)(a))) +#endif + +#endif diff --git a/src/util/date.c b/src/util/date.c new file mode 100644 index 00000000000..0ff5287aef7 --- /dev/null +++ b/src/util/date.c @@ -0,0 +1,907 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2_util.h" + +#ifndef GIT_WIN32 +#include +#endif + +#include "util.h" +#include "posix.h" +#include "date.h" + +#include +#include + +typedef enum { + DATE_NORMAL = 0, + DATE_RELATIVE, + DATE_SHORT, + DATE_LOCAL, + DATE_ISO8601, + DATE_RFC2822, + DATE_RAW +} date_mode; + +/* + * This is like mktime, but without normalization of tm_wday and tm_yday. + */ +static git_time_t tm_to_time_t(const struct tm *tm) +{ + static const int mdays[] = { + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 + }; + int year = tm->tm_year - 70; + int month = tm->tm_mon; + int day = tm->tm_mday; + + if (year < 0 || year > 129) /* algo only works for 1970-2099 */ + return -1; + if (month < 0 || month > 11) /* array bounds */ + return -1; + if (month < 2 || (year + 2) % 4) + day--; + if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0) + return -1; + return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + + tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; +} + +static const char *month_names[] = { + "January", "February", "March", "April", "May", "June", + "July", "August", "September", "October", "November", "December" +}; + +static const char *weekday_names[] = { + "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays" +}; + + + +/* + * Check these. And note how it doesn't do the summer-time conversion. + * + * In my world, it's always summer, and things are probably a bit off + * in other ways too. + */ +static const struct { + const char *name; + int offset; + int dst; +} timezone_names[] = { + { "IDLW", -12, 0, }, /* International Date Line West */ + { "NT", -11, 0, }, /* Nome */ + { "CAT", -10, 0, }, /* Central Alaska */ + { "HST", -10, 0, }, /* Hawaii Standard */ + { "HDT", -10, 1, }, /* Hawaii Daylight */ + { "YST", -9, 0, }, /* Yukon Standard */ + { "YDT", -9, 1, }, /* Yukon Daylight */ + { "PST", -8, 0, }, /* Pacific Standard */ + { "PDT", -8, 1, }, /* Pacific Daylight */ + { "MST", -7, 0, }, /* Mountain Standard */ + { "MDT", -7, 1, }, /* Mountain Daylight */ + { "CST", -6, 0, }, /* Central Standard */ + { "CDT", -6, 1, }, /* Central Daylight */ + { "EST", -5, 0, }, /* Eastern Standard */ + { "EDT", -5, 1, }, /* Eastern Daylight */ + { "AST", -3, 0, }, /* Atlantic Standard */ + { "ADT", -3, 1, }, /* Atlantic Daylight */ + { "WAT", -1, 0, }, /* West Africa */ + + { "GMT", 0, 0, }, /* Greenwich Mean */ + { "UTC", 0, 0, }, /* Universal (Coordinated) */ + { "Z", 0, 0, }, /* Zulu, alias for UTC */ + + { "WET", 0, 0, }, /* Western European */ + { "BST", 0, 1, }, /* British Summer */ + { "CET", +1, 0, }, /* Central European */ + { "MET", +1, 0, }, /* Middle European */ + { "MEWT", +1, 0, }, /* Middle European Winter */ + { "MEST", +1, 1, }, /* Middle European Summer */ + { "CEST", +1, 1, }, /* Central European Summer */ + { "MESZ", +1, 1, }, /* Middle European Summer */ + { "FWT", +1, 0, }, /* French Winter */ + { "FST", +1, 1, }, /* French Summer */ + { "EET", +2, 0, }, /* Eastern Europe */ + { "EEST", +2, 1, }, /* Eastern European Daylight */ + { "WAST", +7, 0, }, /* West Australian Standard */ + { "WADT", +7, 1, }, /* West Australian Daylight */ + { "CCT", +8, 0, }, /* China Coast */ + { "JST", +9, 0, }, /* Japan Standard */ + { "EAST", +10, 0, }, /* Eastern Australian Standard */ + { "EADT", +10, 1, }, /* Eastern Australian Daylight */ + { "GST", +10, 0, }, /* Guam Standard */ + { "NZT", +12, 0, }, /* New Zealand */ + { "NZST", +12, 0, }, /* New Zealand Standard */ + { "NZDT", +12, 1, }, /* New Zealand Daylight */ + { "IDLE", +12, 0, }, /* International Date Line East */ +}; + +static size_t match_string(const char *date, const char *str) +{ + size_t i = 0; + + for (i = 0; *date; date++, str++, i++) { + if (*date == *str) + continue; + if (git__toupper(*date) == git__toupper(*str)) + continue; + if (!git__isalnum(*date)) + break; + return 0; + } + return i; +} + +static int skip_alpha(const char *date) +{ + int i = 0; + do { + i++; + } while (git__isalpha(date[i])); + return i; +} + +/* +* Parse month, weekday, or timezone name +*/ +static size_t match_alpha(const char *date, struct tm *tm, int *offset) +{ + unsigned int i; + + for (i = 0; i < 12; i++) { + size_t match = match_string(date, month_names[i]); + if (match >= 3) { + tm->tm_mon = i; + return match; + } + } + + for (i = 0; i < 7; i++) { + size_t match = match_string(date, weekday_names[i]); + if (match >= 3) { + tm->tm_wday = i; + return match; + } + } + + for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { + size_t match = match_string(date, timezone_names[i].name); + if (match >= 3 || match == strlen(timezone_names[i].name)) { + int off = timezone_names[i].offset; + + /* This is bogus, but we like summer */ + off += timezone_names[i].dst; + + /* Only use the tz name offset if we don't have anything better */ + if (*offset == -1) + *offset = 60*off; + + return match; + } + } + + if (match_string(date, "PM") == 2) { + tm->tm_hour = (tm->tm_hour % 12) + 12; + return 2; + } + + if (match_string(date, "AM") == 2) { + tm->tm_hour = (tm->tm_hour % 12) + 0; + return 2; + } + + /* BAD */ + return skip_alpha(date); +} + +static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) +{ + if (month > 0 && month < 13 && day > 0 && day < 32) { + struct tm check = *tm; + struct tm *r = (now_tm ? &check : tm); + git_time_t specified; + + r->tm_mon = month - 1; + r->tm_mday = day; + if (year == -1) { + if (!now_tm) + return 1; + r->tm_year = now_tm->tm_year; + } + else if (year >= 1970 && year < 2100) + r->tm_year = year - 1900; + else if (year > 70 && year < 100) + r->tm_year = year; + else if (year < 38) + r->tm_year = year + 100; + else + return 0; + if (!now_tm) + return 1; + + specified = tm_to_time_t(r); + + /* Be it commit time or author time, it does not make + * sense to specify timestamp way into the future. Make + * sure it is not later than ten days from now... + */ + if (now + 10*24*3600 < specified) + return 0; + tm->tm_mon = r->tm_mon; + tm->tm_mday = r->tm_mday; + if (year != -1) + tm->tm_year = r->tm_year; + return 1; + } + return 0; +} + +static size_t match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm) +{ + time_t now; + struct tm now_tm; + struct tm *refuse_future; + long num2, num3; + + num2 = strtol(end+1, &end, 10); + num3 = -1; + if (*end == c && git__isdigit(end[1])) + num3 = strtol(end+1, &end, 10); + + /* Time? Date? */ + switch (c) { + case ':': + if (num3 < 0) + num3 = 0; + if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { + tm->tm_hour = num; + tm->tm_min = num2; + tm->tm_sec = num3; + break; + } + return 0; + + case '-': + case '/': + case '.': + now = time(NULL); + refuse_future = NULL; + if (p_gmtime_r(&now, &now_tm)) + refuse_future = &now_tm; + + if (num > 70) { + /* yyyy-mm-dd? */ + if (is_date(num, num2, num3, refuse_future, now, tm)) + break; + /* yyyy-dd-mm? */ + if (is_date(num, num3, num2, refuse_future, now, tm)) + break; + } + /* Our eastern European friends say dd.mm.yy[yy] + * is the norm there, so giving precedence to + * mm/dd/yy[yy] form only when separator is not '.' + */ + if (c != '.' && + is_date(num3, num, num2, refuse_future, now, tm)) + break; + /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ + if (is_date(num3, num2, num, refuse_future, now, tm)) + break; + /* Funny European mm.dd.yy */ + if (c == '.' && + is_date(num3, num, num2, refuse_future, now, tm)) + break; + return 0; + } + return end - date; +} + +/* + * Have we filled in any part of the time/date yet? + * We just do a binary 'and' to see if the sign bit + * is set in all the values. + */ +static int nodate(struct tm *tm) +{ + return (tm->tm_year & + tm->tm_mon & + tm->tm_mday & + tm->tm_hour & + tm->tm_min & + tm->tm_sec) < 0; +} + +/* + * We've seen a digit. Time? Year? Date? + */ +static size_t match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt) +{ + size_t n; + char *end; + unsigned long num; + + num = strtoul(date, &end, 10); + + /* + * Seconds since 1970? We trigger on that for any numbers with + * more than 8 digits. This is because we don't want to rule out + * numbers like 20070606 as a YYYYMMDD date. + */ + if (num >= 100000000 && nodate(tm)) { + time_t time = num; + if (p_gmtime_r(&time, tm)) { + *tm_gmt = 1; + return end - date; + } + } + + /* + * Check for special formats: num[-.:/]num[same]num + */ + switch (*end) { + case ':': + case '.': + case '/': + case '-': + if (git__isdigit(end[1])) { + size_t match = match_multi_number(num, *end, date, end, tm); + if (match) + return match; + } + } + + /* + * None of the special formats? Try to guess what + * the number meant. We use the number of digits + * to make a more educated guess.. + */ + n = 0; + do { + n++; + } while (git__isdigit(date[n])); + + /* Four-digit year or a timezone? */ + if (n == 4) { + if (num <= 1400 && *offset == -1) { + unsigned int minutes = num % 100; + unsigned int hours = num / 100; + *offset = hours*60 + minutes; + } else if (num > 1900 && num < 2100) + tm->tm_year = num - 1900; + return n; + } + + /* + * Ignore lots of numerals. We took care of 4-digit years above. + * Days or months must be one or two digits. + */ + if (n > 2) + return n; + + /* + * NOTE! We will give precedence to day-of-month over month or + * year numbers in the 1-12 range. So 05 is always "mday 5", + * unless we already have a mday.. + * + * IOW, 01 Apr 05 parses as "April 1st, 2005". + */ + if (num > 0 && num < 32 && tm->tm_mday < 0) { + tm->tm_mday = num; + return n; + } + + /* Two-digit year? */ + if (n == 2 && tm->tm_year < 0) { + if (num < 10 && tm->tm_mday >= 0) { + tm->tm_year = num + 100; + return n; + } + if (num >= 70) { + tm->tm_year = num; + return n; + } + } + + if (num > 0 && num < 13 && tm->tm_mon < 0) + tm->tm_mon = num-1; + + return n; +} + +static size_t match_tz(const char *date, int *offp) +{ + char *end; + int hour = strtoul(date + 1, &end, 10); + size_t n = end - (date + 1); + int min = 0; + + if (n == 4) { + /* hhmm */ + min = hour % 100; + hour = hour / 100; + } else if (n != 2) { + min = 99; /* random stuff */ + } else if (*end == ':') { + /* hh:mm? */ + min = strtoul(end + 1, &end, 10); + if (end - (date + 1) != 5) + min = 99; /* random stuff */ + } /* otherwise we parsed "hh" */ + + /* + * Don't accept any random stuff. Even though some places have + * offset larger than 12 hours (e.g. Pacific/Kiritimati is at + * UTC+14), there is something wrong if hour part is much + * larger than that. We might also want to check that the + * minutes are divisible by 15 or something too. (Offset of + * Kathmandu, Nepal is UTC+5:45) + */ + if (min < 60 && hour < 24) { + int offset = hour * 60 + min; + if (*date == '-') + offset = -offset; + *offp = offset; + } + return end - date; +} + +/* + * Parse a string like "0 +0000" as ancient timestamp near epoch, but + * only when it appears not as part of any other string. + */ +static int match_object_header_date(const char *date, git_time_t *timestamp, int *offset) +{ + char *end; + unsigned long stamp; + int ofs; + + if (*date < '0' || '9' <= *date) + return -1; + stamp = strtoul(date, &end, 10); + if (*end != ' ' || stamp == ULONG_MAX || (end[1] != '+' && end[1] != '-')) + return -1; + date = end + 2; + ofs = strtol(date, &end, 10); + if ((*end != '\0' && (*end != '\n')) || end != date + 4) + return -1; + ofs = (ofs / 100) * 60 + (ofs % 100); + if (date[-1] == '-') + ofs = -ofs; + *timestamp = stamp; + *offset = ofs; + return 0; +} + +/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 + (i.e. English) day/month names, and it doesn't work correctly with %z. */ +static int parse_date_basic(const char *date, git_time_t *timestamp, int *offset) +{ + struct tm tm; + int tm_gmt; + git_time_t dummy_timestamp; + int dummy_offset; + + if (!timestamp) + timestamp = &dummy_timestamp; + if (!offset) + offset = &dummy_offset; + + memset(&tm, 0, sizeof(tm)); + tm.tm_year = -1; + tm.tm_mon = -1; + tm.tm_mday = -1; + tm.tm_isdst = -1; + tm.tm_hour = -1; + tm.tm_min = -1; + tm.tm_sec = -1; + *offset = -1; + tm_gmt = 0; + + if (*date == '@' && + !match_object_header_date(date + 1, timestamp, offset)) + return 0; /* success */ + for (;;) { + size_t match = 0; + unsigned char c = *date; + + /* Stop at end of string or newline */ + if (!c || c == '\n') + break; + + if (git__isalpha(c)) + match = match_alpha(date, &tm, offset); + else if (git__isdigit(c)) + match = match_digit(date, &tm, offset, &tm_gmt); + else if ((c == '-' || c == '+') && git__isdigit(date[1])) + match = match_tz(date, offset); + + if (!match) { + /* BAD */ + match = 1; + } + + date += match; + } + + /* mktime uses local timezone */ + *timestamp = tm_to_time_t(&tm); + if (*offset == -1) + *offset = (int)((time_t)*timestamp - mktime(&tm)) / 60; + + if (*timestamp == (git_time_t)-1) + return -1; + + if (!tm_gmt) + *timestamp -= *offset * 60; + return 0; /* success */ +} + + +/* + * Relative time update (eg "2 days ago"). If we haven't set the time + * yet, we need to set it from current time. + */ +static git_time_t update_tm(struct tm *tm, struct tm *now, unsigned long sec) +{ + time_t n; + + if (tm->tm_mday < 0) + tm->tm_mday = now->tm_mday; + if (tm->tm_mon < 0) + tm->tm_mon = now->tm_mon; + if (tm->tm_year < 0) { + tm->tm_year = now->tm_year; + if (tm->tm_mon > now->tm_mon) + tm->tm_year--; + } + + n = mktime(tm) - sec; + p_localtime_r(&n, tm); + return n; +} + +static void date_now(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + update_tm(tm, now, 0); +} + +static void date_yesterday(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + update_tm(tm, now, 24*60*60); +} + +static void date_time(struct tm *tm, struct tm *now, int hour) +{ + if (tm->tm_hour < hour) + date_yesterday(tm, now, NULL); + tm->tm_hour = hour; + tm->tm_min = 0; + tm->tm_sec = 0; +} + +static void date_midnight(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + date_time(tm, now, 0); +} + +static void date_noon(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + date_time(tm, now, 12); +} + +static void date_tea(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + date_time(tm, now, 17); +} + +static void date_pm(struct tm *tm, struct tm *now, int *num) +{ + int hour, n = *num; + *num = 0; + GIT_UNUSED(now); + + hour = tm->tm_hour; + if (n) { + hour = n; + tm->tm_min = 0; + tm->tm_sec = 0; + } + tm->tm_hour = (hour % 12) + 12; +} + +static void date_am(struct tm *tm, struct tm *now, int *num) +{ + int hour, n = *num; + *num = 0; + GIT_UNUSED(now); + + hour = tm->tm_hour; + if (n) { + hour = n; + tm->tm_min = 0; + tm->tm_sec = 0; + } + tm->tm_hour = (hour % 12); +} + +static void date_never(struct tm *tm, struct tm *now, int *num) +{ + time_t n = 0; + GIT_UNUSED(now); + GIT_UNUSED(num); + p_localtime_r(&n, tm); +} + +static const struct special { + const char *name; + void (*fn)(struct tm *, struct tm *, int *); +} special[] = { + { "yesterday", date_yesterday }, + { "noon", date_noon }, + { "midnight", date_midnight }, + { "tea", date_tea }, + { "PM", date_pm }, + { "AM", date_am }, + { "never", date_never }, + { "now", date_now }, + { NULL } +}; + +static const char *number_name[] = { + "zero", "one", "two", "three", "four", + "five", "six", "seven", "eight", "nine", "ten", +}; + +static const struct typelen { + const char *type; + int length; +} typelen[] = { + { "seconds", 1 }, + { "minutes", 60 }, + { "hours", 60*60 }, + { "days", 24*60*60 }, + { "weeks", 7*24*60*60 }, + { NULL } +}; + +static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched) +{ + const struct typelen *tl; + const struct special *s; + const char *end = date; + int i; + + while (git__isalpha(*++end)) + /* scan to non-alpha */; + + for (i = 0; i < 12; i++) { + size_t match = match_string(date, month_names[i]); + if (match >= 3) { + tm->tm_mon = i; + *touched = 1; + return end; + } + } + + for (s = special; s->name; s++) { + size_t len = strlen(s->name); + if (match_string(date, s->name) == len) { + s->fn(tm, now, num); + *touched = 1; + return end; + } + } + + if (!*num) { + for (i = 1; i < 11; i++) { + size_t len = strlen(number_name[i]); + if (match_string(date, number_name[i]) == len) { + *num = i; + *touched = 1; + return end; + } + } + if (match_string(date, "last") == 4) { + *num = 1; + *touched = 1; + } + return end; + } + + tl = typelen; + while (tl->type) { + size_t len = strlen(tl->type); + if (match_string(date, tl->type) >= len-1) { + update_tm(tm, now, tl->length * (unsigned long)*num); + *num = 0; + *touched = 1; + return end; + } + tl++; + } + + for (i = 0; i < 7; i++) { + size_t match = match_string(date, weekday_names[i]); + if (match >= 3) { + int diff, n = *num -1; + *num = 0; + + diff = tm->tm_wday - i; + if (diff <= 0) + n++; + diff += 7*n; + + update_tm(tm, now, diff * 24 * 60 * 60); + *touched = 1; + return end; + } + } + + if (match_string(date, "months") >= 5) { + int n; + update_tm(tm, now, 0); /* fill in date fields if needed */ + n = tm->tm_mon - *num; + *num = 0; + while (n < 0) { + n += 12; + tm->tm_year--; + } + tm->tm_mon = n; + *touched = 1; + return end; + } + + if (match_string(date, "years") >= 4) { + update_tm(tm, now, 0); /* fill in date fields if needed */ + tm->tm_year -= *num; + *num = 0; + *touched = 1; + return end; + } + + return end; +} + +static const char *approxidate_digit(const char *date, struct tm *tm, int *num) +{ + char *end; + unsigned long number = strtoul(date, &end, 10); + + switch (*end) { + case ':': + case '.': + case '/': + case '-': + if (git__isdigit(end[1])) { + size_t match = match_multi_number(number, *end, date, end, tm); + if (match) + return date + match; + } + } + + /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */ + if (date[0] != '0' || end - date <= 2) + *num = number; + return end; +} + +/* + * Do we have a pending number at the end, or when + * we see a new one? Let's assume it's a month day, + * as in "Dec 6, 1992" + */ +static void pending_number(struct tm *tm, int *num) +{ + int number = *num; + + if (number) { + *num = 0; + if (tm->tm_mday < 0 && number < 32) + tm->tm_mday = number; + else if (tm->tm_mon < 0 && number < 13) + tm->tm_mon = number-1; + else if (tm->tm_year < 0) { + if (number > 1969 && number < 2100) + tm->tm_year = number - 1900; + else if (number > 69 && number < 100) + tm->tm_year = number; + else if (number < 38) + tm->tm_year = 100 + number; + /* We mess up for number = 00 ? */ + } + } +} + +static git_time_t approxidate_str(const char *date, + time_t time_sec, + int *error_ret) +{ + int number = 0; + int touched = 0; + struct tm tm = {0}, now; + + p_localtime_r(&time_sec, &tm); + now = tm; + + tm.tm_year = -1; + tm.tm_mon = -1; + tm.tm_mday = -1; + + for (;;) { + unsigned char c = *date; + if (!c) + break; + date++; + if (git__isdigit(c)) { + pending_number(&tm, &number); + date = approxidate_digit(date-1, &tm, &number); + touched = 1; + continue; + } + if (git__isalpha(c)) + date = approxidate_alpha(date-1, &tm, &now, &number, &touched); + } + pending_number(&tm, &number); + if (!touched) + *error_ret = -1; + return update_tm(&tm, &now, 0); +} + +int git_date_offset_parse(git_time_t *out, int *out_offset, const char *date) +{ + time_t time_sec; + git_time_t timestamp; + int offset, error_ret=0; + + if (!parse_date_basic(date, ×tamp, &offset)) { + *out = timestamp; + *out_offset = offset; + return 0; + } + + if (time(&time_sec) == -1) + return -1; + + *out = approxidate_str(date, time_sec, &error_ret); + return error_ret; +} + +int git_date_parse(git_time_t *out, const char *date) +{ + int offset; + + return git_date_offset_parse(out, &offset, date); +} + +int git_date_rfc2822_fmt(git_str *out, git_time_t time, int offset) +{ + time_t t; + struct tm gmt; + + GIT_ASSERT_ARG(out); + + t = (time_t) (time + offset * 60); + + if (p_gmtime_r(&t, &gmt) == NULL) + return -1; + + return git_str_printf(out, "%.3s, %u %.3s %.4u %02u:%02u:%02u %+03d%02d", + weekday_names[gmt.tm_wday], + gmt.tm_mday, + month_names[gmt.tm_mon], + gmt.tm_year + 1900, + gmt.tm_hour, gmt.tm_min, gmt.tm_sec, + offset / 60, offset % 60); +} + diff --git a/src/util/date.h b/src/util/date.h new file mode 100644 index 00000000000..785fc064bf5 --- /dev/null +++ b/src/util/date.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_date_h__ +#define INCLUDE_date_h__ + +#include "util.h" +#include "str.h" + +/* + * Parse a string into a value as a git_time_t with a timezone offset. + * + * Sample valid input: + * - "yesterday" + * - "July 17, 2003" + * - "2003-7-17 08:23i+03" + */ +extern int git_date_offset_parse(git_time_t *out, int *out_offset, const char *date); + +/* + * Parse a string into a value as a git_time_t. + * + * Timezone offset is ignored. + * + * Sample valid input: + * - "yesterday" + * - "July 17, 2003" + * - "2003-7-17 08:23" + */ +extern int git_date_parse(git_time_t *out, const char *date); + +/* + * Format a git_time as a RFC2822 string + * + * @param out buffer to store formatted date + * @param time the time to be formatted + * @param offset the timezone offset + * @return 0 if successful; -1 on error + */ +extern int git_date_rfc2822_fmt(git_str *out, git_time_t time, int offset); + +#endif diff --git a/src/util/errors.c b/src/util/errors.c new file mode 100644 index 00000000000..feed6a835f5 --- /dev/null +++ b/src/util/errors.c @@ -0,0 +1,401 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2_util.h" + +#include "errors.h" +#include "posix.h" +#include "str.h" +#include "runtime.h" + +/* + * Some static error data that is used when we're out of memory, TLS + * has not been setup, or TLS has failed. + */ + +static git_error oom_error = { + "Out of memory", + GIT_ERROR_NOMEMORY +}; + +static git_error uninitialized_error = { + "library has not been initialized", + GIT_ERROR_INVALID +}; + +static git_error tlsdata_error = { + "thread-local data initialization failure", + GIT_ERROR_THREAD +}; + +static git_error no_error = { + "no error", + GIT_ERROR_NONE +}; + +#define IS_STATIC_ERROR(err) \ + ((err) == &oom_error || (err) == &uninitialized_error || \ + (err) == &tlsdata_error || (err) == &no_error) + +/* Per-thread error state (TLS) */ + +static git_tlsdata_key tls_key; + +struct error_threadstate { + /* The error message buffer. */ + git_str message; + + /* Error information, set by `git_error_set` and friends. */ + git_error error; + + /* + * The last error to occur; points to the error member of this + * struct _or_ a static error. + */ + git_error *last; +}; + +static void threadstate_dispose(struct error_threadstate *threadstate) +{ + if (!threadstate) + return; + + git_str_dispose(&threadstate->message); +} + +static struct error_threadstate *threadstate_get(void) +{ + struct error_threadstate *threadstate; + + if ((threadstate = git_tlsdata_get(tls_key)) != NULL) + return threadstate; + + /* + * Avoid git__malloc here, since if it fails, it sets an error + * message, which requires thread state, which would allocate + * here, which would fail, which would set an error message... + */ + + if ((threadstate = git__allocator.gmalloc( + sizeof(struct error_threadstate), + __FILE__, __LINE__)) == NULL) + return NULL; + + memset(threadstate, 0, sizeof(struct error_threadstate)); + + if (git_str_init(&threadstate->message, 0) < 0) { + git__allocator.gfree(threadstate); + return NULL; + } + + git_tlsdata_set(tls_key, threadstate); + return threadstate; +} + +static void GIT_SYSTEM_CALL threadstate_free(void *threadstate) +{ + threadstate_dispose(threadstate); + git__free(threadstate); +} + +static void git_error_global_shutdown(void) +{ + struct error_threadstate *threadstate; + + threadstate = git_tlsdata_get(tls_key); + git_tlsdata_set(tls_key, NULL); + + threadstate_dispose(threadstate); + git__free(threadstate); + + git_tlsdata_dispose(tls_key); +} + +int git_error_global_init(void) +{ + if (git_tlsdata_init(&tls_key, &threadstate_free) != 0) + return -1; + + return git_runtime_shutdown_register(git_error_global_shutdown); +} + +static void set_error_from_buffer(int error_class) +{ + struct error_threadstate *threadstate = threadstate_get(); + git_error *error; + git_str *buf; + + if (!threadstate) + return; + + error = &threadstate->error; + buf = &threadstate->message; + + error->message = buf->ptr; + error->klass = error_class; + + threadstate->last = error; +} + +static void set_error(int error_class, char *string) +{ + struct error_threadstate *threadstate = threadstate_get(); + git_str *buf; + + if (!threadstate) + return; + + buf = &threadstate->message; + + git_str_clear(buf); + + if (string) + git_str_puts(buf, string); + + if (!git_str_oom(buf)) + set_error_from_buffer(error_class); +} + +void git_error_set_oom(void) +{ + struct error_threadstate *threadstate = threadstate_get(); + + if (!threadstate) + return; + + threadstate->last = &oom_error; +} + +void git_error_set(int error_class, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + git_error_vset(error_class, fmt, ap); + va_end(ap); +} + +void git_error_vset(int error_class, const char *fmt, va_list ap) +{ +#ifdef GIT_WIN32 + DWORD win32_error_code = (error_class == GIT_ERROR_OS) ? GetLastError() : 0; +#endif + + struct error_threadstate *threadstate = threadstate_get(); + int error_code = (error_class == GIT_ERROR_OS) ? errno : 0; + git_str *buf; + + if (!threadstate) + return; + + buf = &threadstate->message; + + git_str_clear(buf); + + if (fmt) { + git_str_vprintf(buf, fmt, ap); + if (error_class == GIT_ERROR_OS) + git_str_PUTS(buf, ": "); + } + + if (error_class == GIT_ERROR_OS) { +#ifdef GIT_WIN32 + char *win32_error = git_win32_get_error_message(win32_error_code); + if (win32_error) { + git_str_puts(buf, win32_error); + git__free(win32_error); + + SetLastError(0); + } + else +#endif + if (error_code) + git_str_puts(buf, strerror(error_code)); + + if (error_code) + errno = 0; + } + + if (!git_str_oom(buf)) + set_error_from_buffer(error_class); +} + +int git_error_set_str(int error_class, const char *string) +{ + struct error_threadstate *threadstate = threadstate_get(); + git_str *buf; + + GIT_ASSERT_ARG(string); + + if (!threadstate) + return -1; + + buf = &threadstate->message; + + git_str_clear(buf); + git_str_puts(buf, string); + + if (git_str_oom(buf)) + return -1; + + set_error_from_buffer(error_class); + return 0; +} + +void git_error_clear(void) +{ + struct error_threadstate *threadstate = threadstate_get(); + + if (!threadstate) + return; + + if (threadstate->last != NULL) { + set_error(0, NULL); + threadstate->last = NULL; + } + + errno = 0; +#ifdef GIT_WIN32 + SetLastError(0); +#endif +} + +bool git_error_exists(void) +{ + struct error_threadstate *threadstate; + + if ((threadstate = threadstate_get()) == NULL) + return true; + + return threadstate->last != NULL; +} + +const git_error *git_error_last(void) +{ + struct error_threadstate *threadstate; + + /* If the library is not initialized, return a static error. */ + if (!git_runtime_init_count()) + return &uninitialized_error; + + if ((threadstate = threadstate_get()) == NULL) + return &tlsdata_error; + + if (!threadstate->last) + return &no_error; + + return threadstate->last; +} + +int git_error_save(git_error **out) +{ + struct error_threadstate *threadstate = threadstate_get(); + git_error *error, *dup; + + if (!threadstate) { + *out = &tlsdata_error; + return -1; + } + + error = threadstate->last; + + if (!error || error == &no_error) { + *out = &no_error; + return 0; + } else if (IS_STATIC_ERROR(error)) { + *out = error; + return 0; + } + + if ((dup = git__malloc(sizeof(git_error))) == NULL) { + *out = &oom_error; + return -1; + } + + dup->klass = error->klass; + dup->message = git__strdup(error->message); + + if (!dup->message) { + *out = &oom_error; + return -1; + } + + *out = dup; + return 0; +} + +int git_error_restore(git_error *error) +{ + struct error_threadstate *threadstate = threadstate_get(); + + GIT_ASSERT_ARG(error); + + if (IS_STATIC_ERROR(error) && threadstate) + threadstate->last = error; + else + set_error(error->klass, error->message); + + git_error_free(error); + return 0; +} + +void git_error_free(git_error *error) +{ + if (!error) + return; + + if (IS_STATIC_ERROR(error)) + return; + + git__free(error->message); + git__free(error); +} + +int git_error_system_last(void) +{ +#ifdef GIT_WIN32 + return GetLastError(); +#else + return errno; +#endif +} + +void git_error_system_set(int code) +{ +#ifdef GIT_WIN32 + SetLastError(code); +#else + errno = code; +#endif +} + +/* Deprecated error values and functions */ + +#ifndef GIT_DEPRECATE_HARD + +#include "git2/deprecated.h" + +const git_error *giterr_last(void) +{ + return git_error_last(); +} + +void giterr_clear(void) +{ + git_error_clear(); +} + +void giterr_set_str(int error_class, const char *string) +{ + git_error_set_str(error_class, string); +} + +void giterr_set_oom(void) +{ + git_error_set_oom(); +} +#endif diff --git a/src/util/errors.h b/src/util/errors.h new file mode 100644 index 00000000000..8d5877550b1 --- /dev/null +++ b/src/util/errors.h @@ -0,0 +1,84 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_errors_h__ +#define INCLUDE_errors_h__ + +#include "git2_util.h" +#include "git2/sys/errors.h" + +/* Initialize the error thread-state. */ +int git_error_global_init(void); + +/* + * `vprintf`-style formatting for the error message for this thread. + */ +void git_error_vset(int error_class, const char *fmt, va_list ap); + +/** + * Determines whether an error exists. + */ +bool git_error_exists(void); + +/** + * Set error message for user callback if needed. + * + * If the error code in non-zero and no error message is set, this + * sets a generic error message. + * + * @return This always returns the `error_code` parameter. + */ +GIT_INLINE(int) git_error_set_after_callback_function( + int error_code, const char *action) +{ + if (error_code) { + if (!git_error_exists()) + git_error_set(GIT_ERROR_CALLBACK, + "%s callback returned %d", action, error_code); + } + return error_code; +} + +#ifdef GIT_WIN32 +#define git_error_set_after_callback(code) \ + git_error_set_after_callback_function((code), __FUNCTION__) +#else +#define git_error_set_after_callback(code) \ + git_error_set_after_callback_function((code), __func__) +#endif + +/** + * Gets the system error code for this thread. + */ +int git_error_system_last(void); + +/** + * Sets the system error code for this thread. + */ +void git_error_system_set(int code); + +/** + * Capture current error state to restore later, returning error code. + * If `error_code` is zero, this does not clear the current error state. + * You must either restore this error state, or free it. + * + * This function returns 0 on success, or -1 on failure. If the function + * fails, the `out` structure is set to the failure error message and + * the normal system error message is not updated. + */ +extern int git_error_save(git_error **out); + +/** + * Restore thread error state to the given value. The given value is + * freed and `git_error_free` need not be called on it. + */ +extern int git_error_restore(git_error *error); + +/** Free an error state. */ +extern void git_error_free(git_error *error); + +#endif diff --git a/src/util/filebuf.c b/src/util/filebuf.c new file mode 100644 index 00000000000..7afb76b88ec --- /dev/null +++ b/src/util/filebuf.c @@ -0,0 +1,600 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "filebuf.h" + +#include "futils.h" + +static const size_t WRITE_BUFFER_SIZE = (4096 * 2); + +enum buferr_t { + BUFERR_OK = 0, + BUFERR_WRITE, + BUFERR_ZLIB, + BUFERR_MEM +}; + +#define ENSURE_BUF_OK(buf) if ((buf)->last_error != BUFERR_OK) { return -1; } + +static int verify_last_error(git_filebuf *file) +{ + switch (file->last_error) { + case BUFERR_WRITE: + git_error_set(GIT_ERROR_OS, "failed to write out file"); + return -1; + + case BUFERR_MEM: + git_error_set_oom(); + return -1; + + case BUFERR_ZLIB: + git_error_set(GIT_ERROR_ZLIB, + "Buffer error when writing out ZLib data"); + return -1; + + default: + return 0; + } +} + +static int lock_file(git_filebuf *file, int flags, mode_t mode) +{ + if (git_fs_path_exists(file->path_lock) == true) { + git_error_clear(); /* actual OS error code just confuses */ + git_error_set(GIT_ERROR_OS, + "failed to lock file '%s' for writing", file->path_lock); + return GIT_ELOCKED; + } + + /* create path to the file buffer is required */ + if (flags & GIT_FILEBUF_CREATE_LEADING_DIRS) { + /* XXX: Should dirmode here be configurable? Or is 0777 always fine? */ + file->fd = git_futils_creat_locked_withpath(file->path_lock, 0777, mode); + } else { + file->fd = git_futils_creat_locked(file->path_lock, mode); + } + + if (file->fd < 0) + return file->fd; + + file->fd_is_open = true; + + if ((flags & GIT_FILEBUF_APPEND) && git_fs_path_exists(file->path_original) == true) { + git_file source; + char buffer[GIT_BUFSIZE_FILEIO]; + ssize_t read_bytes; + int error = 0; + + source = p_open(file->path_original, O_RDONLY); + if (source < 0) { + git_error_set(GIT_ERROR_OS, + "failed to open file '%s' for reading", + file->path_original); + return -1; + } + + while ((read_bytes = p_read(source, buffer, sizeof(buffer))) > 0) { + if ((error = p_write(file->fd, buffer, read_bytes)) < 0) + break; + if (file->compute_digest) + git_hash_update(&file->digest, buffer, read_bytes); + } + + p_close(source); + + if (read_bytes < 0) { + git_error_set(GIT_ERROR_OS, "failed to read file '%s'", file->path_original); + return -1; + } else if (error < 0) { + git_error_set(GIT_ERROR_OS, "failed to write file '%s'", file->path_lock); + return -1; + } + } + + return 0; +} + +void git_filebuf_cleanup(git_filebuf *file) +{ + if (file->fd_is_open && file->fd >= 0) + p_close(file->fd); + + if (file->created_lock && !file->did_rename && file->path_lock && git_fs_path_exists(file->path_lock)) + p_unlink(file->path_lock); + + if (file->compute_digest) { + git_hash_ctx_cleanup(&file->digest); + file->compute_digest = 0; + } + + if (file->buffer) + git__free(file->buffer); + + /* use the presence of z_buf to decide if we need to deflateEnd */ + if (file->z_buf) { + git__free(file->z_buf); + deflateEnd(&file->zs); + } + + if (file->path_original) + git__free(file->path_original); + if (file->path_lock) + git__free(file->path_lock); + + memset(file, 0x0, sizeof(git_filebuf)); + file->fd = -1; +} + +GIT_INLINE(int) flush_buffer(git_filebuf *file) +{ + int result = file->write(file, file->buffer, file->buf_pos); + file->buf_pos = 0; + return result; +} + +int git_filebuf_flush(git_filebuf *file) +{ + return flush_buffer(file); +} + +static int write_normal(git_filebuf *file, void *source, size_t len) +{ + if (len > 0) { + if (p_write(file->fd, (void *)source, len) < 0) { + file->last_error = BUFERR_WRITE; + return -1; + } + + if (file->compute_digest) + git_hash_update(&file->digest, source, len); + } + + return 0; +} + +static int write_deflate(git_filebuf *file, void *source, size_t len) +{ + z_stream *zs = &file->zs; + + if (len > 0 || file->flush_mode == Z_FINISH) { + zs->next_in = source; + zs->avail_in = (uInt)len; + + do { + size_t have; + + zs->next_out = file->z_buf; + zs->avail_out = (uInt)file->buf_size; + + if (deflate(zs, file->flush_mode) == Z_STREAM_ERROR) { + file->last_error = BUFERR_ZLIB; + return -1; + } + + have = file->buf_size - (size_t)zs->avail_out; + + if (p_write(file->fd, file->z_buf, have) < 0) { + file->last_error = BUFERR_WRITE; + return -1; + } + + } while (zs->avail_out == 0); + + GIT_ASSERT(zs->avail_in == 0); + + if (file->compute_digest) + git_hash_update(&file->digest, source, len); + } + + return 0; +} + +#define MAX_SYMLINK_DEPTH 5 + +static int resolve_symlink(git_str *out, const char *path) +{ + int i, error, root; + ssize_t ret; + struct stat st; + git_str curpath = GIT_STR_INIT, target = GIT_STR_INIT; + + if ((error = git_str_grow(&target, GIT_PATH_MAX + 1)) < 0 || + (error = git_str_puts(&curpath, path)) < 0) + return error; + + for (i = 0; i < MAX_SYMLINK_DEPTH; i++) { + error = p_lstat(curpath.ptr, &st); + if (error < 0 && errno == ENOENT) { + error = git_str_puts(out, curpath.ptr); + goto cleanup; + } + + if (error < 0) { + git_error_set(GIT_ERROR_OS, "failed to stat '%s'", curpath.ptr); + error = -1; + goto cleanup; + } + + if (!S_ISLNK(st.st_mode)) { + error = git_str_puts(out, curpath.ptr); + goto cleanup; + } + + ret = p_readlink(curpath.ptr, target.ptr, GIT_PATH_MAX); + if (ret < 0) { + git_error_set(GIT_ERROR_OS, "failed to read symlink '%s'", curpath.ptr); + error = -1; + goto cleanup; + } + + if (ret == GIT_PATH_MAX) { + git_error_set(GIT_ERROR_INVALID, "symlink target too long"); + error = -1; + goto cleanup; + } + + /* readlink(2) won't NUL-terminate for us */ + target.ptr[ret] = '\0'; + target.size = ret; + + root = git_fs_path_root(target.ptr); + if (root >= 0) { + if ((error = git_str_sets(&curpath, target.ptr)) < 0) + goto cleanup; + } else { + git_str dir = GIT_STR_INIT; + + if ((error = git_fs_path_dirname_r(&dir, curpath.ptr)) < 0) + goto cleanup; + + git_str_swap(&curpath, &dir); + git_str_dispose(&dir); + + if ((error = git_fs_path_apply_relative(&curpath, target.ptr)) < 0) + goto cleanup; + } + } + + git_error_set(GIT_ERROR_INVALID, "maximum symlink depth reached"); + error = -1; + +cleanup: + git_str_dispose(&curpath); + git_str_dispose(&target); + return error; +} + +int git_filebuf_open(git_filebuf *file, const char *path, int flags, mode_t mode) +{ + return git_filebuf_open_withsize(file, path, flags, mode, WRITE_BUFFER_SIZE); +} + +int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mode_t mode, size_t size) +{ + int compression, error = -1; + size_t path_len, alloc_len; + + GIT_ASSERT_ARG(file); + GIT_ASSERT_ARG(path); + GIT_ASSERT(file->buffer == NULL); + + memset(file, 0x0, sizeof(git_filebuf)); + + if (flags & GIT_FILEBUF_DO_NOT_BUFFER) + file->do_not_buffer = true; + + if (flags & GIT_FILEBUF_FSYNC) + file->do_fsync = true; + + file->buf_size = size; + file->buf_pos = 0; + file->fd = -1; + file->last_error = BUFERR_OK; + + /* Allocate the main cache buffer */ + if (!file->do_not_buffer) { + file->buffer = git__malloc(file->buf_size); + GIT_ERROR_CHECK_ALLOC(file->buffer); + } + + /* If we are hashing on-write, allocate a new hash context */ + if (flags & GIT_FILEBUF_HASH_SHA1) { + file->compute_digest = 1; + + if (git_hash_ctx_init(&file->digest, GIT_HASH_ALGORITHM_SHA1) < 0) + goto cleanup; + } else if (flags & GIT_FILEBUF_HASH_SHA256) { + file->compute_digest = 1; + + if (git_hash_ctx_init(&file->digest, GIT_HASH_ALGORITHM_SHA256) < 0) + goto cleanup; + } + + compression = flags >> GIT_FILEBUF_DEFLATE_SHIFT; + + /* If we are deflating on-write, */ + if (compression != 0) { + /* Initialize the ZLib stream */ + if (deflateInit(&file->zs, compression) != Z_OK) { + git_error_set(GIT_ERROR_ZLIB, "failed to initialize zlib"); + goto cleanup; + } + + /* Allocate the Zlib cache buffer */ + file->z_buf = git__malloc(file->buf_size); + GIT_ERROR_CHECK_ALLOC(file->z_buf); + + /* Never flush */ + file->flush_mode = Z_NO_FLUSH; + file->write = &write_deflate; + } else { + file->write = &write_normal; + } + + /* If we are writing to a temp file */ + if (flags & GIT_FILEBUF_TEMPORARY) { + git_str tmp_path = GIT_STR_INIT; + + /* Open the file as temporary for locking */ + file->fd = git_futils_mktmp(&tmp_path, path, mode); + + if (file->fd < 0) { + git_str_dispose(&tmp_path); + goto cleanup; + } + file->fd_is_open = true; + file->created_lock = true; + + /* No original path */ + file->path_original = NULL; + file->path_lock = git_str_detach(&tmp_path); + GIT_ERROR_CHECK_ALLOC(file->path_lock); + } else { + git_str resolved_path = GIT_STR_INIT; + + if ((error = resolve_symlink(&resolved_path, path)) < 0) + goto cleanup; + + /* Save the original path of the file */ + path_len = resolved_path.size; + file->path_original = git_str_detach(&resolved_path); + + /* create the locking path by appending ".lock" to the original */ + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, path_len, GIT_FILELOCK_EXTLENGTH); + file->path_lock = git__malloc(alloc_len); + GIT_ERROR_CHECK_ALLOC(file->path_lock); + + memcpy(file->path_lock, file->path_original, path_len); + memcpy(file->path_lock + path_len, GIT_FILELOCK_EXTENSION, GIT_FILELOCK_EXTLENGTH); + + if (git_fs_path_isdir(file->path_original)) { + git_error_set(GIT_ERROR_FILESYSTEM, "path '%s' is a directory", file->path_original); + error = GIT_EDIRECTORY; + goto cleanup; + } + + /* open the file for locking */ + if ((error = lock_file(file, flags, mode)) < 0) + goto cleanup; + + file->created_lock = true; + } + + return 0; + +cleanup: + git_filebuf_cleanup(file); + return error; +} + +int git_filebuf_hash(unsigned char *out, git_filebuf *file) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(file); + GIT_ASSERT_ARG(file->compute_digest); + + flush_buffer(file); + + if (verify_last_error(file) < 0) + return -1; + + git_hash_final(out, &file->digest); + git_hash_ctx_cleanup(&file->digest); + file->compute_digest = 0; + + return 0; +} + +int git_filebuf_commit_at(git_filebuf *file, const char *path) +{ + git__free(file->path_original); + file->path_original = git__strdup(path); + GIT_ERROR_CHECK_ALLOC(file->path_original); + + return git_filebuf_commit(file); +} + +int git_filebuf_commit(git_filebuf *file) +{ + /* temporary files cannot be committed */ + GIT_ASSERT_ARG(file); + GIT_ASSERT(file->path_original); + + file->flush_mode = Z_FINISH; + flush_buffer(file); + + if (verify_last_error(file) < 0) + goto on_error; + + file->fd_is_open = false; + + if (file->do_fsync && p_fsync(file->fd) < 0) { + git_error_set(GIT_ERROR_OS, "failed to fsync '%s'", file->path_lock); + goto on_error; + } + + if (p_close(file->fd) < 0) { + git_error_set(GIT_ERROR_OS, "failed to close file at '%s'", file->path_lock); + goto on_error; + } + + file->fd = -1; + + if (p_rename(file->path_lock, file->path_original) < 0) { + git_error_set(GIT_ERROR_OS, "failed to rename lockfile to '%s'", file->path_original); + goto on_error; + } + + if (file->do_fsync && git_futils_fsync_parent(file->path_original) < 0) + goto on_error; + + file->did_rename = true; + + git_filebuf_cleanup(file); + return 0; + +on_error: + git_filebuf_cleanup(file); + return -1; +} + +GIT_INLINE(void) add_to_cache(git_filebuf *file, const void *buf, size_t len) +{ + memcpy(file->buffer + file->buf_pos, buf, len); + file->buf_pos += len; +} + +int git_filebuf_write(git_filebuf *file, const void *buff, size_t len) +{ + const unsigned char *buf = buff; + + ENSURE_BUF_OK(file); + + if (file->do_not_buffer) + return file->write(file, (void *)buff, len); + + for (;;) { + size_t space_left = file->buf_size - file->buf_pos; + + /* cache if it's small */ + if (space_left > len) { + add_to_cache(file, buf, len); + return 0; + } + + add_to_cache(file, buf, space_left); + if (flush_buffer(file) < 0) + return -1; + + len -= space_left; + buf += space_left; + } +} + +int git_filebuf_reserve(git_filebuf *file, void **buffer, size_t len) +{ + size_t space_left = file->buf_size - file->buf_pos; + + *buffer = NULL; + + ENSURE_BUF_OK(file); + + if (len > file->buf_size) { + file->last_error = BUFERR_MEM; + return -1; + } + + if (space_left <= len) { + if (flush_buffer(file) < 0) + return -1; + } + + *buffer = (file->buffer + file->buf_pos); + file->buf_pos += len; + + return 0; +} + +int git_filebuf_printf(git_filebuf *file, const char *format, ...) +{ + va_list arglist; + size_t space_left, len, alloclen; + int written, res; + char *tmp_buffer; + + ENSURE_BUF_OK(file); + + space_left = file->buf_size - file->buf_pos; + + do { + va_start(arglist, format); + written = p_vsnprintf((char *)file->buffer + file->buf_pos, space_left, format, arglist); + va_end(arglist); + + if (written < 0) { + file->last_error = BUFERR_MEM; + return -1; + } + + len = written; + if (len + 1 <= space_left) { + file->buf_pos += len; + return 0; + } + + if (flush_buffer(file) < 0) + return -1; + + space_left = file->buf_size - file->buf_pos; + + } while (len + 1 <= space_left); + + if (GIT_ADD_SIZET_OVERFLOW(&alloclen, len, 1) || + !(tmp_buffer = git__malloc(alloclen))) { + file->last_error = BUFERR_MEM; + return -1; + } + + va_start(arglist, format); + written = p_vsnprintf(tmp_buffer, len + 1, format, arglist); + va_end(arglist); + + if (written < 0) { + git__free(tmp_buffer); + file->last_error = BUFERR_MEM; + return -1; + } + + res = git_filebuf_write(file, tmp_buffer, len); + git__free(tmp_buffer); + + return res; +} + +int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file) +{ + int res; + struct stat st; + + if (file->fd_is_open) + res = p_fstat(file->fd, &st); + else + res = p_stat(file->path_original, &st); + + if (res < 0) { + git_error_set(GIT_ERROR_OS, "could not get stat info for '%s'", + file->path_original); + return res; + } + + if (mtime) + *mtime = st.st_mtime; + if (size) + *size = (size_t)st.st_size; + + return 0; +} diff --git a/src/util/filebuf.h b/src/util/filebuf.h new file mode 100644 index 00000000000..e23b9ed2adc --- /dev/null +++ b/src/util/filebuf.h @@ -0,0 +1,107 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_filebuf_h__ +#define INCLUDE_filebuf_h__ + +#include "git2_util.h" + +#include "futils.h" +#include "hash.h" +#include + +#ifdef GIT_THREADS +# define GIT_FILEBUF_THREADS +#endif + +#define GIT_FILEBUF_HASH_SHA1 (1 << 0) +#define GIT_FILEBUF_HASH_SHA256 (1 << 1) +#define GIT_FILEBUF_APPEND (1 << 2) +#define GIT_FILEBUF_CREATE_LEADING_DIRS (1 << 3) +#define GIT_FILEBUF_TEMPORARY (1 << 4) +#define GIT_FILEBUF_DO_NOT_BUFFER (1 << 5) +#define GIT_FILEBUF_FSYNC (1 << 6) +#define GIT_FILEBUF_DEFLATE_SHIFT (7) + +#define GIT_FILELOCK_EXTENSION ".lock\0" +#define GIT_FILELOCK_EXTLENGTH 6 + +typedef struct git_filebuf git_filebuf; +struct git_filebuf { + char *path_original; + char *path_lock; + + int (*write)(git_filebuf *file, void *source, size_t len); + + bool compute_digest; + git_hash_ctx digest; + + unsigned char *buffer; + unsigned char *z_buf; + + z_stream zs; + int flush_mode; + + size_t buf_size, buf_pos; + git_file fd; + bool fd_is_open; + bool created_lock; + bool did_rename; + bool do_not_buffer; + bool do_fsync; + int last_error; +}; + +#define GIT_FILEBUF_INIT {0} + +/* + * The git_filebuf object lifecycle is: + * - Allocate git_filebuf, preferably using GIT_FILEBUF_INIT. + * + * - Call git_filebuf_open() to initialize the filebuf for use. + * + * - Make as many calls to git_filebuf_write(), git_filebuf_printf(), + * git_filebuf_reserve() as you like. The error codes for these + * functions don't need to be checked. They are stored internally + * by the file buffer. + * + * - While you are writing, you may call git_filebuf_hash() to get + * the hash of all you have written so far. This function will + * fail if any of the previous writes to the buffer failed. + * + * - To close the git_filebuf, you may call git_filebuf_commit() or + * git_filebuf_commit_at() to save the file, or + * git_filebuf_cleanup() to abandon the file. All of these will + * free the git_filebuf object. Likewise, all of these will fail + * if any of the previous writes to the buffer failed, and set + * an error code accordingly. + */ +int git_filebuf_write(git_filebuf *lock, const void *buff, size_t len); +int git_filebuf_reserve(git_filebuf *file, void **buff, size_t len); +int git_filebuf_printf(git_filebuf *file, const char *format, ...) GIT_FORMAT_PRINTF(2, 3); + +int git_filebuf_open(git_filebuf *lock, const char *path, int flags, mode_t mode); +int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mode_t mode, size_t size); +int git_filebuf_commit(git_filebuf *lock); +int git_filebuf_commit_at(git_filebuf *lock, const char *path); +void git_filebuf_cleanup(git_filebuf *lock); +int git_filebuf_hash(unsigned char *out, git_filebuf *file); +int git_filebuf_flush(git_filebuf *file); +int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file); + +GIT_INLINE(int) git_filebuf_hash_flags(git_hash_algorithm_t algorithm) +{ + switch (algorithm) { + case GIT_HASH_ALGORITHM_SHA1: + return GIT_FILEBUF_HASH_SHA1; + case GIT_HASH_ALGORITHM_SHA256: + return GIT_FILEBUF_HASH_SHA256; + default: + return 0; + } +} + +#endif diff --git a/src/util/fs_path.c b/src/util/fs_path.c new file mode 100644 index 00000000000..ff0836ff874 --- /dev/null +++ b/src/util/fs_path.c @@ -0,0 +1,2137 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "fs_path.h" + +#include "git2_util.h" +#include "futils.h" +#include "posix.h" +#ifdef GIT_WIN32 +#include "win32/posix.h" +#include "win32/w32_buffer.h" +#include "win32/w32_util.h" +#include "win32/version.h" +#include +#else +#include +#endif +#include +#include + +#define ensure_error_set(code) do { \ + const git_error *e = git_error_last(); \ + if (!e || !e->message) \ + git_error_set(e ? e->klass : GIT_ERROR_CALLBACK, \ + "filesystem callback returned %d", code); \ + } while(0) + +static int dos_drive_prefix_length(const char *path) +{ + int i; + + /* + * Does it start with an ASCII letter (i.e. highest bit not set), + * followed by a colon? + */ + if (!(0x80 & (unsigned char)*path)) + return *path && path[1] == ':' ? 2 : 0; + + /* + * While drive letters must be letters of the English alphabet, it is + * possible to assign virtually _any_ Unicode character via `subst` as + * a drive letter to "virtual drives". Even `1`, or `ä`. Or fun stuff + * like this: + * + * subst ֍: %USERPROFILE%\Desktop + */ + for (i = 1; i < 4 && (0x80 & (unsigned char)path[i]); i++) + ; /* skip first UTF-8 character */ + return path[i] == ':' ? i + 1 : 0; +} + +#ifdef GIT_WIN32 +static bool looks_like_network_computer_name(const char *path, int pos) +{ + if (pos < 3) + return false; + + if (path[0] != '/' || path[1] != '/') + return false; + + while (pos-- > 2) { + if (path[pos] == '/') + return false; + } + + return true; +} +#endif + +/* + * Based on the Android implementation, BSD licensed. + * http://android.git.kernel.org/ + * + * Copyright (C) 2008 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +int git_fs_path_basename_r(git_str *buffer, const char *path) +{ + const char *endp, *startp; + int len, result; + + /* Empty or NULL string gets treated as "." */ + if (path == NULL || *path == '\0') { + startp = "."; + len = 1; + goto Exit; + } + + /* Strip trailing slashes */ + endp = path + strlen(path) - 1; + while (endp > path && *endp == '/') + endp--; + + /* All slashes becomes "/" */ + if (endp == path && *endp == '/') { + startp = "/"; + len = 1; + goto Exit; + } + + /* Find the start of the base */ + startp = endp; + while (startp > path && *(startp - 1) != '/') + startp--; + + /* Cast is safe because max path < max int */ + len = (int)(endp - startp + 1); + +Exit: + result = len; + + if (buffer != NULL && git_str_set(buffer, startp, len) < 0) + return -1; + + return result; +} + +/* + * Determine if the path is a Windows prefix and, if so, returns + * its actual length. If it is not a prefix, returns -1. + */ +static int win32_prefix_length(const char *path, int len) +{ +#ifndef GIT_WIN32 + GIT_UNUSED(path); + GIT_UNUSED(len); +#else + /* + * Mimic unix behavior where '/.git' returns '/': 'C:/.git' + * will return 'C:/' here + */ + if (dos_drive_prefix_length(path) == len) + return len; + + /* + * Similarly checks if we're dealing with a network computer name + * '//computername/.git' will return '//computername/' + */ + if (looks_like_network_computer_name(path, len)) + return len; +#endif + + return -1; +} + +/* + * Based on the Android implementation, BSD licensed. + * Check http://android.git.kernel.org/ + */ +int git_fs_path_dirname_r(git_str *buffer, const char *path) +{ + const char *endp; + int is_prefix = 0, len; + + /* Empty or NULL string gets treated as "." */ + if (path == NULL || *path == '\0') { + path = "."; + len = 1; + goto Exit; + } + + /* Strip trailing slashes */ + endp = path + strlen(path) - 1; + while (endp > path && *endp == '/') + endp--; + + if (endp - path + 1 > INT_MAX) { + git_error_set(GIT_ERROR_INVALID, "path too long"); + return -1; + } + + if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) { + is_prefix = 1; + goto Exit; + } + + /* Find the start of the dir */ + while (endp > path && *endp != '/') + endp--; + + /* Either the dir is "/" or there are no slashes */ + if (endp == path) { + path = (*endp == '/') ? "/" : "."; + len = 1; + goto Exit; + } + + do { + endp--; + } while (endp > path && *endp == '/'); + + if (endp - path + 1 > INT_MAX) { + git_error_set(GIT_ERROR_INVALID, "path too long"); + return -1; + } + + if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) { + is_prefix = 1; + goto Exit; + } + + /* Cast is safe because max path < max int */ + len = (int)(endp - path + 1); + +Exit: + if (buffer) { + if (git_str_set(buffer, path, len) < 0) + return -1; + if (is_prefix && git_str_putc(buffer, '/') < 0) + return -1; + } + + return len; +} + + +char *git_fs_path_dirname(const char *path) +{ + git_str buf = GIT_STR_INIT; + char *dirname; + + git_fs_path_dirname_r(&buf, path); + dirname = git_str_detach(&buf); + git_str_dispose(&buf); /* avoid memleak if error occurs */ + + return dirname; +} + +char *git_fs_path_basename(const char *path) +{ + git_str buf = GIT_STR_INIT; + char *basename; + + git_fs_path_basename_r(&buf, path); + basename = git_str_detach(&buf); + git_str_dispose(&buf); /* avoid memleak if error occurs */ + + return basename; +} + +size_t git_fs_path_basename_offset(git_str *buffer) +{ + ssize_t slash; + + if (!buffer || buffer->size <= 0) + return 0; + + slash = git_str_rfind_next(buffer, '/'); + + if (slash >= 0 && buffer->ptr[slash] == '/') + return (size_t)(slash + 1); + + return 0; +} + +int git_fs_path_root(const char *path) +{ + int offset = 0, prefix_len; + + /* Does the root of the path look like a windows drive ? */ + if ((prefix_len = dos_drive_prefix_length(path))) + offset += prefix_len; + +#ifdef GIT_WIN32 + /* Are we dealing with a windows network path? */ + else if ((path[0] == '/' && path[1] == '/' && path[2] != '/') || + (path[0] == '\\' && path[1] == '\\' && path[2] != '\\')) + { + offset += 2; + + /* Skip the computer name segment */ + while (path[offset] && path[offset] != '/' && path[offset] != '\\') + offset++; + } + + if (path[offset] == '\\') + return offset; +#endif + + if (path[offset] == '/') + return offset; + + return -1; /* Not a real error - signals that path is not rooted */ +} + +static void path_trim_slashes(git_str *path) +{ + int ceiling = git_fs_path_root(path->ptr) + 1; + + if (ceiling < 0) + return; + + while (path->size > (size_t)ceiling) { + if (path->ptr[path->size-1] != '/') + break; + + path->ptr[path->size-1] = '\0'; + path->size--; + } +} + +int git_fs_path_join_unrooted( + git_str *path_out, const char *path, const char *base, ssize_t *root_at) +{ + ssize_t root; + + GIT_ASSERT_ARG(path_out); + GIT_ASSERT_ARG(path); + + root = (ssize_t)git_fs_path_root(path); + + if (base != NULL && root < 0) { + if (git_str_joinpath(path_out, base, path) < 0) + return -1; + + root = (ssize_t)strlen(base); + } else { + if (git_str_sets(path_out, path) < 0) + return -1; + + if (root < 0) + root = 0; + else if (base) + git_fs_path_equal_or_prefixed(base, path, &root); + } + + if (root_at) + *root_at = root; + + return 0; +} + +void git_fs_path_squash_slashes(git_str *path) +{ + char *p, *q; + + if (path->size == 0) + return; + + for (p = path->ptr, q = path->ptr; *q; p++, q++) { + *p = *q; + + while (*q == '/' && *(q+1) == '/') { + path->size--; + q++; + } + } + + *p = '\0'; +} + +int git_fs_path_prettify(git_str *path_out, const char *path, const char *base) +{ + char buf[GIT_PATH_MAX]; + + GIT_ASSERT_ARG(path_out); + GIT_ASSERT_ARG(path); + + /* construct path if needed */ + if (base != NULL && git_fs_path_root(path) < 0) { + if (git_str_joinpath(path_out, base, path) < 0) + return -1; + path = path_out->ptr; + } + + if (p_realpath(path, buf) == NULL) { + /* git_error_set resets the errno when dealing with a GIT_ERROR_OS kind of error */ + int error = (errno == ENOENT || errno == ENOTDIR) ? GIT_ENOTFOUND : -1; + git_error_set(GIT_ERROR_OS, "failed to resolve path '%s'", path); + + git_str_clear(path_out); + + return error; + } + + return git_str_sets(path_out, buf); +} + +int git_fs_path_prettify_dir(git_str *path_out, const char *path, const char *base) +{ + int error = git_fs_path_prettify(path_out, path, base); + return (error < 0) ? error : git_fs_path_to_dir(path_out); +} + +int git_fs_path_to_dir(git_str *path) +{ + if (path->asize > 0 && + git_str_len(path) > 0 && + path->ptr[git_str_len(path) - 1] != '/') + git_str_putc(path, '/'); + + return git_str_oom(path) ? -1 : 0; +} + +size_t git_fs_path_dirlen(const char *path) +{ + size_t len = strlen(path); + + while (len > 1 && path[len - 1] == '/') + len--; + + return len; +} + +void git_fs_path_string_to_dir(char *path, size_t size) +{ + size_t end = strlen(path); + + if (end && path[end - 1] != '/' && end < size) { + path[end] = '/'; + path[end + 1] = '\0'; + } +} + +int git__percent_decode(git_str *decoded_out, const char *input) +{ + int len, hi, lo, i; + + GIT_ASSERT_ARG(decoded_out); + GIT_ASSERT_ARG(input); + + len = (int)strlen(input); + git_str_clear(decoded_out); + + for(i = 0; i < len; i++) + { + char c = input[i]; + + if (c != '%') + goto append; + + if (i >= len - 2) + goto append; + + hi = git__fromhex(input[i + 1]); + lo = git__fromhex(input[i + 2]); + + if (hi < 0 || lo < 0) + goto append; + + c = (char)(hi << 4 | lo); + i += 2; + +append: + if (git_str_putc(decoded_out, c) < 0) + return -1; + } + + return 0; +} + +static int error_invalid_local_file_uri(const char *uri) +{ + git_error_set(GIT_ERROR_CONFIG, "'%s' is not a valid local file URI", uri); + return -1; +} + +static int local_file_url_prefixlen(const char *file_url) +{ + int len = -1; + + if (git__prefixcmp(file_url, "file://") == 0) { + if (file_url[7] == '/') + len = 8; + else if (git__prefixcmp(file_url + 7, "localhost/") == 0) + len = 17; + } + + return len; +} + +bool git_fs_path_is_local_file_url(const char *file_url) +{ + return (local_file_url_prefixlen(file_url) > 0); +} + +int git_fs_path_fromurl(git_str *local_path_out, const char *file_url) +{ + int offset; + + GIT_ASSERT_ARG(local_path_out); + GIT_ASSERT_ARG(file_url); + + if ((offset = local_file_url_prefixlen(file_url)) < 0 || + file_url[offset] == '\0' || file_url[offset] == '/') + return error_invalid_local_file_uri(file_url); + +#ifndef GIT_WIN32 + offset--; /* A *nix absolute path starts with a forward slash */ +#endif + + git_str_clear(local_path_out); + return git__percent_decode(local_path_out, file_url + offset); +} + +int git_fs_path_walk_up( + git_str *path, + const char *ceiling, + int (*cb)(void *data, const char *), + void *data) +{ + int error = 0; + git_str iter; + ssize_t stop = 0, scan; + char oldc = '\0'; + + GIT_ASSERT_ARG(path); + GIT_ASSERT_ARG(cb); + + if (ceiling != NULL) { + if (git__prefixcmp(path->ptr, ceiling) == 0) + stop = (ssize_t)strlen(ceiling); + else + stop = git_str_len(path); + } + scan = git_str_len(path); + + /* empty path: yield only once */ + if (!scan) { + error = cb(data, ""); + if (error) + ensure_error_set(error); + return error; + } + + iter.ptr = path->ptr; + iter.size = git_str_len(path); + iter.asize = path->asize; + + while (scan >= stop) { + error = cb(data, iter.ptr); + iter.ptr[scan] = oldc; + + if (error) { + ensure_error_set(error); + break; + } + + scan = git_str_rfind_next(&iter, '/'); + if (scan >= 0) { + scan++; + oldc = iter.ptr[scan]; + iter.size = scan; + iter.ptr[scan] = '\0'; + } + } + + if (scan >= 0) + iter.ptr[scan] = oldc; + + /* relative path: yield for the last component */ + if (!error && stop == 0 && iter.ptr[0] != '/') { + error = cb(data, ""); + if (error) + ensure_error_set(error); + } + + return error; +} + +bool git_fs_path_exists(const char *path) +{ + GIT_ASSERT_ARG_WITH_RETVAL(path, false); + return p_access(path, F_OK) == 0; +} + +bool git_fs_path_isdir(const char *path) +{ + struct stat st; + if (p_stat(path, &st) < 0) + return false; + + return S_ISDIR(st.st_mode) != 0; +} + +bool git_fs_path_isfile(const char *path) +{ + struct stat st; + + GIT_ASSERT_ARG_WITH_RETVAL(path, false); + if (p_stat(path, &st) < 0) + return false; + + return S_ISREG(st.st_mode) != 0; +} + +#ifdef GIT_WIN32 + +bool git_fs_path_isexecutable(const char *path) +{ + struct stat st; + + GIT_ASSERT_ARG_WITH_RETVAL(path, false); + + if (git__suffixcmp_icase(path, ".exe") != 0 && + git__suffixcmp_icase(path, ".cmd") != 0) + return false; + + return (p_stat(path, &st) == 0); +} + +#else + +bool git_fs_path_isexecutable(const char *path) +{ + struct stat st; + + GIT_ASSERT_ARG_WITH_RETVAL(path, false); + if (p_stat(path, &st) < 0) + return false; + + return S_ISREG(st.st_mode) != 0 && + ((st.st_mode & S_IXUSR) != 0); +} + +#endif + +bool git_fs_path_islink(const char *path) +{ + struct stat st; + + GIT_ASSERT_ARG_WITH_RETVAL(path, false); + if (p_lstat(path, &st) < 0) + return false; + + return S_ISLNK(st.st_mode) != 0; +} + +#ifdef GIT_WIN32 + +bool git_fs_path_is_empty_dir(const char *path) +{ + git_win32_path filter_w; + bool empty = false; + + if (git_win32__findfirstfile_filter(filter_w, path)) { + WIN32_FIND_DATAW findData; + HANDLE hFind = FindFirstFileW(filter_w, &findData); + + /* FindFirstFile will fail if there are no children to the given + * path, which can happen if the given path is a file (and obviously + * has no children) or if the given path is an empty mount point. + * (Most directories have at least directory entries '.' and '..', + * but ridiculously another volume mounted in another drive letter's + * path space do not, and thus have nothing to enumerate.) If + * FindFirstFile fails, check if this is a directory-like thing + * (a mount point). + */ + if (hFind == INVALID_HANDLE_VALUE) + return git_fs_path_isdir(path); + + /* If the find handle was created successfully, then it's a directory */ + empty = true; + + do { + /* Allow the enumeration to return . and .. and still be considered + * empty. In the special case of drive roots (i.e. C:\) where . and + * .. do not occur, we can still consider the path to be an empty + * directory if there's nothing there. */ + if (!git_fs_path_is_dot_or_dotdotW(findData.cFileName)) { + empty = false; + break; + } + } while (FindNextFileW(hFind, &findData)); + + FindClose(hFind); + } + + return empty; +} + +#else + +static int path_found_entry(void *payload, git_str *path) +{ + GIT_UNUSED(payload); + return !git_fs_path_is_dot_or_dotdot(path->ptr); +} + +bool git_fs_path_is_empty_dir(const char *path) +{ + int error; + git_str dir = GIT_STR_INIT; + + if (!git_fs_path_isdir(path)) + return false; + + if ((error = git_str_sets(&dir, path)) != 0) + git_error_clear(); + else + error = git_fs_path_direach(&dir, 0, path_found_entry, NULL); + + git_str_dispose(&dir); + + return !error; +} + +#endif + +int git_fs_path_set_error(int errno_value, const char *path, const char *action) +{ + switch (errno_value) { + case ENOENT: + case ENOTDIR: + git_error_set(GIT_ERROR_OS, "could not find '%s' to %s", path, action); + return GIT_ENOTFOUND; + + case EINVAL: + case ENAMETOOLONG: + git_error_set(GIT_ERROR_OS, "invalid path for filesystem '%s'", path); + return GIT_EINVALIDSPEC; + + case EEXIST: + git_error_set(GIT_ERROR_OS, "failed %s - '%s' already exists", action, path); + return GIT_EEXISTS; + + case EACCES: + git_error_set(GIT_ERROR_OS, "failed %s - '%s' is locked", action, path); + return GIT_ELOCKED; + + default: + git_error_set(GIT_ERROR_OS, "could not %s '%s'", action, path); + return -1; + } +} + +int git_fs_path_lstat(const char *path, struct stat *st) +{ + if (p_lstat(path, st) == 0) + return 0; + + return git_fs_path_set_error(errno, path, "stat"); +} + +static bool _check_dir_contents( + git_str *dir, + const char *sub, + bool (*predicate)(const char *)) +{ + bool result; + size_t dir_size = git_str_len(dir); + size_t sub_size = strlen(sub); + size_t alloc_size; + + /* leave base valid even if we could not make space for subdir */ + if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, dir_size, sub_size) || + GIT_ADD_SIZET_OVERFLOW(&alloc_size, alloc_size, 2) || + git_str_try_grow(dir, alloc_size, false) < 0) + return false; + + /* save excursion */ + if (git_str_joinpath(dir, dir->ptr, sub) < 0) + return false; + + result = predicate(dir->ptr); + + /* restore path */ + git_str_truncate(dir, dir_size); + return result; +} + +bool git_fs_path_contains(git_str *dir, const char *item) +{ + return _check_dir_contents(dir, item, &git_fs_path_exists); +} + +bool git_fs_path_contains_dir(git_str *base, const char *subdir) +{ + return _check_dir_contents(base, subdir, &git_fs_path_isdir); +} + +bool git_fs_path_contains_file(git_str *base, const char *file) +{ + return _check_dir_contents(base, file, &git_fs_path_isfile); +} + +int git_fs_path_find_dir(git_str *dir) +{ + int error = 0; + char buf[GIT_PATH_MAX]; + + if (p_realpath(dir->ptr, buf) != NULL) + error = git_str_sets(dir, buf); + + /* call dirname if this is not a directory */ + if (!error) /* && git_fs_path_isdir(dir->ptr) == false) */ + error = (git_fs_path_dirname_r(dir, dir->ptr) < 0) ? -1 : 0; + + if (!error) + error = git_fs_path_to_dir(dir); + + return error; +} + +int git_fs_path_resolve_relative(git_str *path, size_t ceiling) +{ + char *base, *to, *from, *next; + size_t len; + + GIT_ERROR_CHECK_ALLOC_STR(path); + + if (ceiling > path->size) + ceiling = path->size; + + /* recognize drive prefixes, etc. that should not be backed over */ + if (ceiling == 0) + ceiling = git_fs_path_root(path->ptr) + 1; + + /* recognize URL prefixes that should not be backed over */ + if (ceiling == 0) { + for (next = path->ptr; *next && git__isalpha(*next); ++next); + if (next[0] == ':' && next[1] == '/' && next[2] == '/') + ceiling = (next + 3) - path->ptr; + } + + base = to = from = path->ptr + ceiling; + + while (*from) { + for (next = from; *next && *next != '/'; ++next); + + len = next - from; + + if (len == 1 && from[0] == '.') + /* do nothing with singleton dot */; + + else if (len == 2 && from[0] == '.' && from[1] == '.') { + /* error out if trying to up one from a hard base */ + if (to == base && ceiling != 0) { + git_error_set(GIT_ERROR_INVALID, + "cannot strip root component off url"); + return -1; + } + + /* no more path segments to strip, + * use '../' as a new base path */ + if (to == base) { + if (*next == '/') + len++; + + if (to != from) + memmove(to, from, len); + + to += len; + /* this is now the base, can't back up from a + * relative prefix */ + base = to; + } else { + /* back up a path segment */ + while (to > base && to[-1] == '/') to--; + while (to > base && to[-1] != '/') to--; + } + } else { + if (*next == '/' && *from != '/') + len++; + + if (to != from) + memmove(to, from, len); + + to += len; + } + + from += len; + + while (*from == '/') from++; + } + + *to = '\0'; + + path->size = to - path->ptr; + + return 0; +} + +int git_fs_path_apply_relative(git_str *target, const char *relpath) +{ + return git_str_joinpath(target, git_str_cstr(target), relpath) || + git_fs_path_resolve_relative(target, 0); +} + +int git_fs_path_cmp( + const char *name1, size_t len1, int isdir1, + const char *name2, size_t len2, int isdir2, + int (*compare)(const char *, const char *, size_t)) +{ + unsigned char c1, c2; + size_t len = len1 < len2 ? len1 : len2; + int cmp; + + cmp = compare(name1, name2, len); + if (cmp) + return cmp; + + c1 = name1[len]; + c2 = name2[len]; + + if (c1 == '\0' && isdir1) + c1 = '/'; + + if (c2 == '\0' && isdir2) + c2 = '/'; + + return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; +} + +size_t git_fs_path_common_dirlen(const char *one, const char *two) +{ + const char *p, *q, *dirsep = NULL; + + for (p = one, q = two; *p && *q; p++, q++) { + if (*p == '/' && *q == '/') + dirsep = p; + else if (*p != *q) + break; + } + + return dirsep ? (dirsep - one) + 1 : 0; +} + +int git_fs_path_make_relative(git_str *path, const char *parent) +{ + const char *p, *q, *p_dirsep, *q_dirsep; + size_t plen = path->size, newlen, alloclen, depth = 1, i, offset; + + for (p_dirsep = p = path->ptr, q_dirsep = q = parent; *p && *q; p++, q++) { + if (*p == '/' && *q == '/') { + p_dirsep = p; + q_dirsep = q; + } + else if (*p != *q) + break; + } + + /* need at least 1 common path segment */ + if ((p_dirsep == path->ptr || q_dirsep == parent) && + (*p_dirsep != '/' || *q_dirsep != '/')) { + git_error_set(GIT_ERROR_INVALID, + "%s is not a parent of %s", parent, path->ptr); + return GIT_ENOTFOUND; + } + + if (*p == '/' && !*q) + p++; + else if (!*p && *q == '/') + q++; + else if (!*p && !*q) + return git_str_clear(path), 0; + else { + p = p_dirsep + 1; + q = q_dirsep + 1; + } + + plen -= (p - path->ptr); + + if (!*q) + return git_str_set(path, p, plen); + + for (; (q = strchr(q, '/')) && *(q + 1); q++) + depth++; + + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&newlen, depth, 3); + GIT_ERROR_CHECK_ALLOC_ADD(&newlen, newlen, plen); + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, newlen, 1); + + /* save the offset as we might realllocate the pointer */ + offset = p - path->ptr; + if (git_str_try_grow(path, alloclen, 1) < 0) + return -1; + p = path->ptr + offset; + + memmove(path->ptr + (depth * 3), p, plen + 1); + + for (i = 0; i < depth; i++) + memcpy(path->ptr + (i * 3), "../", 3); + + path->size = newlen; + return 0; +} + +bool git_fs_path_has_non_ascii(const char *path, size_t pathlen) +{ + const uint8_t *scan = (const uint8_t *)path, *end; + + for (end = scan + pathlen; scan < end; ++scan) + if (*scan & 0x80) + return true; + + return false; +} + +#ifdef GIT_I18N_ICONV + +int git_fs_path_iconv_init_precompose(git_fs_path_iconv_t *ic) +{ + git_str_init(&ic->buf, 0); + ic->map = iconv_open(GIT_PATH_REPO_ENCODING, GIT_PATH_NATIVE_ENCODING); + return 0; +} + +void git_fs_path_iconv_clear(git_fs_path_iconv_t *ic) +{ + if (ic) { + if (ic->map != (iconv_t)-1) + iconv_close(ic->map); + git_str_dispose(&ic->buf); + } +} + +int git_fs_path_iconv(git_fs_path_iconv_t *ic, const char **in, size_t *inlen) +{ + char *nfd = (char*)*in, *nfc; + size_t nfdlen = *inlen, nfclen, wantlen = nfdlen, alloclen, rv; + int retry = 1; + + if (!ic || ic->map == (iconv_t)-1 || + !git_fs_path_has_non_ascii(*in, *inlen)) + return 0; + + git_str_clear(&ic->buf); + + while (1) { + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, wantlen, 1); + if (git_str_grow(&ic->buf, alloclen) < 0) + return -1; + + nfc = ic->buf.ptr + ic->buf.size; + nfclen = ic->buf.asize - ic->buf.size; + + rv = iconv(ic->map, &nfd, &nfdlen, &nfc, &nfclen); + + ic->buf.size = (nfc - ic->buf.ptr); + + if (rv != (size_t)-1) + break; + + /* if we cannot convert the data (probably because iconv thinks + * it is not valid UTF-8 source data), then use original data + */ + if (errno != E2BIG) + return 0; + + /* make space for 2x the remaining data to be converted + * (with per retry overhead to avoid infinite loops) + */ + wantlen = ic->buf.size + max(nfclen, nfdlen) * 2 + (size_t)(retry * 4); + + if (retry++ > 4) + goto fail; + } + + ic->buf.ptr[ic->buf.size] = '\0'; + + *in = ic->buf.ptr; + *inlen = ic->buf.size; + + return 0; + +fail: + git_error_set(GIT_ERROR_OS, "unable to convert unicode path data"); + return -1; +} + +static const char *nfc_file = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D"; +static const char *nfd_file = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D"; + +/* Check if the platform is decomposing unicode data for us. We will + * emulate core Git and prefer to use precomposed unicode data internally + * on these platforms, composing the decomposed unicode on the fly. + * + * This mainly happens on the Mac where HDFS stores filenames as + * decomposed unicode. Even on VFAT and SAMBA file systems, the Mac will + * return decomposed unicode from readdir() even when the actual + * filesystem is storing precomposed unicode. + */ +bool git_fs_path_does_decompose_unicode(const char *root) +{ + git_str nfc_path = GIT_STR_INIT; + git_str nfd_path = GIT_STR_INIT; + int fd; + bool found_decomposed = false; + size_t orig_len; + const char *trailer; + + /* Create a file using a precomposed path and then try to find it + * using the decomposed name. If the lookup fails, then we will mark + * that we should precompose unicode for this repository. + */ + if (git_str_joinpath(&nfc_path, root, nfc_file) < 0) + goto done; + + /* record original path length before trailer */ + orig_len = nfc_path.size; + + if ((fd = git_futils_mktmp(&nfc_path, nfc_path.ptr, 0666)) < 0) + goto done; + p_close(fd); + + trailer = nfc_path.ptr + orig_len; + + /* try to look up as NFD path */ + if (git_str_joinpath(&nfd_path, root, nfd_file) < 0 || + git_str_puts(&nfd_path, trailer) < 0) + goto done; + + found_decomposed = git_fs_path_exists(nfd_path.ptr); + + /* remove temporary file (using original precomposed path) */ + (void)p_unlink(nfc_path.ptr); + +done: + git_str_dispose(&nfc_path); + git_str_dispose(&nfd_path); + return found_decomposed; +} + +#else + +bool git_fs_path_does_decompose_unicode(const char *root) +{ + GIT_UNUSED(root); + return false; +} + +#endif + +#if defined(__sun) || defined(__GNU__) +typedef char path_dirent_data[sizeof(struct dirent) + FILENAME_MAX + 1]; +#else +typedef struct dirent path_dirent_data; +#endif + +int git_fs_path_direach( + git_str *path, + uint32_t flags, + int (*fn)(void *, git_str *), + void *arg) +{ + int error = 0; + ssize_t wd_len; + DIR *dir; + struct dirent *de; + +#ifdef GIT_I18N_ICONV + git_fs_path_iconv_t ic = GIT_PATH_ICONV_INIT; +#endif + + GIT_UNUSED(flags); + + if (git_fs_path_to_dir(path) < 0) + return -1; + + wd_len = git_str_len(path); + + if ((dir = opendir(path->ptr)) == NULL) { + git_error_set(GIT_ERROR_OS, "failed to open directory '%s'", path->ptr); + if (errno == ENOENT) + return GIT_ENOTFOUND; + + return -1; + } + +#ifdef GIT_I18N_ICONV + if ((flags & GIT_FS_PATH_DIR_PRECOMPOSE_UNICODE) != 0) + (void)git_fs_path_iconv_init_precompose(&ic); +#endif + + while ((de = readdir(dir)) != NULL) { + const char *de_path = de->d_name; + size_t de_len = strlen(de_path); + + if (git_fs_path_is_dot_or_dotdot(de_path)) + continue; + +#ifdef GIT_I18N_ICONV + if ((error = git_fs_path_iconv(&ic, &de_path, &de_len)) < 0) + break; +#endif + + if ((error = git_str_put(path, de_path, de_len)) < 0) + break; + + git_error_clear(); + error = fn(arg, path); + + git_str_truncate(path, wd_len); /* restore path */ + + /* Only set our own error if the callback did not set one already */ + if (error != 0) { + if (!git_error_last()) + ensure_error_set(error); + + break; + } + } + + closedir(dir); + +#ifdef GIT_I18N_ICONV + git_fs_path_iconv_clear(&ic); +#endif + + return error; +} + +#if defined(GIT_WIN32) && !defined(__MINGW32__) + +/* Using _FIND_FIRST_EX_LARGE_FETCH may increase performance in Windows 7 + * and better. + */ +#ifndef FIND_FIRST_EX_LARGE_FETCH +# define FIND_FIRST_EX_LARGE_FETCH 2 +#endif + +int git_fs_path_diriter_init( + git_fs_path_diriter *diriter, + const char *path, + unsigned int flags) +{ + git_win32_path path_filter; + + static int is_win7_or_later = -1; + if (is_win7_or_later < 0) + is_win7_or_later = git_has_win32_version(6, 1, 0); + + GIT_ASSERT_ARG(diriter); + GIT_ASSERT_ARG(path); + + memset(diriter, 0, sizeof(git_fs_path_diriter)); + diriter->handle = INVALID_HANDLE_VALUE; + + if (git_str_puts(&diriter->path_utf8, path) < 0) + return -1; + + path_trim_slashes(&diriter->path_utf8); + + if (diriter->path_utf8.size == 0) { + git_error_set(GIT_ERROR_FILESYSTEM, "could not open directory '%s'", path); + return -1; + } + + if ((diriter->parent_len = git_win32_path_from_utf8(diriter->path, diriter->path_utf8.ptr)) < 0 || + !git_win32__findfirstfile_filter(path_filter, diriter->path_utf8.ptr)) { + git_error_set(GIT_ERROR_OS, "could not parse the directory path '%s'", path); + return -1; + } + + diriter->handle = FindFirstFileExW( + path_filter, + is_win7_or_later ? FindExInfoBasic : FindExInfoStandard, + &diriter->current, + FindExSearchNameMatch, + NULL, + is_win7_or_later ? FIND_FIRST_EX_LARGE_FETCH : 0); + + if (diriter->handle == INVALID_HANDLE_VALUE) { + git_error_set(GIT_ERROR_OS, "could not open directory '%s'", path); + return -1; + } + + diriter->parent_utf8_len = diriter->path_utf8.size; + diriter->flags = flags; + return 0; +} + +static int diriter_update_paths(git_fs_path_diriter *diriter) +{ + size_t filename_len, path_len; + + filename_len = wcslen(diriter->current.cFileName); + + if (GIT_ADD_SIZET_OVERFLOW(&path_len, diriter->parent_len, filename_len) || + GIT_ADD_SIZET_OVERFLOW(&path_len, path_len, 2)) + return -1; + + if (path_len > GIT_WIN_PATH_UTF16) { + git_error_set(GIT_ERROR_FILESYSTEM, + "invalid path '%.*ls\\%ls' (path too long)", + diriter->parent_len, diriter->path, diriter->current.cFileName); + return -1; + } + + diriter->path[diriter->parent_len] = L'\\'; + memcpy(&diriter->path[diriter->parent_len+1], + diriter->current.cFileName, filename_len * sizeof(wchar_t)); + diriter->path[path_len-1] = L'\0'; + + git_str_truncate(&diriter->path_utf8, diriter->parent_utf8_len); + + if (diriter->parent_utf8_len > 0 && + diriter->path_utf8.ptr[diriter->parent_utf8_len-1] != '/') + git_str_putc(&diriter->path_utf8, '/'); + + git_str_put_w(&diriter->path_utf8, diriter->current.cFileName, filename_len); + + if (git_str_oom(&diriter->path_utf8)) + return -1; + + return 0; +} + +int git_fs_path_diriter_next(git_fs_path_diriter *diriter) +{ + bool skip_dot = !(diriter->flags & GIT_FS_PATH_DIR_INCLUDE_DOT_AND_DOTDOT); + + do { + /* Our first time through, we already have the data from + * FindFirstFileW. Use it, otherwise get the next file. + */ + if (!diriter->needs_next) + diriter->needs_next = 1; + else if (!FindNextFileW(diriter->handle, &diriter->current)) + return GIT_ITEROVER; + } while (skip_dot && git_fs_path_is_dot_or_dotdotW(diriter->current.cFileName)); + + if (diriter_update_paths(diriter) < 0) + return -1; + + return 0; +} + +int git_fs_path_diriter_filename( + const char **out, + size_t *out_len, + git_fs_path_diriter *diriter) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(out_len); + GIT_ASSERT_ARG(diriter); + GIT_ASSERT(diriter->path_utf8.size > diriter->parent_utf8_len); + + *out = &diriter->path_utf8.ptr[diriter->parent_utf8_len+1]; + *out_len = diriter->path_utf8.size - diriter->parent_utf8_len - 1; + return 0; +} + +int git_fs_path_diriter_fullpath( + const char **out, + size_t *out_len, + git_fs_path_diriter *diriter) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(out_len); + GIT_ASSERT_ARG(diriter); + + *out = diriter->path_utf8.ptr; + *out_len = diriter->path_utf8.size; + return 0; +} + +int git_fs_path_diriter_stat(struct stat *out, git_fs_path_diriter *diriter) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(diriter); + + return git_win32__file_attribute_to_stat(out, + (WIN32_FILE_ATTRIBUTE_DATA *)&diriter->current, + diriter->path); +} + +void git_fs_path_diriter_free(git_fs_path_diriter *diriter) +{ + if (diriter == NULL) + return; + + git_str_dispose(&diriter->path_utf8); + + if (diriter->handle != INVALID_HANDLE_VALUE) { + FindClose(diriter->handle); + diriter->handle = INVALID_HANDLE_VALUE; + } +} + +#else + +int git_fs_path_diriter_init( + git_fs_path_diriter *diriter, + const char *path, + unsigned int flags) +{ + GIT_ASSERT_ARG(diriter); + GIT_ASSERT_ARG(path); + + memset(diriter, 0, sizeof(git_fs_path_diriter)); + + if (git_str_puts(&diriter->path, path) < 0) + return -1; + + path_trim_slashes(&diriter->path); + + if (diriter->path.size == 0) { + git_error_set(GIT_ERROR_FILESYSTEM, "could not open directory '%s'", path); + return -1; + } + + if ((diriter->dir = opendir(diriter->path.ptr)) == NULL) { + git_str_dispose(&diriter->path); + + git_error_set(GIT_ERROR_OS, "failed to open directory '%s'", path); + return -1; + } + +#ifdef GIT_I18N_ICONV + if ((flags & GIT_FS_PATH_DIR_PRECOMPOSE_UNICODE) != 0) + (void)git_fs_path_iconv_init_precompose(&diriter->ic); +#endif + + diriter->parent_len = diriter->path.size; + diriter->flags = flags; + + return 0; +} + +int git_fs_path_diriter_next(git_fs_path_diriter *diriter) +{ + struct dirent *de; + const char *filename; + size_t filename_len; + bool skip_dot = !(diriter->flags & GIT_FS_PATH_DIR_INCLUDE_DOT_AND_DOTDOT); + int error = 0; + + GIT_ASSERT_ARG(diriter); + + errno = 0; + + do { + if ((de = readdir(diriter->dir)) == NULL) { + if (!errno) + return GIT_ITEROVER; + + git_error_set(GIT_ERROR_OS, + "could not read directory '%s'", diriter->path.ptr); + return -1; + } + } while (skip_dot && git_fs_path_is_dot_or_dotdot(de->d_name)); + + filename = de->d_name; + filename_len = strlen(filename); + +#ifdef GIT_I18N_ICONV + if ((diriter->flags & GIT_FS_PATH_DIR_PRECOMPOSE_UNICODE) != 0 && + (error = git_fs_path_iconv(&diriter->ic, &filename, &filename_len)) < 0) + return error; +#endif + + git_str_truncate(&diriter->path, diriter->parent_len); + + if (diriter->parent_len > 0 && + diriter->path.ptr[diriter->parent_len-1] != '/') + git_str_putc(&diriter->path, '/'); + + git_str_put(&diriter->path, filename, filename_len); + + if (git_str_oom(&diriter->path)) + return -1; + + return error; +} + +int git_fs_path_diriter_filename( + const char **out, + size_t *out_len, + git_fs_path_diriter *diriter) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(out_len); + GIT_ASSERT_ARG(diriter); + GIT_ASSERT(diriter->path.size > diriter->parent_len); + + *out = &diriter->path.ptr[diriter->parent_len+1]; + *out_len = diriter->path.size - diriter->parent_len - 1; + return 0; +} + +int git_fs_path_diriter_fullpath( + const char **out, + size_t *out_len, + git_fs_path_diriter *diriter) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(out_len); + GIT_ASSERT_ARG(diriter); + + *out = diriter->path.ptr; + *out_len = diriter->path.size; + return 0; +} + +int git_fs_path_diriter_stat(struct stat *out, git_fs_path_diriter *diriter) +{ + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(diriter); + + return git_fs_path_lstat(diriter->path.ptr, out); +} + +void git_fs_path_diriter_free(git_fs_path_diriter *diriter) +{ + if (diriter == NULL) + return; + + if (diriter->dir) { + closedir(diriter->dir); + diriter->dir = NULL; + } + +#ifdef GIT_I18N_ICONV + git_fs_path_iconv_clear(&diriter->ic); +#endif + + git_str_dispose(&diriter->path); +} + +#endif + +int git_fs_path_dirload( + git_vector *contents, + const char *path, + size_t prefix_len, + uint32_t flags) +{ + git_fs_path_diriter iter = GIT_FS_PATH_DIRITER_INIT; + const char *name; + size_t name_len; + char *dup; + int error; + + GIT_ASSERT_ARG(contents); + GIT_ASSERT_ARG(path); + + if ((error = git_fs_path_diriter_init(&iter, path, flags)) < 0) + return error; + + while ((error = git_fs_path_diriter_next(&iter)) == 0) { + if ((error = git_fs_path_diriter_fullpath(&name, &name_len, &iter)) < 0) + break; + + GIT_ASSERT(name_len > prefix_len); + + dup = git__strndup(name + prefix_len, name_len - prefix_len); + GIT_ERROR_CHECK_ALLOC(dup); + + if ((error = git_vector_insert(contents, dup)) < 0) + break; + } + + if (error == GIT_ITEROVER) + error = 0; + + git_fs_path_diriter_free(&iter); + return error; +} + +int git_fs_path_from_url_or_path(git_str *local_path_out, const char *url_or_path) +{ + if (git_fs_path_is_local_file_url(url_or_path)) + return git_fs_path_fromurl(local_path_out, url_or_path); + else + return git_str_sets(local_path_out, url_or_path); +} + +/* Reject paths like AUX or COM1, or those versions that end in a dot or + * colon. ("AUX." or "AUX:") + */ +GIT_INLINE(bool) validate_dospath( + const char *component, + size_t len, + const char dospath[3], + bool trailing_num) +{ + size_t last = trailing_num ? 4 : 3; + + if (len < last || git__strncasecmp(component, dospath, 3) != 0) + return true; + + if (trailing_num && (component[3] < '1' || component[3] > '9')) + return true; + + return (len > last && + component[last] != '.' && + component[last] != ':'); +} + +GIT_INLINE(bool) validate_char(unsigned char c, unsigned int flags) +{ + if ((flags & GIT_FS_PATH_REJECT_BACKSLASH) && c == '\\') + return false; + + if ((flags & GIT_FS_PATH_REJECT_SLASH) && c == '/') + return false; + + if (flags & GIT_FS_PATH_REJECT_NT_CHARS) { + if (c < 32) + return false; + + switch (c) { + case '<': + case '>': + case ':': + case '"': + case '|': + case '?': + case '*': + return false; + } + } + + return true; +} + +/* + * We fundamentally don't like some paths when dealing with user-inputted + * strings (to avoid escaping a sandbox): we don't want dot or dot-dot + * anywhere, we want to avoid writing weird paths on Windows that can't + * be handled by tools that use the non-\\?\ APIs, we don't want slashes + * or double slashes at the end of paths that can make them ambiguous. + * + * For checkout, we don't want to recurse into ".git" either. + */ +static bool validate_component( + const char *component, + size_t len, + unsigned int flags) +{ + if (len == 0) + return !(flags & GIT_FS_PATH_REJECT_EMPTY_COMPONENT); + + if ((flags & GIT_FS_PATH_REJECT_TRAVERSAL) && + len == 1 && component[0] == '.') + return false; + + if ((flags & GIT_FS_PATH_REJECT_TRAVERSAL) && + len == 2 && component[0] == '.' && component[1] == '.') + return false; + + if ((flags & GIT_FS_PATH_REJECT_TRAILING_DOT) && + component[len - 1] == '.') + return false; + + if ((flags & GIT_FS_PATH_REJECT_TRAILING_SPACE) && + component[len - 1] == ' ') + return false; + + if ((flags & GIT_FS_PATH_REJECT_TRAILING_COLON) && + component[len - 1] == ':') + return false; + + if (flags & GIT_FS_PATH_REJECT_DOS_PATHS) { + if (!validate_dospath(component, len, "CON", false) || + !validate_dospath(component, len, "PRN", false) || + !validate_dospath(component, len, "AUX", false) || + !validate_dospath(component, len, "NUL", false) || + !validate_dospath(component, len, "COM", true) || + !validate_dospath(component, len, "LPT", true)) + return false; + } + + return true; +} + +#ifdef GIT_WIN32 +GIT_INLINE(bool) validate_length( + const char *path, + size_t len, + size_t utf8_char_len) +{ + GIT_UNUSED(path); + GIT_UNUSED(len); + + return (utf8_char_len <= MAX_PATH); +} +#endif + +bool git_fs_path_str_is_valid_ext( + const git_str *path, + unsigned int flags, + bool (*validate_char_cb)(char ch, void *payload), + bool (*validate_component_cb)(const char *component, size_t len, void *payload), + bool (*validate_length_cb)(const char *path, size_t len, size_t utf8_char_len), + void *payload) +{ + const char *start, *c; + size_t len = 0; + + if (!flags) + return true; + + for (start = c = path->ptr; *c && len < path->size; c++, len++) { + if (!validate_char(*c, flags)) + return false; + + if (validate_char_cb && !validate_char_cb(*c, payload)) + return false; + + if (*c != '/') + continue; + + if (!validate_component(start, (c - start), flags)) + return false; + + if (validate_component_cb && + !validate_component_cb(start, (c - start), payload)) + return false; + + start = c + 1; + } + + /* + * We want to support paths specified as either `const char *` + * or `git_str *`; we pass size as `SIZE_MAX` when we use a + * `const char *` to avoid a `strlen`. Ensure that we didn't + * have a NUL in the buffer if there was a non-SIZE_MAX length. + */ + if (path->size != SIZE_MAX && len != path->size) + return false; + + if (!validate_component(start, (c - start), flags)) + return false; + + if (validate_component_cb && + !validate_component_cb(start, (c - start), payload)) + return false; + +#ifdef GIT_WIN32 + if ((flags & GIT_FS_PATH_REJECT_LONG_PATHS) != 0) { + size_t utf8_len = git_utf8_char_length(path->ptr, len); + + if (!validate_length(path->ptr, len, utf8_len)) + return false; + + if (validate_length_cb && + !validate_length_cb(path->ptr, len, utf8_len)) + return false; + } +#else + GIT_UNUSED(validate_length_cb); +#endif + + return true; +} + +int git_fs_path_validate_str_length_with_suffix( + git_str *path, + size_t suffix_len) +{ +#ifdef GIT_WIN32 + size_t utf8_len = git_utf8_char_length(path->ptr, path->size); + size_t total_len; + + if (GIT_ADD_SIZET_OVERFLOW(&total_len, utf8_len, suffix_len) || + total_len > MAX_PATH) { + + git_error_set(GIT_ERROR_FILESYSTEM, "path too long: '%.*s'", + (int)path->size, path->ptr); + return -1; + } +#else + GIT_UNUSED(path); + GIT_UNUSED(suffix_len); +#endif + + return 0; +} + +int git_fs_path_normalize_slashes(git_str *out, const char *path) +{ + int error; + char *p; + + if ((error = git_str_puts(out, path)) < 0) + return error; + + for (p = out->ptr; *p; p++) { + if (*p == '\\') + *p = '/'; + } + + return 0; +} + +bool git_fs_path_supports_symlinks(const char *dir) +{ + git_str path = GIT_STR_INIT; + bool supported = false; + struct stat st; + int fd; + + if ((fd = git_futils_mktmp(&path, dir, 0666)) < 0 || + p_close(fd) < 0 || + p_unlink(path.ptr) < 0 || + p_symlink("testing", path.ptr) < 0 || + p_lstat(path.ptr, &st) < 0) + goto done; + + supported = (S_ISLNK(st.st_mode) != 0); +done: + if (path.size) + (void)p_unlink(path.ptr); + git_str_dispose(&path); + return supported; +} + +static git_fs_path_owner_t mock_owner = GIT_FS_PATH_OWNER_NONE; + +void git_fs_path__set_owner(git_fs_path_owner_t owner) +{ + mock_owner = owner; +} + +#ifdef GIT_WIN32 +static PSID *sid_dup(PSID sid) +{ + DWORD len; + PSID dup; + + len = GetLengthSid(sid); + + if ((dup = git__malloc(len)) == NULL) + return NULL; + + if (!CopySid(len, dup, sid)) { + git_error_set(GIT_ERROR_OS, "could not duplicate sid"); + git__free(dup); + return NULL; + } + + return dup; +} + +static int current_user_sid(PSID *out) +{ + TOKEN_USER *info = NULL; + HANDLE token = NULL; + DWORD len = 0; + int error = -1; + + if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) { + git_error_set(GIT_ERROR_OS, "could not lookup process information"); + goto done; + } + + if (GetTokenInformation(token, TokenUser, NULL, 0, &len) || + GetLastError() != ERROR_INSUFFICIENT_BUFFER) { + git_error_set(GIT_ERROR_OS, "could not lookup token metadata"); + goto done; + } + + info = git__malloc(len); + GIT_ERROR_CHECK_ALLOC(info); + + if (!GetTokenInformation(token, TokenUser, info, len, &len)) { + git_error_set(GIT_ERROR_OS, "could not lookup current user"); + goto done; + } + + if ((*out = sid_dup(info->User.Sid))) + error = 0; + +done: + if (token) + CloseHandle(token); + + git__free(info); + return error; +} + +static int file_owner_sid(PSID *out, const char *path) +{ + git_win32_path path_w32; + PSECURITY_DESCRIPTOR descriptor = NULL; + PSID owner_sid; + DWORD ret; + int error = GIT_EINVALID; + + if (git_win32_path_from_utf8(path_w32, path) < 0) + return -1; + + ret = GetNamedSecurityInfoW(path_w32, SE_FILE_OBJECT, + OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, + &owner_sid, NULL, NULL, NULL, &descriptor); + + if (ret == ERROR_FILE_NOT_FOUND || ret == ERROR_PATH_NOT_FOUND) + error = GIT_ENOTFOUND; + else if (ret != ERROR_SUCCESS) + git_error_set(GIT_ERROR_OS, "failed to get security information"); + else if (!IsValidSid(owner_sid)) + git_error_set(GIT_ERROR_OS, "file owner is not valid"); + else if ((*out = sid_dup(owner_sid))) + error = 0; + + if (descriptor) + LocalFree(descriptor); + + return error; +} + +int git_fs_path_owner_is( + bool *out, + const char *path, + git_fs_path_owner_t owner_type) +{ + PSID owner_sid = NULL, user_sid = NULL; + BOOL is_admin, admin_owned; + int error; + + if (mock_owner) { + *out = ((mock_owner & owner_type) != 0); + return 0; + } + + if ((error = file_owner_sid(&owner_sid, path)) < 0) + goto done; + + if ((owner_type & GIT_FS_PATH_OWNER_CURRENT_USER) != 0) { + if ((error = current_user_sid(&user_sid)) < 0) + goto done; + + if (EqualSid(owner_sid, user_sid)) { + *out = true; + goto done; + } + } + + admin_owned = + IsWellKnownSid(owner_sid, WinBuiltinAdministratorsSid) || + IsWellKnownSid(owner_sid, WinLocalSystemSid); + + if (admin_owned && + (owner_type & GIT_FS_PATH_OWNER_ADMINISTRATOR) != 0) { + *out = true; + goto done; + } + + if (admin_owned && + (owner_type & GIT_FS_PATH_USER_IS_ADMINISTRATOR) != 0 && + CheckTokenMembership(NULL, owner_sid, &is_admin) && + is_admin) { + *out = true; + goto done; + } + + *out = false; + +done: + git__free(owner_sid); + git__free(user_sid); + return error; +} + +#else + +static int sudo_uid_lookup(uid_t *out) +{ + git_str uid_str = GIT_STR_INIT; + int64_t uid; + int error = -1; + + if (git__getenv(&uid_str, "SUDO_UID") == 0 && + git__strntol64(&uid, uid_str.ptr, uid_str.size, NULL, 10) == 0 && + uid == (int64_t)((uid_t)uid)) { + *out = (uid_t)uid; + error = 0; + } + + git_str_dispose(&uid_str); + return error; +} + +int git_fs_path_owner_is( + bool *out, + const char *path, + git_fs_path_owner_t owner_type) +{ + struct stat st; + uid_t euid, sudo_uid; + + if (mock_owner) { + *out = ((mock_owner & owner_type) != 0); + return 0; + } + + euid = geteuid(); + + if (p_lstat(path, &st) != 0) { + if (errno == ENOENT) + return GIT_ENOTFOUND; + + git_error_set(GIT_ERROR_OS, "could not stat '%s'", path); + return -1; + } + + if ((owner_type & GIT_FS_PATH_OWNER_CURRENT_USER) != 0 && + st.st_uid == euid) { + *out = true; + return 0; + } + + if ((owner_type & GIT_FS_PATH_OWNER_ADMINISTRATOR) != 0 && + st.st_uid == 0) { + *out = true; + return 0; + } + + if ((owner_type & GIT_FS_PATH_OWNER_RUNNING_SUDO) != 0 && + euid == 0 && + sudo_uid_lookup(&sudo_uid) == 0 && + st.st_uid == sudo_uid) { + *out = true; + return 0; + } + + *out = false; + return 0; +} + +#endif + +int git_fs_path_owner_is_current_user(bool *out, const char *path) +{ + return git_fs_path_owner_is(out, path, GIT_FS_PATH_OWNER_CURRENT_USER); +} + +int git_fs_path_owner_is_system(bool *out, const char *path) +{ + return git_fs_path_owner_is(out, path, GIT_FS_PATH_OWNER_ADMINISTRATOR); +} + +#ifdef GIT_WIN32 + +static int find_executable(git_str *fullpath, const char *executable) +{ + git_win32_path fullpath_w, executable_w; + int error; + + if (git_utf8_to_16(executable_w, GIT_WIN_PATH_MAX, executable) < 0) + return -1; + + error = git_win32_path_find_executable(fullpath_w, executable_w); + + if (error == 0) + error = git_str_put_w(fullpath, fullpath_w, wcslen(fullpath_w)); + + return error; +} + +#else + +static int find_executable(git_str *fullpath, const char *executable) +{ + git_str path = GIT_STR_INIT; + const char *current_dir, *term; + size_t current_dirlen; + bool found = false; + + if (git__getenv(&path, "PATH") < 0) + return -1; + + current_dir = path.ptr; + + while (*current_dir) { + if (! (term = strchr(current_dir, GIT_PATH_LIST_SEPARATOR))) + term = strchr(current_dir, '\0'); + + current_dirlen = term - current_dir; + git_str_clear(fullpath); + + /* An empty path segment is treated as '.' */ + if (current_dirlen == 0 && git_str_putc(fullpath, '.')) + return -1; + else if (current_dirlen != 0 && + git_str_put(fullpath, current_dir, current_dirlen) < 0) + return -1; + + if (git_str_putc(fullpath, '/') < 0 || + git_str_puts(fullpath, executable) < 0) + return -1; + + if (git_fs_path_isexecutable(fullpath->ptr)) { + found = true; + break; + } + + current_dir = term; + + if (*current_dir == GIT_PATH_LIST_SEPARATOR) + current_dir++; + } + + git_str_dispose(&path); + + if (found) + return 0; + + git_str_clear(fullpath); + return GIT_ENOTFOUND; +} + +#endif + +int git_fs_path_find_executable(git_str *fullpath, const char *executable) +{ + /* For qualified paths we do not look in PATH */ + if (strchr(executable, '/') != NULL) { + if (!git_fs_path_isexecutable(executable)) + return GIT_ENOTFOUND; + + return git_str_puts(fullpath, executable); + } + + return find_executable(fullpath, executable); +} diff --git a/src/util/fs_path.h b/src/util/fs_path.h new file mode 100644 index 00000000000..d6cadc7dd23 --- /dev/null +++ b/src/util/fs_path.h @@ -0,0 +1,819 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_fs_path_h__ +#define INCLUDE_fs_path_h__ + +#include "git2_util.h" + +#include "posix.h" +#include "str.h" +#include "vector.h" +#include "utf8.h" + +/** + * Path manipulation utils + * + * These are path utilities that munge paths without actually + * looking at the real filesystem. + */ + +/* + * The dirname() function shall take a pointer to a character string + * that contains a pathname, and return a pointer to a string that is a + * pathname of the parent directory of that file. Trailing '/' characters + * in the path are not counted as part of the path. + * + * If path does not contain a '/', then dirname() shall return a pointer to + * the string ".". If path is a null pointer or points to an empty string, + * dirname() shall return a pointer to the string "." . + * + * The `git_fs_path_dirname` implementation is thread safe. The returned + * string must be manually free'd. + * + * The `git_fs_path_dirname_r` implementation writes the dirname to a `git_str` + * if the buffer pointer is not NULL. + * It returns an error code < 0 if there is an allocation error, otherwise + * the length of the dirname (which will be > 0). + */ +extern char *git_fs_path_dirname(const char *path); +extern int git_fs_path_dirname_r(git_str *buffer, const char *path); + +/* + * This function returns the basename of the file, which is the last + * part of its full name given by fname, with the drive letter and + * leading directories stripped off. For example, the basename of + * c:/foo/bar/file.ext is file.ext, and the basename of a:foo is foo. + * + * Trailing slashes and backslashes are significant: the basename of + * c:/foo/bar/ is an empty string after the rightmost slash. + * + * The `git_fs_path_basename` implementation is thread safe. The returned + * string must be manually free'd. + * + * The `git_fs_path_basename_r` implementation writes the basename to a `git_str`. + * It returns an error code < 0 if there is an allocation error, otherwise + * the length of the basename (which will be >= 0). + */ +extern char *git_fs_path_basename(const char *path); +extern int git_fs_path_basename_r(git_str *buffer, const char *path); + +/* Return the offset of the start of the basename. Unlike the other + * basename functions, this returns 0 if the path is empty. + */ +extern size_t git_fs_path_basename_offset(git_str *buffer); + +/** + * Find offset to root of path if path has one. + * + * This will return a number >= 0 which is the offset to the start of the + * path, if the path is rooted (i.e. "/rooted/path" returns 0 and + * "c:/windows/rooted/path" returns 2). If the path is not rooted, this + * returns -1. + */ +extern int git_fs_path_root(const char *path); + +/** + * Ensure path has a trailing '/'. + */ +extern int git_fs_path_to_dir(git_str *path); + +/** + * Ensure string has a trailing '/' if there is space for it. + */ +extern void git_fs_path_string_to_dir(char *path, size_t size); + +/** + * Provides the length of the given path string with no trailing + * slashes. + */ +size_t git_fs_path_dirlen(const char *path); + +/** + * Returns nonzero if the given path is a filesystem root; on Windows, this + * means a drive letter (eg `A:/`, `C:\`). On POSIX this is `/`. + */ +GIT_INLINE(int) git_fs_path_is_root(const char *name) +{ +#ifdef GIT_WIN32 + if (((name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z')) && + name[1] == ':' && + (name[2] == '/' || name[2] == '\\') && + name[3] == '\0') + return 1; +#endif + + return (name[0] == '/' && name[1] == '\0'); +} + +/** + * Taken from git.git; returns nonzero if the given path is "." or "..". + */ +GIT_INLINE(int) git_fs_path_is_dot_or_dotdot(const char *name) +{ + return (name[0] == '.' && + (name[1] == '\0' || + (name[1] == '.' && name[2] == '\0'))); +} + +#ifdef GIT_WIN32 +GIT_INLINE(int) git_fs_path_is_dot_or_dotdotW(const wchar_t *name) +{ + return (name[0] == L'.' && + (name[1] == L'\0' || + (name[1] == L'.' && name[2] == L'\0'))); +} + +#define git_fs_path_is_absolute(p) \ + (git__isalpha((p)[0]) && (p)[1] == ':' && ((p)[2] == '\\' || (p)[2] == '/')) + +#define git_fs_path_is_dirsep(p) \ + ((p) == '/' || (p) == '\\') + +/** + * Convert backslashes in path to forward slashes. + */ +GIT_INLINE(void) git_fs_path_mkposix(char *path) +{ + while (*path) { + if (*path == '\\') + *path = '/'; + + path++; + } +} +#else +# define git_fs_path_mkposix(p) /* blank */ + +#define git_fs_path_is_absolute(p) \ + ((p)[0] == '/') + +#define git_fs_path_is_dirsep(p) \ + ((p) == '/') + +#endif + +/** + * Check if string is a relative path (i.e. starts with "./" or "../") + */ +GIT_INLINE(int) git_fs_path_is_relative(const char *p) +{ + return (p[0] == '.' && (p[1] == '/' || (p[1] == '.' && p[2] == '/'))); +} + +/** + * Check if string is at end of path segment (i.e. looking at '/' or '\0') + */ +GIT_INLINE(int) git_fs_path_at_end_of_segment(const char *p) +{ + return !*p || *p == '/'; +} + +extern int git__percent_decode(git_str *decoded_out, const char *input); + +/** + * Extract path from file:// URL. + */ +extern int git_fs_path_fromurl(git_str *local_path_out, const char *file_url); + + +/** + * Path filesystem utils + * + * These are path utilities that actually access the filesystem. + */ + +/** + * Check if a file exists and can be accessed. + * @return true or false + */ +extern bool git_fs_path_exists(const char *path); + +/** + * Check if the given path points to a directory. + * @return true or false + */ +extern bool git_fs_path_isdir(const char *path); + +/** + * Check if the given path points to a regular file. + * @return true or false + */ +extern bool git_fs_path_isfile(const char *path); + +/** + * Check if the given path points to an executable. + * @return true or false + */ +extern bool git_fs_path_isexecutable(const char *path); + +/** + * Check if the given path points to a symbolic link. + * @return true or false + */ +extern bool git_fs_path_islink(const char *path); + +/** + * Check if the given path is a directory, and is empty. + */ +extern bool git_fs_path_is_empty_dir(const char *path); + +/** + * Stat a file and/or link and set error if needed. + */ +extern int git_fs_path_lstat(const char *path, struct stat *st); + +/** + * Check if the parent directory contains the item. + * + * @param dir Directory to check. + * @param item Item that might be in the directory. + * @return 0 if item exists in directory, <0 otherwise. + */ +extern bool git_fs_path_contains(git_str *dir, const char *item); + +/** + * Check if the given path contains the given subdirectory. + * + * @param parent Directory path that might contain subdir + * @param subdir Subdirectory name to look for in parent + * @return true if subdirectory exists, false otherwise. + */ +extern bool git_fs_path_contains_dir(git_str *parent, const char *subdir); + +/** + * Determine the common directory length between two paths, including + * the final path separator. For example, given paths 'a/b/c/1.txt + * and 'a/b/c/d/2.txt', the common directory is 'a/b/c/', and this + * will return the length of the string 'a/b/c/', which is 6. + * + * @param one The first path + * @param two The second path + * @return The length of the common directory + */ +extern size_t git_fs_path_common_dirlen(const char *one, const char *two); + +/** + * Make the path relative to the given parent path. + * + * @param path The path to make relative + * @param parent The parent path to make path relative to + * @return 0 if path was made relative, GIT_ENOTFOUND + * if there was not common root between the paths, + * or <0. + */ +extern int git_fs_path_make_relative(git_str *path, const char *parent); + +/** + * Check if the given path contains the given file. + * + * @param dir Directory path that might contain file + * @param file File name to look for in parent + * @return true if file exists, false otherwise. + */ +extern bool git_fs_path_contains_file(git_str *dir, const char *file); + +/** + * Prepend base to unrooted path or just copy path over. + * + * This will optionally return the index into the path where the "root" + * is, either the end of the base directory prefix or the path root. + */ +extern int git_fs_path_join_unrooted( + git_str *path_out, const char *path, const char *base, ssize_t *root_at); + +/** + * Removes multiple occurrences of '/' in a row, squashing them into a + * single '/'. + */ +extern void git_fs_path_squash_slashes(git_str *path); + +/** + * Clean up path, prepending base if it is not already rooted. + */ +extern int git_fs_path_prettify(git_str *path_out, const char *path, const char *base); + +/** + * Clean up path, prepending base if it is not already rooted and + * appending a slash. + */ +extern int git_fs_path_prettify_dir(git_str *path_out, const char *path, const char *base); + +/** + * Get a directory from a path. + * + * If path is a directory, this acts like `git_fs_path_prettify_dir` + * (cleaning up path and appending a '/'). If path is a normal file, + * this prettifies it, then removed the filename a la dirname and + * appends the trailing '/'. If the path does not exist, it is + * treated like a regular filename. + */ +extern int git_fs_path_find_dir(git_str *dir); + +/** + * Resolve relative references within a path. + * + * This eliminates "./" and "../" relative references inside a path, + * as well as condensing multiple slashes into single ones. It will + * not touch the path before the "ceiling" length. + * + * Additionally, this will recognize an "c:/" drive prefix or a "xyz://" URL + * prefix and not touch that part of the path. + */ +extern int git_fs_path_resolve_relative(git_str *path, size_t ceiling); + +/** + * Apply a relative path to base path. + * + * Note that the base path could be a filename or a URL and this + * should still work. The relative path is walked segment by segment + * with three rules: series of slashes will be condensed to a single + * slash, "." will be eaten with no change, and ".." will remove a + * segment from the base path. + */ +extern int git_fs_path_apply_relative(git_str *target, const char *relpath); + +enum { + GIT_FS_PATH_DIR_IGNORE_CASE = (1u << 0), + GIT_FS_PATH_DIR_PRECOMPOSE_UNICODE = (1u << 1), + GIT_FS_PATH_DIR_INCLUDE_DOT_AND_DOTDOT = (1u << 2), +}; + +/** + * Walk each directory entry, except '.' and '..', calling fn(state). + * + * @param pathbuf Buffer the function reads the initial directory + * path from, and updates with each successive entry's name. + * @param flags Combination of GIT_FS_PATH_DIR flags. + * @param callback Callback for each entry. Passed the `payload` and each + * successive path inside the directory as a full path. This may + * safely append text to the pathbuf if needed. Return non-zero to + * cancel iteration (and return value will be propagated back). + * @param payload Passed to callback as first argument. + * @return 0 on success or error code from OS error or from callback + */ +extern int git_fs_path_direach( + git_str *pathbuf, + uint32_t flags, + int (*callback)(void *payload, git_str *path), + void *payload); + +/** + * Sort function to order two paths + */ +extern int git_fs_path_cmp( + const char *name1, size_t len1, int isdir1, + const char *name2, size_t len2, int isdir2, + int (*compare)(const char *, const char *, size_t)); + +/** + * Invoke callback up path directory by directory until the ceiling is + * reached (inclusive of a final call at the root_path). + * + * Returning anything other than 0 from the callback function + * will stop the iteration and propagate the error to the caller. + * + * @param pathbuf Buffer the function reads the directory from and + * and updates with each successive name. + * @param ceiling Prefix of path at which to stop walking up. If NULL, + * this will walk all the way up to the root. If not a prefix of + * pathbuf, the callback will be invoked a single time on the + * original input path. + * @param callback Function to invoke on each path. Passed the `payload` + * and the buffer containing the current path. The path should not + * be modified in any way. Return non-zero to stop iteration. + * @param payload Passed to fn as the first ath. + */ +extern int git_fs_path_walk_up( + git_str *pathbuf, + const char *ceiling, + int (*callback)(void *payload, const char *path), + void *payload); + + +enum { + GIT_FS_PATH_NOTEQUAL = 0, + GIT_FS_PATH_EQUAL = 1, + GIT_FS_PATH_PREFIX = 2 +}; + +/* + * Determines if a path is equal to or potentially a child of another. + * @param parent The possible parent + * @param child The possible child + */ +GIT_INLINE(int) git_fs_path_equal_or_prefixed( + const char *parent, + const char *child, + ssize_t *prefixlen) +{ + const char *p = parent, *c = child; + int lastslash = 0; + + while (*p && *c) { + lastslash = (*p == '/'); + + if (*p++ != *c++) + return GIT_FS_PATH_NOTEQUAL; + } + + if (*p != '\0') + return GIT_FS_PATH_NOTEQUAL; + + if (*c == '\0') { + if (prefixlen) + *prefixlen = p - parent; + + return GIT_FS_PATH_EQUAL; + } + + if (*c == '/' || lastslash) { + if (prefixlen) + *prefixlen = (p - parent) - lastslash; + + return GIT_FS_PATH_PREFIX; + } + + return GIT_FS_PATH_NOTEQUAL; +} + +/* translate errno to libgit2 error code and set error message */ +extern int git_fs_path_set_error( + int errno_value, const char *path, const char *action); + +/* check if non-ascii characters are present in filename */ +extern bool git_fs_path_has_non_ascii(const char *path, size_t pathlen); + +#define GIT_PATH_REPO_ENCODING "UTF-8" + +#ifdef __APPLE__ +#define GIT_PATH_NATIVE_ENCODING "UTF-8-MAC" +#else +#define GIT_PATH_NATIVE_ENCODING "UTF-8" +#endif + +#ifdef GIT_I18N_ICONV + +#include + +typedef struct { + iconv_t map; + git_str buf; +} git_fs_path_iconv_t; + +#define GIT_PATH_ICONV_INIT { (iconv_t)-1, GIT_STR_INIT } + +/* Init iconv data for converting decomposed UTF-8 to precomposed */ +extern int git_fs_path_iconv_init_precompose(git_fs_path_iconv_t *ic); + +/* Clear allocated iconv data */ +extern void git_fs_path_iconv_clear(git_fs_path_iconv_t *ic); + +/* + * Rewrite `in` buffer using iconv map if necessary, replacing `in` + * pointer internal iconv buffer if rewrite happened. The `in` pointer + * will be left unchanged if no rewrite was needed. + */ +extern int git_fs_path_iconv(git_fs_path_iconv_t *ic, const char **in, size_t *inlen); + +#endif /* GIT_I18N_ICONV */ + +extern bool git_fs_path_does_decompose_unicode(const char *root); + + +typedef struct git_fs_path_diriter git_fs_path_diriter; + +#if defined(GIT_WIN32) && !defined(__MINGW32__) + +struct git_fs_path_diriter +{ + git_win32_path path; + size_t parent_len; + + git_str path_utf8; + size_t parent_utf8_len; + + HANDLE handle; + + unsigned int flags; + + WIN32_FIND_DATAW current; + unsigned int needs_next; +}; + +#define GIT_FS_PATH_DIRITER_INIT { {0}, 0, GIT_STR_INIT, 0, INVALID_HANDLE_VALUE } + +#else + +struct git_fs_path_diriter +{ + git_str path; + size_t parent_len; + + unsigned int flags; + + DIR *dir; + +#ifdef GIT_I18N_ICONV + git_fs_path_iconv_t ic; +#endif +}; + +#define GIT_FS_PATH_DIRITER_INIT { GIT_STR_INIT } + +#endif + +/** + * Initialize a directory iterator. + * + * @param diriter Pointer to a diriter structure that will be setup. + * @param path The path that will be iterated over + * @param flags Directory reader flags + * @return 0 or an error code + */ +extern int git_fs_path_diriter_init( + git_fs_path_diriter *diriter, + const char *path, + unsigned int flags); + +/** + * Advance the directory iterator. Will return GIT_ITEROVER when + * the iteration has completed successfully. + * + * @param diriter The directory iterator + * @return 0, GIT_ITEROVER, or an error code + */ +extern int git_fs_path_diriter_next(git_fs_path_diriter *diriter); + +/** + * Returns the file name of the current item in the iterator. + * + * @param out Pointer to store the path in + * @param out_len Pointer to store the length of the path in + * @param diriter The directory iterator + * @return 0 or an error code + */ +extern int git_fs_path_diriter_filename( + const char **out, + size_t *out_len, + git_fs_path_diriter *diriter); + +/** + * Returns the full path of the current item in the iterator; that + * is the current filename plus the path of the directory that the + * iterator was constructed with. + * + * @param out Pointer to store the path in + * @param out_len Pointer to store the length of the path in + * @param diriter The directory iterator + * @return 0 or an error code + */ +extern int git_fs_path_diriter_fullpath( + const char **out, + size_t *out_len, + git_fs_path_diriter *diriter); + +/** + * Performs an `lstat` on the current item in the iterator. + * + * @param out Pointer to store the stat data in + * @param diriter The directory iterator + * @return 0 or an error code + */ +extern int git_fs_path_diriter_stat(struct stat *out, git_fs_path_diriter *diriter); + +/** + * Closes the directory iterator. + * + * @param diriter The directory iterator + */ +extern void git_fs_path_diriter_free(git_fs_path_diriter *diriter); + +/** + * Load all directory entries (except '.' and '..') into a vector. + * + * For cases where `git_fs_path_direach()` is not appropriate, this + * allows you to load the filenames in a directory into a vector + * of strings. That vector can then be sorted, iterated, or whatever. + * Remember to free alloc of the allocated strings when you are done. + * + * @param contents Vector to fill with directory entry names. + * @param path The directory to read from. + * @param prefix_len When inserting entries, the trailing part of path + * will be prefixed after this length. I.e. given path "/a/b" and + * prefix_len 3, the entries will look like "b/e1", "b/e2", etc. + * @param flags Combination of GIT_FS_PATH_DIR flags. + */ +extern int git_fs_path_dirload( + git_vector *contents, + const char *path, + size_t prefix_len, + uint32_t flags); + + +/* Used for paths to repositories on the filesystem */ +extern bool git_fs_path_is_local_file_url(const char *file_url); +extern int git_fs_path_from_url_or_path(git_str *local_path_out, const char *url_or_path); + +/* Flags to determine path validity in `git_fs_path_isvalid` */ +#define GIT_FS_PATH_REJECT_EMPTY_COMPONENT (1 << 0) +#define GIT_FS_PATH_REJECT_TRAVERSAL (1 << 1) +#define GIT_FS_PATH_REJECT_SLASH (1 << 2) +#define GIT_FS_PATH_REJECT_BACKSLASH (1 << 3) +#define GIT_FS_PATH_REJECT_TRAILING_DOT (1 << 4) +#define GIT_FS_PATH_REJECT_TRAILING_SPACE (1 << 5) +#define GIT_FS_PATH_REJECT_TRAILING_COLON (1 << 6) +#define GIT_FS_PATH_REJECT_DOS_PATHS (1 << 7) +#define GIT_FS_PATH_REJECT_NT_CHARS (1 << 8) +#define GIT_FS_PATH_REJECT_LONG_PATHS (1 << 9) + +#define GIT_FS_PATH_REJECT_MAX (1 << 9) + +/* Default path safety for writing files to disk: since we use the + * Win32 "File Namespace" APIs ("\\?\") we need to protect from + * paths that the normal Win32 APIs would not write. + */ +#ifdef GIT_WIN32 +# define GIT_FS_PATH_REJECT_FILESYSTEM_DEFAULTS \ + GIT_FS_PATH_REJECT_EMPTY_COMPONENT | \ + GIT_FS_PATH_REJECT_TRAVERSAL | \ + GIT_FS_PATH_REJECT_BACKSLASH | \ + GIT_FS_PATH_REJECT_TRAILING_DOT | \ + GIT_FS_PATH_REJECT_TRAILING_SPACE | \ + GIT_FS_PATH_REJECT_TRAILING_COLON | \ + GIT_FS_PATH_REJECT_DOS_PATHS | \ + GIT_FS_PATH_REJECT_NT_CHARS +#else +# define GIT_FS_PATH_REJECT_FILESYSTEM_DEFAULTS \ + GIT_FS_PATH_REJECT_EMPTY_COMPONENT | \ + GIT_FS_PATH_REJECT_TRAVERSAL +#endif + +/** + * Validate a filesystem path; with custom callbacks per-character and + * per-path component. + */ +extern bool git_fs_path_str_is_valid_ext( + const git_str *path, + unsigned int flags, + bool (*validate_char_cb)(char ch, void *payload), + bool (*validate_component_cb)(const char *component, size_t len, void *payload), + bool (*validate_length_cb)(const char *component, size_t len, size_t utf8_char_len), + void *payload); + +GIT_INLINE(bool) git_fs_path_is_valid_ext( + const char *path, + unsigned int flags, + bool (*validate_char_cb)(char ch, void *payload), + bool (*validate_component_cb)(const char *component, size_t len, void *payload), + bool (*validate_length_cb)(const char *component, size_t len, size_t utf8_char_len), + void *payload) +{ + const git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX); + return git_fs_path_str_is_valid_ext( + &str, + flags, + validate_char_cb, + validate_component_cb, + validate_length_cb, + payload); +} + +/** + * Validate a filesystem path. This ensures that the given path is legal + * and does not contain any "unsafe" components like path traversal ('.' + * or '..'), characters that are inappropriate for lesser filesystems + * (trailing ' ' or ':' characters), or filenames ("component names") + * that are not supported ('AUX', 'COM1"). + */ +GIT_INLINE(bool) git_fs_path_is_valid( + const char *path, + unsigned int flags) +{ + const git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX); + return git_fs_path_str_is_valid_ext(&str, flags, NULL, NULL, NULL, NULL); +} + +/** Validate a filesystem path in a `git_str`. */ +GIT_INLINE(bool) git_fs_path_str_is_valid( + const git_str *path, + unsigned int flags) +{ + return git_fs_path_str_is_valid_ext(path, flags, NULL, NULL, NULL, NULL); +} + +extern int git_fs_path_validate_str_length_with_suffix( + git_str *path, + size_t suffix_len); + +/** + * Validate an on-disk path, taking into account that it will have a + * suffix appended (eg, `.lock`). + */ +GIT_INLINE(int) git_fs_path_validate_filesystem_with_suffix( + const char *path, + size_t path_len, + size_t suffix_len) +{ +#ifdef GIT_WIN32 + size_t path_chars, total_chars; + + path_chars = git_utf8_char_length(path, path_len); + + if (GIT_ADD_SIZET_OVERFLOW(&total_chars, path_chars, suffix_len) || + total_chars > MAX_PATH) { + git_error_set(GIT_ERROR_FILESYSTEM, "path too long: '%s'", path); + return -1; + } + return 0; +#else + GIT_UNUSED(path); + GIT_UNUSED(path_len); + GIT_UNUSED(suffix_len); + return 0; +#endif +} + +/** + * Validate an path on the filesystem. This ensures that the given + * path is valid for the operating system/platform; for example, this + * will ensure that the given absolute path is smaller than MAX_PATH on + * Windows. + * + * For paths within the working directory, you should use ensure that + * `core.longpaths` is obeyed. Use `git_fs_path_validate_workdir`. + */ +GIT_INLINE(int) git_fs_path_validate_filesystem( + const char *path, + size_t path_len) +{ + return git_fs_path_validate_filesystem_with_suffix(path, path_len, 0); +} + +/** + * Convert any backslashes into slashes + */ +int git_fs_path_normalize_slashes(git_str *out, const char *path); + +bool git_fs_path_supports_symlinks(const char *dir); + +typedef enum { + GIT_FS_PATH_OWNER_NONE = 0, + + /** The file must be owned by the current user. */ + GIT_FS_PATH_OWNER_CURRENT_USER = (1 << 0), + + /** The file must be owned by the system account. */ + GIT_FS_PATH_OWNER_ADMINISTRATOR = (1 << 1), + + /** + * The file may be owned by a system account if the current + * user is in an administrator group. Windows only; this is + * a noop on non-Windows systems. + */ + GIT_FS_PATH_USER_IS_ADMINISTRATOR = (1 << 2), + + /** + * The file is owned by the current user, who is running `sudo`. + */ + GIT_FS_PATH_OWNER_RUNNING_SUDO = (1 << 3), + + /** The file may be owned by another user. */ + GIT_FS_PATH_OWNER_OTHER = (1 << 4) +} git_fs_path_owner_t; + +/** + * Sets the mock ownership for files; subsequent calls to + * `git_fs_path_owner_is_*` functions will return this data until + * cleared with `GIT_FS_PATH_OWNER_NONE`. + */ +void git_fs_path__set_owner(git_fs_path_owner_t owner); + +/** Verify that the file in question is owned by the given owner. */ +int git_fs_path_owner_is( + bool *out, + const char *path, + git_fs_path_owner_t owner_type); + +/** + * Verify that the file in question is owned by an administrator or system + * account. + */ +int git_fs_path_owner_is_system(bool *out, const char *path); + +/** + * Verify that the file in question is owned by the current user; + */ + +int git_fs_path_owner_is_current_user(bool *out, const char *path); + +/** + * Search the current PATH for the given executable, returning the full + * path if it is found. + */ +int git_fs_path_find_executable(git_str *fullpath, const char *executable); + +#endif diff --git a/src/util/futils.c b/src/util/futils.c new file mode 100644 index 00000000000..25c3a1be180 --- /dev/null +++ b/src/util/futils.c @@ -0,0 +1,1237 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "futils.h" + +#include "runtime.h" +#include "hash.h" +#include "rand.h" +#include "hashmap_str.h" + +#include + +#define GIT_FILEMODE_DEFAULT 0100666 + +int git_futils_mkpath2file(const char *file_path, const mode_t mode) +{ + return git_futils_mkdir( + file_path, mode, + GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR); +} + +int git_futils_mktmp(git_str *path_out, const char *filename, mode_t mode) +{ + const int open_flags = O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC; + unsigned int tries = 32; + int fd; + + while (tries--) { + uint64_t rand = git_rand_next(); + + git_str_sets(path_out, filename); + git_str_puts(path_out, "_git2_"); + git_str_encode_hexstr(path_out, (void *)&rand, sizeof(uint64_t)); + + if (git_str_oom(path_out)) + return -1; + + /* Note that we open with O_CREAT | O_EXCL */ + if ((fd = p_open(path_out->ptr, open_flags, mode)) >= 0) + return fd; + } + + git_error_set(GIT_ERROR_OS, + "failed to create temporary file '%s'", path_out->ptr); + git_str_dispose(path_out); + return -1; +} + +int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode) +{ + int fd; + + if (git_futils_mkpath2file(path, dirmode) < 0) + return -1; + + fd = p_creat(path, mode); + if (fd < 0) { + git_error_set(GIT_ERROR_OS, "failed to create file '%s'", path); + return -1; + } + + return fd; +} + +int git_futils_creat_locked(const char *path, const mode_t mode) +{ + int fd = p_open(path, O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, + mode); + + if (fd < 0) { + int error = errno; + git_error_set(GIT_ERROR_OS, "failed to create locked file '%s'", path); + switch (error) { + case EEXIST: + return GIT_ELOCKED; + case ENOENT: + return GIT_ENOTFOUND; + default: + return -1; + } + } + + return fd; +} + +int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode) +{ + if (git_futils_mkpath2file(path, dirmode) < 0) + return -1; + + return git_futils_creat_locked(path, mode); +} + +int git_futils_open_ro(const char *path) +{ + int fd = p_open(path, O_RDONLY); + if (fd < 0) + return git_fs_path_set_error(errno, path, "open"); + return fd; +} + +int git_futils_truncate(const char *path, int mode) +{ + int fd = p_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode); + if (fd < 0) + return git_fs_path_set_error(errno, path, "open"); + + close(fd); + return 0; +} + +int git_futils_filesize(uint64_t *out, git_file fd) +{ + struct stat sb; + + if (p_fstat(fd, &sb)) { + git_error_set(GIT_ERROR_OS, "failed to stat file descriptor"); + return -1; + } + + if (sb.st_size < 0) { + git_error_set(GIT_ERROR_INVALID, "invalid file size"); + return -1; + } + + *out = sb.st_size; + return 0; +} + +mode_t git_futils_canonical_mode(mode_t raw_mode) +{ + if (S_ISREG(raw_mode)) + return S_IFREG | GIT_PERMS_CANONICAL(raw_mode); + else if (S_ISLNK(raw_mode)) + return S_IFLNK; + else if (S_ISGITLINK(raw_mode)) + return S_IFGITLINK; + else if (S_ISDIR(raw_mode)) + return S_IFDIR; + else + return 0; +} + +int git_futils_readbuffer_fd(git_str *buf, git_file fd, size_t len) +{ + ssize_t read_size = 0; + size_t alloc_len; + + git_str_clear(buf); + + if (!git__is_ssizet(len)) { + git_error_set(GIT_ERROR_INVALID, "read too large"); + return -1; + } + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, len, 1); + if (git_str_grow(buf, alloc_len) < 0) + return -1; + + /* p_read loops internally to read len bytes */ + read_size = p_read(fd, buf->ptr, len); + + if (read_size < 0) { + git_error_set(GIT_ERROR_OS, "failed to read descriptor"); + git_str_dispose(buf); + return -1; + } + + if ((size_t)read_size != len) { + git_error_set(GIT_ERROR_FILESYSTEM, "could not read (expected %" PRIuZ " bytes, read %" PRIuZ ")", len, (size_t)read_size); + git_str_dispose(buf); + return -1; + } + + buf->ptr[read_size] = '\0'; + buf->size = read_size; + + return 0; +} + +int git_futils_readbuffer_fd_full(git_str *buf, git_file fd) +{ + static size_t blocksize = 10240; + size_t alloc_len = 0, total_size = 0; + ssize_t read_size = 0; + + git_str_clear(buf); + + while (true) { + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, blocksize); + + if (git_str_grow(buf, alloc_len) < 0) + return -1; + + /* p_read loops internally to read blocksize bytes */ + read_size = p_read(fd, buf->ptr, blocksize); + + if (read_size < 0) { + git_error_set(GIT_ERROR_OS, "failed to read descriptor"); + git_str_dispose(buf); + return -1; + } + + total_size += read_size; + + if ((size_t)read_size < blocksize) { + break; + } + } + + buf->ptr[total_size] = '\0'; + buf->size = total_size; + + return 0; +} + +int git_futils_readbuffer_updated( + git_str *out, + const char *path, + unsigned char checksum[GIT_HASH_SHA256_SIZE], + int *updated) +{ + int error; + git_file fd; + struct stat st; + git_str buf = GIT_STR_INIT; + unsigned char checksum_new[GIT_HASH_SHA256_SIZE]; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(path && *path); + + if (updated != NULL) + *updated = 0; + + if (p_stat(path, &st) < 0) + return git_fs_path_set_error(errno, path, "stat"); + + + if (S_ISDIR(st.st_mode)) { + git_error_set(GIT_ERROR_INVALID, "requested file is a directory"); + return GIT_ENOTFOUND; + } + + if (!git__is_sizet(st.st_size+1)) { + git_error_set(GIT_ERROR_OS, "invalid regular file stat for '%s'", path); + return -1; + } + + if ((fd = git_futils_open_ro(path)) < 0) + return fd; + + if (git_futils_readbuffer_fd(&buf, fd, (size_t)st.st_size) < 0) { + p_close(fd); + return -1; + } + + p_close(fd); + + if (checksum) { + error = git_hash_buf(checksum_new, buf.ptr, + buf.size, GIT_HASH_ALGORITHM_SHA256); + + if (error < 0) { + git_str_dispose(&buf); + return error; + } + + /* + * If we were given a checksum, we only want to use it if it's different + */ + if (!memcmp(checksum, checksum_new, GIT_HASH_SHA256_SIZE)) { + git_str_dispose(&buf); + if (updated) + *updated = 0; + + return 0; + } + + memcpy(checksum, checksum_new, GIT_HASH_SHA256_SIZE); + } + + /* + * If we're here, the file did change, or the user didn't have an old version + */ + if (updated != NULL) + *updated = 1; + + git_str_swap(out, &buf); + git_str_dispose(&buf); + + return 0; +} + +int git_futils_readbuffer(git_str *buf, const char *path) +{ + return git_futils_readbuffer_updated(buf, path, NULL, NULL); +} + +int git_futils_writebuffer( + const git_str *buf, const char *path, int flags, mode_t mode) +{ + int fd, do_fsync = 0, error = 0; + + if (!flags) + flags = O_CREAT | O_TRUNC | O_WRONLY; + + if ((flags & O_FSYNC) != 0) + do_fsync = 1; + + flags &= ~O_FSYNC; + + if (!mode) + mode = GIT_FILEMODE_DEFAULT; + + if ((fd = p_open(path, flags, mode)) < 0) { + git_error_set(GIT_ERROR_OS, "could not open '%s' for writing", path); + return fd; + } + + if ((error = p_write(fd, git_str_cstr(buf), git_str_len(buf))) < 0) { + git_error_set(GIT_ERROR_OS, "could not write to '%s'", path); + (void)p_close(fd); + return error; + } + + if (do_fsync && (error = p_fsync(fd)) < 0) { + git_error_set(GIT_ERROR_OS, "could not fsync '%s'", path); + p_close(fd); + return error; + } + + if ((error = p_close(fd)) < 0) { + git_error_set(GIT_ERROR_OS, "error while closing '%s'", path); + return error; + } + + if (do_fsync && (flags & O_CREAT)) + error = git_futils_fsync_parent(path); + + return error; +} + +int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode) +{ + if (git_futils_mkpath2file(to, dirmode) < 0) + return -1; + + if (p_rename(from, to) < 0) { + git_error_set(GIT_ERROR_OS, "failed to rename '%s' to '%s'", from, to); + return -1; + } + + return 0; +} + +int git_futils_mmap_ro(git_map *out, git_file fd, off64_t begin, size_t len) +{ + return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin); +} + +int git_futils_mmap_ro_file(git_map *out, const char *path) +{ + git_file fd = git_futils_open_ro(path); + uint64_t len; + int result; + + if (fd < 0) + return fd; + + if ((result = git_futils_filesize(&len, fd)) < 0) + goto out; + + if (!git__is_sizet(len)) { + git_error_set(GIT_ERROR_OS, "file `%s` too large to mmap", path); + result = -1; + goto out; + } + + result = git_futils_mmap_ro(out, fd, 0, (size_t)len); +out: + p_close(fd); + return result; +} + +void git_futils_mmap_free(git_map *out) +{ + p_munmap(out); +} + +GIT_INLINE(int) mkdir_validate_dir( + const char *path, + struct stat *st, + mode_t mode, + uint32_t flags, + struct git_futils_mkdir_options *opts) +{ + /* with exclusive create, existing dir is an error */ + if ((flags & GIT_MKDIR_EXCL) != 0) { + git_error_set(GIT_ERROR_FILESYSTEM, + "failed to make directory '%s': directory exists", path); + return GIT_EEXISTS; + } + + if ((S_ISREG(st->st_mode) && (flags & GIT_MKDIR_REMOVE_FILES)) || + (S_ISLNK(st->st_mode) && (flags & GIT_MKDIR_REMOVE_SYMLINKS))) { + if (p_unlink(path) < 0) { + git_error_set(GIT_ERROR_OS, "failed to remove %s '%s'", + S_ISLNK(st->st_mode) ? "symlink" : "file", path); + return GIT_EEXISTS; + } + + opts->perfdata.mkdir_calls++; + + if (p_mkdir(path, mode) < 0) { + git_error_set(GIT_ERROR_OS, "failed to make directory '%s'", path); + return GIT_EEXISTS; + } + } + + else if (S_ISLNK(st->st_mode)) { + /* Re-stat the target, make sure it's a directory */ + opts->perfdata.stat_calls++; + + if (p_stat(path, st) < 0) { + git_error_set(GIT_ERROR_OS, "failed to make directory '%s'", path); + return GIT_EEXISTS; + } + } + + else if (!S_ISDIR(st->st_mode)) { + git_error_set(GIT_ERROR_FILESYSTEM, + "failed to make directory '%s': directory exists", path); + return GIT_EEXISTS; + } + + return 0; +} + +GIT_INLINE(int) mkdir_validate_mode( + const char *path, + struct stat *st, + bool terminal_path, + mode_t mode, + uint32_t flags, + struct git_futils_mkdir_options *opts) +{ + if (((terminal_path && (flags & GIT_MKDIR_CHMOD) != 0) || + (flags & GIT_MKDIR_CHMOD_PATH) != 0) && st->st_mode != mode) { + + opts->perfdata.chmod_calls++; + + if (p_chmod(path, mode) < 0) { + git_error_set(GIT_ERROR_OS, "failed to set permissions on '%s'", path); + return -1; + } + } + + return 0; +} + +GIT_INLINE(int) mkdir_canonicalize( + git_str *path, + uint32_t flags) +{ + ssize_t root_len; + + if (path->size == 0) { + git_error_set(GIT_ERROR_OS, "attempt to create empty path"); + return -1; + } + + /* Trim trailing slashes (except the root) */ + if ((root_len = git_fs_path_root(path->ptr)) < 0) + root_len = 0; + else + root_len++; + + while (path->size > (size_t)root_len && path->ptr[path->size - 1] == '/') + path->ptr[--path->size] = '\0'; + + /* if we are not supposed to made the last element, truncate it */ + if ((flags & GIT_MKDIR_SKIP_LAST2) != 0) { + git_fs_path_dirname_r(path, path->ptr); + flags |= GIT_MKDIR_SKIP_LAST; + } + if ((flags & GIT_MKDIR_SKIP_LAST) != 0) { + git_fs_path_dirname_r(path, path->ptr); + } + + /* We were either given the root path (or trimmed it to + * the root), we don't have anything to do. + */ + if (path->size <= (size_t)root_len) + git_str_clear(path); + + return 0; +} + +int git_futils_mkdir( + const char *path, + mode_t mode, + uint32_t flags) +{ + git_str make_path = GIT_STR_INIT, parent_path = GIT_STR_INIT; + const char *relative; + struct git_futils_mkdir_options opts = { 0 }; + struct stat st; + size_t depth = 0; + int len = 0, root_len, error; + + if ((error = git_str_puts(&make_path, path)) < 0 || + (error = mkdir_canonicalize(&make_path, flags)) < 0 || + (error = git_str_puts(&parent_path, make_path.ptr)) < 0 || + make_path.size == 0) + goto done; + + root_len = git_fs_path_root(make_path.ptr); + + /* find the first parent directory that exists. this will be used + * as the base to dirname_relative. + */ + for (relative = make_path.ptr; parent_path.size; ) { + error = p_lstat(parent_path.ptr, &st); + + if (error == 0) { + break; + } else if (errno != ENOENT) { + git_error_set(GIT_ERROR_OS, "failed to stat '%s'", parent_path.ptr); + error = -1; + goto done; + } + + depth++; + + /* examine the parent of the current path */ + if ((len = git_fs_path_dirname_r(&parent_path, parent_path.ptr)) < 0) { + error = len; + goto done; + } + + GIT_ASSERT(len); + + /* + * We've walked all the given path's parents and it's either relative + * (the parent is simply '.') or rooted (the length is less than or + * equal to length of the root path). The path may be less than the + * root path length on Windows, where `C:` == `C:/`. + */ + if ((len == 1 && parent_path.ptr[0] == '.') || + (len == 1 && parent_path.ptr[0] == '/') || + len <= root_len) { + relative = make_path.ptr; + break; + } + + relative = make_path.ptr + len + 1; + + /* not recursive? just make this directory relative to its parent. */ + if ((flags & GIT_MKDIR_PATH) == 0) + break; + } + + /* we found an item at the location we're trying to create, + * validate it. + */ + if (depth == 0) { + error = mkdir_validate_dir(make_path.ptr, &st, mode, flags, &opts); + + if (!error) + error = mkdir_validate_mode( + make_path.ptr, &st, true, mode, flags, &opts); + + goto done; + } + + /* we already took `SKIP_LAST` and `SKIP_LAST2` into account when + * canonicalizing `make_path`. + */ + flags &= ~(GIT_MKDIR_SKIP_LAST2 | GIT_MKDIR_SKIP_LAST); + + error = git_futils_mkdir_relative(relative, + parent_path.size ? parent_path.ptr : NULL, mode, flags, &opts); + +done: + git_str_dispose(&make_path); + git_str_dispose(&parent_path); + return error; +} + +int git_futils_mkdir_r(const char *path, const mode_t mode) +{ + return git_futils_mkdir(path, mode, GIT_MKDIR_PATH); +} + +int git_futils_mkdir_relative( + const char *relative_path, + const char *base, + mode_t mode, + uint32_t flags, + struct git_futils_mkdir_options *opts) +{ + git_str make_path = GIT_STR_INIT; + ssize_t root = 0, min_root_len; + char lastch = '/', *tail; + struct stat st; + struct git_futils_mkdir_options empty_opts = {0}; + int error; + + if (!opts) + opts = &empty_opts; + + /* build path and find "root" where we should start calling mkdir */ + if (git_fs_path_join_unrooted(&make_path, relative_path, base, &root) < 0) + return -1; + + if ((error = mkdir_canonicalize(&make_path, flags)) < 0 || + make_path.size == 0) + goto done; + + /* if we are not supposed to make the whole path, reset root */ + if ((flags & GIT_MKDIR_PATH) == 0) + root = git_str_rfind(&make_path, '/'); + + /* advance root past drive name or network mount prefix */ + min_root_len = git_fs_path_root(make_path.ptr); + if (root < min_root_len) + root = min_root_len; + while (root >= 0 && make_path.ptr[root] == '/') + ++root; + + /* clip root to make_path length */ + if (root > (ssize_t)make_path.size) + root = (ssize_t)make_path.size; /* i.e. NUL byte of string */ + if (root < 0) + root = 0; + + /* walk down tail of path making each directory */ + for (tail = &make_path.ptr[root]; *tail; *tail = lastch) { + bool mkdir_attempted = false; + + /* advance tail to include next path component */ + while (*tail == '/') + tail++; + while (*tail && *tail != '/') + tail++; + + /* truncate path at next component */ + lastch = *tail; + *tail = '\0'; + st.st_mode = 0; + + if (opts->cache_pathset && + git_hashset_str_contains(opts->cache_pathset, make_path.ptr)) + continue; + + /* See what's going on with this path component */ + opts->perfdata.stat_calls++; + +retry_lstat: + if (p_lstat(make_path.ptr, &st) < 0) { + if (mkdir_attempted || errno != ENOENT) { + git_error_set(GIT_ERROR_OS, "cannot access component in path '%s'", make_path.ptr); + error = -1; + goto done; + } + + git_error_clear(); + opts->perfdata.mkdir_calls++; + mkdir_attempted = true; + if (p_mkdir(make_path.ptr, mode) < 0) { + if (errno == EEXIST) + goto retry_lstat; + git_error_set(GIT_ERROR_OS, "failed to make directory '%s'", make_path.ptr); + error = -1; + goto done; + } + } else { + if ((error = mkdir_validate_dir( + make_path.ptr, &st, mode, flags, opts)) < 0) + goto done; + } + + /* chmod if requested and necessary */ + if ((error = mkdir_validate_mode( + make_path.ptr, &st, (lastch == '\0'), mode, flags, opts)) < 0) + goto done; + + if (opts->cache_pathset && opts->cache_pool) { + char *cache_path; + size_t alloc_size; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, make_path.size, 1); + cache_path = git_pool_malloc(opts->cache_pool, alloc_size); + GIT_ERROR_CHECK_ALLOC(cache_path); + + memcpy(cache_path, make_path.ptr, make_path.size + 1); + + if ((error = git_hashset_str_add(opts->cache_pathset, cache_path)) < 0) + goto done; + } + } + + error = 0; + + /* check that full path really is a directory if requested & needed */ + if ((flags & GIT_MKDIR_VERIFY_DIR) != 0 && + lastch != '\0') { + opts->perfdata.stat_calls++; + + if (p_stat(make_path.ptr, &st) < 0 || !S_ISDIR(st.st_mode)) { + git_error_set(GIT_ERROR_OS, "path is not a directory '%s'", + make_path.ptr); + error = GIT_ENOTFOUND; + } + } + +done: + git_str_dispose(&make_path); + return error; +} + +typedef struct { + const char *base; + size_t baselen; + uint32_t flags; + int depth; +} futils__rmdir_data; + +#define FUTILS_MAX_DEPTH 100 + +static int futils__error_cannot_rmdir(const char *path, const char *filemsg) +{ + if (filemsg) + git_error_set(GIT_ERROR_OS, "could not remove directory '%s': %s", + path, filemsg); + else + git_error_set(GIT_ERROR_OS, "could not remove directory '%s'", path); + + return -1; +} + +static int futils__rm_first_parent(git_str *path, const char *ceiling) +{ + int error = GIT_ENOTFOUND; + struct stat st; + + while (error == GIT_ENOTFOUND) { + git_str_rtruncate_at_char(path, '/'); + + if (!path->size || git__prefixcmp(path->ptr, ceiling) != 0) + error = 0; + else if (p_lstat_posixly(path->ptr, &st) == 0) { + if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) + error = p_unlink(path->ptr); + else if (!S_ISDIR(st.st_mode)) + error = -1; /* fail to remove non-regular file */ + } else if (errno != ENOTDIR) + error = -1; + } + + if (error) + futils__error_cannot_rmdir(path->ptr, "cannot remove parent"); + + return error; +} + +static int futils__rmdir_recurs_foreach(void *opaque, git_str *path) +{ + int error = 0; + futils__rmdir_data *data = opaque; + struct stat st; + + if (data->depth > FUTILS_MAX_DEPTH) + error = futils__error_cannot_rmdir( + path->ptr, "directory nesting too deep"); + + else if ((error = p_lstat_posixly(path->ptr, &st)) < 0) { + if (errno == ENOENT) + error = 0; + else if (errno == ENOTDIR) { + /* asked to remove a/b/c/d/e and a/b is a normal file */ + if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0) + error = futils__rm_first_parent(path, data->base); + else + futils__error_cannot_rmdir( + path->ptr, "parent is not directory"); + } + else + error = git_fs_path_set_error(errno, path->ptr, "rmdir"); + } + + else if (S_ISDIR(st.st_mode)) { + data->depth++; + + error = git_fs_path_direach(path, 0, futils__rmdir_recurs_foreach, data); + + data->depth--; + + if (error < 0) + return error; + + if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0) + return error; + + if ((error = p_rmdir(path->ptr)) < 0) { + if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 && + (errno == ENOTEMPTY || errno == EEXIST || errno == EBUSY)) + error = 0; + else + error = git_fs_path_set_error(errno, path->ptr, "rmdir"); + } + } + + else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) { + if (p_unlink(path->ptr) < 0) + error = git_fs_path_set_error(errno, path->ptr, "remove"); + } + + else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0) + error = futils__error_cannot_rmdir(path->ptr, "still present"); + + return error; +} + +static int futils__rmdir_empty_parent(void *opaque, const char *path) +{ + futils__rmdir_data *data = opaque; + int error = 0; + + if (strlen(path) <= data->baselen) + error = GIT_ITEROVER; + + else if (p_rmdir(path) < 0) { + int en = errno; + + if (en == ENOENT || en == ENOTDIR) { + /* do nothing */ + } else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0 && + en == EBUSY) { + error = git_fs_path_set_error(errno, path, "rmdir"); + } else if (en == ENOTEMPTY || en == EEXIST || en == EBUSY) { + error = GIT_ITEROVER; + } else { + error = git_fs_path_set_error(errno, path, "rmdir"); + } + } + + return error; +} + +int git_futils_rmdir_r( + const char *path, const char *base, uint32_t flags) +{ + int error; + git_str fullpath = GIT_STR_INIT; + futils__rmdir_data data; + + /* build path and find "root" where we should start calling mkdir */ + if (git_fs_path_join_unrooted(&fullpath, path, base, NULL) < 0) + return -1; + + memset(&data, 0, sizeof(data)); + data.base = base ? base : ""; + data.baselen = base ? strlen(base) : 0; + data.flags = flags; + + error = futils__rmdir_recurs_foreach(&data, &fullpath); + + /* remove now-empty parents if requested */ + if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0) + error = git_fs_path_walk_up( + &fullpath, base, futils__rmdir_empty_parent, &data); + + if (error == GIT_ITEROVER) { + git_error_clear(); + error = 0; + } + + git_str_dispose(&fullpath); + + return error; +} + +int git_futils_fake_symlink(const char *target, const char *path) +{ + int retcode = GIT_ERROR; + int fd = git_futils_creat_withpath(path, 0755, 0644); + if (fd >= 0) { + retcode = p_write(fd, target, strlen(target)); + p_close(fd); + } + return retcode; +} + +static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done) +{ + int error = 0; + char buffer[GIT_BUFSIZE_FILEIO]; + ssize_t len = 0; + + while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0) + /* p_write() does not have the same semantics as write(). It loops + * internally and will return 0 when it has completed writing. + */ + error = p_write(ofd, buffer, len); + + if (len < 0) { + git_error_set(GIT_ERROR_OS, "read error while copying file"); + error = (int)len; + } + + if (error < 0) + git_error_set(GIT_ERROR_OS, "write error while copying file"); + + if (close_fd_when_done) { + p_close(ifd); + p_close(ofd); + } + + return error; +} + +int git_futils_cp(const char *from, const char *to, mode_t filemode) +{ + int ifd, ofd; + + if ((ifd = git_futils_open_ro(from)) < 0) + return ifd; + + if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) { + p_close(ifd); + return git_fs_path_set_error(errno, to, "open for writing"); + } + + return cp_by_fd(ifd, ofd, true); +} + +int git_futils_touch(const char *path, time_t *when) +{ + struct p_timeval times[2]; + int ret; + + times[0].tv_sec = times[1].tv_sec = when ? *when : time(NULL); + times[0].tv_usec = times[1].tv_usec = 0; + + ret = p_utimes(path, times); + + return (ret < 0) ? git_fs_path_set_error(errno, path, "touch") : 0; +} + +static int cp_link(const char *from, const char *to, size_t link_size) +{ + int error = 0; + ssize_t read_len; + char *link_data; + size_t alloc_size; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, link_size, 1); + link_data = git__malloc(alloc_size); + GIT_ERROR_CHECK_ALLOC(link_data); + + read_len = p_readlink(from, link_data, link_size); + if (read_len != (ssize_t)link_size) { + git_error_set(GIT_ERROR_OS, "failed to read symlink data for '%s'", from); + error = -1; + } + else { + link_data[read_len] = '\0'; + + if (p_symlink(link_data, to) < 0) { + git_error_set(GIT_ERROR_OS, "could not symlink '%s' as '%s'", + link_data, to); + error = -1; + } + } + + git__free(link_data); + return error; +} + +typedef struct { + const char *to_root; + git_str to; + ssize_t from_prefix; + uint32_t flags; + uint32_t mkdir_flags; + mode_t dirmode; +} cp_r_info; + +#define GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT (1u << 10) + +static int _cp_r_mkdir(cp_r_info *info, git_str *from) +{ + int error = 0; + + /* create root directory the first time we need to create a directory */ + if ((info->flags & GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT) == 0) { + error = git_futils_mkdir( + info->to_root, info->dirmode, + (info->flags & GIT_CPDIR_CHMOD_DIRS) ? GIT_MKDIR_CHMOD : 0); + + info->flags |= GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT; + } + + /* create directory with root as base to prevent excess chmods */ + if (!error) + error = git_futils_mkdir_relative( + from->ptr + info->from_prefix, info->to_root, + info->dirmode, info->mkdir_flags, NULL); + + return error; +} + +static int _cp_r_callback(void *ref, git_str *from) +{ + int error = 0; + cp_r_info *info = ref; + struct stat from_st, to_st; + bool exists = false; + + if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 && + from->ptr[git_fs_path_basename_offset(from)] == '.') + return 0; + + if ((error = git_str_joinpath( + &info->to, info->to_root, from->ptr + info->from_prefix)) < 0) + return error; + + if (!(error = git_fs_path_lstat(info->to.ptr, &to_st))) + exists = true; + else if (error != GIT_ENOTFOUND) + return error; + else { + git_error_clear(); + error = 0; + } + + if ((error = git_fs_path_lstat(from->ptr, &from_st)) < 0) + return error; + + if (S_ISDIR(from_st.st_mode)) { + mode_t oldmode = info->dirmode; + + /* if we are not chmod'ing, then overwrite dirmode */ + if ((info->flags & GIT_CPDIR_CHMOD_DIRS) == 0) + info->dirmode = from_st.st_mode; + + /* make directory now if CREATE_EMPTY_DIRS is requested and needed */ + if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0) + error = _cp_r_mkdir(info, from); + + /* recurse onto target directory */ + if (!error && (!exists || S_ISDIR(to_st.st_mode))) + error = git_fs_path_direach(from, 0, _cp_r_callback, info); + + if (oldmode != 0) + info->dirmode = oldmode; + + return error; + } + + if (exists) { + if ((info->flags & GIT_CPDIR_OVERWRITE) == 0) + return 0; + + if (p_unlink(info->to.ptr) < 0) { + git_error_set(GIT_ERROR_OS, "cannot overwrite existing file '%s'", + info->to.ptr); + return GIT_EEXISTS; + } + } + + /* Done if this isn't a regular file or a symlink */ + if (!S_ISREG(from_st.st_mode) && + (!S_ISLNK(from_st.st_mode) || + (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0)) + return 0; + + /* Make container directory on demand if needed */ + if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 && + (error = _cp_r_mkdir(info, from)) < 0) + return error; + + /* make symlink or regular file */ + if (info->flags & GIT_CPDIR_LINK_FILES) { + if ((error = p_link(from->ptr, info->to.ptr)) < 0) + git_error_set(GIT_ERROR_OS, "failed to link '%s'", from->ptr); + } else if (S_ISLNK(from_st.st_mode)) { + error = cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size); + } else { + mode_t usemode = from_st.st_mode; + + if ((info->flags & GIT_CPDIR_SIMPLE_TO_MODE) != 0) + usemode = GIT_PERMS_FOR_WRITE(usemode); + + error = git_futils_cp(from->ptr, info->to.ptr, usemode); + } + + return error; +} + +int git_futils_cp_r( + const char *from, + const char *to, + uint32_t flags, + mode_t dirmode) +{ + int error; + git_str path = GIT_STR_INIT; + cp_r_info info; + + if (git_str_joinpath(&path, from, "") < 0) /* ensure trailing slash */ + return -1; + + memset(&info, 0, sizeof(info)); + info.to_root = to; + info.flags = flags; + info.dirmode = dirmode; + info.from_prefix = path.size; + git_str_init(&info.to, 0); + + /* precalculate mkdir flags */ + if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) { + /* if not creating empty dirs, then use mkdir to create the path on + * demand right before files are copied. + */ + info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST; + if ((flags & GIT_CPDIR_CHMOD_DIRS) != 0) + info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH; + } else { + /* otherwise, we will do simple mkdir as directories are encountered */ + info.mkdir_flags = + ((flags & GIT_CPDIR_CHMOD_DIRS) != 0) ? GIT_MKDIR_CHMOD : 0; + } + + error = _cp_r_callback(&info, &path); + + git_str_dispose(&path); + git_str_dispose(&info.to); + + return error; +} + +int git_futils_filestamp_check( + git_futils_filestamp *stamp, const char *path) +{ + struct stat st; + + /* if the stamp is NULL, then always reload */ + if (stamp == NULL) + return 1; + + if (p_stat(path, &st) < 0) + return GIT_ENOTFOUND; + + if (stamp->mtime.tv_sec == st.st_mtime && +#if defined(GIT_NSEC) + stamp->mtime.tv_nsec == st.st_mtime_nsec && +#endif + stamp->size == (uint64_t)st.st_size && + stamp->ino == (unsigned int)st.st_ino) + return 0; + + stamp->mtime.tv_sec = st.st_mtime; +#if defined(GIT_NSEC) + stamp->mtime.tv_nsec = st.st_mtime_nsec; +#endif + stamp->size = (uint64_t)st.st_size; + stamp->ino = (unsigned int)st.st_ino; + + return 1; +} + +void git_futils_filestamp_set( + git_futils_filestamp *target, const git_futils_filestamp *source) +{ + if (source) + memcpy(target, source, sizeof(*target)); + else + memset(target, 0, sizeof(*target)); +} + + +void git_futils_filestamp_set_from_stat( + git_futils_filestamp *stamp, struct stat *st) +{ + if (st) { + stamp->mtime.tv_sec = st->st_mtime; +#if defined(GIT_NSEC) + stamp->mtime.tv_nsec = st->st_mtime_nsec; +#else + stamp->mtime.tv_nsec = 0; +#endif + stamp->size = (uint64_t)st->st_size; + stamp->ino = (unsigned int)st->st_ino; + } else { + memset(stamp, 0, sizeof(*stamp)); + } +} + +int git_futils_fsync_dir(const char *path) +{ +#ifdef GIT_WIN32 + GIT_UNUSED(path); + return 0; +#else + int fd, error = -1; + + if ((fd = p_open(path, O_RDONLY)) < 0) { + git_error_set(GIT_ERROR_OS, "failed to open directory '%s' for fsync", path); + return -1; + } + + if ((error = p_fsync(fd)) < 0) + git_error_set(GIT_ERROR_OS, "failed to fsync directory '%s'", path); + + p_close(fd); + return error; +#endif +} + +int git_futils_fsync_parent(const char *path) +{ + char *parent; + int error; + + if ((parent = git_fs_path_dirname(path)) == NULL) + return -1; + + error = git_futils_fsync_dir(parent); + git__free(parent); + return error; +} diff --git a/src/util/futils.h b/src/util/futils.h new file mode 100644 index 00000000000..7ae869be6ec --- /dev/null +++ b/src/util/futils.h @@ -0,0 +1,412 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_futils_h__ +#define INCLUDE_futils_h__ + +#include "git2_util.h" + +#include "map.h" +#include "posix.h" +#include "fs_path.h" +#include "pool.h" +#include "hash.h" +#include "hashmap_str.h" + +/** + * Filebuffer methods + * + * Read whole files into an in-memory buffer for processing + */ +extern int git_futils_readbuffer(git_str *obj, const char *path); +extern int git_futils_readbuffer_updated( + git_str *obj, + const char *path, + unsigned char checksum[GIT_HASH_SHA256_SIZE], + int *updated); +extern int git_futils_readbuffer_fd_full(git_str *obj, git_file fd); +extern int git_futils_readbuffer_fd(git_str *obj, git_file fd, size_t len); + +/* Additional constants for `git_futils_writebuffer`'s `open_flags`. We + * support these internally and they will be removed before the `open` call. + */ +#ifndef O_FSYNC +# define O_FSYNC (1 << 31) +#endif + +extern int git_futils_writebuffer( + const git_str *buf, const char *path, int open_flags, mode_t mode); + +/** + * File utils + * + * These are custom filesystem-related helper methods. They are + * rather high level, and wrap the underlying POSIX methods + * + * All these methods return 0 on success, + * or an error code on failure and an error message is set. + */ + +/** + * Create and open a file, while also + * creating all the folders in its path + */ +extern int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode); + +/** + * Create and open a process-locked file + */ +extern int git_futils_creat_locked(const char *path, const mode_t mode); + +/** + * Create and open a process-locked file, while + * also creating all the folders in its path + */ +extern int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode); + +/** + * Create a path recursively. + */ +extern int git_futils_mkdir_r(const char *path, const mode_t mode); + +/** + * Flags to pass to `git_futils_mkdir`. + * + * * GIT_MKDIR_EXCL is "exclusive" - i.e. generate an error if dir exists. + * * GIT_MKDIR_PATH says to make all components in the path. + * * GIT_MKDIR_CHMOD says to chmod the final directory entry after creation + * * GIT_MKDIR_CHMOD_PATH says to chmod each directory component in the path + * * GIT_MKDIR_SKIP_LAST says to leave off the last element of the path + * * GIT_MKDIR_SKIP_LAST2 says to leave off the last 2 elements of the path + * * GIT_MKDIR_VERIFY_DIR says confirm final item is a dir, not just EEXIST + * * GIT_MKDIR_REMOVE_FILES says to remove files and recreate dirs + * * GIT_MKDIR_REMOVE_SYMLINKS says to remove symlinks and recreate dirs + * + * Note that the chmod options will be executed even if the directory already + * exists, unless GIT_MKDIR_EXCL is given. + */ +typedef enum { + GIT_MKDIR_EXCL = 1, + GIT_MKDIR_PATH = 2, + GIT_MKDIR_CHMOD = 4, + GIT_MKDIR_CHMOD_PATH = 8, + GIT_MKDIR_SKIP_LAST = 16, + GIT_MKDIR_SKIP_LAST2 = 32, + GIT_MKDIR_VERIFY_DIR = 64, + GIT_MKDIR_REMOVE_FILES = 128, + GIT_MKDIR_REMOVE_SYMLINKS = 256 +} git_futils_mkdir_flags; + +struct git_futils_mkdir_perfdata +{ + size_t stat_calls; + size_t mkdir_calls; + size_t chmod_calls; +}; + +struct git_futils_mkdir_options +{ + /* + * Callers can optionally pass an allocation pool and a + * hashset of strings; mkdir will populate these with the + * path(s) it creates; this can be useful for repeated calls + * to mkdir. This will reduce I/O by avoiding testing for the + * existence of intermediate directories that it knows already + * exist (because it created them). + */ + git_pool *cache_pool; + git_hashset_str *cache_pathset; + + struct git_futils_mkdir_perfdata perfdata; +}; + +/** + * Create a directory or entire path. + * + * This makes a directory (and the entire path leading up to it if requested), + * and optionally chmods the directory immediately after (or each part of the + * path if requested). + * + * @param path The path to create, relative to base. + * @param base Root for relative path. These directories will never be made. + * @param mode The mode to use for created directories. + * @param flags Combination of the mkdir flags above. + * @param opts Extended options, or null. + * @return 0 on success, else error code + */ +extern int git_futils_mkdir_relative(const char *path, const char *base, mode_t mode, uint32_t flags, struct git_futils_mkdir_options *opts); + +/** + * Create a directory or entire path. Similar to `git_futils_mkdir_relative` + * without performance data. + */ +extern int git_futils_mkdir(const char *path, mode_t mode, uint32_t flags); + +/** + * Create all the folders required to contain + * the full path of a file + */ +extern int git_futils_mkpath2file(const char *path, const mode_t mode); + +/** + * Flags to pass to `git_futils_rmdir_r`. + * + * * GIT_RMDIR_EMPTY_HIERARCHY - the default; remove hierarchy of empty + * dirs and generate error if any files are found. + * * GIT_RMDIR_REMOVE_FILES - attempt to remove files in the hierarchy. + * * GIT_RMDIR_SKIP_NONEMPTY - skip non-empty directories with no error. + * * GIT_RMDIR_EMPTY_PARENTS - remove containing directories up to base + * if removing this item leaves them empty + * * GIT_RMDIR_REMOVE_BLOCKERS - remove blocking file that causes ENOTDIR + * * GIT_RMDIR_SKIP_ROOT - don't remove root directory itself + */ +typedef enum { + GIT_RMDIR_EMPTY_HIERARCHY = 0, + GIT_RMDIR_REMOVE_FILES = (1 << 0), + GIT_RMDIR_SKIP_NONEMPTY = (1 << 1), + GIT_RMDIR_EMPTY_PARENTS = (1 << 2), + GIT_RMDIR_REMOVE_BLOCKERS = (1 << 3), + GIT_RMDIR_SKIP_ROOT = (1 << 4) +} git_futils_rmdir_flags; + +/** + * Remove path and any files and directories beneath it. + * + * @param path Path to the top level directory to process. + * @param base Root for relative path. + * @param flags Combination of git_futils_rmdir_flags values + * @return 0 on success; -1 on error. + */ +extern int git_futils_rmdir_r(const char *path, const char *base, uint32_t flags); + +/** + * Create and open a temporary file with a `_git2_` suffix in a + * protected directory; the file created will created will honor + * the current `umask`. Writes the filename into path_out. + * + * This function uses a high-quality PRNG seeded by the system's + * entropy pool _where available_ and falls back to a simple seed + * (time plus system information) when not. This is suitable for + * writing within a protected directory, but the system's safe + * temporary file creation functions should be preferred where + * available when writing into world-writable (temp) directories. + * + * @return On success, an open file descriptor, else an error code < 0. + */ +extern int git_futils_mktmp(git_str *path_out, const char *filename, mode_t mode); + +/** + * Move a file on the filesystem, create the + * destination path if it doesn't exist + */ +extern int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode); + +/** + * Copy a file + * + * The filemode will be used for the newly created file. + */ +extern int git_futils_cp( + const char *from, + const char *to, + mode_t filemode); + +/** + * Set the files atime and mtime to the given time, or the current time + * if `ts` is NULL. + */ +extern int git_futils_touch(const char *path, time_t *when); + +/** + * Flags that can be passed to `git_futils_cp_r`. + * + * - GIT_CPDIR_CREATE_EMPTY_DIRS: create directories even if there are no + * files under them (otherwise directories will only be created lazily + * when a file inside them is copied). + * - GIT_CPDIR_COPY_SYMLINKS: copy symlinks, otherwise they are ignored. + * - GIT_CPDIR_COPY_DOTFILES: copy files with leading '.', otherwise ignored. + * - GIT_CPDIR_OVERWRITE: overwrite pre-existing files with source content, + * otherwise they are silently skipped. + * - GIT_CPDIR_CHMOD_DIRS: explicitly chmod directories to `dirmode` + * - GIT_CPDIR_SIMPLE_TO_MODE: default tries to replicate the mode of the + * source file to the target; with this flag, always use 0666 (or 0777 if + * source has exec bits set) for target. + * - GIT_CPDIR_LINK_FILES will try to use hardlinks for the files + */ +typedef enum { + GIT_CPDIR_CREATE_EMPTY_DIRS = (1u << 0), + GIT_CPDIR_COPY_SYMLINKS = (1u << 1), + GIT_CPDIR_COPY_DOTFILES = (1u << 2), + GIT_CPDIR_OVERWRITE = (1u << 3), + GIT_CPDIR_CHMOD_DIRS = (1u << 4), + GIT_CPDIR_SIMPLE_TO_MODE = (1u << 5), + GIT_CPDIR_LINK_FILES = (1u << 6) +} git_futils_cpdir_flags; + +/** + * Copy a directory tree. + * + * This copies directories and files from one root to another. You can + * pass a combination of GIT_CPDIR flags as defined above. + * + * If you pass the CHMOD flag, then the dirmode will be applied to all + * directories that are created during the copy, overriding the natural + * permissions. If you do not pass the CHMOD flag, then the dirmode + * will actually be copied from the source files and the `dirmode` arg + * will be ignored. + */ +extern int git_futils_cp_r( + const char *from, + const char *to, + uint32_t flags, + mode_t dirmode); + +/** + * Open a file readonly and set error if needed. + */ +extern int git_futils_open_ro(const char *path); + +/** + * Truncate a file, creating it if it doesn't exist. + */ +extern int git_futils_truncate(const char *path, int mode); + +/** + * Get the filesize in bytes of a file + */ +extern int git_futils_filesize(uint64_t *out, git_file fd); + +#define GIT_PERMS_IS_EXEC(MODE) (((MODE) & 0100) != 0) +#define GIT_PERMS_CANONICAL(MODE) (GIT_PERMS_IS_EXEC(MODE) ? 0755 : 0644) +#define GIT_PERMS_FOR_WRITE(MODE) (GIT_PERMS_IS_EXEC(MODE) ? 0777 : 0666) + +#define GIT_MODE_PERMS_MASK 0777 +#define GIT_MODE_TYPE_MASK 0170000 +#define GIT_MODE_TYPE(MODE) ((MODE) & GIT_MODE_TYPE_MASK) +#define GIT_MODE_ISBLOB(MODE) (GIT_MODE_TYPE(MODE) == GIT_MODE_TYPE(GIT_FILEMODE_BLOB)) + +/** + * Convert a mode_t from the OS to a legal git mode_t value. + */ +extern mode_t git_futils_canonical_mode(mode_t raw_mode); + + +/** + * Read-only map all or part of a file into memory. + * When possible this function should favor a virtual memory + * style mapping over some form of malloc()+read(), as the + * data access will be random and is not likely to touch the + * majority of the region requested. + * + * @param out buffer to populate with the mapping information. + * @param fd open descriptor to configure the mapping from. + * @param begin first byte to map, this should be page aligned. + * @param len number of bytes to map. + * @return + * - 0 on success; + * - -1 on error. + */ +extern int git_futils_mmap_ro( + git_map *out, + git_file fd, + off64_t begin, + size_t len); + +/** + * Read-only map an entire file. + * + * @param out buffer to populate with the mapping information. + * @param path path to file to be opened. + * @return + * - 0 on success; + * - GIT_ENOTFOUND if not found; + * - -1 on an unspecified OS related error. + */ +extern int git_futils_mmap_ro_file( + git_map *out, + const char *path); + +/** + * Release the memory associated with a previous memory mapping. + * @param map the mapping description previously configured. + */ +extern void git_futils_mmap_free(git_map *map); + +/** + * Create a "fake" symlink (text file containing the target path). + * + * @param target original symlink target + * @param path symlink file to be created + * @return 0 on success, -1 on error + */ +extern int git_futils_fake_symlink(const char *target, const char *path); + +/** + * A file stamp represents a snapshot of information about a file that can + * be used to test if the file changes. This portable implementation is + * based on stat data about that file, but it is possible that OS specific + * versions could be implemented in the future. + */ +typedef struct { + struct timespec mtime; + uint64_t size; + unsigned int ino; +} git_futils_filestamp; + +/** + * Compare stat information for file with reference info. + * + * This function updates the file stamp to current data for the given path + * and returns 0 if the file is up-to-date relative to the prior setting, + * 1 if the file has been changed, or GIT_ENOTFOUND if the file doesn't + * exist. This will not call git_error_set, so you must set the error if you + * plan to return an error. + * + * @param stamp File stamp to be checked + * @param path Path to stat and check if changed + * @return 0 if up-to-date, 1 if out-of-date, GIT_ENOTFOUND if cannot stat + */ +extern int git_futils_filestamp_check( + git_futils_filestamp *stamp, const char *path); + +/** + * Set or reset file stamp data + * + * This writes the target file stamp. If the source is NULL, this will set + * the target stamp to values that will definitely be out of date. If the + * source is not NULL, this copies the source values to the target. + * + * @param tgt File stamp to write to + * @param src File stamp to copy from or NULL to clear the target + */ +extern void git_futils_filestamp_set( + git_futils_filestamp *tgt, const git_futils_filestamp *src); + +/** + * Set file stamp data from stat structure + */ +extern void git_futils_filestamp_set_from_stat( + git_futils_filestamp *stamp, struct stat *st); + +/** + * `fsync` the parent directory of the given path, if `fsync` is + * supported for directories on this platform. + * + * @param path Path of the directory to sync. + * @return 0 on success, -1 on error + */ +extern int git_futils_fsync_dir(const char *path); + +/** + * `fsync` the parent directory of the given path, if `fsync` is + * supported for directories on this platform. + * + * @param path Path of the file whose parent directory should be synced. + * @return 0 on success, -1 on error + */ +extern int git_futils_fsync_parent(const char *path); + +#endif diff --git a/src/util/git2_features.h.in b/src/util/git2_features.h.in new file mode 100644 index 00000000000..a440561f8d4 --- /dev/null +++ b/src/util/git2_features.h.in @@ -0,0 +1,101 @@ +#ifndef INCLUDE_features_h__ +#define INCLUDE_features_h__ + +/* Debugging options */ + +#cmakedefine GIT_DEBUG_POOL 1 +#cmakedefine GIT_DEBUG_STRICT_ALLOC 1 +#cmakedefine GIT_DEBUG_STRICT_OPEN 1 +#cmakedefine GIT_DEBUG_LEAKCHECK_WIN32 1 + +/* Feature enablement and provider / backend selection */ + +#cmakedefine GIT_THREADS 1 +#cmakedefine GIT_THREADS_PTHREADS 1 +#cmakedefine GIT_THREADS_WIN32 1 + +#cmakedefine GIT_SHA1_BUILTIN 1 +#cmakedefine GIT_SHA1_OPENSSL 1 +#cmakedefine GIT_SHA1_OPENSSL_FIPS 1 +#cmakedefine GIT_SHA1_OPENSSL_DYNAMIC 1 +#cmakedefine GIT_SHA1_MBEDTLS 1 +#cmakedefine GIT_SHA1_COMMON_CRYPTO 1 +#cmakedefine GIT_SHA1_WIN32 1 + +#cmakedefine GIT_SHA256_BUILTIN 1 +#cmakedefine GIT_SHA256_WIN32 1 +#cmakedefine GIT_SHA256_COMMON_CRYPTO 1 +#cmakedefine GIT_SHA256_OPENSSL 1 +#cmakedefine GIT_SHA256_OPENSSL_FIPS 1 +#cmakedefine GIT_SHA256_OPENSSL_DYNAMIC 1 +#cmakedefine GIT_SHA256_MBEDTLS 1 + +#cmakedefine GIT_COMPRESSION_BUILTIN 1 +#cmakedefine GIT_COMPRESSION_ZLIB 1 + +#cmakedefine GIT_NSEC 1 +#cmakedefine GIT_NSEC_MTIM 1 +#cmakedefine GIT_NSEC_MTIMESPEC 1 +#cmakedefine GIT_NSEC_MTIME_NSEC 1 +#cmakedefine GIT_NSEC_WIN32 1 + +#cmakedefine GIT_I18N 1 +#cmakedefine GIT_I18N_ICONV 1 + +#cmakedefine GIT_REGEX_REGCOMP_L 1 +#cmakedefine GIT_REGEX_REGCOMP 1 +#cmakedefine GIT_REGEX_PCRE 1 +#cmakedefine GIT_REGEX_PCRE2 1 +#cmakedefine GIT_REGEX_BUILTIN 1 + +#cmakedefine GIT_SSH 1 +#cmakedefine GIT_SSH_EXEC 1 +#cmakedefine GIT_SSH_LIBSSH2 1 +#cmakedefine GIT_SSH_LIBSSH2_MEMORY_CREDENTIALS 1 + +#cmakedefine GIT_HTTPS 1 +#cmakedefine GIT_HTTPS_OPENSSL 1 +#cmakedefine GIT_HTTPS_OPENSSL_DYNAMIC 1 +#cmakedefine GIT_HTTPS_SECURETRANSPORT 1 +#cmakedefine GIT_HTTPS_MBEDTLS 1 +#cmakedefine GIT_HTTPS_SCHANNEL 1 +#cmakedefine GIT_HTTPS_WINHTTP 1 + +#cmakedefine GIT_HTTPPARSER_HTTPPARSER 1 +#cmakedefine GIT_HTTPPARSER_LLHTTP 1 +#cmakedefine GIT_HTTPPARSER_BUILTIN 1 + +#cmakedefine GIT_AUTH_NTLM 1 +#cmakedefine GIT_AUTH_NTLM_BUILTIN 1 +#cmakedefine GIT_AUTH_NTLM_SSPI 1 + +#cmakedefine GIT_AUTH_NEGOTIATE 1 +#cmakedefine GIT_AUTH_NEGOTIATE_GSSFRAMEWORK 1 +#cmakedefine GIT_AUTH_NEGOTIATE_GSSAPI 1 +#cmakedefine GIT_AUTH_NEGOTIATE_SSPI 1 + +/* Platform details */ + +#cmakedefine GIT_ARCH_64 1 +#cmakedefine GIT_ARCH_32 1 + +#cmakedefine GIT_QSORT_BSD 1 +#cmakedefine GIT_QSORT_GNU 1 +#cmakedefine GIT_QSORT_C11 1 +#cmakedefine GIT_QSORT_MSC 1 + +#cmakedefine GIT_FUTIMENS 1 + +#cmakedefine GIT_RAND_GETENTROPY 1 +#cmakedefine GIT_RAND_GETLOADAVG 1 + +#cmakedefine GIT_IO_POLL 1 +#cmakedefine GIT_IO_WSAPOLL 1 +#cmakedefine GIT_IO_SELECT 1 + +/* Compile-time information */ + +#cmakedefine GIT_BUILD_CPU "@GIT_BUILD_CPU@" +#cmakedefine GIT_BUILD_COMMIT "@GIT_BUILD_COMMIT@" + +#endif diff --git a/src/util/git2_util.h b/src/util/git2_util.h new file mode 100644 index 00000000000..d47ce5f43e9 --- /dev/null +++ b/src/util/git2_util.h @@ -0,0 +1,174 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git2_util_h__ +#define INCLUDE_git2_util_h__ + +#if !defined(LIBGIT2_NO_FEATURES_H) +# include "git2_features.h" +#endif + +#include "git2/common.h" +#include "git2/sys/errors.h" +#include "cc-compat.h" + +typedef struct git_str git_str; + +/** Declare a function as always inlined. */ +#if defined(_MSC_VER) +# define GIT_INLINE(type) static __inline type +#elif defined(__GNUC__) +# define GIT_INLINE(type) static __inline__ type +#else +# define GIT_INLINE(type) static type +#endif + +/** Support for gcc/clang __has_builtin intrinsic */ +#ifndef __has_builtin +# define __has_builtin(x) 0 +#endif + +/** + * Declare that a function's return value must be used. + * + * Used mostly to guard against potential silent bugs at runtime. This is + * recommended to be added to functions that: + * + * - Allocate / reallocate memory. This prevents memory leaks or errors where + * buffers are expected to have grown to a certain size, but could not be + * resized. + * - Acquire locks. When a lock cannot be acquired, that will almost certainly + * cause a data race / undefined behavior. + */ +#if defined(__GNUC__) +# define GIT_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) +#else +# define GIT_WARN_UNUSED_RESULT +#endif + +#if (defined(_WIN32)) && !defined(__CYGWIN__) +# define GIT_WIN32 1 +#endif + +#include +#include +#include +#include +#include +#include + +#include +#include + +#ifdef GIT_WIN32 + +# include +# include +# include +# include +# include +# include "win32/msvc-compat.h" +# include "win32/mingw-compat.h" +# include "win32/win32-compat.h" +# include "win32/w32_common.h" +# include "win32/version.h" +# include "win32/error.h" +# ifdef GIT_THREADS +# include "win32/thread.h" +# endif + +#else + +# include +# include +# ifdef GIT_THREADS +# include +# include +# endif + +#define GIT_LIBGIT2_CALL +#define GIT_SYSTEM_CALL + +#ifdef GIT_USE_STAT_ATIMESPEC +# define st_atim st_atimespec +# define st_ctim st_ctimespec +# define st_mtim st_mtimespec +#endif + +# include + +#endif + +#include "git2/types.h" +#include "git2/errors.h" +#include "thread.h" +#include "integer.h" +#include "assert_safe.h" + +#include "posix.h" + +#define GIT_BUFSIZE_DEFAULT 65536 +#define GIT_BUFSIZE_FILEIO GIT_BUFSIZE_DEFAULT +#define GIT_BUFSIZE_FILTERIO GIT_BUFSIZE_DEFAULT +#define GIT_BUFSIZE_NETIO GIT_BUFSIZE_DEFAULT + + +/** + * Check a pointer allocation result, returning -1 if it failed. + */ +#define GIT_ERROR_CHECK_ALLOC(ptr) do { \ + if ((ptr) == NULL) { return -1; } \ + } while(0) + +/** + * Check a buffer allocation result, returning -1 if it failed. + */ +#define GIT_ERROR_CHECK_ALLOC_STR(buf) do { \ + if ((void *)(buf) == NULL || git_str_oom(buf)) { return -1; } \ + } while(0) + +/** + * Check a return value and propagate result if non-zero. + */ +#define GIT_ERROR_CHECK_ERROR(code) \ + do { int _err = (code); if (_err) return _err; } while (0) + + +/** Check for additive overflow, setting an error if would occur. */ +#define GIT_ADD_SIZET_OVERFLOW(out, one, two) \ + (git__add_sizet_overflow(out, one, two) ? (git_error_set_oom(), 1) : 0) + +/** Check for additive overflow, setting an error if would occur. */ +#define GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize) \ + (git__multiply_sizet_overflow(out, nelem, elsize) ? (git_error_set_oom(), 1) : 0) + +/** Check for additive overflow, failing if it would occur. */ +#define GIT_ERROR_CHECK_ALLOC_ADD(out, one, two) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two)) { return -1; } + +#define GIT_ERROR_CHECK_ALLOC_ADD3(out, one, two, three) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), three)) { return -1; } + +#define GIT_ERROR_CHECK_ALLOC_ADD4(out, one, two, three, four) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), four)) { return -1; } + +#define GIT_ERROR_CHECK_ALLOC_ADD5(out, one, two, three, four, five) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), four) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), five)) { return -1; } + +/** Check for multiplicative overflow, failing if it would occur. */ +#define GIT_ERROR_CHECK_ALLOC_MULTIPLY(out, nelem, elsize) \ + if (GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize)) { return -1; } + +#include "util.h" +#include "ctype_compat.h" + +#endif diff --git a/src/util/hash.c b/src/util/hash.c new file mode 100644 index 00000000000..ff900cea7d6 --- /dev/null +++ b/src/util/hash.c @@ -0,0 +1,158 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "hash.h" + +int git_hash_global_init(void) +{ + if (git_hash_sha1_global_init() < 0 || + git_hash_sha256_global_init() < 0) + return -1; + + return 0; +} + +int git_hash_ctx_init(git_hash_ctx *ctx, git_hash_algorithm_t algorithm) +{ + int error; + + switch (algorithm) { + case GIT_HASH_ALGORITHM_SHA1: + error = git_hash_sha1_ctx_init(&ctx->ctx.sha1); + break; + case GIT_HASH_ALGORITHM_SHA256: + error = git_hash_sha256_ctx_init(&ctx->ctx.sha256); + break; + default: + git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm"); + error = -1; + } + + ctx->algorithm = algorithm; + return error; +} + +void git_hash_ctx_cleanup(git_hash_ctx *ctx) +{ + switch (ctx->algorithm) { + case GIT_HASH_ALGORITHM_SHA1: + git_hash_sha1_ctx_cleanup(&ctx->ctx.sha1); + return; + case GIT_HASH_ALGORITHM_SHA256: + git_hash_sha256_ctx_cleanup(&ctx->ctx.sha256); + return; + default: + /* unreachable */ ; + } +} + +int git_hash_init(git_hash_ctx *ctx) +{ + switch (ctx->algorithm) { + case GIT_HASH_ALGORITHM_SHA1: + return git_hash_sha1_init(&ctx->ctx.sha1); + case GIT_HASH_ALGORITHM_SHA256: + return git_hash_sha256_init(&ctx->ctx.sha256); + default: + /* unreachable */ ; + } + + git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm"); + return -1; +} + +int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len) +{ + switch (ctx->algorithm) { + case GIT_HASH_ALGORITHM_SHA1: + return git_hash_sha1_update(&ctx->ctx.sha1, data, len); + case GIT_HASH_ALGORITHM_SHA256: + return git_hash_sha256_update(&ctx->ctx.sha256, data, len); + default: + /* unreachable */ ; + } + + git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm"); + return -1; +} + +int git_hash_final(unsigned char *out, git_hash_ctx *ctx) +{ + switch (ctx->algorithm) { + case GIT_HASH_ALGORITHM_SHA1: + return git_hash_sha1_final(out, &ctx->ctx.sha1); + case GIT_HASH_ALGORITHM_SHA256: + return git_hash_sha256_final(out, &ctx->ctx.sha256); + default: + /* unreachable */ ; + } + + git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm"); + return -1; +} + +int git_hash_buf( + unsigned char *out, + const void *data, + size_t len, + git_hash_algorithm_t algorithm) +{ + git_hash_ctx ctx; + int error = 0; + + if (git_hash_ctx_init(&ctx, algorithm) < 0) + return -1; + + if ((error = git_hash_update(&ctx, data, len)) >= 0) + error = git_hash_final(out, &ctx); + + git_hash_ctx_cleanup(&ctx); + + return error; +} + +int git_hash_vec( + unsigned char *out, + git_str_vec *vec, + size_t n, + git_hash_algorithm_t algorithm) +{ + git_hash_ctx ctx; + size_t i; + int error = 0; + + if (git_hash_ctx_init(&ctx, algorithm) < 0) + return -1; + + for (i = 0; i < n; i++) { + if ((error = git_hash_update(&ctx, vec[i].data, vec[i].len)) < 0) + goto done; + } + + error = git_hash_final(out, &ctx); + +done: + git_hash_ctx_cleanup(&ctx); + + return error; +} + +int git_hash_fmt(char *out, unsigned char *hash, size_t hash_len) +{ + static char hex[] = "0123456789abcdef"; + char *str = out; + size_t i; + + for (i = 0; i < hash_len; i++) { + *str++ = hex[hash[i] >> 4]; + *str++ = hex[hash[i] & 0x0f]; + } + + *str++ = '\0'; + + return 0; +} diff --git a/src/util/hash.h b/src/util/hash.h new file mode 100644 index 00000000000..21fcaf045e7 --- /dev/null +++ b/src/util/hash.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_hash_h__ +#define INCLUDE_hash_h__ + +#include "git2_util.h" + +#include "hash/sha.h" + +typedef struct { + void *data; + size_t len; +} git_str_vec; + +typedef enum { + GIT_HASH_ALGORITHM_NONE = 0, + GIT_HASH_ALGORITHM_SHA1, + GIT_HASH_ALGORITHM_SHA256 +} git_hash_algorithm_t; + +#define GIT_HASH_MAX_SIZE GIT_HASH_SHA256_SIZE + +typedef struct git_hash_ctx { + union { + git_hash_sha1_ctx sha1; + git_hash_sha256_ctx sha256; + } ctx; + git_hash_algorithm_t algorithm; +} git_hash_ctx; + +int git_hash_global_init(void); + +int git_hash_ctx_init(git_hash_ctx *ctx, git_hash_algorithm_t algorithm); +void git_hash_ctx_cleanup(git_hash_ctx *ctx); + +int git_hash_init(git_hash_ctx *c); +int git_hash_update(git_hash_ctx *c, const void *data, size_t len); +int git_hash_final(unsigned char *out, git_hash_ctx *c); + +int git_hash_buf(unsigned char *out, const void *data, size_t len, git_hash_algorithm_t algorithm); +int git_hash_vec(unsigned char *out, git_str_vec *vec, size_t n, git_hash_algorithm_t algorithm); + +int git_hash_fmt(char *out, unsigned char *hash, size_t hash_len); + +GIT_INLINE(size_t) git_hash_size(git_hash_algorithm_t algorithm) { + switch (algorithm) { + case GIT_HASH_ALGORITHM_SHA1: + return GIT_HASH_SHA1_SIZE; + case GIT_HASH_ALGORITHM_SHA256: + return GIT_HASH_SHA256_SIZE; + default: + return 0; + } +} + +#endif diff --git a/src/util/hash/builtin.c b/src/util/hash/builtin.c new file mode 100644 index 00000000000..b4736efbc66 --- /dev/null +++ b/src/util/hash/builtin.c @@ -0,0 +1,63 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "builtin.h" + +int git_hash_sha256_global_init(void) +{ + return 0; +} + +int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx) +{ + return git_hash_sha256_init(ctx); +} + +void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx) +{ + GIT_UNUSED(ctx); +} + +int git_hash_sha256_init(git_hash_sha256_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + if (SHA256Reset(&ctx->c)) { + git_error_set(GIT_ERROR_SHA, "SHA256 error"); + return -1; + } + return 0; +} + +int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *_data, size_t len) +{ + const unsigned char *data = _data; + GIT_ASSERT_ARG(ctx); + + while (len > 0) { + unsigned int chunk = (len > UINT_MAX) ? UINT_MAX : (unsigned int)len; + + if (SHA256Input(&ctx->c, data, chunk)) { + git_error_set(GIT_ERROR_SHA, "SHA256 error"); + return -1; + } + + data += chunk; + len -= chunk; + } + + return 0; +} + +int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + if (SHA256Result(&ctx->c, out)) { + git_error_set(GIT_ERROR_SHA, "SHA256 error"); + return -1; + } + return 0; +} diff --git a/src/util/hash/builtin.h b/src/util/hash/builtin.h new file mode 100644 index 00000000000..769df1ac29d --- /dev/null +++ b/src/util/hash/builtin.h @@ -0,0 +1,19 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_hash_builtin_h__ +#define INCLUDE_hash_builtin_h__ + +#include "hash/sha.h" + +#include "rfc6234/sha.h" + +struct git_hash_sha256_ctx { + SHA256Context c; +}; + +#endif diff --git a/src/util/hash/collisiondetect.c b/src/util/hash/collisiondetect.c new file mode 100644 index 00000000000..c51a402d793 --- /dev/null +++ b/src/util/hash/collisiondetect.c @@ -0,0 +1,48 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "collisiondetect.h" + +int git_hash_sha1_global_init(void) +{ + return 0; +} + +int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx) +{ + return git_hash_sha1_init(ctx); +} + +void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx) +{ + GIT_UNUSED(ctx); +} + +int git_hash_sha1_init(git_hash_sha1_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + SHA1DCInit(&ctx->c); + return 0; +} + +int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len) +{ + GIT_ASSERT_ARG(ctx); + SHA1DCUpdate(&ctx->c, data, len); + return 0; +} + +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + if (SHA1DCFinal(out, &ctx->c)) { + git_error_set(GIT_ERROR_SHA, "SHA1 collision attack detected"); + return -1; + } + + return 0; +} diff --git a/src/util/hash/collisiondetect.h b/src/util/hash/collisiondetect.h new file mode 100644 index 00000000000..8de55023020 --- /dev/null +++ b/src/util/hash/collisiondetect.h @@ -0,0 +1,19 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_hash_collisiondetect_h__ +#define INCLUDE_hash_collisiondetect_h__ + +#include "hash/sha.h" + +#include "sha1dc/sha1.h" + +struct git_hash_sha1_ctx { + SHA1_CTX c; +}; + +#endif diff --git a/src/util/hash/common_crypto.c b/src/util/hash/common_crypto.c new file mode 100644 index 00000000000..b327ba9e385 --- /dev/null +++ b/src/util/hash/common_crypto.c @@ -0,0 +1,112 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common_crypto.h" + +#define CC_LONG_MAX ((CC_LONG)-1) + +#ifdef GIT_SHA1_COMMON_CRYPTO + +int git_hash_sha1_global_init(void) +{ + return 0; +} + +int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx) +{ + return git_hash_sha1_init(ctx); +} + +void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx) +{ + GIT_UNUSED(ctx); +} + +int git_hash_sha1_init(git_hash_sha1_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + CC_SHA1_Init(&ctx->c); + return 0; +} + +int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *_data, size_t len) +{ + const unsigned char *data = _data; + + GIT_ASSERT_ARG(ctx); + + while (len > 0) { + CC_LONG chunk = (len > CC_LONG_MAX) ? CC_LONG_MAX : (CC_LONG)len; + + CC_SHA1_Update(&ctx->c, data, chunk); + + data += chunk; + len -= chunk; + } + + return 0; +} + +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + CC_SHA1_Final(out, &ctx->c); + return 0; +} + +#endif + +#ifdef GIT_SHA256_COMMON_CRYPTO + +int git_hash_sha256_global_init(void) +{ + return 0; +} + +int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx) +{ + return git_hash_sha256_init(ctx); +} + +void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx) +{ + GIT_UNUSED(ctx); +} + +int git_hash_sha256_init(git_hash_sha256_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + CC_SHA256_Init(&ctx->c); + return 0; +} + +int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *_data, size_t len) +{ + const unsigned char *data = _data; + + GIT_ASSERT_ARG(ctx); + + while (len > 0) { + CC_LONG chunk = (len > CC_LONG_MAX) ? CC_LONG_MAX : (CC_LONG)len; + + CC_SHA256_Update(&ctx->c, data, chunk); + + data += chunk; + len -= chunk; + } + + return 0; +} + +int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + CC_SHA256_Final(out, &ctx->c); + return 0; +} + +#endif diff --git a/src/util/hash/common_crypto.h b/src/util/hash/common_crypto.h new file mode 100644 index 00000000000..157712b5f71 --- /dev/null +++ b/src/util/hash/common_crypto.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_hash_common_crypto_h__ +#define INCLUDE_hash_common_crypto_h__ + +#include "hash/sha.h" + +#include + +#ifdef GIT_SHA1_COMMON_CRYPTO +struct git_hash_sha1_ctx { + CC_SHA1_CTX c; +}; +#endif + +#ifdef GIT_SHA256_COMMON_CRYPTO +struct git_hash_sha256_ctx { + CC_SHA256_CTX c; +}; +#endif + +#endif diff --git a/src/util/hash/mbedtls.c b/src/util/hash/mbedtls.c new file mode 100644 index 00000000000..ecdfb7879ce --- /dev/null +++ b/src/util/hash/mbedtls.c @@ -0,0 +1,92 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "mbedtls.h" + +#ifdef GIT_SHA1_MBEDTLS + +int git_hash_sha1_global_init(void) +{ + return 0; +} + +int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx) +{ + return git_hash_sha1_init(ctx); +} + +void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx) +{ + if (ctx) + mbedtls_sha1_free(&ctx->c); +} + +int git_hash_sha1_init(git_hash_sha1_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + mbedtls_sha1_init(&ctx->c); + mbedtls_sha1_starts(&ctx->c); + return 0; +} + +int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len) +{ + GIT_ASSERT_ARG(ctx); + mbedtls_sha1_update(&ctx->c, data, len); + return 0; +} + +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + mbedtls_sha1_finish(&ctx->c, out); + return 0; +} + +#endif + +#ifdef GIT_SHA256_MBEDTLS + +int git_hash_sha256_global_init(void) +{ + return 0; +} + +int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx) +{ + return git_hash_sha256_init(ctx); +} + +void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx) +{ + if (ctx) + mbedtls_sha256_free(&ctx->c); +} + +int git_hash_sha256_init(git_hash_sha256_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + mbedtls_sha256_init(&ctx->c); + mbedtls_sha256_starts(&ctx->c, 0); + return 0; +} + +int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *data, size_t len) +{ + GIT_ASSERT_ARG(ctx); + mbedtls_sha256_update(&ctx->c, data, len); + return 0; +} + +int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + mbedtls_sha256_finish(&ctx->c, out); + return 0; +} + +#endif diff --git a/src/util/hash/mbedtls.h b/src/util/hash/mbedtls.h new file mode 100644 index 00000000000..05fb38b0ec0 --- /dev/null +++ b/src/util/hash/mbedtls.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_hash_mbedtls_h__ +#define INCLUDE_hash_mbedtls_h__ + +#include "hash/sha.h" + +#ifdef GIT_SHA1_MBEDTLS +# include + +struct git_hash_sha1_ctx { + mbedtls_sha1_context c; +}; +#endif + +#ifdef GIT_SHA256_MBEDTLS +# include + +struct git_hash_sha256_ctx { + mbedtls_sha256_context c; +}; +#endif + +#endif /* INCLUDE_hash_sha1_mbedtls_h__ */ diff --git a/src/util/hash/openssl.c b/src/util/hash/openssl.c new file mode 100644 index 00000000000..fac6b4a27d5 --- /dev/null +++ b/src/util/hash/openssl.c @@ -0,0 +1,350 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "openssl.h" + +#if defined(GIT_SHA1_OPENSSL_DYNAMIC) || defined(GIT_SHA256_OPENSSL_DYNAMIC) +# include + +static int handle_count; +static void *openssl_handle; + +static int git_hash_openssl_global_shutdown(void) +{ + if (--handle_count == 0) { + dlclose(openssl_handle); + openssl_handle = NULL; + } + + return 0; +} + +static int git_hash_openssl_global_init(void) +{ + if (!handle_count) { + if ((openssl_handle = dlopen("libssl.so.1.1", RTLD_NOW)) == NULL && + (openssl_handle = dlopen("libssl.1.1.dylib", RTLD_NOW)) == NULL && + (openssl_handle = dlopen("libssl.so.1.0.0", RTLD_NOW)) == NULL && + (openssl_handle = dlopen("libssl.1.0.0.dylib", RTLD_NOW)) == NULL && + (openssl_handle = dlopen("libssl.so.10", RTLD_NOW)) == NULL && + (openssl_handle = dlopen("libssl.so.3", RTLD_NOW)) == NULL && + (openssl_handle = dlopen("libssl.3.dylib", RTLD_NOW)) == NULL) { + git_error_set(GIT_ERROR_SSL, "could not load ssl libraries"); + return -1; + } + } + + if (git_hash_openssl_global_shutdown() < 0) + return -1; + + handle_count++; + return 0; +} + +#endif + +#ifdef GIT_SHA1_OPENSSL_DYNAMIC +static int (*SHA1_Init)(SHA_CTX *c); +static int (*SHA1_Update)(SHA_CTX *c, const void *data, size_t len); +static int (*SHA1_Final)(unsigned char *md, SHA_CTX *c); + +int git_hash_sha1_global_init(void) +{ + if (git_hash_openssl_global_init() < 0) + return -1; + + if ((SHA1_Init = dlsym(openssl_handle, "SHA1_Init")) == NULL || + (SHA1_Update = dlsym(openssl_handle, "SHA1_Update")) == NULL || + (SHA1_Final = dlsym(openssl_handle, "SHA1_Final")) == NULL) { + const char *msg = dlerror(); + git_error_set(GIT_ERROR_SSL, "could not load hash function: %s", msg ? msg : "unknown error"); + return -1; + } + + return 0; +} +#elif GIT_SHA1_OPENSSL +int git_hash_sha1_global_init(void) +{ + return 0; +} +#endif + +#if defined(GIT_SHA1_OPENSSL) || defined(GIT_SHA1_OPENSSL_DYNAMIC) + +int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx) +{ + return git_hash_sha1_init(ctx); +} + +void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx) +{ + GIT_UNUSED(ctx); +} + +int git_hash_sha1_init(git_hash_sha1_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + + if (SHA1_Init(&ctx->c) != 1) { + git_error_set(GIT_ERROR_SHA, "failed to initialize sha1 context"); + return -1; + } + + return 0; +} + +int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len) +{ + GIT_ASSERT_ARG(ctx); + + if (SHA1_Update(&ctx->c, data, len) != 1) { + git_error_set(GIT_ERROR_SHA, "failed to update sha1"); + return -1; + } + + return 0; +} + +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + + if (SHA1_Final(out, &ctx->c) != 1) { + git_error_set(GIT_ERROR_SHA, "failed to finalize sha1"); + return -1; + } + + return 0; +} + +#endif + +#ifdef GIT_SHA1_OPENSSL_FIPS + +static const EVP_MD *SHA1_ENGINE_DIGEST_TYPE = NULL; + +int git_hash_sha1_global_init(void) +{ + SHA1_ENGINE_DIGEST_TYPE = EVP_sha1(); + return SHA1_ENGINE_DIGEST_TYPE != NULL ? 0 : -1; +} + +int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx) +{ + return git_hash_sha1_init(ctx); +} + +void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx) +{ +#if OPENSSL_VERSION_NUMBER < 0x10100000L + EVP_MD_CTX_destroy(ctx->c); +#else + EVP_MD_CTX_free(ctx->c); +#endif +} + +int git_hash_sha1_init(git_hash_sha1_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + GIT_ASSERT(SHA1_ENGINE_DIGEST_TYPE); + +#if OPENSSL_VERSION_NUMBER < 0x10100000L + ctx->c = EVP_MD_CTX_create(); +#else + ctx->c = EVP_MD_CTX_new(); +#endif + + GIT_ASSERT(ctx->c); + + if (EVP_DigestInit_ex(ctx->c, SHA1_ENGINE_DIGEST_TYPE, NULL) != 1) { + git_hash_sha1_ctx_cleanup(ctx); + git_error_set(GIT_ERROR_SHA, "failed to initialize sha1 context"); + return -1; + } + + return 0; +} + +int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len) +{ + GIT_ASSERT_ARG(ctx && ctx->c); + + if (EVP_DigestUpdate(ctx->c, data, len) != 1) { + git_error_set(GIT_ERROR_SHA, "failed to update sha1"); + return -1; + } + + return 0; +} + +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx) +{ + unsigned int len = 0; + + GIT_ASSERT_ARG(ctx && ctx->c); + + if (EVP_DigestFinal(ctx->c, out, &len) != 1) { + git_error_set(GIT_ERROR_SHA, "failed to finalize sha1"); + return -1; + } + + return 0; +} + +#endif + +#ifdef GIT_SHA256_OPENSSL_DYNAMIC +static int (*SHA256_Init)(SHA256_CTX *c); +static int (*SHA256_Update)(SHA256_CTX *c, const void *data, size_t len); +static int (*SHA256_Final)(unsigned char *md, SHA256_CTX *c); + +int git_hash_sha256_global_init(void) +{ + if (git_hash_openssl_global_init() < 0) + return -1; + + if ((SHA256_Init = dlsym(openssl_handle, "SHA256_Init")) == NULL || + (SHA256_Update = dlsym(openssl_handle, "SHA256_Update")) == NULL || + (SHA256_Final = dlsym(openssl_handle, "SHA256_Final")) == NULL) { + const char *msg = dlerror(); + git_error_set(GIT_ERROR_SSL, "could not load hash function: %s", msg ? msg : "unknown error"); + return -1; + } + + return 0; +} +#elif GIT_SHA256_OPENSSL +int git_hash_sha256_global_init(void) +{ + return 0; +} +#endif + +#if defined(GIT_SHA256_OPENSSL) || defined(GIT_SHA256_OPENSSL_DYNAMIC) + +int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx) +{ + return git_hash_sha256_init(ctx); +} + +void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx) +{ + GIT_UNUSED(ctx); +} + +int git_hash_sha256_init(git_hash_sha256_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + + if (SHA256_Init(&ctx->c) != 1) { + git_error_set(GIT_ERROR_SHA, "failed to initialize sha256 context"); + return -1; + } + + return 0; +} + +int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *data, size_t len) +{ + GIT_ASSERT_ARG(ctx); + + if (SHA256_Update(&ctx->c, data, len) != 1) { + git_error_set(GIT_ERROR_SHA, "failed to update sha256"); + return -1; + } + + return 0; +} + +int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + + if (SHA256_Final(out, &ctx->c) != 1) { + git_error_set(GIT_ERROR_SHA, "failed to finalize sha256"); + return -1; + } + + return 0; +} + +#endif + +#ifdef GIT_SHA256_OPENSSL_FIPS + +static const EVP_MD *SHA256_ENGINE_DIGEST_TYPE = NULL; + +int git_hash_sha256_global_init(void) +{ + SHA256_ENGINE_DIGEST_TYPE = EVP_sha256(); + return SHA256_ENGINE_DIGEST_TYPE != NULL ? 0 : -1; +} + +int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx) +{ + return git_hash_sha256_init(ctx); +} + +void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx) +{ +#if OPENSSL_VERSION_NUMBER < 0x10100000L + EVP_MD_CTX_destroy(ctx->c); +#else + EVP_MD_CTX_free(ctx->c); +#endif +} + +int git_hash_sha256_init(git_hash_sha256_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + GIT_ASSERT(SHA256_ENGINE_DIGEST_TYPE); + +#if OPENSSL_VERSION_NUMBER < 0x10100000L + ctx->c = EVP_MD_CTX_create(); +#else + ctx->c = EVP_MD_CTX_new(); +#endif + + GIT_ASSERT(ctx->c); + + if (EVP_DigestInit_ex(ctx->c, SHA256_ENGINE_DIGEST_TYPE, NULL) != 1) { + git_hash_sha256_ctx_cleanup(ctx); + git_error_set(GIT_ERROR_SHA, "failed to initialize sha256 context"); + return -1; + } + + return 0; +} + +int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *data, size_t len) +{ + GIT_ASSERT_ARG(ctx && ctx->c); + + if (EVP_DigestUpdate(ctx->c, data, len) != 1) { + git_error_set(GIT_ERROR_SHA, "failed to update sha256"); + return -1; + } + + return 0; +} + +int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx) +{ + unsigned int len = 0; + + GIT_ASSERT_ARG(ctx && ctx->c); + + if (EVP_DigestFinal(ctx->c, out, &len) != 1) { + git_error_set(GIT_ERROR_SHA, "failed to finalize sha256"); + return -1; + } + + return 0; +} + +#endif diff --git a/src/util/hash/openssl.h b/src/util/hash/openssl.h new file mode 100644 index 00000000000..2ab73c9893c --- /dev/null +++ b/src/util/hash/openssl.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_hash_openssl_h__ +#define INCLUDE_hash_openssl_h__ + +#include "hash/sha.h" + +#if defined(GIT_SHA1_OPENSSL_FIPS) || defined(GIT_SHA256_OPENSSL_FIPS) +# include +#endif + +#if defined(GIT_SHA1_OPENSSL) || defined(GIT_SHA256_OPENSSL) +# include +#endif + +#if defined(GIT_SHA1_OPENSSL_DYNAMIC) +typedef struct { + unsigned int h0, h1, h2, h3, h4; + unsigned int Nl, Nh; + unsigned int data[16]; + unsigned int num; +} SHA_CTX; +#endif + +#if defined(GIT_SHA256_OPENSSL_DYNAMIC) +typedef struct { + unsigned int h[8]; + unsigned int Nl, Nh; + unsigned int data[16]; + unsigned int num, md_len; +} SHA256_CTX; +#endif + +#if defined(GIT_SHA1_OPENSSL) || defined(GIT_SHA1_OPENSSL_DYNAMIC) +struct git_hash_sha1_ctx { + SHA_CTX c; +}; +#endif + +#ifdef GIT_SHA1_OPENSSL_FIPS +struct git_hash_sha1_ctx { + EVP_MD_CTX* c; +}; +#endif + +#if defined(GIT_SHA256_OPENSSL) || defined(GIT_SHA256_OPENSSL_DYNAMIC) +struct git_hash_sha256_ctx { + SHA256_CTX c; +}; +#endif + +#ifdef GIT_SHA256_OPENSSL_FIPS +struct git_hash_sha256_ctx { + EVP_MD_CTX* c; +}; +#endif + +#endif diff --git a/src/util/hash/rfc6234/sha.h b/src/util/hash/rfc6234/sha.h new file mode 100644 index 00000000000..0fbcc50d399 --- /dev/null +++ b/src/util/hash/rfc6234/sha.h @@ -0,0 +1,243 @@ +/**************************** sha.h ****************************/ +/***************** See RFC 6234 for details. *******************/ +/* + Copyright (c) 2011 IETF Trust and the persons identified as + authors of the code. All rights reserved. + + Redistribution and use in source and binary forms, with or + without modification, are permitted provided that the following + conditions are met: + + - Redistributions of source code must retain the above + copyright notice, this list of conditions and + the following disclaimer. + + - Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + - Neither the name of Internet Society, IETF or IETF Trust, nor + the names of specific contributors, may be used to endorse or + promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +#ifndef _SHA_H_ +#define _SHA_H_ + +/* + * Description: + * This file implements the Secure Hash Algorithms + * as defined in the U.S. National Institute of Standards + * and Technology Federal Information Processing Standards + * Publication (FIPS PUB) 180-3 published in October 2008 + * and formerly defined in its predecessors, FIPS PUB 180-1 + * and FIP PUB 180-2. + * + * A combined document showing all algorithms is available at + * http://csrc.nist.gov/publications/fips/ + * fips180-3/fips180-3_final.pdf + * + * The five hashes are defined in these sizes: + * SHA-1 20 byte / 160 bit + * SHA-224 28 byte / 224 bit + * SHA-256 32 byte / 256 bit + * SHA-384 48 byte / 384 bit + * SHA-512 64 byte / 512 bit + * + * Compilation Note: + * These files may be compiled with two options: + * USE_32BIT_ONLY - use 32-bit arithmetic only, for systems + * without 64-bit integers + * + * USE_MODIFIED_MACROS - use alternate form of the SHA_Ch() + * and SHA_Maj() macros that are equivalent + * and potentially faster on many systems + * + */ + +#include +/* + * If you do not have the ISO standard stdint.h header file, then you + * must typedef the following: + * name meaning + * uint64_t unsigned 64-bit integer + * uint32_t unsigned 32-bit integer + * uint8_t unsigned 8-bit integer (i.e., unsigned char) + * int_least16_t integer of >= 16 bits + * + * See stdint-example.h + */ + +#ifndef _SHA_enum_ +#define _SHA_enum_ +/* + * All SHA functions return one of these values. + */ +enum { + shaSuccess = 0, + shaNull, /* Null pointer parameter */ + shaInputTooLong, /* input data too long */ + shaStateError, /* called Input after FinalBits or Result */ + shaBadParam /* passed a bad parameter */ +}; +#endif /* _SHA_enum_ */ + +/* + * These constants hold size information for each of the SHA + * hashing operations + */ +enum { + SHA1_Message_Block_Size = 64, SHA224_Message_Block_Size = 64, + SHA256_Message_Block_Size = 64, SHA384_Message_Block_Size = 128, + SHA512_Message_Block_Size = 128, + USHA_Max_Message_Block_Size = SHA512_Message_Block_Size, + SHA1HashSize = 20, SHA224HashSize = 28, SHA256HashSize = 32, + SHA384HashSize = 48, SHA512HashSize = 64, + USHAMaxHashSize = SHA512HashSize, + + SHA1HashSizeBits = 160, SHA224HashSizeBits = 224, + SHA256HashSizeBits = 256, SHA384HashSizeBits = 384, + SHA512HashSizeBits = 512, USHAMaxHashSizeBits = SHA512HashSizeBits +}; + +/* + * These constants are used in the USHA (Unified SHA) functions. + */ +typedef enum SHAversion { + SHA1, SHA224, SHA256, SHA384, SHA512 +} SHAversion; + +/* + * This structure will hold context information for the SHA-1 + * hashing operation. + */ +typedef struct SHA1Context { + uint32_t Intermediate_Hash[SHA1HashSize/4]; /* Message Digest */ + + uint32_t Length_High; /* Message length in bits */ + uint32_t Length_Low; /* Message length in bits */ + + int_least16_t Message_Block_Index; /* Message_Block array index */ + /* 512-bit message blocks */ + uint8_t Message_Block[SHA1_Message_Block_Size]; + + int Computed; /* Is the hash computed? */ + int Corrupted; /* Cumulative corruption code */ +} SHA1Context; + +/* + * This structure will hold context information for the SHA-256 + * hashing operation. + */ +typedef struct SHA256Context { + uint32_t Intermediate_Hash[SHA256HashSize/4]; /* Message Digest */ + + uint32_t Length_High; /* Message length in bits */ + uint32_t Length_Low; /* Message length in bits */ + + int_least16_t Message_Block_Index; /* Message_Block array index */ + /* 512-bit message blocks */ + uint8_t Message_Block[SHA256_Message_Block_Size]; + + int Computed; /* Is the hash computed? */ + int Corrupted; /* Cumulative corruption code */ +} SHA256Context; + +/* + * This structure will hold context information for the SHA-512 + * hashing operation. + */ +typedef struct SHA512Context { +#ifdef USE_32BIT_ONLY + uint32_t Intermediate_Hash[SHA512HashSize/4]; /* Message Digest */ + uint32_t Length[4]; /* Message length in bits */ +#else /* !USE_32BIT_ONLY */ + uint64_t Intermediate_Hash[SHA512HashSize/8]; /* Message Digest */ + uint64_t Length_High, Length_Low; /* Message length in bits */ +#endif /* USE_32BIT_ONLY */ + + int_least16_t Message_Block_Index; /* Message_Block array index */ + /* 1024-bit message blocks */ + uint8_t Message_Block[SHA512_Message_Block_Size]; + + int Computed; /* Is the hash computed?*/ + int Corrupted; /* Cumulative corruption code */ +} SHA512Context; + +/* + * This structure will hold context information for the SHA-224 + * hashing operation. It uses the SHA-256 structure for computation. + */ +typedef struct SHA256Context SHA224Context; + +/* + * This structure will hold context information for the SHA-384 + * hashing operation. It uses the SHA-512 structure for computation. + */ +typedef struct SHA512Context SHA384Context; + +/* + * Function Prototypes + */ + +/* SHA-1 */ +extern int SHA1Reset(SHA1Context *); +extern int SHA1Input(SHA1Context *, const uint8_t *bytes, + unsigned int bytecount); +extern int SHA1FinalBits(SHA1Context *, uint8_t bits, + unsigned int bit_count); +extern int SHA1Result(SHA1Context *, + uint8_t Message_Digest[SHA1HashSize]); + +/* SHA-224 */ +extern int SHA224Reset(SHA224Context *); +extern int SHA224Input(SHA224Context *, const uint8_t *bytes, + unsigned int bytecount); +extern int SHA224FinalBits(SHA224Context *, uint8_t bits, + unsigned int bit_count); +extern int SHA224Result(SHA224Context *, + uint8_t Message_Digest[SHA224HashSize]); + +/* SHA-256 */ +extern int SHA256Reset(SHA256Context *); +extern int SHA256Input(SHA256Context *, const uint8_t *bytes, + unsigned int bytecount); +extern int SHA256FinalBits(SHA256Context *, uint8_t bits, + unsigned int bit_count); +extern int SHA256Result(SHA256Context *, + uint8_t Message_Digest[SHA256HashSize]); + +/* SHA-384 */ +extern int SHA384Reset(SHA384Context *); +extern int SHA384Input(SHA384Context *, const uint8_t *bytes, + unsigned int bytecount); +extern int SHA384FinalBits(SHA384Context *, uint8_t bits, + unsigned int bit_count); +extern int SHA384Result(SHA384Context *, + uint8_t Message_Digest[SHA384HashSize]); + +/* SHA-512 */ +extern int SHA512Reset(SHA512Context *); +extern int SHA512Input(SHA512Context *, const uint8_t *bytes, + unsigned int bytecount); +extern int SHA512FinalBits(SHA512Context *, uint8_t bits, + unsigned int bit_count); +extern int SHA512Result(SHA512Context *, + uint8_t Message_Digest[SHA512HashSize]); + +#endif /* _SHA_H_ */ diff --git a/src/util/hash/rfc6234/sha224-256.c b/src/util/hash/rfc6234/sha224-256.c new file mode 100644 index 00000000000..c8e0cf85448 --- /dev/null +++ b/src/util/hash/rfc6234/sha224-256.c @@ -0,0 +1,601 @@ +/************************* sha224-256.c ************************/ +/***************** See RFC 6234 for details. *******************/ +/* Copyright (c) 2011 IETF Trust and the persons identified as */ +/* authors of the code. All rights reserved. */ +/* See sha.h for terms of use and redistribution. */ + +/* + * Description: + * This file implements the Secure Hash Algorithms SHA-224 and + * SHA-256 as defined in the U.S. National Institute of Standards + * and Technology Federal Information Processing Standards + * Publication (FIPS PUB) 180-3 published in October 2008 + * and formerly defined in its predecessors, FIPS PUB 180-1 + * and FIP PUB 180-2. + * + * A combined document showing all algorithms is available at + * http://csrc.nist.gov/publications/fips/ + * fips180-3/fips180-3_final.pdf + * + * The SHA-224 and SHA-256 algorithms produce 224-bit and 256-bit + * message digests for a given data stream. It should take about + * 2**n steps to find a message with the same digest as a given + * message and 2**(n/2) to find any two messages with the same + * digest, when n is the digest size in bits. Therefore, this + * algorithm can serve as a means of providing a + * "fingerprint" for a message. + * + * Portability Issues: + * SHA-224 and SHA-256 are defined in terms of 32-bit "words". + * This code uses (included via "sha.h") to define 32- + * and 8-bit unsigned integer types. If your C compiler does not + * support 32-bit unsigned integers, this code is not + * appropriate. + * + * Caveats: + * SHA-224 and SHA-256 are designed to work with messages less + * than 2^64 bits long. This implementation uses SHA224/256Input() + * to hash the bits that are a multiple of the size of an 8-bit + * octet, and then optionally uses SHA224/256FinalBits() + * to hash the final few bits of the input. + */ + +#include "sha.h" + +/* + * These definitions are defined in FIPS 180-3, section 4.1. + * Ch() and Maj() are defined identically in sections 4.1.1, + * 4.1.2, and 4.1.3. + * + * The definitions used in FIPS 180-3 are as follows: + */ +#ifndef USE_MODIFIED_MACROS +#define SHA_Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) +#define SHA_Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) +#else /* USE_MODIFIED_MACROS */ +/* + * The following definitions are equivalent and potentially faster. + */ +#define SHA_Ch(x, y, z) (((x) & ((y) ^ (z))) ^ (z)) +#define SHA_Maj(x, y, z) (((x) & ((y) | (z))) | ((y) & (z))) +#endif /* USE_MODIFIED_MACROS */ + +#define SHA_Parity(x, y, z) ((x) ^ (y) ^ (z)) + +/* Define the SHA shift, rotate left, and rotate right macros */ +#define SHA256_SHR(bits,word) ((word) >> (bits)) +#define SHA256_ROTL(bits,word) \ + (((word) << (bits)) | ((word) >> (32-(bits)))) +#define SHA256_ROTR(bits,word) \ + (((word) >> (bits)) | ((word) << (32-(bits)))) + +/* Define the SHA SIGMA and sigma macros */ +#define SHA256_SIGMA0(word) \ + (SHA256_ROTR( 2,word) ^ SHA256_ROTR(13,word) ^ SHA256_ROTR(22,word)) +#define SHA256_SIGMA1(word) \ + (SHA256_ROTR( 6,word) ^ SHA256_ROTR(11,word) ^ SHA256_ROTR(25,word)) +#define SHA256_sigma0(word) \ + (SHA256_ROTR( 7,word) ^ SHA256_ROTR(18,word) ^ SHA256_SHR( 3,word)) +#define SHA256_sigma1(word) \ + (SHA256_ROTR(17,word) ^ SHA256_ROTR(19,word) ^ SHA256_SHR(10,word)) + +/* + * Add "length" to the length. + * Set Corrupted when overflow has occurred. + */ +static uint32_t addTemp; +#define SHA224_256AddLength(context, length) \ + (addTemp = (context)->Length_Low, (context)->Corrupted = \ + (((context)->Length_Low += (length)) < addTemp) && \ + (++(context)->Length_High == 0) ? shaInputTooLong : \ + (context)->Corrupted ) + +/* Local Function Prototypes */ +static int SHA224_256Reset(SHA256Context *context, uint32_t *H0); +static void SHA224_256ProcessMessageBlock(SHA256Context *context); +static void SHA224_256Finalize(SHA256Context *context, + uint8_t Pad_Byte); +static void SHA224_256PadMessage(SHA256Context *context, + uint8_t Pad_Byte); +static int SHA224_256ResultN(SHA256Context *context, + uint8_t Message_Digest[ ], int HashSize); + +/* Initial Hash Values: FIPS 180-3 section 5.3.2 */ +static uint32_t SHA224_H0[SHA256HashSize/4] = { + 0xC1059ED8, 0x367CD507, 0x3070DD17, 0xF70E5939, + 0xFFC00B31, 0x68581511, 0x64F98FA7, 0xBEFA4FA4 +}; + +/* Initial Hash Values: FIPS 180-3 section 5.3.3 */ +static uint32_t SHA256_H0[SHA256HashSize/4] = { + 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, + 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 +}; + +/* + * SHA224Reset + * + * Description: + * This function will initialize the SHA224Context in preparation + * for computing a new SHA224 message digest. + * + * Parameters: + * context: [in/out] + * The context to reset. + * + * Returns: + * sha Error Code. + */ +int SHA224Reset(SHA224Context *context) +{ + return SHA224_256Reset(context, SHA224_H0); +} + +/* + * SHA224Input + * + * Description: + * This function accepts an array of octets as the next portion + * of the message. + * + * Parameters: + * context: [in/out] + * The SHA context to update. + * message_array[ ]: [in] + * An array of octets representing the next portion of + * the message. + * length: [in] + * The length of the message in message_array. + * + * Returns: + * sha Error Code. + * + */ +int SHA224Input(SHA224Context *context, const uint8_t *message_array, + unsigned int length) +{ + return SHA256Input(context, message_array, length); +} + +/* + * SHA224FinalBits + * + * Description: + * This function will add in any final bits of the message. + * + * Parameters: + * context: [in/out] + * The SHA context to update. + * message_bits: [in] + * The final bits of the message, in the upper portion of the + * byte. (Use 0b###00000 instead of 0b00000### to input the + * three bits ###.) + * length: [in] + * The number of bits in message_bits, between 1 and 7. + * + * Returns: + * sha Error Code. + */ +int SHA224FinalBits(SHA224Context *context, + uint8_t message_bits, unsigned int length) +{ + return SHA256FinalBits(context, message_bits, length); +} + +/* + * SHA224Result + * + * Description: + * This function will return the 224-bit message digest + * into the Message_Digest array provided by the caller. + * NOTE: + * The first octet of hash is stored in the element with index 0, + * the last octet of hash in the element with index 27. + * + * Parameters: + * context: [in/out] + * The context to use to calculate the SHA hash. + * Message_Digest[ ]: [out] + * Where the digest is returned. + * + * Returns: + * sha Error Code. + */ +int SHA224Result(SHA224Context *context, + uint8_t Message_Digest[SHA224HashSize]) +{ + return SHA224_256ResultN(context, Message_Digest, SHA224HashSize); +} + +/* + * SHA256Reset + * + * Description: + * This function will initialize the SHA256Context in preparation + * for computing a new SHA256 message digest. + * + * Parameters: + * context: [in/out] + * The context to reset. + * + * Returns: + * sha Error Code. + */ +int SHA256Reset(SHA256Context *context) +{ + return SHA224_256Reset(context, SHA256_H0); +} + +/* + * SHA256Input + * + * Description: + * This function accepts an array of octets as the next portion + * of the message. + * + * Parameters: + * context: [in/out] + * The SHA context to update. + * message_array[ ]: [in] + * An array of octets representing the next portion of + * the message. + * length: [in] + * The length of the message in message_array. + * + * Returns: + * sha Error Code. + */ +int SHA256Input(SHA256Context *context, const uint8_t *message_array, + unsigned int length) +{ + if (!context) return shaNull; + if (!length) return shaSuccess; + if (!message_array) return shaNull; + if (context->Computed) return context->Corrupted = shaStateError; + if (context->Corrupted) return context->Corrupted; + + while (length--) { + context->Message_Block[context->Message_Block_Index++] = + *message_array; + + if ((SHA224_256AddLength(context, 8) == shaSuccess) && + (context->Message_Block_Index == SHA256_Message_Block_Size)) + SHA224_256ProcessMessageBlock(context); + + message_array++; + } + + return context->Corrupted; + +} + +/* + * SHA256FinalBits + * + * Description: + * This function will add in any final bits of the message. + * + * Parameters: + * context: [in/out] + * The SHA context to update. + * message_bits: [in] + * The final bits of the message, in the upper portion of the + * byte. (Use 0b###00000 instead of 0b00000### to input the + * three bits ###.) + * length: [in] + * The number of bits in message_bits, between 1 and 7. + * + * Returns: + * sha Error Code. + */ +int SHA256FinalBits(SHA256Context *context, + uint8_t message_bits, unsigned int length) +{ + static uint8_t masks[8] = { + /* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80, + /* 2 0b11000000 */ 0xC0, /* 3 0b11100000 */ 0xE0, + /* 4 0b11110000 */ 0xF0, /* 5 0b11111000 */ 0xF8, + /* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE + }; + static uint8_t markbit[8] = { + /* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40, + /* 2 0b00100000 */ 0x20, /* 3 0b00010000 */ 0x10, + /* 4 0b00001000 */ 0x08, /* 5 0b00000100 */ 0x04, + /* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01 + }; + + if (!context) return shaNull; + if (!length) return shaSuccess; + if (context->Corrupted) return context->Corrupted; + if (context->Computed) return context->Corrupted = shaStateError; + if (length >= 8) return context->Corrupted = shaBadParam; + + SHA224_256AddLength(context, length); + SHA224_256Finalize(context, (uint8_t) + ((message_bits & masks[length]) | markbit[length])); + + return context->Corrupted; +} + +/* + * SHA256Result + * + * Description: + * This function will return the 256-bit message digest + * into the Message_Digest array provided by the caller. + * NOTE: + * The first octet of hash is stored in the element with index 0, + * the last octet of hash in the element with index 31. + * + * Parameters: + * context: [in/out] + * The context to use to calculate the SHA hash. + * Message_Digest[ ]: [out] + * Where the digest is returned. + * + * Returns: + * sha Error Code. + */ +int SHA256Result(SHA256Context *context, + uint8_t Message_Digest[SHA256HashSize]) +{ + return SHA224_256ResultN(context, Message_Digest, SHA256HashSize); +} + +/* + * SHA224_256Reset + * + * Description: + * This helper function will initialize the SHA256Context in + * preparation for computing a new SHA-224 or SHA-256 message digest. + * + * Parameters: + * context: [in/out] + * The context to reset. + * H0[ ]: [in] + * The initial hash value array to use. + * + * Returns: + * sha Error Code. + */ +static int SHA224_256Reset(SHA256Context *context, uint32_t *H0) +{ + if (!context) return shaNull; + + context->Length_High = context->Length_Low = 0; + context->Message_Block_Index = 0; + + context->Intermediate_Hash[0] = H0[0]; + context->Intermediate_Hash[1] = H0[1]; + context->Intermediate_Hash[2] = H0[2]; + context->Intermediate_Hash[3] = H0[3]; + context->Intermediate_Hash[4] = H0[4]; + context->Intermediate_Hash[5] = H0[5]; + context->Intermediate_Hash[6] = H0[6]; + context->Intermediate_Hash[7] = H0[7]; + + context->Computed = 0; + context->Corrupted = shaSuccess; + + return shaSuccess; +} + +/* + * SHA224_256ProcessMessageBlock + * + * Description: + * This helper function will process the next 512 bits of the + * message stored in the Message_Block array. + * + * Parameters: + * context: [in/out] + * The SHA context to update. + * + * Returns: + * Nothing. + * + * Comments: + * Many of the variable names in this code, especially the + * single character names, were used because those were the + * names used in the Secure Hash Standard. + */ +static void SHA224_256ProcessMessageBlock(SHA256Context *context) +{ + /* Constants defined in FIPS 180-3, section 4.2.2 */ + static const uint32_t K[64] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, + 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, + 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, + 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, + 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, + 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, + 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, + 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, + 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, + 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 + }; + int t, t4; /* Loop counter */ + uint32_t temp1, temp2; /* Temporary word value */ + uint32_t W[64]; /* Word sequence */ + uint32_t A, B, C, D, E, F, G, H; /* Word buffers */ + + /* + * Initialize the first 16 words in the array W + */ + for (t = t4 = 0; t < 16; t++, t4 += 4) + W[t] = (((uint32_t)context->Message_Block[t4]) << 24) | + (((uint32_t)context->Message_Block[t4 + 1]) << 16) | + (((uint32_t)context->Message_Block[t4 + 2]) << 8) | + (((uint32_t)context->Message_Block[t4 + 3])); + + for (t = 16; t < 64; t++) + W[t] = SHA256_sigma1(W[t-2]) + W[t-7] + + SHA256_sigma0(W[t-15]) + W[t-16]; + + A = context->Intermediate_Hash[0]; + B = context->Intermediate_Hash[1]; + C = context->Intermediate_Hash[2]; + D = context->Intermediate_Hash[3]; + E = context->Intermediate_Hash[4]; + F = context->Intermediate_Hash[5]; + G = context->Intermediate_Hash[6]; + H = context->Intermediate_Hash[7]; + + for (t = 0; t < 64; t++) { + temp1 = H + SHA256_SIGMA1(E) + SHA_Ch(E,F,G) + K[t] + W[t]; + temp2 = SHA256_SIGMA0(A) + SHA_Maj(A,B,C); + H = G; + G = F; + F = E; + E = D + temp1; + D = C; + C = B; + B = A; + A = temp1 + temp2; + } + + context->Intermediate_Hash[0] += A; + context->Intermediate_Hash[1] += B; + context->Intermediate_Hash[2] += C; + context->Intermediate_Hash[3] += D; + context->Intermediate_Hash[4] += E; + context->Intermediate_Hash[5] += F; + context->Intermediate_Hash[6] += G; + context->Intermediate_Hash[7] += H; + + context->Message_Block_Index = 0; +} + +/* + * SHA224_256Finalize + * + * Description: + * This helper function finishes off the digest calculations. + * + * Parameters: + * context: [in/out] + * The SHA context to update. + * Pad_Byte: [in] + * The last byte to add to the message block before the 0-padding + * and length. This will contain the last bits of the message + * followed by another single bit. If the message was an + * exact multiple of 8-bits long, Pad_Byte will be 0x80. + * + * Returns: + * sha Error Code. + */ +static void SHA224_256Finalize(SHA256Context *context, + uint8_t Pad_Byte) +{ + int i; + SHA224_256PadMessage(context, Pad_Byte); + /* message may be sensitive, so clear it out */ + for (i = 0; i < SHA256_Message_Block_Size; ++i) + context->Message_Block[i] = 0; + context->Length_High = 0; /* and clear length */ + context->Length_Low = 0; + context->Computed = 1; +} + +/* + * SHA224_256PadMessage + * + * Description: + * According to the standard, the message must be padded to the next + * even multiple of 512 bits. The first padding bit must be a '1'. + * The last 64 bits represent the length of the original message. + * All bits in between should be 0. This helper function will pad + * the message according to those rules by filling the + * Message_Block array accordingly. When it returns, it can be + * assumed that the message digest has been computed. + * + * Parameters: + * context: [in/out] + * The context to pad. + * Pad_Byte: [in] + * The last byte to add to the message block before the 0-padding + * and length. This will contain the last bits of the message + * followed by another single bit. If the message was an + * exact multiple of 8-bits long, Pad_Byte will be 0x80. + * + * Returns: + * Nothing. + */ +static void SHA224_256PadMessage(SHA256Context *context, + uint8_t Pad_Byte) +{ + /* + * Check to see if the current message block is too small to hold + * the initial padding bits and length. If so, we will pad the + * block, process it, and then continue padding into a second + * block. + */ + if (context->Message_Block_Index >= (SHA256_Message_Block_Size-8)) { + context->Message_Block[context->Message_Block_Index++] = Pad_Byte; + while (context->Message_Block_Index < SHA256_Message_Block_Size) + context->Message_Block[context->Message_Block_Index++] = 0; + SHA224_256ProcessMessageBlock(context); + } else + context->Message_Block[context->Message_Block_Index++] = Pad_Byte; + + while (context->Message_Block_Index < (SHA256_Message_Block_Size-8)) + context->Message_Block[context->Message_Block_Index++] = 0; + + /* + * Store the message length as the last 8 octets + */ + context->Message_Block[56] = (uint8_t)(context->Length_High >> 24); + context->Message_Block[57] = (uint8_t)(context->Length_High >> 16); + context->Message_Block[58] = (uint8_t)(context->Length_High >> 8); + context->Message_Block[59] = (uint8_t)(context->Length_High); + context->Message_Block[60] = (uint8_t)(context->Length_Low >> 24); + context->Message_Block[61] = (uint8_t)(context->Length_Low >> 16); + context->Message_Block[62] = (uint8_t)(context->Length_Low >> 8); + context->Message_Block[63] = (uint8_t)(context->Length_Low); + + SHA224_256ProcessMessageBlock(context); +} + +/* + * SHA224_256ResultN + * + * Description: + * This helper function will return the 224-bit or 256-bit message + * digest into the Message_Digest array provided by the caller. + * NOTE: + * The first octet of hash is stored in the element with index 0, + * the last octet of hash in the element with index 27/31. + * + * Parameters: + * context: [in/out] + * The context to use to calculate the SHA hash. + * Message_Digest[ ]: [out] + * Where the digest is returned. + * HashSize: [in] + * The size of the hash, either 28 or 32. + * + * Returns: + * sha Error Code. + */ +static int SHA224_256ResultN(SHA256Context *context, + uint8_t Message_Digest[ ], int HashSize) +{ + int i; + + if (!context) return shaNull; + if (!Message_Digest) return shaNull; + if (context->Corrupted) return context->Corrupted; + + if (!context->Computed) + SHA224_256Finalize(context, 0x80); + + for (i = 0; i < HashSize; ++i) + Message_Digest[i] = (uint8_t) + (context->Intermediate_Hash[i>>2] >> 8 * ( 3 - ( i & 0x03 ) )); + + return shaSuccess; +} + diff --git a/src/util/hash/sha.h b/src/util/hash/sha.h new file mode 100644 index 00000000000..f9d04814237 --- /dev/null +++ b/src/util/hash/sha.h @@ -0,0 +1,75 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_hash_sha_h__ +#define INCLUDE_hash_sha_h__ + +#include "git2_util.h" + +typedef struct git_hash_sha1_ctx git_hash_sha1_ctx; +typedef struct git_hash_sha256_ctx git_hash_sha256_ctx; + +#if defined(GIT_SHA1_BUILTIN) +# include "collisiondetect.h" +#endif + +#if defined(GIT_SHA1_COMMON_CRYPTO) || defined(GIT_SHA256_COMMON_CRYPTO) +# include "common_crypto.h" +#endif + +#if defined(GIT_SHA1_OPENSSL) || \ + defined(GIT_SHA1_OPENSSL_DYNAMIC) || \ + defined(GIT_SHA1_OPENSSL_FIPS) || \ + defined(GIT_SHA256_OPENSSL) || \ + defined(GIT_SHA256_OPENSSL_DYNAMIC) || \ + defined(GIT_SHA256_OPENSSL_FIPS) +# include "openssl.h" +#endif + +#if defined(GIT_SHA1_WIN32) || defined(GIT_SHA256_WIN32) +# include "win32.h" +#endif + +#if defined(GIT_SHA1_MBEDTLS) || defined(GIT_SHA256_MBEDTLS) +# include "mbedtls.h" +#endif + +#if defined(GIT_SHA256_BUILTIN) +# include "builtin.h" +#endif + +/* + * SHA1 + */ + +#define GIT_HASH_SHA1_SIZE 20 + +int git_hash_sha1_global_init(void); + +int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx); +void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx); + +int git_hash_sha1_init(git_hash_sha1_ctx *c); +int git_hash_sha1_update(git_hash_sha1_ctx *c, const void *data, size_t len); +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *c); + +/* + * SHA256 + */ + +#define GIT_HASH_SHA256_SIZE 32 + +int git_hash_sha256_global_init(void); + +int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx); +void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx); + +int git_hash_sha256_init(git_hash_sha256_ctx *c); +int git_hash_sha256_update(git_hash_sha256_ctx *c, const void *data, size_t len); +int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *c); + +#endif diff --git a/src/util/hash/sha1dc/sha1.c b/src/util/hash/sha1dc/sha1.c new file mode 100644 index 00000000000..929822728a3 --- /dev/null +++ b/src/util/hash/sha1dc/sha1.c @@ -0,0 +1,1909 @@ +/*** +* Copyright 2017 Marc Stevens , Dan Shumow (danshu@microsoft.com) +* Distributed under the MIT Software License. +* See accompanying file LICENSE.txt or copy at +* https://opensource.org/licenses/MIT +***/ + +#ifndef SHA1DC_NO_STANDARD_INCLUDES +#include +#include +#include +#include +#include /* make sure macros like _BIG_ENDIAN visible */ +#endif + +#ifdef SHA1DC_CUSTOM_INCLUDE_SHA1_C +#include SHA1DC_CUSTOM_INCLUDE_SHA1_C +#endif + +#ifndef SHA1DC_INIT_SAFE_HASH_DEFAULT +#define SHA1DC_INIT_SAFE_HASH_DEFAULT 1 +#endif + +#include "sha1.h" +#include "ubc_check.h" + +#if (defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || \ + defined(i386) || defined(__i386) || defined(__i386__) || defined(__i486__) || \ + defined(__i586__) || defined(__i686__) || defined(_M_IX86) || defined(__X86__) || \ + defined(_X86_) || defined(__THW_INTEL__) || defined(__I86__) || defined(__INTEL__) || \ + defined(__386) || defined(_M_X64) || defined(_M_AMD64)) +#define SHA1DC_ON_INTEL_LIKE_PROCESSOR +#endif + +/* + Because Little-Endian architectures are most common, + we only set SHA1DC_BIGENDIAN if one of these conditions is met. + Note that all MSFT platforms are little endian, + so none of these will be defined under the MSC compiler. + If you are compiling on a big endian platform and your compiler does not define one of these, + you will have to add whatever macros your tool chain defines to indicate Big-Endianness. + */ + +#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) +/* + * Should detect Big Endian under GCC since at least 4.6.0 (gcc svn + * rev #165881). See + * https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html + * + * This also works under clang since 3.2, it copied the GCC-ism. See + * clang.git's 3b198a97d2 ("Preprocessor: add __BYTE_ORDER__ + * predefined macro", 2012-07-27) + */ +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +#define SHA1DC_BIGENDIAN +#endif + +/* Not under GCC-alike */ +#elif defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) +/* + * Should detect Big Endian under glibc.git since 14245eb70e ("entered + * into RCS", 1992-11-25). Defined in which will have been + * brought in by standard headers. See glibc.git and + * https://sourceforge.net/p/predef/wiki/Endianness/ + */ +#if __BYTE_ORDER == __BIG_ENDIAN +#define SHA1DC_BIGENDIAN +#endif + +/* Not under GCC-alike or glibc */ +#elif defined(_BYTE_ORDER) && defined(_BIG_ENDIAN) && defined(_LITTLE_ENDIAN) +/* + * *BSD and newlib (embedded linux, cygwin, etc). + * the defined(_BIG_ENDIAN) && defined(_LITTLE_ENDIAN) part prevents + * this condition from matching with Solaris/sparc. + * (Solaris defines only one endian macro) + */ +#if _BYTE_ORDER == _BIG_ENDIAN +#define SHA1DC_BIGENDIAN +#endif + +/* Not under GCC-alike or glibc or *BSD or newlib */ +#elif (defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \ + defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || \ + defined(__sparc)) +/* + * Should define Big Endian for a whitelist of known processors. See + * https://sourceforge.net/p/predef/wiki/Endianness/ and + * http://www.oracle.com/technetwork/server-storage/solaris/portingtosolaris-138514.html + */ +#define SHA1DC_BIGENDIAN + +/* Not under GCC-alike or glibc or *BSD or newlib or */ +#elif (defined(_AIX) || defined(__hpux)) + +/* + * Defines Big Endian on a whitelist of OSs that are known to be Big + * Endian-only. See + * https://public-inbox.org/git/93056823-2740-d072-1ebd-46b440b33d7e@felt.demon.nl/ + */ +#define SHA1DC_BIGENDIAN + +/* Not under GCC-alike or glibc or *BSD or newlib or or */ +#elif defined(SHA1DC_ON_INTEL_LIKE_PROCESSOR) +/* + * As a last resort before we do anything else we're not 100% sure + * about below, we blacklist specific processors here. We could add + * more, see e.g. https://wiki.debian.org/ArchitectureSpecificsMemo + */ +#else /* Not under GCC-alike or glibc or *BSD or newlib or or or */ + +/* We do nothing more here for now */ +/*#error "Uncomment this to see if you fall through all the detection"*/ + +#endif /* Big Endian detection */ + +#if (defined(SHA1DC_FORCE_LITTLEENDIAN) && defined(SHA1DC_BIGENDIAN)) +#undef SHA1DC_BIGENDIAN +#endif +#if (defined(SHA1DC_FORCE_BIGENDIAN) && !defined(SHA1DC_BIGENDIAN)) +#define SHA1DC_BIGENDIAN +#endif +/*ENDIANNESS SELECTION*/ + +#ifndef SHA1DC_FORCE_ALIGNED_ACCESS +#if defined(SHA1DC_FORCE_UNALIGNED_ACCESS) || defined(SHA1DC_ON_INTEL_LIKE_PROCESSOR) +#define SHA1DC_ALLOW_UNALIGNED_ACCESS +#endif /*UNALIGNED ACCESS DETECTION*/ +#endif /*FORCE ALIGNED ACCESS*/ + +#define rotate_right(x,n) (((x)>>(n))|((x)<<(32-(n)))) +#define rotate_left(x,n) (((x)<<(n))|((x)>>(32-(n)))) + +#define sha1_bswap32(x) \ + {x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF); x = (x << 16) | (x >> 16);} + +#define sha1_mix(W, t) (rotate_left(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1)) + +#ifdef SHA1DC_BIGENDIAN + #define sha1_load(m, t, temp) { temp = m[t]; } +#else + #define sha1_load(m, t, temp) { temp = m[t]; sha1_bswap32(temp); } +#endif + +#define sha1_store(W, t, x) *(volatile uint32_t *)&W[t] = x + +#define sha1_f1(b,c,d) ((d)^((b)&((c)^(d)))) +#define sha1_f2(b,c,d) ((b)^(c)^(d)) +#define sha1_f3(b,c,d) (((b)&(c))+((d)&((b)^(c)))) +#define sha1_f4(b,c,d) ((b)^(c)^(d)) + +#define HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, m, t) \ + { e += rotate_left(a, 5) + sha1_f1(b,c,d) + 0x5A827999 + m[t]; b = rotate_left(b, 30); } +#define HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, m, t) \ + { e += rotate_left(a, 5) + sha1_f2(b,c,d) + 0x6ED9EBA1 + m[t]; b = rotate_left(b, 30); } +#define HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, m, t) \ + { e += rotate_left(a, 5) + sha1_f3(b,c,d) + 0x8F1BBCDC + m[t]; b = rotate_left(b, 30); } +#define HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, m, t) \ + { e += rotate_left(a, 5) + sha1_f4(b,c,d) + 0xCA62C1D6 + m[t]; b = rotate_left(b, 30); } + +#define HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(a, b, c, d, e, m, t) \ + { b = rotate_right(b, 30); e -= rotate_left(a, 5) + sha1_f1(b,c,d) + 0x5A827999 + m[t]; } +#define HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(a, b, c, d, e, m, t) \ + { b = rotate_right(b, 30); e -= rotate_left(a, 5) + sha1_f2(b,c,d) + 0x6ED9EBA1 + m[t]; } +#define HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(a, b, c, d, e, m, t) \ + { b = rotate_right(b, 30); e -= rotate_left(a, 5) + sha1_f3(b,c,d) + 0x8F1BBCDC + m[t]; } +#define HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(a, b, c, d, e, m, t) \ + { b = rotate_right(b, 30); e -= rotate_left(a, 5) + sha1_f4(b,c,d) + 0xCA62C1D6 + m[t]; } + +#define SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(a, b, c, d, e, m, W, t, temp) \ + {sha1_load(m, t, temp); sha1_store(W, t, temp); e += temp + rotate_left(a, 5) + sha1_f1(b,c,d) + 0x5A827999; b = rotate_left(b, 30);} + +#define SHA1COMPRESS_FULL_ROUND1_STEP_EXPAND(a, b, c, d, e, W, t, temp) \ + {temp = sha1_mix(W, t); sha1_store(W, t, temp); e += temp + rotate_left(a, 5) + sha1_f1(b,c,d) + 0x5A827999; b = rotate_left(b, 30); } + +#define SHA1COMPRESS_FULL_ROUND2_STEP(a, b, c, d, e, W, t, temp) \ + {temp = sha1_mix(W, t); sha1_store(W, t, temp); e += temp + rotate_left(a, 5) + sha1_f2(b,c,d) + 0x6ED9EBA1; b = rotate_left(b, 30); } + +#define SHA1COMPRESS_FULL_ROUND3_STEP(a, b, c, d, e, W, t, temp) \ + {temp = sha1_mix(W, t); sha1_store(W, t, temp); e += temp + rotate_left(a, 5) + sha1_f3(b,c,d) + 0x8F1BBCDC; b = rotate_left(b, 30); } + +#define SHA1COMPRESS_FULL_ROUND4_STEP(a, b, c, d, e, W, t, temp) \ + {temp = sha1_mix(W, t); sha1_store(W, t, temp); e += temp + rotate_left(a, 5) + sha1_f4(b,c,d) + 0xCA62C1D6; b = rotate_left(b, 30); } + + +#define SHA1_STORE_STATE(i) states[i][0] = a; states[i][1] = b; states[i][2] = c; states[i][3] = d; states[i][4] = e; + +#ifdef BUILDNOCOLLDETECTSHA1COMPRESSION +void sha1_compression(uint32_t ihv[5], const uint32_t m[16]) +{ + uint32_t W[80]; + uint32_t a,b,c,d,e; + unsigned i; + + memcpy(W, m, 16 * 4); + for (i = 16; i < 80; ++i) + W[i] = sha1_mix(W, i); + + a = ihv[0]; b = ihv[1]; c = ihv[2]; d = ihv[3]; e = ihv[4]; + + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 0); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 1); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 2); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 3); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 4); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 5); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 6); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 7); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 8); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 9); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 10); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 11); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 12); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 13); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 14); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 15); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 16); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 17); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 18); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 19); + + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 20); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 21); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 22); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 23); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 24); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 25); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 26); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 27); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 28); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 29); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 30); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 31); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 32); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 33); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 34); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 35); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 36); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 37); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 38); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 39); + + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 40); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 41); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 42); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 43); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 44); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 45); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 46); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 47); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 48); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 49); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 50); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 51); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 52); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 53); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 54); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 55); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 56); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 57); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 58); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 59); + + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 60); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 61); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 62); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 63); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 64); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 65); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 66); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 67); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 68); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 69); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 70); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 71); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 72); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 73); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 74); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 75); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 76); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 77); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 78); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 79); + + ihv[0] += a; ihv[1] += b; ihv[2] += c; ihv[3] += d; ihv[4] += e; +} +#endif /*BUILDNOCOLLDETECTSHA1COMPRESSION*/ + + +static void sha1_compression_W(uint32_t ihv[5], const uint32_t W[80]) +{ + uint32_t a = ihv[0], b = ihv[1], c = ihv[2], d = ihv[3], e = ihv[4]; + + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 0); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 1); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 2); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 3); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 4); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 5); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 6); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 7); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 8); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 9); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 10); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 11); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 12); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 13); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 14); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 15); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 16); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 17); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 18); + HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 19); + + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 20); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 21); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 22); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 23); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 24); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 25); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 26); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 27); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 28); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 29); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 30); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 31); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 32); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 33); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 34); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 35); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 36); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 37); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 38); + HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 39); + + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 40); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 41); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 42); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 43); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 44); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 45); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 46); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 47); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 48); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 49); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 50); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 51); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 52); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 53); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 54); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 55); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 56); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 57); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 58); + HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 59); + + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 60); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 61); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 62); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 63); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 64); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 65); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 66); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 67); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 68); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 69); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 70); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 71); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 72); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 73); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 74); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 75); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 76); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 77); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 78); + HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 79); + + ihv[0] += a; ihv[1] += b; ihv[2] += c; ihv[3] += d; ihv[4] += e; +} + + + +void sha1_compression_states(uint32_t ihv[5], const uint32_t m[16], uint32_t W[80], uint32_t states[80][5]) +{ + uint32_t a = ihv[0], b = ihv[1], c = ihv[2], d = ihv[3], e = ihv[4]; + uint32_t temp; + +#ifdef DOSTORESTATE00 + SHA1_STORE_STATE(0) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(a, b, c, d, e, m, W, 0, temp); + +#ifdef DOSTORESTATE01 + SHA1_STORE_STATE(1) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(e, a, b, c, d, m, W, 1, temp); + +#ifdef DOSTORESTATE02 + SHA1_STORE_STATE(2) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(d, e, a, b, c, m, W, 2, temp); + +#ifdef DOSTORESTATE03 + SHA1_STORE_STATE(3) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(c, d, e, a, b, m, W, 3, temp); + +#ifdef DOSTORESTATE04 + SHA1_STORE_STATE(4) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(b, c, d, e, a, m, W, 4, temp); + +#ifdef DOSTORESTATE05 + SHA1_STORE_STATE(5) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(a, b, c, d, e, m, W, 5, temp); + +#ifdef DOSTORESTATE06 + SHA1_STORE_STATE(6) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(e, a, b, c, d, m, W, 6, temp); + +#ifdef DOSTORESTATE07 + SHA1_STORE_STATE(7) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(d, e, a, b, c, m, W, 7, temp); + +#ifdef DOSTORESTATE08 + SHA1_STORE_STATE(8) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(c, d, e, a, b, m, W, 8, temp); + +#ifdef DOSTORESTATE09 + SHA1_STORE_STATE(9) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(b, c, d, e, a, m, W, 9, temp); + +#ifdef DOSTORESTATE10 + SHA1_STORE_STATE(10) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(a, b, c, d, e, m, W, 10, temp); + +#ifdef DOSTORESTATE11 + SHA1_STORE_STATE(11) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(e, a, b, c, d, m, W, 11, temp); + +#ifdef DOSTORESTATE12 + SHA1_STORE_STATE(12) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(d, e, a, b, c, m, W, 12, temp); + +#ifdef DOSTORESTATE13 + SHA1_STORE_STATE(13) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(c, d, e, a, b, m, W, 13, temp); + +#ifdef DOSTORESTATE14 + SHA1_STORE_STATE(14) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(b, c, d, e, a, m, W, 14, temp); + +#ifdef DOSTORESTATE15 + SHA1_STORE_STATE(15) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(a, b, c, d, e, m, W, 15, temp); + +#ifdef DOSTORESTATE16 + SHA1_STORE_STATE(16) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_EXPAND(e, a, b, c, d, W, 16, temp); + +#ifdef DOSTORESTATE17 + SHA1_STORE_STATE(17) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_EXPAND(d, e, a, b, c, W, 17, temp); + +#ifdef DOSTORESTATE18 + SHA1_STORE_STATE(18) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_EXPAND(c, d, e, a, b, W, 18, temp); + +#ifdef DOSTORESTATE19 + SHA1_STORE_STATE(19) +#endif + SHA1COMPRESS_FULL_ROUND1_STEP_EXPAND(b, c, d, e, a, W, 19, temp); + + + +#ifdef DOSTORESTATE20 + SHA1_STORE_STATE(20) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(a, b, c, d, e, W, 20, temp); + +#ifdef DOSTORESTATE21 + SHA1_STORE_STATE(21) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(e, a, b, c, d, W, 21, temp); + +#ifdef DOSTORESTATE22 + SHA1_STORE_STATE(22) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(d, e, a, b, c, W, 22, temp); + +#ifdef DOSTORESTATE23 + SHA1_STORE_STATE(23) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(c, d, e, a, b, W, 23, temp); + +#ifdef DOSTORESTATE24 + SHA1_STORE_STATE(24) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(b, c, d, e, a, W, 24, temp); + +#ifdef DOSTORESTATE25 + SHA1_STORE_STATE(25) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(a, b, c, d, e, W, 25, temp); + +#ifdef DOSTORESTATE26 + SHA1_STORE_STATE(26) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(e, a, b, c, d, W, 26, temp); + +#ifdef DOSTORESTATE27 + SHA1_STORE_STATE(27) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(d, e, a, b, c, W, 27, temp); + +#ifdef DOSTORESTATE28 + SHA1_STORE_STATE(28) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(c, d, e, a, b, W, 28, temp); + +#ifdef DOSTORESTATE29 + SHA1_STORE_STATE(29) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(b, c, d, e, a, W, 29, temp); + +#ifdef DOSTORESTATE30 + SHA1_STORE_STATE(30) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(a, b, c, d, e, W, 30, temp); + +#ifdef DOSTORESTATE31 + SHA1_STORE_STATE(31) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(e, a, b, c, d, W, 31, temp); + +#ifdef DOSTORESTATE32 + SHA1_STORE_STATE(32) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(d, e, a, b, c, W, 32, temp); + +#ifdef DOSTORESTATE33 + SHA1_STORE_STATE(33) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(c, d, e, a, b, W, 33, temp); + +#ifdef DOSTORESTATE34 + SHA1_STORE_STATE(34) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(b, c, d, e, a, W, 34, temp); + +#ifdef DOSTORESTATE35 + SHA1_STORE_STATE(35) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(a, b, c, d, e, W, 35, temp); + +#ifdef DOSTORESTATE36 + SHA1_STORE_STATE(36) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(e, a, b, c, d, W, 36, temp); + +#ifdef DOSTORESTATE37 + SHA1_STORE_STATE(37) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(d, e, a, b, c, W, 37, temp); + +#ifdef DOSTORESTATE38 + SHA1_STORE_STATE(38) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(c, d, e, a, b, W, 38, temp); + +#ifdef DOSTORESTATE39 + SHA1_STORE_STATE(39) +#endif + SHA1COMPRESS_FULL_ROUND2_STEP(b, c, d, e, a, W, 39, temp); + + + +#ifdef DOSTORESTATE40 + SHA1_STORE_STATE(40) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(a, b, c, d, e, W, 40, temp); + +#ifdef DOSTORESTATE41 + SHA1_STORE_STATE(41) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(e, a, b, c, d, W, 41, temp); + +#ifdef DOSTORESTATE42 + SHA1_STORE_STATE(42) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(d, e, a, b, c, W, 42, temp); + +#ifdef DOSTORESTATE43 + SHA1_STORE_STATE(43) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(c, d, e, a, b, W, 43, temp); + +#ifdef DOSTORESTATE44 + SHA1_STORE_STATE(44) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(b, c, d, e, a, W, 44, temp); + +#ifdef DOSTORESTATE45 + SHA1_STORE_STATE(45) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(a, b, c, d, e, W, 45, temp); + +#ifdef DOSTORESTATE46 + SHA1_STORE_STATE(46) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(e, a, b, c, d, W, 46, temp); + +#ifdef DOSTORESTATE47 + SHA1_STORE_STATE(47) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(d, e, a, b, c, W, 47, temp); + +#ifdef DOSTORESTATE48 + SHA1_STORE_STATE(48) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(c, d, e, a, b, W, 48, temp); + +#ifdef DOSTORESTATE49 + SHA1_STORE_STATE(49) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(b, c, d, e, a, W, 49, temp); + +#ifdef DOSTORESTATE50 + SHA1_STORE_STATE(50) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(a, b, c, d, e, W, 50, temp); + +#ifdef DOSTORESTATE51 + SHA1_STORE_STATE(51) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(e, a, b, c, d, W, 51, temp); + +#ifdef DOSTORESTATE52 + SHA1_STORE_STATE(52) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(d, e, a, b, c, W, 52, temp); + +#ifdef DOSTORESTATE53 + SHA1_STORE_STATE(53) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(c, d, e, a, b, W, 53, temp); + +#ifdef DOSTORESTATE54 + SHA1_STORE_STATE(54) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(b, c, d, e, a, W, 54, temp); + +#ifdef DOSTORESTATE55 + SHA1_STORE_STATE(55) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(a, b, c, d, e, W, 55, temp); + +#ifdef DOSTORESTATE56 + SHA1_STORE_STATE(56) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(e, a, b, c, d, W, 56, temp); + +#ifdef DOSTORESTATE57 + SHA1_STORE_STATE(57) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(d, e, a, b, c, W, 57, temp); + +#ifdef DOSTORESTATE58 + SHA1_STORE_STATE(58) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(c, d, e, a, b, W, 58, temp); + +#ifdef DOSTORESTATE59 + SHA1_STORE_STATE(59) +#endif + SHA1COMPRESS_FULL_ROUND3_STEP(b, c, d, e, a, W, 59, temp); + + + + +#ifdef DOSTORESTATE60 + SHA1_STORE_STATE(60) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(a, b, c, d, e, W, 60, temp); + +#ifdef DOSTORESTATE61 + SHA1_STORE_STATE(61) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(e, a, b, c, d, W, 61, temp); + +#ifdef DOSTORESTATE62 + SHA1_STORE_STATE(62) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(d, e, a, b, c, W, 62, temp); + +#ifdef DOSTORESTATE63 + SHA1_STORE_STATE(63) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(c, d, e, a, b, W, 63, temp); + +#ifdef DOSTORESTATE64 + SHA1_STORE_STATE(64) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(b, c, d, e, a, W, 64, temp); + +#ifdef DOSTORESTATE65 + SHA1_STORE_STATE(65) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(a, b, c, d, e, W, 65, temp); + +#ifdef DOSTORESTATE66 + SHA1_STORE_STATE(66) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(e, a, b, c, d, W, 66, temp); + +#ifdef DOSTORESTATE67 + SHA1_STORE_STATE(67) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(d, e, a, b, c, W, 67, temp); + +#ifdef DOSTORESTATE68 + SHA1_STORE_STATE(68) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(c, d, e, a, b, W, 68, temp); + +#ifdef DOSTORESTATE69 + SHA1_STORE_STATE(69) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(b, c, d, e, a, W, 69, temp); + +#ifdef DOSTORESTATE70 + SHA1_STORE_STATE(70) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(a, b, c, d, e, W, 70, temp); + +#ifdef DOSTORESTATE71 + SHA1_STORE_STATE(71) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(e, a, b, c, d, W, 71, temp); + +#ifdef DOSTORESTATE72 + SHA1_STORE_STATE(72) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(d, e, a, b, c, W, 72, temp); + +#ifdef DOSTORESTATE73 + SHA1_STORE_STATE(73) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(c, d, e, a, b, W, 73, temp); + +#ifdef DOSTORESTATE74 + SHA1_STORE_STATE(74) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(b, c, d, e, a, W, 74, temp); + +#ifdef DOSTORESTATE75 + SHA1_STORE_STATE(75) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(a, b, c, d, e, W, 75, temp); + +#ifdef DOSTORESTATE76 + SHA1_STORE_STATE(76) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(e, a, b, c, d, W, 76, temp); + +#ifdef DOSTORESTATE77 + SHA1_STORE_STATE(77) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(d, e, a, b, c, W, 77, temp); + +#ifdef DOSTORESTATE78 + SHA1_STORE_STATE(78) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(c, d, e, a, b, W, 78, temp); + +#ifdef DOSTORESTATE79 + SHA1_STORE_STATE(79) +#endif + SHA1COMPRESS_FULL_ROUND4_STEP(b, c, d, e, a, W, 79, temp); + + + + ihv[0] += a; ihv[1] += b; ihv[2] += c; ihv[3] += d; ihv[4] += e; +} + + + + +#define SHA1_RECOMPRESS(t) \ +static void sha1recompress_fast_ ## t (uint32_t ihvin[5], uint32_t ihvout[5], const uint32_t me2[80], const uint32_t state[5]) \ +{ \ + uint32_t a = state[0], b = state[1], c = state[2], d = state[3], e = state[4]; \ + if (t > 79) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(b, c, d, e, a, me2, 79); \ + if (t > 78) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(c, d, e, a, b, me2, 78); \ + if (t > 77) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(d, e, a, b, c, me2, 77); \ + if (t > 76) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(e, a, b, c, d, me2, 76); \ + if (t > 75) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(a, b, c, d, e, me2, 75); \ + if (t > 74) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(b, c, d, e, a, me2, 74); \ + if (t > 73) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(c, d, e, a, b, me2, 73); \ + if (t > 72) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(d, e, a, b, c, me2, 72); \ + if (t > 71) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(e, a, b, c, d, me2, 71); \ + if (t > 70) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(a, b, c, d, e, me2, 70); \ + if (t > 69) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(b, c, d, e, a, me2, 69); \ + if (t > 68) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(c, d, e, a, b, me2, 68); \ + if (t > 67) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(d, e, a, b, c, me2, 67); \ + if (t > 66) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(e, a, b, c, d, me2, 66); \ + if (t > 65) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(a, b, c, d, e, me2, 65); \ + if (t > 64) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(b, c, d, e, a, me2, 64); \ + if (t > 63) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(c, d, e, a, b, me2, 63); \ + if (t > 62) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(d, e, a, b, c, me2, 62); \ + if (t > 61) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(e, a, b, c, d, me2, 61); \ + if (t > 60) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(a, b, c, d, e, me2, 60); \ + if (t > 59) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(b, c, d, e, a, me2, 59); \ + if (t > 58) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(c, d, e, a, b, me2, 58); \ + if (t > 57) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(d, e, a, b, c, me2, 57); \ + if (t > 56) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(e, a, b, c, d, me2, 56); \ + if (t > 55) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(a, b, c, d, e, me2, 55); \ + if (t > 54) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(b, c, d, e, a, me2, 54); \ + if (t > 53) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(c, d, e, a, b, me2, 53); \ + if (t > 52) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(d, e, a, b, c, me2, 52); \ + if (t > 51) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(e, a, b, c, d, me2, 51); \ + if (t > 50) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(a, b, c, d, e, me2, 50); \ + if (t > 49) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(b, c, d, e, a, me2, 49); \ + if (t > 48) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(c, d, e, a, b, me2, 48); \ + if (t > 47) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(d, e, a, b, c, me2, 47); \ + if (t > 46) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(e, a, b, c, d, me2, 46); \ + if (t > 45) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(a, b, c, d, e, me2, 45); \ + if (t > 44) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(b, c, d, e, a, me2, 44); \ + if (t > 43) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(c, d, e, a, b, me2, 43); \ + if (t > 42) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(d, e, a, b, c, me2, 42); \ + if (t > 41) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(e, a, b, c, d, me2, 41); \ + if (t > 40) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(a, b, c, d, e, me2, 40); \ + if (t > 39) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(b, c, d, e, a, me2, 39); \ + if (t > 38) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(c, d, e, a, b, me2, 38); \ + if (t > 37) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(d, e, a, b, c, me2, 37); \ + if (t > 36) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(e, a, b, c, d, me2, 36); \ + if (t > 35) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(a, b, c, d, e, me2, 35); \ + if (t > 34) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(b, c, d, e, a, me2, 34); \ + if (t > 33) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(c, d, e, a, b, me2, 33); \ + if (t > 32) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(d, e, a, b, c, me2, 32); \ + if (t > 31) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(e, a, b, c, d, me2, 31); \ + if (t > 30) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(a, b, c, d, e, me2, 30); \ + if (t > 29) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(b, c, d, e, a, me2, 29); \ + if (t > 28) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(c, d, e, a, b, me2, 28); \ + if (t > 27) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(d, e, a, b, c, me2, 27); \ + if (t > 26) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(e, a, b, c, d, me2, 26); \ + if (t > 25) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(a, b, c, d, e, me2, 25); \ + if (t > 24) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(b, c, d, e, a, me2, 24); \ + if (t > 23) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(c, d, e, a, b, me2, 23); \ + if (t > 22) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(d, e, a, b, c, me2, 22); \ + if (t > 21) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(e, a, b, c, d, me2, 21); \ + if (t > 20) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(a, b, c, d, e, me2, 20); \ + if (t > 19) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(b, c, d, e, a, me2, 19); \ + if (t > 18) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(c, d, e, a, b, me2, 18); \ + if (t > 17) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(d, e, a, b, c, me2, 17); \ + if (t > 16) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(e, a, b, c, d, me2, 16); \ + if (t > 15) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(a, b, c, d, e, me2, 15); \ + if (t > 14) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(b, c, d, e, a, me2, 14); \ + if (t > 13) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(c, d, e, a, b, me2, 13); \ + if (t > 12) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(d, e, a, b, c, me2, 12); \ + if (t > 11) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(e, a, b, c, d, me2, 11); \ + if (t > 10) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(a, b, c, d, e, me2, 10); \ + if (t > 9) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(b, c, d, e, a, me2, 9); \ + if (t > 8) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(c, d, e, a, b, me2, 8); \ + if (t > 7) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(d, e, a, b, c, me2, 7); \ + if (t > 6) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(e, a, b, c, d, me2, 6); \ + if (t > 5) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(a, b, c, d, e, me2, 5); \ + if (t > 4) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(b, c, d, e, a, me2, 4); \ + if (t > 3) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(c, d, e, a, b, me2, 3); \ + if (t > 2) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(d, e, a, b, c, me2, 2); \ + if (t > 1) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(e, a, b, c, d, me2, 1); \ + if (t > 0) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(a, b, c, d, e, me2, 0); \ + ihvin[0] = a; ihvin[1] = b; ihvin[2] = c; ihvin[3] = d; ihvin[4] = e; \ + a = state[0]; b = state[1]; c = state[2]; d = state[3]; e = state[4]; \ + if (t <= 0) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, me2, 0); \ + if (t <= 1) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, me2, 1); \ + if (t <= 2) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, me2, 2); \ + if (t <= 3) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, me2, 3); \ + if (t <= 4) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, me2, 4); \ + if (t <= 5) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, me2, 5); \ + if (t <= 6) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, me2, 6); \ + if (t <= 7) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, me2, 7); \ + if (t <= 8) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, me2, 8); \ + if (t <= 9) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, me2, 9); \ + if (t <= 10) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, me2, 10); \ + if (t <= 11) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, me2, 11); \ + if (t <= 12) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, me2, 12); \ + if (t <= 13) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, me2, 13); \ + if (t <= 14) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, me2, 14); \ + if (t <= 15) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, me2, 15); \ + if (t <= 16) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, me2, 16); \ + if (t <= 17) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, me2, 17); \ + if (t <= 18) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, me2, 18); \ + if (t <= 19) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, me2, 19); \ + if (t <= 20) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, me2, 20); \ + if (t <= 21) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, me2, 21); \ + if (t <= 22) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, me2, 22); \ + if (t <= 23) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, me2, 23); \ + if (t <= 24) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, me2, 24); \ + if (t <= 25) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, me2, 25); \ + if (t <= 26) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, me2, 26); \ + if (t <= 27) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, me2, 27); \ + if (t <= 28) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, me2, 28); \ + if (t <= 29) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, me2, 29); \ + if (t <= 30) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, me2, 30); \ + if (t <= 31) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, me2, 31); \ + if (t <= 32) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, me2, 32); \ + if (t <= 33) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, me2, 33); \ + if (t <= 34) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, me2, 34); \ + if (t <= 35) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, me2, 35); \ + if (t <= 36) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, me2, 36); \ + if (t <= 37) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, me2, 37); \ + if (t <= 38) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, me2, 38); \ + if (t <= 39) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, me2, 39); \ + if (t <= 40) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, me2, 40); \ + if (t <= 41) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, me2, 41); \ + if (t <= 42) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, me2, 42); \ + if (t <= 43) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, me2, 43); \ + if (t <= 44) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, me2, 44); \ + if (t <= 45) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, me2, 45); \ + if (t <= 46) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, me2, 46); \ + if (t <= 47) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, me2, 47); \ + if (t <= 48) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, me2, 48); \ + if (t <= 49) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, me2, 49); \ + if (t <= 50) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, me2, 50); \ + if (t <= 51) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, me2, 51); \ + if (t <= 52) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, me2, 52); \ + if (t <= 53) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, me2, 53); \ + if (t <= 54) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, me2, 54); \ + if (t <= 55) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, me2, 55); \ + if (t <= 56) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, me2, 56); \ + if (t <= 57) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, me2, 57); \ + if (t <= 58) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, me2, 58); \ + if (t <= 59) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, me2, 59); \ + if (t <= 60) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, me2, 60); \ + if (t <= 61) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, me2, 61); \ + if (t <= 62) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, me2, 62); \ + if (t <= 63) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, me2, 63); \ + if (t <= 64) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, me2, 64); \ + if (t <= 65) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, me2, 65); \ + if (t <= 66) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, me2, 66); \ + if (t <= 67) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, me2, 67); \ + if (t <= 68) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, me2, 68); \ + if (t <= 69) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, me2, 69); \ + if (t <= 70) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, me2, 70); \ + if (t <= 71) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, me2, 71); \ + if (t <= 72) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, me2, 72); \ + if (t <= 73) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, me2, 73); \ + if (t <= 74) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, me2, 74); \ + if (t <= 75) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, me2, 75); \ + if (t <= 76) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, me2, 76); \ + if (t <= 77) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, me2, 77); \ + if (t <= 78) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, me2, 78); \ + if (t <= 79) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, me2, 79); \ + ihvout[0] = ihvin[0] + a; ihvout[1] = ihvin[1] + b; ihvout[2] = ihvin[2] + c; ihvout[3] = ihvin[3] + d; ihvout[4] = ihvin[4] + e; \ +} + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable: 4127) /* Compiler complains about the checks in the above macro being constant. */ +#endif + +#ifdef DOSTORESTATE0 +SHA1_RECOMPRESS(0) +#endif + +#ifdef DOSTORESTATE1 +SHA1_RECOMPRESS(1) +#endif + +#ifdef DOSTORESTATE2 +SHA1_RECOMPRESS(2) +#endif + +#ifdef DOSTORESTATE3 +SHA1_RECOMPRESS(3) +#endif + +#ifdef DOSTORESTATE4 +SHA1_RECOMPRESS(4) +#endif + +#ifdef DOSTORESTATE5 +SHA1_RECOMPRESS(5) +#endif + +#ifdef DOSTORESTATE6 +SHA1_RECOMPRESS(6) +#endif + +#ifdef DOSTORESTATE7 +SHA1_RECOMPRESS(7) +#endif + +#ifdef DOSTORESTATE8 +SHA1_RECOMPRESS(8) +#endif + +#ifdef DOSTORESTATE9 +SHA1_RECOMPRESS(9) +#endif + +#ifdef DOSTORESTATE10 +SHA1_RECOMPRESS(10) +#endif + +#ifdef DOSTORESTATE11 +SHA1_RECOMPRESS(11) +#endif + +#ifdef DOSTORESTATE12 +SHA1_RECOMPRESS(12) +#endif + +#ifdef DOSTORESTATE13 +SHA1_RECOMPRESS(13) +#endif + +#ifdef DOSTORESTATE14 +SHA1_RECOMPRESS(14) +#endif + +#ifdef DOSTORESTATE15 +SHA1_RECOMPRESS(15) +#endif + +#ifdef DOSTORESTATE16 +SHA1_RECOMPRESS(16) +#endif + +#ifdef DOSTORESTATE17 +SHA1_RECOMPRESS(17) +#endif + +#ifdef DOSTORESTATE18 +SHA1_RECOMPRESS(18) +#endif + +#ifdef DOSTORESTATE19 +SHA1_RECOMPRESS(19) +#endif + +#ifdef DOSTORESTATE20 +SHA1_RECOMPRESS(20) +#endif + +#ifdef DOSTORESTATE21 +SHA1_RECOMPRESS(21) +#endif + +#ifdef DOSTORESTATE22 +SHA1_RECOMPRESS(22) +#endif + +#ifdef DOSTORESTATE23 +SHA1_RECOMPRESS(23) +#endif + +#ifdef DOSTORESTATE24 +SHA1_RECOMPRESS(24) +#endif + +#ifdef DOSTORESTATE25 +SHA1_RECOMPRESS(25) +#endif + +#ifdef DOSTORESTATE26 +SHA1_RECOMPRESS(26) +#endif + +#ifdef DOSTORESTATE27 +SHA1_RECOMPRESS(27) +#endif + +#ifdef DOSTORESTATE28 +SHA1_RECOMPRESS(28) +#endif + +#ifdef DOSTORESTATE29 +SHA1_RECOMPRESS(29) +#endif + +#ifdef DOSTORESTATE30 +SHA1_RECOMPRESS(30) +#endif + +#ifdef DOSTORESTATE31 +SHA1_RECOMPRESS(31) +#endif + +#ifdef DOSTORESTATE32 +SHA1_RECOMPRESS(32) +#endif + +#ifdef DOSTORESTATE33 +SHA1_RECOMPRESS(33) +#endif + +#ifdef DOSTORESTATE34 +SHA1_RECOMPRESS(34) +#endif + +#ifdef DOSTORESTATE35 +SHA1_RECOMPRESS(35) +#endif + +#ifdef DOSTORESTATE36 +SHA1_RECOMPRESS(36) +#endif + +#ifdef DOSTORESTATE37 +SHA1_RECOMPRESS(37) +#endif + +#ifdef DOSTORESTATE38 +SHA1_RECOMPRESS(38) +#endif + +#ifdef DOSTORESTATE39 +SHA1_RECOMPRESS(39) +#endif + +#ifdef DOSTORESTATE40 +SHA1_RECOMPRESS(40) +#endif + +#ifdef DOSTORESTATE41 +SHA1_RECOMPRESS(41) +#endif + +#ifdef DOSTORESTATE42 +SHA1_RECOMPRESS(42) +#endif + +#ifdef DOSTORESTATE43 +SHA1_RECOMPRESS(43) +#endif + +#ifdef DOSTORESTATE44 +SHA1_RECOMPRESS(44) +#endif + +#ifdef DOSTORESTATE45 +SHA1_RECOMPRESS(45) +#endif + +#ifdef DOSTORESTATE46 +SHA1_RECOMPRESS(46) +#endif + +#ifdef DOSTORESTATE47 +SHA1_RECOMPRESS(47) +#endif + +#ifdef DOSTORESTATE48 +SHA1_RECOMPRESS(48) +#endif + +#ifdef DOSTORESTATE49 +SHA1_RECOMPRESS(49) +#endif + +#ifdef DOSTORESTATE50 +SHA1_RECOMPRESS(50) +#endif + +#ifdef DOSTORESTATE51 +SHA1_RECOMPRESS(51) +#endif + +#ifdef DOSTORESTATE52 +SHA1_RECOMPRESS(52) +#endif + +#ifdef DOSTORESTATE53 +SHA1_RECOMPRESS(53) +#endif + +#ifdef DOSTORESTATE54 +SHA1_RECOMPRESS(54) +#endif + +#ifdef DOSTORESTATE55 +SHA1_RECOMPRESS(55) +#endif + +#ifdef DOSTORESTATE56 +SHA1_RECOMPRESS(56) +#endif + +#ifdef DOSTORESTATE57 +SHA1_RECOMPRESS(57) +#endif + +#ifdef DOSTORESTATE58 +SHA1_RECOMPRESS(58) +#endif + +#ifdef DOSTORESTATE59 +SHA1_RECOMPRESS(59) +#endif + +#ifdef DOSTORESTATE60 +SHA1_RECOMPRESS(60) +#endif + +#ifdef DOSTORESTATE61 +SHA1_RECOMPRESS(61) +#endif + +#ifdef DOSTORESTATE62 +SHA1_RECOMPRESS(62) +#endif + +#ifdef DOSTORESTATE63 +SHA1_RECOMPRESS(63) +#endif + +#ifdef DOSTORESTATE64 +SHA1_RECOMPRESS(64) +#endif + +#ifdef DOSTORESTATE65 +SHA1_RECOMPRESS(65) +#endif + +#ifdef DOSTORESTATE66 +SHA1_RECOMPRESS(66) +#endif + +#ifdef DOSTORESTATE67 +SHA1_RECOMPRESS(67) +#endif + +#ifdef DOSTORESTATE68 +SHA1_RECOMPRESS(68) +#endif + +#ifdef DOSTORESTATE69 +SHA1_RECOMPRESS(69) +#endif + +#ifdef DOSTORESTATE70 +SHA1_RECOMPRESS(70) +#endif + +#ifdef DOSTORESTATE71 +SHA1_RECOMPRESS(71) +#endif + +#ifdef DOSTORESTATE72 +SHA1_RECOMPRESS(72) +#endif + +#ifdef DOSTORESTATE73 +SHA1_RECOMPRESS(73) +#endif + +#ifdef DOSTORESTATE74 +SHA1_RECOMPRESS(74) +#endif + +#ifdef DOSTORESTATE75 +SHA1_RECOMPRESS(75) +#endif + +#ifdef DOSTORESTATE76 +SHA1_RECOMPRESS(76) +#endif + +#ifdef DOSTORESTATE77 +SHA1_RECOMPRESS(77) +#endif + +#ifdef DOSTORESTATE78 +SHA1_RECOMPRESS(78) +#endif + +#ifdef DOSTORESTATE79 +SHA1_RECOMPRESS(79) +#endif + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +static void sha1_recompression_step(uint32_t step, uint32_t ihvin[5], uint32_t ihvout[5], const uint32_t me2[80], const uint32_t state[5]) +{ + switch (step) + { +#ifdef DOSTORESTATE0 + case 0: + sha1recompress_fast_0(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE1 + case 1: + sha1recompress_fast_1(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE2 + case 2: + sha1recompress_fast_2(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE3 + case 3: + sha1recompress_fast_3(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE4 + case 4: + sha1recompress_fast_4(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE5 + case 5: + sha1recompress_fast_5(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE6 + case 6: + sha1recompress_fast_6(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE7 + case 7: + sha1recompress_fast_7(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE8 + case 8: + sha1recompress_fast_8(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE9 + case 9: + sha1recompress_fast_9(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE10 + case 10: + sha1recompress_fast_10(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE11 + case 11: + sha1recompress_fast_11(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE12 + case 12: + sha1recompress_fast_12(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE13 + case 13: + sha1recompress_fast_13(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE14 + case 14: + sha1recompress_fast_14(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE15 + case 15: + sha1recompress_fast_15(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE16 + case 16: + sha1recompress_fast_16(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE17 + case 17: + sha1recompress_fast_17(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE18 + case 18: + sha1recompress_fast_18(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE19 + case 19: + sha1recompress_fast_19(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE20 + case 20: + sha1recompress_fast_20(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE21 + case 21: + sha1recompress_fast_21(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE22 + case 22: + sha1recompress_fast_22(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE23 + case 23: + sha1recompress_fast_23(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE24 + case 24: + sha1recompress_fast_24(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE25 + case 25: + sha1recompress_fast_25(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE26 + case 26: + sha1recompress_fast_26(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE27 + case 27: + sha1recompress_fast_27(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE28 + case 28: + sha1recompress_fast_28(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE29 + case 29: + sha1recompress_fast_29(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE30 + case 30: + sha1recompress_fast_30(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE31 + case 31: + sha1recompress_fast_31(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE32 + case 32: + sha1recompress_fast_32(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE33 + case 33: + sha1recompress_fast_33(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE34 + case 34: + sha1recompress_fast_34(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE35 + case 35: + sha1recompress_fast_35(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE36 + case 36: + sha1recompress_fast_36(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE37 + case 37: + sha1recompress_fast_37(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE38 + case 38: + sha1recompress_fast_38(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE39 + case 39: + sha1recompress_fast_39(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE40 + case 40: + sha1recompress_fast_40(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE41 + case 41: + sha1recompress_fast_41(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE42 + case 42: + sha1recompress_fast_42(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE43 + case 43: + sha1recompress_fast_43(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE44 + case 44: + sha1recompress_fast_44(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE45 + case 45: + sha1recompress_fast_45(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE46 + case 46: + sha1recompress_fast_46(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE47 + case 47: + sha1recompress_fast_47(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE48 + case 48: + sha1recompress_fast_48(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE49 + case 49: + sha1recompress_fast_49(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE50 + case 50: + sha1recompress_fast_50(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE51 + case 51: + sha1recompress_fast_51(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE52 + case 52: + sha1recompress_fast_52(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE53 + case 53: + sha1recompress_fast_53(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE54 + case 54: + sha1recompress_fast_54(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE55 + case 55: + sha1recompress_fast_55(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE56 + case 56: + sha1recompress_fast_56(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE57 + case 57: + sha1recompress_fast_57(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE58 + case 58: + sha1recompress_fast_58(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE59 + case 59: + sha1recompress_fast_59(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE60 + case 60: + sha1recompress_fast_60(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE61 + case 61: + sha1recompress_fast_61(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE62 + case 62: + sha1recompress_fast_62(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE63 + case 63: + sha1recompress_fast_63(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE64 + case 64: + sha1recompress_fast_64(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE65 + case 65: + sha1recompress_fast_65(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE66 + case 66: + sha1recompress_fast_66(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE67 + case 67: + sha1recompress_fast_67(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE68 + case 68: + sha1recompress_fast_68(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE69 + case 69: + sha1recompress_fast_69(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE70 + case 70: + sha1recompress_fast_70(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE71 + case 71: + sha1recompress_fast_71(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE72 + case 72: + sha1recompress_fast_72(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE73 + case 73: + sha1recompress_fast_73(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE74 + case 74: + sha1recompress_fast_74(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE75 + case 75: + sha1recompress_fast_75(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE76 + case 76: + sha1recompress_fast_76(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE77 + case 77: + sha1recompress_fast_77(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE78 + case 78: + sha1recompress_fast_78(ihvin, ihvout, me2, state); + break; +#endif +#ifdef DOSTORESTATE79 + case 79: + sha1recompress_fast_79(ihvin, ihvout, me2, state); + break; +#endif + default: + abort(); + } + +} + + + +static void sha1_process(SHA1_CTX *ctx, const uint32_t block[16]) +{ + unsigned i, j; + uint32_t ubc_dv_mask[DVMASKSIZE] = { 0xFFFFFFFF }; + uint32_t ihvtmp[5]; + + ctx->ihv1[0] = ctx->ihv[0]; + ctx->ihv1[1] = ctx->ihv[1]; + ctx->ihv1[2] = ctx->ihv[2]; + ctx->ihv1[3] = ctx->ihv[3]; + ctx->ihv1[4] = ctx->ihv[4]; + + sha1_compression_states(ctx->ihv, block, ctx->m1, ctx->states); + + if (ctx->detect_coll) + { + if (ctx->ubc_check) + { + ubc_check(ctx->m1, ubc_dv_mask); + } + + if (ubc_dv_mask[0] != 0) + { + for (i = 0; sha1_dvs[i].dvType != 0; ++i) + { + if (ubc_dv_mask[0] & ((uint32_t)(1) << sha1_dvs[i].maskb)) + { + for (j = 0; j < 80; ++j) + ctx->m2[j] = ctx->m1[j] ^ sha1_dvs[i].dm[j]; + + sha1_recompression_step(sha1_dvs[i].testt, ctx->ihv2, ihvtmp, ctx->m2, ctx->states[sha1_dvs[i].testt]); + + /* to verify SHA-1 collision detection code with collisions for reduced-step SHA-1 */ + if ((0 == ((ihvtmp[0] ^ ctx->ihv[0]) | (ihvtmp[1] ^ ctx->ihv[1]) | (ihvtmp[2] ^ ctx->ihv[2]) | (ihvtmp[3] ^ ctx->ihv[3]) | (ihvtmp[4] ^ ctx->ihv[4]))) + || (ctx->reduced_round_coll && 0==((ctx->ihv1[0] ^ ctx->ihv2[0]) | (ctx->ihv1[1] ^ ctx->ihv2[1]) | (ctx->ihv1[2] ^ ctx->ihv2[2]) | (ctx->ihv1[3] ^ ctx->ihv2[3]) | (ctx->ihv1[4] ^ ctx->ihv2[4])))) + { + ctx->found_collision = 1; + + if (ctx->safe_hash) + { + sha1_compression_W(ctx->ihv, ctx->m1); + sha1_compression_W(ctx->ihv, ctx->m1); + } + + break; + } + } + } + } + } +} + +void SHA1DCInit(SHA1_CTX *ctx) +{ + ctx->total = 0; + ctx->ihv[0] = 0x67452301; + ctx->ihv[1] = 0xEFCDAB89; + ctx->ihv[2] = 0x98BADCFE; + ctx->ihv[3] = 0x10325476; + ctx->ihv[4] = 0xC3D2E1F0; + ctx->found_collision = 0; + ctx->safe_hash = SHA1DC_INIT_SAFE_HASH_DEFAULT; + ctx->ubc_check = 1; + ctx->detect_coll = 1; + ctx->reduced_round_coll = 0; + ctx->callback = NULL; +} + +void SHA1DCSetSafeHash(SHA1_CTX *ctx, int safehash) +{ + if (safehash) + ctx->safe_hash = 1; + else + ctx->safe_hash = 0; +} + + +void SHA1DCSetUseUBC(SHA1_CTX *ctx, int ubc_check) +{ + if (ubc_check) + ctx->ubc_check = 1; + else + ctx->ubc_check = 0; +} + +void SHA1DCSetUseDetectColl(SHA1_CTX *ctx, int detect_coll) +{ + if (detect_coll) + ctx->detect_coll = 1; + else + ctx->detect_coll = 0; +} + +void SHA1DCSetDetectReducedRoundCollision(SHA1_CTX *ctx, int reduced_round_coll) +{ + if (reduced_round_coll) + ctx->reduced_round_coll = 1; + else + ctx->reduced_round_coll = 0; +} + +void SHA1DCSetCallback(SHA1_CTX *ctx, collision_block_callback callback) +{ + ctx->callback = callback; +} + +void SHA1DCUpdate(SHA1_CTX *ctx, const char *buf, size_t len) +{ + unsigned left, fill; + + if (len == 0) + return; + + left = ctx->total & 63; + fill = 64 - left; + + if (left && len >= fill) + { + ctx->total += fill; + memcpy(ctx->buffer + left, buf, fill); + sha1_process(ctx, (uint32_t*)(ctx->buffer)); + buf += fill; + len -= fill; + left = 0; + } + while (len >= 64) + { + ctx->total += 64; + +#if defined(SHA1DC_ALLOW_UNALIGNED_ACCESS) + sha1_process(ctx, (uint32_t*)(buf)); +#else + memcpy(ctx->buffer, buf, 64); + sha1_process(ctx, (uint32_t*)(ctx->buffer)); +#endif /* defined(SHA1DC_ALLOW_UNALIGNED_ACCESS) */ + buf += 64; + len -= 64; + } + if (len > 0) + { + ctx->total += len; + memcpy(ctx->buffer + left, buf, len); + } +} + +static const unsigned char sha1_padding[64] = +{ + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +int SHA1DCFinal(unsigned char output[20], SHA1_CTX *ctx) +{ + uint32_t last = ctx->total & 63; + uint32_t padn = (last < 56) ? (56 - last) : (120 - last); + uint64_t total; + SHA1DCUpdate(ctx, (const char*)(sha1_padding), padn); + + total = ctx->total - padn; + total <<= 3; + ctx->buffer[56] = (unsigned char)(total >> 56); + ctx->buffer[57] = (unsigned char)(total >> 48); + ctx->buffer[58] = (unsigned char)(total >> 40); + ctx->buffer[59] = (unsigned char)(total >> 32); + ctx->buffer[60] = (unsigned char)(total >> 24); + ctx->buffer[61] = (unsigned char)(total >> 16); + ctx->buffer[62] = (unsigned char)(total >> 8); + ctx->buffer[63] = (unsigned char)(total); + sha1_process(ctx, (uint32_t*)(ctx->buffer)); + output[0] = (unsigned char)(ctx->ihv[0] >> 24); + output[1] = (unsigned char)(ctx->ihv[0] >> 16); + output[2] = (unsigned char)(ctx->ihv[0] >> 8); + output[3] = (unsigned char)(ctx->ihv[0]); + output[4] = (unsigned char)(ctx->ihv[1] >> 24); + output[5] = (unsigned char)(ctx->ihv[1] >> 16); + output[6] = (unsigned char)(ctx->ihv[1] >> 8); + output[7] = (unsigned char)(ctx->ihv[1]); + output[8] = (unsigned char)(ctx->ihv[2] >> 24); + output[9] = (unsigned char)(ctx->ihv[2] >> 16); + output[10] = (unsigned char)(ctx->ihv[2] >> 8); + output[11] = (unsigned char)(ctx->ihv[2]); + output[12] = (unsigned char)(ctx->ihv[3] >> 24); + output[13] = (unsigned char)(ctx->ihv[3] >> 16); + output[14] = (unsigned char)(ctx->ihv[3] >> 8); + output[15] = (unsigned char)(ctx->ihv[3]); + output[16] = (unsigned char)(ctx->ihv[4] >> 24); + output[17] = (unsigned char)(ctx->ihv[4] >> 16); + output[18] = (unsigned char)(ctx->ihv[4] >> 8); + output[19] = (unsigned char)(ctx->ihv[4]); + return ctx->found_collision; +} + +#ifdef SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_C +#include SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_C +#endif diff --git a/src/util/hash/sha1dc/sha1.h b/src/util/hash/sha1dc/sha1.h new file mode 100644 index 00000000000..1e4e94be54a --- /dev/null +++ b/src/util/hash/sha1dc/sha1.h @@ -0,0 +1,110 @@ +/*** +* Copyright 2017 Marc Stevens , Dan Shumow +* Distributed under the MIT Software License. +* See accompanying file LICENSE.txt or copy at +* https://opensource.org/licenses/MIT +***/ + +#ifndef SHA1DC_SHA1_H +#define SHA1DC_SHA1_H + +#if defined(__cplusplus) +extern "C" { +#endif + +#ifndef SHA1DC_NO_STANDARD_INCLUDES +#include +#endif + +/* sha-1 compression function that takes an already expanded message, and additionally store intermediate states */ +/* only stores states ii (the state between step ii-1 and step ii) when DOSTORESTATEii is defined in ubc_check.h */ +void sha1_compression_states(uint32_t[5], const uint32_t[16], uint32_t[80], uint32_t[80][5]); + +/* +// Function type for sha1_recompression_step_T (uint32_t ihvin[5], uint32_t ihvout[5], const uint32_t me2[80], const uint32_t state[5]). +// Where 0 <= T < 80 +// me2 is an expanded message (the expansion of an original message block XOR'ed with a disturbance vector's message block difference.) +// state is the internal state (a,b,c,d,e) before step T of the SHA-1 compression function while processing the original message block. +// The function will return: +// ihvin: The reconstructed input chaining value. +// ihvout: The reconstructed output chaining value. +*/ +typedef void(*sha1_recompression_type)(uint32_t*, uint32_t*, const uint32_t*, const uint32_t*); + +/* A callback function type that can be set to be called when a collision block has been found: */ +/* void collision_block_callback(uint64_t byteoffset, const uint32_t ihvin1[5], const uint32_t ihvin2[5], const uint32_t m1[80], const uint32_t m2[80]) */ +typedef void(*collision_block_callback)(uint64_t, const uint32_t*, const uint32_t*, const uint32_t*, const uint32_t*); + +/* The SHA-1 context. */ +typedef struct { + uint64_t total; + uint32_t ihv[5]; + unsigned char buffer[64]; + int found_collision; + int safe_hash; + int detect_coll; + int ubc_check; + int reduced_round_coll; + collision_block_callback callback; + + uint32_t ihv1[5]; + uint32_t ihv2[5]; + uint32_t m1[80]; + uint32_t m2[80]; + uint32_t states[80][5]; +} SHA1_CTX; + +/* Initialize SHA-1 context. */ +void SHA1DCInit(SHA1_CTX*); + +/* + Function to enable safe SHA-1 hashing: + Collision attacks are thwarted by hashing a detected near-collision block 3 times. + Think of it as extending SHA-1 from 80-steps to 240-steps for such blocks: + The best collision attacks against SHA-1 have complexity about 2^60, + thus for 240-steps an immediate lower-bound for the best cryptanalytic attacks would be 2^180. + An attacker would be better off using a generic birthday search of complexity 2^80. + + Enabling safe SHA-1 hashing will result in the correct SHA-1 hash for messages where no collision attack was detected, + but it will result in a different SHA-1 hash for messages where a collision attack was detected. + This will automatically invalidate SHA-1 based digital signature forgeries. + Enabled by default. +*/ +void SHA1DCSetSafeHash(SHA1_CTX*, int); + +/* + Function to disable or enable the use of Unavoidable Bitconditions (provides a significant speed up). + Enabled by default + */ +void SHA1DCSetUseUBC(SHA1_CTX*, int); + +/* + Function to disable or enable the use of Collision Detection. + Enabled by default. + */ +void SHA1DCSetUseDetectColl(SHA1_CTX*, int); + +/* function to disable or enable the detection of reduced-round SHA-1 collisions */ +/* disabled by default */ +void SHA1DCSetDetectReducedRoundCollision(SHA1_CTX*, int); + +/* function to set a callback function, pass NULL to disable */ +/* by default no callback set */ +void SHA1DCSetCallback(SHA1_CTX*, collision_block_callback); + +/* update SHA-1 context with buffer contents */ +void SHA1DCUpdate(SHA1_CTX*, const char*, size_t); + +/* obtain SHA-1 hash from SHA-1 context */ +/* returns: 0 = no collision detected, otherwise = collision found => warn user for active attack */ +int SHA1DCFinal(unsigned char[20], SHA1_CTX*); + +#if defined(__cplusplus) +} +#endif + +#ifdef SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_H +#include SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_H +#endif + +#endif diff --git a/src/util/hash/sha1dc/ubc_check.c b/src/util/hash/sha1dc/ubc_check.c new file mode 100644 index 00000000000..b3beff2afba --- /dev/null +++ b/src/util/hash/sha1dc/ubc_check.c @@ -0,0 +1,372 @@ +/*** +* Copyright 2017 Marc Stevens , Dan Shumow +* Distributed under the MIT Software License. +* See accompanying file LICENSE.txt or copy at +* https://opensource.org/licenses/MIT +***/ + +/* +// this file was generated by the 'parse_bitrel' program in the tools section +// using the data files from directory 'tools/data/3565' +// +// sha1_dvs contains a list of SHA-1 Disturbance Vectors (DV) to check +// dvType, dvK and dvB define the DV: I(K,B) or II(K,B) (see the paper) +// dm[80] is the expanded message block XOR-difference defined by the DV +// testt is the step to do the recompression from for collision detection +// maski and maskb define the bit to check for each DV in the dvmask returned by ubc_check +// +// ubc_check takes as input an expanded message block and verifies the unavoidable bitconditions for all listed DVs +// it returns a dvmask where each bit belonging to a DV is set if all unavoidable bitconditions for that DV have been met +// thus one needs to do the recompression check for each DV that has its bit set +// +// ubc_check is programmatically generated and the unavoidable bitconditions have been hardcoded +// a directly verifiable version named ubc_check_verify can be found in ubc_check_verify.c +// ubc_check has been verified against ubc_check_verify using the 'ubc_check_test' program in the tools section +*/ + +#ifndef SHA1DC_NO_STANDARD_INCLUDES +#include +#endif +#ifdef SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C +#include SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C +#endif +#include "ubc_check.h" + +static const uint32_t DV_I_43_0_bit = (uint32_t)(1) << 0; +static const uint32_t DV_I_44_0_bit = (uint32_t)(1) << 1; +static const uint32_t DV_I_45_0_bit = (uint32_t)(1) << 2; +static const uint32_t DV_I_46_0_bit = (uint32_t)(1) << 3; +static const uint32_t DV_I_46_2_bit = (uint32_t)(1) << 4; +static const uint32_t DV_I_47_0_bit = (uint32_t)(1) << 5; +static const uint32_t DV_I_47_2_bit = (uint32_t)(1) << 6; +static const uint32_t DV_I_48_0_bit = (uint32_t)(1) << 7; +static const uint32_t DV_I_48_2_bit = (uint32_t)(1) << 8; +static const uint32_t DV_I_49_0_bit = (uint32_t)(1) << 9; +static const uint32_t DV_I_49_2_bit = (uint32_t)(1) << 10; +static const uint32_t DV_I_50_0_bit = (uint32_t)(1) << 11; +static const uint32_t DV_I_50_2_bit = (uint32_t)(1) << 12; +static const uint32_t DV_I_51_0_bit = (uint32_t)(1) << 13; +static const uint32_t DV_I_51_2_bit = (uint32_t)(1) << 14; +static const uint32_t DV_I_52_0_bit = (uint32_t)(1) << 15; +static const uint32_t DV_II_45_0_bit = (uint32_t)(1) << 16; +static const uint32_t DV_II_46_0_bit = (uint32_t)(1) << 17; +static const uint32_t DV_II_46_2_bit = (uint32_t)(1) << 18; +static const uint32_t DV_II_47_0_bit = (uint32_t)(1) << 19; +static const uint32_t DV_II_48_0_bit = (uint32_t)(1) << 20; +static const uint32_t DV_II_49_0_bit = (uint32_t)(1) << 21; +static const uint32_t DV_II_49_2_bit = (uint32_t)(1) << 22; +static const uint32_t DV_II_50_0_bit = (uint32_t)(1) << 23; +static const uint32_t DV_II_50_2_bit = (uint32_t)(1) << 24; +static const uint32_t DV_II_51_0_bit = (uint32_t)(1) << 25; +static const uint32_t DV_II_51_2_bit = (uint32_t)(1) << 26; +static const uint32_t DV_II_52_0_bit = (uint32_t)(1) << 27; +static const uint32_t DV_II_53_0_bit = (uint32_t)(1) << 28; +static const uint32_t DV_II_54_0_bit = (uint32_t)(1) << 29; +static const uint32_t DV_II_55_0_bit = (uint32_t)(1) << 30; +static const uint32_t DV_II_56_0_bit = (uint32_t)(1) << 31; + +dv_info_t sha1_dvs[] = +{ + {1,43,0,58,0,0, { 0x08000000,0x9800000c,0xd8000010,0x08000010,0xb8000010,0x98000000,0x60000000,0x00000008,0xc0000000,0x90000014,0x10000010,0xb8000014,0x28000000,0x20000010,0x48000000,0x08000018,0x60000000,0x90000010,0xf0000010,0x90000008,0xc0000000,0x90000010,0xf0000010,0xb0000008,0x40000000,0x90000000,0xf0000010,0x90000018,0x60000000,0x90000010,0x90000010,0x90000000,0x80000000,0x00000010,0xa0000000,0x20000000,0xa0000000,0x20000010,0x00000000,0x20000010,0x20000000,0x00000010,0x20000000,0x00000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000040,0x40000002,0x80000004,0x80000080,0x80000006,0x00000049,0x00000103,0x80000009,0x80000012,0x80000202,0x00000018,0x00000164,0x00000408,0x800000e6,0x8000004c,0x00000803,0x80000161,0x80000599 } } +, {1,44,0,58,0,1, { 0xb4000008,0x08000000,0x9800000c,0xd8000010,0x08000010,0xb8000010,0x98000000,0x60000000,0x00000008,0xc0000000,0x90000014,0x10000010,0xb8000014,0x28000000,0x20000010,0x48000000,0x08000018,0x60000000,0x90000010,0xf0000010,0x90000008,0xc0000000,0x90000010,0xf0000010,0xb0000008,0x40000000,0x90000000,0xf0000010,0x90000018,0x60000000,0x90000010,0x90000010,0x90000000,0x80000000,0x00000010,0xa0000000,0x20000000,0xa0000000,0x20000010,0x00000000,0x20000010,0x20000000,0x00000010,0x20000000,0x00000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000040,0x40000002,0x80000004,0x80000080,0x80000006,0x00000049,0x00000103,0x80000009,0x80000012,0x80000202,0x00000018,0x00000164,0x00000408,0x800000e6,0x8000004c,0x00000803,0x80000161 } } +, {1,45,0,58,0,2, { 0xf4000014,0xb4000008,0x08000000,0x9800000c,0xd8000010,0x08000010,0xb8000010,0x98000000,0x60000000,0x00000008,0xc0000000,0x90000014,0x10000010,0xb8000014,0x28000000,0x20000010,0x48000000,0x08000018,0x60000000,0x90000010,0xf0000010,0x90000008,0xc0000000,0x90000010,0xf0000010,0xb0000008,0x40000000,0x90000000,0xf0000010,0x90000018,0x60000000,0x90000010,0x90000010,0x90000000,0x80000000,0x00000010,0xa0000000,0x20000000,0xa0000000,0x20000010,0x00000000,0x20000010,0x20000000,0x00000010,0x20000000,0x00000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000040,0x40000002,0x80000004,0x80000080,0x80000006,0x00000049,0x00000103,0x80000009,0x80000012,0x80000202,0x00000018,0x00000164,0x00000408,0x800000e6,0x8000004c,0x00000803 } } +, {1,46,0,58,0,3, { 0x2c000010,0xf4000014,0xb4000008,0x08000000,0x9800000c,0xd8000010,0x08000010,0xb8000010,0x98000000,0x60000000,0x00000008,0xc0000000,0x90000014,0x10000010,0xb8000014,0x28000000,0x20000010,0x48000000,0x08000018,0x60000000,0x90000010,0xf0000010,0x90000008,0xc0000000,0x90000010,0xf0000010,0xb0000008,0x40000000,0x90000000,0xf0000010,0x90000018,0x60000000,0x90000010,0x90000010,0x90000000,0x80000000,0x00000010,0xa0000000,0x20000000,0xa0000000,0x20000010,0x00000000,0x20000010,0x20000000,0x00000010,0x20000000,0x00000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000040,0x40000002,0x80000004,0x80000080,0x80000006,0x00000049,0x00000103,0x80000009,0x80000012,0x80000202,0x00000018,0x00000164,0x00000408,0x800000e6,0x8000004c } } +, {1,46,2,58,0,4, { 0xb0000040,0xd0000053,0xd0000022,0x20000000,0x60000032,0x60000043,0x20000040,0xe0000042,0x60000002,0x80000001,0x00000020,0x00000003,0x40000052,0x40000040,0xe0000052,0xa0000000,0x80000040,0x20000001,0x20000060,0x80000001,0x40000042,0xc0000043,0x40000022,0x00000003,0x40000042,0xc0000043,0xc0000022,0x00000001,0x40000002,0xc0000043,0x40000062,0x80000001,0x40000042,0x40000042,0x40000002,0x00000002,0x00000040,0x80000002,0x80000000,0x80000002,0x80000040,0x00000000,0x80000040,0x80000000,0x00000040,0x80000000,0x00000040,0x80000002,0x00000000,0x80000000,0x80000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000004,0x00000080,0x00000004,0x00000009,0x00000101,0x00000009,0x00000012,0x00000202,0x0000001a,0x00000124,0x0000040c,0x00000026,0x0000004a,0x0000080a,0x00000060,0x00000590,0x00001020,0x0000039a,0x00000132 } } +, {1,47,0,58,0,5, { 0xc8000010,0x2c000010,0xf4000014,0xb4000008,0x08000000,0x9800000c,0xd8000010,0x08000010,0xb8000010,0x98000000,0x60000000,0x00000008,0xc0000000,0x90000014,0x10000010,0xb8000014,0x28000000,0x20000010,0x48000000,0x08000018,0x60000000,0x90000010,0xf0000010,0x90000008,0xc0000000,0x90000010,0xf0000010,0xb0000008,0x40000000,0x90000000,0xf0000010,0x90000018,0x60000000,0x90000010,0x90000010,0x90000000,0x80000000,0x00000010,0xa0000000,0x20000000,0xa0000000,0x20000010,0x00000000,0x20000010,0x20000000,0x00000010,0x20000000,0x00000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000040,0x40000002,0x80000004,0x80000080,0x80000006,0x00000049,0x00000103,0x80000009,0x80000012,0x80000202,0x00000018,0x00000164,0x00000408,0x800000e6 } } +, {1,47,2,58,0,6, { 0x20000043,0xb0000040,0xd0000053,0xd0000022,0x20000000,0x60000032,0x60000043,0x20000040,0xe0000042,0x60000002,0x80000001,0x00000020,0x00000003,0x40000052,0x40000040,0xe0000052,0xa0000000,0x80000040,0x20000001,0x20000060,0x80000001,0x40000042,0xc0000043,0x40000022,0x00000003,0x40000042,0xc0000043,0xc0000022,0x00000001,0x40000002,0xc0000043,0x40000062,0x80000001,0x40000042,0x40000042,0x40000002,0x00000002,0x00000040,0x80000002,0x80000000,0x80000002,0x80000040,0x00000000,0x80000040,0x80000000,0x00000040,0x80000000,0x00000040,0x80000002,0x00000000,0x80000000,0x80000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000004,0x00000080,0x00000004,0x00000009,0x00000101,0x00000009,0x00000012,0x00000202,0x0000001a,0x00000124,0x0000040c,0x00000026,0x0000004a,0x0000080a,0x00000060,0x00000590,0x00001020,0x0000039a } } +, {1,48,0,58,0,7, { 0xb800000a,0xc8000010,0x2c000010,0xf4000014,0xb4000008,0x08000000,0x9800000c,0xd8000010,0x08000010,0xb8000010,0x98000000,0x60000000,0x00000008,0xc0000000,0x90000014,0x10000010,0xb8000014,0x28000000,0x20000010,0x48000000,0x08000018,0x60000000,0x90000010,0xf0000010,0x90000008,0xc0000000,0x90000010,0xf0000010,0xb0000008,0x40000000,0x90000000,0xf0000010,0x90000018,0x60000000,0x90000010,0x90000010,0x90000000,0x80000000,0x00000010,0xa0000000,0x20000000,0xa0000000,0x20000010,0x00000000,0x20000010,0x20000000,0x00000010,0x20000000,0x00000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000040,0x40000002,0x80000004,0x80000080,0x80000006,0x00000049,0x00000103,0x80000009,0x80000012,0x80000202,0x00000018,0x00000164,0x00000408 } } +, {1,48,2,58,0,8, { 0xe000002a,0x20000043,0xb0000040,0xd0000053,0xd0000022,0x20000000,0x60000032,0x60000043,0x20000040,0xe0000042,0x60000002,0x80000001,0x00000020,0x00000003,0x40000052,0x40000040,0xe0000052,0xa0000000,0x80000040,0x20000001,0x20000060,0x80000001,0x40000042,0xc0000043,0x40000022,0x00000003,0x40000042,0xc0000043,0xc0000022,0x00000001,0x40000002,0xc0000043,0x40000062,0x80000001,0x40000042,0x40000042,0x40000002,0x00000002,0x00000040,0x80000002,0x80000000,0x80000002,0x80000040,0x00000000,0x80000040,0x80000000,0x00000040,0x80000000,0x00000040,0x80000002,0x00000000,0x80000000,0x80000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000004,0x00000080,0x00000004,0x00000009,0x00000101,0x00000009,0x00000012,0x00000202,0x0000001a,0x00000124,0x0000040c,0x00000026,0x0000004a,0x0000080a,0x00000060,0x00000590,0x00001020 } } +, {1,49,0,58,0,9, { 0x18000000,0xb800000a,0xc8000010,0x2c000010,0xf4000014,0xb4000008,0x08000000,0x9800000c,0xd8000010,0x08000010,0xb8000010,0x98000000,0x60000000,0x00000008,0xc0000000,0x90000014,0x10000010,0xb8000014,0x28000000,0x20000010,0x48000000,0x08000018,0x60000000,0x90000010,0xf0000010,0x90000008,0xc0000000,0x90000010,0xf0000010,0xb0000008,0x40000000,0x90000000,0xf0000010,0x90000018,0x60000000,0x90000010,0x90000010,0x90000000,0x80000000,0x00000010,0xa0000000,0x20000000,0xa0000000,0x20000010,0x00000000,0x20000010,0x20000000,0x00000010,0x20000000,0x00000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000040,0x40000002,0x80000004,0x80000080,0x80000006,0x00000049,0x00000103,0x80000009,0x80000012,0x80000202,0x00000018,0x00000164 } } +, {1,49,2,58,0,10, { 0x60000000,0xe000002a,0x20000043,0xb0000040,0xd0000053,0xd0000022,0x20000000,0x60000032,0x60000043,0x20000040,0xe0000042,0x60000002,0x80000001,0x00000020,0x00000003,0x40000052,0x40000040,0xe0000052,0xa0000000,0x80000040,0x20000001,0x20000060,0x80000001,0x40000042,0xc0000043,0x40000022,0x00000003,0x40000042,0xc0000043,0xc0000022,0x00000001,0x40000002,0xc0000043,0x40000062,0x80000001,0x40000042,0x40000042,0x40000002,0x00000002,0x00000040,0x80000002,0x80000000,0x80000002,0x80000040,0x00000000,0x80000040,0x80000000,0x00000040,0x80000000,0x00000040,0x80000002,0x00000000,0x80000000,0x80000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000004,0x00000080,0x00000004,0x00000009,0x00000101,0x00000009,0x00000012,0x00000202,0x0000001a,0x00000124,0x0000040c,0x00000026,0x0000004a,0x0000080a,0x00000060,0x00000590 } } +, {1,50,0,65,0,11, { 0x0800000c,0x18000000,0xb800000a,0xc8000010,0x2c000010,0xf4000014,0xb4000008,0x08000000,0x9800000c,0xd8000010,0x08000010,0xb8000010,0x98000000,0x60000000,0x00000008,0xc0000000,0x90000014,0x10000010,0xb8000014,0x28000000,0x20000010,0x48000000,0x08000018,0x60000000,0x90000010,0xf0000010,0x90000008,0xc0000000,0x90000010,0xf0000010,0xb0000008,0x40000000,0x90000000,0xf0000010,0x90000018,0x60000000,0x90000010,0x90000010,0x90000000,0x80000000,0x00000010,0xa0000000,0x20000000,0xa0000000,0x20000010,0x00000000,0x20000010,0x20000000,0x00000010,0x20000000,0x00000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000040,0x40000002,0x80000004,0x80000080,0x80000006,0x00000049,0x00000103,0x80000009,0x80000012,0x80000202,0x00000018 } } +, {1,50,2,65,0,12, { 0x20000030,0x60000000,0xe000002a,0x20000043,0xb0000040,0xd0000053,0xd0000022,0x20000000,0x60000032,0x60000043,0x20000040,0xe0000042,0x60000002,0x80000001,0x00000020,0x00000003,0x40000052,0x40000040,0xe0000052,0xa0000000,0x80000040,0x20000001,0x20000060,0x80000001,0x40000042,0xc0000043,0x40000022,0x00000003,0x40000042,0xc0000043,0xc0000022,0x00000001,0x40000002,0xc0000043,0x40000062,0x80000001,0x40000042,0x40000042,0x40000002,0x00000002,0x00000040,0x80000002,0x80000000,0x80000002,0x80000040,0x00000000,0x80000040,0x80000000,0x00000040,0x80000000,0x00000040,0x80000002,0x00000000,0x80000000,0x80000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000004,0x00000080,0x00000004,0x00000009,0x00000101,0x00000009,0x00000012,0x00000202,0x0000001a,0x00000124,0x0000040c,0x00000026,0x0000004a,0x0000080a,0x00000060 } } +, {1,51,0,65,0,13, { 0xe8000000,0x0800000c,0x18000000,0xb800000a,0xc8000010,0x2c000010,0xf4000014,0xb4000008,0x08000000,0x9800000c,0xd8000010,0x08000010,0xb8000010,0x98000000,0x60000000,0x00000008,0xc0000000,0x90000014,0x10000010,0xb8000014,0x28000000,0x20000010,0x48000000,0x08000018,0x60000000,0x90000010,0xf0000010,0x90000008,0xc0000000,0x90000010,0xf0000010,0xb0000008,0x40000000,0x90000000,0xf0000010,0x90000018,0x60000000,0x90000010,0x90000010,0x90000000,0x80000000,0x00000010,0xa0000000,0x20000000,0xa0000000,0x20000010,0x00000000,0x20000010,0x20000000,0x00000010,0x20000000,0x00000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000040,0x40000002,0x80000004,0x80000080,0x80000006,0x00000049,0x00000103,0x80000009,0x80000012,0x80000202 } } +, {1,51,2,65,0,14, { 0xa0000003,0x20000030,0x60000000,0xe000002a,0x20000043,0xb0000040,0xd0000053,0xd0000022,0x20000000,0x60000032,0x60000043,0x20000040,0xe0000042,0x60000002,0x80000001,0x00000020,0x00000003,0x40000052,0x40000040,0xe0000052,0xa0000000,0x80000040,0x20000001,0x20000060,0x80000001,0x40000042,0xc0000043,0x40000022,0x00000003,0x40000042,0xc0000043,0xc0000022,0x00000001,0x40000002,0xc0000043,0x40000062,0x80000001,0x40000042,0x40000042,0x40000002,0x00000002,0x00000040,0x80000002,0x80000000,0x80000002,0x80000040,0x00000000,0x80000040,0x80000000,0x00000040,0x80000000,0x00000040,0x80000002,0x00000000,0x80000000,0x80000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000004,0x00000080,0x00000004,0x00000009,0x00000101,0x00000009,0x00000012,0x00000202,0x0000001a,0x00000124,0x0000040c,0x00000026,0x0000004a,0x0000080a } } +, {1,52,0,65,0,15, { 0x04000010,0xe8000000,0x0800000c,0x18000000,0xb800000a,0xc8000010,0x2c000010,0xf4000014,0xb4000008,0x08000000,0x9800000c,0xd8000010,0x08000010,0xb8000010,0x98000000,0x60000000,0x00000008,0xc0000000,0x90000014,0x10000010,0xb8000014,0x28000000,0x20000010,0x48000000,0x08000018,0x60000000,0x90000010,0xf0000010,0x90000008,0xc0000000,0x90000010,0xf0000010,0xb0000008,0x40000000,0x90000000,0xf0000010,0x90000018,0x60000000,0x90000010,0x90000010,0x90000000,0x80000000,0x00000010,0xa0000000,0x20000000,0xa0000000,0x20000010,0x00000000,0x20000010,0x20000000,0x00000010,0x20000000,0x00000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000040,0x40000002,0x80000004,0x80000080,0x80000006,0x00000049,0x00000103,0x80000009,0x80000012 } } +, {2,45,0,58,0,16, { 0xec000014,0x0c000002,0xc0000010,0xb400001c,0x2c000004,0xbc000018,0xb0000010,0x0000000c,0xb8000010,0x08000018,0x78000010,0x08000014,0x70000010,0xb800001c,0xe8000000,0xb0000004,0x58000010,0xb000000c,0x48000000,0xb0000000,0xb8000010,0x98000010,0xa0000000,0x00000000,0x00000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0x20000000,0x00000010,0x60000000,0x00000018,0xe0000000,0x90000000,0x30000010,0xb0000000,0x20000000,0x20000000,0xa0000000,0x00000010,0x80000000,0x20000000,0x20000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000041,0x40000022,0x80000005,0xc0000082,0xc0000046,0x4000004b,0x80000107,0x00000089,0x00000014,0x8000024b,0x0000011b,0x8000016d,0x8000041a,0x000002e4,0x80000054,0x00000967 } } +, {2,46,0,58,0,17, { 0x2400001c,0xec000014,0x0c000002,0xc0000010,0xb400001c,0x2c000004,0xbc000018,0xb0000010,0x0000000c,0xb8000010,0x08000018,0x78000010,0x08000014,0x70000010,0xb800001c,0xe8000000,0xb0000004,0x58000010,0xb000000c,0x48000000,0xb0000000,0xb8000010,0x98000010,0xa0000000,0x00000000,0x00000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0x20000000,0x00000010,0x60000000,0x00000018,0xe0000000,0x90000000,0x30000010,0xb0000000,0x20000000,0x20000000,0xa0000000,0x00000010,0x80000000,0x20000000,0x20000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000041,0x40000022,0x80000005,0xc0000082,0xc0000046,0x4000004b,0x80000107,0x00000089,0x00000014,0x8000024b,0x0000011b,0x8000016d,0x8000041a,0x000002e4,0x80000054 } } +, {2,46,2,58,0,18, { 0x90000070,0xb0000053,0x30000008,0x00000043,0xd0000072,0xb0000010,0xf0000062,0xc0000042,0x00000030,0xe0000042,0x20000060,0xe0000041,0x20000050,0xc0000041,0xe0000072,0xa0000003,0xc0000012,0x60000041,0xc0000032,0x20000001,0xc0000002,0xe0000042,0x60000042,0x80000002,0x00000000,0x00000000,0x80000000,0x00000002,0x00000040,0x00000000,0x80000040,0x80000000,0x00000040,0x80000001,0x00000060,0x80000003,0x40000002,0xc0000040,0xc0000002,0x80000000,0x80000000,0x80000002,0x00000040,0x00000002,0x80000000,0x80000000,0x80000000,0x00000002,0x00000040,0x00000000,0x80000040,0x80000002,0x00000000,0x80000000,0x80000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000004,0x00000080,0x00000004,0x00000009,0x00000105,0x00000089,0x00000016,0x0000020b,0x0000011b,0x0000012d,0x0000041e,0x00000224,0x00000050,0x0000092e,0x0000046c,0x000005b6,0x0000106a,0x00000b90,0x00000152 } } +, {2,47,0,58,0,19, { 0x20000010,0x2400001c,0xec000014,0x0c000002,0xc0000010,0xb400001c,0x2c000004,0xbc000018,0xb0000010,0x0000000c,0xb8000010,0x08000018,0x78000010,0x08000014,0x70000010,0xb800001c,0xe8000000,0xb0000004,0x58000010,0xb000000c,0x48000000,0xb0000000,0xb8000010,0x98000010,0xa0000000,0x00000000,0x00000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0x20000000,0x00000010,0x60000000,0x00000018,0xe0000000,0x90000000,0x30000010,0xb0000000,0x20000000,0x20000000,0xa0000000,0x00000010,0x80000000,0x20000000,0x20000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000041,0x40000022,0x80000005,0xc0000082,0xc0000046,0x4000004b,0x80000107,0x00000089,0x00000014,0x8000024b,0x0000011b,0x8000016d,0x8000041a,0x000002e4 } } +, {2,48,0,58,0,20, { 0xbc00001a,0x20000010,0x2400001c,0xec000014,0x0c000002,0xc0000010,0xb400001c,0x2c000004,0xbc000018,0xb0000010,0x0000000c,0xb8000010,0x08000018,0x78000010,0x08000014,0x70000010,0xb800001c,0xe8000000,0xb0000004,0x58000010,0xb000000c,0x48000000,0xb0000000,0xb8000010,0x98000010,0xa0000000,0x00000000,0x00000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0x20000000,0x00000010,0x60000000,0x00000018,0xe0000000,0x90000000,0x30000010,0xb0000000,0x20000000,0x20000000,0xa0000000,0x00000010,0x80000000,0x20000000,0x20000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000041,0x40000022,0x80000005,0xc0000082,0xc0000046,0x4000004b,0x80000107,0x00000089,0x00000014,0x8000024b,0x0000011b,0x8000016d,0x8000041a } } +, {2,49,0,58,0,21, { 0x3c000004,0xbc00001a,0x20000010,0x2400001c,0xec000014,0x0c000002,0xc0000010,0xb400001c,0x2c000004,0xbc000018,0xb0000010,0x0000000c,0xb8000010,0x08000018,0x78000010,0x08000014,0x70000010,0xb800001c,0xe8000000,0xb0000004,0x58000010,0xb000000c,0x48000000,0xb0000000,0xb8000010,0x98000010,0xa0000000,0x00000000,0x00000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0x20000000,0x00000010,0x60000000,0x00000018,0xe0000000,0x90000000,0x30000010,0xb0000000,0x20000000,0x20000000,0xa0000000,0x00000010,0x80000000,0x20000000,0x20000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000041,0x40000022,0x80000005,0xc0000082,0xc0000046,0x4000004b,0x80000107,0x00000089,0x00000014,0x8000024b,0x0000011b,0x8000016d } } +, {2,49,2,58,0,22, { 0xf0000010,0xf000006a,0x80000040,0x90000070,0xb0000053,0x30000008,0x00000043,0xd0000072,0xb0000010,0xf0000062,0xc0000042,0x00000030,0xe0000042,0x20000060,0xe0000041,0x20000050,0xc0000041,0xe0000072,0xa0000003,0xc0000012,0x60000041,0xc0000032,0x20000001,0xc0000002,0xe0000042,0x60000042,0x80000002,0x00000000,0x00000000,0x80000000,0x00000002,0x00000040,0x00000000,0x80000040,0x80000000,0x00000040,0x80000001,0x00000060,0x80000003,0x40000002,0xc0000040,0xc0000002,0x80000000,0x80000000,0x80000002,0x00000040,0x00000002,0x80000000,0x80000000,0x80000000,0x00000002,0x00000040,0x00000000,0x80000040,0x80000002,0x00000000,0x80000000,0x80000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000004,0x00000080,0x00000004,0x00000009,0x00000105,0x00000089,0x00000016,0x0000020b,0x0000011b,0x0000012d,0x0000041e,0x00000224,0x00000050,0x0000092e,0x0000046c,0x000005b6 } } +, {2,50,0,65,0,23, { 0xb400001c,0x3c000004,0xbc00001a,0x20000010,0x2400001c,0xec000014,0x0c000002,0xc0000010,0xb400001c,0x2c000004,0xbc000018,0xb0000010,0x0000000c,0xb8000010,0x08000018,0x78000010,0x08000014,0x70000010,0xb800001c,0xe8000000,0xb0000004,0x58000010,0xb000000c,0x48000000,0xb0000000,0xb8000010,0x98000010,0xa0000000,0x00000000,0x00000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0x20000000,0x00000010,0x60000000,0x00000018,0xe0000000,0x90000000,0x30000010,0xb0000000,0x20000000,0x20000000,0xa0000000,0x00000010,0x80000000,0x20000000,0x20000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000041,0x40000022,0x80000005,0xc0000082,0xc0000046,0x4000004b,0x80000107,0x00000089,0x00000014,0x8000024b,0x0000011b } } +, {2,50,2,65,0,24, { 0xd0000072,0xf0000010,0xf000006a,0x80000040,0x90000070,0xb0000053,0x30000008,0x00000043,0xd0000072,0xb0000010,0xf0000062,0xc0000042,0x00000030,0xe0000042,0x20000060,0xe0000041,0x20000050,0xc0000041,0xe0000072,0xa0000003,0xc0000012,0x60000041,0xc0000032,0x20000001,0xc0000002,0xe0000042,0x60000042,0x80000002,0x00000000,0x00000000,0x80000000,0x00000002,0x00000040,0x00000000,0x80000040,0x80000000,0x00000040,0x80000001,0x00000060,0x80000003,0x40000002,0xc0000040,0xc0000002,0x80000000,0x80000000,0x80000002,0x00000040,0x00000002,0x80000000,0x80000000,0x80000000,0x00000002,0x00000040,0x00000000,0x80000040,0x80000002,0x00000000,0x80000000,0x80000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000004,0x00000080,0x00000004,0x00000009,0x00000105,0x00000089,0x00000016,0x0000020b,0x0000011b,0x0000012d,0x0000041e,0x00000224,0x00000050,0x0000092e,0x0000046c } } +, {2,51,0,65,0,25, { 0xc0000010,0xb400001c,0x3c000004,0xbc00001a,0x20000010,0x2400001c,0xec000014,0x0c000002,0xc0000010,0xb400001c,0x2c000004,0xbc000018,0xb0000010,0x0000000c,0xb8000010,0x08000018,0x78000010,0x08000014,0x70000010,0xb800001c,0xe8000000,0xb0000004,0x58000010,0xb000000c,0x48000000,0xb0000000,0xb8000010,0x98000010,0xa0000000,0x00000000,0x00000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0x20000000,0x00000010,0x60000000,0x00000018,0xe0000000,0x90000000,0x30000010,0xb0000000,0x20000000,0x20000000,0xa0000000,0x00000010,0x80000000,0x20000000,0x20000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000041,0x40000022,0x80000005,0xc0000082,0xc0000046,0x4000004b,0x80000107,0x00000089,0x00000014,0x8000024b } } +, {2,51,2,65,0,26, { 0x00000043,0xd0000072,0xf0000010,0xf000006a,0x80000040,0x90000070,0xb0000053,0x30000008,0x00000043,0xd0000072,0xb0000010,0xf0000062,0xc0000042,0x00000030,0xe0000042,0x20000060,0xe0000041,0x20000050,0xc0000041,0xe0000072,0xa0000003,0xc0000012,0x60000041,0xc0000032,0x20000001,0xc0000002,0xe0000042,0x60000042,0x80000002,0x00000000,0x00000000,0x80000000,0x00000002,0x00000040,0x00000000,0x80000040,0x80000000,0x00000040,0x80000001,0x00000060,0x80000003,0x40000002,0xc0000040,0xc0000002,0x80000000,0x80000000,0x80000002,0x00000040,0x00000002,0x80000000,0x80000000,0x80000000,0x00000002,0x00000040,0x00000000,0x80000040,0x80000002,0x00000000,0x80000000,0x80000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000004,0x00000080,0x00000004,0x00000009,0x00000105,0x00000089,0x00000016,0x0000020b,0x0000011b,0x0000012d,0x0000041e,0x00000224,0x00000050,0x0000092e } } +, {2,52,0,65,0,27, { 0x0c000002,0xc0000010,0xb400001c,0x3c000004,0xbc00001a,0x20000010,0x2400001c,0xec000014,0x0c000002,0xc0000010,0xb400001c,0x2c000004,0xbc000018,0xb0000010,0x0000000c,0xb8000010,0x08000018,0x78000010,0x08000014,0x70000010,0xb800001c,0xe8000000,0xb0000004,0x58000010,0xb000000c,0x48000000,0xb0000000,0xb8000010,0x98000010,0xa0000000,0x00000000,0x00000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0x20000000,0x00000010,0x60000000,0x00000018,0xe0000000,0x90000000,0x30000010,0xb0000000,0x20000000,0x20000000,0xa0000000,0x00000010,0x80000000,0x20000000,0x20000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000041,0x40000022,0x80000005,0xc0000082,0xc0000046,0x4000004b,0x80000107,0x00000089,0x00000014 } } +, {2,53,0,65,0,28, { 0xcc000014,0x0c000002,0xc0000010,0xb400001c,0x3c000004,0xbc00001a,0x20000010,0x2400001c,0xec000014,0x0c000002,0xc0000010,0xb400001c,0x2c000004,0xbc000018,0xb0000010,0x0000000c,0xb8000010,0x08000018,0x78000010,0x08000014,0x70000010,0xb800001c,0xe8000000,0xb0000004,0x58000010,0xb000000c,0x48000000,0xb0000000,0xb8000010,0x98000010,0xa0000000,0x00000000,0x00000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0x20000000,0x00000010,0x60000000,0x00000018,0xe0000000,0x90000000,0x30000010,0xb0000000,0x20000000,0x20000000,0xa0000000,0x00000010,0x80000000,0x20000000,0x20000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000041,0x40000022,0x80000005,0xc0000082,0xc0000046,0x4000004b,0x80000107,0x00000089 } } +, {2,54,0,65,0,29, { 0x0400001c,0xcc000014,0x0c000002,0xc0000010,0xb400001c,0x3c000004,0xbc00001a,0x20000010,0x2400001c,0xec000014,0x0c000002,0xc0000010,0xb400001c,0x2c000004,0xbc000018,0xb0000010,0x0000000c,0xb8000010,0x08000018,0x78000010,0x08000014,0x70000010,0xb800001c,0xe8000000,0xb0000004,0x58000010,0xb000000c,0x48000000,0xb0000000,0xb8000010,0x98000010,0xa0000000,0x00000000,0x00000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0x20000000,0x00000010,0x60000000,0x00000018,0xe0000000,0x90000000,0x30000010,0xb0000000,0x20000000,0x20000000,0xa0000000,0x00000010,0x80000000,0x20000000,0x20000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000041,0x40000022,0x80000005,0xc0000082,0xc0000046,0x4000004b,0x80000107 } } +, {2,55,0,65,0,30, { 0x00000010,0x0400001c,0xcc000014,0x0c000002,0xc0000010,0xb400001c,0x3c000004,0xbc00001a,0x20000010,0x2400001c,0xec000014,0x0c000002,0xc0000010,0xb400001c,0x2c000004,0xbc000018,0xb0000010,0x0000000c,0xb8000010,0x08000018,0x78000010,0x08000014,0x70000010,0xb800001c,0xe8000000,0xb0000004,0x58000010,0xb000000c,0x48000000,0xb0000000,0xb8000010,0x98000010,0xa0000000,0x00000000,0x00000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0x20000000,0x00000010,0x60000000,0x00000018,0xe0000000,0x90000000,0x30000010,0xb0000000,0x20000000,0x20000000,0xa0000000,0x00000010,0x80000000,0x20000000,0x20000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000041,0x40000022,0x80000005,0xc0000082,0xc0000046,0x4000004b } } +, {2,56,0,65,0,31, { 0x2600001a,0x00000010,0x0400001c,0xcc000014,0x0c000002,0xc0000010,0xb400001c,0x3c000004,0xbc00001a,0x20000010,0x2400001c,0xec000014,0x0c000002,0xc0000010,0xb400001c,0x2c000004,0xbc000018,0xb0000010,0x0000000c,0xb8000010,0x08000018,0x78000010,0x08000014,0x70000010,0xb800001c,0xe8000000,0xb0000004,0x58000010,0xb000000c,0x48000000,0xb0000000,0xb8000010,0x98000010,0xa0000000,0x00000000,0x00000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0x20000000,0x00000010,0x60000000,0x00000018,0xe0000000,0x90000000,0x30000010,0xb0000000,0x20000000,0x20000000,0xa0000000,0x00000010,0x80000000,0x20000000,0x20000000,0x20000000,0x80000000,0x00000010,0x00000000,0x20000010,0xa0000000,0x00000000,0x20000000,0x20000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001,0x00000020,0x00000001,0x40000002,0x40000041,0x40000022,0x80000005,0xc0000082,0xc0000046 } } +, {0,0,0,0,0,0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} +}; +void ubc_check(const uint32_t W[80], uint32_t dvmask[1]) +{ + uint32_t mask = ~((uint32_t)(0)); + mask &= (((((W[44]^W[45])>>29)&1)-1) | ~(DV_I_48_0_bit|DV_I_51_0_bit|DV_I_52_0_bit|DV_II_45_0_bit|DV_II_46_0_bit|DV_II_50_0_bit|DV_II_51_0_bit)); + mask &= (((((W[49]^W[50])>>29)&1)-1) | ~(DV_I_46_0_bit|DV_II_45_0_bit|DV_II_50_0_bit|DV_II_51_0_bit|DV_II_55_0_bit|DV_II_56_0_bit)); + mask &= (((((W[48]^W[49])>>29)&1)-1) | ~(DV_I_45_0_bit|DV_I_52_0_bit|DV_II_49_0_bit|DV_II_50_0_bit|DV_II_54_0_bit|DV_II_55_0_bit)); + mask &= ((((W[47]^(W[50]>>25))&(1<<4))-(1<<4)) | ~(DV_I_47_0_bit|DV_I_49_0_bit|DV_I_51_0_bit|DV_II_45_0_bit|DV_II_51_0_bit|DV_II_56_0_bit)); + mask &= (((((W[47]^W[48])>>29)&1)-1) | ~(DV_I_44_0_bit|DV_I_51_0_bit|DV_II_48_0_bit|DV_II_49_0_bit|DV_II_53_0_bit|DV_II_54_0_bit)); + mask &= (((((W[46]>>4)^(W[49]>>29))&1)-1) | ~(DV_I_46_0_bit|DV_I_48_0_bit|DV_I_50_0_bit|DV_I_52_0_bit|DV_II_50_0_bit|DV_II_55_0_bit)); + mask &= (((((W[46]^W[47])>>29)&1)-1) | ~(DV_I_43_0_bit|DV_I_50_0_bit|DV_II_47_0_bit|DV_II_48_0_bit|DV_II_52_0_bit|DV_II_53_0_bit)); + mask &= (((((W[45]>>4)^(W[48]>>29))&1)-1) | ~(DV_I_45_0_bit|DV_I_47_0_bit|DV_I_49_0_bit|DV_I_51_0_bit|DV_II_49_0_bit|DV_II_54_0_bit)); + mask &= (((((W[45]^W[46])>>29)&1)-1) | ~(DV_I_49_0_bit|DV_I_52_0_bit|DV_II_46_0_bit|DV_II_47_0_bit|DV_II_51_0_bit|DV_II_52_0_bit)); + mask &= (((((W[44]>>4)^(W[47]>>29))&1)-1) | ~(DV_I_44_0_bit|DV_I_46_0_bit|DV_I_48_0_bit|DV_I_50_0_bit|DV_II_48_0_bit|DV_II_53_0_bit)); + mask &= (((((W[43]>>4)^(W[46]>>29))&1)-1) | ~(DV_I_43_0_bit|DV_I_45_0_bit|DV_I_47_0_bit|DV_I_49_0_bit|DV_II_47_0_bit|DV_II_52_0_bit)); + mask &= (((((W[43]^W[44])>>29)&1)-1) | ~(DV_I_47_0_bit|DV_I_50_0_bit|DV_I_51_0_bit|DV_II_45_0_bit|DV_II_49_0_bit|DV_II_50_0_bit)); + mask &= (((((W[42]>>4)^(W[45]>>29))&1)-1) | ~(DV_I_44_0_bit|DV_I_46_0_bit|DV_I_48_0_bit|DV_I_52_0_bit|DV_II_46_0_bit|DV_II_51_0_bit)); + mask &= (((((W[41]>>4)^(W[44]>>29))&1)-1) | ~(DV_I_43_0_bit|DV_I_45_0_bit|DV_I_47_0_bit|DV_I_51_0_bit|DV_II_45_0_bit|DV_II_50_0_bit)); + mask &= (((((W[40]^W[41])>>29)&1)-1) | ~(DV_I_44_0_bit|DV_I_47_0_bit|DV_I_48_0_bit|DV_II_46_0_bit|DV_II_47_0_bit|DV_II_56_0_bit)); + mask &= (((((W[54]^W[55])>>29)&1)-1) | ~(DV_I_51_0_bit|DV_II_47_0_bit|DV_II_50_0_bit|DV_II_55_0_bit|DV_II_56_0_bit)); + mask &= (((((W[53]^W[54])>>29)&1)-1) | ~(DV_I_50_0_bit|DV_II_46_0_bit|DV_II_49_0_bit|DV_II_54_0_bit|DV_II_55_0_bit)); + mask &= (((((W[52]^W[53])>>29)&1)-1) | ~(DV_I_49_0_bit|DV_II_45_0_bit|DV_II_48_0_bit|DV_II_53_0_bit|DV_II_54_0_bit)); + mask &= ((((W[50]^(W[53]>>25))&(1<<4))-(1<<4)) | ~(DV_I_50_0_bit|DV_I_52_0_bit|DV_II_46_0_bit|DV_II_48_0_bit|DV_II_54_0_bit)); + mask &= (((((W[50]^W[51])>>29)&1)-1) | ~(DV_I_47_0_bit|DV_II_46_0_bit|DV_II_51_0_bit|DV_II_52_0_bit|DV_II_56_0_bit)); + mask &= ((((W[49]^(W[52]>>25))&(1<<4))-(1<<4)) | ~(DV_I_49_0_bit|DV_I_51_0_bit|DV_II_45_0_bit|DV_II_47_0_bit|DV_II_53_0_bit)); + mask &= ((((W[48]^(W[51]>>25))&(1<<4))-(1<<4)) | ~(DV_I_48_0_bit|DV_I_50_0_bit|DV_I_52_0_bit|DV_II_46_0_bit|DV_II_52_0_bit)); + mask &= (((((W[42]^W[43])>>29)&1)-1) | ~(DV_I_46_0_bit|DV_I_49_0_bit|DV_I_50_0_bit|DV_II_48_0_bit|DV_II_49_0_bit)); + mask &= (((((W[41]^W[42])>>29)&1)-1) | ~(DV_I_45_0_bit|DV_I_48_0_bit|DV_I_49_0_bit|DV_II_47_0_bit|DV_II_48_0_bit)); + mask &= (((((W[40]>>4)^(W[43]>>29))&1)-1) | ~(DV_I_44_0_bit|DV_I_46_0_bit|DV_I_50_0_bit|DV_II_49_0_bit|DV_II_56_0_bit)); + mask &= (((((W[39]>>4)^(W[42]>>29))&1)-1) | ~(DV_I_43_0_bit|DV_I_45_0_bit|DV_I_49_0_bit|DV_II_48_0_bit|DV_II_55_0_bit)); + if (mask & (DV_I_44_0_bit|DV_I_48_0_bit|DV_II_47_0_bit|DV_II_54_0_bit|DV_II_56_0_bit)) + mask &= (((((W[38]>>4)^(W[41]>>29))&1)-1) | ~(DV_I_44_0_bit|DV_I_48_0_bit|DV_II_47_0_bit|DV_II_54_0_bit|DV_II_56_0_bit)); + mask &= (((((W[37]>>4)^(W[40]>>29))&1)-1) | ~(DV_I_43_0_bit|DV_I_47_0_bit|DV_II_46_0_bit|DV_II_53_0_bit|DV_II_55_0_bit)); + if (mask & (DV_I_52_0_bit|DV_II_48_0_bit|DV_II_51_0_bit|DV_II_56_0_bit)) + mask &= (((((W[55]^W[56])>>29)&1)-1) | ~(DV_I_52_0_bit|DV_II_48_0_bit|DV_II_51_0_bit|DV_II_56_0_bit)); + if (mask & (DV_I_52_0_bit|DV_II_48_0_bit|DV_II_50_0_bit|DV_II_56_0_bit)) + mask &= ((((W[52]^(W[55]>>25))&(1<<4))-(1<<4)) | ~(DV_I_52_0_bit|DV_II_48_0_bit|DV_II_50_0_bit|DV_II_56_0_bit)); + if (mask & (DV_I_51_0_bit|DV_II_47_0_bit|DV_II_49_0_bit|DV_II_55_0_bit)) + mask &= ((((W[51]^(W[54]>>25))&(1<<4))-(1<<4)) | ~(DV_I_51_0_bit|DV_II_47_0_bit|DV_II_49_0_bit|DV_II_55_0_bit)); + if (mask & (DV_I_48_0_bit|DV_II_47_0_bit|DV_II_52_0_bit|DV_II_53_0_bit)) + mask &= (((((W[51]^W[52])>>29)&1)-1) | ~(DV_I_48_0_bit|DV_II_47_0_bit|DV_II_52_0_bit|DV_II_53_0_bit)); + if (mask & (DV_I_46_0_bit|DV_I_49_0_bit|DV_II_45_0_bit|DV_II_48_0_bit)) + mask &= (((((W[36]>>4)^(W[40]>>29))&1)-1) | ~(DV_I_46_0_bit|DV_I_49_0_bit|DV_II_45_0_bit|DV_II_48_0_bit)); + if (mask & (DV_I_52_0_bit|DV_II_48_0_bit|DV_II_49_0_bit)) + mask &= ((0-(((W[53]^W[56])>>29)&1)) | ~(DV_I_52_0_bit|DV_II_48_0_bit|DV_II_49_0_bit)); + if (mask & (DV_I_50_0_bit|DV_II_46_0_bit|DV_II_47_0_bit)) + mask &= ((0-(((W[51]^W[54])>>29)&1)) | ~(DV_I_50_0_bit|DV_II_46_0_bit|DV_II_47_0_bit)); + if (mask & (DV_I_49_0_bit|DV_I_51_0_bit|DV_II_45_0_bit)) + mask &= ((0-(((W[50]^W[52])>>29)&1)) | ~(DV_I_49_0_bit|DV_I_51_0_bit|DV_II_45_0_bit)); + if (mask & (DV_I_48_0_bit|DV_I_50_0_bit|DV_I_52_0_bit)) + mask &= ((0-(((W[49]^W[51])>>29)&1)) | ~(DV_I_48_0_bit|DV_I_50_0_bit|DV_I_52_0_bit)); + if (mask & (DV_I_47_0_bit|DV_I_49_0_bit|DV_I_51_0_bit)) + mask &= ((0-(((W[48]^W[50])>>29)&1)) | ~(DV_I_47_0_bit|DV_I_49_0_bit|DV_I_51_0_bit)); + if (mask & (DV_I_46_0_bit|DV_I_48_0_bit|DV_I_50_0_bit)) + mask &= ((0-(((W[47]^W[49])>>29)&1)) | ~(DV_I_46_0_bit|DV_I_48_0_bit|DV_I_50_0_bit)); + if (mask & (DV_I_45_0_bit|DV_I_47_0_bit|DV_I_49_0_bit)) + mask &= ((0-(((W[46]^W[48])>>29)&1)) | ~(DV_I_45_0_bit|DV_I_47_0_bit|DV_I_49_0_bit)); + mask &= ((((W[45]^W[47])&(1<<6))-(1<<6)) | ~(DV_I_47_2_bit|DV_I_49_2_bit|DV_I_51_2_bit)); + if (mask & (DV_I_44_0_bit|DV_I_46_0_bit|DV_I_48_0_bit)) + mask &= ((0-(((W[45]^W[47])>>29)&1)) | ~(DV_I_44_0_bit|DV_I_46_0_bit|DV_I_48_0_bit)); + mask &= (((((W[44]^W[46])>>6)&1)-1) | ~(DV_I_46_2_bit|DV_I_48_2_bit|DV_I_50_2_bit)); + if (mask & (DV_I_43_0_bit|DV_I_45_0_bit|DV_I_47_0_bit)) + mask &= ((0-(((W[44]^W[46])>>29)&1)) | ~(DV_I_43_0_bit|DV_I_45_0_bit|DV_I_47_0_bit)); + mask &= ((0-((W[41]^(W[42]>>5))&(1<<1))) | ~(DV_I_48_2_bit|DV_II_46_2_bit|DV_II_51_2_bit)); + mask &= ((0-((W[40]^(W[41]>>5))&(1<<1))) | ~(DV_I_47_2_bit|DV_I_51_2_bit|DV_II_50_2_bit)); + if (mask & (DV_I_44_0_bit|DV_I_46_0_bit|DV_II_56_0_bit)) + mask &= ((0-(((W[40]^W[42])>>4)&1)) | ~(DV_I_44_0_bit|DV_I_46_0_bit|DV_II_56_0_bit)); + mask &= ((0-((W[39]^(W[40]>>5))&(1<<1))) | ~(DV_I_46_2_bit|DV_I_50_2_bit|DV_II_49_2_bit)); + if (mask & (DV_I_43_0_bit|DV_I_45_0_bit|DV_II_55_0_bit)) + mask &= ((0-(((W[39]^W[41])>>4)&1)) | ~(DV_I_43_0_bit|DV_I_45_0_bit|DV_II_55_0_bit)); + if (mask & (DV_I_44_0_bit|DV_II_54_0_bit|DV_II_56_0_bit)) + mask &= ((0-(((W[38]^W[40])>>4)&1)) | ~(DV_I_44_0_bit|DV_II_54_0_bit|DV_II_56_0_bit)); + if (mask & (DV_I_43_0_bit|DV_II_53_0_bit|DV_II_55_0_bit)) + mask &= ((0-(((W[37]^W[39])>>4)&1)) | ~(DV_I_43_0_bit|DV_II_53_0_bit|DV_II_55_0_bit)); + mask &= ((0-((W[36]^(W[37]>>5))&(1<<1))) | ~(DV_I_47_2_bit|DV_I_50_2_bit|DV_II_46_2_bit)); + if (mask & (DV_I_45_0_bit|DV_I_48_0_bit|DV_II_47_0_bit)) + mask &= (((((W[35]>>4)^(W[39]>>29))&1)-1) | ~(DV_I_45_0_bit|DV_I_48_0_bit|DV_II_47_0_bit)); + if (mask & (DV_I_48_0_bit|DV_II_48_0_bit)) + mask &= ((0-((W[63]^(W[64]>>5))&(1<<0))) | ~(DV_I_48_0_bit|DV_II_48_0_bit)); + if (mask & (DV_I_45_0_bit|DV_II_45_0_bit)) + mask &= ((0-((W[63]^(W[64]>>5))&(1<<1))) | ~(DV_I_45_0_bit|DV_II_45_0_bit)); + if (mask & (DV_I_47_0_bit|DV_II_47_0_bit)) + mask &= ((0-((W[62]^(W[63]>>5))&(1<<0))) | ~(DV_I_47_0_bit|DV_II_47_0_bit)); + if (mask & (DV_I_46_0_bit|DV_II_46_0_bit)) + mask &= ((0-((W[61]^(W[62]>>5))&(1<<0))) | ~(DV_I_46_0_bit|DV_II_46_0_bit)); + mask &= ((0-((W[61]^(W[62]>>5))&(1<<2))) | ~(DV_I_46_2_bit|DV_II_46_2_bit)); + if (mask & (DV_I_45_0_bit|DV_II_45_0_bit)) + mask &= ((0-((W[60]^(W[61]>>5))&(1<<0))) | ~(DV_I_45_0_bit|DV_II_45_0_bit)); + if (mask & (DV_II_51_0_bit|DV_II_54_0_bit)) + mask &= (((((W[58]^W[59])>>29)&1)-1) | ~(DV_II_51_0_bit|DV_II_54_0_bit)); + if (mask & (DV_II_50_0_bit|DV_II_53_0_bit)) + mask &= (((((W[57]^W[58])>>29)&1)-1) | ~(DV_II_50_0_bit|DV_II_53_0_bit)); + if (mask & (DV_II_52_0_bit|DV_II_54_0_bit)) + mask &= ((((W[56]^(W[59]>>25))&(1<<4))-(1<<4)) | ~(DV_II_52_0_bit|DV_II_54_0_bit)); + if (mask & (DV_II_51_0_bit|DV_II_52_0_bit)) + mask &= ((0-(((W[56]^W[59])>>29)&1)) | ~(DV_II_51_0_bit|DV_II_52_0_bit)); + if (mask & (DV_II_49_0_bit|DV_II_52_0_bit)) + mask &= (((((W[56]^W[57])>>29)&1)-1) | ~(DV_II_49_0_bit|DV_II_52_0_bit)); + if (mask & (DV_II_51_0_bit|DV_II_53_0_bit)) + mask &= ((((W[55]^(W[58]>>25))&(1<<4))-(1<<4)) | ~(DV_II_51_0_bit|DV_II_53_0_bit)); + if (mask & (DV_II_50_0_bit|DV_II_52_0_bit)) + mask &= ((((W[54]^(W[57]>>25))&(1<<4))-(1<<4)) | ~(DV_II_50_0_bit|DV_II_52_0_bit)); + if (mask & (DV_II_49_0_bit|DV_II_51_0_bit)) + mask &= ((((W[53]^(W[56]>>25))&(1<<4))-(1<<4)) | ~(DV_II_49_0_bit|DV_II_51_0_bit)); + mask &= ((((W[51]^(W[50]>>5))&(1<<1))-(1<<1)) | ~(DV_I_50_2_bit|DV_II_46_2_bit)); + mask &= ((((W[48]^W[50])&(1<<6))-(1<<6)) | ~(DV_I_50_2_bit|DV_II_46_2_bit)); + if (mask & (DV_I_51_0_bit|DV_I_52_0_bit)) + mask &= ((0-(((W[48]^W[55])>>29)&1)) | ~(DV_I_51_0_bit|DV_I_52_0_bit)); + mask &= ((((W[47]^W[49])&(1<<6))-(1<<6)) | ~(DV_I_49_2_bit|DV_I_51_2_bit)); + mask &= ((((W[48]^(W[47]>>5))&(1<<1))-(1<<1)) | ~(DV_I_47_2_bit|DV_II_51_2_bit)); + mask &= ((((W[46]^W[48])&(1<<6))-(1<<6)) | ~(DV_I_48_2_bit|DV_I_50_2_bit)); + mask &= ((((W[47]^(W[46]>>5))&(1<<1))-(1<<1)) | ~(DV_I_46_2_bit|DV_II_50_2_bit)); + mask &= ((0-((W[44]^(W[45]>>5))&(1<<1))) | ~(DV_I_51_2_bit|DV_II_49_2_bit)); + mask &= ((((W[43]^W[45])&(1<<6))-(1<<6)) | ~(DV_I_47_2_bit|DV_I_49_2_bit)); + mask &= (((((W[42]^W[44])>>6)&1)-1) | ~(DV_I_46_2_bit|DV_I_48_2_bit)); + mask &= ((((W[43]^(W[42]>>5))&(1<<1))-(1<<1)) | ~(DV_II_46_2_bit|DV_II_51_2_bit)); + mask &= ((((W[42]^(W[41]>>5))&(1<<1))-(1<<1)) | ~(DV_I_51_2_bit|DV_II_50_2_bit)); + mask &= ((((W[41]^(W[40]>>5))&(1<<1))-(1<<1)) | ~(DV_I_50_2_bit|DV_II_49_2_bit)); + if (mask & (DV_I_52_0_bit|DV_II_51_0_bit)) + mask &= ((((W[39]^(W[43]>>25))&(1<<4))-(1<<4)) | ~(DV_I_52_0_bit|DV_II_51_0_bit)); + if (mask & (DV_I_51_0_bit|DV_II_50_0_bit)) + mask &= ((((W[38]^(W[42]>>25))&(1<<4))-(1<<4)) | ~(DV_I_51_0_bit|DV_II_50_0_bit)); + if (mask & (DV_I_48_2_bit|DV_I_51_2_bit)) + mask &= ((0-((W[37]^(W[38]>>5))&(1<<1))) | ~(DV_I_48_2_bit|DV_I_51_2_bit)); + if (mask & (DV_I_50_0_bit|DV_II_49_0_bit)) + mask &= ((((W[37]^(W[41]>>25))&(1<<4))-(1<<4)) | ~(DV_I_50_0_bit|DV_II_49_0_bit)); + if (mask & (DV_II_52_0_bit|DV_II_54_0_bit)) + mask &= ((0-((W[36]^W[38])&(1<<4))) | ~(DV_II_52_0_bit|DV_II_54_0_bit)); + mask &= ((0-((W[35]^(W[36]>>5))&(1<<1))) | ~(DV_I_46_2_bit|DV_I_49_2_bit)); + if (mask & (DV_I_51_0_bit|DV_II_47_0_bit)) + mask &= ((((W[35]^(W[39]>>25))&(1<<3))-(1<<3)) | ~(DV_I_51_0_bit|DV_II_47_0_bit)); +if (mask) { + + if (mask & DV_I_43_0_bit) + if ( + !((W[61]^(W[62]>>5)) & (1<<1)) + || !(!((W[59]^(W[63]>>25)) & (1<<5))) + || !((W[58]^(W[63]>>30)) & (1<<0)) + ) mask &= ~DV_I_43_0_bit; + if (mask & DV_I_44_0_bit) + if ( + !((W[62]^(W[63]>>5)) & (1<<1)) + || !(!((W[60]^(W[64]>>25)) & (1<<5))) + || !((W[59]^(W[64]>>30)) & (1<<0)) + ) mask &= ~DV_I_44_0_bit; + if (mask & DV_I_46_2_bit) + mask &= ((~((W[40]^W[42])>>2)) | ~DV_I_46_2_bit); + if (mask & DV_I_47_2_bit) + if ( + !((W[62]^(W[63]>>5)) & (1<<2)) + || !(!((W[41]^W[43]) & (1<<6))) + ) mask &= ~DV_I_47_2_bit; + if (mask & DV_I_48_2_bit) + if ( + !((W[63]^(W[64]>>5)) & (1<<2)) + || !(!((W[48]^(W[49]<<5)) & (1<<6))) + ) mask &= ~DV_I_48_2_bit; + if (mask & DV_I_49_2_bit) + if ( + !(!((W[49]^(W[50]<<5)) & (1<<6))) + || !((W[42]^W[50]) & (1<<1)) + || !(!((W[39]^(W[40]<<5)) & (1<<6))) + || !((W[38]^W[40]) & (1<<1)) + ) mask &= ~DV_I_49_2_bit; + if (mask & DV_I_50_0_bit) + mask &= ((((W[36]^W[37])<<7)) | ~DV_I_50_0_bit); + if (mask & DV_I_50_2_bit) + mask &= ((((W[43]^W[51])<<11)) | ~DV_I_50_2_bit); + if (mask & DV_I_51_0_bit) + mask &= ((((W[37]^W[38])<<9)) | ~DV_I_51_0_bit); + if (mask & DV_I_51_2_bit) + if ( + !(!((W[51]^(W[52]<<5)) & (1<<6))) + || !(!((W[49]^W[51]) & (1<<6))) + || !(!((W[37]^(W[37]>>5)) & (1<<1))) + || !(!((W[35]^(W[39]>>25)) & (1<<5))) + ) mask &= ~DV_I_51_2_bit; + if (mask & DV_I_52_0_bit) + mask &= ((((W[38]^W[39])<<11)) | ~DV_I_52_0_bit); + if (mask & DV_II_46_2_bit) + mask &= ((((W[47]^W[51])<<17)) | ~DV_II_46_2_bit); + if (mask & DV_II_48_0_bit) + if ( + !(!((W[36]^(W[40]>>25)) & (1<<3))) + || !((W[35]^(W[40]<<2)) & (1<<30)) + ) mask &= ~DV_II_48_0_bit; + if (mask & DV_II_49_0_bit) + if ( + !(!((W[37]^(W[41]>>25)) & (1<<3))) + || !((W[36]^(W[41]<<2)) & (1<<30)) + ) mask &= ~DV_II_49_0_bit; + if (mask & DV_II_49_2_bit) + if ( + !(!((W[53]^(W[54]<<5)) & (1<<6))) + || !(!((W[51]^W[53]) & (1<<6))) + || !((W[50]^W[54]) & (1<<1)) + || !(!((W[45]^(W[46]<<5)) & (1<<6))) + || !(!((W[37]^(W[41]>>25)) & (1<<5))) + || !((W[36]^(W[41]>>30)) & (1<<0)) + ) mask &= ~DV_II_49_2_bit; + if (mask & DV_II_50_0_bit) + if ( + !((W[55]^W[58]) & (1<<29)) + || !(!((W[38]^(W[42]>>25)) & (1<<3))) + || !((W[37]^(W[42]<<2)) & (1<<30)) + ) mask &= ~DV_II_50_0_bit; + if (mask & DV_II_50_2_bit) + if ( + !(!((W[54]^(W[55]<<5)) & (1<<6))) + || !(!((W[52]^W[54]) & (1<<6))) + || !((W[51]^W[55]) & (1<<1)) + || !((W[45]^W[47]) & (1<<1)) + || !(!((W[38]^(W[42]>>25)) & (1<<5))) + || !((W[37]^(W[42]>>30)) & (1<<0)) + ) mask &= ~DV_II_50_2_bit; + if (mask & DV_II_51_0_bit) + if ( + !(!((W[39]^(W[43]>>25)) & (1<<3))) + || !((W[38]^(W[43]<<2)) & (1<<30)) + ) mask &= ~DV_II_51_0_bit; + if (mask & DV_II_51_2_bit) + if ( + !(!((W[55]^(W[56]<<5)) & (1<<6))) + || !(!((W[53]^W[55]) & (1<<6))) + || !((W[52]^W[56]) & (1<<1)) + || !((W[46]^W[48]) & (1<<1)) + || !(!((W[39]^(W[43]>>25)) & (1<<5))) + || !((W[38]^(W[43]>>30)) & (1<<0)) + ) mask &= ~DV_II_51_2_bit; + if (mask & DV_II_52_0_bit) + if ( + !(!((W[59]^W[60]) & (1<<29))) + || !(!((W[40]^(W[44]>>25)) & (1<<3))) + || !(!((W[40]^(W[44]>>25)) & (1<<4))) + || !((W[39]^(W[44]<<2)) & (1<<30)) + ) mask &= ~DV_II_52_0_bit; + if (mask & DV_II_53_0_bit) + if ( + !((W[58]^W[61]) & (1<<29)) + || !(!((W[57]^(W[61]>>25)) & (1<<4))) + || !(!((W[41]^(W[45]>>25)) & (1<<3))) + || !(!((W[41]^(W[45]>>25)) & (1<<4))) + ) mask &= ~DV_II_53_0_bit; + if (mask & DV_II_54_0_bit) + if ( + !(!((W[58]^(W[62]>>25)) & (1<<4))) + || !(!((W[42]^(W[46]>>25)) & (1<<3))) + || !(!((W[42]^(W[46]>>25)) & (1<<4))) + ) mask &= ~DV_II_54_0_bit; + if (mask & DV_II_55_0_bit) + if ( + !(!((W[59]^(W[63]>>25)) & (1<<4))) + || !(!((W[57]^(W[59]>>25)) & (1<<4))) + || !(!((W[43]^(W[47]>>25)) & (1<<3))) + || !(!((W[43]^(W[47]>>25)) & (1<<4))) + ) mask &= ~DV_II_55_0_bit; + if (mask & DV_II_56_0_bit) + if ( + !(!((W[60]^(W[64]>>25)) & (1<<4))) + || !(!((W[44]^(W[48]>>25)) & (1<<3))) + || !(!((W[44]^(W[48]>>25)) & (1<<4))) + ) mask &= ~DV_II_56_0_bit; +} + + dvmask[0]=mask; +} + +#ifdef SHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_C +#include SHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_C +#endif diff --git a/src/util/hash/sha1dc/ubc_check.h b/src/util/hash/sha1dc/ubc_check.h new file mode 100644 index 00000000000..d7e17dc734d --- /dev/null +++ b/src/util/hash/sha1dc/ubc_check.h @@ -0,0 +1,52 @@ +/*** +* Copyright 2017 Marc Stevens , Dan Shumow +* Distributed under the MIT Software License. +* See accompanying file LICENSE.txt or copy at +* https://opensource.org/licenses/MIT +***/ + +/* +// this file was generated by the 'parse_bitrel' program in the tools section +// using the data files from directory 'tools/data/3565' +// +// sha1_dvs contains a list of SHA-1 Disturbance Vectors (DV) to check +// dvType, dvK and dvB define the DV: I(K,B) or II(K,B) (see the paper) +// dm[80] is the expanded message block XOR-difference defined by the DV +// testt is the step to do the recompression from for collision detection +// maski and maskb define the bit to check for each DV in the dvmask returned by ubc_check +// +// ubc_check takes as input an expanded message block and verifies the unavoidable bitconditions for all listed DVs +// it returns a dvmask where each bit belonging to a DV is set if all unavoidable bitconditions for that DV have been met +// thus one needs to do the recompression check for each DV that has its bit set +*/ + +#ifndef SHA1DC_UBC_CHECK_H +#define SHA1DC_UBC_CHECK_H + +#if defined(__cplusplus) +extern "C" { +#endif + +#ifndef SHA1DC_NO_STANDARD_INCLUDES +#include +#endif + +#define DVMASKSIZE 1 +typedef struct { int dvType; int dvK; int dvB; int testt; int maski; int maskb; uint32_t dm[80]; } dv_info_t; +extern dv_info_t sha1_dvs[]; +void ubc_check(const uint32_t W[80], uint32_t dvmask[DVMASKSIZE]); + +#define DOSTORESTATE58 +#define DOSTORESTATE65 + +#define CHECK_DVMASK(_DVMASK) (0 != _DVMASK[0]) + +#if defined(__cplusplus) +} +#endif + +#ifdef SHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_H +#include SHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_H +#endif + +#endif diff --git a/src/util/hash/win32.c b/src/util/hash/win32.c new file mode 100644 index 00000000000..f80c0d5ca44 --- /dev/null +++ b/src/util/hash/win32.c @@ -0,0 +1,549 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "win32.h" + +#include "runtime.h" + +#include +#include + +#define GIT_HASH_CNG_DLL_NAME "bcrypt.dll" + +/* BCRYPT_SHA1_ALGORITHM */ +#define GIT_HASH_CNG_SHA1_TYPE L"SHA1" +#define GIT_HASH_CNG_SHA256_TYPE L"SHA256" + +/* BCRYPT_OBJECT_LENGTH */ +#define GIT_HASH_CNG_HASH_OBJECT_LEN L"ObjectLength" + +/* BCRYPT_HASH_REUSEABLE_FLAGS */ +#define GIT_HASH_CNG_HASH_REUSABLE 0x00000020 + +/* Definitions */ + +/* CryptoAPI is available for hashing on Windows XP and newer. */ +struct cryptoapi_provider { + HCRYPTPROV handle; +}; + +/* + * CNG (bcrypt.dll) is significantly more performant than CryptoAPI and is + * preferred, however it is only available on Windows 2008 and newer and + * must therefore be dynamically loaded, and we must inline constants that + * would not exist when building in pre-Windows 2008 environments. + */ + +/* Function declarations for CNG */ +typedef NTSTATUS (WINAPI *cng_open_algorithm_provider_fn)( + HANDLE /* BCRYPT_ALG_HANDLE */ *phAlgorithm, + LPCWSTR pszAlgId, + LPCWSTR pszImplementation, + DWORD dwFlags); + +typedef NTSTATUS (WINAPI *cng_get_property_fn)( + HANDLE /* BCRYPT_HANDLE */ hObject, + LPCWSTR pszProperty, + PUCHAR pbOutput, + ULONG cbOutput, + ULONG *pcbResult, + ULONG dwFlags); + +typedef NTSTATUS (WINAPI *cng_create_hash_fn)( + HANDLE /* BCRYPT_ALG_HANDLE */ hAlgorithm, + HANDLE /* BCRYPT_HASH_HANDLE */ *phHash, + PUCHAR pbHashObject, ULONG cbHashObject, + PUCHAR pbSecret, + ULONG cbSecret, + ULONG dwFlags); + +typedef NTSTATUS (WINAPI *cng_finish_hash_fn)( + HANDLE /* BCRYPT_HASH_HANDLE */ hHash, + PUCHAR pbOutput, + ULONG cbOutput, + ULONG dwFlags); + +typedef NTSTATUS (WINAPI *cng_hash_data_fn)( + HANDLE /* BCRYPT_HASH_HANDLE */ hHash, + PUCHAR pbInput, + ULONG cbInput, + ULONG dwFlags); + +typedef NTSTATUS (WINAPI *cng_destroy_hash_fn)( + HANDLE /* BCRYPT_HASH_HANDLE */ hHash); + +typedef NTSTATUS (WINAPI *cng_close_algorithm_provider_fn)( + HANDLE /* BCRYPT_ALG_HANDLE */ hAlgorithm, + ULONG dwFlags); + +struct cng_provider { + /* DLL for CNG */ + HINSTANCE dll; + + /* Function pointers for CNG */ + cng_open_algorithm_provider_fn open_algorithm_provider; + cng_get_property_fn get_property; + cng_create_hash_fn create_hash; + cng_finish_hash_fn finish_hash; + cng_hash_data_fn hash_data; + cng_destroy_hash_fn destroy_hash; + cng_close_algorithm_provider_fn close_algorithm_provider; + + HANDLE /* BCRYPT_ALG_HANDLE */ sha1_handle; + DWORD sha1_object_size; + + HANDLE /* BCRYPT_ALG_HANDLE */ sha256_handle; + DWORD sha256_object_size; +}; + +typedef struct { + git_hash_win32_provider_t type; + + union { + struct cryptoapi_provider cryptoapi; + struct cng_provider cng; + } provider; +} hash_win32_provider; + +/* Hash provider definition */ + +static hash_win32_provider hash_provider = {0}; + +/* Hash initialization */ + +/* Initialize CNG, if available */ +GIT_INLINE(int) cng_provider_init(void) +{ + char dll_path[MAX_PATH]; + DWORD dll_path_len, size_len; + + /* Only use CNG on Windows 2008 / Vista SP1 or better (Windows 6.0 SP1) */ + if (!git_has_win32_version(6, 0, 1)) { + git_error_set(GIT_ERROR_SHA, "CryptoNG is not supported on this platform"); + return -1; + } + + /* Load bcrypt.dll explicitly from the system directory */ + if ((dll_path_len = GetSystemDirectory(dll_path, MAX_PATH)) == 0 || + dll_path_len > MAX_PATH || + StringCchCat(dll_path, MAX_PATH, "\\") < 0 || + StringCchCat(dll_path, MAX_PATH, GIT_HASH_CNG_DLL_NAME) < 0 || + (hash_provider.provider.cng.dll = LoadLibrary(dll_path)) == NULL) { + git_error_set(GIT_ERROR_SHA, "CryptoNG library could not be loaded"); + return -1; + } + + /* Load the function addresses */ + if ((hash_provider.provider.cng.open_algorithm_provider = (cng_open_algorithm_provider_fn)((void *)GetProcAddress(hash_provider.provider.cng.dll, "BCryptOpenAlgorithmProvider"))) == NULL || + (hash_provider.provider.cng.get_property = (cng_get_property_fn)((void *)GetProcAddress(hash_provider.provider.cng.dll, "BCryptGetProperty"))) == NULL || + (hash_provider.provider.cng.create_hash = (cng_create_hash_fn)((void *)GetProcAddress(hash_provider.provider.cng.dll, "BCryptCreateHash"))) == NULL || + (hash_provider.provider.cng.finish_hash = (cng_finish_hash_fn)((void *)GetProcAddress(hash_provider.provider.cng.dll, "BCryptFinishHash"))) == NULL || + (hash_provider.provider.cng.hash_data = (cng_hash_data_fn)((void *)GetProcAddress(hash_provider.provider.cng.dll, "BCryptHashData"))) == NULL || + (hash_provider.provider.cng.destroy_hash = (cng_destroy_hash_fn)((void *)GetProcAddress(hash_provider.provider.cng.dll, "BCryptDestroyHash"))) == NULL || + (hash_provider.provider.cng.close_algorithm_provider = (cng_close_algorithm_provider_fn)((void *)GetProcAddress(hash_provider.provider.cng.dll, "BCryptCloseAlgorithmProvider"))) == NULL) { + FreeLibrary(hash_provider.provider.cng.dll); + + git_error_set(GIT_ERROR_OS, "CryptoNG functions could not be loaded"); + return -1; + } + + /* Load the SHA1 algorithm */ + if (hash_provider.provider.cng.open_algorithm_provider(&hash_provider.provider.cng.sha1_handle, GIT_HASH_CNG_SHA1_TYPE, NULL, GIT_HASH_CNG_HASH_REUSABLE) < 0 || + hash_provider.provider.cng.get_property(hash_provider.provider.cng.sha1_handle, GIT_HASH_CNG_HASH_OBJECT_LEN, (PBYTE)&hash_provider.provider.cng.sha1_object_size, sizeof(DWORD), &size_len, 0) < 0) { + git_error_set(GIT_ERROR_OS, "algorithm provider could not be initialized"); + goto on_error; + } + + /* Load the SHA256 algorithm */ + if (hash_provider.provider.cng.open_algorithm_provider(&hash_provider.provider.cng.sha256_handle, GIT_HASH_CNG_SHA256_TYPE, NULL, GIT_HASH_CNG_HASH_REUSABLE) < 0 || + hash_provider.provider.cng.get_property(hash_provider.provider.cng.sha256_handle, GIT_HASH_CNG_HASH_OBJECT_LEN, (PBYTE)&hash_provider.provider.cng.sha256_object_size, sizeof(DWORD), &size_len, 0) < 0) { + git_error_set(GIT_ERROR_OS, "algorithm provider could not be initialized"); + goto on_error; + } + + hash_provider.type = GIT_HASH_WIN32_CNG; + return 0; + +on_error: + if (hash_provider.provider.cng.sha1_handle) + hash_provider.provider.cng.close_algorithm_provider(hash_provider.provider.cng.sha1_handle, 0); + + if (hash_provider.provider.cng.sha256_handle) + hash_provider.provider.cng.close_algorithm_provider(hash_provider.provider.cng.sha256_handle, 0); + + if (hash_provider.provider.cng.dll) + FreeLibrary(hash_provider.provider.cng.dll); + + return -1; +} + +GIT_INLINE(void) cng_provider_shutdown(void) +{ + if (hash_provider.type == GIT_HASH_WIN32_INVALID) + return; + + hash_provider.provider.cng.close_algorithm_provider(hash_provider.provider.cng.sha1_handle, 0); + hash_provider.provider.cng.close_algorithm_provider(hash_provider.provider.cng.sha256_handle, 0); + FreeLibrary(hash_provider.provider.cng.dll); + + hash_provider.type = GIT_HASH_WIN32_INVALID; +} + +/* Initialize CryptoAPI */ +GIT_INLINE(int) cryptoapi_provider_init(void) +{ + if (!CryptAcquireContext(&hash_provider.provider.cryptoapi.handle, NULL, 0, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) { + git_error_set(GIT_ERROR_OS, "legacy hash context could not be started"); + return -1; + } + + hash_provider.type = GIT_HASH_WIN32_CRYPTOAPI; + return 0; +} + +GIT_INLINE(void) cryptoapi_provider_shutdown(void) +{ + if (hash_provider.type == GIT_HASH_WIN32_INVALID) + return; + + CryptReleaseContext(hash_provider.provider.cryptoapi.handle, 0); + + hash_provider.type = GIT_HASH_WIN32_INVALID; +} + +static void hash_provider_shutdown(void) +{ + if (hash_provider.type == GIT_HASH_WIN32_CNG) + cng_provider_shutdown(); + else if (hash_provider.type == GIT_HASH_WIN32_CRYPTOAPI) + cryptoapi_provider_shutdown(); +} + +static int hash_provider_init(void) +{ + int error = 0; + + if (hash_provider.type != GIT_HASH_WIN32_INVALID) + return 0; + + if ((error = cng_provider_init()) < 0) + error = cryptoapi_provider_init(); + + if (!error) + error = git_runtime_shutdown_register(hash_provider_shutdown); + + return error; +} + +git_hash_win32_provider_t git_hash_win32_provider(void) +{ + return hash_provider.type; +} + +int git_hash_win32_set_provider(git_hash_win32_provider_t provider) +{ + if (provider == hash_provider.type) + return 0; + + hash_provider_shutdown(); + + if (provider == GIT_HASH_WIN32_CNG) + return cng_provider_init(); + else if (provider == GIT_HASH_WIN32_CRYPTOAPI) + return cryptoapi_provider_init(); + + git_error_set(GIT_ERROR_SHA, "unsupported win32 provider"); + return -1; +} + +/* CryptoAPI: available in Windows XP and newer */ + +GIT_INLINE(int) hash_cryptoapi_init(git_hash_win32_ctx *ctx) +{ + if (ctx->ctx.cryptoapi.valid) + CryptDestroyHash(ctx->ctx.cryptoapi.hash_handle); + + if (!CryptCreateHash(hash_provider.provider.cryptoapi.handle, ctx->algorithm, 0, 0, &ctx->ctx.cryptoapi.hash_handle)) { + ctx->ctx.cryptoapi.valid = 0; + git_error_set(GIT_ERROR_OS, "legacy hash implementation could not be created"); + return -1; + } + + ctx->ctx.cryptoapi.valid = 1; + return 0; +} + +GIT_INLINE(int) hash_cryptoapi_update(git_hash_win32_ctx *ctx, const void *_data, size_t len) +{ + const BYTE *data = (BYTE *)_data; + + GIT_ASSERT(ctx->ctx.cryptoapi.valid); + + while (len > 0) { + DWORD chunk = (len > MAXDWORD) ? MAXDWORD : (DWORD)len; + + if (!CryptHashData(ctx->ctx.cryptoapi.hash_handle, data, chunk, 0)) { + git_error_set(GIT_ERROR_OS, "legacy hash data could not be updated"); + return -1; + } + + data += chunk; + len -= chunk; + } + + return 0; +} + +GIT_INLINE(int) hash_cryptoapi_final(unsigned char *out, git_hash_win32_ctx *ctx) +{ + DWORD len = ctx->algorithm == CALG_SHA_256 ? GIT_HASH_SHA256_SIZE : GIT_HASH_SHA1_SIZE; + int error = 0; + + GIT_ASSERT(ctx->ctx.cryptoapi.valid); + + if (!CryptGetHashParam(ctx->ctx.cryptoapi.hash_handle, HP_HASHVAL, out, &len, 0)) { + git_error_set(GIT_ERROR_OS, "legacy hash data could not be finished"); + error = -1; + } + + CryptDestroyHash(ctx->ctx.cryptoapi.hash_handle); + ctx->ctx.cryptoapi.valid = 0; + + return error; +} + +GIT_INLINE(void) hash_ctx_cryptoapi_cleanup(git_hash_win32_ctx *ctx) +{ + if (ctx->ctx.cryptoapi.valid) + CryptDestroyHash(ctx->ctx.cryptoapi.hash_handle); +} + +GIT_INLINE(int) hash_sha1_cryptoapi_ctx_init_init(git_hash_win32_ctx *ctx) +{ + ctx->algorithm = CALG_SHA1; + return hash_cryptoapi_init(ctx); +} + +GIT_INLINE(int) hash_sha256_cryptoapi_ctx_init(git_hash_win32_ctx *ctx) +{ + ctx->algorithm = CALG_SHA_256; + return hash_cryptoapi_init(ctx); +} + +/* CNG: Available in Windows Server 2008 and newer */ + +GIT_INLINE(int) hash_sha1_cng_ctx_init(git_hash_win32_ctx *ctx) +{ + if ((ctx->ctx.cng.hash_object = git__malloc(hash_provider.provider.cng.sha1_object_size)) == NULL) + return -1; + + if (hash_provider.provider.cng.create_hash(hash_provider.provider.cng.sha1_handle, &ctx->ctx.cng.hash_handle, ctx->ctx.cng.hash_object, hash_provider.provider.cng.sha1_object_size, NULL, 0, 0) < 0) { + git__free(ctx->ctx.cng.hash_object); + + git_error_set(GIT_ERROR_OS, "sha1 implementation could not be created"); + return -1; + } + + ctx->algorithm = CALG_SHA1; + return 0; +} + +GIT_INLINE(int) hash_sha256_cng_ctx_init(git_hash_win32_ctx *ctx) +{ + if ((ctx->ctx.cng.hash_object = git__malloc(hash_provider.provider.cng.sha256_object_size)) == NULL) + return -1; + + if (hash_provider.provider.cng.create_hash(hash_provider.provider.cng.sha256_handle, &ctx->ctx.cng.hash_handle, ctx->ctx.cng.hash_object, hash_provider.provider.cng.sha256_object_size, NULL, 0, 0) < 0) { + git__free(ctx->ctx.cng.hash_object); + + git_error_set(GIT_ERROR_OS, "sha256 implementation could not be created"); + return -1; + } + + ctx->algorithm = CALG_SHA_256; + return 0; +} + +GIT_INLINE(int) hash_cng_init(git_hash_win32_ctx *ctx) +{ + BYTE hash[GIT_HASH_SHA256_SIZE]; + ULONG size = ctx->algorithm == CALG_SHA_256 ? GIT_HASH_SHA256_SIZE : GIT_HASH_SHA1_SIZE; + + if (!ctx->ctx.cng.updated) + return 0; + + /* CNG needs to be finished to restart */ + if (hash_provider.provider.cng.finish_hash(ctx->ctx.cng.hash_handle, hash, size, 0) < 0) { + git_error_set(GIT_ERROR_OS, "hash implementation could not be finished"); + return -1; + } + + ctx->ctx.cng.updated = 0; + + return 0; +} + +GIT_INLINE(int) hash_cng_update(git_hash_win32_ctx *ctx, const void *_data, size_t len) +{ + PBYTE data = (PBYTE)_data; + + while (len > 0) { + ULONG chunk = (len > ULONG_MAX) ? ULONG_MAX : (ULONG)len; + + if (hash_provider.provider.cng.hash_data(ctx->ctx.cng.hash_handle, data, chunk, 0) < 0) { + git_error_set(GIT_ERROR_OS, "hash could not be updated"); + return -1; + } + + data += chunk; + len -= chunk; + } + + return 0; +} + +GIT_INLINE(int) hash_cng_final(unsigned char *out, git_hash_win32_ctx *ctx) +{ + ULONG size = ctx->algorithm == CALG_SHA_256 ? GIT_HASH_SHA256_SIZE : GIT_HASH_SHA1_SIZE; + + if (hash_provider.provider.cng.finish_hash(ctx->ctx.cng.hash_handle, out, size, 0) < 0) { + git_error_set(GIT_ERROR_OS, "hash could not be finished"); + return -1; + } + + ctx->ctx.cng.updated = 0; + + return 0; +} + +GIT_INLINE(void) hash_ctx_cng_cleanup(git_hash_win32_ctx *ctx) +{ + hash_provider.provider.cng.destroy_hash(ctx->ctx.cng.hash_handle); + git__free(ctx->ctx.cng.hash_object); +} + +/* Indirection between CryptoAPI and CNG */ + +GIT_INLINE(int) hash_sha1_win32_ctx_init(git_hash_win32_ctx *ctx) +{ + GIT_ASSERT_ARG(hash_provider.type); + + memset(ctx, 0x0, sizeof(git_hash_win32_ctx)); + return (hash_provider.type == GIT_HASH_WIN32_CNG) ? hash_sha1_cng_ctx_init(ctx) : hash_sha1_cryptoapi_ctx_init_init(ctx); +} + +GIT_INLINE(int) hash_sha256_win32_ctx_init(git_hash_win32_ctx *ctx) +{ + GIT_ASSERT_ARG(hash_provider.type); + + memset(ctx, 0x0, sizeof(git_hash_win32_ctx)); + return (hash_provider.type == GIT_HASH_WIN32_CNG) ? hash_sha256_cng_ctx_init(ctx) : hash_sha256_cryptoapi_ctx_init(ctx); +} + +GIT_INLINE(int) hash_win32_init(git_hash_win32_ctx *ctx) +{ + return (hash_provider.type == GIT_HASH_WIN32_CNG) ? hash_cng_init(ctx) : hash_cryptoapi_init(ctx); +} + +GIT_INLINE(int) hash_win32_update(git_hash_win32_ctx *ctx, const void *data, size_t len) +{ + return (hash_provider.type == GIT_HASH_WIN32_CNG) ? hash_cng_update(ctx, data, len) : hash_cryptoapi_update(ctx, data, len); +} + +GIT_INLINE(int) hash_win32_final(unsigned char *out, git_hash_win32_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + return (hash_provider.type == GIT_HASH_WIN32_CNG) ? hash_cng_final(out, ctx) : hash_cryptoapi_final(out, ctx); +} + +GIT_INLINE(void) hash_win32_cleanup(git_hash_win32_ctx *ctx) +{ + if (hash_provider.type == GIT_HASH_WIN32_CNG) + hash_ctx_cng_cleanup(ctx); + else if(hash_provider.type == GIT_HASH_WIN32_CRYPTOAPI) + hash_ctx_cryptoapi_cleanup(ctx); +} + +#ifdef GIT_SHA1_WIN32 + +int git_hash_sha1_global_init(void) +{ + return hash_provider_init(); +} + +int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + return hash_sha1_win32_ctx_init(&ctx->win32); +} + +int git_hash_sha1_init(git_hash_sha1_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + return hash_win32_init(&ctx->win32); +} + +int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len) +{ + GIT_ASSERT_ARG(ctx); + return hash_win32_update(&ctx->win32, data, len); +} + +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + return hash_win32_final(out, &ctx->win32); +} + +void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx) +{ + if (!ctx) + return; + hash_win32_cleanup(&ctx->win32); +} + +#endif + +#ifdef GIT_SHA256_WIN32 + +int git_hash_sha256_global_init(void) +{ + return hash_provider_init(); +} + +int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + return hash_sha256_win32_ctx_init(&ctx->win32); +} + +int git_hash_sha256_init(git_hash_sha256_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + return hash_win32_init(&ctx->win32); +} + +int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *data, size_t len) +{ + GIT_ASSERT_ARG(ctx); + return hash_win32_update(&ctx->win32, data, len); +} + +int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx) +{ + GIT_ASSERT_ARG(ctx); + return hash_win32_final(out, &ctx->win32); +} + +void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx) +{ + if (!ctx) + return; + hash_win32_cleanup(&ctx->win32); +} + +#endif diff --git a/src/util/hash/win32.h b/src/util/hash/win32.h new file mode 100644 index 00000000000..a9fb87aee79 --- /dev/null +++ b/src/util/hash/win32.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_hash_win32_h__ +#define INCLUDE_hash_win32_h__ + +#include "hash/sha.h" + +#include + +typedef enum { + GIT_HASH_WIN32_INVALID = 0, + GIT_HASH_WIN32_CRYPTOAPI, + GIT_HASH_WIN32_CNG +} git_hash_win32_provider_t; + +struct git_hash_win32_cryptoapi_ctx { + bool valid; + HCRYPTHASH hash_handle; +}; + +struct git_hash_win32_cng_ctx { + bool updated; + HANDLE /* BCRYPT_HASH_HANDLE */ hash_handle; + PBYTE hash_object; +}; + +typedef struct { + ALG_ID algorithm; + + union { + struct git_hash_win32_cryptoapi_ctx cryptoapi; + struct git_hash_win32_cng_ctx cng; + } ctx; +} git_hash_win32_ctx; + +/* + * Gets/sets the current hash provider (cng or cryptoapi). This is only + * for testing purposes. + */ +git_hash_win32_provider_t git_hash_win32_provider(void); +int git_hash_win32_set_provider(git_hash_win32_provider_t provider); + +#ifdef GIT_SHA1_WIN32 +struct git_hash_sha1_ctx { + git_hash_win32_ctx win32; +}; +#endif + +#ifdef GIT_SHA256_WIN32 +struct git_hash_sha256_ctx { + git_hash_win32_ctx win32; +}; +#endif + +#endif diff --git a/src/util/hashmap.h b/src/util/hashmap.h new file mode 100644 index 00000000000..c29844f3892 --- /dev/null +++ b/src/util/hashmap.h @@ -0,0 +1,424 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_hashmap_h__ +#define INCLUDE_hashmap_h__ + +/* + * This is a variation on khash.h from khlib 2013-05-02 (0.2.8) + * + * The MIT License + * + * Copyright (c) 2008, 2009, 2011 by Attractive Chaos + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include + +#define GIT_HASHMAP_INIT {0} +#define GIT_HASHSET_INIT {0} + +#define GIT_HASHMAP_EMPTY +#define GIT_HASHMAP_INLINE GIT_INLINE(GIT_HASHMAP_EMPTY) + +#define GIT_HASHMAP_IS_EMPTY(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2) +#define GIT_HASHMAP_IS_DELETE(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1) +#define GIT_HASHMAP_IS_EITHER(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3) +#define GIT_HASHMAP_SET_EMPTY_FALSE(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1))) +#define GIT_HASHMAP_SET_DELETE_TRUE(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1)) +#define GIT_HASHMAP_SET_DELETE_FALSE(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1))) +#define GIT_HASHMAP_SET_BOTH_FALSE(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1))) + +#define GIT_HASHMAP_FLAGSIZE(m) ((m) < 16? 1 : (m)>>4) +#define GIT_HASHMAP_ROUNDUP(x) (--(x), (x)|=(x)>>1, \ + (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) + +#define GIT_HASHSET_VAL_T void * + +typedef uint32_t git_hashmap_iter_t; +#define GIT_HASHMAP_ITER_INIT 0 + +#define GIT_HASHMAP_STRUCT_MEMBERS(key_t, val_t) \ + uint32_t n_buckets, \ + size, \ + n_occupied, \ + upper_bound; \ + uint32_t *flags; \ + key_t *keys; \ + val_t *vals; + +#define GIT_HASHMAP_STRUCT(name, key_t, val_t) \ + typedef struct { \ + GIT_HASHMAP_STRUCT_MEMBERS(key_t, val_t) \ + } name; +#define GIT_HASHSET_STRUCT(name, key_t) \ + GIT_HASHMAP_STRUCT(name, key_t, void *) + + +#define GIT_HASHMAP__COMMON_PROTOTYPES(name, key_t, val_t) \ + extern uint32_t name##_size(name *h); \ + extern bool name##_contains(name *h, key_t key); \ + extern int name##_remove(name *h, key_t key); \ + extern void name##_clear(name *h); \ + extern void name##_dispose(name *h); + +#define GIT_HASHMAP_PROTOTYPES(name, key_t, val_t) \ + GIT_HASHMAP__COMMON_PROTOTYPES(name, key_t, val_t) \ + extern int name##_get(val_t *out, name *h, key_t key); \ + extern int name##_put(name *h, key_t key, val_t val); \ + extern int name##_iterate(git_hashmap_iter_t *iter, key_t *key, val_t *val, name *h); \ + extern int name##_foreach(name *h, int (*cb)(key_t, val_t)); + +#define GIT_HASHSET_PROTOTYPES(name, key_t) \ + GIT_HASHMAP__COMMON_PROTOTYPES(name, key_t, GIT_HASHSET_VAL_T) \ + extern int name##_add(name *h, key_t key); \ + extern int name##_iterate(git_hashmap_iter_t *iter, key_t *key, name *h); \ + extern int name##_foreach(name *h, int (*cb)(key_t)); \ + + +#define GIT_HASHMAP__COMMON_FUNCTIONS(name, is_map, scope, key_t, val_t, __hash_fn, __equal_fn) \ + GIT_UNUSED_FUNCTION scope uint32_t name##_size(name *h) \ + { \ + return h->size; \ + } \ + GIT_INLINE(int) name##__idx(uint32_t *out, name *h, key_t key) \ + { \ + if (h->n_buckets) { \ + uint32_t k, i, last, mask, step = 0; \ + GIT_ASSERT((h)->flags); \ + mask = h->n_buckets - 1; \ + k = __hash_fn(key); \ + i = k & mask; \ + last = i; \ + while (!GIT_HASHMAP_IS_EMPTY(h->flags, i) && \ + (GIT_HASHMAP_IS_DELETE(h->flags, i) || !__equal_fn(h->keys[i], key))) { \ + i = (i + (++step)) & mask; \ + if (i == last) \ + return GIT_ENOTFOUND; \ + } \ + if (GIT_HASHMAP_IS_EITHER(h->flags, i)) \ + return GIT_ENOTFOUND; \ + *out = i; \ + return 0; \ + } \ + return GIT_ENOTFOUND; \ + } \ + GIT_UNUSED_FUNCTION scope bool name##_contains(name *h, key_t key) \ + { \ + uint32_t idx; \ + return name##__idx(&idx, h, key) == 0; \ + } \ + GIT_INLINE(int) name##__remove_at_idx(name *h, uint32_t idx) \ + { \ + if (idx < h->n_buckets && !GIT_HASHMAP_IS_EITHER(h->flags, idx)) { \ + GIT_HASHMAP_SET_DELETE_TRUE(h->flags, idx); \ + --h->size; \ + return 0; \ + } \ + return GIT_ENOTFOUND; \ + } \ + GIT_UNUSED_FUNCTION scope int name##_remove(name *h, key_t key) \ + { \ + uint32_t idx; \ + int error; \ + if ((error = name##__idx(&idx, h, key)) == 0) \ + error = name##__remove_at_idx(h, idx); \ + return error; \ + } \ + GIT_INLINE(int) name##__resize(name *h, uint32_t new_n_buckets) \ + { \ + /* This function uses 0.25*n_buckets bytes of working \ + * space instead of [sizeof(key_t+val_t)+.25]*n_buckets. \ + */ \ + double git_hashmap__upper_bound = 0.77; \ + uint32_t *new_flags = 0; \ + uint32_t j = 1; \ + { \ + GIT_HASHMAP_ROUNDUP(new_n_buckets); \ + if (new_n_buckets < 4) \ + new_n_buckets = 4; \ + if (h->size >= (uint32_t)(new_n_buckets * git_hashmap__upper_bound + 0.5)) { \ + /* Requested size is too small */ \ + j = 0; \ + } else { \ + /* Shrink or expand; rehash */ \ + new_flags = git__reallocarray(NULL, GIT_HASHMAP_FLAGSIZE(new_n_buckets), sizeof(uint32_t)); \ + if (!new_flags) \ + return -1; \ + memset(new_flags, 0xaa, GIT_HASHMAP_FLAGSIZE(new_n_buckets) * sizeof(uint32_t)); \ + if (h->n_buckets < new_n_buckets) { \ + /* Expand */ \ + key_t *new_keys = git__reallocarray(h->keys, new_n_buckets, sizeof(key_t)); \ + if (!new_keys) { \ + git__free(new_flags); \ + return -1; \ + } \ + h->keys = new_keys; \ + if (is_map) { \ + val_t *new_vals = git__reallocarray(h->vals, new_n_buckets, sizeof(val_t)); \ + if (!new_vals) { \ + git__free(new_flags); \ + return -1; \ + } \ + h->vals = new_vals; \ + } \ + } \ + } \ + } \ + if (j) { \ + /* Rehashing is needed */ \ + for (j = 0; j != h->n_buckets; ++j) { \ + if (GIT_HASHMAP_IS_EITHER(h->flags, j) == 0) { \ + key_t key = h->keys[j]; \ + val_t val; \ + uint32_t new_mask; \ + new_mask = new_n_buckets - 1; \ + if (is_map) \ + val = h->vals[j]; \ + GIT_HASHMAP_SET_DELETE_TRUE(h->flags, j); \ + while (1) { \ + /* Kick-out process; sort of like in Cuckoo hashing */ \ + uint32_t k, i, step = 0; \ + k = __hash_fn(key); \ + i = k & new_mask; \ + while (!GIT_HASHMAP_IS_EMPTY(new_flags, i)) \ + i = (i + (++step)) & new_mask; \ + GIT_HASHMAP_SET_EMPTY_FALSE(new_flags, i); \ + if (i < h->n_buckets && GIT_HASHMAP_IS_EITHER(h->flags, i) == 0) { \ + /* Kick out the existing element */ \ + { \ + key_t tmp = h->keys[i]; \ + h->keys[i] = key; \ + key = tmp; \ + } \ + if (is_map) { \ + val_t tmp = h->vals[i]; \ + h->vals[i] = val; \ + val = tmp; \ + } \ + /* Mark it as deleted in the old hash table */ \ + GIT_HASHMAP_SET_DELETE_TRUE(h->flags, i); \ + } else { \ + /* Write the element and jump out of the loop */ \ + h->keys[i] = key; \ + if (is_map) \ + h->vals[i] = val; \ + break; \ + } \ + } \ + } \ + } \ + if (h->n_buckets > new_n_buckets) { \ + /* Shrink the hash table */ \ + h->keys = git__reallocarray(h->keys, new_n_buckets, sizeof(key_t)); \ + if (is_map) \ + h->vals = git__reallocarray(h->vals, new_n_buckets, sizeof(val_t)); \ + } \ + /* free the working space */ \ + git__free(h->flags); \ + h->flags = new_flags; \ + h->n_buckets = new_n_buckets; \ + h->n_occupied = h->size; \ + h->upper_bound = (uint32_t)(h->n_buckets * git_hashmap__upper_bound + 0.5); \ + } \ + return 0; \ + } \ + GIT_INLINE(int) name##__put_idx(uint32_t *idx, bool *key_exists, name *h, key_t key) \ + { \ + uint32_t x; \ + if (h->n_occupied >= h->upper_bound) { \ + /* Update the hash table */ \ + if (h->n_buckets > (h->size<<1)) { \ + /* Clear "deleted" elements */ \ + if (name##__resize(h, h->n_buckets - 1) < 0) \ + return -1; \ + } else if (name##__resize(h, h->n_buckets + 1) < 0) { \ + return -1; \ + } \ + } \ + GIT_ASSERT((h)->flags); \ + GIT_ASSERT((h)->keys); \ + /* TODO: to implement automatically shrinking; resize() already support shrinking */ \ + { \ + uint32_t k, i, site, last, mask = h->n_buckets - 1, step = 0; \ + x = site = h->n_buckets; \ + k = __hash_fn(key); \ + i = k & mask; \ + if (GIT_HASHMAP_IS_EMPTY(h->flags, i)) { \ + /* for speed up */ \ + x = i; \ + } else { \ + last = i; \ + while (!GIT_HASHMAP_IS_EMPTY(h->flags, i) && (GIT_HASHMAP_IS_DELETE(h->flags, i) || !__equal_fn(h->keys[i], key))) { \ + if (GIT_HASHMAP_IS_DELETE(h->flags, i)) \ + site = i; \ + i = (i + (++step)) & mask; \ + if (i == last) { \ + x = site; \ + break; \ + } \ + } \ + if (x == h->n_buckets) { \ + if (GIT_HASHMAP_IS_EMPTY(h->flags, i) && site != h->n_buckets) \ + x = site; \ + else \ + x = i; \ + } \ + } \ + } \ + if (GIT_HASHMAP_IS_EMPTY(h->flags, x)) { \ + /* not present at all */ \ + h->keys[x] = key; \ + GIT_HASHMAP_SET_BOTH_FALSE(h->flags, x); \ + ++h->size; \ + ++h->n_occupied; \ + *key_exists = 1; \ + } else if (GIT_HASHMAP_IS_DELETE(h->flags, x)) { \ + /* deleted */ \ + h->keys[x] = key; \ + GIT_HASHMAP_SET_BOTH_FALSE(h->flags, x); \ + ++h->size; \ + *key_exists = 1; \ + } else { \ + /* Don't touch h->keys[x] if present and not deleted */ \ + *key_exists = 0; \ + } \ + *idx = x; \ + return 0; \ + } \ + GIT_UNUSED_FUNCTION scope void name##_clear(name *h) \ + { \ + if (h && h->flags) { \ + memset(h->flags, 0xaa, GIT_HASHMAP_FLAGSIZE(h->n_buckets) * sizeof(uint32_t)); \ + h->size = h->n_occupied = 0; \ + } \ + } \ + GIT_UNUSED_FUNCTION scope void name##_dispose(name *h) \ + { \ + git__free(h->flags); \ + git__free(h->keys); \ + git__free(h->vals); \ + memset(h, 0, sizeof(name)); \ + } + +#define GIT_HASHMAP_FUNCTIONS(name, scope, key_t, val_t, __hash_fn, __equal_fn) \ + GIT_HASHMAP__COMMON_FUNCTIONS(name, true, scope, key_t, val_t, __hash_fn, __equal_fn) \ + \ + GIT_UNUSED_FUNCTION scope int name##_get(val_t *out, name *h, key_t key) \ + { \ + uint32_t idx; \ + int error; \ + if ((error = name##__idx(&idx, h, key)) == 0) \ + *out = (h)->vals[idx]; \ + return error; \ + } \ + GIT_UNUSED_FUNCTION scope int name##_put(name *h, key_t key, val_t val) \ + { \ + uint32_t idx; \ + bool key_exists; \ + int error = name##__put_idx(&idx, &key_exists, h, key); \ + if (error) \ + return error; \ + GIT_ASSERT((h)->vals); \ + if (!key_exists) \ + (h)->keys[idx] = key; \ + (h)->vals[idx] = val; \ + return 0; \ + } \ + GIT_UNUSED_FUNCTION scope int name##_iterate(git_hashmap_iter_t *iter, key_t *key, val_t *val, name *h) \ + { \ + for (; *iter < h->n_buckets; (*iter)++) { \ + if (GIT_HASHMAP_IS_EITHER(h->flags, *iter)) \ + continue; \ + if (key) \ + *key = h->keys[*iter]; \ + if (val) \ + *val = h->vals[*iter]; \ + (*iter)++; \ + return 0; \ + } \ + return GIT_ITEROVER; \ + } \ + GIT_UNUSED_FUNCTION scope int name##_foreach(name *h, int (*cb)(key_t, val_t)) \ + { \ + uint32_t idx = 0; \ + key_t key; \ + val_t val; \ + int ret; \ + while ((ret = name##_iterate(&idx, &key, &val, h)) == 0) { \ + if ((ret = cb(key, val)) != 0) \ + return ret; \ + } \ + return ret == GIT_ITEROVER ? 0 : ret; \ + } + +#define GIT_HASHSET_FUNCTIONS(name, scope, key_t, __hash_fn, __equal_fn) \ + GIT_HASHMAP__COMMON_FUNCTIONS(name, false, scope, key_t, void *, __hash_fn, __equal_fn) \ + \ + GIT_UNUSED_FUNCTION scope int name##_add(name *h, key_t key) \ + { \ + uint32_t idx; \ + bool key_exists; \ + int error = name##__put_idx(&idx, &key_exists, h, key); \ + if (error) \ + return error; \ + if (!key_exists) { \ + (h)->keys[idx] = key; \ + } \ + return 0; \ + } \ + GIT_UNUSED_FUNCTION scope int name##_iterate(git_hashmap_iter_t *iter, key_t *key, name *h) \ + { \ + for (; *iter < h->n_buckets; (*iter)++) { \ + if (GIT_HASHMAP_IS_EITHER(h->flags, *iter)) \ + continue; \ + *key = h->keys[*iter]; \ + return 0; \ + } \ + return GIT_ITEROVER; \ + } \ + GIT_UNUSED_FUNCTION scope int name##_foreach(name *h, int (*cb)(key_t)) \ + { \ + git_hashmap_iter_t iter = 0; \ + key_t key; \ + int ret; \ + while ((ret = name##_iterate(&iter, &key, h)) == 0) { \ + if ((ret = cb(key)) != 0) \ + return ret; \ + } \ + return ret == GIT_ITEROVER ? 0 : ret; \ + } + + +#define GIT_HASHSET_SETUP(name, key_t, __hash_fn, __equal_fn) \ + GIT_HASHSET_STRUCT(name, key_t) \ + GIT_HASHSET_FUNCTIONS(name, GIT_HASHMAP_INLINE, key_t, __hash_fn, __equal_fn) +#define GIT_HASHMAP_SETUP(name, key_t, val_t, __hash_fn, __equal_fn) \ + GIT_HASHMAP_STRUCT(name, key_t, val_t) \ + GIT_HASHMAP_FUNCTIONS(name, GIT_HASHMAP_INLINE, key_t, val_t, __hash_fn, __equal_fn) + +#endif diff --git a/src/util/hashmap_str.h b/src/util/hashmap_str.h new file mode 100644 index 00000000000..099aac5a879 --- /dev/null +++ b/src/util/hashmap_str.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_hashmap_str_h__ +#define INCLUDE_hashmap_str_h__ + +#include "hashmap.h" + +GIT_INLINE(uint32_t) git_hashmap_str_hash(const char *s) +{ + uint32_t h = (uint32_t)*s; + + if (h) { + for (++s; *s; ++s) + h = (h << 5) - h + (uint32_t)*s; + } + + return h; +} + +GIT_INLINE(bool) git_hashmap_str_equal(const char *one, const char *two) +{ + return strcmp(one, two) == 0; +} + +#define GIT_HASHMAP_STR_STRUCT(name, val_t) \ + GIT_HASHMAP_STRUCT(name, const char *, val_t) +#define GIT_HASHMAP_STR_PROTOTYPES(name, val_t) \ + GIT_HASHMAP_PROTOTYPES(name, const char *, val_t) +#define GIT_HASHMAP_STR_FUNCTIONS(name, scope, val_t) \ + GIT_HASHMAP_FUNCTIONS(name, scope, const char *, val_t, git_hashmap_str_hash, git_hashmap_str_equal) + +#define GIT_HASHMAP_STR_SETUP(name, val_t) \ + GIT_HASHMAP_STR_STRUCT(name, val_t) \ + GIT_HASHMAP_STR_FUNCTIONS(name, GIT_HASHMAP_INLINE, val_t) + +GIT_HASHSET_SETUP(git_hashset_str, const char *, git_hashmap_str_hash, git_hashmap_str_equal); +GIT_HASHMAP_SETUP(git_hashmap_str, const char *, void *, git_hashmap_str_hash, git_hashmap_str_equal); + +#endif diff --git a/src/util/integer.h b/src/util/integer.h new file mode 100644 index 00000000000..a9e416cc342 --- /dev/null +++ b/src/util/integer.h @@ -0,0 +1,220 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_integer_h__ +#define INCLUDE_integer_h__ + +/** @return true if p fits into the range of a size_t */ +GIT_INLINE(int) git__is_sizet(int64_t p) +{ + size_t r = (size_t)p; + return p == (int64_t)r; +} + +/** @return true if p fits into the range of an ssize_t */ +GIT_INLINE(int) git__is_ssizet(size_t p) +{ + ssize_t r = (ssize_t)p; + return p == (size_t)r; +} + +/** @return true if p fits into the range of a uint16_t */ +GIT_INLINE(int) git__is_uint16(size_t p) +{ + uint16_t r = (uint16_t)p; + return p == (size_t)r; +} + +/** @return true if p fits into the range of a uint32_t */ +GIT_INLINE(int) git__is_uint32(size_t p) +{ + uint32_t r = (uint32_t)p; + return p == (size_t)r; +} + +/** @return true if p fits into the range of an unsigned long */ +GIT_INLINE(int) git__is_ulong(int64_t p) +{ + unsigned long r = (unsigned long)p; + return p == (int64_t)r; +} + +/** @return true if p fits into the range of an int */ +GIT_INLINE(int) git__is_int(int64_t p) +{ + int r = (int)p; + return p == (int64_t)r; +} + +/* Use clang/gcc compiler intrinsics whenever possible */ +#if (__has_builtin(__builtin_add_overflow) || \ + (defined(__GNUC__) && (__GNUC__ >= 5))) + +# if (SIZE_MAX == UINT_MAX) +# define git__add_sizet_overflow(out, one, two) \ + __builtin_uadd_overflow(one, two, out) +# define git__multiply_sizet_overflow(out, one, two) \ + __builtin_umul_overflow(one, two, out) +# elif (SIZE_MAX == ULONG_MAX) +# define git__add_sizet_overflow(out, one, two) \ + __builtin_uaddl_overflow(one, two, out) +# define git__multiply_sizet_overflow(out, one, two) \ + __builtin_umull_overflow(one, two, out) +# elif (SIZE_MAX == ULLONG_MAX) +# define git__add_sizet_overflow(out, one, two) \ + __builtin_uaddll_overflow(one, two, out) +# define git__multiply_sizet_overflow(out, one, two) \ + __builtin_umulll_overflow(one, two, out) +# else +# error compiler has add with overflow intrinsics but SIZE_MAX is unknown +# endif + +# define git__add_int_overflow(out, one, two) \ + __builtin_sadd_overflow(one, two, out) +# define git__sub_int_overflow(out, one, two) \ + __builtin_ssub_overflow(one, two, out) + +# define git__add_int64_overflow(out, one, two) \ + __builtin_add_overflow(one, two, out) + +/* clang on 32-bit systems produces an undefined reference to `__mulodi4`. */ +# if !defined(__clang__) || !defined(GIT_ARCH_32) +# define git__multiply_int64_overflow(out, one, two) \ + __builtin_mul_overflow(one, two, out) +# endif + +/* Use Microsoft's safe integer handling functions where available */ +#elif defined(_MSC_VER) + +# if !defined(ENABLE_INTSAFE_SIGNED_FUNCTIONS) +# define ENABLE_INTSAFE_SIGNED_FUNCTIONS +# endif +# include + +# define git__add_sizet_overflow(out, one, two) \ + (SizeTAdd(one, two, out) != S_OK) +# define git__multiply_sizet_overflow(out, one, two) \ + (SizeTMult(one, two, out) != S_OK) + +#define git__add_int_overflow(out, one, two) \ + (IntAdd(one, two, out) != S_OK) +#define git__sub_int_overflow(out, one, two) \ + (IntSub(one, two, out) != S_OK) + +#define git__add_int64_overflow(out, one, two) \ + (LongLongAdd(one, two, out) != S_OK) +#define git__multiply_int64_overflow(out, one, two) \ + (LongLongMult(one, two, out) != S_OK) + +#else + +/** + * Sets `one + two` into `out`, unless the arithmetic would overflow. + * @return false if the result fits in a `size_t`, true on overflow. + */ +GIT_INLINE(bool) git__add_sizet_overflow(size_t *out, size_t one, size_t two) +{ + if (SIZE_MAX - one < two) + return true; + *out = one + two; + return false; +} + +/** + * Sets `one * two` into `out`, unless the arithmetic would overflow. + * @return false if the result fits in a `size_t`, true on overflow. + */ +GIT_INLINE(bool) git__multiply_sizet_overflow(size_t *out, size_t one, size_t two) +{ + if (one && SIZE_MAX / one < two) + return true; + *out = one * two; + return false; +} + +GIT_INLINE(bool) git__add_int_overflow(int *out, int one, int two) +{ + if ((two > 0 && one > (INT_MAX - two)) || + (two < 0 && one < (INT_MIN - two))) + return true; + *out = one + two; + return false; +} + +GIT_INLINE(bool) git__sub_int_overflow(int *out, int one, int two) +{ + if ((two > 0 && one < (INT_MIN + two)) || + (two < 0 && one > (INT_MAX + two))) + return true; + *out = one - two; + return false; +} + +GIT_INLINE(bool) git__add_int64_overflow(int64_t *out, int64_t one, int64_t two) +{ + if ((two > 0 && one > (INT64_MAX - two)) || + (two < 0 && one < (INT64_MIN - two))) + return true; + *out = one + two; + return false; +} + +#endif + +/* If we could not provide an intrinsic implementation for this, provide a (slow) fallback. */ +#if !defined(git__multiply_int64_overflow) +GIT_INLINE(bool) git__multiply_int64_overflow(int64_t *out, int64_t one, int64_t two) +{ + /* + * Detects whether `INT64_MAX < (one * two) || INT64_MIN > (one * two)`, + * without incurring in undefined behavior. That is done by performing the + * comparison with a division instead of a multiplication, which translates + * to `INT64_MAX / one < two || INT64_MIN / one > two`. Some caveats: + * + * - The comparison sign is inverted when both sides of the inequality are + * multiplied/divided by a negative number, so if `one < 0` the comparison + * needs to be flipped. + * - `INT64_MAX / -1` itself overflows (or traps), so that case should be + * avoided. + * - Since the overflow flag is defined as the discrepance between the result + * of performing the multiplication in a signed integer at twice the width + * of the operands, and the truncated+sign-extended version of that same + * result, there are four cases where the result is the opposite of what + * would be expected: + * * `INT64_MIN * -1` / `-1 * INT64_MIN` + * * `INT64_MIN * 1 / `1 * INT64_MIN` + */ + if (one && two) { + if (one > 0 && two > 0) { + if (INT64_MAX / one < two) + return true; + } else if (one < 0 && two < 0) { + if ((one == -1 && two == INT64_MIN) || + (two == -1 && one == INT64_MIN)) { + *out = INT64_MIN; + return false; + } + if (INT64_MAX / one > two) + return true; + } else if (one > 0 && two < 0) { + if ((one == 1 && two == INT64_MIN) || + (INT64_MIN / one > two)) + return true; + } else if (one == -1) { + if (INT64_MIN / two > one) + return true; + } else { + if ((one == INT64_MIN && two == 1) || + (INT64_MIN / one < two)) + return true; + } + } + *out = one * two; + return false; +} +#endif + +#endif diff --git a/src/map.h b/src/util/map.h similarity index 80% rename from src/map.h rename to src/util/map.h index da3d1e19ad4..c101e46f6a6 100644 --- a/src/map.h +++ b/src/util/map.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_map_h__ #define INCLUDE_map_h__ -#include "common.h" +#include "git2_util.h" /* p_mmap() prot values */ @@ -36,11 +36,11 @@ typedef struct { /* memory mapped buffer */ } git_map; #define GIT_MMAP_VALIDATE(out, len, prot, flags) do { \ - assert(out != NULL && len > 0); \ - assert((prot & GIT_PROT_WRITE) || (prot & GIT_PROT_READ)); \ - assert((flags & GIT_MAP_FIXED) == 0); } while (0) + GIT_ASSERT(out != NULL && len > 0); \ + GIT_ASSERT((prot & GIT_PROT_WRITE) || (prot & GIT_PROT_READ)); \ + GIT_ASSERT((flags & GIT_MAP_FIXED) == 0); } while (0) -extern int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset); +extern int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, off64_t offset); extern int p_munmap(git_map *map); -#endif /* INCLUDE_map_h__ */ +#endif diff --git a/src/util/net.c b/src/util/net.c new file mode 100644 index 00000000000..b55921b4b6e --- /dev/null +++ b/src/util/net.c @@ -0,0 +1,1160 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "net.h" + +#include + +#include "posix.h" +#include "str.h" +#include "runtime.h" + +#define DEFAULT_PORT_HTTP "80" +#define DEFAULT_PORT_HTTPS "443" +#define DEFAULT_PORT_GIT "9418" +#define DEFAULT_PORT_SSH "22" + +#define GIT_NET_URL_PARSER_INIT { 0 } + +typedef struct { + unsigned int hierarchical : 1; + + const char *scheme; + const char *user; + const char *password; + const char *host; + const char *port; + const char *path; + const char *query; + const char *fragment; + + size_t scheme_len; + size_t user_len; + size_t password_len; + size_t host_len; + size_t port_len; + size_t path_len; + size_t query_len; + size_t fragment_len; +} git_net_url_parser; + +bool git_net_hostname_matches_cert( + const char *hostname, + const char *pattern) +{ + for (;;) { + char c = git__tolower(*pattern++); + + if (c == '\0') + return *hostname ? false : true; + + if (c == '*') { + c = *pattern; + + /* '*' at the end matches everything left */ + if (c == '\0') + return true; + + /* + * We've found a pattern, so move towards the + * next matching char. The '.' is handled + * specially because wildcards aren't allowed + * to cross subdomains. + */ + while(*hostname) { + char h = git__tolower(*hostname); + + if (h == c) + return git_net_hostname_matches_cert(hostname++, pattern); + else if (h == '.') + return git_net_hostname_matches_cert(hostname, pattern); + + hostname++; + } + + return false; + } + + if (c != git__tolower(*hostname++)) + return false; + } + + return false; +} + +#define is_valid_scheme_char(c) \ + (((c) >= 'a' && (c) <= 'z') || \ + ((c) >= 'A' && (c) <= 'Z') || \ + ((c) >= '0' && (c) <= '9') || \ + (c) == '+' || (c) == '-' || (c) == '.') + +bool git_net_str_is_url(const char *str) +{ + const char *c; + + for (c = str; *c; c++) { + if (*c == ':' && *(c+1) == '/' && *(c+2) == '/') + return true; + + if (!is_valid_scheme_char(*c)) + break; + } + + return false; +} + +static const char *default_port_for_scheme(const char *scheme) +{ + if (strcmp(scheme, "http") == 0) + return DEFAULT_PORT_HTTP; + else if (strcmp(scheme, "https") == 0) + return DEFAULT_PORT_HTTPS; + else if (strcmp(scheme, "git") == 0) + return DEFAULT_PORT_GIT; + else if (strcmp(scheme, "ssh") == 0 || + strcmp(scheme, "ssh+git") == 0 || + strcmp(scheme, "git+ssh") == 0) + return DEFAULT_PORT_SSH; + + return NULL; +} + +static bool is_ssh_scheme(const char *scheme, size_t scheme_len) +{ + if (!scheme_len) + return false; + + return strncasecmp(scheme, "ssh", scheme_len) == 0 || + strncasecmp(scheme, "ssh+git", scheme_len) == 0 || + strncasecmp(scheme, "git+ssh", scheme_len) == 0; +} + +int git_net_url_dup(git_net_url *out, git_net_url *in) +{ + if (in->scheme) { + out->scheme = git__strdup(in->scheme); + GIT_ERROR_CHECK_ALLOC(out->scheme); + } + + if (in->host) { + out->host = git__strdup(in->host); + GIT_ERROR_CHECK_ALLOC(out->host); + } + + if (in->port) { + out->port = git__strdup(in->port); + GIT_ERROR_CHECK_ALLOC(out->port); + } + + if (in->path) { + out->path = git__strdup(in->path); + GIT_ERROR_CHECK_ALLOC(out->path); + } + + if (in->query) { + out->query = git__strdup(in->query); + GIT_ERROR_CHECK_ALLOC(out->query); + } + + if (in->username) { + out->username = git__strdup(in->username); + GIT_ERROR_CHECK_ALLOC(out->username); + } + + if (in->password) { + out->password = git__strdup(in->password); + GIT_ERROR_CHECK_ALLOC(out->password); + } + + return 0; +} + +static int url_invalid(const char *message) +{ + git_error_set(GIT_ERROR_NET, "invalid url: %s", message); + return GIT_EINVALIDSPEC; +} + +static int url_parse_authority( + git_net_url_parser *parser, + const char *authority, + size_t len) +{ + const char *c, *hostport_end = NULL, *host_end = NULL, + *userpass_end = NULL, *user_end = NULL; + + enum { + HOSTPORT, HOST, IPV6, HOST_END, USERPASS, USER + } state = HOSTPORT; + + if (len == 0) + return 0; + + /* + * walk the authority backwards so that we can parse google code's + * ssh urls that are not rfc compliant and allow @ in the username + */ + for (hostport_end = authority + len, c = hostport_end - 1; + c >= authority && !user_end; + c--) { + switch (state) { + case HOSTPORT: + if (*c == ':') { + parser->port = c + 1; + parser->port_len = hostport_end - parser->port; + host_end = c; + state = HOST; + break; + } + + /* + * if we've only seen digits then we don't know + * if we're parsing just a host or a host and port. + * if we see a non-digit, then we're in a host, + * otherwise, fall through to possibly match the + * "@" (user/host separator). + */ + + if (*c < '0' || *c > '9') { + host_end = hostport_end; + state = HOST; + } + + /* fall through */ + + case HOST: + if (*c == ']' && host_end == c + 1) { + host_end = c; + state = IPV6; + } + + else if (*c == '@') { + parser->host = c + 1; + parser->host_len = host_end ? + host_end - parser->host : + hostport_end - parser->host; + userpass_end = c; + state = USERPASS; + } + + else if (*c == '[' || *c == ']' || *c == ':') { + return url_invalid("malformed hostname"); + } + + break; + + case IPV6: + if (*c == '[') { + parser->host = c + 1; + parser->host_len = host_end - parser->host; + state = HOST_END; + } + + else if ((*c < '0' || *c > '9') && + (*c < 'a' || *c > 'f') && + (*c < 'A' || *c > 'F') && + (*c != ':')) { + return url_invalid("malformed hostname"); + } + + break; + + case HOST_END: + if (*c == '@') { + userpass_end = c; + state = USERPASS; + break; + } + + return url_invalid("malformed hostname"); + + case USERPASS: + if (*c == '@' && + !is_ssh_scheme(parser->scheme, parser->scheme_len)) + return url_invalid("malformed hostname"); + + if (*c == ':') { + parser->password = c + 1; + parser->password_len = userpass_end - parser->password; + user_end = c; + state = USER; + break; + } + + break; + + default: + GIT_ASSERT(!"unhandled state"); + } + } + + switch (state) { + case HOSTPORT: + parser->host = authority; + parser->host_len = (hostport_end - parser->host); + break; + case HOST: + parser->host = authority; + parser->host_len = (host_end - parser->host); + break; + case IPV6: + return url_invalid("malformed hostname"); + case HOST_END: + break; + case USERPASS: + parser->user = authority; + parser->user_len = (userpass_end - parser->user); + break; + case USER: + parser->user = authority; + parser->user_len = (user_end - parser->user); + break; + default: + GIT_ASSERT(!"unhandled state"); + } + + return 0; +} + +static int url_parse_path( + git_net_url_parser *parser, + const char *path, + size_t len) +{ + const char *c, *end; + + enum { PATH, QUERY, FRAGMENT } state = PATH; + + parser->path = path; + end = path + len; + + for (c = path; c < end; c++) { + switch (state) { + case PATH: + switch (*c) { + case '?': + parser->path_len = (c - parser->path); + parser->query = c + 1; + state = QUERY; + break; + case '#': + parser->path_len = (c - parser->path); + parser->fragment = c + 1; + state = FRAGMENT; + break; + } + break; + + case QUERY: + if (*c == '#') { + parser->query_len = (c - parser->query); + parser->fragment = c + 1; + state = FRAGMENT; + } + break; + + case FRAGMENT: + break; + + default: + GIT_ASSERT(!"unhandled state"); + } + } + + switch (state) { + case PATH: + parser->path_len = (c - parser->path); + break; + case QUERY: + parser->query_len = (c - parser->query); + break; + case FRAGMENT: + parser->fragment_len = (c - parser->fragment); + break; + } + + return 0; +} + +static int url_parse_finalize(git_net_url *url, git_net_url_parser *parser) +{ + git_str scheme = GIT_STR_INIT, user = GIT_STR_INIT, + password = GIT_STR_INIT, host = GIT_STR_INIT, + port = GIT_STR_INIT, path = GIT_STR_INIT, + query = GIT_STR_INIT, fragment = GIT_STR_INIT; + const char *default_port; + int port_specified = 0; + int error = 0; + + if (parser->scheme_len) { + if ((error = git_str_put(&scheme, parser->scheme, parser->scheme_len)) < 0) + goto done; + + git__strntolower(scheme.ptr, scheme.size); + } + + if (parser->user_len && + (error = git_str_decode_percent(&user, parser->user, parser->user_len)) < 0) + goto done; + + if (parser->password_len && + (error = git_str_decode_percent(&password, parser->password, parser->password_len)) < 0) + goto done; + + if (parser->host_len && + (error = git_str_decode_percent(&host, parser->host, parser->host_len)) < 0) + goto done; + + if (parser->port_len) { + port_specified = 1; + error = git_str_put(&port, parser->port, parser->port_len); + } else if (parser->scheme_len && + (default_port = default_port_for_scheme(scheme.ptr)) != NULL) { + error = git_str_puts(&port, default_port); + } + + if (error < 0) + goto done; + + if (parser->path_len) + error = git_str_put(&path, parser->path, parser->path_len); + else if (parser->hierarchical) + error = git_str_puts(&path, "/"); + + if (error < 0) + goto done; + + if (parser->query_len && + (error = git_str_decode_percent(&query, parser->query, parser->query_len)) < 0) + goto done; + + if (parser->fragment_len && + (error = git_str_decode_percent(&fragment, parser->fragment, parser->fragment_len)) < 0) + goto done; + + url->scheme = git_str_detach(&scheme); + url->host = git_str_detach(&host); + url->port = git_str_detach(&port); + url->path = git_str_detach(&path); + url->query = git_str_detach(&query); + url->fragment = git_str_detach(&fragment); + url->username = git_str_detach(&user); + url->password = git_str_detach(&password); + url->port_specified = port_specified; + + error = 0; + +done: + git_str_dispose(&scheme); + git_str_dispose(&user); + git_str_dispose(&password); + git_str_dispose(&host); + git_str_dispose(&port); + git_str_dispose(&path); + git_str_dispose(&query); + git_str_dispose(&fragment); + + return error; +} + +int git_net_url_parse(git_net_url *url, const char *given) +{ + git_net_url_parser parser = GIT_NET_URL_PARSER_INIT; + const char *c, *authority = NULL, *path = NULL; + size_t authority_len = 0, path_len = 0; + int error = 0; + + enum { + SCHEME_START, SCHEME, + AUTHORITY_START, AUTHORITY, + PATH_START, PATH + } state = SCHEME_START; + + memset(url, 0, sizeof(git_net_url)); + + for (c = given; *c; c++) { + switch (state) { + case SCHEME_START: + parser.scheme = c; + state = SCHEME; + + /* fall through */ + + case SCHEME: + if (*c == ':') { + parser.scheme_len = (c - parser.scheme); + + if (parser.scheme_len && + *(c+1) == '/' && *(c+2) == '/') { + c += 2; + parser.hierarchical = 1; + state = AUTHORITY_START; + } else { + state = PATH_START; + } + } else if (!is_valid_scheme_char(*c)) { + /* + * an illegal scheme character means that we + * were just given a relative path + */ + path = given; + state = PATH; + break; + } + break; + + case AUTHORITY_START: + authority = c; + state = AUTHORITY; + + /* fall through */ + case AUTHORITY: + if (*c != '/') + break; + + authority_len = (c - authority); + + /* fall through */ + case PATH_START: + path = c; + state = PATH; + break; + + case PATH: + break; + + default: + GIT_ASSERT(!"unhandled state"); + } + } + + switch (state) { + case SCHEME: + /* + * if we never saw a ':' then we were given a relative + * path, not a bare scheme + */ + path = given; + path_len = (c - path); + break; + case AUTHORITY_START: + break; + case AUTHORITY: + authority_len = (c - authority); + break; + case PATH_START: + break; + case PATH: + path_len = (c - path); + break; + default: + GIT_ASSERT(!"unhandled state"); + } + + if (authority_len && + (error = url_parse_authority(&parser, authority, authority_len)) < 0) + goto done; + + if (path_len && + (error = url_parse_path(&parser, path, path_len)) < 0) + goto done; + + error = url_parse_finalize(url, &parser); + +done: + return error; +} + +int git_net_url_parse_http( + git_net_url *url, + const char *given) +{ + git_net_url_parser parser = GIT_NET_URL_PARSER_INIT; + const char *c = NULL, *authority = NULL, *path = NULL; + size_t authority_len = 0, path_len = 0; + int error; + + /* Hopefully this is a proper URL with a scheme. */ + if (git_net_str_is_url(given)) + return git_net_url_parse(url, given); + + memset(url, 0, sizeof(git_net_url)); + + /* Without a scheme, we are in the host (authority) section. */ + for (c = authority = given; *c; c++) { + if (!path && *c == '/') { + authority_len = (c - authority); + path = c; + } + } + + if (path) + path_len = (c - path); + else + authority_len = (c - authority); + + parser.scheme = "http"; + parser.scheme_len = 4; + parser.hierarchical = 1; + + if (authority_len && + (error = url_parse_authority(&parser, authority, authority_len)) < 0) + return error; + + if (path_len && + (error = url_parse_path(&parser, path, path_len)) < 0) + return error; + + return url_parse_finalize(url, &parser); +} + +static int scp_invalid(const char *message) +{ + git_error_set(GIT_ERROR_NET, "invalid scp-style path: %s", message); + return GIT_EINVALIDSPEC; +} + +static bool is_ipv6(const char *str) +{ + const char *c; + size_t colons = 0; + + if (*str++ != '[') + return false; + + for (c = str; *c; c++) { + if (*c == ':') + colons++; + + if (*c == ']') + return (colons > 1); + + if (*c != ':' && + (*c < '0' || *c > '9') && + (*c < 'a' || *c > 'f') && + (*c < 'A' || *c > 'F')) + return false; + } + + return false; +} + +static bool has_at(const char *str) +{ + const char *c; + + for (c = str; *c; c++) { + if (*c == '@') + return true; + + if (*c == ':') + break; + } + + return false; +} + +int git_net_url_parse_scp(git_net_url *url, const char *given) +{ + const char *default_port = default_port_for_scheme("ssh"); + const char *c, *user = NULL, *host = NULL, *port = NULL, *path = NULL; + size_t user_len = 0, host_len = 0, port_len = 0; + unsigned short bracket = 0; + + enum { + NONE, + USER, + HOST_START, HOST, HOST_END, + IPV6, IPV6_END, + PORT_START, PORT, PORT_END, + PATH_START + } state = NONE; + + memset(url, 0, sizeof(git_net_url)); + + for (c = given; *c && !path; c++) { + switch (state) { + case NONE: + switch (*c) { + case '@': + return scp_invalid("unexpected '@'"); + case ':': + return scp_invalid("unexpected ':'"); + case '[': + if (is_ipv6(c)) { + state = IPV6; + host = c; + } else if (bracket++ > 1) { + return scp_invalid("unexpected '['"); + } + break; + default: + if (has_at(c)) { + state = USER; + user = c; + } else { + state = HOST; + host = c; + } + break; + } + break; + + case USER: + if (*c == '@') { + user_len = (c - user); + state = HOST_START; + } + break; + + case HOST_START: + state = (*c == '[') ? IPV6 : HOST; + host = c; + break; + + case HOST: + if (*c == ':') { + host_len = (c - host); + state = bracket ? PORT_START : PATH_START; + } else if (*c == ']') { + if (bracket-- == 0) + return scp_invalid("unexpected ']'"); + + host_len = (c - host); + state = HOST_END; + } + break; + + case HOST_END: + if (*c != ':') + return scp_invalid("unexpected character after hostname"); + state = PATH_START; + break; + + case IPV6: + if (*c == ']') + state = IPV6_END; + break; + + case IPV6_END: + if (*c != ':') + return scp_invalid("unexpected character after ipv6 address"); + + host_len = (c - host); + state = bracket ? PORT_START : PATH_START; + break; + + case PORT_START: + port = c; + state = PORT; + break; + + case PORT: + if (*c == ']') { + if (bracket-- == 0) + return scp_invalid("unexpected ']'"); + + port_len = c - port; + state = PORT_END; + } + break; + + case PORT_END: + if (*c != ':') + return scp_invalid("unexpected character after ipv6 address"); + + state = PATH_START; + break; + + case PATH_START: + path = c; + break; + + default: + GIT_ASSERT(!"unhandled state"); + } + } + + if (!path) + return scp_invalid("path is required"); + + GIT_ERROR_CHECK_ALLOC(url->scheme = git__strdup("ssh")); + + if (user_len) + GIT_ERROR_CHECK_ALLOC(url->username = git__strndup(user, user_len)); + + GIT_ASSERT(host_len); + GIT_ERROR_CHECK_ALLOC(url->host = git__strndup(host, host_len)); + + if (port_len) { + url->port_specified = 1; + GIT_ERROR_CHECK_ALLOC(url->port = git__strndup(port, port_len)); + } else { + GIT_ERROR_CHECK_ALLOC(url->port = git__strdup(default_port)); + } + + GIT_ASSERT(path); + GIT_ERROR_CHECK_ALLOC(url->path = git__strdup(path)); + + return 0; +} + +int git_net_url_parse_standard_or_scp(git_net_url *url, const char *given) +{ + return git_net_str_is_url(given) ? + git_net_url_parse(url, given) : + git_net_url_parse_scp(url, given); +} + +int git_net_url_joinpath( + git_net_url *out, + git_net_url *one, + const char *two) +{ + git_str path = GIT_STR_INIT; + const char *query; + size_t one_len, two_len; + + git_net_url_dispose(out); + + if ((query = strchr(two, '?')) != NULL) { + two_len = query - two; + + if (*(++query) != '\0') { + out->query = git__strdup(query); + GIT_ERROR_CHECK_ALLOC(out->query); + } + } else { + two_len = strlen(two); + } + + /* Strip all trailing `/`s from the first path */ + one_len = one->path ? strlen(one->path) : 0; + while (one_len && one->path[one_len - 1] == '/') + one_len--; + + /* Strip all leading `/`s from the second path */ + while (*two == '/') { + two++; + two_len--; + } + + git_str_put(&path, one->path, one_len); + git_str_putc(&path, '/'); + git_str_put(&path, two, two_len); + + if (git_str_oom(&path)) + return -1; + + out->path = git_str_detach(&path); + + if (one->scheme) { + out->scheme = git__strdup(one->scheme); + GIT_ERROR_CHECK_ALLOC(out->scheme); + } + + if (one->host) { + out->host = git__strdup(one->host); + GIT_ERROR_CHECK_ALLOC(out->host); + } + + if (one->port) { + out->port = git__strdup(one->port); + GIT_ERROR_CHECK_ALLOC(out->port); + } + + if (one->username) { + out->username = git__strdup(one->username); + GIT_ERROR_CHECK_ALLOC(out->username); + } + + if (one->password) { + out->password = git__strdup(one->password); + GIT_ERROR_CHECK_ALLOC(out->password); + } + + return 0; +} + +/* + * Some servers strip the query parameters from the Location header + * when sending a redirect. Others leave it in place. + * Check for both, starting with the stripped case first, + * since it appears to be more common. + */ +static void remove_service_suffix( + git_net_url *url, + const char *service_suffix) +{ + const char *service_query = strchr(service_suffix, '?'); + size_t full_suffix_len = strlen(service_suffix); + size_t suffix_len = service_query ? + (size_t)(service_query - service_suffix) : full_suffix_len; + size_t path_len = strlen(url->path); + ssize_t truncate = -1; + + /* + * Check for a redirect without query parameters, + * like "/newloc/info/refs"' + */ + if (suffix_len && path_len >= suffix_len) { + size_t suffix_offset = path_len - suffix_len; + + if (git__strncmp(url->path + suffix_offset, service_suffix, suffix_len) == 0 && + (!service_query || git__strcmp(url->query, service_query + 1) == 0)) { + truncate = suffix_offset; + } + } + + /* + * If we haven't already found where to truncate to remove the + * suffix, check for a redirect with query parameters, like + * "/newloc/info/refs?service=git-upload-pack" + */ + if (truncate < 0 && git__suffixcmp(url->path, service_suffix) == 0) + truncate = path_len - full_suffix_len; + + /* Ensure we leave a minimum of '/' as the path */ + if (truncate == 0) + truncate++; + + if (truncate > 0) { + url->path[truncate] = '\0'; + + git__free(url->query); + url->query = NULL; + } +} + +int git_net_url_apply_redirect( + git_net_url *url, + const char *redirect_location, + bool allow_offsite, + const char *service_suffix) +{ + git_net_url tmp = GIT_NET_URL_INIT; + int error = 0; + + GIT_ASSERT(url); + GIT_ASSERT(redirect_location); + + if (redirect_location[0] == '/') { + git__free(url->path); + + if ((url->path = git__strdup(redirect_location)) == NULL) { + error = -1; + goto done; + } + } else { + git_net_url *original = url; + + if ((error = git_net_url_parse(&tmp, redirect_location)) < 0) + goto done; + + /* Validate that this is a legal redirection */ + + if (original->scheme && + strcmp(original->scheme, tmp.scheme) != 0 && + strcmp(tmp.scheme, "https") != 0) { + git_error_set(GIT_ERROR_NET, "cannot redirect from '%s' to '%s'", + original->scheme, tmp.scheme); + + error = -1; + goto done; + } + + if (original->host && + !allow_offsite && + git__strcasecmp(original->host, tmp.host) != 0) { + git_error_set(GIT_ERROR_NET, "cannot redirect from '%s' to '%s'", + original->host, tmp.host); + + error = -1; + goto done; + } + + git_net_url_swap(url, &tmp); + } + + /* Remove the service suffix if it was given to us */ + if (service_suffix) + remove_service_suffix(url, service_suffix); + +done: + git_net_url_dispose(&tmp); + return error; +} + +bool git_net_url_valid(git_net_url *url) +{ + return (url->host && url->port && url->path); +} + +bool git_net_url_is_default_port(git_net_url *url) +{ + const char *default_port; + + if (url->scheme && (default_port = default_port_for_scheme(url->scheme)) != NULL) + return (strcmp(url->port, default_port) == 0); + else + return false; +} + +bool git_net_url_is_ipv6(git_net_url *url) +{ + return (strchr(url->host, ':') != NULL); +} + +void git_net_url_swap(git_net_url *a, git_net_url *b) +{ + git_net_url tmp = GIT_NET_URL_INIT; + + memcpy(&tmp, a, sizeof(git_net_url)); + memcpy(a, b, sizeof(git_net_url)); + memcpy(b, &tmp, sizeof(git_net_url)); +} + +int git_net_url_fmt(git_str *buf, git_net_url *url) +{ + GIT_ASSERT_ARG(url); + GIT_ASSERT_ARG(url->scheme); + GIT_ASSERT_ARG(url->host); + + git_str_puts(buf, url->scheme); + git_str_puts(buf, "://"); + + if (url->username) { + git_str_puts(buf, url->username); + + if (url->password) { + git_str_puts(buf, ":"); + git_str_puts(buf, url->password); + } + + git_str_putc(buf, '@'); + } + + git_str_puts(buf, url->host); + + if (url->port && !git_net_url_is_default_port(url)) { + git_str_putc(buf, ':'); + git_str_puts(buf, url->port); + } + + git_str_puts(buf, url->path ? url->path : "/"); + + if (url->query) { + git_str_putc(buf, '?'); + git_str_puts(buf, url->query); + } + + return git_str_oom(buf) ? -1 : 0; +} + +int git_net_url_fmt_path(git_str *buf, git_net_url *url) +{ + git_str_puts(buf, url->path ? url->path : "/"); + + if (url->query) { + git_str_putc(buf, '?'); + git_str_puts(buf, url->query); + } + + return git_str_oom(buf) ? -1 : 0; +} + +static bool matches_pattern( + git_net_url *url, + const char *pattern, + size_t pattern_len) +{ + const char *domain, *port = NULL, *colon; + size_t host_len, domain_len, port_len = 0, wildcard = 0; + + GIT_UNUSED(url); + GIT_UNUSED(pattern); + + if (!pattern_len) + return false; + else if (pattern_len == 1 && pattern[0] == '*') + return true; + else if (pattern_len > 1 && pattern[0] == '*' && pattern[1] == '.') + wildcard = 2; + else if (pattern[0] == '.') + wildcard = 1; + + domain = pattern + wildcard; + domain_len = pattern_len - wildcard; + + if ((colon = memchr(domain, ':', domain_len)) != NULL) { + domain_len = colon - domain; + port = colon + 1; + port_len = pattern_len - wildcard - domain_len - 1; + } + + /* A pattern's port *must* match if it's specified */ + if (port_len && git__strlcmp(url->port, port, port_len) != 0) + return false; + + /* No wildcard? Host must match exactly. */ + if (!wildcard) + return !git__strlcmp(url->host, domain, domain_len); + + /* Wildcard: ensure there's (at least) a suffix match */ + if ((host_len = strlen(url->host)) < domain_len || + memcmp(url->host + (host_len - domain_len), domain, domain_len)) + return false; + + /* The pattern is *.domain and the host is simply domain */ + if (host_len == domain_len) + return true; + + /* The pattern is *.domain and the host is foo.domain */ + return (url->host[host_len - domain_len - 1] == '.'); +} + +bool git_net_url_matches_pattern(git_net_url *url, const char *pattern) +{ + return matches_pattern(url, pattern, strlen(pattern)); +} + +bool git_net_url_matches_pattern_list( + git_net_url *url, + const char *pattern_list) +{ + const char *pattern, *pattern_end, *sep; + + for (pattern = pattern_list; + pattern && *pattern; + pattern = sep ? sep + 1 : NULL) { + sep = strchr(pattern, ','); + pattern_end = sep ? sep : strchr(pattern, '\0'); + + if (matches_pattern(url, pattern, (pattern_end - pattern))) + return true; + } + + return false; +} + +void git_net_url_dispose(git_net_url *url) +{ + if (url->username) + git__memzero(url->username, strlen(url->username)); + + if (url->password) + git__memzero(url->password, strlen(url->password)); + + git__free(url->scheme); url->scheme = NULL; + git__free(url->host); url->host = NULL; + git__free(url->port); url->port = NULL; + git__free(url->path); url->path = NULL; + git__free(url->query); url->query = NULL; + git__free(url->fragment); url->fragment = NULL; + git__free(url->username); url->username = NULL; + git__free(url->password); url->password = NULL; +} diff --git a/src/util/net.h b/src/util/net.h new file mode 100644 index 00000000000..360a55db493 --- /dev/null +++ b/src/util/net.h @@ -0,0 +1,112 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_net_h__ +#define INCLUDE_net_h__ + +#include "git2_util.h" + +/* + * Hostname handling + */ + +/* + * See if a given hostname matches a certificate name pattern, according + * to RFC2818 rules (which specifies HTTP over TLS). Mainly, an asterisk + * matches anything, but is limited to a single url component. + */ +extern bool git_net_hostname_matches_cert( + const char *hostname, + const char *pattern); + +/* + * URL handling + */ + +typedef struct git_net_url { + char *scheme; + char *host; + char *port; + char *path; + char *query; + char *fragment; + char *username; + char *password; + + unsigned int port_specified; +} git_net_url; + +#define GIT_NET_URL_INIT { NULL } + +/** Is a given string a url? */ +extern bool git_net_str_is_url(const char *str); + +/** Duplicate a URL */ +extern int git_net_url_dup(git_net_url *out, git_net_url *in); + +/** Parses a string containing a URL into a structure. */ +extern int git_net_url_parse(git_net_url *url, const char *str); + +/** Parses a string containing an SCP style path into a URL structure. */ +extern int git_net_url_parse_scp(git_net_url *url, const char *str); + +/** + * Parses a string containing a standard URL or an SCP style path into + * a URL structure. + */ +extern int git_net_url_parse_standard_or_scp(git_net_url *url, const char *str); + +/** + * Parses a string containing an HTTP endpoint that may not be a + * well-formed URL. For example, "localhost" or "localhost:port". + */ +extern int git_net_url_parse_http( + git_net_url *url, + const char *str); + +/** Appends a path and/or query string to the given URL */ +extern int git_net_url_joinpath( + git_net_url *out, + git_net_url *in, + const char *path); + +/** Ensures that a URL is minimally valid (contains a host, port and path) */ +extern bool git_net_url_valid(git_net_url *url); + +/** Returns true if the URL is on the default port. */ +extern bool git_net_url_is_default_port(git_net_url *url); + +/** Returns true if the host portion of the URL is an ipv6 address. */ +extern bool git_net_url_is_ipv6(git_net_url *url); + +/* Applies a redirect to the URL with a git-aware service suffix. */ +extern int git_net_url_apply_redirect( + git_net_url *url, + const char *redirect_location, + bool allow_offsite, + const char *service_suffix); + +/** Swaps the contents of one URL for another. */ +extern void git_net_url_swap(git_net_url *a, git_net_url *b); + +/** Places the URL into the given buffer. */ +extern int git_net_url_fmt(git_str *out, git_net_url *url); + +/** Place the path and query string into the given buffer. */ +extern int git_net_url_fmt_path(git_str *buf, git_net_url *url); + +/** Determines if the url matches given pattern or pattern list */ +extern bool git_net_url_matches_pattern( + git_net_url *url, + const char *pattern); +extern bool git_net_url_matches_pattern_list( + git_net_url *url, + const char *pattern_list); + +/** Disposes the contents of the structure. */ +extern void git_net_url_dispose(git_net_url *url); + +#endif diff --git a/src/util/pool.c b/src/util/pool.c new file mode 100644 index 00000000000..afbae452a7b --- /dev/null +++ b/src/util/pool.c @@ -0,0 +1,260 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "pool.h" + +#include "posix.h" +#ifndef GIT_WIN32 +#include +#endif + +struct git_pool_page { + git_pool_page *next; + size_t size; + size_t avail; + GIT_ALIGN(char data[GIT_FLEX_ARRAY], 8); +}; + +static void *pool_alloc_page(git_pool *pool, size_t size); + +#ifndef GIT_DEBUG_POOL + +static size_t system_page_size = 0; + +int git_pool_global_init(void) +{ + if (git__page_size(&system_page_size) < 0) + system_page_size = 4096; + /* allow space for malloc overhead */ + system_page_size -= (2 * sizeof(void *)) + sizeof(git_pool_page); + return 0; +} + +int git_pool_init(git_pool *pool, size_t item_size) +{ + GIT_ASSERT_ARG(pool); + GIT_ASSERT_ARG(item_size >= 1); + + memset(pool, 0, sizeof(git_pool)); + pool->item_size = item_size; + pool->page_size = system_page_size; + + return 0; +} + +void git_pool_clear(git_pool *pool) +{ + git_pool_page *scan, *next; + + for (scan = pool->pages; scan != NULL; scan = next) { + next = scan->next; + git__free(scan); + } + + pool->pages = NULL; +} + +static void *pool_alloc_page(git_pool *pool, size_t size) +{ + git_pool_page *page; + const size_t new_page_size = (size <= pool->page_size) ? pool->page_size : size; + size_t alloc_size; + + if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, new_page_size, sizeof(git_pool_page)) || + !(page = git__malloc(alloc_size))) + return NULL; + + page->size = new_page_size; + page->avail = new_page_size - size; + page->next = pool->pages; + + pool->pages = page; + + return page->data; +} + +static void *pool_alloc(git_pool *pool, size_t size) +{ + git_pool_page *page = pool->pages; + void *ptr = NULL; + + if (!page || page->avail < size) + return pool_alloc_page(pool, size); + + ptr = &page->data[page->size - page->avail]; + page->avail -= size; + + return ptr; +} + +uint32_t git_pool__open_pages(git_pool *pool) +{ + uint32_t ct = 0; + git_pool_page *scan; + for (scan = pool->pages; scan != NULL; scan = scan->next) ct++; + return ct; +} + +bool git_pool__ptr_in_pool(git_pool *pool, void *ptr) +{ + git_pool_page *scan; + for (scan = pool->pages; scan != NULL; scan = scan->next) + if ((void *)scan->data <= ptr && + (void *)(((char *)scan->data) + scan->size) > ptr) + return true; + return false; +} + +#else + +int git_pool_global_init(void) +{ + return 0; +} + +static int git_pool__ptr_cmp(const void * a, const void * b) +{ + if(a > b) { + return 1; + } + if(a < b) { + return -1; + } + else { + return 0; + } +} + +int git_pool_init(git_pool *pool, size_t item_size) +{ + GIT_ASSERT_ARG(pool); + GIT_ASSERT_ARG(item_size >= 1); + + memset(pool, 0, sizeof(git_pool)); + pool->item_size = item_size; + pool->page_size = git_pool__system_page_size(); + git_vector_init(&pool->allocations, 100, git_pool__ptr_cmp); + + return 0; +} + +void git_pool_clear(git_pool *pool) +{ + git_vector_dispose_deep(&pool->allocations); +} + +static void *pool_alloc(git_pool *pool, size_t size) { + void *ptr = NULL; + if((ptr = git__malloc(size)) == NULL) { + return NULL; + } + git_vector_insert_sorted(&pool->allocations, ptr, NULL); + return ptr; +} + +bool git_pool__ptr_in_pool(git_pool *pool, void *ptr) +{ + size_t pos; + return git_vector_bsearch(&pos, &pool->allocations, ptr) != GIT_ENOTFOUND; +} +#endif + +void git_pool_swap(git_pool *a, git_pool *b) +{ + git_pool temp; + + if (a == b) + return; + + memcpy(&temp, a, sizeof(temp)); + memcpy(a, b, sizeof(temp)); + memcpy(b, &temp, sizeof(temp)); +} + +static size_t alloc_size(git_pool *pool, size_t count) +{ + const size_t align = sizeof(void *) - 1; + + if (pool->item_size > 1) { + const size_t item_size = (pool->item_size + align) & ~align; + return item_size * count; + } + + return (count + align) & ~align; +} + +void *git_pool_malloc(git_pool *pool, size_t items) +{ + return pool_alloc(pool, alloc_size(pool, items)); +} + +void *git_pool_mallocz(git_pool *pool, size_t items) +{ + const size_t size = alloc_size(pool, items); + void *ptr = pool_alloc(pool, size); + if (ptr) + memset(ptr, 0x0, size); + return ptr; +} + +char *git_pool_strndup(git_pool *pool, const char *str, size_t n) +{ + char *ptr = NULL; + + GIT_ASSERT_ARG_WITH_RETVAL(pool, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(str, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(pool->item_size == sizeof(char), NULL); + + if (n == SIZE_MAX) + return NULL; + + if ((ptr = git_pool_malloc(pool, (n + 1))) != NULL) { + memcpy(ptr, str, n); + ptr[n] = '\0'; + } + + return ptr; +} + +char *git_pool_strdup(git_pool *pool, const char *str) +{ + GIT_ASSERT_ARG_WITH_RETVAL(pool, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(str, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(pool->item_size == sizeof(char), NULL); + + return git_pool_strndup(pool, str, strlen(str)); +} + +char *git_pool_strdup_safe(git_pool *pool, const char *str) +{ + return str ? git_pool_strdup(pool, str) : NULL; +} + +char *git_pool_strcat(git_pool *pool, const char *a, const char *b) +{ + void *ptr; + size_t len_a, len_b, total; + + GIT_ASSERT_ARG_WITH_RETVAL(pool, NULL); + GIT_ASSERT_ARG_WITH_RETVAL(pool->item_size == sizeof(char), NULL); + + len_a = a ? strlen(a) : 0; + len_b = b ? strlen(b) : 0; + + if (GIT_ADD_SIZET_OVERFLOW(&total, len_a, len_b) || + GIT_ADD_SIZET_OVERFLOW(&total, total, 1)) + return NULL; + + if ((ptr = git_pool_malloc(pool, total)) != NULL) { + if (len_a) + memcpy(ptr, a, len_a); + if (len_b) + memcpy(((char *)ptr) + len_a, b, len_b); + *(((char *)ptr) + len_a + len_b) = '\0'; + } + return ptr; +} diff --git a/src/util/pool.h b/src/util/pool.h new file mode 100644 index 00000000000..75a28c3babc --- /dev/null +++ b/src/util/pool.h @@ -0,0 +1,151 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_pool_h__ +#define INCLUDE_pool_h__ + +#include "git2_util.h" + +#include "vector.h" + +typedef struct git_pool_page git_pool_page; + +#ifndef GIT_DEBUG_POOL +/** + * Chunked allocator. + * + * A `git_pool` can be used when you want to cheaply allocate + * multiple items of the same type and are willing to free them + * all together with a single call. The two most common cases + * are a set of fixed size items (such as lots of OIDs) or a + * bunch of strings. + * + * Internally, a `git_pool` allocates pages of memory and then + * deals out blocks from the trailing unused portion of each page. + * The pages guarantee that the number of actual allocations done + * will be much smaller than the number of items needed. + * + * For examples of how to set up a `git_pool` see `git_pool_init`. + */ +typedef struct { + git_pool_page *pages; /* allocated pages */ + size_t item_size; /* size of single alloc unit in bytes */ + size_t page_size; /* size of page in bytes */ +} git_pool; + +#define GIT_POOL_INIT { NULL, 0, 0 } + +#else + +/** + * Debug chunked allocator. + * + * Acts just like `git_pool` but instead of actually pooling allocations it + * passes them through to `git__malloc`. This makes it possible to easily debug + * systems that use `git_pool` using valgrind. + * + * In order to track allocations during the lifetime of the pool we use a + * `git_vector`. When the pool is deallocated everything in the vector is + * freed. + * + * `API is exactly the same as the standard `git_pool` with one exception. + * Since we aren't allocating pages to hand out in chunks we can't easily + * implement `git_pool__open_pages`. + */ +typedef struct { + git_vector allocations; + size_t item_size; + size_t page_size; +} git_pool; + +#define GIT_POOL_INIT { GIT_VECTOR_INIT, 0, 0 } + +#endif + +/** + * Initialize a pool. + * + * To allocation strings, use like this: + * + * git_pool_init(&string_pool, 1); + * my_string = git_pool_strdup(&string_pool, your_string); + * + * To allocate items of fixed size, use like this: + * + * git_pool_init(&pool, sizeof(item)); + * my_item = git_pool_malloc(&pool, 1); + * + * Of course, you can use this in other ways, but those are the + * two most common patterns. + */ +extern int git_pool_init(git_pool *pool, size_t item_size); + +GIT_INLINE(bool) git_pool_is_initialized(git_pool *pool) +{ + return (pool->item_size > 0); +} + +/** + * Free all items in pool + */ +extern void git_pool_clear(git_pool *pool); + +/** + * Swap two pools with one another + */ +extern void git_pool_swap(git_pool *a, git_pool *b); + +/** + * Allocate space for one or more items from a pool. + */ +extern void *git_pool_malloc(git_pool *pool, size_t items); +extern void *git_pool_mallocz(git_pool *pool, size_t items); + +/** + * Allocate space and duplicate string data into it. + * + * This is allowed only for pools with item_size == sizeof(char) + */ +extern char *git_pool_strndup(git_pool *pool, const char *str, size_t n); + +/** + * Allocate space and duplicate a string into it. + * + * This is allowed only for pools with item_size == sizeof(char) + */ +extern char *git_pool_strdup(git_pool *pool, const char *str); + +/** + * Allocate space and duplicate a string into it, NULL is no error. + * + * This is allowed only for pools with item_size == sizeof(char) + */ +extern char *git_pool_strdup_safe(git_pool *pool, const char *str); + +/** + * Allocate space for the concatenation of two strings. + * + * This is allowed only for pools with item_size == sizeof(char) + */ +extern char *git_pool_strcat(git_pool *pool, const char *a, const char *b); + +/* + * Misc utilities + */ +#ifndef GIT_DEBUG_POOL +extern uint32_t git_pool__open_pages(git_pool *pool); +#endif +extern bool git_pool__ptr_in_pool(git_pool *pool, void *ptr); + +/** + * This function is being called by our global setup routines to + * initialize the system pool size. + * + * @return 0 on success, <0 on failure + */ +extern int git_pool_global_init(void); + +#endif diff --git a/src/util/posix.c b/src/util/posix.c new file mode 100644 index 00000000000..cfc0e0751be --- /dev/null +++ b/src/util/posix.c @@ -0,0 +1,357 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "posix.h" + +#include "fs_path.h" +#include +#include + +size_t p_fsync__cnt = 0; + +#ifndef GIT_WIN32 + +#ifdef NO_ADDRINFO + +int p_getaddrinfo( + const char *host, + const char *port, + struct addrinfo *hints, + struct addrinfo **info) +{ + struct addrinfo *ainfo, *ai; + int p = 0; + + GIT_UNUSED(hints); + + if ((ainfo = git__malloc(sizeof(struct addrinfo))) == NULL) + return -1; + + if ((ainfo->ai_hostent = gethostbyname(host)) == NULL) { + git__free(ainfo); + return -2; + } + + ainfo->ai_servent = getservbyname(port, 0); + + if (ainfo->ai_servent) + ainfo->ai_port = ainfo->ai_servent->s_port; + else + ainfo->ai_port = htons(atol(port)); + + memcpy(&ainfo->ai_addr_in.sin_addr, + ainfo->ai_hostent->h_addr_list[0], + ainfo->ai_hostent->h_length); + + ainfo->ai_protocol = 0; + ainfo->ai_socktype = hints->ai_socktype; + ainfo->ai_family = ainfo->ai_hostent->h_addrtype; + ainfo->ai_addr_in.sin_family = ainfo->ai_family; + ainfo->ai_addr_in.sin_port = ainfo->ai_port; + ainfo->ai_addr = (struct addrinfo *)&ainfo->ai_addr_in; + ainfo->ai_addrlen = sizeof(struct sockaddr_in); + + *info = ainfo; + + if (ainfo->ai_hostent->h_addr_list[1] == NULL) { + ainfo->ai_next = NULL; + return 0; + } + + ai = ainfo; + + for (p = 1; ainfo->ai_hostent->h_addr_list[p] != NULL; p++) { + if (!(ai->ai_next = git__malloc(sizeof(struct addrinfo)))) { + p_freeaddrinfo(ainfo); + return -1; + } + memcpy(ai->ai_next, ainfo, sizeof(struct addrinfo)); + memcpy(&ai->ai_next->ai_addr_in.sin_addr, + ainfo->ai_hostent->h_addr_list[p], + ainfo->ai_hostent->h_length); + ai->ai_next->ai_addr = (struct addrinfo *)&ai->ai_next->ai_addr_in; + ai = ai->ai_next; + } + + ai->ai_next = NULL; + return 0; +} + +void p_freeaddrinfo(struct addrinfo *info) +{ + struct addrinfo *p, *next; + + p = info; + + while(p != NULL) { + next = p->ai_next; + git__free(p); + p = next; + } +} + +const char *p_gai_strerror(int ret) +{ + switch(ret) { + case -1: return "Out of memory"; break; + case -2: return "Address lookup failed"; break; + default: return "Unknown error"; break; + } +} + +#endif /* NO_ADDRINFO */ + +int p_open(const char *path, volatile int flags, ...) +{ + mode_t mode = 0; + + #ifdef GIT_DEBUG_STRICT_OPEN + if (strstr(path, "//") != NULL) { + errno = EACCES; + return -1; + } + #endif + + if (flags & O_CREAT) { + va_list arg_list; + + va_start(arg_list, flags); + mode = (mode_t)va_arg(arg_list, int); + va_end(arg_list); + } + + return open(path, flags | O_BINARY | O_CLOEXEC, mode); +} + +int p_creat(const char *path, mode_t mode) +{ + return open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_CLOEXEC, mode); +} + +int p_getcwd(char *buffer_out, size_t size) +{ + char *cwd_buffer; + + GIT_ASSERT_ARG(buffer_out); + GIT_ASSERT_ARG(size > 0); + + cwd_buffer = getcwd(buffer_out, size); + + if (cwd_buffer == NULL) + return -1; + + git_fs_path_mkposix(buffer_out); + git_fs_path_string_to_dir(buffer_out, size); /* append trailing slash */ + + return 0; +} + +int p_rename(const char *from, const char *to) +{ + if (!link(from, to)) { + p_unlink(from); + return 0; + } + + if (!rename(from, to)) + return 0; + + return -1; +} + +#endif /* GIT_WIN32 */ + +ssize_t p_read(git_file fd, void *buf, size_t cnt) +{ + char *b = buf; + + if (!git__is_ssizet(cnt)) { +#ifdef GIT_WIN32 + SetLastError(ERROR_INVALID_PARAMETER); +#endif + errno = EINVAL; + return -1; + } + + while (cnt) { + ssize_t r; +#ifdef GIT_WIN32 + r = read(fd, b, cnt > INT_MAX ? INT_MAX : (unsigned int)cnt); +#else + r = read(fd, b, cnt); +#endif + if (r < 0) { + if (errno == EINTR || errno == EAGAIN) + continue; + return -1; + } + if (!r) + break; + cnt -= r; + b += r; + } + return (b - (char *)buf); +} + +int p_write(git_file fd, const void *buf, size_t cnt) +{ + const char *b = buf; + + while (cnt) { + ssize_t r; +#ifdef GIT_WIN32 + GIT_ASSERT((size_t)((unsigned int)cnt) == cnt); + r = write(fd, b, (unsigned int)cnt); +#else + r = write(fd, b, cnt); +#endif + if (r < 0) { + if (errno == EINTR || GIT_ISBLOCKED(errno)) + continue; + return -1; + } + if (!r) { + errno = EPIPE; + return -1; + } + cnt -= r; + b += r; + } + return 0; +} + +#ifdef NO_MMAP + +#include "map.h" + +int git__page_size(size_t *page_size) +{ + /* dummy; here we don't need any alignment anyway */ + *page_size = 4096; + return 0; +} + +int git__mmap_alignment(size_t *alignment) +{ + /* dummy; here we don't need any alignment anyway */ + *alignment = 4096; + return 0; +} + + +int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, off64_t offset) +{ + const char *ptr; + size_t remaining_len; + + GIT_MMAP_VALIDATE(out, len, prot, flags); + + /* writes cannot be emulated without handling pagefaults since write happens by + * writing to mapped memory */ + if (prot & GIT_PROT_WRITE) { + git_error_set(GIT_ERROR_OS, "trying to map %s-writeable", + ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED) ? "shared": "private"); + return -1; + } + + if (!git__is_ssizet(len)) { + errno = EINVAL; + return -1; + } + + out->len = 0; + out->data = git__malloc(len); + GIT_ERROR_CHECK_ALLOC(out->data); + + remaining_len = len; + ptr = (const char *)out->data; + while (remaining_len > 0) { + ssize_t nb; + HANDLE_EINTR(nb, p_pread(fd, (void *)ptr, remaining_len, offset)); + if (nb <= 0) { + git_error_set(GIT_ERROR_OS, "mmap emulation failed"); + git__free(out->data); + out->data = NULL; + return -1; + } + + ptr += nb; + offset += nb; + remaining_len -= nb; + } + + out->len = len; + return 0; +} + +int p_munmap(git_map *map) +{ + GIT_ASSERT_ARG(map); + git__free(map->data); + + /* Initializing will help debug use-after-free */ + map->len = 0; + map->data = NULL; + + return 0; +} + +#endif + +#if defined(GIT_IO_POLL) || defined(GIT_IO_WSAPOLL) + +/* Handled by posix.h; this test simplifies the final else */ + +#elif defined(GIT_IO_SELECT) + +int p_poll(struct pollfd *fds, unsigned int nfds, int timeout_ms) +{ + fd_set read_fds, write_fds, except_fds; + struct timeval timeout = { 0, 0 }; + unsigned int i; + int max_fd = -1, ret; + + FD_ZERO(&read_fds); + FD_ZERO(&write_fds); + FD_ZERO(&except_fds); + + for (i = 0; i < nfds; i++) { + if ((fds[i].events & POLLIN)) + FD_SET(fds[i].fd, &read_fds); + + if ((fds[i].events & POLLOUT)) + FD_SET(fds[i].fd, &write_fds); + + if ((fds[i].events & POLLPRI)) + FD_SET(fds[i].fd, &except_fds); + + max_fd = MAX(max_fd, fds[i].fd); + } + + if (timeout_ms > 0) { + timeout.tv_sec = timeout_ms / 1000; + timeout.tv_usec = (timeout_ms % 1000) * 1000; + } + + if ((ret = select(max_fd + 1, &read_fds, &write_fds, &except_fds, + timeout_ms < 0 ? NULL : &timeout)) < 0) + goto done; + + for (i = 0; i < nfds; i++) { + fds[i].revents = 0 | + FD_ISSET(fds[i].fd, &read_fds) ? POLLIN : 0 | + FD_ISSET(fds[i].fd, &write_fds) ? POLLOUT : 0 | + FD_ISSET(fds[i].fd, &except_fds) ? POLLPRI : 0; + } + +done: + return ret; +} + +#else +# error no poll compatible implementation +#endif diff --git a/src/util/posix.h b/src/util/posix.h new file mode 100644 index 00000000000..74707453a6d --- /dev/null +++ b/src/util/posix.h @@ -0,0 +1,220 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_posix_h__ +#define INCLUDE_posix_h__ + +#include "git2_util.h" + +#include +#include +#include + +/* stat: file mode type testing macros */ +#ifndef S_IFGITLINK +#define S_IFGITLINK 0160000 +#define S_ISGITLINK(m) (((m) & S_IFMT) == S_IFGITLINK) +#endif + +#ifndef S_IFLNK +#define S_IFLNK 0120000 +#undef _S_IFLNK +#define _S_IFLNK S_IFLNK +#endif + +#ifndef S_IWUSR +#define S_IWUSR 00200 +#endif + +#ifndef S_IXUSR +#define S_IXUSR 00100 +#endif + +#ifndef S_ISLNK +#define S_ISLNK(m) (((m) & _S_IFMT) == _S_IFLNK) +#endif + +#ifndef S_ISDIR +#define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR) +#endif + +#ifndef S_ISREG +#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG) +#endif + +#ifndef S_ISFIFO +#define S_ISFIFO(m) (((m) & _S_IFMT) == _S_IFIFO) +#endif + +/* if S_ISGID is not defined, then don't try to set it */ +#ifndef S_ISGID +#define S_ISGID 0 +#endif + +#ifndef O_BINARY +#define O_BINARY 0 +#endif +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#endif +#ifndef SOCK_CLOEXEC +#define SOCK_CLOEXEC 0 +#endif + +/* access() mode parameter #defines */ +#ifndef F_OK +#define F_OK 0 /* existence check */ +#endif +#ifndef W_OK +#define W_OK 2 /* write mode check */ +#endif +#ifndef R_OK +#define R_OK 4 /* read mode check */ +#endif + +/* Determine whether an errno value indicates that a read or write failed + * because the descriptor is blocked. + */ +#if defined(EWOULDBLOCK) +#define GIT_ISBLOCKED(e) ((e) == EAGAIN || (e) == EWOULDBLOCK) +#else +#define GIT_ISBLOCKED(e) ((e) == EAGAIN) +#endif + +/* define some standard errnos that the runtime may be missing. for example, + * mingw lacks EAFNOSUPPORT. */ +#ifndef EAFNOSUPPORT +#define EAFNOSUPPORT (INT_MAX-1) +#endif + +/* Compiler independent macro to handle signal interrpted system calls */ +#define HANDLE_EINTR(result, x) do { \ + result = (x); \ + } while (result == -1 && errno == EINTR); + + +/* Provide a 64-bit size for offsets. */ + +#if defined(_MSC_VER) +typedef __int64 off64_t; +#elif defined(__HAIKU__) +typedef __haiku_std_int64 off64_t; +#elif defined(__APPLE__) +typedef __int64_t off64_t; +#elif defined(_AIX) +typedef long long off64_t; +#else +typedef int64_t off64_t; +#endif + +typedef int git_file; + +/** + * Standard POSIX Methods + * + * All the methods starting with the `p_` prefix are + * direct ports of the standard POSIX methods. + * + * Some of the methods are slightly wrapped to provide + * saner defaults. Some of these methods are emulated + * in Windows platforms. + * + * Use your manpages to check the docs on these. + */ + +extern ssize_t p_read(git_file fd, void *buf, size_t cnt); +extern int p_write(git_file fd, const void *buf, size_t cnt); + +extern ssize_t p_pread(int fd, void *data, size_t size, off64_t offset); +extern ssize_t p_pwrite(int fd, const void *data, size_t size, off64_t offset); + +#define p_close(fd) close(fd) +#define p_umask(m) umask(m) + +extern int p_open(const char *path, int flags, ...); +extern int p_creat(const char *path, mode_t mode); +extern int p_getcwd(char *buffer_out, size_t size); +extern int p_rename(const char *from, const char *to); + +extern int git__page_size(size_t *page_size); +extern int git__mmap_alignment(size_t *page_size); + +/* The number of times `p_fsync` has been called. Note that this is for + * test code only; it it not necessarily thread-safe and should not be + * relied upon in production. + */ +extern size_t p_fsync__cnt; + +/** + * Platform-dependent methods + */ +#ifdef GIT_WIN32 +# include "win32/posix.h" +#else +# include "unix/posix.h" +#endif + +#include "strnlen.h" + +#ifdef NO_READDIR_R +GIT_INLINE(int) p_readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) +{ + GIT_UNUSED(entry); + *result = readdir(dirp); + return 0; +} +#else /* NO_READDIR_R */ +# define p_readdir_r(d,e,r) readdir_r(d,e,r) +#endif + +#ifdef NO_ADDRINFO +# include +struct addrinfo { + struct hostent *ai_hostent; + struct servent *ai_servent; + struct sockaddr_in ai_addr_in; + struct sockaddr *ai_addr; + size_t ai_addrlen; + int ai_family; + int ai_socktype; + int ai_protocol; + long ai_port; + struct addrinfo *ai_next; +}; + +extern int p_getaddrinfo(const char *host, const char *port, + struct addrinfo *hints, struct addrinfo **info); +extern void p_freeaddrinfo(struct addrinfo *info); +extern const char *p_gai_strerror(int ret); +#else +# define p_getaddrinfo(a, b, c, d) getaddrinfo(a, b, c, d) +# define p_freeaddrinfo(a) freeaddrinfo(a) +# define p_gai_strerror(c) gai_strerror(c) +#endif /* NO_ADDRINFO */ + +#ifdef GIT_IO_POLL +# include +# define p_poll poll +#elif GIT_IO_WSAPOLL +# include +# define p_poll WSAPoll +#else +# define POLLIN 0x01 +# define POLLPRI 0x02 +# define POLLOUT 0x04 +# define POLLERR 0x08 +# define POLLHUP 0x10 + +struct pollfd { + int fd; + short events; + short revents; +}; + +extern int p_poll(struct pollfd *fds, unsigned int nfds, int timeout); +#endif + +#endif diff --git a/src/util/pqueue.c b/src/util/pqueue.c new file mode 100644 index 00000000000..3820e999ca7 --- /dev/null +++ b/src/util/pqueue.c @@ -0,0 +1,125 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "pqueue.h" + +#include "util.h" + +#define PQUEUE_LCHILD_OF(I) (((I)<<1)+1) +#define PQUEUE_RCHILD_OF(I) (((I)<<1)+2) +#define PQUEUE_PARENT_OF(I) (((I)-1)>>1) + +int git_pqueue_init( + git_pqueue *pq, + uint32_t flags, + size_t init_size, + git_vector_cmp cmp) +{ + int error = git_vector_init(pq, init_size, cmp); + + if (!error) { + /* mix in our flags */ + pq->flags |= flags; + + /* if fixed size heap, pretend vector is exactly init_size elements */ + if ((flags & GIT_PQUEUE_FIXED_SIZE) && init_size > 0) + pq->_alloc_size = init_size; + } + + return error; +} + +static void pqueue_up(git_pqueue *pq, size_t el) +{ + size_t parent_el = PQUEUE_PARENT_OF(el); + void *kid = git_vector_get(pq, el); + + while (el > 0) { + void *parent = pq->contents[parent_el]; + + if (pq->_cmp(parent, kid) <= 0) + break; + + pq->contents[el] = parent; + + el = parent_el; + parent_el = PQUEUE_PARENT_OF(el); + } + + pq->contents[el] = kid; +} + +static void pqueue_down(git_pqueue *pq, size_t el) +{ + void *parent = git_vector_get(pq, el), *kid, *rkid; + + while (1) { + size_t kid_el = PQUEUE_LCHILD_OF(el); + + if ((kid = git_vector_get(pq, kid_el)) == NULL) + break; + + if ((rkid = git_vector_get(pq, kid_el + 1)) != NULL && + pq->_cmp(kid, rkid) > 0) { + kid = rkid; + kid_el += 1; + } + + if (pq->_cmp(parent, kid) <= 0) + break; + + pq->contents[el] = kid; + el = kid_el; + } + + pq->contents[el] = parent; +} + +int git_pqueue_insert(git_pqueue *pq, void *item) +{ + int error = 0; + + /* if heap is full, pop the top element if new one should replace it */ + if ((pq->flags & GIT_PQUEUE_FIXED_SIZE) != 0 && + pq->length >= pq->_alloc_size) + { + /* skip this item if below min item in heap or if + * we do not have a comparison function */ + if (!pq->_cmp || pq->_cmp(item, git_vector_get(pq, 0)) <= 0) + return 0; + /* otherwise remove the min item before inserting new */ + (void)git_pqueue_pop(pq); + } + + if (!(error = git_vector_insert(pq, item)) && pq->_cmp) + pqueue_up(pq, pq->length - 1); + + return error; +} + +void *git_pqueue_pop(git_pqueue *pq) +{ + void *rval; + + if (!pq->_cmp) { + rval = git_vector_last(pq); + } else { + rval = git_pqueue_get(pq, 0); + } + + if (git_pqueue_size(pq) > 1 && pq->_cmp) { + /* move last item to top of heap, shrink, and push item down */ + pq->contents[0] = git_vector_last(pq); + git_vector_pop(pq); + pqueue_down(pq, 0); + } else { + /* all we need to do is shrink the heap in this case */ + git_vector_pop(pq); + } + + return rval; +} diff --git a/src/util/pqueue.h b/src/util/pqueue.h new file mode 100644 index 00000000000..a8ef018454e --- /dev/null +++ b/src/util/pqueue.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_pqueue_h__ +#define INCLUDE_pqueue_h__ + +#include "git2_util.h" + +#include "vector.h" + +typedef git_vector git_pqueue; + +enum { + /* flag meaning: don't grow heap, keep highest values only */ + GIT_PQUEUE_FIXED_SIZE = (GIT_VECTOR_FLAG_MAX << 1) +}; + +/** + * Initialize priority queue + * + * @param pq The priority queue struct to initialize + * @param flags Flags (see above) to control queue behavior + * @param init_size The initial queue size + * @param cmp The entry priority comparison function + * @return 0 on success, <0 on error + */ +extern int git_pqueue_init( + git_pqueue *pq, + uint32_t flags, + size_t init_size, + git_vector_cmp cmp); + +#define git_pqueue_free git_vector_dispose +#define git_pqueue_clear git_vector_clear +#define git_pqueue_size git_vector_length +#define git_pqueue_get git_vector_get +#define git_pqueue_reverse git_vector_reverse + +/** + * Insert a new item into the queue + * + * @param pq The priority queue + * @param item Pointer to the item data + * @return 0 on success, <0 on failure + */ +extern int git_pqueue_insert(git_pqueue *pq, void *item); + +/** + * Remove the top item in the priority queue + * + * @param pq The priority queue + * @return item from heap on success, NULL if queue is empty + */ +extern void *git_pqueue_pop(git_pqueue *pq); + +#endif diff --git a/src/util/process.h b/src/util/process.h new file mode 100644 index 00000000000..a3804ec7df8 --- /dev/null +++ b/src/util/process.h @@ -0,0 +1,223 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_process_h__ +#define INCLUDE_process_h__ + +typedef struct git_process git_process; + +typedef struct { + unsigned int use_shell : 1, + capture_in : 1, + capture_out : 1, + capture_err : 1, + exclude_env : 1; + + char *cwd; +} git_process_options; + +typedef enum { + GIT_PROCESS_STATUS_NONE, + GIT_PROCESS_STATUS_NORMAL, + GIT_PROCESS_STATUS_ERROR +} git_process_result_status; + +#define GIT_PROCESS_RESULT_INIT { GIT_PROCESS_STATUS_NONE } + +typedef struct { + git_process_result_status status; + int exitcode; + int signal; +} git_process_result; + +#define GIT_PROCESS_OPTIONS_INIT { 0 } + +#ifdef GIT_WIN32 +# define p_pid_t DWORD +#else +# define p_pid_t pid_t +#endif + +/** + * Create a new process. The command to run should be specified as the + * element of the `arg` array, execv-style. This should be the full path + * to the command to run, the PATH is not obeyed. + * + * This function will add the given environment variables (in `env`) + * to the current environment. Operations on environment variables + * are not thread safe, so you may not modify the environment during + * this call. You can avoid this by setting `exclude_env` in the + * options and providing the entire environment yourself. + * + * @param out location to store the process + * @param args the command (with arguments) to run + * @param args_len the length of the args array + * @param env environment variables to add (or NULL) + * @param env_len the length of the env len + * @param opts the options for creating the process + * @return 0 or an error code + */ +extern int git_process_new( + git_process **out, + const char **args, + size_t args_len, + const char **env, + size_t env_len, + git_process_options *opts); + +/** + * Create a new process. The command to run should be specified as the + * `cmdline` option - which is the full text of the command line as it + * would be specified or run by a user. The command to run will be + * looked up in the PATH. + * + * On Unix, this will be executed by the system's shell (`/bin/sh`) + * and may contain _Bourne-style_ shell quoting rules. On Windows, + * this will be passed to `CreateProcess`, and similarly, may + * contain _Windows-style_ shell quoting rules. + * + * This function will add the given environment variables (in `env`) + * to the current environment. Operations on environment variables + * are not thread safe, so you may not modify the environment during + * this call. You can avoid this by setting `exclude_env` in the + * options and providing the entire environment yourself. + */ +extern int git_process_new_from_cmdline( + git_process **out, + const char *cmdline, + const char **env, + size_t env_len, + git_process_options *opts); + +#ifdef GIT_WIN32 + +extern int git_process__appname( + git_str *out, + const char *cmdline); + +/* Windows path parsing is tricky; this helper function is for testing. */ +extern int git_process__cmdline( + git_str *out, + const char **in, + size_t in_len); + +#endif + +/* + * Whether the given string looks like a command line option (starts + * with a dash). This is useful for examining strings that will become + * cmdline arguments to ensure that they are not erroneously treated + * as an option. For example, arguments to `ssh`. + */ +GIT_INLINE(bool) git_process__is_cmdline_option(const char *str) +{ + return (str && str[0] == '-'); +} + +/** + * Start the process. + * + * @param process the process to start + * @return 0 or an error code + */ +extern int git_process_start(git_process *process); + +/** + * Returns the process id of the process. + * + * @param out pointer to a pid_t to store the process id + * @param process the process to query + * @return 0 or an error code + */ +extern int git_process_id(p_pid_t *out, git_process *process); + +/** + * Read from the process's stdout. The process must have been created with + * `capture_out` set to true. + * + * @param process the process to read from + * @param buf the buf to read into + * @param count maximum number of bytes to read + * @return number of bytes read or an error code + */ +extern ssize_t git_process_read(git_process *process, void *buf, size_t count); + +/** + * Read from the process's stderr. The process must have been created with + * `capture_err` set to true. + * + * @param process the process to read from + * @param buf the buf to read into + * @param count maximum number of bytes to read + * @return number of bytes read or an error code + */ +extern ssize_t git_process_read_err(git_process *process, void *buf, size_t count); + +/** + * Write to the process's stdin. The process must have been created with + * `capture_in` set to true. + * + * @param process the process to write to + * @param buf the buf to write + * @param count maximum number of bytes to write + * @return number of bytes written or an error code + */ +extern ssize_t git_process_write(git_process *process, const void *buf, size_t count); + +/** + * Wait for the process to finish. + * + * @param result the result of the process or NULL + * @param process the process to wait on + */ +extern int git_process_wait(git_process_result *result, git_process *process); + +/** + * Close the input pipe from the child. + * + * @param process the process to close the pipe on + */ +extern int git_process_close_in(git_process *process); + +/** + * Close the output pipe from the child. + * + * @param process the process to close the pipe on + */ +extern int git_process_close_out(git_process *process); + +/** + * Close the error pipe from the child. + * + * @param process the process to close the pipe on + */ +extern int git_process_close_err(git_process *process); + +/** + * Close all resources that are used by the process. This does not + * wait for the process to complete. + * + * @parma process the process to close + */ +extern int git_process_close(git_process *process); + +/** + * Place a human-readable error message in the given git buffer. + * + * @param msg the buffer to store the message + * @param result the process result that produced an error + */ +extern int git_process_result_msg(git_str *msg, git_process_result *result); + +/** + * Free a process structure + * + * @param process the process to free + */ +extern void git_process_free(git_process *process); + +#endif diff --git a/src/util/rand.c b/src/util/rand.c new file mode 100644 index 00000000000..2b137a57b56 --- /dev/null +++ b/src/util/rand.c @@ -0,0 +1,230 @@ +/* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ + +#include "git2_util.h" +#include "rand.h" +#include "runtime.h" + +#if defined(GIT_WIN32) +# include +#endif + +static uint64_t state[4]; +static git_mutex state_lock; + +typedef union { + double f; + uint64_t d; +} bits; + +#if defined(GIT_WIN32) +GIT_INLINE(int) getseed(uint64_t *seed) +{ + HCRYPTPROV provider; + SYSTEMTIME systemtime; + FILETIME filetime, idletime, kerneltime, usertime; + + if (CryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT|CRYPT_SILENT)) { + BOOL success = CryptGenRandom(provider, sizeof(uint64_t), (void *)seed); + CryptReleaseContext(provider, 0); + + if (success) + return 0; + } + + GetSystemTime(&systemtime); + if (!SystemTimeToFileTime(&systemtime, &filetime)) { + git_error_set(GIT_ERROR_OS, "could not get time for random seed"); + return -1; + } + + /* Fall-through: generate a seed from the time and system state */ + *seed = 0; + *seed |= ((uint64_t)filetime.dwLowDateTime << 32); + *seed |= ((uint64_t)filetime.dwHighDateTime); + + GetSystemTimes(&idletime, &kerneltime, &usertime); + + *seed ^= ((uint64_t)idletime.dwLowDateTime << 32); + *seed ^= ((uint64_t)kerneltime.dwLowDateTime); + *seed ^= ((uint64_t)usertime.dwLowDateTime << 32); + + *seed ^= ((uint64_t)idletime.dwHighDateTime); + *seed ^= ((uint64_t)kerneltime.dwHighDateTime << 12); + *seed ^= ((uint64_t)usertime.dwHighDateTime << 24); + + *seed ^= ((uint64_t)GetCurrentProcessId() << 32); + *seed ^= ((uint64_t)GetCurrentThreadId() << 48); + + *seed ^= git_time_monotonic(); + + /* Mix in the addresses of some functions and variables */ + *seed ^= (((uint64_t)((uintptr_t)seed) << 32)); + *seed ^= (((uint64_t)((uintptr_t)&errno))); + + return 0; +} + +#else + +GIT_INLINE(int) getseed(uint64_t *seed) +{ + struct timeval tv; + int fd; + +# if defined(GIT_RAND_GETLOADAVG) + double loadavg[3]; + bits convert; +# endif + +# if defined(GIT_RAND_GETENTROPY) + GIT_UNUSED((fd = 0)); + + if (getentropy(seed, sizeof(uint64_t)) == 0) + return 0; +# else + /* + * Try to read from /dev/urandom; most modern systems will have + * this, but we may be chrooted, etc, so it's not a fatal error + */ + if ((fd = open("/dev/urandom", O_RDONLY)) >= 0) { + ssize_t ret = read(fd, seed, sizeof(uint64_t)); + close(fd); + + if (ret == (ssize_t)sizeof(uint64_t)) + return 0; + } +# endif + + /* Fall-through: generate a seed from the time and system state */ + if (gettimeofday(&tv, NULL) < 0) { + git_error_set(GIT_ERROR_OS, "could get time for random seed"); + return -1; + } + + *seed = 0; + *seed |= ((uint64_t)tv.tv_usec << 40); + *seed |= ((uint64_t)tv.tv_sec); + + *seed ^= ((uint64_t)getpid() << 48); + *seed ^= ((uint64_t)getppid() << 32); + *seed ^= ((uint64_t)getpgid(0) << 28); + *seed ^= ((uint64_t)getsid(0) << 16); + *seed ^= ((uint64_t)getuid() << 8); + *seed ^= ((uint64_t)getgid()); + +# if defined(GIT_RAND_GETLOADAVG) + getloadavg(loadavg, 3); + + convert.f = loadavg[0]; *seed ^= (convert.d >> 36); + convert.f = loadavg[1]; *seed ^= (convert.d); + convert.f = loadavg[2]; *seed ^= (convert.d >> 16); +# endif + + *seed ^= git_time_monotonic(); + + /* Mix in the addresses of some variables */ + *seed ^= ((uint64_t)((size_t)((void *)seed)) << 32); + *seed ^= ((uint64_t)((size_t)((void *)&errno))); + + return 0; +} +#endif + +static void git_rand_global_shutdown(void) +{ + git_mutex_free(&state_lock); +} + +int git_rand_global_init(void) +{ + uint64_t seed = 0; + + if (git_mutex_init(&state_lock) < 0 || getseed(&seed) < 0) + return -1; + + if (!seed) { + git_error_set(GIT_ERROR_INTERNAL, "failed to generate random seed"); + return -1; + } + + git_rand_seed(seed); + git_runtime_shutdown_register(git_rand_global_shutdown); + + return 0; +} + +/* + * This is splitmix64. xoroshiro256** uses 256 bit seed; this is used + * to generate 256 bits of seed from the given 64, per the author's + * recommendation. + */ +GIT_INLINE(uint64_t) splitmix64(uint64_t *in) +{ + uint64_t z; + + *in += 0x9e3779b97f4a7c15; + + z = *in; + z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; + z = (z ^ (z >> 27)) * 0x94d049bb133111eb; + return z ^ (z >> 31); +} + +void git_rand_seed(uint64_t seed) +{ + uint64_t mixer; + + mixer = seed; + + git_mutex_lock(&state_lock); + state[0] = splitmix64(&mixer); + state[1] = splitmix64(&mixer); + state[2] = splitmix64(&mixer); + state[3] = splitmix64(&mixer); + git_mutex_unlock(&state_lock); +} + +/* This is xoshiro256** 1.0, one of our all-purpose, rock-solid + generators. It has excellent (sub-ns) speed, a state (256 bits) that is + large enough for any parallel application, and it passes all tests we + are aware of. + + For generating just floating-point numbers, xoshiro256+ is even faster. + + The state must be seeded so that it is not everywhere zero. If you have + a 64-bit seed, we suggest to seed a splitmix64 generator and use its + output to fill s. */ + +GIT_INLINE(uint64_t) rotl(const uint64_t x, int k) { + return (x << k) | (x >> (64 - k)); +} + +uint64_t git_rand_next(void) { + uint64_t t, result; + + git_mutex_lock(&state_lock); + + result = rotl(state[1] * 5, 7) * 9; + + t = state[1] << 17; + + state[2] ^= state[0]; + state[3] ^= state[1]; + state[1] ^= state[2]; + state[0] ^= state[3]; + + state[2] ^= t; + + state[3] = rotl(state[3], 45); + + git_mutex_unlock(&state_lock); + + return result; +} diff --git a/src/util/rand.h b/src/util/rand.h new file mode 100644 index 00000000000..fa0619aa27d --- /dev/null +++ b/src/util/rand.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_rand_h__ +#define INCLUDE_rand_h__ + +#include "git2_util.h" + +/** + * Initialize the random number generation subsystem. This will + * seed the random number generator with the system's entropy pool, + * if available, and will fall back to the current time and + * system information if not. + */ +int git_rand_global_init(void); + +/** + * Seed the pseudo-random number generator. This is not needed to be + * called; the PRNG is seeded by `git_rand_global_init`, but it may + * be useful for testing. When the same seed is specified, the same + * sequence of random numbers from `git_rand_next` is emitted. + * + * @param seed the seed to use + */ +void git_rand_seed(uint64_t seed); + +/** + * Get the next pseudo-random number in the sequence. + * + * @return a 64-bit pseudo-random number + */ +uint64_t git_rand_next(void); + +#endif diff --git a/src/util/regexp.c b/src/util/regexp.c new file mode 100644 index 00000000000..eb45822474d --- /dev/null +++ b/src/util/regexp.c @@ -0,0 +1,221 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "regexp.h" + +#if defined(GIT_REGEX_BUILTIN) || defined(GIT_REGEX_PCRE) + +int git_regexp_compile(git_regexp *r, const char *pattern, int flags) +{ + int erroffset, cflags = 0; + const char *error = NULL; + + if (flags & GIT_REGEXP_ICASE) + cflags |= PCRE_CASELESS; + + if ((*r = pcre_compile(pattern, cflags, &error, &erroffset, NULL)) == NULL) { + git_error_set_str(GIT_ERROR_REGEX, error); + return GIT_EINVALIDSPEC; + } + + return 0; +} + +void git_regexp_dispose(git_regexp *r) +{ + pcre_free(*r); + *r = NULL; +} + +int git_regexp_match(const git_regexp *r, const char *string) +{ + int error; + if ((error = pcre_exec(*r, NULL, string, (int) strlen(string), 0, 0, NULL, 0)) < 0) + return (error == PCRE_ERROR_NOMATCH) ? GIT_ENOTFOUND : GIT_EINVALIDSPEC; + return 0; +} + +int git_regexp_search(const git_regexp *r, const char *string, size_t nmatches, git_regmatch *matches) +{ + int static_ovec[9] = {0}, *ovec; + int error; + size_t i; + + /* The ovec array always needs to be a multiple of three */ + if (nmatches <= ARRAY_SIZE(static_ovec) / 3) + ovec = static_ovec; + else + ovec = git__calloc(nmatches * 3, sizeof(*ovec)); + GIT_ERROR_CHECK_ALLOC(ovec); + + if ((error = pcre_exec(*r, NULL, string, (int) strlen(string), 0, 0, ovec, (int) nmatches * 3)) < 0) + goto out; + + if (error == 0) + error = (int) nmatches; + + for (i = 0; i < (unsigned int) error; i++) { + matches[i].start = (ovec[i * 2] < 0) ? -1 : ovec[i * 2]; + matches[i].end = (ovec[i * 2 + 1] < 0) ? -1 : ovec[i * 2 + 1]; + } + for (i = (unsigned int) error; i < nmatches; i++) + matches[i].start = matches[i].end = -1; + +out: + if (nmatches > ARRAY_SIZE(static_ovec) / 3) + git__free(ovec); + if (error < 0) + return (error == PCRE_ERROR_NOMATCH) ? GIT_ENOTFOUND : GIT_EINVALIDSPEC; + return 0; +} + +#elif defined(GIT_REGEX_PCRE2) + +int git_regexp_compile(git_regexp *r, const char *pattern, int flags) +{ + unsigned char errmsg[1024]; + PCRE2_SIZE erroff; + int error, cflags = 0; + + if (flags & GIT_REGEXP_ICASE) + cflags |= PCRE2_CASELESS; + + if ((*r = pcre2_compile((const unsigned char *) pattern, PCRE2_ZERO_TERMINATED, + cflags, &error, &erroff, NULL)) == NULL) { + pcre2_get_error_message(error, errmsg, sizeof(errmsg)); + git_error_set_str(GIT_ERROR_REGEX, (char *) errmsg); + return GIT_EINVALIDSPEC; + } + + return 0; +} + +void git_regexp_dispose(git_regexp *r) +{ + pcre2_code_free(*r); + *r = NULL; +} + +int git_regexp_match(const git_regexp *r, const char *string) +{ + pcre2_match_data *data; + int error; + + data = pcre2_match_data_create(1, NULL); + GIT_ERROR_CHECK_ALLOC(data); + + error = pcre2_match(*r, (const unsigned char *) string, strlen(string), 0, 0, data, NULL); + pcre2_match_data_free(data); + if (error < 0) + return (error == PCRE2_ERROR_NOMATCH) ? GIT_ENOTFOUND : GIT_EINVALIDSPEC; + + return 0; +} + +int git_regexp_search(const git_regexp *r, const char *string, size_t nmatches, git_regmatch *matches) +{ + pcre2_match_data *data = NULL; + PCRE2_SIZE *ovec; + int error; + size_t i; + + if ((data = pcre2_match_data_create(nmatches, NULL)) == NULL) { + git_error_set_oom(); + return -1; + } + + if ((error = pcre2_match(*r, (const unsigned char *) string, strlen(string), + 0, 0, data, NULL)) < 0) + goto out; + + if (error == 0 || (unsigned int) error > nmatches) + error = nmatches; + ovec = pcre2_get_ovector_pointer(data); + + for (i = 0; i < (unsigned int) error; i++) { + matches[i].start = (ovec[i * 2] == PCRE2_UNSET) ? -1 : (ssize_t) ovec[i * 2]; + matches[i].end = (ovec[i * 2 + 1] == PCRE2_UNSET) ? -1 : (ssize_t) ovec[i * 2 + 1]; + } + for (i = (unsigned int) error; i < nmatches; i++) + matches[i].start = matches[i].end = -1; + +out: + pcre2_match_data_free(data); + if (error < 0) + return (error == PCRE2_ERROR_NOMATCH) ? GIT_ENOTFOUND : GIT_EINVALIDSPEC; + return 0; +} + +#elif defined(GIT_REGEX_REGCOMP) || defined(GIT_REGEX_REGCOMP_L) + +#if defined(GIT_REGEX_REGCOMP_L) +# include +#endif + +int git_regexp_compile(git_regexp *r, const char *pattern, int flags) +{ + int cflags = REG_EXTENDED, error; + char errmsg[1024]; + + if (flags & GIT_REGEXP_ICASE) + cflags |= REG_ICASE; + +# if defined(GIT_REGEX_REGCOMP) + if ((error = regcomp(r, pattern, cflags)) != 0) +# else + if ((error = regcomp_l(r, pattern, cflags, (locale_t) 0)) != 0) +# endif + { + regerror(error, r, errmsg, sizeof(errmsg)); + git_error_set_str(GIT_ERROR_REGEX, errmsg); + return GIT_EINVALIDSPEC; + } + + return 0; +} + +void git_regexp_dispose(git_regexp *r) +{ + regfree(r); +} + +int git_regexp_match(const git_regexp *r, const char *string) +{ + int error; + if ((error = regexec(r, string, 0, NULL, 0)) != 0) + return (error == REG_NOMATCH) ? GIT_ENOTFOUND : GIT_EINVALIDSPEC; + return 0; +} + +int git_regexp_search(const git_regexp *r, const char *string, size_t nmatches, git_regmatch *matches) +{ + regmatch_t static_m[3], *m; + int error; + size_t i; + + if (nmatches <= ARRAY_SIZE(static_m)) + m = static_m; + else + m = git__calloc(nmatches, sizeof(*m)); + + if ((error = regexec(r, string, nmatches, m, 0)) != 0) + goto out; + + for (i = 0; i < nmatches; i++) { + matches[i].start = (m[i].rm_so < 0) ? -1 : m[i].rm_so; + matches[i].end = (m[i].rm_eo < 0) ? -1 : m[i].rm_eo; + } + +out: + if (nmatches > ARRAY_SIZE(static_m)) + git__free(m); + if (error) + return (error == REG_NOMATCH) ? GIT_ENOTFOUND : GIT_EINVALIDSPEC; + return 0; +} + +#endif diff --git a/src/util/regexp.h b/src/util/regexp.h new file mode 100644 index 00000000000..d0862b10786 --- /dev/null +++ b/src/util/regexp.h @@ -0,0 +1,97 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_regexp_h__ +#define INCLUDE_regexp_h__ + +#include "git2_util.h" + +#if defined(GIT_REGEX_BUILTIN) || defined(GIT_REGEX_PCRE) +# include "pcre.h" +typedef pcre *git_regexp; +# define GIT_REGEX_INIT NULL +#elif defined(GIT_REGEX_PCRE2) +# define PCRE2_CODE_UNIT_WIDTH 8 +# include +typedef pcre2_code *git_regexp; +# define GIT_REGEX_INIT NULL +#elif defined(GIT_REGEX_REGCOMP) || defined(GIT_REGEX_REGCOMP_L) +# include +typedef regex_t git_regexp; +# define GIT_REGEX_INIT { 0 } +#else +# error "No regex backend" +#endif + +/** Options supported by @git_regexp_compile. */ +typedef enum { + /** Enable case-insensitive matching */ + GIT_REGEXP_ICASE = (1 << 0) +} git_regexp_flags_t; + +/** Structure containing information about regular expression matching groups */ +typedef struct { + /** Start of the given match. -1 if the group didn't match anything */ + ssize_t start; + /** End of the given match. -1 if the group didn't match anything */ + ssize_t end; +} git_regmatch; + +/** + * Compile a regular expression. The compiled expression needs to + * be cleaned up afterwards with `git_regexp_dispose`. + * + * @param r Pointer to the storage where to initialize the regular expression. + * @param pattern The pattern that shall be compiled. + * @param flags Flags to alter how the pattern shall be handled. + * 0 for defaults, otherwise see @git_regexp_flags_t. + * @return 0 on success, otherwise a negative return value. + */ +int git_regexp_compile(git_regexp *r, const char *pattern, int flags); + +/** + * Free memory associated with the regular expression + * + * @param r The regular expression structure to dispose. + */ +void git_regexp_dispose(git_regexp *r); + +/** + * Test whether a given string matches a compiled regular + * expression. + * + * @param r Compiled regular expression. + * @param string String to match against the regular expression. + * @return 0 if the string matches, a negative error code + * otherwise. GIT_ENOTFOUND if no match was found, + * GIT_EINVALIDSPEC if the regular expression matching + * was invalid. + */ +int git_regexp_match(const git_regexp *r, const char *string); + +/** + * Search for matches inside of a given string. + * + * Given a regular expression with capturing groups, this + * function will populate provided @git_regmatch structures with + * offsets for each of the given matches. Non-matching groups + * will have start and end values of the respective @git_regmatch + * structure set to -1. + * + * @param r Compiled regular expression. + * @param string String to match against the regular expression. + * @param nmatches Number of @git_regmatch structures provided by + * the user. + * @param matches Pointer to an array of @git_regmatch structures. + * @return 0 if the string matches, a negative error code + * otherwise. GIT_ENOTFOUND if no match was found, + * GIT_EINVALIDSPEC if the regular expression matching + * was invalid. + */ +int git_regexp_search(const git_regexp *r, const char *string, size_t nmatches, git_regmatch *matches); + +#endif diff --git a/src/util/runtime.c b/src/util/runtime.c new file mode 100644 index 00000000000..a7711ffc44c --- /dev/null +++ b/src/util/runtime.c @@ -0,0 +1,162 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2_util.h" +#include "runtime.h" + +static git_runtime_shutdown_fn shutdown_callback[32]; +static git_atomic32 shutdown_callback_count; + +static git_atomic32 init_count; + +static int init_common(git_runtime_init_fn init_fns[], size_t cnt) +{ + size_t i; + int ret; + + /* Initialize subsystems that have global state */ + for (i = 0; i < cnt; i++) { + if ((ret = init_fns[i]()) != 0) + break; + } + + GIT_MEMORY_BARRIER; + + return ret; +} + +static void shutdown_common(void) +{ + git_runtime_shutdown_fn cb; + int pos; + + for (pos = git_atomic32_get(&shutdown_callback_count); + pos > 0; + pos = git_atomic32_dec(&shutdown_callback_count)) { + cb = git_atomic_swap(shutdown_callback[pos - 1], NULL); + + if (cb != NULL) + cb(); + } +} + +int git_runtime_shutdown_register(git_runtime_shutdown_fn callback) +{ + int count = git_atomic32_inc(&shutdown_callback_count); + + if (count > (int)ARRAY_SIZE(shutdown_callback) || count == 0) { + git_error_set(GIT_ERROR_INVALID, + "too many shutdown callbacks registered"); + git_atomic32_dec(&shutdown_callback_count); + return -1; + } + + shutdown_callback[count - 1] = callback; + + return 0; +} + +#if defined(GIT_THREADS) && defined(GIT_WIN32) + +/* + * On Win32, we use a spinlock to provide locking semantics. This is + * lighter-weight than a proper critical section. + */ +static volatile LONG init_spinlock = 0; + +GIT_INLINE(int) init_lock(void) +{ + while (InterlockedCompareExchange(&init_spinlock, 1, 0)) { Sleep(0); } + return 0; +} + +GIT_INLINE(int) init_unlock(void) +{ + InterlockedExchange(&init_spinlock, 0); + return 0; +} + +#elif defined(GIT_THREADS) && defined(_POSIX_THREADS) + +/* + * On POSIX, we need to use a proper mutex for locking. We might prefer + * a spinlock here, too, but there's no static initializer for a + * pthread_spinlock_t. + */ +static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER; + +GIT_INLINE(int) init_lock(void) +{ + return pthread_mutex_lock(&init_mutex) == 0 ? 0 : -1; +} + +GIT_INLINE(int) init_unlock(void) +{ + return pthread_mutex_unlock(&init_mutex) == 0 ? 0 : -1; +} + +#elif defined(GIT_THREADS) +# error unknown threading model +#else + +# define init_lock() git__noop() +# define init_unlock() git__noop() + +#endif + +int git_runtime_init(git_runtime_init_fn init_fns[], size_t cnt) +{ + int ret; + + if (init_lock() < 0) + return -1; + + /* Only do work on a 0 -> 1 transition of the refcount */ + if ((ret = git_atomic32_inc(&init_count)) == 1) { + if (init_common(init_fns, cnt) < 0) + ret = -1; + } + + if (init_unlock() < 0) + return -1; + + return ret; +} + +int git_runtime_init_count(void) +{ + int ret; + + if (init_lock() < 0) + return -1; + + ret = git_atomic32_get(&init_count); + + if (init_unlock() < 0) + return -1; + + return ret; +} + +int git_runtime_shutdown(void) +{ + int ret; + + /* Enter the lock */ + if (init_lock() < 0) + return -1; + + /* Only do work on a 1 -> 0 transition of the refcount */ + if ((ret = git_atomic32_dec(&init_count)) == 0) + shutdown_common(); + + /* Exit the lock */ + if (init_unlock() < 0) + return -1; + + return ret; +} diff --git a/src/util/runtime.h b/src/util/runtime.h new file mode 100644 index 00000000000..6cbfd6043a4 --- /dev/null +++ b/src/util/runtime.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_runtime_h__ +#define INCLUDE_runtime_h__ + +#include "git2_util.h" + +typedef int (*git_runtime_init_fn)(void); +typedef void (*git_runtime_shutdown_fn)(void); + +/** + * Start up a new runtime. If this is the first time that this + * function is called within the context of the current library + * or executable, then the given `init_fns` will be invoked. If + * it is not the first time, they will be ignored. + * + * The given initialization functions _may_ register shutdown + * handlers using `git_runtime_shutdown_register` to be notified + * when the runtime is shutdown. + * + * @param init_fns The list of initialization functions to call + * @param cnt The number of init_fns + * @return The number of initializations performed (including this one) or an error + */ +int git_runtime_init(git_runtime_init_fn init_fns[], size_t cnt); + +/* + * Returns the number of initializations active (the number of calls to + * `git_runtime_init` minus the number of calls sto `git_runtime_shutdown`). + * If 0, the runtime is not currently initialized. + * + * @return The number of initializations performed or an error + */ +int git_runtime_init_count(void); + +/** + * Shut down the runtime. If this is the last shutdown call, + * such that there are no remaining `init` calls, then any + * shutdown hooks that have been registered will be invoked. + * + * The number of outstanding initializations will be returned. + * If this number is 0, then the runtime is shutdown. + * + * @return The number of outstanding initializations (after this one) or an error + */ +int git_runtime_shutdown(void); + +/** + * Register a shutdown handler for this runtime. This should be done + * by a function invoked by `git_runtime_init` to ensure that the + * appropriate locks are taken. + * + * @param callback The shutdown handler callback + * @return 0 or an error code + */ +int git_runtime_shutdown_register(git_runtime_shutdown_fn callback); + +#endif diff --git a/src/util/sortedcache.c b/src/util/sortedcache.c new file mode 100644 index 00000000000..05b94347267 --- /dev/null +++ b/src/util/sortedcache.c @@ -0,0 +1,381 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "sortedcache.h" +#include "hashmap.h" + +int git_sortedcache_new( + git_sortedcache **out, + size_t item_path_offset, + git_sortedcache_free_item_fn free_item, + void *free_item_payload, + git_vector_cmp item_cmp, + const char *path) +{ + git_sortedcache *sc; + size_t pathlen, alloclen; + + pathlen = path ? strlen(path) : 0; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_sortedcache), pathlen); + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); + sc = git__calloc(1, alloclen); + GIT_ERROR_CHECK_ALLOC(sc); + + if (git_pool_init(&sc->pool, 1) < 0 || + git_vector_init(&sc->items, 4, item_cmp) < 0) + goto fail; + + if (git_rwlock_init(&sc->lock)) { + git_error_set(GIT_ERROR_OS, "failed to initialize lock"); + goto fail; + } + + sc->item_path_offset = item_path_offset; + sc->free_item = free_item; + sc->free_item_payload = free_item_payload; + GIT_REFCOUNT_INC(sc); + if (pathlen) + memcpy(sc->path, path, pathlen); + + *out = sc; + return 0; + +fail: + git_vector_dispose(&sc->items); + git_pool_clear(&sc->pool); + git__free(sc); + return -1; +} + +void git_sortedcache_incref(git_sortedcache *sc) +{ + GIT_REFCOUNT_INC(sc); +} + +const char *git_sortedcache_path(git_sortedcache *sc) +{ + return sc->path; +} + +static void sortedcache_clear(git_sortedcache *sc) +{ + git_hashmap_str_clear(&sc->map); + + if (sc->free_item) { + size_t i; + void *item; + + git_vector_foreach(&sc->items, i, item) { + sc->free_item(sc->free_item_payload, item); + } + } + + git_vector_clear(&sc->items); + + git_pool_clear(&sc->pool); +} + +static void sortedcache_free(git_sortedcache *sc) +{ + /* acquire write lock to make sure everyone else is done */ + if (git_sortedcache_wlock(sc) < 0) + return; + + sortedcache_clear(sc); + git_vector_dispose(&sc->items); + git_hashmap_str_dispose(&sc->map); + + git_sortedcache_wunlock(sc); + + git_rwlock_free(&sc->lock); + git__free(sc); +} + +void git_sortedcache_free(git_sortedcache *sc) +{ + if (!sc) + return; + GIT_REFCOUNT_DEC(sc, sortedcache_free); +} + +static int sortedcache_copy_item(void *payload, void *tgt_item, void *src_item) +{ + git_sortedcache *sc = payload; + /* path will already have been copied by upsert */ + memcpy(tgt_item, src_item, sc->item_path_offset); + return 0; +} + +/* copy a sorted cache */ +int git_sortedcache_copy( + git_sortedcache **out, + git_sortedcache *src, + bool lock, + int (*copy_item)(void *payload, void *tgt_item, void *src_item), + void *payload) +{ + int error = 0; + git_sortedcache *tgt; + size_t i; + void *src_item, *tgt_item; + + /* just use memcpy if no special copy fn is passed in */ + if (!copy_item) { + copy_item = sortedcache_copy_item; + payload = src; + } + + if ((error = git_sortedcache_new( + &tgt, src->item_path_offset, + src->free_item, src->free_item_payload, + src->items._cmp, src->path)) < 0) + return error; + + if (lock && git_sortedcache_rlock(src) < 0) { + git_sortedcache_free(tgt); + return -1; + } + + git_vector_foreach(&src->items, i, src_item) { + char *path = ((char *)src_item) + src->item_path_offset; + + if ((error = git_sortedcache_upsert(&tgt_item, tgt, path)) < 0 || + (error = copy_item(payload, tgt_item, src_item)) < 0) + break; + } + + if (lock) + git_sortedcache_runlock(src); + if (error) + git_sortedcache_free(tgt); + + *out = !error ? tgt : NULL; + + return error; +} + +/* lock sortedcache while making modifications */ +int git_sortedcache_wlock(git_sortedcache *sc) +{ + GIT_UNUSED(sc); /* prevent warning when compiled w/o threads */ + + if (git_rwlock_wrlock(&sc->lock) < 0) { + git_error_set(GIT_ERROR_OS, "unable to acquire write lock on cache"); + return -1; + } + return 0; +} + +/* unlock sorted cache when done with modifications */ +void git_sortedcache_wunlock(git_sortedcache *sc) +{ + git_vector_sort(&sc->items); + git_rwlock_wrunlock(&sc->lock); +} + +/* lock sortedcache for read */ +int git_sortedcache_rlock(git_sortedcache *sc) +{ + GIT_UNUSED(sc); /* prevent warning when compiled w/o threads */ + + if (git_rwlock_rdlock(&sc->lock) < 0) { + git_error_set(GIT_ERROR_OS, "unable to acquire read lock on cache"); + return -1; + } + return 0; +} + +/* unlock sorted cache when done reading */ +void git_sortedcache_runlock(git_sortedcache *sc) +{ + GIT_UNUSED(sc); /* prevent warning when compiled w/o threads */ + git_rwlock_rdunlock(&sc->lock); +} + +/* if the file has changed, lock cache and load file contents into buf; + * returns <0 on error, >0 if file has not changed + */ +int git_sortedcache_lockandload(git_sortedcache *sc, git_str *buf) +{ + int error, fd; + struct stat st; + + if ((error = git_sortedcache_wlock(sc)) < 0) + return error; + + if ((error = git_futils_filestamp_check(&sc->stamp, sc->path)) <= 0) + goto unlock; + + if ((fd = git_futils_open_ro(sc->path)) < 0) { + error = fd; + goto unlock; + } + + if (p_fstat(fd, &st) < 0) { + git_error_set(GIT_ERROR_OS, "failed to stat file"); + error = -1; + (void)p_close(fd); + goto unlock; + } + + if (!git__is_sizet(st.st_size)) { + git_error_set(GIT_ERROR_INVALID, "unable to load file larger than size_t"); + error = -1; + (void)p_close(fd); + goto unlock; + } + + if (buf) + error = git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size); + + (void)p_close(fd); + + if (error < 0) + goto unlock; + + return 1; /* return 1 -> file needs reload and was successfully loaded */ + +unlock: + git_sortedcache_wunlock(sc); + return error; +} + +void git_sortedcache_updated(git_sortedcache *sc) +{ + /* update filestamp to latest value */ + git_futils_filestamp_check(&sc->stamp, sc->path); +} + +/* release all items in sorted cache */ +int git_sortedcache_clear(git_sortedcache *sc, bool wlock) +{ + if (wlock && git_sortedcache_wlock(sc) < 0) + return -1; + + sortedcache_clear(sc); + + if (wlock) + git_sortedcache_wunlock(sc); + + return 0; +} + +/* find and/or insert item, returning pointer to item data */ +int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key) +{ + size_t keylen, itemlen; + int error = 0; + char *item_key; + void *item; + + if (git_hashmap_str_get(&item, &sc->map, key) == 0) + goto done; + + keylen = strlen(key); + itemlen = sc->item_path_offset + keylen + 1; + itemlen = (itemlen + 7) & ~7; + + if ((item = git_pool_mallocz(&sc->pool, itemlen)) == NULL) { + /* don't use GIT_ERROR_CHECK_ALLOC b/c of lock */ + error = -1; + goto done; + } + + /* one strange thing is that even if the vector or hash table insert + * fail, there is no way to free the pool item so we just abandon it + */ + + item_key = ((char *)item) + sc->item_path_offset; + memcpy(item_key, key, keylen); + + if ((error = git_hashmap_str_put(&sc->map, item_key, item)) < 0) + goto done; + + if ((error = git_vector_insert(&sc->items, item)) < 0) + git_hashmap_str_remove(&sc->map, item_key); + +done: + if (out) + *out = !error ? item : NULL; + return error; +} + +/* lookup item by key */ +void *git_sortedcache_lookup(git_sortedcache *sc, const char *key) +{ + void *value; + + return git_hashmap_str_get(&value, &sc->map, key) == 0 ? value : NULL; +} + +/* find out how many items are in the cache */ +size_t git_sortedcache_entrycount(const git_sortedcache *sc) +{ + return git_vector_length(&sc->items); +} + +/* lookup item by index */ +void *git_sortedcache_entry(git_sortedcache *sc, size_t pos) +{ + /* make sure the items are sorted so this gets the correct item */ + if (!git_vector_is_sorted(&sc->items)) + git_vector_sort(&sc->items); + + return git_vector_get(&sc->items, pos); +} + +/* helper struct so bsearch callback can know offset + key value for cmp */ +struct sortedcache_magic_key { + size_t offset; + const char *key; +}; + +static int sortedcache_magic_cmp(const void *key, const void *value) +{ + const struct sortedcache_magic_key *magic = key; + const char *value_key = ((const char *)value) + magic->offset; + return strcmp(magic->key, value_key); +} + +/* lookup index of item by key */ +int git_sortedcache_lookup_index( + size_t *out, git_sortedcache *sc, const char *key) +{ + struct sortedcache_magic_key magic; + + magic.offset = sc->item_path_offset; + magic.key = key; + + return git_vector_bsearch2(out, &sc->items, sortedcache_magic_cmp, &magic); +} + +/* remove entry from cache */ +int git_sortedcache_remove(git_sortedcache *sc, size_t pos) +{ + char *item; + + /* + * Because of pool allocation, this can't actually remove the item, + * but we can remove it from the items vector and the hash table. + */ + + if ((item = git_vector_get(&sc->items, pos)) == NULL) { + git_error_set(GIT_ERROR_INVALID, "removing item out of range"); + return GIT_ENOTFOUND; + } + + (void)git_vector_remove(&sc->items, pos); + + git_hashmap_str_remove(&sc->map, item + sc->item_path_offset); + + if (sc->free_item) + sc->free_item(sc->free_item_payload, item); + + return 0; +} + diff --git a/src/util/sortedcache.h b/src/util/sortedcache.h new file mode 100644 index 00000000000..d4383e96517 --- /dev/null +++ b/src/util/sortedcache.h @@ -0,0 +1,182 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_sorted_cache_h__ +#define INCLUDE_sorted_cache_h__ + +#include "git2_util.h" + +#include "util.h" +#include "futils.h" +#include "vector.h" +#include "thread.h" +#include "pool.h" +#include "hashmap_str.h" + +#include + +/* + * The purpose of this data structure is to cache the parsed contents of a + * file (a.k.a. the backing file) where each item in the file can be + * identified by a key string and you want to both look them up by name + * and traverse them in sorted order. Each item is assumed to itself end + * in a GIT_FLEX_ARRAY. + */ + +typedef void (*git_sortedcache_free_item_fn)(void *payload, void *item); + +typedef struct { + git_refcount rc; + git_rwlock lock; + size_t item_path_offset; + git_sortedcache_free_item_fn free_item; + void *free_item_payload; + git_pool pool; + git_vector items; + git_hashmap_str map; + git_futils_filestamp stamp; + char path[GIT_FLEX_ARRAY]; +} git_sortedcache; + +/* Create a new sortedcache + * + * Even though every sortedcache stores items with a GIT_FLEX_ARRAY at + * the end containing their key string, you have to provide the item_cmp + * sorting function because the sorting function doesn't get a payload + * and therefore can't know the offset to the item key string. :-( + * + * @param out The allocated git_sortedcache + * @param item_path_offset Offset to the GIT_FLEX_ARRAY item key in the + * struct - use offsetof(struct mine, key-field) to get this + * @param free_item Optional callback to free each item + * @param free_item_payload Optional payload passed to free_item callback + * @param item_cmp Compare the keys of two items + * @param path The path to the backing store file for this cache; this + * may be NULL. The cache makes it easy to load this and check + * if it has been modified since the last load and/or write. + */ +GIT_WARN_UNUSED_RESULT int git_sortedcache_new( + git_sortedcache **out, + size_t item_path_offset, /* use offsetof(struct, path-field) macro */ + git_sortedcache_free_item_fn free_item, + void *free_item_payload, + git_vector_cmp item_cmp, + const char *path); + +/* Copy a sorted cache + * + * - `copy_item` can be NULL to just use memcpy + * - if `lock`, grabs read lock on `src` during copy and releases after + */ +GIT_WARN_UNUSED_RESULT int git_sortedcache_copy( + git_sortedcache **out, + git_sortedcache *src, + bool lock, + int (*copy_item)(void *payload, void *tgt_item, void *src_item), + void *payload); + +/* Free sorted cache (first calling `free_item` callbacks) + * + * Don't call on a locked collection - it may acquire a write lock + */ +void git_sortedcache_free(git_sortedcache *sc); + +/* Increment reference count - balance with call to free */ +void git_sortedcache_incref(git_sortedcache *sc); + +/* Get the pathname associated with this cache at creation time */ +const char *git_sortedcache_path(git_sortedcache *sc); + +/* + * CACHE WRITE FUNCTIONS + * + * The following functions require you to have a writer lock to make the + * modification. Some of the functions take a `wlock` parameter and + * will optionally lock and unlock for you if that is passed as true. + * + */ + +/* Lock sortedcache for write */ +GIT_WARN_UNUSED_RESULT int git_sortedcache_wlock(git_sortedcache *sc); + +/* Unlock sorted cache when done with write */ +void git_sortedcache_wunlock(git_sortedcache *sc); + +/* Lock cache and load backing file into a buffer. + * + * This grabs a write lock on the cache then looks at the modification + * time and size of the file on disk. + * + * If the file appears to have changed, this loads the file contents into + * the buffer and returns a positive value leaving the cache locked - the + * caller should parse the file content, update the cache as needed, then + * release the lock. NOTE: In this case, the caller MUST unlock the cache. + * + * If the file appears to be unchanged, then this automatically releases + * the lock on the cache, clears the buffer, and returns 0. + * + * @return 0 if up-to-date, 1 if out-of-date, <0 on error + */ +GIT_WARN_UNUSED_RESULT int git_sortedcache_lockandload( + git_sortedcache *sc, git_str *buf); + +/* Refresh file timestamp after write completes + * You should already be holding the write lock when you call this. + */ +void git_sortedcache_updated(git_sortedcache *sc); + +/* Release all items in sorted cache + * + * If `wlock` is true, grabs write lock and releases when done, otherwise + * you should already be holding a write lock when you call this. + */ +GIT_WARN_UNUSED_RESULT int git_sortedcache_clear( + git_sortedcache *sc, bool wlock); + +/* Find and/or insert item, returning pointer to item data. + * You should already be holding the write lock when you call this. + */ +GIT_WARN_UNUSED_RESULT int git_sortedcache_upsert( + void **out, git_sortedcache *sc, const char *key); + +/* Removes entry at pos from cache + * You should already be holding the write lock when you call this. + */ +int git_sortedcache_remove(git_sortedcache *sc, size_t pos); + +/* + * CACHE READ FUNCTIONS + * + * The following functions access items in the cache. To prevent the + * results from being invalidated before they can be used, you should be + * holding either a read lock or a write lock when using these functions. + * + */ + +/* Lock sortedcache for read */ +GIT_WARN_UNUSED_RESULT int git_sortedcache_rlock(git_sortedcache *sc); + +/* Unlock sorted cache when done with read */ +void git_sortedcache_runlock(git_sortedcache *sc); + +/* Lookup item by key - returns NULL if not found */ +void *git_sortedcache_lookup(git_sortedcache *sc, const char *key); + +/* Get how many items are in the cache + * + * You can call this function without holding a lock, but be aware + * that it may change before you use it. + */ +size_t git_sortedcache_entrycount(const git_sortedcache *sc); + +/* Lookup item by index - returns NULL if out of range */ +void *git_sortedcache_entry(git_sortedcache *sc, size_t pos); + +/* Lookup index of item by key - returns GIT_ENOTFOUND if not found */ +int git_sortedcache_lookup_index( + size_t *out, git_sortedcache *sc, const char *key); + +#endif diff --git a/src/util/staticstr.h b/src/util/staticstr.h new file mode 100644 index 00000000000..b7d0790c4fd --- /dev/null +++ b/src/util/staticstr.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_stackstr_h__ +#define INCLUDE_stackstr_h__ + +#include "git2_util.h" + +typedef struct { + /* Length of / number of bytes used by `data`. */ + size_t len; + + /* Size of the allocated `data` buffer. */ + size_t size; + + /* The actual string buffer data. */ + char data[GIT_FLEX_ARRAY]; +} git_staticstr; + +#define git_staticstr_with_size(__size) \ + struct { \ + size_t len; \ + size_t size; \ + char data[__size]; \ + } + +#define git_staticstr_init(__str, __size) \ + do { \ + (__str)->len = 0; \ + (__str)->size = __size; \ + (__str)->data[0] = '\0'; \ + } while(0) + +#define git_staticstr_offset(__str) \ + ((__str)->data + (__str)->len) + +#define git_staticstr_remain(__str) \ + ((__str)->len > (__str)->size ? 0 : ((__str)->size - (__str)->len)) + +#define git_staticstr_increase(__str, __len) \ + do { ((__str)->len += __len); } while(0) + +#define git_staticstr_consume_bytes(__str, __len) \ + do { git_staticstr_consume(__str, (__str)->data + __len); } while(0) + +#define git_staticstr_consume(__str, __end) \ + do { \ + if (__end > (__str)->data && \ + __end <= (__str)->data + (__str)->len) { \ + size_t __consumed = __end - (__str)->data; \ + memmove((__str)->data, __end, (__str)->len - __consumed); \ + (__str)->len -= __consumed; \ + (__str)->data[(__str)->len] = '\0'; \ + } \ + } while(0) + +#define git_staticstr_clear(__str) \ + do { \ + (__str)->len = 0; \ + (__str)->data[0] = 0; \ + } while(0) + +#endif diff --git a/src/util/str.c b/src/util/str.c new file mode 100644 index 00000000000..7b8d99bc5f8 --- /dev/null +++ b/src/util/str.c @@ -0,0 +1,1384 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "str.h" +#include "posix.h" +#include + +/* Used as default value for git_str->ptr so that people can always + * assume ptr is non-NULL and zero terminated even for new git_strs. + */ +char git_str__initstr[1]; + +char git_str__oom[1]; + +#define ENSURE_SIZE(b, d) \ + if ((b)->ptr == git_str__oom || \ + ((d) > (b)->asize && git_str_grow((b), (d)) < 0))\ + return -1; + + +int git_str_init(git_str *buf, size_t initial_size) +{ + buf->asize = 0; + buf->size = 0; + buf->ptr = git_str__initstr; + + ENSURE_SIZE(buf, initial_size); + + return 0; +} + +int git_str_try_grow( + git_str *buf, size_t target_size, bool mark_oom) +{ + char *new_ptr; + size_t new_size; + + if (buf->ptr == git_str__oom) + return -1; + + if (buf->asize == 0 && buf->size != 0) { + git_error_set(GIT_ERROR_INVALID, "cannot grow a borrowed buffer"); + return GIT_EINVALID; + } + + if (!target_size) + target_size = buf->size; + + if (target_size <= buf->asize) + return 0; + + if (buf->asize == 0) { + new_size = target_size; + new_ptr = NULL; + } else { + new_size = buf->asize; + /* + * Grow the allocated buffer by 1.5 to allow + * re-use of memory holes resulting from the + * realloc. If this is still too small, then just + * use the target size. + */ + if ((new_size = (new_size << 1) - (new_size >> 1)) < target_size) + new_size = target_size; + new_ptr = buf->ptr; + } + + /* round allocation up to multiple of 8 */ + new_size = (new_size + 7) & ~7; + + if (new_size < buf->size) { + if (mark_oom) { + if (buf->ptr && buf->ptr != git_str__initstr) + git__free(buf->ptr); + buf->ptr = git_str__oom; + } + + git_error_set_oom(); + return -1; + } + + new_ptr = git__realloc(new_ptr, new_size); + + if (!new_ptr) { + if (mark_oom) { + if (buf->ptr && (buf->ptr != git_str__initstr)) + git__free(buf->ptr); + buf->ptr = git_str__oom; + } + return -1; + } + + buf->asize = new_size; + buf->ptr = new_ptr; + + /* truncate the existing buffer size if necessary */ + if (buf->size >= buf->asize) + buf->size = buf->asize - 1; + buf->ptr[buf->size] = '\0'; + + return 0; +} + +int git_str_grow(git_str *buffer, size_t target_size) +{ + return git_str_try_grow(buffer, target_size, true); +} + +int git_str_grow_by(git_str *buffer, size_t additional_size) +{ + size_t newsize; + + if (GIT_ADD_SIZET_OVERFLOW(&newsize, buffer->size, additional_size)) { + buffer->ptr = git_str__oom; + return -1; + } + + return git_str_try_grow(buffer, newsize, true); +} + +void git_str_dispose(git_str *buf) +{ + if (!buf) return; + + if (buf->asize > 0 && buf->ptr != NULL && buf->ptr != git_str__oom) + git__free(buf->ptr); + + git_str_init(buf, 0); +} + +void git_str_clear(git_str *buf) +{ + buf->size = 0; + + if (!buf->ptr) { + buf->ptr = git_str__initstr; + buf->asize = 0; + } + + if (buf->asize > 0) + buf->ptr[0] = '\0'; +} + +int git_str_set(git_str *buf, const void *data, size_t len) +{ + size_t alloclen; + + if (len == 0 || data == NULL) { + git_str_clear(buf); + } else { + if (data != buf->ptr) { + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1); + ENSURE_SIZE(buf, alloclen); + memmove(buf->ptr, data, len); + } + + buf->size = len; + if (buf->asize > buf->size) + buf->ptr[buf->size] = '\0'; + + } + return 0; +} + +int git_str_sets(git_str *buf, const char *string) +{ + return git_str_set(buf, string, string ? strlen(string) : 0); +} + +int git_str_putc(git_str *buf, char c) +{ + size_t new_size; + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, 2); + ENSURE_SIZE(buf, new_size); + buf->ptr[buf->size++] = c; + buf->ptr[buf->size] = '\0'; + return 0; +} + +int git_str_putcn(git_str *buf, char c, size_t len) +{ + size_t new_size; + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, len); + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); + ENSURE_SIZE(buf, new_size); + memset(buf->ptr + buf->size, c, len); + buf->size += len; + buf->ptr[buf->size] = '\0'; + return 0; +} + +int git_str_put(git_str *buf, const char *data, size_t len) +{ + if (len) { + size_t new_size; + + GIT_ASSERT_ARG(data); + + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, len); + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); + ENSURE_SIZE(buf, new_size); + memmove(buf->ptr + buf->size, data, len); + buf->size += len; + buf->ptr[buf->size] = '\0'; + } + return 0; +} + +int git_str_puts(git_str *buf, const char *string) +{ + GIT_ASSERT_ARG(string); + + return git_str_put(buf, string, strlen(string)); +} + +static char hex_encode[] = "0123456789abcdef"; + +int git_str_encode_hexstr(git_str *str, const char *data, size_t len) +{ + size_t new_size, i; + char *s; + + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&new_size, len, 2); + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); + + if (git_str_grow_by(str, new_size) < 0) + return -1; + + s = str->ptr + str->size; + + for (i = 0; i < len; i++) { + *s++ = hex_encode[(data[i] & 0xf0) >> 4]; + *s++ = hex_encode[(data[i] & 0x0f)]; + } + + str->size += (len * 2); + str->ptr[str->size] = '\0'; + + return 0; +} + +static const char base64_encode[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +int git_str_encode_base64(git_str *buf, const char *data, size_t len) +{ + size_t extra = len % 3; + uint8_t *write, a, b, c; + const uint8_t *read = (const uint8_t *)data; + size_t blocks = (len / 3) + !!extra, alloclen; + + GIT_ERROR_CHECK_ALLOC_ADD(&blocks, blocks, 1); + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloclen, blocks, 4); + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, buf->size); + + ENSURE_SIZE(buf, alloclen); + write = (uint8_t *)&buf->ptr[buf->size]; + + /* convert each run of 3 bytes into 4 output bytes */ + for (len -= extra; len > 0; len -= 3) { + a = *read++; + b = *read++; + c = *read++; + + *write++ = base64_encode[a >> 2]; + *write++ = base64_encode[(a & 0x03) << 4 | b >> 4]; + *write++ = base64_encode[(b & 0x0f) << 2 | c >> 6]; + *write++ = base64_encode[c & 0x3f]; + } + + if (extra > 0) { + a = *read++; + b = (extra > 1) ? *read++ : 0; + + *write++ = base64_encode[a >> 2]; + *write++ = base64_encode[(a & 0x03) << 4 | b >> 4]; + *write++ = (extra > 1) ? base64_encode[(b & 0x0f) << 2] : '='; + *write++ = '='; + } + + buf->size = ((char *)write) - buf->ptr; + buf->ptr[buf->size] = '\0'; + + return 0; +} + +/* The inverse of base64_encode */ +static const int8_t base64_decode[] = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1, + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, + -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 +}; + +int git_str_decode_base64(git_str *buf, const char *base64, size_t len) +{ + size_t i; + int8_t a, b, c, d; + size_t orig_size = buf->size, new_size; + + if (len % 4) { + git_error_set(GIT_ERROR_INVALID, "invalid base64 input"); + return -1; + } + + GIT_ASSERT_ARG(len % 4 == 0); + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, (len / 4 * 3), buf->size); + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); + ENSURE_SIZE(buf, new_size); + + for (i = 0; i < len; i += 4) { + if ((a = base64_decode[(unsigned char)base64[i]]) < 0 || + (b = base64_decode[(unsigned char)base64[i+1]]) < 0 || + (c = base64_decode[(unsigned char)base64[i+2]]) < 0 || + (d = base64_decode[(unsigned char)base64[i+3]]) < 0) { + buf->size = orig_size; + buf->ptr[buf->size] = '\0'; + + git_error_set(GIT_ERROR_INVALID, "invalid base64 input"); + return -1; + } + + buf->ptr[buf->size++] = ((a << 2) | (b & 0x30) >> 4); + buf->ptr[buf->size++] = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2); + buf->ptr[buf->size++] = (c & 0x03) << 6 | (d & 0x3f); + } + + buf->ptr[buf->size] = '\0'; + return 0; +} + +static const char base85_encode[] = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"; + +int git_str_encode_base85(git_str *buf, const char *data, size_t len) +{ + size_t blocks = (len / 4) + !!(len % 4), alloclen; + + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloclen, blocks, 5); + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, buf->size); + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); + + ENSURE_SIZE(buf, alloclen); + + while (len) { + uint32_t acc = 0; + char b85[5]; + int i; + + for (i = 24; i >= 0; i -= 8) { + uint8_t ch = *data++; + acc |= (uint32_t)ch << i; + + if (--len == 0) + break; + } + + for (i = 4; i >= 0; i--) { + int val = acc % 85; + acc /= 85; + + b85[i] = base85_encode[val]; + } + + for (i = 0; i < 5; i++) + buf->ptr[buf->size++] = b85[i]; + } + + buf->ptr[buf->size] = '\0'; + + return 0; +} + +/* The inverse of base85_encode */ +static const int8_t base85_decode[] = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, 63, -1, 64, 65, 66, 67, -1, 68, 69, 70, 71, -1, 72, -1, -1, + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1, 73, 74, 75, 76, 77, + 78, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, + 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, -1, -1, -1, 79, 80, + 81, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 82, 83, 84, 85, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 +}; + +int git_str_decode_base85( + git_str *buf, + const char *base85, + size_t base85_len, + size_t output_len) +{ + size_t orig_size = buf->size, new_size; + + if (base85_len % 5 || + output_len > base85_len * 4 / 5) { + git_error_set(GIT_ERROR_INVALID, "invalid base85 input"); + return -1; + } + + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, output_len, buf->size); + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); + ENSURE_SIZE(buf, new_size); + + while (output_len) { + unsigned acc = 0; + int de, cnt = 4; + unsigned char ch; + do { + ch = *base85++; + de = base85_decode[ch]; + if (--de < 0) + goto on_error; + + acc = acc * 85 + de; + } while (--cnt); + ch = *base85++; + de = base85_decode[ch]; + if (--de < 0) + goto on_error; + + /* Detect overflow. */ + if (0xffffffff / 85 < acc || + 0xffffffff - de < (acc *= 85)) + goto on_error; + + acc += de; + + cnt = (output_len < 4) ? (int)output_len : 4; + output_len -= cnt; + do { + acc = (acc << 8) | (acc >> 24); + buf->ptr[buf->size++] = acc; + } while (--cnt); + } + + buf->ptr[buf->size] = 0; + + return 0; + +on_error: + buf->size = orig_size; + buf->ptr[buf->size] = '\0'; + + git_error_set(GIT_ERROR_INVALID, "invalid base85 input"); + return -1; +} + +#define HEX_DECODE(c) ((c | 32) % 39 - 9) + +int git_str_decode_percent( + git_str *buf, + const char *str, + size_t str_len) +{ + size_t str_pos, new_size; + + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, str_len); + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); + ENSURE_SIZE(buf, new_size); + + for (str_pos = 0; str_pos < str_len; buf->size++, str_pos++) { + if (str[str_pos] == '%' && + str_len > str_pos + 2 && + git__isxdigit(str[str_pos + 1]) && + git__isxdigit(str[str_pos + 2])) { + buf->ptr[buf->size] = (HEX_DECODE(str[str_pos + 1]) << 4) + + HEX_DECODE(str[str_pos + 2]); + str_pos += 2; + } else { + buf->ptr[buf->size] = str[str_pos]; + } + } + + buf->ptr[buf->size] = '\0'; + return 0; +} + +int git_str_vprintf(git_str *buf, const char *format, va_list ap) +{ + size_t expected_size, new_size; + int len; + + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&expected_size, strlen(format), 2); + GIT_ERROR_CHECK_ALLOC_ADD(&expected_size, expected_size, buf->size); + ENSURE_SIZE(buf, expected_size); + + while (1) { + va_list args; + va_copy(args, ap); + + len = p_vsnprintf( + buf->ptr + buf->size, + buf->asize - buf->size, + format, args + ); + + va_end(args); + + if (len < 0) { + git__free(buf->ptr); + buf->ptr = git_str__oom; + return -1; + } + + if ((size_t)len + 1 <= buf->asize - buf->size) { + buf->size += len; + break; + } + + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, len); + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); + ENSURE_SIZE(buf, new_size); + } + + return 0; +} + +int git_str_printf(git_str *buf, const char *format, ...) +{ + int r; + va_list ap; + + va_start(ap, format); + r = git_str_vprintf(buf, format, ap); + va_end(ap); + + return r; +} + +int git_str_copy_cstr(char *data, size_t datasize, const git_str *buf) +{ + size_t copylen; + + GIT_ASSERT_ARG(data); + GIT_ASSERT_ARG(datasize); + GIT_ASSERT_ARG(buf); + + data[0] = '\0'; + + if (buf->size == 0 || buf->asize <= 0) + return 0; + + copylen = buf->size; + if (copylen > datasize - 1) + copylen = datasize - 1; + memmove(data, buf->ptr, copylen); + data[copylen] = '\0'; + + return 0; +} + +void git_str_consume_bytes(git_str *buf, size_t len) +{ + git_str_consume(buf, buf->ptr + len); +} + +void git_str_consume(git_str *buf, const char *end) +{ + if (end > buf->ptr && end <= buf->ptr + buf->size) { + size_t consumed = end - buf->ptr; + memmove(buf->ptr, end, buf->size - consumed); + buf->size -= consumed; + buf->ptr[buf->size] = '\0'; + } +} + +void git_str_truncate(git_str *buf, size_t len) +{ + if (len >= buf->size) + return; + + buf->size = len; + if (buf->size < buf->asize) + buf->ptr[buf->size] = '\0'; +} + +void git_str_shorten(git_str *buf, size_t amount) +{ + if (buf->size > amount) + git_str_truncate(buf, buf->size - amount); + else + git_str_clear(buf); +} + +void git_str_truncate_at_char(git_str *buf, char separator) +{ + ssize_t idx = git_str_find(buf, separator); + if (idx >= 0) + git_str_truncate(buf, (size_t)idx); +} + +void git_str_rtruncate_at_char(git_str *buf, char separator) +{ + ssize_t idx = git_str_rfind_next(buf, separator); + git_str_truncate(buf, idx < 0 ? 0 : (size_t)idx); +} + +void git_str_swap(git_str *str_a, git_str *str_b) +{ + git_str t = *str_a; + *str_a = *str_b; + *str_b = t; +} + +char *git_str_detach(git_str *buf) +{ + char *data = buf->ptr; + + if (buf->asize == 0 || buf->ptr == git_str__oom) + return NULL; + + git_str_init(buf, 0); + + return data; +} + +int git_str_attach(git_str *buf, char *ptr, size_t asize) +{ + git_str_dispose(buf); + + if (ptr) { + buf->ptr = ptr; + buf->size = strlen(ptr); + if (asize) + buf->asize = (asize < buf->size) ? buf->size + 1 : asize; + else /* pass 0 to fall back on strlen + 1 */ + buf->asize = buf->size + 1; + } + + ENSURE_SIZE(buf, asize); + return 0; +} + +void git_str_attach_notowned(git_str *buf, const char *ptr, size_t size) +{ + if (git_str_is_allocated(buf)) + git_str_dispose(buf); + + if (!size) { + git_str_init(buf, 0); + } else { + buf->ptr = (char *)ptr; + buf->asize = 0; + buf->size = size; + } +} + +int git_str_join_n(git_str *buf, char separator, int nbuf, ...) +{ + va_list ap; + int i; + size_t total_size = 0, original_size = buf->size; + char *out, *original = buf->ptr; + + if (buf->size > 0 && buf->ptr[buf->size - 1] != separator) + ++total_size; /* space for initial separator */ + + /* Make two passes to avoid multiple reallocation */ + + va_start(ap, nbuf); + for (i = 0; i < nbuf; ++i) { + const char *segment; + size_t segment_len; + + segment = va_arg(ap, const char *); + if (!segment) + continue; + + segment_len = strlen(segment); + + GIT_ERROR_CHECK_ALLOC_ADD(&total_size, total_size, segment_len); + + if (segment_len == 0 || segment[segment_len - 1] != separator) + GIT_ERROR_CHECK_ALLOC_ADD(&total_size, total_size, 1); + } + va_end(ap); + + /* expand buffer if needed */ + if (total_size == 0) + return 0; + + GIT_ERROR_CHECK_ALLOC_ADD(&total_size, total_size, 1); + if (git_str_grow_by(buf, total_size) < 0) + return -1; + + out = buf->ptr + buf->size; + + /* append separator to existing buf if needed */ + if (buf->size > 0 && out[-1] != separator) + *out++ = separator; + + va_start(ap, nbuf); + for (i = 0; i < nbuf; ++i) { + const char *segment; + size_t segment_len; + + segment = va_arg(ap, const char *); + if (!segment) + continue; + + /* deal with join that references buffer's original content */ + if (segment >= original && segment < original + original_size) { + size_t offset = (segment - original); + segment = buf->ptr + offset; + segment_len = original_size - offset; + } else { + segment_len = strlen(segment); + } + + /* skip leading separators */ + if (out > buf->ptr && out[-1] == separator) + while (segment_len > 0 && *segment == separator) { + segment++; + segment_len--; + } + + /* copy over next buffer */ + if (segment_len > 0) { + memmove(out, segment, segment_len); + out += segment_len; + } + + /* append trailing separator (except for last item) */ + if (i < nbuf - 1 && out > buf->ptr && out[-1] != separator) + *out++ = separator; + } + va_end(ap); + + /* set size based on num characters actually written */ + buf->size = out - buf->ptr; + buf->ptr[buf->size] = '\0'; + + return 0; +} + +int git_str_join( + git_str *buf, + char separator, + const char *str_a, + const char *str_b) +{ + size_t strlen_a = str_a ? strlen(str_a) : 0; + size_t strlen_b = strlen(str_b); + size_t alloc_len; + int need_sep = 0; + ssize_t offset_a = -1; + + /* not safe to have str_b point internally to the buffer */ + if (buf->size) + GIT_ASSERT_ARG(str_b < buf->ptr || str_b >= buf->ptr + buf->size); + + /* figure out if we need to insert a separator */ + if (separator && strlen_a) { + while (*str_b == separator) { str_b++; strlen_b--; } + if (str_a[strlen_a - 1] != separator) + need_sep = 1; + } + + /* str_a could be part of the buffer */ + if (buf->size && str_a >= buf->ptr && str_a < buf->ptr + buf->size) + offset_a = str_a - buf->ptr; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, strlen_a, strlen_b); + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, need_sep); + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1); + ENSURE_SIZE(buf, alloc_len); + + /* fix up internal pointers */ + if (offset_a >= 0) + str_a = buf->ptr + offset_a; + + /* do the actual copying */ + if (offset_a != 0 && str_a) + memmove(buf->ptr, str_a, strlen_a); + if (need_sep) + buf->ptr[strlen_a] = separator; + memcpy(buf->ptr + strlen_a + need_sep, str_b, strlen_b); + + buf->size = strlen_a + strlen_b + need_sep; + buf->ptr[buf->size] = '\0'; + + return 0; +} + +int git_str_join3( + git_str *buf, + char separator, + const char *str_a, + const char *str_b, + const char *str_c) +{ + size_t len_a = strlen(str_a), + len_b = strlen(str_b), + len_c = strlen(str_c), + len_total; + int sep_a = 0, sep_b = 0; + char *tgt; + + /* for this function, disallow pointers into the existing buffer */ + GIT_ASSERT(str_a < buf->ptr || str_a >= buf->ptr + buf->size); + GIT_ASSERT(str_b < buf->ptr || str_b >= buf->ptr + buf->size); + GIT_ASSERT(str_c < buf->ptr || str_c >= buf->ptr + buf->size); + + if (separator) { + if (len_a > 0) { + while (*str_b == separator) { str_b++; len_b--; } + sep_a = (str_a[len_a - 1] != separator); + } + if (len_a > 0 || len_b > 0) + while (*str_c == separator) { str_c++; len_c--; } + if (len_b > 0) + sep_b = (str_b[len_b - 1] != separator); + } + + GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_a, sep_a); + GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_total, len_b); + GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_total, sep_b); + GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_total, len_c); + GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_total, 1); + ENSURE_SIZE(buf, len_total); + + tgt = buf->ptr; + + if (len_a) { + memcpy(tgt, str_a, len_a); + tgt += len_a; + } + if (sep_a) + *tgt++ = separator; + if (len_b) { + memcpy(tgt, str_b, len_b); + tgt += len_b; + } + if (sep_b) + *tgt++ = separator; + if (len_c) + memcpy(tgt, str_c, len_c); + + buf->size = len_a + sep_a + len_b + sep_b + len_c; + buf->ptr[buf->size] = '\0'; + + return 0; +} + +void git_str_rtrim(git_str *buf) +{ + while (buf->size > 0) { + if (!git__isspace(buf->ptr[buf->size - 1])) + break; + + buf->size--; + } + + if (buf->asize > buf->size) + buf->ptr[buf->size] = '\0'; +} + +int git_str_cmp(const git_str *a, const git_str *b) +{ + int result = memcmp(a->ptr, b->ptr, min(a->size, b->size)); + return (result != 0) ? result : + (a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0; +} + +int git_str_splice( + git_str *buf, + size_t where, + size_t nb_to_remove, + const char *data, + size_t nb_to_insert) +{ + char *splice_loc; + size_t new_size, alloc_size; + + GIT_ASSERT(buf); + GIT_ASSERT(where <= buf->size); + GIT_ASSERT(nb_to_remove <= buf->size - where); + + splice_loc = buf->ptr + where; + + /* Ported from git.git + * https://github.com/git/git/blob/16eed7c/strbuf.c#L159-176 + */ + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, (buf->size - nb_to_remove), nb_to_insert); + GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, new_size, 1); + ENSURE_SIZE(buf, alloc_size); + + memmove(splice_loc + nb_to_insert, + splice_loc + nb_to_remove, + buf->size - where - nb_to_remove); + + memcpy(splice_loc, data, nb_to_insert); + + buf->size = new_size; + buf->ptr[buf->size] = '\0'; + return 0; +} + +/* Quote per http://marc.info/?l=git&m=112927316408690&w=2 */ +int git_str_quote(git_str *buf) +{ + const char whitespace[] = { 'a', 'b', 't', 'n', 'v', 'f', 'r' }; + git_str quoted = GIT_STR_INIT; + size_t i = 0; + bool quote = false; + int error = 0; + + /* walk to the first char that needs quoting */ + if (buf->size && buf->ptr[0] == '!') + quote = true; + + for (i = 0; !quote && i < buf->size; i++) { + if (buf->ptr[i] == '"' || buf->ptr[i] == '\\' || + buf->ptr[i] < ' ' || buf->ptr[i] > '~') { + quote = true; + break; + } + } + + if (!quote) + goto done; + + git_str_putc("ed, '"'); + git_str_put("ed, buf->ptr, i); + + for (; i < buf->size; i++) { + /* whitespace - use the map above, which is ordered by ascii value */ + if (buf->ptr[i] >= '\a' && buf->ptr[i] <= '\r') { + git_str_putc("ed, '\\'); + git_str_putc("ed, whitespace[buf->ptr[i] - '\a']); + } + + /* double quote and backslash must be escaped */ + else if (buf->ptr[i] == '"' || buf->ptr[i] == '\\') { + git_str_putc("ed, '\\'); + git_str_putc("ed, buf->ptr[i]); + } + + /* escape anything unprintable as octal */ + else if (buf->ptr[i] != ' ' && + (buf->ptr[i] < '!' || buf->ptr[i] > '~')) { + git_str_printf("ed, "\\%03o", (unsigned char)buf->ptr[i]); + } + + /* yay, printable! */ + else { + git_str_putc("ed, buf->ptr[i]); + } + } + + git_str_putc("ed, '"'); + + if (git_str_oom("ed)) { + error = -1; + goto done; + } + + git_str_swap("ed, buf); + +done: + git_str_dispose("ed); + return error; +} + +/* Unquote per http://marc.info/?l=git&m=112927316408690&w=2 */ +int git_str_unquote(git_str *buf) +{ + size_t i, j; + char ch; + + git_str_rtrim(buf); + + if (buf->size < 2 || buf->ptr[0] != '"' || buf->ptr[buf->size-1] != '"') + goto invalid; + + for (i = 0, j = 1; j < buf->size-1; i++, j++) { + ch = buf->ptr[j]; + + if (ch == '\\') { + if (j == buf->size-2) + goto invalid; + + ch = buf->ptr[++j]; + + switch (ch) { + /* \" or \\ simply copy the char in */ + case '"': case '\\': + break; + + /* add the appropriate escaped char */ + case 'a': ch = '\a'; break; + case 'b': ch = '\b'; break; + case 'f': ch = '\f'; break; + case 'n': ch = '\n'; break; + case 'r': ch = '\r'; break; + case 't': ch = '\t'; break; + case 'v': ch = '\v'; break; + + /* \xyz digits convert to the char*/ + case '0': case '1': case '2': case '3': + if (j == buf->size-3) { + git_error_set(GIT_ERROR_INVALID, + "truncated quoted character \\%c", ch); + return -1; + } + + if (buf->ptr[j+1] < '0' || buf->ptr[j+1] > '7' || + buf->ptr[j+2] < '0' || buf->ptr[j+2] > '7') { + git_error_set(GIT_ERROR_INVALID, + "truncated quoted character \\%c%c%c", + buf->ptr[j], buf->ptr[j+1], buf->ptr[j+2]); + return -1; + } + + ch = ((buf->ptr[j] - '0') << 6) | + ((buf->ptr[j+1] - '0') << 3) | + (buf->ptr[j+2] - '0'); + j += 2; + break; + + default: + git_error_set(GIT_ERROR_INVALID, "invalid quoted character \\%c", ch); + return -1; + } + } + + buf->ptr[i] = ch; + } + + buf->ptr[i] = '\0'; + buf->size = i; + + return 0; + +invalid: + git_error_set(GIT_ERROR_INVALID, "invalid quoted line"); + return -1; +} + +int git_str_puts_escaped( + git_str *buf, + const char *string, + const char *esc_chars, + const char *esc_prefix, + const char *esc_suffix) +{ + const char *scan; + size_t total = 0, count, alloclen; + size_t esc_prefix_len = esc_prefix ? strlen(esc_prefix) : 0; + size_t esc_suffix_len = esc_suffix ? strlen(esc_suffix) : 0; + + if (!string) + return 0; + + for (scan = string; *scan; ) { + /* count run of non-escaped characters */ + count = strcspn(scan, esc_chars); + total += count; + scan += count; + /* count run of escaped characters */ + count = strspn(scan, esc_chars); + total += count * (esc_prefix_len + esc_suffix_len + 1); + scan += count; + } + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, total, 1); + if (git_str_grow_by(buf, alloclen) < 0) + return -1; + + for (scan = string; *scan; ) { + count = strcspn(scan, esc_chars); + + memmove(buf->ptr + buf->size, scan, count); + scan += count; + buf->size += count; + + for (count = strspn(scan, esc_chars); count > 0; --count) { + /* copy escape prefix sequence */ + if (esc_prefix) { + memmove(buf->ptr + buf->size, esc_prefix, esc_prefix_len); + buf->size += esc_prefix_len; + } + + /* copy character to be escaped */ + buf->ptr[buf->size] = *scan; + buf->size++; + scan++; + + /* copy escape suffix sequence */ + if (esc_suffix) { + memmove(buf->ptr + buf->size, esc_suffix, esc_suffix_len); + buf->size += esc_suffix_len; + } + } + } + + buf->ptr[buf->size] = '\0'; + + return 0; +} + +void git_str_unescape(git_str *buf) +{ + buf->size = git__unescape(buf->ptr); +} + +int git_str_crlf_to_lf(git_str *tgt, const git_str *src) +{ + const char *scan = src->ptr; + const char *scan_end = src->ptr + src->size; + const char *next = memchr(scan, '\r', src->size); + size_t new_size; + char *out; + + GIT_ASSERT(tgt != src); + + if (!next) + return git_str_set(tgt, src->ptr, src->size); + + /* reduce reallocs while in the loop */ + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, src->size, 1); + if (git_str_grow(tgt, new_size) < 0) + return -1; + + out = tgt->ptr; + tgt->size = 0; + + /* Find the next \r and copy whole chunk up to there to tgt */ + for (; next; scan = next + 1, next = memchr(scan, '\r', scan_end - scan)) { + if (next > scan) { + size_t copylen = (size_t)(next - scan); + memcpy(out, scan, copylen); + out += copylen; + } + + /* Do not drop \r unless it is followed by \n */ + if (next + 1 == scan_end || next[1] != '\n') + *out++ = '\r'; + } + + /* Copy remaining input into dest */ + if (scan < scan_end) { + size_t remaining = (size_t)(scan_end - scan); + memcpy(out, scan, remaining); + out += remaining; + } + + tgt->size = (size_t)(out - tgt->ptr); + tgt->ptr[tgt->size] = '\0'; + + return 0; +} + +int git_str_lf_to_crlf(git_str *tgt, const git_str *src) +{ + const char *start = src->ptr; + const char *end = start + src->size; + const char *scan = start; + const char *next = memchr(scan, '\n', src->size); + size_t alloclen; + + GIT_ASSERT(tgt != src); + + if (!next) + return git_str_set(tgt, src->ptr, src->size); + + /* attempt to reduce reallocs while in the loop */ + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, src->size, src->size >> 4); + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); + if (git_str_grow(tgt, alloclen) < 0) + return -1; + tgt->size = 0; + + for (; next; scan = next + 1, next = memchr(scan, '\n', end - scan)) { + size_t copylen = next - scan; + + /* if we find mixed line endings, carry on */ + if (copylen && next[-1] == '\r') + copylen--; + + GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, copylen, 3); + if (git_str_grow_by(tgt, alloclen) < 0) + return -1; + + if (copylen) { + memcpy(tgt->ptr + tgt->size, scan, copylen); + tgt->size += copylen; + } + + tgt->ptr[tgt->size++] = '\r'; + tgt->ptr[tgt->size++] = '\n'; + } + + tgt->ptr[tgt->size] = '\0'; + return git_str_put(tgt, scan, end - scan); +} + +int git_str_common_prefix(git_str *buf, char *const *const strings, size_t count) +{ + size_t i; + const char *str, *pfx; + + git_str_clear(buf); + + if (!strings || !count) + return 0; + + /* initialize common prefix to first string */ + if (git_str_sets(buf, strings[0]) < 0) + return -1; + + /* go through the rest of the strings, truncating to shared prefix */ + for (i = 1; i < count; ++i) { + + for (str = strings[i], pfx = buf->ptr; + *str && *str == *pfx; + str++, pfx++) + /* scanning */; + + git_str_truncate(buf, pfx - buf->ptr); + + if (!buf->size) + break; + } + + return 0; +} + +int git_str_is_binary(const git_str *buf) +{ + const char *scan = buf->ptr, *end = buf->ptr + buf->size; + git_str_bom_t bom; + int printable = 0, nonprintable = 0; + + scan += git_str_detect_bom(&bom, buf); + + if (bom > GIT_STR_BOM_UTF8) + return 1; + + while (scan < end) { + unsigned char c = *scan++; + + /* Printable characters are those above SPACE (0x1F) excluding DEL, + * and including BS, ESC and FF. + */ + if ((c > 0x1F && c != 127) || c == '\b' || c == '\033' || c == '\014') + printable++; + else if (c == '\0') + return true; + else if (!git__isspace(c)) + nonprintable++; + } + + return ((printable >> 7) < nonprintable); +} + +int git_str_contains_nul(const git_str *buf) +{ + return (memchr(buf->ptr, '\0', buf->size) != NULL); +} + +int git_str_detect_bom(git_str_bom_t *bom, const git_str *buf) +{ + const char *ptr; + size_t len; + + *bom = GIT_STR_BOM_NONE; + /* need at least 2 bytes to look for any BOM */ + if (buf->size < 2) + return 0; + + ptr = buf->ptr; + len = buf->size; + + switch (*ptr++) { + case 0: + if (len >= 4 && ptr[0] == 0 && ptr[1] == '\xFE' && ptr[2] == '\xFF') { + *bom = GIT_STR_BOM_UTF32_BE; + return 4; + } + break; + case '\xEF': + if (len >= 3 && ptr[0] == '\xBB' && ptr[1] == '\xBF') { + *bom = GIT_STR_BOM_UTF8; + return 3; + } + break; + case '\xFE': + if (*ptr == '\xFF') { + *bom = GIT_STR_BOM_UTF16_BE; + return 2; + } + break; + case '\xFF': + if (*ptr != '\xFE') + break; + if (len >= 4 && ptr[1] == 0 && ptr[2] == 0) { + *bom = GIT_STR_BOM_UTF32_LE; + return 4; + } else { + *bom = GIT_STR_BOM_UTF16_LE; + return 2; + } + break; + default: + break; + } + + return 0; +} + +bool git_str_gather_text_stats( + git_str_text_stats *stats, const git_str *buf, bool skip_bom) +{ + const char *scan = buf->ptr, *end = buf->ptr + buf->size; + int skip; + + memset(stats, 0, sizeof(*stats)); + + /* BOM detection */ + skip = git_str_detect_bom(&stats->bom, buf); + if (skip_bom) + scan += skip; + + /* Ignore EOF character */ + if (buf->size > 0 && end[-1] == '\032') + end--; + + /* Counting loop */ + while (scan < end) { + unsigned char c = *scan++; + + if (c > 0x1F && c != 0x7F) + stats->printable++; + else switch (c) { + case '\0': + stats->nul++; + stats->nonprintable++; + break; + case '\n': + stats->lf++; + break; + case '\r': + stats->cr++; + if (scan < end && *scan == '\n') + stats->crlf++; + break; + case '\t': case '\f': case '\v': case '\b': case 0x1b: /*ESC*/ + stats->printable++; + break; + default: + stats->nonprintable++; + break; + } + } + + /* Treat files with a bare CR as binary */ + return (stats->cr != stats->crlf || stats->nul > 0 || + ((stats->printable >> 7) < stats->nonprintable)); +} diff --git a/src/util/str.h b/src/util/str.h new file mode 100644 index 00000000000..ad9b892cda3 --- /dev/null +++ b/src/util/str.h @@ -0,0 +1,359 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_str_h__ +#define INCLUDE_str_h__ + +#include "git2_util.h" + +struct git_str { + char *ptr; + size_t asize; + size_t size; +}; + +typedef enum { + GIT_STR_BOM_NONE = 0, + GIT_STR_BOM_UTF8 = 1, + GIT_STR_BOM_UTF16_LE = 2, + GIT_STR_BOM_UTF16_BE = 3, + GIT_STR_BOM_UTF32_LE = 4, + GIT_STR_BOM_UTF32_BE = 5 +} git_str_bom_t; + +typedef struct { + git_str_bom_t bom; /* BOM found at head of text */ + unsigned int nul, cr, lf, crlf; /* NUL, CR, LF and CRLF counts */ + unsigned int printable, nonprintable; /* These are just approximations! */ +} git_str_text_stats; + +extern char git_str__initstr[]; +extern char git_str__oom[]; + +/* Use to initialize string buffer structure when git_str is on stack */ +#define GIT_STR_INIT { git_str__initstr, 0, 0 } + +/** + * Static initializer for git_str from static string buffer + */ +#define GIT_STR_INIT_CONST(str, len) { (char *)(str), 0, (size_t)(len) } + +GIT_INLINE(bool) git_str_is_allocated(const git_str *str) +{ + return (str->ptr != NULL && str->asize > 0); +} + +/** + * Initialize a git_str structure. + * + * For the cases where GIT_STR_INIT cannot be used to do static + * initialization. + */ +extern int git_str_init(git_str *str, size_t initial_size); + +extern void git_str_dispose(git_str *str); + +/** + * Resize the string buffer allocation to make more space. + * + * This will attempt to grow the string buffer to accommodate the target + * size. The bstring buffer's `ptr` will be replaced with a newly + * allocated block of data. Be careful so that memory allocated by the + * caller is not lost. As a special variant, if you pass `target_size` as + * 0 and the memory is not allocated by libgit2, this will allocate a new + * buffer of size `size` and copy the external data into it. + * + * Currently, this will never shrink a buffer, only expand it. + * + * If the allocation fails, this will return an error and the buffer will be + * marked as invalid for future operations, invaliding the contents. + * + * @param str The buffer to be resized; may or may not be allocated yet + * @param target_size The desired available size + * @return 0 on success, -1 on allocation failure + */ +int git_str_grow(git_str *str, size_t target_size); + +/** + * Resize the buffer allocation to make more space. + * + * This will attempt to grow the string buffer to accommodate the + * additional size. It is similar to `git_str_grow`, but performs the + * new size calculation, checking for overflow. + * + * Like `git_str_grow`, if this is a user-supplied string buffer, + * this will allocate a new string uffer. + */ +extern int git_str_grow_by(git_str *str, size_t additional_size); + +/** + * Attempt to grow the buffer to hold at least `target_size` bytes. + * + * If the allocation fails, this will return an error. If `mark_oom` is + * true, this will mark the string buffer as invalid for future + * operations; if false, existing string buffer content will be preserved, + * but calling code must handle that string buffer was not expanded. If + * `preserve_external` is true, then any existing data pointed to be + * `ptr` even if `asize` is zero will be copied into the newly allocated + * string buffer. + */ +extern int git_str_try_grow( + git_str *str, size_t target_size, bool mark_oom); + +extern void git_str_swap(git_str *str_a, git_str *str_b); +extern char *git_str_detach(git_str *str); +extern int git_str_attach(git_str *str, char *ptr, size_t asize); + +/* Populates a `git_str` where the contents are not "owned" by the string + * buffer, and calls to `git_str_dispose` will not free the given str. + */ +extern void git_str_attach_notowned( + git_str *str, const char *ptr, size_t size); + +/** + * Test if there have been any reallocation failures with this git_str. + * + * Any function that writes to a git_str can fail due to memory allocation + * issues. If one fails, the git_str will be marked with an OOM error and + * further calls to modify the string buffer will fail. Check + * git_str_oom() at the end of your sequence and it will be true if you + * ran out of memory at any point with that string buffer. + * + * @return false if no error, true if allocation error + */ +GIT_INLINE(bool) git_str_oom(const git_str *str) +{ + return (str->ptr == git_str__oom); +} + +/* + * Functions below that return int value error codes will return 0 on + * success or -1 on failure (which generally means an allocation failed). + * Using a git_str where the allocation has failed with result in -1 from + * all further calls using that string buffer. As a result, you can + * ignore the return code of these functions and call them in a series + * then just call git_str_oom at the end. + */ + +int git_str_set(git_str *str, const void *data, size_t datalen); + +int git_str_sets(git_str *str, const char *string); +int git_str_putc(git_str *str, char c); +int git_str_putcn(git_str *str, char c, size_t len); +int git_str_put(git_str *str, const char *data, size_t len); +int git_str_puts(git_str *str, const char *string); +int git_str_printf(git_str *str, const char *format, ...) GIT_FORMAT_PRINTF(2, 3); +int git_str_vprintf(git_str *str, const char *format, va_list ap); +void git_str_clear(git_str *str); +void git_str_consume_bytes(git_str *str, size_t len); +void git_str_consume(git_str *str, const char *end); +void git_str_truncate(git_str *str, size_t len); +void git_str_shorten(git_str *str, size_t amount); +void git_str_truncate_at_char(git_str *path, char separator); +void git_str_rtruncate_at_char(git_str *path, char separator); + +/** General join with separator */ +int git_str_join_n(git_str *str, char separator, int len, ...); +/** Fast join of two strings - first may legally point into `str` data */ +int git_str_join(git_str *str, char separator, const char *str_a, const char *str_b); +/** Fast join of three strings - cannot reference `str` data */ +int git_str_join3(git_str *str, char separator, const char *str_a, const char *str_b, const char *str_c); + +/** + * Join two strings as paths, inserting a slash between as needed. + * @return 0 on success, -1 on failure + */ +GIT_INLINE(int) git_str_joinpath(git_str *str, const char *a, const char *b) +{ + return git_str_join(str, '/', a, b); +} + +GIT_INLINE(const char *) git_str_cstr(const git_str *str) +{ + return str->ptr; +} + +GIT_INLINE(size_t) git_str_len(const git_str *str) +{ + return str->size; +} + +int git_str_copy_cstr(char *data, size_t datasize, const git_str *str); + +#define git_str_PUTS(str, cstr) git_str_put(str, cstr, sizeof(cstr) - 1) + +GIT_INLINE(ssize_t) git_str_rfind_next(const git_str *str, char ch) +{ + ssize_t idx = (ssize_t)str->size - 1; + while (idx >= 0 && str->ptr[idx] == ch) idx--; + while (idx >= 0 && str->ptr[idx] != ch) idx--; + return idx; +} + +GIT_INLINE(ssize_t) git_str_rfind(const git_str *str, char ch) +{ + ssize_t idx = (ssize_t)str->size - 1; + while (idx >= 0 && str->ptr[idx] != ch) idx--; + return idx; +} + +GIT_INLINE(ssize_t) git_str_find(const git_str *str, char ch) +{ + void *found = memchr(str->ptr, ch, str->size); + return found ? (ssize_t)((const char *)found - str->ptr) : -1; +} + +/* Remove whitespace from the end of the string buffer */ +void git_str_rtrim(git_str *str); + +int git_str_cmp(const git_str *a, const git_str *b); + +/* Quote and unquote a string buffer as specified in + * http://marc.info/?l=git&m=112927316408690&w=2 + */ +int git_str_quote(git_str *str); +int git_str_unquote(git_str *str); + +/* Write data as a hex string */ +int git_str_encode_hexstr(git_str *str, const char *data, size_t len); + +/* Write data as base64 encoded in string buffer */ +int git_str_encode_base64(git_str *str, const char *data, size_t len); +/* Decode the given bas64 and write the result to the string buffer */ +int git_str_decode_base64(git_str *str, const char *base64, size_t len); + +/* Write data as "base85" encoded in string buffer */ +int git_str_encode_base85(git_str *str, const char *data, size_t len); +/* Decode the given "base85" and write the result to the string buffer */ +int git_str_decode_base85(git_str *str, const char *base64, size_t len, size_t output_len); + +/* + * Decode the given percent-encoded string and write the result to the + * string buffer. + */ +int git_str_decode_percent(git_str *str, const char *encoded, size_t len); + +/* + * Insert, remove or replace a portion of the string buffer. + * + * @param str The string buffer to work with + * + * @param where The location in the string buffer where the transformation + * should be applied. + * + * @param nb_to_remove The number of chars to be removed. 0 to not + * remove any character in the string buffer. + * + * @param data A pointer to the data which should be inserted. + * + * @param nb_to_insert The number of chars to be inserted. 0 to not + * insert any character from the string buffer. + * + * @return 0 or an error code. + */ +int git_str_splice( + git_str *str, + size_t where, + size_t nb_to_remove, + const char *data, + size_t nb_to_insert); + +/** + * Append string to string buffer, prefixing each character from + * `esc_chars` with `esc_with` string. + * + * @param str String buffer to append data to + * @param string String to escape and append + * @param esc_chars Characters to be escaped + * @param esc_prefix String to insert as prefix of each found character + * @param esc_suffix String to insert as suffix of each found character + * @return 0 on success, <0 on failure (probably allocation problem) + */ +extern int git_str_puts_escaped( + git_str *str, + const char *string, + const char *esc_chars, + const char *esc_prefix, + const char *esc_suffix); + +/** + * Append string escaping characters that are regex special + */ +GIT_INLINE(int) git_str_puts_escape_regex(git_str *str, const char *string) +{ + return git_str_puts_escaped(str, string, "^.[]$()|*+?{}\\", "\\", NULL); +} + +/** + * Unescape all characters in a string buffer in place + * + * I.e. remove backslashes + */ +extern void git_str_unescape(git_str *str); + +/** + * Replace all \r\n with \n. + * + * @return 0 on success, -1 on memory error + */ +extern int git_str_crlf_to_lf(git_str *tgt, const git_str *src); + +/** + * Replace all \n with \r\n. Does not modify existing \r\n. + * + * @return 0 on success, -1 on memory error + */ +extern int git_str_lf_to_crlf(git_str *tgt, const git_str *src); + +/** + * Fill string buffer with the common prefix of a array of strings + * + * String buffer will be set to empty if there is no common prefix + */ +extern int git_str_common_prefix(git_str *buf, char *const *const strings, size_t count); + +/** + * Check if a string buffer begins with a UTF BOM + * + * @param bom Set to the type of BOM detected or GIT_BOM_NONE + * @param str String buffer in which to check the first bytes for a BOM + * @return Number of bytes of BOM data (or 0 if no BOM found) + */ +extern int git_str_detect_bom(git_str_bom_t *bom, const git_str *str); + +/** + * Gather stats for a piece of text + * + * Fill the `stats` structure with counts of unreadable characters, carriage + * returns, etc, so it can be used in heuristics. This automatically skips + * a trailing EOF (\032 character). Also it will look for a BOM at the + * start of the text and can be told to skip that as well. + * + * @param stats Structure to be filled in + * @param str Text to process + * @param skip_bom Exclude leading BOM from stats if true + * @return Does the string buffer heuristically look like binary data + */ +extern bool git_str_gather_text_stats( + git_str_text_stats *stats, const git_str *str, bool skip_bom); + +/** +* Check quickly if string buffer looks like it contains binary data +* +* @param str string buffer to check +* @return 1 if string buffer looks like non-text data +*/ +int git_str_is_binary(const git_str *str); + +/** +* Check quickly if buffer contains a NUL byte +* +* @param str string buffer to check +* @return 1 if string buffer contains a NUL byte +*/ +int git_str_contains_nul(const git_str *str); + +#endif diff --git a/src/util/strlist.c b/src/util/strlist.c new file mode 100644 index 00000000000..df5640c2a1f --- /dev/null +++ b/src/util/strlist.c @@ -0,0 +1,108 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include + +#include "git2_util.h" +#include "vector.h" +#include "strlist.h" + +int git_strlist_copy(char ***out, const char **in, size_t len) +{ + char **dup; + size_t i; + + dup = git__calloc(len, sizeof(char *)); + GIT_ERROR_CHECK_ALLOC(dup); + + for (i = 0; i < len; i++) { + dup[i] = git__strdup(in[i]); + GIT_ERROR_CHECK_ALLOC(dup[i]); + } + + *out = dup; + return 0; +} + +int git_strlist_copy_with_null(char ***out, const char **in, size_t len) +{ + char **dup; + size_t new_len, i; + + GIT_ERROR_CHECK_ALLOC_ADD(&new_len, len, 1); + + dup = git__calloc(new_len, sizeof(char *)); + GIT_ERROR_CHECK_ALLOC(dup); + + for (i = 0; i < len; i++) { + dup[i] = git__strdup(in[i]); + GIT_ERROR_CHECK_ALLOC(dup[i]); + } + + *out = dup; + return 0; +} + +bool git_strlist_contains_prefix( + const char **strings, + size_t len, + const char *str, + size_t n) +{ + size_t i; + + for (i = 0; i < len; i++) { + if (strncmp(strings[i], str, n) == 0) + return true; + } + + return false; +} + +bool git_strlist_contains_key( + const char **strings, + size_t len, + const char *key, + char delimiter) +{ + const char *c; + + for (c = key; *c; c++) { + if (*c == delimiter) + break; + } + + return *c ? + git_strlist_contains_prefix(strings, len, key, (c - key)) : + false; +} + +void git_strlist_free(char **strings, size_t len) +{ + size_t i; + + if (!strings) + return; + + for (i = 0; i < len; i++) + git__free(strings[i]); + + git__free(strings); +} + +void git_strlist_free_with_null(char **strings) +{ + char **s; + + if (!strings) + return; + + for (s = strings; *s; s++) + git__free(*s); + + git__free(strings); +} diff --git a/src/util/strlist.h b/src/util/strlist.h new file mode 100644 index 00000000000..68fbf8fb263 --- /dev/null +++ b/src/util/strlist.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_runtime_h__ +#define INCLUDE_runtime_h__ + +#include "git2_util.h" + +extern int git_strlist_copy(char ***out, const char **in, size_t len); + +extern int git_strlist_copy_with_null( + char ***out, + const char **in, + size_t len); + +extern bool git_strlist_contains_prefix( + const char **strings, + size_t len, + const char *str, + size_t n); + +extern bool git_strlist_contains_key( + const char **strings, + size_t len, + const char *key, + char delimiter); + +extern void git_strlist_free(char **strings, size_t len); + +extern void git_strlist_free_with_null(char **strings); + +#endif diff --git a/src/util/strnlen.h b/src/util/strnlen.h new file mode 100644 index 00000000000..eecfe3c02e9 --- /dev/null +++ b/src/util/strnlen.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_strlen_h__ +#define INCLUDE_strlen_h__ + +#if defined(__MINGW32__) || defined(__sun) || defined(__APPLE__) || defined(__MidnightBSD__) ||\ + (defined(_MSC_VER) && _MSC_VER < 1500) +# define NO_STRNLEN +#endif + +#ifdef NO_STRNLEN +GIT_INLINE(size_t) p_strnlen(const char *s, size_t maxlen) { + const char *end = memchr(s, 0, maxlen); + return end ? (size_t)(end - s) : maxlen; +} +#else +# define p_strnlen strnlen +#endif + +#endif diff --git a/src/util/thread.c b/src/util/thread.c new file mode 100644 index 00000000000..bc7364f8c1a --- /dev/null +++ b/src/util/thread.c @@ -0,0 +1,140 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2_util.h" + +#if !defined(GIT_THREADS) + +#define TLSDATA_MAX 16 + +typedef struct { + void *value; + void (GIT_SYSTEM_CALL *destroy_fn)(void *); +} tlsdata_value; + +static tlsdata_value tlsdata_values[TLSDATA_MAX]; +static int tlsdata_cnt = 0; + +int git_tlsdata_init(git_tlsdata_key *key, void (GIT_SYSTEM_CALL *destroy_fn)(void *)) +{ + if (tlsdata_cnt >= TLSDATA_MAX) + return -1; + + tlsdata_values[tlsdata_cnt].value = NULL; + tlsdata_values[tlsdata_cnt].destroy_fn = destroy_fn; + + *key = tlsdata_cnt; + tlsdata_cnt++; + + return 0; +} + +int git_tlsdata_set(git_tlsdata_key key, void *value) +{ + if (key < 0 || key > tlsdata_cnt) + return -1; + + tlsdata_values[key].value = value; + return 0; +} + +void *git_tlsdata_get(git_tlsdata_key key) +{ + if (key < 0 || key > tlsdata_cnt) + return NULL; + + return tlsdata_values[key].value; +} + +int git_tlsdata_dispose(git_tlsdata_key key) +{ + void *value; + void (*destroy_fn)(void *) = NULL; + + if (key < 0 || key > tlsdata_cnt) + return -1; + + value = tlsdata_values[key].value; + destroy_fn = tlsdata_values[key].destroy_fn; + + tlsdata_values[key].value = NULL; + tlsdata_values[key].destroy_fn = NULL; + + if (value && destroy_fn) + destroy_fn(value); + + return 0; +} + +#elif defined(GIT_WIN32) + +int git_tlsdata_init(git_tlsdata_key *key, void (GIT_SYSTEM_CALL *destroy_fn)(void *)) +{ + DWORD fls_index = FlsAlloc(destroy_fn); + + if (fls_index == FLS_OUT_OF_INDEXES) + return -1; + + *key = fls_index; + return 0; +} + +int git_tlsdata_set(git_tlsdata_key key, void *value) +{ + if (!FlsSetValue(key, value)) + return -1; + + return 0; +} + +void *git_tlsdata_get(git_tlsdata_key key) +{ + return FlsGetValue(key); +} + +int git_tlsdata_dispose(git_tlsdata_key key) +{ + if (!FlsFree(key)) + return -1; + + return 0; +} + +#elif defined(_POSIX_THREADS) + +int git_tlsdata_init(git_tlsdata_key *key, void (GIT_SYSTEM_CALL *destroy_fn)(void *)) +{ + if (pthread_key_create(key, destroy_fn) != 0) + return -1; + + return 0; +} + +int git_tlsdata_set(git_tlsdata_key key, void *value) +{ + if (pthread_setspecific(key, value) != 0) + return -1; + + return 0; +} + +void *git_tlsdata_get(git_tlsdata_key key) +{ + return pthread_getspecific(key); +} + +int git_tlsdata_dispose(git_tlsdata_key key) +{ + if (pthread_key_delete(key) != 0) + return -1; + + return 0; +} + +#else +# error unknown threading model +#endif diff --git a/src/util/thread.h b/src/util/thread.h new file mode 100644 index 00000000000..c32554bfdf1 --- /dev/null +++ b/src/util/thread.h @@ -0,0 +1,480 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_thread_h__ +#define INCLUDE_thread_h__ + +#if defined(GIT_THREADS) + +#if defined(__clang__) + +# if (__clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 1)) +# error Atomic primitives do not exist on this version of clang; configure libgit2 with -DUSE_THREADS=OFF +# else +# define GIT_BUILTIN_ATOMIC +# endif + +#elif defined(__GNUC__) + +# if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)) +# error Atomic primitives do not exist on this version of gcc; configure libgit2 with -DUSE_THREADS=OFF +# elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) +# define GIT_BUILTIN_ATOMIC +# else +# define GIT_BUILTIN_SYNC +# endif + +#endif + +#endif /* GIT_THREADS */ + +/* Common operations even if threading has been disabled */ +typedef struct { +#if defined(GIT_WIN32) + volatile long val; +#else + volatile int val; +#endif +} git_atomic32; + +#ifdef GIT_ARCH_64 + +typedef struct { +#if defined(GIT_WIN32) + volatile __int64 val; +#else + volatile int64_t val; +#endif +} git_atomic64; + +typedef git_atomic64 git_atomic_ssize; + +#define git_atomic_ssize_set git_atomic64_set +#define git_atomic_ssize_add git_atomic64_add +#define git_atomic_ssize_get git_atomic64_get + +#else + +typedef git_atomic32 git_atomic_ssize; + +#define git_atomic_ssize_set git_atomic32_set +#define git_atomic_ssize_add git_atomic32_add +#define git_atomic_ssize_get git_atomic32_get + +#endif + +#ifdef GIT_THREADS + +#ifdef GIT_WIN32 +# include "win32/thread.h" +#else +# include "unix/pthread.h" +#endif + +/* + * Atomically sets the contents of *a to be val. + */ +GIT_INLINE(void) git_atomic32_set(git_atomic32 *a, int val) +{ +#if defined(GIT_WIN32) + InterlockedExchange(&a->val, (LONG)val); +#elif defined(GIT_BUILTIN_ATOMIC) + __atomic_store_n(&a->val, val, __ATOMIC_SEQ_CST); +#elif defined(GIT_BUILTIN_SYNC) + __sync_lock_test_and_set(&a->val, val); +#else +# error "Unsupported architecture for atomic operations" +#endif +} + +/* + * Atomically increments the contents of *a by 1, and stores the result back into *a. + * @return the result of the operation. + */ +GIT_INLINE(int) git_atomic32_inc(git_atomic32 *a) +{ +#if defined(GIT_WIN32) + return InterlockedIncrement(&a->val); +#elif defined(GIT_BUILTIN_ATOMIC) + return __atomic_add_fetch(&a->val, 1, __ATOMIC_SEQ_CST); +#elif defined(GIT_BUILTIN_SYNC) + return __sync_add_and_fetch(&a->val, 1); +#else +# error "Unsupported architecture for atomic operations" +#endif +} + +/* + * Atomically adds the contents of *a and addend, and stores the result back into *a. + * @return the result of the operation. + */ +GIT_INLINE(int) git_atomic32_add(git_atomic32 *a, int32_t addend) +{ +#if defined(GIT_WIN32) + return InterlockedAdd(&a->val, addend); +#elif defined(GIT_BUILTIN_ATOMIC) + return __atomic_add_fetch(&a->val, addend, __ATOMIC_SEQ_CST); +#elif defined(GIT_BUILTIN_SYNC) + return __sync_add_and_fetch(&a->val, addend); +#else +# error "Unsupported architecture for atomic operations" +#endif +} + +/* + * Atomically decrements the contents of *a by 1, and stores the result back into *a. + * @return the result of the operation. + */ +GIT_INLINE(int) git_atomic32_dec(git_atomic32 *a) +{ +#if defined(GIT_WIN32) + return InterlockedDecrement(&a->val); +#elif defined(GIT_BUILTIN_ATOMIC) + return __atomic_sub_fetch(&a->val, 1, __ATOMIC_SEQ_CST); +#elif defined(GIT_BUILTIN_SYNC) + return __sync_sub_and_fetch(&a->val, 1); +#else +# error "Unsupported architecture for atomic operations" +#endif +} + +/* + * Atomically gets the contents of *a. + * @return the contents of *a. + */ +GIT_INLINE(int) git_atomic32_get(git_atomic32 *a) +{ +#if defined(GIT_WIN32) + return (int)InterlockedCompareExchange(&a->val, 0, 0); +#elif defined(GIT_BUILTIN_ATOMIC) + return __atomic_load_n(&a->val, __ATOMIC_SEQ_CST); +#elif defined(GIT_BUILTIN_SYNC) + return __sync_val_compare_and_swap(&a->val, 0, 0); +#else +# error "Unsupported architecture for atomic operations" +#endif +} + +GIT_INLINE(void *) git_atomic__compare_and_swap( + void * volatile *ptr, void *oldval, void *newval) +{ +#if defined(GIT_WIN32) + return InterlockedCompareExchangePointer((volatile PVOID *)ptr, newval, oldval); +#elif defined(GIT_BUILTIN_ATOMIC) + void *foundval = oldval; + __atomic_compare_exchange(ptr, &foundval, &newval, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); + return foundval; +#elif defined(GIT_BUILTIN_SYNC) + return __sync_val_compare_and_swap(ptr, oldval, newval); +#else +# error "Unsupported architecture for atomic operations" +#endif +} + +GIT_INLINE(volatile void *) git_atomic__swap( + void * volatile *ptr, void *newval) +{ +#if defined(GIT_WIN32) + return InterlockedExchangePointer(ptr, newval); +#elif defined(GIT_BUILTIN_ATOMIC) + void * foundval = NULL; + __atomic_exchange(ptr, &newval, &foundval, __ATOMIC_SEQ_CST); + return foundval; +#elif defined(GIT_BUILTIN_SYNC) + return (volatile void *)__sync_lock_test_and_set(ptr, newval); +#else +# error "Unsupported architecture for atomic operations" +#endif +} + +GIT_INLINE(volatile void *) git_atomic__load(void * volatile *ptr) +{ +#if defined(GIT_WIN32) + void *newval = NULL, *oldval = NULL; + return (volatile void *)InterlockedCompareExchangePointer((volatile PVOID *)ptr, newval, oldval); +#elif defined(GIT_BUILTIN_ATOMIC) + return (volatile void *)__atomic_load_n(ptr, __ATOMIC_SEQ_CST); +#elif defined(GIT_BUILTIN_SYNC) + return (volatile void *)__sync_val_compare_and_swap(ptr, 0, 0); +#else +# error "Unsupported architecture for atomic operations" +#endif +} + +#ifdef GIT_ARCH_64 + +/* + * Atomically adds the contents of *a and addend, and stores the result back into *a. + * @return the result of the operation. + */ +GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend) +{ +#if defined(GIT_WIN32) + return InterlockedAdd64(&a->val, addend); +#elif defined(GIT_BUILTIN_ATOMIC) + return __atomic_add_fetch(&a->val, addend, __ATOMIC_SEQ_CST); +#elif defined(GIT_BUILTIN_SYNC) + return __sync_add_and_fetch(&a->val, addend); +#else +# error "Unsupported architecture for atomic operations" +#endif +} + +/* + * Atomically sets the contents of *a to be val. + */ +GIT_INLINE(void) git_atomic64_set(git_atomic64 *a, int64_t val) +{ +#if defined(GIT_WIN32) + InterlockedExchange64(&a->val, val); +#elif defined(GIT_BUILTIN_ATOMIC) + __atomic_store_n(&a->val, val, __ATOMIC_SEQ_CST); +#elif defined(GIT_BUILTIN_SYNC) + __sync_lock_test_and_set(&a->val, val); +#else +# error "Unsupported architecture for atomic operations" +#endif +} + +/* + * Atomically gets the contents of *a. + * @return the contents of *a. + */ +GIT_INLINE(int64_t) git_atomic64_get(git_atomic64 *a) +{ +#if defined(GIT_WIN32) + return (int64_t)InterlockedCompareExchange64(&a->val, 0, 0); +#elif defined(GIT_BUILTIN_ATOMIC) + return __atomic_load_n(&a->val, __ATOMIC_SEQ_CST); +#elif defined(GIT_BUILTIN_SYNC) + return __sync_val_compare_and_swap(&a->val, 0, 0); +#else +# error "Unsupported architecture for atomic operations" +#endif +} + +#endif + +#else + +#define git_threads_global_init git__noop + +#define git_thread unsigned int +#define git_thread_create(t, s, a) git__noop(t, s, a) +#define git_thread_join(i, s) git__noop_args(i, s) + +/* Pthreads Mutex */ +#define git_mutex unsigned int +#define git_mutex_init(a) git__noop_args(a) +#define git_mutex_init(a) git__noop_args(a) +#define git_mutex_lock(a) git__noop_args(a) +#define git_mutex_unlock(a) git__noop_args(a) +#define git_mutex_free(a) git__noop_args(a) + +/* Pthreads condition vars */ +#define git_cond unsigned int +#define git_cond_init(c) git__noop_args(c) +#define git_cond_free(c) git__noop_args(c) +#define git_cond_wait(c, l) git__noop_args(c, l) +#define git_cond_signal(c) git__noop_args(c) +#define git_cond_broadcast(c) git__noop_args(c) + +/* Pthreads rwlock */ +#define git_rwlock unsigned int +#define git_rwlock_init(a) git__noop_args(a) +#define git_rwlock_rdlock(a) git__noop_args(a) +#define git_rwlock_rdunlock(a) git__noop_args(a) +#define git_rwlock_wrlock(a) git__noop_args(a) +#define git_rwlock_wrunlock(a) git__noop_args(a) +#define git_rwlock_free(a) git__noop_args(a) + +#define GIT_RWLOCK_STATIC_INIT 0 + + +GIT_INLINE(void) git_atomic32_set(git_atomic32 *a, int val) +{ + a->val = val; +} + +GIT_INLINE(int) git_atomic32_inc(git_atomic32 *a) +{ + return ++a->val; +} + +GIT_INLINE(int) git_atomic32_add(git_atomic32 *a, int32_t addend) +{ + a->val += addend; + return a->val; +} + +GIT_INLINE(int) git_atomic32_dec(git_atomic32 *a) +{ + return --a->val; +} + +GIT_INLINE(int) git_atomic32_get(git_atomic32 *a) +{ + return (int)a->val; +} + +GIT_INLINE(void *) git_atomic__compare_and_swap( + void * volatile *ptr, void *oldval, void *newval) +{ + void *foundval = *ptr; + if (foundval == oldval) + *ptr = newval; + return foundval; +} + +GIT_INLINE(volatile void *) git_atomic__swap( + void * volatile *ptr, void *newval) +{ + volatile void *old = *ptr; + *ptr = newval; + return old; +} + +GIT_INLINE(volatile void *) git_atomic__load(void * volatile *ptr) +{ + return *ptr; +} + +#ifdef GIT_ARCH_64 + +GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend) +{ + a->val += addend; + return a->val; +} + +GIT_INLINE(void) git_atomic64_set(git_atomic64 *a, int64_t val) +{ + a->val = val; +} + +GIT_INLINE(int64_t) git_atomic64_get(git_atomic64 *a) +{ + return (int64_t)a->val; +} + +#endif + +#endif + +/* + * Atomically replace the contents of *ptr (if they are equal to oldval) with + * newval. ptr must point to a pointer or a value that is the same size as a + * pointer. This is semantically compatible with: + * + * #define git_atomic_compare_and_swap(ptr, oldval, newval) \ + * ({ \ + * void *foundval = *ptr; \ + * if (foundval == oldval) \ + * *ptr = newval; \ + * foundval; \ + * }) + * + * @return the original contents of *ptr. + */ +#define git_atomic_compare_and_swap(ptr, oldval, newval) \ + git_atomic__compare_and_swap((void * volatile *)ptr, oldval, newval) + +/* + * Atomically replace the contents of v with newval. v must be the same size as + * a pointer. This is semantically compatible with: + * + * #define git_atomic_swap(v, newval) \ + * ({ \ + * volatile void *old = v; \ + * v = newval; \ + * old; \ + * }) + * + * @return the original contents of v. + */ +#define git_atomic_swap(v, newval) \ + (void *)git_atomic__swap((void * volatile *)&(v), newval) + +/* + * Atomically reads the contents of v. v must be the same size as a pointer. + * This is semantically compatible with: + * + * #define git_atomic_load(v) v + * + * @return the contents of v. + */ +#define git_atomic_load(v) \ + (void *)git_atomic__load((void * volatile *)&(v)) + +#if defined(GIT_THREADS) + +# if defined(GIT_WIN32) +# define GIT_MEMORY_BARRIER MemoryBarrier() +# elif defined(GIT_BUILTIN_ATOMIC) +# define GIT_MEMORY_BARRIER __atomic_thread_fence(__ATOMIC_SEQ_CST) +# elif defined(GIT_BUILTIN_SYNC) +# define GIT_MEMORY_BARRIER __sync_synchronize() +# endif + +#else + +# define GIT_MEMORY_BARRIER /* noop */ + +#endif + +/* Thread-local data */ + +#if !defined(GIT_THREADS) +# define git_tlsdata_key int +#elif defined(GIT_WIN32) +# define git_tlsdata_key DWORD +#elif defined(_POSIX_THREADS) +# define git_tlsdata_key pthread_key_t +#else +# error unknown threading model +#endif + +/** + * Create a thread-local data key. The destroy function will be + * called upon thread exit. On some platforms, it may be called + * when all threads have deleted their keys. + * + * Note that the tlsdata functions do not set an error message on + * failure; this is because the error handling in libgit2 is itself + * handled by thread-local data storage. + * + * @param key the tlsdata key + * @param destroy_fn function pointer called upon thread exit + * @return 0 on success, non-zero on failure + */ +int git_tlsdata_init(git_tlsdata_key *key, void (GIT_SYSTEM_CALL *destroy_fn)(void *)); + +/** + * Set a the thread-local value for the given key. + * + * @param key the tlsdata key to store data on + * @param value the pointer to store + * @return 0 on success, non-zero on failure + */ +int git_tlsdata_set(git_tlsdata_key key, void *value); + +/** + * Get the thread-local value for the given key. + * + * @param key the tlsdata key to retrieve the value of + * @return the pointer stored with git_tlsdata_set + */ +void *git_tlsdata_get(git_tlsdata_key key); + +/** + * Delete the given thread-local key. + * + * @param key the tlsdata key to dispose + * @return 0 on success, non-zero on failure + */ +int git_tlsdata_dispose(git_tlsdata_key key); + +#endif diff --git a/src/tsort.c b/src/util/tsort.c similarity index 95% rename from src/tsort.c rename to src/util/tsort.c index 97473be91cd..2ef03d03a88 100644 --- a/src/tsort.c +++ b/src/util/tsort.c @@ -5,7 +5,7 @@ * a Linking Exception. For full terms see the included COPYING file. */ -#include "common.h" +#include "git2_util.h" /** * An array-of-pointers implementation of Python's Timsort @@ -24,13 +24,11 @@ #endif static int binsearch( - void **dst, const void *x, size_t size, git__tsort_r_cmp cmp, void *payload) + void **dst, const void *x, size_t size, git__sort_r_cmp cmp, void *payload) { int l, c, r; void *lx, *cx; - assert(size > 0); - l = 0; r = (int)size - 1; c = r >> 1; @@ -71,7 +69,7 @@ static int binsearch( /* Binary insertion sort, but knowing that the first "start" entries are sorted. Used in timsort. */ static void bisort( - void **dst, size_t start, size_t size, git__tsort_r_cmp cmp, void *payload) + void **dst, size_t start, size_t size, git__sort_r_cmp cmp, void *payload) { size_t i; void *x; @@ -102,7 +100,7 @@ struct tsort_run { struct tsort_store { size_t alloc; - git__tsort_r_cmp cmp; + git__sort_r_cmp cmp; void *payload; void **storage; }; @@ -184,7 +182,9 @@ static int check_invariant(struct tsort_run *stack, ssize_t stack_curr) static int resize(struct tsort_store *store, size_t new_size) { if (store->alloc < new_size) { - void **tempstore = git__realloc(store->storage, new_size * sizeof(void *)); + void **tempstore; + + tempstore = git__reallocarray(store->storage, new_size, sizeof(void *)); /** * Do not propagate on OOM; this will abort the sort and @@ -308,7 +308,6 @@ static ssize_t collapse(void **dst, struct tsort_run *stack, ssize_t stack_curr, #define PUSH_NEXT() do {\ len = count_run(dst, curr, size, store);\ run = minrun;\ - if (run < minrun) run = minrun;\ if (run > (ssize_t)size - curr) run = size - curr;\ if (run > len) {\ bisort(&dst[curr], len, run, cmp, payload);\ @@ -334,7 +333,7 @@ static ssize_t collapse(void **dst, struct tsort_run *stack, ssize_t stack_curr, while (0) void git__tsort_r( - void **dst, size_t size, git__tsort_r_cmp cmp, void *payload) + void **dst, size_t size, git__sort_r_cmp cmp, void *payload) { struct tsort_store _store, *store = &_store; struct tsort_run run_stack[128]; diff --git a/src/util/unix/map.c b/src/util/unix/map.c new file mode 100644 index 00000000000..93307768947 --- /dev/null +++ b/src/util/unix/map.c @@ -0,0 +1,76 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2_util.h" + +#if !defined(GIT_WIN32) && !defined(NO_MMAP) + +#include "map.h" +#include +#include +#include + +int git__page_size(size_t *page_size) +{ + long sc_page_size = sysconf(_SC_PAGE_SIZE); + if (sc_page_size < 0) { + git_error_set(GIT_ERROR_OS, "can't determine system page size"); + return -1; + } + *page_size = (size_t) sc_page_size; + return 0; +} + +int git__mmap_alignment(size_t *alignment) +{ + return git__page_size(alignment); +} + +int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, off64_t offset) +{ + int mprot = PROT_READ; + int mflag = 0; + + GIT_MMAP_VALIDATE(out, len, prot, flags); + + out->data = NULL; + out->len = 0; + + if (prot & GIT_PROT_WRITE) + mprot |= PROT_WRITE; + + if ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED) + mflag = MAP_SHARED; + else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE) + mflag = MAP_PRIVATE; + else + mflag = MAP_SHARED; + + out->data = mmap(NULL, len, mprot, mflag, fd, offset); + + if (!out->data || out->data == MAP_FAILED) { + git_error_set(GIT_ERROR_OS, "failed to mmap. Could not write data"); + return -1; + } + + out->len = len; + + return 0; +} + +int p_munmap(git_map *map) +{ + GIT_ASSERT_ARG(map); + munmap(map->data, map->len); + map->data = NULL; + map->len = 0; + + return 0; +} + +#endif + diff --git a/src/util/unix/posix.h b/src/util/unix/posix.h new file mode 100644 index 00000000000..0c5c1065549 --- /dev/null +++ b/src/util/unix/posix.h @@ -0,0 +1,102 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_unix_posix_h__ +#define INCLUDE_unix_posix_h__ + +#include "git2_util.h" + +#include +#include +#include +#include +#include + +typedef int GIT_SOCKET; +#define INVALID_SOCKET -1 + +#define p_lseek(f,n,w) lseek(f, n, w) +#define p_fstat(f,b) fstat(f, b) +#define p_lstat(p,b) lstat(p,b) +#define p_stat(p,b) stat(p, b) + +#if defined(GIT_NSEC_MTIMESPEC) +# define st_atime_nsec st_atimespec.tv_nsec +# define st_mtime_nsec st_mtimespec.tv_nsec +# define st_ctime_nsec st_ctimespec.tv_nsec +#elif defined(GIT_NSEC_MTIM) +# define st_atime_nsec st_atim.tv_nsec +# define st_mtime_nsec st_mtim.tv_nsec +# define st_ctime_nsec st_ctim.tv_nsec +#elif !defined(GIT_NSEC_MTIME_NSEC) && defined(GIT_NSEC) +# error GIT_NSEC defined but unknown struct stat nanosecond type +#endif + +#define p_utimes(f, t) utimes(f, t) + +#define p_readlink(a, b, c) readlink(a, b, c) +#define p_symlink(o,n) symlink(o, n) +#define p_link(o,n) link(o, n) +#define p_unlink(p) unlink(p) +#define p_mkdir(p,m) mkdir(p, m) +extern char *p_realpath(const char *, char *); + +GIT_INLINE(int) p_fsync(int fd) +{ + p_fsync__cnt++; + return fsync(fd); +} + +#define p_recv(s,b,l,f) recv(s,b,l,f) +#define p_send(s,b,l,f) send(s,b,l,f) +#define p_inet_pton(a, b, c) inet_pton(a, b, c) + +#define p_vsnprintf(b, c, f, a) vsnprintf(b, c, f, a) +#define p_snprintf snprintf +#define p_chdir(p) chdir(p) +#define p_rmdir(p) rmdir(p) +#define p_access(p,m) access(p,m) +#define p_ftruncate(fd, sz) ftruncate(fd, sz) + +/* + * Pre-Android 5 did not implement a virtual filesystem atop FAT + * partitions for Unix permissions, which causes chmod to fail. However, + * Unix permissions have no effect on Android anyway as file permissions + * are not actually managed this way, so treating it as a no-op across + * all Android is safe. + */ +#ifdef __ANDROID__ +# define p_chmod(p,m) 0 +#else +# define p_chmod(p,m) chmod(p, m) +#endif + +/* see win32/posix.h for explanation about why this exists */ +#define p_lstat_posixly(p,b) lstat(p,b) + +#define p_localtime_r(c, r) localtime_r(c, r) +#define p_gmtime_r(c, r) gmtime_r(c, r) + +#define p_timeval timeval + +#ifdef GIT_FUTIMENS +GIT_INLINE(int) p_futimes(int f, const struct p_timeval t[2]) +{ + struct timespec s[2]; + s[0].tv_sec = t[0].tv_sec; + s[0].tv_nsec = t[0].tv_usec * 1000; + s[1].tv_sec = t[1].tv_sec; + s[1].tv_nsec = t[1].tv_usec * 1000; + return futimens(f, s); +} +#else +# define p_futimes futimes +#endif + +#define p_pread(f, d, s, o) pread(f, d, s, o) +#define p_pwrite(f, d, s, o) pwrite(f, d, s, o) + +#endif diff --git a/src/util/unix/process.c b/src/util/unix/process.c new file mode 100644 index 00000000000..72308b1de19 --- /dev/null +++ b/src/util/unix/process.c @@ -0,0 +1,706 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include +#include +#include + +#include "git2_util.h" +#include "vector.h" +#include "futils.h" +#include "process.h" +#include "strlist.h" + +#ifdef __APPLE__ + #include + #define environ (*_NSGetEnviron()) +#else + extern char **environ; +#endif + +struct git_process { + char **args; + char **env; + + char *cwd; + + unsigned int use_shell : 1, + capture_in : 1, + capture_out : 1, + capture_err : 1; + + pid_t pid; + + int child_in; + int child_out; + int child_err; + git_process_result_status status; +}; + +GIT_INLINE(bool) is_delete_env(const char *env) +{ + const char *c = strchr(env, '='); + + if (c == NULL) + return false; + + return *(c+1) == '\0'; +} + +GIT_INLINE(int) insert_dup(git_vector *v, const char *s) +{ + char *dup = git__strdup(s); + + GIT_ERROR_CHECK_ALLOC(dup); + + return git_vector_insert(v, dup); +} + +static int setup_args( + char ***out, + const char **args, + size_t args_len, + bool use_shell) +{ + git_vector prefixed = GIT_VECTOR_INIT; + git_str first = GIT_STR_INIT; + size_t cnt; + + GIT_ASSERT(args && args_len); + + if (use_shell) { + if (git_str_puts(&first, args[0]) < 0 || + git_str_puts(&first, " \"$@\"") < 0 || + insert_dup(&prefixed, "/bin/sh") < 0 || + insert_dup(&prefixed, "-c") < 0 || + git_vector_insert(&prefixed, git_str_detach(&first)) < 0) + goto on_error; + } + + for (cnt = 0; args && cnt < args_len; cnt++) { + if (insert_dup(&prefixed, args[cnt]) < 0) + goto on_error; + } + + git_vector_insert(&prefixed, NULL); + + *out = (char **)prefixed.contents; + + return 0; + +on_error: + git_str_dispose(&first); + git_vector_dispose_deep(&prefixed); + return -1; +} + +static int merge_env( + char ***out, + const char **env, + size_t env_len, + bool exclude_env) +{ + git_vector merged = GIT_VECTOR_INIT; + char **kv; + size_t max, cnt; + int error = 0; + + for (max = env_len, kv = environ; !exclude_env && *kv; kv++) + max++; + + if ((error = git_vector_init(&merged, max, NULL)) < 0) + goto on_error; + + for (cnt = 0; env && cnt < env_len; cnt++) { + if (is_delete_env(env[cnt])) + continue; + + if (insert_dup(&merged, env[cnt]) < 0) + goto on_error; + } + + if (!exclude_env) { + for (kv = environ; *kv; kv++) { + if (env && git_strlist_contains_key(env, env_len, *kv, '=')) + continue; + + if (insert_dup(&merged, *kv) < 0) + goto on_error; + } + } + + if (merged.length == 0) { + *out = NULL; + error = 0; + goto on_error; + } + + git_vector_insert(&merged, NULL); + + *out = (char **)merged.contents; + + return 0; + +on_error: + git_vector_dispose_deep(&merged); + return error; +} + +int git_process_new( + git_process **out, + const char **args, + size_t args_len, + const char **env, + size_t env_len, + git_process_options *opts) +{ + git_process *process; + + GIT_ASSERT_ARG(out && args && args_len > 0); + + *out = NULL; + + process = git__calloc(sizeof(git_process), 1); + GIT_ERROR_CHECK_ALLOC(process); + + if (setup_args(&process->args, args, args_len, opts ? opts->use_shell : false) < 0 || + merge_env(&process->env, env, env_len, opts ? opts->exclude_env : false) < 0) { + git_process_free(process); + return -1; + } + + if (opts) { + process->use_shell = opts->use_shell; + process->capture_in = opts->capture_in; + process->capture_out = opts->capture_out; + process->capture_err = opts->capture_err; + + if (opts->cwd) { + process->cwd = git__strdup(opts->cwd); + GIT_ERROR_CHECK_ALLOC(process->cwd); + } + } + + process->child_in = -1; + process->child_out = -1; + process->child_err = -1; + process->status = -1; + + *out = process; + return 0; +} + +extern int git_process_new_from_cmdline( + git_process **out, + const char *cmdline, + const char **env, + size_t env_len, + git_process_options *opts) +{ + git_process_options merged_opts = {0}; + + memcpy(&merged_opts, opts, sizeof(git_process_options)); + merged_opts.use_shell = 1; + + return git_process_new(out, &cmdline, 1, env, env_len, &merged_opts); +} + +#define CLOSE_FD(fd) \ + if (fd >= 0) { \ + close(fd); \ + fd = -1; \ + } + +static int try_read_status(size_t *out, int fd, void *buf, size_t len) +{ + size_t read_len = 0; + int ret = -1; + + while (ret && read_len < len) { + ret = read(fd, buf + read_len, len - read_len); + + if (ret < 0 && errno != EAGAIN && errno != EINTR) { + git_error_set(GIT_ERROR_OS, "could not read child status"); + return -1; + } + + read_len += ret; + } + + *out = read_len; + return 0; +} + + +static int read_status(int fd) +{ + size_t status_len = sizeof(int) * 3, read_len = 0; + char buffer[status_len], fn[128]; + int error, fn_error, os_error, fn_len = 0; + + if ((error = try_read_status(&read_len, fd, buffer, status_len)) < 0) + return error; + + /* Immediate EOF indicates the exec succeeded. */ + if (read_len == 0) + return 0; + + if (read_len < status_len) { + git_error_set(GIT_ERROR_INVALID, "child status truncated"); + return -1; + } + + memcpy(&fn_error, &buffer[0], sizeof(int)); + memcpy(&os_error, &buffer[sizeof(int)], sizeof(int)); + memcpy(&fn_len, &buffer[sizeof(int) * 2], sizeof(int)); + + if (fn_len > 0) { + fn_len = min(fn_len, (int)(ARRAY_SIZE(fn) - 1)); + + if ((error = try_read_status(&read_len, fd, fn, fn_len)) < 0) + return error; + + fn[fn_len] = '\0'; + } else { + fn[0] = '\0'; + } + + if (fn_error) { + errno = os_error; + git_error_set(GIT_ERROR_OS, "could not %s", fn[0] ? fn : "(unknown)"); + } + + return fn_error; +} + +static bool try_write_status(int fd, const void *buf, size_t len) +{ + size_t write_len; + int ret; + + for (write_len = 0; write_len < len; ) { + ret = write(fd, buf + write_len, len - write_len); + + if (ret <= 0) + break; + + write_len += ret; + } + + return (len == write_len); +} + +static void write_status(int fd, const char *fn, int error, int os_error) +{ + size_t status_len = sizeof(int) * 3, fn_len; + char buffer[status_len]; + + fn_len = strlen(fn); + + if (fn_len > INT_MAX) + fn_len = INT_MAX; + + memcpy(&buffer[0], &error, sizeof(int)); + memcpy(&buffer[sizeof(int)], &os_error, sizeof(int)); + memcpy(&buffer[sizeof(int) * 2], &fn_len, sizeof(int)); + + /* Do our best effort to write all the status. */ + if (!try_write_status(fd, buffer, status_len)) + return; + + if (fn_len) + try_write_status(fd, fn, fn_len); +} + +static int resolve_path(git_process *process) +{ + git_str full_path = GIT_STR_INIT; + int error = 0; + + /* The shell will resolve the path for us */ + if (process->use_shell) + goto done; + + error = git_fs_path_find_executable(&full_path, process->args[0]); + + if (error == GIT_ENOTFOUND) { + git_error_set(GIT_ERROR_SSH, "cannot run %s: No such file or directory", process->args[0]); + error = -1; + } + + if (error) + goto done; + + git__free(process->args[0]); + process->args[0] = git_str_detach(&full_path); + +done: + git_str_dispose(&full_path); + return error; +} + +int git_process_start(git_process *process) +{ + int in[2] = { -1, -1 }, out[2] = { -1, -1 }, + err[2] = { -1, -1 }, status[2] = { -1, -1 }; + int fdflags, state, error; + pid_t pid; + + /* Locate the path (unless we're letting the shell do it for us) */ + if ((error = resolve_path(process)) < 0) + goto on_error; + + /* Set up the pipes to read from/write to the process */ + if ((process->capture_in && pipe(in) < 0) || + (process->capture_out && pipe(out) < 0) || + (process->capture_err && pipe(err) < 0)) { + git_error_set(GIT_ERROR_OS, "could not create pipe"); + goto on_error; + } + + /* Set up a self-pipe for status from the forked process. */ + if (pipe(status) < 0 || + (fdflags = fcntl(status[1], F_GETFD)) < 0 || + fcntl(status[1], F_SETFD, fdflags | FD_CLOEXEC) < 0) { + git_error_set(GIT_ERROR_OS, "could not create pipe"); + goto on_error; + } + + switch (pid = fork()) { + case -1: + git_error_set(GIT_ERROR_OS, "could not fork"); + goto on_error; + + /* Child: start the process. */ + case 0: + /* Close the opposing side of the pipes */ + CLOSE_FD(status[0]); + + if (process->capture_in) { + CLOSE_FD(in[1]); + dup2(in[0], STDIN_FILENO); + } + + if (process->capture_out) { + CLOSE_FD(out[0]); + dup2(out[1], STDOUT_FILENO); + } + + if (process->capture_err) { + CLOSE_FD(err[0]); + dup2(err[1], STDERR_FILENO); + } + + if (process->cwd && (error = chdir(process->cwd)) < 0) { + write_status(status[1], "chdir", error, errno); + exit(0); + } + + /* + * Exec the process and write the results back if the + * call fails. If it succeeds, we'll close the status + * pipe (via CLOEXEC) and the parent will know. + */ + error = execve(process->args[0], + process->args, + process->env); + + write_status(status[1], "execve", error, errno); + exit(0); + + /* Parent: make sure the child process exec'd correctly. */ + default: + /* Close the opposing side of the pipes */ + CLOSE_FD(status[1]); + + if (process->capture_in) { + CLOSE_FD(in[0]); + process->child_in = in[1]; + } + + if (process->capture_out) { + CLOSE_FD(out[1]); + process->child_out = out[0]; + } + + if (process->capture_err) { + CLOSE_FD(err[1]); + process->child_err = err[0]; + } + + /* Try to read the status */ + process->status = status[0]; + if ((error = read_status(status[0])) < 0) { + waitpid(process->pid, &state, 0); + goto on_error; + } + + process->pid = pid; + return 0; + } + +on_error: + CLOSE_FD(in[0]); CLOSE_FD(in[1]); + CLOSE_FD(out[0]); CLOSE_FD(out[1]); + CLOSE_FD(err[0]); CLOSE_FD(err[1]); + CLOSE_FD(status[0]); CLOSE_FD(status[1]); + return -1; +} + +int git_process_id(p_pid_t *out, git_process *process) +{ + GIT_ASSERT(out && process); + + if (!process->pid) { + git_error_set(GIT_ERROR_INVALID, "process not running"); + return -1; + } + + *out = process->pid; + return 0; +} + +static ssize_t process_read(int fd, void *buf, size_t count) +{ + ssize_t ret; + + if (count > SSIZE_MAX) + count = SSIZE_MAX; + + if ((ret = read(fd, buf, count)) < 0) { + git_error_set(GIT_ERROR_OS, "could not read from child process"); + return -1; + } + + return ret; +} + +ssize_t git_process_read(git_process *process, void *buf, size_t count) +{ + GIT_ASSERT_ARG(process); + GIT_ASSERT(process->capture_out); + + return process_read(process->child_out, buf, count); +} + +ssize_t git_process_read_err(git_process *process, void *buf, size_t count) +{ + GIT_ASSERT_ARG(process); + GIT_ASSERT(process->capture_err); + + return process_read(process->child_err, buf, count); +} + +#ifdef GIT_THREADS + +# define signal_state sigset_t + +/* + * Since signal-handling is process-wide, we cannot simply use + * SIG_IGN to avoid SIGPIPE. Instead: http://www.microhowto.info:80/howto/ignore_sigpipe_without_affecting_other_threads_in_a_process.html + */ + +GIT_INLINE(int) disable_signals(sigset_t *saved_mask) +{ + sigset_t sigpipe_mask; + + sigemptyset(&sigpipe_mask); + sigaddset(&sigpipe_mask, SIGPIPE); + + if (pthread_sigmask(SIG_BLOCK, &sigpipe_mask, saved_mask) < 0) { + git_error_set(GIT_ERROR_OS, "could not configure signal mask"); + return -1; + } + + return 0; +} + +GIT_INLINE(int) restore_signals(sigset_t *saved_mask) +{ + sigset_t sigpipe_mask, pending; + int signal; + + sigemptyset(&sigpipe_mask); + sigaddset(&sigpipe_mask, SIGPIPE); + + if (sigpending(&pending) < 0) { + git_error_set(GIT_ERROR_OS, "could not examine pending signals"); + return -1; + } + + if (sigismember(&pending, SIGPIPE) == 1 && + sigwait(&sigpipe_mask, &signal) < 0) { + git_error_set(GIT_ERROR_OS, "could not wait for (blocking) signal delivery"); + return -1; + } + + if (pthread_sigmask(SIG_SETMASK, saved_mask, 0) < 0) { + git_error_set(GIT_ERROR_OS, "could not configure signal mask"); + return -1; + } + + return 0; +} + +#else + +# define signal_state struct sigaction + +GIT_INLINE(int) disable_signals(struct sigaction *saved_handler) +{ + struct sigaction ign_handler = { 0 }; + + ign_handler.sa_handler = SIG_IGN; + + if (sigaction(SIGPIPE, &ign_handler, saved_handler) < 0) { + git_error_set(GIT_ERROR_OS, "could not configure signal handler"); + return -1; + } + + return 0; +} + +GIT_INLINE(int) restore_signals(struct sigaction *saved_handler) +{ + if (sigaction(SIGPIPE, saved_handler, NULL) < 0) { + git_error_set(GIT_ERROR_OS, "could not configure signal handler"); + return -1; + } + + return 0; +} + +#endif + +ssize_t git_process_write(git_process *process, const void *buf, size_t count) +{ + signal_state saved_signal; + ssize_t ret; + + GIT_ASSERT_ARG(process); + GIT_ASSERT(process->capture_in); + + if (count > SSIZE_MAX) + count = SSIZE_MAX; + + if (disable_signals(&saved_signal) < 0) + return -1; + + if ((ret = write(process->child_in, buf, count)) < 0) + git_error_set(GIT_ERROR_OS, "could not write to child process"); + + if (restore_signals(&saved_signal) < 0) + return -1; + + return (ret < 0) ? -1 : ret; +} + +int git_process_close_in(git_process *process) +{ + if (!process->capture_in) { + git_error_set(GIT_ERROR_INVALID, "input is not open"); + return -1; + } + + CLOSE_FD(process->child_in); + return 0; +} + +int git_process_close_out(git_process *process) +{ + if (!process->capture_out) { + git_error_set(GIT_ERROR_INVALID, "output is not open"); + return -1; + } + + CLOSE_FD(process->child_out); + return 0; +} + +int git_process_close_err(git_process *process) +{ + if (!process->capture_err) { + git_error_set(GIT_ERROR_INVALID, "error is not open"); + return -1; + } + + CLOSE_FD(process->child_err); + return 0; +} + +int git_process_close(git_process *process) +{ + CLOSE_FD(process->child_in); + CLOSE_FD(process->child_out); + CLOSE_FD(process->child_err); + + return 0; +} + +int git_process_wait(git_process_result *result, git_process *process) +{ + int state; + + if (result) + memset(result, 0, sizeof(git_process_result)); + + if (!process->pid) { + git_error_set(GIT_ERROR_INVALID, "process is stopped"); + return -1; + } + + if (waitpid(process->pid, &state, 0) < 0) { + git_error_set(GIT_ERROR_OS, "could not wait for child"); + return -1; + } + + process->pid = 0; + + if (result) { + if (WIFEXITED(state)) { + result->status = GIT_PROCESS_STATUS_NORMAL; + result->exitcode = WEXITSTATUS(state); + } else if (WIFSIGNALED(state)) { + result->status = GIT_PROCESS_STATUS_ERROR; + result->signal = WTERMSIG(state); + } else { + result->status = GIT_PROCESS_STATUS_ERROR; + } + } + + return 0; +} + +int git_process_result_msg(git_str *out, git_process_result *result) +{ + if (result->status == GIT_PROCESS_STATUS_NONE) { + return git_str_puts(out, "process not started"); + } else if (result->status == GIT_PROCESS_STATUS_NORMAL) { + return git_str_printf(out, "process exited with code %d", + result->exitcode); + } else if (result->signal) { + return git_str_printf(out, "process exited on signal %d", + result->signal); + } + + return git_str_puts(out, "unknown error"); +} + +void git_process_free(git_process *process) +{ + if (!process) + return; + + if (process->pid) + git_process_close(process); + + git__free(process->cwd); + git_strlist_free_with_null(process->args); + git_strlist_free_with_null(process->env); + git__free(process); +} diff --git a/src/util/unix/pthread.h b/src/util/unix/pthread.h new file mode 100644 index 00000000000..55f4ae227ee --- /dev/null +++ b/src/util/unix/pthread.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_unix_pthread_h__ +#define INCLUDE_unix_pthread_h__ + +typedef struct { + pthread_t thread; +} git_thread; + +GIT_INLINE(int) git_threads_global_init(void) { return 0; } + +#define git_thread_create(git_thread_ptr, start_routine, arg) \ + pthread_create(&(git_thread_ptr)->thread, NULL, start_routine, arg) +#define git_thread_join(git_thread_ptr, status) \ + pthread_join((git_thread_ptr)->thread, status) +#define git_thread_currentid() ((size_t)(pthread_self())) +#define git_thread_exit(retval) pthread_exit(retval) + +/* Git Mutex */ +#define git_mutex pthread_mutex_t +#define git_mutex_init(a) pthread_mutex_init(a, NULL) +#define git_mutex_lock(a) pthread_mutex_lock(a) +#define git_mutex_unlock(a) pthread_mutex_unlock(a) +#define git_mutex_free(a) pthread_mutex_destroy(a) + +/* Git condition vars */ +#define git_cond pthread_cond_t +#define git_cond_init(c) pthread_cond_init(c, NULL) +#define git_cond_free(c) pthread_cond_destroy(c) +#define git_cond_wait(c, l) pthread_cond_wait(c, l) +#define git_cond_signal(c) pthread_cond_signal(c) +#define git_cond_broadcast(c) pthread_cond_broadcast(c) + +/* Pthread (-ish) rwlock + * + * This differs from normal pthreads rwlocks in two ways: + * 1. Separate APIs for releasing read locks and write locks (as + * opposed to the pure POSIX API which only has one unlock fn) + * 2. You should not use recursive read locks (i.e. grabbing a read + * lock in a thread that already holds a read lock) because the + * Windows implementation doesn't support it + */ +#define git_rwlock pthread_rwlock_t +#define git_rwlock_init(a) pthread_rwlock_init(a, NULL) +#define git_rwlock_rdlock(a) pthread_rwlock_rdlock(a) +#define git_rwlock_rdunlock(a) pthread_rwlock_unlock(a) +#define git_rwlock_wrlock(a) pthread_rwlock_wrlock(a) +#define git_rwlock_wrunlock(a) pthread_rwlock_unlock(a) +#define git_rwlock_free(a) pthread_rwlock_destroy(a) +#define GIT_RWLOCK_STATIC_INIT PTHREAD_RWLOCK_INITIALIZER + +#endif diff --git a/src/util/unix/realpath.c b/src/util/unix/realpath.c new file mode 100644 index 00000000000..2565e2e83bc --- /dev/null +++ b/src/util/unix/realpath.c @@ -0,0 +1,50 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2_util.h" + +#ifndef GIT_WIN32 + +#include +#include +#include +#include + +char *p_realpath(const char *pathname, char *resolved) +{ + char *result; + + if ((result = realpath(pathname, resolved)) == NULL) + return NULL; + +#ifdef __OpenBSD__ + /* The OpenBSD realpath function behaves differently, + * figure out if the file exists */ + if (access(result, F_OK) < 0) { + if (!resolved) + free(result); + + return NULL; + } +#endif + + /* + * If resolved == NULL, the system has allocated the result + * string. We need to strdup this into _our_ allocator pool + * so that callers can free it with git__free. + */ + if (!resolved) { + char *dup = git__strdup(result); + free(result); + + result = dup; + } + + return result; +} + +#endif diff --git a/src/util/utf8.c b/src/util/utf8.c new file mode 100644 index 00000000000..c566fdf2084 --- /dev/null +++ b/src/util/utf8.c @@ -0,0 +1,150 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "utf8.h" + +#include "git2_util.h" + +/* + * git_utf8_iterate is taken from the utf8proc project, + * http://www.public-software-group.org/utf8proc + * + * Copyright (c) 2009 Public Software Group e. V., Berlin, Germany + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the ""Software""), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +static const uint8_t utf8proc_utf8class[256] = { + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +static int utf8_charlen(const uint8_t *str, size_t str_len) +{ + uint8_t length; + size_t i; + + length = utf8proc_utf8class[str[0]]; + if (!length) + return -1; + + if (str_len > 0 && length > str_len) + return -1; + + for (i = 1; i < length; i++) { + if ((str[i] & 0xC0) != 0x80) + return -1; + } + + return (int)length; +} + +int git_utf8_iterate(uint32_t *out, const char *_str, size_t str_len) +{ + const uint8_t *str = (const uint8_t *)_str; + uint32_t uc = 0; + int length; + + *out = 0; + + if ((length = utf8_charlen(str, str_len)) < 0) + return -1; + + switch (length) { + case 1: + uc = str[0]; + break; + case 2: + uc = ((str[0] & 0x1F) << 6) + (str[1] & 0x3F); + if (uc < 0x80) uc = -1; + break; + case 3: + uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6) + + (str[2] & 0x3F); + if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000) || + (uc >= 0xFDD0 && uc < 0xFDF0)) uc = -1; + break; + case 4: + uc = ((str[0] & 0x07) << 18) + ((str[1] & 0x3F) << 12) + + ((str[2] & 0x3F) << 6) + (str[3] & 0x3F); + if (uc < 0x10000 || uc >= 0x110000) uc = -1; + break; + default: + return -1; + } + + if ((uc & 0xFFFF) >= 0xFFFE) + return -1; + + *out = uc; + return length; +} + +size_t git_utf8_char_length(const char *_str, size_t str_len) +{ + const uint8_t *str = (const uint8_t *)_str; + size_t offset = 0, count = 0; + + while (offset < str_len) { + int length = utf8_charlen(str + offset, str_len - offset); + + if (length < 0) + length = 1; + + offset += length; + count++; + } + + return count; +} + +size_t git_utf8_valid_buf_length(const char *_str, size_t str_len) +{ + const uint8_t *str = (const uint8_t *)_str; + size_t offset = 0; + + while (offset < str_len) { + int length = utf8_charlen(str + offset, str_len - offset); + + if (length < 0) + break; + + offset += length; + } + + return offset; +} diff --git a/src/util/utf8.h b/src/util/utf8.h new file mode 100644 index 00000000000..753ab07e2a5 --- /dev/null +++ b/src/util/utf8.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_utf8_h__ +#define INCLUDE_utf8_h__ + +#include "git2_util.h" + +/* + * Iterate through an UTF-8 string, yielding one codepoint at a time. + * + * @param out pointer where to store the current codepoint + * @param str current position in the string + * @param str_len size left in the string + * @return length in bytes of the read codepoint; -1 if the codepoint was invalid + */ +extern int git_utf8_iterate(uint32_t *out, const char *str, size_t str_len); + +/** + * Returns the number of characters in the given string. + * + * This function will count invalid codepoints; if any given byte is + * not part of a valid UTF-8 codepoint, then it will be counted toward + * the length in characters. + * + * In other words: + * 0x24 (U+0024 "$") has length 1 + * 0xc2 0xa2 (U+00A2 "¢") has length 1 + * 0x24 0xc2 0xa2 (U+0024 U+00A2 "$¢") has length 2 + * 0xf0 0x90 0x8d 0x88 (U+10348 "𐍈") has length 1 + * 0x24 0xc0 0xc1 0x34 (U+0024 "4) has length 4 + * + * @param str string to scan + * @param str_len size of the string + * @return length in characters of the string + */ +extern size_t git_utf8_char_length(const char *str, size_t str_len); + +/** + * Iterate through an UTF-8 string and stops after finding any invalid UTF-8 + * codepoints. + * + * @param str string to scan + * @param str_len size of the string + * @return length in bytes of the string that contains valid data + */ +extern size_t git_utf8_valid_buf_length(const char *str, size_t str_len); + +#endif diff --git a/src/util/util.c b/src/util/util.c new file mode 100644 index 00000000000..f22a75b2f25 --- /dev/null +++ b/src/util/util.c @@ -0,0 +1,837 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "util.h" + +#include "git2_util.h" + +#ifdef GIT_WIN32 +# include "win32/utf-conv.h" +# include "win32/w32_buffer.h" + +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include + +# ifdef GIT_QSORT_MSC +# include +# endif +#endif + +#ifdef _MSC_VER +# include +#endif + +#if defined(hpux) || defined(__hpux) || defined(_hpux) +# include +#endif + +int git__strntol64(int64_t *result, const char *nptr, size_t nptr_len, const char **endptr, int base) +{ + const char *p; + int64_t n, nn, v; + int c, ovfl, neg, ndig; + + p = nptr; + neg = 0; + n = 0; + ndig = 0; + ovfl = 0; + + /* + * White space + */ + while (nptr_len && git__isspace(*p)) + p++, nptr_len--; + + if (!nptr_len) + goto Return; + + /* + * Sign + */ + if (*p == '-' || *p == '+') { + if (*p == '-') + neg = 1; + p++; + nptr_len--; + } + + if (!nptr_len) + goto Return; + + /* + * Automatically detect the base if none was given to us. + * Right now, we assume that a number starting with '0x' + * is hexadecimal and a number starting with '0' is + * octal. + */ + if (base == 0) { + if (*p != '0') + base = 10; + else if (nptr_len > 2 && (p[1] == 'x' || p[1] == 'X')) + base = 16; + else + base = 8; + } + + if (base < 0 || 36 < base) + goto Return; + + /* + * Skip prefix of '0x'-prefixed hexadecimal numbers. There is no + * need to do the same for '0'-prefixed octal numbers as a + * leading '0' does not have any impact. Also, if we skip a + * leading '0' in such a string, then we may end up with no + * digits left and produce an error later on which isn't one. + */ + if (base == 16 && nptr_len > 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { + p += 2; + nptr_len -= 2; + } + + /* + * Non-empty sequence of digits + */ + for (; nptr_len > 0; p++,ndig++,nptr_len--) { + c = *p; + v = base; + if ('0'<=c && c<='9') + v = c - '0'; + else if ('a'<=c && c<='z') + v = c - 'a' + 10; + else if ('A'<=c && c<='Z') + v = c - 'A' + 10; + if (v >= base) + break; + v = neg ? -v : v; + if (git__multiply_int64_overflow(&nn, n, base) || git__add_int64_overflow(&n, nn, v)) { + ovfl = 1; + /* Keep on iterating until the end of this number */ + continue; + } + } + +Return: + if (ndig == 0) { + git_error_set(GIT_ERROR_INVALID, "failed to convert string to long: not a number"); + return -1; + } + + if (endptr) + *endptr = p; + + if (ovfl) { + git_error_set(GIT_ERROR_INVALID, "failed to convert string to long: overflow error"); + return -1; + } + + *result = n; + return 0; +} + +int git__strntol32(int32_t *result, const char *nptr, size_t nptr_len, const char **endptr, int base) +{ + const char *tmp_endptr; + int32_t tmp_int; + int64_t tmp_long; + int error; + + if ((error = git__strntol64(&tmp_long, nptr, nptr_len, &tmp_endptr, base)) < 0) + return error; + + tmp_int = tmp_long & 0xFFFFFFFF; + if (tmp_int != tmp_long) { + int len = (int)(tmp_endptr - nptr); + git_error_set(GIT_ERROR_INVALID, "failed to convert: '%.*s' is too large", len, nptr); + return -1; + } + + *result = tmp_int; + if (endptr) + *endptr = tmp_endptr; + + return error; +} + +int git__strcasecmp(const char *a, const char *b) +{ + while (*a && *b && git__tolower(*a) == git__tolower(*b)) + ++a, ++b; + return ((unsigned char)git__tolower(*a) - (unsigned char)git__tolower(*b)); +} + +int git__strcasesort_cmp(const char *a, const char *b) +{ + int cmp = 0; + + while (*a && *b) { + if (*a != *b) { + if (git__tolower(*a) != git__tolower(*b)) + break; + /* use case in sort order even if not in equivalence */ + if (!cmp) + cmp = (int)(*(const uint8_t *)a) - (int)(*(const uint8_t *)b); + } + + ++a, ++b; + } + + if (*a || *b) + return (unsigned char)git__tolower(*a) - (unsigned char)git__tolower(*b); + + return cmp; +} + +int git__strncasecmp(const char *a, const char *b, size_t sz) +{ + int al, bl; + + do { + al = (unsigned char)git__tolower(*a); + bl = (unsigned char)git__tolower(*b); + ++a, ++b; + } while (--sz && al && al == bl); + + return al - bl; +} + +void git__strntolower(char *str, size_t len) +{ + size_t i; + + for (i = 0; i < len; ++i) { + str[i] = (char)git__tolower(str[i]); + } +} + +void git__strtolower(char *str) +{ + git__strntolower(str, strlen(str)); +} + +GIT_INLINE(int) prefixcmp(const char *str, size_t str_n, const char *prefix, bool icase) +{ + int s, p; + + while (str_n--) { + s = (unsigned char)*str++; + p = (unsigned char)*prefix++; + + if (icase) { + s = git__tolower(s); + p = git__tolower(p); + } + + if (!p) + return 0; + + if (s != p) + return s - p; + } + + return (0 - *prefix); +} + +int git__prefixcmp(const char *str, const char *prefix) +{ + unsigned char s, p; + + while (1) { + p = *prefix++; + s = *str++; + + if (!p) + return 0; + + if (s != p) + return s - p; + } +} + +int git__prefixncmp(const char *str, size_t str_n, const char *prefix) +{ + return prefixcmp(str, str_n, prefix, false); +} + +int git__prefixcmp_icase(const char *str, const char *prefix) +{ + return prefixcmp(str, SIZE_MAX, prefix, true); +} + +int git__prefixncmp_icase(const char *str, size_t str_n, const char *prefix) +{ + return prefixcmp(str, str_n, prefix, true); +} + +static int suffixcmp(const char *str, const char *suffix, bool icase) +{ + size_t a = strlen(str); + size_t b = strlen(suffix); + + if (a < b) + return -1; + + return icase ? strcasecmp(str + (a - b), suffix) : + strcmp(str + (a - b), suffix); +} + +int git__suffixcmp(const char *str, const char *suffix) +{ + return suffixcmp(str, suffix, false); +} + +int git__suffixcmp_icase(const char *str, const char *suffix) +{ + return suffixcmp(str, suffix, true); +} + +char *git__strtok(char **end, const char *sep) +{ + char *ptr = *end; + + while (*ptr && strchr(sep, *ptr)) + ++ptr; + + if (*ptr) { + char *start = ptr; + *end = start + 1; + + while (**end && !strchr(sep, **end)) + ++*end; + + if (**end) { + **end = '\0'; + ++*end; + } + + return start; + } + + return NULL; +} + +/* Similar to strtok, but does not collapse repeated tokens. */ +char *git__strsep(char **end, const char *sep) +{ + char *start = *end, *ptr = *end; + + while (*ptr && !strchr(sep, *ptr)) + ++ptr; + + if (*ptr) { + *end = ptr + 1; + *ptr = '\0'; + + return start; + } + + return NULL; +} + +size_t git__linenlen(const char *buffer, size_t buffer_len) +{ + const char *nl = memchr(buffer, '\n', buffer_len); + return nl ? (size_t)(nl - buffer) + 1 : buffer_len; +} + +/* + * Adapted Not So Naive algorithm from http://www-igm.univ-mlv.fr/~lecroq/string/ + */ +const void * git__memmem(const void *haystack, size_t haystacklen, + const void *needle, size_t needlelen) +{ + const char *h, *n; + size_t j, k, l; + + if (needlelen > haystacklen || !haystacklen || !needlelen) + return NULL; + + h = (const char *) haystack, + n = (const char *) needle; + + if (needlelen == 1) + return memchr(haystack, *n, haystacklen); + + if (n[0] == n[1]) { + k = 2; + l = 1; + } else { + k = 1; + l = 2; + } + + j = 0; + while (j <= haystacklen - needlelen) { + if (n[1] != h[j + 1]) { + j += k; + } else { + if (memcmp(n + 2, h + j + 2, needlelen - 2) == 0 && + n[0] == h[j]) + return h + j; + j += l; + } + } + + return NULL; +} + +void git__hexdump(const char *buffer, size_t len) +{ + static const size_t LINE_WIDTH = 16; + + size_t line_count, last_line, i, j; + const char *line; + + line_count = (len / LINE_WIDTH); + last_line = (len % LINE_WIDTH); + + for (i = 0; i < line_count; ++i) { + printf("%08" PRIxZ " ", (i * LINE_WIDTH)); + + line = buffer + (i * LINE_WIDTH); + for (j = 0; j < LINE_WIDTH; ++j, ++line) { + printf("%02x ", (unsigned char)*line & 0xFF); + + if (j == (LINE_WIDTH / 2)) + printf(" "); + } + + printf(" |"); + + line = buffer + (i * LINE_WIDTH); + for (j = 0; j < LINE_WIDTH; ++j, ++line) + printf("%c", (*line >= 32 && *line <= 126) ? *line : '.'); + + printf("|\n"); + } + + if (last_line > 0) { + printf("%08" PRIxZ " ", (line_count * LINE_WIDTH)); + + line = buffer + (line_count * LINE_WIDTH); + for (j = 0; j < last_line; ++j, ++line) { + printf("%02x ", (unsigned char)*line & 0xFF); + + if (j == (LINE_WIDTH / 2)) + printf(" "); + } + + if (j < (LINE_WIDTH / 2)) + printf(" "); + for (j = 0; j < (LINE_WIDTH - last_line); ++j) + printf(" "); + + printf(" |"); + + line = buffer + (line_count * LINE_WIDTH); + for (j = 0; j < last_line; ++j, ++line) + printf("%c", (*line >= 32 && *line <= 126) ? *line : '.'); + + printf("|\n"); + } + + printf("\n"); +} + +#ifdef GIT_LEGACY_HASH +uint32_t git__hash(const void *key, int len, unsigned int seed) +{ + const uint32_t m = 0x5bd1e995; + const int r = 24; + uint32_t h = seed ^ len; + + const unsigned char *data = (const unsigned char *)key; + + while(len >= 4) { + uint32_t k = *(uint32_t *)data; + + k *= m; + k ^= k >> r; + k *= m; + + h *= m; + h ^= k; + + data += 4; + len -= 4; + } + + switch(len) { + case 3: h ^= data[2] << 16; + case 2: h ^= data[1] << 8; + case 1: h ^= data[0]; + h *= m; + }; + + h ^= h >> 13; + h *= m; + h ^= h >> 15; + + return h; +} +#else +/* + Cross-platform version of Murmurhash3 + http://code.google.com/p/smhasher/wiki/MurmurHash3 + by Austin Appleby (aappleby@gmail.com) + + This code is on the public domain. +*/ +uint32_t git__hash(const void *key, int len, uint32_t seed) +{ + +#define MURMUR_BLOCK() {\ + k1 *= c1; \ + k1 = git__rotl(k1,11);\ + k1 *= c2;\ + h1 ^= k1;\ + h1 = h1*3 + 0x52dce729;\ + c1 = c1*5 + 0x7b7d159c;\ + c2 = c2*5 + 0x6bce6396;\ +} + + const uint8_t *data = (const uint8_t*)key; + const int nblocks = len / 4; + + const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4); + const uint8_t *tail = (const uint8_t *)(data + nblocks * 4); + + uint32_t h1 = 0x971e137b ^ seed; + uint32_t k1; + + uint32_t c1 = 0x95543787; + uint32_t c2 = 0x2ad7eb25; + + int i; + + for (i = -nblocks; i; i++) { + k1 = blocks[i]; + MURMUR_BLOCK(); + } + + k1 = 0; + + switch(len & 3) { + case 3: k1 ^= tail[2] << 16; + /* fall through */ + case 2: k1 ^= tail[1] << 8; + /* fall through */ + case 1: k1 ^= tail[0]; + MURMUR_BLOCK(); + } + + h1 ^= len; + h1 ^= h1 >> 16; + h1 *= 0x85ebca6b; + h1 ^= h1 >> 13; + h1 *= 0xc2b2ae35; + h1 ^= h1 >> 16; + + return h1; +} +#endif + +/** + * A modified `bsearch` from the BSD glibc. + * + * Copyright (c) 1990 Regents of the University of California. + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. [rescinded 22 July 1999] + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +int git__bsearch( + void **array, + size_t array_len, + const void *key, + int (*compare)(const void *, const void *), + size_t *position) +{ + size_t lim; + int cmp = -1; + void **part, **base = array; + + for (lim = array_len; lim != 0; lim >>= 1) { + part = base + (lim >> 1); + cmp = (*compare)(key, *part); + if (cmp == 0) { + base = part; + break; + } + if (cmp > 0) { /* key > p; take right partition */ + base = part + 1; + lim--; + } /* else take left partition */ + } + + if (position) + *position = (base - array); + + return (cmp == 0) ? 0 : GIT_ENOTFOUND; +} + +int git__bsearch_r( + void **array, + size_t array_len, + const void *key, + int (*compare_r)(const void *, const void *, void *), + void *payload, + size_t *position) +{ + size_t lim; + int cmp = -1; + void **part, **base = array; + + for (lim = array_len; lim != 0; lim >>= 1) { + part = base + (lim >> 1); + cmp = (*compare_r)(key, *part, payload); + if (cmp == 0) { + base = part; + break; + } + if (cmp > 0) { /* key > p; take right partition */ + base = part + 1; + lim--; + } /* else take left partition */ + } + + if (position) + *position = (base - array); + + return (cmp == 0) ? 0 : GIT_ENOTFOUND; +} + +/** + * A strcmp wrapper + * + * We don't want direct pointers to the CRT on Windows, we may + * get stdcall conflicts. + */ +int git__strcmp_cb(const void *a, const void *b) +{ + return git__strcmp((const char *)a, (const char *)b); +} + +int git__strcasecmp_cb(const void *a, const void *b) +{ + return git__strcasecmp((const char *)a, (const char *)b); +} + +int git__parse_bool(int *out, const char *value) +{ + /* A missing value means true */ + if (value == NULL || + !strcasecmp(value, "true") || + !strcasecmp(value, "yes") || + !strcasecmp(value, "on")) { + *out = 1; + return 0; + } + if (!strcasecmp(value, "false") || + !strcasecmp(value, "no") || + !strcasecmp(value, "off") || + value[0] == '\0') { + *out = 0; + return 0; + } + + return -1; +} + +size_t git__unescape(char *str) +{ + char *scan, *pos = str; + + if (!str) + return 0; + + for (scan = str; *scan; pos++, scan++) { + if (*scan == '\\' && *(scan + 1) != '\0') + scan++; /* skip '\' but include next char */ + if (pos != scan) + *pos = *scan; + } + + if (pos != scan) { + *pos = '\0'; + } + + return (pos - str); +} + +#if defined(GIT_QSORT_MSC) || defined(GIT_QSORT_BSD) +typedef struct { + git__sort_r_cmp cmp; + void *payload; +} git__qsort_r_glue; + +static int GIT_LIBGIT2_CALL git__qsort_r_glue_cmp( + void *payload, const void *a, const void *b) +{ + git__qsort_r_glue *glue = payload; + return glue->cmp(a, b, glue->payload); +} +#endif + + +#if !defined(GIT_QSORT_BSD) && \ + !defined(GIT_QSORT_GNU) && \ + !defined(GIT_QSORT_C11) && \ + !defined(GIT_QSORT_MSC) + +static void swap(uint8_t *a, uint8_t *b, size_t elsize) +{ + char tmp[256]; + + while (elsize) { + size_t n = elsize < sizeof(tmp) ? elsize : sizeof(tmp); + memcpy(tmp, a + elsize - n, n); + memcpy(a + elsize - n, b + elsize - n, n); + memcpy(b + elsize - n, tmp, n); + elsize -= n; + } +} + +static void insertsort( + void *els, size_t nel, size_t elsize, + git__sort_r_cmp cmp, void *payload) +{ + uint8_t *base = els; + uint8_t *end = base + nel * elsize; + uint8_t *i, *j; + + for (i = base + elsize; i < end; i += elsize) + for (j = i; j > base && cmp(j, j - elsize, payload) < 0; j -= elsize) + swap(j, j - elsize, elsize); +} + +#endif + +void git__qsort_r( + void *els, size_t nel, size_t elsize, git__sort_r_cmp cmp, void *payload) +{ +#if defined(GIT_QSORT_GNU) + qsort_r(els, nel, elsize, cmp, payload); +#elif defined(GIT_QSORT_C11) + qsort_s(els, nel, elsize, cmp, payload); +#elif defined(GIT_QSORT_BSD) + git__qsort_r_glue glue = { cmp, payload }; + qsort_r(els, nel, elsize, &glue, git__qsort_r_glue_cmp); +#elif defined(GIT_QSORT_MSC) + git__qsort_r_glue glue = { cmp, payload }; + qsort_s(els, nel, elsize, git__qsort_r_glue_cmp, &glue); +#else + insertsort(els, nel, elsize, cmp, payload); +#endif +} + +#ifdef GIT_WIN32 +int git__getenv(git_str *out, const char *name) +{ + wchar_t *wide_name = NULL, *wide_value = NULL; + DWORD value_len; + int error = -1; + + git_str_clear(out); + + if (git_utf8_to_16_alloc(&wide_name, name) < 0) + return -1; + + if ((value_len = GetEnvironmentVariableW(wide_name, NULL, 0)) > 0) { + wide_value = git__malloc(value_len * sizeof(wchar_t)); + GIT_ERROR_CHECK_ALLOC(wide_value); + + value_len = GetEnvironmentVariableW(wide_name, wide_value, value_len); + } + + if (value_len) + error = git_str_put_w(out, wide_value, value_len); + else if (GetLastError() == ERROR_SUCCESS || GetLastError() == ERROR_ENVVAR_NOT_FOUND) + error = GIT_ENOTFOUND; + else + git_error_set(GIT_ERROR_OS, "could not read environment variable '%s'", name); + + git__free(wide_name); + git__free(wide_value); + return error; +} +#else +int git__getenv(git_str *out, const char *name) +{ + const char *val = getenv(name); + + git_str_clear(out); + + if (!val) + return GIT_ENOTFOUND; + + return git_str_puts(out, val); +} +#endif + +/* + * By doing this in two steps we can at least get + * the function to be somewhat coherent, even + * with this disgusting nest of #ifdefs. + */ +#ifndef _SC_NPROCESSORS_ONLN +# ifdef _SC_NPROC_ONLN +# define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN +# elif defined _SC_CRAY_NCPU +# define _SC_NPROCESSORS_ONLN _SC_CRAY_NCPU +# endif +#endif + +int git__online_cpus(void) +{ +#ifdef _SC_NPROCESSORS_ONLN + long ncpus; +#endif + +#ifdef _WIN32 + SYSTEM_INFO info; + GetSystemInfo(&info); + + if ((int)info.dwNumberOfProcessors > 0) + return (int)info.dwNumberOfProcessors; +#elif defined(hpux) || defined(__hpux) || defined(_hpux) + struct pst_dynamic psd; + + if (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0)) + return (int)psd.psd_proc_cnt; +#endif + +#ifdef _SC_NPROCESSORS_ONLN + if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0) + return (int)ncpus; +#endif + + return 1; +} diff --git a/src/util/util.h b/src/util/util.h new file mode 100644 index 00000000000..d3cbbf4dda4 --- /dev/null +++ b/src/util/util.h @@ -0,0 +1,363 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_util_h__ +#define INCLUDE_util_h__ + +#include "str.h" +#include "git2_util.h" +#include "strnlen.h" +#include "thread.h" + +#ifndef GIT_WIN32 +# include +#endif + +#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) +#define bitsizeof(x) (CHAR_BIT * sizeof(x)) +#define MSB(x, bits) ((x) & (~UINT64_C(0) << (bitsizeof(x) - (bits)))) +#ifndef min +# define min(a,b) ((a) < (b) ? (a) : (b)) +#endif +#ifndef max +# define max(a,b) ((a) > (b) ? (a) : (b)) +#endif + +#if defined(__GNUC__) +# define GIT_CONTAINER_OF(ptr, type, member) \ + __builtin_choose_expr( \ + __builtin_offsetof(type, member) == 0 && \ + __builtin_types_compatible_p(__typeof__(&((type *) 0)->member), __typeof__(ptr)), \ + ((type *) (ptr)), \ + (void)0) +#else +# define GIT_CONTAINER_OF(ptr, type, member) (type *)(ptr) +#endif + +/** + * Return the length of a constant string. + * We are aware that `strlen` performs the same task and is usually + * optimized away by the compiler, whilst being safer because it returns + * valid values when passed a pointer instead of a constant string; however + * this macro will transparently work with wide-char and single-char strings. + */ +#define CONST_STRLEN(x) ((sizeof(x)/sizeof(x[0])) - 1) + +#define STRCMP_CASESELECT(IGNORE_CASE, STR1, STR2) \ + ((IGNORE_CASE) ? strcasecmp((STR1), (STR2)) : strcmp((STR1), (STR2))) + +#define CASESELECT(IGNORE_CASE, ICASE, CASE) \ + ((IGNORE_CASE) ? (ICASE) : (CASE)) + +extern int git__prefixcmp(const char *str, const char *prefix); +extern int git__prefixcmp_icase(const char *str, const char *prefix); +extern int git__prefixncmp(const char *str, size_t str_n, const char *prefix); +extern int git__prefixncmp_icase(const char *str, size_t str_n, const char *prefix); +extern int git__suffixcmp(const char *str, const char *suffix); +extern int git__suffixcmp_icase(const char *str, const char *suffix); + +GIT_INLINE(int) git__signum(int val) +{ + return ((val > 0) - (val < 0)); +} + +extern int git__strntol32(int32_t *n, const char *buff, size_t buff_len, const char **end_buf, int base); +extern int git__strntol64(int64_t *n, const char *buff, size_t buff_len, const char **end_buf, int base); + + +extern void git__hexdump(const char *buffer, size_t n); +extern uint32_t git__hash(const void *key, int len, uint32_t seed); + +/* 32-bit cross-platform rotl */ +#ifdef _MSC_VER /* use built-in method in MSVC */ +# define git__rotl(v, s) (uint32_t)_rotl(v, s) +#else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */ +# define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s)))) +#endif + +extern char *git__strtok(char **end, const char *sep); +extern char *git__strsep(char **end, const char *sep); + +extern void git__strntolower(char *str, size_t len); +extern void git__strtolower(char *str); + +extern size_t git__linenlen(const char *buffer, size_t buffer_len); + +GIT_INLINE(const char *) git__next_line(const char *s) +{ + while (*s && *s != '\n') s++; + while (*s == '\n' || *s == '\r') s++; + return s; +} + +GIT_INLINE(const void *) git__memrchr(const void *s, int c, size_t n) +{ + const unsigned char *cp; + + if (n != 0) { + cp = (unsigned char *)s + n; + do { + if (*(--cp) == (unsigned char)c) + return cp; + } while (--n != 0); + } + + return NULL; +} + +extern const void * git__memmem(const void *haystack, size_t haystacklen, + const void *needle, size_t needlelen); + +typedef int (*git__tsort_cmp)(const void *a, const void *b); + +extern void git__tsort(void **dst, size_t size, git__tsort_cmp cmp); + +typedef int (*git__sort_r_cmp)(const void *a, const void *b, void *payload); + +extern void git__tsort_r( + void **dst, size_t size, git__sort_r_cmp cmp, void *payload); + +extern void git__qsort_r( + void *els, size_t nel, size_t elsize, git__sort_r_cmp cmp, void *payload); + +/** + * @param position If non-NULL, this will be set to the position where the + * element is or would be inserted if not found. + * @return 0 if found; GIT_ENOTFOUND if not found + */ +extern int git__bsearch( + void **array, + size_t array_len, + const void *key, + int (*compare)(const void *key, const void *element), + size_t *position); + +extern int git__bsearch_r( + void **array, + size_t array_len, + const void *key, + int (*compare_r)(const void *key, const void *element, void *payload), + void *payload, + size_t *position); + +#define git__strcmp strcmp +#define git__strncmp strncmp + +extern int git__strcmp_cb(const void *a, const void *b); +extern int git__strcasecmp_cb(const void *a, const void *b); + +extern int git__strcasecmp(const char *a, const char *b); +extern int git__strncasecmp(const char *a, const char *b, size_t sz); + +extern int git__strcasesort_cmp(const char *a, const char *b); + +/* + * Compare some NUL-terminated `a` to a possibly non-NUL terminated + * `b` of length `b_len`; like `strncmp` but ensuring that + * `strlen(a) == b_len` as well. + */ +GIT_INLINE(int) git__strlcmp(const char *a, const char *b, size_t b_len) +{ + int cmp = strncmp(a, b, b_len); + return cmp ? cmp : (int)a[b_len]; +} + +typedef struct { + git_atomic32 refcount; + void *owner; +} git_refcount; + +typedef void (*git_refcount_freeptr)(void *r); + +#define GIT_REFCOUNT_INC(r) { \ + git_atomic32_inc(&(r)->rc.refcount); \ +} + +#define GIT_REFCOUNT_DEC(_r, do_free) { \ + git_refcount *r = &(_r)->rc; \ + int val = git_atomic32_dec(&r->refcount); \ + if (val <= 0 && r->owner == NULL) { do_free(_r); } \ +} + +#define GIT_REFCOUNT_OWN(r, o) { \ + (void)git_atomic_swap((r)->rc.owner, o); \ +} + +#define GIT_REFCOUNT_OWNER(r) git_atomic_load((r)->rc.owner) + +#define GIT_REFCOUNT_VAL(r) git_atomic32_get((r)->rc.refcount) + + +static signed char from_hex[] = { +-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */ +-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */ +-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */ +-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */ +-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */ +-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */ +-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */ +-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */ +-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */ +-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */ +-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */ +-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */ +-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */ +-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */ +-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */ +}; + +GIT_INLINE(int) git__fromhex(char h) +{ + return from_hex[(unsigned char) h]; +} + +GIT_INLINE(int) git__ishex(const char *str) +{ + unsigned i; + for (i=0; str[i] != '\0'; i++) + if (git__fromhex(str[i]) < 0) + return 0; + return 1; +} + +GIT_INLINE(size_t) git__size_t_bitmask(size_t v) +{ + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + + return v; +} + +GIT_INLINE(size_t) git__size_t_powerof2(size_t v) +{ + return git__size_t_bitmask(v) + 1; +} + +GIT_INLINE(bool) git__isspace_nonlf(int c) +{ + return (c == ' ' || c == '\t' || c == '\f' || c == '\r' || c == '\v'); +} + +GIT_INLINE(bool) git__iswildcard(int c) +{ + return (c == '*' || c == '?' || c == '['); +} + +/* + * Parse a string value as a boolean, just like Core Git does. + * + * Valid values for true are: 'true', 'yes', 'on' + * Valid values for false are: 'false', 'no', 'off' + */ +extern int git__parse_bool(int *out, const char *value); + +/* + * Unescapes a string in-place. + * + * Edge cases behavior: + * - "jackie\" -> "jacky\" + * - "chan\\" -> "chan\" + */ +extern size_t git__unescape(char *str); + +/* + * Safely zero-out memory, making sure that the compiler + * doesn't optimize away the operation. + */ +GIT_INLINE(void) git__memzero(void *data, size_t size) +{ +#ifdef _MSC_VER + SecureZeroMemory((PVOID)data, size); +#else + volatile uint8_t *scan = (volatile uint8_t *)data; + + while (size--) + *scan++ = 0x0; +#endif +} + +#ifdef GIT_WIN32 + +GIT_INLINE(uint64_t) git_time_monotonic(void) +{ + /* GetTickCount64 returns the number of milliseconds that have + * elapsed since the system was started. */ + return GetTickCount64(); +} + +#elif __APPLE__ + +#include +#include + +GIT_INLINE(uint64_t) git_time_monotonic(void) +{ + static double scaling_factor = 0; + + if (scaling_factor == 0) { + mach_timebase_info_data_t info; + + scaling_factor = mach_timebase_info(&info) == KERN_SUCCESS ? + ((double)info.numer / (double)info.denom) / 1.0E6 : + -1; + } else if (scaling_factor < 0) { + struct timeval tv; + + /* mach_timebase_info failed; fall back to gettimeofday */ + gettimeofday(&tv, NULL); + return (tv.tv_sec * 1000) + (tv.tv_usec / 1000); + } + + return (uint64_t)(mach_absolute_time() * scaling_factor); +} + +#elif defined(__amigaos4__) + +#include + +GIT_INLINE(uint64_t) git_time_monotonic(void) +{ + struct TimeVal tv; + ITimer->GetUpTime(&tv); + return (tv.Seconds * 1000) + (tv.Microseconds / 1000); +} + +#else + +#include + +GIT_INLINE(uint64_t) git_time_monotonic(void) +{ + struct timeval tv; + +#ifdef CLOCK_MONOTONIC + struct timespec tp; + if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) + return (tp.tv_sec * 1000) + (tp.tv_nsec / 1.0E6); +#endif + + /* Fall back to using gettimeofday */ + gettimeofday(&tv, NULL); + return (tv.tv_sec * 1000) + (tv.tv_usec / 1000); +} + +#endif + +extern int git__getenv(git_str *out, const char *name); + +extern int git__online_cpus(void); + +GIT_INLINE(int) git__noop(void) { return 0; } +GIT_INLINE(int) git__noop_args(void *a, ...) { GIT_UNUSED(a); return 0; } + +#include "alloc.h" + +#endif diff --git a/src/util/varint.c b/src/util/varint.c new file mode 100644 index 00000000000..9ffc1d7448c --- /dev/null +++ b/src/util/varint.c @@ -0,0 +1,43 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "varint.h" + +uintmax_t git_decode_varint(const unsigned char *bufp, size_t *varint_len) +{ + const unsigned char *buf = bufp; + unsigned char c = *buf++; + uintmax_t val = c & 127; + while (c & 128) { + val += 1; + if (!val || MSB(val, 7)) { + /* This is not a valid varint_len, so it signals + the error */ + *varint_len = 0; + return 0; /* overflow */ + } + c = *buf++; + val = (val << 7) + (c & 127); + } + *varint_len = buf - bufp; + return val; +} + +int git_encode_varint(unsigned char *buf, size_t bufsize, uintmax_t value) +{ + unsigned char varint[16]; + unsigned pos = sizeof(varint) - 1; + varint[pos] = value & 127; + while (value >>= 7) + varint[--pos] = 128 | (--value & 127); + if (buf) { + if (bufsize < (sizeof(varint) - pos)) + return -1; + memcpy(buf, varint + pos, sizeof(varint) - pos); + } + return sizeof(varint) - pos; +} diff --git a/src/util/varint.h b/src/util/varint.h new file mode 100644 index 00000000000..79b8f5548a9 --- /dev/null +++ b/src/util/varint.h @@ -0,0 +1,17 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_varint_h__ +#define INCLUDE_varint_h__ + +#include "git2_util.h" + +#include + +extern int git_encode_varint(unsigned char *, size_t, uintmax_t); +extern uintmax_t git_decode_varint(const unsigned char *, size_t *); + +#endif diff --git a/src/util/vector.c b/src/util/vector.c new file mode 100644 index 00000000000..34ac3bb9c5b --- /dev/null +++ b/src/util/vector.c @@ -0,0 +1,431 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "vector.h" + +#include "integer.h" + +/* In elements, not bytes */ +#define MIN_ALLOCSIZE 8 + +GIT_INLINE(size_t) compute_new_size(git_vector *v) +{ + size_t new_size = v->_alloc_size; + + /* Use a resize factor of 1.5, which is quick to compute using integer + * instructions and less than the golden ratio (1.618...) */ + if (new_size < MIN_ALLOCSIZE) + new_size = MIN_ALLOCSIZE; + else if (new_size <= (SIZE_MAX / 3) * 2) + new_size += new_size / 2; + else + new_size = SIZE_MAX; + + return new_size; +} + +GIT_INLINE(int) resize_vector(git_vector *v, size_t new_size) +{ + void *new_contents; + + if (new_size == 0) + return 0; + + new_contents = git__reallocarray(v->contents, new_size, sizeof(void *)); + GIT_ERROR_CHECK_ALLOC(new_contents); + + v->_alloc_size = new_size; + v->contents = new_contents; + + return 0; +} + +int git_vector_size_hint(git_vector *v, size_t size_hint) +{ + if (v->_alloc_size >= size_hint) + return 0; + return resize_vector(v, size_hint); +} + +int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp) +{ + GIT_ASSERT_ARG(v); + GIT_ASSERT_ARG(src); + + v->_alloc_size = 0; + v->contents = NULL; + v->_cmp = cmp ? cmp : src->_cmp; + v->length = src->length; + v->flags = src->flags; + if (cmp != src->_cmp) + git_vector_set_sorted(v, 0); + + if (src->length) { + size_t bytes; + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&bytes, src->length, sizeof(void *)); + v->contents = git__malloc(bytes); + GIT_ERROR_CHECK_ALLOC(v->contents); + v->_alloc_size = src->length; + memcpy(v->contents, src->contents, bytes); + } + + return 0; +} + +void git_vector_dispose(git_vector *v) +{ + if (!v) + return; + + git__free(v->contents); + v->contents = NULL; + + v->length = 0; + v->_alloc_size = 0; +} + +void git_vector_dispose_deep(git_vector *v) +{ + size_t i; + + if (!v) + return; + + for (i = 0; i < v->length; ++i) { + git__free(v->contents[i]); + v->contents[i] = NULL; + } + + git_vector_dispose(v); +} + +int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp) +{ + GIT_ASSERT_ARG(v); + + v->_alloc_size = 0; + v->_cmp = cmp; + v->length = 0; + v->flags = GIT_VECTOR_SORTED; + v->contents = NULL; + + return resize_vector(v, max(initial_size, MIN_ALLOCSIZE)); +} + +void **git_vector_detach(size_t *size, size_t *asize, git_vector *v) +{ + void **data = v->contents; + + if (size) + *size = v->length; + if (asize) + *asize = v->_alloc_size; + + v->_alloc_size = 0; + v->length = 0; + v->contents = NULL; + + return data; +} + +int git_vector_insert(git_vector *v, void *element) +{ + GIT_ASSERT_ARG(v); + + if (v->length >= v->_alloc_size && + resize_vector(v, compute_new_size(v)) < 0) + return -1; + + v->contents[v->length++] = element; + + git_vector_set_sorted(v, v->length <= 1); + + return 0; +} + +int git_vector_insert_sorted( + git_vector *v, void *element, int (*on_dup)(void **old, void *new)) +{ + int result; + size_t pos; + + GIT_ASSERT_ARG(v); + GIT_ASSERT(v->_cmp); + + if (!git_vector_is_sorted(v)) + git_vector_sort(v); + + if (v->length >= v->_alloc_size && + resize_vector(v, compute_new_size(v)) < 0) + return -1; + + /* If we find the element and have a duplicate handler callback, + * invoke it. If it returns non-zero, then cancel insert, otherwise + * proceed with normal insert. + */ + if (!git__bsearch(v->contents, v->length, element, v->_cmp, &pos) && + on_dup && (result = on_dup(&v->contents[pos], element)) < 0) + return result; + + /* shift elements to the right */ + if (pos < v->length) + memmove(v->contents + pos + 1, v->contents + pos, + (v->length - pos) * sizeof(void *)); + + v->contents[pos] = element; + v->length++; + + return 0; +} + +void git_vector_sort(git_vector *v) +{ + if (git_vector_is_sorted(v) || !v->_cmp) + return; + + if (v->length > 1) + git__tsort(v->contents, v->length, v->_cmp); + git_vector_set_sorted(v, 1); +} + +int git_vector_bsearch2( + size_t *at_pos, + git_vector *v, + git_vector_cmp key_lookup, + const void *key) +{ + GIT_ASSERT_ARG(v); + GIT_ASSERT_ARG(key); + GIT_ASSERT(key_lookup); + + /* need comparison function to sort the vector */ + if (!v->_cmp) + return -1; + + git_vector_sort(v); + + return git__bsearch(v->contents, v->length, key, key_lookup, at_pos); +} + +int git_vector_search2( + size_t *at_pos, const git_vector *v, git_vector_cmp key_lookup, const void *key) +{ + size_t i; + + GIT_ASSERT_ARG(v); + GIT_ASSERT_ARG(key); + GIT_ASSERT(key_lookup); + + for (i = 0; i < v->length; ++i) { + if (key_lookup(key, v->contents[i]) == 0) { + if (at_pos) + *at_pos = i; + + return 0; + } + } + + return GIT_ENOTFOUND; +} + +static int strict_comparison(const void *a, const void *b) +{ + return (a == b) ? 0 : -1; +} + +int git_vector_search(size_t *at_pos, const git_vector *v, const void *entry) +{ + return git_vector_search2(at_pos, v, v->_cmp ? v->_cmp : strict_comparison, entry); +} + +int git_vector_remove(git_vector *v, size_t idx) +{ + size_t shift_count; + + GIT_ASSERT_ARG(v); + + if (idx >= v->length) + return GIT_ENOTFOUND; + + shift_count = v->length - idx - 1; + + if (shift_count) + memmove(&v->contents[idx], &v->contents[idx + 1], + shift_count * sizeof(void *)); + + v->length--; + return 0; +} + +void git_vector_pop(git_vector *v) +{ + if (v->length > 0) + v->length--; +} + +void git_vector_uniq(git_vector *v, void (*git_free_cb)(void *)) +{ + git_vector_cmp cmp; + size_t i, j; + + if (v->length <= 1) + return; + + git_vector_sort(v); + cmp = v->_cmp ? v->_cmp : strict_comparison; + + for (i = 0, j = 1 ; j < v->length; ++j) + if (!cmp(v->contents[i], v->contents[j])) { + if (git_free_cb) + git_free_cb(v->contents[i]); + + v->contents[i] = v->contents[j]; + } else + v->contents[++i] = v->contents[j]; + + v->length -= j - i - 1; +} + +void git_vector_remove_matching( + git_vector *v, + int (*match)(const git_vector *v, size_t idx, void *payload), + void *payload) +{ + size_t i, j; + + for (i = 0, j = 0; j < v->length; ++j) { + v->contents[i] = v->contents[j]; + + if (!match(v, i, payload)) + i++; + } + + v->length = i; +} + +void git_vector_clear(git_vector *v) +{ + v->length = 0; + git_vector_set_sorted(v, 1); +} + +void git_vector_swap(git_vector *a, git_vector *b) +{ + git_vector t; + + if (a != b) { + memcpy(&t, a, sizeof(t)); + memcpy(a, b, sizeof(t)); + memcpy(b, &t, sizeof(t)); + } +} + +int git_vector_resize_to(git_vector *v, size_t new_length) +{ + if (new_length > v->_alloc_size && + resize_vector(v, new_length) < 0) + return -1; + + if (new_length > v->length) + memset(&v->contents[v->length], 0, + sizeof(void *) * (new_length - v->length)); + + v->length = new_length; + + return 0; +} + +int git_vector_insert_null(git_vector *v, size_t idx, size_t insert_len) +{ + size_t new_length; + + GIT_ASSERT_ARG(insert_len > 0); + GIT_ASSERT_ARG(idx <= v->length); + + GIT_ERROR_CHECK_ALLOC_ADD(&new_length, v->length, insert_len); + + if (new_length > v->_alloc_size && resize_vector(v, new_length) < 0) + return -1; + + memmove(&v->contents[idx + insert_len], &v->contents[idx], + sizeof(void *) * (v->length - idx)); + memset(&v->contents[idx], 0, sizeof(void *) * insert_len); + + v->length = new_length; + return 0; +} + +int git_vector_remove_range(git_vector *v, size_t idx, size_t remove_len) +{ + size_t new_length = v->length - remove_len; + size_t end_idx = 0; + + GIT_ASSERT_ARG(remove_len > 0); + + if (git__add_sizet_overflow(&end_idx, idx, remove_len)) + GIT_ASSERT(0); + + GIT_ASSERT(end_idx <= v->length); + + if (end_idx < v->length) + memmove(&v->contents[idx], &v->contents[end_idx], + sizeof(void *) * (v->length - end_idx)); + + memset(&v->contents[new_length], 0, sizeof(void *) * remove_len); + + v->length = new_length; + return 0; +} + +int git_vector_set(void **old, git_vector *v, size_t position, void *value) +{ + if (position + 1 > v->length) { + if (git_vector_resize_to(v, position + 1) < 0) + return -1; + } + + if (old != NULL) + *old = v->contents[position]; + + v->contents[position] = value; + + return 0; +} + +int git_vector_verify_sorted(const git_vector *v) +{ + size_t i; + + if (!git_vector_is_sorted(v)) + return -1; + + for (i = 1; i < v->length; ++i) { + if (v->_cmp(v->contents[i - 1], v->contents[i]) > 0) + return -1; + } + + return 0; +} + +void git_vector_reverse(git_vector *v) +{ + size_t a, b; + + if (v->length == 0) + return; + + a = 0; + b = v->length - 1; + + while (a < b) { + void *tmp = v->contents[a]; + v->contents[a] = v->contents[b]; + v->contents[b] = tmp; + a++; + b--; + } +} diff --git a/src/util/vector.h b/src/util/vector.h new file mode 100644 index 00000000000..02f82b30d03 --- /dev/null +++ b/src/util/vector.h @@ -0,0 +1,128 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_vector_h__ +#define INCLUDE_vector_h__ + +#include "git2_util.h" + +typedef int (*git_vector_cmp)(const void *, const void *); + +enum { + GIT_VECTOR_SORTED = (1u << 0), + GIT_VECTOR_FLAG_MAX = (1u << 1) +}; + +typedef struct git_vector { + size_t _alloc_size; + git_vector_cmp _cmp; + void **contents; + size_t length; + uint32_t flags; +} git_vector; + +#define GIT_VECTOR_INIT {0} + +GIT_WARN_UNUSED_RESULT int git_vector_init( + git_vector *v, size_t initial_size, git_vector_cmp cmp); +void git_vector_dispose(git_vector *v); +void git_vector_dispose_deep(git_vector *v); /* free each entry and self */ +void git_vector_clear(git_vector *v); +GIT_WARN_UNUSED_RESULT int git_vector_dup( + git_vector *v, const git_vector *src, git_vector_cmp cmp); +void git_vector_swap(git_vector *a, git_vector *b); +int git_vector_size_hint(git_vector *v, size_t size_hint); + +void **git_vector_detach(size_t *size, size_t *asize, git_vector *v); + +void git_vector_sort(git_vector *v); + +/** Linear search for matching entry using internal comparison function */ +int git_vector_search(size_t *at_pos, const git_vector *v, const void *entry); + +/** Linear search for matching entry using explicit comparison function */ +int git_vector_search2(size_t *at_pos, const git_vector *v, git_vector_cmp cmp, const void *key); + +/** + * Binary search for matching entry using explicit comparison function that + * returns position where item would go if not found. + */ +int git_vector_bsearch2( + size_t *at_pos, git_vector *v, git_vector_cmp cmp, const void *key); + +/** Binary search for matching entry using internal comparison function */ +GIT_INLINE(int) git_vector_bsearch(size_t *at_pos, git_vector *v, const void *key) +{ + return git_vector_bsearch2(at_pos, v, v->_cmp, key); +} + +GIT_INLINE(void *) git_vector_get(const git_vector *v, size_t position) +{ + return (position < v->length) ? v->contents[position] : NULL; +} + +#define GIT_VECTOR_GET(V,I) ((I) < (V)->length ? (V)->contents[(I)] : NULL) + +GIT_INLINE(size_t) git_vector_length(const git_vector *v) +{ + return v->length; +} + +GIT_INLINE(void *) git_vector_last(const git_vector *v) +{ + return (v->length > 0) ? git_vector_get(v, v->length - 1) : NULL; +} + +#define git_vector_foreach(v, iter, elem) \ + for ((iter) = 0; (iter) < (v)->length && ((elem) = (v)->contents[(iter)], 1); (iter)++ ) + +#define git_vector_rforeach(v, iter, elem) \ + for ((iter) = (v)->length - 1; (iter) < SIZE_MAX && ((elem) = (v)->contents[(iter)], 1); (iter)-- ) + +int git_vector_insert(git_vector *v, void *element); +int git_vector_insert_sorted(git_vector *v, void *element, + int (*on_dup)(void **old, void *new)); +int git_vector_remove(git_vector *v, size_t idx); +void git_vector_pop(git_vector *v); +void git_vector_uniq(git_vector *v, void (*git_free_cb)(void *)); + +void git_vector_remove_matching( + git_vector *v, + int (*match)(const git_vector *v, size_t idx, void *payload), + void *payload); + +int git_vector_resize_to(git_vector *v, size_t new_length); +int git_vector_insert_null(git_vector *v, size_t idx, size_t insert_len); +int git_vector_remove_range(git_vector *v, size_t idx, size_t remove_len); + +int git_vector_set(void **old, git_vector *v, size_t position, void *value); + +/** Check if vector is sorted */ +#define git_vector_is_sorted(V) (((V)->flags & GIT_VECTOR_SORTED) != 0) + +/** Directly set sorted state of vector */ +#define git_vector_set_sorted(V,S) do { \ + (V)->flags = (S) ? ((V)->flags | GIT_VECTOR_SORTED) : \ + ((V)->flags & ~GIT_VECTOR_SORTED); } while (0) + +/** Set the comparison function used for sorting the vector */ +GIT_INLINE(void) git_vector_set_cmp(git_vector *v, git_vector_cmp cmp) +{ + if (cmp != v->_cmp) { + v->_cmp = cmp; + git_vector_set_sorted(v, 0); + } +} + +/* Just use this in tests, not for realz. returns -1 if not sorted */ +int git_vector_verify_sorted(const git_vector *v); + +/** + * Reverse the vector in-place. + */ +void git_vector_reverse(git_vector *v); + +#endif diff --git a/src/util/wildmatch.c b/src/util/wildmatch.c new file mode 100644 index 00000000000..a894e484159 --- /dev/null +++ b/src/util/wildmatch.c @@ -0,0 +1,320 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + * + * Do shell-style pattern matching for ?, \, [], and * characters. + * It is 8bit clean. + * + * Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986. + * Rich $alz is now . + * + * Modified by Wayne Davison to special-case '/' matching, to make '**' + * work differently than '*', and to fix the character-class code. + * + * Imported from git.git. + */ + +#include "wildmatch.h" + +#define GIT_SPACE 0x01 +#define GIT_DIGIT 0x02 +#define GIT_ALPHA 0x04 +#define GIT_GLOB_SPECIAL 0x08 +#define GIT_REGEX_SPECIAL 0x10 +#define GIT_PATHSPEC_MAGIC 0x20 +#define GIT_CNTRL 0x40 +#define GIT_PUNCT 0x80 + +enum { + S = GIT_SPACE, + A = GIT_ALPHA, + D = GIT_DIGIT, + G = GIT_GLOB_SPECIAL, /* *, ?, [, \\ */ + R = GIT_REGEX_SPECIAL, /* $, (, ), +, ., ^, {, | */ + P = GIT_PATHSPEC_MAGIC, /* other non-alnum, except for ] and } */ + X = GIT_CNTRL, + U = GIT_PUNCT, + Z = GIT_CNTRL | GIT_SPACE +}; + +static const unsigned char sane_ctype[256] = { + X, X, X, X, X, X, X, X, X, Z, Z, X, X, Z, X, X, /* 0.. 15 */ + X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, /* 16.. 31 */ + S, P, P, P, R, P, P, P, R, R, G, R, P, P, R, P, /* 32.. 47 */ + D, D, D, D, D, D, D, D, D, D, P, P, P, P, P, G, /* 48.. 63 */ + P, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 64.. 79 */ + A, A, A, A, A, A, A, A, A, A, A, G, G, U, R, P, /* 80.. 95 */ + P, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 96..111 */ + A, A, A, A, A, A, A, A, A, A, A, R, R, U, P, X, /* 112..127 */ + /* Nothing in the 128.. range */ +}; + +#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0) +#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL) + +typedef unsigned char uchar; + +/* What character marks an inverted character class? */ +#define NEGATE_CLASS '!' +#define NEGATE_CLASS2 '^' + +#define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \ + && *(class) == *(litmatch) \ + && strncmp((char*)class, litmatch, len) == 0) + +#if defined STDC_HEADERS || !defined isascii +# define ISASCII(c) 1 +#else +# define ISASCII(c) isascii(c) +#endif + +#ifdef isblank +# define ISBLANK(c) (ISASCII(c) && isblank(c)) +#else +# define ISBLANK(c) ((c) == ' ' || (c) == '\t') +#endif + +#ifdef isgraph +# define ISGRAPH(c) (ISASCII(c) && isgraph(c)) +#else +# define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c)) +#endif + +#define ISPRINT(c) (ISASCII(c) && isprint(c)) +#define ISDIGIT(c) (ISASCII(c) && isdigit(c)) +#define ISALNUM(c) (ISASCII(c) && isalnum(c)) +#define ISALPHA(c) (ISASCII(c) && isalpha(c)) +#define ISCNTRL(c) (ISASCII(c) && iscntrl(c)) +#define ISLOWER(c) (ISASCII(c) && islower(c)) +#define ISPUNCT(c) (ISASCII(c) && ispunct(c)) +#define ISSPACE(c) (ISASCII(c) && isspace(c)) +#define ISUPPER(c) (ISASCII(c) && isupper(c)) +#define ISXDIGIT(c) (ISASCII(c) && isxdigit(c)) + +/* Match pattern "p" against "text" */ +static int dowild(const uchar *p, const uchar *text, unsigned int flags) +{ + uchar p_ch; + const uchar *pattern = p; + + for ( ; (p_ch = *p) != '\0'; text++, p++) { + int matched, match_slash, negated; + uchar t_ch, prev_ch; + if ((t_ch = *text) == '\0' && p_ch != '*') + return WM_ABORT_ALL; + if ((flags & WM_CASEFOLD) && ISUPPER(t_ch)) + t_ch = tolower(t_ch); + if ((flags & WM_CASEFOLD) && ISUPPER(p_ch)) + p_ch = tolower(p_ch); + switch (p_ch) { + case '\\': + /* Literal match with following character. Note that the test + * in "default" handles the p[1] == '\0' failure case. */ + p_ch = *++p; + /* FALLTHROUGH */ + default: + if (t_ch != p_ch) + return WM_NOMATCH; + continue; + case '?': + /* Match anything but '/'. */ + if ((flags & WM_PATHNAME) && t_ch == '/') + return WM_NOMATCH; + continue; + case '*': + if (*++p == '*') { + const uchar *prev_p = p - 2; + while (*++p == '*') {} + if (!(flags & WM_PATHNAME)) + /* without WM_PATHNAME, '*' == '**' */ + match_slash = 1; + else if ((prev_p < pattern || *prev_p == '/') && + (*p == '\0' || *p == '/' || + (p[0] == '\\' && p[1] == '/'))) { + /* + * Assuming we already match 'foo/' and are at + * , just assume it matches + * nothing and go ahead match the rest of the + * pattern with the remaining string. This + * helps make foo/<*><*>/bar (<> because + * otherwise it breaks C comment syntax) match + * both foo/bar and foo/a/bar. + */ + if (p[0] == '/' && + dowild(p + 1, text, flags) == WM_MATCH) + return WM_MATCH; + match_slash = 1; + } else /* WM_PATHNAME is set */ + match_slash = 0; + } else + /* without WM_PATHNAME, '*' == '**' */ + match_slash = flags & WM_PATHNAME ? 0 : 1; + if (*p == '\0') { + /* Trailing "**" matches everything. Trailing "*" matches + * only if there are no more slash characters. */ + if (!match_slash) { + if (strchr((char*)text, '/') != NULL) + return WM_NOMATCH; + } + return WM_MATCH; + } else if (!match_slash && *p == '/') { + /* + * _one_ asterisk followed by a slash + * with WM_PATHNAME matches the next + * directory + */ + const char *slash = strchr((char*)text, '/'); + if (!slash) + return WM_NOMATCH; + text = (const uchar*)slash; + /* the slash is consumed by the top-level for loop */ + break; + } + while (1) { + if (t_ch == '\0') + break; + /* + * Try to advance faster when an asterisk is + * followed by a literal. We know in this case + * that the string before the literal + * must belong to "*". + * If match_slash is false, do not look past + * the first slash as it cannot belong to '*'. + */ + if (!is_glob_special(*p)) { + p_ch = *p; + if ((flags & WM_CASEFOLD) && ISUPPER(p_ch)) + p_ch = tolower(p_ch); + while ((t_ch = *text) != '\0' && + (match_slash || t_ch != '/')) { + if ((flags & WM_CASEFOLD) && ISUPPER(t_ch)) + t_ch = tolower(t_ch); + if (t_ch == p_ch) + break; + text++; + } + if (t_ch != p_ch) + return WM_NOMATCH; + } + if ((matched = dowild(p, text, flags)) != WM_NOMATCH) { + if (!match_slash || matched != WM_ABORT_TO_STARSTAR) + return matched; + } else if (!match_slash && t_ch == '/') + return WM_ABORT_TO_STARSTAR; + t_ch = *++text; + } + return WM_ABORT_ALL; + case '[': + p_ch = *++p; +#ifdef NEGATE_CLASS2 + if (p_ch == NEGATE_CLASS2) + p_ch = NEGATE_CLASS; +#endif + /* Assign literal 1/0 because of "matched" comparison. */ + negated = p_ch == NEGATE_CLASS ? 1 : 0; + if (negated) { + /* Inverted character class. */ + p_ch = *++p; + } + prev_ch = 0; + matched = 0; + do { + if (!p_ch) + return WM_ABORT_ALL; + if (p_ch == '\\') { + p_ch = *++p; + if (!p_ch) + return WM_ABORT_ALL; + if (t_ch == p_ch) + matched = 1; + } else if (p_ch == '-' && prev_ch && p[1] && p[1] != ']') { + p_ch = *++p; + if (p_ch == '\\') { + p_ch = *++p; + if (!p_ch) + return WM_ABORT_ALL; + } + if (t_ch <= p_ch && t_ch >= prev_ch) + matched = 1; + else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch)) { + uchar t_ch_upper = toupper(t_ch); + if (t_ch_upper <= p_ch && t_ch_upper >= prev_ch) + matched = 1; + } + p_ch = 0; /* This makes "prev_ch" get set to 0. */ + } else if (p_ch == '[' && p[1] == ':') { + const uchar *s; + int i; + for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/ + if (!p_ch) + return WM_ABORT_ALL; + i = (int)(p - s - 1); + if (i < 0 || p[-1] != ':') { + /* Didn't find ":]", so treat like a normal set. */ + p = s - 2; + p_ch = '['; + if (t_ch == p_ch) + matched = 1; + continue; + } + if (CC_EQ(s,i, "alnum")) { + if (ISALNUM(t_ch)) + matched = 1; + } else if (CC_EQ(s,i, "alpha")) { + if (ISALPHA(t_ch)) + matched = 1; + } else if (CC_EQ(s,i, "blank")) { + if (ISBLANK(t_ch)) + matched = 1; + } else if (CC_EQ(s,i, "cntrl")) { + if (ISCNTRL(t_ch)) + matched = 1; + } else if (CC_EQ(s,i, "digit")) { + if (ISDIGIT(t_ch)) + matched = 1; + } else if (CC_EQ(s,i, "graph")) { + if (ISGRAPH(t_ch)) + matched = 1; + } else if (CC_EQ(s,i, "lower")) { + if (ISLOWER(t_ch)) + matched = 1; + } else if (CC_EQ(s,i, "print")) { + if (ISPRINT(t_ch)) + matched = 1; + } else if (CC_EQ(s,i, "punct")) { + if (ISPUNCT(t_ch)) + matched = 1; + } else if (CC_EQ(s,i, "space")) { + if (ISSPACE(t_ch)) + matched = 1; + } else if (CC_EQ(s,i, "upper")) { + if (ISUPPER(t_ch)) + matched = 1; + else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch)) + matched = 1; + } else if (CC_EQ(s,i, "xdigit")) { + if (ISXDIGIT(t_ch)) + matched = 1; + } else /* malformed [:class:] string */ + return WM_ABORT_ALL; + p_ch = 0; /* This makes "prev_ch" get set to 0. */ + } else if (t_ch == p_ch) + matched = 1; + } while (prev_ch = p_ch, (p_ch = *++p) != ']'); + if (matched == negated || + ((flags & WM_PATHNAME) && t_ch == '/')) + return WM_NOMATCH; + continue; + } + } + + return *text ? WM_NOMATCH : WM_MATCH; +} + +/* Match the "pattern" against the "text" string. */ +int wildmatch(const char *pattern, const char *text, unsigned int flags) +{ + return dowild((const uchar*)pattern, (const uchar*)text, flags); +} diff --git a/src/util/wildmatch.h b/src/util/wildmatch.h new file mode 100644 index 00000000000..f2064050025 --- /dev/null +++ b/src/util/wildmatch.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_wildmatch_h__ +#define INCLUDE_wildmatch_h__ + +#include "git2_util.h" + +#define WM_CASEFOLD 1 +#define WM_PATHNAME 2 + +#define WM_NOMATCH 1 +#define WM_MATCH 0 +#define WM_ABORT_ALL -1 +#define WM_ABORT_TO_STARSTAR -2 + +int wildmatch(const char *pattern, const char *text, unsigned int flags); + +#endif diff --git a/src/util/win32/dir.c b/src/util/win32/dir.c new file mode 100644 index 00000000000..44052caf04a --- /dev/null +++ b/src/util/win32/dir.c @@ -0,0 +1,122 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "dir.h" + +#define GIT__WIN32_NO_WRAP_DIR +#include "posix.h" + +git__DIR *git__opendir(const char *dir) +{ + git_win32_path filter_w; + git__DIR *new = NULL; + size_t dirlen, alloclen; + + if (!dir || !git_win32__findfirstfile_filter(filter_w, dir)) + return NULL; + + dirlen = strlen(dir); + + if (GIT_ADD_SIZET_OVERFLOW(&alloclen, sizeof(*new), dirlen) || + GIT_ADD_SIZET_OVERFLOW(&alloclen, alloclen, 1) || + !(new = git__calloc(1, alloclen))) + return NULL; + + memcpy(new->dir, dir, dirlen); + + new->h = FindFirstFileW(filter_w, &new->f); + + if (new->h == INVALID_HANDLE_VALUE) { + git_error_set(GIT_ERROR_OS, "could not open directory '%s'", dir); + git__free(new); + return NULL; + } + + new->first = 1; + return new; +} + +int git__readdir_ext( + git__DIR *d, + struct git__dirent *entry, + struct git__dirent **result, + int *is_dir) +{ + if (!d || !entry || !result || d->h == INVALID_HANDLE_VALUE) + return -1; + + *result = NULL; + + if (d->first) + d->first = 0; + else if (!FindNextFileW(d->h, &d->f)) { + if (GetLastError() == ERROR_NO_MORE_FILES) + return 0; + git_error_set(GIT_ERROR_OS, "could not read from directory '%s'", d->dir); + return -1; + } + + /* Convert the path to UTF-8 */ + if (git_win32_path_to_utf8(entry->d_name, d->f.cFileName) < 0) + return -1; + + entry->d_ino = 0; + + *result = entry; + + if (is_dir != NULL) + *is_dir = ((d->f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0); + + return 0; +} + +struct git__dirent *git__readdir(git__DIR *d) +{ + struct git__dirent *result; + if (git__readdir_ext(d, &d->entry, &result, NULL) < 0) + return NULL; + return result; +} + +void git__rewinddir(git__DIR *d) +{ + git_win32_path filter_w; + + if (!d) + return; + + if (d->h != INVALID_HANDLE_VALUE) { + FindClose(d->h); + d->h = INVALID_HANDLE_VALUE; + d->first = 0; + } + + if (!git_win32__findfirstfile_filter(filter_w, d->dir)) + return; + + d->h = FindFirstFileW(filter_w, &d->f); + + if (d->h == INVALID_HANDLE_VALUE) + git_error_set(GIT_ERROR_OS, "could not open directory '%s'", d->dir); + else + d->first = 1; +} + +int git__closedir(git__DIR *d) +{ + if (!d) + return 0; + + if (d->h != INVALID_HANDLE_VALUE) { + FindClose(d->h); + d->h = INVALID_HANDLE_VALUE; + } + + git__free(d); + return 0; +} + diff --git a/src/win32/dir.h b/src/util/win32/dir.h similarity index 84% rename from src/win32/dir.h rename to src/util/win32/dir.h index 7696d468e52..810111534df 100644 --- a/src/win32/dir.h +++ b/src/util/win32/dir.h @@ -4,22 +4,24 @@ * This file is part of libgit2, distributed under the GNU GPL v2 with * a Linking Exception. For full terms see the included COPYING file. */ -#ifndef INCLUDE_dir_h__ -#define INCLUDE_dir_h__ +#ifndef INCLUDE_win32_dir_h__ +#define INCLUDE_win32_dir_h__ -#include "common.h" +#include "git2_util.h" + +#include "w32_util.h" struct git__dirent { int d_ino; - char d_name[261]; + git_win32_utf8_path d_name; }; typedef struct { HANDLE h; WIN32_FIND_DATAW f; struct git__dirent entry; - char *dir; int first; + char dir[GIT_FLEX_ARRAY]; } git__DIR; extern git__DIR *git__opendir(const char *); @@ -39,4 +41,4 @@ extern int git__closedir(git__DIR *); # define closedir git__closedir # endif -#endif /* INCLUDE_dir_h__ */ +#endif diff --git a/src/util/win32/error.c b/src/util/win32/error.c new file mode 100644 index 00000000000..141b1ad4cef --- /dev/null +++ b/src/util/win32/error.c @@ -0,0 +1,53 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "error.h" + +#include "utf-conv.h" + +#ifdef GIT_HTTPS_WINHTTP +# include +#endif + +char *git_win32_get_error_message(DWORD error_code) +{ + LPWSTR lpMsgBuf = NULL; + HMODULE hModule = NULL; + char *utf8_msg = NULL; + DWORD dwFlags = + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS; + + if (!error_code) + return NULL; + +#ifdef GIT_HTTPS_WINHTTP + /* Errors raised by WinHTTP are not in the system resource table */ + if (error_code >= WINHTTP_ERROR_BASE && + error_code <= WINHTTP_ERROR_LAST) + hModule = GetModuleHandleW(L"winhttp"); +#endif + + GIT_UNUSED(hModule); + + if (hModule) + dwFlags |= FORMAT_MESSAGE_FROM_HMODULE; + else + dwFlags |= FORMAT_MESSAGE_FROM_SYSTEM; + + if (FormatMessageW(dwFlags, hModule, error_code, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPWSTR)&lpMsgBuf, 0, NULL)) { + /* Convert the message to UTF-8. If this fails, we will + * return NULL, which is a condition expected by the caller */ + if (git_utf8_from_16_alloc(&utf8_msg, lpMsgBuf) < 0) + utf8_msg = NULL; + + LocalFree(lpMsgBuf); + } + + return utf8_msg; +} diff --git a/src/util/win32/error.h b/src/util/win32/error.h new file mode 100644 index 00000000000..fd53b7f99a8 --- /dev/null +++ b/src/util/win32/error.h @@ -0,0 +1,15 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_win32_error_h__ +#define INCLUDE_win32_error_h__ + +#include "git2_util.h" + +extern char *git_win32_get_error_message(DWORD error_code); + +#endif diff --git a/src/util/win32/map.c b/src/util/win32/map.c new file mode 100644 index 00000000000..52e1363eac9 --- /dev/null +++ b/src/util/win32/map.c @@ -0,0 +1,141 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2_util.h" + +#include "map.h" +#include + +#ifndef NO_MMAP + +static DWORD get_page_size(void) +{ + static DWORD page_size; + SYSTEM_INFO sys; + + if (!page_size) { + GetSystemInfo(&sys); + page_size = sys.dwPageSize; + } + + return page_size; +} + +static DWORD get_allocation_granularity(void) +{ + static DWORD granularity; + SYSTEM_INFO sys; + + if (!granularity) { + GetSystemInfo(&sys); + granularity = sys.dwAllocationGranularity; + } + + return granularity; +} + +int git__page_size(size_t *page_size) +{ + *page_size = get_page_size(); + return 0; +} + +int git__mmap_alignment(size_t *page_size) +{ + *page_size = get_allocation_granularity(); + return 0; +} + +int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, off64_t offset) +{ + HANDLE fh = (HANDLE)_get_osfhandle(fd); + DWORD alignment = get_allocation_granularity(); + DWORD fmap_prot = 0; + DWORD view_prot = 0; + DWORD off_low = 0; + DWORD off_hi = 0; + off64_t page_start; + off64_t page_offset; + + GIT_MMAP_VALIDATE(out, len, prot, flags); + + out->data = NULL; + out->len = 0; + out->fmh = NULL; + + if (fh == INVALID_HANDLE_VALUE) { + errno = EBADF; + git_error_set(GIT_ERROR_OS, "failed to mmap. Invalid handle value"); + return -1; + } + + if (prot & GIT_PROT_WRITE) + fmap_prot |= PAGE_READWRITE; + else if (prot & GIT_PROT_READ) + fmap_prot |= PAGE_READONLY; + + if (prot & GIT_PROT_WRITE) + view_prot |= FILE_MAP_WRITE; + if (prot & GIT_PROT_READ) + view_prot |= FILE_MAP_READ; + + page_start = (offset / alignment) * alignment; + page_offset = offset - page_start; + + if (page_offset != 0) { /* offset must be multiple of the allocation granularity */ + errno = EINVAL; + git_error_set(GIT_ERROR_OS, "failed to mmap. Offset must be multiple of allocation granularity"); + return -1; + } + + out->fmh = CreateFileMapping(fh, NULL, fmap_prot, 0, 0, NULL); + if (!out->fmh || out->fmh == INVALID_HANDLE_VALUE) { + git_error_set(GIT_ERROR_OS, "failed to mmap. Invalid handle value"); + out->fmh = NULL; + return -1; + } + + off_low = (DWORD)(page_start); + off_hi = (DWORD)(page_start >> 32); + out->data = MapViewOfFile(out->fmh, view_prot, off_hi, off_low, len); + if (!out->data) { + git_error_set(GIT_ERROR_OS, "failed to mmap. No data written"); + CloseHandle(out->fmh); + out->fmh = NULL; + return -1; + } + out->len = len; + + return 0; +} + +int p_munmap(git_map *map) +{ + int error = 0; + + GIT_ASSERT_ARG(map); + + if (map->data) { + if (!UnmapViewOfFile(map->data)) { + git_error_set(GIT_ERROR_OS, "failed to munmap. Could not unmap view of file"); + error = -1; + } + map->data = NULL; + } + + if (map->fmh) { + if (!CloseHandle(map->fmh)) { + git_error_set(GIT_ERROR_OS, "failed to munmap. Could not close handle"); + error = -1; + } + map->fmh = NULL; + } + + return error; +} + +#endif diff --git a/src/util/win32/mingw-compat.h b/src/util/win32/mingw-compat.h new file mode 100644 index 00000000000..aa2bef98df7 --- /dev/null +++ b/src/util/win32/mingw-compat.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_win32_mingw_compat_h__ +#define INCLUDE_win32_mingw_compat_h__ + +#if defined(__MINGW32__) + +#undef stat + +#if _WIN32_WINNT < 0x0600 && !defined(__MINGW64_VERSION_MAJOR) +#undef MemoryBarrier +void __mingworg_MemoryBarrier(void); +#define MemoryBarrier __mingworg_MemoryBarrier +#define VOLUME_NAME_DOS 0x0 +#endif + +#endif + +#endif diff --git a/src/util/win32/msvc-compat.h b/src/util/win32/msvc-compat.h new file mode 100644 index 00000000000..03f9f36dc92 --- /dev/null +++ b/src/util/win32/msvc-compat.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_win32_msvc_compat_h__ +#define INCLUDE_win32_msvc_compat_h__ + +#if defined(_MSC_VER) + +typedef unsigned short mode_t; +typedef SSIZE_T ssize_t; + +#ifdef _WIN64 +# define SSIZE_MAX _I64_MAX +#else +# define SSIZE_MAX LONG_MAX +#endif + +#define strcasecmp(s1, s2) _stricmp(s1, s2) +#define strncasecmp(s1, s2, c) _strnicmp(s1, s2, c) + +#endif + +/* + * Offer GIT_LIBGIT2_CALL for our calling conventions (__cdecl, always). + * This is useful for providing callbacks to userspace code. + * + * Offer GIT_SYSTEM_CALL for the system calling conventions (__stdcall on + * Win32). Useful for providing callbacks to system libraries. + */ +#define GIT_LIBGIT2_CALL __cdecl +#define GIT_SYSTEM_CALL NTAPI + +#endif diff --git a/src/util/win32/path_w32.c b/src/util/win32/path_w32.c new file mode 100644 index 00000000000..2714efbb653 --- /dev/null +++ b/src/util/win32/path_w32.c @@ -0,0 +1,718 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "path_w32.h" + +#include "fs_path.h" +#include "utf-conv.h" +#include "posix.h" +#include "reparse.h" +#include "dir.h" + +#define PATH__NT_NAMESPACE L"\\\\?\\" +#define PATH__NT_NAMESPACE_LEN 4 + +#define PATH__ABSOLUTE_LEN 3 + +#define path__is_nt_namespace(p) \ + (((p)[0] == '\\' && (p)[1] == '\\' && (p)[2] == '?' && (p)[3] == '\\') || \ + ((p)[0] == '/' && (p)[1] == '/' && (p)[2] == '?' && (p)[3] == '/')) + +#define path__is_unc(p) \ + (((p)[0] == '\\' && (p)[1] == '\\') || ((p)[0] == '/' && (p)[1] == '/')) + +#define path__startswith_slash(p) \ + ((p)[0] == '\\' || (p)[0] == '/') + +GIT_INLINE(int) path__cwd(wchar_t *path, int size) +{ + int len; + + if ((len = GetCurrentDirectoryW(size, path)) == 0) { + errno = GetLastError() == ERROR_ACCESS_DENIED ? EACCES : ENOENT; + return -1; + } else if (len > size) { + errno = ENAMETOOLONG; + return -1; + } + + /* The Win32 APIs may return "\\?\" once you've used it first. + * But it may not. What a gloriously predictable API! + */ + if (wcsncmp(path, PATH__NT_NAMESPACE, PATH__NT_NAMESPACE_LEN)) + return len; + + len -= PATH__NT_NAMESPACE_LEN; + + memmove(path, path + PATH__NT_NAMESPACE_LEN, sizeof(wchar_t) * len); + return len; +} + +static wchar_t *path__skip_server(wchar_t *path) +{ + wchar_t *c; + + for (c = path; *c; c++) { + if (git_fs_path_is_dirsep(*c)) + return c + 1; + } + + return c; +} + +static wchar_t *path__skip_prefix(wchar_t *path) +{ + if (path__is_nt_namespace(path)) { + path += PATH__NT_NAMESPACE_LEN; + + if (wcsncmp(path, L"UNC\\", 4) == 0) + path = path__skip_server(path + 4); + else if (git_fs_path_is_absolute(path)) + path += PATH__ABSOLUTE_LEN; + } else if (git_fs_path_is_absolute(path)) { + path += PATH__ABSOLUTE_LEN; + } else if (path__is_unc(path)) { + path = path__skip_server(path + 2); + } + + return path; +} + +int git_win32_path_canonicalize(git_win32_path path) +{ + wchar_t *base, *from, *to, *next; + size_t len; + + base = to = path__skip_prefix(path); + + /* Unposixify if the prefix */ + for (from = path; from < to; from++) { + if (*from == L'/') + *from = L'\\'; + } + + while (*from) { + for (next = from; *next; ++next) { + if (*next == L'/') { + *next = L'\\'; + break; + } + + if (*next == L'\\') + break; + } + + len = next - from; + + if (len == 1 && from[0] == L'.') + /* do nothing with singleton dot */; + + else if (len == 2 && from[0] == L'.' && from[1] == L'.') { + if (to == base) { + /* no more path segments to strip, eat the "../" */ + if (*next == L'\\') + len++; + + base = to; + } else { + /* back up a path segment */ + while (to > base && to[-1] == L'\\') to--; + while (to > base && to[-1] != L'\\') to--; + } + } else { + if (*next == L'\\' && *from != L'\\') + len++; + + if (to != from) + memmove(to, from, sizeof(wchar_t) * len); + + to += len; + } + + from += len; + + while (*from == L'\\') from++; + } + + /* Strip trailing backslashes */ + while (to > base && to[-1] == L'\\') to--; + + *to = L'\0'; + + if ((to - path) > INT_MAX) { + SetLastError(ERROR_FILENAME_EXCED_RANGE); + return -1; + } + + return (int)(to - path); +} + +static int git_win32_path_join( + git_win32_path dest, + size_t *dest_len, + const wchar_t *one, + size_t one_len, + const wchar_t *two, + size_t two_len) +{ + size_t backslash = 0; + + if (one_len && two_len && one[one_len - 1] != L'\\') + backslash = 1; + + if (one_len + two_len + backslash > MAX_PATH) { + git_error_set(GIT_ERROR_INVALID, "path too long"); + return -1; + } + + memmove(dest, one, one_len * sizeof(wchar_t)); + + if (backslash) + dest[one_len] = L'\\'; + + memcpy(dest + one_len + backslash, two, two_len * sizeof(wchar_t)); + dest[one_len + backslash + two_len] = L'\0'; + + if (dest_len) + *dest_len = one_len + backslash + two_len; + + return 0; +} + +struct win32_path_iter { + wchar_t *env; + const wchar_t *current_dir; +}; + +static int win32_path_iter_init(struct win32_path_iter *iter) +{ + DWORD len = GetEnvironmentVariableW(L"PATH", NULL, 0); + + if (!len && GetLastError() == ERROR_ENVVAR_NOT_FOUND) { + iter->env = NULL; + iter->current_dir = NULL; + return 0; + } else if (!len) { + git_error_set(GIT_ERROR_OS, "could not load PATH"); + return -1; + } + + iter->env = git__malloc(len * sizeof(wchar_t)); + GIT_ERROR_CHECK_ALLOC(iter->env); + + len = GetEnvironmentVariableW(L"PATH", iter->env, len); + + if (len == 0) { + git_error_set(GIT_ERROR_OS, "could not load PATH"); + return -1; + } + + iter->current_dir = iter->env; + return 0; +} + +static int win32_path_iter_next( + const wchar_t **out, + size_t *out_len, + struct win32_path_iter *iter) +{ + const wchar_t *start; + wchar_t term; + size_t len = 0; + + if (!iter->current_dir || !*iter->current_dir) + return GIT_ITEROVER; + + term = (*iter->current_dir == L'"') ? *iter->current_dir++ : L';'; + start = iter->current_dir; + + while (*iter->current_dir && *iter->current_dir != term) { + iter->current_dir++; + len++; + } + + *out = start; + *out_len = len; + + if (term == L'"' && *iter->current_dir) + iter->current_dir++; + + while (*iter->current_dir == L';') + iter->current_dir++; + + return 0; +} + +static void win32_path_iter_dispose(struct win32_path_iter *iter) +{ + if (!iter) + return; + + git__free(iter->env); + iter->env = NULL; + iter->current_dir = NULL; +} + +struct executable_suffix { + const wchar_t *suffix; + size_t len; +}; + +static struct executable_suffix suffixes[] = { { NULL, 0 }, { L".exe", 4 }, { L".cmd", 4 } }; + +static bool has_executable_suffix(wchar_t *exe, size_t exe_len) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(suffixes); i++) { + struct executable_suffix *suffix = &suffixes[i]; + + if (!suffix->len) + continue; + + if (exe_len < suffix->len) + continue; + + if (memcmp(&exe[exe_len - suffix->len], suffix->suffix, suffix->len) == 0) + return true; + } + + return false; +} + +static int is_executable(git_win32_path path, size_t path_len, bool suffixed) +{ + size_t i; + + /* + * if the given name has an executable suffix, then try looking for it + * directly. in all cases, append executable extensions + * (".exe", ".cmd"...) + */ + for (i = suffixed ? 0 : 1; i < ARRAY_SIZE(suffixes); i++) { + struct executable_suffix *suffix = &suffixes[i]; + + if (suffix->len) { + if (path_len + suffix->len > MAX_PATH) + continue; + + wcscat(path, suffix->suffix); + } + + if (_waccess(path, 0) == 0) + return true; + + path[path_len] = L'\0'; + } + + return false; +} + +int git_win32_path_find_executable(git_win32_path fullpath, wchar_t *exe) +{ + struct win32_path_iter path_iter; + const wchar_t *dir; + size_t dir_len, exe_len, fullpath_len; + bool suffixed = false, found = false; + + if ((exe_len = wcslen(exe)) > MAX_PATH) + goto done; + + /* see if the given executable has an executable suffix; if so we will + * look for the explicit name directly, as well as with added suffixes. + */ + suffixed = has_executable_suffix(exe, exe_len); + + /* For fully-qualified paths we do not look in PATH */ + if (wcschr(exe, L'\\') != NULL || wcschr(exe, L'/') != NULL) { + if ((found = is_executable(exe, exe_len, suffixed))) + wcscpy(fullpath, exe); + + goto done; + } + + if (win32_path_iter_init(&path_iter) < 0) + return -1; + + while (win32_path_iter_next(&dir, &dir_len, &path_iter) != GIT_ITEROVER && !found) { + if (git_win32_path_join(fullpath, &fullpath_len, dir, dir_len, exe, exe_len) < 0) + continue; + + if (is_executable(fullpath, fullpath_len, suffixed)) { + found = true; + break; + } + } + + win32_path_iter_dispose(&path_iter); + +done: + if (found) + return 0; + + fullpath[0] = L'\0'; + return GIT_ENOTFOUND; +} + +static int win32_path_cwd(wchar_t *out, size_t len) +{ + int cwd_len; + + if (len > INT_MAX) { + errno = ENAMETOOLONG; + return -1; + } + + if ((cwd_len = path__cwd(out, (int)len)) < 0) + return -1; + + /* UNC paths */ + if (wcsncmp(L"\\\\", out, 2) == 0) { + /* Our buffer must be at least 5 characters larger than the + * current working directory: we swallow one of the leading + * '\'s, but we we add a 'UNC' specifier to the path, plus + * a trailing directory separator, plus a NUL. + */ + if (cwd_len > GIT_WIN_PATH_MAX - 4) { + errno = ENAMETOOLONG; + return -1; + } + + memmove(out+2, out, sizeof(wchar_t) * cwd_len); + out[0] = L'U'; + out[1] = L'N'; + out[2] = L'C'; + + cwd_len += 2; + } + + /* Our buffer must be at least 2 characters larger than the current + * working directory. (One character for the directory separator, + * one for the null. + */ + else if (cwd_len > GIT_WIN_PATH_MAX - 2) { + errno = ENAMETOOLONG; + return -1; + } + + return cwd_len; +} + +int git_win32_path_from_utf8(git_win32_path out, const char *src) +{ + wchar_t *dest = out; + + /* All win32 paths are in NT-prefixed format, beginning with "\\?\". */ + memcpy(dest, PATH__NT_NAMESPACE, sizeof(wchar_t) * PATH__NT_NAMESPACE_LEN); + dest += PATH__NT_NAMESPACE_LEN; + + /* See if this is an absolute path (beginning with a drive letter) */ + if (git_fs_path_is_absolute(src)) { + if (git_utf8_to_16(dest, GIT_WIN_PATH_MAX, src) < 0) + goto on_error; + } + /* File-prefixed NT-style paths beginning with \\?\ */ + else if (path__is_nt_namespace(src)) { + /* Skip the NT prefix, the destination already contains it */ + if (git_utf8_to_16(dest, GIT_WIN_PATH_MAX, src + PATH__NT_NAMESPACE_LEN) < 0) + goto on_error; + } + /* UNC paths */ + else if (path__is_unc(src)) { + memcpy(dest, L"UNC\\", sizeof(wchar_t) * 4); + dest += 4; + + /* Skip the leading "\\" */ + if (git_utf8_to_16(dest, GIT_WIN_PATH_MAX - 2, src + 2) < 0) + goto on_error; + } + /* Absolute paths omitting the drive letter */ + else if (path__startswith_slash(src)) { + if (path__cwd(dest, GIT_WIN_PATH_MAX) < 0) + goto on_error; + + if (!git_fs_path_is_absolute(dest)) { + errno = ENOENT; + goto on_error; + } + + /* Skip the drive letter specification ("C:") */ + if (git_utf8_to_16(dest + 2, GIT_WIN_PATH_MAX - 2, src) < 0) + goto on_error; + } + /* Relative paths */ + else { + int cwd_len; + + if ((cwd_len = win32_path_cwd(dest, GIT_WIN_PATH_MAX)) < 0) + goto on_error; + + dest[cwd_len++] = L'\\'; + + if (git_utf8_to_16(dest + cwd_len, GIT_WIN_PATH_MAX - cwd_len, src) < 0) + goto on_error; + } + + return git_win32_path_canonicalize(out); + +on_error: + /* set windows error code so we can use its error message */ + if (errno == ENAMETOOLONG) + SetLastError(ERROR_FILENAME_EXCED_RANGE); + + return -1; +} + +int git_win32_path_relative_from_utf8(git_win32_path out, const char *src) +{ + wchar_t *dest = out, *p; + int len; + + /* Handle absolute paths */ + if (git_fs_path_is_absolute(src) || + path__is_nt_namespace(src) || + path__is_unc(src) || + path__startswith_slash(src)) { + return git_win32_path_from_utf8(out, src); + } + + if ((len = git_utf8_to_16(dest, GIT_WIN_PATH_MAX, src)) < 0) + return -1; + + for (p = dest; p < (dest + len); p++) { + if (*p == L'/') + *p = L'\\'; + } + + return len; +} + +int git_win32_path_to_utf8(git_win32_utf8_path dest, const wchar_t *src) +{ + char *out = dest; + int len; + + /* Strip NT namespacing "\\?\" */ + if (path__is_nt_namespace(src)) { + src += 4; + + /* "\\?\UNC\server\share" -> "\\server\share" */ + if (wcsncmp(src, L"UNC\\", 4) == 0) { + src += 4; + + memcpy(dest, "\\\\", 2); + out = dest + 2; + } + } + + if ((len = git_utf8_from_16(out, GIT_WIN_PATH_UTF8, src)) < 0) + return len; + + git_fs_path_mkposix(dest); + + return len; +} + +char *git_win32_path_8dot3_name(const char *path) +{ + git_win32_path longpath, shortpath; + wchar_t *start; + char *shortname; + int len, namelen = 1; + + if (git_win32_path_from_utf8(longpath, path) < 0) + return NULL; + + len = GetShortPathNameW(longpath, shortpath, GIT_WIN_PATH_UTF16); + + while (len && shortpath[len-1] == L'\\') + shortpath[--len] = L'\0'; + + if (len == 0 || len >= GIT_WIN_PATH_UTF16) + return NULL; + + for (start = shortpath + (len - 1); + start > shortpath && *(start-1) != '/' && *(start-1) != '\\'; + start--) + namelen++; + + /* We may not have actually been given a short name. But if we have, + * it will be in the ASCII byte range, so we don't need to worry about + * multi-byte sequences and can allocate naively. + */ + if (namelen > 12 || (shortname = git__malloc(namelen + 1)) == NULL) + return NULL; + + if ((len = git_utf8_from_16(shortname, namelen + 1, start)) < 0) + return NULL; + + return shortname; +} + +static bool path_is_volume(wchar_t *target, size_t target_len) +{ + return (target_len && wcsncmp(target, L"\\??\\Volume{", 11) == 0); +} + +/* On success, returns the length, in characters, of the path stored in dest. + * On failure, returns a negative value. */ +int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path) +{ + BYTE buf[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; + GIT_REPARSE_DATA_BUFFER *reparse_buf = (GIT_REPARSE_DATA_BUFFER *)buf; + HANDLE handle = NULL; + DWORD ioctl_ret; + wchar_t *target; + size_t target_len; + + int error = -1; + + handle = CreateFileW(path, GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, + FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL); + + if (handle == INVALID_HANDLE_VALUE) { + errno = ENOENT; + return -1; + } + + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, + reparse_buf, sizeof(buf), &ioctl_ret, NULL)) { + errno = EINVAL; + goto on_error; + } + + switch (reparse_buf->ReparseTag) { + case IO_REPARSE_TAG_SYMLINK: + target = reparse_buf->ReparseBuffer.SymbolicLink.PathBuffer + + (reparse_buf->ReparseBuffer.SymbolicLink.SubstituteNameOffset / sizeof(WCHAR)); + target_len = reparse_buf->ReparseBuffer.SymbolicLink.SubstituteNameLength / sizeof(WCHAR); + break; + case IO_REPARSE_TAG_MOUNT_POINT: + target = reparse_buf->ReparseBuffer.MountPoint.PathBuffer + + (reparse_buf->ReparseBuffer.MountPoint.SubstituteNameOffset / sizeof(WCHAR)); + target_len = reparse_buf->ReparseBuffer.MountPoint.SubstituteNameLength / sizeof(WCHAR); + break; + default: + errno = EINVAL; + goto on_error; + } + + if (path_is_volume(target, target_len)) { + /* This path is a reparse point that represents another volume mounted + * at this location, it is not a symbolic link our input was canonical. + */ + errno = EINVAL; + error = -1; + } else if (target_len) { + /* The path may need to have a namespace prefix removed. */ + target_len = git_win32_path_remove_namespace(target, target_len); + + /* Need one additional character in the target buffer + * for the terminating NULL. */ + if (GIT_WIN_PATH_UTF16 > target_len) { + wcscpy(dest, target); + error = (int)target_len; + } + } + +on_error: + CloseHandle(handle); + return error; +} + +/** + * Removes any trailing backslashes from a path, except in the case of a drive + * letter path (C:\, D:\, etc.). This function cannot fail. + * + * @param path The path which should be trimmed. + * @return The length of the modified string (<= the input length) + */ +size_t git_win32_path_trim_end(wchar_t *str, size_t len) +{ + while (1) { + if (!len || str[len - 1] != L'\\') + break; + + /* + * Don't trim backslashes from drive letter paths, which + * are 3 characters long and of the form C:\, D:\, etc. + */ + if (len == 3 && git_win32__isalpha(str[0]) && str[1] == ':') + break; + + len--; + } + + str[len] = L'\0'; + + return len; +} + +/** + * Removes any of the following namespace prefixes from a path, + * if found: "\??\", "\\?\", "\\?\UNC\". This function cannot fail. + * + * @param path The path which should be converted. + * @return The length of the modified string (<= the input length) + */ +size_t git_win32_path_remove_namespace(wchar_t *str, size_t len) +{ + static const wchar_t dosdevices_namespace[] = L"\\\?\?\\"; + static const wchar_t nt_namespace[] = L"\\\\?\\"; + static const wchar_t unc_namespace_remainder[] = L"UNC\\"; + static const wchar_t unc_prefix[] = L"\\\\"; + + const wchar_t *prefix = NULL, *remainder = NULL; + size_t prefix_len = 0, remainder_len = 0; + + /* "\??\" -- DOS Devices prefix */ + if (len >= CONST_STRLEN(dosdevices_namespace) && + !wcsncmp(str, dosdevices_namespace, CONST_STRLEN(dosdevices_namespace))) { + remainder = str + CONST_STRLEN(dosdevices_namespace); + remainder_len = len - CONST_STRLEN(dosdevices_namespace); + } + /* "\\?\" -- NT namespace prefix */ + else if (len >= CONST_STRLEN(nt_namespace) && + !wcsncmp(str, nt_namespace, CONST_STRLEN(nt_namespace))) { + remainder = str + CONST_STRLEN(nt_namespace); + remainder_len = len - CONST_STRLEN(nt_namespace); + } + + /* "\??\UNC\", "\\?\UNC\" -- UNC prefix */ + if (remainder_len >= CONST_STRLEN(unc_namespace_remainder) && + !wcsncmp(remainder, unc_namespace_remainder, CONST_STRLEN(unc_namespace_remainder))) { + + /* + * The proper Win32 path for a UNC share has "\\" at beginning of it + * and looks like "\\server\share\". So remove the + * UNC namespace and add a prefix of "\\" in its place. + */ + remainder += CONST_STRLEN(unc_namespace_remainder); + remainder_len -= CONST_STRLEN(unc_namespace_remainder); + + prefix = unc_prefix; + prefix_len = CONST_STRLEN(unc_prefix); + } + + /* + * Sanity check that the new string isn't longer than the old one. + * (This could only happen due to programmer error introducing a + * prefix longer than the namespace it replaces.) + */ + if (remainder && len >= remainder_len + prefix_len) { + if (prefix) + memmove(str, prefix, prefix_len * sizeof(wchar_t)); + + memmove(str + prefix_len, remainder, remainder_len * sizeof(wchar_t)); + + len = remainder_len + prefix_len; + str[len] = L'\0'; + } + + return git_win32_path_trim_end(str, len); +} diff --git a/src/util/win32/path_w32.h b/src/util/win32/path_w32.h new file mode 100644 index 00000000000..b241d5c8a6f --- /dev/null +++ b/src/util/win32/path_w32.h @@ -0,0 +1,91 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_win32_path_w32_h__ +#define INCLUDE_win32_path_w32_h__ + +#include "git2_util.h" + +/** + * Create a Win32 path (in UCS-2 format) from a UTF-8 string. If the given + * path is relative, then it will be turned into an absolute path by having + * the current working directory prepended. + * + * @param dest The buffer to receive the wide string. + * @param src The UTF-8 string to convert. + * @return The length of the wide string, in characters (not counting the NULL terminator), or < 0 for failure + */ +extern int git_win32_path_from_utf8(git_win32_path dest, const char *src); + +/** + * Create a Win32 path (in UCS-2 format) from a UTF-8 string. If the given + * path is relative, then it will not be turned into an absolute path. + * + * @param dest The buffer to receive the wide string. + * @param src The UTF-8 string to convert. + * @return The length of the wide string, in characters (not counting the NULL terminator), or < 0 for failure + */ +extern int git_win32_path_relative_from_utf8(git_win32_path dest, const char *src); + +/** + * Canonicalize a Win32 UCS-2 path so that it is suitable for delivery to the + * Win32 APIs: remove multiple directory separators, squashing to a single one, + * strip trailing directory separators, ensure directory separators are all + * canonical (always backslashes, never forward slashes) and process any + * directory entries of '.' or '..'. + * + * Note that this is intended to be used on absolute Windows paths, those + * that start with `C:\`, `\\server\share`, `\\?\`, etc. + * + * This processes the buffer in place. + * + * @param path The buffer to process + * @return The new length of the buffer, in wchar_t's (not counting the NULL terminator) + */ +extern int git_win32_path_canonicalize(git_win32_path path); + +/** + * Create an internal format (posix-style) UTF-8 path from a Win32 UCS-2 path. + * + * @param dest The buffer to receive the UTF-8 string. + * @param src The wide string to convert. + * @return The length of the UTF-8 string, in bytes (not counting the NULL terminator), or < 0 for failure + */ +extern int git_win32_path_to_utf8(git_win32_utf8_path dest, const wchar_t *src); + +/** + * Get the short name for the terminal path component in the given path. + * For example, given "C:\Foo\Bar\Asdf.txt", this will return the short name + * for the file "Asdf.txt". + * + * @param path The given path in UTF-8 + * @return The name of the shortname for the given path + */ +extern char *git_win32_path_8dot3_name(const char *path); + +extern int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path); + +/** + * Removes any trailing backslashes from a path, except in the case of a drive + * letter path (C:\, D:\, etc.). This function cannot fail. + * + * @param path The path which should be trimmed. + * @return The length of the modified string (<= the input length) + */ +size_t git_win32_path_trim_end(wchar_t *str, size_t len); + +/** + * Removes any of the following namespace prefixes from a path, + * if found: "\??\", "\\?\", "\\?\UNC\". This function cannot fail. + * + * @param path The path which should be converted. + * @return The length of the modified string (<= the input length) + */ +size_t git_win32_path_remove_namespace(wchar_t *str, size_t len); + +int git_win32_path_find_executable(git_win32_path fullpath, wchar_t* exe); + +#endif diff --git a/src/util/win32/posix.h b/src/util/win32/posix.h new file mode 100644 index 00000000000..03fa2ac52b6 --- /dev/null +++ b/src/util/win32/posix.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_win32_posix_h__ +#define INCLUDE_win32_posix_h__ + +#include "git2_util.h" +#include "../posix.h" +#include "win32-compat.h" +#include "path_w32.h" +#include "utf-conv.h" +#include "dir.h" + +extern unsigned long git_win32__createfile_sharemode; +extern int git_win32__retries; + +typedef SOCKET GIT_SOCKET; + +#define p_lseek(f,n,w) _lseeki64(f, n, w) + +extern int p_fstat(int fd, struct stat *buf); +extern int p_lstat(const char *file_name, struct stat *buf); +extern int p_stat(const char *path, struct stat *buf); + +extern int p_utimes(const char *filename, const struct p_timeval times[2]); +extern int p_futimes(int fd, const struct p_timeval times[2]); + +extern int p_readlink(const char *path, char *buf, size_t bufsiz); +extern int p_symlink(const char *old, const char *new); +extern int p_link(const char *old, const char *new); +extern int p_unlink(const char *path); +extern int p_mkdir(const char *path, mode_t mode); +extern int p_fsync(int fd); +extern char *p_realpath(const char *orig_path, char *buffer); + +extern int p_recv(GIT_SOCKET socket, void *buffer, size_t length, int flags); +extern int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags); +extern int p_inet_pton(int af, const char *src, void* dst); + +extern int p_vsnprintf(char *buffer, size_t count, const char *format, va_list argptr); +extern int p_snprintf(char *buffer, size_t count, const char *format, ...) GIT_FORMAT_PRINTF(3, 4); +extern int p_chdir(const char *path); +extern int p_chmod(const char *path, mode_t mode); +extern int p_rmdir(const char *path); +extern int p_access(const char *path, mode_t mode); +extern int p_ftruncate(int fd, off64_t size); + +/* p_lstat is almost but not quite POSIX correct. Specifically, the use of + * ENOTDIR is wrong, in that it does not mean precisely that a non-directory + * entry was encountered. Making it correct is potentially expensive, + * however, so this is a separate version of p_lstat to use when correct + * POSIX ENOTDIR semantics is required. + */ +extern int p_lstat_posixly(const char *filename, struct stat *buf); + +extern struct tm * p_localtime_r(const time_t *timer, struct tm *result); +extern struct tm * p_gmtime_r(const time_t *timer, struct tm *result); + +#endif diff --git a/src/util/win32/posix_w32.c b/src/util/win32/posix_w32.c new file mode 100644 index 00000000000..7ace3b1e1f8 --- /dev/null +++ b/src/util/win32/posix_w32.c @@ -0,0 +1,1070 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "git2_util.h" + +#include "../posix.h" +#include "../futils.h" +#include "fs_path.h" +#include "path_w32.h" +#include "utf-conv.h" +#include "reparse.h" +#include +#include +#include +#include + +#ifndef FILE_NAME_NORMALIZED +# define FILE_NAME_NORMALIZED 0 +#endif + +#ifndef IO_REPARSE_TAG_SYMLINK +#define IO_REPARSE_TAG_SYMLINK (0xA000000CL) +#endif + +#ifndef SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE +# define SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE 0x02 +#endif + +#ifndef SYMBOLIC_LINK_FLAG_DIRECTORY +# define SYMBOLIC_LINK_FLAG_DIRECTORY 0x01 +#endif + +/* Allowable mode bits on Win32. Using mode bits that are not supported on + * Win32 (eg S_IRWXU) is generally ignored, but Wine warns loudly about it + * so we simply remove them. + */ +#define WIN32_MODE_MASK (_S_IREAD | _S_IWRITE) + +unsigned long git_win32__createfile_sharemode = + FILE_SHARE_READ | FILE_SHARE_WRITE; +int git_win32__retries = 10; + +GIT_INLINE(void) set_errno(void) +{ + switch (GetLastError()) { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + case ERROR_INVALID_DRIVE: + case ERROR_NO_MORE_FILES: + case ERROR_BAD_NETPATH: + case ERROR_BAD_NET_NAME: + case ERROR_BAD_PATHNAME: + case ERROR_FILENAME_EXCED_RANGE: + errno = ENOENT; + break; + case ERROR_BAD_ENVIRONMENT: + errno = E2BIG; + break; + case ERROR_BAD_FORMAT: + case ERROR_INVALID_STARTING_CODESEG: + case ERROR_INVALID_STACKSEG: + case ERROR_INVALID_MODULETYPE: + case ERROR_INVALID_EXE_SIGNATURE: + case ERROR_EXE_MARKED_INVALID: + case ERROR_BAD_EXE_FORMAT: + case ERROR_ITERATED_DATA_EXCEEDS_64k: + case ERROR_INVALID_MINALLOCSIZE: + case ERROR_DYNLINK_FROM_INVALID_RING: + case ERROR_IOPL_NOT_ENABLED: + case ERROR_INVALID_SEGDPL: + case ERROR_AUTODATASEG_EXCEEDS_64k: + case ERROR_RING2SEG_MUST_BE_MOVABLE: + case ERROR_RELOC_CHAIN_XEEDS_SEGLIM: + case ERROR_INFLOOP_IN_RELOC_CHAIN: + errno = ENOEXEC; + break; + case ERROR_INVALID_HANDLE: + case ERROR_INVALID_TARGET_HANDLE: + case ERROR_DIRECT_ACCESS_HANDLE: + errno = EBADF; + break; + case ERROR_WAIT_NO_CHILDREN: + case ERROR_CHILD_NOT_COMPLETE: + errno = ECHILD; + break; + case ERROR_NO_PROC_SLOTS: + case ERROR_MAX_THRDS_REACHED: + case ERROR_NESTING_NOT_ALLOWED: + errno = EAGAIN; + break; + case ERROR_ARENA_TRASHED: + case ERROR_NOT_ENOUGH_MEMORY: + case ERROR_INVALID_BLOCK: + case ERROR_NOT_ENOUGH_QUOTA: + errno = ENOMEM; + break; + case ERROR_ACCESS_DENIED: + case ERROR_CURRENT_DIRECTORY: + case ERROR_WRITE_PROTECT: + case ERROR_BAD_UNIT: + case ERROR_NOT_READY: + case ERROR_BAD_COMMAND: + case ERROR_CRC: + case ERROR_BAD_LENGTH: + case ERROR_SEEK: + case ERROR_NOT_DOS_DISK: + case ERROR_SECTOR_NOT_FOUND: + case ERROR_OUT_OF_PAPER: + case ERROR_WRITE_FAULT: + case ERROR_READ_FAULT: + case ERROR_GEN_FAILURE: + case ERROR_SHARING_VIOLATION: + case ERROR_LOCK_VIOLATION: + case ERROR_WRONG_DISK: + case ERROR_SHARING_BUFFER_EXCEEDED: + case ERROR_NETWORK_ACCESS_DENIED: + case ERROR_CANNOT_MAKE: + case ERROR_FAIL_I24: + case ERROR_DRIVE_LOCKED: + case ERROR_SEEK_ON_DEVICE: + case ERROR_NOT_LOCKED: + case ERROR_LOCK_FAILED: + errno = EACCES; + break; + case ERROR_FILE_EXISTS: + case ERROR_ALREADY_EXISTS: + errno = EEXIST; + break; + case ERROR_NOT_SAME_DEVICE: + errno = EXDEV; + break; + case ERROR_INVALID_FUNCTION: + case ERROR_INVALID_ACCESS: + case ERROR_INVALID_DATA: + case ERROR_INVALID_PARAMETER: + case ERROR_NEGATIVE_SEEK: + errno = EINVAL; + break; + case ERROR_TOO_MANY_OPEN_FILES: + errno = EMFILE; + break; + case ERROR_DISK_FULL: + errno = ENOSPC; + break; + case ERROR_BROKEN_PIPE: + errno = EPIPE; + break; + case ERROR_DIR_NOT_EMPTY: + errno = ENOTEMPTY; + break; + default: + errno = EINVAL; + } +} + +GIT_INLINE(bool) last_error_retryable(void) +{ + int os_error = GetLastError(); + + return (os_error == ERROR_SHARING_VIOLATION || + os_error == ERROR_ACCESS_DENIED); +} + +#define do_with_retries(fn, remediation) \ + do { \ + int __retry, __ret; \ + for (__retry = git_win32__retries; __retry; __retry--) { \ + if ((__ret = (fn)) != GIT_RETRY) \ + return __ret; \ + if (__retry > 1 && (__ret = (remediation)) != 0) { \ + if (__ret == GIT_RETRY) \ + continue; \ + return __ret; \ + } \ + Sleep(5); \ + } \ + return -1; \ + } while (0) \ + +static int ensure_writable(wchar_t *path) +{ + DWORD attrs; + + if ((attrs = GetFileAttributesW(path)) == INVALID_FILE_ATTRIBUTES) + goto on_error; + + if ((attrs & FILE_ATTRIBUTE_READONLY) == 0) + return 0; + + if (!SetFileAttributesW(path, (attrs & ~FILE_ATTRIBUTE_READONLY))) + goto on_error; + + return GIT_RETRY; + +on_error: + set_errno(); + return -1; +} + +/** + * Truncate or extend file. + * + * We now take a "git_off_t" rather than "long" because + * files may be longer than 2Gb. + */ +int p_ftruncate(int fd, off64_t size) +{ + if (size < 0) { + errno = EINVAL; + return -1; + } + +#if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API) + return ((_chsize_s(fd, size) == 0) ? 0 : -1); +#else + /* TODO MINGW32 Find a replacement for _chsize() that handles big files. */ + if (size > INT32_MAX) { + errno = EFBIG; + return -1; + } + return _chsize(fd, (long)size); +#endif +} + +int p_mkdir(const char *path, mode_t mode) +{ + git_win32_path buf; + + GIT_UNUSED(mode); + + if (git_win32_path_from_utf8(buf, path) < 0) + return -1; + + return _wmkdir(buf); +} + +int p_link(const char *old, const char *new) +{ + GIT_UNUSED(old); + GIT_UNUSED(new); + errno = ENOSYS; + return -1; +} + +GIT_INLINE(int) unlink_once(const wchar_t *path) +{ + DWORD error; + + if (DeleteFileW(path)) + return 0; + + if ((error = GetLastError()) == ERROR_ACCESS_DENIED) { + WIN32_FILE_ATTRIBUTE_DATA fdata; + if (!GetFileAttributesExW(path, GetFileExInfoStandard, &fdata) || + !(fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) || + !(fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) + goto out; + + if (RemoveDirectoryW(path)) + return 0; + } + +out: + SetLastError(error); + + if (last_error_retryable()) + return GIT_RETRY; + + set_errno(); + return -1; +} + +int p_unlink(const char *path) +{ + git_win32_path wpath; + + if (git_win32_path_from_utf8(wpath, path) < 0) + return -1; + + do_with_retries(unlink_once(wpath), ensure_writable(wpath)); +} + +int p_fsync(int fd) +{ + HANDLE fh = (HANDLE)_get_osfhandle(fd); + + p_fsync__cnt++; + + if (fh == INVALID_HANDLE_VALUE) { + errno = EBADF; + return -1; + } + + if (!FlushFileBuffers(fh)) { + DWORD code = GetLastError(); + + if (code == ERROR_INVALID_HANDLE) + errno = EINVAL; + else + errno = EIO; + + return -1; + } + + return 0; +} + +#define WIN32_IS_WSEP(CH) ((CH) == L'/' || (CH) == L'\\') + +static int lstat_w( + wchar_t *path, + struct stat *buf, + bool posix_enotdir) +{ + WIN32_FILE_ATTRIBUTE_DATA fdata; + + if (GetFileAttributesExW(path, GetFileExInfoStandard, &fdata)) { + if (!buf) + return 0; + + return git_win32__file_attribute_to_stat(buf, &fdata, path); + } + + switch (GetLastError()) { + case ERROR_ACCESS_DENIED: + errno = EACCES; + break; + default: + errno = ENOENT; + break; + } + + /* To match POSIX behavior, set ENOTDIR when any of the folders in the + * file path is a regular file, otherwise set ENOENT. + */ + if (errno == ENOENT && posix_enotdir) { + size_t path_len = wcslen(path); + + /* scan up path until we find an existing item */ + while (1) { + DWORD attrs; + + /* remove last directory component */ + for (path_len--; path_len > 0 && !WIN32_IS_WSEP(path[path_len]); path_len--); + + if (path_len <= 0) + break; + + path[path_len] = L'\0'; + attrs = GetFileAttributesW(path); + + if (attrs != INVALID_FILE_ATTRIBUTES) { + if (!(attrs & FILE_ATTRIBUTE_DIRECTORY)) + errno = ENOTDIR; + break; + } + } + } + + return -1; +} + +static int do_lstat(const char *path, struct stat *buf, bool posixly_correct) +{ + git_win32_path path_w; + int len; + + if ((len = git_win32_path_from_utf8(path_w, path)) < 0) + return -1; + + git_win32_path_trim_end(path_w, len); + + return lstat_w(path_w, buf, posixly_correct); +} + +int p_lstat(const char *filename, struct stat *buf) +{ + return do_lstat(filename, buf, false); +} + +int p_lstat_posixly(const char *filename, struct stat *buf) +{ + return do_lstat(filename, buf, true); +} + +int p_readlink(const char *path, char *buf, size_t bufsiz) +{ + git_win32_path path_w, target_w; + git_win32_utf8_path target; + int len; + + /* readlink(2) does not NULL-terminate the string written + * to the target buffer. Furthermore, the target buffer need + * not be large enough to hold the entire result. A truncated + * result should be written in this case. Since this truncation + * could occur in the middle of the encoding of a code point, + * we need to buffer the result on the stack. */ + + if (git_win32_path_from_utf8(path_w, path) < 0 || + git_win32_path_readlink_w(target_w, path_w) < 0 || + (len = git_win32_path_to_utf8(target, target_w)) < 0) + return -1; + + bufsiz = min((size_t)len, bufsiz); + memcpy(buf, target, bufsiz); + + return (int)bufsiz; +} + +static bool target_is_dir(const char *target, const char *path) +{ + git_str resolved = GIT_STR_INIT; + git_win32_path resolved_w; + bool isdir = true; + + if (git_fs_path_is_absolute(target)) + git_win32_path_from_utf8(resolved_w, target); + else if (git_fs_path_dirname_r(&resolved, path) < 0 || + git_fs_path_apply_relative(&resolved, target) < 0 || + git_win32_path_from_utf8(resolved_w, resolved.ptr) < 0) + goto out; + + isdir = GetFileAttributesW(resolved_w) & FILE_ATTRIBUTE_DIRECTORY; + +out: + git_str_dispose(&resolved); + return isdir; +} + +int p_symlink(const char *target, const char *path) +{ + git_win32_path target_w, path_w; + DWORD dwFlags; + + /* + * Convert both target and path to Windows-style paths. Note that we do + * not want to use `git_win32_path_from_utf8` for converting the target, + * as that function will automatically pre-pend the current working + * directory in case the path is not absolute. As Git will instead use + * relative symlinks, this is not something we want. + */ + if (git_win32_path_from_utf8(path_w, path) < 0 || + git_win32_path_relative_from_utf8(target_w, target) < 0) + return -1; + + dwFlags = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; + if (target_is_dir(target, path)) + dwFlags |= SYMBOLIC_LINK_FLAG_DIRECTORY; + + if (!CreateSymbolicLinkW(path_w, target_w, dwFlags)) + return -1; + + return 0; +} + +struct open_opts { + DWORD access; + DWORD sharing; + SECURITY_ATTRIBUTES security; + DWORD creation_disposition; + DWORD attributes; + int osf_flags; +}; + +GIT_INLINE(void) open_opts_from_posix(struct open_opts *opts, int flags, mode_t mode) +{ + memset(opts, 0, sizeof(struct open_opts)); + + switch (flags & (O_WRONLY | O_RDWR)) { + case O_WRONLY: + opts->access = GENERIC_WRITE; + break; + case O_RDWR: + opts->access = GENERIC_READ | GENERIC_WRITE; + break; + default: + opts->access = GENERIC_READ; + break; + } + + opts->sharing = (DWORD)git_win32__createfile_sharemode; + + switch (flags & (O_CREAT | O_TRUNC | O_EXCL)) { + case O_CREAT | O_EXCL: + case O_CREAT | O_TRUNC | O_EXCL: + opts->creation_disposition = CREATE_NEW; + break; + case O_CREAT | O_TRUNC: + opts->creation_disposition = CREATE_ALWAYS; + break; + case O_TRUNC: + opts->creation_disposition = TRUNCATE_EXISTING; + break; + case O_CREAT: + opts->creation_disposition = OPEN_ALWAYS; + break; + default: + opts->creation_disposition = OPEN_EXISTING; + break; + } + + opts->attributes = ((flags & O_CREAT) && !(mode & S_IWRITE)) ? + FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL; + opts->osf_flags = flags & (O_RDONLY | O_APPEND); + + opts->security.nLength = sizeof(SECURITY_ATTRIBUTES); + opts->security.lpSecurityDescriptor = NULL; + opts->security.bInheritHandle = 0; +} + +GIT_INLINE(int) open_once( + const wchar_t *path, + struct open_opts *opts) +{ + int fd; + + HANDLE handle = CreateFileW(path, opts->access, opts->sharing, + &opts->security, opts->creation_disposition, opts->attributes, 0); + + if (handle == INVALID_HANDLE_VALUE) { + if (last_error_retryable()) + return GIT_RETRY; + + set_errno(); + return -1; + } + + if ((fd = _open_osfhandle((intptr_t)handle, opts->osf_flags)) < 0) + CloseHandle(handle); + + return fd; +} + +int p_open(const char *path, int flags, ...) +{ + git_win32_path wpath; + mode_t mode = 0; + struct open_opts opts = {0}; + + #ifdef GIT_DEBUG_STRICT_OPEN + if (strstr(path, "//") != NULL) { + errno = EACCES; + return -1; + } + #endif + + if (git_win32_path_from_utf8(wpath, path) < 0) + return -1; + + if (flags & O_CREAT) { + va_list arg_list; + + va_start(arg_list, flags); + mode = (mode_t)va_arg(arg_list, int); + va_end(arg_list); + } + + open_opts_from_posix(&opts, flags, mode); + + do_with_retries( + open_once(wpath, &opts), + 0); +} + +int p_creat(const char *path, mode_t mode) +{ + return p_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode); +} + +int p_utimes(const char *path, const struct p_timeval times[2]) +{ + git_win32_path wpath; + int fd, error; + DWORD attrs_orig, attrs_new = 0; + struct open_opts opts = { 0 }; + + if (git_win32_path_from_utf8(wpath, path) < 0) + return -1; + + attrs_orig = GetFileAttributesW(wpath); + + if (attrs_orig & FILE_ATTRIBUTE_READONLY) { + attrs_new = attrs_orig & ~FILE_ATTRIBUTE_READONLY; + + if (!SetFileAttributesW(wpath, attrs_new)) { + git_error_set(GIT_ERROR_OS, "failed to set attributes"); + return -1; + } + } + + open_opts_from_posix(&opts, O_RDWR, 0); + + if ((fd = open_once(wpath, &opts)) < 0) { + error = -1; + goto done; + } + + error = p_futimes(fd, times); + close(fd); + +done: + if (attrs_orig != attrs_new) { + DWORD os_error = GetLastError(); + SetFileAttributesW(wpath, attrs_orig); + SetLastError(os_error); + } + + return error; +} + +int p_futimes(int fd, const struct p_timeval times[2]) +{ + HANDLE handle; + FILETIME atime = { 0 }, mtime = { 0 }; + + if (times == NULL) { + SYSTEMTIME st; + + GetSystemTime(&st); + SystemTimeToFileTime(&st, &atime); + SystemTimeToFileTime(&st, &mtime); + } + else { + git_win32__timeval_to_filetime(&atime, times[0]); + git_win32__timeval_to_filetime(&mtime, times[1]); + } + + if ((handle = (HANDLE)_get_osfhandle(fd)) == INVALID_HANDLE_VALUE) + return -1; + + if (SetFileTime(handle, NULL, &atime, &mtime) == 0) + return -1; + + return 0; +} + +int p_getcwd(char *buffer_out, size_t size) +{ + git_win32_path buf; + wchar_t *cwd = _wgetcwd(buf, GIT_WIN_PATH_UTF16); + + if (!cwd) + return -1; + + git_win32_path_remove_namespace(cwd, wcslen(cwd)); + + /* Convert the working directory back to UTF-8 */ + if (git_utf8_from_16(buffer_out, size, cwd) < 0) { + DWORD code = GetLastError(); + + if (code == ERROR_INSUFFICIENT_BUFFER) + errno = ERANGE; + else + errno = EINVAL; + + return -1; + } + + git_fs_path_mkposix(buffer_out); + return 0; +} + +static int getfinalpath_w( + git_win32_path dest, + const wchar_t *path) +{ + HANDLE hFile; + DWORD dwChars; + + /* Use FILE_FLAG_BACKUP_SEMANTICS so we can open a directory. Do not + * specify FILE_FLAG_OPEN_REPARSE_POINT; we want to open a handle to the + * target of the link. */ + hFile = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, + NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + + if (INVALID_HANDLE_VALUE == hFile) + return -1; + + /* Call GetFinalPathNameByHandle */ + dwChars = GetFinalPathNameByHandleW(hFile, dest, GIT_WIN_PATH_UTF16, FILE_NAME_NORMALIZED); + CloseHandle(hFile); + + if (!dwChars || dwChars >= GIT_WIN_PATH_UTF16) + return -1; + + /* The path may be delivered to us with a namespace prefix; remove */ + return (int)git_win32_path_remove_namespace(dest, dwChars); +} + +static int follow_and_lstat_link(git_win32_path path, struct stat *buf) +{ + git_win32_path target_w; + + if (getfinalpath_w(target_w, path) < 0) + return -1; + + return lstat_w(target_w, buf, false); +} + +int p_fstat(int fd, struct stat *buf) +{ + BY_HANDLE_FILE_INFORMATION fhInfo; + + HANDLE fh = (HANDLE)_get_osfhandle(fd); + + if (fh == INVALID_HANDLE_VALUE || + !GetFileInformationByHandle(fh, &fhInfo)) { + errno = EBADF; + return -1; + } + + git_win32__file_information_to_stat(buf, &fhInfo); + return 0; +} + +int p_stat(const char *path, struct stat *buf) +{ + git_win32_path path_w; + int len; + + if ((len = git_win32_path_from_utf8(path_w, path)) < 0 || + lstat_w(path_w, buf, false) < 0) + return -1; + + /* The item is a symbolic link or mount point. No need to iterate + * to follow multiple links; use GetFinalPathNameFromHandle. */ + if (S_ISLNK(buf->st_mode)) + return follow_and_lstat_link(path_w, buf); + + return 0; +} + +int p_chdir(const char *path) +{ + git_win32_path buf; + + if (git_win32_path_from_utf8(buf, path) < 0) + return -1; + + return _wchdir(buf); +} + +int p_chmod(const char *path, mode_t mode) +{ + git_win32_path buf; + + if (git_win32_path_from_utf8(buf, path) < 0) + return -1; + + return _wchmod(buf, mode); +} + +int p_rmdir(const char *path) +{ + git_win32_path buf; + int error; + + if (git_win32_path_from_utf8(buf, path) < 0) + return -1; + + error = _wrmdir(buf); + + if (error == -1) { + switch (GetLastError()) { + /* _wrmdir() is documented to return EACCES if "A program has an open + * handle to the directory." This sounds like what everybody else calls + * EBUSY. Let's convert appropriate error codes. + */ + case ERROR_ACCESS_DENIED: + case ERROR_SHARING_VIOLATION: + errno = EBUSY; + break; + + /* This error can be returned when trying to rmdir an extant file. */ + case ERROR_DIRECTORY: + errno = ENOTDIR; + break; + } + } + + return error; +} + +char *p_realpath(const char *orig_path, char *buffer) +{ + git_win32_path orig_path_w, buffer_w; + DWORD long_len; + + if (git_win32_path_from_utf8(orig_path_w, orig_path) < 0) + return NULL; + + /* + * POSIX realpath performs two functions: first, it turns relative + * paths into absolute paths. For this, we need GetFullPathName. + * + * Note that if the path provided is a relative path, then the current directory + * is used to resolve the path -- which is a concurrency issue because the current + * directory is a process-wide variable. + */ + if (!GetFullPathNameW(orig_path_w, GIT_WIN_PATH_UTF16, buffer_w, NULL)) { + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) + errno = ENAMETOOLONG; + else + errno = EINVAL; + + return NULL; + } + + /* + * Then, the path is canonicalized. eg, on macOS, + * "/TMP" -> "/private/tmp". For this, we need GetLongPathName. + */ + if ((long_len = GetLongPathNameW(buffer_w, buffer_w, GIT_WIN_PATH_UTF16)) == 0) { + DWORD error = GetLastError(); + + if (error == ERROR_FILE_NOT_FOUND || + error == ERROR_PATH_NOT_FOUND) + errno = ENOENT; + else if (error == ERROR_ACCESS_DENIED) + errno = EPERM; + else + errno = EINVAL; + + return NULL; + } + + if (long_len > GIT_WIN_PATH_UTF16) { + errno = ENAMETOOLONG; + return NULL; + } + + if (!buffer && !(buffer = git__malloc(GIT_WIN_PATH_UTF8))) { + errno = ENOMEM; + return NULL; + } + + /* Convert the path to UTF-8. If the caller provided a buffer, then it + * is assumed to be GIT_WIN_PATH_UTF8 characters in size. If it isn't, + * then we may overflow. */ + if (git_win32_path_to_utf8(buffer, buffer_w) < 0) + return NULL; + + git_fs_path_mkposix(buffer); + return buffer; +} + +int p_vsnprintf(char *buffer, size_t count, const char *format, va_list argptr) +{ +#if defined(_MSC_VER) + int len; + + if (count == 0) + return _vscprintf(format, argptr); + + #if _MSC_VER >= 1500 + len = _vsnprintf_s(buffer, count, _TRUNCATE, format, argptr); + #else + len = _vsnprintf(buffer, count, format, argptr); + #endif + + if (len < 0) + return _vscprintf(format, argptr); + + return len; +#else /* MinGW */ + return vsnprintf(buffer, count, format, argptr); +#endif +} + +int p_snprintf(char *buffer, size_t count, const char *format, ...) +{ + va_list va; + int r; + + va_start(va, format); + r = p_vsnprintf(buffer, count, format, va); + va_end(va); + + return r; +} + +int p_access(const char *path, mode_t mode) +{ + git_win32_path buf; + + if (git_win32_path_from_utf8(buf, path) < 0) + return -1; + + return _waccess(buf, mode & WIN32_MODE_MASK); +} + +GIT_INLINE(int) rename_once(const wchar_t *from, const wchar_t *to) +{ + if (MoveFileExW(from, to, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) + return 0; + + if (last_error_retryable()) + return GIT_RETRY; + + set_errno(); + return -1; +} + +int p_rename(const char *from, const char *to) +{ + git_win32_path wfrom, wto; + + if (git_win32_path_from_utf8(wfrom, from) < 0 || + git_win32_path_from_utf8(wto, to) < 0) + return -1; + + do_with_retries(rename_once(wfrom, wto), ensure_writable(wto)); +} + +int p_recv(GIT_SOCKET socket, void *buffer, size_t length, int flags) +{ + if ((size_t)((int)length) != length) + return -1; /* git_error_set will be done by caller */ + + return recv(socket, buffer, (int)length, flags); +} + +int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags) +{ + if ((size_t)((int)length) != length) + return -1; /* git_error_set will be done by caller */ + + return send(socket, buffer, (int)length, flags); +} + +/** + * Borrowed from http://old.nabble.com/Porting-localtime_r-and-gmtime_r-td15282276.html + * On Win32, `gmtime_r` doesn't exist but `gmtime` is threadsafe, so we can use that + */ +struct tm * +p_localtime_r (const time_t *timer, struct tm *result) +{ + struct tm *local_result; + local_result = localtime (timer); + + if (local_result == NULL || result == NULL) + return NULL; + + memcpy (result, local_result, sizeof (struct tm)); + return result; +} +struct tm * +p_gmtime_r (const time_t *timer, struct tm *result) +{ + struct tm *local_result; + local_result = gmtime (timer); + + if (local_result == NULL || result == NULL) + return NULL; + + memcpy (result, local_result, sizeof (struct tm)); + return result; +} + +int p_inet_pton(int af, const char *src, void *dst) +{ + struct sockaddr_storage sin; + void *addr; + int sin_len = sizeof(struct sockaddr_storage), addr_len; + int error = 0; + + if (af == AF_INET) { + addr = &((struct sockaddr_in *)&sin)->sin_addr; + addr_len = sizeof(struct in_addr); + } else if (af == AF_INET6) { + addr = &((struct sockaddr_in6 *)&sin)->sin6_addr; + addr_len = sizeof(struct in6_addr); + } else { + errno = EAFNOSUPPORT; + return -1; + } + + if ((error = WSAStringToAddressA((LPSTR)src, af, NULL, (LPSOCKADDR)&sin, &sin_len)) == 0) { + memcpy(dst, addr, addr_len); + return 1; + } + + switch(WSAGetLastError()) { + case WSAEINVAL: + return 0; + case WSAEFAULT: + errno = ENOSPC; + return -1; + case WSA_NOT_ENOUGH_MEMORY: + errno = ENOMEM; + return -1; + } + + errno = EINVAL; + return -1; +} + +ssize_t p_pread(int fd, void *data, size_t size, off64_t offset) +{ + HANDLE fh; + DWORD rsize = 0; + OVERLAPPED ov = {0}; + LARGE_INTEGER pos = {0}; + off64_t final_offset = 0; + + /* Fail if the final offset would have overflowed to match POSIX semantics. */ + if (!git__is_ssizet(size) || git__add_int64_overflow(&final_offset, offset, (int64_t)size)) { + errno = EINVAL; + return -1; + } + + /* + * Truncate large writes to the maximum allowable size: the caller + * needs to always call this in a loop anyways. + */ + if (size > INT32_MAX) { + size = INT32_MAX; + } + + pos.QuadPart = offset; + ov.Offset = pos.LowPart; + ov.OffsetHigh = pos.HighPart; + fh = (HANDLE)_get_osfhandle(fd); + + if (ReadFile(fh, data, (DWORD)size, &rsize, &ov)) { + return (ssize_t)rsize; + } + + set_errno(); + return -1; +} + +ssize_t p_pwrite(int fd, const void *data, size_t size, off64_t offset) +{ + HANDLE fh; + DWORD wsize = 0; + OVERLAPPED ov = {0}; + LARGE_INTEGER pos = {0}; + off64_t final_offset = 0; + + /* Fail if the final offset would have overflowed to match POSIX semantics. */ + if (!git__is_ssizet(size) || git__add_int64_overflow(&final_offset, offset, (int64_t)size)) { + errno = EINVAL; + return -1; + } + + /* + * Truncate large writes to the maximum allowable size: the caller + * needs to always call this in a loop anyways. + */ + if (size > INT32_MAX) { + size = INT32_MAX; + } + + pos.QuadPart = offset; + ov.Offset = pos.LowPart; + ov.OffsetHigh = pos.HighPart; + fh = (HANDLE)_get_osfhandle(fd); + + if (WriteFile(fh, data, (DWORD)size, &wsize, &ov)) { + return (ssize_t)wsize; + } + + set_errno(); + return -1; +} diff --git a/src/util/win32/precompiled.c b/src/util/win32/precompiled.c new file mode 100644 index 00000000000..5f656a45da8 --- /dev/null +++ b/src/util/win32/precompiled.c @@ -0,0 +1 @@ +#include "precompiled.h" diff --git a/src/util/win32/precompiled.h b/src/util/win32/precompiled.h new file mode 100644 index 00000000000..1163c3d63eb --- /dev/null +++ b/src/util/win32/precompiled.h @@ -0,0 +1,21 @@ +#include "git2_util.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#ifdef GIT_THREADS + #include "win32/thread.h" +#endif + +#include "git2.h" diff --git a/src/util/win32/process.c b/src/util/win32/process.c new file mode 100644 index 00000000000..ad4ae896c2b --- /dev/null +++ b/src/util/win32/process.c @@ -0,0 +1,522 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include +#include + +#include "git2_util.h" +#include "process.h" +#include "strlist.h" +#include "fs_path.h" + +#ifndef DWORD_MAX +# define DWORD_MAX INT32_MAX +#endif + +#define ENV_MAX 32767 + +struct git_process { + char *app_name; + wchar_t *app_path; + wchar_t *cmdline; + wchar_t *env; + + wchar_t *cwd; + + unsigned int capture_in : 1, + capture_out : 1, + capture_err : 1; + + PROCESS_INFORMATION process_info; + + HANDLE child_in; + HANDLE child_out; + HANDLE child_err; + + git_process_result_status status; +}; + +/* + * Windows processes have a single command-line that is split by the + * invoked application into arguments (instead of an array of + * command-line arguments). This command-line is split by space or + * tab delimiters, unless that whitespace is within a double quote. + * Literal double-quotes themselves can be escaped by a backslash, + * but only when not within double quotes. Literal backslashes can + * be escaped by a backslash. + * + * Effectively, this means that instead of thinking about quoting + * individual strings, think about double quotes as an escaping + * mechanism for whitespace. + * + * In other words (using ` as a string boundary): + * [ `foo`, `bar` ] => `foo bar` + * [ `foo bar` ] => `foo" "bar` + * [ `foo bar`, `foo bar` ] => `foo" "bar foo" "bar` + * [ `foo "bar" foo` ] => `foo" "\"bar\"" "foo` + */ +int git_process__cmdline( + git_str *out, + const char **in, + size_t in_len) +{ + bool quoted = false; + const char *c; + size_t i; + + for (i = 0; i < in_len; i++) { + /* Arguments are delimited by an unquoted space */ + if (i) + git_str_putc(out, ' '); + + for (c = in[i]; *c; c++) { + /* Start or stop quoting spaces within an argument */ + if ((*c == ' ' || *c == '\t') && !quoted) { + git_str_putc(out, '"'); + quoted = true; + } else if (*c != ' ' && *c != '\t' && quoted) { + git_str_putc(out, '"'); + quoted = false; + } + + /* Escape double-quotes and backslashes */ + if (*c == '"' || *c == '\\') + git_str_putc(out, '\\'); + + git_str_putc(out, *c); + } + } + + return git_str_oom(out) ? -1 : 0; +} + +GIT_INLINE(bool) is_delete_env(const char *env) +{ + char *c = strchr(env, '='); + + if (c == NULL) + return false; + + return *(c+1) == '\0'; +} + +static int merge_env(wchar_t **out, const char **in, size_t in_len, bool exclude_env) +{ + git_str merged = GIT_STR_INIT; + wchar_t *in16 = NULL, *env = NULL, *e; + char *e8 = NULL; + size_t e_len; + int ret = 0; + size_t i; + + *out = NULL; + + in16 = git__malloc(ENV_MAX * sizeof(wchar_t)); + GIT_ERROR_CHECK_ALLOC(in16); + + e8 = git__malloc(ENV_MAX); + GIT_ERROR_CHECK_ALLOC(e8); + + for (i = 0; in && i < in_len; i++) { + if (is_delete_env(in[i])) + continue; + + if ((ret = git_utf8_to_16(in16, ENV_MAX, in[i])) < 0) + goto done; + + git_str_put(&merged, (const char *)in16, ret * 2); + git_str_put(&merged, "\0\0", 2); + } + + if (!exclude_env) { + env = GetEnvironmentStringsW(); + + for (e = env; *e; e += (e_len + 1)) { + e_len = wcslen(e); + + if ((ret = git_utf8_from_16(e8, ENV_MAX, e)) < 0) + goto done; + + if (git_strlist_contains_key(in, in_len, e8, '=')) + continue; + + git_str_put(&merged, (const char *)e, e_len * 2); + git_str_put(&merged, "\0\0", 2); + } + } + + git_str_put(&merged, "\0\0", 2); + + *out = (wchar_t *)git_str_detach(&merged); + +done: + if (env) + FreeEnvironmentStringsW(env); + + git_str_dispose(&merged); + git__free(e8); + git__free(in16); + + return ret < 0 ? -1 : 0; +} + +static int process_new( + git_process **out, + const char *appname, + const char *cmdline, + const char **env, + size_t env_len, + git_process_options *opts) +{ + git_process *process; + int error = 0; + + *out = NULL; + + process = git__calloc(1, sizeof(git_process)); + GIT_ERROR_CHECK_ALLOC(process); + + if (appname && (process->app_name = git__strdup(appname)) == NULL) { + error = -1; + goto done; + } + + if (git_utf8_to_16_alloc(&process->cmdline, cmdline) < 0) { + error = -1; + goto done; + } + + if (opts && opts->cwd && + git_utf8_to_16_alloc(&process->cwd, opts->cwd) < 0) { + error = -1; + goto done; + } + + if (env && (error = merge_env(&process->env, env, env_len, opts && opts->exclude_env) < 0)) + goto done; + + if (opts) { + process->capture_in = opts->capture_in; + process->capture_out = opts->capture_out; + process->capture_err = opts->capture_err; + } + +done: + if (error) + git_process_free(process); + else + *out = process; + + return error; +} + +int git_process_new_from_cmdline( + git_process **out, + const char *cmdline, + const char **env, + size_t env_len, + git_process_options *opts) +{ + GIT_ASSERT_ARG(out && cmdline); + + return process_new(out, NULL, cmdline, env, env_len, opts); +} + +int git_process_new( + git_process **out, + const char **args, + size_t args_len, + const char **env, + size_t env_len, + git_process_options *opts) +{ + git_str cmd_path = GIT_STR_INIT, cmdline = GIT_STR_INIT; + int error; + + GIT_ASSERT_ARG(out && args && args_len > 0); + + if ((error = git_process__cmdline(&cmdline, args, args_len)) < 0) + goto done; + + error = process_new(out, args[0], cmdline.ptr, env, env_len, opts); + +done: + git_str_dispose(&cmd_path); + git_str_dispose(&cmdline); + return error; +} + +#define CLOSE_HANDLE(h) do { if ((h) != NULL) CloseHandle(h); } while(0) + +int git_process_start(git_process *process) +{ + STARTUPINFOW startup_info; + SECURITY_ATTRIBUTES security_attrs; + DWORD flags = CREATE_UNICODE_ENVIRONMENT; + HANDLE in[2] = { NULL, NULL }, + out[2] = { NULL, NULL }, + err[2] = { NULL, NULL }; + + if (process->app_name) { + git_str cmd_path = GIT_STR_INIT; + int error; + + if ((error = git_fs_path_find_executable(&cmd_path, process->app_name)) == 0) + error = git_utf8_to_16_alloc(&process->app_path, cmd_path.ptr); + + git_str_dispose(&cmd_path); + + if (error < 0) + goto on_error; + } + + memset(&security_attrs, 0, sizeof(SECURITY_ATTRIBUTES)); + security_attrs.bInheritHandle = TRUE; + + memset(&startup_info, 0, sizeof(STARTUPINFOW)); + startup_info.cb = sizeof(STARTUPINFOW); + startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); + startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); + startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); + + if (process->capture_in) { + if (!CreatePipe(&in[0], &in[1], &security_attrs, 0) || + !SetHandleInformation(in[1], HANDLE_FLAG_INHERIT, 0)) { + git_error_set(GIT_ERROR_OS, "could not create pipe"); + goto on_error; + } + + startup_info.hStdInput = in[0]; + startup_info.dwFlags |= STARTF_USESTDHANDLES; + } + + if (process->capture_out) { + if (!CreatePipe(&out[0], &out[1], &security_attrs, 0) || + !SetHandleInformation(out[0], HANDLE_FLAG_INHERIT, 0)) { + git_error_set(GIT_ERROR_OS, "could not create pipe"); + goto on_error; + } + + startup_info.hStdOutput = out[1]; + startup_info.dwFlags |= STARTF_USESTDHANDLES; + } + + if (process->capture_err) { + if (!CreatePipe(&err[0], &err[1], &security_attrs, 0) || + !SetHandleInformation(err[0], HANDLE_FLAG_INHERIT, 0)) { + git_error_set(GIT_ERROR_OS, "could not create pipe"); + goto on_error; + } + + startup_info.hStdError = err[1]; + startup_info.dwFlags |= STARTF_USESTDHANDLES; + } + + memset(&process->process_info, 0, sizeof(PROCESS_INFORMATION)); + + if (!CreateProcessW(process->app_path, process->cmdline, + NULL, NULL, TRUE, flags, process->env, + process->cwd, + &startup_info, + &process->process_info)) { + git_error_set(GIT_ERROR_OS, "could not create process"); + goto on_error; + } + + CLOSE_HANDLE(in[0]); process->child_in = in[1]; + CLOSE_HANDLE(out[1]); process->child_out = out[0]; + CLOSE_HANDLE(err[1]); process->child_err = err[0]; + + return 0; + +on_error: + CLOSE_HANDLE(in[0]); CLOSE_HANDLE(in[1]); + CLOSE_HANDLE(out[0]); CLOSE_HANDLE(out[1]); + CLOSE_HANDLE(err[0]); CLOSE_HANDLE(err[1]); + return -1; +} + +int git_process_id(p_pid_t *out, git_process *process) +{ + GIT_ASSERT(out && process); + + if (!process->process_info.dwProcessId) { + git_error_set(GIT_ERROR_INVALID, "process not running"); + return -1; + } + + *out = process->process_info.dwProcessId; + return 0; +} + +ssize_t git_process_read(git_process *process, void *buf, size_t count) +{ + DWORD ret; + + if (count > DWORD_MAX) + count = DWORD_MAX; + if (count > SSIZE_MAX) + count = SSIZE_MAX; + + if (!ReadFile(process->child_out, buf, (DWORD)count, &ret, NULL)) { + if (GetLastError() == ERROR_BROKEN_PIPE) + return 0; + + git_error_set(GIT_ERROR_OS, "could not read"); + return -1; + } + + return ret; +} + +ssize_t git_process_write(git_process *process, const void *buf, size_t count) +{ + DWORD ret; + + if (count > DWORD_MAX) + count = DWORD_MAX; + if (count > SSIZE_MAX) + count = SSIZE_MAX; + + if (!WriteFile(process->child_in, buf, (DWORD)count, &ret, NULL)) { + git_error_set(GIT_ERROR_OS, "could not write"); + return -1; + } + + return ret; +} + +int git_process_close_in(git_process *process) +{ + if (!process->capture_in) { + git_error_set(GIT_ERROR_INVALID, "input is not open"); + return -1; + } + + if (process->child_in) { + CloseHandle(process->child_in); + process->child_in = NULL; + } + + return 0; +} + +int git_process_close_out(git_process *process) +{ + if (!process->capture_out) { + git_error_set(GIT_ERROR_INVALID, "output is not open"); + return -1; + } + + if (process->child_out) { + CloseHandle(process->child_out); + process->child_out = NULL; + } + + return 0; +} + +int git_process_close_err(git_process *process) +{ + if (!process->capture_err) { + git_error_set(GIT_ERROR_INVALID, "error is not open"); + return -1; + } + + if (process->child_err) { + CloseHandle(process->child_err); + process->child_err = NULL; + } + + return 0; +} + +int git_process_close(git_process *process) +{ + if (process->child_in) { + CloseHandle(process->child_in); + process->child_in = NULL; + } + + if (process->child_out) { + CloseHandle(process->child_out); + process->child_out = NULL; + } + + if (process->child_err) { + CloseHandle(process->child_err); + process->child_err = NULL; + } + + CloseHandle(process->process_info.hProcess); + process->process_info.hProcess = NULL; + + CloseHandle(process->process_info.hThread); + process->process_info.hThread = NULL; + + return 0; +} + +int git_process_wait(git_process_result *result, git_process *process) +{ + DWORD exitcode; + + if (result) + memset(result, 0, sizeof(git_process_result)); + + if (!process->process_info.dwProcessId) { + git_error_set(GIT_ERROR_INVALID, "process is stopped"); + return -1; + } + + if (WaitForSingleObject(process->process_info.hProcess, INFINITE) == WAIT_FAILED) { + git_error_set(GIT_ERROR_OS, "could not wait for process"); + return -1; + } + + if (!GetExitCodeProcess(process->process_info.hProcess, &exitcode)) { + git_error_set(GIT_ERROR_OS, "could not get process exit code"); + return -1; + } + + result->status = GIT_PROCESS_STATUS_NORMAL; + result->exitcode = exitcode; + + memset(&process->process_info, 0, sizeof(PROCESS_INFORMATION)); + return 0; +} + +int git_process_result_msg(git_str *out, git_process_result *result) +{ + if (result->status == GIT_PROCESS_STATUS_NONE) { + return git_str_puts(out, "process not started"); + } else if (result->status == GIT_PROCESS_STATUS_NORMAL) { + return git_str_printf(out, "process exited with code %d", + result->exitcode); + } else if (result->signal) { + return git_str_printf(out, "process exited on signal %d", + result->signal); + } + + return git_str_puts(out, "unknown error"); +} + +void git_process_free(git_process *process) +{ + if (!process) + return; + + if (process->process_info.hProcess) + git_process_close(process); + + git__free(process->env); + git__free(process->cwd); + git__free(process->cmdline); + git__free(process->app_path); + git__free(process->app_name); + git__free(process); +} diff --git a/src/util/win32/reparse.h b/src/util/win32/reparse.h new file mode 100644 index 00000000000..23312319f68 --- /dev/null +++ b/src/util/win32/reparse.h @@ -0,0 +1,57 @@ +/* +* Copyright (C) the libgit2 contributors. All rights reserved. +* +* This file is part of libgit2, distributed under the GNU GPL v2 with +* a Linking Exception. For full terms see the included COPYING file. +*/ + +#ifndef INCLUDE_win32_reparse_h__ +#define INCLUDE_win32_reparse_h__ + +/* This structure is defined on MSDN at +* http://msdn.microsoft.com/en-us/library/windows/hardware/ff552012(v=vs.85).aspx +* +* It was formerly included in the Windows 2000 SDK and remains defined in +* MinGW, so we must define it with a silly name to avoid conflicting. +*/ +typedef struct _GIT_REPARSE_DATA_BUFFER { + ULONG ReparseTag; + USHORT ReparseDataLength; + USHORT Reserved; + union { + struct { + USHORT SubstituteNameOffset; + USHORT SubstituteNameLength; + USHORT PrintNameOffset; + USHORT PrintNameLength; + ULONG Flags; + WCHAR PathBuffer[1]; + } SymbolicLink; + struct { + USHORT SubstituteNameOffset; + USHORT SubstituteNameLength; + USHORT PrintNameOffset; + USHORT PrintNameLength; + WCHAR PathBuffer[1]; + } MountPoint; + struct { + UCHAR DataBuffer[1]; + } Generic; + } ReparseBuffer; +} GIT_REPARSE_DATA_BUFFER; + +#define REPARSE_DATA_HEADER_SIZE 8 +#define REPARSE_DATA_MOUNTPOINT_HEADER_SIZE 8 +#define REPARSE_DATA_UNION_SIZE 12 + +/* Missing in MinGW */ +#ifndef FSCTL_GET_REPARSE_POINT +# define FSCTL_GET_REPARSE_POINT 0x000900a8 +#endif + +/* Missing in MinGW */ +#ifndef FSCTL_SET_REPARSE_POINT +# define FSCTL_SET_REPARSE_POINT 0x000900a4 +#endif + +#endif diff --git a/src/util/win32/thread.c b/src/util/win32/thread.c new file mode 100644 index 00000000000..f5cacd320d8 --- /dev/null +++ b/src/util/win32/thread.c @@ -0,0 +1,262 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "thread.h" +#include "runtime.h" + +#define CLEAN_THREAD_EXIT 0x6F012842 + +typedef void (WINAPI *win32_srwlock_fn)(GIT_SRWLOCK *); + +static win32_srwlock_fn win32_srwlock_initialize; +static win32_srwlock_fn win32_srwlock_acquire_shared; +static win32_srwlock_fn win32_srwlock_release_shared; +static win32_srwlock_fn win32_srwlock_acquire_exclusive; +static win32_srwlock_fn win32_srwlock_release_exclusive; + +static DWORD fls_index; + +/* The thread procedure stub used to invoke the caller's procedure + * and capture the return value for later collection. Windows will + * only hold a DWORD, but we need to be able to store an entire + * void pointer. This requires the indirection. */ +static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter) +{ + git_thread *thread = lpParameter; + + /* Set the current thread for `git_thread_exit` */ + FlsSetValue(fls_index, thread); + + thread->result = thread->proc(thread->param); + + return CLEAN_THREAD_EXIT; +} + +static void git_threads_global_shutdown(void) +{ + FlsFree(fls_index); +} + +int git_threads_global_init(void) +{ + HMODULE hModule = GetModuleHandleW(L"kernel32"); + + if (hModule) { + win32_srwlock_initialize = (win32_srwlock_fn)(void *) + GetProcAddress(hModule, "InitializeSRWLock"); + win32_srwlock_acquire_shared = (win32_srwlock_fn)(void *) + GetProcAddress(hModule, "AcquireSRWLockShared"); + win32_srwlock_release_shared = (win32_srwlock_fn)(void *) + GetProcAddress(hModule, "ReleaseSRWLockShared"); + win32_srwlock_acquire_exclusive = (win32_srwlock_fn)(void *) + GetProcAddress(hModule, "AcquireSRWLockExclusive"); + win32_srwlock_release_exclusive = (win32_srwlock_fn)(void *) + GetProcAddress(hModule, "ReleaseSRWLockExclusive"); + } + + if ((fls_index = FlsAlloc(NULL)) == FLS_OUT_OF_INDEXES) + return -1; + + return git_runtime_shutdown_register(git_threads_global_shutdown); +} + +int git_thread_create( + git_thread *GIT_RESTRICT thread, + void *(*start_routine)(void*), + void *GIT_RESTRICT arg) +{ + thread->result = NULL; + thread->param = arg; + thread->proc = start_routine; + thread->thread = CreateThread( + NULL, 0, git_win32__threadproc, thread, 0, NULL); + + return thread->thread ? 0 : -1; +} + +int git_thread_join( + git_thread *thread, + void **value_ptr) +{ + DWORD exit; + + if (WaitForSingleObject(thread->thread, INFINITE) != WAIT_OBJECT_0) + return -1; + + if (!GetExitCodeThread(thread->thread, &exit)) { + CloseHandle(thread->thread); + return -1; + } + + /* Check for the thread having exited uncleanly. If exit was unclean, + * then we don't have a return value to give back to the caller. */ + GIT_ASSERT(exit == CLEAN_THREAD_EXIT); + + if (value_ptr) + *value_ptr = thread->result; + + CloseHandle(thread->thread); + return 0; +} + +void git_thread_exit(void *value) +{ + git_thread *thread = FlsGetValue(fls_index); + + if (thread) + thread->result = value; + + ExitThread(CLEAN_THREAD_EXIT); +} + +size_t git_thread_currentid(void) +{ + return GetCurrentThreadId(); +} + +int git_mutex_init(git_mutex *GIT_RESTRICT mutex) +{ + InitializeCriticalSection(mutex); + return 0; +} + +int git_mutex_free(git_mutex *mutex) +{ + DeleteCriticalSection(mutex); + return 0; +} + +int git_mutex_lock(git_mutex *mutex) +{ + EnterCriticalSection(mutex); + return 0; +} + +int git_mutex_unlock(git_mutex *mutex) +{ + LeaveCriticalSection(mutex); + return 0; +} + +int git_cond_init(git_cond *cond) +{ + /* This is an auto-reset event. */ + *cond = CreateEventW(NULL, FALSE, FALSE, NULL); + GIT_ASSERT(*cond); + + /* If we can't create the event, claim that the reason was out-of-memory. + * The actual reason can be fetched with GetLastError(). */ + return *cond ? 0 : ENOMEM; +} + +int git_cond_free(git_cond *cond) +{ + BOOL closed; + + if (!cond) + return EINVAL; + + closed = CloseHandle(*cond); + GIT_ASSERT(closed); + GIT_UNUSED(closed); + + *cond = NULL; + return 0; +} + +int git_cond_wait(git_cond *cond, git_mutex *mutex) +{ + int error; + DWORD wait_result; + + if (!cond || !mutex) + return EINVAL; + + /* The caller must be holding the mutex. */ + error = git_mutex_unlock(mutex); + + if (error) + return error; + + wait_result = WaitForSingleObject(*cond, INFINITE); + GIT_ASSERT(WAIT_OBJECT_0 == wait_result); + GIT_UNUSED(wait_result); + + return git_mutex_lock(mutex); +} + +int git_cond_signal(git_cond *cond) +{ + BOOL signaled; + + if (!cond) + return EINVAL; + + signaled = SetEvent(*cond); + GIT_ASSERT(signaled); + GIT_UNUSED(signaled); + + return 0; +} + +int git_rwlock_init(git_rwlock *GIT_RESTRICT lock) +{ + if (win32_srwlock_initialize) + win32_srwlock_initialize(&lock->native.srwl); + else + InitializeCriticalSection(&lock->native.csec); + + return 0; +} + +int git_rwlock_rdlock(git_rwlock *lock) +{ + if (win32_srwlock_acquire_shared) + win32_srwlock_acquire_shared(&lock->native.srwl); + else + EnterCriticalSection(&lock->native.csec); + + return 0; +} + +int git_rwlock_rdunlock(git_rwlock *lock) +{ + if (win32_srwlock_release_shared) + win32_srwlock_release_shared(&lock->native.srwl); + else + LeaveCriticalSection(&lock->native.csec); + + return 0; +} + +int git_rwlock_wrlock(git_rwlock *lock) +{ + if (win32_srwlock_acquire_exclusive) + win32_srwlock_acquire_exclusive(&lock->native.srwl); + else + EnterCriticalSection(&lock->native.csec); + + return 0; +} + +int git_rwlock_wrunlock(git_rwlock *lock) +{ + if (win32_srwlock_release_exclusive) + win32_srwlock_release_exclusive(&lock->native.srwl); + else + LeaveCriticalSection(&lock->native.csec); + + return 0; +} + +int git_rwlock_free(git_rwlock *lock) +{ + if (!win32_srwlock_initialize) + DeleteCriticalSection(&lock->native.csec); + git__memzero(lock, sizeof(*lock)); + return 0; +} diff --git a/src/util/win32/thread.h b/src/util/win32/thread.h new file mode 100644 index 00000000000..184762e2aa9 --- /dev/null +++ b/src/util/win32/thread.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_win32_thread_h__ +#define INCLUDE_win32_thread_h__ + +#include "git2_util.h" + +#if defined (_MSC_VER) +# define GIT_RESTRICT __restrict +#else +# define GIT_RESTRICT __restrict__ +#endif + +typedef struct { + HANDLE thread; + void *(*proc)(void *); + void *param; + void *result; +} git_thread; + +typedef CRITICAL_SECTION git_mutex; +typedef HANDLE git_cond; + +typedef struct { void *Ptr; } GIT_SRWLOCK; + +typedef struct { + union { + GIT_SRWLOCK srwl; + CRITICAL_SECTION csec; + } native; +} git_rwlock; + +int git_threads_global_init(void); + +int git_thread_create(git_thread *GIT_RESTRICT, + void *(*) (void *), + void *GIT_RESTRICT); +int git_thread_join(git_thread *, void **); +size_t git_thread_currentid(void); +void git_thread_exit(void *); + +int git_mutex_init(git_mutex *GIT_RESTRICT mutex); +int git_mutex_free(git_mutex *); +int git_mutex_lock(git_mutex *); +int git_mutex_unlock(git_mutex *); + +int git_cond_init(git_cond *); +int git_cond_free(git_cond *); +int git_cond_wait(git_cond *, git_mutex *); +int git_cond_signal(git_cond *); + +int git_rwlock_init(git_rwlock *GIT_RESTRICT lock); +int git_rwlock_rdlock(git_rwlock *); +int git_rwlock_rdunlock(git_rwlock *); +int git_rwlock_wrlock(git_rwlock *); +int git_rwlock_wrunlock(git_rwlock *); +int git_rwlock_free(git_rwlock *); + +#endif diff --git a/src/util/win32/utf-conv.c b/src/util/win32/utf-conv.c new file mode 100644 index 00000000000..ad35c0c35ff --- /dev/null +++ b/src/util/win32/utf-conv.c @@ -0,0 +1,144 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "utf-conv.h" + +GIT_INLINE(void) git__set_errno(void) +{ + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) + errno = ENAMETOOLONG; + else + errno = EINVAL; +} + +int git_utf8_to_16(wchar_t *dest, size_t dest_size, const char *src) +{ + /* Length of -1 indicates NULL termination of the input string. */ + return git_utf8_to_16_with_len(dest, dest_size, src, -1); +} + +int git_utf8_to_16_with_len( + wchar_t *dest, + size_t _dest_size, + const char *src, + int src_len) +{ + int dest_size = (int)min(_dest_size, INT_MAX); + int len; + + /* + * Subtract 1 from the result to turn 0 into -1 (an error code) and + * to not count the NULL terminator as part of the string's length. + * MultiByteToWideChar never returns int's minvalue, so underflow + * is not possible. + */ + len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, + src, src_len, dest, dest_size) - 1; + + if (len < 0) + git__set_errno(); + + return len; +} + +int git_utf8_from_16(char *dest, size_t dest_size, const wchar_t *src) +{ + /* Length of -1 indicates NULL termination of the input string. */ + return git_utf8_from_16_with_len(dest, dest_size, src, -1); +} + +int git_utf8_from_16_with_len( + char *dest, + size_t _dest_size, + const wchar_t *src, + int src_len) +{ + int dest_size = (int)min(_dest_size, INT_MAX); + int len; + + /* + * Subtract 1 from the result to turn 0 into -1 (an error code) and + * to not count the NULL terminator as part of the string's length. + * WideCharToMultiByte never returns int's minvalue, so underflow + * is not possible. + */ + len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, + src, src_len, dest, dest_size, NULL, NULL) - 1; + + if (len < 0) + git__set_errno(); + + return len; +} + +int git_utf8_to_16_alloc(wchar_t **dest, const char *src) +{ + /* Length of -1 indicates NULL termination of the input string. */ + return git_utf8_to_16_alloc_with_len(dest, src, -1); +} + +int git_utf8_to_16_alloc_with_len(wchar_t **dest, const char *src, int src_len) +{ + int utf16_size; + + *dest = NULL; + + utf16_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, + src, src_len, NULL, 0); + + if (!utf16_size) { + git__set_errno(); + return -1; + } + + *dest = git__mallocarray(utf16_size, sizeof(wchar_t)); + GIT_ERROR_CHECK_ALLOC(*dest); + + utf16_size = git_utf8_to_16_with_len(*dest, (size_t)utf16_size, + src, src_len); + + if (utf16_size < 0) { + git__free(*dest); + *dest = NULL; + } + + return utf16_size; +} + +int git_utf8_from_16_alloc(char **dest, const wchar_t *src) +{ + /* Length of -1 indicates NULL termination of the input string. */ + return git_utf8_from_16_alloc_with_len(dest, src, -1); +} + +int git_utf8_from_16_alloc_with_len(char **dest, const wchar_t *src, int src_len) +{ + int utf8_size; + + *dest = NULL; + + utf8_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, + src, src_len, NULL, 0, NULL, NULL); + + if (!utf8_size) { + git__set_errno(); + return -1; + } + + *dest = git__malloc(utf8_size); + GIT_ERROR_CHECK_ALLOC(*dest); + + utf8_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, + src, src_len, *dest, utf8_size, NULL, NULL); + + if (utf8_size < 0) { + git__free(*dest); + *dest = NULL; + } + + return utf8_size; +} diff --git a/src/util/win32/utf-conv.h b/src/util/win32/utf-conv.h new file mode 100644 index 00000000000..301f5a6d36c --- /dev/null +++ b/src/util/win32/utf-conv.h @@ -0,0 +1,127 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_win32_utf_conv_h__ +#define INCLUDE_win32_utf_conv_h__ + +#include "git2_util.h" + +#include + +#ifndef WC_ERR_INVALID_CHARS +# define WC_ERR_INVALID_CHARS 0x80 +#endif + +/** + * Converts a NUL-terminated UTF-8 string to wide characters. This is a + * convenience function for `git_utf8_to_16_with_len`. + * + * @param dest The buffer to receive the wide string. + * @param dest_size The size of the buffer, in characters. + * @param src The UTF-8 string to convert. + * @return The length of the wide string, in characters + * (not counting the NULL terminator), or < 0 for failure + */ +int git_utf8_to_16(wchar_t *dest, size_t dest_size, const char *src); + +/** + * Converts a UTF-8 string to wide characters. + * + * @param dest The buffer to receive the wide string. + * @param dest_size The size of the buffer, in characters. + * @param src The UTF-8 string to convert. + * @param src_len The length of the string to convert. + * @return The length of the wide string, in characters + * (not counting the NULL terminator), or < 0 for failure + */ +int git_utf8_to_16_with_len( + wchar_t *dest, + size_t dest_size, + const char *src, + int src_len); + +/** + * Converts a NUL-terminated wide string to UTF-8. This is a convenience + * function for `git_utf8_from_16_with_len`. + * + * @param dest The buffer to receive the UTF-8 string. + * @param dest_size The size of the buffer, in bytes. + * @param src The wide string to convert. + * @param src_len The length of the string to convert. + * @return The length of the UTF-8 string, in bytes + * (not counting the NULL terminator), or < 0 for failure + */ +int git_utf8_from_16(char *dest, size_t dest_size, const wchar_t *src); + +/** + * Converts a wide string to UTF-8. + * + * @param dest The buffer to receive the UTF-8 string. + * @param dest_size The size of the buffer, in bytes. + * @param src The wide string to convert. + * @param src_len The length of the string to convert. + * @return The length of the UTF-8 string, in bytes + * (not counting the NULL terminator), or < 0 for failure + */ +int git_utf8_from_16_with_len(char *dest, size_t dest_size, const wchar_t *src, int src_len); + +/** + * Converts a UTF-8 string to wide characters. Memory is allocated to hold + * the converted string. The caller is responsible for freeing the string + * with git__free. + * + * @param dest Receives a pointer to the wide string. + * @param src The UTF-8 string to convert. + * @return The length of the wide string, in characters + * (not counting the NULL terminator), or < 0 for failure + */ +int git_utf8_to_16_alloc(wchar_t **dest, const char *src); + +/** + * Converts a UTF-8 string to wide characters. Memory is allocated to hold + * the converted string. The caller is responsible for freeing the string + * with git__free. + * + * @param dest Receives a pointer to the wide string. + * @param src The UTF-8 string to convert. + * @param src_len The length of the string. + * @return The length of the wide string, in characters + * (not counting the NULL terminator), or < 0 for failure + */ +int git_utf8_to_16_alloc_with_len( + wchar_t **dest, + const char *src, + int src_len); + +/** + * Converts a wide string to UTF-8. Memory is allocated to hold the + * converted string. The caller is responsible for freeing the string + * with git__free. + * + * @param dest Receives a pointer to the UTF-8 string. + * @param src The wide string to convert. + * @return The length of the UTF-8 string, in bytes + * (not counting the NULL terminator), or < 0 for failure + */ +int git_utf8_from_16_alloc(char **dest, const wchar_t *src); + +/** + * Converts a wide string to UTF-8. Memory is allocated to hold the + * converted string. The caller is responsible for freeing the string + * with git__free. + * + * @param dest Receives a pointer to the UTF-8 string. + * @param src The wide string to convert. + * @param src_len The length of the wide string. + * @return The length of the UTF-8 string, in bytes + * (not counting the NULL terminator), or < 0 for failure + */ +int git_utf8_from_16_alloc_with_len( + char **dest, + const wchar_t *src, + int src_len); + +#endif diff --git a/src/util/win32/version.h b/src/util/win32/version.h new file mode 100644 index 00000000000..79667697f00 --- /dev/null +++ b/src/util/win32/version.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_win32_version_h__ +#define INCLUDE_win32_version_h__ + +#include + +GIT_INLINE(int) git_has_win32_version(int major, int minor, int service_pack) +{ + OSVERSIONINFOEX version_test = {0}; + DWORD version_test_mask; + DWORDLONG version_condition_mask = 0; + + version_test.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); + version_test.dwMajorVersion = major; + version_test.dwMinorVersion = minor; + version_test.wServicePackMajor = (WORD)service_pack; + version_test.wServicePackMinor = 0; + + version_test_mask = (VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR); + + VER_SET_CONDITION(version_condition_mask, VER_MAJORVERSION, VER_GREATER_EQUAL); + VER_SET_CONDITION(version_condition_mask, VER_MINORVERSION, VER_GREATER_EQUAL); + VER_SET_CONDITION(version_condition_mask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL); + VER_SET_CONDITION(version_condition_mask, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL); + + if (!VerifyVersionInfo(&version_test, version_test_mask, version_condition_mask)) + return 0; + + return 1; +} + +#endif diff --git a/src/util/win32/w32_buffer.c b/src/util/win32/w32_buffer.c new file mode 100644 index 00000000000..6fee8203c24 --- /dev/null +++ b/src/util/win32/w32_buffer.c @@ -0,0 +1,57 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "w32_buffer.h" + +#include "utf-conv.h" + +GIT_INLINE(int) handle_wc_error(void) +{ + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) + errno = ENAMETOOLONG; + else + errno = EINVAL; + + return -1; +} + +int git_str_put_w(git_str *buf, const wchar_t *string_w, size_t len_w) +{ + int utf8_len, utf8_write_len; + size_t new_size; + + if (!len_w) { + return 0; + } else if (len_w > INT_MAX) { + git_error_set_oom(); + return -1; + } + + GIT_ASSERT(string_w); + + /* Measure the string necessary for conversion */ + if ((utf8_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string_w, (int)len_w, NULL, 0, NULL, NULL)) == 0) + return 0; + + GIT_ASSERT(utf8_len > 0); + + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, (size_t)utf8_len); + GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); + + if (git_str_grow(buf, new_size) < 0) + return -1; + + if ((utf8_write_len = WideCharToMultiByte( + CP_UTF8, WC_ERR_INVALID_CHARS, string_w, (int)len_w, &buf->ptr[buf->size], utf8_len, NULL, NULL)) == 0) + return handle_wc_error(); + + GIT_ASSERT(utf8_write_len == utf8_len); + + buf->size += utf8_write_len; + buf->ptr[buf->size] = '\0'; + return 0; +} diff --git a/src/util/win32/w32_buffer.h b/src/util/win32/w32_buffer.h new file mode 100644 index 00000000000..68ea9603567 --- /dev/null +++ b/src/util/win32/w32_buffer.h @@ -0,0 +1,19 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_win32_w32_buffer_h__ +#define INCLUDE_win32_w32_buffer_h__ + +#include "git2_util.h" +#include "str.h" + +/** + * Convert a wide character string to UTF-8 and append the results to the + * buffer. + */ +int git_str_put_w(git_str *buf, const wchar_t *string_w, size_t len_w); + +#endif diff --git a/src/util/win32/w32_common.h b/src/util/win32/w32_common.h new file mode 100644 index 00000000000..c20b3e85e7e --- /dev/null +++ b/src/util/win32/w32_common.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_win32_w32_common_h__ +#define INCLUDE_win32_w32_common_h__ + +#include + +/* + * 4096 is the max allowed Git path. `MAX_PATH` (260) is the typical max allowed + * Windows path length, however win32 Unicode APIs generally allow up to 32,767 + * if prefixed with "\\?\" (i.e. converted to an NT-style name). + */ +#define GIT_WIN_PATH_MAX GIT_PATH_MAX + +/* + * Provides a large enough buffer to support Windows Git paths: + * GIT_WIN_PATH_MAX is 4096, corresponding to a maximum path length of 4095 + * characters plus a NULL terminator. Prefixing with "\\?\" adds 4 characters, + * but if the original was a UNC path, then we turn "\\server\share" into + * "\\?\UNC\server\share". So we replace the first two characters with + * 8 characters, a net gain of 6, so the maximum length is GIT_WIN_PATH_MAX+6. + */ +#define GIT_WIN_PATH_UTF16 GIT_WIN_PATH_MAX+6 + +/* Maximum size of a UTF-8 Win32 Git path. We remove the "\\?\" or "\\?\UNC\" + * prefixes for presentation, bringing us back to 4095 (non-NULL) + * characters. UTF-8 does have 4-byte sequences, but they are encoded in + * UTF-16 using surrogate pairs, which takes up the space of two characters. + * Two characters in the range U+0800 -> U+FFFF take up more space in UTF-8 + * (6 bytes) than one surrogate pair (4 bytes). + */ +#define GIT_WIN_PATH_UTF8 ((GIT_WIN_PATH_MAX - 1) * 3 + 1) + +/* + * The length of a Windows "shortname", for 8.3 compatibility. + */ +#define GIT_WIN_PATH_SHORTNAME 13 + +/* Win32 path types */ +typedef wchar_t git_win32_path[GIT_WIN_PATH_UTF16]; +typedef char git_win32_utf8_path[GIT_WIN_PATH_UTF8]; + +#endif diff --git a/src/util/win32/w32_leakcheck.c b/src/util/win32/w32_leakcheck.c new file mode 100644 index 00000000000..26c20918ce3 --- /dev/null +++ b/src/util/win32/w32_leakcheck.c @@ -0,0 +1,581 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "w32_leakcheck.h" + +#if defined(GIT_DEBUG_LEAKCHECK_WIN32) + +#include "Windows.h" +#include "Dbghelp.h" +#include "win32/posix.h" +#include "hash.h" +#include "runtime.h" + +/* Stack frames (for stack tracing, below) */ + +static bool g_win32_stack_initialized = false; +static HANDLE g_win32_stack_process = INVALID_HANDLE_VALUE; +static git_win32_leakcheck_stack_aux_cb_alloc g_aux_cb_alloc = NULL; +static git_win32_leakcheck_stack_aux_cb_lookup g_aux_cb_lookup = NULL; + +int git_win32_leakcheck_stack_set_aux_cb( + git_win32_leakcheck_stack_aux_cb_alloc cb_alloc, + git_win32_leakcheck_stack_aux_cb_lookup cb_lookup) +{ + g_aux_cb_alloc = cb_alloc; + g_aux_cb_lookup = cb_lookup; + + return 0; +} + +/** + * Load symbol table data. This should be done in the primary + * thread at startup (under a lock if there are other threads + * active). + */ +void git_win32_leakcheck_stack_init(void) +{ + if (!g_win32_stack_initialized) { + g_win32_stack_process = GetCurrentProcess(); + SymSetOptions(SYMOPT_LOAD_LINES); + SymInitialize(g_win32_stack_process, NULL, TRUE); + g_win32_stack_initialized = true; + } +} + +/** + * Cleanup symbol table data. This should be done in the + * primary thead at shutdown (under a lock if there are other + * threads active). + */ +void git_win32_leakcheck_stack_cleanup(void) +{ + if (g_win32_stack_initialized) { + SymCleanup(g_win32_stack_process); + g_win32_stack_process = INVALID_HANDLE_VALUE; + g_win32_stack_initialized = false; + } +} + +int git_win32_leakcheck_stack_capture(git_win32_leakcheck_stack_raw_data *pdata, int skip) +{ + if (!g_win32_stack_initialized) { + git_error_set(GIT_ERROR_INVALID, "git_win32_stack not initialized."); + return GIT_ERROR; + } + + memset(pdata, 0, sizeof(*pdata)); + pdata->nr_frames = RtlCaptureStackBackTrace( + skip+1, GIT_WIN32_LEAKCHECK_STACK_MAX_FRAMES, pdata->frames, NULL); + + /* If an "aux" data provider was registered, ask it to capture + * whatever data it needs and give us an "aux_id" to it so that + * we can refer to it later when reporting. + */ + if (g_aux_cb_alloc) + (g_aux_cb_alloc)(&pdata->aux_id); + + return 0; +} + +int git_win32_leakcheck_stack_compare( + git_win32_leakcheck_stack_raw_data *d1, + git_win32_leakcheck_stack_raw_data *d2) +{ + return memcmp(d1, d2, sizeof(*d1)); +} + +int git_win32_leakcheck_stack_format( + char *pbuf, size_t buf_len, + const git_win32_leakcheck_stack_raw_data *pdata, + const char *prefix, const char *suffix) +{ +#define MY_MAX_FILENAME 255 + + /* SYMBOL_INFO has char FileName[1] at the end. The docs say to + * to malloc it with extra space for your desired max filename. + */ + struct { + SYMBOL_INFO symbol; + char extra[MY_MAX_FILENAME + 1]; + } s; + + IMAGEHLP_LINE64 line; + size_t buf_used = 0; + unsigned int k; + char detail[MY_MAX_FILENAME * 2]; /* filename plus space for function name and formatting */ + size_t detail_len; + + if (!g_win32_stack_initialized) { + git_error_set(GIT_ERROR_INVALID, "git_win32_stack not initialized."); + return GIT_ERROR; + } + + if (!prefix) + prefix = "\t"; + if (!suffix) + suffix = "\n"; + + memset(pbuf, 0, buf_len); + + memset(&s, 0, sizeof(s)); + s.symbol.MaxNameLen = MY_MAX_FILENAME; + s.symbol.SizeOfStruct = sizeof(SYMBOL_INFO); + + memset(&line, 0, sizeof(line)); + line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); + + for (k=0; k < pdata->nr_frames; k++) { + DWORD64 frame_k = (DWORD64)pdata->frames[k]; + DWORD dwUnused; + + if (SymFromAddr(g_win32_stack_process, frame_k, 0, &s.symbol) && + SymGetLineFromAddr64(g_win32_stack_process, frame_k, &dwUnused, &line)) { + const char *pslash; + const char *pfile; + + pslash = strrchr(line.FileName, '\\'); + pfile = ((pslash) ? (pslash+1) : line.FileName); + p_snprintf(detail, sizeof(detail), "%s%s:%d> %s%s", + prefix, pfile, line.LineNumber, s.symbol.Name, suffix); + } else { + /* This happens when we cross into another module. + * For example, in CLAR tests, this is typically + * the CRT startup code. Just print an unknown + * frame and continue. + */ + p_snprintf(detail, sizeof(detail), "%s??%s", prefix, suffix); + } + detail_len = strlen(detail); + + if (buf_len < (buf_used + detail_len + 1)) { + /* we don't have room for this frame in the buffer, so just stop. */ + break; + } + + memcpy(&pbuf[buf_used], detail, detail_len); + buf_used += detail_len; + } + + /* "aux_id" 0 is reserved to mean no aux data. This is needed to handle + * allocs that occur before the aux callbacks were registered. + */ + if (pdata->aux_id > 0) { + p_snprintf(detail, sizeof(detail), "%saux_id: %d%s", + prefix, pdata->aux_id, suffix); + detail_len = strlen(detail); + if ((buf_used + detail_len + 1) < buf_len) { + memcpy(&pbuf[buf_used], detail, detail_len); + buf_used += detail_len; + } + + /* If an "aux" data provider is still registered, ask it to append its detailed + * data to the end of ours using the "aux_id" it gave us when this de-duped + * item was created. + */ + if (g_aux_cb_lookup) + (g_aux_cb_lookup)(pdata->aux_id, &pbuf[buf_used], (buf_len - buf_used - 1)); + } + + return GIT_OK; +} + +int git_win32_leakcheck_stack( + char * pbuf, size_t buf_len, + int skip, + const char *prefix, const char *suffix) +{ + git_win32_leakcheck_stack_raw_data data; + int error; + + if ((error = git_win32_leakcheck_stack_capture(&data, skip)) < 0) + return error; + if ((error = git_win32_leakcheck_stack_format(pbuf, buf_len, &data, prefix, suffix)) < 0) + return error; + return 0; +} + +/* Stack tracing */ + +#define STACKTRACE_UID_LEN (15) + +/** + * The stacktrace of an allocation can be distilled + * to a unique id based upon the stackframe pointers + * and ignoring any size arguments. We will use these + * UIDs as the (char const*) __FILE__ argument we + * give to the CRT malloc routines. + */ +typedef struct { + char uid[STACKTRACE_UID_LEN + 1]; +} git_win32_leakcheck_stacktrace_uid; + +/** + * All mallocs with the same stacktrace will be de-duped + * and aggregated into this row. + */ +typedef struct { + git_win32_leakcheck_stacktrace_uid uid; /* must be first */ + git_win32_leakcheck_stack_raw_data raw_data; + unsigned int count_allocs; /* times this alloc signature seen since init */ + unsigned int count_allocs_at_last_checkpoint; /* times since last mark */ + unsigned int transient_count_leaks; /* sum of leaks */ +} git_win32_leakcheck_stacktrace_row; + +static CRITICAL_SECTION g_crtdbg_stacktrace_cs; + +/** + * CRTDBG memory leak tracking takes a "char const * const file_name" + * and stores the pointer in the heap data (instead of allocing a copy + * for itself). Normally, this is not a problem, since we usually pass + * in __FILE__. But I'm going to lie to it and pass in the address of + * the UID in place of the file_name. Also, I do not want to alloc the + * stacktrace data (because we are called from inside our alloc routines). + * Therefore, I'm creating a very large static pool array to store row + * data. This also eliminates the temptation to realloc it (and move the + * UID pointers). + * + * And to efficiently look for duplicates we need an index on the rows + * so we can bsearch it. Again, without mallocing. + * + * If we observe more than MY_ROW_LIMIT unique malloc signatures, we + * fall through and use the traditional __FILE__ processing and don't + * try to de-dup them. If your testing hits this limit, just increase + * it and try again. + */ + +#define MY_ROW_LIMIT (2 * 1024 * 1024) +static git_win32_leakcheck_stacktrace_row g_cs_rows[MY_ROW_LIMIT]; +static git_win32_leakcheck_stacktrace_row *g_cs_index[MY_ROW_LIMIT]; + +static unsigned int g_cs_end = MY_ROW_LIMIT; +static unsigned int g_cs_ins = 0; /* insertion point == unique allocs seen */ +static unsigned int g_count_total_allocs = 0; /* number of allocs seen */ +static unsigned int g_transient_count_total_leaks = 0; /* number of total leaks */ +static unsigned int g_transient_count_dedup_leaks = 0; /* number of unique leaks */ +static bool g_limit_reached = false; /* had allocs after we filled row table */ + +static unsigned int g_checkpoint_id = 0; /* to better label leak checkpoints */ +static bool g_transient_leaks_since_mark = false; /* payload for hook */ + +/** + * Compare function for bsearch on g_cs_index table. + */ +static int row_cmp(const void *v1, const void *v2) +{ + git_win32_leakcheck_stack_raw_data *d1 = (git_win32_leakcheck_stack_raw_data*)v1; + git_win32_leakcheck_stacktrace_row *r2 = (git_win32_leakcheck_stacktrace_row *)v2; + + return (git_win32_leakcheck_stack_compare(d1, &r2->raw_data)); +} + +/** + * Unique insert the new data into the row and index tables. + * We have to sort by the stackframe data itself, not the uid. + */ +static git_win32_leakcheck_stacktrace_row * insert_unique( + const git_win32_leakcheck_stack_raw_data *pdata) +{ + size_t pos; + if (git__bsearch(g_cs_index, g_cs_ins, pdata, row_cmp, &pos) < 0) { + /* Append new unique item to row table. */ + memcpy(&g_cs_rows[g_cs_ins].raw_data, pdata, sizeof(*pdata)); + sprintf(g_cs_rows[g_cs_ins].uid.uid, "##%08lx", g_cs_ins); + + /* Insert pointer to it into the proper place in the index table. */ + if (pos < g_cs_ins) + memmove(&g_cs_index[pos+1], &g_cs_index[pos], (g_cs_ins - pos)*sizeof(g_cs_index[0])); + g_cs_index[pos] = &g_cs_rows[g_cs_ins]; + + g_cs_ins++; + } + + g_cs_index[pos]->count_allocs++; + + return g_cs_index[pos]; +} + +/** + * Hook function to receive leak data from the CRT. (This includes + * both ":()" data, but also each of the + * various headers and fields. + * + * Scan this for the special "##" UID forms that we substituted + * for the "". Map back to the row data and + * increment its leak count. + * + * See https://msdn.microsoft.com/en-us/library/74kabxyx.aspx + * + * We suppress the actual crtdbg output. + */ +static int __cdecl report_hook(int nRptType, char *szMsg, int *retVal) +{ + static int hook_result = TRUE; /* FALSE to get stock dump; TRUE to suppress. */ + unsigned int pos; + + *retVal = 0; /* do not invoke debugger */ + + if ((szMsg[0] != '#') || (szMsg[1] != '#')) + return hook_result; + + if (sscanf(&szMsg[2], "%08lx", &pos) < 1) + return hook_result; + if (pos >= g_cs_ins) + return hook_result; + + if (g_transient_leaks_since_mark) { + if (g_cs_rows[pos].count_allocs == g_cs_rows[pos].count_allocs_at_last_checkpoint) + return hook_result; + } + + g_cs_rows[pos].transient_count_leaks++; + + if (g_cs_rows[pos].transient_count_leaks == 1) + g_transient_count_dedup_leaks++; + + g_transient_count_total_leaks++; + + return hook_result; +} + +/** + * Write leak data to all of the various places we need. + * We force the caller to sprintf() the message first + * because we want to avoid fprintf() because it allocs. + */ +static void my_output(const char *buf) +{ + fwrite(buf, strlen(buf), 1, stderr); + OutputDebugString(buf); +} + +/** + * For each row with leaks, dump a stacktrace for it. + */ +static void dump_summary(const char *label) +{ + unsigned int k; + char buf[10 * 1024]; + + if (g_transient_count_total_leaks == 0) + return; + + fflush(stdout); + fflush(stderr); + my_output("\n"); + + if (g_limit_reached) { + sprintf(buf, + "LEAK SUMMARY: de-dup row table[%d] filled. Increase MY_ROW_LIMIT.\n", + MY_ROW_LIMIT); + my_output(buf); + } + + if (!label) + label = ""; + + if (g_transient_leaks_since_mark) { + sprintf(buf, "LEAK CHECKPOINT %d: leaks %d unique %d: %s\n", + g_checkpoint_id, g_transient_count_total_leaks, g_transient_count_dedup_leaks, label); + my_output(buf); + } else { + sprintf(buf, "LEAK SUMMARY: TOTAL leaks %d de-duped %d: %s\n", + g_transient_count_total_leaks, g_transient_count_dedup_leaks, label); + my_output(buf); + } + my_output("\n"); + + for (k = 0; k < g_cs_ins; k++) { + if (g_cs_rows[k].transient_count_leaks > 0) { + sprintf(buf, "LEAK: %s leaked %d of %d times:\n", + g_cs_rows[k].uid.uid, + g_cs_rows[k].transient_count_leaks, + g_cs_rows[k].count_allocs); + my_output(buf); + + if (git_win32_leakcheck_stack_format( + buf, sizeof(buf), &g_cs_rows[k].raw_data, + NULL, NULL) >= 0) { + my_output(buf); + } + + my_output("\n"); + } + } + + fflush(stderr); +} + +/** + * Initialize our memory leak tracking and de-dup data structures. + * This should ONLY be called by git_libgit2_init(). + */ +void git_win32_leakcheck_stacktrace_init(void) +{ + InitializeCriticalSection(&g_crtdbg_stacktrace_cs); + + EnterCriticalSection(&g_crtdbg_stacktrace_cs); + + _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); + + _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE); + _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE); + _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE); + + _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); + _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR); + _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR); + + LeaveCriticalSection(&g_crtdbg_stacktrace_cs); +} + +int git_win32_leakcheck_stacktrace_dump( + git_win32_leakcheck_stacktrace_options opt, + const char *label) +{ + _CRT_REPORT_HOOK old; + unsigned int k; + int r = 0; + +#define IS_BIT_SET(o,b) (((o) & (b)) != 0) + + bool b_set_mark = IS_BIT_SET(opt, GIT_WIN32_LEAKCHECK_STACKTRACE_SET_MARK); + bool b_leaks_since_mark = IS_BIT_SET(opt, GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_SINCE_MARK); + bool b_leaks_total = IS_BIT_SET(opt, GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_TOTAL); + bool b_quiet = IS_BIT_SET(opt, GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET); + + if (b_leaks_since_mark && b_leaks_total) { + git_error_set(GIT_ERROR_INVALID, "cannot combine LEAKS_SINCE_MARK and LEAKS_TOTAL."); + return GIT_ERROR; + } + if (!b_set_mark && !b_leaks_since_mark && !b_leaks_total) { + git_error_set(GIT_ERROR_INVALID, "nothing to do."); + return GIT_ERROR; + } + + EnterCriticalSection(&g_crtdbg_stacktrace_cs); + + if (b_leaks_since_mark || b_leaks_total) { + /* All variables with "transient" in the name are per-dump counters + * and reset before each dump. This lets us handle checkpoints. + */ + g_transient_count_total_leaks = 0; + g_transient_count_dedup_leaks = 0; + for (k = 0; k < g_cs_ins; k++) { + g_cs_rows[k].transient_count_leaks = 0; + } + } + + g_transient_leaks_since_mark = b_leaks_since_mark; + + old = _CrtSetReportHook(report_hook); + _CrtDumpMemoryLeaks(); + _CrtSetReportHook(old); + + if (b_leaks_since_mark || b_leaks_total) { + r = g_transient_count_dedup_leaks; + + if (!b_quiet) + dump_summary(label); + } + + if (b_set_mark) { + for (k = 0; k < g_cs_ins; k++) { + g_cs_rows[k].count_allocs_at_last_checkpoint = g_cs_rows[k].count_allocs; + } + + g_checkpoint_id++; + } + + LeaveCriticalSection(&g_crtdbg_stacktrace_cs); + + return r; +} + +/** + * Shutdown our memory leak tracking and dump summary data. + * This should ONLY be called by git_libgit2_shutdown(). + * + * We explicitly call _CrtDumpMemoryLeaks() during here so + * that we can compute summary data for the leaks. We print + * the stacktrace of each unique leak. + * + * This cleanup does not happen if the app calls exit() + * without calling the libgit2 shutdown code. + * + * This info we print here is independent of any automatic + * reporting during exit() caused by _CRTDBG_LEAK_CHECK_DF. + * Set it in your app if you also want traditional reporting. + */ +void git_win32_leakcheck_stacktrace_cleanup(void) +{ + /* At shutdown/cleanup, dump cumulative leak info + * with everything since startup. This might generate + * extra noise if the caller has been doing checkpoint + * dumps, but it might also eliminate some false + * positives for resources previously reported during + * checkpoints. + */ + git_win32_leakcheck_stacktrace_dump( + GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_TOTAL, + "CLEANUP"); + + DeleteCriticalSection(&g_crtdbg_stacktrace_cs); +} + +const char *git_win32_leakcheck_stacktrace(int skip, const char *file) +{ + git_win32_leakcheck_stack_raw_data new_data; + git_win32_leakcheck_stacktrace_row *row; + const char * result = file; + + if (git_win32_leakcheck_stack_capture(&new_data, skip+1) < 0) + return result; + + EnterCriticalSection(&g_crtdbg_stacktrace_cs); + + if (g_cs_ins < g_cs_end) { + row = insert_unique(&new_data); + result = row->uid.uid; + } else { + g_limit_reached = true; + } + + g_count_total_allocs++; + + LeaveCriticalSection(&g_crtdbg_stacktrace_cs); + + return result; +} + +static void git_win32_leakcheck_global_shutdown(void) +{ + git_win32_leakcheck_stacktrace_cleanup(); + git_win32_leakcheck_stack_cleanup(); +} + +bool git_win32_leakcheck_has_leaks(void) +{ + return (g_transient_count_total_leaks > 0); +} + +int git_win32_leakcheck_global_init(void) +{ + git_win32_leakcheck_stacktrace_init(); + git_win32_leakcheck_stack_init(); + + return git_runtime_shutdown_register(git_win32_leakcheck_global_shutdown); +} + +#else + +int git_win32_leakcheck_global_init(void) +{ + return 0; +} + +#endif diff --git a/src/util/win32/w32_leakcheck.h b/src/util/win32/w32_leakcheck.h new file mode 100644 index 00000000000..52ff10a777a --- /dev/null +++ b/src/util/win32/w32_leakcheck.h @@ -0,0 +1,222 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_win32_leakcheck_h__ +#define INCLUDE_win32_leakcheck_h__ + +#include "git2_util.h" + +/* Initialize the win32 leak checking system. */ +int git_win32_leakcheck_global_init(void); + +#if defined(GIT_DEBUG_LEAKCHECK_WIN32) + +#include +#include + +#include "git2/errors.h" +#include "strnlen.h" + +bool git_win32_leakcheck_has_leaks(void); + +/* Stack frames (for stack tracing, below) */ + +/** + * This type defines a callback to be used to augment a C stacktrace + * with "aux" data. This can be used, for example, to allow LibGit2Sharp + * (or other interpreted consumer libraries) to give us C# stacktrace + * data for the PInvoke. + * + * This callback will be called during crtdbg-instrumented allocs. + * + * @param aux_id [out] A returned "aux_id" representing a unique + * (de-duped at the C# layer) stacktrace. "aux_id" 0 is reserved + * to mean no aux stacktrace data. + */ +typedef void (*git_win32_leakcheck_stack_aux_cb_alloc)(unsigned int *aux_id); + +/** + * This type defines a callback to be used to augment the output of + * a stacktrace. This will be used to request the C# layer format + * the C# stacktrace associated with "aux_id" into the provided + * buffer. + * + * This callback will be called during leak reporting. + * + * @param aux_id The "aux_id" key associated with a stacktrace. + * @param aux_msg A buffer where a formatted message should be written. + * @param aux_msg_len The size of the buffer. + */ +typedef void (*git_win32_leakcheck_stack_aux_cb_lookup)(unsigned int aux_id, char *aux_msg, size_t aux_msg_len); + +/** + * Register an "aux" data provider to augment our C stacktrace data. + * + * This can be used, for example, to allow LibGit2Sharp (or other + * interpreted consumer libraries) to give us the C# stacktrace of + * the PInvoke. + * + * If you choose to use this feature, it should be registered during + * initialization and not changed for the duration of the process. + */ +int git_win32_leakcheck_stack_set_aux_cb( + git_win32_leakcheck_stack_aux_cb_alloc cb_alloc, + git_win32_leakcheck_stack_aux_cb_lookup cb_lookup); + +/** + * Maximum number of stackframes to record for a + * single stacktrace. + */ +#define GIT_WIN32_LEAKCHECK_STACK_MAX_FRAMES 30 + +/** + * Wrapper containing the raw unprocessed stackframe + * data for a single stacktrace and any "aux_id". + * + * I put the aux_id first so leaks will be sorted by it. + * So, for example, if a specific callstack in C# leaks + * a repo handle, all of the pointers within the associated + * repo pointer will be grouped together. + */ +typedef struct { + unsigned int aux_id; + unsigned int nr_frames; + void *frames[GIT_WIN32_LEAKCHECK_STACK_MAX_FRAMES]; +} git_win32_leakcheck_stack_raw_data; + +/** + * Capture raw stack trace data for the current process/thread. + * + * @param skip Number of initial frames to skip. Pass 0 to + * begin with the caller of this routine. Pass 1 to begin + * with its caller. And so on. + */ +int git_win32_leakcheck_stack_capture(git_win32_leakcheck_stack_raw_data *pdata, int skip); + +/** + * Compare 2 raw stacktraces with the usual -1,0,+1 result. + * This includes any "aux_id" values in the comparison, so that + * our de-dup is also "aux" context relative. + */ +int git_win32_leakcheck_stack_compare( + git_win32_leakcheck_stack_raw_data *d1, + git_win32_leakcheck_stack_raw_data *d2); + +/** + * Format raw stacktrace data into buffer WITHOUT using any mallocs. + * + * @param prefix String written before each frame; defaults to "\t". + * @param suffix String written after each frame; defaults to "\n". + */ +int git_win32_leakcheck_stack_format( + char *pbuf, size_t buf_len, + const git_win32_leakcheck_stack_raw_data *pdata, + const char *prefix, const char *suffix); + +/** + * Convenience routine to capture and format stacktrace into + * a buffer WITHOUT using any mallocs. This is primarily a + * wrapper for testing. + * + * @param skip Number of initial frames to skip. Pass 0 to + * begin with the caller of this routine. Pass 1 to begin + * with its caller. And so on. + * @param prefix String written before each frame; defaults to "\t". + * @param suffix String written after each frame; defaults to "\n". + */ +int git_win32_leakcheck_stack( + char * pbuf, size_t buf_len, + int skip, + const char *prefix, const char *suffix); + +/* Stack tracing */ + +/* MSVC CRTDBG memory leak reporting. + * + * We DO NOT use the "_CRTDBG_MAP_ALLOC" macro described in the MSVC + * documentation because all allocs/frees in libgit2 already go through + * the "git__" routines defined in this file. Simply using the normal + * reporting mechanism causes all leaks to be attributed to a routine + * here in util.h (ie, the actual call to calloc()) rather than the + * caller of git__calloc(). + * + * Therefore, we declare a set of "git__crtdbg__" routines to replace + * the corresponding "git__" routines and re-define the "git__" symbols + * as macros. This allows us to get and report the file:line info of + * the real caller. + * + * We DO NOT replace the "git__free" routine because it needs to remain + * a function pointer because it is used as a function argument when + * setting up various structure "destructors". + * + * We also DO NOT use the "_CRTDBG_MAP_ALLOC" macro because it causes + * "free" to be remapped to "_free_dbg" and this causes problems for + * structures which define a field named "free". + * + * Finally, CRTDBG must be explicitly enabled and configured at program + * startup. See tests/main.c for an example. + */ + +/** + * Checkpoint options. + */ +typedef enum git_win32_leakcheck_stacktrace_options { + /** + * Set checkpoint marker. + */ + GIT_WIN32_LEAKCHECK_STACKTRACE_SET_MARK = (1 << 0), + + /** + * Dump leaks since last checkpoint marker. + * May not be combined with _LEAKS_TOTAL. + * + * Note that this may generate false positives for global TLS + * error state and other global caches that aren't cleaned up + * until the thread/process terminates. So when using this + * around a region of interest, also check the final (at exit) + * dump before digging into leaks reported here. + */ + GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_SINCE_MARK = (1 << 1), + + /** + * Dump leaks since init. May not be combined + * with _LEAKS_SINCE_MARK. + */ + GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_TOTAL = (1 << 2), + + /** + * Suppress printing during dumps. + * Just return leak count. + */ + GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET = (1 << 3), + +} git_win32_leakcheck_stacktrace_options; + +/** + * Checkpoint memory state and/or dump unique stack traces of + * current memory leaks. + * + * @return number of unique leaks (relative to requested starting + * point) or error. + */ +int git_win32_leakcheck_stacktrace_dump( + git_win32_leakcheck_stacktrace_options opt, + const char *label); + +/** + * Construct stacktrace and append it to the global buffer. + * Return pointer to start of this string. On any error or + * lack of buffer space, just return the given file buffer + * so it will behave as usual. + * + * This should ONLY be called by our internal memory allocations + * routines. + */ +const char *git_win32_leakcheck_stacktrace(int skip, const char *file); + +#endif +#endif diff --git a/src/util/win32/w32_util.c b/src/util/win32/w32_util.c new file mode 100644 index 00000000000..f5b006a1974 --- /dev/null +++ b/src/util/win32/w32_util.c @@ -0,0 +1,126 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "w32_util.h" + +/** + * Creates a FindFirstFile(Ex) filter string from a UTF-8 path. + * The filter string enumerates all items in the directory. + * + * @param dest The buffer to receive the filter string. + * @param src The UTF-8 path of the directory to enumerate. + * @return True if the filter string was created successfully; false otherwise + */ +bool git_win32__findfirstfile_filter(git_win32_path dest, const char *src) +{ + static const wchar_t suffix[] = L"\\*"; + int len = git_win32_path_from_utf8(dest, src); + + /* Ensure the path was converted */ + if (len < 0) + return false; + + /* Ensure that the path does not end with a trailing slash, + * because we're about to add one. Don't rely our trim_end + * helper, because we want to remove the backslash even for + * drive letter paths, in this case. */ + if (len > 0 && + (dest[len - 1] == L'/' || dest[len - 1] == L'\\')) { + dest[len - 1] = L'\0'; + len--; + } + + /* Ensure we have enough room to add the suffix */ + if ((size_t)len >= GIT_WIN_PATH_UTF16 - CONST_STRLEN(suffix)) + return false; + + wcscat(dest, suffix); + return true; +} + +/** + * Ensures the given path (file or folder) has the +H (hidden) attribute set. + * + * @param path The path which should receive the +H bit. + * @return 0 on success; -1 on failure + */ +int git_win32__set_hidden(const char *path, bool hidden) +{ + git_win32_path buf; + DWORD attrs, newattrs; + + if (git_win32_path_from_utf8(buf, path) < 0) + return -1; + + attrs = GetFileAttributesW(buf); + + /* Ensure the path exists */ + if (attrs == INVALID_FILE_ATTRIBUTES) + return -1; + + if (hidden) + newattrs = attrs | FILE_ATTRIBUTE_HIDDEN; + else + newattrs = attrs & ~FILE_ATTRIBUTE_HIDDEN; + + if (attrs != newattrs && !SetFileAttributesW(buf, newattrs)) { + git_error_set(GIT_ERROR_OS, "failed to %s hidden bit for '%s'", + hidden ? "set" : "unset", path); + return -1; + } + + return 0; +} + +int git_win32__hidden(bool *out, const char *path) +{ + git_win32_path buf; + DWORD attrs; + + if (git_win32_path_from_utf8(buf, path) < 0) + return -1; + + attrs = GetFileAttributesW(buf); + + /* Ensure the path exists */ + if (attrs == INVALID_FILE_ATTRIBUTES) + return -1; + + *out = (attrs & FILE_ATTRIBUTE_HIDDEN) ? true : false; + return 0; +} + +int git_win32__file_attribute_to_stat( + struct stat *st, + const WIN32_FILE_ATTRIBUTE_DATA *attrdata, + const wchar_t *path) +{ + git_win32__stat_init(st, + attrdata->dwFileAttributes, + attrdata->nFileSizeHigh, + attrdata->nFileSizeLow, + attrdata->ftCreationTime, + attrdata->ftLastAccessTime, + attrdata->ftLastWriteTime); + + if (attrdata->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT && path) { + git_win32_path target; + + if (git_win32_path_readlink_w(target, path) >= 0) { + st->st_mode = (st->st_mode & ~S_IFMT) | S_IFLNK; + + /* st_size gets the UTF-8 length of the target name, in bytes, + * not counting the NULL terminator */ + if ((st->st_size = git_utf8_from_16(NULL, 0, target)) < 0) { + git_error_set(GIT_ERROR_OS, "could not convert reparse point name for '%ls'", path); + return -1; + } + } + } + + return 0; +} diff --git a/src/util/win32/w32_util.h b/src/util/win32/w32_util.h new file mode 100644 index 00000000000..dfdf69cd0db --- /dev/null +++ b/src/util/win32/w32_util.h @@ -0,0 +1,146 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_win32_w32_util_h__ +#define INCLUDE_win32_w32_util_h__ + +#include "git2_util.h" + +#include "utf-conv.h" +#include "posix.h" +#include "path_w32.h" + +/* + +#include "common.h" +#include "path.h" +#include "path_w32.h" +#include "utf-conv.h" +#include "posix.h" +#include "reparse.h" +#include "dir.h" +*/ + + +GIT_INLINE(bool) git_win32__isalpha(wchar_t c) +{ + return ((c >= L'A' && c <= L'Z') || (c >= L'a' && c <= L'z')); +} + +/** + * Creates a FindFirstFile(Ex) filter string from a UTF-8 path. + * The filter string enumerates all items in the directory. + * + * @param dest The buffer to receive the filter string. + * @param src The UTF-8 path of the directory to enumerate. + * @return True if the filter string was created successfully; false otherwise + */ +bool git_win32__findfirstfile_filter(git_win32_path dest, const char *src); + +/** + * Ensures the given path (file or folder) has the +H (hidden) attribute set + * or unset. + * + * @param path The path that should receive the +H bit. + * @param hidden true to set +H, false to unset it + * @return 0 on success; -1 on failure + */ +extern int git_win32__set_hidden(const char *path, bool hidden); + +/** + * Determines if the given file or folder has the hidden attribute set. + * @param hidden pointer to store hidden value + * @param path The path that should be queried for hiddenness. + * @return 0 on success or an error code. + */ +extern int git_win32__hidden(bool *hidden, const char *path); + +extern int git_win32__file_attribute_to_stat( + struct stat *st, + const WIN32_FILE_ATTRIBUTE_DATA *attrdata, + const wchar_t *path); + +/** + * Converts a FILETIME structure to a struct timespec. + * + * @param FILETIME A pointer to a FILETIME + * @param ts A pointer to the timespec structure to fill in + */ +GIT_INLINE(void) git_win32__filetime_to_timespec( + const FILETIME *ft, + struct timespec *ts) +{ + int64_t winTime = ((int64_t)ft->dwHighDateTime << 32) + ft->dwLowDateTime; + winTime -= INT64_C(116444736000000000); /* Windows to Unix Epoch conversion */ + ts->tv_sec = (time_t)(winTime / 10000000); +#ifdef GIT_NSEC_WIN32 + ts->tv_nsec = (winTime % 10000000) * 100; +#elif GIT_NSEC +# error GIT_NSEC defined but GIT_NSEC_WIN32 not defined +#else + ts->tv_nsec = 0; +#endif +} + +GIT_INLINE(void) git_win32__timeval_to_filetime( + FILETIME *ft, const struct p_timeval tv) +{ + int64_t ticks = (tv.tv_sec * INT64_C(10000000)) + + (tv.tv_usec * INT64_C(10)) + INT64_C(116444736000000000); + + ft->dwHighDateTime = ((ticks >> 32) & INT64_C(0xffffffff)); + ft->dwLowDateTime = (ticks & INT64_C(0xffffffff)); +} + +GIT_INLINE(void) git_win32__stat_init( + struct stat *st, + DWORD dwFileAttributes, + DWORD nFileSizeHigh, + DWORD nFileSizeLow, + FILETIME ftCreationTime, + FILETIME ftLastAccessTime, + FILETIME ftLastWriteTime) +{ + mode_t mode = S_IREAD; + + memset(st, 0, sizeof(struct stat)); + + if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + mode |= S_IFDIR; + else + mode |= S_IFREG; + + if ((dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0) + mode |= S_IWRITE; + + st->st_ino = 0; + st->st_gid = 0; + st->st_uid = 0; + st->st_nlink = 1; + st->st_mode = mode; + st->st_size = ((int64_t)nFileSizeHigh << 32) + nFileSizeLow; + st->st_dev = _getdrive() - 1; + st->st_rdev = st->st_dev; + git_win32__filetime_to_timespec(&ftLastAccessTime, &(st->st_atim)); + git_win32__filetime_to_timespec(&ftLastWriteTime, &(st->st_mtim)); + git_win32__filetime_to_timespec(&ftCreationTime, &(st->st_ctim)); +} + +GIT_INLINE(void) git_win32__file_information_to_stat( + struct stat *st, + const BY_HANDLE_FILE_INFORMATION *fileinfo) +{ + git_win32__stat_init(st, + fileinfo->dwFileAttributes, + fileinfo->nFileSizeHigh, + fileinfo->nFileSizeLow, + fileinfo->ftCreationTime, + fileinfo->ftLastAccessTime, + fileinfo->ftLastWriteTime); +} + +#endif diff --git a/src/util/win32/win32-compat.h b/src/util/win32/win32-compat.h new file mode 100644 index 00000000000..dee40a438f0 --- /dev/null +++ b/src/util/win32/win32-compat.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_win32_win32_compat_h__ +#define INCLUDE_win32_win32_compat_h__ + +#include +#include +#include +#include +#include + +typedef long suseconds_t; + +struct p_timeval { + time_t tv_sec; + suseconds_t tv_usec; +}; + +struct p_timespec { + time_t tv_sec; + long tv_nsec; +}; + +#define timespec p_timespec + +struct p_stat { + _dev_t st_dev; + _ino_t st_ino; + mode_t st_mode; + short st_nlink; + short st_uid; + short st_gid; + _dev_t st_rdev; + __int64 st_size; + struct timespec st_atim; + struct timespec st_mtim; + struct timespec st_ctim; +#define st_atime st_atim.tv_sec +#define st_mtime st_mtim.tv_sec +#define st_ctime st_ctim.tv_sec +#define st_atime_nsec st_atim.tv_nsec +#define st_mtime_nsec st_mtim.tv_nsec +#define st_ctime_nsec st_ctim.tv_nsec +}; + +#define stat p_stat + +#endif diff --git a/src/util/zstream.c b/src/util/zstream.c new file mode 100644 index 00000000000..cb8b125ed5b --- /dev/null +++ b/src/util/zstream.c @@ -0,0 +1,210 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "zstream.h" + +#include + +#include "str.h" + +#define ZSTREAM_BUFFER_SIZE (1024 * 1024) +#define ZSTREAM_BUFFER_MIN_EXTRA 8 + +GIT_INLINE(int) zstream_seterr(git_zstream *zs) +{ + switch (zs->zerr) { + case Z_OK: + case Z_STREAM_END: + case Z_BUF_ERROR: /* not fatal; we retry with a larger buffer */ + return 0; + case Z_MEM_ERROR: + git_error_set_oom(); + break; + default: + if (zs->z.msg) + git_error_set_str(GIT_ERROR_ZLIB, zs->z.msg); + else + git_error_set(GIT_ERROR_ZLIB, "unknown compression error"); + } + + return -1; +} + +int git_zstream_init(git_zstream *zstream, git_zstream_t type) +{ + zstream->type = type; + + if (zstream->type == GIT_ZSTREAM_INFLATE) + zstream->zerr = inflateInit(&zstream->z); + else + zstream->zerr = deflateInit(&zstream->z, Z_DEFAULT_COMPRESSION); + return zstream_seterr(zstream); +} + +void git_zstream_free(git_zstream *zstream) +{ + if (zstream->type == GIT_ZSTREAM_INFLATE) + inflateEnd(&zstream->z); + else + deflateEnd(&zstream->z); +} + +void git_zstream_reset(git_zstream *zstream) +{ + if (zstream->type == GIT_ZSTREAM_INFLATE) + inflateReset(&zstream->z); + else + deflateReset(&zstream->z); + zstream->in = NULL; + zstream->in_len = 0; + zstream->zerr = Z_STREAM_END; +} + +int git_zstream_set_input(git_zstream *zstream, const void *in, size_t in_len) +{ + zstream->in = in; + zstream->in_len = in_len; + zstream->zerr = Z_OK; + return 0; +} + +bool git_zstream_done(git_zstream *zstream) +{ + return (!zstream->in_len && zstream->zerr == Z_STREAM_END); +} + +bool git_zstream_eos(git_zstream *zstream) +{ + return zstream->zerr == Z_STREAM_END; +} + +size_t git_zstream_suggest_output_len(git_zstream *zstream) +{ + if (zstream->in_len > ZSTREAM_BUFFER_SIZE) + return ZSTREAM_BUFFER_SIZE; + else if (zstream->in_len > ZSTREAM_BUFFER_MIN_EXTRA) + return zstream->in_len; + else + return ZSTREAM_BUFFER_MIN_EXTRA; +} + +int git_zstream_get_output_chunk( + void *out, size_t *out_len, git_zstream *zstream) +{ + size_t in_queued, in_used, out_queued; + + /* set up input data */ + zstream->z.next_in = (Bytef *)zstream->in; + + /* feed as much data to zlib as it can consume, at most UINT_MAX */ + if (zstream->in_len > UINT_MAX) { + zstream->z.avail_in = UINT_MAX; + zstream->flush = Z_NO_FLUSH; + } else { + zstream->z.avail_in = (uInt)zstream->in_len; + zstream->flush = Z_FINISH; + } + in_queued = (size_t)zstream->z.avail_in; + + /* set up output data */ + zstream->z.next_out = out; + zstream->z.avail_out = (uInt)*out_len; + + if ((size_t)zstream->z.avail_out != *out_len) + zstream->z.avail_out = UINT_MAX; + out_queued = (size_t)zstream->z.avail_out; + + /* compress next chunk */ + if (zstream->type == GIT_ZSTREAM_INFLATE) + zstream->zerr = inflate(&zstream->z, zstream->flush); + else + zstream->zerr = deflate(&zstream->z, zstream->flush); + + if (zstream_seterr(zstream)) + return -1; + + in_used = (in_queued - zstream->z.avail_in); + zstream->in_len -= in_used; + zstream->in += in_used; + + *out_len = (out_queued - zstream->z.avail_out); + + return 0; +} + +int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream) +{ + size_t out_remain = *out_len; + + if (zstream->in_len && zstream->zerr == Z_STREAM_END) { + git_error_set(GIT_ERROR_ZLIB, "zlib input had trailing garbage"); + return -1; + } + + while (out_remain > 0 && zstream->zerr != Z_STREAM_END) { + size_t out_written = out_remain; + + if (git_zstream_get_output_chunk(out, &out_written, zstream) < 0) + return -1; + + out_remain -= out_written; + out = ((char *)out) + out_written; + } + + /* either we finished the input or we did not flush the data */ + GIT_ASSERT(zstream->in_len > 0 || zstream->flush == Z_FINISH); + + /* set out_size to number of bytes actually written to output */ + *out_len = *out_len - out_remain; + + return 0; +} + +static int zstream_buf(git_str *out, const void *in, size_t in_len, git_zstream_t type) +{ + git_zstream zs = GIT_ZSTREAM_INIT; + int error = 0; + + if ((error = git_zstream_init(&zs, type)) < 0) + return error; + + if ((error = git_zstream_set_input(&zs, in, in_len)) < 0) + goto done; + + while (!git_zstream_done(&zs)) { + size_t step = git_zstream_suggest_output_len(&zs), written; + + if ((error = git_str_grow_by(out, step)) < 0) + goto done; + + written = out->asize - out->size; + + if ((error = git_zstream_get_output( + out->ptr + out->size, &written, &zs)) < 0) + goto done; + + out->size += written; + } + + /* NULL terminate for consistency if possible */ + if (out->size < out->asize) + out->ptr[out->size] = '\0'; + +done: + git_zstream_free(&zs); + return error; +} + +int git_zstream_deflatebuf(git_str *out, const void *in, size_t in_len) +{ + return zstream_buf(out, in, in_len, GIT_ZSTREAM_DEFLATE); +} + +int git_zstream_inflatebuf(git_str *out, const void *in, size_t in_len) +{ + return zstream_buf(out, in, in_len, GIT_ZSTREAM_INFLATE); +} diff --git a/src/util/zstream.h b/src/util/zstream.h new file mode 100644 index 00000000000..d78b1129145 --- /dev/null +++ b/src/util/zstream.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_zstream_h__ +#define INCLUDE_zstream_h__ + +#include "git2_util.h" + +#include + +#include "str.h" + +typedef enum { + GIT_ZSTREAM_INFLATE, + GIT_ZSTREAM_DEFLATE +} git_zstream_t; + +typedef struct { + z_stream z; + git_zstream_t type; + const char *in; + size_t in_len; + int flush; + int zerr; +} git_zstream; + +#define GIT_ZSTREAM_INIT {{0}} + +int git_zstream_init(git_zstream *zstream, git_zstream_t type); +void git_zstream_free(git_zstream *zstream); + +int git_zstream_set_input(git_zstream *zstream, const void *in, size_t in_len); + +size_t git_zstream_suggest_output_len(git_zstream *zstream); + +/* get as much output as is available in the input buffer */ +int git_zstream_get_output_chunk( + void *out, size_t *out_len, git_zstream *zstream); + +/* get all the output from the entire input buffer */ +int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream); + +bool git_zstream_done(git_zstream *zstream); +bool git_zstream_eos(git_zstream *zstream); + +void git_zstream_reset(git_zstream *zstream); + +int git_zstream_deflatebuf(git_str *out, const void *in, size_t in_len); +int git_zstream_inflatebuf(git_str *out, const void *in, size_t in_len); + +#endif diff --git a/src/vector.c b/src/vector.c deleted file mode 100644 index f4a818ed287..00000000000 --- a/src/vector.c +++ /dev/null @@ -1,306 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "repository.h" -#include "vector.h" - -/* In elements, not bytes */ -#define MIN_ALLOCSIZE 8 - -GIT_INLINE(size_t) compute_new_size(git_vector *v) -{ - size_t new_size = v->_alloc_size; - - /* Use a resize factor of 1.5, which is quick to compute using integer - * instructions and less than the golden ratio (1.618...) */ - if (new_size < MIN_ALLOCSIZE) - new_size = MIN_ALLOCSIZE; - else if (new_size <= (SIZE_MAX / 3) * 2) - new_size += new_size / 2; - else - new_size = SIZE_MAX; - - return new_size; -} - -GIT_INLINE(int) resize_vector(git_vector *v, size_t new_size) -{ - size_t new_bytes = new_size * sizeof(void *); - void *new_contents; - - /* Check for overflow */ - if (new_bytes / sizeof(void *) != new_size) - GITERR_CHECK_ALLOC(NULL); - - new_contents = git__realloc(v->contents, new_bytes); - GITERR_CHECK_ALLOC(new_contents); - - v->_alloc_size = new_size; - v->contents = new_contents; - - return 0; -} - -int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp) -{ - size_t bytes; - - assert(v && src); - - bytes = src->length * sizeof(void *); - - v->_alloc_size = src->length; - v->_cmp = cmp; - v->length = src->length; - v->sorted = src->sorted && cmp == src->_cmp; - v->contents = git__malloc(bytes); - GITERR_CHECK_ALLOC(v->contents); - - memcpy(v->contents, src->contents, bytes); - - return 0; -} - -void git_vector_free(git_vector *v) -{ - assert(v); - - git__free(v->contents); - v->contents = NULL; - - v->length = 0; - v->_alloc_size = 0; -} - -int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp) -{ - assert(v); - - v->_alloc_size = 0; - v->_cmp = cmp; - v->length = 0; - v->sorted = 1; - v->contents = NULL; - - return resize_vector(v, max(initial_size, MIN_ALLOCSIZE)); -} - -int git_vector_insert(git_vector *v, void *element) -{ - assert(v); - - if (v->length >= v->_alloc_size && - resize_vector(v, compute_new_size(v)) < 0) - return -1; - - v->contents[v->length++] = element; - v->sorted = 0; - - return 0; -} - -int git_vector_insert_sorted( - git_vector *v, void *element, int (*on_dup)(void **old, void *new)) -{ - int result; - size_t pos; - - assert(v && v->_cmp); - - if (!v->sorted) - git_vector_sort(v); - - if (v->length >= v->_alloc_size && - resize_vector(v, compute_new_size(v)) < 0) - return -1; - - /* If we find the element and have a duplicate handler callback, - * invoke it. If it returns non-zero, then cancel insert, otherwise - * proceed with normal insert. - */ - if (!git__bsearch(v->contents, v->length, element, v->_cmp, &pos) && - on_dup && (result = on_dup(&v->contents[pos], element)) < 0) - return result; - - /* shift elements to the right */ - if (pos < v->length) - memmove(v->contents + pos + 1, v->contents + pos, - (v->length - pos) * sizeof(void *)); - - v->contents[pos] = element; - v->length++; - - return 0; -} - -void git_vector_sort(git_vector *v) -{ - assert(v); - - if (v->sorted || !v->_cmp) - return; - - git__tsort(v->contents, v->length, v->_cmp); - v->sorted = 1; -} - -int git_vector_bsearch2( - size_t *at_pos, - git_vector *v, - git_vector_cmp key_lookup, - const void *key) -{ - assert(v && key && key_lookup); - - /* need comparison function to sort the vector */ - if (!v->_cmp) - return -1; - - git_vector_sort(v); - - return git__bsearch(v->contents, v->length, key, key_lookup, at_pos); -} - -int git_vector_search2( - size_t *at_pos, const git_vector *v, git_vector_cmp key_lookup, const void *key) -{ - size_t i; - - assert(v && key && key_lookup); - - for (i = 0; i < v->length; ++i) { - if (key_lookup(key, v->contents[i]) == 0) { - if (at_pos) - *at_pos = i; - - return 0; - } - } - - return GIT_ENOTFOUND; -} - -static int strict_comparison(const void *a, const void *b) -{ - return (a == b) ? 0 : -1; -} - -int git_vector_search(size_t *at_pos, const git_vector *v, const void *entry) -{ - return git_vector_search2(at_pos, v, v->_cmp ? v->_cmp : strict_comparison, entry); -} - -int git_vector_remove(git_vector *v, size_t idx) -{ - size_t shift_count; - - assert(v); - - if (idx >= v->length) - return GIT_ENOTFOUND; - - shift_count = v->length - idx - 1; - - if (shift_count) - memmove(&v->contents[idx], &v->contents[idx + 1], - shift_count * sizeof(void *)); - - v->length--; - return 0; -} - -void git_vector_pop(git_vector *v) -{ - if (v->length > 0) - v->length--; -} - -void git_vector_uniq(git_vector *v) -{ - git_vector_cmp cmp; - size_t i, j; - - if (v->length <= 1) - return; - - git_vector_sort(v); - cmp = v->_cmp ? v->_cmp : strict_comparison; - - for (i = 0, j = 1 ; j < v->length; ++j) - if (!cmp(v->contents[i], v->contents[j])) - v->contents[i] = v->contents[j]; - else - v->contents[++i] = v->contents[j]; - - v->length -= j - i - 1; -} - -void git_vector_remove_matching( - git_vector *v, int (*match)(const git_vector *v, size_t idx)) -{ - size_t i, j; - - for (i = 0, j = 0; j < v->length; ++j) { - v->contents[i] = v->contents[j]; - - if (!match(v, i)) - i++; - } - - v->length = i; -} - -void git_vector_clear(git_vector *v) -{ - assert(v); - v->length = 0; - v->sorted = 1; -} - -void git_vector_swap(git_vector *a, git_vector *b) -{ - git_vector t; - - assert(a && b); - - if (a != b) { - memcpy(&t, a, sizeof(t)); - memcpy(a, b, sizeof(t)); - memcpy(b, &t, sizeof(t)); - } -} - -int git_vector_resize_to(git_vector *v, size_t new_length) -{ - if (new_length <= v->length) - return 0; - - if (new_length > v->_alloc_size && - resize_vector(v, new_length) < 0) - return -1; - - memset(&v->contents[v->length], 0, - sizeof(void *) * (new_length - v->length)); - - v->length = new_length; - - return 0; -} - -int git_vector_set(void **old, git_vector *v, size_t position, void *value) -{ - if (git_vector_resize_to(v, position + 1) < 0) - return -1; - - if (old != NULL) - *old = v->contents[position]; - - v->contents[position] = value; - - return 0; -} diff --git a/src/vector.h b/src/vector.h deleted file mode 100644 index 690e4af9c68..00000000000 --- a/src/vector.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_vector_h__ -#define INCLUDE_vector_h__ - -#include "git2/common.h" - -typedef int (*git_vector_cmp)(const void *, const void *); - -typedef struct git_vector { - size_t _alloc_size; - git_vector_cmp _cmp; - void **contents; - size_t length; - int sorted; -} git_vector; - -#define GIT_VECTOR_INIT {0} - -int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp); -void git_vector_free(git_vector *v); -void git_vector_clear(git_vector *v); -int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp); -void git_vector_swap(git_vector *a, git_vector *b); - -void git_vector_sort(git_vector *v); - -/** Linear search for matching entry using internal comparison function */ -int git_vector_search(size_t *at_pos, const git_vector *v, const void *entry); - -/** Linear search for matching entry using explicit comparison function */ -int git_vector_search2(size_t *at_pos, const git_vector *v, git_vector_cmp cmp, const void *key); - -/** - * Binary search for matching entry using explicit comparison function that - * returns position where item would go if not found. - */ -int git_vector_bsearch2( - size_t *at_pos, git_vector *v, git_vector_cmp cmp, const void *key); - -/** Binary search for matching entry using internal comparison function */ -GIT_INLINE(int) git_vector_bsearch(size_t *at_pos, git_vector *v, const void *key) -{ - return git_vector_bsearch2(at_pos, v, v->_cmp, key); -} - -GIT_INLINE(void *) git_vector_get(const git_vector *v, size_t position) -{ - return (position < v->length) ? v->contents[position] : NULL; -} - -#define GIT_VECTOR_GET(V,I) ((I) < (V)->length ? (V)->contents[(I)] : NULL) - -GIT_INLINE(void *) git_vector_last(const git_vector *v) -{ - return (v->length > 0) ? git_vector_get(v, v->length - 1) : NULL; -} - -#define git_vector_foreach(v, iter, elem) \ - for ((iter) = 0; (iter) < (v)->length && ((elem) = (v)->contents[(iter)], 1); (iter)++ ) - -#define git_vector_rforeach(v, iter, elem) \ - for ((iter) = (v)->length; (iter) > 0 && ((elem) = (v)->contents[(iter)-1], 1); (iter)-- ) - -int git_vector_insert(git_vector *v, void *element); -int git_vector_insert_sorted(git_vector *v, void *element, - int (*on_dup)(void **old, void *new)); -int git_vector_remove(git_vector *v, size_t idx); -void git_vector_pop(git_vector *v); -void git_vector_uniq(git_vector *v); -void git_vector_remove_matching( - git_vector *v, int (*match)(const git_vector *v, size_t idx)); - -int git_vector_resize_to(git_vector *v, size_t new_length); -int git_vector_set(void **old, git_vector *v, size_t position, void *value); - -#endif diff --git a/src/win32/dir.c b/src/win32/dir.c deleted file mode 100644 index 95ae5060e88..00000000000 --- a/src/win32/dir.c +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#define GIT__WIN32_NO_WRAP_DIR -#include "dir.h" -#include "utf-conv.h" - -static int init_filter(char *filter, size_t n, const char *dir) -{ - size_t len = strlen(dir); - - if (len+3 >= n) - return 0; - - strcpy(filter, dir); - if (len && dir[len-1] != '/') - strcat(filter, "/"); - strcat(filter, "*"); - - return 1; -} - -git__DIR *git__opendir(const char *dir) -{ - char filter[GIT_WIN_PATH]; - wchar_t filter_w[GIT_WIN_PATH]; - git__DIR *new = NULL; - - if (!dir || !init_filter(filter, sizeof(filter), dir)) - return NULL; - - new = git__malloc(sizeof(*new)); - if (!new) - return NULL; - - new->dir = git__strdup(dir); - if (!new->dir) - goto fail; - - git__utf8_to_16(filter_w, GIT_WIN_PATH, filter); - new->h = FindFirstFileW(filter_w, &new->f); - - if (new->h == INVALID_HANDLE_VALUE) { - giterr_set(GITERR_OS, "Could not open directory '%s'", dir); - goto fail; - } - - new->first = 1; - return new; - -fail: - git__free(new->dir); - git__free(new); - return NULL; -} - -int git__readdir_ext( - git__DIR *d, - struct git__dirent *entry, - struct git__dirent **result, - int *is_dir) -{ - if (!d || !entry || !result || d->h == INVALID_HANDLE_VALUE) - return -1; - - *result = NULL; - - if (d->first) - d->first = 0; - else if (!FindNextFileW(d->h, &d->f)) { - if (GetLastError() == ERROR_NO_MORE_FILES) - return 0; - giterr_set(GITERR_OS, "Could not read from directory '%s'", d->dir); - return -1; - } - - if (wcslen(d->f.cFileName) >= sizeof(entry->d_name)) - return -1; - - git__utf16_to_8(entry->d_name, d->f.cFileName); - entry->d_ino = 0; - - *result = entry; - - if (is_dir != NULL) - *is_dir = ((d->f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0); - - return 0; -} - -struct git__dirent *git__readdir(git__DIR *d) -{ - struct git__dirent *result; - if (git__readdir_ext(d, &d->entry, &result, NULL) < 0) - return NULL; - return result; -} - -void git__rewinddir(git__DIR *d) -{ - char filter[GIT_WIN_PATH]; - wchar_t filter_w[GIT_WIN_PATH]; - - if (!d) - return; - - if (d->h != INVALID_HANDLE_VALUE) { - FindClose(d->h); - d->h = INVALID_HANDLE_VALUE; - d->first = 0; - } - - if (!init_filter(filter, sizeof(filter), d->dir)) - return; - - git__utf8_to_16(filter_w, GIT_WIN_PATH, filter); - d->h = FindFirstFileW(filter_w, &d->f); - - if (d->h == INVALID_HANDLE_VALUE) - giterr_set(GITERR_OS, "Could not open directory '%s'", d->dir); - else - d->first = 1; -} - -int git__closedir(git__DIR *d) -{ - if (!d) - return 0; - - if (d->h != INVALID_HANDLE_VALUE) { - FindClose(d->h); - d->h = INVALID_HANDLE_VALUE; - } - git__free(d->dir); - d->dir = NULL; - git__free(d); - return 0; -} - diff --git a/src/win32/error.c b/src/win32/error.c deleted file mode 100644 index 3851ff09926..00000000000 --- a/src/win32/error.c +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "error.h" - -char *git_win32_get_error_message(DWORD error_code) -{ - LPWSTR lpMsgBuf = NULL; - - if (!error_code) - return NULL; - - if (FormatMessageW( - FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPWSTR)&lpMsgBuf, 0, NULL)) { - int utf8_size = WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, NULL, 0, NULL, NULL); - - char *lpMsgBuf_utf8 = git__malloc(utf8_size * sizeof(char)); - if (lpMsgBuf_utf8 == NULL) { - LocalFree(lpMsgBuf); - return NULL; - } - if (!WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, lpMsgBuf_utf8, utf8_size, NULL, NULL)) { - LocalFree(lpMsgBuf); - git__free(lpMsgBuf_utf8); - return NULL; - } - - LocalFree(lpMsgBuf); - return lpMsgBuf_utf8; - } - return NULL; -} diff --git a/src/win32/error.h b/src/win32/error.h deleted file mode 100644 index 12947a2e67c..00000000000 --- a/src/win32/error.h +++ /dev/null @@ -1,13 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#ifndef INCLUDE_git_win32_error_h__ -#define INCLUDE_git_win32_error_h__ - -extern char *git_win32_get_error_message(DWORD error_code); - -#endif diff --git a/src/win32/findfile.c b/src/win32/findfile.c deleted file mode 100644 index 6fc7c751315..00000000000 --- a/src/win32/findfile.c +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "utf-conv.h" -#include "path.h" -#include "findfile.h" - -#define REG_MSYSGIT_INSTALL_LOCAL L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1" -#ifndef _WIN64 -#define REG_MSYSGIT_INSTALL REG_MSYSGIT_INSTALL_LOCAL -#else -#define REG_MSYSGIT_INSTALL L"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1" -#endif - -int win32_expand_path(struct win32_path *s_root, const wchar_t *templ) -{ - s_root->len = ExpandEnvironmentStringsW(templ, s_root->path, MAX_PATH); - return s_root->len ? 0 : -1; -} - -int win32_find_file(git_buf *path, const struct win32_path *root, const char *filename) -{ - size_t len, alloc_len; - wchar_t *file_utf16 = NULL; - char file_utf8[GIT_PATH_MAX]; - - if (!root || !filename || (len = strlen(filename)) == 0) - return GIT_ENOTFOUND; - - /* allocate space for wchar_t path to file */ - alloc_len = root->len + len + 2; - file_utf16 = git__calloc(alloc_len, sizeof(wchar_t)); - GITERR_CHECK_ALLOC(file_utf16); - - /* append root + '\\' + filename as wchar_t */ - memcpy(file_utf16, root->path, root->len * sizeof(wchar_t)); - - if (*filename == '/' || *filename == '\\') - filename++; - - git__utf8_to_16(file_utf16 + root->len - 1, alloc_len, filename); - - /* check access */ - if (_waccess(file_utf16, F_OK) < 0) { - git__free(file_utf16); - return GIT_ENOTFOUND; - } - - git__utf16_to_8(file_utf8, file_utf16); - git_path_mkposix(file_utf8); - git_buf_sets(path, file_utf8); - - git__free(file_utf16); - return 0; -} - -wchar_t* win32_nextpath(wchar_t *path, wchar_t *buf, size_t buflen) -{ - wchar_t term, *base = path; - - assert(path && buf && buflen); - - term = (*path == L'"') ? *path++ : L';'; - - for (buflen--; *path && *path != term && buflen; buflen--) - *buf++ = *path++; - - *buf = L'\0'; /* reserved a byte via initial subtract */ - - while (*path == term || *path == L';') - path++; - - return (path != base) ? path : NULL; -} - -int win32_find_system_file_using_path(git_buf *path, const char *filename) -{ - wchar_t * env = NULL; - struct win32_path root; - - env = _wgetenv(L"PATH"); - if (!env) - return -1; - - // search in all paths defined in PATH - while ((env = win32_nextpath(env, root.path, MAX_PATH - 1)) != NULL && *root.path) - { - wchar_t * pfin = root.path + wcslen(root.path) - 1; // last char of the current path entry - - // ensure trailing slash - if (*pfin != L'/' && *pfin != L'\\') - wcscpy(++pfin, L"\\"); // we have enough space left, MAX_PATH - 1 is used in nextpath above - - root.len = (DWORD)wcslen(root.path) + 1; - - if (win32_find_file(path, &root, "git.cmd") == 0 || win32_find_file(path, &root, "git.exe") == 0) { - // we found the cmd or bin directory of a git installaton - if (root.len > 5) { - wcscpy(root.path + wcslen(root.path) - 4, L"etc\\"); - if (win32_find_file(path, &root, filename) == 0) - return 0; - } - } - } - - return GIT_ENOTFOUND; -} - -int win32_find_system_file_using_registry(git_buf *path, const char *filename) -{ - struct win32_path root; - - if (win32_find_msysgit_in_registry(&root, HKEY_CURRENT_USER, REG_MSYSGIT_INSTALL_LOCAL)) { - if (win32_find_msysgit_in_registry(&root, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL)) { - giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory"); - return -1; - } - } - - if (win32_find_file(path, &root, filename) < 0) { - giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename); - git_buf_clear(path); - return GIT_ENOTFOUND; - } - - return 0; -} - -int win32_find_msysgit_in_registry(struct win32_path *root, const HKEY hieve, const wchar_t *key) -{ - HKEY hKey; - DWORD dwType = REG_SZ; - DWORD dwSize = MAX_PATH; - - assert(root); - - root->len = 0; - if (RegOpenKeyExW(hieve, key, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) { - if (RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType, (LPBYTE)&root->path, &dwSize) == ERROR_SUCCESS) { - // InstallLocation points to the root of the msysgit directory - if (dwSize + 4 > MAX_PATH) {// 4 = wcslen(L"etc\\") - giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory - path too long"); - return -1; - } - wcscat(root->path, L"etc\\"); - root->len = (DWORD)wcslen(root->path) + 1; - } - } - RegCloseKey(hKey); - - return root->len ? 0 : GIT_ENOTFOUND; -} diff --git a/src/win32/findfile.h b/src/win32/findfile.h deleted file mode 100644 index 47fe71596ce..00000000000 --- a/src/win32/findfile.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#ifndef INCLUDE_git_findfile_h__ -#define INCLUDE_git_findfile_h__ - -struct win32_path { - wchar_t path[MAX_PATH]; - DWORD len; -}; - -int win32_expand_path(struct win32_path *s_root, const wchar_t *templ); - -int win32_find_file(git_buf *path, const struct win32_path *root, const char *filename); -int win32_find_system_file_using_path(git_buf *path, const char *filename); -int win32_find_system_file_using_registry(git_buf *path, const char *filename); -int win32_find_msysgit_in_registry(struct win32_path *root, const HKEY hieve, const wchar_t *key); - -#endif - diff --git a/src/win32/git2.rc b/src/win32/git2.rc deleted file mode 100644 index b9ff9b081ef..00000000000 --- a/src/win32/git2.rc +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include "../../include/git2/version.h" - -#ifndef INCLUDE_LIB -#define LIBGIT2_FILENAME "git2.dll" -#else -#define LIBGIT2_FILENAME "libgit2.dll" -#endif - -VS_VERSION_INFO VERSIONINFO MOVEABLE IMPURE LOADONCALL DISCARDABLE - FILEVERSION LIBGIT2_VER_MAJOR,LIBGIT2_VER_MINOR,LIBGIT2_VER_REVISION,0 - PRODUCTVERSION LIBGIT2_VER_MAJOR,LIBGIT2_VER_MINOR,LIBGIT2_VER_REVISION,0 - FILEFLAGSMASK VS_FFI_FILEFLAGSMASK -#ifdef _DEBUG - FILEFLAGS 1 -#else - FILEFLAGS 0 -#endif - FILEOS VOS__WINDOWS32 - FILETYPE VFT_DLL - FILESUBTYPE 0 // not used -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "040904E4" - //language ID = U.S. English, char set = Windows, Multilingual - BEGIN - VALUE "FileDescription", "libgit2 - the Git linkable library\0" - VALUE "FileVersion", LIBGIT2_VERSION "\0" - VALUE "InternalName", LIBGIT2_FILENAME "\0" - VALUE "LegalCopyright", "Copyright (C) the libgit2 contributors. All rights reserved.\0" - VALUE "OriginalFilename", LIBGIT2_FILENAME "\0" - VALUE "ProductName", "libgit2\0" - VALUE "ProductVersion", LIBGIT2_VERSION "\0" - VALUE "Comments", "For more information visit http://libgit2.github.com/\0" - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Translation", 0x0409, 1252 - END -END diff --git a/src/win32/map.c b/src/win32/map.c deleted file mode 100644 index 44c6c4e2eab..00000000000 --- a/src/win32/map.c +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "map.h" -#include - - -static DWORD get_page_size(void) -{ - static DWORD page_size; - SYSTEM_INFO sys; - - if (!page_size) { - GetSystemInfo(&sys); - page_size = sys.dwAllocationGranularity; - } - - return page_size; -} - -int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset) -{ - HANDLE fh = (HANDLE)_get_osfhandle(fd); - DWORD page_size = get_page_size(); - DWORD fmap_prot = 0; - DWORD view_prot = 0; - DWORD off_low = 0; - DWORD off_hi = 0; - git_off_t page_start; - git_off_t page_offset; - - GIT_MMAP_VALIDATE(out, len, prot, flags); - - out->data = NULL; - out->len = 0; - out->fmh = NULL; - - if (fh == INVALID_HANDLE_VALUE) { - errno = EBADF; - giterr_set(GITERR_OS, "Failed to mmap. Invalid handle value"); - return -1; - } - - if (prot & GIT_PROT_WRITE) - fmap_prot |= PAGE_READWRITE; - else if (prot & GIT_PROT_READ) - fmap_prot |= PAGE_READONLY; - - if (prot & GIT_PROT_WRITE) - view_prot |= FILE_MAP_WRITE; - if (prot & GIT_PROT_READ) - view_prot |= FILE_MAP_READ; - - page_start = (offset / page_size) * page_size; - page_offset = offset - page_start; - - if (page_offset != 0) { /* offset must be multiple of page size */ - errno = EINVAL; - giterr_set(GITERR_OS, "Failed to mmap. Offset must be multiple of page size"); - return -1; - } - - out->fmh = CreateFileMapping(fh, NULL, fmap_prot, 0, 0, NULL); - if (!out->fmh || out->fmh == INVALID_HANDLE_VALUE) { - giterr_set(GITERR_OS, "Failed to mmap. Invalid handle value"); - out->fmh = NULL; - return -1; - } - - assert(sizeof(git_off_t) == 8); - - off_low = (DWORD)(page_start); - off_hi = (DWORD)(page_start >> 32); - out->data = MapViewOfFile(out->fmh, view_prot, off_hi, off_low, len); - if (!out->data) { - giterr_set(GITERR_OS, "Failed to mmap. No data written"); - CloseHandle(out->fmh); - out->fmh = NULL; - return -1; - } - out->len = len; - - return 0; -} - -int p_munmap(git_map *map) -{ - int error = 0; - - assert(map != NULL); - - if (map->data) { - if (!UnmapViewOfFile(map->data)) { - giterr_set(GITERR_OS, "Failed to munmap. Could not unmap view of file"); - error = -1; - } - map->data = NULL; - } - - if (map->fmh) { - if (!CloseHandle(map->fmh)) { - giterr_set(GITERR_OS, "Failed to munmap. Could not close handle"); - error = -1; - } - map->fmh = NULL; - } - - return error; -} - - diff --git a/src/win32/mingw-compat.h b/src/win32/mingw-compat.h deleted file mode 100644 index 7b97b48db08..00000000000 --- a/src/win32/mingw-compat.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_mingw_compat__ -#define INCLUDE_mingw_compat__ - -#if defined(__MINGW32__) - -/* use a 64-bit file offset type */ -# define lseek _lseeki64 -# define stat _stati64 -# define fstat _fstati64 - -/* stat: file mode type testing macros */ -# define _S_IFLNK 0120000 -# define S_IFLNK _S_IFLNK -# define S_ISLNK(m) (((m) & _S_IFMT) == _S_IFLNK) - -#endif - -#endif /* INCLUDE_mingw_compat__ */ diff --git a/src/win32/msvc-compat.h b/src/win32/msvc-compat.h deleted file mode 100644 index 714a85e21e6..00000000000 --- a/src/win32/msvc-compat.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_msvc_compat__ -#define INCLUDE_msvc_compat__ - -#if defined(_MSC_VER) - -/* access() mode parameter #defines */ -# define F_OK 0 /* existence check */ -# define W_OK 2 /* write mode check */ -# define R_OK 4 /* read mode check */ - -# define lseek _lseeki64 -# define stat _stat64 -# define fstat _fstat64 - -/* stat: file mode type testing macros */ -# define _S_IFLNK 0120000 -# define S_IFLNK _S_IFLNK -# define S_IXUSR 00100 - -# define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR) -# define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG) -# define S_ISFIFO(m) (((m) & _S_IFMT) == _S_IFIFO) -# define S_ISLNK(m) (((m) & _S_IFMT) == _S_IFLNK) - -# define mode_t unsigned short - -/* case-insensitive string comparison */ -# define strcasecmp _stricmp -# define strncasecmp _strnicmp - -/* MSVC doesn't define ssize_t at all */ -typedef SSIZE_T ssize_t; - -#endif - -#endif /* INCLUDE_msvc_compat__ */ diff --git a/src/win32/posix.h b/src/win32/posix.h deleted file mode 100644 index c49c2175c83..00000000000 --- a/src/win32/posix.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_posix__w32_h__ -#define INCLUDE_posix__w32_h__ - -#include "common.h" -#include "utf-conv.h" - -GIT_INLINE(int) p_link(const char *old, const char *new) -{ - GIT_UNUSED(old); - GIT_UNUSED(new); - errno = ENOSYS; - return -1; -} - -GIT_INLINE(int) p_mkdir(const char *path, mode_t mode) -{ - wchar_t buf[GIT_WIN_PATH]; - GIT_UNUSED(mode); - git__utf8_to_16(buf, GIT_WIN_PATH, path); - return _wmkdir(buf); -} - -extern int p_unlink(const char *path); -extern int p_lstat(const char *file_name, struct stat *buf); -extern int p_readlink(const char *link, char *target, size_t target_len); -extern int p_symlink(const char *old, const char *new); -extern int p_hide_directory__w32(const char *path); -extern char *p_realpath(const char *orig_path, char *buffer); -extern int p_vsnprintf(char *buffer, size_t count, const char *format, va_list argptr); -extern int p_snprintf(char *buffer, size_t count, const char *format, ...) GIT_FORMAT_PRINTF(3, 4); -extern int p_mkstemp(char *tmp_path); -extern int p_setenv(const char* name, const char* value, int overwrite); -extern int p_stat(const char* path, struct stat* buf); -extern int p_chdir(const char* path); -extern int p_chmod(const char* path, mode_t mode); -extern int p_rmdir(const char* path); -extern int p_access(const char* path, mode_t mode); -extern int p_fsync(int fd); -extern int p_open(const char *path, int flags, ...); -extern int p_creat(const char *path, mode_t mode); -extern int p_getcwd(char *buffer_out, size_t size); -extern int p_rename(const char *from, const char *to); -extern int p_recv(GIT_SOCKET socket, void *buffer, size_t length, int flags); -extern int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags); -extern int p_inet_pton(int af, const char* src, void* dst); - -/* p_lstat is almost but not quite POSIX correct. Specifically, the use of - * ENOTDIR is wrong, in that it does not mean precisely that a non-directory - * entry was encountered. Making it correct is potentially expensive, - * however, so this is a separate version of p_lstat to use when correct - * POSIX ENOTDIR semantics is required. - */ -extern int p_lstat_posixly(const char *filename, struct stat *buf); - -#endif diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c deleted file mode 100644 index 0c23e2959bd..00000000000 --- a/src/win32/posix_w32.c +++ /dev/null @@ -1,556 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#include "../posix.h" -#include "path.h" -#include "utf-conv.h" -#include "repository.h" -#include -#include -#include -#include - -int p_unlink(const char *path) -{ - wchar_t buf[GIT_WIN_PATH]; - git__utf8_to_16(buf, GIT_WIN_PATH, path); - _wchmod(buf, 0666); - return _wunlink(buf); -} - -int p_fsync(int fd) -{ - HANDLE fh = (HANDLE)_get_osfhandle(fd); - - if (fh == INVALID_HANDLE_VALUE) { - errno = EBADF; - return -1; - } - - if (!FlushFileBuffers(fh)) { - DWORD code = GetLastError(); - - if (code == ERROR_INVALID_HANDLE) - errno = EINVAL; - else - errno = EIO; - - return -1; - } - - return 0; -} - -GIT_INLINE(time_t) filetime_to_time_t(const FILETIME *ft) -{ - long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime; - winTime -= 116444736000000000LL; /* Windows to Unix Epoch conversion */ - winTime /= 10000000; /* Nano to seconds resolution */ - return (time_t)winTime; -} - -#define WIN32_IS_WSEP(CH) ((CH) == L'/' || (CH) == L'\\') - -static int do_lstat( - const char *file_name, struct stat *buf, int posix_enotdir) -{ - WIN32_FILE_ATTRIBUTE_DATA fdata; - wchar_t fbuf[GIT_WIN_PATH], lastch; - int flen; - - flen = git__utf8_to_16(fbuf, GIT_WIN_PATH, file_name); - - /* truncate trailing slashes */ - for (; flen > 0; --flen) { - lastch = fbuf[flen - 1]; - if (WIN32_IS_WSEP(lastch)) - fbuf[flen - 1] = L'\0'; - else if (lastch != L'\0') - break; - } - - if (GetFileAttributesExW(fbuf, GetFileExInfoStandard, &fdata)) { - int fMode = S_IREAD; - - if (!buf) - return 0; - - if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - fMode |= S_IFDIR; - else - fMode |= S_IFREG; - - if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) - fMode |= S_IWRITE; - - if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) - fMode |= S_IFLNK; - - buf->st_ino = 0; - buf->st_gid = 0; - buf->st_uid = 0; - buf->st_nlink = 1; - buf->st_mode = (mode_t)fMode; - buf->st_size = ((git_off_t)fdata.nFileSizeHigh << 32) + fdata.nFileSizeLow; - buf->st_dev = buf->st_rdev = (_getdrive() - 1); - buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); - buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); - buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime)); - - return 0; - } - - errno = ENOENT; - - /* We need POSIX behavior, then ENOTDIR must set when any of the folders in the - * file path is a regular file,otherwise ENOENT must be set. - */ - if (posix_enotdir) { - /* scan up path until we find an existing item */ - while (1) { - /* remove last directory component */ - for (--flen; flen > 0 && !WIN32_IS_WSEP(fbuf[flen]); --flen); - - if (flen <= 0) - break; - - fbuf[flen] = L'\0'; - - if (GetFileAttributesExW(fbuf, GetFileExInfoStandard, &fdata)) { - if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) - errno = ENOTDIR; - break; - } - } - } - - return -1; -} - -int p_lstat(const char *filename, struct stat *buf) -{ - return do_lstat(filename, buf, 0); -} - -int p_lstat_posixly(const char *filename, struct stat *buf) -{ - return do_lstat(filename, buf, 1); -} - -int p_readlink(const char *link, char *target, size_t target_len) -{ - typedef DWORD (WINAPI *fpath_func)(HANDLE, LPWSTR, DWORD, DWORD); - static fpath_func pGetFinalPath = NULL; - HANDLE hFile; - DWORD dwRet; - wchar_t link_w[GIT_WIN_PATH]; - wchar_t* target_w; - int error = 0; - - assert(link && target && target_len > 0); - - /* - * Try to load the pointer to pGetFinalPath dynamically, because - * it is not available in platforms older than Vista - */ - if (pGetFinalPath == NULL) { - HINSTANCE library = LoadLibrary("kernel32"); - - if (library != NULL) - pGetFinalPath = (fpath_func)GetProcAddress(library, "GetFinalPathNameByHandleW"); - - if (pGetFinalPath == NULL) { - giterr_set(GITERR_OS, - "'GetFinalPathNameByHandleW' is not available in this platform"); - return -1; - } - } - - git__utf8_to_16(link_w, GIT_WIN_PATH, link); - - hFile = CreateFileW(link_w, // file to open - GENERIC_READ, // open for reading - FILE_SHARE_READ, // share for reading - NULL, // default security - OPEN_EXISTING, // existing file only - FILE_FLAG_BACKUP_SEMANTICS, // normal file - NULL); // no attr. template - - if (hFile == INVALID_HANDLE_VALUE) { - giterr_set(GITERR_OS, "Cannot open '%s' for reading", link); - return -1; - } - - target_w = (wchar_t*)git__malloc(target_len * sizeof(wchar_t)); - GITERR_CHECK_ALLOC(target_w); - - dwRet = pGetFinalPath(hFile, target_w, (DWORD)target_len, 0x0); - if (dwRet == 0 || - dwRet >= target_len || - !WideCharToMultiByte(CP_UTF8, 0, target_w, -1, target, - (int)(target_len * sizeof(char)), NULL, NULL)) - error = -1; - - git__free(target_w); - CloseHandle(hFile); - - if (error) - return error; - - /* Skip first 4 characters if they are "\\?\" */ - if (dwRet > 4 && - target[0] == '\\' && target[1] == '\\' && - target[2] == '?' && target[3] == '\\') - { - unsigned int offset = 4; - dwRet -= 4; - - /* \??\UNC\ */ - if (dwRet > 7 && - target[4] == 'U' && target[5] == 'N' && target[6] == 'C') - { - offset += 2; - dwRet -= 2; - target[offset] = '\\'; - } - - memmove(target, target + offset, dwRet); - } - - target[dwRet] = '\0'; - - return dwRet; -} - -int p_symlink(const char *old, const char *new) -{ - /* Real symlinks on NTFS require admin privileges. Until this changes, - * libgit2 just creates a text file with the link target in the contents. - */ - return git_futils_fake_symlink(old, new); -} - -int p_open(const char *path, int flags, ...) -{ - wchar_t buf[GIT_WIN_PATH]; - mode_t mode = 0; - - git__utf8_to_16(buf, GIT_WIN_PATH, path); - - if (flags & O_CREAT) { - va_list arg_list; - - va_start(arg_list, flags); - mode = (mode_t)va_arg(arg_list, int); - va_end(arg_list); - } - - return _wopen(buf, flags | _O_BINARY, mode); -} - -int p_creat(const char *path, mode_t mode) -{ - wchar_t buf[GIT_WIN_PATH]; - git__utf8_to_16(buf, GIT_WIN_PATH, path); - return _wopen(buf, _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY, mode); -} - -int p_getcwd(char *buffer_out, size_t size) -{ - int ret; - wchar_t *buf; - - if ((size_t)((int)size) != size) - return -1; - - buf = (wchar_t*)git__malloc(sizeof(wchar_t) * (int)size); - GITERR_CHECK_ALLOC(buf); - - _wgetcwd(buf, (int)size); - - ret = WideCharToMultiByte( - CP_UTF8, 0, buf, -1, buffer_out, (int)size, NULL, NULL); - - git__free(buf); - return !ret ? -1 : 0; -} - -int p_stat(const char* path, struct stat* buf) -{ - return do_lstat(path, buf, 0); -} - -int p_chdir(const char* path) -{ - wchar_t buf[GIT_WIN_PATH]; - git__utf8_to_16(buf, GIT_WIN_PATH, path); - return _wchdir(buf); -} - -int p_chmod(const char* path, mode_t mode) -{ - wchar_t buf[GIT_WIN_PATH]; - git__utf8_to_16(buf, GIT_WIN_PATH, path); - return _wchmod(buf, mode); -} - -int p_rmdir(const char* path) -{ - wchar_t buf[GIT_WIN_PATH]; - git__utf8_to_16(buf, GIT_WIN_PATH, path); - return _wrmdir(buf); -} - -int p_hide_directory__w32(const char *path) -{ - wchar_t buf[GIT_WIN_PATH]; - git__utf8_to_16(buf, GIT_WIN_PATH, path); - return (SetFileAttributesW(buf, FILE_ATTRIBUTE_HIDDEN) != 0) ? 0 : -1; -} - -char *p_realpath(const char *orig_path, char *buffer) -{ - int ret; - wchar_t orig_path_w[GIT_WIN_PATH]; - wchar_t buffer_w[GIT_WIN_PATH]; - - git__utf8_to_16(orig_path_w, GIT_WIN_PATH, orig_path); - - /* Implicitly use GetCurrentDirectory which can be a threading issue */ - ret = GetFullPathNameW(orig_path_w, GIT_WIN_PATH, buffer_w, NULL); - - /* According to MSDN, a return value equals to zero means a failure. */ - if (ret == 0 || ret > GIT_WIN_PATH) - buffer = NULL; - - else if (GetFileAttributesW(buffer_w) == INVALID_FILE_ATTRIBUTES) { - buffer = NULL; - errno = ENOENT; - } - - else if (buffer == NULL) { - int buffer_sz = WideCharToMultiByte( - CP_UTF8, 0, buffer_w, -1, NULL, 0, NULL, NULL); - - if (!buffer_sz || - !(buffer = (char *)git__malloc(buffer_sz)) || - !WideCharToMultiByte( - CP_UTF8, 0, buffer_w, -1, buffer, buffer_sz, NULL, NULL)) - { - git__free(buffer); - buffer = NULL; - } - } - - else if (!WideCharToMultiByte( - CP_UTF8, 0, buffer_w, -1, buffer, GIT_PATH_MAX, NULL, NULL)) - buffer = NULL; - - if (buffer) - git_path_mkposix(buffer); - - return buffer; -} - -int p_vsnprintf(char *buffer, size_t count, const char *format, va_list argptr) -{ -#ifdef _MSC_VER - int len; - - if (count == 0 || (len = _vsnprintf(buffer, count, format, argptr)) < 0) - return _vscprintf(format, argptr); - - return len; -#else /* MinGW */ - return vsnprintf(buffer, count, format, argptr); -#endif -} - -int p_snprintf(char *buffer, size_t count, const char *format, ...) -{ - va_list va; - int r; - - va_start(va, format); - r = p_vsnprintf(buffer, count, format, va); - va_end(va); - - return r; -} - -extern int p_creat(const char *path, mode_t mode); - -int p_mkstemp(char *tmp_path) -{ -#if defined(_MSC_VER) - if (_mktemp_s(tmp_path, strlen(tmp_path) + 1) != 0) - return -1; -#else - if (_mktemp(tmp_path) == NULL) - return -1; -#endif - - return p_creat(tmp_path, 0744); //-V536 -} - -int p_setenv(const char* name, const char* value, int overwrite) -{ - if (overwrite != 1) - return -1; - - return (SetEnvironmentVariableA(name, value) == 0 ? -1 : 0); -} - -int p_access(const char* path, mode_t mode) -{ - wchar_t buf[GIT_WIN_PATH]; - git__utf8_to_16(buf, GIT_WIN_PATH, path); - return _waccess(buf, mode); -} - -int p_rename(const char *from, const char *to) -{ - wchar_t wfrom[GIT_WIN_PATH]; - wchar_t wto[GIT_WIN_PATH]; - - git__utf8_to_16(wfrom, GIT_WIN_PATH, from); - git__utf8_to_16(wto, GIT_WIN_PATH, to); - return MoveFileExW(wfrom, wto, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) ? 0 : -1; -} - -int p_recv(GIT_SOCKET socket, void *buffer, size_t length, int flags) -{ - if ((size_t)((int)length) != length) - return -1; /* giterr_set will be done by caller */ - - return recv(socket, buffer, (int)length, flags); -} - -int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags) -{ - if ((size_t)((int)length) != length) - return -1; /* giterr_set will be done by caller */ - - return send(socket, buffer, (int)length, flags); -} - -/** - * Borrowed from http://old.nabble.com/Porting-localtime_r-and-gmtime_r-td15282276.html - * On Win32, `gmtime_r` doesn't exist but `gmtime` is threadsafe, so we can use that - */ -struct tm * -p_localtime_r (const time_t *timer, struct tm *result) -{ - struct tm *local_result; - local_result = localtime (timer); - - if (local_result == NULL || result == NULL) - return NULL; - - memcpy (result, local_result, sizeof (struct tm)); - return result; -} -struct tm * -p_gmtime_r (const time_t *timer, struct tm *result) -{ - struct tm *local_result; - local_result = gmtime (timer); - - if (local_result == NULL || result == NULL) - return NULL; - - memcpy (result, local_result, sizeof (struct tm)); - return result; -} - -#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) -#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 -#else -#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL -#endif - -struct timezone -{ - int tz_minuteswest; /* minutes W of Greenwich */ - int tz_dsttime; /* type of dst correction */ -}; - -int p_gettimeofday(struct timeval *tv, struct timezone *tz) -{ - FILETIME ft; - unsigned __int64 tmpres = 0; - static int tzflag; - - if (NULL != tv) - { - GetSystemTimeAsFileTime(&ft); - - tmpres |= ft.dwHighDateTime; - tmpres <<= 32; - tmpres |= ft.dwLowDateTime; - - /*converting file time to unix epoch*/ - tmpres /= 10; /*convert into microseconds*/ - tmpres -= DELTA_EPOCH_IN_MICROSECS; - tv->tv_sec = (long)(tmpres / 1000000UL); - tv->tv_usec = (long)(tmpres % 1000000UL); - } - - if (NULL != tz) - { - if (!tzflag) - { - _tzset(); - tzflag++; - } - tz->tz_minuteswest = _timezone / 60; - tz->tz_dsttime = _daylight; - } - - return 0; -} - -int p_inet_pton(int af, const char* src, void* dst) -{ - union { - struct sockaddr_in6 sin6; - struct sockaddr_in sin; - } sa; - int srcsize; - - switch(af) - { - case AF_INET: - sa.sin.sin_family = AF_INET; - srcsize = (int)sizeof(sa.sin); - break; - case AF_INET6: - sa.sin6.sin6_family = AF_INET6; - srcsize = (int)sizeof(sa.sin6); - break; - default: - errno = WSAEPFNOSUPPORT; - return -1; - } - - if (WSAStringToAddress((LPSTR)src, af, NULL, (struct sockaddr *) &sa, &srcsize) != 0) - { - errno = WSAGetLastError(); - return -1; - } - - switch(af) - { - case AF_INET: - memcpy(dst, &sa.sin.sin_addr, sizeof(sa.sin.sin_addr)); - break; - case AF_INET6: - memcpy(dst, &sa.sin6.sin6_addr, sizeof(sa.sin6.sin6_addr)); - break; - } - - return 1; -} diff --git a/src/win32/precompiled.h b/src/win32/precompiled.h deleted file mode 100644 index 5de7e6f34a4..00000000000 --- a/src/win32/precompiled.h +++ /dev/null @@ -1,19 +0,0 @@ -#include "git2.h" - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include -#include -#ifdef GIT_THREADS - #include "win32/pthread.h" -#endif diff --git a/src/win32/pthread.c b/src/win32/pthread.c deleted file mode 100644 index 105f4b89e33..00000000000 --- a/src/win32/pthread.c +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "pthread.h" - -int pthread_create( - pthread_t *GIT_RESTRICT thread, - const pthread_attr_t *GIT_RESTRICT attr, - void *(*start_routine)(void*), - void *GIT_RESTRICT arg) -{ - GIT_UNUSED(attr); - *thread = (pthread_t) CreateThread( - NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, arg, 0, NULL); - return *thread ? 0 : -1; -} - -int pthread_join(pthread_t thread, void **value_ptr) -{ - int ret; - ret = WaitForSingleObject(thread, INFINITE); - if (ret && value_ptr) - GetExitCodeThread(thread, (void*) value_ptr); - return -(!!ret); -} - -int pthread_mutex_init(pthread_mutex_t *GIT_RESTRICT mutex, - const pthread_mutexattr_t *GIT_RESTRICT mutexattr) -{ - GIT_UNUSED(mutexattr); - InitializeCriticalSection(mutex); - return 0; -} - -int pthread_mutex_destroy(pthread_mutex_t *mutex) -{ - DeleteCriticalSection(mutex); - return 0; -} - -int pthread_mutex_lock(pthread_mutex_t *mutex) -{ - EnterCriticalSection(mutex); - return 0; -} - -int pthread_mutex_unlock(pthread_mutex_t *mutex) -{ - LeaveCriticalSection(mutex); - return 0; -} - -int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) -{ - /* We don't support non-default attributes. */ - if (attr) - return EINVAL; - - /* This is an auto-reset event. */ - *cond = CreateEventW(NULL, FALSE, FALSE, NULL); - assert(*cond); - - /* If we can't create the event, claim that the reason was out-of-memory. - * The actual reason can be fetched with GetLastError(). */ - return *cond ? 0 : ENOMEM; -} - -int pthread_cond_destroy(pthread_cond_t *cond) -{ - BOOL closed; - - if (!cond) - return EINVAL; - - closed = CloseHandle(*cond); - assert(closed); - GIT_UNUSED(closed); - - *cond = NULL; - return 0; -} - -int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) -{ - int error; - DWORD wait_result; - - if (!cond || !mutex) - return EINVAL; - - /* The caller must be holding the mutex. */ - error = pthread_mutex_unlock(mutex); - - if (error) - return error; - - wait_result = WaitForSingleObject(*cond, INFINITE); - assert(WAIT_OBJECT_0 == wait_result); - GIT_UNUSED(wait_result); - - return pthread_mutex_lock(mutex); -} - -int pthread_cond_signal(pthread_cond_t *cond) -{ - BOOL signaled; - - if (!cond) - return EINVAL; - - signaled = SetEvent(*cond); - assert(signaled); - GIT_UNUSED(signaled); - - return 0; -} - -/* pthread_cond_broadcast is not implemented because doing so with just Win32 events - * is quite complicated, and no caller in libgit2 uses it yet. */ - -int pthread_num_processors_np(void) -{ - DWORD_PTR p, s; - int n = 0; - - if (GetProcessAffinityMask(GetCurrentProcess(), &p, &s)) - for (; p; p >>= 1) - n += p&1; - - return n ? n : 1; -} - diff --git a/src/win32/pthread.h b/src/win32/pthread.h deleted file mode 100644 index a219a013701..00000000000 --- a/src/win32/pthread.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#ifndef GIT_PTHREAD_H -#define GIT_PTHREAD_H - -#include "../common.h" - -#if defined (_MSC_VER) -# define GIT_RESTRICT __restrict -#else -# define GIT_RESTRICT __restrict__ -#endif - -typedef int pthread_mutexattr_t; -typedef int pthread_condattr_t; -typedef int pthread_attr_t; -typedef CRITICAL_SECTION pthread_mutex_t; -typedef HANDLE pthread_t; -typedef HANDLE pthread_cond_t; - -#define PTHREAD_MUTEX_INITIALIZER {(void*)-1}; - -int pthread_create(pthread_t *GIT_RESTRICT, - const pthread_attr_t *GIT_RESTRICT, - void *(*start_routine)(void*), void *__restrict); - -int pthread_join(pthread_t, void **); - -int pthread_mutex_init(pthread_mutex_t *GIT_RESTRICT, const pthread_mutexattr_t *GIT_RESTRICT); -int pthread_mutex_destroy(pthread_mutex_t *); -int pthread_mutex_lock(pthread_mutex_t *); -int pthread_mutex_unlock(pthread_mutex_t *); - -int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *); -int pthread_cond_destroy(pthread_cond_t *); -int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *); -int pthread_cond_signal(pthread_cond_t *); -/* pthread_cond_broadcast is not supported on Win32 yet. */ - -int pthread_num_processors_np(void); - -#endif diff --git a/src/win32/utf-conv.c b/src/win32/utf-conv.c deleted file mode 100644 index c06f3a8c25a..00000000000 --- a/src/win32/utf-conv.c +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include "common.h" -#include "utf-conv.h" - -#define U16_LEAD(c) (wchar_t)(((c)>>10)+0xd7c0) -#define U16_TRAIL(c) (wchar_t)(((c)&0x3ff)|0xdc00) - -#if 0 -void git__utf8_to_16(wchar_t *dest, size_t length, const char *src) -{ - wchar_t *pDest = dest; - uint32_t ch; - const uint8_t* pSrc = (uint8_t*) src; - - assert(dest && src && length); - - length--; - - while(*pSrc && length > 0) { - ch = *pSrc++; - length--; - - if(ch < 0xc0) { - /* - * ASCII, or a trail byte in lead position which is treated like - * a single-byte sequence for better character boundary - * resynchronization after illegal sequences. - */ - *pDest++ = (wchar_t)ch; - continue; - } else if(ch < 0xe0) { /* U+0080..U+07FF */ - if (pSrc[0]) { - /* 0x3080 = (0xc0 << 6) + 0x80 */ - *pDest++ = (wchar_t)((ch << 6) + *pSrc++ - 0x3080); - continue; - } - } else if(ch < 0xf0) { /* U+0800..U+FFFF */ - if (pSrc[0] && pSrc[1]) { - /* no need for (ch & 0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ - /* 0x2080 = (0x80 << 6) + 0x80 */ - ch = (ch << 12) + (*pSrc++ << 6); - *pDest++ = (wchar_t)(ch + *pSrc++ - 0x2080); - continue; - } - } else /* f0..f4 */ { /* U+10000..U+10FFFF */ - if (length >= 1 && pSrc[0] && pSrc[1] && pSrc[2]) { - /* 0x3c82080 = (0xf0 << 18) + (0x80 << 12) + (0x80 << 6) + 0x80 */ - ch = (ch << 18) + (*pSrc++ << 12); - ch += *pSrc++ << 6; - ch += *pSrc++ - 0x3c82080; - *(pDest++) = U16_LEAD(ch); - *(pDest++) = U16_TRAIL(ch); - length--; /* two bytes for this character */ - continue; - } - } - - /* truncated character at the end */ - *pDest++ = 0xfffd; - break; - } - - *pDest++ = 0x0; -} -#endif - -int git__utf8_to_16(wchar_t *dest, size_t length, const char *src) -{ - return MultiByteToWideChar(CP_UTF8, 0, src, -1, dest, (int)length); -} - -int git__utf16_to_8(char *out, const wchar_t *input) -{ - return WideCharToMultiByte(CP_UTF8, 0, input, -1, out, GIT_WIN_PATH, NULL, NULL); -} diff --git a/src/win32/utf-conv.h b/src/win32/utf-conv.h deleted file mode 100644 index 6cc9205f720..00000000000 --- a/src/win32/utf-conv.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -#include - -#ifndef INCLUDE_git_utfconv_h__ -#define INCLUDE_git_utfconv_h__ - -#define GIT_WIN_PATH (260 + 1) - -int git__utf8_to_16(wchar_t *dest, size_t length, const char *src); -int git__utf16_to_8(char *dest, const wchar_t *src); - -#endif - diff --git a/src/xdiff/xdiff.h b/src/xdiff/xdiff.h deleted file mode 100644 index cb8b235b50f..00000000000 --- a/src/xdiff/xdiff.h +++ /dev/null @@ -1,135 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Davide Libenzi - * - */ - -#if !defined(XDIFF_H) -#define XDIFF_H - -#ifdef __cplusplus -extern "C" { -#endif /* #ifdef __cplusplus */ - - -#define XDF_NEED_MINIMAL (1 << 1) -#define XDF_IGNORE_WHITESPACE (1 << 2) -#define XDF_IGNORE_WHITESPACE_CHANGE (1 << 3) -#define XDF_IGNORE_WHITESPACE_AT_EOL (1 << 4) -#define XDF_PATIENCE_DIFF (1 << 5) -#define XDF_HISTOGRAM_DIFF (1 << 6) -#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE | XDF_IGNORE_WHITESPACE_AT_EOL) - -#define XDL_PATCH_NORMAL '-' -#define XDL_PATCH_REVERSE '+' -#define XDL_PATCH_MODEMASK ((1 << 8) - 1) -#define XDL_PATCH_IGNOREBSPACE (1 << 8) - -#define XDL_EMIT_FUNCNAMES (1 << 0) -#define XDL_EMIT_COMMON (1 << 1) -#define XDL_EMIT_FUNCCONTEXT (1 << 2) - -#define XDL_MMB_READONLY (1 << 0) - -#define XDL_MMF_ATOMIC (1 << 0) - -#define XDL_BDOP_INS 1 -#define XDL_BDOP_CPY 2 -#define XDL_BDOP_INSB 3 - -/* merge simplification levels */ -#define XDL_MERGE_MINIMAL 0 -#define XDL_MERGE_EAGER 1 -#define XDL_MERGE_ZEALOUS 2 -#define XDL_MERGE_ZEALOUS_ALNUM 3 - -/* merge favor modes */ -#define XDL_MERGE_FAVOR_OURS 1 -#define XDL_MERGE_FAVOR_THEIRS 2 -#define XDL_MERGE_FAVOR_UNION 3 - -/* merge output styles */ -#define XDL_MERGE_DIFF3 1 - -typedef struct s_mmfile { - char *ptr; - size_t size; -} mmfile_t; - -typedef struct s_mmbuffer { - char *ptr; - size_t size; -} mmbuffer_t; - -typedef struct s_xpparam { - unsigned long flags; -} xpparam_t; - -typedef struct s_xdemitcb { - void *priv; - int (*outf)(void *, mmbuffer_t *, int); -} xdemitcb_t; - -typedef long (*find_func_t)(const char *line, long line_len, char *buffer, long buffer_size, void *priv); - -typedef struct s_xdemitconf { - long ctxlen; - long interhunkctxlen; - unsigned long flags; - find_func_t find_func; - void *find_func_priv; - void (*emit_func)(void); -} xdemitconf_t; - -typedef struct s_bdiffparam { - long bsize; -} bdiffparam_t; - - -#define xdl_malloc(x) malloc(x) -#define xdl_free(ptr) free(ptr) -#define xdl_realloc(ptr,x) realloc(ptr,x) - -void *xdl_mmfile_first(mmfile_t *mmf, long *size); -long xdl_mmfile_size(mmfile_t *mmf); - -int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, - xdemitconf_t const *xecfg, xdemitcb_t *ecb); - -typedef struct s_xmparam { - xpparam_t xpp; - int marker_size; - int level; - int favor; - int style; - const char *ancestor; /* label for orig */ - const char *file1; /* label for mf1 */ - const char *file2; /* label for mf2 */ -} xmparam_t; - -#define DEFAULT_CONFLICT_MARKER_SIZE 7 - -int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2, - xmparam_t const *xmp, mmbuffer_t *result); - -#ifdef __cplusplus -} -#endif /* #ifdef __cplusplus */ - -#endif /* #if !defined(XDIFF_H) */ diff --git a/src/xdiff/xdiffi.c b/src/xdiff/xdiffi.c deleted file mode 100644 index 75a39227501..00000000000 --- a/src/xdiff/xdiffi.c +++ /dev/null @@ -1,572 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Davide Libenzi - * - */ - -#include "xinclude.h" - - - -#define XDL_MAX_COST_MIN 256 -#define XDL_HEUR_MIN_COST 256 -#define XDL_LINE_MAX (long)((1UL << (CHAR_BIT * sizeof(long) - 1)) - 1) -#define XDL_SNAKE_CNT 20 -#define XDL_K_HEUR 4 - - - -typedef struct s_xdpsplit { - long i1, i2; - int min_lo, min_hi; -} xdpsplit_t; - - - - -static long xdl_split(unsigned long const *ha1, long off1, long lim1, - unsigned long const *ha2, long off2, long lim2, - long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl, - xdalgoenv_t *xenv); -static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2); - - - - - -/* - * See "An O(ND) Difference Algorithm and its Variations", by Eugene Myers. - * Basically considers a "box" (off1, off2, lim1, lim2) and scan from both - * the forward diagonal starting from (off1, off2) and the backward diagonal - * starting from (lim1, lim2). If the K values on the same diagonal crosses - * returns the furthest point of reach. We might end up having to expensive - * cases using this algorithm is full, so a little bit of heuristic is needed - * to cut the search and to return a suboptimal point. - */ -static long xdl_split(unsigned long const *ha1, long off1, long lim1, - unsigned long const *ha2, long off2, long lim2, - long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl, - xdalgoenv_t *xenv) { - long dmin = off1 - lim2, dmax = lim1 - off2; - long fmid = off1 - off2, bmid = lim1 - lim2; - long odd = (fmid - bmid) & 1; - long fmin = fmid, fmax = fmid; - long bmin = bmid, bmax = bmid; - long ec, d, i1, i2, prev1, best, dd, v, k; - - /* - * Set initial diagonal values for both forward and backward path. - */ - kvdf[fmid] = off1; - kvdb[bmid] = lim1; - - for (ec = 1;; ec++) { - int got_snake = 0; - - /* - * We need to extent the diagonal "domain" by one. If the next - * values exits the box boundaries we need to change it in the - * opposite direction because (max - min) must be a power of two. - * Also we initialize the external K value to -1 so that we can - * avoid extra conditions check inside the core loop. - */ - if (fmin > dmin) - kvdf[--fmin - 1] = -1; - else - ++fmin; - if (fmax < dmax) - kvdf[++fmax + 1] = -1; - else - --fmax; - - for (d = fmax; d >= fmin; d -= 2) { - if (kvdf[d - 1] >= kvdf[d + 1]) - i1 = kvdf[d - 1] + 1; - else - i1 = kvdf[d + 1]; - prev1 = i1; - i2 = i1 - d; - for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++); - if (i1 - prev1 > xenv->snake_cnt) - got_snake = 1; - kvdf[d] = i1; - if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) { - spl->i1 = i1; - spl->i2 = i2; - spl->min_lo = spl->min_hi = 1; - return ec; - } - } - - /* - * We need to extent the diagonal "domain" by one. If the next - * values exits the box boundaries we need to change it in the - * opposite direction because (max - min) must be a power of two. - * Also we initialize the external K value to -1 so that we can - * avoid extra conditions check inside the core loop. - */ - if (bmin > dmin) - kvdb[--bmin - 1] = XDL_LINE_MAX; - else - ++bmin; - if (bmax < dmax) - kvdb[++bmax + 1] = XDL_LINE_MAX; - else - --bmax; - - for (d = bmax; d >= bmin; d -= 2) { - if (kvdb[d - 1] < kvdb[d + 1]) - i1 = kvdb[d - 1]; - else - i1 = kvdb[d + 1] - 1; - prev1 = i1; - i2 = i1 - d; - for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--); - if (prev1 - i1 > xenv->snake_cnt) - got_snake = 1; - kvdb[d] = i1; - if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) { - spl->i1 = i1; - spl->i2 = i2; - spl->min_lo = spl->min_hi = 1; - return ec; - } - } - - if (need_min) - continue; - - /* - * If the edit cost is above the heuristic trigger and if - * we got a good snake, we sample current diagonals to see - * if some of the, have reached an "interesting" path. Our - * measure is a function of the distance from the diagonal - * corner (i1 + i2) penalized with the distance from the - * mid diagonal itself. If this value is above the current - * edit cost times a magic factor (XDL_K_HEUR) we consider - * it interesting. - */ - if (got_snake && ec > xenv->heur_min) { - for (best = 0, d = fmax; d >= fmin; d -= 2) { - dd = d > fmid ? d - fmid: fmid - d; - i1 = kvdf[d]; - i2 = i1 - d; - v = (i1 - off1) + (i2 - off2) - dd; - - if (v > XDL_K_HEUR * ec && v > best && - off1 + xenv->snake_cnt <= i1 && i1 < lim1 && - off2 + xenv->snake_cnt <= i2 && i2 < lim2) { - for (k = 1; ha1[i1 - k] == ha2[i2 - k]; k++) - if (k == xenv->snake_cnt) { - best = v; - spl->i1 = i1; - spl->i2 = i2; - break; - } - } - } - if (best > 0) { - spl->min_lo = 1; - spl->min_hi = 0; - return ec; - } - - for (best = 0, d = bmax; d >= bmin; d -= 2) { - dd = d > bmid ? d - bmid: bmid - d; - i1 = kvdb[d]; - i2 = i1 - d; - v = (lim1 - i1) + (lim2 - i2) - dd; - - if (v > XDL_K_HEUR * ec && v > best && - off1 < i1 && i1 <= lim1 - xenv->snake_cnt && - off2 < i2 && i2 <= lim2 - xenv->snake_cnt) { - for (k = 0; ha1[i1 + k] == ha2[i2 + k]; k++) - if (k == xenv->snake_cnt - 1) { - best = v; - spl->i1 = i1; - spl->i2 = i2; - break; - } - } - } - if (best > 0) { - spl->min_lo = 0; - spl->min_hi = 1; - return ec; - } - } - - /* - * Enough is enough. We spent too much time here and now we collect - * the furthest reaching path using the (i1 + i2) measure. - */ - if (ec >= xenv->mxcost) { - long fbest, fbest1, bbest, bbest1; - - fbest = fbest1 = -1; - for (d = fmax; d >= fmin; d -= 2) { - i1 = XDL_MIN(kvdf[d], lim1); - i2 = i1 - d; - if (lim2 < i2) - i1 = lim2 + d, i2 = lim2; - if (fbest < i1 + i2) { - fbest = i1 + i2; - fbest1 = i1; - } - } - - bbest = bbest1 = XDL_LINE_MAX; - for (d = bmax; d >= bmin; d -= 2) { - i1 = XDL_MAX(off1, kvdb[d]); - i2 = i1 - d; - if (i2 < off2) - i1 = off2 + d, i2 = off2; - if (i1 + i2 < bbest) { - bbest = i1 + i2; - bbest1 = i1; - } - } - - if ((lim1 + lim2) - bbest < fbest - (off1 + off2)) { - spl->i1 = fbest1; - spl->i2 = fbest - fbest1; - spl->min_lo = 1; - spl->min_hi = 0; - } else { - spl->i1 = bbest1; - spl->i2 = bbest - bbest1; - spl->min_lo = 0; - spl->min_hi = 1; - } - return ec; - } - } -} - - -/* - * Rule: "Divide et Impera". Recursively split the box in sub-boxes by calling - * the box splitting function. Note that the real job (marking changed lines) - * is done in the two boundary reaching checks. - */ -int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1, - diffdata_t *dd2, long off2, long lim2, - long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv) { - unsigned long const *ha1 = dd1->ha, *ha2 = dd2->ha; - - /* - * Shrink the box by walking through each diagonal snake (SW and NE). - */ - for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++); - for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--); - - /* - * If one dimension is empty, then all records on the other one must - * be obviously changed. - */ - if (off1 == lim1) { - char *rchg2 = dd2->rchg; - long *rindex2 = dd2->rindex; - - for (; off2 < lim2; off2++) - rchg2[rindex2[off2]] = 1; - } else if (off2 == lim2) { - char *rchg1 = dd1->rchg; - long *rindex1 = dd1->rindex; - - for (; off1 < lim1; off1++) - rchg1[rindex1[off1]] = 1; - } else { - xdpsplit_t spl; - spl.i1 = spl.i2 = 0; - - /* - * Divide ... - */ - if (xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb, - need_min, &spl, xenv) < 0) { - - return -1; - } - - /* - * ... et Impera. - */ - if (xdl_recs_cmp(dd1, off1, spl.i1, dd2, off2, spl.i2, - kvdf, kvdb, spl.min_lo, xenv) < 0 || - xdl_recs_cmp(dd1, spl.i1, lim1, dd2, spl.i2, lim2, - kvdf, kvdb, spl.min_hi, xenv) < 0) { - - return -1; - } - } - - return 0; -} - - -int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, - xdfenv_t *xe) { - long ndiags; - long *kvd, *kvdf, *kvdb; - xdalgoenv_t xenv; - diffdata_t dd1, dd2; - - if (xpp->flags & XDF_PATIENCE_DIFF) - return xdl_do_patience_diff(mf1, mf2, xpp, xe); - - if (xpp->flags & XDF_HISTOGRAM_DIFF) - return xdl_do_histogram_diff(mf1, mf2, xpp, xe); - - if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) { - - return -1; - } - - /* - * Allocate and setup K vectors to be used by the differential algorithm. - * One is to store the forward path and one to store the backward path. - */ - ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3; - if (!(kvd = (long *) xdl_malloc((2 * ndiags + 2) * sizeof(long)))) { - - xdl_free_env(xe); - return -1; - } - kvdf = kvd; - kvdb = kvdf + ndiags; - kvdf += xe->xdf2.nreff + 1; - kvdb += xe->xdf2.nreff + 1; - - xenv.mxcost = xdl_bogosqrt(ndiags); - if (xenv.mxcost < XDL_MAX_COST_MIN) - xenv.mxcost = XDL_MAX_COST_MIN; - xenv.snake_cnt = XDL_SNAKE_CNT; - xenv.heur_min = XDL_HEUR_MIN_COST; - - dd1.nrec = xe->xdf1.nreff; - dd1.ha = xe->xdf1.ha; - dd1.rchg = xe->xdf1.rchg; - dd1.rindex = xe->xdf1.rindex; - dd2.nrec = xe->xdf2.nreff; - dd2.ha = xe->xdf2.ha; - dd2.rchg = xe->xdf2.rchg; - dd2.rindex = xe->xdf2.rindex; - - if (xdl_recs_cmp(&dd1, 0, dd1.nrec, &dd2, 0, dd2.nrec, - kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) != 0, &xenv) < 0) { - - xdl_free(kvd); - xdl_free_env(xe); - return -1; - } - - xdl_free(kvd); - - return 0; -} - - -static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2) { - xdchange_t *xch; - - if (!(xch = (xdchange_t *) xdl_malloc(sizeof(xdchange_t)))) - return NULL; - - xch->next = xscr; - xch->i1 = i1; - xch->i2 = i2; - xch->chg1 = chg1; - xch->chg2 = chg2; - - return xch; -} - - -int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) { - long ix, ixo, ixs, ixref, grpsiz, nrec = xdf->nrec; - char *rchg = xdf->rchg, *rchgo = xdfo->rchg; - xrecord_t **recs = xdf->recs; - - /* - * This is the same of what GNU diff does. Move back and forward - * change groups for a consistent and pretty diff output. This also - * helps in finding joinable change groups and reduce the diff size. - */ - for (ix = ixo = 0;;) { - /* - * Find the first changed line in the to-be-compacted file. - * We need to keep track of both indexes, so if we find a - * changed lines group on the other file, while scanning the - * to-be-compacted file, we need to skip it properly. Note - * that loops that are testing for changed lines on rchg* do - * not need index bounding since the array is prepared with - * a zero at position -1 and N. - */ - for (; ix < nrec && !rchg[ix]; ix++) - while (rchgo[ixo++]); - if (ix == nrec) - break; - - /* - * Record the start of a changed-group in the to-be-compacted file - * and find the end of it, on both to-be-compacted and other file - * indexes (ix and ixo). - */ - ixs = ix; - for (ix++; rchg[ix]; ix++); - for (; rchgo[ixo]; ixo++); - - do { - grpsiz = ix - ixs; - - /* - * If the line before the current change group, is equal to - * the last line of the current change group, shift backward - * the group. - */ - while (ixs > 0 && recs[ixs - 1]->ha == recs[ix - 1]->ha && - xdl_recmatch(recs[ixs - 1]->ptr, recs[ixs - 1]->size, recs[ix - 1]->ptr, recs[ix - 1]->size, flags)) { - rchg[--ixs] = 1; - rchg[--ix] = 0; - - /* - * This change might have joined two change groups, - * so we try to take this scenario in account by moving - * the start index accordingly (and so the other-file - * end-of-group index). - */ - for (; rchg[ixs - 1]; ixs--); - while (rchgo[--ixo]); - } - - /* - * Record the end-of-group position in case we are matched - * with a group of changes in the other file (that is, the - * change record before the end-of-group index in the other - * file is set). - */ - ixref = rchgo[ixo - 1] ? ix: nrec; - - /* - * If the first line of the current change group, is equal to - * the line next of the current change group, shift forward - * the group. - */ - while (ix < nrec && recs[ixs]->ha == recs[ix]->ha && - xdl_recmatch(recs[ixs]->ptr, recs[ixs]->size, recs[ix]->ptr, recs[ix]->size, flags)) { - rchg[ixs++] = 0; - rchg[ix++] = 1; - - /* - * This change might have joined two change groups, - * so we try to take this scenario in account by moving - * the start index accordingly (and so the other-file - * end-of-group index). Keep tracking the reference - * index in case we are shifting together with a - * corresponding group of changes in the other file. - */ - for (; rchg[ix]; ix++); - while (rchgo[++ixo]) - ixref = ix; - } - } while (grpsiz != ix - ixs); - - /* - * Try to move back the possibly merged group of changes, to match - * the recorded postion in the other file. - */ - while (ixref < ix) { - rchg[--ixs] = 1; - rchg[--ix] = 0; - while (rchgo[--ixo]); - } - } - - return 0; -} - - -int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr) { - xdchange_t *cscr = NULL, *xch; - char *rchg1 = xe->xdf1.rchg, *rchg2 = xe->xdf2.rchg; - long i1, i2, l1, l2; - - /* - * Trivial. Collects "groups" of changes and creates an edit script. - */ - for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--) - if (rchg1[i1 - 1] || rchg2[i2 - 1]) { - for (l1 = i1; rchg1[i1 - 1]; i1--); - for (l2 = i2; rchg2[i2 - 1]; i2--); - - if (!(xch = xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) { - xdl_free_script(cscr); - return -1; - } - cscr = xch; - } - - *xscr = cscr; - - return 0; -} - - -void xdl_free_script(xdchange_t *xscr) { - xdchange_t *xch; - - while ((xch = xscr) != NULL) { - xscr = xscr->next; - xdl_free(xch); - } -} - - -int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, - xdemitconf_t const *xecfg, xdemitcb_t *ecb) { - xdchange_t *xscr; - xdfenv_t xe; - emit_func_t ef = xecfg->emit_func ? - (emit_func_t)xecfg->emit_func : xdl_emit_diff; - - if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) { - - return -1; - } - if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 || - xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 || - xdl_build_script(&xe, &xscr) < 0) { - - xdl_free_env(&xe); - return -1; - } - if (xscr) { - if (ef(&xe, xscr, ecb, xecfg) < 0) { - - xdl_free_script(xscr); - xdl_free_env(&xe); - return -1; - } - xdl_free_script(xscr); - } - xdl_free_env(&xe); - - return 0; -} diff --git a/src/xdiff/xemit.c b/src/xdiff/xemit.c deleted file mode 100644 index e3e63d90234..00000000000 --- a/src/xdiff/xemit.c +++ /dev/null @@ -1,253 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Davide Libenzi - * - */ - -#include "xinclude.h" - - - - -static long xdl_get_rec(xdfile_t *xdf, long ri, char const **rec); -static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *ecb); - - - - -static long xdl_get_rec(xdfile_t *xdf, long ri, char const **rec) { - - *rec = xdf->recs[ri]->ptr; - - return xdf->recs[ri]->size; -} - - -static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *ecb) { - long size, psize = (long)strlen(pre); - char const *rec; - - size = xdl_get_rec(xdf, ri, &rec); - if (xdl_emit_diffrec(rec, size, pre, psize, ecb) < 0) { - - return -1; - } - - return 0; -} - - -/* - * Starting at the passed change atom, find the latest change atom to be included - * inside the differential hunk according to the specified configuration. - */ -xdchange_t *xdl_get_hunk(xdchange_t *xscr, xdemitconf_t const *xecfg) { - xdchange_t *xch, *xchp; - long max_common = 2 * xecfg->ctxlen + xecfg->interhunkctxlen; - - for (xchp = xscr, xch = xscr->next; xch; xchp = xch, xch = xch->next) - if (xch->i1 - (xchp->i1 + xchp->chg1) > max_common) - break; - - return xchp; -} - - -static long def_ff(const char *rec, long len, char *buf, long sz, void *priv) -{ - (void)priv; - - if (len > 0 && - (isalpha((unsigned char)*rec) || /* identifier? */ - *rec == '_' || /* also identifier? */ - *rec == '$')) { /* identifiers from VMS and other esoterico */ - if (len > sz) - len = sz; - while (0 < len && isspace((unsigned char)rec[len - 1])) - len--; - memcpy(buf, rec, len); - return len; - } - return -1; -} - -static int xdl_emit_common(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, - xdemitconf_t const *xecfg) { - xdfile_t *xdf = &xe->xdf2; - const char *rchg = xdf->rchg; - long ix; - - (void)xscr; - (void)xecfg; - - for (ix = 0; ix < xdf->nrec; ix++) { - if (rchg[ix]) - continue; - if (xdl_emit_record(xdf, ix, "", ecb)) - return -1; - } - return 0; -} - -struct func_line { - long len; - char buf[80]; -}; - -static long get_func_line(xdfenv_t *xe, xdemitconf_t const *xecfg, - struct func_line *func_line, long start, long limit) -{ - find_func_t ff = xecfg->find_func ? xecfg->find_func : def_ff; - long l, size, step = (start > limit) ? -1 : 1; - char *buf, dummy[1]; - - buf = func_line ? func_line->buf : dummy; - size = func_line ? sizeof(func_line->buf) : sizeof(dummy); - - for (l = start; l != limit && 0 <= l && l < xe->xdf1.nrec; l += step) { - const char *rec; - long reclen = xdl_get_rec(&xe->xdf1, l, &rec); - long len = ff(rec, reclen, buf, size, xecfg->find_func_priv); - if (len >= 0) { - if (func_line) - func_line->len = len; - return l; - } - } - return -1; -} - -int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, - xdemitconf_t const *xecfg) { - long s1, s2, e1, e2, lctx; - xdchange_t *xch, *xche; - long funclineprev = -1; - struct func_line func_line = { 0 }; - - if (xecfg->flags & XDL_EMIT_COMMON) - return xdl_emit_common(xe, xscr, ecb, xecfg); - - for (xch = xscr; xch; xch = xche->next) { - xche = xdl_get_hunk(xch, xecfg); - - s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0); - s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0); - - if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) { - long fs1 = get_func_line(xe, xecfg, NULL, xch->i1, -1); - if (fs1 < 0) - fs1 = 0; - if (fs1 < s1) { - s2 -= s1 - fs1; - s1 = fs1; - } - } - - again: - lctx = xecfg->ctxlen; - lctx = XDL_MIN(lctx, xe->xdf1.nrec - (xche->i1 + xche->chg1)); - lctx = XDL_MIN(lctx, xe->xdf2.nrec - (xche->i2 + xche->chg2)); - - e1 = xche->i1 + xche->chg1 + lctx; - e2 = xche->i2 + xche->chg2 + lctx; - - if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) { - long fe1 = get_func_line(xe, xecfg, NULL, - xche->i1 + xche->chg1, - xe->xdf1.nrec); - if (fe1 < 0) - fe1 = xe->xdf1.nrec; - if (fe1 > e1) { - e2 += fe1 - e1; - e1 = fe1; - } - - /* - * Overlap with next change? Then include it - * in the current hunk and start over to find - * its new end. - */ - if (xche->next) { - long l = xche->next->i1; - if (l <= e1 || - get_func_line(xe, xecfg, NULL, l, e1) < 0) { - xche = xche->next; - goto again; - } - } - } - - /* - * Emit current hunk header. - */ - - if (xecfg->flags & XDL_EMIT_FUNCNAMES) { - get_func_line(xe, xecfg, &func_line, - s1 - 1, funclineprev); - funclineprev = s1 - 1; - } - if (xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2, - func_line.buf, func_line.len, ecb) < 0) - return -1; - - /* - * Emit pre-context. - */ - for (; s2 < xch->i2; s2++) - if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) - return -1; - - for (s1 = xch->i1, s2 = xch->i2;; xch = xch->next) { - /* - * Merge previous with current change atom. - */ - for (; s1 < xch->i1 && s2 < xch->i2; s1++, s2++) - if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) - return -1; - - /* - * Removes lines from the first file. - */ - for (s1 = xch->i1; s1 < xch->i1 + xch->chg1; s1++) - if (xdl_emit_record(&xe->xdf1, s1, "-", ecb) < 0) - return -1; - - /* - * Adds lines from the second file. - */ - for (s2 = xch->i2; s2 < xch->i2 + xch->chg2; s2++) - if (xdl_emit_record(&xe->xdf2, s2, "+", ecb) < 0) - return -1; - - if (xch == xche) - break; - s1 = xch->i1 + xch->chg1; - s2 = xch->i2 + xch->chg2; - } - - /* - * Emit post-context. - */ - for (s2 = xche->i2 + xche->chg2; s2 < e2; s2++) - if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) - return -1; - } - - return 0; -} diff --git a/src/xdiff/xhistogram.c b/src/xdiff/xhistogram.c deleted file mode 100644 index 5d101754d50..00000000000 --- a/src/xdiff/xhistogram.c +++ /dev/null @@ -1,371 +0,0 @@ -/* - * Copyright (C) 2010, Google Inc. - * and other copyright owners as documented in JGit's IP log. - * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "xinclude.h" -#include "xtypes.h" -#include "xdiff.h" - -#define MAX_PTR UINT_MAX -#define MAX_CNT UINT_MAX - -#define LINE_END(n) (line##n + count##n - 1) -#define LINE_END_PTR(n) (*line##n + *count##n - 1) - -struct histindex { - struct record { - unsigned int ptr, cnt; - struct record *next; - } **records, /* an ocurrence */ - **line_map; /* map of line to record chain */ - chastore_t rcha; - unsigned int *next_ptrs; - unsigned int table_bits, - records_size, - line_map_size; - - unsigned int max_chain_length, - key_shift, - ptr_shift; - - unsigned int cnt, - has_common; - - xdfenv_t *env; - xpparam_t const *xpp; -}; - -struct region { - unsigned int begin1, end1; - unsigned int begin2, end2; -}; - -#define LINE_MAP(i, a) (i->line_map[(a) - i->ptr_shift]) - -#define NEXT_PTR(index, ptr) \ - (index->next_ptrs[(ptr) - index->ptr_shift]) - -#define CNT(index, ptr) \ - ((LINE_MAP(index, ptr))->cnt) - -#define REC(env, s, l) \ - (env->xdf##s.recs[l - 1]) - -static int cmp_recs(xpparam_t const *xpp, - xrecord_t *r1, xrecord_t *r2) -{ - return r1->ha == r2->ha && - xdl_recmatch(r1->ptr, r1->size, r2->ptr, r2->size, - xpp->flags); -} - -#define CMP_ENV(xpp, env, s1, l1, s2, l2) \ - (cmp_recs(xpp, REC(env, s1, l1), REC(env, s2, l2))) - -#define CMP(i, s1, l1, s2, l2) \ - (cmp_recs(i->xpp, REC(i->env, s1, l1), REC(i->env, s2, l2))) - -#define TABLE_HASH(index, side, line) \ - XDL_HASHLONG((REC(index->env, side, line))->ha, index->table_bits) - -static int scanA(struct histindex *index, unsigned int line1, unsigned int count1) -{ - unsigned int ptr; - unsigned int tbl_idx; - unsigned int chain_len; - struct record **rec_chain, *rec; - - for (ptr = LINE_END(1); line1 <= ptr; ptr--) { - tbl_idx = TABLE_HASH(index, 1, ptr); - rec_chain = index->records + tbl_idx; - rec = *rec_chain; - - chain_len = 0; - while (rec) { - if (CMP(index, 1, rec->ptr, 1, ptr)) { - /* - * ptr is identical to another element. Insert - * it onto the front of the existing element - * chain. - */ - NEXT_PTR(index, ptr) = rec->ptr; - rec->ptr = ptr; - /* cap rec->cnt at MAX_CNT */ - rec->cnt = XDL_MIN(MAX_CNT, rec->cnt + 1); - LINE_MAP(index, ptr) = rec; - goto continue_scan; - } - - rec = rec->next; - chain_len++; - } - - if (chain_len == index->max_chain_length) - return -1; - - /* - * This is the first time we have ever seen this particular - * element in the sequence. Construct a new chain for it. - */ - if (!(rec = xdl_cha_alloc(&index->rcha))) - return -1; - rec->ptr = ptr; - rec->cnt = 1; - rec->next = *rec_chain; - *rec_chain = rec; - LINE_MAP(index, ptr) = rec; - -continue_scan: - ; /* no op */ - } - - return 0; -} - -static int try_lcs( - struct histindex *index, struct region *lcs, unsigned int b_ptr, - unsigned int line1, unsigned int count1, - unsigned int line2, unsigned int count2) -{ - unsigned int b_next = b_ptr + 1; - struct record *rec = index->records[TABLE_HASH(index, 2, b_ptr)]; - unsigned int as, ae, bs, be, np, rc; - int should_break; - - for (; rec; rec = rec->next) { - if (rec->cnt > index->cnt) { - if (!index->has_common) - index->has_common = CMP(index, 1, rec->ptr, 2, b_ptr); - continue; - } - - as = rec->ptr; - if (!CMP(index, 1, as, 2, b_ptr)) - continue; - - index->has_common = 1; - for (;;) { - should_break = 0; - np = NEXT_PTR(index, as); - bs = b_ptr; - ae = as; - be = bs; - rc = rec->cnt; - - while (line1 < as && line2 < bs - && CMP(index, 1, as - 1, 2, bs - 1)) { - as--; - bs--; - if (1 < rc) - rc = XDL_MIN(rc, CNT(index, as)); - } - while (ae < LINE_END(1) && be < LINE_END(2) - && CMP(index, 1, ae + 1, 2, be + 1)) { - ae++; - be++; - if (1 < rc) - rc = XDL_MIN(rc, CNT(index, ae)); - } - - if (b_next <= be) - b_next = be + 1; - if (lcs->end1 - lcs->begin1 < ae - as || rc < index->cnt) { - lcs->begin1 = as; - lcs->begin2 = bs; - lcs->end1 = ae; - lcs->end2 = be; - index->cnt = rc; - } - - if (np == 0) - break; - - while (np <= ae) { - np = NEXT_PTR(index, np); - if (np == 0) { - should_break = 1; - break; - } - } - - if (should_break) - break; - - as = np; - } - } - return b_next; -} - -static int find_lcs( - struct histindex *index, struct region *lcs, - unsigned int line1, unsigned int count1, - unsigned int line2, unsigned int count2) -{ - unsigned int b_ptr; - - if (scanA(index, line1, count1)) - return -1; - - index->cnt = index->max_chain_length + 1; - - for (b_ptr = line2; b_ptr <= LINE_END(2); ) - b_ptr = try_lcs(index, lcs, b_ptr, line1, count1, line2, count2); - - return index->has_common && index->max_chain_length < index->cnt; -} - -static int fall_back_to_classic_diff(struct histindex *index, - int line1, int count1, int line2, int count2) -{ - xpparam_t xpp; - xpp.flags = index->xpp->flags & ~XDF_HISTOGRAM_DIFF; - - return xdl_fall_back_diff(index->env, &xpp, - line1, count1, line2, count2); -} - -static int histogram_diff( - xpparam_t const *xpp, xdfenv_t *env, - unsigned int line1, unsigned int count1, - unsigned int line2, unsigned int count2) -{ - struct histindex index; - struct region lcs; - unsigned int sz; - int result = -1; - - if (count1 <= 0 && count2 <= 0) - return 0; - - if (LINE_END(1) >= MAX_PTR) - return -1; - - if (!count1) { - while(count2--) - env->xdf2.rchg[line2++ - 1] = 1; - return 0; - } else if (!count2) { - while(count1--) - env->xdf1.rchg[line1++ - 1] = 1; - return 0; - } - - memset(&index, 0, sizeof(index)); - - index.env = env; - index.xpp = xpp; - - index.records = NULL; - index.line_map = NULL; - /* in case of early xdl_cha_free() */ - index.rcha.head = NULL; - - index.table_bits = xdl_hashbits(count1); - sz = index.records_size = 1 << index.table_bits; - sz *= sizeof(struct record *); - if (!(index.records = (struct record **) xdl_malloc(sz))) - goto cleanup; - memset(index.records, 0, sz); - - sz = index.line_map_size = count1; - sz *= sizeof(struct record *); - if (!(index.line_map = (struct record **) xdl_malloc(sz))) - goto cleanup; - memset(index.line_map, 0, sz); - - sz = index.line_map_size; - sz *= sizeof(unsigned int); - if (!(index.next_ptrs = (unsigned int *) xdl_malloc(sz))) - goto cleanup; - memset(index.next_ptrs, 0, sz); - - /* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */ - if (xdl_cha_init(&index.rcha, sizeof(struct record), count1 / 4 + 1) < 0) - goto cleanup; - - index.ptr_shift = line1; - index.max_chain_length = 64; - - memset(&lcs, 0, sizeof(lcs)); - if (find_lcs(&index, &lcs, line1, count1, line2, count2)) - result = fall_back_to_classic_diff(&index, line1, count1, line2, count2); - else { - if (lcs.begin1 == 0 && lcs.begin2 == 0) { - while (count1--) - env->xdf1.rchg[line1++ - 1] = 1; - while (count2--) - env->xdf2.rchg[line2++ - 1] = 1; - result = 0; - } else { - result = histogram_diff(xpp, env, - line1, lcs.begin1 - line1, - line2, lcs.begin2 - line2); - if (result) - goto cleanup; - result = histogram_diff(xpp, env, - lcs.end1 + 1, LINE_END(1) - lcs.end1, - lcs.end2 + 1, LINE_END(2) - lcs.end2); - if (result) - goto cleanup; - } - } - -cleanup: - xdl_free(index.records); - xdl_free(index.line_map); - xdl_free(index.next_ptrs); - xdl_cha_free(&index.rcha); - - return result; -} - -int xdl_do_histogram_diff(mmfile_t *file1, mmfile_t *file2, - xpparam_t const *xpp, xdfenv_t *env) -{ - if (xdl_prepare_env(file1, file2, xpp, env) < 0) - return -1; - - return histogram_diff(xpp, env, - env->xdf1.dstart + 1, env->xdf1.dend - env->xdf1.dstart + 1, - env->xdf2.dstart + 1, env->xdf2.dend - env->xdf2.dstart + 1); -} diff --git a/src/xdiff/xmacros.h b/src/xdiff/xmacros.h deleted file mode 100644 index 165a895a93e..00000000000 --- a/src/xdiff/xmacros.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Davide Libenzi - * - */ - -#if !defined(XMACROS_H) -#define XMACROS_H - - - - -#define XDL_MIN(a, b) ((a) < (b) ? (a): (b)) -#define XDL_MAX(a, b) ((a) > (b) ? (a): (b)) -#define XDL_ABS(v) ((v) >= 0 ? (v): -(v)) -#define XDL_ISDIGIT(c) ((c) >= '0' && (c) <= '9') -#define XDL_ISSPACE(c) (isspace((unsigned char)(c))) -#define XDL_ADDBITS(v,b) ((v) + ((v) >> (b))) -#define XDL_MASKBITS(b) ((1UL << (b)) - 1) -#define XDL_HASHLONG(v,b) (XDL_ADDBITS((unsigned long)(v), b) & XDL_MASKBITS(b)) -#define XDL_PTRFREE(p) do { if (p) { xdl_free(p); (p) = NULL; } } while (0) -#define XDL_LE32_PUT(p, v) \ -do { \ - unsigned char *__p = (unsigned char *) (p); \ - *__p++ = (unsigned char) (v); \ - *__p++ = (unsigned char) ((v) >> 8); \ - *__p++ = (unsigned char) ((v) >> 16); \ - *__p = (unsigned char) ((v) >> 24); \ -} while (0) -#define XDL_LE32_GET(p, v) \ -do { \ - unsigned char const *__p = (unsigned char const *) (p); \ - (v) = (unsigned long) __p[0] | ((unsigned long) __p[1]) << 8 | \ - ((unsigned long) __p[2]) << 16 | ((unsigned long) __p[3]) << 24; \ -} while (0) - - -#endif /* #if !defined(XMACROS_H) */ diff --git a/src/xdiff/xmerge.c b/src/xdiff/xmerge.c deleted file mode 100644 index 84e42467228..00000000000 --- a/src/xdiff/xmerge.c +++ /dev/null @@ -1,619 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003-2006 Davide Libenzi, Johannes E. Schindelin - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Davide Libenzi - * - */ - -#include "xinclude.h" - -typedef struct s_xdmerge { - struct s_xdmerge *next; - /* - * 0 = conflict, - * 1 = no conflict, take first, - * 2 = no conflict, take second. - * 3 = no conflict, take both. - */ - int mode; - /* - * These point at the respective postimages. E.g. is - * how side #1 wants to change the common ancestor; if there is no - * overlap, lines before i1 in the postimage of side #1 appear - * in the merge result as a region touched by neither side. - */ - long i1, i2; - long chg1, chg2; - /* - * These point at the preimage; of course there is just one - * preimage, that is from the shared common ancestor. - */ - long i0; - long chg0; -} xdmerge_t; - -static int xdl_append_merge(xdmerge_t **merge, int mode, - long i0, long chg0, - long i1, long chg1, - long i2, long chg2) -{ - xdmerge_t *m = *merge; - if (m && (i1 <= m->i1 + m->chg1 || i2 <= m->i2 + m->chg2)) { - if (mode != m->mode) - m->mode = 0; - m->chg0 = i0 + chg0 - m->i0; - m->chg1 = i1 + chg1 - m->i1; - m->chg2 = i2 + chg2 - m->i2; - } else { - m = xdl_malloc(sizeof(xdmerge_t)); - if (!m) - return -1; - m->next = NULL; - m->mode = mode; - m->i0 = i0; - m->chg0 = chg0; - m->i1 = i1; - m->chg1 = chg1; - m->i2 = i2; - m->chg2 = chg2; - if (*merge) - (*merge)->next = m; - *merge = m; - } - return 0; -} - -static int xdl_cleanup_merge(xdmerge_t *c) -{ - int count = 0; - xdmerge_t *next_c; - - /* were there conflicts? */ - for (; c; c = next_c) { - if (c->mode == 0) - count++; - next_c = c->next; - free(c); - } - return count; -} - -static int xdl_merge_cmp_lines(xdfenv_t *xe1, int i1, xdfenv_t *xe2, int i2, - int line_count, long flags) -{ - int i; - xrecord_t **rec1 = xe1->xdf2.recs + i1; - xrecord_t **rec2 = xe2->xdf2.recs + i2; - - for (i = 0; i < line_count; i++) { - int result = xdl_recmatch(rec1[i]->ptr, rec1[i]->size, - rec2[i]->ptr, rec2[i]->size, flags); - if (!result) - return -1; - } - return 0; -} - -static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int add_nl, char *dest) -{ - xrecord_t **recs; - int size = 0; - - recs = (use_orig ? xe->xdf1.recs : xe->xdf2.recs) + i; - - if (count < 1) - return 0; - - for (i = 0; i < count; size += recs[i++]->size) - if (dest) - memcpy(dest + size, recs[i]->ptr, recs[i]->size); - if (add_nl) { - i = recs[count - 1]->size; - if (i == 0 || recs[count - 1]->ptr[i - 1] != '\n') { - if (dest) - dest[size] = '\n'; - size++; - } - } - return size; -} - -static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest) -{ - return xdl_recs_copy_0(0, xe, i, count, add_nl, dest); -} - -static int xdl_orig_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest) -{ - return xdl_recs_copy_0(1, xe, i, count, add_nl, dest); -} - -static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1, - xdfenv_t *xe2, const char *name2, - const char *name3, - int size, int i, int style, - xdmerge_t *m, char *dest, int marker_size) -{ - int marker1_size = (name1 ? (int)strlen(name1) + 1 : 0); - int marker2_size = (name2 ? (int)strlen(name2) + 1 : 0); - int marker3_size = (name3 ? (int)strlen(name3) + 1 : 0); - - if (marker_size <= 0) - marker_size = DEFAULT_CONFLICT_MARKER_SIZE; - - /* Before conflicting part */ - size += xdl_recs_copy(xe1, i, m->i1 - i, 0, - dest ? dest + size : NULL); - - if (!dest) { - size += marker_size + 1 + marker1_size; - } else { - memset(dest + size, '<', marker_size); - size += marker_size; - if (marker1_size) { - dest[size] = ' '; - memcpy(dest + size + 1, name1, marker1_size - 1); - size += marker1_size; - } - dest[size++] = '\n'; - } - - /* Postimage from side #1 */ - size += xdl_recs_copy(xe1, m->i1, m->chg1, 1, - dest ? dest + size : NULL); - - if (style == XDL_MERGE_DIFF3) { - /* Shared preimage */ - if (!dest) { - size += marker_size + 1 + marker3_size; - } else { - memset(dest + size, '|', marker_size); - size += marker_size; - if (marker3_size) { - dest[size] = ' '; - memcpy(dest + size + 1, name3, marker3_size - 1); - size += marker3_size; - } - dest[size++] = '\n'; - } - size += xdl_orig_copy(xe1, m->i0, m->chg0, 1, - dest ? dest + size : NULL); - } - - if (!dest) { - size += marker_size + 1; - } else { - memset(dest + size, '=', marker_size); - size += marker_size; - dest[size++] = '\n'; - } - - /* Postimage from side #2 */ - size += xdl_recs_copy(xe2, m->i2, m->chg2, 1, - dest ? dest + size : NULL); - if (!dest) { - size += marker_size + 1 + marker2_size; - } else { - memset(dest + size, '>', marker_size); - size += marker_size; - if (marker2_size) { - dest[size] = ' '; - memcpy(dest + size + 1, name2, marker2_size - 1); - size += marker2_size; - } - dest[size++] = '\n'; - } - return size; -} - -static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1, - xdfenv_t *xe2, const char *name2, - const char *ancestor_name, - int favor, - xdmerge_t *m, char *dest, int style, - int marker_size) -{ - int size, i; - - for (size = i = 0; m; m = m->next) { - if (favor && !m->mode) - m->mode = favor; - - if (m->mode == 0) - size = fill_conflict_hunk(xe1, name1, xe2, name2, - ancestor_name, - size, i, style, m, dest, - marker_size); - else if (m->mode & 3) { - /* Before conflicting part */ - size += xdl_recs_copy(xe1, i, m->i1 - i, 0, - dest ? dest + size : NULL); - /* Postimage from side #1 */ - if (m->mode & 1) - size += xdl_recs_copy(xe1, m->i1, m->chg1, 1, - dest ? dest + size : NULL); - /* Postimage from side #2 */ - if (m->mode & 2) - size += xdl_recs_copy(xe2, m->i2, m->chg2, 1, - dest ? dest + size : NULL); - } else - continue; - i = m->i1 + m->chg1; - } - size += xdl_recs_copy(xe1, i, xe1->xdf2.nrec - i, 0, - dest ? dest + size : NULL); - return size; -} - -/* - * Sometimes, changes are not quite identical, but differ in only a few - * lines. Try hard to show only these few lines as conflicting. - */ -static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m, - xpparam_t const *xpp) -{ - for (; m; m = m->next) { - mmfile_t t1, t2; - xdfenv_t xe; - xdchange_t *xscr, *x; - int i1 = m->i1, i2 = m->i2; - - /* let's handle just the conflicts */ - if (m->mode) - continue; - - /* no sense refining a conflict when one side is empty */ - if (m->chg1 == 0 || m->chg2 == 0) - continue; - - /* - * This probably does not work outside git, since - * we have a very simple mmfile structure. - */ - t1.ptr = (char *)xe1->xdf2.recs[m->i1]->ptr; - t1.size = xe1->xdf2.recs[m->i1 + m->chg1 - 1]->ptr - + xe1->xdf2.recs[m->i1 + m->chg1 - 1]->size - t1.ptr; - t2.ptr = (char *)xe2->xdf2.recs[m->i2]->ptr; - t2.size = xe2->xdf2.recs[m->i2 + m->chg2 - 1]->ptr - + xe2->xdf2.recs[m->i2 + m->chg2 - 1]->size - t2.ptr; - if (xdl_do_diff(&t1, &t2, xpp, &xe) < 0) - return -1; - if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 || - xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 || - xdl_build_script(&xe, &xscr) < 0) { - xdl_free_env(&xe); - return -1; - } - if (!xscr) { - /* If this happens, the changes are identical. */ - xdl_free_env(&xe); - m->mode = 4; - continue; - } - x = xscr; - m->i1 = xscr->i1 + i1; - m->chg1 = xscr->chg1; - m->i2 = xscr->i2 + i2; - m->chg2 = xscr->chg2; - while (xscr->next) { - xdmerge_t *m2 = xdl_malloc(sizeof(xdmerge_t)); - if (!m2) { - xdl_free_env(&xe); - xdl_free_script(x); - return -1; - } - xscr = xscr->next; - m2->next = m->next; - m->next = m2; - m = m2; - m->mode = 0; - m->i1 = xscr->i1 + i1; - m->chg1 = xscr->chg1; - m->i2 = xscr->i2 + i2; - m->chg2 = xscr->chg2; - } - xdl_free_env(&xe); - xdl_free_script(x); - } - return 0; -} - -static int line_contains_alnum(const char *ptr, long size) -{ - while (size--) - if (isalnum((unsigned char)*(ptr++))) - return 1; - return 0; -} - -static int lines_contain_alnum(xdfenv_t *xe, int i, int chg) -{ - for (; chg; chg--, i++) - if (line_contains_alnum(xe->xdf2.recs[i]->ptr, - xe->xdf2.recs[i]->size)) - return 1; - return 0; -} - -/* - * This function merges m and m->next, marking everything between those hunks - * as conflicting, too. - */ -static void xdl_merge_two_conflicts(xdmerge_t *m) -{ - xdmerge_t *next_m = m->next; - m->chg1 = next_m->i1 + next_m->chg1 - m->i1; - m->chg2 = next_m->i2 + next_m->chg2 - m->i2; - m->next = next_m->next; - free(next_m); -} - -/* - * If there are less than 3 non-conflicting lines between conflicts, - * it appears simpler -- because it takes up less (or as many) lines -- - * if the lines are moved into the conflicts. - */ -static int xdl_simplify_non_conflicts(xdfenv_t *xe1, xdmerge_t *m, - int simplify_if_no_alnum) -{ - int result = 0; - - if (!m) - return result; - for (;;) { - xdmerge_t *next_m = m->next; - int begin, end; - - if (!next_m) - return result; - - begin = m->i1 + m->chg1; - end = next_m->i1; - - if (m->mode != 0 || next_m->mode != 0 || - (end - begin > 3 && - (!simplify_if_no_alnum || - lines_contain_alnum(xe1, begin, end - begin)))) { - m = next_m; - } else { - result++; - xdl_merge_two_conflicts(m); - } - } -} - -/* - * level == 0: mark all overlapping changes as conflict - * level == 1: mark overlapping changes as conflict only if not identical - * level == 2: analyze non-identical changes for minimal conflict set - * level == 3: analyze non-identical changes for minimal conflict set, but - * treat hunks not containing any letter or number as conflicting - * - * returns < 0 on error, == 0 for no conflicts, else number of conflicts - */ -static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, - xdfenv_t *xe2, xdchange_t *xscr2, - xmparam_t const *xmp, mmbuffer_t *result) -{ - xdmerge_t *changes, *c; - xpparam_t const *xpp = &xmp->xpp; - const char *const ancestor_name = xmp->ancestor; - const char *const name1 = xmp->file1; - const char *const name2 = xmp->file2; - int i0, i1, i2, chg0, chg1, chg2; - int level = xmp->level; - int style = xmp->style; - int favor = xmp->favor; - - if (style == XDL_MERGE_DIFF3) { - /* - * "diff3 -m" output does not make sense for anything - * more aggressive than XDL_MERGE_EAGER. - */ - if (XDL_MERGE_EAGER < level) - level = XDL_MERGE_EAGER; - } - - c = changes = NULL; - - while (xscr1 && xscr2) { - if (!changes) - changes = c; - if (xscr1->i1 + xscr1->chg1 < xscr2->i1) { - i0 = xscr1->i1; - i1 = xscr1->i2; - i2 = xscr2->i2 - xscr2->i1 + xscr1->i1; - chg0 = xscr1->chg1; - chg1 = xscr1->chg2; - chg2 = xscr1->chg1; - if (xdl_append_merge(&c, 1, - i0, chg0, i1, chg1, i2, chg2)) { - xdl_cleanup_merge(changes); - return -1; - } - xscr1 = xscr1->next; - continue; - } - if (xscr2->i1 + xscr2->chg1 < xscr1->i1) { - i0 = xscr2->i1; - i1 = xscr1->i2 - xscr1->i1 + xscr2->i1; - i2 = xscr2->i2; - chg0 = xscr2->chg1; - chg1 = xscr2->chg1; - chg2 = xscr2->chg2; - if (xdl_append_merge(&c, 2, - i0, chg0, i1, chg1, i2, chg2)) { - xdl_cleanup_merge(changes); - return -1; - } - xscr2 = xscr2->next; - continue; - } - if (level == XDL_MERGE_MINIMAL || xscr1->i1 != xscr2->i1 || - xscr1->chg1 != xscr2->chg1 || - xscr1->chg2 != xscr2->chg2 || - xdl_merge_cmp_lines(xe1, xscr1->i2, - xe2, xscr2->i2, - xscr1->chg2, xpp->flags)) { - /* conflict */ - int off = xscr1->i1 - xscr2->i1; - int ffo = off + xscr1->chg1 - xscr2->chg1; - - i0 = xscr1->i1; - i1 = xscr1->i2; - i2 = xscr2->i2; - if (off > 0) { - i0 -= off; - i1 -= off; - } - else - i2 += off; - chg0 = xscr1->i1 + xscr1->chg1 - i0; - chg1 = xscr1->i2 + xscr1->chg2 - i1; - chg2 = xscr2->i2 + xscr2->chg2 - i2; - if (ffo < 0) { - chg0 -= ffo; - chg1 -= ffo; - } else - chg2 += ffo; - if (xdl_append_merge(&c, 0, - i0, chg0, i1, chg1, i2, chg2)) { - xdl_cleanup_merge(changes); - return -1; - } - } - - i1 = xscr1->i1 + xscr1->chg1; - i2 = xscr2->i1 + xscr2->chg1; - - if (i1 >= i2) - xscr2 = xscr2->next; - if (i2 >= i1) - xscr1 = xscr1->next; - } - while (xscr1) { - if (!changes) - changes = c; - i0 = xscr1->i1; - i1 = xscr1->i2; - i2 = xscr1->i1 + xe2->xdf2.nrec - xe2->xdf1.nrec; - chg0 = xscr1->chg1; - chg1 = xscr1->chg2; - chg2 = xscr1->chg1; - if (xdl_append_merge(&c, 1, - i0, chg0, i1, chg1, i2, chg2)) { - xdl_cleanup_merge(changes); - return -1; - } - xscr1 = xscr1->next; - } - while (xscr2) { - if (!changes) - changes = c; - i0 = xscr2->i1; - i1 = xscr2->i1 + xe1->xdf2.nrec - xe1->xdf1.nrec; - i2 = xscr2->i2; - chg0 = xscr2->chg1; - chg1 = xscr2->chg1; - chg2 = xscr2->chg2; - if (xdl_append_merge(&c, 2, - i0, chg0, i1, chg1, i2, chg2)) { - xdl_cleanup_merge(changes); - return -1; - } - xscr2 = xscr2->next; - } - if (!changes) - changes = c; - /* refine conflicts */ - if (XDL_MERGE_ZEALOUS <= level && - (xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0 || - xdl_simplify_non_conflicts(xe1, changes, - XDL_MERGE_ZEALOUS < level) < 0)) { - xdl_cleanup_merge(changes); - return -1; - } - /* output */ - if (result) { - int marker_size = xmp->marker_size; - int size = xdl_fill_merge_buffer(xe1, name1, xe2, name2, - ancestor_name, - favor, changes, NULL, style, - marker_size); - result->ptr = xdl_malloc(size); - if (!result->ptr) { - xdl_cleanup_merge(changes); - return -1; - } - result->size = size; - xdl_fill_merge_buffer(xe1, name1, xe2, name2, - ancestor_name, favor, changes, - result->ptr, style, marker_size); - } - return xdl_cleanup_merge(changes); -} - -int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2, - xmparam_t const *xmp, mmbuffer_t *result) -{ - xdchange_t *xscr1, *xscr2; - xdfenv_t xe1, xe2; - int status; - xpparam_t const *xpp = &xmp->xpp; - - result->ptr = NULL; - result->size = 0; - - if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0 || - xdl_do_diff(orig, mf2, xpp, &xe2) < 0) { - return -1; - } - if (xdl_change_compact(&xe1.xdf1, &xe1.xdf2, xpp->flags) < 0 || - xdl_change_compact(&xe1.xdf2, &xe1.xdf1, xpp->flags) < 0 || - xdl_build_script(&xe1, &xscr1) < 0) { - xdl_free_env(&xe1); - return -1; - } - if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 || - xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 || - xdl_build_script(&xe2, &xscr2) < 0) { - xdl_free_env(&xe2); - return -1; - } - status = 0; - if (!xscr1) { - result->ptr = xdl_malloc(mf2->size); - memcpy(result->ptr, mf2->ptr, mf2->size); - result->size = mf2->size; - } else if (!xscr2) { - result->ptr = xdl_malloc(mf1->size); - memcpy(result->ptr, mf1->ptr, mf1->size); - result->size = mf1->size; - } else { - status = xdl_do_merge(&xe1, xscr1, - &xe2, xscr2, - xmp, result); - } - xdl_free_script(xscr1); - xdl_free_script(xscr2); - - xdl_free_env(&xe1); - xdl_free_env(&xe2); - - return status; -} diff --git a/tests-clar/README.md b/tests-clar/README.md deleted file mode 100644 index 3aeaaf464c3..00000000000 --- a/tests-clar/README.md +++ /dev/null @@ -1,22 +0,0 @@ -Writing Clar tests for libgit2 -============================== - -For information on the Clar testing framework and a detailed introduction -please visit: - -https://github.com/vmg/clar - - -* Write your modules and tests. Use good, meaningful names. - -* Make sure you actually build the tests by setting: - - cmake -DBUILD_CLAR=ON build/ - -* Test: - - ./build/libgit2_clar - -* Make sure everything is fine. - -* Send your pull request. That's it. diff --git a/tests-clar/attr/file.c b/tests-clar/attr/file.c deleted file mode 100644 index 8866fd9bd6e..00000000000 --- a/tests-clar/attr/file.c +++ /dev/null @@ -1,226 +0,0 @@ -#include "clar_libgit2.h" -#include "attr_file.h" -#include "attr_expect.h" - -#define get_rule(X) ((git_attr_rule *)git_vector_get(&file->rules,(X))) -#define get_assign(R,Y) ((git_attr_assignment *)git_vector_get(&(R)->assigns,(Y))) - -void test_attr_file__simple_read(void) -{ - git_attr_file *file; - git_attr_assignment *assign; - git_attr_rule *rule; - - cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr0"))); - - cl_assert_equal_s(cl_fixture("attr/attr0"), file->key + 2); - cl_assert(file->rules.length == 1); - - rule = get_rule(0); - cl_assert(rule != NULL); - cl_assert_equal_s("*", rule->match.pattern); - cl_assert(rule->match.length == 1); - cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_HASWILD) != 0); - - cl_assert(rule->assigns.length == 1); - assign = get_assign(rule, 0); - cl_assert(assign != NULL); - cl_assert_equal_s("binary", assign->name); - cl_assert(GIT_ATTR_TRUE(assign->value)); - - git_attr_file__free(file); -} - -void test_attr_file__match_variants(void) -{ - git_attr_file *file; - git_attr_rule *rule; - git_attr_assignment *assign; - - cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr1"))); - - cl_assert_equal_s(cl_fixture("attr/attr1"), file->key + 2); - cl_assert(file->rules.length == 10); - - /* let's do a thorough check of this rule, then just verify - * the things that are unique for the later rules - */ - rule = get_rule(0); - cl_assert(rule); - cl_assert_equal_s("pat0", rule->match.pattern); - cl_assert(rule->match.length == strlen("pat0")); - cl_assert(rule->match.flags == 0); - cl_assert(rule->assigns.length == 1); - assign = get_assign(rule,0); - cl_assert_equal_s("attr0", assign->name); - cl_assert(assign->name_hash == git_attr_file__name_hash(assign->name)); - cl_assert(GIT_ATTR_TRUE(assign->value)); - - rule = get_rule(1); - cl_assert_equal_s("pat1", rule->match.pattern); - cl_assert(rule->match.length == strlen("pat1")); - cl_assert(rule->match.flags == GIT_ATTR_FNMATCH_NEGATIVE); - - rule = get_rule(2); - cl_assert_equal_s("pat2", rule->match.pattern); - cl_assert(rule->match.length == strlen("pat2")); - cl_assert(rule->match.flags == GIT_ATTR_FNMATCH_DIRECTORY); - - rule = get_rule(3); - cl_assert_equal_s("pat3dir/pat3file", rule->match.pattern); - cl_assert(rule->match.flags == GIT_ATTR_FNMATCH_FULLPATH); - - rule = get_rule(4); - cl_assert_equal_s("pat4.*", rule->match.pattern); - cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_HASWILD) != 0); - - rule = get_rule(5); - cl_assert_equal_s("*.pat5", rule->match.pattern); - cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_HASWILD) != 0); - - rule = get_rule(7); - cl_assert_equal_s("pat7[a-e]??[xyz]", rule->match.pattern); - cl_assert(rule->assigns.length == 1); - cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_HASWILD) != 0); - assign = get_assign(rule,0); - cl_assert_equal_s("attr7", assign->name); - cl_assert(GIT_ATTR_TRUE(assign->value)); - - rule = get_rule(8); - cl_assert_equal_s("pat8 with spaces", rule->match.pattern); - cl_assert(rule->match.length == strlen("pat8 with spaces")); - cl_assert(rule->match.flags == 0); - - rule = get_rule(9); - cl_assert_equal_s("pat9", rule->match.pattern); - - git_attr_file__free(file); -} - -static void check_one_assign( - git_attr_file *file, - int rule_idx, - int assign_idx, - const char *pattern, - const char *name, - enum attr_expect_t expected, - const char *expected_str) -{ - git_attr_rule *rule = get_rule(rule_idx); - git_attr_assignment *assign = get_assign(rule, assign_idx); - - cl_assert_equal_s(pattern, rule->match.pattern); - cl_assert(rule->assigns.length == 1); - cl_assert_equal_s(name, assign->name); - cl_assert(assign->name_hash == git_attr_file__name_hash(assign->name)); - - attr_check_expected(expected, expected_str, assign->name, assign->value); -} - -void test_attr_file__assign_variants(void) -{ - git_attr_file *file; - git_attr_rule *rule; - git_attr_assignment *assign; - - cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr2"))); - - cl_assert_equal_s(cl_fixture("attr/attr2"), file->key + 2); - cl_assert(file->rules.length == 11); - - check_one_assign(file, 0, 0, "pat0", "simple", EXPECT_TRUE, NULL); - check_one_assign(file, 1, 0, "pat1", "neg", EXPECT_FALSE, NULL); - check_one_assign(file, 2, 0, "*", "notundef", EXPECT_TRUE, NULL); - check_one_assign(file, 3, 0, "pat2", "notundef", EXPECT_UNDEFINED, NULL); - check_one_assign(file, 4, 0, "pat3", "assigned", EXPECT_STRING, "test-value"); - check_one_assign(file, 5, 0, "pat4", "rule-with-more-chars", EXPECT_STRING, "value-with-more-chars"); - check_one_assign(file, 6, 0, "pat5", "empty", EXPECT_TRUE, NULL); - check_one_assign(file, 7, 0, "pat6", "negempty", EXPECT_FALSE, NULL); - - rule = get_rule(8); - cl_assert_equal_s("pat7", rule->match.pattern); - cl_assert(rule->assigns.length == 5); - /* assignments will be sorted by hash value, so we have to do - * lookups by search instead of by position - */ - assign = git_attr_rule__lookup_assignment(rule, "multiple"); - cl_assert(assign); - cl_assert_equal_s("multiple", assign->name); - cl_assert(GIT_ATTR_TRUE(assign->value)); - assign = git_attr_rule__lookup_assignment(rule, "single"); - cl_assert(assign); - cl_assert_equal_s("single", assign->name); - cl_assert(GIT_ATTR_FALSE(assign->value)); - assign = git_attr_rule__lookup_assignment(rule, "values"); - cl_assert(assign); - cl_assert_equal_s("values", assign->name); - cl_assert_equal_s("1", assign->value); - assign = git_attr_rule__lookup_assignment(rule, "also"); - cl_assert(assign); - cl_assert_equal_s("also", assign->name); - cl_assert_equal_s("a-really-long-value/*", assign->value); - assign = git_attr_rule__lookup_assignment(rule, "happy"); - cl_assert(assign); - cl_assert_equal_s("happy", assign->name); - cl_assert_equal_s("yes!", assign->value); - assign = git_attr_rule__lookup_assignment(rule, "other"); - cl_assert(!assign); - - rule = get_rule(9); - cl_assert_equal_s("pat8", rule->match.pattern); - cl_assert(rule->assigns.length == 2); - assign = git_attr_rule__lookup_assignment(rule, "again"); - cl_assert(assign); - cl_assert_equal_s("again", assign->name); - cl_assert(GIT_ATTR_TRUE(assign->value)); - assign = git_attr_rule__lookup_assignment(rule, "another"); - cl_assert(assign); - cl_assert_equal_s("another", assign->name); - cl_assert_equal_s("12321", assign->value); - - check_one_assign(file, 10, 0, "pat9", "at-eof", EXPECT_FALSE, NULL); - - git_attr_file__free(file); -} - -void test_attr_file__check_attr_examples(void) -{ - git_attr_file *file; - git_attr_rule *rule; - git_attr_assignment *assign; - - cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr3"))); - cl_assert_equal_s(cl_fixture("attr/attr3"), file->key + 2); - cl_assert(file->rules.length == 3); - - rule = get_rule(0); - cl_assert_equal_s("*.java", rule->match.pattern); - cl_assert(rule->assigns.length == 3); - assign = git_attr_rule__lookup_assignment(rule, "diff"); - cl_assert_equal_s("diff", assign->name); - cl_assert_equal_s("java", assign->value); - assign = git_attr_rule__lookup_assignment(rule, "crlf"); - cl_assert_equal_s("crlf", assign->name); - cl_assert(GIT_ATTR_FALSE(assign->value)); - assign = git_attr_rule__lookup_assignment(rule, "myAttr"); - cl_assert_equal_s("myAttr", assign->name); - cl_assert(GIT_ATTR_TRUE(assign->value)); - assign = git_attr_rule__lookup_assignment(rule, "missing"); - cl_assert(assign == NULL); - - rule = get_rule(1); - cl_assert_equal_s("NoMyAttr.java", rule->match.pattern); - cl_assert(rule->assigns.length == 1); - assign = get_assign(rule, 0); - cl_assert_equal_s("myAttr", assign->name); - cl_assert(GIT_ATTR_UNSPECIFIED(assign->value)); - - rule = get_rule(2); - cl_assert_equal_s("README", rule->match.pattern); - cl_assert(rule->assigns.length == 1); - assign = get_assign(rule, 0); - cl_assert_equal_s("caveat", assign->name); - cl_assert_equal_s("unspecified", assign->value); - - git_attr_file__free(file); -} diff --git a/tests-clar/attr/lookup.c b/tests-clar/attr/lookup.c deleted file mode 100644 index 200bdd2c787..00000000000 --- a/tests-clar/attr/lookup.c +++ /dev/null @@ -1,262 +0,0 @@ -#include "clar_libgit2.h" -#include "attr_file.h" - -#include "attr_expect.h" - -void test_attr_lookup__simple(void) -{ - git_attr_file *file; - git_attr_path path; - const char *value = NULL; - - cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr0"))); - cl_assert_equal_s(cl_fixture("attr/attr0"), file->key + 2); - cl_assert(file->rules.length == 1); - - cl_git_pass(git_attr_path__init(&path, "test", NULL)); - cl_assert_equal_s("test", path.path); - cl_assert_equal_s("test", path.basename); - cl_assert(!path.is_dir); - - cl_git_pass(git_attr_file__lookup_one(file,&path,"binary",&value)); - cl_assert(GIT_ATTR_TRUE(value)); - - cl_git_pass(git_attr_file__lookup_one(file,&path,"missing",&value)); - cl_assert(!value); - - git_attr_path__free(&path); - git_attr_file__free(file); -} - -static void run_test_cases(git_attr_file *file, struct attr_expected *cases, int force_dir) -{ - git_attr_path path; - const char *value = NULL; - struct attr_expected *c; - int error; - - for (c = cases; c->path != NULL; c++) { - cl_git_pass(git_attr_path__init(&path, c->path, NULL)); - - if (force_dir) - path.is_dir = 1; - - error = git_attr_file__lookup_one(file,&path,c->attr,&value); - cl_git_pass(error); - - attr_check_expected(c->expected, c->expected_str, c->attr, value); - - git_attr_path__free(&path); - } -} - -void test_attr_lookup__match_variants(void) -{ - git_attr_file *file; - git_attr_path path; - - struct attr_expected dir_cases[] = { - { "pat2", "attr2", EXPECT_TRUE, NULL }, - { "/testing/for/pat2", "attr2", EXPECT_TRUE, NULL }, - { "/not/pat2/yousee", "attr2", EXPECT_UNDEFINED, NULL }, - { "/fun/fun/fun/pat4.dir", "attr4", EXPECT_TRUE, NULL }, - { "foo.pat5", "attr5", EXPECT_TRUE, NULL }, - { NULL, NULL, 0, NULL } - }; - - struct attr_expected cases[] = { - /* pat0 -> simple match */ - { "pat0", "attr0", EXPECT_TRUE, NULL }, - { "/testing/for/pat0", "attr0", EXPECT_TRUE, NULL }, - { "relative/to/pat0", "attr0", EXPECT_TRUE, NULL }, - { "this-contains-pat0-inside", "attr0", EXPECT_UNDEFINED, NULL }, - { "this-aint-right", "attr0", EXPECT_UNDEFINED, NULL }, - { "/this/pat0/dont/match", "attr0", EXPECT_UNDEFINED, NULL }, - /* negative match */ - { "pat0", "attr1", EXPECT_TRUE, NULL }, - { "pat1", "attr1", EXPECT_UNDEFINED, NULL }, - { "/testing/for/pat1", "attr1", EXPECT_UNDEFINED, NULL }, - { "/testing/for/pat0", "attr1", EXPECT_TRUE, NULL }, - { "/testing/for/pat1/inside", "attr1", EXPECT_TRUE, NULL }, - { "misc", "attr1", EXPECT_TRUE, NULL }, - /* dir match */ - { "pat2", "attr2", EXPECT_UNDEFINED, NULL }, - { "/testing/for/pat2", "attr2", EXPECT_UNDEFINED, NULL }, - { "/not/pat2/yousee", "attr2", EXPECT_UNDEFINED, NULL }, - /* path match */ - { "pat3file", "attr3", EXPECT_UNDEFINED, NULL }, - { "/pat3dir/pat3file", "attr3", EXPECT_TRUE, NULL }, - { "pat3dir/pat3file", "attr3", EXPECT_TRUE, NULL }, - /* pattern* match */ - { "pat4.txt", "attr4", EXPECT_TRUE, NULL }, - { "/fun/fun/fun/pat4.c", "attr4", EXPECT_TRUE, NULL }, - { "pat4.", "attr4", EXPECT_TRUE, NULL }, - { "pat4", "attr4", EXPECT_UNDEFINED, NULL }, - /* *pattern match */ - { "foo.pat5", "attr5", EXPECT_TRUE, NULL }, - { "/this/is/ok.pat5", "attr5", EXPECT_TRUE, NULL }, - { "/this/is/bad.pat5/yousee.txt", "attr5", EXPECT_UNDEFINED, NULL }, - { "foo.pat5", "attr100", EXPECT_UNDEFINED, NULL }, - /* glob match with slashes */ - { "foo.pat6", "attr6", EXPECT_UNDEFINED, NULL }, - { "pat6/pat6/foobar.pat6", "attr6", EXPECT_TRUE, NULL }, - { "pat6/pat6/.pat6", "attr6", EXPECT_TRUE, NULL }, - { "pat6/pat6/extra/foobar.pat6", "attr6", EXPECT_UNDEFINED, NULL }, - { "/prefix/pat6/pat6/foobar.pat6", "attr6", EXPECT_UNDEFINED, NULL }, - { "/pat6/pat6/foobar.pat6", "attr6", EXPECT_TRUE, NULL }, - /* complex pattern */ - { "pat7a12z", "attr7", EXPECT_TRUE, NULL }, - { "pat7e__x", "attr7", EXPECT_TRUE, NULL }, - { "pat7b/1y", "attr7", EXPECT_UNDEFINED, NULL }, /* ? does not match / */ - { "pat7e_x", "attr7", EXPECT_UNDEFINED, NULL }, - { "pat7aaaa", "attr7", EXPECT_UNDEFINED, NULL }, - { "pat7zzzz", "attr7", EXPECT_UNDEFINED, NULL }, - { "/this/can/be/anything/pat7a12z", "attr7", EXPECT_TRUE, NULL }, - { "but/it/still/must/match/pat7aaaa", "attr7", EXPECT_UNDEFINED, NULL }, - { "pat7aaay.fail", "attr7", EXPECT_UNDEFINED, NULL }, - /* pattern with spaces */ - { "pat8 with spaces", "attr8", EXPECT_TRUE, NULL }, - { "/gotta love/pat8 with spaces", "attr8", EXPECT_TRUE, NULL }, - { "failing pat8 with spaces", "attr8", EXPECT_UNDEFINED, NULL }, - { "spaces", "attr8", EXPECT_UNDEFINED, NULL }, - /* pattern at eof */ - { "pat9", "attr9", EXPECT_TRUE, NULL }, - { "/eof/pat9", "attr9", EXPECT_TRUE, NULL }, - { "pat", "attr9", EXPECT_UNDEFINED, NULL }, - { "at9", "attr9", EXPECT_UNDEFINED, NULL }, - { "pat9.fail", "attr9", EXPECT_UNDEFINED, NULL }, - /* sentinel at end */ - { NULL, NULL, 0, NULL } - }; - - cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr1"))); - cl_assert_equal_s(cl_fixture("attr/attr1"), file->key + 2); - cl_assert(file->rules.length == 10); - - cl_git_pass(git_attr_path__init(&path, "/testing/for/pat0", NULL)); - cl_assert_equal_s("pat0", path.basename); - - run_test_cases(file, cases, 0); - run_test_cases(file, dir_cases, 1); - - git_attr_file__free(file); - git_attr_path__free(&path); -} - -void test_attr_lookup__assign_variants(void) -{ - git_attr_file *file; - - struct attr_expected cases[] = { - /* pat0 -> simple assign */ - { "pat0", "simple", EXPECT_TRUE, NULL }, - { "/testing/pat0", "simple", EXPECT_TRUE, NULL }, - { "pat0", "fail", EXPECT_UNDEFINED, NULL }, - { "/testing/pat0", "fail", EXPECT_UNDEFINED, NULL }, - /* negative assign */ - { "pat1", "neg", EXPECT_FALSE, NULL }, - { "/testing/pat1", "neg", EXPECT_FALSE, NULL }, - { "pat1", "fail", EXPECT_UNDEFINED, NULL }, - { "/testing/pat1", "fail", EXPECT_UNDEFINED, NULL }, - /* forced undef */ - { "pat1", "notundef", EXPECT_TRUE, NULL }, - { "pat2", "notundef", EXPECT_UNDEFINED, NULL }, - { "/lead/in/pat1", "notundef", EXPECT_TRUE, NULL }, - { "/lead/in/pat2", "notundef", EXPECT_UNDEFINED, NULL }, - /* assign value */ - { "pat3", "assigned", EXPECT_STRING, "test-value" }, - { "pat3", "notassigned", EXPECT_UNDEFINED, NULL }, - /* assign value */ - { "pat4", "rule-with-more-chars", EXPECT_STRING, "value-with-more-chars" }, - { "pat4", "notassigned-rule-with-more-chars", EXPECT_UNDEFINED, NULL }, - /* empty assignments */ - { "pat5", "empty", EXPECT_TRUE, NULL }, - { "pat6", "negempty", EXPECT_FALSE, NULL }, - /* multiple assignment */ - { "pat7", "multiple", EXPECT_TRUE, NULL }, - { "pat7", "single", EXPECT_FALSE, NULL }, - { "pat7", "values", EXPECT_STRING, "1" }, - { "pat7", "also", EXPECT_STRING, "a-really-long-value/*" }, - { "pat7", "happy", EXPECT_STRING, "yes!" }, - { "pat8", "again", EXPECT_TRUE, NULL }, - { "pat8", "another", EXPECT_STRING, "12321" }, - /* bad assignment */ - { "patbad0", "simple", EXPECT_UNDEFINED, NULL }, - { "patbad0", "notundef", EXPECT_TRUE, NULL }, - { "patbad1", "simple", EXPECT_UNDEFINED, NULL }, - /* eof assignment */ - { "pat9", "at-eof", EXPECT_FALSE, NULL }, - /* sentinel at end */ - { NULL, NULL, 0, NULL } - }; - - cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr2"))); - cl_assert(file->rules.length == 11); - - run_test_cases(file, cases, 0); - - git_attr_file__free(file); -} - -void test_attr_lookup__check_attr_examples(void) -{ - git_attr_file *file; - - struct attr_expected cases[] = { - { "foo.java", "diff", EXPECT_STRING, "java" }, - { "foo.java", "crlf", EXPECT_FALSE, NULL }, - { "foo.java", "myAttr", EXPECT_TRUE, NULL }, - { "foo.java", "other", EXPECT_UNDEFINED, NULL }, - { "/prefix/dir/foo.java", "diff", EXPECT_STRING, "java" }, - { "/prefix/dir/foo.java", "crlf", EXPECT_FALSE, NULL }, - { "/prefix/dir/foo.java", "myAttr", EXPECT_TRUE, NULL }, - { "/prefix/dir/foo.java", "other", EXPECT_UNDEFINED, NULL }, - { "NoMyAttr.java", "crlf", EXPECT_FALSE, NULL }, - { "NoMyAttr.java", "myAttr", EXPECT_UNDEFINED, NULL }, - { "NoMyAttr.java", "other", EXPECT_UNDEFINED, NULL }, - { "/prefix/dir/NoMyAttr.java", "crlf", EXPECT_FALSE, NULL }, - { "/prefix/dir/NoMyAttr.java", "myAttr", EXPECT_UNDEFINED, NULL }, - { "/prefix/dir/NoMyAttr.java", "other", EXPECT_UNDEFINED, NULL }, - { "README", "caveat", EXPECT_STRING, "unspecified" }, - { "/specific/path/README", "caveat", EXPECT_STRING, "unspecified" }, - { "README", "missing", EXPECT_UNDEFINED, NULL }, - { "/specific/path/README", "missing", EXPECT_UNDEFINED, NULL }, - /* sentinel at end */ - { NULL, NULL, 0, NULL } - }; - - cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr3"))); - cl_assert(file->rules.length == 3); - - run_test_cases(file, cases, 0); - - git_attr_file__free(file); -} - -void test_attr_lookup__from_buffer(void) -{ - git_attr_file *file; - - struct attr_expected cases[] = { - { "abc", "foo", EXPECT_TRUE, NULL }, - { "abc", "bar", EXPECT_TRUE, NULL }, - { "abc", "baz", EXPECT_TRUE, NULL }, - { "aaa", "foo", EXPECT_TRUE, NULL }, - { "aaa", "bar", EXPECT_UNDEFINED, NULL }, - { "aaa", "baz", EXPECT_TRUE, NULL }, - { "qqq", "foo", EXPECT_UNDEFINED, NULL }, - { "qqq", "bar", EXPECT_UNDEFINED, NULL }, - { "qqq", "baz", EXPECT_TRUE, NULL }, - { NULL, NULL, 0, NULL } - }; - - cl_git_pass(git_attr_file__new(&file, 0, NULL, NULL)); - - cl_git_pass(git_attr_file__parse_buffer(NULL, NULL, "a* foo\nabc bar\n* baz", file)); - - cl_assert(file->rules.length == 3); - - run_test_cases(file, cases, 0); - - git_attr_file__free(file); -} diff --git a/tests-clar/attr/repo.c b/tests-clar/attr/repo.c deleted file mode 100644 index ca3e71e7f61..00000000000 --- a/tests-clar/attr/repo.c +++ /dev/null @@ -1,294 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" -#include "git2/attr.h" -#include "attr.h" - -#include "attr_expect.h" - -static git_repository *g_repo = NULL; - -void test_attr_repo__initialize(void) -{ - /* Before each test, instantiate the attr repo from the fixtures and - * rename the .gitted to .git so it is a repo with a working dir. - * Also rename gitattributes to .gitattributes, because it contains - * macro definitions which are only allowed in the root. - */ - g_repo = cl_git_sandbox_init("attr"); -} - -void test_attr_repo__cleanup(void) -{ - cl_git_sandbox_cleanup(); - g_repo = NULL; -} - -void test_attr_repo__get_one(void) -{ - struct attr_expected test_cases[] = { - { "root_test1", "repoattr", EXPECT_TRUE, NULL }, - { "root_test1", "rootattr", EXPECT_TRUE, NULL }, - { "root_test1", "missingattr", EXPECT_UNDEFINED, NULL }, - { "root_test1", "subattr", EXPECT_UNDEFINED, NULL }, - { "root_test1", "negattr", EXPECT_UNDEFINED, NULL }, - { "root_test2", "repoattr", EXPECT_TRUE, NULL }, - { "root_test2", "rootattr", EXPECT_FALSE, NULL }, - { "root_test2", "missingattr", EXPECT_UNDEFINED, NULL }, - { "root_test2", "multiattr", EXPECT_FALSE, NULL }, - { "root_test3", "repoattr", EXPECT_TRUE, NULL }, - { "root_test3", "rootattr", EXPECT_UNDEFINED, NULL }, - { "root_test3", "multiattr", EXPECT_STRING, "3" }, - { "root_test3", "multi2", EXPECT_UNDEFINED, NULL }, - { "sub/subdir_test1", "repoattr", EXPECT_TRUE, NULL }, - { "sub/subdir_test1", "rootattr", EXPECT_TRUE, NULL }, - { "sub/subdir_test1", "missingattr", EXPECT_UNDEFINED, NULL }, - { "sub/subdir_test1", "subattr", EXPECT_STRING, "yes" }, - { "sub/subdir_test1", "negattr", EXPECT_FALSE, NULL }, - { "sub/subdir_test1", "another", EXPECT_UNDEFINED, NULL }, - { "sub/subdir_test2.txt", "repoattr", EXPECT_TRUE, NULL }, - { "sub/subdir_test2.txt", "rootattr", EXPECT_TRUE, NULL }, - { "sub/subdir_test2.txt", "missingattr", EXPECT_UNDEFINED, NULL }, - { "sub/subdir_test2.txt", "subattr", EXPECT_STRING, "yes" }, - { "sub/subdir_test2.txt", "negattr", EXPECT_FALSE, NULL }, - { "sub/subdir_test2.txt", "another", EXPECT_STRING, "zero" }, - { "sub/subdir_test2.txt", "reposub", EXPECT_TRUE, NULL }, - { "sub/sub/subdir.txt", "another", EXPECT_STRING, "one" }, - { "sub/sub/subdir.txt", "reposubsub", EXPECT_TRUE, NULL }, - { "sub/sub/subdir.txt", "reposub", EXPECT_UNDEFINED, NULL }, - { "does-not-exist", "foo", EXPECT_STRING, "yes" }, - { "sub/deep/file", "deepdeep", EXPECT_TRUE, NULL }, - { "sub/sub/d/no", "test", EXPECT_STRING, "a/b/d/*" }, - { "sub/sub/d/yes", "test", EXPECT_UNDEFINED, NULL }, - { NULL, NULL, 0, NULL } - }, *scan; - - for (scan = test_cases; scan->path != NULL; scan++) { - const char *value; - cl_git_pass(git_attr_get(&value, g_repo, 0, scan->path, scan->attr)); - attr_check_expected(scan->expected, scan->expected_str, scan->attr, value); - } - - cl_assert(git_attr_cache__is_cached(g_repo, 0, ".git/info/attributes")); - cl_assert(git_attr_cache__is_cached(g_repo, 0, ".gitattributes")); - cl_assert(git_attr_cache__is_cached(g_repo, 0, "sub/.gitattributes")); -} - -void test_attr_repo__get_many(void) -{ - const char *names[4] = { "repoattr", "rootattr", "missingattr", "subattr" }; - const char *values[4]; - - cl_git_pass(git_attr_get_many(values, g_repo, 0, "root_test1", 4, names)); - - cl_assert(GIT_ATTR_TRUE(values[0])); - cl_assert(GIT_ATTR_TRUE(values[1])); - cl_assert(GIT_ATTR_UNSPECIFIED(values[2])); - cl_assert(GIT_ATTR_UNSPECIFIED(values[3])); - - cl_git_pass(git_attr_get_many(values, g_repo, 0, "root_test2", 4, names)); - - cl_assert(GIT_ATTR_TRUE(values[0])); - cl_assert(GIT_ATTR_FALSE(values[1])); - cl_assert(GIT_ATTR_UNSPECIFIED(values[2])); - cl_assert(GIT_ATTR_UNSPECIFIED(values[3])); - - cl_git_pass(git_attr_get_many(values, g_repo, 0, "sub/subdir_test1", 4, names)); - - cl_assert(GIT_ATTR_TRUE(values[0])); - cl_assert(GIT_ATTR_TRUE(values[1])); - cl_assert(GIT_ATTR_UNSPECIFIED(values[2])); - cl_assert_equal_s("yes", values[3]); -} - -static int count_attrs( - const char *name, - const char *value, - void *payload) -{ - GIT_UNUSED(name); - GIT_UNUSED(value); - - *((int *)payload) += 1; - - return 0; -} - -static int cancel_iteration( - const char *name, - const char *value, - void *payload) -{ - GIT_UNUSED(name); - GIT_UNUSED(value); - - *((int *)payload) -= 1; - - if (*((int *)payload) < 0) - return -1; - - return 0; -} - -void test_attr_repo__foreach(void) -{ - int count; - - count = 0; - cl_git_pass(git_attr_foreach( - g_repo, 0, "root_test1", &count_attrs, &count)); - cl_assert(count == 2); - - count = 0; - cl_git_pass(git_attr_foreach(g_repo, 0, "sub/subdir_test1", - &count_attrs, &count)); - cl_assert(count == 4); /* repoattr, rootattr, subattr, negattr */ - - count = 0; - cl_git_pass(git_attr_foreach(g_repo, 0, "sub/subdir_test2.txt", - &count_attrs, &count)); - cl_assert(count == 6); /* repoattr, rootattr, subattr, reposub, negattr, another */ - - count = 2; - cl_assert_equal_i( - GIT_EUSER, git_attr_foreach( - g_repo, 0, "sub/subdir_test1", &cancel_iteration, &count) - ); -} - -void test_attr_repo__manpage_example(void) -{ - const char *value; - - cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "foo")); - cl_assert(GIT_ATTR_TRUE(value)); - - cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "bar")); - cl_assert(GIT_ATTR_UNSPECIFIED(value)); - - cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "baz")); - cl_assert(GIT_ATTR_FALSE(value)); - - cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "merge")); - cl_assert_equal_s("filfre", value); - - cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "frotz")); - cl_assert(GIT_ATTR_UNSPECIFIED(value)); -} - -void test_attr_repo__macros(void) -{ - const char *names[5] = { "rootattr", "binary", "diff", "crlf", "frotz" }; - const char *names2[5] = { "mymacro", "positive", "negative", "rootattr", "another" }; - const char *names3[3] = { "macro2", "multi2", "multi3" }; - const char *values[5]; - - cl_git_pass(git_attr_get_many(values, g_repo, 0, "binfile", 5, names)); - - cl_assert(GIT_ATTR_TRUE(values[0])); - cl_assert(GIT_ATTR_TRUE(values[1])); - cl_assert(GIT_ATTR_FALSE(values[2])); - cl_assert(GIT_ATTR_FALSE(values[3])); - cl_assert(GIT_ATTR_UNSPECIFIED(values[4])); - - cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 5, names2)); - - cl_assert(GIT_ATTR_TRUE(values[0])); - cl_assert(GIT_ATTR_TRUE(values[1])); - cl_assert(GIT_ATTR_FALSE(values[2])); - cl_assert(GIT_ATTR_UNSPECIFIED(values[3])); - cl_assert_equal_s("77", values[4]); - - cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 3, names3)); - - cl_assert(GIT_ATTR_TRUE(values[0])); - cl_assert(GIT_ATTR_FALSE(values[1])); - cl_assert_equal_s("answer", values[2]); -} - -void test_attr_repo__bad_macros(void) -{ - const char *names[6] = { "rootattr", "positive", "negative", - "firstmacro", "secondmacro", "thirdmacro" }; - const char *values[6]; - - cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_bad", 6, names)); - - /* these three just confirm that the "mymacro" rule ran */ - cl_assert(GIT_ATTR_UNSPECIFIED(values[0])); - cl_assert(GIT_ATTR_TRUE(values[1])); - cl_assert(GIT_ATTR_FALSE(values[2])); - - /* file contains: - * # let's try some malicious macro defs - * [attr]firstmacro -thirdmacro -secondmacro - * [attr]secondmacro firstmacro -firstmacro - * [attr]thirdmacro secondmacro=hahaha -firstmacro - * macro_bad firstmacro secondmacro thirdmacro - * - * firstmacro assignment list ends up with: - * -thirdmacro -secondmacro - * secondmacro assignment list expands "firstmacro" and ends up with: - * -thirdmacro -secondmacro -firstmacro - * thirdmacro assignment don't expand so list ends up with: - * secondmacro="hahaha" - * - * macro_bad assignment list ends up with: - * -thirdmacro -secondmacro firstmacro && - * -thirdmacro -secondmacro -firstmacro secondmacro && - * secondmacro="hahaha" thirdmacro - * - * so summary results should be: - * -firstmacro secondmacro="hahaha" thirdmacro - */ - cl_assert(GIT_ATTR_FALSE(values[3])); - cl_assert_equal_s("hahaha", values[4]); - cl_assert(GIT_ATTR_TRUE(values[5])); -} - -#define CONTENT "I'm going to be dynamically processed\r\n" \ - "And my line endings...\r\n" \ - "...are going to be\n" \ - "normalized!\r\n" - -#define GITATTR "* text=auto\n" \ - "*.txt text\n" \ - "*.data binary\n" - -static void add_to_workdir(const char *filename, const char *content) -{ - git_buf buf = GIT_BUF_INIT; - - cl_git_pass(git_buf_joinpath(&buf, "attr", filename)); - cl_git_rewritefile(git_buf_cstr(&buf), content); - - git_buf_free(&buf); -} - -static void assert_proper_normalization(git_index *index, const char *filename, const char *expected_sha) -{ - size_t index_pos; - const git_index_entry *entry; - - add_to_workdir(filename, CONTENT); - cl_git_pass(git_index_add_bypath(index, filename)); - - cl_assert(!git_index_find(&index_pos, index, filename)); - - entry = git_index_get_byindex(index, index_pos); - cl_assert_equal_i(0, git_oid_streq(&entry->oid, expected_sha)); -} - -void test_attr_repo__staging_properly_normalizes_line_endings_according_to_gitattributes_directives(void) -{ - git_index* index; - - cl_git_pass(git_repository_index(&index, g_repo)); - - add_to_workdir(".gitattributes", GITATTR); - - assert_proper_normalization(index, "text.txt", "22c74203bace3c2e950278c7ab08da0fca9f4e9b"); - assert_proper_normalization(index, "huh.dunno", "22c74203bace3c2e950278c7ab08da0fca9f4e9b"); - assert_proper_normalization(index, "binary.data", "66eeff1fcbacf589e6d70aa70edd3fce5be2b37c"); - - git_index_free(index); -} diff --git a/tests-clar/buf/basic.c b/tests-clar/buf/basic.c deleted file mode 100644 index d558757a920..00000000000 --- a/tests-clar/buf/basic.c +++ /dev/null @@ -1,29 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" - -static const char *test_string = "Have you seen that? Have you seeeen that??"; - -void test_buf_basic__resize(void) -{ - git_buf buf1 = GIT_BUF_INIT; - git_buf_puts(&buf1, test_string); - cl_assert(git_buf_oom(&buf1) == 0); - cl_assert_equal_s(git_buf_cstr(&buf1), test_string); - - git_buf_puts(&buf1, test_string); - cl_assert(strlen(git_buf_cstr(&buf1)) == strlen(test_string) * 2); - git_buf_free(&buf1); -} - -void test_buf_basic__printf(void) -{ - git_buf buf2 = GIT_BUF_INIT; - git_buf_printf(&buf2, "%s %s %d ", "shoop", "da", 23); - cl_assert(git_buf_oom(&buf2) == 0); - cl_assert_equal_s(git_buf_cstr(&buf2), "shoop da 23 "); - - git_buf_printf(&buf2, "%s %d", "woop", 42); - cl_assert(git_buf_oom(&buf2) == 0); - cl_assert_equal_s(git_buf_cstr(&buf2), "shoop da 23 woop 42"); - git_buf_free(&buf2); -} diff --git a/tests-clar/buf/splice.c b/tests-clar/buf/splice.c deleted file mode 100644 index e80c93105b3..00000000000 --- a/tests-clar/buf/splice.c +++ /dev/null @@ -1,93 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" - -static git_buf _buf; - -void test_buf_splice__initialize(void) { - git_buf_init(&_buf, 16); -} - -void test_buf_splice__cleanup(void) { - git_buf_free(&_buf); -} - -void test_buf_splice__preprend(void) -{ - git_buf_sets(&_buf, "world!"); - - cl_git_pass(git_buf_splice(&_buf, 0, 0, "Hello Dolly", strlen("Hello "))); - - cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf)); -} - -void test_buf_splice__append(void) -{ - git_buf_sets(&_buf, "Hello"); - - cl_git_pass(git_buf_splice(&_buf, git_buf_len(&_buf), 0, " world!", strlen(" world!"))); - - cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf)); -} - -void test_buf_splice__insert_at(void) -{ - git_buf_sets(&_buf, "Hell world!"); - - cl_git_pass(git_buf_splice(&_buf, strlen("Hell"), 0, "o", strlen("o"))); - - cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf)); -} - -void test_buf_splice__remove_at(void) -{ - git_buf_sets(&_buf, "Hello world of warcraft!"); - - cl_git_pass(git_buf_splice(&_buf, strlen("Hello world"), strlen(" of warcraft"), "", 0)); - - cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf)); -} - -void test_buf_splice__replace(void) -{ - git_buf_sets(&_buf, "Hell0 w0rld!"); - - cl_git_pass(git_buf_splice(&_buf, strlen("Hell"), strlen("0 w0"), "o wo", strlen("o wo"))); - - cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf)); -} - -void test_buf_splice__replace_with_longer(void) -{ - git_buf_sets(&_buf, "Hello you!"); - - cl_git_pass(git_buf_splice(&_buf, strlen("Hello "), strlen("you"), "world", strlen("world"))); - - cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf)); -} - -void test_buf_splice__replace_with_shorter(void) -{ - git_buf_sets(&_buf, "Brave new world!"); - - cl_git_pass(git_buf_splice(&_buf, 0, strlen("Brave new"), "Hello", strlen("Hello"))); - - cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf)); -} - -void test_buf_splice__truncate(void) -{ - git_buf_sets(&_buf, "Hello world!!"); - - cl_git_pass(git_buf_splice(&_buf, strlen("Hello world!"), strlen("!"), "", 0)); - - cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf)); -} - -void test_buf_splice__dont_do_anything(void) -{ - git_buf_sets(&_buf, "Hello world!"); - - cl_git_pass(git_buf_splice(&_buf, 3, 0, "Hello", 0)); - - cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf)); -} diff --git a/tests-clar/checkout/binaryunicode.c b/tests-clar/checkout/binaryunicode.c deleted file mode 100644 index 5a781740fb7..00000000000 --- a/tests-clar/checkout/binaryunicode.c +++ /dev/null @@ -1,68 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" -#include "repo/repo_helpers.h" -#include "path.h" -#include "fileops.h" - -static git_repository *g_repo; - -void test_checkout_binaryunicode__initialize(void) -{ - g_repo = cl_git_sandbox_init("binaryunicode"); -} - -void test_checkout_binaryunicode__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -static void execute_test(void) -{ - git_oid oid, check; - git_commit *commit; - git_tree *tree; - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/branch1")); - cl_git_pass(git_commit_lookup(&commit, g_repo, &oid)); - cl_git_pass(git_commit_tree(&tree, commit)); - - opts.checkout_strategy = GIT_CHECKOUT_SAFE; - - cl_git_pass(git_checkout_tree(g_repo, (git_object *)tree, &opts)); - - git_tree_free(tree); - git_commit_free(commit); - - /* Verify that the lenna.jpg file was checked out correctly */ - cl_git_pass(git_oid_fromstr(&check, "8ab005d890fe53f65eda14b23672f60d9f4ec5a1")); - cl_git_pass(git_odb_hashfile(&oid, "binaryunicode/lenna.jpg", GIT_OBJ_BLOB)); - cl_assert(git_oid_equal(&oid, &check)); - - /* Verify that the text file was checked out correctly */ - cl_git_pass(git_oid_fromstr(&check, "965b223880dd4249e2c66a0cc0b4cffe1dc40f5a")); - cl_git_pass(git_odb_hashfile(&oid, "binaryunicode/utf16_withbom_noeol_crlf.txt", GIT_OBJ_BLOB)); - cl_assert(git_oid_equal(&oid, &check)); -} - -void test_checkout_binaryunicode__noautocrlf(void) -{ - git_config *config; - - cl_git_pass(git_repository_config(&config, g_repo)); - cl_git_pass(git_config_set_bool(config, "core.autocrlf", false)); - git_config_free(config); - - execute_test(); -} - -void test_checkout_binaryunicode__autocrlf(void) -{ - git_config *config; - - cl_git_pass(git_repository_config(&config, g_repo)); - cl_git_pass(git_config_set_bool(config, "core.autocrlf", true)); - git_config_free(config); - - execute_test(); -} diff --git a/tests-clar/checkout/checkout_helpers.c b/tests-clar/checkout/checkout_helpers.c deleted file mode 100644 index 79e80c13ab5..00000000000 --- a/tests-clar/checkout/checkout_helpers.c +++ /dev/null @@ -1,84 +0,0 @@ -#include "clar_libgit2.h" -#include "checkout_helpers.h" -#include "refs.h" -#include "fileops.h" - -/* this is essentially the code from git__unescape modified slightly */ -void strip_cr_from_buf(git_buf *buf) -{ - char *scan, *pos = buf->ptr, *end = pos + buf->size; - - for (scan = pos; scan < end; pos++, scan++) { - if (*scan == '\r') - scan++; /* skip '\r' */ - if (pos != scan) - *pos = *scan; - } - - *pos = '\0'; - buf->size = (pos - buf->ptr); -} - -void assert_on_branch(git_repository *repo, const char *branch) -{ - git_reference *head; - git_buf bname = GIT_BUF_INIT; - - cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE)); - cl_assert_(git_reference_type(head) == GIT_REF_SYMBOLIC, branch); - - cl_git_pass(git_buf_joinpath(&bname, "refs/heads", branch)); - cl_assert_equal_s(bname.ptr, git_reference_symbolic_target(head)); - - git_reference_free(head); - git_buf_free(&bname); -} - -void reset_index_to_treeish(git_object *treeish) -{ - git_object *tree; - git_index *index; - git_repository *repo = git_object_owner(treeish); - - cl_git_pass(git_object_peel(&tree, treeish, GIT_OBJ_TREE)); - - cl_git_pass(git_repository_index(&index, repo)); - cl_git_pass(git_index_read_tree(index, (git_tree *)tree)); - cl_git_pass(git_index_write(index)); - - git_object_free(tree); - git_index_free(index); -} - -static void test_file_contents_internal( - const char *path, const char *expectedcontents, bool strip_cr) -{ - int fd; - char data[1024] = {0}; - git_buf buf = GIT_BUF_INIT; - size_t expectedlen = strlen(expectedcontents); - - fd = p_open(path, O_RDONLY); - cl_assert(fd >= 0); - - buf.ptr = data; - buf.size = p_read(fd, buf.ptr, 1024); - - cl_git_pass(p_close(fd)); - - if (strip_cr) - strip_cr_from_buf(&buf); - - cl_assert_equal_i((int)expectedlen, (int)buf.size); - cl_assert_equal_s(expectedcontents, buf.ptr); -} - -void test_file_contents(const char *path, const char *expected) -{ - test_file_contents_internal(path, expected, false); -} - -void test_file_contents_nocr(const char *path, const char *expected) -{ - test_file_contents_internal(path, expected, true); -} diff --git a/tests-clar/checkout/checkout_helpers.h b/tests-clar/checkout/checkout_helpers.h deleted file mode 100644 index 2c3a4b5bbe5..00000000000 --- a/tests-clar/checkout/checkout_helpers.h +++ /dev/null @@ -1,9 +0,0 @@ -#include "buffer.h" -#include "git2/object.h" -#include "git2/repository.h" - -extern void strip_cr_from_buf(git_buf *buf); -extern void assert_on_branch(git_repository *repo, const char *branch); -extern void reset_index_to_treeish(git_object *treeish); -extern void test_file_contents(const char *path, const char *expected); -extern void test_file_contents_nocr(const char *path, const char *expected); diff --git a/tests-clar/checkout/crlf.c b/tests-clar/checkout/crlf.c deleted file mode 100644 index 856d32bd1ba..00000000000 --- a/tests-clar/checkout/crlf.c +++ /dev/null @@ -1,117 +0,0 @@ -#include "clar_libgit2.h" -#include "checkout_helpers.h" - -#include "git2/checkout.h" -#include "repository.h" - -#define UTF8_BOM "\xEF\xBB\xBF" -#define ALL_CRLF_TEXT_RAW "crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n" -#define ALL_LF_TEXT_RAW "lf\nlf\nlf\nlf\nlf\n" -#define MORE_CRLF_TEXT_RAW "crlf\r\ncrlf\r\nlf\ncrlf\r\ncrlf\r\n" -#define MORE_LF_TEXT_RAW "lf\nlf\ncrlf\r\nlf\nlf\n" - -#define ALL_LF_TEXT_AS_CRLF "lf\r\nlf\r\nlf\r\nlf\r\nlf\r\n" - -static git_repository *g_repo; - -void test_checkout_crlf__initialize(void) -{ - git_tree *tree; - - g_repo = cl_git_sandbox_init("crlf"); - - cl_git_pass(git_repository_head_tree(&tree, g_repo)); - git_tree_free(tree); -} - -void test_checkout_crlf__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -#ifdef GIT_WIN32 -static void set_config_entry_to(const char *entry_name, bool value) -{ - git_config *cfg; - - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_git_pass(git_config_set_bool(cfg, entry_name, value)); - - git_config_free(cfg); -} - -static void set_core_autocrlf_to(bool value) -{ - set_config_entry_to("core.autocrlf", value); -} -#endif - -void test_checkout_crlf__detect_crlf_autocrlf_false(void) -{ -#ifdef GIT_WIN32 - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - - set_core_autocrlf_to(false); - - git_checkout_head(g_repo, &opts); - - test_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW); -#endif -} - -void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void) -{ -#ifdef GIT_WIN32 - git_index *index; - const git_index_entry *entry; - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - - set_core_autocrlf_to(false); - - git_checkout_head(g_repo, &opts); - - git_repository_index(&index, g_repo); - - cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL); - cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW)); - - git_index_free(index); -#endif -} - -void test_checkout_crlf__detect_crlf_autocrlf_true(void) -{ -#ifdef GIT_WIN32 - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - - set_core_autocrlf_to(true); - - git_checkout_head(g_repo, &opts); - - test_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF); -#endif -} - -void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void) -{ -#ifdef GIT_WIN32 - git_index *index; - const git_index_entry *entry; - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - - set_core_autocrlf_to(true); - - git_checkout_head(g_repo, &opts); - - git_repository_index(&index, g_repo); - - cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL); - cl_assert(entry->file_size == strlen(ALL_LF_TEXT_AS_CRLF)); - - git_index_free(index); -#endif -} diff --git a/tests-clar/checkout/head.c b/tests-clar/checkout/head.c deleted file mode 100644 index 46646f8bf60..00000000000 --- a/tests-clar/checkout/head.c +++ /dev/null @@ -1,63 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" -#include "repo/repo_helpers.h" -#include "path.h" -#include "fileops.h" - -static git_repository *g_repo; - -void test_checkout_head__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_checkout_head__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_checkout_head__orphaned_head_returns_GIT_EORPHANEDHEAD(void) -{ - make_head_orphaned(g_repo, NON_EXISTING_HEAD); - - cl_assert_equal_i(GIT_EORPHANEDHEAD, git_checkout_head(g_repo, NULL)); -} - -void test_checkout_head__with_index_only_tree(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - git_index *index; - - /* let's start by getting things into a known state */ - - opts.checkout_strategy = GIT_CHECKOUT_FORCE; - cl_git_pass(git_checkout_head(g_repo, &opts)); - - /* now let's stage some new stuff including a new directory */ - - cl_git_pass(git_repository_index(&index, g_repo)); - - p_mkdir("testrepo/newdir", 0777); - cl_git_mkfile("testrepo/newdir/newfile.txt", "new file\n"); - - cl_git_pass(git_index_add_bypath(index, "newdir/newfile.txt")); - cl_git_pass(git_index_write(index)); - - cl_assert(git_path_isfile("testrepo/newdir/newfile.txt")); - cl_assert(git_index_get_bypath(index, "newdir/newfile.txt", 0) != NULL); - - git_index_free(index); - - /* okay, so now we have staged this new file; let's see if we can remove */ - - opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED; - cl_git_pass(git_checkout_head(g_repo, &opts)); - - cl_git_pass(git_repository_index(&index, g_repo)); - cl_git_pass(git_index_read(index)); /* reload if needed */ - - cl_assert(!git_path_isfile("testrepo/newdir/newfile.txt")); - cl_assert(git_index_get_bypath(index, "newdir/newfile.txt", 0) == NULL); - - git_index_free(index); -} diff --git a/tests-clar/checkout/index.c b/tests-clar/checkout/index.c deleted file mode 100644 index e8a61ca3f7e..00000000000 --- a/tests-clar/checkout/index.c +++ /dev/null @@ -1,510 +0,0 @@ -#include "clar_libgit2.h" -#include "checkout_helpers.h" - -#include "git2/checkout.h" -#include "repository.h" - -static git_repository *g_repo; - -void test_checkout_index__initialize(void) -{ - git_tree *tree; - - g_repo = cl_git_sandbox_init("testrepo"); - - cl_git_pass(git_repository_head_tree(&tree, g_repo)); - - reset_index_to_treeish((git_object *)tree); - git_tree_free(tree); - - cl_git_rewritefile( - "./testrepo/.gitattributes", - "* text eol=lf\n"); -} - -void test_checkout_index__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_checkout_index__cannot_checkout_a_bare_repository(void) -{ - test_checkout_index__cleanup(); - - g_repo = cl_git_sandbox_init("testrepo.git"); - - cl_git_fail(git_checkout_index(g_repo, NULL, NULL)); -} - -void test_checkout_index__can_create_missing_files(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - cl_assert_equal_i(false, git_path_isfile("./testrepo/README")); - cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt")); - cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt")); - - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - test_file_contents("./testrepo/README", "hey there\n"); - test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n"); - test_file_contents("./testrepo/new.txt", "my new file\n"); -} - -void test_checkout_index__can_remove_untracked_files(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - git_futils_mkdir("./testrepo/dir/subdir/subsubdir", NULL, 0755, GIT_MKDIR_PATH); - cl_git_mkfile("./testrepo/dir/one", "one\n"); - cl_git_mkfile("./testrepo/dir/subdir/two", "two\n"); - - cl_assert_equal_i(true, git_path_isdir("./testrepo/dir/subdir/subsubdir")); - - opts.checkout_strategy = - GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_REMOVE_UNTRACKED; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - cl_assert_equal_i(false, git_path_isdir("./testrepo/dir")); -} - -void test_checkout_index__honor_the_specified_pathspecs(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - char *entries[] = { "*.txt" }; - - opts.paths.strings = entries; - opts.paths.count = 1; - - cl_assert_equal_i(false, git_path_isfile("./testrepo/README")); - cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt")); - cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt")); - - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - cl_assert_equal_i(false, git_path_isfile("./testrepo/README")); - test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n"); - test_file_contents("./testrepo/new.txt", "my new file\n"); -} - -static void set_config_entry_to(const char *entry_name, bool value) -{ - git_config *cfg; - - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_git_pass(git_config_set_bool(cfg, entry_name, value)); - - git_config_free(cfg); -} - -static void set_core_autocrlf_to(bool value) -{ - set_config_entry_to("core.autocrlf", value); -} - -void test_checkout_index__honor_the_gitattributes_directives(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - const char *attributes = - "branch_file.txt text eol=crlf\n" - "new.txt text eol=lf\n"; - - cl_git_mkfile("./testrepo/.gitattributes", attributes); - set_core_autocrlf_to(false); - - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - test_file_contents("./testrepo/README", "hey there\n"); - test_file_contents("./testrepo/new.txt", "my new file\n"); - test_file_contents("./testrepo/branch_file.txt", "hi\r\nbye!\r\n"); -} - -void test_checkout_index__honor_coreautocrlf_setting_set_to_true(void) -{ -#ifdef GIT_WIN32 - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - const char *expected_readme_text = "hey there\r\n"; - - cl_git_pass(p_unlink("./testrepo/.gitattributes")); - set_core_autocrlf_to(true); - - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - test_file_contents("./testrepo/README", expected_readme_text); -#endif -} - -static void set_repo_symlink_handling_cap_to(bool value) -{ - set_config_entry_to("core.symlinks", value); -} - -void test_checkout_index__honor_coresymlinks_setting_set_to_true(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - set_repo_symlink_handling_cap_to(true); - - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - -#ifdef GIT_WIN32 - test_file_contents("./testrepo/link_to_new.txt", "new.txt"); -#else - { - char link_data[1024]; - size_t link_size = 1024; - - link_size = p_readlink("./testrepo/link_to_new.txt", link_data, link_size); - link_data[link_size] = '\0'; - cl_assert_equal_i(link_size, strlen("new.txt")); - cl_assert_equal_s(link_data, "new.txt"); - test_file_contents("./testrepo/link_to_new.txt", "my new file\n"); - } -#endif -} - -void test_checkout_index__honor_coresymlinks_setting_set_to_false(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - set_repo_symlink_handling_cap_to(false); - - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - test_file_contents("./testrepo/link_to_new.txt", "new.txt"); -} - -void test_checkout_index__donot_overwrite_modified_file_by_default(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!"); - - /* set this up to not return an error code on conflicts, but it - * still will not have permission to overwrite anything... - */ - opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_ALLOW_CONFLICTS; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - test_file_contents("./testrepo/new.txt", "This isn't what's stored!"); -} - -void test_checkout_index__can_overwrite_modified_file(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!"); - - opts.checkout_strategy = GIT_CHECKOUT_FORCE; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - test_file_contents("./testrepo/new.txt", "my new file\n"); -} - -void test_checkout_index__options_disable_filters(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - cl_git_mkfile("./testrepo/.gitattributes", "*.txt text eol=crlf\n"); - - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - opts.disable_filters = false; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - test_file_contents("./testrepo/new.txt", "my new file\r\n"); - - p_unlink("./testrepo/new.txt"); - - opts.disable_filters = true; - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - test_file_contents("./testrepo/new.txt", "my new file\n"); -} - -void test_checkout_index__options_dir_modes(void) -{ -#ifndef GIT_WIN32 - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - struct stat st; - git_oid oid; - git_commit *commit; - - cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); - cl_git_pass(git_commit_lookup(&commit, g_repo, &oid)); - - reset_index_to_treeish((git_object *)commit); - - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - opts.dir_mode = 0701; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - cl_git_pass(p_stat("./testrepo/a", &st)); - cl_assert_equal_i(st.st_mode & 0777, 0701); - - /* File-mode test, since we're on the 'dir' branch */ - cl_git_pass(p_stat("./testrepo/a/b.txt", &st)); - cl_assert_equal_i(st.st_mode & 0777, 0755); - - git_commit_free(commit); -#endif -} - -void test_checkout_index__options_override_file_modes(void) -{ -#ifndef GIT_WIN32 - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - struct stat st; - - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - opts.file_mode = 0700; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - cl_git_pass(p_stat("./testrepo/new.txt", &st)); - cl_assert_equal_i(st.st_mode & 0777, 0700); -#endif -} - -void test_checkout_index__options_open_flags(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - cl_git_mkfile("./testrepo/new.txt", "hi\n"); - - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND; - - opts.checkout_strategy = GIT_CHECKOUT_FORCE; - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - test_file_contents("./testrepo/new.txt", "hi\nmy new file\n"); -} - -struct notify_data { - const char *file; - const char *sha; -}; - -static int test_checkout_notify_cb( - git_checkout_notify_t why, - const char *path, - const git_diff_file *baseline, - const git_diff_file *target, - const git_diff_file *workdir, - void *payload) -{ - struct notify_data *expectations = (struct notify_data *)payload; - - GIT_UNUSED(workdir); - - cl_assert_equal_i(GIT_CHECKOUT_NOTIFY_CONFLICT, why); - cl_assert_equal_s(expectations->file, path); - cl_assert_equal_i(0, git_oid_streq(&baseline->oid, expectations->sha)); - cl_assert_equal_i(0, git_oid_streq(&target->oid, expectations->sha)); - - return 0; -} - -void test_checkout_index__can_notify_of_skipped_files(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - struct notify_data data; - - cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!"); - - /* - * $ git ls-tree HEAD - * 100644 blob a8233120f6ad708f843d861ce2b7228ec4e3dec6 README - * 100644 blob 3697d64be941a53d4ae8f6a271e4e3fa56b022cc branch_file.txt - * 100644 blob a71586c1dfe8a71c6cbf6c129f404c5642ff31bd new.txt - */ - data.file = "new.txt"; - data.sha = "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"; - - opts.checkout_strategy = - GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_ALLOW_CONFLICTS; - opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT; - opts.notify_cb = test_checkout_notify_cb; - opts.notify_payload = &data; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); -} - -static int dont_notify_cb( - git_checkout_notify_t why, - const char *path, - const git_diff_file *baseline, - const git_diff_file *target, - const git_diff_file *workdir, - void *payload) -{ - GIT_UNUSED(why); - GIT_UNUSED(path); - GIT_UNUSED(baseline); - GIT_UNUSED(target); - GIT_UNUSED(workdir); - GIT_UNUSED(payload); - - cl_assert(false); - - return 0; -} - -void test_checkout_index__wont_notify_of_expected_line_ending_changes(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - cl_git_pass(p_unlink("./testrepo/.gitattributes")); - set_core_autocrlf_to(true); - - cl_git_mkfile("./testrepo/new.txt", "my new file\r\n"); - - opts.checkout_strategy = - GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_ALLOW_CONFLICTS; - opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT; - opts.notify_cb = dont_notify_cb; - opts.notify_payload = NULL; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); -} - -static void checkout_progress_counter( - const char *path, size_t cur, size_t tot, void *payload) -{ - GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot); - (*(int *)payload)++; -} - -void test_checkout_index__calls_progress_callback(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - int calls = 0; - - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - opts.progress_cb = checkout_progress_counter; - opts.progress_payload = &calls; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - cl_assert(calls > 0); -} - -void test_checkout_index__can_overcome_name_clashes(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - git_index *index; - - cl_git_pass(git_repository_index(&index, g_repo)); - git_index_clear(index); - - cl_git_mkfile("./testrepo/path0", "content\r\n"); - cl_git_pass(p_mkdir("./testrepo/path1", 0777)); - cl_git_mkfile("./testrepo/path1/file1", "content\r\n"); - - cl_git_pass(git_index_add_bypath(index, "path0")); - cl_git_pass(git_index_add_bypath(index, "path1/file1")); - - cl_git_pass(p_unlink("./testrepo/path0")); - cl_git_pass(git_futils_rmdir_r( - "./testrepo/path1", NULL, GIT_RMDIR_REMOVE_FILES)); - - cl_git_mkfile("./testrepo/path1", "content\r\n"); - cl_git_pass(p_mkdir("./testrepo/path0", 0777)); - cl_git_mkfile("./testrepo/path0/file0", "content\r\n"); - - cl_assert(git_path_isfile("./testrepo/path1")); - cl_assert(git_path_isfile("./testrepo/path0/file0")); - - opts.checkout_strategy = - GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_ALLOW_CONFLICTS; - cl_git_pass(git_checkout_index(g_repo, index, &opts)); - - cl_assert(git_path_isfile("./testrepo/path1")); - cl_assert(git_path_isfile("./testrepo/path0/file0")); - - opts.checkout_strategy = GIT_CHECKOUT_FORCE; - cl_git_pass(git_checkout_index(g_repo, index, &opts)); - - cl_assert(git_path_isfile("./testrepo/path0")); - cl_assert(git_path_isfile("./testrepo/path1/file1")); - - git_index_free(index); -} - -void test_checkout_index__validates_struct_version(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - const git_error *err; - - opts.version = 1024; - cl_git_fail(git_checkout_index(g_repo, NULL, &opts)); - - err = giterr_last(); - cl_assert_equal_i(err->klass, GITERR_INVALID); - - opts.version = 0; - giterr_clear(); - cl_git_fail(git_checkout_index(g_repo, NULL, &opts)); - - err = giterr_last(); - cl_assert_equal_i(err->klass, GITERR_INVALID); -} - -void test_checkout_index__can_update_prefixed_files(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - cl_assert_equal_i(false, git_path_isfile("./testrepo/README")); - cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt")); - cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt")); - - cl_git_mkfile("./testrepo/READ", "content\n"); - cl_git_mkfile("./testrepo/README.after", "content\n"); - cl_git_pass(p_mkdir("./testrepo/branch_file", 0777)); - cl_git_pass(p_mkdir("./testrepo/branch_file/contained_dir", 0777)); - cl_git_mkfile("./testrepo/branch_file/contained_file", "content\n"); - cl_git_pass(p_mkdir("./testrepo/branch_file.txt.after", 0777)); - - opts.checkout_strategy = - GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_REMOVE_UNTRACKED; - - cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); - - /* remove untracked will remove the .gitattributes file before the blobs - * were created, so they will have had crlf filtering applied on Windows - */ - test_file_contents_nocr("./testrepo/README", "hey there\n"); - test_file_contents_nocr("./testrepo/branch_file.txt", "hi\nbye!\n"); - test_file_contents_nocr("./testrepo/new.txt", "my new file\n"); - - cl_assert(!git_path_exists("testrepo/READ")); - cl_assert(!git_path_exists("testrepo/README.after")); - cl_assert(!git_path_exists("testrepo/branch_file")); - cl_assert(!git_path_exists("testrepo/branch_file.txt.after")); -} - -void test_checkout_index__can_checkout_a_newly_initialized_repository(void) -{ - test_checkout_index__cleanup(); - - g_repo = cl_git_sandbox_init("empty_standard_repo"); - cl_git_remove_placeholders(git_repository_path(g_repo), "dummy-marker.txt"); - - cl_git_pass(git_checkout_index(g_repo, NULL, NULL)); -} diff --git a/tests-clar/checkout/tree.c b/tests-clar/checkout/tree.c deleted file mode 100644 index 821cbfe0e48..00000000000 --- a/tests-clar/checkout/tree.c +++ /dev/null @@ -1,446 +0,0 @@ -#include "clar_libgit2.h" -#include "checkout_helpers.h" - -#include "git2/checkout.h" -#include "repository.h" -#include "buffer.h" -#include "fileops.h" - -static git_repository *g_repo; -static git_checkout_opts g_opts; -static git_object *g_object; - -void test_checkout_tree__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); - - GIT_INIT_STRUCTURE(&g_opts, GIT_CHECKOUT_OPTS_VERSION); - g_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; -} - -void test_checkout_tree__cleanup(void) -{ - git_object_free(g_object); - g_object = NULL; - - cl_git_sandbox_cleanup(); -} - -void test_checkout_tree__cannot_checkout_a_non_treeish(void) -{ - /* blob */ - cl_git_pass(git_revparse_single(&g_object, g_repo, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd")); - - cl_git_fail(git_checkout_tree(g_repo, g_object, NULL)); -} - -void test_checkout_tree__can_checkout_a_subdirectory_from_a_commit(void) -{ - char *entries[] = { "ab/de/" }; - - g_opts.paths.strings = entries; - g_opts.paths.count = 1; - - cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees")); - - cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/")); - - cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); - - cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt")); - cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/fgh/1.txt")); -} - -void test_checkout_tree__can_checkout_and_remove_directory(void) -{ - cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/")); - - /* Checkout brach "subtrees" and update HEAD, so that HEAD matches the - * current working tree - */ - cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees")); - cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); - - cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees")); - - cl_assert_equal_i(true, git_path_isdir("./testrepo/ab/")); - cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt")); - cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/fgh/1.txt")); - - git_object_free(g_object); - g_object = NULL; - - /* Checkout brach "master" and update HEAD, so that HEAD matches the - * current working tree - */ - cl_git_pass(git_revparse_single(&g_object, g_repo, "master")); - cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); - - cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master")); - - /* This directory should no longer exist */ - cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/")); -} - -void test_checkout_tree__can_checkout_a_subdirectory_from_a_subtree(void) -{ - char *entries[] = { "de/" }; - - g_opts.paths.strings = entries; - g_opts.paths.count = 1; - - cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees:ab")); - - cl_assert_equal_i(false, git_path_isdir("./testrepo/de/")); - - cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); - - cl_assert_equal_i(true, git_path_isfile("./testrepo/de/2.txt")); - cl_assert_equal_i(true, git_path_isfile("./testrepo/de/fgh/1.txt")); -} - -static void progress(const char *path, size_t cur, size_t tot, void *payload) -{ - bool *was_called = (bool*)payload; - GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot); - *was_called = true; -} - -void test_checkout_tree__calls_progress_callback(void) -{ - bool was_called = 0; - - g_opts.progress_cb = progress; - g_opts.progress_payload = &was_called; - - cl_git_pass(git_revparse_single(&g_object, g_repo, "master")); - - cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); - - cl_assert_equal_i(was_called, true); -} - -void test_checkout_tree__doesnt_write_unrequested_files_to_worktree(void) -{ - git_oid master_oid; - git_oid chomped_oid; - git_commit* p_master_commit; - git_commit* p_chomped_commit; - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - git_oid_fromstr(&master_oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - git_oid_fromstr(&chomped_oid, "e90810b8df3e80c413d903f631643c716887138d"); - cl_git_pass(git_commit_lookup(&p_master_commit, g_repo, &master_oid)); - cl_git_pass(git_commit_lookup(&p_chomped_commit, g_repo, &chomped_oid)); - - /* GIT_CHECKOUT_NONE should not add any file to the working tree from the - * index as it is supposed to be a dry run. - */ - opts.checkout_strategy = GIT_CHECKOUT_NONE; - git_checkout_tree(g_repo, (git_object*)p_chomped_commit, &opts); - cl_assert_equal_i(false, git_path_isfile("testrepo/readme.txt")); - - git_commit_free(p_master_commit); - git_commit_free(p_chomped_commit); -} - -void test_checkout_tree__can_switch_branches(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - git_oid oid; - git_object *obj = NULL; - - assert_on_branch(g_repo, "master"); - - /* do first checkout with FORCE because we don't know if testrepo - * base data is clean for a checkout or not - */ - opts.checkout_strategy = GIT_CHECKOUT_FORCE; - - cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); - cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY)); - - cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); - cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir")); - - cl_assert(git_path_isfile("testrepo/README")); - cl_assert(git_path_isfile("testrepo/branch_file.txt")); - cl_assert(git_path_isfile("testrepo/new.txt")); - cl_assert(git_path_isfile("testrepo/a/b.txt")); - - cl_assert(!git_path_isdir("testrepo/ab")); - - assert_on_branch(g_repo, "dir"); - - git_object_free(obj); - - /* do second checkout safe because we should be clean after first */ - opts.checkout_strategy = GIT_CHECKOUT_SAFE; - - cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees")); - cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY)); - - cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); - cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees")); - - cl_assert(git_path_isfile("testrepo/README")); - cl_assert(git_path_isfile("testrepo/branch_file.txt")); - cl_assert(git_path_isfile("testrepo/new.txt")); - cl_assert(git_path_isfile("testrepo/ab/4.txt")); - cl_assert(git_path_isfile("testrepo/ab/c/3.txt")); - cl_assert(git_path_isfile("testrepo/ab/de/2.txt")); - cl_assert(git_path_isfile("testrepo/ab/de/fgh/1.txt")); - - cl_assert(!git_path_isdir("testrepo/a")); - - assert_on_branch(g_repo, "subtrees"); - - git_object_free(obj); -} - -void test_checkout_tree__can_remove_untracked(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_REMOVE_UNTRACKED; - - cl_git_mkfile("testrepo/untracked_file", "as you wish"); - cl_assert(git_path_isfile("testrepo/untracked_file")); - - cl_git_pass(git_checkout_head(g_repo, &opts)); - - cl_assert(!git_path_isfile("testrepo/untracked_file")); -} - -void test_checkout_tree__can_remove_ignored(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - int ignored = 0; - - opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_REMOVE_IGNORED; - - cl_git_mkfile("testrepo/ignored_file", "as you wish"); - - cl_git_pass(git_ignore_add_rule(g_repo, "ignored_file\n")); - - cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "ignored_file")); - cl_assert_equal_i(1, ignored); - - cl_assert(git_path_isfile("testrepo/ignored_file")); - - cl_git_pass(git_checkout_head(g_repo, &opts)); - - cl_assert(!git_path_isfile("testrepo/ignored_file")); -} - -void test_checkout_tree__can_update_only(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - git_oid oid; - git_object *obj = NULL; - - /* first let's get things into a known state - by checkout out the HEAD */ - - assert_on_branch(g_repo, "master"); - - opts.checkout_strategy = GIT_CHECKOUT_FORCE; - cl_git_pass(git_checkout_head(g_repo, &opts)); - - cl_assert(!git_path_isdir("testrepo/a")); - - test_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n"); - - /* now checkout branch but with update only */ - - opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY; - - cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); - cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY)); - - cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); - cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir")); - - assert_on_branch(g_repo, "dir"); - - /* this normally would have been created (which was tested separately in - * the test_checkout_tree__can_switch_branches test), but with - * UPDATE_ONLY it will not have been created. - */ - cl_assert(!git_path_isdir("testrepo/a")); - - /* but this file still should have been updated */ - test_file_contents_nocr("testrepo/branch_file.txt", "hi\n"); - - git_object_free(obj); -} - -void test_checkout_tree__can_checkout_with_pattern(void) -{ - char *entries[] = { "[l-z]*.txt" }; - - /* reset to beginning of history (i.e. just a README file) */ - - g_opts.checkout_strategy = - GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED; - - cl_git_pass(git_revparse_single(&g_object, g_repo, - "8496071c1b46c854b31185ea97743be6a8774479")); - - cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); - cl_git_pass( - git_repository_set_head_detached(g_repo, git_object_id(g_object))); - - git_object_free(g_object); - g_object = NULL; - - cl_assert(git_path_exists("testrepo/README")); - cl_assert(!git_path_exists("testrepo/branch_file.txt")); - cl_assert(!git_path_exists("testrepo/link_to_new.txt")); - cl_assert(!git_path_exists("testrepo/new.txt")); - - /* now to a narrow patterned checkout */ - - g_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - g_opts.paths.strings = entries; - g_opts.paths.count = 1; - - cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master")); - - cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); - - cl_assert(git_path_exists("testrepo/README")); - cl_assert(!git_path_exists("testrepo/branch_file.txt")); - cl_assert(git_path_exists("testrepo/link_to_new.txt")); - cl_assert(git_path_exists("testrepo/new.txt")); -} - -void test_checkout_tree__can_disable_pattern_match(void) -{ - char *entries[] = { "b*.txt" }; - - /* reset to beginning of history (i.e. just a README file) */ - - g_opts.checkout_strategy = - GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED; - - cl_git_pass(git_revparse_single(&g_object, g_repo, - "8496071c1b46c854b31185ea97743be6a8774479")); - - cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); - cl_git_pass( - git_repository_set_head_detached(g_repo, git_object_id(g_object))); - - git_object_free(g_object); - g_object = NULL; - - cl_assert(!git_path_isfile("testrepo/branch_file.txt")); - - /* now to a narrow patterned checkout, but disable pattern */ - - g_opts.checkout_strategy = - GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH; - g_opts.paths.strings = entries; - g_opts.paths.count = 1; - - cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master")); - - cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); - - cl_assert(!git_path_isfile("testrepo/branch_file.txt")); - - /* let's try that again, but allow the pattern match */ - - g_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - - cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); - - cl_assert(git_path_isfile("testrepo/branch_file.txt")); -} - -void assert_conflict( - const char *entry_path, - const char *new_content, - const char *parent_sha, - const char *commit_sha) -{ - git_index *index; - git_object *hack_tree; - git_reference *branch, *head; - git_buf file_path = GIT_BUF_INIT; - - cl_git_pass(git_repository_index(&index, g_repo)); - - /* Create a branch pointing at the parent */ - cl_git_pass(git_revparse_single(&g_object, g_repo, parent_sha)); - cl_git_pass(git_branch_create(&branch, g_repo, - "potential_conflict", (git_commit *)g_object, 0)); - - /* Make HEAD point to this branch */ - cl_git_pass(git_reference_symbolic_create( - &head, g_repo, "HEAD", git_reference_name(branch), 1)); - git_reference_free(head); - git_reference_free(branch); - - /* Checkout the parent */ - g_opts.checkout_strategy = GIT_CHECKOUT_FORCE; - cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); - - /* Hack-ishy workaound to ensure *all* the index entries - * match the content of the tree - */ - cl_git_pass(git_object_peel(&hack_tree, g_object, GIT_OBJ_TREE)); - cl_git_pass(git_index_read_tree(index, (git_tree *)hack_tree)); - git_object_free(hack_tree); - git_object_free(g_object); - g_object = NULL; - - /* Create a conflicting file */ - cl_git_pass(git_buf_joinpath(&file_path, "./testrepo", entry_path)); - cl_git_mkfile(git_buf_cstr(&file_path), new_content); - git_buf_free(&file_path); - - /* Trying to checkout the original commit */ - cl_git_pass(git_revparse_single(&g_object, g_repo, commit_sha)); - - g_opts.checkout_strategy = GIT_CHECKOUT_SAFE; - cl_assert_equal_i( - GIT_EMERGECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts)); - - /* Stage the conflicting change */ - cl_git_pass(git_index_add_bypath(index, entry_path)); - cl_git_pass(git_index_write(index)); - git_index_free(index); - - cl_assert_equal_i( - GIT_EMERGECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts)); -} - -void test_checkout_tree__checking_out_a_conflicting_type_change_returns_EMERGECONFLICT(void) -{ - /* - * 099faba adds a symlink named 'link_to_new.txt' - * a65fedf is the parent of 099faba - */ - - assert_conflict("link_to_new.txt", "old.txt", "a65fedf", "099faba"); -} - -void test_checkout_tree__checking_out_a_conflicting_type_change_returns_EMERGECONFLICT_2(void) -{ - /* - * cf80f8d adds a directory named 'a/' - * a4a7dce is the parent of cf80f8d - */ - - assert_conflict("a", "hello\n", "a4a7dce", "cf80f8d"); -} - -void test_checkout_tree__checking_out_a_conflicting_content_change_returns_EMERGECONFLICT(void) -{ - /* - * c47800c adds a symlink named 'branch_file.txt' - * 5b5b025 is the parent of 763d71a - */ - - assert_conflict("branch_file.txt", "hello\n", "5b5b025", "c47800c"); -} diff --git a/tests-clar/checkout/typechange.c b/tests-clar/checkout/typechange.c deleted file mode 100644 index b92cc23fa52..00000000000 --- a/tests-clar/checkout/typechange.c +++ /dev/null @@ -1,240 +0,0 @@ -#include "clar_libgit2.h" -#include "git2/checkout.h" -#include "path.h" -#include "posix.h" -#include "fileops.h" - -static git_repository *g_repo = NULL; - -static const char *g_typechange_oids[] = { - "79b9f23e85f55ea36a472a902e875bc1121a94cb", - "9bdb75b73836a99e3dbeea640a81de81031fdc29", - "0e7ed140b514b8cae23254cb8656fe1674403aff", - "9d0235c7a7edc0889a18f97a42ee6db9fe688447", - "9b19edf33a03a0c59cdfc113bfa5c06179bf9b1a", - "1b63caae4a5ca96f78e8dfefc376c6a39a142475", - "6eae26c90e8ccc4d16208972119c40635489c6f0", - NULL -}; - -static bool g_typechange_empty[] = { - true, false, false, false, false, false, true, true -}; - -void test_checkout_typechange__initialize(void) -{ - g_repo = cl_git_sandbox_init("typechanges"); - - cl_fixture_sandbox("submod2_target"); - p_rename("submod2_target/.gitted", "submod2_target/.git"); -} - -void test_checkout_typechange__cleanup(void) -{ - cl_git_sandbox_cleanup(); - cl_fixture_cleanup("submod2_target"); -} - -static void assert_file_exists(const char *path) -{ - cl_assert_(git_path_isfile(path), path); -} - -static void assert_dir_exists(const char *path) -{ - cl_assert_(git_path_isdir(path), path); -} - -static void assert_workdir_matches_tree( - git_repository *repo, const git_oid *id, const char *root, bool recurse) -{ - git_object *obj; - git_tree *tree; - size_t i, max_i; - git_buf path = GIT_BUF_INIT; - - if (!root) - root = git_repository_workdir(repo); - cl_assert(root); - - cl_git_pass(git_object_lookup(&obj, repo, id, GIT_OBJ_ANY)); - cl_git_pass(git_object_peel((git_object **)&tree, obj, GIT_OBJ_TREE)); - git_object_free(obj); - - max_i = git_tree_entrycount(tree); - - for (i = 0; i < max_i; ++i) { - const git_tree_entry *te = git_tree_entry_byindex(tree, i); - cl_assert(te); - - cl_git_pass(git_buf_joinpath(&path, root, git_tree_entry_name(te))); - - switch (git_tree_entry_type(te)) { - case GIT_OBJ_COMMIT: - assert_dir_exists(path.ptr); - break; - case GIT_OBJ_TREE: - assert_dir_exists(path.ptr); - if (recurse) - assert_workdir_matches_tree( - repo, git_tree_entry_id(te), path.ptr, true); - break; - case GIT_OBJ_BLOB: - switch (git_tree_entry_filemode(te)) { - case GIT_FILEMODE_BLOB: - case GIT_FILEMODE_BLOB_EXECUTABLE: - assert_file_exists(path.ptr); - /* because of cross-platform, don't confirm exec bit yet */ - break; - case GIT_FILEMODE_LINK: - cl_assert_(git_path_exists(path.ptr), path.ptr); - /* because of cross-platform, don't confirm link yet */ - break; - default: - cl_assert(false); /* really?! */ - } - break; - default: - cl_assert(false); /* really?!! */ - } - } - - git_tree_free(tree); - git_buf_free(&path); -} - -void test_checkout_typechange__checkout_typechanges_safe(void) -{ - int i; - git_object *obj; - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - - for (i = 0; g_typechange_oids[i] != NULL; ++i) { - cl_git_pass(git_revparse_single(&obj, g_repo, g_typechange_oids[i])); - - opts.checkout_strategy = GIT_CHECKOUT_FORCE; - - /* There are bugs in some submodule->tree changes that prevent - * SAFE from passing here, even though the following should work: - */ - /* !i ? GIT_CHECKOUT_FORCE : GIT_CHECKOUT_SAFE; */ - - cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); - - cl_git_pass( - git_repository_set_head_detached(g_repo, git_object_id(obj))); - - assert_workdir_matches_tree(g_repo, git_object_id(obj), NULL, true); - - git_object_free(obj); - - if (!g_typechange_empty[i]) { - cl_assert(git_path_isdir("typechanges")); - cl_assert(git_path_exists("typechanges/a")); - cl_assert(git_path_exists("typechanges/b")); - cl_assert(git_path_exists("typechanges/c")); - cl_assert(git_path_exists("typechanges/d")); - cl_assert(git_path_exists("typechanges/e")); - } else { - cl_assert(git_path_isdir("typechanges")); - cl_assert(!git_path_exists("typechanges/a")); - cl_assert(!git_path_exists("typechanges/b")); - cl_assert(!git_path_exists("typechanges/c")); - cl_assert(!git_path_exists("typechanges/d")); - cl_assert(!git_path_exists("typechanges/e")); - } - } -} - -typedef struct { - int conflicts; - int dirty; - int updates; - int untracked; - int ignored; -} notify_counts; - -static int notify_counter( - git_checkout_notify_t why, - const char *path, - const git_diff_file *baseline, - const git_diff_file *target, - const git_diff_file *workdir, - void *payload) -{ - notify_counts *cts = payload; - - GIT_UNUSED(path); - GIT_UNUSED(baseline); - GIT_UNUSED(target); - GIT_UNUSED(workdir); - - switch (why) { - case GIT_CHECKOUT_NOTIFY_CONFLICT: cts->conflicts++; break; - case GIT_CHECKOUT_NOTIFY_DIRTY: cts->dirty++; break; - case GIT_CHECKOUT_NOTIFY_UPDATED: cts->updates++; break; - case GIT_CHECKOUT_NOTIFY_UNTRACKED: cts->untracked++; break; - case GIT_CHECKOUT_NOTIFY_IGNORED: cts->ignored++; break; - default: break; - } - - return 0; -} - -static void force_create_file(const char *file) -{ - int error = git_futils_rmdir_r(file, NULL, - GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS); - cl_assert(!error || error == GIT_ENOTFOUND); - cl_git_pass(git_futils_mkpath2file(file, 0777)); - cl_git_rewritefile(file, "yowza!"); -} - -void test_checkout_typechange__checkout_with_conflicts(void) -{ - int i; - git_object *obj; - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - notify_counts cts = {0}; - - opts.notify_flags = - GIT_CHECKOUT_NOTIFY_CONFLICT | GIT_CHECKOUT_NOTIFY_UNTRACKED; - opts.notify_cb = notify_counter; - opts.notify_payload = &cts; - - for (i = 0; g_typechange_oids[i] != NULL; ++i) { - cl_git_pass(git_revparse_single(&obj, g_repo, g_typechange_oids[i])); - - force_create_file("typechanges/a/blocker"); - force_create_file("typechanges/b"); - force_create_file("typechanges/c/sub/sub/file"); - git_futils_rmdir_r("typechanges/d", NULL, GIT_RMDIR_REMOVE_FILES); - p_mkdir("typechanges/d", 0777); /* intentionally empty dir */ - force_create_file("typechanges/untracked"); - - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - memset(&cts, 0, sizeof(cts)); - - cl_git_fail(git_checkout_tree(g_repo, obj, &opts)); - cl_assert(cts.conflicts > 0); - cl_assert(cts.untracked > 0); - - opts.checkout_strategy = - GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED; - memset(&cts, 0, sizeof(cts)); - - cl_assert(git_path_exists("typechanges/untracked")); - - cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); - cl_assert_equal_i(0, cts.conflicts); - - cl_assert(!git_path_exists("typechanges/untracked")); - - cl_git_pass( - git_repository_set_head_detached(g_repo, git_object_id(obj))); - - assert_workdir_matches_tree(g_repo, git_object_id(obj), NULL, true); - - git_object_free(obj); - } -} diff --git a/tests-clar/clar.c b/tests-clar/clar.c deleted file mode 100644 index 10bea872494..00000000000 --- a/tests-clar/clar.c +++ /dev/null @@ -1,424 +0,0 @@ -/* - * Copyright (c) Vicent Marti. All rights reserved. - * - * This file is part of clar, distributed under the ISC license. - * For full terms see the included COPYING file. - */ -#include -#include -#include -#include -#include -#include -#include - -/* required for sandboxing */ -#include -#include - -#ifdef _WIN32 -# include -# include -# include -# include - -# define _MAIN_CC __cdecl - -# define stat(path, st) _stat(path, st) -# define mkdir(path, mode) _mkdir(path) -# define chdir(path) _chdir(path) -# define access(path, mode) _access(path, mode) -# define strdup(str) _strdup(str) -# define strcasecmp(a,b) _stricmp(a,b) - -# ifndef __MINGW32__ -# pragma comment(lib, "shell32") -# define strncpy(to, from, to_size) strncpy_s(to, to_size, from, _TRUNCATE) -# define W_OK 02 -# define S_ISDIR(x) ((x & _S_IFDIR) != 0) -# define snprint_eq(buf,sz,fmt,a,b) _snprintf_s(buf,sz,_TRUNCATE,fmt,a,b) -# else -# define snprint_eq snprintf -# endif - typedef struct _stat STAT_T; -#else -# include /* waitpid(2) */ -# include -# define _MAIN_CC -# define snprint_eq snprintf - typedef struct stat STAT_T; -#endif - -#include "clar.h" - -static void fs_rm(const char *_source); -static void fs_copy(const char *_source, const char *dest); - -static const char * -fixture_path(const char *base, const char *fixture_name); - -struct clar_error { - const char *test; - int test_number; - const char *suite; - const char *file; - int line_number; - const char *error_msg; - char *description; - - struct clar_error *next; -}; - -static struct { - const char *active_test; - const char *active_suite; - - int suite_errors; - int total_errors; - - int tests_ran; - int suites_ran; - - int report_errors_only; - int exit_on_error; - int report_suite_names; - - struct clar_error *errors; - struct clar_error *last_error; - - void (*local_cleanup)(void *); - void *local_cleanup_payload; - - jmp_buf trampoline; - int trampoline_enabled; -} _clar; - -struct clar_func { - const char *name; - void (*ptr)(void); -}; - -struct clar_suite { - const char *name; - struct clar_func initialize; - struct clar_func cleanup; - const struct clar_func *tests; - size_t test_count; - int enabled; -}; - -/* From clar_print_*.c */ -static void clar_print_init(int test_count, int suite_count, const char *suite_names); -static void clar_print_shutdown(int test_count, int suite_count, int error_count); -static void clar_print_error(int num, const struct clar_error *error); -static void clar_print_ontest(const char *test_name, int test_number, int failed); -static void clar_print_onsuite(const char *suite_name, int suite_index); -static void clar_print_onabort(const char *msg, ...); - -/* From clar_sandbox.c */ -static void clar_unsandbox(void); -static int clar_sandbox(void); - -/* Load the declarations for the test suite */ -#include "clar.suite" - -/* Core test functions */ -static void -clar_report_errors(void) -{ - int i = 1; - struct clar_error *error, *next; - - error = _clar.errors; - while (error != NULL) { - next = error->next; - clar_print_error(i++, error); - free(error->description); - free(error); - error = next; - } - - _clar.errors = _clar.last_error = NULL; -} - -static void -clar_run_test( - const struct clar_func *test, - const struct clar_func *initialize, - const struct clar_func *cleanup) -{ - int error_st = _clar.suite_errors; - - _clar.trampoline_enabled = 1; - - if (setjmp(_clar.trampoline) == 0) { - if (initialize->ptr != NULL) - initialize->ptr(); - - test->ptr(); - } - - _clar.trampoline_enabled = 0; - - if (_clar.local_cleanup != NULL) - _clar.local_cleanup(_clar.local_cleanup_payload); - - if (cleanup->ptr != NULL) - cleanup->ptr(); - - _clar.tests_ran++; - - /* remove any local-set cleanup methods */ - _clar.local_cleanup = NULL; - _clar.local_cleanup_payload = NULL; - - if (_clar.report_errors_only) - clar_report_errors(); - else - clar_print_ontest( - test->name, - _clar.tests_ran, - (_clar.suite_errors > error_st) - ); -} - -static void -clar_run_suite(const struct clar_suite *suite) -{ - const struct clar_func *test = suite->tests; - size_t i; - - if (!suite->enabled) - return; - - if (_clar.exit_on_error && _clar.total_errors) - return; - - if (!_clar.report_errors_only) - clar_print_onsuite(suite->name, ++_clar.suites_ran); - - _clar.active_suite = suite->name; - _clar.suite_errors = 0; - - for (i = 0; i < suite->test_count; ++i) { - _clar.active_test = test[i].name; - clar_run_test(&test[i], &suite->initialize, &suite->cleanup); - - if (_clar.exit_on_error && _clar.total_errors) - return; - } -} - -static void -clar_usage(const char *arg) -{ - printf("Usage: %s [options]\n\n", arg); - printf("Options:\n"); - printf(" -sname\tRun only the suite with `name`\n"); - printf(" -iname\tInclude the suite with `name`\n"); - printf(" -xname\tExclude the suite with `name`\n"); - printf(" -q \tOnly report tests that had an error\n"); - printf(" -Q \tQuit as soon as a test fails\n"); - printf(" -l \tPrint suite names\n"); - exit(-1); -} - -static void -clar_parse_args(int argc, char **argv) -{ - int i; - - for (i = 1; i < argc; ++i) { - char *argument = argv[i]; - - if (argument[0] != '-') - clar_usage(argv[0]); - - switch (argument[1]) { - case 's': - case 'i': - case 'x': { /* given suite name */ - int offset = (argument[2] == '=') ? 3 : 2, found = 0; - char action = argument[1]; - size_t j, len; - - argument += offset; - len = strlen(argument); - - if (len == 0) - clar_usage(argv[0]); - - for (j = 0; j < _clar_suite_count; ++j) { - if (strncmp(argument, _clar_suites[j].name, len) == 0) { - int exact = !strcmp(argument, _clar_suites[j].name); - - ++found; - - if (!exact) - _clar.report_suite_names = 1; - - switch (action) { - case 's': clar_run_suite(&_clar_suites[j]); break; - case 'i': _clar_suites[j].enabled = 1; break; - case 'x': _clar_suites[j].enabled = 0; break; - } - - if (exact) - break; - } - } - - if (!found) { - clar_print_onabort("No suite matching '%s' found.\n", argument); - exit(-1); - } - break; - } - - case 'q': - _clar.report_errors_only = 1; - break; - - case 'Q': - _clar.exit_on_error = 1; - break; - - case 'l': { - size_t j; - printf("Test suites (use -s to run just one):\n"); - for (j = 0; j < _clar_suite_count; ++j) - printf(" %3d: %s\n", (int)j, _clar_suites[j].name); - - exit(0); - } - - default: - clar_usage(argv[0]); - } - } -} - -int -clar_test(int argc, char **argv) -{ - clar_print_init( - (int)_clar_callback_count, - (int)_clar_suite_count, - "" - ); - - if (clar_sandbox() < 0) { - clar_print_onabort("Failed to sandbox the test runner.\n"); - exit(-1); - } - - if (argc > 1) - clar_parse_args(argc, argv); - - if (!_clar.suites_ran) { - size_t i; - for (i = 0; i < _clar_suite_count; ++i) - clar_run_suite(&_clar_suites[i]); - } - - clar_print_shutdown( - _clar.tests_ran, - (int)_clar_suite_count, - _clar.total_errors - ); - - clar_unsandbox(); - return _clar.total_errors; -} - -void -clar__assert( - int condition, - const char *file, - int line, - const char *error_msg, - const char *description, - int should_abort) -{ - struct clar_error *error; - - if (condition) - return; - - error = calloc(1, sizeof(struct clar_error)); - - if (_clar.errors == NULL) - _clar.errors = error; - - if (_clar.last_error != NULL) - _clar.last_error->next = error; - - _clar.last_error = error; - - error->test = _clar.active_test; - error->test_number = _clar.tests_ran; - error->suite = _clar.active_suite; - error->file = file; - error->line_number = line; - error->error_msg = error_msg; - - if (description != NULL) - error->description = strdup(description); - - _clar.suite_errors++; - _clar.total_errors++; - - if (should_abort) { - if (!_clar.trampoline_enabled) { - clar_print_onabort( - "Fatal error: a cleanup method raised an exception."); - clar_report_errors(); - exit(-1); - } - - longjmp(_clar.trampoline, -1); - } -} - -void clar__assert_equal_s( - const char *s1, - const char *s2, - const char *file, - int line, - const char *err, - int should_abort) -{ - int match = (s1 == NULL || s2 == NULL) ? (s1 == s2) : (strcmp(s1, s2) == 0); - - if (!match) { - char buf[4096]; - snprint_eq(buf, 4096, "'%s' != '%s'", s1, s2); - clar__assert(0, file, line, err, buf, should_abort); - } -} - -void clar__assert_equal_i( - int i1, - int i2, - const char *file, - int line, - const char *err, - int should_abort) -{ - if (i1 != i2) { - char buf[128]; - snprint_eq(buf, 128, "%d != %d", i1, i2); - clar__assert(0, file, line, err, buf, should_abort); - } -} - -void cl_set_cleanup(void (*cleanup)(void *), void *opaque) -{ - _clar.local_cleanup = cleanup; - _clar.local_cleanup_payload = opaque; -} - -#include "clar/sandbox.h" -#include "clar/fixtures.h" -#include "clar/fs.h" -#include "clar/print.h" diff --git a/tests-clar/clar.h b/tests-clar/clar.h deleted file mode 100644 index 2ba6416b383..00000000000 --- a/tests-clar/clar.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) Vicent Marti. All rights reserved. - * - * This file is part of clar, distributed under the ISC license. - * For full terms see the included COPYING file. - */ -#ifndef __CLAR_TEST_H__ -#define __CLAR_TEST_H__ - -#include - -int clar_test(int argc, char *argv[]); - -void cl_set_cleanup(void (*cleanup)(void *), void *opaque); -void cl_fs_cleanup(void); - -#ifdef CLAR_FIXTURE_PATH -const char *cl_fixture(const char *fixture_name); -void cl_fixture_sandbox(const char *fixture_name); -void cl_fixture_cleanup(const char *fixture_name); -#endif - -/** - * Assertion macros with explicit error message - */ -#define cl_must_pass_(expr, desc) clar__assert((expr) >= 0, __FILE__, __LINE__, "Function call failed: " #expr, desc, 1) -#define cl_must_fail_(expr, desc) clar__assert((expr) < 0, __FILE__, __LINE__, "Expected function call to fail: " #expr, desc, 1) -#define cl_assert_(expr, desc) clar__assert((expr) != 0, __FILE__, __LINE__, "Expression is not true: " #expr, desc, 1) - -/** - * Check macros with explicit error message - */ -#define cl_check_pass_(expr, desc) clar__assert((expr) >= 0, __FILE__, __LINE__, "Function call failed: " #expr, desc, 0) -#define cl_check_fail_(expr, desc) clar__assert((expr) < 0, __FILE__, __LINE__, "Expected function call to fail: " #expr, desc, 0) -#define cl_check_(expr, desc) clar__assert((expr) != 0, __FILE__, __LINE__, "Expression is not true: " #expr, desc, 0) - -/** - * Assertion macros with no error message - */ -#define cl_must_pass(expr) cl_must_pass_(expr, NULL) -#define cl_must_fail(expr) cl_must_fail_(expr, NULL) -#define cl_assert(expr) cl_assert_(expr, NULL) - -/** - * Check macros with no error message - */ -#define cl_check_pass(expr) cl_check_pass_(expr, NULL) -#define cl_check_fail(expr) cl_check_fail_(expr, NULL) -#define cl_check(expr) cl_check_(expr, NULL) - -/** - * Forced failure/warning - */ -#define cl_fail(desc) clar__assert(0, __FILE__, __LINE__, "Test failed.", desc, 1) -#define cl_warning(desc) clar__assert(0, __FILE__, __LINE__, "Warning during test execution:", desc, 0) - -/** - * Typed assertion macros - */ -#define cl_assert_equal_s(s1,s2) clar__assert_equal_s((s1),(s2),__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2, 1) -#define cl_assert_equal_i(i1,i2) clar__assert_equal_i((i1),(i2),__FILE__,__LINE__,#i1 " != " #i2, 1) -#define cl_assert_equal_b(b1,b2) clar__assert_equal_i(!!(b1),!!(b2),__FILE__,__LINE__,#b1 " != " #b2, 1) -#define cl_assert_equal_p(p1,p2) cl_assert((p1) == (p2)) - -void clar__assert( - int condition, - const char *file, - int line, - const char *error, - const char *description, - int should_abort); - -void clar__assert_equal_s(const char *,const char *,const char *,int,const char *,int); -void clar__assert_equal_i(int,int,const char *,int,const char *,int); - -#endif diff --git a/tests-clar/clar/fixtures.h b/tests-clar/clar/fixtures.h deleted file mode 100644 index 264cd7f4f4c..00000000000 --- a/tests-clar/clar/fixtures.h +++ /dev/null @@ -1,38 +0,0 @@ -static const char * -fixture_path(const char *base, const char *fixture_name) -{ - static char _path[4096]; - size_t root_len; - - root_len = strlen(base); - strncpy(_path, base, sizeof(_path)); - - if (_path[root_len - 1] != '/') - _path[root_len++] = '/'; - - if (fixture_name[0] == '/') - fixture_name++; - - strncpy(_path + root_len, - fixture_name, - sizeof(_path) - root_len); - - return _path; -} - -#ifdef CLAR_FIXTURE_PATH -const char *cl_fixture(const char *fixture_name) -{ - return fixture_path(CLAR_FIXTURE_PATH, fixture_name); -} - -void cl_fixture_sandbox(const char *fixture_name) -{ - fs_copy(cl_fixture(fixture_name), _clar_path); -} - -void cl_fixture_cleanup(const char *fixture_name) -{ - fs_rm(fixture_path(_clar_path, fixture_name)); -} -#endif diff --git a/tests-clar/clar/fs.h b/tests-clar/clar/fs.h deleted file mode 100644 index b7a1ff9d22d..00000000000 --- a/tests-clar/clar/fs.h +++ /dev/null @@ -1,325 +0,0 @@ -#ifdef _WIN32 - -#define RM_RETRY_COUNT 5 -#define RM_RETRY_DELAY 10 - -#ifdef __MINGW32__ - -/* These security-enhanced functions are not available - * in MinGW, so just use the vanilla ones */ -#define wcscpy_s(a, b, c) wcscpy((a), (c)) -#define wcscat_s(a, b, c) wcscat((a), (c)) - -#endif /* __MINGW32__ */ - -static int -fs__dotordotdot(WCHAR *_tocheck) -{ - return _tocheck[0] == '.' && - (_tocheck[1] == '\0' || - (_tocheck[1] == '.' && _tocheck[2] == '\0')); -} - -static int -fs_rmdir_rmdir(WCHAR *_wpath) -{ - unsigned retries = 1; - - while (!RemoveDirectoryW(_wpath)) { - /* Only retry when we have retries remaining, and the - * error was ERROR_DIR_NOT_EMPTY. */ - if (retries++ > RM_RETRY_COUNT || - ERROR_DIR_NOT_EMPTY != GetLastError()) - return -1; - - /* Give whatever has a handle to a child item some time - * to release it before trying again */ - Sleep(RM_RETRY_DELAY * retries * retries); - } - - return 0; -} - -static void -fs_rmdir_helper(WCHAR *_wsource) -{ - WCHAR buffer[MAX_PATH]; - HANDLE find_handle; - WIN32_FIND_DATAW find_data; - size_t buffer_prefix_len; - - /* Set up the buffer and capture the length */ - wcscpy_s(buffer, MAX_PATH, _wsource); - wcscat_s(buffer, MAX_PATH, L"\\"); - buffer_prefix_len = wcslen(buffer); - - /* FindFirstFile needs a wildcard to match multiple items */ - wcscat_s(buffer, MAX_PATH, L"*"); - find_handle = FindFirstFileW(buffer, &find_data); - cl_assert(INVALID_HANDLE_VALUE != find_handle); - - do { - /* FindFirstFile/FindNextFile gives back . and .. - * entries at the beginning */ - if (fs__dotordotdot(find_data.cFileName)) - continue; - - wcscpy_s(buffer + buffer_prefix_len, MAX_PATH - buffer_prefix_len, find_data.cFileName); - - if (FILE_ATTRIBUTE_DIRECTORY & find_data.dwFileAttributes) - fs_rmdir_helper(buffer); - else { - /* If set, the +R bit must be cleared before deleting */ - if (FILE_ATTRIBUTE_READONLY & find_data.dwFileAttributes) - cl_assert(SetFileAttributesW(buffer, find_data.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY)); - - cl_assert(DeleteFileW(buffer)); - } - } - while (FindNextFileW(find_handle, &find_data)); - - /* Ensure that we successfully completed the enumeration */ - cl_assert(ERROR_NO_MORE_FILES == GetLastError()); - - /* Close the find handle */ - FindClose(find_handle); - - /* Now that the directory is empty, remove it */ - cl_assert(0 == fs_rmdir_rmdir(_wsource)); -} - -static int -fs_rm_wait(WCHAR *_wpath) -{ - unsigned retries = 1; - DWORD last_error; - - do { - if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(_wpath)) - last_error = GetLastError(); - else - last_error = ERROR_SUCCESS; - - /* Is the item gone? */ - if (ERROR_FILE_NOT_FOUND == last_error || - ERROR_PATH_NOT_FOUND == last_error) - return 0; - - Sleep(RM_RETRY_DELAY * retries * retries); - } - while (retries++ <= RM_RETRY_COUNT); - - return -1; -} - -static void -fs_rm(const char *_source) -{ - WCHAR wsource[MAX_PATH]; - DWORD attrs; - - /* The input path is UTF-8. Convert it to wide characters - * for use with the Windows API */ - cl_assert(MultiByteToWideChar(CP_UTF8, - MB_ERR_INVALID_CHARS, - _source, - -1, /* Indicates NULL termination */ - wsource, - MAX_PATH)); - - /* Does the item exist? If not, we have no work to do */ - attrs = GetFileAttributesW(wsource); - - if (INVALID_FILE_ATTRIBUTES == attrs) - return; - - if (FILE_ATTRIBUTE_DIRECTORY & attrs) - fs_rmdir_helper(wsource); - else { - /* The item is a file. Strip the +R bit */ - if (FILE_ATTRIBUTE_READONLY & attrs) - cl_assert(SetFileAttributesW(wsource, attrs & ~FILE_ATTRIBUTE_READONLY)); - - cl_assert(DeleteFileW(wsource)); - } - - /* Wait for the DeleteFile or RemoveDirectory call to complete */ - cl_assert(0 == fs_rm_wait(wsource)); -} - -static void -fs_copydir_helper(WCHAR *_wsource, WCHAR *_wdest) -{ - WCHAR buf_source[MAX_PATH], buf_dest[MAX_PATH]; - HANDLE find_handle; - WIN32_FIND_DATAW find_data; - size_t buf_source_prefix_len, buf_dest_prefix_len; - - wcscpy_s(buf_source, MAX_PATH, _wsource); - wcscat_s(buf_source, MAX_PATH, L"\\"); - buf_source_prefix_len = wcslen(buf_source); - - wcscpy_s(buf_dest, MAX_PATH, _wdest); - wcscat_s(buf_dest, MAX_PATH, L"\\"); - buf_dest_prefix_len = wcslen(buf_dest); - - /* Get an enumerator for the items in the source. */ - wcscat_s(buf_source, MAX_PATH, L"*"); - find_handle = FindFirstFileW(buf_source, &find_data); - cl_assert(INVALID_HANDLE_VALUE != find_handle); - - /* Create the target directory. */ - cl_assert(CreateDirectoryW(_wdest, NULL)); - - do { - /* FindFirstFile/FindNextFile gives back . and .. - * entries at the beginning */ - if (fs__dotordotdot(find_data.cFileName)) - continue; - - wcscpy_s(buf_source + buf_source_prefix_len, MAX_PATH - buf_source_prefix_len, find_data.cFileName); - wcscpy_s(buf_dest + buf_dest_prefix_len, MAX_PATH - buf_dest_prefix_len, find_data.cFileName); - - if (FILE_ATTRIBUTE_DIRECTORY & find_data.dwFileAttributes) - fs_copydir_helper(buf_source, buf_dest); - else - cl_assert(CopyFileW(buf_source, buf_dest, TRUE)); - } - while (FindNextFileW(find_handle, &find_data)); - - /* Ensure that we successfully completed the enumeration */ - cl_assert(ERROR_NO_MORE_FILES == GetLastError()); - - /* Close the find handle */ - FindClose(find_handle); -} - -static void -fs_copy(const char *_source, const char *_dest) -{ - WCHAR wsource[MAX_PATH], wdest[MAX_PATH]; - DWORD source_attrs, dest_attrs; - HANDLE find_handle; - WIN32_FIND_DATAW find_data; - - /* The input paths are UTF-8. Convert them to wide characters - * for use with the Windows API. */ - cl_assert(MultiByteToWideChar(CP_UTF8, - MB_ERR_INVALID_CHARS, - _source, - -1, - wsource, - MAX_PATH)); - - cl_assert(MultiByteToWideChar(CP_UTF8, - MB_ERR_INVALID_CHARS, - _dest, - -1, - wdest, - MAX_PATH)); - - /* Check the source for existence */ - source_attrs = GetFileAttributesW(wsource); - cl_assert(INVALID_FILE_ATTRIBUTES != source_attrs); - - /* Check the target for existence */ - dest_attrs = GetFileAttributesW(wdest); - - if (INVALID_FILE_ATTRIBUTES != dest_attrs) { - /* Target exists; append last path part of source to target. - * Use FindFirstFile to parse the path */ - find_handle = FindFirstFileW(wsource, &find_data); - cl_assert(INVALID_HANDLE_VALUE != find_handle); - wcscat_s(wdest, MAX_PATH, L"\\"); - wcscat_s(wdest, MAX_PATH, find_data.cFileName); - FindClose(find_handle); - - /* Check the new target for existence */ - cl_assert(INVALID_FILE_ATTRIBUTES == GetFileAttributesW(wdest)); - } - - if (FILE_ATTRIBUTE_DIRECTORY & source_attrs) - fs_copydir_helper(wsource, wdest); - else - cl_assert(CopyFileW(wsource, wdest, TRUE)); -} - -void -cl_fs_cleanup(void) -{ - fs_rm(fixture_path(_clar_path, "*")); -} - -#else -static int -shell_out(char * const argv[]) -{ - int status; - pid_t pid; - - pid = fork(); - - if (pid < 0) { - fprintf(stderr, - "System error: `fork()` call failed.\n"); - exit(-1); - } - - if (pid == 0) { - execv(argv[0], argv); - } - - waitpid(pid, &status, 0); - return WEXITSTATUS(status); -} - -static void -fs_copy(const char *_source, const char *dest) -{ - char *argv[5]; - char *source; - size_t source_len; - - source = strdup(_source); - source_len = strlen(source); - - if (source[source_len - 1] == '/') - source[source_len - 1] = 0; - - argv[0] = "/bin/cp"; - argv[1] = "-R"; - argv[2] = source; - argv[3] = (char *)dest; - argv[4] = NULL; - - cl_must_pass_( - shell_out(argv), - "Failed to copy test fixtures to sandbox" - ); - - free(source); -} - -static void -fs_rm(const char *source) -{ - char *argv[4]; - - argv[0] = "/bin/rm"; - argv[1] = "-Rf"; - argv[2] = (char *)source; - argv[3] = NULL; - - cl_must_pass_( - shell_out(argv), - "Failed to cleanup the sandbox" - ); -} - -void -cl_fs_cleanup(void) -{ - clar_unsandbox(); - clar_sandbox(); -} -#endif diff --git a/tests-clar/clar/print.h b/tests-clar/clar/print.h deleted file mode 100644 index 368016f2f30..00000000000 --- a/tests-clar/clar/print.h +++ /dev/null @@ -1,60 +0,0 @@ - -static void clar_print_init(int test_count, int suite_count, const char *suite_names) -{ - (void)test_count; - printf("Loaded %d suites: %s\n", (int)suite_count, suite_names); - printf("Started\n"); -} - -static void clar_print_shutdown(int test_count, int suite_count, int error_count) -{ - (void)test_count; - (void)suite_count; - (void)error_count; - - printf("\n\n"); - clar_report_errors(); -} - -static void clar_print_error(int num, const struct clar_error *error) -{ - printf(" %d) Failure:\n", num); - - printf("%s::%s [%s:%d]\n", - error->suite, - error->test, - error->file, - error->line_number); - - printf(" %s\n", error->error_msg); - - if (error->description != NULL) - printf(" %s\n", error->description); - - printf("\n"); - fflush(stdout); -} - -static void clar_print_ontest(const char *test_name, int test_number, int failed) -{ - (void)test_name; - (void)test_number; - printf("%c", failed ? 'F' : '.'); - fflush(stdout); -} - -static void clar_print_onsuite(const char *suite_name, int suite_index) -{ - if (_clar.report_suite_names) - printf("\n%s", suite_name); - - (void)suite_index; -} - -static void clar_print_onabort(const char *msg, ...) -{ - va_list argp; - va_start(argp, msg); - vfprintf(stderr, msg, argp); - va_end(argp); -} diff --git a/tests-clar/clar/sandbox.h b/tests-clar/clar/sandbox.h deleted file mode 100644 index bed3011fe88..00000000000 --- a/tests-clar/clar/sandbox.h +++ /dev/null @@ -1,127 +0,0 @@ -static char _clar_path[4096]; - -static int -is_valid_tmp_path(const char *path) -{ - STAT_T st; - - if (stat(path, &st) != 0) - return 0; - - if (!S_ISDIR(st.st_mode)) - return 0; - - return (access(path, W_OK) == 0); -} - -static int -find_tmp_path(char *buffer, size_t length) -{ -#ifndef _WIN32 - static const size_t var_count = 4; - static const char *env_vars[] = { - "TMPDIR", "TMP", "TEMP", "USERPROFILE" - }; - - size_t i; - - for (i = 0; i < var_count; ++i) { - const char *env = getenv(env_vars[i]); - if (!env) - continue; - - if (is_valid_tmp_path(env)) { - strncpy(buffer, env, length); - return 0; - } - } - - /* If the environment doesn't say anything, try to use /tmp */ - if (is_valid_tmp_path("/tmp")) { - strncpy(buffer, "/tmp", length); - return 0; - } - -#else - if (GetTempPath((DWORD)length, buffer)) - return 0; -#endif - - /* This system doesn't like us, try to use the current directory */ - if (is_valid_tmp_path(".")) { - strncpy(buffer, ".", length); - return 0; - } - - return -1; -} - -static void clar_unsandbox(void) -{ - if (_clar_path[0] == '\0') - return; - -#ifdef _WIN32 - chdir(".."); -#endif - - fs_rm(_clar_path); -} - -static int build_sandbox_path(void) -{ - const char path_tail[] = "clar_tmp_XXXXXX"; - size_t len; - - if (find_tmp_path(_clar_path, sizeof(_clar_path)) < 0) - return -1; - - len = strlen(_clar_path); - -#ifdef _WIN32 - { /* normalize path to POSIX forward slashes */ - size_t i; - for (i = 0; i < len; ++i) { - if (_clar_path[i] == '\\') - _clar_path[i] = '/'; - } - } -#endif - - if (_clar_path[len - 1] != '/') { - _clar_path[len++] = '/'; - } - - strncpy(_clar_path + len, path_tail, sizeof(_clar_path) - len); - -#if defined(__MINGW32__) - if (_mktemp(_clar_path) == NULL) - return -1; - - if (mkdir(_clar_path, 0700) != 0) - return -1; -#elif defined(_WIN32) - if (_mktemp_s(_clar_path, sizeof(_clar_path)) != 0) - return -1; - - if (mkdir(_clar_path, 0700) != 0) - return -1; -#else - if (mkdtemp(_clar_path) == NULL) - return -1; -#endif - - return 0; -} - -static int clar_sandbox(void) -{ - if (_clar_path[0] == '\0' && build_sandbox_path() < 0) - return -1; - - if (chdir(_clar_path) != 0) - return -1; - - return 0; -} - diff --git a/tests-clar/clar_libgit2.c b/tests-clar/clar_libgit2.c deleted file mode 100644 index 88ffb2bca24..00000000000 --- a/tests-clar/clar_libgit2.c +++ /dev/null @@ -1,321 +0,0 @@ -#include "clar_libgit2.h" -#include "posix.h" -#include "path.h" - -void cl_git_report_failure( - int error, const char *file, int line, const char *fncall) -{ - char msg[4096]; - const git_error *last = giterr_last(); - p_snprintf(msg, 4096, "error %d - %s", - error, last ? last->message : ""); - clar__assert(0, file, line, fncall, msg, 1); -} - -void cl_git_mkfile(const char *filename, const char *content) -{ - int fd; - - fd = p_creat(filename, 0666); - cl_assert(fd != 0); - - if (content) { - cl_must_pass(p_write(fd, content, strlen(content))); - } else { - cl_must_pass(p_write(fd, filename, strlen(filename))); - cl_must_pass(p_write(fd, "\n", 1)); - } - - cl_must_pass(p_close(fd)); -} - -void cl_git_write2file( - const char *filename, const char *new_content, int flags, unsigned int mode) -{ - int fd = p_open(filename, flags, mode); - cl_assert(fd >= 0); - if (!new_content) - new_content = "\n"; - cl_must_pass(p_write(fd, new_content, strlen(new_content))); - cl_must_pass(p_close(fd)); -} - -void cl_git_append2file(const char *filename, const char *new_content) -{ - cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_APPEND, 0644); -} - -void cl_git_rewritefile(const char *filename, const char *new_content) -{ - cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_TRUNC, 0644); -} - -#ifdef GIT_WIN32 - -#include "win32/utf-conv.h" - -char *cl_getenv(const char *name) -{ - wchar_t name_utf16[GIT_WIN_PATH]; - DWORD alloc_len; - wchar_t *value_utf16; - char *value_utf8; - - git__utf8_to_16(name_utf16, GIT_WIN_PATH, name); - alloc_len = GetEnvironmentVariableW(name_utf16, NULL, 0); - if (alloc_len <= 0) - return NULL; - - alloc_len = GIT_WIN_PATH; - cl_assert(value_utf16 = git__calloc(alloc_len, sizeof(wchar_t))); - - GetEnvironmentVariableW(name_utf16, value_utf16, alloc_len); - - cl_assert(value_utf8 = git__malloc(alloc_len)); - git__utf16_to_8(value_utf8, value_utf16); - - git__free(value_utf16); - - return value_utf8; -} - -int cl_setenv(const char *name, const char *value) -{ - wchar_t name_utf16[GIT_WIN_PATH]; - wchar_t value_utf16[GIT_WIN_PATH]; - - git__utf8_to_16(name_utf16, GIT_WIN_PATH, name); - - if (value != NULL) - git__utf8_to_16(value_utf16, GIT_WIN_PATH, value); - - /* Windows XP returns 0 (failed) when passing NULL for lpValue when lpName - * does not exist in the environment block. This behavior seems to have changed - * in later versions. Don't fail when SetEnvironmentVariable fails, if we passed - * NULL for lpValue. */ - cl_assert(SetEnvironmentVariableW(name_utf16, value ? value_utf16 : NULL) || !value); - return 0; -} - -/* This function performs retries on calls to MoveFile in order - * to provide enhanced reliability in the face of antivirus - * agents that may be scanning the source (or in the case that - * the source is a directory, a child of the source). */ -int cl_rename(const char *source, const char *dest) -{ - wchar_t source_utf16[GIT_WIN_PATH]; - wchar_t dest_utf16[GIT_WIN_PATH]; - unsigned retries = 1; - - git__utf8_to_16(source_utf16, GIT_WIN_PATH, source); - git__utf8_to_16(dest_utf16, GIT_WIN_PATH, dest); - - while (!MoveFileW(source_utf16, dest_utf16)) { - /* Only retry if the error is ERROR_ACCESS_DENIED; - * this may indicate that an antivirus agent is - * preventing the rename from source to target */ - if (retries > 5 || - ERROR_ACCESS_DENIED != GetLastError()) - return -1; - - /* With 5 retries and a coefficient of 10ms, the maximum - * delay here is 550 ms */ - Sleep(10 * retries * retries); - retries++; - } - - return 0; -} - -#else - -#include -char *cl_getenv(const char *name) -{ - return getenv(name); -} - -int cl_setenv(const char *name, const char *value) -{ - return (value == NULL) ? unsetenv(name) : setenv(name, value, 1); -} - -int cl_rename(const char *source, const char *dest) -{ - return p_rename(source, dest); -} - -#endif - -static const char *_cl_sandbox = NULL; -static git_repository *_cl_repo = NULL; - -git_repository *cl_git_sandbox_init(const char *sandbox) -{ - /* Copy the whole sandbox folder from our fixtures to our test sandbox - * area. After this it can be accessed with `./sandbox` - */ - cl_fixture_sandbox(sandbox); - _cl_sandbox = sandbox; - - cl_git_pass(p_chdir(sandbox)); - - /* If this is not a bare repo, then rename `sandbox/.gitted` to - * `sandbox/.git` which must be done since we cannot store a folder - * named `.git` inside the fixtures folder of our libgit2 repo. - */ - if (p_access(".gitted", F_OK) == 0) - cl_git_pass(cl_rename(".gitted", ".git")); - - /* If we have `gitattributes`, rename to `.gitattributes`. This may - * be necessary if we don't want the attributes to be applied in the - * libgit2 repo, but just during testing. - */ - if (p_access("gitattributes", F_OK) == 0) - cl_git_pass(cl_rename("gitattributes", ".gitattributes")); - - /* As with `gitattributes`, we may need `gitignore` just for testing. */ - if (p_access("gitignore", F_OK) == 0) - cl_git_pass(cl_rename("gitignore", ".gitignore")); - - cl_git_pass(p_chdir("..")); - - /* Now open the sandbox repository and make it available for tests */ - cl_git_pass(git_repository_open(&_cl_repo, sandbox)); - - return _cl_repo; -} - -void cl_git_sandbox_cleanup(void) -{ - if (_cl_repo) { - git_repository_free(_cl_repo); - _cl_repo = NULL; - } - if (_cl_sandbox) { - cl_fixture_cleanup(_cl_sandbox); - _cl_sandbox = NULL; - } -} - -bool cl_toggle_filemode(const char *filename) -{ - struct stat st1, st2; - - cl_must_pass(p_stat(filename, &st1)); - cl_must_pass(p_chmod(filename, st1.st_mode ^ 0100)); - cl_must_pass(p_stat(filename, &st2)); - - return (st1.st_mode != st2.st_mode); -} - -bool cl_is_chmod_supported(void) -{ - static int _is_supported = -1; - - if (_is_supported < 0) { - cl_git_mkfile("filemode.t", "Test if filemode can be modified"); - _is_supported = cl_toggle_filemode("filemode.t"); - cl_must_pass(p_unlink("filemode.t")); - } - - return _is_supported; -} - -const char* cl_git_fixture_url(const char *fixturename) -{ - return cl_git_path_url(cl_fixture(fixturename)); -} - -const char* cl_git_path_url(const char *path) -{ - static char url[4096]; - - const char *in_buf; - git_buf path_buf = GIT_BUF_INIT; - git_buf url_buf = GIT_BUF_INIT; - - cl_git_pass(git_path_prettify_dir(&path_buf, path, NULL)); - cl_git_pass(git_buf_puts(&url_buf, "file://")); - -#ifdef _MSC_VER - /* - * A FILE uri matches the following format: file://[host]/path - * where "host" can be empty and "path" is an absolute path to the resource. - * - * In this test, no hostname is used, but we have to ensure the leading triple slashes: - * - * *nix: file:///usr/home/... - * Windows: file:///C:/Users/... - */ - cl_git_pass(git_buf_putc(&url_buf, '/')); -#endif - - in_buf = git_buf_cstr(&path_buf); - - /* - * A very hacky Url encoding that only takes care of escaping the spaces - */ - while (*in_buf) { - if (*in_buf == ' ') - cl_git_pass(git_buf_puts(&url_buf, "%20")); - else - cl_git_pass(git_buf_putc(&url_buf, *in_buf)); - - in_buf++; - } - - strncpy(url, git_buf_cstr(&url_buf), 4096); - git_buf_free(&url_buf); - git_buf_free(&path_buf); - return url; -} - -typedef struct { - const char *filename; - size_t filename_len; -} remove_data; - -static int remove_placeholders_recurs(void *_data, git_buf *path) -{ - remove_data *data = (remove_data *)_data; - size_t pathlen; - - if (git_path_isdir(path->ptr) == true) - return git_path_direach(path, remove_placeholders_recurs, data); - - pathlen = path->size; - - if (pathlen < data->filename_len) - return 0; - - /* if path ends in '/'+filename (or equals filename) */ - if (!strcmp(data->filename, path->ptr + pathlen - data->filename_len) && - (pathlen == data->filename_len || - path->ptr[pathlen - data->filename_len - 1] == '/')) - return p_unlink(path->ptr); - - return 0; -} - -int cl_git_remove_placeholders(const char *directory_path, const char *filename) -{ - int error; - remove_data data; - git_buf buffer = GIT_BUF_INIT; - - if (git_path_isdir(directory_path) == false) - return -1; - - if (git_buf_sets(&buffer, directory_path) < 0) - return -1; - - data.filename = filename; - data.filename_len = strlen(filename); - - error = remove_placeholders_recurs(&data, &buffer); - - git_buf_free(&buffer); - - return error; -} diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h deleted file mode 100644 index 321ec5f2f37..00000000000 --- a/tests-clar/clar_libgit2.h +++ /dev/null @@ -1,71 +0,0 @@ -#ifndef __CLAR_LIBGIT2__ -#define __CLAR_LIBGIT2__ - -#include "clar.h" -#include -#include "common.h" - -/** - * Replace for `clar_must_pass` that passes the last library error as the - * test failure message. - * - * Use this wrapper around all `git_` library calls that return error codes! - */ -#define cl_git_pass(expr) do { \ - int _lg2_error; \ - giterr_clear(); \ - if ((_lg2_error = (expr)) != 0) \ - cl_git_report_failure(_lg2_error, __FILE__, __LINE__, "Function call failed: " #expr); \ - } while (0) - -/** - * Wrapper for `clar_must_fail` -- this one is - * just for consistency. Use with `git_` library - * calls that are supposed to fail! - */ -#define cl_git_fail(expr) cl_must_fail(expr) - -#define cl_git_fail_with(expr, error) cl_assert_equal_i(error,expr) - -void cl_git_report_failure(int, const char *, int, const char *); - -#define cl_assert_equal_sz(sz1,sz2) cl_assert((sz1) == (sz2)) - -/* - * Some utility macros for building long strings - */ -#define REP4(STR) STR STR STR STR -#define REP15(STR) REP4(STR) REP4(STR) REP4(STR) STR STR STR -#define REP16(STR) REP4(REP4(STR)) -#define REP256(STR) REP16(REP16(STR)) -#define REP1024(STR) REP4(REP256(STR)) - -/* Write the contents of a buffer to disk */ -void cl_git_mkfile(const char *filename, const char *content); -void cl_git_append2file(const char *filename, const char *new_content); -void cl_git_rewritefile(const char *filename, const char *new_content); -void cl_git_write2file(const char *filename, const char *new_content, int flags, unsigned int mode); - -bool cl_toggle_filemode(const char *filename); -bool cl_is_chmod_supported(void); - -/* Environment wrappers */ -char *cl_getenv(const char *name); -int cl_setenv(const char *name, const char *value); - -/* Reliable rename */ -int cl_rename(const char *source, const char *dest); - -/* Git sandbox setup helpers */ - -git_repository *cl_git_sandbox_init(const char *sandbox); -void cl_git_sandbox_cleanup(void); - -/* Local-repo url helpers */ -const char* cl_git_fixture_url(const char *fixturename); -const char* cl_git_path_url(const char *path); - -/* Test repository cleaner */ -int cl_git_remove_placeholders(const char *directory_path, const char *filename); - -#endif diff --git a/tests-clar/clone/empty.c b/tests-clar/clone/empty.c deleted file mode 100644 index 4e53557a065..00000000000 --- a/tests-clar/clone/empty.c +++ /dev/null @@ -1,69 +0,0 @@ -#include "clar_libgit2.h" - -#include "git2/clone.h" -#include "repository.h" - -static git_clone_options g_options; -static git_repository *g_repo; -static git_repository *g_repo_cloned; - -void test_clone_empty__initialize(void) -{ - git_repository *sandbox = cl_git_sandbox_init("empty_bare.git"); - cl_git_remove_placeholders(git_repository_path(sandbox), "dummy-marker.txt"); - - g_repo = NULL; - - memset(&g_options, 0, sizeof(git_clone_options)); - g_options.version = GIT_CLONE_OPTIONS_VERSION; -} - -void test_clone_empty__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -static void cleanup_repository(void *path) -{ - cl_fixture_cleanup((const char *)path); - - git_repository_free(g_repo_cloned); - g_repo_cloned = NULL; -} - -void test_clone_empty__can_clone_an_empty_local_repo_barely(void) -{ - char *local_name = "refs/heads/master"; - char tracking_name[1024]; - git_reference *ref; - - cl_set_cleanup(&cleanup_repository, "./empty"); - - g_options.bare = true; - cl_git_pass(git_clone(&g_repo_cloned, "./empty_bare.git", "./empty", &g_options)); - - /* Although the HEAD is orphaned... */ - cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo_cloned, local_name)); - - /* ...one can still retrieve the name of the remote tracking reference */ - cl_assert_equal_i(strlen("refs/remotes/origin/master") + 1, - git_branch_tracking_name(tracking_name, 1024, g_repo_cloned, local_name)); -} - -void test_clone_empty__can_clone_an_empty_local_repo(void) -{ - cl_set_cleanup(&cleanup_repository, "./empty"); - - cl_git_pass(git_clone(&g_repo_cloned, "./empty_bare.git", "./empty", &g_options)); -} - -void test_clone_empty__can_clone_an_empty_standard_repo(void) -{ - cl_git_sandbox_cleanup(); - g_repo = cl_git_sandbox_init("empty_standard_repo"); - cl_git_remove_placeholders(git_repository_path(g_repo), "dummy-marker.txt"); - - cl_set_cleanup(&cleanup_repository, "./empty"); - - cl_git_pass(git_clone(&g_repo_cloned, "./empty_standard_repo", "./empty", &g_options)); -} diff --git a/tests-clar/clone/nonetwork.c b/tests-clar/clone/nonetwork.c deleted file mode 100644 index 3d327cb1e19..00000000000 --- a/tests-clar/clone/nonetwork.c +++ /dev/null @@ -1,201 +0,0 @@ -#include "clar_libgit2.h" - -#include "git2/clone.h" -#include "repository.h" - -#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository" - -static git_clone_options g_options; -static git_repository *g_repo; -static git_reference* g_ref; -static git_remote* g_remote; - -void test_clone_nonetwork__initialize(void) -{ - git_checkout_opts dummy_opts = GIT_CHECKOUT_OPTS_INIT; - - g_repo = NULL; - - memset(&g_options, 0, sizeof(git_clone_options)); - g_options.version = GIT_CLONE_OPTIONS_VERSION; - g_options.checkout_opts = dummy_opts; - g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; -} - -void test_clone_nonetwork__cleanup(void) -{ - if (g_repo) { - git_repository_free(g_repo); - g_repo = NULL; - } - - if (g_ref) { - git_reference_free(g_ref); - g_ref = NULL; - } - - if (g_remote) { - git_remote_free(g_remote); - g_remote = NULL; - } - - cl_fixture_cleanup("./foo"); -} - -void test_clone_nonetwork__bad_url(void) -{ - /* Clone should clean up the mess if the URL isn't a git repository */ - cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); - cl_assert(!git_path_exists("./foo")); - g_options.bare = true; - cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); - cl_assert(!git_path_exists("./foo")); -} - -void test_clone_nonetwork__local(void) -{ - cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); -} - -void test_clone_nonetwork__local_absolute_path(void) -{ - const char *local_src; - local_src = cl_fixture("testrepo.git"); - cl_git_pass(git_clone(&g_repo, local_src, "./foo", &g_options)); -} - -void test_clone_nonetwork__local_bare(void) -{ - g_options.bare = true; - cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); -} - -void test_clone_nonetwork__fail_when_the_target_is_a_file(void) -{ - cl_git_mkfile("./foo", "Bar!"); - cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); -} - -void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void) -{ - p_mkdir("./foo", GIT_DIR_MODE); - cl_git_mkfile("./foo/bar", "Baz!"); - cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); -} - -void test_clone_nonetwork__custom_origin_name(void) -{ - g_options.remote_name = "my_origin"; - cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); - - cl_git_pass(git_remote_load(&g_remote, g_repo, "my_origin")); -} - -void test_clone_nonetwork__custom_push_url(void) -{ - const char *url = "http://example.com"; - - g_options.pushurl = url; - cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); - - cl_git_pass(git_remote_load(&g_remote, g_repo, "origin")); - cl_assert_equal_s(url, git_remote_pushurl(g_remote)); -} - -void test_clone_nonetwork__custom_fetch_spec(void) -{ - const git_refspec *actual_fs; - const char *spec = "+refs/heads/master:refs/heads/foo"; - - g_options.fetch_spec = spec; - cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); - - cl_git_pass(git_remote_load(&g_remote, g_repo, "origin")); - actual_fs = git_remote_fetchspec(g_remote); - cl_assert_equal_s("refs/heads/master", git_refspec_src(actual_fs)); - cl_assert_equal_s("refs/heads/foo", git_refspec_dst(actual_fs)); - - cl_git_pass(git_reference_lookup(&g_ref, g_repo, "refs/heads/foo")); -} - -void test_clone_nonetwork__custom_push_spec(void) -{ - const git_refspec *actual_fs; - const char *spec = "+refs/heads/master:refs/heads/foo"; - - g_options.push_spec = spec; - cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); - - cl_git_pass(git_remote_load(&g_remote, g_repo, "origin")); - actual_fs = git_remote_pushspec(g_remote); - cl_assert_equal_s("refs/heads/master", git_refspec_src(actual_fs)); - cl_assert_equal_s("refs/heads/foo", git_refspec_dst(actual_fs)); -} - -void test_clone_nonetwork__custom_autotag(void) -{ - git_strarray tags = {0}; - - g_options.remote_autotag = GIT_REMOTE_DOWNLOAD_TAGS_NONE; - cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); - - cl_git_pass(git_tag_list(&tags, g_repo)); - cl_assert_equal_sz(0, tags.count); - - git_strarray_free(&tags); -} - -void test_clone_nonetwork__cope_with_already_existing_directory(void) -{ - p_mkdir("./foo", GIT_DIR_MODE); - cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); -} - -void test_clone_nonetwork__can_prevent_the_checkout_of_a_standard_repo(void) -{ - git_buf path = GIT_BUF_INIT; - - g_options.checkout_opts.checkout_strategy = 0; - cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); - - cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt")); - cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&path))); - - git_buf_free(&path); -} - -void test_clone_nonetwork__can_checkout_given_branch(void) -{ - g_options.checkout_branch = "test"; - cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); - - cl_assert_equal_i(0, git_repository_head_orphan(g_repo)); - - cl_git_pass(git_repository_head(&g_ref, g_repo)); - cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test"); -} - -void test_clone_nonetwork__can_detached_head(void) -{ - git_object *commit; - git_repository *cloned; - git_reference *cloned_head; - - cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); - - cl_git_pass(git_revparse_single(&commit, g_repo, "master~1")); - cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(commit))); - - cl_git_pass(git_clone(&cloned, "./foo", "./foo1", &g_options)); - - cl_assert(git_repository_head_detached(cloned)); - - cl_git_pass(git_repository_head(&cloned_head, cloned)); - cl_assert(!git_oid_cmp(git_object_id(commit), git_reference_target(cloned_head))); - - git_commit_free((git_commit*)commit); - git_reference_free(cloned_head); - git_repository_free(cloned); - - cl_fixture_cleanup("./foo1"); -} diff --git a/tests-clar/commit/commit.c b/tests-clar/commit/commit.c deleted file mode 100644 index 8f071ff942c..00000000000 --- a/tests-clar/commit/commit.c +++ /dev/null @@ -1,46 +0,0 @@ -#include "clar_libgit2.h" - -static git_repository *_repo; - -void test_commit_commit__initialize(void) -{ - cl_fixture_sandbox("testrepo.git"); - cl_git_pass(git_repository_open(&_repo, "testrepo.git")); -} - -void test_commit_commit__cleanup(void) -{ - git_repository_free(_repo); - _repo = NULL; - - cl_fixture_cleanup("testrepo.git"); -} - -void test_commit_commit__create_unexisting_update_ref(void) -{ - git_oid oid; - git_tree *tree; - git_commit *commit; - git_signature *s; - git_reference *ref; - - git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - cl_git_pass(git_commit_lookup(&commit, _repo, &oid)); - - git_oid_fromstr(&oid, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); - cl_git_pass(git_tree_lookup(&tree, _repo, &oid)); - - cl_git_pass(git_signature_now(&s, "alice", "alice@example.com")); - - cl_git_fail(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar")); - cl_git_pass(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s, - NULL, "some msg", tree, 1, (const git_commit **) &commit)); - - cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar")); - cl_assert(!git_oid_cmp(&oid, git_reference_target(ref))); - - git_tree_free(tree); - git_commit_free(commit); - git_signature_free(s); - git_reference_free(ref); -} diff --git a/tests-clar/commit/parse.c b/tests-clar/commit/parse.c deleted file mode 100644 index 9d291bbc2fe..00000000000 --- a/tests-clar/commit/parse.c +++ /dev/null @@ -1,374 +0,0 @@ -#include "clar_libgit2.h" -#include -#include "commit.h" -#include "signature.h" - -// Fixture setup -static git_repository *g_repo; -void test_commit_parse__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} -void test_commit_parse__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - - -// Header parsing -typedef struct { - const char *line; - const char *header; -} parse_test_case; - -static parse_test_case passing_header_cases[] = { - { "parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "parent " }, - { "tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " }, - { "random_heading 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "random_heading " }, - { "stuck_heading05452d6349abcd67aa396dfb28660d765d8b2a36\n", "stuck_heading" }, - { "tree 5F4BEFFC0759261D015AA63A3A85613FF2F235DE\n", "tree " }, - { "tree 1A669B8AB81B5EB7D9DB69562D34952A38A9B504\n", "tree " }, - { "tree 5B20DCC6110FCC75D31C6CEDEBD7F43ECA65B503\n", "tree " }, - { "tree 173E7BF00EA5C33447E99E6C1255954A13026BE4\n", "tree " }, - { NULL, NULL } -}; - -static parse_test_case failing_header_cases[] = { - { "parent 05452d6349abcd67aa396dfb28660d765d8b2a36", "parent " }, - { "05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " }, - { "parent05452d6349abcd67aa396dfb28660d765d8b2a6a\n", "parent " }, - { "parent 05452d6349abcd67aa396dfb280d765d8b2a6\n", "parent " }, - { "tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " }, - { "parent 0545xd6349abcd67aa396dfb28660d765d8b2a36\n", "parent " }, - { "parent 0545xd6349abcd67aa396dfb28660d765d8b2a36FF\n", "parent " }, - { "", "tree " }, - { "", "" }, - { NULL, NULL } -}; - -void test_commit_parse__header(void) -{ - git_oid oid; - - parse_test_case *testcase; - for (testcase = passing_header_cases; testcase->line != NULL; testcase++) - { - const char *line = testcase->line; - const char *line_end = line + strlen(line); - - cl_git_pass(git_oid__parse(&oid, &line, line_end, testcase->header)); - cl_assert(line == line_end); - } - - for (testcase = failing_header_cases; testcase->line != NULL; testcase++) - { - const char *line = testcase->line; - const char *line_end = line + strlen(line); - - cl_git_fail(git_oid__parse(&oid, &line, line_end, testcase->header)); - } -} - - -// Signature parsing -typedef struct { - const char *string; - const char *header; - const char *name; - const char *email; - git_time_t time; - int offset; -} passing_signature_test_case; - -passing_signature_test_case passing_signature_cases[] = { - {"author Vicent Marti 12345 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 12345, 0}, - {"author Vicent Marti <> 12345 \n", "author ", "Vicent Marti", "", 12345, 0}, - {"author Vicent Marti 231301 +1020\n", "author ", "Vicent Marti", "tanoku@gmail.com", 231301, 620}, - {"author Vicent Marti with an outrageously long name which will probably overflow the buffer 12345 \n", "author ", "Vicent Marti with an outrageously long name which will probably overflow the buffer", "tanoku@gmail.com", 12345, 0}, - {"author Vicent Marti 12345 \n", "author ", "Vicent Marti", "tanokuwithaveryveryverylongemailwhichwillprobablyvoverflowtheemailbuffer@gmail.com", 12345, 0}, - {"committer Vicent Marti 123456 +0000 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 0}, - {"committer Vicent Marti 123456 +0100 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 60}, - {"committer Vicent Marti 123456 -0100 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, -60}, - // Parse a signature without an author field - {"committer 123456 -0100 \n", "committer ", "", "tanoku@gmail.com", 123456, -60}, - // Parse a signature without an author field - {"committer 123456 -0100 \n", "committer ", "", "tanoku@gmail.com", 123456, -60}, - // Parse a signature with an empty author field - {"committer 123456 -0100 \n", "committer ", "", "tanoku@gmail.com", 123456, -60}, - // Parse a signature with an empty email field - {"committer Vicent Marti <> 123456 -0100 \n", "committer ", "Vicent Marti", "", 123456, -60}, - // Parse a signature with an empty email field - {"committer Vicent Marti < > 123456 -0100 \n", "committer ", "Vicent Marti", "", 123456, -60}, - // Parse a signature with empty name and email - {"committer <> 123456 -0100 \n", "committer ", "", "", 123456, -60}, - // Parse a signature with empty name and email - {"committer <> 123456 -0100 \n", "committer ", "", "", 123456, -60}, - // Parse a signature with empty name and email - {"committer < > 123456 -0100 \n", "committer ", "", "", 123456, -60}, - // Parse an obviously invalid signature - {"committer foo<@bar> 123456 -0100 \n", "committer ", "foo", "@bar", 123456, -60}, - // Parse an obviously invalid signature - {"committer foo<@bar>123456 -0100 \n", "committer ", "foo", "@bar", 123456, -60}, - // Parse an obviously invalid signature - {"committer <>\n", "committer ", "", "", 0, 0}, - {"committer Vicent Marti 123456 -1500 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, - {"committer Vicent Marti 123456 +0163 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, - {"author Vicent Marti notime \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, - {"author Vicent Marti 123456 notimezone \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, - {"author Vicent Marti notime +0100\n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, - {"author Vicent Marti \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, - {"author A U Thor , C O. Miter 1234567890 -0700\n", "author ", "A U Thor", "author@example.com", 1234567890, -420}, - {"author A U Thor and others 1234567890 -0700\n", "author ", "A U Thor", "author@example.com", 1234567890, -420}, - {"author A U Thor and others 1234567890\n", "author ", "A U Thor", "author@example.com", 1234567890, 0}, - {"author A U Thor> and others 1234567890\n", "author ", "A U Thor>", "author@example.com", 1234567890, 0}, - /* a variety of dates */ - {"author Vicent Marti 0 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, - {"author Vicent Marti 1234567890 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 1234567890, 0}, - {"author Vicent Marti 2147483647 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0x7fffffff, 0}, - {"author Vicent Marti 4294967295 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0xffffffff, 0}, - {"author Vicent Marti 4294967296 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 4294967296, 0}, - {"author Vicent Marti 8589934592 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 8589934592, 0}, - - {NULL,NULL,NULL,NULL,0,0} -}; - -typedef struct { - const char *string; - const char *header; -} failing_signature_test_case; - -failing_signature_test_case failing_signature_cases[] = { - {"committer Vicent Marti tanoku@gmail.com> 123456 -0100 \n", "committer "}, - {"author Vicent Marti 12345 \n", "author "}, - {"author Vicent Marti 12345 \n", "committer "}, - {"author Vicent Marti 12345 \n", "author "}, - {"author Vicent Marti <\n", "committer "}, - {"author ", "author "}, - {NULL,NULL,} -}; - -void test_commit_parse__signature(void) -{ - passing_signature_test_case *passcase; - failing_signature_test_case *failcase; - - for (passcase = passing_signature_cases; passcase->string != NULL; passcase++) - { - const char *str = passcase->string; - size_t len = strlen(passcase->string); - struct git_signature person = {0}; - cl_git_pass(git_signature__parse(&person, &str, str + len, passcase->header, '\n')); - cl_assert_equal_s(passcase->name, person.name); - cl_assert_equal_s(passcase->email, person.email); - cl_assert(passcase->time == person.when.time); - cl_assert(passcase->offset == person.when.offset); - git__free(person.name); git__free(person.email); - } - - for (failcase = failing_signature_cases; failcase->string != NULL; failcase++) - { - const char *str = failcase->string; - size_t len = strlen(failcase->string); - git_signature person = {0}; - cl_git_fail(git_signature__parse(&person, &str, str + len, failcase->header, '\n')); - git__free(person.name); git__free(person.email); - } -} - - - -static char *failing_commit_cases[] = { -// empty commit -"", -// random garbage -"asd97sa9du902e9a0jdsuusad09as9du098709aweu8987sd\n", -// broken endlines 1 -"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\r\n\ -parent 05452d6349abcd67aa396dfb28660d765d8b2a36\r\n\ -author Vicent Marti 1273848544 +0200\r\n\ -committer Vicent Marti 1273848544 +0200\r\n\ -\r\n\ -a test commit with broken endlines\r\n", -// broken endlines 2 -"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\ -parent 05452d6349abcd67aa396dfb28660d765d8b2a36\ -author Vicent Marti 1273848544 +0200\ -committer Vicent Marti 1273848544 +0200\ -\ -another test commit with broken endlines", -// starting endlines -"\ntree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\ -parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\ -author Vicent Marti 1273848544 +0200\n\ -committer Vicent Marti 1273848544 +0200\n\ -\n\ -a test commit with a starting endline\n", -// corrupted commit 1 -"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\ -parent 05452d6349abcd67aa396df", -// corrupted commit 2 -"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\ -parent ", -// corrupted commit 3 -"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\ -parent ", -// corrupted commit 4 -"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\ -par", -}; - - -static char *passing_commit_cases[] = { -// simple commit with no message -"tree 1810dff58d8a660512d4832e740f692884338ccd\n\ -author Vicent Marti 1273848544 +0200\n\ -committer Vicent Marti 1273848544 +0200\n\ -\n", -// simple commit, no parent -"tree 1810dff58d8a660512d4832e740f692884338ccd\n\ -author Vicent Marti 1273848544 +0200\n\ -committer Vicent Marti 1273848544 +0200\n\ -\n\ -a simple commit which works\n", -// simple commit, no parent, no newline in message -"tree 1810dff58d8a660512d4832e740f692884338ccd\n\ -author Vicent Marti 1273848544 +0200\n\ -committer Vicent Marti 1273848544 +0200\n\ -\n\ -a simple commit which works", -// simple commit, 1 parent -"tree 1810dff58d8a660512d4832e740f692884338ccd\n\ -parent e90810b8df3e80c413d903f631643c716887138d\n\ -author Vicent Marti 1273848544 +0200\n\ -committer Vicent Marti 1273848544 +0200\n\ -\n\ -a simple commit which works\n", -/* simple commit with GPG signature */ -"tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\ -parent 34734e478d6cf50c27c9d69026d93974d052c454\n\ -author Ben Burkert 1358451456 -0800\n\ -committer Ben Burkert 1358451456 -0800\n\ -gpgsig -----BEGIN PGP SIGNATURE-----\n\ - Version: GnuPG v1.4.12 (Darwin)\n\ - \n\ - iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\ - o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\ - JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\ - AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\ - SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\ - who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\ - 6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\ - cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\ - c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\ - ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\ - 7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\ - cpxtDQQMGYFpXK/71stq\n\ - =ozeK\n\ - -----END PGP SIGNATURE-----\n\ -\n\ -a simple commit which works\n", -}; - -void test_commit_parse__entire_commit(void) -{ - const int broken_commit_count = sizeof(failing_commit_cases) / sizeof(*failing_commit_cases); - const int working_commit_count = sizeof(passing_commit_cases) / sizeof(*passing_commit_cases); - int i; - - for (i = 0; i < broken_commit_count; ++i) { - git_commit *commit; - commit = (git_commit*)git__malloc(sizeof(git_commit)); - memset(commit, 0x0, sizeof(git_commit)); - commit->object.repo = g_repo; - - cl_git_fail(git_commit__parse_buffer( - commit, failing_commit_cases[i], strlen(failing_commit_cases[i])) - ); - - git_commit__free(commit); - } - - for (i = 0; i < working_commit_count; ++i) { - git_commit *commit; - - commit = (git_commit*)git__malloc(sizeof(git_commit)); - memset(commit, 0x0, sizeof(git_commit)); - commit->object.repo = g_repo; - - cl_git_pass(git_commit__parse_buffer( - commit, - passing_commit_cases[i], - strlen(passing_commit_cases[i])) - ); - - if (!i) - cl_assert_equal_s("\n", git_commit_message(commit)); - else - cl_assert(git__prefixcmp( - git_commit_message(commit), "a simple commit which works") == 0); - - git_commit__free(commit); - } -} - - -// query the details on a parsed commit -void test_commit_parse__details0(void) { - static const char *commit_ids[] = { - "a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */ - "9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */ - "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */ - "c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */ - "8496071c1b46c854b31185ea97743be6a8774479", /* 4 */ - "5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */ - "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", /* 6 */ - }; - const size_t commit_count = sizeof(commit_ids) / sizeof(const char *); - unsigned int i; - - for (i = 0; i < commit_count; ++i) { - git_oid id; - git_commit *commit; - - const git_signature *author, *committer; - const char *message; - git_time_t commit_time; - unsigned int parents, p; - git_commit *parent = NULL, *old_parent = NULL; - - git_oid_fromstr(&id, commit_ids[i]); - - cl_git_pass(git_commit_lookup(&commit, g_repo, &id)); - - message = git_commit_message(commit); - author = git_commit_author(commit); - committer = git_commit_committer(commit); - commit_time = git_commit_time(commit); - parents = git_commit_parentcount(commit); - - cl_assert_equal_s("Scott Chacon", author->name); - cl_assert_equal_s("schacon@gmail.com", author->email); - cl_assert_equal_s("Scott Chacon", committer->name); - cl_assert_equal_s("schacon@gmail.com", committer->email); - cl_assert(message != NULL); - cl_assert(strchr(message, '\n') != NULL); - cl_assert(commit_time > 0); - cl_assert(parents <= 2); - for (p = 0;p < parents;p++) { - if (old_parent != NULL) - git_commit_free(old_parent); - - old_parent = parent; - cl_git_pass(git_commit_parent(&parent, commit, p)); - cl_assert(parent != NULL); - cl_assert(git_commit_author(parent) != NULL); // is it really a commit? - } - git_commit_free(old_parent); - git_commit_free(parent); - - cl_git_fail(git_commit_parent(&parent, commit, parents)); - git_commit_free(commit); - } -} - diff --git a/tests-clar/commit/signature.c b/tests-clar/commit/signature.c deleted file mode 100644 index 9364efb1066..00000000000 --- a/tests-clar/commit/signature.c +++ /dev/null @@ -1,79 +0,0 @@ -#include "clar_libgit2.h" - -static int try_build_signature(const char *name, const char *email, git_time_t time, int offset) -{ - git_signature *sign; - int error = 0; - - if ((error = git_signature_new(&sign, name, email, time, offset)) < 0) - return error; - - git_signature_free((git_signature *)sign); - - return error; -} - -static void assert_name_and_email( - const char *expected_name, - const char *expected_email, - const char *name, - const char *email) -{ - git_signature *sign; - - cl_git_pass(git_signature_new(&sign, name, email, 1234567890, 60)); - cl_assert_equal_s(expected_name, sign->name); - cl_assert_equal_s(expected_email, sign->email); - - git_signature_free(sign); -} - -void test_commit_signature__leading_and_trailing_spaces_are_trimmed(void) -{ - assert_name_and_email("nulltoken", "emeric.fermas@gmail.com", " nulltoken ", " emeric.fermas@gmail.com "); -} - -void test_commit_signature__angle_brackets_in_names_are_not_supported(void) -{ - cl_git_fail(try_build_signature("Haack", "phil@haack", 1234567890, 60)); - cl_git_fail(try_build_signature("", "phil@haack", 1234567890, 60)); -} - -void test_commit_signature__angle_brackets_in_email_are_not_supported(void) -{ - cl_git_fail(try_build_signature("Phil Haack", ">phil@haack", 1234567890, 60)); - cl_git_fail(try_build_signature("Phil Haack", "phil@>haack", 1234567890, 60)); - cl_git_fail(try_build_signature("Phil Haack", "", 1234567890, 60)); -} - -void test_commit_signature__create_empties(void) -{ - // can not create a signature with empty name or email - cl_git_pass(try_build_signature("nulltoken", "emeric.fermas@gmail.com", 1234567890, 60)); - - cl_git_fail(try_build_signature("", "emeric.fermas@gmail.com", 1234567890, 60)); - cl_git_fail(try_build_signature(" ", "emeric.fermas@gmail.com", 1234567890, 60)); - cl_git_fail(try_build_signature("nulltoken", "", 1234567890, 60)); - cl_git_fail(try_build_signature("nulltoken", " ", 1234567890, 60)); -} - -void test_commit_signature__create_one_char(void) -{ - // creating a one character signature - assert_name_and_email("x", "foo@bar.baz", "x", "foo@bar.baz"); -} - -void test_commit_signature__create_two_char(void) -{ - // creating a two character signature - assert_name_and_email("xx", "foo@bar.baz", "xx", "foo@bar.baz"); -} - -void test_commit_signature__create_zero_char(void) -{ - // creating a zero character signature - git_signature *sign; - cl_git_fail(git_signature_new(&sign, "", "x@y.z", 1234567890, 60)); - cl_assert(sign == NULL); -} diff --git a/tests-clar/commit/write.c b/tests-clar/commit/write.c deleted file mode 100644 index 88e2f32fbda..00000000000 --- a/tests-clar/commit/write.c +++ /dev/null @@ -1,146 +0,0 @@ -#include "clar_libgit2.h" - -static const char *committer_name = "Vicent Marti"; -static const char *committer_email = "vicent@github.com"; -static const char *commit_message = "This commit has been created in memory\n\ - This is a commit created in memory and it will be written back to disk\n"; -static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd"; -static const char *root_commit_message = "This is a root commit\n\ - This is a root commit and should be the only one in this branch\n"; -static char *head_old; -static git_reference *head, *branch; -static git_commit *commit; - -// Fixture setup -static git_repository *g_repo; -void test_commit_write__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_commit_write__cleanup(void) -{ - git_reference_free(head); - head = NULL; - - git_reference_free(branch); - branch = NULL; - - git_commit_free(commit); - commit = NULL; - - git__free(head_old); - head_old = NULL; - - cl_git_sandbox_cleanup(); -} - - -// write a new commit object from memory to disk -void test_commit_write__from_memory(void) -{ - git_oid tree_id, parent_id, commit_id; - git_signature *author, *committer; - const git_signature *author1, *committer1; - git_commit *parent; - git_tree *tree; - const char *commit_id_str = "8496071c1b46c854b31185ea97743be6a8774479"; - - git_oid_fromstr(&tree_id, tree_oid); - cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); - - git_oid_fromstr(&parent_id, commit_id_str); - cl_git_pass(git_commit_lookup(&parent, g_repo, &parent_id)); - - /* create signatures */ - cl_git_pass(git_signature_new(&committer, committer_name, committer_email, 123456789, 60)); - cl_git_pass(git_signature_new(&author, committer_name, committer_email, 987654321, 90)); - - cl_git_pass(git_commit_create_v( - &commit_id, /* out id */ - g_repo, - NULL, /* do not update the HEAD */ - author, - committer, - NULL, - commit_message, - tree, - 1, parent)); - - git_object_free((git_object *)parent); - git_object_free((git_object *)tree); - - git_signature_free(committer); - git_signature_free(author); - - cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id)); - - /* Check attributes were set correctly */ - author1 = git_commit_author(commit); - cl_assert(author1 != NULL); - cl_assert_equal_s(committer_name, author1->name); - cl_assert_equal_s(committer_email, author1->email); - cl_assert(author1->when.time == 987654321); - cl_assert(author1->when.offset == 90); - - committer1 = git_commit_committer(commit); - cl_assert(committer1 != NULL); - cl_assert_equal_s(committer_name, committer1->name); - cl_assert_equal_s(committer_email, committer1->email); - cl_assert(committer1->when.time == 123456789); - cl_assert(committer1->when.offset == 60); - - cl_assert_equal_s(commit_message, git_commit_message(commit)); -} - -// create a root commit -void test_commit_write__root(void) -{ - git_oid tree_id, commit_id; - const git_oid *branch_oid; - git_signature *author, *committer; - const char *branch_name = "refs/heads/root-commit-branch"; - git_tree *tree; - - git_oid_fromstr(&tree_id, tree_oid); - cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); - - /* create signatures */ - cl_git_pass(git_signature_new(&committer, committer_name, committer_email, 123456789, 60)); - cl_git_pass(git_signature_new(&author, committer_name, committer_email, 987654321, 90)); - - /* First we need to update HEAD so it points to our non-existant branch */ - cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD")); - cl_assert(git_reference_type(head) == GIT_REF_SYMBOLIC); - head_old = git__strdup(git_reference_symbolic_target(head)); - cl_assert(head_old != NULL); - - cl_git_pass(git_reference_symbolic_set_target(head, branch_name)); - - cl_git_pass(git_commit_create_v( - &commit_id, /* out id */ - g_repo, - "HEAD", - author, - committer, - NULL, - root_commit_message, - tree, - 0)); - - git_object_free((git_object *)tree); - git_signature_free(committer); - git_signature_free(author); - - /* - * The fact that creating a commit works has already been - * tested. Here we just make sure it's our commit and that it was - * written as a root commit. - */ - cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id)); - cl_assert(git_commit_parentcount(commit) == 0); - cl_git_pass(git_reference_lookup(&branch, g_repo, branch_name)); - branch_oid = git_reference_target(branch); - cl_git_pass(git_oid_cmp(branch_oid, &commit_id)); - cl_assert_equal_s(root_commit_message, git_commit_message(commit)); -} diff --git a/tests-clar/config/backend.c b/tests-clar/config/backend.c deleted file mode 100644 index 28502a8babd..00000000000 --- a/tests-clar/config/backend.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "clar_libgit2.h" - -void test_config_backend__checks_version(void) -{ - git_config *cfg; - git_config_backend backend = GIT_CONFIG_BACKEND_INIT; - const git_error *err; - - backend.version = 1024; - - cl_git_pass(git_config_new(&cfg)); - cl_git_fail(git_config_add_backend(cfg, &backend, 0, false)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); - - giterr_clear(); - backend.version = 1024; - cl_git_fail(git_config_add_backend(cfg, &backend, 0, false)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); - - git_config_free(cfg); -} diff --git a/tests-clar/config/config_helpers.c b/tests-clar/config/config_helpers.c deleted file mode 100644 index 53bd945a0da..00000000000 --- a/tests-clar/config/config_helpers.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "clar_libgit2.h" -#include "config_helpers.h" -#include "repository.h" - -void assert_config_entry_existence( - git_repository *repo, - const char *name, - bool is_supposed_to_exist) -{ - git_config *config; - const char *out; - int result; - - cl_git_pass(git_repository_config__weakptr(&config, repo)); - - result = git_config_get_string(&out, config, name); - - if (is_supposed_to_exist) - cl_git_pass(result); - else - cl_assert_equal_i(GIT_ENOTFOUND, result); -} - -void assert_config_entry_value( - git_repository *repo, - const char *name, - const char *expected_value) -{ - git_config *config; - const char *out; - - cl_git_pass(git_repository_config__weakptr(&config, repo)); - - cl_git_pass(git_config_get_string(&out, config, name)); - - cl_assert_equal_s(expected_value, out); -} diff --git a/tests-clar/config/config_helpers.h b/tests-clar/config/config_helpers.h deleted file mode 100644 index b887b3d3800..00000000000 --- a/tests-clar/config/config_helpers.h +++ /dev/null @@ -1,9 +0,0 @@ -extern void assert_config_entry_existence( - git_repository *repo, - const char *name, - bool is_supposed_to_exist); - -extern void assert_config_entry_value( - git_repository *repo, - const char *name, - const char *expected_value); diff --git a/tests-clar/config/configlevel.c b/tests-clar/config/configlevel.c deleted file mode 100644 index 1c22e8d9f59..00000000000 --- a/tests-clar/config/configlevel.c +++ /dev/null @@ -1,71 +0,0 @@ -#include "clar_libgit2.h" - -void test_config_configlevel__adding_the_same_level_twice_returns_EEXISTS(void) -{ - int error; - git_config *cfg; - - cl_git_pass(git_config_new(&cfg)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), - GIT_CONFIG_LEVEL_LOCAL, 0)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"), - GIT_CONFIG_LEVEL_GLOBAL, 0)); - error = git_config_add_file_ondisk(cfg, cl_fixture("config/config16"), - GIT_CONFIG_LEVEL_GLOBAL, 0); - - cl_git_fail(error); - cl_assert_equal_i(GIT_EEXISTS, error); - - git_config_free(cfg); -} - -void test_config_configlevel__can_replace_a_config_file_at_an_existing_level(void) -{ - git_config *cfg; - const char *s; - - cl_git_pass(git_config_new(&cfg)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"), - GIT_CONFIG_LEVEL_LOCAL, 1)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"), - GIT_CONFIG_LEVEL_LOCAL, 1)); - - cl_git_pass(git_config_get_string(&s, cfg, "core.stringglobal")); - cl_assert_equal_s("don't find me!", s); - - git_config_free(cfg); -} - -void test_config_configlevel__can_read_from_a_single_level_focused_file_after_parent_config_has_been_freed(void) -{ - git_config *cfg; - git_config *single_level_cfg; - const char *s; - - cl_git_pass(git_config_new(&cfg)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"), - GIT_CONFIG_LEVEL_GLOBAL, 0)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"), - GIT_CONFIG_LEVEL_LOCAL, 0)); - - cl_git_pass(git_config_open_level(&single_level_cfg, cfg, GIT_CONFIG_LEVEL_LOCAL)); - - git_config_free(cfg); - - cl_git_pass(git_config_get_string(&s, single_level_cfg, "core.stringglobal")); - cl_assert_equal_s("don't find me!", s); - - git_config_free(single_level_cfg); -} - -void test_config_configlevel__fetching_a_level_from_an_empty_compound_config_returns_ENOTFOUND(void) -{ - git_config *cfg; - git_config *local_cfg; - - cl_git_pass(git_config_new(&cfg)); - - cl_assert_equal_i(GIT_ENOTFOUND, git_config_open_level(&local_cfg, cfg, GIT_CONFIG_LEVEL_LOCAL)); - - git_config_free(cfg); -} diff --git a/tests-clar/config/multivar.c b/tests-clar/config/multivar.c deleted file mode 100644 index 26537e20a5d..00000000000 --- a/tests-clar/config/multivar.c +++ /dev/null @@ -1,149 +0,0 @@ -#include "clar_libgit2.h" - -static const char *_name = "remote.fancy.url"; - -void test_config_multivar__initialize(void) -{ - cl_fixture_sandbox("config"); -} - -void test_config_multivar__cleanup(void) -{ - cl_fixture_cleanup("config"); -} - -static int mv_read_cb(const git_config_entry *entry, void *data) -{ - int *n = (int *) data; - - if (!strcmp(entry->name, _name)) - (*n)++; - - return 0; -} - -void test_config_multivar__foreach(void) -{ - git_config *cfg; - int n = 0; - - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config11"))); - - cl_git_pass(git_config_foreach(cfg, mv_read_cb, &n)); - cl_assert(n == 2); - - git_config_free(cfg); -} - -static int cb(const git_config_entry *entry, void *data) -{ - int *n = (int *) data; - - GIT_UNUSED(entry); - - (*n)++; - - return 0; -} - -void test_config_multivar__get(void) -{ - git_config *cfg; - int n; - - cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); - - n = 0; - cl_git_pass(git_config_get_multivar(cfg, _name, NULL, cb, &n)); - cl_assert(n == 2); - - n = 0; - cl_git_pass(git_config_get_multivar(cfg, _name, "example", cb, &n)); - cl_assert(n == 1); - - git_config_free(cfg); -} - -void test_config_multivar__add(void) -{ - git_config *cfg; - int n; - - cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); - cl_git_pass(git_config_set_multivar(cfg, _name, "nonexistant", "git://git.otherplace.org/libgit2")); - - n = 0; - cl_git_pass(git_config_get_multivar(cfg, _name, NULL, cb, &n)); - cl_assert(n == 3); - - n = 0; - cl_git_pass(git_config_get_multivar(cfg, _name, "otherplace", cb, &n)); - cl_assert(n == 1); - - git_config_free(cfg); - - /* We know it works in memory, let's see if the file is written correctly */ - - cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); - - n = 0; - cl_git_pass(git_config_get_multivar(cfg, _name, NULL, cb, &n)); - cl_assert(n == 3); - - n = 0; - cl_git_pass(git_config_get_multivar(cfg, _name, "otherplace", cb, &n)); - cl_assert(n == 1); - - git_config_free(cfg); -} - -void test_config_multivar__replace(void) -{ - git_config *cfg; - int n; - - cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); - - n = 0; - cl_git_pass(git_config_get_multivar(cfg, _name, NULL, cb, &n)); - cl_assert(n == 2); - - cl_git_pass(git_config_set_multivar(cfg, _name, "github", "git://git.otherplace.org/libgit2")); - - n = 0; - cl_git_pass(git_config_get_multivar(cfg, _name, NULL, cb, &n)); - cl_assert(n == 2); - - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); - - n = 0; - cl_git_pass(git_config_get_multivar(cfg, _name, NULL, cb, &n)); - cl_assert(n == 2); - - git_config_free(cfg); -} - -void test_config_multivar__replace_multiple(void) -{ - git_config *cfg; - int n; - - cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); - cl_git_pass(git_config_set_multivar(cfg, _name, "git://", "git://git.otherplace.org/libgit2")); - - n = 0; - cl_git_pass(git_config_get_multivar(cfg, _name, "otherplace", cb, &n)); - cl_assert(n == 2); - - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); - - n = 0; - cl_git_pass(git_config_get_multivar(cfg, _name, "otherplace", cb, &n)); - cl_assert(n == 2); - - git_config_free(cfg); -} diff --git a/tests-clar/config/new.c b/tests-clar/config/new.c deleted file mode 100644 index dd6dbca9e8a..00000000000 --- a/tests-clar/config/new.c +++ /dev/null @@ -1,32 +0,0 @@ -#include "clar_libgit2.h" - -#include "filebuf.h" -#include "fileops.h" -#include "posix.h" - -#define TEST_CONFIG "git-new-config" - -void test_config_new__write_new_config(void) -{ - const char *out; - git_config *config; - - cl_git_mkfile(TEST_CONFIG, ""); - cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); - - cl_git_pass(git_config_set_string(config, "color.ui", "auto")); - cl_git_pass(git_config_set_string(config, "core.editor", "ed")); - - git_config_free(config); - - cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); - - cl_git_pass(git_config_get_string(&out, config, "color.ui")); - cl_assert_equal_s(out, "auto"); - cl_git_pass(git_config_get_string(&out, config, "core.editor")); - cl_assert_equal_s(out, "ed"); - - git_config_free(config); - - p_unlink(TEST_CONFIG); -} diff --git a/tests-clar/config/read.c b/tests-clar/config/read.c deleted file mode 100644 index c8582688694..00000000000 --- a/tests-clar/config/read.c +++ /dev/null @@ -1,458 +0,0 @@ -#include "clar_libgit2.h" - -void test_config_read__simple_read(void) -{ - git_config *cfg; - int32_t i; - - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config0"))); - - cl_git_pass(git_config_get_int32(&i, cfg, "core.repositoryformatversion")); - cl_assert(i == 0); - cl_git_pass(git_config_get_bool(&i, cfg, "core.filemode")); - cl_assert(i == 1); - cl_git_pass(git_config_get_bool(&i, cfg, "core.bare")); - cl_assert(i == 0); - cl_git_pass(git_config_get_bool(&i, cfg, "core.logallrefupdates")); - cl_assert(i == 1); - - git_config_free(cfg); -} - -void test_config_read__case_sensitive(void) -{ - git_config *cfg; - int i; - const char *str; - - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config1"))); - - cl_git_pass(git_config_get_string(&str, cfg, "this.that.other")); - cl_assert_equal_s(str, "true"); - cl_git_pass(git_config_get_string(&str, cfg, "this.That.other")); - cl_assert_equal_s(str, "yes"); - - cl_git_pass(git_config_get_bool(&i, cfg, "this.that.other")); - cl_assert(i == 1); - cl_git_pass(git_config_get_bool(&i, cfg, "this.That.other")); - cl_assert(i == 1); - - /* This one doesn't exist */ - cl_must_fail(git_config_get_bool(&i, cfg, "this.thaT.other")); - - git_config_free(cfg); -} - -/* - * If \ is the last non-space character on the line, we read the next - * one, separating each line with SP. - */ -void test_config_read__multiline_value(void) -{ - git_config *cfg; - const char *str; - - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config2"))); - - cl_git_pass(git_config_get_string(&str, cfg, "this.That.and")); - cl_assert_equal_s(str, "one one one two two three three"); - - git_config_free(cfg); -} - -/* - * This kind of subsection declaration is case-insensitive - */ -void test_config_read__subsection_header(void) -{ - git_config *cfg; - const char *str; - - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config3"))); - - cl_git_pass(git_config_get_string(&str, cfg, "section.subsection.var")); - cl_assert_equal_s(str, "hello"); - - /* The subsection is transformed to lower-case */ - cl_must_fail(git_config_get_string(&str, cfg, "section.subSectIon.var")); - - git_config_free(cfg); -} - -void test_config_read__lone_variable(void) -{ - git_config *cfg; - const char *str; - int i; - - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config4"))); - - cl_git_fail(git_config_get_int32(&i, cfg, "some.section.variable")); - - cl_git_pass(git_config_get_string(&str, cfg, "some.section.variable")); - cl_assert_equal_s(str, ""); - - cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variable")); - cl_assert(i == 1); - - cl_git_pass(git_config_get_string(&str, cfg, "some.section.variableeq")); - cl_assert_equal_s(str, ""); - - cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variableeq")); - cl_assert(i == 0); - - git_config_free(cfg); -} - -void test_config_read__number_suffixes(void) -{ - git_config *cfg; - int64_t i; - - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config5"))); - - cl_git_pass(git_config_get_int64(&i, cfg, "number.simple")); - cl_assert(i == 1); - - cl_git_pass(git_config_get_int64(&i, cfg, "number.k")); - cl_assert(i == 1 * 1024); - - cl_git_pass(git_config_get_int64(&i, cfg, "number.kk")); - cl_assert(i == 1 * 1024); - - cl_git_pass(git_config_get_int64(&i, cfg, "number.m")); - cl_assert(i == 1 * 1024 * 1024); - - cl_git_pass(git_config_get_int64(&i, cfg, "number.mm")); - cl_assert(i == 1 * 1024 * 1024); - - cl_git_pass(git_config_get_int64(&i, cfg, "number.g")); - cl_assert(i == 1 * 1024 * 1024 * 1024); - - cl_git_pass(git_config_get_int64(&i, cfg, "number.gg")); - cl_assert(i == 1 * 1024 * 1024 * 1024); - - git_config_free(cfg); -} - -void test_config_read__blank_lines(void) -{ - git_config *cfg; - int i; - - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config6"))); - - cl_git_pass(git_config_get_bool(&i, cfg, "valid.subsection.something")); - cl_assert(i == 1); - - cl_git_pass(git_config_get_bool(&i, cfg, "something.else.something")); - cl_assert(i == 0); - - git_config_free(cfg); -} - -void test_config_read__invalid_ext_headers(void) -{ - git_config *cfg; - cl_must_fail(git_config_open_ondisk(&cfg, cl_fixture("config/config7"))); -} - -void test_config_read__empty_files(void) -{ - git_config *cfg; - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config8"))); - git_config_free(cfg); -} - -void test_config_read__header_in_last_line(void) -{ - git_config *cfg; - - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config10"))); - git_config_free(cfg); -} - -void test_config_read__prefixes(void) -{ - git_config *cfg; - const char *str; - - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9"))); - cl_git_pass(git_config_get_string(&str, cfg, "remote.ab.url")); - cl_assert_equal_s(str, "http://example.com/git/ab"); - - cl_git_pass(git_config_get_string(&str, cfg, "remote.abba.url")); - cl_assert_equal_s(str, "http://example.com/git/abba"); - - git_config_free(cfg); -} - -void test_config_read__escaping_quotes(void) -{ - git_config *cfg; - const char *str; - - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config13"))); - cl_git_pass(git_config_get_string(&str, cfg, "core.editor")); - cl_assert_equal_s("\"C:/Program Files/Nonsense/bah.exe\" \"--some option\"", str); - - git_config_free(cfg); -} - -static int count_cfg_entries_and_compare_levels( - const git_config_entry *entry, void *payload) -{ - int *count = payload; - - if (!strcmp(entry->value, "7") || !strcmp(entry->value, "17")) - cl_assert(entry->level == GIT_CONFIG_LEVEL_GLOBAL); - else - cl_assert(entry->level == GIT_CONFIG_LEVEL_SYSTEM); - - (*count)++; - return 0; -} - -static int cfg_callback_countdown(const git_config_entry *entry, void *payload) -{ - int *count = payload; - GIT_UNUSED(entry); - (*count)--; - if (*count == 0) - return -100; - return 0; -} - -void test_config_read__foreach(void) -{ - git_config *cfg; - int count, ret; - - cl_git_pass(git_config_new(&cfg)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), - GIT_CONFIG_LEVEL_SYSTEM, 0)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"), - GIT_CONFIG_LEVEL_GLOBAL, 0)); - - count = 0; - cl_git_pass(git_config_foreach(cfg, count_cfg_entries_and_compare_levels, &count)); - cl_assert_equal_i(7, count); - - count = 3; - cl_git_fail(ret = git_config_foreach(cfg, cfg_callback_countdown, &count)); - cl_assert_equal_i(GIT_EUSER, ret); - - git_config_free(cfg); -} - -static int count_cfg_entries(const git_config_entry *entry, void *payload) -{ - int *count = payload; - GIT_UNUSED(entry); - (*count)++; - return 0; -} - -void test_config_read__foreach_match(void) -{ - git_config *cfg; - int count; - - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9"))); - - count = 0; - cl_git_pass( - git_config_foreach_match(cfg, "core.*", count_cfg_entries, &count)); - cl_assert_equal_i(3, count); - - count = 0; - cl_git_pass( - git_config_foreach_match(cfg, "remote\\.ab.*", count_cfg_entries, &count)); - cl_assert_equal_i(2, count); - - count = 0; - cl_git_pass( - git_config_foreach_match(cfg, ".*url$", count_cfg_entries, &count)); - cl_assert_equal_i(2, count); - - count = 0; - cl_git_pass( - git_config_foreach_match(cfg, ".*dummy.*", count_cfg_entries, &count)); - cl_assert_equal_i(2, count); - - count = 0; - cl_git_pass( - git_config_foreach_match(cfg, ".*nomatch.*", count_cfg_entries, &count)); - cl_assert_equal_i(0, count); - - git_config_free(cfg); -} - -void test_config_read__whitespace_not_required_around_assignment(void) -{ - git_config *cfg; - const char *str; - - cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config14"))); - - cl_git_pass(git_config_get_string(&str, cfg, "a.b")); - cl_assert_equal_s(str, "c"); - - cl_git_pass(git_config_get_string(&str, cfg, "d.e")); - cl_assert_equal_s(str, "f"); - - git_config_free(cfg); -} - -void test_config_read__read_git_config_entry(void) -{ - git_config *cfg; - const git_config_entry *entry; - - cl_git_pass(git_config_new(&cfg)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), - GIT_CONFIG_LEVEL_SYSTEM, 0)); - - cl_git_pass(git_config_get_entry(&entry, cfg, "core.dummy2")); - cl_assert_equal_s("core.dummy2", entry->name); - cl_assert_equal_s("42", entry->value); - cl_assert_equal_i(GIT_CONFIG_LEVEL_SYSTEM, entry->level); - - git_config_free(cfg); -} - -/* - * At the beginning of the test: - * - config9 has: core.dummy2=42 - * - config15 has: core.dummy2=7 - * - config16 has: core.dummy2=28 - */ -void test_config_read__local_config_overrides_global_config_overrides_system_config(void) -{ - git_config *cfg; - int32_t i; - - cl_git_pass(git_config_new(&cfg)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), - GIT_CONFIG_LEVEL_SYSTEM, 0)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"), - GIT_CONFIG_LEVEL_GLOBAL, 0)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"), - GIT_CONFIG_LEVEL_LOCAL, 0)); - - cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2")); - cl_assert_equal_i(28, i); - - git_config_free(cfg); - - cl_git_pass(git_config_new(&cfg)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), - GIT_CONFIG_LEVEL_SYSTEM, 0)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"), - GIT_CONFIG_LEVEL_GLOBAL, 0)); - - cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2")); - cl_assert_equal_i(7, i); - - git_config_free(cfg); -} - -/* - * At the beginning of the test: - * - config9 has: core.global does not exist - * - config15 has: core.global=17 - * - config16 has: core.global=29 - * - * And also: - * - config9 has: core.system does not exist - * - config15 has: core.system does not exist - * - config16 has: core.system=11 - */ -void test_config_read__fallback_from_local_to_global_and_from_global_to_system(void) -{ - git_config *cfg; - int32_t i; - - cl_git_pass(git_config_new(&cfg)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), - GIT_CONFIG_LEVEL_SYSTEM, 0)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"), - GIT_CONFIG_LEVEL_GLOBAL, 0)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"), - GIT_CONFIG_LEVEL_LOCAL, 0)); - - cl_git_pass(git_config_get_int32(&i, cfg, "core.global")); - cl_assert_equal_i(17, i); - cl_git_pass(git_config_get_int32(&i, cfg, "core.system")); - cl_assert_equal_i(11, i); - - git_config_free(cfg); -} - -/* - * At the beginning of the test, config18 has: - * int32global = 28 - * int64global = 9223372036854775803 - * boolglobal = true - * stringglobal = I'm a global config value! - * - * And config19 has: - * int32global = -1 - * int64global = -2 - * boolglobal = false - * stringglobal = don't find me! - * - */ -void test_config_read__simple_read_from_specific_level(void) -{ - git_config *cfg, *cfg_specific; - int i; - int64_t l, expected = +9223372036854775803; - const char *s; - - cl_git_pass(git_config_new(&cfg)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"), - GIT_CONFIG_LEVEL_GLOBAL, 0)); - cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"), - GIT_CONFIG_LEVEL_SYSTEM, 0)); - - cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL)); - - cl_git_pass(git_config_get_int32(&i, cfg_specific, "core.int32global")); - cl_assert_equal_i(28, i); - cl_git_pass(git_config_get_int64(&l, cfg_specific, "core.int64global")); - cl_assert(l == expected); - cl_git_pass(git_config_get_bool(&i, cfg_specific, "core.boolglobal")); - cl_assert_equal_b(true, i); - cl_git_pass(git_config_get_string(&s, cfg_specific, "core.stringglobal")); - cl_assert_equal_s("I'm a global config value!", s); - - git_config_free(cfg_specific); - git_config_free(cfg); -} - -static void clean_empty_config(void *unused) -{ - GIT_UNUSED(unused); - cl_fixture_cleanup("./empty"); -} - -void test_config_read__can_load_and_parse_an_empty_config_file(void) -{ - git_config *cfg; - int i; - - cl_set_cleanup(&clean_empty_config, NULL); - cl_git_mkfile("./empty", ""); - cl_git_pass(git_config_open_ondisk(&cfg, "./empty")); - cl_assert_equal_i(GIT_ENOTFOUND, git_config_get_int32(&i, cfg, "nope.neither")); - - git_config_free(cfg); -} - -void test_config_read__cannot_load_a_non_existing_config_file(void) -{ - git_config *cfg; - - cl_assert_equal_i(GIT_ENOTFOUND, git_config_open_ondisk(&cfg, "./no.config")); -} diff --git a/tests-clar/config/refresh.c b/tests-clar/config/refresh.c deleted file mode 100644 index 99d677f0ee3..00000000000 --- a/tests-clar/config/refresh.c +++ /dev/null @@ -1,67 +0,0 @@ -#include "clar_libgit2.h" - -#define TEST_FILE "config.refresh" - -void test_config_refresh__initialize(void) -{ -} - -void test_config_refresh__cleanup(void) -{ - cl_fixture_cleanup(TEST_FILE); -} - -void test_config_refresh__update_value(void) -{ - git_config *cfg; - int32_t v; - - cl_git_mkfile(TEST_FILE, "[section]\n\tvalue = 1\n\n"); - - /* By freeing the config, we make sure we flush the values */ - cl_git_pass(git_config_open_ondisk(&cfg, TEST_FILE)); - - cl_git_pass(git_config_get_int32(&v, cfg, "section.value")); - cl_assert_equal_i(1, v); - - cl_git_rewritefile(TEST_FILE, "[section]\n\tvalue = 10\n\n"); - - cl_git_pass(git_config_get_int32(&v, cfg, "section.value")); - cl_assert_equal_i(1, v); - - cl_git_pass(git_config_refresh(cfg)); - - cl_git_pass(git_config_get_int32(&v, cfg, "section.value")); - cl_assert_equal_i(10, v); - - git_config_free(cfg); -} - -void test_config_refresh__delete_value(void) -{ - git_config *cfg; - int32_t v; - - cl_git_mkfile(TEST_FILE, "[section]\n\tvalue = 1\n\n"); - - /* By freeing the config, we make sure we flush the values */ - cl_git_pass(git_config_open_ondisk(&cfg, TEST_FILE)); - - cl_git_pass(git_config_get_int32(&v, cfg, "section.value")); - cl_assert_equal_i(1, v); - cl_git_fail(git_config_get_int32(&v, cfg, "section.newval")); - - cl_git_rewritefile(TEST_FILE, "[section]\n\tnewval = 10\n\n"); - - cl_git_pass(git_config_get_int32(&v, cfg, "section.value")); - cl_assert_equal_i(1, v); - cl_git_fail(git_config_get_int32(&v, cfg, "section.newval")); - - cl_git_pass(git_config_refresh(cfg)); - - cl_git_fail(git_config_get_int32(&v, cfg, "section.value")); - cl_git_pass(git_config_get_int32(&v, cfg, "section.newval")); - cl_assert_equal_i(10, v); - - git_config_free(cfg); -} diff --git a/tests-clar/config/stress.c b/tests-clar/config/stress.c deleted file mode 100644 index 8cc64d23cf3..00000000000 --- a/tests-clar/config/stress.c +++ /dev/null @@ -1,92 +0,0 @@ -#include "clar_libgit2.h" - -#include "filebuf.h" -#include "fileops.h" -#include "posix.h" - -#define TEST_CONFIG "git-test-config" - -void test_config_stress__initialize(void) -{ - git_filebuf file = GIT_FILEBUF_INIT; - - cl_git_pass(git_filebuf_open(&file, TEST_CONFIG, 0)); - - git_filebuf_printf(&file, "[color]\n\tui = auto\n"); - git_filebuf_printf(&file, "[core]\n\teditor = \n"); - - cl_git_pass(git_filebuf_commit(&file, 0666)); -} - -void test_config_stress__cleanup(void) -{ - p_unlink(TEST_CONFIG); -} - -void test_config_stress__dont_break_on_invalid_input(void) -{ - const char *editor, *color; - git_config *config; - - cl_assert(git_path_exists(TEST_CONFIG)); - cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); - - cl_git_pass(git_config_get_string(&color, config, "color.ui")); - cl_git_pass(git_config_get_string(&editor, config, "core.editor")); - - git_config_free(config); -} - -void test_config_stress__comments(void) -{ - git_config *config; - const char *str; - - cl_git_pass(git_config_open_ondisk(&config, cl_fixture("config/config12"))); - - cl_git_pass(git_config_get_string(&str, config, "some.section.other")); - cl_assert_equal_s("hello! \" ; ; ; ", str); - - cl_git_pass(git_config_get_string(&str, config, "some.section.multi")); - cl_assert_equal_s("hi, this is a ; multiline comment # with ;\n special chars and other stuff !@#", str); - - cl_git_pass(git_config_get_string(&str, config, "some.section.back")); - cl_assert_equal_s("this is \ba phrase", str); - - git_config_free(config); -} - -void test_config_stress__escape_subsection_names(void) -{ - git_config *config; - const char *str; - - cl_assert(git_path_exists("git-test-config")); - cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); - - cl_git_pass(git_config_set_string(config, "some.sec\\tion.other", "foo")); - git_config_free(config); - - cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); - - cl_git_pass(git_config_get_string(&str, config, "some.sec\\tion.other")); - cl_assert_equal_s("foo", str); - git_config_free(config); -} - -void test_config_stress__trailing_backslash(void) -{ - git_config *config; - const char *str; - const char *path = "C:\\iam\\some\\windows\\path\\"; - - cl_assert(git_path_exists("git-test-config")); - cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); - cl_git_pass(git_config_set_string(config, "windows.path", path)); - git_config_free(config); - - cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); - cl_git_pass(git_config_get_string(&str, config, "windows.path")); - cl_assert_equal_s(path, str); - git_config_free(config); -} diff --git a/tests-clar/config/validkeyname.c b/tests-clar/config/validkeyname.c deleted file mode 100644 index 03c13d723c5..00000000000 --- a/tests-clar/config/validkeyname.c +++ /dev/null @@ -1,68 +0,0 @@ -#include "clar_libgit2.h" - -#include "config.h" - -static git_config *cfg; -static const char *value; - -void test_config_validkeyname__initialize(void) -{ - cl_fixture_sandbox("config/config10"); - - cl_git_pass(git_config_open_ondisk(&cfg, "config10")); -} - -void test_config_validkeyname__cleanup(void) -{ - git_config_free(cfg); - cfg = NULL; - - cl_fixture_cleanup("config10"); -} - -static void assert_invalid_config_key_name(const char *name) -{ - cl_git_fail_with(git_config_get_string(&value, cfg, name), - GIT_EINVALIDSPEC); - cl_git_fail_with(git_config_set_string(cfg, name, "42"), - GIT_EINVALIDSPEC); - cl_git_fail_with(git_config_delete_entry(cfg, name), - GIT_EINVALIDSPEC); - cl_git_fail_with(git_config_get_multivar(cfg, name, "*", NULL, NULL), - GIT_EINVALIDSPEC); - cl_git_fail_with(git_config_set_multivar(cfg, name, "*", "42"), - GIT_EINVALIDSPEC); -} - -void test_config_validkeyname__accessing_requires_a_valid_name(void) -{ - assert_invalid_config_key_name(""); - assert_invalid_config_key_name("."); - assert_invalid_config_key_name(".."); - assert_invalid_config_key_name("core."); - assert_invalid_config_key_name("d#ff.dirstat.lines"); - assert_invalid_config_key_name("diff.dirstat.lines#"); - assert_invalid_config_key_name("dif\nf.dirstat.lines"); - assert_invalid_config_key_name("dif.dir\nstat.lines"); - assert_invalid_config_key_name("dif.dirstat.li\nes"); -} - -static void assert_invalid_config_section_name(git_repository *repo, const char *name) -{ - cl_git_fail_with(git_config_rename_section(repo, "branch.remoteless", name), GIT_EINVALIDSPEC); -} - -void test_config_validkeyname__renaming_a_section_requires_a_valid_name(void) -{ - git_repository *repo; - - cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); - - assert_invalid_config_section_name(repo, ""); - assert_invalid_config_section_name(repo, "bra\nch"); - assert_invalid_config_section_name(repo, "branc#"); - assert_invalid_config_section_name(repo, "bra\nch.duh"); - assert_invalid_config_section_name(repo, "branc#.duh"); - - git_repository_free(repo); -} diff --git a/tests-clar/config/write.c b/tests-clar/config/write.c deleted file mode 100644 index 1b665cd19ba..00000000000 --- a/tests-clar/config/write.c +++ /dev/null @@ -1,230 +0,0 @@ -#include "clar_libgit2.h" - -void test_config_write__initialize(void) -{ - cl_fixture_sandbox("config/config9"); - cl_fixture_sandbox("config/config15"); - cl_fixture_sandbox("config/config17"); -} - -void test_config_write__cleanup(void) -{ - cl_fixture_cleanup("config9"); - cl_fixture_cleanup("config15"); - cl_fixture_cleanup("config17"); -} - -void test_config_write__replace_value(void) -{ - git_config *cfg; - int i; - int64_t l, expected = +9223372036854775803; - - /* By freeing the config, we make sure we flush the values */ - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_set_int32(cfg, "core.dummy", 5)); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy")); - cl_assert(i == 5); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_set_int32(cfg, "core.dummy", 1)); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_set_int64(cfg, "core.verylong", expected)); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_get_int64(&l, cfg, "core.verylong")); - cl_assert(l == expected); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_must_fail(git_config_get_int32(&i, cfg, "core.verylong")); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_set_int64(cfg, "core.verylong", 1)); - git_config_free(cfg); -} - -void test_config_write__delete_value(void) -{ - git_config *cfg; - int32_t i; - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_set_int32(cfg, "core.dummy", 5)); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_delete_entry(cfg, "core.dummy")); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_assert(git_config_get_int32(&i, cfg, "core.dummy") == GIT_ENOTFOUND); - cl_git_pass(git_config_set_int32(cfg, "core.dummy", 1)); - git_config_free(cfg); -} - -/* - * At the beginning of the test: - * - config9 has: core.dummy2=42 - * - config15 has: core.dummy2=7 - */ -void test_config_write__delete_value_at_specific_level(void) -{ - git_config *cfg, *cfg_specific; - int32_t i; - - cl_git_pass(git_config_open_ondisk(&cfg, "config15")); - cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2")); - cl_assert(i == 7); - git_config_free(cfg); - - cl_git_pass(git_config_new(&cfg)); - cl_git_pass(git_config_add_file_ondisk(cfg, "config9", - GIT_CONFIG_LEVEL_LOCAL, 0)); - cl_git_pass(git_config_add_file_ondisk(cfg, "config15", - GIT_CONFIG_LEVEL_GLOBAL, 0)); - - cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL)); - - cl_git_pass(git_config_delete_entry(cfg_specific, "core.dummy2")); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config15")); - cl_assert(git_config_get_int32(&i, cfg, "core.dummy2") == GIT_ENOTFOUND); - cl_git_pass(git_config_set_int32(cfg, "core.dummy2", 7)); - - git_config_free(cfg_specific); - git_config_free(cfg); -} - -void test_config_write__write_subsection(void) -{ - git_config *cfg; - const char *str; - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_set_string(cfg, "my.own.var", "works")); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_get_string(&str, cfg, "my.own.var")); - cl_assert_equal_s("works", str); - git_config_free(cfg); -} - -void test_config_write__delete_inexistent(void) -{ - git_config *cfg; - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_assert(git_config_delete_entry(cfg, "core.imaginary") == GIT_ENOTFOUND); - git_config_free(cfg); -} - -void test_config_write__value_containing_quotes(void) -{ - git_config *cfg; - const char* str; - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes")); - cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); - cl_assert_equal_s(str, "this \"has\" quotes"); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); - cl_assert_equal_s(str, "this \"has\" quotes"); - git_config_free(cfg); - - /* The code path for values that already exist is different, check that one as well */ - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_set_string(cfg, "core.somevar", "this also \"has\" quotes")); - cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); - cl_assert_equal_s(str, "this also \"has\" quotes"); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); - cl_assert_equal_s(str, "this also \"has\" quotes"); - git_config_free(cfg); -} - -void test_config_write__escape_value(void) -{ - git_config *cfg; - const char* str; - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes and \t")); - cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); - cl_assert_equal_s(str, "this \"has\" quotes and \t"); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config9")); - cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); - cl_assert_equal_s(str, "this \"has\" quotes and \t"); - git_config_free(cfg); -} - -void test_config_write__add_value_at_specific_level(void) -{ - git_config *cfg, *cfg_specific; - int i; - int64_t l, expected = +9223372036854775803; - const char *s; - - // open config15 as global level config file - cl_git_pass(git_config_new(&cfg)); - cl_git_pass(git_config_add_file_ondisk(cfg, "config9", - GIT_CONFIG_LEVEL_LOCAL, 0)); - cl_git_pass(git_config_add_file_ondisk(cfg, "config15", - GIT_CONFIG_LEVEL_GLOBAL, 0)); - - cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL)); - - cl_git_pass(git_config_set_int32(cfg_specific, "core.int32global", 28)); - cl_git_pass(git_config_set_int64(cfg_specific, "core.int64global", expected)); - cl_git_pass(git_config_set_bool(cfg_specific, "core.boolglobal", true)); - cl_git_pass(git_config_set_string(cfg_specific, "core.stringglobal", "I'm a global config value!")); - git_config_free(cfg_specific); - git_config_free(cfg); - - // open config15 as local level config file - cl_git_pass(git_config_open_ondisk(&cfg, "config15")); - - cl_git_pass(git_config_get_int32(&i, cfg, "core.int32global")); - cl_assert_equal_i(28, i); - cl_git_pass(git_config_get_int64(&l, cfg, "core.int64global")); - cl_assert(l == expected); - cl_git_pass(git_config_get_bool(&i, cfg, "core.boolglobal")); - cl_assert_equal_b(true, i); - cl_git_pass(git_config_get_string(&s, cfg, "core.stringglobal")); - cl_assert_equal_s("I'm a global config value!", s); - - git_config_free(cfg); -} - -void test_config_write__add_value_at_file_with_no_clrf_at_the_end(void) -{ - git_config *cfg; - int i; - - cl_git_pass(git_config_open_ondisk(&cfg, "config17")); - cl_git_pass(git_config_set_int32(cfg, "core.newline", 7)); - git_config_free(cfg); - - cl_git_pass(git_config_open_ondisk(&cfg, "config17")); - cl_git_pass(git_config_get_int32(&i, cfg, "core.newline")); - cl_assert_equal_i(7, i); - - git_config_free(cfg); -} diff --git a/tests-clar/core/buffer.c b/tests-clar/core/buffer.c deleted file mode 100644 index 49ab41f7123..00000000000 --- a/tests-clar/core/buffer.c +++ /dev/null @@ -1,732 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "buf_text.h" - -#define TESTSTR "Have you seen that? Have you seeeen that??" -const char *test_string = TESTSTR; -const char *test_string_x2 = TESTSTR TESTSTR; - -#define TESTSTR_4096 REP1024("1234") -#define TESTSTR_8192 REP1024("12341234") -const char *test_4096 = TESTSTR_4096; -const char *test_8192 = TESTSTR_8192; - -/* test basic data concatenation */ -void test_core_buffer__0(void) -{ - git_buf buf = GIT_BUF_INIT; - - cl_assert(buf.size == 0); - - git_buf_puts(&buf, test_string); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(test_string, git_buf_cstr(&buf)); - - git_buf_puts(&buf, test_string); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(test_string_x2, git_buf_cstr(&buf)); - - git_buf_free(&buf); -} - -/* test git_buf_printf */ -void test_core_buffer__1(void) -{ - git_buf buf = GIT_BUF_INIT; - - git_buf_printf(&buf, "%s %s %d ", "shoop", "da", 23); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s("shoop da 23 ", git_buf_cstr(&buf)); - - git_buf_printf(&buf, "%s %d", "woop", 42); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s("shoop da 23 woop 42", git_buf_cstr(&buf)); - - git_buf_free(&buf); -} - -/* more thorough test of concatenation options */ -void test_core_buffer__2(void) -{ - git_buf buf = GIT_BUF_INIT; - int i; - char data[128]; - - cl_assert(buf.size == 0); - - /* this must be safe to do */ - git_buf_free(&buf); - cl_assert(buf.size == 0); - cl_assert(buf.asize == 0); - - /* empty buffer should be empty string */ - cl_assert_equal_s("", git_buf_cstr(&buf)); - cl_assert(buf.size == 0); - /* cl_assert(buf.asize == 0); -- should not assume what git_buf does */ - - /* free should set us back to the beginning */ - git_buf_free(&buf); - cl_assert(buf.size == 0); - cl_assert(buf.asize == 0); - - /* add letter */ - git_buf_putc(&buf, '+'); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s("+", git_buf_cstr(&buf)); - - /* add letter again */ - git_buf_putc(&buf, '+'); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s("++", git_buf_cstr(&buf)); - - /* let's try that a few times */ - for (i = 0; i < 16; ++i) { - git_buf_putc(&buf, '+'); - cl_assert(git_buf_oom(&buf) == 0); - } - cl_assert_equal_s("++++++++++++++++++", git_buf_cstr(&buf)); - - git_buf_free(&buf); - - /* add data */ - git_buf_put(&buf, "xo", 2); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s("xo", git_buf_cstr(&buf)); - - /* add letter again */ - git_buf_put(&buf, "xo", 2); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s("xoxo", git_buf_cstr(&buf)); - - /* let's try that a few times */ - for (i = 0; i < 16; ++i) { - git_buf_put(&buf, "xo", 2); - cl_assert(git_buf_oom(&buf) == 0); - } - cl_assert_equal_s("xoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxo", - git_buf_cstr(&buf)); - - git_buf_free(&buf); - - /* set to string */ - git_buf_sets(&buf, test_string); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(test_string, git_buf_cstr(&buf)); - - /* append string */ - git_buf_puts(&buf, test_string); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(test_string_x2, git_buf_cstr(&buf)); - - /* set to string again (should overwrite - not append) */ - git_buf_sets(&buf, test_string); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(test_string, git_buf_cstr(&buf)); - - /* test clear */ - git_buf_clear(&buf); - cl_assert_equal_s("", git_buf_cstr(&buf)); - - git_buf_free(&buf); - - /* test extracting data into buffer */ - git_buf_puts(&buf, REP4("0123456789")); - cl_assert(git_buf_oom(&buf) == 0); - - git_buf_copy_cstr(data, sizeof(data), &buf); - cl_assert_equal_s(REP4("0123456789"), data); - git_buf_copy_cstr(data, 11, &buf); - cl_assert_equal_s("0123456789", data); - git_buf_copy_cstr(data, 3, &buf); - cl_assert_equal_s("01", data); - git_buf_copy_cstr(data, 1, &buf); - cl_assert_equal_s("", data); - - git_buf_copy_cstr(data, sizeof(data), &buf); - cl_assert_equal_s(REP4("0123456789"), data); - - git_buf_sets(&buf, REP256("x")); - git_buf_copy_cstr(data, sizeof(data), &buf); - /* since sizeof(data) == 128, only 127 bytes should be copied */ - cl_assert_equal_s(REP4(REP16("x")) REP16("x") REP16("x") - REP16("x") "xxxxxxxxxxxxxxx", data); - - git_buf_free(&buf); - - git_buf_copy_cstr(data, sizeof(data), &buf); - cl_assert_equal_s("", data); -} - -/* let's do some tests with larger buffers to push our limits */ -void test_core_buffer__3(void) -{ - git_buf buf = GIT_BUF_INIT; - - /* set to string */ - git_buf_set(&buf, test_4096, 4096); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(test_4096, git_buf_cstr(&buf)); - - /* append string */ - git_buf_puts(&buf, test_4096); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(test_8192, git_buf_cstr(&buf)); - - /* set to string again (should overwrite - not append) */ - git_buf_set(&buf, test_4096, 4096); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(test_4096, git_buf_cstr(&buf)); - - git_buf_free(&buf); -} - -/* let's try some producer/consumer tests */ -void test_core_buffer__4(void) -{ - git_buf buf = GIT_BUF_INIT; - int i; - - for (i = 0; i < 10; ++i) { - git_buf_puts(&buf, "1234"); /* add 4 */ - cl_assert(git_buf_oom(&buf) == 0); - git_buf_consume(&buf, buf.ptr + 2); /* eat the first two */ - cl_assert(strlen(git_buf_cstr(&buf)) == (size_t)((i + 1) * 2)); - } - /* we have appended 1234 10x and removed the first 20 letters */ - cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf)); - - git_buf_consume(&buf, NULL); - cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf)); - - git_buf_consume(&buf, "invalid pointer"); - cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf)); - - git_buf_consume(&buf, buf.ptr); - cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf)); - - git_buf_consume(&buf, buf.ptr + 1); - cl_assert_equal_s("2341234123412341234", git_buf_cstr(&buf)); - - git_buf_consume(&buf, buf.ptr + buf.size); - cl_assert_equal_s("", git_buf_cstr(&buf)); - - git_buf_free(&buf); -} - - -static void -check_buf_append( - const char* data_a, - const char* data_b, - const char* expected_data, - size_t expected_size, - size_t expected_asize) -{ - git_buf tgt = GIT_BUF_INIT; - - git_buf_sets(&tgt, data_a); - cl_assert(git_buf_oom(&tgt) == 0); - git_buf_puts(&tgt, data_b); - cl_assert(git_buf_oom(&tgt) == 0); - cl_assert_equal_s(expected_data, git_buf_cstr(&tgt)); - cl_assert(tgt.size == expected_size); - if (expected_asize > 0) - cl_assert(tgt.asize == expected_asize); - - git_buf_free(&tgt); -} - -static void -check_buf_append_abc( - const char* buf_a, - const char* buf_b, - const char* buf_c, - const char* expected_ab, - const char* expected_abc, - const char* expected_abca, - const char* expected_abcab, - const char* expected_abcabc) -{ - git_buf buf = GIT_BUF_INIT; - - git_buf_sets(&buf, buf_a); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(buf_a, git_buf_cstr(&buf)); - - git_buf_puts(&buf, buf_b); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(expected_ab, git_buf_cstr(&buf)); - - git_buf_puts(&buf, buf_c); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(expected_abc, git_buf_cstr(&buf)); - - git_buf_puts(&buf, buf_a); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(expected_abca, git_buf_cstr(&buf)); - - git_buf_puts(&buf, buf_b); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(expected_abcab, git_buf_cstr(&buf)); - - git_buf_puts(&buf, buf_c); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(expected_abcabc, git_buf_cstr(&buf)); - - git_buf_free(&buf); -} - -/* more variations on append tests */ -void test_core_buffer__5(void) -{ - check_buf_append("", "", "", 0, 8); - check_buf_append("a", "", "a", 1, 8); - check_buf_append("", "a", "a", 1, 8); - check_buf_append("", "a", "a", 1, 8); - check_buf_append("a", "", "a", 1, 8); - check_buf_append("a", "b", "ab", 2, 8); - check_buf_append("", "abcdefgh", "abcdefgh", 8, 16); - check_buf_append("abcdefgh", "", "abcdefgh", 8, 16); - - /* buffer with starting asize will grow to: - * 1 -> 2, 2 -> 3, 3 -> 5, 4 -> 6, 5 -> 8, 6 -> 9, - * 7 -> 11, 8 -> 12, 9 -> 14, 10 -> 15, 11 -> 17, 12 -> 18, - * 13 -> 20, 14 -> 21, 15 -> 23, 16 -> 24, 17 -> 26, 18 -> 27, - * 19 -> 29, 20 -> 30, 21 -> 32, 22 -> 33, 23 -> 35, 24 -> 36, - * ... - * follow sequence until value > target size, - * then round up to nearest multiple of 8. - */ - - check_buf_append("abcdefgh", "/", "abcdefgh/", 9, 16); - check_buf_append("abcdefgh", "ijklmno", "abcdefghijklmno", 15, 16); - check_buf_append("abcdefgh", "ijklmnop", "abcdefghijklmnop", 16, 24); - check_buf_append("0123456789", "0123456789", - "01234567890123456789", 20, 24); - check_buf_append(REP16("x"), REP16("o"), - REP16("x") REP16("o"), 32, 40); - - check_buf_append(test_4096, "", test_4096, 4096, 4104); - check_buf_append(test_4096, test_4096, test_8192, 8192, 9240); - - /* check sequences of appends */ - check_buf_append_abc("a", "b", "c", - "ab", "abc", "abca", "abcab", "abcabc"); - check_buf_append_abc("a1", "b2", "c3", - "a1b2", "a1b2c3", "a1b2c3a1", - "a1b2c3a1b2", "a1b2c3a1b2c3"); - check_buf_append_abc("a1/", "b2/", "c3/", - "a1/b2/", "a1/b2/c3/", "a1/b2/c3/a1/", - "a1/b2/c3/a1/b2/", "a1/b2/c3/a1/b2/c3/"); -} - -/* test swap */ -void test_core_buffer__6(void) -{ - git_buf a = GIT_BUF_INIT; - git_buf b = GIT_BUF_INIT; - - git_buf_sets(&a, "foo"); - cl_assert(git_buf_oom(&a) == 0); - git_buf_sets(&b, "bar"); - cl_assert(git_buf_oom(&b) == 0); - - cl_assert_equal_s("foo", git_buf_cstr(&a)); - cl_assert_equal_s("bar", git_buf_cstr(&b)); - - git_buf_swap(&a, &b); - - cl_assert_equal_s("bar", git_buf_cstr(&a)); - cl_assert_equal_s("foo", git_buf_cstr(&b)); - - git_buf_free(&a); - git_buf_free(&b); -} - - -/* test detach/attach data */ -void test_core_buffer__7(void) -{ - const char *fun = "This is fun"; - git_buf a = GIT_BUF_INIT; - char *b = NULL; - - git_buf_sets(&a, "foo"); - cl_assert(git_buf_oom(&a) == 0); - cl_assert_equal_s("foo", git_buf_cstr(&a)); - - b = git_buf_detach(&a); - - cl_assert_equal_s("foo", b); - cl_assert_equal_s("", a.ptr); - git__free(b); - - b = git_buf_detach(&a); - - cl_assert_equal_s(NULL, b); - cl_assert_equal_s("", a.ptr); - - git_buf_free(&a); - - b = git__strdup(fun); - git_buf_attach(&a, b, 0); - - cl_assert_equal_s(fun, a.ptr); - cl_assert(a.size == strlen(fun)); - cl_assert(a.asize == strlen(fun) + 1); - - git_buf_free(&a); - - b = git__strdup(fun); - git_buf_attach(&a, b, strlen(fun) + 1); - - cl_assert_equal_s(fun, a.ptr); - cl_assert(a.size == strlen(fun)); - cl_assert(a.asize == strlen(fun) + 1); - - git_buf_free(&a); -} - - -static void -check_joinbuf_2( - const char *a, - const char *b, - const char *expected) -{ - char sep = '/'; - git_buf buf = GIT_BUF_INIT; - - git_buf_join(&buf, sep, a, b); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(expected, git_buf_cstr(&buf)); - git_buf_free(&buf); -} - -static void -check_joinbuf_n_2( - const char *a, - const char *b, - const char *expected) -{ - char sep = '/'; - git_buf buf = GIT_BUF_INIT; - - git_buf_sets(&buf, a); - cl_assert(git_buf_oom(&buf) == 0); - - git_buf_join_n(&buf, sep, 1, b); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(expected, git_buf_cstr(&buf)); - - git_buf_free(&buf); -} - -static void -check_joinbuf_n_4( - const char *a, - const char *b, - const char *c, - const char *d, - const char *expected) -{ - char sep = ';'; - git_buf buf = GIT_BUF_INIT; - git_buf_join_n(&buf, sep, 4, a, b, c, d); - cl_assert(git_buf_oom(&buf) == 0); - cl_assert_equal_s(expected, git_buf_cstr(&buf)); - git_buf_free(&buf); -} - -/* test join */ -void test_core_buffer__8(void) -{ - git_buf a = GIT_BUF_INIT; - - git_buf_join_n(&a, '/', 1, "foo"); - cl_assert(git_buf_oom(&a) == 0); - cl_assert_equal_s("foo", git_buf_cstr(&a)); - - git_buf_join_n(&a, '/', 1, "bar"); - cl_assert(git_buf_oom(&a) == 0); - cl_assert_equal_s("foo/bar", git_buf_cstr(&a)); - - git_buf_join_n(&a, '/', 1, "baz"); - cl_assert(git_buf_oom(&a) == 0); - cl_assert_equal_s("foo/bar/baz", git_buf_cstr(&a)); - - git_buf_free(&a); - - check_joinbuf_2(NULL, "", ""); - check_joinbuf_2(NULL, "a", "a"); - check_joinbuf_2(NULL, "/a", "/a"); - check_joinbuf_2("", "", ""); - check_joinbuf_2("", "a", "a"); - check_joinbuf_2("", "/a", "/a"); - check_joinbuf_2("a", "", "a/"); - check_joinbuf_2("a", "/", "a/"); - check_joinbuf_2("a", "b", "a/b"); - check_joinbuf_2("/", "a", "/a"); - check_joinbuf_2("/", "", "/"); - check_joinbuf_2("/a", "/b", "/a/b"); - check_joinbuf_2("/a", "/b/", "/a/b/"); - check_joinbuf_2("/a/", "b/", "/a/b/"); - check_joinbuf_2("/a/", "/b/", "/a/b/"); - check_joinbuf_2("/a/", "//b/", "/a/b/"); - check_joinbuf_2("/abcd", "/defg", "/abcd/defg"); - check_joinbuf_2("/abcd", "/defg/", "/abcd/defg/"); - check_joinbuf_2("/abcd/", "defg/", "/abcd/defg/"); - check_joinbuf_2("/abcd/", "/defg/", "/abcd/defg/"); - - check_joinbuf_n_2("", "", ""); - check_joinbuf_n_2("", "a", "a"); - check_joinbuf_n_2("", "/a", "/a"); - check_joinbuf_n_2("a", "", "a/"); - check_joinbuf_n_2("a", "/", "a/"); - check_joinbuf_n_2("a", "b", "a/b"); - check_joinbuf_n_2("/", "a", "/a"); - check_joinbuf_n_2("/", "", "/"); - check_joinbuf_n_2("/a", "/b", "/a/b"); - check_joinbuf_n_2("/a", "/b/", "/a/b/"); - check_joinbuf_n_2("/a/", "b/", "/a/b/"); - check_joinbuf_n_2("/a/", "/b/", "/a/b/"); - check_joinbuf_n_2("/abcd", "/defg", "/abcd/defg"); - check_joinbuf_n_2("/abcd", "/defg/", "/abcd/defg/"); - check_joinbuf_n_2("/abcd/", "defg/", "/abcd/defg/"); - check_joinbuf_n_2("/abcd/", "/defg/", "/abcd/defg/"); - - check_joinbuf_n_4("", "", "", "", ""); - check_joinbuf_n_4("", "a", "", "", "a;"); - check_joinbuf_n_4("a", "", "", "", "a;"); - check_joinbuf_n_4("", "", "", "a", "a"); - check_joinbuf_n_4("a", "b", "", ";c;d;", "a;b;c;d;"); - check_joinbuf_n_4("a", "b", "", ";c;d", "a;b;c;d"); - check_joinbuf_n_4("abcd", "efgh", "ijkl", "mnop", "abcd;efgh;ijkl;mnop"); - check_joinbuf_n_4("abcd;", "efgh;", "ijkl;", "mnop;", "abcd;efgh;ijkl;mnop;"); - check_joinbuf_n_4(";abcd;", ";efgh;", ";ijkl;", ";mnop;", ";abcd;efgh;ijkl;mnop;"); -} - -void test_core_buffer__9(void) -{ - git_buf buf = GIT_BUF_INIT; - - /* just some exhaustive tests of various separator placement */ - char *a[] = { "", "-", "a-", "-a", "-a-" }; - char *b[] = { "", "-", "b-", "-b", "-b-" }; - char sep[] = { 0, '-', '/' }; - char *expect_null[] = { "", "-", "a-", "-a", "-a-", - "-", "--", "a--", "-a-", "-a--", - "b-", "-b-", "a-b-", "-ab-", "-a-b-", - "-b", "--b", "a--b", "-a-b", "-a--b", - "-b-", "--b-", "a--b-", "-a-b-", "-a--b-" }; - char *expect_dash[] = { "", "-", "a-", "-a-", "-a-", - "-", "-", "a-", "-a-", "-a-", - "b-", "-b-", "a-b-", "-a-b-", "-a-b-", - "-b", "-b", "a-b", "-a-b", "-a-b", - "-b-", "-b-", "a-b-", "-a-b-", "-a-b-" }; - char *expect_slas[] = { "", "-/", "a-/", "-a/", "-a-/", - "-", "-/-", "a-/-", "-a/-", "-a-/-", - "b-", "-/b-", "a-/b-", "-a/b-", "-a-/b-", - "-b", "-/-b", "a-/-b", "-a/-b", "-a-/-b", - "-b-", "-/-b-", "a-/-b-", "-a/-b-", "-a-/-b-" }; - char **expect_values[] = { expect_null, expect_dash, expect_slas }; - char separator, **expect; - unsigned int s, i, j; - - for (s = 0; s < sizeof(sep) / sizeof(char); ++s) { - separator = sep[s]; - expect = expect_values[s]; - - for (j = 0; j < sizeof(b) / sizeof(char*); ++j) { - for (i = 0; i < sizeof(a) / sizeof(char*); ++i) { - git_buf_join(&buf, separator, a[i], b[j]); - cl_assert_equal_s(*expect, buf.ptr); - expect++; - } - } - } - - git_buf_free(&buf); -} - -void test_core_buffer__10(void) -{ - git_buf a = GIT_BUF_INIT; - - cl_git_pass(git_buf_join_n(&a, '/', 1, "test")); - cl_assert_equal_s(a.ptr, "test"); - cl_git_pass(git_buf_join_n(&a, '/', 1, "string")); - cl_assert_equal_s(a.ptr, "test/string"); - git_buf_clear(&a); - cl_git_pass(git_buf_join_n(&a, '/', 3, "test", "string", "join")); - cl_assert_equal_s(a.ptr, "test/string/join"); - cl_git_pass(git_buf_join_n(&a, '/', 2, a.ptr, "more")); - cl_assert_equal_s(a.ptr, "test/string/join/test/string/join/more"); - - git_buf_free(&a); -} - -void test_core_buffer__11(void) -{ - git_buf a = GIT_BUF_INIT; - git_strarray t; - char *t1[] = { "nothing", "in", "common" }; - char *t2[] = { "something", "something else", "some other" }; - char *t3[] = { "something", "some fun", "no fun" }; - char *t4[] = { "happy", "happier", "happiest" }; - char *t5[] = { "happiest", "happier", "happy" }; - char *t6[] = { "no", "nope", "" }; - char *t7[] = { "", "doesn't matter" }; - - t.strings = t1; - t.count = 3; - cl_git_pass(git_buf_text_common_prefix(&a, &t)); - cl_assert_equal_s(a.ptr, ""); - - t.strings = t2; - t.count = 3; - cl_git_pass(git_buf_text_common_prefix(&a, &t)); - cl_assert_equal_s(a.ptr, "some"); - - t.strings = t3; - t.count = 3; - cl_git_pass(git_buf_text_common_prefix(&a, &t)); - cl_assert_equal_s(a.ptr, ""); - - t.strings = t4; - t.count = 3; - cl_git_pass(git_buf_text_common_prefix(&a, &t)); - cl_assert_equal_s(a.ptr, "happ"); - - t.strings = t5; - t.count = 3; - cl_git_pass(git_buf_text_common_prefix(&a, &t)); - cl_assert_equal_s(a.ptr, "happ"); - - t.strings = t6; - t.count = 3; - cl_git_pass(git_buf_text_common_prefix(&a, &t)); - cl_assert_equal_s(a.ptr, ""); - - t.strings = t7; - t.count = 3; - cl_git_pass(git_buf_text_common_prefix(&a, &t)); - cl_assert_equal_s(a.ptr, ""); - - git_buf_free(&a); -} - -void test_core_buffer__rfind_variants(void) -{ - git_buf a = GIT_BUF_INIT; - ssize_t len; - - cl_git_pass(git_buf_sets(&a, "/this/is/it/")); - - len = (ssize_t)git_buf_len(&a); - - cl_assert(git_buf_rfind(&a, '/') == len - 1); - cl_assert(git_buf_rfind_next(&a, '/') == len - 4); - - cl_assert(git_buf_rfind(&a, 'i') == len - 3); - cl_assert(git_buf_rfind_next(&a, 'i') == len - 3); - - cl_assert(git_buf_rfind(&a, 'h') == 2); - cl_assert(git_buf_rfind_next(&a, 'h') == 2); - - cl_assert(git_buf_rfind(&a, 'q') == -1); - cl_assert(git_buf_rfind_next(&a, 'q') == -1); - - git_buf_free(&a); -} - -void test_core_buffer__puts_escaped(void) -{ - git_buf a = GIT_BUF_INIT; - - git_buf_clear(&a); - cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "", "")); - cl_assert_equal_s("this is a test", a.ptr); - - git_buf_clear(&a); - cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "t", "\\")); - cl_assert_equal_s("\\this is a \\tes\\t", a.ptr); - - git_buf_clear(&a); - cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "i ", "__")); - cl_assert_equal_s("th__is__ __is__ a__ test", a.ptr); - - git_buf_clear(&a); - cl_git_pass(git_buf_text_puts_escape_regex(&a, "^match\\s*[A-Z]+.*")); - cl_assert_equal_s("\\^match\\\\s\\*\\[A-Z\\]\\+\\.\\*", a.ptr); - - git_buf_free(&a); -} - -static void assert_unescape(char *expected, char *to_unescape) { - git_buf buf = GIT_BUF_INIT; - - cl_git_pass(git_buf_sets(&buf, to_unescape)); - git_buf_text_unescape(&buf); - cl_assert_equal_s(expected, buf.ptr); - cl_assert_equal_sz(strlen(expected), buf.size); - - git_buf_free(&buf); -} - -void test_core_buffer__unescape(void) -{ - assert_unescape("Escaped\\", "Es\\ca\\ped\\"); - assert_unescape("Es\\caped\\", "Es\\\\ca\\ped\\\\"); - assert_unescape("\\", "\\"); - assert_unescape("\\", "\\\\"); - assert_unescape("", ""); -} - -void test_core_buffer__base64(void) -{ - git_buf buf = GIT_BUF_INIT; - - /* t h i s - * 0x 74 68 69 73 - * 0b 01110100 01101000 01101001 01110011 - * 0b 011101 000110 100001 101001 011100 110000 - * 0x 1d 06 21 29 1c 30 - * d G h p c w - */ - cl_git_pass(git_buf_put_base64(&buf, "this", 4)); - cl_assert_equal_s("dGhpcw==", buf.ptr); - - git_buf_clear(&buf); - cl_git_pass(git_buf_put_base64(&buf, "this!", 5)); - cl_assert_equal_s("dGhpcyE=", buf.ptr); - - git_buf_clear(&buf); - cl_git_pass(git_buf_put_base64(&buf, "this!\n", 6)); - cl_assert_equal_s("dGhpcyEK", buf.ptr); - - git_buf_free(&buf); -} - -void test_core_buffer__classify_with_utf8(void) -{ - char *data0 = "Simple text\n"; - size_t data0len = 12; - char *data1 = "Is that UTF-8 data I see…\nYep!\n"; - size_t data1len = 31; - char *data2 = "Internal NUL!!!\000\n\nI see you!\n"; - size_t data2len = 29; - git_buf b; - - b.ptr = data0; b.size = b.asize = data0len; - cl_assert(!git_buf_text_is_binary(&b)); - cl_assert(!git_buf_text_contains_nul(&b)); - - b.ptr = data1; b.size = b.asize = data1len; - cl_assert(git_buf_text_is_binary(&b)); - cl_assert(!git_buf_text_contains_nul(&b)); - - b.ptr = data2; b.size = b.asize = data2len; - cl_assert(git_buf_text_is_binary(&b)); - cl_assert(git_buf_text_contains_nul(&b)); -} diff --git a/tests-clar/core/copy.c b/tests-clar/core/copy.c deleted file mode 100644 index c0c59c0569e..00000000000 --- a/tests-clar/core/copy.c +++ /dev/null @@ -1,126 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" -#include "path.h" -#include "posix.h" - -void test_core_copy__file(void) -{ - struct stat st; - const char *content = "This is some stuff to copy\n"; - - cl_git_mkfile("copy_me", content); - - cl_git_pass(git_futils_cp("copy_me", "copy_me_two", 0664)); - - cl_git_pass(git_path_lstat("copy_me_two", &st)); - cl_assert(S_ISREG(st.st_mode)); - cl_assert(strlen(content) == (size_t)st.st_size); - - cl_git_pass(p_unlink("copy_me_two")); - cl_git_pass(p_unlink("copy_me")); -} - -void test_core_copy__file_in_dir(void) -{ - struct stat st; - const char *content = "This is some other stuff to copy\n"; - - cl_git_pass(git_futils_mkdir("an_dir/in_a_dir", NULL, 0775, GIT_MKDIR_PATH)); - cl_git_mkfile("an_dir/in_a_dir/copy_me", content); - cl_assert(git_path_isdir("an_dir")); - - cl_git_pass(git_futils_mkpath2file - ("an_dir/second_dir/and_more/copy_me_two", 0775)); - - cl_git_pass(git_futils_cp - ("an_dir/in_a_dir/copy_me", - "an_dir/second_dir/and_more/copy_me_two", - 0664)); - - cl_git_pass(git_path_lstat("an_dir/second_dir/and_more/copy_me_two", &st)); - cl_assert(S_ISREG(st.st_mode)); - cl_assert(strlen(content) == (size_t)st.st_size); - - cl_git_pass(git_futils_rmdir_r("an_dir", NULL, GIT_RMDIR_REMOVE_FILES)); - cl_assert(!git_path_isdir("an_dir")); -} - -void test_core_copy__tree(void) -{ - struct stat st; - const char *content = "File content\n"; - - cl_git_pass(git_futils_mkdir("src/b", NULL, 0775, GIT_MKDIR_PATH)); - cl_git_pass(git_futils_mkdir("src/c/d", NULL, 0775, GIT_MKDIR_PATH)); - cl_git_pass(git_futils_mkdir("src/c/e", NULL, 0775, GIT_MKDIR_PATH)); - - cl_git_mkfile("src/f1", content); - cl_git_mkfile("src/b/f2", content); - cl_git_mkfile("src/c/f3", content); - cl_git_mkfile("src/c/d/f4", content); - cl_git_mkfile("src/c/d/.f5", content); - -#ifndef GIT_WIN32 - cl_assert(p_symlink("../../b/f2", "src/c/d/l1") == 0); -#endif - - cl_assert(git_path_isdir("src")); - cl_assert(git_path_isdir("src/b")); - cl_assert(git_path_isdir("src/c/d")); - cl_assert(git_path_isfile("src/c/d/f4")); - - /* copy with no empty dirs, yes links, no dotfiles, no overwrite */ - - cl_git_pass( - git_futils_cp_r("src", "t1", GIT_CPDIR_COPY_SYMLINKS, 0) ); - - cl_assert(git_path_isdir("t1")); - cl_assert(git_path_isdir("t1/b")); - cl_assert(git_path_isdir("t1/c")); - cl_assert(git_path_isdir("t1/c/d")); - cl_assert(!git_path_isdir("t1/c/e")); - - cl_assert(git_path_isfile("t1/f1")); - cl_assert(git_path_isfile("t1/b/f2")); - cl_assert(git_path_isfile("t1/c/f3")); - cl_assert(git_path_isfile("t1/c/d/f4")); - cl_assert(!git_path_isfile("t1/c/d/.f5")); - - cl_git_pass(git_path_lstat("t1/c/f3", &st)); - cl_assert(S_ISREG(st.st_mode)); - cl_assert(strlen(content) == (size_t)st.st_size); - -#ifndef GIT_WIN32 - cl_git_pass(git_path_lstat("t1/c/d/l1", &st)); - cl_assert(S_ISLNK(st.st_mode)); -#endif - - cl_git_pass(git_futils_rmdir_r("t1", NULL, GIT_RMDIR_REMOVE_FILES)); - cl_assert(!git_path_isdir("t1")); - - /* copy with empty dirs, no links, yes dotfiles, no overwrite */ - - cl_git_pass( - git_futils_cp_r("src", "t2", GIT_CPDIR_CREATE_EMPTY_DIRS | GIT_CPDIR_COPY_DOTFILES, 0) ); - - cl_assert(git_path_isdir("t2")); - cl_assert(git_path_isdir("t2/b")); - cl_assert(git_path_isdir("t2/c")); - cl_assert(git_path_isdir("t2/c/d")); - cl_assert(git_path_isdir("t2/c/e")); - - cl_assert(git_path_isfile("t2/f1")); - cl_assert(git_path_isfile("t2/b/f2")); - cl_assert(git_path_isfile("t2/c/f3")); - cl_assert(git_path_isfile("t2/c/d/f4")); - cl_assert(git_path_isfile("t2/c/d/.f5")); - -#ifndef GIT_WIN32 - cl_git_fail(git_path_lstat("t2/c/d/l1", &st)); -#endif - - cl_git_pass(git_futils_rmdir_r("t2", NULL, GIT_RMDIR_REMOVE_FILES)); - cl_assert(!git_path_isdir("t2")); - - cl_git_pass(git_futils_rmdir_r("src", NULL, GIT_RMDIR_REMOVE_FILES)); -} diff --git a/tests-clar/core/dirent.c b/tests-clar/core/dirent.c deleted file mode 100644 index 5a7859d1b80..00000000000 --- a/tests-clar/core/dirent.c +++ /dev/null @@ -1,235 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" - -typedef struct name_data { - int count; /* return count */ - char *name; /* filename */ -} name_data; - -typedef struct walk_data { - char *sub; /* sub-directory name */ - name_data *names; /* name state data */ - git_buf path; -} walk_data; - - -static char *top_dir = "dir-walk"; -static walk_data *state_loc; - -static void setup(walk_data *d) -{ - name_data *n; - - cl_must_pass(p_mkdir(top_dir, 0777)); - - cl_must_pass(p_chdir(top_dir)); - - if (strcmp(d->sub, ".") != 0) - cl_must_pass(p_mkdir(d->sub, 0777)); - - cl_git_pass(git_buf_sets(&d->path, d->sub)); - - state_loc = d; - - for (n = d->names; n->name; n++) { - git_file fd = p_creat(n->name, 0666); - cl_assert(fd >= 0); - p_close(fd); - n->count = 0; - } -} - -static void dirent_cleanup__cb(void *_d) -{ - walk_data *d = _d; - name_data *n; - - for (n = d->names; n->name; n++) { - cl_must_pass(p_unlink(n->name)); - } - - if (strcmp(d->sub, ".") != 0) - cl_must_pass(p_rmdir(d->sub)); - - cl_must_pass(p_chdir("..")); - - cl_must_pass(p_rmdir(top_dir)); - - git_buf_free(&d->path); -} - -static void check_counts(walk_data *d) -{ - name_data *n; - - for (n = d->names; n->name; n++) { - cl_assert(n->count == 1); - } -} - -static int one_entry(void *state, git_buf *path) -{ - walk_data *d = (walk_data *) state; - name_data *n; - - if (state != state_loc) - return GIT_ERROR; - - if (path != &d->path) - return GIT_ERROR; - - for (n = d->names; n->name; n++) { - if (!strcmp(n->name, path->ptr)) { - n->count++; - return 0; - } - } - - return GIT_ERROR; -} - -static int dont_call_me(void *state, git_buf *path) -{ - GIT_UNUSED(state); - GIT_UNUSED(path); - return GIT_ERROR; -} - - - -static name_data dot_names[] = { - { 0, "./a" }, - { 0, "./asdf" }, - { 0, "./pack-foo.pack" }, - { 0, NULL } -}; -static walk_data dot = { - ".", - dot_names, - GIT_BUF_INIT -}; - -/* make sure that the '.' folder is not traversed */ -void test_core_dirent__dont_traverse_dot(void) -{ - cl_set_cleanup(&dirent_cleanup__cb, &dot); - setup(&dot); - - cl_git_pass(git_path_direach(&dot.path, - one_entry, - &dot)); - - check_counts(&dot); -} - - -static name_data sub_names[] = { - { 0, "sub/a" }, - { 0, "sub/asdf" }, - { 0, "sub/pack-foo.pack" }, - { 0, NULL } -}; -static walk_data sub = { - "sub", - sub_names, - GIT_BUF_INIT -}; - -/* traverse a subfolder */ -void test_core_dirent__traverse_subfolder(void) -{ - cl_set_cleanup(&dirent_cleanup__cb, &sub); - setup(&sub); - - cl_git_pass(git_path_direach(&sub.path, - one_entry, - &sub)); - - check_counts(&sub); -} - - -static walk_data sub_slash = { - "sub/", - sub_names, - GIT_BUF_INIT -}; - -/* traverse a slash-terminated subfolder */ -void test_core_dirent__traverse_slash_terminated_folder(void) -{ - cl_set_cleanup(&dirent_cleanup__cb, &sub_slash); - setup(&sub_slash); - - cl_git_pass(git_path_direach(&sub_slash.path, - one_entry, - &sub_slash)); - - check_counts(&sub_slash); -} - - -static name_data empty_names[] = { - { 0, NULL } -}; -static walk_data empty = { - "empty", - empty_names, - GIT_BUF_INIT -}; - -/* make sure that empty folders are not traversed */ -void test_core_dirent__dont_traverse_empty_folders(void) -{ - cl_set_cleanup(&dirent_cleanup__cb, &empty); - setup(&empty); - - cl_git_pass(git_path_direach(&empty.path, - one_entry, - &empty)); - - check_counts(&empty); - - /* make sure callback not called */ - cl_git_pass(git_path_direach(&empty.path, - dont_call_me, - &empty)); -} - -static name_data odd_names[] = { - { 0, "odd/.a" }, - { 0, "odd/..c" }, - /* the following don't work on cygwin/win32 */ - /* { 0, "odd/.b." }, */ - /* { 0, "odd/..d.." }, */ - { 0, NULL } -}; -static walk_data odd = { - "odd", - odd_names, - GIT_BUF_INIT -}; - -/* make sure that strange looking filenames ('..c') are traversed */ -void test_core_dirent__traverse_weird_filenames(void) -{ - cl_set_cleanup(&dirent_cleanup__cb, &odd); - setup(&odd); - - cl_git_pass(git_path_direach(&odd.path, - one_entry, - &odd)); - - check_counts(&odd); -} - -/* test filename length limits */ -void test_core_dirent__length_limits(void) -{ - char *big_filename = (char *)git__malloc(FILENAME_MAX + 1); - memset(big_filename, 'a', FILENAME_MAX + 1); - big_filename[FILENAME_MAX] = 0; - - cl_must_fail(p_creat(big_filename, 0666)); - git__free(big_filename); -} diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c deleted file mode 100644 index fa48de17e1b..00000000000 --- a/tests-clar/core/env.c +++ /dev/null @@ -1,182 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" -#include "path.h" - -#ifdef GIT_WIN32 -#define NUM_VARS 5 -static const char *env_vars[NUM_VARS] = { - "HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE", "PROGRAMFILES" -}; -#else -#define NUM_VARS 1 -static const char *env_vars[NUM_VARS] = { "HOME" }; -#endif - -static char *env_save[NUM_VARS]; - -static char *home_values[] = { - "fake_home", - "f\xc3\xa1ke_h\xc3\xb5me", /* all in latin-1 supplement */ - "f\xc4\x80ke_\xc4\xa4ome", /* latin extended */ - "f\xce\xb1\xce\xba\xce\xb5_h\xce\xbfm\xce\xad", /* having fun with greek */ - "fa\xe0" "\xb8" "\x87" "e_\xe0" "\xb8" "\x99" "ome", /* thai characters */ - "f\xe1\x9cx80ke_\xe1\x9c\x91ome", /* tagalog characters */ - "\xe1\xb8\x9f\xe1\xba\xa2" "ke_ho" "\xe1" "\xb9" "\x81" "e", /* latin extended additional */ - "\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */ - NULL -}; - -void test_core_env__initialize(void) -{ - int i; - for (i = 0; i < NUM_VARS; ++i) - env_save[i] = cl_getenv(env_vars[i]); -} - -void test_core_env__cleanup(void) -{ - int i; - char **val; - - for (i = 0; i < NUM_VARS; ++i) { - cl_setenv(env_vars[i], env_save[i]); -#ifdef GIT_WIN32 - git__free(env_save[i]); -#endif - env_save[i] = NULL; - } - - /* these will probably have already been cleaned up, but if a test - * fails, then it's probably good to try and clear out these dirs - */ - for (val = home_values; *val != NULL; val++) { - if (**val != '\0') - (void)p_rmdir(*val); - } -} - -static void setenv_and_check(const char *name, const char *value) -{ - char *check; - - cl_git_pass(cl_setenv(name, value)); - check = cl_getenv(name); - cl_assert_equal_s(value, check); -#ifdef GIT_WIN32 - git__free(check); -#endif -} - -void test_core_env__0(void) -{ - git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT; - char testfile[16], tidx = '0'; - char **val; - - memset(testfile, 0, sizeof(testfile)); - memcpy(testfile, "testfile", 8); - cl_assert_equal_s("testfile", testfile); - - for (val = home_values; *val != NULL; val++) { - - /* if we can't make the directory, let's just assume - * we are on a filesystem that doesn't support the - * characters in question and skip this test... - */ - if (p_mkdir(*val, 0777) != 0) { - *val = ""; /* mark as not created */ - continue; - } - - cl_git_pass(git_path_prettify(&path, *val, NULL)); - - /* vary testfile name in each directory so accidentally leaving - * an environment variable set from a previous iteration won't - * accidentally make this test pass... - */ - testfile[8] = tidx++; - cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile)); - cl_git_mkfile(path.ptr, "find me"); - git_buf_rtruncate_at_char(&path, '/'); - - cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); - - setenv_and_check("HOME", path.ptr); - cl_git_pass(git_futils_find_global_file(&found, testfile)); - - cl_setenv("HOME", env_save[0]); - cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); - -#ifdef GIT_WIN32 - setenv_and_check("HOMEDRIVE", NULL); - setenv_and_check("HOMEPATH", NULL); - setenv_and_check("USERPROFILE", path.ptr); - - cl_git_pass(git_futils_find_global_file(&found, testfile)); - - { - int root = git_path_root(path.ptr); - char old; - - if (root >= 0) { - setenv_and_check("USERPROFILE", NULL); - - cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); - - old = path.ptr[root]; - path.ptr[root] = '\0'; - setenv_and_check("HOMEDRIVE", path.ptr); - path.ptr[root] = old; - setenv_and_check("HOMEPATH", &path.ptr[root]); - - cl_git_pass(git_futils_find_global_file(&found, testfile)); - } - } -#endif - - (void)p_rmdir(*val); - } - - git_buf_free(&path); - git_buf_free(&found); -} - -void test_core_env__1(void) -{ - git_buf path = GIT_BUF_INIT; - - cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile")); - - cl_git_pass(cl_setenv("HOME", "doesnotexist")); -#ifdef GIT_WIN32 - cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist")); - cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist")); -#endif - - cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile")); - - cl_git_pass(cl_setenv("HOME", NULL)); -#ifdef GIT_WIN32 - cl_git_pass(cl_setenv("HOMEPATH", NULL)); - cl_git_pass(cl_setenv("USERPROFILE", NULL)); -#endif - - cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile")); - - cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile")); - -#ifdef GIT_WIN32 - cl_git_pass(cl_setenv("PROGRAMFILES", NULL)); - cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile")); -#endif - - git_buf_free(&path); -} diff --git a/tests-clar/core/errors.c b/tests-clar/core/errors.c deleted file mode 100644 index 512a4134d68..00000000000 --- a/tests-clar/core/errors.c +++ /dev/null @@ -1,87 +0,0 @@ -#include "clar_libgit2.h" - -void test_core_errors__public_api(void) -{ - char *str_in_error; - - giterr_clear(); - cl_assert(giterr_last() == NULL); - - giterr_set_oom(); - - cl_assert(giterr_last() != NULL); - cl_assert(giterr_last()->klass == GITERR_NOMEMORY); - str_in_error = strstr(giterr_last()->message, "memory"); - cl_assert(str_in_error != NULL); - - giterr_clear(); - - giterr_set_str(GITERR_REPOSITORY, "This is a test"); - - cl_assert(giterr_last() != NULL); - str_in_error = strstr(giterr_last()->message, "This is a test"); - cl_assert(str_in_error != NULL); - - giterr_clear(); - cl_assert(giterr_last() == NULL); -} - -#include "common.h" -#include "util.h" -#include "posix.h" - -void test_core_errors__new_school(void) -{ - char *str_in_error; - - giterr_clear(); - cl_assert(giterr_last() == NULL); - - giterr_set_oom(); /* internal fn */ - - cl_assert(giterr_last() != NULL); - cl_assert(giterr_last()->klass == GITERR_NOMEMORY); - str_in_error = strstr(giterr_last()->message, "memory"); - cl_assert(str_in_error != NULL); - - giterr_clear(); - - giterr_set(GITERR_REPOSITORY, "This is a test"); /* internal fn */ - - cl_assert(giterr_last() != NULL); - str_in_error = strstr(giterr_last()->message, "This is a test"); - cl_assert(str_in_error != NULL); - - giterr_clear(); - cl_assert(giterr_last() == NULL); - - do { - struct stat st; - memset(&st, 0, sizeof(st)); - cl_assert(p_lstat("this_file_does_not_exist", &st) < 0); - GIT_UNUSED(st); - } while (false); - giterr_set(GITERR_OS, "stat failed"); /* internal fn */ - - cl_assert(giterr_last() != NULL); - str_in_error = strstr(giterr_last()->message, "stat failed"); - cl_assert(str_in_error != NULL); - cl_assert(git__prefixcmp(str_in_error, "stat failed: ") == 0); - cl_assert(strlen(str_in_error) > strlen("stat failed: ")); - -#ifdef GIT_WIN32 - giterr_clear(); - - /* The MSDN docs use this to generate a sample error */ - cl_assert(GetProcessId(NULL) == 0); - giterr_set(GITERR_OS, "GetProcessId failed"); /* internal fn */ - - cl_assert(giterr_last() != NULL); - str_in_error = strstr(giterr_last()->message, "GetProcessId failed"); - cl_assert(str_in_error != NULL); - cl_assert(git__prefixcmp(str_in_error, "GetProcessId failed: ") == 0); - cl_assert(strlen(str_in_error) > strlen("GetProcessId failed: ")); -#endif - - giterr_clear(); -} diff --git a/tests-clar/core/filebuf.c b/tests-clar/core/filebuf.c deleted file mode 100644 index 4451c01c7eb..00000000000 --- a/tests-clar/core/filebuf.c +++ /dev/null @@ -1,92 +0,0 @@ -#include "clar_libgit2.h" -#include "filebuf.h" - -/* make sure git_filebuf_open doesn't delete an existing lock */ -void test_core_filebuf__0(void) -{ - git_filebuf file = GIT_FILEBUF_INIT; - int fd; - char test[] = "test", testlock[] = "test.lock"; - - fd = p_creat(testlock, 0744); //-V536 - - cl_must_pass(fd); - cl_must_pass(p_close(fd)); - - cl_git_fail(git_filebuf_open(&file, test, 0)); - cl_assert(git_path_exists(testlock)); - - cl_must_pass(p_unlink(testlock)); -} - - -/* make sure GIT_FILEBUF_APPEND works as expected */ -void test_core_filebuf__1(void) -{ - git_filebuf file = GIT_FILEBUF_INIT; - int fd; - char test[] = "test"; - - fd = p_creat(test, 0666); //-V536 - cl_must_pass(fd); - cl_must_pass(p_write(fd, "libgit2 rocks\n", 14)); - cl_must_pass(p_close(fd)); - - cl_git_pass(git_filebuf_open(&file, test, GIT_FILEBUF_APPEND)); - cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks")); - cl_git_pass(git_filebuf_commit(&file, 0666)); - - cl_must_pass(p_unlink(test)); -} - - -/* make sure git_filebuf_write writes large buffer correctly */ -void test_core_filebuf__2(void) -{ - git_filebuf file = GIT_FILEBUF_INIT; - char test[] = "test"; - unsigned char buf[4096 * 4]; /* 2 * WRITE_BUFFER_SIZE */ - - memset(buf, 0xfe, sizeof(buf)); - - cl_git_pass(git_filebuf_open(&file, test, 0)); - cl_git_pass(git_filebuf_write(&file, buf, sizeof(buf))); - cl_git_pass(git_filebuf_commit(&file, 0666)); - - cl_must_pass(p_unlink(test)); -} - -/* make sure git_filebuf_cleanup clears the buffer */ -void test_core_filebuf__4(void) -{ - git_filebuf file = GIT_FILEBUF_INIT; - char test[] = "test"; - - cl_assert(file.buffer == NULL); - - cl_git_pass(git_filebuf_open(&file, test, 0)); - cl_assert(file.buffer != NULL); - - git_filebuf_cleanup(&file); - cl_assert(file.buffer == NULL); -} - - -/* make sure git_filebuf_commit clears the buffer */ -void test_core_filebuf__5(void) -{ - git_filebuf file = GIT_FILEBUF_INIT; - char test[] = "test"; - - cl_assert(file.buffer == NULL); - - cl_git_pass(git_filebuf_open(&file, test, 0)); - cl_assert(file.buffer != NULL); - cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks")); - cl_assert(file.buffer != NULL); - - cl_git_pass(git_filebuf_commit(&file, 0666)); - cl_assert(file.buffer == NULL); - - cl_must_pass(p_unlink(test)); -} diff --git a/tests-clar/core/mkdir.c b/tests-clar/core/mkdir.c deleted file mode 100644 index 1e50b43368e..00000000000 --- a/tests-clar/core/mkdir.c +++ /dev/null @@ -1,182 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" -#include "path.h" -#include "posix.h" - -static void cleanup_basic_dirs(void *ref) -{ - GIT_UNUSED(ref); - git_futils_rmdir_r("d0", NULL, GIT_RMDIR_EMPTY_HIERARCHY); - git_futils_rmdir_r("d1", NULL, GIT_RMDIR_EMPTY_HIERARCHY); - git_futils_rmdir_r("d2", NULL, GIT_RMDIR_EMPTY_HIERARCHY); - git_futils_rmdir_r("d3", NULL, GIT_RMDIR_EMPTY_HIERARCHY); - git_futils_rmdir_r("d4", NULL, GIT_RMDIR_EMPTY_HIERARCHY); -} - -void test_core_mkdir__basic(void) -{ - cl_set_cleanup(cleanup_basic_dirs, NULL); - - /* make a directory */ - cl_assert(!git_path_isdir("d0")); - cl_git_pass(git_futils_mkdir("d0", NULL, 0755, 0)); - cl_assert(git_path_isdir("d0")); - - /* make a path */ - cl_assert(!git_path_isdir("d1")); - cl_git_pass(git_futils_mkdir("d1/d1.1/d1.2", NULL, 0755, GIT_MKDIR_PATH)); - cl_assert(git_path_isdir("d1")); - cl_assert(git_path_isdir("d1/d1.1")); - cl_assert(git_path_isdir("d1/d1.1/d1.2")); - - /* make a dir exclusively */ - cl_assert(!git_path_isdir("d2")); - cl_git_pass(git_futils_mkdir("d2", NULL, 0755, GIT_MKDIR_EXCL)); - cl_assert(git_path_isdir("d2")); - - /* make exclusive failure */ - cl_git_fail(git_futils_mkdir("d2", NULL, 0755, GIT_MKDIR_EXCL)); - - /* make a path exclusively */ - cl_assert(!git_path_isdir("d3")); - cl_git_pass(git_futils_mkdir("d3/d3.1/d3.2", NULL, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL)); - cl_assert(git_path_isdir("d3")); - cl_assert(git_path_isdir("d3/d3.1/d3.2")); - - /* make exclusive path failure */ - cl_git_fail(git_futils_mkdir("d3/d3.1/d3.2", NULL, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL)); - /* ??? Should EXCL only apply to the last item in the path? */ - - /* path with trailing slash? */ - cl_assert(!git_path_isdir("d4")); - cl_git_pass(git_futils_mkdir("d4/d4.1/", NULL, 0755, GIT_MKDIR_PATH)); - cl_assert(git_path_isdir("d4/d4.1")); -} - -static void cleanup_basedir(void *ref) -{ - GIT_UNUSED(ref); - git_futils_rmdir_r("base", NULL, GIT_RMDIR_EMPTY_HIERARCHY); -} - -void test_core_mkdir__with_base(void) -{ -#define BASEDIR "base/dir/here" - - cl_set_cleanup(cleanup_basedir, NULL); - - cl_git_pass(git_futils_mkdir(BASEDIR, NULL, 0755, GIT_MKDIR_PATH)); - - cl_git_pass(git_futils_mkdir("a", BASEDIR, 0755, 0)); - cl_assert(git_path_isdir(BASEDIR "/a")); - - cl_git_pass(git_futils_mkdir("b/b1/b2", BASEDIR, 0755, GIT_MKDIR_PATH)); - cl_assert(git_path_isdir(BASEDIR "/b/b1/b2")); - - /* exclusive with existing base */ - cl_git_pass(git_futils_mkdir("c/c1/c2", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL)); - - /* fail: exclusive with duplicated suffix */ - cl_git_fail(git_futils_mkdir("c/c1/c3", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL)); - - /* fail: exclusive with any duplicated component */ - cl_git_fail(git_futils_mkdir("c/cz/cz", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL)); - - /* success: exclusive without path */ - cl_git_pass(git_futils_mkdir("c/c1/c3", BASEDIR, 0755, GIT_MKDIR_EXCL)); - - /* path with shorter base and existing dirs */ - cl_git_pass(git_futils_mkdir("dir/here/d/", "base", 0755, GIT_MKDIR_PATH)); - cl_assert(git_path_isdir("base/dir/here/d")); - - /* fail: path with shorter base and existing dirs */ - cl_git_fail(git_futils_mkdir("dir/here/e/", "base", 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL)); - - /* fail: base with missing components */ - cl_git_fail(git_futils_mkdir("f/", "base/missing", 0755, GIT_MKDIR_PATH)); - - /* success: shift missing component to path */ - cl_git_pass(git_futils_mkdir("missing/f/", "base/", 0755, GIT_MKDIR_PATH)); -} - -static void cleanup_chmod_root(void *ref) -{ - mode_t *mode = ref; - - if (*mode != 0) { - (void)p_umask(*mode); - git__free(mode); - } - - git_futils_rmdir_r("r", NULL, GIT_RMDIR_EMPTY_HIERARCHY); -} - -static void check_mode(mode_t expected, mode_t actual) -{ -#ifdef GIT_WIN32 - /* chmod on Win32 doesn't support exec bit, not group/world bits */ - cl_assert((expected & 0600) == (actual & 0777)); -#else - cl_assert(expected == (actual & 0777)); -#endif -} - -void test_core_mkdir__chmods(void) -{ - struct stat st; - mode_t *old = git__malloc(sizeof(mode_t)); - *old = p_umask(022); - - cl_set_cleanup(cleanup_chmod_root, old); - - cl_git_pass(git_futils_mkdir("r", NULL, 0777, 0)); - - cl_git_pass(git_futils_mkdir("mode/is/important", "r", 0777, GIT_MKDIR_PATH)); - - cl_git_pass(git_path_lstat("r/mode", &st)); - check_mode(0755, st.st_mode); - cl_git_pass(git_path_lstat("r/mode/is", &st)); - check_mode(0755, st.st_mode); - cl_git_pass(git_path_lstat("r/mode/is/important", &st)); - check_mode(0755, st.st_mode); - - cl_git_pass(git_futils_mkdir("mode2/is2/important2", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD)); - - cl_git_pass(git_path_lstat("r/mode2", &st)); - check_mode(0755, st.st_mode); - cl_git_pass(git_path_lstat("r/mode2/is2", &st)); - check_mode(0755, st.st_mode); - cl_git_pass(git_path_lstat("r/mode2/is2/important2", &st)); - check_mode(0777, st.st_mode); - - cl_git_pass(git_futils_mkdir("mode3/is3/important3", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD_PATH)); - - cl_git_pass(git_path_lstat("r/mode3", &st)); - check_mode(0777, st.st_mode); - cl_git_pass(git_path_lstat("r/mode3/is3", &st)); - check_mode(0777, st.st_mode); - cl_git_pass(git_path_lstat("r/mode3/is3/important3", &st)); - check_mode(0777, st.st_mode); - - /* test that we chmod existing dir */ - - cl_git_pass(git_futils_mkdir("mode/is/important", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD)); - - cl_git_pass(git_path_lstat("r/mode", &st)); - check_mode(0755, st.st_mode); - cl_git_pass(git_path_lstat("r/mode/is", &st)); - check_mode(0755, st.st_mode); - cl_git_pass(git_path_lstat("r/mode/is/important", &st)); - check_mode(0777, st.st_mode); - - /* test that we chmod even existing dirs if CHMOD_PATH is set */ - - cl_git_pass(git_futils_mkdir("mode2/is2/important2.1", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD_PATH)); - - cl_git_pass(git_path_lstat("r/mode2", &st)); - check_mode(0777, st.st_mode); - cl_git_pass(git_path_lstat("r/mode2/is2", &st)); - check_mode(0777, st.st_mode); - cl_git_pass(git_path_lstat("r/mode2/is2/important2.1", &st)); - check_mode(0777, st.st_mode); -} diff --git a/tests-clar/core/oid.c b/tests-clar/core/oid.c deleted file mode 100644 index c89713955fc..00000000000 --- a/tests-clar/core/oid.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "clar_libgit2.h" - -static git_oid id; -const char *str_oid = "ae90f12eea699729ed24555e40b9fd669da12a12"; - -void test_core_oid__initialize(void) -{ - cl_git_pass(git_oid_fromstr(&id, str_oid)); -} - -void test_core_oid__streq(void) -{ - cl_assert(git_oid_streq(&id, str_oid) == 0); - cl_assert(git_oid_streq(&id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") == -1); - - cl_assert(git_oid_streq(&id, "deadbeef") == -1); - cl_assert(git_oid_streq(&id, "I'm not an oid.... :)") == -1); -} diff --git a/tests-clar/core/opts.c b/tests-clar/core/opts.c deleted file mode 100644 index 6468b357a14..00000000000 --- a/tests-clar/core/opts.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "clar_libgit2.h" - -void test_core_opts__readwrite(void) -{ - size_t old_val = 0; - size_t new_val = 0; - - git_libgit2_opts(GIT_OPT_GET_MWINDOW_SIZE, &old_val); - git_libgit2_opts(GIT_OPT_SET_MWINDOW_SIZE, (size_t)1234); - git_libgit2_opts(GIT_OPT_GET_MWINDOW_SIZE, &new_val); - - cl_assert(new_val == 1234); - - git_libgit2_opts(GIT_OPT_SET_MWINDOW_SIZE, old_val); - git_libgit2_opts(GIT_OPT_GET_MWINDOW_SIZE, &new_val); - - cl_assert(new_val == old_val); -} diff --git a/tests-clar/core/path.c b/tests-clar/core/path.c deleted file mode 100644 index 894e81f3da8..00000000000 --- a/tests-clar/core/path.c +++ /dev/null @@ -1,480 +0,0 @@ -#include "clar_libgit2.h" -#include - -static void -check_dirname(const char *A, const char *B) -{ - git_buf dir = GIT_BUF_INIT; - char *dir2; - - cl_assert(git_path_dirname_r(&dir, A) >= 0); - cl_assert_equal_s(B, dir.ptr); - git_buf_free(&dir); - - cl_assert((dir2 = git_path_dirname(A)) != NULL); - cl_assert_equal_s(B, dir2); - git__free(dir2); -} - -static void -check_basename(const char *A, const char *B) -{ - git_buf base = GIT_BUF_INIT; - char *base2; - - cl_assert(git_path_basename_r(&base, A) >= 0); - cl_assert_equal_s(B, base.ptr); - git_buf_free(&base); - - cl_assert((base2 = git_path_basename(A)) != NULL); - cl_assert_equal_s(B, base2); - git__free(base2); -} - -static void -check_topdir(const char *A, const char *B) -{ - const char *dir; - - cl_assert((dir = git_path_topdir(A)) != NULL); - cl_assert_equal_s(B, dir); -} - -static void -check_joinpath(const char *path_a, const char *path_b, const char *expected_path) -{ - git_buf joined_path = GIT_BUF_INIT; - - cl_git_pass(git_buf_joinpath(&joined_path, path_a, path_b)); - cl_assert_equal_s(expected_path, joined_path.ptr); - - git_buf_free(&joined_path); -} - -static void -check_joinpath_n( - const char *path_a, - const char *path_b, - const char *path_c, - const char *path_d, - const char *expected_path) -{ - git_buf joined_path = GIT_BUF_INIT; - - cl_git_pass(git_buf_join_n(&joined_path, '/', 4, - path_a, path_b, path_c, path_d)); - cl_assert_equal_s(expected_path, joined_path.ptr); - - git_buf_free(&joined_path); -} - - -/* get the dirname of a path */ -void test_core_path__00_dirname(void) -{ - check_dirname(NULL, "."); - check_dirname("", "."); - check_dirname("a", "."); - check_dirname("/", "/"); - check_dirname("/usr", "/"); - check_dirname("/usr/", "/"); - check_dirname("/usr/lib", "/usr"); - check_dirname("/usr/lib/", "/usr"); - check_dirname("/usr/lib//", "/usr"); - check_dirname("usr/lib", "usr"); - check_dirname("usr/lib/", "usr"); - check_dirname("usr/lib//", "usr"); - check_dirname(".git/", "."); - - check_dirname(REP16("/abc"), REP15("/abc")); - -#ifdef GIT_WIN32 - check_dirname("C:/path/", "C:/"); - check_dirname("C:/path", "C:/"); - check_dirname("//computername/path/", "//computername/"); - check_dirname("//computername/path", "//computername/"); - check_dirname("//computername/sub/path/", "//computername/sub"); - check_dirname("//computername/sub/path", "//computername/sub"); -#endif -} - -/* get the base name of a path */ -void test_core_path__01_basename(void) -{ - check_basename(NULL, "."); - check_basename("", "."); - check_basename("a", "a"); - check_basename("/", "/"); - check_basename("/usr", "usr"); - check_basename("/usr/", "usr"); - check_basename("/usr/lib", "lib"); - check_basename("/usr/lib//", "lib"); - check_basename("usr/lib", "lib"); - - check_basename(REP16("/abc"), "abc"); - check_basename(REP1024("/abc"), "abc"); -} - -/* get the latest component in a path */ -void test_core_path__02_topdir(void) -{ - check_topdir(".git/", ".git/"); - check_topdir("/.git/", ".git/"); - check_topdir("usr/local/.git/", ".git/"); - check_topdir("./.git/", ".git/"); - check_topdir("/usr/.git/", ".git/"); - check_topdir("/", "/"); - check_topdir("a/", "a/"); - - cl_assert(git_path_topdir("/usr/.git") == NULL); - cl_assert(git_path_topdir(".") == NULL); - cl_assert(git_path_topdir("") == NULL); - cl_assert(git_path_topdir("a") == NULL); -} - -/* properly join path components */ -void test_core_path__05_joins(void) -{ - check_joinpath("", "", ""); - check_joinpath("", "a", "a"); - check_joinpath("", "/a", "/a"); - check_joinpath("a", "", "a/"); - check_joinpath("a", "/", "a/"); - check_joinpath("a", "b", "a/b"); - check_joinpath("/", "a", "/a"); - check_joinpath("/", "", "/"); - check_joinpath("/a", "/b", "/a/b"); - check_joinpath("/a", "/b/", "/a/b/"); - check_joinpath("/a/", "b/", "/a/b/"); - check_joinpath("/a/", "/b/", "/a/b/"); - - check_joinpath("/abcd", "/defg", "/abcd/defg"); - check_joinpath("/abcd", "/defg/", "/abcd/defg/"); - check_joinpath("/abcd/", "defg/", "/abcd/defg/"); - check_joinpath("/abcd/", "/defg/", "/abcd/defg/"); - - check_joinpath("/abcdefgh", "/12345678", "/abcdefgh/12345678"); - check_joinpath("/abcdefgh", "/12345678/", "/abcdefgh/12345678/"); - check_joinpath("/abcdefgh/", "12345678/", "/abcdefgh/12345678/"); - - check_joinpath(REP1024("aaaa"), "", REP1024("aaaa") "/"); - check_joinpath(REP1024("aaaa/"), "", REP1024("aaaa/")); - check_joinpath(REP1024("/aaaa"), "", REP1024("/aaaa") "/"); - - check_joinpath(REP1024("aaaa"), REP1024("bbbb"), - REP1024("aaaa") "/" REP1024("bbbb")); - check_joinpath(REP1024("/aaaa"), REP1024("/bbbb"), - REP1024("/aaaa") REP1024("/bbbb")); -} - -/* properly join path components for more than one path */ -void test_core_path__06_long_joins(void) -{ - check_joinpath_n("", "", "", "", ""); - check_joinpath_n("", "a", "", "", "a/"); - check_joinpath_n("a", "", "", "", "a/"); - check_joinpath_n("", "", "", "a", "a"); - check_joinpath_n("a", "b", "", "/c/d/", "a/b/c/d/"); - check_joinpath_n("a", "b", "", "/c/d", "a/b/c/d"); - check_joinpath_n("abcd", "efgh", "ijkl", "mnop", "abcd/efgh/ijkl/mnop"); - check_joinpath_n("abcd/", "efgh/", "ijkl/", "mnop/", "abcd/efgh/ijkl/mnop/"); - check_joinpath_n("/abcd/", "/efgh/", "/ijkl/", "/mnop/", "/abcd/efgh/ijkl/mnop/"); - - check_joinpath_n(REP1024("a"), REP1024("b"), REP1024("c"), REP1024("d"), - REP1024("a") "/" REP1024("b") "/" - REP1024("c") "/" REP1024("d")); - check_joinpath_n(REP1024("/a"), REP1024("/b"), REP1024("/c"), REP1024("/d"), - REP1024("/a") REP1024("/b") - REP1024("/c") REP1024("/d")); -} - - -static void -check_path_to_dir( - const char* path, - const char* expected) -{ - git_buf tgt = GIT_BUF_INIT; - - git_buf_sets(&tgt, path); - cl_git_pass(git_path_to_dir(&tgt)); - cl_assert_equal_s(expected, tgt.ptr); - - git_buf_free(&tgt); -} - -static void -check_string_to_dir( - const char* path, - size_t maxlen, - const char* expected) -{ - size_t len = strlen(path); - char *buf = git__malloc(len + 2); - cl_assert(buf); - - strncpy(buf, path, len + 2); - - git_path_string_to_dir(buf, maxlen); - - cl_assert_equal_s(expected, buf); - - git__free(buf); -} - -/* convert paths to dirs */ -void test_core_path__07_path_to_dir(void) -{ - check_path_to_dir("", ""); - check_path_to_dir(".", "./"); - check_path_to_dir("./", "./"); - check_path_to_dir("a/", "a/"); - check_path_to_dir("ab", "ab/"); - /* make sure we try just under and just over an expansion that will - * require a realloc - */ - check_path_to_dir("abcdef", "abcdef/"); - check_path_to_dir("abcdefg", "abcdefg/"); - check_path_to_dir("abcdefgh", "abcdefgh/"); - check_path_to_dir("abcdefghi", "abcdefghi/"); - check_path_to_dir(REP1024("abcd") "/", REP1024("abcd") "/"); - check_path_to_dir(REP1024("abcd"), REP1024("abcd") "/"); - - check_string_to_dir("", 1, ""); - check_string_to_dir(".", 1, "."); - check_string_to_dir(".", 2, "./"); - check_string_to_dir(".", 3, "./"); - check_string_to_dir("abcd", 3, "abcd"); - check_string_to_dir("abcd", 4, "abcd"); - check_string_to_dir("abcd", 5, "abcd/"); - check_string_to_dir("abcd", 6, "abcd/"); -} - -/* join path to itself */ -void test_core_path__08_self_join(void) -{ - git_buf path = GIT_BUF_INIT; - size_t asize = 0; - - asize = path.asize; - cl_git_pass(git_buf_sets(&path, "/foo")); - cl_assert_equal_s(path.ptr, "/foo"); - cl_assert(asize < path.asize); - - asize = path.asize; - cl_git_pass(git_buf_joinpath(&path, path.ptr, "this is a new string")); - cl_assert_equal_s(path.ptr, "/foo/this is a new string"); - cl_assert(asize < path.asize); - - asize = path.asize; - cl_git_pass(git_buf_joinpath(&path, path.ptr, "/grow the buffer, grow the buffer, grow the buffer")); - cl_assert_equal_s(path.ptr, "/foo/this is a new string/grow the buffer, grow the buffer, grow the buffer"); - cl_assert(asize < path.asize); - - git_buf_free(&path); - cl_git_pass(git_buf_sets(&path, "/foo/bar")); - - cl_git_pass(git_buf_joinpath(&path, path.ptr + 4, "baz")); - cl_assert_equal_s(path.ptr, "/bar/baz"); - - asize = path.asize; - cl_git_pass(git_buf_joinpath(&path, path.ptr + 4, "somethinglongenoughtorealloc")); - cl_assert_equal_s(path.ptr, "/baz/somethinglongenoughtorealloc"); - cl_assert(asize < path.asize); - - git_buf_free(&path); -} - -static void check_percent_decoding(const char *expected_result, const char *input) -{ - git_buf buf = GIT_BUF_INIT; - - cl_git_pass(git__percent_decode(&buf, input)); - cl_assert_equal_s(expected_result, git_buf_cstr(&buf)); - - git_buf_free(&buf); -} - -void test_core_path__09_percent_decode(void) -{ - check_percent_decoding("abcd", "abcd"); - check_percent_decoding("a2%", "a2%"); - check_percent_decoding("a2%3", "a2%3"); - check_percent_decoding("a2%%3", "a2%%3"); - check_percent_decoding("a2%3z", "a2%3z"); - check_percent_decoding("a,", "a%2c"); - check_percent_decoding("a21", "a2%31"); - check_percent_decoding("a2%1", "a2%%31"); - check_percent_decoding("a bc ", "a%20bc%20"); - check_percent_decoding("Vicent Mart" "\355", "Vicent%20Mart%ED"); -} - -static void check_fromurl(const char *expected_result, const char *input, int should_fail) -{ - git_buf buf = GIT_BUF_INIT; - - assert(should_fail || expected_result); - - if (!should_fail) { - cl_git_pass(git_path_fromurl(&buf, input)); - cl_assert_equal_s(expected_result, git_buf_cstr(&buf)); - } else - cl_git_fail(git_path_fromurl(&buf, input)); - - git_buf_free(&buf); -} - -#ifdef _MSC_VER -#define ABS_PATH_MARKER "" -#else -#define ABS_PATH_MARKER "/" -#endif - -void test_core_path__10_fromurl(void) -{ - /* Failing cases */ - check_fromurl(NULL, "a", 1); - check_fromurl(NULL, "http:///c:/Temp%20folder/note.txt", 1); - check_fromurl(NULL, "file://c:/Temp%20folder/note.txt", 1); - check_fromurl(NULL, "file:////c:/Temp%20folder/note.txt", 1); - check_fromurl(NULL, "file:///", 1); - check_fromurl(NULL, "file:////", 1); - check_fromurl(NULL, "file://servername/c:/Temp%20folder/note.txt", 1); - - /* Passing cases */ - check_fromurl(ABS_PATH_MARKER "c:/Temp folder/note.txt", "file:///c:/Temp%20folder/note.txt", 0); - check_fromurl(ABS_PATH_MARKER "c:/Temp folder/note.txt", "file://localhost/c:/Temp%20folder/note.txt", 0); - check_fromurl(ABS_PATH_MARKER "c:/Temp+folder/note.txt", "file:///c:/Temp+folder/note.txt", 0); - check_fromurl(ABS_PATH_MARKER "a", "file:///a", 0); -} - -typedef struct { - int expect_idx; - char **expect; -} check_walkup_info; - -static int check_one_walkup_step(void *ref, git_buf *path) -{ - check_walkup_info *info = (check_walkup_info *)ref; - cl_assert(info->expect[info->expect_idx] != NULL); - cl_assert_equal_s(info->expect[info->expect_idx], path->ptr); - info->expect_idx++; - return 0; -} - -void test_core_path__11_walkup(void) -{ - git_buf p = GIT_BUF_INIT; - char *expect[] = { - "/a/b/c/d/e/", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL, - "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL, - "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL, - "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL, - "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL, - "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL, - "this is a path", NULL, - "///a///b///c///d///e///", "///a///b///c///d///", "///a///b///c///", "///a///b///", "///a///", "///", NULL, - NULL - }; - char *root[] = { NULL, NULL, "/", "", "/a/b", "/a/b/", NULL, NULL, NULL }; - int i, j; - check_walkup_info info; - - info.expect = expect; - - for (i = 0, j = 0; expect[i] != NULL; i++, j++) { - - git_buf_sets(&p, expect[i]); - - info.expect_idx = i; - cl_git_pass( - git_path_walk_up(&p, root[j], check_one_walkup_step, &info) - ); - - cl_assert_equal_s(p.ptr, expect[i]); - - /* skip to next run of expectations */ - while (expect[i] != NULL) i++; - } - - git_buf_free(&p); -} - -void test_core_path__12_offset_to_path_root(void) -{ - cl_assert(git_path_root("non/rooted/path") == -1); - cl_assert(git_path_root("/rooted/path") == 0); - -#ifdef GIT_WIN32 - /* Windows specific tests */ - cl_assert(git_path_root("C:non/rooted/path") == -1); - cl_assert(git_path_root("C:/rooted/path") == 2); - cl_assert(git_path_root("//computername/sharefolder/resource") == 14); - cl_assert(git_path_root("//computername/sharefolder") == 14); - cl_assert(git_path_root("//computername") == -1); -#endif -} - -#define NON_EXISTING_FILEPATH "i_hope_i_do_not_exist" - -void test_core_path__13_cannot_prettify_a_non_existing_file(void) -{ - git_buf p = GIT_BUF_INIT; - - cl_must_pass(git_path_exists(NON_EXISTING_FILEPATH) == false); - cl_assert_equal_i(GIT_ENOTFOUND, git_path_prettify(&p, NON_EXISTING_FILEPATH, NULL)); - cl_assert_equal_i(GIT_ENOTFOUND, git_path_prettify(&p, NON_EXISTING_FILEPATH "/so-do-i", NULL)); - - git_buf_free(&p); -} - -void test_core_path__14_apply_relative(void) -{ - git_buf p = GIT_BUF_INIT; - - cl_git_pass(git_buf_sets(&p, "/this/is/a/base")); - - cl_git_pass(git_path_apply_relative(&p, "../test")); - cl_assert_equal_s("/this/is/a/test", p.ptr); - - cl_git_pass(git_path_apply_relative(&p, "../../the/./end")); - cl_assert_equal_s("/this/is/the/end", p.ptr); - - cl_git_pass(git_path_apply_relative(&p, "./of/this/../the/string")); - cl_assert_equal_s("/this/is/the/end/of/the/string", p.ptr); - - cl_git_pass(git_path_apply_relative(&p, "../../../../../..")); - cl_assert_equal_s("/this/", p.ptr); - - cl_git_pass(git_path_apply_relative(&p, "../../../../../")); - cl_assert_equal_s("/", p.ptr); - - cl_git_pass(git_path_apply_relative(&p, "../../../../..")); - cl_assert_equal_s("/", p.ptr); - - - cl_git_pass(git_buf_sets(&p, "d:/another/test")); - - cl_git_pass(git_path_apply_relative(&p, "../../../../..")); - cl_assert_equal_s("d:/", p.ptr); - - cl_git_pass(git_path_apply_relative(&p, "from/here/to/../and/./back/.")); - cl_assert_equal_s("d:/from/here/and/back/", p.ptr); - - - cl_git_pass(git_buf_sets(&p, "https://my.url.com/test.git")); - - cl_git_pass(git_path_apply_relative(&p, "../another.git")); - cl_assert_equal_s("https://my.url.com/another.git", p.ptr); - - cl_git_pass(git_path_apply_relative(&p, "../full/path/url.patch")); - cl_assert_equal_s("https://my.url.com/full/path/url.patch", p.ptr); - - cl_git_pass(git_path_apply_relative(&p, "..")); - cl_assert_equal_s("https://my.url.com/full/path/", p.ptr); - - cl_git_pass(git_path_apply_relative(&p, "../../../../../")); - cl_assert_equal_s("https://", p.ptr); - - git_buf_free(&p); -} diff --git a/tests-clar/core/pool.c b/tests-clar/core/pool.c deleted file mode 100644 index 5ed97366ff9..00000000000 --- a/tests-clar/core/pool.c +++ /dev/null @@ -1,85 +0,0 @@ -#include "clar_libgit2.h" -#include "pool.h" -#include "git2/oid.h" - -void test_core_pool__0(void) -{ - int i; - git_pool p; - void *ptr; - - cl_git_pass(git_pool_init(&p, 1, 4000)); - - for (i = 1; i < 10000; i *= 2) { - ptr = git_pool_malloc(&p, i); - cl_assert(ptr != NULL); - cl_assert(git_pool__ptr_in_pool(&p, ptr)); - cl_assert(!git_pool__ptr_in_pool(&p, &i)); - } - - /* 1+2+4+8+16+32+64+128+256+512+1024 -> original block */ - /* 2048 -> 1 block */ - /* 4096 -> 1 block */ - /* 8192 -> 1 block */ - - cl_assert(git_pool__open_pages(&p) + git_pool__full_pages(&p) == 4); - - git_pool_clear(&p); -} - -void test_core_pool__1(void) -{ - int i; - git_pool p; - - cl_git_pass(git_pool_init(&p, 1, 4000)); - - for (i = 2010; i > 0; i--) - cl_assert(git_pool_malloc(&p, i) != NULL); - - /* with fixed page size, allocation must end up with these values */ - cl_assert(git_pool__open_pages(&p) == 1); - cl_assert(git_pool__full_pages(&p) == 505); - - git_pool_clear(&p); - - cl_git_pass(git_pool_init(&p, 1, 4100)); - - for (i = 2010; i > 0; i--) - cl_assert(git_pool_malloc(&p, i) != NULL); - - /* with fixed page size, allocation must end up with these values */ - cl_assert(git_pool__open_pages(&p) == 1); - cl_assert(git_pool__full_pages(&p) == 492); - - git_pool_clear(&p); -} - -static char to_hex[] = "0123456789abcdef"; - -void test_core_pool__2(void) -{ - git_pool p; - char oid_hex[GIT_OID_HEXSZ]; - git_oid *oid; - int i, j; - - memset(oid_hex, '0', sizeof(oid_hex)); - - cl_git_pass(git_pool_init(&p, sizeof(git_oid), 100)); - - for (i = 1000; i < 10000; i++) { - oid = git_pool_malloc(&p, 1); - cl_assert(oid != NULL); - - for (j = 0; j < 8; j++) - oid_hex[j] = to_hex[(i >> (4 * j)) & 0x0f]; - cl_git_pass(git_oid_fromstr(oid, oid_hex)); - } - - /* with fixed page size, allocation must end up with these values */ - cl_assert(git_pool__open_pages(&p) == 0); - cl_assert(git_pool__full_pages(&p) == 90); - - git_pool_clear(&p); -} diff --git a/tests-clar/core/rmdir.c b/tests-clar/core/rmdir.c deleted file mode 100644 index f0b0bfa4293..00000000000 --- a/tests-clar/core/rmdir.c +++ /dev/null @@ -1,98 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" - -static const char *empty_tmp_dir = "test_gitfo_rmdir_recurs_test"; - -void test_core_rmdir__initialize(void) -{ - git_buf path = GIT_BUF_INIT; - - cl_must_pass(p_mkdir(empty_tmp_dir, 0777)); - - cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one")); - cl_must_pass(p_mkdir(path.ptr, 0777)); - - cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one/two_one")); - cl_must_pass(p_mkdir(path.ptr, 0777)); - - cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one/two_two")); - cl_must_pass(p_mkdir(path.ptr, 0777)); - - cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one/two_two/three")); - cl_must_pass(p_mkdir(path.ptr, 0777)); - - cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/two")); - cl_must_pass(p_mkdir(path.ptr, 0777)); - - git_buf_free(&path); -} - -/* make sure empty dir can be deleted recusively */ -void test_core_rmdir__delete_recursive(void) -{ - cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); -} - -/* make sure non-empty dir cannot be deleted recusively */ -void test_core_rmdir__fail_to_delete_non_empty_dir(void) -{ - git_buf file = GIT_BUF_INIT; - - cl_git_pass(git_buf_joinpath(&file, empty_tmp_dir, "/two/file.txt")); - - cl_git_mkfile(git_buf_cstr(&file), "dummy"); - - cl_git_fail(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); - - cl_must_pass(p_unlink(file.ptr)); - cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); - - git_buf_free(&file); -} - -void test_core_rmdir__can_skip_non_empty_dir(void) -{ - git_buf file = GIT_BUF_INIT; - - cl_git_pass(git_buf_joinpath(&file, empty_tmp_dir, "/two/file.txt")); - - cl_git_mkfile(git_buf_cstr(&file), "dummy"); - - cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_SKIP_NONEMPTY)); - cl_assert(git_path_exists(git_buf_cstr(&file)) == true); - - cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_REMOVE_FILES)); - cl_assert(git_path_exists(empty_tmp_dir) == false); - - git_buf_free(&file); -} - -void test_core_rmdir__can_remove_empty_parents(void) -{ - git_buf file = GIT_BUF_INIT; - - cl_git_pass( - git_buf_joinpath(&file, empty_tmp_dir, "/one/two_two/three/file.txt")); - cl_git_mkfile(git_buf_cstr(&file), "dummy"); - cl_assert(git_path_isfile(git_buf_cstr(&file))); - - cl_git_pass(git_futils_rmdir_r("one/two_two/three/file.txt", empty_tmp_dir, - GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_EMPTY_PARENTS)); - - cl_assert(!git_path_exists(git_buf_cstr(&file))); - - git_buf_rtruncate_at_char(&file, '/'); /* three (only contained file.txt) */ - cl_assert(!git_path_exists(git_buf_cstr(&file))); - - git_buf_rtruncate_at_char(&file, '/'); /* two_two (only contained three) */ - cl_assert(!git_path_exists(git_buf_cstr(&file))); - - git_buf_rtruncate_at_char(&file, '/'); /* one (contained two_one also) */ - cl_assert(git_path_exists(git_buf_cstr(&file))); - - cl_assert(git_path_exists(empty_tmp_dir) == true); - - git_buf_free(&file); - - cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); -} diff --git a/tests-clar/core/string.c b/tests-clar/core/string.c deleted file mode 100644 index bf6ec0a801a..00000000000 --- a/tests-clar/core/string.c +++ /dev/null @@ -1,28 +0,0 @@ -#include "clar_libgit2.h" - -/* compare prefixes */ -void test_core_string__0(void) -{ - cl_assert(git__prefixcmp("", "") == 0); - cl_assert(git__prefixcmp("a", "") == 0); - cl_assert(git__prefixcmp("", "a") < 0); - cl_assert(git__prefixcmp("a", "b") < 0); - cl_assert(git__prefixcmp("b", "a") > 0); - cl_assert(git__prefixcmp("ab", "a") == 0); - cl_assert(git__prefixcmp("ab", "ac") < 0); - cl_assert(git__prefixcmp("ab", "aa") > 0); -} - -/* compare suffixes */ -void test_core_string__1(void) -{ - cl_assert(git__suffixcmp("", "") == 0); - cl_assert(git__suffixcmp("a", "") == 0); - cl_assert(git__suffixcmp("", "a") < 0); - cl_assert(git__suffixcmp("a", "b") < 0); - cl_assert(git__suffixcmp("b", "a") > 0); - cl_assert(git__suffixcmp("ba", "a") == 0); - cl_assert(git__suffixcmp("zaa", "ac") < 0); - cl_assert(git__suffixcmp("zaz", "ac") > 0); -} - diff --git a/tests-clar/core/strmap.c b/tests-clar/core/strmap.c deleted file mode 100644 index f34a4f89f7a..00000000000 --- a/tests-clar/core/strmap.c +++ /dev/null @@ -1,102 +0,0 @@ -#include "clar_libgit2.h" -#include "strmap.h" - -GIT__USE_STRMAP; - -void test_core_strmap__0(void) -{ - git_strmap *table = git_strmap_alloc(); - cl_assert(table != NULL); - cl_assert(git_strmap_num_entries(table) == 0); - git_strmap_free(table); -} - -static void insert_strings(git_strmap *table, int count) -{ - int i, j, over, err; - char *str; - - for (i = 0; i < count; ++i) { - str = malloc(10); - for (j = 0; j < 10; ++j) - str[j] = 'a' + (i % 26); - str[9] = '\0'; - - /* if > 26, then encode larger value in first letters */ - for (j = 0, over = i / 26; over > 0; j++, over = over / 26) - str[j] = 'A' + (over % 26); - - git_strmap_insert(table, str, str, err); - cl_assert(err >= 0); - } - - cl_assert((int)git_strmap_num_entries(table) == count); -} - -void test_core_strmap__1(void) -{ - int i; - char *str; - git_strmap *table = git_strmap_alloc(); - cl_assert(table != NULL); - - insert_strings(table, 20); - - cl_assert(git_strmap_exists(table, "aaaaaaaaa")); - cl_assert(git_strmap_exists(table, "ggggggggg")); - cl_assert(!git_strmap_exists(table, "aaaaaaaab")); - cl_assert(!git_strmap_exists(table, "abcdefghi")); - - i = 0; - git_strmap_foreach_value(table, str, { i++; free(str); }); - cl_assert(i == 20); - - git_strmap_free(table); -} - -void test_core_strmap__2(void) -{ - khiter_t pos; - int i; - char *str; - git_strmap *table = git_strmap_alloc(); - cl_assert(table != NULL); - - insert_strings(table, 20); - - cl_assert(git_strmap_exists(table, "aaaaaaaaa")); - cl_assert(git_strmap_exists(table, "ggggggggg")); - cl_assert(!git_strmap_exists(table, "aaaaaaaab")); - cl_assert(!git_strmap_exists(table, "abcdefghi")); - - cl_assert(git_strmap_exists(table, "bbbbbbbbb")); - pos = git_strmap_lookup_index(table, "bbbbbbbbb"); - cl_assert(git_strmap_valid_index(table, pos)); - cl_assert_equal_s(git_strmap_value_at(table, pos), "bbbbbbbbb"); - free(git_strmap_value_at(table, pos)); - git_strmap_delete_at(table, pos); - - cl_assert(!git_strmap_exists(table, "bbbbbbbbb")); - - i = 0; - git_strmap_foreach_value(table, str, { i++; free(str); }); - cl_assert(i == 19); - - git_strmap_free(table); -} - -void test_core_strmap__3(void) -{ - int i; - char *str; - git_strmap *table = git_strmap_alloc(); - cl_assert(table != NULL); - - insert_strings(table, 10000); - - i = 0; - git_strmap_foreach_value(table, str, { i++; free(str); }); - cl_assert(i == 10000); - - git_strmap_free(table); -} diff --git a/tests-clar/core/strtol.c b/tests-clar/core/strtol.c deleted file mode 100644 index 8765e042b1c..00000000000 --- a/tests-clar/core/strtol.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "clar_libgit2.h" - -void test_core_strtol__int32(void) -{ - int32_t i; - - cl_git_pass(git__strtol32(&i, "123", NULL, 10)); - cl_assert(i == 123); - cl_git_pass(git__strtol32(&i, " +123 ", NULL, 10)); - cl_assert(i == 123); - cl_git_pass(git__strtol32(&i, " +2147483647 ", NULL, 10)); - cl_assert(i == 2147483647); - cl_git_pass(git__strtol32(&i, " -2147483648 ", NULL, 10)); - cl_assert(i == -2147483648LL); - - cl_git_fail(git__strtol32(&i, " 2147483657 ", NULL, 10)); - cl_git_fail(git__strtol32(&i, " -2147483657 ", NULL, 10)); -} - -void test_core_strtol__int64(void) -{ - int64_t i; - - cl_git_pass(git__strtol64(&i, "123", NULL, 10)); - cl_assert(i == 123); - cl_git_pass(git__strtol64(&i, " +123 ", NULL, 10)); - cl_assert(i == 123); - cl_git_pass(git__strtol64(&i, " +2147483647 ", NULL, 10)); - cl_assert(i == 2147483647); - cl_git_pass(git__strtol64(&i, " -2147483648 ", NULL, 10)); - cl_assert(i == -2147483648LL); - cl_git_pass(git__strtol64(&i, " 2147483657 ", NULL, 10)); - cl_assert(i == 2147483657LL); - cl_git_pass(git__strtol64(&i, " -2147483657 ", NULL, 10)); - cl_assert(i == -2147483657LL); -} - diff --git a/tests-clar/core/vector.c b/tests-clar/core/vector.c deleted file mode 100644 index c9e43a14999..00000000000 --- a/tests-clar/core/vector.c +++ /dev/null @@ -1,275 +0,0 @@ -#include "clar_libgit2.h" -#include "vector.h" - -/* initial size of 1 would cause writing past array bounds */ -void test_core_vector__0(void) -{ - git_vector x; - int i; - git_vector_init(&x, 1, NULL); - for (i = 0; i < 10; ++i) { - git_vector_insert(&x, (void*) 0xabc); - } - git_vector_free(&x); -} - - -/* don't read past array bounds on remove() */ -void test_core_vector__1(void) -{ - git_vector x; - // make initial capacity exact for our insertions. - git_vector_init(&x, 3, NULL); - git_vector_insert(&x, (void*) 0xabc); - git_vector_insert(&x, (void*) 0xdef); - git_vector_insert(&x, (void*) 0x123); - - git_vector_remove(&x, 0); // used to read past array bounds. - git_vector_free(&x); -} - - -static int test_cmp(const void *a, const void *b) -{ - return *(const int *)a - *(const int *)b; -} - -/* remove duplicates */ -void test_core_vector__2(void) -{ - git_vector x; - int *ptrs[2]; - - ptrs[0] = git__malloc(sizeof(int)); - ptrs[1] = git__malloc(sizeof(int)); - - *ptrs[0] = 2; - *ptrs[1] = 1; - - cl_git_pass(git_vector_init(&x, 5, test_cmp)); - cl_git_pass(git_vector_insert(&x, ptrs[0])); - cl_git_pass(git_vector_insert(&x, ptrs[1])); - cl_git_pass(git_vector_insert(&x, ptrs[1])); - cl_git_pass(git_vector_insert(&x, ptrs[0])); - cl_git_pass(git_vector_insert(&x, ptrs[1])); - cl_assert(x.length == 5); - - git_vector_uniq(&x); - cl_assert(x.length == 2); - - git_vector_free(&x); - - git__free(ptrs[0]); - git__free(ptrs[1]); -} - - -static int compare_them(const void *a, const void *b) -{ - return (int)((long)a - (long)b); -} - -/* insert_sorted */ -void test_core_vector__3(void) -{ - git_vector x; - long i; - git_vector_init(&x, 1, &compare_them); - - for (i = 0; i < 10; i += 2) { - git_vector_insert_sorted(&x, (void*)(i + 1), NULL); - } - - for (i = 9; i > 0; i -= 2) { - git_vector_insert_sorted(&x, (void*)(i + 1), NULL); - } - - cl_assert(x.length == 10); - for (i = 0; i < 10; ++i) { - cl_assert(git_vector_get(&x, i) == (void*)(i + 1)); - } - - git_vector_free(&x); -} - -/* insert_sorted with duplicates */ -void test_core_vector__4(void) -{ - git_vector x; - long i; - git_vector_init(&x, 1, &compare_them); - - for (i = 0; i < 10; i += 2) { - git_vector_insert_sorted(&x, (void*)(i + 1), NULL); - } - - for (i = 9; i > 0; i -= 2) { - git_vector_insert_sorted(&x, (void*)(i + 1), NULL); - } - - for (i = 0; i < 10; i += 2) { - git_vector_insert_sorted(&x, (void*)(i + 1), NULL); - } - - for (i = 9; i > 0; i -= 2) { - git_vector_insert_sorted(&x, (void*)(i + 1), NULL); - } - - cl_assert(x.length == 20); - for (i = 0; i < 20; ++i) { - cl_assert(git_vector_get(&x, i) == (void*)(i / 2 + 1)); - } - - git_vector_free(&x); -} - -typedef struct { - int content; - int count; -} my_struct; - -static int _struct_count = 0; - -static int compare_structs(const void *a, const void *b) -{ - return ((const my_struct *)a)->content - - ((const my_struct *)b)->content; -} - -static int merge_structs(void **old_raw, void *new) -{ - my_struct *old = *(my_struct **)old_raw; - cl_assert(((my_struct *)old)->content == ((my_struct *)new)->content); - ((my_struct *)old)->count += 1; - git__free(new); - _struct_count--; - return GIT_EEXISTS; -} - -static my_struct *alloc_struct(int value) -{ - my_struct *st = git__malloc(sizeof(my_struct)); - st->content = value; - st->count = 0; - _struct_count++; - return st; -} - -/* insert_sorted with duplicates and special handling */ -void test_core_vector__5(void) -{ - git_vector x; - int i; - - git_vector_init(&x, 1, &compare_structs); - - for (i = 0; i < 10; i += 2) - git_vector_insert_sorted(&x, alloc_struct(i), &merge_structs); - - for (i = 9; i > 0; i -= 2) - git_vector_insert_sorted(&x, alloc_struct(i), &merge_structs); - - cl_assert(x.length == 10); - cl_assert(_struct_count == 10); - - for (i = 0; i < 10; i += 2) - git_vector_insert_sorted(&x, alloc_struct(i), &merge_structs); - - for (i = 9; i > 0; i -= 2) - git_vector_insert_sorted(&x, alloc_struct(i), &merge_structs); - - cl_assert(x.length == 10); - cl_assert(_struct_count == 10); - - for (i = 0; i < 10; ++i) { - cl_assert(((my_struct *)git_vector_get(&x, i))->content == i); - git__free(git_vector_get(&x, i)); - _struct_count--; - } - - git_vector_free(&x); -} - -static int remove_ones(const git_vector *v, size_t idx) -{ - return (git_vector_get(v, idx) == (void *)0x001); -} - -/* Test removal based on callback */ -void test_core_vector__remove_matching(void) -{ - git_vector x; - size_t i; - void *compare; - - git_vector_init(&x, 1, NULL); - git_vector_insert(&x, (void*) 0x001); - - cl_assert(x.length == 1); - git_vector_remove_matching(&x, remove_ones); - cl_assert(x.length == 0); - - git_vector_insert(&x, (void*) 0x001); - git_vector_insert(&x, (void*) 0x001); - git_vector_insert(&x, (void*) 0x001); - - cl_assert(x.length == 3); - git_vector_remove_matching(&x, remove_ones); - cl_assert(x.length == 0); - - git_vector_insert(&x, (void*) 0x002); - git_vector_insert(&x, (void*) 0x001); - git_vector_insert(&x, (void*) 0x002); - git_vector_insert(&x, (void*) 0x001); - - cl_assert(x.length == 4); - git_vector_remove_matching(&x, remove_ones); - cl_assert(x.length == 2); - - git_vector_foreach(&x, i, compare) { - cl_assert(compare != (void *)0x001); - } - - git_vector_clear(&x); - - git_vector_insert(&x, (void*) 0x001); - git_vector_insert(&x, (void*) 0x002); - git_vector_insert(&x, (void*) 0x002); - git_vector_insert(&x, (void*) 0x001); - - cl_assert(x.length == 4); - git_vector_remove_matching(&x, remove_ones); - cl_assert(x.length == 2); - - git_vector_foreach(&x, i, compare) { - cl_assert(compare != (void *)0x001); - } - - git_vector_clear(&x); - - git_vector_insert(&x, (void*) 0x002); - git_vector_insert(&x, (void*) 0x001); - git_vector_insert(&x, (void*) 0x002); - git_vector_insert(&x, (void*) 0x001); - - cl_assert(x.length == 4); - git_vector_remove_matching(&x, remove_ones); - cl_assert(x.length == 2); - - git_vector_foreach(&x, i, compare) { - cl_assert(compare != (void *)0x001); - } - - git_vector_clear(&x); - - git_vector_insert(&x, (void*) 0x002); - git_vector_insert(&x, (void*) 0x003); - git_vector_insert(&x, (void*) 0x002); - git_vector_insert(&x, (void*) 0x003); - - cl_assert(x.length == 4); - git_vector_remove_matching(&x, remove_ones); - cl_assert(x.length == 4); - - git_vector_free(&x); -} diff --git a/tests-clar/date/date.c b/tests-clar/date/date.c deleted file mode 100644 index 88881d1e137..00000000000 --- a/tests-clar/date/date.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "clar_libgit2.h" - -#include "util.h" - -void test_date_date__overflow(void) -{ -#ifdef __LP64__ - git_time_t d2038, d2039; - - /* This is expected to fail on a 32-bit machine. */ - cl_git_pass(git__date_parse(&d2038, "2038-1-1")); - cl_git_pass(git__date_parse(&d2039, "2039-1-1")); - cl_assert(d2038 < d2039); -#endif -} diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c deleted file mode 100644 index 4b29c9c9485..00000000000 --- a/tests-clar/diff/blob.c +++ /dev/null @@ -1,427 +0,0 @@ -#include "clar_libgit2.h" -#include "diff_helpers.h" - -static git_repository *g_repo = NULL; -static diff_expects expected; -static git_diff_options opts; -static git_blob *d, *alien; - -void test_diff_blob__initialize(void) -{ - git_oid oid; - - g_repo = cl_git_sandbox_init("attr"); - - GIT_INIT_STRUCTURE(&opts, GIT_DIFF_OPTIONS_VERSION); - opts.context_lines = 1; - opts.interhunk_lines = 0; - - memset(&expected, 0, sizeof(expected)); - - /* tests/resources/attr/root_test4.txt */ - cl_git_pass(git_oid_fromstrn(&oid, "a0f7217a", 8)); - cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &oid, 4)); - - /* alien.png */ - cl_git_pass(git_oid_fromstrn(&oid, "edf3dcee", 8)); - cl_git_pass(git_blob_lookup_prefix(&alien, g_repo, &oid, 4)); -} - -void test_diff_blob__cleanup(void) -{ - git_blob_free(d); - d = NULL; - - git_blob_free(alien); - alien = NULL; - - cl_git_sandbox_cleanup(); -} - -void test_diff_blob__can_compare_text_blobs(void) -{ - git_blob *a, *b, *c; - git_oid a_oid, b_oid, c_oid; - - /* tests/resources/attr/root_test1 */ - cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8)); - cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4)); - - /* tests/resources/attr/root_test2 */ - cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8)); - cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4)); - - /* tests/resources/attr/root_test3 */ - cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16)); - cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 8)); - - /* Doing the equivalent of a `git diff -U1` on these files */ - - /* diff on tests/resources/attr/root_test1 */ - cl_git_pass(git_diff_blobs( - a, b, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected.files_binary); - - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(6, expected.lines); - cl_assert_equal_i(1, expected.line_ctxt); - cl_assert_equal_i(5, expected.line_adds); - cl_assert_equal_i(0, expected.line_dels); - - /* diff on tests/resources/attr/root_test2 */ - memset(&expected, 0, sizeof(expected)); - cl_git_pass(git_diff_blobs( - b, c, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected.files_binary); - - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(15, expected.lines); - cl_assert_equal_i(3, expected.line_ctxt); - cl_assert_equal_i(9, expected.line_adds); - cl_assert_equal_i(3, expected.line_dels); - - /* diff on tests/resources/attr/root_test3 */ - memset(&expected, 0, sizeof(expected)); - cl_git_pass(git_diff_blobs( - a, c, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected.files_binary); - - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(13, expected.lines); - cl_assert_equal_i(0, expected.line_ctxt); - cl_assert_equal_i(12, expected.line_adds); - cl_assert_equal_i(1, expected.line_dels); - - memset(&expected, 0, sizeof(expected)); - cl_git_pass(git_diff_blobs( - c, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected.files_binary); - - cl_assert_equal_i(2, expected.hunks); - cl_assert_equal_i(14, expected.lines); - cl_assert_equal_i(4, expected.line_ctxt); - cl_assert_equal_i(6, expected.line_adds); - cl_assert_equal_i(4, expected.line_dels); - - git_blob_free(a); - git_blob_free(b); - git_blob_free(c); -} - -void test_diff_blob__can_compare_against_null_blobs(void) -{ - git_blob *e = NULL; - - cl_git_pass(git_diff_blobs( - d, e, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(0, expected.files_binary); - - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(14, expected.hunk_old_lines); - cl_assert_equal_i(14, expected.lines); - cl_assert_equal_i(14, expected.line_dels); - - opts.flags |= GIT_DIFF_REVERSE; - memset(&expected, 0, sizeof(expected)); - - cl_git_pass(git_diff_blobs( - d, e, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(0, expected.files_binary); - - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(14, expected.hunk_new_lines); - cl_assert_equal_i(14, expected.lines); - cl_assert_equal_i(14, expected.line_adds); - - opts.flags ^= GIT_DIFF_REVERSE; - memset(&expected, 0, sizeof(expected)); - - cl_git_pass(git_diff_blobs( - alien, NULL, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.files_binary); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(0, expected.hunks); - cl_assert_equal_i(0, expected.lines); - - memset(&expected, 0, sizeof(expected)); - - cl_git_pass(git_diff_blobs( - NULL, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.files_binary); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(0, expected.hunks); - cl_assert_equal_i(0, expected.lines); -} - -static void assert_identical_blobs_comparison(diff_expects *expected) -{ - cl_assert_equal_i(1, expected->files); - cl_assert_equal_i(1, expected->file_status[GIT_DELTA_UNMODIFIED]); - cl_assert_equal_i(0, expected->hunks); - cl_assert_equal_i(0, expected->lines); -} - -void test_diff_blob__can_compare_identical_blobs(void) -{ - cl_git_pass(git_diff_blobs( - d, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(0, expected.files_binary); - assert_identical_blobs_comparison(&expected); - - memset(&expected, 0, sizeof(expected)); - cl_git_pass(git_diff_blobs( - NULL, NULL, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(0, expected.files_binary); - assert_identical_blobs_comparison(&expected); - - memset(&expected, 0, sizeof(expected)); - cl_git_pass(git_diff_blobs( - alien, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert(expected.files_binary > 0); - assert_identical_blobs_comparison(&expected); -} - -static void assert_binary_blobs_comparison(diff_expects *expected) -{ - cl_assert(expected->files_binary > 0); - - cl_assert_equal_i(1, expected->files); - cl_assert_equal_i(1, expected->file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected->hunks); - cl_assert_equal_i(0, expected->lines); -} - -void test_diff_blob__can_compare_two_binary_blobs(void) -{ - git_blob *heart; - git_oid h_oid; - - /* heart.png */ - cl_git_pass(git_oid_fromstrn(&h_oid, "de863bff", 8)); - cl_git_pass(git_blob_lookup_prefix(&heart, g_repo, &h_oid, 4)); - - cl_git_pass(git_diff_blobs( - alien, heart, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - assert_binary_blobs_comparison(&expected); - - memset(&expected, 0, sizeof(expected)); - - cl_git_pass(git_diff_blobs( - heart, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - assert_binary_blobs_comparison(&expected); - - git_blob_free(heart); -} - -void test_diff_blob__can_compare_a_binary_blob_and_a_text_blob(void) -{ - cl_git_pass(git_diff_blobs( - alien, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - assert_binary_blobs_comparison(&expected); - - memset(&expected, 0, sizeof(expected)); - - cl_git_pass(git_diff_blobs( - d, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - assert_binary_blobs_comparison(&expected); -} - -/* - * $ git diff fe773770 a0f7217 - * diff --git a/fe773770 b/a0f7217 - * index fe77377..a0f7217 100644 - * --- a/fe773770 - * +++ b/a0f7217 - * @@ -1,6 +1,6 @@ - * Here is some stuff at the start - * - * -This should go in one hunk - * +This should go in one hunk (first) - * - * Some additional lines - * - * @@ -8,7 +8,7 @@ Down here below the other lines - * - * With even more at the end - * - * -Followed by a second hunk of stuff - * +Followed by a second hunk of stuff (second) - * - * That happens down here - */ -void test_diff_blob__comparing_two_text_blobs_honors_interhunkcontext(void) -{ - git_blob *old_d; - git_oid old_d_oid; - - opts.context_lines = 3; - - /* tests/resources/attr/root_test1 from commit f5b0af1 */ - cl_git_pass(git_oid_fromstrn(&old_d_oid, "fe773770", 8)); - cl_git_pass(git_blob_lookup_prefix(&old_d, g_repo, &old_d_oid, 4)); - - /* Test with default inter-hunk-context (not set) => default is 0 */ - cl_git_pass(git_diff_blobs( - old_d, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(2, expected.hunks); - - /* Test with inter-hunk-context explicitly set to 0 */ - opts.interhunk_lines = 0; - memset(&expected, 0, sizeof(expected)); - cl_git_pass(git_diff_blobs( - old_d, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(2, expected.hunks); - - /* Test with inter-hunk-context explicitly set to 1 */ - opts.interhunk_lines = 1; - memset(&expected, 0, sizeof(expected)); - cl_git_pass(git_diff_blobs( - old_d, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.hunks); - - git_blob_free(old_d); -} - -void test_diff_blob__checks_options_version_too_low(void) -{ - const git_error *err; - - opts.version = 0; - cl_git_fail(git_diff_blobs( - d, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); -} - -void test_diff_blob__checks_options_version_too_high(void) -{ - const git_error *err; - - opts.version = 1024; - cl_git_fail(git_diff_blobs( - d, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); -} - -void test_diff_blob__can_correctly_detect_a_binary_blob_as_binary(void) -{ - /* alien.png */ - cl_assert_equal_i(true, git_blob_is_binary(alien)); -} - -void test_diff_blob__can_correctly_detect_a_textual_blob_as_non_binary(void) -{ - /* tests/resources/attr/root_test4.txt */ - cl_assert_equal_i(false, git_blob_is_binary(d)); -} - -/* - * git_diff_blob_to_buffer tests - */ - -void test_diff_blob__can_compare_blob_to_buffer(void) -{ - git_blob *a; - git_oid a_oid; - const char *a_content = "Hello from the root\n"; - const char *b_content = "Hello from the root\n\nSome additional lines\n\nDown here below\n\n"; - - /* tests/resources/attr/root_test1 */ - cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8)); - cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4)); - - /* diff from blob a to content of b */ - cl_git_pass(git_diff_blob_to_buffer( - a, b_content, strlen(b_content), - &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected.files_binary); - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(6, expected.lines); - cl_assert_equal_i(1, expected.line_ctxt); - cl_assert_equal_i(5, expected.line_adds); - cl_assert_equal_i(0, expected.line_dels); - - /* diff from blob a to content of a */ - memset(&expected, 0, sizeof(expected)); - cl_git_pass(git_diff_blob_to_buffer( - a, a_content, strlen(a_content), - &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - assert_identical_blobs_comparison(&expected); - - /* diff from NULL blob to content of b */ - memset(&expected, 0, sizeof(expected)); - cl_git_pass(git_diff_blob_to_buffer( - NULL, a_content, strlen(a_content), - &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(1, expected.lines); - cl_assert_equal_i(1, expected.line_adds); - - /* diff from blob a to NULL buffer */ - memset(&expected, 0, sizeof(expected)); - cl_git_pass(git_diff_blob_to_buffer( - a, NULL, 0, - &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(1, expected.lines); - cl_assert_equal_i(1, expected.line_dels); - - /* diff with reverse */ - opts.flags ^= GIT_DIFF_REVERSE; - - memset(&expected, 0, sizeof(expected)); - cl_git_pass(git_diff_blob_to_buffer( - a, NULL, 0, - &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(1, expected.lines); - cl_assert_equal_i(1, expected.line_adds); - - git_blob_free(a); -} diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c deleted file mode 100644 index 1436ada034f..00000000000 --- a/tests-clar/diff/diff_helpers.c +++ /dev/null @@ -1,201 +0,0 @@ -#include "clar_libgit2.h" -#include "diff_helpers.h" - -git_tree *resolve_commit_oid_to_tree( - git_repository *repo, - const char *partial_oid) -{ - size_t len = strlen(partial_oid); - git_oid oid; - git_object *obj = NULL; - git_tree *tree = NULL; - - if (git_oid_fromstrn(&oid, partial_oid, len) == 0) - git_object_lookup_prefix(&obj, repo, &oid, len, GIT_OBJ_ANY); - cl_assert(obj); - if (git_object_type(obj) == GIT_OBJ_TREE) - return (git_tree *)obj; - cl_assert(git_object_type(obj) == GIT_OBJ_COMMIT); - cl_git_pass(git_commit_tree(&tree, (git_commit *)obj)); - git_object_free(obj); - return tree; -} - -int diff_file_cb( - const git_diff_delta *delta, - float progress, - void *payload) -{ - diff_expects *e = payload; - - GIT_UNUSED(progress); - - e->files++; - - if (delta->binary) - e->files_binary++; - - cl_assert(delta->status <= GIT_DELTA_TYPECHANGE); - - e->file_status[delta->status] += 1; - - return 0; -} - -int diff_hunk_cb( - const git_diff_delta *delta, - const git_diff_range *range, - const char *header, - size_t header_len, - void *payload) -{ - diff_expects *e = payload; - - GIT_UNUSED(delta); - GIT_UNUSED(header); - GIT_UNUSED(header_len); - - e->hunks++; - e->hunk_old_lines += range->old_lines; - e->hunk_new_lines += range->new_lines; - return 0; -} - -int diff_line_cb( - const git_diff_delta *delta, - const git_diff_range *range, - char line_origin, - const char *content, - size_t content_len, - void *payload) -{ - diff_expects *e = payload; - - GIT_UNUSED(delta); - GIT_UNUSED(range); - GIT_UNUSED(content); - GIT_UNUSED(content_len); - - e->lines++; - switch (line_origin) { - case GIT_DIFF_LINE_CONTEXT: - e->line_ctxt++; - break; - case GIT_DIFF_LINE_ADDITION: - e->line_adds++; - break; - case GIT_DIFF_LINE_ADD_EOFNL: - /* technically not a line add, but we'll count it as such */ - e->line_adds++; - break; - case GIT_DIFF_LINE_DELETION: - e->line_dels++; - break; - case GIT_DIFF_LINE_DEL_EOFNL: - /* technically not a line delete, but we'll count it as such */ - e->line_dels++; - break; - default: - break; - } - return 0; -} - -int diff_foreach_via_iterator( - git_diff_list *diff, - git_diff_file_cb file_cb, - git_diff_hunk_cb hunk_cb, - git_diff_data_cb line_cb, - void *data) -{ - size_t d, num_d = git_diff_num_deltas(diff); - - for (d = 0; d < num_d; ++d) { - git_diff_patch *patch; - const git_diff_delta *delta; - size_t h, num_h; - - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); - cl_assert(delta); - - /* call file_cb for this file */ - if (file_cb != NULL && file_cb(delta, (float)d / num_d, data) != 0) { - git_diff_patch_free(patch); - goto abort; - } - - /* if there are no changes, then the patch will be NULL */ - if (!patch) { - cl_assert(delta->status == GIT_DELTA_UNMODIFIED || delta->binary == 1); - continue; - } - - if (!hunk_cb && !line_cb) { - git_diff_patch_free(patch); - continue; - } - - num_h = git_diff_patch_num_hunks(patch); - - for (h = 0; h < num_h; h++) { - const git_diff_range *range; - const char *hdr; - size_t hdr_len, l, num_l; - - cl_git_pass(git_diff_patch_get_hunk( - &range, &hdr, &hdr_len, &num_l, patch, h)); - - if (hunk_cb && hunk_cb(delta, range, hdr, hdr_len, data) != 0) { - git_diff_patch_free(patch); - goto abort; - } - - for (l = 0; l < num_l; ++l) { - char origin; - const char *line; - size_t line_len; - int old_lineno, new_lineno; - - cl_git_pass(git_diff_patch_get_line_in_hunk( - &origin, &line, &line_len, &old_lineno, &new_lineno, - patch, h, l)); - - if (line_cb && - line_cb(delta, range, origin, line, line_len, data) != 0) { - git_diff_patch_free(patch); - goto abort; - } - } - } - - git_diff_patch_free(patch); - } - - return 0; - -abort: - giterr_clear(); - return GIT_EUSER; -} - -static int diff_print_cb( - const git_diff_delta *delta, - const git_diff_range *range, - char line_origin, /**< GIT_DIFF_LINE_... value from above */ - const char *content, - size_t content_len, - void *payload) -{ - GIT_UNUSED(payload); - GIT_UNUSED(delta); - GIT_UNUSED(range); - GIT_UNUSED(line_origin); - GIT_UNUSED(content_len); - fputs(content, (FILE *)payload); - return 0; -} - -void diff_print(FILE *fp, git_diff_list *diff) -{ - cl_git_pass(git_diff_print_patch(diff, diff_print_cb, fp ? fp : stderr)); -} diff --git a/tests-clar/diff/diff_helpers.h b/tests-clar/diff/diff_helpers.h deleted file mode 100644 index 49c2652854d..00000000000 --- a/tests-clar/diff/diff_helpers.h +++ /dev/null @@ -1,51 +0,0 @@ -#include "fileops.h" -#include "git2/diff.h" - -extern git_tree *resolve_commit_oid_to_tree( - git_repository *repo, const char *partial_oid); - -typedef struct { - int files; - int files_binary; - - int file_status[10]; /* indexed by git_delta_t value */ - - int hunks; - int hunk_new_lines; - int hunk_old_lines; - - int lines; - int line_ctxt; - int line_adds; - int line_dels; -} diff_expects; - -extern int diff_file_cb( - const git_diff_delta *delta, - float progress, - void *cb_data); - -extern int diff_hunk_cb( - const git_diff_delta *delta, - const git_diff_range *range, - const char *header, - size_t header_len, - void *cb_data); - -extern int diff_line_cb( - const git_diff_delta *delta, - const git_diff_range *range, - char line_origin, - const char *content, - size_t content_len, - void *cb_data); - -extern int diff_foreach_via_iterator( - git_diff_list *diff, - git_diff_file_cb file_cb, - git_diff_hunk_cb hunk_cb, - git_diff_data_cb line_cb, - void *data); - -extern void diff_print(FILE *fp, git_diff_list *diff); - diff --git a/tests-clar/diff/diffiter.c b/tests-clar/diff/diffiter.c deleted file mode 100644 index 8d550ec0f39..00000000000 --- a/tests-clar/diff/diffiter.c +++ /dev/null @@ -1,465 +0,0 @@ -#include "clar_libgit2.h" -#include "diff_helpers.h" - -void test_diff_diffiter__initialize(void) -{ -} - -void test_diff_diffiter__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_diff_diffiter__create(void) -{ - git_repository *repo = cl_git_sandbox_init("attr"); - git_diff_list *diff; - size_t d, num_d; - - cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL)); - - num_d = git_diff_num_deltas(diff); - for (d = 0; d < num_d; ++d) { - const git_diff_delta *delta; - cl_git_pass(git_diff_get_patch(NULL, &delta, diff, d)); - } - - git_diff_list_free(diff); -} - -void test_diff_diffiter__iterate_files(void) -{ - git_repository *repo = cl_git_sandbox_init("attr"); - git_diff_list *diff; - size_t d, num_d; - int count = 0; - - cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL)); - - num_d = git_diff_num_deltas(diff); - cl_assert_equal_i(6, (int)num_d); - - for (d = 0; d < num_d; ++d) { - const git_diff_delta *delta; - cl_git_pass(git_diff_get_patch(NULL, &delta, diff, d)); - cl_assert(delta != NULL); - count++; - } - cl_assert_equal_i(6, count); - - git_diff_list_free(diff); -} - -void test_diff_diffiter__iterate_files_2(void) -{ - git_repository *repo = cl_git_sandbox_init("status"); - git_diff_list *diff; - size_t d, num_d; - int count = 0; - - cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL)); - - num_d = git_diff_num_deltas(diff); - cl_assert_equal_i(8, (int)num_d); - - for (d = 0; d < num_d; ++d) { - const git_diff_delta *delta; - cl_git_pass(git_diff_get_patch(NULL, &delta, diff, d)); - cl_assert(delta != NULL); - count++; - } - cl_assert_equal_i(8, count); - - git_diff_list_free(diff); -} - -void test_diff_diffiter__iterate_files_and_hunks(void) -{ - git_repository *repo = cl_git_sandbox_init("status"); - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - size_t d, num_d; - int file_count = 0, hunk_count = 0; - - opts.context_lines = 3; - opts.interhunk_lines = 1; - opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; - - cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts)); - - num_d = git_diff_num_deltas(diff); - - for (d = 0; d < num_d; ++d) { - git_diff_patch *patch; - const git_diff_delta *delta; - size_t h, num_h; - - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); - - cl_assert(delta); - cl_assert(patch); - - file_count++; - - num_h = git_diff_patch_num_hunks(patch); - - for (h = 0; h < num_h; h++) { - const git_diff_range *range; - const char *header; - size_t header_len, num_l; - - cl_git_pass(git_diff_patch_get_hunk( - &range, &header, &header_len, &num_l, patch, h)); - - cl_assert(range); - cl_assert(header); - - hunk_count++; - } - - git_diff_patch_free(patch); - } - - cl_assert_equal_i(13, file_count); - cl_assert_equal_i(8, hunk_count); - - git_diff_list_free(diff); -} - -void test_diff_diffiter__max_size_threshold(void) -{ - git_repository *repo = cl_git_sandbox_init("status"); - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - int file_count = 0, binary_count = 0, hunk_count = 0; - size_t d, num_d; - - opts.context_lines = 3; - opts.interhunk_lines = 1; - opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; - - cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts)); - num_d = git_diff_num_deltas(diff); - - for (d = 0; d < num_d; ++d) { - git_diff_patch *patch; - const git_diff_delta *delta; - - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); - cl_assert(delta); - cl_assert(patch); - - file_count++; - hunk_count += (int)git_diff_patch_num_hunks(patch); - - assert(delta->binary == 0 || delta->binary == 1); - binary_count += delta->binary; - - git_diff_patch_free(patch); - } - - cl_assert_equal_i(13, file_count); - cl_assert_equal_i(0, binary_count); - cl_assert_equal_i(8, hunk_count); - - git_diff_list_free(diff); - - /* try again with low file size threshold */ - - file_count = binary_count = hunk_count = 0; - - opts.context_lines = 3; - opts.interhunk_lines = 1; - opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; - opts.max_size = 50; /* treat anything over 50 bytes as binary! */ - - cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts)); - num_d = git_diff_num_deltas(diff); - - for (d = 0; d < num_d; ++d) { - git_diff_patch *patch; - const git_diff_delta *delta; - - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); - - file_count++; - hunk_count += (int)git_diff_patch_num_hunks(patch); - - assert(delta->binary == 0 || delta->binary == 1); - binary_count += delta->binary; - - git_diff_patch_free(patch); - } - - cl_assert_equal_i(13, file_count); - /* Three files are over the 50 byte threshold: - * - staged_changes_file_deleted - * - staged_changes_modified_file - * - staged_new_file_modified_file - */ - cl_assert_equal_i(3, binary_count); - cl_assert_equal_i(5, hunk_count); - - git_diff_list_free(diff); -} - - -void test_diff_diffiter__iterate_all(void) -{ - git_repository *repo = cl_git_sandbox_init("status"); - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - diff_expects exp = {0}; - size_t d, num_d; - - opts.context_lines = 3; - opts.interhunk_lines = 1; - opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; - - cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts)); - - num_d = git_diff_num_deltas(diff); - for (d = 0; d < num_d; ++d) { - git_diff_patch *patch; - const git_diff_delta *delta; - size_t h, num_h; - - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); - cl_assert(patch && delta); - exp.files++; - - num_h = git_diff_patch_num_hunks(patch); - for (h = 0; h < num_h; h++) { - const git_diff_range *range; - const char *header; - size_t header_len, l, num_l; - - cl_git_pass(git_diff_patch_get_hunk( - &range, &header, &header_len, &num_l, patch, h)); - cl_assert(range && header); - exp.hunks++; - - for (l = 0; l < num_l; ++l) { - char origin; - const char *content; - size_t content_len; - - cl_git_pass(git_diff_patch_get_line_in_hunk( - &origin, &content, &content_len, NULL, NULL, patch, h, l)); - cl_assert(content); - exp.lines++; - } - } - - git_diff_patch_free(patch); - } - - cl_assert_equal_i(13, exp.files); - cl_assert_equal_i(8, exp.hunks); - cl_assert_equal_i(14, exp.lines); - - git_diff_list_free(diff); -} - -static void iterate_over_patch(git_diff_patch *patch, diff_expects *exp) -{ - size_t h, num_h = git_diff_patch_num_hunks(patch), num_l; - - exp->files++; - exp->hunks += (int)num_h; - - /* let's iterate in reverse, just because we can! */ - for (h = 1, num_l = 0; h <= num_h; ++h) - num_l += git_diff_patch_num_lines_in_hunk(patch, num_h - h); - - exp->lines += (int)num_l; -} - -#define PATCH_CACHE 5 - -void test_diff_diffiter__iterate_randomly_while_saving_state(void) -{ - git_repository *repo = cl_git_sandbox_init("status"); - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - diff_expects exp = {0}; - git_diff_patch *patches[PATCH_CACHE]; - size_t p, d, num_d; - - memset(patches, 0, sizeof(patches)); - - opts.context_lines = 3; - opts.interhunk_lines = 1; - opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; - - cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts)); - - num_d = git_diff_num_deltas(diff); - - /* To make sure that references counts work for diff and patch objects, - * this generates patches and randomly caches them. Only when the patch - * is removed from the cache are hunks and lines counted. At the end, - * there are still patches in the cache, so free the diff and try to - * process remaining patches after the diff is freed. - */ - - srand(121212); - p = rand() % PATCH_CACHE; - - for (d = 0; d < num_d; ++d) { - /* take old patch */ - git_diff_patch *patch = patches[p]; - patches[p] = NULL; - - /* cache new patch */ - cl_git_pass(git_diff_get_patch(&patches[p], NULL, diff, d)); - cl_assert(patches[p] != NULL); - - /* process old patch if non-NULL */ - if (patch != NULL) { - iterate_over_patch(patch, &exp); - git_diff_patch_free(patch); - } - - p = rand() % PATCH_CACHE; - } - - /* free diff list now - refcounts should keep things safe */ - git_diff_list_free(diff); - - /* process remaining unprocessed patches */ - for (p = 0; p < PATCH_CACHE; p++) { - git_diff_patch *patch = patches[p]; - - if (patch != NULL) { - iterate_over_patch(patch, &exp); - git_diff_patch_free(patch); - } - } - - /* hopefully it all still added up right */ - cl_assert_equal_i(13, exp.files); - cl_assert_equal_i(8, exp.hunks); - cl_assert_equal_i(14, exp.lines); -} - -/* This output is taken directly from `git diff` on the status test data */ -static const char *expected_patch_text[8] = { - /* 0 */ - "diff --git a/file_deleted b/file_deleted\n" - "deleted file mode 100644\n" - "index 5452d32..0000000\n" - "--- a/file_deleted\n" - "+++ /dev/null\n" - "@@ -1 +0,0 @@\n" - "-file_deleted\n", - /* 1 */ - "diff --git a/modified_file b/modified_file\n" - "index 452e424..0a53963 100644\n" - "--- a/modified_file\n" - "+++ b/modified_file\n" - "@@ -1 +1,2 @@\n" - " modified_file\n" - "+modified_file\n", - /* 2 */ - "diff --git a/staged_changes_file_deleted b/staged_changes_file_deleted\n" - "deleted file mode 100644\n" - "index a6be623..0000000\n" - "--- a/staged_changes_file_deleted\n" - "+++ /dev/null\n" - "@@ -1,2 +0,0 @@\n" - "-staged_changes_file_deleted\n" - "-staged_changes_file_deleted\n", - /* 3 */ - "diff --git a/staged_changes_modified_file b/staged_changes_modified_file\n" - "index 906ee77..011c344 100644\n" - "--- a/staged_changes_modified_file\n" - "+++ b/staged_changes_modified_file\n" - "@@ -1,2 +1,3 @@\n" - " staged_changes_modified_file\n" - " staged_changes_modified_file\n" - "+staged_changes_modified_file\n", - /* 4 */ - "diff --git a/staged_new_file_deleted_file b/staged_new_file_deleted_file\n" - "deleted file mode 100644\n" - "index 90b8c29..0000000\n" - "--- a/staged_new_file_deleted_file\n" - "+++ /dev/null\n" - "@@ -1 +0,0 @@\n" - "-staged_new_file_deleted_file\n", - /* 5 */ - "diff --git a/staged_new_file_modified_file b/staged_new_file_modified_file\n" - "index ed06290..8b090c0 100644\n" - "--- a/staged_new_file_modified_file\n" - "+++ b/staged_new_file_modified_file\n" - "@@ -1 +1,2 @@\n" - " staged_new_file_modified_file\n" - "+staged_new_file_modified_file\n", - /* 6 */ - "diff --git a/subdir/deleted_file b/subdir/deleted_file\n" - "deleted file mode 100644\n" - "index 1888c80..0000000\n" - "--- a/subdir/deleted_file\n" - "+++ /dev/null\n" - "@@ -1 +0,0 @@\n" - "-subdir/deleted_file\n", - /* 7 */ - "diff --git a/subdir/modified_file b/subdir/modified_file\n" - "index a619198..57274b7 100644\n" - "--- a/subdir/modified_file\n" - "+++ b/subdir/modified_file\n" - "@@ -1 +1,2 @@\n" - " subdir/modified_file\n" - "+subdir/modified_file\n" -}; - -void test_diff_diffiter__iterate_and_generate_patch_text(void) -{ - git_repository *repo = cl_git_sandbox_init("status"); - git_diff_list *diff; - size_t d, num_d; - - cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL)); - - num_d = git_diff_num_deltas(diff); - cl_assert_equal_i(8, (int)num_d); - - for (d = 0; d < num_d; ++d) { - git_diff_patch *patch; - char *text; - - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, d)); - cl_assert(patch != NULL); - - cl_git_pass(git_diff_patch_to_str(&text, patch)); - - cl_assert_equal_s(expected_patch_text[d], text); - - git__free(text); - git_diff_patch_free(patch); - } - - git_diff_list_free(diff); -} - -void test_diff_diffiter__checks_options_version(void) -{ - git_repository *repo = cl_git_sandbox_init("status"); - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - const git_error *err; - - opts.version = 0; - opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; - - cl_git_fail(git_diff_index_to_workdir(&diff, repo, NULL, &opts)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); - - giterr_clear(); - opts.version = 1024; - cl_git_fail(git_diff_index_to_workdir(&diff, repo, NULL, &opts)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); -} - diff --git a/tests-clar/diff/index.c b/tests-clar/diff/index.c deleted file mode 100644 index e1c617dae5a..00000000000 --- a/tests-clar/diff/index.c +++ /dev/null @@ -1,167 +0,0 @@ -#include "clar_libgit2.h" -#include "diff_helpers.h" - -static git_repository *g_repo = NULL; - -void test_diff_index__initialize(void) -{ - g_repo = cl_git_sandbox_init("status"); -} - -void test_diff_index__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_diff_index__0(void) -{ - /* grabbed a couple of commit oids from the history of the attr repo */ - const char *a_commit = "26a125ee1bf"; /* the current HEAD */ - const char *b_commit = "0017bd4ab1ec3"; /* the start */ - git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); - git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit); - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - diff_expects exp; - - cl_assert(a); - cl_assert(b); - - opts.context_lines = 1; - opts.interhunk_lines = 1; - - memset(&exp, 0, sizeof(exp)); - - cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts)); - - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - /* to generate these values: - * - cd to tests/resources/status, - * - mv .gitted .git - * - git diff --name-status --cached 26a125ee1bf - * - git diff -U1 --cached 26a125ee1bf - * - mv .git .gitted - */ - cl_assert_equal_i(8, exp.files); - cl_assert_equal_i(3, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]); - - cl_assert_equal_i(8, exp.hunks); - - cl_assert_equal_i(11, exp.lines); - cl_assert_equal_i(3, exp.line_ctxt); - cl_assert_equal_i(6, exp.line_adds); - cl_assert_equal_i(2, exp.line_dels); - - git_diff_list_free(diff); - diff = NULL; - memset(&exp, 0, sizeof(exp)); - - cl_git_pass(git_diff_tree_to_index(&diff, g_repo, b, NULL, &opts)); - - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - /* to generate these values: - * - cd to tests/resources/status, - * - mv .gitted .git - * - git diff --name-status --cached 0017bd4ab1ec3 - * - git diff -U1 --cached 0017bd4ab1ec3 - * - mv .git .gitted - */ - cl_assert_equal_i(12, exp.files); - cl_assert_equal_i(7, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]); - - cl_assert_equal_i(12, exp.hunks); - - cl_assert_equal_i(16, exp.lines); - cl_assert_equal_i(3, exp.line_ctxt); - cl_assert_equal_i(11, exp.line_adds); - cl_assert_equal_i(2, exp.line_dels); - - git_diff_list_free(diff); - diff = NULL; - - git_tree_free(a); - git_tree_free(b); -} - -static int diff_stop_after_2_files( - const git_diff_delta *delta, - float progress, - void *payload) -{ - diff_expects *e = payload; - - GIT_UNUSED(progress); - GIT_UNUSED(delta); - - e->files++; - - return (e->files == 2); -} - -void test_diff_index__1(void) -{ - /* grabbed a couple of commit oids from the history of the attr repo */ - const char *a_commit = "26a125ee1bf"; /* the current HEAD */ - const char *b_commit = "0017bd4ab1ec3"; /* the start */ - git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); - git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit); - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - diff_expects exp; - - cl_assert(a); - cl_assert(b); - - opts.context_lines = 1; - opts.interhunk_lines = 1; - - memset(&exp, 0, sizeof(exp)); - - cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts)); - - cl_assert_equal_i( - GIT_EUSER, - git_diff_foreach(diff, diff_stop_after_2_files, NULL, NULL, &exp) - ); - - cl_assert_equal_i(2, exp.files); - - git_diff_list_free(diff); - diff = NULL; - - git_tree_free(a); - git_tree_free(b); -} - -void test_diff_index__checks_options_version(void) -{ - const char *a_commit = "26a125ee1bf"; - git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - const git_error *err; - - opts.version = 0; - cl_git_fail(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); - cl_assert_equal_p(diff, NULL); - - giterr_clear(); - opts.version = 1024; - cl_git_fail(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); - cl_assert_equal_p(diff, NULL); - - git_tree_free(a); -} - diff --git a/tests-clar/diff/iterator.c b/tests-clar/diff/iterator.c deleted file mode 100644 index efdadbf1fbe..00000000000 --- a/tests-clar/diff/iterator.c +++ /dev/null @@ -1,923 +0,0 @@ -#include "clar_libgit2.h" -#include "diff_helpers.h" -#include "iterator.h" -#include "tree.h" - -void test_diff_iterator__initialize(void) -{ - /* since we are doing tests with different sandboxes, defer setup - * to the actual tests. cleanup will still be done in the global - * cleanup function so that assertion failures don't result in a - * missed cleanup. - */ -} - -void test_diff_iterator__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - - -/* -- TREE ITERATOR TESTS -- */ - -static void tree_iterator_test( - const char *sandbox, - const char *treeish, - const char *start, - const char *end, - int expected_count, - const char **expected_values) -{ - git_tree *t; - git_iterator *i; - const git_index_entry *entry; - int count = 0, count_post_reset = 0; - git_repository *repo = cl_git_sandbox_init(sandbox); - - cl_assert(t = resolve_commit_oid_to_tree(repo, treeish)); - cl_git_pass(git_iterator_for_tree_range( - &i, t, GIT_ITERATOR_DONT_IGNORE_CASE, start, end)); - - /* test loop */ - cl_git_pass(git_iterator_current(i, &entry)); - while (entry != NULL) { - if (expected_values != NULL) - cl_assert_equal_s(expected_values[count], entry->path); - count++; - cl_git_pass(git_iterator_advance(i, &entry)); - } - - /* test reset */ - cl_git_pass(git_iterator_reset(i, NULL, NULL)); - cl_git_pass(git_iterator_current(i, &entry)); - while (entry != NULL) { - if (expected_values != NULL) - cl_assert_equal_s(expected_values[count_post_reset], entry->path); - count_post_reset++; - cl_git_pass(git_iterator_advance(i, &entry)); - } - - git_iterator_free(i); - - cl_assert_equal_i(expected_count, count); - cl_assert_equal_i(count, count_post_reset); - - git_tree_free(t); -} - -/* results of: git ls-tree -r --name-only 605812a */ -const char *expected_tree_0[] = { - ".gitattributes", - "attr0", - "attr1", - "attr2", - "attr3", - "binfile", - "macro_test", - "root_test1", - "root_test2", - "root_test3", - "root_test4.txt", - "subdir/.gitattributes", - "subdir/abc", - "subdir/subdir_test1", - "subdir/subdir_test2.txt", - "subdir2/subdir2_test1", - NULL -}; - -void test_diff_iterator__tree_0(void) -{ - tree_iterator_test("attr", "605812a", NULL, NULL, 16, expected_tree_0); -} - -/* results of: git ls-tree -r --name-only 6bab5c79 */ -const char *expected_tree_1[] = { - ".gitattributes", - "attr0", - "attr1", - "attr2", - "attr3", - "root_test1", - "root_test2", - "root_test3", - "root_test4.txt", - "subdir/.gitattributes", - "subdir/subdir_test1", - "subdir/subdir_test2.txt", - "subdir2/subdir2_test1", - NULL -}; - -void test_diff_iterator__tree_1(void) -{ - tree_iterator_test("attr", "6bab5c79cd5", NULL, NULL, 13, expected_tree_1); -} - -/* results of: git ls-tree -r --name-only 26a125ee1 */ -const char *expected_tree_2[] = { - "current_file", - "file_deleted", - "modified_file", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_delete_file_deleted", - "staged_delete_modified_file", - "subdir.txt", - "subdir/current_file", - "subdir/deleted_file", - "subdir/modified_file", - NULL -}; - -void test_diff_iterator__tree_2(void) -{ - tree_iterator_test("status", "26a125ee1", NULL, NULL, 12, expected_tree_2); -} - -/* $ git ls-tree -r --name-only 0017bd4ab1e */ -const char *expected_tree_3[] = { - "current_file", - "file_deleted", - "modified_file", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_delete_file_deleted", - "staged_delete_modified_file" -}; - -void test_diff_iterator__tree_3(void) -{ - tree_iterator_test("status", "0017bd4ab1e", NULL, NULL, 8, expected_tree_3); -} - -/* $ git ls-tree -r --name-only 24fa9a9fc4e202313e24b648087495441dab432b */ -const char *expected_tree_4[] = { - "attr0", - "attr1", - "attr2", - "attr3", - "binfile", - "gitattributes", - "macro_bad", - "macro_test", - "root_test1", - "root_test2", - "root_test3", - "root_test4.txt", - "sub/abc", - "sub/file", - "sub/sub/file", - "sub/sub/subsub.txt", - "sub/subdir_test1", - "sub/subdir_test2.txt", - "subdir/.gitattributes", - "subdir/abc", - "subdir/subdir_test1", - "subdir/subdir_test2.txt", - "subdir2/subdir2_test1", - NULL -}; - -void test_diff_iterator__tree_4(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", NULL, NULL, - 23, expected_tree_4); -} - -void test_diff_iterator__tree_4_ranged(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", - "sub", "sub", - 11, &expected_tree_4[12]); -} - -const char *expected_tree_ranged_0[] = { - "gitattributes", - "macro_bad", - "macro_test", - "root_test1", - "root_test2", - "root_test3", - "root_test4.txt", - NULL -}; - -void test_diff_iterator__tree_ranged_0(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", - "git", "root", - 7, expected_tree_ranged_0); -} - -const char *expected_tree_ranged_1[] = { - "sub/subdir_test2.txt", - NULL -}; - -void test_diff_iterator__tree_ranged_1(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", - "sub/subdir_test2.txt", "sub/subdir_test2.txt", - 1, expected_tree_ranged_1); -} - -void test_diff_iterator__tree_range_empty_0(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", - "empty", "empty", 0, NULL); -} - -void test_diff_iterator__tree_range_empty_1(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", - "z_empty_after", NULL, 0, NULL); -} - -void test_diff_iterator__tree_range_empty_2(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", - NULL, ".aaa_empty_before", 0, NULL); -} - -static void check_tree_entry( - git_iterator *i, - const char *oid, - const char *oid_p, - const char *oid_pp, - const char *oid_ppp) -{ - const git_index_entry *ie; - const git_tree_entry *te; - const git_tree *tree; - git_buf path = GIT_BUF_INIT; - - cl_git_pass(git_iterator_current_tree_entry(i, &te)); - cl_assert(te); - cl_assert(git_oid_streq(&te->oid, oid) == 0); - - cl_git_pass(git_iterator_current(i, &ie)); - cl_git_pass(git_buf_sets(&path, ie->path)); - - if (oid_p) { - git_buf_rtruncate_at_char(&path, '/'); - cl_git_pass(git_iterator_current_parent_tree(i, path.ptr, &tree)); - cl_assert(tree); - cl_assert(git_oid_streq(git_tree_id(tree), oid_p) == 0); - } - - if (oid_pp) { - git_buf_rtruncate_at_char(&path, '/'); - cl_git_pass(git_iterator_current_parent_tree(i, path.ptr, &tree)); - cl_assert(tree); - cl_assert(git_oid_streq(git_tree_id(tree), oid_pp) == 0); - } - - if (oid_ppp) { - git_buf_rtruncate_at_char(&path, '/'); - cl_git_pass(git_iterator_current_parent_tree(i, path.ptr, &tree)); - cl_assert(tree); - cl_assert(git_oid_streq(git_tree_id(tree), oid_ppp) == 0); - } - - git_buf_free(&path); -} - -void test_diff_iterator__tree_special_functions(void) -{ - git_tree *t; - git_iterator *i; - const git_index_entry *entry; - git_repository *repo = cl_git_sandbox_init("attr"); - int cases = 0; - const char *rootoid = "ce39a97a7fb1fa90bcf5e711249c1e507476ae0e"; - - t = resolve_commit_oid_to_tree( - repo, "24fa9a9fc4e202313e24b648087495441dab432b"); - cl_assert(t != NULL); - - cl_git_pass(git_iterator_for_tree_range( - &i, t, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)); - cl_git_pass(git_iterator_current(i, &entry)); - - while (entry != NULL) { - if (strcmp(entry->path, "sub/file") == 0) { - cases++; - check_tree_entry( - i, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", - "ecb97df2a174987475ac816e3847fc8e9f6c596b", - rootoid, NULL); - } - else if (strcmp(entry->path, "sub/sub/subsub.txt") == 0) { - cases++; - check_tree_entry( - i, "9e5bdc47d6a80f2be0ea3049ad74231b94609242", - "4e49ba8c5b6c32ff28cd9dcb60be34df50fcc485", - "ecb97df2a174987475ac816e3847fc8e9f6c596b", rootoid); - } - else if (strcmp(entry->path, "subdir/.gitattributes") == 0) { - cases++; - check_tree_entry( - i, "99eae476896f4907224978b88e5ecaa6c5bb67a9", - "9fb40b6675dde60b5697afceae91b66d908c02d9", - rootoid, NULL); - } - else if (strcmp(entry->path, "subdir2/subdir2_test1") == 0) { - cases++; - check_tree_entry( - i, "dccada462d3df8ac6de596fb8c896aba9344f941", - "2929de282ce999e95183aedac6451d3384559c4b", - rootoid, NULL); - } - - cl_git_pass(git_iterator_advance(i, &entry)); - } - - cl_assert_equal_i(4, cases); - git_iterator_free(i); - git_tree_free(t); -} - -/* -- INDEX ITERATOR TESTS -- */ - -static void index_iterator_test( - const char *sandbox, - const char *start, - const char *end, - int expected_count, - const char **expected_names, - const char **expected_oids) -{ - git_index *index; - git_iterator *i; - const git_index_entry *entry; - int count = 0; - git_repository *repo = cl_git_sandbox_init(sandbox); - - cl_git_pass(git_repository_index(&index, repo)); - cl_git_pass(git_iterator_for_index_range(&i, index, 0, start, end)); - cl_git_pass(git_iterator_current(i, &entry)); - - while (entry != NULL) { - if (expected_names != NULL) - cl_assert_equal_s(expected_names[count], entry->path); - - if (expected_oids != NULL) { - git_oid oid; - cl_git_pass(git_oid_fromstr(&oid, expected_oids[count])); - cl_assert_equal_i(git_oid_cmp(&oid, &entry->oid), 0); - } - - count++; - cl_git_pass(git_iterator_advance(i, &entry)); - } - - git_iterator_free(i); - git_index_free(index); - - cl_assert_equal_i(expected_count, count); -} - -static const char *expected_index_0[] = { - "attr0", - "attr1", - "attr2", - "attr3", - "binfile", - "gitattributes", - "macro_bad", - "macro_test", - "root_test1", - "root_test2", - "root_test3", - "root_test4.txt", - "sub/abc", - "sub/file", - "sub/sub/file", - "sub/sub/subsub.txt", - "sub/subdir_test1", - "sub/subdir_test2.txt", - "subdir/.gitattributes", - "subdir/abc", - "subdir/subdir_test1", - "subdir/subdir_test2.txt", - "subdir2/subdir2_test1", -}; - -static const char *expected_index_oids_0[] = { - "556f8c827b8e4a02ad5cab77dca2bcb3e226b0b3", - "3b74db7ab381105dc0d28f8295a77f6a82989292", - "2c66e14f77196ea763fb1e41612c1aa2bc2d8ed2", - "c485abe35abd4aa6fd83b076a78bbea9e2e7e06c", - "d800886d9c86731ae5c4a62b0b77c437015e00d2", - "2b40c5aca159b04ea8d20ffe36cdf8b09369b14a", - "5819a185d77b03325aaf87cafc771db36f6ddca7", - "ff69f8639ce2e6010b3f33a74160aad98b48da2b", - "45141a79a77842c59a63229403220a4e4be74e3d", - "4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d", - "108bb4e7fd7b16490dc33ff7d972151e73d7166e", - "a0f7217ae99f5ac3e88534f5cea267febc5fa85b", - "3e42ffc54a663f9401cc25843d6c0e71a33e4249", - "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", - "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", - "9e5bdc47d6a80f2be0ea3049ad74231b94609242", - "e563cf4758f0d646f1b14b76016aa17fa9e549a4", - "fb5067b1aef3ac1ada4b379dbcb7d17255df7d78", - "99eae476896f4907224978b88e5ecaa6c5bb67a9", - "3e42ffc54a663f9401cc25843d6c0e71a33e4249", - "e563cf4758f0d646f1b14b76016aa17fa9e549a4", - "fb5067b1aef3ac1ada4b379dbcb7d17255df7d78", - "dccada462d3df8ac6de596fb8c896aba9344f941" -}; - -void test_diff_iterator__index_0(void) -{ - index_iterator_test( - "attr", NULL, NULL, 23, expected_index_0, expected_index_oids_0); -} - -static const char *expected_index_range[] = { - "root_test1", - "root_test2", - "root_test3", - "root_test4.txt", -}; - -static const char *expected_index_oids_range[] = { - "45141a79a77842c59a63229403220a4e4be74e3d", - "4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d", - "108bb4e7fd7b16490dc33ff7d972151e73d7166e", - "a0f7217ae99f5ac3e88534f5cea267febc5fa85b", -}; - -void test_diff_iterator__index_range(void) -{ - index_iterator_test( - "attr", "root", "root", 4, expected_index_range, expected_index_oids_range); -} - -void test_diff_iterator__index_range_empty_0(void) -{ - index_iterator_test( - "attr", "empty", "empty", 0, NULL, NULL); -} - -void test_diff_iterator__index_range_empty_1(void) -{ - index_iterator_test( - "attr", "z_empty_after", NULL, 0, NULL, NULL); -} - -void test_diff_iterator__index_range_empty_2(void) -{ - index_iterator_test( - "attr", NULL, ".aaa_empty_before", 0, NULL, NULL); -} - -static const char *expected_index_1[] = { - "current_file", - "file_deleted", - "modified_file", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_new_file", - "staged_new_file_deleted_file", - "staged_new_file_modified_file", - "subdir.txt", - "subdir/current_file", - "subdir/deleted_file", - "subdir/modified_file", -}; - -static const char* expected_index_oids_1[] = { - "a0de7e0ac200c489c41c59dfa910154a70264e6e", - "5452d32f1dd538eb0405e8a83cc185f79e25e80f", - "452e4244b5d083ddf0460acf1ecc74db9dcfa11a", - "55d316c9ba708999f1918e9677d01dfcae69c6b9", - "a6be623522ce87a1d862128ac42672604f7b468b", - "906ee7711f4f4928ddcb2a5f8fbc500deba0d2a8", - "529a16e8e762d4acb7b9636ff540a00831f9155a", - "90b8c29d8ba39434d1c63e1b093daaa26e5bd972", - "ed062903b8f6f3dccb2fa81117ba6590944ef9bd", - "e8ee89e15bbe9b20137715232387b3de5b28972e", - "53ace0d1cc1145a5f4fe4f78a186a60263190733", - "1888c805345ba265b0ee9449b8877b6064592058", - "a6191982709b746d5650e93c2acf34ef74e11504" -}; - -void test_diff_iterator__index_1(void) -{ - index_iterator_test( - "status", NULL, NULL, 13, expected_index_1, expected_index_oids_1); -} - - -/* -- WORKDIR ITERATOR TESTS -- */ - -static void workdir_iterator_test( - const char *sandbox, - const char *start, - const char *end, - int expected_count, - int expected_ignores, - const char **expected_names, - const char *an_ignored_name) -{ - git_iterator *i; - const git_index_entry *entry; - int count = 0, count_all = 0, count_all_post_reset = 0; - git_repository *repo = cl_git_sandbox_init(sandbox); - - cl_git_pass(git_iterator_for_workdir_range(&i, repo, 0, start, end)); - cl_git_pass(git_iterator_current(i, &entry)); - - while (entry != NULL) { - int ignored = git_iterator_current_is_ignored(i); - - if (S_ISDIR(entry->mode)) { - cl_git_pass(git_iterator_advance_into_directory(i, &entry)); - continue; - } - - if (expected_names != NULL) - cl_assert_equal_s(expected_names[count_all], entry->path); - - if (an_ignored_name && strcmp(an_ignored_name,entry->path)==0) - cl_assert(ignored); - - if (!ignored) - count++; - count_all++; - - cl_git_pass(git_iterator_advance(i, &entry)); - } - - cl_git_pass(git_iterator_reset(i, NULL, NULL)); - cl_git_pass(git_iterator_current(i, &entry)); - - while (entry != NULL) { - if (S_ISDIR(entry->mode)) { - cl_git_pass(git_iterator_advance_into_directory(i, &entry)); - continue; - } - if (expected_names != NULL) - cl_assert_equal_s( - expected_names[count_all_post_reset], entry->path); - count_all_post_reset++; - cl_git_pass(git_iterator_advance(i, &entry)); - } - - git_iterator_free(i); - - cl_assert_equal_i(expected_count, count); - cl_assert_equal_i(expected_count + expected_ignores, count_all); - cl_assert_equal_i(count_all, count_all_post_reset); -} - -void test_diff_iterator__workdir_0(void) -{ - workdir_iterator_test("attr", NULL, NULL, 27, 1, NULL, "ign"); -} - -static const char *status_paths[] = { - "current_file", - "ignored_file", - "modified_file", - "new_file", - "staged_changes", - "staged_changes_modified_file", - "staged_delete_modified_file", - "staged_new_file", - "staged_new_file_modified_file", - "subdir.txt", - "subdir/current_file", - "subdir/modified_file", - "subdir/new_file", - "\xe8\xbf\x99", - NULL -}; - -void test_diff_iterator__workdir_1(void) -{ - workdir_iterator_test( - "status", NULL, NULL, 13, 1, status_paths, "ignored_file"); -} - -static const char *status_paths_range_0[] = { - "staged_changes", - "staged_changes_modified_file", - "staged_delete_modified_file", - "staged_new_file", - "staged_new_file_modified_file", - NULL -}; - -void test_diff_iterator__workdir_1_ranged_0(void) -{ - workdir_iterator_test( - "status", "staged", "staged", 5, 0, status_paths_range_0, NULL); -} - -static const char *status_paths_range_1[] = { - "modified_file", NULL -}; - -void test_diff_iterator__workdir_1_ranged_1(void) -{ - workdir_iterator_test( - "status", "modified_file", "modified_file", - 1, 0, status_paths_range_1, NULL); -} - -static const char *status_paths_range_3[] = { - "subdir.txt", - "subdir/current_file", - "subdir/modified_file", - NULL -}; - -void test_diff_iterator__workdir_1_ranged_3(void) -{ - workdir_iterator_test( - "status", "subdir", "subdir/modified_file", - 3, 0, status_paths_range_3, NULL); -} - -static const char *status_paths_range_4[] = { - "subdir/current_file", - "subdir/modified_file", - "subdir/new_file", - "\xe8\xbf\x99", - NULL -}; - -void test_diff_iterator__workdir_1_ranged_4(void) -{ - workdir_iterator_test( - "status", "subdir/", NULL, 4, 0, status_paths_range_4, NULL); -} - -static const char *status_paths_range_5[] = { - "subdir/modified_file", - NULL -}; - -void test_diff_iterator__workdir_1_ranged_5(void) -{ - workdir_iterator_test( - "status", "subdir/modified_file", "subdir/modified_file", - 1, 0, status_paths_range_5, NULL); -} - -void test_diff_iterator__workdir_1_ranged_empty_0(void) -{ - workdir_iterator_test( - "status", "\xff_does_not_exist", NULL, - 0, 0, NULL, NULL); -} - -void test_diff_iterator__workdir_1_ranged_empty_1(void) -{ - workdir_iterator_test( - "status", "empty", "empty", - 0, 0, NULL, NULL); -} - -void test_diff_iterator__workdir_1_ranged_empty_2(void) -{ - workdir_iterator_test( - "status", NULL, "aaaa_empty_before", - 0, 0, NULL, NULL); -} - -void test_diff_iterator__workdir_builtin_ignores(void) -{ - git_repository *repo = cl_git_sandbox_init("attr"); - git_iterator *i; - const git_index_entry *entry; - int idx; - static struct { - const char *path; - bool ignored; - } expected[] = { - { "dir/", true }, - { "file", false }, - { "ign", true }, - { "macro_bad", false }, - { "macro_test", false }, - { "root_test1", false }, - { "root_test2", false }, - { "root_test3", false }, - { "root_test4.txt", false }, - { "sub/", false }, - { "sub/.gitattributes", false }, - { "sub/abc", false }, - { "sub/dir/", true }, - { "sub/file", false }, - { "sub/ign/", true }, - { "sub/sub/", false }, - { "sub/sub/.gitattributes", false }, - { "sub/sub/dir", false }, /* file is not actually a dir */ - { "sub/sub/file", false }, - { NULL, false } - }; - - cl_git_pass(p_mkdir("attr/sub/sub/.git", 0777)); - cl_git_mkfile("attr/sub/.git", "whatever"); - - cl_git_pass( - git_iterator_for_workdir_range(&i, repo, 0, "dir", "sub/sub/file")); - cl_git_pass(git_iterator_current(i, &entry)); - - for (idx = 0; entry != NULL; ++idx) { - int ignored = git_iterator_current_is_ignored(i); - - cl_assert_equal_s(expected[idx].path, entry->path); - cl_assert_(ignored == expected[idx].ignored, expected[idx].path); - - if (!ignored && S_ISDIR(entry->mode)) - cl_git_pass(git_iterator_advance_into_directory(i, &entry)); - else - cl_git_pass(git_iterator_advance(i, &entry)); - } - - cl_assert(expected[idx].path == NULL); - - git_iterator_free(i); -} - -static void check_wd_first_through_third_range( - git_repository *repo, const char *start, const char *end) -{ - git_iterator *i; - const git_index_entry *entry; - int idx; - static const char *expected[] = { "FIRST", "second", "THIRD", NULL }; - - cl_git_pass(git_iterator_for_workdir_range( - &i, repo, GIT_ITERATOR_IGNORE_CASE, start, end)); - cl_git_pass(git_iterator_current(i, &entry)); - - for (idx = 0; entry != NULL; ++idx) { - cl_assert_equal_s(expected[idx], entry->path); - - if (S_ISDIR(entry->mode)) - cl_git_pass(git_iterator_advance_into_directory(i, &entry)); - else - cl_git_pass(git_iterator_advance(i, &entry)); - } - - cl_assert(expected[idx] == NULL); - - git_iterator_free(i); -} - -void test_diff_iterator__workdir_handles_icase_range(void) -{ - git_repository *repo; - - repo = cl_git_sandbox_init("empty_standard_repo"); - cl_git_remove_placeholders(git_repository_path(repo), "dummy-marker.txt"); - - cl_git_mkfile("empty_standard_repo/before", "whatever\n"); - cl_git_mkfile("empty_standard_repo/FIRST", "whatever\n"); - cl_git_mkfile("empty_standard_repo/second", "whatever\n"); - cl_git_mkfile("empty_standard_repo/THIRD", "whatever\n"); - cl_git_mkfile("empty_standard_repo/zafter", "whatever\n"); - cl_git_mkfile("empty_standard_repo/Zlast", "whatever\n"); - - check_wd_first_through_third_range(repo, "first", "third"); - check_wd_first_through_third_range(repo, "FIRST", "THIRD"); - check_wd_first_through_third_range(repo, "first", "THIRD"); - check_wd_first_through_third_range(repo, "FIRST", "third"); - check_wd_first_through_third_range(repo, "FirSt", "tHiRd"); -} - -static void check_tree_range( - git_repository *repo, - const char *start, - const char *end, - bool ignore_case, - int expected_count) -{ - git_tree *head; - git_iterator *i; - const git_index_entry *entry; - int count; - - cl_git_pass(git_repository_head_tree(&head, repo)); - - cl_git_pass(git_iterator_for_tree_range( - &i, head, - ignore_case ? GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE, - start, end)); - - cl_git_pass(git_iterator_current(i, &entry)); - - for (count = 0; entry != NULL; ) { - ++count; - cl_git_pass(git_iterator_advance(i, &entry)); - } - - cl_assert_equal_i(expected_count, count); - - git_iterator_free(i); - git_tree_free(head); -} - -void test_diff_iterator__tree_handles_icase_range(void) -{ - git_repository *repo; - - repo = cl_git_sandbox_init("testrepo"); - - check_tree_range(repo, "B", "C", false, 0); - check_tree_range(repo, "B", "C", true, 1); - check_tree_range(repo, "b", "c", false, 1); - check_tree_range(repo, "b", "c", true, 1); - - check_tree_range(repo, "a", "z", false, 3); - check_tree_range(repo, "a", "z", true, 4); - check_tree_range(repo, "A", "Z", false, 1); - check_tree_range(repo, "A", "Z", true, 4); - check_tree_range(repo, "a", "Z", false, 0); - check_tree_range(repo, "a", "Z", true, 4); - check_tree_range(repo, "A", "z", false, 4); - check_tree_range(repo, "A", "z", true, 4); - - check_tree_range(repo, "new.txt", "new.txt", true, 1); - check_tree_range(repo, "new.txt", "new.txt", false, 1); - check_tree_range(repo, "README", "README", true, 1); - check_tree_range(repo, "README", "README", false, 1); -} - -static void check_index_range( - git_repository *repo, - const char *start, - const char *end, - bool ignore_case, - int expected_count) -{ - git_index *index; - git_iterator *i; - const git_index_entry *entry; - int count, caps; - bool is_ignoring_case; - - cl_git_pass(git_repository_index(&index, repo)); - - caps = git_index_caps(index); - is_ignoring_case = ((caps & GIT_INDEXCAP_IGNORE_CASE) != 0); - - if (ignore_case != is_ignoring_case) - cl_git_pass(git_index_set_caps(index, caps ^ GIT_INDEXCAP_IGNORE_CASE)); - - cl_git_pass(git_iterator_for_index_range(&i, index, 0, start, end)); - - cl_assert(git_iterator_ignore_case(i) == ignore_case); - - cl_git_pass(git_iterator_current(i, &entry)); - - for (count = 0; entry != NULL; ) { - ++count; - cl_git_pass(git_iterator_advance(i, &entry)); - } - - cl_assert_equal_i(expected_count, count); - - git_iterator_free(i); - git_index_free(index); -} - -void test_diff_iterator__index_handles_icase_range(void) -{ - git_repository *repo; - git_index *index; - git_tree *head; - - repo = cl_git_sandbox_init("testrepo"); - - /* reset index to match HEAD */ - cl_git_pass(git_repository_head_tree(&head, repo)); - cl_git_pass(git_repository_index(&index, repo)); - cl_git_pass(git_index_read_tree(index, head)); - cl_git_pass(git_index_write(index)); - git_tree_free(head); - git_index_free(index); - - /* do some ranged iterator checks toggling case sensitivity */ - check_index_range(repo, "B", "C", false, 0); - check_index_range(repo, "B", "C", true, 1); - check_index_range(repo, "a", "z", false, 3); - check_index_range(repo, "a", "z", true, 4); -} diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c deleted file mode 100644 index affa761de08..00000000000 --- a/tests-clar/diff/patch.c +++ /dev/null @@ -1,304 +0,0 @@ -#include "clar_libgit2.h" -#include "diff_helpers.h" -#include "repository.h" -#include "buf_text.h" - -static git_repository *g_repo = NULL; - -void test_diff_patch__initialize(void) -{ -} - -void test_diff_patch__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -#define EXPECTED_HEADER "diff --git a/subdir.txt b/subdir.txt\n" \ - "deleted file mode 100644\n" \ - "index e8ee89e..0000000\n" \ - "--- a/subdir.txt\n" \ - "+++ /dev/null\n" - -#define EXPECTED_HUNK "@@ -1,2 +0,0 @@\n" - -static int check_removal_cb( - const git_diff_delta *delta, - const git_diff_range *range, - char line_origin, - const char *formatted_output, - size_t output_len, - void *payload) -{ - GIT_UNUSED(payload); - GIT_UNUSED(output_len); - - switch (line_origin) { - case GIT_DIFF_LINE_FILE_HDR: - cl_assert_equal_s(EXPECTED_HEADER, formatted_output); - cl_assert(range == NULL); - goto check_delta; - - case GIT_DIFF_LINE_HUNK_HDR: - cl_assert_equal_s(EXPECTED_HUNK, formatted_output); - /* Fall through */ - - case GIT_DIFF_LINE_CONTEXT: - case GIT_DIFF_LINE_DELETION: - goto check_range; - - default: - /* unexpected code path */ - return -1; - } - -check_range: - cl_assert(range != NULL); - cl_assert_equal_i(1, range->old_start); - cl_assert_equal_i(2, range->old_lines); - cl_assert_equal_i(0, range->new_start); - cl_assert_equal_i(0, range->new_lines); - -check_delta: - cl_assert_equal_s("subdir.txt", delta->old_file.path); - cl_assert_equal_s("subdir.txt", delta->new_file.path); - cl_assert_equal_i(GIT_DELTA_DELETED, delta->status); - - return 0; -} - -void test_diff_patch__can_properly_display_the_removal_of_a_file(void) -{ - /* - * $ git diff 26a125e..735b6a2 - * diff --git a/subdir.txt b/subdir.txt - * deleted file mode 100644 - * index e8ee89e..0000000 - * --- a/subdir.txt - * +++ /dev/null - * @@ -1,2 +0,0 @@ - * -Is it a bird? - * -Is it a plane? - */ - - const char *one_sha = "26a125e"; - const char *another_sha = "735b6a2"; - git_tree *one, *another; - git_diff_list *diff; - - g_repo = cl_git_sandbox_init("status"); - - one = resolve_commit_oid_to_tree(g_repo, one_sha); - another = resolve_commit_oid_to_tree(g_repo, another_sha); - - cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, one, another, NULL)); - - cl_git_pass(git_diff_print_patch(diff, check_removal_cb, NULL)); - - git_diff_list_free(diff); - - git_tree_free(another); - git_tree_free(one); -} - -void test_diff_patch__to_string(void) -{ - const char *one_sha = "26a125e"; - const char *another_sha = "735b6a2"; - git_tree *one, *another; - git_diff_list *diff; - git_diff_patch *patch; - char *text; - const char *expected = "diff --git a/subdir.txt b/subdir.txt\ndeleted file mode 100644\nindex e8ee89e..0000000\n--- a/subdir.txt\n+++ /dev/null\n@@ -1,2 +0,0 @@\n-Is it a bird?\n-Is it a plane?\n"; - - g_repo = cl_git_sandbox_init("status"); - - one = resolve_commit_oid_to_tree(g_repo, one_sha); - another = resolve_commit_oid_to_tree(g_repo, another_sha); - - cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, one, another, NULL)); - - cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); - - cl_git_pass(git_diff_patch_to_str(&text, patch)); - - cl_assert_equal_s(expected, text); - - git__free(text); - git_diff_patch_free(patch); - git_diff_list_free(diff); - git_tree_free(another); - git_tree_free(one); -} - -void test_diff_patch__hunks_have_correct_line_numbers(void) -{ - git_tree *head; - git_diff_list *diff; - git_diff_patch *patch; - const git_diff_delta *delta; - const git_diff_range *range; - const char *hdr, *text; - size_t hdrlen, hunklen, textlen; - char origin; - int oldno, newno; - const char *new_content = "The Song of Seven Cities\n========================\n\nI WAS Lord of Cities very sumptuously builded.\nSeven roaring Cities paid me tribute from afar.\nIvory their outposts were—the guardrooms of them gilded,\nAnd garrisoned with Amazons invincible in war.\n\nThis is some new text;\nNot as good as the old text;\nBut here it is.\n\nSo they warred and trafficked only yesterday, my Cities.\nTo-day there is no mark or mound of where my Cities stood.\nFor the River rose at midnight and it washed away my Cities.\nThey are evened with Atlantis and the towns before the Flood.\n\nRain on rain-gorged channels raised the water-levels round them,\nFreshet backed on freshet swelled and swept their world from sight,\nTill the emboldened floods linked arms and, flashing forward, drowned them—\nDrowned my Seven Cities and their peoples in one night!\n\nLow among the alders lie their derelict foundations,\nThe beams wherein they trusted and the plinths whereon they built—\nMy rulers and their treasure and their unborn populations,\nDead, destroyed, aborted, and defiled with mud and silt!\n\nAnother replacement;\nBreaking up the poem;\nGenerating some hunks.\n\nTo the sound of trumpets shall their seed restore my Cities\nWealthy and well-weaponed, that once more may I behold\nAll the world go softly when it walks before my Cities,\nAnd the horses and the chariots fleeing from them as of old!\n\n -- Rudyard Kipling\n"; - - g_repo = cl_git_sandbox_init("renames"); - - cl_git_rewritefile("renames/songofseven.txt", new_content); - - cl_git_pass(git_repository_head_tree(&head, g_repo)); - - cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, head, NULL)); - - cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, 0)); - - cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); - cl_assert_equal_i(2, (int)git_diff_patch_num_hunks(patch)); - - /* check hunk 0 */ - - cl_git_pass( - git_diff_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 0)); - - cl_assert_equal_i(18, (int)hunklen); - - cl_assert_equal_i(6, (int)range->old_start); - cl_assert_equal_i(15, (int)range->old_lines); - cl_assert_equal_i(6, (int)range->new_start); - cl_assert_equal_i(9, (int)range->new_lines); - - cl_assert_equal_i(18, (int)git_diff_patch_num_lines_in_hunk(patch, 0)); - - cl_git_pass(git_diff_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 0, 0)); - cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin); - cl_assert(strncmp("Ivory their outposts were—the guardrooms of them gilded,\n", text, textlen) == 0); - cl_assert_equal_i(6, oldno); - cl_assert_equal_i(6, newno); - - cl_git_pass(git_diff_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 0, 3)); - cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin); - cl_assert(strncmp("All the world went softly when it walked before my Cities—\n", text, textlen) == 0); - cl_assert_equal_i(9, oldno); - cl_assert_equal_i(-1, newno); - - cl_git_pass(git_diff_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 0, 12)); - cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin); - cl_assert(strncmp("This is some new text;\n", text, textlen) == 0); - cl_assert_equal_i(-1, oldno); - cl_assert_equal_i(9, newno); - - /* check hunk 1 */ - - cl_git_pass( - git_diff_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 1)); - - cl_assert_equal_i(18, (int)hunklen); - - cl_assert_equal_i(31, (int)range->old_start); - cl_assert_equal_i(15, (int)range->old_lines); - cl_assert_equal_i(25, (int)range->new_start); - cl_assert_equal_i(9, (int)range->new_lines); - - cl_assert_equal_i(18, (int)git_diff_patch_num_lines_in_hunk(patch, 1)); - - cl_git_pass(git_diff_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 1, 0)); - cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin); - cl_assert(strncmp("My rulers and their treasure and their unborn populations,\n", text, textlen) == 0); - cl_assert_equal_i(31, oldno); - cl_assert_equal_i(25, newno); - - cl_git_pass(git_diff_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 1, 3)); - cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin); - cl_assert(strncmp("The Daughters of the Palace whom they cherished in my Cities,\n", text, textlen) == 0); - cl_assert_equal_i(34, oldno); - cl_assert_equal_i(-1, newno); - - cl_git_pass(git_diff_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 1, 12)); - cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin); - cl_assert(strncmp("Another replacement;\n", text, textlen) == 0); - cl_assert_equal_i(-1, oldno); - cl_assert_equal_i(28, newno); - - git_diff_patch_free(patch); - git_diff_list_free(diff); - git_tree_free(head); -} - -static void check_single_patch_stats( - git_repository *repo, size_t hunks, size_t adds, size_t dels) -{ - git_diff_list *diff; - git_diff_patch *patch; - const git_diff_delta *delta; - size_t actual_adds, actual_dels; - - cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL)); - - cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, 0)); - cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); - - cl_assert_equal_i(hunks, (int)git_diff_patch_num_hunks(patch)); - - cl_git_pass( - git_diff_patch_line_stats(NULL, &actual_adds, &actual_dels, patch)); - - cl_assert_equal_i(adds, actual_adds); - cl_assert_equal_i(dels, actual_dels); - - git_diff_patch_free(patch); - git_diff_list_free(diff); -} - -void test_diff_patch__line_counts_with_eofnl(void) -{ - git_buf content = GIT_BUF_INIT; - const char *end; - git_index *index; - - g_repo = cl_git_sandbox_init("renames"); - - cl_git_pass(git_futils_readbuffer(&content, "renames/songofseven.txt")); - - /* remove first line */ - - end = git_buf_cstr(&content) + git_buf_find(&content, '\n') + 1; - git_buf_consume(&content, end); - cl_git_rewritefile("renames/songofseven.txt", content.ptr); - - check_single_patch_stats(g_repo, 1, 0, 1); - - /* remove trailing whitespace */ - - git_buf_rtrim(&content); - cl_git_rewritefile("renames/songofseven.txt", content.ptr); - - check_single_patch_stats(g_repo, 2, 1, 2); - - /* add trailing whitespace */ - - cl_git_pass(git_repository_index(&index, g_repo)); - cl_git_pass(git_index_add_bypath(index, "songofseven.txt")); - cl_git_pass(git_index_write(index)); - git_index_free(index); - - cl_git_pass(git_buf_putc(&content, '\n')); - cl_git_rewritefile("renames/songofseven.txt", content.ptr); - - check_single_patch_stats(g_repo, 1, 1, 1); - - git_buf_free(&content); -} diff --git a/tests-clar/diff/rename.c b/tests-clar/diff/rename.c deleted file mode 100644 index 2995b4ef552..00000000000 --- a/tests-clar/diff/rename.c +++ /dev/null @@ -1,135 +0,0 @@ -#include "clar_libgit2.h" -#include "diff_helpers.h" - -static git_repository *g_repo = NULL; - -void test_diff_rename__initialize(void) -{ - g_repo = cl_git_sandbox_init("renames"); -} - -void test_diff_rename__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -/* - * Renames repo has: - * - * commit 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 - - * serving.txt (25 lines) - * sevencities.txt (50 lines) - * commit 2bc7f351d20b53f1c72c16c4b036e491c478c49a - - * serving.txt -> sixserving.txt (rename, no change, 100% match) - * sevencities.txt -> sevencities.txt (no change) - * sevencities.txt -> songofseven.txt (copy, no change, 100% match) - * - * TODO: add commits with various % changes of copy / rename - */ - -void test_diff_rename__match_oid(void) -{ - const char *old_sha = "31e47d8c1fa36d7f8d537b96158e3f024de0a9f2"; - const char *new_sha = "2bc7f351d20b53f1c72c16c4b036e491c478c49a"; - git_tree *old_tree, *new_tree; - git_diff_list *diff; - git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; - git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; - diff_expects exp; - - old_tree = resolve_commit_oid_to_tree(g_repo, old_sha); - new_tree = resolve_commit_oid_to_tree(g_repo, new_sha); - - /* Must pass GIT_DIFF_INCLUDE_UNMODIFIED if you expect to emulate - * --find-copies-harder during rename transformion... - */ - diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; - - cl_git_pass(git_diff_tree_to_tree( - &diff, g_repo, old_tree, new_tree, &diffopts)); - - /* git diff --no-renames \ - * 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \ - * 2bc7f351d20b53f1c72c16c4b036e491c478c49a - */ - memset(&exp, 0, sizeof(exp)); - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(4, exp.files); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); - cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); - - /* git diff 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \ - * 2bc7f351d20b53f1c72c16c4b036e491c478c49a - */ - cl_git_pass(git_diff_find_similar(diff, NULL)); - - memset(&exp, 0, sizeof(exp)); - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(3, exp.files); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); - - git_diff_list_free(diff); - - cl_git_pass(git_diff_tree_to_tree( - &diff, g_repo, old_tree, new_tree, &diffopts)); - - /* git diff --find-copies-harder \ - * 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \ - * 2bc7f351d20b53f1c72c16c4b036e491c478c49a - */ - opts.flags = GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED; - cl_git_pass(git_diff_find_similar(diff, &opts)); - - memset(&exp, 0, sizeof(exp)); - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(3, exp.files); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); - - git_diff_list_free(diff); - - git_tree_free(old_tree); - git_tree_free(new_tree); -} - -void test_diff_rename__checks_options_version(void) -{ - const char *old_sha = "31e47d8c1fa36d7f8d537b96158e3f024de0a9f2"; - const char *new_sha = "2bc7f351d20b53f1c72c16c4b036e491c478c49a"; - git_tree *old_tree, *new_tree; - git_diff_list *diff; - git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; - git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; - const git_error *err; - - old_tree = resolve_commit_oid_to_tree(g_repo, old_sha); - new_tree = resolve_commit_oid_to_tree(g_repo, new_sha); - diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; - cl_git_pass(git_diff_tree_to_tree( - &diff, g_repo, old_tree, new_tree, &diffopts)); - - opts.version = 0; - cl_git_fail(git_diff_find_similar(diff, &opts)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); - - giterr_clear(); - opts.version = 1024; - cl_git_fail(git_diff_find_similar(diff, &opts)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); - - git_diff_list_free(diff); - git_tree_free(old_tree); - git_tree_free(new_tree); -} diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c deleted file mode 100644 index 442e53b25f6..00000000000 --- a/tests-clar/diff/tree.c +++ /dev/null @@ -1,349 +0,0 @@ -#include "clar_libgit2.h" -#include "diff_helpers.h" - -static git_repository *g_repo = NULL; - -void test_diff_tree__initialize(void) -{ -} - -void test_diff_tree__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_diff_tree__0(void) -{ - /* grabbed a couple of commit oids from the history of the attr repo */ - const char *a_commit = "605812a"; - const char *b_commit = "370fe9ec22"; - const char *c_commit = "f5b0af1fb4f5c"; - git_tree *a, *b, *c; - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - diff_expects exp; - - g_repo = cl_git_sandbox_init("attr"); - - cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL); - cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL); - cl_assert((c = resolve_commit_oid_to_tree(g_repo, c_commit)) != NULL); - - opts.context_lines = 1; - opts.interhunk_lines = 1; - - memset(&exp, 0, sizeof(exp)); - - cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); - - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(5, exp.files); - cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); - - cl_assert_equal_i(5, exp.hunks); - - cl_assert_equal_i(7 + 24 + 1 + 6 + 6, exp.lines); - cl_assert_equal_i(1, exp.line_ctxt); - cl_assert_equal_i(24 + 1 + 5 + 5, exp.line_adds); - cl_assert_equal_i(7 + 1, exp.line_dels); - - git_diff_list_free(diff); - diff = NULL; - - memset(&exp, 0, sizeof(exp)); - - cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, c, b, &opts)); - - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(2, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); - - cl_assert_equal_i(2, exp.hunks); - - cl_assert_equal_i(8 + 15, exp.lines); - cl_assert_equal_i(1, exp.line_ctxt); - cl_assert_equal_i(1, exp.line_adds); - cl_assert_equal_i(7 + 14, exp.line_dels); - - git_diff_list_free(diff); - - git_tree_free(a); - git_tree_free(b); - git_tree_free(c); -} - -void test_diff_tree__options(void) -{ - /* grabbed a couple of commit oids from the history of the attr repo */ - const char *a_commit = "6bab5c79cd5140d0"; - const char *b_commit = "605812ab7fe421fdd"; - const char *c_commit = "f5b0af1fb4f5"; - const char *d_commit = "a97cc019851"; - git_tree *a, *b, *c, *d; - git_diff_options opts = {0}; - git_diff_list *diff = NULL; - diff_expects actual; - int test_ab_or_cd[] = { 0, 0, 0, 0, 1, 1, 1, 1, 1 }; - git_diff_options test_options[] = { - /* a vs b tests */ - { 1, GIT_DIFF_NORMAL, 1, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_NORMAL, 3, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_REVERSE, 2, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_FORCE_TEXT, 2, 1, NULL, NULL, {0} }, - /* c vs d tests */ - { 1, GIT_DIFF_NORMAL, 3, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_IGNORE_WHITESPACE, 3, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_IGNORE_WHITESPACE_CHANGE, 3, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_IGNORE_WHITESPACE_EOL, 3, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_IGNORE_WHITESPACE | GIT_DIFF_REVERSE, 1, 1, NULL, NULL, {0} }, - }; - - /* to generate these values: - * - cd to tests/resources/attr, - * - mv .gitted .git - * - git diff [options] 6bab5c79cd5140d0 605812ab7fe421fdd - * - mv .git .gitted - */ -#define EXPECT_STATUS_ADM(ADDS,DELS,MODS) { 0, ADDS, DELS, MODS, 0, 0, 0, 0, 0 } - - diff_expects test_expects[] = { - /* a vs b tests */ - { 5, 0, EXPECT_STATUS_ADM(3, 0, 2), 4, 0, 0, 51, 2, 46, 3 }, - { 5, 0, EXPECT_STATUS_ADM(3, 0, 2), 4, 0, 0, 53, 4, 46, 3 }, - { 5, 0, EXPECT_STATUS_ADM(0, 3, 2), 4, 0, 0, 52, 3, 3, 46 }, - { 5, 0, EXPECT_STATUS_ADM(3, 0, 2), 5, 0, 0, 54, 3, 47, 4 }, - /* c vs d tests */ - { 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 22, 9, 10, 3 }, - { 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 19, 12, 7, 0 }, - { 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 20, 11, 8, 1 }, - { 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 20, 11, 8, 1 }, - { 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 18, 11, 0, 7 }, - { 0 }, - }; - diff_expects *expected; - int i, j; - - g_repo = cl_git_sandbox_init("attr"); - - cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL); - cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL); - cl_assert((c = resolve_commit_oid_to_tree(g_repo, c_commit)) != NULL); - cl_assert((d = resolve_commit_oid_to_tree(g_repo, d_commit)) != NULL); - - for (i = 0; test_expects[i].files > 0; i++) { - memset(&actual, 0, sizeof(actual)); /* clear accumulator */ - opts = test_options[i]; - - if (test_ab_or_cd[i] == 0) - cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); - else - cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, c, d, &opts)); - - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &actual)); - - expected = &test_expects[i]; - cl_assert_equal_i(actual.files, expected->files); - for (j = GIT_DELTA_UNMODIFIED; j <= GIT_DELTA_TYPECHANGE; ++j) - cl_assert_equal_i(expected->file_status[j], actual.file_status[j]); - cl_assert_equal_i(actual.hunks, expected->hunks); - cl_assert_equal_i(actual.lines, expected->lines); - cl_assert_equal_i(actual.line_ctxt, expected->line_ctxt); - cl_assert_equal_i(actual.line_adds, expected->line_adds); - cl_assert_equal_i(actual.line_dels, expected->line_dels); - - git_diff_list_free(diff); - diff = NULL; - } - - git_tree_free(a); - git_tree_free(b); - git_tree_free(c); - git_tree_free(d); -} - -void test_diff_tree__bare(void) -{ - const char *a_commit = "8496071c1b46c85"; - const char *b_commit = "be3563ae3f79"; - git_tree *a, *b; - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - diff_expects exp; - - g_repo = cl_git_sandbox_init("testrepo.git"); - - cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL); - cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL); - - opts.context_lines = 1; - opts.interhunk_lines = 1; - - memset(&exp, 0, sizeof(exp)); - - cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); - - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(3, exp.files); - cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); - - cl_assert_equal_i(3, exp.hunks); - - cl_assert_equal_i(4, exp.lines); - cl_assert_equal_i(0, exp.line_ctxt); - cl_assert_equal_i(3, exp.line_adds); - cl_assert_equal_i(1, exp.line_dels); - - git_diff_list_free(diff); - git_tree_free(a); - git_tree_free(b); -} - -void test_diff_tree__merge(void) -{ - /* grabbed a couple of commit oids from the history of the attr repo */ - const char *a_commit = "605812a"; - const char *b_commit = "370fe9ec22"; - const char *c_commit = "f5b0af1fb4f5c"; - git_tree *a, *b, *c; - git_diff_list *diff1 = NULL, *diff2 = NULL; - diff_expects exp; - - g_repo = cl_git_sandbox_init("attr"); - - cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL); - cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL); - cl_assert((c = resolve_commit_oid_to_tree(g_repo, c_commit)) != NULL); - - cl_git_pass(git_diff_tree_to_tree(&diff1, g_repo, a, b, NULL)); - - cl_git_pass(git_diff_tree_to_tree(&diff2, g_repo, c, b, NULL)); - - git_tree_free(a); - git_tree_free(b); - git_tree_free(c); - - cl_git_pass(git_diff_merge(diff1, diff2)); - - git_diff_list_free(diff2); - - memset(&exp, 0, sizeof(exp)); - - cl_git_pass(git_diff_foreach( - diff1, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(6, exp.files); - cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]); - - cl_assert_equal_i(6, exp.hunks); - - cl_assert_equal_i(59, exp.lines); - cl_assert_equal_i(1, exp.line_ctxt); - cl_assert_equal_i(36, exp.line_adds); - cl_assert_equal_i(22, exp.line_dels); - - git_diff_list_free(diff1); -} - -void test_diff_tree__larger_hunks(void) -{ - const char *a_commit = "d70d245ed97ed2aa596dd1af6536e4bfdb047b69"; - const char *b_commit = "7a9e0b02e63179929fed24f0a3e0f19168114d10"; - git_tree *a, *b; - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - size_t d, num_d, h, num_h, l, num_l, header_len, line_len; - const git_diff_delta *delta; - git_diff_patch *patch; - const git_diff_range *range; - const char *header, *line; - char origin; - - g_repo = cl_git_sandbox_init("diff"); - - cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL); - cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL); - - opts.context_lines = 1; - opts.interhunk_lines = 0; - - cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); - - num_d = git_diff_num_deltas(diff); - for (d = 0; d < num_d; ++d) { - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); - cl_assert(patch && delta); - - num_h = git_diff_patch_num_hunks(patch); - for (h = 0; h < num_h; h++) { - cl_git_pass(git_diff_patch_get_hunk( - &range, &header, &header_len, &num_l, patch, h)); - - for (l = 0; l < num_l; ++l) { - cl_git_pass(git_diff_patch_get_line_in_hunk( - &origin, &line, &line_len, NULL, NULL, patch, h, l)); - cl_assert(line); - } - - cl_git_fail(git_diff_patch_get_line_in_hunk( - &origin, &line, &line_len, NULL, NULL, patch, h, num_l)); - } - - cl_git_fail(git_diff_patch_get_hunk( - &range, &header, &header_len, &num_l, patch, num_h)); - - git_diff_patch_free(patch); - } - - cl_git_fail(git_diff_get_patch(&patch, &delta, diff, num_d)); - - cl_assert_equal_i(2, (int)num_d); - - git_diff_list_free(diff); - diff = NULL; - - git_tree_free(a); - git_tree_free(b); -} - -void test_diff_tree__checks_options_version(void) -{ - const char *a_commit = "8496071c1b46c85"; - const char *b_commit = "be3563ae3f79"; - git_tree *a, *b; - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - const git_error *err; - - g_repo = cl_git_sandbox_init("testrepo.git"); - - cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL); - cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL); - - opts.version = 0; - cl_git_fail(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); - - giterr_clear(); - opts.version = 1024; - cl_git_fail(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); - err = giterr_last(); - - git_tree_free(a); - git_tree_free(b); -} diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c deleted file mode 100644 index 21da63954ef..00000000000 --- a/tests-clar/diff/workdir.c +++ /dev/null @@ -1,927 +0,0 @@ -#include "clar_libgit2.h" -#include "diff_helpers.h" -#include "repository.h" - -static git_repository *g_repo = NULL; - -void test_diff_workdir__initialize(void) -{ -} - -void test_diff_workdir__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_diff_workdir__to_index(void) -{ - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - diff_expects exp; - int use_iterator; - - g_repo = cl_git_sandbox_init("status"); - - opts.context_lines = 3; - opts.interhunk_lines = 1; - opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; - - cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - else - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - /* to generate these values: - * - cd to tests/resources/status, - * - mv .gitted .git - * - git diff --name-status - * - git diff - * - mv .git .gitted - */ - cl_assert_equal_i(13, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]); - cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNTRACKED]); - - cl_assert_equal_i(8, exp.hunks); - - cl_assert_equal_i(14, exp.lines); - cl_assert_equal_i(5, exp.line_ctxt); - cl_assert_equal_i(4, exp.line_adds); - cl_assert_equal_i(5, exp.line_dels); - } - - git_diff_list_free(diff); -} - -void test_diff_workdir__to_tree(void) -{ - /* grabbed a couple of commit oids from the history of the attr repo */ - const char *a_commit = "26a125ee1bf"; /* the current HEAD */ - const char *b_commit = "0017bd4ab1ec3"; /* the start */ - git_tree *a, *b; - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - git_diff_list *diff2 = NULL; - diff_expects exp; - int use_iterator; - - g_repo = cl_git_sandbox_init("status"); - - a = resolve_commit_oid_to_tree(g_repo, a_commit); - b = resolve_commit_oid_to_tree(g_repo, b_commit); - - opts.context_lines = 3; - opts.interhunk_lines = 1; - opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; - - /* You can't really generate the equivalent of git_diff_tree_to_workdir() - * using C git. It really wants to interpose the index into the diff. - * - * To validate the following results with command line git, I ran the - * following: - * - git ls-tree 26a125 - * - find . ! -path ./.git/\* -a -type f | git hash-object --stdin-paths - * The results are documented at the bottom of this file in the - * long comment entitled "PREPARATION OF TEST DATA". - */ - cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, a, &opts)); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - else - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(14, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]); - cl_assert_equal_i(5, exp.file_status[GIT_DELTA_UNTRACKED]); - } - - /* Since there is no git diff equivalent, let's just assume that the - * text diffs produced by git_diff_foreach are accurate here. We will - * do more apples-to-apples test comparison below. - */ - - git_diff_list_free(diff); - diff = NULL; - memset(&exp, 0, sizeof(exp)); - - /* This is a compatible emulation of "git diff " which looks like - * a workdir to tree diff (even though it is not really). This is what - * you would get from "git diff --name-status 26a125ee1bf" - */ - cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts)); - cl_git_pass(git_diff_index_to_workdir(&diff2, g_repo, NULL, &opts)); - cl_git_pass(git_diff_merge(diff, diff2)); - git_diff_list_free(diff2); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - else - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(15, exp.files); - cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(5, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]); - cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]); - - cl_assert_equal_i(11, exp.hunks); - - cl_assert_equal_i(17, exp.lines); - cl_assert_equal_i(4, exp.line_ctxt); - cl_assert_equal_i(8, exp.line_adds); - cl_assert_equal_i(5, exp.line_dels); - } - - git_diff_list_free(diff); - diff = NULL; - memset(&exp, 0, sizeof(exp)); - - /* Again, emulating "git diff " for testing purposes using - * "git diff --name-status 0017bd4ab1ec3" instead. - */ - cl_git_pass(git_diff_tree_to_index(&diff, g_repo, b, NULL, &opts)); - cl_git_pass(git_diff_index_to_workdir(&diff2, g_repo, NULL, &opts)); - cl_git_pass(git_diff_merge(diff, diff2)); - git_diff_list_free(diff2); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - else - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(16, exp.files); - cl_assert_equal_i(5, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]); - cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]); - - cl_assert_equal_i(12, exp.hunks); - - cl_assert_equal_i(19, exp.lines); - cl_assert_equal_i(3, exp.line_ctxt); - cl_assert_equal_i(12, exp.line_adds); - cl_assert_equal_i(4, exp.line_dels); - } - - git_diff_list_free(diff); - - git_tree_free(a); - git_tree_free(b); -} - -void test_diff_workdir__to_index_with_pathspec(void) -{ - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - diff_expects exp; - char *pathspec = NULL; - int use_iterator; - - g_repo = cl_git_sandbox_init("status"); - - opts.context_lines = 3; - opts.interhunk_lines = 1; - opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; - opts.pathspec.strings = &pathspec; - opts.pathspec.count = 1; - - cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff, diff_file_cb, NULL, NULL, &exp)); - else - cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, &exp)); - - cl_assert_equal_i(13, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]); - cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNTRACKED]); - } - - git_diff_list_free(diff); - - pathspec = "modified_file"; - - cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff, diff_file_cb, NULL, NULL, &exp)); - else - cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, &exp)); - - cl_assert_equal_i(1, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_UNTRACKED]); - } - - git_diff_list_free(diff); - - pathspec = "subdir"; - - cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff, diff_file_cb, NULL, NULL, &exp)); - else - cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, &exp)); - - cl_assert_equal_i(3, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]); - } - - git_diff_list_free(diff); - - pathspec = "*_deleted"; - - cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff, diff_file_cb, NULL, NULL, &exp)); - else - cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, &exp)); - - cl_assert_equal_i(2, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_UNTRACKED]); - } - - git_diff_list_free(diff); -} - -void test_diff_workdir__filemode_changes(void) -{ - git_config *cfg; - git_diff_list *diff = NULL; - diff_expects exp; - int use_iterator; - - if (!cl_is_chmod_supported()) - return; - - g_repo = cl_git_sandbox_init("issue_592"); - - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_git_pass(git_config_set_bool(cfg, "core.filemode", true)); - - /* test once with no mods */ - - cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL)); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - else - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(0, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, exp.hunks); - } - - git_diff_list_free(diff); - - /* chmod file and test again */ - - cl_assert(cl_toggle_filemode("issue_592/a.txt")); - - cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL)); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - else - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(1, exp.files); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, exp.hunks); - } - - git_diff_list_free(diff); - - cl_assert(cl_toggle_filemode("issue_592/a.txt")); - git_config_free(cfg); -} - -void test_diff_workdir__filemode_changes_with_filemode_false(void) -{ - git_config *cfg; - git_diff_list *diff = NULL; - diff_expects exp; - - if (!cl_is_chmod_supported()) - return; - - g_repo = cl_git_sandbox_init("issue_592"); - - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_git_pass(git_config_set_bool(cfg, "core.filemode", false)); - - /* test once with no mods */ - - cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL)); - - memset(&exp, 0, sizeof(exp)); - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(0, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, exp.hunks); - - git_diff_list_free(diff); - - /* chmod file and test again */ - - cl_assert(cl_toggle_filemode("issue_592/a.txt")); - - cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL)); - - memset(&exp, 0, sizeof(exp)); - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(0, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, exp.hunks); - - git_diff_list_free(diff); - - cl_assert(cl_toggle_filemode("issue_592/a.txt")); - git_config_free(cfg); -} - -void test_diff_workdir__head_index_and_workdir_all_differ(void) -{ - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff_i2t = NULL, *diff_w2i = NULL; - diff_expects exp; - char *pathspec = "staged_changes_modified_file"; - git_tree *tree; - int use_iterator; - - /* For this file, - * - head->index diff has 1 line of context, 1 line of diff - * - index->workdir diff has 2 lines of context, 1 line of diff - * but - * - head->workdir diff has 1 line of context, 2 lines of diff - * Let's make sure the right one is returned from each fn. - */ - - g_repo = cl_git_sandbox_init("status"); - - tree = resolve_commit_oid_to_tree(g_repo, "26a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f"); - - opts.pathspec.strings = &pathspec; - opts.pathspec.count = 1; - - cl_git_pass(git_diff_tree_to_index(&diff_i2t, g_repo, tree, NULL, &opts)); - cl_git_pass(git_diff_index_to_workdir(&diff_w2i, g_repo, NULL, &opts)); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff_i2t, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - else - cl_git_pass(git_diff_foreach( - diff_i2t, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(1, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(1, exp.hunks); - cl_assert_equal_i(2, exp.lines); - cl_assert_equal_i(1, exp.line_ctxt); - cl_assert_equal_i(1, exp.line_adds); - cl_assert_equal_i(0, exp.line_dels); - } - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff_w2i, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - else - cl_git_pass(git_diff_foreach( - diff_w2i, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(1, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(1, exp.hunks); - cl_assert_equal_i(3, exp.lines); - cl_assert_equal_i(2, exp.line_ctxt); - cl_assert_equal_i(1, exp.line_adds); - cl_assert_equal_i(0, exp.line_dels); - } - - cl_git_pass(git_diff_merge(diff_i2t, diff_w2i)); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff_i2t, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - else - cl_git_pass(git_diff_foreach( - diff_i2t, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(1, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(1, exp.hunks); - cl_assert_equal_i(3, exp.lines); - cl_assert_equal_i(1, exp.line_ctxt); - cl_assert_equal_i(2, exp.line_adds); - cl_assert_equal_i(0, exp.line_dels); - } - - git_diff_list_free(diff_i2t); - git_diff_list_free(diff_w2i); - - git_tree_free(tree); -} - -void test_diff_workdir__eof_newline_changes(void) -{ - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - diff_expects exp; - char *pathspec = "current_file"; - int use_iterator; - - g_repo = cl_git_sandbox_init("status"); - - opts.pathspec.strings = &pathspec; - opts.pathspec.count = 1; - - cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - else - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(0, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, exp.hunks); - cl_assert_equal_i(0, exp.lines); - cl_assert_equal_i(0, exp.line_ctxt); - cl_assert_equal_i(0, exp.line_adds); - cl_assert_equal_i(0, exp.line_dels); - } - - git_diff_list_free(diff); - - cl_git_append2file("status/current_file", "\n"); - - cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - else - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(1, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(1, exp.hunks); - cl_assert_equal_i(2, exp.lines); - cl_assert_equal_i(1, exp.line_ctxt); - cl_assert_equal_i(1, exp.line_adds); - cl_assert_equal_i(0, exp.line_dels); - } - - git_diff_list_free(diff); - - cl_git_rewritefile("status/current_file", "current_file"); - - cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); - - for (use_iterator = 0; use_iterator <= 1; use_iterator++) { - memset(&exp, 0, sizeof(exp)); - - if (use_iterator) - cl_git_pass(diff_foreach_via_iterator( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - else - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(1, exp.files); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(1, exp.hunks); - cl_assert_equal_i(3, exp.lines); - cl_assert_equal_i(0, exp.line_ctxt); - cl_assert_equal_i(1, exp.line_adds); - cl_assert_equal_i(2, exp.line_dels); - } - - git_diff_list_free(diff); -} - -/* PREPARATION OF TEST DATA - * - * Since there is no command line equivalent of git_diff_tree_to_workdir, - * it was a bit of a pain to confirm that I was getting the expected - * results in the first part of this tests. Here is what I ended up - * doing to set my expectation for the file counts and results: - * - * Running "git ls-tree 26a125" and "git ls-tree aa27a6" shows: - * - * A a0de7e0ac200c489c41c59dfa910154a70264e6e current_file - * B 5452d32f1dd538eb0405e8a83cc185f79e25e80f file_deleted - * C 452e4244b5d083ddf0460acf1ecc74db9dcfa11a modified_file - * D 32504b727382542f9f089e24fddac5e78533e96c staged_changes - * E 061d42a44cacde5726057b67558821d95db96f19 staged_changes_file_deleted - * F 70bd9443ada07063e7fbf0b3ff5c13f7494d89c2 staged_changes_modified_file - * G e9b9107f290627c04d097733a10055af941f6bca staged_delete_file_deleted - * H dabc8af9bd6e9f5bbe96a176f1a24baf3d1f8916 staged_delete_modified_file - * I 53ace0d1cc1145a5f4fe4f78a186a60263190733 subdir/current_file - * J 1888c805345ba265b0ee9449b8877b6064592058 subdir/deleted_file - * K a6191982709b746d5650e93c2acf34ef74e11504 subdir/modified_file - * L e8ee89e15bbe9b20137715232387b3de5b28972e subdir.txt - * - * -------- - * - * find . ! -path ./.git/\* -a -type f | git hash-object --stdin-paths - * - * A a0de7e0ac200c489c41c59dfa910154a70264e6e current_file - * M 6a79f808a9c6bc9531ac726c184bbcd9351ccf11 ignored_file - * C 0a539630525aca2e7bc84975958f92f10a64c9b6 modified_file - * N d4fa8600b4f37d7516bef4816ae2c64dbf029e3a new_file - * D 55d316c9ba708999f1918e9677d01dfcae69c6b9 staged_changes - * F 011c3440d5c596e21d836aa6d7b10eb581f68c49 staged_changes_modified_file - * H dabc8af9bd6e9f5bbe96a176f1a24baf3d1f8916 staged_delete_modified_file - * O 529a16e8e762d4acb7b9636ff540a00831f9155a staged_new_file - * P 8b090c06d14ffa09c4e880088ebad33893f921d1 staged_new_file_modified_file - * I 53ace0d1cc1145a5f4fe4f78a186a60263190733 subdir/current_file - * K 57274b75eeb5f36fd55527806d567b2240a20c57 subdir/modified_file - * Q 80a86a6931b91bc01c2dbf5ca55bdd24ad1ef466 subdir/new_file - * L e8ee89e15bbe9b20137715232387b3de5b28972e subdir.txt - * - * -------- - * - * A - current_file (UNMODIFIED) -> not in results - * B D file_deleted - * M I ignored_file (IGNORED) - * C M modified_file - * N U new_file (UNTRACKED) - * D M staged_changes - * E D staged_changes_file_deleted - * F M staged_changes_modified_file - * G D staged_delete_file_deleted - * H - staged_delete_modified_file (UNMODIFIED) -> not in results - * O U staged_new_file - * P U staged_new_file_modified_file - * I - subdir/current_file (UNMODIFIED) -> not in results - * J D subdir/deleted_file - * K M subdir/modified_file - * Q U subdir/new_file - * L - subdir.txt (UNMODIFIED) -> not in results - * - * Expect 13 files, 0 ADD, 4 DEL, 4 MOD, 1 IGN, 4 UNTR - */ - - -void test_diff_workdir__larger_hunks(void) -{ - const char *a_commit = "d70d245ed97ed2aa596dd1af6536e4bfdb047b69"; - const char *b_commit = "7a9e0b02e63179929fed24f0a3e0f19168114d10"; - git_tree *a, *b; - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - size_t i, d, num_d, h, num_h, l, num_l, header_len, line_len; - - g_repo = cl_git_sandbox_init("diff"); - - cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL); - cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL); - - opts.context_lines = 1; - opts.interhunk_lines = 0; - - for (i = 0; i <= 2; ++i) { - git_diff_list *diff = NULL; - git_diff_patch *patch; - const git_diff_range *range; - const char *header, *line; - char origin; - - /* okay, this is a bit silly, but oh well */ - switch (i) { - case 0: - cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); - break; - case 1: - cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, a, &opts)); - break; - case 2: - cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, b, &opts)); - break; - } - - num_d = git_diff_num_deltas(diff); - cl_assert_equal_i(2, (int)num_d); - - for (d = 0; d < num_d; ++d) { - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, d)); - cl_assert(patch); - - num_h = git_diff_patch_num_hunks(patch); - for (h = 0; h < num_h; h++) { - cl_git_pass(git_diff_patch_get_hunk( - &range, &header, &header_len, &num_l, patch, h)); - - for (l = 0; l < num_l; ++l) { - cl_git_pass(git_diff_patch_get_line_in_hunk( - &origin, &line, &line_len, NULL, NULL, patch, h, l)); - cl_assert(line); - } - - /* confirm fail after the last item */ - cl_git_fail(git_diff_patch_get_line_in_hunk( - &origin, &line, &line_len, NULL, NULL, patch, h, num_l)); - } - - /* confirm fail after the last item */ - cl_git_fail(git_diff_patch_get_hunk( - &range, &header, &header_len, &num_l, patch, num_h)); - - git_diff_patch_free(patch); - } - - git_diff_list_free(diff); - } - - git_tree_free(a); - git_tree_free(b); -} - -/* Set up a test that exercises this code. The easiest test using existing - * test data is probably to create a sandbox of submod2 and then run a - * git_diff_tree_to_workdir against tree - * 873585b94bdeabccea991ea5e3ec1a277895b698. As for what you should actually - * test, you can start by just checking that the number of lines of diff - * content matches the actual output of git diff. That will at least - * demonstrate that the submodule content is being used to generate somewhat - * comparable outputs. It is a test that would fail without this code and - * will succeed with it. - */ - -#include "../submodule/submodule_helpers.h" - -void test_diff_workdir__submodules(void) -{ - const char *a_commit = "873585b94bdeabccea991ea5e3ec1a277895b698"; - git_tree *a; - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - diff_expects exp; - - g_repo = cl_git_sandbox_init("submod2"); - - cl_fixture_sandbox("submod2_target"); - p_rename("submod2_target/.gitted", "submod2_target/.git"); - - rewrite_gitmodules(git_repository_workdir(g_repo)); - p_rename("submod2/not_submodule/.gitted", "submod2/not_submodule/.git"); - - cl_fixture_cleanup("submod2_target"); - - a = resolve_commit_oid_to_tree(g_repo, a_commit); - - opts.flags = - GIT_DIFF_INCLUDE_UNTRACKED | - GIT_DIFF_RECURSE_UNTRACKED_DIRS | - GIT_DIFF_INCLUDE_UNTRACKED_CONTENT; - - cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, a, &opts)); - - /* diff_print(stderr, diff); */ - - /* essentially doing: git diff 873585b94bdeabccea991ea5e3ec1a277895b698 */ - - memset(&exp, 0, sizeof(exp)); - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - /* the following differs from "git diff 873585" by one "untracked" file - * because the diff list includes the "not_submodule/" directory which - * is not displayed in the text diff. - */ - - cl_assert_equal_i(10, exp.files); - - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]); - cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]); - cl_assert_equal_i(9, exp.file_status[GIT_DELTA_UNTRACKED]); - - /* the following numbers match "git diff 873585" exactly */ - - cl_assert_equal_i(9, exp.hunks); - - cl_assert_equal_i(33, exp.lines); - cl_assert_equal_i(2, exp.line_ctxt); - cl_assert_equal_i(30, exp.line_adds); - cl_assert_equal_i(1, exp.line_dels); - - git_diff_list_free(diff); - git_tree_free(a); -} - -void test_diff_workdir__cannot_diff_against_a_bare_repository(void) -{ - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - git_tree *tree; - - g_repo = cl_git_sandbox_init("testrepo.git"); - - cl_assert_equal_i( - GIT_EBAREREPO, git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); - - cl_git_pass(git_repository_head_tree(&tree, g_repo)); - - cl_assert_equal_i( - GIT_EBAREREPO, git_diff_tree_to_workdir(&diff, g_repo, tree, &opts)); - - git_tree_free(tree); -} - -void test_diff_workdir__to_null_tree(void) -{ - git_diff_list *diff; - diff_expects exp; - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - - opts.flags = GIT_DIFF_INCLUDE_UNTRACKED | - GIT_DIFF_RECURSE_UNTRACKED_DIRS; - - g_repo = cl_git_sandbox_init("status"); - - cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, NULL, &opts)); - - memset(&exp, 0, sizeof(exp)); - - cl_git_pass(git_diff_foreach( - diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); - - cl_assert_equal_i(exp.files, exp.file_status[GIT_DELTA_UNTRACKED]); - - git_diff_list_free(diff); -} - -void test_diff_workdir__checks_options_version(void) -{ - git_diff_list *diff; - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - const git_error *err; - - g_repo = cl_git_sandbox_init("status"); - - opts.version = 0; - cl_git_fail(git_diff_tree_to_workdir(&diff, g_repo, NULL, &opts)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); - - giterr_clear(); - opts.version = 1024; - cl_git_fail(git_diff_tree_to_workdir(&diff, g_repo, NULL, &opts)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); -} - -void test_diff_workdir__can_diff_empty_file(void) -{ - git_diff_list *diff; - git_tree *tree; - git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - struct stat st; - git_diff_patch *patch; - - g_repo = cl_git_sandbox_init("attr_index"); - - tree = resolve_commit_oid_to_tree(g_repo, "3812cfef3661"); /* HEAD */ - - /* baseline - make sure there are no outstanding diffs */ - - cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts)); - git_tree_free(tree); - cl_assert_equal_i(2, (int)git_diff_num_deltas(diff)); - git_diff_list_free(diff); - - /* empty contents of file */ - - cl_git_rewritefile("attr_index/README.txt", ""); - cl_git_pass(git_path_lstat("attr_index/README.txt", &st)); - cl_assert_equal_i(0, (int)st.st_size); - - cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts)); - cl_assert_equal_i(3, (int)git_diff_num_deltas(diff)); - /* diffs are: .gitattributes, README.txt, sub/sub/.gitattributes */ - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 1)); - git_diff_patch_free(patch); - git_diff_list_free(diff); - - /* remove a file altogether */ - - cl_git_pass(p_unlink("attr_index/README.txt")); - cl_assert(!git_path_exists("attr_index/README.txt")); - - cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts)); - cl_assert_equal_i(3, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 1)); - git_diff_patch_free(patch); - git_diff_list_free(diff); -} diff --git a/tests-clar/fetchhead/fetchhead_data.h b/tests-clar/fetchhead/fetchhead_data.h deleted file mode 100644 index 71f67be252a..00000000000 --- a/tests-clar/fetchhead/fetchhead_data.h +++ /dev/null @@ -1,21 +0,0 @@ - -#define FETCH_HEAD_WILDCARD_DATA \ - "49322bb17d3acc9146f98c97d078513228bbf3c0\t\tbranch 'master' of git://github.com/libgit2/TestGitRepository\n" \ - "0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of git://github.com/libgit2/TestGitRepository\n" \ - "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of git://github.com/libgit2/TestGitRepository\n" \ - "d96c4e80345534eccee5ac7b07fc7603b56124cb\tnot-for-merge\ttag 'annotated_tag' of git://github.com/libgit2/TestGitRepository\n" \ - "55a1a760df4b86a02094a904dfa511deb5655905\tnot-for-merge\ttag 'blob' of git://github.com/libgit2/TestGitRepository\n" \ - "8f50ba15d49353813cc6e20298002c0d17b0a9ee\tnot-for-merge\ttag 'commit_tree' of git://github.com/libgit2/TestGitRepository\n" - -#define FETCH_HEAD_NO_MERGE_DATA \ - "0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of git://github.com/libgit2/TestGitRepository\n" \ - "49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\tbranch 'master' of git://github.com/libgit2/TestGitRepository\n" \ - "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of git://github.com/libgit2/TestGitRepository\n" \ - "d96c4e80345534eccee5ac7b07fc7603b56124cb\tnot-for-merge\ttag 'annotated_tag' of git://github.com/libgit2/TestGitRepository\n" \ - "55a1a760df4b86a02094a904dfa511deb5655905\tnot-for-merge\ttag 'blob' of git://github.com/libgit2/TestGitRepository\n" \ - "8f50ba15d49353813cc6e20298002c0d17b0a9ee\tnot-for-merge\ttag 'commit_tree' of git://github.com/libgit2/TestGitRepository\n" - - -#define FETCH_HEAD_EXPLICIT_DATA \ - "0966a434eb1a025db6b71485ab63a3bfbea520b6\t\tbranch 'first-merge' of git://github.com/libgit2/TestGitRepository\n" - diff --git a/tests-clar/fetchhead/nonetwork.c b/tests-clar/fetchhead/nonetwork.c deleted file mode 100644 index b8cb69e683a..00000000000 --- a/tests-clar/fetchhead/nonetwork.c +++ /dev/null @@ -1,309 +0,0 @@ -#include "clar_libgit2.h" - -#include "repository.h" -#include "fetchhead.h" - -#include "fetchhead_data.h" - -#define DO_LOCAL_TEST 0 - -static git_repository *g_repo; - -void test_fetchhead_nonetwork__initialize(void) -{ - g_repo = NULL; -} - -static void cleanup_repository(void *path) -{ - if (g_repo) { - git_repository_free(g_repo); - g_repo = NULL; - } - - cl_fixture_cleanup((const char *)path); -} - -static void populate_fetchhead(git_vector *out, git_repository *repo) -{ - git_fetchhead_ref *fetchhead_ref; - git_oid oid; - - cl_git_pass(git_oid_fromstr(&oid, - "49322bb17d3acc9146f98c97d078513228bbf3c0")); - cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 1, - "refs/heads/master", - "git://github.com/libgit2/TestGitRepository")); - cl_git_pass(git_vector_insert(out, fetchhead_ref)); - - cl_git_pass(git_oid_fromstr(&oid, - "0966a434eb1a025db6b71485ab63a3bfbea520b6")); - cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0, - "refs/heads/first-merge", - "git://github.com/libgit2/TestGitRepository")); - cl_git_pass(git_vector_insert(out, fetchhead_ref)); - - cl_git_pass(git_oid_fromstr(&oid, - "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1")); - cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0, - "refs/heads/no-parent", - "git://github.com/libgit2/TestGitRepository")); - cl_git_pass(git_vector_insert(out, fetchhead_ref)); - - cl_git_pass(git_oid_fromstr(&oid, - "d96c4e80345534eccee5ac7b07fc7603b56124cb")); - cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0, - "refs/tags/annotated_tag", - "git://github.com/libgit2/TestGitRepository")); - cl_git_pass(git_vector_insert(out, fetchhead_ref)); - - cl_git_pass(git_oid_fromstr(&oid, - "55a1a760df4b86a02094a904dfa511deb5655905")); - cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0, - "refs/tags/blob", - "git://github.com/libgit2/TestGitRepository")); - cl_git_pass(git_vector_insert(out, fetchhead_ref)); - - cl_git_pass(git_oid_fromstr(&oid, - "8f50ba15d49353813cc6e20298002c0d17b0a9ee")); - cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0, - "refs/tags/commit_tree", - "git://github.com/libgit2/TestGitRepository")); - cl_git_pass(git_vector_insert(out, fetchhead_ref)); - - cl_git_pass(git_fetchhead_write(repo, out)); -} - -void test_fetchhead_nonetwork__write(void) -{ - git_vector fetchhead_vector = GIT_VECTOR_INIT; - git_fetchhead_ref *fetchhead_ref; - git_buf fetchhead_buf = GIT_BUF_INIT; - int equals = 0; - size_t i; - - git_vector_init(&fetchhead_vector, 6, NULL); - - cl_set_cleanup(&cleanup_repository, "./test1"); - cl_git_pass(git_repository_init(&g_repo, "./test1", 0)); - - populate_fetchhead(&fetchhead_vector, g_repo); - - cl_git_pass(git_futils_readbuffer(&fetchhead_buf, - "./test1/.git/FETCH_HEAD")); - - equals = (strcmp(fetchhead_buf.ptr, FETCH_HEAD_WILDCARD_DATA) == 0); - - git_buf_free(&fetchhead_buf); - - git_vector_foreach(&fetchhead_vector, i, fetchhead_ref) { - git_fetchhead_ref_free(fetchhead_ref); - } - - git_vector_free(&fetchhead_vector); - - cl_assert(equals); -} - -typedef struct { - git_vector *fetchhead_vector; - size_t idx; -} fetchhead_ref_cb_data; - -static int fetchhead_ref_cb(const char *name, const char *url, - const git_oid *oid, unsigned int is_merge, void *payload) -{ - fetchhead_ref_cb_data *cb_data = payload; - git_fetchhead_ref *expected; - - cl_assert(payload); - - expected = git_vector_get(cb_data->fetchhead_vector, cb_data->idx); - - cl_assert(git_oid_cmp(&expected->oid, oid) == 0); - cl_assert(expected->is_merge == is_merge); - - if (expected->ref_name) - cl_assert_equal_s(expected->ref_name, name); - else - cl_assert(name == NULL); - - if (expected->remote_url) - cl_assert_equal_s(expected->remote_url, url); - else - cl_assert(url == NULL); - - cb_data->idx++; - - return 0; -} - -void test_fetchhead_nonetwork__read(void) -{ - git_vector fetchhead_vector = GIT_VECTOR_INIT; - git_fetchhead_ref *fetchhead_ref; - fetchhead_ref_cb_data cb_data; - size_t i; - - memset(&cb_data, 0x0, sizeof(fetchhead_ref_cb_data)); - - cl_set_cleanup(&cleanup_repository, "./test1"); - cl_git_pass(git_repository_init(&g_repo, "./test1", 0)); - - populate_fetchhead(&fetchhead_vector, g_repo); - - cb_data.fetchhead_vector = &fetchhead_vector; - - cl_git_pass(git_repository_fetchhead_foreach(g_repo, fetchhead_ref_cb, &cb_data)); - - git_vector_foreach(&fetchhead_vector, i, fetchhead_ref) { - git_fetchhead_ref_free(fetchhead_ref); - } - - git_vector_free(&fetchhead_vector); -} - -static int read_old_style_cb(const char *name, const char *url, - const git_oid *oid, unsigned int is_merge, void *payload) -{ - git_oid expected; - - GIT_UNUSED(payload); - - git_oid_fromstr(&expected, "49322bb17d3acc9146f98c97d078513228bbf3c0"); - - cl_assert(name == NULL); - cl_assert(url == NULL); - cl_assert(git_oid_cmp(&expected, oid) == 0); - cl_assert(is_merge == 1); - - return 0; -} - -void test_fetchhead_nonetwork__read_old_style(void) -{ - cl_set_cleanup(&cleanup_repository, "./test1"); - cl_git_pass(git_repository_init(&g_repo, "./test1", 0)); - - cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\n"); - - cl_git_pass(git_repository_fetchhead_foreach(g_repo, read_old_style_cb, NULL)); -} - -static int read_type_missing(const char *ref_name, const char *remote_url, - const git_oid *oid, unsigned int is_merge, void *payload) -{ - git_oid expected; - - GIT_UNUSED(payload); - - git_oid_fromstr(&expected, "49322bb17d3acc9146f98c97d078513228bbf3c0"); - - cl_assert_equal_s("name", ref_name); - cl_assert_equal_s("remote_url", remote_url); - cl_assert(git_oid_cmp(&expected, oid) == 0); - cl_assert(is_merge == 0); - - return 0; -} - -void test_fetchhead_nonetwork__type_missing(void) -{ - cl_set_cleanup(&cleanup_repository, "./test1"); - cl_git_pass(git_repository_init(&g_repo, "./test1", 0)); - - cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\t'name' of remote_url\n"); - - cl_git_pass(git_repository_fetchhead_foreach(g_repo, read_type_missing, NULL)); -} - -static int read_name_missing(const char *ref_name, const char *remote_url, - const git_oid *oid, unsigned int is_merge, void *payload) -{ - git_oid expected; - - GIT_UNUSED(payload); - - git_oid_fromstr(&expected, "49322bb17d3acc9146f98c97d078513228bbf3c0"); - - cl_assert(ref_name == NULL); - cl_assert_equal_s("remote_url", remote_url); - cl_assert(git_oid_cmp(&expected, oid) == 0); - cl_assert(is_merge == 0); - - return 0; -} - -void test_fetchhead_nonetwork__name_missing(void) -{ - cl_set_cleanup(&cleanup_repository, "./test1"); - cl_git_pass(git_repository_init(&g_repo, "./test1", 0)); - - cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\tremote_url\n"); - - cl_git_pass(git_repository_fetchhead_foreach(g_repo, read_name_missing, NULL)); -} - -static int read_noop(const char *ref_name, const char *remote_url, - const git_oid *oid, unsigned int is_merge, void *payload) -{ - GIT_UNUSED(ref_name); - GIT_UNUSED(remote_url); - GIT_UNUSED(oid); - GIT_UNUSED(is_merge); - GIT_UNUSED(payload); - - return 0; -} - -void test_fetchhead_nonetwork__nonexistent(void) -{ - int error; - - cl_set_cleanup(&cleanup_repository, "./test1"); - cl_git_pass(git_repository_init(&g_repo, "./test1", 0)); - - cl_git_fail((error = git_repository_fetchhead_foreach(g_repo, read_noop, NULL))); - cl_assert(error == GIT_ENOTFOUND); -} - -void test_fetchhead_nonetwork__invalid_unterminated_last_line(void) -{ - cl_set_cleanup(&cleanup_repository, "./test1"); - cl_git_pass(git_repository_init(&g_repo, "./test1", 0)); - - cl_git_rewritefile("./test1/.git/FETCH_HEAD", "unterminated"); - cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL)); -} - -void test_fetchhead_nonetwork__invalid_oid(void) -{ - cl_set_cleanup(&cleanup_repository, "./test1"); - cl_git_pass(git_repository_init(&g_repo, "./test1", 0)); - - cl_git_rewritefile("./test1/.git/FETCH_HEAD", "shortoid\n"); - cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL)); -} - -void test_fetchhead_nonetwork__invalid_for_merge(void) -{ - cl_set_cleanup(&cleanup_repository, "./test1"); - cl_git_pass(git_repository_init(&g_repo, "./test1", 0)); - - cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tinvalid-merge\t\n"); - cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL)); - - cl_assert(git__prefixcmp(giterr_last()->message, "Invalid for-merge") == 0); -} - -void test_fetchhead_nonetwork__invalid_description(void) -{ - cl_set_cleanup(&cleanup_repository, "./test1"); - cl_git_pass(git_repository_init(&g_repo, "./test1", 0)); - - cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\n"); - cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL)); - - cl_assert(git__prefixcmp(giterr_last()->message, "Invalid description") == 0); -} - diff --git a/tests-clar/generate.py b/tests-clar/generate.py deleted file mode 100644 index 1c96f9b6851..00000000000 --- a/tests-clar/generate.py +++ /dev/null @@ -1,244 +0,0 @@ -#!/usr/bin/env python -# -# Copyright (c) Vicent Marti. All rights reserved. -# -# This file is part of clar, distributed under the ISC license. -# For full terms see the included COPYING file. -# - -from __future__ import with_statement -from string import Template -import re, fnmatch, os, codecs, pickle - -class Module(object): - class Template(object): - def __init__(self, module): - self.module = module - - def _render_callback(self, cb): - if not cb: - return ' { NULL, NULL }' - return ' { "%s", &%s }' % (cb['short_name'], cb['symbol']) - - class DeclarationTemplate(Template): - def render(self): - out = "\n".join("extern %s;" % cb['declaration'] for cb in self.module.callbacks) + "\n" - - if self.module.initialize: - out += "extern %s;\n" % self.module.initialize['declaration'] - - if self.module.cleanup: - out += "extern %s;\n" % self.module.cleanup['declaration'] - - return out - - class CallbacksTemplate(Template): - def render(self): - out = "static const struct clar_func _clar_cb_%s[] = {\n" % self.module.name - out += ",\n".join(self._render_callback(cb) for cb in self.module.callbacks) - out += "\n};\n" - return out - - class InfoTemplate(Template): - def render(self): - return Template( - r""" - { - "${clean_name}", - ${initialize}, - ${cleanup}, - ${cb_ptr}, ${cb_count}, ${enabled} - }""" - ).substitute( - clean_name = self.module.clean_name(), - initialize = self._render_callback(self.module.initialize), - cleanup = self._render_callback(self.module.cleanup), - cb_ptr = "_clar_cb_%s" % self.module.name, - cb_count = len(self.module.callbacks), - enabled = int(self.module.enabled) - ) - - def __init__(self, name): - self.name = name - - self.mtime = 0 - self.enabled = True - self.modified = False - - def clean_name(self): - return self.name.replace("_", "::") - - def _skip_comments(self, text): - SKIP_COMMENTS_REGEX = re.compile( - r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', - re.DOTALL | re.MULTILINE) - - def _replacer(match): - s = match.group(0) - return "" if s.startswith('/') else s - - return re.sub(SKIP_COMMENTS_REGEX, _replacer, text) - - def parse(self, contents): - TEST_FUNC_REGEX = r"^(void\s+(test_%s__(\w+))\(\s*void\s*\))\s*\{" - - contents = self._skip_comments(contents) - regex = re.compile(TEST_FUNC_REGEX % self.name, re.MULTILINE) - - self.callbacks = [] - self.initialize = None - self.cleanup = None - - for (declaration, symbol, short_name) in regex.findall(contents): - data = { - "short_name" : short_name, - "declaration" : declaration, - "symbol" : symbol - } - - if short_name == 'initialize': - self.initialize = data - elif short_name == 'cleanup': - self.cleanup = data - else: - self.callbacks.append(data) - - return self.callbacks != [] - - def refresh(self, path): - self.modified = False - - try: - st = os.stat(path) - - # Not modified - if st.st_mtime == self.mtime: - return True - - self.modified = True - self.mtime = st.st_mtime - - with open(path) as fp: - raw_content = fp.read() - - except IOError: - return False - - return self.parse(raw_content) - -class TestSuite(object): - - def __init__(self, path): - self.path = path - - def should_generate(self, path): - if not os.path.isfile(path): - return True - - if any(module.modified for module in self.modules.values()): - return True - - return False - - def find_modules(self): - modules = [] - for root, _, files in os.walk(self.path): - module_root = root[len(self.path):] - module_root = [c for c in module_root.split(os.sep) if c] - - tests_in_module = fnmatch.filter(files, "*.c") - - for test_file in tests_in_module: - full_path = os.path.join(root, test_file) - module_name = "_".join(module_root + [test_file[:-2]]) - - modules.append((full_path, module_name)) - - return modules - - def load_cache(self): - path = os.path.join(self.path, '.clarcache') - cache = {} - - try: - fp = open(path, 'rb') - cache = pickle.load(fp) - fp.close() - except (IOError, ValueError): - pass - - return cache - - def save_cache(self): - path = os.path.join(self.path, '.clarcache') - with open(path, 'wb') as cache: - pickle.dump(self.modules, cache) - - def load(self, force = False): - module_data = self.find_modules() - self.modules = {} if force else self.load_cache() - - for path, name in module_data: - if name not in self.modules: - self.modules[name] = Module(name) - - if not self.modules[name].refresh(path): - del self.modules[name] - - def disable(self, excluded): - for exclude in excluded: - for module in self.modules.values(): - name = module.clean_name() - if name.startswith(exclude): - module.enabled = False - module.modified = True - - def suite_count(self): - return len(self.modules) - - def callback_count(self): - return sum(len(module.callbacks) for module in self.modules.values()) - - def write(self): - output = os.path.join(self.path, 'clar.suite') - - if not self.should_generate(output): - return False - - with open(output, 'w') as data: - for module in self.modules.values(): - t = Module.DeclarationTemplate(module) - data.write(t.render()) - - for module in self.modules.values(): - t = Module.CallbacksTemplate(module) - data.write(t.render()) - - suites = "static struct clar_suite _clar_suites[] = {" + ','.join( - Module.InfoTemplate(module).render() for module in sorted(self.modules.values(), key=lambda module: module.name) - ) + "\n};\n" - - data.write(suites) - - data.write("static const size_t _clar_suite_count = %d;\n" % self.suite_count()) - data.write("static const size_t _clar_callback_count = %d;\n" % self.callback_count()) - - suite.save_cache() - return True - -if __name__ == '__main__': - from optparse import OptionParser - - parser = OptionParser() - parser.add_option('-f', '--force', dest='force', default=False) - parser.add_option('-x', '--exclude', dest='excluded', action='append', default=[]) - - options, args = parser.parse_args() - - for path in args or ['.']: - suite = TestSuite(path) - suite.load(options.force) - suite.disable(options.excluded) - if suite.write(): - print("Written `clar.suite` (%d suites)" % len(suite.modules)) - diff --git a/tests-clar/index/conflicts.c b/tests-clar/index/conflicts.c deleted file mode 100644 index 7eee496deab..00000000000 --- a/tests-clar/index/conflicts.c +++ /dev/null @@ -1,242 +0,0 @@ -#include "clar_libgit2.h" -#include "index.h" -#include "git2/repository.h" - -static git_repository *repo; -static git_index *repo_index; - -#define TEST_REPO_PATH "mergedrepo" -#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" - -#define CONFLICTS_ONE_ANCESTOR_OID "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81" -#define CONFLICTS_ONE_OUR_OID "6aea5f295304c36144ad6e9247a291b7f8112399" -#define CONFLICTS_ONE_THEIR_OID "516bd85f78061e09ccc714561d7b504672cb52da" - -#define CONFLICTS_TWO_ANCESTOR_OID "84af62840be1b1c47b778a8a249f3ff45155038c" -#define CONFLICTS_TWO_OUR_OID "8b3f43d2402825c200f835ca1762413e386fd0b2" -#define CONFLICTS_TWO_THEIR_OID "220bd62631c8cf7a83ef39c6b94595f00517211e" - -#define TEST_ANCESTOR_OID "f00ff00ff00ff00ff00ff00ff00ff00ff00ff00f" -#define TEST_OUR_OID "b44bb44bb44bb44bb44bb44bb44bb44bb44bb44b" -#define TEST_THEIR_OID "0123456789abcdef0123456789abcdef01234567" - -// Fixture setup and teardown -void test_index_conflicts__initialize(void) -{ - repo = cl_git_sandbox_init("mergedrepo"); - git_repository_index(&repo_index, repo); -} - -void test_index_conflicts__cleanup(void) -{ - git_index_free(repo_index); - repo_index = NULL; - - cl_git_sandbox_cleanup(); -} - -void test_index_conflicts__add(void) -{ - git_index_entry ancestor_entry, our_entry, their_entry; - - cl_assert(git_index_entrycount(repo_index) == 8); - - memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); - memset(&our_entry, 0x0, sizeof(git_index_entry)); - memset(&their_entry, 0x0, sizeof(git_index_entry)); - - ancestor_entry.path = "test-one.txt"; - ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT); - git_oid_fromstr(&ancestor_entry.oid, TEST_ANCESTOR_OID); - - our_entry.path = "test-one.txt"; - ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT); - git_oid_fromstr(&our_entry.oid, TEST_OUR_OID); - - their_entry.path = "test-one.txt"; - ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT); - git_oid_fromstr(&their_entry.oid, TEST_THEIR_OID); - - cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry)); - - cl_assert(git_index_entrycount(repo_index) == 11); -} - -void test_index_conflicts__add_fixes_incorrect_stage(void) -{ - git_index_entry ancestor_entry, our_entry, their_entry; - git_index_entry *conflict_entry[3]; - - cl_assert(git_index_entrycount(repo_index) == 8); - - memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); - memset(&our_entry, 0x0, sizeof(git_index_entry)); - memset(&their_entry, 0x0, sizeof(git_index_entry)); - - ancestor_entry.path = "test-one.txt"; - ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT); - git_oid_fromstr(&ancestor_entry.oid, TEST_ANCESTOR_OID); - - our_entry.path = "test-one.txt"; - ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT); - git_oid_fromstr(&our_entry.oid, TEST_OUR_OID); - - their_entry.path = "test-one.txt"; - ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT); - git_oid_fromstr(&their_entry.oid, TEST_THEIR_OID); - - cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry)); - - cl_assert(git_index_entrycount(repo_index) == 11); - - cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], repo_index, "test-one.txt")); - - cl_assert(git_index_entry_stage(conflict_entry[0]) == 1); - cl_assert(git_index_entry_stage(conflict_entry[1]) == 2); - cl_assert(git_index_entry_stage(conflict_entry[2]) == 3); -} - -void test_index_conflicts__get(void) -{ - git_index_entry *conflict_entry[3]; - git_oid oid; - - cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], - &conflict_entry[2], repo_index, "conflicts-one.txt")); - - cl_assert_equal_s("conflicts-one.txt", conflict_entry[0]->path); - - git_oid_fromstr(&oid, CONFLICTS_ONE_ANCESTOR_OID); - cl_assert(git_oid_cmp(&conflict_entry[0]->oid, &oid) == 0); - - git_oid_fromstr(&oid, CONFLICTS_ONE_OUR_OID); - cl_assert(git_oid_cmp(&conflict_entry[1]->oid, &oid) == 0); - - git_oid_fromstr(&oid, CONFLICTS_ONE_THEIR_OID); - cl_assert(git_oid_cmp(&conflict_entry[2]->oid, &oid) == 0); - - cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], - &conflict_entry[2], repo_index, "conflicts-two.txt")); - - cl_assert_equal_s("conflicts-two.txt", conflict_entry[0]->path); - - git_oid_fromstr(&oid, CONFLICTS_TWO_ANCESTOR_OID); - cl_assert(git_oid_cmp(&conflict_entry[0]->oid, &oid) == 0); - - git_oid_fromstr(&oid, CONFLICTS_TWO_OUR_OID); - cl_assert(git_oid_cmp(&conflict_entry[1]->oid, &oid) == 0); - - git_oid_fromstr(&oid, CONFLICTS_TWO_THEIR_OID); - cl_assert(git_oid_cmp(&conflict_entry[2]->oid, &oid) == 0); -} - -void test_index_conflicts__remove(void) -{ - const git_index_entry *entry; - size_t i; - - cl_assert(git_index_entrycount(repo_index) == 8); - - cl_git_pass(git_index_conflict_remove(repo_index, "conflicts-one.txt")); - cl_assert(git_index_entrycount(repo_index) == 5); - - for (i = 0; i < git_index_entrycount(repo_index); i++) { - cl_assert(entry = git_index_get_byindex(repo_index, i)); - cl_assert(strcmp(entry->path, "conflicts-one.txt") != 0); - } - - cl_git_pass(git_index_conflict_remove(repo_index, "conflicts-two.txt")); - cl_assert(git_index_entrycount(repo_index) == 2); - - for (i = 0; i < git_index_entrycount(repo_index); i++) { - cl_assert(entry = git_index_get_byindex(repo_index, i)); - cl_assert(strcmp(entry->path, "conflicts-two.txt") != 0); - } -} - -void test_index_conflicts__moved_to_reuc_on_add(void) -{ - const git_index_entry *entry; - size_t i; - - cl_assert(git_index_entrycount(repo_index) == 8); - - cl_git_mkfile("./mergedrepo/conflicts-one.txt", "new-file\n"); - - cl_git_pass(git_index_add_bypath(repo_index, "conflicts-one.txt")); - - cl_assert(git_index_entrycount(repo_index) == 6); - - for (i = 0; i < git_index_entrycount(repo_index); i++) { - cl_assert(entry = git_index_get_byindex(repo_index, i)); - - if (strcmp(entry->path, "conflicts-one.txt") == 0) - cl_assert(git_index_entry_stage(entry) == 0); - } -} - -void test_index_conflicts__moved_to_reuc_on_remove(void) -{ - const git_index_entry *entry; - size_t i; - - cl_assert(git_index_entrycount(repo_index) == 8); - - cl_git_pass(p_unlink("./mergedrepo/conflicts-one.txt")); - - cl_git_pass(git_index_remove_bypath(repo_index, "conflicts-one.txt")); - - cl_assert(git_index_entrycount(repo_index) == 5); - - for (i = 0; i < git_index_entrycount(repo_index); i++) { - cl_assert(entry = git_index_get_byindex(repo_index, i)); - cl_assert(strcmp(entry->path, "conflicts-one.txt") != 0); - } -} - -void test_index_conflicts__remove_all_conflicts(void) -{ - size_t i; - const git_index_entry *entry; - - cl_assert(git_index_entrycount(repo_index) == 8); - - cl_assert_equal_i(true, git_index_has_conflicts(repo_index)); - - git_index_conflict_cleanup(repo_index); - - cl_assert_equal_i(false, git_index_has_conflicts(repo_index)); - - cl_assert(git_index_entrycount(repo_index) == 2); - - for (i = 0; i < git_index_entrycount(repo_index); i++) { - cl_assert(entry = git_index_get_byindex(repo_index, i)); - cl_assert(git_index_entry_stage(entry) == 0); - } -} - -void test_index_conflicts__partial(void) -{ - git_index_entry ancestor_entry, our_entry, their_entry; - git_index_entry *conflict_entry[3]; - - cl_assert(git_index_entrycount(repo_index) == 8); - - memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); - memset(&our_entry, 0x0, sizeof(git_index_entry)); - memset(&their_entry, 0x0, sizeof(git_index_entry)); - - ancestor_entry.path = "test-one.txt"; - ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT); - git_oid_fromstr(&ancestor_entry.oid, TEST_ANCESTOR_OID); - - cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, NULL, NULL)); - cl_assert(git_index_entrycount(repo_index) == 9); - - cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], - &conflict_entry[2], repo_index, "test-one.txt")); - - cl_assert(git_oid_cmp(&ancestor_entry.oid, &conflict_entry[0]->oid) == 0); - cl_assert(conflict_entry[1] == NULL); - cl_assert(conflict_entry[2] == NULL); -} diff --git a/tests-clar/index/filemodes.c b/tests-clar/index/filemodes.c deleted file mode 100644 index 1bb44173c8b..00000000000 --- a/tests-clar/index/filemodes.c +++ /dev/null @@ -1,159 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "posix.h" -#include "index.h" - -static git_repository *g_repo = NULL; - -void test_index_filemodes__initialize(void) -{ - g_repo = cl_git_sandbox_init("filemodes"); -} - -void test_index_filemodes__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_index_filemodes__read(void) -{ - git_index *index; - unsigned int i; - static bool expected[6] = { 0, 1, 0, 1, 0, 1 }; - - cl_git_pass(git_repository_index(&index, g_repo)); - cl_assert_equal_i(6, (int)git_index_entrycount(index)); - - for (i = 0; i < 6; ++i) { - const git_index_entry *entry = git_index_get_byindex(index, i); - cl_assert(entry != NULL); - cl_assert(((entry->mode & 0100) ? 1 : 0) == expected[i]); - } - - git_index_free(index); -} - -static void replace_file_with_mode( - const char *filename, const char *backup, unsigned int create_mode) -{ - git_buf path = GIT_BUF_INIT, content = GIT_BUF_INIT; - - cl_git_pass(git_buf_joinpath(&path, "filemodes", filename)); - cl_git_pass(git_buf_printf(&content, "%s as %08u (%d)", - filename, create_mode, rand())); - - cl_git_pass(p_rename(path.ptr, backup)); - cl_git_write2file( - path.ptr, content.ptr, O_WRONLY|O_CREAT|O_TRUNC, create_mode); - - git_buf_free(&path); - git_buf_free(&content); -} - -static void add_and_check_mode( - git_index *index, const char *filename, unsigned int expect_mode) -{ - size_t pos; - const git_index_entry *entry; - - cl_git_pass(git_index_add_bypath(index, filename)); - - cl_assert(!git_index_find(&pos, index, filename)); - - entry = git_index_get_byindex(index, pos); - cl_assert(entry->mode == expect_mode); -} - -void test_index_filemodes__untrusted(void) -{ - git_config *cfg; - git_index *index; - bool can_filemode = cl_is_chmod_supported(); - - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_git_pass(git_config_set_bool(cfg, "core.filemode", false)); - git_config_free(cfg); - - cl_git_pass(git_repository_index(&index, g_repo)); - cl_assert((git_index_caps(index) & GIT_INDEXCAP_NO_FILEMODE) != 0); - - /* 1 - add 0644 over existing 0644 -> expect 0644 */ - replace_file_with_mode("exec_off", "filemodes/exec_off.0", 0644); - add_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB); - - /* 2 - add 0644 over existing 0755 -> expect 0755 */ - replace_file_with_mode("exec_on", "filemodes/exec_on.0", 0644); - add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE); - - /* 3 - add 0755 over existing 0644 -> expect 0644 */ - replace_file_with_mode("exec_off", "filemodes/exec_off.1", 0755); - add_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB); - - /* 4 - add 0755 over existing 0755 -> expect 0755 */ - replace_file_with_mode("exec_on", "filemodes/exec_on.1", 0755); - add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE); - - /* 5 - add new 0644 -> expect 0644 */ - cl_git_write2file("filemodes/new_off", "blah", - O_WRONLY | O_CREAT | O_TRUNC, 0644); - add_and_check_mode(index, "new_off", GIT_FILEMODE_BLOB); - - /* this test won't give predictable results on a platform - * that doesn't support filemodes correctly, so skip it. - */ - if (can_filemode) { - /* 6 - add 0755 -> expect 0755 */ - cl_git_write2file("filemodes/new_on", "blah", - O_WRONLY | O_CREAT | O_TRUNC, 0755); - add_and_check_mode(index, "new_on", GIT_FILEMODE_BLOB_EXECUTABLE); - } - - git_index_free(index); -} - -void test_index_filemodes__trusted(void) -{ - git_config *cfg; - git_index *index; - - /* Only run these tests on platforms where I can actually - * chmod a file and get the stat results I expect! - */ - if (!cl_is_chmod_supported()) - return; - - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_git_pass(git_config_set_bool(cfg, "core.filemode", true)); - git_config_free(cfg); - - cl_git_pass(git_repository_index(&index, g_repo)); - cl_assert((git_index_caps(index) & GIT_INDEXCAP_NO_FILEMODE) == 0); - - /* 1 - add 0644 over existing 0644 -> expect 0644 */ - replace_file_with_mode("exec_off", "filemodes/exec_off.0", 0644); - add_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB); - - /* 2 - add 0644 over existing 0755 -> expect 0644 */ - replace_file_with_mode("exec_on", "filemodes/exec_on.0", 0644); - add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB); - - /* 3 - add 0755 over existing 0644 -> expect 0755 */ - replace_file_with_mode("exec_off", "filemodes/exec_off.1", 0755); - add_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB_EXECUTABLE); - - /* 4 - add 0755 over existing 0755 -> expect 0755 */ - replace_file_with_mode("exec_on", "filemodes/exec_on.1", 0755); - add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE); - - /* 5 - add new 0644 -> expect 0644 */ - cl_git_write2file("filemodes/new_off", "blah", - O_WRONLY | O_CREAT | O_TRUNC, 0644); - add_and_check_mode(index, "new_off", GIT_FILEMODE_BLOB); - - /* 6 - add 0755 -> expect 0755 */ - cl_git_write2file("filemodes/new_on", "blah", - O_WRONLY | O_CREAT | O_TRUNC, 0755); - add_and_check_mode(index, "new_on", GIT_FILEMODE_BLOB_EXECUTABLE); - - git_index_free(index); -} diff --git a/tests-clar/index/inmemory.c b/tests-clar/index/inmemory.c deleted file mode 100644 index 38e91e0fd94..00000000000 --- a/tests-clar/index/inmemory.c +++ /dev/null @@ -1,22 +0,0 @@ -#include "clar_libgit2.h" - -void test_index_inmemory__can_create_an_inmemory_index(void) -{ - git_index *index; - - cl_git_pass(git_index_new(&index)); - cl_assert_equal_i(0, (int)git_index_entrycount(index)); - - git_index_free(index); -} - -void test_index_inmemory__cannot_add_bypath_to_an_inmemory_index(void) -{ - git_index *index; - - cl_git_pass(git_index_new(&index)); - - cl_assert_equal_i(GIT_ERROR, git_index_add_bypath(index, "test.txt")); - - git_index_free(index); -} diff --git a/tests-clar/index/rename.c b/tests-clar/index/rename.c deleted file mode 100644 index 4deef133274..00000000000 --- a/tests-clar/index/rename.c +++ /dev/null @@ -1,50 +0,0 @@ -#include "clar_libgit2.h" -#include "posix.h" - -void test_index_rename__single_file(void) -{ - git_repository *repo; - git_index *index; - size_t position; - git_oid expected; - const git_index_entry *entry; - - p_mkdir("rename", 0700); - - cl_git_pass(git_repository_init(&repo, "./rename", 0)); - cl_git_pass(git_repository_index(&index, repo)); - - cl_assert(git_index_entrycount(index) == 0); - - cl_git_mkfile("./rename/lame.name.txt", "new_file\n"); - - /* This should add a new blob to the object database in 'd4/fa8600b4f37d7516bef4816ae2c64dbf029e3a' */ - cl_git_pass(git_index_add_bypath(index, "lame.name.txt")); - cl_assert(git_index_entrycount(index) == 1); - - cl_git_pass(git_oid_fromstr(&expected, "d4fa8600b4f37d7516bef4816ae2c64dbf029e3a")); - - cl_assert(!git_index_find(&position, index, "lame.name.txt")); - - entry = git_index_get_byindex(index, position); - cl_assert(git_oid_cmp(&expected, &entry->oid) == 0); - - /* This removes the entry from the index, but not from the object database */ - cl_git_pass(git_index_remove(index, "lame.name.txt", 0)); - cl_assert(git_index_entrycount(index) == 0); - - p_rename("./rename/lame.name.txt", "./rename/fancy.name.txt"); - - cl_git_pass(git_index_add_bypath(index, "fancy.name.txt")); - cl_assert(git_index_entrycount(index) == 1); - - cl_assert(!git_index_find(&position, index, "fancy.name.txt")); - - entry = git_index_get_byindex(index, position); - cl_assert(git_oid_cmp(&expected, &entry->oid) == 0); - - git_index_free(index); - git_repository_free(repo); - - cl_fixture_cleanup("rename"); -} diff --git a/tests-clar/index/reuc.c b/tests-clar/index/reuc.c deleted file mode 100644 index 80c295eaa2d..00000000000 --- a/tests-clar/index/reuc.c +++ /dev/null @@ -1,288 +0,0 @@ -#include "clar_libgit2.h" -#include "index.h" -#include "git2/repository.h" - -static git_repository *repo; -static git_index *repo_index; - -#define TEST_REPO_PATH "mergedrepo" -#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" - -#define ONE_ANCESTOR_OID "478871385b9cd03908c5383acfd568bef023c6b3" -#define ONE_OUR_OID "4458b8bc9e72b6c8755ae456f60e9844d0538d8c" -#define ONE_THEIR_OID "8b72416545c7e761b64cecad4f1686eae4078aa8" - -#define TWO_ANCESTOR_OID "9d81f82fccc7dcd7de7a1ffead1815294c2e092c" -#define TWO_OUR_OID "8f3c06cff9a83757cec40c80bc9bf31a2582bde9" -#define TWO_THEIR_OID "887b153b165d32409c70163e0f734c090f12f673" - -// Fixture setup and teardown -void test_index_reuc__initialize(void) -{ - repo = cl_git_sandbox_init("mergedrepo"); - git_repository_index(&repo_index, repo); -} - -void test_index_reuc__cleanup(void) -{ - git_index_free(repo_index); - repo_index = NULL; - - cl_git_sandbox_cleanup(); -} - -void test_index_reuc__add(void) -{ - git_oid ancestor_oid, our_oid, their_oid; - const git_index_reuc_entry *reuc; - - git_oid_fromstr(&ancestor_oid, ONE_ANCESTOR_OID); - git_oid_fromstr(&our_oid, ONE_OUR_OID); - git_oid_fromstr(&their_oid, ONE_THEIR_OID); - - cl_git_pass(git_index_reuc_add(repo_index, "newfile.txt", - 0100644, &ancestor_oid, - 0100644, &our_oid, - 0100644, &their_oid)); - - cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "newfile.txt")); - - cl_assert_equal_s("newfile.txt", reuc->path); - cl_assert(reuc->mode[0] == 0100644); - cl_assert(reuc->mode[1] == 0100644); - cl_assert(reuc->mode[2] == 0100644); - cl_assert(git_oid_cmp(&reuc->oid[0], &ancestor_oid) == 0); - cl_assert(git_oid_cmp(&reuc->oid[1], &our_oid) == 0); - cl_assert(git_oid_cmp(&reuc->oid[2], &their_oid) == 0); -} - -void test_index_reuc__add_no_ancestor(void) -{ - git_oid ancestor_oid, our_oid, their_oid; - const git_index_reuc_entry *reuc; - - memset(&ancestor_oid, 0x0, sizeof(git_oid)); - git_oid_fromstr(&our_oid, ONE_OUR_OID); - git_oid_fromstr(&their_oid, ONE_THEIR_OID); - - cl_git_pass(git_index_reuc_add(repo_index, "newfile.txt", - 0, NULL, - 0100644, &our_oid, - 0100644, &their_oid)); - - cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "newfile.txt")); - - cl_assert_equal_s("newfile.txt", reuc->path); - cl_assert(reuc->mode[0] == 0); - cl_assert(reuc->mode[1] == 0100644); - cl_assert(reuc->mode[2] == 0100644); - cl_assert(git_oid_cmp(&reuc->oid[0], &ancestor_oid) == 0); - cl_assert(git_oid_cmp(&reuc->oid[1], &our_oid) == 0); - cl_assert(git_oid_cmp(&reuc->oid[2], &their_oid) == 0); -} - -void test_index_reuc__read_bypath(void) -{ - const git_index_reuc_entry *reuc; - git_oid oid; - - cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index)); - - cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "two.txt")); - - cl_assert_equal_s("two.txt", reuc->path); - cl_assert(reuc->mode[0] == 0100644); - cl_assert(reuc->mode[1] == 0100644); - cl_assert(reuc->mode[2] == 0100644); - git_oid_fromstr(&oid, TWO_ANCESTOR_OID); - cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0); - git_oid_fromstr(&oid, TWO_OUR_OID); - cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0); - git_oid_fromstr(&oid, TWO_THEIR_OID); - cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0); - - cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "one.txt")); - - cl_assert_equal_s("one.txt", reuc->path); - cl_assert(reuc->mode[0] == 0100644); - cl_assert(reuc->mode[1] == 0100644); - cl_assert(reuc->mode[2] == 0100644); - git_oid_fromstr(&oid, ONE_ANCESTOR_OID); - cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0); - git_oid_fromstr(&oid, ONE_OUR_OID); - cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0); - git_oid_fromstr(&oid, ONE_THEIR_OID); - cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0); -} - -void test_index_reuc__ignore_case(void) -{ - const git_index_reuc_entry *reuc; - git_oid oid; - int index_caps; - - index_caps = git_index_caps(repo_index); - - index_caps &= ~GIT_INDEXCAP_IGNORE_CASE; - cl_git_pass(git_index_set_caps(repo_index, index_caps)); - - cl_assert(!git_index_reuc_get_bypath(repo_index, "TWO.txt")); - - index_caps |= GIT_INDEXCAP_IGNORE_CASE; - cl_git_pass(git_index_set_caps(repo_index, index_caps)); - - cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index)); - - cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "TWO.txt")); - - cl_assert_equal_s("two.txt", reuc->path); - cl_assert(reuc->mode[0] == 0100644); - cl_assert(reuc->mode[1] == 0100644); - cl_assert(reuc->mode[2] == 0100644); - git_oid_fromstr(&oid, TWO_ANCESTOR_OID); - cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0); - git_oid_fromstr(&oid, TWO_OUR_OID); - cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0); - git_oid_fromstr(&oid, TWO_THEIR_OID); - cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0); -} - -void test_index_reuc__read_byindex(void) -{ - const git_index_reuc_entry *reuc; - git_oid oid; - - cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index)); - - cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0)); - - cl_assert_equal_s("one.txt", reuc->path); - cl_assert(reuc->mode[0] == 0100644); - cl_assert(reuc->mode[1] == 0100644); - cl_assert(reuc->mode[2] == 0100644); - git_oid_fromstr(&oid, ONE_ANCESTOR_OID); - cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0); - git_oid_fromstr(&oid, ONE_OUR_OID); - cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0); - git_oid_fromstr(&oid, ONE_THEIR_OID); - cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0); - - cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 1)); - - cl_assert_equal_s("two.txt", reuc->path); - cl_assert(reuc->mode[0] == 0100644); - cl_assert(reuc->mode[1] == 0100644); - cl_assert(reuc->mode[2] == 0100644); - git_oid_fromstr(&oid, TWO_ANCESTOR_OID); - cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0); - git_oid_fromstr(&oid, TWO_OUR_OID); - cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0); - git_oid_fromstr(&oid, TWO_THEIR_OID); - cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0); -} - -void test_index_reuc__updates_existing(void) -{ - const git_index_reuc_entry *reuc; - git_oid ancestor_oid, our_oid, their_oid, oid; - int index_caps; - - git_index_clear(repo_index); - - index_caps = git_index_caps(repo_index); - - index_caps |= GIT_INDEXCAP_IGNORE_CASE; - cl_git_pass(git_index_set_caps(repo_index, index_caps)); - - git_oid_fromstr(&ancestor_oid, TWO_ANCESTOR_OID); - git_oid_fromstr(&our_oid, TWO_OUR_OID); - git_oid_fromstr(&their_oid, TWO_THEIR_OID); - - cl_git_pass(git_index_reuc_add(repo_index, "two.txt", - 0100644, &ancestor_oid, - 0100644, &our_oid, - 0100644, &their_oid)); - - cl_git_pass(git_index_reuc_add(repo_index, "TWO.txt", - 0100644, &our_oid, - 0100644, &their_oid, - 0100644, &ancestor_oid)); - - cl_assert_equal_i(1, git_index_reuc_entrycount(repo_index)); - - cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0)); - - cl_assert_equal_s("TWO.txt", reuc->path); - git_oid_fromstr(&oid, TWO_OUR_OID); - cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0); - git_oid_fromstr(&oid, TWO_THEIR_OID); - cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0); - git_oid_fromstr(&oid, TWO_ANCESTOR_OID); - cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0); -} - -void test_index_reuc__remove(void) -{ - git_oid oid; - const git_index_reuc_entry *reuc; - - cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index)); - - cl_git_pass(git_index_reuc_remove(repo_index, 0)); - cl_git_fail(git_index_reuc_remove(repo_index, 1)); - - cl_assert_equal_i(1, git_index_reuc_entrycount(repo_index)); - - cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0)); - - cl_assert_equal_s("two.txt", reuc->path); - cl_assert(reuc->mode[0] == 0100644); - cl_assert(reuc->mode[1] == 0100644); - cl_assert(reuc->mode[2] == 0100644); - git_oid_fromstr(&oid, TWO_ANCESTOR_OID); - cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0); - git_oid_fromstr(&oid, TWO_OUR_OID); - cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0); - git_oid_fromstr(&oid, TWO_THEIR_OID); - cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0); -} - -void test_index_reuc__write(void) -{ - git_oid ancestor_oid, our_oid, their_oid; - const git_index_reuc_entry *reuc; - - git_index_clear(repo_index); - - /* Write out of order to ensure sorting is correct */ - git_oid_fromstr(&ancestor_oid, TWO_ANCESTOR_OID); - git_oid_fromstr(&our_oid, TWO_OUR_OID); - git_oid_fromstr(&their_oid, TWO_THEIR_OID); - - cl_git_pass(git_index_reuc_add(repo_index, "two.txt", - 0100644, &ancestor_oid, - 0100644, &our_oid, - 0100644, &their_oid)); - - git_oid_fromstr(&ancestor_oid, ONE_ANCESTOR_OID); - git_oid_fromstr(&our_oid, ONE_OUR_OID); - git_oid_fromstr(&their_oid, ONE_THEIR_OID); - - cl_git_pass(git_index_reuc_add(repo_index, "one.txt", - 0100644, &ancestor_oid, - 0100644, &our_oid, - 0100644, &their_oid)); - - cl_git_pass(git_index_write(repo_index)); - - cl_git_pass(git_index_read(repo_index)); - cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index)); - - /* ensure sort order was round-tripped correct */ - cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0)); - cl_assert_equal_s("one.txt", reuc->path); - - cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 1)); - cl_assert_equal_s("two.txt", reuc->path); -} - diff --git a/tests-clar/index/tests.c b/tests-clar/index/tests.c deleted file mode 100644 index 64f547ead16..00000000000 --- a/tests-clar/index/tests.c +++ /dev/null @@ -1,376 +0,0 @@ -#include "clar_libgit2.h" -#include "index.h" - -static const size_t index_entry_count = 109; -static const size_t index_entry_count_2 = 1437; -#define TEST_INDEX_PATH cl_fixture("testrepo.git/index") -#define TEST_INDEX2_PATH cl_fixture("gitgit.index") -#define TEST_INDEXBIG_PATH cl_fixture("big.index") - - -// Suite data -struct test_entry { - size_t index; - char path[128]; - git_off_t file_size; - git_time_t mtime; -}; - -static struct test_entry test_entries[] = { - {4, "Makefile", 5064, 0x4C3F7F33}, - {62, "tests/Makefile", 2631, 0x4C3F7F33}, - {36, "src/index.c", 10014, 0x4C43368D}, - {6, "git.git-authors", 2709, 0x4C3F7F33}, - {48, "src/revobject.h", 1448, 0x4C3F7FE2} -}; - -// Helpers -static void copy_file(const char *src, const char *dst) -{ - git_buf source_buf = GIT_BUF_INIT; - git_file dst_fd; - - cl_git_pass(git_futils_readbuffer(&source_buf, src)); - - dst_fd = git_futils_creat_withpath(dst, 0777, 0666); //-V536 - if (dst_fd < 0) - goto cleanup; - - cl_git_pass(p_write(dst_fd, source_buf.ptr, source_buf.size)); - -cleanup: - git_buf_free(&source_buf); - p_close(dst_fd); -} - -static void files_are_equal(const char *a, const char *b) -{ - git_buf buf_a = GIT_BUF_INIT; - git_buf buf_b = GIT_BUF_INIT; - int pass; - - if (git_futils_readbuffer(&buf_a, a) < 0) - cl_assert(0); - - if (git_futils_readbuffer(&buf_b, b) < 0) { - git_buf_free(&buf_a); - cl_assert(0); - } - - pass = (buf_a.size == buf_b.size && !memcmp(buf_a.ptr, buf_b.ptr, buf_a.size)); - - git_buf_free(&buf_a); - git_buf_free(&buf_b); - - cl_assert(pass); -} - - -// Fixture setup and teardown -void test_index_tests__initialize(void) -{ -} - -void test_index_tests__empty_index(void) -{ - git_index *index; - - cl_git_pass(git_index_open(&index, "in-memory-index")); - cl_assert(index->on_disk == 0); - - cl_assert(git_index_entrycount(index) == 0); - cl_assert(index->entries.sorted); - - git_index_free(index); -} - -void test_index_tests__default_test_index(void) -{ - git_index *index; - unsigned int i; - git_index_entry **entries; - - cl_git_pass(git_index_open(&index, TEST_INDEX_PATH)); - cl_assert(index->on_disk); - - cl_assert(git_index_entrycount(index) == index_entry_count); - cl_assert(index->entries.sorted); - - entries = (git_index_entry **)index->entries.contents; - - for (i = 0; i < ARRAY_SIZE(test_entries); ++i) { - git_index_entry *e = entries[test_entries[i].index]; - - cl_assert_equal_s(e->path, test_entries[i].path); - cl_assert(e->mtime.seconds == test_entries[i].mtime); - cl_assert(e->file_size == test_entries[i].file_size); - } - - git_index_free(index); -} - -void test_index_tests__gitgit_index(void) -{ - git_index *index; - - cl_git_pass(git_index_open(&index, TEST_INDEX2_PATH)); - cl_assert(index->on_disk); - - cl_assert(git_index_entrycount(index) == index_entry_count_2); - cl_assert(index->entries.sorted); - cl_assert(index->tree != NULL); - - git_index_free(index); -} - -void test_index_tests__find_in_existing(void) -{ - git_index *index; - unsigned int i; - - cl_git_pass(git_index_open(&index, TEST_INDEX_PATH)); - - for (i = 0; i < ARRAY_SIZE(test_entries); ++i) { - size_t idx; - - cl_assert(!git_index_find(&idx, index, test_entries[i].path)); - cl_assert(idx == test_entries[i].index); - } - - git_index_free(index); -} - -void test_index_tests__find_in_empty(void) -{ - git_index *index; - unsigned int i; - - cl_git_pass(git_index_open(&index, "fake-index")); - - for (i = 0; i < ARRAY_SIZE(test_entries); ++i) { - cl_assert(GIT_ENOTFOUND == git_index_find(NULL, index, test_entries[i].path)); - } - - git_index_free(index); -} - -void test_index_tests__write(void) -{ - git_index *index; - - copy_file(TEST_INDEXBIG_PATH, "index_rewrite"); - - cl_git_pass(git_index_open(&index, "index_rewrite")); - cl_assert(index->on_disk); - - cl_git_pass(git_index_write(index)); - files_are_equal(TEST_INDEXBIG_PATH, "index_rewrite"); - - git_index_free(index); - - p_unlink("index_rewrite"); -} - -void test_index_tests__sort0(void) -{ - // sort the entires in an index - /* - * TODO: This no longer applies: - * index sorting in Git uses some specific changes to the way - * directories are sorted. - * - * We need to specificially check for this by creating a new - * index, adding entries in random order and then - * checking for consistency - */ -} - -void test_index_tests__sort1(void) -{ - // sort the entires in an empty index - git_index *index; - - cl_git_pass(git_index_open(&index, "fake-index")); - - /* FIXME: this test is slightly dumb */ - cl_assert(index->entries.sorted); - - git_index_free(index); -} - -static void cleanup_myrepo(void *opaque) -{ - GIT_UNUSED(opaque); - cl_fixture_cleanup("myrepo"); -} - -void test_index_tests__add(void) -{ - git_index *index; - git_filebuf file = GIT_FILEBUF_INIT; - git_repository *repo; - const git_index_entry *entry; - git_oid id1; - - cl_set_cleanup(&cleanup_myrepo, NULL); - - /* Intialize a new repository */ - cl_git_pass(git_repository_init(&repo, "./myrepo", 0)); - - /* Ensure we're the only guy in the room */ - cl_git_pass(git_repository_index(&index, repo)); - cl_assert(git_index_entrycount(index) == 0); - - /* Create a new file in the working directory */ - cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777)); - cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0)); - cl_git_pass(git_filebuf_write(&file, "hey there\n", 10)); - cl_git_pass(git_filebuf_commit(&file, 0666)); - - /* Store the expected hash of the file/blob - * This has been generated by executing the following - * $ echo "hey there" | git hash-object --stdin - */ - cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6")); - - /* Add the new file to the index */ - cl_git_pass(git_index_add_bypath(index, "test.txt")); - - /* Wow... it worked! */ - cl_assert(git_index_entrycount(index) == 1); - entry = git_index_get_byindex(index, 0); - - /* And the built-in hashing mechanism worked as expected */ - cl_assert(git_oid_cmp(&id1, &entry->oid) == 0); - - /* Test access by path instead of index */ - cl_assert((entry = git_index_get_bypath(index, "test.txt", 0)) != NULL); - cl_assert(git_oid_cmp(&id1, &entry->oid) == 0); - - git_index_free(index); - git_repository_free(repo); -} - -void test_index_tests__add_bypath_to_a_bare_repository_returns_EBAREPO(void) -{ - git_repository *bare_repo; - git_index *index; - - cl_git_pass(git_repository_open(&bare_repo, cl_fixture("testrepo.git"))); - cl_git_pass(git_repository_index(&index, bare_repo)); - - cl_assert_equal_i(GIT_EBAREREPO, git_index_add_bypath(index, "test.txt")); - - git_index_free(index); - git_repository_free(bare_repo); -} - -/* Test that writing an invalid filename fails */ -void test_index_tests__write_invalid_filename(void) -{ - git_repository *repo; - git_index *index; - git_oid expected; - - p_mkdir("read_tree", 0700); - - cl_git_pass(git_repository_init(&repo, "./read_tree", 0)); - cl_git_pass(git_repository_index(&index, repo)); - - cl_assert(git_index_entrycount(index) == 0); - - cl_git_mkfile("./read_tree/.git/hello", NULL); - - cl_git_pass(git_index_add_bypath(index, ".git/hello")); - - /* write-tree */ - cl_git_fail(git_index_write_tree(&expected, index)); - - git_index_free(index); - git_repository_free(repo); - - cl_fixture_cleanup("read_tree"); -} - -void test_index_tests__remove_entry(void) -{ - git_repository *repo; - git_index *index; - - p_mkdir("index_test", 0770); - - cl_git_pass(git_repository_init(&repo, "index_test", 0)); - cl_git_pass(git_repository_index(&index, repo)); - cl_assert(git_index_entrycount(index) == 0); - - cl_git_mkfile("index_test/hello", NULL); - cl_git_pass(git_index_add_bypath(index, "hello")); - cl_git_pass(git_index_write(index)); - - cl_git_pass(git_index_read(index)); /* reload */ - cl_assert(git_index_entrycount(index) == 1); - cl_assert(git_index_get_bypath(index, "hello", 0) != NULL); - - cl_git_pass(git_index_remove(index, "hello", 0)); - cl_git_pass(git_index_write(index)); - - cl_git_pass(git_index_read(index)); /* reload */ - cl_assert(git_index_entrycount(index) == 0); - cl_assert(git_index_get_bypath(index, "hello", 0) == NULL); - - git_index_free(index); - git_repository_free(repo); - cl_fixture_cleanup("index_test"); -} - -void test_index_tests__remove_directory(void) -{ - git_repository *repo; - git_index *index; - - p_mkdir("index_test", 0770); - - cl_git_pass(git_repository_init(&repo, "index_test", 0)); - cl_git_pass(git_repository_index(&index, repo)); - cl_assert_equal_i(0, (int)git_index_entrycount(index)); - - p_mkdir("index_test/a", 0770); - cl_git_mkfile("index_test/a/1.txt", NULL); - cl_git_mkfile("index_test/a/2.txt", NULL); - cl_git_mkfile("index_test/a/3.txt", NULL); - cl_git_mkfile("index_test/b.txt", NULL); - - cl_git_pass(git_index_add_bypath(index, "a/1.txt")); - cl_git_pass(git_index_add_bypath(index, "a/2.txt")); - cl_git_pass(git_index_add_bypath(index, "a/3.txt")); - cl_git_pass(git_index_add_bypath(index, "b.txt")); - cl_git_pass(git_index_write(index)); - - cl_git_pass(git_index_read(index)); /* reload */ - cl_assert_equal_i(4, (int)git_index_entrycount(index)); - cl_assert(git_index_get_bypath(index, "a/1.txt", 0) != NULL); - cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL); - cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL); - - cl_git_pass(git_index_remove(index, "a/1.txt", 0)); - cl_git_pass(git_index_write(index)); - - cl_git_pass(git_index_read(index)); /* reload */ - cl_assert_equal_i(3, (int)git_index_entrycount(index)); - cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL); - cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL); - cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL); - - cl_git_pass(git_index_remove_directory(index, "a", 0)); - cl_git_pass(git_index_write(index)); - - cl_git_pass(git_index_read(index)); /* reload */ - cl_assert_equal_i(1, (int)git_index_entrycount(index)); - cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL); - cl_assert(git_index_get_bypath(index, "a/2.txt", 0) == NULL); - cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL); - - git_index_free(index); - git_repository_free(repo); - cl_fixture_cleanup("index_test"); -} diff --git a/tests-clar/main.c b/tests-clar/main.c deleted file mode 100644 index 6b498939d7f..00000000000 --- a/tests-clar/main.c +++ /dev/null @@ -1,20 +0,0 @@ -#include "clar_libgit2.h" - -#ifdef _WIN32 -int __cdecl main(int argc, char *argv[]) -#else -int main(int argc, char *argv[]) -#endif -{ - int res; - - git_threads_init(); - - /* Run the test suite */ - res = clar_test(argc, argv); - - giterr_clear(); - git_threads_shutdown(); - - return res; -} diff --git a/tests-clar/merge/setup.c b/tests-clar/merge/setup.c deleted file mode 100644 index 946c67e7bc1..00000000000 --- a/tests-clar/merge/setup.c +++ /dev/null @@ -1,139 +0,0 @@ -#include "clar_libgit2.h" -#include "git2/repository.h" -#include "git2/merge.h" -#include "merge.h" -#include "refs.h" -#include "fileops.h" - -static git_repository *repo; -static git_index *repo_index; - -#define TEST_REPO_PATH "testrepo" -#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" - -#define ORIG_HEAD "bd593285fc7fe4ca18ccdbabf027f5d689101452" - -#define THEIRS_SIMPLE_BRANCH "branch" -#define THEIRS_SIMPLE_OID "7cb63eed597130ba4abb87b3e544b85021905520" - -#define OCTO1_BRANCH "octo1" -#define OCTO1_OID "16f825815cfd20a07a75c71554e82d8eede0b061" - -#define OCTO2_BRANCH "octo2" -#define OCTO2_OID "158dc7bedb202f5b26502bf3574faa7f4238d56c" - -#define OCTO3_BRANCH "octo3" -#define OCTO3_OID "50ce7d7d01217679e26c55939eef119e0c93e272" - -#define OCTO4_BRANCH "octo4" -#define OCTO4_OID "54269b3f6ec3d7d4ede24dd350dd5d605495c3ae" - -#define OCTO5_BRANCH "octo5" -#define OCTO5_OID "e4f618a2c3ed0669308735727df5ebf2447f022f" - -// Fixture setup and teardown -void test_merge_setup__initialize(void) -{ - repo = cl_git_sandbox_init(TEST_REPO_PATH); - git_repository_index(&repo_index, repo); -} - -void test_merge_setup__cleanup(void) -{ - git_index_free(repo_index); - cl_git_sandbox_cleanup(); -} - -static void write_file_contents(const char *filename, const char *output) -{ - git_buf file_path_buf = GIT_BUF_INIT; - - git_buf_printf(&file_path_buf, "%s/%s", git_repository_path(repo), filename); - cl_git_rewritefile(file_path_buf.ptr, output); - - git_buf_free(&file_path_buf); -} - -struct merge_head_cb_data { - const char **oid_str; - unsigned int len; - - unsigned int i; -}; - -static int merge_head_foreach_cb(const git_oid *oid, void *payload) -{ - git_oid expected_oid; - struct merge_head_cb_data *cb_data = payload; - - git_oid_fromstr(&expected_oid, cb_data->oid_str[cb_data->i]); - cl_assert(git_oid_cmp(&expected_oid, oid) == 0); - cb_data->i++; - return 0; -} - -void test_merge_setup__head_notfound(void) -{ - int error; - - cl_git_fail((error = git_repository_mergehead_foreach(repo, - merge_head_foreach_cb, NULL))); - cl_assert(error == GIT_ENOTFOUND); -} - -void test_merge_setup__head_invalid_oid(void) -{ - int error; - - write_file_contents(GIT_MERGE_HEAD_FILE, "invalid-oid\n"); - - cl_git_fail((error = git_repository_mergehead_foreach(repo, - merge_head_foreach_cb, NULL))); - cl_assert(error == -1); -} - -void test_merge_setup__head_foreach_nonewline(void) -{ - int error; - - write_file_contents(GIT_MERGE_HEAD_FILE, THEIRS_SIMPLE_OID); - - cl_git_fail((error = git_repository_mergehead_foreach(repo, - merge_head_foreach_cb, NULL))); - cl_assert(error == -1); -} - -void test_merge_setup__head_foreach_one(void) -{ - const char *expected = THEIRS_SIMPLE_OID; - - struct merge_head_cb_data cb_data = { &expected, 1 }; - - write_file_contents(GIT_MERGE_HEAD_FILE, THEIRS_SIMPLE_OID "\n"); - - cl_git_pass(git_repository_mergehead_foreach(repo, - merge_head_foreach_cb, &cb_data)); - - cl_assert(cb_data.i == cb_data.len); -} - -void test_merge_setup__head_foreach_octopus(void) -{ - const char *expected[] = { THEIRS_SIMPLE_OID, - OCTO1_OID, OCTO2_OID, OCTO3_OID, OCTO4_OID, OCTO5_OID }; - - struct merge_head_cb_data cb_data = { expected, 6 }; - - write_file_contents(GIT_MERGE_HEAD_FILE, - THEIRS_SIMPLE_OID "\n" - OCTO1_OID "\n" - OCTO2_OID "\n" - OCTO3_OID "\n" - OCTO4_OID "\n" - OCTO5_OID "\n"); - - cl_git_pass(git_repository_mergehead_foreach(repo, - merge_head_foreach_cb, &cb_data)); - - cl_assert(cb_data.i == cb_data.len); -} diff --git a/tests-clar/network/createremotethenload.c b/tests-clar/network/createremotethenload.c deleted file mode 100644 index b64c2ccc461..00000000000 --- a/tests-clar/network/createremotethenload.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "clar_libgit2.h" - -static git_remote *_remote; -static git_repository *_repo; -static git_config *_config; -static char url[] = "http://github.com/libgit2/libgit2.git"; - -void test_network_createremotethenload__initialize(void) -{ - cl_fixture_sandbox("testrepo.git"); - - cl_git_pass(git_repository_open(&_repo, "testrepo.git")); - - cl_git_pass(git_repository_config(&_config, _repo)); - cl_git_pass(git_config_set_string(_config, "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")); - cl_git_pass(git_config_set_string(_config, "remote.origin.url", url)); - git_config_free(_config); - - cl_git_pass(git_remote_load(&_remote, _repo, "origin")); -} - -void test_network_createremotethenload__cleanup(void) -{ - git_remote_free(_remote); - _remote = NULL; - - git_repository_free(_repo); - _repo = NULL; - - cl_fixture_cleanup("testrepo.git"); -} - -void test_network_createremotethenload__parsing(void) -{ - cl_assert_equal_s(git_remote_name(_remote), "origin"); - cl_assert_equal_s(git_remote_url(_remote), url); -} diff --git a/tests-clar/network/cred.c b/tests-clar/network/cred.c deleted file mode 100644 index b7f45c23b96..00000000000 --- a/tests-clar/network/cred.c +++ /dev/null @@ -1,27 +0,0 @@ -#include "clar_libgit2.h" - -#include "git2/cred_helpers.h" - -void test_network_cred__stock_userpass_validates_args(void) -{ - git_cred_userpass_payload payload = {0}; - - cl_git_fail(git_cred_userpass(NULL, NULL, 0, NULL)); - - payload.username = "user"; - cl_git_fail(git_cred_userpass(NULL, NULL, 0, &payload)); - - payload.username = NULL; - payload.username = "pass"; - cl_git_fail(git_cred_userpass(NULL, NULL, 0, &payload)); -} - -void test_network_cred__stock_userpass_validates_that_method_is_allowed(void) -{ - git_cred *cred; - git_cred_userpass_payload payload = {"user", "pass"}; - - cl_git_fail(git_cred_userpass(&cred, NULL, 0, &payload)); - cl_git_pass(git_cred_userpass(&cred, NULL, GIT_CREDTYPE_USERPASS_PLAINTEXT, &payload)); - cred->free(cred); -} diff --git a/tests-clar/network/fetchlocal.c b/tests-clar/network/fetchlocal.c deleted file mode 100644 index ee3bd9db30b..00000000000 --- a/tests-clar/network/fetchlocal.c +++ /dev/null @@ -1,77 +0,0 @@ -#include "clar_libgit2.h" - -#include "buffer.h" -#include "path.h" -#include "remote.h" - -static void transfer_cb(const git_transfer_progress *stats, void *payload) -{ - int *callcount = (int*)payload; - GIT_UNUSED(stats); - (*callcount)++; -} - -static void cleanup_local_repo(void *path) -{ - cl_fixture_cleanup((char *)path); -} - -void test_network_fetchlocal__complete(void) -{ - git_repository *repo; - git_remote *origin; - int callcount = 0; - git_strarray refnames = {0}; - - const char *url = cl_git_fixture_url("testrepo.git"); - - cl_set_cleanup(&cleanup_local_repo, "foo"); - cl_git_pass(git_repository_init(&repo, "foo", true)); - - cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); - cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH)); - cl_git_pass(git_remote_download(origin, transfer_cb, &callcount)); - cl_git_pass(git_remote_update_tips(origin)); - - cl_git_pass(git_reference_list(&refnames, repo, GIT_REF_LISTALL)); - cl_assert_equal_i(18, (int)refnames.count); - cl_assert(callcount > 0); - - git_strarray_free(&refnames); - git_remote_free(origin); - git_repository_free(repo); -} - -static void cleanup_sandbox(void *unused) -{ - GIT_UNUSED(unused); - cl_git_sandbox_cleanup(); -} - -void test_network_fetchlocal__partial(void) -{ - git_repository *repo = cl_git_sandbox_init("partial-testrepo"); - git_remote *origin; - int callcount = 0; - git_strarray refnames = {0}; - const char *url; - - cl_set_cleanup(&cleanup_sandbox, NULL); - cl_git_pass(git_reference_list(&refnames, repo, GIT_REF_LISTALL)); - cl_assert_equal_i(1, (int)refnames.count); - - url = cl_git_fixture_url("testrepo.git"); - cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); - cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH)); - cl_git_pass(git_remote_download(origin, transfer_cb, &callcount)); - cl_git_pass(git_remote_update_tips(origin)); - - git_strarray_free(&refnames); - - cl_git_pass(git_reference_list(&refnames, repo, GIT_REF_LISTALL)); - cl_assert_equal_i(19, (int)refnames.count); /* 18 remote + 1 local */ - cl_assert(callcount > 0); - - git_strarray_free(&refnames); - git_remote_free(origin); -} diff --git a/tests-clar/network/refspecs.c b/tests-clar/network/refspecs.c deleted file mode 100644 index b3d80fb858b..00000000000 --- a/tests-clar/network/refspecs.c +++ /dev/null @@ -1,84 +0,0 @@ -#include "clar_libgit2.h" -#include "refspec.h" -#include "remote.h" - -static void assert_refspec(unsigned int direction, const char *input, bool is_expected_to_be_valid) -{ - git_refspec refspec; - int error; - - error = git_refspec__parse(&refspec, input, direction == GIT_DIRECTION_FETCH); - git_refspec__free(&refspec); - - if (is_expected_to_be_valid) - cl_assert_equal_i(0, error); - else - cl_assert_equal_i(GIT_ERROR, error); -} - -void test_network_refspecs__parsing(void) -{ - // Ported from https://github.com/git/git/blob/abd2bde78bd994166900290434a2048e660dabed/t/t5511-refspec.sh - - assert_refspec(GIT_DIRECTION_PUSH, "", false); - assert_refspec(GIT_DIRECTION_PUSH, ":", true); - assert_refspec(GIT_DIRECTION_PUSH, "::", false); - assert_refspec(GIT_DIRECTION_PUSH, "+:", true); - - assert_refspec(GIT_DIRECTION_FETCH, "", true); - assert_refspec(GIT_DIRECTION_PUSH, ":", true); - assert_refspec(GIT_DIRECTION_FETCH, "::", false); - - assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*:refs/remotes/frotz/*", true); - assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*:refs/remotes/frotz", false); - assert_refspec(GIT_DIRECTION_PUSH, "refs/heads:refs/remotes/frotz/*", false); - assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/master:refs/remotes/frotz/xyzzy", true); - - /* - * These have invalid LHS, but we do not have a formal "valid sha-1 - * expression syntax checker" so they are not checked with the current - * code. They will be caught downstream anyway, but we may want to - * have tighter check later... - */ - //assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/master::refs/remotes/frotz/xyzzy", false); - //assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/maste :refs/remotes/frotz/xyzzy", false); - - assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*:refs/remotes/frotz/*", true); - assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*:refs/remotes/frotz", false); - assert_refspec(GIT_DIRECTION_FETCH, "refs/heads:refs/remotes/frotz/*", false); - assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/master:refs/remotes/frotz/xyzzy", true); - assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/master::refs/remotes/frotz/xyzzy", false); - assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/maste :refs/remotes/frotz/xyzzy", false); - - assert_refspec(GIT_DIRECTION_PUSH, "master~1:refs/remotes/frotz/backup", true); - assert_refspec(GIT_DIRECTION_FETCH, "master~1:refs/remotes/frotz/backup", false); - assert_refspec(GIT_DIRECTION_PUSH, "HEAD~4:refs/remotes/frotz/new", true); - assert_refspec(GIT_DIRECTION_FETCH, "HEAD~4:refs/remotes/frotz/new", false); - - assert_refspec(GIT_DIRECTION_PUSH, "HEAD", true); - assert_refspec(GIT_DIRECTION_FETCH, "HEAD", true); - assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/ nitfol", false); - assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/ nitfol", false); - - assert_refspec(GIT_DIRECTION_PUSH, "HEAD:", false); - assert_refspec(GIT_DIRECTION_FETCH, "HEAD:", true); - assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/ nitfol:", false); - assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/ nitfol:", false); - - assert_refspec(GIT_DIRECTION_PUSH, ":refs/remotes/frotz/deleteme", true); - assert_refspec(GIT_DIRECTION_FETCH, ":refs/remotes/frotz/HEAD-to-me", true); - assert_refspec(GIT_DIRECTION_PUSH, ":refs/remotes/frotz/delete me", false); - assert_refspec(GIT_DIRECTION_FETCH, ":refs/remotes/frotz/HEAD to me", false); - - assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/for-linus:refs/remotes/mine/*-blah", false); - assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/for-linus:refs/remotes/mine/*-blah", false); - - assert_refspec(GIT_DIRECTION_FETCH, "refs/heads*/for-linus:refs/remotes/mine/*", false); - assert_refspec(GIT_DIRECTION_PUSH, "refs/heads*/for-linus:refs/remotes/mine/*", false); - - assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/*/for-linus:refs/remotes/mine/*", false); - assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/*/for-linus:refs/remotes/mine/*", false); - - assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/for-linus:refs/remotes/mine/*", true); - assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/for-linus:refs/remotes/mine/*", true); -} diff --git a/tests-clar/network/remotelocal.c b/tests-clar/network/remotelocal.c deleted file mode 100644 index 5d6a16a2a11..00000000000 --- a/tests-clar/network/remotelocal.c +++ /dev/null @@ -1,102 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "path.h" -#include "posix.h" - -static git_repository *repo; -static git_buf file_path_buf = GIT_BUF_INIT; -static git_remote *remote; - -void test_network_remotelocal__initialize(void) -{ - cl_git_pass(git_repository_init(&repo, "remotelocal/", 0)); - cl_assert(repo != NULL); -} - -void test_network_remotelocal__cleanup(void) -{ - git_buf_free(&file_path_buf); - - git_remote_free(remote); - remote = NULL; - - git_repository_free(repo); - repo = NULL; - - cl_fixture_cleanup("remotelocal"); -} - -static int count_ref__cb(git_remote_head *head, void *payload) -{ - int *count = (int *)payload; - - (void)head; - (*count)++; - - return 0; -} - -static int ensure_peeled__cb(git_remote_head *head, void *payload) -{ - GIT_UNUSED(payload); - - if(strcmp(head->name, "refs/tags/test^{}") != 0) - return 0; - - return git_oid_streq(&head->oid, "e90810b8df3e80c413d903f631643c716887138d"); -} - -static void connect_to_local_repository(const char *local_repository) -{ - git_buf_sets(&file_path_buf, cl_git_path_url(local_repository)); - - cl_git_pass(git_remote_create_inmemory(&remote, repo, NULL, git_buf_cstr(&file_path_buf))); - cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH)); - -} - -void test_network_remotelocal__connected(void) -{ - connect_to_local_repository(cl_fixture("testrepo.git")); - cl_assert(git_remote_connected(remote)); - - git_remote_disconnect(remote); - cl_assert(!git_remote_connected(remote)); -} - -void test_network_remotelocal__retrieve_advertised_references(void) -{ - int how_many_refs = 0; - - connect_to_local_repository(cl_fixture("testrepo.git")); - - cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs)); - - cl_assert_equal_i(how_many_refs, 26); -} - -void test_network_remotelocal__retrieve_advertised_references_from_spaced_repository(void) -{ - int how_many_refs = 0; - - cl_fixture_sandbox("testrepo.git"); - cl_git_pass(p_rename("testrepo.git", "spaced testrepo.git")); - - connect_to_local_repository("spaced testrepo.git"); - - cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs)); - - cl_assert_equal_i(how_many_refs, 26); - - git_remote_free(remote); /* Disconnect from the "spaced repo" before the cleanup */ - remote = NULL; - - cl_fixture_cleanup("spaced testrepo.git"); -} - -void test_network_remotelocal__nested_tags_are_completely_peeled(void) -{ - connect_to_local_repository(cl_fixture("testrepo.git")); - - cl_git_pass(git_remote_ls(remote, &ensure_peeled__cb, NULL)); -} diff --git a/tests-clar/network/remoterename.c b/tests-clar/network/remoterename.c deleted file mode 100644 index 24cfadcc3d7..00000000000 --- a/tests-clar/network/remoterename.c +++ /dev/null @@ -1,174 +0,0 @@ -#include "clar_libgit2.h" -#include "config/config_helpers.h" - -#include "repository.h" - -static git_remote *_remote; -static git_repository *_repo; - -void test_network_remoterename__initialize(void) -{ - _repo = cl_git_sandbox_init("testrepo.git"); - - cl_git_pass(git_remote_load(&_remote, _repo, "test")); -} - -void test_network_remoterename__cleanup(void) -{ - git_remote_free(_remote); - _remote = NULL; - - cl_git_sandbox_cleanup(); -} - -static int dont_call_me_cb(const char *fetch_refspec, void *payload) -{ - GIT_UNUSED(fetch_refspec); - GIT_UNUSED(payload); - - cl_assert(false); - - return -1; -} - -void test_network_remoterename__renaming_a_remote_moves_related_configuration_section(void) -{ - assert_config_entry_existence(_repo, "remote.test.fetch", true); - assert_config_entry_existence(_repo, "remote.just/renamed.fetch", false); - - cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL)); - - assert_config_entry_existence(_repo, "remote.test.fetch", false); - assert_config_entry_existence(_repo, "remote.just/renamed.fetch", true); -} - -void test_network_remoterename__renaming_a_remote_updates_branch_related_configuration_entries(void) -{ - assert_config_entry_value(_repo, "branch.master.remote", "test"); - - cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL)); - - assert_config_entry_value(_repo, "branch.master.remote", "just/renamed"); -} - -void test_network_remoterename__renaming_a_remote_updates_default_fetchrefspec(void) -{ - cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL)); - - assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/heads/*:refs/remotes/just/renamed/*"); -} - -void test_network_remoterename__renaming_a_remote_without_a_fetchrefspec_doesnt_create_one(void) -{ - git_config *config; - - git_remote_free(_remote); - cl_git_pass(git_repository_config__weakptr(&config, _repo)); - cl_git_pass(git_config_delete_entry(config, "remote.test.fetch")); - - cl_git_pass(git_remote_load(&_remote, _repo, "test")); - - assert_config_entry_existence(_repo, "remote.test.fetch", false); - - cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL)); - - assert_config_entry_existence(_repo, "remote.just/renamed.fetch", false); -} - -static int ensure_refspecs(const char* refspec_name, void *payload) -{ - int i = 0; - bool found = false; - const char ** exp = (const char **)payload; - - while (exp[i]) { - if (strcmp(exp[i++], refspec_name)) - continue; - - found = true; - break; - } - - cl_assert(found); - - return 0; -} - -void test_network_remoterename__renaming_a_remote_notifies_of_non_default_fetchrefspec(void) -{ - git_config *config; - - char *expected_refspecs[] = { - "+refs/*:refs/*", - NULL - }; - - git_remote_free(_remote); - cl_git_pass(git_repository_config__weakptr(&config, _repo)); - cl_git_pass(git_config_set_string(config, "remote.test.fetch", "+refs/*:refs/*")); - cl_git_pass(git_remote_load(&_remote, _repo, "test")); - - cl_git_pass(git_remote_rename(_remote, "just/renamed", ensure_refspecs, &expected_refspecs)); - - assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/*:refs/*"); -} - -void test_network_remoterename__new_name_can_contain_dots(void) -{ - cl_git_pass(git_remote_rename(_remote, "just.renamed", dont_call_me_cb, NULL)); - cl_assert_equal_s("just.renamed", git_remote_name(_remote)); -} - -void test_network_remoterename__new_name_must_conform_to_reference_naming_conventions(void) -{ - cl_assert_equal_i( - GIT_EINVALIDSPEC, - git_remote_rename(_remote, "new@{name", dont_call_me_cb, NULL)); -} - -void test_network_remoterename__renamed_name_is_persisted(void) -{ - git_remote *renamed; - git_repository *another_repo; - - cl_git_fail(git_remote_load(&renamed, _repo, "just/renamed")); - - cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL)); - - cl_git_pass(git_repository_open(&another_repo, "testrepo.git")); - cl_git_pass(git_remote_load(&renamed, _repo, "just/renamed")); - - git_remote_free(renamed); - git_repository_free(another_repo); -} - -void test_network_remoterename__cannot_overwrite_an_existing_remote(void) -{ - cl_assert_equal_i(GIT_EEXISTS, git_remote_rename(_remote, "test", dont_call_me_cb, NULL)); - cl_assert_equal_i(GIT_EEXISTS, git_remote_rename(_remote, "test_with_pushurl", dont_call_me_cb, NULL)); -} - -void test_network_remoterename__renaming_a_remote_moves_the_underlying_reference(void) -{ - git_reference *underlying; - - cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&underlying, _repo, "refs/remotes/just/renamed")); - cl_git_pass(git_reference_lookup(&underlying, _repo, "refs/remotes/test/master")); - git_reference_free(underlying); - - cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL)); - - cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&underlying, _repo, "refs/remotes/test/master")); - cl_git_pass(git_reference_lookup(&underlying, _repo, "refs/remotes/just/renamed/master")); - git_reference_free(underlying); -} - -void test_network_remoterename__cannot_rename_an_inmemory_remote(void) -{ - git_remote *remote; - - cl_git_pass(git_remote_create_inmemory(&remote, _repo, NULL, "file:///blah")); - cl_git_fail(git_remote_rename(remote, "newname", NULL, NULL)); - - git_remote_free(remote); -} diff --git a/tests-clar/network/remotes.c b/tests-clar/network/remotes.c deleted file mode 100644 index b138d8c106a..00000000000 --- a/tests-clar/network/remotes.c +++ /dev/null @@ -1,366 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "refspec.h" -#include "remote.h" - -static git_remote *_remote; -static git_repository *_repo; -static const git_refspec *_refspec; - -void test_network_remotes__initialize(void) -{ - _repo = cl_git_sandbox_init("testrepo.git"); - - cl_git_pass(git_remote_load(&_remote, _repo, "test")); - - _refspec = git_remote_fetchspec(_remote); - cl_assert(_refspec != NULL); -} - -void test_network_remotes__cleanup(void) -{ - git_remote_free(_remote); - _remote = NULL; - - cl_git_sandbox_cleanup(); -} - -void test_network_remotes__parsing(void) -{ - git_remote *_remote2 = NULL; - - cl_assert_equal_s(git_remote_name(_remote), "test"); - cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2"); - cl_assert(git_remote_pushurl(_remote) == NULL); - - cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_FETCH), - "git://github.com/libgit2/libgit2"); - cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_PUSH), - "git://github.com/libgit2/libgit2"); - - cl_git_pass(git_remote_load(&_remote2, _repo, "test_with_pushurl")); - cl_assert_equal_s(git_remote_name(_remote2), "test_with_pushurl"); - cl_assert_equal_s(git_remote_url(_remote2), "git://github.com/libgit2/fetchlibgit2"); - cl_assert_equal_s(git_remote_pushurl(_remote2), "git://github.com/libgit2/pushlibgit2"); - - cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_FETCH), - "git://github.com/libgit2/fetchlibgit2"); - cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_PUSH), - "git://github.com/libgit2/pushlibgit2"); - - git_remote_free(_remote2); -} - -void test_network_remotes__pushurl(void) -{ - cl_git_pass(git_remote_set_pushurl(_remote, "git://github.com/libgit2/notlibgit2")); - cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/notlibgit2"); - - cl_git_pass(git_remote_set_pushurl(_remote, NULL)); - cl_assert(git_remote_pushurl(_remote) == NULL); -} - -void test_network_remotes__error_when_no_push_available(void) -{ - git_remote *r; - git_transport *t; - git_push *p; - - cl_git_pass(git_remote_create_inmemory(&r, _repo, NULL, cl_fixture("testrepo.git"))); - - cl_git_pass(git_transport_local(&t,r,NULL)); - - /* Make sure that push is really not available */ - t->push = NULL; - cl_git_pass(git_remote_connect(r, GIT_DIRECTION_PUSH)); - cl_git_pass(git_push_new(&p, r)); - cl_git_pass(git_push_add_refspec(p, "refs/heads/master")); - cl_git_fail_with(git_push_finish(p), GIT_ERROR); - - git_push_free(p); - t->free(t); - git_remote_free(r); -} - -void test_network_remotes__parsing_ssh_remote(void) -{ - cl_assert( git_remote_valid_url("git@github.com:libgit2/libgit2.git") ); -} - -void test_network_remotes__parsing_local_path_fails_if_path_not_found(void) -{ - cl_assert( !git_remote_valid_url("/home/git/repos/libgit2.git") ); -} - -void test_network_remotes__supported_transport_methods_are_supported(void) -{ - cl_assert( git_remote_supported_url("git://github.com/libgit2/libgit2") ); -} - -void test_network_remotes__unsupported_transport_methods_are_unsupported(void) -{ - cl_assert( !git_remote_supported_url("git@github.com:libgit2/libgit2.git") ); -} - -void test_network_remotes__refspec_parsing(void) -{ - cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*"); - cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/test/*"); -} - -void test_network_remotes__set_fetchspec(void) -{ - cl_git_pass(git_remote_set_fetchspec(_remote, "refs/*:refs/*")); - _refspec = git_remote_fetchspec(_remote); - cl_assert_equal_s(git_refspec_src(_refspec), "refs/*"); - cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*"); -} - -void test_network_remotes__set_pushspec(void) -{ - cl_git_pass(git_remote_set_pushspec(_remote, "refs/*:refs/*")); - _refspec = git_remote_pushspec(_remote); - cl_assert_equal_s(git_refspec_src(_refspec), "refs/*"); - cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*"); -} - -void test_network_remotes__save(void) -{ - git_remote_free(_remote); - _remote = NULL; - - /* Set up the remote and save it to config */ - cl_git_pass(git_remote_create(&_remote, _repo, "upstream", "git://github.com/libgit2/libgit2")); - cl_git_pass(git_remote_set_fetchspec(_remote, "refs/heads/*:refs/remotes/upstream/*")); - cl_git_pass(git_remote_set_pushspec(_remote, "refs/heads/*:refs/heads/*")); - cl_git_pass(git_remote_set_pushurl(_remote, "git://github.com/libgit2/libgit2_push")); - cl_git_pass(git_remote_save(_remote)); - git_remote_free(_remote); - _remote = NULL; - - /* Load it from config and make sure everything matches */ - cl_git_pass(git_remote_load(&_remote, _repo, "upstream")); - - _refspec = git_remote_fetchspec(_remote); - cl_assert(_refspec != NULL); - cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*"); - cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/upstream/*"); - cl_assert_equal_i(0, git_refspec_force(_refspec)); - - _refspec = git_remote_pushspec(_remote); - cl_assert(_refspec != NULL); - cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*"); - cl_assert_equal_s(git_refspec_dst(_refspec), "refs/heads/*"); - - cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2"); - cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/libgit2_push"); - - /* remove the pushurl again and see if we can save that too */ - cl_git_pass(git_remote_set_pushurl(_remote, NULL)); - cl_git_pass(git_remote_save(_remote)); - git_remote_free(_remote); - _remote = NULL; - - cl_git_pass(git_remote_load(&_remote, _repo, "upstream")); - cl_assert(git_remote_pushurl(_remote) == NULL); -} - -void test_network_remotes__fnmatch(void) -{ - cl_assert(git_refspec_src_matches(_refspec, "refs/heads/master")); - cl_assert(git_refspec_src_matches(_refspec, "refs/heads/multi/level/branch")); -} - -void test_network_remotes__transform(void) -{ - char ref[1024]; - - memset(ref, 0x0, sizeof(ref)); - cl_git_pass(git_refspec_transform(ref, sizeof(ref), _refspec, "refs/heads/master")); - cl_assert_equal_s(ref, "refs/remotes/test/master"); -} - -void test_network_remotes__transform_r(void) -{ - git_buf buf = GIT_BUF_INIT; - - cl_git_pass(git_refspec_transform_r(&buf, _refspec, "refs/heads/master")); - cl_assert_equal_s(git_buf_cstr(&buf), "refs/remotes/test/master"); - git_buf_free(&buf); -} - -void test_network_remotes__missing_refspecs(void) -{ - git_config *cfg; - - git_remote_free(_remote); - _remote = NULL; - - cl_git_pass(git_repository_config(&cfg, _repo)); - cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com")); - cl_git_pass(git_remote_load(&_remote, _repo, "specless")); - - git_config_free(cfg); -} - -void test_network_remotes__list(void) -{ - git_strarray list; - git_config *cfg; - - cl_git_pass(git_remote_list(&list, _repo)); - cl_assert(list.count == 4); - git_strarray_free(&list); - - cl_git_pass(git_repository_config(&cfg, _repo)); - cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com")); - cl_git_pass(git_remote_list(&list, _repo)); - cl_assert(list.count == 5); - git_strarray_free(&list); - - git_config_free(cfg); -} - -void test_network_remotes__loading_a_missing_remote_returns_ENOTFOUND(void) -{ - git_remote_free(_remote); - _remote = NULL; - - cl_assert_equal_i(GIT_ENOTFOUND, git_remote_load(&_remote, _repo, "just-left-few-minutes-ago")); -} - -void test_network_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void) -{ - git_remote_free(_remote); - _remote = NULL; - - cl_assert_equal_i(GIT_EINVALIDSPEC, git_remote_load(&_remote, _repo, "Inv@{id")); -} - -/* - * $ git remote add addtest http://github.com/libgit2/libgit2 - * - * $ cat .git/config - * [...] - * [remote "addtest"] - * url = http://github.com/libgit2/libgit2 - * fetch = +refs/heads/\*:refs/remotes/addtest/\* - */ -void test_network_remotes__add(void) -{ - git_remote_free(_remote); - _remote = NULL; - - cl_git_pass(git_remote_create(&_remote, _repo, "addtest", "http://github.com/libgit2/libgit2")); - - git_remote_free(_remote); - _remote = NULL; - - cl_git_pass(git_remote_load(&_remote, _repo, "addtest")); - _refspec = git_remote_fetchspec(_remote); - cl_assert_equal_s("refs/heads/*", git_refspec_src(_refspec)); - cl_assert(git_refspec_force(_refspec) == 1); - cl_assert_equal_s("refs/remotes/addtest/*", git_refspec_dst(_refspec)); - cl_assert_equal_s(git_remote_url(_remote), "http://github.com/libgit2/libgit2"); -} - -void test_network_remotes__cannot_add_a_nameless_remote(void) -{ - git_remote *remote; - - cl_assert_equal_i( - GIT_EINVALIDSPEC, - git_remote_create(&remote, _repo, NULL, "git://github.com/libgit2/libgit2")); -} - -void test_network_remotes__cannot_save_an_inmemory_remote(void) -{ - git_remote *remote; - - cl_git_pass(git_remote_create_inmemory(&remote, _repo, NULL, "git://github.com/libgit2/libgit2")); - - cl_assert_equal_p(NULL, git_remote_name(remote)); - - cl_git_fail(git_remote_save(remote)); - git_remote_free(remote); -} - -void test_network_remotes__cannot_add_a_remote_with_an_invalid_name(void) -{ - git_remote *remote = NULL; - - cl_assert_equal_i( - GIT_EINVALIDSPEC, - git_remote_create(&remote, _repo, "Inv@{id", "git://github.com/libgit2/libgit2")); - cl_assert_equal_p(remote, NULL); - - cl_assert_equal_i( - GIT_EINVALIDSPEC, - git_remote_create(&remote, _repo, "", "git://github.com/libgit2/libgit2")); - cl_assert_equal_p(remote, NULL); -} - -void test_network_remotes__tagopt(void) -{ - const char *opt; - git_config *cfg; - - cl_git_pass(git_repository_config(&cfg, _repo)); - - git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_ALL); - cl_git_pass(git_remote_save(_remote)); - cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt")); - cl_assert_equal_s("--tags", opt); - - git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_NONE); - cl_git_pass(git_remote_save(_remote)); - cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt")); - cl_assert_equal_s("--no-tags", opt); - - git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_AUTO); - cl_git_pass(git_remote_save(_remote)); - cl_assert(git_config_get_string(&opt, cfg, "remote.test.tagopt") == GIT_ENOTFOUND); - - git_config_free(cfg); -} - -void test_network_remotes__cannot_load_with_an_empty_url(void) -{ - git_remote *remote = NULL; - - cl_git_fail(git_remote_load(&remote, _repo, "empty-remote-url")); - cl_assert(giterr_last()->klass == GITERR_INVALID); - cl_assert_equal_p(remote, NULL); -} - -void test_network_remotes__check_structure_version(void) -{ - git_transport transport = GIT_TRANSPORT_INIT; - const git_error *err; - - git_remote_free(_remote); - _remote = NULL; - cl_git_pass(git_remote_create_inmemory(&_remote, _repo, NULL, "test-protocol://localhost")); - - transport.version = 0; - cl_git_fail(git_remote_set_transport(_remote, &transport)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); - - giterr_clear(); - transport.version = 1024; - cl_git_fail(git_remote_set_transport(_remote, &transport)); - err = giterr_last(); - cl_assert_equal_i(GITERR_INVALID, err->klass); -} - -void test_network_remotes__cannot_create_a_remote_which_name_conflicts_with_an_existing_remote(void) -{ - git_remote *remote = NULL; - - cl_assert_equal_i( - GIT_EEXISTS, - git_remote_create(&remote, _repo, "test", "git://github.com/libgit2/libgit2")); - - cl_assert_equal_p(remote, NULL); -} diff --git a/tests-clar/notes/notes.c b/tests-clar/notes/notes.c deleted file mode 100644 index ee0b6c2f833..00000000000 --- a/tests-clar/notes/notes.c +++ /dev/null @@ -1,319 +0,0 @@ -#include "clar_libgit2.h" - -static git_repository *_repo; -static git_signature *_sig; - -void test_notes_notes__initialize(void) -{ - _repo = cl_git_sandbox_init("testrepo.git"); - cl_git_pass(git_signature_now(&_sig, "alice", "alice@example.com")); -} - -void test_notes_notes__cleanup(void) -{ - git_signature_free(_sig); - _sig = NULL; - - cl_git_sandbox_cleanup(); -} - -static void assert_note_equal(git_note *note, char *message, git_oid *note_oid) { - git_blob *blob; - - cl_assert_equal_s(git_note_message(note), message); - cl_assert(!git_oid_cmp(git_note_oid(note), note_oid)); - - cl_git_pass(git_blob_lookup(&blob, _repo, note_oid)); - cl_assert_equal_s(git_note_message(note), (const char *)git_blob_rawcontent(blob)); - - git_blob_free(blob); -} - -static void create_note(git_oid *note_oid, const char *canonical_namespace, const char *target_sha, const char *message) -{ - git_oid oid; - - cl_git_pass(git_oid_fromstr(&oid, target_sha)); - cl_git_pass(git_note_create(note_oid, _repo, _sig, _sig, canonical_namespace, &oid, message, 0)); -} - -static struct { - const char *note_sha; - const char *annotated_object_sha; -} list_expectations[] = { - { "1c73b1f51762155d357bcd1fd4f2c409ef80065b", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045" }, - { "1c73b1f51762155d357bcd1fd4f2c409ef80065b", "9fd738e8f7967c078dceed8190330fc8648ee56a" }, - { "257b43746b6b46caa4aa788376c647cce0a33e2b", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750" }, - { "1ec1c8e03f461f4f5d3f3702172483662e7223f3", "c47800c7266a2be04c571c04d5a6614691ea99bd" }, - { NULL, NULL } -}; - -#define EXPECTATIONS_COUNT (sizeof(list_expectations)/sizeof(list_expectations[0])) - 1 - -static int note_list_cb( - const git_oid *blob_id, const git_oid *annotated_obj_id, void *payload) -{ - git_oid expected_note_oid, expected_target_oid; - - unsigned int *count = (unsigned int *)payload; - - cl_assert(*count < EXPECTATIONS_COUNT); - - cl_git_pass(git_oid_fromstr(&expected_note_oid, list_expectations[*count].note_sha)); - cl_assert(git_oid_cmp(&expected_note_oid, blob_id) == 0); - - cl_git_pass(git_oid_fromstr(&expected_target_oid, list_expectations[*count].annotated_object_sha)); - cl_assert(git_oid_cmp(&expected_target_oid, annotated_obj_id) == 0); - - (*count)++; - - return 0; -} - -/* - * $ git notes --ref i-can-see-dead-notes add -m "I decorate a65f" a65fedf39aefe402d3bb6e24df4d4f5fe4547750 - * $ git notes --ref i-can-see-dead-notes add -m "I decorate c478" c47800c7266a2be04c571c04d5a6614691ea99bd - * $ git notes --ref i-can-see-dead-notes add -m "I decorate 9fd7 and 4a20" 9fd738e8f7967c078dceed8190330fc8648ee56a - * $ git notes --ref i-can-see-dead-notes add -m "I decorate 9fd7 and 4a20" 4a202b346bb0fb0db7eff3cffeb3c70babbd2045 - * - * $ git notes --ref i-can-see-dead-notes list - * 1c73b1f51762155d357bcd1fd4f2c409ef80065b 4a202b346bb0fb0db7eff3cffeb3c70babbd2045 - * 1c73b1f51762155d357bcd1fd4f2c409ef80065b 9fd738e8f7967c078dceed8190330fc8648ee56a - * 257b43746b6b46caa4aa788376c647cce0a33e2b a65fedf39aefe402d3bb6e24df4d4f5fe4547750 - * 1ec1c8e03f461f4f5d3f3702172483662e7223f3 c47800c7266a2be04c571c04d5a6614691ea99bd - * - * $ git ls-tree refs/notes/i-can-see-dead-notes - * 100644 blob 1c73b1f51762155d357bcd1fd4f2c409ef80065b 4a202b346bb0fb0db7eff3cffeb3c70babbd2045 - * 100644 blob 1c73b1f51762155d357bcd1fd4f2c409ef80065b 9fd738e8f7967c078dceed8190330fc8648ee56a - * 100644 blob 257b43746b6b46caa4aa788376c647cce0a33e2b a65fedf39aefe402d3bb6e24df4d4f5fe4547750 - * 100644 blob 1ec1c8e03f461f4f5d3f3702172483662e7223f3 c47800c7266a2be04c571c04d5a6614691ea99bd -*/ -void test_notes_notes__can_retrieve_a_list_of_notes_for_a_given_namespace(void) -{ - git_oid note_oid1, note_oid2, note_oid3, note_oid4; - unsigned int retrieved_notes = 0; - - create_note(¬e_oid1, "refs/notes/i-can-see-dead-notes", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "I decorate a65f\n"); - create_note(¬e_oid2, "refs/notes/i-can-see-dead-notes", "c47800c7266a2be04c571c04d5a6614691ea99bd", "I decorate c478\n"); - create_note(¬e_oid3, "refs/notes/i-can-see-dead-notes", "9fd738e8f7967c078dceed8190330fc8648ee56a", "I decorate 9fd7 and 4a20\n"); - create_note(¬e_oid4, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 9fd7 and 4a20\n"); - - cl_git_pass(git_note_foreach -(_repo, "refs/notes/i-can-see-dead-notes", note_list_cb, &retrieved_notes)); - - cl_assert_equal_i(4, retrieved_notes); -} - -static int note_cancel_cb( - const git_oid *blob_id, const git_oid *annotated_obj_id, void *payload) -{ - unsigned int *count = (unsigned int *)payload; - - GIT_UNUSED(blob_id); - GIT_UNUSED(annotated_obj_id); - - (*count)++; - - return (*count > 2); -} - -void test_notes_notes__can_cancel_foreach(void) -{ - git_oid note_oid1, note_oid2, note_oid3, note_oid4; - unsigned int retrieved_notes = 0; - - create_note(¬e_oid1, "refs/notes/i-can-see-dead-notes", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "I decorate a65f\n"); - create_note(¬e_oid2, "refs/notes/i-can-see-dead-notes", "c47800c7266a2be04c571c04d5a6614691ea99bd", "I decorate c478\n"); - create_note(¬e_oid3, "refs/notes/i-can-see-dead-notes", "9fd738e8f7967c078dceed8190330fc8648ee56a", "I decorate 9fd7 and 4a20\n"); - create_note(¬e_oid4, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 9fd7 and 4a20\n"); - - cl_assert_equal_i( - GIT_EUSER, - git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes", - note_cancel_cb, &retrieved_notes)); -} - -void test_notes_notes__retrieving_a_list_of_notes_for_an_unknown_namespace_returns_ENOTFOUND(void) -{ - int error; - unsigned int retrieved_notes = 0; - - error = git_note_foreach(_repo, "refs/notes/i-am-not", note_list_cb, &retrieved_notes); - cl_git_fail(error); - cl_assert_equal_i(GIT_ENOTFOUND, error); - - cl_assert_equal_i(0, retrieved_notes); -} - -void test_notes_notes__inserting_a_note_without_passing_a_namespace_uses_the_default_namespace(void) -{ - git_oid note_oid, target_oid; - git_note *note, *default_namespace_note; - const char *default_ref; - - cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); - cl_git_pass(git_note_default_ref(&default_ref, _repo)); - - create_note(¬e_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n"); - - cl_git_pass(git_note_read(¬e, _repo, NULL, &target_oid)); - cl_git_pass(git_note_read(&default_namespace_note, _repo, default_ref, &target_oid)); - - assert_note_equal(note, "hello world\n", ¬e_oid); - assert_note_equal(default_namespace_note, "hello world\n", ¬e_oid); - - git_note_free(note); - git_note_free(default_namespace_note); -} - -void test_notes_notes__can_insert_a_note_with_a_custom_namespace(void) -{ - git_oid note_oid, target_oid; - git_note *note; - - cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); - - create_note(¬e_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world on a custom namespace\n"); - - cl_git_pass(git_note_read(¬e, _repo, "refs/notes/some/namespace", &target_oid)); - - assert_note_equal(note, "hello world on a custom namespace\n", ¬e_oid); - - git_note_free(note); -} - -/* - * $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479 - * 08b041783f40edfe12bb406c9c9a8a040177c125 - */ -void test_notes_notes__creating_a_note_on_a_target_which_already_has_one_returns_EEXISTS(void) -{ - int error; - git_oid note_oid, target_oid; - - cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); - - create_note(¬e_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n"); - error = git_note_create(¬e_oid, _repo, _sig, _sig, NULL, &target_oid, "hello world\n", 0); - cl_git_fail(error); - cl_assert_equal_i(GIT_EEXISTS, error); - - create_note(¬e_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n"); - error = git_note_create(¬e_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &target_oid, "hello world\n", 0); - cl_git_fail(error); - cl_assert_equal_i(GIT_EEXISTS, error); -} - - -void test_notes_notes__creating_a_note_on_a_target_can_overwrite_existing_note(void) -{ - git_oid note_oid, target_oid; - git_note *note, *namespace_note; - - cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); - - create_note(¬e_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello old world\n"); - cl_git_pass(git_note_create(¬e_oid, _repo, _sig, _sig, NULL, &target_oid, "hello new world\n", 1)); - - cl_git_pass(git_note_read(¬e, _repo, NULL, &target_oid)); - assert_note_equal(note, "hello new world\n", ¬e_oid); - - create_note(¬e_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello old world\n"); - cl_git_pass(git_note_create(¬e_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &target_oid, "hello new ref world\n", 1)); - - cl_git_pass(git_note_read(&namespace_note, _repo, "refs/notes/some/namespace", &target_oid)); - assert_note_equal(namespace_note, "hello new ref world\n", ¬e_oid); - - git_note_free(note); - git_note_free(namespace_note); -} - -static char *messages[] = { - "08c041783f40edfe12bb406c9c9a8a040177c125", - "96c45fbe09ab7445fc7c60fd8d17f32494399343", - "48cc7e38dcfc1ec87e70ec03e08c3e83d7a16aa1", - "24c3eaafb681c3df668f9df96f58e7b8c756eb04", - "96ca1b6ccc7858ae94684777f85ac0e7447f7040", - "7ac2db4378a08bb244a427c357e0082ee0d57ac6", - "e6cba23dbf4ef84fe35e884f017f4e24dc228572", - "c8cf3462c7d8feba716deeb2ebe6583bd54589e2", - "39c16b9834c2d665ac5f68ad91dc5b933bad8549", - "f3c582b1397df6a664224ebbaf9d4cc952706597", - "29cec67037fe8e89977474988219016ae7f342a6", - "36c4cd238bf8e82e27b740e0741b025f2e8c79ab", - "f1c45a47c02e01d5a9a326f1d9f7f756373387f8", - "4aca84406f5daee34ab513a60717c8d7b1763ead", - "84ce167da452552f63ed8407b55d5ece4901845f", - NULL -}; - -#define MESSAGES_COUNT (sizeof(messages)/sizeof(messages[0])) - 1 - -/* - * $ git ls-tree refs/notes/fanout - * 040000 tree 4b22b35d44b5a4f589edf3dc89196399771796ea 84 - * - * $ git ls-tree 4b22b35 - * 040000 tree d71aab4f9b04b45ce09bcaa636a9be6231474759 96 - * - * $ git ls-tree d71aab4 - * 100644 blob 08b041783f40edfe12bb406c9c9a8a040177c125 071c1b46c854b31185ea97743be6a8774479 - */ -void test_notes_notes__can_insert_a_note_in_an_existing_fanout(void) -{ - size_t i; - git_oid note_oid, target_oid; - git_note *_note; - - cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); - - for (i = 0; i < MESSAGES_COUNT; i++) { - cl_git_pass(git_note_create(¬e_oid, _repo, _sig, _sig, "refs/notes/fanout", &target_oid, messages[i], 0)); - cl_git_pass(git_note_read(&_note, _repo, "refs/notes/fanout", &target_oid)); - git_note_free(_note); - - git_oid_cpy(&target_oid, ¬e_oid); - } -} - -/* - * $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479 - * 08b041783f40edfe12bb406c9c9a8a040177c125 - */ -void test_notes_notes__can_read_a_note_in_an_existing_fanout(void) -{ - git_oid note_oid, target_oid; - git_note *note; - - cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479")); - cl_git_pass(git_note_read(¬e, _repo, "refs/notes/fanout", &target_oid)); - - cl_git_pass(git_oid_fromstr(¬e_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); - cl_assert(!git_oid_cmp(git_note_oid(note), ¬e_oid)); - - git_note_free(note); -} - -void test_notes_notes__can_remove_a_note_in_an_existing_fanout(void) -{ - git_oid target_oid; - git_note *note; - - cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479")); - cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid)); - - cl_git_fail(git_note_read(¬e, _repo, "refs/notes/fanout", &target_oid)); -} - -void test_notes_notes__removing_a_note_which_doesnt_exists_returns_ENOTFOUND(void) -{ - int error; - git_oid target_oid; - - cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479")); - cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid)); - - error = git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid); - cl_git_fail(error); - cl_assert_equal_i(GIT_ENOTFOUND, error); -} diff --git a/tests-clar/notes/notesref.c b/tests-clar/notes/notesref.c deleted file mode 100644 index c89b71ba5c0..00000000000 --- a/tests-clar/notes/notesref.c +++ /dev/null @@ -1,64 +0,0 @@ -#include "clar_libgit2.h" - -#include "notes.h" - -static git_repository *_repo; -static git_note *_note; -static git_signature *_sig; -static git_config *_cfg; - -void test_notes_notesref__initialize(void) -{ - cl_fixture_sandbox("testrepo.git"); - cl_git_pass(git_repository_open(&_repo, "testrepo.git")); -} - -void test_notes_notesref__cleanup(void) -{ - git_note_free(_note); - _note = NULL; - - git_signature_free(_sig); - _sig = NULL; - - git_config_free(_cfg); - _cfg = NULL; - - git_repository_free(_repo); - _repo = NULL; - - cl_fixture_cleanup("testrepo.git"); -} - -void test_notes_notesref__config_corenotesref(void) -{ - git_oid oid, note_oid; - const char *default_ref; - - cl_git_pass(git_signature_now(&_sig, "alice", "alice@example.com")); - cl_git_pass(git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479")); - - cl_git_pass(git_repository_config(&_cfg, _repo)); - - cl_git_pass(git_config_set_string(_cfg, "core.notesRef", "refs/notes/mydefaultnotesref")); - - cl_git_pass(git_note_create(¬e_oid, _repo, _sig, _sig, NULL, &oid, "test123test\n", 0)); - - cl_git_pass(git_note_read(&_note, _repo, NULL, &oid)); - cl_assert_equal_s("test123test\n", git_note_message(_note)); - cl_assert(!git_oid_cmp(git_note_oid(_note), ¬e_oid)); - - git_note_free(_note); - - cl_git_pass(git_note_read(&_note, _repo, "refs/notes/mydefaultnotesref", &oid)); - cl_assert_equal_s("test123test\n", git_note_message(_note)); - cl_assert(!git_oid_cmp(git_note_oid(_note), ¬e_oid)); - - cl_git_pass(git_note_default_ref(&default_ref, _repo)); - cl_assert_equal_s("refs/notes/mydefaultnotesref", default_ref); - - cl_git_pass(git_config_delete_entry(_cfg, "core.notesRef")); - - cl_git_pass(git_note_default_ref(&default_ref, _repo)); - cl_assert_equal_s(GIT_NOTES_DEFAULT_REF, default_ref); -} diff --git a/tests-clar/object/blob/filter.c b/tests-clar/object/blob/filter.c deleted file mode 100644 index b9bbfff0c96..00000000000 --- a/tests-clar/object/blob/filter.c +++ /dev/null @@ -1,131 +0,0 @@ -#include "clar_libgit2.h" -#include "posix.h" -#include "blob.h" -#include "filter.h" - -static git_repository *g_repo = NULL; -#define NUM_TEST_OBJECTS 8 -static git_oid g_oids[NUM_TEST_OBJECTS]; -static const char *g_raw[NUM_TEST_OBJECTS] = { - "", - "foo\nbar\n", - "foo\rbar\r", - "foo\r\nbar\r\n", - "foo\nbar\rboth\r\nreversed\n\ragain\nproblems\r", - "123\n\000\001\002\003\004abc\255\254\253\r\n", - "\xEF\xBB\xBFThis is UTF-8\n", - "\xFE\xFF\x00T\x00h\x00i\x00s\x00!" -}; -static git_off_t g_len[NUM_TEST_OBJECTS] = { -1, -1, -1, -1, -1, 17, -1, 12 }; -static git_buf_text_stats g_stats[NUM_TEST_OBJECTS] = { - { 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 2, 0, 6, 0 }, - { 0, 0, 2, 0, 0, 6, 0 }, - { 0, 0, 2, 2, 2, 6, 0 }, - { 0, 0, 4, 4, 1, 31, 0 }, - { 0, 1, 1, 2, 1, 9, 5 }, - { GIT_BOM_UTF8, 0, 0, 1, 0, 16, 0 }, - { GIT_BOM_UTF16_BE, 5, 0, 0, 0, 7, 5 }, -}; -static git_buf g_crlf_filtered[NUM_TEST_OBJECTS] = { - { "", 0, 0 }, - { "foo\nbar\n", 0, 8 }, - { "foo\rbar\r", 0, 8 }, - { "foo\nbar\n", 0, 8 }, - { "foo\nbar\rboth\nreversed\n\ragain\nproblems\r", 0, 38 }, - { "123\n\000\001\002\003\004abc\255\254\253\n", 0, 16 }, - { "\xEF\xBB\xBFThis is UTF-8\n", 0, 17 }, - { "\xFE\xFF\x00T\x00h\x00i\x00s\x00!", 0, 12 } -}; - -void test_object_blob_filter__initialize(void) -{ - int i; - - cl_fixture_sandbox("empty_standard_repo"); - cl_git_pass(p_rename( - "empty_standard_repo/.gitted", "empty_standard_repo/.git")); - cl_git_pass(git_repository_open(&g_repo, "empty_standard_repo")); - - for (i = 0; i < NUM_TEST_OBJECTS; i++) { - size_t len = (g_len[i] < 0) ? strlen(g_raw[i]) : (size_t)g_len[i]; - g_len[i] = (git_off_t)len; - - cl_git_pass( - git_blob_create_frombuffer(&g_oids[i], g_repo, g_raw[i], len) - ); - } -} - -void test_object_blob_filter__cleanup(void) -{ - git_repository_free(g_repo); - g_repo = NULL; - cl_fixture_cleanup("empty_standard_repo"); -} - -void test_object_blob_filter__unfiltered(void) -{ - int i; - git_blob *blob; - - for (i = 0; i < NUM_TEST_OBJECTS; i++) { - cl_git_pass(git_blob_lookup(&blob, g_repo, &g_oids[i])); - cl_assert(g_len[i] == git_blob_rawsize(blob)); - cl_assert(memcmp(git_blob_rawcontent(blob), g_raw[i], (size_t)g_len[i]) == 0); - git_blob_free(blob); - } -} - -void test_object_blob_filter__stats(void) -{ - int i; - git_blob *blob; - git_buf buf = GIT_BUF_INIT; - git_buf_text_stats stats; - - for (i = 0; i < NUM_TEST_OBJECTS; i++) { - cl_git_pass(git_blob_lookup(&blob, g_repo, &g_oids[i])); - cl_git_pass(git_blob__getbuf(&buf, blob)); - git_buf_text_gather_stats(&stats, &buf, false); - cl_assert(memcmp(&g_stats[i], &stats, sizeof(stats)) == 0); - git_blob_free(blob); - } - - git_buf_free(&buf); -} - -void test_object_blob_filter__to_odb(void) -{ - git_vector filters = GIT_VECTOR_INIT; - git_config *cfg; - int i; - git_blob *blob; - git_buf orig = GIT_BUF_INIT, out = GIT_BUF_INIT; - - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_assert(cfg); - - git_attr_cache_flush(g_repo); - cl_git_append2file("empty_standard_repo/.gitattributes", "*.txt text\n"); - - cl_assert(git_filters_load( - &filters, g_repo, "filename.txt", GIT_FILTER_TO_ODB) > 0); - cl_assert(filters.length == 1); - - for (i = 0; i < NUM_TEST_OBJECTS; i++) { - cl_git_pass(git_blob_lookup(&blob, g_repo, &g_oids[i])); - cl_git_pass(git_blob__getbuf(&orig, blob)); - - cl_git_pass(git_filters_apply(&out, &orig, &filters)); - cl_assert(git_buf_cmp(&out, &g_crlf_filtered[i]) == 0); - - git_blob_free(blob); - } - - git_filters_free(&filters); - git_buf_free(&orig); - git_buf_free(&out); - git_config_free(cfg); -} - diff --git a/tests-clar/object/blob/fromchunks.c b/tests-clar/object/blob/fromchunks.c deleted file mode 100644 index dc57d4fbe41..00000000000 --- a/tests-clar/object/blob/fromchunks.c +++ /dev/null @@ -1,87 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "posix.h" -#include "path.h" -#include "fileops.h" - -static git_repository *repo; -static char textual_content[] = "libgit2\n\r\n\0"; - -void test_object_blob_fromchunks__initialize(void) -{ - repo = cl_git_sandbox_init("testrepo.git"); -} - -void test_object_blob_fromchunks__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -static int text_chunked_source_cb(char *content, size_t max_length, void *payload) -{ - int *count; - - GIT_UNUSED(max_length); - - count = (int *)payload; - (*count)--; - - if (*count == 0) - return 0; - - strcpy(content, textual_content); - return (int)strlen(textual_content); -} - -void test_object_blob_fromchunks__can_create_a_blob_from_a_in_memory_chunk_provider(void) -{ - git_oid expected_oid, oid; - git_object *blob; - int howmany = 7; - - cl_git_pass(git_oid_fromstr(&expected_oid, "321cbdf08803c744082332332838df6bd160f8f9")); - - cl_git_fail(git_object_lookup(&blob, repo, &expected_oid, GIT_OBJ_ANY)); - - cl_git_pass(git_blob_create_fromchunks(&oid, repo, NULL, text_chunked_source_cb, &howmany)); - - cl_git_pass(git_object_lookup(&blob, repo, &expected_oid, GIT_OBJ_ANY)); - git_object_free(blob); -} - -#define GITATTR "* text=auto\n" \ - "*.txt text\n" \ - "*.data binary\n" - -static void write_attributes(git_repository *repo) -{ - git_buf buf = GIT_BUF_INIT; - - cl_git_pass(git_buf_joinpath(&buf, git_repository_path(repo), "info")); - cl_git_pass(git_buf_joinpath(&buf, git_buf_cstr(&buf), "attributes")); - - cl_git_pass(git_futils_mkpath2file(git_buf_cstr(&buf), 0777)); - cl_git_rewritefile(git_buf_cstr(&buf), GITATTR); - - git_buf_free(&buf); -} - -static void assert_named_chunked_blob(const char *expected_sha, const char *fake_name) -{ - git_oid expected_oid, oid; - int howmany = 7; - - cl_git_pass(git_oid_fromstr(&expected_oid, expected_sha)); - - cl_git_pass(git_blob_create_fromchunks(&oid, repo, fake_name, text_chunked_source_cb, &howmany)); - cl_assert(git_oid_cmp(&expected_oid, &oid) == 0); -} - -void test_object_blob_fromchunks__creating_a_blob_from_chunks_honors_the_attributes_directives(void) -{ - write_attributes(repo); - - assert_named_chunked_blob("321cbdf08803c744082332332838df6bd160f8f9", "dummy.data"); - assert_named_chunked_blob("e9671e138a780833cb689753570fd10a55be84fb", "dummy.txt"); - assert_named_chunked_blob("e9671e138a780833cb689753570fd10a55be84fb", "dummy.dunno"); -} diff --git a/tests-clar/object/blob/write.c b/tests-clar/object/blob/write.c deleted file mode 100644 index 203bc67c1a8..00000000000 --- a/tests-clar/object/blob/write.c +++ /dev/null @@ -1,69 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "posix.h" -#include "path.h" -#include "fileops.h" - -static git_repository *repo; - -#define WORKDIR "empty_standard_repo" -#define BARE_REPO "testrepo.git" -#define ELSEWHERE "elsewhere" - -typedef int (*blob_creator_fn)( - git_oid *, - git_repository *, - const char *); - -void test_object_blob_write__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -static void assert_blob_creation(const char *path_to_file, const char *blob_from_path, blob_creator_fn creator) -{ - git_oid oid; - cl_git_mkfile(path_to_file, "1..2...3... Can you hear me?\n"); - - cl_must_pass(creator(&oid, repo, blob_from_path)); - cl_assert(git_oid_streq(&oid, "da5e4f20c91c81b44a7e298f3d3fb3fe2f178e32") == 0); -} - -void test_object_blob_write__can_create_a_blob_in_a_standard_repo_from_a_file_located_in_the_working_directory(void) -{ - repo = cl_git_sandbox_init(WORKDIR); - - assert_blob_creation(WORKDIR "/test.txt", "test.txt", &git_blob_create_fromworkdir); -} - -void test_object_blob_write__can_create_a_blob_in_a_standard_repo_from_a_absolute_filepath_pointing_outside_of_the_working_directory(void) -{ - git_buf full_path = GIT_BUF_INIT; - - repo = cl_git_sandbox_init(WORKDIR); - - cl_must_pass(p_mkdir(ELSEWHERE, 0777)); - cl_must_pass(git_path_prettify_dir(&full_path, ELSEWHERE, NULL)); - cl_must_pass(git_buf_puts(&full_path, "test.txt")); - - assert_blob_creation(ELSEWHERE "/test.txt", git_buf_cstr(&full_path), &git_blob_create_fromdisk); - - git_buf_free(&full_path); - cl_must_pass(git_futils_rmdir_r(ELSEWHERE, NULL, GIT_RMDIR_REMOVE_FILES)); -} - -void test_object_blob_write__can_create_a_blob_in_a_bare_repo_from_a_absolute_filepath(void) -{ - git_buf full_path = GIT_BUF_INIT; - - repo = cl_git_sandbox_init(BARE_REPO); - - cl_must_pass(p_mkdir(ELSEWHERE, 0777)); - cl_must_pass(git_path_prettify_dir(&full_path, ELSEWHERE, NULL)); - cl_must_pass(git_buf_puts(&full_path, "test.txt")); - - assert_blob_creation(ELSEWHERE "/test.txt", git_buf_cstr(&full_path), &git_blob_create_fromdisk); - - git_buf_free(&full_path); - cl_must_pass(git_futils_rmdir_r(ELSEWHERE, NULL, GIT_RMDIR_REMOVE_FILES)); -} diff --git a/tests-clar/object/commit/commitstagedfile.c b/tests-clar/object/commit/commitstagedfile.c deleted file mode 100644 index 9867ab41802..00000000000 --- a/tests-clar/object/commit/commitstagedfile.c +++ /dev/null @@ -1,132 +0,0 @@ -#include "clar_libgit2.h" -#include "posix.h" - -static git_repository *repo; - -void test_object_commit_commitstagedfile__initialize(void) -{ - cl_fixture("treebuilder"); - cl_git_pass(git_repository_init(&repo, "treebuilder/", 0)); - cl_assert(repo != NULL); -} - -void test_object_commit_commitstagedfile__cleanup(void) -{ - git_repository_free(repo); - repo = NULL; - - cl_fixture_cleanup("treebuilder"); -} - -void test_object_commit_commitstagedfile__generate_predictable_object_ids(void) -{ - git_index *index; - const git_index_entry *entry; - git_oid expected_blob_oid, tree_oid, expected_tree_oid, commit_oid, expected_commit_oid; - git_signature *signature; - git_tree *tree; - char buffer[128]; - - /* - * The test below replicates the following git scenario - * - * $ echo "test" > test.txt - * $ git hash-object test.txt - * 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 - * - * $ git add . - * $ git commit -m "Initial commit" - * - * $ git log - * commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d - * Author: nulltoken - * Date: Wed Dec 14 08:29:03 2011 +0100 - * - * Initial commit - * - * $ git show 1fe3 --format=raw - * commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d - * tree 2b297e643c551e76cfa1f93810c50811382f9117 - * author nulltoken 1323847743 +0100 - * committer nulltoken 1323847743 +0100 - * - * Initial commit - * - * diff --git a/test.txt b/test.txt - * new file mode 100644 - * index 0000000..9daeafb - * --- /dev/null - * +++ b/test.txt - * @@ -0,0 +1 @@ - * +test - * - * $ git ls-tree 2b297 - * 100644 blob 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 test.txt - */ - - cl_git_pass(git_oid_fromstr(&expected_commit_oid, "1fe3126578fc4eca68c193e4a3a0a14a0704624d")); - cl_git_pass(git_oid_fromstr(&expected_tree_oid, "2b297e643c551e76cfa1f93810c50811382f9117")); - cl_git_pass(git_oid_fromstr(&expected_blob_oid, "9daeafb9864cf43055ae93beb0afd6c7d144bfa4")); - - /* - * Add a new file to the index - */ - cl_git_mkfile("treebuilder/test.txt", "test\n"); - cl_git_pass(git_repository_index(&index, repo)); - cl_git_pass(git_index_add_bypath(index, "test.txt")); - - entry = git_index_get_byindex(index, 0); - - cl_assert(git_oid_cmp(&expected_blob_oid, &entry->oid) == 0); - - /* - * Information about index entry should match test file - */ - { - struct stat st; - cl_must_pass(p_lstat("treebuilder/test.txt", &st)); - cl_assert(entry->file_size == st.st_size); -#ifndef _WIN32 - /* - * Windows doesn't populate these fields, and the signage is - * wrong in the Windows version of the struct, so lets avoid - * the "comparing signed and unsigned" compilation warning in - * that case. - */ - cl_assert(entry->uid == st.st_uid); - cl_assert(entry->gid == st.st_gid); -#endif - } - - /* - * Build the tree from the index - */ - cl_git_pass(git_index_write_tree(&tree_oid, index)); - - cl_assert(git_oid_cmp(&expected_tree_oid, &tree_oid) == 0); - - /* - * Commit the staged file - */ - cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); - cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid)); - - cl_assert_equal_i(16, git_message_prettify(buffer, 128, "Initial commit", 0)); - - cl_git_pass(git_commit_create_v( - &commit_oid, - repo, - "HEAD", - signature, - signature, - NULL, - buffer, - tree, - 0)); - - cl_assert(git_oid_cmp(&expected_commit_oid, &commit_oid) == 0); - - git_signature_free(signature); - git_tree_free(tree); - git_index_free(index); -} diff --git a/tests-clar/object/lookup.c b/tests-clar/object/lookup.c deleted file mode 100644 index cfa6d467808..00000000000 --- a/tests-clar/object/lookup.c +++ /dev/null @@ -1,65 +0,0 @@ -#include "clar_libgit2.h" - -#include "repository.h" - -static git_repository *g_repo; - -void test_object_lookup__initialize(void) -{ - cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git"))); -} - -void test_object_lookup__cleanup(void) -{ - git_repository_free(g_repo); - g_repo = NULL; -} - -void test_object_lookup__lookup_wrong_type_returns_enotfound(void) -{ - const char *commit = "e90810b8df3e80c413d903f631643c716887138d"; - git_oid oid; - git_object *object; - - cl_git_pass(git_oid_fromstr(&oid, commit)); - cl_assert_equal_i( - GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_TAG)); -} - -void test_object_lookup__lookup_nonexisting_returns_enotfound(void) -{ - const char *unknown = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"; - git_oid oid; - git_object *object; - - cl_git_pass(git_oid_fromstr(&oid, unknown)); - cl_assert_equal_i( - GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_ANY)); -} - -void test_object_lookup__lookup_wrong_type_by_abbreviated_id_returns_enotfound(void) -{ - const char *commit = "e90810b"; - git_oid oid; - git_object *object; - - cl_git_pass(git_oid_fromstrn(&oid, commit, strlen(commit))); - cl_assert_equal_i( - GIT_ENOTFOUND, git_object_lookup_prefix(&object, g_repo, &oid, strlen(commit), GIT_OBJ_TAG)); -} - -void test_object_lookup__lookup_wrong_type_eventually_returns_enotfound(void) -{ - const char *commit = "e90810b8df3e80c413d903f631643c716887138d"; - git_oid oid; - git_object *object; - - cl_git_pass(git_oid_fromstr(&oid, commit)); - - cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT)); - git_object_free(object); - - cl_assert_equal_i( - GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_TAG)); -} - diff --git a/tests-clar/object/message.c b/tests-clar/object/message.c deleted file mode 100644 index 7ef6374b3ce..00000000000 --- a/tests-clar/object/message.c +++ /dev/null @@ -1,236 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "message.h" - -static void assert_message_prettifying(char *expected_output, char *input, int strip_comments) -{ - git_buf prettified_message = GIT_BUF_INIT; - - git_message__prettify(&prettified_message, input, strip_comments); - cl_assert_equal_s(expected_output, git_buf_cstr(&prettified_message)); - - git_buf_free(&prettified_message); -} - -#define t40 "A quick brown fox jumps over the lazy do" -#define s40 " " -#define sss s40 s40 s40 s40 s40 s40 s40 s40 s40 s40 // # 400 -#define ttt t40 t40 t40 t40 t40 t40 t40 t40 t40 t40 // # 400 - -/* Ported from git.git */ -/* see https://github.com/git/git/blob/master/t/t0030-stripspace.sh */ -void test_object_message__long_lines_without_spaces_should_be_unchanged(void) -{ - assert_message_prettifying(ttt "\n", ttt, 0); - assert_message_prettifying(ttt ttt "\n", ttt ttt, 0); - assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt, 0); - assert_message_prettifying(ttt ttt ttt ttt "\n", ttt ttt ttt ttt, 0); -} - -void test_object_message__lines_with_spaces_at_the_beginning_should_be_unchanged(void) -{ - assert_message_prettifying(sss ttt "\n", sss ttt, 0); - assert_message_prettifying(sss sss ttt "\n", sss sss ttt, 0); - assert_message_prettifying(sss sss sss ttt "\n", sss sss sss ttt, 0); -} - -void test_object_message__lines_with_intermediate_spaces_should_be_unchanged(void) -{ - assert_message_prettifying(ttt sss ttt "\n", ttt sss ttt, 0); - assert_message_prettifying(ttt sss sss ttt "\n", ttt sss sss ttt, 0); -} - -void test_object_message__consecutive_blank_lines_should_be_unified(void) -{ - assert_message_prettifying(ttt "\n\n" ttt "\n", ttt "\n\n\n\n\n" ttt "\n", 0); - assert_message_prettifying(ttt ttt "\n\n" ttt "\n", ttt ttt "\n\n\n\n\n" ttt "\n", 0); - assert_message_prettifying(ttt ttt ttt "\n\n" ttt "\n", ttt ttt ttt "\n\n\n\n\n" ttt "\n", 0); - - assert_message_prettifying(ttt "\n\n" ttt ttt "\n", ttt "\n\n\n\n\n" ttt ttt "\n", 0); - assert_message_prettifying(ttt "\n\n" ttt ttt ttt "\n", ttt "\n\n\n\n\n" ttt ttt ttt "\n", 0); - - assert_message_prettifying(ttt "\n\n" ttt "\n", ttt "\n\t\n \n\n \t\t\n" ttt "\n", 0); - assert_message_prettifying(ttt ttt "\n\n" ttt "\n", ttt ttt "\n\t\n \n\n \t\t\n" ttt "\n", 0); - assert_message_prettifying(ttt ttt ttt "\n\n" ttt "\n", ttt ttt ttt "\n\t\n \n\n \t\t\n" ttt "\n", 0); - - assert_message_prettifying(ttt "\n\n" ttt ttt "\n", ttt "\n\t\n \n\n \t\t\n" ttt ttt "\n", 0); - assert_message_prettifying(ttt "\n\n" ttt ttt ttt "\n", ttt "\n\t\n \n\n \t\t\n" ttt ttt ttt "\n", 0); -} - -void test_object_message__only_consecutive_blank_lines_should_be_completely_removed(void) -{ - assert_message_prettifying("", "\n", 0); - assert_message_prettifying("", "\n\n\n", 0); - assert_message_prettifying("", sss "\n" sss "\n" sss "\n", 0); - assert_message_prettifying("", sss sss "\n" sss "\n\n", 0); -} - -void test_object_message__consecutive_blank_lines_at_the_beginning_should_be_removed(void) -{ - assert_message_prettifying(ttt "\n", "\n" ttt "\n", 0); - assert_message_prettifying(ttt "\n", "\n\n\n" ttt "\n", 0); - assert_message_prettifying(ttt ttt "\n", "\n\n\n" ttt ttt "\n", 0); - assert_message_prettifying(ttt ttt ttt "\n", "\n\n\n" ttt ttt ttt "\n", 0); - assert_message_prettifying(ttt ttt ttt ttt "\n", "\n\n\n" ttt ttt ttt ttt "\n", 0); - assert_message_prettifying(ttt "\n", sss "\n" sss "\n" sss "\n" ttt "\n", 0); - assert_message_prettifying(ttt "\n", "\n" sss "\n" sss sss "\n" ttt "\n", 0); - assert_message_prettifying(ttt "\n", sss sss "\n" sss "\n\n" ttt "\n", 0); - assert_message_prettifying(ttt "\n", sss sss sss "\n\n\n" ttt "\n", 0); - assert_message_prettifying(ttt "\n", "\n" sss sss sss "\n\n" ttt "\n", 0); - assert_message_prettifying(ttt "\n", "\n\n" sss sss sss "\n" ttt "\n", 0); -} - -void test_object_message__consecutive_blank_lines_at_the_end_should_be_removed(void) -{ - assert_message_prettifying(ttt "\n", ttt "\n\n", 0); - assert_message_prettifying(ttt "\n", ttt "\n\n\n\n", 0); - assert_message_prettifying(ttt ttt "\n", ttt ttt "\n\n\n\n", 0); - assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt "\n\n\n\n", 0); - assert_message_prettifying(ttt ttt ttt ttt "\n", ttt ttt ttt ttt "\n\n\n\n", 0); - assert_message_prettifying(ttt "\n", ttt "\n" sss "\n" sss "\n" sss "\n", 0); - assert_message_prettifying(ttt "\n", ttt "\n\n" sss "\n" sss sss "\n", 0); - assert_message_prettifying(ttt "\n", ttt "\n" sss sss "\n" sss "\n\n", 0); - assert_message_prettifying(ttt "\n", ttt "\n" sss sss sss "\n\n\n", 0); - assert_message_prettifying(ttt "\n", ttt "\n\n" sss sss sss "\n\n", 0); - assert_message_prettifying(ttt "\n", ttt "\n\n\n" sss sss sss "\n\n", 0); -} - -void test_object_message__text_without_newline_at_end_should_end_with_newline(void) -{ - assert_message_prettifying(ttt "\n", ttt, 0); - assert_message_prettifying(ttt ttt "\n", ttt ttt, 0); - assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt, 0); - assert_message_prettifying(ttt ttt ttt ttt "\n", ttt ttt ttt ttt, 0); -} - -void test_object_message__text_plus_spaces_without_newline_should_not_show_spaces_and_end_with_newline(void) -{ - assert_message_prettifying(ttt "\n", ttt sss, 0); - assert_message_prettifying(ttt ttt "\n", ttt ttt sss, 0); - assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt sss, 0); - assert_message_prettifying(ttt "\n", ttt sss sss, 0); - assert_message_prettifying(ttt ttt "\n", ttt ttt sss sss, 0); - assert_message_prettifying(ttt "\n", ttt sss sss sss, 0); -} - -void test_object_message__text_plus_spaces_ending_with_newline_should_be_cleaned_and_newline_must_remain(void){ - assert_message_prettifying(ttt "\n", ttt sss "\n", 0); - assert_message_prettifying(ttt "\n", ttt sss sss "\n", 0); - assert_message_prettifying(ttt "\n", ttt sss sss sss "\n", 0); - assert_message_prettifying(ttt ttt "\n", ttt ttt sss "\n", 0); - assert_message_prettifying(ttt ttt "\n", ttt ttt sss sss "\n", 0); - assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt sss "\n", 0); -} - -void test_object_message__spaces_with_newline_at_end_should_be_replaced_with_empty_string(void) -{ - assert_message_prettifying("", sss "\n", 0); - assert_message_prettifying("", sss sss "\n", 0); - assert_message_prettifying("", sss sss sss "\n", 0); - assert_message_prettifying("", sss sss sss sss "\n", 0); -} - -void test_object_message__spaces_without_newline_at_end_should_be_replaced_with_empty_string(void) -{ - assert_message_prettifying("", "", 0); - assert_message_prettifying("", sss sss, 0); - assert_message_prettifying("", sss sss sss, 0); - assert_message_prettifying("", sss sss sss sss, 0); -} - -void test_object_message__consecutive_text_lines_should_be_unchanged(void) -{ - assert_message_prettifying(ttt ttt "\n" ttt "\n", ttt ttt "\n" ttt "\n", 0); - assert_message_prettifying(ttt "\n" ttt ttt "\n" ttt "\n", ttt "\n" ttt ttt "\n" ttt "\n", 0); - assert_message_prettifying(ttt "\n" ttt "\n" ttt "\n" ttt ttt "\n", ttt "\n" ttt "\n" ttt "\n" ttt ttt "\n", 0); - assert_message_prettifying(ttt "\n" ttt "\n\n" ttt ttt "\n" ttt "\n", ttt "\n" ttt "\n\n" ttt ttt "\n" ttt "\n", 0); - assert_message_prettifying(ttt ttt "\n\n" ttt "\n" ttt ttt "\n", ttt ttt "\n\n" ttt "\n" ttt ttt "\n", 0); - assert_message_prettifying(ttt "\n" ttt ttt "\n\n" ttt "\n", ttt "\n" ttt ttt "\n\n" ttt "\n", 0); -} - -void test_object_message__strip_comments(void) -{ - assert_message_prettifying("", "# comment", 1); - assert_message_prettifying("", "# comment\n", 1); - assert_message_prettifying("", "# comment \n", 1); - - assert_message_prettifying(ttt "\n", ttt "\n" "# comment\n", 1); - assert_message_prettifying(ttt "\n", "# comment\n" ttt "\n", 1); - assert_message_prettifying(ttt "\n" ttt "\n", ttt "\n" "# comment\n" ttt "\n", 1); -} - -void test_object_message__keep_comments(void) -{ - assert_message_prettifying("# comment\n", "# comment", 0); - assert_message_prettifying("# comment\n", "# comment\n", 0); - assert_message_prettifying("# comment\n", "# comment \n", 0); - - assert_message_prettifying(ttt "\n" "# comment\n", ttt "\n" "# comment\n", 0); - assert_message_prettifying("# comment\n" ttt "\n", "# comment\n" ttt "\n", 0); - assert_message_prettifying(ttt "\n" "# comment\n" ttt "\n", ttt "\n" "# comment\n" ttt "\n", 0); -} - -void test_object_message__message_prettify(void) -{ - char buffer[100]; - - cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 0) == 1); - cl_assert_equal_s(buffer, ""); - cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 1) == 1); - cl_assert_equal_s(buffer, ""); - - cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 0)); - cl_assert_equal_s("Short\n", buffer); - cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 1)); - cl_assert_equal_s("Short\n", buffer); - - cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 0) > 0); - cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n# with some comments still in\n"); - - cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 1) > 0); - cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n"); - - /* try out overflow */ - cl_assert(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "12345678", - 0) > 0); - cl_assert_equal_s(buffer, - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "12345678\n"); - - cl_assert(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "12345678\n", - 0) > 0); - cl_assert_equal_s(buffer, - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "12345678\n"); - - cl_git_fail(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "123456789", - 0)); - cl_git_fail(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "123456789\n", - 0)); - cl_git_fail(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890", - 0)); - cl_git_fail(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890""x", - 0)); - - cl_assert(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n" - "# 1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n" - "1234567890", - 1) > 0); - - cl_assert(git_message_prettify(NULL, 0, "", 0) == 1); - cl_assert(git_message_prettify(NULL, 0, "Short test", 0) == 12); - cl_assert(git_message_prettify(NULL, 0, "Test\n# with\nComments", 1) == 15); -} diff --git a/tests-clar/object/peel.c b/tests-clar/object/peel.c deleted file mode 100644 index bb0bbd096e6..00000000000 --- a/tests-clar/object/peel.c +++ /dev/null @@ -1,110 +0,0 @@ -#include "clar_libgit2.h" - -static git_repository *g_repo; - -void test_object_peel__initialize(void) -{ - cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git"))); -} - -void test_object_peel__cleanup(void) -{ - git_repository_free(g_repo); - g_repo = NULL; -} - -static void assert_peel( - const char *sha, - git_otype requested_type, - const char* expected_sha, - git_otype expected_type) -{ - git_oid oid, expected_oid; - git_object *obj; - git_object *peeled; - - cl_git_pass(git_oid_fromstr(&oid, sha)); - cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY)); - - cl_git_pass(git_object_peel(&peeled, obj, requested_type)); - - cl_git_pass(git_oid_fromstr(&expected_oid, expected_sha)); - cl_assert_equal_i(0, git_oid_cmp(&expected_oid, git_object_id(peeled))); - - cl_assert_equal_i(expected_type, git_object_type(peeled)); - - git_object_free(peeled); - git_object_free(obj); -} - -static void assert_peel_error(int error, const char *sha, git_otype requested_type) -{ - git_oid oid; - git_object *obj; - git_object *peeled; - - cl_git_pass(git_oid_fromstr(&oid, sha)); - cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY)); - - cl_assert_equal_i(error, git_object_peel(&peeled, obj, requested_type)); - - git_object_free(obj); -} - -void test_object_peel__peeling_an_object_into_its_own_type_returns_another_instance_of_it(void) -{ - assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT, - "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT); - assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_TAG, - "7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_TAG); - assert_peel("53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE, - "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE); - assert_peel("0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_BLOB, - "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_BLOB); -} - -void test_object_peel__can_peel_a_tag(void) -{ - assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_COMMIT, - "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT); - assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_TREE, - "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE); -} - -void test_object_peel__can_peel_a_commit(void) -{ - assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_TREE, - "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE); -} - -void test_object_peel__cannot_peel_a_tree(void) -{ - assert_peel_error(GIT_EAMBIGUOUS, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_BLOB); -} - -void test_object_peel__cannot_peel_a_blob(void) -{ - assert_peel_error(GIT_ENOTFOUND, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_COMMIT); -} - -void test_object_peel__target_any_object_for_type_change(void) -{ - /* tag to commit */ - assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_ANY, - "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT); - - /* commit to tree */ - assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_ANY, - "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE); - - /* fail to peel tree */ - assert_peel_error(GIT_EAMBIGUOUS, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_ANY); - - /* fail to peel blob */ - assert_peel_error(GIT_ENOTFOUND, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_ANY); -} - -void test_object_peel__should_use_a_well_known_type(void) -{ - assert_peel_error(GIT_EINVALIDSPEC, "7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ__EXT2); -} diff --git a/tests-clar/object/raw/chars.c b/tests-clar/object/raw/chars.c deleted file mode 100644 index 206bf7119ae..00000000000 --- a/tests-clar/object/raw/chars.c +++ /dev/null @@ -1,41 +0,0 @@ - -#include "clar_libgit2.h" - -#include "odb.h" - -void test_object_raw_chars__find_invalid_chars_in_oid(void) -{ - git_oid out; - unsigned char exp[] = { - 0x16, 0xa6, 0x77, 0x70, 0xb7, - 0xd8, 0xd7, 0x23, 0x17, 0xc4, - 0xb7, 0x75, 0x21, 0x3c, 0x23, - 0xa8, 0xbd, 0x74, 0xf5, 0xe0, - }; - char in[41] = "16a67770b7d8d72317c4b775213c23a8bd74f5e0"; - unsigned int i; - - for (i = 0; i < 256; i++) { - in[38] = (char)i; - if (git__fromhex(i) >= 0) { - exp[19] = (unsigned char)(git__fromhex(i) << 4); - cl_git_pass(git_oid_fromstr(&out, in)); - cl_assert(memcmp(out.id, exp, sizeof(out.id)) == 0); - } else { - cl_git_fail(git_oid_fromstr(&out, in)); - } - } -} - -void test_object_raw_chars__build_valid_oid_from_raw_bytes(void) -{ - git_oid out; - unsigned char exp[] = { - 0x16, 0xa6, 0x77, 0x70, 0xb7, - 0xd8, 0xd7, 0x23, 0x17, 0xc4, - 0xb7, 0x75, 0x21, 0x3c, 0x23, - 0xa8, 0xbd, 0x74, 0xf5, 0xe0, - }; - git_oid_fromraw(&out, exp); - cl_git_pass(memcmp(out.id, exp, sizeof(out.id))); -} diff --git a/tests-clar/object/raw/compare.c b/tests-clar/object/raw/compare.c deleted file mode 100644 index 1c9ce4b812d..00000000000 --- a/tests-clar/object/raw/compare.c +++ /dev/null @@ -1,124 +0,0 @@ - -#include "clar_libgit2.h" - -#include "odb.h" - -void test_object_raw_compare__succeed_on_copy_oid(void) -{ - git_oid a, b; - unsigned char exp[] = { - 0x16, 0xa6, 0x77, 0x70, 0xb7, - 0xd8, 0xd7, 0x23, 0x17, 0xc4, - 0xb7, 0x75, 0x21, 0x3c, 0x23, - 0xa8, 0xbd, 0x74, 0xf5, 0xe0, - }; - memset(&b, 0, sizeof(b)); - git_oid_fromraw(&a, exp); - git_oid_cpy(&b, &a); - cl_git_pass(memcmp(a.id, exp, sizeof(a.id))); -} - -void test_object_raw_compare__succeed_on_oid_comparison_lesser(void) -{ - git_oid a, b; - unsigned char a_in[] = { - 0x16, 0xa6, 0x77, 0x70, 0xb7, - 0xd8, 0xd7, 0x23, 0x17, 0xc4, - 0xb7, 0x75, 0x21, 0x3c, 0x23, - 0xa8, 0xbd, 0x74, 0xf5, 0xe0, - }; - unsigned char b_in[] = { - 0x16, 0xa6, 0x77, 0x70, 0xb7, - 0xd8, 0xd7, 0x23, 0x17, 0xc4, - 0xb7, 0x75, 0x21, 0x3c, 0x23, - 0xa8, 0xbd, 0x74, 0xf5, 0xf0, - }; - git_oid_fromraw(&a, a_in); - git_oid_fromraw(&b, b_in); - cl_assert(git_oid_cmp(&a, &b) < 0); -} - -void test_object_raw_compare__succeed_on_oid_comparison_equal(void) -{ - git_oid a, b; - unsigned char a_in[] = { - 0x16, 0xa6, 0x77, 0x70, 0xb7, - 0xd8, 0xd7, 0x23, 0x17, 0xc4, - 0xb7, 0x75, 0x21, 0x3c, 0x23, - 0xa8, 0xbd, 0x74, 0xf5, 0xe0, - }; - git_oid_fromraw(&a, a_in); - git_oid_fromraw(&b, a_in); - cl_assert(git_oid_cmp(&a, &b) == 0); -} - -void test_object_raw_compare__succeed_on_oid_comparison_greater(void) -{ - git_oid a, b; - unsigned char a_in[] = { - 0x16, 0xa6, 0x77, 0x70, 0xb7, - 0xd8, 0xd7, 0x23, 0x17, 0xc4, - 0xb7, 0x75, 0x21, 0x3c, 0x23, - 0xa8, 0xbd, 0x74, 0xf5, 0xe0, - }; - unsigned char b_in[] = { - 0x16, 0xa6, 0x77, 0x70, 0xb7, - 0xd8, 0xd7, 0x23, 0x17, 0xc4, - 0xb7, 0x75, 0x21, 0x3c, 0x23, - 0xa8, 0xbd, 0x74, 0xf5, 0xd0, - }; - git_oid_fromraw(&a, a_in); - git_oid_fromraw(&b, b_in); - cl_assert(git_oid_cmp(&a, &b) > 0); -} - -void test_object_raw_compare__compare_fmt_oids(void) -{ - const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0"; - git_oid in; - char out[GIT_OID_HEXSZ + 1]; - - cl_git_pass(git_oid_fromstr(&in, exp)); - - /* Format doesn't touch the last byte */ - out[GIT_OID_HEXSZ] = 'Z'; - git_oid_fmt(out, &in); - cl_assert(out[GIT_OID_HEXSZ] == 'Z'); - - /* Format produced the right result */ - out[GIT_OID_HEXSZ] = '\0'; - cl_assert_equal_s(exp, out); -} - -void test_object_raw_compare__compare_allocfmt_oids(void) -{ - const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0"; - git_oid in; - char *out; - - cl_git_pass(git_oid_fromstr(&in, exp)); - - out = git_oid_allocfmt(&in); - cl_assert(out); - cl_assert_equal_s(exp, out); - git__free(out); -} - -void test_object_raw_compare__compare_pathfmt_oids(void) -{ - const char *exp1 = "16a0123456789abcdef4b775213c23a8bd74f5e0"; - const char *exp2 = "16/a0123456789abcdef4b775213c23a8bd74f5e0"; - git_oid in; - char out[GIT_OID_HEXSZ + 2]; - - cl_git_pass(git_oid_fromstr(&in, exp1)); - - /* Format doesn't touch the last byte */ - out[GIT_OID_HEXSZ + 1] = 'Z'; - git_oid_pathfmt(out, &in); - cl_assert(out[GIT_OID_HEXSZ + 1] == 'Z'); - - /* Format produced the right result */ - out[GIT_OID_HEXSZ + 1] = '\0'; - cl_assert_equal_s(exp2, out); -} diff --git a/tests-clar/object/raw/convert.c b/tests-clar/object/raw/convert.c deleted file mode 100644 index 74442c15358..00000000000 --- a/tests-clar/object/raw/convert.c +++ /dev/null @@ -1,75 +0,0 @@ - -#include "clar_libgit2.h" - -#include "odb.h" - -void test_object_raw_convert__succeed_on_oid_to_string_conversion(void) -{ - const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0"; - git_oid in; - char out[GIT_OID_HEXSZ + 1]; - char *str; - int i; - - cl_git_pass(git_oid_fromstr(&in, exp)); - - /* NULL buffer pointer, returns static empty string */ - str = git_oid_tostr(NULL, sizeof(out), &in); - cl_assert(str && *str == '\0' && str != out); - - /* zero buffer size, returns static empty string */ - str = git_oid_tostr(out, 0, &in); - cl_assert(str && *str == '\0' && str != out); - - /* NULL oid pointer, sets existing buffer to empty string */ - str = git_oid_tostr(out, sizeof(out), NULL); - cl_assert(str && *str == '\0' && str == out); - - /* n == 1, returns out as an empty string */ - str = git_oid_tostr(out, 1, &in); - cl_assert(str && *str == '\0' && str == out); - - for (i = 1; i < GIT_OID_HEXSZ; i++) { - out[i+1] = 'Z'; - str = git_oid_tostr(out, i+1, &in); - /* returns out containing c-string */ - cl_assert(str && str == out); - /* must be '\0' terminated */ - cl_assert(*(str+i) == '\0'); - /* must not touch bytes past end of string */ - cl_assert(*(str+(i+1)) == 'Z'); - /* i == n-1 charaters of string */ - cl_git_pass(strncmp(exp, out, i)); - } - - /* returns out as hex formatted c-string */ - str = git_oid_tostr(out, sizeof(out), &in); - cl_assert(str && str == out && *(str+GIT_OID_HEXSZ) == '\0'); - cl_assert_equal_s(exp, out); -} - -void test_object_raw_convert__succeed_on_oid_to_string_conversion_big(void) -{ - const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0"; - git_oid in; - char big[GIT_OID_HEXSZ + 1 + 3]; /* note + 4 => big buffer */ - char *str; - - cl_git_pass(git_oid_fromstr(&in, exp)); - - /* place some tail material */ - big[GIT_OID_HEXSZ+0] = 'W'; /* should be '\0' afterwards */ - big[GIT_OID_HEXSZ+1] = 'X'; /* should remain untouched */ - big[GIT_OID_HEXSZ+2] = 'Y'; /* ditto */ - big[GIT_OID_HEXSZ+3] = 'Z'; /* ditto */ - - /* returns big as hex formatted c-string */ - str = git_oid_tostr(big, sizeof(big), &in); - cl_assert(str && str == big && *(str+GIT_OID_HEXSZ) == '\0'); - cl_assert_equal_s(exp, big); - - /* check tail material is untouched */ - cl_assert(str && str == big && *(str+GIT_OID_HEXSZ+1) == 'X'); - cl_assert(str && str == big && *(str+GIT_OID_HEXSZ+2) == 'Y'); - cl_assert(str && str == big && *(str+GIT_OID_HEXSZ+3) == 'Z'); -} diff --git a/tests-clar/object/raw/fromstr.c b/tests-clar/object/raw/fromstr.c deleted file mode 100644 index 8c11c105fd1..00000000000 --- a/tests-clar/object/raw/fromstr.c +++ /dev/null @@ -1,30 +0,0 @@ - -#include "clar_libgit2.h" - -#include "odb.h" - -void test_object_raw_fromstr__fail_on_invalid_oid_string(void) -{ - git_oid out; - cl_git_fail(git_oid_fromstr(&out, "")); - cl_git_fail(git_oid_fromstr(&out, "moo")); - cl_git_fail(git_oid_fromstr(&out, "16a67770b7d8d72317c4b775213c23a8bd74f5ez")); -} - -void test_object_raw_fromstr__succeed_on_valid_oid_string(void) -{ - git_oid out; - unsigned char exp[] = { - 0x16, 0xa6, 0x77, 0x70, 0xb7, - 0xd8, 0xd7, 0x23, 0x17, 0xc4, - 0xb7, 0x75, 0x21, 0x3c, 0x23, - 0xa8, 0xbd, 0x74, 0xf5, 0xe0, - }; - - cl_git_pass(git_oid_fromstr(&out, "16a67770b7d8d72317c4b775213c23a8bd74f5e0")); - cl_git_pass(memcmp(out.id, exp, sizeof(out.id))); - - cl_git_pass(git_oid_fromstr(&out, "16A67770B7D8D72317C4b775213C23A8BD74F5E0")); - cl_git_pass(memcmp(out.id, exp, sizeof(out.id))); - -} diff --git a/tests-clar/object/raw/hash.c b/tests-clar/object/raw/hash.c deleted file mode 100644 index f26035e45cc..00000000000 --- a/tests-clar/object/raw/hash.c +++ /dev/null @@ -1,166 +0,0 @@ - -#include "clar_libgit2.h" - -#include "odb.h" -#include "hash.h" - -#include "data.h" - -static void hash_object_pass(git_oid *oid, git_rawobj *obj) -{ - cl_git_pass(git_odb_hash(oid, obj->data, obj->len, obj->type)); -} -static void hash_object_fail(git_oid *oid, git_rawobj *obj) -{ - cl_git_fail(git_odb_hash(oid, obj->data, obj->len, obj->type)); -} - -static char *hello_id = "22596363b3de40b06f981fb85d82312e8c0ed511"; -static char *hello_text = "hello world\n"; - -static char *bye_id = "ce08fe4884650f067bd5703b6a59a8b3b3c99a09"; -static char *bye_text = "bye world\n"; - -void test_object_raw_hash__hash_by_blocks(void) -{ - git_hash_ctx ctx; - git_oid id1, id2; - - cl_git_pass(git_hash_ctx_init(&ctx)); - - /* should already be init'd */ - cl_git_pass(git_hash_update(&ctx, hello_text, strlen(hello_text))); - cl_git_pass(git_hash_final(&id2, &ctx)); - cl_git_pass(git_oid_fromstr(&id1, hello_id)); - cl_assert(git_oid_cmp(&id1, &id2) == 0); - - /* reinit should permit reuse */ - cl_git_pass(git_hash_init(&ctx)); - cl_git_pass(git_hash_update(&ctx, bye_text, strlen(bye_text))); - cl_git_pass(git_hash_final(&id2, &ctx)); - cl_git_pass(git_oid_fromstr(&id1, bye_id)); - cl_assert(git_oid_cmp(&id1, &id2) == 0); - - git_hash_ctx_cleanup(&ctx); -} - -void test_object_raw_hash__hash_buffer_in_single_call(void) -{ - git_oid id1, id2; - - cl_git_pass(git_oid_fromstr(&id1, hello_id)); - git_hash_buf(&id2, hello_text, strlen(hello_text)); - cl_assert(git_oid_cmp(&id1, &id2) == 0); -} - -void test_object_raw_hash__hash_vector(void) -{ - git_oid id1, id2; - git_buf_vec vec[2]; - - cl_git_pass(git_oid_fromstr(&id1, hello_id)); - - vec[0].data = hello_text; - vec[0].len = 4; - vec[1].data = hello_text+4; - vec[1].len = strlen(hello_text)-4; - - git_hash_vec(&id2, vec, 2); - - cl_assert(git_oid_cmp(&id1, &id2) == 0); -} - -void test_object_raw_hash__hash_junk_data(void) -{ - git_oid id, id_zero; - - cl_git_pass(git_oid_fromstr(&id_zero, zero_id)); - - /* invalid types: */ - junk_obj.data = some_data; - hash_object_fail(&id, &junk_obj); - - junk_obj.type = GIT_OBJ__EXT1; - hash_object_fail(&id, &junk_obj); - - junk_obj.type = GIT_OBJ__EXT2; - hash_object_fail(&id, &junk_obj); - - junk_obj.type = GIT_OBJ_OFS_DELTA; - hash_object_fail(&id, &junk_obj); - - junk_obj.type = GIT_OBJ_REF_DELTA; - hash_object_fail(&id, &junk_obj); - - /* data can be NULL only if len is zero: */ - junk_obj.type = GIT_OBJ_BLOB; - junk_obj.data = NULL; - hash_object_pass(&id, &junk_obj); - cl_assert(git_oid_cmp(&id, &id_zero) == 0); - - junk_obj.len = 1; - hash_object_fail(&id, &junk_obj); -} - -void test_object_raw_hash__hash_commit_object(void) -{ - git_oid id1, id2; - - cl_git_pass(git_oid_fromstr(&id1, commit_id)); - hash_object_pass(&id2, &commit_obj); - cl_assert(git_oid_cmp(&id1, &id2) == 0); -} - -void test_object_raw_hash__hash_tree_object(void) -{ - git_oid id1, id2; - - cl_git_pass(git_oid_fromstr(&id1, tree_id)); - hash_object_pass(&id2, &tree_obj); - cl_assert(git_oid_cmp(&id1, &id2) == 0); -} - -void test_object_raw_hash__hash_tag_object(void) -{ - git_oid id1, id2; - - cl_git_pass(git_oid_fromstr(&id1, tag_id)); - hash_object_pass(&id2, &tag_obj); - cl_assert(git_oid_cmp(&id1, &id2) == 0); -} - -void test_object_raw_hash__hash_zero_length_object(void) -{ - git_oid id1, id2; - - cl_git_pass(git_oid_fromstr(&id1, zero_id)); - hash_object_pass(&id2, &zero_obj); - cl_assert(git_oid_cmp(&id1, &id2) == 0); -} - -void test_object_raw_hash__hash_one_byte_object(void) -{ - git_oid id1, id2; - - cl_git_pass(git_oid_fromstr(&id1, one_id)); - hash_object_pass(&id2, &one_obj); - cl_assert(git_oid_cmp(&id1, &id2) == 0); -} - -void test_object_raw_hash__hash_two_byte_object(void) -{ - git_oid id1, id2; - - cl_git_pass(git_oid_fromstr(&id1, two_id)); - hash_object_pass(&id2, &two_obj); - cl_assert(git_oid_cmp(&id1, &id2) == 0); -} - -void test_object_raw_hash__hash_multi_byte_object(void) -{ - git_oid id1, id2; - - cl_git_pass(git_oid_fromstr(&id1, some_id)); - hash_object_pass(&id2, &some_obj); - cl_assert(git_oid_cmp(&id1, &id2) == 0); -} diff --git a/tests-clar/object/raw/short.c b/tests-clar/object/raw/short.c deleted file mode 100644 index 93c79b6a58a..00000000000 --- a/tests-clar/object/raw/short.c +++ /dev/null @@ -1,94 +0,0 @@ - -#include "clar_libgit2.h" - -#include "odb.h" -#include "hash.h" - -void test_object_raw_short__oid_shortener_no_duplicates(void) -{ - git_oid_shorten *os; - int min_len; - - os = git_oid_shorten_new(0); - cl_assert(os != NULL); - - git_oid_shorten_add(os, "22596363b3de40b06f981fb85d82312e8c0ed511"); - git_oid_shorten_add(os, "ce08fe4884650f067bd5703b6a59a8b3b3c99a09"); - git_oid_shorten_add(os, "16a0123456789abcdef4b775213c23a8bd74f5e0"); - min_len = git_oid_shorten_add(os, "ce08fe4884650f067bd5703b6a59a8b3b3c99a09"); - - cl_assert(min_len == GIT_OID_HEXSZ + 1); - - git_oid_shorten_free(os); -} - -void test_object_raw_short__oid_shortener_stresstest_git_oid_shorten(void) -{ -#define MAX_OIDS 1000 - - git_oid_shorten *os; - char *oids[MAX_OIDS]; - char number_buffer[16]; - git_oid oid; - size_t i, j; - - int min_len = 0, found_collision; - - os = git_oid_shorten_new(0); - cl_assert(os != NULL); - - /* - * Insert in the shortener 1000 unique SHA1 ids - */ - for (i = 0; i < MAX_OIDS; ++i) { - char *oid_text; - - p_snprintf(number_buffer, 16, "%u", (unsigned int)i); - git_hash_buf(&oid, number_buffer, strlen(number_buffer)); - - oid_text = git__malloc(GIT_OID_HEXSZ + 1); - git_oid_fmt(oid_text, &oid); - oid_text[GIT_OID_HEXSZ] = 0; - - min_len = git_oid_shorten_add(os, oid_text); - cl_assert(min_len >= 0); - - oids[i] = oid_text; - } - - /* - * Compare the first `min_char - 1` characters of each - * SHA1 OID. If the minimizer worked, we should find at - * least one collision - */ - found_collision = 0; - for (i = 0; i < MAX_OIDS; ++i) { - for (j = 0; j < MAX_OIDS; ++j) { - if (i != j && memcmp(oids[i], oids[j], min_len - 1) == 0) - found_collision = 1; - } - } - cl_assert(found_collision == 1); - - /* - * Compare the first `min_char` characters of each - * SHA1 OID. If the minimizer worked, every single preffix - * should be unique. - */ - found_collision = 0; - for (i = 0; i < MAX_OIDS; ++i) { - for (j = 0; j < MAX_OIDS; ++j) { - if (i != j && memcmp(oids[i], oids[j], min_len) == 0) - found_collision = 1; - } - } - cl_assert(found_collision == 0); - - /* cleanup */ - for (i = 0; i < MAX_OIDS; ++i) - git__free(oids[i]); - - git_oid_shorten_free(os); - -#undef MAX_OIDS -} diff --git a/tests-clar/object/raw/size.c b/tests-clar/object/raw/size.c deleted file mode 100644 index 930c6de233c..00000000000 --- a/tests-clar/object/raw/size.c +++ /dev/null @@ -1,13 +0,0 @@ - -#include "clar_libgit2.h" - -#include "odb.h" - -void test_object_raw_size__validate_oid_size(void) -{ - git_oid out; - cl_assert(20 == GIT_OID_RAWSZ); - cl_assert(40 == GIT_OID_HEXSZ); - cl_assert(sizeof(out) == GIT_OID_RAWSZ); - cl_assert(sizeof(out.id) == GIT_OID_RAWSZ); -} diff --git a/tests-clar/object/raw/type2string.c b/tests-clar/object/raw/type2string.c deleted file mode 100644 index a3585487f65..00000000000 --- a/tests-clar/object/raw/type2string.c +++ /dev/null @@ -1,54 +0,0 @@ - -#include "clar_libgit2.h" - -#include "odb.h" -#include "hash.h" - -void test_object_raw_type2string__convert_type_to_string(void) -{ - cl_assert_equal_s(git_object_type2string(GIT_OBJ_BAD), ""); - cl_assert_equal_s(git_object_type2string(GIT_OBJ__EXT1), ""); - cl_assert_equal_s(git_object_type2string(GIT_OBJ_COMMIT), "commit"); - cl_assert_equal_s(git_object_type2string(GIT_OBJ_TREE), "tree"); - cl_assert_equal_s(git_object_type2string(GIT_OBJ_BLOB), "blob"); - cl_assert_equal_s(git_object_type2string(GIT_OBJ_TAG), "tag"); - cl_assert_equal_s(git_object_type2string(GIT_OBJ__EXT2), ""); - cl_assert_equal_s(git_object_type2string(GIT_OBJ_OFS_DELTA), "OFS_DELTA"); - cl_assert_equal_s(git_object_type2string(GIT_OBJ_REF_DELTA), "REF_DELTA"); - - cl_assert_equal_s(git_object_type2string(-2), ""); - cl_assert_equal_s(git_object_type2string(8), ""); - cl_assert_equal_s(git_object_type2string(1234), ""); -} - -void test_object_raw_type2string__convert_string_to_type(void) -{ - cl_assert(git_object_string2type(NULL) == GIT_OBJ_BAD); - cl_assert(git_object_string2type("") == GIT_OBJ_BAD); - cl_assert(git_object_string2type("commit") == GIT_OBJ_COMMIT); - cl_assert(git_object_string2type("tree") == GIT_OBJ_TREE); - cl_assert(git_object_string2type("blob") == GIT_OBJ_BLOB); - cl_assert(git_object_string2type("tag") == GIT_OBJ_TAG); - cl_assert(git_object_string2type("OFS_DELTA") == GIT_OBJ_OFS_DELTA); - cl_assert(git_object_string2type("REF_DELTA") == GIT_OBJ_REF_DELTA); - - cl_assert(git_object_string2type("CoMmIt") == GIT_OBJ_BAD); - cl_assert(git_object_string2type("hohoho") == GIT_OBJ_BAD); -} - -void test_object_raw_type2string__check_type_is_loose(void) -{ - cl_assert(git_object_typeisloose(GIT_OBJ_BAD) == 0); - cl_assert(git_object_typeisloose(GIT_OBJ__EXT1) == 0); - cl_assert(git_object_typeisloose(GIT_OBJ_COMMIT) == 1); - cl_assert(git_object_typeisloose(GIT_OBJ_TREE) == 1); - cl_assert(git_object_typeisloose(GIT_OBJ_BLOB) == 1); - cl_assert(git_object_typeisloose(GIT_OBJ_TAG) == 1); - cl_assert(git_object_typeisloose(GIT_OBJ__EXT2) == 0); - cl_assert(git_object_typeisloose(GIT_OBJ_OFS_DELTA) == 0); - cl_assert(git_object_typeisloose(GIT_OBJ_REF_DELTA) == 0); - - cl_assert(git_object_typeisloose(-2) == 0); - cl_assert(git_object_typeisloose(8) == 0); - cl_assert(git_object_typeisloose(1234) == 0); -} diff --git a/tests-clar/object/raw/write.c b/tests-clar/object/raw/write.c deleted file mode 100644 index 1b28d0df7e4..00000000000 --- a/tests-clar/object/raw/write.c +++ /dev/null @@ -1,455 +0,0 @@ - -#include "clar_libgit2.h" -#include "fileops.h" -#include "odb.h" - -typedef struct object_data { - char *id; /* object id (sha1) */ - char *dir; /* object store (fan-out) directory name */ - char *file; /* object store filename */ -} object_data; - -static const char *odb_dir = "test-objects"; - -void test_body(object_data *d, git_rawobj *o); - - - -// Helpers -static void remove_object_files(object_data *d) -{ - cl_git_pass(p_unlink(d->file)); - cl_git_pass(p_rmdir(d->dir)); - cl_assert(errno != ENOTEMPTY); - cl_git_pass(p_rmdir(odb_dir) < 0); -} - -static void streaming_write(git_oid *oid, git_odb *odb, git_rawobj *raw) -{ - git_odb_stream *stream; - int error; - - cl_git_pass(git_odb_open_wstream(&stream, odb, raw->len, raw->type)); - stream->write(stream, raw->data, raw->len); - error = stream->finalize_write(oid, stream); - stream->free(stream); - cl_git_pass(error); -} - -static void check_object_files(object_data *d) -{ - cl_assert(git_path_exists(d->dir)); - cl_assert(git_path_exists(d->file)); -} - -static void cmp_objects(git_rawobj *o1, git_rawobj *o2) -{ - cl_assert(o1->type == o2->type); - cl_assert(o1->len == o2->len); - if (o1->len > 0) - cl_assert(memcmp(o1->data, o2->data, o1->len) == 0); -} - -static void make_odb_dir(void) -{ - cl_git_pass(p_mkdir(odb_dir, GIT_OBJECT_DIR_MODE)); -} - - -// Standard test form -void test_body(object_data *d, git_rawobj *o) -{ - git_odb *db; - git_oid id1, id2; - git_odb_object *obj; - - make_odb_dir(); - cl_git_pass(git_odb_open(&db, odb_dir)); - cl_git_pass(git_oid_fromstr(&id1, d->id)); - - streaming_write(&id2, db, o); - cl_assert(git_oid_cmp(&id1, &id2) == 0); - check_object_files(d); - - cl_git_pass(git_odb_read(&obj, db, &id1)); - cmp_objects(&obj->raw, o); - - git_odb_object_free(obj); - git_odb_free(db); - remove_object_files(d); -} - - -void test_object_raw_write__loose_object(void) -{ - object_data commit = { - "3d7f8a6af076c8c3f20071a8935cdbe8228594d1", - "test-objects/3d", - "test-objects/3d/7f8a6af076c8c3f20071a8935cdbe8228594d1", - }; - - unsigned char commit_data[] = { - 0x74, 0x72, 0x65, 0x65, 0x20, 0x64, 0x66, 0x66, - 0x32, 0x64, 0x61, 0x39, 0x30, 0x62, 0x32, 0x35, - 0x34, 0x65, 0x31, 0x62, 0x65, 0x62, 0x38, 0x38, - 0x39, 0x64, 0x31, 0x66, 0x31, 0x66, 0x31, 0x32, - 0x38, 0x38, 0x62, 0x65, 0x31, 0x38, 0x30, 0x33, - 0x37, 0x38, 0x32, 0x64, 0x66, 0x0a, 0x61, 0x75, - 0x74, 0x68, 0x6f, 0x72, 0x20, 0x41, 0x20, 0x55, - 0x20, 0x54, 0x68, 0x6f, 0x72, 0x20, 0x3c, 0x61, - 0x75, 0x74, 0x68, 0x6f, 0x72, 0x40, 0x65, 0x78, - 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, - 0x6d, 0x3e, 0x20, 0x31, 0x32, 0x32, 0x37, 0x38, - 0x31, 0x34, 0x32, 0x39, 0x37, 0x20, 0x2b, 0x30, - 0x30, 0x30, 0x30, 0x0a, 0x63, 0x6f, 0x6d, 0x6d, - 0x69, 0x74, 0x74, 0x65, 0x72, 0x20, 0x43, 0x20, - 0x4f, 0x20, 0x4d, 0x69, 0x74, 0x74, 0x65, 0x72, - 0x20, 0x3c, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, - 0x74, 0x65, 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d, - 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, - 0x20, 0x31, 0x32, 0x32, 0x37, 0x38, 0x31, 0x34, - 0x32, 0x39, 0x37, 0x20, 0x2b, 0x30, 0x30, 0x30, - 0x30, 0x0a, 0x0a, 0x41, 0x20, 0x6f, 0x6e, 0x65, - 0x2d, 0x6c, 0x69, 0x6e, 0x65, 0x20, 0x63, 0x6f, - 0x6d, 0x6d, 0x69, 0x74, 0x20, 0x73, 0x75, 0x6d, - 0x6d, 0x61, 0x72, 0x79, 0x0a, 0x0a, 0x54, 0x68, - 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x6f, - 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f, - 0x6d, 0x6d, 0x69, 0x74, 0x20, 0x6d, 0x65, 0x73, - 0x73, 0x61, 0x67, 0x65, 0x2c, 0x20, 0x63, 0x6f, - 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x69, 0x6e, 0x67, - 0x20, 0x66, 0x75, 0x72, 0x74, 0x68, 0x65, 0x72, - 0x20, 0x65, 0x78, 0x70, 0x6c, 0x61, 0x6e, 0x61, - 0x74, 0x69, 0x6f, 0x6e, 0x0a, 0x6f, 0x66, 0x20, - 0x74, 0x68, 0x65, 0x20, 0x70, 0x75, 0x72, 0x70, - 0x6f, 0x73, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x74, - 0x68, 0x65, 0x20, 0x63, 0x68, 0x61, 0x6e, 0x67, - 0x65, 0x73, 0x20, 0x69, 0x6e, 0x74, 0x72, 0x6f, - 0x64, 0x75, 0x63, 0x65, 0x64, 0x20, 0x62, 0x79, - 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f, 0x6d, - 0x6d, 0x69, 0x74, 0x2e, 0x0a, 0x0a, 0x53, 0x69, - 0x67, 0x6e, 0x65, 0x64, 0x2d, 0x6f, 0x66, 0x2d, - 0x62, 0x79, 0x3a, 0x20, 0x41, 0x20, 0x55, 0x20, - 0x54, 0x68, 0x6f, 0x72, 0x20, 0x3c, 0x61, 0x75, - 0x74, 0x68, 0x6f, 0x72, 0x40, 0x65, 0x78, 0x61, - 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, - 0x3e, 0x0a, - }; - - git_rawobj commit_obj = { - commit_data, - sizeof(commit_data), - GIT_OBJ_COMMIT - }; - - test_body(&commit, &commit_obj); -} - -void test_object_raw_write__loose_tree(void) -{ - static object_data tree = { - "dff2da90b254e1beb889d1f1f1288be1803782df", - "test-objects/df", - "test-objects/df/f2da90b254e1beb889d1f1f1288be1803782df", - }; - - static unsigned char tree_data[] = { - 0x31, 0x30, 0x30, 0x36, 0x34, 0x34, 0x20, 0x6f, - 0x6e, 0x65, 0x00, 0x8b, 0x13, 0x78, 0x91, 0x79, - 0x1f, 0xe9, 0x69, 0x27, 0xad, 0x78, 0xe6, 0x4b, - 0x0a, 0xad, 0x7b, 0xde, 0xd0, 0x8b, 0xdc, 0x31, - 0x30, 0x30, 0x36, 0x34, 0x34, 0x20, 0x73, 0x6f, - 0x6d, 0x65, 0x00, 0xfd, 0x84, 0x30, 0xbc, 0x86, - 0x4c, 0xfc, 0xd5, 0xf1, 0x0e, 0x55, 0x90, 0xf8, - 0xa4, 0x47, 0xe0, 0x1b, 0x94, 0x2b, 0xfe, 0x31, - 0x30, 0x30, 0x36, 0x34, 0x34, 0x20, 0x74, 0x77, - 0x6f, 0x00, 0x78, 0x98, 0x19, 0x22, 0x61, 0x3b, - 0x2a, 0xfb, 0x60, 0x25, 0x04, 0x2f, 0xf6, 0xbd, - 0x87, 0x8a, 0xc1, 0x99, 0x4e, 0x85, 0x31, 0x30, - 0x30, 0x36, 0x34, 0x34, 0x20, 0x7a, 0x65, 0x72, - 0x6f, 0x00, 0xe6, 0x9d, 0xe2, 0x9b, 0xb2, 0xd1, - 0xd6, 0x43, 0x4b, 0x8b, 0x29, 0xae, 0x77, 0x5a, - 0xd8, 0xc2, 0xe4, 0x8c, 0x53, 0x91, - }; - - static git_rawobj tree_obj = { - tree_data, - sizeof(tree_data), - GIT_OBJ_TREE - }; - - test_body(&tree, &tree_obj); -} - -void test_object_raw_write__loose_tag(void) -{ - static object_data tag = { - "09d373e1dfdc16b129ceec6dd649739911541e05", - "test-objects/09", - "test-objects/09/d373e1dfdc16b129ceec6dd649739911541e05", - }; - - static unsigned char tag_data[] = { - 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x20, 0x33, - 0x64, 0x37, 0x66, 0x38, 0x61, 0x36, 0x61, 0x66, - 0x30, 0x37, 0x36, 0x63, 0x38, 0x63, 0x33, 0x66, - 0x32, 0x30, 0x30, 0x37, 0x31, 0x61, 0x38, 0x39, - 0x33, 0x35, 0x63, 0x64, 0x62, 0x65, 0x38, 0x32, - 0x32, 0x38, 0x35, 0x39, 0x34, 0x64, 0x31, 0x0a, - 0x74, 0x79, 0x70, 0x65, 0x20, 0x63, 0x6f, 0x6d, - 0x6d, 0x69, 0x74, 0x0a, 0x74, 0x61, 0x67, 0x20, - 0x76, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x0a, 0x74, - 0x61, 0x67, 0x67, 0x65, 0x72, 0x20, 0x43, 0x20, - 0x4f, 0x20, 0x4d, 0x69, 0x74, 0x74, 0x65, 0x72, - 0x20, 0x3c, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, - 0x74, 0x65, 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d, - 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, - 0x20, 0x31, 0x32, 0x32, 0x37, 0x38, 0x31, 0x34, - 0x32, 0x39, 0x37, 0x20, 0x2b, 0x30, 0x30, 0x30, - 0x30, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, - 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, - 0x61, 0x67, 0x20, 0x6f, 0x62, 0x6a, 0x65, 0x63, - 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x72, 0x65, - 0x6c, 0x65, 0x61, 0x73, 0x65, 0x20, 0x76, 0x30, - 0x2e, 0x30, 0x2e, 0x31, 0x0a, - }; - - static git_rawobj tag_obj = { - tag_data, - sizeof(tag_data), - GIT_OBJ_TAG - }; - - - test_body(&tag, &tag_obj); -} - -void test_object_raw_write__zero_length(void) -{ - static object_data zero = { - "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", - "test-objects/e6", - "test-objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391", - }; - - static unsigned char zero_data[] = { - 0x00 /* dummy data */ - }; - - static git_rawobj zero_obj = { - zero_data, - 0, - GIT_OBJ_BLOB - }; - - test_body(&zero, &zero_obj); -} - -void test_object_raw_write__one_byte(void) -{ - static object_data one = { - "8b137891791fe96927ad78e64b0aad7bded08bdc", - "test-objects/8b", - "test-objects/8b/137891791fe96927ad78e64b0aad7bded08bdc", - }; - - static unsigned char one_data[] = { - 0x0a, - }; - - static git_rawobj one_obj = { - one_data, - sizeof(one_data), - GIT_OBJ_BLOB - }; - - test_body(&one, &one_obj); -} - -void test_object_raw_write__two_byte(void) -{ - static object_data two = { - "78981922613b2afb6025042ff6bd878ac1994e85", - "test-objects/78", - "test-objects/78/981922613b2afb6025042ff6bd878ac1994e85", - }; - - static unsigned char two_data[] = { - 0x61, 0x0a, - }; - - static git_rawobj two_obj = { - two_data, - sizeof(two_data), - GIT_OBJ_BLOB - }; - - test_body(&two, &two_obj); -} - -void test_object_raw_write__several_bytes(void) -{ - static object_data some = { - "fd8430bc864cfcd5f10e5590f8a447e01b942bfe", - "test-objects/fd", - "test-objects/fd/8430bc864cfcd5f10e5590f8a447e01b942bfe", - }; - - static unsigned char some_data[] = { - 0x2f, 0x2a, 0x0a, 0x20, 0x2a, 0x20, 0x54, 0x68, - 0x69, 0x73, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x20, - 0x69, 0x73, 0x20, 0x66, 0x72, 0x65, 0x65, 0x20, - 0x73, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, - 0x3b, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x63, 0x61, - 0x6e, 0x20, 0x72, 0x65, 0x64, 0x69, 0x73, 0x74, - 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x69, - 0x74, 0x20, 0x61, 0x6e, 0x64, 0x2f, 0x6f, 0x72, - 0x20, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x79, 0x0a, - 0x20, 0x2a, 0x20, 0x69, 0x74, 0x20, 0x75, 0x6e, - 0x64, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, - 0x74, 0x65, 0x72, 0x6d, 0x73, 0x20, 0x6f, 0x66, - 0x20, 0x74, 0x68, 0x65, 0x20, 0x47, 0x4e, 0x55, - 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, - 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20, - 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x2c, - 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, - 0x20, 0x32, 0x2c, 0x0a, 0x20, 0x2a, 0x20, 0x61, - 0x73, 0x20, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x73, - 0x68, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x74, - 0x68, 0x65, 0x20, 0x46, 0x72, 0x65, 0x65, 0x20, - 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, - 0x20, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x74, - 0x69, 0x6f, 0x6e, 0x2e, 0x0a, 0x20, 0x2a, 0x0a, - 0x20, 0x2a, 0x20, 0x49, 0x6e, 0x20, 0x61, 0x64, - 0x64, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x74, - 0x6f, 0x20, 0x74, 0x68, 0x65, 0x20, 0x70, 0x65, - 0x72, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f, 0x6e, - 0x73, 0x20, 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, - 0x20, 0x47, 0x4e, 0x55, 0x20, 0x47, 0x65, 0x6e, - 0x65, 0x72, 0x61, 0x6c, 0x20, 0x50, 0x75, 0x62, - 0x6c, 0x69, 0x63, 0x20, 0x4c, 0x69, 0x63, 0x65, - 0x6e, 0x73, 0x65, 0x2c, 0x0a, 0x20, 0x2a, 0x20, - 0x74, 0x68, 0x65, 0x20, 0x61, 0x75, 0x74, 0x68, - 0x6f, 0x72, 0x73, 0x20, 0x67, 0x69, 0x76, 0x65, - 0x20, 0x79, 0x6f, 0x75, 0x20, 0x75, 0x6e, 0x6c, - 0x69, 0x6d, 0x69, 0x74, 0x65, 0x64, 0x20, 0x70, - 0x65, 0x72, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f, - 0x6e, 0x20, 0x74, 0x6f, 0x20, 0x6c, 0x69, 0x6e, - 0x6b, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f, - 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x64, 0x0a, 0x20, - 0x2a, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, - 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x69, - 0x73, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x20, 0x69, - 0x6e, 0x74, 0x6f, 0x20, 0x63, 0x6f, 0x6d, 0x62, - 0x69, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, - 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x6f, 0x74, - 0x68, 0x65, 0x72, 0x20, 0x70, 0x72, 0x6f, 0x67, - 0x72, 0x61, 0x6d, 0x73, 0x2c, 0x0a, 0x20, 0x2a, - 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x6f, 0x20, - 0x64, 0x69, 0x73, 0x74, 0x72, 0x69, 0x62, 0x75, - 0x74, 0x65, 0x20, 0x74, 0x68, 0x6f, 0x73, 0x65, - 0x20, 0x63, 0x6f, 0x6d, 0x62, 0x69, 0x6e, 0x61, - 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x77, 0x69, - 0x74, 0x68, 0x6f, 0x75, 0x74, 0x20, 0x61, 0x6e, - 0x79, 0x20, 0x72, 0x65, 0x73, 0x74, 0x72, 0x69, - 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x0a, 0x20, 0x2a, - 0x20, 0x63, 0x6f, 0x6d, 0x69, 0x6e, 0x67, 0x20, - 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x74, 0x68, 0x65, - 0x20, 0x75, 0x73, 0x65, 0x20, 0x6f, 0x66, 0x20, - 0x74, 0x68, 0x69, 0x73, 0x20, 0x66, 0x69, 0x6c, - 0x65, 0x2e, 0x20, 0x20, 0x28, 0x54, 0x68, 0x65, - 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, - 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20, - 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x0a, - 0x20, 0x2a, 0x20, 0x72, 0x65, 0x73, 0x74, 0x72, - 0x69, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, - 0x64, 0x6f, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x79, - 0x20, 0x69, 0x6e, 0x20, 0x6f, 0x74, 0x68, 0x65, - 0x72, 0x20, 0x72, 0x65, 0x73, 0x70, 0x65, 0x63, - 0x74, 0x73, 0x3b, 0x20, 0x66, 0x6f, 0x72, 0x20, - 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2c, - 0x20, 0x74, 0x68, 0x65, 0x79, 0x20, 0x63, 0x6f, - 0x76, 0x65, 0x72, 0x0a, 0x20, 0x2a, 0x20, 0x6d, - 0x6f, 0x64, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, - 0x69, 0x6f, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, - 0x68, 0x65, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2c, - 0x20, 0x61, 0x6e, 0x64, 0x20, 0x64, 0x69, 0x73, - 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x69, 0x6f, - 0x6e, 0x20, 0x77, 0x68, 0x65, 0x6e, 0x20, 0x6e, - 0x6f, 0x74, 0x20, 0x6c, 0x69, 0x6e, 0x6b, 0x65, - 0x64, 0x20, 0x69, 0x6e, 0x74, 0x6f, 0x0a, 0x20, - 0x2a, 0x20, 0x61, 0x20, 0x63, 0x6f, 0x6d, 0x62, - 0x69, 0x6e, 0x65, 0x64, 0x20, 0x65, 0x78, 0x65, - 0x63, 0x75, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x2e, - 0x29, 0x0a, 0x20, 0x2a, 0x0a, 0x20, 0x2a, 0x20, - 0x54, 0x68, 0x69, 0x73, 0x20, 0x66, 0x69, 0x6c, - 0x65, 0x20, 0x69, 0x73, 0x20, 0x64, 0x69, 0x73, - 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x64, - 0x20, 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, - 0x68, 0x6f, 0x70, 0x65, 0x20, 0x74, 0x68, 0x61, - 0x74, 0x20, 0x69, 0x74, 0x20, 0x77, 0x69, 0x6c, - 0x6c, 0x20, 0x62, 0x65, 0x20, 0x75, 0x73, 0x65, - 0x66, 0x75, 0x6c, 0x2c, 0x20, 0x62, 0x75, 0x74, - 0x0a, 0x20, 0x2a, 0x20, 0x57, 0x49, 0x54, 0x48, - 0x4f, 0x55, 0x54, 0x20, 0x41, 0x4e, 0x59, 0x20, - 0x57, 0x41, 0x52, 0x52, 0x41, 0x4e, 0x54, 0x59, - 0x3b, 0x20, 0x77, 0x69, 0x74, 0x68, 0x6f, 0x75, - 0x74, 0x20, 0x65, 0x76, 0x65, 0x6e, 0x20, 0x74, - 0x68, 0x65, 0x20, 0x69, 0x6d, 0x70, 0x6c, 0x69, - 0x65, 0x64, 0x20, 0x77, 0x61, 0x72, 0x72, 0x61, - 0x6e, 0x74, 0x79, 0x20, 0x6f, 0x66, 0x0a, 0x20, - 0x2a, 0x20, 0x4d, 0x45, 0x52, 0x43, 0x48, 0x41, - 0x4e, 0x54, 0x41, 0x42, 0x49, 0x4c, 0x49, 0x54, - 0x59, 0x20, 0x6f, 0x72, 0x20, 0x46, 0x49, 0x54, - 0x4e, 0x45, 0x53, 0x53, 0x20, 0x46, 0x4f, 0x52, - 0x20, 0x41, 0x20, 0x50, 0x41, 0x52, 0x54, 0x49, - 0x43, 0x55, 0x4c, 0x41, 0x52, 0x20, 0x50, 0x55, - 0x52, 0x50, 0x4f, 0x53, 0x45, 0x2e, 0x20, 0x20, - 0x53, 0x65, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, - 0x47, 0x4e, 0x55, 0x0a, 0x20, 0x2a, 0x20, 0x47, - 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x50, - 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20, 0x4c, 0x69, - 0x63, 0x65, 0x6e, 0x73, 0x65, 0x20, 0x66, 0x6f, - 0x72, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x64, - 0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 0x2e, 0x0a, - 0x20, 0x2a, 0x0a, 0x20, 0x2a, 0x20, 0x59, 0x6f, - 0x75, 0x20, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64, - 0x20, 0x68, 0x61, 0x76, 0x65, 0x20, 0x72, 0x65, - 0x63, 0x65, 0x69, 0x76, 0x65, 0x64, 0x20, 0x61, - 0x20, 0x63, 0x6f, 0x70, 0x79, 0x20, 0x6f, 0x66, - 0x20, 0x74, 0x68, 0x65, 0x20, 0x47, 0x4e, 0x55, - 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, - 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20, - 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x0a, - 0x20, 0x2a, 0x20, 0x61, 0x6c, 0x6f, 0x6e, 0x67, - 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x74, 0x68, - 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, - 0x61, 0x6d, 0x3b, 0x20, 0x73, 0x65, 0x65, 0x20, - 0x74, 0x68, 0x65, 0x20, 0x66, 0x69, 0x6c, 0x65, - 0x20, 0x43, 0x4f, 0x50, 0x59, 0x49, 0x4e, 0x47, - 0x2e, 0x20, 0x20, 0x49, 0x66, 0x20, 0x6e, 0x6f, - 0x74, 0x2c, 0x20, 0x77, 0x72, 0x69, 0x74, 0x65, - 0x20, 0x74, 0x6f, 0x0a, 0x20, 0x2a, 0x20, 0x74, - 0x68, 0x65, 0x20, 0x46, 0x72, 0x65, 0x65, 0x20, - 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, - 0x20, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x74, - 0x69, 0x6f, 0x6e, 0x2c, 0x20, 0x35, 0x31, 0x20, - 0x46, 0x72, 0x61, 0x6e, 0x6b, 0x6c, 0x69, 0x6e, - 0x20, 0x53, 0x74, 0x72, 0x65, 0x65, 0x74, 0x2c, - 0x20, 0x46, 0x69, 0x66, 0x74, 0x68, 0x20, 0x46, - 0x6c, 0x6f, 0x6f, 0x72, 0x2c, 0x0a, 0x20, 0x2a, - 0x20, 0x42, 0x6f, 0x73, 0x74, 0x6f, 0x6e, 0x2c, - 0x20, 0x4d, 0x41, 0x20, 0x30, 0x32, 0x31, 0x31, - 0x30, 0x2d, 0x31, 0x33, 0x30, 0x31, 0x2c, 0x20, - 0x55, 0x53, 0x41, 0x2e, 0x0a, 0x20, 0x2a, 0x2f, - 0x0a, - }; - - static git_rawobj some_obj = { - some_data, - sizeof(some_data), - GIT_OBJ_BLOB - }; - - test_body(&some, &some_obj); -} diff --git a/tests-clar/object/tag/list.c b/tests-clar/object/tag/list.c deleted file mode 100644 index 6d5a2434770..00000000000 --- a/tests-clar/object/tag/list.c +++ /dev/null @@ -1,115 +0,0 @@ -#include "clar_libgit2.h" - -#include "tag.h" - -static git_repository *g_repo; - -#define MAX_USED_TAGS 6 - -struct pattern_match_t -{ - const char* pattern; - const size_t expected_matches; - const char* expected_results[MAX_USED_TAGS]; -}; - -// Helpers -static void ensure_tag_pattern_match(git_repository *repo, - const struct pattern_match_t* data) -{ - int already_found[MAX_USED_TAGS] = { 0 }; - git_strarray tag_list; - int error = 0; - size_t sucessfully_found = 0; - size_t i, j; - - cl_assert(data->expected_matches <= MAX_USED_TAGS); - - if ((error = git_tag_list_match(&tag_list, data->pattern, repo)) < 0) - goto exit; - - if (tag_list.count != data->expected_matches) - { - error = GIT_ERROR; - goto exit; - } - - // we have to be prepared that tags come in any order. - for (i = 0; i < tag_list.count; i++) - { - for (j = 0; j < data->expected_matches; j++) - { - if (!already_found[j] && !strcmp(data->expected_results[j], tag_list.strings[i])) - { - already_found[j] = 1; - sucessfully_found++; - break; - } - } - } - cl_assert_equal_i((int)sucessfully_found, (int)data->expected_matches); - -exit: - git_strarray_free(&tag_list); - cl_git_pass(error); -} - -// Fixture setup and teardown -void test_object_tag_list__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_object_tag_list__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_object_tag_list__list_all(void) -{ - // list all tag names from the repository - git_strarray tag_list; - - cl_git_pass(git_tag_list(&tag_list, g_repo)); - - cl_assert_equal_i((int)tag_list.count, 6); - - git_strarray_free(&tag_list); -} - -static const struct pattern_match_t matches[] = { - // All tags, including a packed one and two namespaced ones. - { "", 6, { "e90810b", "point_to_blob", "test", "packed-tag", "foo/bar", "foo/foo/bar" } }, - - // beginning with - { "t*", 1, { "test" } }, - - // ending with - { "*b", 2, { "e90810b", "point_to_blob" } }, - - // exact match - { "e", 0 }, - { "e90810b", 1, { "e90810b" } }, - - // either or - { "e90810[ab]", 1, { "e90810b" } }, - - // glob in the middle - { "foo/*/bar", 1, { "foo/foo/bar" } }, - - // The matching of '*' is based on plain string matching analog to the regular expression ".*" - // => a '/' in the tag name has no special meaning. - // Compare to `git tag -l "*bar"` - { "*bar", 2, { "foo/bar", "foo/foo/bar" } }, - - // End of list - { NULL } -}; - -void test_object_tag_list__list_by_pattern(void) -{ - // list all tag names from the repository matching a specified pattern - size_t i = 0; - while (matches[i].pattern) - ensure_tag_pattern_match(g_repo, &matches[i++]); -} diff --git a/tests-clar/object/tag/peel.c b/tests-clar/object/tag/peel.c deleted file mode 100644 index e2cd8d6a869..00000000000 --- a/tests-clar/object/tag/peel.c +++ /dev/null @@ -1,61 +0,0 @@ -#include "clar_libgit2.h" -#include "tag.h" - -static git_repository *repo; -static git_tag *tag; -static git_object *target; - -void test_object_tag_peel__initialize(void) -{ - cl_fixture_sandbox("testrepo.git"); - cl_git_pass(git_repository_open(&repo, "testrepo.git")); -} - -void test_object_tag_peel__cleanup(void) -{ - git_tag_free(tag); - tag = NULL; - - git_object_free(target); - target = NULL; - - git_repository_free(repo); - repo = NULL; - - cl_fixture_cleanup("testrepo.git"); -} - -static void retrieve_tag_from_oid(git_tag **tag_out, git_repository *repo, const char *sha) -{ - git_oid oid; - - cl_git_pass(git_oid_fromstr(&oid, sha)); - cl_git_pass(git_tag_lookup(tag_out, repo, &oid)); -} - -void test_object_tag_peel__can_peel_to_a_commit(void) -{ - retrieve_tag_from_oid(&tag, repo, "7b4384978d2493e851f9cca7858815fac9b10980"); - - cl_git_pass(git_tag_peel(&target, tag)); - cl_assert(git_object_type(target) == GIT_OBJ_COMMIT); - cl_git_pass(git_oid_streq(git_object_id(target), "e90810b8df3e80c413d903f631643c716887138d")); -} - -void test_object_tag_peel__can_peel_several_nested_tags_to_a_commit(void) -{ - retrieve_tag_from_oid(&tag, repo, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); - - cl_git_pass(git_tag_peel(&target, tag)); - cl_assert(git_object_type(target) == GIT_OBJ_COMMIT); - cl_git_pass(git_oid_streq(git_object_id(target), "e90810b8df3e80c413d903f631643c716887138d")); -} - -void test_object_tag_peel__can_peel_to_a_non_commit(void) -{ - retrieve_tag_from_oid(&tag, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91"); - - cl_git_pass(git_tag_peel(&target, tag)); - cl_assert(git_object_type(target) == GIT_OBJ_BLOB); - cl_git_pass(git_oid_streq(git_object_id(target), "1385f264afb75a56a5bec74243be9b367ba4ca08")); -} diff --git a/tests-clar/object/tag/read.c b/tests-clar/object/tag/read.c deleted file mode 100644 index 16e3e63a2d2..00000000000 --- a/tests-clar/object/tag/read.c +++ /dev/null @@ -1,119 +0,0 @@ -#include "clar_libgit2.h" - -#include "tag.h" - -static const char *tag1_id = "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"; -static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980"; -static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d"; -static const char *bad_tag_id = "eda9f45a2a98d4c17a09d681d88569fa4ea91755"; -static const char *badly_tagged_commit = "e90810b8df3e80c413d903f631643c716887138d"; -static const char *short_tag_id = "5da7760512a953e3c7c4e47e4392c7a4338fb729"; -static const char *short_tagged_commit = "4a5ed60bafcf4638b7c8356bd4ce1916bfede93c"; - -static git_repository *g_repo; - -// Fixture setup and teardown -void test_object_tag_read__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_object_tag_read__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - - -void test_object_tag_read__parse(void) -{ - // read and parse a tag from the repository - git_tag *tag1, *tag2; - git_commit *commit; - git_oid id1, id2, id_commit; - - git_oid_fromstr(&id1, tag1_id); - git_oid_fromstr(&id2, tag2_id); - git_oid_fromstr(&id_commit, tagged_commit); - - cl_git_pass(git_tag_lookup(&tag1, g_repo, &id1)); - - cl_assert_equal_s(git_tag_name(tag1), "test"); - cl_assert(git_tag_target_type(tag1) == GIT_OBJ_TAG); - - cl_git_pass(git_tag_target((git_object **)&tag2, tag1)); - cl_assert(tag2 != NULL); - - cl_assert(git_oid_cmp(&id2, git_tag_id(tag2)) == 0); - - cl_git_pass(git_tag_target((git_object **)&commit, tag2)); - cl_assert(commit != NULL); - - cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0); - - git_tag_free(tag1); - git_tag_free(tag2); - git_commit_free(commit); -} - -void test_object_tag_read__parse_without_tagger(void) -{ - // read and parse a tag without a tagger field - git_repository *bad_tag_repo; - git_tag *bad_tag; - git_commit *commit; - git_oid id, id_commit; - - // TODO: This is a little messy - cl_git_pass(git_repository_open(&bad_tag_repo, cl_fixture("bad_tag.git"))); - - git_oid_fromstr(&id, bad_tag_id); - git_oid_fromstr(&id_commit, badly_tagged_commit); - - cl_git_pass(git_tag_lookup(&bad_tag, bad_tag_repo, &id)); - cl_assert(bad_tag != NULL); - - cl_assert_equal_s(git_tag_name(bad_tag), "e90810b"); - cl_assert(git_oid_cmp(&id, git_tag_id(bad_tag)) == 0); - cl_assert(bad_tag->tagger == NULL); - - cl_git_pass(git_tag_target((git_object **)&commit, bad_tag)); - cl_assert(commit != NULL); - - cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0); - - - git_tag_free(bad_tag); - git_commit_free(commit); - git_repository_free(bad_tag_repo); -} - -void test_object_tag_read__parse_without_message(void) -{ - // read and parse a tag without a message field - git_repository *short_tag_repo; - git_tag *short_tag; - git_commit *commit; - git_oid id, id_commit; - - // TODO: This is a little messy - cl_git_pass(git_repository_open(&short_tag_repo, cl_fixture("short_tag.git"))); - - git_oid_fromstr(&id, short_tag_id); - git_oid_fromstr(&id_commit, short_tagged_commit); - - cl_git_pass(git_tag_lookup(&short_tag, short_tag_repo, &id)); - cl_assert(short_tag != NULL); - - cl_assert_equal_s(git_tag_name(short_tag), "no_description"); - cl_assert(git_oid_cmp(&id, git_tag_id(short_tag)) == 0); - cl_assert(short_tag->message == NULL); - - cl_git_pass(git_tag_target((git_object **)&commit, short_tag)); - cl_assert(commit != NULL); - - cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0); - - git_tag_free(short_tag); - git_commit_free(commit); - git_repository_free(short_tag_repo); -} diff --git a/tests-clar/object/tag/write.c b/tests-clar/object/tag/write.c deleted file mode 100644 index eb0ac2897b0..00000000000 --- a/tests-clar/object/tag/write.c +++ /dev/null @@ -1,221 +0,0 @@ -#include "clar_libgit2.h" - -static const char* tagger_name = "Vicent Marti"; -static const char* tagger_email = "vicent@github.com"; -static const char* tagger_message = "This is my tag.\n\nThere are many tags, but this one is mine\n"; - -static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980"; -static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d"; - -static git_repository *g_repo; - -// Fixture setup and teardown -void test_object_tag_write__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_object_tag_write__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_object_tag_write__basic(void) -{ - // write a tag to the repository and read it again - git_tag *tag; - git_oid target_id, tag_id; - git_signature *tagger; - const git_signature *tagger1; - git_reference *ref_tag; - git_object *target; - - git_oid_fromstr(&target_id, tagged_commit); - cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT)); - - /* create signature */ - cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60)); - - cl_git_pass( - git_tag_create(&tag_id, g_repo, - "the-tag", target, tagger, tagger_message, 0) - ); - - git_object_free(target); - git_signature_free(tagger); - - cl_git_pass(git_tag_lookup(&tag, g_repo, &tag_id)); - cl_assert(git_oid_cmp(git_tag_target_id(tag), &target_id) == 0); - - /* Check attributes were set correctly */ - tagger1 = git_tag_tagger(tag); - cl_assert(tagger1 != NULL); - cl_assert_equal_s(tagger1->name, tagger_name); - cl_assert_equal_s(tagger1->email, tagger_email); - cl_assert(tagger1->when.time == 123456789); - cl_assert(tagger1->when.offset == 60); - - cl_assert_equal_s(git_tag_message(tag), tagger_message); - - cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/the-tag")); - cl_assert(git_oid_cmp(git_reference_target(ref_tag), &tag_id) == 0); - cl_git_pass(git_reference_delete(ref_tag)); - - git_tag_free(tag); -} - -void test_object_tag_write__overwrite(void) -{ - // Attempt to write a tag bearing the same name than an already existing tag - git_oid target_id, tag_id; - git_signature *tagger; - git_object *target; - - git_oid_fromstr(&target_id, tagged_commit); - cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT)); - - /* create signature */ - cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60)); - - cl_assert_equal_i(GIT_EEXISTS, git_tag_create( - &tag_id, /* out id */ - g_repo, - "e90810b", - target, - tagger, - tagger_message, - 0)); - - git_object_free(target); - git_signature_free(tagger); -} - -void test_object_tag_write__replace(void) -{ - // Replace an already existing tag - git_oid target_id, tag_id, old_tag_id; - git_signature *tagger; - git_reference *ref_tag; - git_object *target; - - git_oid_fromstr(&target_id, tagged_commit); - cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT)); - - cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b")); - git_oid_cpy(&old_tag_id, git_reference_target(ref_tag)); - git_reference_free(ref_tag); - - /* create signature */ - cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60)); - - cl_git_pass(git_tag_create( - &tag_id, /* out id */ - g_repo, - "e90810b", - target, - tagger, - tagger_message, - 1)); - - git_object_free(target); - git_signature_free(tagger); - - cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b")); - cl_assert(git_oid_cmp(git_reference_target(ref_tag), &tag_id) == 0); - cl_assert(git_oid_cmp(git_reference_target(ref_tag), &old_tag_id) != 0); - - git_reference_free(ref_tag); -} - -void test_object_tag_write__lightweight(void) -{ - // write a lightweight tag to the repository and read it again - git_oid target_id, object_id; - git_reference *ref_tag; - git_object *target; - - git_oid_fromstr(&target_id, tagged_commit); - cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT)); - - cl_git_pass(git_tag_create_lightweight( - &object_id, - g_repo, - "light-tag", - target, - 0)); - - git_object_free(target); - - cl_assert(git_oid_cmp(&object_id, &target_id) == 0); - - cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/light-tag")); - cl_assert(git_oid_cmp(git_reference_target(ref_tag), &target_id) == 0); - - cl_git_pass(git_tag_delete(g_repo, "light-tag")); - - git_reference_free(ref_tag); -} - -void test_object_tag_write__lightweight_over_existing(void) -{ - // Attempt to write a lightweight tag bearing the same name than an already existing tag - git_oid target_id, object_id, existing_object_id; - git_object *target; - - git_oid_fromstr(&target_id, tagged_commit); - cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT)); - - cl_assert_equal_i(GIT_EEXISTS, git_tag_create_lightweight( - &object_id, - g_repo, - "e90810b", - target, - 0)); - - git_oid_fromstr(&existing_object_id, tag2_id); - cl_assert(git_oid_cmp(&object_id, &existing_object_id) == 0); - - git_object_free(target); -} - -void test_object_tag_write__delete(void) -{ - // Delete an already existing tag - git_reference *ref_tag; - - cl_git_pass(git_tag_delete(g_repo, "e90810b")); - - cl_git_fail(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b")); - - git_reference_free(ref_tag); -} - -void test_object_tag_write__creating_with_an_invalid_name_returns_EINVALIDSPEC(void) -{ - git_oid target_id, tag_id; - git_signature *tagger; - git_object *target; - - git_oid_fromstr(&target_id, tagged_commit); - cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT)); - - cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60)); - - cl_assert_equal_i(GIT_EINVALIDSPEC, - git_tag_create(&tag_id, g_repo, - "Inv@{id", target, tagger, tagger_message, 0) - ); - - cl_assert_equal_i(GIT_EINVALIDSPEC, - git_tag_create_lightweight(&tag_id, g_repo, - "Inv@{id", target, 0) - ); - - git_object_free(target); - git_signature_free(tagger); -} - -void test_object_tag_write__deleting_with_an_invalid_name_returns_EINVALIDSPEC(void) -{ - cl_assert_equal_i(GIT_EINVALIDSPEC, git_tag_delete(g_repo, "Inv@{id")); -} diff --git a/tests-clar/object/tree/attributes.c b/tests-clar/object/tree/attributes.c deleted file mode 100644 index cc93b45d2c8..00000000000 --- a/tests-clar/object/tree/attributes.c +++ /dev/null @@ -1,114 +0,0 @@ -#include "clar_libgit2.h" -#include "tree.h" - -static const char *blob_oid = "3d0970ec547fc41ef8a5882dde99c6adce65b021"; -static const char *tree_oid = "1b05fdaa881ee45b48cbaa5e9b037d667a47745e"; - -void test_object_tree_attributes__ensure_correctness_of_attributes_on_insertion(void) -{ - git_treebuilder *builder; - git_oid oid; - - cl_git_pass(git_oid_fromstr(&oid, blob_oid)); - - cl_git_pass(git_treebuilder_create(&builder, NULL)); - - cl_git_fail(git_treebuilder_insert(NULL, builder, "one.txt", &oid, (git_filemode_t)0777777)); - cl_git_fail(git_treebuilder_insert(NULL, builder, "one.txt", &oid, (git_filemode_t)0100666)); - cl_git_fail(git_treebuilder_insert(NULL, builder, "one.txt", &oid, (git_filemode_t)0000001)); - - git_treebuilder_free(builder); -} - -void test_object_tree_attributes__group_writable_tree_entries_created_with_an_antique_git_version_can_still_be_accessed(void) -{ - git_repository *repo; - git_oid tid; - git_tree *tree; - const git_tree_entry *entry; - - cl_git_pass(git_repository_open(&repo, cl_fixture("deprecated-mode.git"))); - - cl_git_pass(git_oid_fromstr(&tid, tree_oid)); - cl_git_pass(git_tree_lookup(&tree, repo, &tid)); - - entry = git_tree_entry_byname(tree, "old_mode.txt"); - cl_assert_equal_i( - GIT_FILEMODE_BLOB, - git_tree_entry_filemode(entry)); - - git_tree_free(tree); - git_repository_free(repo); -} - -void test_object_tree_attributes__treebuilder_reject_invalid_filemode(void) -{ - git_treebuilder *builder; - git_oid bid; - const git_tree_entry *entry; - - cl_git_pass(git_oid_fromstr(&bid, blob_oid)); - cl_git_pass(git_treebuilder_create(&builder, NULL)); - - cl_git_fail(git_treebuilder_insert( - &entry, - builder, - "normalized.txt", - &bid, - GIT_FILEMODE_BLOB_GROUP_WRITABLE)); - - git_treebuilder_free(builder); -} - -void test_object_tree_attributes__normalize_attributes_when_creating_a_tree_from_an_existing_one(void) -{ - git_repository *repo; - git_treebuilder *builder; - git_oid tid, tid2; - git_tree *tree; - const git_tree_entry *entry; - - repo = cl_git_sandbox_init("deprecated-mode.git"); - - cl_git_pass(git_oid_fromstr(&tid, tree_oid)); - cl_git_pass(git_tree_lookup(&tree, repo, &tid)); - - cl_git_pass(git_treebuilder_create(&builder, tree)); - - entry = git_treebuilder_get(builder, "old_mode.txt"); - cl_assert_equal_i( - GIT_FILEMODE_BLOB, - git_tree_entry_filemode(entry)); - - cl_git_pass(git_treebuilder_write(&tid2, repo, builder)); - git_treebuilder_free(builder); - git_tree_free(tree); - - cl_git_pass(git_tree_lookup(&tree, repo, &tid2)); - entry = git_tree_entry_byname(tree, "old_mode.txt"); - cl_assert_equal_i( - GIT_FILEMODE_BLOB, - git_tree_entry_filemode(entry)); - - git_tree_free(tree); - cl_git_sandbox_cleanup(); -} - -void test_object_tree_attributes__normalize_600(void) -{ - git_oid id; - git_tree *tree; - git_repository *repo; - const git_tree_entry *entry; - - repo = cl_git_sandbox_init("deprecated-mode.git"); - - git_oid_fromstr(&id, "0810fb7818088ff5ac41ee49199b51473b1bd6c7"); - cl_git_pass(git_tree_lookup(&tree, repo, &id)); - - entry = git_tree_entry_byname(tree, "ListaTeste.xml"); - cl_assert_equal_i(entry->attr, GIT_FILEMODE_BLOB); - - git_tree_free(tree); - cl_git_sandbox_cleanup(); -} diff --git a/tests-clar/object/tree/read.c b/tests-clar/object/tree/read.c deleted file mode 100644 index 59a809bf19c..00000000000 --- a/tests-clar/object/tree/read.c +++ /dev/null @@ -1,75 +0,0 @@ -#include "clar_libgit2.h" - -#include "tree.h" - -static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd"; - -static git_repository *g_repo; - -// Fixture setup and teardown -void test_object_tree_read__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_object_tree_read__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - - - -void test_object_tree_read__loaded(void) -{ - // acces randomly the entries on a loaded tree - git_oid id; - git_tree *tree; - - git_oid_fromstr(&id, tree_oid); - - cl_git_pass(git_tree_lookup(&tree, g_repo, &id)); - - cl_assert(git_tree_entry_byname(tree, "README") != NULL); - cl_assert(git_tree_entry_byname(tree, "NOTEXISTS") == NULL); - cl_assert(git_tree_entry_byname(tree, "") == NULL); - cl_assert(git_tree_entry_byindex(tree, 0) != NULL); - cl_assert(git_tree_entry_byindex(tree, 2) != NULL); - cl_assert(git_tree_entry_byindex(tree, 3) == NULL); - cl_assert(git_tree_entry_byindex(tree, (unsigned int)-1) == NULL); - - git_tree_free(tree); -} - -void test_object_tree_read__two(void) -{ - // read a tree from the repository - git_oid id; - git_tree *tree; - const git_tree_entry *entry; - git_object *obj; - - git_oid_fromstr(&id, tree_oid); - - cl_git_pass(git_tree_lookup(&tree, g_repo, &id)); - - cl_assert(git_tree_entrycount(tree) == 3); - - /* GH-86: git_object_lookup() should also check the type if the object comes from the cache */ - cl_assert(git_object_lookup(&obj, g_repo, &id, GIT_OBJ_TREE) == 0); - cl_assert(obj != NULL); - git_object_free(obj); - obj = NULL; - cl_git_fail(git_object_lookup(&obj, g_repo, &id, GIT_OBJ_BLOB)); - cl_assert(obj == NULL); - - entry = git_tree_entry_byname(tree, "README"); - cl_assert(entry != NULL); - - cl_assert_equal_s(git_tree_entry_name(entry), "README"); - - cl_git_pass(git_tree_entry_to_object(&obj, g_repo, entry)); - cl_assert(obj != NULL); - - git_object_free(obj); - git_tree_free(tree); -} diff --git a/tests-clar/object/tree/walk.c b/tests-clar/object/tree/walk.c deleted file mode 100644 index b7af4924d3f..00000000000 --- a/tests-clar/object/tree/walk.c +++ /dev/null @@ -1,103 +0,0 @@ -#include "clar_libgit2.h" -#include "tree.h" - -static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd"; -static git_repository *g_repo; - -void test_object_tree_walk__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_object_tree_walk__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -static int treewalk_count_cb( - const char *root, const git_tree_entry *entry, void *payload) -{ - int *count = payload; - - GIT_UNUSED(root); - GIT_UNUSED(entry); - - (*count) += 1; - - return 0; -} - -void test_object_tree_walk__0(void) -{ - git_oid id; - git_tree *tree; - int ct; - - git_oid_fromstr(&id, tree_oid); - - cl_git_pass(git_tree_lookup(&tree, g_repo, &id)); - - ct = 0; - cl_git_pass(git_tree_walk(tree, GIT_TREEWALK_PRE, treewalk_count_cb, &ct)); - cl_assert_equal_i(3, ct); - - ct = 0; - cl_git_pass(git_tree_walk(tree, GIT_TREEWALK_POST, treewalk_count_cb, &ct)); - cl_assert_equal_i(3, ct); - - git_tree_free(tree); -} - - -static int treewalk_stop_cb( - const char *root, const git_tree_entry *entry, void *payload) -{ - int *count = payload; - - GIT_UNUSED(root); - GIT_UNUSED(entry); - - (*count) += 1; - - return (*count == 2) ? -1 : 0; -} - -static int treewalk_stop_immediately_cb( - const char *root, const git_tree_entry *entry, void *payload) -{ - GIT_UNUSED(root); - GIT_UNUSED(entry); - GIT_UNUSED(payload); - return -100; -} - -void test_object_tree_walk__1(void) -{ - git_oid id; - git_tree *tree; - int ct; - - git_oid_fromstr(&id, tree_oid); - - cl_git_pass(git_tree_lookup(&tree, g_repo, &id)); - - ct = 0; - cl_assert_equal_i( - GIT_EUSER, git_tree_walk(tree, GIT_TREEWALK_PRE, treewalk_stop_cb, &ct)); - cl_assert_equal_i(2, ct); - - ct = 0; - cl_assert_equal_i( - GIT_EUSER, git_tree_walk(tree, GIT_TREEWALK_POST, treewalk_stop_cb, &ct)); - cl_assert_equal_i(2, ct); - - cl_assert_equal_i( - GIT_EUSER, git_tree_walk( - tree, GIT_TREEWALK_PRE, treewalk_stop_immediately_cb, NULL)); - - cl_assert_equal_i( - GIT_EUSER, git_tree_walk( - tree, GIT_TREEWALK_POST, treewalk_stop_immediately_cb, NULL)); - - git_tree_free(tree); -} diff --git a/tests-clar/object/tree/write.c b/tests-clar/object/tree/write.c deleted file mode 100644 index cc5438b0584..00000000000 --- a/tests-clar/object/tree/write.c +++ /dev/null @@ -1,165 +0,0 @@ -#include "clar_libgit2.h" - -#include "tree.h" - -static const char *blob_oid = "fa49b077972391ad58037050f2a75f74e3671e92"; -static const char *first_tree = "181037049a54a1eb5fab404658a3a250b44335d7"; -static const char *second_tree = "f60079018b664e4e79329a7ef9559c8d9e0378d1"; -static const char *third_tree = "eb86d8b81d6adbd5290a935d6c9976882de98488"; - -static git_repository *g_repo; - -// Fixture setup and teardown -void test_object_tree_write__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_object_tree_write__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_object_tree_write__from_memory(void) -{ - // write a tree from a memory - git_treebuilder *builder; - git_tree *tree; - git_oid id, bid, rid, id2; - - git_oid_fromstr(&id, first_tree); - git_oid_fromstr(&id2, second_tree); - git_oid_fromstr(&bid, blob_oid); - - //create a second tree from first tree using `git_treebuilder_insert` on REPOSITORY_FOLDER. - cl_git_pass(git_tree_lookup(&tree, g_repo, &id)); - cl_git_pass(git_treebuilder_create(&builder, tree)); - - cl_git_fail(git_treebuilder_insert(NULL, builder, "", - &bid, GIT_FILEMODE_BLOB)); - cl_git_fail(git_treebuilder_insert(NULL, builder, "/", - &bid, GIT_FILEMODE_BLOB)); - cl_git_fail(git_treebuilder_insert(NULL, builder, ".git", - &bid, GIT_FILEMODE_BLOB)); - cl_git_fail(git_treebuilder_insert(NULL, builder, "..", - &bid, GIT_FILEMODE_BLOB)); - cl_git_fail(git_treebuilder_insert(NULL, builder, ".", - &bid, GIT_FILEMODE_BLOB)); - cl_git_fail(git_treebuilder_insert(NULL, builder, "folder/new.txt", - &bid, GIT_FILEMODE_BLOB)); - - cl_git_pass(git_treebuilder_insert( - NULL, builder, "new.txt", &bid, GIT_FILEMODE_BLOB)); - - cl_git_pass(git_treebuilder_write(&rid, g_repo, builder)); - - cl_assert(git_oid_cmp(&rid, &id2) == 0); - - git_treebuilder_free(builder); - git_tree_free(tree); -} - -void test_object_tree_write__subtree(void) -{ - // write a hierarchical tree from a memory - git_treebuilder *builder; - git_tree *tree; - git_oid id, bid, subtree_id, id2, id3; - git_oid id_hiearar; - - git_oid_fromstr(&id, first_tree); - git_oid_fromstr(&id2, second_tree); - git_oid_fromstr(&id3, third_tree); - git_oid_fromstr(&bid, blob_oid); - - //create subtree - cl_git_pass(git_treebuilder_create(&builder, NULL)); - cl_git_pass(git_treebuilder_insert( - NULL, builder, "new.txt", &bid, GIT_FILEMODE_BLOB)); //-V536 - cl_git_pass(git_treebuilder_write(&subtree_id, g_repo, builder)); - git_treebuilder_free(builder); - - // create parent tree - cl_git_pass(git_tree_lookup(&tree, g_repo, &id)); - cl_git_pass(git_treebuilder_create(&builder, tree)); - cl_git_pass(git_treebuilder_insert( - NULL, builder, "new", &subtree_id, GIT_FILEMODE_TREE)); //-V536 - cl_git_pass(git_treebuilder_write(&id_hiearar, g_repo, builder)); - git_treebuilder_free(builder); - git_tree_free(tree); - - cl_assert(git_oid_cmp(&id_hiearar, &id3) == 0); - - // check data is correct - cl_git_pass(git_tree_lookup(&tree, g_repo, &id_hiearar)); - cl_assert(2 == git_tree_entrycount(tree)); - git_tree_free(tree); -} - -/* - * And the Lord said: Is this tree properly sorted? - */ -void test_object_tree_write__sorted_subtrees(void) -{ - git_treebuilder *builder; - unsigned int i; - int position_c = -1, position_cake = -1, position_config = -1; - - struct { - unsigned int attr; - const char *filename; - } entries[] = { - { GIT_FILEMODE_BLOB, ".gitattributes" }, - { GIT_FILEMODE_BLOB, ".gitignore" }, - { GIT_FILEMODE_BLOB, ".htaccess" }, - { GIT_FILEMODE_BLOB, "Capfile" }, - { GIT_FILEMODE_BLOB, "Makefile"}, - { GIT_FILEMODE_BLOB, "README"}, - { GIT_FILEMODE_TREE, "app"}, - { GIT_FILEMODE_TREE, "cake"}, - { GIT_FILEMODE_TREE, "config"}, - { GIT_FILEMODE_BLOB, "c"}, - { GIT_FILEMODE_BLOB, "git_test.txt"}, - { GIT_FILEMODE_BLOB, "htaccess.htaccess"}, - { GIT_FILEMODE_BLOB, "index.php"}, - { GIT_FILEMODE_TREE, "plugins"}, - { GIT_FILEMODE_TREE, "schemas"}, - { GIT_FILEMODE_TREE, "ssl-certs"}, - { GIT_FILEMODE_TREE, "vendors"} - }; - - git_oid blank_oid, tree_oid; - - memset(&blank_oid, 0x0, sizeof(blank_oid)); - - cl_git_pass(git_treebuilder_create(&builder, NULL)); - - for (i = 0; i < ARRAY_SIZE(entries); ++i) { - cl_git_pass(git_treebuilder_insert(NULL, - builder, entries[i].filename, &blank_oid, entries[i].attr)); - } - - cl_git_pass(git_treebuilder_write(&tree_oid, g_repo, builder)); - - for (i = 0; i < builder->entries.length; ++i) { - git_tree_entry *entry = git_vector_get(&builder->entries, i); - - if (strcmp(entry->filename, "c") == 0) - position_c = i; - - if (strcmp(entry->filename, "cake") == 0) - position_cake = i; - - if (strcmp(entry->filename, "config") == 0) - position_config = i; - } - - cl_assert(position_c != -1); - cl_assert(position_cake != -1); - cl_assert(position_config != -1); - - cl_assert(position_c < position_cake); - cl_assert(position_cake < position_config); - - git_treebuilder_free(builder); -} diff --git a/tests-clar/odb/alternates.c b/tests-clar/odb/alternates.c deleted file mode 100644 index be7bfa9cd99..00000000000 --- a/tests-clar/odb/alternates.c +++ /dev/null @@ -1,80 +0,0 @@ -#include "clar_libgit2.h" -#include "odb.h" -#include "repository.h" - -static git_buf destpath, filepath; -static const char *paths[] = { - "A.git", "B.git", "C.git", "D.git", "E.git", "F.git", "G.git" -}; -static git_filebuf file; -static git_repository *repo; - -void test_odb_alternates__cleanup(void) -{ - size_t i; - - git_buf_free(&destpath); - git_buf_free(&filepath); - - for (i = 0; i < ARRAY_SIZE(paths); i++) - cl_fixture_cleanup(paths[i]); -} - -static void init_linked_repo(const char *path, const char *alternate) -{ - git_buf_clear(&destpath); - git_buf_clear(&filepath); - - cl_git_pass(git_repository_init(&repo, path, 1)); - cl_git_pass(git_path_prettify(&destpath, alternate, NULL)); - cl_git_pass(git_buf_joinpath(&destpath, destpath.ptr, "objects")); - cl_git_pass(git_buf_joinpath(&filepath, git_repository_path(repo), "objects/info")); - cl_git_pass(git_futils_mkdir(filepath.ptr, NULL, 0755, GIT_MKDIR_PATH)); - cl_git_pass(git_buf_joinpath(&filepath, filepath.ptr , "alternates")); - - cl_git_pass(git_filebuf_open(&file, git_buf_cstr(&filepath), 0)); - git_filebuf_printf(&file, "%s\n", git_buf_cstr(&destpath)); - cl_git_pass(git_filebuf_commit(&file, 0644)); - - git_repository_free(repo); -} - -void test_odb_alternates__chained(void) -{ - git_commit *commit; - git_oid oid; - - /* Set the alternate A -> testrepo.git */ - init_linked_repo(paths[0], cl_fixture("testrepo.git")); - - /* Set the alternate B -> A */ - init_linked_repo(paths[1], paths[0]); - - /* Now load B and see if we can find an object from testrepo.git */ - cl_git_pass(git_repository_open(&repo, paths[1])); - git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - cl_git_pass(git_commit_lookup(&commit, repo, &oid)); - git_commit_free(commit); - git_repository_free(repo); -} - -void test_odb_alternates__long_chain(void) -{ - git_commit *commit; - git_oid oid; - size_t i; - - /* Set the alternate A -> testrepo.git */ - init_linked_repo(paths[0], cl_fixture("testrepo.git")); - - /* Set up the five-element chain */ - for (i = 1; i < ARRAY_SIZE(paths); i++) { - init_linked_repo(paths[i], paths[i-1]); - } - - /* Now load the last one and see if we can find an object from testrepo.git */ - cl_git_pass(git_repository_open(&repo, paths[ARRAY_SIZE(paths)-1])); - git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - cl_git_fail(git_commit_lookup(&commit, repo, &oid)); - git_repository_free(repo); -} diff --git a/tests-clar/odb/foreach.c b/tests-clar/odb/foreach.c deleted file mode 100644 index 37158d4580b..00000000000 --- a/tests-clar/odb/foreach.c +++ /dev/null @@ -1,80 +0,0 @@ -#include "clar_libgit2.h" -#include "odb.h" -#include "git2/odb_backend.h" -#include "pack.h" - -static git_odb *_odb; -static git_repository *_repo; -static int nobj; - -void test_odb_foreach__cleanup(void) -{ - git_odb_free(_odb); - git_repository_free(_repo); - - _odb = NULL; - _repo = NULL; -} - -static int foreach_cb(const git_oid *oid, void *data) -{ - GIT_UNUSED(data); - GIT_UNUSED(oid); - - nobj++; - - return 0; -} - -/* - * $ git --git-dir tests-clar/resources/testrepo.git count-objects --verbose - * count: 43 - * size: 3 - * in-pack: 1640 - * packs: 3 - * size-pack: 425 - * prune-packable: 0 - * garbage: 0 - */ -void test_odb_foreach__foreach(void) -{ - cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git"))); - git_repository_odb(&_odb, _repo); - - cl_git_pass(git_odb_foreach(_odb, foreach_cb, NULL)); - cl_assert_equal_i(46 + 1640, nobj); /* count + in-pack */ -} - -void test_odb_foreach__one_pack(void) -{ - git_odb_backend *backend = NULL; - - cl_git_pass(git_odb_new(&_odb)); - cl_git_pass(git_odb_backend_one_pack(&backend, cl_fixture("testrepo.git/objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx"))); - cl_git_pass(git_odb_add_backend(_odb, backend, 1)); - _repo = NULL; - - nobj = 0; - cl_git_pass(git_odb_foreach(_odb, foreach_cb, NULL)); - cl_assert(nobj == 1628); -} - -static int foreach_stop_cb(const git_oid *oid, void *data) -{ - GIT_UNUSED(data); - GIT_UNUSED(oid); - - nobj++; - - return (nobj == 1000); -} - -void test_odb_foreach__interrupt_foreach(void) -{ - nobj = 0; - cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git"))); - git_repository_odb(&_odb, _repo); - - cl_assert_equal_i(GIT_EUSER, git_odb_foreach(_odb, foreach_stop_cb, NULL)); - cl_assert(nobj == 1000); -} diff --git a/tests-clar/odb/loose.c b/tests-clar/odb/loose.c deleted file mode 100644 index f95dc28d4db..00000000000 --- a/tests-clar/odb/loose.c +++ /dev/null @@ -1,84 +0,0 @@ -#include "clar_libgit2.h" -#include "odb.h" -#include "posix.h" -#include "loose_data.h" - -static void write_object_files(object_data *d) -{ - int fd; - - if (p_mkdir(d->dir, GIT_OBJECT_DIR_MODE) < 0) - cl_assert(errno == EEXIST); - - cl_assert((fd = p_creat(d->file, S_IREAD | S_IWRITE)) >= 0); - cl_must_pass(p_write(fd, d->bytes, d->blen)); - - p_close(fd); -} - -static void cmp_objects(git_rawobj *o, object_data *d) -{ - cl_assert(o->type == git_object_string2type(d->type)); - cl_assert(o->len == d->dlen); - - if (o->len > 0) - cl_assert(memcmp(o->data, d->data, o->len) == 0); -} - -static void test_read_object(object_data *data) -{ - git_oid id; - git_odb_object *obj; - git_odb *odb; - - write_object_files(data); - - cl_git_pass(git_odb_open(&odb, "test-objects")); - cl_git_pass(git_oid_fromstr(&id, data->id)); - cl_git_pass(git_odb_read(&obj, odb, &id)); - - cmp_objects((git_rawobj *)&obj->raw, data); - - git_odb_object_free(obj); - git_odb_free(odb); -} - -void test_odb_loose__initialize(void) -{ - cl_must_pass(p_mkdir("test-objects", GIT_OBJECT_DIR_MODE)); -} - -void test_odb_loose__cleanup(void) -{ - cl_fixture_cleanup("test-objects"); -} - -void test_odb_loose__exists(void) -{ - git_oid id, id2; - git_odb *odb; - - write_object_files(&one); - cl_git_pass(git_odb_open(&odb, "test-objects")); - - cl_git_pass(git_oid_fromstr(&id, one.id)); - - cl_assert(git_odb_exists(odb, &id)); - - /* Test for a non-existant object */ - cl_git_pass(git_oid_fromstr(&id2, "8b137891791fe96927ad78e64b0aad7bded08baa")); - cl_assert(!git_odb_exists(odb, &id2)); - - git_odb_free(odb); -} - -void test_odb_loose__simple_reads(void) -{ - test_read_object(&commit); - test_read_object(&tree); - test_read_object(&tag); - test_read_object(&zero); - test_read_object(&one); - test_read_object(&two); - test_read_object(&some); -} diff --git a/tests-clar/odb/loose_data.h b/tests-clar/odb/loose_data.h deleted file mode 100644 index c10c9bc7f3e..00000000000 --- a/tests-clar/odb/loose_data.h +++ /dev/null @@ -1,522 +0,0 @@ -typedef struct object_data { - unsigned char *bytes; /* (compressed) bytes stored in object store */ - size_t blen; /* length of data in object store */ - char *id; /* object id (sha1) */ - char *type; /* object type */ - char *dir; /* object store (fan-out) directory name */ - char *file; /* object store filename */ - unsigned char *data; /* (uncompressed) object data */ - size_t dlen; /* length of (uncompressed) object data */ -} object_data; - -/* one == 8b137891791fe96927ad78e64b0aad7bded08bdc */ -static unsigned char one_bytes[] = { - 0x31, 0x78, 0x9c, 0xe3, 0x02, 0x00, 0x00, 0x0b, - 0x00, 0x0b, -}; - -static unsigned char one_data[] = { - 0x0a, -}; - -static object_data one = { - one_bytes, - sizeof(one_bytes), - "8b137891791fe96927ad78e64b0aad7bded08bdc", - "blob", - "test-objects/8b", - "test-objects/8b/137891791fe96927ad78e64b0aad7bded08bdc", - one_data, - sizeof(one_data), -}; - - -/* commit == 3d7f8a6af076c8c3f20071a8935cdbe8228594d1 */ -static unsigned char commit_bytes[] = { - 0x78, 0x01, 0x85, 0x50, 0xc1, 0x6a, 0xc3, 0x30, - 0x0c, 0xdd, 0xd9, 0x5f, 0xa1, 0xfb, 0x96, 0x12, - 0xbb, 0x29, 0x71, 0x46, 0x19, 0x2b, 0x3d, 0x97, - 0x1d, 0xd6, 0x7d, 0x80, 0x1d, 0xcb, 0x89, 0x21, - 0xb6, 0x82, 0xed, 0x40, 0xf3, 0xf7, 0xf3, 0x48, - 0x29, 0x3b, 0x6d, 0xd2, 0xe5, 0xbd, 0x27, 0xbd, - 0x27, 0x50, 0x4f, 0xde, 0xbb, 0x0c, 0xfb, 0x43, - 0xf3, 0x94, 0x23, 0x22, 0x18, 0x6b, 0x85, 0x51, - 0x5d, 0xad, 0xc5, 0xa1, 0x41, 0xae, 0x51, 0x4b, - 0xd9, 0x19, 0x6e, 0x4b, 0x0b, 0x29, 0x35, 0x72, - 0x59, 0xef, 0x5b, 0x29, 0x8c, 0x65, 0x6a, 0xc9, - 0x23, 0x45, 0x38, 0xc1, 0x17, 0x5c, 0x7f, 0xc0, - 0x71, 0x13, 0xde, 0xf1, 0xa6, 0xfc, 0x3c, 0xe1, - 0xae, 0x27, 0xff, 0x06, 0x5c, 0x88, 0x56, 0xf2, - 0x46, 0x74, 0x2d, 0x3c, 0xd7, 0xa5, 0x58, 0x51, - 0xcb, 0xb9, 0x8c, 0x11, 0xce, 0xf0, 0x01, 0x97, - 0x0d, 0x1e, 0x1f, 0xea, 0x3f, 0x6e, 0x76, 0x02, - 0x0a, 0x58, 0x4d, 0x2e, 0x20, 0x6c, 0x1e, 0x48, - 0x8b, 0xf7, 0x2a, 0xae, 0x8c, 0x5d, 0x47, 0x04, - 0x4d, 0x66, 0x05, 0xb2, 0x90, 0x0b, 0xbe, 0xcf, - 0x3d, 0xa6, 0xa4, 0x06, 0x7c, 0x29, 0x3c, 0x64, - 0xe5, 0x82, 0x0b, 0x03, 0xd8, 0x25, 0x96, 0x8d, - 0x08, 0x78, 0x9b, 0x27, 0x15, 0x54, 0x76, 0x14, - 0xd8, 0xdd, 0x35, 0x2f, 0x71, 0xa6, 0x84, 0x8f, - 0x90, 0x51, 0x85, 0x01, 0x13, 0xb8, 0x90, 0x23, - 0x99, 0xa5, 0x47, 0x03, 0x7a, 0xfd, 0x15, 0xbf, - 0x63, 0xec, 0xd3, 0x0d, 0x01, 0x4d, 0x45, 0xb6, - 0xd2, 0xeb, 0xeb, 0xdf, 0xef, 0x60, 0xdf, 0xef, - 0x1f, 0x78, 0x35, -}; - -static unsigned char commit_data[] = { - 0x74, 0x72, 0x65, 0x65, 0x20, 0x64, 0x66, 0x66, - 0x32, 0x64, 0x61, 0x39, 0x30, 0x62, 0x32, 0x35, - 0x34, 0x65, 0x31, 0x62, 0x65, 0x62, 0x38, 0x38, - 0x39, 0x64, 0x31, 0x66, 0x31, 0x66, 0x31, 0x32, - 0x38, 0x38, 0x62, 0x65, 0x31, 0x38, 0x30, 0x33, - 0x37, 0x38, 0x32, 0x64, 0x66, 0x0a, 0x61, 0x75, - 0x74, 0x68, 0x6f, 0x72, 0x20, 0x41, 0x20, 0x55, - 0x20, 0x54, 0x68, 0x6f, 0x72, 0x20, 0x3c, 0x61, - 0x75, 0x74, 0x68, 0x6f, 0x72, 0x40, 0x65, 0x78, - 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, - 0x6d, 0x3e, 0x20, 0x31, 0x32, 0x32, 0x37, 0x38, - 0x31, 0x34, 0x32, 0x39, 0x37, 0x20, 0x2b, 0x30, - 0x30, 0x30, 0x30, 0x0a, 0x63, 0x6f, 0x6d, 0x6d, - 0x69, 0x74, 0x74, 0x65, 0x72, 0x20, 0x43, 0x20, - 0x4f, 0x20, 0x4d, 0x69, 0x74, 0x74, 0x65, 0x72, - 0x20, 0x3c, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, - 0x74, 0x65, 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d, - 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, - 0x20, 0x31, 0x32, 0x32, 0x37, 0x38, 0x31, 0x34, - 0x32, 0x39, 0x37, 0x20, 0x2b, 0x30, 0x30, 0x30, - 0x30, 0x0a, 0x0a, 0x41, 0x20, 0x6f, 0x6e, 0x65, - 0x2d, 0x6c, 0x69, 0x6e, 0x65, 0x20, 0x63, 0x6f, - 0x6d, 0x6d, 0x69, 0x74, 0x20, 0x73, 0x75, 0x6d, - 0x6d, 0x61, 0x72, 0x79, 0x0a, 0x0a, 0x54, 0x68, - 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x6f, - 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f, - 0x6d, 0x6d, 0x69, 0x74, 0x20, 0x6d, 0x65, 0x73, - 0x73, 0x61, 0x67, 0x65, 0x2c, 0x20, 0x63, 0x6f, - 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x69, 0x6e, 0x67, - 0x20, 0x66, 0x75, 0x72, 0x74, 0x68, 0x65, 0x72, - 0x20, 0x65, 0x78, 0x70, 0x6c, 0x61, 0x6e, 0x61, - 0x74, 0x69, 0x6f, 0x6e, 0x0a, 0x6f, 0x66, 0x20, - 0x74, 0x68, 0x65, 0x20, 0x70, 0x75, 0x72, 0x70, - 0x6f, 0x73, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x74, - 0x68, 0x65, 0x20, 0x63, 0x68, 0x61, 0x6e, 0x67, - 0x65, 0x73, 0x20, 0x69, 0x6e, 0x74, 0x72, 0x6f, - 0x64, 0x75, 0x63, 0x65, 0x64, 0x20, 0x62, 0x79, - 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f, 0x6d, - 0x6d, 0x69, 0x74, 0x2e, 0x0a, 0x0a, 0x53, 0x69, - 0x67, 0x6e, 0x65, 0x64, 0x2d, 0x6f, 0x66, 0x2d, - 0x62, 0x79, 0x3a, 0x20, 0x41, 0x20, 0x55, 0x20, - 0x54, 0x68, 0x6f, 0x72, 0x20, 0x3c, 0x61, 0x75, - 0x74, 0x68, 0x6f, 0x72, 0x40, 0x65, 0x78, 0x61, - 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, - 0x3e, 0x0a, -}; - -static object_data commit = { - commit_bytes, - sizeof(commit_bytes), - "3d7f8a6af076c8c3f20071a8935cdbe8228594d1", - "commit", - "test-objects/3d", - "test-objects/3d/7f8a6af076c8c3f20071a8935cdbe8228594d1", - commit_data, - sizeof(commit_data), -}; - -/* tree == dff2da90b254e1beb889d1f1f1288be1803782df */ -static unsigned char tree_bytes[] = { - 0x78, 0x01, 0x2b, 0x29, 0x4a, 0x4d, 0x55, 0x30, - 0x34, 0x32, 0x63, 0x30, 0x34, 0x30, 0x30, 0x33, - 0x31, 0x51, 0xc8, 0xcf, 0x4b, 0x65, 0xe8, 0x16, - 0xae, 0x98, 0x58, 0x29, 0xff, 0x32, 0x53, 0x7d, - 0x6d, 0xc5, 0x33, 0x6f, 0xae, 0xb5, 0xd5, 0xf7, - 0x2e, 0x74, 0xdf, 0x81, 0x4a, 0x17, 0xe7, 0xe7, - 0xa6, 0x32, 0xfc, 0x6d, 0x31, 0xd8, 0xd3, 0xe6, - 0xf3, 0xe7, 0xea, 0x47, 0xbe, 0xd0, 0x09, 0x3f, - 0x96, 0xb8, 0x3f, 0x90, 0x9e, 0xa2, 0xfd, 0x0f, - 0x2a, 0x5f, 0x52, 0x9e, 0xcf, 0x50, 0x31, 0x43, - 0x52, 0x29, 0xd1, 0x5a, 0xeb, 0x77, 0x82, 0x2a, - 0x8b, 0xfe, 0xb7, 0xbd, 0xed, 0x5d, 0x07, 0x67, - 0xfa, 0xb5, 0x42, 0xa5, 0xab, 0x52, 0x8b, 0xf2, - 0x19, 0x9e, 0xcd, 0x7d, 0x34, 0x7b, 0xd3, 0xc5, - 0x6b, 0xce, 0xde, 0xdd, 0x9a, 0xeb, 0xca, 0xa3, - 0x6e, 0x1c, 0x7a, 0xd2, 0x13, 0x3c, 0x11, 0x00, - 0xe2, 0xaa, 0x38, 0x57, -}; - -static unsigned char tree_data[] = { - 0x31, 0x30, 0x30, 0x36, 0x34, 0x34, 0x20, 0x6f, - 0x6e, 0x65, 0x00, 0x8b, 0x13, 0x78, 0x91, 0x79, - 0x1f, 0xe9, 0x69, 0x27, 0xad, 0x78, 0xe6, 0x4b, - 0x0a, 0xad, 0x7b, 0xde, 0xd0, 0x8b, 0xdc, 0x31, - 0x30, 0x30, 0x36, 0x34, 0x34, 0x20, 0x73, 0x6f, - 0x6d, 0x65, 0x00, 0xfd, 0x84, 0x30, 0xbc, 0x86, - 0x4c, 0xfc, 0xd5, 0xf1, 0x0e, 0x55, 0x90, 0xf8, - 0xa4, 0x47, 0xe0, 0x1b, 0x94, 0x2b, 0xfe, 0x31, - 0x30, 0x30, 0x36, 0x34, 0x34, 0x20, 0x74, 0x77, - 0x6f, 0x00, 0x78, 0x98, 0x19, 0x22, 0x61, 0x3b, - 0x2a, 0xfb, 0x60, 0x25, 0x04, 0x2f, 0xf6, 0xbd, - 0x87, 0x8a, 0xc1, 0x99, 0x4e, 0x85, 0x31, 0x30, - 0x30, 0x36, 0x34, 0x34, 0x20, 0x7a, 0x65, 0x72, - 0x6f, 0x00, 0xe6, 0x9d, 0xe2, 0x9b, 0xb2, 0xd1, - 0xd6, 0x43, 0x4b, 0x8b, 0x29, 0xae, 0x77, 0x5a, - 0xd8, 0xc2, 0xe4, 0x8c, 0x53, 0x91, -}; - -static object_data tree = { - tree_bytes, - sizeof(tree_bytes), - "dff2da90b254e1beb889d1f1f1288be1803782df", - "tree", - "test-objects/df", - "test-objects/df/f2da90b254e1beb889d1f1f1288be1803782df", - tree_data, - sizeof(tree_data), -}; - -/* tag == 09d373e1dfdc16b129ceec6dd649739911541e05 */ -static unsigned char tag_bytes[] = { - 0x78, 0x01, 0x35, 0x4e, 0xcb, 0x0a, 0xc2, 0x40, - 0x10, 0xf3, 0xbc, 0x5f, 0x31, 0x77, 0xa1, 0xec, - 0xa3, 0xed, 0x6e, 0x41, 0x44, 0xf0, 0x2c, 0x5e, - 0xfc, 0x81, 0xe9, 0x76, 0xb6, 0xad, 0xb4, 0xb4, - 0x6c, 0x07, 0xd1, 0xbf, 0x77, 0x44, 0x0d, 0x39, - 0x84, 0x10, 0x92, 0x30, 0xf6, 0x60, 0xbc, 0xdb, - 0x2d, 0xed, 0x9d, 0x22, 0x83, 0xeb, 0x7c, 0x0a, - 0x58, 0x63, 0xd2, 0xbe, 0x8e, 0x21, 0xba, 0x64, - 0xb5, 0xf6, 0x06, 0x43, 0xe3, 0xaa, 0xd8, 0xb5, - 0x14, 0xac, 0x0d, 0x55, 0x53, 0x76, 0x46, 0xf1, - 0x6b, 0x25, 0x88, 0xcb, 0x3c, 0x8f, 0xac, 0x58, - 0x3a, 0x1e, 0xba, 0xd0, 0x85, 0xd8, 0xd8, 0xf7, - 0x94, 0xe1, 0x0c, 0x57, 0xb8, 0x8c, 0xcc, 0x22, - 0x0f, 0xdf, 0x90, 0xc8, 0x13, 0x3d, 0x71, 0x5e, - 0x27, 0x2a, 0xc4, 0x39, 0x82, 0xb1, 0xd6, 0x07, - 0x53, 0xda, 0xc6, 0xc3, 0x5e, 0x0b, 0x94, 0xba, - 0x0d, 0xe3, 0x06, 0x42, 0x1e, 0x08, 0x3e, 0x95, - 0xbf, 0x4b, 0x69, 0xc9, 0x90, 0x69, 0x22, 0xdc, - 0xe8, 0xbf, 0xf2, 0x06, 0x42, 0x9a, 0x36, 0xb1, -}; - -static unsigned char tag_data[] = { - 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x20, 0x33, - 0x64, 0x37, 0x66, 0x38, 0x61, 0x36, 0x61, 0x66, - 0x30, 0x37, 0x36, 0x63, 0x38, 0x63, 0x33, 0x66, - 0x32, 0x30, 0x30, 0x37, 0x31, 0x61, 0x38, 0x39, - 0x33, 0x35, 0x63, 0x64, 0x62, 0x65, 0x38, 0x32, - 0x32, 0x38, 0x35, 0x39, 0x34, 0x64, 0x31, 0x0a, - 0x74, 0x79, 0x70, 0x65, 0x20, 0x63, 0x6f, 0x6d, - 0x6d, 0x69, 0x74, 0x0a, 0x74, 0x61, 0x67, 0x20, - 0x76, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x0a, 0x74, - 0x61, 0x67, 0x67, 0x65, 0x72, 0x20, 0x43, 0x20, - 0x4f, 0x20, 0x4d, 0x69, 0x74, 0x74, 0x65, 0x72, - 0x20, 0x3c, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, - 0x74, 0x65, 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d, - 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, - 0x20, 0x31, 0x32, 0x32, 0x37, 0x38, 0x31, 0x34, - 0x32, 0x39, 0x37, 0x20, 0x2b, 0x30, 0x30, 0x30, - 0x30, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, - 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, - 0x61, 0x67, 0x20, 0x6f, 0x62, 0x6a, 0x65, 0x63, - 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x72, 0x65, - 0x6c, 0x65, 0x61, 0x73, 0x65, 0x20, 0x76, 0x30, - 0x2e, 0x30, 0x2e, 0x31, 0x0a, -}; - -static object_data tag = { - tag_bytes, - sizeof(tag_bytes), - "09d373e1dfdc16b129ceec6dd649739911541e05", - "tag", - "test-objects/09", - "test-objects/09/d373e1dfdc16b129ceec6dd649739911541e05", - tag_data, - sizeof(tag_data), -}; - -/* zero == e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 */ -static unsigned char zero_bytes[] = { - 0x78, 0x01, 0x4b, 0xca, 0xc9, 0x4f, 0x52, 0x30, - 0x60, 0x00, 0x00, 0x09, 0xb0, 0x01, 0xf0, -}; - -static unsigned char zero_data[] = { - 0x00 /* dummy data */ -}; - -static object_data zero = { - zero_bytes, - sizeof(zero_bytes), - "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", - "blob", - "test-objects/e6", - "test-objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391", - zero_data, - 0, -}; - -/* two == 78981922613b2afb6025042ff6bd878ac1994e85 */ -static unsigned char two_bytes[] = { - 0x78, 0x01, 0x4b, 0xca, 0xc9, 0x4f, 0x52, 0x30, - 0x62, 0x48, 0xe4, 0x02, 0x00, 0x0e, 0x64, 0x02, - 0x5d, -}; - -static unsigned char two_data[] = { - 0x61, 0x0a, -}; - -static object_data two = { - two_bytes, - sizeof(two_bytes), - "78981922613b2afb6025042ff6bd878ac1994e85", - "blob", - "test-objects/78", - "test-objects/78/981922613b2afb6025042ff6bd878ac1994e85", - two_data, - sizeof(two_data), -}; - -/* some == fd8430bc864cfcd5f10e5590f8a447e01b942bfe */ -static unsigned char some_bytes[] = { - 0x78, 0x01, 0x7d, 0x54, 0xc1, 0x4e, 0xe3, 0x30, - 0x10, 0xdd, 0x33, 0x5f, 0x31, 0xc7, 0x5d, 0x94, - 0xa5, 0x84, 0xd5, 0x22, 0xad, 0x7a, 0x0a, 0x15, - 0x85, 0x48, 0xd0, 0x56, 0x49, 0x2a, 0xd4, 0xa3, - 0x13, 0x4f, 0x88, 0x85, 0x63, 0x47, 0xb6, 0x43, - 0xc9, 0xdf, 0xef, 0x8c, 0x69, 0x17, 0x56, 0x0b, - 0x7b, 0xaa, 0x62, 0x7b, 0xde, 0xbc, 0xf7, 0xe6, - 0x4d, 0x6b, 0x6d, 0x6b, 0x48, 0xd3, 0xcb, 0x5f, - 0x5f, 0x66, 0xa7, 0x27, 0x70, 0x0a, 0x55, 0xa7, - 0x3c, 0xb4, 0x4a, 0x23, 0xf0, 0xaf, 0x43, 0x04, - 0x6f, 0xdb, 0xb0, 0x17, 0x0e, 0xe7, 0x30, 0xd9, - 0x11, 0x1a, 0x61, 0xc0, 0xa1, 0x54, 0x3e, 0x38, - 0x55, 0x8f, 0x81, 0x9e, 0x05, 0x10, 0x46, 0xce, - 0xac, 0x83, 0xde, 0x4a, 0xd5, 0x4e, 0x0c, 0x42, - 0x67, 0xa3, 0x91, 0xe8, 0x20, 0x74, 0x08, 0x01, - 0x5d, 0xef, 0xc1, 0xb6, 0xf1, 0xe3, 0x66, 0xb5, - 0x85, 0x1b, 0x34, 0xe8, 0x84, 0x86, 0xcd, 0x58, - 0x6b, 0xd5, 0xc0, 0x9d, 0x6a, 0xd0, 0x78, 0x4c, - 0xe0, 0x19, 0x9d, 0x57, 0xd6, 0xc0, 0x45, 0xc2, - 0x18, 0xc2, 0xc3, 0xc0, 0x0f, 0x7c, 0x87, 0x12, - 0xea, 0x29, 0x56, 0x2f, 0x99, 0x4f, 0x79, 0xe0, - 0x03, 0x4b, 0x4b, 0x4d, 0x44, 0xa0, 0x92, 0x33, - 0x2a, 0xe0, 0x9a, 0xdc, 0x80, 0x90, 0x52, 0xf1, - 0x11, 0x04, 0x1b, 0x4b, 0x06, 0xea, 0xae, 0x3c, - 0xe3, 0x7a, 0x50, 0x74, 0x4a, 0x84, 0xfe, 0xc3, - 0x81, 0x41, 0xf8, 0x89, 0x18, 0x43, 0x67, 0x9d, - 0x87, 0x47, 0xf5, 0x8c, 0x51, 0xf6, 0x68, 0xb4, - 0xea, 0x55, 0x20, 0x2a, 0x6f, 0x80, 0xdc, 0x42, - 0x2b, 0xf3, 0x14, 0x2b, 0x1a, 0xdb, 0x0f, 0xe4, - 0x9a, 0x64, 0x84, 0xa3, 0x90, 0xa8, 0xf9, 0x8f, - 0x9d, 0x86, 0x9e, 0xd3, 0xab, 0x5a, 0x99, 0xc8, - 0xd9, 0xc3, 0x5e, 0x85, 0x0e, 0x2c, 0xb5, 0x73, - 0x30, 0x38, 0xfb, 0xe8, 0x44, 0xef, 0x5f, 0x95, - 0x1b, 0xc9, 0xd0, 0xef, 0x3c, 0x26, 0x32, 0x1e, - 0xff, 0x2d, 0xb6, 0x23, 0x7b, 0x3f, 0xd1, 0x3c, - 0x78, 0x1a, 0x0d, 0xcb, 0xe6, 0xf6, 0xd4, 0x44, - 0x99, 0x47, 0x1a, 0x9e, 0xed, 0x23, 0xb5, 0x91, - 0x6a, 0xdf, 0x53, 0x39, 0x03, 0xf8, 0x5a, 0xb1, - 0x0f, 0x1f, 0xce, 0x81, 0x11, 0xde, 0x01, 0x7a, - 0x90, 0x16, 0xc4, 0x30, 0xe8, 0x89, 0xed, 0x7b, - 0x65, 0x4b, 0xd7, 0x03, 0x36, 0xc1, 0xcf, 0xa1, - 0xa5, 0xb1, 0xe3, 0x8b, 0xe8, 0x07, 0x4d, 0xf3, - 0x23, 0x25, 0x13, 0x35, 0x27, 0xf5, 0x8c, 0x11, - 0xd3, 0xa0, 0x9a, 0xa8, 0xf5, 0x38, 0x7d, 0xce, - 0x55, 0xc2, 0x71, 0x79, 0x13, 0xc7, 0xa3, 0xda, - 0x77, 0x68, 0xc0, 0xd8, 0x10, 0xdd, 0x24, 0x8b, - 0x15, 0x59, 0xc5, 0x10, 0xe2, 0x20, 0x99, 0x8e, - 0xf0, 0x05, 0x9b, 0x31, 0x88, 0x5a, 0xe3, 0xd9, - 0x37, 0xba, 0xe2, 0xdb, 0xbf, 0x92, 0xfa, 0x66, - 0x16, 0x97, 0x47, 0xd9, 0x9d, 0x1d, 0x28, 0x7c, - 0x9d, 0x08, 0x1c, 0xc7, 0xbd, 0xd2, 0x1a, 0x6a, - 0x04, 0xf2, 0xa2, 0x1d, 0x75, 0x02, 0x14, 0x5d, - 0xc6, 0x78, 0xc8, 0xab, 0xdb, 0xf5, 0xb6, 0x82, - 0x6c, 0xb5, 0x83, 0x87, 0xac, 0x28, 0xb2, 0x55, - 0xb5, 0x9b, 0xc7, 0xc1, 0xb0, 0xb7, 0xf8, 0x4c, - 0xbc, 0x38, 0x0e, 0x8a, 0x04, 0x2a, 0x62, 0x41, - 0x6b, 0xe0, 0x84, 0x09, 0x13, 0xe9, 0xe1, 0xea, - 0xfb, 0xeb, 0x62, 0x71, 0x4b, 0x25, 0xd9, 0x55, - 0x7e, 0x97, 0x57, 0x3b, 0x20, 0x33, 0x96, 0x79, - 0xb5, 0xba, 0x2e, 0x4b, 0x58, 0xae, 0x0b, 0xc8, - 0x60, 0x93, 0x15, 0x55, 0xbe, 0xd8, 0xde, 0x65, - 0x05, 0x6c, 0xb6, 0xc5, 0x66, 0x5d, 0x5e, 0x93, - 0xf7, 0x25, 0x65, 0x98, 0x41, 0x29, 0x86, 0x0c, - 0xf2, 0xf1, 0x14, 0xa2, 0xb3, 0xbd, 0x75, 0x08, - 0x12, 0x83, 0x50, 0xda, 0x1f, 0x23, 0xbe, 0xa3, - 0x1d, 0xf4, 0x9d, 0x1d, 0xb5, 0x84, 0x4e, 0x50, - 0x38, 0x1d, 0x36, 0x48, 0x21, 0x95, 0xd1, 0xac, - 0x81, 0x99, 0x1d, 0xc1, 0x3f, 0x41, 0xe6, 0x9e, - 0x42, 0x5b, 0x0a, 0x48, 0xcc, 0x5f, 0xe0, 0x7d, - 0x3f, 0xc4, 0x6f, 0x0e, 0xfe, 0xc0, 0x2d, 0xfe, - 0x01, 0x2c, 0xd6, 0x9b, 0x5d, 0xbe, 0xba, 0x21, - 0xca, 0x79, 0xcb, 0xe3, 0x49, 0x60, 0xef, 0x68, - 0x05, 0x28, 0x9b, 0x8c, 0xc1, 0x12, 0x3e, 0xdb, - 0xc7, 0x04, 0x7e, 0xa6, 0x74, 0x29, 0xcc, 0x13, - 0xed, 0x07, 0x94, 0x81, 0xd6, 0x96, 0xaa, 0x97, - 0xaa, 0xa5, 0xc0, 0x2f, 0xb5, 0xb5, 0x2e, 0xe6, - 0xfc, 0xca, 0xfa, 0x60, 0x4d, 0x02, 0xf7, 0x19, - 0x9c, 0x5f, 0xa4, 0xe9, 0xf9, 0xf7, 0xf4, 0xc7, - 0x79, 0x9a, 0xc0, 0xb6, 0xcc, 0x58, 0xec, 0xec, - 0xe4, 0x37, 0x22, 0xfa, 0x8b, 0x53, -}; - -static unsigned char some_data[] = { - 0x2f, 0x2a, 0x0a, 0x20, 0x2a, 0x20, 0x54, 0x68, - 0x69, 0x73, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x20, - 0x69, 0x73, 0x20, 0x66, 0x72, 0x65, 0x65, 0x20, - 0x73, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, - 0x3b, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x63, 0x61, - 0x6e, 0x20, 0x72, 0x65, 0x64, 0x69, 0x73, 0x74, - 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x69, - 0x74, 0x20, 0x61, 0x6e, 0x64, 0x2f, 0x6f, 0x72, - 0x20, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x79, 0x0a, - 0x20, 0x2a, 0x20, 0x69, 0x74, 0x20, 0x75, 0x6e, - 0x64, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, - 0x74, 0x65, 0x72, 0x6d, 0x73, 0x20, 0x6f, 0x66, - 0x20, 0x74, 0x68, 0x65, 0x20, 0x47, 0x4e, 0x55, - 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, - 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20, - 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x2c, - 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, - 0x20, 0x32, 0x2c, 0x0a, 0x20, 0x2a, 0x20, 0x61, - 0x73, 0x20, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x73, - 0x68, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x74, - 0x68, 0x65, 0x20, 0x46, 0x72, 0x65, 0x65, 0x20, - 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, - 0x20, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x74, - 0x69, 0x6f, 0x6e, 0x2e, 0x0a, 0x20, 0x2a, 0x0a, - 0x20, 0x2a, 0x20, 0x49, 0x6e, 0x20, 0x61, 0x64, - 0x64, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x74, - 0x6f, 0x20, 0x74, 0x68, 0x65, 0x20, 0x70, 0x65, - 0x72, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f, 0x6e, - 0x73, 0x20, 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, - 0x20, 0x47, 0x4e, 0x55, 0x20, 0x47, 0x65, 0x6e, - 0x65, 0x72, 0x61, 0x6c, 0x20, 0x50, 0x75, 0x62, - 0x6c, 0x69, 0x63, 0x20, 0x4c, 0x69, 0x63, 0x65, - 0x6e, 0x73, 0x65, 0x2c, 0x0a, 0x20, 0x2a, 0x20, - 0x74, 0x68, 0x65, 0x20, 0x61, 0x75, 0x74, 0x68, - 0x6f, 0x72, 0x73, 0x20, 0x67, 0x69, 0x76, 0x65, - 0x20, 0x79, 0x6f, 0x75, 0x20, 0x75, 0x6e, 0x6c, - 0x69, 0x6d, 0x69, 0x74, 0x65, 0x64, 0x20, 0x70, - 0x65, 0x72, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f, - 0x6e, 0x20, 0x74, 0x6f, 0x20, 0x6c, 0x69, 0x6e, - 0x6b, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f, - 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x64, 0x0a, 0x20, - 0x2a, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, - 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x69, - 0x73, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x20, 0x69, - 0x6e, 0x74, 0x6f, 0x20, 0x63, 0x6f, 0x6d, 0x62, - 0x69, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, - 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x6f, 0x74, - 0x68, 0x65, 0x72, 0x20, 0x70, 0x72, 0x6f, 0x67, - 0x72, 0x61, 0x6d, 0x73, 0x2c, 0x0a, 0x20, 0x2a, - 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x6f, 0x20, - 0x64, 0x69, 0x73, 0x74, 0x72, 0x69, 0x62, 0x75, - 0x74, 0x65, 0x20, 0x74, 0x68, 0x6f, 0x73, 0x65, - 0x20, 0x63, 0x6f, 0x6d, 0x62, 0x69, 0x6e, 0x61, - 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x77, 0x69, - 0x74, 0x68, 0x6f, 0x75, 0x74, 0x20, 0x61, 0x6e, - 0x79, 0x20, 0x72, 0x65, 0x73, 0x74, 0x72, 0x69, - 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x0a, 0x20, 0x2a, - 0x20, 0x63, 0x6f, 0x6d, 0x69, 0x6e, 0x67, 0x20, - 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x74, 0x68, 0x65, - 0x20, 0x75, 0x73, 0x65, 0x20, 0x6f, 0x66, 0x20, - 0x74, 0x68, 0x69, 0x73, 0x20, 0x66, 0x69, 0x6c, - 0x65, 0x2e, 0x20, 0x20, 0x28, 0x54, 0x68, 0x65, - 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, - 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20, - 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x0a, - 0x20, 0x2a, 0x20, 0x72, 0x65, 0x73, 0x74, 0x72, - 0x69, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, - 0x64, 0x6f, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x79, - 0x20, 0x69, 0x6e, 0x20, 0x6f, 0x74, 0x68, 0x65, - 0x72, 0x20, 0x72, 0x65, 0x73, 0x70, 0x65, 0x63, - 0x74, 0x73, 0x3b, 0x20, 0x66, 0x6f, 0x72, 0x20, - 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2c, - 0x20, 0x74, 0x68, 0x65, 0x79, 0x20, 0x63, 0x6f, - 0x76, 0x65, 0x72, 0x0a, 0x20, 0x2a, 0x20, 0x6d, - 0x6f, 0x64, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, - 0x69, 0x6f, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, - 0x68, 0x65, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2c, - 0x20, 0x61, 0x6e, 0x64, 0x20, 0x64, 0x69, 0x73, - 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x69, 0x6f, - 0x6e, 0x20, 0x77, 0x68, 0x65, 0x6e, 0x20, 0x6e, - 0x6f, 0x74, 0x20, 0x6c, 0x69, 0x6e, 0x6b, 0x65, - 0x64, 0x20, 0x69, 0x6e, 0x74, 0x6f, 0x0a, 0x20, - 0x2a, 0x20, 0x61, 0x20, 0x63, 0x6f, 0x6d, 0x62, - 0x69, 0x6e, 0x65, 0x64, 0x20, 0x65, 0x78, 0x65, - 0x63, 0x75, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x2e, - 0x29, 0x0a, 0x20, 0x2a, 0x0a, 0x20, 0x2a, 0x20, - 0x54, 0x68, 0x69, 0x73, 0x20, 0x66, 0x69, 0x6c, - 0x65, 0x20, 0x69, 0x73, 0x20, 0x64, 0x69, 0x73, - 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x64, - 0x20, 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, - 0x68, 0x6f, 0x70, 0x65, 0x20, 0x74, 0x68, 0x61, - 0x74, 0x20, 0x69, 0x74, 0x20, 0x77, 0x69, 0x6c, - 0x6c, 0x20, 0x62, 0x65, 0x20, 0x75, 0x73, 0x65, - 0x66, 0x75, 0x6c, 0x2c, 0x20, 0x62, 0x75, 0x74, - 0x0a, 0x20, 0x2a, 0x20, 0x57, 0x49, 0x54, 0x48, - 0x4f, 0x55, 0x54, 0x20, 0x41, 0x4e, 0x59, 0x20, - 0x57, 0x41, 0x52, 0x52, 0x41, 0x4e, 0x54, 0x59, - 0x3b, 0x20, 0x77, 0x69, 0x74, 0x68, 0x6f, 0x75, - 0x74, 0x20, 0x65, 0x76, 0x65, 0x6e, 0x20, 0x74, - 0x68, 0x65, 0x20, 0x69, 0x6d, 0x70, 0x6c, 0x69, - 0x65, 0x64, 0x20, 0x77, 0x61, 0x72, 0x72, 0x61, - 0x6e, 0x74, 0x79, 0x20, 0x6f, 0x66, 0x0a, 0x20, - 0x2a, 0x20, 0x4d, 0x45, 0x52, 0x43, 0x48, 0x41, - 0x4e, 0x54, 0x41, 0x42, 0x49, 0x4c, 0x49, 0x54, - 0x59, 0x20, 0x6f, 0x72, 0x20, 0x46, 0x49, 0x54, - 0x4e, 0x45, 0x53, 0x53, 0x20, 0x46, 0x4f, 0x52, - 0x20, 0x41, 0x20, 0x50, 0x41, 0x52, 0x54, 0x49, - 0x43, 0x55, 0x4c, 0x41, 0x52, 0x20, 0x50, 0x55, - 0x52, 0x50, 0x4f, 0x53, 0x45, 0x2e, 0x20, 0x20, - 0x53, 0x65, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, - 0x47, 0x4e, 0x55, 0x0a, 0x20, 0x2a, 0x20, 0x47, - 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x50, - 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20, 0x4c, 0x69, - 0x63, 0x65, 0x6e, 0x73, 0x65, 0x20, 0x66, 0x6f, - 0x72, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x64, - 0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 0x2e, 0x0a, - 0x20, 0x2a, 0x0a, 0x20, 0x2a, 0x20, 0x59, 0x6f, - 0x75, 0x20, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64, - 0x20, 0x68, 0x61, 0x76, 0x65, 0x20, 0x72, 0x65, - 0x63, 0x65, 0x69, 0x76, 0x65, 0x64, 0x20, 0x61, - 0x20, 0x63, 0x6f, 0x70, 0x79, 0x20, 0x6f, 0x66, - 0x20, 0x74, 0x68, 0x65, 0x20, 0x47, 0x4e, 0x55, - 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, - 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20, - 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x0a, - 0x20, 0x2a, 0x20, 0x61, 0x6c, 0x6f, 0x6e, 0x67, - 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x74, 0x68, - 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, - 0x61, 0x6d, 0x3b, 0x20, 0x73, 0x65, 0x65, 0x20, - 0x74, 0x68, 0x65, 0x20, 0x66, 0x69, 0x6c, 0x65, - 0x20, 0x43, 0x4f, 0x50, 0x59, 0x49, 0x4e, 0x47, - 0x2e, 0x20, 0x20, 0x49, 0x66, 0x20, 0x6e, 0x6f, - 0x74, 0x2c, 0x20, 0x77, 0x72, 0x69, 0x74, 0x65, - 0x20, 0x74, 0x6f, 0x0a, 0x20, 0x2a, 0x20, 0x74, - 0x68, 0x65, 0x20, 0x46, 0x72, 0x65, 0x65, 0x20, - 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, - 0x20, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x74, - 0x69, 0x6f, 0x6e, 0x2c, 0x20, 0x35, 0x31, 0x20, - 0x46, 0x72, 0x61, 0x6e, 0x6b, 0x6c, 0x69, 0x6e, - 0x20, 0x53, 0x74, 0x72, 0x65, 0x65, 0x74, 0x2c, - 0x20, 0x46, 0x69, 0x66, 0x74, 0x68, 0x20, 0x46, - 0x6c, 0x6f, 0x6f, 0x72, 0x2c, 0x0a, 0x20, 0x2a, - 0x20, 0x42, 0x6f, 0x73, 0x74, 0x6f, 0x6e, 0x2c, - 0x20, 0x4d, 0x41, 0x20, 0x30, 0x32, 0x31, 0x31, - 0x30, 0x2d, 0x31, 0x33, 0x30, 0x31, 0x2c, 0x20, - 0x55, 0x53, 0x41, 0x2e, 0x0a, 0x20, 0x2a, 0x2f, - 0x0a, -}; - -static object_data some = { - some_bytes, - sizeof(some_bytes), - "fd8430bc864cfcd5f10e5590f8a447e01b942bfe", - "blob", - "test-objects/fd", - "test-objects/fd/8430bc864cfcd5f10e5590f8a447e01b942bfe", - some_data, - sizeof(some_data), -}; diff --git a/tests-clar/odb/mixed.c b/tests-clar/odb/mixed.c deleted file mode 100644 index da0ed97d740..00000000000 --- a/tests-clar/odb/mixed.c +++ /dev/null @@ -1,25 +0,0 @@ -#include "clar_libgit2.h" -#include "odb.h" - -static git_odb *_odb; - -void test_odb_mixed__initialize(void) -{ - cl_git_pass(git_odb_open(&_odb, cl_fixture("duplicate.git/objects"))); -} - -void test_odb_mixed__cleanup(void) -{ - git_odb_free(_odb); - _odb = NULL; -} - -void test_odb_mixed__dup_oid(void) { - const char hex[] = "ce013625030ba8dba906f756967f9e9ca394464a"; - git_oid oid; - git_odb_object *obj; - cl_git_pass(git_oid_fromstr(&oid, hex)); - cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, GIT_OID_HEXSZ)); - git_odb_object_free(obj); -} - diff --git a/tests-clar/odb/packed.c b/tests-clar/odb/packed.c deleted file mode 100644 index 90e9f3abdab..00000000000 --- a/tests-clar/odb/packed.c +++ /dev/null @@ -1,79 +0,0 @@ -#include "clar_libgit2.h" -#include "odb.h" -#include "pack_data.h" - -static git_odb *_odb; - -void test_odb_packed__initialize(void) -{ - cl_git_pass(git_odb_open(&_odb, cl_fixture("testrepo.git/objects"))); -} - -void test_odb_packed__cleanup(void) -{ - git_odb_free(_odb); - _odb = NULL; -} - -void test_odb_packed__mass_read(void) -{ - unsigned int i; - - for (i = 0; i < ARRAY_SIZE(packed_objects); ++i) { - git_oid id; - git_odb_object *obj; - - cl_git_pass(git_oid_fromstr(&id, packed_objects[i])); - cl_assert(git_odb_exists(_odb, &id) == 1); - cl_git_pass(git_odb_read(&obj, _odb, &id)); - - git_odb_object_free(obj); - } -} - -void test_odb_packed__read_header_0(void) -{ - unsigned int i; - - for (i = 0; i < ARRAY_SIZE(packed_objects); ++i) { - git_oid id; - git_odb_object *obj; - size_t len; - git_otype type; - - cl_git_pass(git_oid_fromstr(&id, packed_objects[i])); - - cl_git_pass(git_odb_read(&obj, _odb, &id)); - cl_git_pass(git_odb_read_header(&len, &type, _odb, &id)); - - cl_assert(obj->raw.len == len); - cl_assert(obj->raw.type == type); - - git_odb_object_free(obj); - } -} - -void test_odb_packed__read_header_1(void) -{ - unsigned int i; - - for (i = 0; i < ARRAY_SIZE(loose_objects); ++i) { - git_oid id; - git_odb_object *obj; - size_t len; - git_otype type; - - cl_git_pass(git_oid_fromstr(&id, loose_objects[i])); - - cl_assert(git_odb_exists(_odb, &id) == 1); - - cl_git_pass(git_odb_read(&obj, _odb, &id)); - cl_git_pass(git_odb_read_header(&len, &type, _odb, &id)); - - cl_assert(obj->raw.len == len); - cl_assert(obj->raw.type == type); - - git_odb_object_free(obj); - } -} - diff --git a/tests-clar/odb/packed_one.c b/tests-clar/odb/packed_one.c deleted file mode 100644 index e9d246c234a..00000000000 --- a/tests-clar/odb/packed_one.c +++ /dev/null @@ -1,59 +0,0 @@ -#include "clar_libgit2.h" -#include "odb.h" -#include "pack_data_one.h" -#include "pack.h" - -static git_odb *_odb; - -void test_odb_packed_one__initialize(void) -{ - git_odb_backend *backend = NULL; - - cl_git_pass(git_odb_new(&_odb)); - cl_git_pass(git_odb_backend_one_pack(&backend, cl_fixture("testrepo.git/objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx"))); - cl_git_pass(git_odb_add_backend(_odb, backend, 1)); -} - -void test_odb_packed_one__cleanup(void) -{ - git_odb_free(_odb); - _odb = NULL; -} - -void test_odb_packed_one__mass_read(void) -{ - unsigned int i; - - for (i = 0; i < ARRAY_SIZE(packed_objects_one); ++i) { - git_oid id; - git_odb_object *obj; - - cl_git_pass(git_oid_fromstr(&id, packed_objects_one[i])); - cl_assert(git_odb_exists(_odb, &id) == 1); - cl_git_pass(git_odb_read(&obj, _odb, &id)); - - git_odb_object_free(obj); - } -} - -void test_odb_packed_one__read_header_0(void) -{ - unsigned int i; - - for (i = 0; i < ARRAY_SIZE(packed_objects_one); ++i) { - git_oid id; - git_odb_object *obj; - size_t len; - git_otype type; - - cl_git_pass(git_oid_fromstr(&id, packed_objects_one[i])); - - cl_git_pass(git_odb_read(&obj, _odb, &id)); - cl_git_pass(git_odb_read_header(&len, &type, _odb, &id)); - - cl_assert(obj->raw.len == len); - cl_assert(obj->raw.type == type); - - git_odb_object_free(obj); - } -} diff --git a/tests-clar/odb/sorting.c b/tests-clar/odb/sorting.c deleted file mode 100644 index b4f9e44bca4..00000000000 --- a/tests-clar/odb/sorting.c +++ /dev/null @@ -1,71 +0,0 @@ -#include "clar_libgit2.h" -#include "git2/odb_backend.h" -#include "odb.h" - -typedef struct { - git_odb_backend base; - int position; -} fake_backend; - -static git_odb_backend *new_backend(int position) -{ - fake_backend *b; - - b = git__calloc(1, sizeof(fake_backend)); - if (b == NULL) - return NULL; - - b->base.version = GIT_ODB_BACKEND_VERSION; - b->position = position; - return (git_odb_backend *)b; -} - -static void check_backend_sorting(git_odb *odb) -{ - unsigned int i; - - for (i = 0; i < odb->backends.length; ++i) { - fake_backend *internal = - *((fake_backend **)git_vector_get(&odb->backends, i)); - - cl_assert(internal != NULL); - cl_assert(internal->position == (int)i); - } -} - -static git_odb *_odb; - -void test_odb_sorting__initialize(void) -{ - cl_git_pass(git_odb_new(&_odb)); -} - -void test_odb_sorting__cleanup(void) -{ - git_odb_free(_odb); - _odb = NULL; -} - -void test_odb_sorting__basic_backends_sorting(void) -{ - cl_git_pass(git_odb_add_backend(_odb, new_backend(0), 5)); - cl_git_pass(git_odb_add_backend(_odb, new_backend(2), 3)); - cl_git_pass(git_odb_add_backend(_odb, new_backend(1), 4)); - cl_git_pass(git_odb_add_backend(_odb, new_backend(3), 1)); - - check_backend_sorting(_odb); -} - -void test_odb_sorting__alternate_backends_sorting(void) -{ - cl_git_pass(git_odb_add_backend(_odb, new_backend(0), 5)); - cl_git_pass(git_odb_add_backend(_odb, new_backend(2), 3)); - cl_git_pass(git_odb_add_backend(_odb, new_backend(1), 4)); - cl_git_pass(git_odb_add_backend(_odb, new_backend(3), 1)); - cl_git_pass(git_odb_add_alternate(_odb, new_backend(4), 5)); - cl_git_pass(git_odb_add_alternate(_odb, new_backend(6), 3)); - cl_git_pass(git_odb_add_alternate(_odb, new_backend(5), 4)); - cl_git_pass(git_odb_add_alternate(_odb, new_backend(7), 1)); - - check_backend_sorting(_odb); -} diff --git a/tests-clar/online/clone.c b/tests-clar/online/clone.c deleted file mode 100644 index 8226bd054da..00000000000 --- a/tests-clar/online/clone.c +++ /dev/null @@ -1,153 +0,0 @@ -#include "clar_libgit2.h" - -#include "git2/clone.h" -#include "git2/cred_helpers.h" -#include "repository.h" - -#define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository" -#define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository" - -static git_repository *g_repo; -static git_clone_options g_options; - -void test_online_clone__initialize(void) -{ - git_checkout_opts dummy_opts = GIT_CHECKOUT_OPTS_INIT; - - g_repo = NULL; - - memset(&g_options, 0, sizeof(git_clone_options)); - g_options.version = GIT_CLONE_OPTIONS_VERSION; - g_options.checkout_opts = dummy_opts; - g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; -} - -void test_online_clone__cleanup(void) -{ - if (g_repo) { - git_repository_free(g_repo); - g_repo = NULL; - } - cl_fixture_cleanup("./foo"); -} - -void test_online_clone__network_full(void) -{ - git_remote *origin; - - cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options)); - cl_assert(!git_repository_is_bare(g_repo)); - cl_git_pass(git_remote_load(&origin, g_repo, "origin")); - - git_remote_free(origin); -} - -void test_online_clone__network_bare(void) -{ - git_remote *origin; - - g_options.bare = true; - - cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options)); - cl_assert(git_repository_is_bare(g_repo)); - cl_git_pass(git_remote_load(&origin, g_repo, "origin")); - - git_remote_free(origin); -} - -void test_online_clone__empty_repository(void) -{ - git_reference *head; - - cl_git_pass(git_clone(&g_repo, LIVE_EMPTYREPO_URL, "./foo", &g_options)); - - cl_assert_equal_i(true, git_repository_is_empty(g_repo)); - cl_assert_equal_i(true, git_repository_head_orphan(g_repo)); - - cl_git_pass(git_reference_lookup(&head, g_repo, GIT_HEAD_FILE)); - cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head)); - cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head)); - - git_reference_free(head); -} - -static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload) -{ - bool *was_called = (bool*)payload; - GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot); - (*was_called) = true; -} - -static void fetch_progress(const git_transfer_progress *stats, void *payload) -{ - bool *was_called = (bool*)payload; - GIT_UNUSED(stats); - (*was_called) = true; -} - -void test_online_clone__can_checkout_a_cloned_repo(void) -{ - git_buf path = GIT_BUF_INIT; - git_reference *head; - bool checkout_progress_cb_was_called = false, - fetch_progress_cb_was_called = false; - - g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - g_options.checkout_opts.progress_cb = &checkout_progress; - g_options.checkout_opts.progress_payload = &checkout_progress_cb_was_called; - g_options.fetch_progress_cb = &fetch_progress; - g_options.fetch_progress_payload = &fetch_progress_cb_was_called; - - cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options)); - - cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt")); - cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&path))); - - cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD")); - cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head)); - cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head)); - - cl_assert_equal_i(true, checkout_progress_cb_was_called); - cl_assert_equal_i(true, fetch_progress_cb_was_called); - - git_reference_free(head); - git_buf_free(&path); -} - -static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *payload) -{ - int *callcount = (int*)payload; - GIT_UNUSED(refname); GIT_UNUSED(a); GIT_UNUSED(b); - *callcount = *callcount + 1; - return 0; -} - -void test_online_clone__custom_remote_callbacks(void) -{ - git_remote_callbacks remote_callbacks = GIT_REMOTE_CALLBACKS_INIT; - int callcount = 0; - - g_options.remote_callbacks = &remote_callbacks; - remote_callbacks.update_tips = update_tips; - remote_callbacks.payload = &callcount; - - cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options)); - cl_assert(callcount > 0); -} - -void test_online_clone__credentials(void) -{ - /* Remote URL environment variable must be set. User and password are optional. */ - const char *remote_url = cl_getenv("GITTEST_REMOTE_URL"); - git_cred_userpass_payload user_pass = { - cl_getenv("GITTEST_REMOTE_USER"), - cl_getenv("GITTEST_REMOTE_PASS") - }; - - if (!remote_url) return; - - g_options.cred_acquire_cb = git_cred_userpass; - g_options.cred_acquire_payload = &user_pass; - - cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options)); -} diff --git a/tests-clar/online/fetch.c b/tests-clar/online/fetch.c deleted file mode 100644 index 41cdb30e1e3..00000000000 --- a/tests-clar/online/fetch.c +++ /dev/null @@ -1,112 +0,0 @@ -#include "clar_libgit2.h" - -static git_repository *_repo; -static int counter; - -void test_online_fetch__initialize(void) -{ - cl_git_pass(git_repository_init(&_repo, "./fetch", 0)); -} - -void test_online_fetch__cleanup(void) -{ - git_repository_free(_repo); - _repo = NULL; - - cl_fixture_cleanup("./fetch"); -} - -static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *data) -{ - GIT_UNUSED(refname); GIT_UNUSED(a); GIT_UNUSED(b); GIT_UNUSED(data); - - ++counter; - - return 0; -} - -static void progress(const git_transfer_progress *stats, void *payload) -{ - size_t *bytes_received = (size_t *)payload; - *bytes_received = stats->received_bytes; -} - -static void do_fetch(const char *url, git_remote_autotag_option_t flag, int n) -{ - git_remote *remote; - git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT; - size_t bytes_received = 0; - - callbacks.update_tips = update_tips; - counter = 0; - - cl_git_pass(git_remote_create(&remote, _repo, "test", url)); - git_remote_set_callbacks(remote, &callbacks); - git_remote_set_autotag(remote, flag); - cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH)); - cl_git_pass(git_remote_download(remote, progress, &bytes_received)); - cl_git_pass(git_remote_update_tips(remote)); - git_remote_disconnect(remote); - cl_assert_equal_i(counter, n); - cl_assert(bytes_received > 0); - - git_remote_free(remote); -} - -void test_online_fetch__default_git(void) -{ - do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6); -} - -void test_online_fetch__default_http(void) -{ - do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6); -} - -void test_online_fetch__no_tags_git(void) -{ - do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3); -} - -void test_online_fetch__no_tags_http(void) -{ - do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3); -} - -static void transferProgressCallback(const git_transfer_progress *stats, void *payload) -{ - bool *invoked = (bool *)payload; - - GIT_UNUSED(stats); - *invoked = true; -} - -void test_online_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date(void) -{ - git_repository *_repository; - bool invoked = false; - git_remote *remote; - git_clone_options opts = GIT_CLONE_OPTIONS_INIT; - opts.bare = true; - - cl_git_pass(git_clone(&_repository, "https://github.com/libgit2/TestGitRepository.git", - "./fetch/lg2", &opts)); - git_repository_free(_repository); - - cl_git_pass(git_repository_open(&_repository, "./fetch/lg2")); - - cl_git_pass(git_remote_load(&remote, _repository, "origin")); - cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH)); - - cl_assert_equal_i(false, invoked); - - cl_git_pass(git_remote_download(remote, &transferProgressCallback, &invoked)); - - cl_assert_equal_i(false, invoked); - - cl_git_pass(git_remote_update_tips(remote)); - git_remote_disconnect(remote); - - git_remote_free(remote); - git_repository_free(_repository); -} diff --git a/tests-clar/online/fetchhead.c b/tests-clar/online/fetchhead.c deleted file mode 100644 index f892707414a..00000000000 --- a/tests-clar/online/fetchhead.c +++ /dev/null @@ -1,87 +0,0 @@ -#include "clar_libgit2.h" - -#include "repository.h" -#include "fetchhead.h" -#include "../fetchhead/fetchhead_data.h" -#include "git2/clone.h" - -#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository" - -static git_repository *g_repo; -static git_clone_options g_options; - -void test_online_fetchhead__initialize(void) -{ - g_repo = NULL; - - memset(&g_options, 0, sizeof(git_clone_options)); - g_options.version = GIT_CLONE_OPTIONS_VERSION; -} - -void test_online_fetchhead__cleanup(void) -{ - if (g_repo) { - git_repository_free(g_repo); - g_repo = NULL; - } - - cl_fixture_cleanup("./foo"); -} - -static void fetchhead_test_clone(void) -{ - cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options)); -} - -static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fetchhead) -{ - git_remote *remote; - git_buf fetchhead_buf = GIT_BUF_INIT; - int equals = 0; - - cl_git_pass(git_remote_load(&remote, g_repo, "origin")); - git_remote_set_autotag(remote, GIT_REMOTE_DOWNLOAD_TAGS_AUTO); - - if(fetchspec != NULL) - git_remote_set_fetchspec(remote, fetchspec); - - cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH)); - cl_git_pass(git_remote_download(remote, NULL, NULL)); - cl_git_pass(git_remote_update_tips(remote)); - git_remote_disconnect(remote); - git_remote_free(remote); - - cl_git_pass(git_futils_readbuffer(&fetchhead_buf, "./foo/.git/FETCH_HEAD")); - - equals = (strcmp(fetchhead_buf.ptr, expected_fetchhead) == 0); - - git_buf_free(&fetchhead_buf); - - cl_assert(equals); -} - -void test_online_fetchhead__wildcard_spec(void) -{ - fetchhead_test_clone(); - fetchhead_test_fetch(NULL, FETCH_HEAD_WILDCARD_DATA); -} - -void test_online_fetchhead__explicit_spec(void) -{ - fetchhead_test_clone(); - fetchhead_test_fetch("refs/heads/first-merge:refs/remotes/origin/first-merge", FETCH_HEAD_EXPLICIT_DATA); -} - -void test_online_fetchhead__no_merges(void) -{ - git_config *config; - - fetchhead_test_clone(); - - cl_git_pass(git_repository_config(&config, g_repo)); - cl_git_pass(git_config_set_string(config, "branch.master.remote", NULL)); - cl_git_pass(git_config_set_string(config, "branch.master.merge", NULL)); - git_config_free(config); - - fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA); -} diff --git a/tests-clar/online/push.c b/tests-clar/online/push.c deleted file mode 100644 index 8f92cdd5e30..00000000000 --- a/tests-clar/online/push.c +++ /dev/null @@ -1,699 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "posix.h" -#include "vector.h" -#include "../submodule/submodule_helpers.h" -#include "push_util.h" -#include "refspec.h" -#include "remote.h" - -static git_repository *_repo; - -static char *_remote_url; -static char *_remote_user; -static char *_remote_pass; - -static git_remote *_remote; -static bool _cred_acquire_called; -static record_callbacks_data _record_cbs_data = {{ 0 }}; -static git_remote_callbacks _record_cbs = RECORD_CALLBACKS_INIT(&_record_cbs_data); - -static git_oid _oid_b6; -static git_oid _oid_b5; -static git_oid _oid_b4; -static git_oid _oid_b3; -static git_oid _oid_b2; -static git_oid _oid_b1; - -static git_oid _tag_commit; -static git_oid _tag_tree; -static git_oid _tag_blob; -static git_oid _tag_lightweight; - -static int cred_acquire_cb(git_cred **cred, const char *url, unsigned int allowed_types, void *payload) -{ - GIT_UNUSED(url); - - *((bool*)payload) = true; - - if ((GIT_CREDTYPE_USERPASS_PLAINTEXT & allowed_types) == 0 || - git_cred_userpass_plaintext_new(cred, _remote_user, _remote_pass) < 0) - return -1; - - return 0; -} - -typedef struct { - const char *ref; - const char *msg; -} push_status; - -/** - * git_push_status_foreach callback that records status entries. - * @param data (git_vector *) of push_status instances - */ -static int record_push_status_cb(const char *ref, const char *msg, void *data) -{ - git_vector *statuses = (git_vector *)data; - push_status *s; - - cl_assert(s = git__malloc(sizeof(*s))); - s->ref = ref; - s->msg = msg; - - git_vector_insert(statuses, s); - - return 0; -} - -static void do_verify_push_status(git_push *push, const push_status expected[], const size_t expected_len) -{ - git_vector actual = GIT_VECTOR_INIT; - push_status *iter; - bool failed = false; - size_t i; - - git_push_status_foreach(push, record_push_status_cb, &actual); - - if (expected_len != actual.length) - failed = true; - else - git_vector_foreach(&actual, i, iter) - if (strcmp(expected[i].ref, iter->ref) || - (expected[i].msg && !iter->msg) || - (!expected[i].msg && iter->msg) || - (expected[i].msg && iter->msg && strcmp(expected[i].msg, iter->msg))) { - failed = true; - break; - } - - if (failed) { - git_buf msg = GIT_BUF_INIT; - - git_buf_puts(&msg, "Expected and actual push statuses differ:\nEXPECTED:\n"); - - for(i = 0; i < expected_len; i++) { - git_buf_printf(&msg, "%s: %s\n", - expected[i].ref, - expected[i].msg ? expected[i].msg : ""); - } - - git_buf_puts(&msg, "\nACTUAL:\n"); - - git_vector_foreach(&actual, i, iter) - git_buf_printf(&msg, "%s: %s\n", iter->ref, iter->msg); - - cl_fail(git_buf_cstr(&msg)); - - git_buf_free(&msg); - } - - git_vector_foreach(&actual, i, iter) - git__free(iter); - - git_vector_free(&actual); -} - -/** - * Verifies that after git_push_finish(), refs on a remote have the expected - * names, oids, and order. - * - * @param remote remote to verify - * @param expected_refs expected remote refs after push - * @param expected_refs_len length of expected_refs - */ -static void verify_refs(git_remote *remote, expected_ref expected_refs[], size_t expected_refs_len) -{ - git_vector actual_refs = GIT_VECTOR_INIT; - - git_remote_ls(remote, record_ref_cb, &actual_refs); - verify_remote_refs(&actual_refs, expected_refs, expected_refs_len); - - git_vector_free(&actual_refs); -} - -static int tracking_branch_list_cb(const char *branch_name, git_branch_t branch_type, void *payload) -{ - git_vector *tracking = (git_vector *)payload; - - if (branch_type == GIT_BRANCH_REMOTE) - git_vector_insert(tracking, git__strdup(branch_name)); - else - GIT_UNUSED(branch_name); - - return 0; -} - -/** - * Verifies that after git_push_update_tips(), remote tracking branches have the expected - * names and oids. - * - * @param remote remote to verify - * @param expected_refs expected remote refs after push - * @param expected_refs_len length of expected_refs - */ -static void verify_tracking_branches(git_remote *remote, expected_ref expected_refs[], size_t expected_refs_len) -{ - git_refspec *fetch_spec = &remote->fetch; - size_t i, j; - git_buf msg = GIT_BUF_INIT; - git_buf ref_name = GIT_BUF_INIT; - git_buf canonical_ref_name = GIT_BUF_INIT; - git_vector actual_refs = GIT_VECTOR_INIT; - char *actual_ref; - git_oid oid; - int failed = 0; - - /* Get current remote branches */ - cl_git_pass(git_branch_foreach(remote->repo, GIT_BRANCH_REMOTE, tracking_branch_list_cb, &actual_refs)); - - /* Loop through expected refs, make sure they exist */ - for (i = 0; i < expected_refs_len; i++) { - - /* Convert remote reference name into tracking branch name. - * If the spec is not under refs/heads/, then skip. - */ - if (!git_refspec_src_matches(fetch_spec, expected_refs[i].name)) - continue; - - cl_git_pass(git_refspec_transform_r(&ref_name, fetch_spec, expected_refs[i].name)); - - /* Find matching remote branch */ - git_vector_foreach(&actual_refs, j, actual_ref) { - - /* Construct canonical ref name from the actual_ref name */ - git_buf_clear(&canonical_ref_name); - cl_git_pass(git_buf_printf(&canonical_ref_name, "refs/remotes/%s", actual_ref)); - if (!strcmp(git_buf_cstr(&ref_name), git_buf_cstr(&canonical_ref_name))) - break; - } - - if (j == actual_refs.length) { - git_buf_printf(&msg, "Did not find expected tracking branch '%s'.", git_buf_cstr(&ref_name)); - failed = 1; - goto failed; - } - - /* Make sure tracking branch is at expected commit ID */ - cl_git_pass(git_reference_name_to_id(&oid, remote->repo, git_buf_cstr(&canonical_ref_name))); - - if (git_oid_cmp(expected_refs[i].oid, &oid) != 0) { - git_buf_puts(&msg, "Tracking branch commit does not match expected ID."); - failed = 1; - goto failed; - } - - git__free(actual_ref); - cl_git_pass(git_vector_remove(&actual_refs, j)); - } - - /* Make sure there are no extra branches */ - if (actual_refs.length > 0) { - git_buf_puts(&msg, "Unexpected remote tracking branches exist."); - failed = 1; - goto failed; - } - -failed: - - if(failed) - cl_fail(git_buf_cstr(&msg)); - - git_vector_foreach(&actual_refs, i, actual_ref) - git__free(actual_ref); - - git_vector_free(&actual_refs); - git_buf_free(&msg); - git_buf_free(&canonical_ref_name); - git_buf_free(&ref_name); - return; -} - -void test_online_push__initialize(void) -{ - git_vector delete_specs = GIT_VECTOR_INIT; - size_t i; - char *curr_del_spec; - _cred_acquire_called = false; - - _repo = cl_git_sandbox_init("push_src"); - - cl_fixture_sandbox("testrepo.git"); - cl_rename("push_src/submodule/.gitted", "push_src/submodule/.git"); - - rewrite_gitmodules(git_repository_workdir(_repo)); - - /* git log --format=oneline --decorate --graph - * *-. 951bbbb90e2259a4c8950db78946784fb53fcbce (HEAD, b6) merge b3, b4, and b5 to b6 - * |\ \ - * | | * fa38b91f199934685819bea316186d8b008c52a2 (b5) added submodule named 'submodule' pointing to '../testrepo.git' - * | * | 27b7ce66243eb1403862d05f958c002312df173d (b4) edited fold\b.txt - * | |/ - * * | d9b63a88223d8367516f50bd131a5f7349b7f3e4 (b3) edited a.txt - * |/ - * * a78705c3b2725f931d3ee05348d83cc26700f247 (b2, b1) added fold and fold/b.txt - * * 5c0bb3d1b9449d1cc69d7519fd05166f01840915 added a.txt - */ - git_oid_fromstr(&_oid_b6, "951bbbb90e2259a4c8950db78946784fb53fcbce"); - git_oid_fromstr(&_oid_b5, "fa38b91f199934685819bea316186d8b008c52a2"); - git_oid_fromstr(&_oid_b4, "27b7ce66243eb1403862d05f958c002312df173d"); - git_oid_fromstr(&_oid_b3, "d9b63a88223d8367516f50bd131a5f7349b7f3e4"); - git_oid_fromstr(&_oid_b2, "a78705c3b2725f931d3ee05348d83cc26700f247"); - git_oid_fromstr(&_oid_b1, "a78705c3b2725f931d3ee05348d83cc26700f247"); - - git_oid_fromstr(&_tag_commit, "805c54522e614f29f70d2413a0470247d8b424ac"); - git_oid_fromstr(&_tag_tree, "ff83aa4c5e5d28e3bcba2f5c6e2adc61286a4e5e"); - git_oid_fromstr(&_tag_blob, "b483ae7ba66decee9aee971f501221dea84b1498"); - git_oid_fromstr(&_tag_lightweight, "951bbbb90e2259a4c8950db78946784fb53fcbce"); - - /* Remote URL environment variable must be set. User and password are optional. */ - _remote_url = cl_getenv("GITTEST_REMOTE_URL"); - _remote_user = cl_getenv("GITTEST_REMOTE_USER"); - _remote_pass = cl_getenv("GITTEST_REMOTE_PASS"); - _remote = NULL; - - if (_remote_url) { - cl_git_pass(git_remote_create(&_remote, _repo, "test", _remote_url)); - - git_remote_set_cred_acquire_cb(_remote, cred_acquire_cb, &_cred_acquire_called); - record_callbacks_data_clear(&_record_cbs_data); - git_remote_set_callbacks(_remote, &_record_cbs); - - cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH)); - - /* Clean up previously pushed branches. Fails if receive.denyDeletes is - * set on the remote. Also, on Git 1.7.0 and newer, you must run - * 'git config receive.denyDeleteCurrent ignore' in the remote repo in - * order to delete the remote branch pointed to by HEAD (usually master). - * See: https://raw.github.com/git/git/master/Documentation/RelNotes/1.7.0.txt - */ - cl_git_pass(git_remote_ls(_remote, delete_ref_cb, &delete_specs)); - if (delete_specs.length) { - git_push *push; - - cl_git_pass(git_push_new(&push, _remote)); - - git_vector_foreach(&delete_specs, i, curr_del_spec) { - git_push_add_refspec(push, curr_del_spec); - git__free(curr_del_spec); - } - - cl_git_pass(git_push_finish(push)); - git_push_free(push); - } - - git_remote_disconnect(_remote); - git_vector_free(&delete_specs); - - /* Now that we've deleted everything, fetch from the remote */ - cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_FETCH)); - cl_git_pass(git_remote_download(_remote, NULL, NULL)); - cl_git_pass(git_remote_update_tips(_remote)); - git_remote_disconnect(_remote); - } else - printf("GITTEST_REMOTE_URL unset; skipping push test\n"); -} - -void test_online_push__cleanup(void) -{ - if (_remote) - git_remote_free(_remote); - _remote = NULL; - - /* Freed by cl_git_sandbox_cleanup */ - _repo = NULL; - - record_callbacks_data_clear(&_record_cbs_data); - - cl_fixture_cleanup("testrepo.git"); - cl_git_sandbox_cleanup(); -} - -/** - * Calls push and relists refs on remote to verify success. - * - * @param refspecs refspecs to push - * @param refspecs_len length of refspecs - * @param expected_refs expected remote refs after push - * @param expected_refs_len length of expected_refs - * @param expected_ret expected return value from git_push_finish() - */ -static void do_push(const char *refspecs[], size_t refspecs_len, - push_status expected_statuses[], size_t expected_statuses_len, - expected_ref expected_refs[], size_t expected_refs_len, int expected_ret) -{ - git_push *push; - size_t i; - int ret; - - if (_remote) { - cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH)); - - cl_git_pass(git_push_new(&push, _remote)); - - for (i = 0; i < refspecs_len; i++) - cl_git_pass(git_push_add_refspec(push, refspecs[i])); - - if (expected_ret < 0) { - cl_git_fail(ret = git_push_finish(push)); - cl_assert_equal_i(0, git_push_unpack_ok(push)); - } - else { - cl_git_pass(ret = git_push_finish(push)); - cl_assert_equal_i(1, git_push_unpack_ok(push)); - } - - do_verify_push_status(push, expected_statuses, expected_statuses_len); - - cl_assert_equal_i(expected_ret, ret); - - verify_refs(_remote, expected_refs, expected_refs_len); - - cl_git_pass(git_push_update_tips(push)); - verify_tracking_branches(_remote, expected_refs, expected_refs_len); - - git_push_free(push); - - git_remote_disconnect(_remote); - } -} - -/* Call push_finish() without ever calling git_push_add_refspec() */ -void test_online_push__noop(void) -{ - do_push(NULL, 0, NULL, 0, NULL, 0, 0); -} - -void test_online_push__b1(void) -{ - const char *specs[] = { "refs/heads/b1:refs/heads/b1" }; - push_status exp_stats[] = { { "refs/heads/b1", NULL } }; - expected_ref exp_refs[] = { { "refs/heads/b1", &_oid_b1 } }; - do_push(specs, ARRAY_SIZE(specs), - exp_stats, ARRAY_SIZE(exp_stats), - exp_refs, ARRAY_SIZE(exp_refs), 0); -} - -void test_online_push__b2(void) -{ - const char *specs[] = { "refs/heads/b2:refs/heads/b2" }; - push_status exp_stats[] = { { "refs/heads/b2", NULL } }; - expected_ref exp_refs[] = { { "refs/heads/b2", &_oid_b2 } }; - do_push(specs, ARRAY_SIZE(specs), - exp_stats, ARRAY_SIZE(exp_stats), - exp_refs, ARRAY_SIZE(exp_refs), 0); -} - -void test_online_push__b3(void) -{ - const char *specs[] = { "refs/heads/b3:refs/heads/b3" }; - push_status exp_stats[] = { { "refs/heads/b3", NULL } }; - expected_ref exp_refs[] = { { "refs/heads/b3", &_oid_b3 } }; - do_push(specs, ARRAY_SIZE(specs), - exp_stats, ARRAY_SIZE(exp_stats), - exp_refs, ARRAY_SIZE(exp_refs), 0); -} - -void test_online_push__b4(void) -{ - const char *specs[] = { "refs/heads/b4:refs/heads/b4" }; - push_status exp_stats[] = { { "refs/heads/b4", NULL } }; - expected_ref exp_refs[] = { { "refs/heads/b4", &_oid_b4 } }; - do_push(specs, ARRAY_SIZE(specs), - exp_stats, ARRAY_SIZE(exp_stats), - exp_refs, ARRAY_SIZE(exp_refs), 0); -} - -void test_online_push__b5(void) -{ - const char *specs[] = { "refs/heads/b5:refs/heads/b5" }; - push_status exp_stats[] = { { "refs/heads/b5", NULL } }; - expected_ref exp_refs[] = { { "refs/heads/b5", &_oid_b5 } }; - do_push(specs, ARRAY_SIZE(specs), - exp_stats, ARRAY_SIZE(exp_stats), - exp_refs, ARRAY_SIZE(exp_refs), 0); -} - -void test_online_push__multi(void) -{ - const char *specs[] = { - "refs/heads/b1:refs/heads/b1", - "refs/heads/b2:refs/heads/b2", - "refs/heads/b3:refs/heads/b3", - "refs/heads/b4:refs/heads/b4", - "refs/heads/b5:refs/heads/b5" - }; - push_status exp_stats[] = { - { "refs/heads/b1", NULL }, - { "refs/heads/b2", NULL }, - { "refs/heads/b3", NULL }, - { "refs/heads/b4", NULL }, - { "refs/heads/b5", NULL } - }; - expected_ref exp_refs[] = { - { "refs/heads/b1", &_oid_b1 }, - { "refs/heads/b2", &_oid_b2 }, - { "refs/heads/b3", &_oid_b3 }, - { "refs/heads/b4", &_oid_b4 }, - { "refs/heads/b5", &_oid_b5 } - }; - do_push(specs, ARRAY_SIZE(specs), - exp_stats, ARRAY_SIZE(exp_stats), - exp_refs, ARRAY_SIZE(exp_refs), 0); -} - -void test_online_push__implicit_tgt(void) -{ - const char *specs1[] = { "refs/heads/b1:" }; - push_status exp_stats1[] = { { "refs/heads/b1", NULL } }; - expected_ref exp_refs1[] = { { "refs/heads/b1", &_oid_b1 } }; - - const char *specs2[] = { "refs/heads/b2:" }; - push_status exp_stats2[] = { { "refs/heads/b2", NULL } }; - expected_ref exp_refs2[] = { - { "refs/heads/b1", &_oid_b1 }, - { "refs/heads/b2", &_oid_b2 } - }; - - do_push(specs1, ARRAY_SIZE(specs1), - exp_stats1, ARRAY_SIZE(exp_stats1), - exp_refs1, ARRAY_SIZE(exp_refs1), 0); - do_push(specs2, ARRAY_SIZE(specs2), - exp_stats2, ARRAY_SIZE(exp_stats2), - exp_refs2, ARRAY_SIZE(exp_refs2), 0); -} - -void test_online_push__fast_fwd(void) -{ - /* Fast forward b1 in tgt from _oid_b1 to _oid_b6. */ - - const char *specs_init[] = { "refs/heads/b1:refs/heads/b1" }; - push_status exp_stats_init[] = { { "refs/heads/b1", NULL } }; - expected_ref exp_refs_init[] = { { "refs/heads/b1", &_oid_b1 } }; - - const char *specs_ff[] = { "refs/heads/b6:refs/heads/b1" }; - push_status exp_stats_ff[] = { { "refs/heads/b1", NULL } }; - expected_ref exp_refs_ff[] = { { "refs/heads/b1", &_oid_b6 } }; - - /* Do a force push to reset b1 in target back to _oid_b1 */ - const char *specs_reset[] = { "+refs/heads/b1:refs/heads/b1" }; - /* Force should have no effect on a fast forward push */ - const char *specs_ff_force[] = { "+refs/heads/b6:refs/heads/b1" }; - - do_push(specs_init, ARRAY_SIZE(specs_init), - exp_stats_init, ARRAY_SIZE(exp_stats_init), - exp_refs_init, ARRAY_SIZE(exp_refs_init), 0); - - do_push(specs_ff, ARRAY_SIZE(specs_ff), - exp_stats_ff, ARRAY_SIZE(exp_stats_ff), - exp_refs_ff, ARRAY_SIZE(exp_refs_ff), 0); - - do_push(specs_reset, ARRAY_SIZE(specs_reset), - exp_stats_init, ARRAY_SIZE(exp_stats_init), - exp_refs_init, ARRAY_SIZE(exp_refs_init), 0); - - do_push(specs_ff_force, ARRAY_SIZE(specs_ff_force), - exp_stats_ff, ARRAY_SIZE(exp_stats_ff), - exp_refs_ff, ARRAY_SIZE(exp_refs_ff), 0); -} - -void test_online_push__tag_commit(void) -{ - const char *specs[] = { "refs/tags/tag-commit:refs/tags/tag-commit" }; - push_status exp_stats[] = { { "refs/tags/tag-commit", NULL } }; - expected_ref exp_refs[] = { { "refs/tags/tag-commit", &_tag_commit } }; - do_push(specs, ARRAY_SIZE(specs), - exp_stats, ARRAY_SIZE(exp_stats), - exp_refs, ARRAY_SIZE(exp_refs), 0); -} - -void test_online_push__tag_tree(void) -{ - const char *specs[] = { "refs/tags/tag-tree:refs/tags/tag-tree" }; - push_status exp_stats[] = { { "refs/tags/tag-tree", NULL } }; - expected_ref exp_refs[] = { { "refs/tags/tag-tree", &_tag_tree } }; - do_push(specs, ARRAY_SIZE(specs), - exp_stats, ARRAY_SIZE(exp_stats), - exp_refs, ARRAY_SIZE(exp_refs), 0); -} - -void test_online_push__tag_blob(void) -{ - const char *specs[] = { "refs/tags/tag-blob:refs/tags/tag-blob" }; - push_status exp_stats[] = { { "refs/tags/tag-blob", NULL } }; - expected_ref exp_refs[] = { { "refs/tags/tag-blob", &_tag_blob } }; - do_push(specs, ARRAY_SIZE(specs), - exp_stats, ARRAY_SIZE(exp_stats), - exp_refs, ARRAY_SIZE(exp_refs), 0); -} - -void test_online_push__tag_lightweight(void) -{ - const char *specs[] = { "refs/tags/tag-lightweight:refs/tags/tag-lightweight" }; - push_status exp_stats[] = { { "refs/tags/tag-lightweight", NULL } }; - expected_ref exp_refs[] = { { "refs/tags/tag-lightweight", &_tag_lightweight } }; - do_push(specs, ARRAY_SIZE(specs), - exp_stats, ARRAY_SIZE(exp_stats), - exp_refs, ARRAY_SIZE(exp_refs), 0); -} - -void test_online_push__force(void) -{ - const char *specs1[] = {"refs/heads/b3:refs/heads/tgt"}; - push_status exp_stats1[] = { { "refs/heads/tgt", NULL } }; - expected_ref exp_refs1[] = { { "refs/heads/tgt", &_oid_b3 } }; - - const char *specs2[] = {"refs/heads/b4:refs/heads/tgt"}; - - const char *specs2_force[] = {"+refs/heads/b4:refs/heads/tgt"}; - push_status exp_stats2_force[] = { { "refs/heads/tgt", NULL } }; - expected_ref exp_refs2_force[] = { { "refs/heads/tgt", &_oid_b4 } }; - - do_push(specs1, ARRAY_SIZE(specs1), - exp_stats1, ARRAY_SIZE(exp_stats1), - exp_refs1, ARRAY_SIZE(exp_refs1), 0); - - do_push(specs2, ARRAY_SIZE(specs2), - NULL, 0, - exp_refs1, ARRAY_SIZE(exp_refs1), GIT_ENONFASTFORWARD); - - /* Non-fast-forward update with force should pass. */ - do_push(specs2_force, ARRAY_SIZE(specs2_force), - exp_stats2_force, ARRAY_SIZE(exp_stats2_force), - exp_refs2_force, ARRAY_SIZE(exp_refs2_force), 0); -} - -void test_online_push__delete(void) -{ - const char *specs1[] = { - "refs/heads/b1:refs/heads/tgt1", - "refs/heads/b1:refs/heads/tgt2" - }; - push_status exp_stats1[] = { - { "refs/heads/tgt1", NULL }, - { "refs/heads/tgt2", NULL } - }; - expected_ref exp_refs1[] = { - { "refs/heads/tgt1", &_oid_b1 }, - { "refs/heads/tgt2", &_oid_b1 } - }; - - const char *specs_del_fake[] = { ":refs/heads/fake" }; - /* Force has no effect for delete. */ - const char *specs_del_fake_force[] = { "+:refs/heads/fake" }; - push_status exp_stats_fake[] = { { "refs/heads/fake", NULL } }; - - const char *specs_delete[] = { ":refs/heads/tgt1" }; - push_status exp_stats_delete[] = { { "refs/heads/tgt1", NULL } }; - expected_ref exp_refs_delete[] = { { "refs/heads/tgt2", &_oid_b1 } }; - /* Force has no effect for delete. */ - const char *specs_delete_force[] = { "+:refs/heads/tgt1" }; - - do_push(specs1, ARRAY_SIZE(specs1), - exp_stats1, ARRAY_SIZE(exp_stats1), - exp_refs1, ARRAY_SIZE(exp_refs1), 0); - - /* When deleting a non-existent branch, the git client sends zero for both - * the old and new commit id. This should succeed on the server with the - * same status report as if the branch were actually deleted. The server - * returns a warning on the side-band iff the side-band is supported. - * Since libgit2 doesn't support the side-band yet, there are no warnings. - */ - do_push(specs_del_fake, ARRAY_SIZE(specs_del_fake), - exp_stats_fake, 1, - exp_refs1, ARRAY_SIZE(exp_refs1), 0); - do_push(specs_del_fake_force, ARRAY_SIZE(specs_del_fake_force), - exp_stats_fake, 1, - exp_refs1, ARRAY_SIZE(exp_refs1), 0); - - /* Delete one of the pushed branches. */ - do_push(specs_delete, ARRAY_SIZE(specs_delete), - exp_stats_delete, ARRAY_SIZE(exp_stats_delete), - exp_refs_delete, ARRAY_SIZE(exp_refs_delete), 0); - - /* Re-push branches and retry delete with force. */ - do_push(specs1, ARRAY_SIZE(specs1), - exp_stats1, ARRAY_SIZE(exp_stats1), - exp_refs1, ARRAY_SIZE(exp_refs1), 0); - do_push(specs_delete_force, ARRAY_SIZE(specs_delete_force), - exp_stats_delete, ARRAY_SIZE(exp_stats_delete), - exp_refs_delete, ARRAY_SIZE(exp_refs_delete), 0); -} - -void test_online_push__bad_refspecs(void) -{ - /* All classes of refspecs that should be rejected by - * git_push_add_refspec() should go in this test. - */ - git_push *push; - - if (_remote) { -// cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH)); - cl_git_pass(git_push_new(&push, _remote)); - - /* Unexpanded branch names not supported */ - cl_git_fail(git_push_add_refspec(push, "b6:b6")); - - git_push_free(push); - } -} - -void test_online_push__expressions(void) -{ - /* TODO: Expressions in refspecs doesn't actually work yet */ - const char *specs_left_expr[] = { "refs/heads/b2~1:refs/heads/b2" }; - - const char *specs_right_expr[] = { "refs/heads/b2:refs/heads/b2~1" }; - push_status exp_stats_right_expr[] = { { "refs/heads/b2~1", "funny refname" } }; - - /* TODO: Find a more precise way of checking errors than a exit code of -1. */ - do_push(specs_left_expr, ARRAY_SIZE(specs_left_expr), - NULL, 0, - NULL, 0, -1); - - do_push(specs_right_expr, ARRAY_SIZE(specs_right_expr), - exp_stats_right_expr, ARRAY_SIZE(exp_stats_right_expr), - NULL, 0, 0); -} - -void test_online_push__notes(void) -{ - git_oid note_oid, *target_oid, expected_oid; - git_signature *signature; - const char *specs[] = { "refs/notes/commits:refs/notes/commits" }; - push_status exp_stats[] = { { "refs/notes/commits", NULL } }; - expected_ref exp_refs[] = { { "refs/notes/commits", &expected_oid } }; - git_oid_fromstr(&expected_oid, "8461a99b27b7043e58ff6e1f5d2cf07d282534fb"); - - target_oid = &_oid_b6; - - /* Create note to push */ - cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */ - cl_git_pass(git_note_create(¬e_oid, _repo, signature, signature, NULL, target_oid, "hello world\n", 0)); - - do_push(specs, ARRAY_SIZE(specs), - exp_stats, ARRAY_SIZE(exp_stats), - exp_refs, ARRAY_SIZE(exp_refs), 0); - - git_signature_free(signature); -} diff --git a/tests-clar/online/push_util.c b/tests-clar/online/push_util.c deleted file mode 100644 index 2e457844d55..00000000000 --- a/tests-clar/online/push_util.c +++ /dev/null @@ -1,126 +0,0 @@ - -#include "clar_libgit2.h" -#include "buffer.h" -#include "vector.h" -#include "push_util.h" - -const git_oid OID_ZERO = {{ 0 }}; - -void updated_tip_free(updated_tip *t) -{ - git__free(t->name); - git__free(t->old_oid); - git__free(t->new_oid); - git__free(t); -} - -void record_callbacks_data_clear(record_callbacks_data *data) -{ - size_t i; - updated_tip *tip; - - git_vector_foreach(&data->updated_tips, i, tip) - updated_tip_free(tip); - - git_vector_free(&data->updated_tips); -} - -int record_update_tips_cb(const char *refname, const git_oid *a, const git_oid *b, void *data) -{ - updated_tip *t; - record_callbacks_data *record_data = (record_callbacks_data *)data; - - cl_assert(t = git__malloc(sizeof(*t))); - - cl_assert(t->name = git__strdup(refname)); - cl_assert(t->old_oid = git__malloc(sizeof(*t->old_oid))); - git_oid_cpy(t->old_oid, a); - - cl_assert(t->new_oid = git__malloc(sizeof(*t->new_oid))); - git_oid_cpy(t->new_oid, b); - - git_vector_insert(&record_data->updated_tips, t); - - return 0; -} - -int delete_ref_cb(git_remote_head *head, void *payload) -{ - git_vector *delete_specs = (git_vector *)payload; - git_buf del_spec = GIT_BUF_INIT; - - /* Ignore malformed ref names (which also saves us from tag^{} */ - if (!git_reference_is_valid_name(head->name)) - return 0; - - /* Create a refspec that deletes a branch in the remote */ - if (strcmp(head->name, "refs/heads/master")) { - cl_git_pass(git_buf_putc(&del_spec, ':')); - cl_git_pass(git_buf_puts(&del_spec, head->name)); - cl_git_pass(git_vector_insert(delete_specs, git_buf_detach(&del_spec))); - } - - return 0; -} - -int record_ref_cb(git_remote_head *head, void *payload) -{ - git_vector *refs = (git_vector *) payload; - return git_vector_insert(refs, head); -} - -void verify_remote_refs(git_vector *actual_refs, const expected_ref expected_refs[], size_t expected_refs_len) -{ - size_t i, j = 0; - git_buf msg = GIT_BUF_INIT; - git_remote_head *actual; - char *oid_str; - bool master_present = false; - - /* We don't care whether "master" is present on the other end or not */ - git_vector_foreach(actual_refs, i, actual) { - if (!strcmp(actual->name, "refs/heads/master")) { - master_present = true; - break; - } - } - - if (expected_refs_len + (master_present ? 1 : 0) != actual_refs->length) - goto failed; - - git_vector_foreach(actual_refs, i, actual) { - if (master_present && !strcmp(actual->name, "refs/heads/master")) - continue; - - if (strcmp(expected_refs[j].name, actual->name) || - git_oid_cmp(expected_refs[j].oid, &actual->oid)) - goto failed; - - j++; - } - - return; - -failed: - git_buf_puts(&msg, "Expected and actual refs differ:\nEXPECTED:\n"); - - for(i = 0; i < expected_refs_len; i++) { - cl_assert(oid_str = git_oid_allocfmt(expected_refs[i].oid)); - cl_git_pass(git_buf_printf(&msg, "%s = %s\n", expected_refs[i].name, oid_str)); - git__free(oid_str); - } - - git_buf_puts(&msg, "\nACTUAL:\n"); - git_vector_foreach(actual_refs, i, actual) { - if (master_present && !strcmp(actual->name, "refs/heads/master")) - continue; - - cl_assert(oid_str = git_oid_allocfmt(&actual->oid)); - cl_git_pass(git_buf_printf(&msg, "%s = %s\n", actual->name, oid_str)); - git__free(oid_str); - } - - cl_fail(git_buf_cstr(&msg)); - - git_buf_free(&msg); -} diff --git a/tests-clar/online/push_util.h b/tests-clar/online/push_util.h deleted file mode 100644 index 759122aa620..00000000000 --- a/tests-clar/online/push_util.h +++ /dev/null @@ -1,69 +0,0 @@ -#ifndef INCLUDE_cl_push_util_h__ -#define INCLUDE_cl_push_util_h__ - -#include "git2/oid.h" - -/* Constant for zero oid */ -extern const git_oid OID_ZERO; - -/** - * Macro for initializing git_remote_callbacks to use test helpers that - * record data in a record_callbacks_data instance. - * @param data pointer to a record_callbacks_data instance - */ -#define RECORD_CALLBACKS_INIT(data) \ - { GIT_REMOTE_CALLBACKS_VERSION, NULL, NULL, record_update_tips_cb, data } - -typedef struct { - char *name; - git_oid *old_oid; - git_oid *new_oid; -} updated_tip; - -typedef struct { - git_vector updated_tips; -} record_callbacks_data; - -typedef struct { - const char *name; - const git_oid *oid; -} expected_ref; - -void updated_tip_free(updated_tip *t); - -void record_callbacks_data_clear(record_callbacks_data *data); - -/** - * Callback for git_remote_update_tips that records updates - * - * @param data (git_vector *) of updated_tip instances - */ -int record_update_tips_cb(const char *refname, const git_oid *a, const git_oid *b, void *data); - -/** - * Callback for git_remote_list that adds refspecs to delete each ref - * - * @param head a ref on the remote - * @param payload a git_push instance - */ -int delete_ref_cb(git_remote_head *head, void *payload); - -/** - * Callback for git_remote_list that adds refspecs to vector - * - * @param head a ref on the remote - * @param payload (git_vector *) of git_remote_head instances - */ -int record_ref_cb(git_remote_head *head, void *payload); - -/** - * Verifies that refs on remote stored by record_ref_cb match the expected - * names, oids, and order. - * - * @param actual_refs actual refs stored by record_ref_cb() - * @param expected_refs expected remote refs - * @param expected_refs_len length of expected_refs - */ -void verify_remote_refs(git_vector *actual_refs, const expected_ref expected_refs[], size_t expected_refs_len); - -#endif /* INCLUDE_cl_push_util_h__ */ diff --git a/tests-clar/pack/packbuilder.c b/tests-clar/pack/packbuilder.c deleted file mode 100644 index 51377878142..00000000000 --- a/tests-clar/pack/packbuilder.c +++ /dev/null @@ -1,105 +0,0 @@ -#include "clar_libgit2.h" -#include "iterator.h" -#include "vector.h" -#include "posix.h" - -static git_repository *_repo; -static git_revwalk *_revwalker; -static git_packbuilder *_packbuilder; -static git_indexer *_indexer; -static git_vector _commits; -static int _commits_is_initialized; - -void test_pack_packbuilder__initialize(void) -{ - _repo = cl_git_sandbox_init("testrepo.git"); - cl_git_pass(git_revwalk_new(&_revwalker, _repo)); - cl_git_pass(git_packbuilder_new(&_packbuilder, _repo)); - cl_git_pass(git_vector_init(&_commits, 0, NULL)); - _commits_is_initialized = 1; -} - -void test_pack_packbuilder__cleanup(void) -{ - git_oid *o; - unsigned int i; - - if (_commits_is_initialized) { - _commits_is_initialized = 0; - git_vector_foreach(&_commits, i, o) { - git__free(o); - } - git_vector_free(&_commits); - } - - git_packbuilder_free(_packbuilder); - _packbuilder = NULL; - - git_revwalk_free(_revwalker); - _revwalker = NULL; - - git_indexer_free(_indexer); - _indexer = NULL; - - cl_git_sandbox_cleanup(); - _repo = NULL; -} - -static void seed_packbuilder(void) -{ - git_oid oid, *o; - unsigned int i; - - git_revwalk_sorting(_revwalker, GIT_SORT_TIME); - cl_git_pass(git_revwalk_push_ref(_revwalker, "HEAD")); - - while (git_revwalk_next(&oid, _revwalker) == 0) { - o = git__malloc(GIT_OID_RAWSZ); - cl_assert(o != NULL); - git_oid_cpy(o, &oid); - cl_git_pass(git_vector_insert(&_commits, o)); - } - - git_vector_foreach(&_commits, i, o) { - cl_git_pass(git_packbuilder_insert(_packbuilder, o, NULL)); - } - - git_vector_foreach(&_commits, i, o) { - git_object *obj; - cl_git_pass(git_object_lookup(&obj, _repo, o, GIT_OBJ_COMMIT)); - cl_git_pass(git_packbuilder_insert_tree(_packbuilder, - git_commit_tree_id((git_commit *)obj))); - git_object_free(obj); - } -} - -void test_pack_packbuilder__create_pack(void) -{ - git_transfer_progress stats; - - seed_packbuilder(); - cl_git_pass(git_packbuilder_write(_packbuilder, "testpack.pack")); - - cl_git_pass(git_indexer_new(&_indexer, "testpack.pack")); - cl_git_pass(git_indexer_run(_indexer, &stats)); - cl_git_pass(git_indexer_write(_indexer)); -} - -static git_transfer_progress stats; -static int foreach_cb(void *buf, size_t len, void *payload) -{ - git_indexer_stream *idx = (git_indexer_stream *) payload; - cl_git_pass(git_indexer_stream_add(idx, buf, len, &stats)); - return 0; -} - -void test_pack_packbuilder__foreach(void) -{ - git_indexer_stream *idx; - - seed_packbuilder(); - cl_git_pass(git_indexer_stream_new(&idx, ".", NULL, NULL)); - cl_git_pass(git_packbuilder_foreach(_packbuilder, foreach_cb, idx)); - cl_git_pass(git_indexer_stream_finalize(idx, &stats)); - git_indexer_stream_free(idx); -} diff --git a/tests-clar/refs/branches/create.c b/tests-clar/refs/branches/create.c deleted file mode 100644 index 693a592a33d..00000000000 --- a/tests-clar/refs/branches/create.c +++ /dev/null @@ -1,76 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" - -static git_repository *repo; -static git_commit *target; -static git_reference *branch; - -void test_refs_branches_create__initialize(void) -{ - cl_fixture_sandbox("testrepo.git"); - cl_git_pass(git_repository_open(&repo, "testrepo.git")); - - branch = NULL; -} - -void test_refs_branches_create__cleanup(void) -{ - git_reference_free(branch); - branch = NULL; - - git_commit_free(target); - target = NULL; - - git_repository_free(repo); - repo = NULL; - - cl_fixture_cleanup("testrepo.git"); -} - -static void retrieve_target_from_oid(git_commit **out, git_repository *repo, const char *sha) -{ - git_oid oid; - - cl_git_pass(git_oid_fromstr(&oid, sha)); - cl_git_pass(git_commit_lookup(out, repo, &oid)); -} - -static void retrieve_known_commit(git_commit **commit, git_repository *repo) -{ - retrieve_target_from_oid(commit, repo, "e90810b8df3e80c413d903f631643c716887138d"); -} - -#define NEW_BRANCH_NAME "new-branch-on-the-block" - -void test_refs_branches_create__can_create_a_local_branch(void) -{ - retrieve_known_commit(&target, repo); - - cl_git_pass(git_branch_create(&branch, repo, NEW_BRANCH_NAME, target, 0)); - cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target))); -} - -void test_refs_branches_create__can_not_create_a_branch_if_its_name_collide_with_an_existing_one(void) -{ - retrieve_known_commit(&target, repo); - - cl_assert_equal_i(GIT_EEXISTS, git_branch_create(&branch, repo, "br2", target, 0)); -} - -void test_refs_branches_create__can_force_create_over_an_existing_branch(void) -{ - retrieve_known_commit(&target, repo); - - cl_git_pass(git_branch_create(&branch, repo, "br2", target, 1)); - cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target))); - cl_assert_equal_s("refs/heads/br2", git_reference_name(branch)); -} - - -void test_refs_branches_create__creating_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void) -{ - retrieve_known_commit(&target, repo); - - cl_assert_equal_i(GIT_EINVALIDSPEC, - git_branch_create(&branch, repo, "inv@{id", target, 0)); -} \ No newline at end of file diff --git a/tests-clar/refs/branches/delete.c b/tests-clar/refs/branches/delete.c deleted file mode 100644 index 21fbc09bb28..00000000000 --- a/tests-clar/refs/branches/delete.c +++ /dev/null @@ -1,110 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" -#include "repo/repo_helpers.h" -#include "config/config_helpers.h" - -static git_repository *repo; -static git_reference *fake_remote; - -void test_refs_branches_delete__initialize(void) -{ - git_oid id; - - cl_fixture_sandbox("testrepo.git"); - cl_git_pass(git_repository_open(&repo, "testrepo.git")); - - cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644")); - cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0)); -} - -void test_refs_branches_delete__cleanup(void) -{ - git_reference_free(fake_remote); - fake_remote = NULL; - - git_repository_free(repo); - repo = NULL; - - cl_fixture_cleanup("testrepo.git"); -} - -void test_refs_branches_delete__can_not_delete_a_branch_pointed_at_by_HEAD(void) -{ - git_reference *head; - git_reference *branch; - - /* Ensure HEAD targets the local master branch */ - cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE)); - cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head)); - git_reference_free(head); - - cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL)); - cl_git_fail(git_branch_delete(branch)); - git_reference_free(branch); -} - -void test_refs_branches_delete__can_delete_a_branch_even_if_HEAD_is_missing(void) -{ - git_reference *head; - git_reference *branch; - - cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE)); - git_reference_delete(head); - - cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL)); - cl_git_pass(git_branch_delete(branch)); -} - -void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_orphaned(void) -{ - git_reference *branch; - - make_head_orphaned(repo, NON_EXISTING_HEAD); - - cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL)); - cl_git_pass(git_branch_delete(branch)); -} - -void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void) -{ - git_reference *head, *branch; - - cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE)); - cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head)); - cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head)); - git_reference_free(head); - - /* Detach HEAD and make it target the commit that "master" points to */ - git_repository_detach_head(repo); - - cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL)); - cl_git_pass(git_branch_delete(branch)); -} - -void test_refs_branches_delete__can_delete_a_local_branch(void) -{ - git_reference *branch; - cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL)); - cl_git_pass(git_branch_delete(branch)); -} - -void test_refs_branches_delete__can_delete_a_remote_branch(void) -{ - git_reference *branch; - cl_git_pass(git_branch_lookup(&branch, repo, "nulltoken/master", GIT_BRANCH_REMOTE)); - cl_git_pass(git_branch_delete(branch)); -} - -void test_refs_branches_delete__deleting_a_branch_removes_related_configuration_data(void) -{ - git_reference *branch; - - assert_config_entry_existence(repo, "branch.track-local.remote", true); - assert_config_entry_existence(repo, "branch.track-local.merge", true); - - cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL)); - cl_git_pass(git_branch_delete(branch)); - - assert_config_entry_existence(repo, "branch.track-local.remote", false); - assert_config_entry_existence(repo, "branch.track-local.merge", false); -} diff --git a/tests-clar/refs/branches/foreach.c b/tests-clar/refs/branches/foreach.c deleted file mode 100644 index 96a5bc2b9f4..00000000000 --- a/tests-clar/refs/branches/foreach.c +++ /dev/null @@ -1,155 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" - -static git_repository *repo; -static git_reference *fake_remote; - -void test_refs_branches_foreach__initialize(void) -{ - git_oid id; - - cl_fixture_sandbox("testrepo.git"); - cl_git_pass(git_repository_open(&repo, "testrepo.git")); - - cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644")); - cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0)); -} - -void test_refs_branches_foreach__cleanup(void) -{ - git_reference_free(fake_remote); - fake_remote = NULL; - - git_repository_free(repo); - repo = NULL; - - cl_fixture_cleanup("testrepo.git"); -} - -static int count_branch_list_cb(const char *branch_name, git_branch_t branch_type, void *payload) -{ - int *count; - - GIT_UNUSED(branch_type); - GIT_UNUSED(branch_name); - - count = (int *)payload; - (*count)++; - - return 0; -} - -static void assert_retrieval(unsigned int flags, unsigned int expected_count) -{ - int count = 0; - - cl_git_pass(git_branch_foreach(repo, flags, count_branch_list_cb, &count)); - - cl_assert_equal_i(expected_count, count); -} - -void test_refs_branches_foreach__retrieve_all_branches(void) -{ - assert_retrieval(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, 14); -} - -void test_refs_branches_foreach__retrieve_remote_branches(void) -{ - assert_retrieval(GIT_BRANCH_REMOTE, 2); -} - -void test_refs_branches_foreach__retrieve_local_branches(void) -{ - assert_retrieval(GIT_BRANCH_LOCAL, 12); -} - -struct expectations { - const char *branch_name; - int encounters; -}; - -static void assert_branch_has_been_found(struct expectations *findings, const char* expected_branch_name) -{ - int pos = 0; - - while (findings[pos].branch_name) - { - if (strcmp(expected_branch_name, findings[pos].branch_name) == 0) { - cl_assert_equal_i(1, findings[pos].encounters); - return; - } - - pos++; - } - - cl_fail("expected branch not found in list."); -} - -static int contains_branch_list_cb(const char *branch_name, git_branch_t branch_type, void *payload) -{ - int pos = 0; - struct expectations *exp; - - GIT_UNUSED(branch_type); - - exp = (struct expectations *)payload; - - while (exp[pos].branch_name) - { - if (strcmp(branch_name, exp[pos].branch_name) == 0) - exp[pos].encounters++; - - pos++; - } - - return 0; -} - -/* - * $ git branch -r - * nulltoken/HEAD -> nulltoken/master - * nulltoken/master - */ -void test_refs_branches_foreach__retrieve_remote_symbolic_HEAD_when_present(void) -{ - struct expectations exp[] = { - { "nulltoken/HEAD", 0 }, - { "nulltoken/master", 0 }, - { NULL, 0 } - }; - - git_reference_free(fake_remote); - cl_git_pass(git_reference_symbolic_create(&fake_remote, repo, "refs/remotes/nulltoken/HEAD", "refs/remotes/nulltoken/master", 0)); - - assert_retrieval(GIT_BRANCH_REMOTE, 3); - - cl_git_pass(git_branch_foreach(repo, GIT_BRANCH_REMOTE, contains_branch_list_cb, &exp)); - - assert_branch_has_been_found(exp, "nulltoken/HEAD"); - assert_branch_has_been_found(exp, "nulltoken/HEAD"); -} - -static int branch_list_interrupt_cb( - const char *branch_name, git_branch_t branch_type, void *payload) -{ - int *count; - - GIT_UNUSED(branch_type); - GIT_UNUSED(branch_name); - - count = (int *)payload; - (*count)++; - - return (*count == 5); -} - -void test_refs_branches_foreach__can_cancel(void) -{ - int count = 0; - - cl_assert_equal_i(GIT_EUSER, - git_branch_foreach(repo, GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, - branch_list_interrupt_cb, &count)); - - cl_assert_equal_i(5, count); -} diff --git a/tests-clar/refs/branches/lookup.c b/tests-clar/refs/branches/lookup.c deleted file mode 100644 index 95d49a4b35c..00000000000 --- a/tests-clar/refs/branches/lookup.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" - -static git_repository *repo; -static git_reference *branch; - -void test_refs_branches_lookup__initialize(void) -{ - cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); - - branch = NULL; -} - -void test_refs_branches_lookup__cleanup(void) -{ - git_reference_free(branch); - branch = NULL; - - git_repository_free(repo); - repo = NULL; -} - -void test_refs_branches_lookup__can_retrieve_a_local_branch(void) -{ - cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL)); -} - -void test_refs_branches_lookup__can_retrieve_a_remote_tracking_branch(void) -{ - cl_git_pass(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_REMOTE)); -} - -void test_refs_branches_lookup__trying_to_retrieve_an_unknown_branch_returns_ENOTFOUND(void) -{ - cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "where/are/you", GIT_BRANCH_LOCAL)); - cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "over/here", GIT_BRANCH_REMOTE)); -} - -void test_refs_branches_lookup__trying_to_retrieve_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void) -{ - cl_assert_equal_i(GIT_EINVALIDSPEC, - git_branch_lookup(&branch, repo, "are/you/inv@{id", GIT_BRANCH_LOCAL)); - cl_assert_equal_i(GIT_EINVALIDSPEC, - git_branch_lookup(&branch, repo, "yes/i am", GIT_BRANCH_REMOTE)); -} diff --git a/tests-clar/refs/branches/move.c b/tests-clar/refs/branches/move.c deleted file mode 100644 index 17fb6dfe681..00000000000 --- a/tests-clar/refs/branches/move.c +++ /dev/null @@ -1,106 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" -#include "config/config_helpers.h" - -static git_repository *repo; -static git_reference *ref; - -void test_refs_branches_move__initialize(void) -{ - repo = cl_git_sandbox_init("testrepo.git"); - - cl_git_pass(git_reference_lookup(&ref, repo, "refs/heads/br2")); -} - -void test_refs_branches_move__cleanup(void) -{ - git_reference_free(ref); - ref = NULL; - - cl_git_sandbox_cleanup(); -} - -#define NEW_BRANCH_NAME "new-branch-on-the-block" - -void test_refs_branches_move__can_move_a_local_branch(void) -{ - cl_git_pass(git_branch_move(ref, NEW_BRANCH_NAME, 0)); - cl_assert_equal_s(GIT_REFS_HEADS_DIR NEW_BRANCH_NAME, git_reference_name(ref)); -} - -void test_refs_branches_move__can_move_a_local_branch_to_a_different_namespace(void) -{ - /* Downward */ - cl_git_pass(git_branch_move(ref, "somewhere/" NEW_BRANCH_NAME, 0)); - - /* Upward */ - cl_git_pass(git_branch_move(ref, "br2", 0)); -} - -void test_refs_branches_move__can_move_a_local_branch_to_a_partially_colliding_namespace(void) -{ - /* Downward */ - cl_git_pass(git_branch_move(ref, "br2/" NEW_BRANCH_NAME, 0)); - - /* Upward */ - cl_git_pass(git_branch_move(ref, "br2", 0)); -} - -void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_collide_with_an_existing_one(void) -{ - cl_assert_equal_i(GIT_EEXISTS, git_branch_move(ref, "master", 0)); -} - -void test_refs_branches_move__moving_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void) -{ - cl_assert_equal_i(GIT_EINVALIDSPEC, git_branch_move(ref, "Inv@{id", 0)); -} - -void test_refs_branches_move__can_not_move_a_non_branch(void) -{ - git_reference *tag; - - cl_git_pass(git_reference_lookup(&tag, repo, "refs/tags/e90810b")); - cl_git_fail(git_branch_move(tag, NEW_BRANCH_NAME, 0)); - - git_reference_free(tag); -} - -void test_refs_branches_move__can_force_move_over_an_existing_branch(void) -{ - cl_git_pass(git_branch_move(ref, "master", 1)); -} - -void test_refs_branches_move__moving_a_branch_moves_related_configuration_data(void) -{ - git_reference *branch; - - cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL)); - - assert_config_entry_existence(repo, "branch.track-local.remote", true); - assert_config_entry_existence(repo, "branch.track-local.merge", true); - assert_config_entry_existence(repo, "branch.moved.remote", false); - assert_config_entry_existence(repo, "branch.moved.merge", false); - - cl_git_pass(git_branch_move(branch, "moved", 0)); - - assert_config_entry_existence(repo, "branch.track-local.remote", false); - assert_config_entry_existence(repo, "branch.track-local.merge", false); - assert_config_entry_existence(repo, "branch.moved.remote", true); - assert_config_entry_existence(repo, "branch.moved.merge", true); - - git_reference_free(branch); -} - -void test_refs_branches_move__moving_the_branch_pointed_at_by_HEAD_updates_HEAD(void) -{ - git_reference *branch; - - cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master")); - cl_git_pass(git_branch_move(branch, "master2", 0)); - git_reference_free(branch); - - cl_git_pass(git_repository_head(&branch, repo)); - cl_assert_equal_s("refs/heads/master2", git_reference_name(branch)); - git_reference_free(branch); -} diff --git a/tests-clar/refs/branches/name.c b/tests-clar/refs/branches/name.c deleted file mode 100644 index 176f836a426..00000000000 --- a/tests-clar/refs/branches/name.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "clar_libgit2.h" -#include "branch.h" - -static git_repository *repo; -static git_reference *ref; - -void test_refs_branches_name__initialize(void) -{ - cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); -} - -void test_refs_branches_name__cleanup(void) -{ - git_reference_free(ref); - ref = NULL; - - git_repository_free(repo); - repo = NULL; -} - -void test_refs_branches_name__can_get_local_branch_name(void) -{ - const char *name; - - cl_git_pass(git_branch_lookup(&ref,repo,"master",GIT_BRANCH_LOCAL)); - cl_git_pass(git_branch_name(&name,ref)); - cl_assert_equal_s("master",name); -} - -void test_refs_branches_name__can_get_remote_branch_name(void) -{ - const char *name; - - cl_git_pass(git_branch_lookup(&ref,repo,"test/master",GIT_BRANCH_REMOTE)); - cl_git_pass(git_branch_name(&name,ref)); - cl_assert_equal_s("test/master",name); -} - -void test_refs_branches_name__error_when_ref_is_no_branch(void) -{ - const char *name; - - cl_git_pass(git_reference_lookup(&ref,repo,"refs/notes/fanout")); - cl_git_fail(git_branch_name(&name,ref)); -} diff --git a/tests-clar/refs/branches/tracking.c b/tests-clar/refs/branches/tracking.c deleted file mode 100644 index 30599d9fcd7..00000000000 --- a/tests-clar/refs/branches/tracking.c +++ /dev/null @@ -1,95 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" - -static git_repository *repo; -static git_reference *branch, *tracking; - -void test_refs_branches_tracking__initialize(void) -{ - cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); - - branch = NULL; - tracking = NULL; -} - -void test_refs_branches_tracking__cleanup(void) -{ - git_reference_free(tracking); - git_reference_free(branch); - branch = NULL; - - git_repository_free(repo); - repo = NULL; -} - -void test_refs_branches_tracking__can_retrieve_the_remote_tracking_reference_of_a_local_branch(void) -{ - cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master")); - - cl_git_pass(git_branch_tracking(&tracking, branch)); - - cl_assert_equal_s("refs/remotes/test/master", git_reference_name(tracking)); -} - -void test_refs_branches_tracking__can_retrieve_the_local_tracking_reference_of_a_local_branch(void) -{ - cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/track-local")); - - cl_git_pass(git_branch_tracking(&tracking, branch)); - - cl_assert_equal_s("refs/heads/master", git_reference_name(tracking)); -} - -void test_refs_branches_tracking__cannot_retrieve_a_remote_tracking_reference_from_a_non_branch(void) -{ - cl_git_pass(git_reference_lookup(&branch, repo, "refs/tags/e90810b")); - - cl_git_fail(git_branch_tracking(&tracking, branch)); -} - -void test_refs_branches_tracking__trying_to_retrieve_a_remote_tracking_reference_from_a_plain_local_branch_returns_GIT_ENOTFOUND(void) -{ - cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/subtrees")); - - cl_assert_equal_i(GIT_ENOTFOUND, git_branch_tracking(&tracking, branch)); -} - -void test_refs_branches_tracking__trying_to_retrieve_a_remote_tracking_reference_from_a_branch_with_no_fetchspec_returns_GIT_ENOTFOUND(void) -{ - cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/cannot-fetch")); - - cl_assert_equal_i(GIT_ENOTFOUND, git_branch_tracking(&tracking, branch)); -} - -static void assert_merge_and_or_remote_key_missing(git_repository *repository, const git_commit *target, const char *entry_name) -{ - git_reference *branch; - - cl_assert_equal_i(GIT_OBJ_COMMIT, git_object_type((git_object*)target)); - cl_git_pass(git_branch_create(&branch, repository, entry_name, (git_commit*)target, 0)); - - cl_assert_equal_i(GIT_ENOTFOUND, git_branch_tracking(&tracking, branch)); - - git_reference_free(branch); -} - -void test_refs_branches_tracking__retrieve_a_remote_tracking_reference_from_a_branch_with_no_remote_returns_GIT_ENOTFOUND(void) -{ - git_reference *head; - git_repository *repository; - git_commit *target; - - repository = cl_git_sandbox_init("testrepo.git"); - - cl_git_pass(git_repository_head(&head, repository)); - cl_git_pass(git_reference_peel(((git_object **)&target), head, GIT_OBJ_COMMIT)); - git_reference_free(head); - - assert_merge_and_or_remote_key_missing(repository, target, "remoteless"); - assert_merge_and_or_remote_key_missing(repository, target, "mergeless"); - assert_merge_and_or_remote_key_missing(repository, target, "mergeandremoteless"); - - git_commit_free(target); - - cl_git_sandbox_cleanup(); -} diff --git a/tests-clar/refs/branches/trackingname.c b/tests-clar/refs/branches/trackingname.c deleted file mode 100644 index ea9058357d0..00000000000 --- a/tests-clar/refs/branches/trackingname.c +++ /dev/null @@ -1,42 +0,0 @@ -#include "clar_libgit2.h" -#include "branch.h" - -static git_repository *repo; -static git_buf tracking_name; - -void test_refs_branches_trackingname__initialize(void) -{ - cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); - - git_buf_init(&tracking_name, 0); -} - -void test_refs_branches_trackingname__cleanup(void) -{ - git_buf_free(&tracking_name); - - git_repository_free(repo); - repo = NULL; -} - -void test_refs_branches_trackingname__can_retrieve_the_remote_tracking_reference_name_of_a_local_branch(void) -{ - cl_git_pass(git_branch_tracking__name( - &tracking_name, repo, "refs/heads/master")); - - cl_assert_equal_s("refs/remotes/test/master", git_buf_cstr(&tracking_name)); -} - -void test_refs_branches_trackingname__can_retrieve_the_local_tracking_reference_name_of_a_local_branch(void) -{ - cl_git_pass(git_branch_tracking__name( - &tracking_name, repo, "refs/heads/track-local")); - - cl_assert_equal_s("refs/heads/master", git_buf_cstr(&tracking_name)); -} - -void test_refs_branches_trackingname__can_return_the_size_of_thelocal_tracking_reference_name_of_a_local_branch(void) -{ - cl_assert_equal_i(strlen("refs/heads/master") + 1, - git_branch_tracking_name(NULL, 0, repo, "refs/heads/track-local")); -} diff --git a/tests-clar/refs/crashes.c b/tests-clar/refs/crashes.c deleted file mode 100644 index 9fb5ff62708..00000000000 --- a/tests-clar/refs/crashes.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "clar_libgit2.h" - -void test_refs_crashes__double_free(void) -{ - git_repository *repo; - git_reference *ref, *ref2; - const char *REFNAME = "refs/heads/xxx"; - - cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); - cl_git_pass(git_reference_symbolic_create(&ref, repo, REFNAME, "refs/heads/master", 0)); - cl_git_pass(git_reference_lookup(&ref2, repo, REFNAME)); - cl_git_pass(git_reference_delete(ref)); - /* reference is gone from disk, so reloading it will fail */ - cl_git_fail(git_reference_reload(ref2)); - - git_repository_free(repo); -} diff --git a/tests-clar/refs/create.c b/tests-clar/refs/create.c deleted file mode 100644 index 56c323d8ac7..00000000000 --- a/tests-clar/refs/create.c +++ /dev/null @@ -1,167 +0,0 @@ -#include "clar_libgit2.h" - -#include "repository.h" -#include "git2/reflog.h" -#include "reflog.h" - -static const char *current_master_tip = "099fabac3a9ea935598528c27f866e34089c2eff"; -static const char *current_head_target = "refs/heads/master"; - -static git_repository *g_repo; - -void test_refs_create__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_refs_create__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_refs_create__symbolic(void) -{ - // create a new symbolic reference - git_reference *new_reference, *looked_up_ref, *resolved_ref; - git_repository *repo2; - git_oid id; - - const char *new_head_tracker = "ANOTHER_HEAD_TRACKER"; - - git_oid_fromstr(&id, current_master_tip); - - /* Create and write the new symbolic reference */ - cl_git_pass(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, current_head_target, 0)); - - /* Ensure the reference can be looked-up... */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker)); - cl_assert(git_reference_type(looked_up_ref) & GIT_REF_SYMBOLIC); - cl_assert(git_reference_is_packed(looked_up_ref) == 0); - cl_assert_equal_s(looked_up_ref->name, new_head_tracker); - - /* ...peeled.. */ - cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref)); - cl_assert(git_reference_type(resolved_ref) == GIT_REF_OID); - - /* ...and that it points to the current master tip */ - cl_assert(git_oid_cmp(&id, git_reference_target(resolved_ref)) == 0); - git_reference_free(looked_up_ref); - git_reference_free(resolved_ref); - - /* Similar test with a fresh new repository */ - cl_git_pass(git_repository_open(&repo2, "testrepo")); - - cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head_tracker)); - cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref)); - cl_assert(git_oid_cmp(&id, git_reference_target(resolved_ref)) == 0); - - git_repository_free(repo2); - - git_reference_free(new_reference); - git_reference_free(looked_up_ref); - git_reference_free(resolved_ref); -} - -void test_refs_create__deep_symbolic(void) -{ - // create a deep symbolic reference - git_reference *new_reference, *looked_up_ref, *resolved_ref; - git_oid id; - - const char *new_head_tracker = "deep/rooted/tracker"; - - git_oid_fromstr(&id, current_master_tip); - - cl_git_pass(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, current_head_target, 0)); - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker)); - cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref)); - cl_assert(git_oid_cmp(&id, git_reference_target(resolved_ref)) == 0); - - git_reference_free(new_reference); - git_reference_free(looked_up_ref); - git_reference_free(resolved_ref); -} - -void test_refs_create__oid(void) -{ - // create a new OID reference - git_reference *new_reference, *looked_up_ref; - git_repository *repo2; - git_oid id; - - const char *new_head = "refs/heads/new-head"; - - git_oid_fromstr(&id, current_master_tip); - - /* Create and write the new object id reference */ - cl_git_pass(git_reference_create(&new_reference, g_repo, new_head, &id, 0)); - - /* Ensure the reference can be looked-up... */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head)); - cl_assert(git_reference_type(looked_up_ref) & GIT_REF_OID); - cl_assert(git_reference_is_packed(looked_up_ref) == 0); - cl_assert_equal_s(looked_up_ref->name, new_head); - - /* ...and that it points to the current master tip */ - cl_assert(git_oid_cmp(&id, git_reference_target(looked_up_ref)) == 0); - git_reference_free(looked_up_ref); - - /* Similar test with a fresh new repository */ - cl_git_pass(git_repository_open(&repo2, "testrepo")); - - cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head)); - cl_assert(git_oid_cmp(&id, git_reference_target(looked_up_ref)) == 0); - - git_repository_free(repo2); - - git_reference_free(new_reference); - git_reference_free(looked_up_ref); -} - -void test_refs_create__oid_unknown(void) -{ - // Can not create a new OID reference which targets at an unknown id - git_reference *new_reference, *looked_up_ref; - git_oid id; - - const char *new_head = "refs/heads/new-head"; - - git_oid_fromstr(&id, "deadbeef3f795b2b4353bcce3a527ad0a4f7f644"); - - /* Create and write the new object id reference */ - cl_git_fail(git_reference_create(&new_reference, g_repo, new_head, &id, 0)); - - /* Ensure the reference can't be looked-up... */ - cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, new_head)); -} - -void test_refs_create__propagate_eexists(void) -{ - int error; - git_oid oid; - git_reference *ref; - - /* Make sure it works for oid and for symbolic both */ - git_oid_fromstr(&oid, current_master_tip); - error = git_reference_create(&ref, g_repo, current_head_target, &oid, false); - cl_assert(error == GIT_EEXISTS); - - error = git_reference_symbolic_create(&ref, g_repo, "HEAD", current_head_target, false); - cl_assert(error == GIT_EEXISTS); -} - -void test_refs_create__creating_a_reference_with_an_invalid_name_returns_EINVALIDSPEC(void) -{ - git_reference *new_reference; - git_oid id; - - const char *name = "refs/heads/inv@{id"; - - git_oid_fromstr(&id, current_master_tip); - - cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_create( - &new_reference, g_repo, name, &id, 0)); - - cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_symbolic_create( - &new_reference, g_repo, name, current_head_target, 0)); -} diff --git a/tests-clar/refs/delete.c b/tests-clar/refs/delete.c deleted file mode 100644 index cc5ab394096..00000000000 --- a/tests-clar/refs/delete.c +++ /dev/null @@ -1,85 +0,0 @@ -#include "clar_libgit2.h" - -#include "repository.h" -#include "git2/reflog.h" -#include "reflog.h" - -static const char *packed_test_head_name = "refs/heads/packed-test"; -static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"; - -static git_repository *g_repo; - - - -void test_refs_delete__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_refs_delete__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - - - -void test_refs_delete__packed_loose(void) -{ - // deleting a ref which is both packed and loose should remove both tracks in the filesystem - git_reference *looked_up_ref, *another_looked_up_ref; - git_buf temp_path = GIT_BUF_INIT; - - /* Ensure the loose reference exists on the file system */ - cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, packed_test_head_name)); - cl_assert(git_path_exists(temp_path.ptr)); - - /* Lookup the reference */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name)); - - /* Ensure it's the loose version that has been found */ - cl_assert(git_reference_is_packed(looked_up_ref) == 0); - - /* Now that the reference is deleted... */ - cl_git_pass(git_reference_delete(looked_up_ref)); - - /* Looking up the reference once again should not retrieve it */ - cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name)); - - /* Ensure the loose reference doesn't exist any longer on the file system */ - cl_assert(!git_path_exists(temp_path.ptr)); - - git_reference_free(another_looked_up_ref); - git_buf_free(&temp_path); -} - -void test_refs_delete__packed_only(void) -{ - // can delete a just packed reference - git_reference *ref; - git_oid id; - const char *new_ref = "refs/heads/new_ref"; - - git_oid_fromstr(&id, current_master_tip); - - /* Create and write the new object id reference */ - cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &id, 0)); - git_reference_free(ref); - - /* Lookup the reference */ - cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref)); - - /* Ensure it's a loose reference */ - cl_assert(git_reference_is_packed(ref) == 0); - - /* Pack all existing references */ - cl_git_pass(git_reference_packall(g_repo)); - - /* Reload the reference from disk */ - cl_git_pass(git_reference_reload(ref)); - - /* Ensure it's a packed reference */ - cl_assert(git_reference_is_packed(ref) == 1); - - /* This should pass */ - cl_git_pass(git_reference_delete(ref)); -} diff --git a/tests-clar/refs/foreachglob.c b/tests-clar/refs/foreachglob.c deleted file mode 100644 index 88516ddcec9..00000000000 --- a/tests-clar/refs/foreachglob.c +++ /dev/null @@ -1,95 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" - -static git_repository *repo; -static git_reference *fake_remote; - -void test_refs_foreachglob__initialize(void) -{ - git_oid id; - - cl_fixture_sandbox("testrepo.git"); - cl_git_pass(git_repository_open(&repo, "testrepo.git")); - - cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644")); - cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0)); -} - -void test_refs_foreachglob__cleanup(void) -{ - git_reference_free(fake_remote); - fake_remote = NULL; - - git_repository_free(repo); - repo = NULL; - - cl_fixture_cleanup("testrepo.git"); -} - -static int count_cb(const char *reference_name, void *payload) -{ - int *count = (int *)payload; - - GIT_UNUSED(reference_name); - - (*count)++; - - return 0; -} - -static void assert_retrieval(const char *glob, unsigned int flags, int expected_count) -{ - int count = 0; - - cl_git_pass(git_reference_foreach_glob(repo, glob, flags, count_cb, &count)); - - cl_assert_equal_i(expected_count, count); -} - -void test_refs_foreachglob__retrieve_all_refs(void) -{ - /* 8 heads (including one packed head) + 1 note + 2 remotes + 6 tags */ - assert_retrieval("*", GIT_REF_LISTALL, 21); -} - -void test_refs_foreachglob__retrieve_remote_branches(void) -{ - assert_retrieval("refs/remotes/*", GIT_REF_LISTALL, 2); -} - -void test_refs_foreachglob__retrieve_local_branches(void) -{ - assert_retrieval("refs/heads/*", GIT_REF_LISTALL, 12); -} - -void test_refs_foreachglob__retrieve_partially_named_references(void) -{ - /* - * refs/heads/packed-test, refs/heads/test - * refs/remotes/test/master, refs/tags/test - */ - - assert_retrieval("*test*", GIT_REF_LISTALL, 4); -} - - -static int interrupt_cb(const char *reference_name, void *payload) -{ - int *count = (int *)payload; - - GIT_UNUSED(reference_name); - - (*count)++; - - return (*count == 11); -} - -void test_refs_foreachglob__can_cancel(void) -{ - int count = 0; - - cl_assert_equal_i(GIT_EUSER, git_reference_foreach_glob( - repo, "*", GIT_REF_LISTALL, interrupt_cb, &count) ); - - cl_assert_equal_i(11, count); -} diff --git a/tests-clar/refs/isvalidname.c b/tests-clar/refs/isvalidname.c deleted file mode 100644 index b61a023602c..00000000000 --- a/tests-clar/refs/isvalidname.c +++ /dev/null @@ -1,29 +0,0 @@ -#include "clar_libgit2.h" - -void test_refs_isvalidname__can_detect_invalid_formats(void) -{ - cl_assert_equal_i(false, git_reference_is_valid_name("refs/tags/0.17.0^{}")); - cl_assert_equal_i(false, git_reference_is_valid_name("TWO/LEVELS")); - cl_assert_equal_i(false, git_reference_is_valid_name("ONE.LEVEL")); - cl_assert_equal_i(false, git_reference_is_valid_name("HEAD/")); - cl_assert_equal_i(false, git_reference_is_valid_name("NO_TRAILING_UNDERSCORE_")); - cl_assert_equal_i(false, git_reference_is_valid_name("_NO_LEADING_UNDERSCORE")); - cl_assert_equal_i(false, git_reference_is_valid_name("HEAD/aa")); - cl_assert_equal_i(false, git_reference_is_valid_name("lower_case")); - cl_assert_equal_i(false, git_reference_is_valid_name("/stupid/name/master")); - cl_assert_equal_i(false, git_reference_is_valid_name("/")); - cl_assert_equal_i(false, git_reference_is_valid_name("")); -} - -void test_refs_isvalidname__wont_hopefully_choke_on_valid_formats(void) -{ - cl_assert_equal_i(true, git_reference_is_valid_name("refs/tags/0.17.0")); - cl_assert_equal_i(true, git_reference_is_valid_name("refs/LEVELS")); - cl_assert_equal_i(true, git_reference_is_valid_name("HEAD")); - cl_assert_equal_i(true, git_reference_is_valid_name("ONE_LEVEL")); - cl_assert_equal_i(true, git_reference_is_valid_name("refs/stash")); - cl_assert_equal_i(true, git_reference_is_valid_name("refs/remotes/origin/bim_with_3d@11296")); - cl_assert_equal_i(true, git_reference_is_valid_name("refs/master{yesterday")); - cl_assert_equal_i(true, git_reference_is_valid_name("refs/master}yesterday")); - cl_assert_equal_i(true, git_reference_is_valid_name("refs/master{yesterday}")); -} diff --git a/tests-clar/refs/list.c b/tests-clar/refs/list.c deleted file mode 100644 index 3948b2b7a4a..00000000000 --- a/tests-clar/refs/list.c +++ /dev/null @@ -1,68 +0,0 @@ -#include "clar_libgit2.h" - -#include "repository.h" -#include "git2/reflog.h" -#include "reflog.h" - -static git_repository *g_repo; - - - -void test_refs_list__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_refs_list__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - - - -void test_refs_list__all(void) -{ - // try to list all the references in our test repo - git_strarray ref_list; - - cl_git_pass(git_reference_list(&ref_list, g_repo, GIT_REF_LISTALL)); - - /*{ - unsigned short i; - for (i = 0; i < ref_list.count; ++i) - printf("# %s\n", ref_list.strings[i]); - }*/ - - /* We have exactly 12 refs in total if we include the packed ones: - * there is a reference that exists both in the packfile and as - * loose, but we only list it once */ - cl_assert_equal_i((int)ref_list.count, 13); - - git_strarray_free(&ref_list); -} - -void test_refs_list__symbolic_only(void) -{ - // try to list only the symbolic references - git_strarray ref_list; - - cl_git_pass(git_reference_list(&ref_list, g_repo, GIT_REF_SYMBOLIC)); - cl_assert(ref_list.count == 0); /* no symrefs in the test repo */ - - git_strarray_free(&ref_list); -} - -void test_refs_list__do_not_retrieve_references_which_name_end_with_a_lock_extension(void) -{ - git_strarray ref_list; - - /* Create a fake locked reference */ - cl_git_mkfile( - "./testrepo/.git/refs/heads/hanwen.lock", - "144344043ba4d4a405da03de3844aa829ae8be0e\n"); - - cl_git_pass(git_reference_list(&ref_list, g_repo, GIT_REF_LISTALL)); - cl_assert_equal_i((int)ref_list.count, 13); - - git_strarray_free(&ref_list); -} diff --git a/tests-clar/refs/lookup.c b/tests-clar/refs/lookup.c deleted file mode 100644 index 11fd68f90c2..00000000000 --- a/tests-clar/refs/lookup.c +++ /dev/null @@ -1,42 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" - -static git_repository *g_repo; - -void test_refs_lookup__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo.git"); -} - -void test_refs_lookup__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_refs_lookup__with_resolve(void) -{ - git_reference *a, *b, *temp; - - cl_git_pass(git_reference_lookup(&temp, g_repo, "HEAD")); - cl_git_pass(git_reference_resolve(&a, temp)); - git_reference_free(temp); - - cl_git_pass(git_reference_lookup_resolved(&b, g_repo, "HEAD", 5)); - cl_assert(git_reference_cmp(a, b) == 0); - git_reference_free(b); - - cl_git_pass(git_reference_lookup_resolved(&b, g_repo, "HEAD_TRACKER", 5)); - cl_assert(git_reference_cmp(a, b) == 0); - git_reference_free(b); - - git_reference_free(a); -} - -void test_refs_lookup__oid(void) -{ - git_oid tag, expected; - - cl_git_pass(git_reference_name_to_id(&tag, g_repo, "refs/tags/point_to_blob")); - cl_git_pass(git_oid_fromstr(&expected, "1385f264afb75a56a5bec74243be9b367ba4ca08")); - cl_assert(git_oid_cmp(&tag, &expected) == 0); -} diff --git a/tests-clar/refs/normalize.c b/tests-clar/refs/normalize.c deleted file mode 100644 index 562c34f06f4..00000000000 --- a/tests-clar/refs/normalize.c +++ /dev/null @@ -1,401 +0,0 @@ -#include "clar_libgit2.h" - -#include "repository.h" -#include "git2/reflog.h" -#include "reflog.h" - -// Helpers -static void ensure_refname_normalized( - unsigned int flags, - const char *input_refname, - const char *expected_refname) -{ - char buffer_out[GIT_REFNAME_MAX]; - - cl_git_pass(git_reference_normalize_name(buffer_out, sizeof(buffer_out), input_refname, flags)); - - cl_assert_equal_s(expected_refname, buffer_out); -} - -static void ensure_refname_invalid(unsigned int flags, const char *input_refname) -{ - char buffer_out[GIT_REFNAME_MAX]; - - cl_assert_equal_i( - GIT_EINVALIDSPEC, - git_reference_normalize_name(buffer_out, sizeof(buffer_out), input_refname, flags)); -} - -void test_refs_normalize__can_normalize_a_direct_reference_name(void) -{ - ensure_refname_normalized( - GIT_REF_FORMAT_NORMAL, "refs/dummy/a", "refs/dummy/a"); - ensure_refname_normalized( - GIT_REF_FORMAT_NORMAL, "refs/stash", "refs/stash"); - ensure_refname_normalized( - GIT_REF_FORMAT_NORMAL, "refs/tags/a", "refs/tags/a"); - ensure_refname_normalized( - GIT_REF_FORMAT_NORMAL, "refs/heads/a/b", "refs/heads/a/b"); - ensure_refname_normalized( - GIT_REF_FORMAT_NORMAL, "refs/heads/a./b", "refs/heads/a./b"); - ensure_refname_normalized( - GIT_REF_FORMAT_NORMAL, "refs/heads/v@ation", "refs/heads/v@ation"); - ensure_refname_normalized( - GIT_REF_FORMAT_NORMAL, "refs///heads///a", "refs/heads/a"); -} - -void test_refs_normalize__cannot_normalize_any_direct_reference_name(void) -{ - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "a"); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "/a"); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "//a"); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, ""); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "/refs/heads/a/"); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "refs/heads/a/"); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "refs/heads/a."); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "refs/heads/a.lock"); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "refs/heads/foo?bar"); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "refs/heads\foo"); - ensure_refname_normalized( - GIT_REF_FORMAT_NORMAL, "refs/heads/v@ation", "refs/heads/v@ation"); - ensure_refname_normalized( - GIT_REF_FORMAT_NORMAL, "refs///heads///a", "refs/heads/a"); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "refs/heads/.a/b"); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "refs/heads/foo/../bar"); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "refs/heads/foo..bar"); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "refs/heads/./foo"); - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "refs/heads/v@{ation"); -} - -void test_refs_normalize__symbolic(void) -{ - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, ""); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "heads\foo"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "///"); - - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "ALL_CAPS_AND_UNDERSCORES", "ALL_CAPS_AND_UNDERSCORES"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/MixedCasing", "refs/MixedCasing"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs///heads///a", "refs/heads/a"); - - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "HEAD", "HEAD"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "MERGE_HEAD", "MERGE_HEAD"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "FETCH_HEAD", "FETCH_HEAD"); -} - -/* Ported from JGit, BSD licence. - * See https://github.com/spearce/JGit/commit/e4bf8f6957bbb29362575d641d1e77a02d906739 - * - * Copyright (C) 2009, Google Inc. - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Git Development Community nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -void test_refs_normalize__jgit_suite(void) -{ - // tests borrowed from JGit - -/* EmptyString */ - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, ""); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "/"); - -/* MustHaveTwoComponents */ - ensure_refname_invalid( - GIT_REF_FORMAT_NORMAL, "master"); - ensure_refname_normalized( - GIT_REF_FORMAT_NORMAL, "heads/master", "heads/master"); - -/* ValidHead */ - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master", "refs/heads/master"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/pu", "refs/heads/pu"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/z", "refs/heads/z"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/FoO", "refs/heads/FoO"); - -/* ValidTag */ - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/tags/v1.0", "refs/tags/v1.0"); - -/* NoLockSuffix */ - ensure_refname_invalid(GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master.lock"); - -/* NoDirectorySuffix */ - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master/"); - -/* NoSpace */ - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/i haz space"); - -/* NoAsciiControlCharacters */ - { - char c; - char buffer[GIT_REFNAME_MAX]; - for (c = '\1'; c < ' '; c++) { - strncpy(buffer, "refs/heads/mast", 15); - strncpy(buffer + 15, (const char *)&c, 1); - strncpy(buffer + 16, "er", 2); - buffer[18 - 1] = '\0'; - ensure_refname_invalid(GIT_REF_FORMAT_ALLOW_ONELEVEL, buffer); - } - } - -/* NoBareDot */ - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/."); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/.."); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/./master"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/../master"); - -/* NoLeadingOrTrailingDot */ - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "."); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/.bar"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/..bar"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/bar."); - -/* ContainsDot */ - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/m.a.s.t.e.r", "refs/heads/m.a.s.t.e.r"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master..pu"); - -/* NoMagicRefCharacters */ - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master^"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/^master"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "^refs/heads/master"); - - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master~"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/~master"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "~refs/heads/master"); - - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master:"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/:master"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, ":refs/heads/master"); - -/* ShellGlob */ - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master?"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/?master"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "?refs/heads/master"); - - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master["); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/[master"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "[refs/heads/master"); - - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master*"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/*master"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "*refs/heads/master"); - -/* ValidSpecialCharacters */ - ensure_refname_normalized - (GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/!", "refs/heads/!"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/\"", "refs/heads/\""); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/#", "refs/heads/#"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/$", "refs/heads/$"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/%", "refs/heads/%"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/&", "refs/heads/&"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/'", "refs/heads/'"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/(", "refs/heads/("); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/)", "refs/heads/)"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/+", "refs/heads/+"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/,", "refs/heads/,"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/-", "refs/heads/-"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/;", "refs/heads/;"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/<", "refs/heads/<"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/=", "refs/heads/="); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/>", "refs/heads/>"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/@", "refs/heads/@"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/]", "refs/heads/]"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/_", "refs/heads/_"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/`", "refs/heads/`"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/{", "refs/heads/{"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/|", "refs/heads/|"); - ensure_refname_normalized( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/}", "refs/heads/}"); - - // This is valid on UNIX, but not on Windows - // hence we make in invalid due to non-portability - // - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/\\"); - -/* UnicodeNames */ - /* - * Currently this fails. - * ensure_refname_normalized(GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/\u00e5ngstr\u00f6m", "refs/heads/\u00e5ngstr\u00f6m"); - */ - -/* RefLogQueryIsValidRef */ - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master@{1}"); - ensure_refname_invalid( - GIT_REF_FORMAT_ALLOW_ONELEVEL, "refs/heads/master@{1.hour.ago}"); -} - -void test_refs_normalize__buffer_has_to_be_big_enough_to_hold_the_normalized_version(void) -{ - char buffer_out[21]; - - cl_git_pass(git_reference_normalize_name( - buffer_out, 21, "refs//heads///long///name", GIT_REF_FORMAT_NORMAL)); - cl_git_fail(git_reference_normalize_name( - buffer_out, 20, "refs//heads///long///name", GIT_REF_FORMAT_NORMAL)); -} - -#define ONE_LEVEL_AND_REFSPEC \ - GIT_REF_FORMAT_ALLOW_ONELEVEL \ - | GIT_REF_FORMAT_REFSPEC_PATTERN - -void test_refs_normalize__refspec_pattern(void) -{ - ensure_refname_invalid( - GIT_REF_FORMAT_REFSPEC_PATTERN, "heads/*foo/bar"); - ensure_refname_invalid( - GIT_REF_FORMAT_REFSPEC_PATTERN, "heads/foo*/bar"); - ensure_refname_invalid( - GIT_REF_FORMAT_REFSPEC_PATTERN, "heads/f*o/bar"); - - ensure_refname_invalid( - GIT_REF_FORMAT_REFSPEC_PATTERN, "foo"); - ensure_refname_normalized( - ONE_LEVEL_AND_REFSPEC, "FOO", "FOO"); - - ensure_refname_normalized( - GIT_REF_FORMAT_REFSPEC_PATTERN, "foo/bar", "foo/bar"); - ensure_refname_normalized( - ONE_LEVEL_AND_REFSPEC, "foo/bar", "foo/bar"); - - ensure_refname_normalized( - GIT_REF_FORMAT_REFSPEC_PATTERN, "*/foo", "*/foo"); - ensure_refname_normalized( - ONE_LEVEL_AND_REFSPEC, "*/foo", "*/foo"); - - ensure_refname_normalized( - GIT_REF_FORMAT_REFSPEC_PATTERN, "foo/*/bar", "foo/*/bar"); - ensure_refname_normalized( - ONE_LEVEL_AND_REFSPEC, "foo/*/bar", "foo/*/bar"); - - ensure_refname_invalid( - GIT_REF_FORMAT_REFSPEC_PATTERN, "*"); - ensure_refname_normalized( - ONE_LEVEL_AND_REFSPEC, "*", "*"); - - ensure_refname_invalid( - GIT_REF_FORMAT_REFSPEC_PATTERN, "foo/*/*"); - ensure_refname_invalid( - ONE_LEVEL_AND_REFSPEC, "foo/*/*"); - - ensure_refname_invalid( - GIT_REF_FORMAT_REFSPEC_PATTERN, "*/foo/*"); - ensure_refname_invalid( - ONE_LEVEL_AND_REFSPEC, "*/foo/*"); - - ensure_refname_invalid( - GIT_REF_FORMAT_REFSPEC_PATTERN, "*/*/foo"); - ensure_refname_invalid( - ONE_LEVEL_AND_REFSPEC, "*/*/foo"); -} diff --git a/tests-clar/refs/pack.c b/tests-clar/refs/pack.c deleted file mode 100644 index 305594c2865..00000000000 --- a/tests-clar/refs/pack.c +++ /dev/null @@ -1,67 +0,0 @@ -#include "clar_libgit2.h" - -#include "repository.h" -#include "git2/reflog.h" -#include "reflog.h" - -static const char *loose_tag_ref_name = "refs/tags/e90810b"; - -static git_repository *g_repo; - -void test_refs_pack__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_refs_pack__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_refs_pack__empty(void) -{ - // create a packfile for an empty folder - git_buf temp_path = GIT_BUF_INIT; - - cl_git_pass(git_buf_join_n(&temp_path, '/', 3, g_repo->path_repository, GIT_REFS_HEADS_DIR, "empty_dir")); - cl_git_pass(git_futils_mkdir_r(temp_path.ptr, NULL, GIT_REFS_DIR_MODE)); - git_buf_free(&temp_path); - - cl_git_pass(git_reference_packall(g_repo)); -} - -void test_refs_pack__loose(void) -{ - // create a packfile from all the loose rn a repo - git_reference *reference; - git_buf temp_path = GIT_BUF_INIT; - - /* Ensure a known loose ref can be looked up */ - cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name)); - cl_assert(git_reference_is_packed(reference) == 0); - cl_assert_equal_s(reference->name, loose_tag_ref_name); - git_reference_free(reference); - - /* - * We are now trying to pack also a loose reference - * called `points_to_blob`, to make sure we can properly - * pack weak tags - */ - cl_git_pass(git_reference_packall(g_repo)); - - /* Ensure the packed-refs file exists */ - cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, GIT_PACKEDREFS_FILE)); - cl_assert(git_path_exists(temp_path.ptr)); - - /* Ensure the known ref can still be looked up but is now packed */ - cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name)); - cl_assert(git_reference_is_packed(reference)); - cl_assert_equal_s(reference->name, loose_tag_ref_name); - - /* Ensure the known ref has been removed from the loose folder structure */ - cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, loose_tag_ref_name)); - cl_assert(!git_path_exists(temp_path.ptr)); - - git_reference_free(reference); - git_buf_free(&temp_path); -} diff --git a/tests-clar/refs/peel.c b/tests-clar/refs/peel.c deleted file mode 100644 index 34bd02ce0c4..00000000000 --- a/tests-clar/refs/peel.c +++ /dev/null @@ -1,92 +0,0 @@ -#include "clar_libgit2.h" - -static git_repository *g_repo; - -void test_refs_peel__initialize(void) -{ - cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git"))); -} - -void test_refs_peel__cleanup(void) -{ - git_repository_free(g_repo); - g_repo = NULL; -} - -static void assert_peel( - const char *ref_name, - git_otype requested_type, - const char* expected_sha, - git_otype expected_type) -{ - git_oid expected_oid; - git_reference *ref; - git_object *peeled; - - cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name)); - - cl_git_pass(git_reference_peel(&peeled, ref, requested_type)); - - cl_git_pass(git_oid_fromstr(&expected_oid, expected_sha)); - cl_assert_equal_i(0, git_oid_cmp(&expected_oid, git_object_id(peeled))); - - cl_assert_equal_i(expected_type, git_object_type(peeled)); - - git_object_free(peeled); - git_reference_free(ref); -} - -static void assert_peel_error(int error, const char *ref_name, git_otype requested_type) -{ - git_reference *ref; - git_object *peeled; - - cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name)); - - cl_assert_equal_i(error, git_reference_peel(&peeled, ref, requested_type)); - - git_reference_free(ref); -} - -void test_refs_peel__can_peel_a_tag(void) -{ - assert_peel("refs/tags/test", GIT_OBJ_TAG, - "b25fa35b38051e4ae45d4222e795f9df2e43f1d1", GIT_OBJ_TAG); - assert_peel("refs/tags/test", GIT_OBJ_COMMIT, - "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT); - assert_peel("refs/tags/test", GIT_OBJ_TREE, - "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE); - assert_peel("refs/tags/point_to_blob", GIT_OBJ_BLOB, - "1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OBJ_BLOB); -} - -void test_refs_peel__can_peel_a_branch(void) -{ - assert_peel("refs/heads/master", GIT_OBJ_COMMIT, - "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJ_COMMIT); - assert_peel("refs/heads/master", GIT_OBJ_TREE, - "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162", GIT_OBJ_TREE); -} - -void test_refs_peel__can_peel_a_symbolic_reference(void) -{ - assert_peel("HEAD", GIT_OBJ_COMMIT, - "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJ_COMMIT); - assert_peel("HEAD", GIT_OBJ_TREE, - "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162", GIT_OBJ_TREE); -} - -void test_refs_peel__cannot_peel_into_a_non_existing_target(void) -{ - assert_peel_error(GIT_ENOTFOUND, "refs/tags/point_to_blob", GIT_OBJ_TAG); -} - -void test_refs_peel__can_peel_into_any_non_tag_object(void) -{ - assert_peel("refs/heads/master", GIT_OBJ_ANY, - "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJ_COMMIT); - assert_peel("refs/tags/point_to_blob", GIT_OBJ_ANY, - "1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OBJ_BLOB); - assert_peel("refs/tags/test", GIT_OBJ_ANY, - "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT); -} diff --git a/tests-clar/refs/read.c b/tests-clar/refs/read.c deleted file mode 100644 index 3e2a59afd75..00000000000 --- a/tests-clar/refs/read.c +++ /dev/null @@ -1,267 +0,0 @@ -#include "clar_libgit2.h" - -#include "repository.h" -#include "git2/reflog.h" -#include "reflog.h" - -static const char *loose_tag_ref_name = "refs/tags/e90810b"; -static const char *non_existing_tag_ref_name = "refs/tags/i-do-not-exist"; -static const char *head_tracker_sym_ref_name = "HEAD_TRACKER"; -static const char *current_head_target = "refs/heads/master"; -static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"; -static const char *packed_head_name = "refs/heads/packed"; -static const char *packed_test_head_name = "refs/heads/packed-test"; - -static git_repository *g_repo; - -void test_refs_read__initialize(void) -{ - cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git"))); -} - -void test_refs_read__cleanup(void) -{ - git_repository_free(g_repo); - g_repo = NULL; -} - -void test_refs_read__loose_tag(void) -{ - // lookup a loose tag reference - git_reference *reference; - git_object *object; - git_buf ref_name_from_tag_name = GIT_BUF_INIT; - - cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name)); - cl_assert(git_reference_type(reference) & GIT_REF_OID); - cl_assert(git_reference_is_packed(reference) == 0); - cl_assert_equal_s(reference->name, loose_tag_ref_name); - - cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(reference), GIT_OBJ_ANY)); - cl_assert(object != NULL); - cl_assert(git_object_type(object) == GIT_OBJ_TAG); - - /* Ensure the name of the tag matches the name of the reference */ - cl_git_pass(git_buf_joinpath(&ref_name_from_tag_name, GIT_REFS_TAGS_DIR, git_tag_name((git_tag *)object))); - cl_assert_equal_s(ref_name_from_tag_name.ptr, loose_tag_ref_name); - git_buf_free(&ref_name_from_tag_name); - - git_object_free(object); - - git_reference_free(reference); -} - -void test_refs_read__nonexisting_tag(void) -{ - // lookup a loose tag reference that doesn't exist - git_reference *reference; - - cl_git_fail(git_reference_lookup(&reference, g_repo, non_existing_tag_ref_name)); - - git_reference_free(reference); -} - - -void test_refs_read__symbolic(void) -{ - // lookup a symbolic reference - git_reference *reference, *resolved_ref; - git_object *object; - git_oid id; - - cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE)); - cl_assert(git_reference_type(reference) & GIT_REF_SYMBOLIC); - cl_assert(git_reference_is_packed(reference) == 0); - cl_assert_equal_s(reference->name, GIT_HEAD_FILE); - - cl_git_pass(git_reference_resolve(&resolved_ref, reference)); - cl_assert(git_reference_type(resolved_ref) == GIT_REF_OID); - - cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(resolved_ref), GIT_OBJ_ANY)); - cl_assert(object != NULL); - cl_assert(git_object_type(object) == GIT_OBJ_COMMIT); - - git_oid_fromstr(&id, current_master_tip); - cl_assert(git_oid_cmp(&id, git_object_id(object)) == 0); - - git_object_free(object); - - git_reference_free(reference); - git_reference_free(resolved_ref); -} - -void test_refs_read__nested_symbolic(void) -{ - // lookup a nested symbolic reference - git_reference *reference, *resolved_ref; - git_object *object; - git_oid id; - - cl_git_pass(git_reference_lookup(&reference, g_repo, head_tracker_sym_ref_name)); - cl_assert(git_reference_type(reference) & GIT_REF_SYMBOLIC); - cl_assert(git_reference_is_packed(reference) == 0); - cl_assert_equal_s(reference->name, head_tracker_sym_ref_name); - - cl_git_pass(git_reference_resolve(&resolved_ref, reference)); - cl_assert(git_reference_type(resolved_ref) == GIT_REF_OID); - - cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(resolved_ref), GIT_OBJ_ANY)); - cl_assert(object != NULL); - cl_assert(git_object_type(object) == GIT_OBJ_COMMIT); - - git_oid_fromstr(&id, current_master_tip); - cl_assert(git_oid_cmp(&id, git_object_id(object)) == 0); - - git_object_free(object); - - git_reference_free(reference); - git_reference_free(resolved_ref); -} - -void test_refs_read__head_then_master(void) -{ - // lookup the HEAD and resolve the master branch - git_reference *reference, *resolved_ref, *comp_base_ref; - - cl_git_pass(git_reference_lookup(&reference, g_repo, head_tracker_sym_ref_name)); - cl_git_pass(git_reference_resolve(&comp_base_ref, reference)); - git_reference_free(reference); - - cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE)); - cl_git_pass(git_reference_resolve(&resolved_ref, reference)); - cl_git_pass(git_oid_cmp(git_reference_target(comp_base_ref), git_reference_target(resolved_ref))); - git_reference_free(reference); - git_reference_free(resolved_ref); - - cl_git_pass(git_reference_lookup(&reference, g_repo, current_head_target)); - cl_git_pass(git_reference_resolve(&resolved_ref, reference)); - cl_git_pass(git_oid_cmp(git_reference_target(comp_base_ref), git_reference_target(resolved_ref))); - git_reference_free(reference); - git_reference_free(resolved_ref); - - git_reference_free(comp_base_ref); -} - -void test_refs_read__master_then_head(void) -{ - // lookup the master branch and then the HEAD - git_reference *reference, *master_ref, *resolved_ref; - - cl_git_pass(git_reference_lookup(&master_ref, g_repo, current_head_target)); - cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE)); - - cl_git_pass(git_reference_resolve(&resolved_ref, reference)); - cl_git_pass(git_oid_cmp(git_reference_target(master_ref), git_reference_target(resolved_ref))); - - git_reference_free(reference); - git_reference_free(resolved_ref); - git_reference_free(master_ref); -} - - -void test_refs_read__packed(void) -{ - // lookup a packed reference - git_reference *reference; - git_object *object; - - cl_git_pass(git_reference_lookup(&reference, g_repo, packed_head_name)); - cl_assert(git_reference_type(reference) & GIT_REF_OID); - cl_assert(git_reference_is_packed(reference)); - cl_assert_equal_s(reference->name, packed_head_name); - - cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(reference), GIT_OBJ_ANY)); - cl_assert(object != NULL); - cl_assert(git_object_type(object) == GIT_OBJ_COMMIT); - - git_object_free(object); - - git_reference_free(reference); -} - -void test_refs_read__loose_first(void) -{ - // assure that a loose reference is looked up before a packed reference - git_reference *reference; - - cl_git_pass(git_reference_lookup(&reference, g_repo, packed_head_name)); - git_reference_free(reference); - cl_git_pass(git_reference_lookup(&reference, g_repo, packed_test_head_name)); - cl_assert(git_reference_type(reference) & GIT_REF_OID); - cl_assert(git_reference_is_packed(reference) == 0); - cl_assert_equal_s(reference->name, packed_test_head_name); - - git_reference_free(reference); -} - -void test_refs_read__chomped(void) -{ - git_reference *test, *chomped; - - cl_git_pass(git_reference_lookup(&test, g_repo, "refs/heads/test")); - cl_git_pass(git_reference_lookup(&chomped, g_repo, "refs/heads/chomped")); - cl_git_pass(git_oid_cmp(git_reference_target(test), git_reference_target(chomped))); - - git_reference_free(test); - git_reference_free(chomped); -} - -void test_refs_read__trailing(void) -{ - git_reference *test, *trailing; - - cl_git_pass(git_reference_lookup(&test, g_repo, "refs/heads/test")); - cl_git_pass(git_reference_lookup(&trailing, g_repo, "refs/heads/trailing")); - cl_git_pass(git_oid_cmp(git_reference_target(test), git_reference_target(trailing))); - git_reference_free(trailing); - cl_git_pass(git_reference_lookup(&trailing, g_repo, "FETCH_HEAD")); - - git_reference_free(test); - git_reference_free(trailing); -} - -void test_refs_read__unfound_return_ENOTFOUND(void) -{ - git_reference *reference; - git_oid id; - - cl_assert_equal_i(GIT_ENOTFOUND, - git_reference_lookup(&reference, g_repo, "TEST_MASTER")); - cl_assert_equal_i(GIT_ENOTFOUND, - git_reference_lookup(&reference, g_repo, "refs/test/master")); - cl_assert_equal_i(GIT_ENOTFOUND, - git_reference_lookup(&reference, g_repo, "refs/tags/test/master")); - cl_assert_equal_i(GIT_ENOTFOUND, - git_reference_lookup(&reference, g_repo, "refs/tags/test/farther/master")); - - cl_assert_equal_i(GIT_ENOTFOUND, - git_reference_name_to_id(&id, g_repo, "refs/tags/test/farther/master")); -} - -static void assert_is_branch(const char *name, bool expected_branchness) -{ - git_reference *reference; - cl_git_pass(git_reference_lookup(&reference, g_repo, name)); - cl_assert_equal_i(expected_branchness, git_reference_is_branch(reference)); - git_reference_free(reference); -} - -void test_refs_read__can_determine_if_a_reference_is_a_local_branch(void) -{ - assert_is_branch("refs/heads/master", true); - assert_is_branch("refs/heads/packed", true); - assert_is_branch("refs/remotes/test/master", false); - assert_is_branch("refs/tags/e90810b", false); -} - -void test_refs_read__invalid_name_returns_EINVALIDSPEC(void) -{ - git_reference *reference; - git_oid id; - - cl_assert_equal_i(GIT_EINVALIDSPEC, - git_reference_lookup(&reference, g_repo, "refs/heads/Inv@{id")); - - cl_assert_equal_i(GIT_EINVALIDSPEC, - git_reference_name_to_id(&id, g_repo, "refs/heads/Inv@{id")); -} diff --git a/tests-clar/refs/reflog/drop.c b/tests-clar/refs/reflog/drop.c deleted file mode 100644 index 21cc847bf71..00000000000 --- a/tests-clar/refs/reflog/drop.c +++ /dev/null @@ -1,124 +0,0 @@ -#include "clar_libgit2.h" - -#include "reflog.h" - -static git_repository *g_repo; -static git_reflog *g_reflog; -static size_t entrycount; - -void test_refs_reflog_drop__initialize(void) -{ - git_reference *ref; - - g_repo = cl_git_sandbox_init("testrepo.git"); - cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD")); - - git_reflog_read(&g_reflog, ref); - entrycount = git_reflog_entrycount(g_reflog); - - git_reference_free(ref); -} - -void test_refs_reflog_drop__cleanup(void) -{ - git_reflog_free(g_reflog); - g_reflog = NULL; - - cl_git_sandbox_cleanup(); -} - -void test_refs_reflog_drop__dropping_a_non_exisiting_entry_from_the_log_returns_ENOTFOUND(void) -{ - cl_assert_equal_i(GIT_ENOTFOUND, git_reflog_drop(g_reflog, entrycount, 0)); - - cl_assert_equal_sz(entrycount, git_reflog_entrycount(g_reflog)); -} - -void test_refs_reflog_drop__can_drop_an_entry(void) -{ - cl_assert(entrycount > 4); - - cl_git_pass(git_reflog_drop(g_reflog, 2, 0)); - cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); -} - -void test_refs_reflog_drop__can_drop_an_entry_and_rewrite_the_log_history(void) -{ - const git_reflog_entry *before_current; - const git_reflog_entry *after_current; - git_oid before_current_old_oid, before_current_cur_oid; - - cl_assert(entrycount > 4); - - before_current = git_reflog_entry_byindex(g_reflog, 1); - - git_oid_cpy(&before_current_old_oid, &before_current->oid_old); - git_oid_cpy(&before_current_cur_oid, &before_current->oid_cur); - - cl_git_pass(git_reflog_drop(g_reflog, 1, 1)); - - cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); - - after_current = git_reflog_entry_byindex(g_reflog, 0); - - cl_assert_equal_i(0, git_oid_cmp(&before_current_old_oid, &after_current->oid_old)); - cl_assert(0 != git_oid_cmp(&before_current_cur_oid, &after_current->oid_cur)); -} - -void test_refs_reflog_drop__can_drop_the_oldest_entry(void) -{ - const git_reflog_entry *entry; - - cl_assert(entrycount > 2); - - cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 0)); - cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); - - entry = git_reflog_entry_byindex(g_reflog, entrycount - 2); - cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) != 0); -} - -void test_refs_reflog_drop__can_drop_the_oldest_entry_and_rewrite_the_log_history(void) -{ - const git_reflog_entry *entry; - - cl_assert(entrycount > 2); - - cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 1)); - cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); - - entry = git_reflog_entry_byindex(g_reflog, entrycount - 2); - cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) == 0); -} - -void test_refs_reflog_drop__can_drop_all_the_entries(void) -{ - cl_assert(--entrycount > 0); - - do { - cl_git_pass(git_reflog_drop(g_reflog, 0, 1)); - } while (--entrycount > 0); - - cl_git_pass(git_reflog_drop(g_reflog, 0, 1)); - - cl_assert_equal_i(0, (int)git_reflog_entrycount(g_reflog)); -} - -void test_refs_reflog_drop__can_persist_deletion_on_disk(void) -{ - git_reference *ref; - - cl_assert(entrycount > 2); - - cl_git_pass(git_reference_lookup(&ref, g_repo, g_reflog->ref_name)); - cl_git_pass(git_reflog_drop(g_reflog, 0, 1)); - cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); - cl_git_pass(git_reflog_write(g_reflog)); - - git_reflog_free(g_reflog); - - git_reflog_read(&g_reflog, ref); - git_reference_free(ref); - - cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); -} diff --git a/tests-clar/refs/reflog/reflog.c b/tests-clar/refs/reflog/reflog.c deleted file mode 100644 index 19ee53567bb..00000000000 --- a/tests-clar/refs/reflog/reflog.c +++ /dev/null @@ -1,184 +0,0 @@ -#include "clar_libgit2.h" - -#include "repository.h" -#include "git2/reflog.h" -#include "reflog.h" - - -static const char *new_ref = "refs/heads/test-reflog"; -static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"; -#define commit_msg "commit: bla bla" - -static git_repository *g_repo; - - -// helpers -static void assert_signature(git_signature *expected, git_signature *actual) -{ - cl_assert(actual); - cl_assert_equal_s(expected->name, actual->name); - cl_assert_equal_s(expected->email, actual->email); - cl_assert(expected->when.offset == actual->when.offset); - cl_assert(expected->when.time == actual->when.time); -} - - -// Fixture setup and teardown -void test_refs_reflog_reflog__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo.git"); -} - -void test_refs_reflog_reflog__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_refs_reflog_reflog__append_then_read(void) -{ - // write a reflog for a given reference and ensure it can be read back - git_repository *repo2; - git_reference *ref, *lookedup_ref; - git_oid oid; - git_signature *committer; - git_reflog *reflog; - const git_reflog_entry *entry; - - /* Create a new branch pointing at the HEAD */ - git_oid_fromstr(&oid, current_master_tip); - cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &oid, 0)); - - cl_git_pass(git_signature_now(&committer, "foo", "foo@bar")); - - cl_git_pass(git_reflog_read(&reflog, ref)); - - cl_git_fail(git_reflog_append(reflog, &oid, committer, "no inner\nnewline")); - cl_git_pass(git_reflog_append(reflog, &oid, committer, NULL)); - cl_git_pass(git_reflog_append(reflog, &oid, committer, commit_msg "\n")); - cl_git_pass(git_reflog_write(reflog)); - git_reflog_free(reflog); - - /* Reopen a new instance of the repository */ - cl_git_pass(git_repository_open(&repo2, "testrepo.git")); - - /* Lookup the previously created branch */ - cl_git_pass(git_reference_lookup(&lookedup_ref, repo2, new_ref)); - - /* Read and parse the reflog for this branch */ - cl_git_pass(git_reflog_read(&reflog, lookedup_ref)); - cl_assert_equal_i(2, (int)git_reflog_entrycount(reflog)); - - entry = git_reflog_entry_byindex(reflog, 1); - assert_signature(committer, entry->committer); - cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) == 0); - cl_assert(git_oid_cmp(&oid, &entry->oid_cur) == 0); - cl_assert(entry->msg == NULL); - - entry = git_reflog_entry_byindex(reflog, 0); - assert_signature(committer, entry->committer); - cl_assert(git_oid_cmp(&oid, &entry->oid_old) == 0); - cl_assert(git_oid_cmp(&oid, &entry->oid_cur) == 0); - cl_assert_equal_s(commit_msg, entry->msg); - - git_signature_free(committer); - git_reflog_free(reflog); - git_repository_free(repo2); - - git_reference_free(ref); - git_reference_free(lookedup_ref); -} - -void test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog(void) -{ - git_reference *master; - git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT; - - git_buf_joinpath(&master_log_path, git_repository_path(g_repo), GIT_REFLOG_DIR); - git_buf_puts(&moved_log_path, git_buf_cstr(&master_log_path)); - git_buf_joinpath(&master_log_path, git_buf_cstr(&master_log_path), "refs/heads/master"); - git_buf_joinpath(&moved_log_path, git_buf_cstr(&moved_log_path), "refs/moved"); - - cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&master_log_path))); - cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&moved_log_path))); - - cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master")); - cl_git_pass(git_reference_rename(master, "refs/moved", 0)); - - cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&master_log_path))); - cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&moved_log_path))); - - git_reference_free(master); - git_buf_free(&moved_log_path); - git_buf_free(&master_log_path); -} - -static void assert_has_reflog(bool expected_result, const char *name) -{ - git_reference *ref; - - cl_git_pass(git_reference_lookup(&ref, g_repo, name)); - - cl_assert_equal_i(expected_result, git_reference_has_log(ref)); - - git_reference_free(ref); -} - -void test_refs_reflog_reflog__reference_has_reflog(void) -{ - assert_has_reflog(true, "HEAD"); - assert_has_reflog(true, "refs/heads/master"); - assert_has_reflog(false, "refs/heads/subtrees"); -} - -void test_refs_reflog_reflog__reading_the_reflog_from_a_reference_with_no_log_returns_an_empty_one(void) -{ - git_reference *subtrees; - git_reflog *reflog; - git_buf subtrees_log_path = GIT_BUF_INIT; - - cl_git_pass(git_reference_lookup(&subtrees, g_repo, "refs/heads/subtrees")); - - git_buf_join_n(&subtrees_log_path, '/', 3, git_repository_path(g_repo), GIT_REFLOG_DIR, git_reference_name(subtrees)); - cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&subtrees_log_path))); - - cl_git_pass(git_reflog_read(&reflog, subtrees)); - - cl_assert_equal_i(0, (int)git_reflog_entrycount(reflog)); - - git_reflog_free(reflog); - git_reference_free(subtrees); - git_buf_free(&subtrees_log_path); -} - -void test_refs_reflog_reflog__cannot_write_a_moved_reflog(void) -{ - git_reference *master; - git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT; - git_reflog *reflog; - - cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master")); - cl_git_pass(git_reflog_read(&reflog, master)); - - cl_git_pass(git_reflog_write(reflog)); - - cl_git_pass(git_reference_rename(master, "refs/moved", 0)); - - cl_git_fail(git_reflog_write(reflog)); - - git_reflog_free(reflog); - git_reference_free(master); - git_buf_free(&moved_log_path); - git_buf_free(&master_log_path); -} - -void test_refs_reflog_reflog__renaming_with_an_invalid_name_returns_EINVALIDSPEC(void) -{ - git_reference *master; - - cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master")); - - cl_assert_equal_i(GIT_EINVALIDSPEC, - git_reflog_rename(master, "refs/heads/Inv@{id")); - - git_reference_free(master); -} diff --git a/tests-clar/refs/rename.c b/tests-clar/refs/rename.c deleted file mode 100644 index bfdef15faf2..00000000000 --- a/tests-clar/refs/rename.c +++ /dev/null @@ -1,354 +0,0 @@ -#include "clar_libgit2.h" - -#include "repository.h" -#include "git2/reflog.h" -#include "reflog.h" - -static const char *loose_tag_ref_name = "refs/tags/e90810b"; -static const char *packed_head_name = "refs/heads/packed"; -static const char *packed_test_head_name = "refs/heads/packed-test"; -static const char *ref_one_name = "refs/heads/one/branch"; -static const char *ref_one_name_new = "refs/heads/two/branch"; -static const char *ref_two_name = "refs/heads/two"; -static const char *ref_master_name = "refs/heads/master"; -static const char *ref_two_name_new = "refs/heads/two/two"; - -static git_repository *g_repo; - - - -void test_refs_rename__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_refs_rename__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - - - -void test_refs_rename__loose(void) -{ - // rename a loose reference - git_reference *looked_up_ref, *another_looked_up_ref; - git_buf temp_path = GIT_BUF_INIT; - const char *new_name = "refs/tags/Nemo/knows/refs.kung-fu"; - - /* Ensure the ref doesn't exist on the file system */ - cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, new_name)); - cl_assert(!git_path_exists(temp_path.ptr)); - - /* Retrieval of the reference to rename */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, loose_tag_ref_name)); - - /* ... which is indeed loose */ - cl_assert(git_reference_is_packed(looked_up_ref) == 0); - - /* Now that the reference is renamed... */ - cl_git_pass(git_reference_rename(looked_up_ref, new_name, 0)); - cl_assert_equal_s(looked_up_ref->name, new_name); - - /* ...It can't be looked-up with the old name... */ - cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, loose_tag_ref_name)); - - /* ...but the new name works ok... */ - cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, new_name)); - cl_assert_equal_s(another_looked_up_ref->name, new_name); - - /* .. the ref is still loose... */ - cl_assert(git_reference_is_packed(another_looked_up_ref) == 0); - cl_assert(git_reference_is_packed(looked_up_ref) == 0); - - /* ...and the ref can be found in the file system */ - cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, new_name)); - cl_assert(git_path_exists(temp_path.ptr)); - - git_reference_free(looked_up_ref); - git_reference_free(another_looked_up_ref); - git_buf_free(&temp_path); -} - -void test_refs_rename__packed(void) -{ - // rename a packed reference (should make it loose) - git_reference *looked_up_ref, *another_looked_up_ref; - git_buf temp_path = GIT_BUF_INIT; - const char *brand_new_name = "refs/heads/brand_new_name"; - - /* Ensure the ref doesn't exist on the file system */ - cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, packed_head_name)); - cl_assert(!git_path_exists(temp_path.ptr)); - - /* The reference can however be looked-up... */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name)); - - /* .. and it's packed */ - cl_assert(git_reference_is_packed(looked_up_ref) != 0); - - /* Now that the reference is renamed... */ - cl_git_pass(git_reference_rename(looked_up_ref, brand_new_name, 0)); - cl_assert_equal_s(looked_up_ref->name, brand_new_name); - - /* ...It can't be looked-up with the old name... */ - cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, packed_head_name)); - - /* ...but the new name works ok... */ - cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, brand_new_name)); - cl_assert_equal_s(another_looked_up_ref->name, brand_new_name); - - /* .. the ref is no longer packed... */ - cl_assert(git_reference_is_packed(another_looked_up_ref) == 0); - cl_assert(git_reference_is_packed(looked_up_ref) == 0); - - /* ...and the ref now happily lives in the file system */ - cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, brand_new_name)); - cl_assert(git_path_exists(temp_path.ptr)); - - git_reference_free(looked_up_ref); - git_reference_free(another_looked_up_ref); - git_buf_free(&temp_path); -} - -void test_refs_rename__packed_doesnt_pack_others(void) -{ - // renaming a packed reference does not pack another reference which happens to be in both loose and pack state - git_reference *looked_up_ref, *another_looked_up_ref; - git_buf temp_path = GIT_BUF_INIT; - const char *brand_new_name = "refs/heads/brand_new_name"; - - /* Ensure the other reference exists on the file system */ - cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, packed_test_head_name)); - cl_assert(git_path_exists(temp_path.ptr)); - - /* Lookup the other reference */ - cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name)); - - /* Ensure it's loose */ - cl_assert(git_reference_is_packed(another_looked_up_ref) == 0); - git_reference_free(another_looked_up_ref); - - /* Lookup the reference to rename */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name)); - - /* Ensure it's packed */ - cl_assert(git_reference_is_packed(looked_up_ref) != 0); - - /* Now that the reference is renamed... */ - cl_git_pass(git_reference_rename(looked_up_ref, brand_new_name, 0)); - - /* Lookup the other reference */ - cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name)); - - /* Ensure it's loose */ - cl_assert(git_reference_is_packed(another_looked_up_ref) == 0); - - /* Ensure the other ref still exists on the file system */ - cl_assert(git_path_exists(temp_path.ptr)); - - git_reference_free(looked_up_ref); - git_reference_free(another_looked_up_ref); - git_buf_free(&temp_path); -} - -void test_refs_rename__name_collision(void) -{ - // can not rename a reference with the name of an existing reference - git_reference *looked_up_ref; - - /* An existing reference... */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name)); - - /* Can not be renamed to the name of another existing reference. */ - cl_git_fail(git_reference_rename(looked_up_ref, packed_test_head_name, 0)); - git_reference_free(looked_up_ref); - - /* Failure to rename it hasn't corrupted its state */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name)); - cl_assert_equal_s(looked_up_ref->name, packed_head_name); - - git_reference_free(looked_up_ref); -} - -void test_refs_rename__invalid_name(void) -{ - // can not rename a reference with an invalid name - git_reference *looked_up_ref; - - /* An existing oid reference... */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name)); - - /* Can not be renamed with an invalid name. */ - cl_assert_equal_i( - GIT_EINVALIDSPEC, - git_reference_rename(looked_up_ref, "Hello! I'm a very invalid name.", 0)); - - /* Can not be renamed outside of the refs hierarchy - * unless it's ALL_CAPS_AND_UNDERSCORES. - */ - cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_rename(looked_up_ref, "i-will-sudo-you", 0)); - - /* Failure to rename it hasn't corrupted its state */ - git_reference_free(looked_up_ref); - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name)); - cl_assert_equal_s(looked_up_ref->name, packed_test_head_name); - - git_reference_free(looked_up_ref); -} - -void test_refs_rename__force_loose_packed(void) -{ - // can force-rename a packed reference with the name of an existing loose and packed reference - git_reference *looked_up_ref; - git_oid oid; - - /* An existing reference... */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name)); - git_oid_cpy(&oid, git_reference_target(looked_up_ref)); - - /* Can be force-renamed to the name of another existing reference. */ - cl_git_pass(git_reference_rename(looked_up_ref, packed_test_head_name, 1)); - git_reference_free(looked_up_ref); - - /* Check we actually renamed it */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name)); - cl_assert_equal_s(looked_up_ref->name, packed_test_head_name); - cl_assert(!git_oid_cmp(&oid, git_reference_target(looked_up_ref))); - git_reference_free(looked_up_ref); - - /* And that the previous one doesn't exist any longer */ - cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name)); -} - -void test_refs_rename__force_loose(void) -{ - // can force-rename a loose reference with the name of an existing loose reference - git_reference *looked_up_ref; - git_oid oid; - - /* An existing reference... */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, "refs/heads/br2")); - git_oid_cpy(&oid, git_reference_target(looked_up_ref)); - - /* Can be force-renamed to the name of another existing reference. */ - cl_git_pass(git_reference_rename(looked_up_ref, "refs/heads/test", 1)); - git_reference_free(looked_up_ref); - - /* Check we actually renamed it */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, "refs/heads/test")); - cl_assert_equal_s(looked_up_ref->name, "refs/heads/test"); - cl_assert(!git_oid_cmp(&oid, git_reference_target(looked_up_ref))); - git_reference_free(looked_up_ref); - - /* And that the previous one doesn't exist any longer */ - cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, "refs/heads/br2")); - - git_reference_free(looked_up_ref); -} - - -void test_refs_rename__overwrite(void) -{ - // can not overwrite name of existing reference - git_reference *ref, *ref_one, *ref_one_new, *ref_two; - git_oid id; - - cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name)); - cl_assert(git_reference_type(ref) & GIT_REF_OID); - - git_oid_cpy(&id, git_reference_target(ref)); - - /* Create loose references */ - cl_git_pass(git_reference_create(&ref_one, g_repo, ref_one_name, &id, 0)); - cl_git_pass(git_reference_create(&ref_two, g_repo, ref_two_name, &id, 0)); - - /* Pack everything */ - cl_git_pass(git_reference_packall(g_repo)); - - /* Attempt to create illegal reference */ - cl_git_fail(git_reference_create(&ref_one_new, g_repo, ref_one_name_new, &id, 0)); - - /* Illegal reference couldn't be created so this is supposed to fail */ - cl_git_fail(git_reference_lookup(&ref_one_new, g_repo, ref_one_name_new)); - - git_reference_free(ref); - git_reference_free(ref_one); - git_reference_free(ref_one_new); - git_reference_free(ref_two); -} - - -void test_refs_rename__prefix(void) -{ - // can be renamed to a new name prefixed with the old name - git_reference *ref, *ref_two, *looked_up_ref; - git_oid id; - - cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name)); - cl_assert(git_reference_type(ref) & GIT_REF_OID); - - git_oid_cpy(&id, git_reference_target(ref)); - - /* Create loose references */ - cl_git_pass(git_reference_create(&ref_two, g_repo, ref_two_name, &id, 0)); - - /* An existing reference... */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name)); - - /* Can be rename to a new name starting with the old name. */ - cl_git_pass(git_reference_rename(looked_up_ref, ref_two_name_new, 0)); - git_reference_free(looked_up_ref); - - /* Check we actually renamed it */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name_new)); - cl_assert_equal_s(looked_up_ref->name, ref_two_name_new); - git_reference_free(looked_up_ref); - cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name)); - - git_reference_free(ref); - git_reference_free(ref_two); - git_reference_free(looked_up_ref); -} - -void test_refs_rename__move_up(void) -{ - // can move a reference to a upper reference hierarchy - git_reference *ref, *ref_two, *looked_up_ref; - git_oid id; - - cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name)); - cl_assert(git_reference_type(ref) & GIT_REF_OID); - - git_oid_cpy(&id, git_reference_target(ref)); - - /* Create loose references */ - cl_git_pass(git_reference_create(&ref_two, g_repo, ref_two_name_new, &id, 0)); - git_reference_free(ref_two); - - /* An existing reference... */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name_new)); - - /* Can be renamed upward the reference tree. */ - cl_git_pass(git_reference_rename(looked_up_ref, ref_two_name, 0)); - git_reference_free(looked_up_ref); - - /* Check we actually renamed it */ - cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name)); - cl_assert_equal_s(looked_up_ref->name, ref_two_name); - git_reference_free(looked_up_ref); - cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name_new)); - git_reference_free(ref); - git_reference_free(looked_up_ref); -} - -void test_refs_rename__propagate_eexists(void) -{ - git_reference *ref; - - cl_git_pass(git_reference_lookup(&ref, g_repo, packed_head_name)); - - cl_assert_equal_i(GIT_EEXISTS, git_reference_rename(ref, packed_test_head_name, 0)); - - git_reference_free(ref); -} diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c deleted file mode 100644 index 81a6bc469fe..00000000000 --- a/tests-clar/refs/revparse.c +++ /dev/null @@ -1,488 +0,0 @@ -#include "clar_libgit2.h" - -#include "git2/revparse.h" -#include "buffer.h" -#include "refs.h" -#include "path.h" - -static git_repository *g_repo; -static git_object *g_obj; - -/* Helpers */ -static void test_object_inrepo(const char *spec, const char *expected_oid, git_repository *repo) -{ - char objstr[64] = {0}; - git_object *obj = NULL; - int error; - - error = git_revparse_single(&obj, repo, spec); - - if (expected_oid != NULL) { - cl_assert_equal_i(0, error); - git_oid_fmt(objstr, git_object_id(obj)); - cl_assert_equal_s(objstr, expected_oid); - } else - cl_assert_equal_i(GIT_ENOTFOUND, error); - - git_object_free(obj); -} - -static void test_object(const char *spec, const char *expected_oid) -{ - test_object_inrepo(spec, expected_oid, g_repo); -} - -void test_refs_revparse__initialize(void) -{ - cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git"))); -} - -void test_refs_revparse__cleanup(void) -{ - git_repository_free(g_repo); -} - -void test_refs_revparse__nonexistant_object(void) -{ - test_object("this-does-not-exist", NULL); - test_object("this-does-not-exist^1", NULL); - test_object("this-does-not-exist~2", NULL); -} - -static void assert_invalid_spec(const char *invalid_spec) -{ - cl_assert_equal_i( - GIT_EINVALIDSPEC, git_revparse_single(&g_obj, g_repo, invalid_spec)); -} - -void test_refs_revparse__invalid_reference_name(void) -{ - assert_invalid_spec("this doesn't make sense"); - assert_invalid_spec("Inv@{id"); - assert_invalid_spec(""); -} - -void test_refs_revparse__shas(void) -{ - test_object("c47800c7266a2be04c571c04d5a6614691ea99bd", "c47800c7266a2be04c571c04d5a6614691ea99bd"); - test_object("c47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd"); -} - -void test_refs_revparse__head(void) -{ - test_object("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("HEAD^0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("HEAD~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("master", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); -} - -void test_refs_revparse__full_refs(void) -{ - test_object("refs/heads/master", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("refs/heads/test", "e90810b8df3e80c413d903f631643c716887138d"); - test_object("refs/tags/test", "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); -} - -void test_refs_revparse__partial_refs(void) -{ - test_object("point_to_blob", "1385f264afb75a56a5bec74243be9b367ba4ca08"); - test_object("packed-test", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); - test_object("br2", "a4a7dce85cf63874e984719f4fdd239f5145052f"); -} - -void test_refs_revparse__describe_output(void) -{ - test_object("blah-7-gc47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd"); - test_object("not-good", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); -} - -void test_refs_revparse__nth_parent(void) -{ - assert_invalid_spec("be3563a^-1"); - assert_invalid_spec("^"); - assert_invalid_spec("be3563a^{tree}^"); - assert_invalid_spec("point_to_blob^{blob}^"); - assert_invalid_spec("this doesn't make sense^1"); - - test_object("be3563a^1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); - test_object("be3563a^", "9fd738e8f7967c078dceed8190330fc8648ee56a"); - test_object("be3563a^2", "c47800c7266a2be04c571c04d5a6614691ea99bd"); - test_object("be3563a^1^1", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); - test_object("be3563a^^", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); - test_object("be3563a^2^1", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); - test_object("be3563a^0", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("be3563a^{commit}^", "9fd738e8f7967c078dceed8190330fc8648ee56a"); - - test_object("be3563a^42", NULL); -} - -void test_refs_revparse__not_tag(void) -{ - test_object("point_to_blob^{}", "1385f264afb75a56a5bec74243be9b367ba4ca08"); - test_object("wrapped_tag^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("master^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("master^{tree}^{}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); - test_object("e90810b^{}", "e90810b8df3e80c413d903f631643c716887138d"); - test_object("tags/e90810b^{}", "e90810b8df3e80c413d903f631643c716887138d"); - test_object("e908^{}", "e90810b8df3e80c413d903f631643c716887138d"); -} - -void test_refs_revparse__to_type(void) -{ - assert_invalid_spec("wrapped_tag^{trip}"); - test_object("point_to_blob^{commit}", NULL); - cl_assert_equal_i( - GIT_EAMBIGUOUS, git_revparse_single(&g_obj, g_repo, "wrapped_tag^{blob}")); - - test_object("wrapped_tag^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("wrapped_tag^{tree}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); - test_object("point_to_blob^{blob}", "1385f264afb75a56a5bec74243be9b367ba4ca08"); - test_object("master^{commit}^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); -} - -void test_refs_revparse__linear_history(void) -{ - assert_invalid_spec("~"); - test_object("foo~bar", NULL); - - assert_invalid_spec("master~bar"); - assert_invalid_spec("master~-1"); - assert_invalid_spec("master~0bar"); - assert_invalid_spec("this doesn't make sense~2"); - assert_invalid_spec("be3563a^{tree}~"); - assert_invalid_spec("point_to_blob^{blob}~"); - - test_object("master~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("master~1", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("master~2", "9fd738e8f7967c078dceed8190330fc8648ee56a"); - test_object("master~1~1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); - test_object("master~~", "9fd738e8f7967c078dceed8190330fc8648ee56a"); -} - -void test_refs_revparse__chaining(void) -{ - assert_invalid_spec("master@{0}@{0}"); - assert_invalid_spec("@{u}@{-1}"); - assert_invalid_spec("@{-1}@{-1}"); - assert_invalid_spec("@{-3}@{0}"); - - test_object("master@{0}~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); - test_object("@{u}@{0}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("@{-1}@{0}", "a4a7dce85cf63874e984719f4fdd239f5145052f"); - test_object("@{-4}@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("master~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); - test_object("master~1^2", "c47800c7266a2be04c571c04d5a6614691ea99bd"); - test_object("master^1^2~1", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); - test_object("master^^2^", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); - test_object("master^1^1^1^1^1", "8496071c1b46c854b31185ea97743be6a8774479"); - test_object("master^^1^2^1", NULL); -} - -void test_refs_revparse__upstream(void) -{ - assert_invalid_spec("e90810b@{u}"); - assert_invalid_spec("refs/tags/e90810b@{u}"); - test_object("refs/heads/e90810b@{u}", NULL); - - test_object("master@{upstream}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("heads/master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("refs/heads/master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); -} - -void test_refs_revparse__ordinal(void) -{ - assert_invalid_spec("master@{-2}"); - - /* TODO: make the test below actually fail - * cl_git_fail(git_revparse_single(&g_obj, g_repo, "master@{1a}")); - */ - - test_object("nope@{0}", NULL); - test_object("master@{31415}", NULL); - test_object("@{1000}", NULL); - test_object("@{2}", NULL); - - test_object("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - - test_object("master@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("heads/master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("refs/heads/master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); -} - -void test_refs_revparse__previous_head(void) -{ - assert_invalid_spec("@{-xyz}"); - assert_invalid_spec("@{-0}"); - assert_invalid_spec("@{-1b}"); - - test_object("@{-42}", NULL); - - test_object("@{-2}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("@{-1}", "a4a7dce85cf63874e984719f4fdd239f5145052f"); -} - -static void create_fake_stash_reference_and_reflog(git_repository *repo) -{ - git_reference *master; - git_buf log_path = GIT_BUF_INIT; - - git_buf_joinpath(&log_path, git_repository_path(repo), "logs/refs/fakestash"); - - cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&log_path))); - - cl_git_pass(git_reference_lookup(&master, repo, "refs/heads/master")); - cl_git_pass(git_reference_rename(master, "refs/fakestash", 0)); - - cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&log_path))); - - git_buf_free(&log_path); - git_reference_free(master); -} - -void test_refs_revparse__reflog_of_a_ref_under_refs(void) -{ - git_repository *repo = cl_git_sandbox_init("testrepo.git"); - - test_object_inrepo("refs/fakestash", NULL, repo); - - create_fake_stash_reference_and_reflog(repo); - - /* - * $ git reflog -1 refs/fakestash - * a65fedf refs/fakestash@{0}: commit: checking in - * - * $ git reflog -1 refs/fakestash@{0} - * a65fedf refs/fakestash@{0}: commit: checking in - * - * $ git reflog -1 fakestash - * a65fedf fakestash@{0}: commit: checking in - * - * $ git reflog -1 fakestash@{0} - * a65fedf fakestash@{0}: commit: checking in - */ - test_object_inrepo("refs/fakestash", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo); - test_object_inrepo("refs/fakestash@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo); - test_object_inrepo("fakestash", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo); - test_object_inrepo("fakestash@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo); - - cl_git_sandbox_cleanup(); -} - -void test_refs_revparse__revwalk(void) -{ - test_object("master^{/not found in any commit}", NULL); - test_object("master^{/merge}", NULL); - assert_invalid_spec("master^{/((}"); - - test_object("master^{/anoth}", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); - test_object("master^{/Merge}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("br2^{/Merge}", "a4a7dce85cf63874e984719f4fdd239f5145052f"); - test_object("master^{/fo.rth}", "9fd738e8f7967c078dceed8190330fc8648ee56a"); -} - -void test_refs_revparse__date(void) -{ - /* - * $ git reflog HEAD --date=iso - * a65fedf HEAD@{2012-04-30 08:23:41 -0900}: checkout: moving from br2 to master - * a4a7dce HEAD@{2012-04-30 08:23:37 -0900}: commit: checking in - * c47800c HEAD@{2012-04-30 08:23:28 -0900}: checkout: moving from master to br2 - * a65fedf HEAD@{2012-04-30 08:23:23 -0900}: commit: - * be3563a HEAD@{2012-04-30 10:22:43 -0700}: clone: from /Users/ben/src/libgit2/tes - * - * $ git reflog HEAD --date=raw - * a65fedf HEAD@{1335806621 -0900}: checkout: moving from br2 to master - * a4a7dce HEAD@{1335806617 -0900}: commit: checking in - * c47800c HEAD@{1335806608 -0900}: checkout: moving from master to br2 - * a65fedf HEAD@{1335806603 -0900}: commit: - * be3563a HEAD@{1335806563 -0700}: clone: from /Users/ben/src/libgit2/tests/resour - */ - test_object("HEAD@{10 years ago}", NULL); - - test_object("HEAD@{1 second}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("HEAD@{1 second ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("HEAD@{2 days ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - - /* - * $ git reflog master --date=iso - * a65fedf master@{2012-04-30 09:23:23 -0800}: commit: checking in - * be3563a master@{2012-04-30 09:22:43 -0800}: clone: from /Users/ben/src... - * - * $ git reflog master --date=raw - * a65fedf master@{1335806603 -0800}: commit: checking in - * be3563a master@{1335806563 -0800}: clone: from /Users/ben/src/libgit2/tests/reso - */ - - - /* - * $ git reflog -1 "master@{2012-04-30 17:22:42 +0000}" - * warning: Log for 'master' only goes back to Mon, 30 Apr 2012 09:22:43 -0800. - */ - test_object("master@{2012-04-30 17:22:42 +0000}", NULL); - test_object("master@{2012-04-30 09:22:42 -0800}", NULL); - - /* - * $ git reflog -1 "master@{2012-04-30 17:22:43 +0000}" - * be3563a master@{Mon Apr 30 09:22:43 2012 -0800}: clone: from /Users/ben/src/libg - */ - test_object("master@{2012-04-30 17:22:43 +0000}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("master@{2012-04-30 09:22:43 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - - /* - * $ git reflog -1 "master@{2012-4-30 09:23:27 -0800}" - * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in - */ - test_object("master@{2012-4-30 09:23:27 -0800}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - - /* - * $ git reflog -1 master@{2012-05-03} - * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in - */ - test_object("master@{2012-05-03}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - - /* - * $ git reflog -1 "master@{1335806603}" - * a65fedf - * - * $ git reflog -1 "master@{1335806602}" - * be3563a - */ - test_object("master@{1335806603}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("master@{1335806602}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); -} - -void test_refs_revparse__colon(void) -{ - assert_invalid_spec(":/"); - assert_invalid_spec("point_to_blob:readme.txt"); - cl_git_fail(git_revparse_single(&g_obj, g_repo, ":2:README")); /* Not implemented */ - - test_object(":/not found in any commit", NULL); - test_object("subtrees:ab/42.txt", NULL); - test_object("subtrees:ab/4.txt/nope", NULL); - test_object("subtrees:nope", NULL); - test_object("test/master^1:branch_file.txt", NULL); - - /* From tags */ - test_object("test:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf"); - test_object("tags/test:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf"); - test_object("e90810b:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf"); - test_object("tags/e90810b:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf"); - - /* From commits */ - test_object("a65f:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc"); - - /* From trees */ - test_object("a65f^{tree}:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc"); - test_object("944c:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc"); - - /* Retrieving trees */ - test_object("master:", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); - test_object("subtrees:", "ae90f12eea699729ed24555e40b9fd669da12a12"); - test_object("subtrees:ab", "f1425cef211cc08caa31e7b545ffb232acb098c3"); - test_object("subtrees:ab/", "f1425cef211cc08caa31e7b545ffb232acb098c3"); - - /* Retrieving blobs */ - test_object("subtrees:ab/4.txt", "d6c93164c249c8000205dd4ec5cbca1b516d487f"); - test_object("subtrees:ab/de/fgh/1.txt", "1f67fc4386b2d171e0d21be1c447e12660561f9b"); - test_object("master:README", "a8233120f6ad708f843d861ce2b7228ec4e3dec6"); - test_object("master:new.txt", "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"); - test_object(":/Merge", "a4a7dce85cf63874e984719f4fdd239f5145052f"); - test_object(":/one", "c47800c7266a2be04c571c04d5a6614691ea99bd"); - test_object(":/packed commit t", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9"); - test_object("test/master^2:branch_file.txt", "45b983be36b73c0788dc9cbcb76cbb80fc7bb057"); - test_object("test/master@{1}:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc"); -} - -void test_refs_revparse__disambiguation(void) -{ - /* - * $ git show e90810b - * tag e90810b - * Tagger: Vicent Marti - * Date: Thu Aug 12 03:59:17 2010 +0200 - * - * This is a very simple tag. - * - * commit e90810b8df3e80c413d903f631643c716887138d - * Author: Vicent Marti - * Date: Thu Aug 5 18:42:20 2010 +0200 - * - * Test commit 2 - * - * diff --git a/readme.txt b/readme.txt - * index 6336846..0266163 100644 - * --- a/readme.txt - * +++ b/readme.txt - * @@ -1 +1,2 @@ - * Testing a readme.txt - * +Now we add a single line here - * - * $ git show-ref e90810b - * 7b4384978d2493e851f9cca7858815fac9b10980 refs/tags/e90810b - * - */ - test_object("e90810b", "7b4384978d2493e851f9cca7858815fac9b10980"); - - /* - * $ git show e90810 - * commit e90810b8df3e80c413d903f631643c716887138d - * Author: Vicent Marti - * Date: Thu Aug 5 18:42:20 2010 +0200 - * - * Test commit 2 - * - * diff --git a/readme.txt b/readme.txt - * index 6336846..0266163 100644 - * --- a/readme.txt - * +++ b/readme.txt - * @@ -1 +1,2 @@ - * Testing a readme.txt - * +Now we add a single line here - */ - test_object("e90810", "e90810b8df3e80c413d903f631643c716887138d"); -} - -void test_refs_revparse__a_too_short_objectid_returns_EAMBIGUOUS(void) -{ - cl_assert_equal_i( - GIT_EAMBIGUOUS, git_revparse_single(&g_obj, g_repo, "e90")); -} - -void test_refs_revparse__issue_994(void) -{ - git_repository *repo; - git_reference *head, *with_at; - git_object *target; - - repo = cl_git_sandbox_init("testrepo.git"); - - cl_assert_equal_i(GIT_ENOTFOUND, - git_revparse_single(&target, repo, "origin/bim_with_3d@11296")); - - cl_assert_equal_i(GIT_ENOTFOUND, - git_revparse_single(&target, repo, "refs/remotes/origin/bim_with_3d@11296")); - - - cl_git_pass(git_repository_head(&head, repo)); - cl_git_pass(git_reference_create( - &with_at, - repo, - "refs/remotes/origin/bim_with_3d@11296", - git_reference_target(head), - 0)); - - cl_git_pass(git_revparse_single(&target, repo, "origin/bim_with_3d@11296")); - git_object_free(target); - - cl_git_pass(git_revparse_single(&target, repo, "refs/remotes/origin/bim_with_3d@11296")); - git_object_free(target); - - git_reference_free(with_at); - git_reference_free(head); - cl_git_sandbox_cleanup(); -} diff --git a/tests-clar/refs/unicode.c b/tests-clar/refs/unicode.c deleted file mode 100644 index 2ec103275d5..00000000000 --- a/tests-clar/refs/unicode.c +++ /dev/null @@ -1,44 +0,0 @@ -#include "clar_libgit2.h" - -static git_repository *repo; - -void test_refs_unicode__initialize(void) -{ - cl_fixture_sandbox("testrepo.git"); - - cl_git_pass(git_repository_open(&repo, "testrepo.git")); -} - -void test_refs_unicode__cleanup(void) -{ - git_repository_free(repo); - repo = NULL; - - cl_fixture_cleanup("testrepo.git"); -} - -void test_refs_unicode__create_and_lookup(void) -{ - git_reference *ref0, *ref1, *ref2; - git_repository *repo2; - - const char *REFNAME = "refs/heads/" "\305" "ngstr" "\366" "m"; - const char *master = "refs/heads/master"; - - /* Create the reference */ - cl_git_pass(git_reference_lookup(&ref0, repo, master)); - cl_git_pass(git_reference_create(&ref1, repo, REFNAME, git_reference_target(ref0), 0)); - cl_assert_equal_s(REFNAME, git_reference_name(ref1)); - - /* Lookup the reference in a different instance of the repository */ - cl_git_pass(git_repository_open(&repo2, "testrepo.git")); - cl_git_pass(git_reference_lookup(&ref2, repo2, REFNAME)); - - cl_assert(git_oid_cmp(git_reference_target(ref1), git_reference_target(ref2)) == 0); - cl_assert_equal_s(REFNAME, git_reference_name(ref2)); - - git_reference_free(ref0); - git_reference_free(ref1); - git_reference_free(ref2); - git_repository_free(repo2); -} diff --git a/tests-clar/refs/update.c b/tests-clar/refs/update.c deleted file mode 100644 index 6c2107ee2f9..00000000000 --- a/tests-clar/refs/update.c +++ /dev/null @@ -1,29 +0,0 @@ -#include "clar_libgit2.h" - -#include "refs.h" - -static git_repository *g_repo; - -void test_refs_update__initialize(void) -{ - g_repo = cl_git_sandbox_init("testrepo.git"); -} - -void test_refs_update__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_refs_update__updating_the_target_of_a_symref_with_an_invalid_name_returns_EINVALIDSPEC(void) -{ - git_reference *head; - - cl_git_pass(git_reference_lookup(&head, g_repo, GIT_HEAD_FILE)); - - cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head)); - - cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_symbolic_set_target( - head, "refs/heads/inv@{id")); - - git_reference_free(head); -} diff --git a/tests-clar/repo/discover.c b/tests-clar/repo/discover.c deleted file mode 100644 index 3d9aeedd789..00000000000 --- a/tests-clar/repo/discover.c +++ /dev/null @@ -1,142 +0,0 @@ -#include "clar_libgit2.h" - -#include "odb.h" -#include "repository.h" - - -#define TEMP_REPO_FOLDER "temprepo/" -#define DISCOVER_FOLDER TEMP_REPO_FOLDER "discover.git" - -#define SUB_REPOSITORY_FOLDER_NAME "sub_repo" -#define SUB_REPOSITORY_FOLDER DISCOVER_FOLDER "/" SUB_REPOSITORY_FOLDER_NAME -#define SUB_REPOSITORY_FOLDER_SUB SUB_REPOSITORY_FOLDER "/sub" -#define SUB_REPOSITORY_FOLDER_SUB_SUB SUB_REPOSITORY_FOLDER_SUB "/subsub" -#define SUB_REPOSITORY_FOLDER_SUB_SUB_SUB SUB_REPOSITORY_FOLDER_SUB_SUB "/subsubsub" - -#define REPOSITORY_ALTERNATE_FOLDER DISCOVER_FOLDER "/alternate_sub_repo" -#define REPOSITORY_ALTERNATE_FOLDER_SUB REPOSITORY_ALTERNATE_FOLDER "/sub" -#define REPOSITORY_ALTERNATE_FOLDER_SUB_SUB REPOSITORY_ALTERNATE_FOLDER_SUB "/subsub" -#define REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB REPOSITORY_ALTERNATE_FOLDER_SUB_SUB "/subsubsub" - -#define ALTERNATE_MALFORMED_FOLDER1 DISCOVER_FOLDER "/alternate_malformed_repo1" -#define ALTERNATE_MALFORMED_FOLDER2 DISCOVER_FOLDER "/alternate_malformed_repo2" -#define ALTERNATE_MALFORMED_FOLDER3 DISCOVER_FOLDER "/alternate_malformed_repo3" -#define ALTERNATE_NOT_FOUND_FOLDER DISCOVER_FOLDER "/alternate_not_found_repo" - -static void ensure_repository_discover(const char *start_path, - const char *ceiling_dirs, - const char *expected_path) -{ - char found_path[GIT_PATH_MAX]; - cl_git_pass(git_repository_discover(found_path, sizeof(found_path), start_path, 0, ceiling_dirs)); - //across_fs is always 0 as we can't automate the filesystem change tests - cl_assert_equal_s(found_path, expected_path); -} - -static void write_file(const char *path, const char *content) -{ - git_file file; - int error; - - if (git_path_exists(path)) { - cl_git_pass(p_unlink(path)); - } - - file = git_futils_creat_withpath(path, 0777, 0666); - cl_assert(file >= 0); - - error = p_write(file, content, strlen(content) * sizeof(char)); - p_close(file); - cl_git_pass(error); -} - -//no check is performed on ceiling_dirs length, so be sure it's long enough -static void append_ceiling_dir(git_buf *ceiling_dirs, const char *path) -{ - git_buf pretty_path = GIT_BUF_INIT; - char ceiling_separator[2] = { GIT_PATH_LIST_SEPARATOR, '\0' }; - - cl_git_pass(git_path_prettify_dir(&pretty_path, path, NULL)); - - if (ceiling_dirs->size > 0) - git_buf_puts(ceiling_dirs, ceiling_separator); - - git_buf_puts(ceiling_dirs, pretty_path.ptr); - - git_buf_free(&pretty_path); - cl_assert(git_buf_oom(ceiling_dirs) == 0); -} - -void test_repo_discover__0(void) -{ - // test discover - git_repository *repo; - git_buf ceiling_dirs_buf = GIT_BUF_INIT; - const char *ceiling_dirs; - char repository_path[GIT_PATH_MAX]; - char sub_repository_path[GIT_PATH_MAX]; - char found_path[GIT_PATH_MAX]; - const mode_t mode = 0777; - - git_futils_mkdir_r(DISCOVER_FOLDER, NULL, mode); - append_ceiling_dir(&ceiling_dirs_buf, TEMP_REPO_FOLDER); - ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf); - - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs)); - - cl_git_pass(git_repository_init(&repo, DISCOVER_FOLDER, 1)); - cl_git_pass(git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs)); - git_repository_free(repo); - - cl_git_pass(git_repository_init(&repo, SUB_REPOSITORY_FOLDER, 0)); - cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode)); - cl_git_pass(git_repository_discover(sub_repository_path, sizeof(sub_repository_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs)); - - cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode)); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, sub_repository_path); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, sub_repository_path); - - cl_git_pass(git_futils_mkdir_r(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, NULL, mode)); - write_file(REPOSITORY_ALTERNATE_FOLDER "/" DOT_GIT, "gitdir: ../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT); - write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB "/" DOT_GIT, "gitdir: ../../../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT); - write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB "/" DOT_GIT, "gitdir: ../../../../"); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, repository_path); - - cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER1, NULL, mode)); - write_file(ALTERNATE_MALFORMED_FOLDER1 "/" DOT_GIT, "Anything but not gitdir:"); - cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER2, NULL, mode)); - write_file(ALTERNATE_MALFORMED_FOLDER2 "/" DOT_GIT, "gitdir:"); - cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER3, NULL, mode)); - write_file(ALTERNATE_MALFORMED_FOLDER3 "/" DOT_GIT, "gitdir: \n\n\n"); - cl_git_pass(git_futils_mkdir_r(ALTERNATE_NOT_FOUND_FOLDER, NULL, mode)); - write_file(ALTERNATE_NOT_FOUND_FOLDER "/" DOT_GIT, "gitdir: a_repository_that_surely_does_not_exist"); - cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs)); - cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs)); - cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER3, 0, ceiling_dirs)); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), ALTERNATE_NOT_FOUND_FOLDER, 0, ceiling_dirs)); - - append_ceiling_dir(&ceiling_dirs_buf, SUB_REPOSITORY_FOLDER); - ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf); - - //this must pass as ceiling_directories cannot predent the current - //working directory to be checked - cl_git_pass(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs)); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB, 0, ceiling_dirs)); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs)); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs)); - - //.gitfile redirection should not be affected by ceiling directories - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, repository_path); - - cl_git_pass(git_futils_rmdir_r(TEMP_REPO_FOLDER, NULL, GIT_RMDIR_REMOVE_FILES)); - git_repository_free(repo); - git_buf_free(&ceiling_dirs_buf); -} - diff --git a/tests-clar/repo/getters.c b/tests-clar/repo/getters.c deleted file mode 100644 index b372f5b7098..00000000000 --- a/tests-clar/repo/getters.c +++ /dev/null @@ -1,40 +0,0 @@ -#include "clar_libgit2.h" - -void test_repo_getters__is_empty_correctly_deals_with_pristine_looking_repos(void) -{ - git_repository *repo; - - repo = cl_git_sandbox_init("empty_bare.git"); - cl_git_remove_placeholders(git_repository_path(repo), "dummy-marker.txt"); - - cl_assert_equal_i(true, git_repository_is_empty(repo)); - - cl_git_sandbox_cleanup(); -} - -void test_repo_getters__is_empty_can_detect_used_repositories(void) -{ - git_repository *repo; - - cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); - - cl_assert_equal_i(false, git_repository_is_empty(repo)); - - git_repository_free(repo); -} - -void test_repo_getters__retrieving_the_odb_honors_the_refcount(void) -{ - git_odb *odb; - git_repository *repo; - - cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); - - cl_git_pass(git_repository_odb(&odb, repo)); - cl_assert(((git_refcount *)odb)->refcount == 2); - - git_repository_free(repo); - cl_assert(((git_refcount *)odb)->refcount == 1); - - git_odb_free(odb); -} diff --git a/tests-clar/repo/hashfile.c b/tests-clar/repo/hashfile.c deleted file mode 100644 index 129e5d371b9..00000000000 --- a/tests-clar/repo/hashfile.c +++ /dev/null @@ -1,88 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" - -static git_repository *_repo; - -void test_repo_hashfile__initialize(void) -{ - _repo = cl_git_sandbox_init("status"); -} - -void test_repo_hashfile__cleanup(void) -{ - cl_git_sandbox_cleanup(); - _repo = NULL; -} - -void test_repo_hashfile__simple(void) -{ - git_oid a, b; - git_buf full = GIT_BUF_INIT; - - /* hash with repo relative path */ - cl_git_pass(git_odb_hashfile(&a, "status/current_file", GIT_OBJ_BLOB)); - cl_git_pass(git_repository_hashfile(&b, _repo, "current_file", GIT_OBJ_BLOB, NULL)); - cl_assert(git_oid_equal(&a, &b)); - - cl_git_pass(git_buf_joinpath(&full, git_repository_workdir(_repo), "current_file")); - - /* hash with full path */ - cl_git_pass(git_odb_hashfile(&a, full.ptr, GIT_OBJ_BLOB)); - cl_git_pass(git_repository_hashfile(&b, _repo, full.ptr, GIT_OBJ_BLOB, NULL)); - cl_assert(git_oid_equal(&a, &b)); - - /* hash with invalid type */ - cl_git_fail(git_odb_hashfile(&a, full.ptr, GIT_OBJ_ANY)); - cl_git_fail(git_repository_hashfile(&b, _repo, full.ptr, GIT_OBJ_OFS_DELTA, NULL)); - - git_buf_free(&full); -} - -void test_repo_hashfile__filtered(void) -{ - git_oid a, b; - git_config *config; - - cl_git_pass(git_repository_config(&config, _repo)); - cl_git_pass(git_config_set_bool(config, "core.autocrlf", true)); - git_config_free(config); - - cl_git_append2file("status/.gitattributes", "*.txt text\n*.bin binary\n\n"); - - /* create some sample content with CRLF in it */ - cl_git_mkfile("status/testfile.txt", "content\r\n"); - cl_git_mkfile("status/testfile.bin", "other\r\nstuff\r\n"); - - /* not equal hashes because of filtering */ - cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJ_BLOB)); - cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJ_BLOB, NULL)); - cl_assert(git_oid_cmp(&a, &b)); - - /* equal hashes because filter is binary */ - cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJ_BLOB)); - cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJ_BLOB, NULL)); - cl_assert(git_oid_equal(&a, &b)); - - /* equal hashes when 'as_file' points to binary filtering */ - cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJ_BLOB)); - cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJ_BLOB, "foo.bin")); - cl_assert(git_oid_equal(&a, &b)); - - /* not equal hashes when 'as_file' points to text filtering */ - cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJ_BLOB)); - cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJ_BLOB, "foo.txt")); - cl_assert(git_oid_cmp(&a, &b)); - - /* equal hashes when 'as_file' is empty and turns off filtering */ - cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJ_BLOB)); - cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJ_BLOB, "")); - cl_assert(git_oid_equal(&a, &b)); - - cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJ_BLOB)); - cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJ_BLOB, "")); - cl_assert(git_oid_equal(&a, &b)); - - /* some hash type failures */ - cl_git_fail(git_odb_hashfile(&a, "status/testfile.txt", 0)); - cl_git_fail(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJ_ANY, NULL)); -} diff --git a/tests-clar/repo/head.c b/tests-clar/repo/head.c deleted file mode 100644 index a9f5cfc58d7..00000000000 --- a/tests-clar/repo/head.c +++ /dev/null @@ -1,196 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" -#include "repo_helpers.h" -#include "posix.h" - -static git_repository *repo; - -void test_repo_head__initialize(void) -{ - repo = cl_git_sandbox_init("testrepo.git"); -} - -void test_repo_head__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_repo_head__head_detached(void) -{ - git_reference *ref; - - cl_git_pass(git_repository_head_detached(repo)); - - cl_git_pass(git_repository_detach_head(repo)); - - cl_assert_equal_i(true, git_repository_head_detached(repo)); - - /* take the reop back to it's original state */ - cl_git_pass(git_reference_symbolic_create(&ref, repo, "HEAD", "refs/heads/master", 1)); - git_reference_free(ref); - - cl_assert_equal_i(false, git_repository_head_detached(repo)); -} - -void test_repo_head__head_orphan(void) -{ - git_reference *ref; - - cl_git_pass(git_repository_head_detached(repo)); - - make_head_orphaned(repo, NON_EXISTING_HEAD); - - cl_assert(git_repository_head_orphan(repo) == 1); - - - /* take the repo back to it's original state */ - cl_git_pass(git_reference_symbolic_create(&ref, repo, "HEAD", "refs/heads/master", 1)); - cl_assert(git_repository_head_orphan(repo) == 0); - - git_reference_free(ref); -} - -void test_repo_head__set_head_Attaches_HEAD_to_un_unborn_branch_when_the_branch_doesnt_exist(void) -{ - git_reference *head; - - cl_git_pass(git_repository_set_head(repo, "refs/heads/doesnt/exist/yet")); - - cl_assert_equal_i(false, git_repository_head_detached(repo)); - - cl_assert_equal_i(GIT_EORPHANEDHEAD, git_repository_head(&head, repo)); -} - -void test_repo_head__set_head_Returns_ENOTFOUND_when_the_reference_doesnt_exist(void) -{ - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head(repo, "refs/tags/doesnt/exist/yet")); -} - -void test_repo_head__set_head_Fails_when_the_reference_points_to_a_non_commitish(void) -{ - cl_git_fail(git_repository_set_head(repo, "refs/tags/point_to_blob")); -} - -void test_repo_head__set_head_Attaches_HEAD_when_the_reference_points_to_a_branch(void) -{ - git_reference *head; - - cl_git_pass(git_repository_set_head(repo, "refs/heads/br2")); - - cl_assert_equal_i(false, git_repository_head_detached(repo)); - - cl_git_pass(git_repository_head(&head, repo)); - cl_assert_equal_s("refs/heads/br2", git_reference_name(head)); - - git_reference_free(head); -} - -static void assert_head_is_correctly_detached(void) -{ - git_reference *head; - git_object *commit; - - cl_assert_equal_i(true, git_repository_head_detached(repo)); - - cl_git_pass(git_repository_head(&head, repo)); - - cl_git_pass(git_object_lookup(&commit, repo, git_reference_target(head), GIT_OBJ_COMMIT)); - - git_object_free(commit); - git_reference_free(head); -} - -void test_repo_head__set_head_Detaches_HEAD_when_the_reference_doesnt_point_to_a_branch(void) -{ - cl_git_pass(git_repository_set_head(repo, "refs/tags/test")); - - cl_assert_equal_i(true, git_repository_head_detached(repo)); - - assert_head_is_correctly_detached(); -} - -void test_repo_head__set_head_detached_Return_ENOTFOUND_when_the_object_doesnt_exist(void) -{ - git_oid oid; - - cl_git_pass(git_oid_fromstr(&oid, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef")); - - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head_detached(repo, &oid)); -} - -void test_repo_head__set_head_detached_Fails_when_the_object_isnt_a_commitish(void) -{ - git_object *blob; - - cl_git_pass(git_revparse_single(&blob, repo, "point_to_blob")); - - cl_git_fail(git_repository_set_head_detached(repo, git_object_id(blob))); - - git_object_free(blob); -} - -void test_repo_head__set_head_detached_Detaches_HEAD_and_make_it_point_to_the_peeled_commit(void) -{ - git_object *tag; - - cl_git_pass(git_revparse_single(&tag, repo, "tags/test")); - cl_assert_equal_i(GIT_OBJ_TAG, git_object_type(tag)); - - cl_git_pass(git_repository_set_head_detached(repo, git_object_id(tag))); - - assert_head_is_correctly_detached(); - - git_object_free(tag); -} - -void test_repo_head__detach_head_Detaches_HEAD_and_make_it_point_to_the_peeled_commit(void) -{ - cl_assert_equal_i(false, git_repository_head_detached(repo)); - - cl_git_pass(git_repository_detach_head(repo)); - - assert_head_is_correctly_detached(); -} - -void test_repo_head__detach_head_Fails_if_HEAD_and_point_to_a_non_commitish(void) -{ - git_reference *head; - - cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, "refs/tags/point_to_blob", 1)); - - cl_git_fail(git_repository_detach_head(repo)); - - git_reference_free(head); -} - -void test_repo_head__detaching_an_orphaned_head_returns_GIT_EORPHANEDHEAD(void) -{ - make_head_orphaned(repo, NON_EXISTING_HEAD); - - cl_assert_equal_i(GIT_EORPHANEDHEAD, git_repository_detach_head(repo)); -} - -void test_repo_head__retrieving_an_orphaned_head_returns_GIT_EORPHANEDHEAD(void) -{ - git_reference *head; - - make_head_orphaned(repo, NON_EXISTING_HEAD); - - cl_assert_equal_i(GIT_EORPHANEDHEAD, git_repository_head(&head, repo)); -} - -void test_repo_head__retrieving_a_missing_head_returns_GIT_ENOTFOUND(void) -{ - git_reference *head; - - delete_head(repo); - - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_head(&head, repo)); -} - -void test_repo_head__can_tell_if_an_orphaned_head_is_detached(void) -{ - make_head_orphaned(repo, NON_EXISTING_HEAD); - - cl_assert_equal_i(false, git_repository_head_detached(repo)); -} diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c deleted file mode 100644 index 9ddb3954558..00000000000 --- a/tests-clar/repo/init.c +++ /dev/null @@ -1,405 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" -#include "repository.h" -#include "config.h" -#include "path.h" - -enum repo_mode { - STANDARD_REPOSITORY = 0, - BARE_REPOSITORY = 1 -}; - -static git_repository *_repo; - -void test_repo_init__initialize(void) -{ - _repo = NULL; -} - -static void cleanup_repository(void *path) -{ - git_repository_free(_repo); - _repo = NULL; - - cl_fixture_cleanup((const char *)path); -} - -static void ensure_repository_init( - const char *working_directory, - int is_bare, - const char *expected_path_repository, - const char *expected_working_directory) -{ - const char *workdir; - - cl_assert(!git_path_isdir(working_directory)); - - cl_git_pass(git_repository_init(&_repo, working_directory, is_bare)); - - workdir = git_repository_workdir(_repo); - if (workdir != NULL || expected_working_directory != NULL) { - cl_assert( - git__suffixcmp(workdir, expected_working_directory) == 0 - ); - } - - cl_assert( - git__suffixcmp(git_repository_path(_repo), expected_path_repository) == 0 - ); - - cl_assert(git_repository_is_bare(_repo) == is_bare); - -#ifdef GIT_WIN32 - if (!is_bare) { - DWORD fattrs = GetFileAttributes(git_repository_path(_repo)); - cl_assert((fattrs & FILE_ATTRIBUTE_HIDDEN) != 0); - } -#endif - - cl_assert(git_repository_is_empty(_repo)); -} - -void test_repo_init__standard_repo(void) -{ - cl_set_cleanup(&cleanup_repository, "testrepo"); - ensure_repository_init("testrepo/", 0, "testrepo/.git/", "testrepo/"); -} - -void test_repo_init__standard_repo_noslash(void) -{ - cl_set_cleanup(&cleanup_repository, "testrepo"); - ensure_repository_init("testrepo", 0, "testrepo/.git/", "testrepo/"); -} - -void test_repo_init__bare_repo(void) -{ - cl_set_cleanup(&cleanup_repository, "testrepo.git"); - ensure_repository_init("testrepo.git/", 1, "testrepo.git/", NULL); -} - -void test_repo_init__bare_repo_noslash(void) -{ - cl_set_cleanup(&cleanup_repository, "testrepo.git"); - ensure_repository_init("testrepo.git", 1, "testrepo.git/", NULL); -} - -void test_repo_init__bare_repo_escaping_current_workdir(void) -{ - git_buf path_repository = GIT_BUF_INIT; - git_buf path_current_workdir = GIT_BUF_INIT; - - cl_git_pass(git_path_prettify_dir(&path_current_workdir, ".", NULL)); - - cl_git_pass(git_buf_joinpath(&path_repository, git_buf_cstr(&path_current_workdir), "a/b/c")); - cl_git_pass(git_futils_mkdir_r(git_buf_cstr(&path_repository), NULL, GIT_DIR_MODE)); - - /* Change the current working directory */ - cl_git_pass(chdir(git_buf_cstr(&path_repository))); - - /* Initialize a bare repo with a relative path escaping out of the current working directory */ - cl_git_pass(git_repository_init(&_repo, "../d/e.git", 1)); - cl_git_pass(git__suffixcmp(git_repository_path(_repo), "/a/b/d/e.git/")); - - git_repository_free(_repo); - - /* Open a bare repo with a relative path escaping out of the current working directory */ - cl_git_pass(git_repository_open(&_repo, "../d/e.git")); - - cl_git_pass(chdir(git_buf_cstr(&path_current_workdir))); - - git_buf_free(&path_current_workdir); - git_buf_free(&path_repository); - - cleanup_repository("a"); -} - -void test_repo_init__reinit_bare_repo(void) -{ - cl_set_cleanup(&cleanup_repository, "reinit.git"); - - /* Initialize the repository */ - cl_git_pass(git_repository_init(&_repo, "reinit.git", 1)); - git_repository_free(_repo); - - /* Reinitialize the repository */ - cl_git_pass(git_repository_init(&_repo, "reinit.git", 1)); -} - -void test_repo_init__reinit_too_recent_bare_repo(void) -{ - git_config *config; - - /* Initialize the repository */ - cl_git_pass(git_repository_init(&_repo, "reinit.git", 1)); - git_repository_config(&config, _repo); - - /* - * Hack the config of the repository to make it look like it has - * been created by a recenter version of git/libgit2 - */ - cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 42)); - - git_config_free(config); - git_repository_free(_repo); - - /* Try to reinitialize the repository */ - cl_git_fail(git_repository_init(&_repo, "reinit.git", 1)); - - cl_fixture_cleanup("reinit.git"); -} - -void test_repo_init__additional_templates(void) -{ - git_buf path = GIT_BUF_INIT; - - cl_set_cleanup(&cleanup_repository, "tester"); - - ensure_repository_init("tester", 0, "tester/.git/", "tester/"); - - cl_git_pass( - git_buf_joinpath(&path, git_repository_path(_repo), "description")); - cl_assert(git_path_isfile(git_buf_cstr(&path))); - - cl_git_pass( - git_buf_joinpath(&path, git_repository_path(_repo), "info/exclude")); - cl_assert(git_path_isfile(git_buf_cstr(&path))); - - cl_git_pass( - git_buf_joinpath(&path, git_repository_path(_repo), "hooks")); - cl_assert(git_path_isdir(git_buf_cstr(&path))); - /* won't confirm specific contents of hooks dir since it may vary */ - - git_buf_free(&path); -} - -static void assert_config_entry_on_init_bytype(const char *config_key, int expected_value, bool is_bare) -{ - git_config *config; - int current_value; - git_buf repo_path = GIT_BUF_INIT; - - cl_set_cleanup(&cleanup_repository, "config_entry"); - - cl_git_pass(git_buf_puts(&repo_path, "config_entry/test.")); - - if (!is_bare) - cl_git_pass(git_buf_puts(&repo_path, "non.")); - - cl_git_pass(git_buf_puts(&repo_path, "bare.git")); - - cl_git_pass(git_repository_init(&_repo, git_buf_cstr(&repo_path), is_bare)); - - git_buf_free(&repo_path); - - git_repository_config(&config, _repo); - - if (expected_value >= 0) { - cl_git_pass(git_config_get_bool(¤t_value, config, config_key)); - - cl_assert_equal_i(expected_value, current_value); - } else { - int error = git_config_get_bool(¤t_value, config, config_key); - - cl_assert_equal_i(expected_value, error); - } - - git_config_free(config); -} - -static void assert_config_entry_on_init(const char *config_key, int expected_value) -{ - assert_config_entry_on_init_bytype(config_key, expected_value, true); - git_repository_free(_repo); - - assert_config_entry_on_init_bytype(config_key, expected_value, false); -} - -void test_repo_init__detect_filemode(void) -{ -#ifdef GIT_WIN32 - assert_config_entry_on_init("core.filemode", false); -#else - assert_config_entry_on_init("core.filemode", true); -#endif -} - -#define CASE_INSENSITIVE_FILESYSTEM (defined GIT_WIN32 || defined __APPLE__) - -void test_repo_init__detect_ignorecase(void) -{ -#if CASE_INSENSITIVE_FILESYSTEM - assert_config_entry_on_init("core.ignorecase", true); -#else - assert_config_entry_on_init("core.ignorecase", GIT_ENOTFOUND); -#endif -} - -void test_repo_init__reinit_doesnot_overwrite_ignorecase(void) -{ - git_config *config; - int current_value; - - /* Init a new repo */ - cl_set_cleanup(&cleanup_repository, "not.overwrite.git"); - cl_git_pass(git_repository_init(&_repo, "not.overwrite.git", 1)); - - /* Change the "core.ignorecase" config value to something unlikely */ - git_repository_config(&config, _repo); - git_config_set_int32(config, "core.ignorecase", 42); - git_config_free(config); - git_repository_free(_repo); - _repo = NULL; - - /* Reinit the repository */ - cl_git_pass(git_repository_init(&_repo, "not.overwrite.git", 1)); - git_repository_config(&config, _repo); - - /* Ensure the "core.ignorecase" config value hasn't been updated */ - cl_git_pass(git_config_get_int32(¤t_value, config, "core.ignorecase")); - cl_assert_equal_i(42, current_value); - - git_config_free(config); -} - -void test_repo_init__reinit_overwrites_filemode(void) -{ - git_config *config; - int expected, current_value; - -#ifdef GIT_WIN32 - expected = false; -#else - expected = true; -#endif - - /* Init a new repo */ - cl_set_cleanup(&cleanup_repository, "overwrite.git"); - cl_git_pass(git_repository_init(&_repo, "overwrite.git", 1)); - - - /* Change the "core.filemode" config value to something unlikely */ - git_repository_config(&config, _repo); - git_config_set_bool(config, "core.filemode", !expected); - git_config_free(config); - git_repository_free(_repo); - _repo = NULL; - - /* Reinit the repository */ - cl_git_pass(git_repository_init(&_repo, "overwrite.git", 1)); - git_repository_config(&config, _repo); - - /* Ensure the "core.filemode" config value has been reset */ - cl_git_pass(git_config_get_bool(¤t_value, config, "core.filemode")); - cl_assert_equal_i(expected, current_value); - - git_config_free(config); -} - -void test_repo_init__sets_logAllRefUpdates_according_to_type_of_repository(void) -{ - assert_config_entry_on_init_bytype("core.logallrefupdates", GIT_ENOTFOUND, true); - git_repository_free(_repo); - assert_config_entry_on_init_bytype("core.logallrefupdates", true, false); -} - -void test_repo_init__extended_0(void) -{ - git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; - - /* without MKDIR this should fail */ - cl_git_fail(git_repository_init_ext(&_repo, "extended", &opts)); - - /* make the directory first, then it should succeed */ - cl_git_pass(git_futils_mkdir("extended", NULL, 0775, 0)); - cl_git_pass(git_repository_init_ext(&_repo, "extended", &opts)); - - cl_assert(!git__suffixcmp(git_repository_workdir(_repo), "/extended/")); - cl_assert(!git__suffixcmp(git_repository_path(_repo), "/extended/.git/")); - cl_assert(!git_repository_is_bare(_repo)); - cl_assert(git_repository_is_empty(_repo)); - - cleanup_repository("extended"); -} - -void test_repo_init__extended_1(void) -{ - git_reference *ref; - git_remote *remote; - struct stat st; - git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; - - opts.flags = GIT_REPOSITORY_INIT_MKPATH | - GIT_REPOSITORY_INIT_NO_DOTGIT_DIR; - opts.mode = GIT_REPOSITORY_INIT_SHARED_GROUP; - opts.workdir_path = "../c_wd"; - opts.description = "Awesomest test repository evah"; - opts.initial_head = "development"; - opts.origin_url = "https://github.com/libgit2/libgit2.git"; - - cl_git_pass(git_repository_init_ext(&_repo, "root/b/c.git", &opts)); - - cl_assert(!git__suffixcmp(git_repository_workdir(_repo), "/c_wd/")); - cl_assert(!git__suffixcmp(git_repository_path(_repo), "/c.git/")); - cl_assert(git_path_isfile("root/b/c_wd/.git")); - cl_assert(!git_repository_is_bare(_repo)); - /* repo will not be counted as empty because we set head to "development" */ - cl_assert(!git_repository_is_empty(_repo)); - - cl_git_pass(git_path_lstat(git_repository_path(_repo), &st)); - cl_assert(S_ISDIR(st.st_mode)); - cl_assert((S_ISGID & st.st_mode) == S_ISGID); - - cl_git_pass(git_reference_lookup(&ref, _repo, "HEAD")); - cl_assert(git_reference_type(ref) == GIT_REF_SYMBOLIC); - cl_assert_equal_s("refs/heads/development", git_reference_symbolic_target(ref)); - git_reference_free(ref); - - cl_git_pass(git_remote_load(&remote, _repo, "origin")); - cl_assert_equal_s("origin", git_remote_name(remote)); - cl_assert_equal_s(opts.origin_url, git_remote_url(remote)); - git_remote_free(remote); - - git_repository_free(_repo); - cl_fixture_cleanup("root"); -} - -void test_repo_init__extended_with_template(void) -{ - git_buf expected = GIT_BUF_INIT; - git_buf actual = GIT_BUF_INIT; - - git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; - - opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_BARE | GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE; - opts.template_path = cl_fixture("template"); - - cl_git_pass(git_repository_init_ext(&_repo, "templated.git", &opts)); - - cl_assert(git_repository_is_bare(_repo)); - cl_assert(!git__suffixcmp(git_repository_path(_repo), "/templated.git/")); - - cl_assert(git_futils_readbuffer(&expected,cl_fixture("template/description")) == GIT_OK); - cl_assert(git_futils_readbuffer(&actual,"templated.git/description") == GIT_OK); - - cl_assert(!git_buf_cmp(&expected,&actual)); - - git_buf_free(&expected); - git_buf_free(&actual); - - cleanup_repository("templated.git"); -} - -void test_repo_init__can_reinit_an_initialized_repository(void) -{ - git_repository *reinit; - - cl_git_pass(git_futils_mkdir("extended", NULL, 0775, 0)); - cl_git_pass(git_repository_init(&_repo, "extended", false)); - - cl_git_pass(git_repository_init(&reinit, "extended", false)); - - cl_assert_equal_s(git_repository_path(_repo), git_repository_path(reinit)); - - git_repository_free(reinit); - cleanup_repository("extended"); -} diff --git a/tests-clar/repo/message.c b/tests-clar/repo/message.c deleted file mode 100644 index 4a6f13b9df1..00000000000 --- a/tests-clar/repo/message.c +++ /dev/null @@ -1,47 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "refs.h" -#include "posix.h" - -static git_repository *_repo; -static git_buf _path; -static char *_actual; - -void test_repo_message__initialize(void) -{ - _repo = cl_git_sandbox_init("testrepo.git"); -} - -void test_repo_message__cleanup(void) -{ - cl_git_sandbox_cleanup(); - git_buf_free(&_path); - git__free(_actual); - _actual = NULL; -} - -void test_repo_message__none(void) -{ - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(NULL, 0, _repo)); -} - -void test_repo_message__message(void) -{ - const char expected[] = "Test\n\nThis is a test of the emergency broadcast system\n"; - ssize_t len; - - cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), "MERGE_MSG")); - cl_git_mkfile(git_buf_cstr(&_path), expected); - - len = git_repository_message(NULL, 0, _repo); - cl_assert(len > 0); - _actual = git__malloc(len + 1); - cl_assert(_actual != NULL); - - cl_assert(git_repository_message(_actual, len, _repo) > 0); - _actual[len] = '\0'; - cl_assert_equal_s(expected, _actual); - - cl_git_pass(p_unlink(git_buf_cstr(&_path))); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(NULL, 0, _repo)); -} diff --git a/tests-clar/repo/open.c b/tests-clar/repo/open.c deleted file mode 100644 index 7f93ae91a0e..00000000000 --- a/tests-clar/repo/open.c +++ /dev/null @@ -1,282 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" -#include - -void test_repo_open__cleanup(void) -{ - cl_git_sandbox_cleanup(); - - if (git_path_isdir("alternate")) - git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES); -} - -void test_repo_open__bare_empty_repo(void) -{ - git_repository *repo = cl_git_sandbox_init("empty_bare.git"); - - cl_assert(git_repository_path(repo) != NULL); - cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0); - cl_assert(git_repository_workdir(repo) == NULL); -} - -void test_repo_open__standard_empty_repo_through_gitdir(void) -{ - git_repository *repo; - - cl_git_pass(git_repository_open(&repo, cl_fixture("empty_standard_repo/.gitted"))); - - cl_assert(git_repository_path(repo) != NULL); - cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0); - - cl_assert(git_repository_workdir(repo) != NULL); - cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0); - - git_repository_free(repo); -} - -void test_repo_open__standard_empty_repo_through_workdir(void) -{ - git_repository *repo = cl_git_sandbox_init("empty_standard_repo"); - - cl_assert(git_repository_path(repo) != NULL); - cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0); - - cl_assert(git_repository_workdir(repo) != NULL); - cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0); -} - - -void test_repo_open__open_with_discover(void) -{ - static const char *variants[] = { - "attr", "attr/", "attr/.git", "attr/.git/", - "attr/sub", "attr/sub/", "attr/sub/sub", "attr/sub/sub/", - NULL - }; - git_repository *repo; - const char **scan; - - cl_fixture_sandbox("attr"); - cl_git_pass(p_rename("attr/.gitted", "attr/.git")); - - for (scan = variants; *scan != NULL; scan++) { - cl_git_pass(git_repository_open_ext(&repo, *scan, 0, NULL)); - cl_assert(git__suffixcmp(git_repository_path(repo), "attr/.git/") == 0); - cl_assert(git__suffixcmp(git_repository_workdir(repo), "attr/") == 0); - git_repository_free(repo); - } - - cl_fixture_cleanup("attr"); -} - -void test_repo_open__gitlinked(void) -{ - /* need to have both repo dir and workdir set up correctly */ - git_repository *repo = cl_git_sandbox_init("empty_standard_repo"); - git_repository *repo2; - - cl_must_pass(p_mkdir("alternate", 0777)); - cl_git_mkfile("alternate/.git", "gitdir: ../empty_standard_repo/.git"); - - cl_git_pass(git_repository_open(&repo2, "alternate")); - - cl_assert(git_repository_path(repo2) != NULL); - cl_assert_(git__suffixcmp(git_repository_path(repo2), "empty_standard_repo/.git/") == 0, git_repository_path(repo2)); - cl_assert_equal_s(git_repository_path(repo), git_repository_path(repo2)); - - cl_assert(git_repository_workdir(repo2) != NULL); - cl_assert_(git__suffixcmp(git_repository_workdir(repo2), "alternate/") == 0, git_repository_workdir(repo2)); - - git_repository_free(repo2); -} - -void test_repo_open__from_git_new_workdir(void) -{ - /* The git-new-workdir script that ships with git sets up a bunch of - * symlinks to create a second workdir that shares the object db with - * another checkout. Libgit2 can open a repo that has been configured - * this way. - */ - cl_git_sandbox_init("empty_standard_repo"); - -#ifndef GIT_WIN32 - git_repository *repo2; - git_buf link_tgt = GIT_BUF_INIT, link = GIT_BUF_INIT, body = GIT_BUF_INIT; - const char **scan; - int link_fd; - static const char *links[] = { - "config", "refs", "logs/refs", "objects", "info", "hooks", - "packed-refs", "remotes", "rr-cache", "svn", NULL - }; - static const char *copies[] = { - "HEAD", NULL - }; - - cl_git_pass(p_mkdir("alternate", 0777)); - cl_git_pass(p_mkdir("alternate/.git", 0777)); - - for (scan = links; *scan != NULL; scan++) { - git_buf_joinpath(&link_tgt, "empty_standard_repo/.git", *scan); - if (git_path_exists(link_tgt.ptr)) { - git_buf_joinpath(&link_tgt, "../../empty_standard_repo/.git", *scan); - git_buf_joinpath(&link, "alternate/.git", *scan); - if (strchr(*scan, '/')) - git_futils_mkpath2file(link.ptr, 0777); - cl_assert_(symlink(link_tgt.ptr, link.ptr) == 0, strerror(errno)); - } - } - for (scan = copies; *scan != NULL; scan++) { - git_buf_joinpath(&link_tgt, "empty_standard_repo/.git", *scan); - if (git_path_exists(link_tgt.ptr)) { - git_buf_joinpath(&link, "alternate/.git", *scan); - cl_git_pass(git_futils_readbuffer(&body, link_tgt.ptr)); - - cl_assert((link_fd = git_futils_creat_withpath(link.ptr, 0777, 0666)) >= 0); - cl_must_pass(p_write(link_fd, body.ptr, body.size)); - p_close(link_fd); - } - } - - git_buf_free(&link_tgt); - git_buf_free(&link); - git_buf_free(&body); - - - cl_git_pass(git_repository_open(&repo2, "alternate")); - - cl_assert(git_repository_path(repo2) != NULL); - cl_assert_(git__suffixcmp(git_repository_path(repo2), "alternate/.git/") == 0, git_repository_path(repo2)); - - cl_assert(git_repository_workdir(repo2) != NULL); - cl_assert_(git__suffixcmp(git_repository_workdir(repo2), "alternate/") == 0, git_repository_workdir(repo2)); - - git_repository_free(repo2); -#endif -} - -void test_repo_open__failures(void) -{ - git_repository *base, *repo; - git_buf ceiling = GIT_BUF_INIT; - - base = cl_git_sandbox_init("attr"); - cl_git_pass(git_buf_sets(&ceiling, git_repository_workdir(base))); - - /* fail with no searching */ - cl_git_fail(git_repository_open(&repo, "attr/sub")); - cl_git_fail(git_repository_open_ext( - &repo, "attr/sub", GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)); - - /* fail with ceiling too low */ - cl_git_pass(git_buf_joinpath(&ceiling, ceiling.ptr, "sub")); - cl_git_fail(git_repository_open_ext(&repo, "attr/sub", 0, ceiling.ptr)); - - /* fail with no repo */ - cl_git_pass(p_mkdir("alternate", 0777)); - cl_git_pass(p_mkdir("alternate/.git", 0777)); - cl_git_fail(git_repository_open_ext(&repo, "alternate", 0, NULL)); - cl_git_fail(git_repository_open_ext(&repo, "alternate/.git", 0, NULL)); - - git_buf_free(&ceiling); -} - -void test_repo_open__bad_gitlinks(void) -{ - git_repository *repo; - static const char *bad_links[] = { - "garbage\n", "gitdir", "gitdir:\n", "gitdir: foobar", - "gitdir: ../invalid", "gitdir: ../invalid2", - "gitdir: ../attr/.git with extra stuff", - NULL - }; - const char **scan; - - cl_git_sandbox_init("attr"); - - cl_git_pass(p_mkdir("alternate", 0777)); - cl_git_pass(p_mkdir("invalid", 0777)); - cl_git_pass(git_futils_mkdir_r("invalid2/.git", NULL, 0777)); - - for (scan = bad_links; *scan != NULL; scan++) { - cl_git_rewritefile("alternate/.git", *scan); - cl_git_fail(git_repository_open_ext(&repo, "alternate", 0, NULL)); - } - - git_futils_rmdir_r("invalid", NULL, GIT_RMDIR_REMOVE_FILES); - git_futils_rmdir_r("invalid2", NULL, GIT_RMDIR_REMOVE_FILES); -} - -#ifdef GIT_WIN32 -static void unposix_path(git_buf *path) -{ - char *src, *tgt; - - src = tgt = path->ptr; - - /* convert "/d/..." to "d:\..." */ - if (src[0] == '/' && isalpha(src[1]) && src[2] == '/') { - *tgt++ = src[1]; - *tgt++ = ':'; - *tgt++ = '\\'; - src += 3; - } - - while (*src) { - *tgt++ = (*src == '/') ? '\\' : *src; - src++; - } - - *tgt = '\0'; -} -#endif - -void test_repo_open__win32_path(void) -{ -#ifdef GIT_WIN32 - git_repository *repo = cl_git_sandbox_init("empty_standard_repo"), *repo2; - git_buf winpath = GIT_BUF_INIT; - static const char *repo_path = "empty_standard_repo/.git/"; - static const char *repo_wd = "empty_standard_repo/"; - - cl_assert(git__suffixcmp(git_repository_path(repo), repo_path) == 0); - cl_assert(git__suffixcmp(git_repository_workdir(repo), repo_wd) == 0); - - cl_git_pass(git_buf_sets(&winpath, git_repository_path(repo))); - unposix_path(&winpath); - cl_git_pass(git_repository_open(&repo2, winpath.ptr)); - cl_assert(git__suffixcmp(git_repository_path(repo2), repo_path) == 0); - cl_assert(git__suffixcmp(git_repository_workdir(repo2), repo_wd) == 0); - git_repository_free(repo2); - - cl_git_pass(git_buf_sets(&winpath, git_repository_path(repo))); - git_buf_truncate(&winpath, winpath.size - 1); /* remove trailing '/' */ - unposix_path(&winpath); - cl_git_pass(git_repository_open(&repo2, winpath.ptr)); - cl_assert(git__suffixcmp(git_repository_path(repo2), repo_path) == 0); - cl_assert(git__suffixcmp(git_repository_workdir(repo2), repo_wd) == 0); - git_repository_free(repo2); - - cl_git_pass(git_buf_sets(&winpath, git_repository_workdir(repo))); - unposix_path(&winpath); - cl_git_pass(git_repository_open(&repo2, winpath.ptr)); - cl_assert(git__suffixcmp(git_repository_path(repo2), repo_path) == 0); - cl_assert(git__suffixcmp(git_repository_workdir(repo2), repo_wd) == 0); - git_repository_free(repo2); - - cl_git_pass(git_buf_sets(&winpath, git_repository_workdir(repo))); - git_buf_truncate(&winpath, winpath.size - 1); /* remove trailing '/' */ - unposix_path(&winpath); - cl_git_pass(git_repository_open(&repo2, winpath.ptr)); - cl_assert(git__suffixcmp(git_repository_path(repo2), repo_path) == 0); - cl_assert(git__suffixcmp(git_repository_workdir(repo2), repo_wd) == 0); - git_repository_free(repo2); - - git_buf_free(&winpath); -#endif -} - -void test_repo_open__opening_a_non_existing_repository_returns_ENOTFOUND(void) -{ - git_repository *repo; - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_open(&repo, "i-do-not/exist")); -} diff --git a/tests-clar/repo/repo_helpers.c b/tests-clar/repo/repo_helpers.c deleted file mode 100644 index 74902e4393c..00000000000 --- a/tests-clar/repo/repo_helpers.c +++ /dev/null @@ -1,22 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" -#include "repo_helpers.h" -#include "posix.h" - -void make_head_orphaned(git_repository* repo, const char *target) -{ - git_reference *head; - - cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, target, 1)); - git_reference_free(head); -} - -void delete_head(git_repository* repo) -{ - git_buf head_path = GIT_BUF_INIT; - - cl_git_pass(git_buf_joinpath(&head_path, git_repository_path(repo), GIT_HEAD_FILE)); - cl_git_pass(p_unlink(git_buf_cstr(&head_path))); - - git_buf_free(&head_path); -} diff --git a/tests-clar/repo/repo_helpers.h b/tests-clar/repo/repo_helpers.h deleted file mode 100644 index 09b5cac84d1..00000000000 --- a/tests-clar/repo/repo_helpers.h +++ /dev/null @@ -1,6 +0,0 @@ -#include "common.h" - -#define NON_EXISTING_HEAD "refs/heads/hide/and/seek" - -extern void make_head_orphaned(git_repository* repo, const char *target); -extern void delete_head(git_repository* repo); diff --git a/tests-clar/repo/setters.c b/tests-clar/repo/setters.c deleted file mode 100644 index 7e482dee19f..00000000000 --- a/tests-clar/repo/setters.c +++ /dev/null @@ -1,105 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "posix.h" -#include "util.h" -#include "path.h" -#include "fileops.h" - -static git_repository *repo; - -void test_repo_setters__initialize(void) -{ - cl_fixture_sandbox("testrepo.git"); - cl_git_pass(git_repository_open(&repo, "testrepo.git")); - cl_must_pass(p_mkdir("new_workdir", 0777)); -} - -void test_repo_setters__cleanup(void) -{ - git_repository_free(repo); - repo = NULL; - - cl_fixture_cleanup("testrepo.git"); - cl_fixture_cleanup("new_workdir"); -} - -void test_repo_setters__setting_a_workdir_turns_a_bare_repository_into_a_standard_one(void) -{ - cl_assert(git_repository_is_bare(repo) == 1); - - cl_assert(git_repository_workdir(repo) == NULL); - cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false)); - - cl_assert(git_repository_workdir(repo) != NULL); - cl_assert(git_repository_is_bare(repo) == 0); -} - -void test_repo_setters__setting_a_workdir_prettifies_its_path(void) -{ - cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false)); - - cl_assert(git__suffixcmp(git_repository_workdir(repo), "new_workdir/") == 0); -} - -void test_repo_setters__setting_a_workdir_creates_a_gitlink(void) -{ - git_config *cfg; - const char *val; - git_buf content = GIT_BUF_INIT; - - cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", true)); - - cl_assert(git_path_isfile("./new_workdir/.git")); - - cl_git_pass(git_futils_readbuffer(&content, "./new_workdir/.git")); - cl_assert(git__prefixcmp(git_buf_cstr(&content), "gitdir: ") == 0); - cl_assert(git__suffixcmp(git_buf_cstr(&content), "testrepo.git/") == 0); - git_buf_free(&content); - - cl_git_pass(git_repository_config(&cfg, repo)); - cl_git_pass(git_config_get_string(&val, cfg, "core.worktree")); - cl_assert(git__suffixcmp(val, "new_workdir/") == 0); - git_config_free(cfg); -} - -void test_repo_setters__setting_a_new_index_on_a_repo_which_has_already_loaded_one_properly_honors_the_refcount(void) -{ - git_index *new_index; - - cl_git_pass(git_index_open(&new_index, "./my-index")); - cl_assert(((git_refcount *)new_index)->refcount == 1); - - git_repository_set_index(repo, new_index); - cl_assert(((git_refcount *)new_index)->refcount == 2); - - git_repository_free(repo); - cl_assert(((git_refcount *)new_index)->refcount == 1); - - git_index_free(new_index); - - /* - * Ensure the cleanup method won't try to free the repo as it's already been taken care of - */ - repo = NULL; -} - -void test_repo_setters__setting_a_new_odb_on_a_repo_which_already_loaded_one_properly_honors_the_refcount(void) -{ - git_odb *new_odb; - - cl_git_pass(git_odb_open(&new_odb, "./testrepo.git/objects")); - cl_assert(((git_refcount *)new_odb)->refcount == 1); - - git_repository_set_odb(repo, new_odb); - cl_assert(((git_refcount *)new_odb)->refcount == 2); - - git_repository_free(repo); - cl_assert(((git_refcount *)new_odb)->refcount == 1); - - git_odb_free(new_odb); - - /* - * Ensure the cleanup method won't try to free the repo as it's already been taken care of - */ - repo = NULL; -} diff --git a/tests-clar/repo/state.c b/tests-clar/repo/state.c deleted file mode 100644 index 5a0a5f36047..00000000000 --- a/tests-clar/repo/state.c +++ /dev/null @@ -1,96 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "refs.h" -#include "posix.h" -#include "fileops.h" - -static git_repository *_repo; -static git_buf _path; - -void test_repo_state__initialize(void) -{ - _repo = cl_git_sandbox_init("testrepo.git"); -} - -void test_repo_state__cleanup(void) -{ - cl_git_sandbox_cleanup(); - git_buf_free(&_path); -} - -static void setup_simple_state(const char *filename) -{ - cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), filename)); - git_futils_mkpath2file(git_buf_cstr(&_path), 0777); - cl_git_mkfile(git_buf_cstr(&_path), "dummy"); -} - -static void assert_repo_state(git_repository_state_t state) -{ - cl_assert_equal_i(state, git_repository_state(_repo)); -} - -void test_repo_state__none_with_HEAD_attached(void) -{ - assert_repo_state(GIT_REPOSITORY_STATE_NONE); -} - -void test_repo_state__none_with_HEAD_detached(void) -{ - cl_git_pass(git_repository_detach_head(_repo)); - assert_repo_state(GIT_REPOSITORY_STATE_NONE); -} - -void test_repo_state__merge(void) -{ - setup_simple_state(GIT_MERGE_HEAD_FILE); - assert_repo_state(GIT_REPOSITORY_STATE_MERGE); -} - -void test_repo_state__revert(void) -{ - setup_simple_state(GIT_REVERT_HEAD_FILE); - assert_repo_state(GIT_REPOSITORY_STATE_REVERT); -} - -void test_repo_state__cherry_pick(void) -{ - setup_simple_state(GIT_CHERRY_PICK_HEAD_FILE); - assert_repo_state(GIT_REPOSITORY_STATE_CHERRY_PICK); -} - -void test_repo_state__bisect(void) -{ - setup_simple_state(GIT_BISECT_LOG_FILE); - assert_repo_state(GIT_REPOSITORY_STATE_BISECT); -} - -void test_repo_state__rebase_interactive(void) -{ - setup_simple_state(GIT_REBASE_MERGE_INTERACTIVE_FILE); - assert_repo_state(GIT_REPOSITORY_STATE_REBASE_INTERACTIVE); -} - -void test_repo_state__rebase_merge(void) -{ - setup_simple_state(GIT_REBASE_MERGE_DIR "whatever"); - assert_repo_state(GIT_REPOSITORY_STATE_REBASE_MERGE); -} - -void test_repo_state__rebase(void) -{ - setup_simple_state(GIT_REBASE_APPLY_REBASING_FILE); - assert_repo_state(GIT_REPOSITORY_STATE_REBASE); -} - -void test_repo_state__apply_mailbox(void) -{ - setup_simple_state(GIT_REBASE_APPLY_APPLYING_FILE); - assert_repo_state(GIT_REPOSITORY_STATE_APPLY_MAILBOX); -} - -void test_repo_state__apply_mailbox_or_rebase(void) -{ - setup_simple_state(GIT_REBASE_APPLY_DIR "whatever"); - assert_repo_state(GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE); -} diff --git a/tests-clar/reset/hard.c b/tests-clar/reset/hard.c deleted file mode 100644 index 9bbce31ad80..00000000000 --- a/tests-clar/reset/hard.c +++ /dev/null @@ -1,136 +0,0 @@ -#include "clar_libgit2.h" -#include "posix.h" -#include "reset_helpers.h" -#include "path.h" -#include "fileops.h" - -static git_repository *repo; -static git_object *target; - -void test_reset_hard__initialize(void) -{ - repo = cl_git_sandbox_init("status"); - target = NULL; -} - -void test_reset_hard__cleanup(void) -{ - git_object_free(target); - target = NULL; - - cl_git_sandbox_cleanup(); -} - -static int strequal_ignore_eol(const char *exp, const char *str) -{ - while (*exp && *str) { - if (*exp != *str) { - while (*exp == '\r' || *exp == '\n') ++exp; - while (*str == '\r' || *str == '\n') ++str; - if (*exp != *str) - return false; - } else { - exp++; str++; - } - } - return (!*exp && !*str); -} - -void test_reset_hard__resetting_reverts_modified_files(void) -{ - git_buf path = GIT_BUF_INIT, content = GIT_BUF_INIT; - int i; - static const char *files[4] = { - "current_file", - "modified_file", - "staged_new_file", - "staged_changes_modified_file" }; - static const char *before[4] = { - "current_file\n", - "modified_file\nmodified_file\n", - "staged_new_file\n", - "staged_changes_modified_file\nstaged_changes_modified_file\nstaged_changes_modified_file\n" - }; - static const char *after[4] = { - "current_file\n", - "modified_file\n", - NULL, - "staged_changes_modified_file\n" - }; - const char *wd = git_repository_workdir(repo); - - cl_assert(wd); - - for (i = 0; i < 4; ++i) { - cl_git_pass(git_buf_joinpath(&path, wd, files[i])); - cl_git_pass(git_futils_readbuffer(&content, path.ptr)); - cl_assert_equal_s(before[i], content.ptr); - } - - retrieve_target_from_oid( - &target, repo, "26a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f"); - - cl_git_pass(git_reset(repo, target, GIT_RESET_HARD)); - - for (i = 0; i < 4; ++i) { - cl_git_pass(git_buf_joinpath(&path, wd, files[i])); - if (after[i]) { - cl_git_pass(git_futils_readbuffer(&content, path.ptr)); - cl_assert(strequal_ignore_eol(after[i], content.ptr)); - } else { - cl_assert(!git_path_exists(path.ptr)); - } - } - - git_buf_free(&content); - git_buf_free(&path); -} - -void test_reset_hard__cannot_reset_in_a_bare_repository(void) -{ - git_repository *bare; - - cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git"))); - cl_assert(git_repository_is_bare(bare) == true); - - retrieve_target_from_oid(&target, bare, KNOWN_COMMIT_IN_BARE_REPO); - - cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_HARD)); - - git_repository_free(bare); -} - -void test_reset_hard__cleans_up_merge(void) -{ - git_buf merge_head_path = GIT_BUF_INIT, - merge_msg_path = GIT_BUF_INIT, - merge_mode_path = GIT_BUF_INIT, - orig_head_path = GIT_BUF_INIT; - - cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD")); - cl_git_mkfile(git_buf_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n"); - - cl_git_pass(git_buf_joinpath(&merge_msg_path, git_repository_path(repo), "MERGE_MSG")); - cl_git_mkfile(git_buf_cstr(&merge_msg_path), "Merge commit 0017bd4ab1ec30440b17bae1680cff124ab5f1f6\n"); - - cl_git_pass(git_buf_joinpath(&merge_mode_path, git_repository_path(repo), "MERGE_MODE")); - cl_git_mkfile(git_buf_cstr(&merge_mode_path), ""); - - cl_git_pass(git_buf_joinpath(&orig_head_path, git_repository_path(repo), "ORIG_HEAD")); - cl_git_mkfile(git_buf_cstr(&orig_head_path), "0017bd4ab1ec30440b17bae1680cff124ab5f1f6"); - - retrieve_target_from_oid(&target, repo, "0017bd4ab1ec30440b17bae1680cff124ab5f1f6"); - cl_git_pass(git_reset(repo, target, GIT_RESET_HARD)); - - cl_assert(!git_path_exists(git_buf_cstr(&merge_head_path))); - cl_assert(!git_path_exists(git_buf_cstr(&merge_msg_path))); - cl_assert(!git_path_exists(git_buf_cstr(&merge_mode_path))); - - cl_assert(git_path_exists(git_buf_cstr(&orig_head_path))); - cl_git_pass(p_unlink(git_buf_cstr(&orig_head_path))); - - git_buf_free(&merge_head_path); - git_buf_free(&merge_msg_path); - git_buf_free(&merge_mode_path); - git_buf_free(&orig_head_path); -} diff --git a/tests-clar/reset/mixed.c b/tests-clar/reset/mixed.c deleted file mode 100644 index 7b90c23f1e5..00000000000 --- a/tests-clar/reset/mixed.c +++ /dev/null @@ -1,49 +0,0 @@ -#include "clar_libgit2.h" -#include "posix.h" -#include "reset_helpers.h" -#include "path.h" - -static git_repository *repo; -static git_object *target; - -void test_reset_mixed__initialize(void) -{ - repo = cl_git_sandbox_init("attr"); - target = NULL; -} - -void test_reset_mixed__cleanup(void) -{ - git_object_free(target); - target = NULL; - - cl_git_sandbox_cleanup(); -} - -void test_reset_mixed__cannot_reset_in_a_bare_repository(void) -{ - git_repository *bare; - - cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git"))); - cl_assert(git_repository_is_bare(bare) == true); - - retrieve_target_from_oid(&target, bare, KNOWN_COMMIT_IN_BARE_REPO); - - cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_MIXED)); - - git_repository_free(bare); -} - -void test_reset_mixed__resetting_refreshes_the_index_to_the_commit_tree(void) -{ - unsigned int status; - - cl_git_pass(git_status_file(&status, repo, "macro_bad")); - cl_assert(status == GIT_STATUS_CURRENT); - retrieve_target_from_oid(&target, repo, "605812ab7fe421fdd325a935d35cb06a9234a7d7"); - - cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED)); - - cl_git_pass(git_status_file(&status, repo, "macro_bad")); - cl_assert(status == GIT_STATUS_WT_NEW); -} diff --git a/tests-clar/reset/reset_helpers.c b/tests-clar/reset/reset_helpers.c deleted file mode 100644 index 17edca4e9cd..00000000000 --- a/tests-clar/reset/reset_helpers.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "clar_libgit2.h" -#include "reset_helpers.h" - -void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha) -{ - git_oid oid; - - cl_git_pass(git_oid_fromstr(&oid, sha)); - cl_git_pass(git_object_lookup(object_out, repo, &oid, GIT_OBJ_ANY)); -} diff --git a/tests-clar/reset/reset_helpers.h b/tests-clar/reset/reset_helpers.h deleted file mode 100644 index 5dbe9d2c79a..00000000000 --- a/tests-clar/reset/reset_helpers.h +++ /dev/null @@ -1,6 +0,0 @@ -#include "common.h" - -#define KNOWN_COMMIT_IN_BARE_REPO "e90810b8df3e80c413d903f631643c716887138d" -#define KNOWN_COMMIT_IN_ATTR_REPO "217878ab49e1314388ea2e32dc6fdb58a1b969e0" - -extern void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha); diff --git a/tests-clar/reset/soft.c b/tests-clar/reset/soft.c deleted file mode 100644 index 884697c91fd..00000000000 --- a/tests-clar/reset/soft.c +++ /dev/null @@ -1,157 +0,0 @@ -#include "clar_libgit2.h" -#include "posix.h" -#include "reset_helpers.h" -#include "path.h" -#include "repo/repo_helpers.h" - -static git_repository *repo; -static git_object *target; - -void test_reset_soft__initialize(void) -{ - repo = cl_git_sandbox_init("testrepo.git"); -} - -void test_reset_soft__cleanup(void) -{ - git_object_free(target); - target = NULL; - - cl_git_sandbox_cleanup(); -} - -static void assert_reset_soft(bool should_be_detached) -{ - git_oid oid; - - cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); - cl_git_fail(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); - - retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO); - - cl_assert(git_repository_head_detached(repo) == should_be_detached); - - cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); - - cl_assert(git_repository_head_detached(repo) == should_be_detached); - - cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); - cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); -} - -void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(void) -{ - assert_reset_soft(false); -} - -void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void) -{ - git_repository_detach_head(repo); - - assert_reset_soft(true); -} - -void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_change_the_target_of_the_Head(void) -{ - git_oid oid; - char raw_head_oid[GIT_OID_HEXSZ + 1]; - - cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); - git_oid_fmt(raw_head_oid, &oid); - raw_head_oid[GIT_OID_HEXSZ] = '\0'; - - retrieve_target_from_oid(&target, repo, raw_head_oid); - - cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); - - cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); - cl_git_pass(git_oid_streq(&oid, raw_head_oid)); -} - -void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void) -{ - git_oid oid; - - /* b25fa35 is a tag, pointing to another tag which points to commit e90810b */ - retrieve_target_from_oid(&target, repo, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); - - cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); - - cl_assert(git_repository_head_detached(repo) == false); - cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); - cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); -} - -void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void) -{ - /* 53fc32d is the tree of commit e90810b */ - retrieve_target_from_oid(&target, repo, "53fc32d17276939fc79ed05badaef2db09990016"); - - cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT)); - git_object_free(target); - - /* 521d87c is an annotated tag pointing to a blob */ - retrieve_target_from_oid(&target, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91"); - cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT)); -} - -void test_reset_soft__resetting_against_an_orphaned_head_repo_makes_the_head_no_longer_orphaned(void) -{ - git_reference *head; - - retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO); - - make_head_orphaned(repo, NON_EXISTING_HEAD); - - cl_assert_equal_i(true, git_repository_head_orphan(repo)); - - cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); - - cl_assert_equal_i(false, git_repository_head_orphan(repo)); - - cl_git_pass(git_reference_lookup(&head, repo, NON_EXISTING_HEAD)); - cl_assert_equal_i(0, git_oid_streq(git_reference_target(head), KNOWN_COMMIT_IN_BARE_REPO)); - - git_reference_free(head); -} - -void test_reset_soft__fails_when_merging(void) -{ - git_buf merge_head_path = GIT_BUF_INIT; - - cl_git_pass(git_repository_detach_head(repo)); - cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD")); - cl_git_mkfile(git_buf_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n"); - - retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO); - - cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT)); - cl_git_pass(p_unlink(git_buf_cstr(&merge_head_path))); - - git_buf_free(&merge_head_path); -} - -void test_reset_soft__fails_when_index_contains_conflicts_independently_of_MERGE_HEAD_file_existence(void) -{ - git_index *index; - git_reference *head; - git_buf merge_head_path = GIT_BUF_INIT; - - cl_git_sandbox_cleanup(); - - repo = cl_git_sandbox_init("mergedrepo"); - - cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD")); - cl_git_pass(p_unlink(git_buf_cstr(&merge_head_path))); - git_buf_free(&merge_head_path); - - cl_git_pass(git_repository_index(&index, repo)); - cl_assert_equal_i(true, git_index_has_conflicts(index)); - git_index_free(index); - - cl_git_pass(git_repository_head(&head, repo)); - cl_git_pass(git_reference_peel(&target, head, GIT_OBJ_COMMIT)); - git_reference_free(head); - - cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT)); -} diff --git a/tests-clar/resources/.gitattributes b/tests-clar/resources/.gitattributes deleted file mode 100644 index 556f8c827b8..00000000000 --- a/tests-clar/resources/.gitattributes +++ /dev/null @@ -1 +0,0 @@ -* binary diff --git a/tests-clar/resources/config/config12 b/tests-clar/resources/config/config12 deleted file mode 100644 index b57a81b08a3..00000000000 --- a/tests-clar/resources/config/config12 +++ /dev/null @@ -1,7 +0,0 @@ -[some "section"] - test = hi ; comment - other = "hello! \" ; ; ; " ; more test - multi = "hi, this is a ; \ -multiline comment # with ;\n special chars \ -and other stuff !@#" - back = "this is \ba phrase" diff --git a/tests-clar/resources/crlf/.gitted/objects/12/faf3c1ea55f572473cec9052fca468c3584ccb b/tests-clar/resources/crlf/.gitted/objects/12/faf3c1ea55f572473cec9052fca468c3584ccb deleted file mode 100644 index 96d5b2f91d4..00000000000 --- a/tests-clar/resources/crlf/.gitted/objects/12/faf3c1ea55f572473cec9052fca468c3584ccb +++ /dev/null @@ -1 +0,0 @@ -x 1}Nۀ,b6K6`.ؾQoab-A0dXbtnr:0cy(*Y/dev/null 2>&1 -then - against=HEAD -else - # Initial commit: diff against an empty tree object - against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 -fi - -# If you want to allow non-ascii filenames set this variable to true. -allownonascii=$(git config hooks.allownonascii) - -# Redirect output to stderr. -exec 1>&2 - -# Cross platform projects tend to avoid non-ascii filenames; prevent -# them from being added to the repository. We exploit the fact that the -# printable range starts at the space character and ends with tilde. -if [ "$allownonascii" != "true" ] && - # Note that the use of brackets around a tr range is ok here, (it's - # even required, for portability to Solaris 10's /usr/bin/tr), since - # the square bracket bytes happen to fall in the designated range. - test $(git diff --cached --name-only --diff-filter=A -z $against | - LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 -then - echo "Error: Attempt to add a non-ascii file name." - echo - echo "This can cause problems if you want to work" - echo "with people on other platforms." - echo - echo "To be portable it is advisable to rename the file ..." - echo - echo "If you know what you are doing you can disable this" - echo "check using:" - echo - echo " git config hooks.allownonascii true" - echo - exit 1 -fi - -# If there are whitespace errors, print the offending file names and fail. -exec git diff-index --check --cached $against -- diff --git a/tests-clar/resources/duplicate.git/hooks/pre-rebase.sample b/tests-clar/resources/duplicate.git/hooks/pre-rebase.sample deleted file mode 100755 index 9773ed4cb29..00000000000 --- a/tests-clar/resources/duplicate.git/hooks/pre-rebase.sample +++ /dev/null @@ -1,169 +0,0 @@ -#!/bin/sh -# -# Copyright (c) 2006, 2008 Junio C Hamano -# -# The "pre-rebase" hook is run just before "git rebase" starts doing -# its job, and can prevent the command from running by exiting with -# non-zero status. -# -# The hook is called with the following parameters: -# -# $1 -- the upstream the series was forked from. -# $2 -- the branch being rebased (or empty when rebasing the current branch). -# -# This sample shows how to prevent topic branches that are already -# merged to 'next' branch from getting rebased, because allowing it -# would result in rebasing already published history. - -publish=next -basebranch="$1" -if test "$#" = 2 -then - topic="refs/heads/$2" -else - topic=`git symbolic-ref HEAD` || - exit 0 ;# we do not interrupt rebasing detached HEAD -fi - -case "$topic" in -refs/heads/??/*) - ;; -*) - exit 0 ;# we do not interrupt others. - ;; -esac - -# Now we are dealing with a topic branch being rebased -# on top of master. Is it OK to rebase it? - -# Does the topic really exist? -git show-ref -q "$topic" || { - echo >&2 "No such branch $topic" - exit 1 -} - -# Is topic fully merged to master? -not_in_master=`git rev-list --pretty=oneline ^master "$topic"` -if test -z "$not_in_master" -then - echo >&2 "$topic is fully merged to master; better remove it." - exit 1 ;# we could allow it, but there is no point. -fi - -# Is topic ever merged to next? If so you should not be rebasing it. -only_next_1=`git rev-list ^master "^$topic" ${publish} | sort` -only_next_2=`git rev-list ^master ${publish} | sort` -if test "$only_next_1" = "$only_next_2" -then - not_in_topic=`git rev-list "^$topic" master` - if test -z "$not_in_topic" - then - echo >&2 "$topic is already up-to-date with master" - exit 1 ;# we could allow it, but there is no point. - else - exit 0 - fi -else - not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"` - /usr/bin/perl -e ' - my $topic = $ARGV[0]; - my $msg = "* $topic has commits already merged to public branch:\n"; - my (%not_in_next) = map { - /^([0-9a-f]+) /; - ($1 => 1); - } split(/\n/, $ARGV[1]); - for my $elem (map { - /^([0-9a-f]+) (.*)$/; - [$1 => $2]; - } split(/\n/, $ARGV[2])) { - if (!exists $not_in_next{$elem->[0]}) { - if ($msg) { - print STDERR $msg; - undef $msg; - } - print STDERR " $elem->[1]\n"; - } - } - ' "$topic" "$not_in_next" "$not_in_master" - exit 1 -fi - -exit 0 - -################################################################ - -This sample hook safeguards topic branches that have been -published from being rewound. - -The workflow assumed here is: - - * Once a topic branch forks from "master", "master" is never - merged into it again (either directly or indirectly). - - * Once a topic branch is fully cooked and merged into "master", - it is deleted. If you need to build on top of it to correct - earlier mistakes, a new topic branch is created by forking at - the tip of the "master". This is not strictly necessary, but - it makes it easier to keep your history simple. - - * Whenever you need to test or publish your changes to topic - branches, merge them into "next" branch. - -The script, being an example, hardcodes the publish branch name -to be "next", but it is trivial to make it configurable via -$GIT_DIR/config mechanism. - -With this workflow, you would want to know: - -(1) ... if a topic branch has ever been merged to "next". Young - topic branches can have stupid mistakes you would rather - clean up before publishing, and things that have not been - merged into other branches can be easily rebased without - affecting other people. But once it is published, you would - not want to rewind it. - -(2) ... if a topic branch has been fully merged to "master". - Then you can delete it. More importantly, you should not - build on top of it -- other people may already want to - change things related to the topic as patches against your - "master", so if you need further changes, it is better to - fork the topic (perhaps with the same name) afresh from the - tip of "master". - -Let's look at this example: - - o---o---o---o---o---o---o---o---o---o "next" - / / / / - / a---a---b A / / - / / / / - / / c---c---c---c B / - / / / \ / - / / / b---b C \ / - / / / / \ / - ---o---o---o---o---o---o---o---o---o---o---o "master" - - -A, B and C are topic branches. - - * A has one fix since it was merged up to "next". - - * B has finished. It has been fully merged up to "master" and "next", - and is ready to be deleted. - - * C has not merged to "next" at all. - -We would want to allow C to be rebased, refuse A, and encourage -B to be deleted. - -To compute (1): - - git rev-list ^master ^topic next - git rev-list ^master next - - if these match, topic has not merged in next at all. - -To compute (2): - - git rev-list master..topic - - if this is empty, it is fully merged to "master". diff --git a/tests-clar/resources/duplicate.git/hooks/prepare-commit-msg.sample b/tests-clar/resources/duplicate.git/hooks/prepare-commit-msg.sample deleted file mode 100755 index f093a02ec49..00000000000 --- a/tests-clar/resources/duplicate.git/hooks/prepare-commit-msg.sample +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/sh -# -# An example hook script to prepare the commit log message. -# Called by "git commit" with the name of the file that has the -# commit message, followed by the description of the commit -# message's source. The hook's purpose is to edit the commit -# message file. If the hook fails with a non-zero status, -# the commit is aborted. -# -# To enable this hook, rename this file to "prepare-commit-msg". - -# This hook includes three examples. The first comments out the -# "Conflicts:" part of a merge commit. -# -# The second includes the output of "git diff --name-status -r" -# into the message, just before the "git status" output. It is -# commented because it doesn't cope with --amend or with squashed -# commits. -# -# The third example adds a Signed-off-by line to the message, that can -# still be edited. This is rarely a good idea. - -case "$2,$3" in - merge,) - /usr/bin/perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;; - -# ,|template,) -# /usr/bin/perl -i.bak -pe ' -# print "\n" . `git diff --cached --name-status -r` -# if /^#/ && $first++ == 0' "$1" ;; - - *) ;; -esac - -# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') -# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" diff --git a/tests-clar/resources/duplicate.git/hooks/update.sample b/tests-clar/resources/duplicate.git/hooks/update.sample deleted file mode 100755 index 71ab04edc09..00000000000 --- a/tests-clar/resources/duplicate.git/hooks/update.sample +++ /dev/null @@ -1,128 +0,0 @@ -#!/bin/sh -# -# An example hook script to blocks unannotated tags from entering. -# Called by "git receive-pack" with arguments: refname sha1-old sha1-new -# -# To enable this hook, rename this file to "update". -# -# Config -# ------ -# hooks.allowunannotated -# This boolean sets whether unannotated tags will be allowed into the -# repository. By default they won't be. -# hooks.allowdeletetag -# This boolean sets whether deleting tags will be allowed in the -# repository. By default they won't be. -# hooks.allowmodifytag -# This boolean sets whether a tag may be modified after creation. By default -# it won't be. -# hooks.allowdeletebranch -# This boolean sets whether deleting branches will be allowed in the -# repository. By default they won't be. -# hooks.denycreatebranch -# This boolean sets whether remotely creating branches will be denied -# in the repository. By default this is allowed. -# - -# --- Command line -refname="$1" -oldrev="$2" -newrev="$3" - -# --- Safety check -if [ -z "$GIT_DIR" ]; then - echo "Don't run this script from the command line." >&2 - echo " (if you want, you could supply GIT_DIR then run" >&2 - echo " $0 )" >&2 - exit 1 -fi - -if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then - echo "Usage: $0 " >&2 - exit 1 -fi - -# --- Config -allowunannotated=$(git config --bool hooks.allowunannotated) -allowdeletebranch=$(git config --bool hooks.allowdeletebranch) -denycreatebranch=$(git config --bool hooks.denycreatebranch) -allowdeletetag=$(git config --bool hooks.allowdeletetag) -allowmodifytag=$(git config --bool hooks.allowmodifytag) - -# check for no description -projectdesc=$(sed -e '1q' "$GIT_DIR/description") -case "$projectdesc" in -"Unnamed repository"* | "") - echo "*** Project description file hasn't been set" >&2 - exit 1 - ;; -esac - -# --- Check types -# if $newrev is 0000...0000, it's a commit to delete a ref. -zero="0000000000000000000000000000000000000000" -if [ "$newrev" = "$zero" ]; then - newrev_type=delete -else - newrev_type=$(git cat-file -t $newrev) -fi - -case "$refname","$newrev_type" in - refs/tags/*,commit) - # un-annotated tag - short_refname=${refname##refs/tags/} - if [ "$allowunannotated" != "true" ]; then - echo "*** The un-annotated tag, $short_refname, is not allowed in this repository" >&2 - echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2 - exit 1 - fi - ;; - refs/tags/*,delete) - # delete tag - if [ "$allowdeletetag" != "true" ]; then - echo "*** Deleting a tag is not allowed in this repository" >&2 - exit 1 - fi - ;; - refs/tags/*,tag) - # annotated tag - if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1 - then - echo "*** Tag '$refname' already exists." >&2 - echo "*** Modifying a tag is not allowed in this repository." >&2 - exit 1 - fi - ;; - refs/heads/*,commit) - # branch - if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then - echo "*** Creating a branch is not allowed in this repository" >&2 - exit 1 - fi - ;; - refs/heads/*,delete) - # delete branch - if [ "$allowdeletebranch" != "true" ]; then - echo "*** Deleting a branch is not allowed in this repository" >&2 - exit 1 - fi - ;; - refs/remotes/*,commit) - # tracking branch - ;; - refs/remotes/*,delete) - # delete tracking branch - if [ "$allowdeletebranch" != "true" ]; then - echo "*** Deleting a tracking branch is not allowed in this repository" >&2 - exit 1 - fi - ;; - *) - # Anything else (is there anything else?) - echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 - exit 1 - ;; -esac - -# --- Finished -exit 0 diff --git a/tests-clar/resources/duplicate.git/objects/info/packs b/tests-clar/resources/duplicate.git/objects/info/packs deleted file mode 100644 index 3696a7d36d8..00000000000 --- a/tests-clar/resources/duplicate.git/objects/info/packs +++ /dev/null @@ -1,2 +0,0 @@ -P pack-e87994ad581c9af946de0eb890175c08cd005f38.pack - diff --git a/tests-clar/resources/filemodes/.gitted/hooks/commit-msg.sample b/tests-clar/resources/filemodes/.gitted/hooks/commit-msg.sample deleted file mode 100755 index b58d1184a9d..00000000000 --- a/tests-clar/resources/filemodes/.gitted/hooks/commit-msg.sample +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message. -# Called by "git commit" with one argument, the name of the file -# that has the commit message. The hook should exit with non-zero -# status after issuing an appropriate message if it wants to stop the -# commit. The hook is allowed to edit the commit message file. -# -# To enable this hook, rename this file to "commit-msg". - -# Uncomment the below to add a Signed-off-by line to the message. -# Doing this in a hook is a bad idea in general, but the prepare-commit-msg -# hook is more suited to it. -# -# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') -# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" - -# This example catches duplicate Signed-off-by lines. - -test "" = "$(grep '^Signed-off-by: ' "$1" | - sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { - echo >&2 Duplicate Signed-off-by lines. - exit 1 -} diff --git a/tests-clar/resources/issue_592b/.gitted/hooks/post-update.sample b/tests-clar/resources/issue_592b/.gitted/hooks/post-update.sample deleted file mode 100755 index ec17ec1939b..00000000000 --- a/tests-clar/resources/issue_592b/.gitted/hooks/post-update.sample +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh -# -# An example hook script to prepare a packed repository for use over -# dumb transports. -# -# To enable this hook, rename this file to "post-update". - -exec git update-server-info diff --git a/tests-clar/resources/renames/.gitted/index b/tests-clar/resources/renames/.gitted/index deleted file mode 100644 index 1fc69fcbefd..00000000000 Binary files a/tests-clar/resources/renames/.gitted/index and /dev/null differ diff --git a/tests-clar/resources/renames/.gitted/logs/HEAD b/tests-clar/resources/renames/.gitted/logs/HEAD deleted file mode 100644 index 34222ed7de4..00000000000 --- a/tests-clar/resources/renames/.gitted/logs/HEAD +++ /dev/null @@ -1,2 +0,0 @@ -0000000000000000000000000000000000000000 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 Russell Belfer 1351024687 -0700 commit (initial): Initial commit -31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 2bc7f351d20b53f1c72c16c4b036e491c478c49a Russell Belfer 1351024817 -0700 commit: copy and rename with no change diff --git a/tests-clar/resources/renames/.gitted/logs/refs/heads/master b/tests-clar/resources/renames/.gitted/logs/refs/heads/master deleted file mode 100644 index 34222ed7de4..00000000000 --- a/tests-clar/resources/renames/.gitted/logs/refs/heads/master +++ /dev/null @@ -1,2 +0,0 @@ -0000000000000000000000000000000000000000 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 Russell Belfer 1351024687 -0700 commit (initial): Initial commit -31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 2bc7f351d20b53f1c72c16c4b036e491c478c49a Russell Belfer 1351024817 -0700 commit: copy and rename with no change diff --git a/tests-clar/resources/renames/.gitted/refs/heads/master b/tests-clar/resources/renames/.gitted/refs/heads/master deleted file mode 100644 index 049b1f5add4..00000000000 --- a/tests-clar/resources/renames/.gitted/refs/heads/master +++ /dev/null @@ -1 +0,0 @@ -2bc7f351d20b53f1c72c16c4b036e491c478c49a diff --git a/tests-clar/resources/renames/sevencities.txt b/tests-clar/resources/renames/sevencities.txt deleted file mode 100644 index 66311f5cfbe..00000000000 --- a/tests-clar/resources/renames/sevencities.txt +++ /dev/null @@ -1,49 +0,0 @@ -The Song of Seven Cities -======================== - -I WAS Lord of Cities very sumptuously builded. -Seven roaring Cities paid me tribute from afar. -Ivory their outposts were—the guardrooms of them gilded, -And garrisoned with Amazons invincible in war. - -All the world went softly when it walked before my Cities— -Neither King nor Army vexed my peoples at their toil, -Never horse nor chariot irked or overbore my Cities, -Never Mob nor Ruler questioned whence they drew their spoil. - -Banded, mailed and arrogant from sunrise unto sunset; -Singing while they sacked it, they possessed the land at large. -Yet when men would rob them, they resisted, they made onset -And pierced the smoke of battle with a thousand-sabred charge. - -So they warred and trafficked only yesterday, my Cities. -To-day there is no mark or mound of where my Cities stood. -For the River rose at midnight and it washed away my Cities. -They are evened with Atlantis and the towns before the Flood. - -Rain on rain-gorged channels raised the water-levels round them, -Freshet backed on freshet swelled and swept their world from sight, -Till the emboldened floods linked arms and, flashing forward, drowned them— -Drowned my Seven Cities and their peoples in one night! - -Low among the alders lie their derelict foundations, -The beams wherein they trusted and the plinths whereon they built— -My rulers and their treasure and their unborn populations, -Dead, destroyed, aborted, and defiled with mud and silt! - -The Daughters of the Palace whom they cherished in my Cities, -My silver-tongued Princesses, and the promise of their May— -Their bridegrooms of the June-tide—all have perished in my Cities, -With the harsh envenomed virgins that can neither love nor play. - -I was Lord of Cities—I will build anew my Cities, -Seven, set on rocks, above the wrath of any flood. -Nor will I rest from search till I have filled anew my Cities -With peoples undefeated of the dark, enduring blood. - -To the sound of trumpets shall their seed restore my Cities -Wealthy and well-weaponed, that once more may I behold -All the world go softly when it walks before my Cities, -And the horses and the chariots fleeing from them as of old! - - -- Rudyard Kipling diff --git a/tests-clar/resources/renames/sixserving.txt b/tests-clar/resources/renames/sixserving.txt deleted file mode 100644 index ad0a8e55a10..00000000000 --- a/tests-clar/resources/renames/sixserving.txt +++ /dev/null @@ -1,24 +0,0 @@ -I KEEP six honest serving-men - (They taught me all I knew); -Their names are What and Why and When - And How and Where and Who. -I send them over land and sea, - I send them east and west; -But after they have worked for me, - I give them all a rest. - -I let them rest from nine till five, - For I am busy then, -As well as breakfast, lunch, and tea, - For they are hungry men. -But different folk have different views; -I know a person small— -She keeps ten million serving-men, -Who get no rest at all! - -She sends'em abroad on her own affairs, - From the second she opens her eyes— -One million Hows, two million Wheres, -And seven million Whys! - - -- Rudyard Kipling diff --git a/tests-clar/resources/renames/songofseven.txt b/tests-clar/resources/renames/songofseven.txt deleted file mode 100644 index 66311f5cfbe..00000000000 --- a/tests-clar/resources/renames/songofseven.txt +++ /dev/null @@ -1,49 +0,0 @@ -The Song of Seven Cities -======================== - -I WAS Lord of Cities very sumptuously builded. -Seven roaring Cities paid me tribute from afar. -Ivory their outposts were—the guardrooms of them gilded, -And garrisoned with Amazons invincible in war. - -All the world went softly when it walked before my Cities— -Neither King nor Army vexed my peoples at their toil, -Never horse nor chariot irked or overbore my Cities, -Never Mob nor Ruler questioned whence they drew their spoil. - -Banded, mailed and arrogant from sunrise unto sunset; -Singing while they sacked it, they possessed the land at large. -Yet when men would rob them, they resisted, they made onset -And pierced the smoke of battle with a thousand-sabred charge. - -So they warred and trafficked only yesterday, my Cities. -To-day there is no mark or mound of where my Cities stood. -For the River rose at midnight and it washed away my Cities. -They are evened with Atlantis and the towns before the Flood. - -Rain on rain-gorged channels raised the water-levels round them, -Freshet backed on freshet swelled and swept their world from sight, -Till the emboldened floods linked arms and, flashing forward, drowned them— -Drowned my Seven Cities and their peoples in one night! - -Low among the alders lie their derelict foundations, -The beams wherein they trusted and the plinths whereon they built— -My rulers and their treasure and their unborn populations, -Dead, destroyed, aborted, and defiled with mud and silt! - -The Daughters of the Palace whom they cherished in my Cities, -My silver-tongued Princesses, and the promise of their May— -Their bridegrooms of the June-tide—all have perished in my Cities, -With the harsh envenomed virgins that can neither love nor play. - -I was Lord of Cities—I will build anew my Cities, -Seven, set on rocks, above the wrath of any flood. -Nor will I rest from search till I have filled anew my Cities -With peoples undefeated of the dark, enduring blood. - -To the sound of trumpets shall their seed restore my Cities -Wealthy and well-weaponed, that once more may I behold -All the world go softly when it walks before my Cities, -And the horses and the chariots fleeing from them as of old! - - -- Rudyard Kipling diff --git a/tests-clar/resources/submod2/.gitted/hooks/applypatch-msg.sample b/tests-clar/resources/submod2/.gitted/hooks/applypatch-msg.sample deleted file mode 100755 index 8b2a2fe84fe..00000000000 --- a/tests-clar/resources/submod2/.gitted/hooks/applypatch-msg.sample +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message taken by -# applypatch from an e-mail message. -# -# The hook should exit with non-zero status after issuing an -# appropriate message if it wants to stop the commit. The hook is -# allowed to edit the commit message file. -# -# To enable this hook, rename this file to "applypatch-msg". - -. git-sh-setup -test -x "$GIT_DIR/hooks/commit-msg" && - exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"} -: diff --git a/tests-clar/resources/submod2/.gitted/modules/sm_added_and_uncommited/hooks/applypatch-msg.sample b/tests-clar/resources/submod2/.gitted/modules/sm_added_and_uncommited/hooks/applypatch-msg.sample deleted file mode 100755 index 8b2a2fe84fe..00000000000 --- a/tests-clar/resources/submod2/.gitted/modules/sm_added_and_uncommited/hooks/applypatch-msg.sample +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message taken by -# applypatch from an e-mail message. -# -# The hook should exit with non-zero status after issuing an -# appropriate message if it wants to stop the commit. The hook is -# allowed to edit the commit message file. -# -# To enable this hook, rename this file to "applypatch-msg". - -. git-sh-setup -test -x "$GIT_DIR/hooks/commit-msg" && - exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"} -: diff --git a/tests-clar/resources/submod2/.gitted/modules/sm_changed_file/hooks/applypatch-msg.sample b/tests-clar/resources/submod2/.gitted/modules/sm_changed_file/hooks/applypatch-msg.sample deleted file mode 100755 index 8b2a2fe84fe..00000000000 --- a/tests-clar/resources/submod2/.gitted/modules/sm_changed_file/hooks/applypatch-msg.sample +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message taken by -# applypatch from an e-mail message. -# -# The hook should exit with non-zero status after issuing an -# appropriate message if it wants to stop the commit. The hook is -# allowed to edit the commit message file. -# -# To enable this hook, rename this file to "applypatch-msg". - -. git-sh-setup -test -x "$GIT_DIR/hooks/commit-msg" && - exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"} -: diff --git a/tests-clar/resources/submod2/.gitted/modules/sm_changed_head/hooks/applypatch-msg.sample b/tests-clar/resources/submod2/.gitted/modules/sm_changed_head/hooks/applypatch-msg.sample deleted file mode 100755 index 8b2a2fe84fe..00000000000 --- a/tests-clar/resources/submod2/.gitted/modules/sm_changed_head/hooks/applypatch-msg.sample +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message taken by -# applypatch from an e-mail message. -# -# The hook should exit with non-zero status after issuing an -# appropriate message if it wants to stop the commit. The hook is -# allowed to edit the commit message file. -# -# To enable this hook, rename this file to "applypatch-msg". - -. git-sh-setup -test -x "$GIT_DIR/hooks/commit-msg" && - exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"} -: diff --git a/tests-clar/resources/submod2/.gitted/modules/sm_changed_index/hooks/applypatch-msg.sample b/tests-clar/resources/submod2/.gitted/modules/sm_changed_index/hooks/applypatch-msg.sample deleted file mode 100755 index 8b2a2fe84fe..00000000000 --- a/tests-clar/resources/submod2/.gitted/modules/sm_changed_index/hooks/applypatch-msg.sample +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message taken by -# applypatch from an e-mail message. -# -# The hook should exit with non-zero status after issuing an -# appropriate message if it wants to stop the commit. The hook is -# allowed to edit the commit message file. -# -# To enable this hook, rename this file to "applypatch-msg". - -. git-sh-setup -test -x "$GIT_DIR/hooks/commit-msg" && - exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"} -: diff --git a/tests-clar/resources/submod2/.gitted/modules/sm_changed_untracked_file/hooks/applypatch-msg.sample b/tests-clar/resources/submod2/.gitted/modules/sm_changed_untracked_file/hooks/applypatch-msg.sample deleted file mode 100755 index 8b2a2fe84fe..00000000000 --- a/tests-clar/resources/submod2/.gitted/modules/sm_changed_untracked_file/hooks/applypatch-msg.sample +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message taken by -# applypatch from an e-mail message. -# -# The hook should exit with non-zero status after issuing an -# appropriate message if it wants to stop the commit. The hook is -# allowed to edit the commit message file. -# -# To enable this hook, rename this file to "applypatch-msg". - -. git-sh-setup -test -x "$GIT_DIR/hooks/commit-msg" && - exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"} -: diff --git a/tests-clar/resources/submod2/.gitted/modules/sm_missing_commits/hooks/applypatch-msg.sample b/tests-clar/resources/submod2/.gitted/modules/sm_missing_commits/hooks/applypatch-msg.sample deleted file mode 100755 index 8b2a2fe84fe..00000000000 --- a/tests-clar/resources/submod2/.gitted/modules/sm_missing_commits/hooks/applypatch-msg.sample +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message taken by -# applypatch from an e-mail message. -# -# The hook should exit with non-zero status after issuing an -# appropriate message if it wants to stop the commit. The hook is -# allowed to edit the commit message file. -# -# To enable this hook, rename this file to "applypatch-msg". - -. git-sh-setup -test -x "$GIT_DIR/hooks/commit-msg" && - exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"} -: diff --git a/tests-clar/resources/submod2/.gitted/modules/sm_unchanged/hooks/applypatch-msg.sample b/tests-clar/resources/submod2/.gitted/modules/sm_unchanged/hooks/applypatch-msg.sample deleted file mode 100755 index 8b2a2fe84fe..00000000000 --- a/tests-clar/resources/submod2/.gitted/modules/sm_unchanged/hooks/applypatch-msg.sample +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message taken by -# applypatch from an e-mail message. -# -# The hook should exit with non-zero status after issuing an -# appropriate message if it wants to stop the commit. The hook is -# allowed to edit the commit message file. -# -# To enable this hook, rename this file to "applypatch-msg". - -. git-sh-setup -test -x "$GIT_DIR/hooks/commit-msg" && - exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"} -: diff --git a/tests-clar/resources/submod2/not_submodule/.gitted/COMMIT_EDITMSG b/tests-clar/resources/submod2/not_submodule/.gitted/COMMIT_EDITMSG deleted file mode 100644 index 5852f44639f..00000000000 --- a/tests-clar/resources/submod2/not_submodule/.gitted/COMMIT_EDITMSG +++ /dev/null @@ -1 +0,0 @@ -Initial commit diff --git a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/applypatch-msg.sample b/tests-clar/resources/submod2/not_submodule/.gitted/hooks/applypatch-msg.sample deleted file mode 100755 index 8b2a2fe84fe..00000000000 --- a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/applypatch-msg.sample +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message taken by -# applypatch from an e-mail message. -# -# The hook should exit with non-zero status after issuing an -# appropriate message if it wants to stop the commit. The hook is -# allowed to edit the commit message file. -# -# To enable this hook, rename this file to "applypatch-msg". - -. git-sh-setup -test -x "$GIT_DIR/hooks/commit-msg" && - exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"} -: diff --git a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/commit-msg.sample b/tests-clar/resources/submod2/not_submodule/.gitted/hooks/commit-msg.sample deleted file mode 100755 index b58d1184a9d..00000000000 --- a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/commit-msg.sample +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message. -# Called by "git commit" with one argument, the name of the file -# that has the commit message. The hook should exit with non-zero -# status after issuing an appropriate message if it wants to stop the -# commit. The hook is allowed to edit the commit message file. -# -# To enable this hook, rename this file to "commit-msg". - -# Uncomment the below to add a Signed-off-by line to the message. -# Doing this in a hook is a bad idea in general, but the prepare-commit-msg -# hook is more suited to it. -# -# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') -# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" - -# This example catches duplicate Signed-off-by lines. - -test "" = "$(grep '^Signed-off-by: ' "$1" | - sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { - echo >&2 Duplicate Signed-off-by lines. - exit 1 -} diff --git a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/post-update.sample b/tests-clar/resources/submod2/not_submodule/.gitted/hooks/post-update.sample deleted file mode 100755 index ec17ec1939b..00000000000 --- a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/post-update.sample +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh -# -# An example hook script to prepare a packed repository for use over -# dumb transports. -# -# To enable this hook, rename this file to "post-update". - -exec git update-server-info diff --git a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/pre-applypatch.sample b/tests-clar/resources/submod2/not_submodule/.gitted/hooks/pre-applypatch.sample deleted file mode 100755 index b1f187c2e9a..00000000000 --- a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/pre-applypatch.sample +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh -# -# An example hook script to verify what is about to be committed -# by applypatch from an e-mail message. -# -# The hook should exit with non-zero status after issuing an -# appropriate message if it wants to stop the commit. -# -# To enable this hook, rename this file to "pre-applypatch". - -. git-sh-setup -test -x "$GIT_DIR/hooks/pre-commit" && - exec "$GIT_DIR/hooks/pre-commit" ${1+"$@"} -: diff --git a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/pre-commit.sample b/tests-clar/resources/submod2/not_submodule/.gitted/hooks/pre-commit.sample deleted file mode 100755 index 18c48297652..00000000000 --- a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/pre-commit.sample +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/sh -# -# An example hook script to verify what is about to be committed. -# Called by "git commit" with no arguments. The hook should -# exit with non-zero status after issuing an appropriate message if -# it wants to stop the commit. -# -# To enable this hook, rename this file to "pre-commit". - -if git rev-parse --verify HEAD >/dev/null 2>&1 -then - against=HEAD -else - # Initial commit: diff against an empty tree object - against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 -fi - -# If you want to allow non-ascii filenames set this variable to true. -allownonascii=$(git config hooks.allownonascii) - -# Redirect output to stderr. -exec 1>&2 - -# Cross platform projects tend to avoid non-ascii filenames; prevent -# them from being added to the repository. We exploit the fact that the -# printable range starts at the space character and ends with tilde. -if [ "$allownonascii" != "true" ] && - # Note that the use of brackets around a tr range is ok here, (it's - # even required, for portability to Solaris 10's /usr/bin/tr), since - # the square bracket bytes happen to fall in the designated range. - test $(git diff --cached --name-only --diff-filter=A -z $against | - LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 -then - echo "Error: Attempt to add a non-ascii file name." - echo - echo "This can cause problems if you want to work" - echo "with people on other platforms." - echo - echo "To be portable it is advisable to rename the file ..." - echo - echo "If you know what you are doing you can disable this" - echo "check using:" - echo - echo " git config hooks.allownonascii true" - echo - exit 1 -fi - -# If there are whitespace errors, print the offending file names and fail. -exec git diff-index --check --cached $against -- diff --git a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/pre-rebase.sample b/tests-clar/resources/submod2/not_submodule/.gitted/hooks/pre-rebase.sample deleted file mode 100755 index 9773ed4cb29..00000000000 --- a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/pre-rebase.sample +++ /dev/null @@ -1,169 +0,0 @@ -#!/bin/sh -# -# Copyright (c) 2006, 2008 Junio C Hamano -# -# The "pre-rebase" hook is run just before "git rebase" starts doing -# its job, and can prevent the command from running by exiting with -# non-zero status. -# -# The hook is called with the following parameters: -# -# $1 -- the upstream the series was forked from. -# $2 -- the branch being rebased (or empty when rebasing the current branch). -# -# This sample shows how to prevent topic branches that are already -# merged to 'next' branch from getting rebased, because allowing it -# would result in rebasing already published history. - -publish=next -basebranch="$1" -if test "$#" = 2 -then - topic="refs/heads/$2" -else - topic=`git symbolic-ref HEAD` || - exit 0 ;# we do not interrupt rebasing detached HEAD -fi - -case "$topic" in -refs/heads/??/*) - ;; -*) - exit 0 ;# we do not interrupt others. - ;; -esac - -# Now we are dealing with a topic branch being rebased -# on top of master. Is it OK to rebase it? - -# Does the topic really exist? -git show-ref -q "$topic" || { - echo >&2 "No such branch $topic" - exit 1 -} - -# Is topic fully merged to master? -not_in_master=`git rev-list --pretty=oneline ^master "$topic"` -if test -z "$not_in_master" -then - echo >&2 "$topic is fully merged to master; better remove it." - exit 1 ;# we could allow it, but there is no point. -fi - -# Is topic ever merged to next? If so you should not be rebasing it. -only_next_1=`git rev-list ^master "^$topic" ${publish} | sort` -only_next_2=`git rev-list ^master ${publish} | sort` -if test "$only_next_1" = "$only_next_2" -then - not_in_topic=`git rev-list "^$topic" master` - if test -z "$not_in_topic" - then - echo >&2 "$topic is already up-to-date with master" - exit 1 ;# we could allow it, but there is no point. - else - exit 0 - fi -else - not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"` - /usr/bin/perl -e ' - my $topic = $ARGV[0]; - my $msg = "* $topic has commits already merged to public branch:\n"; - my (%not_in_next) = map { - /^([0-9a-f]+) /; - ($1 => 1); - } split(/\n/, $ARGV[1]); - for my $elem (map { - /^([0-9a-f]+) (.*)$/; - [$1 => $2]; - } split(/\n/, $ARGV[2])) { - if (!exists $not_in_next{$elem->[0]}) { - if ($msg) { - print STDERR $msg; - undef $msg; - } - print STDERR " $elem->[1]\n"; - } - } - ' "$topic" "$not_in_next" "$not_in_master" - exit 1 -fi - -exit 0 - -################################################################ - -This sample hook safeguards topic branches that have been -published from being rewound. - -The workflow assumed here is: - - * Once a topic branch forks from "master", "master" is never - merged into it again (either directly or indirectly). - - * Once a topic branch is fully cooked and merged into "master", - it is deleted. If you need to build on top of it to correct - earlier mistakes, a new topic branch is created by forking at - the tip of the "master". This is not strictly necessary, but - it makes it easier to keep your history simple. - - * Whenever you need to test or publish your changes to topic - branches, merge them into "next" branch. - -The script, being an example, hardcodes the publish branch name -to be "next", but it is trivial to make it configurable via -$GIT_DIR/config mechanism. - -With this workflow, you would want to know: - -(1) ... if a topic branch has ever been merged to "next". Young - topic branches can have stupid mistakes you would rather - clean up before publishing, and things that have not been - merged into other branches can be easily rebased without - affecting other people. But once it is published, you would - not want to rewind it. - -(2) ... if a topic branch has been fully merged to "master". - Then you can delete it. More importantly, you should not - build on top of it -- other people may already want to - change things related to the topic as patches against your - "master", so if you need further changes, it is better to - fork the topic (perhaps with the same name) afresh from the - tip of "master". - -Let's look at this example: - - o---o---o---o---o---o---o---o---o---o "next" - / / / / - / a---a---b A / / - / / / / - / / c---c---c---c B / - / / / \ / - / / / b---b C \ / - / / / / \ / - ---o---o---o---o---o---o---o---o---o---o---o "master" - - -A, B and C are topic branches. - - * A has one fix since it was merged up to "next". - - * B has finished. It has been fully merged up to "master" and "next", - and is ready to be deleted. - - * C has not merged to "next" at all. - -We would want to allow C to be rebased, refuse A, and encourage -B to be deleted. - -To compute (1): - - git rev-list ^master ^topic next - git rev-list ^master next - - if these match, topic has not merged in next at all. - -To compute (2): - - git rev-list master..topic - - if this is empty, it is fully merged to "master". diff --git a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/prepare-commit-msg.sample b/tests-clar/resources/submod2/not_submodule/.gitted/hooks/prepare-commit-msg.sample deleted file mode 100755 index f093a02ec49..00000000000 --- a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/prepare-commit-msg.sample +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/sh -# -# An example hook script to prepare the commit log message. -# Called by "git commit" with the name of the file that has the -# commit message, followed by the description of the commit -# message's source. The hook's purpose is to edit the commit -# message file. If the hook fails with a non-zero status, -# the commit is aborted. -# -# To enable this hook, rename this file to "prepare-commit-msg". - -# This hook includes three examples. The first comments out the -# "Conflicts:" part of a merge commit. -# -# The second includes the output of "git diff --name-status -r" -# into the message, just before the "git status" output. It is -# commented because it doesn't cope with --amend or with squashed -# commits. -# -# The third example adds a Signed-off-by line to the message, that can -# still be edited. This is rarely a good idea. - -case "$2,$3" in - merge,) - /usr/bin/perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;; - -# ,|template,) -# /usr/bin/perl -i.bak -pe ' -# print "\n" . `git diff --cached --name-status -r` -# if /^#/ && $first++ == 0' "$1" ;; - - *) ;; -esac - -# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') -# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" diff --git a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/update.sample b/tests-clar/resources/submod2/not_submodule/.gitted/hooks/update.sample deleted file mode 100755 index 71ab04edc09..00000000000 --- a/tests-clar/resources/submod2/not_submodule/.gitted/hooks/update.sample +++ /dev/null @@ -1,128 +0,0 @@ -#!/bin/sh -# -# An example hook script to blocks unannotated tags from entering. -# Called by "git receive-pack" with arguments: refname sha1-old sha1-new -# -# To enable this hook, rename this file to "update". -# -# Config -# ------ -# hooks.allowunannotated -# This boolean sets whether unannotated tags will be allowed into the -# repository. By default they won't be. -# hooks.allowdeletetag -# This boolean sets whether deleting tags will be allowed in the -# repository. By default they won't be. -# hooks.allowmodifytag -# This boolean sets whether a tag may be modified after creation. By default -# it won't be. -# hooks.allowdeletebranch -# This boolean sets whether deleting branches will be allowed in the -# repository. By default they won't be. -# hooks.denycreatebranch -# This boolean sets whether remotely creating branches will be denied -# in the repository. By default this is allowed. -# - -# --- Command line -refname="$1" -oldrev="$2" -newrev="$3" - -# --- Safety check -if [ -z "$GIT_DIR" ]; then - echo "Don't run this script from the command line." >&2 - echo " (if you want, you could supply GIT_DIR then run" >&2 - echo " $0 )" >&2 - exit 1 -fi - -if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then - echo "Usage: $0 " >&2 - exit 1 -fi - -# --- Config -allowunannotated=$(git config --bool hooks.allowunannotated) -allowdeletebranch=$(git config --bool hooks.allowdeletebranch) -denycreatebranch=$(git config --bool hooks.denycreatebranch) -allowdeletetag=$(git config --bool hooks.allowdeletetag) -allowmodifytag=$(git config --bool hooks.allowmodifytag) - -# check for no description -projectdesc=$(sed -e '1q' "$GIT_DIR/description") -case "$projectdesc" in -"Unnamed repository"* | "") - echo "*** Project description file hasn't been set" >&2 - exit 1 - ;; -esac - -# --- Check types -# if $newrev is 0000...0000, it's a commit to delete a ref. -zero="0000000000000000000000000000000000000000" -if [ "$newrev" = "$zero" ]; then - newrev_type=delete -else - newrev_type=$(git cat-file -t $newrev) -fi - -case "$refname","$newrev_type" in - refs/tags/*,commit) - # un-annotated tag - short_refname=${refname##refs/tags/} - if [ "$allowunannotated" != "true" ]; then - echo "*** The un-annotated tag, $short_refname, is not allowed in this repository" >&2 - echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2 - exit 1 - fi - ;; - refs/tags/*,delete) - # delete tag - if [ "$allowdeletetag" != "true" ]; then - echo "*** Deleting a tag is not allowed in this repository" >&2 - exit 1 - fi - ;; - refs/tags/*,tag) - # annotated tag - if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1 - then - echo "*** Tag '$refname' already exists." >&2 - echo "*** Modifying a tag is not allowed in this repository." >&2 - exit 1 - fi - ;; - refs/heads/*,commit) - # branch - if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then - echo "*** Creating a branch is not allowed in this repository" >&2 - exit 1 - fi - ;; - refs/heads/*,delete) - # delete branch - if [ "$allowdeletebranch" != "true" ]; then - echo "*** Deleting a branch is not allowed in this repository" >&2 - exit 1 - fi - ;; - refs/remotes/*,commit) - # tracking branch - ;; - refs/remotes/*,delete) - # delete tracking branch - if [ "$allowdeletebranch" != "true" ]; then - echo "*** Deleting a tracking branch is not allowed in this repository" >&2 - exit 1 - fi - ;; - *) - # Anything else (is there anything else?) - echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 - exit 1 - ;; -esac - -# --- Finished -exit 0 diff --git a/tests-clar/resources/submod2_target/.gitted/hooks/applypatch-msg.sample b/tests-clar/resources/submod2_target/.gitted/hooks/applypatch-msg.sample deleted file mode 100755 index 8b2a2fe84fe..00000000000 --- a/tests-clar/resources/submod2_target/.gitted/hooks/applypatch-msg.sample +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message taken by -# applypatch from an e-mail message. -# -# The hook should exit with non-zero status after issuing an -# appropriate message if it wants to stop the commit. The hook is -# allowed to edit the commit message file. -# -# To enable this hook, rename this file to "applypatch-msg". - -. git-sh-setup -test -x "$GIT_DIR/hooks/commit-msg" && - exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"} -: diff --git a/tests-clar/resources/submodules/gitmodules b/tests-clar/resources/submodules/gitmodules deleted file mode 100644 index 1262f8bb044..00000000000 --- a/tests-clar/resources/submodules/gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "testrepo"] - path = testrepo - url = \ No newline at end of file diff --git a/tests-clar/resources/submodules/testrepo/.gitted/config b/tests-clar/resources/submodules/testrepo/.gitted/config deleted file mode 100644 index d6dcad12b3e..00000000000 --- a/tests-clar/resources/submodules/testrepo/.gitted/config +++ /dev/null @@ -1,12 +0,0 @@ -[core] - repositoryformatversion = 0 - filemode = true - bare = false - logallrefupdates = true - ignorecase = true -[remote "origin"] - fetch = +refs/heads/*:refs/remotes/origin/* - url = /Users/rb/src/libgit2/tests/resources/testrepo.git -[branch "master"] - remote = origin - merge = refs/heads/master diff --git a/tests-clar/resources/template/hooks/applypatch-msg.sample b/tests-clar/resources/template/hooks/applypatch-msg.sample deleted file mode 100755 index 8b2a2fe84fe..00000000000 --- a/tests-clar/resources/template/hooks/applypatch-msg.sample +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message taken by -# applypatch from an e-mail message. -# -# The hook should exit with non-zero status after issuing an -# appropriate message if it wants to stop the commit. The hook is -# allowed to edit the commit message file. -# -# To enable this hook, rename this file to "applypatch-msg". - -. git-sh-setup -test -x "$GIT_DIR/hooks/commit-msg" && - exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"} -: diff --git a/tests-clar/resources/template/hooks/commit-msg.sample b/tests-clar/resources/template/hooks/commit-msg.sample deleted file mode 100755 index b58d1184a9d..00000000000 --- a/tests-clar/resources/template/hooks/commit-msg.sample +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh -# -# An example hook script to check the commit log message. -# Called by "git commit" with one argument, the name of the file -# that has the commit message. The hook should exit with non-zero -# status after issuing an appropriate message if it wants to stop the -# commit. The hook is allowed to edit the commit message file. -# -# To enable this hook, rename this file to "commit-msg". - -# Uncomment the below to add a Signed-off-by line to the message. -# Doing this in a hook is a bad idea in general, but the prepare-commit-msg -# hook is more suited to it. -# -# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') -# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" - -# This example catches duplicate Signed-off-by lines. - -test "" = "$(grep '^Signed-off-by: ' "$1" | - sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { - echo >&2 Duplicate Signed-off-by lines. - exit 1 -} diff --git a/tests-clar/resources/template/hooks/post-commit.sample b/tests-clar/resources/template/hooks/post-commit.sample deleted file mode 100755 index 22668216a3c..00000000000 --- a/tests-clar/resources/template/hooks/post-commit.sample +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh -# -# An example hook script that is called after a successful -# commit is made. -# -# To enable this hook, rename this file to "post-commit". - -: Nothing diff --git a/tests-clar/resources/template/hooks/post-receive.sample b/tests-clar/resources/template/hooks/post-receive.sample deleted file mode 100755 index 7a83e17ab5f..00000000000 --- a/tests-clar/resources/template/hooks/post-receive.sample +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# -# An example hook script for the "post-receive" event. -# -# The "post-receive" script is run after receive-pack has accepted a pack -# and the repository has been updated. It is passed arguments in through -# stdin in the form -# -# For example: -# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master -# -# see contrib/hooks/ for a sample, or uncomment the next line and -# rename the file to "post-receive". - -#. /usr/share/doc/git-core/contrib/hooks/post-receive-email diff --git a/tests-clar/resources/template/hooks/post-update.sample b/tests-clar/resources/template/hooks/post-update.sample deleted file mode 100755 index ec17ec1939b..00000000000 --- a/tests-clar/resources/template/hooks/post-update.sample +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh -# -# An example hook script to prepare a packed repository for use over -# dumb transports. -# -# To enable this hook, rename this file to "post-update". - -exec git update-server-info diff --git a/tests-clar/resources/template/hooks/pre-applypatch.sample b/tests-clar/resources/template/hooks/pre-applypatch.sample deleted file mode 100755 index b1f187c2e9a..00000000000 --- a/tests-clar/resources/template/hooks/pre-applypatch.sample +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh -# -# An example hook script to verify what is about to be committed -# by applypatch from an e-mail message. -# -# The hook should exit with non-zero status after issuing an -# appropriate message if it wants to stop the commit. -# -# To enable this hook, rename this file to "pre-applypatch". - -. git-sh-setup -test -x "$GIT_DIR/hooks/pre-commit" && - exec "$GIT_DIR/hooks/pre-commit" ${1+"$@"} -: diff --git a/tests-clar/resources/template/hooks/pre-commit.sample b/tests-clar/resources/template/hooks/pre-commit.sample deleted file mode 100755 index b187c4bb1f2..00000000000 --- a/tests-clar/resources/template/hooks/pre-commit.sample +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/sh -# -# An example hook script to verify what is about to be committed. -# Called by "git commit" with no arguments. The hook should -# exit with non-zero status after issuing an appropriate message if -# it wants to stop the commit. -# -# To enable this hook, rename this file to "pre-commit". - -if git rev-parse --verify HEAD >/dev/null 2>&1 -then - against=HEAD -else - # Initial commit: diff against an empty tree object - against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 -fi - -# If you want to allow non-ascii filenames set this variable to true. -allownonascii=$(git config hooks.allownonascii) - -# Cross platform projects tend to avoid non-ascii filenames; prevent -# them from being added to the repository. We exploit the fact that the -# printable range starts at the space character and ends with tilde. -if [ "$allownonascii" != "true" ] && - # Note that the use of brackets around a tr range is ok here, (it's - # even required, for portability to Solaris 10's /usr/bin/tr), since - # the square bracket bytes happen to fall in the designated range. - test "$(git diff --cached --name-only --diff-filter=A -z $against | - LC_ALL=C tr -d '[ -~]\0')" -then - echo "Error: Attempt to add a non-ascii file name." - echo - echo "This can cause problems if you want to work" - echo "with people on other platforms." - echo - echo "To be portable it is advisable to rename the file ..." - echo - echo "If you know what you are doing you can disable this" - echo "check using:" - echo - echo " git config hooks.allownonascii true" - echo - exit 1 -fi - -exec git diff-index --check --cached $against -- diff --git a/tests-clar/resources/template/hooks/pre-rebase.sample b/tests-clar/resources/template/hooks/pre-rebase.sample deleted file mode 100755 index 9773ed4cb29..00000000000 --- a/tests-clar/resources/template/hooks/pre-rebase.sample +++ /dev/null @@ -1,169 +0,0 @@ -#!/bin/sh -# -# Copyright (c) 2006, 2008 Junio C Hamano -# -# The "pre-rebase" hook is run just before "git rebase" starts doing -# its job, and can prevent the command from running by exiting with -# non-zero status. -# -# The hook is called with the following parameters: -# -# $1 -- the upstream the series was forked from. -# $2 -- the branch being rebased (or empty when rebasing the current branch). -# -# This sample shows how to prevent topic branches that are already -# merged to 'next' branch from getting rebased, because allowing it -# would result in rebasing already published history. - -publish=next -basebranch="$1" -if test "$#" = 2 -then - topic="refs/heads/$2" -else - topic=`git symbolic-ref HEAD` || - exit 0 ;# we do not interrupt rebasing detached HEAD -fi - -case "$topic" in -refs/heads/??/*) - ;; -*) - exit 0 ;# we do not interrupt others. - ;; -esac - -# Now we are dealing with a topic branch being rebased -# on top of master. Is it OK to rebase it? - -# Does the topic really exist? -git show-ref -q "$topic" || { - echo >&2 "No such branch $topic" - exit 1 -} - -# Is topic fully merged to master? -not_in_master=`git rev-list --pretty=oneline ^master "$topic"` -if test -z "$not_in_master" -then - echo >&2 "$topic is fully merged to master; better remove it." - exit 1 ;# we could allow it, but there is no point. -fi - -# Is topic ever merged to next? If so you should not be rebasing it. -only_next_1=`git rev-list ^master "^$topic" ${publish} | sort` -only_next_2=`git rev-list ^master ${publish} | sort` -if test "$only_next_1" = "$only_next_2" -then - not_in_topic=`git rev-list "^$topic" master` - if test -z "$not_in_topic" - then - echo >&2 "$topic is already up-to-date with master" - exit 1 ;# we could allow it, but there is no point. - else - exit 0 - fi -else - not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"` - /usr/bin/perl -e ' - my $topic = $ARGV[0]; - my $msg = "* $topic has commits already merged to public branch:\n"; - my (%not_in_next) = map { - /^([0-9a-f]+) /; - ($1 => 1); - } split(/\n/, $ARGV[1]); - for my $elem (map { - /^([0-9a-f]+) (.*)$/; - [$1 => $2]; - } split(/\n/, $ARGV[2])) { - if (!exists $not_in_next{$elem->[0]}) { - if ($msg) { - print STDERR $msg; - undef $msg; - } - print STDERR " $elem->[1]\n"; - } - } - ' "$topic" "$not_in_next" "$not_in_master" - exit 1 -fi - -exit 0 - -################################################################ - -This sample hook safeguards topic branches that have been -published from being rewound. - -The workflow assumed here is: - - * Once a topic branch forks from "master", "master" is never - merged into it again (either directly or indirectly). - - * Once a topic branch is fully cooked and merged into "master", - it is deleted. If you need to build on top of it to correct - earlier mistakes, a new topic branch is created by forking at - the tip of the "master". This is not strictly necessary, but - it makes it easier to keep your history simple. - - * Whenever you need to test or publish your changes to topic - branches, merge them into "next" branch. - -The script, being an example, hardcodes the publish branch name -to be "next", but it is trivial to make it configurable via -$GIT_DIR/config mechanism. - -With this workflow, you would want to know: - -(1) ... if a topic branch has ever been merged to "next". Young - topic branches can have stupid mistakes you would rather - clean up before publishing, and things that have not been - merged into other branches can be easily rebased without - affecting other people. But once it is published, you would - not want to rewind it. - -(2) ... if a topic branch has been fully merged to "master". - Then you can delete it. More importantly, you should not - build on top of it -- other people may already want to - change things related to the topic as patches against your - "master", so if you need further changes, it is better to - fork the topic (perhaps with the same name) afresh from the - tip of "master". - -Let's look at this example: - - o---o---o---o---o---o---o---o---o---o "next" - / / / / - / a---a---b A / / - / / / / - / / c---c---c---c B / - / / / \ / - / / / b---b C \ / - / / / / \ / - ---o---o---o---o---o---o---o---o---o---o---o "master" - - -A, B and C are topic branches. - - * A has one fix since it was merged up to "next". - - * B has finished. It has been fully merged up to "master" and "next", - and is ready to be deleted. - - * C has not merged to "next" at all. - -We would want to allow C to be rebased, refuse A, and encourage -B to be deleted. - -To compute (1): - - git rev-list ^master ^topic next - git rev-list ^master next - - if these match, topic has not merged in next at all. - -To compute (2): - - git rev-list master..topic - - if this is empty, it is fully merged to "master". diff --git a/tests-clar/resources/template/hooks/prepare-commit-msg.sample b/tests-clar/resources/template/hooks/prepare-commit-msg.sample deleted file mode 100755 index f093a02ec49..00000000000 --- a/tests-clar/resources/template/hooks/prepare-commit-msg.sample +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/sh -# -# An example hook script to prepare the commit log message. -# Called by "git commit" with the name of the file that has the -# commit message, followed by the description of the commit -# message's source. The hook's purpose is to edit the commit -# message file. If the hook fails with a non-zero status, -# the commit is aborted. -# -# To enable this hook, rename this file to "prepare-commit-msg". - -# This hook includes three examples. The first comments out the -# "Conflicts:" part of a merge commit. -# -# The second includes the output of "git diff --name-status -r" -# into the message, just before the "git status" output. It is -# commented because it doesn't cope with --amend or with squashed -# commits. -# -# The third example adds a Signed-off-by line to the message, that can -# still be edited. This is rarely a good idea. - -case "$2,$3" in - merge,) - /usr/bin/perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;; - -# ,|template,) -# /usr/bin/perl -i.bak -pe ' -# print "\n" . `git diff --cached --name-status -r` -# if /^#/ && $first++ == 0' "$1" ;; - - *) ;; -esac - -# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') -# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" diff --git a/tests-clar/resources/template/hooks/update.sample b/tests-clar/resources/template/hooks/update.sample deleted file mode 100755 index 71ab04edc09..00000000000 --- a/tests-clar/resources/template/hooks/update.sample +++ /dev/null @@ -1,128 +0,0 @@ -#!/bin/sh -# -# An example hook script to blocks unannotated tags from entering. -# Called by "git receive-pack" with arguments: refname sha1-old sha1-new -# -# To enable this hook, rename this file to "update". -# -# Config -# ------ -# hooks.allowunannotated -# This boolean sets whether unannotated tags will be allowed into the -# repository. By default they won't be. -# hooks.allowdeletetag -# This boolean sets whether deleting tags will be allowed in the -# repository. By default they won't be. -# hooks.allowmodifytag -# This boolean sets whether a tag may be modified after creation. By default -# it won't be. -# hooks.allowdeletebranch -# This boolean sets whether deleting branches will be allowed in the -# repository. By default they won't be. -# hooks.denycreatebranch -# This boolean sets whether remotely creating branches will be denied -# in the repository. By default this is allowed. -# - -# --- Command line -refname="$1" -oldrev="$2" -newrev="$3" - -# --- Safety check -if [ -z "$GIT_DIR" ]; then - echo "Don't run this script from the command line." >&2 - echo " (if you want, you could supply GIT_DIR then run" >&2 - echo " $0 )" >&2 - exit 1 -fi - -if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then - echo "Usage: $0 " >&2 - exit 1 -fi - -# --- Config -allowunannotated=$(git config --bool hooks.allowunannotated) -allowdeletebranch=$(git config --bool hooks.allowdeletebranch) -denycreatebranch=$(git config --bool hooks.denycreatebranch) -allowdeletetag=$(git config --bool hooks.allowdeletetag) -allowmodifytag=$(git config --bool hooks.allowmodifytag) - -# check for no description -projectdesc=$(sed -e '1q' "$GIT_DIR/description") -case "$projectdesc" in -"Unnamed repository"* | "") - echo "*** Project description file hasn't been set" >&2 - exit 1 - ;; -esac - -# --- Check types -# if $newrev is 0000...0000, it's a commit to delete a ref. -zero="0000000000000000000000000000000000000000" -if [ "$newrev" = "$zero" ]; then - newrev_type=delete -else - newrev_type=$(git cat-file -t $newrev) -fi - -case "$refname","$newrev_type" in - refs/tags/*,commit) - # un-annotated tag - short_refname=${refname##refs/tags/} - if [ "$allowunannotated" != "true" ]; then - echo "*** The un-annotated tag, $short_refname, is not allowed in this repository" >&2 - echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2 - exit 1 - fi - ;; - refs/tags/*,delete) - # delete tag - if [ "$allowdeletetag" != "true" ]; then - echo "*** Deleting a tag is not allowed in this repository" >&2 - exit 1 - fi - ;; - refs/tags/*,tag) - # annotated tag - if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1 - then - echo "*** Tag '$refname' already exists." >&2 - echo "*** Modifying a tag is not allowed in this repository." >&2 - exit 1 - fi - ;; - refs/heads/*,commit) - # branch - if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then - echo "*** Creating a branch is not allowed in this repository" >&2 - exit 1 - fi - ;; - refs/heads/*,delete) - # delete branch - if [ "$allowdeletebranch" != "true" ]; then - echo "*** Deleting a branch is not allowed in this repository" >&2 - exit 1 - fi - ;; - refs/remotes/*,commit) - # tracking branch - ;; - refs/remotes/*,delete) - # delete tracking branch - if [ "$allowdeletebranch" != "true" ]; then - echo "*** Deleting a tracking branch is not allowed in this repository" >&2 - exit 1 - fi - ;; - *) - # Anything else (is there anything else?) - echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 - exit 1 - ;; -esac - -# --- Finished -exit 0 diff --git a/tests-clar/resources/testrepo.git/config b/tests-clar/resources/testrepo.git/config deleted file mode 100644 index 904a4e3f361..00000000000 --- a/tests-clar/resources/testrepo.git/config +++ /dev/null @@ -1,36 +0,0 @@ -[core] - repositoryformatversion = 0 - filemode = true - bare = true - logallrefupdates = true -[remote "test"] - url = git://github.com/libgit2/libgit2 - fetch = +refs/heads/*:refs/remotes/test/* -[remote "joshaber"] - url = git://github.com/libgit2/libgit2 -[remote "empty-remote-url"] - url = - -[remote "test_with_pushurl"] - url = git://github.com/libgit2/fetchlibgit2 - pushurl = git://github.com/libgit2/pushlibgit2 - fetch = +refs/heads/*:refs/remotes/test_with_pushurl/* - -[branch "master"] - remote = test - merge = refs/heads/master -[branch "track-local"] - remote = . - merge = refs/heads/master -[branch "cannot-fetch"] - remote = joshaber - merge = refs/heads/cannot-fetch -[branch "remoteless"] - remote = - merge = refs/heads/master -[branch "mergeless"] - remote = test - merge = -[branch "mergeandremoteless"] - remote = - merge = diff --git a/tests-clar/resources/testrepo.git/packed-refs b/tests-clar/resources/testrepo.git/packed-refs deleted file mode 100644 index 52f5e876fbc..00000000000 --- a/tests-clar/resources/testrepo.git/packed-refs +++ /dev/null @@ -1,3 +0,0 @@ -# pack-refs with: peeled -41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9 refs/heads/packed -5b5b025afb0b4c913b4c338a42934a3863bf3644 refs/heads/packed-test diff --git a/tests-clar/resources/testrepo/.gitted/config b/tests-clar/resources/testrepo/.gitted/config deleted file mode 100644 index d0114012f98..00000000000 --- a/tests-clar/resources/testrepo/.gitted/config +++ /dev/null @@ -1,8 +0,0 @@ -[core] - repositoryformatversion = 0 - filemode = true - bare = false - logallrefupdates = true -[remote "test"] - url = git://github.com/libgit2/libgit2 - fetch = +refs/heads/*:refs/remotes/test/* diff --git a/tests-clar/resources/testrepo/.gitted/index b/tests-clar/resources/testrepo/.gitted/index deleted file mode 100644 index a27fb9c96fe..00000000000 Binary files a/tests-clar/resources/testrepo/.gitted/index and /dev/null differ diff --git a/tests-clar/resources/testrepo/.gitted/packed-refs b/tests-clar/resources/testrepo/.gitted/packed-refs deleted file mode 100644 index 6018a19d2e9..00000000000 --- a/tests-clar/resources/testrepo/.gitted/packed-refs +++ /dev/null @@ -1,4 +0,0 @@ -# pack-refs with: peeled -41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9 refs/heads/packed -5b5b025afb0b4c913b4c338a42934a3863bf3644 refs/heads/packed-test -b25fa35b38051e4ae45d4222e795f9df2e43f1d1 refs/tags/packed-tag diff --git a/tests-clar/revwalk/basic.c b/tests-clar/revwalk/basic.c deleted file mode 100644 index 438ec01627d..00000000000 --- a/tests-clar/revwalk/basic.c +++ /dev/null @@ -1,191 +0,0 @@ -#include "clar_libgit2.h" - -/* - $ git log --oneline --graph --decorate - * a4a7dce (HEAD, br2) Merge branch 'master' into br2 - |\ - | * 9fd738e (master) a fourth commit - | * 4a202b3 a third commit - * | c47800c branch commit one - |/ - * 5b5b025 another commit - * 8496071 testing -*/ -static const char *commit_head = "a4a7dce85cf63874e984719f4fdd239f5145052f"; - -static const char *commit_ids[] = { - "a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */ - "9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */ - "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */ - "c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */ - "8496071c1b46c854b31185ea97743be6a8774479", /* 4 */ - "5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */ -}; - -/* Careful: there are two possible topological sorts */ -static const int commit_sorting_topo[][6] = { - {0, 1, 2, 3, 5, 4}, {0, 3, 1, 2, 5, 4} -}; - -static const int commit_sorting_time[][6] = { - {0, 3, 1, 2, 5, 4} -}; - -static const int commit_sorting_topo_reverse[][6] = { - {4, 5, 3, 2, 1, 0}, {4, 5, 2, 1, 3, 0} -}; - -static const int commit_sorting_time_reverse[][6] = { - {4, 5, 2, 1, 3, 0} -}; - -#define commit_count 6 -static const int result_bytes = 24; - - -static int get_commit_index(git_oid *raw_oid) -{ - int i; - char oid[40]; - - git_oid_fmt(oid, raw_oid); - - for (i = 0; i < commit_count; ++i) - if (memcmp(oid, commit_ids[i], 40) == 0) - return i; - - return -1; -} - -static int test_walk(git_revwalk *walk, const git_oid *root, - int flags, const int possible_results[][6], int results_count) -{ - git_oid oid; - - int i; - int result_array[commit_count]; - - git_revwalk_sorting(walk, flags); - git_revwalk_push(walk, root); - - for (i = 0; i < commit_count; ++i) - result_array[i] = -1; - - i = 0; - - while (git_revwalk_next(&oid, walk) == 0) { - result_array[i++] = get_commit_index(&oid); - /*{ - char str[41]; - git_oid_fmt(str, &oid); - str[40] = 0; - printf(" %d) %s\n", i, str); - }*/ - } - - for (i = 0; i < results_count; ++i) - if (memcmp(possible_results[i], - result_array, result_bytes) == 0) - return 0; - - return GIT_ERROR; -} - -static git_repository *_repo; -static git_revwalk *_walk; - -void test_revwalk_basic__initialize(void) -{ - cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git"))); - cl_git_pass(git_revwalk_new(&_walk, _repo)); -} - -void test_revwalk_basic__cleanup(void) -{ - git_revwalk_free(_walk); - _walk = NULL; - git_repository_free(_repo); - _repo = NULL; -} - -void test_revwalk_basic__sorting_modes(void) -{ - git_oid id; - - git_oid_fromstr(&id, commit_head); - - cl_git_pass(test_walk(_walk, &id, GIT_SORT_TIME, commit_sorting_time, 1)); - cl_git_pass(test_walk(_walk, &id, GIT_SORT_TOPOLOGICAL, commit_sorting_topo, 2)); - cl_git_pass(test_walk(_walk, &id, GIT_SORT_TIME | GIT_SORT_REVERSE, commit_sorting_time_reverse, 1)); - cl_git_pass(test_walk(_walk, &id, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE, commit_sorting_topo_reverse, 2)); -} - -void test_revwalk_basic__glob_heads(void) -{ - int i = 0; - git_oid oid; - - cl_git_pass(git_revwalk_push_glob(_walk, "heads")); - - while (git_revwalk_next(&oid, _walk) == 0) { - i++; - } - - /* git log --branches --oneline | wc -l => 14 */ - cl_assert(i == 14); -} - -void test_revwalk_basic__push_head(void) -{ - int i = 0; - git_oid oid; - - cl_git_pass(git_revwalk_push_head(_walk)); - - while (git_revwalk_next(&oid, _walk) == 0) { - i++; - } - - /* git log HEAD --oneline | wc -l => 7 */ - cl_assert(i == 7); -} - -void test_revwalk_basic__push_head_hide_ref(void) -{ - int i = 0; - git_oid oid; - - cl_git_pass(git_revwalk_push_head(_walk)); - cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed-test")); - - while (git_revwalk_next(&oid, _walk) == 0) { - i++; - } - - /* git log HEAD --oneline --not refs/heads/packed-test | wc -l => 4 */ - cl_assert(i == 4); -} - -void test_revwalk_basic__push_head_hide_ref_nobase(void) -{ - int i = 0; - git_oid oid; - - cl_git_pass(git_revwalk_push_head(_walk)); - cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed")); - - while (git_revwalk_next(&oid, _walk) == 0) { - i++; - } - - /* git log HEAD --oneline --not refs/heads/packed | wc -l => 7 */ - cl_assert(i == 7); -} - -void test_revwalk_basic__disallow_non_commit(void) -{ - git_oid oid; - - cl_git_pass(git_oid_fromstr(&oid, "521d87c1ec3aef9824daf6d96cc0ae3710766d91")); - cl_git_fail(git_revwalk_push(_walk, &oid)); -} diff --git a/tests-clar/revwalk/mergebase.c b/tests-clar/revwalk/mergebase.c deleted file mode 100644 index e2617ab0e4d..00000000000 --- a/tests-clar/revwalk/mergebase.c +++ /dev/null @@ -1,380 +0,0 @@ -#include "clar_libgit2.h" -#include "vector.h" -#include - -static git_repository *_repo; -static git_repository *_repo2; - -void test_revwalk_mergebase__initialize(void) -{ - cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git"))); - cl_git_pass(git_repository_open(&_repo2, cl_fixture("twowaymerge.git"))); -} - -void test_revwalk_mergebase__cleanup(void) -{ - git_repository_free(_repo); - _repo = NULL; - - git_repository_free(_repo2); - _repo2 = NULL; -} - -void test_revwalk_mergebase__single1(void) -{ - git_oid result, one, two, expected; - size_t ahead, behind; - - cl_git_pass(git_oid_fromstr(&one, "c47800c7266a2be04c571c04d5a6614691ea99bd ")); - cl_git_pass(git_oid_fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a")); - cl_git_pass(git_oid_fromstr(&expected, "5b5b025afb0b4c913b4c338a42934a3863bf3644")); - - cl_git_pass(git_merge_base(&result, _repo, &one, &two)); - cl_assert(git_oid_cmp(&result, &expected) == 0); - - cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two)); - cl_assert_equal_sz(ahead, 2); - cl_assert_equal_sz(behind, 1); - - cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &two, &one)); - cl_assert_equal_sz(ahead, 1); - cl_assert_equal_sz(behind, 2); -} - -void test_revwalk_mergebase__single2(void) -{ - git_oid result, one, two, expected; - size_t ahead, behind; - - cl_git_pass(git_oid_fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af")); - cl_git_pass(git_oid_fromstr(&two, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750")); - cl_git_pass(git_oid_fromstr(&expected, "c47800c7266a2be04c571c04d5a6614691ea99bd")); - - cl_git_pass(git_merge_base(&result, _repo, &one, &two)); - cl_assert(git_oid_cmp(&result, &expected) == 0); - - cl_git_pass(git_graph_ahead_behind( &ahead, &behind, _repo, &one, &two)); - cl_assert_equal_sz(ahead, 4); - cl_assert_equal_sz(behind, 1); - - cl_git_pass(git_graph_ahead_behind( &ahead, &behind, _repo, &two, &one)); - cl_assert_equal_sz(ahead, 1); - cl_assert_equal_sz(behind, 4); -} - -void test_revwalk_mergebase__merged_branch(void) -{ - git_oid result, one, two, expected; - size_t ahead, behind; - - cl_git_pass(git_oid_fromstr(&one, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750")); - cl_git_pass(git_oid_fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a")); - cl_git_pass(git_oid_fromstr(&expected, "9fd738e8f7967c078dceed8190330fc8648ee56a")); - - cl_git_pass(git_merge_base(&result, _repo, &one, &two)); - cl_assert(git_oid_cmp(&result, &expected) == 0); - - cl_git_pass(git_merge_base(&result, _repo, &two, &one)); - cl_assert(git_oid_cmp(&result, &expected) == 0); - - cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two)); - cl_assert_equal_sz(ahead, 0); - cl_assert_equal_sz(behind, 3); - - cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &two, &one)); - cl_assert_equal_sz(ahead, 3); - cl_assert_equal_sz(behind, 0); -} - -void test_revwalk_mergebase__two_way_merge(void) -{ - git_oid one, two; - size_t ahead, behind; - - cl_git_pass(git_oid_fromstr(&one, "9b219343610c88a1187c996d0dc58330b55cee28")); - cl_git_pass(git_oid_fromstr(&two, "a953a018c5b10b20c86e69fef55ebc8ad4c5a417")); - cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo2, &one, &two)); - - cl_assert_equal_sz(ahead, 2); - cl_assert_equal_sz(behind, 8); - - cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo2, &two, &one)); - - cl_assert_equal_sz(ahead, 8); - cl_assert_equal_sz(behind, 2); -} - -void test_revwalk_mergebase__no_common_ancestor_returns_ENOTFOUND(void) -{ - git_oid result, one, two; - size_t ahead, behind; - int error; - - cl_git_pass(git_oid_fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af")); - cl_git_pass(git_oid_fromstr(&two, "e90810b8df3e80c413d903f631643c716887138d")); - - error = git_merge_base(&result, _repo, &one, &two); - cl_git_fail(error); - - cl_assert_equal_i(GIT_ENOTFOUND, error); - - cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two)); - cl_assert_equal_sz(2, ahead); - cl_assert_equal_sz(4, behind); -} - -void test_revwalk_mergebase__no_off_by_one_missing(void) -{ - git_oid result, one, two; - - cl_git_pass(git_oid_fromstr(&one, "1a443023183e3f2bfbef8ac923cd81c1018a18fd")); - cl_git_pass(git_oid_fromstr(&two, "9f13f7d0a9402c681f91dc590cf7b5470e6a77d2")); - cl_git_pass(git_merge_base(&result, _repo, &one, &two)); -} - -static void assert_mergebase_many(const char *expected_sha, int count, ...) -{ - va_list ap; - int i; - git_oid *oids; - git_oid oid, expected; - char *partial_oid; - git_object *object; - - oids = git__malloc(count * sizeof(git_oid)); - cl_assert(oids != NULL); - - memset(oids, 0x0, count * sizeof(git_oid)); - - va_start(ap, count); - - for (i = 0; i < count; ++i) { - partial_oid = va_arg(ap, char *); - cl_git_pass(git_oid_fromstrn(&oid, partial_oid, strlen(partial_oid))); - - cl_git_pass(git_object_lookup_prefix(&object, _repo, &oid, strlen(partial_oid), GIT_OBJ_COMMIT)); - git_oid_cpy(&oids[i], git_object_id(object)); - git_object_free(object); - } - - va_end(ap); - - if (expected_sha == NULL) - cl_assert_equal_i(GIT_ENOTFOUND, git_merge_base_many(&oid, _repo, oids, count)); - else { - cl_git_pass(git_merge_base_many(&oid, _repo, oids, count)); - cl_git_pass(git_oid_fromstr(&expected, expected_sha)); - - cl_assert(git_oid_cmp(&expected, &oid) == 0); - } - - git__free(oids); -} - -void test_revwalk_mergebase__many_no_common_ancestor_returns_ENOTFOUND(void) -{ - assert_mergebase_many(NULL, 3, "41bc8c", "e90810", "a65fed"); - assert_mergebase_many(NULL, 3, "e90810", "41bc8c", "a65fed"); - assert_mergebase_many(NULL, 3, "e90810", "a65fed", "41bc8c"); - assert_mergebase_many(NULL, 3, "a65fed", "e90810", "41bc8c"); - assert_mergebase_many(NULL, 3, "a65fed", "e90810", "41bc8c"); - assert_mergebase_many(NULL, 3, "a65fed", "41bc8c", "e90810"); - - assert_mergebase_many(NULL, 3, "e90810", "763d71", "a65fed"); -} - -void test_revwalk_mergebase__many_merge_branch(void) -{ - assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "763d71", "849607"); - - assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "763d71", "e90810", "a65fed"); - assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "763d71", "a65fed", "e90810"); - - assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "763d71", "849607"); - assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "849607", "763d71"); - assert_mergebase_many("8496071c1b46c854b31185ea97743be6a8774479", 3, "849607", "a65fed", "763d71"); - - assert_mergebase_many("5b5b025afb0b4c913b4c338a42934a3863bf3644", 5, "5b5b02", "763d71", "a4a7dc", "a65fed", "41bc8c"); -} - -/* - * testrepo.git $ git log --graph --all - * * commit 763d71aadf09a7951596c9746c024e7eece7c7af - * | Author: nulltoken - * | Date: Sun Oct 9 12:54:47 2011 +0200 - * | - * | Add some files into subdirectories - * | - * | * commit a65fedf39aefe402d3bb6e24df4d4f5fe4547750 - * | | Author: Scott Chacon - * | | Date: Tue Aug 9 19:33:46 2011 -0700 - * | | - * | * commit be3563ae3f795b2b4353bcce3a527ad0a4f7f644 - * | |\ Merge: 9fd738e c47800c - * | |/ Author: Scott Chacon - * |/| Date: Tue May 25 11:58:27 2010 -0700 - * | | - * | | Merge branch 'br2' - * | | - * | | * commit e90810b8df3e80c413d903f631643c716887138d - * | | | Author: Vicent Marti - * | | | Date: Thu Aug 5 18:42:20 2010 +0200 - * | | | - * | | | Test commit 2 - * | | | - * | | * commit 6dcf9bf7541ee10456529833502442f385010c3d - * | | Author: Vicent Marti - * | | Date: Thu Aug 5 18:41:33 2010 +0200 - * | | - * | | Test commit 1 - * | | - * | | * commit a4a7dce85cf63874e984719f4fdd239f5145052f - * | | |\ Merge: c47800c 9fd738e - * | |/ / Author: Scott Chacon - * |/| / Date: Tue May 25 12:00:23 2010 -0700 - * | |/ - * | | Merge branch 'master' into br2 - * | | - * | * commit 9fd738e8f7967c078dceed8190330fc8648ee56a - * | | Author: Scott Chacon - * | | Date: Mon May 24 10:19:19 2010 -0700 - * | | - * | | a fourth commit - * | | - * | * commit 4a202b346bb0fb0db7eff3cffeb3c70babbd2045 - * | | Author: Scott Chacon - * | | Date: Mon May 24 10:19:04 2010 -0700 - * | | - * | | a third commit - * | | - * * | commit c47800c7266a2be04c571c04d5a6614691ea99bd - * |/ Author: Scott Chacon - * | Date: Tue May 25 11:58:14 2010 -0700 - * | - * | branch commit one - * | - * * commit 5b5b025afb0b4c913b4c338a42934a3863bf3644 - * | Author: Scott Chacon - * | Date: Tue May 11 13:38:42 2010 -0700 - * | - * | another commit - * | - * * commit 8496071c1b46c854b31185ea97743be6a8774479 - * Author: Scott Chacon - * Date: Sat May 8 16:13:06 2010 -0700 - * - * testing - * - * * commit 41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9 - * | Author: Scott Chacon - * | Date: Tue May 11 13:40:41 2010 -0700 - * | - * | packed commit two - * | - * * commit 5001298e0c09ad9c34e4249bc5801c75e9754fa5 - * Author: Scott Chacon - * Date: Tue May 11 13:40:23 2010 -0700 - * - * packed commit one - */ - -/* - * twowaymerge.git $ git log --graph --all - * * commit 9b219343610c88a1187c996d0dc58330b55cee28 - * |\ Merge: c37a783 2224e19 - * | | Author: Scott J. Goldman - * | | Date: Tue Nov 27 20:31:04 2012 -0800 - * | | - * | | Merge branch 'first-branch' into second-branch - * | | - * | * commit 2224e191514cb4bd8c566d80dac22dfcb1e9bb83 - * | | Author: Scott J. Goldman - * | | Date: Tue Nov 27 20:28:51 2012 -0800 - * | | - * | | j - * | | - * | * commit a41a49f8f5cd9b6cb14a076bf8394881ed0b4d19 - * | | Author: Scott J. Goldman - * | | Date: Tue Nov 27 20:28:39 2012 -0800 - * | | - * | | i - * | | - * | * commit 82bf9a1a10a4b25c1f14c9607b60970705e92545 - * | | Author: Scott J. Goldman - * | | Date: Tue Nov 27 20:28:28 2012 -0800 - * | | - * | | h - * | | - * * | commit c37a783c20d92ac92362a78a32860f7eebf938ef - * | | Author: Scott J. Goldman - * | | Date: Tue Nov 27 20:30:57 2012 -0800 - * | | - * | | n - * | | - * * | commit 8b82fb1794cb1c8c7f172ec730a4c2db0ae3e650 - * | | Author: Scott J. Goldman - * | | Date: Tue Nov 27 20:30:43 2012 -0800 - * | | - * | | m - * | | - * * | commit 6ab5d28acbf3c3bdff276f7ccfdf29c1520e542f - * | | Author: Scott J. Goldman - * | | Date: Tue Nov 27 20:30:38 2012 -0800 - * | | - * | | l - * | | - * * | commit 7b8c336c45fc6895c1c60827260fe5d798e5d247 - * | | Author: Scott J. Goldman - * | | Date: Tue Nov 27 20:30:24 2012 -0800 - * | | - * | | k - * | | - * | | * commit 1c30b88f5f3ee66d78df6520a7de9e89b890818b - * | | | Author: Scott J. Goldman - * | | | Date: Tue Nov 27 20:28:10 2012 -0800 - * | | | - * | | | e - * | | | - * | | * commit 42b7311aa626e712891940c1ec5d5cba201946a4 - * | | | Author: Scott J. Goldman - * | | | Date: Tue Nov 27 20:28:06 2012 -0800 - * | | | - * | | | d - * | | | - * | | * commit a953a018c5b10b20c86e69fef55ebc8ad4c5a417 - * | | |\ Merge: bd1732c cdf97fd - * | | |/ Author: Scott J. Goldman - * | |/| Date: Tue Nov 27 20:26:43 2012 -0800 - * | | | - * | | | Merge branch 'first-branch' - * | | | - * | * | commit cdf97fd3bb48eb3827638bb33d208f5fd32d0aa6 - * | | | Author: Scott J. Goldman - * | | | Date: Tue Nov 27 20:24:46 2012 -0800 - * | | | - * | | | g - * | | | - * | * | commit ef0488f0b722f0be8bcb90a7730ac7efafd1d694 - * | | | Author: Scott J. Goldman - * | | | Date: Tue Nov 27 20:24:39 2012 -0800 - * | | | - * | | | f - * | | | - * | | * commit bd1732c43c68d712ad09e1d872b9be6d4b9efdc4 - * | |/ Author: Scott J. Goldman - * | | Date: Tue Nov 27 17:43:58 2012 -0800 - * | | - * | | c - * | | - * | * commit 0c8a3f1f3d5f421cf83048c7c73ee3b55a5e0f29 - * |/ Author: Scott J. Goldman - * | Date: Tue Nov 27 17:43:48 2012 -0800 - * | - * | b - * | - * * commit 1f4c0311a24b63f6fc209a59a1e404942d4a5006 - * Author: Scott J. Goldman - * Date: Tue Nov 27 17:43:41 2012 -0800 - * - * a - */ diff --git a/tests-clar/stash/drop.c b/tests-clar/stash/drop.c deleted file mode 100644 index 16e3d77ac0e..00000000000 --- a/tests-clar/stash/drop.c +++ /dev/null @@ -1,137 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" -#include "stash_helpers.h" - -static git_repository *repo; -static git_signature *signature; - -void test_stash_drop__initialize(void) -{ - cl_git_pass(git_repository_init(&repo, "stash", 0)); - cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */ -} - -void test_stash_drop__cleanup(void) -{ - git_signature_free(signature); - signature = NULL; - - git_repository_free(repo); - repo = NULL; - - cl_git_pass(git_futils_rmdir_r("stash", NULL, GIT_RMDIR_REMOVE_FILES)); -} - -void test_stash_drop__cannot_drop_from_an_empty_stash(void) -{ - cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 0)); -} - -static void push_three_states(void) -{ - git_oid oid; - git_index *index; - - cl_git_mkfile("stash/zero.txt", "content\n"); - cl_git_pass(git_repository_index(&index, repo)); - cl_git_pass(git_index_add_bypath(index, "zero.txt")); - commit_staged_files(&oid, index, signature); - cl_assert(git_path_exists("stash/zero.txt")); - - cl_git_mkfile("stash/one.txt", "content\n"); - cl_git_pass(git_stash_save(&oid, repo, signature, "First", GIT_STASH_INCLUDE_UNTRACKED)); - cl_assert(!git_path_exists("stash/one.txt")); - cl_assert(git_path_exists("stash/zero.txt")); - - cl_git_mkfile("stash/two.txt", "content\n"); - cl_git_pass(git_stash_save(&oid, repo, signature, "Second", GIT_STASH_INCLUDE_UNTRACKED)); - cl_assert(!git_path_exists("stash/two.txt")); - cl_assert(git_path_exists("stash/zero.txt")); - - cl_git_mkfile("stash/three.txt", "content\n"); - cl_git_pass(git_stash_save(&oid, repo, signature, "Third", GIT_STASH_INCLUDE_UNTRACKED)); - cl_assert(!git_path_exists("stash/three.txt")); - cl_assert(git_path_exists("stash/zero.txt")); - - git_index_free(index); -} - -void test_stash_drop__cannot_drop_a_non_existing_stashed_state(void) -{ - push_three_states(); - - cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 666)); - cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 42)); - cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 3)); -} - -void test_stash_drop__can_purge_the_stash_from_the_top(void) -{ - push_three_states(); - - cl_git_pass(git_stash_drop(repo, 0)); - cl_git_pass(git_stash_drop(repo, 0)); - cl_git_pass(git_stash_drop(repo, 0)); - - cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 0)); -} - -void test_stash_drop__can_purge_the_stash_from_the_bottom(void) -{ - push_three_states(); - - cl_git_pass(git_stash_drop(repo, 2)); - cl_git_pass(git_stash_drop(repo, 1)); - cl_git_pass(git_stash_drop(repo, 0)); - - cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 0)); -} - -void test_stash_drop__dropping_an_entry_rewrites_reflog_history(void) -{ - git_reference *stash; - git_reflog *reflog; - const git_reflog_entry *entry; - git_oid oid; - size_t count; - - push_three_states(); - - cl_git_pass(git_reference_lookup(&stash, repo, "refs/stash")); - - cl_git_pass(git_reflog_read(&reflog, stash)); - entry = git_reflog_entry_byindex(reflog, 1); - - git_oid_cpy(&oid, git_reflog_entry_id_old(entry)); - count = git_reflog_entrycount(reflog); - - git_reflog_free(reflog); - - cl_git_pass(git_stash_drop(repo, 1)); - - cl_git_pass(git_reflog_read(&reflog, stash)); - entry = git_reflog_entry_byindex(reflog, 0); - - cl_assert_equal_i(0, git_oid_cmp(&oid, git_reflog_entry_id_old(entry))); - cl_assert_equal_sz(count - 1, git_reflog_entrycount(reflog)); - - git_reflog_free(reflog); - - git_reference_free(stash); -} - -void test_stash_drop__dropping_the_last_entry_removes_the_stash(void) -{ - git_reference *stash; - - push_three_states(); - - cl_git_pass(git_reference_lookup(&stash, repo, "refs/stash")); - git_reference_free(stash); - - cl_git_pass(git_stash_drop(repo, 0)); - cl_git_pass(git_stash_drop(repo, 0)); - cl_git_pass(git_stash_drop(repo, 0)); - - cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&stash, repo, "refs/stash")); -} diff --git a/tests-clar/stash/foreach.c b/tests-clar/stash/foreach.c deleted file mode 100644 index f1983625fa6..00000000000 --- a/tests-clar/stash/foreach.c +++ /dev/null @@ -1,124 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" -#include "stash_helpers.h" - -struct callback_data -{ - char **oids; - int invokes; -}; - -static git_repository *repo; -static git_signature *signature; -static git_oid stash_tip_oid; -struct callback_data data; - -#define REPO_NAME "stash" - -void test_stash_foreach__initialize(void) -{ - cl_git_pass(git_signature_new( - &signature, - "nulltoken", - "emeric.fermas@gmail.com", - 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */ - - memset(&data, 0, sizeof(struct callback_data)); -} - -void test_stash_foreach__cleanup(void) -{ - git_signature_free(signature); - signature = NULL; - - git_repository_free(repo); - repo = NULL; - - cl_git_pass(git_futils_rmdir_r(REPO_NAME, NULL, GIT_RMDIR_REMOVE_FILES)); -} - -static int callback_cb( - size_t index, - const char* message, - const git_oid *stash_oid, - void *payload) -{ - struct callback_data *data = (struct callback_data *)payload; - - GIT_UNUSED(index); - GIT_UNUSED(message); - - cl_assert_equal_i(0, git_oid_streq(stash_oid, data->oids[data->invokes++])); - - return 0; -} - -void test_stash_foreach__enumerating_a_empty_repository_doesnt_fail(void) -{ - char *oids[] = { NULL }; - - data.oids = oids; - - cl_git_pass(git_repository_init(&repo, REPO_NAME, 0)); - - cl_git_pass(git_stash_foreach(repo, callback_cb, &data)); - - cl_assert_equal_i(0, data.invokes); -} - -void test_stash_foreach__can_enumerate_a_repository(void) -{ - char *oids_default[] = { - "1d91c842a7cdfc25872b3a763e5c31add8816c25", NULL }; - - char *oids_untracked[] = { - "7f89a8b15c878809c5c54d1ff8f8c9674154017b", - "1d91c842a7cdfc25872b3a763e5c31add8816c25", NULL }; - - char *oids_ignored[] = { - "c95599a8fef20a7e57582c6727b1a0d02e0a5828", - "7f89a8b15c878809c5c54d1ff8f8c9674154017b", - "1d91c842a7cdfc25872b3a763e5c31add8816c25", NULL }; - - cl_git_pass(git_repository_init(&repo, REPO_NAME, 0)); - - setup_stash(repo, signature); - - cl_git_pass(git_stash_save( - &stash_tip_oid, - repo, - signature, - NULL, - GIT_STASH_DEFAULT)); - - data.oids = oids_default; - - cl_git_pass(git_stash_foreach(repo, callback_cb, &data)); - cl_assert_equal_i(1, data.invokes); - - data.oids = oids_untracked; - data.invokes = 0; - - cl_git_pass(git_stash_save( - &stash_tip_oid, - repo, - signature, - NULL, - GIT_STASH_INCLUDE_UNTRACKED)); - - cl_git_pass(git_stash_foreach(repo, callback_cb, &data)); - cl_assert_equal_i(2, data.invokes); - - data.oids = oids_ignored; - data.invokes = 0; - - cl_git_pass(git_stash_save( - &stash_tip_oid, - repo, - signature, - NULL, - GIT_STASH_INCLUDE_IGNORED)); - - cl_git_pass(git_stash_foreach(repo, callback_cb, &data)); - cl_assert_equal_i(3, data.invokes); -} diff --git a/tests-clar/stash/save.c b/tests-clar/stash/save.c deleted file mode 100644 index ea2eb282d28..00000000000 --- a/tests-clar/stash/save.c +++ /dev/null @@ -1,375 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" -#include "stash_helpers.h" - -static git_repository *repo; -static git_signature *signature; -static git_oid stash_tip_oid; - -/* - * Friendly reminder, in order to ease the reading of the following tests: - * - * "stash" points to the worktree commit - * "stash^1" points to the base commit (HEAD when the stash was created) - * "stash^2" points to the index commit - * "stash^3" points to the untracked commit - */ - -void test_stash_save__initialize(void) -{ - cl_git_pass(git_repository_init(&repo, "stash", 0)); - cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */ - - setup_stash(repo, signature); -} - -void test_stash_save__cleanup(void) -{ - git_signature_free(signature); - signature = NULL; - - git_repository_free(repo); - repo = NULL; - - cl_git_pass(git_futils_rmdir_r("stash", NULL, GIT_RMDIR_REMOVE_FILES)); - cl_fixture_cleanup("sorry-it-is-a-non-bare-only-party"); -} - -static void assert_object_oid(const char* revision, const char* expected_oid, git_otype type) -{ - git_object *object; - int result; - - result = git_revparse_single(&object, repo, revision); - - if (!expected_oid) { - cl_assert_equal_i(GIT_ENOTFOUND, result); - return; - } else - cl_assert_equal_i(0, result); - - cl_assert_equal_i(type, git_object_type(object)); - cl_git_pass(git_oid_streq(git_object_id(object), expected_oid)); - - git_object_free(object); -} - -static void assert_blob_oid(const char* revision, const char* expected_oid) -{ - assert_object_oid(revision, expected_oid, GIT_OBJ_BLOB); -} - -void test_stash_save__does_not_keep_index_by_default(void) -{ -/* -$ git stash - -$ git show refs/stash:what -see you later - -$ git show refs/stash:how -not so small and - -$ git show refs/stash:who -funky world - -$ git show refs/stash:when -fatal: Path 'when' exists on disk, but not in 'stash'. - -$ git show refs/stash^2:what -goodbye - -$ git show refs/stash^2:how -not so small and - -$ git show refs/stash^2:who -world - -$ git show refs/stash^2:when -fatal: Path 'when' exists on disk, but not in 'stash^2'. - -$ git status --short -?? when - -*/ - unsigned int status; - - cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); - cl_git_pass(git_status_file(&status, repo, "when")); - - assert_blob_oid("refs/stash:what", "bc99dc98b3eba0e9157e94769cd4d49cb49de449"); /* see you later */ - assert_blob_oid("refs/stash:how", "e6d64adb2c7f3eb8feb493b556cc8070dca379a3"); /* not so small and */ - assert_blob_oid("refs/stash:who", "a0400d4954659306a976567af43125a0b1aa8595"); /* funky world */ - assert_blob_oid("refs/stash:when", NULL); - assert_blob_oid("refs/stash:just.ignore", NULL); - - assert_blob_oid("refs/stash^2:what", "dd7e1c6f0fefe118f0b63d9f10908c460aa317a6"); /* goodbye */ - assert_blob_oid("refs/stash^2:how", "e6d64adb2c7f3eb8feb493b556cc8070dca379a3"); /* not so small and */ - assert_blob_oid("refs/stash^2:who", "cc628ccd10742baea8241c5924df992b5c019f71"); /* world */ - assert_blob_oid("refs/stash^2:when", NULL); - assert_blob_oid("refs/stash^2:just.ignore", NULL); - - assert_blob_oid("refs/stash^3", NULL); - - cl_assert_equal_i(GIT_STATUS_WT_NEW, status); -} - -static void assert_status( - const char *path, - int status_flags) -{ - unsigned int status; - int error; - - error = git_status_file(&status, repo, path); - - if (status_flags < 0) { - cl_assert_equal_i(status_flags, error); - return; - } - - cl_assert_equal_i(0, error); - cl_assert_equal_i((unsigned int)status_flags, status); -} - -void test_stash_save__can_keep_index(void) -{ - cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_KEEP_INDEX)); - - assert_status("what", GIT_STATUS_INDEX_MODIFIED); - assert_status("how", GIT_STATUS_INDEX_MODIFIED); - assert_status("who", GIT_STATUS_CURRENT); - assert_status("when", GIT_STATUS_WT_NEW); - assert_status("just.ignore", GIT_STATUS_IGNORED); -} - -static void assert_commit_message_contains(const char *revision, const char *fragment) -{ - git_commit *commit; - - cl_git_pass(git_revparse_single(((git_object **)&commit), repo, revision)); - - cl_assert(strstr(git_commit_message(commit), fragment) != NULL); - - git_commit_free(commit); -} - -void test_stash_save__can_include_untracked_files(void) -{ - cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED)); - - assert_commit_message_contains("refs/stash^3", "untracked files on master: "); - - assert_blob_oid("refs/stash^3:what", NULL); - assert_blob_oid("refs/stash^3:how", NULL); - assert_blob_oid("refs/stash^3:who", NULL); - assert_blob_oid("refs/stash^3:when", "b6ed15e81e2593d7bb6265eb4a991d29dc3e628b"); - assert_blob_oid("refs/stash^3:just.ignore", NULL); -} - -void test_stash_save__can_include_untracked_and_ignored_files(void) -{ - cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)); - - assert_commit_message_contains("refs/stash^3", "untracked files on master: "); - - assert_blob_oid("refs/stash^3:what", NULL); - assert_blob_oid("refs/stash^3:how", NULL); - assert_blob_oid("refs/stash^3:who", NULL); - assert_blob_oid("refs/stash^3:when", "b6ed15e81e2593d7bb6265eb4a991d29dc3e628b"); - assert_blob_oid("refs/stash^3:just.ignore", "78925fb1236b98b37a35e9723033e627f97aa88b"); -} - -#define MESSAGE "Look Ma! I'm on TV!" -void test_stash_save__can_accept_a_message(void) -{ - cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, MESSAGE, GIT_STASH_DEFAULT)); - - assert_commit_message_contains("refs/stash^2", "index on master: "); - assert_commit_message_contains("refs/stash", "On master: " MESSAGE); -} - -void test_stash_save__cannot_stash_against_an_unborn_branch(void) -{ - git_reference *head; - - cl_git_pass(git_reference_lookup(&head, repo, "HEAD")); - cl_git_pass(git_reference_symbolic_set_target(head, "refs/heads/unborn")); - - cl_assert_equal_i(GIT_EORPHANEDHEAD, - git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); - - git_reference_free(head); -} - -void test_stash_save__cannot_stash_against_a_bare_repository(void) -{ - git_repository *local; - - cl_git_pass(git_repository_init(&local, "sorry-it-is-a-non-bare-only-party", 1)); - - cl_assert_equal_i(GIT_EBAREREPO, - git_stash_save(&stash_tip_oid, local, signature, NULL, GIT_STASH_DEFAULT)); - - git_repository_free(local); -} - -void test_stash_save__can_stash_against_a_detached_head(void) -{ - git_repository_detach_head(repo); - - cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); - - assert_commit_message_contains("refs/stash^2", "index on (no branch): "); - assert_commit_message_contains("refs/stash", "WIP on (no branch): "); -} - -void test_stash_save__stashing_updates_the_reflog(void) -{ - char *sha; - - assert_object_oid("refs/stash@{0}", NULL, GIT_OBJ_COMMIT); - - cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); - - sha = git_oid_allocfmt(&stash_tip_oid); - - assert_object_oid("refs/stash@{0}", sha, GIT_OBJ_COMMIT); - assert_object_oid("refs/stash@{1}", NULL, GIT_OBJ_COMMIT); - - git__free(sha); -} - -void test_stash_save__cannot_stash_when_there_are_no_local_change(void) -{ - git_index *index; - git_oid commit_oid, stash_tip_oid; - - cl_git_pass(git_repository_index(&index, repo)); - - /* - * 'what' and 'who' are being committed. - * 'when' remain untracked. - */ - cl_git_pass(git_index_add_bypath(index, "what")); - cl_git_pass(git_index_add_bypath(index, "who")); - cl_git_pass(git_index_write(index)); - commit_staged_files(&commit_oid, index, signature); - git_index_free(index); - - cl_assert_equal_i(GIT_ENOTFOUND, - git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); - - p_unlink("stash/when"); - cl_assert_equal_i(GIT_ENOTFOUND, - git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED)); -} - -void test_stash_save__can_stage_normal_then_stage_untracked(void) -{ - /* - * $ git ls-tree stash@{1}^0 - * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980 .gitignore - * 100644 blob e6d64adb2c7f3eb8feb493b556cc8070dca379a3 how - * 100644 blob bc99dc98b3eba0e9157e94769cd4d49cb49de449 what - * 100644 blob a0400d4954659306a976567af43125a0b1aa8595 who - * - * $ git ls-tree stash@{1}^1 - * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980 .gitignore - * 100644 blob ac790413e2d7a26c3767e78c57bb28716686eebc how - * 100644 blob ce013625030ba8dba906f756967f9e9ca394464a what - * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71 who - * - * $ git ls-tree stash@{1}^2 - * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980 .gitignore - * 100644 blob e6d64adb2c7f3eb8feb493b556cc8070dca379a3 how - * 100644 blob dd7e1c6f0fefe118f0b63d9f10908c460aa317a6 what - * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71 who - * - * $ git ls-tree stash@{1}^3 - * fatal: Not a valid object name stash@{1}^3 - * - * $ git ls-tree stash@{0}^0 - * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980 .gitignore - * 100644 blob ac790413e2d7a26c3767e78c57bb28716686eebc how - * 100644 blob ce013625030ba8dba906f756967f9e9ca394464a what - * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71 who - * - * $ git ls-tree stash@{0}^1 - * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980 .gitignore - * 100644 blob ac790413e2d7a26c3767e78c57bb28716686eebc how - * 100644 blob ce013625030ba8dba906f756967f9e9ca394464a what - * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71 who - * - * $ git ls-tree stash@{0}^2 - * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980 .gitignore - * 100644 blob ac790413e2d7a26c3767e78c57bb28716686eebc how - * 100644 blob ce013625030ba8dba906f756967f9e9ca394464a what - * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71 who - * - * $ git ls-tree stash@{0}^3 - * 100644 blob b6ed15e81e2593d7bb6265eb4a991d29dc3e628b when - */ - - assert_status("what", GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED); - assert_status("how", GIT_STATUS_INDEX_MODIFIED); - assert_status("who", GIT_STATUS_WT_MODIFIED); - assert_status("when", GIT_STATUS_WT_NEW); - assert_status("just.ignore", GIT_STATUS_IGNORED); - - cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); - assert_status("what", GIT_STATUS_CURRENT); - assert_status("how", GIT_STATUS_CURRENT); - assert_status("who", GIT_STATUS_CURRENT); - assert_status("when", GIT_STATUS_WT_NEW); - assert_status("just.ignore", GIT_STATUS_IGNORED); - - cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED)); - assert_status("what", GIT_STATUS_CURRENT); - assert_status("how", GIT_STATUS_CURRENT); - assert_status("who", GIT_STATUS_CURRENT); - assert_status("when", GIT_ENOTFOUND); - assert_status("just.ignore", GIT_STATUS_IGNORED); - - - assert_blob_oid("stash@{1}^0:what", "bc99dc98b3eba0e9157e94769cd4d49cb49de449"); /* see you later */ - assert_blob_oid("stash@{1}^0:how", "e6d64adb2c7f3eb8feb493b556cc8070dca379a3"); /* not so small and */ - assert_blob_oid("stash@{1}^0:who", "a0400d4954659306a976567af43125a0b1aa8595"); /* funky world */ - assert_blob_oid("stash@{1}^0:when", NULL); - - assert_blob_oid("stash@{1}^2:what", "dd7e1c6f0fefe118f0b63d9f10908c460aa317a6"); /* goodbye */ - assert_blob_oid("stash@{1}^2:how", "e6d64adb2c7f3eb8feb493b556cc8070dca379a3"); /* not so small and */ - assert_blob_oid("stash@{1}^2:who", "cc628ccd10742baea8241c5924df992b5c019f71"); /* world */ - assert_blob_oid("stash@{1}^2:when", NULL); - - assert_object_oid("stash@{1}^3", NULL, GIT_OBJ_COMMIT); - - assert_blob_oid("stash@{0}^0:what", "ce013625030ba8dba906f756967f9e9ca394464a"); /* hello */ - assert_blob_oid("stash@{0}^0:how", "ac790413e2d7a26c3767e78c57bb28716686eebc"); /* small */ - assert_blob_oid("stash@{0}^0:who", "cc628ccd10742baea8241c5924df992b5c019f71"); /* world */ - assert_blob_oid("stash@{0}^0:when", NULL); - - assert_blob_oid("stash@{0}^2:what", "ce013625030ba8dba906f756967f9e9ca394464a"); /* hello */ - assert_blob_oid("stash@{0}^2:how", "ac790413e2d7a26c3767e78c57bb28716686eebc"); /* small */ - assert_blob_oid("stash@{0}^2:who", "cc628ccd10742baea8241c5924df992b5c019f71"); /* world */ - assert_blob_oid("stash@{0}^2:when", NULL); - - assert_blob_oid("stash@{0}^3:when", "b6ed15e81e2593d7bb6265eb4a991d29dc3e628b"); /* now */ -} - -#define EMPTY_TREE "4b825dc642cb6eb9a060e54bf8d69288fbee4904" - -void test_stash_save__including_untracked_without_any_untracked_file_creates_an_empty_tree(void) -{ - cl_git_pass(p_unlink("stash/when")); - - assert_status("what", GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED); - assert_status("how", GIT_STATUS_INDEX_MODIFIED); - assert_status("who", GIT_STATUS_WT_MODIFIED); - assert_status("when", GIT_ENOTFOUND); - assert_status("just.ignore", GIT_STATUS_IGNORED); - - cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED)); - - assert_object_oid("stash^3^{tree}", EMPTY_TREE, GIT_OBJ_TREE); -} diff --git a/tests-clar/stash/stash_helpers.c b/tests-clar/stash/stash_helpers.c deleted file mode 100644 index f462a135179..00000000000 --- a/tests-clar/stash/stash_helpers.c +++ /dev/null @@ -1,68 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" -#include "stash_helpers.h" - -void commit_staged_files( - git_oid *commit_oid, - git_index *index, - git_signature *signature) -{ - git_tree *tree; - git_oid tree_oid; - git_repository *repo; - - repo = git_index_owner(index); - - cl_git_pass(git_index_write_tree(&tree_oid, index)); - - cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid)); - - cl_git_pass(git_commit_create_v( - commit_oid, - repo, - "HEAD", - signature, - signature, - NULL, - "Initial commit", - tree, - 0)); - - git_tree_free(tree); -} - -void setup_stash(git_repository *repo, git_signature *signature) -{ - git_oid commit_oid; - git_index *index; - - cl_git_pass(git_repository_index(&index, repo)); - - cl_git_mkfile("stash/what", "hello\n"); /* ce013625030ba8dba906f756967f9e9ca394464a */ - cl_git_mkfile("stash/how", "small\n"); /* ac790413e2d7a26c3767e78c57bb28716686eebc */ - cl_git_mkfile("stash/who", "world\n"); /* cc628ccd10742baea8241c5924df992b5c019f71 */ - cl_git_mkfile("stash/when", "now\n"); /* b6ed15e81e2593d7bb6265eb4a991d29dc3e628b */ - cl_git_mkfile("stash/just.ignore", "me\n"); /* 78925fb1236b98b37a35e9723033e627f97aa88b */ - - cl_git_mkfile("stash/.gitignore", "*.ignore\n"); - - cl_git_pass(git_index_add_bypath(index, "what")); - cl_git_pass(git_index_add_bypath(index, "how")); - cl_git_pass(git_index_add_bypath(index, "who")); - cl_git_pass(git_index_add_bypath(index, ".gitignore")); - cl_git_pass(git_index_write(index)); - - commit_staged_files(&commit_oid, index, signature); - - cl_git_rewritefile("stash/what", "goodbye\n"); /* dd7e1c6f0fefe118f0b63d9f10908c460aa317a6 */ - cl_git_rewritefile("stash/how", "not so small and\n"); /* e6d64adb2c7f3eb8feb493b556cc8070dca379a3 */ - cl_git_rewritefile("stash/who", "funky world\n"); /* a0400d4954659306a976567af43125a0b1aa8595 */ - - cl_git_pass(git_index_add_bypath(index, "what")); - cl_git_pass(git_index_add_bypath(index, "how")); - cl_git_pass(git_index_write(index)); - - cl_git_rewritefile("stash/what", "see you later\n"); /* bc99dc98b3eba0e9157e94769cd4d49cb49de449 */ - - git_index_free(index); -} diff --git a/tests-clar/stash/stash_helpers.h b/tests-clar/stash/stash_helpers.h deleted file mode 100644 index bb7fec4f539..00000000000 --- a/tests-clar/stash/stash_helpers.h +++ /dev/null @@ -1,8 +0,0 @@ -void setup_stash( - git_repository *repo, - git_signature *signature); - -void commit_staged_files( - git_oid *commit_oid, - git_index *index, - git_signature *signature); \ No newline at end of file diff --git a/tests-clar/status/ignore.c b/tests-clar/status/ignore.c deleted file mode 100644 index e2e4aaf18d9..00000000000 --- a/tests-clar/status/ignore.c +++ /dev/null @@ -1,373 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" -#include "git2/attr.h" -#include "ignore.h" -#include "attr.h" -#include "status_helpers.h" - -static git_repository *g_repo = NULL; - -void test_status_ignore__initialize(void) -{ -} - -void test_status_ignore__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_status_ignore__0(void) -{ - struct { - const char *path; - int expected; - } test_cases[] = { - /* pattern "ign" from .gitignore */ - { "file", 0 }, - { "ign", 1 }, - { "sub", 0 }, - { "sub/file", 0 }, - { "sub/ign", 1 }, - { "sub/ign/file", 1 }, - { "sub/ign/sub", 1 }, - { "sub/ign/sub/file", 1 }, - { "sub/sub", 0 }, - { "sub/sub/file", 0 }, - { "sub/sub/ign", 1 }, - { "sub/sub/sub", 0 }, - /* pattern "dir/" from .gitignore */ - { "dir", 1 }, - { "dir/", 1 }, - { "sub/dir", 1 }, - { "sub/dir/", 1 }, - { "sub/dir/file", 1 }, /* contained in ignored parent */ - { "sub/sub/dir", 0 }, /* dir is not actually a dir, but a file */ - { NULL, 0 } - }, *one_test; - - g_repo = cl_git_sandbox_init("attr"); - - for (one_test = test_cases; one_test->path != NULL; one_test++) { - int ignored; - cl_git_pass(git_status_should_ignore(&ignored, g_repo, one_test->path)); - cl_assert_(ignored == one_test->expected, one_test->path); - } - - /* confirm that ignore files were cached */ - cl_assert(git_attr_cache__is_cached(g_repo, 0, ".git/info/exclude")); - cl_assert(git_attr_cache__is_cached(g_repo, 0, ".gitignore")); -} - - -void test_status_ignore__1(void) -{ - int ignored; - - g_repo = cl_git_sandbox_init("attr"); - - cl_git_rewritefile("attr/.gitignore", "/*.txt\n/dir/\n"); - git_attr_cache_flush(g_repo); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "root_test4.txt")); - cl_assert(ignored); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "sub/subdir_test2.txt")); - cl_assert(!ignored); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "dir")); - cl_assert(ignored); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "dir/")); - cl_assert(ignored); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "sub/dir")); - cl_assert(!ignored); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "sub/dir/")); - cl_assert(!ignored); -} - - -void test_status_ignore__empty_repo_with_gitignore_rewrite(void) -{ - status_entry_single st; - int ignored; - - g_repo = cl_git_sandbox_init("empty_standard_repo"); - - cl_git_mkfile( - "empty_standard_repo/look-ma.txt", "I'm going to be ignored!"); - - memset(&st, 0, sizeof(st)); - cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st)); - cl_assert(st.count == 1); - cl_assert(st.status == GIT_STATUS_WT_NEW); - - cl_git_pass(git_status_file(&st.status, g_repo, "look-ma.txt")); - cl_assert(st.status == GIT_STATUS_WT_NEW); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "look-ma.txt")); - cl_assert(!ignored); - - cl_git_rewritefile("empty_standard_repo/.gitignore", "*.nomatch\n"); - - memset(&st, 0, sizeof(st)); - cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st)); - cl_assert(st.count == 2); - cl_assert(st.status == GIT_STATUS_WT_NEW); - - cl_git_pass(git_status_file(&st.status, g_repo, "look-ma.txt")); - cl_assert(st.status == GIT_STATUS_WT_NEW); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "look-ma.txt")); - cl_assert(!ignored); - - cl_git_rewritefile("empty_standard_repo/.gitignore", "*.txt\n"); - - memset(&st, 0, sizeof(st)); - cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st)); - cl_assert(st.count == 2); - cl_assert(st.status == GIT_STATUS_IGNORED); - - cl_git_pass(git_status_file(&st.status, g_repo, "look-ma.txt")); - cl_assert(st.status == GIT_STATUS_IGNORED); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "look-ma.txt")); - cl_assert(ignored); -} - -void test_status_ignore__ignore_pattern_contains_space(void) -{ - unsigned int flags; - const mode_t mode = 0777; - - g_repo = cl_git_sandbox_init("empty_standard_repo"); - cl_git_rewritefile("empty_standard_repo/.gitignore", "foo bar.txt\n"); - - cl_git_mkfile( - "empty_standard_repo/foo bar.txt", "I'm going to be ignored!"); - - cl_git_pass(git_status_file(&flags, g_repo, "foo bar.txt")); - cl_assert(flags == GIT_STATUS_IGNORED); - - cl_git_pass(git_futils_mkdir_r("empty_standard_repo/foo", NULL, mode)); - cl_git_mkfile("empty_standard_repo/foo/look-ma.txt", "I'm not going to be ignored!"); - - cl_git_pass(git_status_file(&flags, g_repo, "foo/look-ma.txt")); - cl_assert(flags == GIT_STATUS_WT_NEW); -} - -void test_status_ignore__ignore_pattern_ignorecase(void) -{ - unsigned int flags; - bool ignore_case; - git_index *index; - - g_repo = cl_git_sandbox_init("empty_standard_repo"); - cl_git_rewritefile("empty_standard_repo/.gitignore", "a.txt\n"); - - cl_git_mkfile("empty_standard_repo/A.txt", "Differs in case"); - - cl_git_pass(git_repository_index(&index, g_repo)); - ignore_case = index->ignore_case; - git_index_free(index); - - cl_git_pass(git_status_file(&flags, g_repo, "A.txt")); - cl_assert(flags == ignore_case ? GIT_STATUS_IGNORED : GIT_STATUS_WT_NEW); -} - -void test_status_ignore__subdirectories(void) -{ - status_entry_single st; - int ignored; - - g_repo = cl_git_sandbox_init("empty_standard_repo"); - - cl_git_mkfile( - "empty_standard_repo/ignore_me", "I'm going to be ignored!"); - - cl_git_rewritefile("empty_standard_repo/.gitignore", "ignore_me\n"); - - memset(&st, 0, sizeof(st)); - cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st)); - cl_assert_equal_i(2, st.count); - cl_assert(st.status == GIT_STATUS_IGNORED); - - cl_git_pass(git_status_file(&st.status, g_repo, "ignore_me")); - cl_assert(st.status == GIT_STATUS_IGNORED); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "ignore_me")); - cl_assert(ignored); - - - /* So, interestingly, as per the comment in diff_from_iterators() the - * following file is ignored, but in a way so that it does not show up - * in status even if INCLUDE_IGNORED is used. This actually matches - * core git's behavior - if you follow these steps and try running "git - * status -uall --ignored" then the following file and directory will - * not show up in the output at all. - */ - - cl_git_pass( - git_futils_mkdir_r("empty_standard_repo/test/ignore_me", NULL, 0775)); - cl_git_mkfile( - "empty_standard_repo/test/ignore_me/file", "I'm going to be ignored!"); - - memset(&st, 0, sizeof(st)); - cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st)); - cl_assert_equal_i(2, st.count); - - cl_git_pass(git_status_file(&st.status, g_repo, "test/ignore_me/file")); - cl_assert(st.status == GIT_STATUS_IGNORED); - - cl_git_pass( - git_status_should_ignore(&ignored, g_repo, "test/ignore_me/file")); - cl_assert(ignored); -} - -void test_status_ignore__adding_internal_ignores(void) -{ - int ignored; - - g_repo = cl_git_sandbox_init("empty_standard_repo"); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "one.txt")); - cl_assert(!ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "two.bar")); - cl_assert(!ignored); - - cl_git_pass(git_ignore_add_rule(g_repo, "*.nomatch\n")); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "one.txt")); - cl_assert(!ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "two.bar")); - cl_assert(!ignored); - - cl_git_pass(git_ignore_add_rule(g_repo, "*.txt\n")); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "one.txt")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "two.bar")); - cl_assert(!ignored); - - cl_git_pass(git_ignore_add_rule(g_repo, "*.bar\n")); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "one.txt")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "two.bar")); - cl_assert(ignored); - - cl_git_pass(git_ignore_clear_internal_rules(g_repo)); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "one.txt")); - cl_assert(!ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "two.bar")); - cl_assert(!ignored); - - cl_git_pass(git_ignore_add_rule( - g_repo, "multiple\n*.rules\n# comment line\n*.bar\n")); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "one.txt")); - cl_assert(!ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "two.bar")); - cl_assert(ignored); -} - -void test_status_ignore__add_internal_as_first_thing(void) -{ - int ignored; - const char *add_me = "\n#################\n## Eclipse\n#################\n\n*.pydevproject\n.project\n.metadata\nbin/\ntmp/\n*.tmp\n\n"; - - g_repo = cl_git_sandbox_init("empty_standard_repo"); - - cl_git_pass(git_ignore_add_rule(g_repo, add_me)); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "one.tmp")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "two.bar")); - cl_assert(!ignored); -} - -void test_status_ignore__internal_ignores_inside_deep_paths(void) -{ - int ignored; - const char *add_me = "Debug\nthis/is/deep\npatterned*/dir\n"; - - g_repo = cl_git_sandbox_init("empty_standard_repo"); - - cl_git_pass(git_ignore_add_rule(g_repo, add_me)); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "Debug")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "and/Debug")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "really/Debug/this/file")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "Debug/what/I/say")); - cl_assert(ignored); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "and/NoDebug")); - cl_assert(!ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "NoDebug/this")); - cl_assert(!ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "please/NoDebug/this")); - cl_assert(!ignored); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "this/is/deep")); - cl_assert(ignored); - /* pattern containing slash gets FNM_PATHNAME so all slashes must match */ - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "and/this/is/deep")); - cl_assert(!ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "this/is/deep/too")); - cl_assert(ignored); - /* pattern containing slash gets FNM_PATHNAME so all slashes must match */ - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "but/this/is/deep/and/ignored")); - cl_assert(!ignored); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "this/is/not/deep")); - cl_assert(!ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "is/this/not/as/deep")); - cl_assert(!ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "this/is/deepish")); - cl_assert(!ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "xthis/is/deep")); - cl_assert(!ignored); -} - -void test_status_ignore__automatically_ignore_bad_files(void) -{ - int ignored; - - g_repo = cl_git_sandbox_init("empty_standard_repo"); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, ".git")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "this/file/.")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "path/../funky")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "path/whatever.c")); - cl_assert(!ignored); - - cl_git_pass(git_ignore_add_rule(g_repo, "*.c\n")); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, ".git")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "this/file/.")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "path/../funky")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "path/whatever.c")); - cl_assert(ignored); - - cl_git_pass(git_ignore_clear_internal_rules(g_repo)); - - cl_git_pass(git_status_should_ignore(&ignored, g_repo, ".git")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "this/file/.")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "path/../funky")); - cl_assert(ignored); - cl_git_pass(git_status_should_ignore(&ignored, g_repo, "path/whatever.c")); - cl_assert(!ignored); -} diff --git a/tests-clar/status/single.c b/tests-clar/status/single.c deleted file mode 100644 index 292c9120a84..00000000000 --- a/tests-clar/status/single.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "clar_libgit2.h" -#include "posix.h" - -static void -cleanup__remove_file(void *_file) -{ - cl_must_pass(p_unlink((char *)_file)); -} - -/* test retrieving OID from a file apart from the ODB */ -void test_status_single__hash_single_file(void) -{ - static const char file_name[] = "new_file"; - static const char file_contents[] = "new_file\n"; - static const char file_hash[] = "d4fa8600b4f37d7516bef4816ae2c64dbf029e3a"; - - git_oid expected_id, actual_id; - - /* initialization */ - git_oid_fromstr(&expected_id, file_hash); - cl_git_mkfile(file_name, file_contents); - cl_set_cleanup(&cleanup__remove_file, (void *)file_name); - - cl_git_pass(git_odb_hashfile(&actual_id, file_name, GIT_OBJ_BLOB)); - cl_assert(git_oid_cmp(&expected_id, &actual_id) == 0); -} - -/* test retrieving OID from an empty file apart from the ODB */ -void test_status_single__hash_single_empty_file(void) -{ - static const char file_name[] = "new_empty_file"; - static const char file_contents[] = ""; - static const char file_hash[] = "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"; - - git_oid expected_id, actual_id; - - /* initialization */ - git_oid_fromstr(&expected_id, file_hash); - cl_git_mkfile(file_name, file_contents); - cl_set_cleanup(&cleanup__remove_file, (void *)file_name); - - cl_git_pass(git_odb_hashfile(&actual_id, file_name, GIT_OBJ_BLOB)); - cl_assert(git_oid_cmp(&expected_id, &actual_id) == 0); -} - diff --git a/tests-clar/status/status_data.h b/tests-clar/status/status_data.h deleted file mode 100644 index a41bde7c2ea..00000000000 --- a/tests-clar/status/status_data.h +++ /dev/null @@ -1,252 +0,0 @@ -#include "status_helpers.h" - -/* entries for a plain copy of tests/resources/status */ - -static const char *entry_paths0[] = { - "file_deleted", - "ignored_file", - "modified_file", - "new_file", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_delete_file_deleted", - "staged_delete_modified_file", - "staged_new_file", - "staged_new_file_deleted_file", - "staged_new_file_modified_file", - - "subdir/deleted_file", - "subdir/modified_file", - "subdir/new_file", - - "\xe8\xbf\x99", -}; - -static const unsigned int entry_statuses0[] = { - GIT_STATUS_WT_DELETED, - GIT_STATUS_IGNORED, - GIT_STATUS_WT_MODIFIED, - GIT_STATUS_WT_NEW, - GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_DELETED, - GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_MODIFIED, - GIT_STATUS_INDEX_DELETED, - GIT_STATUS_INDEX_DELETED | GIT_STATUS_WT_NEW, - GIT_STATUS_INDEX_NEW, - GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_DELETED, - GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_MODIFIED, - - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_MODIFIED, - GIT_STATUS_WT_NEW, - - GIT_STATUS_WT_NEW, -}; - -static const int entry_count0 = 16; - -/* entries for a copy of tests/resources/status with all content - * deleted from the working directory - */ - -static const char *entry_paths2[] = { - "current_file", - "file_deleted", - "modified_file", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_delete_file_deleted", - "staged_delete_modified_file", - "staged_new_file", - "staged_new_file_deleted_file", - "staged_new_file_modified_file", - "subdir.txt", - "subdir/current_file", - "subdir/deleted_file", - "subdir/modified_file", -}; - -static const unsigned int entry_statuses2[] = { - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_INDEX_DELETED, - GIT_STATUS_INDEX_DELETED, - GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW, - GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW, - GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_DELETED, -}; - -static const int entry_count2 = 15; - -/* entries for a copy of tests/resources/status with some mods */ - -static const char *entry_paths3_icase[] = { - ".HEADER", - "42-is-not-prime.sigh", - "current_file", - "current_file/", - "file_deleted", - "ignored_file", - "modified_file", - "new_file", - "README.md", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_delete_file_deleted", - "staged_delete_modified_file", - "staged_new_file", - "staged_new_file_deleted_file", - "staged_new_file_modified_file", - "subdir", - "subdir/current_file", - "subdir/deleted_file", - "subdir/modified_file", - "\xe8\xbf\x99", -}; - -static const unsigned int entry_statuses3_icase[] = { - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_DELETED, - GIT_STATUS_IGNORED, - GIT_STATUS_WT_MODIFIED, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_NEW, - GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_INDEX_DELETED, - GIT_STATUS_WT_NEW | GIT_STATUS_INDEX_DELETED, - GIT_STATUS_INDEX_NEW, - GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW, - GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_NEW, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_NEW, -}; - -static const char *entry_paths3[] = { - ".HEADER", - "42-is-not-prime.sigh", - "README.md", - "current_file", - "current_file/", - "file_deleted", - "ignored_file", - "modified_file", - "new_file", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_delete_file_deleted", - "staged_delete_modified_file", - "staged_new_file", - "staged_new_file_deleted_file", - "staged_new_file_modified_file", - "subdir", - "subdir/current_file", - "subdir/deleted_file", - "subdir/modified_file", - "\xe8\xbf\x99", -}; - -static const unsigned int entry_statuses3[] = { - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_DELETED, - GIT_STATUS_IGNORED, - GIT_STATUS_WT_MODIFIED, - GIT_STATUS_WT_NEW, - GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_INDEX_DELETED, - GIT_STATUS_WT_NEW | GIT_STATUS_INDEX_DELETED, - GIT_STATUS_INDEX_NEW, - GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW, - GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_NEW, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_NEW, -}; - -static const int entry_count3 = 22; - - -/* entries for a copy of tests/resources/status with some mods - * and different options to the status call - */ - -static const char *entry_paths4[] = { - ".new_file", - "current_file", - "current_file/current_file", - "current_file/modified_file", - "current_file/new_file", - "file_deleted", - "modified_file", - "new_file", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_delete_file_deleted", - "staged_delete_modified_file", - "staged_new_file", - "staged_new_file_deleted_file", - "staged_new_file_modified_file", - "subdir", - "subdir/current_file", - "subdir/deleted_file", - "subdir/modified_file", - "zzz_new_dir/new_file", - "zzz_new_file", - "\xe8\xbf\x99", -}; - -static const unsigned int entry_statuses4[] = { - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_MODIFIED, - GIT_STATUS_WT_NEW, - GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_INDEX_DELETED, - GIT_STATUS_WT_NEW | GIT_STATUS_INDEX_DELETED, - GIT_STATUS_INDEX_NEW, - GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW, - GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_NEW, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_DELETED, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_NEW, - GIT_STATUS_WT_NEW, -}; - -static const int entry_count4 = 23; diff --git a/tests-clar/status/status_helpers.c b/tests-clar/status/status_helpers.c deleted file mode 100644 index 3dbf43a5b9c..00000000000 --- a/tests-clar/status/status_helpers.c +++ /dev/null @@ -1,49 +0,0 @@ -#include "clar_libgit2.h" -#include "status_helpers.h" - -int cb_status__normal( - const char *path, unsigned int status_flags, void *payload) -{ - status_entry_counts *counts = payload; - - if (counts->entry_count >= counts->expected_entry_count) { - counts->wrong_status_flags_count++; - goto exit; - } - - if (strcmp(path, counts->expected_paths[counts->entry_count])) { - counts->wrong_sorted_path++; - goto exit; - } - - if (status_flags != counts->expected_statuses[counts->entry_count]) - counts->wrong_status_flags_count++; - -exit: - counts->entry_count++; - return 0; -} - -int cb_status__count(const char *p, unsigned int s, void *payload) -{ - volatile int *count = (int *)payload; - - GIT_UNUSED(p); - GIT_UNUSED(s); - - (*count)++; - - return 0; -} - -int cb_status__single(const char *p, unsigned int s, void *payload) -{ - status_entry_single *data = (status_entry_single *)payload; - - GIT_UNUSED(p); - - data->count++; - data->status = s; - - return 0; -} diff --git a/tests-clar/status/status_helpers.h b/tests-clar/status/status_helpers.h deleted file mode 100644 index 3f9c1f57d1b..00000000000 --- a/tests-clar/status/status_helpers.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef INCLUDE_cl_status_helpers_h__ -#define INCLUDE_cl_status_helpers_h__ - -typedef struct { - int wrong_status_flags_count; - int wrong_sorted_path; - int entry_count; - const unsigned int* expected_statuses; - const char** expected_paths; - int expected_entry_count; -} status_entry_counts; - -/* cb_status__normal takes payload of "status_entry_counts *" */ - -extern int cb_status__normal( - const char *path, unsigned int status_flags, void *payload); - - -/* cb_status__count takes payload of "int *" */ - -extern int cb_status__count(const char *p, unsigned int s, void *payload); - - -typedef struct { - int count; - unsigned int status; -} status_entry_single; - -/* cb_status__single takes payload of "status_entry_single *" */ - -extern int cb_status__single(const char *p, unsigned int s, void *payload); - -#endif diff --git a/tests-clar/status/submodules.c b/tests-clar/status/submodules.c deleted file mode 100644 index 24dd660aba9..00000000000 --- a/tests-clar/status/submodules.c +++ /dev/null @@ -1,106 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "path.h" -#include "posix.h" -#include "status_helpers.h" -#include "../submodule/submodule_helpers.h" - -static git_repository *g_repo = NULL; - -void test_status_submodules__initialize(void) -{ - g_repo = cl_git_sandbox_init("submodules"); - - cl_fixture_sandbox("testrepo.git"); - - rewrite_gitmodules(git_repository_workdir(g_repo)); - - p_rename("submodules/testrepo/.gitted", "submodules/testrepo/.git"); -} - -void test_status_submodules__cleanup(void) -{ - cl_git_sandbox_cleanup(); - cl_fixture_cleanup("testrepo.git"); -} - -void test_status_submodules__api(void) -{ - git_submodule *sm; - - cl_assert(git_submodule_lookup(NULL, g_repo, "nonexistent") == GIT_ENOTFOUND); - - cl_assert(git_submodule_lookup(NULL, g_repo, "modified") == GIT_ENOTFOUND); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo")); - cl_assert(sm != NULL); - cl_assert_equal_s("testrepo", git_submodule_name(sm)); - cl_assert_equal_s("testrepo", git_submodule_path(sm)); -} - -void test_status_submodules__0(void) -{ - int counts = 0; - - cl_assert(git_path_isdir("submodules/.git")); - cl_assert(git_path_isdir("submodules/testrepo/.git")); - cl_assert(git_path_isfile("submodules/.gitmodules")); - - cl_git_pass( - git_status_foreach(g_repo, cb_status__count, &counts) - ); - - cl_assert_equal_i(6, counts); -} - -static const char *expected_files[] = { - ".gitmodules", - "added", - "deleted", - "ignored", - "modified", - "untracked" -}; - -static unsigned int expected_status[] = { - GIT_STATUS_WT_MODIFIED, - GIT_STATUS_INDEX_NEW, - GIT_STATUS_INDEX_DELETED, - GIT_STATUS_IGNORED, - GIT_STATUS_WT_MODIFIED, - GIT_STATUS_WT_NEW -}; - -static int -cb_status__match(const char *p, unsigned int s, void *payload) -{ - volatile int *index = (int *)payload; - - cl_assert_equal_s(expected_files[*index], p); - cl_assert(expected_status[*index] == s); - (*index)++; - - return 0; -} - -void test_status_submodules__1(void) -{ - int index = 0; - - cl_assert(git_path_isdir("submodules/.git")); - cl_assert(git_path_isdir("submodules/testrepo/.git")); - cl_assert(git_path_isfile("submodules/.gitmodules")); - - cl_git_pass( - git_status_foreach(g_repo, cb_status__match, &index) - ); - - cl_assert_equal_i(6, index); -} - -void test_status_submodules__single_file(void) -{ - unsigned int status = 0; - cl_git_pass( git_status_file(&status, g_repo, "testrepo") ); - cl_assert(!status); -} diff --git a/tests-clar/status/worktree.c b/tests-clar/status/worktree.c deleted file mode 100644 index e1fc8dff8e3..00000000000 --- a/tests-clar/status/worktree.c +++ /dev/null @@ -1,669 +0,0 @@ -#include "clar_libgit2.h" -#include "fileops.h" -#include "ignore.h" -#include "status_data.h" -#include "posix.h" -#include "util.h" -#include "path.h" - -/** - * Cleanup - * - * This will be called once after each test finishes, even - * if the test failed - */ -void test_status_worktree__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -/** - * Tests - Status determination on a working tree - */ -/* this test is equivalent to t18-status.c:statuscb0 */ -void test_status_worktree__whole_repository(void) -{ - status_entry_counts counts; - git_repository *repo = cl_git_sandbox_init("status"); - - memset(&counts, 0x0, sizeof(status_entry_counts)); - counts.expected_entry_count = entry_count0; - counts.expected_paths = entry_paths0; - counts.expected_statuses = entry_statuses0; - - cl_git_pass( - git_status_foreach(repo, cb_status__normal, &counts) - ); - - cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); - cl_assert_equal_i(0, counts.wrong_status_flags_count); - cl_assert_equal_i(0, counts.wrong_sorted_path); -} - -/* this test is equivalent to t18-status.c:statuscb1 */ -void test_status_worktree__empty_repository(void) -{ - int count = 0; - git_repository *repo = cl_git_sandbox_init("empty_standard_repo"); - - cl_git_pass(git_status_foreach(repo, cb_status__count, &count)); - - cl_assert_equal_i(0, count); -} - -static int remove_file_cb(void *data, git_buf *file) -{ - const char *filename = git_buf_cstr(file); - - GIT_UNUSED(data); - - if (git__suffixcmp(filename, ".git") == 0) - return 0; - - if (git_path_isdir(filename)) - cl_git_pass(git_futils_rmdir_r(filename, NULL, GIT_RMDIR_REMOVE_FILES)); - else - cl_git_pass(p_unlink(git_buf_cstr(file))); - - return 0; -} - -/* this test is equivalent to t18-status.c:statuscb2 */ -void test_status_worktree__purged_worktree(void) -{ - status_entry_counts counts; - git_repository *repo = cl_git_sandbox_init("status"); - git_buf workdir = GIT_BUF_INIT; - - /* first purge the contents of the worktree */ - cl_git_pass(git_buf_sets(&workdir, git_repository_workdir(repo))); - cl_git_pass(git_path_direach(&workdir, remove_file_cb, NULL)); - git_buf_free(&workdir); - - /* now get status */ - memset(&counts, 0x0, sizeof(status_entry_counts)); - counts.expected_entry_count = entry_count2; - counts.expected_paths = entry_paths2; - counts.expected_statuses = entry_statuses2; - - cl_git_pass( - git_status_foreach(repo, cb_status__normal, &counts) - ); - - cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); - cl_assert_equal_i(0, counts.wrong_status_flags_count); - cl_assert_equal_i(0, counts.wrong_sorted_path); -} - -/* this test is similar to t18-status.c:statuscb3 */ -void test_status_worktree__swap_subdir_and_file(void) -{ - status_entry_counts counts; - git_repository *repo = cl_git_sandbox_init("status"); - git_index *index; - git_status_options opts = GIT_STATUS_OPTIONS_INIT; - bool ignore_case; - - cl_git_pass(git_repository_index(&index, repo)); - ignore_case = index->ignore_case; - git_index_free(index); - - /* first alter the contents of the worktree */ - cl_git_pass(p_rename("status/current_file", "status/swap")); - cl_git_pass(p_rename("status/subdir", "status/current_file")); - cl_git_pass(p_rename("status/swap", "status/subdir")); - - cl_git_mkfile("status/.HEADER", "dummy"); - cl_git_mkfile("status/42-is-not-prime.sigh", "dummy"); - cl_git_mkfile("status/README.md", "dummy"); - - /* now get status */ - memset(&counts, 0x0, sizeof(status_entry_counts)); - counts.expected_entry_count = entry_count3; - counts.expected_paths = ignore_case ? entry_paths3_icase : entry_paths3; - counts.expected_statuses = ignore_case ? entry_statuses3_icase : entry_statuses3; - - opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | - GIT_STATUS_OPT_INCLUDE_IGNORED; - - cl_git_pass( - git_status_foreach_ext(repo, &opts, cb_status__normal, &counts) - ); - - cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); - cl_assert_equal_i(0, counts.wrong_status_flags_count); - cl_assert_equal_i(0, counts.wrong_sorted_path); -} - -void test_status_worktree__swap_subdir_with_recurse_and_pathspec(void) -{ - status_entry_counts counts; - git_repository *repo = cl_git_sandbox_init("status"); - git_status_options opts = GIT_STATUS_OPTIONS_INIT; - - /* first alter the contents of the worktree */ - cl_git_pass(p_rename("status/current_file", "status/swap")); - cl_git_pass(p_rename("status/subdir", "status/current_file")); - cl_git_pass(p_rename("status/swap", "status/subdir")); - cl_git_mkfile("status/.new_file", "dummy"); - cl_git_pass(git_futils_mkdir_r("status/zzz_new_dir", NULL, 0777)); - cl_git_mkfile("status/zzz_new_dir/new_file", "dummy"); - cl_git_mkfile("status/zzz_new_file", "dummy"); - - /* now get status */ - memset(&counts, 0x0, sizeof(status_entry_counts)); - counts.expected_entry_count = entry_count4; - counts.expected_paths = entry_paths4; - counts.expected_statuses = entry_statuses4; - - opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | - GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS; - /* TODO: set pathspec to "current_file" eventually */ - - cl_git_pass( - git_status_foreach_ext(repo, &opts, cb_status__normal, &counts) - ); - - cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); - cl_assert_equal_i(0, counts.wrong_status_flags_count); - cl_assert_equal_i(0, counts.wrong_sorted_path); -} - -/* this test is equivalent to t18-status.c:singlestatus0 */ -void test_status_worktree__single_file(void) -{ - int i; - unsigned int status_flags; - git_repository *repo = cl_git_sandbox_init("status"); - - for (i = 0; i < (int)entry_count0; i++) { - cl_git_pass( - git_status_file(&status_flags, repo, entry_paths0[i]) - ); - cl_assert(entry_statuses0[i] == status_flags); - } -} - -/* this test is equivalent to t18-status.c:singlestatus1 */ -void test_status_worktree__single_nonexistent_file(void) -{ - int error; - unsigned int status_flags; - git_repository *repo = cl_git_sandbox_init("status"); - - error = git_status_file(&status_flags, repo, "nonexistent"); - cl_git_fail(error); - cl_assert(error == GIT_ENOTFOUND); -} - -/* this test is equivalent to t18-status.c:singlestatus2 */ -void test_status_worktree__single_nonexistent_file_empty_repo(void) -{ - int error; - unsigned int status_flags; - git_repository *repo = cl_git_sandbox_init("empty_standard_repo"); - - error = git_status_file(&status_flags, repo, "nonexistent"); - cl_git_fail(error); - cl_assert(error == GIT_ENOTFOUND); -} - -/* this test is equivalent to t18-status.c:singlestatus3 */ -void test_status_worktree__single_file_empty_repo(void) -{ - unsigned int status_flags; - git_repository *repo = cl_git_sandbox_init("empty_standard_repo"); - - cl_git_mkfile("empty_standard_repo/new_file", "new_file\n"); - - cl_git_pass(git_status_file(&status_flags, repo, "new_file")); - cl_assert(status_flags == GIT_STATUS_WT_NEW); -} - -/* this test is equivalent to t18-status.c:singlestatus4 */ -void test_status_worktree__single_folder(void) -{ - int error; - unsigned int status_flags; - git_repository *repo = cl_git_sandbox_init("status"); - - error = git_status_file(&status_flags, repo, "subdir"); - cl_git_fail(error); - cl_assert(error != GIT_ENOTFOUND); -} - - -void test_status_worktree__ignores(void) -{ - int i, ignored; - git_repository *repo = cl_git_sandbox_init("status"); - - for (i = 0; i < (int)entry_count0; i++) { - cl_git_pass( - git_status_should_ignore(&ignored, repo, entry_paths0[i]) - ); - cl_assert(ignored == (entry_statuses0[i] == GIT_STATUS_IGNORED)); - } - - cl_git_pass( - git_status_should_ignore(&ignored, repo, "nonexistent_file") - ); - cl_assert(!ignored); - - cl_git_pass( - git_status_should_ignore(&ignored, repo, "ignored_nonexistent_file") - ); - cl_assert(ignored); -} - -static int cb_status__check_592(const char *p, unsigned int s, void *payload) -{ - GIT_UNUSED(payload); - - if (s != GIT_STATUS_WT_DELETED || (payload != NULL && strcmp(p, (const char *)payload) != 0)) - return -1; - - return 0; -} - -void test_status_worktree__issue_592(void) -{ - git_repository *repo; - git_buf path = GIT_BUF_INIT; - - repo = cl_git_sandbox_init("issue_592"); - cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(repo), "l.txt")); - cl_git_pass(p_unlink(git_buf_cstr(&path))); - - cl_git_pass(git_status_foreach(repo, cb_status__check_592, "l.txt")); - - git_buf_free(&path); -} - -void test_status_worktree__issue_592_2(void) -{ - git_repository *repo; - git_buf path = GIT_BUF_INIT; - - repo = cl_git_sandbox_init("issue_592"); - cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(repo), "c/a.txt")); - cl_git_pass(p_unlink(git_buf_cstr(&path))); - - cl_git_pass(git_status_foreach(repo, cb_status__check_592, "c/a.txt")); - - git_buf_free(&path); -} - -void test_status_worktree__issue_592_3(void) -{ - git_repository *repo; - git_buf path = GIT_BUF_INIT; - - repo = cl_git_sandbox_init("issue_592"); - - cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(repo), "c")); - cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES)); - - cl_git_pass(git_status_foreach(repo, cb_status__check_592, "c/a.txt")); - - git_buf_free(&path); -} - -void test_status_worktree__issue_592_4(void) -{ - git_repository *repo; - git_buf path = GIT_BUF_INIT; - - repo = cl_git_sandbox_init("issue_592"); - - cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(repo), "t/b.txt")); - cl_git_pass(p_unlink(git_buf_cstr(&path))); - - cl_git_pass(git_status_foreach(repo, cb_status__check_592, "t/b.txt")); - - git_buf_free(&path); -} - -void test_status_worktree__issue_592_5(void) -{ - git_repository *repo; - git_buf path = GIT_BUF_INIT; - - repo = cl_git_sandbox_init("issue_592"); - - cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(repo), "t")); - cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES)); - cl_git_pass(p_mkdir(git_buf_cstr(&path), 0777)); - - cl_git_pass(git_status_foreach(repo, cb_status__check_592, NULL)); - - git_buf_free(&path); -} - -void test_status_worktree__issue_592_ignores_0(void) -{ - int count = 0; - status_entry_single st; - git_repository *repo = cl_git_sandbox_init("issue_592"); - - cl_git_pass(git_status_foreach(repo, cb_status__count, &count)); - cl_assert_equal_i(0, count); - - cl_git_rewritefile("issue_592/.gitignore", - ".gitignore\n*.txt\nc/\n[tT]*/\n"); - - cl_git_pass(git_status_foreach(repo, cb_status__count, &count)); - cl_assert_equal_i(1, count); - - /* This is a situation where the behavior of libgit2 is - * different from core git. Core git will show ignored.txt - * in the list of ignored files, even though the directory - * "t" is ignored and the file is untracked because we have - * the explicit "*.txt" ignore rule. Libgit2 just excludes - * all untracked files that are contained within ignored - * directories without explicitly listing them. - */ - cl_git_rewritefile("issue_592/t/ignored.txt", "ping"); - - memset(&st, 0, sizeof(st)); - cl_git_pass(git_status_foreach(repo, cb_status__single, &st)); - cl_assert_equal_i(1, st.count); - cl_assert(st.status == GIT_STATUS_IGNORED); - - cl_git_rewritefile("issue_592/c/ignored_by_dir", "ping"); - - memset(&st, 0, sizeof(st)); - cl_git_pass(git_status_foreach(repo, cb_status__single, &st)); - cl_assert_equal_i(1, st.count); - cl_assert(st.status == GIT_STATUS_IGNORED); - - cl_git_rewritefile("issue_592/t/ignored_by_dir_pattern", "ping"); - - memset(&st, 0, sizeof(st)); - cl_git_pass(git_status_foreach(repo, cb_status__single, &st)); - cl_assert_equal_i(1, st.count); - cl_assert(st.status == GIT_STATUS_IGNORED); -} - -void test_status_worktree__issue_592_ignored_dirs_with_tracked_content(void) -{ - int count = 0; - git_repository *repo = cl_git_sandbox_init("issue_592b"); - - cl_git_pass(git_status_foreach(repo, cb_status__count, &count)); - cl_assert_equal_i(1, count); - - /* if we are really mimicking core git, then only ignored1.txt - * at the top level will show up in the ignores list here. - * everything else will be unmodified or skipped completely. - */ -} - -void test_status_worktree__conflict_with_diff3(void) -{ - git_repository *repo = cl_git_sandbox_init("status"); - git_index *index; - unsigned int status; - git_index_entry ancestor_entry, our_entry, their_entry; - - memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); - memset(&our_entry, 0x0, sizeof(git_index_entry)); - memset(&their_entry, 0x0, sizeof(git_index_entry)); - - ancestor_entry.path = "modified_file"; - git_oid_fromstr(&ancestor_entry.oid, - "452e4244b5d083ddf0460acf1ecc74db9dcfa11a"); - - our_entry.path = "modified_file"; - git_oid_fromstr(&our_entry.oid, - "452e4244b5d083ddf0460acf1ecc74db9dcfa11a"); - - their_entry.path = "modified_file"; - git_oid_fromstr(&their_entry.oid, - "452e4244b5d083ddf0460acf1ecc74db9dcfa11a"); - - cl_git_pass(git_status_file(&status, repo, "modified_file")); - cl_assert_equal_i(GIT_STATUS_WT_MODIFIED, status); - - cl_git_pass(git_repository_index(&index, repo)); - - cl_git_pass(git_index_remove(index, "modified_file", 0)); - cl_git_pass(git_index_conflict_add(index, &ancestor_entry, - &our_entry, &their_entry)); - - cl_git_pass(git_status_file(&status, repo, "modified_file")); - - cl_assert_equal_i(GIT_STATUS_INDEX_DELETED | GIT_STATUS_WT_NEW, status); - - git_index_free(index); -} - -static const char *filemode_paths[] = { - "exec_off", - "exec_off2on_staged", - "exec_off2on_workdir", - "exec_off_untracked", - "exec_on", - "exec_on2off_staged", - "exec_on2off_workdir", - "exec_on_untracked", -}; - -static unsigned int filemode_statuses[] = { - GIT_STATUS_CURRENT, - GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_WT_MODIFIED, - GIT_STATUS_WT_NEW, - GIT_STATUS_CURRENT, - GIT_STATUS_INDEX_MODIFIED, - GIT_STATUS_WT_MODIFIED, - GIT_STATUS_WT_NEW -}; - -static const int filemode_count = 8; - -void test_status_worktree__filemode_changes(void) -{ - git_repository *repo = cl_git_sandbox_init("filemodes"); - status_entry_counts counts; - git_status_options opts = GIT_STATUS_OPTIONS_INIT; - git_config *cfg; - - /* overwrite stored filemode with platform appropriate value */ - cl_git_pass(git_repository_config(&cfg, repo)); - if (cl_is_chmod_supported()) - cl_git_pass(git_config_set_bool(cfg, "core.filemode", true)); - else { - int i; - cl_git_pass(git_config_set_bool(cfg, "core.filemode", false)); - - /* won't trust filesystem mode diffs, so these will appear unchanged */ - for (i = 0; i < filemode_count; ++i) - if (filemode_statuses[i] == GIT_STATUS_WT_MODIFIED) - filemode_statuses[i] = GIT_STATUS_CURRENT; - } - - opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | - GIT_STATUS_OPT_INCLUDE_IGNORED | - GIT_STATUS_OPT_INCLUDE_UNMODIFIED; - - memset(&counts, 0, sizeof(counts)); - counts.expected_entry_count = filemode_count; - counts.expected_paths = filemode_paths; - counts.expected_statuses = filemode_statuses; - - cl_git_pass( - git_status_foreach_ext(repo, &opts, cb_status__normal, &counts) - ); - - cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); - cl_assert_equal_i(0, counts.wrong_status_flags_count); - cl_assert_equal_i(0, counts.wrong_sorted_path); - - git_config_free(cfg); -} - -static int cb_status__interrupt(const char *p, unsigned int s, void *payload) -{ - volatile int *count = (int *)payload; - - GIT_UNUSED(p); - GIT_UNUSED(s); - - (*count)++; - - return (*count == 8); -} - -void test_status_worktree__interruptable_foreach(void) -{ - int count = 0; - git_repository *repo = cl_git_sandbox_init("status"); - - cl_assert_equal_i( - GIT_EUSER, git_status_foreach(repo, cb_status__interrupt, &count) - ); - - cl_assert_equal_i(8, count); -} - -void test_status_worktree__line_endings_dont_count_as_changes_with_autocrlf(void) -{ - git_repository *repo = cl_git_sandbox_init("status"); - git_config *config; - unsigned int status; - - cl_git_pass(git_repository_config(&config, repo)); - cl_git_pass(git_config_set_bool(config, "core.autocrlf", true)); - git_config_free(config); - - cl_git_rewritefile("status/current_file", "current_file\r\n"); - - cl_git_pass(git_status_file(&status, repo, "current_file")); - - cl_assert_equal_i(GIT_STATUS_CURRENT, status); -} - -void test_status_worktree__conflicted_item(void) -{ - git_repository *repo = cl_git_sandbox_init("status"); - git_index *index; - unsigned int status; - git_index_entry ancestor_entry, our_entry, their_entry; - - memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); - memset(&our_entry, 0x0, sizeof(git_index_entry)); - memset(&their_entry, 0x0, sizeof(git_index_entry)); - - ancestor_entry.path = "modified_file"; - git_oid_fromstr(&ancestor_entry.oid, - "452e4244b5d083ddf0460acf1ecc74db9dcfa11a"); - - our_entry.path = "modified_file"; - git_oid_fromstr(&our_entry.oid, - "452e4244b5d083ddf0460acf1ecc74db9dcfa11a"); - - their_entry.path = "modified_file"; - git_oid_fromstr(&their_entry.oid, - "452e4244b5d083ddf0460acf1ecc74db9dcfa11a"); - - cl_git_pass(git_status_file(&status, repo, "modified_file")); - cl_assert_equal_i(GIT_STATUS_WT_MODIFIED, status); - - cl_git_pass(git_repository_index(&index, repo)); - cl_git_pass(git_index_conflict_add(index, &ancestor_entry, - &our_entry, &their_entry)); - - cl_git_pass(git_status_file(&status, repo, "modified_file")); - cl_assert_equal_i(GIT_STATUS_WT_MODIFIED, status); - - git_index_free(index); -} - -static void stage_and_commit(git_repository *repo, const char *path) -{ - git_oid tree_oid, commit_oid; - git_tree *tree; - git_signature *signature; - git_index *index; - - cl_git_pass(git_repository_index(&index, repo)); - cl_git_pass(git_index_add_bypath(index, path)); - cl_git_pass(git_index_write(index)); - - cl_git_pass(git_index_write_tree(&tree_oid, index)); - git_index_free(index); - - cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid)); - - cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); - - cl_git_pass(git_commit_create_v( - &commit_oid, - repo, - "HEAD", - signature, - signature, - NULL, - "Initial commit\n\0", - tree, - 0)); - - git_tree_free(tree); - git_signature_free(signature); -} - -static void assert_ignore_case( - bool should_ignore_case, - int expected_lower_cased_file_status, - int expected_camel_cased_file_status) -{ - git_config *config; - unsigned int status; - git_buf lower_case_path = GIT_BUF_INIT, camel_case_path = GIT_BUF_INIT; - git_repository *repo, *repo2; - - repo = cl_git_sandbox_init("empty_standard_repo"); - cl_git_remove_placeholders(git_repository_path(repo), "dummy-marker.txt"); - - cl_git_pass(git_repository_config(&config, repo)); - cl_git_pass(git_config_set_bool(config, "core.ignorecase", should_ignore_case)); - git_config_free(config); - - cl_git_pass(git_buf_joinpath(&lower_case_path, - git_repository_workdir(repo), "plop")); - - cl_git_mkfile(git_buf_cstr(&lower_case_path), ""); - - stage_and_commit(repo, "plop"); - - cl_git_pass(git_repository_open(&repo2, "./empty_standard_repo")); - - cl_git_pass(git_status_file(&status, repo2, "plop")); - cl_assert_equal_i(GIT_STATUS_CURRENT, status); - - cl_git_pass(git_buf_joinpath(&camel_case_path, - git_repository_workdir(repo), "Plop")); - - cl_git_pass(p_rename(git_buf_cstr(&lower_case_path), git_buf_cstr(&camel_case_path))); - - cl_git_pass(git_status_file(&status, repo2, "plop")); - cl_assert_equal_i(expected_lower_cased_file_status, status); - - cl_git_pass(git_status_file(&status, repo2, "Plop")); - cl_assert_equal_i(expected_camel_cased_file_status, status); - - git_repository_free(repo2); - git_buf_free(&lower_case_path); - git_buf_free(&camel_case_path); -} - -void test_status_worktree__file_status_honors_core_ignorecase_true(void) -{ - assert_ignore_case(true, GIT_STATUS_CURRENT, GIT_STATUS_CURRENT); -} - -void test_status_worktree__file_status_honors_core_ignorecase_false(void) -{ - assert_ignore_case(false, GIT_STATUS_WT_DELETED, GIT_STATUS_WT_NEW); -} diff --git a/tests-clar/submodule/lookup.c b/tests-clar/submodule/lookup.c deleted file mode 100644 index 868b51e558c..00000000000 --- a/tests-clar/submodule/lookup.c +++ /dev/null @@ -1,114 +0,0 @@ -#include "clar_libgit2.h" -#include "submodule_helpers.h" -#include "posix.h" - -static git_repository *g_repo = NULL; - -void test_submodule_lookup__initialize(void) -{ - g_repo = cl_git_sandbox_init("submod2"); - - cl_fixture_sandbox("submod2_target"); - p_rename("submod2_target/.gitted", "submod2_target/.git"); - - /* must create submod2_target before rewrite so prettify will work */ - rewrite_gitmodules(git_repository_workdir(g_repo)); - p_rename("submod2/not_submodule/.gitted", "submod2/not_submodule/.git"); -} - -void test_submodule_lookup__cleanup(void) -{ - cl_git_sandbox_cleanup(); - cl_fixture_cleanup("submod2_target"); -} - -void test_submodule_lookup__simple_lookup(void) -{ - git_submodule *sm; - - /* lookup existing */ - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); - cl_assert(sm); - - /* lookup pending change in .gitmodules that is not in HEAD */ - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited")); - cl_assert(sm); - - /* lookup pending change in .gitmodules that is neither in HEAD nor index */ - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_gitmodules_only")); - cl_assert(sm); - - /* lookup git repo subdir that is not added as submodule */ - cl_assert(git_submodule_lookup(&sm, g_repo, "not_submodule") == GIT_EEXISTS); - - /* lookup existing directory that is not a submodule */ - cl_assert(git_submodule_lookup(&sm, g_repo, "just_a_dir") == GIT_ENOTFOUND); - - /* lookup existing file that is not a submodule */ - cl_assert(git_submodule_lookup(&sm, g_repo, "just_a_file") == GIT_ENOTFOUND); - - /* lookup non-existent item */ - cl_assert(git_submodule_lookup(&sm, g_repo, "no_such_file") == GIT_ENOTFOUND); -} - -void test_submodule_lookup__accessors(void) -{ - git_submodule *sm; - const char *oid = "480095882d281ed676fe5b863569520e54a7d5c0"; - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); - cl_assert(git_submodule_owner(sm) == g_repo); - cl_assert_equal_s("sm_unchanged", git_submodule_name(sm)); - cl_assert(git__suffixcmp(git_submodule_path(sm), "sm_unchanged") == 0); - cl_assert(git__suffixcmp(git_submodule_url(sm), "/submod2_target") == 0); - - cl_assert(git_oid_streq(git_submodule_index_id(sm), oid) == 0); - cl_assert(git_oid_streq(git_submodule_head_id(sm), oid) == 0); - cl_assert(git_oid_streq(git_submodule_wd_id(sm), oid) == 0); - - cl_assert(git_submodule_ignore(sm) == GIT_SUBMODULE_IGNORE_NONE); - cl_assert(git_submodule_update(sm) == GIT_SUBMODULE_UPDATE_CHECKOUT); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); - cl_assert_equal_s("sm_changed_head", git_submodule_name(sm)); - - cl_assert(git_oid_streq(git_submodule_index_id(sm), oid) == 0); - cl_assert(git_oid_streq(git_submodule_head_id(sm), oid) == 0); - cl_assert(git_oid_streq(git_submodule_wd_id(sm), - "3d9386c507f6b093471a3e324085657a3c2b4247") == 0); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited")); - cl_assert_equal_s("sm_added_and_uncommited", git_submodule_name(sm)); - - cl_assert(git_oid_streq(git_submodule_index_id(sm), oid) == 0); - cl_assert(git_submodule_head_id(sm) == NULL); - cl_assert(git_oid_streq(git_submodule_wd_id(sm), oid) == 0); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits")); - cl_assert_equal_s("sm_missing_commits", git_submodule_name(sm)); - - cl_assert(git_oid_streq(git_submodule_index_id(sm), oid) == 0); - cl_assert(git_oid_streq(git_submodule_head_id(sm), oid) == 0); - cl_assert(git_oid_streq(git_submodule_wd_id(sm), - "5e4963595a9774b90524d35a807169049de8ccad") == 0); -} - -typedef struct { - int count; -} sm_lookup_data; - -static int sm_lookup_cb(git_submodule *sm, const char *name, void *payload) -{ - sm_lookup_data *data = payload; - data->count += 1; - cl_assert_equal_s(git_submodule_name(sm), name); - return 0; -} - -void test_submodule_lookup__foreach(void) -{ - sm_lookup_data data; - memset(&data, 0, sizeof(data)); - cl_git_pass(git_submodule_foreach(g_repo, sm_lookup_cb, &data)); - cl_assert_equal_i(8, data.count); -} diff --git a/tests-clar/submodule/modify.c b/tests-clar/submodule/modify.c deleted file mode 100644 index f6d41fdf243..00000000000 --- a/tests-clar/submodule/modify.c +++ /dev/null @@ -1,266 +0,0 @@ -#include "clar_libgit2.h" -#include "posix.h" -#include "path.h" -#include "submodule_helpers.h" - -static git_repository *g_repo = NULL; - -#define SM_LIBGIT2_URL "https://github.com/libgit2/libgit2.git" -#define SM_LIBGIT2 "sm_libgit2" -#define SM_LIBGIT2B "sm_libgit2b" - -void test_submodule_modify__initialize(void) -{ - g_repo = cl_git_sandbox_init("submod2"); - - cl_fixture_sandbox("submod2_target"); - p_rename("submod2_target/.gitted", "submod2_target/.git"); - - /* must create submod2_target before rewrite so prettify will work */ - rewrite_gitmodules(git_repository_workdir(g_repo)); - p_rename("submod2/not_submodule/.gitted", "submod2/not_submodule/.git"); -} - -void test_submodule_modify__cleanup(void) -{ - cl_git_sandbox_cleanup(); - cl_fixture_cleanup("submod2_target"); -} - -void test_submodule_modify__add(void) -{ - git_submodule *sm; - git_config *cfg; - const char *s; - - /* re-add existing submodule */ - cl_assert( - git_submodule_add_setup(NULL, g_repo, "whatever", "sm_unchanged", 1) == - GIT_EEXISTS ); - - /* add a submodule using a gitlink */ - - cl_git_pass( - git_submodule_add_setup(&sm, g_repo, SM_LIBGIT2_URL, SM_LIBGIT2, 1) - ); - - cl_assert(git_path_isfile("submod2/" SM_LIBGIT2 "/.git")); - - cl_assert(git_path_isdir("submod2/.git/modules")); - cl_assert(git_path_isdir("submod2/.git/modules/" SM_LIBGIT2)); - cl_assert(git_path_isfile("submod2/.git/modules/" SM_LIBGIT2 "/HEAD")); - - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_git_pass( - git_config_get_string(&s, cfg, "submodule." SM_LIBGIT2 ".url")); - cl_assert_equal_s(s, SM_LIBGIT2_URL); - git_config_free(cfg); - - /* add a submodule not using a gitlink */ - - cl_git_pass( - git_submodule_add_setup(&sm, g_repo, SM_LIBGIT2_URL, SM_LIBGIT2B, 0) - ); - - cl_assert(git_path_isdir("submod2/" SM_LIBGIT2B "/.git")); - cl_assert(git_path_isfile("submod2/" SM_LIBGIT2B "/.git/HEAD")); - cl_assert(!git_path_exists("submod2/.git/modules/" SM_LIBGIT2B)); - - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_git_pass( - git_config_get_string(&s, cfg, "submodule." SM_LIBGIT2B ".url")); - cl_assert_equal_s(s, SM_LIBGIT2_URL); - git_config_free(cfg); -} - -static int delete_one_config(const git_config_entry *entry, void *payload) -{ - git_config *cfg = payload; - return git_config_delete_entry(cfg, entry->name); -} - -static int init_one_submodule( - git_submodule *sm, const char *name, void *payload) -{ - GIT_UNUSED(name); - GIT_UNUSED(payload); - return git_submodule_init(sm, false); -} - -void test_submodule_modify__init(void) -{ - git_config *cfg; - const char *str; - - /* erase submodule data from .git/config */ - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_git_pass( - git_config_foreach_match(cfg, "submodule\\..*", delete_one_config, cfg)); - git_config_free(cfg); - - /* confirm no submodule data in config */ - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_git_fail(git_config_get_string(&str, cfg, "submodule.sm_unchanged.url")); - cl_git_fail(git_config_get_string(&str, cfg, "submodule.sm_changed_head.url")); - cl_git_fail(git_config_get_string(&str, cfg, "submodule.sm_added_and_uncommited.url")); - git_config_free(cfg); - - /* call init and see that settings are copied */ - cl_git_pass(git_submodule_foreach(g_repo, init_one_submodule, NULL)); - - git_submodule_reload_all(g_repo); - - /* confirm submodule data in config */ - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_unchanged.url")); - cl_assert(git__suffixcmp(str, "/submod2_target") == 0); - cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_changed_head.url")); - cl_assert(git__suffixcmp(str, "/submod2_target") == 0); - cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_added_and_uncommited.url")); - cl_assert(git__suffixcmp(str, "/submod2_target") == 0); - git_config_free(cfg); -} - -static int sync_one_submodule( - git_submodule *sm, const char *name, void *payload) -{ - GIT_UNUSED(name); - GIT_UNUSED(payload); - return git_submodule_sync(sm); -} - -void test_submodule_modify__sync(void) -{ - git_submodule *sm1, *sm2, *sm3; - git_config *cfg; - const char *str; - -#define SM1 "sm_unchanged" -#define SM2 "sm_changed_head" -#define SM3 "sm_added_and_uncommited" - - /* look up some submodules */ - cl_git_pass(git_submodule_lookup(&sm1, g_repo, SM1)); - cl_git_pass(git_submodule_lookup(&sm2, g_repo, SM2)); - cl_git_pass(git_submodule_lookup(&sm3, g_repo, SM3)); - - /* At this point, the .git/config URLs for the submodules have - * not be rewritten with the absolute paths (although the - * .gitmodules have. Let's confirm that they DO NOT match - * yet, then we can do a sync to make them match... - */ - - /* check submodule info does not match before sync */ - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM1".url")); - cl_assert(strcmp(git_submodule_url(sm1), str) != 0); - cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM2".url")); - cl_assert(strcmp(git_submodule_url(sm2), str) != 0); - cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM3".url")); - cl_assert(strcmp(git_submodule_url(sm3), str) != 0); - git_config_free(cfg); - - /* sync all the submodules */ - cl_git_pass(git_submodule_foreach(g_repo, sync_one_submodule, NULL)); - - /* check that submodule config is updated */ - cl_git_pass(git_repository_config(&cfg, g_repo)); - cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM1".url")); - cl_assert_equal_s(git_submodule_url(sm1), str); - cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM2".url")); - cl_assert_equal_s(git_submodule_url(sm2), str); - cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM3".url")); - cl_assert_equal_s(git_submodule_url(sm3), str); - git_config_free(cfg); -} - -void test_submodule_modify__edit_and_save(void) -{ - git_submodule *sm1, *sm2; - char *old_url; - git_submodule_ignore_t old_ignore; - git_submodule_update_t old_update; - git_repository *r2; - int old_fetchrecurse; - - cl_git_pass(git_submodule_lookup(&sm1, g_repo, "sm_changed_head")); - - old_url = git__strdup(git_submodule_url(sm1)); - - /* modify properties of submodule */ - cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL)); - old_ignore = git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_UNTRACKED); - old_update = git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_REBASE); - old_fetchrecurse = git_submodule_set_fetch_recurse_submodules(sm1, 1); - - cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1)); - cl_assert_equal_i( - (int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1)); - cl_assert_equal_i( - (int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm1)); - cl_assert_equal_i(1, git_submodule_fetch_recurse_submodules(sm1)); - - /* revert without saving (and confirm setters return old value) */ - cl_git_pass(git_submodule_set_url(sm1, old_url)); - cl_assert_equal_i( - (int)GIT_SUBMODULE_IGNORE_UNTRACKED, - (int)git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_DEFAULT)); - cl_assert_equal_i( - (int)GIT_SUBMODULE_UPDATE_REBASE, - (int)git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_DEFAULT)); - cl_assert_equal_i( - 1, git_submodule_set_fetch_recurse_submodules(sm1, old_fetchrecurse)); - - /* check that revert was successful */ - cl_assert_equal_s(old_url, git_submodule_url(sm1)); - cl_assert_equal_i((int)old_ignore, (int)git_submodule_ignore(sm1)); - cl_assert_equal_i((int)old_update, (int)git_submodule_update(sm1)); - cl_assert_equal_i( - old_fetchrecurse, git_submodule_fetch_recurse_submodules(sm1)); - - /* modify properties of submodule (again) */ - cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL)); - git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_UNTRACKED); - git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_REBASE); - git_submodule_set_fetch_recurse_submodules(sm1, 1); - - /* call save */ - cl_git_pass(git_submodule_save(sm1)); - - /* attempt to "revert" values */ - git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_DEFAULT); - git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_DEFAULT); - - /* but ignore and update should NOT revert because the DEFAULT - * should now be the newly saved value... - */ - cl_assert_equal_i( - (int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1)); - cl_assert_equal_i( - (int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm1)); - cl_assert_equal_i(1, git_submodule_fetch_recurse_submodules(sm1)); - - /* call reload and check that the new values are loaded */ - cl_git_pass(git_submodule_reload(sm1)); - - cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1)); - cl_assert_equal_i( - (int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1)); - cl_assert_equal_i( - (int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm1)); - cl_assert_equal_i(1, git_submodule_fetch_recurse_submodules(sm1)); - - /* open a second copy of the repo and compare submodule */ - cl_git_pass(git_repository_open(&r2, "submod2")); - cl_git_pass(git_submodule_lookup(&sm2, r2, "sm_changed_head")); - - cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm2)); - cl_assert_equal_i( - (int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm2)); - cl_assert_equal_i( - (int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm2)); - cl_assert_equal_i(1, git_submodule_fetch_recurse_submodules(sm2)); - - git_repository_free(r2); - git__free(old_url); -} diff --git a/tests-clar/submodule/status.c b/tests-clar/submodule/status.c deleted file mode 100644 index 3fd6960c9dd..00000000000 --- a/tests-clar/submodule/status.c +++ /dev/null @@ -1,307 +0,0 @@ -#include "clar_libgit2.h" -#include "posix.h" -#include "path.h" -#include "submodule_helpers.h" -#include "fileops.h" - -static git_repository *g_repo = NULL; - -void test_submodule_status__initialize(void) -{ - g_repo = cl_git_sandbox_init("submod2"); - - cl_fixture_sandbox("submod2_target"); - p_rename("submod2_target/.gitted", "submod2_target/.git"); - - /* must create submod2_target before rewrite so prettify will work */ - rewrite_gitmodules(git_repository_workdir(g_repo)); - p_rename("submod2/not_submodule/.gitted", "submod2/not_submodule/.git"); -} - -void test_submodule_status__cleanup(void) -{ - cl_git_sandbox_cleanup(); - cl_fixture_cleanup("submod2_target"); -} - -void test_submodule_status__unchanged(void) -{ - unsigned int status, expected; - git_submodule *sm; - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - expected = GIT_SUBMODULE_STATUS_IN_HEAD | - GIT_SUBMODULE_STATUS_IN_INDEX | - GIT_SUBMODULE_STATUS_IN_CONFIG | - GIT_SUBMODULE_STATUS_IN_WD; - - cl_assert(status == expected); -} - -/* 4 values of GIT_SUBMODULE_IGNORE to check */ - -void test_submodule_status__ignore_none(void) -{ - unsigned int status; - git_submodule *sm; - git_buf path = GIT_BUF_INIT; - - cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged")); - cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES)); - - cl_git_fail(git_submodule_lookup(&sm, g_repo, "not_submodule")); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED) != 0); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_WD_MODIFIED) != 0); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNTRACKED) != 0); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0); - - /* removed sm_unchanged for deleted workdir */ - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0); - - /* now mkdir sm_unchanged to test uninitialized */ - cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0)); - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); - cl_git_pass(git_submodule_reload(sm)); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0); - - /* update sm_changed_head in index */ - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); - cl_git_pass(git_submodule_add_to_index(sm, true)); - /* reload is not needed because add_to_index updates the submodule data */ - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0); - - /* remove sm_changed_head from index */ - { - git_index *index; - size_t pos; - - cl_git_pass(git_repository_index(&index, g_repo)); - cl_assert(!git_index_find(&pos, index, "sm_changed_head")); - cl_git_pass(git_index_remove(index, "sm_changed_head", 0)); - cl_git_pass(git_index_write(index)); - - git_index_free(index); - } - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); - cl_git_pass(git_submodule_reload(sm)); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_DELETED) != 0); - - git_buf_free(&path); -} - -static int set_sm_ignore(git_submodule *sm, const char *name, void *payload) -{ - git_submodule_ignore_t ignore = *(git_submodule_ignore_t *)payload; - GIT_UNUSED(name); - git_submodule_set_ignore(sm, ignore); - return 0; -} - -void test_submodule_status__ignore_untracked(void) -{ - unsigned int status; - git_submodule *sm; - git_buf path = GIT_BUF_INIT; - git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_UNTRACKED; - - cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged")); - cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES)); - - cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign)); - - cl_git_fail(git_submodule_lookup(&sm, g_repo, "not_submodule")); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED) != 0); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_WD_MODIFIED) != 0); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0); - - /* removed sm_unchanged for deleted workdir */ - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0); - - /* now mkdir sm_unchanged to test uninitialized */ - cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0)); - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); - cl_git_pass(git_submodule_reload(sm)); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0); - - /* update sm_changed_head in index */ - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); - cl_git_pass(git_submodule_add_to_index(sm, true)); - /* reload is not needed because add_to_index updates the submodule data */ - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0); - - git_buf_free(&path); -} - -void test_submodule_status__ignore_dirty(void) -{ - unsigned int status; - git_submodule *sm; - git_buf path = GIT_BUF_INIT; - git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_DIRTY; - - cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged")); - cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES)); - - cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign)); - - cl_git_fail(git_submodule_lookup(&sm, g_repo, "not_submodule")); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0); - - /* removed sm_unchanged for deleted workdir */ - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0); - - /* now mkdir sm_unchanged to test uninitialized */ - cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0)); - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); - cl_git_pass(git_submodule_reload(sm)); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0); - - /* update sm_changed_head in index */ - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); - cl_git_pass(git_submodule_add_to_index(sm, true)); - /* reload is not needed because add_to_index updates the submodule data */ - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0); - - git_buf_free(&path); -} - -void test_submodule_status__ignore_all(void) -{ - unsigned int status; - git_submodule *sm; - git_buf path = GIT_BUF_INIT; - git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_ALL; - - cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged")); - cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES)); - - cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign)); - - cl_git_fail(git_submodule_lookup(&sm, g_repo, "not_submodule")); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - /* removed sm_unchanged for deleted workdir */ - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - /* now mkdir sm_unchanged to test uninitialized */ - cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0)); - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); - cl_git_pass(git_submodule_reload(sm)); - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - /* update sm_changed_head in index */ - cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); - cl_git_pass(git_submodule_add_to_index(sm, true)); - /* reload is not needed because add_to_index updates the submodule data */ - cl_git_pass(git_submodule_status(&status, sm)); - cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - - git_buf_free(&path); -} diff --git a/tests-clar/submodule/submodule_helpers.c b/tests-clar/submodule/submodule_helpers.c deleted file mode 100644 index 0c3e79f717e..00000000000 --- a/tests-clar/submodule/submodule_helpers.c +++ /dev/null @@ -1,84 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "path.h" -#include "util.h" -#include "posix.h" -#include "submodule_helpers.h" - -/* rewrite gitmodules -> .gitmodules - * rewrite the empty or relative urls inside each module - * rename the .gitted directory inside any submodule to .git - */ -void rewrite_gitmodules(const char *workdir) -{ - git_buf in_f = GIT_BUF_INIT, out_f = GIT_BUF_INIT, path = GIT_BUF_INIT; - FILE *in, *out; - char line[256]; - - cl_git_pass(git_buf_joinpath(&in_f, workdir, "gitmodules")); - cl_git_pass(git_buf_joinpath(&out_f, workdir, ".gitmodules")); - - cl_assert((in = fopen(in_f.ptr, "r")) != NULL); - cl_assert((out = fopen(out_f.ptr, "w")) != NULL); - - while (fgets(line, sizeof(line), in) != NULL) { - char *scan = line; - - while (*scan == ' ' || *scan == '\t') scan++; - - /* rename .gitted -> .git in submodule directories */ - if (git__prefixcmp(scan, "path =") == 0) { - scan += strlen("path ="); - while (*scan == ' ') scan++; - - git_buf_joinpath(&path, workdir, scan); - git_buf_rtrim(&path); - git_buf_joinpath(&path, path.ptr, ".gitted"); - - if (!git_buf_oom(&path) && p_access(path.ptr, F_OK) == 0) { - git_buf_joinpath(&out_f, workdir, scan); - git_buf_rtrim(&out_f); - git_buf_joinpath(&out_f, out_f.ptr, ".git"); - - if (!git_buf_oom(&out_f)) - p_rename(path.ptr, out_f.ptr); - } - } - - /* copy non-"url =" lines verbatim */ - if (git__prefixcmp(scan, "url =") != 0) { - fputs(line, out); - continue; - } - - /* convert relative URLs in "url =" lines */ - scan += strlen("url ="); - while (*scan == ' ') scan++; - - if (*scan == '.') { - git_buf_joinpath(&path, workdir, scan); - git_buf_rtrim(&path); - } else if (!*scan || *scan == '\n') { - git_buf_joinpath(&path, workdir, "../testrepo.git"); - } else { - fputs(line, out); - continue; - } - - git_path_prettify(&path, path.ptr, NULL); - git_buf_putc(&path, '\n'); - cl_assert(!git_buf_oom(&path)); - - fwrite(line, scan - line, sizeof(char), out); - fputs(path.ptr, out); - } - - fclose(in); - fclose(out); - - cl_must_pass(p_unlink(in_f.ptr)); - - git_buf_free(&in_f); - git_buf_free(&out_f); - git_buf_free(&path); -} diff --git a/tests-clar/submodule/submodule_helpers.h b/tests-clar/submodule/submodule_helpers.h deleted file mode 100644 index 6b76a832e9a..00000000000 --- a/tests-clar/submodule/submodule_helpers.h +++ /dev/null @@ -1,2 +0,0 @@ -extern void rewrite_gitmodules(const char *workdir); - diff --git a/tests-clar/threads/basic.c b/tests-clar/threads/basic.c deleted file mode 100644 index 2b1c3680833..00000000000 --- a/tests-clar/threads/basic.c +++ /dev/null @@ -1,20 +0,0 @@ -#include "clar_libgit2.h" - -#include "cache.h" - - -static git_repository *g_repo; - -void test_threads_basic__initialize(void) { - g_repo = cl_git_sandbox_init("testrepo"); -} - -void test_threads_basic__cleanup(void) { - cl_git_sandbox_cleanup(); -} - - -void test_threads_basic__cache(void) { - // run several threads polling the cache at the same time - cl_assert(1 == 1); -} diff --git a/tests-clar/valgrind-supp-mac.txt b/tests-clar/valgrind-supp-mac.txt deleted file mode 100644 index 03e60dcd773..00000000000 --- a/tests-clar/valgrind-supp-mac.txt +++ /dev/null @@ -1,82 +0,0 @@ -{ - libgit2-giterr-set-buffer - Memcheck:Leak - ... - fun:git__realloc - fun:git_buf_try_grow - fun:git_buf_grow - fun:git_buf_vprintf - fun:giterr_set -} -{ - mac-setenv-leak-1 - Memcheck:Leak - fun:malloc_zone_malloc - fun:__setenv - fun:setenv -} -{ - mac-setenv-leak-2 - Memcheck:Leak - fun:malloc_zone_malloc - fun:malloc_set_zone_name - ... - fun:init__zone0 - fun:setenv -} -{ - mac-dyld-initializer-leak - Memcheck:Leak - fun:malloc - ... - fun:dyld_register_image_state_change_handler - fun:_dyld_initializer -} -{ - mac-tz-leak-1 - Memcheck:Leak - ... - fun:token_table_add - fun:notify_register_check - fun:notify_register_tz -} -{ - mac-tz-leak-2 - Memcheck:Leak - fun:malloc - fun:tzload -} -{ - mac-tz-leak-3 - Memcheck:Leak - fun:malloc - fun:tzsetwall_basic -} -{ - mac-tz-leak-4 - Memcheck:Leak - fun:malloc - fun:gmtsub -} -{ - mac-system-init-leak-1 - Memcheck:Leak - ... - fun:_libxpc_initializer - fun:libSystem_initializer -} -{ - mac-system-init-leak-2 - Memcheck:Leak - ... - fun:__keymgr_initializer - fun:libSystem_initializer -} -{ - mac-puts-leak - Memcheck:Leak - fun:malloc - fun:__smakebuf - ... - fun:puts -} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000000..df100e980a9 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,6 @@ +# The main libgit2 tests tree: this CMakeLists.txt includes the +# subprojects that make up core libgit2 support. + +add_subdirectory(headertest) +add_subdirectory(libgit2) +add_subdirectory(util) diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 00000000000..460e045e386 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,83 @@ +# libgit2 tests + +These are the unit and integration tests for the libgit2 projects. + +* `benchmarks` + These are benchmark tests that excercise the CLI. +* `clar` + This is [clar](https://github.com/clar-test/clar) the common test framework. +* `headertest` + This is a simple project that ensures that our public headers are + compatible with extremely strict compilation options. +* `libgit2` + These tests exercise the core git functionality in libgit2 itself. +* `resources` + These are the resources for the tests, including files and git + repositories. +* `util` + These are tests of the common utility library. + +## Writing tests for libgit2 + +libgit2 uses the [clar test framework](http://github.com/clar-test/clar), a +C testing framework. + +The best resources for learning clar are [clar itself](https://github.com/clar-test/clar) +and the existing tests within libgit2. In general: + +* If you place a `.c` file into a test directory, it is eligible to contain +test cases. +* The function name for your test is important; test function names begin + with `test_`, followed by the folder path (underscore separated), two + underscores as a delimiter, then the test name. For example, a file + `merge/analysis.c` may contain a test `uptodate`: + + ``` + void test_merge_analysis__uptodate(void) + { + ... + } + ``` + +* You can run an individual test by passing `-s` to the test runner. Tests + are referred to by their function names; for example, the function + `test_merge_analysis__uptodate` is referred to as `merge::analysis::uptodate`. + To run only that function you can use the `-s` option on the test runner: + + ``` + libgit2_tests -smerge::analysis::uptodate + ``` + +## Memory leak checking + +These are automatically run as part of CI, but if you want to check locally: + +### Linux + +Uses [`valgrind`](http://www.valgrind.org/): + +```console +$ cmake -DBUILD_TESTS=ON -DVALGRIND=ON .. +$ cmake --build . +$ valgrind --leak-check=full --show-reachable=yes --num-callers=50 --suppressions=../libgit2_tests.supp \ + ./libgit2_tests +``` + +### macOS + +Uses [`leaks`](https://developer.apple.com/library/archive/documentation/Performance/Conceptual/ManagingMemory/Articles/FindingLeaks.html), which requires XCode installed: + +```console +$ MallocStackLogging=1 MallocScribble=1 MallocLogFile=/dev/null CLAR_AT_EXIT="leaks -quiet \$PPID" \ + ./libgit2_tests +``` + +### Windows + +Build with the `WIN32_LEAKCHECK` option: + +```console +$ cmake -DBUILD_TESTS=ON -DWIN32_LEAKCHECK=ON .. +$ cmake --build . +$ ./libgit2_tests +``` diff --git a/tests/benchmarks/README.md b/tests/benchmarks/README.md new file mode 100644 index 00000000000..f66b27aea3b --- /dev/null +++ b/tests/benchmarks/README.md @@ -0,0 +1,121 @@ +# libgit2 benchmarks + +This folder contains the individual benchmark tests for libgit2, +meant for understanding the performance characteristics of libgit2, +comparing your development code to the existing libgit2 code, or +comparing libgit2 to the git reference implementation. + +## Running benchmark tests + +Benchmark tests can be run in several different ways: running all +benchmarks, running one (or more) suite of benchmarks, or running a +single individual benchmark. You can target either an individual +version of a CLI, or you can A/B test a baseline CLI against a test +CLI. + +### Specifying the command-line interface to test + +By default, the `git` in your path is benchmarked. Use the +`-c` (or `--cli`) option to specify the command-line interface +to test. + +Example: `libgit2_bench --cli git2_cli` will run the tests against +`git2_cli`. + +### Running tests to compare two different implementations + +You can compare a baseline command-line interface against a test +command-line interface using the `-b (or `--baseline-cli`) option. + +Example: `libgit2_bench --baseline-cli git --cli git2_cli` will +run the tests against both `git` and `git2_cli`. + +### Running individual benchmark tests + +Similar to how a test suite or individual test is specified in +[clar](https://github.com/clar-test/clar), the `-s` (or `--suite`) +option may be used to specify the suite or individual test to run. +Like clar, the suite and test name are separated by `::`, and like +clar, this is a prefix match. + +Examples: +* `libgit2_bench -shash_object` will run the tests in the + `hash_object` suite. +* `libgit2_bench -shash_object::random_1kb` will run the + `hash_object::random_1kb` test. +* `libgit2_bench -shash_object::random` will run all the tests that + begin with `hash_object::random`. + +## Writing benchmark tests + +Benchmark tests are meant to be easy to write. Each individual +benchmark is a shell script that allows it to do set up (eg, creating +or cloning a repository, creating temporary files, etc), then running +benchmarks, then teardown. + +The `benchmark_helpers.sh` script provides many helpful utility +functions to allow for cross-platform benchmarking, as well as a +wrapper for `hyperfine` that is suited to testing libgit2. +Note that the helper script must be included first, at the beginning +of the benchmark test. + +### Benchmark example + +This simplistic example compares the speed of running the `git help` +command in the baseline CLI to the test CLI. + +```bash +#!/bin/bash -e + +# include the benchmark library +. "$(dirname "$0")/benchmark_helpers.sh" + +# run the "help" command; this will benchmark `git2_cli help` +gitbench help +``` + +### Naming + +The filename of the benchmark itself is important. A benchmark's +filename should be the name of the benchmark suite, followed by two +underscores, followed by the name of the benchmark. For example, +`hash-object__random_1kb` is the `random_1kb` test in the `hash-object` +suite. + +### Options + +The `gitbench` function accepts several options. + +* `--sandbox ` + The name of a test resource (in the `tests/resources` directory). + This will be copied as-is to the sandbox location before test + execution. This is copied _before_ the `prepare` script is run. + This option may be specified multiple times. +* `--repository ` + The name of a test resource repository (in the `tests/resources` + directory). This repository will be copied into a sandbox location + before test execution, and your test will run in this directory. + This is copied _before_ the `prepare` script is run. +* `--prepare + +Flame Graph + +Reset Zoom +Search + +unix`mutex_enter (195 samples, 0.34%) + + + +genunix`as_fault (12 samples, 0.02%) + + + +genunix`disp_lock_exit (27 samples, 0.05%) + + + +genunix`vsd_free (17 samples, 0.03%) + + + +genunix`pn_fixslash (44 samples, 0.08%) + + + +unix`mutex_exit (105 samples, 0.18%) + + + +genunix`falloc (1,363 samples, 2.37%) +g.. + + +genunix`traverse (30 samples, 0.05%) + + + +genunix`fop_lookup (55 samples, 0.10%) + + + +genunix`kmem_cache_free (29 samples, 0.05%) + + + +lofs`makelonode (39 samples, 0.07%) + + + +genunix`vsd_free (155 samples, 0.27%) + + + +unix`strlen (2,659 samples, 4.63%) +unix`.. + + +unix`clear_int_flag (180 samples, 0.31%) + + + +unix`mutex_exit (38 samples, 0.07%) + + + +genunix`kmem_cpu_reload (5 samples, 0.01%) + + + +unix`mutex_exit (26 samples, 0.05%) + + + +genunix`vn_vfslocks_getlock (47 samples, 0.08%) + + + +unix`bzero (8 samples, 0.01%) + + + +genunix`vn_exists (50 samples, 0.09%) + + + +unix`mutex_enter (727 samples, 1.27%) + + + +genunix`kmem_cache_alloc (179 samples, 0.31%) + + + +unix`mutex_enter (905 samples, 1.58%) + + + +genunix`ufalloc (10 samples, 0.02%) + + + +genunix`vn_rele (25 samples, 0.04%) + + + +genunix`vn_exists (17 samples, 0.03%) + + + +unix`lock_try (778 samples, 1.35%) + + + +genunix`rwst_enter_common (314 samples, 0.55%) + + + +genunix`fsop_root (62 samples, 0.11%) + + + +lofs`table_lock_enter (44 samples, 0.08%) + + + +unix`mutex_exit (138 samples, 0.24%) + + + +unix`mutex_enter (316 samples, 0.55%) + + + +genunix`kmem_cache_free (5 samples, 0.01%) + + + +unix`preempt (14 samples, 0.02%) + + + +genunix`vn_alloc (1,189 samples, 2.07%) +g.. + + +genunix`kmem_cache_alloc (126 samples, 0.22%) + + + +genunix`vfs_getops (157 samples, 0.27%) + + + +lofs`lsave (27 samples, 0.05%) + + + +unix`tsc_read (160 samples, 0.28%) + + + +lofs`lfind (26 samples, 0.05%) + + + +unix`atomic_add_64 (205 samples, 0.36%) + + + +unix`mutex_enter (320 samples, 0.56%) + + + +genunix`traverse (17 samples, 0.03%) + + + +unix`mutex_enter (197 samples, 0.34%) + + + +genunix`vn_mountedvfs (20 samples, 0.03%) + + + +genunix`audit_unfalloc (340 samples, 0.59%) + + + +genunix`kmem_cache_free (209 samples, 0.36%) + + + +genunix`kmem_zalloc (13 samples, 0.02%) + + + +genunix`thread_lock (33 samples, 0.06%) + + + +unix`tsc_read (186 samples, 0.32%) + + + +genunix`vn_vfsrlock (12 samples, 0.02%) + + + +lofs`lo_inactive (21 samples, 0.04%) + + + +genunix`rwst_destroy (20 samples, 0.03%) + + + +unix`mutex_enter (379 samples, 0.66%) + + + +genunix`vn_setops (41 samples, 0.07%) + + + +genunix`vn_recycle (33 samples, 0.06%) + + + +lofs`lo_inactive (6,307 samples, 10.98%) +lofs`lo_inactive + + +lofs`table_lock_enter (220 samples, 0.38%) + + + +genunix`cv_broadcast (25 samples, 0.04%) + + + +unix`mutex_exit (358 samples, 0.62%) + + + +genunix`kmem_cache_alloc (234 samples, 0.41%) + + + +unix`rw_enter (525 samples, 0.91%) + + + +unix`membar_consumer (237 samples, 0.41%) + + + +unix`swtch (5 samples, 0.01%) + + + +genunix`rwst_enter_common (32 samples, 0.06%) + + + +lofs`freelonode (5,313 samples, 9.25%) +lofs`freelonode + + +genunix`vn_openat (46,342 samples, 80.68%) +genunix`vn_openat + + +genunix`vn_rele (19 samples, 0.03%) + + + +genunix`proc_exit (5 samples, 0.01%) + + + +unix`mutex_exit (512 samples, 0.89%) + + + +genunix`kmem_free (35 samples, 0.06%) + + + +unix`mutex_enter (252 samples, 0.44%) + + + +genunix`rwst_exit (12 samples, 0.02%) + + + +genunix`crgetuid (22 samples, 0.04%) + + + +genunix`kmem_free (17 samples, 0.03%) + + + +unix`mutex_init (53 samples, 0.09%) + + + +ufs`ufs_iaccess (648 samples, 1.13%) + + + +all (57,441 samples, 100%) + + + +genunix`fop_inactive (6,689 samples, 11.64%) +genunix`fop_inact.. + + +genunix`kmem_cache_alloc (9 samples, 0.02%) + + + +genunix`kmem_cache_free (184 samples, 0.32%) + + + +genunix`pn_get_buf (13 samples, 0.02%) + + + +unix`strlen (107 samples, 0.19%) + + + +unix`mutex_exit (46 samples, 0.08%) + + + +genunix`post_syscall (12 samples, 0.02%) + + + +unix`mutex_init (38 samples, 0.07%) + + + +unix`rw_exit (439 samples, 0.76%) + + + +lofs`lo_lookup (65 samples, 0.11%) + + + +genunix`clear_stale_fd (44 samples, 0.08%) + + + +unix`mutex_enter (238 samples, 0.41%) + + + +genunix`pn_get_buf (687 samples, 1.20%) + + + +genunix`vn_free (1,663 samples, 2.90%) +ge.. + + +unix`mutex_enter (980 samples, 1.71%) + + + +genunix`crhold (5 samples, 0.01%) + + + +unix`mutex_exit (59 samples, 0.10%) + + + +genunix`vn_reinit (48 samples, 0.08%) + + + +genunix`vfs_getops (21 samples, 0.04%) + + + +genunix`open (49,669 samples, 86.47%) +genunix`open + + +genunix`kmem_cache_alloc (39 samples, 0.07%) + + + +genunix`vn_vfslocks_getlock (79 samples, 0.14%) + + + +unix`clear_int_flag (39 samples, 0.07%) + + + +genunix`kmem_cache_free (215 samples, 0.37%) + + + +unix`mutex_destroy (53 samples, 0.09%) + + + +genunix`vn_vfsunlock (3,578 samples, 6.23%) +genunix`.. + + +genunix`dnlc_lookup (1,843 samples, 3.21%) +gen.. + + +genunix`lookupnameatcred (45,978 samples, 80.04%) +genunix`lookupnameatcred + + +genunix`crgetmapped (41 samples, 0.07%) + + + +genunix`anon_zero (7 samples, 0.01%) + + + +genunix`rwst_tryenter (628 samples, 1.09%) + + + +unix`mutex_enter (309 samples, 0.54%) + + + +genunix`vn_rele (14 samples, 0.02%) + + + +genunix`vn_setpath (1,969 samples, 3.43%) +gen.. + + +unix`mutex_enter (111 samples, 0.19%) + + + +genunix`cv_broadcast (40 samples, 0.07%) + + + +genunix`kmem_cache_alloc (66 samples, 0.11%) + + + +genunix`audit_getstate (21 samples, 0.04%) + + + +genunix`vn_setpath (58 samples, 0.10%) + + + +genunix`open (17 samples, 0.03%) + + + +unix`bcopy (896 samples, 1.56%) + + + +unix`mutex_enter (99 samples, 0.17%) + + + +genunix`traverse (5,557 samples, 9.67%) +genunix`traverse + + +genunix`pn_getcomponent (41 samples, 0.07%) + + + +unix`mutex_enter (640 samples, 1.11%) + + + +unix`mutex_destroy (176 samples, 0.31%) + + + +unix`lwp_getdatamodel (6 samples, 0.01%) + + + +genunix`unfalloc (39 samples, 0.07%) + + + +genunix`syscall_mstate (355 samples, 0.62%) + + + +genunix`cv_init (65 samples, 0.11%) + + + +unix`mutex_enter (95 samples, 0.17%) + + + +unix`bcmp (42 samples, 0.07%) + + + +unix`mutex_exit (350 samples, 0.61%) + + + +genunix`kmem_free (288 samples, 0.50%) + + + +unix`mutex_exit (58 samples, 0.10%) + + + +genunix`kmem_alloc (32 samples, 0.06%) + + + +unix`mutex_exit (356 samples, 0.62%) + + + +unix`mutex_init (46 samples, 0.08%) + + + +genunix`rwst_init (173 samples, 0.30%) + + + +genunix`rwst_enter_common (28 samples, 0.05%) + + + +genunix`openat (49,647 samples, 86.43%) +genunix`openat + + +unix`mutex_enter (303 samples, 0.53%) + + + +lofs`lfind (278 samples, 0.48%) + + + +unix`mutex_exit (90 samples, 0.16%) + + + +genunix`cv_init (49 samples, 0.09%) + + + +unix`tsc_gethrtimeunscaled (43 samples, 0.07%) + + + +genunix`rwst_tryenter (32 samples, 0.06%) + + + +genunix`pn_fixslash (14 samples, 0.02%) + + + +genunix`gethrtime_unscaled (420 samples, 0.73%) + + + +genunix`post_syscall (4,245 samples, 7.39%) +genunix`po.. + + +genunix`kmem_zalloc (280 samples, 0.49%) + + + +genunix`vn_alloc (20 samples, 0.03%) + + + +genunix`vn_mountedvfs (43 samples, 0.07%) + + + +genunix`audit_getstate (15 samples, 0.03%) + + + +zfs`zfs_lookup (22 samples, 0.04%) + + + +genunix`crgetuid (6 samples, 0.01%) + + + +unix`copystr (598 samples, 1.04%) + + + +unix`i_ddi_splhigh (23 samples, 0.04%) + + + +unix`trap (13 samples, 0.02%) + + + +genunix`audit_getstate (27 samples, 0.05%) + + + +genunix`vn_mountedvfs (56 samples, 0.10%) + + + +unix`mutex_destroy (17 samples, 0.03%) + + + +genunix`cv_broadcast (14 samples, 0.02%) + + + +genunix`segvn_fault (11 samples, 0.02%) + + + +genunix`vn_rele (39 samples, 0.07%) + + + +genunix`kmem_free (457 samples, 0.80%) + + + +genunix`vn_vfsunlock (20 samples, 0.03%) + + + +genunix`vn_vfslocks_rele (34 samples, 0.06%) + + + +unix`atomic_cas_64 (318 samples, 0.55%) + + + +unix`mutex_enter (337 samples, 0.59%) + + + +unix`do_splx (31 samples, 0.05%) + + + +genunix`ufalloc_file (20 samples, 0.03%) + + + +genunix`fd_reserve (35 samples, 0.06%) + + + +genunix`copen (49,444 samples, 86.08%) +genunix`copen + + +unix`mutex_enter (279 samples, 0.49%) + + + +unix`0xfffffffffb800c91 (4,361 samples, 7.59%) +unix`0xfff.. + + +genunix`crgetmapped (55 samples, 0.10%) + + + +genunix`cv_init (56 samples, 0.10%) + + + +genunix`dnlc_lookup (26 samples, 0.05%) + + + +genunix`kmem_alloc (11 samples, 0.02%) + + + +genunix`cv_init (53 samples, 0.09%) + + + +unix`copyinstr (25 samples, 0.04%) + + + +genunix`gethrtime_unscaled (203 samples, 0.35%) + + + +genunix`kmem_cache_alloc (11 samples, 0.02%) + + + +genunix`vn_free (26 samples, 0.05%) + + + +unix`mutex_exit (149 samples, 0.26%) + + + +genunix`vn_recycle (319 samples, 0.56%) + + + +genunix`vn_rele (64 samples, 0.11%) + + + +unix`bcmp (11 samples, 0.02%) + + + +genunix`kmem_cache_free (154 samples, 0.27%) + + + +unix`lock_clear_splx (28 samples, 0.05%) + + + +genunix`unfalloc (729 samples, 1.27%) + + + +genunix`fop_lookup (85 samples, 0.15%) + + + +zfs`specvp_check (10 samples, 0.02%) + + + +genunix`lookupnameatcred (22 samples, 0.04%) + + + +unix`tsc_read (367 samples, 0.64%) + + + +genunix`memcmp (38 samples, 0.07%) + + + +unix`splx (6 samples, 0.01%) + + + +unix`mutex_exit (95 samples, 0.17%) + + + +genunix`gethrtime_unscaled (7 samples, 0.01%) + + + +genunix`rwst_init (13 samples, 0.02%) + + + +genunix`audit_getstate (31 samples, 0.05%) + + + +genunix`kmem_cache_alloc (32 samples, 0.06%) + + + +genunix`disp_lock_exit (2,096 samples, 3.65%) +genu.. + + +unix`mutex_exit (49 samples, 0.09%) + + + +unix`copyinstr (18 samples, 0.03%) + + + +ufs`ufs_lookup (46 samples, 0.08%) + + + +genunix`clear_stale_fd (10 samples, 0.02%) + + + +genunix`rwst_destroy (296 samples, 0.52%) + + + +genunix`syscall_mstate (1,336 samples, 2.33%) +g.. + + +genunix`kmem_alloc (934 samples, 1.63%) + + + +unix`atomic_add_32 (325 samples, 0.57%) + + + +unix`mutex_enter (947 samples, 1.65%) + + + +unix`mutex_exit (56 samples, 0.10%) + + + +unix`mutex_enter (318 samples, 0.55%) + + + +lofs`lo_root (80 samples, 0.14%) + + + +genunix`lookuppnvp (44,242 samples, 77.02%) +genunix`lookuppnvp + + +genunix`lookupnameat (46,075 samples, 80.21%) +genunix`lookupnameat + + +unix`setbackdq (5 samples, 0.01%) + + + +lofs`lo_root (31 samples, 0.05%) + + + +genunix`kmem_cache_alloc (17 samples, 0.03%) + + + +unix`mutex_exit (212 samples, 0.37%) + + + +genunix`vn_vfsrlock (2,414 samples, 4.20%) +genun.. + + +genunix`vfs_matchops (28 samples, 0.05%) + + + +unix`prunstop (36 samples, 0.06%) + + + +unix`mutex_exit (155 samples, 0.27%) + + + +unix`mutex_init (31 samples, 0.05%) + + + +unix`atomic_add_32_nv (100 samples, 0.17%) + + + +genunix`lookupnameat (69 samples, 0.12%) + + + +unix`_sys_rtt (6 samples, 0.01%) + + + +genunix`kmem_cache_alloc (49 samples, 0.09%) + + + +unix`tsc_gethrtimeunscaled (17 samples, 0.03%) + + + +genunix`fop_lookup (29,216 samples, 50.86%) +genunix`fop_lookup + + +unix`mutex_exit (142 samples, 0.25%) + + + +genunix`crgetmapped (31 samples, 0.05%) + + + +unix`do_splx (1,993 samples, 3.47%) +uni.. + + +genunix`kmem_cache_free (22 samples, 0.04%) + + + +unix`mutex_enter (95 samples, 0.17%) + + + +genunix`crhold (11 samples, 0.02%) + + + +unix`mutex_enter (823 samples, 1.43%) + + + +unix`mutex_exit (29 samples, 0.05%) + + + +genunix`vn_vfsrlock (3,342 samples, 5.82%) +genunix.. + + +unix`tsc_gethrtimeunscaled (13 samples, 0.02%) + + + +genunix`vn_rele (73 samples, 0.13%) + + + +unix`mutex_exit (337 samples, 0.59%) + + + +genunix`vn_vfslocks_getlock (973 samples, 1.69%) + + + +zfs`specvp_check (20 samples, 0.03%) + + + +genunix`vsd_free (14 samples, 0.02%) + + + +unix`mutex_enter (314 samples, 0.55%) + + + +genunix`cv_destroy (81 samples, 0.14%) + + + +genunix`cv_broadcast (25 samples, 0.04%) + + + +unix`mutex_enter (122 samples, 0.21%) + + + +unix`mutex_exit (55 samples, 0.10%) + + + +genunix`set_errno (24 samples, 0.04%) + + + +genunix`cv_destroy (42 samples, 0.07%) + + + +genunix`fd_find (13 samples, 0.02%) + + + +genunix`vn_invalid (47 samples, 0.08%) + + + +genunix`vfs_matchops (336 samples, 0.58%) + + + +unix`tsc_gethrtimeunscaled (59 samples, 0.10%) + + + +genunix`fop_inactive (39 samples, 0.07%) + + + +genunix`kmem_free (693 samples, 1.21%) + + + +genunix`syscall_mstate (412 samples, 0.72%) + + + +genunix`thread_lock (670 samples, 1.17%) + + + +lofs`lsave (162 samples, 0.28%) + + + +unix`atomic_add_64 (95 samples, 0.17%) + + + +genunix`audit_getstate (66 samples, 0.11%) + + + +genunix`dnlc_lookup (70 samples, 0.12%) + + + +genunix`vn_mountedvfs (30 samples, 0.05%) + + + +genunix`cv_broadcast (19 samples, 0.03%) + + + +genunix`kmem_alloc (533 samples, 0.93%) + + + +unix`mutex_exit (160 samples, 0.28%) + + + +genunix`memcmp (38 samples, 0.07%) + + + +unix`strlen (1,238 samples, 2.16%) +u.. + + +genunix`lookuppnatcred (12 samples, 0.02%) + + + +genunix`crfree (13 samples, 0.02%) + + + +lofs`table_lock_enter (43 samples, 0.07%) + + + +genunix`rwst_exit (18 samples, 0.03%) + + + +genunix`cv_destroy (31 samples, 0.05%) + + + +genunix`rwst_init (236 samples, 0.41%) + + + +genunix`vn_vfslocks_rele (1,420 samples, 2.47%) +ge.. + + +genunix`falloc (36 samples, 0.06%) + + + +genunix`setf (187 samples, 0.33%) + + + +zfs`zfs_fastaccesschk_execute (50 samples, 0.09%) + + + +genunix`vn_vfslocks_getlock (120 samples, 0.21%) + + + +genunix`fd_reserve (9 samples, 0.02%) + + + +genunix`vn_setops (160 samples, 0.28%) + + + +unix`sys_syscall (51,908 samples, 90.37%) +unix`sys_syscall + + +genunix`kmem_free (115 samples, 0.20%) + + + +genunix`vsd_free (48 samples, 0.08%) + + + +genunix`rexit (5 samples, 0.01%) + + + +genunix`vn_mountedvfs (11 samples, 0.02%) + + + +genunix`lookuppnatcred (44,681 samples, 77.79%) +genunix`lookuppnatcred + + +unix`splr (92 samples, 0.16%) + + + +genunix`vn_vfsrlock (13 samples, 0.02%) + + + +unix`mutex_exit (371 samples, 0.65%) + + + +genunix`kmem_cache_free (5 samples, 0.01%) + + + +genunix`dnlc_lookup (263 samples, 0.46%) + + + +genunix`audit_unfalloc (32 samples, 0.06%) + + + +unix`0xfffffffffb8001d6 (13 samples, 0.02%) + + + +genunix`rwst_destroy (146 samples, 0.25%) + + + +genunix`gethrtime_unscaled (182 samples, 0.32%) + + + +unix`mutex_enter (575 samples, 1.00%) + + + +unix`mutex_exit (148 samples, 0.26%) + + + +genunix`ufalloc_file (294 samples, 0.51%) + + + +unix`mutex_exit (163 samples, 0.28%) + + + +unix`membar_consumer (106 samples, 0.18%) + + + +genunix`crgetmapped (36 samples, 0.06%) + + + +genunix`memcmp (277 samples, 0.48%) + + + +genunix`cv_destroy (77 samples, 0.13%) + + + +genunix`kmem_cache_free (116 samples, 0.20%) + + + +genunix`kmem_cache_alloc (29 samples, 0.05%) + + + +genunix`fd_reserve (8 samples, 0.01%) + + + +zfs`zfs_lookup (946 samples, 1.65%) + + + +genunix`kmem_alloc (795 samples, 1.38%) + + + +unix`tsc_gethrtimeunscaled (11 samples, 0.02%) + + + +genunix`segvn_faultpage (7 samples, 0.01%) + + + +genunix`set_errno (9 samples, 0.02%) + + + +unix`splr (400 samples, 0.70%) + + + +genunix`rwst_destroy (32 samples, 0.06%) + + + +genunix`rwst_init (28 samples, 0.05%) + + + +unix`atomic_add_32 (292 samples, 0.51%) + + + +unix`0xfffffffffb800ca0 (517 samples, 0.90%) + + + +genunix`syscall_mstate (89 samples, 0.15%) + + + +genunix`kmem_alloc (73 samples, 0.13%) + + + +genunix`vn_vfsunlock (40 samples, 0.07%) + + + +unix`mutex_enter (1,202 samples, 2.09%) +u.. + + +lofs`makelfsnode (28 samples, 0.05%) + + + +unix`0xfffffffffb800c86 (472 samples, 0.82%) + + + +genunix`vn_rele (6,943 samples, 12.09%) +genunix`vn_rele + + +unix`mutex_exit (56 samples, 0.10%) + + + +genunix`kmem_cache_free (51 samples, 0.09%) + + + +genunix`gethrtime_unscaled (11 samples, 0.02%) + + + +unix`pagefault (13 samples, 0.02%) + + + +genunix`secpolicy_vnode_access2 (217 samples, 0.38%) + + + +genunix`vn_vfslocks_getlock (1,357 samples, 2.36%) +g.. + + +unix`bcmp (295 samples, 0.51%) + + + +unix`mutex_enter (97 samples, 0.17%) + + + +unix`membar_consumer (123 samples, 0.21%) + + + +genunix`audit_getstate (16 samples, 0.03%) + + + +unix`mutex_enter (455 samples, 0.79%) + + + +lofs`makelonode (4,212 samples, 7.33%) +lofs`makel.. + + +genunix`kmem_cache_alloc (168 samples, 0.29%) + + + +genunix`vn_vfslocks_getlock (62 samples, 0.11%) + + + +genunix`secpolicy_vnode_access2 (72 samples, 0.13%) + + + +genunix`kmem_cache_free (73 samples, 0.13%) + + + +genunix`vn_reinit (424 samples, 0.74%) + + + +genunix`pn_getcomponent (454 samples, 0.79%) + + + +genunix`fsop_root (297 samples, 0.52%) + + + +genunix`crgetuid (30 samples, 0.05%) + + + +genunix`kmem_free (785 samples, 1.37%) + + + +unix`mutex_exit (171 samples, 0.30%) + + + +genunix`crgetmapped (58 samples, 0.10%) + + + +unix`mutex_enter (299 samples, 0.52%) + + + +genunix`rwst_exit (167 samples, 0.29%) + + + +genunix`audit_falloc (8 samples, 0.01%) + + + +genunix`rwst_exit (110 samples, 0.19%) + + + +genunix`exit (5 samples, 0.01%) + + + +unix`mutex_exit (250 samples, 0.44%) + + + +lofs`freelonode (35 samples, 0.06%) + + + +genunix`rwst_tryenter (37 samples, 0.06%) + + + +ufs`ufs_iaccess (91 samples, 0.16%) + + + +unix`tsc_gethrtimeunscaled (12 samples, 0.02%) + + + +genunix`kmem_cache_alloc (241 samples, 0.42%) + + + +FSS`fss_preempt (8 samples, 0.01%) + + + +genunix`fd_reserve (15 samples, 0.03%) + + + +genunix`cv_broadcast (16 samples, 0.03%) + + + +genunix`crgetmapped (57 samples, 0.10%) + + + +unix`mutex_exit (379 samples, 0.66%) + + + +unix`mutex_destroy (31 samples, 0.05%) + + + +lofs`table_lock_enter (189 samples, 0.33%) + + + +genunix`rwst_enter_common (264 samples, 0.46%) + + + +genunix`kmem_free (11 samples, 0.02%) + + + +unix`atomic_add_32 (134 samples, 0.23%) + + + +genunix`ufalloc (551 samples, 0.96%) + + + +genunix`audit_falloc (313 samples, 0.54%) + + + +lofs`lo_lookup (19,887 samples, 34.62%) +lofs`lo_lookup + + +unix`atomic_add_64 (110 samples, 0.19%) + + + +genunix`vn_vfsunlock (2,372 samples, 4.13%) +genu.. + + +genunix`openat (17 samples, 0.03%) + + + +unix`bcmp (45 samples, 0.08%) + + + +genunix`audit_getstate (62 samples, 0.11%) + + + +genunix`crfree (9 samples, 0.02%) + + + +genunix`kmem_cache_free (18 samples, 0.03%) + + + +genunix`vn_vfslocks_rele (903 samples, 1.57%) + + + +genunix`vn_invalid (20 samples, 0.03%) + + + +genunix`vn_vfslocks_rele (50 samples, 0.09%) + + + +genunix`lookuppnvp (10 samples, 0.02%) + + + +genunix`fd_find (161 samples, 0.28%) + + + +ufs`ufs_lookup (5,399 samples, 9.40%) +ufs`ufs_lookup + + +unix`0xfffffffffb800c7c (42 samples, 0.07%) + + + +genunix`vn_openat (14 samples, 0.02%) + + + +genunix`setf (16 samples, 0.03%) + + + +genunix`traverse (7,243 samples, 12.61%) +genunix`traverse + + +genunix`rwst_tryenter (734 samples, 1.28%) + + + +unix`mutex_enter (366 samples, 0.64%) + + + +genunix`fop_lookup (6,470 samples, 11.26%) +genunix`fop_lookup + + +unix`mutex_exit (135 samples, 0.24%) + + + +lofs`makelfsnode (82 samples, 0.14%) + + + +genunix`copen (7 samples, 0.01%) + + + diff --git a/tests/benchmarks/_script/flamegraph/example-perf-stacks.txt.gz b/tests/benchmarks/_script/flamegraph/example-perf-stacks.txt.gz new file mode 100644 index 00000000000..e7b762b88bb Binary files /dev/null and b/tests/benchmarks/_script/flamegraph/example-perf-stacks.txt.gz differ diff --git a/tests/benchmarks/_script/flamegraph/example-perf.svg b/tests/benchmarks/_script/flamegraph/example-perf.svg new file mode 100644 index 00000000000..d4896fc3503 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/example-perf.svg @@ -0,0 +1,4895 @@ + + + + + + + + + + + + + +Flame Graph + +Reset Zoom +Search + + +rw_verify_area (9 samples, 0.68%) + + + +_raw_spin_lock_irqsave (2 samples, 0.15%) + + + +sun/nio/ch/FileDispatcherImpl:.read0 (31 samples, 2.36%) +s.. + + +do_sync_read (22 samples, 1.67%) + + + +sun/nio/ch/SocketChannelImpl:.write (209 samples, 15.89%) +sun/nio/ch/SocketChannel.. + + +timerqueue_del (1 samples, 0.08%) + + + +io/netty/channel/AdaptiveRecvByteBufAllocator$HandleImpl:.record (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptRuntime:.setObjectProp (86 samples, 6.54%) +org/mozi.. + + +read_tsc (1 samples, 0.08%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_http_js_2 (14 samples, 1.06%) + + + +org/mozilla/javascript/ScriptableObject:.putImpl (45 samples, 3.42%) +org.. + + +netdev_pick_tx (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.write (33 samples, 2.51%) +io.. + + +java/lang/String:.equals (1 samples, 0.08%) + + + +system_call_fastpath (7 samples, 0.53%) + + + +GCTaskManager::get_task (1 samples, 0.08%) + + + +security_file_free (1 samples, 0.08%) + + + +apparmor_socket_recvmsg (5 samples, 0.38%) + + + +itable stub (1 samples, 0.08%) + + + +skb_release_data (3 samples, 0.23%) + + + +hrtimer_try_to_cancel (3 samples, 0.23%) + + + +default_wake_function (25 samples, 1.90%) +d.. + + +__remove_hrtimer (3 samples, 0.23%) + + + +epoll_ctl (1 samples, 0.08%) + + + +fsnotify (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.nameOrFunction (4 samples, 0.30%) + + + +tcp_clean_rtx_queue (1 samples, 0.08%) + + + +tcp_send_delayed_ack (5 samples, 0.38%) + + + +org/mozilla/javascript/ScriptRuntime:.nameOrFunction (1 samples, 0.08%) + + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +tcp_v4_rcv (87 samples, 6.62%) +tcp_v4_rcv + + +aeProcessEvents (1 samples, 0.08%) + + + +org/mozilla/javascript/NativeJavaObject:.initMembers (1 samples, 0.08%) + + + +schedule_preempt_disabled (2 samples, 0.15%) + + + +kfree (1 samples, 0.08%) + + + +sun/nio/ch/SocketChannelImpl:.write (1 samples, 0.08%) + + + +sk_reset_timer (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptableObject:.putImpl (6 samples, 0.46%) + + + +remote_function (4 samples, 0.30%) + + + +io/netty/handler/codec/http/HttpObjectDecoder:.skipControlCharacters (1 samples, 0.08%) + + + +intel_pmu_enable_all (4 samples, 0.30%) + + + +mod_timer (5 samples, 0.38%) + + + +io/netty/handler/codec/MessageToMessageEncoder:.write (31 samples, 2.36%) +i.. + + +io/netty/buffer/UnpooledHeapByteBuf:.init (1 samples, 0.08%) + + + +intel_pmu_enable_all (4 samples, 0.30%) + + + +io/netty/channel/AbstractChannelHandlerContext:.write (2 samples, 0.15%) + + + +enqueue_hrtimer (1 samples, 0.08%) + + + +itable stub (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject$Slot:.setAttributes (12 samples, 0.91%) + + + +cpuidle_idle_call (6 samples, 0.46%) + + + +system_call (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.get (2 samples, 0.15%) + + + +[unknown] (6 samples, 0.46%) + + + +Monitor::IWait (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.bind (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (40 samples, 3.04%) +org.. + + +__wake_up_sync_key (3 samples, 0.23%) + + + +system_call_fastpath (1 samples, 0.08%) + + + +vfs_write (85 samples, 6.46%) +vfs_write + + +mod_timer (2 samples, 0.15%) + + + +rcu_sysidle_enter (1 samples, 0.08%) + + + +oopDesc* PSPromotionManager::copy_to_survivor_spacefalse (1 samples, 0.08%) + + + +__wake_up_common (2 samples, 0.15%) + + + +io/netty/channel/AbstractChannelHandlerContext:.fireChannelRead (637 samples, 48.44%) +io/netty/channel/AbstractChannelHandlerContext:.fireChannelRead + + +_raw_spin_lock_irqsave (2 samples, 0.15%) + + + +ScavengeRootsTask::do_it (1 samples, 0.08%) + + + +tcp_urg (1 samples, 0.08%) + + + +aa_file_perm (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.setObjectProp (21 samples, 1.60%) + + + +__remove_hrtimer (1 samples, 0.08%) + + + +put_filp (1 samples, 0.08%) + + + +skb_free_head (1 samples, 0.08%) + + + +apparmor_file_permission (1 samples, 0.08%) + + + +ktime_get (1 samples, 0.08%) + + + +JavaCalls::call_virtual (956 samples, 72.70%) +JavaCalls::call_virtual + + +__copy_skb_header (1 samples, 0.08%) + + + +__slab_alloc (1 samples, 0.08%) + + + +cpuidle_idle_call (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.has (30 samples, 2.28%) +o.. + + +ip_queue_xmit (51 samples, 3.88%) +ip_q.. + + +org/mozilla/javascript/NativeCall:.init (15 samples, 1.14%) + + + +tcp_ack (9 samples, 0.68%) + + + +sys_ioctl (5 samples, 0.38%) + + + +fsnotify (2 samples, 0.15%) + + + +sk_reset_timer (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptableObject:.addKnownAbsentSlot (1 samples, 0.08%) + + + +lapic_next_deadline (2 samples, 0.15%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_Server2_js_1:.call (79 samples, 6.01%) +org/mozi.. + + +sys_execve (1 samples, 0.08%) + + + +perf_event_enable (5 samples, 0.38%) + + + +sys_futex (1 samples, 0.08%) + + + +java/lang/String:.init (1 samples, 0.08%) + + + +inet_recvmsg (7 samples, 0.53%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_http_js_2 (2 samples, 0.15%) + + + +io/netty/util/internal/AppendableCharSequence:.substring (4 samples, 0.30%) + + + +_raw_spin_lock_irqsave (1 samples, 0.08%) + + + +x86_pmu_enable (4 samples, 0.30%) + + + +__libc_read (1 samples, 0.08%) + + + +tcp_sendmsg (77 samples, 5.86%) +tcp_sen.. + + +cpuidle_enter_state (12 samples, 0.91%) + + + +flush_tlb_mm_range (1 samples, 0.08%) + + + +ksize (1 samples, 0.08%) + + + +cpu_startup_entry (44 samples, 3.35%) +cpu.. + + +pthread_self (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +_raw_spin_lock_bh (1 samples, 0.08%) + + + +io/netty/channel/DefaultChannelPipeline$HeadContext:.flush (2 samples, 0.15%) + + + +tcp_rcv_established (23 samples, 1.75%) + + + +org/mozilla/javascript/BaseFunction:.execIdCall (48 samples, 3.65%) +org/.. + + +lapic_next_deadline (1 samples, 0.08%) + + + +[unknown] (197 samples, 14.98%) +[unknown] + + +io/netty/channel/AbstractChannelHandlerContext:.fireChannelReadComplete (242 samples, 18.40%) +io/netty/channel/AbstractCha.. + + +bictcp_cong_avoid (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.nameOrFunction (5 samples, 0.38%) + + + +JavaCalls::call_virtual (956 samples, 72.70%) +JavaCalls::call_virtual + + +resched_task (2 samples, 0.15%) + + + +sock_wfree (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (4 samples, 0.30%) + + + +io/netty/buffer/AbstractByteBuf:.getByte (1 samples, 0.08%) + + + +check_preempt_curr (2 samples, 0.15%) + + + +io/netty/channel/ChannelOutboundBuffer:.progress (1 samples, 0.08%) + + + +tcp_current_mss (1 samples, 0.08%) + + + +__execve (1 samples, 0.08%) + + + +hrtimer_force_reprogram (1 samples, 0.08%) + + + +__GI___mprotect (1 samples, 0.08%) + + + +ep_send_events_proc (9 samples, 0.68%) + + + +schedule (11 samples, 0.84%) + + + +org/mozilla/javascript/IdScriptableObject:.put (3 samples, 0.23%) + + + +org/mozilla/javascript/IdScriptableObject:.get (1 samples, 0.08%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_http_js_2 (409 samples, 31.10%) +org/mozilla/javascript/gen/file__root_vert_x_2_1_.. + + +io/netty/channel/ChannelOutboundBuffer:.decrementPendingOutboundBytes (2 samples, 0.15%) + + + +ktime_get_real (1 samples, 0.08%) + + + +aa_revalidate_sk (2 samples, 0.15%) + + + +stats_record (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +__perf_event_enable (4 samples, 0.30%) + + + +__alloc_skb (9 samples, 0.68%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_http_js_2 (17 samples, 1.29%) + + + +socket_readable (2 samples, 0.15%) + + + +ns_to_timeval (1 samples, 0.08%) + + + +ip_rcv (33 samples, 2.51%) +ip.. + + +SafepointSynchronize::begin (1 samples, 0.08%) + + + +java/nio/DirectByteBuffer:.duplicate (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (2 samples, 0.15%) + + + +org/mozilla/javascript/IdScriptableObject:.get (1 samples, 0.08%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_http_js_2 (20 samples, 1.52%) + + + +io/netty/channel/nio/AbstractNioByteChannel$NioByteUnsafe:.read (939 samples, 71.41%) +io/netty/channel/nio/AbstractNioByteChannel$NioByteUnsafe:.read + + +raw_local_deliver (1 samples, 0.08%) + + + +__dev_queue_xmit (4 samples, 0.30%) + + + +skb_copy_datagram_iovec (3 samples, 0.23%) + + + +apic_timer_interrupt (1 samples, 0.08%) + + + +do_vfs_ioctl (5 samples, 0.38%) + + + +do_sync_read (8 samples, 0.61%) + + + +system_call_after_swapgs (1 samples, 0.08%) + + + +_raw_spin_lock_irqsave (1 samples, 0.08%) + + + +call_function_single_interrupt (4 samples, 0.30%) + + + +io/netty/handler/codec/http/HttpHeaders:.hash (4 samples, 0.30%) + + + +io/netty/handler/codec/http/DefaultHttpMessage:.init (2 samples, 0.15%) + + + +rcu_sysidle_enter (1 samples, 0.08%) + + + +java/nio/channels/spi/AbstractInterruptibleChannel:.end (3 samples, 0.23%) + + + +clockevents_program_event (2 samples, 0.15%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_http_js_2 (9 samples, 0.68%) + + + +tcp_try_rmem_schedule (2 samples, 0.15%) + + + +__schedule (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.putImpl (10 samples, 0.76%) + + + +tcp_v4_md5_lookup (1 samples, 0.08%) + + + +CardTableExtension::scavenge_contents_parallel (20 samples, 1.52%) + + + +aa_revalidate_sk (1 samples, 0.08%) + + + +__fsnotify_parent (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.get (7 samples, 0.53%) + + + +sk_reset_timer (5 samples, 0.38%) + + + +__schedule (2 samples, 0.15%) + + + +io/netty/handler/codec/http/DefaultHttpHeaders:.init (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +io/netty/channel/nio/AbstractNioByteChannel:.doWrite (225 samples, 17.11%) +io/netty/channel/nio/Abstr.. + + +timerqueue_add (1 samples, 0.08%) + + + +_raw_spin_unlock_irqrestore (2 samples, 0.15%) + + + +try_to_wake_up (24 samples, 1.83%) +t.. + + +org/mozilla/javascript/IdScriptableObject:.setAttributes (4 samples, 0.30%) + + + +io/netty/handler/codec/http/HttpResponseEncoder:.acceptOutboundMessage (1 samples, 0.08%) + + + +rw_verify_area (2 samples, 0.15%) + + + +x86_pmu_commit_txn (4 samples, 0.30%) + + + +alloc_pages_current (1 samples, 0.08%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_streams_j (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.setAttributes (7 samples, 0.53%) + + + +org/mozilla/javascript/IdScriptableObject:.get (1 samples, 0.08%) + + + +tcp_transmit_skb (1 samples, 0.08%) + + + +sock_aio_read.part.8 (7 samples, 0.53%) + + + +sys_read (28 samples, 2.13%) +s.. + + +org/mozilla/javascript/ScriptRuntime:.setObjectProp (28 samples, 2.13%) +o.. + + +JavaCalls::call_helper (956 samples, 72.70%) +JavaCalls::call_helper + + +ttwu_do_wakeup (1 samples, 0.08%) + + + +generic_smp_call_function_single_interrupt (4 samples, 0.30%) + + + +mutex_unlock (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpHeaders:.hash (2 samples, 0.15%) + + + +http_parser_execute (1 samples, 0.08%) + + + +mod_timer (5 samples, 0.38%) + + + +system_call_fastpath (1 samples, 0.08%) + + + +tcp_recvmsg (13 samples, 0.99%) + + + +__slab_alloc (1 samples, 0.08%) + + + +__alloc_skb (7 samples, 0.53%) + + + +clockevents_program_event (1 samples, 0.08%) + + + +vfs_read (18 samples, 1.37%) + + + +__internal_add_timer (1 samples, 0.08%) + + + +epoll_wait (1 samples, 0.08%) + + + +lock_sock_nested (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +native_write_msr_safe (4 samples, 0.30%) + + + +Interpreter (956 samples, 72.70%) +Interpreter + + +org/mozilla/javascript/ScriptableObject:.getBase (4 samples, 0.30%) + + + +dev_hard_start_xmit (9 samples, 0.68%) + + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +ip_output (46 samples, 3.50%) +ip_.. + + +account_entity_enqueue (1 samples, 0.08%) + + + +itable stub (1 samples, 0.08%) + + + +ip_rcv (1 samples, 0.08%) + + + +io/netty/buffer/AbstractByteBuf:.writeBytes (5 samples, 0.38%) + + + +tcp_clean_rtx_queue (14 samples, 1.06%) + + + +io/netty/channel/AbstractChannelHandlerContext:.flush (1 samples, 0.08%) + + + +sys_read (21 samples, 1.60%) + + + +[unknown] (10 samples, 0.76%) + + + +java/util/concurrent/ConcurrentHashMap:.get (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannel:.hashCode (4 samples, 0.30%) + + + +rcu_idle_enter (1 samples, 0.08%) + + + +gettimeofday@plt (1 samples, 0.08%) + + + +__do_softirq (103 samples, 7.83%) +__do_softirq + + +org/mozilla/javascript/ScriptRuntime:.nameOrFunction (8 samples, 0.61%) + + + +io/netty/buffer/AbstractByteBuf:.writeBytes (3 samples, 0.23%) + + + +inotify_add_watch (1 samples, 0.08%) + + + +fdval (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpHeaders:.encode (7 samples, 0.53%) + + + +unsafe_arraycopy (1 samples, 0.08%) + + + +sk_stream_alloc_skb (10 samples, 0.76%) + + + +lock_timer_base.isra.35 (1 samples, 0.08%) + + + +ip_local_out (121 samples, 9.20%) +ip_local_out + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +io/netty/util/internal/AppendableCharSequence:.substring (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptRuntime:.bind (1 samples, 0.08%) + + + +ep_poll (53 samples, 4.03%) +ep_p.. + + +lock_hrtimer_base.isra.19 (1 samples, 0.08%) + + + +InstanceKlass::oop_push_contents (1 samples, 0.08%) + + + +cpuacct_charge (1 samples, 0.08%) + + + +harmonize_features.isra.92.part.93 (1 samples, 0.08%) + + + +update_rq_clock.part.63 (1 samples, 0.08%) + + + +native_write_msr_safe (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpObjectDecoder:.findNonWhitespace (1 samples, 0.08%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_streams_j (1 samples, 0.08%) + + + +org/mozilla/javascript/BaseFunction:.construct (156 samples, 11.86%) +org/mozilla/javas.. + + +_raw_spin_lock (2 samples, 0.15%) + + + +cpu_function_call (5 samples, 0.38%) + + + +fget_light (2 samples, 0.15%) + + + +start_kernel (24 samples, 1.83%) +s.. + + +native_write_msr_safe (2 samples, 0.15%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_http_js_2 (511 samples, 38.86%) +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io.. + + +ipv4_mtu (1 samples, 0.08%) + + + +__schedule (11 samples, 0.84%) + + + +system_call_fastpath (88 samples, 6.69%) +system_ca.. + + +io/netty/channel/nio/NioEventLoop:.select (7 samples, 0.53%) + + + +org/mozilla/javascript/IdScriptableObject:.get (3 samples, 0.23%) + + + +org/mozilla/javascript/TopLevel:.getBuiltinPrototype (7 samples, 0.53%) + + + +sun/nio/ch/IOUtil:.readIntoNativeBuffer (31 samples, 2.36%) +s.. + + +itable stub (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.write (6 samples, 0.46%) + + + +timerqueue_del (1 samples, 0.08%) + + + +__tcp_v4_send_check (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.nameOrFunction (3 samples, 0.23%) + + + +io/netty/buffer/AbstractByteBuf:.writeBytes (4 samples, 0.30%) + + + +org/mozilla/javascript/WrapFactory:.wrapAsJavaObject (1 samples, 0.08%) + + + +io/netty/handler/codec/http/DefaultHttpMessage:.init (2 samples, 0.15%) + + + +_raw_spin_lock_irqsave (2 samples, 0.15%) + + + +org/mozilla/javascript/IdScriptableObject:.put (7 samples, 0.53%) + + + +io/netty/handler/codec/http/DefaultHttpHeaders:.add0 (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (8 samples, 0.61%) + + + +java/util/ArrayList:.ensureCapacityInternal (1 samples, 0.08%) + + + +__wake_up_locked (25 samples, 1.90%) +_.. + + +java/util/HashMap:.getNode (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.toObjectOrNull (1 samples, 0.08%) + + + +__tcp_push_pending_frames (1 samples, 0.08%) + + + +[unknown] (61 samples, 4.64%) +[unkn.. + + +__slab_free (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.put (11 samples, 0.84%) + + + +sock_def_readable (5 samples, 0.38%) + + + +gmain (1 samples, 0.08%) + + + +_raw_spin_lock_irqsave (1 samples, 0.08%) + + + +__kmalloc_reserve.isra.26 (3 samples, 0.23%) + + + +org/mozilla/javascript/ScriptableObject:.putImpl (5 samples, 0.38%) + + + +org/mozilla/javascript/NativeCall:.init (20 samples, 1.52%) + + + +org/mozilla/javascript/WrapFactory:.wrap (1 samples, 0.08%) + + + +_raw_spin_lock_bh (1 samples, 0.08%) + + + +aeProcessEvents (3 samples, 0.23%) + + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.executor (1 samples, 0.08%) + + + +fget_light (2 samples, 0.15%) + + + +io/netty/buffer/PooledByteBufAllocator:.newDirectBuffer (2 samples, 0.15%) + + + +menu_select (1 samples, 0.08%) + + + +generic_smp_call_function_single_interrupt (4 samples, 0.30%) + + + +org/mozilla/javascript/TopLevel:.getBuiltinPrototype (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.setAttributes (1 samples, 0.08%) + + + +schedule_hrtimeout_range_clock (20 samples, 1.52%) + + + +io/netty/buffer/UnreleasableByteBuf:.duplicate (1 samples, 0.08%) + + + +tick_program_event (2 samples, 0.15%) + + + +__netif_receive_skb_core (33 samples, 2.51%) +__.. + + +java/util/HashMap:.getNode (1 samples, 0.08%) + + + +io/netty/buffer/AbstractByteBuf:.forEachByteAsc0 (2 samples, 0.15%) + + + +get_next_timer_interrupt (2 samples, 0.15%) + + + +vtable stub (1 samples, 0.08%) + + + +start_secondary (44 samples, 3.35%) +sta.. + + +skb_release_all (3 samples, 0.23%) + + + +update_cfs_rq_blocked_load (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.get (1 samples, 0.08%) + + + +call_function_single_interrupt (4 samples, 0.30%) + + + +sun/nio/ch/SocketChannelImpl:.read (40 samples, 3.04%) +sun.. + + +sys_epoll_wait (1 samples, 0.08%) + + + +tcp_check_space (1 samples, 0.08%) + + + +__wake_up_common (25 samples, 1.90%) +_.. + + +native_sched_clock (1 samples, 0.08%) + + + +fget_light (3 samples, 0.23%) + + + +sys_inotify_add_watch (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject$Slot:.getValue (1 samples, 0.08%) + + + +_raw_spin_lock (1 samples, 0.08%) + + + +smp_call_function_single_interrupt (4 samples, 0.30%) + + + +__kmalloc_node_track_caller (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject$RelinkedSlot:.getValue (1 samples, 0.08%) + + + +tcp_send_mss (6 samples, 0.46%) + + + +sched_clock (1 samples, 0.08%) + + + +kmem_cache_alloc_node (4 samples, 0.30%) + + + +_raw_spin_lock_irqsave (1 samples, 0.08%) + + + +tick_sched_handle.isra.17 (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getBase (1 samples, 0.08%) + + + +[unknown] (6 samples, 0.46%) + + + +io/netty/util/internal/AppendableCharSequence:.substring (2 samples, 0.15%) + + + +__inet_lookup_established (2 samples, 0.15%) + + + +apparmor_file_permission (1 samples, 0.08%) + + + +dst_release (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpObjectEncoder:.encode (1 samples, 0.08%) + + + +open_exec (1 samples, 0.08%) + + + +tcp_transmit_skb (132 samples, 10.04%) +tcp_transmit_skb + + +ttwu_do_wakeup (5 samples, 0.38%) + + + +idle_cpu (1 samples, 0.08%) + + + +__lll_unlock_wake (1 samples, 0.08%) + + + +[unknown] (7 samples, 0.53%) + + + +security_file_permission (2 samples, 0.15%) + + + +[unknown] (1 samples, 0.08%) + + + +__switch_to (1 samples, 0.08%) + + + +io/netty/channel/DefaultChannelPromise:.trySuccess (3 samples, 0.23%) + + + +org/mozilla/javascript/IdScriptableObject:.has (7 samples, 0.53%) + + + +native_write_msr_safe (3 samples, 0.23%) + + + +io/netty/handler/codec/http/DefaultHttpHeaders:.add0 (2 samples, 0.15%) + + + +do_softirq (103 samples, 7.83%) +do_softirq + + +rw_verify_area (1 samples, 0.08%) + + + +tcp_poll (1 samples, 0.08%) + + + +tcp_rearm_rto (5 samples, 0.38%) + + + +io/netty/channel/AbstractChannelHandlerContext:.newPromise (1 samples, 0.08%) + + + +tick_nohz_idle_exit (5 samples, 0.38%) + + + +org/mozilla/javascript/BaseFunction:.execIdCall (60 samples, 4.56%) +org/m.. + + +org/mozilla/javascript/ScriptableObject:.putImpl (1 samples, 0.08%) + + + +_copy_from_user (1 samples, 0.08%) + + + +__netif_receive_skb (34 samples, 2.59%) +__.. + + +java/util/concurrent/ConcurrentHashMap:.get (3 samples, 0.23%) + + + +fput (1 samples, 0.08%) + + + +JavaThread::thread_main_inner (956 samples, 72.70%) +JavaThread::thread_main_inner + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +io/netty/util/Recycler:.get (2 samples, 0.15%) + + + +[unknown] (6 samples, 0.46%) + + + +__dev_queue_xmit (1 samples, 0.08%) + + + +common_file_perm (1 samples, 0.08%) + + + +org/mozilla/javascript/JavaMembers:.get (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.getPropFunctionAndThisHelper (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.findInstanceIdInfo (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (6 samples, 0.46%) + + + +jiffies_to_timeval (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.setName (2 samples, 0.15%) + + + +PSRootsClosurefalse::do_oop (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +vtable stub (1 samples, 0.08%) + + + +skb_clone (4 samples, 0.30%) + + + +OldToYoungRootsTask::do_it (20 samples, 1.52%) + + + +io/netty/channel/ChannelDuplexHandler:.flush (237 samples, 18.02%) +io/netty/channel/ChannelDupl.. + + +org/mozilla/javascript/ScriptableObject:.putImpl (1 samples, 0.08%) + + + +mutex_unlock (1 samples, 0.08%) + + + +hrtimer_force_reprogram (1 samples, 0.08%) + + + +stub_execve (1 samples, 0.08%) + + + +sock_poll (3 samples, 0.23%) + + + +org/mozilla/javascript/IdScriptableObject:.setAttributes (5 samples, 0.38%) + + + +_raw_spin_lock_bh (1 samples, 0.08%) + + + +native_write_msr_safe (3 samples, 0.23%) + + + +sched_clock_cpu (1 samples, 0.08%) + + + +io/netty/channel/DefaultChannelPipeline$HeadContext:.flush (232 samples, 17.64%) +io/netty/channel/DefaultCha.. + + +org/mozilla/javascript/IdScriptableObject:.get (4 samples, 0.30%) + + + +rcu_idle_enter (1 samples, 0.08%) + + + +java (995 samples, 75.67%) +java + + +tcp_cleanup_rbuf (2 samples, 0.15%) + + + +org/mozilla/javascript/NativeJavaObject:.initMembers (4 samples, 0.30%) + + + +org/mozilla/javascript/NativeCall:.init (16 samples, 1.22%) + + + +http_parser_execute (2 samples, 0.15%) + + + +_raw_spin_unlock_irqrestore (1 samples, 0.08%) + + + +ThreadRootsTask::do_it (3 samples, 0.23%) + + + +mutex_lock (3 samples, 0.23%) + + + +cpu_startup_entry (23 samples, 1.75%) + + + +itable stub (1 samples, 0.08%) + + + +__srcu_read_lock (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (5 samples, 0.38%) + + + +org/vertx/java/core/impl/DefaultVertx:.setContext (1 samples, 0.08%) + + + +ip_rcv_finish (89 samples, 6.77%) +ip_rcv_fi.. + + +response_complete (13 samples, 0.99%) + + + +io/netty/handler/codec/http/HttpObjectDecoder:.skipControlCharacters (2 samples, 0.15%) + + + +tcp_v4_rcv (27 samples, 2.05%) +t.. + + +ktime_get_ts (2 samples, 0.15%) + + + +tick_nohz_restart (4 samples, 0.30%) + + + +io/netty/channel/ChannelOutboundHandlerAdapter:.flush (1 samples, 0.08%) + + + +sun/nio/ch/FileDispatcherImpl:.write0 (2 samples, 0.15%) + + + +GCTaskThread::run (28 samples, 2.13%) +G.. + + +io/netty/handler/codec/http/HttpHeaders:.encode (1 samples, 0.08%) + + + +__kmalloc_node_track_caller (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.put (25 samples, 1.90%) +o.. + + +org/mozilla/javascript/IdScriptableObject:.has (2 samples, 0.15%) + + + +atomic_notifier_call_chain (1 samples, 0.08%) + + + +remote_function (4 samples, 0.30%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +common_file_perm (1 samples, 0.08%) + + + +sun/nio/ch/SocketChannelImpl:.isConnected (2 samples, 0.15%) + + + +org/mozilla/javascript/IdScriptableObject:.has (9 samples, 0.68%) + + + +tcp_init_tso_segs (1 samples, 0.08%) + + + +org/mozilla/javascript/BaseFunction:.findInstanceIdInfo (1 samples, 0.08%) + + + +tcp_v4_do_rcv (77 samples, 5.86%) +tcp_v4_.. + + +__tcp_push_pending_frames (61 samples, 4.64%) +__tcp.. + + +org/mozilla/javascript/IdScriptableObject:.findInstanceIdInfo (1 samples, 0.08%) + + + +native_read_tsc (1 samples, 0.08%) + + + +tcp_md5_do_lookup (1 samples, 0.08%) + + + +do_sync_write (186 samples, 14.14%) +do_sync_write + + +cpuidle_enter_state (4 samples, 0.30%) + + + +ep_poll_callback (1 samples, 0.08%) + + + +x86_pmu_enable (4 samples, 0.30%) + + + +copy_user_generic_string (3 samples, 0.23%) + + + +perf_pmu_enable (4 samples, 0.30%) + + + +vfs_read (25 samples, 1.90%) +v.. + + +x86_64_start_reservations (24 samples, 1.83%) +x.. + + +security_file_permission (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.addKnownAbsentSlot (1 samples, 0.08%) + + + +nr_iowait_cpu (1 samples, 0.08%) + + + +__hrtimer_start_range_ns (2 samples, 0.15%) + + + +system_call_after_swapgs (1 samples, 0.08%) + + + +release_sock (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (11 samples, 0.84%) + + + +_raw_spin_unlock_irqrestore (1 samples, 0.08%) + + + +itable stub (1 samples, 0.08%) + + + +call_stub (956 samples, 72.70%) +call_stub + + +dev_hard_start_xmit (3 samples, 0.23%) + + + +dev_queue_xmit (11 samples, 0.84%) + + + +task_nice (2 samples, 0.15%) + + + +ip_finish_output (119 samples, 9.05%) +ip_finish_out.. + + +__remove_hrtimer (1 samples, 0.08%) + + + +sys_epoll_wait (4 samples, 0.30%) + + + +rcu_cpu_has_callbacks (1 samples, 0.08%) + + + +java/lang/ThreadLocal:.get (1 samples, 0.08%) + + + +rcu_idle_exit (1 samples, 0.08%) + + + +net_rx_action (97 samples, 7.38%) +net_rx_act.. + + +lock_sock_nested (1 samples, 0.08%) + + + +mod_timer (2 samples, 0.15%) + + + +apparmor_file_free_security (1 samples, 0.08%) + + + +__remove_hrtimer (1 samples, 0.08%) + + + +tcp_established_options (4 samples, 0.30%) + + + +sk_reset_timer (5 samples, 0.38%) + + + +io/netty/channel/ChannelOutboundHandlerAdapter:.flush (235 samples, 17.87%) +io/netty/channel/ChannelOut.. + + +org/mozilla/javascript/NativeFunction:.initScriptFunction (1 samples, 0.08%) + + + +menu_reflect (1 samples, 0.08%) + + + +__slab_alloc (3 samples, 0.23%) + + + +PSScavengeKlassClosure::do_klass (1 samples, 0.08%) + + + +__ip_local_out (1 samples, 0.08%) + + + +org/mozilla/javascript/TopLevel:.getBuiltinPrototype (5 samples, 0.38%) + + + +tcp_send_delayed_ack (3 samples, 0.23%) + + + +arch_local_irq_save (1 samples, 0.08%) + + + +__kmalloc_node_track_caller (1 samples, 0.08%) + + + +java/nio/channels/spi/AbstractInterruptibleChannel:.end (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.createSlot (15 samples, 1.14%) + + + +apparmor_file_permission (2 samples, 0.15%) + + + +mutex_lock (2 samples, 0.15%) + + + +sys_epoll_ctl (5 samples, 0.38%) + + + +hrtimer_interrupt (1 samples, 0.08%) + + + +ParallelTaskTerminator::offer_termination (2 samples, 0.15%) + + + +dequeue_entity (4 samples, 0.30%) + + + +io/netty/buffer/PooledByteBuf:.deallocate (5 samples, 0.38%) + + + +org/mozilla/javascript/IdScriptableObject:.findInstanceIdInfo (1 samples, 0.08%) + + + +wrk (240 samples, 18.25%) +wrk + + +perf_pmu_enable (4 samples, 0.30%) + + + +org/mozilla/javascript/IdScriptableObject:.get (2 samples, 0.15%) + + + +remote_function (4 samples, 0.30%) + + + +__GI___ioctl (5 samples, 0.38%) + + + +socket_readable (2 samples, 0.15%) + + + +epoll_ctl (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.putImpl (21 samples, 1.60%) + + + +tick_nohz_stop_sched_tick (4 samples, 0.30%) + + + +io/netty/channel/DefaultChannelPipeline$HeadContext:.write (6 samples, 0.46%) + + + +tcp_write_xmit (147 samples, 11.18%) +tcp_write_xmit + + +org/mozilla/javascript/ScriptRuntime:.getPropFunctionAndThisHelper (5 samples, 0.38%) + + + +lock_hrtimer_base.isra.19 (1 samples, 0.08%) + + + +inet_recvmsg (17 samples, 1.29%) + + + +native_write_msr_safe (4 samples, 0.30%) + + + +ip_local_out (46 samples, 3.50%) +ip_.. + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.has (1 samples, 0.08%) + + + +local_bh_enable (42 samples, 3.19%) +loc.. + + +hrtimer_start_range_ns (3 samples, 0.23%) + + + +jlong_disjoint_arraycopy (1 samples, 0.08%) + + + +ep_send_events_proc (4 samples, 0.30%) + + + +org/mozilla/javascript/ScriptableObject:.getParentScope (3 samples, 0.23%) + + + +itable stub (1 samples, 0.08%) + + + +path_openat (1 samples, 0.08%) + + + +activate_task (7 samples, 0.53%) + + + +pick_next_task_fair (1 samples, 0.08%) + + + +security_file_permission (5 samples, 0.38%) + + + +io/netty/channel/ChannelOutboundBuffer:.decrementPendingOutboundBytes (1 samples, 0.08%) + + + +system_call_fastpath (56 samples, 4.26%) +syste.. + + +org/mozilla/javascript/NativeFunction:.initScriptFunction (6 samples, 0.46%) + + + +ip_local_deliver_finish (30 samples, 2.28%) +i.. + + +sock_read (2 samples, 0.15%) + + + +deactivate_task (7 samples, 0.53%) + + + +lock_sock_nested (1 samples, 0.08%) + + + +sock_put (1 samples, 0.08%) + + + +mod_timer (3 samples, 0.23%) + + + +aeProcessEvents (171 samples, 13.00%) +aeProcessEvents + + +io/netty/buffer/AbstractByteBuf:.ensureWritable (2 samples, 0.15%) + + + +tick_nohz_stop_sched_tick (5 samples, 0.38%) + + + +org/mozilla/javascript/NativeJavaMethod:.findCachedFunction (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpMethod:.valueOf (2 samples, 0.15%) + + + +hrtimer_try_to_cancel (1 samples, 0.08%) + + + +system_call (1 samples, 0.08%) + + + +hrtimer_cancel (1 samples, 0.08%) + + + +system_call_fastpath (196 samples, 14.90%) +system_call_fastpath + + +io/netty/channel/AbstractChannelHandlerContext:.read (2 samples, 0.15%) + + + +[unknown] (10 samples, 0.76%) + + + +io/netty/handler/codec/MessageToMessageEncoder:.write (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.put (47 samples, 3.57%) +org.. + + +jlong_disjoint_arraycopy (1 samples, 0.08%) + + + +[unknown] (1 samples, 0.08%) + + + +native_read_tsc (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpObjectDecoder:.splitHeader (8 samples, 0.61%) + + + +intel_pmu_enable_all (4 samples, 0.30%) + + + +io/netty/handler/codec/http/DefaultHttpHeaders:.set (3 samples, 0.23%) + + + +read_tsc (1 samples, 0.08%) + + + +_raw_spin_lock (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.toObjectOrNull (1 samples, 0.08%) + + + +_raw_spin_lock_irqsave (1 samples, 0.08%) + + + +dequeue_task_fair (6 samples, 0.46%) + + + +org/mozilla/javascript/ScriptRuntime:.getPropFunctionAndThisHelper (1 samples, 0.08%) + + + +do_softirq (38 samples, 2.89%) +do.. + + +response_complete (2 samples, 0.15%) + + + +get_next_timer_interrupt (3 samples, 0.23%) + + + +__perf_event_enable (4 samples, 0.30%) + + + +_raw_spin_lock_irqsave (1 samples, 0.08%) + + + +lapic_next_deadline (3 samples, 0.23%) + + + +org/mozilla/javascript/ScriptableObject:.createSlot (4 samples, 0.30%) + + + +fput (1 samples, 0.08%) + + + +tcp_rearm_rto (3 samples, 0.23%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (2 samples, 0.15%) + + + +group_sched_in (4 samples, 0.30%) + + + +__getnstimeofday (1 samples, 0.08%) + + + +java/util/Arrays:.copyOf (1 samples, 0.08%) + + + +local_bh_enable (104 samples, 7.91%) +local_bh_en.. + + +tcp_event_new_data_sent (3 samples, 0.23%) + + + +read_tsc (2 samples, 0.15%) + + + +system_call_fastpath (6 samples, 0.46%) + + + +tcp_prequeue (1 samples, 0.08%) + + + +call_function_single_interrupt (4 samples, 0.30%) + + + +get_page_from_freelist (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject$Slot:.setAttributes (5 samples, 0.38%) + + + +io/netty/channel/AbstractChannelHandlerContext:.read (4 samples, 0.30%) + + + +org/vertx/java/core/http/impl/VertxHttpHandler:.write (34 samples, 2.59%) +or.. + + +_raw_spin_lock_irqsave (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpObjectDecoder:.splitInitialLine (5 samples, 0.38%) + + + +org/mozilla/javascript/ScriptableObject:.getParentScope (4 samples, 0.30%) + + + +org/mozilla/javascript/IdScriptableObject:.get (3 samples, 0.23%) + + + +org/mozilla/javascript/ScriptRuntime:.getObjectProp (1 samples, 0.08%) + + + +swapper (72 samples, 5.48%) +swapper + + +ktime_get (1 samples, 0.08%) + + + +_raw_spin_unlock_irqrestore (1 samples, 0.08%) + + + +java/lang/ThreadLocal:.get (1 samples, 0.08%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_http_js_2 (416 samples, 31.63%) +org/mozilla/javascript/gen/file__root_vert_x_2_1_5.. + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +__schedule (4 samples, 0.30%) + + + +bictcp_cong_avoid (3 samples, 0.23%) + + + +tcp_rcv_space_adjust (2 samples, 0.15%) + + + +JavaThread::run (956 samples, 72.70%) +JavaThread::run + + +apparmor_socket_sendmsg (1 samples, 0.08%) + + + +InstanceKlass::oop_push_contents (8 samples, 0.61%) + + + +sun/nio/ch/SocketChannelImpl:.isConnected (1 samples, 0.08%) + + + +__libc_start_main (6 samples, 0.46%) + + + +tcp_is_cwnd_limited (2 samples, 0.15%) + + + +sun/nio/ch/FileDispatcherImpl:.write0 (203 samples, 15.44%) +sun/nio/ch/FileDispatch.. + + +internal_add_timer (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptableObject$Slot:.setAttributes (2 samples, 0.15%) + + + +[unknown] (30 samples, 2.28%) +[.. + + +io/netty/buffer/AbstractByteBufAllocator:.heapBuffer (3 samples, 0.23%) + + + +__tcp_push_pending_frames (149 samples, 11.33%) +__tcp_push_pendi.. + + +ClassLoaderDataGraph::oops_do (1 samples, 0.08%) + + + +tick_nohz_stop_idle (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.executor (1 samples, 0.08%) + + + +__skb_clone (1 samples, 0.08%) + + + +tcp_ack (20 samples, 1.52%) + + + +__inet_lookup_established (4 samples, 0.30%) + + + +org/mozilla/javascript/NativeJavaMethod:.findFunction (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptableObject:.createSlot (4 samples, 0.30%) + + + +enqueue_task (7 samples, 0.53%) + + + +sock_def_readable (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptableObject:.addKnownAbsentSlot (1 samples, 0.08%) + + + +tcp_data_queue (39 samples, 2.97%) +tc.. + + +security_file_permission (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.executor (1 samples, 0.08%) + + + +sun/reflect/DelegatingMethodAccessorImpl:.invoke (66 samples, 5.02%) +sun/re.. + + +__skb_clone (1 samples, 0.08%) + + + +org/vertx/java/platform/impl/RhinoContextFactory:.onContextCreated (1 samples, 0.08%) + + + +tcp_poll (1 samples, 0.08%) + + + +netif_skb_dev_features (1 samples, 0.08%) + + + +ep_scan_ready_list.isra.9 (4 samples, 0.30%) + + + +native_write_msr_safe (4 samples, 0.30%) + + + +copy_user_generic_string (1 samples, 0.08%) + + + +intel_pmu_enable_all (4 samples, 0.30%) + + + +__switch_to (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.has (4 samples, 0.30%) + + + +__hrtimer_start_range_ns (3 samples, 0.23%) + + + +__srcu_read_lock (2 samples, 0.15%) + + + +io/netty/channel/AbstractChannelHandlerContext:.validatePromise (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptableObject:.createSlot (11 samples, 0.84%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (8 samples, 0.61%) + + + +org/mozilla/javascript/NativeJavaMethod:.findCachedFunction (2 samples, 0.15%) + + + +sock_poll (1 samples, 0.08%) + + + +tick_program_event (3 samples, 0.23%) + + + +tcp_transmit_skb (55 samples, 4.18%) +tcp_.. + + +org/mozilla/javascript/NativeFunction:.initScriptFunction (1 samples, 0.08%) + + + +org/mozilla/javascript/WrapFactory:.wrap (5 samples, 0.38%) + + + +java/lang/String:.getBytes (3 samples, 0.23%) + + + +org/mozilla/javascript/NativeJavaObject:.initMembers (4 samples, 0.30%) + + + +bictcp_cong_avoid (1 samples, 0.08%) + + + +ktime_get_real (3 samples, 0.23%) + + + +java/lang/ThreadLocal:.get (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (21 samples, 1.60%) + + + +rcu_sysidle_force_exit (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptableObject$Slot:.getValue (2 samples, 0.15%) + + + +aa_file_perm (1 samples, 0.08%) + + + +tick_sched_timer (1 samples, 0.08%) + + + +sk_reset_timer (3 samples, 0.23%) + + + +org/mozilla/javascript/IdScriptableObject:.findInstanceIdInfo (1 samples, 0.08%) + + + +msecs_to_jiffies (1 samples, 0.08%) + + + +ipv4_dst_check (1 samples, 0.08%) + + + +tcp_write_xmit (60 samples, 4.56%) +tcp_w.. + + +io/netty/util/Recycler:.recycle (1 samples, 0.08%) + + + +group_sched_in (4 samples, 0.30%) + + + +generic_exec_single (1 samples, 0.08%) + + + +menu_select (2 samples, 0.15%) + + + +org/mozilla/javascript/BaseFunction:.construct (1 samples, 0.08%) + + + +process_backlog (97 samples, 7.38%) +process_ba.. + + +__pthread_disable_asynccancel (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpObjectDecoder:.decode (57 samples, 4.33%) +io/ne.. + + +schedule_preempt_disabled (4 samples, 0.30%) + + + +rcu_idle_exit (2 samples, 0.15%) + + + +tcp_send_mss (1 samples, 0.08%) + + + +[unknown] (26 samples, 1.98%) +[.. + + +org/mozilla/javascript/ScriptRuntime:.nameOrFunction (1 samples, 0.08%) + + + +inet_sendmsg (78 samples, 5.93%) +inet_se.. + + +__getnstimeofday (1 samples, 0.08%) + + + +kfree_skbmem (1 samples, 0.08%) + + + +smp_apic_timer_interrupt (1 samples, 0.08%) + + + +kmalloc_slab (2 samples, 0.15%) + + + +[unknown] (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpObjectDecoder:.readHeaders (2 samples, 0.15%) + + + +ip_rcv_finish (32 samples, 2.43%) +ip.. + + +io/netty/channel/DefaultChannelPipeline$HeadContext:.read (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.putImpl (8 samples, 0.61%) + + + +inet_ehashfn (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.createSlot (33 samples, 2.51%) +or.. + + +frame::oops_do_internal (1 samples, 0.08%) + + + +thread_entry (956 samples, 72.70%) +thread_entry + + +sun/nio/ch/SelectorImpl:.select (7 samples, 0.53%) + + + +_raw_spin_lock_irq (1 samples, 0.08%) + + + +ttwu_do_activate.constprop.74 (12 samples, 0.91%) + + + +skb_copy_datagram_iovec (1 samples, 0.08%) + + + +Interpreter (956 samples, 72.70%) +Interpreter + + +io/netty/handler/codec/http/HttpObjectDecoder:.readHeaders (22 samples, 1.67%) + + + +org/vertx/java/core/http/impl/DefaultHttpServer$ServerHandler:.doMessageReceived (540 samples, 41.06%) +org/vertx/java/core/http/impl/DefaultHttpServer$ServerHandler:.doM.. + + +_raw_spin_unlock_irqrestore (1 samples, 0.08%) + + + +fget_light (1 samples, 0.08%) + + + +io/netty/handler/codec/http/DefaultHttpHeaders:.contains (1 samples, 0.08%) + + + +kfree_skbmem (1 samples, 0.08%) + + + +__alloc_pages_nodemask (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.addKnownAbsentSlot (1 samples, 0.08%) + + + +enqueue_task_fair (5 samples, 0.38%) + + + +perf_pmu_enable (4 samples, 0.30%) + + + +tcp_rcv_established (73 samples, 5.55%) +tcp_rcv.. + + +org/mozilla/javascript/NativeJavaObject:.initMembers (1 samples, 0.08%) + + + +vfs_write (192 samples, 14.60%) +vfs_write + + +fdval (1 samples, 0.08%) + + + +ip_queue_xmit (122 samples, 9.28%) +ip_queue_xmit + + +sock_aio_write (82 samples, 6.24%) +sock_aio.. + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_streams_j (1 samples, 0.08%) + + + +aeMain (236 samples, 17.95%) +aeMain + + +io/netty/channel/ChannelDuplexHandler:.read (3 samples, 0.23%) + + + +org/vertx/java/core/http/impl/AssembledFullHttpResponse:.toLastContent (2 samples, 0.15%) + + + +internal_add_timer (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +vtable stub (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptableObject:.getParentScope (1 samples, 0.08%) + + + +io/netty/buffer/PooledByteBufAllocator:.newDirectBuffer (2 samples, 0.15%) + + + +SpinPause (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.has (3 samples, 0.23%) + + + +java/nio/charset/CharsetEncoder:.replaceWith (2 samples, 0.15%) + + + +tcp_queue_rcv (2 samples, 0.15%) + + + +stats_record (3 samples, 0.23%) + + + +org/mozilla/javascript/WrapFactory:.wrap (5 samples, 0.38%) + + + +__wake_up_sync_key (27 samples, 2.05%) +_.. + + +__acct_update_integrals (1 samples, 0.08%) + + + +fget_light (1 samples, 0.08%) + + + +local_bh_enable (1 samples, 0.08%) + + + +eth_type_trans (1 samples, 0.08%) + + + +org/vertx/java/core/net/impl/VertxHandler:.channelRead (555 samples, 42.21%) +org/vertx/java/core/net/impl/VertxHandler:.channelRead + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +sock_put (1 samples, 0.08%) + + + +__kfree_skb (3 samples, 0.23%) + + + +dequeue_task (7 samples, 0.53%) + + + +io/netty/channel/AbstractChannelHandlerContext:.executor (1 samples, 0.08%) + + + +ip_rcv_finish (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.getObjectProp (4 samples, 0.30%) + + + +__tick_nohz_idle_enter (4 samples, 0.30%) + + + +__tcp_ack_snd_check (3 samples, 0.23%) + + + +[unknown] (4 samples, 0.30%) + + + +org/mozilla/javascript/IdScriptableObject:.get (1 samples, 0.08%) + + + +org/mozilla/javascript/NativeFunction:.initScriptFunction (2 samples, 0.15%) + + + +int_sqrt (1 samples, 0.08%) + + + +nmethod::fix_oop_relocations (1 samples, 0.08%) + + + +tcp_sendmsg (1 samples, 0.08%) + + + +java/lang/ThreadLocal:.get (1 samples, 0.08%) + + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +org/mozilla/javascript/BaseFunction:.findPrototypeId (1 samples, 0.08%) + + + +native_write_msr_safe (3 samples, 0.23%) + + + +perf_pmu_enable (4 samples, 0.30%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_http_js_2 (77 samples, 5.86%) +org/moz.. + + +__netif_receive_skb_core (94 samples, 7.15%) +__netif_r.. + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +hrtimer_start (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptRuntime:.setName (5 samples, 0.38%) + + + +__netif_receive_skb (94 samples, 7.15%) +__netif_r.. + + +change_protection (1 samples, 0.08%) + + + +io/netty/channel/ChannelOutboundBuffer:.current (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.flush (233 samples, 17.72%) +io/netty/channel/AbstractCh.. + + +do_softirq_own_stack (103 samples, 7.83%) +do_softirq_.. + + +do_filp_open (1 samples, 0.08%) + + + +x86_pmu_commit_txn (4 samples, 0.30%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +io/netty/util/Recycler:.get (1 samples, 0.08%) + + + +_raw_spin_lock (1 samples, 0.08%) + + + +sun/nio/ch/SocketChannelImpl:.isConnected (1 samples, 0.08%) + + + +change_protection_range (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.putImpl (12 samples, 0.91%) + + + +__libc_write (1 samples, 0.08%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_http_js_2 (513 samples, 39.01%) +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_.. + + +raw_local_deliver (1 samples, 0.08%) + + + +apparmor_file_permission (1 samples, 0.08%) + + + +VMThread::loop (1 samples, 0.08%) + + + +_raw_spin_unlock_irqrestore (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.newPromise (1 samples, 0.08%) + + + +epoll_ctl (7 samples, 0.53%) + + + +io/netty/handler/codec/http/HttpVersion:.compareTo (1 samples, 0.08%) + + + +io/netty/channel/nio/NioEventLoop:.processSelectedKeys (949 samples, 72.17%) +io/netty/channel/nio/NioEventLoop:.processSelectedKeys + + +io/netty/handler/codec/http/DefaultHttpHeaders:.init (1 samples, 0.08%) + + + +java/lang/String:.hashCode (2 samples, 0.15%) + + + +org/mozilla/javascript/IdScriptableObject:.findInstanceIdInfo (1 samples, 0.08%) + + + +effective_load.isra.35 (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (2 samples, 0.15%) + + + +rcu_sysidle_exit (1 samples, 0.08%) + + + +native_load_tls (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.get (1 samples, 0.08%) + + + +org/mozilla/javascript/BaseFunction:.findPrototypeId (1 samples, 0.08%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_streams_j (6 samples, 0.46%) + + + +system_call_fastpath (22 samples, 1.67%) + + + +org/mozilla/javascript/TopLevel:.getBuiltinPrototype (1 samples, 0.08%) + + + +tcp_current_mss (5 samples, 0.38%) + + + +io/netty/channel/ChannelOutboundBuffer:.incrementPendingOutboundBytes (1 samples, 0.08%) + + + +oopDesc* PSPromotionManager::copy_to_survivor_spacefalse (2 samples, 0.15%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_http_js_2 (156 samples, 11.86%) +org/mozilla/javas.. + + +StealTask::do_it (3 samples, 0.23%) + + + +Interpreter (956 samples, 72.70%) +Interpreter + + +PSPromotionManager::drain_stacks_depth (2 samples, 0.15%) + + + +sock_def_readable (32 samples, 2.43%) +so.. + + +org/mozilla/javascript/IdScriptableObject:.findInstanceIdInfo (1 samples, 0.08%) + + + +perf (6 samples, 0.46%) + + + +__wake_up_common (27 samples, 2.05%) +_.. + + +org/mozilla/javascript/IdScriptableObject:.has (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpObjectDecoder$HeaderParser:.process (1 samples, 0.08%) + + + +common_file_perm (1 samples, 0.08%) + + + +io/netty/buffer/AbstractReferenceCountedByteBuf:.release (5 samples, 0.38%) + + + +hrtimer_force_reprogram (3 samples, 0.23%) + + + +org/vertx/java/core/impl/DefaultVertx:.setContext (1 samples, 0.08%) + + + +msecs_to_jiffies (1 samples, 0.08%) + + + +arch_cpu_idle (7 samples, 0.53%) + + + +[unknown] (91 samples, 6.92%) +[unknown] + + +tcp_cleanup_rbuf (1 samples, 0.08%) + + + +release_sock (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.flush (235 samples, 17.87%) +io/netty/channel/AbstractCh.. + + +io/netty/channel/AbstractChannelHandlerContext:.fireChannelReadComplete (1 samples, 0.08%) + + + +fput (2 samples, 0.15%) + + + +bictcp_acked (1 samples, 0.08%) + + + +java/nio/DirectByteBuffer:.duplicate (1 samples, 0.08%) + + + +io/netty/buffer/AbstractByteBuf:.checkIndex (3 samples, 0.23%) + + + +java/util/HashMap:.getNode (2 samples, 0.15%) + + + +ttwu_stat (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpHeaders:.hash (1 samples, 0.08%) + + + +org/vertx/java/core/net/impl/ConnectionBase:.write (38 samples, 2.89%) +or.. + + +local_apic_timer_interrupt (1 samples, 0.08%) + + + +inet_ehashfn (1 samples, 0.08%) + + + +__srcu_read_unlock (1 samples, 0.08%) + + + +java/nio/channels/spi/AbstractInterruptibleChannel:.begin (1 samples, 0.08%) + + + +skb_clone (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.fireChannelRead (562 samples, 42.74%) +io/netty/channel/AbstractChannelHandlerContext:.fireChannelRead + + +ip_local_deliver (89 samples, 6.77%) +ip_local_.. + + +org/mozilla/javascript/ScriptableObject:.createSlot (8 samples, 0.61%) + + + +itable stub (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +__do_softirq (36 samples, 2.74%) +__.. + + +io/netty/handler/codec/http/HttpMethod:.valueOf (2 samples, 0.15%) + + + +clockevents_program_event (3 samples, 0.23%) + + + +tcp_set_skb_tso_segs (1 samples, 0.08%) + + + +io/netty/buffer/AbstractByteBuf:.checkSrcIndex (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpVersion:.compareTo (2 samples, 0.15%) + + + +ttwu_do_activate.constprop.74 (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject$Slot:.setAttributes (6 samples, 0.46%) + + + +cpuidle_idle_call (21 samples, 1.60%) + + + +__hrtimer_start_range_ns (2 samples, 0.15%) + + + +io/netty/channel/ChannelOutboundHandlerAdapter:.read (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptRuntime:.bind (7 samples, 0.53%) + + + +x86_pmu_enable (4 samples, 0.30%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +sun/nio/ch/EPollArrayWrapper:.poll (5 samples, 0.38%) + + + +org/mozilla/javascript/IdScriptableObject:.setAttributes (12 samples, 0.91%) + + + +sock_read (3 samples, 0.23%) + + + +HandleArea::oops_do (1 samples, 0.08%) + + + +tcp_v4_send_check (1 samples, 0.08%) + + + +tcp_wfree (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject$Slot:.getValue (1 samples, 0.08%) + + + +sun/nio/ch/FileDispatcherImpl:.read0 (1 samples, 0.08%) + + + +org/vertx/java/core/net/impl/VertxHandler:.channelReadComplete (240 samples, 18.25%) +org/vertx/java/core/net/impl.. + + +rcu_bh_qs (1 samples, 0.08%) + + + +lock_timer_base.isra.35 (1 samples, 0.08%) + + + +io/netty/buffer/PooledUnsafeDirectByteBuf:.setBytes (42 samples, 3.19%) +io/.. + + +sun/nio/cs/UTF_8$Encoder:.init (3 samples, 0.23%) + + + +io/netty/channel/ChannelDuplexHandler:.read (1 samples, 0.08%) + + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +account_entity_dequeue (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.executor (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (17 samples, 1.29%) + + + +io/netty/handler/codec/http/HttpObjectEncoder:.encode (17 samples, 1.29%) + + + +org/mozilla/javascript/ScriptableObject:.addKnownAbsentSlot (2 samples, 0.15%) + + + +ksize (1 samples, 0.08%) + + + +[unknown] (4 samples, 0.30%) + + + +org/mozilla/javascript/ScriptRuntime:.toObjectOrNull (2 samples, 0.15%) + + + +fget_light (1 samples, 0.08%) + + + +sched_clock_idle_sleep_event (1 samples, 0.08%) + + + +sock_aio_write (185 samples, 14.07%) +sock_aio_write + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_streams_j (1 samples, 0.08%) + + + +smp_call_function_single (5 samples, 0.38%) + + + +org/mozilla/javascript/TopLevel:.getBuiltinPrototype (1 samples, 0.08%) + + + +ip_rcv (91 samples, 6.92%) +ip_rcv + + +tcp_sendmsg (176 samples, 13.38%) +tcp_sendmsg + + +release_sock (1 samples, 0.08%) + + + +ep_poll_callback (27 samples, 2.05%) +e.. + + +update_min_vruntime (1 samples, 0.08%) + + + +java/lang/Integer:.toString (1 samples, 0.08%) + + + +itable stub (1 samples, 0.08%) + + + +do_softirq_own_stack (37 samples, 2.81%) +do.. + + +io/netty/buffer/AbstractByteBufAllocator:.heapBuffer (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (3 samples, 0.23%) + + + +org/mozilla/javascript/ScriptableObject:.addKnownAbsentSlot (1 samples, 0.08%) + + + +sched_clock_cpu (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpHeaders:.encodeAscii0 (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptRuntime:.toObjectOrNull (1 samples, 0.08%) + + + +inet_sendmsg (1 samples, 0.08%) + + + +VMThread::run (1 samples, 0.08%) + + + +io/netty/handler/codec/http/DefaultHttpHeaders:.init (1 samples, 0.08%) + + + +java/util/HashMap:.getNode (2 samples, 0.15%) + + + +__run_hrtimer (1 samples, 0.08%) + + + +java/nio/channels/spi/AbstractInterruptibleChannel:.begin (1 samples, 0.08%) + + + +io/netty/handler/codec/http/HttpHeaders:.hash (1 samples, 0.08%) + + + +sun/nio/ch/EPollSelectorImpl:.updateSelectedKeys (1 samples, 0.08%) + + + +x86_pmu_enable (4 samples, 0.30%) + + + +thread_main (237 samples, 18.02%) +thread_main + + +enqueue_hrtimer (1 samples, 0.08%) + + + +ep_poll (4 samples, 0.30%) + + + +sock_aio_read (7 samples, 0.53%) + + + +io/netty/buffer/AbstractByteBuf:.checkSrcIndex (3 samples, 0.23%) + + + +sock_aio_read (22 samples, 1.67%) + + + +io/netty/handler/codec/http/HttpObjectDecoder$LineParser:.parse (6 samples, 0.46%) + + + +sys_epoll_ctl (5 samples, 0.38%) + + + +java/lang/String:.init (4 samples, 0.30%) + + + +rb_erase (1 samples, 0.08%) + + + +select_estimate_accuracy (5 samples, 0.38%) + + + +link_path_walk (1 samples, 0.08%) + + + +sock_aio_read.part.8 (22 samples, 1.67%) + + + +local_bh_enable_ip (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.putImpl (3 samples, 0.23%) + + + +__tcp_select_window (1 samples, 0.08%) + + + +fget_light (1 samples, 0.08%) + + + +io/netty/buffer/AbstractReferenceCountedByteBuf:.release (4 samples, 0.30%) + + + +acct_account_cputime (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.put (1 samples, 0.08%) + + + +__perf_event_enable (4 samples, 0.30%) + + + +ip_finish_output (46 samples, 3.50%) +ip_.. + + +__tick_nohz_idle_enter (6 samples, 0.46%) + + + +__skb_clone (4 samples, 0.30%) + + + +org/mozilla/javascript/ScriptableObject:.getPrototype (1 samples, 0.08%) + + + +__switch_to (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.write (35 samples, 2.66%) +io.. + + +_raw_spin_lock (1 samples, 0.08%) + + + +_raw_spin_unlock_irqrestore (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.setObjectProp (3 samples, 0.23%) + + + +org/mozilla/javascript/IdScriptableObject:.has (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (2 samples, 0.15%) + + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +idle_cpu (2 samples, 0.15%) + + + +org/mozilla/javascript/IdScriptableObject:.get (1 samples, 0.08%) + + + +io/netty/util/Recycler:.get (1 samples, 0.08%) + + + +native_write_msr_safe (4 samples, 0.30%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (2 samples, 0.15%) + + + +ip_output (119 samples, 9.05%) +ip_output + + +io/netty/buffer/AbstractByteBuf:.forEachByteAsc0 (3 samples, 0.23%) + + + +skb_network_protocol (1 samples, 0.08%) + + + +enqueue_entity (5 samples, 0.38%) + + + +tcp_established_options (1 samples, 0.08%) + + + +update_process_times (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.read (3 samples, 0.23%) + + + +update_rq_clock.part.63 (1 samples, 0.08%) + + + +sun/nio/ch/EPollArrayWrapper:.epollWait (4 samples, 0.30%) + + + +sock_poll (2 samples, 0.15%) + + + +io/netty/channel/nio/NioEventLoop:.processSelectedKeysOptimized (949 samples, 72.17%) +io/netty/channel/nio/NioEventLoop:.processSelectedKeysOptimized + + +java_start (985 samples, 74.90%) +java_start + + +mprotect_fixup (1 samples, 0.08%) + + + +ep_scan_ready_list.isra.9 (20 samples, 1.52%) + + + +tcp_v4_do_rcv (23 samples, 1.75%) + + + +sk_stream_alloc_skb (7 samples, 0.53%) + + + +update_curr (2 samples, 0.15%) + + + +tcp_wfree (2 samples, 0.15%) + + + +user_path_at_empty (1 samples, 0.08%) + + + +io/netty/handler/codec/http/DefaultHttpHeaders:.init (1 samples, 0.08%) + + + +io/netty/buffer/AbstractByteBuf:.writeBytes (1 samples, 0.08%) + + + +sun/nio/ch/NativeThread:.current (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.get (1 samples, 0.08%) + + + +JavaThread::oops_do (3 samples, 0.23%) + + + +org/mozilla/javascript/ScriptableObject:.getBase (2 samples, 0.15%) + + + +ip_local_deliver (1 samples, 0.08%) + + + +org/vertx/java/core/http/impl/ServerConnection:.handleRequest (526 samples, 40.00%) +org/vertx/java/core/http/impl/ServerConnection:.handleRequest + + +__hrtimer_start_range_ns (3 samples, 0.23%) + + + +org/mozilla/javascript/NativeFunction:.initScriptFunction (10 samples, 0.76%) + + + +hrtimer_start (1 samples, 0.08%) + + + +intel_idle (11 samples, 0.84%) + + + +org/mozilla/javascript/IdScriptableObject:.has (3 samples, 0.23%) + + + +ktime_get (1 samples, 0.08%) + + + +update_cfs_rq_blocked_load (1 samples, 0.08%) + + + +org/mozilla/javascript/WrapFactory:.setJavaPrimitiveWrap (1 samples, 0.08%) + + + +sun/nio/ch/NativeThread:.current (1 samples, 0.08%) + + + +system_call_fastpath (28 samples, 2.13%) +s.. + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +sched_clock_cpu (1 samples, 0.08%) + + + +lapic_next_deadline (3 samples, 0.23%) + + + +io/netty/buffer/UnpooledHeapByteBuf:.init (1 samples, 0.08%) + + + +filename_lookup (1 samples, 0.08%) + + + +jni_fast_GetIntField (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.put (6 samples, 0.46%) + + + +x86_pmu_commit_txn (4 samples, 0.30%) + + + +__kfree_skb (1 samples, 0.08%) + + + +org/mozilla/javascript/NativeJavaMethod:.call (10 samples, 0.76%) + + + +process_backlog (34 samples, 2.59%) +pr.. + + +all (1,315 samples, 100%) + + + +io/netty/handler/codec/http/HttpHeaders:.encodeAscii0 (2 samples, 0.15%) + + + +system_call_after_swapgs (6 samples, 0.46%) + + + +_raw_spin_unlock_bh (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +tcp_event_new_data_sent (6 samples, 0.46%) + + + +_raw_spin_unlock_bh (1 samples, 0.08%) + + + +tcp_schedule_loss_probe (3 samples, 0.23%) + + + +tcp_check_space (3 samples, 0.23%) + + + +dev_queue_xmit (4 samples, 0.30%) + + + +tick_nohz_restart (6 samples, 0.46%) + + + +__tcp_ack_snd_check (5 samples, 0.38%) + + + +user_path_at (1 samples, 0.08%) + + + +socket_readable (60 samples, 4.56%) +socke.. + + +org/mozilla/javascript/ScriptableObject:.getSlot (4 samples, 0.30%) + + + +OopMapSet::all_do (1 samples, 0.08%) + + + +socket_writeable (1 samples, 0.08%) + + + +internal_add_timer (1 samples, 0.08%) + + + +select_task_rq_fair (4 samples, 0.30%) + + + +loopback_xmit (5 samples, 0.38%) + + + +sys_epoll_wait (56 samples, 4.26%) +sys_e.. + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +clockevents_program_event (3 samples, 0.23%) + + + +io/netty/buffer/PooledByteBuf:.deallocate (2 samples, 0.15%) + + + +io/netty/util/Recycler:.get (1 samples, 0.08%) + + + +fsnotify (1 samples, 0.08%) + + + +org/mozilla/javascript/NativeJavaMethod:.call (74 samples, 5.63%) +org/moz.. + + +org/mozilla/javascript/ScriptRuntime:.setObjectProp (37 samples, 2.81%) +or.. + + +schedule_preempt_disabled (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.flush (238 samples, 18.10%) +io/netty/channel/AbstractCha.. + + +tcp_queue_rcv (2 samples, 0.15%) + + + +org/mozilla/javascript/ScriptableObject:.createSlot (5 samples, 0.38%) + + + +tcp_recvmsg (7 samples, 0.53%) + + + +update_curr (1 samples, 0.08%) + + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +tcp_md5_do_lookup (1 samples, 0.08%) + + + +smp_call_function_single_interrupt (4 samples, 0.30%) + + + +netif_rx (2 samples, 0.15%) + + + +enqueue_to_backlog (1 samples, 0.08%) + + + +_raw_spin_unlock_bh (1 samples, 0.08%) + + + +perf_ioctl (5 samples, 0.38%) + + + +tick_program_event (3 samples, 0.23%) + + + +io/netty/handler/codec/ByteToMessageDecoder:.channelRead (635 samples, 48.29%) +io/netty/handler/codec/ByteToMessageDecoder:.channelRead + + +put_prev_task_fair (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.createSlot (15 samples, 1.14%) + + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +sys_mprotect (1 samples, 0.08%) + + + +Monitor::wait (1 samples, 0.08%) + + + +skb_push (1 samples, 0.08%) + + + +java/lang/ThreadLocal:.get (1 samples, 0.08%) + + + +vtable stub (1 samples, 0.08%) + + + +x86_64_start_kernel (24 samples, 1.83%) +x.. + + +[unknown] (1 samples, 0.08%) + + + +kfree (1 samples, 0.08%) + + + +io/netty/channel/AbstractChannelHandlerContext:.fireChannelReadComplete (241 samples, 18.33%) +io/netty/channel/AbstractCha.. + + +io/netty/buffer/AbstractByteBuf:.forEachByteAsc0 (3 samples, 0.23%) + + + +Java_sun_nio_ch_FileDispatcherImpl_write0 (1 samples, 0.08%) + + + +do_execve_common.isra.22 (1 samples, 0.08%) + + + +group_sched_in (4 samples, 0.30%) + + + +socket_writeable (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject$RelinkedSlot:.getValue (1 samples, 0.08%) + + + +java/lang/String:.hashCode (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getTopLevelScope (1 samples, 0.08%) + + + +kmem_cache_alloc_node (2 samples, 0.15%) + + + +org/mozilla/javascript/IdScriptableObject:.has (12 samples, 0.91%) + + + +getnstimeofday (1 samples, 0.08%) + + + +java/util/Arrays:.copyOf (1 samples, 0.08%) + + + +arch_cpu_idle (22 samples, 1.67%) + + + +http_parser_execute (30 samples, 2.28%) +h.. + + +new_slab (2 samples, 0.15%) + + + +io/netty/channel/ChannelDuplexHandler:.flush (1 samples, 0.08%) + + + +generic_smp_call_function_single_interrupt (4 samples, 0.30%) + + + +io/netty/handler/codec/http/DefaultHttpHeaders:.contains (1 samples, 0.08%) + + + +java/lang/ThreadLocal:.get (1 samples, 0.08%) + + + +security_socket_sendmsg (1 samples, 0.08%) + + + +update_cpu_load_nohz (1 samples, 0.08%) + + + +__perf_event_enable (4 samples, 0.30%) + + + +skb_clone (1 samples, 0.08%) + + + +start_thread (237 samples, 18.02%) +start_thread + + +mod_timer (3 samples, 0.23%) + + + +tcp_data_queue (9 samples, 0.68%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (1 samples, 0.08%) + + + +java/lang/String:.trim (1 samples, 0.08%) + + + +net_rx_action (35 samples, 2.66%) +ne.. + + +inet_sendmsg (177 samples, 13.46%) +inet_sendmsg + + +getnstimeofday (3 samples, 0.23%) + + + +io/netty/buffer/AbstractByteBuf:.writeBytes (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getSlot (3 samples, 0.23%) + + + +[unknown] (1 samples, 0.08%) + + + +hrtimer_try_to_cancel (4 samples, 0.30%) + + + +java/nio/charset/Charset:.lookup (2 samples, 0.15%) + + + +org/mozilla/javascript/NativeJavaObject:.initMembers (1 samples, 0.08%) + + + +start_thread (985 samples, 74.90%) +start_thread + + +x86_pmu_commit_txn (4 samples, 0.30%) + + + +org/mozilla/javascript/ScriptableObject:.getParentScope (1 samples, 0.08%) + + + +system_call_fastpath (5 samples, 0.38%) + + + +_raw_spin_lock (1 samples, 0.08%) + + + +menu_select (4 samples, 0.30%) + + + +io/netty/handler/codec/http/DefaultHttpHeaders:.add0 (3 samples, 0.23%) + + + +io/netty/buffer/PooledByteBuf:.deallocate (2 samples, 0.15%) + + + +org/mozilla/javascript/BaseFunction:.findInstanceIdInfo (4 samples, 0.30%) + + + +rest_init (24 samples, 1.83%) +r.. + + +ksoftirqd/3 (1 samples, 0.08%) + + + +group_sched_in (4 samples, 0.30%) + + + +account_user_time (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.name (8 samples, 0.61%) + + + +perf_event_for_each_child (5 samples, 0.38%) + + + +io/netty/handler/codec/http/HttpObjectDecoder:.findWhitespace (1 samples, 0.08%) + + + +java/lang/String:.init (1 samples, 0.08%) + + + +set_next_entity (2 samples, 0.15%) + + + +ip_local_deliver_finish (89 samples, 6.77%) +ip_local_.. + + +org/mozilla/javascript/NativeJavaMethod:.findCachedFunction (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.put (12 samples, 0.91%) + + + +hrtimer_start_range_ns (2 samples, 0.15%) + + + +__internal_add_timer (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.getPropFunctionAndThisHelper (9 samples, 0.68%) + + + +smp_call_function_single_interrupt (4 samples, 0.30%) + + + +_raw_spin_lock_bh (1 samples, 0.08%) + + + +__hrtimer_start_range_ns (1 samples, 0.08%) + + + +lock_sock_nested (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject$PrototypeValues:.ensureId (1 samples, 0.08%) + + + +sk_reset_timer (3 samples, 0.23%) + + + +org/mozilla/javascript/gen/file__root_vert_x_2_1_5_sys_mods_io_vertx_lang_js_1_1_0_vertx_http_js_2 (154 samples, 11.71%) +org/mozilla/javas.. + + +io/netty/handler/codec/ByteToMessageDecoder:.channelRead (1 samples, 0.08%) + + + +security_socket_sendmsg (2 samples, 0.15%) + + + +io/netty/handler/codec/http/DefaultHttpHeaders:.add0 (1 samples, 0.08%) + + + +system_call_after_swapgs (1 samples, 0.08%) + + + +hrtimer_cancel (4 samples, 0.30%) + + + +ObjArrayKlass::oop_push_contents (2 samples, 0.15%) + + + +org/mozilla/javascript/IdScriptableObject:.findInstanceIdInfo (1 samples, 0.08%) + + + +org/mozilla/javascript/IdScriptableObject:.has (1 samples, 0.08%) + + + +schedule_hrtimeout_range (20 samples, 1.52%) + + + +org/mozilla/javascript/ScriptableObject:.putImpl (24 samples, 1.83%) +o.. + + +__fsnotify_parent (1 samples, 0.08%) + + + +vtable stub (1 samples, 0.08%) + + + +__dev_queue_xmit (10 samples, 0.76%) + + + +sys_write (88 samples, 6.69%) +sys_write + + +detach_if_pending (1 samples, 0.08%) + + + +socket_writeable (99 samples, 7.53%) +socket_wri.. + + +org/mozilla/javascript/IdScriptableObject:.findInstanceIdInfo (2 samples, 0.15%) + + + +epoll_ctl (6 samples, 0.46%) + + + +org/vertx/java/core/http/impl/AssembledFullHttpResponse:.toLastContent (1 samples, 0.08%) + + + +org/mozilla/javascript/NativeJavaObject:.get (1 samples, 0.08%) + + + +lock_hrtimer_base.isra.19 (1 samples, 0.08%) + + + +intel_idle (3 samples, 0.23%) + + + +ip_local_deliver (31 samples, 2.36%) +i.. + + +org/mozilla/javascript/IdScriptableObject:.put (23 samples, 1.75%) + + + +io/netty/handler/codec/ByteToMessageDecoder:.channelReadComplete (242 samples, 18.40%) +io/netty/handler/codec/ByteT.. + + +tcp_event_data_recv (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptableObject:.getTopScopeValue (1 samples, 0.08%) + + + +tcp_rearm_rto (3 samples, 0.23%) + + + +org/mozilla/javascript/NativeCall:.init (48 samples, 3.65%) +org/.. + + +tick_nohz_idle_enter (5 samples, 0.38%) + + + +system_call_fastpath (4 samples, 0.30%) + + + +system_call (1 samples, 0.08%) + + + +tick_nohz_idle_enter (6 samples, 0.46%) + + + +tick_program_event (1 samples, 0.08%) + + + +org/mozilla/javascript/ScriptRuntime:.getPropFunctionAndThisHelper (1 samples, 0.08%) + + + +remote_function (4 samples, 0.30%) + + + +tcp_clean_rtx_queue (1 samples, 0.08%) + + + +tick_nohz_idle_exit (7 samples, 0.53%) + + + +org/mozilla/javascript/IdScriptableObject:.put (9 samples, 0.68%) + + + +fsnotify (1 samples, 0.08%) + + + +pick_next_task_fair (2 samples, 0.15%) + + + +do_sync_write (82 samples, 6.24%) +do_sync_.. + + +sys_write (195 samples, 14.83%) +sys_write + + +common_file_perm (1 samples, 0.08%) + + + +account_process_tick (1 samples, 0.08%) + + + diff --git a/tests/benchmarks/_script/flamegraph/files.pl b/tests/benchmarks/_script/flamegraph/files.pl new file mode 100755 index 00000000000..50426b2e47c --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/files.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl -w +# +# files.pl Print file sizes in folded format, for a flame graph. +# +# This helps you understand storage consumed by a file system, by creating +# a flame graph visualization of space consumed. This is basically a Perl +# version of the "find" command, which emits in folded format for piping +# into flamegraph.pl. +# +# Copyright (c) 2017 Brendan Gregg. +# Licensed under the Apache License, Version 2.0 (the "License") +# +# 03-Feb-2017 Brendan Gregg Created this. + +use strict; +use File::Find; + +sub usage { + print STDERR "USAGE: $0 [--xdev] [DIRECTORY]...\n"; + print STDERR " eg, $0 /Users\n"; + print STDERR " To not descend directories on other filesystems:"; + print STDERR " eg, $0 --xdev /\n"; + print STDERR "Intended to be piped to flamegraph.pl. Full example:\n"; + print STDERR " $0 /Users | flamegraph.pl " . + "--hash --countname=bytes > files.svg\n"; + print STDERR " $0 /usr /home /root /etc | flamegraph.pl " . + "--hash --countname=bytes > files.svg\n"; + print STDERR " $0 --xdev / | flamegraph.pl " . + "--hash --countname=bytes > files.svg\n"; + exit 1; +} + +usage() if @ARGV == 0 or $ARGV[0] eq "--help" or $ARGV[0] eq "-h"; + +my $filter_xdev = 0; +my $xdev_id; + +foreach my $dir (@ARGV) { + if ($dir eq "--xdev") { + $filter_xdev = 1; + } else { + find(\&wanted, $dir); + } +} + +sub wanted { + my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) = lstat($_); + return unless defined $size; + if ($filter_xdev) { + if (!$xdev_id) { + $xdev_id = $dev; + } elsif ($xdev_id ne $dev) { + $File::Find::prune = 1; + return; + } + } + my $path = $File::Find::name; + $path =~ tr/\//;/; # delimiter + $path =~ tr/;.a-zA-Z0-9-/_/c; # ditch whitespace and other chars + $path =~ s/^;//; + print "$path $size\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/flamegraph.pl b/tests/benchmarks/_script/flamegraph/flamegraph.pl new file mode 100755 index 00000000000..4536d98bafc --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/flamegraph.pl @@ -0,0 +1,1318 @@ +#!/usr/bin/perl -w +# +# flamegraph.pl flame stack grapher. +# +# This takes stack samples and renders a call graph, allowing hot functions +# and codepaths to be quickly identified. Stack samples can be generated using +# tools such as DTrace, perf, SystemTap, and Instruments. +# +# USAGE: ./flamegraph.pl [options] input.txt > graph.svg +# +# grep funcA input.txt | ./flamegraph.pl [options] > graph.svg +# +# Then open the resulting .svg in a web browser, for interactivity: mouse-over +# frames for info, click to zoom, and ctrl-F to search. +# +# Options are listed in the usage message (--help). +# +# The input is stack frames and sample counts formatted as single lines. Each +# frame in the stack is semicolon separated, with a space and count at the end +# of the line. These can be generated for Linux perf script output using +# stackcollapse-perf.pl, for DTrace using stackcollapse.pl, and for other tools +# using the other stackcollapse programs. Example input: +# +# swapper;start_kernel;rest_init;cpu_idle;default_idle;native_safe_halt 1 +# +# An optional extra column of counts can be provided to generate a differential +# flame graph of the counts, colored red for more, and blue for less. This +# can be useful when using flame graphs for non-regression testing. +# See the header comment in the difffolded.pl program for instructions. +# +# The input functions can optionally have annotations at the end of each +# function name, following a precedent by some tools (Linux perf's _[k]): +# _[k] for kernel +# _[i] for inlined +# _[j] for jit +# _[w] for waker +# Some of the stackcollapse programs support adding these annotations, eg, +# stackcollapse-perf.pl --kernel --jit. They are used merely for colors by +# some palettes, eg, flamegraph.pl --color=java. +# +# The output flame graph shows relative presence of functions in stack samples. +# The ordering on the x-axis has no meaning; since the data is samples, time +# order of events is not known. The order used sorts function names +# alphabetically. +# +# While intended to process stack samples, this can also process stack traces. +# For example, tracing stacks for memory allocation, or resource usage. You +# can use --title to set the title to reflect the content, and --countname +# to change "samples" to "bytes" etc. +# +# There are a few different palettes, selectable using --color. By default, +# the colors are selected at random (except for differentials). Functions +# called "-" will be printed gray, which can be used for stack separators (eg, +# between user and kernel stacks). +# +# HISTORY +# +# This was inspired by Neelakanth Nadgir's excellent function_call_graph.rb +# program, which visualized function entry and return trace events. As Neel +# wrote: "The output displayed is inspired by Roch's CallStackAnalyzer which +# was in turn inspired by the work on vftrace by Jan Boerhout". See: +# https://blogs.oracle.com/realneel/entry/visualizing_callstacks_via_dtrace_and +# +# Copyright 2016 Netflix, Inc. +# Copyright 2011 Joyent, Inc. All rights reserved. +# Copyright 2011 Brendan Gregg. All rights reserved. +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at docs/cddl1.txt or +# http://opensource.org/licenses/CDDL-1.0. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at docs/cddl1.txt. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END +# +# 11-Oct-2014 Adrien Mahieux Added zoom. +# 21-Nov-2013 Shawn Sterling Added consistent palette file option +# 17-Mar-2013 Tim Bunce Added options and more tunables. +# 15-Dec-2011 Dave Pacheco Support for frames with whitespace. +# 10-Sep-2011 Brendan Gregg Created this. + +use strict; + +use Getopt::Long; + +use open qw(:std :utf8); + +# tunables +my $encoding; +my $fonttype = "Verdana"; +my $imagewidth = 1200; # max width, pixels +my $frameheight = 16; # max height is dynamic +my $fontsize = 12; # base text size +my $fontwidth = 0.59; # avg width relative to fontsize +my $minwidth = 0.1; # min function width, pixels or percentage of time +my $nametype = "Function:"; # what are the names in the data? +my $countname = "samples"; # what are the counts in the data? +my $colors = "hot"; # color theme +my $bgcolors = ""; # background color theme +my $nameattrfile; # file holding function attributes +my $timemax; # (override the) sum of the counts +my $factor = 1; # factor to scale counts by +my $hash = 0; # color by function name +my $rand = 0; # color randomly +my $palette = 0; # if we use consistent palettes (default off) +my %palette_map; # palette map hash +my $pal_file = "palette.map"; # palette map file name +my $stackreverse = 0; # reverse stack order, switching merge end +my $inverted = 0; # icicle graph +my $flamechart = 0; # produce a flame chart (sort by time, do not merge stacks) +my $negate = 0; # switch differential hues +my $titletext = "\000"; # centered heading +my $titledefault = "Flame Graph"; # overwritten by --title +my $titleinverted = "Icicle Graph"; # " " +my $searchcolor = "rgb(230,0,230)"; # color for search highlighting +my $notestext = ""; # embedded notes in SVG +my $subtitletext = ""; # second level title (optional) +my $help = 0; + +sub usage { + die < outfile.svg\n + --title TEXT # change title text + --subtitle TEXT # second level title (optional) + --width NUM # width of image (default 1200) + --height NUM # height of each frame (default 16) + --minwidth NUM # omit smaller functions. In pixels or use "%" for + # percentage of time (default 0.1 pixels) + --fonttype FONT # font type (default "Verdana") + --fontsize NUM # font size (default 12) + --countname TEXT # count type label (default "samples") + --nametype TEXT # name type label (default "Function:") + --colors PALETTE # set color palette. choices are: hot (default), mem, + # io, wakeup, chain, java, js, perl, red, green, blue, + # aqua, yellow, purple, orange + --bgcolors COLOR # set background colors. gradient choices are yellow + # (default), blue, green, grey; flat colors use "#rrggbb"; + # or none to omit a background + --hash # colors are keyed by function name hash + --random # colors are randomly generated + --cp # use consistent palette (palette.map) + --reverse # generate stack-reversed flame graph + --inverted # icicle graph + --flamechart # produce a flame chart (sort by time, do not merge stacks) + --negate # switch differential hues (blue<->red) + --notes TEXT # add notes comment in SVG (for debugging) + --help # this message + + eg, + $0 --title="Flame Graph: malloc()" trace.txt > graph.svg +USAGE_END +} + +GetOptions( + 'fonttype=s' => \$fonttype, + 'width=i' => \$imagewidth, + 'height=i' => \$frameheight, + 'encoding=s' => \$encoding, + 'fontsize=f' => \$fontsize, + 'fontwidth=f' => \$fontwidth, + 'minwidth=s' => \$minwidth, + 'title=s' => \$titletext, + 'subtitle=s' => \$subtitletext, + 'nametype=s' => \$nametype, + 'countname=s' => \$countname, + 'nameattr=s' => \$nameattrfile, + 'total=s' => \$timemax, + 'factor=f' => \$factor, + 'colors=s' => \$colors, + 'bgcolors=s' => \$bgcolors, + 'hash' => \$hash, + 'random' => \$rand, + 'cp' => \$palette, + 'reverse' => \$stackreverse, + 'inverted' => \$inverted, + 'flamechart' => \$flamechart, + 'negate' => \$negate, + 'notes=s' => \$notestext, + 'help' => \$help, +) or usage(); +$help && usage(); + +# internals +my $ypad1 = $fontsize * 3; # pad top, include title +my $ypad2 = $fontsize * 2 + 10; # pad bottom, include labels +my $ypad3 = $fontsize * 2; # pad top, include subtitle (optional) +my $xpad = 10; # pad lefm and right +my $framepad = 1; # vertical padding for frames +my $depthmax = 0; +my %Events; +my %nameattr; + +if ($flamechart && $titletext eq "\000") { + $titletext = "Flame Chart"; +} + +if ($titletext eq "\000") { + unless ($inverted) { + $titletext = $titledefault; + } else { + $titletext = $titleinverted; + } +} + +if ($nameattrfile) { + # The name-attribute file format is a function name followed by a tab then + # a sequence of tab separated name=value pairs. + open my $attrfh, $nameattrfile or die "Can't read $nameattrfile: $!\n"; + while (<$attrfh>) { + chomp; + my ($funcname, $attrstr) = split /\t/, $_, 2; + die "Invalid format in $nameattrfile" unless defined $attrstr; + $nameattr{$funcname} = { map { split /=/, $_, 2 } split /\t/, $attrstr }; + } +} + +if ($notestext =~ /[<>]/) { + die "Notes string can't contain < or >" +} + +# Ensure minwidth is a valid floating-point number, +# print usage string if not +my $minwidth_f; +if ($minwidth =~ /^([0-9.]+)%?$/) { + $minwidth_f = $1; +} else { + warn "Value '$minwidth' is invalid for minwidth, expected a float.\n"; + usage(); +} + +# background colors: +# - yellow gradient: default (hot, java, js, perl) +# - green gradient: mem +# - blue gradient: io, wakeup, chain +# - gray gradient: flat colors (red, green, blue, ...) +if ($bgcolors eq "") { + # choose a default + if ($colors eq "mem") { + $bgcolors = "green"; + } elsif ($colors =~ /^(io|wakeup|chain)$/) { + $bgcolors = "blue"; + } elsif ($colors =~ /^(red|green|blue|aqua|yellow|purple|orange)$/) { + $bgcolors = "grey"; + } else { + $bgcolors = "yellow"; + } +} +my ($bgcolor1, $bgcolor2); +if ($bgcolors eq "yellow") { + $bgcolor1 = "#eeeeee"; # background color gradient start + $bgcolor2 = "#eeeeb0"; # background color gradient stop +} elsif ($bgcolors eq "blue") { + $bgcolor1 = "#eeeeee"; $bgcolor2 = "#e0e0ff"; +} elsif ($bgcolors eq "green") { + $bgcolor1 = "#eef2ee"; $bgcolor2 = "#e0ffe0"; +} elsif ($bgcolors eq "grey") { + $bgcolor1 = "#f8f8f8"; $bgcolor2 = "#e8e8e8"; +} elsif ($bgcolors =~ /^#......$/) { + $bgcolor1 = $bgcolor2 = $bgcolors; +} elsif ($bgcolors ne 'none') { + die "Unrecognized bgcolor option \"$bgcolors\"" +} + +# SVG functions +{ package SVG; + sub new { + my $class = shift; + my $self = {}; + bless ($self, $class); + return $self; + } + + sub header { + my ($self, $w, $h) = @_; + my $enc_attr = ''; + if (defined $encoding) { + $enc_attr = qq{ encoding="$encoding"}; + } + $self->{svg} .= < + + + + +SVG + } + + sub include { + my ($self, $content) = @_; + $self->{svg} .= $content; + } + + sub colorAllocate { + my ($self, $r, $g, $b) = @_; + return "rgb($r,$g,$b)"; + } + + sub group_start { + my ($self, $attr) = @_; + + my @g_attr = map { + exists $attr->{$_} ? sprintf(qq/$_="%s"/, $attr->{$_}) : () + } qw(id class); + push @g_attr, $attr->{g_extra} if $attr->{g_extra}; + if ($attr->{href}) { + my @a_attr; + push @a_attr, sprintf qq/xlink:href="%s"/, $attr->{href} if $attr->{href}; + # default target=_top else links will open within SVG + push @a_attr, sprintf qq/target="%s"/, $attr->{target} || "_top"; + push @a_attr, $attr->{a_extra} if $attr->{a_extra}; + $self->{svg} .= sprintf qq/\n/, join(' ', (@a_attr, @g_attr)); + } else { + $self->{svg} .= sprintf qq/\n/, join(' ', @g_attr); + } + + $self->{svg} .= sprintf qq/%s<\/title>/, $attr->{title} + if $attr->{title}; # should be first element within g container + } + + sub group_end { + my ($self, $attr) = @_; + $self->{svg} .= $attr->{href} ? qq/<\/a>\n/ : qq/<\/g>\n/; + } + + sub filledRectangle { + my ($self, $x1, $y1, $x2, $y2, $fill, $extra) = @_; + $x1 = sprintf "%0.1f", $x1; + $x2 = sprintf "%0.1f", $x2; + my $w = sprintf "%0.1f", $x2 - $x1; + my $h = sprintf "%0.1f", $y2 - $y1; + $extra = defined $extra ? $extra : ""; + $self->{svg} .= qq/\n/; + } + + sub stringTTF { + my ($self, $id, $x, $y, $str, $extra) = @_; + $x = sprintf "%0.2f", $x; + $id = defined $id ? qq/id="$id"/ : ""; + $extra ||= ""; + $self->{svg} .= qq/$str<\/text>\n/; + } + + sub svg { + my $self = shift; + return "$self->{svg}\n"; + } + 1; +} + +sub namehash { + # Generate a vector hash for the name string, weighting early over + # later characters. We want to pick the same colors for function + # names across different flame graphs. + my $name = shift; + my $vector = 0; + my $weight = 1; + my $max = 1; + my $mod = 10; + # if module name present, trunc to 1st char + $name =~ s/.(.*?)`//; + foreach my $c (split //, $name) { + my $i = (ord $c) % $mod; + $vector += ($i / ($mod++ - 1)) * $weight; + $max += 1 * $weight; + $weight *= 0.70; + last if $mod > 12; + } + return (1 - $vector / $max) +} + +sub sum_namehash { + my $name = shift; + return unpack("%32W*", $name); +} + +sub random_namehash { + # Generate a random hash for the name string. + # This ensures that functions with the same name have the same color, + # both within a flamegraph and across multiple flamegraphs without + # needing to set a palette and while preserving the original flamegraph + # optic, unlike what happens with --hash. + my $name = shift; + my $hash = sum_namehash($name); + srand($hash); + return rand(1) +} + +sub color { + my ($type, $hash, $name) = @_; + my ($v1, $v2, $v3); + + if ($hash) { + $v1 = namehash($name); + $v2 = $v3 = namehash(scalar reverse $name); + } elsif ($rand) { + $v1 = rand(1); + $v2 = rand(1); + $v3 = rand(1); + } else { + $v1 = random_namehash($name); + $v2 = random_namehash($name); + $v3 = random_namehash($name); + } + + # theme palettes + if (defined $type and $type eq "hot") { + my $r = 205 + int(50 * $v3); + my $g = 0 + int(230 * $v1); + my $b = 0 + int(55 * $v2); + return "rgb($r,$g,$b)"; + } + if (defined $type and $type eq "mem") { + my $r = 0; + my $g = 190 + int(50 * $v2); + my $b = 0 + int(210 * $v1); + return "rgb($r,$g,$b)"; + } + if (defined $type and $type eq "io") { + my $r = 80 + int(60 * $v1); + my $g = $r; + my $b = 190 + int(55 * $v2); + return "rgb($r,$g,$b)"; + } + if (defined $type and $type eq "libgit2") { + my $alpha = sprintf("%.2f", 0.2 + (0.8 * (($v1 + $v2) / 2))); + return ($v3 > 0.5) ? + "rgba(241,80,47,$alpha)" : + "rgba(55,125,205,$alpha)"; + } + + # multi palettes + if (defined $type and $type eq "java") { + # Handle both annotations (_[j], _[i], ...; which are + # accurate), as well as input that lacks any annotations, as + # best as possible. Without annotations, we get a little hacky + # and match on java|org|com, etc. + if ($name =~ m:_\[j\]$:) { # jit annotation + $type = "green"; + } elsif ($name =~ m:_\[i\]$:) { # inline annotation + $type = "aqua"; + } elsif ($name =~ m:^L?(java|javax|jdk|net|org|com|io|sun)/:) { # Java + $type = "green"; + } elsif ($name =~ /:::/) { # Java, typical perf-map-agent method separator + $type = "green"; + } elsif ($name =~ /::/) { # C++ + $type = "yellow"; + } elsif ($name =~ m:_\[k\]$:) { # kernel annotation + $type = "orange"; + } elsif ($name =~ /::/) { # C++ + $type = "yellow"; + } else { # system + $type = "red"; + } + # fall-through to color palettes + } + if (defined $type and $type eq "perl") { + if ($name =~ /::/) { # C++ + $type = "yellow"; + } elsif ($name =~ m:Perl: or $name =~ m:\.pl:) { # Perl + $type = "green"; + } elsif ($name =~ m:_\[k\]$:) { # kernel + $type = "orange"; + } else { # system + $type = "red"; + } + # fall-through to color palettes + } + if (defined $type and $type eq "js") { + # Handle both annotations (_[j], _[i], ...; which are + # accurate), as well as input that lacks any annotations, as + # best as possible. Without annotations, we get a little hacky, + # and match on a "/" with a ".js", etc. + if ($name =~ m:_\[j\]$:) { # jit annotation + if ($name =~ m:/:) { + $type = "green"; # source + } else { + $type = "aqua"; # builtin + } + } elsif ($name =~ /::/) { # C++ + $type = "yellow"; + } elsif ($name =~ m:/.*\.js:) { # JavaScript (match "/" in path) + $type = "green"; + } elsif ($name =~ m/:/) { # JavaScript (match ":" in builtin) + $type = "aqua"; + } elsif ($name =~ m/^ $/) { # Missing symbol + $type = "green"; + } elsif ($name =~ m:_\[k\]:) { # kernel + $type = "orange"; + } else { # system + $type = "red"; + } + # fall-through to color palettes + } + if (defined $type and $type eq "wakeup") { + $type = "aqua"; + # fall-through to color palettes + } + if (defined $type and $type eq "chain") { + if ($name =~ m:_\[w\]:) { # waker + $type = "aqua" + } else { # off-CPU + $type = "blue"; + } + # fall-through to color palettes + } + + # color palettes + if (defined $type and $type eq "red") { + my $r = 200 + int(55 * $v1); + my $x = 50 + int(80 * $v1); + return "rgb($r,$x,$x)"; + } + if (defined $type and $type eq "green") { + my $g = 200 + int(55 * $v1); + my $x = 50 + int(60 * $v1); + return "rgb($x,$g,$x)"; + } + if (defined $type and $type eq "blue") { + my $b = 205 + int(50 * $v1); + my $x = 80 + int(60 * $v1); + return "rgb($x,$x,$b)"; + } + if (defined $type and $type eq "yellow") { + my $x = 175 + int(55 * $v1); + my $b = 50 + int(20 * $v1); + return "rgb($x,$x,$b)"; + } + if (defined $type and $type eq "purple") { + my $x = 190 + int(65 * $v1); + my $g = 80 + int(60 * $v1); + return "rgb($x,$g,$x)"; + } + if (defined $type and $type eq "aqua") { + my $r = 50 + int(60 * $v1); + my $g = 165 + int(55 * $v1); + my $b = 165 + int(55 * $v1); + return "rgb($r,$g,$b)"; + } + if (defined $type and $type eq "orange") { + my $r = 190 + int(65 * $v1); + my $g = 90 + int(65 * $v1); + return "rgb($r,$g,0)"; + } + + return "rgb(0,0,0)"; +} + +sub color_scale { + my ($value, $max) = @_; + my ($r, $g, $b) = (255, 255, 255); + $value = -$value if $negate; + if ($value > 0) { + $g = $b = int(210 * ($max - $value) / $max); + } elsif ($value < 0) { + $r = $g = int(210 * ($max + $value) / $max); + } + return "rgb($r,$g,$b)"; +} + +sub color_map { + my ($colors, $func) = @_; + if (exists $palette_map{$func}) { + return $palette_map{$func}; + } else { + $palette_map{$func} = color($colors, $hash, $func); + return $palette_map{$func}; + } +} + +sub write_palette { + open(FILE, ">$pal_file"); + foreach my $key (sort keys %palette_map) { + print FILE $key."->".$palette_map{$key}."\n"; + } + close(FILE); +} + +sub read_palette { + if (-e $pal_file) { + open(FILE, $pal_file) or die "can't open file $pal_file: $!"; + while ( my $line = ) { + chomp($line); + (my $key, my $value) = split("->",$line); + $palette_map{$key}=$value; + } + close(FILE) + } +} + +my %Node; # Hash of merged frame data +my %Tmp; + +# flow() merges two stacks, storing the merged frames and value data in %Node. +sub flow { + my ($last, $this, $v, $d) = @_; + + my $len_a = @$last - 1; + my $len_b = @$this - 1; + + my $i = 0; + my $len_same; + for (; $i <= $len_a; $i++) { + last if $i > $len_b; + last if $last->[$i] ne $this->[$i]; + } + $len_same = $i; + + for ($i = $len_a; $i >= $len_same; $i--) { + my $k = "$last->[$i];$i"; + # a unique ID is constructed from "func;depth;etime"; + # func-depth isn't unique, it may be repeated later. + $Node{"$k;$v"}->{stime} = delete $Tmp{$k}->{stime}; + if (defined $Tmp{$k}->{delta}) { + $Node{"$k;$v"}->{delta} = delete $Tmp{$k}->{delta}; + } + delete $Tmp{$k}; + } + + for ($i = $len_same; $i <= $len_b; $i++) { + my $k = "$this->[$i];$i"; + $Tmp{$k}->{stime} = $v; + if (defined $d) { + $Tmp{$k}->{delta} += $i == $len_b ? $d : 0; + } + } + + return $this; +} + +# parse input +my @Data; +my @SortedData; +my $last = []; +my $time = 0; +my $delta = undef; +my $ignored = 0; +my $line; +my $maxdelta = 1; + +# reverse if needed +foreach (<>) { + chomp; + $line = $_; + if ($stackreverse) { + # there may be an extra samples column for differentials + # XXX todo: redo these REs as one. It's repeated below. + my($stack, $samples) = (/^(.*)\s+?(\d+(?:\.\d*)?)$/); + my $samples2 = undef; + if ($stack =~ /^(.*)\s+?(\d+(?:\.\d*)?)$/) { + $samples2 = $samples; + ($stack, $samples) = $stack =~ (/^(.*)\s+?(\d+(?:\.\d*)?)$/); + unshift @Data, join(";", reverse split(";", $stack)) . " $samples $samples2"; + } else { + unshift @Data, join(";", reverse split(";", $stack)) . " $samples"; + } + } else { + unshift @Data, $line; + } +} + +if ($flamechart) { + # In flame chart mode, just reverse the data so time moves from left to right. + @SortedData = reverse @Data; +} else { + @SortedData = sort @Data; +} + +# process and merge frames +foreach (@SortedData) { + chomp; + # process: folded_stack count + # eg: func_a;func_b;func_c 31 + my ($stack, $samples) = (/^(.*)\s+?(\d+(?:\.\d*)?)$/); + unless (defined $samples and defined $stack) { + ++$ignored; + next; + } + + # there may be an extra samples column for differentials: + my $samples2 = undef; + if ($stack =~ /^(.*)\s+?(\d+(?:\.\d*)?)$/) { + $samples2 = $samples; + ($stack, $samples) = $stack =~ (/^(.*)\s+?(\d+(?:\.\d*)?)$/); + } + $delta = undef; + if (defined $samples2) { + $delta = $samples2 - $samples; + $maxdelta = abs($delta) if abs($delta) > $maxdelta; + } + + # for chain graphs, annotate waker frames with "_[w]", for later + # coloring. This is a hack, but has a precedent ("_[k]" from perf). + if ($colors eq "chain") { + my @parts = split ";--;", $stack; + my @newparts = (); + $stack = shift @parts; + $stack .= ";--;"; + foreach my $part (@parts) { + $part =~ s/;/_[w];/g; + $part .= "_[w]"; + push @newparts, $part; + } + $stack .= join ";--;", @parts; + } + + # merge frames and populate %Node: + $last = flow($last, [ '', split ";", $stack ], $time, $delta); + + if (defined $samples2) { + $time += $samples2; + } else { + $time += $samples; + } +} +flow($last, [], $time, $delta); + +if ($countname eq "samples") { + # If $countname is used, it's likely that we're not measuring in stack samples + # (e.g. time could be the unit), so don't warn. + warn "Stack count is low ($time). Did something go wrong?\n" if $time < 100; +} + +warn "Ignored $ignored lines with invalid format\n" if $ignored; +unless ($time) { + warn "ERROR: No stack counts found\n"; + my $im = SVG->new(); + # emit an error message SVG, for tools automating flamegraph use + my $imageheight = $fontsize * 5; + $im->header($imagewidth, $imageheight); + $im->stringTTF(undef, int($imagewidth / 2), $fontsize * 2, + "ERROR: No valid input provided to flamegraph.pl."); + print $im->svg; + exit 2; +} +if ($timemax and $timemax < $time) { + warn "Specified --total $timemax is less than actual total $time, so ignored\n" + if $timemax/$time > 0.02; # only warn is significant (e.g., not rounding etc) + undef $timemax; +} +$timemax ||= $time; + +my $widthpertime = ($imagewidth - 2 * $xpad) / $timemax; + +# Treat as a percentage of time if the string ends in a "%". +my $minwidth_time; +if ($minwidth =~ /%$/) { + $minwidth_time = $timemax * $minwidth_f / 100; +} else { + $minwidth_time = $minwidth_f / $widthpertime; +} + +# prune blocks that are too narrow and determine max depth +while (my ($id, $node) = each %Node) { + my ($func, $depth, $etime) = split ";", $id; + my $stime = $node->{stime}; + die "missing start for $id" if not defined $stime; + + if (($etime-$stime) < $minwidth_time) { + delete $Node{$id}; + next; + } + $depthmax = $depth if $depth > $depthmax; +} + +# draw canvas, and embed interactive JavaScript program +my $imageheight = (($depthmax + 1) * $frameheight) + $ypad1 + $ypad2; +$imageheight += $ypad3 if $subtitletext ne ""; +my $titlesize = $fontsize + 5; +my $im = SVG->new(); +my ($black, $vdgrey, $dgrey) = ( + $im->colorAllocate(0, 0, 0), + $im->colorAllocate(160, 160, 160), + $im->colorAllocate(200, 200, 200), + ); +$im->header($imagewidth, $imageheight); +my $backgroundinc = ''; + +if ($bgcolors ne 'none') { + $backgroundinc = < + + + + + +INC +} + +my $inc = < + text { font-family:$fonttype; font-size:${fontsize}px; fill:$black; } + #search, #ignorecase { opacity:0.1; cursor:pointer; } + #search:hover, #search.show, #ignorecase:hover, #ignorecase.show { opacity:1; } + #subtitle { text-anchor:middle; font-color:$vdgrey; } + #title { text-anchor:middle; font-size:${titlesize}px} + #unzoom { cursor:pointer; } + #frames > *:hover { stroke:black; stroke-width:0.5; cursor:pointer; } + .hide { display:none; } + .parent { opacity:0.5; } + + +INC +$im->include($inc); +$im->filledRectangle(0, 0, $imagewidth, $imageheight, 'url(#background)'); +$im->stringTTF("title", int($imagewidth / 2), $fontsize * 2, $titletext) if $titletext ne ""; +$im->stringTTF("subtitle", int($imagewidth / 2), $fontsize * 4, $subtitletext) if $subtitletext ne ""; +$im->stringTTF("details", $xpad, $imageheight - ($ypad2 / 2), " "); +$im->stringTTF("unzoom", $xpad, $fontsize * 2, "Reset Zoom", 'class="hide"'); +$im->stringTTF("search", $imagewidth - $xpad - 100, $fontsize * 2, "Search"); +$im->stringTTF("ignorecase", $imagewidth - $xpad - 16, $fontsize * 2, "ic"); +$im->stringTTF("matched", $imagewidth - $xpad - 100, $imageheight - ($ypad2 / 2), " "); + +if ($palette) { + read_palette(); +} + +# draw frames +$im->group_start({id => "frames"}); +while (my ($id, $node) = each %Node) { + my ($func, $depth, $etime) = split ";", $id; + my $stime = $node->{stime}; + my $delta = $node->{delta}; + + $etime = $timemax if $func eq "" and $depth == 0; + + my $x1 = $xpad + $stime * $widthpertime; + my $x2 = $xpad + $etime * $widthpertime; + my ($y1, $y2); + unless ($inverted) { + $y1 = $imageheight - $ypad2 - ($depth + 1) * $frameheight + $framepad; + $y2 = $imageheight - $ypad2 - $depth * $frameheight; + } else { + $y1 = $ypad1 + $depth * $frameheight; + $y2 = $ypad1 + ($depth + 1) * $frameheight - $framepad; + } + + # Add commas per perlfaq5: + # https://perldoc.perl.org/perlfaq5#How-can-I-output-my-numbers-with-commas-added? + my $samples = sprintf "%.0f", ($etime - $stime) * $factor; + (my $samples_txt = $samples) + =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g; + + my $info; + if ($func eq "" and $depth == 0) { + $info = "all ($samples_txt $countname, 100%)"; + } else { + my $pct = sprintf "%.2f", ((100 * $samples) / ($timemax * $factor)); + my $escaped_func = $func; + # clean up SVG breaking characters: + $escaped_func =~ s/&/&/g; + $escaped_func =~ s//>/g; + $escaped_func =~ s/"/"/g; + $escaped_func =~ s/_\[[kwij]\]$//; # strip any annotation + unless (defined $delta) { + $info = "$escaped_func ($samples_txt $countname, $pct%)"; + } else { + my $d = $negate ? -$delta : $delta; + my $deltapct = sprintf "%.2f", ((100 * $d) / ($timemax * $factor)); + $deltapct = $d > 0 ? "+$deltapct" : $deltapct; + $info = "$escaped_func ($samples_txt $countname, $pct%; $deltapct%)"; + } + } + + my $nameattr = { %{ $nameattr{$func}||{} } }; # shallow clone + $nameattr->{title} ||= $info; + $im->group_start($nameattr); + + my $color; + if ($func eq "--") { + $color = $vdgrey; + } elsif ($func eq "-") { + $color = $dgrey; + } elsif (defined $delta) { + $color = color_scale($delta, $maxdelta); + } elsif ($palette) { + $color = color_map($colors, $func); + } else { + $color = color($colors, $hash, $func); + } + $im->filledRectangle($x1, $y1, $x2, $y2, $color, 'rx="2" ry="2"'); + + my $chars = int( ($x2 - $x1) / ($fontsize * $fontwidth)); + my $text = ""; + if ($chars >= 3) { # room for one char plus two dots + $func =~ s/_\[[kwij]\]$//; # strip any annotation + $text = substr $func, 0, $chars; + substr($text, -2, 2) = ".." if $chars < length $func; + $text =~ s/&/&/g; + $text =~ s//>/g; + } + $im->stringTTF(undef, $x1 + 3, 3 + ($y1 + $y2) / 2, $text); + + $im->group_end($nameattr); +} +$im->group_end(); + +print $im->svg; + +if ($palette) { + write_palette(); +} + +# vim: ts=8 sts=8 sw=8 noexpandtab diff --git a/tests/benchmarks/_script/flamegraph/jmaps b/tests/benchmarks/_script/flamegraph/jmaps new file mode 100755 index 00000000000..f8014f5a82f --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/jmaps @@ -0,0 +1,104 @@ +#!/bin/bash +# +# jmaps - creates java /tmp/perf-PID.map symbol maps for all java processes. +# +# This is a helper script that finds all running "java" processes, then executes +# perf-map-agent on them all, creating symbol map files in /tmp. These map files +# are read by perf_events (aka "perf") when doing system profiles (specifically, +# the "report" and "script" subcommands). +# +# USAGE: jmaps [-u] +# -u # unfoldall: include inlined symbols +# +# My typical workflow is this: +# +# perf record -F 99 -a -g -- sleep 30; jmaps +# perf script > out.stacks +# ./stackcollapse-perf.pl out.stacks | ./flamegraph.pl --color=java --hash > out.stacks.svg +# +# The stackcollapse-perf.pl and flamegraph.pl programs come from: +# https://github.com/brendangregg/FlameGraph +# +# REQUIREMENTS: +# Tune two environment settings below. +# +# 13-Feb-2015 Brendan Gregg Created this. +# 20-Feb-2017 " " Added -u for unfoldall. + +JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-8-oracle} +AGENT_HOME=${AGENT_HOME:-/usr/lib/jvm/perf-map-agent} # from https://github.com/jvm-profiling-tools/perf-map-agent +debug=0 + +if [[ "$USER" != root ]]; then + echo "ERROR: not root user? exiting..." + exit +fi + +if [[ ! -x $JAVA_HOME ]]; then + echo "ERROR: JAVA_HOME not set correctly; edit $0 and fix" + exit +fi + +if [[ ! -x $AGENT_HOME ]]; then + echo "ERROR: AGENT_HOME not set correctly; edit $0 and fix" + exit +fi + +if [[ "$1" == "-u" ]]; then + opts=unfoldall +fi + +# figure out where the agent files are: +AGENT_OUT="" +AGENT_JAR="" +if [[ -e $AGENT_HOME/out/attach-main.jar ]]; then + AGENT_JAR=$AGENT_HOME/out/attach-main.jar +elif [[ -e $AGENT_HOME/attach-main.jar ]]; then + AGENT_JAR=$AGENT_HOME/attach-main.jar +fi +if [[ -e $AGENT_HOME/out/libperfmap.so ]]; then + AGENT_OUT=$AGENT_HOME/out +elif [[ -e $AGENT_HOME/libperfmap.so ]]; then + AGENT_OUT=$AGENT_HOME +fi +if [[ "$AGENT_OUT" == "" || "$AGENT_JAR" == "" ]]; then + echo "ERROR: Missing perf-map-agent files in $AGENT_HOME. Check installation." + exit +fi + +# Fetch map for all "java" processes +echo "Fetching maps for all java processes..." +for pid in $(pgrep -x java); do + mapfile=/tmp/perf-$pid.map + [[ -e $mapfile ]] && rm $mapfile + + cmd="cd $AGENT_OUT; $JAVA_HOME/bin/java -Xms32m -Xmx128m -cp $AGENT_JAR:$JAVA_HOME/lib/tools.jar net.virtualvoid.perf.AttachOnce $pid $opts" + (( debug )) && echo $cmd + + user=$(ps ho user -p $pid) + group=$(ps ho group -p $pid) + if [[ "$user" != root ]]; then + if [[ "$user" == [0-9]* ]]; then + # UID only, likely GID too, run sudo with #UID: + cmd="sudo -u '#'$user -g '#'$group sh -c '$cmd'" + else + cmd="sudo -u $user -g $group sh -c '$cmd'" + fi + fi + + echo "Mapping PID $pid (user $user):" + if (( debug )); then + time eval $cmd + else + eval $cmd + fi + if [[ -e "$mapfile" ]]; then + chown root $mapfile + chmod 666 $mapfile + else + echo "ERROR: $mapfile not created." + fi + + echo "wc(1): $(wc $mapfile)" + echo +done diff --git a/tests/benchmarks/_script/flamegraph/pkgsplit-perf.pl b/tests/benchmarks/_script/flamegraph/pkgsplit-perf.pl new file mode 100755 index 00000000000..3a9902da49f --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/pkgsplit-perf.pl @@ -0,0 +1,86 @@ +#!/usr/bin/perl -w +# +# pkgsplit-perf.pl Split IP samples on package names "/", eg, Java. +# +# This is for the creation of Java package flame graphs. Example steps: +# +# perf record -F 199 -a -- sleep 30; ./jmaps +# perf script | ./pkgsplit-perf.pl | ./flamegraph.pl > out.svg +# +# Note that stack traces are not sampled (no -g), as we split Java package +# names into frames rather than stack frames. +# +# (jmaps is a helper script for automating perf-map-agent: Java symbol dumps.) +# +# The default output of "perf script" varies between kernel versions, so we'll +# need to deal with that here. I could make people use the perf script option +# to pick fields, so our input is static, but A) I prefer the simplicity of +# just saying: run "perf script", and B) the option to choose fields itself +# changed between kernel versions! -f became -F. +# +# Copyright 2017 Netflix, Inc. +# Licensed under the Apache License, Version 2.0 (the "License") +# +# 20-Sep-2016 Brendan Gregg Created this. + +use strict; + +my $include_pname = 1; # include process names in stacks +my $include_pid = 0; # include process ID with process name +my $include_tid = 0; # include process & thread ID with process name + +while (<>) { + # filter comments + next if /^#/; + + # filter idle events + next if /xen_hypercall_sched_op|cpu_idle|native_safe_halt/; + + my ($pid, $tid, $pname); + + # Linux 3.13: + # java 13905 [000] 8048.096572: cpu-clock: 7fd781ac3053 Ljava/util/Arrays$ArrayList;::toArray (/tmp/perf-12149.map) + # java 8301 [050] 13527.392454: cycles: 7fa8a80d9bff Dictionary::find(int, unsigned int, Symbol*, ClassLoaderData*, Handle, Thread*) (/usr/lib/jvm/java-8-oracle-1.8.0.121/jre/lib/amd64/server/libjvm.so) + # java 4567/8603 [023] 13527.389886: cycles: 7fa863349895 Lcom/google/gson/JsonObject;::add (/tmp/perf-4567.map) + # + # Linux 4.8: + # java 30894 [007] 452884.077440: 10101010 cpu-clock: 7f0acc8eff67 Lsun/nio/ch/SocketChannelImpl;::read+0x27 (/tmp/perf-30849.map) + # bash 26858/26858 [006] 5440237.995639: cpu-clock: 433573 [unknown] (/bin/bash) + # + if (/^\s+(\S.+?)\s+(\d+)\/*(\d+)*\s.*?:.*:/) { + # parse process name and pid/tid + if ($3) { + ($pid, $tid) = ($2, $3); + } else { + ($pid, $tid) = ("?", $2); + } + + if ($include_tid) { + $pname = "$1-$pid/$tid"; + } elsif ($include_pid) { + $pname = "$1-$pid"; + } else { + $pname = $1; + } + $pname =~ tr/ /_/; + } else { + # not a match + next; + } + + # parse rest of line + s/^.*?:.*?:\s+//; + s/ \(.*?\)$//; + chomp; + my ($addr, $func) = split(' ', $_, 2); + + # strip Java's leading "L" + $func =~ s/^L//; + + # replace numbers with X + $func =~ s/[0-9]/X/g; + + # colon delimitered + $func =~ s:/:;:g; + print "$pname;$func 1\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/range-perf.pl b/tests/benchmarks/_script/flamegraph/range-perf.pl new file mode 100755 index 00000000000..0fca6decd23 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/range-perf.pl @@ -0,0 +1,137 @@ +#!/usr/bin/perl -w +# +# range-perf Extract a time range from Linux "perf script" output. +# +# USAGE EXAMPLE: +# +# perf record -F 100 -a -- sleep 60 +# perf script | ./perf2range.pl 10 20 # range 10 to 20 seconds only +# perf script | ./perf2range.pl 0 0.5 # first half second only +# +# MAKING A SERIES OF FLAME GRAPHS: +# +# Let's say you had the output of "perf script" in a file, out.stacks01, which +# was for a 180 second profile. The following command creates a series of +# flame graphs for each 10 second interval: +# +# for i in `seq 0 10 170`; do cat out.stacks01 | \ +# ./perf2range.pl $i $((i + 10)) | ./stackcollapse-perf.pl | \ +# grep -v cpu_idle | ./flamegraph.pl --hash --color=java \ +# --title="range $i $((i + 10))" > out.range_$i.svg; echo $i done; done +# +# In that example, I used "--color=java" for the Java palette, and excluded +# the idle CPU task. Customize as needed. +# +# Copyright 2017 Netflix, Inc. +# Licensed under the Apache License, Version 2.0 (the "License") +# +# 21-Feb-2017 Brendan Gregg Created this. + +use strict; +use Getopt::Long; +use POSIX 'floor'; + +sub usage { + die < \$timeraw, + 'timezerosecs' => \$timezerosecs, +) or usage(); + +if (@ARGV < 2 || $ARGV[0] eq "-h" || $ARGV[0] eq "--help") { + usage(); + exit; +} +my $begin = $ARGV[0]; +my $end = $ARGV[1]; + +# +# Parsing +# +# IP only examples: +# +# java 52025 [026] 99161.926202: cycles: +# java 14341 [016] 252732.474759: cycles: 7f36571947c0 nmethod::is_nmethod() const (/... +# java 14514 [022] 28191.353083: cpu-clock: 7f92b4fdb7d4 Ljava_util_List$size$0;::call (/tmp/perf-11936.map) +# swapper 0 [002] 6035557.056977: 10101010 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (/lib/modules/4.9-virtual/build/vmlinux) +# bash 25370 603are 6036.991603: 10101010 cpu-clock: 4b931e [unknown] (/bin/bash) +# bash 25370/25370 6036036.799684: cpu-clock: 4b913b [unknown] (/bin/bash) +# other combinations are possible. +# +# Stack examples (-g): +# +# swapper 0 [021] 28648.467059: cpu-clock: +# ffffffff810013aa xen_hypercall_sched_op ([kernel.kallsyms]) +# ffffffff8101cb2f default_idle ([kernel.kallsyms]) +# ffffffff8101d406 arch_cpu_idle ([kernel.kallsyms]) +# ffffffff810bf475 cpu_startup_entry ([kernel.kallsyms]) +# ffffffff81010228 cpu_bringup_and_idle ([kernel.kallsyms]) +# +# java 14375 [022] 28648.467079: cpu-clock: +# 7f92bdd98965 Ljava/io/OutputStream;::write (/tmp/perf-11936.map) +# 7f8808cae7a8 [unknown] ([unknown]) +# +# swapper 0 [005] 5076.836336: cpu-clock: +# ffffffff81051586 native_safe_halt ([kernel.kallsyms]) +# ffffffff8101db4f default_idle ([kernel.kallsyms]) +# ffffffff8101e466 arch_cpu_idle ([kernel.kallsyms]) +# ffffffff810c2b31 cpu_startup_entry ([kernel.kallsyms]) +# ffffffff810427cd start_secondary ([kernel.kallsyms]) +# +# swapper 0 [002] 6034779.719110: 10101010 cpu-clock: +# 2013aa xen_hypercall_sched_op+0xfe20000a (/lib/modules/4.9-virtual/build/vmlinux) +# a72f0e default_idle+0xfe20001e (/lib/modules/4.9-virtual/build/vmlinux) +# 2392bf arch_cpu_idle+0xfe20000f (/lib/modules/4.9-virtual/build/vmlinux) +# a73333 default_idle_call+0xfe200023 (/lib/modules/4.9-virtual/build/vmlinux) +# 2c91a4 cpu_startup_entry+0xfe2001c4 (/lib/modules/4.9-virtual/build/vmlinux) +# 22b64a cpu_bringup_and_idle+0xfe20002a (/lib/modules/4.9-virtual/build/vmlinux) +# +# bash 25370/25370 6035935.188539: cpu-clock: +# b9218 [unknown] (/bin/bash) +# 2037fe8 [unknown] ([unknown]) +# other combinations are possible. +# +# This regexp matches the event line, and puts time in $1, and the event name +# in $2: +# +my $event_regexp = qr/ +([0-9\.]+): *\S* *(\S+):/; + +my $line; +my $start = 0; +my $ok = 0; +my $time; + +while (1) { + $line = ; + last unless defined $line; + next if $line =~ /^#/; # skip comments + + if ($line =~ $event_regexp) { + my ($ts, $event) = ($1, $2, $3); + $start = $ts if $start == 0; + + if ($timezerosecs) { + $time = $ts - floor($start); + } elsif (!$timeraw) { + $time = $ts - $start; + } else { + $time = $ts; # raw times + } + + $ok = 1 if $time >= $begin; + # assume samples are in time order: + exit if $time > $end; + } + + print $line if $ok; +} diff --git a/tests/benchmarks/_script/flamegraph/record-test.sh b/tests/benchmarks/_script/flamegraph/record-test.sh new file mode 100755 index 00000000000..569ecc966bb --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/record-test.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# +# record-test.sh - Overwrite flame graph test result files. +# +# See test.sh, which checks these resulting files. +# +# Currently only tests stackcollapse-perf.pl. + +set -v -x + +# ToDo: add some form of --inline, and --inline --context tests. These are +# tricky since they use addr2line, whose output will vary based on the test +# system's binaries and symbol tables. +for opt in pid tid kernel jit all addrs; do + for testfile in test/*.txt ; do + echo testing $testfile : $opt + outfile=${testfile#*/} + outfile=test/results/${outfile%.txt}"-collapsed-${opt}.txt" + ./stackcollapse-perf.pl --"${opt}" "${testfile}" 2> /dev/null > $outfile + done +done diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-aix.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-aix.pl new file mode 100755 index 00000000000..8456d56b918 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-aix.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl -ws +# +# stackcollapse-aix Collapse AIX /usr/bin/procstack backtraces +# +# Parse a list of backtraces as generated with the poor man's aix-perf.pl +# profiler +# + +use strict; + +my $process = ""; +my $current = ""; +my $previous_function = ""; + +my %stacks; + +while(<>) { + chomp; + if (m/^\d+:/) { + if(!($current eq "")) { + $current = $process . ";" . $current; + $stacks{$current} += 1; + $current = ""; + } + m/^\d+: ([^ ]*)/; + $process = $1; + $current = ""; + } + elsif(m/^---------- tid# \d+/){ + if(!($current eq "")) { + $current = $process . ";" . $current; + $stacks{$current} += 1; + } + $current = ""; + } + elsif(m/^(0x[0-9abcdef]*) *([^ ]*) ([^ ]*) ([^ ]*)/) { + my $function = $2; + my $alt = $1; + $function=~s/\(.*\)?//; + if($function =~ /^\[.*\]$/) { + $function = $alt; + } + if ($current) { + $current = $function . ";" . $current; + } + else { + $current = $function; + } + } +} + +if(!($current eq "")) { + $current = $process . ";" . $current; + $stacks{$current} += 1; + $current = ""; + $process = ""; +} + +foreach my $k (sort { $a cmp $b } keys %stacks) { + print "$k $stacks{$k}\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-bpftrace.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-bpftrace.pl new file mode 100755 index 00000000000..f458c3e3af1 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-bpftrace.pl @@ -0,0 +1,72 @@ +#!/usr/bin/perl -w +# +# stackcollapse-bpftrace.pl collapse bpftrace samples into single lines. +# +# USAGE ./stackcollapse-bpftrace.pl infile > outfile +# +# Example input: +# +# @[ +# _raw_spin_lock_bh+0 +# tcp_recvmsg+808 +# inet_recvmsg+81 +# sock_recvmsg+67 +# sock_read_iter+144 +# new_sync_read+228 +# __vfs_read+41 +# vfs_read+142 +# sys_read+85 +# do_syscall_64+115 +# entry_SYSCALL_64_after_hwframe+61 +# ]: 3 +# +# Example output: +# +# entry_SYSCALL_64_after_hwframe+61;do_syscall_64+115;sys_read+85;vfs_read+142;__vfs_read+41;new_sync_read+228;sock_read_iter+144;sock_recvmsg+67;inet_recvmsg+81;tcp_recvmsg+808;_raw_spin_lock_bh+0 3 +# +# Copyright 2018 Peter Sanford. All rights reserved. +# +# This program 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 program 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 program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# (http://www.gnu.org/copyleft/gpl.html) +# + +use strict; + +my @stack; +my $in_stack = 0; + +foreach (<>) { + chomp; + if (!$in_stack) { + if (/^@\[$/) { + $in_stack = 1; + } elsif (/^@\[,\s(.*)\]: (\d+)/) { + print $1 . " $2\n"; + } + } else { + if (m/^,?\s?(.*)\]: (\d+)/) { + if (length $1) { + push(@stack, $1); + } + print join(';', reverse(@stack)) . " $2\n"; + $in_stack = 0; + @stack = (); + } else { + $_ =~ s/^\s+//; + push(@stack, $_); + } + } +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-chrome-tracing.py b/tests/benchmarks/_script/flamegraph/stackcollapse-chrome-tracing.py new file mode 100755 index 00000000000..b4869781d90 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-chrome-tracing.py @@ -0,0 +1,144 @@ +#!/usr/bin/python +# +# stackcolllapse-chrome-tracing.py collapse Trace Event Format [1] +# callstack events into single lines. +# +# [1] https://github.com/catapult-project/catapult/wiki/Trace-Event-Format +# +# USAGE: ./stackcollapse-chrome-tracing.py input_json [input_json...] > outfile +# +# Example input: +# +# {"traceEvents":[ +# {"pid":1,"tid":2,"ts":0,"ph":"X","name":"Foo","dur":50}, +# {"pid":1,"tid":2,"ts":10,"ph":"X","name":"Bar","dur":30} +# ]} +# +# Example output: +# +# Foo 20.0 +# Foo;Bar 30.0 +# +# Input may contain many stack trace events from many processes/threads. +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at docs/cddl1.txt or +# http://opensource.org/licenses/CDDL-1.0. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at docs/cddl1.txt. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END +# +# 4-Jan-2018 Marcin Kolny Created this. +import argparse +import json + +stack_identifiers = {} + + +class Event: + def __init__(self, label, timestamp, dur): + self.label = label + self.timestamp = timestamp + self.duration = dur + self.total_duration = dur + + def get_stop_timestamp(self): + return self.timestamp + self.duration + + +def cantor_pairing(a, b): + s = a + b + return s * (s + 1) / 2 + b + + +def get_trace_events(trace_file, events_dict): + json_data = json.load(trace_file) + + for entry in json_data['traceEvents']: + if entry['ph'] == 'X': + cantor_val = cantor_pairing(int(entry['tid']), int(entry['pid'])) + if 'dur' not in entry: + continue + if cantor_val not in events_dict: + events_dict[cantor_val] = [] + events_dict[cantor_val].append(Event(entry['name'], float(entry['ts']), float(entry['dur']))) + + +def load_events(trace_files): + events = {} + + for trace_file in trace_files: + get_trace_events(trace_file, events) + + for key in events: + events[key].sort(key=lambda x: x.timestamp) + + return events + + +def save_stack(stack): + first = True + event = None + identifier = '' + + for event in stack: + if first: + first = False + else: + identifier += ';' + identifier += event.label + + if not event: + return + + if identifier in stack_identifiers: + stack_identifiers[identifier] += event.total_duration + else: + stack_identifiers[identifier] = event.total_duration + + +def load_stack_identifiers(events): + event_stack = [] + + for e in events: + if not event_stack: + event_stack.append(e) + else: + while event_stack and event_stack[-1].get_stop_timestamp() <= e.timestamp: + save_stack(event_stack) + event_stack.pop() + + if event_stack: + event_stack[-1].total_duration -= e.duration + + event_stack.append(e) + + while event_stack: + save_stack(event_stack) + event_stack.pop() + + +parser = argparse.ArgumentParser() +parser.add_argument('input_file', nargs='+', + type=argparse.FileType('r'), + help='Chrome Tracing input files') +args = parser.parse_args() + +all_events = load_events(args.input_file) +for tid_pid in all_events: + load_stack_identifiers(all_events[tid_pid]) + +for identifiers, duration in stack_identifiers.items(): + print(identifiers + ' ' + str(duration)) diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-elfutils.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-elfutils.pl new file mode 100755 index 00000000000..c5e6b17e44b --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-elfutils.pl @@ -0,0 +1,98 @@ +#!/usr/bin/perl -w +# +# stackcollapse-elfutils Collapse elfutils stack (eu-stack) backtraces +# +# Parse a list of elfutils backtraces as generated with the poor man's +# profiler [1]: +# +# for x in $(seq 1 "$nsamples"); do +# eu-stack -p "$pid" "$@" +# sleep "$sleeptime" +# done +# +# [1] http://poormansprofiler.org/ +# +# Copyright 2014 Gabriel Corona. All rights reserved. +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at docs/cddl1.txt or +# http://opensource.org/licenses/CDDL-1.0. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at docs/cddl1.txt. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END + +use strict; +use Getopt::Long; + +my $with_pid = 0; +my $with_tid = 0; + +GetOptions('pid' => \$with_pid, + 'tid' => \$with_tid) +or die < outfile\n + --pid # include PID + --tid # include TID +USAGE_END + +my $pid = ""; +my $tid = ""; +my $current = ""; +my $previous_function = ""; + +my %stacks; + +sub add_current { + if(!($current eq "")) { + my $entry; + if ($with_tid) { + $current = "TID=$tid;$current"; + } + if ($with_pid) { + $current = "PID=$pid;$current"; + } + $stacks{$current} += 1; + $current = ""; + } +} + +while(<>) { + chomp; + if (m/^PID ([0-9]*)/) { + add_current(); + $pid = $1; + } + elsif(m/^TID ([0-9]*)/) { + add_current(); + $tid = $1; + } elsif(m/^#[0-9]* *0x[0-9a-f]* (.*)/) { + if ($current eq "") { + $current = $1; + } else { + $current = "$1;$current"; + } + } elsif(m/^#[0-9]* *0x[0-9a-f]*/) { + if ($current eq "") { + $current = "[unknown]"; + } else { + $current = "[unknown];$current"; + } + } +} +add_current(); + +foreach my $k (sort { $a cmp $b } keys %stacks) { + print "$k $stacks{$k}\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-faulthandler.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-faulthandler.pl new file mode 100755 index 00000000000..4fe74ffa7b8 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-faulthandler.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl -ws +# +# stackcollapse-faulthandler Collapse Python faulthandler backtraces +# +# Parse a list of Python faulthandler backtraces as generated with +# faulthandler.dump_traceback_later. +# +# Copyright 2014 Gabriel Corona. All rights reserved. +# Copyright 2017 Jonathan Kolb. All rights reserved. +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at docs/cddl1.txt or +# http://opensource.org/licenses/CDDL-1.0. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at docs/cddl1.txt. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END + +use strict; + +my $current = ""; + +my %stacks; + +while(<>) { + chomp; + if (m/^Thread/) { + $current="" + } + elsif(m/^ File "([^"]*)", line ([0-9]*) in (.*)/) { + my $function = $1 . ":" . $2 . ":" . $3; + if ($current eq "") { + $current = $function; + } else { + $current = $function . ";" . $current; + } + } elsif(!($current eq "")) { + $stacks{$current} += 1; + $current = ""; + } +} + +if(!($current eq "")) { + $stacks{$current} += 1; + $current = ""; +} + +foreach my $k (sort { $a cmp $b } keys %stacks) { + print "$k $stacks{$k}\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-gdb.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-gdb.pl new file mode 100755 index 00000000000..8e9831b22e5 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-gdb.pl @@ -0,0 +1,72 @@ +#!/usr/bin/perl -ws +# +# stackcollapse-gdb Collapse GDB backtraces +# +# Parse a list of GDB backtraces as generated with the poor man's +# profiler [1]: +# +# for x in $(seq 1 500); do +# gdb -ex "set pagination 0" -ex "thread apply all bt" -batch -p $pid 2> /dev/null +# sleep 0.01 +# done +# +# [1] http://poormansprofiler.org/ +# +# Copyright 2014 Gabriel Corona. All rights reserved. +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at docs/cddl1.txt or +# http://opensource.org/licenses/CDDL-1.0. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at docs/cddl1.txt. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END + +use strict; + +my $current = ""; +my $previous_function = ""; + +my %stacks; + +while(<>) { + chomp; + if (m/^Thread/) { + $current="" + } + elsif(m/^#[0-9]* *([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*)/) { + my $function = $3; + my $alt = $1; + if(not($1 =~ /0x[a-zA-Z0-9]*/)) { + $function = $alt; + } + if ($current eq "") { + $current = $function; + } else { + $current = $function . ";" . $current; + } + } elsif(!($current eq "")) { + $stacks{$current} += 1; + $current = ""; + } +} + +if(!($current eq "")) { + $stacks{$current} += 1; + $current = ""; +} + +foreach my $k (sort { $a cmp $b } keys %stacks) { + print "$k $stacks{$k}\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-go.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-go.pl new file mode 100755 index 00000000000..3b2ce3c552f --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-go.pl @@ -0,0 +1,150 @@ +#!/usr/bin/perl -w +# +# stackcollapse-go.pl collapse golang samples into single lines. +# +# Parses golang smaples generated by "go tool pprof" and outputs stacks as +# single lines, with methods separated by semicolons, and then a space and an +# occurrence count. For use with flamegraph.pl. +# +# USAGE: ./stackcollapse-go.pl infile > outfile +# +# Example Input: +# ... +# Samples: +# samples/count cpu/nanoseconds +# 1 10000000: 1 2 +# 2 10000000: 3 2 +# 1 10000000: 4 2 +# ... +# Locations +# 1: 0x58b265 scanblock :0 s=0 +# 2: 0x599530 GC :0 s=0 +# 3: 0x58a999 flushptrbuf :0 s=0 +# 4: 0x58d6a8 runtime.MSpan_Sweep :0 s=0 +# ... +# Mappings +# ... +# +# Example Output: +# +# GC;flushptrbuf 2 +# GC;runtime.MSpan_Sweep 1 +# GC;scanblock 1 +# +# Input may contain many stacks as generated from go tool pprof: +# +# go tool pprof -seconds=60 -raw -output=a.pprof http://$ADDR/debug/pprof/profile +# +# For format of text profile, See golang/src/internal/pprof/profile/profile.go +# +# Copyright 2017 Sijie Yang (yangsijie@baidu.com). All rights reserved. +# +# This program 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 program 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 program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# (http://www.gnu.org/copyleft/gpl.html) +# +# 16-Jan-2017 Sijie Yang Created this. + +use strict; + +use Getopt::Long; + +# tunables +my $help = 0; + +sub usage { + die < outfile\n +USAGE_END +} + +GetOptions( + 'help' => \$help, +) or usage(); +$help && usage(); + +# internals +my $state = "ignore"; +my %stacks; +my %frames; +my %collapsed; + +sub remember_stack { + my ($stack, $count) = @_; + $stacks{$stack} += $count; +} + +# +# Output stack string in required format. For example, for the following samples, +# format_statck() would return GC;runtime.MSpan_Sweep for stack "4 2" +# +# Locations +# 1: 0x58b265 scanblock :0 s=0 +# 2: 0x599530 GC :0 s=0 +# 3: 0x58a999 flushptrbuf :0 s=0 +# 4: 0x58d6a8 runtime.MSpan_Sweep :0 s=0 +# +sub format_statck { + my ($stack) = @_; + my @loc_list = split(/ /, $stack); + + for (my $i=0; $i<=$#loc_list; $i++) { + my $loc_name = $frames{$loc_list[$i]}; + $loc_list[$i] = $loc_name if ($loc_name); + } + return join(";", reverse(@loc_list)); +} + +foreach (<>) { + next if m/^#/; + chomp; + + if ($state eq "ignore") { + if (/Samples:/) { + $state = "sample"; + next; + } + + } elsif ($state eq "sample") { + if (/^\s*([0-9]+)\s*[0-9]+: ([0-9 ]+)/) { + my $samples = $1; + my $stack = $2; + remember_stack($stack, $samples); + } elsif (/Locations/) { + $state = "location"; + next; + } + + } elsif ($state eq "location") { + if (/^\s*([0-9]*): 0x[0-9a-f]+ (M=[0-9]+ )?([^ ]+) .*/) { + my $loc_id = $1; + my $loc_name = $3; + $frames{$loc_id} = $loc_name; + } elsif (/Mappings/) { + $state = "mapping"; + last; + } + } +} + +foreach my $k (keys %stacks) { + my $stack = format_statck($k); + my $count = $stacks{$k}; + $collapsed{$stack} += $count; +} + +foreach my $k (sort { $a cmp $b } keys %collapsed) { + print "$k $collapsed{$k}\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-ibmjava.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-ibmjava.pl new file mode 100644 index 00000000000..f8ffa8bd4d7 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-ibmjava.pl @@ -0,0 +1,145 @@ +#!/usr/bin/perl -w +# +# stackcollapse-ibmjava.pl collapse jstack samples into single lines. +# +# Parses Java stacks generated by IBM Java with methods separated by semicolons, +# and then a space and an occurrence count. +# +# USAGE: ./stackcollapse-ibmjava.pl infile > outfile +# +# Example input: +# +# NULL +# 1XMTHDINFO Thread Details +# NULL +# NULL +# 3XMTHREADINFO "Default Executor-thread-149164" J9VMThread:0x0000000008132B00, j9thread_t:0x000000001A810B90, java/lang/Thread:0x0000000712BE8E48, state:R, prio=5 +# 3XMJAVALTHREAD (java/lang/Thread getId:0x3493E, isDaemon:true) +# 3XMTHREADINFO1 (native thread ID:0x3158, native priority:0x5, native policy:UNKNOWN, vmstate:R, vm thread flags:0x00000001) +# 3XMCPUTIME CPU usage total: 0.421875000 secs, user: 0.343750000 secs, system: 0.078125000 secs, current category="Application" +# 3XMHEAPALLOC Heap bytes allocated since last GC cycle=0 (0x0) +# 3XMTHREADINFO3 Java callstack: +# 4XESTACKTRACE at java/net/SocketInputStream.socketRead0(Native Method) +# 4XESTACKTRACE at java/net/SocketInputStream.socketRead(SocketInputStream.java:127(Compiled Code)) +# 4XESTACKTRACE at java/net/SocketInputStream.read(SocketInputStream.java:182(Compiled Code)) +# 4XESTACKTRACE at java/net/SocketInputStream.read(SocketInputStream.java:152(Compiled Code)) +# 4XESTACKTRACE at java/io/FilterInputStream.read(FilterInputStream.java:144(Compiled Code)) +# ... +# 4XESTACKTRACE at java/lang/Thread.run(Thread.java:785(Compiled Code)) +# +# Example output: +# +# Default Executor-thread-149164;java/lang/Thread.run;java/net/SocketInputStream/read;java/net/SocketInputStream.socketRead0 1 +# +# +# Copyright 2014 Federico Juinio. All rights reserved. +# +# This program 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 program 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 program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# (http://www.gnu.org/copyleft/gpl.html) +# +# 23-Aug-2023 Federico Juinio created this based from stackcollapse-jstack.pl + +use strict; + +use Getopt::Long; + +# tunables +my $include_tname = 1; # include thread names in stacks +my $include_tid = 0; # include thread IDs in stacks +my $shorten_pkgs = 0; # shorten package names +my $help = 0; + +sub usage { + die < outfile\n + --include-tname + --no-include-tname # include/omit thread names in stacks (default: include) + --include-tid + --no-include-tid # include/omit thread IDs in stacks (default: omit) + --shorten-pkgs + --no-shorten-pkgs # (don't) shorten package names (default: don't shorten) + + eg, + $0 --no-include-tname stacks.txt > collapsed.txt +USAGE_END +} + +GetOptions( + 'include-tname!' => \$include_tname, + 'include-tid!' => \$include_tid, + 'shorten-pkgs!' => \$shorten_pkgs, + 'help' => \$help, +) or usage(); +$help && usage(); + + +# internals +my %collapsed; + +sub remember_stack { + my ($stack, $count) = @_; + $collapsed{$stack} += $count; +} + +my @stack; +my $tname; +my $state = "?"; + +foreach (<>) { + next if m/^#/; + chomp; + + if (m/^3XMTHREADINFO3 Native callstack:/) { + # save stack + if (defined $tname) { unshift @stack, $tname; } + remember_stack(join(";", @stack), 1) if @stack; + undef @stack; + undef $tname; + $state = "?"; + next; + } + + # look for thread header line and parse thread name and state + if (/^3XMTHREADINFO "([^"]*).* state:(.*), /) { + my $name = $1; + if ($include_tname) { + $tname = $name; + } + $state = $2; + # special handling for "Anonymous native threads" + } elsif (/3XMTHREADINFO Anonymous native thread/) { + $tname = "Anonymous native thread"; + # look for thread id + } elsif (/^3XMTHREADINFO1 \(native thread ID:([^ ]*), native priority/) { + if ($include_tname && $include_tid) { + $tname = $tname . "-" . $1 + } + # collect stack frames + } elsif (/^4XESTACKTRACE at ([^\(]*)/) { + my $func = $1; + if ($shorten_pkgs) { + my ($pkgs, $clsFunc) = ( $func =~ m/(.*\.)([^.]+\.[^.]+)$/ ); + $pkgs =~ s/(\w)\w*/$1/g; + $func = $pkgs . $clsFunc; + } + unshift @stack, $func; + + } +} + +foreach my $k (sort { $a cmp $b } keys %collapsed) { + print "$k $collapsed{$k}\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-instruments.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-instruments.pl new file mode 100755 index 00000000000..3cbaa87bc27 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-instruments.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl -w +# +# stackcollapse-instruments.pl +# +# Parses a file containing a call tree as produced by XCode Instruments +# (Edit > Deep Copy) and produces output suitable for flamegraph.pl. +# +# USAGE: ./stackcollapse-instruments.pl infile > outfile + +use strict; + +my @stack = (); + +<>; +foreach (<>) { + chomp; + /\d+\.\d+ (?:min|s|ms)\s+\d+\.\d+%\s+(\d+(?:\.\d+)?) (min|s|ms)\t \t(\s*)(.+)/ or die; + my $func = $4; + my $depth = length ($3); + $stack [$depth] = $4; + foreach my $i (0 .. $depth - 1) { + print $stack [$i]; + print ";"; + } + + my $time = 0 + $1; + if ($2 eq "min") { + $time *= 60*1000; + } elsif ($2 eq "s") { + $time *= 1000; + } + + printf("%s %.0f\n", $func, $time); +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-java-exceptions.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-java-exceptions.pl new file mode 100755 index 00000000000..19badbca6cb --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-java-exceptions.pl @@ -0,0 +1,72 @@ +#!/usr/bin/perl -w +# +# stackcolllapse-java-exceptions.pl collapse java exceptions (found in logs) into single lines. +# +# Parses Java error stacks found in a log file and outputs them as +# single lines, with methods separated by semicolons, and then a space and an +# occurrence count. Inspired by stackcollapse-jstack.pl except that it does +# not act as a performance profiler. +# +# It can be useful if a Java process dumps a lot of different stacks in its logs +# and you want to quickly identify the biggest culprits. +# +# USAGE: ./stackcollapse-java-exceptions.pl infile > outfile +# +# Copyright 2018 Paul de Verdiere. All rights reserved. + +use strict; +use Getopt::Long; + +# tunables +my $shorten_pkgs = 0; # shorten package names +my $no_pkgs = 0; # really shorten package names!! +my $help = 0; + +sub usage { + die < outfile\n + --shorten-pkgs : shorten package names + --no-pkgs : suppress package names (makes SVG much more readable) + +USAGE_END +} + +GetOptions( + 'shorten-pkgs!' => \$shorten_pkgs, + 'no-pkgs!' => \$no_pkgs, + 'help' => \$help, +) or usage(); +$help && usage(); + +my %collapsed; + +sub remember_stack { + my ($stack, $count) = @_; + $collapsed{$stack} += $count; +} + +my @stack; + +foreach (<>) { + chomp; + + if (/^\s*at ([^\(]*)/) { + my $func = $1; + if ($shorten_pkgs || $no_pkgs) { + my ($pkgs, $clsFunc) = ( $func =~ m/(.*\.)([^.]+\.[^.]+)$/ ); + $pkgs =~ s/(\w)\w*/$1/g; + $func = $no_pkgs ? $clsFunc: $pkgs . $clsFunc; + } + unshift @stack, $func; + } elsif (@stack ) { + next if m/.*waiting on .*/; + remember_stack(join(";", @stack), 1) if @stack; + undef @stack; + } +} + +remember_stack(join(";", @stack), 1) if @stack; + +foreach my $k (sort { $a cmp $b } keys %collapsed) { + print "$k $collapsed{$k}\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-jstack.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-jstack.pl new file mode 100755 index 00000000000..da5740b6ee6 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-jstack.pl @@ -0,0 +1,176 @@ +#!/usr/bin/perl -w +# +# stackcollapse-jstack.pl collapse jstack samples into single lines. +# +# Parses Java stacks generated by jstack(1) and outputs RUNNABLE stacks as +# single lines, with methods separated by semicolons, and then a space and an +# occurrence count. This also filters some other "RUNNABLE" states that we +# know are probably not running, such as epollWait. For use with flamegraph.pl. +# +# You want this to process the output of at least 100 jstack(1)s. ie, run it +# 100 times with a sleep interval, and append to a file. This is really a poor +# man's Java profiler, due to the overheads of jstack(1), and how it isn't +# capturing stacks asynchronously. For a better profiler, see: +# http://www.brendangregg.com/blog/2014-06-12/java-flame-graphs.html +# +# USAGE: ./stackcollapse-jstack.pl infile > outfile +# +# Example input: +# +# "MyProg" #273 daemon prio=9 os_prio=0 tid=0x00007f273c038800 nid=0xe3c runnable [0x00007f28a30f2000] +# java.lang.Thread.State: RUNNABLE +# at java.net.SocketInputStream.socketRead0(Native Method) +# at java.net.SocketInputStream.read(SocketInputStream.java:121) +# ... +# at java.lang.Thread.run(Thread.java:744) +# +# Example output: +# +# MyProg;java.lang.Thread.run;java.net.SocketInputStream.read;java.net.SocketInputStream.socketRead0 1 +# +# Input may be created and processed using: +# +# i=0; while (( i++ < 200 )); do jstack PID >> out.jstacks; sleep 10; done +# cat out.jstacks | ./stackcollapse-jstack.pl > out.stacks-folded +# +# WARNING: jstack(1) incurs overheads. Test before use, or use a real profiler. +# +# Copyright 2014 Brendan Gregg. All rights reserved. +# +# This program 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 program 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 program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# (http://www.gnu.org/copyleft/gpl.html) +# +# 14-Sep-2014 Brendan Gregg Created this. + +use strict; + +use Getopt::Long; + +# tunables +my $include_tname = 1; # include thread names in stacks +my $include_tid = 0; # include thread IDs in stacks +my $shorten_pkgs = 0; # shorten package names +my $help = 0; + +sub usage { + die < outfile\n + --include-tname + --no-include-tname # include/omit thread names in stacks (default: include) + --include-tid + --no-include-tid # include/omit thread IDs in stacks (default: omit) + --shorten-pkgs + --no-shorten-pkgs # (don't) shorten package names (default: don't shorten) + + eg, + $0 --no-include-tname stacks.txt > collapsed.txt +USAGE_END +} + +GetOptions( + 'include-tname!' => \$include_tname, + 'include-tid!' => \$include_tid, + 'shorten-pkgs!' => \$shorten_pkgs, + 'help' => \$help, +) or usage(); +$help && usage(); + + +# internals +my %collapsed; + +sub remember_stack { + my ($stack, $count) = @_; + $collapsed{$stack} += $count; +} + +my @stack; +my $tname; +my $state = "?"; + +foreach (<>) { + next if m/^#/; + chomp; + + if (m/^$/) { + # only include RUNNABLE states + goto clear if $state ne "RUNNABLE"; + + # save stack + if (defined $tname) { unshift @stack, $tname; } + remember_stack(join(";", @stack), 1) if @stack; +clear: + undef @stack; + undef $tname; + $state = "?"; + next; + } + + # + # While parsing jstack output, the $state variable may be altered from + # RUNNABLE to other states. This causes the stacks to be filtered later, + # since only RUNNABLE stacks are included. + # + + if (/^"([^"]*)/) { + my $name = $1; + + if ($include_tname) { + $tname = $name; + unless ($include_tid) { + $tname =~ s/-\d+$//; + } + } + + # set state for various background threads + $state = "BACKGROUND" if $name =~ /C. CompilerThread/; + $state = "BACKGROUND" if $name =~ /Signal Dispatcher/; + $state = "BACKGROUND" if $name =~ /Service Thread/; + $state = "BACKGROUND" if $name =~ /Attach Listener/; + + } elsif (/java.lang.Thread.State: (\S+)/) { + $state = $1 if $state eq "?"; + } elsif (/^\s*at ([^\(]*)/) { + my $func = $1; + if ($shorten_pkgs) { + my ($pkgs, $clsFunc) = ( $func =~ m/(.*\.)([^.]+\.[^.]+)$/ ); + $pkgs =~ s/(\w)\w*/$1/g; + $func = $pkgs . $clsFunc; + } + unshift @stack, $func; + + # fix state for epollWait + $state = "WAITING" if $func =~ /epollWait/; + $state = "WAITING" if $func =~ /EPoll\.wait/; + + + # fix state for various networking functions + $state = "NETWORK" if $func =~ /socketAccept$/; + $state = "NETWORK" if $func =~ /Socket.*accept0$/; + $state = "NETWORK" if $func =~ /socketRead0$/; + + } elsif (/^\s*-/ or /^2\d\d\d-/ or /^Full thread dump/ or + /^JNI global references:/) { + # skip these info lines + next; + } else { + warn "Unrecognized line: $_"; + } +} + +foreach my $k (sort { $a cmp $b } keys %collapsed) { + print "$k $collapsed{$k}\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-ljp.awk b/tests/benchmarks/_script/flamegraph/stackcollapse-ljp.awk new file mode 100755 index 00000000000..59aaae3d73b --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-ljp.awk @@ -0,0 +1,74 @@ +#!/usr/bin/awk -f +# +# stackcollapse-ljp.awk collapse lightweight java profile reports +# into single lines stacks. +# +# Parses a list of multiline stacks generated by: +# +# https://code.google.com/p/lightweight-java-profiler +# +# and outputs a semicolon separated stack followed by a space and a count. +# +# USAGE: ./stackcollapse-ljp.pl infile > outfile +# +# Example input: +# +# 42 3 my_func_b(prog.java:455) +# my_func_a(prog.java:123) +# java.lang.Thread.run(Thread.java:744) +# [...] +# +# Example output: +# +# java.lang.Thread.run;my_func_a;my_func_b 42 +# +# The unused number is the number of frames in each stack. +# +# Copyright 2014 Brendan Gregg. All rights reserved. +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at docs/cddl1.txt or +# http://opensource.org/licenses/CDDL-1.0. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at docs/cddl1.txt. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END +# +# 12-Jun-2014 Brendan Gregg Created this. + +$1 == "Total" { + # We're done. Print last stack and exit. + print stack, count + exit +} + +{ + # Strip file location. Comment this out to keep. + gsub(/\(.*\)/, "") +} + +NF == 3 { + # New stack begins. Print previous buffered stack. + if (count) + print stack, count + + # Begin a new stack. + count = $1 + stack = $3 +} + +NF == 1 { + # Build stack. + stack = $1 ";" stack +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-perf-sched.awk b/tests/benchmarks/_script/flamegraph/stackcollapse-perf-sched.awk new file mode 100755 index 00000000000..e1a122d459e --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-perf-sched.awk @@ -0,0 +1,228 @@ +#!/usr/bin/awk -f + +# +# This program generates collapsed off-cpu stacks fit for use by flamegraph.pl +# from scheduler data collected via perf_events. +# +# Outputs the cumulative time off cpu in us for each distinct stack observed. +# +# Some awk variables further control behavior: +# +# record_tid If truthy, causes all stack traces to include the +# command and LWP id. +# +# record_wake_stack If truthy, stacks include the frames from the wakeup +# event in addition to the sleep event. +# See http://www.brendangregg.com/FlameGraphs/offcpuflamegraphs.html#Wakeup +# +# recurse If truthy, attempt to recursively identify and +# visualize the full wakeup stack chain. +# See http://www.brendangregg.com/FlameGraphs/offcpuflamegraphs.html#ChainGraph +# +# Note that this is only an approximation, as only the +# last sleep event is recorded (e.g. if a thread slept +# multiple times before waking another thread, only the +# last sleep event is used). Implies record_wake_stack=1 +# +# To set any of these variables from the command line, run via: +# +# stackcollapse-perf-sched.awk -v recurse=1 +# +# == Important warning == +# +# WARNING: tracing all scheduler events is very high overhead in perf. Even +# more alarmingly, there appear to be bugs in perf that prevent it from reliably +# getting consistent traces (even with large trace buffers), causing it to +# produce empty perf.data files with error messages of the form: +# +# 0x952790 [0x736d]: failed to process type: 3410 +# +# This failure is not determinisitic, so re-executing perf record will +# eventually succeed. +# +# == Usage == +# +# First, record data via perf_events: +# +# sudo perf record -g -e 'sched:sched_switch' \ +# -e 'sched:sched_stat_sleep' -e 'sched:sched_stat_blocked' \ +# -p -o perf.data -- sleep 1 +# +# Then post process with this script: +# +# sudo perf script -f time,comm,pid,tid,event,ip,sym,dso,trace -i perf.data | \ +# stackcollapse-perf-sched.awk -v recurse=1 | \ +# flamegraph.pl --color=io --countname=us >out.svg +# + +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at docs/cddl1.txt or +# http://opensource.org/licenses/CDDL-1.0. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at docs/cddl1.txt. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END +# + +# +# Copyright (c) 2015 by MemSQL. All rights reserved. +# + +# +# Match a perf captured variable, returning just the contents. For example, for +# the following line, get_perf_captured_variable("pid") would return "27235": +# +# swapper 0 [006] 708189.626415: sched:sched_stat_sleep: comm=memsqld pid=27235 delay=100078421 [ns +# +function get_perf_captured_variable(variable) +{ + match($0, variable "=[^[:space:]]+") + return substr($0, RSTART + length(variable) + 1, + RLENGTH - length(variable) - 1) +} + +# +# The timestamp is the first field that ends in a colon, e.g.: +# +# swapper 0 [006] 708189.626415: sched:sched_stat_sleep: comm=memsqld pid=27235 delay=100078421 [ns +# +# or +# +# swapper 0/0 708189.626415: sched:sched_stat_sleep: comm=memsqld pid=27235 delay=100078421 [ns] +# +function get_perf_timestamp() +{ + match($0, " [^ :]+:") + return substr($0, RSTART + 1, RLENGTH - 2) +} + +!/^#/ && /sched:sched_switch/ { + switchcommand = get_perf_captured_variable("comm") + + switchpid = get_perf_captured_variable("prev_pid") + + switchtime=get_perf_timestamp() + + switchstack="" +} + +# +# Strip the function name from a stack entry +# +# Stack entry is expected to be formatted like: +# c60849 MyClass::Foo(unsigned long) (/home/areece/a.out) +# +function get_function_name() +{ + # We start from 2 since we don't need the hex offset. + # We stop at NF - 1 since we don't need the library path. + funcname = $2 + for (i = 3; i <= NF - 1; i++) { + funcname = funcname " " $i + } + return funcname +} + +(switchpid != 0 && /^\s/) { + if (switchstack == "") { + switchstack = get_function_name() + } else { + switchstack = get_function_name() ";" switchstack + } +} + +(switchpid != 0 && /^$/) { + switch_stacks[switchpid] = switchstack + delete last_switch_stacks[switchpid] + switch_time[switchpid] = switchtime + + switchpid=0 + switchcommand="" + switchstack="" +} + +!/^#/ && (/sched:sched_stat_sleep/ || /sched:sched_stat_blocked/) { + wakecommand=$1 + wakepid=$2 + + waketime=get_perf_timestamp() + + stat_next_command = get_perf_captured_variable("comm") + + stat_next_pid = get_perf_captured_variable("pid") + + stat_delay_ns = int(get_perf_captured_variable("delay")) + + wakestack="" +} + +(stat_next_pid != 0 && /^\s/) { + if (wakestack == "") { + wakestack = get_function_name() + } else { + # We build the wakestack in reverse order. + wakestack = wakestack ";" get_function_name() + } +} + +(stat_next_pid != 0 && /^$/) { + # + # For some reason, perf appears to output duplicate + # sched:sched_stat_sleep and sched:sched_stat_blocked events. We only + # handle the first event. + # + if (stat_next_pid in switch_stacks) { + last_wake_time[stat_next_pid] = waketime + + stack = switch_stacks[stat_next_pid] + if (recurse || record_wake_stack) { + stack = stack ";" wakestack + if (record_tid) { + stack = stack ";" wakecommand "-" wakepid + } else { + stack = stack ";" wakecommand + } + } + + if (recurse) { + if (last_wake_time[wakepid] > last_switch_time[stat_next_pid]) { + stack = stack ";-;" last_switch_stacks[wakepid] + } + last_switch_stacks[stat_next_pid] = stack + } + + delete switch_stacks[stat_next_pid] + + if (record_tid) { + stack_times[stat_next_command "-" stat_next_pid ";" stack] += stat_delay_ns + } else { + stack_times[stat_next_command ";" stack] += stat_delay_ns + } + } + + wakecommand="" + wakepid=0 + stat_next_pid=0 + stat_next_command="" + stat_delay_ms=0 +} + +END { + for (stack in stack_times) { + if (int(stack_times[stack] / 1000) > 0) { + print stack, int(stack_times[stack] / 1000) + } + } +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-perf.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-perf.pl new file mode 100755 index 00000000000..3ff39bfb87f --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-perf.pl @@ -0,0 +1,435 @@ +#!/usr/bin/perl -w +# +# stackcollapse-perf.pl collapse perf samples into single lines. +# +# Parses a list of multiline stacks generated by "perf script", and +# outputs a semicolon separated stack followed by a space and a count. +# If memory addresses (+0xd) are present, they are stripped, and resulting +# identical stacks are colased with their counts summed. +# +# USAGE: ./stackcollapse-perf.pl [options] infile > outfile +# +# Run "./stackcollapse-perf.pl -h" to list options. +# +# Example input: +# +# swapper 0 [000] 158665.570607: cpu-clock: +# ffffffff8103ce3b native_safe_halt ([kernel.kallsyms]) +# ffffffff8101c6a3 default_idle ([kernel.kallsyms]) +# ffffffff81013236 cpu_idle ([kernel.kallsyms]) +# ffffffff815bf03e rest_init ([kernel.kallsyms]) +# ffffffff81aebbfe start_kernel ([kernel.kallsyms].init.text) +# [...] +# +# Example output: +# +# swapper;start_kernel;rest_init;cpu_idle;default_idle;native_safe_halt 1 +# +# Input may be created and processed using: +# +# perf record -a -g -F 997 sleep 60 +# perf script | ./stackcollapse-perf.pl > out.stacks-folded +# +# The output of "perf script" should include stack traces. If these are missing +# for you, try manually selecting the perf script output; eg: +# +# perf script -f comm,pid,tid,cpu,time,event,ip,sym,dso,trace | ... +# +# This is also required for the --pid or --tid options, so that the output has +# both the PID and TID. +# +# Copyright 2012 Joyent, Inc. All rights reserved. +# Copyright 2012 Brendan Gregg. All rights reserved. +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at docs/cddl1.txt or +# http://opensource.org/licenses/CDDL-1.0. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at docs/cddl1.txt. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END +# +# 02-Mar-2012 Brendan Gregg Created this. +# 02-Jul-2014 " " Added process name to stacks. + +use strict; +use Getopt::Long; + +my %collapsed; + +sub remember_stack { + my ($stack, $count) = @_; + $collapsed{$stack} += $count; +} +my $annotate_kernel = 0; # put an annotation on kernel function +my $annotate_jit = 0; # put an annotation on jit symbols +my $annotate_all = 0; # enale all annotations +my $include_pname = 1; # include process names in stacks +my $include_pid = 0; # include process ID with process name +my $include_tid = 0; # include process & thread ID with process name +my $include_addrs = 0; # include raw address where a symbol can't be found +my $tidy_java = 1; # condense Java signatures +my $tidy_generic = 1; # clean up function names a little +my $target_pname; # target process name from perf invocation +my $event_filter = ""; # event type filter, defaults to first encountered event +my $event_defaulted = 0; # whether we defaulted to an event (none provided) +my $event_warning = 0; # if we printed a warning for the event + +my $show_inline = 0; +my $show_context = 0; + +my $srcline_in_input = 0; # if there are extra lines with source location (perf script -F+srcline) +GetOptions('inline' => \$show_inline, + 'context' => \$show_context, + 'srcline' => \$srcline_in_input, + 'pid' => \$include_pid, + 'kernel' => \$annotate_kernel, + 'jit' => \$annotate_jit, + 'all' => \$annotate_all, + 'tid' => \$include_tid, + 'addrs' => \$include_addrs, + 'event-filter=s' => \$event_filter) +or die < outfile\n + --pid # include PID with process names [1] + --tid # include TID and PID with process names [1] + --inline # un-inline using addr2line + --all # all annotations (--kernel --jit) + --kernel # annotate kernel functions with a _[k] + --jit # annotate jit functions with a _[j] + --context # adds source context to --inline + --srcline # parses output of 'perf script -F+srcline' and adds source context + --addrs # include raw addresses where symbols can't be found + --event-filter=EVENT # event name filter\n +[1] perf script must emit both PID and TIDs for these to work; eg, Linux < 4.1: + perf script -f comm,pid,tid,cpu,time,event,ip,sym,dso,trace + for Linux >= 4.1: + perf script -F comm,pid,tid,cpu,time,event,ip,sym,dso,trace + If you save this output add --header on Linux >= 3.14 to include perf info. +USAGE_END + +if ($annotate_all) { + $annotate_kernel = $annotate_jit = 1; +} + +my %inlineCache; + +my %nmCache; + +sub inlineCacheAdd { + my ($pc, $mod, $result) = @_; + if (defined($inlineCache{$pc})) { + $inlineCache{$pc}{$mod} = $result; + } else { + $inlineCache{$pc} = {$mod => $result}; + } +} + +# for the --inline option +sub inline { + my ($pc, $rawfunc, $mod) = @_; + + return $inlineCache{$pc}{$mod} if defined($inlineCache{$pc}{$mod}); + + # capture addr2line output + my $a2l_output = `addr2line -a $pc -e $mod -i -f -s -C`; + + # remove first line + $a2l_output =~ s/^(.*\n){1}//; + + if ($a2l_output =~ /\?\?\n\?\?:0/) { + # if addr2line fails and rawfunc is func+offset, then fall back to it + if ($rawfunc =~ /^(.+)\+0x([0-9a-f]+)$/) { + my $func = $1; + my $addr = hex $2; + + $nmCache{$mod}=`nm $mod` unless defined $nmCache{$mod}; + + if ($nmCache{$mod} =~ /^([0-9a-f]+) . \Q$func\E$/m) { + my $base = hex $1; + my $newPc = sprintf "0x%x", $base+$addr; + my $result = inline($newPc, '', $mod); + inlineCacheAdd($pc, $mod, $result); + return $result; + } + } + } + + my @fullfunc; + my $one_item = ""; + for (split /^/, $a2l_output) { + chomp $_; + + # remove discriminator info if exists + $_ =~ s/ \(discriminator \S+\)//; + + if ($one_item eq "") { + $one_item = $_; + } else { + if ($show_context == 1) { + unshift @fullfunc, $one_item . ":$_"; + } else { + unshift @fullfunc, $one_item; + } + $one_item = ""; + } + } + + my $result = join ";" , @fullfunc; + + inlineCacheAdd($pc, $mod, $result); + + return $result; +} + +my @stack; +my $pname; +my $m_pid; +my $m_tid; +my $m_period; + +# +# Main loop +# +while (defined($_ = <>)) { + + # find the name of the process launched by perf, by stepping backwards + # over the args to find the first non-option (no dash): + if (/^# cmdline/) { + my @args = split ' ', $_; + foreach my $arg (reverse @args) { + if ($arg !~ /^-/) { + $target_pname = $arg; + $target_pname =~ s:.*/::; # strip pathname + last; + } + } + } + + # skip remaining comments + next if m/^#/; + chomp; + + # end of stack. save cached data. + if (m/^$/) { + # ignore filtered samples + next if not $pname; + + if ($include_pname) { + if (defined $pname) { + unshift @stack, $pname; + } else { + unshift @stack, ""; + } + } + remember_stack(join(";", @stack), $m_period) if @stack; + undef @stack; + undef $pname; + next; + } + + # + # event record start + # + if (/^(\S.+?)\s+(\d+)\/*(\d+)*\s+/) { + # default "perf script" output has TID but not PID + # eg, "java 25607 4794564.109216: 1 cycles:" + # eg, "java 12688 [002] 6544038.708352: 235 cpu-clock:" + # eg, "V8 WorkerThread 25607 4794564.109216: 104345 cycles:" + # eg, "java 24636/25607 [000] 4794564.109216: 1 cycles:" + # eg, "java 12688/12764 6544038.708352: 10309278 cpu-clock:" + # eg, "V8 WorkerThread 24636/25607 [000] 94564.109216: 100 cycles:" + # other combinations possible + my ($comm, $pid, $tid, $period) = ($1, $2, $3, ""); + if (not $tid) { + $tid = $pid; + $pid = "?"; + } + + if (/:\s*(\d+)*\s+(\S+):\s*$/) { + $period = $1; + my $event = $2; + + if ($event_filter eq "") { + # By default only show events of the first encountered + # event type. Merging together different types, such as + # instructions and cycles, produces misleading results. + $event_filter = $event; + $event_defaulted = 1; + } elsif ($event ne $event_filter) { + if ($event_defaulted and $event_warning == 0) { + # only print this warning if necessary: + # when we defaulted and there was + # multiple event types. + print STDERR "Filtering for events of type: $event\n"; + $event_warning = 1; + } + next; + } + } + + if (not $period) { + $period = 1 + } + ($m_pid, $m_tid, $m_period) = ($pid, $tid, $period); + + if ($include_tid) { + $pname = "$comm-$m_pid/$m_tid"; + } elsif ($include_pid) { + $pname = "$comm-$m_pid"; + } else { + $pname = "$comm"; + } + $pname =~ tr/ /_/; + + # + # stack line + # + } elsif (/^\s*(\w+)\s*(.+) \((.*)\)/) { + # ignore filtered samples + next if not $pname; + + my ($pc, $rawfunc, $mod) = ($1, $2, $3); + + if ($show_inline == 1 && $mod !~ m/(perf-\d+.map|kernel\.|\[[^\]]+\])/) { + my $inlineRes = inline($pc, $rawfunc, $mod); + # - empty result this happens e.g., when $mod does not exist or is a path to a compressed kernel module + # if this happens, the user will see error message from addr2line written to stderr + # - if addr2line results in "??" , then it's much more sane to fall back than produce a '??' in graph + if($inlineRes ne "" and $inlineRes ne "??" and $inlineRes ne "??:??:0" ) { + unshift @stack, $inlineRes; + next; + } + } + + # Linux 4.8 included symbol offsets in perf script output by default, eg: + # 7fffb84c9afc cpu_startup_entry+0x800047c022ec ([kernel.kallsyms]) + # strip these off: + $rawfunc =~ s/\+0x[\da-f]+$//; + + next if $rawfunc =~ /^\(/; # skip process names + + my $is_unknown=0; + my @inline; + for (split /\->/, $rawfunc) { + my $func = $_; + + if ($func eq "[unknown]") { + if ($mod ne "[unknown]") { # use module name instead, if known + $func = $mod; + $func =~ s/.*\///; + } else { + $func = "unknown"; + $is_unknown=1; + } + + if ($include_addrs) { + $func = "\[$func \<$pc\>\]"; + } else { + $func = "\[$func\]"; + } + } + + if ($tidy_generic) { + $func =~ s/;/:/g; + if ($func !~ m/\.\(.*\)\./) { + # This doesn't look like a Go method name (such as + # "net/http.(*Client).Do"), so everything after the first open + # paren (that is not part of an "(anonymous namespace)") is + # just noise. + $func =~ s/\((?!anonymous namespace\)).*//; + } + # now tidy this horrible thing: + # 13a80b608e0a RegExp:[&<>\"\'] (/tmp/perf-7539.map) + $func =~ tr/"\'//d; + # fall through to $tidy_java + } + + if ($tidy_java and $pname =~ m/^java/) { + # along with $tidy_generic, converts the following: + # Lorg/mozilla/javascript/ContextFactory;.call(Lorg/mozilla/javascript/ContextAction;)Ljava/lang/Object; + # Lorg/mozilla/javascript/ContextFactory;.call(Lorg/mozilla/javascript/C + # Lorg/mozilla/javascript/MemberBox;.(Ljava/lang/reflect/Method;)V + # into: + # org/mozilla/javascript/ContextFactory:.call + # org/mozilla/javascript/ContextFactory:.call + # org/mozilla/javascript/MemberBox:.init + $func =~ s/^L// if $func =~ m:/:; + } + + # + # Annotations + # + # detect inlined from the @inline array + # detect kernel from the module name; eg, frames to parse include: + # ffffffff8103ce3b native_safe_halt ([kernel.kallsyms]) + # 8c3453 tcp_sendmsg (/lib/modules/4.3.0-rc1-virtual/build/vmlinux) + # 7d8 ipv4_conntrack_local+0x7f8f80b8 ([nf_conntrack_ipv4]) + # detect jit from the module name; eg: + # 7f722d142778 Ljava/io/PrintStream;::print (/tmp/perf-19982.map) + if (scalar(@inline) > 0) { + $func .= "_[i]" unless $func =~ m/\_\[i\]/; # inlined + } elsif ($annotate_kernel == 1 && $mod =~ m/(^\[|vmlinux$)/ && $mod !~ /unknown/) { + $func .= "_[k]"; # kernel + } elsif ($annotate_jit == 1 && $mod =~ m:/tmp/perf-\d+\.map:) { + $func .= "_[j]" unless $func =~ m/\_\[j\]/; # jitted + } + + # + # Source lines + # + # + # Sample outputs: + # | a.out 35081 252436.005167: 667783 cycles: + # | 408ebb some_method_name+0x8b (/full/path/to/a.out) + # | uniform_int_dist.h:300 + # | 4069f5 main+0x935 (/full/path/to/a.out) + # | file.cpp:137 + # | 7f6d2148eb25 __libc_start_main+0xd5 (/lib64/libc-2.33.so) + # | libc-2.33.so[27b25] + # + # | a.out 35081 252435.738165: 306459 cycles: + # | 7f6d213c2750 [unknown] (/usr/lib64/libkmod.so.2.3.6) + # | libkmod.so.2.3.6[6750] + # + # | a.out 35081 252435.738373: 315813 cycles: + # | 7f6d215ca51b __strlen_avx2+0x4b (/lib64/libc-2.33.so) + # | libc-2.33.so[16351b] + # | 7ffc71ee9580 [unknown] ([unknown]) + # | + # + # | a.out 35081 252435.718940: 247984 cycles: + # | ffffffff814f9302 up_write+0x32 ([kernel.kallsyms]) + # | [kernel.kallsyms][ffffffff814f9302] + if($srcline_in_input and not $is_unknown){ + $_ = <>; + chomp; + s/\[.*?\]//g; + s/^\s*//g; + s/\s*$//g; + $func.=':'.$_ unless $_ eq ""; + } + + push @inline, $func; + } + + unshift @stack, @inline; + } else { + warn "Unrecognized line: $_"; + } +} + +foreach my $k (sort { $a cmp $b } keys %collapsed) { + print "$k $collapsed{$k}\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-pmc.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-pmc.pl new file mode 100755 index 00000000000..5bd7c2dada4 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-pmc.pl @@ -0,0 +1,74 @@ +#!/usr/bin/env perl +# +# Copyright (c) 2014 Ed Maste. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# stackcollapse-pmc.pl collapse hwpmc samples into single lines. +# +# Parses a list of multiline stacks generated by "hwpmc -G", and outputs a +# semicolon-separated stack followed by a space and a count. +# +# Usage: +# pmcstat -S unhalted-cycles -O pmc.out +# pmcstat -R pmc.out -z16 -G pmc.graph +# stackcollapse-pmc.pl pmc.graph > pmc.stack +# +# Example input: +# +# 03.07% [17] witness_unlock @ /boot/kernel/kernel +# 70.59% [12] __mtx_unlock_flags +# 16.67% [2] selfdfree +# 100.0% [2] sys_poll +# 100.0% [2] amd64_syscall +# 08.33% [1] pmap_ts_referenced +# 100.0% [1] vm_pageout +# 100.0% [1] fork_exit +# ... +# +# Example output: +# +# amd64_syscall;sys_poll;selfdfree;__mtx_unlock_flags;witness_unlock 2 +# amd64_syscall;sys_poll;pmap_ts_referenced;__mtx_unlock_flagsgeout;fork_exit 1 +# ... + +use warnings; +use strict; + +my @stack; +my $prev_count; +my $prev_indent = -1; + +while (defined($_ = <>)) { + if (m/^( *)[0-9.]+% \[([0-9]+)\]\s*(\S+)/) { + my $indent = length($1); + if ($indent <= $prev_indent) { + print join(';', reverse(@stack[0 .. $prev_indent])) . + " $prev_count\n"; + } + $stack[$indent] = $3; + $prev_count = $2; + $prev_indent = $indent; + } +} +print join(';', reverse(@stack[0 .. $prev_indent])) . " $prev_count\n"; diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-recursive.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-recursive.pl new file mode 100755 index 00000000000..9eae54592c4 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-recursive.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl -ws +# +# stackcollapse-recursive Collapse direct recursive backtraces +# +# Post-process a stack list and merge direct recursive calls: +# +# Example input: +# +# main;recursive;recursive;recursive;helper 1 +# +# Output: +# +# main;recursive;helper 1 +# +# Copyright 2014 Gabriel Corona. All rights reserved. +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at docs/cddl1.txt or +# http://opensource.org/licenses/CDDL-1.0. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at docs/cddl1.txt. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END + +my %stacks; + +while(<>) { + chomp; + my ($stack_, $value) = (/^(.*)\s+?(\d+(?:\.\d*)?)$/); + if ($stack_) { + my @stack = split(/;/, $stack_); + + my @result = (); + my $i; + my $last=""; + for($i=0; $i!=@stack; ++$i) { + if(!($stack[$i] eq $last)) { + $result[@result] = $stack[$i]; + $last = $stack[$i]; + } + } + + $stacks{join(";", @result)} += $value; + } +} + +foreach my $k (sort { $a cmp $b } keys %stacks) { + print "$k $stacks{$k}\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-sample.awk b/tests/benchmarks/_script/flamegraph/stackcollapse-sample.awk new file mode 100755 index 00000000000..bafc4af3468 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-sample.awk @@ -0,0 +1,231 @@ +#!/usr/bin/awk -f +# +# Uses MacOS' /usr/bin/sample to generate a flamegraph of a process +# +# Usage: +# +# sudo sample [pid] -file /dev/stdout | stackcollapse-sample.awk | flamegraph.pl +# +# Options: +# +# The output will show the name of the library/framework at the call-site +# with the form AppKit`NSApplication or libsystem`start_wqthread. +# +# If showing the framework or library name is not required, pass +# MODULES=0 as an argument of the sample program. +# +# The generated SVG will be written to the output stream, and can be piped +# into flamegraph.pl directly, or written to a file for conversion later. +# +# --- +# +# Copyright (c) 2017, Apple Inc. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +BEGIN { + + # Command line options + MODULES = 1 # Allows the user to enable/disable printing of modules. + + # Internal variables + _FOUND_STACK = 0 # Found the stack traces in the output. + _LEVEL = -1 # The current level of indentation we are running. + + # The set of symbols to ignore for 'waiting' threads, for ease of use. + # This will hide waiting threads from the view, making it easier to + # see what is actually running in the sample. These may be adjusted + # as necessary or appended to if other symbols need to be filtered out. + + _IGNORE["libsystem_kernel`__psynch_cvwait"] = 1 + _IGNORE["libsystem_kernel`__select"] = 1 + _IGNORE["libsystem_kernel`__semwait_signal"] = 1 + _IGNORE["libsystem_kernel`__ulock_wait"] = 1 + _IGNORE["libsystem_kernel`__wait4"] = 1 + _IGNORE["libsystem_kernel`__workq_kernreturn"] = 1 + _IGNORE["libsystem_kernel`kevent"] = 1 + _IGNORE["libsystem_kernel`mach_msg_trap"] = 1 + _IGNORE["libsystem_kernel`read"] = 1 + _IGNORE["libsystem_kernel`semaphore_wait_trap"] = 1 + + # The same set of symbols as above, without the module name. + _IGNORE["__psynch_cvwait"] = 1 + _IGNORE["__select"] = 1 + _IGNORE["__semwait_signal"] = 1 + _IGNORE["__ulock_wait"] = 1 + _IGNORE["__wait4"] = 1 + _IGNORE["__workq_kernreturn"] = 1 + _IGNORE["kevent"] = 1 + _IGNORE["mach_msg_trap"] = 1 + _IGNORE["read"] = 1 + _IGNORE["semaphore_wait_trap"] = 1 + +} + +# This is the first line in the /usr/bin/sample output that indicates the +# samples follow subsequently. Until we see this line, the rest is ignored. + +/^Call graph/ { + _FOUND_STACK = 1 +} + +# This is found when we have reached the end of the stack output. +# Identified by the string "Total number in stack (...)". + +/^Total number/ { + _FOUND_STACK = 0 + printStack(_NEST,0) +} + +# Prints the stack from FROM to TO (where FROM > TO) +# Called when indenting back from a previous level, or at the end +# of processing to flush the last recorded sample + +function printStack(FROM,TO) { + + # We ignore certain blocking wait states, in the absence of being + # able to filter these threads from collection, otherwise + # we'll end up with many threads of equal length that represent + # the total time the sample was collected. + # + # Note that we need to collect the information to ensure that the + # timekeeping for the parental functions is appropriately adjusted + # so we just avoid printing it out when that occurs. + _PRINT_IT = !_IGNORE[_NAMES[FROM]] + + # We run through all the names, from the root to the leaf, so that + # we generate a line that flamegraph.pl will like, of the form: + # Thread1234;example`main;example`otherFn 1234 + + for(l = FROM; l>=TO; l--) { + if (_PRINT_IT) { + printf("%s", _NAMES[0]) + for(i=1; i<=l; i++) { + printf(";%s", _NAMES[i]) + } + print " " _TIMES[l] + } + + # We clean up our current state to avoid bugs. + delete _NAMES[l] + delete _TIMES[l] + } +} + +# This is where we process each line, of the form: +# 5130 Thread_8749954 +# + 5130 start_wqthread (in libsystem_pthread.dylib) ... +# + 4282 _pthread_wqthread (in libsystem_pthread.dylib) ... +# + ! 4282 __doworkq_kernreturn (in libsystem_kernel.dylib) ... +# + 848 _pthread_wqthread (in libsystem_pthread.dylib) ... +# + 848 __doworkq_kernreturn (in libsystem_kernel.dylib) ... + +_FOUND_STACK && match($0,/^ [^0-9]*[0-9]/) { + + # We maintain two counters: + # _LEVEL: the high water mark of the indentation level we have seen. + # _NEST: the current indentation level. + # + # We keep track of these two levels such that when the nesting level + # decreases, we print out the current state of where we are. + + _NEST=(RLENGTH-5)/2 + sub(/^[^0-9]*/,"") # Normalise the leading content so we start with time. + _TIME=$1 # The time recorded by 'sample', first integer value. + + # The function name is in one or two parts, depending on what kind of + # function it is. + # + # If it is a standard C or C++ function, it will be of the form: + # exampleFunction + # Example::Function + # + # If it is an Objective-C funtion, it will be of the form: + # -[NSExample function] + # +[NSExample staticFunction] + # -[NSExample function:withParameter] + # +[NSExample staticFunction:withParameter:andAnother] + + _FN1 = $2 + _FN2 = $3 + + # If it is a standard C or C++ function then the following word will + # either be blank, or the text '(in', so we jut use the first one: + + if (_FN2 == "(in" || _FN2 == "") { + _FN =_FN1 + } else { + # Otherwise we concatenate the first two parts with . + _FN = _FN1 "." _FN2 + } + + # Modules are shown with '(in libfoo.dylib)' or '(in AppKit)' + + _MODULE = "" + match($0, /\(in [^)]*\)/) + + if (RSTART > 0 && MODULES) { + + # Strip off the '(in ' (4 chars) and the final ')' char (1 char) + _MODULE = substr($0, RSTART+4, RLENGTH-5) + + # Remove the .dylib function, since it adds no value. + gsub(/\.dylib/, "", _MODULE) + + # The function name is 'module`functionName' + _FN = _MODULE "`" _FN + } + + # Now we have set up the variables, we can decide how to apply it + # If we are descending in the nesting, we don't print anything out: + # a + # ab + # abc + # + # We only print out something when we go back a level, or hit the end: + # abcd + # abe < prints out the stack up until this point, i.e. abcd + + # We store a pair of arrays, indexed by the nesting level: + # + # _TIMES - a list of the time reported to that function + # _NAMES - a list of the function names for each current stack trace + + # If we are backtracking, we need to flush the current output. + if (_NEST <= _LEVEL) { + printStack(_LEVEL,_NEST) + } + + # Record the name and time of the function where we are. + _NAMES[_NEST] = _FN + _TIMES[_NEST] = _TIME + + # We subtract the time we took from our parent so we don't double count. + if (_NEST > 0) { + _TIMES[_NEST-1] -= _TIME + } + + # Raise the high water mark of the level we have reached. + _LEVEL = _NEST +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-stap.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-stap.pl new file mode 100755 index 00000000000..bca4046192f --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-stap.pl @@ -0,0 +1,84 @@ +#!/usr/bin/perl -w +# +# stackcollapse-stap.pl collapse multiline SystemTap stacks +# into single lines. +# +# Parses a multiline stack followed by a number on a separate line, and +# outputs a semicolon separated stack followed by a space and the number. +# If memory addresses (+0xd) are present, they are stripped, and resulting +# identical stacks are colased with their counts summed. +# +# USAGE: ./stackcollapse.pl infile > outfile +# +# Example input: +# +# 0xffffffff8103ce3b : native_safe_halt+0xb/0x10 [kernel] +# 0xffffffff8101c6a3 : default_idle+0x53/0x1d0 [kernel] +# 0xffffffff81013236 : cpu_idle+0xd6/0x120 [kernel] +# 0xffffffff815bf03e : rest_init+0x72/0x74 [kernel] +# 0xffffffff81aebbfe : start_kernel+0x3ba/0x3c5 [kernel] +# 2404 +# +# Example output: +# +# start_kernel;rest_init;cpu_idle;default_idle;native_safe_halt 2404 +# +# Input may contain many stacks as generated from SystemTap. +# +# Copyright 2011 Joyent, Inc. All rights reserved. +# Copyright 2011 Brendan Gregg. All rights reserved. +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at docs/cddl1.txt or +# http://opensource.org/licenses/CDDL-1.0. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at docs/cddl1.txt. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END +# +# 16-Feb-2012 Brendan Gregg Created this. + +use strict; + +my %collapsed; + +sub remember_stack { + my ($stack, $count) = @_; + $collapsed{$stack} += $count; +} + +my @stack; + +foreach (<>) { + chomp; + + if (m/^\s*(\d+)+$/) { + remember_stack(join(";", @stack), $1); + @stack = (); + next; + } + + next if (m/^\s*$/); + + my $frame = $_; + $frame =~ s/^\s*//; + $frame =~ s/\+[^+]*$//; + $frame =~ s/.* : //; + $frame = "-" if $frame eq ""; + unshift @stack, $frame; +} + +foreach my $k (sort { $a cmp $b } keys %collapsed) { + printf "$k $collapsed{$k}\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-vsprof.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-vsprof.pl new file mode 100755 index 00000000000..a13c1daab35 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-vsprof.pl @@ -0,0 +1,98 @@ +#!/usr/bin/perl -w +# +# stackcollapse-vsprof.pl +# +# Parses the CSV file containing a call tree from a visual studio profiler and produces an output suitable for flamegraph.pl. +# +# USAGE: perl stackcollapse-vsprof.pl infile > outfile +# +# WORKFLOW: +# +# This example assumes you have visual studio 2015 installed. +# +# 1. Profile C++ your application using visual studio +# 2. On visual studio, choose export the call tree as csv +# 3. Generate a flamegraph: perl stackcollapse-vsprof CallTreeSummary.csv | perl flamegraph.pl > result_vsprof.svg +# +# INPUT EXAMPLE : +# +# Level,Function Name,Inclusive Samples,Exclusive Samples,Inclusive Samples %,Exclusive Samples %,Module Name, +# 1,"main","8,735",0,100.00,0.00,"an_executable.exe", +# 2,"testing::UnitTest::Run","8,735",0,100.00,0.00,"an_executable.exe", +# 3,"boost::trim_end_iter_select > >,boost::is_classifiedF>",306,16,3.50,0.18,"an_executable.exe", +# +# OUTPUT EXAMPLE : +# +# main;testing::UnitTest::Run;boost::trim_end_iter_select>>,boost::is_classifiedF> 306 + +use strict; + +sub massage_function_names; +sub parse_integer; +sub print_stack_trace; + +# data initialization +my @stack = (); +my $line_number = 0; +my $previous_samples = 0; + +my $num_args = $#ARGV + 1; +if ($num_args != 1) { + print "$ARGV[0]\n"; + print "Usage : stackcollapse-vsprof.pl > out.txt\n"; + exit; +} + +my $input_csv_file = $ARGV[0]; +my $line_parser_rx = qr{ + ^\s*(\d+?), # level in the stack + ("[^"]+" | [^,]+), # function name (beware of spaces) + ("[^"]+" | [^,]+), # number of samples (beware of locale number formatting) +}ox; + +open(my $fh, '<', $input_csv_file) or die "Can't read file '$input_csv_file' [$!]\n"; + +while (my $current_line = <$fh>){ + $line_number = $line_number + 1; + + # to discard first line which typically contains headers + next if $line_number == 1; + next if $current_line =~ /^\s*$/o; + + ($current_line =~ $line_parser_rx) or die "Error in regular expression at line $line_number : $current_line\n"; + + my $level = int $1; + my $function = massage_function_names($2); + my $samples = parse_integer($3); + my $stack_len = @stack; + + #print "[DEBUG] $line_number : $level $function $samples $stack_len\n"; + + next if not $level; + ($level <= $stack_len + 1) or die "Error in stack at line $line_number : $current_line\n"; + + if ($level <= $stack_len) { + print_stack_trace(\@stack, $previous_samples); + my $to_remove = $level - $stack_len - 1; + splice(@stack, $to_remove); + } + + $stack_len < 1000 or die "Stack overflow at line $line_number"; + push(@stack, $function); + $previous_samples = $samples; +} +print_stack_trace(\@stack, $previous_samples); + +sub massage_function_names { + return ($_[0] =~ s/\s*|^"|"$//gro); +} + +sub parse_integer { + return int ($_[0] =~ s/[., ]|^"|"$//gro); +} + +sub print_stack_trace { + my ($stack_ref, $sample) = @_; + my $stack_trace = join(";", @$stack_ref); + print "$stack_trace $sample\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-vtune-mc.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-vtune-mc.pl new file mode 100755 index 00000000000..e132ab08cf3 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-vtune-mc.pl @@ -0,0 +1,103 @@ +#!/usr/bin/perl -w +# +# stackcollapse-vtune-mc.pl +# +# Parses the CSV file containing a call tree from Intel VTune memory-consumption profiler and produces an output suitable for flamegraph.pl. +# +# USAGE: perl stackcollapse-vtune-mc.pl [options] infile > outfile +# +# WORKFLOW: +# +# This assumes you have Intel VTune installed and on path (using Command Line) +# +# 1. Profile C++ application tachyon (example shipped with Intel VTune 2019): +# +# amplxe-cl -collect memory-consumption -r mc_tachyon -- ./tachyon +# +# 2. Export raw VTune data to csv file: +# ### for Intel VTune 2019 +# amplxe-cl -R top-down -call-stack-mode all \ +# -column="Allocations:Self","Allocation Size:Self","Module" \ +# -report-out allocations.csv -format csv \ +# -csv-delimiter comma -r mc_tachyon +# +# 3. Generate a flamegraph: +# ## Generate for allocations amount. +# perl stackcollapse-vtune-mc.pl allocations.csv > out.folded +# perl flamegraph.pl --countname=allocations out.folded > vtune_tachyon_mc.svg +# +# ## Or you can generate for allocation size in bytes. +# perl stackcollapse-vtune-mc.pl -s allocations.csv > out.folded +# perl flamegraph.pl --countname=allocations out.folded > vtune_tachyon_mc_size.svg +# +# AUTHOR: Rohith Bakkannagari +# 27-Nov-2019 UnpluggedCoder Forked from stackcollapse-vtune.pl, for memory-consumption flamegraph + +use strict; +use Getopt::Long; + +sub usage { + die < out.folded\n + --size # Accumulate allocation size in bytes instead of allocation counts.\n +NOTE : The csv file should exported by `amplxe-cl` tool with the exact -column parameter shows below. + amplxe-cl -R top-down -call-stack-mode all \ + -column="Allocations:Self","Allocation Size:Self","Module" \ + -report-out allocations.csv -format csv \ + -csv-delimiter comma -r mc_tachyon +USAGE_END +} + +# data initialization +my @stack = (); +my $rowCounter = 0; # flag for row number + +my $accSize = ''; +GetOptions ('size' => \$accSize) +or usage(); + +my $numArgs = $#ARGV + 1; +if ($numArgs != 1){ + usage(); + exit; +} + +my $inputCSVFile = $ARGV[0]; +open(my $fh, '<', $inputCSVFile) or die "Can't read file '$inputCSVFile' [$!]\n"; + +while (my $currLine = <$fh>){ + # discard warning line + next if $rowCounter == 0 && rindex($currLine, "war:", 0) == 0; + $rowCounter = $rowCounter + 1; + # to discard first row which typically contains headers + next if $rowCounter == 1; + chomp $currLine; + #VTune - sometimes the call stack information is enclosed in double quotes (?). To remove double quotes. + $currLine =~ s/\"//g; + + ### for Intel VTune 2019 + ### CSV header should be like below + ### Function Stack,Allocation Size:Self,Deallocation Size:Self,Allocations:Self,Module + $currLine =~ /(\s*)(.*?),([0-9]*?\.?[0-9]*?),([0-9]*?\.?[0-9]*?),([0-9]*?\.?[0-9]*?),(.*)/ or die "Error in regular expression on the current line $currLine\n"; + my $func = $2.'('.$6.')'; # function(module) + my $depth = length ($1); + my $allocBytes = $3; # allocation size + my $allocs = $5; # allocations + + my $tempString = ''; + $stack [$depth] = $func; + if ($accSize){ + next if $allocBytes eq ''; + foreach my $i (0 .. $depth - 1) { + $tempString = $tempString.$stack[$i].";"; + } + $tempString = $tempString.$func." $allocBytes\n"; + } else { + next if $allocs == 0; + foreach my $i (0 .. $depth - 1) { + $tempString = $tempString.$stack[$i].";"; + } + $tempString = $tempString.$func." $allocs\n"; + } + print "$tempString"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-vtune.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-vtune.pl new file mode 100644 index 00000000000..2a13e3b2d95 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-vtune.pl @@ -0,0 +1,97 @@ +#!/usr/bin/perl -w +# +# stackcollapse-vtune.pl +# +# Parses the CSV file containing a call tree from Intel VTune hotspots profiler and produces an output suitable for flamegraph.pl. +# +# USAGE: perl stackcollapse-vtune.pl infile > outfile +# +# WORKFLOW: +# +# This assumes you have Intel VTune installed and on path (using Command Line) +# +# 1. Profile C++ application tachyon_find_hotspots (example shipped with Intel VTune 2013): +# +# amplxe-cl -collect hotspots -r result_vtune_tachyon -- ./tachyon_find_hotspots +# +# 2. Export raw VTune data to csv file: +# +##### VTune 2013 & 2015 +# amplxe-cl -R top-down -report-out result_vtune_tachyon.csv -filter "Function Stack" -format csv -csv-delimiter comma -r result_vtune_tachyon +#### VTune 2016 +# amplxe-cl.exe -R top-down -call-stack-mode all -column="CPU Time:Self","Module" -report-output result_vtune_tachyon.csv -filter "Function Stack" -format csv -csv-delimiter comma -r result_vtune_tachyon +# +# 3. Generate a flamegraph: +# +# perl stackcollapse-vtune result_vtune_tachyon.csv | perl flamegraph.pl > result_vtune_tachyon.svg +# +# AUTHOR: Rohith Bakkannagari + +use strict; + +# data initialization +my @stack = (); +my $rowCounter = 0; #flag for row number + +my $numArgs = $#ARGV + 1; +if ($numArgs != 1) +{ +print "$ARGV[0]\n"; +print "Usage : stackcollapse-vtune.pl > out.txt\n"; +exit; +} + +my $inputCSVFile = $ARGV[0]; +my $funcOnly = ''; +my $depth = 0; +my $selfTime = 0; +my $dllName = ''; + +open(my $fh, '<', $inputCSVFile) or die "Can't read file '$inputCSVFile' [$!]\n"; + +while (my $currLine = <$fh>){ + $rowCounter = $rowCounter + 1; + # to discard first row which typically contains headers + next if $rowCounter == 1; + chomp $currLine; + + ### VTune 2013 & 2015 + #VTune - sometimes the call stack information is enclosed in double quotes (?). To remove double quotes. Not necessary for XCode instruments (MAC) + $currLine =~ s/\"//g; + $currLine =~ /(\s*)(.*),(.*),.*,([0-9]*\.?[0-9]+)/ or die "Error in regular expression on the current line\n"; + $dllName = $3; + $func = $dllName.'!'.$2; # Eg : m_lxe.dll!MathWorks::lxe::IrEngineDecorator::Apply + $depth = length ($1); + $selfTime = $4*1000; # selfTime in msec + ### VTune 2013 & 2015 + + ### VTune 2016 + # $currLine =~ /(\s*)(.*?),([0-9]*\.?[0-9]+?),(.*)/ or die "Error in regular expression on the current line $currLine\n"; + # if ($2 =~ /\"/) + # { + # $currLine =~ /(\s*)\"(.*?)\",([0-9]*\.?[0-9]+?),(.*)/ or die "Error in regular expression on the current line $currLine\n"; + # $funcOnly = $2; + # $depth = length ($1); + # $selfTime = $3*1000; # selfTime in msec + # $dllName = $4; + # } + # else + # { + # $funcOnly = $2; + # $depth = length ($1); + # $selfTime = $3*1000; # selfTime in msec + # $dllName = $4; + # } + # my $func = $dllName.'!'.$funcOnly; # Eg : m_lxe.dll!MathWorks::lxe::IrEngineDecorator::Apply + ### VTune 2016 + + my $tempString = ''; + $stack [$depth] = $func; + foreach my $i (0 .. $depth - 1) { + $tempString = $tempString.$stack[$i].";"; + } + $tempString = $tempString.$func." $selfTime\n"; + if ($selfTime != 0){ + print "$tempString"; + } +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-wcp.pl b/tests/benchmarks/_script/flamegraph/stackcollapse-wcp.pl new file mode 100755 index 00000000000..4d1d58434a0 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-wcp.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl -ws +# +# stackcollapse-wcp Collapse wallClockProfiler backtraces +# +# Parse a list of GDB backtraces as generated by https://github.com/jasonrohrer/wallClockProfiler +# +# Copyright 2014 Gabriel Corona. All rights reserved. +# Portions Copyright 2020 Ștefan Talpalaru +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at docs/cddl1.txt or +# http://opensource.org/licenses/CDDL-1.0. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at docs/cddl1.txt. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END + +use strict; + +my $current = ""; +my $start_processing = 0; +my $samples = 0; +my %stacks; + +while(<>) { + s/^\s+|\s+$//g; + + if (m/^Full stacks/) { + $start_processing = 1; + next; + } + + if (not $start_processing) { + next; + } + + if(m/^\d+\.\d+% =+ \((\d+) samples\)/) { + # 99.791% ===================================== (17194 samples) + $samples = $1; + next; + } elsif (m/^\d+: (.*)$/) { + # 1: poll__YNjd8fE6xG8CRNwfLnrx0g_2 (at /mnt/sde1/storage/nim-beacon-chain-clean/vendor/nim-chronos/chronos/asyncloop.nim:343) + my $function = $1; + if ($current eq "") { + $current = $function; + } else { + $current = $function . ";" . $current; + } + } elsif (m/^$/ and $current ne "") { + $stacks{$current} += $samples; + $current = ""; + } +} + +foreach my $k (sort { $a cmp $b } keys %stacks) { + print "$k $stacks{$k}\n"; +} + diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse-xdebug.php b/tests/benchmarks/_script/flamegraph/stackcollapse-xdebug.php new file mode 100755 index 00000000000..52cc3d65a0c --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse-xdebug.php @@ -0,0 +1,197 @@ +#!/usr/bin/php + outfile + -h --help Show this message + -t Weight stack counts by duration using the time index in the trace (default) + -c Invocation counts only. Simply count stacks in the trace and sum duplicates, don't weight by duration. + +Example input: +For more info on xdebug and generating traces see +https://xdebug.org/docs/execution_trace. + +Version: 2.0.0RC4-dev +TRACE START [2007-05-06 18:29:01] +1 0 0 0.010870 114112 {main} 1 ../trace.php 0 +2 1 0 0.032009 114272 str_split 0 ../trace.php 8 +2 1 1 0.032073 116632 +2 2 0 0.033505 117424 ret_ord 1 ../trace.php 10 +3 3 0 0.033531 117584 ord 0 ../trace.php 5 +3 3 1 0.033551 117584 +... +TRACE END [2007-05-06 18:29:01] + +Example output: + +- c +{main};str_split 1 +{main};ret_ord;ord 6 + +-t +{main} 23381 +{main};str_split 64 +{main};ret_ord 215 +{main};ret_ord;ord 106 + +EOT; + + exit($exit); +} + +function collapseStack(array $stack, string $func_name_key): string { + return implode(';', array_column($stack, $func_name_key)); +} + +function addCurrentStackToStacks(array $stack, float $dur, array &$stacks) { + $collapsed = implode(';', $stack); + $duration = SCALE_FACTOR * $dur; + + if (array_key_exists($collapsed, $stacks)) { + $stacks[$collapsed] += $duration; + } else { + $stacks[$collapsed] = $duration; + } +} + +function isEOTrace(string $l) { + $pattern = "/^(\\t|TRACE END)/"; + return preg_match($pattern, $l); +} + +$filename = $argv[$optind] ?? null; +if ($filename === null) { + usage(1); +} + +$do_time = !isset($args['c']); + +// First make sure our file is consistently formatted with only one \t delimiting each field +$out = []; +$retval = null; +exec("sed -in 's/\t\+/\t/g' " . escapeshellarg($filename), $out, $retval); +if ($retval !== 0) { + usage(1); +} + +$handle = fopen($filename, 'r'); + +if ($handle === false) { + echo "Unable to open $filename \n\n"; + usage(1); +} + +// Loop till we find TRACE START +while ($l = fgets($handle)) { + if (strpos($l, "TRACE START") === 0) { + break; + } +} + +const SCALE_FACTOR = 1000000; +$stacks = []; +$current_stack = []; +$was_exit = false; +$prev_start_time = 0; + +if ($do_time) { + // Weight counts by duration + // Xdebug trace time indices have 6 sigfigs of precision + // We have a perfect trace, but let's instead pretend that + // this was collected by sampling at 10^6 Hz + // then each millionth of a second this stack took to execute is 1 count + while ($l = fgets($handle)) { + if (isEOTrace($l)) { + break; + } + + $parts = explode("\t", $l); + list($level, $fn_no, $is_exit, $time) = $parts; + + if ($is_exit) { + if (empty($current_stack)) { + echo "[WARNING] Found function exit without corresponding entrance. Discarding line. Check your input.\n"; + continue; + } + + addCurrentStackToStacks($current_stack, $time - $prev_start_time, $stacks); + array_pop($current_stack); + } else { + $func_name = $parts[5]; + + if (!empty($current_stack)) { + addCurrentStackToStacks($current_stack, $time - $prev_start_time, $stacks); + } + + $current_stack[] = $func_name; + } + $prev_start_time = $time; + } +} else { + // Counts only + while ($l = fgets($handle)) { + if (isEOTrace($l)) { + break; + } + + $parts = explode("\t", $l); + list($level, $fn_no, $is_exit) = $parts; + + if ($is_exit === "1") { + if (!$was_exit) { + $collapsed = implode(";", $current_stack); + if (array_key_exists($collapsed, $stacks)) { + $stacks[$collapsed]++; + } else { + $stacks[$collapsed] = 1; + } + } + + array_pop($current_stack); + $was_exit = true; + } else { + $func_name = $parts[5]; + $current_stack[] = $func_name; + $was_exit = false; + } + } +} + +foreach ($stacks as $stack => $count) { + echo "$stack $count\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/stackcollapse.pl b/tests/benchmarks/_script/flamegraph/stackcollapse.pl new file mode 100755 index 00000000000..1e00c521368 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/stackcollapse.pl @@ -0,0 +1,109 @@ +#!/usr/bin/perl -w +# +# stackcollapse.pl collapse multiline stacks into single lines. +# +# Parses a multiline stack followed by a number on a separate line, and +# outputs a semicolon separated stack followed by a space and the number. +# If memory addresses (+0xd) are present, they are stripped, and resulting +# identical stacks are colased with their counts summed. +# +# USAGE: ./stackcollapse.pl infile > outfile +# +# Example input: +# +# unix`i86_mwait+0xd +# unix`cpu_idle_mwait+0xf1 +# unix`idle+0x114 +# unix`thread_start+0x8 +# 1641 +# +# Example output: +# +# unix`thread_start;unix`idle;unix`cpu_idle_mwait;unix`i86_mwait 1641 +# +# Input may contain many stacks, and can be generated using DTrace. The +# first few lines of input are skipped (see $headerlines). +# +# Copyright 2011 Joyent, Inc. All rights reserved. +# Copyright 2011 Brendan Gregg. All rights reserved. +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at docs/cddl1.txt or +# http://opensource.org/licenses/CDDL-1.0. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at docs/cddl1.txt. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END +# +# 14-Aug-2011 Brendan Gregg Created this. + +use strict; + +my $headerlines = 3; # number of input lines to skip +my $includeoffset = 0; # include function offset (except leafs) +my %collapsed; + +sub remember_stack { + my ($stack, $count) = @_; + $collapsed{$stack} += $count; +} + +my $nr = 0; +my @stack; + +foreach (<>) { + next if $nr++ < $headerlines; + chomp; + + if (m/^\s*(\d+)+$/) { + my $count = $1; + my $joined = join(";", @stack); + + # trim leaf offset if these were retained: + $joined =~ s/\+[^+]*$// if $includeoffset; + + remember_stack($joined, $count); + @stack = (); + next; + } + + next if (m/^\s*$/); + + my $frame = $_; + $frame =~ s/^\s*//; + $frame =~ s/\+[^+]*$// unless $includeoffset; + + # Remove arguments from C++ function names: + $frame =~ s/(::.*)[(<].*/$1/; + + $frame = "-" if $frame eq ""; + + my @inline; + for (split /\->/, $frame) { + my $func = $_; + + # Strip out L and ; included in java stacks + $func =~ tr/\;/:/; + $func =~ s/^L//; + $func .= "_[i]" if scalar(@inline) > 0; #inlined + + push @inline, $func; + } + + unshift @stack, @inline; +} + +foreach my $k (sort { $a cmp $b } keys %collapsed) { + print "$k $collapsed{$k}\n"; +} diff --git a/tests/benchmarks/_script/flamegraph/test.sh b/tests/benchmarks/_script/flamegraph/test.sh new file mode 100755 index 00000000000..3592f351f10 --- /dev/null +++ b/tests/benchmarks/_script/flamegraph/test.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# +# test.sh - Check flame graph software vs test result files. +# +# This is used to detect regressions in the flame graph software. +# See record-test.sh, which refreshes these files after intended software +# changes. +# +# Currently only tests stackcollapse-perf.pl. + +set -euo pipefail +set -x +set -v + +# ToDo: add some form of --inline, and --inline --context tests. These are +# tricky since they use addr2line, whose output will vary based on the test +# system's binaries and symbol tables. +for opt in pid tid kernel jit all addrs; do + for testfile in test/*.txt ; do + echo testing $testfile : $opt + outfile=${testfile#*/} + outfile=test/results/${outfile%.txt}"-collapsed-${opt}.txt" + perl ./stackcollapse-perf.pl --"${opt}" "${testfile}" 2> /dev/null | diff -u - "${outfile}" + perl ./flamegraph.pl "${outfile}" > /dev/null + done +done diff --git a/tests/benchmarks/benchmark.sh b/tests/benchmarks/benchmark.sh new file mode 100755 index 00000000000..4f39a0b14e9 --- /dev/null +++ b/tests/benchmarks/benchmark.sh @@ -0,0 +1,381 @@ +#!/bin/bash + +set -eo pipefail + +# +# parse the command line +# + +usage() { echo "usage: $(basename "$0") [--cli ] [--name ] [--baseline-cli ] [--suite ] [--admin] [--json ] [--flamegraph] [--zip ] [--verbose] [--debug]"; } + +TEST_CLI="git" +TEST_CLI_NAME= +BASELINE_CLI= +SUITE= +JSON_RESULT= +FLAMEGRAPH= +ZIP_RESULT= +OUTPUT_DIR= +ADMIN= +VERBOSE= +DEBUG= +NEXT= + +for a in "$@"; do + if [ "${NEXT}" = "cli" ]; then + TEST_CLI="${a}" + NEXT= + elif [ "${NEXT}" = "name" ]; then + TEST_CLI_NAME="${a}" + NEXT= + elif [ "${NEXT}" = "baseline-cli" ]; then + BASELINE_CLI="${a}" + NEXT= + elif [ "${NEXT}" = "suite" ]; then + SUITE="${a}" + NEXT= + elif [ "${NEXT}" = "json" ]; then + JSON_RESULT="${a}" + NEXT= + elif [ "${NEXT}" = "zip" ]; then + ZIP_RESULT="${a}" + NEXT= + elif [ "${NEXT}" = "output-dir" ]; then + OUTPUT_DIR="${a}" + NEXT= + elif [ "${a}" = "c" ] || [ "${a}" = "--cli" ]; then + NEXT="cli" + elif [[ "${a}" == "-c"* ]]; then + TEST_CLI="${a/-c/}" + elif [ "${a}" = "n" ] || [ "${a}" = "--name" ]; then + NEXT="name" + elif [[ "${a}" == "-n"* ]]; then + TEST_CLI_NAME="${a/-n/}" + elif [ "${a}" = "b" ] || [ "${a}" = "--baseline-cli" ]; then + NEXT="baseline-cli" + elif [[ "${a}" == "-b"* ]]; then + BASELINE_CLI="${a/-b/}" + elif [ "${a}" = "-s" ] || [ "${a}" = "--suite" ]; then + NEXT="suite" + elif [[ "${a}" == "-s"* ]]; then + SUITE="${a/-s/}" + elif [ "${a}" == "--admin" ]; then + ADMIN=1 + elif [ "${a}" = "-v" ] || [ "${a}" == "--verbose" ]; then + VERBOSE=1 + elif [ "${a}" == "--debug" ]; then + VERBOSE=1 + DEBUG=1 + elif [ "${a}" = "-j" ] || [ "${a}" == "--json" ]; then + NEXT="json" + elif [[ "${a}" == "-j"* ]]; then + JSON_RESULT="${a/-j/}" + elif [ "${a}" = "-F" ] || [ "${a}" == "--flamegraph" ]; then + FLAMEGRAPH=1 + elif [ "${a}" = "-z" ] || [ "${a}" == "--zip" ]; then + NEXT="zip" + elif [[ "${a}" == "-z"* ]]; then + ZIP_RESULT="${a/-z/}" + elif [ "${a}" = "--output-dir" ]; then + NEXT="output-dir" + else + echo "$(basename "$0"): unknown option: ${a}" 1>&2 + usage 1>&2 + exit 1 + fi +done + +if [ "${NEXT}" != "" ]; then + usage 1>&2 + exit 1 +fi + +if [ "${OUTPUT_DIR}" = "" ]; then + OUTPUT_DIR=${OUTPUT_DIR:="$(mktemp -d)"} + CLEANUP_DIR=1 +fi + +# +# collect some information about the test environment +# + +SYSTEM_OS=$(uname -s) +if [ "${SYSTEM_OS}" = "Darwin" ]; then SYSTEM_OS="macOS"; fi + +SYSTEM_KERNEL=$(uname -v) + +fullpath() { + if [[ "$(uname -s)" == "MINGW"* && $(cygpath -u "${TEST_CLI}") == "/"* ]]; then + echo "$1" + elif [[ "${TEST_CLI}" == "/"* ]]; then + echo "$1" + else + which "$1" + fi +} + +cli_version() { + if [[ "$(uname -s)" == "MINGW"* ]]; then + $(cygpath -u "$1") --version + else + "$1" --version + fi +} + +cli_commit() { + if [[ "$(uname -s)" == "MINGW"* ]]; then + BUILD_OPTIONS=$($(cygpath -u "$1") version --build-options) + else + BUILD_OPTIONS=$("$1" version --build-options) + fi + + echo "${BUILD_OPTIONS}" | { grep '^built from commit: ' || echo "unknown"; } | sed -e 's/^built from commit: //' +} + +TEST_CLI_NAME=$(basename "${TEST_CLI}") +TEST_CLI_PATH=$(fullpath "${TEST_CLI}") +TEST_CLI_VERSION=$(cli_version "${TEST_CLI}") +TEST_CLI_COMMIT=$(cli_commit "${TEST_CLI}") + +if [ "${BASELINE_CLI}" != "" ]; then + if [[ "${BASELINE_CLI}" == "/"* ]]; then + BASELINE_CLI_PATH="${BASELINE_CLI}" + else + BASELINE_CLI_PATH=$(which "${BASELINE_CLI}") + fi + + BASELINE_CLI_NAME=$(basename "${BASELINE_CLI}") + BASELINE_CLI_PATH=$(fullpath "${BASELINE_CLI}") + BASELINE_CLI_VERSION=$(cli_version "${BASELINE_CLI}") + BASELINE_CLI_COMMIT=$(cli_commit "${BASELINE_CLI}") +fi + +# +# run the benchmarks +# + +echo "##############################################################################" +if [ "${SUITE}" != "" ]; then + SUITE_PREFIX="${SUITE/::/__}" + echo "## Running ${SUITE} benchmarks" +else + echo "## Running all benchmarks" +fi +echo "##############################################################################" +echo "" + +if [ "${BASELINE_CLI}" != "" ]; then + echo "# Baseline CLI: ${BASELINE_CLI} (${BASELINE_CLI_VERSION})" +fi +echo "# Test CLI: ${TEST_CLI} (${TEST_CLI_VERSION})" +echo "" + +BENCHMARK_DIR=${BENCHMARK_DIR:=$(dirname "$0")} +ANY_FOUND= +ANY_FAILED= + +indent() { sed "s/^/ /"; } +time_in_ms() { if [ "$(uname -s)" = "Darwin" ]; then date "+%s000"; else date "+%s%N" ; fi; } +humanize_secs() { + units=('s' 'ms' 'us' 'ns') + unit=0 + time="${1}" + + if [ "${time}" = "" ]; then + echo "" + return + fi + + # bash doesn't do floating point arithmetic. ick. + while [[ "${time}" == "0."* ]] && [ "$((unit+1))" != "${#units[*]}" ]; do + time="$(echo | awk "{ print ${time} * 1000 }")" + unit=$((unit+1)) + done + + echo "${time} ${units[$unit]}" +} + +TIME_START=$(time_in_ms) + +for TEST_PATH in "${BENCHMARK_DIR}"/*; do + TEST_FILE=$(basename "${TEST_PATH}") + + if [ ! -f "${TEST_PATH}" ] || [ ! -x "${TEST_PATH}" ]; then + continue + fi + + if [[ "${TEST_FILE}" != *"__"* ]]; then + continue + fi + + if [[ "${TEST_FILE}" != "${SUITE_PREFIX}"* ]]; then + continue + fi + + ANY_FOUND=1 + TEST_NAME="${TEST_FILE/__/::}" + + echo -n "${TEST_NAME}:" + if [ "${VERBOSE}" = "1" ]; then + echo "" + else + echo -n " " + fi + + if [ "${DEBUG}" = "1" ]; then + SHOW_OUTPUT="--show-output" + fi + + if [ "${ADMIN}" = "1" ]; then + ALLOW_ADMIN="--admin" + fi + + OUTPUT_FILE="${OUTPUT_DIR}/${TEST_FILE}.out" + ERROR_FILE="${OUTPUT_DIR}/${TEST_FILE}.err" + JSON_FILE="${OUTPUT_DIR}/${TEST_FILE}.json" + FLAMEGRAPH_FILE="${OUTPUT_DIR}/${TEST_FILE}.svg" + + FAILED= + { + ${TEST_PATH} --cli "${TEST_CLI}" --baseline-cli "${BASELINE_CLI}" --json "${JSON_FILE}" ${ALLOW_ADMIN} ${SHOW_OUTPUT} >"${OUTPUT_FILE}" 2>"${ERROR_FILE}"; + FAILED=$? + } || true + + if [ "${FAILED}" = "2" ]; then + if [ "${VERBOSE}" != "1" ]; then + echo "skipped!" + fi + + indent < "${ERROR_FILE}" + continue + elif [ "${FAILED}" != "0" ]; then + if [ "${VERBOSE}" != "1" ]; then + echo "failed!" + fi + + indent < "${ERROR_FILE}" + ANY_FAILED=1 + continue + fi + + # in verbose mode, just print the hyperfine results; otherwise, + # pull the useful information out of its json and summarize it + if [ "${VERBOSE}" = "1" ]; then + indent < "${OUTPUT_FILE}" + else + jq -r '[ .results[0].mean, .results[0].stddev, .results[1].mean, .results[1].stddev ] | @tsv' < "${JSON_FILE}" | while IFS=$'\t' read -r one_mean one_stddev two_mean two_stddev; do + one_mean=$(humanize_secs "${one_mean}") + one_stddev=$(humanize_secs "${one_stddev}") + + if [ "${two_mean}" != "" ]; then + two_mean=$(humanize_secs "${two_mean}") + two_stddev=$(humanize_secs "${two_stddev}") + + echo -n "${one_mean} ± ${one_stddev} vs ${two_mean} ± ${two_stddev}" + else + echo -n "${one_mean} ± ${one_stddev}" + fi + done + fi + + # add our metadata to the hyperfine json result + jq ". |= { \"name\": \"${TEST_NAME}\" } + ." < "${JSON_FILE}" > "${JSON_FILE}.new" && mv "${JSON_FILE}.new" "${JSON_FILE}" + + # run with flamegraph output if requested + if [ "${FLAMEGRAPH}" ]; then + PROFILER_OUTPUT_FILE="${OUTPUT_DIR}/${TEST_FILE}-profiler.out" + PROFILER_ERROR_FILE="${OUTPUT_DIR}/${TEST_FILE}-profiler.err" + + if [ "${VERBOSE}" = "1" ]; then + echo " Profiling and creating flamegraph ..." + else + echo -n " -- profiling..." + fi + + RESULT= + { ${TEST_PATH} --cli "${TEST_CLI}" --profile --flamegraph "${FLAMEGRAPH_FILE}" >>"${PROFILER_OUTPUT_FILE}" 2>>"${PROFILER_ERROR_FILE}" || RESULT=$?; } + + if [ "${VERBOSE}" = "1" ]; then + indent < "${PROFILER_OUTPUT_FILE}" + indent < "${PROFILER_ERROR_FILE}" + else + # error code 2 indicates a non-fatal error creating + # the flamegraph + if [ "${RESULT}" = "" -o "${RESULT}" = "0" ]; then + echo " done." + elif [ "${RESULT}" = "2" ]; then + echo " missing resources." + elif [ "${RESULT}" = "3" ]; then + echo " sample too small." + + indent < "${PROFILER_ERROR_FILE}" + elif [ "${RESULT}" = "4" ]; then + echo " unavailable." + else + echo " failed." + + indent < "${PROFILER_ERROR_FILE}" + ANY_FAILED=1 + fi + fi + else + echo "" + fi +done + +TIME_END=$(time_in_ms) + +if [ "$ANY_FOUND" != "1" ]; then + echo "" + echo "error: no benchmark suite \"${SUITE}\"." + echo "" + exit 1 +fi + +escape() { + echo "${1//\\/\\\\}" +} + +# combine all the individual benchmark results into a single json file +if [ "${JSON_RESULT}" != "" ]; then + if [ "${VERBOSE}" = "1" ]; then + echo "" + echo "# Writing JSON results: ${JSON_RESULT}" + fi + + SYSTEM_JSON="{ \"os\": \"${SYSTEM_OS}\", \"kernel\": \"${SYSTEM_KERNEL}\" }" + TIME_JSON="{ \"start\": ${TIME_START}, \"end\": ${TIME_END} }" + TEST_CLI_JSON="{ \"name\": \"${TEST_CLI_NAME}\", \"path\": \"$(escape "${TEST_CLI_PATH}")\", \"version\": \"${TEST_CLI_VERSION}\", \"commit\": \"${TEST_CLI_COMMIT}\" }" + BASELINE_CLI_JSON="{ \"name\": \"${BASELINE_CLI_NAME}\", \"path\": \"$(escape "${BASELINE_CLI_PATH}")\", \"version\": \"${BASELINE_CLI_VERSION}\", \"commit\": \"${BASELINE_CLI_COMMIT}\" }" + + if [ "${BASELINE_CLI}" != "" ]; then + EXECUTOR_JSON="{ \"baseline\": ${BASELINE_CLI_JSON}, \"cli\": ${TEST_CLI_JSON} }" + else + EXECUTOR_JSON="{ \"cli\": ${TEST_CLI_JSON} }" + fi + + # add our metadata to all the test results + jq -n "{ \"system\": ${SYSTEM_JSON}, \"time\": ${TIME_JSON}, \"executor\": ${EXECUTOR_JSON}, \"tests\": [inputs] }" "${OUTPUT_DIR}"/*.json > "${JSON_RESULT}" +fi + +# combine all the data into a zip if requested +if [ "${ZIP_RESULT}" != "" ]; then + if [ "${VERBOSE}" = "1" ]; then + if [ "${JSON_RESULT}" = "" ]; then echo ""; fi + echo "# Writing ZIP results: ${ZIP_RESULT}" + fi + + zip -jr "${ZIP_RESULT}" "${OUTPUT_DIR}" >/dev/null +fi + +if [ "$CLEANUP_DIR" = "1" ]; then + rm -f "${OUTPUT_DIR}"/*.out + rm -f "${OUTPUT_DIR}"/*.err + rm -f "${OUTPUT_DIR}"/*.json + rm -f "${OUTPUT_DIR}"/*.svg + rmdir "${OUTPUT_DIR}" +fi + +if [ "$ANY_FAILED" = "1" ]; then + exit 1 +fi diff --git a/tests/benchmarks/benchmark_helpers.sh b/tests/benchmarks/benchmark_helpers.sh new file mode 100644 index 00000000000..cf0cd512129 --- /dev/null +++ b/tests/benchmarks/benchmark_helpers.sh @@ -0,0 +1,589 @@ +# variables that benchmark tests can set +# + +set -eo pipefail + +# +# command-line parsing +# + +usage() { echo "usage: $(basename "$0") [--cli ] [--baseline-cli ] [--admin] [--output-style